#include #include #include #include #include static UDisksClient *client = NULL; static const gchar *dev="block_devices/sdb1"; int main(int argc, char *argv[]) { GError *error = NULL; /* open connection to UDisks daemon */ client = udisks_client_new_sync (NULL, &error); if (client == NULL) { g_printerr ("Error connecting to the udisks daemon: %s\n", error->message); g_error_free (error); return 1; } UDisksObject *object; UDisksBlock *block; UDisksEncrypted *encrypted; /* find encrypted devices */ const gchar *object_path; GList *objects, *l; objects = g_dbus_object_manager_get_objects (udisks_client_get_object_manager (client)); for (l = objects; l != NULL; l = l->next) { object = UDISKS_OBJECT (l->data); encrypted = udisks_object_peek_encrypted (object); if (encrypted == NULL) continue; object_path = g_dbus_object_get_object_path (G_DBUS_OBJECT (object)); g_assert (g_str_has_prefix (object_path, "/org/freedesktop/UDisks2/")); if (!strcmp (object_path + sizeof ("/org/freedesktop/UDisks2/") - 1, dev)) { g_print ("FOUND %s \n", object_path + sizeof ("/org/freedesktop/UDisks2/") - 1); break; /* we found the correct object */ } else g_print ("IGNORING %s \n", object_path + sizeof ("/org/freedesktop/UDisks2/") - 1); } g_list_foreach (objects, (GFunc) g_object_unref, NULL); g_list_free (objects); if (!object) { g_printerr ("Object not found for %s\n", dev); return 1; } /* actual unlock */ block = udisks_object_peek_block (object); encrypted = udisks_object_peek_encrypted (object); error = NULL; gchar *cleartext_object_path; UDisksObject *cleartext_object; GVariant *options; GVariantBuilder builder; g_variant_builder_init (&builder, G_VARIANT_TYPE_VARDICT); options = g_variant_builder_end (&builder); g_variant_ref_sink (options); if (!udisks_encrypted_call_unlock_sync (encrypted, "WrongPass", options, &cleartext_object_path, NULL, &error)) { g_printerr ("Error unlocking %s: %s, %u, %u\n", udisks_block_get_device (block), error->message, error->domain, error->code); g_clear_error (&error); g_print ("G_IO_ERROR=%u, G_IO_ERROR_FAILED=%u\n", G_IO_ERROR, G_IO_ERROR_FAILED); g_print ("UDISKS_ERROR=%u, UDISKS_ERROR_FAILED=%u\n", UDISKS_ERROR, UDISKS_ERROR_FAILED); g_object_unref (object); goto out; } udisks_client_settle (client); cleartext_object = UDISKS_OBJECT (g_dbus_object_manager_get_object (udisks_client_get_object_manager (client), (cleartext_object_path))); g_print ("Unlocked %s as %s.\n", udisks_block_get_device (block), udisks_block_get_device (udisks_object_get_block (cleartext_object))); g_object_unref (cleartext_object); g_free (cleartext_object_path); g_object_unref (object); out: return 0; }