auth.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * uhttpd - Tiny single-threaded httpd
  3. *
  4. * Copyright (C) 2010-2012 Jo-Philipp Wich <xm@subsignal.org>
  5. * Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. */
  19. #include "uhttpd.h"
  20. static LIST_HEAD(auth_realms);
  21. void uh_auth_add(const char *path, const char *user, const char *pass)
  22. {
  23. struct auth_realm *new = NULL;
  24. struct passwd *pwd;
  25. const char *new_pass = NULL;
  26. char *dest_path, *dest_user, *dest_pass;
  27. #ifdef HAVE_SHADOW
  28. struct spwd *spwd;
  29. #endif
  30. /* given password refers to a passwd entry */
  31. if ((strlen(pass) > 3) && !strncmp(pass, "$p$", 3)) {
  32. #ifdef HAVE_SHADOW
  33. /* try to resolve shadow entry */
  34. spwd = getspnam(&pass[3]);
  35. if (spwd)
  36. new_pass = spwd->sp_pwdp;
  37. #endif
  38. if (!new_pass) {
  39. pwd = getpwnam(&pass[3]);
  40. if (pwd && pwd->pw_passwd && pwd->pw_passwd[0] &&
  41. pwd->pw_passwd[0] != '!')
  42. new_pass = pwd->pw_passwd;
  43. }
  44. } else {
  45. new_pass = pass;
  46. }
  47. if (!new_pass || !new_pass[0])
  48. return;
  49. new = calloc_a(sizeof(*new),
  50. &dest_path, strlen(path) + 1,
  51. &dest_user, strlen(user) + 1,
  52. &dest_pass, strlen(new_pass) + 1);
  53. if (!new)
  54. return;
  55. new->path = strcpy(dest_path, path);
  56. new->user = strcpy(dest_user, user);
  57. new->pass = strcpy(dest_pass, new_pass);
  58. list_add(&new->list, &auth_realms);
  59. }
  60. bool uh_auth_check(struct client *cl, struct path_info *pi)
  61. {
  62. struct http_request *req = &cl->request;
  63. struct auth_realm *realm;
  64. bool user_match = false;
  65. char *user = NULL;
  66. char *pass = NULL;
  67. int plen;
  68. if (pi->auth && !strncasecmp(pi->auth, "Basic ", 6)) {
  69. const char *auth = pi->auth + 6;
  70. uh_b64decode(uh_buf, sizeof(uh_buf), auth, strlen(auth));
  71. pass = strchr(uh_buf, ':');
  72. if (pass) {
  73. user = uh_buf;
  74. *pass++ = 0;
  75. }
  76. }
  77. req->realm = NULL;
  78. plen = strlen(pi->name);
  79. list_for_each_entry(realm, &auth_realms, list) {
  80. int rlen = strlen(realm->path);
  81. if (plen < rlen)
  82. continue;
  83. if (strncasecmp(pi->name, realm->path, rlen) != 0)
  84. continue;
  85. req->realm = realm;
  86. if (!user)
  87. break;
  88. if (strcmp(user, realm->user) != 0)
  89. continue;
  90. user_match = true;
  91. break;
  92. }
  93. if (!req->realm)
  94. return true;
  95. if (user_match && !strcmp(crypt(pass, realm->pass), realm->pass))
  96. return true;
  97. uh_http_header(cl, 401, "Authorization Required");
  98. ustream_printf(cl->us,
  99. "WWW-Authenticate: Basic realm=\"%s\"\r\n"
  100. "Content-Type: text/plain\r\n\r\n",
  101. conf.realm);
  102. uh_chunk_printf(cl, "Authorization Required\n");
  103. uh_request_done(cl);
  104. return false;
  105. }