From 0610060da870da9f112970c880f1ad542cae16d7 Mon Sep 17 00:00:00 2001 From: job Date: Fri, 22 Mar 2024 03:38:12 +0000 Subject: [PATCH] Replace protocol literal strings and strlen() calls with defined constants OK tb@ claudio@ --- usr.sbin/rpki-client/cert.c | 8 ++++---- usr.sbin/rpki-client/extern.h | 9 ++++++++- usr.sbin/rpki-client/filemode.c | 14 +++++++------- usr.sbin/rpki-client/http.c | 6 +++--- usr.sbin/rpki-client/main.c | 6 +++--- usr.sbin/rpki-client/repo.c | 14 ++++++++------ usr.sbin/rpki-client/rrdp_delta.c | 4 ++-- usr.sbin/rpki-client/rrdp_notification.c | 6 +++--- usr.sbin/rpki-client/rrdp_snapshot.c | 4 ++-- usr.sbin/rpki-client/rsync.c | 4 ++-- usr.sbin/rpki-client/tal.c | 6 +++--- usr.sbin/rpki-client/x509.c | 8 ++++---- 12 files changed, 49 insertions(+), 40 deletions(-) diff --git a/usr.sbin/rpki-client/cert.c b/usr.sbin/rpki-client/cert.c index 309ac438e50..b5fbf3d56fc 100644 --- a/usr.sbin/rpki-client/cert.c +++ b/usr.sbin/rpki-client/cert.c @@ -1,4 +1,4 @@ -/* $OpenBSD: cert.c,v 1.128 2024/03/19 05:04:13 tb Exp $ */ +/* $OpenBSD: cert.c,v 1.129 2024/03/22 03:38:12 job Exp $ */ /* * Copyright (c) 2022 Theo Buehler * Copyright (c) 2021 Job Snijders @@ -524,15 +524,15 @@ sbgp_sia(const char *fn, struct cert *cert, X509_EXTENSION *ext) if (OBJ_cmp(oid, carepo_oid) == 0) { if (!x509_location(fn, "SIA: caRepository", - "rsync://", ad->location, &cert->repo)) + RSYNC_PROTO, ad->location, &cert->repo)) goto out; } else if (OBJ_cmp(oid, manifest_oid) == 0) { if (!x509_location(fn, "SIA: rpkiManifest", - "rsync://", ad->location, &cert->mft)) + RSYNC_PROTO, ad->location, &cert->mft)) goto out; } else if (OBJ_cmp(oid, notify_oid) == 0) { if (!x509_location(fn, "SIA: rpkiNotify", - "https://", ad->location, &cert->notify)) + HTTPS_PROTO, ad->location, &cert->notify)) goto out; } } diff --git a/usr.sbin/rpki-client/extern.h b/usr.sbin/rpki-client/extern.h index 142c26e5260..d89c352e44b 100644 --- a/usr.sbin/rpki-client/extern.h +++ b/usr.sbin/rpki-client/extern.h @@ -1,4 +1,4 @@ -/* $OpenBSD: extern.h,v 1.212 2024/03/19 05:04:13 tb Exp $ */ +/* $OpenBSD: extern.h,v 1.213 2024/03/22 03:38:12 job Exp $ */ /* * Copyright (c) 2019 Kristaps Dzonsons * @@ -1015,4 +1015,11 @@ int mkpathat(int, const char *); /* Maximum number of delegated hosting locations (repositories) for each TAL. */ #define MAX_REPO_PER_TAL 1000 +#define HTTP_PROTO "http://" +#define HTTP_PROTO_LEN (sizeof(HTTP_PROTO) - 1) +#define HTTPS_PROTO "https://" +#define HTTPS_PROTO_LEN (sizeof(HTTPS_PROTO) - 1) +#define RSYNC_PROTO "rsync://" +#define RSYNC_PROTO_LEN (sizeof(RSYNC_PROTO) - 1) + #endif /* ! EXTERN_H */ diff --git a/usr.sbin/rpki-client/filemode.c b/usr.sbin/rpki-client/filemode.c index 90f20bf7473..cd4baade192 100644 --- a/usr.sbin/rpki-client/filemode.c +++ b/usr.sbin/rpki-client/filemode.c @@ -1,4 +1,4 @@ -/* $OpenBSD: filemode.c,v 1.39 2024/03/19 05:04:13 tb Exp $ */ +/* $OpenBSD: filemode.c,v 1.40 2024/03/22 03:38:12 job Exp $ */ /* * Copyright (c) 2019 Claudio Jeker * Copyright (c) 2019 Kristaps Dzonsons @@ -62,11 +62,11 @@ parse_load_crl(char *uri) if (uri == NULL) return; - if (strncmp(uri, "rsync://", strlen("rsync://")) != 0) { + if (strncmp(uri, RSYNC_PROTO, RSYNC_PROTO_LEN) != 0) { warnx("bad CRL distribution point URI %s", uri); return; } - uri += strlen("rsync://"); + uri += RSYNC_PROTO_LEN; f = load_file(uri, &flen); if (f == NULL) { @@ -97,11 +97,11 @@ parse_load_cert(char *uri) if (uri == NULL) return NULL; - if (strncmp(uri, "rsync://", strlen("rsync://")) != 0) { + if (strncmp(uri, RSYNC_PROTO, RSYNC_PROTO_LEN) != 0) { warnx("bad authority information access URI %s", uri); return NULL; } - uri += strlen("rsync://"); + uri += RSYNC_PROTO_LEN; f = load_file(uri, &flen); if (f == NULL) { @@ -318,8 +318,8 @@ proc_parser_file(char *file, unsigned char *buf, size_t len) printf("--\n"); } - if (strncmp(file, "rsync://", strlen("rsync://")) == 0) { - file += strlen("rsync://"); + if (strncmp(file, RSYNC_PROTO, RSYNC_PROTO_LEN) == 0) { + file += RSYNC_PROTO_LEN; buf = load_file(file, &len); if (buf == NULL) { warn("parse file %s", file); diff --git a/usr.sbin/rpki-client/http.c b/usr.sbin/rpki-client/http.c index 2d76f67d926..c066ef07554 100644 --- a/usr.sbin/rpki-client/http.c +++ b/usr.sbin/rpki-client/http.c @@ -1,4 +1,4 @@ -/* $OpenBSD: http.c,v 1.81 2024/03/12 16:02:30 job Exp $ */ +/* $OpenBSD: http.c,v 1.82 2024/03/22 03:38:12 job Exp $ */ /* * Copyright (c) 2020 Nils Fisher * Copyright (c) 2020 Claudio Jeker @@ -412,7 +412,7 @@ proxy_parse_uri(char *uri) if (uri == NULL) return; - if (strncasecmp(uri, "http://", 7) != 0) + if (strncasecmp(uri, HTTP_PROTO, HTTP_PROTO_LEN) != 0) errx(1, "%s: http_proxy not using http schema", http_info(uri)); host = uri + 7; @@ -479,7 +479,7 @@ http_parse_uri(char *uri, char **ohost, char **oport, char **opath) char *host, *port = NULL, *path; char *hosttail; - if (strncasecmp(uri, "https://", 8) != 0) { + if (strncasecmp(uri, HTTPS_PROTO, HTTPS_PROTO_LEN) != 0) { warnx("%s: not using https schema", http_info(uri)); return -1; } diff --git a/usr.sbin/rpki-client/main.c b/usr.sbin/rpki-client/main.c index f962d8174f8..a13faa83d92 100644 --- a/usr.sbin/rpki-client/main.c +++ b/usr.sbin/rpki-client/main.c @@ -1,4 +1,4 @@ -/* $OpenBSD: main.c,v 1.254 2024/03/01 09:36:55 job Exp $ */ +/* $OpenBSD: main.c,v 1.255 2024/03/22 03:38:12 job Exp $ */ /* * Copyright (c) 2021 Claudio Jeker * Copyright (c) 2019 Kristaps Dzonsons @@ -433,7 +433,7 @@ queue_add_file(const char *file, enum rtype type, int talid) char *nfile; size_t len = 0; - if (!filemode || strncmp(file, "rsync://", strlen("rsync://")) != 0) { + if (!filemode || strncmp(file, RSYNC_PROTO, RSYNC_PROTO_LEN) != 0) { buf = load_file(file, &len); if (buf == NULL) err(1, "%s", file); @@ -494,7 +494,7 @@ queue_add_from_cert(const struct cert *cert) size_t repourisz; int shortlisted = 0; - if (strncmp(cert->repo, "rsync://", 8) != 0) + if (strncmp(cert->repo, RSYNC_PROTO, RSYNC_PROTO_LEN) != 0) errx(1, "unexpected protocol"); host = cert->repo + 8; diff --git a/usr.sbin/rpki-client/repo.c b/usr.sbin/rpki-client/repo.c index 77967f4acc0..1b7fa574430 100644 --- a/usr.sbin/rpki-client/repo.c +++ b/usr.sbin/rpki-client/repo.c @@ -1,4 +1,4 @@ -/* $OpenBSD: repo.c,v 1.54 2024/02/26 15:40:33 job Exp $ */ +/* $OpenBSD: repo.c,v 1.55 2024/03/22 03:38:12 job Exp $ */ /* * Copyright (c) 2021 Claudio Jeker * Copyright (c) 2019 Kristaps Dzonsons @@ -338,7 +338,7 @@ ta_fetch(struct tarepo *tr) if (!rrdpon) { for (; tr->uriidx < tr->urisz; tr->uriidx++) { if (strncasecmp(tr->uri[tr->uriidx], - "rsync://", 8) == 0) + RSYNC_PROTO, RSYNC_PROTO_LEN) == 0) break; } } @@ -353,7 +353,8 @@ ta_fetch(struct tarepo *tr) logx("ta/%s: pulling from %s", tr->descr, tr->uri[tr->uriidx]); - if (strncasecmp(tr->uri[tr->uriidx], "rsync://", 8) == 0) { + if (strncasecmp(tr->uri[tr->uriidx], RSYNC_PROTO, + RSYNC_PROTO_LEN) == 0) { /* * Create destination location. * Build up the tree to this point. @@ -499,9 +500,9 @@ rrdp_filename(const struct rrdprepo *rr, const char *uri, int valid) char *nfile; const char *dir = rr->basedir; - if (!valid_uri(uri, strlen(uri), "rsync://")) + if (!valid_uri(uri, strlen(uri), RSYNC_PROTO)) errx(1, "%s: bad URI %s", rr->basedir, uri); - uri += strlen("rsync://"); /* skip proto */ + uri += RSYNC_PROTO_LEN; /* skip proto */ if (valid) { if ((nfile = strdup(uri)) == NULL) err(1, NULL); @@ -1301,7 +1302,8 @@ repo_proto(const struct repo *rp) if (rp->ta != NULL) { const struct tarepo *tr = rp->ta; if (tr->uriidx < tr->urisz && - strncasecmp(tr->uri[tr->uriidx], "rsync://", 8) == 0) + strncasecmp(tr->uri[tr->uriidx], RSYNC_PROTO, + RSYNC_PROTO_LEN) == 0) return "rsync"; else return "https"; diff --git a/usr.sbin/rpki-client/rrdp_delta.c b/usr.sbin/rpki-client/rrdp_delta.c index a4303bc4536..7b38669adc7 100644 --- a/usr.sbin/rpki-client/rrdp_delta.c +++ b/usr.sbin/rpki-client/rrdp_delta.c @@ -1,4 +1,4 @@ -/* $OpenBSD: rrdp_delta.c,v 1.12 2023/12/27 07:17:39 tb Exp $ */ +/* $OpenBSD: rrdp_delta.c,v 1.13 2024/03/22 03:38:12 job Exp $ */ /* * Copyright (c) 2020 Nils Fisher * Copyright (c) 2021 Claudio Jeker @@ -124,7 +124,7 @@ start_publish_withdraw_elem(struct delta_xml *dxml, const char **attr, for (i = 0; attr[i]; i += 2) { if (strcmp("uri", attr[i]) == 0 && hasUri++ == 0) { if (valid_uri(attr[i + 1], strlen(attr[i + 1]), - "rsync://")) { + RSYNC_PROTO)) { uri = xstrdup(attr[i + 1]); continue; } diff --git a/usr.sbin/rpki-client/rrdp_notification.c b/usr.sbin/rpki-client/rrdp_notification.c index 4c5585a9cc1..310ba99ade2 100644 --- a/usr.sbin/rpki-client/rrdp_notification.c +++ b/usr.sbin/rpki-client/rrdp_notification.c @@ -1,4 +1,4 @@ -/* $OpenBSD: rrdp_notification.c,v 1.19 2023/12/27 07:17:39 tb Exp $ */ +/* $OpenBSD: rrdp_notification.c,v 1.20 2024/03/22 03:38:12 job Exp $ */ /* * Copyright (c) 2020 Nils Fisher * Copyright (c) 2021 Claudio Jeker @@ -228,7 +228,7 @@ start_snapshot_elem(struct notification_xml *nxml, const char **attr) for (i = 0; attr[i]; i += 2) { if (strcmp("uri", attr[i]) == 0 && hasUri++ == 0) { if (valid_uri(attr[i + 1], strlen(attr[i + 1]), - "https://") && + HTTPS_PROTO) && valid_origin(attr[i + 1], nxml->notifyuri)) { nxml->snapshot_uri = xstrdup(attr[i + 1]); continue; @@ -274,7 +274,7 @@ start_delta_elem(struct notification_xml *nxml, const char **attr) for (i = 0; attr[i]; i += 2) { if (strcmp("uri", attr[i]) == 0 && hasUri++ == 0) { if (valid_uri(attr[i + 1], strlen(attr[i + 1]), - "https://") && + HTTPS_PROTO) && valid_origin(attr[i + 1], nxml->notifyuri)) { delta_uri = attr[i + 1]; continue; diff --git a/usr.sbin/rpki-client/rrdp_snapshot.c b/usr.sbin/rpki-client/rrdp_snapshot.c index 3aa3855adf6..47c4380fddd 100644 --- a/usr.sbin/rpki-client/rrdp_snapshot.c +++ b/usr.sbin/rpki-client/rrdp_snapshot.c @@ -1,4 +1,4 @@ -/* $OpenBSD: rrdp_snapshot.c,v 1.8 2023/12/27 07:17:39 tb Exp $ */ +/* $OpenBSD: rrdp_snapshot.c,v 1.9 2024/03/22 03:38:12 job Exp $ */ /* * Copyright (c) 2020 Nils Fisher * Copyright (c) 2021 Claudio Jeker @@ -119,7 +119,7 @@ start_publish_elem(struct snapshot_xml *sxml, const char **attr) for (i = 0; attr[i]; i += 2) { if (strcmp("uri", attr[i]) == 0 && hasUri++ == 0) { if (valid_uri(attr[i + 1], strlen(attr[i + 1]), - "rsync://")) { + RSYNC_PROTO)) { uri = xstrdup(attr[i + 1]); continue; } diff --git a/usr.sbin/rpki-client/rsync.c b/usr.sbin/rpki-client/rsync.c index ca838e08fa7..33fe81541b7 100644 --- a/usr.sbin/rpki-client/rsync.c +++ b/usr.sbin/rpki-client/rsync.c @@ -1,4 +1,4 @@ -/* $OpenBSD: rsync.c,v 1.49 2024/02/26 20:37:27 job Exp $ */ +/* $OpenBSD: rsync.c,v 1.50 2024/03/22 03:38:12 job Exp $ */ /* * Copyright (c) 2019 Kristaps Dzonsons * @@ -65,7 +65,7 @@ rsync_base_uri(const char *uri) char *base_uri; /* Case-insensitive rsync URI. */ - if (strncasecmp(uri, "rsync://", 8) != 0) { + if (strncasecmp(uri, RSYNC_PROTO, RSYNC_PROTO_LEN) != 0) { warnx("%s: not using rsync schema", uri); return NULL; } diff --git a/usr.sbin/rpki-client/tal.c b/usr.sbin/rpki-client/tal.c index a1af4a70de1..d8f475c1a71 100644 --- a/usr.sbin/rpki-client/tal.c +++ b/usr.sbin/rpki-client/tal.c @@ -1,4 +1,4 @@ -/* $OpenBSD: tal.c,v 1.39 2023/06/29 10:28:25 tb Exp $ */ +/* $OpenBSD: tal.c,v 1.40 2024/03/22 03:38:12 job Exp $ */ /* * Copyright (c) 2019 Kristaps Dzonsons * @@ -84,8 +84,8 @@ tal_parse_buffer(const char *fn, char *buf, size_t len) goto out; } /* Check that the URI is sensible */ - if (!(strncasecmp(line, "https://", 8) == 0 || - strncasecmp(line, "rsync://", 8) == 0)) { + if (!(strncasecmp(line, HTTPS_PROTO, HTTPS_PROTO_LEN) == 0 || + strncasecmp(line, RSYNC_PROTO, RSYNC_PROTO_LEN) == 0)) { warnx("%s: unsupported URL schema: %s", fn, line); goto out; } diff --git a/usr.sbin/rpki-client/x509.c b/usr.sbin/rpki-client/x509.c index 962cd829d68..a2257ccf9bb 100644 --- a/usr.sbin/rpki-client/x509.c +++ b/usr.sbin/rpki-client/x509.c @@ -1,4 +1,4 @@ -/* $OpenBSD: x509.c,v 1.83 2024/03/20 04:36:30 job Exp $ */ +/* $OpenBSD: x509.c,v 1.84 2024/03/22 03:38:12 job Exp $ */ /* * Copyright (c) 2022 Theo Buehler * Copyright (c) 2021 Claudio Jeker @@ -535,8 +535,8 @@ x509_get_sia(X509 *x, const char *fn, char **sia) if (rsync_found) continue; - if (strncasecmp(*sia, "rsync://", 8) == 0) { - const char *p = *sia + strlen("rsync://"); + if (strncasecmp(*sia, RSYNC_PROTO, RSYNC_PROTO_LEN) == 0) { + const char *p = *sia + RSYNC_PROTO_LEN; size_t fnlen, plen; rsync_found = 1; @@ -773,7 +773,7 @@ x509_get_crl(X509 *x, const char *fn, char **crl) crl)) goto out; - if (strncasecmp(*crl, "rsync://", 8) == 0) { + if (strncasecmp(*crl, RSYNC_PROTO, RSYNC_PROTO_LEN) == 0) { rsync_found = 1; goto out; } -- 2.20.1