~drizzle-trunk/drizzle/development

1122.2.2 by Monty Taylor
Added missing copyright headers. Added drizzled/global.h to a few things that
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
974.2.1 by Mark Atwood
add auth_http plugin
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
1122.2.2 by Monty Taylor
Added missing copyright headers. Added drizzled/global.h to a few things that
3
 *
1999.6.1 by kalebral at gmail
update Copyright strings to a more common format to help with creating the master debian copyright file
4
 *  Copyright (C) 2009 Sun Microsystems, Inc.
1122.2.2 by Monty Taylor
Added missing copyright headers. Added drizzled/global.h to a few things that
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
 */
974.2.1 by Mark Atwood
add auth_http plugin
19
2173.2.1 by Monty Taylor
Fixes incorrect usage of include
20
#include <config.h>
974.2.1 by Mark Atwood
add auth_http plugin
21
22
#include <curl/curl.h>
23
971.1.72 by Monty Taylor
Migrated Mark's new plugin to new plugin registration.
24
#include <string>
1317.1.3 by Monty Taylor
Fixed Authentication plugin interface to use SecurityContext rather than the
25
#include <cassert>
1660.2.1 by Vijay Samuel
Merge Refactored commandline with boost::program_options
26
#include <boost/program_options.hpp>
27
#include <drizzled/module/option_map.h>
2173.2.1 by Monty Taylor
Fixes incorrect usage of include
28
#include <drizzled/identifier.h>
29
#include <drizzled/plugin/authentication.h>
30
#include <drizzled/gettext.h>
1660.2.1 by Vijay Samuel
Merge Refactored commandline with boost::program_options
31
namespace po= boost::program_options;
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
32
using namespace drizzled;
971.1.72 by Monty Taylor
Migrated Mark's new plugin to new plugin registration.
33
using namespace std;
974.2.1 by Mark Atwood
add auth_http plugin
34
1085.1.2 by Monty Taylor
Fixed -Wmissing-declarations
35
static size_t curl_cb_read(void *ptr, size_t size, size_t nmemb, void *stream)
974.2.1 by Mark Atwood
add auth_http plugin
36
{
37
  (void) ptr;
38
  (void) stream;
39
  return (size * nmemb);
40
}
41
42
1130.1.1 by Monty Taylor
Merged in plugin-slot-reorg patches.
43
class Auth_http : public drizzled::plugin::Authentication
974.2.1 by Mark Atwood
add auth_http plugin
44
{
971.1.72 by Monty Taylor
Migrated Mark's new plugin to new plugin registration.
45
  CURLcode rv;
46
  CURL *curl_handle;
1945.1.3 by Monty Taylor
Updates to haildb and auth_http.
47
  const std::string auth_url;
974.2.1 by Mark Atwood
add auth_http plugin
48
public:
1945.1.3 by Monty Taylor
Updates to haildb and auth_http.
49
  Auth_http(std::string name_arg, const std::string &url_arg) :
50
    drizzled::plugin::Authentication(name_arg),
51
    auth_url(url_arg)
974.2.1 by Mark Atwood
add auth_http plugin
52
  {
971.1.72 by Monty Taylor
Migrated Mark's new plugin to new plugin registration.
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();
974.2.1 by Mark Atwood
add auth_http plugin
57
58
    // turn off curl stuff that might mess us up
971.1.72 by Monty Taylor
Migrated Mark's new plugin to new plugin registration.
59
    rv= curl_easy_setopt(curl_handle, CURLOPT_VERBOSE, 0);
974.2.1 by Mark Atwood
add auth_http plugin
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);
971.1.72 by Monty Taylor
Migrated Mark's new plugin to new plugin registration.
68
  }
69
70
  ~Auth_http()
71
  {
72
    curl_easy_cleanup(curl_handle);
1324.2.3 by Monty Taylor
Remove plugin deinit.
73
    curl_global_cleanup();
971.1.72 by Monty Taylor
Migrated Mark's new plugin to new plugin registration.
74
  }
75
2008.1.1 by Brian Aker
Adding user identifier that makes use of a shared ptr to handle concurrency
76
  virtual bool authenticate(const identifier::User &sctx, const string &password)
