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
18 #include <sys/cygwin.h>
26 #include "child_info.h"
32 /* Initialize the part of cygheap_user that does not depend on files.
33 The information is used in shared.cc for the user shared.
34 Final initialization occurs in uinfo_init */
38 char user_name[UNLEN + 1];
39 DWORD user_name_len = UNLEN + 1;
41 set_name (GetUserName (user_name, &user_name_len) ? user_name : "unknown");
44 PSECURITY_DESCRIPTOR psd;
46 if (!GetTokenInformation (hProcToken, TokenPrimaryGroup,
47 &groups.pgsid, sizeof (cygsid), &siz))
48 system_printf ("GetTokenInformation (TokenPrimaryGroup), %E");
50 /* Get the SID from current process and store it in effec_cygsid */
51 if (!GetTokenInformation (hProcToken, TokenUser, &effec_cygsid,
52 sizeof (cygsid), &siz))
54 system_printf ("GetTokenInformation (TokenUser), %E");
58 /* Set token owner to the same value as token user */
59 if (!SetTokenInformation (hProcToken, TokenOwner, &effec_cygsid,
61 debug_printf ("SetTokenInformation(TokenOwner), %E");
63 /* Standard way to build a security descriptor with the usual DACL */
64 PSECURITY_ATTRIBUTES sa_buf = (PSECURITY_ATTRIBUTES) alloca (1024);
65 psd = (PSECURITY_DESCRIPTOR)
66 (sec_user_nih (sa_buf, sid()))->lpSecurityDescriptor;
68 BOOL acl_exists, dummy;
69 TOKEN_DEFAULT_DACL dacl;
70 if (GetSecurityDescriptorDacl (psd, &acl_exists, &dacl.DefaultDacl, &dummy)
71 && acl_exists && dacl.DefaultDacl)
75 /* Set the default DACL and the process DACL */
76 if (!SetTokenInformation (hProcToken, TokenDefaultDacl, &dacl,
78 system_printf ("SetTokenInformation (TokenDefaultDacl), %E");
79 if ((status = NtSetSecurityObject (hMainProc, DACL_SECURITY_INFORMATION,
81 system_printf ("NtSetSecurityObject, %lx", status);
84 system_printf("Cannot get dacl, %E");
88 internal_getlogin (cygheap_user &user)
90 struct passwd *pw = NULL;
92 cygpsid psid = user.sid ();
93 pw = internal_getpwsid (psid);
95 if (!pw && !(pw = internal_getpwnam (user.name ()))
96 && !(pw = internal_getpwuid (DEFAULT_UID)))
97 debug_printf ("user not found in augmented /etc/passwd");
102 myself->uid = pw->pw_uid;
103 myself->gid = pw->pw_gid;
104 user.set_name (pw->pw_name);
105 if (gsid.getfromgr (internal_getgrgid (pw->pw_gid)))
107 if (gsid != user.groups.pgsid)
109 /* Set primary group to the group in /etc/passwd. */
110 if (!SetTokenInformation (hProcToken, TokenPrimaryGroup,
112 debug_printf ("SetTokenInformation(TokenPrimaryGroup), %E");
114 user.groups.pgsid = gsid;
115 clear_procimptoken ();
119 debug_printf ("gsid not found in augmented /etc/group");
121 cygheap->user.ontherange (CH_HOME, pw);
127 if (child_proc_info && !cygheap->user.has_impersonation_tokens ())
130 if (!child_proc_info)
131 internal_getlogin (cygheap->user); /* Set the cygheap->user. */
132 /* Conditions must match those in spawn to allow starting child
133 processes with ruid != euid and rgid != egid. */
134 else if (cygheap->user.issetuid ()
135 && cygheap->user.saved_uid == cygheap->user.real_uid
136 && cygheap->user.saved_gid == cygheap->user.real_gid
137 && !cygheap->user.groups.issetgroups ())
139 cygheap->user.reimpersonate ();
143 cygheap->user.close_impersonation_tokens ();
145 cygheap->user.saved_uid = cygheap->user.real_uid = myself->uid;
146 cygheap->user.saved_gid = cygheap->user.real_gid = myself->gid;
147 cygheap->user.external_token = NO_IMPERSONATION;
148 cygheap->user.internal_token = NO_IMPERSONATION;
149 cygheap->user.curr_primary_token = NO_IMPERSONATION;
150 cygheap->user.curr_imp_token = NO_IMPERSONATION;
151 cygheap->user.set_saved_sid (); /* Update the original sid */
152 cygheap->user.reimpersonate ();
156 getlogin_r (char *name, size_t namesize)
158 char *login = getlogin ();
159 size_t len = strlen (login) + 1;
163 if (efault.faulted ())
165 strncpy (name, login, len);
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 homedrive_env_buf[3];
241 char *newhomedrive = NULL;
242 char *newhomepath = NULL;
245 debug_printf ("what %d, pw %p", what, pw);
250 newhomedrive = homedrive;
251 else if ((p = getenv ("HOMEDRIVE")))
255 newhomepath = homepath;
256 else if ((p = getenv ("HOMEPATH")))
259 if ((p = getenv ("HOME")))
260 debug_printf ("HOME is already in the environment %s", p);
263 if (pw && pw->pw_dir && *pw->pw_dir)
265 debug_printf ("Set HOME (from /etc/passwd) to %s", pw->pw_dir);
266 setenv ("HOME", pw->pw_dir, 1);
268 else if (!newhomedrive || !newhomepath)
269 setenv ("HOME", "/", 1);
272 char *home = tp.c_get ();
273 char *buf = tp.c_get ();
274 strcpy (buf, newhomedrive);
275 strcat (buf, newhomepath);
276 cygwin_conv_path (CCP_WIN_A_TO_POSIX | CCP_ABSOLUTE, 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)
286 char *homepath_env_buf = tp.c_get ();
288 pw = internal_getpwnam (name ());
289 if (pw && pw->pw_dir && *pw->pw_dir)
290 cygwin_conv_path (CCP_POSIX_TO_WIN_A, pw->pw_dir, homepath_env_buf,
294 homepath_env_buf[0] = homepath_env_buf[1] = '\0';
297 WCHAR wlogsrv[INTERNET_MAX_HOST_NAME_LENGTH + 3];
298 sys_mbstowcs (wlogsrv, sizeof (wlogsrv) / sizeof (*wlogsrv),
300 sys_mbstowcs (wuser, sizeof (wuser) / sizeof (*wuser), winname ());
301 if (!(ret = NetUserGetInfo (wlogsrv, wuser, 3, (LPBYTE *) &ui)))
303 sys_wcstombs (homepath_env_buf, NT_MAX_PATH,
305 if (!homepath_env_buf[0])
307 sys_wcstombs (homepath_env_buf, NT_MAX_PATH,
308 ui->usri3_home_dir_drive);
309 if (homepath_env_buf[0])
310 strcat (homepath_env_buf, "\\");
312 cygwin_conv_path (CCP_POSIX_TO_WIN_A | CCP_ABSOLUTE,
313 "/", homepath_env_buf, NT_MAX_PATH);
318 NetApiBufferFree (ui);
321 if (homepath_env_buf[1] != ':')
323 newhomedrive = almost_null;
324 newhomepath = homepath_env_buf;
328 homedrive_env_buf[0] = homepath_env_buf[0];
329 homedrive_env_buf[1] = homepath_env_buf[1];
330 homedrive_env_buf[2] = '\0';
331 newhomedrive = homedrive_env_buf;
332 newhomepath = homepath_env_buf + 2;
336 if (newhomedrive && newhomedrive != homedrive)
337 cfree_and_set (homedrive, (newhomedrive == almost_null)
338 ? almost_null : cstrdup (newhomedrive));
340 if (newhomepath && newhomepath != homepath)
341 cfree_and_set (homepath, cstrdup (newhomepath));
355 cygheap_user::test_uid (char *&what, const char *name, size_t namelen)
357 if (!what && !issetuid ())
358 what = getwinenveq (name, namelen, HEAP_STR);
363 cygheap_user::env_logsrv (const char *name, size_t namelen)
365 if (test_uid (plogsrv, name, namelen))
368 const char *mydomain = domain ();
369 const char *myname = winname ();
370 if (!mydomain || ascii_strcasematch (myname, "SYSTEM"))
373 WCHAR wdomain[MAX_DOMAIN_NAME_LEN + 1];
374 WCHAR wlogsrv[INTERNET_MAX_HOST_NAME_LENGTH + 3];
375 sys_mbstowcs (wdomain, MAX_DOMAIN_NAME_LEN + 1, mydomain);
376 cfree_and_set (plogsrv, almost_null);
377 if (get_logon_server (wdomain, wlogsrv, false))
378 sys_wcstombs_alloc (&plogsrv, HEAP_STR, wlogsrv);
383 cygheap_user::env_domain (const char *name, size_t namelen)
385 if (pwinname && test_uid (pdomain, name, namelen))
388 char username[UNLEN + 1];
389 DWORD ulen = sizeof (username);
390 char userdomain[DNLEN + 1];
391 DWORD dlen = sizeof (userdomain);
394 cfree_and_set (pwinname, almost_null);
395 cfree_and_set (pdomain, almost_null);
396 if (!LookupAccountSid (NULL, sid (), username, &ulen,
397 userdomain, &dlen, &use))
401 pwinname = cstrdup (username);
402 pdomain = cstrdup (userdomain);
408 cygheap_user::env_userprofile (const char *name, size_t namelen)
410 if (test_uid (puserprof, name, namelen))
413 WCHAR userprofile_env_buf[NT_MAX_PATH];
414 WCHAR win_id[UNLEN + 1]; /* Large enough for SID */
416 cfree_and_set (puserprof, almost_null);
417 if (get_registry_hive_path (get_windows_id (win_id), userprofile_env_buf))
418 sys_wcstombs_alloc (&puserprof, HEAP_STR, userprofile_env_buf);
424 cygheap_user::env_homepath (const char *name, size_t namelen)
426 return ontherange (CH_HOMEPATH);
430 cygheap_user::env_homedrive (const char *name, size_t namelen)
432 return ontherange (CH_HOMEDRIVE);
436 cygheap_user::env_name (const char *name, size_t namelen)
438 if (!test_uid (pwinname, name, namelen))
444 cygheap_user::env_systemroot (const char *name, size_t namelen)
448 int size = GetWindowsDirectory (NULL, 0);
451 psystemroot = (char *) cmalloc_abort (HEAP_STR, ++size);
452 size = GetWindowsDirectory (psystemroot, size);
460 debug_printf ("GetWindowsDirectory(), %E");
466 pwdgrp::next_str (char c)
469 lptr = strechr (lptr, c);
476 pwdgrp::next_num (unsigned long& n)
478 char *p = next_str (':');
480 n = strtoul (p, &cp, 10);
481 return p != cp && !*cp;
485 pwdgrp::add_line (char *eptr)
490 eptr = strchr (lptr, '\n');
493 if (eptr > lptr && eptr[-1] == '\r')
499 if (curr_lines >= max_lines)
502 *pwdgrp_buf = realloc (*pwdgrp_buf, max_lines * pwdgrp_buf_elem_size);
504 if ((this->*parse) ())
511 pwdgrp::load (const char *posix_fname)
513 static const char failed[] = "failed";
514 static const char succeeded[] = "succeeded";
515 const char *res = failed;
517 LARGE_INTEGER off = { QuadPart:0LL };
520 OBJECT_ATTRIBUTES attr;
522 FILE_STANDARD_INFORMATION fsi;
529 pc.check (posix_fname);
530 etc_ix = etc::init (etc_ix, pc.get_nt_native_path ());
532 paranoid_printf ("%s", posix_fname);
534 if (pc.error || !pc.exists () || pc.isdir ())
536 paranoid_printf ("strange path_conv problem");
539 status = NtOpenFile (&fh, FILE_READ_DATA,
540 pc.get_object_attr (attr, sec_none_nih), &io,
541 FILE_SHARE_VALID_FLAGS, 0);
542 if (!NT_SUCCESS (status))
544 paranoid_printf ("NtOpenFile(%S) failed, status %p",
545 pc.get_nt_native_path (), status);
548 status = NtQueryInformationFile (fh, &io, &fsi, sizeof fsi,
549 FileStandardInformation);
550 if (!NT_SUCCESS (status))
552 paranoid_printf ("NtQueryInformationFile(%S) failed, status %p",
553 pc.get_nt_native_path (), status);
556 /* FIXME: Should we test for HighPart set? If so, the
557 passwd or group file is way beyond what we can handle. */
558 /* FIXME 2: It's still ugly that we keep the file in memory.
559 Big organizations have naturally large passwd files. */
560 buf = (char *) malloc (fsi.EndOfFile.LowPart + 1);
563 paranoid_printf ("malloc (%d) failed", fsi.EndOfFile.LowPart);
566 status = NtReadFile (fh, NULL, NULL, NULL, &io, buf,
567 fsi.EndOfFile.LowPart, &off, NULL);
568 if (!NT_SUCCESS (status))
570 paranoid_printf ("NtReadFile(%S) failed, status %p",
571 pc.get_nt_native_path (), status);
575 buf[fsi.EndOfFile.LowPart] = '\0';
577 while ((eptr = add_line (eptr)))
579 debug_printf ("%s curr_lines %d", posix_fname, curr_lines);
585 debug_printf ("%s load %s", posix_fname, res);