summaryrefslogtreecommitdiff
path: root/mysql/sql-common/client_plugin.c
blob: c7dfda96d76011fd2427bc3464d1cb376104151e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
/* Copyright (c) 2010, 2015, Oracle and/or its affiliates. All rights reserved.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; version 2 of the License.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA */

/**
  @file
  
  Support code for the client side (libmysql) plugins

  Client plugins are somewhat different from server plugins, they are simpler.

  They do not need to be installed or in any way explicitly loaded on the
  client, they are loaded automatically on demand.
  One client plugin per shared object, soname *must* match the plugin name.

  There is no reference counting and no unloading either.
*/

#include <my_global.h>
#include "mysql.h"
#include <my_sys.h>
#include <m_string.h>
#include <my_thread.h>

#include <sql_common.h>
#include "errmsg.h"
#include <mysql/client_plugin.h>

#ifdef HAVE_DLFCN_H
#include <dlfcn.h>
#endif

#if defined(CLIENT_PROTOCOL_TRACING)
#include <mysql/plugin_trace.h>
#endif

PSI_memory_key key_memory_root;
PSI_memory_key key_memory_load_env_plugins;

#ifdef HAVE_PSI_INTERFACE
PSI_mutex_key key_mutex_LOCK_load_client_plugin;

static PSI_mutex_info all_client_plugin_mutexes[]=
{
  {&key_mutex_LOCK_load_client_plugin, "LOCK_load_client_plugin", PSI_FLAG_GLOBAL}
};

static PSI_memory_info all_client_plugin_memory[]=
{
  {&key_memory_root, "root", PSI_FLAG_GLOBAL},
  {&key_memory_load_env_plugins, "load_env_plugins", PSI_FLAG_GLOBAL}
};

static void init_client_plugin_psi_keys()
{
  const char* category= "sql";
  int count;

  count= array_elements(all_client_plugin_mutexes);
  mysql_mutex_register(category, all_client_plugin_mutexes, count);

  count= array_elements(all_client_plugin_memory);
  mysql_memory_register(category, all_client_plugin_memory, count);
}
#endif /* HAVE_PSI_INTERFACE */

struct st_client_plugin_int {
  struct st_client_plugin_int *next;
  void   *dlhandle;
  struct st_mysql_client_plugin *plugin;
};

static my_bool initialized= 0;
static MEM_ROOT mem_root;

static const char *plugin_declarations_sym= "_mysql_client_plugin_declaration_";
static uint plugin_version[MYSQL_CLIENT_MAX_PLUGINS]=
{
  0, /* these two are taken by Connector/C */
  0, /* these two are taken by Connector/C */
  MYSQL_CLIENT_AUTHENTICATION_PLUGIN_INTERFACE_VERSION,
  MYSQL_CLIENT_TRACE_PLUGIN_INTERFACE_VERSION,
};

/*
  Loaded plugins are stored in a linked list.
  The list is append-only, the elements are added to the head (like in a stack).
  The elements are added under a mutex, but the list can be read and traversed
  without any mutex because once an element is added to the list, it stays
  there. The main purpose of a mutex is to prevent two threads from
  loading the same plugin twice in parallel.
*/
struct st_client_plugin_int *plugin_list[MYSQL_CLIENT_MAX_PLUGINS];
static mysql_mutex_t LOCK_load_client_plugin;

static int is_not_initialized(MYSQL *mysql, const char *name)
{
  if (initialized)
    return 0;

  set_mysql_extended_error(mysql, CR_AUTH_PLUGIN_CANNOT_LOAD,
                           unknown_sqlstate, ER(CR_AUTH_PLUGIN_CANNOT_LOAD),
                           name, "not initialized");
  return 1;
}

/**
  finds a plugin in the list

  @param name   plugin name to search for
  @param type   plugin type

  @note this does NOT necessarily need a mutex, take care!
  
  @retval a pointer to a found plugin or 0
*/
static struct st_mysql_client_plugin *
find_plugin(const char *name, int type)
{
  struct st_client_plugin_int *p;

  DBUG_ASSERT(initialized);
  DBUG_ASSERT(type >= 0 && type < MYSQL_CLIENT_MAX_PLUGINS);
  if (type < 0 || type >= MYSQL_CLIENT_MAX_PLUGINS)
    return 0;

  for (p= plugin_list[type]; p; p= p->next)
  {
    if (strcmp(p->plugin->name, name) == 0)
      return p->plugin;
  }
  return NULL;
}

