nss_test1.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  1. /* Template generic NSS service provider. See nss_test.h for usage.
  2. Copyright (C) 2017-2026 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <https://www.gnu.org/licenses/>. */
  15. #include <errno.h>
  16. #include <nss.h>
  17. #include <pthread.h>
  18. #include <string.h>
  19. #include <stdio.h>
  20. #include <alloc_buffer.h>
  21. /* We need to be able to handle NULLs "properly" within the testsuite,
  22. to test known bad data. */
  23. #define alloc_buffer_maybe_copy_string(b,s) s ? alloc_buffer_copy_string (b, s) : NULL;
  24. /* This file is both a standalone test and a parametrized template.
  25. Other instances of this test module should define NAME(x) to have
  26. their name instead of "test1", then include this file.
  27. */
  28. #define NAME_(x,n) _nss_##n##_##x
  29. #ifndef NAME
  30. #define NAME(x) NAME_(x,test1)
  31. #endif
  32. #define NAMESTR__(x) #x
  33. #define NAMESTR_(x) NAMESTR__(x)
  34. #define NAMESTR(x) NAMESTR_(NAME(x))
  35. #include "nss_test.h"
  36. /* -------------------------------------------------- */
  37. /* Default Data. */
  38. static struct passwd default_pwd_data[] =
  39. {
  40. #define PWD(u) \
  41. { .pw_name = (char *) "name" #u, .pw_passwd = (char *) "*", .pw_uid = u, \
  42. .pw_gid = 100, .pw_gecos = (char *) "*", .pw_dir = (char *) "*", \
  43. .pw_shell = (char *) "*" }
  44. PWD (30),
  45. PWD (100),
  46. PWD (200),
  47. PWD (60),
  48. PWD (20000)
  49. };
  50. #define default_npwd_data \
  51. (sizeof (default_pwd_data) / sizeof (default_pwd_data[0]))
  52. static struct passwd *pwd_data = default_pwd_data;
  53. static int npwd_data = default_npwd_data;
  54. static struct group *grp_data = NULL;
  55. static int ngrp_data = 0;
  56. static struct spwd *spwd_data = NULL;
  57. static int nspwd_data = 0;
  58. static struct hostent *host_data = NULL;
  59. static int nhost_data = 0;
  60. /* This function will get called, and once per session, look back into
  61. the test case's executable for an init hook function, and call
  62. it. */
  63. static int initted = 0;
  64. static void
  65. init(void)
  66. {
  67. test_tables t;
  68. int i;
  69. if (initted)
  70. return;
  71. if (NAME(init_hook))
  72. {
  73. memset (&t, 0, sizeof (t));
  74. NAME(init_hook)(&t);
  75. if (t.pwd_table)
  76. {
  77. pwd_data = t.pwd_table;
  78. for (i=0; ! PWD_ISLAST(& pwd_data[i]); i++)
  79. ;
  80. npwd_data = i;
  81. }
  82. if (t.grp_table)
  83. {
  84. grp_data = t.grp_table;
  85. for (i=0; ! GRP_ISLAST(& grp_data[i]); i++)
  86. ;
  87. ngrp_data = i;
  88. }
  89. if (t.spwd_table)
  90. {
  91. spwd_data = t.spwd_table;
  92. for (i=0; ! SPWD_ISLAST(& spwd_data[i]); i++)
  93. ;
  94. nspwd_data = i;
  95. }
  96. if (t.host_table)
  97. {
  98. host_data = t.host_table;
  99. for (i=0; ! HOST_ISLAST(& host_data[i]); i++)
  100. ;
  101. nhost_data = i;
  102. }
  103. }
  104. initted = 1;
  105. }
  106. /* -------------------------------------------------- */
  107. /* Password handling. */
  108. static size_t pwd_iter;
  109. #define CURPWD pwd_data[pwd_iter]
  110. static pthread_mutex_t pwd_lock = PTHREAD_MUTEX_INITIALIZER;
  111. enum nss_status
  112. NAME(setpwent) (int stayopen)
  113. {
  114. init();
  115. pwd_iter = 0;
  116. return NSS_STATUS_SUCCESS;
  117. }
  118. enum nss_status
  119. NAME(endpwent) (void)
  120. {
  121. init();
  122. return NSS_STATUS_SUCCESS;
  123. }
  124. static enum nss_status
  125. copy_passwd (struct passwd *result, struct passwd *local,
  126. char *buffer, size_t buflen, int *errnop)
  127. {
  128. struct alloc_buffer buf = alloc_buffer_create (buffer, buflen);
  129. result->pw_name = alloc_buffer_maybe_copy_string (&buf, local->pw_name);
  130. result->pw_passwd = alloc_buffer_maybe_copy_string (&buf, local->pw_passwd);
  131. result->pw_uid = local->pw_uid;
  132. result->pw_gid = local->pw_gid;
  133. result->pw_gecos = alloc_buffer_maybe_copy_string (&buf, local->pw_gecos);
  134. result->pw_dir = alloc_buffer_maybe_copy_string (&buf, local->pw_dir);
  135. result->pw_shell = alloc_buffer_maybe_copy_string (&buf, local->pw_shell);
  136. if (alloc_buffer_has_failed (&buf))
  137. {
  138. *errnop = ERANGE;
  139. return NSS_STATUS_TRYAGAIN;
  140. }
  141. return NSS_STATUS_SUCCESS;
  142. }
  143. enum nss_status
  144. NAME(getpwent_r) (struct passwd *result, char *buffer, size_t buflen,
  145. int *errnop)
  146. {
  147. int res = NSS_STATUS_SUCCESS;
  148. init();
  149. pthread_mutex_lock (&pwd_lock);
  150. if (pwd_iter >= npwd_data)
  151. res = NSS_STATUS_NOTFOUND;
  152. else
  153. {
  154. res = copy_passwd (result, &CURPWD, buffer, buflen, errnop);
  155. ++pwd_iter;
  156. }
  157. pthread_mutex_unlock (&pwd_lock);
  158. return res;
  159. }
  160. enum nss_status
  161. NAME(getpwuid_r) (uid_t uid, struct passwd *result, char *buffer,
  162. size_t buflen, int *errnop)
  163. {
  164. init();
  165. for (size_t idx = 0; idx < npwd_data; ++idx)
  166. if (pwd_data[idx].pw_uid == uid)
  167. return copy_passwd (result, &pwd_data[idx], buffer, buflen, errnop);
  168. return NSS_STATUS_NOTFOUND;
  169. }
  170. enum nss_status
  171. NAME(getpwnam_r) (const char *name, struct passwd *result, char *buffer,
  172. size_t buflen, int *errnop)
  173. {
  174. init();
  175. for (size_t idx = 0; idx < npwd_data; ++idx)
  176. if (strcmp (pwd_data[idx].pw_name, name) == 0)
  177. return copy_passwd (result, &pwd_data[idx], buffer, buflen, errnop);
  178. return NSS_STATUS_NOTFOUND;
  179. }
  180. /* -------------------------------------------------- */
  181. /* Group handling. */
  182. static size_t grp_iter;
  183. #define CURGRP grp_data[grp_iter]
  184. static pthread_mutex_t grp_lock = PTHREAD_MUTEX_INITIALIZER;
  185. enum nss_status
  186. NAME(setgrent) (int stayopen)
  187. {
  188. init();
  189. grp_iter = 0;
  190. return NSS_STATUS_SUCCESS;
  191. }
  192. enum nss_status
  193. NAME(endgrent) (void)
  194. {
  195. init();
  196. return NSS_STATUS_SUCCESS;
  197. }
  198. static enum nss_status
  199. copy_group (struct group *result, struct group *local,
  200. char *buffer, size_t buflen, int *errnop)
  201. {
  202. struct alloc_buffer buf = alloc_buffer_create (buffer, buflen);
  203. char **memlist;
  204. int i;
  205. if (local->gr_mem)
  206. {
  207. i = 0;
  208. while (local->gr_mem[i])
  209. ++i;
  210. memlist = alloc_buffer_alloc_array (&buf, char *, i + 1);
  211. if (memlist) {
  212. for (i = 0; local->gr_mem[i]; ++i)
  213. memlist[i] = alloc_buffer_maybe_copy_string (&buf, local->gr_mem[i]);
  214. memlist[i] = NULL;
  215. }
  216. result->gr_mem = memlist;
  217. }
  218. else
  219. result->gr_mem = NULL;
  220. result->gr_name = alloc_buffer_maybe_copy_string (&buf, local->gr_name);
  221. result->gr_passwd = alloc_buffer_maybe_copy_string (&buf, local->gr_passwd);
  222. result->gr_gid = local->gr_gid;
  223. if (alloc_buffer_has_failed (&buf))
  224. {
  225. *errnop = ERANGE;
  226. return NSS_STATUS_TRYAGAIN;
  227. }
  228. return NSS_STATUS_SUCCESS;
  229. }
  230. enum nss_status
  231. NAME(getgrent_r) (struct group *result, char *buffer, size_t buflen,
  232. int *errnop)
  233. {
  234. int res = NSS_STATUS_SUCCESS;
  235. init();
  236. pthread_mutex_lock (&grp_lock);
  237. if (grp_iter >= ngrp_data)
  238. res = NSS_STATUS_NOTFOUND;
  239. else
  240. {
  241. res = copy_group (result, &CURGRP, buffer, buflen, errnop);
  242. ++grp_iter;
  243. }
  244. pthread_mutex_unlock (&grp_lock);
  245. return res;
  246. }
  247. enum nss_status
  248. NAME(getgrgid_r) (gid_t gid, struct group *result, char *buffer,
  249. size_t buflen, int *errnop)
  250. {
  251. init();
  252. for (size_t idx = 0; idx < ngrp_data; ++idx)
  253. if (grp_data[idx].gr_gid == gid)
  254. return copy_group (result, &grp_data[idx], buffer, buflen, errnop);
  255. return NSS_STATUS_NOTFOUND;
  256. }
  257. enum nss_status
  258. NAME(getgrnam_r) (const char *name, struct group *result, char *buffer,
  259. size_t buflen, int *errnop)
  260. {
  261. init();
  262. for (size_t idx = 0; idx < ngrp_data; ++idx)
  263. if (strcmp (pwd_data[idx].pw_name, name) == 0)
  264. {
  265. return copy_group (result, &grp_data[idx], buffer, buflen, errnop);
  266. }
  267. return NSS_STATUS_NOTFOUND;
  268. }
  269. /* -------------------------------------------------- */
  270. /* Shadow password handling. */
  271. static size_t spwd_iter;
  272. #define CURSPWD spwd_data[spwd_iter]
  273. static pthread_mutex_t spwd_lock = PTHREAD_MUTEX_INITIALIZER;
  274. enum nss_status
  275. NAME(setspent) (int stayopen)
  276. {
  277. init();
  278. spwd_iter = 0;
  279. return NSS_STATUS_SUCCESS;
  280. }
  281. enum nss_status
  282. NAME(endspwent) (void)
  283. {
  284. init();
  285. return NSS_STATUS_SUCCESS;
  286. }
  287. static enum nss_status
  288. copy_shadow (struct spwd *result, struct spwd *local,
  289. char *buffer, size_t buflen, int *errnop)
  290. {
  291. struct alloc_buffer buf = alloc_buffer_create (buffer, buflen);
  292. result->sp_namp = alloc_buffer_maybe_copy_string (&buf, local->sp_namp);
  293. result->sp_pwdp = alloc_buffer_maybe_copy_string (&buf, local->sp_pwdp);
  294. result->sp_lstchg = local->sp_lstchg;
  295. result->sp_min = local->sp_min;
  296. result->sp_max = local->sp_max;
  297. result->sp_warn = local->sp_warn;
  298. result->sp_inact = local->sp_inact;
  299. result->sp_expire = local->sp_expire;
  300. result->sp_flag = local->sp_flag;
  301. if (alloc_buffer_has_failed (&buf))
  302. {
  303. *errnop = ERANGE;
  304. return NSS_STATUS_TRYAGAIN;
  305. }
  306. return NSS_STATUS_SUCCESS;
  307. }
  308. enum nss_status
  309. NAME(getspent_r) (struct spwd *result, char *buffer, size_t buflen,
  310. int *errnop)
  311. {
  312. int res = NSS_STATUS_SUCCESS;
  313. init();
  314. pthread_mutex_lock (&spwd_lock);
  315. if (spwd_iter >= nspwd_data)
  316. res = NSS_STATUS_NOTFOUND;
  317. else
  318. {
  319. res = copy_shadow (result, &CURSPWD, buffer, buflen, errnop);
  320. ++spwd_iter;
  321. }
  322. pthread_mutex_unlock (&spwd_lock);
  323. return res;
  324. }
  325. enum nss_status
  326. NAME(getspnam_r) (const char *name, struct spwd *result, char *buffer,
  327. size_t buflen, int *errnop)
  328. {
  329. init();
  330. for (size_t idx = 0; idx < nspwd_data; ++idx)
  331. if (strcmp (spwd_data[idx].sp_namp, name) == 0)
  332. return copy_shadow (result, &spwd_data[idx], buffer, buflen, errnop);
  333. return NSS_STATUS_NOTFOUND;
  334. }
  335. /* -------------------------------------------------- */
  336. /* Host handling. */
  337. static size_t host_iter;
  338. #define CURHOST host_data[host_iter]
  339. static pthread_mutex_t host_lock = PTHREAD_MUTEX_INITIALIZER;
  340. enum nss_status
  341. NAME(sethostent) (int stayopen)
  342. {
  343. init();
  344. host_iter = 0;
  345. return NSS_STATUS_SUCCESS;
  346. }
  347. enum nss_status
  348. NAME(endhostent) (void)
  349. {
  350. init();
  351. return NSS_STATUS_SUCCESS;
  352. }
  353. static enum nss_status
  354. copy_host (struct hostent *result, struct hostent *local,
  355. char *buffer, size_t buflen, int *errnop)
  356. {
  357. struct alloc_buffer buf = alloc_buffer_create (buffer, buflen);
  358. char **memlist;
  359. int i, j;
  360. if (local->h_addr_list)
  361. {
  362. i = 0;
  363. while (local->h_addr_list[i])
  364. ++i;
  365. memlist = alloc_buffer_alloc_array (&buf, char *, i + 1);
  366. if (memlist) {
  367. for (j = 0; j < i; ++j)
  368. memlist[j] = alloc_buffer_maybe_copy_string (&buf, local->h_addr_list[j]);
  369. memlist[j] = NULL;
  370. }
  371. result->h_addr_list = memlist;
  372. }
  373. else
  374. {
  375. result->h_addr_list = NULL;
  376. }
  377. result->h_aliases = NULL;
  378. result->h_addrtype = AF_INET;
  379. result->h_length = 4;
  380. result->h_name = alloc_buffer_maybe_copy_string (&buf, local->h_name);
  381. if (alloc_buffer_has_failed (&buf))
  382. {
  383. *errnop = ERANGE;
  384. return NSS_STATUS_TRYAGAIN;
  385. }
  386. return NSS_STATUS_SUCCESS;
  387. }
  388. enum nss_status
  389. NAME(gethostent_r) (struct hostent *ret, char *buffer, size_t buflen,
  390. struct hostent **result, int *errnop)
  391. {
  392. int res = NSS_STATUS_SUCCESS;
  393. init();
  394. pthread_mutex_lock (&host_lock);
  395. if (host_iter >= nhost_data)
  396. {
  397. res = NSS_STATUS_NOTFOUND;
  398. *result = NULL;
  399. }
  400. else
  401. {
  402. res = copy_host (ret, &CURHOST, buffer, buflen, errnop);
  403. *result = ret;
  404. ++host_iter;
  405. }
  406. pthread_mutex_unlock (&host_lock);
  407. return res;
  408. }
  409. enum nss_status
  410. NAME(gethostbyname3_r) (const char *name, int af, struct hostent *ret,
  411. char *buffer, size_t buflen, int *errnop,
  412. int *h_errnop, int32_t *ttlp, char **canonp)
  413. {
  414. init();
  415. for (size_t idx = 0; idx < nhost_data; ++idx)
  416. if (strcmp (host_data[idx].h_name, name) == 0)
  417. return copy_host (ret, & host_data[idx], buffer, buflen, h_errnop);
  418. return NSS_STATUS_NOTFOUND;
  419. }
  420. enum nss_status
  421. NAME(gethostbyname_r) (const char *name, struct hostent *result,
  422. char *buffer, size_t buflen,
  423. int *errnop, int *h_errnop)
  424. {
  425. return NAME(gethostbyname3_r) (name, AF_INET, result, buffer, buflen,
  426. errnop, h_errnop, NULL, NULL);
  427. }
  428. enum nss_status
  429. NAME(gethostbyname2_r) (const char *name, int af, struct hostent *result,
  430. char *buffer, size_t buflen,
  431. int *errnop, int *h_errnop)
  432. {
  433. return NAME(gethostbyname3_r) (name, af, result, buffer, buflen,
  434. errnop, h_errnop, NULL, NULL);
  435. }
  436. enum nss_status
  437. NAME(gethostbyaddr2_r) (const void *addr, socklen_t len, int af,
  438. struct hostent *result, char *buffer, size_t buflen,
  439. int *errnop, int *h_errnop, int32_t *ttlp)
  440. {
  441. init();
  442. /* Support this later. */
  443. if (len != 4)
  444. return NSS_STATUS_NOTFOUND;
  445. for (size_t idx = 0; idx < nhost_data; ++idx)
  446. if (memcmp (host_data[idx].h_addr, addr, len) == 0)
  447. return copy_host (result, & host_data[idx], buffer, buflen, h_errnop);
  448. return NSS_STATUS_NOTFOUND;
  449. }
  450. /* Note: only the first address is supported, intentionally. */
  451. enum nss_status
  452. NAME(gethostbyaddr_r) (const void *addr, socklen_t len, int af,
  453. struct hostent *result, char *buffer, size_t buflen,
  454. int *errnop, int *h_errnop)
  455. {
  456. return NAME(gethostbyaddr2_r) (addr, len, af, result, buffer, buflen,
  457. errnop, h_errnop, NULL);
  458. }