pwd_grp.c 27 KB

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