Merge from vendor branch OPENSSH:
[dragonfly.git] / contrib / openpam / lib / openpam_load.c
1 /*-
2  * Copyright (c) 2002-2003 Networks Associates Technology, Inc.
3  * All rights reserved.
4  *
5  * This software was developed for the FreeBSD Project by ThinkSec AS and
6  * Network Associates Laboratories, the Security Research Division of
7  * Network Associates, Inc.  under DARPA/SPAWAR contract N66001-01-C-8035
8  * ("CBOSS"), as part of the 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#21 $
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 const char *_pam_func_name[PAM_NUM_PRIMITIVES] = {
46         "pam_authenticate",
47         "pam_setcred",
48         "pam_acct_mgmt",
49         "pam_open_session",
50         "pam_close_session",
51         "pam_chauthtok"
52 };
53
54 const char *_pam_sm_func_name[PAM_NUM_PRIMITIVES] = {
55         "pam_sm_authenticate",
56         "pam_sm_setcred",
57         "pam_sm_acct_mgmt",
58         "pam_sm_open_session",
59         "pam_sm_close_session",
60         "pam_sm_chauthtok"
61 };
62
63 static pam_module_t *modules;
64
65 /*
66  * Locate a matching dynamic or static module.  Keep a list of previously
67  * found modules to speed up the process.
68  */
69
70 pam_module_t *
71 openpam_load_module(const char *path)
72 {
73         pam_module_t *module;
74
75         /* check cache first */
76         for (module = modules; module != NULL; module = module->next)
77                 if (strcmp(module->path, path) == 0)
78                         goto found;
79
80         /* nope; try to load */
81         module = openpam_dynamic(path);
82         openpam_log(PAM_LOG_DEBUG, "%s dynamic %s",
83             (module == NULL) ? "no" : "using", path);
84
85 #ifdef OPENPAM_STATIC_MODULES
86         /* look for a static module */
87         if (module == NULL && strchr(path, '/') == NULL) {
88                 module = openpam_static(path);
89                 openpam_log(PAM_LOG_DEBUG, "%s static %s",
90                     (module == NULL) ? "no" : "using", path);
91         }
92 #endif
93         if (module == NULL) {
94                 openpam_log(PAM_LOG_ERROR, "no %s found", path);
95                 return (NULL);
96         }
97         openpam_log(PAM_LOG_DEBUG, "adding %s to cache", module->path);
98         module->next = modules;
99         if (module->next != NULL)
100                 module->next->prev = module;
101         module->prev = NULL;
102         modules = module;
103  found:
104         ++module->refcount;
105         return (module);
106 }
107
108
109 /*
110  * Release a module.
111  * XXX highly thread-unsafe
112  */
113
114 static void
115 openpam_release_module(pam_module_t *module)
116 {
117         if (module == NULL)
118                 return;
119         --module->refcount;
120         if (module->refcount > 0)
121                 /* still in use */
122                 return;
123         if (module->refcount < 0) {
124                 openpam_log(PAM_LOG_ERROR, "module %s has negative refcount",
125                     module->path);
126                 module->refcount = 0;
127         }
128         if (module->dlh == NULL)
129                 /* static module */
130                 return;
131         dlclose(module->dlh);
132         if (module->prev != NULL)
133                 module->prev->next = module->next;
134         if (module->next != NULL)
135                 module->next->prev = module->prev;
136         if (module == modules)
137                 modules = module->next;
138         openpam_log(PAM_LOG_DEBUG, "releasing %s", module->path);
139         FREE(module->path);
140         FREE(module);
141 }
142
143
144 /*
145  * Destroy a chain, freeing all its links and releasing the modules
146  * they point to.
147  */
148
149 static void
150 openpam_destroy_chain(pam_chain_t *chain)
151 {
152         if (chain == NULL)
153                 return;
154         openpam_destroy_chain(chain->next);
155         chain->next = NULL;
156         while (chain->optc) {
157                 --chain->optc;
158                 FREE(chain->optv[chain->optc]);
159         }
160         FREE(chain->optv);
161         openpam_release_module(chain->module);
162         chain->module = NULL;
163         FREE(chain);
164 }
165
166
167 /*
168  * Clear the chains and release the modules
169  */
170
171 void
172 openpam_clear_chains(pam_chain_t *policy[])
173 {
174         int i;
175
176         for (i = 0; i < PAM_NUM_FACILITIES; ++i) {
177                 openpam_destroy_chain(policy[i]);
178                 policy[i] = NULL;
179         }
180 }
181
182 /*
183  * NOPARSE
184  */