pwd_grp.c 27 KB

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