basicauthentication.inc 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. With the small exception of IP address based access control,
  2. requests from all connecting clients where served equally until now.
  3. This chapter discusses a first method of client's authentication and
  4. its limits.
  5. A very simple approach feasible with the means already discussed would
  6. be to expect the password in the @emph{URI} string before granting access to
  7. the secured areas. The password could be separated from the actual resource identifier
  8. by a certain character, thus the request line might look like
  9. @verbatim
  10. GET /picture.png?mypassword
  11. @end verbatim
  12. @noindent
  13. In the rare situation where the client is customized enough and the connection occurs
  14. through secured lines (e.g., a embedded device directly attached to another via wire)
  15. and where the ability to embedd a password in the URI or to pass on a URI with a
  16. password are desired, this can be a reasonable choice.
  17. But when it is assumed that the user connecting does so with an ordinary Internet browser,
  18. this implementation brings some problems about. For example, the URI including the password
  19. stays in the address field or at least in the history of the browser for anybody near enough to see.
  20. It will also be inconvenient to add the password manually to any new URI when the browser does
  21. not know how to compose this automatically.
  22. At least the convenience issue can be addressed by employing the simplest built-in password
  23. facilities of HTTP compliant browsers, hence we want to start there. It will however turn out
  24. to have still severe weaknesses in terms of security which need consideration.
  25. Before we will start implementing @emph{Basic Authentication} as described in @emph{RFC 2617},
  26. we should finally abandon the bad practice of responding every request the first time our callback
  27. is called for a given connection. This is becoming more important now because the client and
  28. the server will have to talk in a more bi-directional way than before to
  29. But how can we tell whether the callback has been called before for the particular connection?
  30. Initially, the pointer this parameter references is set by @emph{MHD} in the callback. But it will
  31. also be "remembered" on the next call (for the same connection).
  32. Thus, we will generate no response until the parameter is non-null---implying the callback was
  33. called before at least once. We do not need to share information between different calls of the callback,
  34. so we can set the parameter to any adress that is assured to be not null. The pointer to the
  35. @code{connection} structure will be pointing to a legal address, so we take this.
  36. The first time @code{answer_to_connection} is called, we will not even look at the headers.
  37. @verbatim
  38. static int
  39. answer_to_connection (void *cls, struct MHD_Connection *connection,
  40. const char *url, const char *method, const char *version,
  41. const char *upload_data, size_t *upload_data_size,
  42. void **con_cls)
  43. {
  44. if (0 != strcmp(method, "GET")) return MHD_NO;
  45. if (NULL == *con_cls) {*con_cls = connection; return MHD_YES;}
  46. ...
  47. /* else respond accordingly */
  48. ...
  49. }
  50. @end verbatim
  51. @noindent
  52. Note how we lop off the connection on the first condition (no "GET" request), but return asking for more on
  53. the other one with @code{MHD_YES}.
  54. With this minor change, we can proceed to implement the actual authentication process.
  55. @heading Request for authentication
  56. Let us assume we had only files not intended to be handed out without the correct username/password,
  57. so every "GET" request will be challenged.
  58. @emph{RFC 2617} describes how the server shall ask for authentication by adding a
  59. @emph{WWW-Authenticate} response header with the name of the @emph{realm} protected.
  60. MHD can generate and queue such a failure response for you using
  61. the @code{MHD_queue_basic_auth_fail_response} API. The only thing you need to do
  62. is construct a response with the error page to be shown to the user
  63. if he aborts basic authentication. But first, you should check if the
  64. proper credentials were already supplied using the
  65. @code{MHD_basic_auth_get_username_password} call.
  66. Your code would then look like this:
  67. @verbatim
  68. static int
  69. answer_to_connection (void *cls, struct MHD_Connection *connection,
  70. const char *url, const char *method,
  71. const char *version, const char *upload_data,
  72. size_t *upload_data_size, void **con_cls)
  73. {
  74. char *user;
  75. char *pass;
  76. int fail;
  77. struct MHD_Response *response;
  78. if (0 != strcmp (method, MHD_HTTP_METHOD_GET))
  79. return MHD_NO;
  80. if (NULL == *con_cls)
  81. {
  82. *con_cls = connection;
  83. return MHD_YES;
  84. }
  85. pass = NULL;
  86. user = MHD_basic_auth_get_username_password (connection, &pass);
  87. fail = ( (user == NULL) ||
  88. (0 != strcmp (user, "root")) ||
  89. (0 != strcmp (pass, "pa$$w0rd") ) );
  90. if (user != NULL) free (user);
  91. if (pass != NULL) free (pass);
  92. if (fail)
  93. {
  94. const char *page = "<html><body>Go away.</body></html>";
  95. response =
  96. MHD_create_response_from_buffer (strlen (page), (void *) page,
  97. MHD_RESPMEM_PERSISTENT);
  98. ret = MHD_queue_basic_auth_fail_response (connection,
  99. "my realm",
  100. response);
  101. }
  102. else
  103. {
  104. const char *page = "<html><body>A secret.</body></html>";
  105. response =
  106. MHD_create_response_from_buffer (strlen (page), (void *) page,
  107. MHD_RESPMEM_PERSISTENT);
  108. ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
  109. }
  110. MHD_destroy_response (response);
  111. return ret;
  112. }
  113. @end verbatim
  114. See the @code{examples} directory for the complete example file.
  115. @heading Remarks
  116. For a proper server, the conditional statements leading to a return of @code{MHD_NO} should yield a
  117. response with a more precise status code instead of silently closing the connection. For example,
  118. failures of memory allocation are best reported as @emph{internal server error} and unexpected
  119. authentication methods as @emph{400 bad request}.
  120. @heading Exercises
  121. @itemize @bullet
  122. @item
  123. Make the server respond to wrong credentials (but otherwise well-formed requests) with the recommended
  124. @emph{401 unauthorized} status code. If the client still does not authenticate correctly within the
  125. same connection, close it and store the client's IP address for a certain time. (It is OK to check for
  126. expiration not until the main thread wakes up again on the next connection.) If the client fails
  127. authenticating three times during this period, add it to another list for which the
  128. @code{AcceptPolicyCallback} function denies connection (temporally).
  129. @item
  130. With the network utility @code{netcat} connect and log the response of a "GET" request as you
  131. did in the exercise of the first example, this time to a file. Now stop the server and let @emph{netcat}
  132. listen on the same port the server used to listen on and have it fake being the proper server by giving
  133. the file's content as the response (e.g. @code{cat log | nc -l -p 8888}). Pretending to think your were
  134. connecting to the actual server, browse to the eavesdropper and give the correct credentials.
  135. Copy and paste the encoded string you see in @code{netcat}'s output to some of the Base64 decode tools available online
  136. and see how both the user's name and password could be completely restored.
  137. @end itemize