1*0d9d0fd8Schristos /* $NetBSD: openpam_dispatch.c,v 1.5 2023/06/30 21:46:20 christos Exp $ */
2201780c4Schristos
376e8c542Schristos /*-
476e8c542Schristos * Copyright (c) 2002-2003 Networks Associates Technology, Inc.
54cb4af11Schristos * Copyright (c) 2004-2017 Dag-Erling Smørgrav
676e8c542Schristos * All rights reserved.
776e8c542Schristos *
876e8c542Schristos * This software was developed for the FreeBSD Project by ThinkSec AS and
976e8c542Schristos * Network Associates Laboratories, the Security Research Division of
1076e8c542Schristos * Network Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035
1176e8c542Schristos * ("CBOSS"), as part of the DARPA CHATS research program.
1276e8c542Schristos *
1376e8c542Schristos * Redistribution and use in source and binary forms, with or without
1476e8c542Schristos * modification, are permitted provided that the following conditions
1576e8c542Schristos * are met:
1676e8c542Schristos * 1. Redistributions of source code must retain the above copyright
1776e8c542Schristos * notice, this list of conditions and the following disclaimer.
1876e8c542Schristos * 2. Redistributions in binary form must reproduce the above copyright
1976e8c542Schristos * notice, this list of conditions and the following disclaimer in the
2076e8c542Schristos * documentation and/or other materials provided with the distribution.
2176e8c542Schristos * 3. The name of the author may not be used to endorse or promote
2276e8c542Schristos * products derived from this software without specific prior written
2376e8c542Schristos * permission.
2476e8c542Schristos *
2576e8c542Schristos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
2676e8c542Schristos * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2776e8c542Schristos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2876e8c542Schristos * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2976e8c542Schristos * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
3076e8c542Schristos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3176e8c542Schristos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3276e8c542Schristos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3376e8c542Schristos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3476e8c542Schristos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3576e8c542Schristos * SUCH DAMAGE.
3676e8c542Schristos */
3776e8c542Schristos
3876e8c542Schristos #ifdef HAVE_CONFIG_H
3976e8c542Schristos # include "config.h"
4076e8c542Schristos #endif
4176e8c542Schristos
42201780c4Schristos #include <sys/cdefs.h>
43*0d9d0fd8Schristos __RCSID("$NetBSD: openpam_dispatch.c,v 1.5 2023/06/30 21:46:20 christos Exp $");
44201780c4Schristos
4576e8c542Schristos #include <sys/param.h>
4676e8c542Schristos
474cb4af11Schristos #include <stdint.h>
484cb4af11Schristos
4976e8c542Schristos #include <security/pam_appl.h>
5076e8c542Schristos
5176e8c542Schristos #include "openpam_impl.h"
5276e8c542Schristos
5376e8c542Schristos #if !defined(OPENPAM_RELAX_CHECKS)
5476e8c542Schristos static void openpam_check_error_code(int, int);
5576e8c542Schristos #else
5676e8c542Schristos #define openpam_check_error_code(a, b)
5776e8c542Schristos #endif /* !defined(OPENPAM_RELAX_CHECKS) */
5876e8c542Schristos
5976e8c542Schristos /*
6076e8c542Schristos * OpenPAM internal
6176e8c542Schristos *
6276e8c542Schristos * Execute a module chain
6376e8c542Schristos */
6476e8c542Schristos
6576e8c542Schristos int
openpam_dispatch(pam_handle_t * pamh,int primitive,int flags)6676e8c542Schristos openpam_dispatch(pam_handle_t *pamh,
6776e8c542Schristos int primitive,
6876e8c542Schristos int flags)
6976e8c542Schristos {
7076e8c542Schristos pam_chain_t *chain;
7176e8c542Schristos int err, fail, nsuccess, r;
7276e8c542Schristos int debug;
7376e8c542Schristos
7476e8c542Schristos ENTER();
7576e8c542Schristos
7676e8c542Schristos /* prevent recursion */
7776e8c542Schristos if (pamh->current != NULL) {
7876e8c542Schristos openpam_log(PAM_LOG_ERROR,
7976e8c542Schristos "%s() called while %s::%s() is in progress",
8076e8c542Schristos pam_func_name[primitive],
8176e8c542Schristos pamh->current->module->path,
8276e8c542Schristos pam_sm_func_name[pamh->primitive]);
8376e8c542Schristos RETURNC(PAM_ABORT);
8476e8c542Schristos }
8576e8c542Schristos
8676e8c542Schristos /* pick a chain */
8772d8d6c3Schristos switch ((enum openpam_sm_primitives)primitive) {
8876e8c542Schristos case PAM_SM_AUTHENTICATE:
8976e8c542Schristos case PAM_SM_SETCRED:
9076e8c542Schristos chain = pamh->chains[PAM_AUTH];
9176e8c542Schristos break;
9276e8c542Schristos case PAM_SM_ACCT_MGMT:
9376e8c542Schristos chain = pamh->chains[PAM_ACCOUNT];
9476e8c542Schristos break;
9576e8c542Schristos case PAM_SM_OPEN_SESSION:
9676e8c542Schristos case PAM_SM_CLOSE_SESSION:
9776e8c542Schristos chain = pamh->chains[PAM_SESSION];
9876e8c542Schristos break;
9976e8c542Schristos case PAM_SM_CHAUTHTOK:
10076e8c542Schristos chain = pamh->chains[PAM_PASSWORD];
10176e8c542Schristos break;
10272d8d6c3Schristos case PAM_NUM_PRIMITIVES:
10376e8c542Schristos default:
10476e8c542Schristos RETURNC(PAM_SYSTEM_ERR);
10576e8c542Schristos }
10676e8c542Schristos
10776e8c542Schristos /* execute */
10876e8c542Schristos err = PAM_SUCCESS;
10976e8c542Schristos fail = nsuccess = 0;
11076e8c542Schristos for (; chain != NULL; chain = chain->next) {
11176e8c542Schristos if (chain->module->func[primitive] == NULL) {
11276e8c542Schristos openpam_log(PAM_LOG_ERROR, "%s: no %s()",
11376e8c542Schristos chain->module->path, pam_sm_func_name[primitive]);
1144cb4af11Schristos r = PAM_SYMBOL_ERR;
11576e8c542Schristos } else {
11676e8c542Schristos pamh->primitive = primitive;
11776e8c542Schristos pamh->current = chain;
11876e8c542Schristos debug = (openpam_get_option(pamh, "debug") != NULL);
11976e8c542Schristos if (debug)
12076e8c542Schristos ++openpam_debug;
12176e8c542Schristos openpam_log(PAM_LOG_LIBDEBUG, "calling %s() in %s",
12276e8c542Schristos pam_sm_func_name[primitive], chain->module->path);
12376e8c542Schristos r = (chain->module->func[primitive])(pamh, flags,
1244cb4af11Schristos chain->optc, (const char **)(intptr_t)chain->optv);
12576e8c542Schristos pamh->current = NULL;
12676e8c542Schristos openpam_log(PAM_LOG_LIBDEBUG, "%s: %s(): %s",
12776e8c542Schristos chain->module->path, pam_sm_func_name[primitive],
12876e8c542Schristos pam_strerror(pamh, r));
12976e8c542Schristos if (debug)
13076e8c542Schristos --openpam_debug;
13176e8c542Schristos }
13276e8c542Schristos
13376e8c542Schristos if (r == PAM_IGNORE)
13476e8c542Schristos continue;
13576e8c542Schristos if (r == PAM_SUCCESS) {
13676e8c542Schristos ++nsuccess;
13776e8c542Schristos /*
13876e8c542Schristos * For pam_setcred() and pam_chauthtok() with the
13976e8c542Schristos * PAM_PRELIM_CHECK flag, treat "sufficient" as
14076e8c542Schristos * "optional".
14176e8c542Schristos */
14276e8c542Schristos if ((chain->flag == PAM_SUFFICIENT ||
14376e8c542Schristos chain->flag == PAM_BINDING) && !fail &&
14476e8c542Schristos primitive != PAM_SM_SETCRED &&
14576e8c542Schristos !(primitive == PAM_SM_CHAUTHTOK &&
14676e8c542Schristos (flags & PAM_PRELIM_CHECK)))
14776e8c542Schristos break;
14876e8c542Schristos continue;
14976e8c542Schristos }
15076e8c542Schristos
15176e8c542Schristos openpam_check_error_code(primitive, r);
15276e8c542Schristos
15376e8c542Schristos /*
15476e8c542Schristos * Record the return code from the first module to
15576e8c542Schristos * fail. If a required module fails, record the
15676e8c542Schristos * return code from the first required module to fail.
15776e8c542Schristos */
15876e8c542Schristos if (err == PAM_SUCCESS)
15976e8c542Schristos err = r;
16076e8c542Schristos if ((chain->flag == PAM_REQUIRED ||
16176e8c542Schristos chain->flag == PAM_BINDING) && !fail) {
16276e8c542Schristos openpam_log(PAM_LOG_LIBDEBUG, "required module failed");
16376e8c542Schristos fail = 1;
16476e8c542Schristos err = r;
16576e8c542Schristos }
16676e8c542Schristos
16776e8c542Schristos /*
16876e8c542Schristos * If a requisite module fails, terminate the chain
16976e8c542Schristos * immediately.
17076e8c542Schristos */
17176e8c542Schristos if (chain->flag == PAM_REQUISITE) {
17276e8c542Schristos openpam_log(PAM_LOG_LIBDEBUG, "requisite module failed");
17376e8c542Schristos fail = 1;
17476e8c542Schristos break;
17576e8c542Schristos }
17676e8c542Schristos }
17776e8c542Schristos
17876e8c542Schristos if (!fail && err != PAM_NEW_AUTHTOK_REQD)
17976e8c542Schristos err = PAM_SUCCESS;
18076e8c542Schristos
18176e8c542Schristos /*
18276e8c542Schristos * Require the chain to be non-empty, and at least one module
18376e8c542Schristos * in the chain to be successful, so that we don't fail open.
18476e8c542Schristos */
18576e8c542Schristos if (err == PAM_SUCCESS && nsuccess < 1) {
18676e8c542Schristos openpam_log(PAM_LOG_ERROR,
18776e8c542Schristos "all modules were unsuccessful for %s()",
18876e8c542Schristos pam_sm_func_name[primitive]);
18976e8c542Schristos err = PAM_SYSTEM_ERR;
19076e8c542Schristos }
19176e8c542Schristos
19276e8c542Schristos RETURNC(err);
19376e8c542Schristos }
19476e8c542Schristos
19576e8c542Schristos #if !defined(OPENPAM_RELAX_CHECKS)
19676e8c542Schristos static void
openpam_check_error_code(int primitive,int r)19776e8c542Schristos openpam_check_error_code(int primitive, int r)
19876e8c542Schristos {
19976e8c542Schristos /* common error codes */
20076e8c542Schristos if (r == PAM_SUCCESS ||
20176e8c542Schristos r == PAM_SYSTEM_ERR ||
20276e8c542Schristos r == PAM_SERVICE_ERR ||
20376e8c542Schristos r == PAM_BUF_ERR ||
20476e8c542Schristos r == PAM_CONV_ERR ||
20576e8c542Schristos r == PAM_PERM_DENIED ||
20676e8c542Schristos r == PAM_ABORT)
20776e8c542Schristos return;
20876e8c542Schristos
20976e8c542Schristos /* specific error codes */
21072d8d6c3Schristos switch ((enum openpam_sm_primitives)primitive) {
21176e8c542Schristos case PAM_SM_AUTHENTICATE:
21276e8c542Schristos if (r == PAM_AUTH_ERR ||
21376e8c542Schristos r == PAM_CRED_INSUFFICIENT ||
21476e8c542Schristos r == PAM_AUTHINFO_UNAVAIL ||
21576e8c542Schristos r == PAM_USER_UNKNOWN ||
21676e8c542Schristos r == PAM_MAXTRIES)
21776e8c542Schristos return;
21876e8c542Schristos break;
21976e8c542Schristos case PAM_SM_SETCRED:
22076e8c542Schristos if (r == PAM_CRED_UNAVAIL ||
22176e8c542Schristos r == PAM_CRED_EXPIRED ||
22276e8c542Schristos r == PAM_USER_UNKNOWN ||
22376e8c542Schristos r == PAM_CRED_ERR)
22476e8c542Schristos return;
22576e8c542Schristos break;
22676e8c542Schristos case PAM_SM_ACCT_MGMT:
22776e8c542Schristos if (r == PAM_USER_UNKNOWN ||
22876e8c542Schristos r == PAM_AUTH_ERR ||
22976e8c542Schristos r == PAM_NEW_AUTHTOK_REQD ||
23076e8c542Schristos r == PAM_ACCT_EXPIRED)
23176e8c542Schristos return;
23276e8c542Schristos break;
23376e8c542Schristos case PAM_SM_OPEN_SESSION:
23476e8c542Schristos case PAM_SM_CLOSE_SESSION:
23576e8c542Schristos if (r == PAM_SESSION_ERR)
23676e8c542Schristos return;
23776e8c542Schristos break;
23876e8c542Schristos case PAM_SM_CHAUTHTOK:
23976e8c542Schristos if (r == PAM_PERM_DENIED ||
24076e8c542Schristos r == PAM_AUTHTOK_ERR ||
24176e8c542Schristos r == PAM_AUTHTOK_RECOVERY_ERR ||
24276e8c542Schristos r == PAM_AUTHTOK_LOCK_BUSY ||
24376e8c542Schristos r == PAM_AUTHTOK_DISABLE_AGING ||
24476e8c542Schristos r == PAM_TRY_AGAIN)
24576e8c542Schristos return;
24676e8c542Schristos break;
24772d8d6c3Schristos case PAM_NUM_PRIMITIVES:
24872d8d6c3Schristos break;
24976e8c542Schristos }
25076e8c542Schristos
25176e8c542Schristos openpam_log(PAM_LOG_ERROR, "%s(): unexpected return value %d",
25276e8c542Schristos pam_sm_func_name[primitive], r);
25376e8c542Schristos }
25476e8c542Schristos #endif /* !defined(OPENPAM_RELAX_CHECKS) */
25576e8c542Schristos
25676e8c542Schristos /*
25776e8c542Schristos * NODOC
25876e8c542Schristos *
25976e8c542Schristos * Error codes:
26076e8c542Schristos */
261