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"
35 /* Initialize the part of cygheap_user that does not depend on files.
36 The information is used in shared.cc for the user shared.
37 Final initialization occurs in uinfo_init */
41 char user_name[UNLEN + 1];
42 DWORD user_name_len = UNLEN + 1;
44 set_name (GetUserName (user_name, &user_name_len) ? user_name : "unknown");
47 PSECURITY_DESCRIPTOR psd;
49 if (!GetTokenInformation (hProcToken, TokenPrimaryGroup,
50 &groups.pgsid, sizeof (cygsid), &siz))
51 system_printf ("GetTokenInformation (TokenPrimaryGroup), %E");
53 /* Get the SID from current process and store it in effec_cygsid */
54 if (!GetTokenInformation (hProcToken, TokenUser, &effec_cygsid,
55 sizeof (cygsid), &siz))
57 system_printf ("GetTokenInformation (TokenUser), %E");
61 /* Set token owner to the same value as token user */
62 if (!SetTokenInformation (hProcToken, TokenOwner, &effec_cygsid,
64 debug_printf ("SetTokenInformation(TokenOwner), %E");
66 /* Standard way to build a security descriptor with the usual DACL */
67 PSECURITY_ATTRIBUTES sa_buf = (PSECURITY_ATTRIBUTES) alloca (1024);
68 psd = (PSECURITY_DESCRIPTOR)
69 (sec_user_nih (sa_buf, sid()))->lpSecurityDescriptor;
71 BOOL acl_exists, dummy;
72 TOKEN_DEFAULT_DACL dacl;
73 if (GetSecurityDescriptorDacl (psd, &acl_exists, &dacl.DefaultDacl, &dummy)
74 && acl_exists && dacl.DefaultDacl)
78 /* Set the default DACL and the process DACL */
79 if (!SetTokenInformation (hProcToken, TokenDefaultDacl, &dacl,
81 system_printf ("SetTokenInformation (TokenDefaultDacl), %E");
82 if ((status = NtSetSecurityObject (hMainProc, DACL_SECURITY_INFORMATION,
84 system_printf ("NtSetSecurityObject, %lx", status);
87 system_printf("Cannot get dacl, %E");
91 internal_getlogin (cygheap_user &user)
93 struct passwd *pw = NULL;
95 cygpsid psid = user.sid ();
96 pw = internal_getpwsid (psid);
98 if (!pw && !(pw = internal_getpwnam (user.name ()))
99 && !(pw = internal_getpwuid (DEFAULT_UID)))
100 debug_printf ("user not found in augmented /etc/passwd");
105 myself->uid = pw->pw_uid;
106 myself->gid = pw->pw_gid;
107 user.set_name (pw->pw_name);
108 if (gsid.getfromgr (internal_getgrgid (pw->pw_gid)))
110 if (gsid != user.groups.pgsid)
112 /* Set primary group to the group in /etc/passwd. */
113 if (!SetTokenInformation (hProcToken, TokenPrimaryGroup,
115 debug_printf ("SetTokenInformation(TokenPrimaryGroup), %E");
117 user.groups.pgsid = gsid;
118 clear_procimptoken ();
122 debug_printf ("gsid not found in augmented /etc/group");
124 cygheap->user.ontherange (CH_HOME, pw);
130 if (child_proc_info && !cygheap->user.has_impersonation_tokens ())
133 if (!child_proc_info)
134 internal_getlogin (cygheap->user); /* Set the cygheap->user. */
135 /* Conditions must match those in spawn to allow starting child
136 processes with ruid != euid and rgid != egid. */
137 else if (cygheap->user.issetuid ()
138 && cygheap->user.saved_uid == cygheap->user.real_uid
139 && cygheap->user.saved_gid == cygheap->user.real_gid
140 && !cygheap->user.groups.issetgroups ())
142 cygheap->user.reimpersonate ();
146 cygheap->user.close_impersonation_tokens ();
148 cygheap->user.saved_uid = cygheap->user.real_uid = myself->uid;
149 cygheap->user.saved_gid = cygheap->user.real_gid = myself->gid;
150 cygheap->user.external_token = NO_IMPERSONATION;
151 cygheap->user.internal_token = NO_IMPERSONATION;
152 cygheap->user.curr_primary_token = NO_IMPERSONATION;
153 cygheap->user.curr_imp_token = NO_IMPERSONATION;
154 cygheap->user.set_saved_sid (); /* Update the original sid */
155 cygheap->user.reimpersonate ();
159 getlogin_r (char *name, size_t namesize)
161 char *login = getlogin ();
162 size_t len = strlen (login) + 1;
166 if (efault.faulted ())
168 strncpy (name, login, len);
175 return strcpy (_my_tls.locals.username, cygheap->user.name ());
181 return cygheap->user.real_uid;
187 return cygheap->user.real_uid;
193 return cygheap->user.real_gid;
199 return cygheap->user.real_gid;
226 /* Not quite right - cuserid can change, getlogin can't */
233 strcpy (src, getlogin ());
238 cygheap_user::ontherange (homebodies what, struct passwd *pw)
240 LPUSER_INFO_3 ui = NULL;
241 WCHAR wuser[UNLEN + 1];
243 char homepath_env_buf[CYG_MAX_PATH];
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[CYG_MAX_PATH];
277 char buf[CYG_MAX_PATH];
278 strcpy (buf, newhomedrive);
279 strcat (buf, newhomepath);
280 cygwin_conv_to_full_posix_path (buf, home);
281 debug_printf ("Set HOME (from HOMEDRIVE/HOMEPATH) to %s", home);
282 setenv ("HOME", home, 1);
287 if (what != CH_HOME && homepath == NULL && newhomepath == NULL)
290 pw = internal_getpwnam (name ());
291 if (pw && pw->pw_dir && *pw->pw_dir)
292 cygwin_conv_to_full_win32_path (pw->pw_dir, homepath_env_buf);
295 homepath_env_buf[0] = homepath_env_buf[1] = '\0';
298 WCHAR wlogsrv[INTERNET_MAX_HOST_NAME_LENGTH + 3];
299 sys_mbstowcs (wlogsrv, logsrv (),
300 sizeof (wlogsrv) / sizeof (*wlogsrv));
301 sys_mbstowcs (wuser, winname (), sizeof (wuser) / sizeof (*wuser));
302 if (!(ret = NetUserGetInfo (wlogsrv, wuser, 3, (LPBYTE *) &ui)))
304 sys_wcstombs (homepath_env_buf, CYG_MAX_PATH,
306 if (!homepath_env_buf[0])
308 sys_wcstombs (homepath_env_buf, CYG_MAX_PATH,
309 ui->usri3_home_dir_drive);
310 if (homepath_env_buf[0])
311 strcat (homepath_env_buf, "\\");
313 cygwin_conv_to_full_win32_path ("/", homepath_env_buf);
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 || strcasematch (myname, "SYSTEM"))
373 char logsrv[INTERNET_MAX_HOST_NAME_LENGTH + 3];
374 cfree_and_set (plogsrv, almost_null);
375 if (get_logon_server (mydomain, logsrv, NULL, false))
376 plogsrv = cstrdup (logsrv);
381 cygheap_user::env_domain (const char *name, size_t namelen)
383 if (pwinname && test_uid (pdomain, name, namelen))
386 char username[UNLEN + 1];
387 DWORD ulen = sizeof (username);
388 char userdomain[DNLEN + 1];
389 DWORD dlen = sizeof (userdomain);
392 cfree_and_set (pwinname, almost_null);
393 cfree_and_set (pdomain, almost_null);
394 if (!LookupAccountSid (NULL, sid (), username, &ulen,
395 userdomain, &dlen, &use))
399 pwinname = cstrdup (username);
400 pdomain = cstrdup (userdomain);
406 cygheap_user::env_userprofile (const char *name, size_t namelen)
408 if (test_uid (puserprof, name, namelen))
411 char userprofile_env_buf[PATH_MAX];
412 char win_id[UNLEN + 1]; /* Large enough for SID */
414 cfree_and_set (puserprof, almost_null);
415 if (get_registry_hive_path (get_windows_id (win_id), userprofile_env_buf))
416 puserprof = cstrdup (userprofile_env_buf);
422 cygheap_user::env_homepath (const char *name, size_t namelen)
424 return ontherange (CH_HOMEPATH);
428 cygheap_user::env_homedrive (const char *name, size_t namelen)
430 return ontherange (CH_HOMEDRIVE);
434 cygheap_user::env_name (const char *name, size_t namelen)
436 if (!test_uid (pwinname, name, namelen))
442 cygheap_user::env_systemroot (const char *name, size_t namelen)
446 int size = GetWindowsDirectory (NULL, 0);
449 psystemroot = (char *) cmalloc_abort (HEAP_STR, ++size);
450 size = GetWindowsDirectory (psystemroot, size);
458 debug_printf ("GetWindowsDirectory(), %E");
464 pwdgrp::next_str (char c)
467 lptr = strechr (lptr, c);
474 pwdgrp::next_num (unsigned long& n)
476 char *p = next_str (':');
478 n = strtoul (p, &cp, 10);
479 return p != cp && !*cp;
483 pwdgrp::add_line (char *eptr)
488 eptr = strchr (lptr, '\n');
491 if (eptr > lptr && eptr[-1] == '\r')
497 if (curr_lines >= max_lines)
500 *pwdgrp_buf = realloc (*pwdgrp_buf, max_lines * pwdgrp_buf_elem_size);
502 if ((this->*parse) ())
509 pwdgrp::load (const char *posix_fname)
511 static const char failed[] = "failed";
512 static const char succeeded[] = "succeeded";
513 const char *res = failed;
515 LARGE_INTEGER off = { QuadPart:0LL };
518 OBJECT_ATTRIBUTES attr;
520 FILE_STANDARD_INFORMATION fsi;
527 pc.check (posix_fname);
528 etc_ix = etc::init (etc_ix, pc.get_nt_native_path ());
530 paranoid_printf ("%s", posix_fname);
532 if (pc.error || !pc.exists () || pc.isdir ())
534 paranoid_printf ("strange path_conv problem");
537 status = NtOpenFile (&fh, FILE_READ_DATA,
538 pc.get_object_attr (attr, sec_none_nih), &io,
539 FILE_SHARE_VALID_FLAGS, 0);
540 if (!NT_SUCCESS (status))
542 paranoid_printf ("NtOpenFile(%S) failed, status %p",
543 pc.get_nt_native_path (), status);
546 status = NtQueryInformationFile (fh, &io, &fsi, sizeof fsi,
547 FileStandardInformation);
548 if (!NT_SUCCESS (status))
550 paranoid_printf ("NtQueryInformationFile(%S) failed, status %p",
551 pc.get_nt_native_path (), status);
554 /* FIXME: Should we test for HighPart set? If so, the
555 passwd or group file is way beyond what we can handle. */
556 /* FIXME 2: It's still ugly that we keep the file in memory.
557 Big organizations have naturally large passwd files. */
558 buf = (char *) malloc (fsi.EndOfFile.LowPart + 1);
561 paranoid_printf ("malloc (%d) failed", fsi.EndOfFile.LowPart);
564 status = NtReadFile (fh, NULL, NULL, NULL, &io, buf,
565 fsi.EndOfFile.LowPart, &off, NULL);
566 if (!NT_SUCCESS (status))
568 paranoid_printf ("NtReadFile(%S) failed, status %p",
569 pc.get_nt_native_path (), status);
573 buf[fsi.EndOfFile.LowPart] = '\0';
575 while ((eptr = add_line (eptr)))
577 debug_printf ("%s curr_lines %d", posix_fname, curr_lines);
583 debug_printf ("%s load %s", posix_fname, res);