pwd_grp.c 25 KB

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