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.2 2000/09/16 02:01:38 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 |
#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 |
#ifndef USE_LTDL |
37 |
void *mcrypt_dlopen_ext(const char *filename); |
38 |
#endif |
39 |
void *mcrypt_dlopen ( const char* a_directory, const char *m_directory, const char *filename); |
40 |
|
41 |
struct dirent *mcrypt_readdir(DIR * dirstream) |
42 |
{ |
43 |
|
44 |
struct dirent *result; |
45 |
struct dirent *ret = NULL; |
46 |
|
47 |
#ifdef HAVE_READDIR_R |
48 |
struct dirent ret2[sizeof(struct dirent)]; |
49 |
|
50 |
readdir_r(dirstream, ret2, &ret); |
51 |
#else |
52 |
ret = readdir(dirstream); |
53 |
#endif |
54 |
if (ret != NULL) { |
55 |
result = malloc(sizeof(struct dirent)); |
56 |
memmove(result, ret, sizeof(struct dirent)); |
57 |
} else { |
58 |
result = NULL; |
59 |
} |
60 |
|
61 |
return result; |
62 |
|
63 |
} |
64 |
|
65 |
char **mcrypt_list_algorithms(char *libdir, int *size) |
66 |
{ |
67 |
DIR *pdir; |
68 |
char directory[512]; |
69 |
struct dirent *dirname; |
70 |
char **filename = NULL, *ptr; |
71 |
int tmpsize; |
72 |
|
73 |
*size = 0; |
74 |
|
75 |
if (libdir == NULL) { |
76 |
strcpy(directory, LIBDIR); |
77 |
} else { |
78 |
strcpy(directory, libdir); |
79 |
} |
80 |
|
81 |
pdir = opendir(directory); |
82 |
if (pdir == NULL) { |
83 |
fprintf(stderr, "Unable to open directory %s.\n", |
84 |
directory); |
85 |
return NULL; |
86 |
} |
87 |
|
88 |
|
89 |
for (;;) { |
90 |
dirname = mcrypt_readdir(pdir); |
91 |
if (dirname != NULL) { |
92 |
#ifdef DT_REG |
93 |
if ((dirname->d_type == DT_REG) |
94 |
|| (dirname->d_type == DT_UNKNOWN)) { |
95 |
#endif |
96 |
tmpsize = strlen(dirname->d_name); /*(unsigned int)dirname->d_namlen;*/ |
97 |
|
98 |
if (tmpsize > 3) { |
99 |
if (mcrypt_algorithm_module_ok(dirname->d_name, directory) > 0) { |
100 |
filename = realloc(filename, (*size + 1) * sizeof(char *)); |
101 |
filename[*size] = calloc(1, tmpsize + 1); |
102 |
strcpy(filename[*size], dirname->d_name); |
103 |
|
104 |
ptr = strrchr(filename[*size], '.'); |
105 |
if (ptr != NULL) |
106 |
*ptr = '\0'; |
107 |
(*size)++; |
108 |
} |
109 |
} |
110 |
#ifdef DT_REG |
111 |
} |
112 |
#endif |
113 |
if (dirname != NULL) |
114 |
free(dirname); |
115 |
} else { |
116 |
if (dirname != NULL) |
117 |
free(dirname); |
118 |
break; |
119 |
} |
120 |
|
121 |
} |
122 |
|
123 |
|
124 |
closedir(pdir); |
125 |
|
126 |
return filename; |
127 |
|
128 |
} |
129 |
|
130 |
char **mcrypt_list_modes(char *libdir, int *size) |
131 |
{ |
132 |
DIR *pdir; |
133 |
char directory[512]; |
134 |
struct dirent *dirname; |
135 |
char **filename = NULL, *ptr; |
136 |
int tmpsize; |
137 |
|
138 |
if (libdir == NULL) { |
139 |
strcpy(directory, LIBDIR); |
140 |
} else { |
141 |
strcpy(directory, libdir); |
142 |
} |
143 |
|
144 |
pdir = opendir(directory); |
145 |
if (pdir == NULL) { |
146 |
fprintf(stderr, "Unable to open directory %s.\n", |
147 |
directory); |
148 |
return NULL; |
149 |
} |
150 |
|
151 |
*size = 0; |
152 |
for (;;) { |
153 |
|
154 |
dirname = mcrypt_readdir(pdir); |
155 |
if (dirname != NULL) { |
156 |
#ifdef DT_REG |
157 |
if ((dirname->d_type == DT_REG) |
158 |
|| (dirname->d_type == DT_UNKNOWN)) { |
159 |
#endif |
160 |
tmpsize = strlen(dirname->d_name); |
161 |
if (tmpsize > 3) { |
162 |
if (mcrypt_mode_module_ok |
163 |
(dirname->d_name, |
164 |
directory) > 0) { |
165 |
filename = |
166 |
realloc(filename, |
167 |
(*size + |
168 |
1) * |
169 |
sizeof(char |
170 |
*)); |
171 |
filename[*size] = |
172 |
calloc(1, tmpsize + 1); |
173 |
strcpy(filename[*size], |
174 |
dirname->d_name); |
175 |
ptr = |
176 |
strrchr(filename |
177 |
[*size], '.'); |
178 |
if (ptr != NULL) |
179 |
*ptr = '\0'; |
180 |
(*size)++; |
181 |
} |
182 |
} |
183 |
#ifdef DT_REG |
184 |
} |
185 |
#endif |
186 |
if (dirname != NULL) |
187 |
free(dirname); |
188 |
} else { |
189 |
if (dirname != NULL) |
190 |
free(dirname); |
191 |
break; |
192 |
} |
193 |
|
194 |
} |
195 |
|
196 |
closedir(pdir); |
197 |
return filename; |
198 |
} |
199 |
|
200 |
|
201 |
void mcrypt_free_p(char **p, int size) |
202 |
{ |
203 |
int i; |
204 |
|
205 |
for (i = 0; i < size; i++) { |
206 |
free(p[i]); |
207 |
} |
208 |
free(p); |
209 |
} |
210 |
|
211 |
int mcrypt_algorithm_module_ok(const char *file, const char *directory) |
212 |
{ |
213 |
word32 ret; |
214 |
lt_dlhandle *_handle; |
215 |
int (*_version) (void); |
216 |
|
217 |
if (file == NULL && directory == NULL) { |
218 |
return MCRYPT_UNKNOWN_ERROR; |
219 |
} |
220 |
|
221 |
if (lt_dlinit() != 0) { |
222 |
return MCRYPT_UNKNOWN_ERROR; |
223 |
} |
224 |
|
225 |
/* LTDL_SET_PRELOADED_SYMBOLS(); */ |
226 |
|
227 |
_handle = mcrypt_dlopen(directory, NULL, file); |
228 |
|
229 |
if (!_handle) { |
230 |
/* fputs(lt_dlerror(), stderr); |
231 |
* fputs("\n", stderr); |
232 |
*/ |
233 |
lt_dlexit(); |
234 |
return MCRYPT_UNKNOWN_ERROR; |
235 |
} |
236 |
|
237 |
|
238 |
_version = lt_dlsym(_handle, "_mcrypt_algorithm_version"); |
239 |
|
240 |
if (_version == NULL) { |
241 |
lt_dlclose(_handle); |
242 |
lt_dlexit(); |
243 |
return MCRYPT_UNKNOWN_ERROR; |
244 |
} |
245 |
|
246 |
ret = _version(); |
247 |
|
248 |
lt_dlclose(_handle); |
249 |
lt_dlexit(); |
250 |
|
251 |
return ret; |
252 |
|
253 |
} |
254 |
|
255 |
int mcrypt_mode_module_ok(const char *file, const char *directory) |
256 |
{ |
257 |
word32 ret; |
258 |
lt_dlhandle *_handle; |
259 |
int (*_version) (void); |
260 |
|
261 |
if (file == NULL && directory == NULL) { |
262 |
return MCRYPT_UNKNOWN_ERROR; |
263 |
} |
264 |
|
265 |
if (lt_dlinit() != 0) { |
266 |
return MCRYPT_UNKNOWN_ERROR; |
267 |
} |
268 |
/* LTDL_SET_PRELOADED_SYMBOLS(); */ |
269 |
|
270 |
_handle = mcrypt_dlopen(directory, NULL, file); |
271 |
|
272 |
if (!_handle) { |
273 |
lt_dlexit(); |
274 |
return MCRYPT_UNKNOWN_ERROR; |
275 |
} |
276 |
|
277 |
|
278 |
_version = lt_dlsym(_handle, "_mcrypt_mode_version"); |
279 |
|
280 |
if (_version == NULL) { |
281 |
lt_dlclose(_handle); |
282 |
lt_dlexit(); |
283 |
return MCRYPT_UNKNOWN_ERROR; |
284 |
} |
285 |
|
286 |
ret = _version(); |
287 |
|
288 |
lt_dlclose(_handle); |
289 |
lt_dlexit(); |
290 |
|
291 |
return ret; |
292 |
|
293 |
} |
294 |
|