2 * Copyright (c) 2006 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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
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.
56 dir_init(hx509_context context
,
57 hx509_certs certs
, void **data
, int flags
,
58 const char *residue
, hx509_lock lock
)
66 ret
= stat(residue
, &sb
);
68 hx509_set_error_string(context
, 0, ENOENT
,
69 "No such file %s", residue
);
73 if (!S_ISDIR(sb
.st_mode
)) {
74 hx509_set_error_string(context
, 0, ENOTDIR
,
75 "%s is not a directory", residue
);
80 *data
= strdup(residue
);
82 hx509_clear_error_string(context
);
90 dir_free(hx509_certs certs
, void *data
)
97 dir_iter_start(hx509_context context
,
98 hx509_certs certs
, void *data
, void **cursor
)
104 d
= calloc(1, sizeof(*d
));
106 hx509_clear_error_string(context
);
110 d
->dir
= opendir(data
);
111 if (d
->dir
== NULL
) {
112 hx509_clear_error_string(context
);
116 rk_cloexec_dir(d
->dir
);
125 dir_iter(hx509_context context
,
126 hx509_certs certs
, void *data
, void *iter
, hx509_cert
*cert
)
128 struct dircursor
*d
= iter
;
138 ret
= hx509_certs_next_cert(context
, d
->certs
, d
->iter
, cert
);
140 hx509_certs_end_seq(context
, d
->certs
, d
->iter
);
142 hx509_certs_free(&d
->certs
);
149 hx509_certs_end_seq(context
, d
->certs
, d
->iter
);
151 hx509_certs_free(&d
->certs
);
154 dir
= readdir(d
->dir
);
159 if (strcmp(dir
->d_name
, ".") == 0 || strcmp(dir
->d_name
, "..") == 0)
162 if (asprintf(&fn
, "FILE:%s/%s", (char *)data
, dir
->d_name
) == -1)
165 ret
= hx509_certs_init(context
, fn
, 0, NULL
, &d
->certs
);
168 ret
= hx509_certs_start_seq(context
, d
->certs
, &d
->iter
);
170 hx509_certs_free(&d
->certs
);
186 dir_iter_end(hx509_context context
,
191 struct dircursor
*d
= cursor
;
194 hx509_certs_end_seq(context
, d
->certs
, d
->iter
);
196 hx509_certs_free(&d
->certs
);
204 static struct hx509_keyset_ops keyset_dir
= {
218 _hx509_ks_dir_register(hx509_context context
)
220 _hx509_ks_register(context
, &keyset_dir
);