Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <libudev.h>
- #include <stdbool.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <strings.h>
- #include "detect.h"
- /*
- * Match device labels on Tails or TailsData. These are the two partition
- * labels that are documented within the Tails documentation.
- * ref: https://tails.net/doc/persistent_storage/fsck/
- */
- static bool is_tails_device(struct udev_device *dev) {
- const char *label = udev_device_get_property_value(dev, "ID_FS_LABEL");
- if (label == NULL) return false;
- return strcasecmp(label, "tails") == 0 ||
- strcasecmp(label, "tailsdata") == 0;
- }
- static struct udev_device *get_parent_disk_from_tails_device(struct udev_device *dev) {
- struct udev_device *curr = dev;
- while (curr != NULL) {
- const char *devtype = udev_device_get_devtype(curr);
- if (devtype != NULL && strcmp(devtype, "disk") == 0) {
- break;
- }
- curr = udev_device_get_parent(curr);
- }
- return curr;
- }
- char *alloc_tails_device() {
- printf("Scanning for tails device:\n");
- struct udev *udev = udev_new();
- if (udev == NULL) return NULL;
- struct udev_enumerate *enumerate = udev_enumerate_new(udev);
- if (enumerate == NULL) {
- udev_unref(udev);
- return NULL;
- }
- udev_enumerate_add_match_subsystem(enumerate, "block");
- int scan_return_code = udev_enumerate_scan_devices(enumerate);
- if (scan_return_code < 0) {
- fprintf(stderr, "An error occurred while scanning block devices. Error code: %d\n", scan_return_code);
- udev_enumerate_unref(enumerate);
- udev_unref(udev);
- return NULL;
- }
- struct udev_list_entry *device_entry = udev_enumerate_get_list_entry(enumerate);
- while (device_entry) {
- const char *path = udev_list_entry_get_name(device_entry);
- struct udev_device *dev = udev_device_new_from_syspath(udev, path);
- if (dev == NULL) goto next_item;
- if (is_tails_device(dev)) {
- char *return_path = NULL;
- // Parent-chain pointer derived from dev, not separately owned.
- struct udev_device *tails_disk = get_parent_disk_from_tails_device(dev);
- if (tails_disk == NULL) {
- fprintf(stderr, "Could not resolve the parent disk for the detected partition!\n");
- goto cleanup_and_return;
- }
- const char *tails_disk_devnode = udev_device_get_devnode(tails_disk);
- if (tails_disk_devnode == NULL) {
- fprintf(stderr, "Tails device identified, but devnode couldn't be resolved. Continuing...\n");
- goto next_item;
- }
- printf("Tails device found: %s\n", tails_disk_devnode);
- return_path = strdup(tails_disk_devnode);
- if (return_path == NULL) {
- fprintf(stderr, "Out of memory error when preparing to return path to Tails device!\n");
- goto cleanup_and_return;
- }
- cleanup_and_return:
- udev_device_unref(dev);
- udev_enumerate_unref(enumerate);
- udev_unref(udev);
- return return_path;
- }
- next_item:
- if (dev != NULL) udev_device_unref(dev);
- device_entry = udev_list_entry_get_next(device_entry);
- }
- printf("No device found!\n");
- udev_enumerate_unref(enumerate);
- udev_unref(udev);
- return NULL;
- }
Advertisement
Add Comment
Please, Sign In to add comment