~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/console/console.cc

  • Committer: Patrick Crews
  • Date: 2010-09-14 20:21:03 UTC
  • mto: (1771.1.1 pcrews)
  • mto: This revision was merged to the branch mainline in revision 1772.
  • Revision ID: gleebix@gmail.com-20100914202103-1db2n0bshzafep19
Moved transaction_log tests into updated non-publisher-based tree

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* Copyright (C) 2009 Sun Microsystems, Inc.
 
1
/* Copyright (C) 2009 Sun Microsystems
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
20
20
#include <drizzled/session.h>
21
21
#include <drizzled/module/option_map.h>
22
22
 
23
 
#include <drizzled/plugin/catalog.h>
24
 
 
25
23
#include <iostream>
26
24
 
27
25
#include <boost/program_options.hpp>
33
31
 
34
32
static bool enabled= false;
35
33
static bool debug_enabled= false;
 
34
static char* username= NULL;
 
35
static char* password= NULL;
 
36
static char* db= NULL;
36
37
 
37
38
 
38
39
class ClientConsole: public plugin::Client
40
41
  bool is_dead;
41
42
  uint32_t column;
42
43
  uint32_t max_column;
43
 
  const std::string &username;
44
 
  const std::string &password;
45
 
  const std::string &schema;
46
 
  const std::string &_catalog;
47
44
 
48
45
public:
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) :
 
46
  ClientConsole():
53
47
    is_dead(false),
54
48
    column(0),
55
 
    max_column(0),
56
 
    username(username_arg),
57
 
    password(password_arg),
58
 
    schema(schema_arg),
59
 
    _catalog(catalog_arg)
 
49
    max_column(0)
60
50
  {}
61
51
 
62
52
  virtual void printDebug(const char *message)
65
55
      cout << "CONSOLE: " << message << endl;
66
56
  }
67
57
 
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
 
 
79
58
  virtual int getFileDescriptor(void)
80
59
  {
81
60
    printDebug("getFileDescriptor");
115
94
  virtual bool authenticate(void)
116
95
  {
117
96
    printDebug("authenticate");
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);
 
97
    session->getSecurityContext().setUser(username);
 
98
    return session->checkUser(password, strlen(password), db);
123
99
  }
124
100
 
125
101
  virtual bool readCommand(char **packet, uint32_t *packet_length)
150
126
    }
151
127
    while (cin.eof() == false && cin.fail() == true);
152
128
 
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))
 
129
    if ((*packet_length == 1 && cin.eof() == true) ||
 
130
        !strncasecmp(*packet + 1, "quit", 4) ||
 
131
        !strncasecmp(*packet + 1, "exit", 4))
157
132
    {
158
133
      is_dead= true;
159
134
      *packet_length= 1;
160
135
      (*packet)[0]= COM_SHUTDOWN;
161
 
 
162
136
      return true;
163
137
    }
164
138
 
166
140
    cin.ignore(2, '\n');
167
141
 
168
142
    (*packet)[0]= COM_QUERY;
169
 
 
170
143
    return true;
171
144
  }
172
145
 
180
153
    printDebug("sendEOF");
181
154
  }
182
155
 
183
 
  virtual void sendError(const drizzled::error_t sql_errno, const char *err)
 
156
  virtual void sendError(uint32_t sql_errno, const char *err)
184
157
  {
185
 
    cout << "Error: " << static_cast<long>(sql_errno) << " " << err << endl;
 
158
    cout << "Error: " << sql_errno << " " << err << endl;
186
159
  }
187
160
 
188
161
  virtual bool sendFields(List<Item> *list)
221
194
 
222
195
    char buff[MAX_FIELD_WIDTH];
223
196
    String str(buff, sizeof(buff), &my_charset_bin);
224
 
    from->val_str_internal(&str);
 
197
    from->val_str(&str);
225
198
    return store(str.ptr(), str.length());
226
199
  }
227
200
 
291
264
    printDebug("wasAborted");
292
265
    return false;
293
266
  }
294
 
 
295
 
  bool isConsole()
296
 
  {
297
 
    return true;
298
 
  }
299
267
};
300
268
 
301
269
class ListenConsole: public plugin::Listen
302
270
{
303
271
  int pipe_fds[2];
304
 
  const std::string username;
305
 
  const std::string password;
306
 
  const std::string schema;
307
 
  const std::string _catalog;
308
272
 
309
273
public:
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)
 
274
  ListenConsole(const std::string &name_arg) :
 
275
    plugin::Listen(name_arg)
320
276
  {
321
277
    pipe_fds[0]= -1;
322
278
  }
328
284
      close(pipe_fds[0]);
329
285
      close(pipe_fds[1]);
330
286
    }
 
287
 
 
288
    /* Cleanup from the module strdup'ing these below */
 
