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.

152 lines
4.0 KiB

18 years ago
18 years ago
18 years ago
  1. /*
  2. * Copyright (c) 2003-2006 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., 59 Temple
  15. * Place, Suite 330, Boston, MA 02111-1307 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. #include <sys/mount.h>
  24. #include <libhal-storage.h>
  25. #include "conf.h"
  26. #include "log.h"
  27. #include "hal.h"
  28. #include "volume.h"
  29. static int pusb_volume_mount(t_pusb_options *opts, LibHalVolume **volume,
  30. LibHalContext *ctx)
  31. {
  32. char command[1024];
  33. char tempname[32];
  34. const char *devname;
  35. const char *udi;
  36. const char *fs;
  37. snprintf(tempname, sizeof(tempname), "pam_usb%d", getpid());
  38. if (!(devname = libhal_volume_get_device_file(*volume)))
  39. {
  40. log_error("Unable to retrieve device filename\n");
  41. return (0);
  42. }
  43. fs = libhal_volume_get_fstype(*volume);
  44. log_debug("Attempting to mount device %s with label %s\n",
  45. devname, tempname);
  46. if (!fs)
  47. snprintf(command, sizeof(command), "pmount -s %s %s",
  48. devname, tempname);
  49. else
  50. snprintf(command, sizeof(command), "pmount -s -t %s %s %s",
  51. fs, devname, tempname);
  52. log_debug("Executing \"%s\"\n", command);
  53. if (system(command) != 0)
  54. {
  55. log_error("Mount failed\n");
  56. return (0);
  57. }
  58. udi = libhal_volume_get_udi(*volume);
  59. if (!udi)
  60. {
  61. log_error("Unable to retrieve volume UDI\n");
  62. return (0);
  63. }
  64. udi = strdup(udi);
  65. libhal_volume_free(*volume);
  66. *volume = libhal_volume_from_udi(ctx, udi);
  67. free((char *)udi);
  68. log_debug("Mount succeeded.\n");
  69. return (1);
  70. }
  71. static LibHalVolume *pusb_volume_probe(t_pusb_options *opts,
  72. LibHalContext *ctx)
  73. {
  74. LibHalVolume *volume = NULL;
  75. int maxtries = 0;
  76. int i;
  77. if (!*(opts->device.volume_uuid))
  78. {
  79. log_debug("No UUID configured for device\n");
  80. return (NULL);
  81. }
  82. log_debug("Searching for volume with uuid %s\n", opts->device.volume_uuid);
  83. maxtries = ((opts->probe_timeout * 1000000) / 250000);
  84. for (i = 0; i < maxtries; ++i)
  85. {
  86. char *udi = NULL;
  87. if (i == 1)
  88. log_info("Probing volume (this could take a while)...\n");
  89. udi = pusb_hal_find_item(ctx,
  90. "volume.uuid", opts->device.volume_uuid,
  91. NULL);
  92. if (!udi)
  93. {
  94. usleep(250000);
  95. continue;
  96. }
  97. volume = libhal_volume_from_udi(ctx, udi);
  98. libhal_free_string(udi);
  99. if (!libhal_volume_should_ignore(volume))
  100. return (volume);
  101. libhal_volume_free(volume);
  102. usleep(250000);
  103. }
  104. return (NULL);
  105. }
  106. LibHalVolume *pusb_volume_get(t_pusb_options *opts, LibHalContext *ctx)
  107. {
  108. LibHalVolume *volume;
  109. if (!(volume = pusb_volume_probe(opts, ctx)))
  110. return (NULL);
  111. log_debug("Found volume %s\n", opts->device.volume_uuid);
  112. if (libhal_volume_is_mounted(volume))
  113. {
  114. log_debug("Volume is already mounted.\n");
  115. return (volume);
  116. }
  117. if (!pusb_volume_mount(opts, &volume, ctx))
  118. {
  119. libhal_volume_free(volume);
  120. return (NULL);
  121. }
  122. return (volume);
  123. }
  124. void pusb_volume_destroy(LibHalVolume *volume)
  125. {
  126. const char *mntpoint;
  127. mntpoint = libhal_volume_get_mount_point(volume);
  128. if (mntpoint && strstr(mntpoint, "pam_usb"))
  129. {
  130. char command[1024];
  131. log_debug("Attempting to umount %s\n",
  132. mntpoint);
  133. snprintf(command, sizeof(command), "pumount %s", mntpoint);
  134. log_debug("Executing \"%s\"\n", command);
  135. if (!system(command))
  136. log_debug("Umount succeeded.\n");
  137. else
  138. log_error("Unable to umount %s\n", mntpoint);
  139. }
  140. libhal_volume_free(volume);
  141. }