1 /* uinfo.cc: user info (uid, gid, etc...)
3 Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004 Red Hat, Inc.
5 This file is part of Cygwin.
7 This software is a copyrighted work licensed under the terms of the
8 Cygwin license. Please consult the file "CYGWIN_LICENSE" for
20 #include <sys/cygwin.h>
29 #include "child_info.h"
33 /* Initialize the part of cygheap_user that does not depend on files.
34 The information is used in shared.cc for the user shared.
35 Final initialization occurs in uinfo_init */
39 char user_name[UNLEN + 1];
40 DWORD user_name_len = UNLEN + 1;
42 set_name (GetUserName (user_name, &user_name_len) ? user_name : "unknown");
44 if (!wincap.has_security ())
49 PSECURITY_DESCRIPTOR psd;
51 if (!OpenProcessToken (hMainProc, TOKEN_ADJUST_DEFAULT | TOKEN_QUERY,
54 system_printf ("OpenProcessToken(), %E");
57 if (!GetTokenInformation (ptok, TokenPrimaryGroup,
58 &groups.pgsid, sizeof (cygsid), &siz))
59 system_printf ("GetTokenInformation (TokenPrimaryGroup), %E");
61 /* Get the SID from current process and store it in effec_cygsid */
62 if (!GetTokenInformation (ptok, TokenUser, &effec_cygsid, sizeof (cygsid), &siz))
64 system_printf ("GetTokenInformation (TokenUser), %E");
68 /* Set token owner to the same value as token user */
69 if (!SetTokenInformation (ptok, TokenOwner, &effec_cygsid, sizeof (cygsid)))
70 debug_printf ("SetTokenInformation(TokenOwner), %E");
72 /* Standard way to build a security descriptor with the usual DACL */
74 psd = (PSECURITY_DESCRIPTOR) (sec_user_nih (sa_buf, sid()))->lpSecurityDescriptor;
76 BOOL acl_exists, dummy;
77 TOKEN_DEFAULT_DACL dacl;
78 if (GetSecurityDescriptorDacl (psd, &acl_exists,
79 &dacl.DefaultDacl, &dummy)
80 && acl_exists && dacl.DefaultDacl)
82 /* Set the default DACL and the process DACL */
83 if (!SetTokenInformation (ptok, TokenDefaultDacl, &dacl, sizeof (dacl)))
84 system_printf ("SetTokenInformation (TokenDefaultDacl), %E");
85 if (!SetKernelObjectSecurity (hMainProc, DACL_SECURITY_INFORMATION, psd))
86 system_printf ("SetKernelObjectSecurity, %E");
89 system_printf("Cannot get dacl, %E");
95 internal_getlogin (cygheap_user &user)
97 struct passwd *pw = NULL;
99 if (wincap.has_security ())
101 cygpsid psid = user.sid ();
102 pw = internal_getpwsid (psid);
105 if (!pw && !(pw = internal_getpwnam (user.name ()))
106 && !(pw = internal_getpwuid (DEFAULT_UID)))
107 debug_printf ("user not found in augmented /etc/passwd");
110 myself->uid = pw->pw_uid;
111 myself->gid = pw->pw_gid;
112 user.set_name (pw->pw_name);
113 if (wincap.has_security ())
116 if (gsid.getfromgr (internal_getgrgid (pw->pw_gid)))
119 if (gsid != user.groups.pgsid
120 && OpenProcessToken (hMainProc, TOKEN_ADJUST_DEFAULT, &ptok))
122 /* Set primary group to the group in /etc/passwd. */
123 if (!SetTokenInformation (ptok, TokenPrimaryGroup,
125 debug_printf ("SetTokenInformation(TokenPrimaryGroup), %E");
127 user.groups.pgsid = gsid;
132 debug_printf ("gsid not found in augmented /etc/group");
135 (void) cygheap->user.ontherange (CH_HOME, pw);
143 if (child_proc_info && !cygheap->user.has_impersonation_tokens ())
146 if (!child_proc_info)
147 internal_getlogin (cygheap->user); /* Set the cygheap->user. */
148 /* Conditions must match those in spawn to allow starting child
149 processes with ruid != euid and rgid != egid. */
150 else if (cygheap->user.issetuid ()
151 && cygheap->user.saved_uid == cygheap->user.real_uid
152 && cygheap->user.saved_gid == cygheap->user.real_gid
153 && !cygheap->user.groups.issetgroups ())
155 cygheap->user.reimpersonate ();
159 cygheap->user.close_impersonation_tokens ();
161 cygheap->user.saved_uid = cygheap->user.real_uid = myself->uid;
162 cygheap->user.saved_gid = cygheap->user.real_gid = myself->gid;
163 cygheap->user.external_token = NO_IMPERSONATION;
164 cygheap->user.internal_token = NO_IMPERSONATION;
165 cygheap->user.current_token = NO_IMPERSONATION;
166 cygheap->user.set_saved_sid (); /* Update the original sid */
170 getlogin_r (char *name, size_t namesize)
172 char *login = getlogin ();
173 size_t len = strlen (login) + 1;
176 int err = __check_null_invalid_struct (name, len);
179 strncpy (name, login, len);
186 return strcpy (_my_tls.locals.username, cygheap->user.name ());
192 return cygheap->user.real_uid;
198 return cygheap->user.real_uid;
204 return cygheap->user.real_gid;
210 return cygheap->user.real_gid;
237 /* Not quite right - cuserid can change, getlogin can't */
244 strcpy (src, getlogin ());
249 cygheap_user::ontherange (homebodies what, struct passwd *pw)
251 LPUSER_INFO_3 ui = NULL;
252 WCHAR wuser[UNLEN + 1];
254 char homepath_env_buf[CYG_MAX_PATH + 1];
255 char homedrive_env_buf[3];
256 char *newhomedrive = NULL;
257 char *newhomepath = NULL;
260 debug_printf ("what %d, pw %p", what, pw);
265 newhomedrive = homedrive;
266 else if ((p = getenv ("HOMEDRIVE")))
270 newhomepath = homepath;
271 else if ((p = getenv ("HOMEPATH")))
274 if ((p = getenv ("HOME")))
275 debug_printf ("HOME is already in the environment %s", p);
278 if (pw && pw->pw_dir && *pw->pw_dir)
280 debug_printf ("Set HOME (from /etc/passwd) to %s", pw->pw_dir);
281 setenv ("HOME", pw->pw_dir, 1);
283 else if (!newhomedrive || !newhomepath)
284 setenv ("HOME", "/", 1);
287 char home[CYG_MAX_PATH];
288 char buf[CYG_MAX_PATH + 1];
289 strcpy (buf, newhomedrive);
290 strcat (buf, newhomepath);
291 cygwin_conv_to_full_posix_path (buf, home);
292 debug_printf ("Set HOME (from HOMEDRIVE/HOMEPATH) to %s", home);
293 setenv ("HOME", home, 1);
298 if (what != CH_HOME && homepath == NULL && newhomepath == NULL)
301 pw = internal_getpwnam (name ());
302 if (pw && pw->pw_dir && *pw->pw_dir)
303 cygwin_conv_to_full_win32_path (pw->pw_dir, homepath_env_buf);
306 homepath_env_buf[0] = homepath_env_buf[1] = '\0';
309 WCHAR wlogsrv[INTERNET_MAX_HOST_NAME_LENGTH + 3];
310 sys_mbstowcs (wlogsrv, logsrv (),
311 sizeof (wlogsrv) / sizeof (*wlogsrv));
312 sys_mbstowcs (wuser, winname (), sizeof (wuser) / sizeof (*wuser));
313 if (!(ret = NetUserGetInfo (wlogsrv, wuser, 3, (LPBYTE *) &ui)))
315 sys_wcstombs (homepath_env_buf, ui->usri3_home_dir, CYG_MAX_PATH);
316 if (!homepath_env_buf[0])
318 sys_wcstombs (homepath_env_buf, ui->usri3_home_dir_drive,
320 if (homepath_env_buf[0])
321 strcat (homepath_env_buf, "\\");
323 cygwin_conv_to_full_win32_path ("/", homepath_env_buf);
328 NetApiBufferFree (ui);
331 if (homepath_env_buf[1] != ':')
333 newhomedrive = almost_null;
334 newhomepath = homepath_env_buf;
338 homedrive_env_buf[0] = homepath_env_buf[0];
339 homedrive_env_buf[1] = homepath_env_buf[1];
340 homedrive_env_buf[2] = '\0';
341 newhomedrive = homedrive_env_buf;
342 newhomepath = homepath_env_buf + 2;
346 if (newhomedrive && newhomedrive != homedrive)
347 cfree_and_set (homedrive, (newhomedrive == almost_null)
348 ? almost_null : cstrdup (newhomedrive));
350 if (newhomepath && newhomepath != homepath)
351 cfree_and_set (homepath, cstrdup (newhomepath));
365 cygheap_user::test_uid (char *&what, const char *name, size_t namelen)
367 if (!what && !issetuid ())
368 what = getwinenveq (name, namelen, HEAP_STR);
373 cygheap_user::env_logsrv (const char *name, size_t namelen)
375 if (test_uid (plogsrv, name, namelen))
378 const char *mydomain = domain ();
379 const char *myname = winname ();
380 if (!mydomain || strcasematch (myname, "SYSTEM"))
383 char logsrv[INTERNET_MAX_HOST_NAME_LENGTH + 3];
384 cfree_and_set (plogsrv, almost_null);
385 if (get_logon_server (mydomain, logsrv, NULL))
386 plogsrv = cstrdup (logsrv);
391 cygheap_user::env_domain (const char *name, size_t namelen)
393 if (pwinname && test_uid (pdomain, name, namelen))
396 char username[UNLEN + 1];
397 DWORD ulen = sizeof (username);
398 char userdomain[DNLEN + 1];
399 DWORD dlen = sizeof (userdomain);
402 cfree_and_set (pwinname, almost_null);
403 cfree_and_set (pdomain, almost_null);
404 if (!LookupAccountSid (NULL, sid (), username, &ulen,
405 userdomain, &dlen, &use))
409 pwinname = cstrdup (username);
410 pdomain = cstrdup (userdomain);
416 cygheap_user::env_userprofile (const char *name, size_t namelen)
418 if (test_uid (puserprof, name, namelen))
421 char userprofile_env_buf[CYG_MAX_PATH + 1];
422 char win_id[UNLEN + 1]; /* Large enough for SID */
424 cfree_and_set (puserprof, almost_null);
425 if (get_registry_hive_path (get_windows_id (win_id), userprofile_env_buf))
426 puserprof = cstrdup (userprofile_env_buf);
432 cygheap_user::env_homepath (const char *name, size_t namelen)
434 return ontherange (CH_HOMEPATH);
438 cygheap_user::env_homedrive (const char *name, size_t namelen)
440 return ontherange (CH_HOMEDRIVE);
444 cygheap_user::env_name (const char *name, size_t namelen)
446 if (!test_uid (pwinname, name, namelen))
452 cygheap_user::env_systemroot (const char *name, size_t namelen)
456 int size = GetWindowsDirectory (NULL, 0);
459 psystemroot = (char *) cmalloc (HEAP_STR, ++size);
460 size = GetWindowsDirectory (psystemroot, size);
468 debug_printf ("GetWindowsDirectory(), %E");
474 pwdgrp::next_str (char c)
477 lptr = strechr (lptr, c);
484 pwdgrp::next_num (unsigned long& n)
486 char *p = next_str (':');
488 n = strtoul (p, &cp, 10);
489 return p != cp && !*cp;
493 pwdgrp::add_line (char *eptr)
498 eptr = strchr (lptr, '\n');
501 if (eptr > lptr && eptr[-1] == '\r')
507 if (curr_lines >= max_lines)
510 *pwdgrp_buf = realloc (*pwdgrp_buf, max_lines * pwdgrp_buf_elem_size);
512 if ((this->*parse) ())
519 pwdgrp::load (const char *posix_fname)
522 static const char failed[] = "failed";
523 static const char succeeded[] = "succeeded";
530 pc.check (posix_fname);
531 etc_ix = etc::init (etc_ix, pc);
533 paranoid_printf ("%s", posix_fname);
535 if (pc.error || !pc.exists () || pc.isdir ())
537 paranoid_printf ("strange path_conv problem");
542 HANDLE fh = CreateFile (pc, GENERIC_READ, wincap.shared (), NULL,
543 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
544 if (fh == INVALID_HANDLE_VALUE)
546 paranoid_printf ("%s CreateFile failed, %E");
551 DWORD size = GetFileSize (fh, NULL), read_bytes;
552 buf = (char *) malloc (size + 1);
553 if (!ReadFile (fh, buf, size, &read_bytes, NULL))
555 paranoid_printf ("ReadFile failed, %E");
565 buf[read_bytes] = '\0';
567 while ((eptr = add_line (eptr)))
569 debug_printf ("%s curr_lines %d", posix_fname, curr_lines);
575 debug_printf ("%s load %s", posix_fname, res);