]>
Commit | Line | Data |
---|---|---|
0fff78ff | 1 | /*- |
2 | * Copyright (c) 2002 Networks Associates Technology, Inc. | |
3 | * All rights reserved. | |
4 | * | |
5 | * This software was developed for the FreeBSD Project by ThinkSec AS and | |
6 | * NAI Labs, the Security Research Division of Network Associates, Inc. | |
7 | * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the | |
8 | * DARPA CHATS research program. | |
3c0ef626 | 9 | * |
10 | * Redistribution and use in source and binary forms, with or without | |
11 | * modification, are permitted provided that the following conditions | |
12 | * are met: | |
13 | * 1. Redistributions of source code must retain the above copyright | |
14 | * notice, this list of conditions and the following disclaimer. | |
15 | * 2. Redistributions in binary form must reproduce the above copyright | |
16 | * notice, this list of conditions and the following disclaimer in the | |
17 | * documentation and/or other materials provided with the distribution. | |
18 | * | |
0fff78ff | 19 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
20 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE | |
23 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
24 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
25 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
27 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
28 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
29 | * SUCH DAMAGE. | |
3c0ef626 | 30 | */ |
c9f39d2c | 31 | /* |
32 | * Copyright (c) 2003,2004 Damien Miller <djm@mindrot.org> | |
33 | * Copyright (c) 2003,2004 Darren Tucker <dtucker@zip.com.au> | |
34 | * | |
35 | * Permission to use, copy, modify, and distribute this software for any | |
36 | * purpose with or without fee is hereby granted, provided that the above | |
37 | * copyright notice and this permission notice appear in all copies. | |
38 | * | |
39 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | |
40 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | |
41 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | |
42 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | |
43 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | |
44 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | |
45 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | |
46 | */ | |
3c0ef626 | 47 | |
0fff78ff | 48 | /* Based on $FreeBSD: src/crypto/openssh/auth2-pam-freebsd.c,v 1.11 2003/03/31 13:48:18 des Exp $ */ |
3c0ef626 | 49 | #include "includes.h" |
0fff78ff | 50 | RCSID("$Id$"); |
3c0ef626 | 51 | |
52 | #ifdef USE_PAM | |
cdd66111 | 53 | #if defined(HAVE_SECURITY_PAM_APPL_H) |
0fff78ff | 54 | #include <security/pam_appl.h> |
cdd66111 | 55 | #elif defined (HAVE_PAM_PAM_APPL_H) |
56 | #include <pam/pam_appl.h> | |
57 | #endif | |
0fff78ff | 58 | |
700318f3 | 59 | #include "auth.h" |
3c0ef626 | 60 | #include "auth-pam.h" |
0fff78ff | 61 | #include "buffer.h" |
62 | #include "bufaux.h" | |
3c0ef626 | 63 | #include "canohost.h" |
0fff78ff | 64 | #include "log.h" |
65 | #include "monitor_wrap.h" | |
66 | #include "msg.h" | |
67 | #include "packet.h" | |
c9f39d2c | 68 | #include "misc.h" |
0fff78ff | 69 | #include "servconf.h" |
70 | #include "ssh2.h" | |
71 | #include "xmalloc.h" | |
72 | #include "auth-options.h" | |
3c0ef626 | 73 | |
0fff78ff | 74 | extern ServerOptions options; |
cdd66111 | 75 | extern Buffer loginmsg; |
76 | extern int compat20; | |
99be0775 | 77 | extern u_int utmp_len; |
41b2f314 | 78 | |
0fff78ff | 79 | #ifdef USE_POSIX_THREADS |
80 | #include <pthread.h> | |
81 | /* | |
cdd66111 | 82 | * Avoid namespace clash when *not* using pthreads for systems *with* |
83 | * pthreads, which unconditionally define pthread_t via sys/types.h | |
0fff78ff | 84 | * (e.g. Linux) |
85 | */ | |
cdd66111 | 86 | typedef pthread_t sp_pthread_t; |
0fff78ff | 87 | #else |
cdd66111 | 88 | typedef pid_t sp_pthread_t; |
89 | #endif | |
90 | ||
91 | struct pam_ctxt { | |
92 | sp_pthread_t pam_thread; | |
93 | int pam_psock; | |
94 | int pam_csock; | |
95 | int pam_done; | |
96 | }; | |
97 | ||
98 | static void sshpam_free_ctx(void *); | |
99 | static struct pam_ctxt *cleanup_ctxt; | |
100 | ||
101 | #ifndef USE_POSIX_THREADS | |
0fff78ff | 102 | /* |
103 | * Simulate threads with processes. | |
104 | */ | |
cdd66111 | 105 | |
106 | static int sshpam_thread_status = -1; | |
107 | static mysig_t sshpam_oldsig; | |
108 | ||
109 | static void | |
110 | sshpam_sigchld_handler(int sig) | |
111 | { | |
c9f39d2c | 112 | signal(SIGCHLD, SIG_DFL); |
cdd66111 | 113 | if (cleanup_ctxt == NULL) |
114 | return; /* handler called after PAM cleanup, shouldn't happen */ | |
c9f39d2c | 115 | if (waitpid(cleanup_ctxt->pam_thread, &sshpam_thread_status, WNOHANG) |
116 | <= 0) { | |
117 | /* PAM thread has not exitted, privsep slave must have */ | |
118 | kill(cleanup_ctxt->pam_thread, SIGTERM); | |
119 | if (waitpid(cleanup_ctxt->pam_thread, &sshpam_thread_status, 0) | |
120 | <= 0) | |
121 | return; /* could not wait */ | |
122 | } | |
cdd66111 | 123 | if (WIFSIGNALED(sshpam_thread_status) && |
124 | WTERMSIG(sshpam_thread_status) == SIGTERM) | |
125 | return; /* terminated by pthread_cancel */ | |
126 | if (!WIFEXITED(sshpam_thread_status)) | |
127 | fatal("PAM: authentication thread exited unexpectedly"); | |
128 | if (WEXITSTATUS(sshpam_thread_status) != 0) | |
129 | fatal("PAM: authentication thread exited uncleanly"); | |
130 | } | |
3c0ef626 | 131 | |
0fff78ff | 132 | static void |
133 | pthread_exit(void *value __unused) | |
134 | { | |
135 | _exit(0); | |
136 | } | |
3c0ef626 | 137 | |
0fff78ff | 138 | static int |
139 | pthread_create(sp_pthread_t *thread, const void *attr __unused, | |
140 | void *(*thread_start)(void *), void *arg) | |
141 | { | |
142 | pid_t pid; | |
143 | ||
99be0775 | 144 | sshpam_thread_status = -1; |
0fff78ff | 145 | switch ((pid = fork())) { |
146 | case -1: | |
147 | error("fork(): %s", strerror(errno)); | |
148 | return (-1); | |
149 | case 0: | |
150 | thread_start(arg); | |
151 | _exit(1); | |
152 | default: | |
153 | *thread = pid; | |
cdd66111 | 154 | sshpam_oldsig = signal(SIGCHLD, sshpam_sigchld_handler); |
0fff78ff | 155 | return (0); |
156 | } | |
157 | } | |
3c0ef626 | 158 | |
0fff78ff | 159 | static int |
160 | pthread_cancel(sp_pthread_t thread) | |
3c0ef626 | 161 | { |
cdd66111 | 162 | signal(SIGCHLD, sshpam_oldsig); |
0fff78ff | 163 | return (kill(thread, SIGTERM)); |
3c0ef626 | 164 | } |
165 | ||
0fff78ff | 166 | static int |
167 | pthread_join(sp_pthread_t thread, void **value __unused) | |
3c0ef626 | 168 | { |
0fff78ff | 169 | int status; |
170 | ||
cdd66111 | 171 | if (sshpam_thread_status != -1) |
172 | return (sshpam_thread_status); | |
173 | signal(SIGCHLD, sshpam_oldsig); | |
0fff78ff | 174 | waitpid(thread, &status, 0); |
175 | return (status); | |
3c0ef626 | 176 | } |
0fff78ff | 177 | #endif |
178 | ||
179 | ||
acc3d05e | 180 | static pam_handle_t *sshpam_handle = NULL; |
181 | static int sshpam_err = 0; | |
182 | static int sshpam_authenticated = 0; | |
acc3d05e | 183 | static int sshpam_session_open = 0; |
184 | static int sshpam_cred_established = 0; | |
cdd66111 | 185 | static int sshpam_account_status = -1; |
186 | static char **sshpam_env = NULL; | |
99be0775 | 187 | static Authctxt *sshpam_authctxt = NULL; |
c9f39d2c | 188 | static const char *sshpam_password = NULL; |
996d5e62 | 189 | static char badpw[] = "\b\n\r\177INCORRECT"; |
cdd66111 | 190 | |
191 | /* Some PAM implementations don't implement this */ | |
192 | #ifndef HAVE_PAM_GETENVLIST | |
193 | static char ** | |
194 | pam_getenvlist(pam_handle_t *pamh) | |
195 | { | |
196 | /* | |
197 | * XXX - If necessary, we can still support envrionment passing | |
198 | * for platforms without pam_getenvlist by searching for known | |
199 | * env vars (e.g. KRB5CCNAME) from the PAM environment. | |
200 | */ | |
201 | return NULL; | |
202 | } | |
203 | #endif | |
0fff78ff | 204 | |
c9f39d2c | 205 | /* |
206 | * Some platforms, notably Solaris, do not enforce password complexity | |
207 | * rules during pam_chauthtok() if the real uid of the calling process | |
208 | * is 0, on the assumption that it's being called by "passwd" run by root. | |
209 | * This wraps pam_chauthtok and sets/restore the real uid so PAM will do | |
210 | * the right thing. | |
211 | */ | |
212 | #ifdef SSHPAM_CHAUTHTOK_NEEDS_RUID | |
213 | static int | |
214 | sshpam_chauthtok_ruid(pam_handle_t *pamh, int flags) | |
215 | { | |
216 | int result; | |
217 | ||
218 | if (sshpam_authctxt == NULL) | |
219 | fatal("PAM: sshpam_authctxt not initialized"); | |
220 | if (setreuid(sshpam_authctxt->pw->pw_uid, -1) == -1) | |
221 | fatal("%s: setreuid failed: %s", __func__, strerror(errno)); | |
222 | result = pam_chauthtok(pamh, flags); | |
223 | if (setreuid(0, -1) == -1) | |
224 | fatal("%s: setreuid failed: %s", __func__, strerror(errno)); | |
225 | return result; | |
226 | } | |
227 | # define pam_chauthtok(a,b) (sshpam_chauthtok_ruid((a), (b))) | |
228 | #endif | |
229 | ||
cdd66111 | 230 | void |
c9f39d2c | 231 | sshpam_password_change_required(int reqd) |
cdd66111 | 232 | { |
233 | debug3("%s %d", __func__, reqd); | |
99be0775 | 234 | if (sshpam_authctxt == NULL) |
235 | fatal("%s: PAM authctxt not initialized", __func__); | |
236 | sshpam_authctxt->force_pwchange = reqd; | |
cdd66111 | 237 | if (reqd) { |
238 | no_port_forwarding_flag |= 2; | |
239 | no_agent_forwarding_flag |= 2; | |
240 | no_x11_forwarding_flag |= 2; | |
241 | } else { | |
242 | no_port_forwarding_flag &= ~2; | |
243 | no_agent_forwarding_flag &= ~2; | |
244 | no_x11_forwarding_flag &= ~2; | |
245 | } | |
246 | } | |
0fff78ff | 247 | |
cdd66111 | 248 | /* Import regular and PAM environment from subprocess */ |
249 | static void | |
250 | import_environments(Buffer *b) | |
251 | { | |
252 | char *env; | |
253 | u_int i, num_env; | |
254 | int err; | |
255 | ||
256 | debug3("PAM: %s entering", __func__); | |
257 | ||
99be0775 | 258 | #ifndef USE_POSIX_THREADS |
cdd66111 | 259 | /* Import variables set by do_pam_account */ |
260 | sshpam_account_status = buffer_get_int(b); | |
c9f39d2c | 261 | sshpam_password_change_required(buffer_get_int(b)); |
cdd66111 | 262 | |
263 | /* Import environment from subprocess */ | |
264 | num_env = buffer_get_int(b); | |
265 | sshpam_env = xmalloc((num_env + 1) * sizeof(*sshpam_env)); | |
266 | debug3("PAM: num env strings %d", num_env); | |
267 | for(i = 0; i < num_env; i++) | |
268 | sshpam_env[i] = buffer_get_string(b, NULL); | |
269 | ||
270 | sshpam_env[num_env] = NULL; | |
271 | ||
272 | /* Import PAM environment from subprocess */ | |
273 | num_env = buffer_get_int(b); | |
274 | debug("PAM: num PAM env strings %d", num_env); | |
275 | for(i = 0; i < num_env; i++) { | |
276 | env = buffer_get_string(b, NULL); | |
277 | ||
278 | #ifdef HAVE_PAM_PUTENV | |
279 | /* Errors are not fatal here */ | |
280 | if ((err = pam_putenv(sshpam_handle, env)) != PAM_SUCCESS) { | |
281 | error("PAM: pam_putenv: %s", | |
282 | pam_strerror(sshpam_handle, sshpam_err)); | |
283 | } | |
284 | #endif | |
285 | } | |
99be0775 | 286 | #endif |
cdd66111 | 287 | } |
3c0ef626 | 288 | |
289 | /* | |
0fff78ff | 290 | * Conversation function for authentication thread. |
3c0ef626 | 291 | */ |
0fff78ff | 292 | static int |
c9f39d2c | 293 | sshpam_thread_conv(int n, struct pam_message **msg, |
0fff78ff | 294 | struct pam_response **resp, void *data) |
3c0ef626 | 295 | { |
0fff78ff | 296 | Buffer buffer; |
297 | struct pam_ctxt *ctxt; | |
acc3d05e | 298 | struct pam_response *reply; |
0fff78ff | 299 | int i; |
300 | ||
cdd66111 | 301 | debug3("PAM: %s entering, %d messages", __func__, n); |
acc3d05e | 302 | *resp = NULL; |
303 | ||
c9f39d2c | 304 | if (data == NULL) { |
305 | error("PAM: conversation function passed a null context"); | |
306 | return (PAM_CONV_ERR); | |
307 | } | |
0fff78ff | 308 | ctxt = data; |
309 | if (n <= 0 || n > PAM_MAX_NUM_MSG) | |
310 | return (PAM_CONV_ERR); | |
acc3d05e | 311 | |
312 | if ((reply = malloc(n * sizeof(*reply))) == NULL) | |
313 | return (PAM_CONV_ERR); | |
314 | memset(reply, 0, n * sizeof(*reply)); | |
315 | ||
0fff78ff | 316 | buffer_init(&buffer); |
317 | for (i = 0; i < n; ++i) { | |
0fff78ff | 318 | switch (PAM_MSG_MEMBER(msg, i, msg_style)) { |
319 | case PAM_PROMPT_ECHO_OFF: | |
cdd66111 | 320 | buffer_put_cstring(&buffer, |
acc3d05e | 321 | PAM_MSG_MEMBER(msg, i, msg)); |
cdd66111 | 322 | if (ssh_msg_send(ctxt->pam_csock, |
323 | PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1) | |
324 | goto fail; | |
325 | if (ssh_msg_recv(ctxt->pam_csock, &buffer) == -1) | |
326 | goto fail; | |
0fff78ff | 327 | if (buffer_get_char(&buffer) != PAM_AUTHTOK) |
328 | goto fail; | |
acc3d05e | 329 | reply[i].resp = buffer_get_string(&buffer, NULL); |
0fff78ff | 330 | break; |
331 | case PAM_PROMPT_ECHO_ON: | |
cdd66111 | 332 | buffer_put_cstring(&buffer, |
acc3d05e | 333 | PAM_MSG_MEMBER(msg, i, msg)); |
cdd66111 | 334 | if (ssh_msg_send(ctxt->pam_csock, |
335 | PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1) | |
336 | goto fail; | |
337 | if (ssh_msg_recv(ctxt->pam_csock, &buffer) == -1) | |
338 | goto fail; | |
0fff78ff | 339 | if (buffer_get_char(&buffer) != PAM_AUTHTOK) |
340 | goto fail; | |
acc3d05e | 341 | reply[i].resp = buffer_get_string(&buffer, NULL); |
0fff78ff | 342 | break; |
343 | case PAM_ERROR_MSG: | |
cdd66111 | 344 | buffer_put_cstring(&buffer, |
acc3d05e | 345 | PAM_MSG_MEMBER(msg, i, msg)); |
cdd66111 | 346 | if (ssh_msg_send(ctxt->pam_csock, |
347 | PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1) | |
348 | goto fail; | |
0fff78ff | 349 | break; |
350 | case PAM_TEXT_INFO: | |
cdd66111 | 351 | buffer_put_cstring(&buffer, |
acc3d05e | 352 | PAM_MSG_MEMBER(msg, i, msg)); |
cdd66111 | 353 | if (ssh_msg_send(ctxt->pam_csock, |
354 | PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1) | |
355 | goto fail; | |
0fff78ff | 356 | break; |
357 | default: | |
358 | goto fail; | |
3c0ef626 | 359 | } |
0fff78ff | 360 | buffer_clear(&buffer); |
3c0ef626 | 361 | } |
0fff78ff | 362 | buffer_free(&buffer); |
acc3d05e | 363 | *resp = reply; |
0fff78ff | 364 | return (PAM_SUCCESS); |
acc3d05e | 365 | |
0fff78ff | 366 | fail: |
acc3d05e | 367 | for(i = 0; i < n; i++) { |
368 | if (reply[i].resp != NULL) | |
369 | xfree(reply[i].resp); | |
370 | } | |
371 | xfree(reply); | |
0fff78ff | 372 | buffer_free(&buffer); |
373 | return (PAM_CONV_ERR); | |
3c0ef626 | 374 | } |
375 | ||
0fff78ff | 376 | /* |
377 | * Authentication thread. | |
378 | */ | |
379 | static void * | |
380 | sshpam_thread(void *ctxtp) | |
3c0ef626 | 381 | { |
0fff78ff | 382 | struct pam_ctxt *ctxt = ctxtp; |
383 | Buffer buffer; | |
384 | struct pam_conv sshpam_conv; | |
c9f39d2c | 385 | int flags = (options.permit_empty_passwd == 0 ? |
386 | PAM_DISALLOW_NULL_AUTHTOK : 0); | |
0fff78ff | 387 | #ifndef USE_POSIX_THREADS |
cdd66111 | 388 | extern char **environ; |
389 | char **env_from_pam; | |
390 | u_int i; | |
0fff78ff | 391 | const char *pam_user; |
392 | ||
c9f39d2c | 393 | pam_get_item(sshpam_handle, PAM_USER, (void **)&pam_user); |
cdd66111 | 394 | environ[0] = NULL; |
c9f39d2c | 395 | |
396 | if (sshpam_authctxt != NULL) { | |
397 | setproctitle("%s [pam]", | |
398 | sshpam_authctxt->valid ? pam_user : "unknown"); | |
399 | } | |
0fff78ff | 400 | #endif |
3c0ef626 | 401 | |
0fff78ff | 402 | sshpam_conv.conv = sshpam_thread_conv; |
403 | sshpam_conv.appdata_ptr = ctxt; | |
404 | ||
99be0775 | 405 | if (sshpam_authctxt == NULL) |
406 | fatal("%s: PAM authctxt not initialized", __func__); | |
407 | ||
0fff78ff | 408 | buffer_init(&buffer); |
409 | sshpam_err = pam_set_item(sshpam_handle, PAM_CONV, | |
410 | (const void *)&sshpam_conv); | |
411 | if (sshpam_err != PAM_SUCCESS) | |
412 | goto auth_fail; | |
c9f39d2c | 413 | sshpam_err = pam_authenticate(sshpam_handle, flags); |
0fff78ff | 414 | if (sshpam_err != PAM_SUCCESS) |
415 | goto auth_fail; | |
cdd66111 | 416 | |
417 | if (compat20) { | |
418 | if (!do_pam_account()) | |
419 | goto auth_fail; | |
99be0775 | 420 | if (sshpam_authctxt->force_pwchange) { |
cdd66111 | 421 | sshpam_err = pam_chauthtok(sshpam_handle, |
422 | PAM_CHANGE_EXPIRED_AUTHTOK); | |
423 | if (sshpam_err != PAM_SUCCESS) | |
424 | goto auth_fail; | |
c9f39d2c | 425 | sshpam_password_change_required(0); |
cdd66111 | 426 | } |
427 | } | |
428 | ||
0fff78ff | 429 | buffer_put_cstring(&buffer, "OK"); |
cdd66111 | 430 | |
431 | #ifndef USE_POSIX_THREADS | |
432 | /* Export variables set by do_pam_account */ | |
433 | buffer_put_int(&buffer, sshpam_account_status); | |
99be0775 | 434 | buffer_put_int(&buffer, sshpam_authctxt->force_pwchange); |
cdd66111 | 435 | |
436 | /* Export any environment strings set in child */ | |
437 | for(i = 0; environ[i] != NULL; i++) | |
438 | ; /* Count */ | |
439 | buffer_put_int(&buffer, i); | |
440 | for(i = 0; environ[i] != NULL; i++) | |
441 | buffer_put_cstring(&buffer, environ[i]); | |
442 | ||
443 | /* Export any environment strings set by PAM in child */ | |
444 | env_from_pam = pam_getenvlist(sshpam_handle); | |
445 | for(i = 0; env_from_pam != NULL && env_from_pam[i] != NULL; i++) | |
446 | ; /* Count */ | |
447 | buffer_put_int(&buffer, i); | |
448 | for(i = 0; env_from_pam != NULL && env_from_pam[i] != NULL; i++) | |
449 | buffer_put_cstring(&buffer, env_from_pam[i]); | |
450 | #endif /* USE_POSIX_THREADS */ | |
451 | ||
452 | /* XXX - can't do much about an error here */ | |
0fff78ff | 453 | ssh_msg_send(ctxt->pam_csock, sshpam_err, &buffer); |
454 | buffer_free(&buffer); | |
455 | pthread_exit(NULL); | |
456 | ||
457 | auth_fail: | |
458 | buffer_put_cstring(&buffer, | |
459 | pam_strerror(sshpam_handle, sshpam_err)); | |
cdd66111 | 460 | /* XXX - can't do much about an error here */ |
0fff78ff | 461 | ssh_msg_send(ctxt->pam_csock, PAM_AUTH_ERR, &buffer); |
462 | buffer_free(&buffer); | |
463 | pthread_exit(NULL); | |
cdd66111 | 464 | |
0fff78ff | 465 | return (NULL); /* Avoid warning for non-pthread case */ |
466 | } | |
3c0ef626 | 467 | |
cdd66111 | 468 | void |
469 | sshpam_thread_cleanup(void) | |
0fff78ff | 470 | { |
cdd66111 | 471 | struct pam_ctxt *ctxt = cleanup_ctxt; |
472 | ||
473 | debug3("PAM: %s entering", __func__); | |
474 | if (ctxt != NULL && ctxt->pam_thread != 0) { | |
475 | pthread_cancel(ctxt->pam_thread); | |
476 | pthread_join(ctxt->pam_thread, NULL); | |
477 | close(ctxt->pam_psock); | |
478 | close(ctxt->pam_csock); | |
479 | memset(ctxt, 0, sizeof(*ctxt)); | |
480 | cleanup_ctxt = NULL; | |
481 | } | |
3c0ef626 | 482 | } |
483 | ||
0fff78ff | 484 | static int |
c9f39d2c | 485 | sshpam_null_conv(int n, struct pam_message **msg, |
0fff78ff | 486 | struct pam_response **resp, void *data) |
3c0ef626 | 487 | { |
cdd66111 | 488 | debug3("PAM: %s entering, %d messages", __func__, n); |
0fff78ff | 489 | return (PAM_CONV_ERR); |
490 | } | |
3c0ef626 | 491 | |
0fff78ff | 492 | static struct pam_conv null_conv = { sshpam_null_conv, NULL }; |
3c0ef626 | 493 | |
996d5e62 | 494 | static int |
495 | sshpam_store_conv(int n, struct pam_message **msg, | |
496 | struct pam_response **resp, void *data) | |
497 | { | |
498 | struct pam_response *reply; | |
499 | int i; | |
500 | size_t len; | |
501 | ||
502 | debug3("PAM: %s called with %d messages", __func__, n); | |
503 | *resp = NULL; | |
504 | ||
505 | if (n <= 0 || n > PAM_MAX_NUM_MSG) | |
506 | return (PAM_CONV_ERR); | |
507 | ||
508 | if ((reply = malloc(n * sizeof(*reply))) == NULL) | |
509 | return (PAM_CONV_ERR); | |
510 | memset(reply, 0, n * sizeof(*reply)); | |
511 | ||
512 | for (i = 0; i < n; ++i) { | |
513 | switch (PAM_MSG_MEMBER(msg, i, msg_style)) { | |
514 | case PAM_ERROR_MSG: | |
515 | case PAM_TEXT_INFO: | |
516 | len = strlen(PAM_MSG_MEMBER(msg, i, msg)); | |
517 | buffer_append(&loginmsg, PAM_MSG_MEMBER(msg, i, msg), len); | |
518 | buffer_append(&loginmsg, "\n", 1 ); | |
519 | reply[i].resp_retcode = PAM_SUCCESS; | |
520 | break; | |
521 | default: | |
522 | goto fail; | |
523 | } | |
524 | } | |
525 | *resp = reply; | |
526 | return (PAM_SUCCESS); | |
527 | ||
528 | fail: | |
529 | for(i = 0; i < n; i++) { | |
530 | if (reply[i].resp != NULL) | |
531 | xfree(reply[i].resp); | |
532 | } | |
533 | xfree(reply); | |
534 | return (PAM_CONV_ERR); | |
535 | } | |
536 | ||
537 | static struct pam_conv store_conv = { sshpam_store_conv, NULL }; | |
538 | ||
cdd66111 | 539 | void |
540 | sshpam_cleanup(void) | |
0fff78ff | 541 | { |
0fff78ff | 542 | debug("PAM: cleanup"); |
acc3d05e | 543 | if (sshpam_handle == NULL) |
544 | return; | |
0fff78ff | 545 | pam_set_item(sshpam_handle, PAM_CONV, (const void *)&null_conv); |
546 | if (sshpam_cred_established) { | |
547 | pam_setcred(sshpam_handle, PAM_DELETE_CRED); | |
548 | sshpam_cred_established = 0; | |
549 | } | |
550 | if (sshpam_session_open) { | |
551 | pam_close_session(sshpam_handle, PAM_SILENT); | |
552 | sshpam_session_open = 0; | |
553 | } | |
cdd66111 | 554 | sshpam_authenticated = 0; |
0fff78ff | 555 | pam_end(sshpam_handle, sshpam_err); |
556 | sshpam_handle = NULL; | |
557 | } | |
3c0ef626 | 558 | |
0fff78ff | 559 | static int |
99be0775 | 560 | sshpam_init(Authctxt *authctxt) |
0fff78ff | 561 | { |
0fff78ff | 562 | extern char *__progname; |
99be0775 | 563 | const char *pam_rhost, *pam_user, *user = authctxt->user; |
0fff78ff | 564 | |
565 | if (sshpam_handle != NULL) { | |
566 | /* We already have a PAM context; check if the user matches */ | |
567 | sshpam_err = pam_get_item(sshpam_handle, | |
c9f39d2c | 568 | PAM_USER, (void **)&pam_user); |
0fff78ff | 569 | if (sshpam_err == PAM_SUCCESS && strcmp(user, pam_user) == 0) |
570 | return (0); | |
0fff78ff | 571 | pam_end(sshpam_handle, sshpam_err); |
572 | sshpam_handle = NULL; | |
3c0ef626 | 573 | } |
0fff78ff | 574 | debug("PAM: initializing for \"%s\"", user); |
575 | sshpam_err = | |
996d5e62 | 576 | pam_start(SSHD_PAM_SERVICE, user, &store_conv, &sshpam_handle); |
99be0775 | 577 | sshpam_authctxt = authctxt; |
578 | ||
0fff78ff | 579 | if (sshpam_err != PAM_SUCCESS) { |
580 | pam_end(sshpam_handle, sshpam_err); | |
581 | sshpam_handle = NULL; | |
582 | return (-1); | |
583 | } | |
584 | pam_rhost = get_remote_name_or_ip(utmp_len, options.use_dns); | |
585 | debug("PAM: setting PAM_RHOST to \"%s\"", pam_rhost); | |
586 | sshpam_err = pam_set_item(sshpam_handle, PAM_RHOST, pam_rhost); | |
587 | if (sshpam_err != PAM_SUCCESS) { | |
588 | pam_end(sshpam_handle, sshpam_err); | |
589 | sshpam_handle = NULL; | |
590 | return (-1); | |
591 | } | |
592 | #ifdef PAM_TTY_KLUDGE | |
cdd66111 | 593 | /* |
594 | * Some silly PAM modules (e.g. pam_time) require a TTY to operate. | |
595 | * sshd doesn't set the tty until too late in the auth process and | |
0fff78ff | 596 | * may not even set one (for tty-less connections) |
cdd66111 | 597 | */ |
0fff78ff | 598 | debug("PAM: setting PAM_TTY to \"ssh\""); |
599 | sshpam_err = pam_set_item(sshpam_handle, PAM_TTY, "ssh"); | |
600 | if (sshpam_err != PAM_SUCCESS) { | |
601 | pam_end(sshpam_handle, sshpam_err); | |
602 | sshpam_handle = NULL; | |
603 | return (-1); | |
604 | } | |
605 | #endif | |
0fff78ff | 606 | return (0); |
3c0ef626 | 607 | } |
608 | ||
0fff78ff | 609 | static void * |
610 | sshpam_init_ctx(Authctxt *authctxt) | |
3c0ef626 | 611 | { |
0fff78ff | 612 | struct pam_ctxt *ctxt; |
613 | int socks[2]; | |
3c0ef626 | 614 | |
cdd66111 | 615 | debug3("PAM: %s entering", __func__); |
0fff78ff | 616 | /* Refuse to start if we don't have PAM enabled */ |
617 | if (!options.use_pam) | |
618 | return NULL; | |
3c0ef626 | 619 | |
0fff78ff | 620 | /* Initialize PAM */ |
99be0775 | 621 | if (sshpam_init(authctxt) == -1) { |
0fff78ff | 622 | error("PAM: initialization failed"); |
623 | return (NULL); | |
3c0ef626 | 624 | } |
625 | ||
0fff78ff | 626 | ctxt = xmalloc(sizeof *ctxt); |
cdd66111 | 627 | memset(ctxt, 0, sizeof(*ctxt)); |
628 | ||
0fff78ff | 629 | /* Start the authentication thread */ |
630 | if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, socks) == -1) { | |
631 | error("PAM: failed create sockets: %s", strerror(errno)); | |
632 | xfree(ctxt); | |
633 | return (NULL); | |
634 | } | |
635 | ctxt->pam_psock = socks[0]; | |
636 | ctxt->pam_csock = socks[1]; | |
637 | if (pthread_create(&ctxt->pam_thread, NULL, sshpam_thread, ctxt) == -1) { | |
638 | error("PAM: failed to start authentication thread: %s", | |
639 | strerror(errno)); | |
640 | close(socks[0]); | |
641 | close(socks[1]); | |
642 | xfree(ctxt); | |
643 | return (NULL); | |
644 | } | |
cdd66111 | 645 | cleanup_ctxt = ctxt; |
0fff78ff | 646 | return (ctxt); |
647 | } | |
648 | ||
649 | static int | |
650 | sshpam_query(void *ctx, char **name, char **info, | |
651 | u_int *num, char ***prompts, u_int **echo_on) | |
652 | { | |
653 | Buffer buffer; | |
654 | struct pam_ctxt *ctxt = ctx; | |
655 | size_t plen; | |
656 | u_char type; | |
657 | char *msg; | |
996d5e62 | 658 | size_t len, mlen; |
0fff78ff | 659 | |
cdd66111 | 660 | debug3("PAM: %s entering", __func__); |
0fff78ff | 661 | buffer_init(&buffer); |
662 | *name = xstrdup(""); | |
663 | *info = xstrdup(""); | |
664 | *prompts = xmalloc(sizeof(char *)); | |
665 | **prompts = NULL; | |
666 | plen = 0; | |
667 | *echo_on = xmalloc(sizeof(u_int)); | |
668 | while (ssh_msg_recv(ctxt->pam_psock, &buffer) == 0) { | |
669 | type = buffer_get_char(&buffer); | |
670 | msg = buffer_get_string(&buffer, NULL); | |
996d5e62 | 671 | mlen = strlen(msg); |
0fff78ff | 672 | switch (type) { |
673 | case PAM_PROMPT_ECHO_ON: | |
674 | case PAM_PROMPT_ECHO_OFF: | |
675 | *num = 1; | |
996d5e62 | 676 | len = plen + mlen + 1; |
0fff78ff | 677 | **prompts = xrealloc(**prompts, len); |
996d5e62 | 678 | strlcpy(**prompts + plen, msg, len - plen); |
679 | plen += mlen; | |
0fff78ff | 680 | **echo_on = (type == PAM_PROMPT_ECHO_ON); |
681 | xfree(msg); | |
682 | return (0); | |
683 | case PAM_ERROR_MSG: | |
684 | case PAM_TEXT_INFO: | |
685 | /* accumulate messages */ | |
996d5e62 | 686 | len = plen + mlen + 2; |
0fff78ff | 687 | **prompts = xrealloc(**prompts, len); |
996d5e62 | 688 | strlcpy(**prompts + plen, msg, len - plen); |
689 | plen += mlen; | |
690 | strlcat(**prompts + plen, "\n", len - plen); | |
691 | plen++; | |
0fff78ff | 692 | xfree(msg); |
3c0ef626 | 693 | break; |
0fff78ff | 694 | case PAM_SUCCESS: |
695 | case PAM_AUTH_ERR: | |
696 | if (**prompts != NULL) { | |
697 | /* drain any accumulated messages */ | |
cdd66111 | 698 | debug("PAM: %s", **prompts); |
699 | buffer_append(&loginmsg, **prompts, | |
700 | strlen(**prompts)); | |
0fff78ff | 701 | xfree(**prompts); |
702 | **prompts = NULL; | |
703 | } | |
704 | if (type == PAM_SUCCESS) { | |
996d5e62 | 705 | if (!sshpam_authctxt->valid || |
706 | (sshpam_authctxt->pw->pw_uid == 0 && | |
707 | options.permit_root_login != PERMIT_YES)) | |
708 | fatal("Internal error: PAM auth " | |
709 | "succeeded when it should have " | |
710 | "failed"); | |
cdd66111 | 711 | import_environments(&buffer); |
0fff78ff | 712 | *num = 0; |
713 | **echo_on = 0; | |
714 | ctxt->pam_done = 1; | |
715 | xfree(msg); | |
716 | return (0); | |
717 | } | |
99be0775 | 718 | error("PAM: %s for %s%.100s from %.100s", msg, |
719 | sshpam_authctxt->valid ? "" : "illegal user ", | |
720 | sshpam_authctxt->user, | |
721 | get_remote_name_or_ip(utmp_len, options.use_dns)); | |
cdd66111 | 722 | /* FALLTHROUGH */ |
3c0ef626 | 723 | default: |
0fff78ff | 724 | *num = 0; |
725 | **echo_on = 0; | |
726 | xfree(msg); | |
727 | ctxt->pam_done = -1; | |
728 | return (-1); | |
729 | } | |
3c0ef626 | 730 | } |
0fff78ff | 731 | return (-1); |
732 | } | |
3c0ef626 | 733 | |
0fff78ff | 734 | /* XXX - see also comment in auth-chall.c:verify_response */ |
735 | static int | |
736 | sshpam_respond(void *ctx, u_int num, char **resp) | |
737 | { | |
738 | Buffer buffer; | |
739 | struct pam_ctxt *ctxt = ctx; | |
740 | ||
cdd66111 | 741 | debug2("PAM: %s entering, %d responses", __func__, num); |
0fff78ff | 742 | switch (ctxt->pam_done) { |
743 | case 1: | |
744 | sshpam_authenticated = 1; | |
745 | return (0); | |
746 | case 0: | |
747 | break; | |
748 | default: | |
749 | return (-1); | |
750 | } | |
751 | if (num != 1) { | |
752 | error("PAM: expected one response, got %u", num); | |
753 | return (-1); | |
754 | } | |
755 | buffer_init(&buffer); | |
996d5e62 | 756 | if (sshpam_authctxt->valid && |
757 | (sshpam_authctxt->pw->pw_uid != 0 || | |
758 | options.permit_root_login == PERMIT_YES)) | |
759 | buffer_put_cstring(&buffer, *resp); | |
760 | else | |
761 | buffer_put_cstring(&buffer, badpw); | |
cdd66111 | 762 | if (ssh_msg_send(ctxt->pam_psock, PAM_AUTHTOK, &buffer) == -1) { |
763 | buffer_free(&buffer); | |
764 | return (-1); | |
765 | } | |
0fff78ff | 766 | buffer_free(&buffer); |
767 | return (1); | |
3c0ef626 | 768 | } |
769 | ||
0fff78ff | 770 | static void |
771 | sshpam_free_ctx(void *ctxtp) | |
3c0ef626 | 772 | { |
0fff78ff | 773 | struct pam_ctxt *ctxt = ctxtp; |
3c0ef626 | 774 | |
cdd66111 | 775 | debug3("PAM: %s entering", __func__); |
776 | sshpam_thread_cleanup(); | |
0fff78ff | 777 | xfree(ctxt); |
778 | /* | |
779 | * We don't call sshpam_cleanup() here because we may need the PAM | |
780 | * handle at a later stage, e.g. when setting up a session. It's | |
781 | * still on the cleanup list, so pam_end() *will* be called before | |
782 | * the server process terminates. | |
783 | */ | |
784 | } | |
3c0ef626 | 785 | |
0fff78ff | 786 | KbdintDevice sshpam_device = { |
787 | "pam", | |
788 | sshpam_init_ctx, | |
789 | sshpam_query, | |
790 | sshpam_respond, | |
791 | sshpam_free_ctx | |
792 | }; | |
3c0ef626 | 793 | |
0fff78ff | 794 | KbdintDevice mm_sshpam_device = { |
795 | "pam", | |
796 | mm_sshpam_init_ctx, | |
797 | mm_sshpam_query, | |
798 | mm_sshpam_respond, | |
799 | mm_sshpam_free_ctx | |
800 | }; | |
3c0ef626 | 801 | |
0fff78ff | 802 | /* |
803 | * This replaces auth-pam.c | |
804 | */ | |
805 | void | |
99be0775 | 806 | start_pam(Authctxt *authctxt) |
0fff78ff | 807 | { |
808 | if (!options.use_pam) | |
809 | fatal("PAM: initialisation requested when UsePAM=no"); | |
810 | ||
99be0775 | 811 | if (sshpam_init(authctxt) == -1) |
0fff78ff | 812 | fatal("PAM: initialisation failed"); |
3c0ef626 | 813 | } |
814 | ||
0fff78ff | 815 | void |
816 | finish_pam(void) | |
3c0ef626 | 817 | { |
cdd66111 | 818 | sshpam_cleanup(); |
0fff78ff | 819 | } |
3c0ef626 | 820 | |
0fff78ff | 821 | u_int |
822 | do_pam_account(void) | |
823 | { | |
996d5e62 | 824 | debug("%s: called", __func__); |
cdd66111 | 825 | if (sshpam_account_status != -1) |
826 | return (sshpam_account_status); | |
827 | ||
0fff78ff | 828 | sshpam_err = pam_acct_mgmt(sshpam_handle, 0); |
996d5e62 | 829 | debug3("PAM: %s pam_acct_mgmt = %d (%s)", __func__, sshpam_err, |
830 | pam_strerror(sshpam_handle, sshpam_err)); | |
0fff78ff | 831 | |
cdd66111 | 832 | if (sshpam_err != PAM_SUCCESS && sshpam_err != PAM_NEW_AUTHTOK_REQD) { |
833 | sshpam_account_status = 0; | |
834 | return (sshpam_account_status); | |
0fff78ff | 835 | } |
700318f3 | 836 | |
cdd66111 | 837 | if (sshpam_err == PAM_NEW_AUTHTOK_REQD) |
c9f39d2c | 838 | sshpam_password_change_required(1); |
3c0ef626 | 839 | |
cdd66111 | 840 | sshpam_account_status = 1; |
841 | return (sshpam_account_status); | |
3c0ef626 | 842 | } |
843 | ||
0fff78ff | 844 | void |
845 | do_pam_set_tty(const char *tty) | |
3c0ef626 | 846 | { |
0fff78ff | 847 | if (tty != NULL) { |
848 | debug("PAM: setting PAM_TTY to \"%s\"", tty); | |
849 | sshpam_err = pam_set_item(sshpam_handle, PAM_TTY, tty); | |
850 | if (sshpam_err != PAM_SUCCESS) | |
851 | fatal("PAM: failed to set PAM_TTY: %s", | |
852 | pam_strerror(sshpam_handle, sshpam_err)); | |
3c0ef626 | 853 | } |
854 | } | |
855 | ||
0fff78ff | 856 | void |
857 | do_pam_setcred(int init) | |
3c0ef626 | 858 | { |
0fff78ff | 859 | sshpam_err = pam_set_item(sshpam_handle, PAM_CONV, |
996d5e62 | 860 | (const void *)&store_conv); |
0fff78ff | 861 | if (sshpam_err != PAM_SUCCESS) |
862 | fatal("PAM: failed to set PAM_CONV: %s", | |
863 | pam_strerror(sshpam_handle, sshpam_err)); | |
864 | if (init) { | |
865 | debug("PAM: establishing credentials"); | |
866 | sshpam_err = pam_setcred(sshpam_handle, PAM_ESTABLISH_CRED); | |
867 | } else { | |
868 | debug("PAM: reinitializing credentials"); | |
869 | sshpam_err = pam_setcred(sshpam_handle, PAM_REINITIALIZE_CRED); | |
870 | } | |
871 | if (sshpam_err == PAM_SUCCESS) { | |
872 | sshpam_cred_established = 1; | |
873 | return; | |
874 | } | |
875 | if (sshpam_authenticated) | |
876 | fatal("PAM: pam_setcred(): %s", | |
877 | pam_strerror(sshpam_handle, sshpam_err)); | |
878 | else | |
879 | debug("PAM: pam_setcred(): %s", | |
880 | pam_strerror(sshpam_handle, sshpam_err)); | |
3c0ef626 | 881 | } |
882 | ||
0fff78ff | 883 | static int |
c9f39d2c | 884 | sshpam_tty_conv(int n, struct pam_message **msg, |
0fff78ff | 885 | struct pam_response **resp, void *data) |
886 | { | |
887 | char input[PAM_MAX_MSG_SIZE]; | |
acc3d05e | 888 | struct pam_response *reply; |
0fff78ff | 889 | int i; |
3c0ef626 | 890 | |
cdd66111 | 891 | debug3("PAM: %s called with %d messages", __func__, n); |
892 | ||
acc3d05e | 893 | *resp = NULL; |
894 | ||
cdd66111 | 895 | if (n <= 0 || n > PAM_MAX_NUM_MSG || !isatty(STDIN_FILENO)) |
0fff78ff | 896 | return (PAM_CONV_ERR); |
acc3d05e | 897 | |
898 | if ((reply = malloc(n * sizeof(*reply))) == NULL) | |
899 | return (PAM_CONV_ERR); | |
900 | memset(reply, 0, n * sizeof(*reply)); | |
901 | ||
0fff78ff | 902 | for (i = 0; i < n; ++i) { |
903 | switch (PAM_MSG_MEMBER(msg, i, msg_style)) { | |
904 | case PAM_PROMPT_ECHO_OFF: | |
acc3d05e | 905 | reply[i].resp = |
cdd66111 | 906 | read_passphrase(PAM_MSG_MEMBER(msg, i, msg), |
0fff78ff | 907 | RP_ALLOW_STDIN); |
acc3d05e | 908 | reply[i].resp_retcode = PAM_SUCCESS; |
0fff78ff | 909 | break; |
910 | case PAM_PROMPT_ECHO_ON: | |
cdd66111 | 911 | fprintf(stderr, "%s\n", PAM_MSG_MEMBER(msg, i, msg)); |
0fff78ff | 912 | fgets(input, sizeof input, stdin); |
c9f39d2c | 913 | if ((reply[i].resp = strdup(input)) == NULL) |
914 | goto fail; | |
acc3d05e | 915 | reply[i].resp_retcode = PAM_SUCCESS; |
0fff78ff | 916 | break; |
917 | case PAM_ERROR_MSG: | |
918 | case PAM_TEXT_INFO: | |
cdd66111 | 919 | fprintf(stderr, "%s\n", PAM_MSG_MEMBER(msg, i, msg)); |
acc3d05e | 920 | reply[i].resp_retcode = PAM_SUCCESS; |
0fff78ff | 921 | break; |
922 | default: | |
923 | goto fail; | |
924 | } | |
925 | } | |
acc3d05e | 926 | *resp = reply; |
0fff78ff | 927 | return (PAM_SUCCESS); |
acc3d05e | 928 | |
0fff78ff | 929 | fail: |
acc3d05e | 930 | for(i = 0; i < n; i++) { |
931 | if (reply[i].resp != NULL) | |
932 | xfree(reply[i].resp); | |
933 | } | |
934 | xfree(reply); | |
0fff78ff | 935 | return (PAM_CONV_ERR); |
3c0ef626 | 936 | } |
937 | ||
c9f39d2c | 938 | static struct pam_conv tty_conv = { sshpam_tty_conv, NULL }; |
cdd66111 | 939 | |
0fff78ff | 940 | /* |
941 | * XXX this should be done in the authentication phase, but ssh1 doesn't | |
942 | * support that | |
943 | */ | |
944 | void | |
945 | do_pam_chauthtok(void) | |
3c0ef626 | 946 | { |
0fff78ff | 947 | if (use_privsep) |
948 | fatal("Password expired (unable to change with privsep)"); | |
949 | sshpam_err = pam_set_item(sshpam_handle, PAM_CONV, | |
cdd66111 | 950 | (const void *)&tty_conv); |
0fff78ff | 951 | if (sshpam_err != PAM_SUCCESS) |
952 | fatal("PAM: failed to set PAM_CONV: %s", | |
953 | pam_strerror(sshpam_handle, sshpam_err)); | |
954 | debug("PAM: changing password"); | |
955 | sshpam_err = pam_chauthtok(sshpam_handle, PAM_CHANGE_EXPIRED_AUTHTOK); | |
956 | if (sshpam_err != PAM_SUCCESS) | |
957 | fatal("PAM: pam_chauthtok(): %s", | |
958 | pam_strerror(sshpam_handle, sshpam_err)); | |
3c0ef626 | 959 | } |
960 | ||
cdd66111 | 961 | void |
962 | do_pam_session(void) | |
963 | { | |
964 | debug3("PAM: opening session"); | |
965 | sshpam_err = pam_set_item(sshpam_handle, PAM_CONV, | |
966 | (const void *)&store_conv); | |
967 | if (sshpam_err != PAM_SUCCESS) | |
968 | fatal("PAM: failed to set PAM_CONV: %s", | |
969 | pam_strerror(sshpam_handle, sshpam_err)); | |
970 | sshpam_err = pam_open_session(sshpam_handle, 0); | |
996d5e62 | 971 | if (sshpam_err == PAM_SUCCESS) |
972 | sshpam_session_open = 1; | |
973 | else { | |
974 | sshpam_session_open = 0; | |
975 | disable_forwarding(); | |
976 | error("PAM: pam_open_session(): %s", | |
cdd66111 | 977 | pam_strerror(sshpam_handle, sshpam_err)); |
996d5e62 | 978 | } |
979 | ||
980 | } | |
981 | ||
982 | int | |
983 | is_pam_session_open(void) | |
984 | { | |
985 | return sshpam_session_open; | |
cdd66111 | 986 | } |
987 | ||
988 | /* | |
0fff78ff | 989 | * Set a PAM environment string. We need to do this so that the session |
990 | * modules can handle things like Kerberos/GSI credentials that appear | |
991 | * during the ssh authentication process. | |
992 | */ | |
0fff78ff | 993 | int |
cdd66111 | 994 | do_pam_putenv(char *name, char *value) |
41b2f314 | 995 | { |
0fff78ff | 996 | int ret = 1; |
cdd66111 | 997 | #ifdef HAVE_PAM_PUTENV |
0fff78ff | 998 | char *compound; |
999 | size_t len; | |
41b2f314 | 1000 | |
0fff78ff | 1001 | len = strlen(name) + strlen(value) + 2; |
1002 | compound = xmalloc(len); | |
1003 | ||
1004 | snprintf(compound, len, "%s=%s", name, value); | |
1005 | ret = pam_putenv(sshpam_handle, compound); | |
1006 | xfree(compound); | |
1007 | #endif | |
1008 | ||
1009 | return (ret); | |
41b2f314 | 1010 | } |
1011 | ||
cdd66111 | 1012 | char ** |
1013 | fetch_pam_child_environment(void) | |
3c0ef626 | 1014 | { |
cdd66111 | 1015 | return sshpam_env; |
3c0ef626 | 1016 | } |
1017 | ||
0fff78ff | 1018 | char ** |
1019 | fetch_pam_environment(void) | |
3c0ef626 | 1020 | { |
0fff78ff | 1021 | return (pam_getenvlist(sshpam_handle)); |
0fff78ff | 1022 | } |
3c0ef626 | 1023 | |
0fff78ff | 1024 | void |
1025 | free_pam_environment(char **env) | |
1026 | { | |
1027 | char **envp; | |
3c0ef626 | 1028 | |
0fff78ff | 1029 | if (env == NULL) |
1030 | return; | |
3c0ef626 | 1031 | |
0fff78ff | 1032 | for (envp = env; *envp; envp++) |
1033 | xfree(*envp); | |
1034 | xfree(env); | |
3c0ef626 | 1035 | } |
1036 | ||
c9f39d2c | 1037 | /* |
1038 | * "Blind" conversation function for password authentication. Assumes that | |
1039 | * echo-off prompts are for the password and stores messages for later | |
1040 | * display. | |
1041 | */ | |
1042 | static int | |
1043 | sshpam_passwd_conv(int n, struct pam_message **msg, | |
1044 | struct pam_response **resp, void *data) | |
1045 | { | |
1046 | struct pam_response *reply; | |
1047 | int i; | |
1048 | size_t len; | |
1049 | ||
1050 | debug3("PAM: %s called with %d messages", __func__, n); | |
1051 | ||
1052 | *resp = NULL; | |
1053 | ||
1054 | if (n <= 0 || n > PAM_MAX_NUM_MSG) | |
1055 | return (PAM_CONV_ERR); | |
1056 | ||
1057 | if ((reply = malloc(n * sizeof(*reply))) == NULL) | |
1058 | return (PAM_CONV_ERR); | |
1059 | memset(reply, 0, n * sizeof(*reply)); | |
1060 | ||
1061 | for (i = 0; i < n; ++i) { | |
1062 | switch (PAM_MSG_MEMBER(msg, i, msg_style)) { | |
1063 | case PAM_PROMPT_ECHO_OFF: | |
1064 | if (sshpam_password == NULL) | |
1065 | goto fail; | |
1066 | if ((reply[i].resp = strdup(sshpam_password)) == NULL) | |
1067 | goto fail; | |
1068 | reply[i].resp_retcode = PAM_SUCCESS; | |
1069 | break; | |
1070 | case PAM_ERROR_MSG: | |
1071 | case PAM_TEXT_INFO: | |
1072 | len = strlen(PAM_MSG_MEMBER(msg, i, msg)); | |
1073 | if (len > 0) { | |
1074 | buffer_append(&loginmsg, | |
1075 | PAM_MSG_MEMBER(msg, i, msg), len); | |
1076 | buffer_append(&loginmsg, "\n", 1); | |
1077 | } | |
1078 | if ((reply[i].resp = strdup("")) == NULL) | |
1079 | goto fail; | |
1080 | reply[i].resp_retcode = PAM_SUCCESS; | |
1081 | break; | |
1082 | default: | |
1083 | goto fail; | |
1084 | } | |
1085 | } | |
1086 | *resp = reply; | |
1087 | return (PAM_SUCCESS); | |
1088 | ||
1089 | fail: | |
1090 | for(i = 0; i < n; i++) { | |
1091 | if (reply[i].resp != NULL) | |
1092 | xfree(reply[i].resp); | |
1093 | } | |
1094 | xfree(reply); | |
1095 | return (PAM_CONV_ERR); | |
1096 | } | |
1097 | ||
1098 | static struct pam_conv passwd_conv = { sshpam_passwd_conv, NULL }; | |
1099 | ||
1100 | /* | |
1101 | * Attempt password authentication via PAM | |
1102 | */ | |
1103 | int | |
1104 | sshpam_auth_passwd(Authctxt *authctxt, const char *password) | |
1105 | { | |
1106 | int flags = (options.permit_empty_passwd == 0 ? | |
1107 | PAM_DISALLOW_NULL_AUTHTOK : 0); | |
c9f39d2c | 1108 | |
1109 | if (!options.use_pam || sshpam_handle == NULL) | |
1110 | fatal("PAM: %s called when PAM disabled or failed to " | |
1111 | "initialise.", __func__); | |
1112 | ||
1113 | sshpam_password = password; | |
1114 | sshpam_authctxt = authctxt; | |
1115 | ||
1116 | /* | |
1117 | * If the user logging in is invalid, or is root but is not permitted | |
1118 | * by PermitRootLogin, use an invalid password to prevent leaking | |
1119 | * information via timing (eg if the PAM config has a delay on fail). | |
1120 | */ | |
1121 | if (!authctxt->valid || (authctxt->pw->pw_uid == 0 && | |
1122 | options.permit_root_login != PERMIT_YES)) | |
1123 | sshpam_password = badpw; | |
1124 | ||
1125 | sshpam_err = pam_set_item(sshpam_handle, PAM_CONV, | |
1126 | (const void *)&passwd_conv); | |
1127 | if (sshpam_err != PAM_SUCCESS) | |
1128 | fatal("PAM: %s: failed to set PAM_CONV: %s", __func__, | |
1129 | pam_strerror(sshpam_handle, sshpam_err)); | |
1130 | ||
1131 | sshpam_err = pam_authenticate(sshpam_handle, flags); | |
1132 | sshpam_password = NULL; | |
1133 | if (sshpam_err == PAM_SUCCESS && authctxt->valid) { | |
1134 | debug("PAM: password authentication accepted for %.100s", | |
1135 | authctxt->user); | |
1136 | return 1; | |
1137 | } else { | |
1138 | debug("PAM: password authentication failed for %.100s: %s", | |
1139 | authctxt->valid ? authctxt->user : "an illegal user", | |
1140 | pam_strerror(sshpam_handle, sshpam_err)); | |
1141 | return 0; | |
1142 | } | |
1143 | } | |
3c0ef626 | 1144 | #endif /* USE_PAM */ |