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
21 #include <sys/cygwin.h>
30 #include "child_info.h"
35 internal_getlogin (cygheap_user &user)
37 struct passwd *pw = NULL;
38 HANDLE ptok = INVALID_HANDLE_VALUE;
40 myself->gid = DEFAULT_GID;
41 if (wincap.has_security ())
47 /* Try to get the SID either from current process and
48 store it in user.psid */
49 if (!OpenProcessToken (hMainProc, TOKEN_ADJUST_DEFAULT | TOKEN_QUERY,
51 system_printf ("OpenProcessToken(): %E");
52 else if (!GetTokenInformation (ptok, TokenUser, &tu, sizeof tu, &siz))
53 system_printf ("GetTokenInformation (TokenUser): %E");
54 else if (!(ret = user.set_sid (tu)))
55 system_printf ("Couldn't retrieve SID from access token!");
56 else if (!GetTokenInformation (ptok, TokenPrimaryGroup,
57 &user.groups.pgsid, sizeof tu, &siz))
58 system_printf ("GetTokenInformation (TokenPrimaryGroup): %E");
59 /* We must set the user name, uid and gid.
60 If we have a SID, try to get the corresponding Cygwin
61 password entry. Set user name which can be different
62 from the Windows user name */
65 pw = internal_getpwsid (tu);
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 = internal_getpwnam (user.name ()))
73 && !(pw = internal_getpwuid (DEFAULT_UID)))
74 debug_printf("user not found in augmented /etc/passwd");
77 myself->uid = pw->pw_uid;
78 myself->gid = pw->pw_gid;
79 user.set_name (pw->pw_name);
80 if (wincap.has_security ())
83 if (gsid.getfromgr (internal_getgrgid (pw->pw_gid)))
85 /* Set primary group to the group in /etc/passwd. */
86 user.groups.pgsid = gsid;
87 if (!SetTokenInformation (ptok, TokenPrimaryGroup,
89 debug_printf ("SetTokenInformation(TokenPrimaryGroup): %E");
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.token != INVALID_HANDLE_VALUE)
107 if (!child_proc_info)
108 internal_getlogin (cygheap->user); /* Set the cygheap->user. */
110 CloseHandle (cygheap->user.token);
111 cygheap->user.set_orig_sid (); /* Update the original sid */
112 cygheap->user.token = INVALID_HANDLE_VALUE; /* No token present */
114 /* Real and effective uid/gid are identical on process start up. */
115 cygheap->user.orig_uid = cygheap->user.real_uid = myself->uid;
116 cygheap->user.orig_gid = cygheap->user.real_gid = myself->gid;
123 char *this_username=_reent_winsup ()->_username;
125 static char this_username[UNLEN + 1] NO_COPY;
128 return strcpy (this_username, cygheap->user.name ());
134 return cygheap->user.real_uid;
140 return cygheap->user.real_uid;
146 return cygheap->user.real_gid;
152 return cygheap->user.real_gid;
179 /* Not quite right - cuserid can change, getlogin can't */
186 strcpy (src, getlogin ());
191 cygheap_user::ontherange (homebodies what, struct passwd *pw)
193 LPUSER_INFO_3 ui = NULL;
194 WCHAR wuser[UNLEN + 1];
196 char homepath_env_buf[MAX_PATH + 1];
197 char homedrive_env_buf[3];
198 char *newhomedrive = NULL;
199 char *newhomepath = NULL;
202 debug_printf ("what %d, pw %p", what, pw);
207 newhomedrive = homedrive;
208 else if ((p = getenv ("HOMEDRIVE")))
212 newhomepath = homepath;
213 else if ((p = getenv ("HOMEPATH")))
216 if ((p = getenv ("HOME")))
217 debug_printf ("HOME is already in the environment %s", p);
220 if (pw && pw->pw_dir && *pw->pw_dir)
222 debug_printf ("Set HOME (from /etc/passwd) to %s", pw->pw_dir);
223 setenv ("HOME", pw->pw_dir, 1);
225 else if (!newhomedrive || !newhomepath)
226 setenv ("HOME", "/", 1);
230 char buf[MAX_PATH + 1];
231 strcpy (buf, newhomedrive);
232 strcat (buf, newhomepath);
233 cygwin_conv_to_full_posix_path (buf, home);
234 debug_printf ("Set HOME (from HOMEDRIVE/HOMEPATH) to %s", home);
235 setenv ("HOME", home, 1);
240 if (what != CH_HOME && homepath == NULL && newhomepath == NULL)
243 pw = internal_getpwnam (name ());
244 if (pw && pw->pw_dir && *pw->pw_dir)
245 cygwin_conv_to_full_win32_path (pw->pw_dir, homepath_env_buf);
248 homepath_env_buf[0] = homepath_env_buf[1] = '\0';
251 WCHAR wlogsrv[INTERNET_MAX_HOST_NAME_LENGTH + 3];
252 sys_mbstowcs (wlogsrv, logsrv (),
253 sizeof (wlogsrv) / sizeof (*wlogsrv));
254 sys_mbstowcs (wuser, winname (), sizeof (wuser) / sizeof (*wuser));
255 if (!(ret = NetUserGetInfo (wlogsrv, wuser, 3,(LPBYTE *)&ui)))
257 sys_wcstombs (homepath_env_buf, ui->usri3_home_dir, MAX_PATH);
258 if (!homepath_env_buf[0])
260 sys_wcstombs (homepath_env_buf, ui->usri3_home_dir_drive,
262 if (homepath_env_buf[0])
263 strcat (homepath_env_buf, "\\");
265 cygwin_conv_to_full_win32_path ("/", 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;
288 if (newhomedrive && newhomedrive != homedrive)
289 cfree_and_set (homedrive, (newhomedrive == almost_null)
290 ? almost_null : cstrdup (newhomedrive));
292 if (newhomepath && newhomepath != homepath)
293 cfree_and_set (homepath, cstrdup (newhomepath));
307 cygheap_user::test_uid (char *&what, const char *name, size_t namelen)
309 if (!what && !issetuid ())
310 what = getwinenveq (name, namelen, HEAP_STR);
315 cygheap_user::env_logsrv (const char *name, size_t namelen)
317 if (test_uid (plogsrv, name, namelen))
320 const char *mydomain = domain ();
321 const char *myname = winname ();
322 if (!mydomain || strcasematch (myname, "SYSTEM"))
325 char logsrv[INTERNET_MAX_HOST_NAME_LENGTH + 3];
326 cfree_and_set (plogsrv, almost_null);
327 if (get_logon_server (mydomain, logsrv, NULL))
328 plogsrv = cstrdup (logsrv);
333 cygheap_user::env_domain (const char *name, size_t namelen)
335 if (pwinname && test_uid (pdomain, name, namelen))
338 char username[UNLEN + 1];
339 DWORD ulen = sizeof (username);
340 char userdomain[DNLEN + 1];
341 DWORD dlen = sizeof (userdomain);
344 cfree_and_set (pwinname, almost_null);
345 cfree_and_set (pdomain, almost_null);
346 if (!LookupAccountSid (NULL, sid (), username, &ulen,
347 userdomain, &dlen, &use))
351 pwinname = cstrdup (username);
352 pdomain = cstrdup (userdomain);
358 cygheap_user::env_userprofile (const char *name, size_t namelen)
360 if (test_uid (puserprof, name, namelen))
363 char userprofile_env_buf[MAX_PATH + 1];
364 cfree_and_set (puserprof, almost_null);
365 /* FIXME: Should this just be setting a puserprofile like everything else? */
366 const char *myname = winname ();
367 if (myname && strcasematch (myname, "SYSTEM")
368 && get_registry_hive_path (sid (), userprofile_env_buf))
369 puserprof = cstrdup (userprofile_env_buf);
375 cygheap_user::env_homepath (const char *name, size_t namelen)
377 return ontherange (CH_HOMEPATH);
381 cygheap_user::env_homedrive (const char *name, size_t namelen)
383 return ontherange (CH_HOMEDRIVE);
387 cygheap_user::env_name (const char *name, size_t namelen)
389 if (!test_uid (pwinname, name, namelen))
395 pwdgrp::next_str (char c)
399 char search[] = ":\n\0\0";
402 char *p = strpbrk (lptr, search);
405 lptr = (*p == '\n') ? p : p + 1;
412 pwdgrp::next_num (unsigned long& n)
414 char *p = next_str ();
418 n = strtoul (p, &cp, 10);
419 return p != cp && !*cp;
423 pwdgrp::add_line (char *eptr)
428 eptr = strchr (lptr, '\n');
431 if (eptr > lptr && eptr[-1] == '\r')
435 if (curr_lines >= max_lines)
438 *pwdgrp_buf = realloc (*pwdgrp_buf, max_lines * pwdgrp_buf_elem_size);
440 if ((this->*parse) ())
447 pwdgrp::load (const char *posix_fname)
450 static const char failed[] = "failed";
451 static const char succeeded[] = "succeeded";
457 pc.check (posix_fname);
458 etc_ix = etc::init (etc_ix, pc);
460 paranoid_printf ("%s", posix_fname);
462 if (pc.error || !pc.exists () || !pc.isdisk () || pc.isdir ())
464 paranoid_printf ("strange path_conv problem");
469 HANDLE fh = CreateFile (pc, GENERIC_READ, wincap.shared (), NULL,
470 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
471 if (fh == INVALID_HANDLE_VALUE)
473 paranoid_printf ("%s CreateFile failed, %E");
478 DWORD size = GetFileSize (fh, NULL), read_bytes;
479 buf = (char *) malloc (size + 1);
480 if (!ReadFile (fh, buf, size, &read_bytes, NULL))
482 paranoid_printf ("ReadFile failed, %E");
492 buf[read_bytes] = '\0';
495 while ((eptr = add_line (eptr)))
497 debug_printf ("%s curr_lines %d", posix_fname, curr_lines);
503 debug_printf ("%s load %s", posix_fname, res);