OSDN Git Service

* Makefile.in (DLL_IMPORTS): Remove libadvapi32.a.
[pf3gnuchains/pf3gnuchains4x.git] / winsup / cygwin / uinfo.cc
1 /* uinfo.cc: user info (uid, gid, etc...)
2
3    Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
4    2006, 2007, 2008 Red Hat, Inc.
5
6 This file is part of Cygwin.
7
8 This software is a copyrighted work licensed under the terms of the
9 Cygwin license.  Please consult the file "CYGWIN_LICENSE" for
10 details. */
11
12 #include "winsup.h"
13 #include <unistd.h>
14 #include <wininet.h>
15 #include <stdlib.h>
16 #include <wchar.h>
17 #include <lm.h>
18 #include <iptypes.h>
19 #include <sys/cygwin.h>
20 #include "cygerrno.h"
21 #include "pinfo.h"
22 #include "path.h"
23 #include "fhandler.h"
24 #include "dtable.h"
25 #include "cygheap.h"
26 #include "shared_info.h"
27 #include "registry.h"
28 #include "child_info.h"
29 #include "environ.h"
30 #include "pwdgrp.h"
31 #include "tls_pbuf.h"
32 #include "ntdll.h"
33
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 */
37 void
38 cygheap_user::init ()
39 {
40   WCHAR user_name[UNLEN + 1];
41   DWORD user_name_len;
42
43   user_name_len = GetEnvironmentVariableW (L"USERNAME", user_name, UNLEN + 1);
44   if (user_name_len)
45     {
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);
50     }
51
52   DWORD siz;
53   PSECURITY_DESCRIPTOR psd;
54
55   if (!GetTokenInformation (hProcToken, TokenPrimaryGroup,
56                             &groups.pgsid, sizeof (cygsid), &siz))
57     system_printf ("GetTokenInformation (TokenPrimaryGroup), %E");
58
59   /* Get the SID from current process and store it in effec_cygsid */
60   if (!GetTokenInformation (hProcToken, TokenUser, &effec_cygsid,
61                             sizeof (cygsid), &siz))
62     {
63       system_printf ("GetTokenInformation (TokenUser), %E");
64       return;
65     }
66
67   /* Set token owner to the same value as token user */
68   if (!SetTokenInformation (hProcToken, TokenOwner, &effec_cygsid,
69                             sizeof (cygsid)))
70     debug_printf ("SetTokenInformation(TokenOwner), %E");
71
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;
76
77   BOOL acl_exists, dummy;
78   TOKEN_DEFAULT_DACL dacl;
79   if (GetSecurityDescriptorDacl (psd, &acl_exists, &dacl.DefaultDacl, &dummy)
80       && acl_exists && dacl.DefaultDacl)
81     {
82       NTSTATUS status;
83
84       /* Set the default DACL and the process DACL */
85       if (!SetTokenInformation (hProcToken, TokenDefaultDacl, &dacl,
86                                 sizeof (dacl)))
87         system_printf ("SetTokenInformation (TokenDefaultDacl), %E");
88       if ((status = NtSetSecurityObject (NtCurrentProcess (),
89                                          DACL_SECURITY_INFORMATION, psd)))
90         system_printf ("NtSetSecurityObject, %lx", status);
91     }
92   else
93     system_printf("Cannot get dacl, %E");
94 }
95
96 void
97 internal_getlogin (cygheap_user &user)
98 {
99   struct passwd *pw = NULL;
100
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 ())
104     {
105       WCHAR user_name[UNLEN + 1];
106       DWORD user_name_len = UNLEN + 1;
107
108       if (GetUserNameW (user_name, &user_name_len))
109         {
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);
113         }
114       else
115         user.set_name ("unknown");
116     }
117   else
118     {
119       cygpsid psid = user.sid ();
120       pw = internal_getpwsid (psid);
121     }
122
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");
126   else
127     {
128       cygsid gsid;
129
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)))
134         {
135           if (gsid != user.groups.pgsid)
136             {
137               /* Set primary group to the group in /etc/passwd. */
138               if (!SetTokenInformation (hProcToken, TokenPrimaryGroup,
139                                         &gsid, sizeof gsid))
140                 debug_printf ("SetTokenInformation(TokenPrimaryGroup), %E");
141               else
142                 user.groups.pgsid = gsid;
143               clear_procimptoken ();
144             }
145         }
146       else
147         debug_printf ("gsid not found in augmented /etc/group");
148     }
149   cygheap->user.ontherange (CH_HOME, pw);
150 }
151
152 void
153 uinfo_init ()
154 {
155   if (child_proc_info && !cygheap->user.has_impersonation_tokens ())
156     return;
157
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)
167     {
168       cygheap->user.reimpersonate ();
169       return;
170     }
171   else
172     cygheap->user.close_impersonation_tokens ();
173
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 ();
185 }
186
187 extern "C" int
188 getlogin_r (char *name, size_t namesize)
189 {
190   char *login = getlogin ();
191   size_t len = strlen (login) + 1;
192   if (len > namesize)
193     return ERANGE;
194   myfault efault;
195   if (efault.faulted ())
196     return EFAULT;
197   strncpy (name, login, len);
198   return 0;
199 }
200
201 extern "C" char *
202 getlogin (void)
203 {
204   return strcpy (_my_tls.locals.username, cygheap->user.name ());
205 }
206
207 extern "C" __uid32_t
208 getuid32 (void)
209 {
210   return cygheap->user.real_uid;
211 }
212
213 extern "C" __uid16_t
214 getuid (void)
215 {
216   return cygheap->user.real_uid;
217 }
218
219 extern "C" __gid32_t
220 getgid32 (void)
221 {
222   return cygheap->user.real_gid;
223 }
224
225 extern "C" __gid16_t
226 getgid (void)
227 {
228   return cygheap->user.real_gid;
229 }
230
231 extern "C" __uid32_t
232 geteuid32 (void)
233 {
234   return myself->uid;
235 }
236
237 extern "C" __uid16_t
238 geteuid (void)
239 {
240   return myself->uid;
241 }
242
243 extern "C" __gid32_t
244 getegid32 (void)
245 {
246   return myself->gid;
247 }
248
249 extern "C" __gid16_t
250 getegid (void)
251 {
252   return myself->gid;
253 }
254
255 /* Not quite right - cuserid can change, getlogin can't */
256 extern "C" char *
257 cuserid (char *src)
258 {
259   if (!src)
260     return getlogin ();
261
262   strcpy (src, getlogin ());
263   return src;
264 }
265
266 const char *
267 cygheap_user::ontherange (homebodies what, struct passwd *pw)
268 {
269   LPUSER_INFO_3 ui = NULL;
270   WCHAR wuser[UNLEN + 1];
271   NET_API_STATUS ret;
272   char homedrive_env_buf[3];
273   char *newhomedrive = NULL;
274   char *newhomepath = NULL;
275   tmp_pathbuf tp;
276
277   debug_printf ("what %d, pw %p", what, pw);
278   if (what == CH_HOME)
279     {
280       char *p;
281
282       if ((p = getenv ("HOME")))
283         debug_printf ("HOME is already in the environment %s", p);
284       else
285         {
286           if (pw && pw->pw_dir && *pw->pw_dir)
287             {
288               debug_printf ("Set HOME (from /etc/passwd) to %s", pw->pw_dir);
289               setenv ("HOME", pw->pw_dir, 1);
290             }
291           else
292             {
293               char home[strlen (name ()) + 8];
294
295               debug_printf ("Set HOME to default /home/USER");
296               __small_sprintf (home, "/home/%s", name ());
297               setenv ("HOME", home, 1);
298             }
299         }
300     }
301
302   if (what != CH_HOME && homepath == NULL && newhomepath == NULL)
303     {
304       char *homepath_env_buf = tp.c_get ();
305       if (!pw)
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,
309                           NT_MAX_PATH);
310       else
311         {
312           homepath_env_buf[0] = homepath_env_buf[1] = '\0';
313           if (logsrv ())
314             {
315               WCHAR wlogsrv[INTERNET_MAX_HOST_NAME_LENGTH + 3];
316               sys_mbstowcs (wlogsrv, sizeof (wlogsrv) / sizeof (*wlogsrv),
317                             logsrv ());
318              sys_mbstowcs (wuser, sizeof (wuser) / sizeof (*wuser), winname ());
319               if (!(ret = NetUserGetInfo (wlogsrv, wuser, 3, (LPBYTE *) &ui)))
320                 {
321                   sys_wcstombs (homepath_env_buf, NT_MAX_PATH,
322                                 ui->usri3_home_dir);
323                   if (!homepath_env_buf[0])
324                     {
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, "\\");
329                       else
330                         cygwin_conv_path (CCP_POSIX_TO_WIN_A | CCP_ABSOLUTE,
331                                           "/", homepath_env_buf, NT_MAX_PATH);
332                     }
333                 }
334             }
335           if (ui)
336             NetApiBufferFree (ui);
337         }
338
339       if (homepath_env_buf[1] != ':')
340         {
341           newhomedrive = almost_null;
342           newhomepath = homepath_env_buf;
343         }
344       else
345         {
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;
351         }
352     }
353
354   if (newhomedrive && newhomedrive != homedrive)
355     cfree_and_set (homedrive, (newhomedrive == almost_null)
356                               ? almost_null : cstrdup (newhomedrive));
357
358   if (newhomepath && newhomepath != homepath)
359     cfree_and_set (homepath, cstrdup (newhomepath));
360
361   switch (what)
362     {
363     case CH_HOMEDRIVE:
364       return homedrive;
365     case CH_HOMEPATH:
366       return homepath;
367     default:
368       return homepath;
369     }
370 }
371
372 const char *
373 cygheap_user::test_uid (char *&what, const char *name, size_t namelen)
374 {
375   if (!what && !issetuid ())
376     what = getwinenveq (name, namelen, HEAP_STR);
377   return what;
378 }
379
380 const char *
381 cygheap_user::env_logsrv (const char *name, size_t namelen)
382 {
383   if (test_uid (plogsrv, name, namelen))
384     return plogsrv;
385
386   const char *mydomain = domain ();
387   const char *myname = winname ();
388   if (!mydomain || ascii_strcasematch (myname, "SYSTEM"))
389     return almost_null;
390
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);
397   return plogsrv;
398 }
399
400 const char *
401 cygheap_user::env_domain (const char *name, size_t namelen)
402 {
403   if (pwinname && test_uid (pdomain, name, namelen))
404     return pdomain;
405
406   char username[UNLEN + 1];
407   DWORD ulen = sizeof (username);
408   char userdomain[DNLEN + 1];
409   DWORD dlen = sizeof (userdomain);
410   SID_NAME_USE use;
411
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))
416     __seterrno ();
417   else
418     {
419       pwinname = cstrdup (username);
420       pdomain = cstrdup (userdomain);
421     }
422   return pdomain;
423 }
424
425 const char *
426 cygheap_user::env_userprofile (const char *name, size_t namelen)
427 {
428   if (test_uid (puserprof, name, namelen))
429     return puserprof;
430
431   WCHAR userprofile_env_buf[NT_MAX_PATH];
432   WCHAR win_id[UNLEN + 1]; /* Large enough for SID */
433
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);
437
438   return puserprof;
439 }
440
441 const char *
442 cygheap_user::env_homepath (const char *name, size_t namelen)
443 {
444   return ontherange (CH_HOMEPATH);
445 }
446
447 const char *
448 cygheap_user::env_homedrive (const char *name, size_t namelen)
449 {
450   return ontherange (CH_HOMEDRIVE);
451 }
452
453 const char *
454 cygheap_user::env_name (const char *name, size_t namelen)
455 {
456   if (!test_uid (pwinname, name, namelen))
457     domain ();
458   return pwinname;
459 }
460
461 const char *
462 cygheap_user::env_systemroot (const char *name, size_t namelen)
463 {
464   if (!psystemroot)
465     {
466       int size = GetWindowsDirectory (NULL, 0);
467       if (size > 0)
468         {
469           psystemroot = (char *) cmalloc_abort (HEAP_STR, ++size);
470           size = GetWindowsDirectory (psystemroot, size);
471           if (size <= 0)
472             {
473               cfree (psystemroot);
474               psystemroot = NULL;
475             }
476         }
477       if (size <= 0)
478         debug_printf ("GetWindowsDirectory(), %E");
479     }
480   return psystemroot;
481 }
482
483 char *
484 pwdgrp::next_str (char c)
485 {
486   char *res = lptr;
487   lptr = strechr (lptr, c);
488   if (*lptr)
489     *lptr++ = '\0';
490   return res;
491 }
492
493 bool
494 pwdgrp::next_num (unsigned long& n)
495 {
496   char *p = next_str (':');
497   char *cp;
498   n = strtoul (p, &cp, 10);
499   return p != cp && !*cp;
500 }
501
502 char *
503 pwdgrp::add_line (char *eptr)
504 {
505   if (eptr)
506     {
507       lptr = eptr;
508       eptr = strchr (lptr, '\n');
509       if (eptr)
510         {
511           if (eptr > lptr && eptr[-1] == '\r')
512             eptr[-1] = '\0';
513           else
514             *eptr = '\0';
515           eptr++;
516         }
517       if (curr_lines >= max_lines)
518         {
519           max_lines += 10;
520           *pwdgrp_buf = realloc (*pwdgrp_buf, max_lines * pwdgrp_buf_elem_size);
521         }
522       if ((this->*parse) ())
523         curr_lines++;
524     }
525   return eptr;
526 }
527
528 void
529 pwdgrp::load (const wchar_t *rel_path)
530 {
531   static const char failed[] = "failed";
532   static const char succeeded[] = "succeeded";
533   const char *res = failed;
534   HANDLE fh = NULL;
535
536   NTSTATUS status;
537   OBJECT_ATTRIBUTES attr;
538   IO_STATUS_BLOCK io;
539   FILE_STANDARD_INFORMATION fsi;
540
541   if (buf)
542     free (buf);
543   buf = NULL;
544   curr_lines = 0;
545
546   if (!path &&
547       !(path = (PWCHAR) malloc ((wcslen (installation_root)
548                                  + wcslen (rel_path) + 1) * sizeof (WCHAR))))
549     {
550       paranoid_printf ("malloc (%W) failed", rel_path);
551       goto out;
552     }
553   wcpcpy (wcpcpy (path, installation_root), rel_path);
554   RtlInitUnicodeString (&upath, path);
555
556   InitializeObjectAttributes (&attr, &upath, OBJ_CASE_INSENSITIVE, NULL, NULL);
557   etc_ix = etc::init (etc_ix, &attr);
558
559   paranoid_printf ("%S", &upath);
560
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))
566     {
567       paranoid_printf ("NtOpenFile(%S) failed, status %p", &upath, status);
568       goto out;
569     }
570   status = NtQueryInformationFile (fh, &io, &fsi, sizeof fsi,
571                                    FileStandardInformation);
572   if (!NT_SUCCESS (status))
573     {
574       paranoid_printf ("NtQueryInformationFile(%S) failed, status %p",
575                        &upath, status);
576       goto out;
577     }
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);
583   if (!buf)
584     {
585       paranoid_printf ("malloc (%d) failed", fsi.EndOfFile.LowPart);
586       goto out;
587     }
588   status = NtReadFile (fh, NULL, NULL, NULL, &io, buf, fsi.EndOfFile.LowPart,
589                        NULL, NULL);
590   if (!NT_SUCCESS (status))
591     {
592       paranoid_printf ("NtReadFile(%S) failed, status %p", &upath, status);
593       free (buf);
594       goto out;
595     }
596   buf[fsi.EndOfFile.LowPart] = '\0';
597   for (char *eptr = buf; (eptr = add_line (eptr)); )
598     continue;
599   debug_printf ("%W curr_lines %d", rel_path, curr_lines);
600   res = succeeded;
601
602 out:
603   if (fh)
604     NtClose (fh);
605   debug_printf ("%W load %s", rel_path, res);
606   initialized = true;
607 }