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 ()
140 && !cygheap->user.setuid_to_restricted)
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.ext_token_is_restricted = false;
155 cygheap->user.curr_token_is_restricted = false;
156 cygheap->user.setuid_to_restricted = false;
157 cygheap->user.set_saved_sid (); /* Update the original sid */
158 cygheap->user.reimpersonate ();
162 getlogin_r (char *name, size_t namesize)
164 char *login = getlogin ();
165 size_t len = strlen (login) + 1;
169 if (efault.faulted ())
171 strncpy (name, login, len);
178 return strcpy (_my_tls.locals.username, cygheap->user.name ());
184 return cygheap->user.real_uid;
190 return cygheap->user.real_uid;
196 return cygheap->user.real_gid;
202 return cygheap->user.real_gid;
229 /* Not quite right - cuserid can change, getlogin can't */
236 strcpy (src, getlogin ());
241 cygheap_user::ontherange (homebodies what, struct passwd *pw)
243 LPUSER_INFO_3 ui = NULL;
244 WCHAR wuser[UNLEN + 1];
246 char homedrive_env_buf[3];
247 char *newhomedrive = NULL;
248 char *newhomepath = NULL;
251 debug_printf ("what %d, pw %p", what, pw);
256 if ((p = getenv ("HOME")))
257 debug_printf ("HOME is already in the environment %s", p);
260 if (pw && pw->pw_dir && *pw->pw_dir)
262 debug_printf ("Set HOME (from /etc/passwd) to %s", pw->pw_dir);
263 setenv ("HOME", pw->pw_dir, 1);
267 char home[strlen (name ()) + 8];
269 debug_printf ("Set HOME to default /home/USER");
270 __small_sprintf (home, "/home/%s", name ());
271 setenv ("HOME", home, 1);
276 if (what != CH_HOME && homepath == NULL && newhomepath == NULL)
278 char *homepath_env_buf = tp.c_get ();
280 pw = internal_getpwnam (name ());
281 if (pw && pw->pw_dir && *pw->pw_dir)
282 cygwin_conv_path (CCP_POSIX_TO_WIN_A, pw->pw_dir, homepath_env_buf,
286 homepath_env_buf[0] = homepath_env_buf[1] = '\0';
289 WCHAR wlogsrv[INTERNET_MAX_HOST_NAME_LENGTH + 3];
290 sys_mbstowcs (wlogsrv, sizeof (wlogsrv) / sizeof (*wlogsrv),
292 sys_mbstowcs (wuser, sizeof (wuser) / sizeof (*wuser), winname ());
293 if (!(ret = NetUserGetInfo (wlogsrv, wuser, 3, (LPBYTE *) &ui)))
295 sys_wcstombs (homepath_env_buf, NT_MAX_PATH,
297 if (!homepath_env_buf[0])
299 sys_wcstombs (homepath_env_buf, NT_MAX_PATH,
300 ui->usri3_home_dir_drive);
301 if (homepath_env_buf[0])
302 strcat (homepath_env_buf, "\\");
304 cygwin_conv_path (CCP_POSIX_TO_WIN_A | CCP_ABSOLUTE,
305 "/", homepath_env_buf, NT_MAX_PATH);
310 NetApiBufferFree (ui);
313 if (homepath_env_buf[1] != ':')
315 newhomedrive = almost_null;
316 newhomepath = homepath_env_buf;
320 homedrive_env_buf[0] = homepath_env_buf[0];
321 homedrive_env_buf[1] = homepath_env_buf[1];
322 homedrive_env_buf[2] = '\0';
323 newhomedrive = homedrive_env_buf;
324 newhomepath = homepath_env_buf + 2;
328 if (newhomedrive && newhomedrive != homedrive)
329 cfree_and_set (homedrive, (newhomedrive == almost_null)
330 ? almost_null : cstrdup (newhomedrive));
332 if (newhomepath && newhomepath != homepath)
333 cfree_and_set (homepath, cstrdup (newhomepath));
347 cygheap_user::test_uid (char *&what, const char *name, size_t namelen)
349 if (!what && !issetuid ())
350 what = getwinenveq (name, namelen, HEAP_STR);
355 cygheap_user::env_logsrv (const char *name, size_t namelen)
357 if (test_uid (plogsrv, name, namelen))
360 const char *mydomain = domain ();
361 const char *myname = winname ();
362 if (!mydomain || ascii_strcasematch (myname, "SYSTEM"))
365 WCHAR wdomain[MAX_DOMAIN_NAME_LEN + 1];
366 WCHAR wlogsrv[INTERNET_MAX_HOST_NAME_LENGTH + 3];
367 sys_mbstowcs (wdomain, MAX_DOMAIN_NAME_LEN + 1, mydomain);
368 cfree_and_set (plogsrv, almost_null);
369 if (get_logon_server (wdomain, wlogsrv, false))
370 sys_wcstombs_alloc (&plogsrv, HEAP_STR, wlogsrv);
375 cygheap_user::env_domain (const char *name, size_t namelen)
377 if (pwinname && test_uid (pdomain, name, namelen))
380 char username[UNLEN + 1];
381 DWORD ulen = sizeof (username);
382 char userdomain[DNLEN + 1];
383 DWORD dlen = sizeof (userdomain);
386 cfree_and_set (pwinname, almost_null);
387 cfree_and_set (pdomain, almost_null);
388 if (!LookupAccountSid (NULL, sid (), username, &ulen,
389 userdomain, &dlen, &use))
393 pwinname = cstrdup (username);
394 pdomain = cstrdup (userdomain);
400 cygheap_user::env_userprofile (const char *name, size_t namelen)
402 if (test_uid (puserprof, name, namelen))
405 WCHAR userprofile_env_buf[NT_MAX_PATH];
406 WCHAR win_id[UNLEN + 1]; /* Large enough for SID */
408 cfree_and_set (puserprof, almost_null);
409 if (get_registry_hive_path (get_windows_id (win_id), userprofile_env_buf))
410 sys_wcstombs_alloc (&puserprof, HEAP_STR, userprofile_env_buf);
416 cygheap_user::env_homepath (const char *name, size_t namelen)
418 return ontherange (CH_HOMEPATH);
422 cygheap_user::env_homedrive (const char *name, size_t namelen)
424 return ontherange (CH_HOMEDRIVE);
428 cygheap_user::env_name (const char *name, size_t namelen)
430 if (!test_uid (pwinname, name, namelen))
436 cygheap_user::env_systemroot (const char *name, size_t namelen)
440 int size = GetWindowsDirectory (NULL, 0);
443 psystemroot = (char *) cmalloc_abort (HEAP_STR, ++size);
444 size = GetWindowsDirectory (psystemroot, size);
452 debug_printf ("GetWindowsDirectory(), %E");
458 pwdgrp::next_str (char c)
461 lptr = strechr (lptr, c);
468 pwdgrp::next_num (unsigned long& n)
470 char *p = next_str (':');
472 n = strtoul (p, &cp, 10);
473 return p != cp && !*cp;
477 pwdgrp::add_line (char *eptr)
482 eptr = strchr (lptr, '\n');
485 if (eptr > lptr && eptr[-1] == '\r')
491 if (curr_lines >= max_lines)
494 *pwdgrp_buf = realloc (*pwdgrp_buf, max_lines * pwdgrp_buf_elem_size);
496 if ((this->*parse) ())
503 pwdgrp::load (const wchar_t *rel_path)
505 static const char failed[] = "failed";
506 static const char succeeded[] = "succeeded";
507 const char *res = failed;
511 OBJECT_ATTRIBUTES attr;
513 FILE_STANDARD_INFORMATION fsi;
521 !(path = (PWCHAR) malloc ((wcslen (cygwin_shared->installation_root)
522 + wcslen (rel_path) + 1) * sizeof (WCHAR))))
524 paranoid_printf ("malloc (%W) failed", rel_path);
527 wcpcpy (wcpcpy (path, cygwin_shared->installation_root), rel_path);
528 RtlInitUnicodeString (&upath, path);
530 InitializeObjectAttributes (&attr, &upath, OBJ_CASE_INSENSITIVE, NULL, NULL);
531 etc_ix = etc::init (etc_ix, &attr);
533 paranoid_printf ("%S", &upath);
535 status = NtOpenFile (&fh, SYNCHRONIZE | FILE_READ_DATA, &attr, &io,
536 FILE_SHARE_VALID_FLAGS,
537 FILE_SYNCHRONOUS_IO_NONALERT
538 | FILE_OPEN_FOR_BACKUP_INTENT);
539 if (!NT_SUCCESS (status))
541 paranoid_printf ("NtOpenFile(%S) failed, status %p", &upath, status);
544 status = NtQueryInformationFile (fh, &io, &fsi, sizeof fsi,
545 FileStandardInformation);
546 if (!NT_SUCCESS (status))
548 paranoid_printf ("NtQueryInformationFile(%S) failed, status %p",
552 /* FIXME: Should we test for HighPart set? If so, the
553 passwd or group file is way beyond what we can handle. */
554 /* FIXME 2: It's still ugly that we keep the file in memory.
555 Big organizations have naturally large passwd files. */
556 buf = (char *) malloc (fsi.EndOfFile.LowPart + 1);
559 paranoid_printf ("malloc (%d) failed", fsi.EndOfFile.LowPart);
562 status = NtReadFile (fh, NULL, NULL, NULL, &io, buf, fsi.EndOfFile.LowPart,
564 if (!NT_SUCCESS (status))
566 paranoid_printf ("NtReadFile(%S) failed, status %p", &upath, status);
570 buf[fsi.EndOfFile.LowPart] = '\0';
571 for (char *eptr = buf; (eptr = add_line (eptr)); )
573 debug_printf ("%W curr_lines %d", rel_path, curr_lines);
579 debug_printf ("%W load %s", rel_path, res);