Eric Biggers | 7d7c3d5 | 2018-03-13 11:57:29 -0700 | [diff] [blame] | 1 | #include <fcntl.h> |
| 2 | #include <stdio.h> |
| 3 | #include <stdlib.h> |
| 4 | #include <string.h> |
| 5 | #include <sys/ioctl.h> |
| 6 | #include <unistd.h> |
| 7 | |
| 8 | #include "fsverity_api.h" |
| 9 | |
| 10 | static void usage(void) |
| 11 | { |
| 12 | fprintf(stderr, |
| 13 | "Usage: fsveritymeasure FILE EXPECTED_MEASUREMENT\n" |
| 14 | "\n" |
| 15 | "EXPECTED_MEASUREMENT must be a 64-character hex string.\n"); |
| 16 | exit(2); |
| 17 | } |
| 18 | |
| 19 | int main(int args, char *argv[]) |
| 20 | { |
| 21 | int fd, i; |
| 22 | unsigned int byte; |
| 23 | struct fsverity_root_hash measurement = { 0 }; |
| 24 | |
| 25 | if (args != 3 || strlen(argv[2]) != 64) |
| 26 | usage(); |
| 27 | |
| 28 | for (i = 0; i < 32; i++) { |
| 29 | if (sscanf(&argv[2][i*2], "%02x", &byte) != 1) |
| 30 | usage(); |
| 31 | measurement.root_hash[i] = byte; |
| 32 | } |
| 33 | fd = open(argv[1], O_RDONLY); |
| 34 | if (fd < 0) { |
| 35 | fprintf(stderr, "Can't open %s: %m\n", argv[1]); |
| 36 | return 1; |
| 37 | } |
| 38 | if (ioctl(fd, FS_IOC_MEASURE_FSVERITY, &measurement)) { |
| 39 | fprintf(stderr, "FS_IOC_MEASURE_FSVERITY: %m\n"); |
| 40 | return 1; |
| 41 | } |
| 42 | close(fd); |
| 43 | return 0; |
| 44 | } |