OSDN Git Service

* security.h: Declare internal_getpwsid and internal_getgrsid.
[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 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 <errno.h>
21 #include <sys/cygwin.h>
22 #include "pinfo.h"
23 #include "security.h"
24 #include "fhandler.h"
25 #include "path.h"
26 #include "dtable.h"
27 #include "cygerrno.h"
28 #include "cygheap.h"
29 #include "registry.h"
30 #include "child_info.h"
31 #include "environ.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 = DEFAULT_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           if ((pw = internal_getpwsid (tu)))
65             user.set_name (pw->pw_name);
66           /* Set token owner to the same value as token user */
67           if (!SetTokenInformation (ptok, TokenOwner, &tu, sizeof tu))
68             debug_printf ("SetTokenInformation(TokenOwner): %E");
69          }
70     }
71
72   if (!pw && !(pw = getpwnam (user.name ())))
73     debug_printf("user name not found in augmented /etc/passwd");
74   else
75     {
76       myself->uid = pw->pw_uid;
77       myself->gid = pw->pw_gid;
78       if (wincap.has_security ())
79         {
80           cygsid gsid;
81           if (gsid.getfromgr (getgrgid32 (pw->pw_gid)))
82             {
83               /* Set primary group to the group in /etc/passwd. */
84               user.groups.pgsid = gsid;
85               if (!SetTokenInformation (ptok, TokenPrimaryGroup,
86                                         &gsid, sizeof gsid))
87                 debug_printf ("SetTokenInformation(TokenPrimaryGroup): %E");
88             }
89           else
90             debug_printf ("gsid not found in augmented /etc/group");
91         }
92     }
93   if (ptok != INVALID_HANDLE_VALUE)
94     CloseHandle (ptok);
95   (void) cygheap->user.ontherange (CH_HOME, pw);
96
97   return;
98 }
99
100 void
101 uinfo_init ()
102 {
103   if (!child_proc_info)
104     internal_getlogin (cygheap->user); /* Set the cygheap->user. */
105
106   /* Real and effective uid/gid are identical on process start up. */
107   cygheap->user.orig_uid = cygheap->user.real_uid = myself->uid;
108   cygheap->user.orig_gid = cygheap->user.real_gid = myself->gid;
109   cygheap->user.set_orig_sid ();        /* Update the original sid */
110
111   cygheap->user.token = INVALID_HANDLE_VALUE; /* No token present */
112 }
113
114 extern "C" char *
115 getlogin (void)
116 {
117 #ifdef _MT_SAFE
118   char *this_username=_reent_winsup ()->_username;
119 #else
120   static char this_username[UNLEN + 1] NO_COPY;
121 #endif
122
123   return strcpy (this_username, cygheap->user.name ());
124 }
125
126 extern "C" __uid32_t
127 getuid32 (void)
128 {
129   return cygheap->user.real_uid;
130 }
131
132 extern "C" __uid16_t
133 getuid (void)
134 {
135   return cygheap->user.real_uid;
136 }
137
138 extern "C" __gid32_t
139 getgid32 (void)
140 {
141   return cygheap->user.real_gid;
142 }
143
144 extern "C" __gid16_t
145 getgid (void)
146 {
147   return cygheap->user.real_gid;
148 }
149
150 extern "C" __uid32_t
151 geteuid32 (void)
152 {
153   return myself->uid;
154 }
155
156 extern "C" __uid16_t
157 geteuid (void)
158 {
159   return myself->uid;
160 }
161
162 extern "C" __gid32_t
163 getegid32 (void)
164 {
165   return myself->gid;
166 }
167
168 extern "C" __gid16_t
169 getegid (void)
170 {
171   return myself->gid;
172 }
173
174 /* Not quite right - cuserid can change, getlogin can't */
175 extern "C" char *
176 cuserid (char *src)
177 {
178   if (!src)
179     return getlogin ();
180
181   strcpy (src, getlogin ());
182   return src;
183 }
184
185 const char *
186 cygheap_user::ontherange (homebodies what, struct passwd *pw)
187 {
188   LPUSER_INFO_3 ui = NULL;
189   WCHAR wuser[UNLEN + 1];
190   NET_API_STATUS ret;
191   char homepath_env_buf[MAX_PATH + 1];
192   char homedrive_env_buf[3];
193   char *newhomedrive = NULL;
194   char *newhomepath = NULL;
195
196
197   debug_printf ("what %d, pw %p", what, pw);
198   if (what == CH_HOME)
199     {
200       char *p;
201       if (homedrive)
202         newhomedrive = homedrive;
203       else if ((p = getenv ("HOMEDRIVE")))
204         newhomedrive = p;
205
206       if (homepath)
207         newhomepath = homepath;
208       else if ((p = getenv ("HOMEPATH")))
209         newhomepath = p;
210
211       if ((p = getenv ("HOME")))
212         debug_printf ("HOME is already in the environment %s", p);
213       else
214         {
215           if (!pw)
216             pw = getpwnam (name ());
217           if (pw && pw->pw_dir && *pw->pw_dir)
218             {
219               debug_printf ("Set HOME (from /etc/passwd) to %s", pw->pw_dir);
220               setenv ("HOME", pw->pw_dir, 1);
221             }
222           else if (!newhomedrive || !newhomepath)
223             setenv ("HOME", "/", 1);
224           else
225             {
226               char home[MAX_PATH];
227               char buf[MAX_PATH + 1];
228               strcpy (buf, newhomedrive);
229               strcat (buf, newhomepath);
230               cygwin_conv_to_full_posix_path (buf, home);
231               debug_printf ("Set HOME (from HOMEDRIVE/HOMEPATH) to %s", home);
232               setenv ("HOME", home, 1);
233             }
234         }
235     }
236
237   if (what != CH_HOME && homepath == NULL && newhomepath == NULL)
238     {
239       if (!pw)
240         pw = getpwnam (name ());
241       if (pw && pw->pw_dir && *pw->pw_dir)
242         cygwin_conv_to_full_win32_path (pw->pw_dir, homepath_env_buf);
243       else
244         {
245           homepath_env_buf[0] = homepath_env_buf[1] = '\0';
246           if (logsrv ())
247             {
248               WCHAR wlogsrv[INTERNET_MAX_HOST_NAME_LENGTH + 3];
249               sys_mbstowcs (wlogsrv, logsrv (),
250                             sizeof (wlogsrv) / sizeof (*wlogsrv));
251              sys_mbstowcs (wuser, winname (), sizeof (wuser) / sizeof (*wuser));
252               if (!(ret = NetUserGetInfo (wlogsrv, wuser, 3,(LPBYTE *)&ui)))
253                 {
254                   sys_wcstombs (homepath_env_buf, ui->usri3_home_dir, MAX_PATH);
255                   if (!homepath_env_buf[0])
256                     {
257                       sys_wcstombs (homepath_env_buf, ui->usri3_home_dir_drive,
258                                     MAX_PATH);
259                       if (homepath_env_buf[0])
260                         strcat (homepath_env_buf, "\\");
261                       else
262                         cygwin_conv_to_full_win32_path ("/", homepath_env_buf);
263                     }
264                 }
265             }
266           if (ui)
267             NetApiBufferFree (ui);
268         }
269
270       if (homepath_env_buf[1] != ':')
271         {
272           newhomedrive = almost_null;
273           newhomepath = homepath_env_buf;
274         }
275       else
276         {
277           homedrive_env_buf[0] = homepath_env_buf[0];
278           homedrive_env_buf[1] = homepath_env_buf[1];
279           homedrive_env_buf[2] = '\0';
280           newhomedrive = homedrive_env_buf;
281           newhomepath = homepath_env_buf + 2;
282         }
283     }
284
285   if (newhomedrive && newhomedrive != homedrive)
286     cfree_and_set (homedrive, (newhomedrive == almost_null)
287                               ? almost_null : cstrdup (newhomedrive));
288
289   if (newhomepath && newhomepath != homepath)
290     cfree_and_set (homepath, cstrdup (newhomepath));
291
292   switch (what)
293     {
294     case CH_HOMEDRIVE:
295       return homedrive;
296     case CH_HOMEPATH:
297       return homepath;
298     default:
299       return homepath;
300     }
301 }
302
303 const char *
304 cygheap_user::test_uid (char *&what, const char *name, size_t namelen)
305 {
306   if (!what && !issetuid ())
307     what = getwinenveq (name, namelen, HEAP_STR);
308   return what;
309 }
310
311 const char *
312 cygheap_user::env_logsrv (const char *name, size_t namelen)
313 {
314   if (test_uid (plogsrv, name, namelen))
315     return plogsrv;
316
317   const char *mydomain = domain ();
318   const char *myname = winname ();
319   if (!mydomain || strcasematch (myname, "SYSTEM"))
320     return almost_null;
321
322   char logsrv[INTERNET_MAX_HOST_NAME_LENGTH + 3];
323   cfree_and_set (plogsrv, almost_null);
324   if (get_logon_server (mydomain, logsrv, NULL))
325     plogsrv = cstrdup (logsrv);
326   return plogsrv;
327 }
328
329 const char *
330 cygheap_user::env_domain (const char *name, size_t namelen)
331 {
332   if (pwinname && test_uid (pdomain, name, namelen))
333     return pdomain;
334
335   char username[UNLEN + 1];
336   DWORD ulen = sizeof (username);
337   char userdomain[DNLEN + 1];
338   DWORD dlen = sizeof (userdomain);
339   SID_NAME_USE use;
340
341   cfree_and_set (pwinname, almost_null);
342   cfree_and_set (pdomain, almost_null);
343   if (!LookupAccountSid (NULL, sid (), username, &ulen,
344                          userdomain, &dlen, &use))
345     __seterrno ();
346   else
347     {
348       pwinname = cstrdup (username);
349       pdomain = cstrdup (userdomain);
350     }
351   return pdomain;
352 }
353
354 const char *
355 cygheap_user::env_userprofile (const char *name, size_t namelen)
356 {
357   if (test_uid (puserprof, name, namelen))
358     return puserprof;
359
360   char userprofile_env_buf[MAX_PATH + 1];
361   cfree_and_set (puserprof, almost_null);
362   /* FIXME: Should this just be setting a puserprofile like everything else? */
363   const char *myname = winname ();
364   if (myname && strcasematch (myname, "SYSTEM")
365       && get_registry_hive_path (sid (), userprofile_env_buf))
366     puserprof = cstrdup (userprofile_env_buf);
367
368   return puserprof;
369 }
370
371 const char *
372 cygheap_user::env_homepath (const char *name, size_t namelen)
373 {
374   return ontherange (CH_HOMEPATH);
375 }
376
377 const char *
378 cygheap_user::env_homedrive (const char *name, size_t namelen)
379 {
380   return ontherange (CH_HOMEDRIVE);
381 }
382
383 const char *
384 cygheap_user::env_name (const char *name, size_t namelen)
385 {
386   if (!test_uid (pwinname, name, namelen))
387     (void) domain ();
388   return pwinname;
389 }