Changeset 1770 in MondoRescue for branches/stable/mindi-busybox/libpwdgrp
- Timestamp:
- Nov 6, 2007, 11:01:53 AM (17 years ago)
- Location:
- branches/stable/mindi-busybox/libpwdgrp
- Files:
-
- 2 deleted
- 2 edited
- 2 copied
Legend:
- Unmodified
- Added
- Removed
-
branches/stable/mindi-busybox/libpwdgrp/pwd_grp.c
r821 r1770 1 /* vi: set sw=4 ts=4: */ 1 2 /* Copyright (C) 2003 Manuel Novoa III 2 3 * … … 20 21 #include "libbb.h" 21 22 #include <features.h> 22 #include <stdio.h>23 #include <stdlib.h>24 #include <stdint.h>25 #include <string.h>26 #include <stddef.h>27 #include <errno.h>28 23 #include <assert.h> 29 #include <ctype.h>30 31 #include "shadow_.h"32 24 33 25 #ifndef _PATH_SHADOW … … 52 44 /* Prototypes for internal functions. */ 53 45 54 extern int __parsepwent(void *pw, char *line); 55 extern int __parsegrent(void *gr, char *line); 56 extern int __parsespent(void *sp, char *line); 57 58 extern int __pgsreader(int (*__parserfunc)(void *d, char *line), void *data, 59 char *__restrict line_buff, size_t buflen, FILE *f); 46 static int bb__pgsreader(int (*parserfunc)(void *d, char *line), void *data, 47 char *__restrict line_buff, size_t buflen, FILE *f); 48 49 static int bb__parsepwent(void *pw, char *line); 50 static int bb__parsegrent(void *gr, char *line); 51 #if ENABLE_USE_BB_SHADOW 52 static int bb__parsespent(void *sp, char *line); 53 #endif 54 55 /**********************************************************************/ 56 /* We avoid having big global data. */ 57 58 struct statics { 59 /* Smaller things first */ 60 struct passwd getpwuid_resultbuf; 61 struct group getgrgid_resultbuf; 62 struct passwd getpwnam_resultbuf; 63 struct group getgrnam_resultbuf; 64 65 char getpwuid_buffer[PWD_BUFFER_SIZE]; 66 char getgrgid_buffer[GRP_BUFFER_SIZE]; 67 char getpwnam_buffer[PWD_BUFFER_SIZE]; 68 char getgrnam_buffer[GRP_BUFFER_SIZE]; 69 #if 0 70 struct passwd fgetpwent_resultbuf; 71 struct group fgetgrent_resultbuf; 72 struct spwd fgetspent_resultbuf; 73 char fgetpwent_buffer[PWD_BUFFER_SIZE]; 74 char fgetgrent_buffer[GRP_BUFFER_SIZE]; 75 char fgetspent_buffer[PWD_BUFFER_SIZE]; 76 #endif 77 #if 0 //ENABLE_USE_BB_SHADOW 78 struct spwd getspuid_resultbuf; 79 struct spwd getspnam_resultbuf; 80 char getspuid_buffer[PWD_BUFFER_SIZE]; 81 char getspnam_buffer[PWD_BUFFER_SIZE]; 82 #endif 83 // Not converted - too small to bother 84 //pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER; 85 //FILE *pwf /*= NULL*/; 86 //FILE *grf /*= NULL*/; 87 //FILE *spf /*= NULL*/; 88 #if 0 89 struct passwd getpwent_pwd; 90 struct group getgrent_gr; 91 char getpwent_line_buff[PWD_BUFFER_SIZE]; 92 char getgrent_line_buff[GRP_BUFFER_SIZE]; 93 #endif 94 #if 0 //ENABLE_USE_BB_SHADOW 95 struct spwd getspent_spwd; 96 struct spwd sgetspent_spwd; 97 char getspent_line_buff[PWD_BUFFER_SIZE]; 98 char sgetspent_line_buff[PWD_BUFFER_SIZE]; 99 #endif 100 }; 101 102 static struct statics *ptr_to_statics; 103 104 static struct statics *get_S(void) 105 { 106 if (!ptr_to_statics) 107 ptr_to_statics = xzalloc(sizeof(*ptr_to_statics)); 108 return ptr_to_statics; 109 } 110 111 /* Always use in this order, get_S() must be called first */ 112 #define RESULTBUF(name) &((S = get_S())->name##_resultbuf) 113 #define BUFFER(name) (S->name##_buffer) 60 114 61 115 /**********************************************************************/ … … 65 119 * ENOENT: end-of-file encountered 66 120 * ERANGE: buflen too small 67 * other error values possible. See __pgsreader.121 * other error values possible. See bb__pgsreader. 68 122 * 69 123 * Also, *result == resultbuf on success and NULL on failure. … … 75 129 /**********************************************************************/ 76 130 77 #ifdef L_fgetpwent_r78 79 131 int fgetpwent_r(FILE *__restrict stream, struct passwd *__restrict resultbuf, 80 132 char *__restrict buffer, size_t buflen, … … 85 137 *result = NULL; 86 138 87 if (!(rv = __pgsreader(__parsepwent, resultbuf, buffer, buflen, stream))) { 139 rv = bb__pgsreader(bb__parsepwent, resultbuf, buffer, buflen, stream); 140 if (!rv) { 88 141 *result = resultbuf; 89 142 } … … 91 144 return rv; 92 145 } 93 94 #endif95 /**********************************************************************/96 #ifdef L_fgetgrent_r97 146 98 147 int fgetgrent_r(FILE *__restrict stream, struct group *__restrict resultbuf, … … 104 153 *result = NULL; 105 154 106 if (!(rv = __pgsreader(__parsegrent, resultbuf, buffer, buflen, stream))) { 155 rv = bb__pgsreader(bb__parsegrent, resultbuf, buffer, buflen, stream); 156 if (!rv) { 107 157 *result = resultbuf; 108 158 } … … 111 161 } 112 162 113 #endif 114 /**********************************************************************/ 115 #ifdef L_fgetspent_r 116 163 #if ENABLE_USE_BB_SHADOW 117 164 int fgetspent_r(FILE *__restrict stream, struct spwd *__restrict resultbuf, 118 165 char *__restrict buffer, size_t buflen, … … 123 170 *result = NULL; 124 171 125 if (!(rv = __pgsreader(__parsespent, resultbuf, buffer, buflen, stream))) { 172 rv = bb__pgsreader(bb__parsespent, resultbuf, buffer, buflen, stream); 173 if (!rv) { 126 174 *result = resultbuf; 127 175 } … … 129 177 return rv; 130 178 } 131 132 #endif 179 #endif 180 133 181 /**********************************************************************/ 134 182 /* For the various fget??ent funcs, return NULL on failure and a 135 183 * pointer to the appropriate struct (statically allocated) on success. 136 * /137 /**********************************************************************/ 138 #ifdef L_fgetpwent 139 184 * TODO: audit & stop using these in bbox, they pull in static buffers */ 185 /**********************************************************************/ 186 187 #if 0 140 188 struct passwd *fgetpwent(FILE *stream) 141 189 { 142 static char buffer[PWD_BUFFER_SIZE]; 143 static struct passwd resultbuf; 190 struct statics *S; 191 struct passwd *resultbuf = RESULTBUF(fgetpwent); 192 char *buffer = BUFFER(fgetpwent); 144 193 struct passwd *result; 145 194 146 fgetpwent_r(stream, &resultbuf, buffer, sizeof(buffer), &result); 147 return result; 148 } 149 150 #endif 151 /**********************************************************************/ 152 #ifdef L_fgetgrent 195 fgetpwent_r(stream, resultbuf, buffer, sizeof(BUFFER(fgetpwent)), &result); 196 return result; 197 } 153 198 154 199 struct group *fgetgrent(FILE *stream) 155 200 { 156 static char buffer[GRP_BUFFER_SIZE]; 157 static struct group resultbuf; 201 struct statics *S; 202 struct group *resultbuf = RESULTBUF(fgetgrent); 203 char *buffer = BUFFER(fgetgrent); 158 204 struct group *result; 159 205 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); 206 fgetgrent_r(stream, resultbuf, buffer, sizeof(BUFFER(fgetgrent)), &result); 207 return result; 208 } 209 #endif 210 211 #if ENABLE_USE_BB_SHADOW 212 #if 0 171 213 struct spwd *fgetspent(FILE *stream) 172 214 { 173 static char buffer[PWD_BUFFER_SIZE]; 174 static struct spwd resultbuf; 215 struct statics *S; 216 struct spwd *resultbuf = RESULTBUF(fgetspent); 217 char *buffer = BUFFER(fgetspent); 175 218 struct spwd *result; 176 219 177 fgetspent_r(stream, &resultbuf, buffer, sizeof(buffer), &result); 178 return result; 179 } 180 181 #endif 182 /**********************************************************************/ 183 #ifdef L_sgetspent_r 220 fgetspent_r(stream, resultbuf, buffer, sizeof(BUFFER(fgetspent)), &result); 221 return result; 222 } 223 #endif 184 224 185 225 int sgetspent_r(const char *string, struct spwd *result_buf, … … 203 243 } 204 244 205 if (!(rv = __parsespent(result_buf, buffer))) { 245 rv = bb__parsespent(result_buf, buffer); 246 if (!rv) { 206 247 *result = result_buf; 207 248 } … … 210 251 return rv; 211 252 } 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 253 #endif 254 255 /**********************************************************************/ 256 257 #define GETXXKEY_R_FUNC getpwnam_r 258 #define GETXXKEY_R_PARSER bb__parsepwent 259 #define GETXXKEY_R_ENTTYPE struct passwd 260 #define GETXXKEY_R_TEST(ENT) (!strcmp((ENT)->pw_name, key)) 261 #define GETXXKEY_R_KEYTYPE const char *__restrict 262 #define GETXXKEY_R_PATHNAME _PATH_PASSWD 227 263 #include "pwd_grp_internal.c" 228 #endif 229 230 #ifdef L_getgrnam_r 231 #define GETXXKEY_R_FUNC getgrnam_r 232 #define GETXXKEY_R_PARSER __parsegrent 233 #define GETXXKEY_R_ENTTYPE struct group 234 #define GETXXKEY_R_TEST(ENT) (!strcmp((ENT)->gr_name, key)) 235 #define DO_GETXXKEY_R_KEYTYPE const char *__restrict 236 #define DO_GETXXKEY_R_PATHNAME _PATH_GROUP 264 265 #define GETXXKEY_R_FUNC getgrnam_r 266 #define GETXXKEY_R_PARSER bb__parsegrent 267 #define GETXXKEY_R_ENTTYPE struct group 268 #define GETXXKEY_R_TEST(ENT) (!strcmp((ENT)->gr_name, key)) 269 #define GETXXKEY_R_KEYTYPE const char *__restrict 270 #define GETXXKEY_R_PATHNAME _PATH_GROUP 237 271 #include "pwd_grp_internal.c" 238 #endif 239 240 #ifdef L_getspnam_r 241 #define GETXXKEY_R_FUNC getspnam_r 242 #define GETXXKEY_R_PARSER __parsespent 243 #define GETXXKEY_R_ENTTYPE struct spwd 244 #define GETXXKEY_R_TEST(ENT) (!strcmp((ENT)->sp_namp, key)) 245 #define DO_GETXXKEY_R_KEYTYPE const char *__restrict 246 #define DO_GETXXKEY_R_PATHNAME _PATH_SHADOW 272 273 #if ENABLE_USE_BB_SHADOW 274 #define GETXXKEY_R_FUNC getspnam_r 275 #define GETXXKEY_R_PARSER bb__parsespent 276 #define GETXXKEY_R_ENTTYPE struct spwd 277 #define GETXXKEY_R_TEST(ENT) (!strcmp((ENT)->sp_namp, key)) 278 #define GETXXKEY_R_KEYTYPE const char *__restrict 279 #define GETXXKEY_R_PATHNAME _PATH_SHADOW 247 280 #include "pwd_grp_internal.c" 248 281 #endif 249 282 250 #ifdef L_getpwuid_r 251 #define GETXXKEY_R_FUNC getpwuid_r 252 #define GETXXKEY_R_PARSER __parsepwent 253 #define GETXXKEY_R_ENTTYPE struct passwd 254 #define GETXXKEY_R_TEST(ENT) ((ENT)->pw_uid == key) 255 #define DO_GETXXKEY_R_KEYTYPE uid_t 256 #define DO_GETXXKEY_R_PATHNAME _PATH_PASSWD 283 #define GETXXKEY_R_FUNC getpwuid_r 284 #define GETXXKEY_R_PARSER bb__parsepwent 285 #define GETXXKEY_R_ENTTYPE struct passwd 286 #define GETXXKEY_R_TEST(ENT) ((ENT)->pw_uid == key) 287 #define GETXXKEY_R_KEYTYPE uid_t 288 #define GETXXKEY_R_PATHNAME _PATH_PASSWD 257 289 #include "pwd_grp_internal.c" 258 #endif 259 260 #ifdef L_getgrgid_r 261 #define GETXXKEY_R_FUNC getgrgid_r 262 #define GETXXKEY_R_PARSER __parsegrent 263 #define GETXXKEY_R_ENTTYPE struct group 264 #define GETXXKEY_R_TEST(ENT) ((ENT)->gr_gid == key) 265 #define DO_GETXXKEY_R_KEYTYPE gid_t 266 #define DO_GETXXKEY_R_PATHNAME _PATH_GROUP 290 291 #define GETXXKEY_R_FUNC getgrgid_r 292 #define GETXXKEY_R_PARSER bb__parsegrent 293 #define GETXXKEY_R_ENTTYPE struct group 294 #define GETXXKEY_R_TEST(ENT) ((ENT)->gr_gid == key) 295 #define GETXXKEY_R_KEYTYPE gid_t 296 #define GETXXKEY_R_PATHNAME _PATH_GROUP 267 297 #include "pwd_grp_internal.c" 268 #endif 269 270 /* *********************************************************************/271 #ifdef L_getpwuid 272 298 299 /**********************************************************************/ 300 /* TODO: audit & stop using these in bbox, they pull in static buffers */ 301 302 /* This one has many users */ 273 303 struct passwd *getpwuid(uid_t uid) 274 304 { 275 static char buffer[PWD_BUFFER_SIZE]; 276 static struct passwd resultbuf; 305 struct statics *S; 306 struct passwd *resultbuf = RESULTBUF(getpwuid); 307 char *buffer = BUFFER(getpwuid); 277 308 struct passwd *result; 278 309 279 getpwuid_r(uid, &resultbuf, buffer, sizeof(buffer), &result); 280 return result; 281 } 282 283 #endif 284 /**********************************************************************/ 285 #ifdef L_getgrgid 286 310 getpwuid_r(uid, resultbuf, buffer, sizeof(BUFFER(getpwuid)), &result); 311 return result; 312 } 313 314 /* This one has many users */ 287 315 struct group *getgrgid(gid_t gid) 288 316 { 289 static char buffer[GRP_BUFFER_SIZE]; 290 static struct group resultbuf; 317 struct statics *S; 318 struct group *resultbuf = RESULTBUF(getgrgid); 319 char *buffer = BUFFER(getgrgid); 291 320 struct group *result; 292 321 293 getgrgid_r(gid, &resultbuf, buffer, sizeof(buffer), &result); 294 return result; 295 } 296 297 #endif 298 /**********************************************************************/ 299 #ifdef L_getspuid_r 300 322 getgrgid_r(gid, resultbuf, buffer, sizeof(BUFFER(getgrgid)), &result); 323 return result; 324 } 325 326 #if 0 //ENABLE_USE_BB_SHADOW 301 327 /* This function is non-standard and is currently not built. It seems 302 328 * to have been created as a reentrant version of the non-standard 303 329 * functions getspuid. Why getspuid was added, I do not know. */ 304 305 330 int getspuid_r(uid_t uid, struct spwd *__restrict resultbuf, 306 331 char *__restrict buffer, size_t buflen, … … 313 338 314 339 *result = NULL; 315 if (!(rv = getpwuid_r(uid, &password, pwd_buff, sizeof(pwd_buff), &pp))) { 340 rv = getpwuid_r(uid, &password, pwd_buff, sizeof(pwd_buff), &pp); 341 if (!rv) { 316 342 rv = getspnam_r(password.pw_name, resultbuf, buffer, buflen, result); 317 343 } … … 319 345 return rv; 320 346 } 321 322 #endif323 /**********************************************************************/324 #ifdef L_getspuid325 347 326 348 /* This function is non-standard and is currently not built. 327 349 * Why it was added, I do not know. */ 328 329 350 struct spwd *getspuid(uid_t uid) 330 351 { 331 static char buffer[PWD_BUFFER_SIZE]; 332 static struct spwd resultbuf; 352 struct statics *S; 353 struct spwd *resultbuf = RESULTBUF(getspuid); 354 char *buffer = BUFFER(getspuid); 333 355 struct spwd *result; 334 356 335 getspuid_r(uid, &resultbuf, buffer, sizeof(buffer), &result); 336 return result; 337 } 338 339 #endif 340 /**********************************************************************/ 341 #ifdef L_getpwnam 342 357 getspuid_r(uid, resultbuf, buffer, sizeof(BUFFER(getspuid)), &result); 358 return result; 359 } 360 #endif 361 362 /* This one has many users */ 343 363 struct passwd *getpwnam(const char *name) 344 364 { 345 static char buffer[PWD_BUFFER_SIZE]; 346 static struct passwd resultbuf; 365 struct statics *S; 366 struct passwd *resultbuf = RESULTBUF(getpwnam); 367 char *buffer = BUFFER(getpwnam); 347 368 struct passwd *result; 348 369 349 getpwnam_r(name, &resultbuf, buffer, sizeof(buffer), &result); 350 return result; 351 } 352 353 #endif 354 /**********************************************************************/ 355 #ifdef L_getgrnam 356 370 getpwnam_r(name, resultbuf, buffer, sizeof(BUFFER(getpwnam)), &result); 371 return result; 372 } 373 374 /* This one has many users */ 357 375 struct group *getgrnam(const char *name) 358 376 { 359 static char buffer[GRP_BUFFER_SIZE]; 360 static struct group resultbuf; 377 struct statics *S; 378 struct group *resultbuf = RESULTBUF(getgrnam); 379 char *buffer = BUFFER(getgrnam); 361 380 struct group *result; 362 381 363 getgrnam_r(name, &resultbuf, buffer, sizeof(buffer), &result); 364 return result; 365 } 366 367 #endif 368 /**********************************************************************/ 369 #ifdef L_getspnam 370 382 getgrnam_r(name, resultbuf, buffer, sizeof(BUFFER(getgrnam)), &result); 383 return result; 384 } 385 386 #if 0 //ENABLE_USE_BB_SHADOW 371 387 struct spwd *getspnam(const char *name) 372 388 { 373 static char buffer[PWD_BUFFER_SIZE]; 374 static struct spwd resultbuf; 389 struct statics *S; 390 struct spwd *resultbuf = RESULTBUF(getspnam); 391 char *buffer = BUFFER(getspnam); 375 392 struct spwd *result; 376 393 377 getspnam_r(name, &resultbuf, buffer, sizeof(buffer), &result); 378 return result; 379 } 380 381 #endif 382 /**********************************************************************/ 383 #ifdef L_getpw 384 394 getspnam_r(name, resultbuf, buffer, sizeof(BUFFER(getspnam)), &result); 395 return result; 396 } 397 #endif 398 399 /* This one doesn't use static buffers */ 385 400 int getpw(uid_t uid, char *buf) 386 401 { … … 390 405 391 406 if (!buf) { 392 errno =EINVAL;407 errno = EINVAL; 393 408 } else if (!getpwuid_r(uid, &resultbuf, buffer, sizeof(buffer), &result)) { 394 409 if (sprintf(buf, "%s:%s:%lu:%lu:%s:%s:%s\n", … … 406 421 } 407 422 408 #endif 409 /**********************************************************************/ 410 411 #if defined(L_getpwent_r) || defined(L_getgrent_r) || defined(L_getspent_r) 423 /**********************************************************************/ 424 425 /* FIXME: we don't have such CONFIG_xx - ?! */ 426 412 427 #if defined CONFIG_USE_BB_THREADSAFE_SHADOW && defined PTHREAD_MUTEX_INITIALIZER 413 428 static pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER; … … 418 433 # define UNLOCK ((void) 0) 419 434 #endif 420 #endif 421 422 #ifdef L_getpwent_r 435 423 436 static FILE *pwf /*= NULL*/; 424 437 void setpwent(void) … … 452 465 453 466 if (!pwf) { 454 if (!(pwf = fopen(_PATH_PASSWD, "r"))) { 467 pwf = fopen(_PATH_PASSWD, "r"); 468 if (!pwf) { 455 469 rv = errno; 456 470 goto ERR; … … 458 472 } 459 473 460 if (!(rv = __pgsreader(__parsepwent, resultbuf,461 buffer, buflen, pwf))) {474 rv = bb__pgsreader(bb__parsepwent, resultbuf, buffer, buflen, pwf); 475 if (!rv) { 462 476 *result = resultbuf; 463 477 } … … 467 481 return rv; 468 482 } 469 470 #endif471 /**********************************************************************/472 #ifdef L_getgrent_r473 483 474 484 static FILE *grf /*= NULL*/; … … 502 512 503 513 if (!grf) { 504 if (!(grf = fopen(_PATH_GROUP, "r"))) { 514 grf = fopen(_PATH_GROUP, "r"); 515 if (!grf) { 505 516 rv = errno; 506 517 goto ERR; … … 508 519 } 509 520 510 if (!(rv = __pgsreader(__parsegrent, resultbuf,511 buffer, buflen, grf))) {521 rv = bb__pgsreader(bb__parsegrent, resultbuf, buffer, buflen, grf); 522 if (!rv) { 512 523 *result = resultbuf; 513 524 } … … 518 529 } 519 530 520 #endif 521 /**********************************************************************/ 522 #ifdef L_getspent_r 523 531 #if ENABLE_USE_BB_SHADOW 524 532 static FILE *spf /*= NULL*/; 525 533 void setspent(void) … … 551 559 552 560 if (!spf) { 553 if (!(spf = fopen(_PATH_SHADOW, "r"))) { 561 spf = fopen(_PATH_SHADOW, "r"); 562 if (!spf) { 554 563 rv = errno; 555 564 goto ERR; … … 557 566 } 558 567 559 if (!(rv = __pgsreader(__parsespent, resultbuf,560 buffer, buflen, spf))) {568 rv = bb__pgsreader(bb__parsespent, resultbuf, buffer, buflen, spf); 569 if (!rv) { 561 570 *result = resultbuf; 562 571 } … … 566 575 return rv; 567 576 } 568 569 #endif 570 /**********************************************************************/ 571 #ifdef L_getpwent 572 577 #endif 578 579 #if 0 573 580 struct passwd *getpwent(void) 574 581 { … … 581 588 } 582 589 583 #endif584 /**********************************************************************/585 #ifdef L_getgrent586 587 590 struct group *getgrent(void) 588 591 { … … 594 597 return result; 595 598 } 596 597 #endif 598 /**********************************************************************/ 599 #ifdef L_getspent 600 599 #endif 600 601 #if 0 //ENABLE_USE_BB_SHADOW 601 602 struct spwd *getspent(void) 602 603 { … … 609 610 } 610 611 611 #endif612 /**********************************************************************/613 #ifdef L_sgetspent614 615 612 struct spwd *sgetspent(const char *string) 616 613 { … … 622 619 return result; 623 620 } 624 625 #endif 626 /**********************************************************************/ 627 #ifdef L_initgroups 621 #endif 628 622 629 623 int initgroups(const char *user, gid_t gid) … … 639 633 640 634 /* We alloc space for 8 gids at a time. */ 641 if (((group_list = (gid_t *) malloc(8*sizeof(gid_t *))) != NULL)642 && ((grfile = fopen(_PATH_GROUP, "r")) != NULL)643 ) {644 635 group_list = (gid_t *) malloc(8*sizeof(gid_t *)); 636 if (group_list 637 && ((grfile = fopen(_PATH_GROUP, "r")) != NULL) 638 ) { 645 639 *group_list = gid; 646 640 num_groups = 1; 647 641 648 while (! __pgsreader(__parsegrent, &group, buff, sizeof(buff), grfile)) {642 while (!bb__pgsreader(bb__parsegrent, &group, buff, sizeof(buff), grfile)) { 649 643 assert(group.gr_mem); /* Must have at least a NULL terminator. */ 650 644 if (group.gr_gid != gid) { 651 for (m =group.gr_mem ; *m; m++) {645 for (m = group.gr_mem; *m; m++) { 652 646 if (!strcmp(*m, user)) { 653 647 if (!(num_groups & 7)) { … … 679 673 } 680 674 681 #endif682 /**********************************************************************/683 #ifdef L_putpwent684 685 675 int putpwent(const struct passwd *__restrict p, FILE *__restrict f) 686 676 { … … 704 694 } 705 695 706 #endif707 /**********************************************************************/708 #ifdef L_putgrent709 710 696 int putgrent(const struct group *__restrict p, FILE *__restrict f) 711 697 { 712 static const char format[] = ",%s"; 698 static const char format[] ALIGN1 = ",%s"; 699 713 700 char **m; 714 701 const char *fmt; … … 749 736 } 750 737 751 #endif 752 /**********************************************************************/ 753 #ifdef L_putspent 754 755 static const unsigned char _sp_off[] = { 756 offsetof(struct spwd, sp_lstchg), /* 2 - not a char ptr */ 757 offsetof(struct spwd, sp_min), /* 3 - not a char ptr */ 758 offsetof(struct spwd, sp_max), /* 4 - not a char ptr */ 759 offsetof(struct spwd, sp_warn), /* 5 - not a char ptr */ 760 offsetof(struct spwd, sp_inact), /* 6 - not a char ptr */ 761 offsetof(struct spwd, sp_expire), /* 7 - not a char ptr */ 738 #if ENABLE_USE_BB_SHADOW 739 static const unsigned char _sp_off[] ALIGN1 = { 740 offsetof(struct spwd, sp_lstchg), /* 2 - not a char ptr */ 741 offsetof(struct spwd, sp_min), /* 3 - not a char ptr */ 742 offsetof(struct spwd, sp_max), /* 4 - not a char ptr */ 743 offsetof(struct spwd, sp_warn), /* 5 - not a char ptr */ 744 offsetof(struct spwd, sp_inact), /* 6 - not a char ptr */ 745 offsetof(struct spwd, sp_expire) /* 7 - not a char ptr */ 762 746 }; 763 747 764 748 int putspent(const struct spwd *p, FILE *stream) 765 749 { 766 static const char ld_format[] = "%ld:"; 750 static const char ld_format[] ALIGN1 = "%ld:"; 751 767 752 const char *f; 768 long intx;753 long x; 769 754 int i; 770 755 int rv = -1; … … 773 758 if (fprintf(stream, "%s:%s:", p->sp_namp, 774 759 (p->sp_pwdp ? p->sp_pwdp : "")) < 0 775 760 ) { 776 761 goto DO_UNLOCK; 777 762 } 778 763 779 for (i =0 ; i < sizeof(_sp_off); i++) {764 for (i = 0; i < sizeof(_sp_off); i++) { 780 765 f = ld_format; 781 if ((x = *(const long int *)(((const char *) p) + _sp_off[i])) == -1) { 766 x = *(const long *)(((const char *) p) + _sp_off[i]); 767 if (x == -1) { 782 768 f += 3; 783 769 } … … 798 784 return rv; 799 785 } 800 801 #endif 802 /**********************************************************************/ 803 /* Internal uClibc functions. */ 804 /**********************************************************************/ 805 #ifdef L___parsepwent 806 807 static const unsigned char pw_off[] = { 808 offsetof(struct passwd, pw_name), /* 0 */ 809 offsetof(struct passwd, pw_passwd), /* 1 */ 810 offsetof(struct passwd, pw_uid), /* 2 - not a char ptr */ 811 offsetof(struct passwd, pw_gid), /* 3 - not a char ptr */ 812 offsetof(struct passwd, pw_gecos), /* 4 */ 813 offsetof(struct passwd, pw_dir), /* 5 */ 814 offsetof(struct passwd, pw_shell) /* 6 */ 786 #endif 787 788 /**********************************************************************/ 789 /* Internal uClibc functions. */ 790 /**********************************************************************/ 791 792 static const unsigned char pw_off[] ALIGN1 = { 793 offsetof(struct passwd, pw_name), /* 0 */ 794 offsetof(struct passwd, pw_passwd), /* 1 */ 795 offsetof(struct passwd, pw_uid), /* 2 - not a char ptr */ 796 offsetof(struct passwd, pw_gid), /* 3 - not a char ptr */ 797 offsetof(struct passwd, pw_gecos), /* 4 */ 798 offsetof(struct passwd, pw_dir), /* 5 */ 799 offsetof(struct passwd, pw_shell) /* 6 */ 815 800 }; 816 801 817 int__parsepwent(void *data, char *line)802 static int bb__parsepwent(void *data, char *line) 818 803 { 819 804 char *endptr; … … 833 818 * ':' seperators after the gid field if all remaining 834 819 * entries are empty. We require all separators. */ 835 if (!(line = strchr(line, ':'))) { 820 line = strchr(line, ':'); 821 if (!line) { 836 822 break; 837 823 } … … 860 846 } 861 847 862 #endif 863 /**********************************************************************/ 864 #ifdef L___parsegrent 865 866 static const unsigned char gr_off[] = { 867 offsetof(struct group, gr_name), /* 0 */ 868 offsetof(struct group, gr_passwd), /* 1 */ 869 offsetof(struct group, gr_gid) /* 2 - not a char ptr */ 848 /**********************************************************************/ 849 850 static const unsigned char gr_off[] ALIGN1 = { 851 offsetof(struct group, gr_name), /* 0 */ 852 offsetof(struct group, gr_passwd), /* 1 */ 853 offsetof(struct group, gr_gid) /* 2 - not a char ptr */ 870 854 }; 871 855 872 int__parsegrent(void *data, char *line)856 static int bb__parsegrent(void *data, char *line) 873 857 { 874 858 char *endptr; … … 885 869 if (i < 2) { 886 870 *((char **) p) = line; 887 if (!(line = strchr(line, ':'))) { 871 line = strchr(line, ':'); 872 if (!line) { 888 873 break; 889 874 } … … 958 943 } 959 944 960 #endif 961 /**********************************************************************/ 962 #ifdef L___parsespent 963 964 static const unsigned char sp_off[] = { 965 offsetof(struct spwd, sp_namp), /* 0 */ 966 offsetof(struct spwd, sp_pwdp), /* 1 */ 967 offsetof(struct spwd, sp_lstchg), /* 2 - not a char ptr */ 968 offsetof(struct spwd, sp_min), /* 3 - not a char ptr */ 969 offsetof(struct spwd, sp_max), /* 4 - not a char ptr */ 970 offsetof(struct spwd, sp_warn), /* 5 - not a char ptr */ 971 offsetof(struct spwd, sp_inact), /* 6 - not a char ptr */ 972 offsetof(struct spwd, sp_expire), /* 7 - not a char ptr */ 973 offsetof(struct spwd, sp_flag) /* 8 - not a char ptr */ 945 /**********************************************************************/ 946 947 #if ENABLE_USE_BB_SHADOW 948 static const unsigned char sp_off[] ALIGN1 = { 949 offsetof(struct spwd, sp_namp), /* 0 */ 950 offsetof(struct spwd, sp_pwdp), /* 1 */ 951 offsetof(struct spwd, sp_lstchg), /* 2 - not a char ptr */ 952 offsetof(struct spwd, sp_min), /* 3 - not a char ptr */ 953 offsetof(struct spwd, sp_max), /* 4 - not a char ptr */ 954 offsetof(struct spwd, sp_warn), /* 5 - not a char ptr */ 955 offsetof(struct spwd, sp_inact), /* 6 - not a char ptr */ 956 offsetof(struct spwd, sp_expire), /* 7 - not a char ptr */ 957 offsetof(struct spwd, sp_flag) /* 8 - not a char ptr */ 974 958 }; 975 959 976 int__parsespent(void *data, char * line)960 static int bb__parsespent(void *data, char * line) 977 961 { 978 962 char *endptr; … … 985 969 if (i < 2) { 986 970 *((char **) p) = line; 987 if (!(line = strchr(line, ':'))) { 971 line = strchr(line, ':'); 972 if (!line) { 988 973 break; 989 974 } 990 975 } else { 991 #if 0992 if (i==5) { /* Support for old format. */993 while (isspace(*line)) ++line; /* glibc eats space here. */994 if (!*line) {995 ((struct spwd *) data)->sp_warn = -1;996 ((struct spwd *) data)->sp_inact = -1;997 ((struct spwd *) data)->sp_expire = -1;998 ((struct spwd *) data)->sp_flag = ~0UL;999 return 0;1000 }1001 }1002 #endif1003 1004 976 *((long *) p) = (long) strtoul(line, &endptr, 10); 1005 977 … … 1029 1001 return EINVAL; 1030 1002 } 1031 1032 #endif 1033 /**********************************************************************/ 1034 #ifdef L___pgsreader 1003 #endif 1004 1005 /**********************************************************************/ 1035 1006 1036 1007 /* Reads until if EOF, or until if finds a line which fits in the buffer … … 1040 1011 */ 1041 1012 1042 int __pgsreader(int (*__parserfunc)(void *d, char *line), void *data,1013 static int bb__pgsreader(int (*parserfunc)(void *d, char *line), void *data, 1043 1014 char *__restrict line_buff, size_t buflen, FILE *f) 1044 1015 { … … 1048 1019 1049 1020 if (buflen < PWD_BUFFER_SIZE) { 1050 errno =rv;1021 errno = rv; 1051 1022 } else { 1052 1023 skip = 0; … … 1078 1049 * whitespace. */ 1079 1050 if (*line_buff && (*line_buff != '#') && !isspace(*line_buff)) { 1080 if ( __parserfunc ==__parsegrent) { /* Do evil group hack. */1051 if (parserfunc == bb__parsegrent) { /* Do evil group hack. */ 1081 1052 /* The group entry parsing function needs to know where 1082 1053 * the end of the buffer is so that it can construct the … … 1085 1056 } 1086 1057 1087 if (! __parserfunc(data, line_buff)) {1058 if (!parserfunc(data, line_buff)) { 1088 1059 rv = 0; 1089 1060 break; … … 1096 1067 return rv; 1097 1068 } 1098 1099 #endif1100 /**********************************************************************/ -
branches/stable/mindi-busybox/libpwdgrp/pwd_grp_internal.c
r821 r1770 1 /* vi: set sw=4 ts=4: */ 1 2 /* Copyright (C) 2003 Manuel Novoa III 2 3 * … … 18 19 */ 19 20 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 30 #include "pwd_.h"31 #include "grp_.h"32 #include "shadow_.h"33 #include "libbb.h"34 35 #ifndef _PATH_SHADOW36 #define _PATH_SHADOW "/etc/shadow"37 #endif38 #ifndef _PATH_PASSWD39 #define _PATH_PASSWD "/etc/passwd"40 #endif41 #ifndef _PATH_GROUP42 #define _PATH_GROUP "/etc/group"43 #endif44 45 /**********************************************************************/46 /* Sizes for statically allocated buffers. */47 48 /* If you change these values, also change _SC_GETPW_R_SIZE_MAX and49 * _SC_GETGR_R_SIZE_MAX in libc/unistd/sysconf.c to match */50 #define PWD_BUFFER_SIZE 25651 #define GRP_BUFFER_SIZE 25652 53 /**********************************************************************/54 /* Prototypes for internal functions. */55 56 extern int __parsepwent(void *pw, char *line);57 extern int __parsegrent(void *gr, char *line);58 extern int __parsespent(void *sp, char *line);59 60 extern int __pgsreader(int (*__parserfunc)(void *d, char *line), void *data,61 char *__restrict line_buff, size_t buflen, FILE *f);62 63 64 21 #ifndef GETXXKEY_R_FUNC 65 22 #error GETXXKEY_R_FUNC is not defined! 66 23 #endif 67 /**********************************************************************/68 #ifdef GETXXKEY_R_FUNC69 24 70 int GETXXKEY_R_FUNC( DO_GETXXKEY_R_KEYTYPE key,71 72 73 25 int GETXXKEY_R_FUNC(GETXXKEY_R_KEYTYPE key, 26 GETXXKEY_R_ENTTYPE *__restrict resultbuf, 27 char *__restrict buffer, size_t buflen, 28 GETXXKEY_R_ENTTYPE **__restrict result) 74 29 { 75 30 FILE *stream; … … 78 33 *result = NULL; 79 34 80 if (!(stream = fopen(DO_GETXXKEY_R_PATHNAME, "r"))) { 81 rv = errno; 82 } else { 83 do { 84 if (!(rv = __pgsreader(GETXXKEY_R_PARSER, resultbuf, 85 buffer, buflen, stream)) 86 ) { 87 if (GETXXKEY_R_TEST(resultbuf)) { /* Found key? */ 88 *result = resultbuf; 89 break; 90 } 91 } else { 92 if (rv == ENOENT) { /* end-of-file encountered. */ 93 rv = 0; 94 } 35 stream = fopen(GETXXKEY_R_PATHNAME, "r"); 36 if (!stream) 37 return errno; 38 while (1) { 39 rv = bb__pgsreader(GETXXKEY_R_PARSER, resultbuf, buffer, buflen, stream); 40 if (!rv) { 41 if (GETXXKEY_R_TEST(resultbuf)) { /* Found key? */ 42 *result = resultbuf; 95 43 break; 96 44 } 97 } while (1); 98 fclose(stream); 45 } else { 46 if (rv == ENOENT) { /* end-of-file encountered. */ 47 rv = 0; 48 } 49 break; 50 } 99 51 } 52 fclose(stream); 100 53 101 54 return rv; 102 55 } 103 56 104 #endif105 /**********************************************************************/106 57 #undef GETXXKEY_R_FUNC 107 58 #undef GETXXKEY_R_PARSER 108 59 #undef GETXXKEY_R_ENTTYPE 109 60 #undef GETXXKEY_R_TEST 110 #undef DO_GETXXKEY_R_KEYTYPE 111 #undef DO_GETXXKEY_R_PATHNAME 112 61 #undef GETXXKEY_R_KEYTYPE 62 #undef GETXXKEY_R_PATHNAME
Note:
See TracChangeset
for help on using the changeset viewer.