htpasswd.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. * Copyright (c) 2018 Markus Hennecke <markus-hennecke@markus-hennecke.de>
  3. *
  4. * Permission to use, copy, modify, and distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #include <err.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include <unistd.h>
  20. #include "helper.h"
  21. #include "htpasswd.h"
  22. static struct htpasswd *_htpasswd_init(struct memmap *);
  23. struct htpasswd *
  24. _htpasswd_init(struct memmap *_file)
  25. {
  26. struct htpasswd *htp = calloc(1, sizeof(struct htpasswd));
  27. htp->htpasswd = strndup(_file->data, _file->size);
  28. if (htp->htpasswd == NULL)
  29. err(1, NULL);
  30. char *s = htp->htpasswd;
  31. int i = 0;
  32. while (s && *s != '\0' && i < MAX_HTPASSWD_ENTRIES) {
  33. htp->usernames[i] = s;
  34. // Find a ':'
  35. if ((s = strchr(s, ':')) != NULL) {
  36. *s++ = '\0';
  37. htp->hashes[i++] = s;
  38. s = strchr(s, '\n');
  39. if (s != NULL)
  40. *s++ = '\0';
  41. } else {
  42. break;
  43. }
  44. }
  45. return htp;
  46. }
  47. struct htpasswd *
  48. htpasswd_init(const char *_filename)
  49. {
  50. struct htpasswd *result = NULL;
  51. struct memmap *file = memmap_new(_filename);
  52. if (file) {
  53. result = _htpasswd_init(file);
  54. memmap_free(file);
  55. }
  56. return result;
  57. }
  58. struct htpasswd *
  59. htpasswd_init_at(int _fd, const char *_filename)
  60. {
  61. struct htpasswd *result = NULL;
  62. struct memmap *file = memmap_new_at(_fd, _filename);
  63. if (file) {
  64. result = _htpasswd_init(file);
  65. memmap_free(file);
  66. }
  67. return result;
  68. }
  69. void
  70. htpasswd_free(struct htpasswd *_htpasswd)
  71. {
  72. if (_htpasswd) {
  73. if (_htpasswd->filesize)
  74. freezero(_htpasswd->htpasswd, _htpasswd->filesize);
  75. free(_htpasswd);
  76. }
  77. }
  78. bool
  79. htpasswd_check_password(struct htpasswd *_htp, const char *_username,
  80. const char *_pass)
  81. {
  82. if (_htp== NULL)
  83. return false;
  84. int i = 0;
  85. while (_htp->usernames[i] && i < MAX_HTPASSWD_ENTRIES) {
  86. if (strcmp(_htp->usernames[i], _username) == 0) {
  87. return (crypt_checkpass(_pass, _htp->hashes[i]) == 0);
  88. }
  89. i++;
  90. }
  91. return false;
  92. }