~michaeleguo/ubuntu/trusty/percona-xtradb-cluster-5.5/arm64fix

« back to all changes in this revision

Viewing changes to sql/sql_acl.h

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2014-02-10 14:44:23 UTC
  • Revision ID: package-import@ubuntu.com-20140210144423-f2134l2gxuvq2m6l
Tags: upstream-5.5.34-25.9+dfsg
ImportĀ upstreamĀ versionĀ 5.5.34-25.9+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#ifndef SQL_ACL_INCLUDED
 
2
#define SQL_ACL_INCLUDED
 
3
 
 
4
/* Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
 
5
 
 
6
   This program is free software; you can redistribute it and/or modify
 
7
   it under the terms of the GNU General Public License as published by
 
8
   the Free Software Foundation; version 2 of the License.
 
9
 
 
10
   This program is distributed in the hope that it will be useful,
 
11
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
   GNU General Public License for more details.
 
14
 
 
15
   You should have received a copy of the GNU General Public License
 
16
   along with this program; if not, write to the Free Software
 
17
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA */
 
18
 
 
19
#include "my_global.h"                          /* NO_EMBEDDED_ACCESS_CHECKS */
 
20
#include "violite.h"                            /* SSL_type */
 
21
#include "sql_class.h"                          /* LEX_COLUMN */
 
22
 
 
23
#define SELECT_ACL      (1L << 0)
 
24
#define INSERT_ACL      (1L << 1)
 
25
#define UPDATE_ACL      (1L << 2)
 
26
#define DELETE_ACL      (1L << 3)
 
27
#define CREATE_ACL      (1L << 4)
 
28
#define DROP_ACL        (1L << 5)
 
29
#define RELOAD_ACL      (1L << 6)
 
30
#define SHUTDOWN_ACL    (1L << 7)
 
31
#define PROCESS_ACL     (1L << 8)
 
32
#define FILE_ACL        (1L << 9)
 
33
#define GRANT_ACL       (1L << 10)
 
34
#define REFERENCES_ACL  (1L << 11)
 
35
#define INDEX_ACL       (1L << 12)
 
36
#define ALTER_ACL       (1L << 13)
 
37
#define SHOW_DB_ACL     (1L << 14)
 
38
#define SUPER_ACL       (1L << 15)
 
39
#define CREATE_TMP_ACL  (1L << 16)
 
40
#define LOCK_TABLES_ACL (1L << 17)
 
41
#define EXECUTE_ACL     (1L << 18)
 
42
#define REPL_SLAVE_ACL  (1L << 19)
 
43
#define REPL_CLIENT_ACL (1L << 20)
 
44
#define CREATE_VIEW_ACL (1L << 21)
 
45
#define SHOW_VIEW_ACL   (1L << 22)
 
46
#define CREATE_PROC_ACL (1L << 23)
 
47
#define ALTER_PROC_ACL  (1L << 24)
 
48
#define CREATE_USER_ACL (1L << 25)
 
49
#define EVENT_ACL       (1L << 26)
 
50
#define TRIGGER_ACL     (1L << 27)
 
51
#define CREATE_TABLESPACE_ACL (1L << 28)
 
52
/*
 
53
  don't forget to update
 
54
  1. static struct show_privileges_st sys_privileges[]
 
55
  2. static const char *command_array[] and static uint command_lengths[]
 
56
  3. mysql_system_tables.sql and mysql_system_tables_fix.sql
 
57
  4. acl_init() or whatever - to define behaviour for old privilege tables
 
58
  5. sql_yacc.yy - for GRANT/REVOKE to work
 
59
*/
 
60
#define NO_ACCESS       (1L << 30)
 
61
#define DB_ACLS \
 
62
(UPDATE_ACL | SELECT_ACL | INSERT_ACL | DELETE_ACL | CREATE_ACL | DROP_ACL | \
 
63
 GRANT_ACL | REFERENCES_ACL | INDEX_ACL | ALTER_ACL | CREATE_TMP_ACL | \
 
64
 LOCK_TABLES_ACL | EXECUTE_ACL | CREATE_VIEW_ACL | SHOW_VIEW_ACL | \
 
65
 CREATE_PROC_ACL | ALTER_PROC_ACL | EVENT_ACL | TRIGGER_ACL)
 
66
 
 
67
#define TABLE_ACLS \
 
68
(SELECT_ACL | INSERT_ACL | UPDATE_ACL | DELETE_ACL | CREATE_ACL | DROP_ACL | \
 
69
 GRANT_ACL | REFERENCES_ACL | INDEX_ACL | ALTER_ACL | CREATE_VIEW_ACL | \
 
70
 SHOW_VIEW_ACL | TRIGGER_ACL)
 
