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

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

Move most of the dynamic loader code into a separate file, and add
support for module versioning. OpenPAM will prefer a PAM module with
the same version number as the library itself to one with no version
number at all.

Sponsored by: DARPA, NAI Labs

File size: 5.1 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 * $P4: //depot/projects/openpam/lib/openpam_load.c#10 $
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                return (NULL);
86        module->next = modules;
87        module->prev = NULL;
88        modules = module;
89 found:
90        ++module->refcount;
91        return (module);
92}
93
94
95/*
96 * Release a module.
97 * XXX highly thread-unsafe
98 */
99
100static void
101openpam_release_module(pam_module_t *module)
102{
103        if (module == NULL)
104                return;
105        --module->refcount;
106        if (module->refcount > 0)
107                /* still in use */
108                return;
109        if (module->refcount < 0) {
110                openpam_log(PAM_LOG_ERROR, "module %s has negative refcount",
111                    module->path);
112                module->refcount = 0;
113        }
114        if (module->dlh == NULL)
115                /* static module */
116                return;
117        dlclose(module->dlh);
118        if (module->prev != NULL)
119                module->prev->next = module->next;
120        if (module->next != NULL)
121                module->next->prev = module->prev;
122        free(module->path);
123        free(module);
124}
125
126
127/*
128 * Destroy a chain, freeing all its links and releasing the modules
129 * they point to.
130 */
131
132static void
133openpam_destroy_chain(pam_chain_t *chain)
134{
135        if (chain == NULL)
136                return;
137        openpam_destroy_chain(chain->next);
138        chain->next = NULL;
139        while (chain->optc--)
140                free(chain->optv[chain->optc]);
141        free(chain->optv);
142        openpam_release_module(chain->module);
143        free(chain);
144}
145
146/*
147 * Add a module to a chain.
148 */
149
150int
151openpam_add_module(pam_handle_t *pamh,
152        int chain,
153        int flag,
154        const char *modpath,
155        int optc,
156        const char *optv[])
157{
158        pam_chain_t *new, *iterator;
159
160        if ((new = calloc(1, sizeof *new)) == NULL)
161                goto buf_err;
162        if ((new->optv = malloc(sizeof(char *) * (optc + 1))) == NULL)
163                goto buf_err;
164        while (optc--)
165                if ((new->optv[new->optc++] = strdup(*optv++)) == NULL)
166                        goto buf_err;
167        new->optv[new->optc] = NULL;
168        new->flag = flag;
169        if ((new->module = openpam_load_module(modpath)) == NULL) {
170                openpam_destroy_chain(new);
171                return (PAM_OPEN_ERR);
172        }
173        if ((iterator = pamh->chains[chain]) != NULL) {
174                while (iterator->next != NULL)
175                        iterator = iterator->next;
176                iterator->next = new;
177        } else {
178                pamh->chains[chain] = new;
179        }
180        return (PAM_SUCCESS);
181
182 buf_err:
183        openpam_log(PAM_LOG_ERROR, "%m");
184        openpam_destroy_chain(new);
185        return (PAM_BUF_ERR);
186}
187
188
189/*
190 * Clear the chains and release the modules
191 */
192
193void
194openpam_clear_chains(pam_handle_t *pamh)
195{
196        int i;
197
198        for (i = 0; i < PAM_NUM_CHAINS; ++i)
199                openpam_destroy_chain(pamh->chains[i]);
200}
201
202/*
203 * NOPARSE
204 */
Note: See TracBrowser for help on using the repository browser.