basicauthentication.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. #include <microhttpd.h>
  2. #include <string.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <time.h>
  6. #define PORT 8888
  7. #define REALM "\"Maintenance\""
  8. #define USER "a legitimate user"
  9. #define PASSWORD "and his password"
  10. char *string_to_base64 (const char *message);
  11. int
  12. ask_for_authentication (struct MHD_Connection *connection, const char *realm)
  13. {
  14. int ret;
  15. struct MHD_Response *response;
  16. char *headervalue;
  17. const char *strbase = "Basic realm=";
  18. response = MHD_create_response_from_data (0, NULL, MHD_NO, MHD_NO);
  19. if (!response)
  20. return MHD_NO;
  21. headervalue = malloc (strlen (strbase) + strlen (realm) + 1);
  22. if (!headervalue)
  23. return MHD_NO;
  24. strcpy (headervalue, strbase);
  25. strcat (headervalue, realm);
  26. ret = MHD_add_response_header (response, "WWW-Authenticate", headervalue);
  27. free (headervalue);
  28. if (!ret)
  29. {
  30. MHD_destroy_response (response);
  31. return MHD_NO;
  32. }
  33. ret = MHD_queue_response (connection, MHD_HTTP_UNAUTHORIZED, response);
  34. MHD_destroy_response (response);
  35. return ret;
  36. }
  37. int
  38. is_authenticated (struct MHD_Connection *connection,
  39. const char *username, const char *password)
  40. {
  41. const char *headervalue;
  42. char *expected_b64, *expected;
  43. const char *strbase = "Basic ";
  44. int authenticated;
  45. headervalue =
  46. MHD_lookup_connection_value (connection, MHD_HEADER_KIND,
  47. "Authorization");
  48. if (NULL == headervalue)
  49. return 0;
  50. if (0 != strncmp (headervalue, strbase, strlen (strbase)))
  51. return 0;
  52. expected = malloc (strlen (username) + 1 + strlen (password) + 1);
  53. if (NULL == expected)
  54. return 0;
  55. strcpy (expected, username);
  56. strcat (expected, ":");
  57. strcat (expected, password);
  58. expected_b64 = string_to_base64 (expected);
  59. if (NULL == expected_b64)
  60. return 0;
  61. strcpy (expected, strbase);
  62. authenticated =
  63. (strcmp (headervalue + strlen (strbase), expected_b64) == 0);
  64. free (expected_b64);
  65. return authenticated;
  66. }
  67. int
  68. secret_page (struct MHD_Connection *connection)
  69. {
  70. int ret;
  71. struct MHD_Response *response;
  72. const char *page = "<html><body>A secret.</body></html>";
  73. response =
  74. MHD_create_response_from_data (strlen (page), (void *) page, MHD_NO,
  75. MHD_NO);
  76. if (!response)
  77. return MHD_NO;
  78. ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
  79. MHD_destroy_response (response);
  80. return ret;
  81. }
  82. int
  83. answer_to_connection (void *cls, struct MHD_Connection *connection,
  84. const char *url, const char *method,
  85. const char *version, const char *upload_data,
  86. unsigned int *upload_data_size, void **con_cls)
  87. {
  88. if (0 != strcmp (method, "GET"))
  89. return MHD_NO;
  90. if (NULL == *con_cls)
  91. {
  92. *con_cls = connection;
  93. return MHD_YES;
  94. }
  95. if (!is_authenticated (connection, USER, PASSWORD))
  96. return ask_for_authentication (connection, REALM);
  97. return secret_page (connection);
  98. }
  99. int
  100. main ()
  101. {
  102. struct MHD_Daemon *daemon;
  103. daemon = MHD_start_daemon (MHD_USE_SELECT_INTERNALLY, PORT, NULL, NULL,
  104. &answer_to_connection, NULL, MHD_OPTION_END);
  105. if (NULL == daemon)
  106. return 1;
  107. getchar ();
  108. MHD_stop_daemon (daemon);
  109. return 0;
  110. }
  111. char *
  112. string_to_base64 (const char *message)
  113. {
  114. const char *lookup =
  115. "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  116. unsigned long l;
  117. int i;
  118. char *tmp;
  119. size_t length = strlen (message);
  120. tmp = malloc (length * 2);
  121. if (NULL == tmp)
  122. return tmp;
  123. tmp[0] = 0;
  124. for (i = 0; i < length; i += 3)
  125. {
  126. l = (((unsigned long) message[i]) << 16)
  127. | (((i + 1) < length) ? (((unsigned long) message[i + 1]) << 8) : 0)
  128. | (((i + 2) < length) ? ((unsigned long) message[i + 2]) : 0);
  129. strncat (tmp, &lookup[(l >> 18) & 0x3F], 1);
  130. strncat (tmp, &lookup[(l >> 12) & 0x3F], 1);
  131. if (i + 1 < length)
  132. strncat (tmp, &lookup[(l >> 6) & 0x3F], 1);
  133. if (i + 2 < length)
  134. strncat (tmp, &lookup[l & 0x3F], 1);
  135. }
  136. if (length % 3)
  137. strncat (tmp, "===", 3 - length % 3);
  138. return tmp;
  139. }