~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/console/console.cc

  • Committer: Stewart Smith
  • Date: 2010-03-18 12:01:34 UTC
  • mto: (1666.2.3 build)
  • mto: This revision was merged to the branch mainline in revision 1596.
  • Revision ID: stewart@flamingspork.com-20100318120134-45fdnsw8g3j6c7oy
move RAND() into a plugin

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
#include <drizzled/plugin/listen_tcp.h>
19
19
#include <drizzled/plugin/client.h>
20
20
#include <drizzled/session.h>
21
 
#include <drizzled/module/option_map.h>
22
21
 
23
22
#include <iostream>
24
23
 
25
 
#include <boost/program_options.hpp>
26
 
 
27
24
using namespace std;
28
25
using namespace drizzled;
29
26
 
30
 
namespace po= boost::program_options;
31
 
 
32
27
static bool enabled= false;
33
28
static bool debug_enabled= false;
 
29
static char* user = (char*)"";
 
30
static char* password = (char*)"";
 
31
static char* db = NULL;
34
32
 
35
33
 
36
34
class ClientConsole: public plugin::Client
38
36
  bool is_dead;
39
37
  uint32_t column;
40
38
  uint32_t max_column;
41
 
  const std::string &username;
42
 
  const std::string &password;
43
 
  const std::string &db;
44
39
 
45
40
public:
46
 
  ClientConsole(const std::string &username_arg,
47
 
                const std::string &password_arg,
48
 
                const std::string &db_arg) :
 
41
  ClientConsole():
49
42
    is_dead(false),
50
43
    column(0),
51
 
    max_column(0),
52
 
    username(username_arg),
53
 
    password(password_arg),
54
 
    db(db_arg)
 
44
    max_column(0)
55
45
  {}
56
46
 
57
47
  virtual void printDebug(const char *message)
99
89
  virtual bool authenticate(void)
100
90
  {
101
91
    printDebug("authenticate");
102
 
    session->getSecurityContext().setUser(username);
103
 
    return session->checkUser(password, db);
 
92
    session->getSecurityContext().setUser(user);
 
93
    return session->checkUser(password, strlen(password), db);
104
94
  }
105
95
 
106
96
  virtual bool readCommand(char **packet, uint32_t *packet_length)
199
189
 
200
190
    char buff[MAX_FIELD_WIDTH];
201
191
    String str(buff, sizeof(buff), &my_charset_bin);
202
 
    from->val_str_internal(&str);
 
192
    from->val_str(&str);
203
193
    return store(str.ptr(), str.length());
204
194
  }
205
195
 
274
264
class ListenConsole: public plugin::Listen
275
265
{
276
266
  int pipe_fds[2];
277
 
  const std::string username;
278
 
  const std::string password;
279
 
  const std::string db;
280
267
 
281
268
public:
282
 
  ListenConsole(const std::string &name_arg,
283
 
                const std::string &username_arg,
284
 
                const std::string &password_arg,
285
 
                const std::string &db_arg) :
286
 
    plugin::Listen(name_arg),
287
 
    username(username_arg),
288
 
    password(password_arg),
289
 
    db(db_arg)
 
269
  ListenConsole(std::string name_arg)
 
270
    : plugin::Listen(name_arg)
290
271
  {
291
272
    pipe_fds[0]= -1;
292
273
  }
305
286
    if (debug_enabled)
306
287
      enabled= true;
307
288
 
308
 
    if (not enabled)
 
289
    if (enabled == false)
309
290
      return false;
310
291
 
311
292
    if (pipe(pipe_fds) == -1)
323
304
  {
324
305
    char buffer[1];
325
306
    assert(read(fd, buffer, 1) == 1);
326
 
    return new ClientConsole(username, password, db);
 
307
    return new ClientConsole;
327
308
  }
328
309
};
329
310
 
330
 
static int init(drizzled::module::Context &context)
331
 