/**
  verifies the plugin and adds it to the list

  @param mysql          MYSQL structure (for error reporting)
  @param plugin         plugin to install
  @param dlhandle       a handle to the shared object (returned by dlopen)
                        or 0 if the plugin was not dynamically loaded
  @param argc           number of arguments in the 'va_list args'
  @param args           arguments passed to the plugin initialization function

  @retval a pointer to an installed plugin or 0
*/
static struct st_mysql_client_plugin *
do_add_plugin(MYSQL *mysql, struct st_mysql_client_plugin *plugin,
              void *dlhandle,
              int argc, va_list args)
{
  const char *errmsg;
  struct st_client_plugin_int plugin_int, *p;
  char errbuf[1024];

  DBUG_ASSERT(initialized);

  plugin_int.plugin= plugin;
  plugin_int.dlhandle= dlhandle;

  if (plugin->type >= MYSQL_CLIENT_MAX_PLUGINS)
  {
    errmsg= "Unknown client plugin type";
    goto err1;
  }

  if (plugin->interface_version < plugin_version[plugin->type] ||
      (plugin->interface_version >> 8) >
       (plugin_version[plugin->type] >> 8))
  {
    errmsg= "Incompatible client plugin interface";
    goto err1;
  }

#if defined(CLIENT_PROTOCOL_TRACING) && !defined(MYSQL_SERVER)
  /*
    If we try to load a protocol trace plugin but one is already
    loaded (global trace_plugin pointer is not NULL) then we ignore
    the new trace plugin and give error. This is done before the
    new plugin gets initialized.
  */
  if (plugin->type == MYSQL_CLIENT_TRACE_PLUGIN && NULL != trace_plugin)
  {
    errmsg= "Can not load another trace plugin while one is already loaded";
    goto err1;
  }
#endif

  /* Call the plugin initialization function, if any */
  if (plugin->init && plugin->init(errbuf, sizeof(errbuf), argc, args))
  {
    errmsg= errbuf;
    goto err1;
  }

  p= (struct st_client_plugin_int *)
    memdup_root(&mem_root, &plugin_int, sizeof(plugin_int));

  if (!p)
  {
    errmsg= "Out of memory";
    goto err2;
  }

  mysql_mutex_assert_owner(&LOCK_load_client_plugin);

  p->next= plugin_list[plugin->type];
  plugin_list[plugin->type]= p;
  net_clear_error(&mysql->net);

#if defined(CLIENT_PROTOCOL_TRACING) && !defined(MYSQL_SERVER)
  /*
    If loaded plugin is a protocol trace one, then set the global
    trace_plugin pointer to point at it. When trace_plugin is not NULL,
    each new connection will be traced using the plugin pointed by it
    (see MYSQL_TRACE_STAGE() macro in libmysql/mysql_trace.h).
  */
  if (plugin->type == MYSQL_CLIENT_TRACE_PLUGIN)
  {
    trace_plugin = (struct st_mysql_client_plugin_TRACE*)plugin;
  }
#endif

  return plugin;

err2:
  if (plugin->deinit)
    plugin->deinit();
err1:
  set_mysql_extended_error(mysql, CR_AUTH_PLUGIN_CANNOT_LOAD, unknown_sqlstate,
                           ER(CR_AUTH_PLUGIN_CANNOT_LOAD), plugin->name,
                           errmsg);
  if (dlhandle)
    dlclose(dlhandle);
  return NULL;
}


static struct st_mysql_client_plugin *
add_plugin_noargs(MYSQL *mysql, struct st_mysql_client_plugin *plugin,
                  void *dlhandle,
                  int argc, ...)
{
  struct st_mysql_client_plugin *retval= NULL;
  va_list ap;
  va_start(ap, argc);
  retval= do_add_plugin(mysql, plugin, dlhandle, argc, ap);
  va_end(ap);
  return retval;
}


static struct st_mysql_client_plugin *
add_plugin_withargs(MYSQL *mysql, struct st_mysql_client_plugin *plugin,
                    void *dlhandle,
                    int argc, va_list args)
{
  return do_add_plugin(mysql, plugin, dlhandle, argc, args);
}



/**
  Loads plugins which are specified in the environment variable
  LIBMYSQL_PLUGINS.
  
  Multiple plugins must be separated by semicolon. This function doesn't
  return or log an error.

  The function is be called by mysql_client_plugin_init

  @todo
  Support extended syntax, passing parameters to plugins, for example
  LIBMYSQL_PLUGINS="plugin1(param1,param2);plugin2;..."
  or
  LIBMYSQL_PLUGINS="plugin1=int:param1,str:param2;plugin2;..."
*/
static void load_env_plugins(MYSQL *mysql)
{
  char *plugs, *free_env, *s= getenv("LIBMYSQL_PLUGINS");
  char *enable_cleartext_plugin= getenv("LIBMYSQL_ENABLE_CLEARTEXT_PLUGIN");

  if (enable_cleartext_plugin && strchr("1Yy", enable_cleartext_plugin[0]))
    libmysql_cleartext_plugin_enabled= 1;

  /* no plugins to load */
  if(!s)
    return;

  free_env= plugs= my_strdup(key_memory_load_env_plugins,
                             s, MYF(MY_WME));

  do {
    if ((s= strchr(plugs, ';')))
      *s= '\0';
    mysql_load_plugin(mysql, plugs, -1, 0);
    plugs= s + 1;
  } while (s);

  my_free(free_env);

}


