1/*	$NetBSD: openpam_dynamic.c,v 1.3.2.1 2012/08/16 02:41:05 riz 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_dynamic.c 502 2011-12-18 13:59:22Z des
38 */
39
40#ifdef HAVE_CONFIG_H
41# include "config.h"
42#endif
43
44#include <dlfcn.h>
45#include <errno.h>
46#include <stdio.h>
47#include <stdlib.h>
48#include <string.h>
49#include <unistd.h>
50
51#include <security/pam_appl.h>
52
53#include "openpam_impl.h"
54
55#ifndef RTLD_NOW
56#define RTLD_NOW RTLD_LAZY
57#endif
58
59/*
60 * OpenPAM internal
61 *
62 * Perform sanity checks and attempt to load a module
63 */
64
65static void *
66try_dlopen(const char *modfn)
67{
68
69	if (openpam_check_path_owner_perms(modfn) != 0)
70		return (NULL);
71	return (dlopen(modfn, RTLD_NOW));
72}
73
74/*
75 * OpenPAM internal
76 *
77 * Locate a dynamically linked module
78 */
79
80pam_module_t *
81openpam_dynamic(const char *path)
82{
83	const pam_module_t *dlmodule;
84	pam_module_t *module;
85	const char *prefix, *epath = path;
86	char *vpath;
87	void *dlh;
88	int i, serrno;
89
90	dlh = NULL;
91
92	/* Prepend the standard prefix if not an absolute pathname. */
93	if (path[0] != '/')
94		prefix = OPENPAM_MODULES_DIR;
95	else
96		prefix = "";
97
98	/* try versioned module first, then unversioned module */
99	if (asprintf(&vpath, "%s/%s.%d", prefix, path, LIB_MAJ) < 0)
100		goto err;
101	epath = vpath;
102	if ((dlh = try_dlopen(vpath)) == NULL && errno == ENOENT) {
103		*strrchr(vpath, '.') = '\0';
104		dlh = try_dlopen(vpath);
105	}
106	serrno = errno;
107	FREE(vpath);
108	errno = serrno;
109	if (dlh == NULL)
110		goto err;
111	if ((module = calloc((size_t)1, sizeof *module)) == NULL)
112		goto buf_err;
113	if ((module->path = strdup(path)) == NULL)
114		goto buf_err;
115	module->dlh = dlh;
116	dlmodule = dlsym(dlh, "_pam_module");
117	for (i = 0; i < PAM_NUM_PRIMITIVES; ++i) {
118		module->func[i] = dlmodule ? dlmodule->func[i] :
119		    (pam_func_t)dlsym(dlh, pam_sm_func_name[i]);
120		if (module->func[i] == NULL)
121			openpam_log(PAM_LOG_DEBUG, "%s: %s(): %s",
122			    path, pam_sm_func_name[i], dlerror());
123	}
124	return (module);
125buf_err:
126	serrno = errno;
127	if (dlh != NULL)
128		dlclose(dlh);
129	FREE(module);
130	errno = serrno;
131err:
132	openpam_log(errno == ENOENT ? PAM_LOG_DEBUG : PAM_LOG_ERROR, "%s: %s",
133	    epath, strerror(errno));
134	return (NULL);
135}
136
137/*
138 * NOPARSE
139 */
140