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.

168 lines
4.2 KiB

13 years ago
13 years ago
13 years ago
13 years ago
  1. /*
  2. * Copyright (c) 2003-2007 Andrea Luzzardi <scox@sig11.org>
  3. *
  4. * This file is part of the pam_usb project. pam_usb is free software;
  5. * you can redistribute it and/or modify it under the terms of the GNU General
  6. * Public License version 2, as published by the Free Software Foundation.
  7. *
  8. * pam_usb is distributed in the hope that it will be useful, but WITHOUT ANY
  9. * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  10. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  11. * details.
  12. *
  13. * You should have received a copy of the GNU General Public License along with
  14. * this program; if not, write to the Free Software Foundation, Inc., 51 Franklin
  15. * Street, Fifth Floor, Boston, MA 02110-1301 USA
  16. */
  17. #include <stdio.h>
  18. #include <string.h>
  19. #include <errno.h>
  20. #include <unistd.h>
  21. #include <stdlib.h>
  22. #include <sys/types.h>
  23. #ifndef __GNU__
  24. #include <sys/mount.h>
  25. #endif
  26. #include "mem.h"
  27. #include "conf.h"
  28. #include "log.h"
  29. #include "hal.h"
  30. #include "volume.h"
  31. static int pusb_volume_mount(t_pusb_options *opts, char *udi,
  32. DBusConnection *dbus)
  33. {
  34. char command[1024];
  35. char tempname[32];
  36. const char *devname;
  37. snprintf(tempname, sizeof(tempname), "pam_usb%d", getpid());
  38. if (!(devname = pusb_hal_get_string_property(dbus, udi, "DeviceFile")))
  39. {
  40. log_error("Unable to retrieve device filename\n");
  41. return (0);
  42. }
  43. log_debug("Attempting to mount device %s with label %s\n",
  44. devname, tempname);
  45. snprintf(command, sizeof(command), "pmount -A -s %s %s",
  46. devname, tempname);
  47. log_debug("Executing \"%s\"\n", command);
  48. if (system(command) != 0)
  49. {
  50. log_error("Mount failed\n");
  51. return (0);
  52. }
  53. log_debug("Mount succeeded.\n");
  54. return (1);
  55. }
  56. static char *pusb_volume_mount_path(t_pusb_options *opts, char *udi, DBusConnection* dbus)
  57. {
  58. dbus_bool_t is_mounted;
  59. if (!pusb_hal_get_bool_property(dbus, udi, "DeviceIsMounted", &is_mounted))
  60. {
  61. return (NULL);
  62. }
  63. if (is_mounted != TRUE)
  64. {
  65. log_debug("Device %s is not mounted\n", udi);
  66. return (NULL);
  67. }
  68. int n_mount;
  69. char **mount_pathes = pusb_hal_get_string_array_property(dbus, udi, "DeviceMountPaths", &n_mount);
  70. if (!mount_pathes)
  71. {
  72. log_debug("Failed to retrieve device %s mount path\n", udi);
  73. return (NULL);
  74. }
  75. if (n_mount > 1)
  76. {
  77. log_debug("Device %s is mounted more than once\n", udi);
  78. }
  79. char *mount_path = xstrdup(mount_pathes[0]);
  80. pusb_hal_free_string_array(mount_pathes, n_mount);
  81. log_debug("Device %s is mounted on %s\n", udi, mount_path);
  82. return (mount_path);
  83. }
  84. static char *pusb_volume_probe(t_pusb_options *opts,
  85. DBusConnection *dbus)
  86. {
  87. int maxtries = 0;
  88. int i;
  89. if (!*(opts->device.volume_uuid))
  90. {
  91. log_debug("No UUID configured for device\n");
  92. return (NULL);
  93. }
  94. log_debug("Searching for volume with uuid %s\n", opts->device.volume_uuid);
  95. maxtries = ((opts->probe_timeout * 1000000) / 250000);
  96. for (i = 0; i < maxtries; ++i)
  97. {
  98. char *udi = NULL;
  99. if (i == 1)
  100. log_info("Probing volume (this could take a while)...\n");
  101. udi = pusb_hal_find_item(dbus,
  102. "IdUuid", opts->device.volume_uuid,
  103. NULL);
  104. if (!udi)
  105. {
  106. usleep(250000);
  107. continue;
  108. }
  109. return (udi);
  110. }
  111. return (NULL);
  112. }
  113. char *pusb_volume_get(t_pusb_options *opts, DBusConnection *dbus)
  114. {
  115. char *volume_udi;
  116. char *mount_point;
  117. if (!(volume_udi = pusb_volume_probe(opts, dbus)))
  118. return (NULL);
  119. log_debug("Found volume %s\n", opts->device.volume_uuid);
  120. mount_point = pusb_volume_mount_path(opts, volume_udi, dbus);
  121. if (mount_point)
  122. {
  123. log_debug("Volume is already mounted.\n");
  124. return (mount_point);
  125. }
  126. if (!pusb_volume_mount(opts, volume_udi, dbus))
  127. {
  128. xfree(volume_udi);
  129. return (NULL);
  130. }
  131. mount_point = pusb_volume_mount_path(opts, volume_udi, dbus);
  132. if (!mount_point)
  133. {
  134. log_error("Unable to retrieve %s mount point\n", volume_udi);
  135. pusb_volume_destroy(mount_point);
  136. return (NULL);
  137. }
  138. return (mount_point);
  139. }
  140. void pusb_volume_destroy(char *mntpoint)
  141. {
  142. if (mntpoint && strstr(mntpoint, "pam_usb"))
  143. {
  144. char command[1024];
  145. log_debug("Attempting to umount %s\n",
  146. mntpoint);
  147. snprintf(command, sizeof(command), "pumount %s", mntpoint);
  148. log_debug("Executing \"%s\"\n", command);
  149. if (!system(command))
  150. log_debug("Umount succeeded.\n");
  151. else
  152. log_error("Unable to umount %s\n", mntpoint);
  153. }
  154. xfree(mntpoint);
  155. }