pam_unix_auth.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*
  2. * pam_unix authentication management
  3. *
  4. * Copyright Alexander O. Yuriev, 1996. All rights reserved.
  5. * NIS+ support by Thorsten Kukuk <kukuk@weber.uni-paderborn.de>
  6. * Copyright Jan Rękorajski, 1999. All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, and the entire permission notice in its entirety,
  13. * including the disclaimer of warranties.
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in the
  16. * documentation and/or other materials provided with the distribution.
  17. * 3. The name of the author may not be used to endorse or promote
  18. * products derived from this software without specific prior
  19. * written permission.
  20. *
  21. * ALTERNATIVELY, this product may be distributed under the terms of
  22. * the GNU Public License, in which case the provisions of the GPL are
  23. * required INSTEAD OF the above restrictions. (This clause is
  24. * necessary due to a potential bad interaction between the GPL and
  25. * the restrictions contained in a BSD-style copyright.)
  26. *
  27. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  28. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  29. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  30. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
  31. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  32. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  33. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  34. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  35. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  36. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  37. * OF THE POSSIBILITY OF SUCH DAMAGE.
  38. */
  39. #include "config.h"
  40. #include <stdio.h>
  41. #include <stdlib.h>
  42. #include <stdarg.h>
  43. #include <string.h>
  44. #include <unistd.h>
  45. #include <fcntl.h>
  46. #include <ctype.h>
  47. #include <sys/types.h>
  48. #include <sys/stat.h>
  49. #include <syslog.h>
  50. #include <security/_pam_macros.h>
  51. #include <security/pam_modules.h>
  52. #include <security/pam_ext.h>
  53. #include "support.h"
  54. /*
  55. * PAM framework looks for these entry-points to pass control to the
  56. * authentication module.
  57. */
  58. /* Fun starts here :)
  59. * pam_sm_authenticate() performs UNIX/shadow authentication
  60. *
  61. * First, if shadow support is available, attempt to perform
  62. * authentication using shadow passwords. If shadow is not
  63. * available, or user does not have a shadow password, fallback
  64. * onto a normal UNIX authentication
  65. */
  66. #define AUTH_RETURN \
  67. do { \
  68. D(("recording return code for next time [%d]", \
  69. retval)); \
  70. *ret_data = retval; \
  71. pam_set_data(pamh, "unix_setcred_return", \
  72. (void *) ret_data, setcred_free); \
  73. D(("done. [%s]", pam_strerror(pamh, retval))); \
  74. return retval; \
  75. } while (0)
  76. static void
  77. setcred_free (pam_handle_t *pamh UNUSED, void *ptr, int err UNUSED)
  78. {
  79. if (ptr)
  80. free (ptr);
  81. }
  82. int
  83. pam_sm_authenticate(pam_handle_t *pamh, int flags, int argc, const char **argv)
  84. {
  85. unsigned long long ctrl;
  86. int retval, *ret_data = NULL;
  87. const char *name;
  88. const char *p;
  89. D(("called."));
  90. ctrl = _set_ctrl(pamh, flags, NULL, NULL, NULL, argc, argv);
  91. /* Get a few bytes so we can pass our return value to
  92. pam_sm_setcred() and pam_sm_acct_mgmt(). */
  93. ret_data = malloc(sizeof(int));
  94. if (!ret_data) {
  95. D(("cannot malloc ret_data"));
  96. pam_syslog(pamh, LOG_CRIT,
  97. "pam_unix_auth: cannot allocate ret_data");
  98. return PAM_BUF_ERR;
  99. }
  100. /* get the user'name' */
  101. retval = pam_get_user(pamh, &name, NULL);
  102. if (retval == PAM_SUCCESS) {
  103. /*
  104. * Various libraries at various times have had bugs related to
  105. * '+' or '-' as the first character of a user name. Don't
  106. * allow this characters here.
  107. */
  108. if (name[0] == '-' || name[0] == '+') {
  109. pam_syslog(pamh, LOG_NOTICE, "bad username [%s]", name);
  110. retval = PAM_USER_UNKNOWN;
  111. AUTH_RETURN;
  112. }
  113. if (on(UNIX_DEBUG, ctrl))
  114. pam_syslog(pamh, LOG_DEBUG, "username [%s] obtained", name);
  115. } else {
  116. if (retval == PAM_CONV_AGAIN) {
  117. D(("pam_get_user/conv() function is not ready yet"));
  118. /* it is safe to resume this function so we translate this
  119. * retval to the value that indicates we're happy to resume.
  120. */
  121. retval = PAM_INCOMPLETE;
  122. } else if (on(UNIX_DEBUG, ctrl)) {
  123. pam_syslog(pamh, LOG_DEBUG, "could not obtain username");
  124. }
  125. AUTH_RETURN;
  126. }
  127. /* if this user does not have a password... */
  128. if (_unix_blankpasswd(pamh, ctrl, name)) {
  129. pam_syslog(pamh, LOG_DEBUG, "user [%s] has blank password; authenticated without it", name);
  130. name = NULL;
  131. retval = PAM_SUCCESS;
  132. AUTH_RETURN;
  133. }
  134. /* get this user's authentication token */
  135. retval = pam_get_authtok(pamh, PAM_AUTHTOK, &p , NULL);
  136. if (retval != PAM_SUCCESS) {
  137. if (retval != PAM_CONV_AGAIN) {
  138. pam_syslog(pamh, LOG_CRIT,
  139. "auth could not identify password for [%s]", name);
  140. } else {
  141. D(("conversation function is not ready yet"));
  142. /*
  143. * it is safe to resume this function so we translate this
  144. * retval to the value that indicates we're happy to resume.
  145. */
  146. retval = PAM_INCOMPLETE;
  147. }
  148. name = NULL;
  149. AUTH_RETURN;
  150. }
  151. D(("user=%s, password=[%s]", name, p));
  152. /* verify the password of this user */
  153. retval = _unix_verify_password(pamh, name, p, ctrl);
  154. name = p = NULL;
  155. AUTH_RETURN;
  156. }
  157. /*
  158. * The only thing _pam_set_credentials_unix() does is initialization of
  159. * UNIX group IDs.
  160. *
  161. * Well, everybody but me on linux-pam is convinced that it should not
  162. * initialize group IDs, so I am not doing it but don't say that I haven't
  163. * warned you. -- AOY
  164. */
  165. int
  166. pam_sm_setcred (pam_handle_t *pamh, int flags,
  167. int argc, const char **argv)
  168. {
  169. int retval;
  170. const void *pretval = NULL;
  171. unsigned long long ctrl;
  172. D(("called."));
  173. ctrl = _set_ctrl(pamh, flags, NULL, NULL, NULL, argc, argv);
  174. retval = PAM_SUCCESS;
  175. D(("recovering return code from auth call"));
  176. /* We will only find something here if UNIX_LIKE_AUTH is set --
  177. don't worry about an explicit check of argv. */
  178. if (on(UNIX_LIKE_AUTH, ctrl)
  179. && pam_get_data(pamh, "unix_setcred_return", &pretval) == PAM_SUCCESS
  180. && pretval) {
  181. retval = *(const int *)pretval;
  182. pam_set_data(pamh, "unix_setcred_return", NULL, NULL);
  183. D(("recovered data indicates that old retval was %d", retval));
  184. }
  185. return retval;
  186. }