~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/auth_http/auth_http.cc

  • Committer: Brian Aker
  • Date: 2010-10-28 17:12:01 UTC
  • mfrom: (1887.1.3 merge)
  • Revision ID: brian@tangent.org-20101028171201-baj6l1bnntn1s4ad
Merge in POTFILES changes.

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