On Thu, Jan 13, 2022 at 02:16:02PM +0100, Claudio Jeker wrote:
> Right now a file can only exist in one place in the rpki-client cache.
> This will change when we split valid data to its own repo.
>
> One step to get closer to that is to alter valid_filehash() to take an
> open filedescriptor instead of using open(2) itself. This allows the
> callers to decide which file to pass. valid_filehash() handles fd == -1
> and so the open call does not need to be checked for error.
>
> Additionally move mft_check() from mft.c to parser.c. It simplifies later
> work.
>
ok
> Index: validate.c
> ===================================================================
> RCS file: /cvs/src/usr.sbin/rpki-client/validate.c,v
> retrieving revision 1.23
> diff -u -p -r1.23 validate.c
> --- validate.c 26 Dec 2021 12:32:28 -0000 1.23
> +++ validate.c 13 Jan 2022 12:07:54 -0000
> @@ -269,29 +269,29 @@ valid_filename(const char *fn)
>
> /*
> * Validate a file by verifying the SHA256 hash of that file.
> - * Returns 1 if valid, 0 otherwise.
> + * The file is passed a an open filedescriptor fd which can be -1.
missing s: passed as
and "file descriptor" should be two words
(also: -1 can't be an open fd, but I guess it's clear what is meant)
> + * Returns 1 if valid, 0 otherwise. Closes fd when done.
> */
> int
> -valid_filehash(const char *fn, const char *hash, size_t hlen)
> +valid_filehash(int fd, const char *hash, size_t hlen)
> {
> SHA256_CTX ctx;
> char filehash[SHA256_DIGEST_LENGTH];
> char buffer[8192];
> ssize_t nr;
> - int fd;
>
> if (hlen != sizeof(filehash))
> errx(1, "bad hash size");
>
> - if ((fd = open(fn, O_RDONLY)) == -1)
> + if (fd == -1)
> return 0;
>
> SHA256_Init(&ctx);
> while ((nr = read(fd, buffer, sizeof(buffer))) > 0)
> SHA256_Update(&ctx, buffer, nr);
> close(fd);
> -
> SHA256_Final(filehash, &ctx);
> +
> if (memcmp(hash, filehash, sizeof(filehash)) != 0)
> return 0;
>
>