~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/auth_http/auth_http.cc

Merge Stewart's dead code removal

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
3
 *
4
 
 *  Copyright (C) 2009 Sun Microsystems, Inc.
 
4
 *  Copyright (C) 2009 Sun Microsystems
5
5
 *
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
18
18
 */
19
19
 
20
 
#include "config.h"
 
20
#include <drizzled/server_includes.h>
 
21
#include <drizzled/session.h>
 
22
#include <drizzled/plugin/authentication.h>
 
23
#include <drizzled/gettext.h>
21
24
 
22
25
#include <curl/curl.h>
23
26
 
24
27
#include <string>
25
 
#include <cassert>
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;
 
28
 
33
29
using namespace std;
34
30
 
 
31
static bool sysvar_auth_http_enable= false;
 
32
static char* sysvar_auth_http_url= NULL;
 
33
 
35
34
static size_t curl_cb_read(void *ptr, size_t size, size_t nmemb, void *stream)
36
35
{
37
36
  (void) ptr;
44
43
{
45
44
  CURLcode rv;
46
45
  CURL *curl_handle;
47
 
  const std::string auth_url;
48
46
public:
49
 
  Auth_http(std::string name_arg, const std::string &url_arg) :
50
 
    drizzled::plugin::Authentication(name_arg),
51
 
    auth_url(url_arg)
 
47
  Auth_http(std::string name_arg)
 
48
    : drizzled::plugin::Authentication(name_arg)
52
49
  {
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,
70
67
  ~Auth_http()
71
68
  {
72
69
    curl_easy_cleanup(curl_handle);
73
 
    curl_global_cleanup();
74
70
  }
75
71
 
76
 
  virtual bool authenticate(const identifier::User &sctx, const string &password)
 
72
  virtual bool authenticate(Session *session, const char *password)
77
73
  {
78
74
    long http_response_code;
79
75
 
80
 
    assert(sctx.username().c_str());
 
76
    if (sysvar_auth_http_enable == false)
 
77
      return true;
 
78
 
 
79
    assert(session->security_ctx.user.c_str());
 
80
    assert(password);
 
81
 
81
82
 
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)
85
86
 
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);
89
90
 
90
91
#else
91
92
 
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());
116
117
 
117
118
Auth_http* auth= NULL;
118
119
 
119
 
static int initialize(drizzled::module::Context &context)
 
120
static int initialize(drizzled::plugin::Registry &registry)
120
121
{
121
 
  const module::option_map &vm= context.getOptions();
122
 
 
123
122
  /* 
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)
129
128
    return 1;
130
129
 
131
 
  const string auth_url(vm["url"].as<string>());
132
 
  if (auth_url.size() == 0)
 
130
  auth= new Auth_http("auth_http");
 
131
  registry.add(auth);
 
132
 
 
133
  return 0;
 
134
}
 
135
 
 
136
static int finalize(drizzled::plugin::Registry &registry)
 
137
{
 
138
  if (auth)
133
139
  {
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"));
138
 
    return 1;
 
140
    registry.remove(auth);
 
141
    delete auth;
 
142
 
 
143
    curl_global_cleanup();
139
144
  }
140
145
 
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
146
  return 0;
146
147
}
147
148
 
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",
 
149
static DRIZZLE_SYSVAR_BOOL(
 
150
  enable,
 
151
  sysvar_auth_http_enable,
 
152
  PLUGIN_VAR_NOCMDARG,
 
153
  N_("Enable HTTP Auth check"),
 
154
  NULL, /* check func */
 
155
  NULL, /* update func */
 
156
  false /* default */);
 
157
 
 
158
 
 
159
static DRIZZLE_SYSVAR_STR(
 
160
  url,
 
161
  sysvar_auth_http_url,
 
162
  PLUGIN_VAR_READONLY,
 
163
  N_("URL for HTTP Auth check"),
 
164
  NULL, /* check func */
 
165
  NULL, /* update func*/
 
166
  "http://localhost/" /* default */);
 
167
 
 
168
static struct st_mysql_sys_var* auth_http_system_variables[]= {
 
169
  DRIZZLE_SYSVAR(enable),
 
170
  DRIZZLE_SYSVAR(url),
 
171
  NULL
 
172
};
 
173
 
 
174
 
 
175
drizzle_declare_plugin(auth_http)
 
176
{
 
177
  "auth_http",
159
178
  "0.1",
160
179
  "Mark Atwood",
161
180
  "HTTP based authenication.",
162
181
  PLUGIN_LICENSE_GPL,
163
182
  initialize, /* Plugin Init */
164
 
  NULL,
165
 
  init_options    /* config options */
 
183
  finalize, /* Plugin Deinit */
 
184
  NULL,   /* status variables */
 
185
  auth_http_system_variables,
 
186
  NULL    /* config options */
166
187
}
167
 
DRIZZLE_DECLARE_PLUGIN_END;
 
188
drizzle_declare_plugin_end;