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