289
    free(username);
 
290
    free(password);
 
291
    free(db);
331
292
  }
332
293
 
333
294
  virtual bool getFileDescriptors(std::vector<int> &fds)
335
296
    if (debug_enabled)
336
297
      enabled= true;
337
298
 
338
 
    if (not enabled)
 
299
    if (enabled == false)
339
300
      return false;
340
301
 
341
302
    if (pipe(pipe_fds) == -1)
342
303
    {
343
 
      errmsg_printf(error::ERROR, _("pipe() failed with errno %d"), errno);
 
304
      errmsg_printf(ERRMSG_LVL_ERROR, _("pipe() failed with errno %d"), errno);
344
305
      return true;
345
306
    }
346
307
 
353
314
  {
354
315
    char buffer[1];
355
316
    assert(read(fd, buffer, 1) == 1);
356
 
 
357
 
    return new ClientConsole(username, password, schema, _catalog);
 
317
    return new ClientConsole;
358
318
  }
359
319
};
360
320
 
361
321
static int init(drizzled::module::Context &context)
362
322
{
363
323
  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
 
 
 
324
  /* duplicating these here means they need to be freed. They're global, so
 
325
     we'll just have the ListenConsole object do it in its destructor */
 
326
  if (vm.count("username"))
 
327
    username= strdup(vm["username"].as<string>().c_str());
 
328
  else
 
329
    username= strdup("");
 
330
 
 
331
  if (vm.count("password"))
 
332
    password= strdup(vm["password"].as<string>().c_str());
 
333
  else
 
334
    password= strdup("");
 
335
 
 
336
  if (vm.count("db"))
 
337
    db= strdup(vm["db"].as<string>().c_str());
 
338
  else
 
339
    db= strdup("");
 
340
 
 
341
  context.add(new ListenConsole("console"));
372
342
  return 0;
373
343
}
374
344
 
 
345
static DRIZZLE_SYSVAR_BOOL(enable, enabled, PLUGIN_VAR_NOCMDARG,
 
346
                           N_("Enable the console."), NULL, NULL, false);
 
347
static DRIZZLE_SYSVAR_BOOL(debug, debug_enabled, PLUGIN_VAR_NOCMDARG,
 
348
                           N_("Turn on extra debugging."), NULL, NULL, false);
 
349
 
 
350
static DRIZZLE_SYSVAR_STR(username, username, PLUGIN_VAR_READONLY,
 
351
                          N_("User to use for auth."), NULL, NULL, NULL);
 
352
static DRIZZLE_SYSVAR_STR(password, password, PLUGIN_VAR_READONLY,
 
353
                          N_("Password to use for auth."), NULL, NULL, NULL);
 
354
static DRIZZLE_SYSVAR_STR(db, db, PLUGIN_VAR_READONLY,
 
355
                          N_("Default database to use."), NULL, NULL, NULL);
 
356
 
375
357
static void init_options(drizzled::module::option_context &context)
376
358
{
377
359
  context("enable",
386
368
  context("password",
387
369
          po::value<string>(),
388
370
          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."));
 
371
  context("db",
 
372
          po::value<string>(),
 
373
          N_("Default database to use."));
395
374
}
396
375
 
 
376
static drizzle_sys_var* vars[]= {
 
377
  DRIZZLE_SYSVAR(enable),
 
378
  DRIZZLE_SYSVAR(debug),
 
379
  DRIZZLE_SYSVAR(username),
 
380
  DRIZZLE_SYSVAR(password),
 
381
  DRIZZLE_SYSVAR(db),
 
382
  NULL
 
383
};
 
384
 
397
385
DRIZZLE_DECLARE_PLUGIN
398
386
{
399
387
  DRIZZLE_VERSION_ID,
400
388
  "console",
401
 
  "0.2",
 
389
  "0.1",
402
390
  "Eric Day",
403
391
  "Console Client",
404
392
  PLUGIN_LICENSE_BSD,
405
393
  init,   /* Plugin Init */
406
 
  NULL,   /* depends */
 
394
  vars,   /* system variables */
407
395
  init_options    /* config options */
408
396
}
409
397
DRIZZLE_DECLARE_PLUGIN_END;