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

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

Add support for static linking. Also rename a handful of constants
to reduce the chance of every running into a naming conflict.

Sponsored by: DARPA, NAI Labs

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