tst-pam_get_user.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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 <config.h>
  37. #include <stdio.h>
  38. #include <unistd.h>
  39. #include <string.h>
  40. #include <stdlib.h>
  41. #include <security/pam_appl.h>
  42. #include <security/pam_modules.h>
  43. static const char *prompt = "myprompt:";
  44. static const char *user = "itsme";
  45. static int
  46. login_conv (int num_msg, const struct pam_message **mesg,
  47. struct pam_response **resp, void *appdata_ptr UNUSED)
  48. {
  49. struct pam_response *reply;
  50. int count;
  51. reply = calloc(num_msg, sizeof (struct pam_response));
  52. if (reply == NULL)
  53. return PAM_BUF_ERR;
  54. for (count = 0; count < num_msg; count++)
  55. {
  56. reply[count].resp_retcode = 0;
  57. reply[count].resp = NULL;
  58. switch (mesg[count]->msg_style)
  59. {
  60. case PAM_PROMPT_ECHO_ON:
  61. if (strcmp (mesg[count]->msg, prompt) != 0)
  62. {
  63. fprintf (stderr, "conv function called with wrong prompt: %s\n",
  64. mesg[count]->msg);
  65. exit (1);
  66. }
  67. reply[count].resp = strdup (user);
  68. break;
  69. default:
  70. fprintf (stderr,
  71. "pam_get_user calls conv function with unexpected msg style");
  72. exit (1);
  73. }
  74. }
  75. *resp = reply;
  76. return PAM_SUCCESS;
  77. }
  78. int
  79. main (void)
  80. {
  81. const char *service = "dummy";
  82. const char *value;
  83. struct pam_conv conv = { &login_conv, NULL};
  84. pam_handle_t *pamh;
  85. int retval;
  86. /* 1: Call with NULL for every argument */
  87. retval = pam_get_user (NULL, NULL, NULL);
  88. if (retval == PAM_SUCCESS)
  89. {
  90. fprintf (stderr,
  91. "tst-pam_get_user (NULL, NULL, NULL) returned PAM_SUCCESS\n");
  92. return 1;
  93. }
  94. /* setup pam handle */
  95. retval = pam_start (service, user, &conv, &pamh);
  96. if (retval != PAM_SUCCESS)
  97. {
  98. fprintf (stderr, "pam_start (%s, %s, &conv, &pamh) returned %d\n",
  99. service, user, retval);
  100. return 1;
  101. }
  102. /* 2: Call with valid pamh handle but NULL for user */
  103. retval = pam_get_user (pamh, NULL, NULL);
  104. if (retval == PAM_SUCCESS)
  105. {
  106. fprintf (stderr,
  107. "tst-pam_get_user (pamh, NULL, NULL) returned PAM_SUCCESS\n");
  108. return 1;
  109. }
  110. /* 3: Call with valid pamh handle and valid user ptr */
  111. retval = pam_get_user (pamh, &value, NULL);
  112. if (retval != PAM_SUCCESS)
  113. {
  114. fprintf (stderr,
  115. "tst-pam_get_user (pamh, &value, NULL) returned %d\n",
  116. retval);
  117. return 1;
  118. }
  119. if (strcmp (user, value) != 0)
  120. {
  121. fprintf (stderr,
  122. "tst-pam_get_user (pamh, &value, NULL) mismatch:\n"
  123. "expected: %s\n"
  124. "got: %s\n", user, value);
  125. return 1;
  126. }
  127. pam_end (pamh, 0);
  128. /* setup pam handle without user */
  129. retval = pam_start (service, NULL, &conv, &pamh);
  130. if (retval != PAM_SUCCESS)
  131. {
  132. fprintf (stderr, "pam_start (%s, %s, &conv, &pamh) returned %d\n",
  133. service, user, retval);
  134. return 1;
  135. }
  136. /* 4: Call with valid pamh handle and valid user ptr */
  137. retval = pam_get_user (pamh, &value, prompt);
  138. if (retval != PAM_SUCCESS)
  139. {
  140. fprintf (stderr,
  141. "tst-pam_get_user (pamh, &value, prompt) returned %d\n",
  142. retval);
  143. return 1;
  144. }
  145. if (strcmp (user, value) != 0)
  146. {
  147. fprintf (stderr,
  148. "tst-pam_get_user (pamh, &value, prompt) mismatch:\n"
  149. "expected: %s\n"
  150. "got: %s\n", user, value);
  151. return 1;
  152. }
  153. pam_end (pamh, 0);
  154. return 0;
  155. }