tentative fix for issue 3 (ex 53)
[mplib] / src / texk / kpathsea / user.c
1 /* Utility and Unix shadow routines for XEmacs on Windows NT.
2    Copyright (C) 1994, 1995 Free Software Foundation, Inc.
3
4 This file is part of XEmacs.
5
6 XEmacs is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
9 later version.
10
11 XEmacs is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with XEmacs; see the file COPYING.  If not, write to the Free
18 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19 02111-1307, USA.
20
21
22    Geoff Voelker (voelker@cs.washington.edu) 7-29-94 */
23
24 /* Adapted for XEmacs by David Hobley <david@spook-le0.cia.com.au> */
25 /* Sync'ed with Emacs 19.34.6 by Marc Paquette <marcpa@cam.org> */
26 /* Adapted to fpTeX 0.4 by Fabrice Popineau <Fabrice.Popineau@supelec.fr> */
27
28 #ifdef __MINGW32__
29
30 /* Emulate getpwuid, getpwnam and others.  */
31
32 #include <pwd.h>
33 #include <kpathsea/config.h>
34 #include <kpathsea/c-proto.h>
35 #include <kpathsea/win32lib.h>
36 #include <kpathsea/lib.h>
37
38 #define PASSWD_FIELD_SIZE 256
39
40 static char the_passwd_name[PASSWD_FIELD_SIZE];
41 static char the_passwd_passwd[PASSWD_FIELD_SIZE];
42 static char the_passwd_gecos[PASSWD_FIELD_SIZE];
43 static char the_passwd_dir[PASSWD_FIELD_SIZE];
44 static char the_passwd_shell[PASSWD_FIELD_SIZE];
45
46 struct passwd the_passwd = 
47 {
48   the_passwd_name,
49   the_passwd_passwd,
50   0,
51   0,
52   0,
53   the_passwd_gecos,
54   the_passwd_dir,
55   the_passwd_shell,
56 };
57
58 int 
59 getuid () 
60
61   return the_passwd.pw_uid;
62 }
63
64 int 
65 geteuid () 
66
67   /* I could imagine arguing for checking to see whether the user is
68      in the Administrators group and returning a UID of 0 for that
69      case, but I don't know how wise that would be in the long run.  */
70   return getuid (); 
71 }
72
73 int 
74 getgid () 
75
76   return the_passwd.pw_gid;
77 }
78
79 int 
80 getegid () 
81
82   return getgid ();
83 }
84
85 struct passwd *
86 getpwuid (int uid)
87 {
88   if (uid == the_passwd.pw_uid)
89     return &the_passwd;
90   return NULL;
91 }
92
93 struct passwd *
94 getpwnam (const char *name)
95 {
96   struct passwd *pw;
97   
98   pw = getpwuid (getuid ());
99   if (!pw)
100     return pw;
101
102   if (stricmp (name, pw->pw_name))
103     return NULL;
104
105   return pw;
106 }
107
108 void
109 init_user_info ()
110 {
111   /* Find the user's real name by opening the process token and
112      looking up the name associated with the user-sid in that token.
113
114      Use the relative portion of the identifier authority value from
115      the user-sid as the user id value (same for group id using the
116      primary group sid from the process token). */
117 #if 0
118   char            user_sid[256], name[256], domain[256];
119   DWORD           length = sizeof (name), dlength = sizeof (domain), trash;
120   HANDLE          token = NULL;
121   SID_NAME_USE    user_type;
122
123   if (OpenProcessToken (GetCurrentProcess (), TOKEN_QUERY, &token)
124       && GetTokenInformation (token, TokenUser,
125                               (PVOID) user_sid, sizeof (user_sid), &trash)
126       && LookupAccountSid (NULL, *((PSID *) user_sid), name, &length,
127                            domain, &dlength, &user_type))
128     {
129       strcpy (the_passwd.pw_name, name);
130       /* Determine a reasonable uid value. */
131       if (stricmp ("administrator", name) == 0)
132         {
133           the_passwd.pw_uid = 0;
134           the_passwd.pw_gid = 0;
135         }
136       else
137         {
138           SID_IDENTIFIER_AUTHORITY * pSIA;
139
140           pSIA = GetSidIdentifierAuthority (*((PSID *) user_sid));
141           /* I believe the relative portion is the last 4 bytes (of 6)
142              with msb first. */
143           the_passwd.pw_uid = ((pSIA->Value[2] << 24) +
144                                (pSIA->Value[3] << 16) +
145                                (pSIA->Value[4] << 8)  +
146                                (pSIA->Value[5] << 0));
147           /* restrict to conventional uid range for normal users */
148           the_passwd.pw_uid = the_passwd.pw_uid % 60001;
149
150           /* Get group id */
151           if (GetTokenInformation (token, TokenPrimaryGroup,
152                                    (PVOID) user_sid, sizeof (user_sid), &trash))
153             {
154               SID_IDENTIFIER_AUTHORITY * pSIA;
155
156               pSIA = GetSidIdentifierAuthority (*((PSID *) user_sid));
157               the_passwd.pw_gid = ((pSIA->Value[2] << 24) +
158                                    (pSIA->Value[3] << 16) +
159                                    (pSIA->Value[4] << 8)  +
160                                    (pSIA->Value[5] << 0));
161               /* I don't know if this is necessary, but for safety... */
162               the_passwd.pw_gid = the_passwd.pw_gid % 60001;
163             }
164           else
165             the_passwd.pw_gid = the_passwd.pw_uid;
166         }
167     }
168   /* If security calls are not supported (presumably because we
169        are running under Windows 95), fallback to this. */
170   else 
171     if (GetUserName (name, &length))
172     {
173       strcpy (the_passwd.pw_name, name);
174       if (stricmp ("administrator", name) == 0)
175         the_passwd.pw_uid = 0;
176       else
177         the_passwd.pw_uid = 123;
178       the_passwd.pw_gid = the_passwd.pw_uid;
179     }
180   else
181 #else
182     {
183       strcpy (the_passwd.pw_name, "unknown");
184       the_passwd.pw_uid = 123;
185       the_passwd.pw_gid = 123;
186     }
187 #endif
188
189   /* Ensure HOME and SHELL are defined. */
190 #if 1
191   /*
192    * With XEmacs, setting $HOME is deprecated.
193    * But with fpTeX, it is safer to assume $HOME defined.
194    */
195   {
196     char *home = get_home_directory();
197     if (home) {
198       putenv(concat("HOME=", home));
199     }
200     else {
201       putenv ("HOME=c:/");
202     }
203   }
204 #endif
205   if (getenv ("SHELL") == NULL)
206     putenv ((GetVersion () & 0x80000000) ? "SHELL=command" : "SHELL=cmd");
207
208   {
209     /* Win2K problem : we need a specific TEMP directory with 
210        full access rights so that any user building a format file
211        or a font file will build it with full access rights. The installer 
212        takes care of defining TEXMFTEMP=$SELFAUTOPARENT/tmp in the environment.
213        If it is defined, then use it as the TEMP and TMP variables.
214     */
215     char *p;
216     if ((p = getenv("TEXMFTEMP")) != NULL) {
217       putenv(concat("TEMP=", p));
218       putenv(concat("TMP=", p));
219     }
220   }
221
222   /* Set dir and shell from environment variables. */
223   strcpy (the_passwd.pw_dir, get_home_directory());
224   strcpy (the_passwd.pw_shell, getenv ("SHELL"));
225
226 #if 0
227   if (token)
228     CloseHandle (token);
229 #endif
230 }
231
232 #endif