/* * 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 #include #include #include #include #include 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; }