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
17 #include <sys/cygwin.h>
25 #include "child_info.h"
31 /* Initialize the part of cygheap_user that does not depend on files.
32 The information is used in shared.cc for the user shared.
33 Final initialization occurs in uinfo_init */
37 char user_name[UNLEN + 1];
38 DWORD user_name_len = UNLEN + 1;
40 set_name (GetUserName (user_name, &user_name_len) ? user_name : "unknown");
43 PSECURITY_DESCRIPTOR psd;
45 if (!GetTokenInformation (hProcToken, TokenPrimaryGroup,
46 &groups.pgsid, sizeof (cygsid), &siz))
47 system_printf ("GetTokenInformation (TokenPrimaryGroup), %E");
49 /* Get the SID from current process and store it in effec_cygsid */
50 if (!GetTokenInformation (hProcToken, TokenUser, &effec_cygsid,
51 sizeof (cygsid), &siz))
53 system_printf ("GetTokenInformation (TokenUser), %E");
57 /* Set token owner to the same value as token user */
58 if (!SetTokenInformation (hProcToken, TokenOwner, &effec_cygsid,
60 debug_printf ("SetTokenInformation(TokenOwner), %E");
62 /* Standard way to build a security descriptor with the usual DACL */
63 PSECURITY_ATTRIBUTES sa_buf = (PSECURITY_ATTRIBUTES) alloca (1024);
64 psd = (PSECURITY_DESCRIPTOR)
65 (sec_user_nih (sa_buf, sid()))->lpSecurityDescriptor;
67 BOOL acl_exists, dummy;
68 TOKEN_DEFAULT_DACL dacl;
69 if (GetSecurityDescriptorDacl (psd, &acl_exists, &dacl.DefaultDacl, &dummy)
70 && acl_exists && dacl.DefaultDacl)
74 /* Set the default DACL and the process DACL */
75 if (!SetTokenInformation (hProcToken, TokenDefaultDacl, &dacl,
77 system_printf ("SetTokenInformation (TokenDefaultDacl), %E");
78 if ((status = NtSetSecurityObject (hMainProc, DACL_SECURITY_INFORMATION,
80 system_printf ("NtSetSecurityObject, %lx", status);
83 system_printf("Cannot get dacl, %E");
87 internal_getlogin (cygheap_user &user)
89 struct passwd *pw = NULL;
91 cygpsid psid = user.sid ();
92 pw = internal_getpwsid (psid);
94 if (!pw && !(pw = internal_getpwnam (user.name ()))
95 && !(pw = internal_getpwuid (DEFAULT_UID)))
96 debug_printf ("user not found in augmented /etc/passwd");
101 myself->uid = pw->pw_uid;
102 myself->gid = pw->pw_gid;
103 user.set_name (pw->pw_name);
104 if (gsid.getfromgr (internal_getgrgid (pw->pw_gid)))
106 if (gsid != user.groups.pgsid)
108 /* Set primary group to the group in /etc/passwd. */
109 if (!SetTokenInformation (hProcToken, TokenPrimaryGroup,
111 debug_printf ("SetTokenInformation(TokenPrimaryGroup), %E");
113 user.groups.pgsid = gsid;
114 clear_procimptoken ();
118 debug_printf ("gsid not found in augmented /etc/group");
120 cygheap->user.ontherange (CH_HOME, pw);
126 if (child_proc_info && !cygheap->user.has_impersonation_tokens ())
129 if (!child_proc_info)
130 internal_getlogin (cygheap->user); /* Set the cygheap->user. */
131 /* Conditions must match those in spawn to allow starting child
132 processes with ruid != euid and rgid != egid. */
133 else if (cygheap->user.issetuid ()
134 && cygheap->user.saved_uid == cygheap->user.real_uid
135 && cygheap->user.saved_gid == cygheap->user.real_gid
136 && !cygheap->user.groups.issetgroups ())
138 cygheap->user.reimpersonate ();
142 cygheap->user.close_impersonation_tokens ();
144 cygheap->user.saved_uid = cygheap->user.real_uid = myself->uid;
145 cygheap->user.saved_gid = cygheap->user.real_gid = myself->gid;
146 cygheap->user.external_token = NO_IMPERSONATION;
147 cygheap->user.internal_token = NO_IMPERSONATION;
148 cygheap->user.curr_primary_token = NO_IMPERSONATION;
149 cygheap->user.curr_imp_token = NO_IMPERSONATION;
150 cygheap->user.set_saved_sid (); /* Update the original sid */
151 cygheap->user.reimpersonate ();
155 getlogin_r (char *name, size_t namesize)
157 char *login = getlogin ();
158 size_t len = strlen (login) + 1;
162 if (efault.faulted ())
164 strncpy (name, login, len);
171 return strcpy (_my_tls.locals.username, cygheap->user.name ());
177 return cygheap->user.real_uid;
183 return cygheap->user.real_uid;
189 return cygheap->user.real_gid;
195 return cygheap->user.real_gid;
222 /* Not quite right - cuserid can change, getlogin can't */
229 strcpy (src, getlogin ());
234 cygheap_user::ontherange (homebodies what, struct passwd *pw)
236 LPUSER_INFO_3 ui = NULL;
237 WCHAR wuser[UNLEN + 1];
239 char homedrive_env_buf[3];
240 char *newhomedrive = NULL;
241 char *newhomepath = NULL;
244 debug_printf ("what %d, pw %p", what, pw);
249 newhomedrive = homedrive;
250 else if ((p = getenv ("HOMEDRIVE")))
254 newhomepath = homepath;
255 else if ((p = getenv ("HOMEPATH")))
258 if ((p = getenv ("HOME")))
259 debug_printf ("HOME is already in the environment %s", p);
262 if (pw && pw->pw_dir && *pw->pw_dir)
264 debug_printf ("Set HOME (from /etc/passwd) to %s", pw->pw_dir);
265 setenv ("HOME", pw->pw_dir, 1);
267 else if (!newhomedrive || !newhomepath)
268 setenv ("HOME", "/", 1);
271 char *home = tp.c_get ();
272 char *buf = tp.c_get ();
273 strcpy (buf, newhomedrive);
274 strcat (buf, newhomepath);
275 cygwin_conv_path (CCP_WIN_A_TO_POSIX | CCP_ABSOLUTE, buf, home,
277 debug_printf ("Set HOME (from HOMEDRIVE/HOMEPATH) to %s", home);
278 setenv ("HOME", home, 1);
283 if (what != CH_HOME && homepath == NULL && newhomepath == NULL)
285 char *homepath_env_buf = tp.c_get ();
287 pw = internal_getpwnam (name ());
288 if (pw && pw->pw_dir && *pw->pw_dir)
289 cygwin_conv_path (CCP_POSIX_TO_WIN_A, pw->pw_dir, homepath_env_buf,
293 homepath_env_buf[0] = homepath_env_buf[1] = '\0';
296 WCHAR wlogsrv[INTERNET_MAX_HOST_NAME_LENGTH + 3];
297 sys_mbstowcs (wlogsrv, sizeof (wlogsrv) / sizeof (*wlogsrv),
299 sys_mbstowcs (wuser, sizeof (wuser) / sizeof (*wuser), winname ());
300 if (!(ret = NetUserGetInfo (wlogsrv, wuser, 3, (LPBYTE *) &ui)))
302 sys_wcstombs (homepath_env_buf, NT_MAX_PATH,
304 if (!homepath_env_buf[0])
306 sys_wcstombs (homepath_env_buf, NT_MAX_PATH,
307 ui->usri3_home_dir_drive);
308 if (homepath_env_buf[0])
309 strcat (homepath_env_buf, "\\");
311 cygwin_conv_path (CCP_POSIX_TO_WIN_A | CCP_ABSOLUTE,
312 "/", homepath_env_buf, NT_MAX_PATH);
317 NetApiBufferFree (ui);
320 if (homepath_env_buf[1] != ':')
322 newhomedrive = almost_null;
323 newhomepath = homepath_env_buf;
327 homedrive_env_buf[0] = homepath_env_buf[0];
328 homedrive_env_buf[1] = homepath_env_buf[1];
329 homedrive_env_buf[2] = '\0';
330 newhomedrive = homedrive_env_buf;
331 newhomepath = homepath_env_buf + 2;
335 if (newhomedrive && newhomedrive != homedrive)
336 cfree_and_set (homedrive, (newhomedrive == almost_null)
337 ? almost_null : cstrdup (newhomedrive));
339 if (newhomepath && newhomepath != homepath)
340 cfree_and_set (homepath, cstrdup (newhomepath));
354 cygheap_user::test_uid (char *&what, const char *name, size_t namelen)
356 if (!what && !issetuid ())
357 what = getwinenveq (name, namelen, HEAP_STR);
362 cygheap_user::env_logsrv (const char *name, size_t namelen)
364 if (test_uid (plogsrv, name, namelen))
367 const char *mydomain = domain ();
368 const char *myname = winname ();
369 if (!mydomain || ascii_strcasematch (myname, "SYSTEM"))
372 WCHAR wdomain[INTERNET_MAX_HOST_NAME_LENGTH + 1];
373 WCHAR wlogsrv[INTERNET_MAX_HOST_NAME_LENGTH + 3];
374 sys_mbstowcs (wdomain, INTERNET_MAX_HOST_NAME_LENGTH + 1, mydomain);
375 cfree_and_set (plogsrv, almost_null);
376 if (get_logon_server (wdomain, wlogsrv, false))
377 sys_wcstombs_alloc (&plogsrv, HEAP_STR, wlogsrv);
382 cygheap_user::env_domain (const char *name, size_t namelen)
384 if (pwinname && test_uid (pdomain, name, namelen))
387 char username[UNLEN + 1];
388 DWORD ulen = sizeof (username);
389 char userdomain[DNLEN + 1];
390 DWORD dlen = sizeof (userdomain);
393 cfree_and_set (pwinname, almost_null);
394 cfree_and_set (pdomain, almost_null);
395 if (!LookupAccountSid (NULL, sid (), username, &ulen,
396 userdomain, &dlen, &use))
400 pwinname = cstrdup (username);
401 pdomain = cstrdup (userdomain);
407 cygheap_user::env_userprofile (const char *name, size_t namelen)
409 if (test_uid (puserprof, name, namelen))
412 WCHAR userprofile_env_buf[NT_MAX_PATH];
413 WCHAR win_id[UNLEN + 1]; /* Large enough for SID */
415 cfree_and_set (puserprof, almost_null);
416 if (get_registry_hive_path (get_windows_id (win_id), userprofile_env_buf))
417 sys_wcstombs_alloc (&puserprof, HEAP_STR, userprofile_env_buf);
423 cygheap_user::env_homepath (const char *name, size_t namelen)
425 return ontherange (CH_HOMEPATH);
429 cygheap_user::env_homedrive (const char *name, size_t namelen)
431 return ontherange (CH_HOMEDRIVE);
435 cygheap_user::env_name (const char *name, size_t namelen)
437 if (!test_uid (pwinname, name, namelen))
443 cygheap_user::env_systemroot (const char *name, size_t namelen)
447 int size = GetWindowsDirectory (NULL, 0);
450 psystemroot = (char *) cmalloc_abort (HEAP_STR, ++size);
451 size = GetWindowsDirectory (psystemroot, size);
459 debug_printf ("GetWindowsDirectory(), %E");
465 pwdgrp::next_str (char c)
468 lptr = strechr (lptr, c);
475 pwdgrp::next_num (unsigned long& n)
477 char *p = next_str (':');
479 n = strtoul (p, &cp, 10);
480 return p != cp && !*cp;
484 pwdgrp::add_line (char *eptr)
489 eptr = strchr (lptr, '\n');
492 if (eptr > lptr && eptr[-1] == '\r')
498 if (curr_lines >= max_lines)
501 *pwdgrp_buf = realloc (*pwdgrp_buf, max_lines * pwdgrp_buf_elem_size);
503 if ((this->*parse) ())
510 pwdgrp::load (const char *posix_fname)
512 static const char failed[] = "failed";
513 static const char succeeded[] = "succeeded";
514 const char *res = failed;
516 LARGE_INTEGER off = { QuadPart:0LL };
519 OBJECT_ATTRIBUTES attr;
521 FILE_STANDARD_INFORMATION fsi;
528 pc.check (posix_fname);
529 etc_ix = etc::init (etc_ix, pc.get_nt_native_path ());
531 paranoid_printf ("%s", posix_fname);
533 if (pc.error || !pc.exists () || pc.isdir ())
535 paranoid_printf ("strange path_conv problem");
538 status = NtOpenFile (&fh, FILE_READ_DATA,
539 pc.get_object_attr (attr, sec_none_nih), &io,
540 FILE_SHARE_VALID_FLAGS, 0);
541 if (!NT_SUCCESS (status))
543 paranoid_printf ("NtOpenFile(%S) failed, status %p",
544 pc.get_nt_native_path (), status);
547 status = NtQueryInformationFile (fh, &io, &fsi, sizeof fsi,
548 FileStandardInformation);
549 if (!NT_SUCCESS (status))
551 paranoid_printf ("NtQueryInformationFile(%S) failed, status %p",
552 pc.get_nt_native_path (), status);
555 /* FIXME: Should we test for HighPart set? If so, the
556 passwd or group file is way beyond what we can handle. */
557 /* FIXME 2: It's still ugly that we keep the file in memory.
558 Big organizations have naturally large passwd files. */
559 buf = (char *) malloc (fsi.EndOfFile.LowPart + 1);
562 paranoid_printf ("malloc (%d) failed", fsi.EndOfFile.LowPart);
565 status = NtReadFile (fh, NULL, NULL, NULL, &io, buf,
566 fsi.EndOfFile.LowPart, &off, NULL);
567 if (!NT_SUCCESS (status))
569 paranoid_printf ("NtReadFile(%S) failed, status %p",
570 pc.get_nt_native_path (), status);
574 buf[fsi.EndOfFile.LowPart] = '\0';
576 while ((eptr = add_line (eptr)))
578 debug_printf ("%s curr_lines %d", posix_fname, curr_lines);
584 debug_printf ("%s load %s", posix_fname, res);