1
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright (C) 2009 Sun Microsystems, Inc.
4
* Copyright (C) 2009 Sun Microsystems
6
6
* This program is free software; you can redistribute it and/or modify
7
7
* it under the terms of the GNU General Public License as published by
17
17
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20
#include <drizzled/server_includes.h>
21
#include <drizzled/session.h>
22
#include <drizzled/plugin/authentication.h>
23
#include <drizzled/gettext.h>
22
25
#include <curl/curl.h>
26
#include <boost/program_options.hpp>
27
#include <drizzled/module/option_map.h>
28
#include "drizzled/identifier.h"
29
#include "drizzled/plugin/authentication.h"
30
#include "drizzled/gettext.h"
31
namespace po= boost::program_options;
32
using namespace drizzled;
33
29
using namespace std;
31
static bool sysvar_auth_http_enable= false;
32
static char* sysvar_auth_http_url= NULL;
35
34
static size_t curl_cb_read(void *ptr, size_t size, size_t nmemb, void *stream)
47
const std::string auth_url;
49
Auth_http(std::string name_arg, const std::string &url_arg) :
50
drizzled::plugin::Authentication(name_arg),
47
Auth_http(std::string name_arg)
48
: drizzled::plugin::Authentication(name_arg)
53
50
// we are trusting that plugin initializers are called singlethreaded at startup
54
51
// if something else also calls curl_global_init() in a threadrace while we are here,
72
69
curl_easy_cleanup(curl_handle);
73
curl_global_cleanup();
76
virtual bool authenticate(const identifier::User &sctx, const string &password)
72
virtual bool authenticate(Session *session, const char *password)
78
74
long http_response_code;
80
assert(sctx.username().c_str());
76
if (sysvar_auth_http_enable == false)
79
assert(session->security_ctx.user.c_str());
82
83
// set the parameters: url, username, password
83
rv= curl_easy_setopt(curl_handle, CURLOPT_URL, auth_url.c_str());
84
rv= curl_easy_setopt(curl_handle, CURLOPT_URL, sysvar_auth_http_url);
84
85
#if defined(HAVE_CURLOPT_USERNAME)
86
87
rv= curl_easy_setopt(curl_handle, CURLOPT_USERNAME,
87
sctx.username().c_str());
88
rv= curl_easy_setopt(curl_handle, CURLOPT_PASSWORD, password.c_str());
88
session->security_ctx.user.c_str());
89
rv= curl_easy_setopt(curl_handle, CURLOPT_PASSWORD, password);
92
string userpwd(sctx.username());
93
string userpwd= session->security_ctx.user;
93
94
userpwd.append(":");
94
95
userpwd.append(password);
95
96
rv= curl_easy_setopt(curl_handle, CURLOPT_USERPWD, userpwd.c_str());
117
118
Auth_http* auth= NULL;
119
static int initialize(drizzled::module::Context &context)
120
static int initialize(drizzled::plugin::Registry ®istry)
121
const module::option_map &vm= context.getOptions();
124
123
* Per libcurl manual, in multi-threaded applications, curl_global_init() should
125
124
* be called *before* curl_easy_init()...which is called in Auto_http's
128
127
if (curl_global_init(CURL_GLOBAL_NOTHING) != 0)
131
const string auth_url(vm["url"].as<string>());
132
if (auth_url.size() == 0)
130
auth= new Auth_http("auth_http");
136
static int finalize(drizzled::plugin::Registry ®istry)
134
errmsg_printf(error::ERROR,
135
_("auth_http plugin loaded but required option url not "
136
"specified. Against which URL are you intending on "
137
"authenticating?\n"));
140
registry.remove(auth);
143
curl_global_cleanup();
141
auth= new Auth_http("auth_http", auth_url);
143
context.registerVariable(new sys_var_const_string_val("url", auth_url));
148
static void init_options(drizzled::module::option_context &context)
150
context("url", po::value<string>()->default_value(""),
151
N_("URL for HTTP Auth check"));
155
DRIZZLE_DECLARE_PLUGIN
149
static DRIZZLE_SYSVAR_BOOL(
151
sysvar_auth_http_enable,
153
N_("Enable HTTP Auth check"),
154
NULL, /* check func */
155
NULL, /* update func */
156
false /* default */);
159
static DRIZZLE_SYSVAR_STR(
161
sysvar_auth_http_url,
163
N_("URL for HTTP Auth check"),
164
NULL, /* check func */
165
NULL, /* update func*/
166
"http://localhost/" /* default */);
168
static struct st_mysql_sys_var* auth_http_system_variables[]= {
169
DRIZZLE_SYSVAR(enable),
175
drizzle_declare_plugin(auth_http)
161
180
"HTTP based authenication.",
162
181
PLUGIN_LICENSE_GPL,
163
182
initialize, /* Plugin Init */
165
init_options /* config options */
183
finalize, /* Plugin Deinit */
184
NULL, /* status variables */
185
auth_http_system_variables,
186
NULL /* config options */
167
DRIZZLE_DECLARE_PLUGIN_END;
188
drizzle_declare_plugin_end;