OSDN Git Service

* cygheap.h (class cygheap_user): Use INVALID_HANDLE_VALUE as invalid
[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 Red Hat, Inc.
4
5 This file is part of Cygwin.
6
7 This software is a copyrighted work licensed under the terms of the
8 Cygwin license.  Please consult the file "CYGWIN_LICENSE" for
9 details. */
10
11 #include "winsup.h"
12 #include <pwd.h>
13 #include <unistd.h>
14 #include <winnls.h>
15 #include <wininet.h>
16 #include <utmp.h>
17 #include <limits.h>
18 #include <stdlib.h>
19 #include <lm.h>
20 #include <sys/cygwin.h>
21 #include "pinfo.h"
22 #include "security.h"
23 #include "fhandler.h"
24 #include "path.h"
25 #include "dtable.h"
26 #include "cygerrno.h"
27 #include "cygheap.h"
28 #include "registry.h"
29 #include "child_info.h"
30 #include "environ.h"
31 #include "pwdgrp.h"
32
33 void
34 internal_getlogin (cygheap_user &user)
35 {
36   struct passwd *pw = NULL;
37   HANDLE ptok = INVALID_HANDLE_VALUE;
38
39   myself->gid = UNKNOWN_GID;
40   if (wincap.has_security ())
41     {
42       DWORD siz;
43       cygsid tu;
44       DWORD ret = 0;
45
46       /* Try to get the SID either from current process and
47          store it in user.psid */
48       if (!OpenProcessToken (hMainProc, TOKEN_ADJUST_DEFAULT | TOKEN_QUERY,
49                              &ptok))
50         system_printf ("OpenProcessToken(): %E");
51       else if (!GetTokenInformation (ptok, TokenUser, &tu, sizeof tu, &siz))
52         system_printf ("GetTokenInformation (TokenUser): %E");
53       else if (!(ret = user.set_sid (tu)))
54         system_printf ("Couldn't retrieve SID from access token!");
55       else if (!GetTokenInformation (ptok, TokenPrimaryGroup,
56                                      &user.groups.pgsid, sizeof tu, &siz))
57         system_printf ("GetTokenInformation (TokenPrimaryGroup): %E");
58        /* We must set the user name, uid and gid.
59          If we have a SID, try to get the corresponding Cygwin
60          password entry. Set user name which can be different
61          from the Windows user name */
62       if (ret)
63         {
64           pw = internal_getpwsid (tu);
65           /* Set token owner to the same value as token user */
66           if (!SetTokenInformation (ptok, TokenOwner, &tu, sizeof tu))
67             debug_printf ("SetTokenInformation(TokenOwner): %E");
68          }
69     }
70
71   if (!pw && !(pw = internal_getpwnam (user.name ()))
72       && !(pw = internal_getpwuid (DEFAULT_UID)))
73     debug_printf("user not found in augmented /etc/passwd");
74   else
75     {
76       myself->uid = pw->pw_uid;
77       myself->gid = pw->pw_gid;
78       user.set_name (pw->pw_name);
79       if (wincap.has_security ())
80         {
81           cygsid gsid;
82           if (gsid.getfromgr (internal_getgrgid (pw->pw_gid)))
83             {
84               /* Set primary group to the group in /etc/passwd. */
85               if (!SetTokenInformation (ptok, TokenPrimaryGroup,
86                                         &gsid, sizeof gsid))
87                 debug_printf ("SetTokenInformation(TokenPrimaryGroup): %E");
88               else
89                 user.groups.pgsid = gsid;
90             }
91           else
92             debug_printf ("gsid not found in augmented /etc/group");
93         }
94     }
95   if (ptok != INVALID_HANDLE_VALUE)
96     CloseHandle (ptok);
97   (void) cygheap->user.ontherange (CH_HOME, pw);
98
99   return;
100 }
101
102 void
103 uinfo_init ()
104 {
105   if (child_proc_info && !cygheap->user.has_impersonation_tokens ())
106     return;
107
108   if (!child_proc_info)
109     internal_getlogin (cygheap->user); /* Set the cygheap->user. */
110   /* Conditions must match those in spawn to allow starting child
111      processes with ruid != euid and rgid != egid. */
112   else if (cygheap->user.issetuid ()
113            && cygheap->user.orig_uid == cygheap->user.real_uid
114            && cygheap->user.orig_gid == cygheap->user.real_gid
115            && !cygheap->user.groups.issetgroups ())
116     {
117       cygheap->user.reimpersonate ();
118       return;
119     }
120   else
121     cygheap->user.close_impersonation_tokens ();
122
123   cygheap->user.orig_uid = cygheap->user.real_uid = myself->uid;
124   cygheap->user.orig_gid = cygheap->user.real_gid = myself->gid;
125   cygheap->user.external_token = INVALID_HANDLE_VALUE;
126   cygheap->user.internal_token = INVALID_HANDLE_VALUE;
127   cygheap->user.current_token = INVALID_HANDLE_VALUE;
128   cygheap->user.set_orig_sid ();        /* Update the original sid */
129 }
130
131 extern "C" char *
132 getlogin (void)
133 {
134 #ifdef _MT_SAFE
135   char *this_username=_reent_winsup ()->_username;
136 #else
137   static char this_username[UNLEN + 1] NO_COPY;
138 #endif
139
140   return strcpy (this_username, cygheap->user.name ());
141 }
142
143 extern "C" __uid32_t
144 getuid32 (void)
145 {
146   return cygheap->user.real_uid;
147 }
148
149 extern "C" __uid16_t
150 getuid (void)
151 {
152   return cygheap->user.real_uid;
153 }
154
155 extern "C" __gid32_t
156 getgid32 (void)
157 {
158   return cygheap->user.real_gid;
159 }
160
161 extern "C" __gid16_t
162 getgid (void)
163 {
164   return cygheap->user.real_gid;
165 }
166
167 extern "C" __uid32_t
168 geteuid32 (void)
169 {
170   return myself->uid;
171 }
172
173 extern "C" __uid16_t
174 geteuid (void)
175 {
176   return myself->uid;
177 }
178
179 extern "C" __gid32_t
180 getegid32 (void)
181 {
182   return myself->gid;
183 }
184
185 extern "C" __gid16_t
186 getegid (void)
187 {
188   return myself->gid;
189 }
190
191 /* Not quite right - cuserid can change, getlogin can't */
192 extern "C" char *
193 cuserid (char *src)
194 {
195   if (!src)
196     return getlogin ();
197
198   strcpy (src, getlogin ());
199   return src;
200 }
201
202 const char *
203 cygheap_user::ontherange (homebodies what, struct passwd *pw)
204 {
205   LPUSER_INFO_3 ui = NULL;
206   WCHAR wuser[UNLEN + 1];
207   NET_API_STATUS ret;
208   char homepath_env_buf[MAX_PATH + 1];
209   char homedrive_env_buf[3];
210   char *newhomedrive = NULL;
211   char *newhomepath = NULL;
212
213
214   debug_printf ("what %d, pw %p", what, pw);
215   if (what == CH_HOME)
216     {
217       char *p;
218       if (homedrive)
219         newhomedrive = homedrive;
220       else if ((p = getenv ("HOMEDRIVE")))
221         newhomedrive = p;
222
223       if (homepath)
224         newhomepath = homepath;
225       else if ((p = getenv ("HOMEPATH")))
226         newhomepath = p;
227
228       if ((p = getenv ("HOME")))
229         debug_printf ("HOME is already in the environment %s", p);
230       else
231         {
232           if (pw && pw->pw_dir && *pw->pw_dir)
233             {
234               debug_printf ("Set HOME (from /etc/passwd) to %s", pw->pw_dir);
235               setenv ("HOME", pw->pw_dir, 1);
236             }
237           else if (!newhomedrive || !newhomepath)
238             setenv ("HOME", "/", 1);
239           else
240             {
241               char home[MAX_PATH];
242               char buf[MAX_PATH + 1];
243               strcpy (buf, newhomedrive);
244               strcat (buf, newhomepath);
245               cygwin_conv_to_full_posix_path (buf, home);
246               debug_printf ("Set HOME (from HOMEDRIVE/HOMEPATH) to %s", home);
247               setenv ("HOME", home, 1);
248             }
249         }
250     }
251
252   if (what != CH_HOME && homepath == NULL && newhomepath == NULL)
253     {
254       if (!pw)
255         pw = internal_getpwnam (name ());
256       if (pw && pw->pw_dir && *pw->pw_dir)
257         cygwin_conv_to_full_win32_path (pw->pw_dir, homepath_env_buf);
258       else
259         {
260           homepath_env_buf[0] = homepath_env_buf[1] = '\0';
261           if (logsrv ())
262             {
263               WCHAR wlogsrv[INTERNET_MAX_HOST_NAME_LENGTH + 3];
264               sys_mbstowcs (wlogsrv, logsrv (),
265                             sizeof (wlogsrv) / sizeof (*wlogsrv));
266              sys_mbstowcs (wuser, winname (), sizeof (wuser) / sizeof (*wuser));
267               if (!(ret = NetUserGetInfo (wlogsrv, wuser, 3,(LPBYTE *)&ui)))
268                 {
269                   sys_wcstombs (homepath_env_buf, ui->usri3_home_dir, MAX_PATH);
270                   if (!homepath_env_buf[0])
271                     {
272                       sys_wcstombs (homepath_env_buf, ui->usri3_home_dir_drive,
273                                     MAX_PATH);
274                       if (homepath_env_buf[0])
275                         strcat (homepath_env_buf, "\\");
276                       else
277                         cygwin_conv_to_full_win32_path ("/", homepath_env_buf);
278                     }
279                 }
280             }
281           if (ui)
282             NetApiBufferFree (ui);
283         }
284
285       if (homepath_env_buf[1] != ':')
286         {
287           newhomedrive = almost_null;
288           newhomepath = homepath_env_buf;
289         }
290       else
291         {
292           homedrive_env_buf[0] = homepath_env_buf[0];
293           homedrive_env_buf[1] = homepath_env_buf[1];
294           homedrive_env_buf[2] = '\0';
295           newhomedrive = homedrive_env_buf;
296           newhomepath = homepath_env_buf + 2;
297         }
298     }
299
300   if (newhomedrive && newhomedrive != homedrive)
301     cfree_and_set (homedrive, (newhomedrive == almost_null)
302                               ? almost_null : cstrdup (newhomedrive));
303
304   if (newhomepath && newhomepath != homepath)
305     cfree_and_set (homepath, cstrdup (newhomepath));
306
307   switch (what)
308     {
309     case CH_HOMEDRIVE:
310       return homedrive;
311     case CH_HOMEPATH:
312       return homepath;
313     default:
314       return homepath;
315     }
316 }
317
318 const char *
319 cygheap_user::test_uid (char *&what, const char *name, size_t namelen)
320 {
321   if (!what && !issetuid ())
322     what = getwinenveq (name, namelen, HEAP_STR);
323   return what;
324 }
325
326 const char *
327 cygheap_user::env_logsrv (const char *name, size_t namelen)
328 {
329   if (test_uid (plogsrv, name, namelen))
330     return plogsrv;
331
332   const char *mydomain = domain ();
333   const char *myname = winname ();
334   if (!mydomain || strcasematch (myname, "SYSTEM"))
335     return almost_null;
336
337   char logsrv[INTERNET_MAX_HOST_NAME_LENGTH + 3];
338   cfree_and_set (plogsrv, almost_null);
339   if (get_logon_server (mydomain, logsrv, NULL))
340     plogsrv = cstrdup (logsrv);
341   return plogsrv;
342 }
343
344 const char *
345 cygheap_user::env_domain (const char *name, size_t namelen)
346 {
347   if (pwinname && test_uid (pdomain, name, namelen))
348     return pdomain;
349
350   char username[UNLEN + 1];
351   DWORD ulen = sizeof (username);
352   char userdomain[DNLEN + 1];
353   DWORD dlen = sizeof (userdomain);
354   SID_NAME_USE use;
355
356   cfree_and_set (pwinname, almost_null);
357   cfree_and_set (pdomain, almost_null);
358   if (!LookupAccountSid (NULL, sid (), username, &ulen,
359                          userdomain, &dlen, &use))
360     __seterrno ();
361   else
362     {
363       pwinname = cstrdup (username);
364       pdomain = cstrdup (userdomain);
365     }
366   return pdomain;
367 }
368
369 const char *
370 cygheap_user::env_userprofile (const char *name, size_t namelen)
371 {
372   if (test_uid (puserprof, name, namelen))
373     return puserprof;
374
375   char userprofile_env_buf[MAX_PATH + 1];
376   cfree_and_set (puserprof, almost_null);
377   /* FIXME: Should this just be setting a puserprofile like everything else? */
378   const char *myname = winname ();
379   if (myname && strcasematch (myname, "SYSTEM")
380       && get_registry_hive_path (sid (), userprofile_env_buf))
381     puserprof = cstrdup (userprofile_env_buf);
382
383   return puserprof;
384 }
385
386 const char *
387 cygheap_user::env_homepath (const char *name, size_t namelen)
388 {
389   return ontherange (CH_HOMEPATH);
390 }
391
392 const char *
393 cygheap_user::env_homedrive (const char *name, size_t namelen)
394 {
395   return ontherange (CH_HOMEDRIVE);
396 }
397
398 const char *
399 cygheap_user::env_name (const char *name, size_t namelen)
400 {
401   if (!test_uid (pwinname, name, namelen))
402     (void) domain ();
403   return pwinname;
404 }
405
406 char *
407 pwdgrp::next_str (char c)
408 {
409   char *res = lptr;
410   lptr = strechr (lptr, c);
411   if (*lptr)
412     *lptr++ = '\0';
413   return res;
414 }
415
416 bool
417 pwdgrp::next_num (unsigned long& n)
418 {
419   char *p = next_str (':');
420   char *cp;
421   n = strtoul (p, &cp, 10);
422   return p != cp && !*cp;
423 }
424
425 char *
426 pwdgrp::add_line (char *eptr)
427 {
428   if (eptr)
429     {
430       lptr = eptr;
431       eptr = strchr (lptr, '\n');
432       if (eptr)
433         {
434           if (eptr > lptr && eptr[-1] == '\r')
435             eptr[-1] = '\0';
436           else
437             *eptr = '\0';
438           eptr++;
439         }
440       if (curr_lines >= max_lines)
441         {
442           max_lines += 10;
443           *pwdgrp_buf = realloc (*pwdgrp_buf, max_lines * pwdgrp_buf_elem_size);
444         }
445       if ((this->*parse) ())
446         curr_lines++;
447     }
448   return eptr;
449 }
450
451 void
452 pwdgrp::load (const char *posix_fname)
453 {
454   const char *res;
455   static const char failed[] = "failed";
456   static const char succeeded[] = "succeeded";
457
458   if (buf)
459     free (buf);
460   buf = NULL;
461
462   pc.check (posix_fname);
463   etc_ix = etc::init (etc_ix, pc);
464
465   paranoid_printf ("%s", posix_fname);
466
467   if (pc.error || !pc.exists () || !pc.isdisk () || pc.isdir ())
468     {
469       paranoid_printf ("strange path_conv problem");
470       res = failed;
471     }
472   else
473     {
474       HANDLE fh = CreateFile (pc, GENERIC_READ, wincap.shared (), NULL,
475                               OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
476       if (fh == INVALID_HANDLE_VALUE)
477         {
478           paranoid_printf ("%s CreateFile failed, %E");
479           res = failed;
480         }
481       else
482         {
483           DWORD size = GetFileSize (fh, NULL), read_bytes;
484           buf = (char *) malloc (size + 1);
485           if (!ReadFile (fh, buf, size, &read_bytes, NULL))
486             {
487               paranoid_printf ("ReadFile failed, %E");
488               CloseHandle (fh);
489               if (buf)
490                 free (buf);
491               buf = NULL;
492               res = failed;
493             }
494           else
495             {
496               CloseHandle (fh);
497               buf[read_bytes] = '\0';
498               char *eptr = buf;
499               curr_lines = 0;
500               while ((eptr = add_line (eptr)))
501                 continue;
502               debug_printf ("%s curr_lines %d", posix_fname, curr_lines);
503               res = succeeded;
504             }
505         }
506     }
507
508   debug_printf ("%s load %s", posix_fname, res);
509   initialized = true;
510   return;
511 }