1 /* uinfo.cc: user info (uid, gid, etc...)
3 Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
4 2006, 2007 Red Hat, Inc.
6 This file is part of Cygwin.
8 This software is a copyrighted work licensed under the terms of the
9 Cygwin license. Please consult the file "CYGWIN_LICENSE" for
21 #include <sys/cygwin.h>
30 #include "child_info.h"
36 /* Initialize the part of cygheap_user that does not depend on files.
37 The information is used in shared.cc for the user shared.
38 Final initialization occurs in uinfo_init */
42 char user_name[UNLEN + 1];
43 DWORD user_name_len = UNLEN + 1;
45 set_name (GetUserName (user_name, &user_name_len) ? user_name : "unknown");
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)
79 /* Set the default DACL and the process DACL */
80 if (!SetTokenInformation (hProcToken, TokenDefaultDacl, &dacl,
82 system_printf ("SetTokenInformation (TokenDefaultDacl), %E");
83 if ((status = NtSetSecurityObject (hMainProc, DACL_SECURITY_INFORMATION,
85 system_printf ("NtSetSecurityObject, %lx", status);
88 system_printf("Cannot get dacl, %E");
92 internal_getlogin (cygheap_user &user)
94 struct passwd *pw = NULL;
96 cygpsid psid = user.sid ();
97 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");
106 myself->uid = pw->pw_uid;
107 myself->gid = pw->pw_gid;
108 user.set_name (pw->pw_name);
109 if (gsid.getfromgr (internal_getgrgid (pw->pw_gid)))
111 if (gsid != user.groups.pgsid)
113 /* Set primary group to the group in /etc/passwd. */
114 if (!SetTokenInformation (hProcToken, TokenPrimaryGroup,
116 debug_printf ("SetTokenInformation(TokenPrimaryGroup), %E");
118 user.groups.pgsid = gsid;
119 clear_procimptoken ();
123 debug_printf ("gsid not found in augmented /etc/group");
125 cygheap->user.ontherange (CH_HOME, pw);
131 if (child_proc_info && !cygheap->user.has_impersonation_tokens ())
134 if (!child_proc_info)
135 internal_getlogin (cygheap->user); /* Set the cygheap->user. */
136 /* Conditions must match those in spawn to allow starting child
137 processes with ruid != euid and rgid != egid. */
138 else if (cygheap->user.issetuid ()
139 && cygheap->user.saved_uid == cygheap->user.real_uid
140 && cygheap->user.saved_gid == cygheap->user.real_gid
141 && !cygheap->user.groups.issetgroups ())
143 cygheap->user.reimpersonate ();
147 cygheap->user.close_impersonation_tokens ();
149 cygheap->user.saved_uid = cygheap->user.real_uid = myself->uid;
150 cygheap->user.saved_gid = cygheap->user.real_gid = myself->gid;
151 cygheap->user.external_token = NO_IMPERSONATION;
152 cygheap->user.internal_token = NO_IMPERSONATION;
153 cygheap->user.curr_primary_token = NO_IMPERSONATION;
154 cygheap->user.curr_imp_token = NO_IMPERSONATION;
155 cygheap->user.set_saved_sid (); /* Update the original sid */
156 cygheap->user.reimpersonate ();
160 getlogin_r (char *name, size_t namesize)
162 char *login = getlogin ();
163 size_t len = strlen (login) + 1;
167 if (efault.faulted ())
169 strncpy (name, login, len);
176 return strcpy (_my_tls.locals.username, cygheap->user.name ());
182 return cygheap->user.real_uid;
188 return cygheap->user.real_uid;
194 return cygheap->user.real_gid;
200 return cygheap->user.real_gid;
227 /* Not quite right - cuserid can change, getlogin can't */
234 strcpy (src, getlogin ());
239 cygheap_user::ontherange (homebodies what, struct passwd *pw)
241 LPUSER_INFO_3 ui = NULL;
242 WCHAR wuser[UNLEN + 1];
244 char homedrive_env_buf[3];
245 char *newhomedrive = NULL;
246 char *newhomepath = NULL;
249 debug_printf ("what %d, pw %p", what, pw);
254 newhomedrive = homedrive;
255 else if ((p = getenv ("HOMEDRIVE")))
259 newhomepath = homepath;
260 else if ((p = getenv ("HOMEPATH")))
263 if ((p = getenv ("HOME")))
264 debug_printf ("HOME is already in the environment %s", p);
267 if (pw && pw->pw_dir && *pw->pw_dir)
269 debug_printf ("Set HOME (from /etc/passwd) to %s", pw->pw_dir);
270 setenv ("HOME", pw->pw_dir, 1);
272 else if (!newhomedrive || !newhomepath)
273 setenv ("HOME", "/", 1);
276 char *home = tp.c_get ();
277 char *buf = tp.c_get ();
278 strcpy (buf, newhomedrive);
279 strcat (buf, newhomepath);
280 cygwin_conv_path (CCP_WIN_A_TO_POSIX | CCP_ABSOLUTE, buf, home,
282 debug_printf ("Set HOME (from HOMEDRIVE/HOMEPATH) to %s", home);
283 setenv ("HOME", home, 1);
288 if (what != CH_HOME && homepath == NULL && newhomepath == NULL)
290 char *homepath_env_buf = tp.c_get ();
292 pw = internal_getpwnam (name ());
293 if (pw && pw->pw_dir && *pw->pw_dir)
294 cygwin_conv_path (CCP_POSIX_TO_WIN_A, 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, sizeof (wlogsrv) / sizeof (*wlogsrv),
304 sys_mbstowcs (wuser, sizeof (wuser) / sizeof (*wuser), winname ());
305 if (!(ret = NetUserGetInfo (wlogsrv, wuser, 3, (LPBYTE *) &ui)))
307 sys_wcstombs (homepath_env_buf, NT_MAX_PATH,
309 if (!homepath_env_buf[0])
311 sys_wcstombs (homepath_env_buf, NT_MAX_PATH,
312 ui->usri3_home_dir_drive);
313 if (homepath_env_buf[0])
314 strcat (homepath_env_buf, "\\");
316 cygwin_conv_path (CCP_POSIX_TO_WIN_A | CCP_ABSOLUTE,
317 "/", homepath_env_buf, NT_MAX_PATH);
322 NetApiBufferFree (ui);
325 if (homepath_env_buf[1] != ':')
327 newhomedrive = almost_null;
328 newhomepath = homepath_env_buf;
332 homedrive_env_buf[0] = homepath_env_buf[0];
333 homedrive_env_buf[1] = homepath_env_buf[1];
334 homedrive_env_buf[2] = '\0';
335 newhomedrive = homedrive_env_buf;
336 newhomepath = homepath_env_buf + 2;
340 if (newhomedrive && newhomedrive != homedrive)
341 cfree_and_set (homedrive, (newhomedrive == almost_null)
342 ? almost_null : cstrdup (newhomedrive));
344 if (newhomepath && newhomepath != homepath)
345 cfree_and_set (homepath, cstrdup (newhomepath));
359 cygheap_user::test_uid (char *&what, const char *name, size_t namelen)
361 if (!what && !issetuid ())
362 what = getwinenveq (name, namelen, HEAP_STR);
367 cygheap_user::env_logsrv (const char *name, size_t namelen)
369 if (test_uid (plogsrv, name, namelen))
372 const char *mydomain = domain ();
373 const char *myname = winname ();
374 if (!mydomain || ascii_strcasematch (myname, "SYSTEM"))
377 char logsrv[INTERNET_MAX_HOST_NAME_LENGTH + 3];
378 cfree_and_set (plogsrv, almost_null);
379 if (get_logon_server (mydomain, logsrv, NULL, false))
380 plogsrv = cstrdup (logsrv);
385 cygheap_user::env_domain (const char *name, size_t namelen)
387 if (pwinname && test_uid (pdomain, name, namelen))
390 char username[UNLEN + 1];
391 DWORD ulen = sizeof (username);
392 char userdomain[DNLEN + 1];
393 DWORD dlen = sizeof (userdomain);
396 cfree_and_set (pwinname, almost_null);
397 cfree_and_set (pdomain, almost_null);
398 if (!LookupAccountSid (NULL, sid (), username, &ulen,
399 userdomain, &dlen, &use))
403 pwinname = cstrdup (username);
404 pdomain = cstrdup (userdomain);
410 cygheap_user::env_userprofile (const char *name, size_t namelen)
412 if (test_uid (puserprof, name, namelen))
415 WCHAR userprofile_env_buf[NT_MAX_PATH];
416 WCHAR win_id[UNLEN + 1]; /* Large enough for SID */
418 cfree_and_set (puserprof, almost_null);
419 if (get_registry_hive_path (get_windows_id (win_id), userprofile_env_buf))
420 sys_wcstombs_alloc (&puserprof, HEAP_STR, userprofile_env_buf);
426 cygheap_user::env_homepath (const char *name, size_t namelen)
428 return ontherange (CH_HOMEPATH);
432 cygheap_user::env_homedrive (const char *name, size_t namelen)
434 return ontherange (CH_HOMEDRIVE);
438 cygheap_user::env_name (const char *name, size_t namelen)
440 if (!test_uid (pwinname, name, namelen))
446 cygheap_user::env_systemroot (const char *name, size_t namelen)
450 int size = GetWindowsDirectory (NULL, 0);
453 psystemroot = (char *) cmalloc_abort (HEAP_STR, ++size);
454 size = GetWindowsDirectory (psystemroot, size);
462 debug_printf ("GetWindowsDirectory(), %E");
468 pwdgrp::next_str (char c)
471 lptr = strechr (lptr, c);
478 pwdgrp::next_num (unsigned long& n)
480 char *p = next_str (':');
482 n = strtoul (p, &cp, 10);
483 return p != cp && !*cp;
487 pwdgrp::add_line (char *eptr)
492 eptr = strchr (lptr, '\n');
495 if (eptr > lptr && eptr[-1] == '\r')
501 if (curr_lines >= max_lines)
504 *pwdgrp_buf = realloc (*pwdgrp_buf, max_lines * pwdgrp_buf_elem_size);
506 if ((this->*parse) ())
513 pwdgrp::load (const char *posix_fname)
515 static const char failed[] = "failed";
516 static const char succeeded[] = "succeeded";
517 const char *res = failed;
519 LARGE_INTEGER off = { QuadPart:0LL };
522 OBJECT_ATTRIBUTES attr;
524 FILE_STANDARD_INFORMATION fsi;
531 pc.check (posix_fname);
532 etc_ix = etc::init (etc_ix, pc.get_nt_native_path ());
534 paranoid_printf ("%s", posix_fname);
536 if (pc.error || !pc.exists () || pc.isdir ())
538 paranoid_printf ("strange path_conv problem");
541 status = NtOpenFile (&fh, FILE_READ_DATA,
542 pc.get_object_attr (attr, sec_none_nih), &io,
543 FILE_SHARE_VALID_FLAGS, 0);
544 if (!NT_SUCCESS (status))
546 paranoid_printf ("NtOpenFile(%S) failed, status %p",
547 pc.get_nt_native_path (), status);
550 status = NtQueryInformationFile (fh, &io, &fsi, sizeof fsi,
551 FileStandardInformation);
552 if (!NT_SUCCESS (status))
554 paranoid_printf ("NtQueryInformationFile(%S) failed, status %p",
555 pc.get_nt_native_path (), status);
558 /* FIXME: Should we test for HighPart set? If so, the
559 passwd or group file is way beyond what we can handle. */
560 /* FIXME 2: It's still ugly that we keep the file in memory.
561 Big organizations have naturally large passwd files. */
562 buf = (char *) malloc (fsi.EndOfFile.LowPart + 1);
565 paranoid_printf ("malloc (%d) failed", fsi.EndOfFile.LowPart);
568 status = NtReadFile (fh, NULL, NULL, NULL, &io, buf,
569 fsi.EndOfFile.LowPart, &off, NULL);
570 if (!NT_SUCCESS (status))
572 paranoid_printf ("NtReadFile(%S) failed, status %p",
573 pc.get_nt_native_path (), status);
577 buf[fsi.EndOfFile.LowPart] = '\0';
579 while ((eptr = add_line (eptr)))
581 debug_printf ("%s curr_lines %d", posix_fname, curr_lines);
587 debug_printf ("%s load %s", posix_fname, res);