1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright (C) 2009 Sun Microsystems
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; version 2 of the License.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22
#include <curl/curl.h>
27
#include "drizzled/security_context.h"
28
#include "drizzled/plugin/authentication.h"
29
#include "drizzled/gettext.h"
31
using namespace drizzled;
34
static bool sysvar_auth_http_enable= false;
35
static char* sysvar_auth_http_url= NULL;
37
static size_t curl_cb_read(void *ptr, size_t size, size_t nmemb, void *stream)
41
return (size * nmemb);
45
class Auth_http : public drizzled::plugin::Authentication
50
Auth_http(std::string name_arg)
51
: drizzled::plugin::Authentication(name_arg)
53
// we are trusting that plugin initializers are called singlethreaded at startup
54
// if something else also calls curl_global_init() in a threadrace while we are here,
55
// we will crash the server.
56
curl_handle= curl_easy_init();
58
// turn off curl stuff that might mess us up
59
rv= curl_easy_setopt(curl_handle, CURLOPT_VERBOSE, 0);
60
rv= curl_easy_setopt(curl_handle, CURLOPT_NOPROGRESS, 1);
61
rv= curl_easy_setopt(curl_handle, CURLOPT_NOSIGNAL, 1);
63
// do a HEAD instead of a default GET
64
rv= curl_easy_setopt(curl_handle, CURLOPT_NOBODY, 1);
66
// set the read callback. this shouldnt get called, because we are doing a HEAD
67
rv= curl_easy_setopt(curl_handle, CURLOPT_READFUNCTION, curl_cb_read);
72
curl_easy_cleanup(curl_handle);
73
curl_global_cleanup();
76
virtual bool authenticate(const SecurityContext &sctx, const string &password)
78
long http_response_code;
80
if (sysvar_auth_http_enable == false)
83
assert(sctx.getUser().c_str());
86
// set the parameters: url, username, password
87
rv= curl_easy_setopt(curl_handle, CURLOPT_URL, sysvar_auth_http_url);
88
#if defined(HAVE_CURLOPT_USERNAME)
90
rv= curl_easy_setopt(curl_handle, CURLOPT_USERNAME,
91
sctx.getUser().c_str());
92
rv= curl_easy_setopt(curl_handle, CURLOPT_PASSWORD, password.c_str());
96
string userpwd(sctx.getUser());
98
userpwd.append(password);
99
rv= curl_easy_setopt(curl_handle, CURLOPT_USERPWD, userpwd.c_str());
101
#endif /* defined(HAVE_CURLOPT_USERNAME) */
104
rv= curl_easy_perform(curl_handle);
106
// what did we get? goes into http_response_code
107
rv= curl_easy_getinfo(curl_handle, CURLINFO_RESPONSE_CODE, &http_response_code);
109
// so here is an interesting question.
110
// return true if the response_code is 2XX, or return false if its 4XX
111
// for now, return false for 401, true otherwise
112
// this means that if the url breaks, then anyone can log in
113
// this might be the wrong thing
115
if (http_response_code == 401)
121
Auth_http* auth= NULL;
123
static int initialize(drizzled::plugin::Context &context)
126
* Per libcurl manual, in multi-threaded applications, curl_global_init() should
127
* be called *before* curl_easy_init()...which is called in Auto_http's
130
if (curl_global_init(CURL_GLOBAL_NOTHING) != 0)
133
auth= new Auth_http("auth_http");
139
static DRIZZLE_SYSVAR_BOOL(
141
sysvar_auth_http_enable,
143
N_("Enable HTTP Auth check"),
144
NULL, /* check func */
145
NULL, /* update func */
146
false /* default */);
149
static DRIZZLE_SYSVAR_STR(
151
sysvar_auth_http_url,
153
N_("URL for HTTP Auth check"),
154
NULL, /* check func */
155
NULL, /* update func*/
156
"http://localhost/" /* default */);
158
static drizzle_sys_var* auth_http_system_variables[]= {
159
DRIZZLE_SYSVAR(enable),
165
DRIZZLE_DECLARE_PLUGIN
171
"HTTP based authenication.",
173
initialize, /* Plugin Init */
174
auth_http_system_variables,
175
NULL /* config options */
177
DRIZZLE_DECLARE_PLUGIN_END;