2
-*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
3
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
6
#include <drizzled/server_includes.h>
7
#include <drizzled/session.h>
8
#include <drizzled/plugin/authentication.h>
9
#include <drizzled/gettext.h>
11
#include <curl/curl.h>
17
static bool sysvar_auth_http_enable= false;
18
static char* sysvar_auth_http_url= NULL;
20
static size_t curl_cb_read(void *ptr, size_t size, size_t nmemb, void *stream)
24
return (size * nmemb);
28
class Auth_http : public drizzled::plugin::Authentication
33
Auth_http(std::string name_arg) : drizzled::plugin::Authentication(name_arg)
35
// we are trusting that plugin initializers are called singlethreaded at startup
36
// if something else also calls curl_global_init() in a threadrace while we are here,
37
// we will crash the server.
38
curl_handle= curl_easy_init();
40
// turn off curl stuff that might mess us up
41
rv= curl_easy_setopt(curl_handle, CURLOPT_VERBOSE, 0);
42
rv= curl_easy_setopt(curl_handle, CURLOPT_NOPROGRESS, 1);
43
rv= curl_easy_setopt(curl_handle, CURLOPT_NOSIGNAL, 1);
45
// do a HEAD instead of a default GET
46
rv= curl_easy_setopt(curl_handle, CURLOPT_NOBODY, 1);
48
// set the read callback. this shouldnt get called, because we are doing a HEAD
49
rv= curl_easy_setopt(curl_handle, CURLOPT_READFUNCTION, curl_cb_read);
54
curl_easy_cleanup(curl_handle);
57
virtual bool authenticate(Session *session, const char *password)
59
long http_response_code;
61
if (sysvar_auth_http_enable == false)
64
assert(session->security_ctx.user.c_str());
68
// set the parameters: url, username, password
69
rv= curl_easy_setopt(curl_handle, CURLOPT_URL, sysvar_auth_http_url);
70
#if defined(HAVE_CURLOPT_USERNAME)
72
rv= curl_easy_setopt(curl_handle, CURLOPT_USERNAME,
73
session->security_ctx.user.c_str());
74
rv= curl_easy_setopt(curl_handle, CURLOPT_PASSWORD, password);
78
string userpwd= session->security_ctx.user;
80
userpwd.append(password);
81
rv= curl_easy_setopt(curl_handle, CURLOPT_USERPWD, userpwd.c_str());
83
#endif /* defined(HAVE_CURLOPT_USERNAME) */
86
rv= curl_easy_perform(curl_handle);
88
// what did we get? goes into http_response_code
89
rv= curl_easy_getinfo(curl_handle, CURLINFO_RESPONSE_CODE, &http_response_code);
91
// so here is an interesting question.
92
// return true if the response_code is 2XX, or return false if its 4XX
93
// for now, return false for 401, true otherwise
94
// this means that if the url breaks, then anyone can log in
95
// this might be the wrong thing
97
if (http_response_code == 401)
103
Auth_http* auth= NULL;
105
static int initialize(drizzled::plugin::Registry ®istry)
108
* Per libcurl manual, in multi-threaded applications, curl_global_init() should
109
* be called *before* curl_easy_init()...which is called in Auto_http's
112
if (curl_global_init(CURL_GLOBAL_NOTHING) != 0)
115
auth= new Auth_http("auth_http");
121
static int finalize(drizzled::plugin::Registry ®istry)
125
registry.remove(auth);
128
curl_global_cleanup();
134
static DRIZZLE_SYSVAR_BOOL(
136
sysvar_auth_http_enable,
138
N_("Enable HTTP Auth check"),
139
NULL, /* check func */
140
NULL, /* update func */
141
false /* default */);
144
static DRIZZLE_SYSVAR_STR(
146
sysvar_auth_http_url,
148
N_("URL for HTTP Auth check"),
149
NULL, /* check func */
150
NULL, /* update func*/
151
"http://localhost/" /* default */);
153
static struct st_mysql_sys_var* auth_http_system_variables[]= {
154
DRIZZLE_SYSVAR(enable),
160
drizzle_declare_plugin(auth_http)
165
"HTTP based authenication.",
167
initialize, /* Plugin Init */
168
finalize, /* Plugin Deinit */
169
NULL, /* status variables */
170
auth_http_system_variables,
171
NULL /* config options */
173
drizzle_declare_plugin_end;