pwd_grp.c 27 KB

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