pwd_grp.c 27 KB

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