1/*	$NetBSD: openpam_load.c,v 1.2 2011/12/25 22:27:55 christos Exp $	*/
2
3/*-
4 * Copyright (c) 2002-2003 Networks Associates Technology, Inc.
5 * Copyright (c) 2004-2011 Dag-Erling Smørgrav
6 * All rights reserved.
7 *
8 * This software was developed for the FreeBSD Project by ThinkSec AS and
9 * Network Associates Laboratories, the Security Research Division of
10 * Network Associates, Inc.  under DARPA/SPAWAR contract N66001-01-C-8035
11 * ("CBOSS"), as part of the DARPA CHATS research program.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 * 1. Redistributions of source code must retain the above copyright
17 *    notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 *    notice, this list of conditions and the following disclaimer in the
20 *    documentation and/or other materials provided with the distribution.
21 * 3. The name of the author may not be used to endorse or promote
22 *    products derived from this software without specific prior written
23 *    permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * SUCH DAMAGE.
36 *
37 * Id: openpam_load.c 491 2011-11-12 00:12:32Z des
38 */
39
40#ifdef HAVE_CONFIG_H
41# include "config.h"
42#endif
43
44#include <dlfcn.h>
45#include <stdlib.h>
46#include <string.h>
47
48#include <security/pam_appl.h>
49
50#include "openpam_impl.h"
51
52/*
53 * Locate a matching dynamic or static module.
54 */
55
56pam_module_t *
57openpam_load_module(const char *path)
58{
59	pam_module_t *module;
60
61	module = openpam_dynamic(path);
62	openpam_log(PAM_LOG_DEBUG, "%s dynamic %s",
63	    (module == NULL) ? "no" : "using", path);
64
65#ifdef OPENPAM_STATIC_MODULES
66	/* look for a static module */
67	if (module == NULL && strchr(path, '/') == NULL) {
68		module = openpam_static(path);
69		openpam_log(PAM_LOG_DEBUG, "%s static %s",
70		    (module == NULL) ? "no" : "using", path);
71	}
72#endif
73	if (module == NULL) {
74		openpam_log(PAM_LOG_ERROR, "no %s found", path);
75		return (NULL);
76	}
77	return (module);
78}
79
80
81/*
82 * Release a module.
83 * XXX highly thread-unsafe
84 */
85
86static void
87openpam_release_module(pam_module_t *module)
88{
89	if (module == NULL)
90		return;
91	if (module->dlh == NULL)
92		/* static module */
93		return;
94	dlclose(module->dlh);
95	openpam_log(PAM_LOG_DEBUG, "releasing %s", module->path);
96	FREE(module->path);
97	FREE(module);
98}
99
100
101/*
102 * Destroy a chain, freeing all its links and releasing the modules
103 * they point to.
104 */
105
106static void
107openpam_destroy_chain(pam_chain_t *chain)
108{
109	if (chain == NULL)
110		return;
111	openpam_destroy_chain(chain->next);
112	chain->next = NULL;
113	while (chain->optc--)
114		FREE(chain->optv[chain->optc]);
115	FREE(chain->optv);
116	openpam_release_module(chain->module);
117	chain->module = NULL;
118	FREE(chain);
119}
120
121
122/*
123 * Clear the chains and release the modules
124 */
125
126void
127openpam_clear_chains(pam_chain_t *policy[])
128{
129	int i;
130
131	for (i = 0; i < PAM_NUM_FACILITIES; ++i) {
132		openpam_destroy_chain(policy[i]);
133		policy[i] = NULL;
134	}
135}
136
137/*
138 * NOPARSE
139 */
140