pwd_grp.c 27 KB

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