~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/auth_http/auth_http.cc

Merged in latest plugin-slot-reorg.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
 
1
/*
 
2
 -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
3
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
 
 *
4
 
 *  Copyright (C) 2009 Sun Microsystems
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
 
 */
 
4
*/
19
5
 
20
 
#include "config.h"
 
6
#include <drizzled/server_includes.h>
 
7
#include <drizzled/session.h>
 
8
#include <drizzled/plugin/authentication.h>
 
9
#include <drizzled/gettext.h>
21
10
 
22
11
#include <curl/curl.h>
23
12
 
24
13
#include <string>
25
 
#include <cassert>
26
 
#include <boost/program_options.hpp>
27
 
#include <drizzled/module/option_map.h>
28
 
#include "drizzled/security_context.h"
29
 
#include "drizzled/plugin/authentication.h"
30
 
#include "drizzled/gettext.h"
31
 
namespace po= boost::program_options;
32
 
using namespace drizzled;
 
14
 
33
15
using namespace std;
34
16
 
 
17
static bool sysvar_auth_http_enable= false;
 
18
static char* sysvar_auth_http_url= NULL;
 
19
 
35
20
static size_t curl_cb_read(void *ptr, size_t size, size_t nmemb, void *stream)
36
21
{
37
22
  (void) ptr;
44
29
{
45
30
  CURLcode rv;
46
31
  CURL *curl_handle;
47
 
  const std::string auth_url;
48
32
public:
49
 
  Auth_http(std::string name_arg, const std::string &url_arg) :
50
 
    drizzled::plugin::Authentication(name_arg),
51
 
    auth_url(url_arg)
 
33
  Auth_http(std::string name_arg) : drizzled::plugin::Authentication(name_arg)
52
34
  {
53
35
    // we are trusting that plugin initializers are called singlethreaded at startup
54
36
    // if something else also calls curl_global_init() in a threadrace while we are here,
70
52
  ~Auth_http()
71
53
  {
72
54
    curl_easy_cleanup(curl_handle);
73
 
    curl_global_cleanup();
74
55
  }
75
56
 
76
 
  virtual bool authenticate(const SecurityContext &sctx, const string &password)
 
57
  virtual bool authenticate(Session *session, const char *password)
77
58
  {
78
59
    long http_response_code;
79
60
 
80
 
    assert(sctx.getUser().c_str());
 
61
    if (sysvar_auth_http_enable == false)
 
62
      return true;
 
63
 
 
64
    assert(session->security_ctx.user.c_str());
 
65
    assert(password);
 
66
 
81
67
 
82
68
    // set the parameters: url, username, password
83
 
    rv= curl_easy_setopt(curl_handle, CURLOPT_URL, auth_url.c_str());
 
69
    rv= curl_easy_setopt(curl_handle, CURLOPT_URL, sysvar_auth_http_url);
84
70
#if defined(HAVE_CURLOPT_USERNAME)
85
71
 
86
72
    rv= curl_easy_setopt(curl_handle, CURLOPT_USERNAME,
87
 
                         sctx.getUser().c_str());
88
 
    rv= curl_easy_setopt(curl_handle, CURLOPT_PASSWORD, password.c_str());
 
73
                         session->security_ctx.user.c_str());
 
74
    rv= curl_easy_setopt(curl_handle, CURLOPT_PASSWORD, password);
89
75
 
90
76
#else
91
77
 
92
 
    string userpwd(sctx.getUser());
 
78
    string userpwd= session->security_ctx.user;
93
79
    userpwd.append(":");
94
80
    userpwd.append(password);
95
81
    rv= curl_easy_setopt(curl_handle, CURLOPT_USERPWD, userpwd.c_str());
116
102
 
117
103
Auth_http* auth= NULL;
118
104
 
119
 
static int initialize(drizzled::module::Context &context)
 
105
static int initialize(drizzled::plugin::Registry &registry)
120
106
{
121
 
  const module::option_map &vm= context.getOptions();
122
 
 
123
107
  /* 
124
108
   * Per libcurl manual, in multi-threaded applications, curl_global_init() should
125
109
   * be called *before* curl_easy_init()...which is called in Auto_http's 
128
112
  if (curl_global_init(CURL_GLOBAL_NOTHING) != 0)
129
113
    return 1;
130
114
 
131
 
  const string auth_url(vm["url"].as<string>());
132
 
  if (auth_url.size() == 0)
 
115
  auth= new Auth_http("auth_http");
 
116
  registry.add(auth);
 
117
 
 
118
  return 0;
 
119
}
 
120
 
 
121
static int finalize(drizzled::plugin::Registry &registry)
 
122
{
 
123
  if (auth)
133
124
  {
134
 
    errmsg_printf(ERRMSG_LVL_ERROR,
135
 
                  _("auth_http plugin loaded but required option url not "
136
 
                    "specified. Against which URL are you intending on "
137
 
                    "authenticating?\n"));
138
 
    return 1;
 
125
    registry.remove(auth);
 
126
    delete auth;
 
127
 
 
128
    curl_global_cleanup();
139
129
  }
140
130
 
141
 
  auth= new Auth_http("auth_http", auth_url);
142
 
  context.add(auth);
143
 
  context.registerVariable(new sys_var_const_string_val("url", auth_url));
144
 
 
145
131
  return 0;
146
132
}
147
133
 
148
 
static void init_options(drizzled::module::option_context &context)
149
 
{
150
 
  context("url", po::value<string>()->default_value(""),
151
 
          N_("URL for HTTP Auth check"));
152
 
153
 
 
154
 
 
155
 
DRIZZLE_DECLARE_PLUGIN
156
 
{
157
 
  DRIZZLE_VERSION_ID,
158
 
  "auth-http",
 
134
static DRIZZLE_SYSVAR_BOOL(
 
135
  enable,
 
136
  sysvar_auth_http_enable,
 
137
  PLUGIN_VAR_NOCMDARG,
 
138
  N_("Enable HTTP Auth check"),
 
139
  NULL, /* check func */
 
140
  NULL, /* update func */
 
141
  false /* default */);
 
142
 
 
143
 
 
144
static DRIZZLE_SYSVAR_STR(
 
145
  url,
 
146
  sysvar_auth_http_url,
 
147
  PLUGIN_VAR_READONLY,
 
148
  N_("URL for HTTP Auth check"),
 
149
  NULL, /* check func */
 
150
  NULL, /* update func*/
 
151
  "http://localhost/" /* default */);
 
152
 
 
153
static struct st_mysql_sys_var* auth_http_system_variables[]= {
 
154
  DRIZZLE_SYSVAR(enable),
 
155
  DRIZZLE_SYSVAR(url),
 
156
  NULL
 
157
};
 
158
 
 
159
 
 
160
drizzle_declare_plugin(auth_http)
 
161
{
 
162
  "auth_http",
159
163
  "0.1",
160
164
  "Mark Atwood",
161
165
  "HTTP based authenication.",
162
166
  PLUGIN_LICENSE_GPL,
163
167
  initialize, /* Plugin Init */
164
 
  NULL,
165
 
  init_options    /* config options */
 
168
  finalize, /* Plugin Deinit */
 
169
  NULL,   /* status variables */
 
170
  auth_http_system_variables,
 
171
  NULL    /* config options */
166
172
}
167
 
DRIZZLE_DECLARE_PLUGIN_END;
 
173
drizzle_declare_plugin_end;