1 /* uinfo.cc: user info (uid, gid, etc...)
3 Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
5 This file is part of Cygwin.
7 This software is a copyrighted work licensed under the terms of the
8 Cygwin license. Please consult the file "CYGWIN_LICENSE" for
21 #include <sys/cygwin.h>
30 #include "child_info.h"
34 internal_getlogin (cygheap_user &user)
36 struct passwd *pw = NULL;
37 HANDLE ptok = INVALID_HANDLE_VALUE;
39 myself->gid = DEFAULT_GID;
40 if (wincap.has_security ())
46 /* Try to get the SID either from current process and
47 store it in user.psid */
48 if (!OpenProcessToken (hMainProc, TOKEN_ADJUST_DEFAULT | TOKEN_QUERY,
50 system_printf ("OpenProcessToken(): %E");
51 else if (!GetTokenInformation (ptok, TokenUser, &tu, sizeof tu, &siz))
52 system_printf ("GetTokenInformation (TokenUser): %E");
53 else if (!(ret = user.set_sid (tu)))
54 system_printf ("Couldn't retrieve SID from access token!");
55 else if (!GetTokenInformation (ptok, TokenPrimaryGroup,
56 &user.groups.pgsid, sizeof tu, &siz))
57 system_printf ("GetTokenInformation (TokenPrimaryGroup): %E");
58 /* We must set the user name, uid and gid.
59 If we have a SID, try to get the corresponding Cygwin
60 password entry. Set user name which can be different
61 from the Windows user name */
64 pw = internal_getpwsid (tu);
65 /* Set token owner to the same value as token user */
66 if (!SetTokenInformation (ptok, TokenOwner, &tu, sizeof tu))
67 debug_printf ("SetTokenInformation(TokenOwner): %E");
71 if (!pw && !(pw = getpwnam (user.name ()))
72 && !(pw = getpwuid32 (DEFAULT_UID)))
73 debug_printf("user not found in augmented /etc/passwd");
76 myself->uid = pw->pw_uid;
77 myself->gid = pw->pw_gid;
78 user.set_name (pw->pw_name);
79 if (wincap.has_security ())
82 if (gsid.getfromgr (getgrgid32 (pw->pw_gid)))
84 /* Set primary group to the group in /etc/passwd. */
85 user.groups.pgsid = gsid;
86 if (!SetTokenInformation (ptok, TokenPrimaryGroup,
88 debug_printf ("SetTokenInformation(TokenPrimaryGroup): %E");
91 debug_printf ("gsid not found in augmented /etc/group");
94 if (ptok != INVALID_HANDLE_VALUE)
96 (void) cygheap->user.ontherange (CH_HOME, pw);
104 if (!child_proc_info)
105 internal_getlogin (cygheap->user); /* Set the cygheap->user. */
107 /* Real and effective uid/gid are identical on process start up. */
108 cygheap->user.orig_uid = cygheap->user.real_uid = myself->uid;
109 cygheap->user.orig_gid = cygheap->user.real_gid = myself->gid;
110 cygheap->user.set_orig_sid (); /* Update the original sid */
112 cygheap->user.token = INVALID_HANDLE_VALUE; /* No token present */
119 char *this_username=_reent_winsup ()->_username;
121 static char this_username[UNLEN + 1] NO_COPY;
124 return strcpy (this_username, cygheap->user.name ());
130 return cygheap->user.real_uid;
136 return cygheap->user.real_uid;
142 return cygheap->user.real_gid;
148 return cygheap->user.real_gid;
175 /* Not quite right - cuserid can change, getlogin can't */
182 strcpy (src, getlogin ());
187 cygheap_user::ontherange (homebodies what, struct passwd *pw)
189 LPUSER_INFO_3 ui = NULL;
190 WCHAR wuser[UNLEN + 1];
192 char homepath_env_buf[MAX_PATH + 1];
193 char homedrive_env_buf[3];
194 char *newhomedrive = NULL;
195 char *newhomepath = NULL;
198 debug_printf ("what %d, pw %p", what, pw);
203 newhomedrive = homedrive;
204 else if ((p = getenv ("HOMEDRIVE")))
208 newhomepath = homepath;
209 else if ((p = getenv ("HOMEPATH")))
212 if ((p = getenv ("HOME")))
213 debug_printf ("HOME is already in the environment %s", p);
217 pw = getpwnam (name ());
218 if (pw && pw->pw_dir && *pw->pw_dir)
220 debug_printf ("Set HOME (from /etc/passwd) to %s", pw->pw_dir);
221 setenv ("HOME", pw->pw_dir, 1);
223 else if (!newhomedrive || !newhomepath)
224 setenv ("HOME", "/", 1);
228 char buf[MAX_PATH + 1];
229 strcpy (buf, newhomedrive);
230 strcat (buf, newhomepath);
231 cygwin_conv_to_full_posix_path (buf, home);
232 debug_printf ("Set HOME (from HOMEDRIVE/HOMEPATH) to %s", home);
233 setenv ("HOME", home, 1);
238 if (what != CH_HOME && homepath == NULL && newhomepath == NULL)
241 pw = getpwnam (name ());
242 if (pw && pw->pw_dir && *pw->pw_dir)
243 cygwin_conv_to_full_win32_path (pw->pw_dir, homepath_env_buf);
246 homepath_env_buf[0] = homepath_env_buf[1] = '\0';
249 WCHAR wlogsrv[INTERNET_MAX_HOST_NAME_LENGTH + 3];
250 sys_mbstowcs (wlogsrv, logsrv (),
251 sizeof (wlogsrv) / sizeof (*wlogsrv));
252 sys_mbstowcs (wuser, winname (), sizeof (wuser) / sizeof (*wuser));
253 if (!(ret = NetUserGetInfo (wlogsrv, wuser, 3,(LPBYTE *)&ui)))
255 sys_wcstombs (homepath_env_buf, ui->usri3_home_dir, MAX_PATH);
256 if (!homepath_env_buf[0])
258 sys_wcstombs (homepath_env_buf, ui->usri3_home_dir_drive,
260 if (homepath_env_buf[0])
261 strcat (homepath_env_buf, "\\");
263 cygwin_conv_to_full_win32_path ("/", homepath_env_buf);
268 NetApiBufferFree (ui);
271 if (homepath_env_buf[1] != ':')
273 newhomedrive = almost_null;
274 newhomepath = homepath_env_buf;
278 homedrive_env_buf[0] = homepath_env_buf[0];
279 homedrive_env_buf[1] = homepath_env_buf[1];
280 homedrive_env_buf[2] = '\0';
281 newhomedrive = homedrive_env_buf;
282 newhomepath = homepath_env_buf + 2;
286 if (newhomedrive && newhomedrive != homedrive)
287 cfree_and_set (homedrive, (newhomedrive == almost_null)
288 ? almost_null : cstrdup (newhomedrive));
290 if (newhomepath && newhomepath != homepath)
291 cfree_and_set (homepath, cstrdup (newhomepath));
305 cygheap_user::test_uid (char *&what, const char *name, size_t namelen)
307 if (!what && !issetuid ())
308 what = getwinenveq (name, namelen, HEAP_STR);
313 cygheap_user::env_logsrv (const char *name, size_t namelen)
315 if (test_uid (plogsrv, name, namelen))
318 const char *mydomain = domain ();
319 const char *myname = winname ();
320 if (!mydomain || strcasematch (myname, "SYSTEM"))
323 char logsrv[INTERNET_MAX_HOST_NAME_LENGTH + 3];
324 cfree_and_set (plogsrv, almost_null);
325 if (get_logon_server (mydomain, logsrv, NULL))
326 plogsrv = cstrdup (logsrv);
331 cygheap_user::env_domain (const char *name, size_t namelen)
333 if (pwinname && test_uid (pdomain, name, namelen))
336 char username[UNLEN + 1];
337 DWORD ulen = sizeof (username);
338 char userdomain[DNLEN + 1];
339 DWORD dlen = sizeof (userdomain);
342 cfree_and_set (pwinname, almost_null);
343 cfree_and_set (pdomain, almost_null);
344 if (!LookupAccountSid (NULL, sid (), username, &ulen,
345 userdomain, &dlen, &use))
349 pwinname = cstrdup (username);
350 pdomain = cstrdup (userdomain);
356 cygheap_user::env_userprofile (const char *name, size_t namelen)
358 if (test_uid (puserprof, name, namelen))
361 char userprofile_env_buf[MAX_PATH + 1];
362 cfree_and_set (puserprof, almost_null);
363 /* FIXME: Should this just be setting a puserprofile like everything else? */
364 const char *myname = winname ();
365 if (myname && strcasematch (myname, "SYSTEM")
366 && get_registry_hive_path (sid (), userprofile_env_buf))
367 puserprof = cstrdup (userprofile_env_buf);
373 cygheap_user::env_homepath (const char *name, size_t namelen)
375 return ontherange (CH_HOMEPATH);
379 cygheap_user::env_homedrive (const char *name, size_t namelen)
381 return ontherange (CH_HOMEDRIVE);
385 cygheap_user::env_name (const char *name, size_t namelen)
387 if (!test_uid (pwinname, name, namelen))