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