mod_auth_basic.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. * Copyright (C) 2012 by egnite GmbH
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. Neither the name of the copyright holders nor the names of
  16. * contributors may be used to endorse or promote products derived
  17. * from this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  22. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  23. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  24. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  25. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  26. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  27. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  28. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  29. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  30. * SUCH DAMAGE.
  31. *
  32. * For additional information see http://www.ethernut.de/
  33. */
  34. /*!
  35. * $Id$
  36. */
  37. #include <stdlib.h>
  38. #include <string.h>
  39. #include <pro/uhttp/utils.h>
  40. #include <pro/uhttp/modules/mod_auth_basic.h>
  41. static ISC_LIST(AUTH_BASIC_ENTRY) authInfoList = ISC_LIST_INITIAL_TYPE(AUTH_BASIC_ENTRY);
  42. const AUTH_BASIC_ENTRY *HttpAuthBasicLookup(const char *resource, const char *login, int best)
  43. {
  44. AUTH_BASIC_ENTRY *auth;
  45. int i;
  46. HTTP_ASSERT(resource != NULL);
  47. /* Walk down the list, which is kept in descending order. */
  48. for (auth = ISC_LIST_HEAD(authInfoList); auth; auth = ISC_LIST_NEXT(auth, auth_link)) {
  49. if (best) {
  50. /* Check if the leading part of the resource fits. */
  51. i = strncasecmp(auth->auth_path, resource, strlen(auth->auth_path));
  52. } else {
  53. /* Check if path and resouce fit exactly. */
  54. i = strcasecmp(auth->auth_path, resource);
  55. }
  56. if (i < 0) {
  57. /* Items are in descending order. Thus, all items below
  58. will not match, not even partially. Give up! */
  59. auth = NULL;
  60. break;
  61. }
  62. if (i == 0) {
  63. /* Resource matches. */
  64. if (login) {
  65. /* Login has been given, check for exact macth. */
  66. if (strcmp(auth->auth_login, login) == 0) {
  67. /* This is the requested item. */
  68. break;
  69. }
  70. } else {
  71. /* No login provided, we found the requested item. */
  72. break;
  73. }
  74. }
  75. }
  76. return auth;
  77. }
  78. int HttpRegisterAuthBasic(const char *path, const char *login, const char *realm)
  79. {
  80. int rc = -1;
  81. AUTH_BASIC_ENTRY *auth;
  82. AUTH_BASIC_ENTRY *cur = NULL;
  83. int i = -1;
  84. HTTP_ASSERT(path != NULL);
  85. for (cur = ISC_LIST_HEAD(authInfoList); cur; cur = ISC_LIST_NEXT(cur, auth_link)) {
  86. i = strcasecmp(cur->auth_path, path);
  87. if (i < 0) {
  88. /* Items are in descending order. Thus, all items below
  89. will not match, not even partially. */
  90. break;
  91. }
  92. if (i == 0) {
  93. if (login) {
  94. /* Verify provided login string. */
  95. i = strcmp(cur->auth_login, login);
  96. if (i <= 0) {
  97. if (i == 0) {
  98. /* Silently ignore duplicates. */
  99. rc = 0;
  100. }
  101. break;
  102. }
  103. } else {
  104. /* No login provided, caller wants to unprotect the resource. */
  105. auth = cur;
  106. cur = ISC_LIST_PREV(cur, auth_link);
  107. ISC_LIST_UNLINK_TYPE(authInfoList, auth, auth_link, AUTH_BASIC_ENTRY);
  108. free(auth->auth_path);
  109. free(auth->auth_login);
  110. free(auth->auth_realm);
  111. free(auth);
  112. /* Indicate success, but continue searching additional
  113. logins for this resource. */
  114. rc = 0;
  115. }
  116. }
  117. }
  118. if (login && i) {
  119. /* Create a new entry. */
  120. auth = calloc(1, sizeof(AUTH_BASIC_ENTRY));
  121. if (auth) {
  122. auth->auth_path = strdup(path);
  123. if (auth->auth_path) {
  124. auth->auth_login = strdup(login);
  125. if (auth->auth_login) {
  126. if (realm) {
  127. auth->auth_realm = strdup(realm);
  128. } else {
  129. auth->auth_realm = NULL;
  130. }
  131. if (cur) {
  132. /* Insert entry, maintaining descending sort order. */
  133. ISC_LIST_INSERTBEFORE(authInfoList, cur, auth, auth_link);
  134. } else {
  135. /* Append entry to the list. */
  136. ISC_LIST_APPEND(authInfoList, auth, auth_link);
  137. }
  138. /* Indicate success and let the internal client handler
  139. use our validator. */
  140. rc = 0;
  141. httpd_auth_validator = HttpAuthBasicValidate;
  142. } else {
  143. free(auth->auth_path);
  144. }
  145. } else {
  146. free(auth);
  147. }
  148. }
  149. }
  150. return rc;
  151. }
  152. int HttpAuthBasicValidate(HTTPD_SESSION *hs)
  153. {
  154. #if HTTP_VERSION >= 0x10
  155. int rc = -1;
  156. const AUTH_BASIC_ENTRY *auth;
  157. HTTP_ASSERT(hs != NULL);
  158. /* Check if this resource is protected. */
  159. auth = HttpAuthBasicLookup(hs->s_req.req_url, NULL, 1);
  160. if (auth == NULL) {
  161. /* Unprotected resource, grant access. */
  162. rc = 0;
  163. } else {
  164. /* Protected resource, check authentication. */
  165. hs->s_req.req_realm = strdup(auth->auth_realm ? auth->auth_realm : auth->auth_path);
  166. if (hs->s_req.req_auth) {
  167. /* Accept basic authentication only. */
  168. if (strncasecmp(hs->s_req.req_auth, "Basic ", 6) == 0) {
  169. HttpDecodeBase64(hs->s_req.req_auth + 6);
  170. if (HttpAuthBasicLookup(auth->auth_path, hs->s_req.req_auth + 6, 0)) {
  171. rc = 0;
  172. }
  173. }
  174. }
  175. }
  176. return rc;
  177. #else
  178. return 0;
  179. #endif
  180. }