pwd_grp.c 25 KB

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