971.1.72 by Monty Taylor
Migrated Mark's new plugin to new plugin registration.
77
  {
78
    long http_response_code;
79
2008.1.1 by Brian Aker
Adding user identifier that makes use of a shared ptr to handle concurrency
80
    assert(sctx.username().c_str());
971.1.72 by Monty Taylor
Migrated Mark's new plugin to new plugin registration.
81
974.2.1 by Mark Atwood
add auth_http plugin
82
    // set the parameters: url, username, password
1945.1.3 by Monty Taylor
Updates to haildb and auth_http.
83
    rv= curl_easy_setopt(curl_handle, CURLOPT_URL, auth_url.c_str());
971.1.72 by Monty Taylor
Migrated Mark's new plugin to new plugin registration.
84
#if defined(HAVE_CURLOPT_USERNAME)
85
86
    rv= curl_easy_setopt(curl_handle, CURLOPT_USERNAME,
2008.1.1 by Brian Aker
Adding user identifier that makes use of a shared ptr to handle concurrency
87
                         sctx.username().c_str());
1317.1.3 by Monty Taylor
Fixed Authentication plugin interface to use SecurityContext rather than the
88
    rv= curl_easy_setopt(curl_handle, CURLOPT_PASSWORD, password.c_str());
974.2.1 by Mark Atwood
add auth_http plugin
89
971.1.72 by Monty Taylor
Migrated Mark's new plugin to new plugin registration.
90
#else
91
2008.1.1 by Brian Aker
Adding user identifier that makes use of a shared ptr to handle concurrency
92
    string userpwd(sctx.username());
971.1.72 by Monty Taylor
Migrated Mark's new plugin to new plugin registration.
93
    userpwd.append(":");
94
    userpwd.append(password);
95
    rv= curl_easy_setopt(curl_handle, CURLOPT_USERPWD, userpwd.c_str());
96
97
#endif /* defined(HAVE_CURLOPT_USERNAME) */
98
974.2.1 by Mark Atwood
add auth_http plugin
99
    // do it
100
    rv= curl_easy_perform(curl_handle);
101
102
    // what did we get? goes into http_response_code
103
    rv= curl_easy_getinfo(curl_handle, CURLINFO_RESPONSE_CODE, &http_response_code);
104
105
    // so here is an interesting question.
106
    // return true if the response_code is 2XX, or return false if its 4XX
107
    // for now, return false for 401, true otherwise
108
    // this means that if the url breaks, then anyone can log in
109
    // this might be the wrong thing
110
111
    if (http_response_code == 401)
112
      return false;
113
    return true;
114
  }
115
};
116
971.1.72 by Monty Taylor
Migrated Mark's new plugin to new plugin registration.
117
Auth_http* auth= NULL;
118
1530.2.6 by Monty Taylor
Moved plugin::Context to module::Context.
119
static int initialize(drizzled::module::Context &context)
974.2.1 by Mark Atwood
add auth_http plugin
120
{
1945.1.3 by Monty Taylor
Updates to haildb and auth_http.
121
  const module::option_map &vm= context.getOptions();
122
1089.1.2 by Brian Aker
Rename work (cheery pick from new-cleanup). Jay's fix for auth_http. Update
123
  /* 
124
   * Per libcurl manual, in multi-threaded applications, curl_global_init() should
125
   * be called *before* curl_easy_init()...which is called in Auto_http's 
126
   * constructor.
127
   */
128
  if (curl_global_init(CURL_GLOBAL_NOTHING) != 0)
129
    return 1;
130
1945.1.3 by Monty Taylor
Updates to haildb and auth_http.
131
  const string auth_url(vm["url"].as<string>());
132
  if (auth_url.size() == 0)
133
  {
2126.3.3 by Brian Aker
Merge in error message rework. Many error messages are fixed in this patch.
134
    errmsg_printf(error::ERROR,
1945.1.3 by Monty Taylor
Updates to haildb and auth_http.
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);
1324.2.2 by Monty Taylor
Use the plugin::Context everywhere.
142
  context.add(auth);
1945.1.3 by Monty Taylor
Updates to haildb and auth_http.
143
  context.registerVariable(new sys_var_const_string_val("url", auth_url));
974.2.1 by Mark Atwood
add auth_http plugin
144
145
  return 0;
146
}
147
1660.2.1 by Vijay Samuel
Merge Refactored commandline with boost::program_options
148
static void init_options(drizzled::module::option_context &context)
149
{
1945.1.3 by Monty Taylor
Updates to haildb and auth_http.
150
  context("url", po::value<string>()->default_value(""),
151
          N_("URL for HTTP Auth check"));
1660.2.1 by Vijay Samuel
Merge Refactored commandline with boost::program_options
152
} 
153
974.2.1 by Mark Atwood
add auth_http plugin
154
1228.1.5 by Monty Taylor
Merged in some naming things.
155
DRIZZLE_DECLARE_PLUGIN
974.2.1 by Mark Atwood
add auth_http plugin
156
{
1241.10.2 by Monty Taylor
Added support for embedding the drizzle version number in the plugin file.
157
  DRIZZLE_VERSION_ID,
1660.2.1 by Vijay Samuel
Merge Refactored commandline with boost::program_options
158
  "auth-http",
974.2.1 by Mark Atwood
add auth_http plugin
159
  "0.1",
160
  "Mark Atwood",
971.1.76 by Monty Taylor
Fixed a comment string.
161
  "HTTP based authenication.",
974.2.1 by Mark Atwood
add auth_http plugin
162
  PLUGIN_LICENSE_GPL,
163
  initialize, /* Plugin Init */
1945.1.3 by Monty Taylor
Updates to haildb and auth_http.
164
  NULL,
1660.2.1 by Vijay Samuel
Merge Refactored commandline with boost::program_options
165
  init_options    /* config options */
974.2.1 by Mark Atwood
add auth_http plugin
166
}
1228.1.5 by Monty Taylor
Merged in some naming things.
167
DRIZZLE_DECLARE_PLUGIN_END;