/*1* Copyright (c) 2006 Kungliga Tekniska Högskolan2* (Royal Institute of Technology, Stockholm, Sweden).3* All rights reserved.4*5* Redistribution and use in source and binary forms, with or without6* modification, are permitted provided that the following conditions7* are met:8*9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11*12* 2. Redistributions in binary form must reproduce the above copyright13* notice, this list of conditions and the following disclaimer in the14* documentation and/or other materials provided with the distribution.15*16* 3. Neither the name of the Institute nor the names of its contributors17* may be used to endorse or promote products derived from this software18* without specific prior written permission.19*20* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND21* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE22* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE23* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE24* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL25* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS26* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)27* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT28* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY29* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF30* SUCH DAMAGE.31*/3233#include "hx_locl.h"34#include <dirent.h>3536/*37* The DIR keyset module is strange compared to the other modules38* since it does lazy evaluation and really doesn't keep any local39* state except for the directory iteration and cert iteration of40* files. DIR ignores most errors so that the consumer doesn't get41* failes for stray files in directories.42*/4344struct dircursor {45DIR *dir;46hx509_certs certs;47void *iter;48};4950/*51*52*/5354static int55dir_init(hx509_context context,56hx509_certs certs, void **data, int flags,57const char *residue, hx509_lock lock)58{59*data = NULL;6061{62struct stat sb;63int ret;6465ret = stat(residue, &sb);66if (ret == -1) {67hx509_set_error_string(context, 0, ENOENT,68"No such file %s", residue);69return ENOENT;70}7172if (!S_ISDIR(sb.st_mode)) {73hx509_set_error_string(context, 0, ENOTDIR,74"%s is not a directory", residue);75return ENOTDIR;76}77}7879*data = strdup(residue);80if (*data == NULL) {81hx509_clear_error_string(context);82return ENOMEM;83}8485return 0;86}8788static int89dir_free(hx509_certs certs, void *data)90{91free(data);92return 0;93}9495static int96dir_iter_start(hx509_context context,97hx509_certs certs, void *data, void **cursor)98{99struct dircursor *d;100101*cursor = NULL;102103d = calloc(1, sizeof(*d));104if (d == NULL) {105hx509_clear_error_string(context);106return ENOMEM;107}108109d->dir = opendir(data);110if (d->dir == NULL) {111hx509_clear_error_string(context);112free(d);113return errno;114}115rk_cloexec_dir(d->dir);116d->certs = NULL;117d->iter = NULL;118119*cursor = d;120return 0;121}122123static int124dir_iter(hx509_context context,125hx509_certs certs, void *data, void *iter, hx509_cert *cert)126{127struct dircursor *d = iter;128int ret = 0;129130*cert = NULL;131132do {133struct dirent *dir;134char *fn;135136if (d->certs) {137ret = hx509_certs_next_cert(context, d->certs, d->iter, cert);138if (ret) {139hx509_certs_end_seq(context, d->certs, d->iter);140d->iter = NULL;141hx509_certs_free(&d->certs);142return ret;143}144if (*cert) {145ret = 0;146break;147}148hx509_certs_end_seq(context, d->certs, d->iter);149d->iter = NULL;150hx509_certs_free(&d->certs);151}152153dir = readdir(d->dir);154if (dir == NULL) {155ret = 0;156break;157}158if (strcmp(dir->d_name, ".") == 0 || strcmp(dir->d_name, "..") == 0)159continue;160161if (asprintf(&fn, "FILE:%s/%s", (char *)data, dir->d_name) == -1)162return ENOMEM;163164ret = hx509_certs_init(context, fn, 0, NULL, &d->certs);165if (ret == 0) {166167ret = hx509_certs_start_seq(context, d->certs, &d->iter);168if (ret)169hx509_certs_free(&d->certs);170}171/* ignore errors */172if (ret) {173d->certs = NULL;174ret = 0;175}176177free(fn);178} while(ret == 0);179180return ret;181}182183184static int185dir_iter_end(hx509_context context,186hx509_certs certs,187void *data,188void *cursor)189{190struct dircursor *d = cursor;191192if (d->certs) {193hx509_certs_end_seq(context, d->certs, d->iter);194d->iter = NULL;195hx509_certs_free(&d->certs);196}197closedir(d->dir);198free(d);199return 0;200}201202203static struct hx509_keyset_ops keyset_dir = {204"DIR",2050,206dir_init,207NULL,208dir_free,209NULL,210NULL,211dir_iter_start,212dir_iter,213dir_iter_end214};215216void217_hx509_ks_dir_register(hx509_context context)218{219_hx509_ks_register(context, &keyset_dir);220}221222223