svr-authpam.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /*
  2. * Dropbear SSH
  3. *
  4. * Copyright (c) 2004 Martin Carlsson
  5. * Portions (c) 2004 Matt Johnston
  6. * All rights reserved.
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  24. * SOFTWARE. */
  25. /* Validates a user password using PAM */
  26. #include "includes.h"
  27. #include "session.h"
  28. #include "buffer.h"
  29. #include "dbutil.h"
  30. #include "auth.h"
  31. #ifdef ENABLE_SVR_PAM_AUTH
  32. #if defined(HAVE_SECURITY_PAM_APPL_H)
  33. #include <security/pam_appl.h>
  34. #elif defined (HAVE_PAM_PAM_APPL_H)
  35. #include <pam/pam_appl.h>
  36. #endif
  37. struct UserDataS {
  38. char* user;
  39. char* passwd;
  40. };
  41. /* PAM conversation function - for now we only handle one message */
  42. int
  43. pamConvFunc(int num_msg,
  44. const struct pam_message **msg,
  45. struct pam_response **respp,
  46. void *appdata_ptr) {
  47. int rc = PAM_SUCCESS;
  48. struct pam_response* resp = NULL;
  49. struct UserDataS* userDatap = (struct UserDataS*) appdata_ptr;
  50. unsigned int msg_len = 0;
  51. unsigned int i = 0;
  52. char * compare_message = NULL;
  53. TRACE(("enter pamConvFunc"))
  54. if (num_msg != 1) {
  55. /* If you're getting here - Dropbear probably can't support your pam
  56. * modules. This whole file is a bit of a hack around lack of
  57. * asynchronocity in PAM anyway. */
  58. dropbear_log(LOG_INFO, "pamConvFunc() called with >1 messages: not supported.");
  59. return PAM_CONV_ERR;
  60. }
  61. /* make a copy we can strip */
  62. compare_message = m_strdup((*msg)->msg);
  63. /* Make the string lowercase. */
  64. msg_len = strlen(compare_message);
  65. for (i = 0; i < msg_len; i++) {
  66. compare_message[i] = tolower(compare_message[i]);
  67. }
  68. /* If the string ends with ": ", remove the space.
  69. ie "login: " vs "login:" */
  70. if (msg_len > 2
  71. && compare_message[msg_len-2] == ':'
  72. && compare_message[msg_len-1] == ' ') {
  73. compare_message[msg_len-1] = '\0';
  74. }
  75. switch((*msg)->msg_style) {
  76. case PAM_PROMPT_ECHO_OFF:
  77. if (!(strcmp(compare_message, "password:") == 0)) {
  78. /* We don't recognise the prompt as asking for a password,
  79. so can't handle it. Add more above as required for
  80. different pam modules/implementations. If you need
  81. to add an entry here please mail the Dropbear developer */
  82. dropbear_log(LOG_NOTICE, "PAM unknown prompt '%s' (no echo)",
  83. compare_message);
  84. rc = PAM_CONV_ERR;
  85. break;
  86. }
  87. /* You have to read the PAM module-writers' docs (do we look like
  88. * module writers? no.) to find out that the module will
  89. * free the pam_response and its resp element - ie we _must_ malloc
  90. * it here */
  91. resp = (struct pam_response*) m_malloc(sizeof(struct pam_response));
  92. memset(resp, 0, sizeof(struct pam_response));
  93. resp->resp = m_strdup(userDatap->passwd);
  94. m_burn(userDatap->passwd, strlen(userDatap->passwd));
  95. (*respp) = resp;
  96. break;
  97. case PAM_PROMPT_ECHO_ON:
  98. if (!(
  99. (strcmp(compare_message, "login:" ) == 0)
  100. || (strcmp(compare_message, "please enter username:") == 0)
  101. || (strcmp(compare_message, "username:") == 0)
  102. )) {
  103. /* We don't recognise the prompt as asking for a username,
  104. so can't handle it. Add more above as required for
  105. different pam modules/implementations. If you need
  106. to add an entry here please mail the Dropbear developer */
  107. dropbear_log(LOG_NOTICE, "PAM unknown prompt '%s' (with echo)",
  108. compare_message);
  109. rc = PAM_CONV_ERR;
  110. break;
  111. }
  112. /* You have to read the PAM module-writers' docs (do we look like
  113. * module writers? no.) to find out that the module will
  114. * free the pam_response and its resp element - ie we _must_ malloc
  115. * it here */
  116. resp = (struct pam_response*) m_malloc(sizeof(struct pam_response));
  117. memset(resp, 0, sizeof(struct pam_response));
  118. resp->resp = m_strdup(userDatap->user);
  119. TRACE(("userDatap->user='%s'", userDatap->user))
  120. (*respp) = resp;
  121. break;
  122. case PAM_ERROR_MSG:
  123. case PAM_TEXT_INFO:
  124. if (msg_len > 0) {
  125. buffer * pam_err = buf_new(msg_len + 4);
  126. buf_setpos(pam_err, 0);
  127. buf_putbytes(pam_err, "\r\n", 2);
  128. buf_putbytes(pam_err, (*msg)->msg, msg_len);
  129. buf_putbytes(pam_err, "\r\n", 2);
  130. buf_setpos(pam_err, 0);
  131. send_msg_userauth_banner(pam_err);
  132. buf_free(pam_err);
  133. }
  134. break;
  135. default:
  136. TRACE(("Unknown message type"))
  137. rc = PAM_CONV_ERR;
  138. break;
  139. }
  140. m_free(compare_message);
  141. TRACE(("leave pamConvFunc, rc %d", rc))
  142. return rc;
  143. }
  144. /* Process a password auth request, sending success or failure messages as
  145. * appropriate. To the client it looks like it's doing normal password auth (as
  146. * opposed to keyboard-interactive or something), so the pam module has to be
  147. * fairly standard (ie just "what's your username, what's your password, OK").
  148. *
  149. * Keyboard interactive would be a lot nicer, but since PAM is synchronous, it
  150. * gets very messy trying to send the interactive challenges, and read the
  151. * interactive responses, over the network. */
  152. void svr_auth_pam() {
  153. struct UserDataS userData = {NULL, NULL};
  154. struct pam_conv pamConv = {
  155. pamConvFunc,
  156. &userData /* submitted to pamvConvFunc as appdata_ptr */
  157. };
  158. pam_handle_t* pamHandlep = NULL;
  159. char * password = NULL;
  160. unsigned int passwordlen;
  161. int rc = PAM_SUCCESS;
  162. unsigned char changepw;
  163. /* check if client wants to change password */
  164. changepw = buf_getbool(ses.payload);
  165. if (changepw) {
  166. /* not implemented by this server */
  167. send_msg_userauth_failure(0, 1);
  168. goto cleanup;
  169. }
  170. password = buf_getstring(ses.payload, &passwordlen);
  171. /* used to pass data to the PAM conversation function - don't bother with
  172. * strdup() etc since these are touched only by our own conversation
  173. * function (above) which takes care of it */
  174. userData.user = ses.authstate.pw_name;
  175. userData.passwd = password;
  176. /* Init pam */
  177. if ((rc = pam_start("sshd", NULL, &pamConv, &pamHandlep)) != PAM_SUCCESS) {
  178. dropbear_log(LOG_WARNING, "pam_start() failed, rc=%d, %s",
  179. rc, pam_strerror(pamHandlep, rc));
  180. goto cleanup;
  181. }
  182. /* just to set it to something */
  183. if ((rc = pam_set_item(pamHandlep, PAM_TTY, "ssh")) != PAM_SUCCESS) {
  184. dropbear_log(LOG_WARNING, "pam_set_item() failed, rc=%d, %s",
  185. rc, pam_strerror(pamHandlep, rc));
  186. goto cleanup;
  187. }
  188. #ifdef HAVE_PAM_FAIL_DELAY
  189. /* We have our own random delay code already, disable PAM's */
  190. (void) pam_fail_delay(pamHandlep, 0 /* musec_delay */);
  191. #endif
  192. /* (void) pam_set_item(pamHandlep, PAM_FAIL_DELAY, (void*) pamDelayFunc); */
  193. if ((rc = pam_authenticate(pamHandlep, 0)) != PAM_SUCCESS) {
  194. dropbear_log(LOG_WARNING, "pam_authenticate() failed, rc=%d, %s",
  195. rc, pam_strerror(pamHandlep, rc));
  196. dropbear_log(LOG_WARNING,
  197. "Bad PAM password attempt for '%s' from %s",
  198. ses.authstate.pw_name,
  199. svr_ses.addrstring);
  200. send_msg_userauth_failure(0, 1);
  201. goto cleanup;
  202. }
  203. if ((rc = pam_acct_mgmt(pamHandlep, 0)) != PAM_SUCCESS) {
  204. dropbear_log(LOG_WARNING, "pam_acct_mgmt() failed, rc=%d, %s",
  205. rc, pam_strerror(pamHandlep, rc));
  206. dropbear_log(LOG_WARNING,
  207. "Bad PAM password attempt for '%s' from %s",
  208. ses.authstate.pw_name,
  209. svr_ses.addrstring);
  210. send_msg_userauth_failure(0, 1);
  211. goto cleanup;
  212. }
  213. /* successful authentication */
  214. dropbear_log(LOG_NOTICE, "PAM password auth succeeded for '%s' from %s",
  215. ses.authstate.pw_name,
  216. svr_ses.addrstring);
  217. send_msg_userauth_success();
  218. cleanup:
  219. if (password != NULL) {
  220. m_burn(password, passwordlen);
  221. m_free(password);
  222. }
  223. if (pamHandlep != NULL) {
  224. TRACE(("pam_end"))
  225. (void) pam_end(pamHandlep, 0 /* pam_status */);
  226. }
  227. }
  228. #endif /* ENABLE_SVR_PAM_AUTH */