test debug
[heimdal.git] / lib / hx509 / ks_dir.c
blob15d44982cc7405101c26ba2c9c74bca44f821f63
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("$Id$");
36 #include <dirent.h>
39 * The DIR keyset module is strange compared to the other modules
40 * since it does lazy evaluation and really doesn't keep any local
41 * state except for the directory iteration and cert iteration of
42 * files. DIR ignores most errors so that the consumer doesn't get
43 * failes for stray files in directories.
46 struct dircursor {
47 DIR *dir;
48 hx509_certs certs;
49 void *iter;
56 static int
57 dir_init(hx509_context context,
58 hx509_certs certs, void **data, int flags,
59 const char *residue, hx509_lock lock)
61 *data = NULL;
64 struct stat sb;
65 int ret;
67 ret = stat(residue, &sb);
68 if (ret == -1) {
69 hx509_set_error_string(context, 0, ENOENT,
70 "No such file %s", residue);
71 return ENOENT;
74 if ((sb.st_mode & S_IFDIR) == 0) {
75 hx509_set_error_string(context, 0, ENOTDIR,
76 "%s is not a directory", residue);
77 return ENOTDIR;
81 *data = strdup(residue);
82 if (*data == NULL) {
83 hx509_clear_error_string(context);
84 return ENOMEM;
87 return 0;
90 static int
91 dir_free(hx509_certs certs, void *data)
93 free(data);
94 return 0;
99 static int
100 dir_iter_start(hx509_context context,
101 hx509_certs certs, void *data, void **cursor)
103 struct dircursor *d;
105 *cursor = NULL;
107 d = calloc(1, sizeof(*d));
108 if (d == NULL) {
109 hx509_clear_error_string(context);
110 return ENOMEM;
113 d->dir = opendir(data);
114 if (d->dir == NULL) {
115 hx509_clear_error_string(context);
116 free(d);
117 return errno;
119 d->certs = NULL;
120 d->iter = NULL;
122 *cursor = d;
123 return 0;
126 static int
127 dir_iter(hx509_context context,
128 hx509_certs certs, void *data, void *iter, hx509_cert *cert)
130 struct dircursor *d = iter;
131 int ret = 0;
133 *cert = NULL;
135 do {
136 struct dirent *dir;
137 char *fn;
139 if (d->certs) {
140 ret = hx509_certs_next_cert(context, d->certs, d->iter, cert);
141 if (ret) {
142 hx509_certs_end_seq(context, d->certs, d->iter);
143 d->iter = NULL;
144 hx509_certs_free(&d->certs);
145 return ret;
147 if (*cert) {
148 ret = 0;
149 break;
151 hx509_certs_end_seq(context, d->certs, d->iter);
152 d->iter = NULL;
153 hx509_certs_free(&d->certs);
156 dir = readdir(d->dir);
157 if (dir == NULL) {
158 ret = 0;
159 break;
161 if (strcmp(dir->d_name, ".") == 0 || strcmp(dir->d_name, "..") == 0)
162 continue;
164 if (asprintf(&fn, "FILE:%s/%s", (char *)data, dir->d_name) == -1)
165 return ENOMEM;
167 ret = hx509_certs_init(context, fn, 0, NULL, &d->certs);
168 if (ret == 0) {
170 ret = hx509_certs_start_seq(context, d->certs, &d->iter);
171 if (ret)
172 hx509_certs_free(&d->certs);
174 /* ignore errors */
175 if (ret) {
176 d->certs = NULL;
177 ret = 0;
180 free(fn);
181 } while(ret == 0);
183 return ret;
187 static int
188 dir_iter_end(hx509_context context,
189 hx509_certs certs,
190 void *data,
191 void *cursor)
193 struct dircursor *d = cursor;
195 if (d->certs) {
196 hx509_certs_end_seq(context, d->certs, d->iter);
197 d->iter = NULL;
198 hx509_certs_free(&d->certs);
200 closedir(d->dir);
201 free(d);
202 return 0;
206 static struct hx509_keyset_ops keyset_dir = {
207 "DIR",
209 dir_init,
210 NULL,
211 dir_free,
212 NULL,
213 NULL,
214 dir_iter_start,
215 dir_iter,
216 dir_iter_end
219 void
220 _hx509_ks_dir_register(hx509_context context)
222 _hx509_ks_register(context, &keyset_dir);