]>
Commit | Line | Data |
---|---|---|
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. | |
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 | * | |
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. | |
30 | */ | |
31 | ||
32 | /* Based on $FreeBSD: src/crypto/openssh/auth2-pam-freebsd.c,v 1.11 2003/03/31 13:48:18 des Exp $ */ | |
33 | #include "includes.h" | |
34 | RCSID("$Id$"); | |
35 | ||
36 | #ifdef USE_PAM | |
37 | #if defined(HAVE_SECURITY_PAM_APPL_H) | |
38 | #include <security/pam_appl.h> | |
39 | #elif defined (HAVE_PAM_PAM_APPL_H) | |
40 | #include <pam/pam_appl.h> | |
41 | #endif | |
42 | ||
43 | #include "auth.h" | |
44 | #include "auth-pam.h" | |
45 | #include "buffer.h" | |
46 | #include "bufaux.h" | |
47 | #include "canohost.h" | |
48 | #include "log.h" | |
49 | #include "monitor_wrap.h" | |
50 | #include "msg.h" | |
51 | #include "packet.h" | |
52 | #include "readpass.h" | |
53 | #include "servconf.h" | |
54 | #include "ssh2.h" | |
55 | #include "xmalloc.h" | |
56 | #include "auth-options.h" | |
57 | ||
58 | extern ServerOptions options; | |
59 | extern Buffer loginmsg; | |
60 | extern int compat20; | |
61 | ||
62 | #ifdef USE_POSIX_THREADS | |
63 | #include <pthread.h> | |
64 | /* | |
65 | * Avoid namespace clash when *not* using pthreads for systems *with* | |
66 | * pthreads, which unconditionally define pthread_t via sys/types.h | |
67 | * (e.g. Linux) | |
68 | */ | |
69 | typedef pthread_t sp_pthread_t; | |
70 | #else | |
71 | typedef pid_t sp_pthread_t; | |
72 | #endif | |
73 | ||
74 | struct pam_ctxt { | |
75 | sp_pthread_t pam_thread; | |
76 | int pam_psock; | |
77 | int pam_csock; | |
78 | int pam_done; | |
79 | }; | |
80 | ||
81 | static void sshpam_free_ctx(void *); | |
82 | static struct pam_ctxt *cleanup_ctxt; | |
83 | ||
84 | #ifndef USE_POSIX_THREADS | |
85 | /* | |
86 | * Simulate threads with processes. | |
87 | */ | |
88 | ||
89 | static int sshpam_thread_status = -1; | |
90 | static mysig_t sshpam_oldsig; | |
91 | ||
92 | static void | |
93 | sshpam_sigchld_handler(int sig) | |
94 | { | |
95 | if (cleanup_ctxt == NULL) | |
96 | return; /* handler called after PAM cleanup, shouldn't happen */ | |
97 | if (waitpid(cleanup_ctxt->pam_thread, &sshpam_thread_status, 0) == -1) | |
98 | return; /* couldn't wait for process */ | |
99 | if (WIFSIGNALED(sshpam_thread_status) && | |
100 | WTERMSIG(sshpam_thread_status) == SIGTERM) | |
101 | return; /* terminated by pthread_cancel */ | |
102 | if (!WIFEXITED(sshpam_thread_status)) | |
103 | fatal("PAM: authentication thread exited unexpectedly"); | |
104 | if (WEXITSTATUS(sshpam_thread_status) != 0) | |
105 | fatal("PAM: authentication thread exited uncleanly"); | |
106 | } | |
107 | ||
108 | static void | |
109 | pthread_exit(void *value __unused) | |
110 | { | |
111 | _exit(0); | |
112 | } | |
113 | ||
114 | static int | |
115 | pthread_create(sp_pthread_t *thread, const void *attr __unused, | |
116 | void *(*thread_start)(void *), void *arg) | |
117 | { | |
118 | pid_t pid; | |
119 | ||
120 | sshpam_thread_status = -1; | |
121 | switch ((pid = fork())) { | |
122 | case -1: | |
123 | error("fork(): %s", strerror(errno)); | |
124 | return (-1); | |
125 | case 0: | |
126 | thread_start(arg); | |
127 | _exit(1); | |
128 | default: | |
129 | *thread = pid; | |
130 | sshpam_oldsig = signal(SIGCHLD, sshpam_sigchld_handler); | |
131 | return (0); | |
132 | } | |
133 | } | |
134 | ||
135 | static int | |
136 | pthread_cancel(sp_pthread_t thread) | |
137 | { | |
138 | signal(SIGCHLD, sshpam_oldsig); | |
139 | return (kill(thread, SIGTERM)); | |
140 | } | |
141 | ||
142 | static int | |
143 | pthread_join(sp_pthread_t thread, void **value __unused) | |
144 | { | |
145 | int status; | |
146 | ||
147 | if (sshpam_thread_status != -1) | |
148 | return (sshpam_thread_status); | |
149 | signal(SIGCHLD, sshpam_oldsig); | |
150 | waitpid(thread, &status, 0); | |
151 | return (status); | |
152 | } | |
153 | #endif | |
154 | ||
155 | ||
156 | static pam_handle_t *sshpam_handle = NULL; | |
157 | static int sshpam_err = 0; | |
158 | static int sshpam_authenticated = 0; | |
159 | static int sshpam_session_open = 0; | |
160 | static int sshpam_cred_established = 0; | |
161 | static int sshpam_account_status = -1; | |
162 | static char **sshpam_env = NULL; | |
163 | static Authctxt *the_authctxt = NULL; | |
164 | ||
165 | /* Some PAM implementations don't implement this */ | |
166 | #ifndef HAVE_PAM_GETENVLIST | |
167 | static char ** | |
168 | pam_getenvlist(pam_handle_t *pamh) | |
169 | { | |
170 | /* | |
171 | * XXX - If necessary, we can still support envrionment passing | |
172 | * for platforms without pam_getenvlist by searching for known | |
173 | * env vars (e.g. KRB5CCNAME) from the PAM environment. | |
174 | */ | |
175 | return NULL; | |
176 | } | |
177 | #endif | |
178 | ||
179 | void | |
180 | pam_password_change_required(int reqd) | |
181 | { | |
182 | debug3("%s %d", __func__, reqd); | |
183 | if (the_authctxt == NULL) | |
184 | fatal("%s: PAM authctxt not initialized", __func__); | |
185 | the_authctxt->force_pwchange = reqd; | |
186 | if (reqd) { | |
187 | no_port_forwarding_flag |= 2; | |
188 | no_agent_forwarding_flag |= 2; | |
189 | no_x11_forwarding_flag |= 2; | |
190 | } else { | |
191 | no_port_forwarding_flag &= ~2; | |
192 | no_agent_forwarding_flag &= ~2; | |
193 | no_x11_forwarding_flag &= ~2; | |
194 | } | |
195 | } | |
196 | ||
197 | /* Import regular and PAM environment from subprocess */ | |
198 | static void | |
199 | import_environments(Buffer *b) | |
200 | { | |
201 | char *env; | |
202 | u_int i, num_env; | |
203 | int err; | |
204 | ||
205 | debug3("PAM: %s entering", __func__); | |
206 | ||
207 | #ifndef USE_POSIX_THREADS | |
208 | /* Import variables set by do_pam_account */ | |
209 | sshpam_account_status = buffer_get_int(b); | |
210 | pam_password_change_required(buffer_get_int(b)); | |
211 | ||
212 | /* Import environment from subprocess */ | |
213 | num_env = buffer_get_int(b); | |
214 | sshpam_env = xmalloc((num_env + 1) * sizeof(*sshpam_env)); | |
215 | debug3("PAM: num env strings %d", num_env); | |
216 | for(i = 0; i < num_env; i++) | |
217 | sshpam_env[i] = buffer_get_string(b, NULL); | |
218 | ||
219 | sshpam_env[num_env] = NULL; | |
220 | ||
221 | /* Import PAM environment from subprocess */ | |
222 | num_env = buffer_get_int(b); | |
223 | debug("PAM: num PAM env strings %d", num_env); | |
224 | for(i = 0; i < num_env; i++) { | |
225 | env = buffer_get_string(b, NULL); | |
226 | ||
227 | #ifdef HAVE_PAM_PUTENV | |
228 | /* Errors are not fatal here */ | |
229 | if ((err = pam_putenv(sshpam_handle, env)) != PAM_SUCCESS) { | |
230 | error("PAM: pam_putenv: %s", | |
231 | pam_strerror(sshpam_handle, sshpam_err)); | |
232 | } | |
233 | #endif | |
234 | } | |
235 | #endif | |
236 | } | |
237 | ||
238 | /* | |
239 | * Conversation function for authentication thread. | |
240 | */ | |
241 | static int | |
242 | sshpam_thread_conv(int n, const struct pam_message **msg, | |
243 | struct pam_response **resp, void *data) | |
244 | { | |
245 | Buffer buffer; | |
246 | struct pam_ctxt *ctxt; | |
247 | struct pam_response *reply; | |
248 | int i; | |
249 | ||
250 | debug3("PAM: %s entering, %d messages", __func__, n); | |
251 | *resp = NULL; | |
252 | ||
253 | ctxt = data; | |
254 | if (n <= 0 || n > PAM_MAX_NUM_MSG) | |
255 | return (PAM_CONV_ERR); | |
256 | ||
257 | if ((reply = malloc(n * sizeof(*reply))) == NULL) | |
258 | return (PAM_CONV_ERR); | |
259 | memset(reply, 0, n * sizeof(*reply)); | |
260 | ||
261 | buffer_init(&buffer); | |
262 | for (i = 0; i < n; ++i) { | |
263 | switch (PAM_MSG_MEMBER(msg, i, msg_style)) { | |
264 | case PAM_PROMPT_ECHO_OFF: | |
265 | buffer_put_cstring(&buffer, | |
266 | PAM_MSG_MEMBER(msg, i, msg)); | |
267 | if (ssh_msg_send(ctxt->pam_csock, | |
268 | PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1) | |
269 | goto fail; | |
270 | if (ssh_msg_recv(ctxt->pam_csock, &buffer) == -1) | |
271 | goto fail; | |
272 | if (buffer_get_char(&buffer) != PAM_AUTHTOK) | |
273 | goto fail; | |
274 | reply[i].resp = buffer_get_string(&buffer, NULL); | |
275 | break; | |
276 | case PAM_PROMPT_ECHO_ON: | |
277 | buffer_put_cstring(&buffer, | |
278 | PAM_MSG_MEMBER(msg, i, msg)); | |
279 | if (ssh_msg_send(ctxt->pam_csock, | |
280 | PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1) | |
281 | goto fail; | |
282 | if (ssh_msg_recv(ctxt->pam_csock, &buffer) == -1) | |
283 | goto fail; | |
284 | if (buffer_get_char(&buffer) != PAM_AUTHTOK) | |
285 | goto fail; | |
286 | reply[i].resp = buffer_get_string(&buffer, NULL); | |
287 | break; | |
288 | case PAM_ERROR_MSG: | |
289 | buffer_put_cstring(&buffer, | |
290 | PAM_MSG_MEMBER(msg, i, msg)); | |
291 | if (ssh_msg_send(ctxt->pam_csock, | |
292 | PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1) | |
293 | goto fail; | |
294 | break; | |
295 | case PAM_TEXT_INFO: | |
296 | buffer_put_cstring(&buffer, | |
297 | PAM_MSG_MEMBER(msg, i, msg)); | |
298 | if (ssh_msg_send(ctxt->pam_csock, | |
299 | PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1) | |
300 | goto fail; | |
301 | break; | |
302 | default: | |
303 | goto fail; | |
304 | } | |
305 | buffer_clear(&buffer); | |
306 | } | |
307 | buffer_free(&buffer); | |
308 | *resp = reply; | |
309 | return (PAM_SUCCESS); | |
310 | ||
311 | fail: | |
312 | for(i = 0; i < n; i++) { | |
313 | if (reply[i].resp != NULL) | |
314 | xfree(reply[i].resp); | |
315 | } | |
316 | xfree(reply); | |
317 | buffer_free(&buffer); | |
318 | return (PAM_CONV_ERR); | |
319 | } | |
320 | ||
321 | /* | |
322 | * Authentication thread. | |
323 | */ | |
324 | static void * | |
325 | sshpam_thread(void *ctxtp) | |
326 | { | |
327 | struct pam_ctxt *ctxt = ctxtp; | |
328 | Buffer buffer; | |
329 | struct pam_conv sshpam_conv; | |
330 | #ifndef USE_POSIX_THREADS | |
331 | extern char **environ; | |
332 | char **env_from_pam; | |
333 | u_int i; | |
334 | const char *pam_user; | |
335 | ||
336 | pam_get_item(sshpam_handle, PAM_USER, (const void **)&pam_user); | |
337 | setproctitle("%s [pam]", pam_user); | |
338 | environ[0] = NULL; | |
339 | #endif | |
340 | ||
341 | sshpam_conv.conv = sshpam_thread_conv; | |
342 | sshpam_conv.appdata_ptr = ctxt; | |
343 | ||
344 | if (the_authctxt == NULL) | |
345 | fatal("%s: PAM authctxt not initialized", __func__); | |
346 | ||
347 | buffer_init(&buffer); | |
348 | sshpam_err = pam_set_item(sshpam_handle, PAM_CONV, | |
349 | (const void *)&sshpam_conv); | |
350 | if (sshpam_err != PAM_SUCCESS) | |
351 | goto auth_fail; | |
352 | sshpam_err = pam_authenticate(sshpam_handle, 0); | |
353 | if (sshpam_err != PAM_SUCCESS) | |
354 | goto auth_fail; | |
355 | ||
356 | if (compat20) { | |
357 | if (!do_pam_account()) | |
358 | goto auth_fail; | |
359 | if (the_authctxt->force_pwchange) { | |
360 | sshpam_err = pam_chauthtok(sshpam_handle, | |
361 | PAM_CHANGE_EXPIRED_AUTHTOK); | |
362 | if (sshpam_err != PAM_SUCCESS) | |
363 | goto auth_fail; | |
364 | pam_password_change_required(0); | |
365 | } | |
366 | } | |
367 | ||
368 | buffer_put_cstring(&buffer, "OK"); | |
369 | ||
370 | #ifndef USE_POSIX_THREADS | |
371 | /* Export variables set by do_pam_account */ | |
372 | buffer_put_int(&buffer, sshpam_account_status); | |
373 | buffer_put_int(&buffer, the_authctxt->force_pwchange); | |
374 | ||
375 | /* Export any environment strings set in child */ | |
376 | for(i = 0; environ[i] != NULL; i++) | |
377 | ; /* Count */ | |
378 | buffer_put_int(&buffer, i); | |
379 | for(i = 0; environ[i] != NULL; i++) | |
380 | buffer_put_cstring(&buffer, environ[i]); | |
381 | ||
382 | /* Export any environment strings set by PAM in child */ | |
383 | env_from_pam = pam_getenvlist(sshpam_handle); | |
384 | for(i = 0; env_from_pam != NULL && env_from_pam[i] != NULL; i++) | |
385 | ; /* Count */ | |
386 | buffer_put_int(&buffer, i); | |
387 | for(i = 0; env_from_pam != NULL && env_from_pam[i] != NULL; i++) | |
388 | buffer_put_cstring(&buffer, env_from_pam[i]); | |
389 | #endif /* USE_POSIX_THREADS */ | |
390 | ||
391 | /* XXX - can't do much about an error here */ | |
392 | ssh_msg_send(ctxt->pam_csock, sshpam_err, &buffer); | |
393 | buffer_free(&buffer); | |
394 | pthread_exit(NULL); | |
395 | ||
396 | auth_fail: | |
397 | buffer_put_cstring(&buffer, | |
398 | pam_strerror(sshpam_handle, sshpam_err)); | |
399 | /* XXX - can't do much about an error here */ | |
400 | ssh_msg_send(ctxt->pam_csock, PAM_AUTH_ERR, &buffer); | |
401 | buffer_free(&buffer); | |
402 | pthread_exit(NULL); | |
403 | ||
404 | return (NULL); /* Avoid warning for non-pthread case */ | |
405 | } | |
406 | ||
407 | void | |
408 | sshpam_thread_cleanup(void) | |
409 | { | |
410 | struct pam_ctxt *ctxt = cleanup_ctxt; | |
411 | ||
412 | debug3("PAM: %s entering", __func__); | |
413 | if (ctxt != NULL && ctxt->pam_thread != 0) { | |
414 | pthread_cancel(ctxt->pam_thread); | |
415 | pthread_join(ctxt->pam_thread, NULL); | |
416 | close(ctxt->pam_psock); | |
417 | close(ctxt->pam_csock); | |
418 | memset(ctxt, 0, sizeof(*ctxt)); | |
419 | cleanup_ctxt = NULL; | |
420 | } | |
421 | } | |
422 | ||
423 | static int | |
424 | sshpam_null_conv(int n, const struct pam_message **msg, | |
425 | struct pam_response **resp, void *data) | |
426 | { | |
427 | debug3("PAM: %s entering, %d messages", __func__, n); | |
428 | return (PAM_CONV_ERR); | |
429 | } | |
430 | ||
431 | static struct pam_conv null_conv = { sshpam_null_conv, NULL }; | |
432 | ||
433 | void | |
434 | sshpam_cleanup(void) | |
435 | { | |
436 | debug("PAM: cleanup"); | |
437 | if (sshpam_handle == NULL) | |
438 | return; | |
439 | pam_set_item(sshpam_handle, PAM_CONV, (const void *)&null_conv); | |
440 | if (sshpam_cred_established) { | |
441 | pam_setcred(sshpam_handle, PAM_DELETE_CRED); | |
442 | sshpam_cred_established = 0; | |
443 | } | |
444 | if (sshpam_session_open) { | |
445 | pam_close_session(sshpam_handle, PAM_SILENT); | |
446 | sshpam_session_open = 0; | |
447 | } | |
448 | sshpam_authenticated = 0; | |
449 | pam_end(sshpam_handle, sshpam_err); | |
450 | sshpam_handle = NULL; | |
451 | } | |
452 | ||
453 | static int | |
454 | sshpam_init(Authctxt *authctxt) | |
455 | { | |
456 | extern u_int utmp_len; | |
457 | extern char *__progname; | |
458 | const char *pam_rhost, *pam_user, *user = authctxt->user; | |
459 | ||
460 | if (sshpam_handle != NULL) { | |
461 | /* We already have a PAM context; check if the user matches */ | |
462 | sshpam_err = pam_get_item(sshpam_handle, | |
463 | PAM_USER, (const void **)&pam_user); | |
464 | if (sshpam_err == PAM_SUCCESS && strcmp(user, pam_user) == 0) | |
465 | return (0); | |
466 | pam_end(sshpam_handle, sshpam_err); | |
467 | sshpam_handle = NULL; | |
468 | } | |
469 | debug("PAM: initializing for \"%s\"", user); | |
470 | sshpam_err = | |
471 | pam_start(SSHD_PAM_SERVICE, user, &null_conv, &sshpam_handle); | |
472 | the_authctxt = authctxt; | |
473 | ||
474 | if (sshpam_err != PAM_SUCCESS) { | |
475 | pam_end(sshpam_handle, sshpam_err); | |
476 | sshpam_handle = NULL; | |
477 | return (-1); | |
478 | } | |
479 | pam_rhost = get_remote_name_or_ip(utmp_len, options.use_dns); | |
480 | debug("PAM: setting PAM_RHOST to \"%s\"", pam_rhost); | |
481 | sshpam_err = pam_set_item(sshpam_handle, PAM_RHOST, pam_rhost); | |
482 | if (sshpam_err != PAM_SUCCESS) { | |
483 | pam_end(sshpam_handle, sshpam_err); | |
484 | sshpam_handle = NULL; | |
485 | return (-1); | |
486 | } | |
487 | #ifdef PAM_TTY_KLUDGE | |
488 | /* | |
489 | * Some silly PAM modules (e.g. pam_time) require a TTY to operate. | |
490 | * sshd doesn't set the tty until too late in the auth process and | |
491 | * may not even set one (for tty-less connections) | |
492 | */ | |
493 | debug("PAM: setting PAM_TTY to \"ssh\""); | |
494 | sshpam_err = pam_set_item(sshpam_handle, PAM_TTY, "ssh"); | |
495 | if (sshpam_err != PAM_SUCCESS) { | |
496 | pam_end(sshpam_handle, sshpam_err); | |
497 | sshpam_handle = NULL; | |
498 | return (-1); | |
499 | } | |
500 | #endif | |
501 | return (0); | |
502 | } | |
503 | ||
504 | static void * | |
505 | sshpam_init_ctx(Authctxt *authctxt) | |
506 | { | |
507 | struct pam_ctxt *ctxt; | |
508 | int socks[2]; | |
509 | ||
510 | debug3("PAM: %s entering", __func__); | |
511 | /* Refuse to start if we don't have PAM enabled */ | |
512 | if (!options.use_pam) | |
513 | return NULL; | |
514 | ||
515 | /* Initialize PAM */ | |
516 | if (sshpam_init(authctxt) == -1) { | |
517 | error("PAM: initialization failed"); | |
518 | return (NULL); | |
519 | } | |
520 | ||
521 | ctxt = xmalloc(sizeof *ctxt); | |
522 | memset(ctxt, 0, sizeof(*ctxt)); | |
523 | ||
524 | /* Start the authentication thread */ | |
525 | if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, socks) == -1) { | |
526 | error("PAM: failed create sockets: %s", strerror(errno)); | |
527 | xfree(ctxt); | |
528 | return (NULL); | |
529 | } | |
530 | ctxt->pam_psock = socks[0]; | |
531 | ctxt->pam_csock = socks[1]; | |
532 | if (pthread_create(&ctxt->pam_thread, NULL, sshpam_thread, ctxt) == -1) { | |
533 | error("PAM: failed to start authentication thread: %s", | |
534 | strerror(errno)); | |
535 | close(socks[0]); | |
536 | close(socks[1]); | |
537 | xfree(ctxt); | |
538 | return (NULL); | |
539 | } | |
540 | cleanup_ctxt = ctxt; | |
541 | return (ctxt); | |
542 | } | |
543 | ||
544 | static int | |
545 | sshpam_query(void *ctx, char **name, char **info, | |
546 | u_int *num, char ***prompts, u_int **echo_on) | |
547 | { | |
548 | Buffer buffer; | |
549 | struct pam_ctxt *ctxt = ctx; | |
550 | size_t plen; | |
551 | u_char type; | |
552 | char *msg; | |
553 | size_t len; | |
554 | ||
555 | debug3("PAM: %s entering", __func__); | |
556 | buffer_init(&buffer); | |
557 | *name = xstrdup(""); | |
558 | *info = xstrdup(""); | |
559 | *prompts = xmalloc(sizeof(char *)); | |
560 | **prompts = NULL; | |
561 | plen = 0; | |
562 | *echo_on = xmalloc(sizeof(u_int)); | |
563 | while (ssh_msg_recv(ctxt->pam_psock, &buffer) == 0) { | |
564 | type = buffer_get_char(&buffer); | |
565 | msg = buffer_get_string(&buffer, NULL); | |
566 | switch (type) { | |
567 | case PAM_PROMPT_ECHO_ON: | |
568 | case PAM_PROMPT_ECHO_OFF: | |
569 | *num = 1; | |
570 | len = plen + strlen(msg) + 1; | |
571 | **prompts = xrealloc(**prompts, len); | |
572 | plen += snprintf(**prompts + plen, len, "%s", msg); | |
573 | **echo_on = (type == PAM_PROMPT_ECHO_ON); | |
574 | xfree(msg); | |
575 | return (0); | |
576 | case PAM_ERROR_MSG: | |
577 | case PAM_TEXT_INFO: | |
578 | /* accumulate messages */ | |
579 | len = plen + strlen(msg) + 2; | |
580 | **prompts = xrealloc(**prompts, len); | |
581 | plen += snprintf(**prompts + plen, len, "%s\n", msg); | |
582 | xfree(msg); | |
583 | break; | |
584 | case PAM_SUCCESS: | |
585 | case PAM_AUTH_ERR: | |
586 | if (**prompts != NULL) { | |
587 | /* drain any accumulated messages */ | |
588 | debug("PAM: %s", **prompts); | |
589 | buffer_append(&loginmsg, **prompts, | |
590 | strlen(**prompts)); | |
591 | xfree(**prompts); | |
592 | **prompts = NULL; | |
593 | } | |
594 | if (type == PAM_SUCCESS) { | |
595 | import_environments(&buffer); | |
596 | *num = 0; | |
597 | **echo_on = 0; | |
598 | ctxt->pam_done = 1; | |
599 | xfree(msg); | |
600 | return (0); | |
601 | } | |
602 | error("PAM: %s", msg); | |
603 | /* FALLTHROUGH */ | |
604 | default: | |
605 | *num = 0; | |
606 | **echo_on = 0; | |
607 | xfree(msg); | |
608 | ctxt->pam_done = -1; | |
609 | return (-1); | |
610 | } | |
611 | } | |
612 | return (-1); | |
613 | } | |
614 | ||
615 | /* XXX - see also comment in auth-chall.c:verify_response */ | |
616 | static int | |
617 | sshpam_respond(void *ctx, u_int num, char **resp) | |
618 | { | |
619 | Buffer buffer; | |
620 | struct pam_ctxt *ctxt = ctx; | |
621 | ||
622 | debug2("PAM: %s entering, %d responses", __func__, num); | |
623 | switch (ctxt->pam_done) { | |
624 | case 1: | |
625 | sshpam_authenticated = 1; | |
626 | return (0); | |
627 | case 0: | |
628 | break; | |
629 | default: | |
630 | return (-1); | |
631 | } | |
632 | if (num != 1) { | |
633 | error("PAM: expected one response, got %u", num); | |
634 | return (-1); | |
635 | } | |
636 | buffer_init(&buffer); | |
637 | buffer_put_cstring(&buffer, *resp); | |
638 | if (ssh_msg_send(ctxt->pam_psock, PAM_AUTHTOK, &buffer) == -1) { | |
639 | buffer_free(&buffer); | |
640 | return (-1); | |
641 | } | |
642 | buffer_free(&buffer); | |
643 | return (1); | |
644 | } | |
645 | ||
646 | static void | |
647 | sshpam_free_ctx(void *ctxtp) | |
648 | { | |
649 | struct pam_ctxt *ctxt = ctxtp; | |
650 | ||
651 | debug3("PAM: %s entering", __func__); | |
652 | sshpam_thread_cleanup(); | |
653 | xfree(ctxt); | |
654 | /* | |
655 | * We don't call sshpam_cleanup() here because we may need the PAM | |
656 | * handle at a later stage, e.g. when setting up a session. It's | |
657 | * still on the cleanup list, so pam_end() *will* be called before | |
658 | * the server process terminates. | |
659 | */ | |
660 | } | |
661 | ||
662 | KbdintDevice sshpam_device = { | |
663 | "pam", | |
664 | sshpam_init_ctx, | |
665 | sshpam_query, | |
666 | sshpam_respond, | |
667 | sshpam_free_ctx | |
668 | }; | |
669 | ||
670 | KbdintDevice mm_sshpam_device = { | |
671 | "pam", | |
672 | mm_sshpam_init_ctx, | |
673 | mm_sshpam_query, | |
674 | mm_sshpam_respond, | |
675 | mm_sshpam_free_ctx | |
676 | }; | |
677 | ||
678 | /* | |
679 | * This replaces auth-pam.c | |
680 | */ | |
681 | void | |
682 | start_pam(Authctxt *authctxt) | |
683 | { | |
684 | if (!options.use_pam) | |
685 | fatal("PAM: initialisation requested when UsePAM=no"); | |
686 | ||
687 | if (sshpam_init(authctxt) == -1) | |
688 | fatal("PAM: initialisation failed"); | |
689 | } | |
690 | ||
691 | void | |
692 | finish_pam(void) | |
693 | { | |
694 | sshpam_cleanup(); | |
695 | } | |
696 | ||
697 | u_int | |
698 | do_pam_account(void) | |
699 | { | |
700 | if (sshpam_account_status != -1) | |
701 | return (sshpam_account_status); | |
702 | ||
703 | sshpam_err = pam_acct_mgmt(sshpam_handle, 0); | |
704 | debug3("PAM: %s pam_acct_mgmt = %d", __func__, sshpam_err); | |
705 | ||
706 | if (sshpam_err != PAM_SUCCESS && sshpam_err != PAM_NEW_AUTHTOK_REQD) { | |
707 | sshpam_account_status = 0; | |
708 | return (sshpam_account_status); | |
709 | } | |
710 | ||
711 | if (sshpam_err == PAM_NEW_AUTHTOK_REQD) | |
712 | pam_password_change_required(1); | |
713 | ||
714 | sshpam_account_status = 1; | |
715 | return (sshpam_account_status); | |
716 | } | |
717 | ||
718 | void | |
719 | do_pam_set_tty(const char *tty) | |
720 | { | |
721 | if (tty != NULL) { | |
722 | debug("PAM: setting PAM_TTY to \"%s\"", tty); | |
723 | sshpam_err = pam_set_item(sshpam_handle, PAM_TTY, tty); | |
724 | if (sshpam_err != PAM_SUCCESS) | |
725 | fatal("PAM: failed to set PAM_TTY: %s", | |
726 | pam_strerror(sshpam_handle, sshpam_err)); | |
727 | } | |
728 | } | |
729 | ||
730 | void | |
731 | do_pam_setcred(int init) | |
732 | { | |
733 | sshpam_err = pam_set_item(sshpam_handle, PAM_CONV, | |
734 | (const void *)&null_conv); | |
735 | if (sshpam_err != PAM_SUCCESS) | |
736 | fatal("PAM: failed to set PAM_CONV: %s", | |
737 | pam_strerror(sshpam_handle, sshpam_err)); | |
738 | if (init) { | |
739 | debug("PAM: establishing credentials"); | |
740 | sshpam_err = pam_setcred(sshpam_handle, PAM_ESTABLISH_CRED); | |
741 | } else { | |
742 | debug("PAM: reinitializing credentials"); | |
743 | sshpam_err = pam_setcred(sshpam_handle, PAM_REINITIALIZE_CRED); | |
744 | } | |
745 | if (sshpam_err == PAM_SUCCESS) { | |
746 | sshpam_cred_established = 1; | |
747 | return; | |
748 | } | |
749 | if (sshpam_authenticated) | |
750 | fatal("PAM: pam_setcred(): %s", | |
751 | pam_strerror(sshpam_handle, sshpam_err)); | |
752 | else | |
753 | debug("PAM: pam_setcred(): %s", | |
754 | pam_strerror(sshpam_handle, sshpam_err)); | |
755 | } | |
756 | ||
757 | static int | |
758 | pam_tty_conv(int n, const struct pam_message **msg, | |
759 | struct pam_response **resp, void *data) | |
760 | { | |
761 | char input[PAM_MAX_MSG_SIZE]; | |
762 | struct pam_response *reply; | |
763 | int i; | |
764 | ||
765 | debug3("PAM: %s called with %d messages", __func__, n); | |
766 | ||
767 | *resp = NULL; | |
768 | ||
769 | if (n <= 0 || n > PAM_MAX_NUM_MSG || !isatty(STDIN_FILENO)) | |
770 | return (PAM_CONV_ERR); | |
771 | ||
772 | if ((reply = malloc(n * sizeof(*reply))) == NULL) | |
773 | return (PAM_CONV_ERR); | |
774 | memset(reply, 0, n * sizeof(*reply)); | |
775 | ||
776 | for (i = 0; i < n; ++i) { | |
777 | switch (PAM_MSG_MEMBER(msg, i, msg_style)) { | |
778 | case PAM_PROMPT_ECHO_OFF: | |
779 | reply[i].resp = | |
780 | read_passphrase(PAM_MSG_MEMBER(msg, i, msg), | |
781 | RP_ALLOW_STDIN); | |
782 | reply[i].resp_retcode = PAM_SUCCESS; | |
783 | break; | |
784 | case PAM_PROMPT_ECHO_ON: | |
785 | fprintf(stderr, "%s\n", PAM_MSG_MEMBER(msg, i, msg)); | |
786 | fgets(input, sizeof input, stdin); | |
787 | reply[i].resp = xstrdup(input); | |
788 | reply[i].resp_retcode = PAM_SUCCESS; | |
789 | break; | |
790 | case PAM_ERROR_MSG: | |
791 | case PAM_TEXT_INFO: | |
792 | fprintf(stderr, "%s\n", PAM_MSG_MEMBER(msg, i, msg)); | |
793 | reply[i].resp_retcode = PAM_SUCCESS; | |
794 | break; | |
795 | default: | |
796 | goto fail; | |
797 | } | |
798 | } | |
799 | *resp = reply; | |
800 | return (PAM_SUCCESS); | |
801 | ||
802 | fail: | |
803 | for(i = 0; i < n; i++) { | |
804 | if (reply[i].resp != NULL) | |
805 | xfree(reply[i].resp); | |
806 | } | |
807 | xfree(reply); | |
808 | return (PAM_CONV_ERR); | |
809 | } | |
810 | ||
811 | static struct pam_conv tty_conv = { pam_tty_conv, NULL }; | |
812 | ||
813 | /* | |
814 | * XXX this should be done in the authentication phase, but ssh1 doesn't | |
815 | * support that | |
816 | */ | |
817 | void | |
818 | do_pam_chauthtok(void) | |
819 | { | |
820 | if (use_privsep) | |
821 | fatal("Password expired (unable to change with privsep)"); | |
822 | sshpam_err = pam_set_item(sshpam_handle, PAM_CONV, | |
823 | (const void *)&tty_conv); | |
824 | if (sshpam_err != PAM_SUCCESS) | |
825 | fatal("PAM: failed to set PAM_CONV: %s", | |
826 | pam_strerror(sshpam_handle, sshpam_err)); | |
827 | debug("PAM: changing password"); | |
828 | sshpam_err = pam_chauthtok(sshpam_handle, PAM_CHANGE_EXPIRED_AUTHTOK); | |
829 | if (sshpam_err != PAM_SUCCESS) | |
830 | fatal("PAM: pam_chauthtok(): %s", | |
831 | pam_strerror(sshpam_handle, sshpam_err)); | |
832 | } | |
833 | ||
834 | static int | |
835 | pam_store_conv(int n, const struct pam_message **msg, | |
836 | struct pam_response **resp, void *data) | |
837 | { | |
838 | struct pam_response *reply; | |
839 | int i; | |
840 | size_t len; | |
841 | ||
842 | debug3("PAM: %s called with %d messages", __func__, n); | |
843 | *resp = NULL; | |
844 | ||
845 | if (n <= 0 || n > PAM_MAX_NUM_MSG) | |
846 | return (PAM_CONV_ERR); | |
847 | ||
848 | if ((reply = malloc(n * sizeof(*reply))) == NULL) | |
849 | return (PAM_CONV_ERR); | |
850 | memset(reply, 0, n * sizeof(*reply)); | |
851 | ||
852 | for (i = 0; i < n; ++i) { | |
853 | switch (PAM_MSG_MEMBER(msg, i, msg_style)) { | |
854 | case PAM_ERROR_MSG: | |
855 | case PAM_TEXT_INFO: | |
856 | len = strlen(PAM_MSG_MEMBER(msg, i, msg)); | |
857 | buffer_append(&loginmsg, PAM_MSG_MEMBER(msg, i, msg), len); | |
858 | buffer_append(&loginmsg, "\n", 1 ); | |
859 | reply[i].resp_retcode = PAM_SUCCESS; | |
860 | break; | |
861 | default: | |
862 | goto fail; | |
863 | } | |
864 | } | |
865 | *resp = reply; | |
866 | return (PAM_SUCCESS); | |
867 | ||
868 | fail: | |
869 | for(i = 0; i < n; i++) { | |
870 | if (reply[i].resp != NULL) | |
871 | xfree(reply[i].resp); | |
872 | } | |
873 | xfree(reply); | |
874 | return (PAM_CONV_ERR); | |
875 | } | |
876 | ||
877 | static struct pam_conv store_conv = { pam_store_conv, NULL }; | |
878 | ||
879 | void | |
880 | do_pam_session(void) | |
881 | { | |
882 | debug3("PAM: opening session"); | |
883 | sshpam_err = pam_set_item(sshpam_handle, PAM_CONV, | |
884 | (const void *)&store_conv); | |
885 | if (sshpam_err != PAM_SUCCESS) | |
886 | fatal("PAM: failed to set PAM_CONV: %s", | |
887 | pam_strerror(sshpam_handle, sshpam_err)); | |
888 | sshpam_err = pam_open_session(sshpam_handle, 0); | |
889 | if (sshpam_err != PAM_SUCCESS) | |
890 | fatal("PAM: pam_open_session(): %s", | |
891 | pam_strerror(sshpam_handle, sshpam_err)); | |
892 | sshpam_session_open = 1; | |
893 | } | |
894 | ||
895 | /* | |
896 | * Set a PAM environment string. We need to do this so that the session | |
897 | * modules can handle things like Kerberos/GSI credentials that appear | |
898 | * during the ssh authentication process. | |
899 | */ | |
900 | int | |
901 | do_pam_putenv(char *name, char *value) | |
902 | { | |
903 | int ret = 1; | |
904 | #ifdef HAVE_PAM_PUTENV | |
905 | char *compound; | |
906 | size_t len; | |
907 | ||
908 | len = strlen(name) + strlen(value) + 2; | |
909 | compound = xmalloc(len); | |
910 | ||
911 | snprintf(compound, len, "%s=%s", name, value); | |
912 | ret = pam_putenv(sshpam_handle, compound); | |
913 | xfree(compound); | |
914 | #endif | |
915 | ||
916 | return (ret); | |
917 | } | |
918 | ||
919 | char ** | |
920 | fetch_pam_child_environment(void) | |
921 | { | |
922 | return sshpam_env; | |
923 | } | |
924 | ||
925 | char ** | |
926 | fetch_pam_environment(void) | |
927 | { | |
928 | return (pam_getenvlist(sshpam_handle)); | |
929 | } | |
930 | ||
931 | void | |
932 | free_pam_environment(char **env) | |
933 | { | |
934 | char **envp; | |
935 | ||
936 | if (env == NULL) | |
937 | return; | |
938 | ||
939 | for (envp = env; *envp; envp++) | |
940 | xfree(*envp); | |
941 | xfree(env); | |
942 | } | |
943 | ||
944 | #endif /* USE_PAM */ |