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.

98 lines
2.2 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 <syslog.h>
  20. #include <stdarg.h>
  21. #include "conf.h"
  22. #include "log.h"
  23. static t_pusb_options *pusb_opts = NULL;
  24. static void pusb_log_syslog(int level, const char *format, va_list ap)
  25. {
  26. openlog("pam_usb", LOG_PID, LOG_AUTH);
  27. vsyslog(level, format, ap);
  28. closelog();
  29. }
  30. static void pusb_log_output(int level, const char *format, va_list ap)
  31. {
  32. if (!isatty(fileno(stdin)))
  33. return ;
  34. if (pusb_opts && !pusb_opts->quiet)
  35. {
  36. if (pusb_opts && pusb_opts->color_log)
  37. {
  38. if (level == LOG_ERR)
  39. fprintf(stderr, "\033[01;31m*\033[00m ");
  40. else if (level == LOG_NOTICE)
  41. fprintf(stderr, "\033[01;32m*\033[00m ");
  42. }
  43. else
  44. fprintf(stderr, "* ");
  45. vfprintf(stderr, format, ap);
  46. }
  47. }
  48. void __log_debug(const char *file, int line, const char *fmt, ...)
  49. {
  50. va_list ap;
  51. if (!pusb_opts || !pusb_opts->debug)
  52. return ;
  53. fprintf(stderr, "[%s:%03d] ", file, line);
  54. va_start(ap, fmt);
  55. vfprintf(stderr, fmt, ap);
  56. va_end(ap);
  57. va_start(ap, fmt);
  58. pusb_log_syslog(LOG_DEBUG, fmt, ap);
  59. va_end(ap);
  60. }
  61. void log_error(const char *fmt, ...)
  62. {
  63. va_list ap;
  64. va_start(ap, fmt);
  65. pusb_log_syslog(LOG_ERR, fmt, ap);
  66. va_end(ap);
  67. va_start(ap, fmt);
  68. pusb_log_output(LOG_ERR, fmt, ap);
  69. va_end(ap);
  70. }
  71. void log_info(const char *fmt, ...)
  72. {
  73. va_list ap;
  74. va_start(ap, fmt);
  75. pusb_log_syslog(LOG_NOTICE, fmt, ap);
  76. va_end(ap);
  77. va_start(ap, fmt);
  78. pusb_log_output(LOG_NOTICE, fmt, ap);
  79. va_end(ap);
  80. }
  81. void pusb_log_init(t_pusb_options *opts)
  82. {
  83. pusb_opts = opts;
  84. }