PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/libmicrohttpd/doc/examples/basicauthentication.c

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