pwd_grp.c 26 KB

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