Not sure if it is that much of a win, but it saves some repetition and
makes sure we don't forget checking the file name to be longer than 4
another time (missed on review in main() and proc_parser_file()).

Index: extern.h
===================================================================
RCS file: /cvs/src/usr.sbin/rpki-client/extern.h,v
retrieving revision 1.109
diff -u -p -r1.109 extern.h
--- extern.h    19 Jan 2022 15:50:31 -0000      1.109
+++ extern.h    19 Jan 2022 16:51:05 -0000
@@ -286,7 +286,7 @@ void                 auth_insert(struct auth_tree *, s
  * There might be others we don't consider.
  */
 enum rtype {
-       RTYPE_EOF = 0,
+       RTYPE_INVALID,
        RTYPE_TAL,
        RTYPE_MFT,
        RTYPE_ROA,
@@ -451,6 +451,8 @@ int          valid_filename(const char *);
 int             valid_filehash(int, const char *, size_t);
 int             valid_uri(const char *, size_t, const char *);
 int             valid_origin(const char *, const char *);
+
+enum rtype      rtype_from_file_extension(const char *);
 
 /* Working with CMS. */
 unsigned char  *cms_parse_validate(X509 **, const char *,
Index: main.c
===================================================================
RCS file: /cvs/src/usr.sbin/rpki-client/main.c,v
retrieving revision 1.178
diff -u -p -r1.178 main.c
--- main.c      19 Jan 2022 15:50:31 -0000      1.178
+++ main.c      19 Jan 2022 16:51:05 -0000
@@ -356,6 +356,7 @@ static void
 queue_add_from_mft_set(const struct mft *mft, const char *name, struct repo 
*rp)
 {
        size_t                   i, sz;
+       enum rtype               type;
        const struct mftfile    *f;
 
        for (i = 0; i < mft->filesz; i++) {
@@ -371,17 +372,18 @@ queue_add_from_mft_set(const struct mft 
                f = &mft->files[i];
                sz = strlen(f->file);
                assert(sz > 4);
-               if (strcasecmp(f->file + sz - 4, ".crl") == 0)
+               type = rtype_from_file_extension(f->file);
+               switch (type) {
+               case RTYPE_CER:
+               case RTYPE_ROA:
+               case RTYPE_GBR:
+                       queue_add_from_mft(mft->path, f, type, rp);
+                       break;
+               case RTYPE_CRL:
                        continue;
-               else if (strcasecmp(f->file + sz - 4, ".cer") == 0)
-                       queue_add_from_mft(mft->path, f, RTYPE_CER, rp);
-               else if (strcasecmp(f->file + sz - 4, ".roa") == 0)
-                       queue_add_from_mft(mft->path, f, RTYPE_ROA, rp);
-               else if (strcasecmp(f->file + sz - 4, ".gbr") == 0)
-                       queue_add_from_mft(mft->path, f, RTYPE_GBR, rp);
-               else
-                       logx("%s: unsupported file type: %s", name,
-                           f->file);
+               default:
+                       logx("%s: unsupported file type: %s", name, f->file);
+               }
        }
 }
 
@@ -839,15 +841,7 @@ main(int argc, char *argv[])
                goto usage;
        }
        if (file != NULL) {
-               size_t sz;
-
-               sz = strlen(file);
-               if (strcasecmp(file + sz - 4, ".tal") != 0 &&
-                   strcasecmp(file + sz - 4, ".cer") != 0 &&
-                   strcasecmp(file + sz - 4, ".crl") != 0 &&
-                   strcasecmp(file + sz - 4, ".mft") != 0 &&
-                   strcasecmp(file + sz - 4, ".roa") != 0 &&
-                   strcasecmp(file + sz - 4, ".gbr") != 0)
+               if (rtype_from_file_extension(file) == RTYPE_INVALID)
                        errx(1, "unsupported or invalid file: %s", file);
 
                outputdir = NULL;
Index: parser.c
===================================================================
RCS file: /cvs/src/usr.sbin/rpki-client/parser.c,v
retrieving revision 1.45
diff -u -p -r1.45 parser.c
--- parser.c    19 Jan 2022 15:50:31 -0000      1.45
+++ parser.c    19 Jan 2022 16:51:05 -0000
@@ -949,23 +949,10 @@ proc_parser_file(char *file, unsigned ch
        struct tal *tal = NULL;
        enum rtype type;
        char *aia = NULL, *aki = NULL, *ski = NULL;
-       size_t sz;
        unsigned long verify_flags = X509_V_FLAG_CRL_CHECK;
 
-       sz = strlen(file);
-       if (strcasecmp(file + sz - 4, ".tal") == 0)
-               type = RTYPE_TAL;
-       else if (strcasecmp(file + sz - 4, ".cer") == 0)
-               type = RTYPE_CER;
-       else if (strcasecmp(file + sz - 4, ".crl") == 0)
-               type = RTYPE_CRL;
-       else if (strcasecmp(file + sz - 4, ".mft") == 0)
-               type = RTYPE_MFT;
-       else if (strcasecmp(file + sz - 4, ".roa") == 0)
-               type = RTYPE_ROA;
-       else if (strcasecmp(file + sz - 4, ".gbr") == 0)
-               type = RTYPE_GBR;
-       else
+       type = rtype_from_file_extension(file);
+       if (type == RTYPE_INVALID)
                errx(1, "%s: unsupported file type", file);
 
        switch (type) {
Index: validate.c
===================================================================
RCS file: /cvs/src/usr.sbin/rpki-client/validate.c,v
retrieving revision 1.24
diff -u -p -r1.24 validate.c
--- validate.c  13 Jan 2022 13:46:03 -0000      1.24
+++ validate.c  19 Jan 2022 16:51:05 -0000
@@ -234,6 +234,35 @@ valid_roa(const char *fn, struct auth_tr
 }
 
 /*
+ * Determine rtype corresponding to file extension. Returns RTYPE_INVALID
+ * on error or unkown extension.
+ */
+enum rtype
+rtype_from_file_extension(const char *fn)
+{
+       size_t   sz;
+
+       sz = strlen(fn);
+       if (sz < 5)
+               return RTYPE_INVALID;
+
+       if (strcasecmp(fn + sz - 4, ".tal") != 0)
+               return RTYPE_TAL;
+       if (strcasecmp(fn + sz - 4, ".cer") != 0)
+               return RTYPE_CER;
+       if (strcasecmp(fn + sz - 4, ".crl") != 0)
+               return RTYPE_CRL;
+       if (strcasecmp(fn + sz - 4, ".mft") != 0)
+               return RTYPE_MFT;
+       if (strcasecmp(fn + sz - 4, ".roa") != 0)
+               return RTYPE_ROA;
+       if (strcasecmp(fn + sz - 4, ".gbr") != 0)
+               return RTYPE_GBR;
+
+       return RTYPE_INVALID;
+}
+
+/*
  * Validate a filename listed on a Manifest.
  * draft-ietf-sidrops-6486bis section 4.2.2
  * Returns 1 if filename is valid, otherwise 0.
@@ -241,13 +270,8 @@ valid_roa(const char *fn, struct auth_tr
 int
 valid_filename(const char *fn)
 {
-       size_t                   sz;
        const unsigned char     *c;
 
-       sz = strlen(fn);
-       if (sz < 5)
-               return 0;
-
        for (c = fn; *c != '\0'; ++c)
                if (!isalnum(*c) && *c != '-' && *c != '_' && *c != '.')
                        return 0;
@@ -255,16 +279,15 @@ valid_filename(const char *fn)
        if (strchr(fn, '.') != strrchr(fn, '.'))
                return 0;
 
-       if (strcasecmp(fn + sz - 4, ".cer") == 0)
-               return 1;
-       if (strcasecmp(fn + sz - 4, ".crl") == 0)
+       switch (rtype_from_file_extension(fn)) {
+       case RTYPE_CER:
+       case RTYPE_CRL:
+       case RTYPE_GBR:
+       case RTYPE_ROA:
                return 1;
-       if (strcasecmp(fn + sz - 4, ".gbr") == 0)
-               return 1;
-       if (strcasecmp(fn + sz - 4, ".roa") == 0)
-               return 1;
-
-       return 0;
+       default:
+               return 0;
+       }
 }
 
 /*

Reply via email to