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 WCHAR user_name[UNLEN + 1];
43 user_name_len = GetEnvironmentVariableW (L"USERNAME", user_name, UNLEN + 1);
46 user_name[UNLEN] = L'\0';
47 char mb_user_name[user_name_len = sys_wcstombs (NULL, 0, user_name)];
48 sys_wcstombs (mb_user_name, user_name_len, user_name);
49 set_name (mb_user_name);
53 PSECURITY_DESCRIPTOR psd;
55 if (!GetTokenInformation (hProcToken, TokenPrimaryGroup,
56 &groups.pgsid, sizeof (cygsid), &siz))
57 system_printf ("GetTokenInformation (TokenPrimaryGroup), %E");
59 /* Get the SID from current process and store it in effec_cygsid */
60 if (!GetTokenInformation (hProcToken, TokenUser, &effec_cygsid,
61 sizeof (cygsid), &siz))
63 system_printf ("GetTokenInformation (TokenUser), %E");
67 /* Set token owner to the same value as token user */
68 if (!SetTokenInformation (hProcToken, TokenOwner, &effec_cygsid,
70 debug_printf ("SetTokenInformation(TokenOwner), %E");
72 /* Standard way to build a security descriptor with the usual DACL */
73 PSECURITY_ATTRIBUTES sa_buf = (PSECURITY_ATTRIBUTES) alloca (1024);
74 psd = (PSECURITY_DESCRIPTOR)
75 (sec_user_nih (sa_buf, sid()))->lpSecurityDescriptor;
77 BOOL acl_exists, dummy;
78 TOKEN_DEFAULT_DACL dacl;
79 if (GetSecurityDescriptorDacl (psd, &acl_exists, &dacl.DefaultDacl, &dummy)
80 && acl_exists && dacl.DefaultDacl)
84 /* Set the default DACL and the process DACL */
85 if (!SetTokenInformation (hProcToken, TokenDefaultDacl, &dacl,
87 system_printf ("SetTokenInformation (TokenDefaultDacl), %E");
88 if ((status = NtSetSecurityObject (NtCurrentProcess (),
89 DACL_SECURITY_INFORMATION, psd)))
90 system_printf ("NtSetSecurityObject, %lx", status);
93 system_printf("Cannot get dacl, %E");
97 internal_getlogin (cygheap_user &user)
99 struct passwd *pw = NULL;
101 /* Handle a border case. If neither $USERNAME, nor /etc/passwd exists,
102 we tryto fetch the username from the system now. */
103 if (!user.name () || !*user.name ())
105 WCHAR user_name[UNLEN + 1];
106 DWORD user_name_len = UNLEN + 1;
108 if (GetUserNameW (user_name, &user_name_len))
110 char mb_user_name[user_name_len = sys_wcstombs (NULL, 0, user_name)];
111 sys_wcstombs (mb_user_name, user_name_len, user_name);
112 user.set_name (mb_user_name);
115 user.set_name ("unknown");
119 cygpsid psid = user.sid ();
120 pw = internal_getpwsid (psid);
123 if (!pw && !(pw = internal_getpwnam (user.name (), true))
124 && !(pw = internal_getpwuid (DEFAULT_UID)))
125 debug_printf ("user not found in augmented /etc/passwd");
130 myself->uid = pw->pw_uid;
131 myself->gid = pw->pw_gid;
132 user.set_name (pw->pw_name);
133 if (gsid.getfromgr (internal_getgrgid (pw->pw_gid)))
135 if (gsid != user.groups.pgsid)
137 /* Set primary group to the group in /etc/passwd. */
138 if (!SetTokenInformation (hProcToken, TokenPrimaryGroup,
140 debug_printf ("SetTokenInformation(TokenPrimaryGroup), %E");
142 user.groups.pgsid = gsid;
143 clear_procimptoken ();
147 debug_printf ("gsid not found in augmented /etc/group");
149 cygheap->user.ontherange (CH_HOME, pw);
155 if (child_proc_info && !cygheap->user.has_impersonation_tokens ())
158 if (!child_proc_info)
159 internal_getlogin (cygheap->user); /* Set the cygheap->user. */
160 /* Conditions must match those in spawn to allow starting child
161 processes with ruid != euid and rgid != egid. */
162 else if (cygheap->user.issetuid ()
163 && cygheap->user.saved_uid == cygheap->user.real_uid
164 && cygheap->user.saved_gid == cygheap->user.real_gid
165 && !cygheap->user.groups.issetgroups ()
166 && !cygheap->user.setuid_to_restricted)
168 cygheap->user.reimpersonate ();
172 cygheap->user.close_impersonation_tokens ();
174 cygheap->user.saved_uid = cygheap->user.real_uid = myself->uid;
175 cygheap->user.saved_gid = cygheap->user.real_gid = myself->gid;
176 cygheap->user.external_token = NO_IMPERSONATION;
177 cygheap->user.internal_token = NO_IMPERSONATION;
178 cygheap->user.curr_primary_token = NO_IMPERSONATION;
179 cygheap->user.curr_imp_token = NO_IMPERSONATION;
180 cygheap->user.ext_token_is_restricted = false;
181 cygheap->user.curr_token_is_restricted = false;
182 cygheap->user.setuid_to_restricted = false;
183 cygheap->user.set_saved_sid (); /* Update the original sid */
184 cygheap->user.deimpersonate ();
188 getlogin_r (char *name, size_t namesize)
190 char *login = getlogin ();
191 size_t len = strlen (login) + 1;
195 if (efault.faulted ())
197 strncpy (name, login, len);
204 return strcpy (_my_tls.locals.username, cygheap->user.name ());
210 return cygheap->user.real_uid;
216 return cygheap->user.real_uid;
222 return cygheap->user.real_gid;
228 return cygheap->user.real_gid;
255 /* Not quite right - cuserid can change, getlogin can't */
262 strcpy (src, getlogin ());
267 cygheap_user::ontherange (homebodies what, struct passwd *pw)
269 LPUSER_INFO_3 ui = NULL;
270 WCHAR wuser[UNLEN + 1];
272 char homedrive_env_buf[3];
273 char *newhomedrive = NULL;
274 char *newhomepath = NULL;
277 debug_printf ("what %d, pw %p", what, pw);
282 if ((p = getenv ("HOME")))
283 debug_printf ("HOME is already in the environment %s", p);
286 if (pw && pw->pw_dir && *pw->pw_dir)
288 debug_printf ("Set HOME (from /etc/passwd) to %s", pw->pw_dir);
289 setenv ("HOME", pw->pw_dir, 1);
293 char home[strlen (name ()) + 8];
295 debug_printf ("Set HOME to default /home/USER");
296 __small_sprintf (home, "/home/%s", name ());
297 setenv ("HOME", home, 1);
302 if (what != CH_HOME && homepath == NULL && newhomepath == NULL)
304 char *homepath_env_buf = tp.c_get ();
306 pw = internal_getpwnam (name ());
307 if (pw && pw->pw_dir && *pw->pw_dir)
308 cygwin_conv_path (CCP_POSIX_TO_WIN_A, pw->pw_dir, homepath_env_buf,
312 homepath_env_buf[0] = homepath_env_buf[1] = '\0';
315 WCHAR wlogsrv[INTERNET_MAX_HOST_NAME_LENGTH + 3];
316 sys_mbstowcs (wlogsrv, sizeof (wlogsrv) / sizeof (*wlogsrv),
318 sys_mbstowcs (wuser, sizeof (wuser) / sizeof (*wuser), winname ());
319 if (!(ret = NetUserGetInfo (wlogsrv, wuser, 3, (LPBYTE *) &ui)))
321 sys_wcstombs (homepath_env_buf, NT_MAX_PATH,
323 if (!homepath_env_buf[0])
325 sys_wcstombs (homepath_env_buf, NT_MAX_PATH,
326 ui->usri3_home_dir_drive);
327 if (homepath_env_buf[0])
328 strcat (homepath_env_buf, "\\");
330 cygwin_conv_path (CCP_POSIX_TO_WIN_A | CCP_ABSOLUTE,
331 "/", homepath_env_buf, NT_MAX_PATH);
336 NetApiBufferFree (ui);
339 if (homepath_env_buf[1] != ':')
341 newhomedrive = almost_null;
342 newhomepath = homepath_env_buf;
346 homedrive_env_buf[0] = homepath_env_buf[0];
347 homedrive_env_buf[1] = homepath_env_buf[1];
348 homedrive_env_buf[2] = '\0';
349 newhomedrive = homedrive_env_buf;
350 newhomepath = homepath_env_buf + 2;
354 if (newhomedrive && newhomedrive != homedrive)
355 cfree_and_set (homedrive, (newhomedrive == almost_null)
356 ? almost_null : cstrdup (newhomedrive));
358 if (newhomepath && newhomepath != homepath)
359 cfree_and_set (homepath, cstrdup (newhomepath));
373 cygheap_user::test_uid (char *&what, const char *name, size_t namelen)
375 if (!what && !issetuid ())
376 what = getwinenveq (name, namelen, HEAP_STR);
381 cygheap_user::env_logsrv (const char *name, size_t namelen)
383 if (test_uid (plogsrv, name, namelen))
386 const char *mydomain = domain ();
387 const char *myname = winname ();
388 if (!mydomain || ascii_strcasematch (myname, "SYSTEM"))
391 WCHAR wdomain[MAX_DOMAIN_NAME_LEN + 1];
392 WCHAR wlogsrv[INTERNET_MAX_HOST_NAME_LENGTH + 3];
393 sys_mbstowcs (wdomain, MAX_DOMAIN_NAME_LEN + 1, mydomain);
394 cfree_and_set (plogsrv, almost_null);
395 if (get_logon_server (wdomain, wlogsrv, false))
396 sys_wcstombs_alloc (&plogsrv, HEAP_STR, wlogsrv);
401 cygheap_user::env_domain (const char *name, size_t namelen)
403 if (pwinname && test_uid (pdomain, name, namelen))
406 char username[UNLEN + 1];
407 DWORD ulen = sizeof (username);
408 char userdomain[DNLEN + 1];
409 DWORD dlen = sizeof (userdomain);
412 cfree_and_set (pwinname, almost_null);
413 cfree_and_set (pdomain, almost_null);
414 if (!LookupAccountSid (NULL, sid (), username, &ulen,
415 userdomain, &dlen, &use))
419 pwinname = cstrdup (username);
420 pdomain = cstrdup (userdomain);
426 cygheap_user::env_userprofile (const char *name, size_t namelen)
428 if (test_uid (puserprof, name, namelen))
431 WCHAR userprofile_env_buf[NT_MAX_PATH];
432 WCHAR win_id[UNLEN + 1]; /* Large enough for SID */
434 cfree_and_set (puserprof, almost_null);
435 if (get_registry_hive_path (get_windows_id (win_id), userprofile_env_buf))
436 sys_wcstombs_alloc (&puserprof, HEAP_STR, userprofile_env_buf);
442 cygheap_user::env_homepath (const char *name, size_t namelen)
444 return ontherange (CH_HOMEPATH);
448 cygheap_user::env_homedrive (const char *name, size_t namelen)
450 return ontherange (CH_HOMEDRIVE);
454 cygheap_user::env_name (const char *name, size_t namelen)
456 if (!test_uid (pwinname, name, namelen))
462 cygheap_user::env_systemroot (const char *name, size_t namelen)
466 int size = GetWindowsDirectory (NULL, 0);
469 psystemroot = (char *) cmalloc_abort (HEAP_STR, ++size);
470 size = GetWindowsDirectory (psystemroot, size);
478 debug_printf ("GetWindowsDirectory(), %E");
484 pwdgrp::next_str (char c)
487 lptr = strechr (lptr, c);
494 pwdgrp::next_num (unsigned long& n)
496 char *p = next_str (':');
498 n = strtoul (p, &cp, 10);
499 return p != cp && !*cp;
503 pwdgrp::add_line (char *eptr)
508 eptr = strchr (lptr, '\n');
511 if (eptr > lptr && eptr[-1] == '\r')
517 if (curr_lines >= max_lines)
520 *pwdgrp_buf = realloc (*pwdgrp_buf, max_lines * pwdgrp_buf_elem_size);
522 if ((this->*parse) ())
529 pwdgrp::load (const wchar_t *rel_path)
531 static const char failed[] = "failed";
532 static const char succeeded[] = "succeeded";
533 const char *res = failed;
537 OBJECT_ATTRIBUTES attr;
539 FILE_STANDARD_INFORMATION fsi;
547 !(path = (PWCHAR) malloc ((wcslen (installation_root)
548 + wcslen (rel_path) + 1) * sizeof (WCHAR))))
550 paranoid_printf ("malloc (%W) failed", rel_path);
553 wcpcpy (wcpcpy (path, installation_root), rel_path);
554 RtlInitUnicodeString (&upath, path);
556 InitializeObjectAttributes (&attr, &upath, OBJ_CASE_INSENSITIVE, NULL, NULL);
557 etc_ix = etc::init (etc_ix, &attr);
559 paranoid_printf ("%S", &upath);
561 status = NtOpenFile (&fh, SYNCHRONIZE | FILE_READ_DATA, &attr, &io,
562 FILE_SHARE_VALID_FLAGS,
563 FILE_SYNCHRONOUS_IO_NONALERT
564 | FILE_OPEN_FOR_BACKUP_INTENT);
565 if (!NT_SUCCESS (status))
567 paranoid_printf ("NtOpenFile(%S) failed, status %p", &upath, status);
570 status = NtQueryInformationFile (fh, &io, &fsi, sizeof fsi,
571 FileStandardInformation);
572 if (!NT_SUCCESS (status))
574 paranoid_printf ("NtQueryInformationFile(%S) failed, status %p",
578 /* FIXME: Should we test for HighPart set? If so, the
579 passwd or group file is way beyond what we can handle. */
580 /* FIXME 2: It's still ugly that we keep the file in memory.
581 Big organizations have naturally large passwd files. */
582 buf = (char *) malloc (fsi.EndOfFile.LowPart + 1);
585 paranoid_printf ("malloc (%d) failed", fsi.EndOfFile.LowPart);
588 status = NtReadFile (fh, NULL, NULL, NULL, &io, buf, fsi.EndOfFile.LowPart,
590 if (!NT_SUCCESS (status))
592 paranoid_printf ("NtReadFile(%S) failed, status %p", &upath, status);
596 buf[fsi.EndOfFile.LowPart] = '\0';
597 for (char *eptr = buf; (eptr = add_line (eptr)); )
599 debug_printf ("%W curr_lines %d", rel_path, curr_lines);
605 debug_printf ("%W load %s", rel_path, res);