Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 1 | /* Copyright (C) 2003 Manuel Novoa III |
| 2 | * |
Rob Landley | 25413bf | 2005-10-08 02:23:22 +0000 | [diff] [blame] | 3 | * Licensed under GPL v2, or later. See file LICENSE in this tarball. |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | /* Nov 6, 2003 Initial version. |
| 7 | * |
| 8 | * NOTE: This implementation is quite strict about requiring all |
| 9 | * field seperators. It also does not allow leading whitespace |
| 10 | * except when processing the numeric fields. glibc is more |
| 11 | * lenient. See the various glibc difference comments below. |
| 12 | * |
| 13 | * TODO: |
| 14 | * Move to dynamic allocation of (currently staticly allocated) |
| 15 | * buffers; especially for the group-related functions since |
| 16 | * large group member lists will cause error returns. |
| 17 | * |
| 18 | */ |
| 19 | |
| 20 | #include <features.h> |
| 21 | #include <stdio.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <stdint.h> |
| 24 | #include <string.h> |
| 25 | #include <stddef.h> |
| 26 | #include <errno.h> |
| 27 | #include <assert.h> |
| 28 | #include <ctype.h> |
| 29 | #include "busybox.h" |
| 30 | #include "pwd_.h" |
| 31 | #include "grp_.h" |
| 32 | #include "shadow_.h" |
| 33 | |
| 34 | #ifndef _PATH_SHADOW |
| 35 | #define _PATH_SHADOW "/etc/shadow" |
| 36 | #endif |
| 37 | #ifndef _PATH_PASSWD |
| 38 | #define _PATH_PASSWD "/etc/passwd" |
| 39 | #endif |
| 40 | #ifndef _PATH_GROUP |
| 41 | #define _PATH_GROUP "/etc/group" |
| 42 | #endif |
| 43 | |
| 44 | /**********************************************************************/ |
| 45 | /* Sizes for staticly allocated buffers. */ |
| 46 | |
| 47 | /* If you change these values, also change _SC_GETPW_R_SIZE_MAX and |
| 48 | * _SC_GETGR_R_SIZE_MAX in libc/unistd/sysconf.c to match */ |
| 49 | #define PWD_BUFFER_SIZE 256 |
| 50 | #define GRP_BUFFER_SIZE 256 |
| 51 | |
| 52 | /**********************************************************************/ |
| 53 | /* Prototypes for internal functions. */ |
| 54 | |
| 55 | extern int __parsepwent(void *pw, char *line); |
| 56 | extern int __parsegrent(void *gr, char *line); |
| 57 | extern int __parsespent(void *sp, char *line); |
| 58 | |
| 59 | extern int __pgsreader(int (*__parserfunc)(void *d, char *line), void *data, |
| 60 | char *__restrict line_buff, size_t buflen, FILE *f); |
| 61 | |
| 62 | /**********************************************************************/ |
| 63 | /* For the various fget??ent_r funcs, return |
| 64 | * |
| 65 | * 0: success |
| 66 | * ENOENT: end-of-file encountered |
| 67 | * ERANGE: buflen too small |
| 68 | * other error values possible. See __pgsreader. |
| 69 | * |
| 70 | * Also, *result == resultbuf on success and NULL on failure. |
| 71 | * |
| 72 | * NOTE: glibc difference - For the ENOENT case, glibc also sets errno. |
| 73 | * We do not, as it really isn't an error if we reach the end-of-file. |
| 74 | * Doing so is analogous to having fgetc() set errno on EOF. |
| 75 | */ |
| 76 | /**********************************************************************/ |
| 77 | #ifdef L_fgetpwent_r |
| 78 | |
| 79 | int fgetpwent_r(FILE *__restrict stream, struct passwd *__restrict resultbuf, |
| 80 | char *__restrict buffer, size_t buflen, |
| 81 | struct passwd **__restrict result) |
| 82 | { |
| 83 | int rv; |
| 84 | |
| 85 | *result = NULL; |
| 86 | |
| 87 | if (!(rv = __pgsreader(__parsepwent, resultbuf, buffer, buflen, stream))) { |
| 88 | *result = resultbuf; |
| 89 | } |
| 90 | |
| 91 | return rv; |
| 92 | } |
| 93 | |
| 94 | #endif |
| 95 | /**********************************************************************/ |
| 96 | #ifdef L_fgetgrent_r |
| 97 | |
| 98 | int fgetgrent_r(FILE *__restrict stream, struct group *__restrict resultbuf, |
| 99 | char *__restrict buffer, size_t buflen, |
| 100 | struct group **__restrict result) |
| 101 | { |
| 102 | int rv; |
| 103 | |
| 104 | *result = NULL; |
| 105 | |
| 106 | if (!(rv = __pgsreader(__parsegrent, resultbuf, buffer, buflen, stream))) { |
| 107 | *result = resultbuf; |
| 108 | } |
| 109 | |
| 110 | return rv; |
| 111 | } |
| 112 | |
| 113 | #endif |
| 114 | /**********************************************************************/ |
| 115 | #ifdef L_fgetspent_r |
| 116 | |
| 117 | int fgetspent_r(FILE *__restrict stream, struct spwd *__restrict resultbuf, |
| 118 | char *__restrict buffer, size_t buflen, |
| 119 | struct spwd **__restrict result) |
| 120 | { |
| 121 | int rv; |
| 122 | |
| 123 | *result = NULL; |
| 124 | |
| 125 | if (!(rv = __pgsreader(__parsespent, resultbuf, buffer, buflen, stream))) { |
| 126 | *result = resultbuf; |
| 127 | } |
| 128 | |
| 129 | return rv; |
| 130 | } |
| 131 | |
| 132 | #endif |
| 133 | /**********************************************************************/ |
| 134 | /* For the various fget??ent funcs, return NULL on failure and a |
| 135 | * pointer to the appropriate struct (staticly allocated) on success. |
| 136 | */ |
| 137 | /**********************************************************************/ |
| 138 | #ifdef L_fgetpwent |
| 139 | |
| 140 | struct passwd *fgetpwent(FILE *stream) |
| 141 | { |
| 142 | static char buffer[PWD_BUFFER_SIZE]; |
| 143 | static struct passwd resultbuf; |
| 144 | struct passwd *result; |
| 145 | |
| 146 | fgetpwent_r(stream, &resultbuf, buffer, sizeof(buffer), &result); |
| 147 | return result; |
| 148 | } |
| 149 | |
| 150 | #endif |
| 151 | /**********************************************************************/ |
| 152 | #ifdef L_fgetgrent |
| 153 | |
| 154 | struct group *fgetgrent(FILE *stream) |
| 155 | { |
| 156 | static char buffer[GRP_BUFFER_SIZE]; |
| 157 | static struct group resultbuf; |
| 158 | struct group *result; |
| 159 | |
| 160 | fgetgrent_r(stream, &resultbuf, buffer, sizeof(buffer), &result); |
| 161 | return result; |
| 162 | } |
| 163 | |
| 164 | #endif |
| 165 | /**********************************************************************/ |
| 166 | #ifdef L_fgetspent |
| 167 | |
| 168 | extern int fgetspent_r(FILE *__restrict stream, struct spwd *__restrict resultbuf, |
| 169 | char *__restrict buffer, size_t buflen, |
| 170 | struct spwd **__restrict result); |
| 171 | struct spwd *fgetspent(FILE *stream) |
| 172 | { |
| 173 | static char buffer[PWD_BUFFER_SIZE]; |
| 174 | static struct spwd resultbuf; |
| 175 | struct spwd *result; |
| 176 | |
| 177 | fgetspent_r(stream, &resultbuf, buffer, sizeof(buffer), &result); |
| 178 | return result; |
| 179 | } |
| 180 | |
| 181 | #endif |
| 182 | /**********************************************************************/ |
| 183 | #ifdef L_sgetspent_r |
| 184 | |
| 185 | int sgetspent_r(const char *string, struct spwd *result_buf, |
| 186 | char *buffer, size_t buflen, struct spwd **result) |
| 187 | { |
| 188 | int rv = ERANGE; |
| 189 | |
| 190 | *result = NULL; |
| 191 | |
| 192 | if (buflen < PWD_BUFFER_SIZE) { |
| 193 | DO_ERANGE: |
| 194 | errno=rv; |
| 195 | goto DONE; |
| 196 | } |
| 197 | |
| 198 | if (string != buffer) { |
| 199 | if (strlen(string) >= buflen) { |
| 200 | goto DO_ERANGE; |
| 201 | } |
| 202 | strcpy(buffer, string); |
| 203 | } |
| 204 | |
| 205 | if (!(rv = __parsespent(result_buf, buffer))) { |
| 206 | *result = result_buf; |
| 207 | } |
| 208 | |
| 209 | DONE: |
| 210 | return rv; |
| 211 | } |
| 212 | |
| 213 | #endif |
| 214 | /**********************************************************************/ |
| 215 | |
| 216 | #ifdef GETXXKEY_R_FUNC |
| 217 | #error GETXXKEY_R_FUNC is already defined! |
| 218 | #endif |
| 219 | |
| 220 | #ifdef L_getpwnam_r |
| 221 | #define GETXXKEY_R_FUNC getpwnam_r |
| 222 | #define GETXXKEY_R_PARSER __parsepwent |
| 223 | #define GETXXKEY_R_ENTTYPE struct passwd |
| 224 | #define GETXXKEY_R_TEST(ENT) (!strcmp((ENT)->pw_name, key)) |
| 225 | #define DO_GETXXKEY_R_KEYTYPE const char *__restrict |
| 226 | #define DO_GETXXKEY_R_PATHNAME _PATH_PASSWD |
| 227 | #endif |
| 228 | |
| 229 | #ifdef L_getgrnam_r |
| 230 | #define GETXXKEY_R_FUNC getgrnam_r |
| 231 | #define GETXXKEY_R_PARSER __parsegrent |
| 232 | #define GETXXKEY_R_ENTTYPE struct group |
| 233 | #define GETXXKEY_R_TEST(ENT) (!strcmp((ENT)->gr_name, key)) |
| 234 | #define DO_GETXXKEY_R_KEYTYPE const char *__restrict |
| 235 | #define DO_GETXXKEY_R_PATHNAME _PATH_GROUP |
| 236 | #endif |
| 237 | |
| 238 | #ifdef L_getspnam_r |
| 239 | #define GETXXKEY_R_FUNC getspnam_r |
| 240 | #define GETXXKEY_R_PARSER __parsespent |
| 241 | #define GETXXKEY_R_ENTTYPE struct spwd |
| 242 | #define GETXXKEY_R_TEST(ENT) (!strcmp((ENT)->sp_namp, key)) |
| 243 | #define DO_GETXXKEY_R_KEYTYPE const char *__restrict |
| 244 | #define DO_GETXXKEY_R_PATHNAME _PATH_SHADOW |
| 245 | #endif |
| 246 | |
| 247 | #ifdef L_getpwuid_r |
| 248 | #define GETXXKEY_R_FUNC getpwuid_r |
| 249 | #define GETXXKEY_R_PARSER __parsepwent |
| 250 | #define GETXXKEY_R_ENTTYPE struct passwd |
| 251 | #define GETXXKEY_R_TEST(ENT) ((ENT)->pw_uid == key) |
| 252 | #define DO_GETXXKEY_R_KEYTYPE uid_t |
| 253 | #define DO_GETXXKEY_R_PATHNAME _PATH_PASSWD |
| 254 | #endif |
| 255 | |
| 256 | #ifdef L_getgrgid_r |
| 257 | #define GETXXKEY_R_FUNC getgrgid_r |
| 258 | #define GETXXKEY_R_PARSER __parsegrent |
| 259 | #define GETXXKEY_R_ENTTYPE struct group |
| 260 | #define GETXXKEY_R_TEST(ENT) ((ENT)->gr_gid == key) |
| 261 | #define DO_GETXXKEY_R_KEYTYPE gid_t |
| 262 | #define DO_GETXXKEY_R_PATHNAME _PATH_GROUP |
| 263 | #endif |
| 264 | |
| 265 | /**********************************************************************/ |
| 266 | #ifdef GETXXKEY_R_FUNC |
| 267 | |
| 268 | int GETXXKEY_R_FUNC(DO_GETXXKEY_R_KEYTYPE key, |
| 269 | GETXXKEY_R_ENTTYPE *__restrict resultbuf, |
| 270 | char *__restrict buffer, size_t buflen, |
| 271 | GETXXKEY_R_ENTTYPE **__restrict result) |
| 272 | { |
| 273 | FILE *stream; |
| 274 | int rv; |
| 275 | |
| 276 | *result = NULL; |
| 277 | |
| 278 | if (!(stream = fopen(DO_GETXXKEY_R_PATHNAME, "r"))) { |
| 279 | rv = errno; |
| 280 | } else { |
| 281 | do { |
| 282 | if (!(rv = __pgsreader(GETXXKEY_R_PARSER, resultbuf, |
| 283 | buffer, buflen, stream)) |
| 284 | ) { |
| 285 | if (GETXXKEY_R_TEST(resultbuf)) { /* Found key? */ |
| 286 | *result = resultbuf; |
| 287 | break; |
| 288 | } |
| 289 | } else { |
| 290 | if (rv == ENOENT) { /* end-of-file encountered. */ |
| 291 | rv = 0; |
| 292 | } |
| 293 | break; |
| 294 | } |
| 295 | } while (1); |
| 296 | fclose(stream); |
| 297 | } |
| 298 | |
| 299 | return rv; |
| 300 | } |
| 301 | |
| 302 | #endif |
| 303 | /**********************************************************************/ |
| 304 | #ifdef L_getpwuid |
| 305 | |
| 306 | struct passwd *getpwuid(uid_t uid) |
| 307 | { |
| 308 | static char buffer[PWD_BUFFER_SIZE]; |
| 309 | static struct passwd resultbuf; |
| 310 | struct passwd *result; |
| 311 | |
| 312 | getpwuid_r(uid, &resultbuf, buffer, sizeof(buffer), &result); |
| 313 | return result; |
| 314 | } |
| 315 | |
| 316 | #endif |
| 317 | /**********************************************************************/ |
| 318 | #ifdef L_getgrgid |
| 319 | |
| 320 | struct group *getgrgid(gid_t gid) |
| 321 | { |
| 322 | static char buffer[GRP_BUFFER_SIZE]; |
| 323 | static struct group resultbuf; |
| 324 | struct group *result; |
| 325 | |
| 326 | getgrgid_r(gid, &resultbuf, buffer, sizeof(buffer), &result); |
| 327 | return result; |
| 328 | } |
| 329 | |
| 330 | #endif |
| 331 | /**********************************************************************/ |
| 332 | #ifdef L_getspuid_r |
| 333 | |
| 334 | /* This function is non-standard and is currently not built. It seems |
| 335 | * to have been created as a reentrant version of the non-standard |
| 336 | * functions getspuid. Why getspuid was added, I do not know. */ |
| 337 | |
| 338 | int getspuid_r(uid_t uid, struct spwd *__restrict resultbuf, |
| 339 | char *__restrict buffer, size_t buflen, |
| 340 | struct spwd **__restrict result) |
| 341 | { |
| 342 | int rv; |
| 343 | struct passwd *pp; |
| 344 | struct passwd password; |
| 345 | char pwd_buff[PWD_BUFFER_SIZE]; |
| 346 | |
| 347 | *result = NULL; |
| 348 | if (!(rv = getpwuid_r(uid, &password, pwd_buff, sizeof(pwd_buff), &pp))) { |
| 349 | rv = getspnam_r(password.pw_name, resultbuf, buffer, buflen, result); |
| 350 | } |
| 351 | |
| 352 | return rv; |
| 353 | } |
| 354 | |
| 355 | #endif |
| 356 | /**********************************************************************/ |
| 357 | #ifdef L_getspuid |
| 358 | |
| 359 | /* This function is non-standard and is currently not built. |
| 360 | * Why it was added, I do not know. */ |
| 361 | |
| 362 | struct spwd *getspuid(uid_t uid) |
| 363 | { |
| 364 | static char buffer[PWD_BUFFER_SIZE]; |
| 365 | static struct spwd resultbuf; |
| 366 | struct spwd *result; |
| 367 | |
| 368 | getspuid_r(uid, &resultbuf, buffer, sizeof(buffer), &result); |
| 369 | return result; |
| 370 | } |
| 371 | |
| 372 | #endif |
| 373 | /**********************************************************************/ |
| 374 | #ifdef L_getpwnam |
| 375 | |
| 376 | struct passwd *getpwnam(const char *name) |
| 377 | { |
| 378 | static char buffer[PWD_BUFFER_SIZE]; |
| 379 | static struct passwd resultbuf; |
| 380 | struct passwd *result; |
| 381 | |
| 382 | getpwnam_r(name, &resultbuf, buffer, sizeof(buffer), &result); |
| 383 | return result; |
| 384 | } |
| 385 | |
| 386 | #endif |
| 387 | /**********************************************************************/ |
| 388 | #ifdef L_getgrnam |
| 389 | |
| 390 | struct group *getgrnam(const char *name) |
| 391 | { |
| 392 | static char buffer[GRP_BUFFER_SIZE]; |
| 393 | static struct group resultbuf; |
| 394 | struct group *result; |
| 395 | |
| 396 | getgrnam_r(name, &resultbuf, buffer, sizeof(buffer), &result); |
| 397 | return result; |
| 398 | } |
| 399 | |
| 400 | #endif |
| 401 | /**********************************************************************/ |
| 402 | #ifdef L_getspnam |
| 403 | |
| 404 | struct spwd *getspnam(const char *name) |
| 405 | { |
| 406 | static char buffer[PWD_BUFFER_SIZE]; |
| 407 | static struct spwd resultbuf; |
| 408 | struct spwd *result; |
| 409 | |
| 410 | getspnam_r(name, &resultbuf, buffer, sizeof(buffer), &result); |
| 411 | return result; |
| 412 | } |
| 413 | |
| 414 | #endif |
| 415 | /**********************************************************************/ |
| 416 | #ifdef L_getpw |
| 417 | |
| 418 | int getpw(uid_t uid, char *buf) |
| 419 | { |
| 420 | struct passwd resultbuf; |
| 421 | struct passwd *result; |
| 422 | char buffer[PWD_BUFFER_SIZE]; |
| 423 | |
| 424 | if (!buf) { |
| 425 | errno=EINVAL; |
| 426 | } else if (!getpwuid_r(uid, &resultbuf, buffer, sizeof(buffer), &result)) { |
| 427 | if (sprintf(buf, "%s:%s:%lu:%lu:%s:%s:%s\n", |
| 428 | resultbuf.pw_name, resultbuf.pw_passwd, |
| 429 | (unsigned long)(resultbuf.pw_uid), |
| 430 | (unsigned long)(resultbuf.pw_gid), |
| 431 | resultbuf.pw_gecos, resultbuf.pw_dir, |
| 432 | resultbuf.pw_shell) >= 0 |
| 433 | ) { |
| 434 | return 0; |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | return -1; |
| 439 | } |
| 440 | |
| 441 | #endif |
| 442 | /**********************************************************************/ |
| 443 | #ifdef L_getpwent_r |
| 444 | |
| 445 | static FILE *pwf /*= NULL*/; |
| 446 | void setpwent(void) |
| 447 | { |
| 448 | if (pwf) { |
| 449 | rewind(pwf); |
| 450 | } |
| 451 | } |
| 452 | |
| 453 | void endpwent(void) |
| 454 | { |
| 455 | if (pwf) { |
| 456 | fclose(pwf); |
| 457 | pwf = NULL; |
| 458 | } |
| 459 | } |
| 460 | |
| 461 | |
| 462 | int getpwent_r(struct passwd *__restrict resultbuf, |
| 463 | char *__restrict buffer, size_t buflen, |
| 464 | struct passwd **__restrict result) |
| 465 | { |
| 466 | int rv; |
| 467 | |
| 468 | *result = NULL; /* In case of error... */ |
| 469 | |
| 470 | if (!pwf) { |
| 471 | if (!(pwf = fopen(_PATH_PASSWD, "r"))) { |
| 472 | rv = errno; |
| 473 | goto ERR; |
| 474 | } |
| 475 | } |
| 476 | |
| 477 | if (!(rv = __pgsreader(__parsepwent, resultbuf, |
| 478 | buffer, buflen, pwf))) { |
| 479 | *result = resultbuf; |
| 480 | } |
| 481 | |
| 482 | ERR: |
| 483 | return rv; |
| 484 | } |
| 485 | |
| 486 | #endif |
| 487 | /**********************************************************************/ |
| 488 | #ifdef L_getgrent_r |
| 489 | |
| 490 | static FILE *grf /*= NULL*/; |
| 491 | void setgrent(void) |
| 492 | { |
| 493 | if (grf) { |
| 494 | rewind(grf); |
| 495 | } |
| 496 | } |
| 497 | |
| 498 | void endgrent(void) |
| 499 | { |
| 500 | if (grf) { |
| 501 | fclose(grf); |
| 502 | grf = NULL; |
| 503 | } |
| 504 | } |
| 505 | |
| 506 | int getgrent_r(struct group *__restrict resultbuf, |
| 507 | char *__restrict buffer, size_t buflen, |
| 508 | struct group **__restrict result) |
| 509 | { |
| 510 | int rv; |
| 511 | |
| 512 | *result = NULL; /* In case of error... */ |
| 513 | |
| 514 | if (!grf) { |
| 515 | if (!(grf = fopen(_PATH_GROUP, "r"))) { |
| 516 | rv = errno; |
| 517 | goto ERR; |
| 518 | } |
| 519 | } |
| 520 | |
| 521 | if (!(rv = __pgsreader(__parsegrent, resultbuf, |
| 522 | buffer, buflen, grf))) { |
| 523 | *result = resultbuf; |
| 524 | } |
| 525 | |
| 526 | ERR: |
| 527 | return rv; |
| 528 | } |
| 529 | |
| 530 | #endif |
| 531 | /**********************************************************************/ |
| 532 | #ifdef L_getspent_r |
| 533 | |
| 534 | static FILE *spf /*= NULL*/; |
| 535 | void setspent(void) |
| 536 | { |
| 537 | if (spf) { |
| 538 | rewind(spf); |
| 539 | } |
| 540 | } |
| 541 | |
| 542 | void endspent(void) |
| 543 | { |
| 544 | if (spf) { |
| 545 | fclose(spf); |
| 546 | spf = NULL; |
| 547 | } |
| 548 | } |
| 549 | |
| 550 | int getspent_r(struct spwd *resultbuf, char *buffer, |
| 551 | size_t buflen, struct spwd **result) |
| 552 | { |
| 553 | int rv; |
| 554 | |
| 555 | *result = NULL; /* In case of error... */ |
| 556 | |
| 557 | if (!spf) { |
| 558 | if (!(spf = fopen(_PATH_SHADOW, "r"))) { |
| 559 | rv = errno; |
| 560 | goto ERR; |
| 561 | } |
| 562 | } |
| 563 | |
| 564 | if (!(rv = __pgsreader(__parsespent, resultbuf, |
| 565 | buffer, buflen, spf))) { |
| 566 | *result = resultbuf; |
| 567 | } |
| 568 | |
| 569 | ERR: |
| 570 | return rv; |
| 571 | } |
| 572 | |
| 573 | #endif |
| 574 | /**********************************************************************/ |
| 575 | #ifdef L_getpwent |
| 576 | |
| 577 | struct passwd *getpwent(void) |
| 578 | { |
| 579 | static char line_buff[PWD_BUFFER_SIZE]; |
| 580 | static struct passwd pwd; |
| 581 | struct passwd *result; |
| 582 | |
| 583 | getpwent_r(&pwd, line_buff, sizeof(line_buff), &result); |
| 584 | return result; |
| 585 | } |
| 586 | |
| 587 | #endif |
| 588 | /**********************************************************************/ |
| 589 | #ifdef L_getgrent |
| 590 | |
| 591 | struct group *getgrent(void) |
| 592 | { |
| 593 | static char line_buff[GRP_BUFFER_SIZE]; |
| 594 | static struct group gr; |
| 595 | struct group *result; |
| 596 | |
| 597 | getgrent_r(&gr, line_buff, sizeof(line_buff), &result); |
| 598 | return result; |
| 599 | } |
| 600 | |
| 601 | #endif |
| 602 | /**********************************************************************/ |
| 603 | #ifdef L_getspent |
| 604 | |
| 605 | struct spwd *getspent(void) |
| 606 | { |
| 607 | static char line_buff[PWD_BUFFER_SIZE]; |
| 608 | static struct spwd spwd; |
| 609 | struct spwd *result; |
| 610 | |
| 611 | getspent_r(&spwd, line_buff, sizeof(line_buff), &result); |
| 612 | return result; |
| 613 | } |
| 614 | |
| 615 | #endif |
| 616 | /**********************************************************************/ |
| 617 | #ifdef L_sgetspent |
| 618 | |
| 619 | struct spwd *sgetspent(const char *string) |
| 620 | { |
| 621 | static char line_buff[PWD_BUFFER_SIZE]; |
| 622 | static struct spwd spwd; |
| 623 | struct spwd *result; |
| 624 | |
| 625 | sgetspent_r(string, &spwd, line_buff, sizeof(line_buff), &result); |
| 626 | return result; |
| 627 | } |
| 628 | |
| 629 | #endif |
| 630 | /**********************************************************************/ |
| 631 | #ifdef L_initgroups |
| 632 | |
| 633 | int initgroups(const char *user, gid_t gid) |
| 634 | { |
| 635 | FILE *grf; |
| 636 | gid_t *group_list; |
| 637 | int num_groups, rv; |
| 638 | char **m; |
| 639 | struct group group; |
| 640 | char buff[PWD_BUFFER_SIZE]; |
| 641 | |
| 642 | rv = -1; |
| 643 | |
| 644 | /* We alloc space for 8 gids at a time. */ |
| 645 | if (((group_list = (gid_t *) malloc(8*sizeof(gid_t *))) != NULL) |
| 646 | && ((grf = fopen(_PATH_GROUP, "r")) != NULL) |
| 647 | ) { |
| 648 | |
| 649 | *group_list = gid; |
| 650 | num_groups = 1; |
| 651 | |
| 652 | while (!__pgsreader(__parsegrent, &group, buff, sizeof(buff), grf)) { |
| 653 | assert(group.gr_mem); /* Must have at least a NULL terminator. */ |
| 654 | if (group.gr_gid != gid) { |
| 655 | for (m=group.gr_mem ; *m ; m++) { |
| 656 | if (!strcmp(*m, user)) { |
| 657 | if (!(num_groups & 7)) { |
| 658 | gid_t *tmp = (gid_t *) |
| 659 | realloc(group_list, |
| 660 | (num_groups+8) * sizeof(gid_t *)); |
| 661 | if (!tmp) { |
| 662 | rv = -1; |
| 663 | goto DO_CLOSE; |
| 664 | } |
| 665 | group_list = tmp; |
| 666 | } |
| 667 | group_list[num_groups++] = group.gr_gid; |
| 668 | break; |
| 669 | } |
| 670 | } |
| 671 | } |
| 672 | } |
| 673 | |
| 674 | rv = setgroups(num_groups, group_list); |
| 675 | DO_CLOSE: |
| 676 | fclose(grf); |
| 677 | } |
| 678 | |
| 679 | /* group_list will be NULL if initial malloc failed, which may trigger |
| 680 | * warnings from various malloc debuggers. */ |
| 681 | free(group_list); |
| 682 | return rv; |
| 683 | } |
| 684 | |
| 685 | #endif |
| 686 | /**********************************************************************/ |
| 687 | #ifdef L_putpwent |
| 688 | |
| 689 | int putpwent(const struct passwd *__restrict p, FILE *__restrict f) |
| 690 | { |
| 691 | int rv = -1; |
| 692 | |
| 693 | if (!p || !f) { |
| 694 | errno=EINVAL; |
| 695 | } else { |
| 696 | /* No extra thread locking is needed above what fprintf does. */ |
| 697 | if (fprintf(f, "%s:%s:%lu:%lu:%s:%s:%s\n", |
| 698 | p->pw_name, p->pw_passwd, |
| 699 | (unsigned long)(p->pw_uid), |
| 700 | (unsigned long)(p->pw_gid), |
| 701 | p->pw_gecos, p->pw_dir, p->pw_shell) >= 0 |
| 702 | ) { |
| 703 | rv = 0; |
| 704 | } |
| 705 | } |
| 706 | |
| 707 | return rv; |
| 708 | } |
| 709 | |
| 710 | #endif |
| 711 | /**********************************************************************/ |
| 712 | #ifdef L_putgrent |
| 713 | |
| 714 | int putgrent(const struct group *__restrict p, FILE *__restrict f) |
| 715 | { |
| 716 | static const char format[] = ",%s"; |
| 717 | char **m; |
| 718 | const char *fmt; |
| 719 | int rv = -1; |
| 720 | |
| 721 | if (!p || !f) { /* Sigh... glibc checks. */ |
| 722 | errno=EINVAL; |
| 723 | } else { |
| 724 | if (fprintf(f, "%s:%s:%lu:", |
| 725 | p->gr_name, p->gr_passwd, |
| 726 | (unsigned long)(p->gr_gid)) >= 0 |
| 727 | ) { |
| 728 | |
| 729 | fmt = format + 1; |
| 730 | |
| 731 | assert(p->gr_mem); |
| 732 | m = p->gr_mem; |
| 733 | |
| 734 | do { |
| 735 | if (!*m) { |
Rob Landley | 25413bf | 2005-10-08 02:23:22 +0000 | [diff] [blame] | 736 | if (fputc('\n', f) >= 0) { |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 737 | rv = 0; |
| 738 | } |
| 739 | break; |
| 740 | } |
| 741 | if (fprintf(f, fmt, *m) < 0) { |
| 742 | break; |
| 743 | } |
| 744 | ++m; |
| 745 | fmt = format; |
| 746 | } while (1); |
| 747 | |
| 748 | } |
| 749 | |
| 750 | } |
| 751 | |
| 752 | return rv; |
| 753 | } |
| 754 | |
| 755 | #endif |
| 756 | /**********************************************************************/ |
| 757 | #ifdef L_putspent |
| 758 | |
| 759 | static const unsigned char sp_off[] = { |
| 760 | offsetof(struct spwd, sp_lstchg), /* 2 - not a char ptr */ |
| 761 | offsetof(struct spwd, sp_min), /* 3 - not a char ptr */ |
| 762 | offsetof(struct spwd, sp_max), /* 4 - not a char ptr */ |
| 763 | offsetof(struct spwd, sp_warn), /* 5 - not a char ptr */ |
| 764 | offsetof(struct spwd, sp_inact), /* 6 - not a char ptr */ |
| 765 | offsetof(struct spwd, sp_expire), /* 7 - not a char ptr */ |
| 766 | }; |
| 767 | |
| 768 | int putspent(const struct spwd *p, FILE *stream) |
| 769 | { |
| 770 | static const char ld_format[] = "%ld:"; |
| 771 | const char *f; |
| 772 | long int x; |
| 773 | int i; |
| 774 | int rv = -1; |
| 775 | |
| 776 | /* Unlike putpwent and putgrent, glibc does not check the args. */ |
| 777 | if (fprintf(stream, "%s:%s:", p->sp_namp, |
| 778 | (p->sp_pwdp ? p->sp_pwdp : "")) < 0 |
| 779 | ) { |
| 780 | goto DO_UNLOCK; |
| 781 | } |
| 782 | |
| 783 | for (i=0 ; i < sizeof(sp_off) ; i++) { |
| 784 | f = ld_format; |
| 785 | if ((x = *(const long int *)(((const char *) p) + sp_off[i])) == -1) { |
| 786 | f += 3; |
| 787 | } |
| 788 | if (fprintf(stream, f, x) < 0) { |
| 789 | goto DO_UNLOCK; |
| 790 | } |
| 791 | } |
| 792 | |
| 793 | if ((p->sp_flag != ~0UL) && (fprintf(stream, "%lu", p->sp_flag) < 0)) { |
| 794 | goto DO_UNLOCK; |
| 795 | } |
| 796 | |
Rob Landley | 25413bf | 2005-10-08 02:23:22 +0000 | [diff] [blame] | 797 | if (fputc('\n', stream) > 0) { |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 798 | rv = 0; |
| 799 | } |
| 800 | |
| 801 | DO_UNLOCK: |
| 802 | return rv; |
| 803 | } |
| 804 | |
| 805 | #endif |
| 806 | /**********************************************************************/ |
| 807 | /* Internal uClibc functions. */ |
| 808 | /**********************************************************************/ |
| 809 | #ifdef L___parsepwent |
| 810 | |
| 811 | static const unsigned char pw_off[] = { |
| 812 | offsetof(struct passwd, pw_name), /* 0 */ |
| 813 | offsetof(struct passwd, pw_passwd), /* 1 */ |
| 814 | offsetof(struct passwd, pw_uid), /* 2 - not a char ptr */ |
| 815 | offsetof(struct passwd, pw_gid), /* 3 - not a char ptr */ |
| 816 | offsetof(struct passwd, pw_gecos), /* 4 */ |
| 817 | offsetof(struct passwd, pw_dir), /* 5 */ |
| 818 | offsetof(struct passwd, pw_shell) /* 6 */ |
| 819 | }; |
| 820 | |
| 821 | int __parsepwent(void *data, char *line) |
| 822 | { |
| 823 | char *endptr; |
| 824 | char *p; |
| 825 | int i; |
| 826 | |
| 827 | i = 0; |
| 828 | do { |
| 829 | p = ((char *) ((struct passwd *) data)) + pw_off[i]; |
| 830 | |
| 831 | if ((i & 6) ^ 2) { /* i!=2 and i!=3 */ |
| 832 | *((char **) p) = line; |
| 833 | if (i==6) { |
| 834 | return 0; |
| 835 | } |
| 836 | /* NOTE: glibc difference - glibc allows omission of |
| 837 | * ':' seperators after the gid field if all remaining |
| 838 | * entries are empty. We require all separators. */ |
| 839 | if (!(line = strchr(line, ':'))) { |
| 840 | break; |
| 841 | } |
| 842 | } else { |
| 843 | unsigned long t = strtoul(line, &endptr, 10); |
| 844 | /* Make sure we had at least one digit, and that the |
| 845 | * failing char is the next field seperator ':'. See |
| 846 | * glibc difference note above. */ |
| 847 | /* TODO: Also check for leading whitespace? */ |
| 848 | if ((endptr == line) || (*endptr != ':')) { |
| 849 | break; |
| 850 | } |
| 851 | line = endptr; |
| 852 | if (i & 1) { /* i == 3 -- gid */ |
| 853 | *((gid_t *) p) = t; |
| 854 | } else { /* i == 2 -- uid */ |
| 855 | *((uid_t *) p) = t; |
| 856 | } |
| 857 | } |
| 858 | |
| 859 | *line++ = 0; |
| 860 | ++i; |
| 861 | } while (1); |
| 862 | |
| 863 | return -1; |
| 864 | } |
| 865 | |
| 866 | #endif |
| 867 | /**********************************************************************/ |
| 868 | #ifdef L___parsegrent |
| 869 | |
| 870 | static const unsigned char gr_off[] = { |
| 871 | offsetof(struct group, gr_name), /* 0 */ |
| 872 | offsetof(struct group, gr_passwd), /* 1 */ |
| 873 | offsetof(struct group, gr_gid) /* 2 - not a char ptr */ |
| 874 | }; |
| 875 | |
| 876 | int __parsegrent(void *data, char *line) |
| 877 | { |
| 878 | char *endptr; |
| 879 | char *p; |
| 880 | int i; |
| 881 | char **members; |
| 882 | char *end_of_buf; |
| 883 | |
| 884 | end_of_buf = ((struct group *) data)->gr_name; /* Evil hack! */ |
| 885 | i = 0; |
| 886 | do { |
| 887 | p = ((char *) ((struct group *) data)) + gr_off[i]; |
| 888 | |
| 889 | if (i < 2) { |
| 890 | *((char **) p) = line; |
| 891 | if (!(line = strchr(line, ':'))) { |
| 892 | break; |
| 893 | } |
| 894 | *line++ = 0; |
| 895 | ++i; |
| 896 | } else { |
| 897 | *((gid_t *) p) = strtoul(line, &endptr, 10); |
| 898 | |
| 899 | /* NOTE: glibc difference - glibc allows omission of the |
| 900 | * trailing colon when there is no member list. We treat |
| 901 | * this as an error. */ |
| 902 | |
| 903 | /* Make sure we had at least one digit, and that the |
| 904 | * failing char is the next field seperator ':'. See |
| 905 | * glibc difference note above. */ |
| 906 | if ((endptr == line) || (*endptr != ':')) { |
| 907 | break; |
| 908 | } |
| 909 | |
| 910 | i = 1; /* Count terminating NULL ptr. */ |
| 911 | p = endptr; |
| 912 | |
| 913 | if (p[1]) { /* We have a member list to process. */ |
| 914 | /* Overwrite the last ':' with a ',' before counting. |
| 915 | * This allows us to test for initial ',' and adds |
| 916 | * one ',' so that the ',' count equals the member |
| 917 | * count. */ |
| 918 | *p = ','; |
| 919 | do { |
| 920 | /* NOTE: glibc difference - glibc allows and trims leading |
| 921 | * (but not trailing) space. We treat this as an error. */ |
| 922 | /* NOTE: glibc difference - glibc allows consecutive and |
| 923 | * trailing commas, and ignores "empty string" users. We |
| 924 | * treat this as an error. */ |
| 925 | if (*p == ',') { |
| 926 | ++i; |
| 927 | *p = 0; /* nul-terminate each member string. */ |
| 928 | if (!*++p || (*p == ',') || isspace(*p)) { |
| 929 | goto ERR; |
| 930 | } |
| 931 | } |
| 932 | } while (*++p); |
| 933 | } |
| 934 | |
| 935 | /* Now align (p+1), rounding up. */ |
| 936 | /* Assumes sizeof(char **) is a power of 2. */ |
| 937 | members = (char **)( (((intptr_t) p) + sizeof(char **)) |
| 938 | & ~((intptr_t)(sizeof(char **) - 1)) ); |
| 939 | |
| 940 | if (((char *)(members + i)) > end_of_buf) { /* No space. */ |
| 941 | break; |
| 942 | } |
| 943 | |
| 944 | ((struct group *) data)->gr_mem = members; |
| 945 | |
| 946 | if (--i) { |
| 947 | p = endptr; /* Pointing to char prior to first member. */ |
| 948 | do { |
| 949 | *members++ = ++p; |
| 950 | if (!--i) break; |
| 951 | while (*++p) {} |
| 952 | } while (1); |
| 953 | } |
| 954 | *members = NULL; |
| 955 | |
| 956 | return 0; |
| 957 | } |
| 958 | } while (1); |
| 959 | |
| 960 | ERR: |
| 961 | return -1; |
| 962 | } |
| 963 | |
| 964 | #endif |
| 965 | /**********************************************************************/ |
| 966 | #ifdef L___parsespent |
| 967 | |
| 968 | static const unsigned char sp_off[] = { |
| 969 | offsetof(struct spwd, sp_namp), /* 0 */ |
| 970 | offsetof(struct spwd, sp_pwdp), /* 1 */ |
| 971 | offsetof(struct spwd, sp_lstchg), /* 2 - not a char ptr */ |
| 972 | offsetof(struct spwd, sp_min), /* 3 - not a char ptr */ |
| 973 | offsetof(struct spwd, sp_max), /* 4 - not a char ptr */ |
| 974 | offsetof(struct spwd, sp_warn), /* 5 - not a char ptr */ |
| 975 | offsetof(struct spwd, sp_inact), /* 6 - not a char ptr */ |
| 976 | offsetof(struct spwd, sp_expire), /* 7 - not a char ptr */ |
| 977 | offsetof(struct spwd, sp_flag) /* 8 - not a char ptr */ |
| 978 | }; |
| 979 | |
| 980 | int __parsespent(void *data, char * line) |
| 981 | { |
| 982 | char *endptr; |
| 983 | char *p; |
| 984 | int i; |
| 985 | |
| 986 | i = 0; |
| 987 | do { |
| 988 | p = ((char *) ((struct spwd *) data)) + sp_off[i]; |
| 989 | if (i < 2) { |
| 990 | *((char **) p) = line; |
| 991 | if (!(line = strchr(line, ':'))) { |
| 992 | break; |
| 993 | } |
| 994 | } else { |
| 995 | #if 0 |
| 996 | if (i==5) { /* Support for old format. */ |
| 997 | while (isspace(*line)) ++line; /* glibc eats space here. */ |
| 998 | if (!*line) { |
| 999 | ((struct spwd *) data)->sp_warn = -1; |
| 1000 | ((struct spwd *) data)->sp_inact = -1; |
| 1001 | ((struct spwd *) data)->sp_expire = -1; |
| 1002 | ((struct spwd *) data)->sp_flag = ~0UL; |
| 1003 | return 0; |
| 1004 | } |
| 1005 | } |
| 1006 | #endif |
| 1007 | |
| 1008 | *((long *) p) = (long) strtoul(line, &endptr, 10); |
| 1009 | |
| 1010 | if (endptr == line) { |
| 1011 | *((long *) p) = ((i != 8) ? -1L : ((long)(~0UL))); |
| 1012 | } |
| 1013 | |
| 1014 | line = endptr; |
| 1015 | |
| 1016 | if (i == 8) { |
| 1017 | if (!*endptr) { |
| 1018 | return 0; |
| 1019 | } |
| 1020 | break; |
| 1021 | } |
| 1022 | |
| 1023 | if (*endptr != ':') { |
| 1024 | break; |
| 1025 | } |
| 1026 | |
| 1027 | } |
| 1028 | |
| 1029 | *line++ = 0; |
| 1030 | ++i; |
| 1031 | } while (1); |
| 1032 | |
| 1033 | return EINVAL; |
| 1034 | } |
| 1035 | |
| 1036 | #endif |
| 1037 | /**********************************************************************/ |
| 1038 | #ifdef L___pgsreader |
| 1039 | |
| 1040 | /* Reads until if EOF, or until if finds a line which fits in the buffer |
| 1041 | * and for which the parser function succeeds. |
| 1042 | * |
| 1043 | * Returns 0 on success and ENOENT for end-of-file (glibc concession). |
| 1044 | */ |
| 1045 | |
| 1046 | int __pgsreader(int (*__parserfunc)(void *d, char *line), void *data, |
| 1047 | char *__restrict line_buff, size_t buflen, FILE *f) |
| 1048 | { |
| 1049 | int line_len; |
| 1050 | int skip; |
| 1051 | int rv = ERANGE; |
| 1052 | |
| 1053 | if (buflen < PWD_BUFFER_SIZE) { |
| 1054 | errno=rv; |
| 1055 | } else { |
| 1056 | skip = 0; |
| 1057 | do { |
Rob Landley | 25413bf | 2005-10-08 02:23:22 +0000 | [diff] [blame] | 1058 | if (!fgets(line_buff, buflen, f)) { |
| 1059 | if (feof(f)) { |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 1060 | rv = ENOENT; |
| 1061 | } |
| 1062 | break; |
| 1063 | } |
| 1064 | |
| 1065 | line_len = strlen(line_buff) - 1; /* strlen() must be > 0. */ |
| 1066 | if (line_buff[line_len] == '\n') { |
| 1067 | line_buff[line_len] = 0; |
| 1068 | } else if (line_len + 2 == buflen) { /* line too long */ |
| 1069 | ++skip; |
| 1070 | continue; |
| 1071 | } |
| 1072 | |
| 1073 | if (skip) { |
| 1074 | --skip; |
| 1075 | continue; |
| 1076 | } |
| 1077 | |
| 1078 | /* NOTE: glibc difference - glibc strips leading whitespace from |
| 1079 | * records. We do not allow leading whitespace. */ |
| 1080 | |
| 1081 | /* Skip empty lines, comment lines, and lines with leading |
| 1082 | * whitespace. */ |
| 1083 | if (*line_buff && (*line_buff != '#') && !isspace(*line_buff)) { |
| 1084 | if (__parserfunc == __parsegrent) { /* Do evil group hack. */ |
| 1085 | /* The group entry parsing function needs to know where |
| 1086 | * the end of the buffer is so that it can construct the |
| 1087 | * group member ptr table. */ |
| 1088 | ((struct group *) data)->gr_name = line_buff + buflen; |
| 1089 | } |
| 1090 | |
| 1091 | if (!__parserfunc(data, line_buff)) { |
| 1092 | rv = 0; |
| 1093 | break; |
| 1094 | } |
| 1095 | } |
| 1096 | } while (1); |
| 1097 | |
| 1098 | } |
| 1099 | |
| 1100 | return rv; |
| 1101 | } |
| 1102 | |
| 1103 | #endif |
| 1104 | /**********************************************************************/ |