pwd_grp.c 27 KB

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