~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/auth_http/auth_http.cc

  • Committer: Brian Aker
  • Date: 2011-02-22 06:12:02 UTC
  • mfrom: (2190.1.6 drizzle-build)
  • Revision ID: brian@tangent.org-20110222061202-k03czxykqy4x9hjs
List update, header fixes, multiple symbols, and David deletes some code.

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
 
4
 *  Copyright (C) 2009 Sun Microsystems, Inc.
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 <config.h>
21
21
 
22
22
#include <curl/curl.h>
23
23
 
25
25
#include <cassert>
26
26
#include <boost/program_options.hpp>
27
27
#include <drizzled/module/option_map.h>
28
 
#include "drizzled/security_context.h"
29
 
#include "drizzled/plugin/authentication.h"
30
 
#include "drizzled/gettext.h"
 
28
#include <drizzled/identifier.h>
 
29
#include <drizzled/plugin/authentication.h>
 
30
#include <drizzled/gettext.h>
31
31
namespace po= boost::program_options;
32
32
using namespace drizzled;
33
33
using namespace std;
34
34
 
35
 
static bool sysvar_auth_http_enable;
36
 
static char* sysvar_auth_http_url= NULL;
37
 
 
38
35
static size_t curl_cb_read(void *ptr, size_t size, size_t nmemb, void *stream)
39
36
{
40
37
  (void) ptr;
47
44
{
48
45
  CURLcode rv;
49
46
  CURL *curl_handle;
 
47
  const std::string auth_url;
50
48
public:
51
 
  Auth_http(std::string name_arg)
52
 
    : drizzled::plugin::Authentication(name_arg)
 
49
  Auth_http(std::string name_arg, const std::string &url_arg) :
 
50
    drizzled::plugin::Authentication(name_arg),
 
51
    auth_url(url_arg)
53
52
  {
54
53
    // we are trusting that plugin initializers are called singlethreaded at startup
55
54
    // if something else also calls curl_global_init() in a threadrace while we are here,
74
73
    curl_global_cleanup();
75
74
  }
76
75
 
77
 
  virtual bool authenticate(const SecurityContext &sctx, const string &password)
 
76
  virtual bool authenticate(const identifier::User &sctx, const string &password)
78
77
  {
79
78
    long http_response_code;
80
79
 
81
 
    if (sysvar_auth_http_enable == false)
82
 
      return true;
83
 
 
84
 
    assert(sctx.getUser().c_str());
85
 
 
 
80
    assert(sctx.username().c_str());
86
81
 
87
82
    // set the parameters: url, username, password
88
 
    rv= curl_easy_setopt(curl_handle, CURLOPT_URL, sysvar_auth_http_url);
 
83
    rv= curl_easy_setopt(curl_handle, CURLOPT_URL, auth_url.c_str());
89
84
#if defined(HAVE_CURLOPT_USERNAME)
90
85
 
91
86
    rv= curl_easy_setopt(curl_handle, CURLOPT_USERNAME,
92
 
                         sctx.getUser().c_str());
 
87
                         sctx.username().c_str());
93
88
    rv= curl_easy_setopt(curl_handle, CURLOPT_PASSWORD, password.c_str());
94
89
 
95
90
#else
96
91
 
97
 
    string userpwd(sctx.getUser());
 
92
    string userpwd(sctx.username());
98
93
    userpwd.append(":");
99
94
    userpwd.append(password);
100
95
    rv= curl_easy_setopt(curl_handle, CURLOPT_USERPWD, userpwd.c_str());
123
118
 
124
119
static int initialize(drizzled::module::Context &context)
125
120
{
 
121
  const module::option_map &vm= context.getOptions();
 
122
 
126
123
  /* 
127
124
   * Per libcurl manual, in multi-threaded applications, curl_global_init() should
128
125
   * be called *before* curl_easy_init()...which is called in Auto_http's 
131
128
  if (curl_global_init(CURL_GLOBAL_NOTHING) != 0)
132
129
    return 1;
133
130
 
134
 
  auth= new Auth_http("auth_http");
 
131
  const string auth_url(vm["url"].as<string>());
 
132
  if (auth_url.size() == 0)
 
133
  {
 
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;
 
139
  }
 
140
 
 
141
  auth= new Auth_http("auth_http", auth_url);
135
142
  context.add(auth);
 
143
  context.registerVariable(new sys_var_const_string_val("url", auth_url));
136
144
 
137
145
  return 0;
138
146
}
139
147
 
140
148
static void init_options(drizzled::module::option_context &context)
141
149
{
142
 
   context("enable", po::value<bool>(&sysvar_auth_http_enable)->default_value(false)->zero_tokens(),
143
 
           N_("Enable HTTP Auth check"));
 
150
  context("url", po::value<string>()->default_value(""),
 
151
          N_("URL for HTTP Auth check"));
144
152
145
153
 
146
 
static DRIZZLE_SYSVAR_BOOL(
147
 
  enable,
148
 
  sysvar_auth_http_enable,
149
 
  PLUGIN_VAR_NOCMDARG,
150
 
  N_("Enable HTTP Auth check"),
151
 
  NULL, /* check func */
152
 
  NULL, /* update func */
153
 
  false /* default */);
154
 
 
155
 
 
156
 
static DRIZZLE_SYSVAR_STR(
157
 
  url,
158
 
  sysvar_auth_http_url,
159
 
  PLUGIN_VAR_READONLY,
160
 
  N_("URL for HTTP Auth check"),
161
 
  NULL, /* check func */
162
 
  NULL, /* update func*/
163
 
  "http://localhost/" /* default */);
164
 
 
165
 
static drizzle_sys_var* auth_http_system_variables[]= {
166
 
  DRIZZLE_SYSVAR(enable),
167
 
  DRIZZLE_SYSVAR(url),
168
 
  NULL
169
 
};
170
 
 
171
154
 
172
155
DRIZZLE_DECLARE_PLUGIN
173
156
{
178
161
  "HTTP based authenication.",
179
162
  PLUGIN_LICENSE_GPL,
180
163
  initialize, /* Plugin Init */
181
 
  auth_http_system_variables,
 
164
  NULL,
182
165
  init_options    /* config options */
183
166
}
184
167
DRIZZLE_DECLARE_PLUGIN_END;