auth.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * uhttpd - Tiny single-threaded httpd
  3. *
  4. * Copyright (C) 2010-2013 Jo-Philipp Wich <xm@subsignal.org>
  5. * Copyright (C) 2013 Felix Fietkau <nbd@openwrt.org>
  6. *
  7. * Permission to use, copy, modify, and/or distribute this software for any
  8. * purpose with or without fee is hereby granted, provided that the above
  9. * copyright notice and this permission notice appear in all copies.
  10. *
  11. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  12. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  13. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  14. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  15. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  16. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  17. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  18. */
  19. #define _GNU_SOURCE
  20. #define _XOPEN_SOURCE 700
  21. #include <strings.h>
  22. #ifdef HAVE_SHADOW
  23. #include <shadow.h>
  24. #endif
  25. #include "uhttpd.h"
  26. static LIST_HEAD(auth_realms);
  27. void uh_auth_add(const char *path, const char *user, const char *pass)
  28. {
  29. struct auth_realm *new = NULL;
  30. struct passwd *pwd;
  31. const char *new_pass = NULL;
  32. char *dest_path, *dest_user, *dest_pass;
  33. #ifdef HAVE_SHADOW
  34. struct spwd *spwd;
  35. #endif
  36. /* given password refers to a passwd entry */
  37. if ((strlen(pass) > 3) && !strncmp(pass, "$p$", 3)) {
  38. #ifdef HAVE_SHADOW
  39. /* try to resolve shadow entry */
  40. spwd = getspnam(&pass[3]);
  41. if (spwd)
  42. new_pass = spwd->sp_pwdp;
  43. #endif
  44. if (!new_pass) {
  45. pwd = getpwnam(&pass[3]);
  46. if (pwd && pwd->pw_passwd && pwd->pw_passwd[0] &&
  47. pwd->pw_passwd[0] != '!')
  48. new_pass = pwd->pw_passwd;
  49. }
  50. } else {
  51. new_pass = pass;
  52. }
  53. if (!new_pass || !new_pass[0])
  54. return;
  55. new = calloc_a(sizeof(*new),
  56. &dest_path, strlen(path) + 1,
  57. &dest_user, strlen(user) + 1,
  58. &dest_pass, strlen(new_pass) + 1);
  59. if (!new)
  60. return;
  61. new->path = strcpy(dest_path, path);
  62. new->user = strcpy(dest_user, user);
  63. new->pass = strcpy(dest_pass, new_pass);
  64. list_add(&new->list, &auth_realms);
  65. }
  66. bool uh_auth_check(struct client *cl, const char *path, const char *auth,
  67. char **uptr, char **pptr)
  68. {
  69. struct http_request *req = &cl->request;
  70. struct auth_realm *realm;
  71. bool user_match = false;
  72. char *user = NULL;
  73. char *pass = NULL;
  74. int plen;
  75. if (uptr)
  76. *uptr = NULL;
  77. if (pptr)
  78. *pptr = NULL;
  79. if (auth && !strncasecmp(auth, "Basic ", 6)) {
  80. auth += 6;
  81. uh_b64decode(uh_buf, sizeof(uh_buf), auth, strlen(auth));
  82. pass = strchr(uh_buf, ':');
  83. if (pass) {
  84. user = uh_buf;
  85. *pass++ = 0;
  86. }
  87. }
  88. req->realm = NULL;
  89. plen = strlen(path);
  90. list_for_each_entry(realm, &auth_realms, list) {
  91. int rlen = strlen(realm->path);
  92. if (plen < rlen)
  93. continue;
  94. if (strncasecmp(path, realm->path, rlen) != 0)
  95. continue;
  96. req->realm = realm;
  97. if (!user)
  98. break;
  99. if (strcmp(user, realm->user) != 0)
  100. continue;
  101. user_match = true;
  102. break;
  103. }
  104. if (!req->realm)
  105. return true;
  106. if (user_match &&
  107. (!strcmp(pass, realm->pass) ||
  108. !strcmp(crypt(pass, realm->pass), realm->pass))) {
  109. if (uptr)
  110. *uptr = user;
  111. if (pptr)
  112. *pptr = pass;
  113. return true;
  114. }
  115. uh_http_header(cl, 401, "Authorization Required");
  116. ustream_printf(cl->us,
  117. "WWW-Authenticate: Basic realm=\"%s\"\r\n"
  118. "Content-Type: text/plain\r\n\r\n",
  119. conf.realm);
  120. uh_chunk_printf(cl, "Authorization Required\n");
  121. uh_request_done(cl);
  122. return false;
  123. }