1 /*-
2 * Copyright (c) 2002-2003 Networks Associates Technology, Inc.
3 * Copyright (c) 2004-2011 Dag-Erling Smørgrav
4 * All rights reserved.
5 *
6 * This software was developed for the FreeBSD Project by ThinkSec AS and
7 * Network Associates Laboratories, the Security Research Division of
8 * Network Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035
9 * ("CBOSS"), as part of the DARPA CHATS research program.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. The name of the author may not be used to endorse or promote
20 * products derived from this software without specific prior written
21 * permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 #ifdef HAVE_CONFIG_H
37 # include "config.h"
38 #endif
39
40 #include <sys/param.h>
41
42 #include <dlfcn.h>
43 #include <errno.h>
44 #include <fcntl.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <unistd.h>
49
50 #include <security/pam_appl.h>
51
52 #include "openpam_impl.h"
53 #include "openpam_asprintf.h"
54 #include "openpam_ctype.h"
55 #include "openpam_dlfunc.h"
56
57 #ifndef RTLD_NOW
58 #define RTLD_NOW RTLD_LAZY
59 #endif
60
61 /*
62 * OpenPAM internal
63 *
64 * Perform sanity checks and attempt to load a module
65 */
66
67 #ifdef HAVE_FDLOPEN
68 static void *
try_dlopen(const char * modfn)69 try_dlopen(const char *modfn)
70 {
71 void *dlh;
72 int fd;
73
74 openpam_log(PAM_LOG_LIBDEBUG, "dlopen(%s)", modfn);
75 if ((fd = open(modfn, O_RDONLY)) < 0) {
76 if (errno != ENOENT)
77 openpam_log(PAM_LOG_ERROR, "%s: %m", modfn);
78 return (NULL);
79 }
80 if (OPENPAM_FEATURE(VERIFY_MODULE_FILE) &&
81 openpam_check_desc_owner_perms(modfn, fd) != 0) {
82 close(fd);
83 return (NULL);
84 }
85 if ((dlh = fdlopen(fd, RTLD_NOW)) == NULL) {
86 openpam_log(PAM_LOG_ERROR, "%s: %s", modfn, dlerror());
87 close(fd);
88 errno = 0;
89 return (NULL);
90 }
91 close(fd);
92 return (dlh);
93 }
94 #else
95 static void *
try_dlopen(const char * modfn)96 try_dlopen(const char *modfn)
97 {
98 int check_module_file;
99 void *dlh;
100
101 openpam_log(PAM_LOG_LIBDEBUG, "dlopen(%s)", modfn);
102 openpam_get_feature(OPENPAM_VERIFY_MODULE_FILE,
103 &check_module_file);
104 if (check_module_file &&
105 openpam_check_path_owner_perms(modfn) != 0)
106 return (NULL);
107 if ((dlh = dlopen(modfn, RTLD_NOW)) == NULL) {
108 openpam_log(PAM_LOG_ERROR, "%s: %s", modfn, dlerror());
109 errno = 0;
110 return (NULL);
111 }
112 return (dlh);
113 }
114 #endif
115
116 /*
117 * Try to load a module from the suggested location.
118 */
119 static pam_module_t *
try_module(const char * modpath)120 try_module(const char *modpath)
121 {
122 const pam_module_t *dlmodule;
123 pam_module_t *module;
124 int i, serrno;
125
126 if ((module = calloc(1, sizeof *module)) == NULL ||
127 (module->path = strdup(modpath)) == NULL ||
128 (module->dlh = try_dlopen(modpath)) == NULL)
129 goto err;
130 dlmodule = dlsym(module->dlh, "_pam_module");
131 for (i = 0; i < PAM_NUM_PRIMITIVES; ++i) {
132 if (dlmodule) {
133 module->func[i] = dlmodule->func[i];
134 } else {
135 module->func[i] = (pam_func_t)dlfunc(module->dlh,
136 pam_sm_func_name[i]);
137 /*
138 * This openpam_log() call is a major source of
139 * log spam, and the cases that matter are caught
140 * and logged in openpam_dispatch(). This would
141 * be less problematic if dlerror() returned an
142 * error code so we could log an error only when
143 * dlfunc() failed for a reason other than "no
144 * such symbol".
145 */
146 #if 0
147 if (module->func[i] == NULL)
148 openpam_log(PAM_LOG_LIBDEBUG, "%s: %s(): %s",
149 modpath, pam_sm_func_name[i], dlerror());
150 #endif
151 }
152 }
153 return (module);
154 err:
155 serrno = errno;
156 if (module != NULL) {
157 if (module->dlh != NULL)
158 dlclose(module->dlh);
159 if (module->path != NULL)
160 FREE(module->path);
161 FREE(module);
162 }
163 errno = serrno;
164 if (serrno != 0 && serrno != ENOENT)
165 openpam_log(PAM_LOG_ERROR, "%s: %m", modpath);
166 errno = serrno;
167 return (NULL);
168 }
169
170 /*
171 * OpenPAM internal
172 *
173 * Locate a dynamically linked module
174 */
175
176 pam_module_t *
openpam_dynamic(const char * modname)177 openpam_dynamic(const char *modname)
178 {
179 pam_module_t *module;
180 char modpath[PATH_MAX];
181 const char **path, *p;
182 int has_so, has_ver;
183 int dot, len;
184
185 /*
186 * Simple case: module name contains path separator(s)
187 */
188 if (strchr(modname, '/') != NULL) {
189 /*
190 * Absolute paths are not allowed if RESTRICT_MODULE_NAME
191 * is in effect (default off). Relative paths are never
192 * allowed.
193 */
194 if (OPENPAM_FEATURE(RESTRICT_MODULE_NAME) ||
195 modname[0] != '/') {
196 openpam_log(PAM_LOG_ERROR,
197 "invalid module name: %s", modname);
198 return (NULL);
199 }
200 return (try_module(modname));
201 }
202
203 /*
204 * Check for .so and version sufixes
205 */
206 p = strchr(modname, '\0');
207 has_ver = has_so = 0;
208 while (is_digit(*p))
209 --p;
210 if (*p == '.' && *++p != '\0') {
211 /* found a numeric suffix */
212 has_ver = 1;
213 /* assume that .so is either present or unneeded */
214 has_so = 1;
215 } else if (*p == '\0' && p >= modname + sizeof PAM_SOEXT &&
216 strcmp(p - sizeof PAM_SOEXT + 1, PAM_SOEXT) == 0) {
217 /* found .so suffix */
218 has_so = 1;
219 }
220
221 /*
222 * Complicated case: search for the module in the usual places.
223 */
224 for (path = openpam_module_path; *path != NULL; ++path) {
225 /*
226 * Assemble the full path, including the version suffix. Take
227 * note of where the suffix begins so we can cut it off later.
228 */
229 if (has_ver)
230 len = snprintf(modpath, sizeof modpath, "%s/%s%n",
231 *path, modname, &dot);
232 else if (has_so)
233 len = snprintf(modpath, sizeof modpath, "%s/%s%n.%d",
234 *path, modname, &dot, LIB_MAJ);
235 else
236 len = snprintf(modpath, sizeof modpath, "%s/%s%s%n.%d",
237 *path, modname, PAM_SOEXT, &dot, LIB_MAJ);
238 /* check for overflow */
239 if (len < 0 || (unsigned int)len >= sizeof modpath) {
240 errno = ENOENT;
241 continue;
242 }
243 /* try the versioned path */
244 if ((module = try_module(modpath)) != NULL)
245 return (module);
246 if (errno == ENOENT && modpath[dot] != '\0') {
247 /* no luck, try the unversioned path */
248 modpath[dot] = '\0';
249 if ((module = try_module(modpath)) != NULL)
250 return (module);
251 }
252 }
253
254 /* :( */
255 return (NULL);
256 }
257
258 /*
259 * NOPARSE
260 */
261