pwd_grp.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060
  1. /* vi: set sw=4 ts=4: */
  2. /* Copyright (C) 2003 Manuel Novoa III
  3. *
  4. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  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 statically allocated)
  15. * buffers; especially for the group-related functions since
  16. * large group member lists will cause error returns.
  17. */
  18. #include "libbb.h"
  19. #include <assert.h>
  20. /**********************************************************************/
  21. /* Sizes for statically allocated buffers. */
  22. /* If you change these values, also change _SC_GETPW_R_SIZE_MAX and
  23. * _SC_GETGR_R_SIZE_MAX in libc/unistd/sysconf.c to match */
  24. #define PWD_BUFFER_SIZE 256
  25. #define GRP_BUFFER_SIZE 256
  26. /**********************************************************************/
  27. /* Prototypes for internal functions. */
  28. static int bb__pgsreader(
  29. int FAST_FUNC (*parserfunc)(void *d, char *line),
  30. void *data,
  31. char *__restrict line_buff,
  32. size_t buflen,
  33. FILE *f);
  34. static int FAST_FUNC bb__parsepwent(void *pw, char *line);
  35. static int FAST_FUNC bb__parsegrent(void *gr, char *line);
  36. #if ENABLE_USE_BB_SHADOW
  37. static int FAST_FUNC bb__parsespent(void *sp, char *line);
  38. #endif
  39. /**********************************************************************/
  40. /* We avoid having big global data. */
  41. struct statics {
  42. /* Smaller things first */
  43. struct passwd getpwuid_resultbuf;
  44. struct group getgrgid_resultbuf;
  45. struct passwd getpwnam_resultbuf;
  46. struct group getgrnam_resultbuf;
  47. char getpwuid_buffer[PWD_BUFFER_SIZE];
  48. char getgrgid_buffer[GRP_BUFFER_SIZE];
  49. char getpwnam_buffer[PWD_BUFFER_SIZE];
  50. char getgrnam_buffer[GRP_BUFFER_SIZE];
  51. #if 0
  52. struct passwd fgetpwent_resultbuf;
  53. struct group fgetgrent_resultbuf;
  54. struct spwd fgetspent_resultbuf;
  55. char fgetpwent_buffer[PWD_BUFFER_SIZE];
  56. char fgetgrent_buffer[GRP_BUFFER_SIZE];
  57. char fgetspent_buffer[PWD_BUFFER_SIZE];
  58. #endif
  59. #if 0 //ENABLE_USE_BB_SHADOW
  60. struct spwd getspuid_resultbuf;
  61. struct spwd getspnam_resultbuf;
  62. char getspuid_buffer[PWD_BUFFER_SIZE];
  63. char getspnam_buffer[PWD_BUFFER_SIZE];
  64. #endif
  65. // Not converted - too small to bother
  66. //pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER;
  67. //FILE *pwf /*= NULL*/;
  68. //FILE *grf /*= NULL*/;
  69. //FILE *spf /*= NULL*/;
  70. #if 0
  71. struct passwd getpwent_pwd;
  72. struct group getgrent_gr;
  73. char getpwent_line_buff[PWD_BUFFER_SIZE];
  74. char getgrent_line_buff[GRP_BUFFER_SIZE];
  75. #endif
  76. #if 0 //ENABLE_USE_BB_SHADOW
  77. struct spwd getspent_spwd;
  78. struct spwd sgetspent_spwd;
  79. char getspent_line_buff[PWD_BUFFER_SIZE];
  80. char sgetspent_line_buff[PWD_BUFFER_SIZE];
  81. #endif
  82. };
  83. static struct statics *ptr_to_statics;
  84. static struct statics *get_S(void)
  85. {
  86. if (!ptr_to_statics)
  87. ptr_to_statics = xzalloc(sizeof(*ptr_to_statics));
  88. return ptr_to_statics;
  89. }
  90. /* Always use in this order, get_S() must be called first */
  91. #define RESULTBUF(name) &((S = get_S())->name##_resultbuf)
  92. #define BUFFER(name) (S->name##_buffer)
  93. /**********************************************************************/
  94. /* For the various fget??ent_r funcs, return
  95. *
  96. * 0: success
  97. * ENOENT: end-of-file encountered
  98. * ERANGE: buflen too small
  99. * other error values possible. See bb__pgsreader.
  100. *
  101. * Also, *result == resultbuf on success and NULL on failure.
  102. *
  103. * NOTE: glibc difference - For the ENOENT case, glibc also sets errno.
  104. * We do not, as it really isn't an error if we reach the end-of-file.
  105. * Doing so is analogous to having fgetc() set errno on EOF.
  106. */
  107. /**********************************************************************/
  108. int fgetpwent_r(FILE *__restrict stream, struct passwd *__restrict resultbuf,
  109. char *__restrict buffer, size_t buflen,
  110. struct passwd **__restrict result)
  111. {
  112. int rv;
  113. *result = NULL;
  114. rv = bb__pgsreader(bb__parsepwent, resultbuf, buffer, buflen, stream);
  115. if (!rv) {
  116. *result = resultbuf;
  117. }
  118. return rv;
  119. }
  120. int fgetgrent_r(FILE *__restrict stream, struct group *__restrict resultbuf,
  121. char *__restrict buffer, size_t buflen,
  122. struct group **__restrict result)
  123. {
  124. int rv;
  125. *result = NULL;
  126. rv = bb__pgsreader(bb__parsegrent, resultbuf, buffer, buflen, stream);
  127. if (!rv) {
  128. *result = resultbuf;
  129. }
  130. return rv;
  131. }
  132. #if ENABLE_USE_BB_SHADOW
  133. #ifdef UNUSED_FOR_NOW
  134. int fgetspent_r(FILE *__restrict stream, struct spwd *__restrict resultbuf,
  135. char *__restrict buffer, size_t buflen,
  136. struct spwd **__restrict result)
  137. {
  138. int rv;
  139. *result = NULL;
  140. rv = bb__pgsreader(bb__parsespent, resultbuf, buffer, buflen, stream);
  141. if (!rv) {
  142. *result = resultbuf;
  143. }
  144. return rv;
  145. }
  146. #endif
  147. #endif
  148. /**********************************************************************/
  149. /* For the various fget??ent funcs, return NULL on failure and a
  150. * pointer to the appropriate struct (statically allocated) on success.
  151. * TODO: audit & stop using these in bbox, they pull in static buffers */
  152. /**********************************************************************/
  153. #ifdef UNUSED_SINCE_WE_AVOID_STATIC_BUFS
  154. struct passwd *fgetpwent(FILE *stream)
  155. {
  156. struct statics *S;
  157. struct passwd *resultbuf = RESULTBUF(fgetpwent);
  158. char *buffer = BUFFER(fgetpwent);
  159. struct passwd *result;
  160. fgetpwent_r(stream, resultbuf, buffer, sizeof(BUFFER(fgetpwent)), &result);
  161. return result;
  162. }
  163. struct group *fgetgrent(FILE *stream)
  164. {
  165. struct statics *S;
  166. struct group *resultbuf = RESULTBUF(fgetgrent);
  167. char *buffer = BUFFER(fgetgrent);
  168. struct group *result;
  169. fgetgrent_r(stream, resultbuf, buffer, sizeof(BUFFER(fgetgrent)), &result);
  170. return result;
  171. }
  172. #endif
  173. #if ENABLE_USE_BB_SHADOW
  174. #ifdef UNUSED_SINCE_WE_AVOID_STATIC_BUFS
  175. struct spwd *fgetspent(FILE *stream)
  176. {
  177. struct statics *S;
  178. struct spwd *resultbuf = RESULTBUF(fgetspent);
  179. char *buffer = BUFFER(fgetspent);
  180. struct spwd *result;
  181. fgetspent_r(stream, resultbuf, buffer, sizeof(BUFFER(fgetspent)), &result);
  182. return result;
  183. }
  184. #endif
  185. #ifdef UNUSED_FOR_NOW
  186. int sgetspent_r(const char *string, struct spwd *result_buf,
  187. char *buffer, size_t buflen, struct spwd **result)
  188. {
  189. int rv = ERANGE;
  190. *result = NULL;
  191. if (buflen < PWD_BUFFER_SIZE) {
  192. DO_ERANGE:
  193. errno = rv;
  194. goto DONE;
  195. }
  196. if (string != buffer) {
  197. if (strlen(string) >= buflen) {
  198. goto DO_ERANGE;
  199. }
  200. strcpy(buffer, string);
  201. }
  202. rv = bb__parsespent(result_buf, buffer);
  203. if (!rv) {
  204. *result = result_buf;
  205. }
  206. DONE:
  207. return rv;
  208. }
  209. #endif
  210. #endif /* ENABLE_USE_BB_SHADOW */
  211. /**********************************************************************/
  212. #define GETXXKEY_R_FUNC getpwnam_r
  213. #define GETXXKEY_R_PARSER bb__parsepwent
  214. #define GETXXKEY_R_ENTTYPE struct passwd
  215. #define GETXXKEY_R_TEST(ENT) (!strcmp((ENT)->pw_name, key))
  216. #define GETXXKEY_R_KEYTYPE const char *__restrict
  217. #define GETXXKEY_R_PATHNAME _PATH_PASSWD
  218. #include "pwd_grp_internal.c"
  219. #define GETXXKEY_R_FUNC getgrnam_r
  220. #define GETXXKEY_R_PARSER bb__parsegrent
  221. #define GETXXKEY_R_ENTTYPE struct group
  222. #define GETXXKEY_R_TEST(ENT) (!strcmp((ENT)->gr_name, key))
  223. #define GETXXKEY_R_KEYTYPE const char *__restrict
  224. #define GETXXKEY_R_PATHNAME _PATH_GROUP
  225. #include "pwd_grp_internal.c"
  226. #if ENABLE_USE_BB_SHADOW
  227. #define GETXXKEY_R_FUNC getspnam_r
  228. #define GETXXKEY_R_PARSER bb__parsespent
  229. #define GETXXKEY_R_ENTTYPE struct spwd
  230. #define GETXXKEY_R_TEST(ENT) (!strcmp((ENT)->sp_namp, key))
  231. #define GETXXKEY_R_KEYTYPE const char *__restrict
  232. #define GETXXKEY_R_PATHNAME _PATH_SHADOW
  233. #include "pwd_grp_internal.c"
  234. #endif
  235. #define GETXXKEY_R_FUNC getpwuid_r
  236. #define GETXXKEY_R_PARSER bb__parsepwent
  237. #define GETXXKEY_R_ENTTYPE struct passwd
  238. #define GETXXKEY_R_TEST(ENT) ((ENT)->pw_uid == key)
  239. #define GETXXKEY_R_KEYTYPE uid_t
  240. #define GETXXKEY_R_PATHNAME _PATH_PASSWD
  241. #include "pwd_grp_internal.c"
  242. #define GETXXKEY_R_FUNC getgrgid_r
  243. #define GETXXKEY_R_PARSER bb__parsegrent
  244. #define GETXXKEY_R_ENTTYPE struct group
  245. #define GETXXKEY_R_TEST(ENT) ((ENT)->gr_gid == key)
  246. #define GETXXKEY_R_KEYTYPE gid_t
  247. #define GETXXKEY_R_PATHNAME _PATH_GROUP
  248. #include "pwd_grp_internal.c"
  249. /**********************************************************************/
  250. /* TODO: audit & stop using these in bbox, they pull in static buffers */
  251. /* This one has many users */
  252. struct passwd *getpwuid(uid_t uid)
  253. {
  254. struct statics *S;
  255. struct passwd *resultbuf = RESULTBUF(getpwuid);
  256. char *buffer = BUFFER(getpwuid);
  257. struct passwd *result;
  258. getpwuid_r(uid, resultbuf, buffer, sizeof(BUFFER(getpwuid)), &result);
  259. return result;
  260. }
  261. /* This one has many users */
  262. struct group *getgrgid(gid_t gid)
  263. {
  264. struct statics *S;
  265. struct group *resultbuf = RESULTBUF(getgrgid);
  266. char *buffer = BUFFER(getgrgid);
  267. struct group *result;
  268. getgrgid_r(gid, resultbuf, buffer, sizeof(BUFFER(getgrgid)), &result);
  269. return result;
  270. }
  271. #if 0 //ENABLE_USE_BB_SHADOW
  272. /* This function is non-standard and is currently not built. It seems
  273. * to have been created as a reentrant version of the non-standard
  274. * functions getspuid. Why getspuid was added, I do not know. */
  275. int getspuid_r(uid_t uid, struct spwd *__restrict resultbuf,
  276. char *__restrict buffer, size_t buflen,
  277. struct spwd **__restrict result)
  278. {
  279. int rv;
  280. struct passwd *pp;
  281. struct passwd password;
  282. char pwd_buff[PWD_BUFFER_SIZE];
  283. *result = NULL;
  284. rv = getpwuid_r(uid, &password, pwd_buff, sizeof(pwd_buff), &pp);
  285. if (!rv) {
  286. rv = getspnam_r(password.pw_name, resultbuf, buffer, buflen, result);
  287. }
  288. return rv;
  289. }
  290. /* This function is non-standard and is currently not built.
  291. * Why it was added, I do not know. */
  292. struct spwd *getspuid(uid_t uid)
  293. {
  294. struct statics *S;
  295. struct spwd *resultbuf = RESULTBUF(getspuid);
  296. char *buffer = BUFFER(getspuid);
  297. struct spwd *result;
  298. getspuid_r(uid, resultbuf, buffer, sizeof(BUFFER(getspuid)), &result);
  299. return result;
  300. }
  301. #endif
  302. /* This one has many users */
  303. struct passwd *getpwnam(const char *name)
  304. {
  305. struct statics *S;
  306. struct passwd *resultbuf = RESULTBUF(getpwnam);
  307. char *buffer = BUFFER(getpwnam);
  308. struct passwd *result;
  309. getpwnam_r(name, resultbuf, buffer, sizeof(BUFFER(getpwnam)), &result);
  310. return result;
  311. }
  312. /* This one has many users */
  313. struct group *getgrnam(const char *name)
  314. {
  315. struct statics *S;
  316. struct group *resultbuf = RESULTBUF(getgrnam);
  317. char *buffer = BUFFER(getgrnam);
  318. struct group *result;
  319. getgrnam_r(name, resultbuf, buffer, sizeof(BUFFER(getgrnam)), &result);
  320. return result;
  321. }
  322. #if 0 //ENABLE_USE_BB_SHADOW
  323. struct spwd *getspnam(const char *name)
  324. {
  325. struct statics *S;
  326. struct spwd *resultbuf = RESULTBUF(getspnam);
  327. char *buffer = BUFFER(getspnam);
  328. struct spwd *result;
  329. getspnam_r(name, resultbuf, buffer, sizeof(BUFFER(getspnam)), &result);
  330. return result;
  331. }
  332. #endif
  333. /**********************************************************************/
  334. /* FIXME: we don't have such CONFIG_xx - ?! */
  335. #if defined CONFIG_USE_BB_THREADSAFE_SHADOW && defined PTHREAD_MUTEX_INITIALIZER
  336. static pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER;
  337. # define LOCK pthread_mutex_lock(&mylock)
  338. # define UNLOCK pthread_mutex_unlock(&mylock);
  339. #else
  340. # define LOCK ((void) 0)
  341. # define UNLOCK ((void) 0)
  342. #endif
  343. static FILE *pwf /*= NULL*/;
  344. void setpwent(void)
  345. {
  346. LOCK;
  347. if (pwf) {
  348. rewind(pwf);
  349. }
  350. UNLOCK;
  351. }
  352. void endpwent(void)
  353. {
  354. LOCK;
  355. if (pwf) {
  356. fclose(pwf);
  357. pwf = NULL;
  358. }
  359. UNLOCK;
  360. }
  361. int getpwent_r(struct passwd *__restrict resultbuf,
  362. char *__restrict buffer, size_t buflen,
  363. struct passwd **__restrict result)
  364. {
  365. int rv;
  366. LOCK;
  367. *result = NULL; /* In case of error... */
  368. if (!pwf) {
  369. pwf = fopen_for_read(_PATH_PASSWD);
  370. if (!pwf) {
  371. rv = errno;
  372. goto ERR;
  373. }
  374. }
  375. rv = bb__pgsreader(bb__parsepwent, resultbuf, buffer, buflen, pwf);
  376. if (!rv) {
  377. *result = resultbuf;
  378. }
  379. ERR:
  380. UNLOCK;
  381. return rv;
  382. }
  383. static FILE *grf /*= NULL*/;
  384. void setgrent(void)
  385. {
  386. LOCK;
  387. if (grf) {
  388. rewind(grf);
  389. }
  390. UNLOCK;
  391. }
  392. void endgrent(void)
  393. {
  394. LOCK;
  395. if (grf) {
  396. fclose(grf);
  397. grf = NULL;
  398. }
  399. UNLOCK;
  400. }
  401. int getgrent_r(struct group *__restrict resultbuf,
  402. char *__restrict buffer, size_t buflen,
  403. struct group **__restrict result)
  404. {
  405. int rv;
  406. LOCK;
  407. *result = NULL; /* In case of error... */
  408. if (!grf) {
  409. grf = fopen_for_read(_PATH_GROUP);
  410. if (!grf) {
  411. rv = errno;
  412. goto ERR;
  413. }
  414. }
  415. rv = bb__pgsreader(bb__parsegrent, resultbuf, buffer, buflen, grf);
  416. if (!rv) {
  417. *result = resultbuf;
  418. }
  419. ERR:
  420. UNLOCK;
  421. return rv;
  422. }
  423. #ifdef UNUSED_FOR_NOW
  424. #if ENABLE_USE_BB_SHADOW
  425. static FILE *spf /*= NULL*/;
  426. void setspent(void)
  427. {
  428. LOCK;
  429. if (spf) {
  430. rewind(spf);
  431. }
  432. UNLOCK;
  433. }
  434. void endspent(void)
  435. {
  436. LOCK;
  437. if (spf) {
  438. fclose(spf);
  439. spf = NULL;
  440. }
  441. UNLOCK;
  442. }
  443. int getspent_r(struct spwd *resultbuf, char *buffer,
  444. size_t buflen, struct spwd **result)
  445. {
  446. int rv;
  447. LOCK;
  448. *result = NULL; /* In case of error... */
  449. if (!spf) {
  450. spf = fopen_for_read(_PATH_SHADOW);
  451. if (!spf) {
  452. rv = errno;
  453. goto ERR;
  454. }
  455. }
  456. rv = bb__pgsreader(bb__parsespent, resultbuf, buffer, buflen, spf);
  457. if (!rv) {
  458. *result = resultbuf;
  459. }
  460. ERR:
  461. UNLOCK;
  462. return rv;
  463. }
  464. #endif
  465. #endif /* UNUSED_FOR_NOW */
  466. #ifdef UNUSED_SINCE_WE_AVOID_STATIC_BUFS
  467. struct passwd *getpwent(void)
  468. {
  469. static char line_buff[PWD_BUFFER_SIZE];
  470. static struct passwd pwd;
  471. struct passwd *result;
  472. getpwent_r(&pwd, line_buff, sizeof(line_buff), &result);
  473. return result;
  474. }
  475. struct group *getgrent(void)
  476. {
  477. static char line_buff[GRP_BUFFER_SIZE];
  478. static struct group gr;
  479. struct group *result;
  480. getgrent_r(&gr, line_buff, sizeof(line_buff), &result);
  481. return result;
  482. }
  483. #if ENABLE_USE_BB_SHADOW
  484. struct spwd *getspent(void)
  485. {
  486. static char line_buff[PWD_BUFFER_SIZE];
  487. static struct spwd spwd;
  488. struct spwd *result;
  489. getspent_r(&spwd, line_buff, sizeof(line_buff), &result);
  490. return result;
  491. }
  492. struct spwd *sgetspent(const char *string)
  493. {
  494. static char line_buff[PWD_BUFFER_SIZE];
  495. static struct spwd spwd;
  496. struct spwd *result;
  497. sgetspent_r(string, &spwd, line_buff, sizeof(line_buff), &result);
  498. return result;
  499. }
  500. #endif
  501. #endif /* UNUSED_SINCE_WE_AVOID_STATIC_BUFS */
  502. static gid_t *getgrouplist_internal(int *ngroups_ptr, const char *user, gid_t gid)
  503. {
  504. FILE *grfile;
  505. gid_t *group_list;
  506. int ngroups;
  507. struct group group;
  508. char buff[PWD_BUFFER_SIZE];
  509. /* We alloc space for 8 gids at a time. */
  510. group_list = xmalloc(8 * sizeof(group_list[0]));
  511. group_list[0] = gid;
  512. ngroups = 1;
  513. grfile = fopen_for_read(_PATH_GROUP);
  514. if (grfile) {
  515. while (!bb__pgsreader(bb__parsegrent, &group, buff, sizeof(buff), grfile)) {
  516. char **m;
  517. assert(group.gr_mem); /* Must have at least a NULL terminator. */
  518. if (group.gr_gid == gid)
  519. continue;
  520. for (m = group.gr_mem; *m; m++) {
  521. if (strcmp(*m, user) != 0)
  522. continue;
  523. group_list = xrealloc_vector(group_list, /*8=2^3:*/ 3, ngroups);
  524. group_list[ngroups++] = group.gr_gid;
  525. break;
  526. }
  527. }
  528. fclose(grfile);
  529. }
  530. *ngroups_ptr = ngroups;
  531. return group_list;
  532. }
  533. int initgroups(const char *user, gid_t gid)
  534. {
  535. int ngroups;
  536. gid_t *group_list = getgrouplist_internal(&ngroups, user, gid);
  537. ngroups = setgroups(ngroups, group_list);
  538. free(group_list);
  539. return ngroups;
  540. }
  541. int getgrouplist(const char *user, gid_t gid, gid_t *groups, int *ngroups)
  542. {
  543. int ngroups_old = *ngroups;
  544. gid_t *group_list = getgrouplist_internal(ngroups, user, gid);
  545. if (*ngroups <= ngroups_old) {
  546. ngroups_old = *ngroups;
  547. memcpy(groups, group_list, ngroups_old * sizeof(groups[0]));
  548. } else {
  549. ngroups_old = -1;
  550. }
  551. free(group_list);
  552. return ngroups_old;
  553. }
  554. #ifdef UNUSED_SINCE_WE_AVOID_STATIC_BUFS
  555. int putpwent(const struct passwd *__restrict p, FILE *__restrict f)
  556. {
  557. int rv = -1;
  558. #if 0
  559. /* glibc does this check */
  560. if (!p || !f) {
  561. errno = EINVAL;
  562. return rv;
  563. }
  564. #endif
  565. /* No extra thread locking is needed above what fprintf does. */
  566. if (fprintf(f, "%s:%s:%lu:%lu:%s:%s:%s\n",
  567. p->pw_name, p->pw_passwd,
  568. (unsigned long)(p->pw_uid),
  569. (unsigned long)(p->pw_gid),
  570. p->pw_gecos, p->pw_dir, p->pw_shell) >= 0
  571. ) {
  572. rv = 0;
  573. }
  574. return rv;
  575. }
  576. int putgrent(const struct group *__restrict p, FILE *__restrict f)
  577. {
  578. int rv = -1;
  579. #if 0
  580. /* glibc does this check */
  581. if (!p || !f) {
  582. errno = EINVAL;
  583. return rv;
  584. }
  585. #endif
  586. if (fprintf(f, "%s:%s:%lu:",
  587. p->gr_name, p->gr_passwd,
  588. (unsigned long)(p->gr_gid)) >= 0
  589. ) {
  590. static const char format[] ALIGN1 = ",%s";
  591. char **m;
  592. const char *fmt;
  593. fmt = format + 1;
  594. assert(p->gr_mem);
  595. m = p->gr_mem;
  596. while (1) {
  597. if (!*m) {
  598. if (fputc('\n', f) >= 0) {
  599. rv = 0;
  600. }
  601. break;
  602. }
  603. if (fprintf(f, fmt, *m) < 0) {
  604. break;
  605. }
  606. m++;
  607. fmt = format;
  608. }
  609. }
  610. return rv;
  611. }
  612. #endif
  613. #if ENABLE_USE_BB_SHADOW
  614. #ifdef UNUSED_FOR_NOW
  615. static const unsigned char put_sp_off[] ALIGN1 = {
  616. offsetof(struct spwd, sp_lstchg), /* 2 - not a char ptr */
  617. offsetof(struct spwd, sp_min), /* 3 - not a char ptr */
  618. offsetof(struct spwd, sp_max), /* 4 - not a char ptr */
  619. offsetof(struct spwd, sp_warn), /* 5 - not a char ptr */
  620. offsetof(struct spwd, sp_inact), /* 6 - not a char ptr */
  621. offsetof(struct spwd, sp_expire) /* 7 - not a char ptr */
  622. };
  623. int putspent(const struct spwd *p, FILE *stream)
  624. {
  625. const char *fmt;
  626. long x;
  627. int i;
  628. int rv = -1;
  629. /* Unlike putpwent and putgrent, glibc does not check the args. */
  630. if (fprintf(stream, "%s:%s:", p->sp_namp,
  631. (p->sp_pwdp ? p->sp_pwdp : "")) < 0
  632. ) {
  633. goto DO_UNLOCK;
  634. }
  635. for (i = 0; i < sizeof(put_sp_off); i++) {
  636. fmt = "%ld:";
  637. x = *(long *)((char *)p + put_sp_off[i]);
  638. if (x == -1) {
  639. fmt += 3;
  640. }
  641. if (fprintf(stream, fmt, x) < 0) {
  642. goto DO_UNLOCK;
  643. }
  644. }
  645. if ((p->sp_flag != ~0UL) && (fprintf(stream, "%lu", p->sp_flag) < 0)) {
  646. goto DO_UNLOCK;
  647. }
  648. if (fputc('\n', stream) > 0) {
  649. rv = 0;
  650. }
  651. DO_UNLOCK:
  652. return rv;
  653. }
  654. #endif
  655. #endif /* USE_BB_SHADOW */
  656. /**********************************************************************/
  657. /* Internal functions */
  658. /**********************************************************************/
  659. static const unsigned char pw_off[] ALIGN1 = {
  660. offsetof(struct passwd, pw_name), /* 0 */
  661. offsetof(struct passwd, pw_passwd), /* 1 */
  662. offsetof(struct passwd, pw_uid), /* 2 - not a char ptr */
  663. offsetof(struct passwd, pw_gid), /* 3 - not a char ptr */
  664. offsetof(struct passwd, pw_gecos), /* 4 */
  665. offsetof(struct passwd, pw_dir), /* 5 */
  666. offsetof(struct passwd, pw_shell) /* 6 */
  667. };
  668. static int FAST_FUNC bb__parsepwent(void *data, char *line)
  669. {
  670. char *endptr;
  671. char *p;
  672. int i;
  673. i = 0;
  674. while (1) {
  675. p = (char *) data + pw_off[i];
  676. if (i < 2 || i > 3) {
  677. *((char **) p) = line;
  678. if (i == 6) {
  679. return 0;
  680. }
  681. /* NOTE: glibc difference - glibc allows omission of
  682. * ':' seperators after the gid field if all remaining
  683. * entries are empty. We require all separators. */
  684. line = strchr(line, ':');
  685. if (!line) {
  686. break;
  687. }
  688. } else {
  689. unsigned long t = strtoul(line, &endptr, 10);
  690. /* Make sure we had at least one digit, and that the
  691. * failing char is the next field seperator ':'. See
  692. * glibc difference note above. */
  693. /* TODO: Also check for leading whitespace? */
  694. if ((endptr == line) || (*endptr != ':')) {
  695. break;
  696. }
  697. line = endptr;
  698. if (i & 1) { /* i == 3 -- gid */
  699. *((gid_t *) p) = t;
  700. } else { /* i == 2 -- uid */
  701. *((uid_t *) p) = t;
  702. }
  703. }
  704. *line++ = '\0';
  705. i++;
  706. } /* while (1) */
  707. return -1;
  708. }
  709. /**********************************************************************/
  710. static const unsigned char gr_off[] ALIGN1 = {
  711. offsetof(struct group, gr_name), /* 0 */
  712. offsetof(struct group, gr_passwd), /* 1 */
  713. offsetof(struct group, gr_gid) /* 2 - not a char ptr */
  714. };
  715. static int FAST_FUNC bb__parsegrent(void *data, char *line)
  716. {
  717. char *endptr;
  718. char *p;
  719. int i;
  720. char **members;
  721. char *end_of_buf;
  722. end_of_buf = ((struct group *) data)->gr_name; /* Evil hack! */
  723. i = 0;
  724. while (1) {
  725. p = (char *) data + gr_off[i];
  726. if (i < 2) {
  727. *((char **) p) = line;
  728. line = strchr(line, ':');
  729. if (!line) {
  730. break;
  731. }
  732. *line++ = '\0';
  733. i++;
  734. } else {
  735. *((gid_t *) p) = strtoul(line, &endptr, 10);
  736. /* NOTE: glibc difference - glibc allows omission of the
  737. * trailing colon when there is no member list. We treat
  738. * this as an error. */
  739. /* Make sure we had at least one digit, and that the
  740. * failing char is the next field seperator ':'. See
  741. * glibc difference note above. */
  742. if ((endptr == line) || (*endptr != ':')) {
  743. break;
  744. }
  745. i = 1; /* Count terminating NULL ptr. */
  746. p = endptr;
  747. if (p[1]) { /* We have a member list to process. */
  748. /* Overwrite the last ':' with a ',' before counting.
  749. * This allows us to (1) test for initial ','
  750. * and (2) adds one ',' so that the number of commas
  751. * equals the member count. */
  752. *p = ',';
  753. do {
  754. /* NOTE: glibc difference - glibc allows and trims leading
  755. * (but not trailing) space. We treat this as an error. */
  756. /* NOTE: glibc difference - glibc allows consecutive and
  757. * trailing commas, and ignores "empty string" users. We
  758. * treat this as an error. */
  759. if (*p == ',') {
  760. ++i;
  761. *p = 0; /* nul-terminate each member string. */
  762. if (!*++p || (*p == ',') || isspace(*p)) {
  763. goto ERR;
  764. }
  765. }
  766. } while (*++p);
  767. }
  768. /* Now align (p+1), rounding up. */
  769. /* Assumes sizeof(char **) is a power of 2. */
  770. members = (char **)( (((intptr_t) p) + sizeof(char **))
  771. & ~((intptr_t)(sizeof(char **) - 1)) );
  772. if (((char *)(members + i)) > end_of_buf) { /* No space. */
  773. break;
  774. }
  775. ((struct group *) data)->gr_mem = members;
  776. if (--i) {
  777. p = endptr; /* Pointing to char prior to first member. */
  778. while (1) {
  779. *members++ = ++p;
  780. if (!--i)
  781. break;
  782. while (*++p)
  783. continue;
  784. }
  785. }
  786. *members = NULL;
  787. return 0;
  788. }
  789. } /* while (1) */
  790. ERR:
  791. return -1;
  792. }
  793. /**********************************************************************/
  794. #if ENABLE_USE_BB_SHADOW
  795. static const unsigned char sp_off[] ALIGN1 = {
  796. offsetof(struct spwd, sp_namp), /* 0: char* */
  797. offsetof(struct spwd, sp_pwdp), /* 1: char* */
  798. offsetof(struct spwd, sp_lstchg), /* 2: long */
  799. offsetof(struct spwd, sp_min), /* 3: long */
  800. offsetof(struct spwd, sp_max), /* 4: long */
  801. offsetof(struct spwd, sp_warn), /* 5: long */
  802. offsetof(struct spwd, sp_inact), /* 6: long */
  803. offsetof(struct spwd, sp_expire), /* 7: long */
  804. offsetof(struct spwd, sp_flag) /* 8: unsigned long */
  805. };
  806. static int FAST_FUNC bb__parsespent(void *data, char *line)
  807. {
  808. char *endptr;
  809. char *p;
  810. int i;
  811. i = 0;
  812. while (1) {
  813. p = (char *) data + sp_off[i];
  814. if (i < 2) {
  815. *((char **) p) = line;
  816. line = strchr(line, ':');
  817. if (!line) {
  818. break; /* error */
  819. }
  820. } else {
  821. *((long *) p) = strtoul(line, &endptr, 10);
  822. if (endptr == line) {
  823. *((long *) p) = -1L;
  824. }
  825. line = endptr;
  826. if (i == 8) {
  827. if (*line != '\0') {
  828. break; /* error */
  829. }
  830. return 0; /* all ok */
  831. }
  832. if (*line != ':') {
  833. break; /* error */
  834. }
  835. }
  836. *line++ = '\0';
  837. i++;
  838. }
  839. return EINVAL;
  840. }
  841. #endif
  842. /**********************************************************************/
  843. /* Reads until EOF, or until it finds a line which fits in the buffer
  844. * and for which the parser function succeeds.
  845. *
  846. * Returns 0 on success and ENOENT for end-of-file (glibc convention).
  847. */
  848. static int bb__pgsreader(
  849. int FAST_FUNC (*parserfunc)(void *d, char *line),
  850. void *data,
  851. char *__restrict line_buff,
  852. size_t buflen,
  853. FILE *f)
  854. {
  855. int skip;
  856. int rv = ERANGE;
  857. if (buflen < PWD_BUFFER_SIZE) {
  858. errno = rv;
  859. return rv;
  860. }
  861. skip = 0;
  862. while (1) {
  863. if (!fgets(line_buff, buflen, f)) {
  864. if (feof(f)) {
  865. rv = ENOENT;
  866. }
  867. break;
  868. }
  869. {
  870. int line_len = strlen(line_buff) - 1;
  871. if (line_len >= 0 && line_buff[line_len] == '\n') {
  872. line_buff[line_len] = '\0';
  873. } else
  874. if (line_len + 2 == buflen) {
  875. /* A start (or continuation) of overlong line */
  876. skip = 1;
  877. continue;
  878. } /* else: a last line in the file, and it has no '\n' */
  879. }
  880. if (skip) {
  881. /* This "line" is a remainder of overlong line, ignore */
  882. skip = 0;
  883. continue;
  884. }
  885. /* NOTE: glibc difference - glibc strips leading whitespace from
  886. * records. We do not allow leading whitespace. */
  887. /* Skip empty lines, comment lines, and lines with leading
  888. * whitespace. */
  889. if (line_buff[0] != '\0' && line_buff[0] != '#' && !isspace(line_buff[0])) {
  890. if (parserfunc == bb__parsegrent) {
  891. /* Do evil group hack:
  892. * The group entry parsing function needs to know where
  893. * the end of the buffer is so that it can construct the
  894. * group member ptr table. */
  895. ((struct group *) data)->gr_name = line_buff + buflen;
  896. }
  897. if (parserfunc(data, line_buff) == 0) {
  898. rv = 0;
  899. break;
  900. }
  901. }
  902. } /* while (1) */
  903. return rv;
  904. }