pwd_grp.c 26 KB

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