~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/auth_http/auth_http.cc

  • Committer: Brian Aker
  • Date: 2009-01-24 09:43:35 UTC
  • Revision ID: brian@gir-3.local-20090124094335-6qdtvc35gl5fvivz
Adding in an example singe thread scheduler

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
 
 *  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
 
 */
19
 
 
20
 
#include "config.h"
21
 
 
22
 
#include <curl/curl.h>
23
 
 
24
 
#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;
32
 
using namespace std;
33
 
 
34
 
static bool sysvar_auth_http_enable= false;
35
 
static char* sysvar_auth_http_url= NULL;
36
 
 
37
 
static size_t curl_cb_read(void *ptr, size_t size, size_t nmemb, void *stream)
38
 
{
39
 
  (void) ptr;
40
 
  (void) stream;
41
 
  return (size * nmemb);
42
 
}
43
 
 
44
 
 
45
 
class Auth_http : public drizzled::plugin::Authentication
46
 
{
47
 
  CURLcode rv;
48
 
  CURL *curl_handle;
49
 
public:
50
 
  Auth_http(std::string name_arg)
51
 
    : drizzled::plugin::Authentication(name_arg)
52
 
  {
53
 
    // we are trusting that plugin initializers are called singlethreaded at startup
54
 
    // if something else also calls curl_global_init() in a threadrace while we are here,
55
 
    // we will crash the server. 
56
 
    curl_handle= curl_easy_init();
57
 
 
58
 
    // turn off curl stuff that might mess us up
59
 
    rv= curl_easy_setopt(curl_handle, CURLOPT_VERBOSE, 0);
60
 
    rv= curl_easy_setopt(curl_handle, CURLOPT_NOPROGRESS, 1);
61
 
    rv= curl_easy_setopt(curl_handle, CURLOPT_NOSIGNAL, 1);
62
 
 
63
 
    // do a HEAD instead of a default GET
64
 
    rv= curl_easy_setopt(curl_handle, CURLOPT_NOBODY, 1);
65
 
 
66
 
    // set the read callback.  this shouldnt get called, because we are doing a HEAD
67
 
    rv= curl_easy_setopt(curl_handle, CURLOPT_READFUNCTION, curl_cb_read);
68
 
  }
69
 
 
70
 
  ~Auth_http()
71
 
  {
72
 
    curl_easy_cleanup(curl_handle);
73
 
    curl_global_cleanup();
74
 
  }
75
 
 
76
 
  virtual bool authenticate(const SecurityContext &sctx, const string &password)
77
 
  {
78
 
    long http_response_code;
79
 
 
80
 
    if (sysvar_auth_http_enable == false)
81
 
      return true;
82
 
 
83
 
    assert(sctx.getUser().c_str());
84
 
 
85
 
 
86
 
    // set the parameters: url, username, password
87
 
    rv= curl_easy_setopt(curl_handle, CURLOPT_URL, sysvar_auth_http_url);
88
 
#if defined(HAVE_CURLOPT_USERNAME)
89
 
 
90
 
    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());
93
 
 
94
 
#else
95
 
 
96
 
    string userpwd(sctx.getUser());
97
 
    userpwd.append(":");
98
 
    userpwd.append(password);
99
 
    rv= curl_easy_setopt(curl_handle, CURLOPT_USERPWD, userpwd.c_str());
100
 
 
101
 
#endif /* defined(HAVE_CURLOPT_USERNAME) */
102
 
 
103
 
    // do it
104
 
    rv= curl_easy_perform(curl_handle);
105
 
 
106
 
    // what did we get? goes into http_response_code
107
 
    rv= curl_easy_getinfo(curl_handle, CURLINFO_RESPONSE_CODE, &http_response_code);
108
 
 
109
 
    // so here is an interesting question.
110
 
    // return true if the response_code is 2XX, or return false if its 4XX
111
 
    // for now, return false for 401, true otherwise
112
 
    // this means that if the url breaks, then anyone can log in
113
 
    // this might be the wrong thing
114
 
 
115
 
    if (http_response_code == 401)
116
 
      return false;
117
 
    return true;
118
 
  }
119
 
};
120
 
 
121
 
Auth_http* auth= NULL;
122
 
 
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);
135
 
 
136
 
  return 0;
137
 
}
138
 
 
139
 
static DRIZZLE_SYSVAR_BOOL(
140
 
  enable,
141
 
  sysvar_auth_http_enable,
142
 
  PLUGIN_VAR_NOCMDARG,
143
 
  N_("Enable HTTP Auth check"),
144
 
  NULL, /* check func */
145
 
  NULL, /* update func */
146
 
  false /* default */);
147
 
 
148
 
 
149
 
static DRIZZLE_SYSVAR_STR(
150
 
  url,
151
 
  sysvar_auth_http_url,
152
 
  PLUGIN_VAR_READONLY,
153
 
  N_("URL for HTTP Auth check"),
154
 
  NULL, /* check func */
155
 
  NULL, /* update func*/
156
 
  "http://localhost/" /* default */);
157
 
 
158
 
static drizzle_sys_var* auth_http_system_variables[]= {
159
 
  DRIZZLE_SYSVAR(enable),
160
 
  DRIZZLE_SYSVAR(url),
161
 
  NULL
162
 
};
163
 
 
164
 
 
165
 
DRIZZLE_DECLARE_PLUGIN
166
 
{
167
 
  DRIZZLE_VERSION_ID,
168
 
  "auth_http",
169
 
  "0.1",
170
 
  "Mark Atwood",
171
 
  "HTTP based authenication.",
172
 
  PLUGIN_LICENSE_GPL,
173
 
  initialize, /* Plugin Init */
174
 
  auth_http_system_variables,
175
 
  NULL    /* config options */
176
 
}
177
 
DRIZZLE_DECLARE_PLUGIN_END;