~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/console/console.cc

  • Committer: Olaf van der Spek
  • Date: 2011-02-12 18:24:24 UTC
  • mto: (2167.1.2 build) (2172.1.4 build)
  • mto: This revision was merged to the branch mainline in revision 2168.
  • Revision ID: olafvdspek@gmail.com-20110212182424-kgnm9osi7qo97at2
casts

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* Copyright (C) 2009 Sun Microsystems
 
1
/* Copyright (C) 2009 Sun Microsystems, Inc.
2
2
 
3
3
   This program is free software; you can redistribute it and/or modify
4
4
   it under the terms of the GNU General Public License as published by
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
 
 
23
#include <drizzled/plugin/catalog.h>
21
24
 
22
25
#include <iostream>
23
26
 
 
27
#include <boost/program_options.hpp>
 
28
 
24
29
using namespace std;
25
30
using namespace drizzled;
26
31
 
 
32
namespace po= boost::program_options;
 
33
 
27
34
static bool enabled= false;
28
35
static bool debug_enabled= false;
29
 
static char* user = (char*)"";
30
 
static char* password = (char*)"";
31
 
static char* db = NULL;
32
36
 
33
37
 
34
38
class ClientConsole: public plugin::Client
36
40
  bool is_dead;
37
41
  uint32_t column;
38
42
  uint32_t max_column;
 
43
  const std::string &username;
 
44
  const std::string &password;
 
45
  const std::string &schema;
 
46
  const std::string &_catalog;
39
47
 
40
48
public:
41
 
  ClientConsole():
 
49
  ClientConsole(const std::string &username_arg,
 
50
                const std::string &password_arg,
 
51
                const std::string &schema_arg,
 
52
                const std::string &catalog_arg) :
42
53
    is_dead(false),
43
54
    column(0),
44
 
    max_column(0)
 
55
    max_column(0),
 
56
    username(username_arg),
 
57
    password(password_arg),
 
58
    schema(schema_arg),
 
59
    _catalog(catalog_arg)
45
60
  {}
46
61
 
47
62
  virtual void printDebug(const char *message)
50
65
      cout << "CONSOLE: " << message << endl;
51
66
  }
52
67
 
 
68
  catalog::Instance::shared_ptr catalog()
 
69
  {
 
70
    identifier::Catalog identifier(_catalog);
 
71
    catalog::Instance::shared_ptr tmp= plugin::Catalog::getInstance(identifier);
 
72
    if (not tmp)
 
73
    {
 
74
      std::cerr << "Invalid catalog '" << identifier << "', resorting to 'local' catalog" << std::endl;
 
75
    }
 
76
    return tmp;
 
77
  }
 
78
 
53
79
  virtual int getFileDescriptor(void)
54
80
  {
55
81
    printDebug("getFileDescriptor");
89
115
  virtual bool authenticate(void)
90
116
  {
91
117
    printDebug("authenticate");
92
 
    session->getSecurityContext().setUser(user);
93
 
    return session->checkUser(password, strlen(password), db);
 
118
    identifier::User::shared_ptr user= identifier::User::make_shared();
 
119
    user->setUser(username);
 
120
    session->setUser(user);
 
121
 
 
122
    return session->checkUser(password, schema);
94
123
  }
95
124
 
96
125
  virtual bool readCommand(char **packet, uint32_t *packet_length)
121
150
    }
122
151
    while (cin.eof() == false && cin.fail() == true);
123
152
 
124
 
    if ((*packet_length == 1 && cin.eof() == true) ||
125
 
        !strncasecmp(*packet + 1, "quit", 4) ||
126
 
        !strncasecmp(*packet + 1, "exit", 4))
 
153
    if ((*packet_length == 1 && cin.eof() == true) or
 
154
        not strncasecmp(*packet + 1, "quit", 4) or
 
155
        not strncasecmp(*packet + 1, "exit", 4) or
 
156
        not strncasecmp(*packet + 1, "shutdown", sizeof("shutdown") -1))
127
157
    {
128
158
      is_dead= true;
129
159
      *packet_length= 1;
130
160
      (*packet)[0]= COM_SHUTDOWN;
 
161
 
131
162
      return true;
132
163
    }
133
164
 
135
166
    cin.ignore(2, '\n');
136
167
 
137
168
    (*packet)[0]= COM_QUERY;
 
169
 
138
170
    return true;
139
171
  }
140
172
 
148
180
    printDebug("sendEOF");
149
181
  }
150
182
 
151
 
  virtual void sendError(uint32_t sql_errno, const char *err)
 
183
  virtual void sendError(const drizzled::error_t sql_errno, const char *err)
152
184
  {
153
 
    cout << "Error: " << sql_errno << " " << err << endl;
 
185
    cout << "Error: " << static_cast<long>(sql_errno) << " " << err << endl;
154
186
  }
155
187
 
156
188
  virtual bool sendFields(List<Item> *list)
189
221
 
190
222
    char buff[MAX_FIELD_WIDTH];
191
223
    String str(buff, sizeof(buff), &my_charset_bin);
192
 
    from->val_str(&str);
 
224
    from->val_str_internal(&str);
193
225
    return store(str.ptr(), str.length());
194
226
  }
195
227
 
259
291
    printDebug("wasAborted");
260
292
    return false;
261
293
  }
 
294
 
 
295
  bool isConsole()
 
296
  {
 
297
    return true;
 
298
  }
262
299
};
263
300
 
264
301
class ListenConsole: public plugin::Listen
265
302
{
266
303
  int pipe_fds[2];
 
304
  const std::string username;
 
305
  const std::string password;
 
306
  const std::string schema;
 
307
  const std::string _catalog;
267
308
 
268
309
public:
269
 
  ListenConsole(std::string name_arg)
270
 
    : plugin::Listen(name_arg)
 
310
  ListenConsole(const std::string &name_arg,
 
311
                const std::string &username_arg,
 
312
                const std::string &password_arg,
 
313
                const std::string &schema_arg,
 
314
                const std::string &catalog_arg) :
 
315
    plugin::Listen(name_arg),
 
316
    username(username_arg),
 
317
    password(password_arg),
 
318
    schema(schema_arg),
 
319
    _catalog(catalog_arg)
271
320
  {
272
321
    pipe_fds[0]= -1;
273
322
  }
286
335
    if (debug_enabled)
287
336
      enabled= true;
288
337
 
289
 
    if (enabled == false)
 
338
    if (not enabled)
290
339
      return false;
291
340
 
292
341
    if (pipe(pipe_fds) == -1)
293
342
    {
294
 
      errmsg_printf(ERRMSG_LVL_ERROR, _("pipe() failed with errno %d"), errno);
 
343
      errmsg_printf(error::ERROR, _("pipe() failed with errno %d"), errno);
295
344
      return true;
296
345
    }
297
346
 
304
353
  {
305
354
    char buffer[1];
306
355
    assert(read(fd, buffer, 1) == 1);
307
 
    return new ClientConsole;
 
356
 
 
357
    return new ClientConsole(username, password, schema, _catalog);
308
358
  }
309
359
};
310
360
 
311
 
static ListenConsole *listen_obj= NULL;
312
 
 
313
 
static int init(drizzled::plugin::Context &context)
 
361
static int init(drizzled::module::Context &context)
314
362
{
315
 
  listen_obj= new ListenConsole("console");
316
 
  context.add(listen_obj);
 
363
  const module::option_map &vm= context.getOptions();
 
364
  const string username(vm.count("username") ? vm["username"].as<string>() : "");
 
365
  const string password(vm.count("password") ? vm["password"].as<string>() : "");
 
366
  const string schema(vm.count("schema") ? vm["schema"].as<string>() : "");
 
367
 
 
368
  const std::string catalog(vm.count("catalog") ? vm["catalog"].as<string>() : "LOCAL");
 
369
 
 
370
  context.add(new ListenConsole("console", username, password, schema, catalog));
 
371
 
317
372
  return 0;
318
373
}
319
374
 
320
 
static DRIZZLE_SYSVAR_BOOL(enable, enabled, PLUGIN_VAR_NOCMDARG,
321
 
                           N_("Enable the console."), NULL, NULL, false);
322
 
 
323
 
static DRIZZLE_SYSVAR_BOOL(debug, debug_enabled, PLUGIN_VAR_NOCMDARG,
324
 
                           N_("Turn on extra debugging."), NULL, NULL, false);
325
 
static DRIZZLE_SYSVAR_STR(user, user, PLUGIN_VAR_READONLY,
326
 
                          N_("User to use for auth."), NULL, NULL, NULL);
327
 
static DRIZZLE_SYSVAR_STR(password, password, PLUGIN_VAR_READONLY,
328
 
                          N_("Password to use for auth."), NULL, NULL, NULL);
329
 
static DRIZZLE_SYSVAR_STR(db, db, PLUGIN_VAR_READONLY,
330
 
                          N_("Default database to use."), NULL, NULL, NULL);
331
 
 
332
 
static drizzle_sys_var* vars[]= {
333
 
  DRIZZLE_SYSVAR(enable),
334
 
  DRIZZLE_SYSVAR(debug),
335
 
  DRIZZLE_SYSVAR(user),
336
 
  DRIZZLE_SYSVAR(password),
337
 
  DRIZZLE_SYSVAR(db),
338
 
  NULL
339
 
};
 
375
static void init_options(drizzled::module::option_context &context)
 
376
{
 
377
  context("enable",
 
378
          po::value<bool>(&enabled)->default_value(false)->zero_tokens(),
 
379
          N_("Enable the console."));
 
380
  context("debug",
 
381
          po::value<bool>(&debug_enabled)->default_value(false)->zero_tokens(),
 
382
          N_("Turn on extra debugging."));
 
383
  context("username",
 
384
          po::value<string>(),
 
385
          N_("User to use for auth."));
 
386
  context("password",
 
387
          po::value<string>(),
 
388
          N_("Password to use for auth."));
 
389
  context("catalog",
 
390
          po::value<string>(),
 
391
          N_("Default catalog to use."));
 
392
  context("schema",
 
393
          po::value<string>(),
 
394
          N_("Default schema to use."));
 
395
}
340
396
 
341
397
DRIZZLE_DECLARE_PLUGIN
342
398
{
343
399
  DRIZZLE_VERSION_ID,
344
400
  "console",
345
 
  "0.1",
 
401
  "0.2",
346
402
  "Eric Day",
347
403
  "Console Client",
348
404
  PLUGIN_LICENSE_BSD,
349
405
  init,   /* Plugin Init */
350
 
  vars,   /* system variables */
351
 
  NULL    /* config options */
 
406
  NULL,   /* depends */
 
407
  init_options    /* config options */
352
408
}
353
409
DRIZZLE_DECLARE_PLUGIN_END;