cf/largefile.m4: Fix build with autoconf-2.72
[heimdal.git] / lib / hx509 / ks_dir.c
blob3bc99f2dc6cf64039c281ed954479f445f445e32
1 /*
2 * Copyright (c) 2006 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
34 #include "hx_locl.h"
35 #include <dirent.h>
38 * The DIR keyset module is strange compared to the other modules
39 * since it does lazy evaluation and really doesn't keep any local
40 * state except for the directory iteration and cert iteration of
41 * files. DIR ignores most errors so that the consumer doesn't get
42 * failes for stray files in directories.
45 struct dircursor {
46 DIR *dir;
47 hx509_certs certs;
48 void *iter;
55 static int
56 dir_init(hx509_context context,
57 hx509_certs certs, void **data, int flags,
58 const char *residue, hx509_lock lock)
60 *data = NULL;
62 if (residue == NULL || residue[0] == '\0') {
63 hx509_set_error_string(context, 0, EINVAL,
64 "DIR file name not specified");
65 return EINVAL;
69 struct stat sb;
70 int ret;
72 ret = stat(residue, &sb);
73 if (ret == -1) {
74 hx509_set_error_string(context, 0, ENOENT,
75 "No such file %s", residue);
76 return ENOENT;
79 if (!S_ISDIR(sb.st_mode)) {
80 hx509_set_error_string(context, 0, ENOTDIR,
81 "%s is not a directory", residue);
82 return ENOTDIR;
86 *data = strdup(residue);
87 if (*data == NULL) {
88 hx509_clear_error_string(context);
89 return ENOMEM;
92 return 0;
95 static int
96 dir_free(hx509_certs certs, void *data)
98 free(data);
99 return 0;
102 static int
103 dir_iter_start(hx509_context context,
104 hx509_certs certs, void *data, void **cursor)
106 struct dircursor *d;
108 *cursor = NULL;
110 d = calloc(1, sizeof(*d));
111 if (d == NULL) {
112 hx509_clear_error_string(context);
113 return ENOMEM;
116 d->dir = opendir(data);
117 if (d->dir == NULL) {
118 hx509_clear_error_string(context);
119 free(d);
120 return errno;
122 rk_cloexec_dir(d->dir);
123 d->certs = NULL;
124 d->iter = NULL;
126 *cursor = d;
127 return 0;
130 static int
131 dir_iter(hx509_context context,
132 hx509_certs certs, void *data, void *iter, hx509_cert *cert)
134 struct dircursor *d = iter;
135 int ret = 0;
137 *cert = NULL;
139 do {
140 struct dirent *dir;
141 char *fn;
143 if (d->certs) {
144 ret = hx509_certs_next_cert(context, d->certs, d->iter, cert);
145 if (ret) {
146 hx509_certs_end_seq(context, d->certs, d->iter);
147 d->iter = NULL;
148 hx509_certs_free(&d->certs);
149 return ret;
151 if (*cert) {
152 ret = 0;
153 break;
155 hx509_certs_end_seq(context, d->certs, d->iter);
156 d->iter = NULL;
157 hx509_certs_free(&d->certs);
160 dir = readdir(d->dir);
161 if (dir == NULL) {
162 ret = 0;
163 break;
165 if (strcmp(dir->d_name, ".") == 0 || strcmp(dir->d_name, "..") == 0)
166 continue;
168 if (asprintf(&fn, "FILE:%s/%s", (char *)data, dir->d_name) == -1)
169 return ENOMEM;
171 ret = hx509_certs_init(context, fn, 0, NULL, &d->certs);
172 if (ret == 0) {
174 ret = hx509_certs_start_seq(context, d->certs, &d->iter);
175 if (ret)
176 hx509_certs_free(&d->certs);
178 /* ignore errors */
179 if (ret) {
180 d->certs = NULL;
181 ret = 0;
184 free(fn);
185 } while(ret == 0);
187 return ret;
191 static int
192 dir_iter_end(hx509_context context,
193 hx509_certs certs,
194 void *data,
195 void *cursor)
197 struct dircursor *d = cursor;
199 if (d->certs) {
200 hx509_certs_end_seq(context, d->certs, d->iter);
201 d->iter = NULL;
202 hx509_certs_free(&d->certs);
204 closedir(d->dir);
205 free(d);
206 return 0;
210 static struct hx509_keyset_ops keyset_dir = {
211 "DIR",
213 dir_init,
214 NULL,
215 dir_free,
216 NULL,
217 NULL,
218 dir_iter_start,
219 dir_iter,
220 dir_iter_end,
221 NULL,
222 NULL,
223 NULL,
224 NULL
227 HX509_LIB_FUNCTION void HX509_LIB_CALL
228 _hx509_ks_dir_register(hx509_context context)
230 _hx509_ks_register(context, &keyset_dir);