~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/auth_http/auth_http.cc

  • Committer: Stewart Smith
  • Date: 2008-11-21 16:06:07 UTC
  • mto: This revision was merged to the branch mainline in revision 593.
  • Revision ID: stewart@flamingspork.com-20081121160607-n6gdlt013spuo54r
remove mysql_frm_type
and fix engines to return correct value from delete_table when table doesn't exist.
(it should be ENOENT).

Also fix up some tests that manipulated frm files by hand. These tests are no longer valid and will need to be rewritten in the not too distant future.

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
 
#include <boost/program_options.hpp>
27
 
#include <drizzled/module/option_map.h>
28
 
#include "drizzled/security_context.h"
29
 
#include "drizzled/plugin/authentication.h"
30
 
#include "drizzled/gettext.h"
31
 
namespace po= boost::program_options;
32
 
using namespace drizzled;
33
 
using namespace std;
34
 
 
35
 
static bool sysvar_auth_http_enable;
36
 
static char* sysvar_auth_http_url= NULL;
37
 
 
38
 
static size_t curl_cb_read(void *ptr, size_t size, size_t nmemb, void *stream)
39
 
{
40
 
  (void) ptr;
41
 
  (void) stream;
42
 
  return (size * nmemb);
43
 
}
44
 
 
45
 
 
46
 
class Auth_http : public drizzled::plugin::Authentication
47
 
{
48
 
  CURLcode rv;
49
 
  CURL *curl_handle;
50
 
public:
51
 
  Auth_http(std::string name_arg)
52
 
    : drizzled::plugin::Authentication(name_arg)
53
 
  {
54
 
    // we are trusting that plugin initializers are called singlethreaded at startup
55
 
    // if something else also calls curl_global_init() in a threadrace while we are here,
56
 
    // we will crash the server. 
57
 
    curl_handle= curl_easy_init();
58
 
 
59
 
    // turn off curl stuff that might mess us up
60
 
    rv= curl_easy_setopt(curl_handle, CURLOPT_VERBOSE, 0);
61
 
    rv= curl_easy_setopt(curl_handle, CURLOPT_NOPROGRESS, 1);
62
 
    rv= curl_easy_setopt(curl_handle, CURLOPT_NOSIGNAL, 1);
63
 
 
64
 
    // do a HEAD instead of a default GET
65
 
    rv= curl_easy_setopt(curl_handle, CURLOPT_NOBODY, 1);
66
 
 
67
 
    // set the read callback.  this shouldnt get called, because we are doing a HEAD
68
 
    rv= curl_easy_setopt(curl_handle, CURLOPT_READFUNCTION, curl_cb_read);
69
 
  }
70
 
 
71
 
  ~Auth_http()
72
 
  {
73
 
    curl_easy_cleanup(curl_handle);
74
 
    curl_global_cleanup();
75
 
  }
76
 
 
77
 
  virtual bool authenticate(const SecurityContext &sctx, const string &password)
78
 
  {
79
 
    long http_response_code;
80
 
 
81
 
    if (sysvar_auth_http_enable == false)
82
 
      return true;
83
 
 
84
 
    assert(sctx.getUser().c_str());
85
 
 
86
 
 
87
 
    // set the parameters: url, username, password
88
 
    rv= curl_easy_setopt(curl_handle, CURLOPT_URL, sysvar_auth_http_url);
89
 
#if defined(HAVE_CURLOPT_USERNAME)
90
 
 
91
 
    rv= curl_easy_setopt(curl_handle, CURLOPT_USERNAME,
92
 
                         sctx.getUser().c_str());
93
 
    rv= curl_easy_setopt(curl_handle, CURLOPT_PASSWORD, password.c_str());
94
 
 
95
 
#else
96
 
 
97
 
    string userpwd(sctx.getUser());
98
 
    userpwd.append(":");
99
 
    userpwd.append(password);
100
 
    rv= curl_easy_setopt(curl_handle, CURLOPT_USERPWD, userpwd.c_str());
101
 
 
102
 
#endif /* defined(HAVE_CURLOPT_USERNAME) */
103
 
 
104
 
    // do it
105
 
    rv= curl_easy_perform(curl_handle);
106
 
 
107
 
    // what did we get? goes into http_response_code
108
 
    rv= curl_easy_getinfo(curl_handle, CURLINFO_RESPONSE_CODE, &http_response_code);
109
 
 
110
 
    // so here is an interesting question.
111
 
    // return true if the response_code is 2XX, or return false if its 4XX
112
 
    // for now, return false for 401, true otherwise
113
 
    // this means that if the url breaks, then anyone can log in
114
 
    // this might be the wrong thing
115
 
 
116
 
    if (http_response_code == 401)
117
 
      return false;
118
 
    return true;
119
 
  }
120
 
};
121
 
 
122
 
Auth_http* auth= NULL;
123
 
 
124
 
static int initialize(drizzled::module::Context &context)
125
 
{
126
 
  /* 
127
 
   * Per libcurl manual, in multi-threaded applications, curl_global_init() should
128
 
   * be called *before* curl_easy_init()...which is called in Auto_http's 
129
 
   * constructor.
130
 
   */
131
 
  if (curl_global_init(CURL_GLOBAL_NOTHING) != 0)
132
 
    return 1;
133
 
 
134
 
  auth= new Auth_http("auth_http");
135
 
  context.add(auth);
136
 
 
137
 
  return 0;
138
 
}
139
 
 
140
 
static void init_options(drizzled::module::option_context &context)
141
 
{
142
 
   context("enable", po::value<bool>(&sysvar_auth_http_enable)->default_value(false)->zero_tokens(),
143
 
           N_("Enable HTTP Auth check"));
144
 
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
 
 
171
 
 
172
 
DRIZZLE_DECLARE_PLUGIN
173
 
{
174
 
  DRIZZLE_VERSION_ID,
175
 
  "auth-http",
176
 
  "0.1",
177
 
  "Mark Atwood",
178
 
  "HTTP based authenication.",
179
 
  PLUGIN_LICENSE_GPL,
180
 
  initialize, /* Plugin Init */
181
 
  auth_http_system_variables,
182
 
  init_options    /* config options */
183
 
}
184
 
DRIZZLE_DECLARE_PLUGIN_END;