static int dir_iter_start(hx509_context context, hx509_certs certs, void *data, void **cursor) { struct dircursor *d; *cursor = NULL; d = calloc(1, sizeof(*d)); if (d == NULL) { hx509_clear_error_string(context); return ENOMEM; } d->dir = opendir(data); if (d->dir == NULL) { hx509_clear_error_string(context); free(d); return errno; } rk_cloexec_dir(d->dir); d->certs = NULL; d->iter = NULL; *cursor = d; return 0; }
void krb5_load_plugins(krb5_context context, const char *name, const char **paths) { #ifdef HAVE_DLOPEN heim_string_t s = heim_string_create(name); heim_dict_t module; struct dirent *entry; krb5_error_code ret; const char **di; DIR *d; HEIMDAL_MUTEX_lock(&plugin_mutex); if (modules == NULL) { modules = heim_dict_create(11); if (modules == NULL) { HEIMDAL_MUTEX_unlock(&plugin_mutex); return; } } module = heim_dict_copy_value(modules, s); if (module == NULL) { module = heim_dict_create(11); if (module == NULL) { HEIMDAL_MUTEX_unlock(&plugin_mutex); heim_release(s); return; } heim_dict_add_value(modules, s, module); } heim_release(s); for (di = paths; *di != NULL; di++) { d = opendir(*di); if (d == NULL) continue; rk_cloexec_dir(d); while ((entry = readdir(d)) != NULL) { char *n = entry->d_name; char *path = NULL; heim_string_t spath; struct plugin2 *p; /* skip . and .. */ if (n[0] == '.' && (n[1] == '\0' || (n[1] == '.' && n[2] == '\0'))) continue; ret = 0; #ifdef __APPLE__ { /* support loading bundles on MacOS */ size_t len = strlen(n); if (len > 7 && strcmp(&n[len - 7], ".bundle") == 0) ret = asprintf(&path, "%s/%s/Contents/MacOS/%.*s", *di, n, (int)(len - 7), n); } #endif if (ret < 0 || path == NULL) ret = asprintf(&path, "%s/%s", *di, n); if (ret < 0 || path == NULL) continue; spath = heim_string_create(n); if (spath == NULL) { free(path); continue; } /* check if already cached */ p = heim_dict_copy_value(module, spath); if (p == NULL) { p = heim_uniq_alloc(sizeof(*p), "krb5-plugin", plug_dealloc); if (p) p->dsohandle = dlopen(path, RTLD_LOCAL|RTLD_LAZY); if (p && p->dsohandle) { p->path = heim_retain(spath); p->names = heim_dict_create(11); heim_dict_add_value(module, spath, p); } } heim_release(spath); heim_release(p); free(path); } closedir(d); } heim_release(module); HEIMDAL_MUTEX_unlock(&plugin_mutex); #endif /* HAVE_DLOPEN */ }
static krb5_error_code load_plugins(krb5_context context) { struct plugin *e; krb5_error_code ret; char **dirs = NULL, **di; struct dirent *entry; char *path; DIR *d = NULL; if (!plugins_needs_scan) return 0; plugins_needs_scan = 0; #ifdef HAVE_DLOPEN dirs = krb5_config_get_strings(context, NULL, "libdefaults", "plugin_dir", NULL); if (dirs == NULL) dirs = rk_UNCONST(sysplugin_dirs); for (di = dirs; *di != NULL; di++) { char * dir = *di; #ifdef KRB5_USE_PATH_TOKENS if (_krb5_expand_path_tokens(context, *di, &dir)) goto next_dir; #endif trim_trailing_slash(dir); d = opendir(dir); if (d == NULL) goto next_dir; rk_cloexec_dir(d); while ((entry = readdir(d)) != NULL) { char *n = entry->d_name; /* skip . and .. */ if (!is_valid_plugin_filename(n)) continue; path = NULL; ret = 0; #ifdef __APPLE__ { /* support loading bundles on MacOS */ size_t len = strlen(n); if (len > 7 && strcmp(&n[len - 7], ".bundle") == 0) ret = asprintf(&path, "%s/%s/Contents/MacOS/%.*s", dir, n, (int)(len - 7), n); } #endif if (ret < 0 || path == NULL) ret = asprintf(&path, "%s/%s", dir, n); if (ret < 0 || path == NULL) { ret = ENOMEM; krb5_set_error_message(context, ret, "malloc: out of memory"); return ret; } /* check if already tried */ for (e = registered; e != NULL; e = e->next) if (e->type == DSO && strcmp(e->u.dso.path, path) == 0) break; if (e) { free(path); } else { loadlib(context, path); /* store or frees path */ } } closedir(d); next_dir: if (dir != *di) free(dir); } if (dirs != rk_UNCONST(sysplugin_dirs)) krb5_config_free_strings(dirs); #endif /* HAVE_DLOPEN */ return 0; }