xref: /netbsd-src/external/bsd/openpam/dist/lib/libpam/openpam_dispatch.c (revision 0d9d0fd8a30be9a1924e715bbcf67a4a83efd262)
1 /*	$NetBSD: openpam_dispatch.c,v 1.5 2023/06/30 21:46:20 christos Exp $	*/
2 
3 /*-
4  * Copyright (c) 2002-2003 Networks Associates Technology, Inc.
5  * Copyright (c) 2004-2017 Dag-Erling Smørgrav
6  * All rights reserved.
7  *
8  * This software was developed for the FreeBSD Project by ThinkSec AS and
9  * Network Associates Laboratories, the Security Research Division of
10  * Network Associates, Inc.  under DARPA/SPAWAR contract N66001-01-C-8035
11  * ("CBOSS"), as part of the DARPA CHATS research program.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  * 3. The name of the author may not be used to endorse or promote
22  *    products derived from this software without specific prior written
23  *    permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * SUCH DAMAGE.
36  */
37 
38 #ifdef HAVE_CONFIG_H
39 # include "config.h"
40 #endif
41 
42 #include <sys/cdefs.h>
43 __RCSID("$NetBSD: openpam_dispatch.c,v 1.5 2023/06/30 21:46:20 christos Exp $");
44 
45 #include <sys/param.h>
46 
47 #include <stdint.h>
48 
49 #include <security/pam_appl.h>
50 
51 #include "openpam_impl.h"
52 
53 #if !defined(OPENPAM_RELAX_CHECKS)
54 static void openpam_check_error_code(int, int);
55 #else
56 #define openpam_check_error_code(a, b)
57 #endif /* !defined(OPENPAM_RELAX_CHECKS) */
58 
59 /*
60  * OpenPAM internal
61  *
62  * Execute a module chain
63  */
64 
65 int
openpam_dispatch(pam_handle_t * pamh,int primitive,int flags)66 openpam_dispatch(pam_handle_t *pamh,
67 	int primitive,
68 	int flags)
69 {
70 	pam_chain_t *chain;
71 	int err, fail, nsuccess, r;
72 	int debug;
73 
74 	ENTER();
75 
76 	/* prevent recursion */
77 	if (pamh->current != NULL) {
78 		openpam_log(PAM_LOG_ERROR,
79 		    "%s() called while %s::%s() is in progress",
80 		    pam_func_name[primitive],
81 		    pamh->current->module->path,
82 		    pam_sm_func_name[pamh->primitive]);
83 		RETURNC(PAM_ABORT);
84 	}
85 
86 	/* pick a chain */
87 	switch ((enum openpam_sm_primitives)primitive) {
88 	case PAM_SM_AUTHENTICATE:
89 	case PAM_SM_SETCRED:
90 		chain = pamh->chains[PAM_AUTH];
91 		break;
92 	case PAM_SM_ACCT_MGMT:
93 		chain = pamh->chains[PAM_ACCOUNT];
94 		break;
95 	case PAM_SM_OPEN_SESSION:
96 	case PAM_SM_CLOSE_SESSION:
97 		chain = pamh->chains[PAM_SESSION];
98 		break;
99 	case PAM_SM_CHAUTHTOK:
100 		chain = pamh->chains[PAM_PASSWORD];
101 		break;
102 	case PAM_NUM_PRIMITIVES:
103 	default:
104 		RETURNC(PAM_SYSTEM_ERR);
105 	}
106 
107 	/* execute */
108 	err = PAM_SUCCESS;
109 	fail = nsuccess = 0;
110 	for (; chain != NULL; chain = chain->next) {
111 		if (chain->module->func[primitive] == NULL) {
112 			openpam_log(PAM_LOG_ERROR, "%s: no %s()",
113 			    chain->module->path, pam_sm_func_name[primitive]);
114 			r = PAM_SYMBOL_ERR;
115 		} else {
116 			pamh->primitive = primitive;
117 			pamh->current = chain;
118 			debug = (openpam_get_option(pamh, "debug") != NULL);
119 			if (debug)
120 				++openpam_debug;
121 			openpam_log(PAM_LOG_LIBDEBUG, "calling %s() in %s",
122 			    pam_sm_func_name[primitive], chain->module->path);
123 			r = (chain->module->func[primitive])(pamh, flags,
124 			    chain->optc, (const char **)(intptr_t)chain->optv);
125 			pamh->current = NULL;
126 			openpam_log(PAM_LOG_LIBDEBUG, "%s: %s(): %s",
127 			    chain->module->path, pam_sm_func_name[primitive],
128 			    pam_strerror(pamh, r));
129 			if (debug)
130 				--openpam_debug;
131 		}
132 
133 		if (r == PAM_IGNORE)
134 			continue;
135 		if (r == PAM_SUCCESS) {
136 			++nsuccess;
137 			/*
138 			 * For pam_setcred() and pam_chauthtok() with the
139 			 * PAM_PRELIM_CHECK flag, treat "sufficient" as
140 			 * "optional".
141 			 */
142 			if ((chain->flag == PAM_SUFFICIENT ||
143 			    chain->flag == PAM_BINDING) && !fail &&
144 			    primitive != PAM_SM_SETCRED &&
145 			    !(primitive == PAM_SM_CHAUTHTOK &&
146 				(flags & PAM_PRELIM_CHECK)))
147 				break;
148 			continue;
149 		}
150 
151 		openpam_check_error_code(primitive, r);
152 
153 		/*
154 		 * Record the return code from the first module to
155 		 * fail.  If a required module fails, record the
156 		 * return code from the first required module to fail.
157 		 */
158 		if (err == PAM_SUCCESS)
159 			err = r;
160 		if ((chain->flag == PAM_REQUIRED ||
161 		    chain->flag == PAM_BINDING) && !fail) {
162 			openpam_log(PAM_LOG_LIBDEBUG, "required module failed");
163 			fail = 1;
164 			err = r;
165 		}
166 
167 		/*
168 		 * If a requisite module fails, terminate the chain
169 		 * immediately.
170 		 */
171 		if (chain->flag == PAM_REQUISITE) {
172 			openpam_log(PAM_LOG_LIBDEBUG, "requisite module failed");
173 			fail = 1;
174 			break;
175 		}
176 	}
177 
178 	if (!fail && err != PAM_NEW_AUTHTOK_REQD)
179 		err = PAM_SUCCESS;
180 
181 	/*
182 	 * Require the chain to be non-empty, and at least one module
183 	 * in the chain to be successful, so that we don't fail open.
184 	 */
185 	if (err == PAM_SUCCESS && nsuccess < 1) {
186 		openpam_log(PAM_LOG_ERROR,
187 		    "all modules were unsuccessful for %s()",
188 		    pam_sm_func_name[primitive]);
189 		err = PAM_SYSTEM_ERR;
190 	}
191 
192 	RETURNC(err);
193 }
194 
195 #if !defined(OPENPAM_RELAX_CHECKS)
196 static void
openpam_check_error_code(int primitive,int r)197 openpam_check_error_code(int primitive, int r)
198 {
199 	/* common error codes */
200 	if (r == PAM_SUCCESS ||
201 	    r == PAM_SYSTEM_ERR ||
202 	    r == PAM_SERVICE_ERR ||
203 	    r == PAM_BUF_ERR ||
204 	    r == PAM_CONV_ERR ||
205 	    r == PAM_PERM_DENIED ||
206 	    r == PAM_ABORT)
207 		return;
208 
209 	/* specific error codes */
210 	switch ((enum openpam_sm_primitives)primitive) {
211 	case PAM_SM_AUTHENTICATE:
212 		if (r == PAM_AUTH_ERR ||
213 		    r == PAM_CRED_INSUFFICIENT ||
214 		    r == PAM_AUTHINFO_UNAVAIL ||
215 		    r == PAM_USER_UNKNOWN ||
216 		    r == PAM_MAXTRIES)
217 			return;
218 		break;
219 	case PAM_SM_SETCRED:
220 		if (r == PAM_CRED_UNAVAIL ||
221 		    r == PAM_CRED_EXPIRED ||
222 		    r == PAM_USER_UNKNOWN ||
223 		    r == PAM_CRED_ERR)
224 			return;
225 		break;
226 	case PAM_SM_ACCT_MGMT:
227 		if (r == PAM_USER_UNKNOWN ||
228 		    r == PAM_AUTH_ERR ||
229 		    r == PAM_NEW_AUTHTOK_REQD ||
230 		    r == PAM_ACCT_EXPIRED)
231 			return;
232 		break;
233 	case PAM_SM_OPEN_SESSION:
234 	case PAM_SM_CLOSE_SESSION:
235 		if (r == PAM_SESSION_ERR)
236 			return;
237 		break;
238 	case PAM_SM_CHAUTHTOK:
239 		if (r == PAM_PERM_DENIED ||
240 		    r == PAM_AUTHTOK_ERR ||
241 		    r == PAM_AUTHTOK_RECOVERY_ERR ||
242 		    r == PAM_AUTHTOK_LOCK_BUSY ||
243 		    r == PAM_AUTHTOK_DISABLE_AGING ||
244 		    r == PAM_TRY_AGAIN)
245 			return;
246 		break;
247 	case PAM_NUM_PRIMITIVES:
248 		break;
249 	}
250 
251 	openpam_log(PAM_LOG_ERROR, "%s(): unexpected return value %d",
252 	    pam_sm_func_name[primitive], r);
253 }
254 #endif /* !defined(OPENPAM_RELAX_CHECKS) */
255 
256 /*
257  * NODOC
258  *
259  * Error codes:
260  */
261