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 ())
48 PSECURITY_DESCRIPTOR psd;
50 if (!GetTokenInformation (hProcToken, TokenPrimaryGroup,
51 &groups.pgsid, sizeof (cygsid), &siz))
52 system_printf ("GetTokenInformation (TokenPrimaryGroup), %E");
54 /* Get the SID from current process and store it in effec_cygsid */
55 if (!GetTokenInformation (hProcToken, TokenUser, &effec_cygsid,
56 sizeof (cygsid), &siz))
58 system_printf ("GetTokenInformation (TokenUser), %E");
62 /* Set token owner to the same value as token user */
63 if (!SetTokenInformation (hProcToken, TokenOwner, &effec_cygsid,
65 debug_printf ("SetTokenInformation(TokenOwner), %E");
67 /* Standard way to build a security descriptor with the usual DACL */
68 PSECURITY_ATTRIBUTES sa_buf = (PSECURITY_ATTRIBUTES) alloca (1024);
69 psd = (PSECURITY_DESCRIPTOR)
70 (sec_user_nih (sa_buf, sid()))->lpSecurityDescriptor;
72 BOOL acl_exists, dummy;
73 TOKEN_DEFAULT_DACL dacl;
74 if (GetSecurityDescriptorDacl (psd, &acl_exists, &dacl.DefaultDacl, &dummy)
75 && acl_exists && dacl.DefaultDacl)
77 /* Set the default DACL and the process DACL */
78 if (!SetTokenInformation (hProcToken, TokenDefaultDacl, &dacl,
80 system_printf ("SetTokenInformation (TokenDefaultDacl), %E");
81 if (!SetKernelObjectSecurity (hMainProc, DACL_SECURITY_INFORMATION, psd))
82 system_printf ("SetKernelObjectSecurity, %E");
85 system_printf("Cannot get dacl, %E");
89 internal_getlogin (cygheap_user &user)
91 struct passwd *pw = NULL;
93 if (wincap.has_security ())
95 cygpsid psid = user.sid ();
96 pw = internal_getpwsid (psid);
99 if (!pw && !(pw = internal_getpwnam (user.name ()))
100 && !(pw = internal_getpwuid (DEFAULT_UID)))
101 debug_printf ("user not found in augmented /etc/passwd");
104 myself->uid = pw->pw_uid;
105 myself->gid = pw->pw_gid;
106 user.set_name (pw->pw_name);
107 if (wincap.has_security ())
110 if (gsid.getfromgr (internal_getgrgid (pw->pw_gid)))
112 if (gsid != user.groups.pgsid)
114 /* Set primary group to the group in /etc/passwd. */
115 if (!SetTokenInformation (hProcToken, TokenPrimaryGroup,
117 debug_printf ("SetTokenInformation(TokenPrimaryGroup), %E");
119 user.groups.pgsid = gsid;
120 clear_procimptoken ();
124 debug_printf ("gsid not found in augmented /etc/group");
127 cygheap->user.ontherange (CH_HOME, pw);
133 if (child_proc_info && !cygheap->user.has_impersonation_tokens ())
136 if (!child_proc_info)
137 internal_getlogin (cygheap->user); /* Set the cygheap->user. */
138 /* Conditions must match those in spawn to allow starting child
139 processes with ruid != euid and rgid != egid. */
140 else if (cygheap->user.issetuid ()
141 && cygheap->user.saved_uid == cygheap->user.real_uid
142 && cygheap->user.saved_gid == cygheap->user.real_gid
143 && !cygheap->user.groups.issetgroups ())
145 cygheap->user.reimpersonate ();
149 cygheap->user.close_impersonation_tokens ();
151 cygheap->user.saved_uid = cygheap->user.real_uid = myself->uid;
152 cygheap->user.saved_gid = cygheap->user.real_gid = myself->gid;
153 cygheap->user.external_token = NO_IMPERSONATION;
154 cygheap->user.internal_token = NO_IMPERSONATION;
155 cygheap->user.curr_primary_token = NO_IMPERSONATION;
156 cygheap->user.current_token = NO_IMPERSONATION;
157 cygheap->user.set_saved_sid (); /* Update the original sid */
158 cygheap->user.reimpersonate ();
162 getlogin_r (char *name, size_t namesize)
164 char *login = getlogin ();
165 size_t len = strlen (login) + 1;
169 if (efault.faulted ())
171 strncpy (name, login, len);
178 return strcpy (_my_tls.locals.username, cygheap->user.name ());
184 return cygheap->user.real_uid;
190 return cygheap->user.real_uid;
196 return cygheap->user.real_gid;
202 return cygheap->user.real_gid;
229 /* Not quite right - cuserid can change, getlogin can't */
236 strcpy (src, getlogin ());
241 cygheap_user::ontherange (homebodies what, struct passwd *pw)
243 LPUSER_INFO_3 ui = NULL;
244 WCHAR wuser[UNLEN + 1];
246 char homepath_env_buf[CYG_MAX_PATH];
247 char homedrive_env_buf[3];
248 char *newhomedrive = NULL;
249 char *newhomepath = NULL;
252 debug_printf ("what %d, pw %p", what, pw);
257 newhomedrive = homedrive;
258 else if ((p = getenv ("HOMEDRIVE")))
262 newhomepath = homepath;
263 else if ((p = getenv ("HOMEPATH")))
266 if ((p = getenv ("HOME")))
267 debug_printf ("HOME is already in the environment %s", p);
270 if (pw && pw->pw_dir && *pw->pw_dir)
272 debug_printf ("Set HOME (from /etc/passwd) to %s", pw->pw_dir);
273 setenv ("HOME", pw->pw_dir, 1);
275 else if (!newhomedrive || !newhomepath)
276 setenv ("HOME", "/", 1);
279 char home[CYG_MAX_PATH];
280 char buf[CYG_MAX_PATH];
281 strcpy (buf, newhomedrive);
282 strcat (buf, newhomepath);
283 cygwin_conv_to_full_posix_path (buf, home);
284 debug_printf ("Set HOME (from HOMEDRIVE/HOMEPATH) to %s", home);
285 setenv ("HOME", home, 1);
290 if (what != CH_HOME && homepath == NULL && newhomepath == NULL)
293 pw = internal_getpwnam (name ());
294 if (pw && pw->pw_dir && *pw->pw_dir)
295 cygwin_conv_to_full_win32_path (pw->pw_dir, homepath_env_buf);
298 homepath_env_buf[0] = homepath_env_buf[1] = '\0';
301 WCHAR wlogsrv[INTERNET_MAX_HOST_NAME_LENGTH + 3];
302 sys_mbstowcs (wlogsrv, logsrv (),
303 sizeof (wlogsrv) / sizeof (*wlogsrv));
304 sys_mbstowcs (wuser, winname (), sizeof (wuser) / sizeof (*wuser));
305 if (!(ret = NetUserGetInfo (wlogsrv, wuser, 3, (LPBYTE *) &ui)))
307 sys_wcstombs (homepath_env_buf, CYG_MAX_PATH,
309 if (!homepath_env_buf[0])
311 sys_wcstombs (homepath_env_buf, CYG_MAX_PATH,
312 ui->usri3_home_dir_drive);
313 if (homepath_env_buf[0])
314 strcat (homepath_env_buf, "\\");
316 cygwin_conv_to_full_win32_path ("/", homepath_env_buf);
321 NetApiBufferFree (ui);
324 if (homepath_env_buf[1] != ':')
326 newhomedrive = almost_null;
327 newhomepath = homepath_env_buf;
331 homedrive_env_buf[0] = homepath_env_buf[0];
332 homedrive_env_buf[1] = homepath_env_buf[1];
333 homedrive_env_buf[2] = '\0';
334 newhomedrive = homedrive_env_buf;
335 newhomepath = homepath_env_buf + 2;
339 if (newhomedrive && newhomedrive != homedrive)
340 cfree_and_set (homedrive, (newhomedrive == almost_null)
341 ? almost_null : cstrdup (newhomedrive));
343 if (newhomepath && newhomepath != homepath)
344 cfree_and_set (homepath, cstrdup (newhomepath));
358 cygheap_user::test_uid (char *&what, const char *name, size_t namelen)
360 if (!what && !issetuid ())
361 what = getwinenveq (name, namelen, HEAP_STR);
366 cygheap_user::env_logsrv (const char *name, size_t namelen)
368 if (test_uid (plogsrv, name, namelen))
371 const char *mydomain = domain ();
372 const char *myname = winname ();
373 if (!mydomain || strcasematch (myname, "SYSTEM"))
376 char logsrv[INTERNET_MAX_HOST_NAME_LENGTH + 3];
377 cfree_and_set (plogsrv, almost_null);
378 if (get_logon_server (mydomain, logsrv, NULL, false))
379 plogsrv = cstrdup (logsrv);
384 cygheap_user::env_domain (const char *name, size_t namelen)
386 if (pwinname && test_uid (pdomain, name, namelen))
389 char username[UNLEN + 1];
390 DWORD ulen = sizeof (username);
391 char userdomain[DNLEN + 1];
392 DWORD dlen = sizeof (userdomain);
395 cfree_and_set (pwinname, almost_null);
396 cfree_and_set (pdomain, almost_null);
397 if (!LookupAccountSid (NULL, sid (), username, &ulen,
398 userdomain, &dlen, &use))
402 pwinname = cstrdup (username);
403 pdomain = cstrdup (userdomain);
409 cygheap_user::env_userprofile (const char *name, size_t namelen)
411 if (test_uid (puserprof, name, namelen))
414 char userprofile_env_buf[CYG_MAX_PATH];
415 char win_id[UNLEN + 1]; /* Large enough for SID */
417 cfree_and_set (puserprof, almost_null);
418 if (get_registry_hive_path (get_windows_id (win_id), userprofile_env_buf))
419 puserprof = cstrdup (userprofile_env_buf);
425 cygheap_user::env_homepath (const char *name, size_t namelen)
427 return ontherange (CH_HOMEPATH);
431 cygheap_user::env_homedrive (const char *name, size_t namelen)
433 return ontherange (CH_HOMEDRIVE);
437 cygheap_user::env_name (const char *name, size_t namelen)
439 if (!test_uid (pwinname, name, namelen))
445 cygheap_user::env_systemroot (const char *name, size_t namelen)
449 int size = GetWindowsDirectory (NULL, 0);
452 psystemroot = (char *) cmalloc (HEAP_STR, ++size);
453 size = GetWindowsDirectory (psystemroot, size);
461 debug_printf ("GetWindowsDirectory(), %E");
467 pwdgrp::next_str (char c)
470 lptr = strechr (lptr, c);
477 pwdgrp::next_num (unsigned long& n)
479 char *p = next_str (':');
481 n = strtoul (p, &cp, 10);
482 return p != cp && !*cp;
486 pwdgrp::add_line (char *eptr)
491 eptr = strchr (lptr, '\n');
494 if (eptr > lptr && eptr[-1] == '\r')
500 if (curr_lines >= max_lines)
503 *pwdgrp_buf = realloc (*pwdgrp_buf, max_lines * pwdgrp_buf_elem_size);
505 if ((this->*parse) ())
512 pwdgrp::load (const char *posix_fname)
515 static const char failed[] = "failed";
516 static const char succeeded[] = "succeeded";
523 pc.check (posix_fname);
524 etc_ix = etc::init (etc_ix, pc);
526 paranoid_printf ("%s", posix_fname);
528 if (pc.error || !pc.exists () || pc.isdir ())
530 paranoid_printf ("strange path_conv problem");
535 HANDLE fh = CreateFile (pc, GENERIC_READ, wincap.shared (), NULL,
536 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
537 if (fh == INVALID_HANDLE_VALUE)
539 paranoid_printf ("%s CreateFile failed, %E");
544 DWORD size = GetFileSize (fh, NULL), read_bytes;
545 buf = (char *) malloc (size + 1);
546 if (!ReadFile (fh, buf, size, &read_bytes, NULL))
548 paranoid_printf ("ReadFile failed, %E");
558 buf[read_bytes] = '\0';
560 while ((eptr = add_line (eptr)))
562 debug_printf ("%s curr_lines %d", posix_fname, curr_lines);
568 debug_printf ("%s load %s", posix_fname, res);