source: openpam/trunk/lib/openpam_load.c @ 94

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

Fix a number of bugs in the module cache:

  • Don't log dlopen() failures, since they're rarely interesting; instead, log a failure if no module was found at all.
  • When loading a versioned module, store its logical name in the module structure rather than its physical name, since it will be looked up by its logical name if it's needed again.
  • Initialize module->next->prev when adding a module to the cache.
  • Set modules to NULL when releasing the last module in the cache.

Sponsored by: DARPA, NAI Labs

File size: 5.4 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 * 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 * $P4: //depot/projects/openpam/lib/openpam_load.c#12 $
35 */
36
37#include <dlfcn.h>
38#include <stdlib.h>
39#include <string.h>
40
41#include <security/pam_appl.h>
42
43#include "openpam_impl.h"
44
45const char *_pam_sm_func_name[PAM_NUM_PRIMITIVES] = {
46        "pam_sm_authenticate",
47        "pam_sm_setcred",
48        "pam_sm_acct_mgmt",
49        "pam_sm_open_session",
50        "pam_sm_close_session",
51        "pam_sm_chauthtok"
52};
53
54static pam_module_t *modules;
55
56/*
57 * Locate a matching dynamic or static module.  Keep a list of previously
58 * found modules to speed up the process.
59 */
60
61static pam_module_t *
62openpam_load_module(const char *path)
63{
64        pam_module_t *module;
65
66        /* check cache first */
67        for (module = modules; module != NULL; module = module->next)
68                if (strcmp(module->path, path) == 0)
69                        goto found;
70
71        /* nope; try to load */
72        module = openpam_dynamic(path);
73        openpam_log(PAM_LOG_DEBUG, "%s dynamic %s",
74            (module == NULL) ? "no" : "using", path);
75
76#ifdef OPENPAM_STATIC_MODULES
77        /* look for a static module */
78        if (module == NULL && strchr(path, '/') == NULL) {
79                module = openpam_static(path);
80                openpam_log(PAM_LOG_DEBUG, "%s static %s",
81                    (module == NULL) ? "no" : "using", path);
82        }
83#endif
84        if (module == NULL) {
85                openpam_log(PAM_LOG_ERROR, "no %s found", path);
86                return (NULL);
87        }
88        openpam_log(PAM_LOG_DEBUG, "adding %s to cache", module->path);
89        module->next = modules;
90        if (module->next != NULL)
91                module->next->prev = module;
92        module->prev = NULL;
93        modules = module;
94 found:
95        ++module->refcount;
96        return (module);
97}
98
99
100/*
101 * Release a module.
102 * XXX highly thread-unsafe
103 */
104
105static void
106openpam_release_module(pam_module_t *module)
107{
108        if (module == NULL)
109                return;
110        --module->refcount;
111        if (module->refcount > 0)
112                /* still in use */
113                return;
114        if (module->refcount < 0) {
115                openpam_log(PAM_LOG_ERROR, "module %s has negative refcount",
116                    module->path);
117                module->refcount = 0;
118        }
119        if (module->dlh == NULL)
120                /* static module */
121                return;
122        dlclose(module->dlh);
123        if (module->prev != NULL)
124                module->prev->next = module->next;
125        if (module->next != NULL)
126                module->next->prev = module->prev;
127        if (module == modules)
128                modules = module->next;
129        openpam_log(PAM_LOG_DEBUG, "releasing %s", module->path);
130        free(module->path);
131        free(module);
132}
133
134
135/*
136 * Destroy a chain, freeing all its links and releasing the modules
137 * they point to.
138 */
139
140static void
141openpam_destroy_chain(pam_chain_t *chain)
142{
143        if (chain == NULL)
144                return;
145        openpam_destroy_chain(chain->next);
146        chain->next = NULL;
147        while (chain->optc--)
148                free(chain->optv[chain->optc]);
149        free(chain->optv);
150        openpam_release_module(chain->module);
151        free(chain);
152}
153
154/*
155 * Add a module to a chain.
156 */
157
158int
159openpam_add_module(pam_handle_t *pamh,
160        int chain,
161        int flag,
162        const char *modpath,
163        int optc,
164        const char *optv[])
165{
166        pam_chain_t *new, *iterator;
167
168        if ((new = calloc(1, sizeof *new)) == NULL)
169                goto buf_err;
170        if ((new->optv = malloc(sizeof(char *) * (optc + 1))) == NULL)
171                goto buf_err;
172        while (optc--)
173                if ((new->optv[new->optc++] = strdup(*optv++)) == NULL)
174                        goto buf_err;
175        new->optv[new->optc] = NULL;
176        new->flag = flag;
177        if ((new->module = openpam_load_module(modpath)) == NULL) {
178                openpam_destroy_chain(new);
179                return (PAM_OPEN_ERR);
180        }
181        if ((iterator = pamh->chains[chain]) != NULL) {
182                while (iterator->next != NULL)
183                        iterator = iterator->next;
184                iterator->next = new;
185        } else {
186                pamh->chains[chain] = new;
187        }
188        return (PAM_SUCCESS);
189
190 buf_err:
191        openpam_log(PAM_LOG_ERROR, "%m");
192        openpam_destroy_chain(new);
193        return (PAM_BUF_ERR);
194}
195
196
197/*
198 * Clear the chains and release the modules
199 */
200
201void
202openpam_clear_chains(pam_handle_t *pamh)
203{
204        int i;
205
206        for (i = 0; i < PAM_NUM_CHAINS; ++i)
207                openpam_destroy_chain(pamh->chains[i]);
208}
209
210/*
211 * NOPARSE
212 */
Note: See TracBrowser for help on using the repository browser.