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