| Line | Source | Count |
| 1 | | - |
| 2 | | - |
| 3 | | - |
| 4 | | - |
| 5 | | - |
| 6 | | - |
| 7 | | - |
| 8 | | - |
| 9 | | - |
| 10 | | - |
| 11 | | - |
| 12 | | - |
| 13 | | - |
| 14 | | - |
| 15 | | - |
| 16 | | - |
| 17 | | - |
| 18 | | - |
| 19 | | - |
| 20 | | - |
| 21 | | - |
| 22 | | - |
| 23 | | - |
| 24 | | - |
| 25 | | - |
| 26 | | - |
| 27 | | - |
| 28 | | - |
| 29 | | - |
| 30 | | - |
| 31 | | - |
| 32 | | - |
| 33 | | - |
| 34 | | - |
| 35 | | - |
| 36 | | - |
| 37 | | - |
| 38 | | - |
| 39 | | - |
| 40 | | - |
| 41 | | - |
| 42 | | - |
| 43 | | - |
| 44 | | - |
| 45 | | - |
| 46 | | - |
| 47 | | - |
| 48 | | - |
| 49 | | - |
| 50 | | - |
| 51 | | - |
| 52 | | - |
| 53 | | - |
| 54 | | - |
| 55 | | - |
| 56 | | - |
| 57 | | - |
| 58 | | - |
| 59 | #include <stdio.h> | - |
| 60 | #include <string.h> | - |
| 61 | | - |
| 62 | #include <openssl/dso.h> | - |
| 63 | #include <openssl/err.h> | - |
| 64 | | - |
| 65 | #ifndef DSO_DLFCN | - |
| 66 | DSO_METHOD * | - |
| 67 | DSO_METHOD_dlfcn(void) | - |
| 68 | { | - |
| 69 | return NULL; never executed: return ((void *)0) ; | 0 |
| 70 | } | - |
| 71 | #else | - |
| 72 | | - |
| 73 | #ifdef HAVE_DLFCN_H | - |
| 74 | # include <dlfcn.h> | - |
| 75 | # define HAVE_DLINFO 1 | - |
| 76 | #endif | - |
| 77 | | - |
| 78 | | - |
| 79 | #define DSO_MAX_TRANSLATED_SIZE 256 | - |
| 80 | | - |
| 81 | static int dlfcn_load(DSO *dso); | - |
| 82 | static int dlfcn_unload(DSO *dso); | - |
| 83 | static void *dlfcn_bind_var(DSO *dso, const char *symname); | - |
| 84 | static DSO_FUNC_TYPE dlfcn_bind_func(DSO *dso, const char *symname); | - |
| 85 | static char *dlfcn_name_converter(DSO *dso, const char *filename); | - |
| 86 | static char *dlfcn_merger(DSO *dso, const char *filespec1, | - |
| 87 | const char *filespec2); | - |
| 88 | static int dlfcn_pathbyaddr(void *addr, char *path, int sz); | - |
| 89 | static void *dlfcn_globallookup(const char *name); | - |
| 90 | | - |
| 91 | static DSO_METHOD dso_meth_dlfcn = { | - |
| 92 | .name = "OpenSSL 'dlfcn' shared library method", | - |
| 93 | .dso_load = dlfcn_load, | - |
| 94 | .dso_unload = dlfcn_unload, | - |
| 95 | .dso_bind_var = dlfcn_bind_var, | - |
| 96 | .dso_bind_func = dlfcn_bind_func, | - |
| 97 | .dso_name_converter = dlfcn_name_converter, | - |
| 98 | .dso_merger = dlfcn_merger, | - |
| 99 | .pathbyaddr = dlfcn_pathbyaddr, | - |
| 100 | .globallookup = dlfcn_globallookup | - |
| 101 | }; | - |
| 102 | | - |
| 103 | DSO_METHOD * | - |
| 104 | DSO_METHOD_dlfcn(void) | - |
| 105 | { | - |
| 106 | return (&dso_meth_dlfcn); | - |
| 107 | } | - |
| 108 | | - |
| 109 | | - |
| 110 | | - |
| 111 | | - |
| 112 | | - |
| 113 | static int | - |
| 114 | dlfcn_load(DSO *dso) | - |
| 115 | { | - |
| 116 | void *ptr = NULL; | - |
| 117 | | - |
| 118 | char *filename = DSO_convert_filename(dso, NULL); | - |
| 119 | int flags = RTLD_LAZY; | - |
| 120 | | - |
| 121 | if (filename == NULL) { | - |
| 122 | DSOerror(DSO_R_NO_FILENAME); | - |
| 123 | goto err; | - |
| 124 | } | - |
| 125 | | - |
| 126 | if (dso->flags & DSO_FLAG_GLOBAL_SYMBOLS) | - |
| 127 | flags |= RTLD_GLOBAL; | - |
| 128 | ptr = dlopen(filename, flags); | - |
| 129 | if (ptr == NULL) { | - |
| 130 | DSOerror(DSO_R_LOAD_FAILED); | - |
| 131 | ERR_asprintf_error_data("filename(%s): %s", filename, | - |
| 132 | dlerror()); | - |
| 133 | goto err; | - |
| 134 | } | - |
| 135 | if (!sk_void_push(dso->meth_data, (char *)ptr)) { | - |
| 136 | DSOerror(DSO_R_STACK_ERROR); | - |
| 137 | goto err; | - |
| 138 | } | - |
| 139 | | - |
| 140 | dso->loaded_filename = filename; | - |
| 141 | return (1); | - |
| 142 | | - |
| 143 | err: | - |
| 144 | | - |
| 145 | free(filename); | - |
| 146 | if (ptr != NULL) | - |
| 147 | dlclose(ptr); | - |
| 148 | return (0); | - |
| 149 | } | - |
| 150 | | - |
| 151 | static int | - |
| 152 | dlfcn_unload(DSO *dso) | - |
| 153 | { | - |
| 154 | void *ptr; | - |
| 155 | if (dso == NULL) { | - |
| 156 | DSOerror(ERR_R_PASSED_NULL_PARAMETER); | - |
| 157 | return (0); | - |
| 158 | } | - |
| 159 | if (sk_void_num(dso->meth_data) < 1) | - |
| 160 | return (1); | - |
| 161 | ptr = sk_void_pop(dso->meth_data); | - |
| 162 | if (ptr == NULL) { | - |
| 163 | DSOerror(DSO_R_NULL_HANDLE); | - |
| 164 | | - |
| 165 | | - |
| 166 | sk_void_push(dso->meth_data, ptr); | - |
| 167 | return (0); | - |
| 168 | } | - |
| 169 | | - |
| 170 | dlclose(ptr); | - |
| 171 | return (1); | - |
| 172 | } | - |
| 173 | | - |
| 174 | static void * | - |
| 175 | dlfcn_bind_var(DSO *dso, const char *symname) | - |
| 176 | { | - |
| 177 | void *ptr, *sym; | - |
| 178 | | - |
| 179 | if ((dso == NULL) || (symname == NULL)) { | - |
| 180 | DSOerror(ERR_R_PASSED_NULL_PARAMETER); | - |
| 181 | return (NULL); | - |
| 182 | } | - |
| 183 | if (sk_void_num(dso->meth_data) < 1) { | - |
| 184 | DSOerror(DSO_R_STACK_ERROR); | - |
| 185 | return (NULL); | - |
| 186 | } | - |
| 187 | ptr = sk_void_value(dso->meth_data, sk_void_num(dso->meth_data) - 1); | - |
| 188 | if (ptr == NULL) { | - |
| 189 | DSOerror(DSO_R_NULL_HANDLE); | - |
| 190 | return (NULL); | - |
| 191 | } | - |
| 192 | sym = dlsym(ptr, symname); | - |
| 193 | if (sym == NULL) { | - |
| 194 | DSOerror(DSO_R_SYM_FAILURE); | - |
| 195 | ERR_asprintf_error_data("symname(%s): %s", symname, dlerror()); | - |
| 196 | return (NULL); | - |
| 197 | } | - |
| 198 | return (sym); | - |
| 199 | } | - |
| 200 | | - |
| 201 | static DSO_FUNC_TYPE | - |
| 202 | dlfcn_bind_func(DSO *dso, const char *symname) | - |
| 203 | { | - |
| 204 | void *ptr; | - |
| 205 | union { | - |
| 206 | DSO_FUNC_TYPE sym; | - |
| 207 | void *dlret; | - |
| 208 | } u; | - |
| 209 | | - |
| 210 | if ((dso == NULL) || (symname == NULL)) { | - |
| 211 | DSOerror(ERR_R_PASSED_NULL_PARAMETER); | - |
| 212 | return (NULL); | - |
| 213 | } | - |
| 214 | if (sk_void_num(dso->meth_data) < 1) { | - |
| 215 | DSOerror(DSO_R_STACK_ERROR); | - |
| 216 | return (NULL); | - |
| 217 | } | - |
| 218 | ptr = sk_void_value(dso->meth_data, sk_void_num(dso->meth_data) - 1); | - |
| 219 | if (ptr == NULL) { | - |
| 220 | DSOerror(DSO_R_NULL_HANDLE); | - |
| 221 | return (NULL); | - |
| 222 | } | - |
| 223 | u.dlret = dlsym(ptr, symname); | - |
| 224 | if (u.dlret == NULL) { | - |
| 225 | DSOerror(DSO_R_SYM_FAILURE); | - |
| 226 | ERR_asprintf_error_data("symname(%s): %s", symname, dlerror()); | - |
| 227 | return (NULL); | - |
| 228 | } | - |
| 229 | return u.sym; | - |
| 230 | } | - |
| 231 | | - |
| 232 | static char * | - |
| 233 | dlfcn_merger(DSO *dso, const char *filespec1, const char *filespec2) | - |
| 234 | { | - |
| 235 | char *merged; | - |
| 236 | | - |
| 237 | if (!filespec1 && !filespec2) { | - |
| 238 | DSOerror(ERR_R_PASSED_NULL_PARAMETER); | - |
| 239 | return (NULL); | - |
| 240 | } | - |
| 241 | | - |
| 242 | | - |
| 243 | if (!filespec2 || (filespec1 != NULL && filespec1[0] == '/')) { | - |
| 244 | merged = strdup(filespec1); | - |
| 245 | if (!merged) { | - |
| 246 | DSOerror(ERR_R_MALLOC_FAILURE); | - |
| 247 | return (NULL); | - |
| 248 | } | - |
| 249 | } | - |
| 250 | | - |
| 251 | else if (!filespec1) { | - |
| 252 | merged = strdup(filespec2); | - |
| 253 | if (!merged) { | - |
| 254 | DSOerror(ERR_R_MALLOC_FAILURE); | - |
| 255 | return (NULL); | - |
| 256 | } | - |
| 257 | } else | - |
| 258 | | - |
| 259 | | - |
| 260 | | - |
| 261 | | - |
| 262 | | - |
| 263 | { | - |
| 264 | size_t spec2len, len; | - |
| 265 | | - |
| 266 | spec2len = strlen(filespec2); | - |
| 267 | len = spec2len + (filespec1 ? strlen(filespec1) : 0); | - |
| 268 | | - |
| 269 | if (filespec2 && filespec2[spec2len - 1] == '/') { | - |
| 270 | spec2len--; | - |
| 271 | len--; | - |
| 272 | } | - |
| 273 | merged = malloc(len + 2); | - |
| 274 | if (!merged) { | - |
| 275 | DSOerror(ERR_R_MALLOC_FAILURE); | - |
| 276 | return (NULL); | - |
| 277 | } | - |
| 278 | strlcpy(merged, filespec2, len + 2); | - |
| 279 | merged[spec2len] = '/'; | - |
| 280 | strlcpy(&merged[spec2len + 1], filespec1, len + 1 - spec2len); | - |
| 281 | } | - |
| 282 | return (merged); | - |
| 283 | } | - |
| 284 | | - |
| 285 | #define DSO_ext ".so" | - |
| 286 | #define DSO_extlen 3 | - |
| 287 | | - |
| 288 | static char * | - |
| 289 | dlfcn_name_converter(DSO *dso, const char *filename) | - |
| 290 | { | - |
| 291 | char *translated; | - |
| 292 | int ret; | - |
| 293 | | - |
| 294 | if (strchr(filename, '/') == NULL) { | - |
| 295 | | - |
| 296 | if ((DSO_flags(dso) & DSO_FLAG_NAME_TRANSLATION_EXT_ONLY) == 0) | - |
| 297 | ret = asprintf(&translated, "lib%s" DSO_ext, filename); | - |
| 298 | else | - |
| 299 | ret = asprintf(&translated, "%s" DSO_ext, filename); | - |
| 300 | if (ret == -1) | - |
| 301 | translated = NULL; | - |
| 302 | } else { | - |
| 303 | | - |
| 304 | translated = strdup(filename); | - |
| 305 | } | - |
| 306 | | - |
| 307 | if (translated == NULL) | - |
| 308 | DSOerror(DSO_R_NAME_TRANSLATION_FAILED); | - |
| 309 | return (translated); | - |
| 310 | } | - |
| 311 | | - |
| 312 | static int | - |
| 313 | dlfcn_pathbyaddr(void *addr, char *path, int sz) | - |
| 314 | { | - |
| 315 | Dl_info dli; | - |
| 316 | int len; | - |
| 317 | | - |
| 318 | if (addr == NULL) { | - |
| 319 | union{ | - |
| 320 | int(*f)(void*, char*, int); | - |
| 321 | void *p; | - |
| 322 | } t = { dlfcn_pathbyaddr }; | - |
| 323 | addr = t.p; | - |
| 324 | } | - |
| 325 | | - |
| 326 | if (dladdr(addr, &dli)) { | - |
| 327 | len = (int)strlen(dli.dli_fname); | - |
| 328 | if (sz <= 0) | - |
| 329 | return len + 1; | - |
| 330 | if (len >= sz) | - |
| 331 | len = sz - 1; | - |
| 332 | memcpy(path, dli.dli_fname, len); | - |
| 333 | path[len++] = 0; | - |
| 334 | return len; | - |
| 335 | } | - |
| 336 | | - |
| 337 | ERR_asprintf_error_data("dlfcn_pathbyaddr(): %s", dlerror()); | - |
| 338 | return -1; | - |
| 339 | } | - |
| 340 | | - |
| 341 | static void * | - |
| 342 | dlfcn_globallookup(const char *name) | - |
| 343 | { | - |
| 344 | void *ret = NULL, *handle = dlopen(NULL, RTLD_LAZY); | - |
| 345 | | - |
| 346 | if (handle) { | - |
| 347 | ret = dlsym(handle, name); | - |
| 348 | dlclose(handle); | - |
| 349 | } | - |
| 350 | | - |
| 351 | return ret; | - |
| 352 | } | - |
| 353 | #endif /* DSO_DLFCN */ | - |
| | |