pwd_grp.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123
  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);
  47. extern int __parsegrent(void *gr, char *line);
  48. extern int __parsespent(void *sp, char *line);
  49. extern int __pgsreader(int (*__parserfunc)(void *d, char *line), void *data,
  50. char *__restrict line_buff, size_t buflen, FILE *f);
  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. static pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER;
  333. # define LOCK __pthread_mutex_lock(&mylock)
  334. # define UNLOCK __pthread_mutex_unlock(&mylock);
  335. #else
  336. # define LOCK ((void) 0)
  337. # define UNLOCK ((void) 0)
  338. #endif
  339. #endif
  340. #ifdef L_getpwent_r
  341. static FILE *pwf /*= NULL*/;
  342. void setpwent(void)
  343. {
  344. LOCK;
  345. if (pwf) {
  346. rewind(pwf);
  347. }
  348. UNLOCK;
  349. }
  350. void endpwent(void)
  351. {
  352. LOCK;
  353. if (pwf) {
  354. fclose(pwf);
  355. pwf = NULL;
  356. }
  357. UNLOCK;
  358. }
  359. int getpwent_r(struct passwd *__restrict resultbuf,
  360. char *__restrict buffer, size_t buflen,
  361. struct passwd **__restrict result)
  362. {
  363. int rv;
  364. LOCK;
  365. *result = NULL; /* In case of error... */
  366. if (!pwf) {
  367. if (!(pwf = fopen(_PATH_PASSWD, "r"))) {
  368. rv = errno;
  369. goto ERR;
  370. }
  371. __STDIO_SET_USER_LOCKING(pwf);
  372. }
  373. if (!(rv = __pgsreader(__parsepwent, resultbuf,
  374. buffer, buflen, pwf))) {
  375. *result = resultbuf;
  376. }
  377. ERR:
  378. UNLOCK;
  379. return rv;
  380. }
  381. #endif
  382. /**********************************************************************/
  383. #ifdef L_getgrent_r
  384. static FILE *grf /*= NULL*/;
  385. void setgrent(void)
  386. {
  387. LOCK;
  388. if (grf) {
  389. rewind(grf);
  390. }
  391. UNLOCK;
  392. }
  393. void endgrent(void)
  394. {
  395. LOCK;
  396. if (grf) {
  397. fclose(grf);
  398. grf = NULL;
  399. }
  400. UNLOCK;
  401. }
  402. int getgrent_r(struct group *__restrict resultbuf,
  403. char *__restrict buffer, size_t buflen,
  404. struct group **__restrict result)
  405. {
  406. int rv;
  407. LOCK;
  408. *result = NULL; /* In case of error... */
  409. if (!grf) {
  410. if (!(grf = fopen(_PATH_GROUP, "r"))) {
  411. rv = errno;
  412. goto ERR;
  413. }
  414. __STDIO_SET_USER_LOCKING(grf);
  415. }
  416. if (!(rv = __pgsreader(__parsegrent, resultbuf,
  417. buffer, buflen, grf))) {
  418. *result = resultbuf;
  419. }
  420. ERR:
  421. UNLOCK;
  422. return rv;
  423. }
  424. #endif
  425. /**********************************************************************/
  426. #ifdef L_getspent_r
  427. static FILE *spf /*= NULL*/;
  428. void setspent(void)
  429. {
  430. LOCK;
  431. if (spf) {
  432. rewind(spf);
  433. }
  434. UNLOCK;
  435. }
  436. void endspent(void)
  437. {
  438. LOCK;
  439. if (spf) {
  440. fclose(spf);
  441. spf = NULL;
  442. }
  443. UNLOCK;
  444. }
  445. int getspent_r(struct spwd *resultbuf, char *buffer,
  446. size_t buflen, struct spwd **result)
  447. {
  448. int rv;
  449. LOCK;
  450. *result = NULL; /* In case of error... */
  451. if (!spf) {
  452. if (!(spf = fopen(_PATH_SHADOW, "r"))) {
  453. rv = errno;
  454. goto ERR;
  455. }
  456. __STDIO_SET_USER_LOCKING(spf);
  457. }
  458. if (!(rv = __pgsreader(__parsespent, resultbuf,
  459. buffer, buflen, spf))) {
  460. *result = resultbuf;
  461. }
  462. ERR:
  463. UNLOCK;
  464. return rv;
  465. }
  466. #endif
  467. /**********************************************************************/
  468. #ifdef L_getpwent
  469. struct passwd *getpwent(void)
  470. {
  471. static char line_buff[PWD_BUFFER_SIZE];
  472. static struct passwd pwd;
  473. struct passwd *result;
  474. getpwent_r(&pwd, line_buff, sizeof(line_buff), &result);
  475. return result;
  476. }
  477. #endif
  478. /**********************************************************************/
  479. #ifdef L_getgrent
  480. struct group *getgrent(void)
  481. {
  482. static char line_buff[GRP_BUFFER_SIZE];
  483. static struct group gr;
  484. struct group *result;
  485. getgrent_r(&gr, line_buff, sizeof(line_buff), &result);
  486. return result;
  487. }
  488. #endif
  489. /**********************************************************************/
  490. #ifdef L_getspent
  491. struct spwd *getspent(void)
  492. {
  493. static char line_buff[PWD_BUFFER_SIZE];
  494. static struct spwd spwd;
  495. struct spwd *result;
  496. getspent_r(&spwd, line_buff, sizeof(line_buff), &result);
  497. return result;
  498. }
  499. #endif
  500. /**********************************************************************/
  501. #ifdef L_sgetspent
  502. struct spwd *sgetspent(const char *string)
  503. {
  504. static char line_buff[PWD_BUFFER_SIZE];
  505. static struct spwd spwd;
  506. struct spwd *result;
  507. sgetspent_r(string, &spwd, line_buff, sizeof(line_buff), &result);
  508. return result;
  509. }
  510. #endif
  511. /**********************************************************************/
  512. #ifdef L_initgroups
  513. int initgroups(const char *user, gid_t gid)
  514. {
  515. FILE *grfile;
  516. gid_t *group_list;
  517. int num_groups, rv;
  518. char **m;
  519. struct group group;
  520. char buff[PWD_BUFFER_SIZE];
  521. rv = -1;
  522. /* We alloc space for 8 gids at a time. */
  523. if (((group_list = (gid_t *) malloc(8*sizeof(gid_t *))) != NULL)
  524. && ((grfile = fopen(_PATH_GROUP, "r")) != NULL)
  525. ) {
  526. __STDIO_SET_USER_LOCKING(grfile);
  527. *group_list = gid;
  528. num_groups = 1;
  529. while (!__pgsreader(__parsegrent, &group, buff, sizeof(buff), grfile)) {
  530. assert(group.gr_mem); /* Must have at least a NULL terminator. */
  531. if (group.gr_gid != gid) {
  532. for (m=group.gr_mem ; *m ; m++) {
  533. if (!strcmp(*m, user)) {
  534. if (!(num_groups & 7)) {
  535. gid_t *tmp = (gid_t *)
  536. realloc(group_list,
  537. (num_groups+8) * sizeof(gid_t *));
  538. if (!tmp) {
  539. rv = -1;
  540. goto DO_CLOSE;
  541. }
  542. group_list = tmp;
  543. }
  544. group_list[num_groups++] = group.gr_gid;
  545. break;
  546. }
  547. }
  548. }
  549. }
  550. rv = setgroups(num_groups, group_list);
  551. DO_CLOSE:
  552. fclose(grfile);
  553. }
  554. /* group_list will be NULL if initial malloc failed, which may trigger
  555. * warnings from various malloc debuggers. */
  556. free(group_list);
  557. return rv;
  558. }
  559. #endif
  560. /**********************************************************************/
  561. #ifdef L_putpwent
  562. int putpwent(const struct passwd *__restrict p, FILE *__restrict f)
  563. {
  564. int rv = -1;
  565. if (!p || !f) {
  566. __set_errno(EINVAL);
  567. } else {
  568. /* No extra thread locking is needed above what fprintf does. */
  569. if (fprintf(f, "%s:%s:%lu:%lu:%s:%s:%s\n",
  570. p->pw_name, p->pw_passwd,
  571. (unsigned long)(p->pw_uid),
  572. (unsigned long)(p->pw_gid),
  573. p->pw_gecos, p->pw_dir, p->pw_shell) >= 0
  574. ) {
  575. rv = 0;
  576. }
  577. }
  578. return rv;
  579. }
  580. #endif
  581. /**********************************************************************/
  582. #ifdef L_putgrent
  583. int putgrent(const struct group *__restrict p, FILE *__restrict f)
  584. {
  585. static const char format[] = ",%s";
  586. char **m;
  587. const char *fmt;
  588. int rv = -1;
  589. __STDIO_AUTO_THREADLOCK_VAR;
  590. if (!p || !f) { /* Sigh... glibc checks. */
  591. __set_errno(EINVAL);
  592. } else {
  593. __STDIO_AUTO_THREADLOCK(f);
  594. if (fprintf(f, "%s:%s:%lu:",
  595. p->gr_name, p->gr_passwd,
  596. (unsigned long)(p->gr_gid)) >= 0
  597. ) {
  598. fmt = format + 1;
  599. assert(p->gr_mem);
  600. m = p->gr_mem;
  601. do {
  602. if (!*m) {
  603. if (fputc_unlocked('\n', f) >= 0) {
  604. rv = 0;
  605. }
  606. break;
  607. }
  608. if (fprintf(f, fmt, *m) < 0) {
  609. break;
  610. }
  611. ++m;
  612. fmt = format;
  613. } while (1);
  614. }
  615. __STDIO_AUTO_THREADUNLOCK(f);
  616. }
  617. return rv;
  618. }
  619. #endif
  620. /**********************************************************************/
  621. #ifdef L_putspent
  622. static const unsigned char _sp_off[] = {
  623. offsetof(struct spwd, sp_lstchg), /* 2 - not a char ptr */
  624. offsetof(struct spwd, sp_min), /* 3 - not a char ptr */
  625. offsetof(struct spwd, sp_max), /* 4 - not a char ptr */
  626. offsetof(struct spwd, sp_warn), /* 5 - not a char ptr */
  627. offsetof(struct spwd, sp_inact), /* 6 - not a char ptr */
  628. offsetof(struct spwd, sp_expire), /* 7 - not a char ptr */
  629. };
  630. int putspent(const struct spwd *p, FILE *stream)
  631. {
  632. static const char ld_format[] = "%ld:";
  633. const char *f;
  634. long int x;
  635. int i;
  636. int rv = -1;
  637. __STDIO_AUTO_THREADLOCK_VAR;
  638. /* Unlike putpwent and putgrent, glibc does not check the args. */
  639. __STDIO_AUTO_THREADLOCK(stream);
  640. if (fprintf(stream, "%s:%s:", p->sp_namp,
  641. (p->sp_pwdp ? p->sp_pwdp : "")) < 0
  642. ) {
  643. goto DO_UNLOCK;
  644. }
  645. for (i=0 ; i < sizeof(_sp_off) ; i++) {
  646. f = ld_format;
  647. if ((x = *(const long int *)(((const char *) p) + _sp_off[i])) == -1) {
  648. f += 3;
  649. }
  650. if (fprintf(stream, f, x) < 0) {
  651. goto DO_UNLOCK;
  652. }
  653. }
  654. if ((p->sp_flag != ~0UL) && (fprintf(stream, "%lu", p->sp_flag) < 0)) {
  655. goto DO_UNLOCK;
  656. }
  657. if (fputc_unlocked('\n', stream) > 0) {
  658. rv = 0;
  659. }
  660. DO_UNLOCK:
  661. __STDIO_AUTO_THREADUNLOCK(stream);
  662. return rv;
  663. }
  664. #endif
  665. /**********************************************************************/
  666. /* Internal uClibc functions. */
  667. /**********************************************************************/
  668. #ifdef L___parsepwent
  669. static const unsigned char pw_off[] = {
  670. offsetof(struct passwd, pw_name), /* 0 */
  671. offsetof(struct passwd, pw_passwd), /* 1 */
  672. offsetof(struct passwd, pw_uid), /* 2 - not a char ptr */
  673. offsetof(struct passwd, pw_gid), /* 3 - not a char ptr */
  674. offsetof(struct passwd, pw_gecos), /* 4 */
  675. offsetof(struct passwd, pw_dir), /* 5 */
  676. offsetof(struct passwd, pw_shell) /* 6 */
  677. };
  678. int __parsepwent(void *data, char *line)
  679. {
  680. char *endptr;
  681. char *p;
  682. int i;
  683. i = 0;
  684. do {
  685. p = ((char *) ((struct passwd *) data)) + pw_off[i];
  686. if ((i & 6) ^ 2) { /* i!=2 and i!=3 */
  687. *((char **) p) = line;
  688. if (i==6) {
  689. return 0;
  690. }
  691. /* NOTE: glibc difference - glibc allows omission of
  692. * ':' seperators after the gid field if all remaining
  693. * entries are empty. We require all separators. */
  694. if (!(line = strchr(line, ':'))) {
  695. break;
  696. }
  697. } else {
  698. unsigned long t = strtoul(line, &endptr, 10);
  699. /* Make sure we had at least one digit, and that the
  700. * failing char is the next field seperator ':'. See
  701. * glibc difference note above. */
  702. /* TODO: Also check for leading whitespace? */
  703. if ((endptr == line) || (*endptr != ':')) {
  704. break;
  705. }
  706. line = endptr;
  707. if (i & 1) { /* i == 3 -- gid */
  708. *((gid_t *) p) = t;
  709. } else { /* i == 2 -- uid */
  710. *((uid_t *) p) = t;
  711. }
  712. }
  713. *line++ = 0;
  714. ++i;
  715. } while (1);
  716. return -1;
  717. }
  718. #endif
  719. /**********************************************************************/
  720. #ifdef L___parsegrent
  721. static const unsigned char gr_off[] = {
  722. offsetof(struct group, gr_name), /* 0 */
  723. offsetof(struct group, gr_passwd), /* 1 */
  724. offsetof(struct group, gr_gid) /* 2 - not a char ptr */
  725. };
  726. int __parsegrent(void *data, char *line)
  727. {
  728. char *endptr;
  729. char *p;
  730. int i;
  731. char **members;
  732. char *end_of_buf;
  733. end_of_buf = ((struct group *) data)->gr_name; /* Evil hack! */
  734. i = 0;
  735. do {
  736. p = ((char *) ((struct group *) data)) + gr_off[i];
  737. if (i < 2) {
  738. *((char **) p) = line;
  739. if (!(line = strchr(line, ':'))) {
  740. break;
  741. }
  742. *line++ = 0;
  743. ++i;
  744. } else {
  745. *((gid_t *) p) = strtoul(line, &endptr, 10);
  746. /* NOTE: glibc difference - glibc allows omission of the
  747. * trailing colon when there is no member list. We treat
  748. * this as an error. */
  749. /* Make sure we had at least one digit, and that the
  750. * failing char is the next field seperator ':'. See
  751. * glibc difference note above. */
  752. if ((endptr == line) || (*endptr != ':')) {
  753. break;
  754. }
  755. i = 1; /* Count terminating NULL ptr. */
  756. p = endptr;
  757. if (p[1]) { /* We have a member list to process. */
  758. /* Overwrite the last ':' with a ',' before counting.
  759. * This allows us to test for initial ',' and adds
  760. * one ',' so that the ',' count equals the member
  761. * count. */
  762. *p = ',';
  763. do {
  764. /* NOTE: glibc difference - glibc allows and trims leading
  765. * (but not trailing) space. We treat this as an error. */
  766. /* NOTE: glibc difference - glibc allows consecutive and
  767. * trailing commas, and ignores "empty string" users. We
  768. * treat this as an error. */
  769. if (*p == ',') {
  770. ++i;
  771. *p = 0; /* nul-terminate each member string. */
  772. if (!*++p || (*p == ',') || isspace(*p)) {
  773. goto ERR;
  774. }
  775. }
  776. } while (*++p);
  777. }
  778. /* Now align (p+1), rounding up. */
  779. /* Assumes sizeof(char **) is a power of 2. */
  780. members = (char **)( (((intptr_t) p) + sizeof(char **))
  781. & ~((intptr_t)(sizeof(char **) - 1)) );
  782. if (((char *)(members + i)) > end_of_buf) { /* No space. */
  783. break;
  784. }
  785. ((struct group *) data)->gr_mem = members;
  786. if (--i) {
  787. p = endptr; /* Pointing to char prior to first member. */
  788. do {
  789. *members++ = ++p;
  790. if (!--i) break;
  791. while (*++p) {}
  792. } while (1);
  793. }
  794. *members = NULL;
  795. return 0;
  796. }
  797. } while (1);
  798. ERR:
  799. return -1;
  800. }
  801. #endif
  802. /**********************************************************************/
  803. #ifdef L___parsespent
  804. static const unsigned char sp_off[] = {
  805. offsetof(struct spwd, sp_namp), /* 0 */
  806. offsetof(struct spwd, sp_pwdp), /* 1 */
  807. offsetof(struct spwd, sp_lstchg), /* 2 - not a char ptr */
  808. offsetof(struct spwd, sp_min), /* 3 - not a char ptr */
  809. offsetof(struct spwd, sp_max), /* 4 - not a char ptr */
  810. offsetof(struct spwd, sp_warn), /* 5 - not a char ptr */
  811. offsetof(struct spwd, sp_inact), /* 6 - not a char ptr */
  812. offsetof(struct spwd, sp_expire), /* 7 - not a char ptr */
  813. offsetof(struct spwd, sp_flag) /* 8 - not a char ptr */
  814. };
  815. int __parsespent(void *data, char * line)
  816. {
  817. char *endptr;
  818. char *p;
  819. int i;
  820. i = 0;
  821. do {
  822. p = ((char *) ((struct spwd *) data)) + sp_off[i];
  823. if (i < 2) {
  824. *((char **) p) = line;
  825. if (!(line = strchr(line, ':'))) {
  826. break;
  827. }
  828. } else {
  829. #if 0
  830. if (i==5) { /* Support for old format. */
  831. while (isspace(*line)) ++line; /* glibc eats space here. */
  832. if (!*line) {
  833. ((struct spwd *) data)->sp_warn = -1;
  834. ((struct spwd *) data)->sp_inact = -1;
  835. ((struct spwd *) data)->sp_expire = -1;
  836. ((struct spwd *) data)->sp_flag = ~0UL;
  837. return 0;
  838. }
  839. }
  840. #endif
  841. *((long *) p) = (long) strtoul(line, &endptr, 10);
  842. if (endptr == line) {
  843. *((long *) p) = ((i != 8) ? -1L : ((long)(~0UL)));
  844. }
  845. line = endptr;
  846. if (i == 8) {
  847. if (!*endptr) {
  848. return 0;
  849. }
  850. break;
  851. }
  852. if (*endptr != ':') {
  853. break;
  854. }
  855. }
  856. *line++ = 0;
  857. ++i;
  858. } while (1);
  859. return EINVAL;
  860. }
  861. #endif
  862. /**********************************************************************/
  863. #ifdef L___pgsreader
  864. /* Reads until if EOF, or until if finds a line which fits in the buffer
  865. * and for which the parser function succeeds.
  866. *
  867. * Returns 0 on success and ENOENT for end-of-file (glibc concession).
  868. */
  869. int __pgsreader(int (*__parserfunc)(void *d, char *line), void *data,
  870. char *__restrict line_buff, size_t buflen, FILE *f)
  871. {
  872. int line_len;
  873. int skip;
  874. int rv = ERANGE;
  875. __STDIO_AUTO_THREADLOCK_VAR;
  876. if (buflen < PWD_BUFFER_SIZE) {
  877. __set_errno(rv);
  878. } else {
  879. __STDIO_AUTO_THREADLOCK(f);
  880. skip = 0;
  881. do {
  882. if (!fgets_unlocked(line_buff, buflen, f)) {
  883. if (feof_unlocked(f)) {
  884. rv = ENOENT;
  885. }
  886. break;
  887. }
  888. line_len = strlen(line_buff) - 1; /* strlen() must be > 0. */
  889. if (line_buff[line_len] == '\n') {
  890. line_buff[line_len] = 0;
  891. } else if (line_len + 2 == buflen) { /* line too long */
  892. ++skip;
  893. continue;
  894. }
  895. if (skip) {
  896. --skip;
  897. continue;
  898. }
  899. /* NOTE: glibc difference - glibc strips leading whitespace from
  900. * records. We do not allow leading whitespace. */
  901. /* Skip empty lines, comment lines, and lines with leading
  902. * whitespace. */
  903. if (*line_buff && (*line_buff != '#') && !isspace(*line_buff)) {
  904. if (__parserfunc == __parsegrent) { /* Do evil group hack. */
  905. /* The group entry parsing function needs to know where
  906. * the end of the buffer is so that it can construct the
  907. * group member ptr table. */
  908. ((struct group *) data)->gr_name = line_buff + buflen;
  909. }
  910. if (!__parserfunc(data, line_buff)) {
  911. rv = 0;
  912. break;
  913. }
  914. }
  915. } while (1);
  916. __STDIO_AUTO_THREADUNLOCK(f);
  917. }
  918. return rv;
  919. }
  920. #endif
  921. /**********************************************************************/