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