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

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

Log a debug message before calling the service function.

Accept PAM_SUCCESS and PAM_ABORT as valid return codes, even though
the normal code path will not call _openpam_check_error_code() if
the module returns one of them.

Sponsored by: DARPA, NAI Labs

  • Property svn:keywords set to Id LastChangedRevision HeadURL LastChangedDate LastChangedBy
File size: 5.6 KB
Line 
1/*-
2 * Copyright (c) 2002 Networks Associates Technologies, Inc.
3 * All rights reserved.
4 *
5 * This software was developed for the FreeBSD Project by ThinkSec AS and
6 * NAI Labs, the Security Research Division of Network Associates, Inc.
7 * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the
8 * 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 * $Id: openpam_dispatch.c 53 2002-02-13 00:43:57Z des $
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 * Execute a module chain
51 */
52
53int
54openpam_dispatch(pam_handle_t *pamh,
55        int primitive,
56        int flags)
57{
58        pam_chain_t *chain;
59        int err, fail, r;
60
61        if (pamh == NULL)
62                return (PAM_SYSTEM_ERR);
63
64        /* prevent recursion */
65        if (pamh->current != NULL) {
66                openpam_log(PAM_LOG_ERROR, "indirect recursion");
67                return (PAM_ABORT);
68        }
69
70        /* pick a chain */
71        switch (primitive) {
72        case PAM_SM_AUTHENTICATE:
73        case PAM_SM_SETCRED:
74                chain = pamh->chains[PAM_AUTH];
75                break;
76        case PAM_SM_ACCT_MGMT:
77                chain = pamh->chains[PAM_ACCOUNT];
78                break;
79        case PAM_SM_OPEN_SESSION:
80        case PAM_SM_CLOSE_SESSION:
81                chain = pamh->chains[PAM_SESSION];
82                break;
83        case PAM_SM_CHAUTHTOK:
84                chain = pamh->chains[PAM_PASSWORD];
85                break;
86        default:
87                return (PAM_SYSTEM_ERR);
88        }
89
90        /* execute */
91        for (err = fail = 0; chain != NULL; chain = chain->next) {
92                openpam_log(PAM_LOG_DEBUG, "calling %s() in %s",
93                    _pam_sm_func_name[primitive], chain->module->path);
94                if (chain->module->func[primitive] == NULL) {
95                        openpam_log(PAM_LOG_ERROR, "%s: no %s()",
96                            chain->module->path, _pam_sm_func_name[primitive]);
97                        continue;
98                } else {
99                        pamh->current = chain;
100                        r = (chain->module->func[primitive])(pamh, flags,
101                            chain->optc, (const char **)chain->optv);
102                        pamh->current = NULL;
103                        openpam_log(PAM_LOG_DEBUG, "%s: %s(): %s",
104                            chain->module->path, _pam_sm_func_name[primitive],
105                            pam_strerror(pamh, r));
106                }
107
108                if (r == PAM_IGNORE)
109                        continue;
110                if (r == PAM_SUCCESS) {
111                        /*
112                         * For pam_setcred(), treat "sufficient" as
113                         * "optional".
114                         *
115                         * Note that Solaris libpam does not terminate
116                         * the chain here if a required module has
117                         * previously failed.  I'm not sure why.
118                         */
119                        if (chain->flag == PAM_SUFFICIENT &&
120                            primitive != PAM_SM_SETCRED)
121                                break;
122                }
123
124                _openpam_check_error_code(primitive, r);
125
126                /*
127                 * Record the return code from the first module to
128                 * fail.  If a required module fails, record the
129                 * return code from the first required module to fail.
130                 */
131                if (err == 0)
132                        err = r;
133                if (chain->flag == PAM_REQUIRED && !fail) {
134                        fail = 1;
135                        err = r;
136                }
137
138                /*
139                 * If a requisite module fails, terminate the chain
140                 * immediately.
141                 */
142                if (chain->flag == PAM_REQUISITE) {
143                        fail = 1;
144                        break;
145                }
146        }
147
148        return (fail ? err : PAM_SUCCESS);
149}
150
151#if !defined(OPENPAM_RELAX_CHECKS)
152static void
153_openpam_check_error_code(int primitive, int r)
154{
155        /* common error codes */
156        if (r == PAM_SUCCESS ||
157            r == PAM_SERVICE_ERR ||
158            r == PAM_BUF_ERR ||
159            r == PAM_CONV_ERR ||
160            r == PAM_PERM_DENIED ||
161            r == PAM_ABORT)
162                return;
163
164        /* specific error codes */
165        switch (primitive) {
166        case PAM_SM_AUTHENTICATE:
167                if (r == PAM_AUTH_ERR ||
168                    r == PAM_CRED_INSUFFICIENT ||
169                    r == PAM_AUTHINFO_UNAVAIL ||
170                    r == PAM_USER_UNKNOWN ||
171                    r == PAM_MAXTRIES)
172                        return;
173                break;
174        case PAM_SM_SETCRED:
175                if (r == PAM_CRED_UNAVAIL ||
176                    r == PAM_CRED_EXPIRED ||
177                    r == PAM_USER_UNKNOWN ||
178                    r == PAM_CRED_ERR)
179                        return;
180                break;
181        case PAM_SM_ACCT_MGMT:
182                if (r == PAM_USER_UNKNOWN ||
183                    r == PAM_AUTH_ERR ||
184                    r == PAM_NEW_AUTHTOK_REQD ||
185                    r == PAM_ACCT_EXPIRED)
186                        return;
187                break;
188        case PAM_SM_OPEN_SESSION:
189        case PAM_SM_CLOSE_SESSION:
190                if (r == PAM_SESSION_ERR)
191                        return;
192                break;
193        case PAM_SM_CHAUTHTOK:
194                if (r == PAM_PERM_DENIED ||
195                    r == PAM_AUTHTOK_ERR ||
196                    r == PAM_AUTHTOK_RECOVERY_ERR ||
197                    r == PAM_AUTHTOK_LOCK_BUSY ||
198                    r == PAM_AUTHTOK_DISABLE_AGING)
199                        return;
200                break;
201        }
202
203        openpam_log(PAM_LOG_ERROR, "%s(): unexpected return value %d",
204            _pam_sm_func_name[primitive], r);
205}
206#endif /* !defined(OPENPAM_RELAX_CHECKS) */
Note: See TracBrowser for help on using the repository browser.