source: openpam/trunk/lib/openpam_dispatch.c @ 161

Last change on this file since 161 was 161, checked in by Dag-Erling Smørgrav, 18 years ago

Implement the "binding" control flag (from Solaris 9)

Sponsored by: DARPA, NAI Labs

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