OSDN Git Service

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