71
 
 
72
#define COL_ACLS \
 
73
(SELECT_ACL | INSERT_ACL | UPDATE_ACL | REFERENCES_ACL)
 
74
 
 
75
#define PROC_ACLS \
 
76
(ALTER_PROC_ACL | EXECUTE_ACL | GRANT_ACL)
 
77
 
 
78
#define SHOW_PROC_ACLS \
 
79
(ALTER_PROC_ACL | EXECUTE_ACL | CREATE_PROC_ACL)
 
80
 
 
81
#define GLOBAL_ACLS \
 
82
(SELECT_ACL | INSERT_ACL | UPDATE_ACL | DELETE_ACL | CREATE_ACL | DROP_ACL | \
 
83
 RELOAD_ACL | SHUTDOWN_ACL | PROCESS_ACL | FILE_ACL | GRANT_ACL | \
 
84
 REFERENCES_ACL | INDEX_ACL | ALTER_ACL | SHOW_DB_ACL | SUPER_ACL | \
 
85
 CREATE_TMP_ACL | LOCK_TABLES_ACL | REPL_SLAVE_ACL | REPL_CLIENT_ACL | \
 
86
 EXECUTE_ACL | CREATE_VIEW_ACL | SHOW_VIEW_ACL | CREATE_PROC_ACL | \
 
87
 ALTER_PROC_ACL | CREATE_USER_ACL | EVENT_ACL | TRIGGER_ACL | \
 
88
 CREATE_TABLESPACE_ACL)
 
89
 
 
90
#define DEFAULT_CREATE_PROC_ACLS \
 
91
(ALTER_PROC_ACL | EXECUTE_ACL)
 
92
 
 
93
#define SHOW_CREATE_TABLE_ACLS \
 
94
(SELECT_ACL | INSERT_ACL | UPDATE_ACL | DELETE_ACL | \
 
95
 CREATE_ACL | DROP_ACL | ALTER_ACL | INDEX_ACL | \
 
96
 TRIGGER_ACL | REFERENCES_ACL | GRANT_ACL | CREATE_VIEW_ACL | SHOW_VIEW_ACL)
 
97
 
 
98
/*
 
99
  Defines to change the above bits to how things are stored in tables
 
100
  This is needed as the 'host' and 'db' table is missing a few privileges
 
101
*/
 
102
 
 
103
/* Privileges that needs to be reallocated (in continous chunks) */
 
104
#define DB_CHUNK0 (SELECT_ACL | INSERT_ACL | UPDATE_ACL | DELETE_ACL | \
 
105
                   CREATE_ACL | DROP_ACL)
 
106
#define DB_CHUNK1 (GRANT_ACL | REFERENCES_ACL | INDEX_ACL | ALTER_ACL)
 
107
#define DB_CHUNK2 (CREATE_TMP_ACL | LOCK_TABLES_ACL)
 
108
#define DB_CHUNK3 (CREATE_VIEW_ACL | SHOW_VIEW_ACL | \
 
109
                   CREATE_PROC_ACL | ALTER_PROC_ACL )
 
110
#define DB_CHUNK4 (EXECUTE_ACL)
 
111
#define DB_CHUNK5 (EVENT_ACL | TRIGGER_ACL)
 
112
 
 
113
#define fix_rights_for_db(A)  (((A)       & DB_CHUNK0) | \
 
114
                              (((A) << 4) & DB_CHUNK1) | \
 
115
                              (((A) << 6) & DB_CHUNK2) | \
 
116
                              (((A) << 9) & DB_CHUNK3) | \
 
117
                              (((A) << 2) & DB_CHUNK4))| \
 
118
                              (((A) << 9) & DB_CHUNK5)
 
119
#define get_rights_for_db(A)  (((A) & DB_CHUNK0)       | \
 
120
                              (((A) & DB_CHUNK1) >> 4) | \
 
121
                              (((A) & DB_CHUNK2) >> 6) | \
 
122
                              (((A) & DB_CHUNK3) >> 9) | \
 
123
                              (((A) & DB_CHUNK4) >> 2))| \
 
124
                              (((A) & DB_CHUNK5) >> 9)
 
125
#define TBL_CHUNK0 DB_CHUNK0
 
126
#define TBL_CHUNK1 DB_CHUNK1
 
127
#define TBL_CHUNK2 (CREATE_VIEW_ACL | SHOW_VIEW_ACL)
 
128
#define TBL_CHUNK3 TRIGGER_ACL
 
