10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * Copyright (c) 2000 Damien Miller. All rights reserved.
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without
50Sstevel@tonic-gate * modification, are permitted provided that the following conditions
60Sstevel@tonic-gate * are met:
70Sstevel@tonic-gate * 1. Redistributions of source code must retain the above copyright
80Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer.
90Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright
100Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in the
110Sstevel@tonic-gate * documentation and/or other materials provided with the distribution.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
140Sstevel@tonic-gate * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
150Sstevel@tonic-gate * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
160Sstevel@tonic-gate * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
170Sstevel@tonic-gate * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
180Sstevel@tonic-gate * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
190Sstevel@tonic-gate * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
200Sstevel@tonic-gate * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
210Sstevel@tonic-gate * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
220Sstevel@tonic-gate * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
230Sstevel@tonic-gate */
240Sstevel@tonic-gate /*
25*12317SDarren.Moffat@oracle.com * Copyright (c) 2001, 2010, Oracle and/or its affiliates. All rights reserved.
260Sstevel@tonic-gate */
270Sstevel@tonic-gate
280Sstevel@tonic-gate #include "includes.h"
290Sstevel@tonic-gate
300Sstevel@tonic-gate #ifdef USE_PAM
310Sstevel@tonic-gate #include "xmalloc.h"
320Sstevel@tonic-gate #include "log.h"
330Sstevel@tonic-gate #include "auth.h"
340Sstevel@tonic-gate #include "auth-options.h"
350Sstevel@tonic-gate #include "auth-pam.h"
3611044SHuie-Ying.Lee@Sun.COM #include "buffer.h"
370Sstevel@tonic-gate #include "servconf.h"
380Sstevel@tonic-gate #include "canohost.h"
390Sstevel@tonic-gate #include "compat.h"
400Sstevel@tonic-gate #include "misc.h"
410Sstevel@tonic-gate #include "sshlogin.h"
425562Sjp161948 #include "ssh-gss.h"
430Sstevel@tonic-gate
440Sstevel@tonic-gate #include <security/pam_appl.h>
450Sstevel@tonic-gate
460Sstevel@tonic-gate extern char *__progname;
470Sstevel@tonic-gate
480Sstevel@tonic-gate extern u_int utmp_len;
490Sstevel@tonic-gate extern ServerOptions options;
500Sstevel@tonic-gate
510Sstevel@tonic-gate extern Authmethod method_kbdint;
520Sstevel@tonic-gate
530Sstevel@tonic-gate RCSID("$Id: auth-pam.c,v 1.54 2002/07/28 20:24:08 stevesk Exp $");
540Sstevel@tonic-gate
550Sstevel@tonic-gate #define NEW_AUTHTOK_MSG \
560Sstevel@tonic-gate "Warning: Your password has expired, please change it now."
570Sstevel@tonic-gate
580Sstevel@tonic-gate /* PAM conversation for non-interactive userauth methods */
590Sstevel@tonic-gate static int do_pam_conversation(int num_msg, const struct pam_message **msg,
600Sstevel@tonic-gate struct pam_response **resp, void *appdata_ptr);
610Sstevel@tonic-gate
620Sstevel@tonic-gate static void do_pam_cleanup_proc(void *context);
630Sstevel@tonic-gate
640Sstevel@tonic-gate static char *get_method_name(Authctxt *authctxt);
650Sstevel@tonic-gate
660Sstevel@tonic-gate /* PAM conversation for non-interactive userauth methods */
670Sstevel@tonic-gate static struct pam_conv conv = {
680Sstevel@tonic-gate (int (*)())do_pam_conversation,
690Sstevel@tonic-gate NULL
700Sstevel@tonic-gate };
710Sstevel@tonic-gate static char *__pam_msg = NULL;
720Sstevel@tonic-gate
730Sstevel@tonic-gate static
740Sstevel@tonic-gate char *
get_method_name(Authctxt * authctxt)750Sstevel@tonic-gate get_method_name(Authctxt *authctxt)
760Sstevel@tonic-gate {
770Sstevel@tonic-gate if (!authctxt)
780Sstevel@tonic-gate return "(unknown)";
790Sstevel@tonic-gate
800Sstevel@tonic-gate if (!compat20)
810Sstevel@tonic-gate return (authctxt->v1_auth_name) ? authctxt->v1_auth_name :
820Sstevel@tonic-gate "(sshv1-unknown)";
830Sstevel@tonic-gate
840Sstevel@tonic-gate if (!authctxt->method || !authctxt->method->name)
850Sstevel@tonic-gate return "(sshv2-unknown)";
860Sstevel@tonic-gate
870Sstevel@tonic-gate return authctxt->method->name;
880Sstevel@tonic-gate }
890Sstevel@tonic-gate
900Sstevel@tonic-gate char *
derive_pam_service_name(Authmethod * method)91*12317SDarren.Moffat@oracle.com derive_pam_service_name(Authmethod *method)
920Sstevel@tonic-gate {
93*12317SDarren.Moffat@oracle.com char *svcname = xmalloc(BUFSIZ);
94*12317SDarren.Moffat@oracle.com
95*12317SDarren.Moffat@oracle.com /*
96*12317SDarren.Moffat@oracle.com * If PamServiceName is set we use that for everything, including
97*12317SDarren.Moffat@oracle.com * SSHv1
98*12317SDarren.Moffat@oracle.com */
99*12317SDarren.Moffat@oracle.com if (options.pam_service_name != NULL) {
100*12317SDarren.Moffat@oracle.com (void) strlcpy(svcname, options.pam_service_name, BUFSIZ);
101*12317SDarren.Moffat@oracle.com return (svcname);
102*12317SDarren.Moffat@oracle.com }
103*12317SDarren.Moffat@oracle.com
1040Sstevel@tonic-gate if (compat20 && method) {
1050Sstevel@tonic-gate char *method_name = method->name;
1060Sstevel@tonic-gate
1070Sstevel@tonic-gate if (!method_name)
1080Sstevel@tonic-gate fatal("Userauth method unknown while starting PAM");
1090Sstevel@tonic-gate
110*12317SDarren.Moffat@oracle.com /*
111*12317SDarren.Moffat@oracle.com * For SSHv2 we use "sshd-<userauth name>
112*12317SDarren.Moffat@oracle.com * The "sshd" prefix can be changed via the PAMServicePrefix
113*12317SDarren.Moffat@oracle.com * sshd_config option.
114*12317SDarren.Moffat@oracle.com */
1150Sstevel@tonic-gate if (strcmp(method_name, "none") == 0) {
116*12317SDarren.Moffat@oracle.com snprintf(svcname, BUFSIZ, "%s-none",
117*12317SDarren.Moffat@oracle.com options.pam_service_prefix);
1180Sstevel@tonic-gate }
1190Sstevel@tonic-gate if (strcmp(method_name, "password") == 0) {
120*12317SDarren.Moffat@oracle.com snprintf(svcname, BUFSIZ, "%s-password",
121*12317SDarren.Moffat@oracle.com options.pam_service_prefix);
1220Sstevel@tonic-gate }
1230Sstevel@tonic-gate if (strcmp(method_name, "keyboard-interactive") == 0) {
1240Sstevel@tonic-gate /* "keyboard-interactive" is too long, shorten it */
125*12317SDarren.Moffat@oracle.com snprintf(svcname, BUFSIZ, "%s-kbdint",
126*12317SDarren.Moffat@oracle.com options.pam_service_prefix);
1270Sstevel@tonic-gate }
1280Sstevel@tonic-gate if (strcmp(method_name, "publickey") == 0) {
1290Sstevel@tonic-gate /* "publickey" is too long, shorten it */
130*12317SDarren.Moffat@oracle.com snprintf(svcname, BUFSIZ, "%s-pubkey",
131*12317SDarren.Moffat@oracle.com options.pam_service_prefix);
1320Sstevel@tonic-gate }
1330Sstevel@tonic-gate if (strcmp(method_name, "hostbased") == 0) {
1340Sstevel@tonic-gate /* "hostbased" can't really be shortened... */
135*12317SDarren.Moffat@oracle.com snprintf(svcname, BUFSIZ, "%s-hostbased",
136*12317SDarren.Moffat@oracle.com options.pam_service_prefix);
1370Sstevel@tonic-gate }
1380Sstevel@tonic-gate if (strncmp(method_name, "gss", 3) == 0) {
1391411Sme23304 /* "gss" is too short, elongate it */
140*12317SDarren.Moffat@oracle.com snprintf(svcname, BUFSIZ, "%s-gssapi",
141*12317SDarren.Moffat@oracle.com options.pam_service_prefix);
1420Sstevel@tonic-gate }
143*12317SDarren.Moffat@oracle.com return svcname;
144*12317SDarren.Moffat@oracle.com } else {
145*12317SDarren.Moffat@oracle.com /* SSHv1 doesn't get to be so cool */
146*12317SDarren.Moffat@oracle.com snprintf(svcname, BUFSIZ, "%s-v1",
147*12317SDarren.Moffat@oracle.com options.pam_service_prefix);
1480Sstevel@tonic-gate }
149*12317SDarren.Moffat@oracle.com return svcname;
1500Sstevel@tonic-gate }
1510Sstevel@tonic-gate
1520Sstevel@tonic-gate void
new_start_pam(Authctxt * authctxt,struct pam_conv * conv)1530Sstevel@tonic-gate new_start_pam(Authctxt *authctxt, struct pam_conv *conv)
1540Sstevel@tonic-gate {
1550Sstevel@tonic-gate int retval;
1560Sstevel@tonic-gate pam_handle_t *pamh;
157*12317SDarren.Moffat@oracle.com const char *rhost;
158*12317SDarren.Moffat@oracle.com char *svc;
1590Sstevel@tonic-gate char *user = NULL;
1600Sstevel@tonic-gate pam_stuff *pam;
1610Sstevel@tonic-gate
1620Sstevel@tonic-gate if (authctxt == NULL)
1630Sstevel@tonic-gate fatal("Internal error during userauth");
1640Sstevel@tonic-gate
1650Sstevel@tonic-gate if (compat20 && authctxt->method == NULL)
1660Sstevel@tonic-gate fatal("Userauth method unknown while starting PAM");
1670Sstevel@tonic-gate
1680Sstevel@tonic-gate /* PAM service selected here */
169*12317SDarren.Moffat@oracle.com svc = derive_pam_service_name(authctxt->method);
1700Sstevel@tonic-gate debug2("Starting PAM service %s for method %s", svc,
1710Sstevel@tonic-gate get_method_name(authctxt));
1720Sstevel@tonic-gate
1730Sstevel@tonic-gate if (authctxt->user != NULL)
1740Sstevel@tonic-gate user = authctxt->user;
1750Sstevel@tonic-gate
1760Sstevel@tonic-gate /* Cleanup previous PAM state */
1770Sstevel@tonic-gate if (authctxt->pam != NULL) {
1780Sstevel@tonic-gate fatal_remove_cleanup(&do_pam_cleanup_proc, authctxt->pam);
1790Sstevel@tonic-gate do_pam_cleanup_proc(authctxt->pam);
1800Sstevel@tonic-gate }
1810Sstevel@tonic-gate
1820Sstevel@tonic-gate pam = xmalloc(sizeof(pam_stuff));
1830Sstevel@tonic-gate (void) memset(pam, 0, sizeof(pam_stuff));
1840Sstevel@tonic-gate
1850Sstevel@tonic-gate /*
1860Sstevel@tonic-gate * pam->last_pam_retval has to be and is considered
1870Sstevel@tonic-gate * along with pam->state.
1880Sstevel@tonic-gate *
1890Sstevel@tonic-gate * pam->state = 0; -> no PAM auth, account, etc, work
1900Sstevel@tonic-gate * done yet. (Set by memset() above.)
1910Sstevel@tonic-gate *
1920Sstevel@tonic-gate * pam->last_pam_retval = PAM_SUCCESS; -> meaningless at
1930Sstevel@tonic-gate * this point.
1940Sstevel@tonic-gate *
1950Sstevel@tonic-gate * See finish_userauth_do_pam() below.
1960Sstevel@tonic-gate */
1970Sstevel@tonic-gate pam->authctxt = authctxt;
1980Sstevel@tonic-gate pam->last_pam_retval = PAM_SUCCESS;
1990Sstevel@tonic-gate
2000Sstevel@tonic-gate authctxt->pam = pam;
2010Sstevel@tonic-gate
2020Sstevel@tonic-gate /* Free any previously stored text/error PAM prompts */
2030Sstevel@tonic-gate if (__pam_msg) {
2040Sstevel@tonic-gate xfree(__pam_msg);
2050Sstevel@tonic-gate __pam_msg = NULL;
2060Sstevel@tonic-gate }
2070Sstevel@tonic-gate
2080Sstevel@tonic-gate if ((retval = pam_start(svc, user, conv, &pamh)) != PAM_SUCCESS) {
2090Sstevel@tonic-gate fatal("PAM initialization failed during %s userauth",
2100Sstevel@tonic-gate get_method_name(authctxt));
2110Sstevel@tonic-gate }
2120Sstevel@tonic-gate
213*12317SDarren.Moffat@oracle.com free(svc);
214*12317SDarren.Moffat@oracle.com
2150Sstevel@tonic-gate fatal_add_cleanup((void (*)(void *)) &do_pam_cleanup_proc,
2160Sstevel@tonic-gate (void *) authctxt->pam);
2170Sstevel@tonic-gate
2180Sstevel@tonic-gate rhost = get_remote_name_or_ip(utmp_len, options.verify_reverse_mapping);
2190Sstevel@tonic-gate if ((retval = pam_set_item(pamh, PAM_RHOST, rhost)) != PAM_SUCCESS) {
2200Sstevel@tonic-gate (void) pam_end(pamh, retval);
2210Sstevel@tonic-gate fatal("Could not set PAM_RHOST item during %s userauth",
2220Sstevel@tonic-gate get_method_name(authctxt));
2230Sstevel@tonic-gate }
2240Sstevel@tonic-gate
2250Sstevel@tonic-gate if ((retval = pam_set_item(pamh, PAM_TTY, "sshd")) != PAM_SUCCESS) {
2260Sstevel@tonic-gate (void) pam_end(pamh, retval);
2270Sstevel@tonic-gate fatal("Could not set PAM_TTY item during %s userauth",
2280Sstevel@tonic-gate get_method_name(authctxt));
2290Sstevel@tonic-gate }
2300Sstevel@tonic-gate
2313908Sjp161948 if (authctxt->cuser != NULL)
2323908Sjp161948 if ((retval = pam_set_item(pamh, PAM_AUSER, authctxt->cuser)) != PAM_SUCCESS) {
2333908Sjp161948 (void) pam_end(pamh, retval);
2343908Sjp161948 fatal("Could not set PAM_AUSER item during %s userauth",
2353908Sjp161948 get_method_name(authctxt));
2363908Sjp161948 }
2373908Sjp161948
2380Sstevel@tonic-gate authctxt->pam->h = pamh;
2390Sstevel@tonic-gate }
2400Sstevel@tonic-gate
2410Sstevel@tonic-gate /*
2420Sstevel@tonic-gate * To be called from userauth methods, directly (as in keyboard-interactive) or
2430Sstevel@tonic-gate * indirectly (from auth_pam_password() or from do_pam_non_initial_userauth().
2440Sstevel@tonic-gate *
2450Sstevel@tonic-gate * The caller is responsible for calling new_start_pam() first.
2460Sstevel@tonic-gate *
2470Sstevel@tonic-gate * PAM state is not cleaned up here on error. This is left to subsequent calls
2480Sstevel@tonic-gate * to new_start_pam() or to the cleanup function upon authentication error.
2490Sstevel@tonic-gate */
2500Sstevel@tonic-gate int
finish_userauth_do_pam(Authctxt * authctxt)2510Sstevel@tonic-gate finish_userauth_do_pam(Authctxt *authctxt)
2520Sstevel@tonic-gate {
2530Sstevel@tonic-gate int retval;
2540Sstevel@tonic-gate char *user, *method;
2550Sstevel@tonic-gate
2560Sstevel@tonic-gate /* Various checks; fail gracefully */
2570Sstevel@tonic-gate if (authctxt == NULL || authctxt->pam == NULL)
2580Sstevel@tonic-gate return PAM_SYSTEM_ERR; /* shouldn't happen */
2590Sstevel@tonic-gate
2600Sstevel@tonic-gate if (compat20) {
2610Sstevel@tonic-gate if (authctxt->method == NULL || authctxt->method->name == NULL)
2620Sstevel@tonic-gate return PAM_SYSTEM_ERR; /* shouldn't happen */
2630Sstevel@tonic-gate method = authctxt->method->name;
2640Sstevel@tonic-gate } else if ((method = authctxt->v1_auth_name) == NULL)
2650Sstevel@tonic-gate return PAM_SYSTEM_ERR; /* shouldn't happen */
2660Sstevel@tonic-gate
2670Sstevel@tonic-gate if (AUTHPAM_DONE(authctxt))
2680Sstevel@tonic-gate return PAM_SYSTEM_ERR; /* shouldn't happen */
2690Sstevel@tonic-gate
2700Sstevel@tonic-gate if (!(authctxt->pam->state & PAM_S_DONE_ACCT_MGMT)) {
2710Sstevel@tonic-gate retval = pam_acct_mgmt(authctxt->pam->h, 0);
2720Sstevel@tonic-gate authctxt->pam->last_pam_retval = retval;
2730Sstevel@tonic-gate if (retval == PAM_NEW_AUTHTOK_REQD) {
2740Sstevel@tonic-gate userauth_force_kbdint();
2750Sstevel@tonic-gate return retval;
2760Sstevel@tonic-gate }
2770Sstevel@tonic-gate if (retval != PAM_SUCCESS)
2780Sstevel@tonic-gate return retval;
2790Sstevel@tonic-gate authctxt->pam->state |= PAM_S_DONE_ACCT_MGMT;
2800Sstevel@tonic-gate }
2810Sstevel@tonic-gate
2820Sstevel@tonic-gate /*
2830Sstevel@tonic-gate * Handle PAM_USER change, if any.
2840Sstevel@tonic-gate *
2850Sstevel@tonic-gate * We do this before pam_open_session() because we need the PAM_USER's
2860Sstevel@tonic-gate * UID for:
2870Sstevel@tonic-gate *
2880Sstevel@tonic-gate * a) PermitRootLogin checking
2890Sstevel@tonic-gate * b) to get at the lastlog entry before pam_open_session() updates it.
2900Sstevel@tonic-gate */
2910Sstevel@tonic-gate retval = pam_get_item(authctxt->pam->h, PAM_USER, (void **) &user);
2920Sstevel@tonic-gate if (retval != PAM_SUCCESS) {
2930Sstevel@tonic-gate fatal("PAM failure: pam_get_item(PAM_USER) "
2940Sstevel@tonic-gate "returned %d: %.200s", retval,
2950Sstevel@tonic-gate PAM_STRERROR(authctxt->pam->h, retval));
2960Sstevel@tonic-gate }
2970Sstevel@tonic-gate
2980Sstevel@tonic-gate if (user == NULL || *user == '\0') {
2990Sstevel@tonic-gate debug("PAM set NULL PAM_USER");
3000Sstevel@tonic-gate return PAM_PERM_DENIED;
3010Sstevel@tonic-gate }
3020Sstevel@tonic-gate
3030Sstevel@tonic-gate if (strcmp(user, authctxt->user) != 0) {
3040Sstevel@tonic-gate log("PAM changed the SSH username");
3050Sstevel@tonic-gate pwfree(&authctxt->pw);
3065562Sjp161948 authctxt->pw = getpwnamallow(user);
3070Sstevel@tonic-gate authctxt->valid = (authctxt->pw != NULL);
3080Sstevel@tonic-gate xfree(authctxt->user);
3090Sstevel@tonic-gate authctxt->user = xstrdup(user);
3100Sstevel@tonic-gate }
3110Sstevel@tonic-gate
3120Sstevel@tonic-gate if (!authctxt->valid) {
3130Sstevel@tonic-gate debug2("PAM set PAM_USER to unknown user");
3140Sstevel@tonic-gate /*
3150Sstevel@tonic-gate * Return success, userauth_finish() will catch
3160Sstevel@tonic-gate * this and send back a failure message.
3170Sstevel@tonic-gate */
3180Sstevel@tonic-gate return PAM_SUCCESS;
3190Sstevel@tonic-gate }
3200Sstevel@tonic-gate
3210Sstevel@tonic-gate /* Check PermitRootLogin semantics */
3220Sstevel@tonic-gate if (authctxt->pw->pw_uid == 0 && !auth_root_allowed(method))
3230Sstevel@tonic-gate return PAM_PERM_DENIED;
3240Sstevel@tonic-gate
3250Sstevel@tonic-gate if (!(authctxt->pam->state & PAM_S_DONE_SETCRED)) {
3260Sstevel@tonic-gate retval = pam_setcred(authctxt->pam->h,
3270Sstevel@tonic-gate PAM_ESTABLISH_CRED);
3280Sstevel@tonic-gate authctxt->pam->last_pam_retval = retval;
3290Sstevel@tonic-gate if (retval != PAM_SUCCESS)
3300Sstevel@tonic-gate return retval;
3310Sstevel@tonic-gate authctxt->pam->state |= PAM_S_DONE_SETCRED;
3320Sstevel@tonic-gate
3330Sstevel@tonic-gate #ifdef GSSAPI
3340Sstevel@tonic-gate /*
3350Sstevel@tonic-gate * Store GSS-API delegated creds after pam_setcred(), which may
3360Sstevel@tonic-gate * have set the current credential store.
3370Sstevel@tonic-gate */
3380Sstevel@tonic-gate ssh_gssapi_storecreds(NULL, authctxt);
3390Sstevel@tonic-gate #endif /* GSSAPI */
3400Sstevel@tonic-gate }
3410Sstevel@tonic-gate
3420Sstevel@tonic-gate /*
3430Sstevel@tonic-gate * On Solaris pam_unix_session.so updates the lastlog, but does
3440Sstevel@tonic-gate * not converse a PAM_TEXT_INFO message about it. So we need to
3450Sstevel@tonic-gate * fetch the lastlog entry here and save it for use later.
3460Sstevel@tonic-gate */
3470Sstevel@tonic-gate authctxt->last_login_time =
3480Sstevel@tonic-gate get_last_login_time(authctxt->pw->pw_uid,
3490Sstevel@tonic-gate authctxt->pw->pw_name,
3500Sstevel@tonic-gate authctxt->last_login_host,
3510Sstevel@tonic-gate sizeof(authctxt->last_login_host));
3520Sstevel@tonic-gate
3530Sstevel@tonic-gate if (!(authctxt->pam->state & PAM_S_DONE_OPEN_SESSION)) {
3540Sstevel@tonic-gate retval = pam_open_session(authctxt->pam->h, 0);
3550Sstevel@tonic-gate authctxt->pam->last_pam_retval = retval;
3560Sstevel@tonic-gate if (retval != PAM_SUCCESS)
3570Sstevel@tonic-gate return retval;
3580Sstevel@tonic-gate authctxt->pam->state |= PAM_S_DONE_OPEN_SESSION;
3590Sstevel@tonic-gate }
3600Sstevel@tonic-gate
3610Sstevel@tonic-gate /*
3620Sstevel@tonic-gate * All PAM work done successfully.
3630Sstevel@tonic-gate *
3640Sstevel@tonic-gate * PAM handle stays around so we can call pam_close_session() on
3650Sstevel@tonic-gate * it later.
3660Sstevel@tonic-gate */
3670Sstevel@tonic-gate return PAM_SUCCESS;
3680Sstevel@tonic-gate }
3690Sstevel@tonic-gate
3700Sstevel@tonic-gate /*
3710Sstevel@tonic-gate * PAM conversation function for non-interactive userauth methods that
3720Sstevel@tonic-gate * really cannot do any prompting. Password userauth and CHANGEREQ can
3730Sstevel@tonic-gate * always set the PAM_AUTHTOK and PAM_OLDAUTHTOK items to avoid
3740Sstevel@tonic-gate * conversation (and if they do and nonetheless some module tries to
3750Sstevel@tonic-gate * converse, then password userauth / CHANGEREQ MUST fail).
3760Sstevel@tonic-gate *
3770Sstevel@tonic-gate * Except, PAM_TEXT_INFO and PAM_ERROR_MSG prompts can be squirelled
3780Sstevel@tonic-gate * away and shown to the user later.
3790Sstevel@tonic-gate *
3800Sstevel@tonic-gate * Keyboard-interactive userauth has its own much more interesting
3810Sstevel@tonic-gate * conversation function.
3820Sstevel@tonic-gate *
3830Sstevel@tonic-gate */
3840Sstevel@tonic-gate static int
do_pam_conversation(int num_msg,const struct pam_message ** msg,struct pam_response ** resp,void * appdata_ptr)3850Sstevel@tonic-gate do_pam_conversation(int num_msg, const struct pam_message **msg,
3860Sstevel@tonic-gate struct pam_response **resp, void *appdata_ptr)
3870Sstevel@tonic-gate {
3880Sstevel@tonic-gate struct pam_response *reply;
3890Sstevel@tonic-gate int count;
3900Sstevel@tonic-gate
3910Sstevel@tonic-gate /* PAM will free this later */
3920Sstevel@tonic-gate reply = xmalloc(num_msg * sizeof(*reply));
3930Sstevel@tonic-gate
3940Sstevel@tonic-gate (void) memset(reply, 0, num_msg * sizeof(*reply));
3950Sstevel@tonic-gate
3960Sstevel@tonic-gate for (count = 0; count < num_msg; count++) {
3970Sstevel@tonic-gate /*
3980Sstevel@tonic-gate * We can't use stdio yet, queue messages for
3990Sstevel@tonic-gate * printing later
4000Sstevel@tonic-gate */
4010Sstevel@tonic-gate switch(PAM_MSG_MEMBER(msg, count, msg_style)) {
4020Sstevel@tonic-gate case PAM_PROMPT_ECHO_ON:
4030Sstevel@tonic-gate xfree(reply);
4040Sstevel@tonic-gate return PAM_CONV_ERR;
4050Sstevel@tonic-gate case PAM_PROMPT_ECHO_OFF:
4060Sstevel@tonic-gate xfree(reply);
4070Sstevel@tonic-gate return PAM_CONV_ERR;
4080Sstevel@tonic-gate break;
4090Sstevel@tonic-gate case PAM_ERROR_MSG:
4100Sstevel@tonic-gate case PAM_TEXT_INFO:
4110Sstevel@tonic-gate if (PAM_MSG_MEMBER(msg, count, msg) != NULL) {
4120Sstevel@tonic-gate message_cat(&__pam_msg,
4130Sstevel@tonic-gate PAM_MSG_MEMBER(msg, count, msg));
4140Sstevel@tonic-gate }
4150Sstevel@tonic-gate reply[count].resp = xstrdup("");
4160Sstevel@tonic-gate reply[count].resp_retcode = PAM_SUCCESS;
4170Sstevel@tonic-gate break;
4180Sstevel@tonic-gate default:
4190Sstevel@tonic-gate xfree(reply);
4200Sstevel@tonic-gate return PAM_CONV_ERR;
4210Sstevel@tonic-gate }
4220Sstevel@tonic-gate }
4230Sstevel@tonic-gate
4240Sstevel@tonic-gate *resp = reply;
4250Sstevel@tonic-gate
4260Sstevel@tonic-gate return PAM_SUCCESS;
4270Sstevel@tonic-gate }
4280Sstevel@tonic-gate
4290Sstevel@tonic-gate /* Called at exit to cleanly shutdown PAM */
4300Sstevel@tonic-gate static void
do_pam_cleanup_proc(void * context)4310Sstevel@tonic-gate do_pam_cleanup_proc(void *context)
4320Sstevel@tonic-gate {
4330Sstevel@tonic-gate int pam_retval;
4340Sstevel@tonic-gate pam_stuff *pam = (pam_stuff *) context;
4350Sstevel@tonic-gate
4360Sstevel@tonic-gate if (pam == NULL)
4370Sstevel@tonic-gate return;
4380Sstevel@tonic-gate
4390Sstevel@tonic-gate if (pam->authctxt != NULL && pam->authctxt->pam == pam) {
4400Sstevel@tonic-gate pam->authctxt->pam_retval = pam->last_pam_retval;
4410Sstevel@tonic-gate pam->authctxt->pam = NULL;
4420Sstevel@tonic-gate pam->authctxt = NULL;
4430Sstevel@tonic-gate }
4440Sstevel@tonic-gate
4450Sstevel@tonic-gate if (pam->h == NULL)
4460Sstevel@tonic-gate return;
4470Sstevel@tonic-gate
4480Sstevel@tonic-gate /*
4490Sstevel@tonic-gate * We're in fatal_cleanup() or not in userauth or without a
4500Sstevel@tonic-gate * channel -- can't converse now, too bad.
4510Sstevel@tonic-gate */
4520Sstevel@tonic-gate pam_retval = pam_set_item(pam->h, PAM_CONV, NULL);
4530Sstevel@tonic-gate if (pam_retval != PAM_SUCCESS) {
4540Sstevel@tonic-gate log("Cannot remove PAM conv, close session or delete creds[%d]: %.200s",
4550Sstevel@tonic-gate pam_retval, PAM_STRERROR(pam->h, pam_retval));
4560Sstevel@tonic-gate goto cleanup;
4570Sstevel@tonic-gate }
4580Sstevel@tonic-gate
4590Sstevel@tonic-gate if (pam->state & PAM_S_DONE_OPEN_SESSION) {
4600Sstevel@tonic-gate pam_retval = pam_close_session(pam->h, 0);
4610Sstevel@tonic-gate if (pam_retval != PAM_SUCCESS)
4620Sstevel@tonic-gate log("Cannot close PAM session[%d]: %.200s",
4630Sstevel@tonic-gate pam_retval, PAM_STRERROR(pam->h, pam_retval));
4640Sstevel@tonic-gate }
4650Sstevel@tonic-gate
4660Sstevel@tonic-gate if (pam->state & PAM_S_DONE_SETCRED) {
4670Sstevel@tonic-gate pam_retval = pam_setcred(pam->h, PAM_DELETE_CRED);
4680Sstevel@tonic-gate if (pam_retval != PAM_SUCCESS)
4690Sstevel@tonic-gate debug("Cannot delete credentials[%d]: %.200s",
4700Sstevel@tonic-gate pam_retval, PAM_STRERROR(pam->h, pam_retval));
4710Sstevel@tonic-gate }
4720Sstevel@tonic-gate
4730Sstevel@tonic-gate cleanup:
4740Sstevel@tonic-gate
4750Sstevel@tonic-gate /* Use the previous PAM result, if not PAM_SUCCESS for pam_end() */
4760Sstevel@tonic-gate if (pam->last_pam_retval != PAM_SUCCESS)
4770Sstevel@tonic-gate pam_retval = pam_end(pam->h, pam->last_pam_retval);
4780Sstevel@tonic-gate else if (pam_retval != PAM_SUCCESS)
4790Sstevel@tonic-gate pam_retval = pam_end(pam->h, pam_retval);
4800Sstevel@tonic-gate else
4810Sstevel@tonic-gate pam_retval = pam_end(pam->h, PAM_ABORT);
4820Sstevel@tonic-gate
4830Sstevel@tonic-gate if (pam_retval != PAM_SUCCESS)
4840Sstevel@tonic-gate log("Cannot release PAM authentication[%d]: %.200s",
4850Sstevel@tonic-gate pam_retval, PAM_STRERROR(pam->h, pam_retval));
4860Sstevel@tonic-gate
4870Sstevel@tonic-gate xfree(pam);
4880Sstevel@tonic-gate }
4890Sstevel@tonic-gate
4900Sstevel@tonic-gate /* Attempt password authentation using PAM */
4910Sstevel@tonic-gate int
auth_pam_password(Authctxt * authctxt,const char * password)4920Sstevel@tonic-gate auth_pam_password(Authctxt *authctxt, const char *password)
4930Sstevel@tonic-gate {
4940Sstevel@tonic-gate int retval;
4950Sstevel@tonic-gate
4960Sstevel@tonic-gate /* Ensure we have a fresh PAM handle / state */
4970Sstevel@tonic-gate new_start_pam(authctxt, &conv);
4980Sstevel@tonic-gate
4990Sstevel@tonic-gate retval = pam_set_item(authctxt->pam->h, PAM_AUTHTOK, password);
5008064SBrent.Paulson@Sun.COM if (retval != PAM_SUCCESS) {
5018064SBrent.Paulson@Sun.COM authctxt->pam->last_pam_retval = retval;
5020Sstevel@tonic-gate return 1;
5038064SBrent.Paulson@Sun.COM }
5040Sstevel@tonic-gate
5050Sstevel@tonic-gate retval = pam_authenticate(authctxt->pam->h,
5060Sstevel@tonic-gate options.permit_empty_passwd ? 0 :
5070Sstevel@tonic-gate PAM_DISALLOW_NULL_AUTHTOK);
5080Sstevel@tonic-gate
5098064SBrent.Paulson@Sun.COM if (retval != PAM_SUCCESS) {
5108064SBrent.Paulson@Sun.COM authctxt->pam->last_pam_retval = retval;
5110Sstevel@tonic-gate return 0;
5128064SBrent.Paulson@Sun.COM }
5130Sstevel@tonic-gate
5140Sstevel@tonic-gate if ((retval = finish_userauth_do_pam(authctxt)) != PAM_SUCCESS)
5150Sstevel@tonic-gate return 0;
5160Sstevel@tonic-gate
5170Sstevel@tonic-gate if (authctxt->method)
5180Sstevel@tonic-gate authctxt->method->authenticated = 1; /* SSHv2 */
5190Sstevel@tonic-gate
5200Sstevel@tonic-gate return 1;
5210Sstevel@tonic-gate }
5220Sstevel@tonic-gate
5230Sstevel@tonic-gate int
do_pam_non_initial_userauth(Authctxt * authctxt)5240Sstevel@tonic-gate do_pam_non_initial_userauth(Authctxt *authctxt)
5250Sstevel@tonic-gate {
5260Sstevel@tonic-gate new_start_pam(authctxt, NULL);
5270Sstevel@tonic-gate return (finish_userauth_do_pam(authctxt) == PAM_SUCCESS);
5280Sstevel@tonic-gate }
5290Sstevel@tonic-gate
5300Sstevel@tonic-gate /* Cleanly shutdown PAM */
finish_pam(Authctxt * authctxt)5310Sstevel@tonic-gate void finish_pam(Authctxt *authctxt)
5320Sstevel@tonic-gate {
5330Sstevel@tonic-gate fatal_remove_cleanup(&do_pam_cleanup_proc, authctxt->pam);
5340Sstevel@tonic-gate do_pam_cleanup_proc(authctxt->pam);
5350Sstevel@tonic-gate }
5360Sstevel@tonic-gate
5370Sstevel@tonic-gate static
5380Sstevel@tonic-gate char **
find_env(char ** env,char * var)5390Sstevel@tonic-gate find_env(char **env, char *var)
5400Sstevel@tonic-gate {
5410Sstevel@tonic-gate char **p;
5420Sstevel@tonic-gate int len;
5430Sstevel@tonic-gate
5440Sstevel@tonic-gate if (strchr(var, '=') == NULL)
5450Sstevel@tonic-gate len = strlen(var);
5460Sstevel@tonic-gate else
5470Sstevel@tonic-gate len = (strchr(var, '=') - var) + 1;
5480Sstevel@tonic-gate
5490Sstevel@tonic-gate for ( p = env ; p != NULL && *p != NULL ; p++ ) {
5500Sstevel@tonic-gate if (strncmp(*p, var, len) == 0)
5510Sstevel@tonic-gate return (p);
5520Sstevel@tonic-gate }
5530Sstevel@tonic-gate
5540Sstevel@tonic-gate return (NULL);
5550Sstevel@tonic-gate }
5560Sstevel@tonic-gate
5570Sstevel@tonic-gate /* Return list of PAM environment strings */
5580Sstevel@tonic-gate char **
fetch_pam_environment(Authctxt * authctxt)5590Sstevel@tonic-gate fetch_pam_environment(Authctxt *authctxt)
5600Sstevel@tonic-gate {
5610Sstevel@tonic-gate #ifdef HAVE_PAM_GETENVLIST
5620Sstevel@tonic-gate char **penv;
5630Sstevel@tonic-gate
5640Sstevel@tonic-gate if (authctxt == NULL || authctxt->pam == NULL ||
5650Sstevel@tonic-gate authctxt->pam->h == NULL)
5660Sstevel@tonic-gate return (NULL);
5670Sstevel@tonic-gate
5680Sstevel@tonic-gate penv = pam_getenvlist(authctxt->pam->h);
5690Sstevel@tonic-gate
5700Sstevel@tonic-gate return (penv);
5710Sstevel@tonic-gate #else /* HAVE_PAM_GETENVLIST */
5720Sstevel@tonic-gate return(NULL);
5730Sstevel@tonic-gate #endif /* HAVE_PAM_GETENVLIST */
5740Sstevel@tonic-gate }
5750Sstevel@tonic-gate
free_pam_environment(char ** env)5760Sstevel@tonic-gate void free_pam_environment(char **env)
5770Sstevel@tonic-gate {
5780Sstevel@tonic-gate int i;
5790Sstevel@tonic-gate
5800Sstevel@tonic-gate if (env != NULL) {
5810Sstevel@tonic-gate for (i = 0; env[i] != NULL; i++)
5820Sstevel@tonic-gate xfree(env[i]);
5830Sstevel@tonic-gate }
5840Sstevel@tonic-gate
5850Sstevel@tonic-gate xfree(env);
5860Sstevel@tonic-gate }
5870Sstevel@tonic-gate
5880Sstevel@tonic-gate /* Print any messages that have been generated during authentication */
5890Sstevel@tonic-gate /* or account checking to stderr */
print_pam_messages(void)5900Sstevel@tonic-gate void print_pam_messages(void)
5910Sstevel@tonic-gate {
5920Sstevel@tonic-gate if (__pam_msg != NULL)
5930Sstevel@tonic-gate (void) fputs(__pam_msg, stderr);
5940Sstevel@tonic-gate }
5950Sstevel@tonic-gate
5960Sstevel@tonic-gate /* Append a message to buffer */
message_cat(char ** p,const char * a)5970Sstevel@tonic-gate void message_cat(char **p, const char *a)
5980Sstevel@tonic-gate {
5990Sstevel@tonic-gate char *cp;
6000Sstevel@tonic-gate size_t new_len;
6010Sstevel@tonic-gate
6020Sstevel@tonic-gate new_len = strlen(a);
6030Sstevel@tonic-gate
6040Sstevel@tonic-gate if (*p) {
6050Sstevel@tonic-gate size_t len = strlen(*p);
6060Sstevel@tonic-gate
6070Sstevel@tonic-gate *p = xrealloc(*p, new_len + len + 2);
6080Sstevel@tonic-gate cp = *p + len;
6090Sstevel@tonic-gate } else
6100Sstevel@tonic-gate *p = cp = xmalloc(new_len + 2);
6110Sstevel@tonic-gate
6120Sstevel@tonic-gate (void) memcpy(cp, a, new_len);
6130Sstevel@tonic-gate cp[new_len] = '\n';
6140Sstevel@tonic-gate cp[new_len + 1] = '\0';
6150Sstevel@tonic-gate }
6160Sstevel@tonic-gate
6170Sstevel@tonic-gate #endif /* USE_PAM */
618