1 /* uinfo.cc: user info (uid, gid, etc...)
3 Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
4 2006, 2007, 2008 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
19 #include <sys/cygwin.h>
26 #include "shared_info.h"
28 #include "child_info.h"
34 /* Initialize the part of cygheap_user that does not depend on files.
35 The information is used in shared.cc for the user shared.
36 Final initialization occurs in uinfo_init */
40 char user_name[UNLEN + 1];
41 DWORD user_name_len = UNLEN + 1;
43 set_name (GetUserName (user_name, &user_name_len) ? user_name : "unknown");
46 PSECURITY_DESCRIPTOR psd;
48 if (!GetTokenInformation (hProcToken, TokenPrimaryGroup,
49 &groups.pgsid, sizeof (cygsid), &siz))
50 system_printf ("GetTokenInformation (TokenPrimaryGroup), %E");
52 /* Get the SID from current process and store it in effec_cygsid */
53 if (!GetTokenInformation (hProcToken, TokenUser, &effec_cygsid,
54 sizeof (cygsid), &siz))
56 system_printf ("GetTokenInformation (TokenUser), %E");
60 /* Set token owner to the same value as token user */
61 if (!SetTokenInformation (hProcToken, TokenOwner, &effec_cygsid,
63 debug_printf ("SetTokenInformation(TokenOwner), %E");
65 /* Standard way to build a security descriptor with the usual DACL */
66 PSECURITY_ATTRIBUTES sa_buf = (PSECURITY_ATTRIBUTES) alloca (1024);
67 psd = (PSECURITY_DESCRIPTOR)
68 (sec_user_nih (sa_buf, sid()))->lpSecurityDescriptor;
70 BOOL acl_exists, dummy;
71 TOKEN_DEFAULT_DACL dacl;
72 if (GetSecurityDescriptorDacl (psd, &acl_exists, &dacl.DefaultDacl, &dummy)
73 && 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 ((status = NtSetSecurityObject (hMainProc, DACL_SECURITY_INFORMATION,
83 system_printf ("NtSetSecurityObject, %lx", status);
86 system_printf("Cannot get dacl, %E");
90 internal_getlogin (cygheap_user &user)
92 struct passwd *pw = NULL;
94 cygpsid psid = user.sid ();
95 pw = internal_getpwsid (psid);
97 if (!pw && !(pw = internal_getpwnam (user.name ()))
98 && !(pw = internal_getpwuid (DEFAULT_UID)))
99 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 (gsid.getfromgr (internal_getgrgid (pw->pw_gid)))
109 if (gsid != user.groups.pgsid)
111 /* Set primary group to the group in /etc/passwd. */
112 if (!SetTokenInformation (hProcToken, TokenPrimaryGroup,
114 debug_printf ("SetTokenInformation(TokenPrimaryGroup), %E");
116 user.groups.pgsid = gsid;
117 clear_procimptoken ();
121 debug_printf ("gsid not found in augmented /etc/group");
123 cygheap->user.ontherange (CH_HOME, pw);
129 if (child_proc_info && !cygheap->user.has_impersonation_tokens ())
132 if (!child_proc_info)
133 internal_getlogin (cygheap->user); /* Set the cygheap->user. */
134 /* Conditions must match those in spawn to allow starting child
135 processes with ruid != euid and rgid != egid. */
136 else if (cygheap->user.issetuid ()
137 && cygheap->user.saved_uid == cygheap->user.real_uid
138 && cygheap->user.saved_gid == cygheap->user.real_gid
139 && !cygheap->user.groups.issetgroups ())
141 cygheap->user.reimpersonate ();
145 cygheap->user.close_impersonation_tokens ();
147 cygheap->user.saved_uid = cygheap->user.real_uid = myself->uid;
148 cygheap->user.saved_gid = cygheap->user.real_gid = myself->gid;
149 cygheap->user.external_token = NO_IMPERSONATION;
150 cygheap->user.internal_token = NO_IMPERSONATION;
151 cygheap->user.curr_primary_token = NO_IMPERSONATION;
152 cygheap->user.curr_imp_token = NO_IMPERSONATION;
153 cygheap->user.set_saved_sid (); /* Update the original sid */
154 cygheap->user.reimpersonate ();
158 getlogin_r (char *name, size_t namesize)
160 char *login = getlogin ();
161 size_t len = strlen (login) + 1;
165 if (efault.faulted ())
167 strncpy (name, login, len);
174 return strcpy (_my_tls.locals.username, cygheap->user.name ());
180 return cygheap->user.real_uid;
186 return cygheap->user.real_uid;
192 return cygheap->user.real_gid;
198 return cygheap->user.real_gid;
225 /* Not quite right - cuserid can change, getlogin can't */
232 strcpy (src, getlogin ());
237 cygheap_user::ontherange (homebodies what, struct passwd *pw)
239 LPUSER_INFO_3 ui = NULL;
240 WCHAR wuser[UNLEN + 1];
242 char homedrive_env_buf[3];
243 char *newhomedrive = NULL;
244 char *newhomepath = NULL;
247 debug_printf ("what %d, pw %p", what, pw);
252 newhomedrive = homedrive;
253 else if ((p = getenv ("HOMEDRIVE")))
257 newhomepath = homepath;
258 else if ((p = getenv ("HOMEPATH")))
261 if ((p = getenv ("HOME")))
262 debug_printf ("HOME is already in the environment %s", p);
265 if (pw && pw->pw_dir && *pw->pw_dir)
267 debug_printf ("Set HOME (from /etc/passwd) to %s", pw->pw_dir);
268 setenv ("HOME", pw->pw_dir, 1);
270 else if (!newhomedrive || !newhomepath)
271 setenv ("HOME", "/", 1);
274 char *home = tp.c_get ();
275 char *buf = tp.c_get ();
276 strcpy (buf, newhomedrive);
277 strcat (buf, newhomepath);
278 cygwin_conv_path (CCP_WIN_A_TO_POSIX | CCP_ABSOLUTE, buf, home,
280 debug_printf ("Set HOME (from HOMEDRIVE/HOMEPATH) to %s", home);
281 setenv ("HOME", home, 1);
286 if (what != CH_HOME && homepath == NULL && newhomepath == NULL)
288 char *homepath_env_buf = tp.c_get ();
290 pw = internal_getpwnam (name ());
291 if (pw && pw->pw_dir && *pw->pw_dir)
292 cygwin_conv_path (CCP_POSIX_TO_WIN_A, pw->pw_dir, homepath_env_buf,
296 homepath_env_buf[0] = homepath_env_buf[1] = '\0';
299 WCHAR wlogsrv[INTERNET_MAX_HOST_NAME_LENGTH + 3];
300 sys_mbstowcs (wlogsrv, sizeof (wlogsrv) / sizeof (*wlogsrv),
302 sys_mbstowcs (wuser, sizeof (wuser) / sizeof (*wuser), winname ());
303 if (!(ret = NetUserGetInfo (wlogsrv, wuser, 3, (LPBYTE *) &ui)))
305 sys_wcstombs (homepath_env_buf, NT_MAX_PATH,
307 if (!homepath_env_buf[0])
309 sys_wcstombs (homepath_env_buf, NT_MAX_PATH,
310 ui->usri3_home_dir_drive);
311 if (homepath_env_buf[0])
312 strcat (homepath_env_buf, "\\");
314 cygwin_conv_path (CCP_POSIX_TO_WIN_A | CCP_ABSOLUTE,
315 "/", homepath_env_buf, NT_MAX_PATH);
320 NetApiBufferFree (ui);
323 if (homepath_env_buf[1] != ':')
325 newhomedrive = almost_null;
326 newhomepath = homepath_env_buf;
330 homedrive_env_buf[0] = homepath_env_buf[0];
331 homedrive_env_buf[1] = homepath_env_buf[1];
332 homedrive_env_buf[2] = '\0';
333 newhomedrive = homedrive_env_buf;
334 newhomepath = homepath_env_buf + 2;
338 if (newhomedrive && newhomedrive != homedrive)
339 cfree_and_set (homedrive, (newhomedrive == almost_null)
340 ? almost_null : cstrdup (newhomedrive));
342 if (newhomepath && newhomepath != homepath)
343 cfree_and_set (homepath, cstrdup (newhomepath));
357 cygheap_user::test_uid (char *&what, const char *name, size_t namelen)
359 if (!what && !issetuid ())
360 what = getwinenveq (name, namelen, HEAP_STR);
365 cygheap_user::env_logsrv (const char *name, size_t namelen)
367 if (test_uid (plogsrv, name, namelen))
370 const char *mydomain = domain ();
371 const char *myname = winname ();
372 if (!mydomain || ascii_strcasematch (myname, "SYSTEM"))
375 WCHAR wdomain[MAX_DOMAIN_NAME_LEN + 1];
376 WCHAR wlogsrv[INTERNET_MAX_HOST_NAME_LENGTH + 3];
377 sys_mbstowcs (wdomain, MAX_DOMAIN_NAME_LEN + 1, mydomain);
378 cfree_and_set (plogsrv, almost_null);
379 if (get_logon_server (wdomain, wlogsrv, false))
380 sys_wcstombs_alloc (&plogsrv, HEAP_STR, wlogsrv);
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 wchar_t *rel_path)
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;
532 !(path = (PWCHAR) malloc ((wcslen (cygwin_shared->installation_root)
533 + wcslen (rel_path) + 1) * sizeof (WCHAR))))
535 paranoid_printf ("malloc (%W) failed", rel_path);
538 wcpcpy (wcpcpy (path, cygwin_shared->installation_root), rel_path);
539 RtlInitUnicodeString (&upath, path);
541 InitializeObjectAttributes (&attr, &upath, OBJ_CASE_INSENSITIVE, NULL, NULL);
542 etc_ix = etc::init (etc_ix, &attr);
544 paranoid_printf ("%S", &upath);
546 status = NtOpenFile (&fh, FILE_READ_DATA, &attr, &io,
547 FILE_SHARE_VALID_FLAGS, 0);
548 if (!NT_SUCCESS (status))
550 paranoid_printf ("NtOpenFile(%S) failed, status %p", &upath, status);
553 status = NtQueryInformationFile (fh, &io, &fsi, sizeof fsi,
554 FileStandardInformation);
555 if (!NT_SUCCESS (status))
557 paranoid_printf ("NtQueryInformationFile(%S) failed, status %p",
561 /* FIXME: Should we test for HighPart set? If so, the
562 passwd or group file is way beyond what we can handle. */
563 /* FIXME 2: It's still ugly that we keep the file in memory.
564 Big organizations have naturally large passwd files. */
565 buf = (char *) malloc (fsi.EndOfFile.LowPart + 1);
568 paranoid_printf ("malloc (%d) failed", fsi.EndOfFile.LowPart);
571 status = NtReadFile (fh, NULL, NULL, NULL, &io, buf,
572 fsi.EndOfFile.LowPart, &off, NULL);
573 if (!NT_SUCCESS (status))
575 paranoid_printf ("NtReadFile(%S) failed, status %p", &upath, status);
579 buf[fsi.EndOfFile.LowPart] = '\0';
580 for (char *eptr = buf; (eptr = add_line (eptr)); )
582 debug_printf ("%W curr_lines %d", rel_path, curr_lines);
588 debug_printf ("%W load %s", rel_path, res);