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
|
/* Copyright (c) 2017, 2025, Oracle and/or its affiliates.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2.0,
as published by the Free Software Foundation.
This program is designed to work with certain software (including
but not limited to OpenSSL) that is licensed under separate terms,
as designated in a particular file or component or in included license
documentation. The authors of MySQL hereby grant you an additional
permission to link the program and your derivative works with the
separately licensed software that they have either included with
the program or referenced in the documentation.
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, version 2.0, 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 */
#include "security_context_imp.h"
#include <mysql/components/minimal_chassis.h>
#include "sql/auth/auth_acls.h"
#include "sql/current_thd.h"
#include "sql/sql_class.h"
#include "sql/sql_thd_internal_api.h" // create_internal_thd
/**
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
*/
DEFINE_BOOL_METHOD(mysql_security_context_imp::get,
(void *_thd, Security_context_handle *out_ctx)) {
THD *thd = reinterpret_cast<THD *>(_thd);
if (!out_ctx) return true;
try {
*out_ctx =
reinterpret_cast<Security_context_handle>(thd->security_context());
return false;
} catch (...) {
mysql_components_handle_std_exception(__func__);
}
return true;
}
/**
Sets a new security context for the thread.
@param[in] _thd The thread to set the context to
@param[in] in_ctx The handle of the new security context
@retval true failure
@retval false success
*/
DEFINE_BOOL_METHOD(mysql_security_context_imp::set,
(void *_thd, Security_context_handle in_ctx)) {
THD *thd = reinterpret_cast<THD *>(_thd);
if (!in_ctx) return true;
try {
Security_context *in_sctx = reinterpret_cast<Security_context *>(in_ctx);
if (in_sctx) {
thd->set_security_context(in_sctx);
in_sctx->set_thd(thd);
// Turn ON the flag in THD iff the user is granted SYSTEM_USER privilege
set_system_user_flag(thd);
// Update the flag in THD based on if the user is granted CONNECTION_ADMIN
// privilege
set_connection_admin_flag(thd);
}
return false;
} catch (...) {
mysql_components_handle_std_exception(__func__);
}
return 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
*/
DEFINE_BOOL_METHOD(mysql_security_context_imp::create,
(Security_context_handle * out_ctx)) {
try {
*out_ctx =
reinterpret_cast<Security_context_handle>(new Security_context());
return false;
} catch (...) {
mysql_components_handle_std_exception(__func__);
}
return true;
}
/**
Deallocates a security context.
@param[in] ctx The handle of the security context to destroy
@retval true failure
@retval false success
*/
DEFINE_BOOL_METHOD(mysql_security_context_imp::destroy,
(Security_context_handle ctx)) {
try {
delete reinterpret_cast<Security_context *>(ctx);
return false;
} catch (...) {
mysql_components_handle_std_exception(__func__);
}
return true;
}
/**
Duplicates a security context.
@param[in] 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
*/
DEFINE_BOOL_METHOD(mysql_security_context_imp::copy,
(Security_context_handle in_ctx,
Security_context_handle *out_ctx)) {
try {
if (out_ctx) {
*out_ctx =
reinterpret_cast<Security_context_handle>(new Security_context());
if (in_ctx && *out_ctx)
*out_ctx = reinterpret_cast<Security_context_handle>(in_ctx);
else
return true;
return false;
}
} catch (...) {
mysql_components_handle_std_exception(__func__);
}
return 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.
The new security context need to checkout additional privileges using
the checkout_acl method.
@param[in] ctx The handle of the security context to update
@param[in] user The user name to look up, the name has to be in utf8 charset
@param[in] host The host name to look up, the name has to be in utf8 charset
@param[in] ip The ip of the incoming connection
@param[in] db The database to check access to
@retval true failure
@retval false success
*/
DEFINE_BOOL_METHOD(mysql_security_context_imp::lookup,
(Security_context_handle ctx, const char *user,
const char *host, const char *ip, const char *db)) {
try {
THD *tmp_thd = nullptr;
bool retval;
if (current_thd == nullptr) {
tmp_thd = create_internal_thd();
if (!tmp_thd) return true;
}
retval = acl_getroot(tmp_thd ? tmp_thd : current_thd,
reinterpret_cast<Security_context *>(ctx), user, host,
ip, db)
? true
: false;
/*
If it is not a new security context then update the
system_user and connection_admin flags in its referenced THD.
*/
Security_context *sctx = reinterpret_cast<Security_context *>(ctx);
THD *sctx_thd = sctx->get_thd();
if (sctx_thd) {
set_system_user_flag(sctx_thd);
set_connection_admin_flag(sctx_thd);
}
if (tmp_thd) {
destroy_internal_thd(tmp_thd);
tmp_thd = nullptr;
}
return retval;
} catch (...) {
mysql_components_handle_std_exception(__func__);
}
return true;
}
/**
Reads a named security context attribute and returns 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 DECLARE_BOOL_METHOD * 1 if the user account has
supper privilege, 0 otherwise
- privilege_execute DECLARE_BOOL_METHOD * 1 if the user account has
execute privilege, 0 otherwise
@param[in] ctx_h The handle of the security context to read from
@param[in] name The option name to read
@param[out] inout_pvalue The value of the option. Type depends on the name.
@retval true failure
@retval false success
*/
DEFINE_BOOL_METHOD(mysql_security_context_imp::get,
(Security_context_handle ctx_h, const char *name,
void *inout_pvalue)) {
try {
Security_context *ctx = reinterpret_cast<Security_context *>(ctx_h);
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);
*((bool *)inout_pvalue) = checked ? true : false;
} else if (!strcmp(name, "privilege_execute")) {
bool checked = ctx->check_access(EXECUTE_ACL);
*((bool *)inout_pvalue) = checked ? true : false;
} else
return true; /* invalid option */
}
return false;
} catch (...) {
mysql_components_handle_std_exception(__func__);
}
return 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 DECLARE_BOOL_METHOD * 1 if the user account has
supper privilege, 0 otherwise
- privilege_execute DECLARE_BOOL_METHOD * 1 if the user account has
execute privilege, 0 otherwise
@param[in] ctx_h The handle of the security context to set into
@param[in] name The option name to set
@param[in] pvalue The value of the option. Type depends on the name.
@retval true failure
@retval false success
*/
DEFINE_BOOL_METHOD(mysql_security_context_imp::set,
(Security_context_handle ctx_h, const char *name,
void *pvalue)) {
try {
Security_context *ctx = reinterpret_cast<Security_context *>(ctx_h);
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")) {
char value = *(char *)pvalue;
if (value)
ctx->set_master_access(ctx->master_access() | (SUPER_ACL),
ctx->restrictions());
else
ctx->set_master_access(ctx->master_access() & ~(SUPER_ACL),
ctx->restrictions());
} else if (!strcmp(name, "privilege_execute")) {
char value = *(char *)pvalue;
if (value)
ctx->set_master_access(ctx->master_access() | (EXECUTE_ACL),
ctx->restrictions());
else
ctx->set_master_access(ctx->master_access() & ~(EXECUTE_ACL),
ctx->restrictions());
} else
return true; /* invalid option */
return false;
} catch (...) {
mysql_components_handle_std_exception(__func__);
}
return true;
}
|