129
#define fix_rights_for_table(A) (((A)        & TBL_CHUNK0) | \
 
130
                                (((A) <<  4) & TBL_CHUNK1) | \
 
131
                                (((A) << 11) & TBL_CHUNK2) | \
 
132
                                (((A) << 15) & TBL_CHUNK3))
 
133
#define get_rights_for_table(A) (((A) & TBL_CHUNK0)        | \
 
134
                                (((A) & TBL_CHUNK1) >>  4) | \
 
135
                                (((A) & TBL_CHUNK2) >> 11) | \
 
136
                                (((A) & TBL_CHUNK3) >> 15))
 
137
#define fix_rights_for_column(A) (((A) & 7) | (((A) & ~7) << 8))
 
138
#define get_rights_for_column(A) (((A) & 7) | ((A) >> 8))
 
139
#define fix_rights_for_procedure(A) ((((A) << 18) & EXECUTE_ACL) | \
 
140
                                     (((A) << 23) & ALTER_PROC_ACL) | \
 
141
                                     (((A) << 8) & GRANT_ACL))
 
142
#define get_rights_for_procedure(A) ((((A) & EXECUTE_ACL) >> 18) |  \
 
143
                                     (((A) & ALTER_PROC_ACL) >> 23) | \
 
144
                                     (((A) & GRANT_ACL) >> 8))
 
145
 
 
146
enum mysql_db_table_field
 
147
{
 
148
  MYSQL_DB_FIELD_HOST = 0,
 
149
  MYSQL_DB_FIELD_DB,
 
150
  MYSQL_DB_FIELD_USER,
 
151
  MYSQL_DB_FIELD_SELECT_PRIV,
 
152
  MYSQL_DB_FIELD_INSERT_PRIV,
 
153
  MYSQL_DB_FIELD_UPDATE_PRIV,
 
154
  MYSQL_DB_FIELD_DELETE_PRIV,
 
155
  MYSQL_DB_FIELD_CREATE_PRIV,
 
156
  MYSQL_DB_FIELD_DROP_PRIV,
 
157
  MYSQL_DB_FIELD_GRANT_PRIV,
 
158
  MYSQL_DB_FIELD_REFERENCES_PRIV,
 
159
  MYSQL_DB_FIELD_INDEX_PRIV,
 
160
  MYSQL_DB_FIELD_ALTER_PRIV,
 
161
  MYSQL_DB_FIELD_CREATE_TMP_TABLE_PRIV,
 
162
  MYSQL_DB_FIELD_LOCK_TABLES_PRIV,
 
163
  MYSQL_DB_FIELD_CREATE_VIEW_PRIV,
 
164
  MYSQL_DB_FIELD_SHOW_VIEW_PRIV,
 
165
  MYSQL_DB_FIELD_CREATE_ROUTINE_PRIV,
 
166
  MYSQL_DB_FIELD_ALTER_ROUTINE_PRIV,
 
167
  MYSQL_DB_FIELD_EXECUTE_PRIV,
 
168
  MYSQL_DB_FIELD_EVENT_PRIV,
 
169
  MYSQL_DB_FIELD_TRIGGER_PRIV,
 
170
  MYSQL_DB_FIELD_COUNT
 
171
};
 
172
 
 
173
extern const TABLE_FIELD_DEF mysql_db_table_def;
 
174
extern bool mysql_user_table_is_in_short_password_format;
 
175
 
 
176
/* prototypes */
 
177
 
 
178
bool hostname_requires_resolving(const char *hostname);
 
179
my_bool  acl_init(bool dont_read_acl_tables);
 
180
my_bool acl_reload(THD *thd);
 
181
void acl_free(bool end=0);
 
182
ulong acl_get(const char *host, const char *ip,
 
183
              const char *user, const char *db, my_bool db_is_pattern);
 
184
bool acl_authenticate(THD *thd, uint connect_errors, uint com_change_user_pkt_len);
 
185
bool acl_getroot(Security_context *sctx, char *user, char *host,
 
186
                 char *ip, char *db);
 
187
bool acl_check_host(const char *host, const char *ip);
 
188
int check_change_password(THD *thd, const char *host, const char *user,
 
189
                           char *password, uint password_len);
 
190
bool change_password(THD *thd, const char *host, const char *user,
 
191
                     char *password);
 
192
bool mysql_grant(THD *thd, const char *db, List <LEX_USER> &user_list,
 
193
                 ulong rights, bool revoke, bool is_proxy);
 
194
int mysql_table_grant(THD *thd, TABLE_LIST *table, List <LEX_USER> &user_list,
 
195
                       List <LEX_COLUMN> &column_list, ulong rights,
 
196
                       bool revoke);
 
