~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/console/console.cc

  • Committer: lbieber
  • Date: 2010-09-07 22:29:56 UTC
  • mfrom: (1747.1.3 build)
  • Revision ID: lbieber@orisndriz03-20100907222956-d60u695b7hx3zs0d
Merge Andrew - bug 597778 - Timestamp cannot reliably store leap seconds, so don't try. Leave that to datetime only
Merge Andrew - bug 621862 - Add / clean-up error messages for unireg_abort
Merge Andrew - bug 629563 - Drop the broken --mysql options and Change the --protocol option to accept 'mysql' or 'drizzle' ('mysql' currently default)

Show diffs side-by-side

added added

removed removed

Lines of Context:
31
31
 
32
32
static bool enabled= false;
33
33
static bool debug_enabled= false;
 
34
static char* username= NULL;
 
35
static char* password= NULL;
 
36
static char* db= NULL;
34
37
 
35
38
 
36
39
class ClientConsole: public plugin::Client
38
41
  bool is_dead;
39
42
  uint32_t column;
40
43
  uint32_t max_column;
41
 
  const std::string &username;
42
 
  const std::string &password;
43
 
  const std::string &db;
44
44
 
45
45
public:
46
 
  ClientConsole(const std::string &username_arg,
47
 
                const std::string &password_arg,
48
 
                const std::string &db_arg) :
 
46
  ClientConsole():
49
47
    is_dead(false),
50
48
    column(0),
51
 
    max_column(0),
52
 
    username(username_arg),
53
 
    password(password_arg),
54
 
    db(db_arg)
 
49
    max_column(0)
55
50
  {}
56
51
 
57
52
  virtual void printDebug(const char *message)
99
94
  virtual bool authenticate(void)
100
95
  {
101
96
    printDebug("authenticate");
102
 
    identifier::User::shared_ptr user= identifier::User::make_shared();
103
 
    user->setUser(username);
104
 
    session->setUser(user);
105
 
    return session->checkUser(password, db);
 
97
    session->getSecurityContext().setUser(username);
 
98
    return session->checkUser(password, strlen(password), db);
106
99
  }
107
100
 
108
101
  virtual bool readCommand(char **packet, uint32_t *packet_length)
201
194
 
202
195
    char buff[MAX_FIELD_WIDTH];
203
196
    String str(buff, sizeof(buff), &my_charset_bin);
204
 
    from->val_str_internal(&str);
 
197
    from->val_str(&str);
205
198
    return store(str.ptr(), str.length());
206
199
  }
207
200
 
276
269
class ListenConsole: public plugin::Listen
277
270
{
278
271
  int pipe_fds[2];
279
 
  const std::string username;
280
 
  const std::string password;
281
 
  const std::string db;
282
272
 
283
273
public:
284
 
  ListenConsole(const std::string &name_arg,
285
 
                const std::string &username_arg,
286
 
                const std::string &password_arg,
287
 
                const std::string &db_arg) :
288
 
    plugin::Listen(name_arg),
289
 
    username(username_arg),
290
 
    password(password_arg),
291
 
    db(db_arg)
 
274
  ListenConsole(const std::string &name_arg) :
 
275
    plugin::Listen(name_arg)
292
276
  {
293
277
    pipe_fds[0]= -1;
294
278
  }
300
284
      close(pipe_fds[0]);
301
285
      close(pipe_fds[1]);
302
286
    }
 
287
 
 
288
    /* Cleanup from the module strdup'ing these below */
 
289
    free(username);
 
290
    free(password);
 
291
    free(db);
303
292
  }
304
293
 
305
294
  virtual bool getFileDescriptors(std::vector<int> &fds)
307
296
    if (debug_enabled)
308
297
      enabled= true;
309
298
 
310
 
    if (not enabled)
 
299
    if (enabled == false)
311
300
      return false;
312
301
 
313
302
    if (pipe(pipe_fds) == -1)
325
314
  {
326
315
    char buffer[1];
327
316
    assert(read(fd, buffer, 1) == 1);
328
 
    return new ClientConsole(username, password, db);
 
317
    return new ClientConsole;
329
318
  }
330
319
};
331
320
 
332
321
static int init(drizzled::module::Context &context)
333
322
{
334
323
  const module::option_map &vm= context.getOptions();
335
 
  const string username(vm.count("username") ? vm["username"].as<string>() : "");
336
 
  const string password(vm.count("password") ? vm["password"].as<string>() : "");
337
 
  const string db(vm.count("db") ? vm["db"].as<string>() : "");
338
 
  context.registerVariable(new sys_var_bool_ptr("enable", &enabled));
339
 
  context.registerVariable(new sys_var_bool_ptr("debug_enable", &debug_enabled));
340
 
  context.registerVariable(new sys_var_const_string_val("username", username));
341
 
  context.registerVariable(new sys_var_const_string_val("password", password));
342
 
  context.registerVariable(new sys_var_const_string_val("db", db));
343
 
  context.add(new ListenConsole("console", username, password, db));
 
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"));
344
342
  return 0;
345
343
}
346
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
 
347
357
static void init_options(drizzled::module::option_context &context)
348
358
{
349
359
  context("enable",
363
373
          N_("Default database to use."));
364
374
}
365
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
 
366
385
DRIZZLE_DECLARE_PLUGIN
367
386
{
368
387
  DRIZZLE_VERSION_ID,
372
391
  "Console Client",
373
392
  PLUGIN_LICENSE_BSD,
374
393
  init,   /* Plugin Init */
375
 
  NULL,   /* system variables */
 
394
  vars,   /* system variables */
376
395
  init_options    /* config options */
377
396
}
378
397
DRIZZLE_DECLARE_PLUGIN_END;