1 |
/* |
2 |
* Copyright (C) 1998,1999 Nikos Mavroyanopoulos |
3 |
* |
4 |
* This library is free software; you can redistribute it and/or modify it under the terms of the |
5 |
* GNU Library General Public License as published by the Free Software |
6 |
* Foundation; either version 2 of the License, or (at your option) any |
7 |
* later version. |
8 |
* |
9 |
* This library is distributed in the hope that it will be useful, |
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
12 |
* Library General Public License for more details. |
13 |
* |
14 |
* You should have received a copy of the GNU Library General Public |
15 |
* License along with this library; if not, write to the |
16 |
* Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
17 |
* Boston, MA 02111-1307, USA. |
18 |
*/ |
19 |
|
20 |
/* $Id: mcrypt_extra.c,v 1.10 2001/05/17 18:58:20 nmav Exp $ */ |
21 |
|
22 |
#ifndef LIBDEFS_H |
23 |
#define LIBDEFS_H |
24 |
#include <libdefs.h> |
25 |
#endif |
26 |
#include <bzero.h> |
27 |
#include <xmemory.h> |
28 |
#include <mcrypt_internal.h> |
29 |
|
30 |
|
31 |
int mcrypt_algorithm_module_ok(const char *file, const char *directory); |
32 |
int mcrypt_mode_module_ok(const char *file, const char *directory); |
33 |
#ifndef USE_LTDL |
34 |
void *mcrypt_dlopen_ext(const char *filename); |
35 |
#endif |
36 |
void *mcrypt_dlopen(const char *a_directory, const char *m_directory, |
37 |
const char *filename); |
38 |
|
39 |
#define MAXPATHLEN 256 |
40 |
WIN32DLL_DEFINE char *mcrypt_readdir(DIR * dirstream) |
41 |
{ |
42 |
|
43 |
char *result; |
44 |
struct dirent *ret = NULL; |
45 |
#ifdef HAVE_READDIR_R |
46 |
struct dirent ret2[sizeof(struct dirent)+MAXPATHLEN]; |
47 |
#endif |
48 |
|
49 |
#ifdef DT_REG |
50 |
do { |
51 |
#endif |
52 |
|
53 |
#ifdef HAVE_READDIR_R |
54 |
readdir_r(dirstream, ret2, &ret); |
55 |
if (ret==NULL) return NULL; |
56 |
#else |
57 |
ret = readdir(dirstream); |
58 |
#endif |
59 |
|
60 |
result = calloc(1, strlen(ret->d_name) + 1); |
61 |
if (result == NULL) { |
62 |
return NULL; |
63 |
} |
64 |
strcpy(result, ret->d_name); |
65 |
#ifdef DT_REG |
66 |
} |
67 |
while ((ret->d_type != DT_REG) && (ret->d_type != DT_UNKNOWN) |
68 |
&& (ret != NULL)); |
69 |
#endif |
70 |
|
71 |
return result; |
72 |
|
73 |
} |
74 |
|
75 |
WIN32DLL_DEFINE char **mcrypt_list_algorithms(char *libdir, int *size) |
76 |
{ |
77 |
DIR *pdir; |
78 |
char directory[512]; |
79 |
char *dirname; |
80 |
char **filename = NULL, *ptr; |
81 |
int tmpsize; |
82 |
|
83 |
*size = 0; |
84 |
|
85 |
if (libdir == NULL) { |
86 |
strcpy(directory, LIBDIR); |
87 |
} else { |
88 |
strcpy(directory, libdir); |
89 |
} |
90 |
|
91 |
pdir = opendir(directory); |
92 |
if (pdir == NULL) { |
93 |
#ifdef DEBUG |
94 |
fprintf(stderr, "Unable to open directory %s.\n", |
95 |
directory); |
96 |
#endif |
97 |
return NULL; |
98 |
} |
99 |
|
100 |
for (;;) { |
101 |
dirname = mcrypt_readdir(pdir); |
102 |
if (dirname != NULL) { |
103 |
tmpsize = strlen(dirname); |
104 |
if (tmpsize > 3) { |
105 |
if (mcrypt_algorithm_module_ok |
106 |
(dirname, directory) > 0) { |
107 |
filename = |
108 |
realloc(filename, |
109 |
(*size + |
110 |
1) * sizeof(char *)); |
111 |
if (filename==NULL) { |
112 |
free(dirname); |
113 |
return NULL; |
114 |
} |
115 |
filename[*size] = |
116 |
calloc(1, tmpsize + 1); |
117 |
if (filename[*size]==NULL) return NULL; |
118 |
strcpy(filename[*size], dirname); |
119 |
|
120 |
ptr = |
121 |
strrchr(filename[*size], '.'); |
122 |
if (ptr != NULL) |
123 |
*ptr = '\0'; |
124 |
(*size)++; |
125 |
} |
126 |
} |
127 |
free(dirname); |
128 |
} else { |
129 |
break; |
130 |
} |
131 |
|
132 |
} |
133 |
|
134 |
|
135 |
closedir(pdir); |
136 |
|
137 |
return filename; |
138 |
|
139 |
} |
140 |
|
141 |
WIN32DLL_DEFINE char **mcrypt_list_modes(char *libdir, int *size) |
142 |
{ |
143 |
DIR *pdir; |
144 |
char directory[512]; |
145 |
char *dirname; |
146 |
char **filename = NULL, *ptr; |
147 |
int tmpsize; |
148 |
|
149 |
if (libdir == NULL) { |
150 |
strcpy(directory, LIBDIR); |
151 |
} else { |
152 |
strcpy(directory, libdir); |
153 |
} |
154 |
|
155 |
pdir = opendir(directory); |
156 |
if (pdir == NULL) { |
157 |
#ifdef DEBUG |
158 |
fprintf(stderr, "Unable to open directory %s.\n", |
159 |
directory); |
160 |
#endif |
161 |
return NULL; |
162 |
} |
163 |
|
164 |
*size = 0; |
165 |
for (;;) { |
166 |
|
167 |
dirname = mcrypt_readdir(pdir); |
168 |
if (dirname != NULL) { |
169 |
tmpsize = strlen(dirname); |
170 |
if (tmpsize > 3) { |
171 |
if (mcrypt_mode_module_ok |
172 |
(dirname, directory) > 0) { |
173 |
filename = |
174 |
realloc(filename, |
175 |
(*size + |
176 |
1) * sizeof(char *)); |
177 |
if (filename==NULL) { |
178 |
free(dirname); |
179 |
return NULL; |
180 |
} |
181 |
filename[*size] = |
182 |
calloc(1, tmpsize + 1); |
183 |
if (filename[*size]==NULL) return NULL; |
184 |
|
185 |
strcpy(filename[*size], dirname); |
186 |
ptr = |
187 |
strrchr(filename[*size], '.'); |
188 |
if (ptr != NULL) |
189 |
*ptr = '\0'; |
190 |
(*size)++; |
191 |
} |
192 |
} |
193 |
free(dirname); |
194 |
} else { |
195 |
break; |
196 |
} |
197 |
|
198 |
} |
199 |
|
200 |
closedir(pdir); |
201 |
return filename; |
202 |
} |
203 |
|
204 |
WIN32DLL_DEFINE void mcrypt_free_p(char **p, int size) |
205 |
{ |
206 |
int i; |
207 |
|
208 |
for (i = 0; i < size; i++) { |
209 |
free(p[i]); |
210 |
} |
211 |
free(p); |
212 |
} |
213 |
|
214 |
WIN32DLL_DEFINE |
215 |
int mcrypt_algorithm_module_ok(const char *file, const char *directory) |
216 |
{ |
217 |
word32 ret; |
218 |
lt_dlhandle _handle; |
219 |
int (*_version) (void); |
220 |
|
221 |
if (file == NULL && directory == NULL) { |
222 |
return MCRYPT_UNKNOWN_ERROR; |
223 |
} |
224 |
|
225 |
if (lt_dlinit() != 0) { |
226 |
return MCRYPT_UNKNOWN_ERROR; |
227 |
} |
228 |
|
229 |
|
230 |
/* LTDL_SET_PRELOADED_SYMBOLS(); */ |
231 |
|
232 |
_handle = mcrypt_dlopen(directory, NULL, file); |
233 |
|
234 |
if (!_handle) { |
235 |
lt_dlexit(); |
236 |
return MCRYPT_UNKNOWN_ERROR; |
237 |
} |
238 |
|
239 |
|
240 |
_version = lt_dlsym(_handle, "_mcrypt_algorithm_version"); |
241 |
|
242 |
if (_version == NULL) { |
243 |
lt_dlclose(_handle); |
244 |
lt_dlexit(); |
245 |
return MCRYPT_UNKNOWN_ERROR; |
246 |
} |
247 |
|
248 |
ret = _version(); |
249 |
|
250 |
lt_dlclose(_handle); |
251 |
lt_dlexit(); |
252 |
|
253 |
return ret; |
254 |
|
255 |
} |
256 |
|
257 |
WIN32DLL_DEFINE |
258 |
int mcrypt_mode_module_ok(const char *file, const char *directory) |
259 |
{ |
260 |
word32 ret; |
261 |
lt_dlhandle _handle; |
262 |
int (*_version) (void); |
263 |
|
264 |
if (file == NULL && directory == NULL) { |
265 |
return MCRYPT_UNKNOWN_ERROR; |
266 |
} |
267 |
|
268 |
if (lt_dlinit() != 0) { |
269 |
return MCRYPT_UNKNOWN_ERROR; |
270 |
} |
271 |
/* LTDL_SET_PRELOADED_SYMBOLS(); */ |
272 |
|
273 |
_handle = mcrypt_dlopen(directory, NULL, file); |
274 |
|
275 |
if (!_handle) { |
276 |
lt_dlexit(); |
277 |
return MCRYPT_UNKNOWN_ERROR; |
278 |
} |
279 |
|
280 |
|
281 |
_version = lt_dlsym(_handle, "_mcrypt_mode_version"); |
282 |
|
283 |
if (_version == NULL) { |
284 |
lt_dlclose(_handle); |
285 |
lt_dlexit(); |
286 |
return MCRYPT_UNKNOWN_ERROR; |
287 |
} |
288 |
|
289 |
ret = _version(); |
290 |
|
291 |
lt_dlclose(_handle); |
292 |
lt_dlexit(); |
293 |
|
294 |
return ret; |
295 |
|
296 |
} |
297 |
|
298 |
/* Taken from libgcrypt */ |
299 |
|
300 |
static const char* |
301 |
parse_version_number( const char *s, int *number ) |
302 |
{ |
303 |
int val = 0; |
304 |
|
305 |
if( *s == '0' && isdigit(s[1]) ) |
306 |
return NULL; /* leading zeros are not allowed */ |
307 |
for ( ; isdigit(*s); s++ ) { |
308 |
val *= 10; |
309 |
val += *s - '0'; |
310 |
} |
311 |
*number = val; |
312 |
return val < 0? NULL : s; |
313 |
} |
314 |
|
315 |
|
316 |
static const char * |
317 |
parse_version_string( const char *s, int *major, int *minor, int *micro ) |
318 |
{ |
319 |
s = parse_version_number( s, major ); |
320 |
if( !s || *s != '.' ) |
321 |
return NULL; |
322 |
s++; |
323 |
s = parse_version_number( s, minor ); |
324 |
if( !s || *s != '.' ) |
325 |
return NULL; |
326 |
s++; |
327 |
s = parse_version_number( s, micro ); |
328 |
if( !s ) |
329 |
return NULL; |
330 |
return s; /* patchlevel */ |
331 |
} |
332 |
|
333 |
/**************** |
334 |
* Check that the the version of the library is at minimum the requested one |
335 |
* and return the version string; return NULL if the condition is not |
336 |
* satisfied. If a NULL is passed to this function, no check is done, |
337 |
* but the version string is simply returned. |
338 |
*/ |
339 |
const char * |
340 |
mcrypt_check_version( const char *req_version ) |
341 |
{ |
342 |
const char *ver = VERSION; |
343 |
int my_major, my_minor, my_micro; |
344 |
int rq_major, rq_minor, rq_micro; |
345 |
const char *my_plvl, *rq_plvl; |
346 |
|
347 |
if ( !req_version ) |
348 |
return ver; |
349 |
|
350 |
my_plvl = parse_version_string( ver, &my_major, &my_minor, &my_micro ); |
351 |
if ( !my_plvl ) |
352 |
return NULL; /* very strange our own version is bogus */ |
353 |
rq_plvl = parse_version_string( req_version, &rq_major, &rq_minor, |
354 |
&rq_micro ); |
355 |
if ( !rq_plvl ) |
356 |
return NULL; /* req version string is invalid */ |
357 |
|
358 |
if ( my_major > rq_major |
359 |
|| (my_major == rq_major && my_minor > rq_minor) |
360 |
|| (my_major == rq_major && my_minor == rq_minor |
361 |
&& my_micro > rq_micro) |
362 |
|| (my_major == rq_major && my_minor == rq_minor |
363 |
&& my_micro == rq_micro |
364 |
&& strcmp( my_plvl, rq_plvl ) >= 0) ) { |
365 |
return ver; |
366 |
} |
367 |
return NULL; |
368 |
} |
369 |
|