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
22
22
#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;
27
#include "drizzled/security_context.h"
28
#include "drizzled/plugin/authentication.h"
29
#include "drizzled/gettext.h"
32
31
using namespace drizzled;
33
32
using namespace std;
34
static bool sysvar_auth_http_enable= false;
35
static char* sysvar_auth_http_url= NULL;
35
37
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),
50
Auth_http(std::string name_arg)
51
: drizzled::plugin::Authentication(name_arg)
53
53
// we are trusting that plugin initializers are called singlethreaded at startup
54
54
// if something else also calls curl_global_init() in a threadrace while we are here,
73
73
curl_global_cleanup();
76
virtual bool authenticate(const identifier::User &sctx, const string &password)
76
virtual bool authenticate(const SecurityContext &sctx, const string &password)
78
78
long http_response_code;
80
assert(sctx.username().c_str());
80
if (sysvar_auth_http_enable == false)
83
assert(sctx.getUser().c_str());
82
86
// set the parameters: url, username, password
83
rv= curl_easy_setopt(curl_handle, CURLOPT_URL, auth_url.c_str());
87
rv= curl_easy_setopt(curl_handle, CURLOPT_URL, sysvar_auth_http_url);
84
88
#if defined(HAVE_CURLOPT_USERNAME)
86
90
rv= curl_easy_setopt(curl_handle, CURLOPT_USERNAME,
87
sctx.username().c_str());
91
sctx.getUser().c_str());
88
92
rv= curl_easy_setopt(curl_handle, CURLOPT_PASSWORD, password.c_str());
92
string userpwd(sctx.username());
96
string userpwd(sctx.getUser());
93
97
userpwd.append(":");
94
98
userpwd.append(password);
95
99
rv= curl_easy_setopt(curl_handle, CURLOPT_USERPWD, userpwd.c_str());
119
123
static int initialize(drizzled::module::Context &context)
121
const module::option_map &vm= context.getOptions();
124
126
* Per libcurl manual, in multi-threaded applications, curl_global_init() should
125
127
* be called *before* curl_easy_init()...which is called in Auto_http's
128
130
if (curl_global_init(CURL_GLOBAL_NOTHING) != 0)
131
const string auth_url(vm["url"].as<string>());
132
if (auth_url.size() == 0)
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"));
141
auth= new Auth_http("auth_http", auth_url);
133
auth= new Auth_http("auth_http");
142
134
context.add(auth);
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"));
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),
155
165
DRIZZLE_DECLARE_PLUGIN
157
167
DRIZZLE_VERSION_ID,
161
171
"HTTP based authenication.",
162
172
PLUGIN_LICENSE_GPL,
163
173
initialize, /* Plugin Init */
165
init_options /* config options */
174
auth_http_system_variables,
175
NULL /* config options */
167
177
DRIZZLE_DECLARE_PLUGIN_END;