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