197
bool mysql_routine_grant(THD *thd, TABLE_LIST *table, bool is_proc,
 
198
                         List <LEX_USER> &user_list, ulong rights,
 
199
                         bool revoke, bool write_to_binlog);
 
200
my_bool grant_init();
 
201
void grant_free(void);
 
202
my_bool grant_reload(THD *thd);
 
203
bool check_grant(THD *thd, ulong want_access, TABLE_LIST *tables,
 
204
                 bool any_combination_will_do, uint number, bool no_errors);
 
205
bool check_grant_column (THD *thd, GRANT_INFO *grant,
 
206
                         const char *db_name, const char *table_name,
 
207
                         const char *name, uint length, Security_context *sctx);
 
208
bool check_column_grant_in_table_ref(THD *thd, TABLE_LIST * table_ref,
 
209
                                     const char *name, uint length);
 
210
bool check_grant_all_columns(THD *thd, ulong want_access, 
 
211
                             Field_iterator_table_ref *fields);
 
212
bool check_grant_routine(THD *thd, ulong want_access,
 
213
                         TABLE_LIST *procs, bool is_proc, bool no_error);
 
214
bool check_grant_db(THD *thd,const char *db);
 
215
ulong get_table_grant(THD *thd, TABLE_LIST *table);
 
216
ulong get_column_grant(THD *thd, GRANT_INFO *grant,
 
217
                       const char *db_name, const char *table_name,
 
218
                       const char *field_name);
 
219
bool mysql_show_grants(THD *thd, LEX_USER *user);
 
220
void get_privilege_desc(char *to, uint max_length, ulong access);
 
221
void get_mqh(const char *user, const char *host, USER_CONN *uc);
 
222
bool mysql_create_user(THD *thd, List <LEX_USER> &list);
 
223
bool mysql_drop_user(THD *thd, List <LEX_USER> &list);
 
224
bool mysql_rename_user(THD *thd, List <LEX_USER> &list);
 
225
bool mysql_revoke_all(THD *thd, List <LEX_USER> &list);
 
226
void fill_effective_table_privileges(THD *thd, GRANT_INFO *grant,
 
227
                                     const char *db, const char *table);
 
228
bool sp_revoke_privileges(THD *thd, const char *sp_db, const char *sp_name,
 
229
                          bool is_proc);
 
230
bool sp_grant_privileges(THD *thd, const char *sp_db, const char *sp_name,
 
231
                         bool is_proc);
 
232
bool check_routine_level_acl(THD *thd, const char *db, const char *name,
 
233
                             bool is_proc);
 
234
bool is_acl_user(const char *host, const char *user);
 
235
int fill_schema_user_privileges(THD *thd, TABLE_LIST *tables, COND *cond);
 
236
int fill_schema_schema_privileges(THD *thd, TABLE_LIST *tables, COND *cond);
 
237
int fill_schema_table_privileges(THD *thd, TABLE_LIST *tables, COND *cond);
 
238
int fill_schema_column_privileges(THD *thd, TABLE_LIST *tables, COND *cond);
 
239
int wild_case_compare(CHARSET_INFO *cs, const char *str,const char *wildstr);
 
240
 
 
241
my_bool acl_is_utility_user(const char *user, const char *host,
 
242
                         const char *ip);
 
243
 
 
244
#ifdef NO_EMBEDDED_ACCESS_CHECKS
 
245
#define check_grant(A,B,C,D,E,F) 0
 
246
#define check_grant_db(A,B) 0
 
247
#endif
 
248
 
 
249
/**
 
250
  Result of an access check for an internal schema or table.
 
251
  Internal ACL checks are always performed *before* using
 
252
  the grant tables.
 
253
  This mechanism enforces that the server implementation has full
 
254
  control on its internal tables.
 
255
  Depending on the internal check result, the server implementation
 
256
  can choose to:
 
257
  - always allow access,
 
258
  - always deny access,
 
259
  - delegate the decision to the database administrator,
 
260
  by using the grant tables.
 
261
*/
 
262
enum ACL_internal_access_result
 
263
{
 
264
  /**
 
265
    Access granted for all the requested privileges,
 
266
    do not use the grant tables.
 
267
    This flag is used only for the INFORMATION_SCHEMA privileges,
 
268
    for compatibility reasons.
 
269
  */
 
270
  ACL_INTERNAL_ACCESS_GRANTED,
 
271
  /** Access denied, do not use the grant tables. */
 
272
  ACL_INTERNAL_ACCESS_DENIED,
 
273
  /** No decision yet, use the grant tables. */
 
274
  ACL_INTERNAL_ACCESS_CHECK_GRANT
 
275
};
 
