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;
38 if (wincap.has_security ())
40 HANDLE ptok = INVALID_HANDLE_VALUE;
45 /* Try to get the SID either from current process and
46 store it in user.psid */
47 if (!OpenProcessToken (hMainProc, TOKEN_ADJUST_DEFAULT | TOKEN_QUERY,
49 system_printf ("OpenProcessToken(): %E\n");
50 else if (!GetTokenInformation (ptok, TokenUser, &tu, sizeof tu, &siz))
51 system_printf ("GetTokenInformation(): %E");
52 else if (!(ret = user.set_sid (tu)))
53 system_printf ("Couldn't retrieve SID from access token!");
54 /* We must set the user name, uid and gid.
55 If we have a SID, try to get the corresponding Cygwin
56 password entry. Set user name which can be different
57 from the Windows user name */
63 for (int pidx = 0; (pw = internal_getpwent (pidx)); ++pidx)
64 if (psid.getfrompw (pw) && EqualSid (user.sid (), psid))
66 user.set_name (pw->pw_name);
67 struct __group32 *gr = getgrgid32 (pw->pw_gid);
69 if (!gsid.getfromgr (gr))
74 /* Set token owner to the same value as token user and
75 primary group to the group in /etc/passwd. */
76 if (!SetTokenInformation (ptok, TokenOwner, &tu, sizeof tu))
77 debug_printf ("SetTokenInformation(TokenOwner): %E");
78 if (gsid && !SetTokenInformation (ptok, TokenPrimaryGroup,
80 debug_printf ("SetTokenInformation(TokenPrimaryGroup): %E");
83 if (ptok != INVALID_HANDLE_VALUE)
88 pw = getpwnam (user.name ());
92 myself->uid = pw->pw_uid;
93 myself->gid = pw->pw_gid;
97 myself->uid = DEFAULT_UID;
98 myself->gid = DEFAULT_GID;
101 (void) cygheap->user.ontherange (CH_HOME, pw);
109 if (!child_proc_info)
110 internal_getlogin (cygheap->user); /* Set the cygheap->user. */
112 /* Real and effective uid/gid are identical on process start up. */
113 cygheap->user.orig_uid = cygheap->user.real_uid = myself->uid;
114 cygheap->user.orig_gid = cygheap->user.real_gid = myself->gid;
115 cygheap->user.set_orig_sid(); /* Update the original sid */
117 cygheap->user.token = INVALID_HANDLE_VALUE; /* No token present */
124 char *this_username=_reent_winsup ()->_username;
126 static char this_username[UNLEN + 1] NO_COPY;
129 return strcpy (this_username, cygheap->user.name ());
135 return cygheap->user.real_uid;
141 return cygheap->user.real_uid;
147 return cygheap->user.real_gid;
153 return cygheap->user.real_gid;
180 /* Not quite right - cuserid can change, getlogin can't */
187 strcpy (src, getlogin ());
192 cygheap_user::ontherange (homebodies what, struct passwd *pw)
194 LPUSER_INFO_3 ui = NULL;
195 WCHAR wuser[UNLEN + 1];
197 char homepath_env_buf[MAX_PATH + 1];
198 char homedrive_env_buf[3];
199 char *newhomedrive = NULL;
200 char *newhomepath = NULL;
203 debug_printf ("what %d, pw %p", what, pw);
207 if ((p = getenv ("HOMEDRIVE")))
210 if ((p = getenv ("HOMEPATH")))
213 if ((p = getenv ("HOME")))
214 debug_printf ("HOME is already in the environment %s", p);
218 pw = getpwnam (name ());
219 if (pw && pw->pw_dir && *pw->pw_dir)
221 setenv ("HOME", pw->pw_dir, 1);
222 debug_printf ("Set HOME (from /etc/passwd) to %s", pw->pw_dir);
224 else if (newhomedrive && newhomepath)
227 char buf[MAX_PATH + 1];
228 strcpy (buf, newhomedrive);
229 strcat (buf, newhomepath);
230 cygwin_conv_to_full_posix_path (buf, home);
231 setenv ("HOME", home, 1);
232 debug_printf ("Set HOME (from HOMEDRIVE/HOMEPATH) to %s", home);
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)))
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, "\\");
262 else if (!GetSystemDirectory (homepath_env_buf, MAX_PATH))
263 strcpy (homepath_env_buf, "c:\\");
264 else if ((p = strchr (homepath_env_buf, '\\')))
270 NetApiBufferFree (ui);
273 if (homepath_env_buf[1] != ':')
275 newhomedrive = almost_null;
276 newhomepath = homepath_env_buf;
280 homedrive_env_buf[0] = homepath_env_buf[0];
281 homedrive_env_buf[1] = homepath_env_buf[1];
282 homedrive_env_buf[2] = '\0';
283 newhomedrive = homedrive_env_buf;
284 newhomepath = homepath_env_buf + 2;
289 cfree_and_set (homedrive, (newhomedrive == almost_null)
290 ? almost_null : cstrdup (newhomedrive));
293 cfree_and_set (homepath, cstrdup (newhomepath));
307 cygheap_user::test_uid (char *&what, const char *name, size_t namelen)
311 if (orig_uid == myself->uid)
312 what = getwinenveq (name, namelen, HEAP_STR);
317 cygheap_user::env_logsrv (const char *name, size_t namelen)
319 if (test_uid (plogsrv, name, namelen))
322 const char *mydomain = domain ();
323 const char *myname = winname ();
324 if (!mydomain || strcasematch (myname, "SYSTEM"))
327 char logsrv[INTERNET_MAX_HOST_NAME_LENGTH + 3];
328 cfree_and_set (plogsrv, almost_null);
329 if (get_logon_server (mydomain, logsrv, NULL))
330 plogsrv = cstrdup (logsrv);
335 cygheap_user::env_domain (const char *name, size_t namelen)
337 if (pwinname && test_uid (pdomain, name, namelen))
340 char username[UNLEN + 1];
341 DWORD ulen = sizeof (username);
342 char userdomain[DNLEN + 1];
343 DWORD dlen = sizeof (userdomain);
346 cfree_and_set (pwinname, almost_null);
347 cfree_and_set (pdomain, almost_null);
348 if (!LookupAccountSid (NULL, sid (), username, &ulen,
349 userdomain, &dlen, &use))
353 pwinname = cstrdup (username);
354 pdomain = cstrdup (userdomain);
360 cygheap_user::env_userprofile (const char *name, size_t namelen)
362 if (test_uid (puserprof, name, namelen))
365 char userprofile_env_buf[MAX_PATH + 1];
366 cfree_and_set (puserprof, almost_null);
367 /* FIXME: Should this just be setting a puserprofile like everything else? */
368 const char *myname = winname ();
369 if (myname && strcasematch (myname, "SYSTEM")
370 && get_registry_hive_path (sid (), userprofile_env_buf))
371 puserprof = cstrdup (userprofile_env_buf);
377 cygheap_user::env_homepath (const char *name, size_t namelen)
379 return ontherange (CH_HOMEPATH);
383 cygheap_user::env_homedrive (const char *name, size_t namelen)
385 return ontherange (CH_HOMEDRIVE);
389 cygheap_user::env_name (const char *name, size_t namelen)
391 if (!test_uid (pwinname, name, namelen))