pwhistory_helper.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * Copyright (c) 2013 Red Hat, Inc.
  3. * Author: Tomas Mraz <tmraz@redhat.com>
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, and the entire permission notice in its entirety,
  10. * including the disclaimer of warranties.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. The name of the author may not be used to endorse or promote
  15. * products derived from this software without specific prior
  16. * written permission.
  17. *
  18. * ALTERNATIVELY, this product may be distributed under the terms of
  19. * the GNU Public License, in which case the provisions of the GPL are
  20. * required INSTEAD OF the above restrictions. (This clause is
  21. * necessary due to a potential bad interaction between the GPL and
  22. * the restrictions contained in a BSD-style copyright.)
  23. *
  24. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  25. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  26. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  27. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
  28. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  29. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  30. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  31. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  32. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  33. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  34. * OF THE POSSIBILITY OF SUCH DAMAGE.
  35. */
  36. #include "config.h"
  37. #include <stdio.h>
  38. #include <stdlib.h>
  39. #include <string.h>
  40. #include <syslog.h>
  41. #include <errno.h>
  42. #include <unistd.h>
  43. #include <signal.h>
  44. #include <security/_pam_types.h>
  45. #include <security/_pam_macros.h>
  46. #include <security/pam_modutil.h>
  47. #include "opasswd.h"
  48. #include "pam_inline.h"
  49. static int
  50. check_history(const char *user, const char *debug)
  51. {
  52. char pass[PAM_MAX_RESP_SIZE + 1];
  53. char *passwords[] = { pass };
  54. int npass;
  55. int dbg = atoi(debug); /* no need to be too fancy here */
  56. int retval;
  57. /* read the password from stdin (a pipe from the pam_pwhistory module) */
  58. npass = pam_read_passwords(STDIN_FILENO, 1, passwords);
  59. if (npass != 1)
  60. { /* is it a valid password? */
  61. helper_log_err(LOG_DEBUG, "no password supplied");
  62. return PAM_AUTHTOK_ERR;
  63. }
  64. retval = check_old_pass(user, pass, dbg);
  65. memset(pass, '\0', PAM_MAX_RESP_SIZE); /* clear memory of the password */
  66. return retval;
  67. }
  68. static int
  69. save_history(const char *user, const char *howmany, const char *debug)
  70. {
  71. int num = atoi(howmany);
  72. int dbg = atoi(debug); /* no need to be too fancy here */
  73. int retval;
  74. retval = save_old_pass(user, num, dbg);
  75. return retval;
  76. }
  77. int
  78. main(int argc, char *argv[])
  79. {
  80. const char *option;
  81. const char *user;
  82. /*
  83. * we establish that this program is running with non-tty stdin.
  84. * this is to discourage casual use.
  85. */
  86. if (isatty(STDIN_FILENO) || argc < 4)
  87. {
  88. fprintf(stderr,
  89. "This binary is not designed for running in this way.\n");
  90. return PAM_SYSTEM_ERR;
  91. }
  92. option = argv[1];
  93. user = argv[2];
  94. if (strcmp(option, "check") == 0 && argc == 4)
  95. return check_history(user, argv[3]);
  96. else if (strcmp(option, "save") == 0 && argc == 5)
  97. return save_history(user, argv[3], argv[4]);
  98. fprintf(stderr, "This binary is not designed for running in this way.\n");
  99. return PAM_SYSTEM_ERR;
  100. }