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.

164 lines
4.5 KiB

  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 <unistd.h>
  19. #include <string.h>
  20. #include <getopt.h>
  21. #include "conf.h"
  22. #include "log.h"
  23. #include "device.h"
  24. #include "local.h"
  25. static void pusb_check_conf_dump(t_pusb_options *opts, const char *username,
  26. const char *service)
  27. {
  28. fprintf(stdout, "Configuration dump for user %s (service: %s):\n",
  29. username, service);
  30. fprintf(stdout, "enable\t\t\t: %s\n", opts->enable ? "true" : "false");
  31. fprintf(stdout, "debug\t\t\t: %s\n", opts->debug ? "true" : "false");
  32. fprintf(stdout, "quiet\t\t\t: %s\n", opts->quiet ? "true" : "false");
  33. fprintf(stdout, "color_log\t\t: %s\n", opts->color_log ? "true" : "false");
  34. fprintf(stdout, "one_time_pad\t\t: %s\n",
  35. opts->one_time_pad ? "true" : "false");
  36. fprintf(stdout, "deny_remote\t\t: %s\n",
  37. opts->deny_remote ? "true" : "false");
  38. fprintf(stdout, "pad_expiration\t\t: %u seconds\n", (unsigned int)opts->pad_expiration);
  39. fprintf(stdout, "probe_timeout\t\t: %d seconds\n", (unsigned int)opts->probe_timeout);
  40. fprintf(stdout, "hostname\t\t: %s\n", opts->hostname);
  41. fprintf(stdout, "system_pad_directory\t: %s\n",
  42. opts->system_pad_directory);
  43. fprintf(stdout, "device_pad_directory\t: %s\n",
  44. opts->device_pad_directory);
  45. }
  46. static int pusb_check_perform_authentication(t_pusb_options *opts,
  47. const char *user,
  48. const char *service)
  49. {
  50. int retval;
  51. if (!opts->enable)
  52. {
  53. log_debug("Not enabled, exiting...\n");
  54. return (0);
  55. }
  56. log_info("Authentication request for user \"%s\" (%s)\n",
  57. user, service);
  58. if (!pusb_local_login(opts, user))
  59. {
  60. log_error("Access denied.\n");
  61. return (0);
  62. }
  63. retval = pusb_device_check(opts, user);
  64. if (retval)
  65. log_info("Access granted.\n");
  66. else
  67. log_error("Access denied.\n");
  68. return (retval);
  69. }
  70. static void pusb_check_usage(const char *name)
  71. {
  72. fprintf(stderr, "Usage: %s [--help] [--debug] [--config=path] [--service=name] [--dump] [--quiet] [--debug]" \
  73. " <username>\n", name);
  74. }
  75. int main(int argc, char **argv)
  76. {
  77. t_pusb_options opts;
  78. char *conf_file = PUSB_CONF_FILE;
  79. char *service = "pamusb-check";
  80. char *user = NULL;
  81. int quiet = 0;
  82. int dump = 0;
  83. int debug = 0;
  84. int opt;
  85. int opt_index = 0;
  86. extern char *optarg;
  87. char *short_options = "hc:s:dqD";
  88. struct option long_options[] = {
  89. { "help", 0, 0, 0 },
  90. { "config", 1, 0, 0 },
  91. { "service", 1, 0, 0 },
  92. { "dump", 0, 0, 0 },
  93. { "quiet", 0, 0, 0 },
  94. { "debug", 0, 0, 0 },
  95. { 0, 0, 0, 0 }
  96. };
  97. while ((opt = getopt_long(argc, argv, short_options, long_options,
  98. &opt_index)) != EOF)
  99. {
  100. if (opt == 'h' || (!opt && !strcmp(long_options[opt_index].name, "help")))
  101. {
  102. pusb_check_usage(argv[0]);
  103. return (1);
  104. }
  105. else if (opt == 'c' || (!opt && !strcmp(long_options[opt_index].name, "config")))
  106. conf_file = optarg;
  107. else if (opt == 's' || (!opt && !strcmp(long_options[opt_index].name, "service")))
  108. service = optarg;
  109. else if (opt == 'd' || (!opt && !strcmp(long_options[opt_index].name, "dump")))
  110. dump = 1;
  111. else if (opt == 'q' || (!opt && !strcmp(long_options[opt_index].name, "quiet")))
  112. quiet = 1;
  113. else if (opt == 'D' || (!opt && !strcmp(long_options[opt_index].name, "debug")))
  114. debug = 1;
  115. else if (opt == '?')
  116. {
  117. pusb_check_usage(argv[0]);
  118. return (1);
  119. }
  120. }
  121. if ((argc - 1) == optind)
  122. user = argv[optind];
  123. else
  124. {
  125. pusb_check_usage(argv[0]);
  126. return (1);
  127. }
  128. if (quiet && debug)
  129. {
  130. fprintf(stderr, "Error: You cannot use --quiet and --debug together.");
  131. return (1);
  132. }
  133. pusb_log_init(&opts);
  134. if (!pusb_conf_init(&opts))
  135. return (1);
  136. if (!pusb_conf_parse(conf_file, &opts, user, service))
  137. return (1);
  138. if (quiet)
  139. {
  140. opts.quiet = 1;
  141. opts.debug = 0;
  142. }
  143. else if (debug)
  144. {
  145. opts.quiet = 0;
  146. opts.debug = 1;
  147. }
  148. if (dump)
  149. {
  150. pusb_check_conf_dump(&opts, user, service);
  151. return (1);
  152. }
  153. return (!pusb_check_perform_authentication(&opts, user, service));
  154. }