xref: /netbsd-src/external/bsd/openpam/dist/lib/libpam/openpam_dispatch.c (revision bdc22b2e01993381dcefeff2bc9b56ca75a4235c)
1 /*	$NetBSD: openpam_dispatch.c,v 1.3 2017/05/06 19:50:09 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.3 2017/05/06 19:50:09 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 (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 	default:
105 		RETURNC(PAM_SYSTEM_ERR);
106 	}
107 
108 	/* execute */
109 	err = PAM_SUCCESS;
110 	fail = nsuccess = 0;
111 	for (; chain != NULL; chain = chain->next) {
112 		if (chain->module->func[primitive] == NULL) {
113 			openpam_log(PAM_LOG_ERROR, "%s: no %s()",
114 			    chain->module->path, pam_sm_func_name[primitive]);
115 			r = PAM_SYMBOL_ERR;
116 		} else {
117 			pamh->primitive = primitive;
118 			pamh->current = chain;
119 			debug = (openpam_get_option(pamh, "debug") != NULL);
120 			if (debug)
121 				++openpam_debug;
122 			openpam_log(PAM_LOG_LIBDEBUG, "calling %s() in %s",
123 			    pam_sm_func_name[primitive], chain->module->path);
124 			r = (chain->module->func[primitive])(pamh, flags,
125 			    chain->optc, (const char **)(intptr_t)chain->optv);
126 			pamh->current = NULL;
127 			openpam_log(PAM_LOG_LIBDEBUG, "%s: %s(): %s",
128 			    chain->module->path, pam_sm_func_name[primitive],
129 			    pam_strerror(pamh, r));
130 			if (debug)
131 				--openpam_debug;
132 		}
133 
134 		if (r == PAM_IGNORE)
135 			continue;
136 		if (r == PAM_SUCCESS) {
137 			++nsuccess;
138 			/*
139 			 * For pam_setcred() and pam_chauthtok() with the
140 			 * PAM_PRELIM_CHECK flag, treat "sufficient" as
141 			 * "optional".
142 			 */
143 			if ((chain->flag == PAM_SUFFICIENT ||
144 			    chain->flag == PAM_BINDING) && !fail &&
145 			    primitive != PAM_SM_SETCRED &&
146 			    !(primitive == PAM_SM_CHAUTHTOK &&
147 				(flags & PAM_PRELIM_CHECK)))
148 				break;
149 			continue;
150 		}
151 
152 		openpam_check_error_code(primitive, r);
153 
154 		/*
155 		 * Record the return code from the first module to
156 		 * fail.  If a required module fails, record the
157 		 * return code from the first required module to fail.
158 		 */
159 		if (err == PAM_SUCCESS)
160 			err = r;
161 		if ((chain->flag == PAM_REQUIRED ||
162 		    chain->flag == PAM_BINDING) && !fail) {
163 			openpam_log(PAM_LOG_LIBDEBUG, "required module failed");
164 			fail = 1;
165 			err = r;
166 		}
167 
168 		/*
169 		 * If a requisite module fails, terminate the chain
170 		 * immediately.
171 		 */
172 		if (chain->flag == PAM_REQUISITE) {
173 			openpam_log(PAM_LOG_LIBDEBUG, "requisite module failed");
174 			fail = 1;
175 			break;
176 		}
177 	}
178 
179 	if (!fail && err != PAM_NEW_AUTHTOK_REQD)
180 		err = PAM_SUCCESS;
181 
182 	/*
183 	 * Require the chain to be non-empty, and at least one module
184 	 * in the chain to be successful, so that we don't fail open.
185 	 */
186 	if (err == PAM_SUCCESS && nsuccess < 1) {
187 		openpam_log(PAM_LOG_ERROR,
188 		    "all modules were unsuccessful for %s()",
189 		    pam_sm_func_name[primitive]);
190 		err = PAM_SYSTEM_ERR;
191 	}
192 
193 	RETURNC(err);
194 }
195 
196 #if !defined(OPENPAM_RELAX_CHECKS)
197 static void
198 openpam_check_error_code(int primitive, int r)
199 {
200 	/* common error codes */
201 	if (r == PAM_SUCCESS ||
202 	    r == PAM_SYSTEM_ERR ||
203 	    r == PAM_SERVICE_ERR ||
204 	    r == PAM_BUF_ERR ||
205 	    r == PAM_CONV_ERR ||
206 	    r == PAM_PERM_DENIED ||
207 	    r == PAM_ABORT)
208 		return;
209 
210 	/* specific error codes */
211 	switch (primitive) {
212 	case PAM_SM_AUTHENTICATE:
213 		if (r == PAM_AUTH_ERR ||
214 		    r == PAM_CRED_INSUFFICIENT ||
215 		    r == PAM_AUTHINFO_UNAVAIL ||
216 		    r == PAM_USER_UNKNOWN ||
217 		    r == PAM_MAXTRIES)
218 			return;
219 		break;
220 	case PAM_SM_SETCRED:
221 		if (r == PAM_CRED_UNAVAIL ||
222 		    r == PAM_CRED_EXPIRED ||
223 		    r == PAM_USER_UNKNOWN ||
224 		    r == PAM_CRED_ERR)
225 			return;
226 		break;
227 	case PAM_SM_ACCT_MGMT:
228 		if (r == PAM_USER_UNKNOWN ||
229 		    r == PAM_AUTH_ERR ||
230 		    r == PAM_NEW_AUTHTOK_REQD ||
231 		    r == PAM_ACCT_EXPIRED)
232 			return;
233 		break;
234 	case PAM_SM_OPEN_SESSION:
235 	case PAM_SM_CLOSE_SESSION:
236 		if (r == PAM_SESSION_ERR)
237 			return;
238 		break;
239 	case PAM_SM_CHAUTHTOK:
240 		if (r == PAM_PERM_DENIED ||
241 		    r == PAM_AUTHTOK_ERR ||
242 		    r == PAM_AUTHTOK_RECOVERY_ERR ||
243 		    r == PAM_AUTHTOK_LOCK_BUSY ||
244 		    r == PAM_AUTHTOK_DISABLE_AGING ||
245 		    r == PAM_TRY_AGAIN)
246 			return;
247 		break;
248 	}
249 
250 	openpam_log(PAM_LOG_ERROR, "%s(): unexpected return value %d",
251 	    pam_sm_func_name[primitive], r);
252 }
253 #endif /* !defined(OPENPAM_RELAX_CHECKS) */
254 
255 /*
256  * NODOC
257  *
258  * Error codes:
259  */
260