pwd_grp.c 28 KB

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