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