pwd_grp.c 28 KB

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