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.12 2000/03/31 03:13:17 nikos 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 |
#if 0 |
31 |
extern int preloaded_symbols; |
32 |
#endif |
33 |
|
34 |
int mcrypt_algorithm_module_ok(const char *file, const char* directory); |
35 |
int mcrypt_mode_module_ok(const char *file, const char* directory); |
36 |
|
37 |
char ** mcrypt_list_algorithms(char *libdir, int *size) |
38 |
{ |
39 |
DIR *pdir; |
40 |
char directory[500]; |
41 |
struct dirent *dirname; |
42 |
char **filename = NULL, *ptr; |
43 |
int tmpsize; |
44 |
|
45 |
*size = 0; |
46 |
|
47 |
if (libdir == NULL) { |
48 |
strcpy(directory, LIBDIR); |
49 |
} else { |
50 |
strcpy(directory, libdir); |
51 |
} |
52 |
|
53 |
#if 0 |
54 |
if (preloaded_symbols!=0) { /* We have preloaded symbols */ |
55 |
i=0; |
56 |
for (;;) { |
57 |
if (mcrypt_algorithm_module_ok(lt_preloaded_symbols[i].name, NULL) > 0 ) { |
58 |
tmpsize=strlen(lt_preloaded_symbols[i].name); |
59 |
filename = |
60 |
realloc(filename, |
61 |
(*size + |
62 |
1) * |
63 |
sizeof(char*)); |
64 |
filename[*size] = |
65 |
calloc(1, tmpsize + 1); |
66 |
strcpy(filename[*size], |
67 |
lt_preloaded_symbols[i].name); |
68 |
ptr=strrchr(filename[*size], '.'); |
69 |
if (ptr!=NULL) *ptr='\0'; |
70 |
(*size)++; |
71 |
} |
72 |
if (lt_preloaded_symbols[i].address==0 && lt_preloaded_symbols[i].name==NULL) break; |
73 |
i++; |
74 |
} |
75 |
} |
76 |
#endif |
77 |
|
78 |
pdir = opendir(directory); |
79 |
if (pdir == NULL) { |
80 |
fprintf(stderr, "Unable to open directory %s.\n", |
81 |
directory); |
82 |
return NULL; |
83 |
} |
84 |
|
85 |
for (;;) { |
86 |
dirname = readdir(pdir); |
87 |
if (dirname != NULL) { |
88 |
#ifdef DT_REG |
89 |
if ((dirname->d_type == DT_REG) |
90 |
|| (dirname->d_type == DT_UNKNOWN)) { |
91 |
#endif |
92 |
if (strlen(dirname->d_name) > 3) { |
93 |
tmpsize = strlen(dirname->d_name); |
94 |
|
95 |
if (mcrypt_algorithm_module_ok(dirname->d_name, directory) > 0 ) { |
96 |
filename = |
97 |
realloc(filename, |
98 |
(*size + |
99 |
1) * |
100 |
sizeof(char |
101 |
*)); |
102 |
filename[*size] = |
103 |
calloc(1, tmpsize + 1); |
104 |
strcpy(filename[*size], |
105 |
dirname->d_name); |
106 |
|
107 |
ptr=strrchr(filename[*size], '.'); |
108 |
if (ptr!=NULL) *ptr='\0'; |
109 |
(*size)++; |
110 |
} |
111 |
} |
112 |
#ifdef DT_REG |
113 |
} |
114 |
#endif |
115 |
} else { |
116 |
break; |
117 |
} |
118 |
|
119 |
} |
120 |
|
121 |
|
122 |
|
123 |
closedir(pdir); |
124 |
|
125 |
return filename; |
126 |
|
127 |
} |
128 |
|
129 |
char ** mcrypt_list_modes(char *libdir, int *size) |
130 |
{ |
131 |
DIR *pdir; |
132 |
char directory[500]; |
133 |
struct dirent *dirname; |
134 |
char **filename = NULL, *ptr; |
135 |
int tmpsize; |
136 |
|
137 |
if (libdir == NULL) { |
138 |
strcpy(directory, LIBDIR); |
139 |
} else { |
140 |
strcpy(directory, libdir); |
141 |
} |
142 |
|
143 |
lt_dladdsearchdir(directory); |
144 |
|
145 |
#if 0 |
146 |
if (preloaded_symbols!=0) { /* We have preloaded symbols */ |
147 |
i=0; |
148 |
for (;;) { |
149 |
if (mcrypt_mode_module_ok(lt_preloaded_symbols[i].name, NULL) > 0 ) { |
150 |
tmpsize=strlen(lt_preloaded_symbols[i].name); |
151 |
filename = |
152 |
realloc(filename, |
153 |
(*size + |
154 |
1) * |
155 |
sizeof(char*)); |
156 |
filename[*size] = |
157 |
calloc(1, tmpsize + 1); |
158 |
strcpy(filename[*size], |
159 |
lt_preloaded_symbols[i].name); |
160 |
ptr=strrchr(filename[*size], '.'); |
161 |
if (ptr!=NULL) *ptr='\0'; |
162 |
(*size)++; |
163 |
} |
164 |
if (lt_preloaded_symbols[i].address==0 && lt_preloaded_symbols[i].name==NULL) break; |
165 |
i++; |
166 |
} |
167 |
} |
168 |
#endif |
169 |
|
170 |
pdir = opendir(directory); |
171 |
if (pdir == NULL) { |
172 |
fprintf(stderr, "Unable to open directory %s.\n", |
173 |
directory); |
174 |
return NULL; |
175 |
} |
176 |
|
177 |
*size = 0; |
178 |
for (;;) { |
179 |
|
180 |
dirname = readdir(pdir); |
181 |
if (dirname != NULL) { |
182 |
#ifdef DT_REG |
183 |
if ((dirname->d_type == DT_REG) |
184 |
|| (dirname->d_type == DT_UNKNOWN)) { |
185 |
#endif |
186 |
if (strlen(dirname->d_name) > 3) { |
187 |
tmpsize = strlen(dirname->d_name); |
188 |
if ( mcrypt_mode_module_ok(dirname->d_name, directory) > 0 ) { |
189 |
filename = |
190 |
realloc(filename, |
191 |
(*size + |
192 |
1) * |
193 |
sizeof(char |
194 |
*)); |
195 |
filename[*size] = |
196 |
calloc(1, tmpsize + 1); |
197 |
strcpy(filename[*size], |
198 |
dirname->d_name); |
199 |
ptr=strrchr(filename[*size], '.'); |
200 |
if (ptr!=NULL) *ptr='\0'; |
201 |
(*size)++; |
202 |
} |
203 |
} |
204 |
#ifdef DT_REG |
205 |
} |
206 |
#endif |
207 |
} else { |
208 |
break; |
209 |
} |
210 |
|
211 |
} |
212 |
|
213 |
closedir(pdir); |
214 |
return filename; |
215 |
} |
216 |
|
217 |
|
218 |
void mcrypt_free_p(char** p, int size) { |
219 |
int i; |
220 |
|
221 |
for (i=0;i<size;i++) { |
222 |
free(p[i]); |
223 |
} |
224 |
free(p); |
225 |
} |
226 |
|
227 |
int mcrypt_algorithm_module_ok(const char *file, const char* directory) |
228 |
{ |
229 |
int i; |
230 |
word32 ret; |
231 |
lt_dlhandle *_handle; |
232 |
int (*_version) (void); |
233 |
|
234 |
if (file == NULL && directory==NULL) { |
235 |
return MCRYPT_UNKNOWN_ERROR; |
236 |
} |
237 |
|
238 |
if (lt_dlinit()!=0) { |
239 |
return MCRYPT_UNKNOWN_ERROR; |
240 |
} |
241 |
|
242 |
i=lt_dladdsearchdir(directory); |
243 |
|
244 |
/* LTDL_SET_PRELOADED_SYMBOLS(); */ |
245 |
_handle = lt_dlopenext(file); |
246 |
if (!_handle) { |
247 |
_handle = lt_dlopen(file); |
248 |
} |
249 |
|
250 |
|
251 |
|
252 |
if (_handle==NULL) { |
253 |
/* fputs(lt_dlerror(), stderr); |
254 |
* fputs("\n", stderr); |
255 |
*/ |
256 |
lt_dlexit(); |
257 |
return MCRYPT_UNKNOWN_ERROR; |
258 |
} |
259 |
|
260 |
|
261 |
_version = lt_dlsym(_handle, "_mcrypt_algorithm_version"); |
262 |
|
263 |
if (_version==NULL) { |
264 |
lt_dlclose(_handle); |
265 |
lt_dlexit(); |
266 |
return MCRYPT_UNKNOWN_ERROR; |
267 |
} |
268 |
|
269 |
ret = _version(); |
270 |
|
271 |
lt_dlclose(_handle); |
272 |
lt_dlexit(); |
273 |
|
274 |
return ret; |
275 |
|
276 |
} |
277 |
|
278 |
int mcrypt_mode_module_ok(const char *file, const char* directory) |
279 |
{ |
280 |
int i; |
281 |
word32 ret; |
282 |
lt_dlhandle *_handle; |
283 |
int (*_version) (void); |
284 |
|
285 |
if (file == NULL && directory==NULL) { |
286 |
return MCRYPT_UNKNOWN_ERROR; |
287 |
} |
288 |
|
289 |
if (lt_dlinit()!=0) { |
290 |
return MCRYPT_UNKNOWN_ERROR; |
291 |
} |
292 |
/* LTDL_SET_PRELOADED_SYMBOLS(); */ |
293 |
i=lt_dladdsearchdir(directory); |
294 |
|
295 |
_handle = lt_dlopenext(file); |
296 |
if (!_handle) { |
297 |
_handle = lt_dlopen(file); |
298 |
} |
299 |
|
300 |
if (!_handle) { |
301 |
lt_dlexit(); |
302 |
return MCRYPT_UNKNOWN_ERROR; |
303 |
} |
304 |
|
305 |
|
306 |
_version = lt_dlsym(_handle, "_mcrypt_mode_version"); |
307 |
|
308 |
if (_version==NULL) { |
309 |
lt_dlclose(_handle); |
310 |
lt_dlexit(); |
311 |
return MCRYPT_UNKNOWN_ERROR; |
312 |
} |
313 |
|
314 |
ret = _version(); |
315 |
|
316 |
lt_dlclose(_handle); |
317 |
lt_dlexit(); |
318 |
|
319 |
return ret; |
320 |
|
321 |
} |
322 |
#if 0 |
323 |
char ** mcrypt_list_algorithms(char *libdir, int *size) |
324 |
{ |
325 |
|
326 |
char **filename; |
327 |
int i; |
328 |
|
329 |
*size = 21; |
330 |
|
331 |
filename = malloc( (*size)*sizeof(char*)); |
332 |
|
333 |
for (i=0; i<*size; i++) { |
334 |
filename[i]=malloc(strlen(algos[i])+1); |
335 |
strcpy(filename[i], algos[i]); |
336 |
} |
337 |
|
338 |
return filename; |
339 |
|
340 |
} |
341 |
|
342 |
char ** mcrypt_list_modes(char *libdir, int *size) |
343 |
{ |
344 |
char modes[6][64] = { "stream", "ecb", "cbc", "cfb", "ofb", |
345 |
"nofb" }; |
346 |
char **filename; |
347 |
int i; |
348 |
|
349 |
*size = 6; |
350 |
|
351 |
filename = malloc( (*size)*sizeof(char*)); |
352 |
|
353 |
for (i=0; i<*size; i++) { |
354 |
filename[i]=malloc(strlen(modes[i])+1); |
355 |
strcpy(filename[i], modes[i]); |
356 |
} |
357 |
|
358 |
return filename; |
359 |
} |
360 |
|
361 |
|
362 |
void mcrypt_free_p(char** p, int size) { |
363 |
int i; |
364 |
|
365 |
for (i=0;i<size;i++) { |
366 |
free(p[i]); |
367 |
} |
368 |
free(p); |
369 |
} |
370 |
|
371 |
int mcrypt_algorithm_module_ok(char *file, char* directory) |
372 |
{ |
373 |
return algo_name2num(file); |
374 |
} |
375 |
|
376 |
int mcrypt_mode_module_ok(char *file, char* directory) |
377 |
{ |
378 |
return mode_name2num(file); |
379 |
} |
380 |
|
381 |
#endif |