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.

191 lines
4.1 KiB

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 <string.h>
  18. #include <stdarg.h>
  19. #include <dbus/dbus.h>
  20. #include <libhal-storage.h>
  21. #include "log.h"
  22. DBusConnection *pusb_hal_dbus_connect(void)
  23. {
  24. DBusConnection *dbus = NULL;
  25. DBusError error;
  26. dbus_error_init(&error);
  27. if (!(dbus = dbus_bus_get(DBUS_BUS_SYSTEM, &error)))
  28. {
  29. log_error("Cannot connect to system bus: %s\n",
  30. error.message);
  31. dbus_error_free(&error);
  32. return (NULL);
  33. }
  34. return (dbus);
  35. }
  36. void pusb_hal_dbus_disconnect(DBusConnection *dbus)
  37. {
  38. dbus_connection_close(dbus);
  39. dbus_connection_unref(dbus);
  40. dbus_shutdown();
  41. }
  42. LibHalContext *pusb_hal_init(DBusConnection *dbus)
  43. {
  44. DBusError error;
  45. LibHalContext *ctx = NULL;
  46. dbus_error_init(&error);
  47. if (!(ctx = libhal_ctx_new()))
  48. {
  49. log_error("Failed to create a HAL context\n");
  50. return (NULL);
  51. }
  52. if (!libhal_ctx_set_dbus_connection(ctx, dbus))
  53. {
  54. log_error("Failed to attach dbus connection to hal\n");
  55. libhal_ctx_free(ctx);
  56. return (NULL);
  57. }
  58. if (!libhal_ctx_init(ctx, &error))
  59. {
  60. log_error("libhal_ctx_init: %s\n", error.name, error.message);
  61. libhal_ctx_free(ctx);
  62. return (NULL);
  63. }
  64. return (ctx);
  65. }
  66. void pusb_hal_destroy(LibHalContext *ctx)
  67. {
  68. libhal_ctx_free(ctx);
  69. }
  70. char *pusb_hal_get_property(LibHalContext *ctx,
  71. const char *udi,
  72. const char *name)
  73. {
  74. DBusError error;
  75. char *data;
  76. dbus_error_init(&error);
  77. data = libhal_device_get_property_string(ctx, udi,
  78. name, &error);
  79. if (!data)
  80. {
  81. log_debug("%s\n", error.message);
  82. dbus_error_free(&error);
  83. return (NULL);
  84. }
  85. return (data);
  86. }
  87. int pusb_hal_check_property(LibHalContext *ctx,
  88. const char *udi,
  89. const char *name,
  90. const char *value)
  91. {
  92. char *data;
  93. int retval;
  94. data = pusb_hal_get_property(ctx, udi, name);
  95. if (!data)
  96. return (0);
  97. retval = (strcmp(data, value) == 0);
  98. libhal_free_string(data);
  99. return (retval);
  100. }
  101. char **pusb_hal_find_all_items(LibHalContext *ctx,
  102. const char *property,
  103. const char *value,
  104. int *count)
  105. {
  106. DBusError error;
  107. char **devices;
  108. int n_devices;
  109. dbus_error_init(&error);
  110. *count = 0;
  111. devices = libhal_manager_find_device_string_match(ctx,
  112. property,
  113. value,
  114. &n_devices,
  115. &error);
  116. if (!devices)
  117. {
  118. log_error("Unable to find item \"%s\": %s\n", property,
  119. error.message);
  120. dbus_error_free(&error);
  121. return (NULL);
  122. }
  123. if (!n_devices)
  124. {
  125. libhal_free_string_array(devices);
  126. return (NULL);
  127. }
  128. *count = n_devices;
  129. return (devices);
  130. }
  131. char *pusb_hal_find_item(LibHalContext *ctx,
  132. const char *property,
  133. const char *value,
  134. ...)
  135. {
  136. char **devices;
  137. int n_devices;
  138. char *udi = NULL;
  139. va_list ap;
  140. int i;
  141. devices = pusb_hal_find_all_items(ctx, property, value, &n_devices);
  142. if (!devices)
  143. return (NULL);
  144. if (!n_devices)
  145. return (NULL);
  146. for (i = 0; i < n_devices; ++i)
  147. {
  148. char *key = NULL;
  149. int match = 1;
  150. va_start(ap, value);
  151. while ((key = va_arg(ap, char *)))
  152. {
  153. char *value = NULL;
  154. value = va_arg(ap, char *);
  155. if (!pusb_hal_check_property(ctx, devices[i],
  156. key, value))
  157. {
  158. match = 0;
  159. break;
  160. }
  161. match = 1;
  162. }
  163. if (match)
  164. {
  165. udi = strdup(devices[i]);
  166. break;
  167. }
  168. va_end(ap);
  169. }
  170. libhal_free_string_array(devices);
  171. return (udi);
  172. }