xref: /netbsd-src/external/bsd/openpam/dist/lib/libpam/openpam_dynamic.c (revision 4724848cf0da353df257f730694b7882798e5daf)
1 /*	$NetBSD: openpam_dynamic.c,v 1.3 2017/05/06 19:50:09 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  * $OpenPAM: openpam_dynamic.c 938 2017-04-30 21:34:42Z des $
38  */
39 
40 #ifdef HAVE_CONFIG_H
41 # include "config.h"
42 #endif
43 
44 #include <sys/cdefs.h>
45 __RCSID("$NetBSD: openpam_dynamic.c,v 1.3 2017/05/06 19:50:09 christos Exp $");
46 
47 #include <sys/param.h>
48 
49 #include <dlfcn.h>
50 #include <errno.h>
51 #include <fcntl.h>
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <string.h>
55 #include <unistd.h>
56 
57 #include <security/pam_appl.h>
58 
59 #include "openpam_impl.h"
60 #include "openpam_asprintf.h"
61 #include "openpam_ctype.h"
62 #include "openpam_dlfunc.h"
63 
64 #ifndef RTLD_NOW
65 #define RTLD_NOW RTLD_LAZY
66 #endif
67 
68 /*
69  * OpenPAM internal
70  *
71  * Perform sanity checks and attempt to load a module
72  */
73 
74 #ifdef HAVE_FDLOPEN
75 static void *
76 try_dlopen(const char *modfn, int *error)
77 {
78 	void *dlh;
79 	int fd;
80 
81 	openpam_log(PAM_LOG_LIBDEBUG, "dlopen(%s)", modfn);
82 	if ((fd = open(modfn, O_RDONLY)) < 0) {
83 		if (errno != ENOENT)
84 			openpam_log(PAM_LOG_ERROR, "%s: %m", modfn);
85 		return (NULL);
86 	}
87 	if (OPENPAM_FEATURE(VERIFY_MODULE_FILE) &&
88 	    openpam_check_desc_owner_perms(modfn, fd) != 0) {
89 		close(fd);
90 		return (NULL);
91 	}
92 	if ((dlh = fdlopen(fd, RTLD_NOW)) == NULL) {
93 		openpam_log(PAM_LOG_ERROR, "%s: %s", modfn, dlerror());
94 		close(fd);
95 		errno = 0;
96 		return (NULL);
97 	}
98 	close(fd);
99 	return (dlh);
100 }
101 #else
102 static void *
103 try_dlopen(const char *modfn)
104 {
105 	int check_module_file;
106 	void *dlh;
107 
108 	openpam_log(PAM_LOG_LIBDEBUG, "dlopen(%s)", modfn);
109 	openpam_get_feature(OPENPAM_VERIFY_MODULE_FILE,
110 	    &check_module_file);
111 	if (check_module_file &&
112 	    openpam_check_path_owner_perms(modfn) != 0)
113 		return (NULL);
114 	if ((dlh = dlopen(modfn, RTLD_NOW)) == NULL) {
115 		openpam_log(PAM_LOG_ERROR, "%s: %s", modfn, dlerror());
116 		errno = 0;
117 		return (NULL);
118 	}
119 	return (dlh);
120 }
121 #endif
122 
123 /*
124  * Try to load a module from the suggested location.
125  */
126 static pam_module_t *
127 try_module(const char *modpath)
128 {
129 	const pam_module_t *dlmodule;
130 	pam_module_t *module;
131 	int i, serrno;
132 
133 	if ((module = calloc(1, sizeof *module)) == NULL ||
134 	    (module->path = strdup(modpath)) == NULL ||
135 	    (module->dlh = try_dlopen(modpath)) == NULL)
136 		goto err;
137 	dlmodule = dlsym(module->dlh, "_pam_module");
138 	for (i = 0; i < PAM_NUM_PRIMITIVES; ++i) {
139 		if (dlmodule) {
140 			module->func[i] = dlmodule->func[i];
141 		} else {
142 			module->func[i] = (pam_func_t)dlfunc(module->dlh,
143 			    pam_sm_func_name[i]);
144 			/*
145 			 * This openpam_log() call is a major source of
146 			 * log spam, and the cases that matter are caught
147 			 * and logged in openpam_dispatch().  This would
148 			 * be less problematic if dlerror() returned an
149 			 * error code so we could log an error only when
150 			 * dlfunc() failed for a reason other than "no
151 			 * such symbol".
152 			 */
153 #if 0
154 			if (module->func[i] == NULL)
155 				openpam_log(PAM_LOG_LIBDEBUG, "%s: %s(): %s",
156 				    modpath, pam_sm_func_name[i], dlerror());
157 #endif
158 		}
159 	}
160 	return (module);
161 err:
162 	serrno = errno;
163 	if (module != NULL) {
164 		if (module->dlh != NULL)
165 			dlclose(module->dlh);
166 		if (module->path != NULL)
167 			FREE(module->path);
168 		FREE(module);
169 	}
170 	errno = serrno;
171 	if (serrno != 0 && serrno != ENOENT)
172 		openpam_log(PAM_LOG_ERROR, "%s: %m", modpath);
173 	errno = serrno;
174 	return (NULL);
175 }
176 
177 /*
178  * OpenPAM internal
179  *
180  * Locate a dynamically linked module
181  */
182 
183 pam_module_t *
184 openpam_dynamic(const char *modname)
185 {
186 	pam_module_t *module;
187 	char modpath[PATH_MAX];
188 	const char **path, *p;
189 	int has_so, has_ver;
190 	int dot, len;
191 
192 	/*
193 	 * Simple case: module name contains path separator(s)
194 	 */
195 	if (strchr(modname, '/') != NULL) {
196 		/*
197 		 * Absolute paths are not allowed if RESTRICT_MODULE_NAME
198 		 * is in effect (default off).  Relative paths are never
199 		 * allowed.
200 		 */
201 		if (OPENPAM_FEATURE(RESTRICT_MODULE_NAME) ||
202 		    modname[0] != '/') {
203 			openpam_log(PAM_LOG_ERROR,
204 			    "invalid module name: %s", modname);
205 			return (NULL);
206 		}
207 		return (try_module(modname));
208 	}
209 
210 	/*
211 	 * Check for .so and version sufixes
212 	 */
213 	p = strchr(modname, '\0');
214 	has_ver = has_so = 0;
215 	while (is_digit(*p))
216 		--p;
217 	if (*p == '.' && *++p != '\0') {
218 		/* found a numeric suffix */
219 		has_ver = 1;
220 		/* assume that .so is either present or unneeded */
221 		has_so = 1;
222 	} else if (*p == '\0' && p >= modname + sizeof PAM_SOEXT &&
223 	    strcmp(p - sizeof PAM_SOEXT + 1, PAM_SOEXT) == 0) {
224 		/* found .so suffix */
225 		has_so = 1;
226 	}
227 
228 	/*
229 	 * Complicated case: search for the module in the usual places.
230 	 */
231 	for (path = openpam_module_path; *path != NULL; ++path) {
232 		/*
233 		 * Assemble the full path, including the version suffix.  Take
234 		 * note of where the suffix begins so we can cut it off later.
235 		 */
236 		if (has_ver)
237 			len = snprintf(modpath, sizeof modpath, "%s/%s%n",
238 			    *path, modname, &dot);
239 		else if (has_so)
240 			len = snprintf(modpath, sizeof modpath, "%s/%s%n.%d",
241 			    *path, modname, &dot, LIB_MAJ);
242 		else
243 			len = snprintf(modpath, sizeof modpath, "%s/%s%s%n.%d",
244 			    *path, modname, PAM_SOEXT, &dot, LIB_MAJ);
245 		/* check for overflow */
246 		if (len < 0 || (unsigned int)len >= sizeof modpath) {
247 			errno = ENOENT;
248 			continue;
249 		}
250 		/* try the versioned path */
251 		if ((module = try_module(modpath)) != NULL)
252 			return (module);
253 		if (errno == ENOENT && modpath[dot] != '\0') {
254 			/* no luck, try the unversioned path */
255 			modpath[dot] = '\0';
256 			if ((module = try_module(modpath)) != NULL)
257 				return (module);
258 		}
259 	}
260 
261 	/* :( */
262 	return (NULL);
263 }
264 
265 /*
266  * NOPARSE
267  */
268