forked from trdtnguyen/mysql-plnvm
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathservice_security_context.cc
334 lines (304 loc) · 9.67 KB
/
service_security_context.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
/* Copyright (c) 2015, 2017, 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 */
#ifndef SQL_SECURITY_CTX_SERVICE_INCLUDED
#define SQL_SECURITY_CTX_SERVICE_INCLUDED
#ifndef NO_EMBEDDED_ACCESS_CHECKS
#include "auth_common.h"
#include "sql_class.h"
#include <mysql/service_security_context.h>
#define MY_SVC_TRUE 1
#define MY_SVC_FALSE 0
/**
Gets the security context for the thread.
@param[in] thd The thread to get the context from
@param[out] out_ctx placeholder for the security context handle
@retval true failure
@retval false success
*/
my_svc_bool thd_get_security_context(MYSQL_THD _thd,
MYSQL_SECURITY_CONTEXT *out_ctx)
{
THD *thd= dynamic_cast<THD *>(_thd);
try
{
if (out_ctx)
*out_ctx= thd->security_context();
else
return MY_SVC_TRUE;
return MY_SVC_FALSE;
}
catch (...)
{
return MY_SVC_FALSE;
}
}
/**
Sets a new security context for the thread.
@param[in] thd The thread to set the context to
@param[in] ctx The handle of the new security context
@retval true failure
@retval false success
*/
my_svc_bool thd_set_security_context(MYSQL_THD _thd,
MYSQL_SECURITY_CONTEXT in_ctx)
{
THD *thd= dynamic_cast<THD *>(_thd);
try
{
if (in_ctx)
thd->set_security_context(in_ctx);
return MY_SVC_FALSE;
}
catch (...)
{
return MY_SVC_TRUE;
}
}
/**
Creates a new security context and initializes it with the defaults
(no access, no user etc).
@param[out] out_ctx placeholder for the newly created security context handle
@retval true failure
@retval false success
*/
my_svc_bool security_context_create(MYSQL_SECURITY_CONTEXT *out_ctx)
{
try
{
if (out_ctx)
*out_ctx= new Security_context();
return MY_SVC_FALSE;
}
catch (...)
{
return MY_SVC_TRUE;
}
}
/**
Deallocates a security context.
@param[in] ctx The handle of the security context to destroy
@retval true failure
@retval false success
*/
my_svc_bool security_context_destroy(MYSQL_SECURITY_CONTEXT ctx)
{
try
{
delete ctx;
return MY_SVC_FALSE;
}
catch (...)
{
return MY_SVC_TRUE;
}
}
/**
Duplicates a security context.
@param[in] ctx The handle of the security context to copy
@param[out] out_ctx placeholder for the handle of the copied security context
@retval true failure
@retval false success
*/
my_svc_bool security_context_copy(MYSQL_SECURITY_CONTEXT in_ctx,
MYSQL_SECURITY_CONTEXT *out_ctx)
{
try
{
if (out_ctx)
{
*out_ctx= new Security_context();
if (in_ctx)
**out_ctx= *in_ctx;
}
return MY_SVC_FALSE;
}
catch (...)
{
return MY_SVC_TRUE;
}
}
/**
Looks up in the defined user accounts an account based on
the user@host[ip] combo supplied and checks if the user
has access to the database requested.
The lookup is done in exactly the same way as at login time.
@param[in] ctx The handle of the security context to update
@param[in] user The user name to look up
@param[in] host The host name to look up
@param[in] ip The ip of the incoming connection
@param[in] db The database to check access to
@retval true failure
@retval false success
*/
my_svc_bool security_context_lookup(MYSQL_SECURITY_CONTEXT ctx,
const char *user, const char *host,
const char *ip, const char *db)
{
return acl_getroot(ctx, (char *) user, (char *) host, (char *) ip, db) ?
TRUE : FALSE;
}
/**
Reads a named security context attribute and retuns its value.
Currently defined names are:
user MYSQL_LEX_CSTRING * login user (a.k.a. the user's part of USER())
host MYSQL_LEX_CSTRING * login host (a.k.a. the host's part of USER())
ip MYSQL_LEX_CSTRING * login client ip
host_or_ip MYSQL_LEX_CSTRING * host, if present, ip if not.
priv_user MYSQL_LEX_CSTRING * authenticated user (a.k.a. the user's part of CURRENT_USER())
priv_host MYSQL_LEX_CSTRING * authenticated host (a.k.a. the host's part of CURRENT_USER())
proxy_user MYSQL_LEX_CSTRING * the proxy user used in authenticating
privilege_super my_svc_bool * 1 if the user account has supper privilege, 0 otherwise
privilege_execute my_svc_bool * 1 if the user account has execute privilege, 0 otherwise
@param[in] ctx The handle of the security context to read from
@param[in] name The option name to read
@param[out] value The value of the option. Type depens on the name.
@retval true failure
@retval false success
*/
my_svc_bool security_context_get_option(MYSQL_SECURITY_CONTEXT ctx,
const char *name, void *inout_pvalue)
{
try
{
if (inout_pvalue)
{
if (!strcmp(name, "user"))
{
*((MYSQL_LEX_CSTRING *)inout_pvalue)= ctx->user();
}
else if (!strcmp(name, "host"))
{
*((MYSQL_LEX_CSTRING *) inout_pvalue)= ctx->host();
}
else if (!strcmp(name, "ip"))
{
*((MYSQL_LEX_CSTRING *) inout_pvalue)= ctx->ip();
}
else if (!strcmp(name, "host_or_ip"))
{
*((MYSQL_LEX_CSTRING *) inout_pvalue)= ctx->host_or_ip();
}
else if (!strcmp(name, "priv_user"))
{
*((MYSQL_LEX_CSTRING *) inout_pvalue)= ctx->priv_user();
}
else if (!strcmp(name, "priv_host"))
{
*((MYSQL_LEX_CSTRING *) inout_pvalue)= ctx->priv_host();
}
else if (!strcmp(name, "proxy_user"))
{
*((MYSQL_LEX_CSTRING *) inout_pvalue)= ctx->proxy_user();
}
else if (!strcmp(name, "external_user"))
{
*((MYSQL_LEX_CSTRING *) inout_pvalue)= ctx->external_user();
}
else if (!strcmp(name, "privilege_super"))
{
bool checked= ctx->check_access(SUPER_ACL);
*((my_svc_bool *) inout_pvalue)= checked ? MY_SVC_TRUE : MY_SVC_FALSE;
}
else if (!strcmp(name, "privilege_execute"))
{
bool checked= ctx->check_access(EXECUTE_ACL);
*((my_svc_bool *) inout_pvalue)= checked ? MY_SVC_TRUE : MY_SVC_FALSE;
}
else
return MY_SVC_TRUE; /** invalid option */
}
return MY_SVC_FALSE;
}
catch (...)
{
return MY_SVC_TRUE;
}
}
/**
Sets a value for a named security context attribute
Currently defined names are:
user MYSQL_LEX_CSTRING * login user (a.k.a. the user's part of USER())
host MYSQL_LEX_CSTRING * login host (a.k.a. the host's part of USER())
ip MYSQL_LEX_CSTRING * login client ip
priv_user MYSQL_LEX_CSTRING * authenticated user (a.k.a. the user's part of CURRENT_USER())
priv_host MYSQL_LEX_CSTRING * authenticated host (a.k.a. the host's part of CURRENT_USER())
proxy_user MYSQL_LEX_CSTRING * the proxy user used in authenticating
privilege_super my_svc_bool * 1 if the user account has supper privilege, 0 otherwise
privilege_execute my_svc_bool * 1 if the user account has execute privilege, 0 otherwise
@param[in] ctx The handle of the security context to set into
@param[in] name The option name to set
@param[in] value The value of the option. Type depens on the name.
@retval true failure
@retval false success
*/
my_svc_bool security_context_set_option(MYSQL_SECURITY_CONTEXT ctx,
const char *name, void *pvalue)
{
try
{
if (!strcmp(name, "user"))
{
LEX_CSTRING *value= (LEX_CSTRING *) pvalue;
ctx->assign_user(value->str, value->length);
}
else if (!strcmp(name, "host"))
{
LEX_CSTRING *value= (LEX_CSTRING *) pvalue;
ctx->assign_host(value->str, value->length);
}
else if (!strcmp(name, "ip"))
{
LEX_CSTRING *value= (LEX_CSTRING *) pvalue;
ctx->assign_ip(value->str, value->length);
}
else if (!strcmp(name, "priv_user"))
{
LEX_CSTRING *value= (LEX_CSTRING *) pvalue;
ctx->assign_priv_user(value->str, value->length);
}
else if (!strcmp(name, "priv_host"))
{
LEX_CSTRING *value= (LEX_CSTRING *) pvalue;
ctx->assign_priv_host(value->str, value->length);
}
else if (!strcmp(name, "proxy_user"))
{
LEX_CSTRING *value= (LEX_CSTRING *) pvalue;
ctx->assign_proxy_user(value->str, value->length);
}
else if (!strcmp(name, "privilege_super"))
{
my_svc_bool value= *(my_svc_bool *) pvalue;
if (value)
ctx->set_master_access(ctx->master_access() | (SUPER_ACL));
else
ctx->set_master_access(ctx->master_access() & ~(SUPER_ACL));
}
else if (!strcmp(name, "privilege_execute"))
{
my_svc_bool value= *(my_svc_bool *) pvalue;
if (value)
ctx->set_master_access(ctx->master_access() | (EXECUTE_ACL));
else
ctx->set_master_access(ctx->master_access() & ~(EXECUTE_ACL));
}
else
return MY_SVC_TRUE; /** invalid option */
return MY_SVC_FALSE;
}
catch (...)
{
return MY_SVC_TRUE;
}
}
#endif /* !NO_EMBEDDED_ACCESS_CHECKS */
#endif /* !SQL_SECURITY_CTX_SERVICE_INCLUDED */