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