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 if ((pw = internal_getpwsid (tu)))
65 user.set_name (pw->pw_name);
66 /* Set token owner to the same value as token user */
67 if (!SetTokenInformation (ptok, TokenOwner, &tu, sizeof tu))
68 debug_printf ("SetTokenInformation(TokenOwner): %E");
72 if (!pw && !(pw = getpwnam (user.name ())))
73 debug_printf("user name not found in augmented /etc/passwd");
76 myself->uid = pw->pw_uid;
77 myself->gid = pw->pw_gid;
78 if (wincap.has_security ())
81 if (gsid.getfromgr (getgrgid32 (pw->pw_gid)))
83 /* Set primary group to the group in /etc/passwd. */
84 user.groups.pgsid = gsid;
85 if (!SetTokenInformation (ptok, TokenPrimaryGroup,
87 debug_printf ("SetTokenInformation(TokenPrimaryGroup): %E");
90 debug_printf ("gsid not found in augmented /etc/group");
93 if (ptok != INVALID_HANDLE_VALUE)
95 (void) cygheap->user.ontherange (CH_HOME, pw);
103 if (!child_proc_info)
104 internal_getlogin (cygheap->user); /* Set the cygheap->user. */
106 /* Real and effective uid/gid are identical on process start up. */
107 cygheap->user.orig_uid = cygheap->user.real_uid = myself->uid;
108 cygheap->user.orig_gid = cygheap->user.real_gid = myself->gid;
109 cygheap->user.set_orig_sid (); /* Update the original sid */
111 cygheap->user.token = INVALID_HANDLE_VALUE; /* No token present */
118 char *this_username=_reent_winsup ()->_username;
120 static char this_username[UNLEN + 1] NO_COPY;
123 return strcpy (this_username, cygheap->user.name ());
129 return cygheap->user.real_uid;
135 return cygheap->user.real_uid;
141 return cygheap->user.real_gid;
147 return cygheap->user.real_gid;
174 /* Not quite right - cuserid can change, getlogin can't */
181 strcpy (src, getlogin ());
186 cygheap_user::ontherange (homebodies what, struct passwd *pw)
188 LPUSER_INFO_3 ui = NULL;
189 WCHAR wuser[UNLEN + 1];
191 char homepath_env_buf[MAX_PATH + 1];
192 char homedrive_env_buf[3];
193 char *newhomedrive = NULL;
194 char *newhomepath = NULL;
197 debug_printf ("what %d, pw %p", what, pw);
202 newhomedrive = homedrive;
203 else if ((p = getenv ("HOMEDRIVE")))
207 newhomepath = homepath;
208 else if ((p = getenv ("HOMEPATH")))
211 if ((p = getenv ("HOME")))
212 debug_printf ("HOME is already in the environment %s", p);
216 pw = getpwnam (name ());
217 if (pw && pw->pw_dir && *pw->pw_dir)
219 debug_printf ("Set HOME (from /etc/passwd) to %s", pw->pw_dir);
220 setenv ("HOME", pw->pw_dir, 1);
222 else if (!newhomedrive || !newhomepath)
223 setenv ("HOME", "/", 1);
227 char buf[MAX_PATH + 1];
228 strcpy (buf, newhomedrive);
229 strcat (buf, newhomepath);
230 cygwin_conv_to_full_posix_path (buf, home);
231 debug_printf ("Set HOME (from HOMEDRIVE/HOMEPATH) to %s", home);
232 setenv ("HOME", home, 1);
237 if (what != CH_HOME && homepath == NULL && newhomepath == NULL)
240 pw = getpwnam (name ());
241 if (pw && pw->pw_dir && *pw->pw_dir)
242 cygwin_conv_to_full_win32_path (pw->pw_dir, homepath_env_buf);
245 homepath_env_buf[0] = homepath_env_buf[1] = '\0';
248 WCHAR wlogsrv[INTERNET_MAX_HOST_NAME_LENGTH + 3];
249 sys_mbstowcs (wlogsrv, logsrv (),
250 sizeof (wlogsrv) / sizeof (*wlogsrv));
251 sys_mbstowcs (wuser, winname (), sizeof (wuser) / sizeof (*wuser));
252 if (!(ret = NetUserGetInfo (wlogsrv, wuser, 3,(LPBYTE *)&ui)))
254 sys_wcstombs (homepath_env_buf, ui->usri3_home_dir, MAX_PATH);
255 if (!homepath_env_buf[0])
257 sys_wcstombs (homepath_env_buf, ui->usri3_home_dir_drive,
259 if (homepath_env_buf[0])
260 strcat (homepath_env_buf, "\\");
262 cygwin_conv_to_full_win32_path ("/", homepath_env_buf);
267 NetApiBufferFree (ui);
270 if (homepath_env_buf[1] != ':')
272 newhomedrive = almost_null;
273 newhomepath = homepath_env_buf;
277 homedrive_env_buf[0] = homepath_env_buf[0];
278 homedrive_env_buf[1] = homepath_env_buf[1];
279 homedrive_env_buf[2] = '\0';
280 newhomedrive = homedrive_env_buf;
281 newhomepath = homepath_env_buf + 2;
285 if (newhomedrive && newhomedrive != homedrive)
286 cfree_and_set (homedrive, (newhomedrive == almost_null)
287 ? almost_null : cstrdup (newhomedrive));
289 if (newhomepath && newhomepath != homepath)
290 cfree_and_set (homepath, cstrdup (newhomepath));
304 cygheap_user::test_uid (char *&what, const char *name, size_t namelen)
306 if (!what && !issetuid ())
307 what = getwinenveq (name, namelen, HEAP_STR);
312 cygheap_user::env_logsrv (const char *name, size_t namelen)
314 if (test_uid (plogsrv, name, namelen))
317 const char *mydomain = domain ();
318 const char *myname = winname ();
319 if (!mydomain || strcasematch (myname, "SYSTEM"))
322 char logsrv[INTERNET_MAX_HOST_NAME_LENGTH + 3];
323 cfree_and_set (plogsrv, almost_null);
324 if (get_logon_server (mydomain, logsrv, NULL))
325 plogsrv = cstrdup (logsrv);
330 cygheap_user::env_domain (const char *name, size_t namelen)
332 if (pwinname && test_uid (pdomain, name, namelen))
335 char username[UNLEN + 1];
336 DWORD ulen = sizeof (username);
337 char userdomain[DNLEN + 1];
338 DWORD dlen = sizeof (userdomain);
341 cfree_and_set (pwinname, almost_null);
342 cfree_and_set (pdomain, almost_null);
343 if (!LookupAccountSid (NULL, sid (), username, &ulen,
344 userdomain, &dlen, &use))
348 pwinname = cstrdup (username);
349 pdomain = cstrdup (userdomain);
355 cygheap_user::env_userprofile (const char *name, size_t namelen)
357 if (test_uid (puserprof, name, namelen))
360 char userprofile_env_buf[MAX_PATH + 1];
361 cfree_and_set (puserprof, almost_null);
362 /* FIXME: Should this just be setting a puserprofile like everything else? */
363 const char *myname = winname ();
364 if (myname && strcasematch (myname, "SYSTEM")
365 && get_registry_hive_path (sid (), userprofile_env_buf))
366 puserprof = cstrdup (userprofile_env_buf);
372 cygheap_user::env_homepath (const char *name, size_t namelen)
374 return ontherange (CH_HOMEPATH);
378 cygheap_user::env_homedrive (const char *name, size_t namelen)
380 return ontherange (CH_HOMEDRIVE);
384 cygheap_user::env_name (const char *name, size_t namelen)
386 if (!test_uid (pwinname, name, namelen))