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