mod_auth_basic.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. #ifndef _PRO_UHTTP_MODULES_MOD_AUTH_BASIC_H_
  2. #define _PRO_UHTTP_MODULES_MOD_AUTH_BASIC_H_
  3. /*
  4. * Copyright (C) 2012 by egnite GmbH
  5. *
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in the
  16. * documentation and/or other materials provided with the distribution.
  17. * 3. Neither the name of the copyright holders nor the names of
  18. * contributors may be used to endorse or promote products derived
  19. * from this software without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  24. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  25. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  26. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  27. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  28. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  29. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  30. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  31. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  32. * SUCH DAMAGE.
  33. *
  34. * For additional information see http://www.ethernut.de/
  35. */
  36. /*!
  37. * $Id$
  38. */
  39. #include <isc/list.h>
  40. #include <pro/uhttp/uhttpd.h>
  41. /*!
  42. * \addtogroup xgUHTTPModBasicAuth Basic access authentication
  43. * \ingroup xgUHTTPModules
  44. */
  45. /*@{*/
  46. /*!
  47. * \brief Basic authorization entry type.
  48. */
  49. typedef struct _AUTH_BASIC_ENTRY AUTH_BASIC_ENTRY;
  50. /*!
  51. * \brief Basic authorization entry structure.
  52. *
  53. * Be prepared, that the layout of this structure may change in future
  54. * versions.
  55. */
  56. struct _AUTH_BASIC_ENTRY {
  57. /*! \brief Chain link. */
  58. ISC_LINK(AUTH_BASIC_ENTRY) auth_link;
  59. /*! \brief URL of protected area. */
  60. char *auth_path;
  61. /*! \brief Login user and password, separated by a colon. */
  62. char *auth_login;
  63. /*! \brief Description of the resource. */
  64. char *auth_realm;
  65. };
  66. /*!
  67. * \brief Register a basic authorization.
  68. *
  69. * Protect a specified URL from unauthorized access. Resources, which
  70. * are not registered by this function are accessible by anyone.
  71. *
  72. * It is allowed to specify several different logins for the same resource.
  73. *
  74. * Alternatively it is possible to unprotect a previously protected resource
  75. * by passing a NULL pointer instead of a login string.
  76. *
  77. * Usage example:
  78. *
  79. * \code
  80. * #include <pro/uhttp/modules/mod_auth_basic.h>
  81. *
  82. * if (HttpRegisterAuthBasic("dir", "User:Pass") == 0) {
  83. * puts("Resource is protected.");
  84. * ...
  85. * } else {
  86. * puts("Failed to protect resource.");
  87. * ...
  88. * }
  89. *
  90. * if (HttpRegisterAuthBasic("dir", NULL) == 0) {
  91. * puts("Resource is unprotected.");
  92. * ...
  93. * } else {
  94. * puts("Failed to remove protection.");
  95. * ...
  96. * }
  97. * \endcode
  98. *
  99. * \param path Path to the protected resource.
  100. * \param login Required login to access the given resource or NULL to
  101. * remove any previously registered protection for the given
  102. * resource . To protect a resource, this string must contain
  103. * a user name, followed by a colon followed by an unencrypted
  104. * password.
  105. * \param realm Description of the protected resource. This optional
  106. * parameter can be a NULL pointer, in which case the path
  107. * is used instead.
  108. *
  109. * \return 0 on success or -1 on error. Trying to add duplicate entries
  110. * will be silently ignored.
  111. */
  112. extern int HttpRegisterAuthBasic(const char *path, const char *login, const char *realm);
  113. /*!
  114. * \brief Validate an authentication for a specified realm.
  115. *
  116. * If the requested resource had been previously protected by a call to
  117. * HttpRegisterAuthBasic() and if the client of the specified session
  118. * hasn't provided a valid authentication, then access is rejected. In
  119. * the this case the caller should return a 401 response code to the
  120. * client. This will typically prompt the user to enter a valid
  121. * user/password pair.
  122. *
  123. * \param hs Pointer to a \ref _HTTPD_SESSION structure, which
  124. * should contain the requested resource and a valid
  125. * authentication.
  126. *
  127. * \return 0 if access is granted, -1 if not.
  128. */
  129. extern int HttpAuthBasicValidate(HTTPD_SESSION *hs);
  130. /*!
  131. * \brief Look up a basic authorization entry.
  132. *
  133. * This low level routine can be used to retrieve a previously
  134. * registered authorization entry. Note, that the structure
  135. * layout of authorization entries may change in future versions.
  136. *
  137. * Usage example:
  138. *
  139. * \code
  140. * #include <pro/uhttp/modules/mod_auth_basic.h>
  141. *
  142. * if (HttpAuthBasicLookup("/dir/index.html", NULL, 1) == NULL) {
  143. * puts("Resource is unprotected.");
  144. * ...
  145. * } else {
  146. * puts("Resource is protected.");
  147. * ...
  148. * }
  149. *
  150. * if (HttpAuthBasicLookup("/dir/index.html", "User:Pass", 1)) {
  151. * puts("Access is granted.");
  152. * ...
  153. * } else {
  154. * puts("Access is rejected.");
  155. * ...
  156. * }
  157. * \endcode
  158. *
  159. * \param realm Requested resource realm, case insensitive.
  160. * \param login Requested authentication user and password, separated
  161. * by a colon. This pointer may be NULL, if the caller
  162. * only wants to check if the specified resource is
  163. * protected or not.
  164. * \param best Set to 1 to find the best matching realm. If 0, then
  165. * an exact match is requested.
  166. *
  167. * \return Pointer to the AUTH_BASIC_ENTRY structure or NULL if the
  168. * requested entry doesn't exists.
  169. */
  170. extern const AUTH_BASIC_ENTRY *HttpAuthBasicLookup(const char *realm, const char *login, int best);
  171. /*@}*/
  172. #endif