tst-pam_set_data.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. /*
  2. * Redistribution and use in source and binary forms, with or without
  3. * modification, are permitted provided that the following conditions
  4. * are met:
  5. * 1. Redistributions of source code must retain the above copyright
  6. * notice, and the entire permission notice in its entirety,
  7. * including the disclaimer of warranties.
  8. * 2. Redistributions in binary form must reproduce the above copyright
  9. * notice, this list of conditions and the following disclaimer in the
  10. * documentation and/or other materials provided with the distribution.
  11. * 3. The name of the author may not be used to endorse or promote
  12. * products derived from this software without specific prior
  13. * written permission.
  14. *
  15. * ALTERNATIVELY, this product may be distributed under the terms of
  16. * the GNU Public License, in which case the provisions of the GPL are
  17. * required INSTEAD OF the above restrictions. (This clause is
  18. * necessary due to a potential bad interaction between the GPL and
  19. * the restrictions contained in a BSD-style copyright.)
  20. *
  21. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  22. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  23. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  24. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
  25. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  26. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  27. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  29. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  30. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  31. * OF THE POSSIBILITY OF SUCH DAMAGE.
  32. */
  33. #ifdef HAVE_CONFIG_H
  34. #include <config.h>
  35. #endif
  36. #include <stdio.h>
  37. #include <unistd.h>
  38. #include <string.h>
  39. #include <security/pam_appl.h>
  40. #include <security/pam_modules.h>
  41. #include <pam_private.h>
  42. static int cleanup_was_called = 0;
  43. static int cleanup3_was_called = 0;
  44. static int cleanup3_retval = 0;
  45. static int cleanup6_was_called = 0;
  46. static int cleanup6_retval = 0;
  47. static int cleanup7_was_called = 0;
  48. static int cleanup7_retval = 0;
  49. static int cleanup7b_was_called = 0;
  50. static int cleanup7b_retval = 0;
  51. static int cleanup8_was_called = 0;
  52. static int cleanup8_retval = 0;
  53. static void
  54. tst_cleanup (pam_handle_t *pamh UNUSED, void *data, int error_status)
  55. {
  56. cleanup_was_called = 1;
  57. fprintf (stderr, "tst_cleanup was called: data=\"%s\", error_status=%d\n",
  58. (char *)data, error_status);
  59. }
  60. static void
  61. tst_cleanup_3 (pam_handle_t *pamh UNUSED, void *data, int error_status)
  62. {
  63. cleanup3_was_called = 1;
  64. if (strcmp (data, "test3") != 0)
  65. {
  66. fprintf (stderr, "tst_cleanup_3 called with wrong data, got \"%s\"\n",
  67. (char *)data);
  68. cleanup3_retval = 1;
  69. return;
  70. }
  71. free (data);
  72. if (error_status & PAM_DATA_REPLACE)
  73. {
  74. fprintf (stderr, "tst_cleanup_3 called with PAM_DATA_REPLACE set\n");
  75. cleanup3_retval = 1;
  76. return;
  77. }
  78. if (error_status & PAM_DATA_SILENT)
  79. {
  80. fprintf (stderr, "tst_cleanup_3 called with PAM_DATA_SILENT set\n");
  81. cleanup3_retval = 1;
  82. return;
  83. }
  84. if (error_status != 0)
  85. {
  86. fprintf (stderr, "tst_cleanup_3 called with error_status set: %d\n",
  87. error_status);
  88. cleanup3_retval = 1;
  89. return;
  90. }
  91. }
  92. static void
  93. tst_cleanup_6 (pam_handle_t *pamh UNUSED, void *data, int error_status)
  94. {
  95. cleanup6_was_called = 1;
  96. if (error_status & PAM_DATA_SILENT)
  97. {
  98. fprintf (stderr, "tst_cleanup_6 called with PAM_DATA_SILENT set\n");
  99. cleanup6_retval = 1;
  100. return;
  101. }
  102. if (error_status & PAM_DATA_REPLACE)
  103. {
  104. if (strcmp (data, "test6a") != 0)
  105. {
  106. fprintf (stderr, "tst_cleanup_6 called with wrong data, got \"%s\"\n",
  107. (char *)data);
  108. cleanup6_retval = 1;
  109. return;
  110. }
  111. if (error_status != PAM_DATA_REPLACE)
  112. {
  113. fprintf (stderr, "tst_cleanup_6 called with error_status set: %d\n",
  114. error_status);
  115. cleanup6_retval = 1;
  116. return;
  117. }
  118. }
  119. else
  120. {
  121. if (strcmp (data, "test6b") != 0)
  122. {
  123. fprintf (stderr, "tst_cleanup_6 called with wrong data, got \"%s\"\n",
  124. (char *)data);
  125. cleanup6_retval = 1;
  126. return;
  127. }
  128. if (error_status != 0)
  129. {
  130. fprintf (stderr, "tst_cleanup_6 called with error_status set: %d\n",
  131. error_status);
  132. cleanup6_retval = 1;
  133. return;
  134. }
  135. }
  136. free (data);
  137. }
  138. static void
  139. tst_cleanup_7 (pam_handle_t *pamh UNUSED, void *data, int error_status)
  140. {
  141. cleanup7_was_called = 1;
  142. if (error_status & PAM_DATA_SILENT)
  143. {
  144. fprintf (stderr, "tst_cleanup_7 called with PAM_DATA_SILENT set\n");
  145. cleanup7_retval = 1;
  146. return;
  147. }
  148. if (error_status & PAM_DATA_REPLACE)
  149. {
  150. if (strcmp (data, "test7a") != 0)
  151. {
  152. fprintf (stderr, "tst_cleanup_7 called with wrong data, got \"%s\"\n",
  153. (char *)data);
  154. cleanup7_retval = 1;
  155. return;
  156. }
  157. if (error_status != PAM_DATA_REPLACE)
  158. {
  159. fprintf (stderr, "tst_cleanup_7 called with error_status set: %d\n",
  160. error_status);
  161. cleanup7_retval = 1;
  162. return;
  163. }
  164. }
  165. else
  166. {
  167. fprintf (stderr, "tst_cleanup_7 called without PAM_DATA_REPLACE set: %d\n",
  168. error_status);
  169. cleanup7_retval = 1;
  170. return;
  171. }
  172. free (data);
  173. }
  174. static void
  175. tst_cleanup_7b (pam_handle_t *pamh UNUSED, void *data, int error_status)
  176. {
  177. cleanup7b_was_called = 1;
  178. if (strcmp (data, "test7b") != 0)
  179. {
  180. fprintf (stderr, "tst_cleanup_7b called with wrong data, got \"%s\"\n",
  181. (char *)data);
  182. cleanup7b_retval = 1;
  183. return;
  184. }
  185. free (data);
  186. if (error_status & PAM_DATA_REPLACE)
  187. {
  188. fprintf (stderr, "tst_cleanup_7b called with PAM_DATA_REPLACE set\n");
  189. cleanup7b_retval = 1;
  190. return;
  191. }
  192. if (error_status & PAM_DATA_SILENT)
  193. {
  194. fprintf (stderr, "tst_cleanup_7b called with PAM_DATA_SILENT set\n");
  195. cleanup7b_retval = 1;
  196. return;
  197. }
  198. if (error_status != 0)
  199. {
  200. fprintf (stderr, "tst_cleanup_7b called with error_status set: %d\n",
  201. error_status);
  202. cleanup7b_retval = 1;
  203. return;
  204. }
  205. }
  206. static void
  207. tst_cleanup_8 (pam_handle_t *pamh UNUSED, void *data, int error_status)
  208. {
  209. cleanup8_was_called = 1;
  210. if (strcmp (data, "test8") != 0)
  211. {
  212. fprintf (stderr, "tst_cleanup_8 called with wrong data, got \"%s\"\n",
  213. (char *)data);
  214. cleanup8_retval = 1;
  215. return;
  216. }
  217. free (data);
  218. if (error_status & PAM_DATA_REPLACE)
  219. {
  220. fprintf (stderr, "tst_cleanup_8 called with PAM_DATA_REPLACE set\n");
  221. cleanup8_retval = 1;
  222. return;
  223. }
  224. if (error_status & PAM_DATA_SILENT)
  225. {
  226. fprintf (stderr, "tst_cleanup_8 called with PAM_DATA_SILENT set\n");
  227. cleanup8_retval = 1;
  228. return;
  229. }
  230. if (error_status != 987)
  231. {
  232. fprintf (stderr, "tst_cleanup_8 called with wrong error_status set: %d\n",
  233. error_status);
  234. cleanup8_retval = 1;
  235. return;
  236. }
  237. }
  238. int
  239. main (void)
  240. {
  241. const char *service = "dummy";
  242. const char *user = "root";
  243. struct pam_conv conv = { NULL, NULL };
  244. pam_handle_t *pamh;
  245. void *dataptr;
  246. int retval;
  247. /* 1: Call with NULL as pam handle */
  248. dataptr = strdup ("test1");
  249. retval = pam_set_data (NULL, "tst-pam_set_data-1", dataptr, tst_cleanup);
  250. if (retval == PAM_SUCCESS)
  251. {
  252. fprintf (stderr, "pam_set_data (NULL, ...) returned PAM_SUCCESS\n");
  253. return 1;
  254. }
  255. free (dataptr);
  256. /* setup pam handle */
  257. retval = pam_start (service, user, &conv, &pamh);
  258. if (retval != PAM_SUCCESS)
  259. {
  260. fprintf (stderr, "pam_start (%s, %s, &conv, &pamh) returned %d\n",
  261. service, user, retval);
  262. return 1;
  263. }
  264. /* 2: check for call from application */
  265. dataptr = strdup ("test2");
  266. retval = pam_set_data (pamh, "tst-pam_set_data-2", dataptr, tst_cleanup);
  267. if (retval != PAM_SYSTEM_ERR)
  268. {
  269. fprintf (stderr,
  270. "pam_set_data returned %d when expecting PAM_SYSTEM_ERR\n",
  271. retval);
  272. return 1;
  273. }
  274. free (dataptr);
  275. /* 3: check for call from module */
  276. __PAM_TO_MODULE(pamh);
  277. dataptr = strdup ("test3");
  278. retval = pam_set_data (pamh, "tst-pam_set_data-3", dataptr,
  279. tst_cleanup_3);
  280. if (retval != PAM_SUCCESS)
  281. {
  282. fprintf (stderr,
  283. "pam_set_data failed: %d\n",
  284. retval);
  285. return 1;
  286. }
  287. /* 4: check for call with NULL as module_data_name */
  288. dataptr = strdup ("test4");
  289. retval = pam_set_data (pamh, NULL, dataptr, tst_cleanup);
  290. if (retval == PAM_SUCCESS)
  291. {
  292. fprintf (stderr,
  293. "pam_set_data with NULL as module_data_name succeeded!\n");
  294. return 1;
  295. }
  296. free (dataptr);
  297. /* 5: check for call with NULL as cleanup function */
  298. dataptr = strdup ("test5");
  299. retval = pam_set_data (pamh, "tst-pam_set_data-5", dataptr, NULL);
  300. if (retval != PAM_SUCCESS)
  301. {
  302. fprintf (stderr,
  303. "pam_set_data with NULL as cleanup function failed: %d\n",
  304. retval);
  305. return 1;
  306. }
  307. free (dataptr);
  308. /* 6: Overwrite data and check cleanup flags */
  309. dataptr = strdup ("test6a");
  310. retval = pam_set_data (pamh, "tst-pam_set_data-6", dataptr,
  311. tst_cleanup_6);
  312. if (retval != PAM_SUCCESS)
  313. {
  314. fprintf (stderr,
  315. "test6: first pam_set_data failed: %d\n",
  316. retval);
  317. return 1;
  318. }
  319. dataptr = strdup ("test6b");
  320. retval = pam_set_data (pamh, "tst-pam_set_data-6", dataptr,
  321. tst_cleanup_6);
  322. if (retval != PAM_SUCCESS)
  323. {
  324. fprintf (stderr,
  325. "test6: second pam_set_data failed: %d\n",
  326. retval);
  327. return 1;
  328. }
  329. /* 7: Overwrite data and cleanup function, check cleanup flags */
  330. dataptr = strdup ("test7a");
  331. retval = pam_set_data (pamh, "tst-pam_set_data-7", dataptr,
  332. tst_cleanup_7);
  333. if (retval != PAM_SUCCESS)
  334. {
  335. fprintf (stderr,
  336. "test7: first pam_set_data failed: %d\n",
  337. retval);
  338. return 1;
  339. }
  340. dataptr = strdup ("test7b");
  341. retval = pam_set_data (pamh, "tst-pam_set_data-7", dataptr,
  342. tst_cleanup_7b);
  343. if (retval != PAM_SUCCESS)
  344. {
  345. fprintf (stderr,
  346. "test7: second pam_set_data failed: %d\n",
  347. retval);
  348. return 1;
  349. }
  350. __PAM_TO_APP(pamh);
  351. /* Close PAM handle and check return codes of cleanup functions */
  352. retval = pam_end (pamh, 0);
  353. if (retval != PAM_SUCCESS)
  354. {
  355. fprintf (stderr,
  356. "pam_end reported an error: %d\n",
  357. retval);
  358. return 1;
  359. }
  360. if (cleanup_was_called == 1)
  361. return 1;
  362. if (cleanup3_was_called == 0)
  363. {
  364. fprintf (stderr, "tst_cleanup_3 was never called!\n");
  365. return 1;
  366. }
  367. if (cleanup3_retval != 0)
  368. return 1;
  369. if (cleanup6_was_called == 0)
  370. {
  371. fprintf (stderr, "tst_cleanup_6 was never called!\n");
  372. return 1;
  373. }
  374. if (cleanup6_retval != 0)
  375. return 1;
  376. if (cleanup7_was_called == 0)
  377. {
  378. fprintf (stderr, "tst_cleanup_7 was never called!\n");
  379. return 1;
  380. }
  381. if (cleanup7_retval != 0)
  382. return 1;
  383. if (cleanup7b_was_called == 0)
  384. {
  385. fprintf (stderr, "tst_cleanup_7b was never called!\n");
  386. return 1;
  387. }
  388. if (cleanup7b_retval != 0)
  389. return 1;
  390. /* test if error code is delivered to cleanup function */
  391. /* setup pam handle */
  392. retval = pam_start (service, user, &conv, &pamh);
  393. if (retval != PAM_SUCCESS)
  394. {
  395. fprintf (stderr, "pam_start (%s, %s, &conv, &pamh) returned %d\n",
  396. service, user, retval);
  397. return 1;
  398. }
  399. /* 8: check if cleanup function is called with correct error code */
  400. __PAM_TO_MODULE(pamh);
  401. dataptr = strdup ("test8");
  402. retval = pam_set_data (pamh, "tst-pam_set_data-8", dataptr,
  403. tst_cleanup_8);
  404. if (retval != PAM_SUCCESS)
  405. {
  406. fprintf (stderr,
  407. "test8: pam_set_data failed: %d\n",
  408. retval);
  409. return 1;
  410. }
  411. __PAM_TO_APP(pamh);
  412. retval = pam_end (pamh, 987);
  413. if (retval != PAM_SUCCESS)
  414. {
  415. fprintf (stderr,
  416. "pam_end reported an error: %d\n",
  417. retval);
  418. return 1;
  419. }
  420. if (cleanup8_was_called == 0)
  421. {
  422. fprintf (stderr, "tst_cleanup_3 was never called!\n");
  423. return 1;
  424. }
  425. if (cleanup8_retval != 0)
  426. return 1;
  427. return 0;
  428. }