Timo Teras | 8f6f144 | 2015-05-07 12:48:47 -0400 | [diff] [blame] | 1 | /* |
Rich Salz | 846e33c | 2016-05-17 14:18:30 -0400 | [diff] [blame] | 2 | * Copyright 2015-2016 The OpenSSL Project Authors. All Rights Reserved. |
| 3 | * |
| 4 | * Licensed under the OpenSSL license (the "License"). You may not use |
| 5 | * this file except in compliance with the License. You can obtain a copy |
| 6 | * in the file LICENSE in the source distribution or at |
| 7 | * https://www.openssl.org/source/license.html |
| 8 | */ |
| 9 | |
| 10 | /* |
Timo Teras | 8f6f144 | 2015-05-07 12:48:47 -0400 | [diff] [blame] | 11 | * C implementation based on the original Perl and shell versions |
| 12 | * |
| 13 | * Copyright (c) 2013-2014 Timo Teräs <timo.teras@iki.fi> |
Timo Teras | 8f6f144 | 2015-05-07 12:48:47 -0400 | [diff] [blame] | 14 | */ |
Timo Teras | 8f6f144 | 2015-05-07 12:48:47 -0400 | [diff] [blame] | 15 | |
| 16 | #include "apps.h" |
| 17 | |
Richard Levitte | 6616429 | 2016-06-02 20:44:11 +0200 | [diff] [blame] | 18 | #if defined(OPENSSL_SYS_UNIX) || defined(__APPLE__) || \ |
Richard Levitte | 5c80e2a | 2017-02-22 16:48:55 +0100 | [diff] [blame] | 19 | (defined(__VMS) && defined(__DECC) && __CRTL_VER >= 80300000) |
Timo Teras | 8f6f144 | 2015-05-07 12:48:47 -0400 | [diff] [blame] | 20 | # include <unistd.h> |
| 21 | # include <stdio.h> |
| 22 | # include <limits.h> |
| 23 | # include <errno.h> |
| 24 | # include <string.h> |
| 25 | # include <ctype.h> |
| 26 | # include <sys/stat.h> |
| 27 | |
Richard Levitte | 2ac915f | 2017-02-23 13:45:00 +0100 | [diff] [blame] | 28 | /* |
| 29 | * Make sure that the processing of symbol names is treated the same as when |
| 30 | * libcrypto is built. This is done automatically for public headers (see |
| 31 | * include/openssl/__DECC_INCLUDE_PROLOGUE.H and __DECC_INCLUDE_EPILOGUE.H), |
| 32 | * but not for internal headers. |
| 33 | */ |
| 34 | # ifdef __VMS |
| 35 | # pragma names save |
| 36 | # pragma names as_is,shortened |
| 37 | # endif |
| 38 | |
Timo Teras | 8f6f144 | 2015-05-07 12:48:47 -0400 | [diff] [blame] | 39 | # include "internal/o_dir.h" |
Richard Levitte | 2ac915f | 2017-02-23 13:45:00 +0100 | [diff] [blame] | 40 | |
| 41 | # ifdef __VMS |
| 42 | # pragma names restore |
| 43 | # endif |
| 44 | |
Timo Teras | 8f6f144 | 2015-05-07 12:48:47 -0400 | [diff] [blame] | 45 | # include <openssl/evp.h> |
| 46 | # include <openssl/pem.h> |
| 47 | # include <openssl/x509.h> |
| 48 | |
| 49 | |
Richard Levitte | 6616429 | 2016-06-02 20:44:11 +0200 | [diff] [blame] | 50 | # ifndef PATH_MAX |
| 51 | # define PATH_MAX 4096 |
| 52 | # endif |
Alessandro Ghedini | c9c84a1 | 2015-09-25 13:51:27 -0400 | [diff] [blame] | 53 | # ifndef NAME_MAX |
| 54 | # define NAME_MAX 255 |
| 55 | # endif |
Timo Teras | 8f6f144 | 2015-05-07 12:48:47 -0400 | [diff] [blame] | 56 | # define MAX_COLLISIONS 256 |
| 57 | |
| 58 | typedef struct hentry_st { |
| 59 | struct hentry_st *next; |
| 60 | char *filename; |
| 61 | unsigned short old_id; |
| 62 | unsigned char need_symlink; |
| 63 | unsigned char digest[EVP_MAX_MD_SIZE]; |
| 64 | } HENTRY; |
| 65 | |
| 66 | typedef struct bucket_st { |
| 67 | struct bucket_st *next; |
| 68 | HENTRY *first_entry, *last_entry; |
| 69 | unsigned int hash; |
| 70 | unsigned short type; |
| 71 | unsigned short num_needed; |
| 72 | } BUCKET; |
| 73 | |
| 74 | enum Type { |
| 75 | /* Keep in sync with |suffixes|, below. */ |
| 76 | TYPE_CERT=0, TYPE_CRL=1 |
| 77 | }; |
| 78 | |
| 79 | enum Hash { |
| 80 | HASH_OLD, HASH_NEW, HASH_BOTH |
| 81 | }; |
| 82 | |
| 83 | |
| 84 | static int evpmdsize; |
| 85 | static const EVP_MD *evpmd; |
| 86 | static int remove_links = 1; |
| 87 | static int verbose = 0; |
| 88 | static BUCKET *hash_table[257]; |
| 89 | |
| 90 | static const char *suffixes[] = { "", "r" }; |
| 91 | static const char *extensions[] = { "pem", "crt", "cer", "crl" }; |
| 92 | |
| 93 | |
| 94 | static void bit_set(unsigned char *set, unsigned int bit) |
| 95 | { |
| 96 | set[bit >> 3] |= 1 << (bit & 0x7); |
| 97 | } |
| 98 | |
| 99 | static int bit_isset(unsigned char *set, unsigned int bit) |
| 100 | { |
| 101 | return set[bit >> 3] & (1 << (bit & 0x7)); |
| 102 | } |
| 103 | |
| 104 | |
Rich Salz | ce249fa | 2015-09-07 10:08:14 -0400 | [diff] [blame] | 105 | /* |
| 106 | * Process an entry; return number of errors. |
| 107 | */ |
| 108 | static int add_entry(enum Type type, unsigned int hash, const char *filename, |
Timo Teras | 8f6f144 | 2015-05-07 12:48:47 -0400 | [diff] [blame] | 109 | const unsigned char *digest, int need_symlink, |
| 110 | unsigned short old_id) |
| 111 | { |
| 112 | static BUCKET nilbucket; |
| 113 | static HENTRY nilhentry; |
| 114 | BUCKET *bp; |
| 115 | HENTRY *ep, *found = NULL; |
| 116 | unsigned int ndx = (type + hash) % OSSL_NELEM(hash_table); |
| 117 | |
| 118 | for (bp = hash_table[ndx]; bp; bp = bp->next) |
| 119 | if (bp->type == type && bp->hash == hash) |
| 120 | break; |
| 121 | if (bp == NULL) { |
| 122 | bp = app_malloc(sizeof(*bp), "hash bucket"); |
| 123 | *bp = nilbucket; |
| 124 | bp->next = hash_table[ndx]; |
| 125 | bp->type = type; |
| 126 | bp->hash = hash; |
| 127 | hash_table[ndx] = bp; |
| 128 | } |
| 129 | |
| 130 | for (ep = bp->first_entry; ep; ep = ep->next) { |
| 131 | if (digest && memcmp(digest, ep->digest, evpmdsize) == 0) { |
| 132 | BIO_printf(bio_err, |
Dr. Stephen Henson | f22ff0e | 2016-07-21 16:23:48 +0100 | [diff] [blame] | 133 | "%s: skipping duplicate %s in %s\n", opt_getprog(), |
| 134 | type == TYPE_CERT ? "certificate" : "CRL", filename); |
Rich Salz | ce249fa | 2015-09-07 10:08:14 -0400 | [diff] [blame] | 135 | return 1; |
Timo Teras | 8f6f144 | 2015-05-07 12:48:47 -0400 | [diff] [blame] | 136 | } |
| 137 | if (strcmp(filename, ep->filename) == 0) { |
| 138 | found = ep; |
| 139 | if (digest == NULL) |
| 140 | break; |
| 141 | } |
| 142 | } |
| 143 | ep = found; |
| 144 | if (ep == NULL) { |
Rich Salz | ce249fa | 2015-09-07 10:08:14 -0400 | [diff] [blame] | 145 | if (bp->num_needed >= MAX_COLLISIONS) { |
| 146 | BIO_printf(bio_err, |
| 147 | "%s: hash table overflow for %s\n", |
| 148 | opt_getprog(), filename); |
| 149 | return 1; |
| 150 | } |
Timo Teras | 8f6f144 | 2015-05-07 12:48:47 -0400 | [diff] [blame] | 151 | ep = app_malloc(sizeof(*ep), "collision bucket"); |
| 152 | *ep = nilhentry; |
| 153 | ep->old_id = ~0; |
Rich Salz | 7644a9a | 2015-12-16 16:12:24 -0500 | [diff] [blame] | 154 | ep->filename = OPENSSL_strdup(filename); |
Timo Teras | 8f6f144 | 2015-05-07 12:48:47 -0400 | [diff] [blame] | 155 | if (bp->last_entry) |
| 156 | bp->last_entry->next = ep; |
| 157 | if (bp->first_entry == NULL) |
| 158 | bp->first_entry = ep; |
| 159 | bp->last_entry = ep; |
| 160 | } |
| 161 | |
| 162 | if (old_id < ep->old_id) |
| 163 | ep->old_id = old_id; |
| 164 | if (need_symlink && !ep->need_symlink) { |
| 165 | ep->need_symlink = 1; |
| 166 | bp->num_needed++; |
| 167 | memcpy(ep->digest, digest, evpmdsize); |
| 168 | } |
Rich Salz | ce249fa | 2015-09-07 10:08:14 -0400 | [diff] [blame] | 169 | return 0; |
Timo Teras | 8f6f144 | 2015-05-07 12:48:47 -0400 | [diff] [blame] | 170 | } |
| 171 | |
Rich Salz | ce249fa | 2015-09-07 10:08:14 -0400 | [diff] [blame] | 172 | /* |
| 173 | * Check if a symlink goes to the right spot; return 0 if okay. |
| 174 | * This can be -1 if bad filename, or an error count. |
| 175 | */ |
Timo Teras | 8f6f144 | 2015-05-07 12:48:47 -0400 | [diff] [blame] | 176 | static int handle_symlink(const char *filename, const char *fullpath) |
| 177 | { |
| 178 | unsigned int hash = 0; |
| 179 | int i, type, id; |
| 180 | unsigned char ch; |
Vladimir Kotal | e7a6898 | 2015-09-21 21:07:28 +0200 | [diff] [blame] | 181 | char linktarget[PATH_MAX], *endptr; |
Richard Levitte | 6616429 | 2016-06-02 20:44:11 +0200 | [diff] [blame] | 182 | ossl_ssize_t n; |
Timo Teras | 8f6f144 | 2015-05-07 12:48:47 -0400 | [diff] [blame] | 183 | |
| 184 | for (i = 0; i < 8; i++) { |
| 185 | ch = filename[i]; |
| 186 | if (!isxdigit(ch)) |
| 187 | return -1; |
| 188 | hash <<= 4; |
Rich Salz | 14f051a | 2016-04-13 15:58:28 -0400 | [diff] [blame] | 189 | hash += OPENSSL_hexchar2int(ch); |
Timo Teras | 8f6f144 | 2015-05-07 12:48:47 -0400 | [diff] [blame] | 190 | } |
| 191 | if (filename[i++] != '.') |
| 192 | return -1; |
Dr. Stephen Henson | ee8f785 | 2016-07-22 01:09:04 +0100 | [diff] [blame] | 193 | for (type = OSSL_NELEM(suffixes) - 1; type > 0; type--) { |
| 194 | const char *suffix = suffixes[type]; |
| 195 | if (strncasecmp(suffix, &filename[i], strlen(suffix)) == 0) |
Timo Teras | 8f6f144 | 2015-05-07 12:48:47 -0400 | [diff] [blame] | 196 | break; |
Dr. Stephen Henson | ee8f785 | 2016-07-22 01:09:04 +0100 | [diff] [blame] | 197 | } |
Timo Teras | 8f6f144 | 2015-05-07 12:48:47 -0400 | [diff] [blame] | 198 | i += strlen(suffixes[type]); |
| 199 | |
| 200 | id = strtoul(&filename[i], &endptr, 10); |
| 201 | if (*endptr != '\0') |
| 202 | return -1; |
| 203 | |
| 204 | n = readlink(fullpath, linktarget, sizeof(linktarget)); |
| 205 | if (n < 0 || n >= (int)sizeof(linktarget)) |
| 206 | return -1; |
| 207 | linktarget[n] = 0; |
| 208 | |
Rich Salz | ce249fa | 2015-09-07 10:08:14 -0400 | [diff] [blame] | 209 | return add_entry(type, hash, linktarget, NULL, 0, id); |
Timo Teras | 8f6f144 | 2015-05-07 12:48:47 -0400 | [diff] [blame] | 210 | } |
| 211 | |
Rich Salz | ce249fa | 2015-09-07 10:08:14 -0400 | [diff] [blame] | 212 | /* |
| 213 | * process a file, return number of errors. |
| 214 | */ |
Timo Teras | 8f6f144 | 2015-05-07 12:48:47 -0400 | [diff] [blame] | 215 | static int do_file(const char *filename, const char *fullpath, enum Hash h) |
| 216 | { |
Rich Salz | ce249fa | 2015-09-07 10:08:14 -0400 | [diff] [blame] | 217 | STACK_OF (X509_INFO) *inf = NULL; |
Timo Teras | 8f6f144 | 2015-05-07 12:48:47 -0400 | [diff] [blame] | 218 | X509_INFO *x; |
| 219 | X509_NAME *name = NULL; |
| 220 | BIO *b; |
| 221 | const char *ext; |
| 222 | unsigned char digest[EVP_MAX_MD_SIZE]; |
Ben Laurie | 8c82de9 | 2015-09-07 18:33:09 +0100 | [diff] [blame] | 223 | int type, errs = 0; |
| 224 | size_t i; |
Timo Teras | 8f6f144 | 2015-05-07 12:48:47 -0400 | [diff] [blame] | 225 | |
Rich Salz | ce249fa | 2015-09-07 10:08:14 -0400 | [diff] [blame] | 226 | /* Does it end with a recognized extension? */ |
Timo Teras | 8f6f144 | 2015-05-07 12:48:47 -0400 | [diff] [blame] | 227 | if ((ext = strrchr(filename, '.')) == NULL) |
Rich Salz | ce249fa | 2015-09-07 10:08:14 -0400 | [diff] [blame] | 228 | goto end; |
Ben Laurie | 8c82de9 | 2015-09-07 18:33:09 +0100 | [diff] [blame] | 229 | for (i = 0; i < OSSL_NELEM(extensions); i++) { |
Timo Teras | 8f6f144 | 2015-05-07 12:48:47 -0400 | [diff] [blame] | 230 | if (strcasecmp(extensions[i], ext + 1) == 0) |
| 231 | break; |
| 232 | } |
Ben Laurie | 8c82de9 | 2015-09-07 18:33:09 +0100 | [diff] [blame] | 233 | if (i >= OSSL_NELEM(extensions)) |
Rich Salz | ce249fa | 2015-09-07 10:08:14 -0400 | [diff] [blame] | 234 | goto end; |
Timo Teras | 8f6f144 | 2015-05-07 12:48:47 -0400 | [diff] [blame] | 235 | |
Rich Salz | ce249fa | 2015-09-07 10:08:14 -0400 | [diff] [blame] | 236 | /* Does it have X.509 data in it? */ |
| 237 | if ((b = BIO_new_file(fullpath, "r")) == NULL) { |
| 238 | BIO_printf(bio_err, "%s: skipping %s, cannot open file\n", |
| 239 | opt_getprog(), filename); |
| 240 | errs++; |
| 241 | goto end; |
| 242 | } |
Timo Teras | 8f6f144 | 2015-05-07 12:48:47 -0400 | [diff] [blame] | 243 | inf = PEM_X509_INFO_read_bio(b, NULL, NULL, NULL); |
| 244 | BIO_free(b); |
| 245 | if (inf == NULL) |
Rich Salz | ce249fa | 2015-09-07 10:08:14 -0400 | [diff] [blame] | 246 | goto end; |
Timo Teras | 8f6f144 | 2015-05-07 12:48:47 -0400 | [diff] [blame] | 247 | |
| 248 | if (sk_X509_INFO_num(inf) != 1) { |
| 249 | BIO_printf(bio_err, |
| 250 | "%s: skipping %s," |
| 251 | "it does not contain exactly one certificate or CRL\n", |
| 252 | opt_getprog(), filename); |
Rich Salz | ce249fa | 2015-09-07 10:08:14 -0400 | [diff] [blame] | 253 | /* This is not an error. */ |
Timo Teras | 8f6f144 | 2015-05-07 12:48:47 -0400 | [diff] [blame] | 254 | goto end; |
| 255 | } |
| 256 | x = sk_X509_INFO_value(inf, 0); |
| 257 | if (x->x509) { |
| 258 | type = TYPE_CERT; |
| 259 | name = X509_get_subject_name(x->x509); |
| 260 | X509_digest(x->x509, evpmd, digest, NULL); |
| 261 | } else if (x->crl) { |
| 262 | type = TYPE_CRL; |
| 263 | name = X509_CRL_get_issuer(x->crl); |
| 264 | X509_CRL_digest(x->crl, evpmd, digest, NULL); |
Ben Laurie | 8c82de9 | 2015-09-07 18:33:09 +0100 | [diff] [blame] | 265 | } else { |
| 266 | ++errs; |
| 267 | goto end; |
Timo Teras | 8f6f144 | 2015-05-07 12:48:47 -0400 | [diff] [blame] | 268 | } |
| 269 | if (name) { |
| 270 | if ((h == HASH_NEW) || (h == HASH_BOTH)) |
Rich Salz | ce249fa | 2015-09-07 10:08:14 -0400 | [diff] [blame] | 271 | errs += add_entry(type, X509_NAME_hash(name), filename, digest, 1, ~0); |
Timo Teras | 8f6f144 | 2015-05-07 12:48:47 -0400 | [diff] [blame] | 272 | if ((h == HASH_OLD) || (h == HASH_BOTH)) |
Rich Salz | ce249fa | 2015-09-07 10:08:14 -0400 | [diff] [blame] | 273 | errs += add_entry(type, X509_NAME_hash_old(name), filename, digest, 1, ~0); |
Timo Teras | 8f6f144 | 2015-05-07 12:48:47 -0400 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | end: |
| 277 | sk_X509_INFO_pop_free(inf, X509_INFO_free); |
Rich Salz | ce249fa | 2015-09-07 10:08:14 -0400 | [diff] [blame] | 278 | return errs; |
Timo Teras | 8f6f144 | 2015-05-07 12:48:47 -0400 | [diff] [blame] | 279 | } |
| 280 | |
Rich Salz | b1ffe8d | 2016-05-25 08:59:10 -0400 | [diff] [blame] | 281 | static void str_free(char *s) |
| 282 | { |
| 283 | OPENSSL_free(s); |
| 284 | } |
| 285 | |
Richard Levitte | 5c80e2a | 2017-02-22 16:48:55 +0100 | [diff] [blame] | 286 | static int ends_with_dirsep(const char *path) |
| 287 | { |
| 288 | if (*path != '\0') |
| 289 | path += strlen(path) - 1; |
Richard Levitte | 46958a0 | 2017-02-23 14:41:20 +0100 | [diff] [blame] | 290 | # if defined __VMS |
Richard Levitte | 5c80e2a | 2017-02-22 16:48:55 +0100 | [diff] [blame] | 291 | if (*path == ']' || *path == '>' || *path == ':') |
| 292 | return 1; |
Richard Levitte | 46958a0 | 2017-02-23 14:41:20 +0100 | [diff] [blame] | 293 | # elif defined _WIN32 |
Richard Levitte | 5c80e2a | 2017-02-22 16:48:55 +0100 | [diff] [blame] | 294 | if (*path == '\\') |
| 295 | return 1; |
| 296 | # endif |
| 297 | return *path == '/'; |
| 298 | } |
| 299 | |
Richard Levitte | 39aceac | 2017-02-23 01:45:04 +0100 | [diff] [blame] | 300 | static int massage_filename(char *name) |
| 301 | { |
| 302 | # ifdef __VMS |
| 303 | char *p = strchr(name, ';'); |
| 304 | char *q = p; |
| 305 | |
| 306 | if (q != NULL) { |
| 307 | for (q++; *q != '\0'; q++) { |
| 308 | if (!isdigit(*q)) |
| 309 | return 1; |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | *p = '\0'; |
| 314 | # endif |
| 315 | return 1; |
| 316 | } |
| 317 | |
Rich Salz | ce249fa | 2015-09-07 10:08:14 -0400 | [diff] [blame] | 318 | /* |
| 319 | * Process a directory; return number of errors found. |
| 320 | */ |
Timo Teras | 8f6f144 | 2015-05-07 12:48:47 -0400 | [diff] [blame] | 321 | static int do_dir(const char *dirname, enum Hash h) |
| 322 | { |
| 323 | BUCKET *bp, *nextbp; |
| 324 | HENTRY *ep, *nextep; |
| 325 | OPENSSL_DIR_CTX *d = NULL; |
| 326 | struct stat st; |
| 327 | unsigned char idmask[MAX_COLLISIONS / 8]; |
Rich Salz | b1ffe8d | 2016-05-25 08:59:10 -0400 | [diff] [blame] | 328 | int n, numfiles, nextid, buflen, errs = 0; |
Ben Laurie | 8c82de9 | 2015-09-07 18:33:09 +0100 | [diff] [blame] | 329 | size_t i; |
Timo Teras | 8f6f144 | 2015-05-07 12:48:47 -0400 | [diff] [blame] | 330 | const char *pathsep; |
| 331 | const char *filename; |
Rich Salz | b1ffe8d | 2016-05-25 08:59:10 -0400 | [diff] [blame] | 332 | char *buf, *copy; |
| 333 | STACK_OF(OPENSSL_STRING) *files = NULL; |
Timo Teras | 8f6f144 | 2015-05-07 12:48:47 -0400 | [diff] [blame] | 334 | |
Rich Salz | ff2f6bb | 2015-09-07 22:21:38 -0400 | [diff] [blame] | 335 | if (app_access(dirname, W_OK) < 0) { |
| 336 | BIO_printf(bio_err, "Skipping %s, can't write\n", dirname); |
Rich Salz | 4c7103a | 2015-09-10 11:46:13 -0400 | [diff] [blame] | 337 | return 1; |
Rich Salz | ff2f6bb | 2015-09-07 22:21:38 -0400 | [diff] [blame] | 338 | } |
Timo Teras | 8f6f144 | 2015-05-07 12:48:47 -0400 | [diff] [blame] | 339 | buflen = strlen(dirname); |
Richard Levitte | 5c80e2a | 2017-02-22 16:48:55 +0100 | [diff] [blame] | 340 | pathsep = (buflen && !ends_with_dirsep(dirname)) ? "/": ""; |
Alessandro Ghedini | c9c84a1 | 2015-09-25 13:51:27 -0400 | [diff] [blame] | 341 | buflen += NAME_MAX + 1 + 1; |
| 342 | buf = app_malloc(buflen, "filename buffer"); |
Timo Teras | 8f6f144 | 2015-05-07 12:48:47 -0400 | [diff] [blame] | 343 | |
| 344 | if (verbose) |
| 345 | BIO_printf(bio_out, "Doing %s\n", dirname); |
| 346 | |
Rich Salz | b1ffe8d | 2016-05-25 08:59:10 -0400 | [diff] [blame] | 347 | if ((files = sk_OPENSSL_STRING_new_null()) == NULL) { |
| 348 | BIO_printf(bio_err, "Skipping %s, out of memory\n", dirname); |
| 349 | exit(1); |
| 350 | } |
Timo Teras | 8f6f144 | 2015-05-07 12:48:47 -0400 | [diff] [blame] | 351 | while ((filename = OPENSSL_DIR_read(&d, dirname)) != NULL) { |
Rich Salz | b1ffe8d | 2016-05-25 08:59:10 -0400 | [diff] [blame] | 352 | if ((copy = strdup(filename)) == NULL |
Richard Levitte | 39aceac | 2017-02-23 01:45:04 +0100 | [diff] [blame] | 353 | || !massage_filename(copy) |
Rich Salz | b1ffe8d | 2016-05-25 08:59:10 -0400 | [diff] [blame] | 354 | || sk_OPENSSL_STRING_push(files, copy) == 0) { |
| 355 | BIO_puts(bio_err, "out of memory\n"); |
| 356 | exit(1); |
| 357 | } |
| 358 | } |
| 359 | OPENSSL_DIR_end(&d); |
| 360 | sk_OPENSSL_STRING_sort(files); |
| 361 | |
| 362 | numfiles = sk_OPENSSL_STRING_num(files); |
| 363 | for (n = 0; n < numfiles; ++n) { |
| 364 | filename = sk_OPENSSL_STRING_value(files, n); |
Timo Teras | 8f6f144 | 2015-05-07 12:48:47 -0400 | [diff] [blame] | 365 | if (snprintf(buf, buflen, "%s%s%s", |
| 366 | dirname, pathsep, filename) >= buflen) |
| 367 | continue; |
| 368 | if (lstat(buf, &st) < 0) |
| 369 | continue; |
| 370 | if (S_ISLNK(st.st_mode) && handle_symlink(filename, buf) == 0) |
| 371 | continue; |
Rich Salz | ce249fa | 2015-09-07 10:08:14 -0400 | [diff] [blame] | 372 | errs += do_file(filename, buf, h); |
Timo Teras | 8f6f144 | 2015-05-07 12:48:47 -0400 | [diff] [blame] | 373 | } |
Rich Salz | b1ffe8d | 2016-05-25 08:59:10 -0400 | [diff] [blame] | 374 | sk_OPENSSL_STRING_pop_free(files, str_free); |
Timo Teras | 8f6f144 | 2015-05-07 12:48:47 -0400 | [diff] [blame] | 375 | |
Ben Laurie | 8c82de9 | 2015-09-07 18:33:09 +0100 | [diff] [blame] | 376 | for (i = 0; i < OSSL_NELEM(hash_table); i++) { |
Timo Teras | 8f6f144 | 2015-05-07 12:48:47 -0400 | [diff] [blame] | 377 | for (bp = hash_table[i]; bp; bp = nextbp) { |
| 378 | nextbp = bp->next; |
| 379 | nextid = 0; |
| 380 | memset(idmask, 0, (bp->num_needed + 7) / 8); |
| 381 | for (ep = bp->first_entry; ep; ep = ep->next) |
| 382 | if (ep->old_id < bp->num_needed) |
| 383 | bit_set(idmask, ep->old_id); |
| 384 | |
| 385 | for (ep = bp->first_entry; ep; ep = nextep) { |
| 386 | nextep = ep->next; |
| 387 | if (ep->old_id < bp->num_needed) { |
| 388 | /* Link exists, and is used as-is */ |
| 389 | snprintf(buf, buflen, "%08x.%s%d", bp->hash, |
| 390 | suffixes[bp->type], ep->old_id); |
| 391 | if (verbose) |
| 392 | BIO_printf(bio_out, "link %s -> %s\n", |
| 393 | ep->filename, buf); |
| 394 | } else if (ep->need_symlink) { |
| 395 | /* New link needed (it may replace something) */ |
| 396 | while (bit_isset(idmask, nextid)) |
| 397 | nextid++; |
| 398 | |
| 399 | snprintf(buf, buflen, "%s%s%n%08x.%s%d", |
| 400 | dirname, pathsep, &n, bp->hash, |
| 401 | suffixes[bp->type], nextid); |
| 402 | if (verbose) |
| 403 | BIO_printf(bio_out, "link %s -> %s\n", |
| 404 | ep->filename, &buf[n]); |
Rich Salz | ce249fa | 2015-09-07 10:08:14 -0400 | [diff] [blame] | 405 | if (unlink(buf) < 0 && errno != ENOENT) { |
Timo Teras | 8f6f144 | 2015-05-07 12:48:47 -0400 | [diff] [blame] | 406 | BIO_printf(bio_err, |
| 407 | "%s: Can't unlink %s, %s\n", |
| 408 | opt_getprog(), buf, strerror(errno)); |
Rich Salz | ce249fa | 2015-09-07 10:08:14 -0400 | [diff] [blame] | 409 | errs++; |
| 410 | } |
| 411 | if (symlink(ep->filename, buf) < 0) { |
Timo Teras | 8f6f144 | 2015-05-07 12:48:47 -0400 | [diff] [blame] | 412 | BIO_printf(bio_err, |
| 413 | "%s: Can't symlink %s, %s\n", |
| 414 | opt_getprog(), ep->filename, |
| 415 | strerror(errno)); |
Rich Salz | ce249fa | 2015-09-07 10:08:14 -0400 | [diff] [blame] | 416 | errs++; |
| 417 | } |
Dr. Stephen Henson | 3770b87 | 2016-07-22 01:09:52 +0100 | [diff] [blame] | 418 | bit_set(idmask, nextid); |
Timo Teras | 8f6f144 | 2015-05-07 12:48:47 -0400 | [diff] [blame] | 419 | } else if (remove_links) { |
| 420 | /* Link to be deleted */ |
| 421 | snprintf(buf, buflen, "%s%s%n%08x.%s%d", |
| 422 | dirname, pathsep, &n, bp->hash, |
| 423 | suffixes[bp->type], ep->old_id); |
| 424 | if (verbose) |
| 425 | BIO_printf(bio_out, "unlink %s\n", |
| 426 | &buf[n]); |
Rich Salz | ce249fa | 2015-09-07 10:08:14 -0400 | [diff] [blame] | 427 | if (unlink(buf) < 0 && errno != ENOENT) { |
Timo Teras | 8f6f144 | 2015-05-07 12:48:47 -0400 | [diff] [blame] | 428 | BIO_printf(bio_err, |
| 429 | "%s: Can't unlink %s, %s\n", |
| 430 | opt_getprog(), buf, strerror(errno)); |
Rich Salz | ce249fa | 2015-09-07 10:08:14 -0400 | [diff] [blame] | 431 | errs++; |
| 432 | } |
Timo Teras | 8f6f144 | 2015-05-07 12:48:47 -0400 | [diff] [blame] | 433 | } |
| 434 | OPENSSL_free(ep->filename); |
| 435 | OPENSSL_free(ep); |
| 436 | } |
| 437 | OPENSSL_free(bp); |
| 438 | } |
| 439 | hash_table[i] = NULL; |
| 440 | } |
Timo Teras | 8f6f144 | 2015-05-07 12:48:47 -0400 | [diff] [blame] | 441 | |
| 442 | OPENSSL_free(buf); |
Rich Salz | ce249fa | 2015-09-07 10:08:14 -0400 | [diff] [blame] | 443 | return errs; |
Timo Teras | 8f6f144 | 2015-05-07 12:48:47 -0400 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | typedef enum OPTION_choice { |
| 447 | OPT_ERR = -1, OPT_EOF = 0, OPT_HELP, |
| 448 | OPT_COMPAT, OPT_OLD, OPT_N, OPT_VERBOSE |
| 449 | } OPTION_CHOICE; |
| 450 | |
FdaSilvaYY | 44c83eb | 2016-03-13 14:07:50 +0100 | [diff] [blame] | 451 | const OPTIONS rehash_options[] = { |
Timo Teras | 8f6f144 | 2015-05-07 12:48:47 -0400 | [diff] [blame] | 452 | {OPT_HELP_STR, 1, '-', "Usage: %s [options] [cert-directory...]\n"}, |
| 453 | {OPT_HELP_STR, 1, '-', "Valid options are:\n"}, |
| 454 | {"help", OPT_HELP, '-', "Display this summary"}, |
Rich Salz | 7d959c3 | 2016-09-12 11:29:22 -0400 | [diff] [blame] | 455 | {"h", OPT_HELP, '-', "Display this summary"}, |
Timo Teras | 8f6f144 | 2015-05-07 12:48:47 -0400 | [diff] [blame] | 456 | {"compat", OPT_COMPAT, '-', "Create both new- and old-style hash links"}, |
| 457 | {"old", OPT_OLD, '-', "Use old-style hash to generate links"}, |
| 458 | {"n", OPT_N, '-', "Do not remove existing links"}, |
| 459 | {"v", OPT_VERBOSE, '-', "Verbose output"}, |
| 460 | {NULL} |
| 461 | }; |
| 462 | |
| 463 | |
| 464 | int rehash_main(int argc, char **argv) |
| 465 | { |
| 466 | const char *env, *prog; |
| 467 | char *e, *m; |
Rich Salz | ce249fa | 2015-09-07 10:08:14 -0400 | [diff] [blame] | 468 | int errs = 0; |
Timo Teras | 8f6f144 | 2015-05-07 12:48:47 -0400 | [diff] [blame] | 469 | OPTION_CHOICE o; |
| 470 | enum Hash h = HASH_NEW; |
| 471 | |
| 472 | prog = opt_init(argc, argv, rehash_options); |
| 473 | while ((o = opt_next()) != OPT_EOF) { |
| 474 | switch (o) { |
| 475 | case OPT_EOF: |
| 476 | case OPT_ERR: |
| 477 | BIO_printf(bio_err, "%s: Use -help for summary.\n", prog); |
| 478 | goto end; |
| 479 | case OPT_HELP: |
| 480 | opt_help(rehash_options); |
| 481 | goto end; |
| 482 | case OPT_COMPAT: |
| 483 | h = HASH_BOTH; |
| 484 | break; |
| 485 | case OPT_OLD: |
| 486 | h = HASH_OLD; |
| 487 | break; |
| 488 | case OPT_N: |
| 489 | remove_links = 0; |
| 490 | break; |
| 491 | case OPT_VERBOSE: |
| 492 | verbose = 1; |
| 493 | break; |
| 494 | } |
| 495 | } |
| 496 | argc = opt_num_rest(); |
| 497 | argv = opt_rest(); |
| 498 | |
| 499 | evpmd = EVP_sha1(); |
| 500 | evpmdsize = EVP_MD_size(evpmd); |
| 501 | |
| 502 | if (*argv) { |
| 503 | while (*argv) |
Rich Salz | ce249fa | 2015-09-07 10:08:14 -0400 | [diff] [blame] | 504 | errs += do_dir(*argv++, h); |
Timo Teras | 8f6f144 | 2015-05-07 12:48:47 -0400 | [diff] [blame] | 505 | } else if ((env = getenv("SSL_CERT_DIR")) != NULL) { |
Rich Salz | 7644a9a | 2015-12-16 16:12:24 -0500 | [diff] [blame] | 506 | m = OPENSSL_strdup(env); |
Timo Teras | 8f6f144 | 2015-05-07 12:48:47 -0400 | [diff] [blame] | 507 | for (e = strtok(m, ":"); e != NULL; e = strtok(NULL, ":")) |
Rich Salz | ce249fa | 2015-09-07 10:08:14 -0400 | [diff] [blame] | 508 | errs += do_dir(e, h); |
Timo Teras | 8f6f144 | 2015-05-07 12:48:47 -0400 | [diff] [blame] | 509 | OPENSSL_free(m); |
| 510 | } else { |
Rich Salz | ce249fa | 2015-09-07 10:08:14 -0400 | [diff] [blame] | 511 | errs += do_dir("/etc/ssl/certs", h); |
Timo Teras | 8f6f144 | 2015-05-07 12:48:47 -0400 | [diff] [blame] | 512 | } |
| 513 | |
| 514 | end: |
Rich Salz | ce249fa | 2015-09-07 10:08:14 -0400 | [diff] [blame] | 515 | return errs; |
Timo Teras | 8f6f144 | 2015-05-07 12:48:47 -0400 | [diff] [blame] | 516 | } |
| 517 | |
| 518 | #else |
FdaSilvaYY | 44c83eb | 2016-03-13 14:07:50 +0100 | [diff] [blame] | 519 | const OPTIONS rehash_options[] = { |
Rich Salz | 62fdf4e | 2015-09-06 21:39:26 -0400 | [diff] [blame] | 520 | {NULL} |
| 521 | }; |
Timo Teras | 8f6f144 | 2015-05-07 12:48:47 -0400 | [diff] [blame] | 522 | |
| 523 | int rehash_main(int argc, char **argv) |
| 524 | { |
Rich Salz | 9e0da06 | 2015-09-06 19:01:24 -0400 | [diff] [blame] | 525 | BIO_printf(bio_err, "Not available; use c_rehash script\n"); |
Timo Teras | 8f6f144 | 2015-05-07 12:48:47 -0400 | [diff] [blame] | 526 | return (1); |
| 527 | } |
| 528 | |
Rich Salz | 568b802 | 2015-09-16 22:17:55 -0400 | [diff] [blame] | 529 | #endif /* defined(OPENSSL_SYS_UNIX) || defined(__APPLE__) */ |