1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
|
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
*
* Copyright (C) 2009 Sun Microsystems
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 2 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "config.h"
#include <curl/curl.h>
#include <string>
#include <cassert>
#include <boost/program_options.hpp>
#include <drizzled/module/option_map.h>
#include "drizzled/security_context.h"
#include "drizzled/plugin/authentication.h"
#include "drizzled/gettext.h"
namespace po= boost::program_options;
using namespace drizzled;
using namespace std;
static size_t curl_cb_read(void *ptr, size_t size, size_t nmemb, void *stream)
{
(void) ptr;
(void) stream;
return (size * nmemb);
}
class Auth_http : public drizzled::plugin::Authentication
{
CURLcode rv;
CURL *curl_handle;
const std::string auth_url;
public:
Auth_http(std::string name_arg, const std::string &url_arg) :
drizzled::plugin::Authentication(name_arg),
auth_url(url_arg)
{
// we are trusting that plugin initializers are called singlethreaded at startup
// if something else also calls curl_global_init() in a threadrace while we are here,
// we will crash the server.
curl_handle= curl_easy_init();
// turn off curl stuff that might mess us up
rv= curl_easy_setopt(curl_handle, CURLOPT_VERBOSE, 0);
rv= curl_easy_setopt(curl_handle, CURLOPT_NOPROGRESS, 1);
rv= curl_easy_setopt(curl_handle, CURLOPT_NOSIGNAL, 1);
// do a HEAD instead of a default GET
rv= curl_easy_setopt(curl_handle, CURLOPT_NOBODY, 1);
// set the read callback. this shouldnt get called, because we are doing a HEAD
rv= curl_easy_setopt(curl_handle, CURLOPT_READFUNCTION, curl_cb_read);
}
~Auth_http()
{
curl_easy_cleanup(curl_handle);
curl_global_cleanup();
}
virtual bool authenticate(const SecurityContext &sctx, const string &password)
{
long http_response_code;
assert(sctx.getUser().c_str());
// set the parameters: url, username, password
rv= curl_easy_setopt(curl_handle, CURLOPT_URL, auth_url.c_str());
#if defined(HAVE_CURLOPT_USERNAME)
rv= curl_easy_setopt(curl_handle, CURLOPT_USERNAME,
sctx.getUser().c_str());
rv= curl_easy_setopt(curl_handle, CURLOPT_PASSWORD, password.c_str());
#else
string userpwd(sctx.getUser());
userpwd.append(":");
userpwd.append(password);
rv= curl_easy_setopt(curl_handle, CURLOPT_USERPWD, userpwd.c_str());
#endif /* defined(HAVE_CURLOPT_USERNAME) */
// do it
rv= curl_easy_perform(curl_handle);
// what did we get? goes into http_response_code
rv= curl_easy_getinfo(curl_handle, CURLINFO_RESPONSE_CODE, &http_response_code);
// so here is an interesting question.
// return true if the response_code is 2XX, or return false if its 4XX
// for now, return false for 401, true otherwise
// this means that if the url breaks, then anyone can log in
// this might be the wrong thing
if (http_response_code == 401)
return false;
return true;
}
};
Auth_http* auth= NULL;
static int initialize(drizzled::module::Context &context)
{
const module::option_map &vm= context.getOptions();
/*
* Per libcurl manual, in multi-threaded applications, curl_global_init() should
* be called *before* curl_easy_init()...which is called in Auto_http's
* constructor.
*/
if (curl_global_init(CURL_GLOBAL_NOTHING) != 0)
return 1;
const string auth_url(vm["url"].as<string>());
if (auth_url.size() == 0)
{
errmsg_printf(ERRMSG_LVL_ERROR,
_("auth_http plugin loaded but required option url not "
"specified. Against which URL are you intending on "
"authenticating?\n"));
return 1;
}
auth= new Auth_http("auth_http", auth_url);
context.add(auth);
context.registerVariable(new sys_var_const_string_val("url", auth_url));
return 0;
}
static void init_options(drizzled::module::option_context &context)
{
context("url", po::value<string>()->default_value(""),
N_("URL for HTTP Auth check"));
}
DRIZZLE_DECLARE_PLUGIN
{
DRIZZLE_VERSION_ID,
"auth-http",
"0.1",
"Mark Atwood",
"HTTP based authenication.",
PLUGIN_LICENSE_GPL,
initialize, /* Plugin Init */
NULL,
init_options /* config options */
}
DRIZZLE_DECLARE_PLUGIN_END;
|