1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439
|
/*===========================================================================*
* *
* sfluid.c - User and group ID functions *
* *
* Copyright (c) 1991-2010 iMatix Corporation *
* *
* ------------------ GPL Licensed Source Code ------------------ *
* iMatix makes this software available under the GNU General *
* Public License (GPL) license for open source projects. For *
* details of the GPL license please see www.gnu.org or read the *
* file license.gpl provided in this package. *
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License as *
* published by the Free Software Foundation; either version 3 of *
* the License, or (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public *
* License along with this program in the file 'license.gpl'; if *
* not, see <http://www.gnu.org/licenses/>. *
* *
* You can also license this software under iMatix's General Terms *
* of Business (GTB) for commercial projects. If you have not *
* explicitly licensed this software under the iMatix GTB you may *
* only use it under the terms of the GNU General Public License. *
* *
* For more information, send an email to info@imatix.com. *
* -------------------------------------------------------------- *
*===========================================================================*/
#include "prelude.h" /* Universal header file */
#include "sflstr.h" /* String functions */
#include "sfluid.h" /* Prototypes for functions */
/* Local constants and function prototypes */
#define UID_CACHE_MAX 10 /* Max. different uid's we cache */
#define GID_CACHE_MAX 10 /* Max. different gid's we cache */
#define REAL_ID 0 /* Arguments for get_uid/get_gid */
#define EFFECTIVE_ID 1
#if (defined (DOES_UID)) /* Only if uid/gid implemented */
# if (!defined (__OS2__)) /* But not needed under OS/2 */
static uid_t get_uid (int type);
# endif
# if (!defined (__VMS__)) /* No gid under OpenVMS */
static gid_t get_gid (int type);
# endif
#endif
/* ---------------------------------------------------------------------[<]-
Function: get_uid_name
Synopsis:
Get user name from passwd file. We optimise by keeping a table of uids
and names in memory. Note that this will cause problems if the program
stays running when the passwd file has been changed. Returns a string
containing the translated user name, or "<none>" if the uid could not
be translated. Under MS-DOS the uid must be zero. The returned string
is in a static area that is _not_ overwritten with each call, but which
should be treated as read-only, and unstable: i.e. the value returned
by one call to get_uid_name may change as a result of a later call. If
you need persistent strings, use strdupl() after each call.
---------------------------------------------------------------------[>]-*/
char *
get_uid_name (uid_t uid)
{
# if (defined (DOES_UID))
static struct uids { /* Table of cached uids */
uid_t id;
char *name;
} cache [UID_CACHE_MAX];
static int
cache_size = 0, /* Number of uid's in cache */
cache_oldest = 0; /* Oldest entry in cache */
int
cache_scan; /* Scan through cache */
struct passwd
*passwd_entry;
/* First, look for uid in cache */
for (cache_scan = 0; cache_scan < cache_size; cache_scan++)
if (cache [cache_scan].id == uid)
return (cache [cache_scan].name);
/* Add new name to cache: if cache was full, kick-out oldest entry */
if (cache_size == UID_CACHE_MAX)
{
cache_scan = cache_oldest++;
cache_oldest %= UID_CACHE_MAX;
free (cache [cache_scan].name);
}
else
cache_scan = cache_size++;
cache [cache_scan].id = uid;
if ((passwd_entry = getpwuid (uid)) == NULL)
cache [cache_scan].name = "<none>";
else
cache [cache_scan].name = strdupl (passwd_entry-> pw_name);
return (cache [cache_scan].name);
# elif (defined (__MSDOS__))
return (uid == 0? "user": "<none>");
# endif
}
/* ---------------------------------------------------------------------[<]-
Function: get_gid_name
Synopsis:
Get group name from group file. We optimise by keeping a table of gids
and names in memory. Note that this will cause problems if the program
stays running when the group file has been changed. Returns a string
containing the translated user name, or "<none>" if the gid could not
be translated. Under MS-DOS the gid must be zero. The returned string
is in a static area that is _not_ overwritten with each call, but which
should be treated as read-only, and unstable: i.e. the value returned
by one call to get_gid_name may change as a result of a later call. If
you need persistent strings, use strdupl() after each call.
---------------------------------------------------------------------[>]-*/
char *
get_gid_name (gid_t gid)
{
# if (defined (DOES_UID))
static struct gids { /* Table of cache'd gids */
gid_t id;
char *name;
} cache [GID_CACHE_MAX];
static int
cache_size = 0, /* Number of gid's in cache */
cache_oldest = 0; /* Oldest entry in cache */
int
cache_scan; /* Scan through cache */
struct group
*group_entry;
/* First, look for gid in cache */
for (cache_scan = 0; cache_scan < cache_size; cache_scan++)
if (cache [cache_scan].id == gid)
return (cache [cache_scan].name);
/* Add new name to cache: if cache was full, kick-out oldest entry */
if (cache_size == GID_CACHE_MAX)
{
cache_scan = cache_oldest++;
cache_oldest %= GID_CACHE_MAX;
free (cache [cache_scan].name);
}
else
cache_scan = cache_size++;
cache [cache_scan].id = gid;
# if (defined (__VMS__))
cache [cache_scan].name = "<none>";
# else
if ((group_entry = getgrgid (gid)) == NULL)
cache [cache_scan].name = "<none>";
else
cache [cache_scan].name = strdupl (group_entry-> gr_name);
# endif
return (cache [cache_scan].name);
# elif (defined (__MSDOS__))
return (gid == 0? "group": "<none>");
# endif
}
/* ---------------------------------------------------------------------[<]-
Function: set_uid_user
Synopsis: This function can be used by 'setuid' programs; i.e. programs
that run under a fixed uid such as 'root'. Typically such programs need
to access root resources, but user data files. To do this they must
switch between the 'root' uid and the 'user' uid. This function switches
to the real user id. Use set_uid_root() to switch (back) to the 'root'
uid. See also: set_gid_user() and set_gid_root().
---------------------------------------------------------------------[>]-*/
int
set_uid_user (void)
{
#if (defined (DOES_UID))
# if (defined (__UTYPE_HPUX) || defined (__UTYPE_BEOS))
return (setuid (get_uid (REAL_ID)));
# elif (defined (__OS2__)) /* OS/2 only supports one UID */
return (0);
# elif (defined (__VMS__)) /* No setuid under OpenVMS */
return (0);
# else
return (seteuid (get_uid (REAL_ID)));
# endif
#else
return (0);
#endif
}
/* ---------------------------------------------------------------------[<]-
Function: set_uid_root
Synopsis: This function can be used by 'setuid' programs; i.e. programs
that run under a fixed uid such as 'root'. Typically such programs need
to access root resources, but user data files. To do this they must
switch between the 'root' uid and the 'user' uid. This function switches
back to the root user id. Use set_uid_user() to switch to the 'user'
uid. See also: set_gid_user() and set_gid_root().
---------------------------------------------------------------------[>]-*/
int
set_uid_root (void)
{
#if (defined (DOES_UID))
# if (defined (__UTYPE_HPUX) || defined (__UTYPE_BEOS))
return (setuid (get_uid (EFFECTIVE_ID)));
# elif (defined (__OS2__)) /* OS/2 only supports one UID */
return (0);
# elif (defined (__VMS__)) /* No setuid under OpenVMS */
return (0);
# else
return (seteuid (get_uid (EFFECTIVE_ID)));
# endif
#else
return (0);
#endif
}
#if (defined (DOES_UID) && !defined (__OS2__))
/* -------------------------------------------------------------------------
Function: get_uid_id -- internal
Synopsis: Returns the real (REAL_ID) or effective (EFFECTIVE_ID) uid.
These values are loaded the first time that the function is called: you
should not rely on the effective uid after changing the uid.
-------------------------------------------------------------------------*/
static uid_t
get_uid (int type)
{
static int
ruid = -1,
euid = -1;
if (ruid == -1)
ruid = getuid ();
if (euid == -1)
# if (defined (__UTYPE_HPUX) || defined (__UTYPE_BEOS))
euid = getuid ();
# else
euid = geteuid ();
# endif
if (type == REAL_ID)
return (ruid);
else
if (type == EFFECTIVE_ID)
return (euid);
else
return (-1);
}
#endif
/* ---------------------------------------------------------------------[<]-
Function: set_gid_user
Synopsis: This function can be used by 'setgid' programs; i.e. programs
that run under a fixed gid such as 'root'. Typically such programs need
to access root resources, but user data files. To do this they must
switch between the 'root' gid and the 'user' gid. This function switches
to the real user id. Use set_gid_root() to switch (back) to the 'root'
gid. See also: set_uid_user() and set_uid_root().
---------------------------------------------------------------------[>]-*/
int
set_gid_user (void)
{
#if (defined (DOES_UID))
# if (defined (__UTYPE_HPUX) || defined (__UTYPE_BEOS))
return (setgid (get_gid (REAL_ID)));
# elif (defined (__OS2__)) /* OS/2 only supports one UID */
return (0);
# elif (defined (__VMS__)) /* No setgid under OpenVMS */
return (0);
# else
return (setegid (get_gid (REAL_ID)));
# endif
#else
return (0);
#endif
}
/* ---------------------------------------------------------------------[<]-
Function: set_gid_root
Synopsis: This function can be used by 'setgid' programs; i.e. programs
that run under a fixed gid such as 'root'. Typically such programs need
to access root resources, but user data files. To do this they must
switch between the 'root' gid and the 'user' gid. This function switches
back to the root user id. Use set_gid_user() to switch to the 'user'
gid. See also: set_gid_user() and set_gid_root().
---------------------------------------------------------------------[>]-*/
int
set_gid_root (void)
{
#if (defined (DOES_UID))
# if (defined (__UTYPE_HPUX) || defined (__UTYPE_BEOS))
return (setgid (get_gid (EFFECTIVE_ID)));
# elif (defined (__OS2__)) /* OS/2 only supports one UID */
return (0);
# elif (defined (__VMS__)) /* No setgid under OpenVMS */
return (0);
# else
return (setegid (get_gid (EFFECTIVE_ID)));
# endif
#else
return (0);
#endif
}
#if (defined (DOES_UID) && !defined (__OS2__) && !defined (__VMS__))
/* -------------------------------------------------------------------------
Function: get_gid -- internal
Synopsis: Returns the real (REAL_ID) or effective (EFFECTIVE_ID) gid.
These values are loaded the first time that the function is called: you
should not rely on the effective gid after changing the gid.
-------------------------------------------------------------------------*/
static gid_t
get_gid (int type)
{
static int
rgid = -1,
egid = -1;
if (rgid == -1)
rgid = getgid ();
if (egid == -1)
# if (defined (__UTYPE_HPUX) || defined (__UTYPE_BEOS))
egid = getgid ();
# else
egid = getegid ();
# endif
if (type == REAL_ID)
return (rgid);
else
if (type == EFFECTIVE_ID)
return (egid);
else
return (0);
}
#endif
/* ---------------------------------------------------------------------[<]-
Function: set_uid_gid
Synopsis: Sets the program's uid and gid to new values as specified
(as names). The program must be currently running as 'root'. Returns
0 if the new names could be correctly used. Returns -1 if the specified
user id or group id was not valid, or -2 if the process was unable to
change to the new uid/gid as specified. The gid may be null or empty.
---------------------------------------------------------------------[>]-*/
int
set_uid_gid (char *new_uid, char *new_gid)
{
#if (defined (DOES_UID))
struct passwd
*pwdbuf;
struct group
*grpbuf;
# if (defined (__VMS__))
return (0);
# else
if (new_gid && *new_gid)
{
if ((grpbuf = getgrnam (new_gid)) == NULL)
return (-1);
else
if (setgid (grpbuf-> gr_gid) == -1)
return (-2);
}
if ((pwdbuf = getpwnam (new_uid)) == NULL)
return (-1);
else
if (setuid (pwdbuf-> pw_uid) == -1)
return (-2);
# endif
#endif
return (0);
}
/* ---------------------------------------------------------------------[<]-
Function: get_login
Synopsis: Returns the identity of the currently-logged user. The
returned string is in a static buffer. Returns NULL if no user is
currently logged-in. Fixed for compilers with broken uuid.lib functions.
---------------------------------------------------------------------[>]-*/
char *
get_login (void)
{
#if (defined (WIN32))
ULONG
user_name_max = 255;
static char
user_name [256];
if (!GetUserNameA (user_name, &user_name_max))
strncpy (user_name, "unknown", sizeof (user_name));
return (user_name);
#elif (defined (__UNIX__) || defined (__OS2__) || defined (__VMS__))
return (getlogin ());
#endif
}
|