OSDN Git Service

* autoload.cc (NetGetDCName): Change to make this an optional load function.
[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
38   if (wincap.has_security ())
39     {
40       HANDLE ptok = INVALID_HANDLE_VALUE;
41       DWORD siz;
42       cygsid tu;
43       DWORD ret = 0;
44
45       /* Try to get the SID either from current process and
46          store it in user.psid */
47       if (!OpenProcessToken (hMainProc, TOKEN_ADJUST_DEFAULT | TOKEN_QUERY,
48                              &ptok))
49         system_printf ("OpenProcessToken(): %E\n");
50       else if (!GetTokenInformation (ptok, TokenUser, &tu, sizeof tu, &siz))
51         system_printf ("GetTokenInformation(): %E");
52       else if (!(ret = user.set_sid (tu)))
53         system_printf ("Couldn't retrieve SID from access token!");
54        /* We must set the user name, uid and gid.
55          If we have a SID, try to get the corresponding Cygwin
56          password entry. Set user name which can be different
57          from the Windows user name */
58        if (ret)
59          {
60           cygsid gsid (NO_SID);
61           cygsid psid;
62
63           for (int pidx = 0; (pw = internal_getpwent (pidx)); ++pidx)
64             if (psid.getfrompw (pw) && EqualSid (user.sid (), psid))
65               {
66                 user.set_name (pw->pw_name);
67                 struct __group32 *gr = getgrgid32 (pw->pw_gid);
68                 if (gr)
69                   if (!gsid.getfromgr (gr))
70                       gsid = NO_SID;
71                 break;
72               }
73
74           /* Set token owner to the same value as token user and
75              primary group to the group in /etc/passwd. */
76           if (!SetTokenInformation (ptok, TokenOwner, &tu, sizeof tu))
77             debug_printf ("SetTokenInformation(TokenOwner): %E");
78           if (gsid && !SetTokenInformation (ptok, TokenPrimaryGroup,
79                                             &gsid, sizeof gsid))
80             debug_printf ("SetTokenInformation(TokenPrimaryGroup): %E");
81          }
82
83       if (ptok != INVALID_HANDLE_VALUE)
84         CloseHandle (ptok);
85     }
86
87   if (!pw)
88     pw = getpwnam (user.name ());
89
90   if (pw)
91     {
92       myself->uid = pw->pw_uid;
93       myself->gid = pw->pw_gid;
94     }
95   else
96     {
97       myself->uid = DEFAULT_UID;
98       myself->gid = DEFAULT_GID;
99     }
100
101   (void) cygheap->user.ontherange (CH_HOME, pw);
102
103   return;
104 }
105
106 void
107 uinfo_init ()
108 {
109   if (!child_proc_info)
110     internal_getlogin (cygheap->user); /* Set the cygheap->user. */
111
112   /* Real and effective uid/gid are identical on process start up. */
113   cygheap->user.orig_uid = cygheap->user.real_uid = myself->uid;
114   cygheap->user.orig_gid = cygheap->user.real_gid = myself->gid;
115   cygheap->user.set_orig_sid();      /* Update the original sid */
116
117   cygheap->user.token = INVALID_HANDLE_VALUE; /* No token present */
118 }
119
120 extern "C" char *
121 getlogin (void)
122 {
123 #ifdef _MT_SAFE
124   char *this_username=_reent_winsup ()->_username;
125 #else
126   static char this_username[UNLEN + 1] NO_COPY;
127 #endif
128
129   return strcpy (this_username, cygheap->user.name ());
130 }
131
132 extern "C" __uid32_t
133 getuid32 (void)
134 {
135   return cygheap->user.real_uid;
136 }
137
138 extern "C" __uid16_t
139 getuid (void)
140 {
141   return cygheap->user.real_uid;
142 }
143
144 extern "C" __gid32_t
145 getgid32 (void)
146 {
147   return cygheap->user.real_gid;
148 }
149
150 extern "C" __gid16_t
151 getgid (void)
152 {
153   return cygheap->user.real_gid;
154 }
155
156 extern "C" __uid32_t
157 geteuid32 (void)
158 {
159   return myself->uid;
160 }
161
162 extern "C" __uid16_t
163 geteuid (void)
164 {
165   return myself->uid;
166 }
167
168 extern "C" __gid32_t
169 getegid32 (void)
170 {
171   return myself->gid;
172 }
173
174 extern "C" __gid16_t
175 getegid (void)
176 {
177   return myself->gid;
178 }
179
180 /* Not quite right - cuserid can change, getlogin can't */
181 extern "C" char *
182 cuserid (char *src)
183 {
184   if (!src)
185     return getlogin ();
186
187   strcpy (src, getlogin ());
188   return src;
189 }
190
191 const char *
192 cygheap_user::ontherange (homebodies what, struct passwd *pw)
193 {
194   LPUSER_INFO_3 ui = NULL;
195   WCHAR wuser[UNLEN + 1];
196   NET_API_STATUS ret;
197   char homepath_env_buf[MAX_PATH + 1];
198   char homedrive_env_buf[3];
199   char *newhomedrive = NULL;
200   char *newhomepath = NULL;
201
202
203   debug_printf ("what %d, pw %p", what, pw);
204   if (what == CH_HOME)
205     {
206       char *p;
207       if ((p = getenv ("HOMEDRIVE")))
208         newhomedrive = p;
209
210       if ((p = getenv ("HOMEPATH")))
211         newhomepath = p;
212
213       if ((p = getenv ("HOME")))
214         debug_printf ("HOME is already in the environment %s", p);
215       else
216         {
217           if (!pw)
218             pw = getpwnam (name ());
219           if (pw && pw->pw_dir && *pw->pw_dir)
220             {
221               setenv ("HOME", pw->pw_dir, 1);
222               debug_printf ("Set HOME (from /etc/passwd) to %s", pw->pw_dir);
223             }
224           else if (newhomedrive && newhomepath)
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               setenv ("HOME", home, 1);
232               debug_printf ("Set HOME (from HOMEDRIVE/HOMEPATH) to %s", home);
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                   char *p;
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 if (!GetSystemDirectory (homepath_env_buf, MAX_PATH))
263                         strcpy (homepath_env_buf, "c:\\");
264                       else if ((p = strchr (homepath_env_buf, '\\')))
265                         p[1] = '\0';
266                     }
267                 }
268             }
269           if (ui)
270             NetApiBufferFree (ui);
271         }
272
273       if (homepath_env_buf[1] != ':')
274         {
275           newhomedrive = almost_null;
276           newhomepath = homepath_env_buf;
277         }
278       else
279         {
280           homedrive_env_buf[0] = homepath_env_buf[0];
281           homedrive_env_buf[1] = homepath_env_buf[1];
282           homedrive_env_buf[2] = '\0';
283           newhomedrive = homedrive_env_buf;
284           newhomepath = homepath_env_buf + 2;
285         }
286     }
287
288   if (newhomedrive)
289     cfree_and_set (homedrive, (newhomedrive == almost_null)
290                               ? almost_null : cstrdup (newhomedrive));
291
292   if (newhomepath)
293     cfree_and_set (homepath, cstrdup (newhomepath));
294
295   switch (what)
296     {
297     case CH_HOMEDRIVE:
298       return homedrive;
299     case CH_HOMEPATH:
300       return homepath;
301     default:
302       return homepath;
303     }
304 }
305
306 const char *
307 cygheap_user::test_uid (char *&what, const char *name, size_t namelen)
308 {
309   if (what)
310     return what;
311   if (orig_uid == myself->uid)
312     what = getwinenveq (name, namelen, HEAP_STR);
313   return what;
314 }
315
316 const char *
317 cygheap_user::env_logsrv (const char *name, size_t namelen)
318 {
319   if (test_uid (plogsrv, name, namelen))
320     return plogsrv;
321
322   const char *mydomain = domain ();
323   const char *myname = winname ();
324   if (!mydomain || strcasematch (myname, "SYSTEM"))
325     return almost_null;
326
327   char logsrv[INTERNET_MAX_HOST_NAME_LENGTH + 3];
328   cfree_and_set (plogsrv, almost_null);
329   if (get_logon_server (mydomain, logsrv, NULL))
330     plogsrv = cstrdup (logsrv);
331   return plogsrv;
332 }
333
334 const char *
335 cygheap_user::env_domain (const char *name, size_t namelen)
336 {
337   if (pwinname && test_uid (pdomain, name, namelen))
338     return pdomain;
339
340   char username[UNLEN + 1];
341   DWORD ulen = sizeof (username);
342   char userdomain[DNLEN + 1];
343   DWORD dlen = sizeof (userdomain);
344   SID_NAME_USE use;
345
346   cfree_and_set (pwinname, almost_null);
347   cfree_and_set (pdomain, almost_null);
348   if (!LookupAccountSid (NULL, sid (), username, &ulen,
349                          userdomain, &dlen, &use))
350     __seterrno ();
351   else
352     {
353       pwinname = cstrdup (username);
354       pdomain = cstrdup (userdomain);
355     }
356   return pdomain;
357 }
358
359 const char *
360 cygheap_user::env_userprofile (const char *name, size_t namelen)
361 {
362   if (test_uid (puserprof, name, namelen))
363     return puserprof;
364
365   char userprofile_env_buf[MAX_PATH + 1];
366   cfree_and_set (puserprof, almost_null);
367   /* FIXME: Should this just be setting a puserprofile like everything else? */
368   const char *myname = winname ();
369   if (myname && strcasematch (myname, "SYSTEM")
370       && get_registry_hive_path (sid (), userprofile_env_buf))
371     puserprof = cstrdup (userprofile_env_buf);
372
373   return puserprof;
374 }
375
376 const char *
377 cygheap_user::env_homepath (const char *name, size_t namelen)
378 {
379   return ontherange (CH_HOMEPATH);
380 }
381
382 const char *
383 cygheap_user::env_homedrive (const char *name, size_t namelen)
384 {
385   return ontherange (CH_HOMEDRIVE);
386 }
387
388 const char *
389 cygheap_user::env_name (const char *name, size_t namelen)
390 {
391   if (!test_uid (pwinname, name, namelen))
392     (void) domain ();
393   return pwinname;
394 }