pwd_grp.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191
  1. /*
  2. * Copyright (C) 2003 Manuel Novoa III <mjn3@uclibc.org>
  3. * Copyright (C) 2000-2006 Erik Andersen <andersen@uclibc.org>
  4. *
  5. * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
  6. */
  7. /* Nov 6, 2003 Initial version.
  8. *
  9. * NOTE: This implementation is quite strict about requiring all
  10. * field seperators. It also does not allow leading whitespace
  11. * except when processing the numeric fields. glibc is more
  12. * lenient. See the various glibc difference comments below.
  13. *
  14. * TODO:
  15. * Move to dynamic allocation of (currently statically allocated)
  16. * buffers; especially for the group-related functions since
  17. * large group member lists will cause error returns.
  18. *
  19. */
  20. #include <features.h>
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <stdint.h>
  24. #include <string.h>
  25. #include <stddef.h>
  26. #include <errno.h>
  27. #include <assert.h>
  28. #include <ctype.h>
  29. #include <pwd.h>
  30. #include <grp.h>
  31. #include <paths.h>
  32. #ifdef __UCLIBC_HAS_SHADOW__
  33. #include <shadow.h>
  34. #endif
  35. #include <bits/uClibc_mutex.h>
  36. libc_hidden_proto(strchr)
  37. libc_hidden_proto(strcmp)
  38. libc_hidden_proto(strcpy)
  39. libc_hidden_proto(strlen)
  40. libc_hidden_proto(strtoul)
  41. libc_hidden_proto(rewind)
  42. libc_hidden_proto(fgets_unlocked)
  43. libc_hidden_proto(__fputc_unlocked)
  44. libc_hidden_proto(sprintf)
  45. libc_hidden_proto(fopen)
  46. libc_hidden_proto(fclose)
  47. libc_hidden_proto(fprintf)
  48. #ifdef __UCLIBC_HAS_XLOCALE__
  49. libc_hidden_proto(__ctype_b_loc)
  50. #elif __UCLIBC_HAS_CTYPE_TABLES__
  51. libc_hidden_proto(__ctype_b)
  52. #endif
  53. /**********************************************************************/
  54. /* Sizes for statically allocated buffers. */
  55. /* If you change these values, also change _SC_GETPW_R_SIZE_MAX and
  56. * _SC_GETGR_R_SIZE_MAX in libc/unistd/sysconf.c to match */
  57. #define PWD_BUFFER_SIZE 256
  58. #define GRP_BUFFER_SIZE 256
  59. /**********************************************************************/
  60. /* Prototypes for internal functions. */
  61. extern int __parsepwent(void *pw, char *line) attribute_hidden;
  62. extern int __parsegrent(void *gr, char *line) attribute_hidden;
  63. extern int __parsespent(void *sp, char *line) attribute_hidden;
  64. extern int __pgsreader(int (*__parserfunc)(void *d, char *line), void *data,
  65. char *__restrict line_buff, size_t buflen, FILE *f) attribute_hidden;
  66. /**********************************************************************/
  67. /* For the various fget??ent_r funcs, return
  68. *
  69. * 0: success
  70. * ENOENT: end-of-file encountered
  71. * ERANGE: buflen too small
  72. * other error values possible. See __pgsreader.
  73. *
  74. * Also, *result == resultbuf on success and NULL on failure.
  75. *
  76. * NOTE: glibc difference - For the ENOENT case, glibc also sets errno.
  77. * We do not, as it really isn't an error if we reach the end-of-file.
  78. * Doing so is analogous to having fgetc() set errno on EOF.
  79. */
  80. /**********************************************************************/
  81. #ifdef L_fgetpwent_r
  82. #ifdef __USE_SVID
  83. libc_hidden_proto(fgetpwent_r)
  84. int fgetpwent_r(FILE *__restrict stream, struct passwd *__restrict resultbuf,
  85. char *__restrict buffer, size_t buflen,
  86. struct passwd **__restrict result)
  87. {
  88. int rv;
  89. *result = NULL;
  90. if (!(rv = __pgsreader(__parsepwent, resultbuf, buffer, buflen, stream))) {
  91. *result = resultbuf;
  92. }
  93. return rv;
  94. }
  95. libc_hidden_def(fgetpwent_r)
  96. #endif
  97. #endif
  98. /**********************************************************************/
  99. #ifdef L_fgetgrent_r
  100. #ifdef __USE_SVID
  101. libc_hidden_proto(fgetgrent_r)
  102. int fgetgrent_r(FILE *__restrict stream, struct group *__restrict resultbuf,
  103. char *__restrict buffer, size_t buflen,
  104. struct group **__restrict result)
  105. {
  106. int rv;
  107. *result = NULL;
  108. if (!(rv = __pgsreader(__parsegrent, resultbuf, buffer, buflen, stream))) {
  109. *result = resultbuf;
  110. }
  111. return rv;
  112. }
  113. libc_hidden_def(fgetgrent_r)
  114. #endif
  115. #endif
  116. /**********************************************************************/
  117. #ifdef L_fgetspent_r
  118. libc_hidden_proto(fgetspent_r)
  119. int fgetspent_r(FILE *__restrict stream, struct spwd *__restrict resultbuf,
  120. char *__restrict buffer, size_t buflen,
  121. struct spwd **__restrict result)
  122. {
  123. int rv;
  124. *result = NULL;
  125. if (!(rv = __pgsreader(__parsespent, resultbuf, buffer, buflen, stream))) {
  126. *result = resultbuf;
  127. }
  128. return rv;
  129. }
  130. libc_hidden_def(fgetspent_r)
  131. #endif
  132. /**********************************************************************/
  133. /* For the various fget??ent funcs, return NULL on failure and a
  134. * pointer to the appropriate struct (statically allocated) on success.
  135. */
  136. /**********************************************************************/
  137. #ifdef L_fgetpwent
  138. #ifdef __USE_SVID
  139. libc_hidden_proto(fgetpwent_r)
  140. struct passwd *fgetpwent(FILE *stream)
  141. {
  142. static char buffer[PWD_BUFFER_SIZE];
  143. static struct passwd resultbuf;
  144. struct passwd *result;
  145. fgetpwent_r(stream, &resultbuf, buffer, sizeof(buffer), &result);
  146. return result;
  147. }
  148. #endif
  149. #endif
  150. /**********************************************************************/
  151. #ifdef L_fgetgrent
  152. #ifdef __USE_SVID
  153. libc_hidden_proto(fgetgrent_r)
  154. struct group *fgetgrent(FILE *stream)
  155. {
  156. static char buffer[GRP_BUFFER_SIZE];
  157. static struct group resultbuf;
  158. struct group *result;
  159. fgetgrent_r(stream, &resultbuf, buffer, sizeof(buffer), &result);
  160. return result;
  161. }
  162. #endif
  163. #endif
  164. /**********************************************************************/
  165. #ifdef L_fgetspent
  166. libc_hidden_proto(fgetspent_r)
  167. struct spwd *fgetspent(FILE *stream)
  168. {
  169. static char buffer[PWD_BUFFER_SIZE];
  170. static struct spwd resultbuf;
  171. struct spwd *result;
  172. fgetspent_r(stream, &resultbuf, buffer, sizeof(buffer), &result);
  173. return result;
  174. }
  175. #endif
  176. /**********************************************************************/
  177. #ifdef L_sgetspent_r
  178. libc_hidden_proto(sgetspent_r)
  179. int sgetspent_r(const char *string, struct spwd *result_buf,
  180. char *buffer, size_t buflen, struct spwd **result)
  181. {
  182. int rv = ERANGE;
  183. *result = NULL;
  184. if (buflen < PWD_BUFFER_SIZE) {
  185. DO_ERANGE:
  186. __set_errno(rv);
  187. goto DONE;
  188. }
  189. if (string != buffer) {
  190. if (strlen(string) >= buflen) {
  191. goto DO_ERANGE;
  192. }
  193. strcpy(buffer, string);
  194. }
  195. if (!(rv = __parsespent(result_buf, buffer))) {
  196. *result = result_buf;
  197. }
  198. DONE:
  199. return rv;
  200. }
  201. libc_hidden_def(sgetspent_r)
  202. #endif
  203. /**********************************************************************/
  204. #ifdef GETXXKEY_R_FUNC
  205. #error GETXXKEY_R_FUNC is already defined!
  206. #endif
  207. #ifdef L_getpwnam_r
  208. #define GETXXKEY_R_FUNC getpwnam_r
  209. #define GETXXKEY_R_PARSER __parsepwent
  210. #define GETXXKEY_R_ENTTYPE struct passwd
  211. #define GETXXKEY_R_TEST(ENT) (!strcmp((ENT)->pw_name, key))
  212. #define DO_GETXXKEY_R_KEYTYPE const char *__restrict
  213. #define DO_GETXXKEY_R_PATHNAME _PATH_PASSWD
  214. #include "pwd_grp_internal.c"
  215. #endif
  216. #ifdef L_getgrnam_r
  217. #define GETXXKEY_R_FUNC getgrnam_r
  218. #define GETXXKEY_R_PARSER __parsegrent
  219. #define GETXXKEY_R_ENTTYPE struct group
  220. #define GETXXKEY_R_TEST(ENT) (!strcmp((ENT)->gr_name, key))
  221. #define DO_GETXXKEY_R_KEYTYPE const char *__restrict
  222. #define DO_GETXXKEY_R_PATHNAME _PATH_GROUP
  223. #include "pwd_grp_internal.c"
  224. #endif
  225. #ifdef L_getspnam_r
  226. #define GETXXKEY_R_FUNC getspnam_r
  227. #define GETXXKEY_R_PARSER __parsespent
  228. #define GETXXKEY_R_ENTTYPE struct spwd
  229. #define GETXXKEY_R_TEST(ENT) (!strcmp((ENT)->sp_namp, key))
  230. #define DO_GETXXKEY_R_KEYTYPE const char *__restrict
  231. #define DO_GETXXKEY_R_PATHNAME _PATH_SHADOW
  232. #include "pwd_grp_internal.c"
  233. #endif
  234. #ifdef L_getpwuid_r
  235. #define GETXXKEY_R_FUNC getpwuid_r
  236. #define GETXXKEY_R_PARSER __parsepwent
  237. #define GETXXKEY_R_ENTTYPE struct passwd
  238. #define GETXXKEY_R_TEST(ENT) ((ENT)->pw_uid == key)
  239. #define DO_GETXXKEY_R_KEYTYPE uid_t
  240. #define DO_GETXXKEY_R_PATHNAME _PATH_PASSWD
  241. #include "pwd_grp_internal.c"
  242. #endif
  243. #ifdef L_getgrgid_r
  244. #define GETXXKEY_R_FUNC getgrgid_r
  245. #define GETXXKEY_R_PARSER __parsegrent
  246. #define GETXXKEY_R_ENTTYPE struct group
  247. #define GETXXKEY_R_TEST(ENT) ((ENT)->gr_gid == key)
  248. #define DO_GETXXKEY_R_KEYTYPE gid_t
  249. #define DO_GETXXKEY_R_PATHNAME _PATH_GROUP
  250. #include "pwd_grp_internal.c"
  251. #endif
  252. /**********************************************************************/
  253. #ifdef L_getpwuid
  254. libc_hidden_proto(getpwuid_r)
  255. struct passwd *getpwuid(uid_t uid)
  256. {
  257. static char buffer[PWD_BUFFER_SIZE];
  258. static struct passwd resultbuf;
  259. struct passwd *result;
  260. getpwuid_r(uid, &resultbuf, buffer, sizeof(buffer), &result);
  261. return result;
  262. }
  263. #endif
  264. /**********************************************************************/
  265. #ifdef L_getgrgid
  266. libc_hidden_proto(getgrgid_r)
  267. struct group *getgrgid(gid_t gid)
  268. {
  269. static char buffer[GRP_BUFFER_SIZE];
  270. static struct group resultbuf;
  271. struct group *result;
  272. getgrgid_r(gid, &resultbuf, buffer, sizeof(buffer), &result);
  273. return result;
  274. }
  275. #endif
  276. /**********************************************************************/
  277. #ifdef L_getspuid_r
  278. /* This function is non-standard and is currently not built. It seems
  279. * to have been created as a reentrant version of the non-standard
  280. * functions getspuid. Why getspuid was added, I do not know. */
  281. libc_hidden_proto(getpwuid_r)
  282. libc_hidden_proto(getspnam_r)
  283. int getspuid_r(uid_t uid, struct spwd *__restrict resultbuf,
  284. char *__restrict buffer, size_t buflen,
  285. struct spwd **__restrict result)
  286. {
  287. int rv;
  288. struct passwd *pp;
  289. struct passwd password;
  290. char pwd_buff[PWD_BUFFER_SIZE];
  291. *result = NULL;
  292. if (!(rv = getpwuid_r(uid, &password, pwd_buff, sizeof(pwd_buff), &pp))) {
  293. rv = getspnam_r(password.pw_name, resultbuf, buffer, buflen, result);
  294. }
  295. return rv;
  296. }
  297. #endif
  298. /**********************************************************************/
  299. #ifdef L_getspuid
  300. /* This function is non-standard and is currently not built.
  301. * Why it was added, I do not know. */
  302. libc_hidden_proto(getspuid_r)
  303. struct spwd *getspuid(uid_t uid)
  304. {
  305. static char buffer[PWD_BUFFER_SIZE];
  306. static struct spwd resultbuf;
  307. struct spwd *result;
  308. getspuid_r(uid, &resultbuf, buffer, sizeof(buffer), &result);
  309. return result;
  310. }
  311. #endif
  312. /**********************************************************************/
  313. #ifdef L_getpwnam
  314. libc_hidden_proto(getpwnam_r)
  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. libc_hidden_proto(getgrnam_r)
  327. struct group *getgrnam(const char *name)
  328. {
  329. static char buffer[GRP_BUFFER_SIZE];
  330. static struct group resultbuf;
  331. struct group *result;
  332. getgrnam_r(name, &resultbuf, buffer, sizeof(buffer), &result);
  333. return result;
  334. }
  335. #endif
  336. /**********************************************************************/
  337. #ifdef L_getspnam
  338. libc_hidden_proto(getspnam_r)
  339. struct spwd *getspnam(const char *name)
  340. {
  341. static char buffer[PWD_BUFFER_SIZE];
  342. static struct spwd resultbuf;
  343. struct spwd *result;
  344. getspnam_r(name, &resultbuf, buffer, sizeof(buffer), &result);
  345. return result;
  346. }
  347. #endif
  348. /**********************************************************************/
  349. #ifdef L_getpw
  350. libc_hidden_proto(getpwuid_r)
  351. int getpw(uid_t uid, char *buf)
  352. {
  353. struct passwd resultbuf;
  354. struct passwd *result;
  355. char buffer[PWD_BUFFER_SIZE];
  356. if (!buf) {
  357. __set_errno(EINVAL);
  358. } else if (!getpwuid_r(uid, &resultbuf, buffer, sizeof(buffer), &result)) {
  359. if (sprintf(buf, "%s:%s:%lu:%lu:%s:%s:%s\n",
  360. resultbuf.pw_name, resultbuf.pw_passwd,
  361. (unsigned long)(resultbuf.pw_uid),
  362. (unsigned long)(resultbuf.pw_gid),
  363. resultbuf.pw_gecos, resultbuf.pw_dir,
  364. resultbuf.pw_shell) >= 0
  365. ) {
  366. return 0;
  367. }
  368. }
  369. return -1;
  370. }
  371. #endif
  372. /**********************************************************************/
  373. #ifdef L_getpwent_r
  374. __UCLIBC_MUTEX_STATIC(mylock, PTHREAD_MUTEX_INITIALIZER);
  375. static FILE *pwf /*= NULL*/;
  376. void setpwent(void)
  377. {
  378. __UCLIBC_MUTEX_LOCK(mylock);
  379. if (pwf) {
  380. rewind(pwf);
  381. }
  382. __UCLIBC_MUTEX_UNLOCK(mylock);
  383. }
  384. void endpwent(void)
  385. {
  386. __UCLIBC_MUTEX_LOCK(mylock);
  387. if (pwf) {
  388. fclose(pwf);
  389. pwf = NULL;
  390. }
  391. __UCLIBC_MUTEX_UNLOCK(mylock);
  392. }
  393. libc_hidden_proto(getpwent_r)
  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. __UCLIBC_MUTEX_LOCK(mylock);
  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. __UCLIBC_MUTEX_UNLOCK(mylock);
  414. return rv;
  415. }
  416. libc_hidden_def(getpwent_r)
  417. #endif
  418. /**********************************************************************/
  419. #ifdef L_getgrent_r
  420. __UCLIBC_MUTEX_STATIC(mylock, PTHREAD_MUTEX_INITIALIZER);
  421. static FILE *grf /*= NULL*/;
  422. void setgrent(void)
  423. {
  424. __UCLIBC_MUTEX_LOCK(mylock);
  425. if (grf) {
  426. rewind(grf);
  427. }
  428. __UCLIBC_MUTEX_UNLOCK(mylock);
  429. }
  430. void endgrent(void)
  431. {
  432. __UCLIBC_MUTEX_LOCK(mylock);
  433. if (grf) {
  434. fclose(grf);
  435. grf = NULL;
  436. }
  437. __UCLIBC_MUTEX_UNLOCK(mylock);
  438. }
  439. libc_hidden_proto(getgrent_r)
  440. int getgrent_r(struct group *__restrict resultbuf,
  441. char *__restrict buffer, size_t buflen,
  442. struct group **__restrict result)
  443. {
  444. int rv;
  445. __UCLIBC_MUTEX_LOCK(mylock);
  446. *result = NULL; /* In case of error... */
  447. if (!grf) {
  448. if (!(grf = fopen(_PATH_GROUP, "r"))) {
  449. rv = errno;
  450. goto ERR;
  451. }
  452. __STDIO_SET_USER_LOCKING(grf);
  453. }
  454. if (!(rv = __pgsreader(__parsegrent, resultbuf,
  455. buffer, buflen, grf))) {
  456. *result = resultbuf;
  457. }
  458. ERR:
  459. __UCLIBC_MUTEX_UNLOCK(mylock);
  460. return rv;
  461. }
  462. libc_hidden_def(getgrent_r)
  463. #endif
  464. /**********************************************************************/
  465. #ifdef L_getspent_r
  466. __UCLIBC_MUTEX_STATIC(mylock, PTHREAD_MUTEX_INITIALIZER);
  467. static FILE *spf /*= NULL*/;
  468. void setspent(void)
  469. {
  470. __UCLIBC_MUTEX_LOCK(mylock);
  471. if (spf) {
  472. rewind(spf);
  473. }
  474. __UCLIBC_MUTEX_UNLOCK(mylock);
  475. }
  476. void endspent(void)
  477. {
  478. __UCLIBC_MUTEX_LOCK(mylock);
  479. if (spf) {
  480. fclose(spf);
  481. spf = NULL;
  482. }
  483. __UCLIBC_MUTEX_UNLOCK(mylock);
  484. }
  485. libc_hidden_proto(getspent_r)
  486. int getspent_r(struct spwd *resultbuf, char *buffer,
  487. size_t buflen, struct spwd **result)
  488. {
  489. int rv;
  490. __UCLIBC_MUTEX_LOCK(mylock);
  491. *result = NULL; /* In case of error... */
  492. if (!spf) {
  493. if (!(spf = fopen(_PATH_SHADOW, "r"))) {
  494. rv = errno;
  495. goto ERR;
  496. }
  497. __STDIO_SET_USER_LOCKING(spf);
  498. }
  499. if (!(rv = __pgsreader(__parsespent, resultbuf,
  500. buffer, buflen, spf))) {
  501. *result = resultbuf;
  502. }
  503. ERR:
  504. __UCLIBC_MUTEX_UNLOCK(mylock);
  505. return rv;
  506. }
  507. libc_hidden_def(getspent_r)
  508. #endif
  509. /**********************************************************************/
  510. #ifdef L_getpwent
  511. libc_hidden_proto(getpwent_r)
  512. struct passwd *getpwent(void)
  513. {
  514. static char line_buff[PWD_BUFFER_SIZE];
  515. static struct passwd pwd;
  516. struct passwd *result;
  517. getpwent_r(&pwd, line_buff, sizeof(line_buff), &result);
  518. return result;
  519. }
  520. #endif
  521. /**********************************************************************/
  522. #ifdef L_getgrent
  523. libc_hidden_proto(getgrent_r)
  524. struct group *getgrent(void)
  525. {
  526. static char line_buff[GRP_BUFFER_SIZE];
  527. static struct group gr;
  528. struct group *result;
  529. getgrent_r(&gr, line_buff, sizeof(line_buff), &result);
  530. return result;
  531. }
  532. #endif
  533. /**********************************************************************/
  534. #ifdef L_getspent
  535. libc_hidden_proto(getspent_r)
  536. struct spwd *getspent(void)
  537. {
  538. static char line_buff[PWD_BUFFER_SIZE];
  539. static struct spwd spwd;
  540. struct spwd *result;
  541. getspent_r(&spwd, line_buff, sizeof(line_buff), &result);
  542. return result;
  543. }
  544. #endif
  545. /**********************************************************************/
  546. #ifdef L_sgetspent
  547. libc_hidden_proto(sgetspent_r)
  548. struct spwd *sgetspent(const char *string)
  549. {
  550. static char line_buff[PWD_BUFFER_SIZE];
  551. static struct spwd spwd;
  552. struct spwd *result;
  553. sgetspent_r(string, &spwd, line_buff, sizeof(line_buff), &result);
  554. return result;
  555. }
  556. #endif
  557. /**********************************************************************/
  558. #ifdef L_initgroups
  559. #ifdef __USE_BSD
  560. libc_hidden_proto(setgroups)
  561. int initgroups(const char *user, gid_t gid)
  562. {
  563. FILE *grfile;
  564. gid_t *group_list;
  565. int num_groups, rv;
  566. char **m;
  567. struct group group;
  568. char buff[PWD_BUFFER_SIZE];
  569. rv = -1;
  570. /* We alloc space for 8 gids at a time. */
  571. if (((group_list = (gid_t *) malloc(8*sizeof(gid_t *))) != NULL)
  572. && ((grfile = fopen(_PATH_GROUP, "r")) != NULL)
  573. ) {
  574. __STDIO_SET_USER_LOCKING(grfile);
  575. *group_list = gid;
  576. num_groups = 1;
  577. while (!__pgsreader(__parsegrent, &group, buff, sizeof(buff), grfile)) {
  578. assert(group.gr_mem); /* Must have at least a NULL terminator. */
  579. if (group.gr_gid != gid) {
  580. for (m=group.gr_mem ; *m ; m++) {
  581. if (!strcmp(*m, user)) {
  582. if (!(num_groups & 7)) {
  583. gid_t *tmp = (gid_t *)
  584. realloc(group_list,
  585. (num_groups+8) * sizeof(gid_t *));
  586. if (!tmp) {
  587. rv = -1;
  588. goto DO_CLOSE;
  589. }
  590. group_list = tmp;
  591. }
  592. group_list[num_groups++] = group.gr_gid;
  593. break;
  594. }
  595. }
  596. }
  597. }
  598. rv = setgroups(num_groups, group_list);
  599. DO_CLOSE:
  600. fclose(grfile);
  601. }
  602. /* group_list will be NULL if initial malloc failed, which may trigger
  603. * warnings from various malloc debuggers. */
  604. free(group_list);
  605. return rv;
  606. }
  607. #endif
  608. #endif
  609. /**********************************************************************/
  610. #ifdef L_putpwent
  611. #ifdef __USE_SVID
  612. int putpwent(const struct passwd *__restrict p, FILE *__restrict f)
  613. {
  614. int rv = -1;
  615. if (!p || !f) {
  616. __set_errno(EINVAL);
  617. } else {
  618. /* No extra thread locking is needed above what fprintf does. */
  619. if (fprintf(f, "%s:%s:%lu:%lu:%s:%s:%s\n",
  620. p->pw_name, p->pw_passwd,
  621. (unsigned long)(p->pw_uid),
  622. (unsigned long)(p->pw_gid),
  623. p->pw_gecos, p->pw_dir, p->pw_shell) >= 0
  624. ) {
  625. rv = 0;
  626. }
  627. }
  628. return rv;
  629. }
  630. #endif
  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. size_t 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 attribute_hidden __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 attribute_hidden __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 attribute_hidden __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 attribute_hidden __pgsreader(int (*__parserfunc)(void *d, char *line), void *data,
  921. char *__restrict line_buff, size_t buflen, FILE *f)
  922. {
  923. size_t 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. /**********************************************************************/