pwd_grp.c 26 KB

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