xref: /freebsd/lib/libc/iconv/citrus_module.c (revision bfcc09ddd422c95a1a2e4e794b63ee54c4902398)
1 /* $FreeBSD$ */
2 /* $NetBSD: citrus_module.c,v 1.9 2009/01/11 02:46:24 christos Exp $ */
3 
4 /*-
5  * SPDX-License-Identifier: BSD-2-Clause
6  *
7  * Copyright (c)1999, 2000, 2001, 2002 Citrus Project,
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 /*-
33  * Copyright (c) 1998 The NetBSD Foundation, Inc.
34  * All rights reserved.
35  *
36  * This code is derived from software contributed to The NetBSD Foundation
37  * by Paul Kranenburg.
38  *
39  * Redistribution and use in source and binary forms, with or without
40  * modification, are permitted provided that the following conditions
41  * are met:
42  * 1. Redistributions of source code must retain the above copyright
43  *    notice, this list of conditions and the following disclaimer.
44  * 2. Redistributions in binary form must reproduce the above copyright
45  *    notice, this list of conditions and the following disclaimer in the
46  *    documentation and/or other materials provided with the distribution.
47  *
48  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
49  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
50  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
51  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
52  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
53  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
54  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
55  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
56  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
57  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
58  * POSSIBILITY OF SUCH DAMAGE.
59  */
60 
61 /*-
62  * Copyright (c) 1993
63  *	The Regents of the University of California.  All rights reserved.
64  *
65  * This code is derived from software contributed to Berkeley by
66  * Paul Borman at Krystal Technologies.
67  *
68  * Redistribution and use in source and binary forms, with or without
69  * modification, are permitted provided that the following conditions
70  * are met:
71  * 1. Redistributions of source code must retain the above copyright
72  *    notice, this list of conditions and the following disclaimer.
73  * 2. Redistributions in binary form must reproduce the above copyright
74  *    notice, this list of conditions and the following disclaimer in the
75  *    documentation and/or other materials provided with the distribution.
76  * 3. Neither the name of the University nor the names of its contributors
77  *    may be used to endorse or promote products derived from this software
78  *    without specific prior written permission.
79  *
80  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
81  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
82  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
83  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
84  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
85  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
86  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
87  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
88  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
89  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
90  * SUCH DAMAGE.
91  */
92 
93 #include <sys/cdefs.h>
94 #include <sys/types.h>
95 
96 #include <assert.h>
97 #include <dirent.h>
98 #include <dlfcn.h>
99 #include <errno.h>
100 #include <limits.h>
101 #include <paths.h>
102 #include <stdbool.h>
103 #include <stddef.h>
104 #include <stdio.h>
105 #include <stdlib.h>
106 #include <string.h>
107 #include <unistd.h>
108 
109 #define	I18NMODULE_MAJOR	4
110 
111 #include "citrus_namespace.h"
112 #include "citrus_bcs.h"
113 #include "citrus_module.h"
114 #include "libc_private.h"
115 
116 static int		 _getdewey(int[], char *);
117 static int		 _cmpndewey(int[], int, int[], int);
118 static const char	*_findshlib(char *, int *, int *);
119 
120 static const char *_pathI18nModule = NULL;
121 
122 /* from libexec/ld.aout_so/shlib.c */
123 #undef major
124 #undef minor
125 #define MAXDEWEY	3	/*ELF*/
126 
127 static int
128 _getdewey(int dewey[], char *cp)
129 {
130 	int i, n;
131 
132 	for (n = 0, i = 0; i < MAXDEWEY; i++) {
133 		if (*cp == '\0')
134 			break;
135 
136 		if (*cp == '.') cp++;
137 		if (*cp < '0' || '9' < *cp)
138 			return (0);
139 
140 		dewey[n++] = (int)_bcs_strtol(cp, &cp, 10);
141 	}
142 
143 	return (n);
144 }
145 
146 /*
147  * Compare two dewey arrays.
148  * Return -1 if `d1' represents a smaller value than `d2'.
149  * Return  1 if `d1' represents a greater value than `d2'.
150  * Return  0 if equal.
151  */
152 static int
153 _cmpndewey(int d1[], int n1, int d2[], int n2)
154 {
155 	int i;
156 
157 	for (i = 0; i < n1 && i < n2; i++) {
158 		if (d1[i] < d2[i])
159 			return (-1);
160 		if (d1[i] > d2[i])
161 			return (1);
162 	}
163 
164 	if (n1 == n2)
165 		return (0);
166 
167 	if (i == n1)
168 		return (-1);
169 
170 	if (i == n2)
171 		return (1);
172 
173 	/* cannot happen */
174 	return (0);
175 }
176 
177 static const char *
178 _findshlib(char *name, int *majorp, int *minorp)
179 {
180 	char *lname;
181 	const char *search_dirs[1];
182 	static char path[PATH_MAX];
183 	int dewey[MAXDEWEY], tmp[MAXDEWEY];
184 	int i, len, major, minor, ndewey, n_search_dirs;
185 
186 	n_search_dirs = 1;
187 	major = *majorp;
188 	minor = *minorp;
189 	path[0] = '\0';
190 	search_dirs[0] = _pathI18nModule;
191 	len = strlen(name);
192 	lname = name;
193 
194 	ndewey = 0;
195 
196 	for (i = 0; i < n_search_dirs; i++) {
197 		struct dirent *dp;
198 		DIR *dd = opendir(search_dirs[i]);
199 		int found_dot_a = 0, found_dot_so = 0;
200 
201 		if (dd == NULL)
202 			break;
203 
204 		while ((dp = readdir(dd)) != NULL) {
205 			int n;
206 
207 			if (dp->d_namlen < len + 4)
208 				continue;
209 			if (strncmp(dp->d_name, lname, (size_t)len) != 0)
210 				continue;
211 			if (strncmp(dp->d_name+len, ".so.", 4) != 0)
212 				continue;
213 
214 			if ((n = _getdewey(tmp, dp->d_name+len+4)) == 0)
215 				continue;
216 
217 			if (major != -1 && found_dot_a)
218 				found_dot_a = 0;
219 
220 			/* XXX should verify the library is a.out/ELF? */
221 
222 			if (major == -1 && minor == -1)
223 				goto compare_version;
224 			else if (major != -1 && minor == -1) {
225 				if (tmp[0] == major)
226 					goto compare_version;
227 			} else if (major != -1 && minor != -1) {
228 				if (tmp[0] == major) {
229 					if (n == 1 || tmp[1] >= minor)
230 						goto compare_version;
231 				}
232 			}
233 
234 			/* else, this file does not qualify */
235 			continue;
236 
237 		compare_version:
238 			if (_cmpndewey(tmp, n, dewey, ndewey) <= 0)
239 				continue;
240 
241 			/* We have a better version */
242 			found_dot_so = 1;
243 			snprintf(path, sizeof(path), "%s/%s", search_dirs[i],
244 			    dp->d_name);
245 			found_dot_a = 0;
246 			bcopy(tmp, dewey, sizeof(dewey));
247 			ndewey = n;
248 			*majorp = dewey[0];
249 			*minorp = dewey[1];
250 		}
251 		closedir(dd);
252 
253 		if (found_dot_a || found_dot_so)
254 			/*
255 			 * There's a lib in this dir; take it.
256 			 */
257 			return (path[0] ? path : NULL);
258 	}
259 
260 	return (path[0] ? path : NULL);
261 }
262 
263 void *
264 _citrus_find_getops(_citrus_module_t handle, const char *modname,
265     const char *ifname)
266 {
267 	char name[PATH_MAX];
268 	void *p;
269 
270 	snprintf(name, sizeof(name), "_citrus_%s_%s_getops",
271 	    modname, ifname);
272 	p = dlsym((void *)handle, name);
273 	return (p);
274 }
275 
276 int
277 _citrus_load_module(_citrus_module_t *rhandle, const char *encname)
278 {
279 	const char *p;
280 	char path[PATH_MAX];
281 	void *handle;
282 	int maj, min;
283 
284 	if (_pathI18nModule == NULL) {
285 		p = getenv("PATH_I18NMODULE");
286 		if (p != NULL && !issetugid()) {
287 			_pathI18nModule = strdup(p);
288 			if (_pathI18nModule == NULL)
289 				return (ENOMEM);
290 		} else
291 			_pathI18nModule = _PATH_I18NMODULE;
292 	}
293 
294 	(void)snprintf(path, sizeof(path), "lib%s", encname);
295 	maj = I18NMODULE_MAJOR;
296 	min = -1;
297 	p = _findshlib(path, &maj, &min);
298 	if (!p)
299 		return (EINVAL);
300 	handle = libc_dlopen(p, RTLD_LAZY);
301 	if (!handle) {
302 		printf("%s", dlerror());
303 		return (EINVAL);
304 	}
305 
306 	*rhandle = (_citrus_module_t)handle;
307 
308 	return (0);
309 }
310 
311 void
312 _citrus_unload_module(_citrus_module_t handle)
313 {
314 
315 	if (handle)
316 		dlclose((void *)handle);
317 }
318