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");
118 if (!SetTokenInformation (hProcImpToken, TokenPrimaryGroup,
120 debug_printf ("SetTokenInformation(TokenPrimaryGroup), %E");
122 user.groups.pgsid = gsid;
126 debug_printf ("gsid not found in augmented /etc/group");
129 (void) cygheap->user.ontherange (CH_HOME, pw);
137 if (child_proc_info && !cygheap->user.has_impersonation_tokens ())
140 if (!child_proc_info)
141 internal_getlogin (cygheap->user); /* Set the cygheap->user. */
142 /* Conditions must match those in spawn to allow starting child
143 processes with ruid != euid and rgid != egid. */
144 else if (cygheap->user.issetuid ()
145 && cygheap->user.saved_uid == cygheap->user.real_uid
146 && cygheap->user.saved_gid == cygheap->user.real_gid
147 && !cygheap->user.groups.issetgroups ())
149 cygheap->user.reimpersonate ();
153 cygheap->user.close_impersonation_tokens ();
155 cygheap->user.saved_uid = cygheap->user.real_uid = myself->uid;
156 cygheap->user.saved_gid = cygheap->user.real_gid = myself->gid;
157 cygheap->user.external_token = NO_IMPERSONATION;
158 cygheap->user.internal_token = NO_IMPERSONATION;
159 cygheap->user.curr_primary_token = NO_IMPERSONATION;
160 cygheap->user.current_token = NO_IMPERSONATION;
161 cygheap->user.set_saved_sid (); /* Update the original sid */
162 cygheap->user.reimpersonate ();
166 getlogin_r (char *name, size_t namesize)
168 char *login = getlogin ();
169 size_t len = strlen (login) + 1;
173 if (efault.faulted ())
175 strncpy (name, login, len);
182 return strcpy (_my_tls.locals.username, cygheap->user.name ());
188 return cygheap->user.real_uid;
194 return cygheap->user.real_uid;
200 return cygheap->user.real_gid;
206 return cygheap->user.real_gid;
233 /* Not quite right - cuserid can change, getlogin can't */
240 strcpy (src, getlogin ());
245 cygheap_user::ontherange (homebodies what, struct passwd *pw)
247 LPUSER_INFO_3 ui = NULL;
248 WCHAR wuser[UNLEN + 1];
250 char homepath_env_buf[CYG_MAX_PATH];
251 char homedrive_env_buf[3];
252 char *newhomedrive = NULL;
253 char *newhomepath = NULL;
256 debug_printf ("what %d, pw %p", what, pw);
261 newhomedrive = homedrive;
262 else if ((p = getenv ("HOMEDRIVE")))
266 newhomepath = homepath;
267 else if ((p = getenv ("HOMEPATH")))
270 if ((p = getenv ("HOME")))
271 debug_printf ("HOME is already in the environment %s", p);
274 if (pw && pw->pw_dir && *pw->pw_dir)
276 debug_printf ("Set HOME (from /etc/passwd) to %s", pw->pw_dir);
277 setenv ("HOME", pw->pw_dir, 1);
279 else if (!newhomedrive || !newhomepath)
280 setenv ("HOME", "/", 1);
283 char home[CYG_MAX_PATH];
284 char buf[CYG_MAX_PATH];
285 strcpy (buf, newhomedrive);
286 strcat (buf, newhomepath);
287 cygwin_conv_to_full_posix_path (buf, home);
288 debug_printf ("Set HOME (from HOMEDRIVE/HOMEPATH) to %s", home);
289 setenv ("HOME", home, 1);
294 if (what != CH_HOME && homepath == NULL && newhomepath == NULL)
297 pw = internal_getpwnam (name ());
298 if (pw && pw->pw_dir && *pw->pw_dir)
299 cygwin_conv_to_full_win32_path (pw->pw_dir, homepath_env_buf);
302 homepath_env_buf[0] = homepath_env_buf[1] = '\0';
305 WCHAR wlogsrv[INTERNET_MAX_HOST_NAME_LENGTH + 3];
306 sys_mbstowcs (wlogsrv, logsrv (),
307 sizeof (wlogsrv) / sizeof (*wlogsrv));
308 sys_mbstowcs (wuser, winname (), sizeof (wuser) / sizeof (*wuser));
309 if (!(ret = NetUserGetInfo (wlogsrv, wuser, 3, (LPBYTE *) &ui)))
311 sys_wcstombs (homepath_env_buf, ui->usri3_home_dir, CYG_MAX_PATH);
312 if (!homepath_env_buf[0])
314 sys_wcstombs (homepath_env_buf, ui->usri3_home_dir_drive,
316 if (homepath_env_buf[0])
317 strcat (homepath_env_buf, "\\");
319 cygwin_conv_to_full_win32_path ("/", homepath_env_buf);
324 NetApiBufferFree (ui);
327 if (homepath_env_buf[1] != ':')
329 newhomedrive = almost_null;
330 newhomepath = homepath_env_buf;
334 homedrive_env_buf[0] = homepath_env_buf[0];
335 homedrive_env_buf[1] = homepath_env_buf[1];
336 homedrive_env_buf[2] = '\0';
337 newhomedrive = homedrive_env_buf;
338 newhomepath = homepath_env_buf + 2;
342 if (newhomedrive && newhomedrive != homedrive)
343 cfree_and_set (homedrive, (newhomedrive == almost_null)
344 ? almost_null : cstrdup (newhomedrive));
346 if (newhomepath && newhomepath != homepath)
347 cfree_and_set (homepath, cstrdup (newhomepath));
361 cygheap_user::test_uid (char *&what, const char *name, size_t namelen)
363 if (!what && !issetuid ())
364 what = getwinenveq (name, namelen, HEAP_STR);
369 cygheap_user::env_logsrv (const char *name, size_t namelen)
371 if (test_uid (plogsrv, name, namelen))
374 const char *mydomain = domain ();
375 const char *myname = winname ();
376 if (!mydomain || strcasematch (myname, "SYSTEM"))
379 char logsrv[INTERNET_MAX_HOST_NAME_LENGTH + 3];
380 cfree_and_set (plogsrv, almost_null);
381 if (get_logon_server (mydomain, logsrv, NULL))
382 plogsrv = cstrdup (logsrv);
387 cygheap_user::env_domain (const char *name, size_t namelen)
389 if (pwinname && test_uid (pdomain, name, namelen))
392 char username[UNLEN + 1];
393 DWORD ulen = sizeof (username);
394 char userdomain[DNLEN + 1];
395 DWORD dlen = sizeof (userdomain);
398 cfree_and_set (pwinname, almost_null);
399 cfree_and_set (pdomain, almost_null);
400 if (!LookupAccountSid (NULL, sid (), username, &ulen,
401 userdomain, &dlen, &use))
405 pwinname = cstrdup (username);
406 pdomain = cstrdup (userdomain);
412 cygheap_user::env_userprofile (const char *name, size_t namelen)
414 if (test_uid (puserprof, name, namelen))
417 char userprofile_env_buf[CYG_MAX_PATH];
418 char win_id[UNLEN + 1]; /* Large enough for SID */
420 cfree_and_set (puserprof, almost_null);
421 if (get_registry_hive_path (get_windows_id (win_id), userprofile_env_buf))
422 puserprof = cstrdup (userprofile_env_buf);
428 cygheap_user::env_homepath (const char *name, size_t namelen)
430 return ontherange (CH_HOMEPATH);
434 cygheap_user::env_homedrive (const char *name, size_t namelen)
436 return ontherange (CH_HOMEDRIVE);
440 cygheap_user::env_name (const char *name, size_t namelen)
442 if (!test_uid (pwinname, name, namelen))
448 cygheap_user::env_systemroot (const char *name, size_t namelen)
452 int size = GetWindowsDirectory (NULL, 0);
455 psystemroot = (char *) cmalloc (HEAP_STR, ++size);
456 size = GetWindowsDirectory (psystemroot, size);
464 debug_printf ("GetWindowsDirectory(), %E");
470 pwdgrp::next_str (char c)
473 lptr = strechr (lptr, c);
480 pwdgrp::next_num (unsigned long& n)
482 char *p = next_str (':');
484 n = strtoul (p, &cp, 10);
485 return p != cp && !*cp;
489 pwdgrp::add_line (char *eptr)
494 eptr = strchr (lptr, '\n');
497 if (eptr > lptr && eptr[-1] == '\r')
503 if (curr_lines >= max_lines)
506 *pwdgrp_buf = realloc (*pwdgrp_buf, max_lines * pwdgrp_buf_elem_size);
508 if ((this->*parse) ())
515 pwdgrp::load (const char *posix_fname)
518 static const char failed[] = "failed";
519 static const char succeeded[] = "succeeded";
526 pc.check (posix_fname);
527 etc_ix = etc::init (etc_ix, pc);
529 paranoid_printf ("%s", posix_fname);
531 if (pc.error || !pc.exists () || pc.isdir ())
533 paranoid_printf ("strange path_conv problem");
538 HANDLE fh = CreateFile (pc, GENERIC_READ, wincap.shared (), NULL,
539 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
540 if (fh == INVALID_HANDLE_VALUE)
542 paranoid_printf ("%s CreateFile failed, %E");
547 DWORD size = GetFileSize (fh, NULL), read_bytes;
548 buf = (char *) malloc (size + 1);
549 if (!ReadFile (fh, buf, size, &read_bytes, NULL))
551 paranoid_printf ("ReadFile failed, %E");
561 buf[read_bytes] = '\0';
563 while ((eptr = add_line (eptr)))
565 debug_printf ("%s curr_lines %d", posix_fname, curr_lines);
571 debug_printf ("%s load %s", posix_fname, res);