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.

135 lines
3.6 KiB

18 years ago
18 years ago
18 years ago
18 years ago
18 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., 59 Temple
  15. * Place, Suite 330, Boston, MA 02111-1307 USA
  16. */
  17. #include <stdio.h>
  18. #include <unistd.h>
  19. #include "conf.h"
  20. #include "log.h"
  21. #include "device.h"
  22. #include "local.h"
  23. static void pusb_check_conf_dump(t_pusb_options *opts)
  24. {
  25. fprintf(stdout, "Configuration dump:\n");
  26. fprintf(stdout, "enable\t\t\t: %s\n", opts->enable ? "true" : "false");
  27. fprintf(stdout, "debug\t\t\t: %s\n", opts->debug ? "true" : "false");
  28. fprintf(stdout, "quiet\t\t\t: %s\n", opts->quiet ? "true" : "false");
  29. fprintf(stdout, "color_log\t\t: %s\n", opts->color_log ? "true" : "false");
  30. fprintf(stdout, "one_time_pad\t\t: %s\n",
  31. opts->one_time_pad ? "true" : "false");
  32. fprintf(stdout, "probe_timeout\t\t: %d\n", opts->probe_timeout);
  33. fprintf(stdout, "hostname\t\t: %s\n", opts->hostname);
  34. fprintf(stdout, "system_pad_directory\t: %s\n",
  35. opts->system_pad_directory);
  36. fprintf(stdout, "device_pad_directory\t: %s\n",
  37. opts->device_pad_directory);
  38. }
  39. static int pusb_check_perform_authentication(t_pusb_options *opts,
  40. const char *user,
  41. const char *service)
  42. {
  43. int retval;
  44. if (!opts->enable)
  45. {
  46. log_debug("Not enabled, exiting...\n");
  47. return (0);
  48. }
  49. log_info("Authentication request for user \"%s\" (%s)\n",
  50. user, service);
  51. if (!pusb_local_login(opts, user))
  52. {
  53. log_error("Access denied.\n");
  54. return (0);
  55. }
  56. retval = pusb_device_check(opts, user);
  57. if (retval)
  58. log_info("Access granted.\n");
  59. else
  60. log_error("Access denied.\n");
  61. return (retval);
  62. }
  63. static void pusb_check_usage(const char *name)
  64. {
  65. fprintf(stderr, "Usage: %s [-c <config file>] -u <username> -s <service>" \
  66. " [options]\n", name);
  67. fprintf(stderr, "-u and -s are mandatory\n");
  68. fprintf(stderr, "Options can be one or more of the followings:\n");
  69. fprintf(stderr, "\t-a Authenticate: Try to authenticate the user\n");
  70. fprintf(stderr, "\t-d Dump: Parse and dump the settings\n");
  71. fprintf(stderr, "\t-q Quiet: Silent mode\n");
  72. }
  73. int main(int argc, char **argv)
  74. {
  75. t_pusb_options opts;
  76. char *conf_file = PUSB_CONF_FILE;
  77. int quiet = 0;
  78. char *user = NULL;
  79. char *service = NULL;
  80. int opt;
  81. int mode = 0;
  82. extern char *optarg;
  83. while ((opt = getopt(argc, argv, "u:s:c:qad")) != EOF)
  84. {
  85. switch (opt)
  86. {
  87. case 'u':
  88. user = optarg;
  89. break;
  90. case 's':
  91. service = optarg;
  92. break;
  93. case 'c':
  94. conf_file = optarg;
  95. break;
  96. case 'q':
  97. quiet = 1;
  98. break;
  99. case 'a':
  100. mode = 1;
  101. break;
  102. case 'd':
  103. mode = 2;
  104. break;
  105. default:
  106. break;
  107. }
  108. }
  109. if (!user || !service || !mode)
  110. {
  111. pusb_check_usage(argv[0]);
  112. return (1);
  113. }
  114. pusb_conf_init(&opts);
  115. if (!pusb_conf_parse(conf_file, &opts, user, service))
  116. return (1);
  117. if (quiet)
  118. {
  119. opts.quiet = 1;
  120. opts.debug = 0;
  121. }
  122. pusb_log_init(&opts);
  123. if (mode == 1)
  124. return (!pusb_check_perform_authentication(&opts, user, service));
  125. else if (mode == 2)
  126. pusb_check_conf_dump(&opts);
  127. return (0);
  128. }