-
Notifications
You must be signed in to change notification settings - Fork 4k
/
Copy pathversion_token.cc
1070 lines (857 loc) · 29.1 KB
/
version_token.cc
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
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright (c) 2015, 2016, 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 */
#define MYSQL_SERVER
#include <current_thd.h>
#include <derror.h>
#include <errmsg.h>
#include <hash.h>
#include <locking_service.h>
#include <m_string.h>
#include <my_global.h>
#include <mysql/plugin_audit.h>
#include <mysql/psi/mysql_memory.h>
#include <mysql/psi/mysql_rwlock.h>
#include <mysql/service_locking.h>
#include <sql_class.h>
#include <sstream>
#include "my_psi_config.h"
#ifdef WIN32
#define PLUGIN_EXPORT extern "C" __declspec(dllexport)
#else
#define PLUGIN_EXPORT extern "C"
#endif
// This global value is initiated with 1 and the corresponding session
// value with 0. Hence Every session must compare its tokens with the
// global values when it runs its very first query.
static volatile int64 session_number= 1;
static size_t vtoken_string_length;
// This flag is for memory management when the variable
// is updated for the first time.
struct version_token_st {
LEX_STRING token_name;
LEX_STRING token_val;
};
#define VTOKEN_LOCKS_NAMESPACE "version_token_locks"
#define LONG_TIMEOUT ((ulong) 3600L*24L*365L)
static HASH version_tokens_hash;
static MYSQL_THDVAR_ULONG(session_number,
PLUGIN_VAR_RQCMDARG | PLUGIN_VAR_READONLY,
"Version number to assist with session tokens check",
NULL, NULL, 0L, 0, ((ulong) -1), 0);
static void update_session_version_tokens(MYSQL_THD thd,
struct st_mysql_sys_var *var,
void *var_ptr, const void *save)
{
THDVAR(thd, session_number)= 0;
*(char **) var_ptr= *(char **) save;
}
static MYSQL_THDVAR_STR(session,
PLUGIN_VAR_RQCMDARG | PLUGIN_VAR_MEMALLOC,
"Holds the session value for version tokens",
NULL, update_session_version_tokens, NULL);
// Lock to be used for global variable hash.
mysql_rwlock_t LOCK_vtoken_hash;
PSI_memory_key key_memory_vtoken;
#ifdef HAVE_PSI_INTERFACE
PSI_rwlock_key key_LOCK_vtoken_hash;
static PSI_rwlock_info all_vtoken_rwlocks[]=
{
{&key_LOCK_vtoken_hash, "LOCK_vtoken_hash", 0},
};
static PSI_memory_info all_vtoken_memory[]=
{
{&key_memory_vtoken, "vtoken", 0}
};
// Function to register the lock
static void vtoken_init_psi_keys(void)
{
const char* category= "vtoken";
int count;
count= static_cast<int>(array_elements(all_vtoken_rwlocks));
PSI_RWLOCK_CALL(register_rwlock)(category, all_vtoken_rwlocks, count);
count= static_cast<int>(array_elements(all_vtoken_memory));
PSI_MEMORY_CALL(register_memory)(category, all_vtoken_memory, count);
}
#endif /* HAVE_PSI_INTERFACE */
static bool is_blank_string(char *input)
{
LEX_STRING input_lex;
input_lex.str= input;
input_lex.length= strlen(input);
trim_whitespace(&my_charset_bin, &input_lex);
if (input_lex.length == 0)
return true;
else
return false;
}
static const uchar *
version_token_get_key(const uchar *entry, size_t *length);
static void set_vtoken_string_length()
{
version_token_st *token_obj;
int i= 0;
size_t str_size= 0;
while ((token_obj= (version_token_st *) my_hash_element(&version_tokens_hash, i)))
{
if ((token_obj->token_name).str)
str_size= str_size+ (token_obj->token_name).length;
if ((token_obj->token_val).str)
str_size= str_size+ (token_obj->token_val).length;
str_size+= 2;
i++;
}
vtoken_string_length= str_size;
}
// UDF
PLUGIN_EXPORT my_bool version_tokens_set_init(UDF_INIT *initid, UDF_ARGS *args,
char *message);
PLUGIN_EXPORT char *version_tokens_set(UDF_INIT *initid, UDF_ARGS *args,
char *result, unsigned long *length,
char *null_value, char *error);
PLUGIN_EXPORT my_bool version_tokens_show_init(UDF_INIT *initid, UDF_ARGS *args,
char *message);
PLUGIN_EXPORT void version_tokens_show_deinit(UDF_INIT *initid);
PLUGIN_EXPORT char *version_tokens_show(UDF_INIT *initid, UDF_ARGS *args,
char *result, unsigned long *length,
char *null_value, char *error);
PLUGIN_EXPORT my_bool version_tokens_edit_init(UDF_INIT *initid, UDF_ARGS *args,
char *message);
PLUGIN_EXPORT char *version_tokens_edit(UDF_INIT *initid, UDF_ARGS *args,
char *result, unsigned long *length,
char *null_value, char *error);
PLUGIN_EXPORT my_bool version_tokens_delete_init(UDF_INIT *initid,
UDF_ARGS *args, char *message);
PLUGIN_EXPORT char *version_tokens_delete(UDF_INIT *initid, UDF_ARGS *args,
char *result, unsigned long *length,
char *null_value, char *error);
PLUGIN_EXPORT my_bool version_tokens_lock_shared_init(
UDF_INIT *initid, UDF_ARGS *args, char *message);
PLUGIN_EXPORT long long version_tokens_lock_shared(
UDF_INIT *initid, UDF_ARGS *args, char *is_null, char *error);
PLUGIN_EXPORT my_bool version_tokens_lock_exclusive_init(
UDF_INIT *initid, UDF_ARGS *args, char *message);
PLUGIN_EXPORT long long version_tokens_lock_exclusive(
UDF_INIT *initid, UDF_ARGS *args, char *is_null, char *error);
PLUGIN_EXPORT my_bool version_tokens_unlock_init(
UDF_INIT *initid, UDF_ARGS *args, char *message);
PLUGIN_EXPORT long long version_tokens_unlock(
UDF_INIT *initid, UDF_ARGS *args, char *is_null, char *error);
enum command {
SET_VTOKEN= 0,
EDIT_VTOKEN,
CHECK_VTOKEN
};
/**
@brief Parses the list of version tokens and either updates the global
list with the input or checks the input against the global according
to which function the caller is.
@param [in] input List of semicolon separated token name/value pairs
@param [in] type Helps determining the caller function.
@return (error) -1 in case of error.
@return (success) Number of tokens updated/set on EDIT_VTOKEN and SET_VTOKEN.
0 in case of CHECK_VTOKEN.
TODO: Add calls to get_lock services in CHECK_VTOKEN.
*/
static int parse_vtokens(char *input, enum command type)
{
char *token, *lasts_token= NULL;
const char *separator= ";";
int result= 0;
THD *thd= current_thd;
ulonglong thd_session_number= THDVAR(thd, session_number);
ulonglong tmp_token_number= (ulonglong)
my_atomic_load64((volatile int64 *) &session_number);
bool vtokens_unchanged= (thd_session_number == tmp_token_number);
token= my_strtok_r(input, separator, &lasts_token);
while (token)
{
const char *equal= "=";
char *lasts_val= NULL;
LEX_STRING token_name, token_val;
if (is_blank_string(token))
{
token= my_strtok_r(NULL, separator, &lasts_token);
continue;
}
token_name.str= my_strtok_r(token, equal, &lasts_val);
token_val.str= lasts_val;
token_name.length= token_name.str ? strlen(token_name.str) : 0;
token_val.length= lasts_val ? strlen(lasts_val):0;
trim_whitespace(&my_charset_bin, &token_name);
trim_whitespace(&my_charset_bin, &token_val);
if ((token_name.length == 0) || (token_val.length == 0))
{
switch (type)
{
case CHECK_VTOKEN:
{
if (!thd->get_stmt_da()->is_set())
thd->get_stmt_da()->set_error_status(ER_ACCESS_DENIED_ERROR,
"Empty version token "
"name/value encountered",
"42000");
return -1;
}
case SET_VTOKEN:
case EDIT_VTOKEN:
{
push_warning(thd, Sql_condition::SL_WARNING, 42000,
"Invalid version token pair encountered. The list "
"provided is only partially updated.");
}
}
return result;
}
if (token_name.length > 64)
{
switch (type)
{
case CHECK_VTOKEN:
{
if (!thd->get_stmt_da()->is_set())
thd->get_stmt_da()->set_error_status(ER_ACCESS_DENIED_ERROR,
"Lengthy version token name "
"encountered. Maximum length "
"allowed for a token name is "
"64 characters.",
"42000");
return -1;
}
case SET_VTOKEN:
case EDIT_VTOKEN:
{
push_warning(thd, Sql_condition::SL_WARNING, 42000,
"Lengthy version token name encountered. Maximum length "
"allowed for a token name is 64 characters. The list "
"provided is only partially updated.");
}
}
return result;
}
switch (type) {
case SET_VTOKEN:
case EDIT_VTOKEN:
{
char *name= NULL, *val= NULL;
size_t name_len, val_len;
name_len= token_name.length;
val_len= token_val.length;
version_token_st *v_token= NULL;
if (!my_multi_malloc(key_memory_vtoken, MYF(0),
&v_token, sizeof(version_token_st), &name, name_len,
&val, val_len, NULL))
{
push_warning(thd, Sql_condition::SL_WARNING, CR_OUT_OF_MEMORY,
"Not enough memory available");
return result;
}
memcpy(name, token_name.str, name_len);
memcpy(val, token_val.str, val_len);
v_token->token_name.str= name;
v_token->token_val.str= val;
v_token->token_name.length= name_len;
v_token->token_val.length= val_len;
if (my_hash_insert(&version_tokens_hash, (uchar *) v_token))
{
version_token_st *tmp= (version_token_st *)
my_hash_search(&version_tokens_hash,
(uchar *) name, name_len);
if (tmp)
{
my_hash_delete(&version_tokens_hash, (uchar *) tmp);
}
my_hash_insert(&version_tokens_hash, (uchar *) v_token);
}
result++;
}
break;
case CHECK_VTOKEN:
{
version_token_st *token_obj;
char error_str[MYSQL_ERRMSG_SIZE];
if (!mysql_acquire_locking_service_locks(thd, VTOKEN_LOCKS_NAMESPACE,
(const char **) &(token_name.str), 1,
LOCKING_SERVICE_READ, LONG_TIMEOUT) &&
!vtokens_unchanged)
{
if ((token_obj= (version_token_st *)my_hash_search(&version_tokens_hash,
(const uchar*) token_name.str,
token_name.length)))
{
if ((token_obj->token_val.length != token_val.length) ||
(memcmp(token_obj->token_val.str, token_val.str, token_val.length) != 0))
{
if (!thd->get_stmt_da()->is_set())
{
my_snprintf(error_str, sizeof(error_str),
ER_THD(thd, ER_VTOKEN_PLUGIN_TOKEN_MISMATCH),
(int) token_name.length, token_name.str,
(int) token_obj->token_val.length,
token_obj->token_val.str);
thd->get_stmt_da()->set_error_status(ER_VTOKEN_PLUGIN_TOKEN_MISMATCH,
(const char *) error_str,
"42000");
}
return -1;
}
}
else
{
if (!thd->get_stmt_da()->is_set())
{
my_snprintf(error_str, sizeof(error_str),
ER_THD(thd, ER_VTOKEN_PLUGIN_TOKEN_NOT_FOUND),
(int) token_name.length, token_name.str);
thd->get_stmt_da()->set_error_status(ER_VTOKEN_PLUGIN_TOKEN_NOT_FOUND,
(const char *) error_str,
"42000");
}
return -1;
}
}
}
}
token= my_strtok_r(NULL, separator, &lasts_token);
}
if (type == CHECK_VTOKEN)
{
THDVAR(thd, session_number)= (long) tmp_token_number;
}
return result;
}
/**
Audit API entry point for the version token pluign
Plugin audit function to compare session version tokens with
the global ones.
At the start of each query (MYSQL_AUDIT_GENERAL_LOG
currently) if there's a session version token vector it will
acquire the GET_LOCK shared locks for the session version tokens
and then will try to find them in the global version lock and
compare their values with the ones found. Throws errors if not
found or the version values do not match. See parse_vtokens().
At query end (MYSQL_AUDIT_GENERAL_STATUS currently) it releases
the GET_LOCK shared locks it has aquired.
@param thd The current thread
@param event_class audit API event class
@param event pointer to the audit API event data
*/
static int version_token_check(MYSQL_THD thd,
mysql_event_class_t event_class,
const void *event)
{
char *sess_var;
const struct mysql_event_general *event_general=
(const struct mysql_event_general *) event;
const uchar *command= (const uchar *) event_general->general_command.str;
unsigned int length= event_general->general_command.length;
DBUG_ASSERT(event_class == MYSQL_AUDIT_GENERAL_CLASS);
switch (event_general->event_subclass)
{
case MYSQL_AUDIT_GENERAL_LOG:
{
/* Ignore all commands but COM_QUERY and COM_STMT_PREPARE */
if (0 != my_charset_latin1.coll->strnncoll(&my_charset_latin1,
command, length,
(const uchar *) STRING_WITH_LEN("Query"),
0) &&
0 != my_charset_latin1.coll->strnncoll(&my_charset_latin1,
command, length,
(const uchar *) STRING_WITH_LEN("Prepare"),
0))
return 0;
if (THDVAR(thd, session))
sess_var= my_strndup(key_memory_vtoken,
THDVAR(thd, session),
strlen(THDVAR(thd, session)),
MYF(MY_FAE));
else
return 0;
// Lock the hash before checking for values.
mysql_rwlock_rdlock(&LOCK_vtoken_hash);
parse_vtokens(sess_var, CHECK_VTOKEN);
// Unlock hash
mysql_rwlock_unlock(&LOCK_vtoken_hash);
my_free(sess_var);
break;
}
case MYSQL_AUDIT_GENERAL_STATUS:
{
/*
Release locks only if the session variable is set.
This relies on the fact that MYSQL_AUDIT_GENERAL_STATUS
is always generated at the end of query execution.
*/
if (THDVAR(thd, session))
mysql_release_locking_service_locks(NULL, VTOKEN_LOCKS_NAMESPACE);
break;
}
default:
break;
}
return 0;
}
static struct st_mysql_audit version_token_descriptor=
{
MYSQL_AUDIT_INTERFACE_VERSION, /* interface version */
NULL, /* release_thd() */
version_token_check, /* event_notify() */
{ (unsigned long) MYSQL_AUDIT_GENERAL_ALL, } /* class mask */
};
/** Plugin init. */
static int version_tokens_init(void *arg MY_ATTRIBUTE((unused)))
{
#ifdef HAVE_PSI_INTERFACE
// Initialize psi keys.
vtoken_init_psi_keys();
#endif /* HAVE_PSI_INTERFACE */
// Initialize hash.
my_hash_init(&version_tokens_hash,
&my_charset_bin,
4, 0, version_token_get_key,
my_free, HASH_UNIQUE,
key_memory_vtoken);
// Lock for hash.
mysql_rwlock_init(key_LOCK_vtoken_hash, &LOCK_vtoken_hash);
// Lock for version number.
return 0;
}
/** Plugin deinit. */
static int version_tokens_deinit(void *arg MY_ATTRIBUTE((unused)))
{
mysql_rwlock_wrlock(&LOCK_vtoken_hash);
if (version_tokens_hash.records)
my_hash_reset(&version_tokens_hash);
my_hash_free(&version_tokens_hash);
mysql_rwlock_unlock(&LOCK_vtoken_hash);
return 0;
}
static struct st_mysql_sys_var* system_variables[]={
MYSQL_SYSVAR(session_number),
MYSQL_SYSVAR(session),
NULL
};
// Declare plugin
mysql_declare_plugin(version_tokens)
{
MYSQL_AUDIT_PLUGIN, /* type */
&version_token_descriptor, /* descriptor */
"version_tokens", /* name */
"Oracle Corp", /* author */
"version token check", /* description */
PLUGIN_LICENSE_GPL,
version_tokens_init, /* init function (when loaded) */
version_tokens_deinit, /* deinit function (when unloaded) */
0x0101, /* version */
NULL, /* status variables */
system_variables, /* system variables */
NULL,
0
}
mysql_declare_plugin_end;
/*
Below is the UDF for setting global list of version tokens.
Input must be provided as semicolon separated tokens.
Function signature:
VERSION_TOKENS_SET(tokens_list varchar)
*/
PLUGIN_EXPORT my_bool version_tokens_set_init(UDF_INIT* initid, UDF_ARGS* args,
char* message)
{
THD *thd= current_thd;
if (!(thd->security_context()->check_access(SUPER_ACL)))
{
my_stpcpy(message, "The user is not privileged to use this function.");
return true;
}
if (!(my_hash_inited(&version_tokens_hash)))
{
my_stpcpy(message, "version_token plugin is not installed.");
return true;
}
if (args->arg_count != 1 || args->arg_type[0] != STRING_RESULT)
{
my_stpcpy(message, "Wrong arguments provided for the function.");
return true;
}
return false;
}
PLUGIN_EXPORT char *version_tokens_set(UDF_INIT *initid, UDF_ARGS *args,
char *result, unsigned long *length,
char *null_value, char *error)
{
char *hash_str;
int len= args->lengths[0];
int vtokens_count= 0;
std::stringstream ss;
mysql_rwlock_wrlock(&LOCK_vtoken_hash);
if (len > 0)
{
// Separate copy for values to be inserted in hash.
hash_str= (char *)my_malloc(key_memory_vtoken,
len+1, MYF(MY_WME));
if (!hash_str)
{
*error= 1;
return NULL;
}
memcpy(hash_str, args->args[0], len);
hash_str[len]= 0;
// Hash built with its own copy of string.
if (version_tokens_hash.records)
my_hash_reset(&version_tokens_hash);
vtokens_count= parse_vtokens(hash_str, SET_VTOKEN);
ss << vtokens_count << " version tokens set.";
my_free(hash_str);
}
else
{
if (version_tokens_hash.records)
my_hash_reset(&version_tokens_hash);
ss << "Version tokens list cleared.";
}
set_vtoken_string_length();
my_atomic_add64((volatile int64 *) &session_number, 1);
mysql_rwlock_unlock(&LOCK_vtoken_hash);
ss.getline(result, MAX_FIELD_WIDTH, '\0');
*length= (unsigned long) ss.gcount();
return result;
}
/*
Below is the UDF for updating global version tokens.
Tokens to be updated must be provided as semicolon
separated tokens.
Function signature:
VERSION_TOKENS_EDIT(tokens_list varchar)
*/
PLUGIN_EXPORT my_bool version_tokens_edit_init(UDF_INIT *initid, UDF_ARGS *args,
char *message)
{
THD *thd= current_thd;
if (!(my_hash_inited(&version_tokens_hash)))
{
my_stpcpy(message, "version_token plugin is not installed.");
return true;
}
if (!(thd->security_context()->check_access(SUPER_ACL)))
{
my_stpcpy(message, "The user is not privileged to use this function.");
return true;
}
if (args->arg_count != 1 || args->arg_type[0] != STRING_RESULT)
{
my_stpcpy(message, "Wrong arguments provided for the function.");
return true;
}
return false;
}
PLUGIN_EXPORT char *version_tokens_edit(UDF_INIT *initid, UDF_ARGS *args,
char *result, unsigned long *length,
char *null_value, char *error)
{
char *hash_str;
int len= args->lengths[0];
std::stringstream ss;
int vtokens_count= 0;
if (len > 0)
{
// Separate copy for values to be inserted in hash.
hash_str= (char *)my_malloc(key_memory_vtoken,
len+1, MYF(MY_WME));
if (!hash_str)
{
*error= 1;
return NULL;
}
memcpy(hash_str, args->args[0], len);
hash_str[len]= 0;
// Hash built with its own copy of string.
mysql_rwlock_wrlock(&LOCK_vtoken_hash);
vtokens_count= parse_vtokens(hash_str, EDIT_VTOKEN);
set_vtoken_string_length();
if (vtokens_count)
my_atomic_add64((volatile int64 *) &session_number, 1);
mysql_rwlock_unlock(&LOCK_vtoken_hash);
my_free(hash_str);
}
ss << vtokens_count << " version tokens updated.";
ss.getline(result, MAX_FIELD_WIDTH, '\0');
*length= (unsigned long) ss.gcount();
return result;
}
/*
Below is the UDF for deleting selected global version tokens.
Names of tokens to be deleted must be provided in a semicolon separated list.
Function signature:
VERSION_TOKENS_DELETE(tokens_list varchar)
*/
PLUGIN_EXPORT my_bool version_tokens_delete_init(UDF_INIT *initid,
UDF_ARGS *args, char *message)
{
THD *thd= current_thd;
if (!(my_hash_inited(&version_tokens_hash)))
{
my_stpcpy(message, "version_token plugin is not installed.");
return true;
}
if (!(thd->security_context()->check_access(SUPER_ACL)))
{
my_stpcpy(message, "The user is not privileged to use this function.");
return true;
}
if (args->arg_count != 1 || args->arg_type[0] != STRING_RESULT)
{
my_stpcpy(message, "Wrong arguments provided for the function.");
return true;
}
return false;
}
PLUGIN_EXPORT char *version_tokens_delete(UDF_INIT *initid, UDF_ARGS *args,
char *result, unsigned long *length,
char *null_value, char *error)
{
const char *arg= args->args[0];
std::stringstream ss;
int vtokens_count= 0;
if (args->lengths[0] > 0)
{
char *input;
const char *separator= ";";
char *token, *lasts_token= NULL;
if (NULL == (input= my_strdup(key_memory_vtoken, arg, MYF(MY_WME))))
{
*error= 1;
return NULL;
}
mysql_rwlock_wrlock(&LOCK_vtoken_hash);
token= my_strtok_r(input, separator, &lasts_token);
while (token)
{
version_token_st *tmp;
LEX_STRING st={ token, strlen(token) };
trim_whitespace(&my_charset_bin, &st);
if (st.length)
{
tmp= (version_token_st *) my_hash_search(&version_tokens_hash,
(uchar *) st.str,
st.length);
if (tmp)
{
my_hash_delete(&version_tokens_hash, (uchar *) tmp);
vtokens_count++;
}
}
token= my_strtok_r(NULL, separator, &lasts_token);
}
set_vtoken_string_length();
if (vtokens_count)
{
my_atomic_add64((volatile int64 *) &session_number, 1);
}
mysql_rwlock_unlock(&LOCK_vtoken_hash);
my_free(input);
}
ss << vtokens_count << " version tokens deleted.";
ss.getline(result, MAX_FIELD_WIDTH, '\0');
*length= (unsigned long) ss.gcount();
return result;
}
/*
Below is the UDF for showing the existing list of global version tokens.
Names of tokens will be returned in a semicolon separated list.
Function signature:
VERSION_TOKENS_SHOW()
*/
PLUGIN_EXPORT my_bool version_tokens_show_init(UDF_INIT *initid, UDF_ARGS *args,
char *message)
{
int i= 0;
size_t str_size= 0;
char *result_str;
version_token_st *token_obj;
THD *thd= current_thd;
if (!(my_hash_inited(&version_tokens_hash)))
{
my_stpcpy(message, "version_token plugin is not installed.");
return true;
}
if (!(thd->security_context()->check_access(SUPER_ACL)))
{
my_stpcpy(message, "The user is not privileged to use this function.");
return true;
}
if (args->arg_count != 0)
{
my_stpcpy(message, "This function does not take any arguments.");
return true;
}
mysql_rwlock_rdlock(&LOCK_vtoken_hash);
str_size= vtoken_string_length;
if (str_size)
{
str_size++;
initid->ptr= (char *)my_malloc(key_memory_vtoken,
str_size, MYF(MY_WME));
if (initid->ptr == NULL)
{
my_stpcpy(message, "Not enough memory available.");
return true;
}
result_str= initid->ptr;
i= 0;
while ((token_obj= (version_token_st *) my_hash_element(&version_tokens_hash, i)))
{
memcpy(result_str, (token_obj->token_name).str, (token_obj->token_name).length);
result_str+= (token_obj->token_name).length;
memcpy(result_str, "=", 1);
result_str++;
memcpy(result_str, (token_obj->token_val).str, (token_obj->token_val).length);
result_str+= (token_obj->token_val).length;
memcpy(result_str, ";", 1);
result_str++;
i++;
}
initid->ptr[str_size-1]= '\0';
}
else
initid->ptr= NULL;
mysql_rwlock_unlock(&LOCK_vtoken_hash);
return false;
}
PLUGIN_EXPORT void version_tokens_show_deinit(UDF_INIT *initid)
{
if (initid->ptr)
my_free(initid->ptr);
}
PLUGIN_EXPORT char *version_tokens_show(UDF_INIT *initid, UDF_ARGS *args,
char *result, unsigned long *length,
char *null_value, char *error)
{
char *result_str= initid->ptr;
*length= 0;
if (!result_str)
return NULL;
*length= (unsigned long) strlen(result_str);
return result_str;
}
static inline my_bool init_acquire(UDF_INIT *initid, UDF_ARGS *args, char *message)
{
initid->maybe_null= FALSE;
initid->decimals= 0;
initid->max_length= 1;
initid->ptr= NULL;
initid->const_item= 0;
initid->extension= NULL;
THD *thd= current_thd;
if (!(thd->security_context()->check_access(SUPER_ACL)))
{
my_stpcpy(message, "The user is not privileged to use this function.");
return true;
}
// At least two arguments - lock, timeout
if (args->arg_count < 2)
{
strcpy(message,
"Requires at least two arguments: (lock(...),timeout).");
return TRUE;
}
// Timeout is the last argument, should be INT
if (args->arg_type[args->arg_count - 1] != INT_RESULT)
{
strcpy(message, "Wrong argument type - expected integer.");
return TRUE;
}
// All other arguments should be strings
for (size_t i= 0; i < (args->arg_count - 1); i++)
{
if (args->arg_type[i] != STRING_RESULT)
{
strcpy(message, "Wrong argument type - expected string.");
return TRUE;
}
}
return FALSE;
}
PLUGIN_EXPORT my_bool version_tokens_lock_shared_init(
UDF_INIT *initid, UDF_ARGS *args, char *message)
{
return init_acquire(initid, args, message);
}
PLUGIN_EXPORT long long version_tokens_lock_shared(
UDF_INIT *initid, UDF_ARGS *args, char *is_null, char *error)
{
long long timeout=
args->args[args->arg_count - 1] ? // Null ?
*((long long *) args->args[args->arg_count - 1]) : -1;
if (timeout < 0)
{
my_error(ER_DATA_OUT_OF_RANGE, MYF(0), "timeout",
"version_tokens_lock_shared");
*error= 1;
}
// For the UDF 1 == success, 0 == failure.
return !acquire_locking_service_locks(NULL, VTOKEN_LOCKS_NAMESPACE,
const_cast<const char**>(&args->args[0]),