pwd_grp.c 28 KB

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