No empty .Rs/.Re
[netbsd-mini2440.git] / crypto / dist / heimdal / lib / hx509 / ks_dir.c
blobea74203df61479e10c2d2ac44b48b2a178e16598
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 __RCSID("$Heimdal: ks_dir.c 19778 2007-01-09 10:52:13Z lha $"
36 "$NetBSD$");
37 #include <dirent.h>
40 * The DIR keyset module is strange compared to the other modules
41 * since it does lazy evaluation and really doesn't keep any local
42 * state except for the directory iteration and cert iteration of
43 * files. DIR ignores most errors so that the consumer doesn't get
44 * failes for stray files in directories.
47 struct dircursor {
48 DIR *dir;
49 hx509_certs certs;
50 void *iter;
57 static int
58 dir_init(hx509_context context,
59 hx509_certs certs, void **data, int flags,
60 const char *residue, hx509_lock lock)
62 *data = NULL;
65 struct stat sb;
66 int ret;
68 ret = stat(residue, &sb);
69 if (ret == -1) {
70 hx509_set_error_string(context, 0, ENOENT,
71 "No such file %s", residue);
72 return ENOENT;
75 if ((sb.st_mode & S_IFDIR) == 0) {
76 hx509_set_error_string(context, 0, ENOTDIR,
77 "%s is not a directory", residue);
78 return ENOTDIR;
82 *data = strdup(residue);
83 if (*data == NULL) {
84 hx509_clear_error_string(context);
85 return ENOMEM;
88 return 0;
91 static int
92 dir_free(hx509_certs certs, void *data)
94 free(data);
95 return 0;
100 static int
101 dir_iter_start(hx509_context context,
102 hx509_certs certs, void *data, void **cursor)
104 struct dircursor *d;
106 *cursor = NULL;
108 d = calloc(1, sizeof(*d));
109 if (d == NULL) {
110 hx509_clear_error_string(context);
111 return ENOMEM;
114 d->dir = opendir(data);
115 if (d->dir == NULL) {
116 hx509_clear_error_string(context);
117 free(d);
118 return errno;
120 d->certs = NULL;
121 d->iter = NULL;
123 *cursor = d;
124 return 0;
127 static int
128 dir_iter(hx509_context context,
129 hx509_certs certs, void *data, void *iter, hx509_cert *cert)
131 struct dircursor *d = iter;
132 int ret = 0;
134 *cert = NULL;
136 do {
137 struct dirent *dir;
138 char *fn;
140 if (d->certs) {
141 ret = hx509_certs_next_cert(context, d->certs, d->iter, cert);
142 if (ret) {
143 hx509_certs_end_seq(context, d->certs, d->iter);
144 d->iter = NULL;
145 hx509_certs_free(&d->certs);
146 return ret;
148 if (*cert) {
149 ret = 0;
150 break;
152 hx509_certs_end_seq(context, d->certs, d->iter);
153 d->iter = NULL;
154 hx509_certs_free(&d->certs);
157 dir = readdir(d->dir);
158 if (dir == NULL) {
159 ret = 0;
160 break;
162 if (strcmp(dir->d_name, ".") == 0 || strcmp(dir->d_name, "..") == 0)
163 continue;
165 if (asprintf(&fn, "FILE:%s/%s", (char *)data, dir->d_name) == -1)
166 return ENOMEM;
168 ret = hx509_certs_init(context, fn, 0, NULL, &d->certs);
169 if (ret == 0) {
171 ret = hx509_certs_start_seq(context, d->certs, &d->iter);
172 if (ret)
173 hx509_certs_free(&d->certs);
175 /* ignore errors */
176 if (ret) {
177 d->certs = NULL;
178 ret = 0;
181 free(fn);
182 } while(ret == 0);
184 return ret;
188 static int
189 dir_iter_end(hx509_context context,
190 hx509_certs certs,
191 void *data,
192 void *cursor)
194 struct dircursor *d = cursor;
196 if (d->certs) {
197 hx509_certs_end_seq(context, d->certs, d->iter);
198 d->iter = NULL;
199 hx509_certs_free(&d->certs);
201 closedir(d->dir);
202 free(d);
203 return 0;
207 static struct hx509_keyset_ops keyset_dir = {
208 "DIR",
210 dir_init,
211 NULL,
212 dir_free,
213 NULL,
214 NULL,
215 dir_iter_start,
216 dir_iter,
217 dir_iter_end
220 void
221 _hx509_ks_dir_register(hx509_context context)
223 _hx509_ks_register(context, &keyset_dir);