1 /* uinfo.cc: user info (uid, gid, etc...)
3 Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004 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"
33 /* Initialize the part of cygheap_user that does not depend on files.
34 The information is used in shared.cc for the user shared.
35 Final initialization occurs in uinfo_init */
39 char user_name[UNLEN + 1];
40 DWORD user_name_len = UNLEN + 1;
42 set_name (GetUserName (user_name, &user_name_len) ? user_name : "unknown");
44 if (!wincap.has_security ())
49 PSECURITY_DESCRIPTOR psd;
51 if (!OpenProcessToken (hMainProc, TOKEN_ADJUST_DEFAULT | TOKEN_QUERY,
54 system_printf ("OpenProcessToken(): %E");
57 if (!GetTokenInformation (ptok, TokenPrimaryGroup,
58 &groups.pgsid, sizeof (cygsid), &siz))
59 system_printf ("GetTokenInformation (TokenPrimaryGroup): %E");
61 /* Get the SID from current process and store it in effec_cygsid */
62 if (!GetTokenInformation (ptok, TokenUser, &effec_cygsid, sizeof (cygsid), &siz))
64 system_printf ("GetTokenInformation (TokenUser): %E");
68 /* Set token owner to the same value as token user */
69 if (!SetTokenInformation (ptok, TokenOwner, &effec_cygsid, sizeof (cygsid)))
70 debug_printf ("SetTokenInformation(TokenOwner): %E");
72 /* Standard way to build a security descriptor with the usual DACL */
74 psd = (PSECURITY_DESCRIPTOR) (sec_user_nih (sa_buf, sid()))->lpSecurityDescriptor;
76 BOOL acl_exists, dummy;
77 TOKEN_DEFAULT_DACL dacl;
78 if (GetSecurityDescriptorDacl (psd, &acl_exists,
79 &dacl.DefaultDacl, &dummy)
80 && acl_exists && dacl.DefaultDacl)
82 /* Set the default DACL and the process DACL */
83 if (!SetTokenInformation (ptok, TokenDefaultDacl, &dacl, sizeof (dacl)))
84 system_printf ("SetTokenInformation (TokenDefaultDacl): %E");
85 if (!SetKernelObjectSecurity (hMainProc, DACL_SECURITY_INFORMATION, psd))
86 system_printf ("SetKernelObjectSecurity: %E");
89 system_printf("Cannot get dacl: %E");
95 internal_getlogin (cygheap_user &user)
97 struct passwd *pw = NULL;
99 if (wincap.has_security ())
101 cygpsid psid = user.sid ();
102 pw = internal_getpwsid (psid);
105 if (!pw && !(pw = internal_getpwnam (user.name ()))
106 && !(pw = internal_getpwuid (DEFAULT_UID)))
107 debug_printf ("user not found in augmented /etc/passwd");
110 myself->uid = pw->pw_uid;
111 myself->gid = pw->pw_gid;
112 user.set_name (pw->pw_name);
113 if (wincap.has_security ())
116 if (gsid.getfromgr (internal_getgrgid (pw->pw_gid)))
119 if (gsid != user.groups.pgsid
120 && OpenProcessToken (hMainProc, TOKEN_ADJUST_DEFAULT, &ptok))
122 /* Set primary group to the group in /etc/passwd. */
123 if (!SetTokenInformation (ptok, TokenPrimaryGroup,
125 debug_printf ("SetTokenInformation(TokenPrimaryGroup): %E");
127 user.groups.pgsid = gsid;
132 debug_printf ("gsid not found in augmented /etc/group");
135 (void) cygheap->user.ontherange (CH_HOME, pw);
143 if (child_proc_info && !cygheap->user.has_impersonation_tokens ())
146 if (!child_proc_info)
147 internal_getlogin (cygheap->user); /* Set the cygheap->user. */
148 /* Conditions must match those in spawn to allow starting child
149 processes with ruid != euid and rgid != egid. */
150 else if (cygheap->user.issetuid ()
151 && cygheap->user.saved_uid == cygheap->user.real_uid
152 && cygheap->user.saved_gid == cygheap->user.real_gid
153 && !cygheap->user.groups.issetgroups ())
155 cygheap->user.reimpersonate ();
159 cygheap->user.close_impersonation_tokens ();
161 cygheap->user.saved_uid = cygheap->user.real_uid = myself->uid;
162 cygheap->user.saved_gid = cygheap->user.real_gid = myself->gid;
163 cygheap->user.external_token = INVALID_HANDLE_VALUE;
164 cygheap->user.internal_token = INVALID_HANDLE_VALUE;
165 cygheap->user.current_token = INVALID_HANDLE_VALUE;
166 cygheap->user.set_saved_sid (); /* Update the original sid */
172 return strcpy (_my_tls.locals.username, cygheap->user.name ());
178 return cygheap->user.real_uid;
184 return cygheap->user.real_uid;
190 return cygheap->user.real_gid;
196 return cygheap->user.real_gid;
223 /* Not quite right - cuserid can change, getlogin can't */
230 strcpy (src, getlogin ());
235 cygheap_user::ontherange (homebodies what, struct passwd *pw)
237 LPUSER_INFO_3 ui = NULL;
238 WCHAR wuser[UNLEN + 1];
240 char homepath_env_buf[CYG_MAX_PATH + 1];
241 char homedrive_env_buf[3];
242 char *newhomedrive = NULL;
243 char *newhomepath = NULL;
246 debug_printf ("what %d, pw %p", what, pw);
251 newhomedrive = homedrive;
252 else if ((p = getenv ("HOMEDRIVE")))
256 newhomepath = homepath;
257 else if ((p = getenv ("HOMEPATH")))
260 if ((p = getenv ("HOME")))
261 debug_printf ("HOME is already in the environment %s", p);
264 if (pw && pw->pw_dir && *pw->pw_dir)
266 debug_printf ("Set HOME (from /etc/passwd) to %s", pw->pw_dir);
267 setenv ("HOME", pw->pw_dir, 1);
269 else if (!newhomedrive || !newhomepath)
270 setenv ("HOME", "/", 1);
273 char home[CYG_MAX_PATH];
274 char buf[CYG_MAX_PATH + 1];
275 strcpy (buf, newhomedrive);
276 strcat (buf, newhomepath);
277 cygwin_conv_to_full_posix_path (buf, home);
278 debug_printf ("Set HOME (from HOMEDRIVE/HOMEPATH) to %s", home);
279 setenv ("HOME", home, 1);
284 if (what != CH_HOME && homepath == NULL && newhomepath == NULL)
287 pw = internal_getpwnam (name ());
288 if (pw && pw->pw_dir && *pw->pw_dir)
289 cygwin_conv_to_full_win32_path (pw->pw_dir, homepath_env_buf);
292 homepath_env_buf[0] = homepath_env_buf[1] = '\0';
295 WCHAR wlogsrv[INTERNET_MAX_HOST_NAME_LENGTH + 3];
296 sys_mbstowcs (wlogsrv, logsrv (),
297 sizeof (wlogsrv) / sizeof (*wlogsrv));
298 sys_mbstowcs (wuser, winname (), sizeof (wuser) / sizeof (*wuser));
299 if (!(ret = NetUserGetInfo (wlogsrv, wuser, 3, (LPBYTE *) &ui)))
301 sys_wcstombs (homepath_env_buf, ui->usri3_home_dir, CYG_MAX_PATH);
302 if (!homepath_env_buf[0])
304 sys_wcstombs (homepath_env_buf, ui->usri3_home_dir_drive,
306 if (homepath_env_buf[0])
307 strcat (homepath_env_buf, "\\");
309 cygwin_conv_to_full_win32_path ("/", homepath_env_buf);
314 NetApiBufferFree (ui);
317 if (homepath_env_buf[1] != ':')
319 newhomedrive = almost_null;
320 newhomepath = homepath_env_buf;
324 homedrive_env_buf[0] = homepath_env_buf[0];
325 homedrive_env_buf[1] = homepath_env_buf[1];
326 homedrive_env_buf[2] = '\0';
327 newhomedrive = homedrive_env_buf;
328 newhomepath = homepath_env_buf + 2;
332 if (newhomedrive && newhomedrive != homedrive)
333 cfree_and_set (homedrive, (newhomedrive == almost_null)
334 ? almost_null : cstrdup (newhomedrive));
336 if (newhomepath && newhomepath != homepath)
337 cfree_and_set (homepath, cstrdup (newhomepath));
351 cygheap_user::test_uid (char *&what, const char *name, size_t namelen)
353 if (!what && !issetuid ())
354 what = getwinenveq (name, namelen, HEAP_STR);
359 cygheap_user::env_logsrv (const char *name, size_t namelen)
361 if (test_uid (plogsrv, name, namelen))
364 const char *mydomain = domain ();
365 const char *myname = winname ();
366 if (!mydomain || strcasematch (myname, "SYSTEM"))
369 char logsrv[INTERNET_MAX_HOST_NAME_LENGTH + 3];
370 cfree_and_set (plogsrv, almost_null);
371 if (get_logon_server (mydomain, logsrv, NULL))
372 plogsrv = cstrdup (logsrv);
377 cygheap_user::env_domain (const char *name, size_t namelen)
379 if (pwinname && test_uid (pdomain, name, namelen))
382 char username[UNLEN + 1];
383 DWORD ulen = sizeof (username);
384 char userdomain[DNLEN + 1];
385 DWORD dlen = sizeof (userdomain);
388 cfree_and_set (pwinname, almost_null);
389 cfree_and_set (pdomain, almost_null);
390 if (!LookupAccountSid (NULL, sid (), username, &ulen,
391 userdomain, &dlen, &use))
395 pwinname = cstrdup (username);
396 pdomain = cstrdup (userdomain);
402 cygheap_user::env_userprofile (const char *name, size_t namelen)
404 if (test_uid (puserprof, name, namelen))
407 char userprofile_env_buf[CYG_MAX_PATH + 1];
408 cfree_and_set (puserprof, almost_null);
409 /* FIXME: Should this just be setting a puserprofile like everything else? */
410 const char *myname = winname ();
411 if (myname && strcasematch (myname, "SYSTEM")
412 && get_registry_hive_path (sid (), userprofile_env_buf))
413 puserprof = cstrdup (userprofile_env_buf);
419 cygheap_user::env_homepath (const char *name, size_t namelen)
421 return ontherange (CH_HOMEPATH);
425 cygheap_user::env_homedrive (const char *name, size_t namelen)
427 return ontherange (CH_HOMEDRIVE);
431 cygheap_user::env_name (const char *name, size_t namelen)
433 if (!test_uid (pwinname, name, namelen))
439 pwdgrp::next_str (char c)
442 lptr = strechr (lptr, c);
449 pwdgrp::next_num (unsigned long& n)
451 char *p = next_str (':');
453 n = strtoul (p, &cp, 10);
454 return p != cp && !*cp;
458 pwdgrp::add_line (char *eptr)
463 eptr = strchr (lptr, '\n');
466 if (eptr > lptr && eptr[-1] == '\r')
472 if (curr_lines >= max_lines)
475 *pwdgrp_buf = realloc (*pwdgrp_buf, max_lines * pwdgrp_buf_elem_size);
477 if ((this->*parse) ())
484 pwdgrp::load (const char *posix_fname)
487 static const char failed[] = "failed";
488 static const char succeeded[] = "succeeded";
495 pc.check (posix_fname);
496 etc_ix = etc::init (etc_ix, pc);
498 paranoid_printf ("%s", posix_fname);
500 if (pc.error || !pc.exists () || !pc.isdisk () || pc.isdir ())
502 paranoid_printf ("strange path_conv problem");
507 HANDLE fh = CreateFile (pc, GENERIC_READ, wincap.shared (), NULL,
508 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
509 if (fh == INVALID_HANDLE_VALUE)
511 paranoid_printf ("%s CreateFile failed, %E");
516 DWORD size = GetFileSize (fh, NULL), read_bytes;
517 buf = (char *) malloc (size + 1);
518 if (!ReadFile (fh, buf, size, &read_bytes, NULL))
520 paranoid_printf ("ReadFile failed, %E");
530 buf[read_bytes] = '\0';
532 while ((eptr = add_line (eptr)))
534 debug_printf ("%s curr_lines %d", posix_fname, curr_lines);
540 debug_printf ("%s load %s", posix_fname, res);