forked from trdtnguyen/mysql-plnvm
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsql_security_ctx.h
732 lines (514 loc) · 16.6 KB
/
sql_security_ctx.h
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
#ifndef SQL_SECURITY_CTX_INCLUDED
#define SQL_SECURITY_CTX_INCLUDED
/* Copyright (c) 2014, 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,
51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA */
#include "my_global.h"
#include "sql_string.h"
#include "mysql_com.h"
#include "sql_const.h"
#include "auth/auth_acls.h"
#include "mysqld.h"
#include <algorithm>
/**
@class Security_context
@brief A set of THD members describing the current authenticated user.
*/
class Security_context {
public:
Security_context() { init(); }
~Security_context() { destroy(); }
Security_context(const Security_context &src_sctx)
{ copy_security_ctx(src_sctx); }
Security_context& operator =(const Security_context &src_sctx)
{
DBUG_ENTER("Security_context::operator =");
if (this != &src_sctx)
{
destroy();
copy_security_ctx(src_sctx);
}
DBUG_RETURN(*this);
}
void skip_grants();
/**
Getter method for member m_user.
@retval LEX_CSTRING object having constant pointer to m_user.Ptr
and its length.
*/
LEX_CSTRING user() const
{
LEX_CSTRING user;
DBUG_ENTER("Security_context::user");
user.str= m_user.ptr();
user.length= m_user.length();
DBUG_RETURN(user);
}
inline void set_user_ptr(const char *user_arg, const int user_arg_length);
inline void assign_user(const char *user_arg, const int user_arg_length);
/**
Getter method for member m_host.
@retval LEX_CSTRING object having constant pointer to m_host.Ptr
and its length.
*/
LEX_CSTRING host() const
{
LEX_CSTRING host;
DBUG_ENTER("Security_context::host");
host.str= m_host.ptr();
host.length= m_host.length();
DBUG_RETURN(host);
}
inline void set_host_ptr(const char *host_arg, const int host_arg_length);
inline void assign_host(const char *host_arg, const int host_arg_length);
/**
Getter method for member m_ip.
@retval LEX_CSTRING object having constant pointer to m_ip.Ptr
and its length
*/
LEX_CSTRING ip() const
{
LEX_CSTRING ip;
DBUG_ENTER("Security_context::ip");
ip.str= m_ip.ptr();
ip.length= m_ip.length();
DBUG_RETURN(ip);
}
inline void set_ip_ptr(const char *ip_arg, const int ip_arg_length);
inline void assign_ip(const char *ip_arg, const int ip_arg_length);
/**
Getter method for member m_host_or_ip.
@retval LEX_CSTRING object having constant pointer to m_host_or_ip.Ptr
and its length
*/
LEX_CSTRING host_or_ip() const
{
LEX_CSTRING host_or_ip;
DBUG_ENTER("Security_context::host_or_ip");
host_or_ip.str= m_host_or_ip.ptr();
host_or_ip.length= m_host_or_ip.length();
DBUG_RETURN(host_or_ip);
}
/**
Setter method for member m_host_or_ip.
*/
void set_host_or_ip_ptr()
{
DBUG_ENTER("Security_context::set_host_or_ip_ptr");
/*
Set host_or_ip to either host or ip if they are available else set it to
empty string.
*/
const char *host_or_ip= m_host.length() ? m_host.ptr() :
(m_ip.length() ?
m_ip.ptr() : "");
m_host_or_ip.set(host_or_ip, strlen(host_or_ip), system_charset_info);
DBUG_VOID_RETURN;
}
/**
Setter method for member m_host_or_ip.
@param[in] host_or_ip_arg New user value for m_host_or_ip.
@param[in] host_or_ip_arg_length Length of "host_or_ip_arg" param.
*/
void set_host_or_ip_ptr(const char *host_or_ip_arg,
const int host_or_ip_arg_length)
{
DBUG_ENTER("Security_context::set_host_or_ip_ptr");
m_host_or_ip.set(host_or_ip_arg, host_or_ip_arg_length,
system_charset_info);
DBUG_VOID_RETURN;
}
/**
Getter method for member m_external_user.
@retval LEX_CSTRING object having constant pointer to m_external_host.Ptr
and its length
*/
LEX_CSTRING external_user() const
{
LEX_CSTRING ext_user;
DBUG_ENTER("Security_context::external_user");
ext_user.str= m_external_user.ptr();
ext_user.length= m_external_user.length();
DBUG_RETURN(ext_user);
}
inline void set_external_user_ptr(const char *ext_user_arg,
const int ext_user_arg_length);
inline void assign_external_user(const char *ext_user_arg,
const int ext_user_arg_length);
/**
Getter method for member m_priv_user.
@retval LEX_CSTRING object having constant pointer to m_priv_user.Ptr
and its length
*/
LEX_CSTRING priv_user() const
{
LEX_CSTRING priv_user;
DBUG_ENTER("Security_context::priv_user");
priv_user.str= m_priv_user;
priv_user.length= m_priv_user_length;
DBUG_RETURN(priv_user);
}
inline void assign_priv_user(const char *priv_user_arg,
const size_t priv_user_arg_length);
/**
Getter method for member m_proxy_user.
@retval LEX_CSTRING object having constant pointer to m_proxy_user.Ptr
and its length
*/
LEX_CSTRING proxy_user() const
{
LEX_CSTRING proxy_user;
DBUG_ENTER("Security_context::proxy_user");
proxy_user.str= m_proxy_user;
proxy_user.length= m_proxy_user_length;
DBUG_RETURN(proxy_user);
}
inline void assign_proxy_user(const char *proxy_user_arg,
const size_t proxy_user_arg_length);
/**
Getter method for member m_priv_host.
@retval LEX_CSTRING object having constant pointer to m_priv_host.Ptr
and its length
*/
LEX_CSTRING priv_host() const
{
LEX_CSTRING priv_host;
DBUG_ENTER("Security_context::priv_host");
priv_host.str= m_priv_host;
priv_host.length= m_priv_host_length;
DBUG_RETURN(priv_host);
}
inline void assign_priv_host(const char *priv_host_arg,
const size_t priv_host_arg_length);
const char* priv_host_name() const
{
return (*m_priv_host ? m_priv_host : (char *) "%");
}
/**
Getter method for member m_master_access.
*/
ulong master_access() const
{
return m_master_access;
}
void set_master_access(ulong master_access)
{
m_master_access= master_access;
}
/**
Check if a an account has been assigned to the security context
The account assigment to the security context is always executed in the
following order:
1) assign user's name to the context
2) assign user's hostname to the context
Whilst user name can be null, hostname cannot. This is why we can say that
the full account has been assigned to the context when hostname is not
equal to empty string.
@return Account assignment status
@retval TRUE account has been assigned to the security context
@retval FALSE account has not yet been assigned to the security context
*/
bool has_account_assigned() const
{
return m_priv_host[0] != '\0';
}
/**
Check permission against m_master_access
*/
bool check_access(ulong want_access, bool match_any= false)
{
return (match_any ? (m_master_access & want_access) :
((m_master_access & want_access) == want_access));
}
/**
Getter method for member m_db_access.
*/
ulong db_access() const
{
return m_db_access;
}
void set_db_access(ulong db_access)
{
m_db_access= db_access;
}
/**
Getter method for member m_password_expired.
*/
bool password_expired() const { return m_password_expired; }
void set_password_expired(bool password_expired)
{
m_password_expired= password_expired;
}
#ifndef NO_EMBEDDED_ACCESS_CHECKS
bool
change_security_context(THD *thd,
const LEX_CSTRING &definer_user,
const LEX_CSTRING &definer_host,
LEX_STRING *db,
Security_context **backup);
void
restore_security_context(THD *thd, Security_context *backup);
#endif
bool user_matches(Security_context *);
private:
void init();
void destroy();
void copy_security_ctx(const Security_context &src_sctx);
private:
/**
m_user - user of the client, set to NULL until the user has been read from
the connection
*/
String m_user;
/** m_host - host of the client */
String m_host;
/** m_ip - client IP */
String m_ip;
/**
m_host_or_ip - points to host if host is available, otherwise points to ip
*/
String m_host_or_ip;
String m_external_user;
/**
m_priv_user - The user privilege we are using. May be "" for anonymous user.
*/
char m_priv_user[USERNAME_LENGTH];
size_t m_priv_user_length;
char m_proxy_user[USERNAME_LENGTH + MAX_HOSTNAME + 5];
size_t m_proxy_user_length;
/**
The host privilege we are using
*/
char m_priv_host[MAX_HOSTNAME];
size_t m_priv_host_length;
/**
Global privileges from mysql.user.
*/
ulong m_master_access;
/**
Privileges for current db
*/
ulong m_db_access;
/**
password expiration flag.
This flag is set according to connecting user's context and not the
effective user.
*/
bool m_password_expired;
};
/**
Setter method for member m_user.
Function just sets the user_arg pointer value to the
m_user, user_arg value is *not* copied.
@param[in] user_arg New user value for m_user.
@param[in] user_arg_length Length of "user_arg" param.
*/
void Security_context::set_user_ptr(const char *user_arg,
const int user_arg_length)
{
DBUG_ENTER("Security_context::set_user_ptr");
if (user_arg == m_user.ptr())
DBUG_VOID_RETURN;
// set new user value to m_user.
m_user.set(user_arg, user_arg_length, system_charset_info);
DBUG_VOID_RETURN;
}
/**
Setter method for member m_user.
Copies user_arg value to the m_user if it is not null else m_user is set
to NULL.
@param[in] user_arg New user value for m_user.
@param[in] user_arg_length Length of "user_arg" param.
*/
void Security_context::assign_user(const char *user_arg,
const int user_arg_length)
{
DBUG_ENTER("Security_context::assign_user");
if (user_arg == m_user.ptr())
DBUG_VOID_RETURN;
if (user_arg)
m_user.copy(user_arg, user_arg_length, system_charset_info);
else
m_user.set((const char *) 0, 0, system_charset_info);
DBUG_VOID_RETURN;
}
/**
Setter method for member m_host.
Function just sets the host_arg pointer value to the
m_host, host_arg value is *not* copied.
@param[in] host_arg New user value for m_host.
@param[in] host_arg_length Length of "host_arg" param.
*/
void Security_context::set_host_ptr(const char *host_arg, const int host_arg_length)
{
DBUG_ENTER("Security_context::set_host_ptr");
if (host_arg == m_host.ptr())
DBUG_VOID_RETURN;
// set new host value to m_host.
m_host.set(host_arg, host_arg_length, system_charset_info);
DBUG_VOID_RETURN;
}
/**
Setter method for member m_host.
Copies host_arg value to the m_host if it is not null else m_user is set
to NULL.
@param[in] host_arg New user value for m_host.
@param[in] host_arg_length Length of "host_arg" param.
*/
void Security_context::assign_host(const char *host_arg, const int host_arg_length)
{
DBUG_ENTER("Security_context::assign_host");
if (host_arg == m_host.ptr())
DBUG_VOID_RETURN;
if (host_arg)
m_host.copy(host_arg, host_arg_length, system_charset_info);
else
m_host.set((const char *) 0, 0, system_charset_info);
DBUG_VOID_RETURN;
}
/**
Setter method for member m_ip.
Function just sets the ip_arg pointer value to the
m_ip, ip_arg value is *not* copied.
@param[in] ip_arg New user value for m_ip.
@param[in] ip_arg_length Length of "ip_arg" param.
*/
void Security_context::set_ip_ptr(const char *ip_arg, const int ip_arg_length)
{
DBUG_ENTER("Security_context::set_ip_ptr");
if (ip_arg == m_ip.ptr())
DBUG_VOID_RETURN;
// set new ip value to m_ip.
m_ip.set(ip_arg, ip_arg_length, system_charset_info);
DBUG_VOID_RETURN;
}
/**
Setter method for member m_ip.
Copies ip_arg value to the m_ip if it is not null else m_ip is set
to NULL.
@param[in] ip_arg New user value for m_ip.
@param[in] ip_arg_length Length of "ip_arg" param.
*/
void Security_context::assign_ip(const char *ip_arg, const int ip_arg_length)
{
DBUG_ENTER("Security_context::assign_ip");
if (ip_arg == m_ip.ptr())
DBUG_VOID_RETURN;
if (ip_arg)
m_ip.copy(ip_arg, ip_arg_length, system_charset_info);
else
m_ip.set((const char *) 0, 0, system_charset_info);
DBUG_VOID_RETURN;
}
/**
Setter method for member m_external_user.
Function just sets the ext_user_arg pointer to the
m_external_user, ext_user_arg is *not* copied.
@param[in] ext_user_arg New user value for m_external_user.
@param[in] ext_user_arg_length Length of "ext_user_arg" param.
*/
void Security_context::set_external_user_ptr(const char *ext_user_arg,
const int ext_user_arg_length)
{
DBUG_ENTER("Security_context::set_external_user_ptr");
if (ext_user_arg == m_external_user.ptr())
DBUG_VOID_RETURN;
// set new ip value to m_ip.
m_external_user.set(ext_user_arg, ext_user_arg_length, system_charset_info);
DBUG_VOID_RETURN;
}
/**
Setter method for member m_external_user.
Copies ext_user_arg value to the m_external_user if it is not null
else m_external_user is set to NULL.
@param[in] ext_user_arg New user value for m_external_user.
@param[in] ext_user_arg_length Length of "ext_user_arg" param.
*/
void Security_context::assign_external_user(const char *ext_user_arg,
const int ext_user_arg_length)
{
DBUG_ENTER("Security_context::assign_external_user");
if (ext_user_arg == m_external_user.ptr())
DBUG_VOID_RETURN;
if (ext_user_arg)
m_external_user.copy(ext_user_arg, ext_user_arg_length,
system_charset_info);
else
m_external_user.set((const char *) 0, 0, system_charset_info);
DBUG_VOID_RETURN;
}
/**
Setter method for member m_priv_user.
@param[in] priv_user_arg New user value for m_priv_user.
@param[in] priv_user_arg_length Length of "priv_user_arg" param.
*/
void Security_context::assign_priv_user(const char *priv_user_arg,
const size_t priv_user_arg_length)
{
DBUG_ENTER("Security_context::assign_priv_user");
if (priv_user_arg_length)
{
m_priv_user_length= std::min(priv_user_arg_length,
sizeof(m_priv_user) - 1);
strmake(m_priv_user, priv_user_arg, m_priv_user_length);
}
else
{
*m_priv_user= 0;
m_priv_user_length= 0;
}
DBUG_VOID_RETURN;
}
/**
Setter method for member m_proxy_user.
@param[in] proxy_user_arg New user value for m_proxy_user.
@param[in] proxy_user_arg_length Length of "proxy_user_arg" param.
*/
void Security_context::assign_proxy_user(const char *proxy_user_arg,
const size_t proxy_user_arg_length)
{
DBUG_ENTER("Security_context::assign_proxy_user");
if (proxy_user_arg_length)
{
m_proxy_user_length= std::min(proxy_user_arg_length,
sizeof(m_proxy_user) - 1);
strmake(m_proxy_user, proxy_user_arg, m_proxy_user_length);
}
else
{
*m_proxy_user= 0;
m_proxy_user_length= 0;
}
DBUG_VOID_RETURN;
}
/**
Setter method for member m_priv_host.
@param[in] priv_host_arg New user value for m_priv_host.
@param[in] priv_host_arg_length Length of "priv_host_arg" param.
*/
void Security_context::assign_priv_host(const char *priv_host_arg,
const size_t priv_host_arg_length)
{
DBUG_ENTER("Security_context::assign_priv_host");
if (priv_host_arg_length)
{
m_priv_host_length= std::min(priv_host_arg_length,
sizeof(m_priv_host) - 1);
strmake(m_priv_host, priv_host_arg, m_priv_host_length);
}
else
{
*m_priv_host= 0;
m_priv_host_length= 0;
}
DBUG_VOID_RETURN;
}
#endif /* SQL_SECURITY_CTX_INCLUDED */