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

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

Store options, and pass them to modules.

Replace the "dispatching" flag with a pam_chain_t pointer. It is set
to point at the currently executing module right before calling the
module, and cleared right after the module returns. Note that this
isn't intended to prevent reentrancy in multi-threaded applications,
but simply to prevent modules from using the application interface.

When recursion is detected, return PAM_ABORT rather than
PAM_SYSTEM_ERR, since this is a programmatical error rather than
a runtime one.

Sponsored by: DARPA, NAI Labs

File size: 4.2 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
45const char *_pam_sm_func_name[PAM_NUM_PRIMITIVES] = {
46        "pam_sm_acct_mgmt",
47        "pam_sm_authenticate",
48        "pam_sm_chauthtok",
49        "pam_sm_close_session",
50        "pam_sm_open_session",
51        "pam_sm_setcred"
52};
53
54static void
55openpam_destroy_module(pam_chain_t *module)
56{
57        if (module->dlh != NULL)
58                dlclose(module->dlh);
59        while (module->optc--)
60                free(module->optv[module->optc]);
61        free(module->optv);
62        free(module->modpath);
63        free(module);
64}
65
66int
67openpam_add_module(pam_handle_t *pamh,
68        int chain,
69        int flag,
70        const char *modpath,
71        int optc,
72        const char *optv[])
73{
74        pam_chain_t *module, *iterator;
75        int i;
76
77        /* fill in configuration data */
78        if ((module = calloc(1, sizeof(*module))) == NULL)
79                goto buf_err;
80        if ((module->modpath = strdup(modpath)) == NULL)
81                goto buf_err;
82        if ((module->optv = malloc(sizeof(char *) * (optc + 1))) == NULL)
83                goto buf_err;
84        while (optc--)
85                if ((module->optv[module->optc++] = strdup(*optv++)) == NULL)
86                        goto buf_err;
87        module->optv[module->optc] = NULL;
88        module->flag = flag;
89        module->next = NULL;
90
91        /* load module and resolve symbols */
92        /*
93         * Each module is dlopen()'d once for evey time it occurs in
94         * any chain.  While the linker is smart enough to not load
95         * the same module more than once, it does waste space in the
96         * form of linker handles and pam_func structs.
97         *
98         * TODO: implement a central module cache and replace the
99         * array of pam_func structs in struct pam_chain with pointers
100         * to the appropriate entry in the module cache.
101         */
102        if ((module->dlh = dlopen(modpath, RTLD_NOW)) == NULL) {
103                openpam_log(PAM_LOG_ERROR, "dlopen(): %s", dlerror());
104                openpam_destroy_module(module);
105                return (PAM_OPEN_ERR);
106        }
107        for (i = 0; i < PAM_NUM_PRIMITIVES; ++i)
108                module->primitive[i] =
109                    dlsym(module->dlh, _pam_sm_func_name[i]);
110
111        if ((iterator = pamh->chains[chain]) != NULL) {
112                while (iterator->next != NULL)
113                        iterator = iterator->next;
114                iterator->next = module;
115        } else {
116                pamh->chains[chain] = module;
117        }
118        return (PAM_SUCCESS);
119
120 buf_err:
121        openpam_log(PAM_LOG_ERROR, "%m");
122        openpam_destroy_module(module);
123        return (PAM_BUF_ERR);
124}
125
126/*
127 * Clear the chains and release the modules
128 */
129
130void
131openpam_clear_chains(pam_handle_t *pamh)
132{
133        pam_chain_t *module;
134        int i;
135
136        for (i = 0; i < PAM_NUM_CHAINS; ++i) {
137                while (pamh->chains[i] != NULL) {
138                        module = pamh->chains[i];
139                        pamh->chains[i] = module->next;
140                        openpam_destroy_module(module);
141                }
142        }
143}
Note: See TracBrowser for help on using the repository browser.