xref: /netbsd-src/external/bsd/openpam/dist/lib/libpam/openpam_dispatch.c (revision 82d56013d7b633d116a93943de88e08335357a7c)
1 /*	$NetBSD: openpam_dispatch.c,v 1.4 2021/03/08 19:38:10 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  * $OpenPAM: openpam_dispatch.c 938 2017-04-30 21:34:42Z des $
38  */
39 
40 #ifdef HAVE_CONFIG_H
41 # include "config.h"
42 #endif
43 
44 #include <sys/cdefs.h>
45 __RCSID("$NetBSD: openpam_dispatch.c,v 1.4 2021/03/08 19:38:10 christos Exp $");
46 
47 #include <sys/param.h>
48 
49 #include <stdint.h>
50 
51 #include <security/pam_appl.h>
52 
53 #include "openpam_impl.h"
54 
55 #if !defined(OPENPAM_RELAX_CHECKS)
56 static void openpam_check_error_code(int, int);
57 #else
58 #define openpam_check_error_code(a, b)
59 #endif /* !defined(OPENPAM_RELAX_CHECKS) */
60 
61 /*
62  * OpenPAM internal
63  *
64  * Execute a module chain
65  */
66 
67 int
68 openpam_dispatch(pam_handle_t *pamh,
69 	int primitive,
70 	int flags)
71 {
72 	pam_chain_t *chain;
73 	int err, fail, nsuccess, r;
74 	int debug;
75 
76 	ENTER();
77 
78 	/* prevent recursion */
79 	if (pamh->current != NULL) {
80 		openpam_log(PAM_LOG_ERROR,
81 		    "%s() called while %s::%s() is in progress",
82 		    pam_func_name[primitive],
83 		    pamh->current->module->path,
84 		    pam_sm_func_name[pamh->primitive]);
85 		RETURNC(PAM_ABORT);
86 	}
87 
88 	/* pick a chain */
89 	switch ((enum openpam_sm_primitives)primitive) {
90 	case PAM_SM_AUTHENTICATE:
91 	case PAM_SM_SETCRED:
92 		chain = pamh->chains[PAM_AUTH];
93 		break;
94 	case PAM_SM_ACCT_MGMT:
95 		chain = pamh->chains[PAM_ACCOUNT];
96 		break;
97 	case PAM_SM_OPEN_SESSION:
98 	case PAM_SM_CLOSE_SESSION:
99 		chain = pamh->chains[PAM_SESSION];
100 		break;
101 	case PAM_SM_CHAUTHTOK:
102 		chain = pamh->chains[PAM_PASSWORD];
103 		break;
104 	case PAM_NUM_PRIMITIVES:
105 	default:
106 		RETURNC(PAM_SYSTEM_ERR);
107 	}
108 
109 	/* execute */
110 	err = PAM_SUCCESS;
111 	fail = nsuccess = 0;
112 	for (; chain != NULL; chain = chain->next) {
113 		if (chain->module->func[primitive] == NULL) {
114 			openpam_log(PAM_LOG_ERROR, "%s: no %s()",
115 			    chain->module->path, pam_sm_func_name[primitive]);
116 			r = PAM_SYMBOL_ERR;
117 		} else {
118 			pamh->primitive = primitive;
119 			pamh->current = chain;
120 			debug = (openpam_get_option(pamh, "debug") != NULL);
121 			if (debug)
122 				++openpam_debug;
123 			openpam_log(PAM_LOG_LIBDEBUG, "calling %s() in %s",
124 			    pam_sm_func_name[primitive], chain->module->path);
125 			r = (chain->module->func[primitive])(pamh, flags,
126 			    chain->optc, (const char **)(intptr_t)chain->optv);
127 			pamh->current = NULL;
128 			openpam_log(PAM_LOG_LIBDEBUG, "%s: %s(): %s",
129 			    chain->module->path, pam_sm_func_name[primitive],
130 			    pam_strerror(pamh, r));
131 			if (debug)
132 				--openpam_debug;
133 		}
134 
135 		if (r == PAM_IGNORE)
136 			continue;
137 		if (r == PAM_SUCCESS) {
138 			++nsuccess;
139 			/*
140 			 * For pam_setcred() and pam_chauthtok() with the
141 			 * PAM_PRELIM_CHECK flag, treat "sufficient" as
142 			 * "optional".
143 			 */
144 			if ((chain->flag == PAM_SUFFICIENT ||
145 			    chain->flag == PAM_BINDING) && !fail &&
146 			    primitive != PAM_SM_SETCRED &&
147 			    !(primitive == PAM_SM_CHAUTHTOK &&
148 				(flags & PAM_PRELIM_CHECK)))
149 				break;
150 			continue;
151 		}
152 
153 		openpam_check_error_code(primitive, r);
154 
155 		/*
156 		 * Record the return code from the first module to
157 		 * fail.  If a required module fails, record the
158 		 * return code from the first required module to fail.
159 		 */
160 		if (err == PAM_SUCCESS)
161 			err = r;
162 		if ((chain->flag == PAM_REQUIRED ||
163 		    chain->flag == PAM_BINDING) && !fail) {
164 			openpam_log(PAM_LOG_LIBDEBUG, "required module failed");
165 			fail = 1;
166 			err = r;
167 		}
168 
169 		/*
170 		 * If a requisite module fails, terminate the chain
171 		 * immediately.
172 		 */
173 		if (chain->flag == PAM_REQUISITE) {
174 			openpam_log(PAM_LOG_LIBDEBUG, "requisite module failed");
175 			fail = 1;
176 			break;
177 		}
178 	}
179 
180 	if (!fail && err != PAM_NEW_AUTHTOK_REQD)
181 		err = PAM_SUCCESS;
182 
183 	/*
184 	 * Require the chain to be non-empty, and at least one module
185 	 * in the chain to be successful, so that we don't fail open.
186 	 */
187 	if (err == PAM_SUCCESS && nsuccess < 1) {
188 		openpam_log(PAM_LOG_ERROR,
189 		    "all modules were unsuccessful for %s()",
190 		    pam_sm_func_name[primitive]);
191 		err = PAM_SYSTEM_ERR;
192 	}
193 
194 	RETURNC(err);
195 }
196 
197 #if !defined(OPENPAM_RELAX_CHECKS)
198 static void
199 openpam_check_error_code(int primitive, int r)
200 {
201 	/* common error codes */
202 	if (r == PAM_SUCCESS ||
203 	    r == PAM_SYSTEM_ERR ||
204 	    r == PAM_SERVICE_ERR ||
205 	    r == PAM_BUF_ERR ||
206 	    r == PAM_CONV_ERR ||
207 	    r == PAM_PERM_DENIED ||
208 	    r == PAM_ABORT)
209 		return;
210 
211 	/* specific error codes */
212 	switch ((enum openpam_sm_primitives)primitive) {
213 	case PAM_SM_AUTHENTICATE:
214 		if (r == PAM_AUTH_ERR ||
215 		    r == PAM_CRED_INSUFFICIENT ||
216 		    r == PAM_AUTHINFO_UNAVAIL ||
217 		    r == PAM_USER_UNKNOWN ||
218 		    r == PAM_MAXTRIES)
219 			return;
220 		break;
221 	case PAM_SM_SETCRED:
222 		if (r == PAM_CRED_UNAVAIL ||
223 		    r == PAM_CRED_EXPIRED ||
224 		    r == PAM_USER_UNKNOWN ||
225 		    r == PAM_CRED_ERR)
226 			return;
227 		break;
228 	case PAM_SM_ACCT_MGMT:
229 		if (r == PAM_USER_UNKNOWN ||
230 		    r == PAM_AUTH_ERR ||
231 		    r == PAM_NEW_AUTHTOK_REQD ||
232 		    r == PAM_ACCT_EXPIRED)
233 			return;
234 		break;
235 	case PAM_SM_OPEN_SESSION:
236 	case PAM_SM_CLOSE_SESSION:
237 		if (r == PAM_SESSION_ERR)
238 			return;
239 		break;
240 	case PAM_SM_CHAUTHTOK:
241 		if (r == PAM_PERM_DENIED ||
242 		    r == PAM_AUTHTOK_ERR ||
243 		    r == PAM_AUTHTOK_RECOVERY_ERR ||
244 		    r == PAM_AUTHTOK_LOCK_BUSY ||
245 		    r == PAM_AUTHTOK_DISABLE_AGING ||
246 		    r == PAM_TRY_AGAIN)
247 			return;
248 		break;
249 	case PAM_NUM_PRIMITIVES:
250 		break;
251 	}
252 
253 	openpam_log(PAM_LOG_ERROR, "%s(): unexpected return value %d",
254 	    pam_sm_func_name[primitive], r);
255 }
256 #endif /* !defined(OPENPAM_RELAX_CHECKS) */
257 
258 /*
259  * NODOC
260  *
261  * Error codes:
262  */
263