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 extern char *get_home_directory(void);
40 #define PASSWD_FIELD_SIZE 256
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];
48 struct passwd the_passwd =
63 return the_passwd.pw_uid;
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. */
78 return the_passwd.pw_gid;
90 if (uid == the_passwd.pw_uid)
96 getpwnam (const char *name)
100 pw = getpwuid (getuid ());
104 if (stricmp (name, pw->pw_name))
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.
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). */
120 char user_sid[256], name[256], domain[256];
121 DWORD length = sizeof (name), dlength = sizeof (domain), trash;
123 SID_NAME_USE user_type;
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))
131 strcpy (the_passwd.pw_name, name);
132 /* Determine a reasonable uid value. */
133 if (stricmp ("administrator", name) == 0)
135 the_passwd.pw_uid = 0;
136 the_passwd.pw_gid = 0;
140 SID_IDENTIFIER_AUTHORITY * pSIA;
142 pSIA = GetSidIdentifierAuthority (*((PSID *) user_sid));
143 /* I believe the relative portion is the last 4 bytes (of 6)
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;
153 if (GetTokenInformation (token, TokenPrimaryGroup,
154 (PVOID) user_sid, sizeof (user_sid), &trash))
156 SID_IDENTIFIER_AUTHORITY * pSIA;
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;
167 the_passwd.pw_gid = the_passwd.pw_uid;
170 /* If security calls are not supported (presumably because we
171 are running under Windows 95), fallback to this. */
173 if (GetUserName (name, &length))
175 strcpy (the_passwd.pw_name, name);
176 if (stricmp ("administrator", name) == 0)
177 the_passwd.pw_uid = 0;
179 the_passwd.pw_uid = 123;
180 the_passwd.pw_gid = the_passwd.pw_uid;
185 strcpy (the_passwd.pw_name, "unknown");
186 the_passwd.pw_uid = 123;
187 the_passwd.pw_gid = 123;
191 /* Ensure HOME and SHELL are defined. */
194 * With XEmacs, setting $HOME is deprecated.
195 * But with fpTeX, it is safer to assume $HOME defined.
198 char *home = get_home_directory();
200 putenv(concat("HOME=", home));
207 if (getenv ("SHELL") == NULL)
208 putenv ((GetVersion () & 0x80000000) ? "SHELL=command" : "SHELL=cmd");
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.
218 if ((p = getenv("TEXMFTEMP")) != NULL) {
219 putenv(concat("TEMP=", p));
220 putenv(concat("TMP=", p));
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"));