pwd_grp.c 28 KB

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