diff options
author | Claudio Jeker <claudio@cvs.openbsd.org> | 2019-11-04 09:35:44 +0000 |
---|---|---|
committer | Claudio Jeker <claudio@cvs.openbsd.org> | 2019-11-04 09:35:44 +0000 |
commit | 6470286cfeebe1b3f14de8fa43679eba933f36ee (patch) | |
tree | d5eb6e283ea96a72dd30794fffcc0e044c708bbe /usr.sbin | |
parent | 2617806ff8aff056898aa218179735eda80d8276 (diff) |
Refactor tal code a bit. Move the file reader back into tal.c so that the
regress test is able to use it.
OK deraadt@
Diffstat (limited to 'usr.sbin')
-rw-r--r-- | usr.sbin/rpki-client/extern.h | 3 | ||||
-rw-r--r-- | usr.sbin/rpki-client/main.c | 55 | ||||
-rw-r--r-- | usr.sbin/rpki-client/tal.c | 60 |
3 files changed, 64 insertions, 54 deletions
diff --git a/usr.sbin/rpki-client/extern.h b/usr.sbin/rpki-client/extern.h index 666b9955abd..7be9af5e324 100644 --- a/usr.sbin/rpki-client/extern.h +++ b/usr.sbin/rpki-client/extern.h @@ -1,4 +1,4 @@ -/* $OpenBSD: extern.h,v 1.10 2019/10/31 08:36:43 claudio Exp $ */ +/* $OpenBSD: extern.h,v 1.11 2019/11/04 09:35:43 claudio Exp $ */ /* * Copyright (c) 2019 Kristaps Dzonsons <kristaps@bsd.lv> * @@ -230,6 +230,7 @@ extern int verbose; void tal_buffer(char **, size_t *, size_t *, const struct tal *); void tal_free(struct tal *); struct tal *tal_parse(const char *, char *); +char *tal_read_file(const char *); struct tal *tal_read(int); void cert_buffer(char **, size_t *, size_t *, const struct cert *); diff --git a/usr.sbin/rpki-client/main.c b/usr.sbin/rpki-client/main.c index 95b30b6b861..4fd5b5cefe8 100644 --- a/usr.sbin/rpki-client/main.c +++ b/usr.sbin/rpki-client/main.c @@ -1,4 +1,4 @@ -/* $OpenBSD: main.c,v 1.21 2019/10/31 08:36:43 claudio Exp $ */ +/* $OpenBSD: main.c,v 1.22 2019/11/04 09:35:43 claudio Exp $ */ /* * Copyright (c) 2019 Kristaps Dzonsons <kristaps@bsd.lv> * @@ -22,7 +22,6 @@ #include <sys/wait.h> #include <assert.h> -#include <ctype.h> #include <err.h> #include <dirent.h> #include <fcntl.h> @@ -463,59 +462,11 @@ queue_add_from_mft_set(int fd, struct entityq *q, const struct mft *mft, static void queue_add_tal(int fd, struct entityq *q, const char *file, size_t *eid) { - char *nfile, *nbuf, *line = NULL, *buf = NULL; - FILE *in; - ssize_t n, i; - size_t sz = 0, bsz = 0; - int optcomment = 1; - - if ((in = fopen(file, "r")) == NULL) - err(EXIT_FAILURE, "fopen: %s", file); - - while ((n = getline(&line, &sz, in)) != -1) { - /* replace CRLF with just LF */ - if (n > 1 && line[n - 1] == '\n' && line[n - 2] == '\r') { - line[n - 2] = '\n'; - line[n - 1] = '\0'; - n--; - } - if (optcomment) { - /* if this is comment, just eat the line */ - if (line[0] == '#') - continue; - optcomment = 0; - /* - * Empty line is end of section and needs - * to be eaten as well. - */ - if (line[0] == '\n') - continue; - } - - /* make sure every line is valid ascii */ - for (i = 0; i < n; i++) - if (!isprint(line[i]) && !isspace(line[i])) - errx(EXIT_FAILURE, "getline: %s: " - "invalid content", file); - - /* concat line to buf */ - if ((nbuf = realloc(buf, bsz + n + 1)) == NULL) - err(EXIT_FAILURE, NULL); - buf = nbuf; - bsz += n + 1; - strlcat(buf, line, bsz); - /* limit the buffer size */ - if (bsz > 4096) - errx(EXIT_FAILURE, "%s: file too big", file); - } - - free(line); - if (ferror(in)) - err(EXIT_FAILURE, "getline: %s", file); - fclose(in); + char *nfile, *buf; if ((nfile = strdup(file)) == NULL) err(EXIT_FAILURE, "strdup"); + buf = tal_read_file(file); /* Not in a repository, so directly add to queue. */ entityq_add(fd, q, nfile, RTYPE_TAL, NULL, NULL, NULL, 0, buf, eid); diff --git a/usr.sbin/rpki-client/tal.c b/usr.sbin/rpki-client/tal.c index f6f0742e7a7..1cdf2c24062 100644 --- a/usr.sbin/rpki-client/tal.c +++ b/usr.sbin/rpki-client/tal.c @@ -1,4 +1,4 @@ -/* $OpenBSD: tal.c,v 1.8 2019/10/31 08:36:43 claudio Exp $ */ +/* $OpenBSD: tal.c,v 1.9 2019/11/04 09:35:43 claudio Exp $ */ /* * Copyright (c) 2019 Kristaps Dzonsons <kristaps@bsd.lv> * @@ -17,6 +17,7 @@ #include <netinet/in.h> #include <assert.h> +#include <ctype.h> #include <err.h> #include <libgen.h> #include <resolv.h> @@ -156,6 +157,63 @@ tal_parse(const char *fn, char *buf) return p; } +char * +tal_read_file(const char *file) +{ + char *nbuf, *line = NULL, *buf = NULL; + FILE *in; + ssize_t n, i; + size_t sz = 0, bsz = 0; + int optcomment = 1; + + if ((in = fopen(file, "r")) == NULL) + err(EXIT_FAILURE, "fopen: %s", file); + + while ((n = getline(&line, &sz, in)) != -1) { + /* replace CRLF with just LF */ + if (n > 1 && line[n - 1] == '\n' && line[n - 2] == '\r') { + line[n - 2] = '\n'; + line[n - 1] = '\0'; + n--; + } + if (optcomment) { + /* if this is comment, just eat the line */ + if (line[0] == '#') + continue; + optcomment = 0; + /* + * Empty line is end of section and needs + * to be eaten as well. + */ + if (line[0] == '\n') + continue; + } + + /* make sure every line is valid ascii */ + for (i = 0; i < n; i++) + if (!isprint(line[i]) && !isspace(line[i])) + errx(EXIT_FAILURE, "getline: %s: " + "invalid content", file); + + /* concat line to buf */ + if ((nbuf = realloc(buf, bsz + n + 1)) == NULL) + err(EXIT_FAILURE, NULL); + buf = nbuf; + bsz += n + 1; + strlcat(buf, line, bsz); + /* limit the buffer size */ + if (bsz > 4096) + errx(EXIT_FAILURE, "%s: file too big", file); + } + + free(line); + if (ferror(in)) + err(EXIT_FAILURE, "getline: %s", file); + fclose(in); + + return buf; +} + /* * Free a TAL pointer. * Safe to call with NULL. |