drizzle
C++ MySQL-protocol-compatible database server
Loading...
Searching...
No Matches
auth_http.cc
1/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3 *
4 * Copyright (C) 2009 Sun Microsystems, Inc.
5 *
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.
9 *
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.
14 *
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
18 */
19
20#include <config.h>
21
22#include <curl/curl.h>
23
24#include <string>
25#include <cassert>
26#include <boost/program_options.hpp>
27
28#include <drizzled/item.h>
30#include <drizzled/identifier.h>
31#include <drizzled/plugin/authentication.h>
32#include <drizzled/gettext.h>
33namespace po= boost::program_options;
34using namespace drizzled;
35using namespace std;
36
37namespace auth_http {
38bool updateAuthURL(Session *, set_var *);
39
40static size_t curl_cb_read(void *ptr, size_t size, size_t nmemb, void *stream)
41{
42 (void) ptr;
43 (void) stream;
44 return (size * nmemb);
45}
46
47
48class Auth_http : public drizzled::plugin::Authentication
49{
50 CURLcode rv;
51 CURL *curl_handle;
52 std::string sysvar_auth_url;
53public:
54 Auth_http(std::string name_arg, const std::string &url_arg) :
56 sysvar_auth_url(url_arg)
57 {
58 // we are trusting that plugin initializers are called singlethreaded at startup
59 // if something else also calls curl_global_init() in a threadrace while we are here,
60 // we will crash the server.
61 curl_handle= curl_easy_init();
62
63 // turn off curl stuff that might mess us up
64 rv= curl_easy_setopt(curl_handle, CURLOPT_VERBOSE, 0);
65 rv= curl_easy_setopt(curl_handle, CURLOPT_NOPROGRESS, 1);
66 rv= curl_easy_setopt(curl_handle, CURLOPT_NOSIGNAL, 1);
67
68 // do a HEAD instead of a default GET
69 rv= curl_easy_setopt(curl_handle, CURLOPT_NOBODY, 1);
70
71 // set the read callback. this shouldnt get called, because we are doing a HEAD
72 rv= curl_easy_setopt(curl_handle, CURLOPT_READFUNCTION, curl_cb_read);
73 }
74
75 std::string& getAuthURL()
76 {
77 return sysvar_auth_url;
78 }
79
80 bool setAuthURL(std::string& new_auth_url)
81 {
82 if (new_auth_url.empty())
83 {
84 errmsg_printf(error::ERROR, _("auth url cannot be an empty string"));
85 return false; // error
86 }
87 sysvar_auth_url= new_auth_url;
88 return true;
89 }
90
91 ~Auth_http()
92 {
93 curl_easy_cleanup(curl_handle);
94 curl_global_cleanup();
95 }
96
97 virtual bool authenticate(const identifier::User &sctx, const string &password)
98 {
99 long http_response_code;
100
101 assert(sctx.username().c_str());
102
103 // set the parameters: url, username, password
104 rv= curl_easy_setopt(curl_handle, CURLOPT_URL, sysvar_auth_url.c_str());
105
106 rv= curl_easy_setopt(curl_handle, CURLOPT_USERNAME,
107 sctx.username().c_str());
108 rv= curl_easy_setopt(curl_handle, CURLOPT_PASSWORD, password.c_str());
109
110
111 // do it
112 rv= curl_easy_perform(curl_handle);
113
114 // what did we get? goes into http_response_code
115 rv= curl_easy_getinfo(curl_handle, CURLINFO_RESPONSE_CODE, &http_response_code);
116
117 // so here is an interesting question.
118 // return true if the response_code is 2XX, or return false if its 4XX
119 // for now, return false for 401, true otherwise
120 // this means that if the url breaks, then anyone can log in
121 // this might be the wrong thing
122
123 if (http_response_code == 401)
124 return false;
125 return true;
126 }
127};
128
129Auth_http* auth= NULL;
130
136bool updateAuthURL(Session *, set_var* var)
137{
138 if (not var->value->str_value.empty())
139 {
140 std::string new_auth_url(var->value->str_value.data());
141 if (auth->setAuthURL(new_auth_url))
142 return false; //success
143 else
144 return true; // error
145 }
146 errmsg_printf(error::ERROR, _("auth_http url cannot be NULL"));
147 return true; // error
148}
149
150
151static int initialize(drizzled::module::Context &context)
152{
153 const module::option_map &vm= context.getOptions();
154
155 /*
156 * Per libcurl manual, in multi-threaded applications, curl_global_init() should
157 * be called *before* curl_easy_init()...which is called in Auto_http's
158 * constructor.
159 */
160 if (curl_global_init(CURL_GLOBAL_NOTHING) != 0)
161 return 1;
162
163 const string auth_url(vm["url"].as<string>());
164 if (auth_url.size() == 0)
165 {
166 errmsg_printf(error::ERROR,
167 _("auth_http plugin loaded but required option url not "
168 "specified. Against which URL are you intending on "
169 "authenticating?\n"));
170 return 1;
171 }
172
173 auth= new Auth_http("auth_http", auth_url);
174 context.add(auth);
175 context.registerVariable(new sys_var_std_string("url", auth->getAuthURL(), NULL, &updateAuthURL));
176
177 return 0;
178}
179
180static void init_options(drizzled::module::option_context &context)
181{
182 context("url", po::value<string>()->default_value(""),
183 N_("URL for HTTP Auth check"));
184}
185
186} /* namespace auth_http */
187
188DRIZZLE_DECLARE_PLUGIN
189{
190 DRIZZLE_VERSION_ID,
191 "auth-http",
192 "0.1",
193 "Mark Atwood",
194 N_("Authenication against a web server using HTTP"),
195 PLUGIN_LICENSE_GPL,
196 auth_http::initialize,
197 NULL,
198 auth_http::init_options
199}
200DRIZZLE_DECLARE_PLUGIN_END;
A set of Session members describing the current authenticated user.
TODO: Rename this file - func.h is stupid.
STL namespace.
An Proxy Wrapper around boost::program_options::variables_map.