Damien Miller | e72b7af | 1999-12-30 15:08:44 +1100 | [diff] [blame] | 1 | /* |
Damien Miller | e69f18c | 2000-06-12 16:38:54 +1000 | [diff] [blame] | 2 | * Copyright (c) 2000 Damien Miller. All rights reserved. |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions |
| 6 | * are met: |
| 7 | * 1. Redistributions of source code must retain the above copyright |
| 8 | * notice, this list of conditions and the following disclaimer. |
| 9 | * 2. Redistributions in binary form must reproduce the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer in the |
| 11 | * documentation and/or other materials provided with the distribution. |
| 12 | * 3. All advertising materials mentioning features or use of this software |
| 13 | * must display the following acknowledgement: |
| 14 | * This product includes software developed by Markus Friedl. |
| 15 | * 4. The name of the author may not be used to endorse or promote products |
| 16 | * derived from this software without specific prior written permission. |
| 17 | * |
| 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
Damien Miller | e72b7af | 1999-12-30 15:08:44 +1100 | [diff] [blame] | 28 | */ |
| 29 | |
| 30 | #include "includes.h" |
| 31 | |
| 32 | #ifdef USE_PAM |
| 33 | #include "ssh.h" |
| 34 | #include "xmalloc.h" |
| 35 | #include "servconf.h" |
| 36 | |
Damien Miller | c19fd5f | 2000-06-22 21:44:54 +1000 | [diff] [blame] | 37 | RCSID("$Id: auth-pam.c,v 1.8 2000/06/22 11:44:54 djm Exp $"); |
Damien Miller | 2f6a0ad | 2000-05-31 11:20:11 +1000 | [diff] [blame] | 38 | |
| 39 | #define NEW_AUTHTOK_MSG \ |
| 40 | "Warning: You password has expired, please change it now" |
Damien Miller | e72b7af | 1999-12-30 15:08:44 +1100 | [diff] [blame] | 41 | |
| 42 | /* Callbacks */ |
| 43 | static int pamconv(int num_msg, const struct pam_message **msg, |
| 44 | struct pam_response **resp, void *appdata_ptr); |
| 45 | void pam_cleanup_proc(void *context); |
Damien Miller | 2f6a0ad | 2000-05-31 11:20:11 +1000 | [diff] [blame] | 46 | void pam_msg_cat(const char *msg); |
Damien Miller | e72b7af | 1999-12-30 15:08:44 +1100 | [diff] [blame] | 47 | |
| 48 | /* module-local variables */ |
| 49 | static struct pam_conv conv = { |
| 50 | pamconv, |
| 51 | NULL |
| 52 | }; |
| 53 | static struct pam_handle_t *pamh = NULL; |
| 54 | static const char *pampasswd = NULL; |
Damien Miller | 2f6a0ad | 2000-05-31 11:20:11 +1000 | [diff] [blame] | 55 | static char *pam_msg = NULL; |
Damien Miller | e72b7af | 1999-12-30 15:08:44 +1100 | [diff] [blame] | 56 | |
| 57 | /* PAM conversation function. This is really a kludge to get the password */ |
| 58 | /* into PAM and to pick up any messages generated by PAM into pamconv_msg */ |
| 59 | static int pamconv(int num_msg, const struct pam_message **msg, |
| 60 | struct pam_response **resp, void *appdata_ptr) |
| 61 | { |
| 62 | struct pam_response *reply; |
| 63 | int count; |
Damien Miller | e72b7af | 1999-12-30 15:08:44 +1100 | [diff] [blame] | 64 | |
| 65 | /* PAM will free this later */ |
| 66 | reply = malloc(num_msg * sizeof(*reply)); |
| 67 | if (reply == NULL) |
| 68 | return PAM_CONV_ERR; |
| 69 | |
| 70 | for(count = 0; count < num_msg; count++) { |
| 71 | switch (msg[count]->msg_style) { |
| 72 | case PAM_PROMPT_ECHO_OFF: |
| 73 | if (pampasswd == NULL) { |
| 74 | free(reply); |
| 75 | return PAM_CONV_ERR; |
| 76 | } |
| 77 | reply[count].resp_retcode = PAM_SUCCESS; |
| 78 | reply[count].resp = xstrdup(pampasswd); |
| 79 | break; |
Damien Miller | e72b7af | 1999-12-30 15:08:44 +1100 | [diff] [blame] | 80 | case PAM_TEXT_INFO: |
| 81 | reply[count].resp_retcode = PAM_SUCCESS; |
| 82 | reply[count].resp = xstrdup(""); |
| 83 | |
Damien Miller | 2f6a0ad | 2000-05-31 11:20:11 +1000 | [diff] [blame] | 84 | if (msg[count]->msg != NULL) |
| 85 | pam_msg_cat(msg[count]->msg); |
Damien Miller | e72b7af | 1999-12-30 15:08:44 +1100 | [diff] [blame] | 86 | |
Damien Miller | e72b7af | 1999-12-30 15:08:44 +1100 | [diff] [blame] | 87 | break; |
Damien Miller | e72b7af | 1999-12-30 15:08:44 +1100 | [diff] [blame] | 88 | default: |
| 89 | free(reply); |
| 90 | return PAM_CONV_ERR; |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | *resp = reply; |
| 95 | |
| 96 | return PAM_SUCCESS; |
| 97 | } |
| 98 | |
| 99 | /* Called at exit to cleanly shutdown PAM */ |
| 100 | void pam_cleanup_proc(void *context) |
| 101 | { |
| 102 | int pam_retval; |
| 103 | |
| 104 | if (pamh != NULL) |
| 105 | { |
| 106 | pam_retval = pam_close_session((pam_handle_t *)pamh, 0); |
| 107 | if (pam_retval != PAM_SUCCESS) { |
| 108 | log("Cannot close PAM session: %.200s", |
Damien Miller | 2f6a0ad | 2000-05-31 11:20:11 +1000 | [diff] [blame] | 109 | PAM_STRERROR((pam_handle_t *)pamh, pam_retval)); |
Damien Miller | e72b7af | 1999-12-30 15:08:44 +1100 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | pam_retval = pam_setcred((pam_handle_t *)pamh, PAM_DELETE_CRED); |
| 113 | if (pam_retval != PAM_SUCCESS) { |
| 114 | log("Cannot delete credentials: %.200s", |
Damien Miller | 2f6a0ad | 2000-05-31 11:20:11 +1000 | [diff] [blame] | 115 | PAM_STRERROR((pam_handle_t *)pamh, pam_retval)); |
Damien Miller | e72b7af | 1999-12-30 15:08:44 +1100 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | pam_retval = pam_end((pam_handle_t *)pamh, pam_retval); |
| 119 | if (pam_retval != PAM_SUCCESS) { |
| 120 | log("Cannot release PAM authentication: %.200s", |
Damien Miller | 2f6a0ad | 2000-05-31 11:20:11 +1000 | [diff] [blame] | 121 | PAM_STRERROR((pam_handle_t *)pamh, pam_retval)); |
Damien Miller | e72b7af | 1999-12-30 15:08:44 +1100 | [diff] [blame] | 122 | } |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | /* Attempt password authentation using PAM */ |
| 127 | int auth_pam_password(struct passwd *pw, const char *password) |
| 128 | { |
| 129 | extern ServerOptions options; |
| 130 | int pam_retval; |
| 131 | |
| 132 | /* deny if no user. */ |
| 133 | if (pw == NULL) |
| 134 | return 0; |
| 135 | if (pw->pw_uid == 0 && options.permit_root_login == 2) |
| 136 | return 0; |
| 137 | if (*password == '\0' && options.permit_empty_passwd == 0) |
| 138 | return 0; |
| 139 | |
| 140 | pampasswd = password; |
| 141 | |
| 142 | pam_retval = pam_authenticate((pam_handle_t *)pamh, 0); |
| 143 | if (pam_retval == PAM_SUCCESS) { |
Damien Miller | 2f6a0ad | 2000-05-31 11:20:11 +1000 | [diff] [blame] | 144 | debug("PAM Password authentication accepted for user \"%.100s\"", |
| 145 | pw->pw_name); |
Damien Miller | e72b7af | 1999-12-30 15:08:44 +1100 | [diff] [blame] | 146 | return 1; |
| 147 | } else { |
| 148 | debug("PAM Password authentication for \"%.100s\" failed: %s", |
| 149 | pw->pw_name, PAM_STRERROR((pam_handle_t *)pamh, pam_retval)); |
| 150 | return 0; |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | /* Do account management using PAM */ |
| 155 | int do_pam_account(char *username, char *remote_user) |
| 156 | { |
| 157 | int pam_retval; |
Damien Miller | 2f6a0ad | 2000-05-31 11:20:11 +1000 | [diff] [blame] | 158 | |
Damien Miller | e72b7af | 1999-12-30 15:08:44 +1100 | [diff] [blame] | 159 | debug("PAM setting rhost to \"%.200s\"", get_canonical_hostname()); |
| 160 | pam_retval = pam_set_item((pam_handle_t *)pamh, PAM_RHOST, |
| 161 | get_canonical_hostname()); |
| 162 | if (pam_retval != PAM_SUCCESS) { |
Damien Miller | 2f6a0ad | 2000-05-31 11:20:11 +1000 | [diff] [blame] | 163 | fatal("PAM set rhost failed: %.200s", |
| 164 | PAM_STRERROR((pam_handle_t *)pamh, pam_retval)); |
Damien Miller | e72b7af | 1999-12-30 15:08:44 +1100 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | if (remote_user != NULL) { |
| 168 | debug("PAM setting ruser to \"%.200s\"", remote_user); |
| 169 | pam_retval = pam_set_item((pam_handle_t *)pamh, PAM_RUSER, remote_user); |
| 170 | if (pam_retval != PAM_SUCCESS) { |
Damien Miller | 2f6a0ad | 2000-05-31 11:20:11 +1000 | [diff] [blame] | 171 | fatal("PAM set ruser failed: %.200s", |
| 172 | PAM_STRERROR((pam_handle_t *)pamh, pam_retval)); |
Damien Miller | e72b7af | 1999-12-30 15:08:44 +1100 | [diff] [blame] | 173 | } |
| 174 | } |
| 175 | |
| 176 | pam_retval = pam_acct_mgmt((pam_handle_t *)pamh, 0); |
Damien Miller | 2f6a0ad | 2000-05-31 11:20:11 +1000 | [diff] [blame] | 177 | switch (pam_retval) { |
| 178 | case PAM_SUCCESS: |
| 179 | /* This is what we want */ |
| 180 | break; |
| 181 | case PAM_NEW_AUTHTOK_REQD: |
| 182 | pam_msg_cat(NEW_AUTHTOK_MSG); |
| 183 | break; |
| 184 | default: |
| 185 | log("PAM rejected by account configuration: %.200s", |
| 186 | PAM_STRERROR((pam_handle_t *)pamh, pam_retval)); |
| 187 | return(0); |
Damien Miller | e72b7af | 1999-12-30 15:08:44 +1100 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | return(1); |
| 191 | } |
| 192 | |
| 193 | /* Do PAM-specific session initialisation */ |
Damien Miller | 3e955e7 | 2000-01-27 10:55:38 +1100 | [diff] [blame] | 194 | void do_pam_session(char *username, const char *ttyname) |
Damien Miller | e72b7af | 1999-12-30 15:08:44 +1100 | [diff] [blame] | 195 | { |
| 196 | int pam_retval; |
| 197 | |
| 198 | if (ttyname != NULL) { |
| 199 | debug("PAM setting tty to \"%.200s\"", ttyname); |
| 200 | pam_retval = pam_set_item((pam_handle_t *)pamh, PAM_TTY, ttyname); |
Damien Miller | 2f6a0ad | 2000-05-31 11:20:11 +1000 | [diff] [blame] | 201 | if (pam_retval != PAM_SUCCESS) { |
| 202 | fatal("PAM set tty failed: %.200s", |
| 203 | PAM_STRERROR((pam_handle_t *)pamh, pam_retval)); |
| 204 | } |
Damien Miller | e72b7af | 1999-12-30 15:08:44 +1100 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | pam_retval = pam_open_session((pam_handle_t *)pamh, 0); |
Damien Miller | 2f6a0ad | 2000-05-31 11:20:11 +1000 | [diff] [blame] | 208 | if (pam_retval != PAM_SUCCESS) { |
| 209 | fatal("PAM session setup failed: %.200s", |
| 210 | PAM_STRERROR((pam_handle_t *)pamh, pam_retval)); |
| 211 | } |
Damien Miller | e72b7af | 1999-12-30 15:08:44 +1100 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | /* Set PAM credentials */ |
| 215 | void do_pam_setcred() |
| 216 | { |
| 217 | int pam_retval; |
| 218 | |
| 219 | debug("PAM establishing creds"); |
| 220 | pam_retval = pam_setcred((pam_handle_t *)pamh, PAM_ESTABLISH_CRED); |
Damien Miller | 2f6a0ad | 2000-05-31 11:20:11 +1000 | [diff] [blame] | 221 | if (pam_retval != PAM_SUCCESS) { |
| 222 | fatal("PAM setcred failed: %.200s", |
| 223 | PAM_STRERROR((pam_handle_t *)pamh, pam_retval)); |
| 224 | } |
Damien Miller | e72b7af | 1999-12-30 15:08:44 +1100 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | /* Cleanly shutdown PAM */ |
| 228 | void finish_pam(void) |
| 229 | { |
| 230 | pam_cleanup_proc(NULL); |
| 231 | fatal_remove_cleanup(&pam_cleanup_proc, NULL); |
| 232 | } |
| 233 | |
| 234 | /* Start PAM authentication for specified account */ |
| 235 | void start_pam(struct passwd *pw) |
| 236 | { |
| 237 | int pam_retval; |
| 238 | |
| 239 | debug("Starting up PAM with username \"%.200s\"", pw->pw_name); |
| 240 | |
Damien Miller | d0cff3e | 2000-04-20 23:12:58 +1000 | [diff] [blame] | 241 | pam_retval = pam_start(SSHD_PAM_SERVICE, pw->pw_name, &conv, |
| 242 | (pam_handle_t**)&pamh); |
Damien Miller | e72b7af | 1999-12-30 15:08:44 +1100 | [diff] [blame] | 243 | |
Damien Miller | 2f6a0ad | 2000-05-31 11:20:11 +1000 | [diff] [blame] | 244 | if (pam_retval != PAM_SUCCESS) { |
| 245 | fatal("PAM initialisation failed: %.200s", |
| 246 | PAM_STRERROR((pam_handle_t *)pamh, pam_retval)); |
| 247 | } |
Damien Miller | c19fd5f | 2000-06-22 21:44:54 +1000 | [diff] [blame] | 248 | |
| 249 | /* |
| 250 | * Some PAM modules (e.g. pam_time) require a TTY to operate, |
| 251 | * and will fail in various stupid ways if they don't get one. |
| 252 | * sshd doesn't set the tty until too late in the auth process and may |
| 253 | * not even need one (for tty-less connections) |
| 254 | * Kludge: Set a fake PAM_TTY |
| 255 | */ |
Damien Miller | 7b22d65 | 2000-06-18 14:07:04 +1000 | [diff] [blame] | 256 | pam_retval = pam_set_item((pam_handle_t *)pamh, PAM_TTY, "ssh"); |
| 257 | if (pam_retval != PAM_SUCCESS) { |
| 258 | fatal("PAM set tty failed: %.200s", |
| 259 | PAM_STRERROR((pam_handle_t *)pamh, pam_retval)); |
| 260 | } |
| 261 | |
Damien Miller | e72b7af | 1999-12-30 15:08:44 +1100 | [diff] [blame] | 262 | fatal_add_cleanup(&pam_cleanup_proc, NULL); |
| 263 | } |
| 264 | |
| 265 | /* Return list of PAM enviornment strings */ |
| 266 | char **fetch_pam_environment(void) |
| 267 | { |
Damien Miller | 1bead33 | 2000-04-30 00:47:29 +1000 | [diff] [blame] | 268 | #ifdef HAVE_PAM_GETENVLIST |
Damien Miller | e72b7af | 1999-12-30 15:08:44 +1100 | [diff] [blame] | 269 | return(pam_getenvlist((pam_handle_t *)pamh)); |
Damien Miller | 1bead33 | 2000-04-30 00:47:29 +1000 | [diff] [blame] | 270 | #else /* HAVE_PAM_GETENVLIST */ |
| 271 | return(NULL); |
| 272 | #endif /* HAVE_PAM_GETENVLIST */ |
Damien Miller | e72b7af | 1999-12-30 15:08:44 +1100 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | /* Print any messages that have been generated during authentication */ |
| 276 | /* or account checking to stderr */ |
| 277 | void print_pam_messages(void) |
| 278 | { |
Damien Miller | 2f6a0ad | 2000-05-31 11:20:11 +1000 | [diff] [blame] | 279 | if (pam_msg != NULL) |
| 280 | fprintf(stderr, pam_msg); |
| 281 | } |
| 282 | |
| 283 | /* Append a message to the PAM message buffer */ |
| 284 | void pam_msg_cat(const char *msg) |
| 285 | { |
| 286 | char *p; |
| 287 | size_t new_msg_len; |
| 288 | size_t pam_msg_len; |
| 289 | |
| 290 | new_msg_len = strlen(msg); |
| 291 | |
| 292 | if (pam_msg) { |
| 293 | pam_msg_len = strlen(pam_msg); |
| 294 | pam_msg = xrealloc(pam_msg, new_msg_len + pam_msg_len + 2); |
| 295 | p = pam_msg + pam_msg_len; |
| 296 | } else { |
| 297 | pam_msg = p = xmalloc(new_msg_len + 2); |
| 298 | } |
| 299 | |
| 300 | memcpy(p, msg, new_msg_len); |
| 301 | p[new_msg_len] = '\n'; |
| 302 | p[new_msg_len + 1] = '\0'; |
Damien Miller | e72b7af | 1999-12-30 15:08:44 +1100 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | #endif /* USE_PAM */ |