Hardware authentication for Linux using ordinary USB Flash Drives.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

203 lines
4.9 KiB

8 years ago
8 years ago
8 years ago
13 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
  1. /* -*- mode: c; c-file-style: "bsd"; indent-tabs-mode: t; -*- */
  2. /*
  3. * Copyright (c) 2016 Luka Novsak <lnovsak@gmail.com>
  4. * Copyright (c) 2003-2007 Andrea Luzzardi <scox@sig11.org>
  5. *
  6. * This file is part of the pam_usb project. pam_usb is free software;
  7. * you can redistribute it and/or modify it under the terms of the GNU General
  8. * Public License version 2, as published by the Free Software Foundation.
  9. *
  10. * pam_usb is distributed in the hope that it will be useful, but WITHOUT ANY
  11. * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  12. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  13. * details.
  14. *
  15. * You should have received a copy of the GNU General Public License along with
  16. * this program; if not, write to the Free Software Foundation, Inc., 51 Franklin
  17. * Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. */
  19. #include <stdio.h>
  20. #include <string.h>
  21. #include <errno.h>
  22. #include <unistd.h>
  23. #include <stdlib.h>
  24. #include <sys/types.h>
  25. #ifndef __GNU__
  26. #include <sys/mount.h>
  27. #endif
  28. #include "mem.h"
  29. #include "conf.h"
  30. #include "log.h"
  31. #include "volume.h"
  32. static int pusb_volume_mount(t_pusb_volume *volume)
  33. {
  34. GError *error = NULL;
  35. GVariant *options = NULL;
  36. GVariantBuilder builder;
  37. int retval = 0;
  38. const gchar *const *mount_points = NULL;
  39. g_variant_builder_init(&builder, G_VARIANT_TYPE_VARDICT);
  40. options = g_variant_builder_end(&builder);
  41. log_debug("Attempting to mount device %s.\n", volume->device);
  42. udisks_filesystem_call_mount_sync(volume->filesystem,
  43. options,
  44. &volume->mount_point,
  45. NULL,
  46. &error);
  47. if (!error)
  48. {
  49. volume->unmount = 1;
  50. retval = 1;
  51. log_debug("Mounted device %s to %s.\n",
  52. volume->device, volume->mount_point);
  53. }
  54. else if (error->code == UDISKS_ERROR_ALREADY_MOUNTED)
  55. {
  56. g_main_context_iteration(NULL, FALSE);
  57. mount_points = udisks_filesystem_get_mount_points(volume->filesystem);
  58. volume->mount_point = xstrdup(*mount_points);
  59. retval = 1;
  60. log_debug("Device %s mounted in between our probe and mount.\n",
  61. volume->device);
  62. }
  63. else
  64. {
  65. log_error("Failed to mount device %s.\n", volume->device);
  66. }
  67. if (error)
  68. g_error_free(error);
  69. return (retval);
  70. }
  71. static t_pusb_volume *pusb_volume_probe(t_pusb_options *opts,
  72. UDisksClient *udisks)
  73. {
  74. t_pusb_volume *volume = NULL;
  75. int maxtries = (opts->probe_timeout * 1000000) / 250000;
  76. int i;
  77. int j;
  78. GList *blocks = NULL;
  79. UDisksBlock *block = NULL;
  80. UDisksObject *object = NULL;
  81. const gchar *const *mount_points = NULL;
  82. if (!*(opts->device.volume_uuid))
  83. {
  84. log_debug("No UUID configured for device.\n");
  85. return (NULL);
  86. }
  87. log_debug("Searching for volume with uuid %s.\n",
  88. opts->device.volume_uuid);
  89. for (i = 0; i < maxtries; ++i)
  90. {
  91. blocks = udisks_client_get_block_for_uuid(udisks, opts->device.volume_uuid);
  92. if (i == 1)
  93. log_info("Probing volume (this could take a while)...\n");
  94. for (j = 0; j < g_list_length(blocks); ++j)
  95. {
  96. block = UDISKS_BLOCK(g_list_nth(blocks, j)->data);
  97. object = UDISKS_OBJECT(g_dbus_interface_get_object(G_DBUS_INTERFACE(block)));
  98. if (udisks_object_peek_filesystem(object))
  99. {
  100. volume = xmalloc(sizeof(t_pusb_volume));
  101. volume->filesystem = udisks_object_get_filesystem(object);
  102. volume->unmount = 0;
  103. volume->device = xstrdup(udisks_block_get_device(block));
  104. volume->mount_point = NULL;
  105. mount_points = udisks_filesystem_get_mount_points(volume->filesystem);
  106. if (mount_points && *mount_points)
  107. volume->mount_point = xstrdup(*mount_points);
  108. break;
  109. }
  110. }
  111. g_list_foreach (blocks, (GFunc) g_object_unref, NULL);
  112. g_list_free (blocks);
  113. if (volume)
  114. {
  115. log_debug("Found volume %s.\n", opts->device.volume_uuid);
  116. break;
  117. }
  118. usleep(250000);
  119. g_main_context_iteration(NULL, FALSE);
  120. }
  121. if (!volume)
  122. log_debug("Could not find volume %s.\n",
  123. opts->device.volume_uuid);
  124. return (volume);
  125. }
  126. t_pusb_volume *pusb_volume_get(t_pusb_options *opts, UDisksClient *udisks)
  127. {
  128. t_pusb_volume *volume = pusb_volume_probe(opts, udisks);
  129. if (!volume)
  130. return (NULL);
  131. if (volume->mount_point)
  132. {
  133. log_debug("Volume %s is already mounted.\n",
  134. opts->device.volume_uuid);
  135. return (volume);
  136. }
  137. if(!pusb_volume_mount(volume))
  138. {
  139. pusb_volume_destroy(volume);
  140. return (NULL);
  141. }
  142. return (volume);
  143. }
  144. void pusb_volume_destroy(t_pusb_volume *volume)
  145. {
  146. GVariantBuilder builder;
  147. GVariant *options;
  148. int ret;
  149. if (volume->unmount)
  150. {
  151. g_variant_builder_init(&builder, G_VARIANT_TYPE_VARDICT);
  152. options = g_variant_builder_end(&builder);
  153. log_debug("Attempting to unmount %s from %s.\n",
  154. volume->device, volume->mount_point);
  155. ret = udisks_filesystem_call_unmount_sync(volume->filesystem,
  156. options,
  157. NULL,
  158. NULL);
  159. if (!ret)
  160. log_error("Unable to unmount %s from %s\n",
  161. volume->device, volume->mount_point);
  162. log_debug("Unmount succeeded.\n");
  163. }
  164. g_object_unref(volume->filesystem);
  165. xfree(volume->device);
  166. xfree(volume->mount_point);
  167. xfree(volume);
  168. }