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