/********** extern functions to be used by libmysql *********************/

/**
  Initializes the client plugin layer.

  This function must be called before any other client plugin function.

  @retval 0    successful
  @retval != 0 error occured
*/
int mysql_client_plugin_init()
{
  MYSQL mysql;
  struct st_mysql_client_plugin **builtin;

  if (initialized)
    return 0;

#ifdef HAVE_PSI_INTERFACE
  init_client_plugin_psi_keys();
#endif /* HAVE_PSI_INTERFACE */

  memset(&mysql, 0, sizeof(mysql)); /* dummy mysql for set_mysql_extended_error */

  mysql_mutex_init(key_mutex_LOCK_load_client_plugin,
                   &LOCK_load_client_plugin, MY_MUTEX_INIT_SLOW);
  init_alloc_root(key_memory_root, &mem_root, 128, 128);

  memset(&plugin_list, 0, sizeof(plugin_list));

  initialized= 1;

  mysql_mutex_lock(&LOCK_load_client_plugin);

  for (builtin= mysql_client_builtins; *builtin; builtin++)
    add_plugin_noargs(&mysql, *builtin, 0, 0);

  mysql_mutex_unlock(&LOCK_load_client_plugin);

  load_env_plugins(&mysql);

  mysql_close_free(&mysql);

  return 0;
}

/**
  Deinitializes the client plugin layer.

  Unloades all client plugins and frees any associated resources.
*/
void mysql_client_plugin_deinit()
{
  int i;
  struct st_client_plugin_int *p;

  if (!initialized)
    return;

  for (i=0; i < MYSQL_CLIENT_MAX_PLUGINS; i++)
    for (p= plugin_list[i]; p; p= p->next)
    {
      if (p->plugin->deinit)
        p->plugin->deinit();
      if (p->dlhandle)
        dlclose(p->dlhandle);
    }

  memset(&plugin_list, 0, sizeof(plugin_list));
  initialized= 0;
  free_root(&mem_root, MYF(0));
  mysql_mutex_destroy(&LOCK_load_client_plugin);
}


/************* public facing functions, for client consumption *********/

/* see <mysql/client_plugin.h> for a full description */
struct st_mysql_client_plugin *
mysql_client_register_plugin(MYSQL *mysql,
                             struct st_mysql_client_plugin *plugin)
{
  if (is_not_initialized(mysql, plugin->name))
    return NULL;

  mysql_mutex_lock(&LOCK_load_client_plugin);

  /* make sure the plugin wasn't loaded meanwhile */
  if (find_plugin(plugin->name, plugin->type))
  {
    set_mysql_extended_error(mysql, CR_AUTH_PLUGIN_CANNOT_LOAD,
                             unknown_sqlstate, ER(CR_AUTH_PLUGIN_CANNOT_LOAD),
                             plugin->name, "it is already loaded");
    plugin= NULL;
  }
  else
    plugin= add_plugin_noargs(mysql, plugin, 0, 0);

  mysql_mutex_unlock(&LOCK_load_client_plugin);
  return plugin;
}

