diff options
author | Jason A. Donenfeld <Jason@zx2c4.com> | 2012-12-20 02:07:52 +0100 |
---|---|---|
committer | Jason A. Donenfeld <Jason@zx2c4.com> | 2012-12-20 02:10:38 +0100 |
commit | b5d8b88aef17e29980c1a795b5cf5d9799149804 (patch) | |
tree | 919fa03dcdc7db717ffae06bcef35d2ddd099c9a | |
download | knock-knock-token-b5d8b88aef17e29980c1a795b5cf5d9799149804.tar.xz knock-knock-token-b5d8b88aef17e29980c1a795b5cf5d9799149804.zip |
-rw-r--r-- | Makefile | 6 | ||||
-rw-r--r-- | knock-knock-token.c | 71 |
2 files changed, 77 insertions, 0 deletions
diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..516c19d --- /dev/null +++ b/Makefile @@ -0,0 +1,6 @@ +CFLAGS ?= -O3 -march=native -fomit-frame-pointer -pipe + +knock-knock-token: +.PHONY: clean +clean: + rm -f knock-knock-token diff --git a/knock-knock-token.c b/knock-knock-token.c new file mode 100644 index 0000000..617202f --- /dev/null +++ b/knock-knock-token.c @@ -0,0 +1,71 @@ +/* + * Knock-Knock Token + * by zx2c4 + * Jason@zx2c4.com + * + * Someone about to steal your laptop and you have sensitive things open on it? + * With Knock-Knock Token, you specify a block device that belongs to removable + * storage such as a USB flash drive. When the flash drive is removed from the + * USB port, and the block device disappears as a consequence, your computer + * immediately turns off. So, as the thief is snatching your laptop from you, + * simply snatch the USB key, and your data is saved. The program automatically + * daemonizes. + * + * $ sudo ./knock-knock-token /dev/sdc1 + */ + +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> +#include <sys/stat.h> +#include <sys/inotify.h> +#include <sys/reboot.h> + +int main(int argc, char *argv[]) +{ + int inotify, device_monitor; + struct stat file_info; + + if (argc < 2) { + fprintf(stderr, "Usage: %s BLOCK_DEVICE\n", argv[0]); + return EXIT_FAILURE; + } + + if (getuid()) { + fprintf(stderr, "You must be root to run this program.\n"); + return EXIT_FAILURE; + } + + if (stat(argv[1], &file_info) < 0) { + perror("stat"); + return EXIT_FAILURE; + } + if (!S_ISBLK(file_info.st_mode)) + fprintf(stderr, "Warning: %s is not a block device. Are you sure you meant to monitor it?\n", argv[1]); + + inotify = inotify_init(); + if (inotify < 0) { + perror("inotify_init"); + return EXIT_FAILURE; + } + device_monitor = inotify_add_watch(inotify, argv[1], IN_DELETE_SELF); + if (device_monitor < 0) { + perror("inotify_add_watch"); + return EXIT_FAILURE; + } + + fprintf(stderr, "Daemonizing...\n"); + if (daemon(0, 0) < 0) + perror("daemon"); + + device_monitor = read(inotify, NULL, 0); + + //TODO: securely wipe memory + + sync(); + reboot(RB_ENABLE_CAD); + reboot(RB_POWER_OFF); + reboot(RB_HALT_SYSTEM); + + return EXIT_SUCCESS; +} |