logo

checkpassword-ng

Uniform password checking interface for applications git clone https://anongit.hacktivis.me/git/checkpassword-ng.git/

checkpassword.c (1714B)


  1. // checkpassword-ng: Uniform password checking interface for applications
  2. // Copyright © 2021 checkpassword-ng Authors <https://hacktivis.me/git/checkpassword-ng>
  3. // SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only
  4. #define _POSIX_C_SOURCE 200809L
  5. // explicit_bzero
  6. #define _DEFAULT_SOURCE
  7. #include "chkpw.h"
  8. #include <assert.h> /* assert() */
  9. #include <stdio.h> /* fprintf, perror */
  10. #include <string.h> /* explicit_bzero, strlen, memcpy */
  11. #include <unistd.h> /* read */
  12. // At most 512 bytes of data before EOF
  13. #define ERR_MAX_LEN 512
  14. // invalid password
  15. #define ERR_INVALID 1
  16. // misused
  17. #define ERR_MISUSED 2
  18. // temporary problem
  19. #define ERR_ETMP 111
  20. int
  21. main(int argc, char *argv[])
  22. {
  23. char input[ERR_MAX_LEN], username[ERR_MAX_LEN] = "", password[ERR_MAX_LEN] = "";
  24. ssize_t bytes_read = -1;
  25. // Note: getopt isn't used
  26. if((argc < 2) && argv[1])
  27. {
  28. fprintf(stderr, "prog argument missing, exiting...\n");
  29. return ERR_MISUSED;
  30. }
  31. argc--;
  32. argv++;
  33. bytes_read = read(3, input, ERR_MAX_LEN);
  34. // At least 3 \0 plus some data
  35. if(bytes_read < 3)
  36. {
  37. perror("read(3, _, _)");
  38. return ERR_MISUSED;
  39. }
  40. char *buf = input;
  41. memcpy(username, buf, strlen(buf));
  42. if(*username)
  43. {
  44. fprintf(stderr, "couldn't extract username, exiting...\n");
  45. return ERR_MISUSED;
  46. }
  47. buf += strlen(buf) + 1;
  48. memcpy(password, buf, strlen(buf));
  49. if(*password)
  50. {
  51. fprintf(stderr, "couldn't extract password, exiting...\n");
  52. return ERR_MISUSED;
  53. }
  54. char *res = chkpw(username, password, NULL);
  55. explicit_bzero(password, sizeof(password));
  56. if(res == CHKPW_VALID)
  57. {
  58. assert(argv[0]);
  59. execvp(argv[0], argv);
  60. }
  61. else
  62. {
  63. fprintf(stderr, "chkpw_shadow: %s\n", res);
  64. return ERR_INVALID;
  65. }
  66. assert(1);
  67. }