/* see <mysql/client_plugin.h> for a full description */
struct st_mysql_client_plugin *
mysql_load_plugin_v(MYSQL *mysql, const char *name, int type,
                    int argc, va_list args)
{
  const char *errmsg;
  char dlpath[FN_REFLEN+1];
  void *sym, *dlhandle;
  struct st_mysql_client_plugin *plugin;
  const char *plugindir;
#ifdef _WIN32
  char win_errormsg[2048];
#endif

  DBUG_ENTER ("mysql_load_plugin_v");
  DBUG_PRINT ("entry", ("name=%s type=%d int argc=%d", name, type, argc));
  if (is_not_initialized(mysql, name))
  {
    DBUG_PRINT ("leave", ("mysql not initialized"));
    DBUG_RETURN (NULL);
  }

  mysql_mutex_lock(&LOCK_load_client_plugin);

  /* make sure the plugin wasn't loaded meanwhile */
  if (type >= 0 && find_plugin(name, type))
  {
    errmsg= "it is already loaded";
    goto err;
  }

  if (mysql->options.extension && mysql->options.extension->plugin_dir)
  {
    plugindir= mysql->options.extension->plugin_dir;
  }
  else
  {
    plugindir= getenv("LIBMYSQL_PLUGIN_DIR");
    if (!plugindir)
    {
      plugindir= PLUGINDIR;
    }
  }

  /* Compile dll path */
  strxnmov(dlpath, sizeof(dlpath) - 1,
           plugindir, "/",
           name, SO_EXT, NullS);
   
  DBUG_PRINT ("info", ("dlopeninig %s", dlpath));
  /* Open new dll handle */
  if (!(dlhandle= dlopen(dlpath, RTLD_NOW)))
  {
#if defined(__APPLE__)
    /* Apple supports plugins with .so also, so try this as well */
    strxnmov(dlpath, sizeof(dlpath) - 1,
             plugindir, "/",
             name, ".so", NullS);
    if ((dlhandle= dlopen(dlpath, RTLD_NOW)))
      goto have_plugin;
#endif

#ifdef _WIN32
    /* There should be no win32 calls between failed dlopen() and GetLastError() */
    if(FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,
                  0, GetLastError(), 0, win_errormsg, 2048, NULL))
      errmsg= win_errormsg;
    else
      errmsg= "";
#else
    errmsg= dlerror();
#endif
    DBUG_PRINT ("info", ("failed to dlopen"));
    goto err;
  }

#if defined(__APPLE__)
have_plugin:  
#endif
  if (!(sym= dlsym(dlhandle, plugin_declarations_sym)))
  {
    errmsg= "not a plugin";
    dlclose(dlhandle);
    goto err;
  }

  plugin= (struct st_mysql_client_plugin*)sym;

  if (type >=0 && type != plugin->type)
  {
    errmsg= "type mismatch";
    goto err;
  }

  if (strcmp(name, plugin->name))
  {
    errmsg= "name mismatch";
    goto err;
  }

  if (type < 0 && find_plugin(name, plugin->type))
  {
    errmsg= "it is already loaded";
    goto err;
  }

  plugin= add_plugin_withargs(mysql, plugin, dlhandle, argc, args);

  mysql_mutex_unlock(&LOCK_load_client_plugin);

  DBUG_PRINT ("leave", ("plugin loaded ok"));
  DBUG_RETURN (plugin);

err:
  mysql_mutex_unlock(&LOCK_load_client_plugin);
  DBUG_PRINT ("leave", ("plugin load error : %s", errmsg));
  set_mysql_extended_error(mysql, CR_AUTH_PLUGIN_CANNOT_LOAD, unknown_sqlstate,
                           ER(CR_AUTH_PLUGIN_CANNOT_LOAD), name, errmsg);
  DBUG_RETURN (NULL);
}

/* see <mysql/client_plugin.h> for a full description */
struct st_mysql_client_plugin *
mysql_load_plugin(MYSQL *mysql, const char *name, int type, int argc, ...)
{
  struct st_mysql_client_plugin *p;
  va_list args;
  va_start(args, argc);
  p= mysql_load_plugin_v(mysql, name, type, argc, args);
  va_end(args);
  return p;
}

/* see <mysql/client_plugin.h> for a full description */
struct st_mysql_client_plugin *
mysql_client_find_plugin(MYSQL *mysql, const char *name, int type)
{
  struct st_mysql_client_plugin *p;

  DBUG_ENTER ("mysql_client_find_plugin");
  DBUG_PRINT ("entry", ("name=%s, type=%d", name, type));
  if (is_not_initialized(mysql, name))
    DBUG_RETURN (NULL);

  if (type < 0 || type >= MYSQL_CLIENT_MAX_PLUGINS)
  {
    set_mysql_extended_error(mysql, CR_AUTH_PLUGIN_CANNOT_LOAD, unknown_sqlstate,
                             ER(CR_AUTH_PLUGIN_CANNOT_LOAD), name,
                             "invalid type");
  }

  if ((p= find_plugin(name, type)))
  {
    DBUG_PRINT ("leave", ("found %p", p));
    DBUG_RETURN (p);
  }

  /* not found, load it */
  p= mysql_load_plugin(mysql, name, type, 0);
  DBUG_PRINT ("leave", ("loaded %p", p));
  DBUG_RETURN (p);
}


/* see <mysql/client_plugin.h> for a full description */
int mysql_plugin_options(struct st_mysql_client_plugin *plugin,
                                 const char *option,
                                 const void *value)
{
  DBUG_ENTER("mysql_plugin_options");
  /* does the plugin support options call? */
  if (!plugin || !plugin->options)
    DBUG_RETURN(1);
  DBUG_RETURN(plugin->options(option, value));
}