auth.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /*
  2. * Copyright (C) 2001-2003 by egnite Software GmbH. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of the copyright holders nor the names of
  14. * contributors may be used to endorse or promote products derived
  15. * from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  18. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  20. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  21. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  22. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  23. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  24. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  25. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  26. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  27. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  28. * SUCH DAMAGE.
  29. *
  30. * For additional information see http://www.ethernut.de/
  31. */
  32. /*
  33. * $Log$
  34. * Revision 1.7 2009/03/07 00:18:13 olereinhardt
  35. * Use base64 decoder from gorp/base64/base64_decode.c instead from dencode.c
  36. *
  37. * Revision 1.6 2009/02/13 14:52:05 haraldkipp
  38. * Include memdebug.h for heap management debugging support.
  39. *
  40. * Revision 1.5 2009/02/06 15:40:29 haraldkipp
  41. * Using newly available strdup() and calloc().
  42. * Replaced NutHeap routines by standard malloc/free.
  43. * Replaced pointer value 0 by NULL.
  44. *
  45. * Revision 1.4 2008/07/17 11:29:15 olereinhardt
  46. * Allow authentication for subdirectories
  47. *
  48. * Revision 1.3 2006/10/08 16:43:53 haraldkipp
  49. * Authentication info depended on static memory kept by the caller. Now a
  50. * local copy is held and NutClearAuth (which should have been named
  51. * NutHttpAuthClear, btw.) works correctly.
  52. *
  53. * Revision 1.2 2006/08/25 13:42:16 olereinhardt
  54. * added NutClearAuth(void); Thanks to Peter Sondermanns
  55. *
  56. * Revision 1.1.1.1 2003/05/09 14:41:56 haraldkipp
  57. * Initial using 3.2.1
  58. *
  59. * Revision 1.7 2003/02/04 18:17:31 harald
  60. * Version 3 released
  61. *
  62. * Revision 1.6 2002/06/26 17:29:49 harald
  63. * First pre-release with 2.4 stack
  64. *
  65. */
  66. #include <sys/heap.h>
  67. #include <stdlib.h>
  68. #include <string.h>
  69. #include <memdebug.h>
  70. #include <pro/httpd.h>
  71. #include <gorp/base64.h>
  72. /*!
  73. * \addtogroup xgHTTPD
  74. */
  75. /*@{*/
  76. AUTHINFO *authList = 0;
  77. /*!
  78. * \brief Look up an authorization entry.
  79. */
  80. static AUTHINFO *NutHttpAuthLookup(const char *dirname, const char *login)
  81. {
  82. AUTHINFO *auth;
  83. for (auth = authList; auth; auth = auth->auth_next) {
  84. if (dirname && (strstr(dirname, auth->auth_dirname) != dirname))
  85. continue;
  86. if (login && strcmp(login, auth->auth_login))
  87. continue;
  88. break;
  89. }
  90. return auth;
  91. }
  92. /*!
  93. * \brief Register an authorization entry.
  94. *
  95. * Protect a specified directory from unauthorized access.
  96. *
  97. * \warning Directories not registered by this function are
  98. * accessible by anyone.
  99. *
  100. * \param dirname Name of the directory to protect.
  101. * \param login Required login to access this directory. This
  102. * string must contain a user name, followed by
  103. * a colon followed by an uncrypted password.
  104. *
  105. * \return 0 on success, -1 otherwise.
  106. */
  107. int NutRegisterAuth(const char *dirname, const char *login)
  108. {
  109. AUTHINFO *auth;
  110. /* Allocate a new list element. */
  111. if ((auth = malloc(sizeof(AUTHINFO))) != NULL) {
  112. auth->auth_next = authList;
  113. /* Allocate the path component. */
  114. if ((auth->auth_dirname = strdup(dirname)) != NULL) {
  115. /* Allocate the login component. */
  116. if ((auth->auth_login = strdup(login)) != NULL) {
  117. /* Success. Add element to the list and return. */
  118. authList = auth;
  119. return 0;
  120. }
  121. /* Allocation failed. */
  122. free(auth->auth_dirname);
  123. }
  124. free(auth);
  125. }
  126. return -1;
  127. }
  128. /*!
  129. * \brief Clear all authorization entries.
  130. *
  131. * Clears all authorization entries and frees the used ressouces.
  132. *
  133. */
  134. void NutClearAuth(void)
  135. {
  136. AUTHINFO *auth;
  137. while (authList) {
  138. auth = authList;
  139. authList = auth->auth_next;
  140. free(auth->auth_dirname);
  141. free(auth->auth_login);
  142. free(auth);
  143. }
  144. }
  145. /*!
  146. * \brief Validate an authorization request.
  147. *
  148. * \note This function is automatically called by the HTTP
  149. * library on incoming requests. Applications do not
  150. * need to call this function.
  151. *
  152. * \param req Request to be checked.
  153. *
  154. * \return 0, if access granted, -1 otherwise.
  155. */
  156. int NutHttpAuthValidate(REQUEST * req)
  157. {
  158. char *realm;
  159. char *cp = 0;
  160. int rc = -1;
  161. /*
  162. * Get directory by chopping off filename.
  163. */
  164. realm = req->req_url;
  165. if ((cp = strrchr(realm, '/')) != 0)
  166. *cp = 0;
  167. else
  168. realm = ".";
  169. /*
  170. * Check if authorization required.
  171. */
  172. if (NutHttpAuthLookup(realm, 0)) {
  173. /*
  174. * Check authorization.
  175. */
  176. if (req->req_auth) {
  177. /*
  178. * Acceptint basic authorization only.
  179. */
  180. if (strncmp(req->req_auth, "Basic ", 6) == 0) {
  181. NutDecodeBase64(req->req_auth + 6);
  182. if (NutHttpAuthLookup(realm, req->req_auth + 6))
  183. rc = 0;
  184. }
  185. }
  186. } else
  187. rc = 0;
  188. if (cp)
  189. *cp = '/';
  190. return rc;
  191. }
  192. /*@}*/