1 /* Utility and Unix shadow routines for XEmacs on Windows NT.
2 Copyright (C) 1994, 1995 Free Software Foundation, Inc.
4 This file is part of XEmacs.
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
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
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
22 Geoff Voelker (voelker@cs.washington.edu) 7-29-94 */
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> */
30 /* Emulate getpwuid, getpwnam and others. */
33 #include <kpathsea/config.h>
34 #include <kpathsea/c-proto.h>
35 #include <kpathsea/win32lib.h>
36 #include <kpathsea/lib.h>
38 #define PASSWD_FIELD_SIZE 256
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];
46 struct passwd the_passwd =
61 return the_passwd.pw_uid;
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. */
76 return the_passwd.pw_gid;
88 if (uid == the_passwd.pw_uid)
94 getpwnam (const char *name)
98 pw = getpwuid (getuid ());
102 if (stricmp (name, pw->pw_name))
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.
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). */
118 char user_sid[256], name[256], domain[256];
119 DWORD length = sizeof (name), dlength = sizeof (domain), trash;
121 SID_NAME_USE user_type;
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))
129 strcpy (the_passwd.pw_name, name);
130 /* Determine a reasonable uid value. */
131 if (stricmp ("administrator", name) == 0)
133 the_passwd.pw_uid = 0;
134 the_passwd.pw_gid = 0;
138 SID_IDENTIFIER_AUTHORITY * pSIA;
140 pSIA = GetSidIdentifierAuthority (*((PSID *) user_sid));
141 /* I believe the relative portion is the last 4 bytes (of 6)
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;
151 if (GetTokenInformation (token, TokenPrimaryGroup,
152 (PVOID) user_sid, sizeof (user_sid), &trash))
154 SID_IDENTIFIER_AUTHORITY * pSIA;
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;
165 the_passwd.pw_gid = the_passwd.pw_uid;
168 /* If security calls are not supported (presumably because we
169 are running under Windows 95), fallback to this. */
171 if (GetUserName (name, &length))
173 strcpy (the_passwd.pw_name, name);
174 if (stricmp ("administrator", name) == 0)
175 the_passwd.pw_uid = 0;
177 the_passwd.pw_uid = 123;
178 the_passwd.pw_gid = the_passwd.pw_uid;
183 strcpy (the_passwd.pw_name, "unknown");
184 the_passwd.pw_uid = 123;
185 the_passwd.pw_gid = 123;
189 /* Ensure HOME and SHELL are defined. */
192 * With XEmacs, setting $HOME is deprecated.
193 * But with fpTeX, it is safer to assume $HOME defined.
196 char *home = get_home_directory();
198 putenv(concat("HOME=", home));
205 if (getenv ("SHELL") == NULL)
206 putenv ((GetVersion () & 0x80000000) ? "SHELL=command" : "SHELL=cmd");
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.
216 if ((p = getenv("TEXMFTEMP")) != NULL) {
217 putenv(concat("TEMP=", p));
218 putenv(concat("TMP=", p));
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"));