{
332
 
  const module::option_map &vm= context.getOptions();
333
 
  const string username(vm.count("username") ? vm["username"].as<string>() : "");
334
 
  const string password(vm.count("password") ? vm["password"].as<string>() : "");
335
 
  const string db(vm.count("db") ? vm["db"].as<string>() : "");
336
 
  context.registerVariable(new sys_var_bool_ptr("enable", &enabled));
337
 
  context.registerVariable(new sys_var_bool_ptr("debug_enable", &debug_enabled));
338
 
  context.registerVariable(new sys_var_const_string_val("username", username));
339
 
  context.registerVariable(new sys_var_const_string_val("password", password));
340
 
  context.registerVariable(new sys_var_const_string_val("db", db));
341
 
  context.add(new ListenConsole("console", username, password, db));
342
 
  return 0;
343
 
}
344
 
 
345
 
static void init_options(drizzled::module::option_context &context)
346
 
{
347
 
  context("enable",
348
 
          po::value<bool>(&enabled)->default_value(false)->zero_tokens(),
349
 
          N_("Enable the console."));
350
 
  context("debug",
351
 
          po::value<bool>(&debug_enabled)->default_value(false)->zero_tokens(),
352
 
          N_("Turn on extra debugging."));
353
 
  context("username",
354
 
          po::value<string>(),
355
 
          N_("User to use for auth."));
356
 
  context("password",
357
 
          po::value<string>(),
358
 
          N_("Password to use for auth."));
359
 
  context("db",
360
 
          po::value<string>(),
361
 
          N_("Default database to use."));
362
 
}
 
311
static ListenConsole *listen_obj= NULL;
 
312
 
 
313
static int init(drizzled::plugin::Registry &registry)
 
314
{
 
315
  listen_obj= new ListenConsole("console");
 
316
  registry.add(listen_obj);
 
317
  return 0;
 
318
}
 
319
 
 
320
static int deinit(drizzled::plugin::Registry &registry)
 
321
{
 
322
  registry.remove(listen_obj);
 
323
  delete listen_obj;
 
324
  return 0;
 
325
}
 
326
 
 
327
static DRIZZLE_SYSVAR_BOOL(enable, enabled, PLUGIN_VAR_NOCMDARG,
 
328
                           N_("Enable the console."), NULL, NULL, false);
 
329
 
 
330
static DRIZZLE_SYSVAR_BOOL(debug, debug_enabled, PLUGIN_VAR_NOCMDARG,
 
331
                           N_("Turn on extra debugging."), NULL, NULL, false);
 
332
static DRIZZLE_SYSVAR_STR(user, user, PLUGIN_VAR_READONLY,
 
333
                          N_("User to use for auth."), NULL, NULL, NULL);
 
334
static DRIZZLE_SYSVAR_STR(password, password, PLUGIN_VAR_READONLY,
 
335
                          N_("Password to use for auth."), NULL, NULL, NULL);
 
336
static DRIZZLE_SYSVAR_STR(db, db, PLUGIN_VAR_READONLY,
 
337
                          N_("Default database to use."), NULL, NULL, NULL);
 
338
 
 
339
static drizzle_sys_var* vars[]= {
 
340
  DRIZZLE_SYSVAR(enable),
 
341
  DRIZZLE_SYSVAR(debug),
 
342
  DRIZZLE_SYSVAR(user),
 
343
  DRIZZLE_SYSVAR(password),
 
344
  DRIZZLE_SYSVAR(db),
 
345
  NULL
 
346
};
363
347
 
364
348
DRIZZLE_DECLARE_PLUGIN
365
349
{
370
354
  "Console Client",
371
355
  PLUGIN_LICENSE_BSD,
372
356
  init,   /* Plugin Init */
373
 
  NULL,   /* system variables */
374
 
  init_options    /* config options */
 
357
  deinit, /* Plugin Deinit */
 
358
  vars,   /* system variables */
 
359
  NULL    /* config options */
375
360
}
376
361
DRIZZLE_DECLARE_PLUGIN_END;