dvitomp fix from Akira
[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 extern char *get_home_directory(void);
39
40 #define PASSWD_FIELD_SIZE 256
41
42 static char the_passwd_name[PASSWD_FIELD_SIZE];
43 static char the_passwd_passwd[PASSWD_FIELD_SIZE];
44 static char the_passwd_gecos[PASSWD_FIELD_SIZE];
45 static char the_passwd_dir[PASSWD_FIELD_SIZE];
46 static char the_passwd_shell[PASSWD_FIELD_SIZE];
47
48 struct passwd the_passwd = 
49 {
50   the_passwd_name,
51   the_passwd_passwd,
52   0,
53   0,
54   0,
55   the_passwd_gecos,
56   the_passwd_dir,
57   the_passwd_shell,
58 };
59
60 int 
61 getuid () 
62
63   return the_passwd.pw_uid;
64 }
65
66 int 
67 geteuid () 
68
69   /* I could imagine arguing for checking to see whether the user is
70      in the Administrators group and returning a UID of 0 for that
71      case, but I don't know how wise that would be in the long run.  */
72   return getuid (); 
73 }
74
75 int 
76 getgid () 
77
78   return the_passwd.pw_gid;
79 }
80
81 int 
82 getegid () 
83
84   return getgid ();
85 }
86
87 struct passwd *
88 getpwuid (int uid)
89 {
90   if (uid == the_passwd.pw_uid)
91     return &the_passwd;
92   return NULL;
93 }
94
95 struct passwd *
96 getpwnam (const char *name)
97 {
98   struct passwd *pw;
99   
100   pw = getpwuid (getuid ());
101   if (!pw)
102     return pw;
103
104   if (stricmp (name, pw->pw_name))
105     return NULL;
106
107   return pw;
108 }
109
110 void
111 init_user_info ()
112 {
113   /* Find the user's real name by opening the process token and
114      looking up the name associated with the user-sid in that token.
115
116      Use the relative portion of the identifier authority value from
117      the user-sid as the user id value (same for group id using the
118      primary group sid from the process token). */
119 #if 0
120   char            user_sid[256], name[256], domain[256];
121   DWORD           length = sizeof (name), dlength = sizeof (domain), trash;
122   HANDLE          token = NULL;
123   SID_NAME_USE    user_type;
124
125   if (OpenProcessToken (GetCurrentProcess (), TOKEN_QUERY, &token)
126       && GetTokenInformation (token, TokenUser,
127                               (PVOID) user_sid, sizeof (user_sid), &trash)
128       && LookupAccountSid (NULL, *((PSID *) user_sid), name, &length,
129                            domain, &dlength, &user_type))
130     {
131       strcpy (the_passwd.pw_name, name);
132       /* Determine a reasonable uid value. */
133       if (stricmp ("administrator", name) == 0)
134         {
135           the_passwd.pw_uid = 0;
136           the_passwd.pw_gid = 0;
137         }
138       else
139         {
140           SID_IDENTIFIER_AUTHORITY * pSIA;
141
142           pSIA = GetSidIdentifierAuthority (*((PSID *) user_sid));
143           /* I believe the relative portion is the last 4 bytes (of 6)
144              with msb first. */
145           the_passwd.pw_uid = ((pSIA->Value[2] << 24) +
146                                (pSIA->Value[3] << 16) +
147                                (pSIA->Value[4] << 8)  +
148                                (pSIA->Value[5] << 0));
149           /* restrict to conventional uid range for normal users */
150           the_passwd.pw_uid = the_passwd.pw_uid % 60001;
151
152           /* Get group id */
153           if (GetTokenInformation (token, TokenPrimaryGroup,
154                                    (PVOID) user_sid, sizeof (user_sid), &trash))
155             {
156               SID_IDENTIFIER_AUTHORITY * pSIA;
157
158               pSIA = GetSidIdentifierAuthority (*((PSID *) user_sid));
159               the_passwd.pw_gid = ((pSIA->Value[2] << 24) +
160                                    (pSIA->Value[3] << 16) +
161                                    (pSIA->Value[4] << 8)  +
162                                    (pSIA->Value[5] << 0));
163               /* I don't know if this is necessary, but for safety... */
164               the_passwd.pw_gid = the_passwd.pw_gid % 60001;
165             }
166           else
167             the_passwd.pw_gid = the_passwd.pw_uid;
168         }
169     }
170   /* If security calls are not supported (presumably because we
171        are running under Windows 95), fallback to this. */
172   else 
173     if (GetUserName (name, &length))
174     {
175       strcpy (the_passwd.pw_name, name);
176       if (stricmp ("administrator", name) == 0)
177         the_passwd.pw_uid = 0;
178       else
179         the_passwd.pw_uid = 123;
180       the_passwd.pw_gid = the_passwd.pw_uid;
181     }
182   else
183 #else
184     {
185       strcpy (the_passwd.pw_name, "unknown");
186       the_passwd.pw_uid = 123;
187       the_passwd.pw_gid = 123;
188     }
189 #endif
190
191   /* Ensure HOME and SHELL are defined. */
192 #if 1
193   /*
194    * With XEmacs, setting $HOME is deprecated.
195    * But with fpTeX, it is safer to assume $HOME defined.
196    */
197   {
198     char *home = get_home_directory();
199     if (home) {
200       putenv(concat("HOME=", home));
201     }
202     else {
203       putenv ("HOME=c:/");
204     }
205   }
206 #endif
207   if (getenv ("SHELL") == NULL)
208     putenv ((GetVersion () & 0x80000000) ? "SHELL=command" : "SHELL=cmd");
209
210   {
211     /* Win2K problem : we need a specific TEMP directory with 
212        full access rights so that any user building a format file
213        or a font file will build it with full access rights. The installer 
214        takes care of defining TEXMFTEMP=$SELFAUTOPARENT/tmp in the environment.
215        If it is defined, then use it as the TEMP and TMP variables.
216     */
217     char *p;
218     if ((p = getenv("TEXMFTEMP")) != NULL) {
219       putenv(concat("TEMP=", p));
220       putenv(concat("TMP=", p));
221     }
222   }
223
224   /* Set dir and shell from environment variables. */
225   strcpy (the_passwd.pw_dir, get_home_directory());
226   strcpy (the_passwd.pw_shell, getenv ("SHELL"));
227
228 #if 0
229   if (token)
230     CloseHandle (token);
231 #endif
232 }
233
234 #endif