1 /* uinfo.cc: user info (uid, gid, etc...)
3 Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003 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
20 #include <sys/cygwin.h>
29 #include "child_info.h"
34 internal_getlogin (cygheap_user &user)
36 struct passwd *pw = NULL;
37 HANDLE ptok = INVALID_HANDLE_VALUE;
39 myself->gid = UNKNOWN_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 = internal_getpwnam (user.name ()))
72 && !(pw = internal_getpwuid (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 (internal_getgrgid (pw->pw_gid)))
84 /* Set primary group to the group in /etc/passwd. */
85 if (!SetTokenInformation (ptok, TokenPrimaryGroup,
87 debug_printf ("SetTokenInformation(TokenPrimaryGroup): %E");
89 user.groups.pgsid = gsid;
92 debug_printf ("gsid not found in augmented /etc/group");
95 if (ptok != INVALID_HANDLE_VALUE)
97 (void) cygheap->user.ontherange (CH_HOME, pw);
105 if (child_proc_info && !cygheap->user.has_impersonation_tokens ())
108 if (!child_proc_info)
109 internal_getlogin (cygheap->user); /* Set the cygheap->user. */
110 /* Conditions must match those in spawn to allow starting child
111 processes with ruid != euid and rgid != egid. */
112 else if (cygheap->user.issetuid ()
113 && cygheap->user.orig_uid == cygheap->user.real_uid
114 && cygheap->user.orig_gid == cygheap->user.real_gid
115 && !cygheap->user.groups.issetgroups ())
117 cygheap->user.reimpersonate ();
121 cygheap->user.close_impersonation_tokens ();
123 cygheap->user.orig_uid = cygheap->user.real_uid = myself->uid;
124 cygheap->user.orig_gid = cygheap->user.real_gid = myself->gid;
125 cygheap->user.external_token = INVALID_HANDLE_VALUE;
126 cygheap->user.internal_token = INVALID_HANDLE_VALUE;
127 cygheap->user.current_token = INVALID_HANDLE_VALUE;
128 cygheap->user.set_orig_sid (); /* Update the original sid */
135 char *this_username=_reent_winsup ()->_username;
137 static char this_username[UNLEN + 1] NO_COPY;
140 return strcpy (this_username, cygheap->user.name ());
146 return cygheap->user.real_uid;
152 return cygheap->user.real_uid;
158 return cygheap->user.real_gid;
164 return cygheap->user.real_gid;
191 /* Not quite right - cuserid can change, getlogin can't */
198 strcpy (src, getlogin ());
203 cygheap_user::ontherange (homebodies what, struct passwd *pw)
205 LPUSER_INFO_3 ui = NULL;
206 WCHAR wuser[UNLEN + 1];
208 char homepath_env_buf[MAX_PATH + 1];
209 char homedrive_env_buf[3];
210 char *newhomedrive = NULL;
211 char *newhomepath = NULL;
214 debug_printf ("what %d, pw %p", what, pw);
219 newhomedrive = homedrive;
220 else if ((p = getenv ("HOMEDRIVE")))
224 newhomepath = homepath;
225 else if ((p = getenv ("HOMEPATH")))
228 if ((p = getenv ("HOME")))
229 debug_printf ("HOME is already in the environment %s", p);
232 if (pw && pw->pw_dir && *pw->pw_dir)
234 debug_printf ("Set HOME (from /etc/passwd) to %s", pw->pw_dir);
235 setenv ("HOME", pw->pw_dir, 1);
237 else if (!newhomedrive || !newhomepath)
238 setenv ("HOME", "/", 1);
242 char buf[MAX_PATH + 1];
243 strcpy (buf, newhomedrive);
244 strcat (buf, newhomepath);
245 cygwin_conv_to_full_posix_path (buf, home);
246 debug_printf ("Set HOME (from HOMEDRIVE/HOMEPATH) to %s", home);
247 setenv ("HOME", home, 1);
252 if (what != CH_HOME && homepath == NULL && newhomepath == NULL)
255 pw = internal_getpwnam (name ());
256 if (pw && pw->pw_dir && *pw->pw_dir)
257 cygwin_conv_to_full_win32_path (pw->pw_dir, homepath_env_buf);
260 homepath_env_buf[0] = homepath_env_buf[1] = '\0';
263 WCHAR wlogsrv[INTERNET_MAX_HOST_NAME_LENGTH + 3];
264 sys_mbstowcs (wlogsrv, logsrv (),
265 sizeof (wlogsrv) / sizeof (*wlogsrv));
266 sys_mbstowcs (wuser, winname (), sizeof (wuser) / sizeof (*wuser));
267 if (!(ret = NetUserGetInfo (wlogsrv, wuser, 3,(LPBYTE *)&ui)))
269 sys_wcstombs (homepath_env_buf, ui->usri3_home_dir, MAX_PATH);
270 if (!homepath_env_buf[0])
272 sys_wcstombs (homepath_env_buf, ui->usri3_home_dir_drive,
274 if (homepath_env_buf[0])
275 strcat (homepath_env_buf, "\\");
277 cygwin_conv_to_full_win32_path ("/", homepath_env_buf);
282 NetApiBufferFree (ui);
285 if (homepath_env_buf[1] != ':')
287 newhomedrive = almost_null;
288 newhomepath = homepath_env_buf;
292 homedrive_env_buf[0] = homepath_env_buf[0];
293 homedrive_env_buf[1] = homepath_env_buf[1];
294 homedrive_env_buf[2] = '\0';
295 newhomedrive = homedrive_env_buf;
296 newhomepath = homepath_env_buf + 2;
300 if (newhomedrive && newhomedrive != homedrive)
301 cfree_and_set (homedrive, (newhomedrive == almost_null)
302 ? almost_null : cstrdup (newhomedrive));
304 if (newhomepath && newhomepath != homepath)
305 cfree_and_set (homepath, cstrdup (newhomepath));
319 cygheap_user::test_uid (char *&what, const char *name, size_t namelen)
321 if (!what && !issetuid ())
322 what = getwinenveq (name, namelen, HEAP_STR);
327 cygheap_user::env_logsrv (const char *name, size_t namelen)
329 if (test_uid (plogsrv, name, namelen))
332 const char *mydomain = domain ();
333 const char *myname = winname ();
334 if (!mydomain || strcasematch (myname, "SYSTEM"))
337 char logsrv[INTERNET_MAX_HOST_NAME_LENGTH + 3];
338 cfree_and_set (plogsrv, almost_null);
339 if (get_logon_server (mydomain, logsrv, NULL))
340 plogsrv = cstrdup (logsrv);
345 cygheap_user::env_domain (const char *name, size_t namelen)
347 if (pwinname && test_uid (pdomain, name, namelen))
350 char username[UNLEN + 1];
351 DWORD ulen = sizeof (username);
352 char userdomain[DNLEN + 1];
353 DWORD dlen = sizeof (userdomain);
356 cfree_and_set (pwinname, almost_null);
357 cfree_and_set (pdomain, almost_null);
358 if (!LookupAccountSid (NULL, sid (), username, &ulen,
359 userdomain, &dlen, &use))
363 pwinname = cstrdup (username);
364 pdomain = cstrdup (userdomain);
370 cygheap_user::env_userprofile (const char *name, size_t namelen)
372 if (test_uid (puserprof, name, namelen))
375 char userprofile_env_buf[MAX_PATH + 1];
376 cfree_and_set (puserprof, almost_null);
377 /* FIXME: Should this just be setting a puserprofile like everything else? */
378 const char *myname = winname ();
379 if (myname && strcasematch (myname, "SYSTEM")
380 && get_registry_hive_path (sid (), userprofile_env_buf))
381 puserprof = cstrdup (userprofile_env_buf);
387 cygheap_user::env_homepath (const char *name, size_t namelen)
389 return ontherange (CH_HOMEPATH);
393 cygheap_user::env_homedrive (const char *name, size_t namelen)
395 return ontherange (CH_HOMEDRIVE);
399 cygheap_user::env_name (const char *name, size_t namelen)
401 if (!test_uid (pwinname, name, namelen))
407 pwdgrp::next_str (char c)
410 lptr = strechr (lptr, c);
417 pwdgrp::next_num (unsigned long& n)
419 char *p = next_str (':');
421 n = strtoul (p, &cp, 10);
422 return p != cp && !*cp;
426 pwdgrp::add_line (char *eptr)
431 eptr = strchr (lptr, '\n');
434 if (eptr > lptr && eptr[-1] == '\r')
440 if (curr_lines >= max_lines)
443 *pwdgrp_buf = realloc (*pwdgrp_buf, max_lines * pwdgrp_buf_elem_size);
445 if ((this->*parse) ())
452 pwdgrp::load (const char *posix_fname)
455 static const char failed[] = "failed";
456 static const char succeeded[] = "succeeded";
462 pc.check (posix_fname);
463 etc_ix = etc::init (etc_ix, pc);
465 paranoid_printf ("%s", posix_fname);
467 if (pc.error || !pc.exists () || !pc.isdisk () || pc.isdir ())
469 paranoid_printf ("strange path_conv problem");
474 HANDLE fh = CreateFile (pc, GENERIC_READ, wincap.shared (), NULL,
475 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
476 if (fh == INVALID_HANDLE_VALUE)
478 paranoid_printf ("%s CreateFile failed, %E");
483 DWORD size = GetFileSize (fh, NULL), read_bytes;
484 buf = (char *) malloc (size + 1);
485 if (!ReadFile (fh, buf, size, &read_bytes, NULL))
487 paranoid_printf ("ReadFile failed, %E");
497 buf[read_bytes] = '\0';
500 while ((eptr = add_line (eptr)))
502 debug_printf ("%s curr_lines %d", posix_fname, curr_lines);
508 debug_printf ("%s load %s", posix_fname, res);