1 |
/* |
2 |
* Copyright (C) 1998,1999,2001 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.22 2002/06/23 10:35:20 nmav Exp $ */ |
21 |
|
22 |
#include <libdefs.h> |
23 |
#include <bzero.h> |
24 |
#include <xmemory.h> |
25 |
#include <mcrypt_internal.h> |
26 |
|
27 |
int mcrypt_algorithm_module_ok(const char *file, const char *directory); |
28 |
int mcrypt_mode_module_ok(const char *file, const char *directory); |
29 |
|
30 |
void *mcrypt_dlopen(mcrypt_dlhandle *handle, const char *a_directory, const char *m_directory, |
31 |
const char *filename); |
32 |
|
33 |
#ifdef HAVE_READDIR_R |
34 |
# define MAXPATHLEN 256 |
35 |
#endif |
36 |
|
37 |
WIN32DLL_DEFINE char *mcrypt_readdir(DIR * dirstream) |
38 |
{ |
39 |
|
40 |
char *result; |
41 |
struct dirent *ret = NULL; |
42 |
#ifdef HAVE_READDIR_R |
43 |
struct dirent ret2[sizeof(struct dirent)+MAXPATHLEN]; |
44 |
#endif |
45 |
|
46 |
#ifdef HAVE_READDIR_R |
47 |
readdir_r(dirstream, ret2, &ret); |
48 |
#else |
49 |
ret = readdir(dirstream); |
50 |
#endif |
51 |
if (ret==NULL) return NULL; |
52 |
|
53 |
result = calloc(1, strlen(ret->d_name) + 1); |
54 |
if (result == NULL) { |
55 |
return NULL; |
56 |
} |
57 |
strcpy(result, ret->d_name); |
58 |
|
59 |
return result; |
60 |
|
61 |
} |
62 |
|
63 |
extern const mcrypt_preloaded mps[]; |
64 |
|
65 |
WIN32DLL_DEFINE char **mcrypt_list_algorithms(const char *libdir, int *size) |
66 |
{ |
67 |
DIR *pdir; |
68 |
char directory[512]; |
69 |
char *dirname; |
70 |
char **filename = NULL, *ptr; |
71 |
int tmpsize, i=0; |
72 |
|
73 |
|
74 |
*size = 0; |
75 |
|
76 |
while( mps[i].name!=0 || mps[i].address!=0) { |
77 |
if (mps[i].name!=NULL && mps[i].address==NULL) { |
78 |
if (mcrypt_algorithm_module_ok( mps[i].name, NULL) > 0) { |
79 |
filename = |
80 |
realloc(filename, |
81 |
((*size) + 1) * sizeof(char *)); |
82 |
if (filename==NULL) { |
83 |
goto freeall; |
84 |
} |
85 |
filename[*size] = strdup( mps[i].name); |
86 |
if (filename[*size]==NULL) goto freeall; |
87 |
(*size)++; |
88 |
} |
89 |
} |
90 |
i++; |
91 |
} |
92 |
|
93 |
#ifdef USE_LTDL |
94 |
|
95 |
if (libdir == NULL) { |
96 |
strcpy(directory, LIBDIR); |
97 |
} else { |
98 |
strcpy(directory, libdir); |
99 |
} |
100 |
|
101 |
pdir = opendir(directory); |
102 |
if (pdir == NULL) { |
103 |
#ifdef DEBUG |
104 |
fprintf(stderr, "Unable to open directory %s.\n", |
105 |
directory); |
106 |
#endif |
107 |
return filename; |
108 |
} |
109 |
|
110 |
for (;;) { |
111 |
dirname = mcrypt_readdir(pdir); |
112 |
if (dirname != NULL) { |
113 |
tmpsize = strlen(dirname); |
114 |
if (tmpsize > 3) { |
115 |
if (mcrypt_algorithm_module_ok |
116 |
(dirname, directory) > 0) { |
117 |
|
118 |
ptr = |
119 |
strrchr( dirname, '.'); |
120 |
if (ptr != NULL) { |
121 |
*ptr = '\0'; |
122 |
tmpsize = strlen(dirname); |
123 |
} |
124 |
if (_mcrypt_search_symlist_lib( dirname)!=NULL) { |
125 |
free(dirname); |
126 |
continue; /* it's already in the list, |
127 |
* since it's included in the lib. |
128 |
*/ |
129 |
} |
130 |
filename = |
131 |
realloc(filename, |
132 |
(*size + |
133 |
1) * sizeof(char *)); |
134 |
if (filename==NULL) { |
135 |
free(dirname); |
136 |
goto freeall; |
137 |
} |
138 |
|
139 |
|
140 |
filename[*size] = (char*) |
141 |
calloc(1, tmpsize + 1); |
142 |
if (filename[*size]==NULL) { |
143 |
free(dirname); |
144 |
goto freeall; |
145 |
} |
146 |
strcpy(filename[*size], dirname); |
147 |
|
148 |
(*size)++; |
149 |
} |
150 |
} |
151 |
free(dirname); |
152 |
} else { |
153 |
break; |
154 |
} |
155 |
|
156 |
} |
157 |
|
158 |
|
159 |
closedir(pdir); |
160 |
|
161 |
#endif |
162 |
|
163 |
return filename; |
164 |
|
165 |
freeall: |
166 |
for (i=0;i<(*size);i++) { |
167 |
free(filename[i]); |
168 |
} |
169 |
free(filename); |
170 |
return NULL; |
171 |
} |
172 |
|
173 |
WIN32DLL_DEFINE char **mcrypt_list_modes(const char *libdir, int *size) |
174 |
{ |
175 |
DIR *pdir; |
176 |
char directory[512]; |
177 |
char *dirname; |
178 |
char **filename = NULL, *ptr; |
179 |
int tmpsize; |
180 |
int i=0; |
181 |
|
182 |
*size = 0; |
183 |
|
184 |
while( mps[i].name!=0 || mps[i].address!=0) { |
185 |
if (mps[i].name!=NULL && mps[i].address==NULL) { |
186 |
if (mcrypt_mode_module_ok( mps[i].name, NULL) > 0) { |
187 |
filename = |
188 |
realloc(filename, |
189 |
(*size + |
190 |
1) * sizeof(char *)); |
191 |
if (filename==NULL) { |
192 |
goto freeall; |
193 |
} |
194 |
filename[*size] = strdup( mps[i].name); |
195 |
if (filename[*size]==NULL) goto freeall; |
196 |
(*size)++; |
197 |
} |
198 |
} |
199 |
i++; |
200 |
} |
201 |
|
202 |
#ifdef USE_LTDL |
203 |
|
204 |
if (libdir == NULL) { |
205 |
strcpy(directory, LIBDIR); |
206 |
} else { |
207 |
strcpy(directory, libdir); |
208 |
} |
209 |
|
210 |
pdir = opendir(directory); |
211 |
if (pdir == NULL) { |
212 |
#ifdef DEBUG |
213 |
fprintf(stderr, "Unable to open directory %s.\n", |
214 |
directory); |
215 |
#endif |
216 |
return filename; |
217 |
} |
218 |
|
219 |
for (;;) { |
220 |
|
221 |
dirname = mcrypt_readdir(pdir); |
222 |
if (dirname != NULL) { |
223 |
tmpsize = strlen(dirname); |
224 |
if (tmpsize > 3) { |
225 |
if (mcrypt_mode_module_ok |
226 |
(dirname, directory) > 0) { |
227 |
|
228 |
ptr = |
229 |
strrchr( dirname, '.'); |
230 |
if (ptr != NULL) { |
231 |
*ptr = '\0'; |
232 |
tmpsize = strlen(dirname); |
233 |
} |
234 |
if (_mcrypt_search_symlist_lib( dirname)!=NULL) { |
235 |
free(dirname); |
236 |
continue; /* it's already in the list, |
237 |
* since it's included in the lib. |
238 |
*/ |
239 |
} |
240 |
filename = |
241 |
realloc(filename, |
242 |
(*size + |
243 |
1) * sizeof(char *)); |
244 |
if (filename==NULL) { |
245 |
free(dirname); |
246 |
goto freeall; |
247 |
} |
248 |
filename[*size] = |
249 |
calloc(1, tmpsize + 1); |
250 |
if (filename[*size]==NULL) { |
251 |
free(dirname); |
252 |
goto freeall; |
253 |
} |
254 |
|
255 |
strcpy(filename[*size], dirname); |
256 |
(*size)++; |
257 |
} |
258 |
} |
259 |
free(dirname); |
260 |
} else { |
261 |
break; |
262 |
} |
263 |
|
264 |
} |
265 |
|
266 |
closedir(pdir); |
267 |
#endif |
268 |
|
269 |
return filename; |
270 |
|
271 |
freeall: |
272 |
for (i=0;i<(*size);i++) { |
273 |
free(filename[i]); |
274 |
} |
275 |
free(filename); |
276 |
return NULL; |
277 |
} |
278 |
|
279 |
WIN32DLL_DEFINE void mcrypt_free_p(char **p, int size) |
280 |
{ |
281 |
int i; |
282 |
|
283 |
for (i = 0; i < size; i++) { |
284 |
free(p[i]); |
285 |
} |
286 |
free(p); |
287 |
} |
288 |
|
289 |
WIN32DLL_DEFINE |
290 |
int mcrypt_algorithm_module_ok(const char *file, const char *directory) |
291 |
{ |
292 |
word32 ret; |
293 |
mcrypt_dlhandle _handle; |
294 |
void* rr; |
295 |
int (*_version) (void); |
296 |
|
297 |
if (file == NULL && directory == NULL) { |
298 |
return MCRYPT_UNKNOWN_ERROR; |
299 |
} |
300 |
|
301 |
if (lt_dlinit() != 0) { |
302 |
return MCRYPT_UNKNOWN_ERROR; |
303 |
} |
304 |
|
305 |
|
306 |
rr = mcrypt_dlopen(&_handle, directory, NULL, file); |
307 |
|
308 |
if (!rr) { |
309 |
lt_dlexit(); |
310 |
return MCRYPT_UNKNOWN_ERROR; |
311 |
} |
312 |
|
313 |
|
314 |
_version = mcrypt_dlsym(_handle, "_mcrypt_algorithm_version"); |
315 |
|
316 |
if (_version == NULL) { |
317 |
mcrypt_dlclose(_handle); |
318 |
lt_dlexit(); |
319 |
return MCRYPT_UNKNOWN_ERROR; |
320 |
} |
321 |
|
322 |
ret = _version(); |
323 |
|
324 |
mcrypt_dlclose(_handle); |
325 |
lt_dlexit(); |
326 |
|
327 |
return ret; |
328 |
|
329 |
} |
330 |
|
331 |
WIN32DLL_DEFINE |
332 |
int mcrypt_mode_module_ok(const char *file, const char *directory) |
333 |
{ |
334 |
word32 ret; |
335 |
mcrypt_dlhandle _handle; |
336 |
void* rr; |
337 |
int (*_version) (void); |
338 |
|
339 |
if (file == NULL && directory == NULL) { |
340 |
return MCRYPT_UNKNOWN_ERROR; |
341 |
} |
342 |
|
343 |
if (lt_dlinit() != 0) { |
344 |
return MCRYPT_UNKNOWN_ERROR; |
345 |
} |
346 |
|
347 |
rr = mcrypt_dlopen(&_handle, directory, NULL, file); |
348 |
if (!rr) { |
349 |
lt_dlexit(); |
350 |
return MCRYPT_UNKNOWN_ERROR; |
351 |
} |
352 |
|
353 |
|
354 |
_version = mcrypt_dlsym(_handle, "_mcrypt_mode_version"); |
355 |
|
356 |
if (_version == NULL) { |
357 |
mcrypt_dlclose(_handle); |
358 |
lt_dlexit(); |
359 |
return MCRYPT_UNKNOWN_ERROR; |
360 |
} |
361 |
|
362 |
ret = _version(); |
363 |
|
364 |
mcrypt_dlclose(_handle); |
365 |
lt_dlexit(); |
366 |
|
367 |
return ret; |
368 |
|
369 |
} |
370 |
|
371 |
/* Taken from libgcrypt */ |
372 |
|
373 |
static const char* |
374 |
parse_version_number( const char *s, int *number ) |
375 |
{ |
376 |
int val = 0; |
377 |
|
378 |
if( *s == '0' && isdigit(s[1]) ) |
379 |
return NULL; /* leading zeros are not allowed */ |
380 |
for ( ; isdigit(*s); s++ ) { |
381 |
val *= 10; |
382 |
val += *s - '0'; |
383 |
} |
384 |
*number = val; |
385 |
return val < 0? NULL : s; |
386 |
} |
387 |
|
388 |
|
389 |
static const char * |
390 |
parse_version_string( const char *s, int *major, int *minor, int *micro ) |
391 |
{ |
392 |
s = parse_version_number( s, major ); |
393 |
if( !s || *s != '.' ) |
394 |
return NULL; |
395 |
s++; |
396 |
s = parse_version_number( s, minor ); |
397 |
if( !s || *s != '.' ) |
398 |
return NULL; |
399 |
s++; |
400 |
s = parse_version_number( s, micro ); |
401 |
if( !s ) |
402 |
return NULL; |
403 |
return s; /* patchlevel */ |
404 |
} |
405 |
|
406 |
/**************** |
407 |
* Check that the the version of the library is at minimum the requested one |
408 |
* and return the version string; return NULL if the condition is not |
409 |
* satisfied. If a NULL is passed to this function, no check is done, |
410 |
* but the version string is simply returned. |
411 |
*/ |
412 |
const char * |
413 |
mcrypt_check_version( const char *req_version ) |
414 |
{ |
415 |
const char *ver = VERSION; |
416 |
int my_major, my_minor, my_micro; |
417 |
int rq_major, rq_minor, rq_micro; |
418 |
const char *my_plvl, *rq_plvl; |
419 |
|
420 |
if ( !req_version ) |
421 |
return ver; |
422 |
|
423 |
my_plvl = parse_version_string( ver, &my_major, &my_minor, &my_micro ); |
424 |
if ( !my_plvl ) |
425 |
return NULL; /* very strange our own version is bogus */ |
426 |
rq_plvl = parse_version_string( req_version, &rq_major, &rq_minor, |
427 |
&rq_micro ); |
428 |
if ( !rq_plvl ) |
429 |
return NULL; /* req version string is invalid */ |
430 |
|
431 |
if ( my_major > rq_major |
432 |
|| (my_major == rq_major && my_minor > rq_minor) |
433 |
|| (my_major == rq_major && my_minor == rq_minor |
434 |
&& my_micro > rq_micro) |
435 |
|| (my_major == rq_major && my_minor == rq_minor |
436 |
&& my_micro == rq_micro |
437 |
&& strcmp( my_plvl, rq_plvl ) >= 0) ) { |
438 |
return ver; |
439 |
} |
440 |
return NULL; |
441 |
} |
442 |
|