pwd_grp.c 27 KB

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