pwd_grp.c 28 KB

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