276
 
 
277
/**
 
278
  Per internal table ACL access rules.
 
279
  This class is an interface.
 
280
  Per table(s) specific access rule should be implemented in a subclass.
 
281
  @sa ACL_internal_schema_access
 
282
*/
 
283
class ACL_internal_table_access
 
284
{
 
285
public:
 
286
  ACL_internal_table_access()
 
287
  {}
 
288
 
 
289
  virtual ~ACL_internal_table_access()
 
290
  {}
 
291
 
 
292
  /**
 
293
    Check access to an internal table.
 
294
    When a privilege is granted, this method add the requested privilege
 
295
    to save_priv.
 
296
    @param want_access the privileges requested
 
297
    @param [in, out] save_priv the privileges granted
 
298
    @return
 
299
      @retval ACL_INTERNAL_ACCESS_GRANTED All the requested privileges
 
300
      are granted, and saved in save_priv.
 
301
      @retval ACL_INTERNAL_ACCESS_DENIED At least one of the requested
 
302
      privileges was denied.
 
303
      @retval ACL_INTERNAL_ACCESS_CHECK_GRANT No requested privilege
 
304
      was denied, and grant should be checked for at least one
 
305
      privilege. Requested privileges that are granted, if any, are saved
 
306
      in save_priv.
 
307
  */
 
308
  virtual ACL_internal_access_result check(ulong want_access,
 
309
                                           ulong *save_priv) const= 0;
 
310
};
 
311
 
 
312
/**
 
313
  Per internal schema ACL access rules.
 
314
  This class is an interface.
 
315
  Each per schema specific access rule should be implemented
 
316
  in a different subclass, and registered.
 
317
  Per schema access rules can control:
 
318
  - every schema privileges on schema.*
 
319
  - every table privileges on schema.table
 
320
  @sa ACL_internal_schema_registry
 
321
*/
 
322
class ACL_internal_schema_access
 
323
{
 
324
public:
 
325
  ACL_internal_schema_access()
 
326
  {}
 
327
 
 
328
  virtual ~ACL_internal_schema_access()
 
329
  {}
 
330
 
 
331
  /**
 
332
    Check access to an internal schema.
 
333
    @param want_access the privileges requested
 
334
    @param [in, out] save_priv the privileges granted
 
335
    @return
 
336
      @retval ACL_INTERNAL_ACCESS_GRANTED All the requested privileges
 
337
      are granted, and saved in save_priv.
 
338
      @retval ACL_INTERNAL_ACCESS_DENIED At least one of the requested
 
339
      privileges was denied.
 
340
      @retval ACL_INTERNAL_ACCESS_CHECK_GRANT No requested privilege
 
341
      was denied, and grant should be checked for at least one
 
342
      privilege. Requested privileges that are granted, if any, are saved
 
343
      in save_priv.
 
344
  */
 
345
  virtual ACL_internal_access_result check(ulong want_access,
 
346
                                           ulong *save_priv) const= 0;
 
347
 
 
348
  /**
 
349
    Search for per table ACL access rules by table name.
 
350
    @param name the table name
 
351
    @return per table access rules, or NULL
 
352
  */
 
353
  virtual const ACL_internal_table_access *lookup(const char *name) const= 0;
 
354
};
 
355
 
 
356
/**
 
357
  A registry for per internal schema ACL.
 
358
  An 'internal schema' is a database schema maintained by the
 
359
  server implementation, such as 'performance_schema' and 'INFORMATION_SCHEMA'.
 
360
*/
 
361
class ACL_internal_schema_registry
 
362
{
 
363
public:
 
364
  static void register_schema(const LEX_STRING *name,
 
365
                              const ACL_internal_schema_access *access);
 
366
  static const ACL_internal_schema_access *lookup(const char *name);
 
367
};
 
368
 
 
369
const ACL_internal_schema_access *
 
370
get_cached_schema_access(GRANT_INTERNAL_INFO *grant_internal_info,
 
371
                         const char *schema_name);
 
372
 
 
373
const ACL_internal_table_access *
 
374
get_cached_table_access(GRANT_INTERNAL_INFO *grant_internal_info,
 
375
                        const char *schema_name,
 
376
                        const char *table_name);
 
377
 
 
378
bool acl_check_proxy_grant_access (THD *thd, const char *host, const char *user,
 
379
                                   bool with_grant);
 
380
#endif /* SQL_ACL_INCLUDED */