~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/auth_http/auth_http.cc

  • Committer: Monty Taylor
  • Date: 2009-04-14 19:16:51 UTC
  • mto: (997.2.5 mordred)
  • mto: This revision was merged to the branch mainline in revision 994.
  • Revision ID: mordred@inaugust.com-20090414191651-ltbww6hpqks8k7qk
Clarified instructions in README.

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
 
 
27
 
#include "drizzled/security_context.h"
28
 
#include "drizzled/plugin/authentication.h"
29
 
#include "drizzled/gettext.h"
30
 
 
31
 
using namespace drizzled;
 
14
 
32
15
using namespace std;
33
16
 
34
17
static bool sysvar_auth_http_enable= false;
35
18
static char* sysvar_auth_http_url= NULL;
36
19
 
37
 
static size_t curl_cb_read(void *ptr, size_t size, size_t nmemb, void *stream)
 
20
size_t curl_cb_read(void *ptr, size_t size, size_t nmemb, void *stream)
38
21
{
39
22
  (void) ptr;
40
23
  (void) stream;
42
25
}
43
26
 
44
27
 
45
 
class Auth_http : public drizzled::plugin::Authentication
 
28
class Auth_http : public Authentication
46
29
{
47
30
  CURLcode rv;
48
31
  CURL *curl_handle;
49
32
public:
50
 
  Auth_http(std::string name_arg)
51
 
    : drizzled::plugin::Authentication(name_arg)
 
33
  Auth_http() : Authentication()
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
61
    if (sysvar_auth_http_enable == false)
81
62
      return true;
82
63
 
83
 
    assert(sctx.getUser().c_str());
 
64
    assert(session->security_ctx.user.c_str());
 
65
    assert(password);
84
66
 
85
67
 
86
68
    // set the parameters: url, username, password
88
70
#if defined(HAVE_CURLOPT_USERNAME)
89
71
 
90
72
    rv= curl_easy_setopt(curl_handle, CURLOPT_USERNAME,
91
 
                         sctx.getUser().c_str());
92
 
    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);
93
75
 
94
76
#else
95
77
 
96
 
    string userpwd(sctx.getUser());
 
78
    string userpwd= session->security_ctx.user;
97
79
    userpwd.append(":");
98
80
    userpwd.append(password);
99
81
    rv= curl_easy_setopt(curl_handle, CURLOPT_USERPWD, userpwd.c_str());
120
102
 
121
103
Auth_http* auth= NULL;
122
104
 
123
 
static int initialize(drizzled::plugin::Context &context)
124
 
{
125
 
  /* 
126
 
   * Per libcurl manual, in multi-threaded applications, curl_global_init() should
127
 
   * be called *before* curl_easy_init()...which is called in Auto_http's 
128
 
   * constructor.
129
 
   */
130
 
  if (curl_global_init(CURL_GLOBAL_NOTHING) != 0)
131
 
    return 1;
132
 
 
133
 
  auth= new Auth_http("auth_http");
134
 
  context.add(auth);
 
105
static int initialize(PluginRegistry &registry)
 
106
{
 
107
  auth= new Auth_http();
 
108
  registry.add(auth);
 
109
 
 
110
  return 0;
 
111
}
 
112
 
 
113
static int finalize(PluginRegistry &registry)
 
114
{
 
115
  if (auth)
 
116
  {
 
117
    registry.remove(auth);
 
118
    delete auth;
 
119
  }
135
120
 
136
121
  return 0;
137
122
}
155
140
  NULL, /* update func*/
156
141
  "http://localhost/" /* default */);
157
142
 
158
 
static drizzle_sys_var* auth_http_system_variables[]= {
 
143
static struct st_mysql_sys_var* auth_http_system_variables[]= {
159
144
  DRIZZLE_SYSVAR(enable),
160
145
  DRIZZLE_SYSVAR(url),
161
146
  NULL
162
147
};
163
148
 
164
149
 
165
 
DRIZZLE_DECLARE_PLUGIN
 
150
drizzle_declare_plugin(auth_http)
166
151
{
167
 
  DRIZZLE_VERSION_ID,
168
152
  "auth_http",
169
153
  "0.1",
170
154
  "Mark Atwood",
171
155
  "HTTP based authenication.",
172
156
  PLUGIN_LICENSE_GPL,
173
157
  initialize, /* Plugin Init */
 
158
  finalize, /* Plugin Deinit */
 
159
  NULL,   /* status variables */
174
160
  auth_http_system_variables,
175
161
  NULL    /* config options */
176
162
}
177
 
DRIZZLE_DECLARE_PLUGIN_END;
 
163
drizzle_declare_plugin_end;