sql_acl.cc 108 KB
Newer Older
1
/* Copyright (C) 2000-2003 MySQL AB
unknown's avatar
unknown committed
2

unknown's avatar
unknown committed
3 4 5 6
   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; either version 2 of the License, or
   (at your option) any later version.
unknown's avatar
unknown committed
7

unknown's avatar
unknown committed
8 9 10 11
   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.
unknown's avatar
unknown committed
12

unknown's avatar
unknown committed
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
   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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */


/*
  The privileges are saved in the following tables:
  mysql/user	 ; super user who are allowed to do almoust anything
  mysql/host	 ; host priviliges. This is used if host is empty in mysql/db.
  mysql/db	 ; database privileges / user

  data in tables is sorted according to how many not-wild-cards there is
  in the relevant fields. Empty strings comes last.
*/

#include "mysql_priv.h"
#include "hash_filo.h"
30 31 32
#ifdef HAVE_REPLICATION
#include "sql_repl.h" //for tables_ok()
#endif
unknown's avatar
unknown committed
33 34 35
#include <m_ctype.h>
#include <stdarg.h>

unknown's avatar
SCRUM:  
unknown committed
36
#ifndef NO_EMBEDDED_ACCESS_CHECKS
37

unknown's avatar
unknown committed
38 39 40
class acl_entry :public hash_filo_element
{
public:
unknown's avatar
unknown committed
41
  ulong access;
unknown's avatar
unknown committed
42 43 44 45
  uint16 length;
  char key[1];					// Key will be stored here
};

unknown's avatar
unknown committed
46

unknown's avatar
unknown committed
47 48 49 50 51 52 53
static byte* acl_entry_get_key(acl_entry *entry,uint *length,
			       my_bool not_used __attribute__((unused)))
{
  *length=(uint) entry->length;
  return (byte*) entry->key;
}

unknown's avatar
unknown committed
54
#define IP_ADDR_STRLEN (3+1+3+1+3+1+3)
55
#define ACL_KEY_LENGTH (IP_ADDR_STRLEN+1+NAME_LEN+1+USERNAME_LENGTH+1)
unknown's avatar
unknown committed
56 57 58 59 60

static DYNAMIC_ARRAY acl_hosts,acl_users,acl_dbs;
static MEM_ROOT mem, memex;
static bool initialized=0;
static bool allow_all_hosts=1;
61
static HASH acl_check_hosts, column_priv_hash;
unknown's avatar
unknown committed
62 63 64
static DYNAMIC_ARRAY acl_wild_hosts;
static hash_filo *acl_cache;
static uint grant_version=0;
unknown's avatar
unknown committed
65
static uint priv_version=0; /* Version of priv tables. incremented by acl_init */
unknown's avatar
unknown committed
66
static ulong get_access(TABLE *form,uint fieldnr, uint *next_field=0);
unknown's avatar
unknown committed
67 68 69 70 71
static int acl_compare(ACL_ACCESS *a,ACL_ACCESS *b);
static ulong get_sort(uint count,...);
static void init_check_host(void);
static ACL_USER *find_acl_user(const char *host, const char *user);
static bool update_user_table(THD *thd, const char *host, const char *user,
72
			      const char *new_password, uint new_password_len);
unknown's avatar
unknown committed
73
static void update_hostname(acl_host_and_ip *host, const char *hostname);
74
static bool compare_hostname(const acl_host_and_ip *host,const char *hostname,
unknown's avatar
unknown committed
75 76
			     const char *ip);

77 78 79 80 81 82 83 84 85 86 87 88 89 90
/*
  Convert scrambled password to binary form, according to scramble type, 
  Binary form is stored in user.salt.
*/

static
void
set_user_salt(ACL_USER *acl_user, const char *password, uint password_len)
{
  if (password_len == SCRAMBLED_PASSWORD_CHAR_LENGTH)
  {
    get_salt_from_password(acl_user->salt, password);
    acl_user->salt_len= SCRAMBLE_LENGTH;
  }
91
  else if (password_len == SCRAMBLED_PASSWORD_CHAR_LENGTH_323)
92 93
  {
    get_salt_from_password_323((ulong *) acl_user->salt, password);
94
    acl_user->salt_len= SCRAMBLE_LENGTH_323;
95 96 97 98 99
  }
  else
    acl_user->salt_len= 0;
}

100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
/*
  This after_update function is used when user.password is less than
  SCRAMBLE_LENGTH bytes.
*/

static void restrict_update_of_old_passwords_var(THD *thd,
                                                 enum_var_type var_type)
{
  if (var_type == OPT_GLOBAL)
  {
    pthread_mutex_lock(&LOCK_global_system_variables);
    global_system_variables.old_passwords= 1;
    pthread_mutex_unlock(&LOCK_global_system_variables);
  }
  else
    thd->variables.old_passwords= 1;
}

118

119 120 121 122 123
/*
  Read grant privileges from the privilege tables in the 'mysql' database.

  SYNOPSIS
    acl_init()
unknown's avatar
unknown committed
124
    thd				Thread handler
125 126 127 128 129 130 131 132
    dont_read_acl_tables	Set to 1 if run with --skip-grant

  RETURN VALUES
    0	ok
    1	Could not initialize grant's
*/


unknown's avatar
unknown committed
133
my_bool acl_init(THD *org_thd, bool dont_read_acl_tables)
unknown's avatar
unknown committed
134
{
unknown's avatar
unknown committed
135
  THD  *thd;
unknown's avatar
unknown committed
136 137 138
  TABLE_LIST tables[3];
  TABLE *table;
  READ_RECORD read_record_info;
139 140
  MYSQL_LOCK *lock;
  my_bool return_val=1;
unknown's avatar
SCRUM  
unknown committed
141 142
  bool check_no_resolve= specialflag & SPECIAL_NO_RESOLVE;

unknown's avatar
unknown committed
143 144 145 146 147
  DBUG_ENTER("acl_init");

  if (!acl_cache)
    acl_cache=new hash_filo(ACL_CACHE_SIZE,0,0,
			    (hash_get_key) acl_entry_get_key,
unknown's avatar
unknown committed
148
			    (hash_free_key) free, system_charset_info);
unknown's avatar
unknown committed
149
  if (dont_read_acl_tables)
150
  {
unknown's avatar
unknown committed
151
    DBUG_RETURN(0); /* purecov: tested */
unknown's avatar
unknown committed
152 153
  }

154
  priv_version++; /* Privileges updated */
unknown's avatar
unknown committed
155

156 157 158
  /*
    To be able to run this from boot, we allocate a temporary THD
  */
unknown's avatar
unknown committed
159 160
  if (!(thd=new THD))
    DBUG_RETURN(1); /* purecov: inspected */
161 162
  thd->store_globals();

unknown's avatar
unknown committed
163
  acl_cache->clear(1);				// Clear locked hostname cache
unknown's avatar
unknown committed
164 165
  thd->db= my_strdup("mysql",MYF(0));
  thd->db_length=5;				// Safety
unknown's avatar
unknown committed
166
  bzero((char*) &tables,sizeof(tables));
167 168 169
  tables[0].alias=tables[0].real_name=(char*) "host";
  tables[1].alias=tables[1].real_name=(char*) "user";
  tables[2].alias=tables[2].real_name=(char*) "db";
unknown's avatar
unknown committed
170 171 172 173 174
  tables[0].next=tables+1;
  tables[1].next=tables+2;
  tables[0].lock_type=tables[1].lock_type=tables[2].lock_type=TL_READ;
  tables[0].db=tables[1].db=tables[2].db=thd->db;

unknown's avatar
unknown committed
175 176
  uint counter;
  if (open_tables(thd, tables, &counter))
unknown's avatar
unknown committed
177 178 179
  {
    sql_print_error("Fatal error: Can't open privilege tables: %s",
		    thd->net.last_error);
180
    goto end;
unknown's avatar
unknown committed
181
  }
unknown's avatar
unknown committed
182 183 184 185
  TABLE *ptr[3];				// Lock tables for quick update
  ptr[0]= tables[0].table;
  ptr[1]= tables[1].table;
  ptr[2]= tables[2].table;
186
  if (!(lock=mysql_lock_tables(thd,ptr,3)))
unknown's avatar
unknown committed
187 188 189
  {
    sql_print_error("Fatal error: Can't lock privilege tables: %s",
		    thd->net.last_error);
190
    goto end;
unknown's avatar
unknown committed
191
  }
192
  init_sql_alloc(&mem, ACL_ALLOC_BLOCK_SIZE, 0);
unknown's avatar
unknown committed
193
  init_read_record(&read_record_info,thd,table= tables[0].table,NULL,1,0);
194
  VOID(my_init_dynamic_array(&acl_hosts,sizeof(ACL_HOST),20,50));
unknown's avatar
unknown committed
195 196 197
  while (!(read_record_info.read_record(&read_record_info)))
  {
    ACL_HOST host;
198 199
    update_hostname(&host.host,get_field(&mem, table->field[0]));
    host.db=	 get_field(&mem, table->field[1]);
unknown's avatar
unknown committed
200 201
    host.access= get_access(table,2);
    host.access= fix_rights_for_db(host.access);
202
    host.sort=	 get_sort(2,host.host.hostname,host.db);
unknown's avatar
SCRUM  
unknown committed
203 204
    if (check_no_resolve && hostname_requires_resolving(host.host.hostname))
    {
unknown's avatar
unknown committed
205
      sql_print_warning("'host' entry '%s|%s' "
unknown's avatar
SCRUM  
unknown committed
206
		      "ignored in --skip-name-resolve mode.",
unknown's avatar
SCRUM  
unknown committed
207 208 209
		      host.host.hostname, host.db, host.host.hostname);
      continue;
    }
unknown's avatar
unknown committed
210 211 212 213
#ifndef TO_BE_REMOVED
    if (table->fields ==  8)
    {						// Without grant
      if (host.access & CREATE_ACL)
214
	host.access|=REFERENCES_ACL | INDEX_ACL | ALTER_ACL | CREATE_TMP_ACL;
unknown's avatar
unknown committed
215 216 217 218 219 220 221 222 223 224
    }
#endif
    VOID(push_dynamic(&acl_hosts,(gptr) &host));
  }
  qsort((gptr) dynamic_element(&acl_hosts,0,ACL_HOST*),acl_hosts.elements,
	sizeof(ACL_HOST),(qsort_cmp) acl_compare);
  end_read_record(&read_record_info);
  freeze_size(&acl_hosts);

  init_read_record(&read_record_info,thd,table=tables[1].table,NULL,1,0);
225
  VOID(my_init_dynamic_array(&acl_users,sizeof(ACL_USER),50,100));
226
  if (table->field[2]->field_length < SCRAMBLED_PASSWORD_CHAR_LENGTH_323)
unknown's avatar
unknown committed
227
  {
228 229 230
    sql_print_error("Fatal error: mysql.user table is damaged or in "
                    "unsupported 3.20 format.");
    goto end;
unknown's avatar
unknown committed
231 232
  }

233 234
  DBUG_PRINT("info",("user table fields: %d, password length: %d",
		     table->fields, table->field[2]->field_length));
235 236 237
  
  pthread_mutex_lock(&LOCK_global_system_variables);
  if (table->field[2]->field_length < SCRAMBLED_PASSWORD_CHAR_LENGTH)
238
  {
239 240 241 242 243 244 245 246 247 248 249 250 251 252
    if (opt_secure_auth)
    {
      pthread_mutex_unlock(&LOCK_global_system_variables);
      sql_print_error("Fatal error: mysql.user table is in old format, "
                      "but server started with --secure-auth option.");
      goto end;
    }
    sys_old_passwords.after_update= restrict_update_of_old_passwords_var;
    if (global_system_variables.old_passwords)
      pthread_mutex_unlock(&LOCK_global_system_variables);
    else
    {
      global_system_variables.old_passwords= 1;
      pthread_mutex_unlock(&LOCK_global_system_variables);
253 254 255
      sql_print_warning("mysql.user table is not updated to new password format; "
                        "Disabling new password usage until "
                        "mysql_fix_privilege_tables is run");
256 257 258 259
    }
    thd->variables.old_passwords= 1;
  }
  else
260
  {
261 262
    sys_old_passwords.after_update= 0;
    pthread_mutex_unlock(&LOCK_global_system_variables);
263 264
  }

unknown's avatar
unknown committed
265 266 267 268
  allow_all_hosts=0;
  while (!(read_record_info.read_record(&read_record_info)))
  {
    ACL_USER user;
269 270
    update_hostname(&user.host, get_field(&mem, table->field[0]));
    user.user= get_field(&mem, table->field[1]);
unknown's avatar
SCRUM  
unknown committed
271 272
    if (check_no_resolve && hostname_requires_resolving(user.host.hostname))
    {
unknown's avatar
unknown committed
273 274
      sql_print_warning("'user' entry '%s@%s' "
                        "ignored in --skip-name-resolve mode.",
unknown's avatar
SCRUM  
unknown committed
275 276 277 278
		      user.user, user.host.hostname, user.host.hostname);
      continue;
    }

279 280 281 282
    const char *password= get_field(&mem, table->field[2]);
    uint password_len= password ? strlen(password) : 0;
    set_user_salt(&user, password, password_len);
    if (user.salt_len == 0 && password_len != 0)
unknown's avatar
unknown committed
283
    {
284 285
      switch (password_len) {
      case 45: /* 4.1: to be removed */
unknown's avatar
unknown committed
286 287 288 289 290
        sql_print_warning("Found 4.1 style password for user '%s@%s'. "
                          "Ignoring user. "
                          "You should change password for this user.",
                          user.user ? user.user : "",
                          user.host.hostname ? user.host.hostname : "");
291 292
        break;
      default:
unknown's avatar
unknown committed
293 294 295
        sql_print_warning("Found invalid password for user: '%s@%s'; "
                          "Ignoring user", user.user ? user.user : "",
                           user.host.hostname ? user.host.hostname : "");
296 297
        break;
      }
unknown's avatar
unknown committed
298
    }
299
    else                                        // password is correct
unknown's avatar
unknown committed
300
    {
unknown's avatar
unknown committed
301 302
      uint next_field;
      user.access= get_access(table,3,&next_field) & GLOBAL_ACLS;
303 304 305
      user.sort= get_sort(2,user.host.hostname,user.user);
      user.hostname_length= (user.host.hostname ?
                             (uint) strlen(user.host.hostname) : 0);
306
      if (table->fields >= 31)	 /* Starting from 4.0.2 we have more fields */
307
      {
unknown's avatar
unknown committed
308
        char *ssl_type=get_field(&mem, table->field[next_field++]);
309 310 311 312 313 314 315 316 317
        if (!ssl_type)
          user.ssl_type=SSL_TYPE_NONE;
        else if (!strcmp(ssl_type, "ANY"))
          user.ssl_type=SSL_TYPE_ANY;
        else if (!strcmp(ssl_type, "X509"))
          user.ssl_type=SSL_TYPE_X509;
        else  /* !strcmp(ssl_type, "SPECIFIED") */
          user.ssl_type=SSL_TYPE_SPECIFIED;

unknown's avatar
unknown committed
318 319 320
        user.ssl_cipher=   get_field(&mem, table->field[next_field++]);
        user.x509_issuer=  get_field(&mem, table->field[next_field++]);
        user.x509_subject= get_field(&mem, table->field[next_field++]);
321

unknown's avatar
unknown committed
322 323 324 325 326 327
        char *ptr = get_field(&mem, table->field[next_field++]);
        user.user_resource.questions=ptr ? atoi(ptr) : 0;
        ptr = get_field(&mem, table->field[next_field++]);
        user.user_resource.updates=ptr ? atoi(ptr) : 0;
        ptr = get_field(&mem, table->field[next_field++]);
        user.user_resource.connections=ptr ? atoi(ptr) : 0;
328 329 330
        if (user.user_resource.questions || user.user_resource.updates ||
            user.user_resource.connections)
          mqh_used=1;
331
      }
332 333 334
      else
      {
        user.ssl_type=SSL_TYPE_NONE;
335
        bzero((char *)&(user.user_resource),sizeof(user.user_resource));
unknown's avatar
unknown committed
336
#ifndef TO_BE_REMOVED
337 338 339 340 341 342 343 344 345 346 347
        if (table->fields <= 13)
        {						// Without grant
          if (user.access & CREATE_ACL)
            user.access|=REFERENCES_ACL | INDEX_ACL | ALTER_ACL;
        }
        /* Convert old privileges */
        user.access|= LOCK_TABLES_ACL | CREATE_TMP_ACL | SHOW_DB_ACL;
        if (user.access & FILE_ACL)
          user.access|= REPL_CLIENT_ACL | REPL_SLAVE_ACL;
        if (user.access & PROCESS_ACL)
          user.access|= SUPER_ACL | EXECUTE_ACL;
unknown's avatar
unknown committed
348
#endif
349 350 351 352 353
      }
      VOID(push_dynamic(&acl_users,(gptr) &user));
      if (!user.host.hostname || user.host.hostname[0] == wild_many &&
          !user.host.hostname[1])
        allow_all_hosts=1;			// Anyone can connect
unknown's avatar
unknown committed
354
    }
unknown's avatar
unknown committed
355 356 357 358 359
  }
  qsort((gptr) dynamic_element(&acl_users,0,ACL_USER*),acl_users.elements,
	sizeof(ACL_USER),(qsort_cmp) acl_compare);
  end_read_record(&read_record_info);
  freeze_size(&acl_users);
unknown's avatar
unknown committed
360

unknown's avatar
unknown committed
361
  init_read_record(&read_record_info,thd,table=tables[2].table,NULL,1,0);
362
  VOID(my_init_dynamic_array(&acl_dbs,sizeof(ACL_DB),50,100));
unknown's avatar
unknown committed
363 364 365
  while (!(read_record_info.read_record(&read_record_info)))
  {
    ACL_DB db;
366 367
    update_hostname(&db.host,get_field(&mem, table->field[0]));
    db.db=get_field(&mem, table->field[1]);
368 369
    if (!db.db)
    {
unknown's avatar
unknown committed
370
      sql_print_warning("Found an entry in the 'db' table with empty database name; Skipped");
371
      continue;
372
    }
373
    db.user=get_field(&mem, table->field[2]);
unknown's avatar
SCRUM  
unknown committed
374 375
    if (check_no_resolve && hostname_requires_resolving(db.host.hostname))
    {
unknown's avatar
unknown committed
376 377 378
      sql_print_warning("'db' entry '%s %s@%s' "
		        "ignored in --skip-name-resolve mode.",
		        db.db, db.user, db.host.hostname, db.host.hostname);
unknown's avatar
SCRUM  
unknown committed
379 380
      continue;
    }
unknown's avatar
unknown committed
381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399
    db.access=get_access(table,3);
    db.access=fix_rights_for_db(db.access);
    db.sort=get_sort(3,db.host.hostname,db.db,db.user);
#ifndef TO_BE_REMOVED
    if (table->fields <=  9)
    {						// Without grant
      if (db.access & CREATE_ACL)
	db.access|=REFERENCES_ACL | INDEX_ACL | ALTER_ACL;
    }
#endif
    VOID(push_dynamic(&acl_dbs,(gptr) &db));
  }
  qsort((gptr) dynamic_element(&acl_dbs,0,ACL_DB*),acl_dbs.elements,
	sizeof(ACL_DB),(qsort_cmp) acl_compare);
  end_read_record(&read_record_info);
  freeze_size(&acl_dbs);
  init_check_host();

  mysql_unlock_tables(thd, lock);
400
  initialized=1;
unknown's avatar
unknown committed
401
  thd->version--;				// Force close to free memory
402 403 404
  return_val=0;

end:
unknown's avatar
unknown committed
405 406
  close_thread_tables(thd);
  delete thd;
407 408
  if (org_thd)
    org_thd->store_globals();			/* purecov: inspected */
unknown's avatar
unknown committed
409 410 411 412 413
  else
  {
    /* Remember that we don't have a THD */
    my_pthread_setspecific_ptr(THR_THD,  0);
  }
414
  DBUG_RETURN(return_val);
unknown's avatar
unknown committed
415 416 417 418 419
}


void acl_free(bool end)
{
420
  free_root(&mem,MYF(0));
unknown's avatar
unknown committed
421 422 423 424 425 426 427 428 429 430 431 432 433 434
  delete_dynamic(&acl_hosts);
  delete_dynamic(&acl_users);
  delete_dynamic(&acl_dbs);
  delete_dynamic(&acl_wild_hosts);
  hash_free(&acl_check_hosts);
  if (!end)
    acl_cache->clear(1); /* purecov: inspected */
  else
  {
    delete acl_cache;
    acl_cache=0;
  }
}

435 436 437 438 439 440 441 442

/*
  Forget current privileges and read new privileges from the privilege tables

  SYNOPSIS
    acl_reload()
    thd			Thread handle
*/
unknown's avatar
unknown committed
443

444
void acl_reload(THD *thd)
unknown's avatar
unknown committed
445 446 447 448 449 450
{
  DYNAMIC_ARRAY old_acl_hosts,old_acl_users,old_acl_dbs;
  MEM_ROOT old_mem;
  bool old_initialized;
  DBUG_ENTER("acl_reload");

451
  if (thd && thd->locked_tables)
unknown's avatar
unknown committed
452
  {					// Can't have locked tables here
453 454 455
    thd->lock=thd->locked_tables;
    thd->locked_tables=0;
    close_thread_tables(thd);
unknown's avatar
unknown committed
456 457 458 459 460 461 462 463 464 465 466
  }
  if ((old_initialized=initialized))
    VOID(pthread_mutex_lock(&acl_cache->lock));

  old_acl_hosts=acl_hosts;
  old_acl_users=acl_users;
  old_acl_dbs=acl_dbs;
  old_mem=mem;
  delete_dynamic(&acl_wild_hosts);
  hash_free(&acl_check_hosts);

unknown's avatar
unknown committed
467
  if (acl_init(thd, 0))
unknown's avatar
unknown committed
468
  {					// Error. Revert to old list
469
    DBUG_PRINT("error",("Reverting to old privileges"));
470
    acl_free();				/* purecov: inspected */
unknown's avatar
unknown committed
471 472 473 474 475 476 477 478
    acl_hosts=old_acl_hosts;
    acl_users=old_acl_users;
    acl_dbs=old_acl_dbs;
    mem=old_mem;
    init_check_host();
  }
  else
  {
479
    free_root(&old_mem,MYF(0));
unknown's avatar
unknown committed
480 481 482 483 484 485 486 487 488 489
    delete_dynamic(&old_acl_hosts);
    delete_dynamic(&old_acl_users);
    delete_dynamic(&old_acl_dbs);
  }
  if (old_initialized)
    VOID(pthread_mutex_unlock(&acl_cache->lock));
  DBUG_VOID_RETURN;
}


unknown's avatar
unknown committed
490 491
/*
  Get all access bits from table after fieldnr
unknown's avatar
unknown committed
492 493 494 495 496 497 498 499 500 501 502 503 504 505 506

  IMPLEMENTATION
    We know that the access privileges ends when there is no more fields
    or the field is not an enum with two elements.

  SYNOPSIS
    get_access()
    form        an open table to read privileges from.
                The record should be already read in table->record[0]
    fieldnr     number of the first privilege (that is ENUM('N','Y') field
    next_field  on return - number of the field next to the last ENUM
                (unless next_field == 0)

  RETURN VALUE
    privilege mask
unknown's avatar
unknown committed
507
*/
unknown's avatar
unknown committed
508

unknown's avatar
unknown committed
509
static ulong get_access(TABLE *form, uint fieldnr, uint *next_field)
unknown's avatar
unknown committed
510
{
unknown's avatar
unknown committed
511
  ulong access_bits=0,bit;
unknown's avatar
unknown committed
512
  char buff[2];
unknown's avatar
unknown committed
513
  String res(buff,sizeof(buff),&my_charset_latin1);
unknown's avatar
unknown committed
514 515
  Field **pos;

unknown's avatar
unknown committed
516 517 518
  for (pos=form->field+fieldnr, bit=1;
       *pos && (*pos)->real_type() == FIELD_TYPE_ENUM &&
	 ((Field_enum*) (*pos))->typelib->count == 2 ;
unknown's avatar
unknown committed
519
       pos++, fieldnr++, bit<<=1)
unknown's avatar
unknown committed
520
  {
521
    (*pos)->val_str(&res);
unknown's avatar
unknown committed
522
    if (my_toupper(&my_charset_latin1, res[0]) == 'Y')
unknown's avatar
unknown committed
523
      access_bits|= bit;
unknown's avatar
unknown committed
524
  }
unknown's avatar
unknown committed
525 526
  if (next_field)
    *next_field=fieldnr;
unknown's avatar
unknown committed
527 528 529 530 531
  return access_bits;
}


/*
unknown's avatar
unknown committed
532 533 534 535 536
  Return a number which, if sorted 'desc', puts strings in this order:
    no wildcards
    wildcards
    empty string
*/
unknown's avatar
unknown committed
537 538 539 540 541 542 543

static ulong get_sort(uint count,...)
{
  va_list args;
  va_start(args,count);
  ulong sort=0;

544 545 546
  /* Should not use this function with more than 4 arguments for compare. */
  DBUG_ASSERT(count <= 4);

unknown's avatar
unknown committed
547 548
  while (count--)
  {
549 550 551
    char *start, *str= va_arg(args,char*);
    uint chars= 0;
    uint wild_pos= 0;           /* first wildcard position */
unknown's avatar
unknown committed
552

unknown's avatar
unknown committed
553
    if ((start= str))
unknown's avatar
unknown committed
554 555 556 557
    {
      for (; *str ; str++)
      {
	if (*str == wild_many || *str == wild_one || *str == wild_prefix)
558
        {
unknown's avatar
unknown committed
559
          wild_pos= (uint) (str - start) + 1;
560 561
          break;
        }
unknown's avatar
unknown committed
562
        chars= 128;                             // Marker that chars existed
unknown's avatar
unknown committed
563 564
      }
    }
unknown's avatar
unknown committed
565
    sort= (sort << 8) + (wild_pos ? min(wild_pos, 127) : chars);
unknown's avatar
unknown committed
566 567 568 569 570 571 572 573 574 575 576 577 578 579 580
  }
  va_end(args);
  return sort;
}


static int acl_compare(ACL_ACCESS *a,ACL_ACCESS *b)
{
  if (a->sort > b->sort)
    return -1;
  if (a->sort < b->sort)
    return 1;
  return 0;
}

581

582
/*
unknown's avatar
unknown committed
583 584
  Seek ACL entry for a user, check password, SSL cypher, and if
  everything is OK, update THD user data and USER_RESOURCES struct.
585

unknown's avatar
unknown committed
586 587 588 589
  IMPLEMENTATION
   This function does not check if the user has any sensible privileges:
   only user's existence and  validity is checked.
   Note, that entire operation is protected by acl_cache_lock.
unknown's avatar
unknown committed
590

591
  SYNOPSIS
592 593 594 595 596 597 598 599 600 601 602 603 604 605
    acl_getroot()
    thd         thread handle. If all checks are OK,
                thd->priv_user, thd->master_access are updated.
                thd->host, thd->ip, thd->user are used for checks.
    mqh         user resources; on success mqh is reset, else
                unchanged
    passwd      scrambled & crypted password, recieved from client
                (to check): thd->scramble or thd->scramble_323 is
                used to decrypt passwd, so they must contain
                original random string,
    passwd_len  length of passwd, must be one of 0, 8,
                SCRAMBLE_LENGTH_323, SCRAMBLE_LENGTH
    'thd' and 'mqh' are updated on success; other params are IN.
  
unknown's avatar
unknown committed
606
  RETURN VALUE
607 608
    0  success: thd->priv_user, thd->priv_host, thd->master_access, mqh are
       updated
609
    1  user not found or authentification failure
unknown's avatar
unknown committed
610
    2  user found, has long (4.1.1) salt, but passwd is in old (3.23) format.
611
   -1  user found, has short (3.23) salt, but passwd is in new (4.1.1) format.
unknown's avatar
unknown committed
612 613
*/

614 615
int acl_getroot(THD *thd, USER_RESOURCES  *mqh,
                const char *passwd, uint passwd_len)
unknown's avatar
unknown committed
616
{
unknown's avatar
merge  
unknown committed
617 618 619
  ulong user_access= NO_ACCESS;
  int res= 1;
  ACL_USER *acl_user= 0;
620
  DBUG_ENTER("acl_getroot");
unknown's avatar
unknown committed
621 622

  if (!initialized)
623
  {
624 625 626 627 628 629
    /* 
      here if mysqld's been started with --skip-grant-tables option.
    */
    thd->priv_user= (char *) "";                // privileges for
    *thd->priv_host= '\0';                      // the user are unknown
    thd->master_access= ~NO_ACCESS;             // everything is allowed
unknown's avatar
merge  
unknown committed
630
    bzero((char*) mqh, sizeof(*mqh));
631
    DBUG_RETURN(0);
632
  }
633

unknown's avatar
unknown committed
634
  VOID(pthread_mutex_lock(&acl_cache->lock));
unknown's avatar
unknown committed
635

unknown's avatar
unknown committed
636
  /*
637 638 639
    Find acl entry in user database. Note, that find_acl_user is not the same,
    because it doesn't take into account the case when user is not empty,
    but acl_user->user is empty
unknown's avatar
unknown committed
640
  */
unknown's avatar
unknown committed
641

642
  for (uint i=0 ; i < acl_users.elements ; i++)
643
  {
unknown's avatar
unknown committed
644 645
    ACL_USER *acl_user_tmp= dynamic_element(&acl_users,i,ACL_USER*);
    if (!acl_user_tmp->user || !strcmp(thd->user, acl_user_tmp->user))
unknown's avatar
unknown committed
646
    {
unknown's avatar
unknown committed
647
      if (compare_hostname(&acl_user_tmp->host, thd->host, thd->ip))
unknown's avatar
unknown committed
648
      {
649
        /* check password: it should be empty or valid */
unknown's avatar
unknown committed
650
        if (passwd_len == acl_user_tmp->salt_len)
unknown's avatar
unknown committed
651
        {
unknown's avatar
unknown committed
652
          if (acl_user_tmp->salt_len == 0 ||
unknown's avatar
cleanup  
unknown committed
653 654
              (acl_user_tmp->salt_len == SCRAMBLE_LENGTH ?
              check_scramble(passwd, thd->scramble, acl_user_tmp->salt) :
655
              check_scramble_323(passwd, thd->scramble,
unknown's avatar
cleanup  
unknown committed
656
                                 (ulong *) acl_user_tmp->salt)) == 0)
657
          {
unknown's avatar
unknown committed
658
            acl_user= acl_user_tmp;
659 660
            res= 0;
          }
unknown's avatar
unknown committed
661
        }
662
        else if (passwd_len == SCRAMBLE_LENGTH &&
unknown's avatar
unknown committed
663
                 acl_user_tmp->salt_len == SCRAMBLE_LENGTH_323)
664
          res= -1;
unknown's avatar
unknown committed
665
        else if (passwd_len == SCRAMBLE_LENGTH_323 &&
unknown's avatar
unknown committed
666
                 acl_user_tmp->salt_len == SCRAMBLE_LENGTH)
unknown's avatar
unknown committed
667
          res= 2;
668 669
        /* linear search complete: */
        break;
unknown's avatar
unknown committed
670
      }
unknown's avatar
unknown committed
671
    }
672
  }
673 674 675 676
  /*
    This was moved to separate tree because of heavy HAVE_OPENSSL case.
    If acl_user is not null, res is 0.
  */
unknown's avatar
unknown committed
677 678 679

  if (acl_user)
  {
680
    /* OK. User found and password checked continue validation */
681
#ifdef HAVE_OPENSSL
unknown's avatar
unknown committed
682
    Vio *vio=thd->net.vio;
unknown's avatar
unknown committed
683
    SSL *ssl= (SSL*) vio->ssl_arg;
684
#endif
unknown's avatar
merge  
unknown committed
685

686
    /*
unknown's avatar
unknown committed
687
      At this point we know that user is allowed to connect
688 689 690 691 692 693
      from given host by given username/password pair. Now
      we check if SSL is required, if user is using SSL and
      if X509 certificate attributes are OK
    */
    switch (acl_user->ssl_type) {
    case SSL_TYPE_NOT_SPECIFIED:		// Impossible
unknown's avatar
merge  
unknown committed
694 695
    case SSL_TYPE_NONE:				// SSL is not required
      user_access= acl_user->access;
696
      break;
697
#ifdef HAVE_OPENSSL
unknown's avatar
merge  
unknown committed
698
    case SSL_TYPE_ANY:				// Any kind of SSL is ok
699
      if (vio_type(vio) == VIO_TYPE_SSL)
unknown's avatar
merge  
unknown committed
700
	user_access= acl_user->access;
701 702 703 704 705
      break;
    case SSL_TYPE_X509: /* Client should have any valid certificate. */
      /*
	Connections with non-valid certificates are dropped already
	in sslaccept() anyway, so we do not check validity here.
unknown's avatar
merge  
unknown committed
706

unknown's avatar
unknown committed
707 708
	We need to check for absence of SSL because without SSL
	we should reject connection.
709
      */
unknown's avatar
unknown committed
710
      if (vio_type(vio) == VIO_TYPE_SSL &&
unknown's avatar
unknown committed
711 712
	  SSL_get_verify_result(ssl) == X509_V_OK &&
	  SSL_get_peer_certificate(ssl))
unknown's avatar
merge  
unknown committed
713
	user_access= acl_user->access;
714 715 716 717 718 719 720 721
      break;
    case SSL_TYPE_SPECIFIED: /* Client should have specified attrib */
      /*
	We do not check for absence of SSL because without SSL it does
	not pass all checks here anyway.
	If cipher name is specified, we compare it to actual cipher in
	use.
      */
unknown's avatar
unknown committed
722
      X509 *cert;
unknown's avatar
unknown committed
723
      if (vio_type(vio) != VIO_TYPE_SSL ||
unknown's avatar
unknown committed
724
	  SSL_get_verify_result(ssl) != X509_V_OK)
unknown's avatar
unknown committed
725
	break;
726
      if (acl_user->ssl_cipher)
unknown's avatar
unknown committed
727
      {
728
	DBUG_PRINT("info",("comparing ciphers: '%s' and '%s'",
unknown's avatar
unknown committed
729 730
			   acl_user->ssl_cipher,SSL_get_cipher(ssl)));
	if (!strcmp(acl_user->ssl_cipher,SSL_get_cipher(ssl)))
unknown's avatar
merge  
unknown committed
731
	  user_access= acl_user->access;
732 733
	else
	{
unknown's avatar
unknown committed
734
	  if (global_system_variables.log_warnings)
unknown's avatar
unknown committed
735 736 737
	    sql_print_information("X509 ciphers mismatch: should be '%s' but is '%s'",
			      acl_user->ssl_cipher,
			      SSL_get_cipher(ssl));
738 739
	  break;
	}
unknown's avatar
unknown committed
740
      }
741 742
      /* Prepare certificate (if exists) */
      DBUG_PRINT("info",("checkpoint 1"));
unknown's avatar
unknown committed
743 744 745 746 747
      if (!(cert= SSL_get_peer_certificate(ssl)))
      {
	user_access=NO_ACCESS;
	break;
      }
748 749 750
      DBUG_PRINT("info",("checkpoint 2"));
      /* If X509 issuer is speified, we check it... */
      if (acl_user->x509_issuer)
unknown's avatar
unknown committed
751
      {
unknown's avatar
unknown committed
752
        DBUG_PRINT("info",("checkpoint 3"));
753 754 755
	char *ptr = X509_NAME_oneline(X509_get_issuer_name(cert), 0, 0);
	DBUG_PRINT("info",("comparing issuers: '%s' and '%s'",
			   acl_user->x509_issuer, ptr));
unknown's avatar
unknown committed
756
        if (strcmp(acl_user->x509_issuer, ptr))
757
        {
unknown's avatar
unknown committed
758
          if (global_system_variables.log_warnings)
unknown's avatar
unknown committed
759 760
            sql_print_information("X509 issuer mismatch: should be '%s' "
			      "but is '%s'", acl_user->x509_issuer, ptr);
761
          free(ptr);
unknown's avatar
unknown committed
762
          break;
763
        }
unknown's avatar
merge  
unknown committed
764
        user_access= acl_user->access;
unknown's avatar
unknown committed
765
        free(ptr);
unknown's avatar
unknown committed
766
      }
767 768 769 770
      DBUG_PRINT("info",("checkpoint 4"));
      /* X509 subject is specified, we check it .. */
      if (acl_user->x509_subject)
      {
unknown's avatar
unknown committed
771 772 773 774
        char *ptr= X509_NAME_oneline(X509_get_subject_name(cert), 0, 0);
        DBUG_PRINT("info",("comparing subjects: '%s' and '%s'",
                           acl_user->x509_subject, ptr));
        if (strcmp(acl_user->x509_subject,ptr))
775
        {
unknown's avatar
unknown committed
776
          if (global_system_variables.log_warnings)
unknown's avatar
unknown committed
777
            sql_print_information("X509 subject mismatch: '%s' vs '%s'",
unknown's avatar
unknown committed
778
                            acl_user->x509_subject, ptr);
779
        }
unknown's avatar
unknown committed
780
        else
unknown's avatar
merge  
unknown committed
781
          user_access= acl_user->access;
unknown's avatar
unknown committed
782
        free(ptr);
783 784
      }
      break;
unknown's avatar
unknown committed
785
#else  /* HAVE_OPENSSL */
unknown's avatar
unknown committed
786
    default:
787
      /*
unknown's avatar
unknown committed
788 789 790
        If we don't have SSL but SSL is required for this user the 
        authentication should fail.
      */
791 792
      break;
#endif /* HAVE_OPENSSL */
unknown's avatar
unknown committed
793
    }
unknown's avatar
merge  
unknown committed
794
    thd->master_access= user_access;
795 796
    thd->priv_user= acl_user->user ? thd->user : (char *) "";
    *mqh= acl_user->user_resource;
797

798 799 800 801 802
    if (acl_user->host.hostname)
      strmake(thd->priv_host, acl_user->host.hostname, MAX_HOSTNAME);
    else
      *thd->priv_host= 0;
  }
unknown's avatar
unknown committed
803
  VOID(pthread_mutex_unlock(&acl_cache->lock));
804
  DBUG_RETURN(res);
unknown's avatar
unknown committed
805 806 807 808 809 810 811 812 813 814
}


static byte* check_get_key(ACL_USER *buff,uint *length,
			   my_bool not_used __attribute__((unused)))
{
  *length=buff->hostname_length;
  return (byte*) buff->host.hostname;
}

815

unknown's avatar
unknown committed
816
static void acl_update_user(const char *user, const char *host,
817
			    const char *password, uint password_len,
818 819 820 821
			    enum SSL_type ssl_type,
			    const char *ssl_cipher,
			    const char *x509_issuer,
			    const char *x509_subject,
unknown's avatar
unknown committed
822
			    USER_RESOURCES  *mqh,
unknown's avatar
unknown committed
823
			    ulong privileges)
unknown's avatar
unknown committed
824 825 826 827 828 829 830 831 832
{
  for (uint i=0 ; i < acl_users.elements ; i++)
  {
    ACL_USER *acl_user=dynamic_element(&acl_users,i,ACL_USER*);
    if (!acl_user->user && !user[0] ||
	acl_user->user &&
	!strcmp(user,acl_user->user))
    {
      if (!acl_user->host.hostname && !host[0] ||
unknown's avatar
unknown committed
833
	  acl_user->host.hostname &&
unknown's avatar
unknown committed
834
	  !my_strcasecmp(&my_charset_latin1, host, acl_user->host.hostname))
unknown's avatar
unknown committed
835 836
      {
	acl_user->access=privileges;
837
	if (mqh->bits & 1)
838
	  acl_user->user_resource.questions=mqh->questions;
839
	if (mqh->bits & 2)
840
	  acl_user->user_resource.updates=mqh->updates;
841
	if (mqh->bits & 4)
842
	  acl_user->user_resource.connections=mqh->connections;
843 844 845 846 847 848 849 850 851 852
	if (ssl_type != SSL_TYPE_NOT_SPECIFIED)
	{
	  acl_user->ssl_type= ssl_type;
	  acl_user->ssl_cipher= (ssl_cipher ? strdup_root(&mem,ssl_cipher) :
				 0);
	  acl_user->x509_issuer= (x509_issuer ? strdup_root(&mem,x509_issuer) :
				  0);
	  acl_user->x509_subject= (x509_subject ?
				   strdup_root(&mem,x509_subject) : 0);
	}
unknown's avatar
unknown committed
853 854
	if (password)
	  set_user_salt(acl_user, password, password_len);
855
        /* search complete: */
unknown's avatar
unknown committed
856 857 858 859 860 861 862 863
	break;
      }
    }
  }
}


static void acl_insert_user(const char *user, const char *host,
864
			    const char *password, uint password_len,
865 866 867 868
			    enum SSL_type ssl_type,
			    const char *ssl_cipher,
			    const char *x509_issuer,
			    const char *x509_subject,
869
			    USER_RESOURCES *mqh,
unknown's avatar
unknown committed
870
			    ulong privileges)
unknown's avatar
unknown committed
871 872
{
  ACL_USER acl_user;
873
  acl_user.user=*user ? strdup_root(&mem,user) : 0;
unknown's avatar
unknown committed
874
  update_hostname(&acl_user.host, *host ? strdup_root(&mem, host): 0);
unknown's avatar
unknown committed
875
  acl_user.access=privileges;
876
  acl_user.user_resource = *mqh;
unknown's avatar
unknown committed
877
  acl_user.sort=get_sort(2,acl_user.host.hostname,acl_user.user);
878
  acl_user.hostname_length=(uint) strlen(host);
879 880 881 882 883
  acl_user.ssl_type= (ssl_type != SSL_TYPE_NOT_SPECIFIED ?
		      ssl_type : SSL_TYPE_NONE);
  acl_user.ssl_cipher=	ssl_cipher   ? strdup_root(&mem,ssl_cipher) : 0;
  acl_user.x509_issuer= x509_issuer  ? strdup_root(&mem,x509_issuer) : 0;
  acl_user.x509_subject=x509_subject ? strdup_root(&mem,x509_subject) : 0;
884 885

  set_user_salt(&acl_user, password, password_len);
unknown's avatar
unknown committed
886 887 888 889

  VOID(push_dynamic(&acl_users,(gptr) &acl_user));
  if (!acl_user.host.hostname || acl_user.host.hostname[0] == wild_many
      && !acl_user.host.hostname[1])
unknown's avatar
unknown committed
890
    allow_all_hosts=1;		// Anyone can connect /* purecov: tested */
unknown's avatar
unknown committed
891 892 893 894 895 896 897 898 899 900 901
  qsort((gptr) dynamic_element(&acl_users,0,ACL_USER*),acl_users.elements,
	sizeof(ACL_USER),(qsort_cmp) acl_compare);

  /* We must free acl_check_hosts as its memory is mapped to acl_user */
  delete_dynamic(&acl_wild_hosts);
  hash_free(&acl_check_hosts);
  init_check_host();
}


static void acl_update_db(const char *user, const char *host, const char *db,
unknown's avatar
unknown committed
902
			  ulong privileges)
unknown's avatar
unknown committed
903 904 905 906 907 908 909 910 911
{
  for (uint i=0 ; i < acl_dbs.elements ; i++)
  {
    ACL_DB *acl_db=dynamic_element(&acl_dbs,i,ACL_DB*);
    if (!acl_db->user && !user[0] ||
	acl_db->user &&
	!strcmp(user,acl_db->user))
    {
      if (!acl_db->host.hostname && !host[0] ||
912
	  acl_db->host.hostname &&
unknown's avatar
unknown committed
913
	  !my_strcasecmp(&my_charset_latin1, host, acl_db->host.hostname))
unknown's avatar
unknown committed
914 915 916 917 918 919 920 921 922 923 924 925 926 927 928
      {
	if (!acl_db->db && !db[0] ||
	    acl_db->db && !strcmp(db,acl_db->db))
	{
	  if (privileges)
	    acl_db->access=privileges;
	  else
	    delete_dynamic_element(&acl_dbs,i);
	}
      }
    }
  }
}


929 930 931 932 933 934 935 936 937 938 939 940 941 942
/*
  Insert a user/db/host combination into the global acl_cache

  SYNOPSIS
    acl_insert_db()
    user		User name
    host		Host name
    db			Database name
    privileges		Bitmap of privileges

  NOTES
    acl_cache->lock must be locked when calling this
*/

unknown's avatar
unknown committed
943
static void acl_insert_db(const char *user, const char *host, const char *db,
unknown's avatar
unknown committed
944
			  ulong privileges)
unknown's avatar
unknown committed
945 946
{
  ACL_DB acl_db;
947
  safe_mutex_assert_owner(&acl_cache->lock);
unknown's avatar
unknown committed
948 949 950 951 952 953 954 955 956 957 958
  acl_db.user=strdup_root(&mem,user);
  update_hostname(&acl_db.host,strdup_root(&mem,host));
  acl_db.db=strdup_root(&mem,db);
  acl_db.access=privileges;
  acl_db.sort=get_sort(3,acl_db.host.hostname,acl_db.db,acl_db.user);
  VOID(push_dynamic(&acl_dbs,(gptr) &acl_db));
  qsort((gptr) dynamic_element(&acl_dbs,0,ACL_DB*),acl_dbs.elements,
	sizeof(ACL_DB),(qsort_cmp) acl_compare);
}


959 960 961 962

/*
  Get privilege for a host, user and db combination
*/
unknown's avatar
unknown committed
963

964
ulong acl_get(const char *host, const char *ip,
965
              const char *user, const char *db, my_bool db_is_pattern)
unknown's avatar
unknown committed
966
{
unknown's avatar
unknown committed
967 968
  ulong host_access,db_access;
  uint i,key_length;
unknown's avatar
unknown committed
969
  db_access=0; host_access= ~0;
unknown's avatar
unknown committed
970
  char key[ACL_KEY_LENGTH],*tmp_db,*end;
unknown's avatar
unknown committed
971
  acl_entry *entry;
unknown's avatar
unknown committed
972
  DBUG_ENTER("acl_get");
unknown's avatar
unknown committed
973 974

  VOID(pthread_mutex_lock(&acl_cache->lock));
975
  end=strmov((tmp_db=strmov(strmov(key, ip ? ip : "")+1,user)+1),db);
unknown's avatar
unknown committed
976 977
  if (lower_case_table_names)
  {
978
    my_casedn_str(files_charset_info, tmp_db);
unknown's avatar
unknown committed
979 980
    db=tmp_db;
  }
unknown's avatar
unknown committed
981 982 983 984 985
  key_length=(uint) (end-key);
  if ((entry=(acl_entry*) acl_cache->search(key,key_length)))
  {
    db_access=entry->access;
    VOID(pthread_mutex_unlock(&acl_cache->lock));
unknown's avatar
unknown committed
986 987
    DBUG_PRINT("exit", ("access: 0x%lx", db_access));
    DBUG_RETURN(db_access);
unknown's avatar
unknown committed
988 989 990 991 992 993 994 995 996 997 998 999
  }

  /*
    Check if there are some access rights for database and user
  */
  for (i=0 ; i < acl_dbs.elements ; i++)
  {
    ACL_DB *acl_db=dynamic_element(&acl_dbs,i,ACL_DB*);
    if (!acl_db->user || !strcmp(user,acl_db->user))
    {
      if (compare_hostname(&acl_db->host,host,ip))
      {
1000
	if (!acl_db->db || !wild_compare(db,acl_db->db,db_is_pattern))
unknown's avatar
unknown committed
1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021
	{
	  db_access=acl_db->access;
	  if (acl_db->host.hostname)
	    goto exit;				// Fully specified. Take it
	  break; /* purecov: tested */
	}
      }
    }
  }
  if (!db_access)
    goto exit;					// Can't be better

  /*
    No host specified for user. Get hostdata from host table
  */
  host_access=0;				// Host must be found
  for (i=0 ; i < acl_hosts.elements ; i++)
  {
    ACL_HOST *acl_host=dynamic_element(&acl_hosts,i,ACL_HOST*);
    if (compare_hostname(&acl_host->host,host,ip))
    {
1022
      if (!acl_host->db || !wild_compare(db,acl_host->db,db_is_pattern))
unknown's avatar
unknown committed
1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038
      {
	host_access=acl_host->access;		// Fully specified. Take it
	break;
      }
    }
  }
exit:
  /* Save entry in cache for quick retrieval */
  if ((entry= (acl_entry*) malloc(sizeof(acl_entry)+key_length)))
  {
    entry->access=(db_access & host_access);
    entry->length=key_length;
    memcpy((gptr) entry->key,key,key_length);
    acl_cache->add(entry);
  }
  VOID(pthread_mutex_unlock(&acl_cache->lock));
unknown's avatar
unknown committed
1039 1040
  DBUG_PRINT("exit", ("access: 0x%lx", db_access & host_access));
  DBUG_RETURN(db_access & host_access);
unknown's avatar
unknown committed
1041 1042
}

1043 1044 1045 1046 1047 1048 1049
/*
  Check if there are any possible matching entries for this host

  NOTES
    All host names without wild cards are stored in a hash table,
    entries with wildcards are stored in a dynamic array
*/
unknown's avatar
unknown committed
1050 1051 1052 1053

static void init_check_host(void)
{
  DBUG_ENTER("init_check_host");
1054
  VOID(my_init_dynamic_array(&acl_wild_hosts,sizeof(struct acl_host_and_ip),
unknown's avatar
unknown committed
1055
			  acl_users.elements,1));
unknown's avatar
unknown committed
1056
  VOID(hash_init(&acl_check_hosts,&my_charset_latin1,acl_users.elements,0,0,
unknown's avatar
unknown committed
1057
		 (hash_get_key) check_get_key,0,0));
unknown's avatar
unknown committed
1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071
  if (!allow_all_hosts)
  {
    for (uint i=0 ; i < acl_users.elements ; i++)
    {
      ACL_USER *acl_user=dynamic_element(&acl_users,i,ACL_USER*);
      if (strchr(acl_user->host.hostname,wild_many) ||
	  strchr(acl_user->host.hostname,wild_one) ||
	  acl_user->host.ip_mask)
      {						// Has wildcard
	uint j;
	for (j=0 ; j < acl_wild_hosts.elements ; j++)
	{					// Check if host already exists
	  acl_host_and_ip *acl=dynamic_element(&acl_wild_hosts,j,
					       acl_host_and_ip *);
unknown's avatar
unknown committed
1072
	  if (!my_strcasecmp(&my_charset_latin1,
1073
                             acl_user->host.hostname, acl->hostname))
unknown's avatar
unknown committed
1074 1075 1076 1077 1078 1079
	    break;				// already stored
	}
	if (j == acl_wild_hosts.elements)	// If new
	  (void) push_dynamic(&acl_wild_hosts,(char*) &acl_user->host);
      }
      else if (!hash_search(&acl_check_hosts,(byte*) &acl_user->host,
unknown's avatar
unknown committed
1080
			    (uint) strlen(acl_user->host.hostname)))
unknown's avatar
unknown committed
1081
      {
unknown's avatar
SCRUM  
unknown committed
1082
	if (my_hash_insert(&acl_check_hosts,(byte*) acl_user))
unknown's avatar
unknown committed
1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103
	{					// End of memory
	  allow_all_hosts=1;			// Should never happen
	  DBUG_VOID_RETURN;
	}
      }
    }
  }
  freeze_size(&acl_wild_hosts);
  freeze_size(&acl_check_hosts.array);
  DBUG_VOID_RETURN;
}


/* Return true if there is no users that can match the given host */

bool acl_check_host(const char *host, const char *ip)
{
  if (allow_all_hosts)
    return 0;
  VOID(pthread_mutex_lock(&acl_cache->lock));

unknown's avatar
unknown committed
1104 1105
  if (host && hash_search(&acl_check_hosts,(byte*) host,(uint) strlen(host)) ||
      ip && hash_search(&acl_check_hosts,(byte*) ip,(uint) strlen(ip)))
unknown's avatar
unknown committed
1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123
  {
    VOID(pthread_mutex_unlock(&acl_cache->lock));
    return 0;					// Found host
  }
  for (uint i=0 ; i < acl_wild_hosts.elements ; i++)
  {
    acl_host_and_ip *acl=dynamic_element(&acl_wild_hosts,i,acl_host_and_ip*);
    if (compare_hostname(acl, host, ip))
    {
      VOID(pthread_mutex_unlock(&acl_cache->lock));
      return 0;					// Host ok
    }
  }
  VOID(pthread_mutex_unlock(&acl_cache->lock));
  return 1;					// Host is not allowed
}


unknown's avatar
unknown committed
1124 1125 1126 1127 1128 1129 1130 1131
/*
  Check if the user is allowed to change password

  SYNOPSIS:
    check_change_password()
    thd		THD
    host	hostname for the user
    user	user name
unknown's avatar
merge  
unknown committed
1132

unknown's avatar
unknown committed
1133
    RETURN VALUE
1134 1135
      0		OK
      1		ERROR  ; In this case the error is sent to the client.
unknown's avatar
unknown committed
1136 1137
*/

1138 1139
bool check_change_password(THD *thd, const char *host, const char *user,
                           char *new_password)
unknown's avatar
unknown committed
1140
{
unknown's avatar
unknown committed
1141 1142
  if (!initialized)
  {
unknown's avatar
unknown committed
1143
    net_printf(thd,ER_OPTION_PREVENTS_STATEMENT,
1144 1145
             "--skip-grant-tables");
    return(1);
unknown's avatar
unknown committed
1146
  }
unknown's avatar
unknown committed
1147 1148
  if (!thd->slave_thread &&
      (strcmp(thd->user,user) ||
unknown's avatar
unknown committed
1149
       my_strcasecmp(&my_charset_latin1, host, thd->host_or_ip)))
unknown's avatar
unknown committed
1150
  {
unknown's avatar
SCRUM:  
unknown committed
1151
    if (check_access(thd, UPDATE_ACL, "mysql",0,1,0))
unknown's avatar
unknown committed
1152
      return(1);
unknown's avatar
unknown committed
1153
  }
unknown's avatar
unknown committed
1154 1155
  if (!thd->slave_thread && !thd->user[0])
  {
1156
    send_error(thd, ER_PASSWORD_ANONYMOUS_USER);
unknown's avatar
unknown committed
1157
    return(1);
unknown's avatar
unknown committed
1158
  }
1159
  uint len=strlen(new_password);
unknown's avatar
unknown committed
1160
  if (len && len != SCRAMBLED_PASSWORD_CHAR_LENGTH &&
1161 1162 1163 1164 1165 1166 1167
      len != SCRAMBLED_PASSWORD_CHAR_LENGTH_323)
  {
    net_printf(thd, 0,
               "Password hash should be a %d-digit hexadecimal number",
               SCRAMBLED_PASSWORD_CHAR_LENGTH);
    return -1;
  }
unknown's avatar
unknown committed
1168 1169 1170 1171
  return(0);
}


1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184
/*
  Change a password for a user

  SYNOPSIS
    change_password()
    thd			Thread handle
    host		Hostname
    user		User name
    new_password	New password for host@user

  RETURN VALUES
    0	ok
    1	ERROR; In this case the error is sent to the client.
unknown's avatar
unknown committed
1185
*/
1186

unknown's avatar
unknown committed
1187 1188 1189 1190 1191 1192 1193 1194
bool change_password(THD *thd, const char *host, const char *user,
		     char *new_password)
{
  DBUG_ENTER("change_password");
  DBUG_PRINT("enter",("host: '%s'  user: '%s'  new_password: '%s'",
		      host,user,new_password));
  DBUG_ASSERT(host != 0);			// Ensured by parent

1195
  if (check_change_password(thd, host, user, new_password))
unknown's avatar
unknown committed
1196 1197
    DBUG_RETURN(1);

unknown's avatar
unknown committed
1198 1199
  VOID(pthread_mutex_lock(&acl_cache->lock));
  ACL_USER *acl_user;
1200
  if (!(acl_user= find_acl_user(host, user)))
unknown's avatar
unknown committed
1201 1202
  {
    VOID(pthread_mutex_unlock(&acl_cache->lock));
1203
    send_error(thd, ER_PASSWORD_NO_MATCH);
unknown's avatar
unknown committed
1204
    DBUG_RETURN(1);
unknown's avatar
unknown committed
1205
  }
1206 1207 1208 1209
  /* update loaded acl entry: */
  uint new_password_len= new_password ? strlen(new_password) : 0;
  set_user_salt(acl_user, new_password, new_password_len);

unknown's avatar
unknown committed
1210 1211
  if (update_user_table(thd,
			acl_user->host.hostname ? acl_user->host.hostname : "",
unknown's avatar
unknown committed
1212
			acl_user->user ? acl_user->user : "",
1213
			new_password, new_password_len))
unknown's avatar
unknown committed
1214 1215
  {
    VOID(pthread_mutex_unlock(&acl_cache->lock)); /* purecov: deadcode */
1216
    send_error(thd,0); /* purecov: deadcode */
unknown's avatar
unknown committed
1217
    DBUG_RETURN(1); /* purecov: deadcode */
unknown's avatar
unknown committed
1218
  }
unknown's avatar
unknown committed
1219

unknown's avatar
unknown committed
1220 1221 1222
  acl_cache->clear(1);				// Clear locked hostname cache
  VOID(pthread_mutex_unlock(&acl_cache->lock));

1223
  char buff[512]; /* Extend with extended password length*/
1224
  ulong query_length=
unknown's avatar
unknown committed
1225 1226
    my_sprintf(buff,
	       (buff,"SET PASSWORD FOR \"%-.120s\"@\"%-.120s\"=\"%-.120s\"",
unknown's avatar
unknown committed
1227
		acl_user->user ? acl_user->user : "",
unknown's avatar
unknown committed
1228 1229
		acl_user->host.hostname ? acl_user->host.hostname : "",
		new_password));
unknown's avatar
unknown committed
1230
  thd->clear_error();
1231
  mysql_update_log.write(thd, buff, query_length);
1232
  Query_log_event qinfo(thd, buff, query_length, 0, FALSE);
unknown's avatar
unknown committed
1233
  mysql_bin_log.write(&qinfo);
unknown's avatar
unknown committed
1234
  DBUG_RETURN(0);
unknown's avatar
unknown committed
1235 1236 1237 1238 1239 1240 1241 1242 1243 1244
}


/*
  Find first entry that matches the current user
*/

static ACL_USER *
find_acl_user(const char *host, const char *user)
{
unknown's avatar
unknown committed
1245
  DBUG_ENTER("find_acl_user");
1246
  DBUG_PRINT("enter",("host: '%s'  user: '%s'",host,user));
unknown's avatar
unknown committed
1247 1248 1249
  for (uint i=0 ; i < acl_users.elements ; i++)
  {
    ACL_USER *acl_user=dynamic_element(&acl_users,i,ACL_USER*);
unknown's avatar
unknown committed
1250
    DBUG_PRINT("info",("strcmp('%s','%s'), compare_hostname('%s','%s'),",
1251 1252 1253 1254 1255
		       user,
		       acl_user->user ? acl_user->user : "",
		       host,
		       acl_user->host.hostname ? acl_user->host.hostname :
		       ""));
unknown's avatar
unknown committed
1256 1257 1258
    if (!acl_user->user && !user[0] ||
	acl_user->user && !strcmp(user,acl_user->user))
    {
1259
      if (compare_hostname(&acl_user->host,host,host))
unknown's avatar
unknown committed
1260 1261 1262
      {
	DBUG_RETURN(acl_user);
      }
unknown's avatar
unknown committed
1263 1264
    }
  }
unknown's avatar
unknown committed
1265
  DBUG_RETURN(0);
unknown's avatar
unknown committed
1266 1267 1268
}


1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279
/*
  Comparing of hostnames

  NOTES
  A hostname may be of type:
  hostname   (May include wildcards);   monty.pp.sci.fi
  ip	   (May include wildcards);   192.168.0.0
  ip/netmask			      192.168.0.0/255.255.255.0

  A net mask of 0.0.0.0 is not allowed.
*/
unknown's avatar
unknown committed
1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302

static const char *calc_ip(const char *ip, long *val, char end)
{
  long ip_val,tmp;
  if (!(ip=str2int(ip,10,0,255,&ip_val)) || *ip != '.')
    return 0;
  ip_val<<=24;
  if (!(ip=str2int(ip+1,10,0,255,&tmp)) || *ip != '.')
    return 0;
  ip_val+=tmp<<16;
  if (!(ip=str2int(ip+1,10,0,255,&tmp)) || *ip != '.')
    return 0;
  ip_val+=tmp<<8;
  if (!(ip=str2int(ip+1,10,0,255,&tmp)) || *ip != end)
    return 0;
  *val=ip_val+tmp;
  return ip;
}


static void update_hostname(acl_host_and_ip *host, const char *hostname)
{
  host->hostname=(char*) hostname;		// This will not be modified!
1303
  if (!hostname ||
unknown's avatar
unknown committed
1304 1305 1306
      (!(hostname=calc_ip(hostname,&host->ip,'/')) ||
       !(hostname=calc_ip(hostname+1,&host->ip_mask,'\0'))))
  {
1307
    host->ip= host->ip_mask=0;			// Not a masked ip
unknown's avatar
unknown committed
1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320
  }
}


static bool compare_hostname(const acl_host_and_ip *host, const char *hostname,
			     const char *ip)
{
  long tmp;
  if (host->ip_mask && ip && calc_ip(ip,&tmp,'\0'))
  {
    return (tmp & host->ip_mask) == host->ip;
  }
  return (!host->hostname ||
unknown's avatar
unknown committed
1321
	  (hostname && !wild_case_compare(&my_charset_latin1,
1322
                                          hostname,host->hostname)) ||
1323
	  (ip && !wild_compare(ip,host->hostname,0)));
unknown's avatar
unknown committed
1324 1325
}

unknown's avatar
SCRUM  
unknown committed
1326 1327 1328 1329
bool hostname_requires_resolving(const char *hostname)
{
  char cur;
  if (!hostname)
unknown's avatar
unknown committed
1330
    return FALSE;
unknown's avatar
SCRUM  
unknown committed
1331 1332 1333 1334 1335
  int namelen= strlen(hostname);
  int lhlen= strlen(my_localhost);
  if ((namelen == lhlen) &&
      !my_strnncoll(&my_charset_latin1, (const uchar *)hostname,  namelen,
		    (const uchar *)my_localhost, strlen(my_localhost)))
unknown's avatar
unknown committed
1336
    return FALSE;
unknown's avatar
SCRUM  
unknown committed
1337 1338 1339 1340
  for (; (cur=*hostname); hostname++)
  {
    if ((cur != '%') && (cur != '_') && (cur != '.') &&
	((cur < '0') || (cur > '9')))
unknown's avatar
unknown committed
1341
      return TRUE;
unknown's avatar
SCRUM  
unknown committed
1342
  }
unknown's avatar
unknown committed
1343
  return FALSE;
unknown's avatar
SCRUM  
unknown committed
1344
}
unknown's avatar
unknown committed
1345

1346 1347 1348
/*
  Update grants in the user and database privilege tables
*/
unknown's avatar
unknown committed
1349 1350

static bool update_user_table(THD *thd, const char *host, const char *user,
1351
			      const char *new_password, uint new_password_len)
unknown's avatar
unknown committed
1352 1353 1354 1355 1356 1357 1358 1359
{
  TABLE_LIST tables;
  TABLE *table;
  bool error=1;
  DBUG_ENTER("update_user_table");
  DBUG_PRINT("enter",("user: %s  host: %s",user,host));

  bzero((char*) &tables,sizeof(tables));
1360
  tables.alias=tables.real_name=(char*) "user";
unknown's avatar
unknown committed
1361
  tables.db=(char*) "mysql";
unknown's avatar
unknown committed
1362

1363 1364 1365 1366 1367 1368 1369
#ifdef HAVE_REPLICATION
  /*
    GRANT and REVOKE are applied the slave in/exclusion rules as they are
    some kind of updates to the mysql.% tables.
  */
  if (thd->slave_thread && table_rules_on)
  {
unknown's avatar
unknown committed
1370 1371 1372
    /*
      The tables must be marked "updating" so that tables_ok() takes them into
      account in tests.  It's ok to leave 'updating' set after tables_ok.
1373
    */
1374
    tables.updating= 1;
1375 1376 1377 1378 1379 1380
    /* Thanks to bzero, tables.next==0 */
    if (!tables_ok(0, &tables))
      DBUG_RETURN(0);
  }
#endif

unknown's avatar
unknown committed
1381 1382
  if (!(table=open_ltable(thd,&tables,TL_WRITE)))
    DBUG_RETURN(1); /* purecov: deadcode */
unknown's avatar
unknown committed
1383 1384
  table->field[0]->store(host,(uint) strlen(host), &my_charset_latin1);
  table->field[1]->store(user,(uint) strlen(user), &my_charset_latin1);
unknown's avatar
unknown committed
1385

unknown's avatar
unknown committed
1386
  table->file->extra(HA_EXTRA_RETRIEVE_ALL_COLS);
unknown's avatar
unknown committed
1387
  if (table->file->index_read_idx(table->record[0],0,
unknown's avatar
unknown committed
1388 1389
				  (byte*) table->field[0]->ptr,
				  table->key_info[0].key_length,
unknown's avatar
unknown committed
1390 1391 1392 1393 1394
				  HA_READ_KEY_EXACT))
  {
    my_error(ER_PASSWORD_NO_MATCH,MYF(0));	/* purecov: deadcode */
    DBUG_RETURN(1);				/* purecov: deadcode */
  }
unknown's avatar
unknown committed
1395
  store_record(table,record[1]);
1396
  table->field[2]->store(new_password, new_password_len, &my_charset_latin1);
unknown's avatar
unknown committed
1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408
  if ((error=table->file->update_row(table->record[1],table->record[0])))
  {
    table->file->print_error(error,MYF(0));	/* purecov: deadcode */
    goto end;					/* purecov: deadcode */
  }
  error=0;					// Record updated

end:
  close_thread_tables(thd);
  DBUG_RETURN(error);
}

unknown's avatar
unknown committed
1409 1410 1411 1412 1413 1414 1415 1416 1417

/* Return 1 if we are allowed to create new users */

static bool test_if_create_new_users(THD *thd)
{
  bool create_new_users=1;    // Assume that we are allowed to create new users
  if (opt_safe_user_create && !(thd->master_access & INSERT_ACL))
  {
    TABLE_LIST tl;
unknown's avatar
unknown committed
1418
    ulong db_access;
unknown's avatar
unknown committed
1419 1420
    bzero((char*) &tl,sizeof(tl));
    tl.db=	   (char*) "mysql";
unknown's avatar
unknown committed
1421
    tl.real_name=  (char*) "user";
unknown's avatar
unknown committed
1422

1423
    db_access=acl_get(thd->host, thd->ip,
1424
		      thd->priv_user, tl.db, 0);
unknown's avatar
unknown committed
1425 1426
    if (!(db_access & INSERT_ACL))
    {
unknown's avatar
unknown committed
1427
      if (check_grant(thd, INSERT_ACL, &tl, 0, UINT_MAX, 1))
unknown's avatar
unknown committed
1428 1429 1430 1431 1432 1433 1434
	create_new_users=0;
    }
  }
  return create_new_users;
}


unknown's avatar
unknown committed
1435
/****************************************************************************
1436
  Handle GRANT commands
unknown's avatar
unknown committed
1437 1438
****************************************************************************/

1439
static int replace_user_table(THD *thd, TABLE *table, const LEX_USER &combo,
unknown's avatar
unknown committed
1440 1441
			      ulong rights, bool revoke_grant,
			      bool create_user)
unknown's avatar
unknown committed
1442 1443
{
  int error = -1;
unknown's avatar
unknown committed
1444
  bool old_row_exists=0;
1445
  const char *password= "";
1446
  uint password_len= 0;
unknown's avatar
unknown committed
1447
  char what= (revoke_grant) ? 'N' : 'Y';
unknown's avatar
unknown committed
1448
  DBUG_ENTER("replace_user_table");
1449
  safe_mutex_assert_owner(&acl_cache->lock);
unknown's avatar
unknown committed
1450 1451

  if (combo.password.str && combo.password.str[0])
1452
  {
1453 1454
    if (combo.password.length != SCRAMBLED_PASSWORD_CHAR_LENGTH &&
        combo.password.length != SCRAMBLED_PASSWORD_CHAR_LENGTH_323)
1455
    {
1456
      my_printf_error(ER_UNKNOWN_ERROR,
1457 1458
                      "Password hash should be a %d-digit hexadecimal number",
                      MYF(0), SCRAMBLED_PASSWORD_CHAR_LENGTH);
unknown's avatar
unknown committed
1459
      DBUG_RETURN(-1);
1460
    }
1461
    password_len= combo.password.length;
unknown's avatar
unknown committed
1462
    password=combo.password.str;
1463
  }
unknown's avatar
unknown committed
1464

unknown's avatar
unknown committed
1465 1466
  table->field[0]->store(combo.host.str,combo.host.length, &my_charset_latin1);
  table->field[1]->store(combo.user.str,combo.user.length, &my_charset_latin1);
unknown's avatar
unknown committed
1467
  table->file->extra(HA_EXTRA_RETRIEVE_ALL_COLS);
unknown's avatar
unknown committed
1468
  if (table->file->index_read_idx(table->record[0], 0,
unknown's avatar
unknown committed
1469 1470 1471
				  (byte*) table->field[0]->ptr,
				  table->key_info[0].key_length,
				  HA_READ_KEY_EXACT))
unknown's avatar
unknown committed
1472
  {
unknown's avatar
unknown committed
1473
    if (!create_user)
unknown's avatar
unknown committed
1474
    {
unknown's avatar
unknown committed
1475
      if (what == 'N')
unknown's avatar
unknown committed
1476
	my_error(ER_NONEXISTING_GRANT, MYF(0), combo.user.str, combo.host.str);
unknown's avatar
unknown committed
1477
      else
unknown's avatar
unknown committed
1478 1479
	my_error(ER_NO_PERMISSION_TO_CREATE_USER, MYF(0),
                 thd->user, thd->host_or_ip);
unknown's avatar
unknown committed
1480 1481
      goto end;
    }
unknown's avatar
unknown committed
1482
    old_row_exists = 0;
1483 1484 1485 1486 1487 1488 1489
    restore_record(table,default_values);       // cp empty row from default_values
    table->field[0]->store(combo.host.str,combo.host.length,
                           &my_charset_latin1);
    table->field[1]->store(combo.user.str,combo.user.length,
                           &my_charset_latin1);
    table->field[2]->store(password, password_len,
                           &my_charset_latin1);
unknown's avatar
unknown committed
1490 1491 1492
  }
  else
  {
unknown's avatar
unknown committed
1493
    old_row_exists = 1;
unknown's avatar
unknown committed
1494
    store_record(table,record[1]);			// Save copy for update
unknown's avatar
unknown committed
1495
    if (combo.password.str)			// If password given
1496
      table->field[2]->store(password, password_len, &my_charset_latin1);
unknown's avatar
unknown committed
1497 1498 1499 1500 1501
    else if (!rights && !revoke_grant && thd->lex->ssl_type == SSL_TYPE_NOT_SPECIFIED &&
	     !thd->lex->mqh.bits)
    {
      DBUG_RETURN(0);
    }
unknown's avatar
unknown committed
1502 1503
  }

unknown's avatar
unknown committed
1504 1505 1506 1507 1508 1509 1510 1511
  /* Update table columns with new privileges */

  Field **tmp_field;
  ulong priv;
  for (tmp_field= table->field+3, priv = SELECT_ACL;
       *tmp_field && (*tmp_field)->real_type() == FIELD_TYPE_ENUM &&
	 ((Field_enum*) (*tmp_field))->typelib->count == 2 ;
       tmp_field++, priv <<= 1)
unknown's avatar
unknown committed
1512
  {
unknown's avatar
unknown committed
1513
    if (priv & rights)				 // set requested privileges
unknown's avatar
unknown committed
1514
      (*tmp_field)->store(&what, 1, &my_charset_latin1);
unknown's avatar
unknown committed
1515 1516
  }
  rights=get_access(table,3);
1517
  DBUG_PRINT("info",("table->fields: %d",table->fields));
unknown's avatar
unknown committed
1518
  if (table->fields >= 31)		/* From 4.0.0 we have more fields */
1519
  {
unknown's avatar
unknown committed
1520
    /* We write down SSL related ACL stuff */
1521
    switch (thd->lex->ssl_type) {
unknown's avatar
unknown committed
1522
    case SSL_TYPE_ANY:
unknown's avatar
unknown committed
1523 1524 1525 1526
      table->field[24]->store("ANY",3, &my_charset_latin1);
      table->field[25]->store("", 0, &my_charset_latin1);
      table->field[26]->store("", 0, &my_charset_latin1);
      table->field[27]->store("", 0, &my_charset_latin1);
unknown's avatar
unknown committed
1527 1528
      break;
    case SSL_TYPE_X509:
unknown's avatar
unknown committed
1529 1530 1531 1532
      table->field[24]->store("X509",4, &my_charset_latin1);
      table->field[25]->store("", 0, &my_charset_latin1);
      table->field[26]->store("", 0, &my_charset_latin1);
      table->field[27]->store("", 0, &my_charset_latin1);
unknown's avatar
unknown committed
1533 1534
      break;
    case SSL_TYPE_SPECIFIED:
unknown's avatar
unknown committed
1535 1536 1537 1538
      table->field[24]->store("SPECIFIED",9, &my_charset_latin1);
      table->field[25]->store("", 0, &my_charset_latin1);
      table->field[26]->store("", 0, &my_charset_latin1);
      table->field[27]->store("", 0, &my_charset_latin1);
1539 1540 1541 1542 1543 1544 1545 1546 1547
      if (thd->lex->ssl_cipher)
	table->field[25]->store(thd->lex->ssl_cipher,
				strlen(thd->lex->ssl_cipher), &my_charset_latin1);
      if (thd->lex->x509_issuer)
	table->field[26]->store(thd->lex->x509_issuer,
				strlen(thd->lex->x509_issuer), &my_charset_latin1);
      if (thd->lex->x509_subject)
	table->field[27]->store(thd->lex->x509_subject,
				strlen(thd->lex->x509_subject), &my_charset_latin1);
unknown's avatar
unknown committed
1548
      break;
unknown's avatar
unknown committed
1549
    case SSL_TYPE_NOT_SPECIFIED:
unknown's avatar
unknown committed
1550 1551
      break;
    case SSL_TYPE_NONE:
unknown's avatar
unknown committed
1552 1553 1554 1555
      table->field[24]->store("", 0, &my_charset_latin1);
      table->field[25]->store("", 0, &my_charset_latin1);
      table->field[26]->store("", 0, &my_charset_latin1);
      table->field[27]->store("", 0, &my_charset_latin1);
unknown's avatar
unknown committed
1556
      break;
1557
    }
unknown's avatar
unknown committed
1558

1559
    USER_RESOURCES mqh= thd->lex->mqh;
1560
    if (mqh.bits & 1)
unknown's avatar
unknown committed
1561
      table->field[28]->store((longlong) mqh.questions);
1562
    if (mqh.bits & 2)
unknown's avatar
unknown committed
1563
      table->field[29]->store((longlong) mqh.updates);
1564
    if (mqh.bits & 4)
unknown's avatar
unknown committed
1565
      table->field[30]->store((longlong) mqh.connections);
1566
    mqh_used = mqh_used || mqh.questions || mqh.updates || mqh.connections;
unknown's avatar
unknown committed
1567
  }
unknown's avatar
unknown committed
1568
  if (old_row_exists)
unknown's avatar
unknown committed
1569 1570 1571 1572 1573
  {
    /*
      We should NEVER delete from the user table, as a uses can still
      use mysqld even if he doesn't have any privileges in the user table!
    */
unknown's avatar
unknown committed
1574
    table->file->extra(HA_EXTRA_RETRIEVE_ALL_COLS);
unknown's avatar
unknown committed
1575
    if (cmp_record(table,record[1]) &&
unknown's avatar
unknown committed
1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594
	(error=table->file->update_row(table->record[1],table->record[0])))
    {						// This should never happen
      table->file->print_error(error,MYF(0));	/* purecov: deadcode */
      error= -1;				/* purecov: deadcode */
      goto end;					/* purecov: deadcode */
    }
  }
  else if ((error=table->file->write_row(table->record[0]))) // insert
  {						// This should never happen
    if (error && error != HA_ERR_FOUND_DUPP_KEY &&
	error != HA_ERR_FOUND_DUPP_UNIQUE)	/* purecov: inspected */
    {
      table->file->print_error(error,MYF(0));	/* purecov: deadcode */
      error= -1;				/* purecov: deadcode */
      goto end;					/* purecov: deadcode */
    }
  }
  error=0;					// Privileges granted / revoked

unknown's avatar
unknown committed
1595
end:
unknown's avatar
unknown committed
1596 1597 1598
  if (!error)
  {
    acl_cache->clear(1);			// Clear privilege cache
unknown's avatar
unknown committed
1599
    if (old_row_exists)
1600 1601
      acl_update_user(combo.user.str, combo.host.str,
                      combo.password.str, password_len,
1602 1603 1604 1605 1606
		      thd->lex->ssl_type,
		      thd->lex->ssl_cipher,
		      thd->lex->x509_issuer,
		      thd->lex->x509_subject,
		      &thd->lex->mqh,
unknown's avatar
unknown committed
1607
		      rights);
unknown's avatar
unknown committed
1608
    else
1609
      acl_insert_user(combo.user.str, combo.host.str, password, password_len,
1610 1611 1612 1613 1614
		      thd->lex->ssl_type,
		      thd->lex->ssl_cipher,
		      thd->lex->x509_issuer,
		      thd->lex->x509_subject,
		      &thd->lex->mqh,
unknown's avatar
unknown committed
1615
		      rights);
unknown's avatar
unknown committed
1616 1617 1618 1619 1620 1621
  }
  DBUG_RETURN(error);
}


/*
unknown's avatar
unknown committed
1622
  change grants in the mysql.db table
unknown's avatar
unknown committed
1623 1624 1625 1626
*/

static int replace_db_table(TABLE *table, const char *db,
			    const LEX_USER &combo,
unknown's avatar
unknown committed
1627
			    ulong rights, bool revoke_grant)
unknown's avatar
unknown committed
1628
{
unknown's avatar
unknown committed
1629 1630
  uint i;
  ulong priv,store_rights;
unknown's avatar
unknown committed
1631
  bool old_row_exists=0;
unknown's avatar
unknown committed
1632
  int error;
unknown's avatar
unknown committed
1633
  char what= (revoke_grant) ? 'N' : 'Y';
unknown's avatar
unknown committed
1634 1635
  DBUG_ENTER("replace_db_table");

1636 1637
  if (!initialized)
  {
unknown's avatar
unknown committed
1638
    my_error(ER_OPTION_PREVENTS_STATEMENT, MYF(0), "--skip-grant-tables");
1639 1640 1641
    DBUG_RETURN(-1);
  }

1642
  /* Check if there is such a user in user table in memory? */
1643
  if (!find_acl_user(combo.host.str,combo.user.str))
unknown's avatar
unknown committed
1644 1645 1646 1647 1648
  {
    my_error(ER_PASSWORD_NO_MATCH,MYF(0));
    DBUG_RETURN(-1);
  }

unknown's avatar
unknown committed
1649 1650 1651
  table->field[0]->store(combo.host.str,combo.host.length, &my_charset_latin1);
  table->field[1]->store(db,(uint) strlen(db), &my_charset_latin1);
  table->field[2]->store(combo.user.str,combo.user.length, &my_charset_latin1);
unknown's avatar
unknown committed
1652 1653 1654 1655 1656
  table->file->extra(HA_EXTRA_RETRIEVE_ALL_COLS);
  if (table->file->index_read_idx(table->record[0],0,
				  (byte*) table->field[0]->ptr,
				  table->key_info[0].key_length,
				  HA_READ_KEY_EXACT))
unknown's avatar
unknown committed
1657 1658 1659
  {
    if (what == 'N')
    { // no row, no revoke
unknown's avatar
unknown committed
1660
      my_error(ER_NONEXISTING_GRANT, MYF(0), combo.user.str, combo.host.str);
unknown's avatar
unknown committed
1661 1662
      goto abort;
    }
unknown's avatar
unknown committed
1663
    old_row_exists = 0;
unknown's avatar
unknown committed
1664
    restore_record(table,default_values);			// cp empty row from default_values
unknown's avatar
unknown committed
1665 1666 1667
    table->field[0]->store(combo.host.str,combo.host.length, &my_charset_latin1);
    table->field[1]->store(db,(uint) strlen(db), &my_charset_latin1);
    table->field[2]->store(combo.user.str,combo.user.length, &my_charset_latin1);
unknown's avatar
unknown committed
1668 1669 1670
  }
  else
  {
unknown's avatar
unknown committed
1671
    old_row_exists = 1;
unknown's avatar
unknown committed
1672
    store_record(table,record[1]);
unknown's avatar
unknown committed
1673 1674 1675
  }

  store_rights=get_rights_for_db(rights);
unknown's avatar
unknown committed
1676
  for (i= 3, priv= 1; i < table->fields; i++, priv <<= 1)
unknown's avatar
unknown committed
1677
  {
unknown's avatar
unknown committed
1678
    if (priv & store_rights)			// do it if priv is chosen
unknown's avatar
unknown committed
1679
      table->field [i]->store(&what,1, &my_charset_latin1);// set requested privileges
unknown's avatar
unknown committed
1680 1681 1682 1683
  }
  rights=get_access(table,3);
  rights=fix_rights_for_db(rights);

unknown's avatar
unknown committed
1684
  if (old_row_exists)
unknown's avatar
unknown committed
1685
  {
1686
    /* update old existing row */
unknown's avatar
unknown committed
1687 1688
    if (rights)
    {
unknown's avatar
unknown committed
1689
      table->file->extra(HA_EXTRA_RETRIEVE_ALL_COLS);
unknown's avatar
unknown committed
1690 1691 1692 1693 1694 1695 1696 1697 1698
      if ((error=table->file->update_row(table->record[1],table->record[0])))
	goto table_error;			/* purecov: deadcode */
    }
    else	/* must have been a revoke of all privileges */
    {
      if ((error = table->file->delete_row(table->record[1])))
	goto table_error;			/* purecov: deadcode */
    }
  }
1699
  else if (rights && (error=table->file->write_row(table->record[0])))
unknown's avatar
unknown committed
1700 1701 1702 1703 1704 1705
  {
    if (error && error != HA_ERR_FOUND_DUPP_KEY) /* purecov: inspected */
      goto table_error; /* purecov: deadcode */
  }

  acl_cache->clear(1);				// Clear privilege cache
unknown's avatar
unknown committed
1706
  if (old_row_exists)
unknown's avatar
unknown committed
1707 1708
    acl_update_db(combo.user.str,combo.host.str,db,rights);
  else
1709
  if (rights)
unknown's avatar
unknown committed
1710 1711 1712 1713
    acl_insert_db(combo.user.str,combo.host.str,db,rights);
  DBUG_RETURN(0);

  /* This could only happen if the grant tables got corrupted */
1714
table_error:
unknown's avatar
unknown committed
1715 1716
  table->file->print_error(error,MYF(0));	/* purecov: deadcode */

1717
abort:
unknown's avatar
unknown committed
1718 1719 1720 1721 1722 1723 1724 1725
  DBUG_RETURN(-1);
}


class GRANT_COLUMN :public Sql_alloc
{
public:
  char *column;
unknown's avatar
unknown committed
1726 1727 1728
  ulong rights;
  uint key_length;
  GRANT_COLUMN(String &c,  ulong y) :rights (y)
unknown's avatar
unknown committed
1729
  {
unknown's avatar
unknown committed
1730
    column= memdup_root(&memex,c.ptr(), key_length=c.length());
unknown's avatar
unknown committed
1731 1732 1733
  }
};

unknown's avatar
unknown committed
1734

unknown's avatar
unknown committed
1735 1736 1737 1738 1739 1740 1741
static byte* get_key_column(GRANT_COLUMN *buff,uint *length,
			    my_bool not_used __attribute__((unused)))
{
  *length=buff->key_length;
  return (byte*) buff->column;
}

unknown's avatar
unknown committed
1742

unknown's avatar
unknown committed
1743 1744 1745
class GRANT_TABLE :public Sql_alloc
{
public:
unknown's avatar
unknown committed
1746
  char *host,*db, *user, *tname, *hash_key, *orig_host;
unknown's avatar
unknown committed
1747
  ulong privs, cols;
1748
  ulong sort;
unknown's avatar
unknown committed
1749
  uint key_length;
unknown's avatar
unknown committed
1750
  HASH hash_columns;
unknown's avatar
unknown committed
1751 1752 1753 1754

  GRANT_TABLE(const char *h, const char *d,const char *u,
              const char *t, ulong p, ulong c);
  GRANT_TABLE (TABLE *form, TABLE *col_privs);
1755 1756
  bool ok() { return privs != 0 || cols != 0; }
};
unknown's avatar
unknown committed
1757

1758

unknown's avatar
unknown committed
1759

1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772
GRANT_TABLE::GRANT_TABLE(const char *h, const char *d,const char *u,
                         const char *t, ulong p, ulong c)
  :privs(p), cols(c)
{
  /* Host given by user */
  orig_host=  strdup_root(&memex,h);
  /* Convert empty hostname to '%' for easy comparision */
  host=  orig_host[0] ? orig_host : (char*) "%";
  db =   strdup_root(&memex,d);
  user = strdup_root(&memex,u);
  sort=  get_sort(3,host,db,user);
  tname= strdup_root(&memex,t);
  if (lower_case_table_names)
unknown's avatar
unknown committed
1773
  {
1774 1775
    my_casedn_str(files_charset_info, db);
    my_casedn_str(files_charset_info, tname);
1776 1777 1778 1779
  }
  key_length =(uint) strlen(d)+(uint) strlen(u)+(uint) strlen(t)+3;
  hash_key = (char*) alloc_root(&memex,key_length);
  strmov(strmov(strmov(hash_key,user)+1,db)+1,tname);
unknown's avatar
unknown committed
1780 1781
  (void) hash_init(&hash_columns,&my_charset_latin1,
                   0,0,0, (hash_get_key) get_key_column,0,0);
1782
}
unknown's avatar
unknown committed
1783

1784 1785 1786 1787 1788

GRANT_TABLE::GRANT_TABLE(TABLE *form, TABLE *col_privs)
{
  byte key[MAX_KEY_LENGTH];

unknown's avatar
unknown committed
1789 1790 1791
  orig_host= host= get_field(&memex, form->field[0]);
  db=    get_field(&memex,form->field[1]);
  user=  get_field(&memex,form->field[2]);
1792 1793 1794 1795 1796 1797 1798
  if (!user)
    user= (char*) "";
  if (!orig_host)
  {
    orig_host= (char*) "";
    host= (char*) "%";
  }
unknown's avatar
unknown committed
1799 1800
  sort=  get_sort(3, orig_host, db, user);
  tname= get_field(&memex,form->field[3]);
1801 1802 1803 1804 1805 1806 1807 1808
  if (!db || !tname)
  {
    /* Wrong table row; Ignore it */
    privs = cols = 0;				/* purecov: inspected */
    return;					/* purecov: inspected */
  }
  if (lower_case_table_names)
  {
1809 1810
    my_casedn_str(files_charset_info, db);
    my_casedn_str(files_charset_info, tname);
1811 1812 1813 1814 1815 1816 1817 1818 1819 1820
  }
  key_length = ((uint) strlen(db) + (uint) strlen(user) +
                (uint) strlen(tname) + 3);
  hash_key = (char*) alloc_root(&memex,key_length);
  strmov(strmov(strmov(hash_key,user)+1,db)+1,tname);
  privs = (ulong) form->field[6]->val_int();
  cols  = (ulong) form->field[7]->val_int();
  privs = fix_rights_for_table(privs);
  cols =  fix_rights_for_column(cols);

unknown's avatar
unknown committed
1821 1822
  (void) hash_init(&hash_columns,&my_charset_latin1,
                   0,0,0, (hash_get_key) get_key_column,0,0);
1823 1824 1825
  if (cols)
  {
    int key_len;
unknown's avatar
unknown committed
1826 1827 1828 1829 1830
    col_privs->field[0]->store(orig_host,(uint) strlen(orig_host),
                               &my_charset_latin1);
    col_privs->field[1]->store(db,(uint) strlen(db), &my_charset_latin1);
    col_privs->field[2]->store(user,(uint) strlen(user), &my_charset_latin1);
    col_privs->field[3]->store(tname,(uint) strlen(tname), &my_charset_latin1);
1831 1832 1833 1834 1835
    key_len=(col_privs->field[0]->pack_length()+
             col_privs->field[1]->pack_length()+
             col_privs->field[2]->pack_length()+
             col_privs->field[3]->pack_length());
    key_copy(key,col_privs,0,key_len);
unknown's avatar
unknown committed
1836
    col_privs->field[4]->store("",0, &my_charset_latin1);
unknown's avatar
unknown committed
1837 1838
    col_privs->file->ha_index_init(0);
    if (col_privs->file->index_read(col_privs->record[0],
1839 1840
                                    (byte*) col_privs->field[0]->ptr,
                                    key_len, HA_READ_KEY_EXACT))
unknown's avatar
unknown committed
1841
    {
1842
      cols = 0; /* purecov: deadcode */
unknown's avatar
unknown committed
1843
      col_privs->file->ha_index_end();
1844
      return;
unknown's avatar
unknown committed
1845
    }
1846
    do
unknown's avatar
unknown committed
1847
    {
1848 1849 1850
      String *res,column_name;
      GRANT_COLUMN *mem_check;
      /* As column name is a string, we don't have to supply a buffer */
unknown's avatar
unknown committed
1851
      res=col_privs->field[4]->val_str(&column_name);
1852 1853 1854
      ulong priv= (ulong) col_privs->field[6]->val_int();
      if (!(mem_check = new GRANT_COLUMN(*res,
                                         fix_rights_for_column(priv))))
unknown's avatar
unknown committed
1855
      {
1856 1857 1858
        /* Don't use this entry */
        privs = cols = 0;			/* purecov: deadcode */
        return;				/* purecov: deadcode */
unknown's avatar
unknown committed
1859
      }
unknown's avatar
unknown committed
1860
      my_hash_insert(&hash_columns, (byte *) mem_check);
1861
    } while (!col_privs->file->index_next(col_privs->record[0]) &&
unknown's avatar
unknown committed
1862
             !key_cmp_if_same(col_privs,key,0,key_len));
unknown's avatar
unknown committed
1863
    col_privs->file->ha_index_end();
unknown's avatar
unknown committed
1864
  }
1865
}
unknown's avatar
unknown committed
1866

unknown's avatar
unknown committed
1867

unknown's avatar
unknown committed
1868 1869 1870 1871 1872 1873 1874
static byte* get_grant_table(GRANT_TABLE *buff,uint *length,
			     my_bool not_used __attribute__((unused)))
{
  *length=buff->key_length;
  return (byte*) buff->hash_key;
}

unknown's avatar
unknown committed
1875

unknown's avatar
unknown committed
1876 1877 1878 1879 1880
void free_grant_table(GRANT_TABLE *grant_table)
{
  hash_free(&grant_table->hash_columns);
}

unknown's avatar
unknown committed
1881

unknown's avatar
unknown committed
1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893
/* Search after a matching grant. Prefer exact grants before not exact ones */

static GRANT_TABLE *table_hash_search(const char *host,const char* ip,
				      const char *db,
				      const char *user, const char *tname,
				      bool exact)
{
  char helping [NAME_LEN*2+USERNAME_LENGTH+3];
  uint len;
  GRANT_TABLE *grant_table,*found=0;

  len  = (uint) (strmov(strmov(strmov(helping,user)+1,db)+1,tname)-helping)+ 1;
1894 1895
  for (grant_table=(GRANT_TABLE*) hash_search(&column_priv_hash,
					      (byte*) helping,
unknown's avatar
unknown committed
1896 1897
					      len) ;
       grant_table ;
1898 1899
       grant_table= (GRANT_TABLE*) hash_next(&column_priv_hash,(byte*) helping,
					     len))
unknown's avatar
unknown committed
1900 1901 1902
  {
    if (exact)
    {
1903
      if ((host &&
unknown's avatar
unknown committed
1904
	   !my_strcasecmp(&my_charset_latin1, host, grant_table->host)) ||
unknown's avatar
unknown committed
1905 1906 1907 1908 1909
	  (ip && !strcmp(ip,grant_table->host)))
	return grant_table;
    }
    else
    {
unknown's avatar
unknown committed
1910 1911 1912 1913
      if (((host && !wild_case_compare(&my_charset_latin1,
				       host,grant_table->host)) ||
	   (ip && !wild_case_compare(&my_charset_latin1,
				     ip,grant_table->host))) &&
1914
          (!found || found->sort < grant_table->sort))
unknown's avatar
unknown committed
1915 1916 1917 1918 1919 1920 1921 1922
	found=grant_table;					// Host ok
    }
  }
  return found;
}



unknown's avatar
unknown committed
1923
inline GRANT_COLUMN *
unknown's avatar
unknown committed
1924
column_hash_search(GRANT_TABLE *t, const char *cname, uint length)
unknown's avatar
unknown committed
1925 1926 1927 1928 1929 1930 1931 1932 1933
{
  return (GRANT_COLUMN*) hash_search(&t->hash_columns, (byte*) cname,length);
}


static int replace_column_table(GRANT_TABLE *g_t,
				TABLE *table, const LEX_USER &combo,
				List <LEX_COLUMN> &columns,
				const char *db, const char *table_name,
unknown's avatar
unknown committed
1934
				ulong rights, bool revoke_grant)
unknown's avatar
unknown committed
1935 1936 1937 1938 1939 1940
{
  int error=0,result=0;
  uint key_length;
  byte key[MAX_KEY_LENGTH];
  DBUG_ENTER("replace_column_table");

unknown's avatar
unknown committed
1941 1942 1943 1944
  table->field[0]->store(combo.host.str,combo.host.length, &my_charset_latin1);
  table->field[1]->store(db,(uint) strlen(db), &my_charset_latin1);
  table->field[2]->store(combo.user.str,combo.user.length, &my_charset_latin1);
  table->field[3]->store(table_name,(uint) strlen(table_name), &my_charset_latin1);
unknown's avatar
unknown committed
1945 1946 1947 1948 1949 1950 1951 1952 1953 1954
  key_length=(table->field[0]->pack_length()+ table->field[1]->pack_length()+
	      table->field[2]->pack_length()+ table->field[3]->pack_length());
  key_copy(key,table,0,key_length);

  rights &= COL_ACLS;				// Only ACL for columns

  /* first fix privileges for all columns in column list */

  List_iterator <LEX_COLUMN> iter(columns);
  class LEX_COLUMN *xx;
unknown's avatar
unknown committed
1955
  table->file->ha_index_init(0);
unknown's avatar
unknown committed
1956 1957
  while ((xx=iter++))
  {
unknown's avatar
unknown committed
1958
    ulong privileges = xx->rights;
unknown's avatar
unknown committed
1959
    bool old_row_exists=0;
unknown's avatar
unknown committed
1960
    key_restore(table,key,0,key_length);
1961 1962
    table->field[4]->store(xx->column.ptr(),xx->column.length(),
                           &my_charset_latin1);
unknown's avatar
unknown committed
1963

unknown's avatar
unknown committed
1964
    table->file->extra(HA_EXTRA_RETRIEVE_ALL_COLS);
unknown's avatar
unknown committed
1965
    if (table->file->index_read(table->record[0],(byte*) table->field[0]->ptr,
unknown's avatar
unknown committed
1966 1967
				table->key_info[0].key_length,
				HA_READ_KEY_EXACT))
unknown's avatar
unknown committed
1968 1969 1970
    {
      if (revoke_grant)
      {
unknown's avatar
unknown committed
1971 1972
	my_error(ER_NONEXISTING_TABLE_GRANT, MYF(0),
                 combo.user.str, combo.host.str, table_name); /* purecov: inspected */
unknown's avatar
unknown committed
1973 1974 1975
	result= -1; /* purecov: inspected */
	continue; /* purecov: inspected */
      }
unknown's avatar
unknown committed
1976
      old_row_exists = 0;
1977
      restore_record(table,default_values);		// Get empty record
unknown's avatar
unknown committed
1978
      key_restore(table,key,0,key_length);
1979 1980
      table->field[4]->store(xx->column.ptr(),xx->column.length(),
                             &my_charset_latin1);
unknown's avatar
unknown committed
1981 1982 1983
    }
    else
    {
unknown's avatar
unknown committed
1984
      ulong tmp= (ulong) table->field[6]->val_int();
unknown's avatar
unknown committed
1985 1986 1987 1988 1989 1990
      tmp=fix_rights_for_column(tmp);

      if (revoke_grant)
	privileges = tmp & ~(privileges | rights);
      else
	privileges |= tmp;
unknown's avatar
unknown committed
1991
      old_row_exists = 1;
unknown's avatar
unknown committed
1992
      store_record(table,record[1]);			// copy original row
unknown's avatar
unknown committed
1993 1994 1995 1996
    }

    table->field[6]->store((longlong) get_rights_for_column(privileges));

unknown's avatar
unknown committed
1997
    if (old_row_exists)
unknown's avatar
unknown committed
1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023
    {
      if (privileges)
	error=table->file->update_row(table->record[1],table->record[0]);
      else
	error=table->file->delete_row(table->record[1]);
      if (error)
      {
	table->file->print_error(error,MYF(0)); /* purecov: inspected */
	result= -1;				/* purecov: inspected */
	goto end;				/* purecov: inspected */
      }
      GRANT_COLUMN *grant_column = column_hash_search(g_t,
						      xx->column.ptr(),
						      xx->column.length());
      if (grant_column)				// Should always be true
	grant_column->rights = privileges;	// Update hash
    }
    else					// new grant
    {
      if ((error=table->file->write_row(table->record[0])))
      {
	table->file->print_error(error,MYF(0)); /* purecov: inspected */
	result= -1;				/* purecov: inspected */
	goto end;				/* purecov: inspected */
      }
      GRANT_COLUMN *grant_column = new GRANT_COLUMN(xx->column,privileges);
unknown's avatar
SCRUM  
unknown committed
2024
      my_hash_insert(&g_t->hash_columns,(byte*) grant_column);
unknown's avatar
unknown committed
2025 2026 2027 2028 2029 2030 2031 2032 2033 2034
    }
  }

  /*
    If revoke of privileges on the table level, remove all such privileges
    for all columns
  */

  if (revoke_grant)
  {
unknown's avatar
unknown committed
2035
    table->file->extra(HA_EXTRA_RETRIEVE_ALL_COLS);
unknown's avatar
unknown committed
2036
    if (table->file->index_read(table->record[0], (byte*) table->field[0]->ptr,
2037
                                key_length,
unknown's avatar
unknown committed
2038
				HA_READ_KEY_EXACT))
unknown's avatar
unknown committed
2039 2040
      goto end;

2041
    /* Scan through all rows with the same host,db,user and table */
unknown's avatar
unknown committed
2042 2043
    do
    {
unknown's avatar
unknown committed
2044
      ulong privileges = (ulong) table->field[6]->val_int();
unknown's avatar
unknown committed
2045
      privileges=fix_rights_for_column(privileges);
unknown's avatar
unknown committed
2046
      store_record(table,record[1]);
unknown's avatar
unknown committed
2047 2048 2049 2050 2051

      if (privileges & rights)	// is in this record the priv to be revoked ??
      {
	GRANT_COLUMN *grant_column = NULL;
	char  colum_name_buf[HOSTNAME_LENGTH+1];
unknown's avatar
unknown committed
2052
	String column_name(colum_name_buf,sizeof(colum_name_buf),&my_charset_latin1);
unknown's avatar
unknown committed
2053 2054 2055 2056

	privileges&= ~rights;
	table->field[6]->store((longlong)
			       get_rights_for_column(privileges));
2057
	table->field[4]->val_str(&column_name);
unknown's avatar
unknown committed
2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087
	grant_column = column_hash_search(g_t,
					  column_name.ptr(),
					  column_name.length());
	if (privileges)
	{
	  int tmp_error;
	  if ((tmp_error=table->file->update_row(table->record[1],
						 table->record[0])))
	  {					/* purecov: deadcode */
	    table->file->print_error(tmp_error,MYF(0)); /* purecov: deadcode */
	    result= -1;				/* purecov: deadcode */
	    goto end;				/* purecov: deadcode */
	  }
	  if (grant_column)
	    grant_column->rights  = privileges; // Update hash
	}
	else
	{
	  int tmp_error;
	  if ((tmp_error = table->file->delete_row(table->record[1])))
	  {					/* purecov: deadcode */
	    table->file->print_error(tmp_error,MYF(0)); /* purecov: deadcode */
	    result= -1;				/* purecov: deadcode */
	    goto end;				/* purecov: deadcode */
	  }
	  if (grant_column)
	    hash_delete(&g_t->hash_columns,(byte*) grant_column);
	}
      }
    } while (!table->file->index_next(table->record[0]) &&
unknown's avatar
unknown committed
2088
	     !key_cmp_if_same(table,key,0,key_length));
unknown's avatar
unknown committed
2089 2090
  }

2091
end:
unknown's avatar
unknown committed
2092
  table->file->ha_index_end();
unknown's avatar
unknown committed
2093 2094 2095 2096 2097 2098 2099
  DBUG_RETURN(result);
}


static int replace_table_table(THD *thd, GRANT_TABLE *grant_table,
			       TABLE *table, const LEX_USER &combo,
			       const char *db, const char *table_name,
unknown's avatar
unknown committed
2100 2101
			       ulong rights, ulong col_rights,
			       bool revoke_grant)
unknown's avatar
unknown committed
2102
{
2103
  char grantor[HOSTNAME_LENGTH+USERNAME_LENGTH+2];
unknown's avatar
unknown committed
2104
  int old_row_exists = 1;
unknown's avatar
unknown committed
2105
  int error=0;
unknown's avatar
unknown committed
2106
  ulong store_table_rights, store_col_rights;
unknown's avatar
unknown committed
2107 2108
  DBUG_ENTER("replace_table_table");

2109
  strxmov(grantor, thd->user, "@", thd->host_or_ip, NullS);
unknown's avatar
unknown committed
2110

unknown's avatar
unknown committed
2111 2112 2113 2114
  /*
    The following should always succeed as new users are created before
    this function is called!
  */
unknown's avatar
unknown committed
2115 2116 2117 2118 2119 2120
  if (!find_acl_user(combo.host.str,combo.user.str))
  {
    my_error(ER_PASSWORD_NO_MATCH,MYF(0));	/* purecov: deadcode */
    DBUG_RETURN(-1);				/* purecov: deadcode */
  }

unknown's avatar
unknown committed
2121
  restore_record(table,default_values);			// Get empty record
unknown's avatar
unknown committed
2122 2123 2124 2125
  table->field[0]->store(combo.host.str,combo.host.length, &my_charset_latin1);
  table->field[1]->store(db,(uint) strlen(db), &my_charset_latin1);
  table->field[2]->store(combo.user.str,combo.user.length, &my_charset_latin1);
  table->field[3]->store(table_name,(uint) strlen(table_name), &my_charset_latin1);
unknown's avatar
unknown committed
2126
  store_record(table,record[1]);			// store at pos 1
unknown's avatar
unknown committed
2127
  table->file->extra(HA_EXTRA_RETRIEVE_ALL_COLS);
unknown's avatar
unknown committed
2128
  if (table->file->index_read_idx(table->record[0],0,
unknown's avatar
unknown committed
2129 2130
				  (byte*) table->field[0]->ptr,
				  table->key_info[0].key_length,
unknown's avatar
unknown committed
2131 2132 2133 2134 2135 2136 2137 2138 2139
				  HA_READ_KEY_EXACT))
  {
    /*
      The following should never happen as we first check the in memory
      grant tables for the user.  There is however always a small change that
      the user has modified the grant tables directly.
    */
    if (revoke_grant)
    { // no row, no revoke
unknown's avatar
unknown committed
2140 2141 2142
      my_error(ER_NONEXISTING_TABLE_GRANT, MYF(0),
               combo.user.str, combo.host.str,
               table_name);		/* purecov: deadcode */
unknown's avatar
unknown committed
2143 2144
      DBUG_RETURN(-1);				/* purecov: deadcode */
    }
unknown's avatar
unknown committed
2145
    old_row_exists = 0;
unknown's avatar
unknown committed
2146
    restore_record(table,record[1]);			// Get saved record
unknown's avatar
unknown committed
2147 2148
  }

unknown's avatar
unknown committed
2149 2150
  store_table_rights= get_rights_for_table(rights);
  store_col_rights=   get_rights_for_column(col_rights);
unknown's avatar
unknown committed
2151
  if (old_row_exists)
unknown's avatar
unknown committed
2152
  {
unknown's avatar
unknown committed
2153
    ulong j,k;
unknown's avatar
unknown committed
2154
    store_record(table,record[1]);
unknown's avatar
unknown committed
2155 2156
    j = (ulong) table->field[6]->val_int();
    k = (ulong) table->field[7]->val_int();
unknown's avatar
unknown committed
2157 2158 2159

    if (revoke_grant)
    {
2160
      /* column rights are already fixed in mysql_table_grant */
unknown's avatar
unknown committed
2161 2162 2163 2164
      store_table_rights=j & ~store_table_rights;
    }
    else
    {
unknown's avatar
unknown committed
2165 2166
      store_table_rights|= j;
      store_col_rights|=   k;
unknown's avatar
unknown committed
2167 2168 2169
    }
  }

unknown's avatar
unknown committed
2170
  table->field[4]->store(grantor,(uint) strlen(grantor), &my_charset_latin1);
unknown's avatar
unknown committed
2171 2172 2173
  table->field[6]->store((longlong) store_table_rights);
  table->field[7]->store((longlong) store_col_rights);
  rights=fix_rights_for_table(store_table_rights);
unknown's avatar
unknown committed
2174
  col_rights=fix_rights_for_column(store_col_rights);
unknown's avatar
unknown committed
2175

unknown's avatar
unknown committed
2176
  if (old_row_exists)
unknown's avatar
unknown committed
2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192
  {
    if (store_table_rights || store_col_rights)
    {
      if ((error=table->file->update_row(table->record[1],table->record[0])))
	goto table_error;			/* purecov: deadcode */
    }
    else if ((error = table->file->delete_row(table->record[1])))
      goto table_error;				/* purecov: deadcode */
  }
  else
  {
    error=table->file->write_row(table->record[0]);
    if (error && error != HA_ERR_FOUND_DUPP_KEY)
      goto table_error;				/* purecov: deadcode */
  }

unknown's avatar
unknown committed
2193
  if (rights | col_rights)
unknown's avatar
unknown committed
2194
  {
unknown's avatar
unknown committed
2195
    grant_table->privs= rights;
2196
    grant_table->cols=	col_rights;
unknown's avatar
unknown committed
2197 2198 2199
  }
  else
  {
2200
    hash_delete(&column_priv_hash,(byte*) grant_table);
unknown's avatar
unknown committed
2201 2202 2203
  }
  DBUG_RETURN(0);

2204 2205
  /* This should never happen */
table_error:
unknown's avatar
unknown committed
2206 2207 2208 2209 2210
  table->file->print_error(error,MYF(0)); /* purecov: deadcode */
  DBUG_RETURN(-1); /* purecov: deadcode */
}


2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227
/*
  Store table level and column level grants in the privilege tables

  SYNOPSIS
    mysql_table_grant()
    thd			Thread handle
    table_list		List of tables to give grant
    user_list		List of users to give grant
    columns		List of columns to give grant
    rights		Table level grant
    revoke_grant	Set to 1 if this is a REVOKE command

  RETURN
    0	ok
    1	error
*/

unknown's avatar
unknown committed
2228 2229 2230 2231
int mysql_table_grant(THD *thd, TABLE_LIST *table_list,
		      List <LEX_USER> &user_list,
		      List <LEX_COLUMN> &columns, ulong rights,
		      bool revoke_grant)
unknown's avatar
unknown committed
2232
{
2233
  ulong column_priv= 0;
unknown's avatar
unknown committed
2234 2235 2236
  List_iterator <LEX_USER> str_list (user_list);
  LEX_USER *Str;
  TABLE_LIST tables[3];
unknown's avatar
unknown committed
2237
  bool create_new_users=0;
unknown's avatar
unknown committed
2238 2239 2240 2241
  DBUG_ENTER("mysql_table_grant");

  if (!initialized)
  {
unknown's avatar
unknown committed
2242 2243 2244
    my_error(ER_OPTION_PREVENTS_STATEMENT, MYF(0),
             "--skip-grant-tables");	/* purecov: inspected */
    DBUG_RETURN(-1);				/* purecov: inspected */
unknown's avatar
unknown committed
2245 2246 2247 2248 2249 2250 2251 2252 2253 2254
  }
  if (rights & ~TABLE_ACLS)
  {
    my_error(ER_ILLEGAL_GRANT_FOR_TABLE,MYF(0));
    DBUG_RETURN(-1);
  }

  if (columns.elements && !revoke_grant)
  {
    TABLE *table;
2255 2256
    class LEX_COLUMN *column;
    List_iterator <LEX_COLUMN> column_iter(columns);
unknown's avatar
unknown committed
2257 2258 2259

    if (!(table=open_ltable(thd,table_list,TL_READ)))
      DBUG_RETURN(-1);
2260
    while ((column = column_iter++))
unknown's avatar
unknown committed
2261
    {
2262
      uint unused_field_idx= NO_CACHED_FIELD_INDEX;
2263
      if (!find_field_in_table(thd,table,column->column.ptr(),
2264 2265
                               column->column.length(),0,0,
                               &unused_field_idx))
unknown's avatar
unknown committed
2266
      {
unknown's avatar
unknown committed
2267 2268
	my_error(ER_BAD_FIELD_ERROR, MYF(0),
                 column->column.c_ptr(), table_list->alias);
unknown's avatar
unknown committed
2269 2270
	DBUG_RETURN(-1);
      }
2271
      column_priv|= column->rights;
unknown's avatar
unknown committed
2272 2273 2274 2275 2276 2277
    }
    close_thread_tables(thd);
  }
  else if (!(rights & CREATE_ACL) && !revoke_grant)
  {
    char buf[FN_REFLEN];
2278 2279
    sprintf(buf,"%s/%s/%s.frm",mysql_data_home, table_list->db,
	    table_list->real_name);
unknown's avatar
unknown committed
2280 2281 2282
    fn_format(buf,buf,"","",4+16+32);
    if (access(buf,F_OK))
    {
unknown's avatar
unknown committed
2283
      my_error(ER_NO_SUCH_TABLE, MYF(0), table_list->db, table_list->alias);
unknown's avatar
unknown committed
2284 2285 2286 2287 2288 2289 2290
      DBUG_RETURN(-1);
    }
  }

  /* open the mysql.tables_priv and mysql.columns_priv tables */

  bzero((char*) &tables,sizeof(tables));
2291 2292 2293
  tables[0].alias=tables[0].real_name= (char*) "user";
  tables[1].alias=tables[1].real_name= (char*) "tables_priv";
  tables[2].alias=tables[2].real_name= (char*) "columns_priv";
unknown's avatar
unknown committed
2294 2295 2296 2297 2298 2299 2300 2301
  tables[0].next=tables+1;
  /* Don't open column table if we don't need it ! */
  tables[1].next=((column_priv ||
		   (revoke_grant && ((rights & COL_ACLS) || columns.elements)))
		  ? tables+2 : 0);
  tables[0].lock_type=tables[1].lock_type=tables[2].lock_type=TL_WRITE;
  tables[0].db=tables[1].db=tables[2].db=(char*) "mysql";

2302 2303 2304 2305 2306
#ifdef HAVE_REPLICATION
  /*
    GRANT and REVOKE are applied the slave in/exclusion rules as they are
    some kind of updates to the mysql.% tables.
  */
2307 2308
  if (thd->slave_thread && table_rules_on)
  {
unknown's avatar
unknown committed
2309 2310 2311
    /*
      The tables must be marked "updating" so that tables_ok() takes them into
      account in tests.
2312
    */
2313
    tables[0].updating= tables[1].updating= tables[2].updating= 1;
2314 2315 2316
    if (!tables_ok(0, tables))
      DBUG_RETURN(0);
  }
2317 2318
#endif

2319
  if (simple_open_n_lock_tables(thd,tables))
unknown's avatar
unknown committed
2320 2321 2322 2323 2324
  {						// Should never happen
    close_thread_tables(thd);			/* purecov: deadcode */
    DBUG_RETURN(-1);				/* purecov: deadcode */
  }

unknown's avatar
unknown committed
2325 2326
  if (!revoke_grant)
    create_new_users= test_if_create_new_users(thd);
unknown's avatar
unknown committed
2327
  int result=0;
2328
  rw_wrlock(&LOCK_grant);
unknown's avatar
unknown committed
2329 2330
  MEM_ROOT *old_root= thd->mem_root;
  thd->mem_root= &memex;
unknown's avatar
unknown committed
2331 2332 2333

  while ((Str = str_list++))
  {
unknown's avatar
unknown committed
2334
    int error;
unknown's avatar
unknown committed
2335 2336 2337 2338 2339 2340 2341 2342 2343
    GRANT_TABLE *grant_table;
    if (Str->host.length > HOSTNAME_LENGTH ||
	Str->user.length > USERNAME_LENGTH)
    {
      my_error(ER_GRANT_WRONG_HOST_OR_USER,MYF(0));
      result= -1;
      continue;
    }
    /* Create user if needed */
unknown's avatar
unknown committed
2344
    pthread_mutex_lock(&acl_cache->lock);
unknown's avatar
unknown committed
2345 2346
    error=replace_user_table(thd, tables[0].table, *Str,
			     0, revoke_grant, create_new_users);
unknown's avatar
unknown committed
2347 2348
    pthread_mutex_unlock(&acl_cache->lock);
    if (error)
unknown's avatar
unknown committed
2349 2350 2351 2352 2353 2354 2355 2356
    {
      result= -1;				// Remember error
      continue;					// Add next user
    }

    /* Find/create cached table grant */
    grant_table= table_hash_search(Str->host.str,NullS,table_list->db,
				   Str->user.str,
2357
				   table_list->real_name,1);
unknown's avatar
unknown committed
2358 2359 2360 2361
    if (!grant_table)
    {
      if (revoke_grant)
      {
unknown's avatar
unknown committed
2362 2363
	my_error(ER_NONEXISTING_TABLE_GRANT, MYF(0),
                 Str->user.str, Str->host.str, table_list->real_name);
unknown's avatar
unknown committed
2364 2365 2366 2367 2368
	result= -1;
	continue;
      }
      grant_table = new GRANT_TABLE (Str->host.str,table_list->db,
				     Str->user.str,
2369
				     table_list->real_name,
unknown's avatar
unknown committed
2370 2371 2372 2373 2374 2375 2376
				     rights,
				     column_priv);
      if (!grant_table)				// end of memory
      {
	result= -1;				/* purecov: deadcode */
	continue;				/* purecov: deadcode */
      }
unknown's avatar
SCRUM  
unknown committed
2377
      my_hash_insert(&column_priv_hash,(byte*) grant_table);
unknown's avatar
unknown committed
2378 2379 2380 2381 2382
    }

    /* If revoke_grant, calculate the new column privilege for tables_priv */
    if (revoke_grant)
    {
2383 2384
      class LEX_COLUMN *column;
      List_iterator <LEX_COLUMN> column_iter(columns);
unknown's avatar
unknown committed
2385 2386 2387
      GRANT_COLUMN *grant_column;

      /* Fix old grants */
2388
      while ((column = column_iter++))
unknown's avatar
unknown committed
2389 2390
      {
	grant_column = column_hash_search(grant_table,
2391 2392
					  column->column.ptr(),
					  column->column.length());
unknown's avatar
unknown committed
2393
	if (grant_column)
2394
	  grant_column->rights&= ~(column->rights | rights);
unknown's avatar
unknown committed
2395 2396
      }
      /* scan trough all columns to get new column grant */
2397
      column_priv= 0;
unknown's avatar
unknown committed
2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415
      for (uint idx=0 ; idx < grant_table->hash_columns.records ; idx++)
      {
	grant_column= (GRANT_COLUMN*) hash_element(&grant_table->hash_columns,
						   idx);
	grant_column->rights&= ~rights;		// Fix other columns
	column_priv|= grant_column->rights;
      }
    }
    else
    {
      column_priv|= grant_table->cols;
    }


    /* update table and columns */

    if (replace_table_table(thd,grant_table,tables[1].table,*Str,
			    table_list->db,
2416
			    table_list->real_name,
unknown's avatar
unknown committed
2417
			    rights, column_priv, revoke_grant))
2418 2419
    {
      /* Should only happen if table is crashed */
unknown's avatar
unknown committed
2420 2421 2422 2423 2424 2425 2426
      result= -1;			       /* purecov: deadcode */
    }
    else if (tables[2].table)
    {
      if ((replace_column_table(grant_table,tables[2].table, *Str,
				columns,
				table_list->db,
2427
				table_list->real_name,
unknown's avatar
unknown committed
2428 2429 2430 2431 2432 2433 2434
				rights, revoke_grant)))
      {
	result= -1;
      }
    }
  }
  grant_option=TRUE;
unknown's avatar
unknown committed
2435
  thd->mem_root= old_root;
2436
  rw_unlock(&LOCK_grant);
unknown's avatar
unknown committed
2437
  if (!result)
2438
    send_ok(thd);
2439
  /* Tables are automatically closed */
unknown's avatar
unknown committed
2440 2441 2442 2443
  DBUG_RETURN(result);
}


unknown's avatar
unknown committed
2444 2445
int mysql_grant(THD *thd, const char *db, List <LEX_USER> &list,
		ulong rights, bool revoke_grant)
unknown's avatar
unknown committed
2446 2447 2448
{
  List_iterator <LEX_USER> str_list (list);
  LEX_USER *Str;
unknown's avatar
unknown committed
2449
  char tmp_db[NAME_LEN+1];
unknown's avatar
unknown committed
2450
  bool create_new_users=0;
unknown's avatar
unknown committed
2451 2452 2453 2454
  TABLE_LIST tables[2];
  DBUG_ENTER("mysql_grant");
  if (!initialized)
  {
unknown's avatar
unknown committed
2455 2456
    my_error(ER_OPTION_PREVENTS_STATEMENT, MYF(0),
             "--skip-grant-tables");	/* purecov: tested */
2457
    DBUG_RETURN(-1);				/* purecov: tested */
unknown's avatar
unknown committed
2458 2459
  }

unknown's avatar
unknown committed
2460 2461 2462
  if (lower_case_table_names && db)
  {
    strmov(tmp_db,db);
2463
    my_casedn_str(files_charset_info, tmp_db);
unknown's avatar
unknown committed
2464 2465
    db=tmp_db;
  }
unknown's avatar
unknown committed
2466 2467

  /* open the mysql.user and mysql.db tables */
2468
  bzero((char*) &tables,sizeof(tables));
2469 2470
  tables[0].alias=tables[0].real_name=(char*) "user";
  tables[1].alias=tables[1].real_name=(char*) "db";
unknown's avatar
unknown committed
2471 2472 2473 2474 2475
  tables[0].next=tables+1;
  tables[1].next=0;
  tables[0].lock_type=tables[1].lock_type=TL_WRITE;
  tables[0].db=tables[1].db=(char*) "mysql";
  tables[0].table=tables[1].table=0;
2476 2477 2478 2479 2480 2481

#ifdef HAVE_REPLICATION
  /*
    GRANT and REVOKE are applied the slave in/exclusion rules as they are
    some kind of updates to the mysql.% tables.
  */
2482 2483
  if (thd->slave_thread && table_rules_on)
  {
unknown's avatar
unknown committed
2484 2485 2486
    /*
      The tables must be marked "updating" so that tables_ok() takes them into
      account in tests.
2487
    */
2488
    tables[0].updating= tables[1].updating= 1;
2489 2490 2491
    if (!tables_ok(0, tables))
      DBUG_RETURN(0);
  }
2492 2493
#endif

2494
  if (simple_open_n_lock_tables(thd,tables))
unknown's avatar
unknown committed
2495 2496 2497 2498 2499
  {						// This should never happen
    close_thread_tables(thd);			/* purecov: deadcode */
    DBUG_RETURN(-1);				/* purecov: deadcode */
  }

unknown's avatar
unknown committed
2500 2501
  if (!revoke_grant)
    create_new_users= test_if_create_new_users(thd);
unknown's avatar
unknown committed
2502

2503
  /* go through users in user_list */
2504
  rw_wrlock(&LOCK_grant);
unknown's avatar
unknown committed
2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517
  VOID(pthread_mutex_lock(&acl_cache->lock));
  grant_version++;

  int result=0;
  while ((Str = str_list++))
  {
    if (Str->host.length > HOSTNAME_LENGTH ||
	Str->user.length > USERNAME_LENGTH)
    {
      my_error(ER_GRANT_WRONG_HOST_OR_USER,MYF(0));
      result= -1;
      continue;
    }
2518
    if ((replace_user_table(thd,
2519
			    tables[0].table,
unknown's avatar
unknown committed
2520
			    *Str,
unknown's avatar
unknown committed
2521 2522
			    (!db ? rights : 0), revoke_grant,
			    create_new_users)))
unknown's avatar
unknown committed
2523
      result= -1;
unknown's avatar
unknown committed
2524
    else if (db)
unknown's avatar
unknown committed
2525
    {
unknown's avatar
unknown committed
2526 2527 2528 2529 2530 2531 2532 2533 2534
      ulong db_rights= rights & DB_ACLS;
      if (db_rights  == rights)
      {
	if (replace_db_table(tables[1].table, db, *Str, db_rights,
			     revoke_grant))
	  result= -1;
      }
      else
      {
unknown's avatar
unknown committed
2535
	my_error(ER_WRONG_USAGE, MYF(0), "DB GRANT", "GLOBAL PRIVILEGES");
unknown's avatar
unknown committed
2536
	result= -1;
unknown's avatar
unknown committed
2537
      }
unknown's avatar
unknown committed
2538
    }
unknown's avatar
unknown committed
2539 2540
  }
  VOID(pthread_mutex_unlock(&acl_cache->lock));
2541
  rw_unlock(&LOCK_grant);
unknown's avatar
unknown committed
2542 2543 2544
  close_thread_tables(thd);

  if (!result)
2545
    send_ok(thd);
unknown's avatar
unknown committed
2546 2547 2548
  DBUG_RETURN(result);
}

unknown's avatar
unknown committed
2549 2550

/* Free grant array if possible */
unknown's avatar
unknown committed
2551 2552 2553 2554 2555

void  grant_free(void)
{
  DBUG_ENTER("grant_free");
  grant_option = FALSE;
2556
  hash_free(&column_priv_hash);
2557
  free_root(&memex,MYF(0));
unknown's avatar
unknown committed
2558 2559 2560 2561 2562 2563
  DBUG_VOID_RETURN;
}


/* Init grant array if possible */

unknown's avatar
unknown committed
2564
my_bool grant_init(THD *org_thd)
unknown's avatar
unknown committed
2565
{
unknown's avatar
unknown committed
2566
  THD  *thd;
unknown's avatar
unknown committed
2567
  TABLE_LIST tables[2];
2568
  MYSQL_LOCK *lock;
unknown's avatar
unknown committed
2569
  MEM_ROOT *memex_ptr;
2570
  my_bool return_val= 1;
unknown's avatar
unknown committed
2571
  TABLE *t_table, *c_table;
unknown's avatar
SCRUM  
unknown committed
2572
  bool check_no_resolve= specialflag & SPECIAL_NO_RESOLVE;
unknown's avatar
unknown committed
2573 2574 2575
  DBUG_ENTER("grant_init");

  grant_option = FALSE;
unknown's avatar
unknown committed
2576
  (void) hash_init(&column_priv_hash,&my_charset_latin1,
unknown's avatar
unknown committed
2577
		   0,0,0, (hash_get_key) get_grant_table,
unknown's avatar
unknown committed
2578
		   (hash_free_key) free_grant_table,0);
2579
  init_sql_alloc(&memex, ACL_ALLOC_BLOCK_SIZE, 0);
unknown's avatar
unknown committed
2580

2581
  /* Don't do anything if running with --skip-grant */
unknown's avatar
unknown committed
2582 2583
  if (!initialized)
    DBUG_RETURN(0);				/* purecov: tested */
2584

unknown's avatar
unknown committed
2585 2586
  if (!(thd=new THD))
    DBUG_RETURN(1);				/* purecov: deadcode */
2587
  thd->store_globals();
unknown's avatar
unknown committed
2588 2589
  thd->db= my_strdup("mysql",MYF(0));
  thd->db_length=5;				// Safety
2590
  bzero((char*) &tables, sizeof(tables));
2591 2592
  tables[0].alias=tables[0].real_name= (char*) "tables_priv";
  tables[1].alias=tables[1].real_name= (char*) "columns_priv";
unknown's avatar
unknown committed
2593 2594 2595 2596
  tables[0].next=tables+1;
  tables[0].lock_type=tables[1].lock_type=TL_READ;
  tables[0].db=tables[1].db=thd->db;

unknown's avatar
unknown committed
2597 2598
  uint counter;
  if (open_tables(thd, tables, &counter))
2599 2600
    goto end;

unknown's avatar
unknown committed
2601 2602 2603
  TABLE *ptr[2];				// Lock tables for quick update
  ptr[0]= tables[0].table;
  ptr[1]= tables[1].table;
2604 2605
  if (!(lock=mysql_lock_tables(thd,ptr,2)))
    goto end;
unknown's avatar
unknown committed
2606 2607

  t_table = tables[0].table; c_table = tables[1].table;
unknown's avatar
unknown committed
2608
  t_table->file->ha_index_init(0);
unknown's avatar
unknown committed
2609 2610
  if (t_table->file->index_first(t_table->record[0]))
  {
2611
    return_val= 0;
2612
    goto end_unlock;
unknown's avatar
unknown committed
2613
  }
unknown's avatar
unknown committed
2614
  grant_option= TRUE;
unknown's avatar
unknown committed
2615

2616
  /* Will be restored by org_thd->store_globals() */
unknown's avatar
unknown committed
2617 2618
  memex_ptr= &memex;
  my_pthread_setspecific_ptr(THR_MALLOC, &memex_ptr);
2619
  do
unknown's avatar
unknown committed
2620 2621
  {
    GRANT_TABLE *mem_check;
unknown's avatar
unknown committed
2622
    if (!(mem_check=new GRANT_TABLE(t_table,c_table)))
unknown's avatar
unknown committed
2623 2624
    {
      /* This could only happen if we are out memory */
unknown's avatar
unknown committed
2625
      grant_option= FALSE;			/* purecov: deadcode */
2626
      goto end_unlock;
unknown's avatar
unknown committed
2627
    }
unknown's avatar
SCRUM  
unknown committed
2628 2629 2630 2631 2632

    if (check_no_resolve)
    {
      if (hostname_requires_resolving(mem_check->host))
      {
unknown's avatar
unknown committed
2633 2634 2635 2636
        sql_print_warning("'tables_priv' entry '%s %s@%s' "
                          "ignored in --skip-name-resolve mode.",
                          mem_check->tname, mem_check->user,
                          mem_check->host, mem_check->host);
unknown's avatar
SCRUM  
unknown committed
2637 2638 2639 2640
	continue;
      }
    }

unknown's avatar
unknown committed
2641
    if (mem_check->ok() && my_hash_insert(&column_priv_hash,(byte*) mem_check))
unknown's avatar
SCRUM  
unknown committed
2642 2643 2644 2645
    {
      grant_option= FALSE;
      goto end_unlock;
    }
unknown's avatar
unknown committed
2646
  }
2647 2648 2649 2650 2651
  while (!t_table->file->index_next(t_table->record[0]));

  return_val=0;					// Return ok

end_unlock:
unknown's avatar
unknown committed
2652
  t_table->file->ha_index_end();
unknown's avatar
unknown committed
2653 2654
  mysql_unlock_tables(thd, lock);
  thd->version--;				// Force close to free memory
2655 2656

end:
unknown's avatar
unknown committed
2657 2658
  close_thread_tables(thd);
  delete thd;
2659 2660
  if (org_thd)
    org_thd->store_globals();
unknown's avatar
unknown committed
2661 2662 2663 2664 2665
  else
  {
    /* Remember that we don't have a THD */
    my_pthread_setspecific_ptr(THR_THD,  0);
  }
2666
  DBUG_RETURN(return_val);
unknown's avatar
unknown committed
2667 2668 2669
}


2670
/*
unknown's avatar
unknown committed
2671
 Reload grant array (table and column privileges) if possible
2672 2673 2674 2675 2676 2677 2678 2679

  SYNOPSIS
    grant_reload()
    thd			Thread handler

  NOTES
    Locked tables are checked by acl_init and doesn't have to be checked here
*/
unknown's avatar
unknown committed
2680

unknown's avatar
unknown committed
2681
void grant_reload(THD *thd)
unknown's avatar
unknown committed
2682
{
2683 2684
  HASH old_column_priv_hash;
  bool old_grant_option;
unknown's avatar
unknown committed
2685 2686 2687
  MEM_ROOT old_mem;
  DBUG_ENTER("grant_reload");

2688
  rw_wrlock(&LOCK_grant);
unknown's avatar
unknown committed
2689
  grant_version++;
2690
  old_column_priv_hash= column_priv_hash;
unknown's avatar
unknown committed
2691
  old_grant_option= grant_option;
unknown's avatar
unknown committed
2692
  old_mem= memex;
unknown's avatar
unknown committed
2693

unknown's avatar
unknown committed
2694
  if (grant_init(thd))
unknown's avatar
unknown committed
2695
  {						// Error. Revert to old hash
2696
    DBUG_PRINT("error",("Reverting to old privileges"));
unknown's avatar
unknown committed
2697
    grant_free();				/* purecov: deadcode */
2698
    column_priv_hash= old_column_priv_hash;	/* purecov: deadcode */
unknown's avatar
unknown committed
2699
    grant_option= old_grant_option;		/* purecov: deadcode */
unknown's avatar
unknown committed
2700
    memex= old_mem;				/* purecov: deadcode */
unknown's avatar
unknown committed
2701 2702 2703
  }
  else
  {
2704
    hash_free(&old_column_priv_hash);
2705
    free_root(&old_mem,MYF(0));
unknown's avatar
unknown committed
2706
  }
2707
  rw_unlock(&LOCK_grant);
unknown's avatar
unknown committed
2708 2709 2710 2711 2712
  DBUG_VOID_RETURN;
}


/****************************************************************************
2713
  Check table level grants
2714
  All errors are written directly to the client if no_errors is given !
unknown's avatar
unknown committed
2715 2716
****************************************************************************/

unknown's avatar
unknown committed
2717
bool check_grant(THD *thd, ulong want_access, TABLE_LIST *tables,
unknown's avatar
unknown committed
2718
		 uint show_table, uint number, bool no_errors)
unknown's avatar
unknown committed
2719 2720 2721
{
  TABLE_LIST *table;
  char *user = thd->priv_user;
unknown's avatar
unknown committed
2722
  DBUG_ENTER("check_grant");
unknown's avatar
unknown committed
2723 2724 2725

  want_access &= ~thd->master_access;
  if (!want_access)
unknown's avatar
unknown committed
2726
    DBUG_RETURN(0);                             // ok
unknown's avatar
unknown committed
2727

2728
  rw_rdlock(&LOCK_grant);
unknown's avatar
unknown committed
2729
  for (table= tables; table && number--; table= table->next)
unknown's avatar
unknown committed
2730
  {
2731
    if (!(~table->grant.privilege & want_access) || table->derived)
unknown's avatar
unknown committed
2732 2733 2734 2735
    {
      table->grant.want_privilege=0;
      continue;					// Already checked
    }
unknown's avatar
unknown committed
2736 2737
    GRANT_TABLE *grant_table = table_hash_search(thd->host,thd->ip,
						 table->db,user,
unknown's avatar
unknown committed
2738 2739 2740 2741 2742 2743
						 table->real_name,0);
    if (!grant_table)
    {
      want_access &= ~table->grant.privilege;
      goto err;					// No grants
    }
unknown's avatar
unknown committed
2744 2745
    if (show_table)
      continue;					// We have some priv on this
unknown's avatar
unknown committed
2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761

    table->grant.grant_table=grant_table;	// Remember for column test
    table->grant.version=grant_version;
    table->grant.privilege|= grant_table->privs;
    table->grant.want_privilege= ((want_access & COL_ACLS)
				  & ~table->grant.privilege);

    if (!(~table->grant.privilege & want_access))
      continue;

    if (want_access & ~(grant_table->cols | table->grant.privilege))
    {
      want_access &= ~(grant_table->cols | table->grant.privilege);
      goto err;					// impossible
    }
  }
2762
  rw_unlock(&LOCK_grant);
unknown's avatar
unknown committed
2763
  DBUG_RETURN(0);
unknown's avatar
unknown committed
2764

2765
err:
2766
  rw_unlock(&LOCK_grant);
unknown's avatar
unknown committed
2767
  if (!no_errors)				// Not a silent skip of table
unknown's avatar
unknown committed
2768 2769 2770
  {
    const char *command="";
    if (want_access & SELECT_ACL)
unknown's avatar
unknown committed
2771
      command= "select";
unknown's avatar
unknown committed
2772
    else if (want_access & INSERT_ACL)
unknown's avatar
unknown committed
2773
      command= "insert";
unknown's avatar
unknown committed
2774
    else if (want_access & UPDATE_ACL)
unknown's avatar
unknown committed
2775
      command= "update";
unknown's avatar
unknown committed
2776
    else if (want_access & DELETE_ACL)
unknown's avatar
unknown committed
2777
      command= "delete";
unknown's avatar
unknown committed
2778
    else if (want_access & DROP_ACL)
unknown's avatar
unknown committed
2779
      command= "drop";
unknown's avatar
unknown committed
2780
    else if (want_access & CREATE_ACL)
unknown's avatar
unknown committed
2781
      command= "create";
unknown's avatar
unknown committed
2782
    else if (want_access & ALTER_ACL)
unknown's avatar
unknown committed
2783
      command= "alter";
unknown's avatar
unknown committed
2784
    else if (want_access & INDEX_ACL)
unknown's avatar
unknown committed
2785
      command= "index";
unknown's avatar
unknown committed
2786
    else if (want_access & GRANT_ACL)
unknown's avatar
unknown committed
2787
      command= "grant";
2788
    net_printf(thd,ER_TABLEACCESS_DENIED_ERROR,
unknown's avatar
unknown committed
2789 2790
	       command,
	       thd->priv_user,
2791
	       thd->host_or_ip,
unknown's avatar
unknown committed
2792 2793
	       table ? table->real_name : "unknown");
  }
unknown's avatar
unknown committed
2794
  DBUG_RETURN(1);
unknown's avatar
unknown committed
2795 2796 2797
}


2798 2799
bool check_grant_column(THD *thd,TABLE *table, const char *name,
			uint length, uint show_tables)
unknown's avatar
unknown committed
2800 2801 2802 2803
{
  GRANT_TABLE *grant_table;
  GRANT_COLUMN *grant_column;

unknown's avatar
unknown committed
2804
  ulong want_access=table->grant.want_privilege;
unknown's avatar
unknown committed
2805 2806 2807
  if (!want_access)
    return 0;					// Already checked

2808
  rw_rdlock(&LOCK_grant);
unknown's avatar
unknown committed
2809

2810
  /* reload table if someone has modified any grants */
unknown's avatar
unknown committed
2811 2812 2813 2814

  if (table->grant.version != grant_version)
  {
    table->grant.grant_table=
2815
      table_hash_search(thd->host, thd->ip, table->table_cache_key,
unknown's avatar
unknown committed
2816
			thd->priv_user,
2817
			table->real_name, 0);	/* purecov: inspected */
unknown's avatar
unknown committed
2818 2819 2820 2821 2822 2823 2824 2825
    table->grant.version=grant_version;		/* purecov: inspected */
  }
  if (!(grant_table=table->grant.grant_table))
    goto err;					/* purecov: deadcode */

  grant_column=column_hash_search(grant_table, name, length);
  if (grant_column && !(~grant_column->rights & want_access))
  {
2826
    rw_unlock(&LOCK_grant);
unknown's avatar
unknown committed
2827 2828 2829 2830 2831
    return 0;
  }
#ifdef NOT_USED
  if (show_tables && (grant_column || table->grant.privilege & COL_ACLS))
  {
2832
    rw_unlock(&LOCK_grant);			/* purecov: deadcode */
unknown's avatar
unknown committed
2833 2834 2835 2836 2837
    return 0;					/* purecov: deadcode */
  }
#endif

  /* We must use my_printf_error() here! */
2838
err:
2839
  rw_unlock(&LOCK_grant);
unknown's avatar
unknown committed
2840 2841
  if (!show_tables)
  {
unknown's avatar
unknown committed
2842 2843
    char command[128];
    get_privilege_desc(command, sizeof(command), want_access);
unknown's avatar
unknown committed
2844 2845 2846 2847 2848
    my_printf_error(ER_COLUMNACCESS_DENIED_ERROR,
		    ER(ER_COLUMNACCESS_DENIED_ERROR),
		    MYF(0),
		    command,
		    thd->priv_user,
2849
		    thd->host_or_ip,
unknown's avatar
unknown committed
2850 2851 2852 2853 2854 2855 2856
		    name,
		    table ? table->real_name : "unknown");
  }
  return 1;
}


unknown's avatar
unknown committed
2857
bool check_grant_all_columns(THD *thd, ulong want_access, TABLE *table)
unknown's avatar
unknown committed
2858 2859 2860 2861 2862 2863 2864
{
  GRANT_TABLE *grant_table;
  GRANT_COLUMN *grant_column;
  Field *field=0,**ptr;

  want_access &= ~table->grant.privilege;
  if (!want_access)
unknown's avatar
unknown committed
2865
    return 0;				// Already checked
unknown's avatar
unknown committed
2866
  if (!grant_option)
unknown's avatar
unknown committed
2867 2868
  {
    field= table->field[0];		// To give a meaningful error message
unknown's avatar
unknown committed
2869
    goto err2;
unknown's avatar
unknown committed
2870
  }
unknown's avatar
unknown committed
2871

2872
  rw_rdlock(&LOCK_grant);
unknown's avatar
unknown committed
2873

2874
  /* reload table if someone has modified any grants */
unknown's avatar
unknown committed
2875 2876 2877 2878

  if (table->grant.version != grant_version)
  {
    table->grant.grant_table=
unknown's avatar
unknown committed
2879
      table_hash_search(thd->host, thd->ip, table->table_cache_key,
unknown's avatar
unknown committed
2880 2881 2882 2883
			thd->priv_user,
			table->real_name,0);	/* purecov: inspected */
    table->grant.version=grant_version;		/* purecov: inspected */
  }
2884
  /* The following should always be true */
unknown's avatar
unknown committed
2885 2886 2887 2888 2889 2890
  if (!(grant_table=table->grant.grant_table))
    goto err;					/* purecov: inspected */

  for (ptr=table->field; (field= *ptr) ; ptr++)
  {
    grant_column=column_hash_search(grant_table, field->field_name,
unknown's avatar
unknown committed
2891
				    (uint) strlen(field->field_name));
unknown's avatar
unknown committed
2892 2893 2894
    if (!grant_column || (~grant_column->rights & want_access))
      goto err;
  }
2895
  rw_unlock(&LOCK_grant);
unknown's avatar
unknown committed
2896 2897 2898
  return 0;

  /* We must use my_printf_error() here! */
unknown's avatar
unknown committed
2899
err:
2900
  rw_unlock(&LOCK_grant);
unknown's avatar
unknown committed
2901
err2:
unknown's avatar
unknown committed
2902
  const char *command= "";
unknown's avatar
unknown committed
2903
  if (want_access & SELECT_ACL)
unknown's avatar
unknown committed
2904
    command= "select";
unknown's avatar
unknown committed
2905
  else if (want_access & INSERT_ACL)
unknown's avatar
unknown committed
2906
    command= "insert";
unknown's avatar
unknown committed
2907 2908 2909 2910 2911
  my_printf_error(ER_COLUMNACCESS_DENIED_ERROR,
		  ER(ER_COLUMNACCESS_DENIED_ERROR),
		  MYF(0),
		  command,
		  thd->priv_user,
2912
		  thd->host_or_ip,
unknown's avatar
unknown committed
2913 2914 2915 2916 2917 2918
		  field ? field->field_name : "unknown",
		  table->real_name);
  return 1;
}


2919
/*
unknown's avatar
unknown committed
2920 2921 2922
  Check if a user has the right to access a database
  Access is accepted if he has a grant for any table in the database
  Return 1 if access is denied
2923
*/
unknown's avatar
unknown committed
2924 2925 2926 2927 2928 2929 2930 2931

bool check_grant_db(THD *thd,const char *db)
{
  char helping [NAME_LEN+USERNAME_LENGTH+2];
  uint len;
  bool error=1;

  len  = (uint) (strmov(strmov(helping,thd->priv_user)+1,db)-helping)+ 1;
2932
  rw_rdlock(&LOCK_grant);
unknown's avatar
unknown committed
2933

2934
  for (uint idx=0 ; idx < column_priv_hash.records ; idx++)
unknown's avatar
unknown committed
2935
  {
2936 2937
    GRANT_TABLE *grant_table= (GRANT_TABLE*) hash_element(&column_priv_hash,
							  idx);
unknown's avatar
unknown committed
2938 2939
    if (len < grant_table->key_length &&
	!memcmp(grant_table->hash_key,helping,len) &&
unknown's avatar
unknown committed
2940
	(thd->host && !wild_case_compare(&my_charset_latin1,
2941
                                         thd->host,grant_table->host) ||
unknown's avatar
unknown committed
2942
	 (thd->ip && !wild_case_compare(&my_charset_latin1,
2943
                                        thd->ip,grant_table->host))))
unknown's avatar
unknown committed
2944 2945 2946 2947 2948
    {
      error=0;					// Found match
      break;
    }
  }
2949
  rw_unlock(&LOCK_grant);
unknown's avatar
unknown committed
2950 2951 2952 2953
  return error;
}

/*****************************************************************************
unknown's avatar
unknown committed
2954
  Functions to retrieve the grant for a table/column  (for SHOW functions)
unknown's avatar
unknown committed
2955 2956
*****************************************************************************/

unknown's avatar
unknown committed
2957
ulong get_table_grant(THD *thd, TABLE_LIST *table)
unknown's avatar
unknown committed
2958
{
unknown's avatar
unknown committed
2959
  ulong privilege;
unknown's avatar
unknown committed
2960 2961 2962 2963
  char *user = thd->priv_user;
  const char *db = table->db ? table->db : thd->db;
  GRANT_TABLE *grant_table;

2964
  rw_rdlock(&LOCK_grant);
2965 2966 2967
#ifdef EMBEDDED_LIBRARY
  grant_table= NULL;
#else
unknown's avatar
unknown committed
2968 2969
  grant_table= table_hash_search(thd->host, thd->ip, db, user,
				 table->real_name, 0);
2970
#endif
unknown's avatar
unknown committed
2971 2972 2973 2974
  table->grant.grant_table=grant_table; // Remember for column test
  table->grant.version=grant_version;
  if (grant_table)
    table->grant.privilege|= grant_table->privs;
unknown's avatar
unknown committed
2975
  privilege= table->grant.privilege;
2976
  rw_unlock(&LOCK_grant);
unknown's avatar
unknown committed
2977
  return privilege;
unknown's avatar
unknown committed
2978 2979 2980
}


unknown's avatar
unknown committed
2981
ulong get_column_grant(THD *thd, TABLE_LIST *table, Field *field)
unknown's avatar
unknown committed
2982 2983 2984
{
  GRANT_TABLE *grant_table;
  GRANT_COLUMN *grant_column;
unknown's avatar
unknown committed
2985
  ulong priv;
unknown's avatar
unknown committed
2986

2987
  rw_rdlock(&LOCK_grant);
2988
  /* reload table if someone has modified any grants */
unknown's avatar
unknown committed
2989 2990 2991
  if (table->grant.version != grant_version)
  {
    table->grant.grant_table=
unknown's avatar
unknown committed
2992
      table_hash_search(thd->host, thd->ip, table->db,
unknown's avatar
unknown committed
2993 2994 2995 2996 2997 2998 2999 3000 3001 3002
			thd->priv_user,
			table->real_name,0);	/* purecov: inspected */
    table->grant.version=grant_version;		/* purecov: inspected */
  }

  if (!(grant_table=table->grant.grant_table))
    priv=table->grant.privilege;
  else
  {
    grant_column=column_hash_search(grant_table, field->field_name,
unknown's avatar
unknown committed
3003
				    (uint) strlen(field->field_name));
unknown's avatar
unknown committed
3004 3005 3006 3007 3008
    if (!grant_column)
      priv=table->grant.privilege;
    else
      priv=table->grant.privilege | grant_column->rights;
  }
3009
  rw_unlock(&LOCK_grant);
unknown's avatar
unknown committed
3010 3011 3012
  return priv;
}

3013
/* Help function for mysql_show_grants */
unknown's avatar
unknown committed
3014

3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026
static void add_user_option(String *grant, ulong value, const char *name)
{
  if (value)
  {
    char buff[22], *p; // just as in int2str
    grant->append(' ');
    grant->append(name, strlen(name));
    grant->append(' ');
    p=int10_to_str(value, buff, 10);
    grant->append(buff,p-buff);
  }
}
unknown's avatar
unknown committed
3027 3028

static const char *command_array[]=
unknown's avatar
unknown committed
3029 3030 3031 3032 3033 3034
{
  "SELECT", "INSERT","UPDATE","DELETE","CREATE", "DROP", "RELOAD","SHUTDOWN",
  "PROCESS","FILE","GRANT","REFERENCES","INDEX", "ALTER", "SHOW DATABASES",
  "SUPER", "CREATE TEMPORARY TABLES", "LOCK TABLES", "EXECUTE",
  "REPLICATION SLAVE", "REPLICATION CLIENT",
};
3035

unknown's avatar
unknown committed
3036 3037 3038 3039 3040
static uint command_lengths[]=
{
  6,6,6,6,6,4,6,8,7,4,5,10,5,5,14,5,23,11,7,17,18
};

unknown's avatar
unknown committed
3041

3042 3043 3044 3045 3046 3047 3048
/*
  SHOW GRANTS;  Send grants for a user to the client

  IMPLEMENTATION
   Send to client grant-like strings depicting user@host privileges
*/

unknown's avatar
unknown committed
3049
int mysql_show_grants(THD *thd,LEX_USER *lex_user)
unknown's avatar
unknown committed
3050
{
unknown's avatar
unknown committed
3051 3052
  ulong want_access;
  uint counter,index;
unknown's avatar
unknown committed
3053
  int  error = 0;
unknown's avatar
unknown committed
3054 3055
  ACL_USER *acl_user;
  ACL_DB *acl_db;
unknown's avatar
unknown committed
3056
  char buff[1024];
unknown's avatar
unknown committed
3057
  Protocol *protocol= thd->protocol;
unknown's avatar
unknown committed
3058
  DBUG_ENTER("mysql_show_grants");
unknown's avatar
unknown committed
3059 3060 3061 3062

  LINT_INIT(acl_user);
  if (!initialized)
  {
unknown's avatar
unknown committed
3063 3064
    my_error(ER_OPTION_PREVENTS_STATEMENT, MYF(0), "--skip-grant-tables");
    DBUG_RETURN(-1);
unknown's avatar
unknown committed
3065
  }
unknown's avatar
unknown committed
3066 3067 3068 3069 3070 3071

  if (!lex_user->host.str)
  {
    lex_user->host.str= (char*) "%";
    lex_user->host.length=1;
  }
unknown's avatar
unknown committed
3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083
  if (lex_user->host.length > HOSTNAME_LENGTH ||
      lex_user->user.length > USERNAME_LENGTH)
  {
    my_error(ER_GRANT_WRONG_HOST_OR_USER,MYF(0));
    DBUG_RETURN(-1);
  }

  for (counter=0 ; counter < acl_users.elements ; counter++)
  {
    const char *user,*host;
    acl_user=dynamic_element(&acl_users,counter,ACL_USER*);
    if (!(user=acl_user->user))
unknown's avatar
unknown committed
3084
      user= "";
unknown's avatar
unknown committed
3085
    if (!(host=acl_user->host.hostname))
unknown's avatar
unknown committed
3086
      host= "";
unknown's avatar
unknown committed
3087
    if (!strcmp(lex_user->user.str,user) &&
unknown's avatar
unknown committed
3088
	!my_strcasecmp(&my_charset_latin1, lex_user->host.str, host))
unknown's avatar
unknown committed
3089 3090
      break;
  }
unknown's avatar
unknown committed
3091
  if (counter == acl_users.elements)
unknown's avatar
unknown committed
3092
  {
unknown's avatar
unknown committed
3093 3094
    my_error(ER_NONEXISTING_GRANT, MYF(0),
             lex_user->user.str, lex_user->host.str);
unknown's avatar
unknown committed
3095 3096 3097
    DBUG_RETURN(-1);
  }

unknown's avatar
unknown committed
3098
  Item_string *field=new Item_string("",0,&my_charset_latin1);
unknown's avatar
unknown committed
3099 3100 3101 3102 3103 3104
  List<Item> field_list;
  field->name=buff;
  field->max_length=1024;
  strxmov(buff,"Grants for ",lex_user->user.str,"@",
	  lex_user->host.str,NullS);
  field_list.push_back(field);
unknown's avatar
unknown committed
3105
  if (protocol->send_fields(&field_list,1))
unknown's avatar
unknown committed
3106 3107
    DBUG_RETURN(-1);

unknown's avatar
unknown committed
3108
  rw_wrlock(&LOCK_grant);
unknown's avatar
unknown committed
3109 3110 3111 3112
  VOID(pthread_mutex_lock(&acl_cache->lock));

  /* Add first global access grants */
  {
3113
    String global(buff,sizeof(buff),system_charset_info);
unknown's avatar
unknown committed
3114 3115 3116
    global.length(0);
    global.append("GRANT ",6);

unknown's avatar
unknown committed
3117
    want_access= acl_user->access;
unknown's avatar
unknown committed
3118 3119 3120 3121
    if (test_all_bits(want_access, (GLOBAL_ACLS & ~ GRANT_ACL)))
      global.append("ALL PRIVILEGES",14);
    else if (!(want_access & ~GRANT_ACL))
      global.append("USAGE",5);
unknown's avatar
unknown committed
3122
    else
unknown's avatar
unknown committed
3123 3124
    {
      bool found=0;
unknown's avatar
unknown committed
3125
      ulong j,test_access= want_access & ~GRANT_ACL;
unknown's avatar
unknown committed
3126 3127
      for (counter=0, j = SELECT_ACL;j <= GLOBAL_ACLS;counter++,j <<= 1)
      {
unknown's avatar
unknown committed
3128
	if (test_access & j)
unknown's avatar
unknown committed
3129 3130 3131 3132 3133 3134 3135 3136 3137
	{
	  if (found)
	    global.append(", ",2);
	  found=1;
	  global.append(command_array[counter],command_lengths[counter]);
	}
      }
    }
    global.append (" ON *.* TO '",12);
3138 3139
    global.append(lex_user->user.str, lex_user->user.length,
		  system_charset_info);
unknown's avatar
unknown committed
3140 3141 3142
    global.append ("'@'",3);
    global.append(lex_user->host.str,lex_user->host.length);
    global.append ('\'');
3143
    if (acl_user->salt_len)
unknown's avatar
unknown committed
3144
    {
3145 3146 3147 3148 3149
      char passwd_buff[SCRAMBLED_PASSWORD_CHAR_LENGTH+1];
      if (acl_user->salt_len == SCRAMBLE_LENGTH)
        make_password_from_salt(passwd_buff, acl_user->salt);
      else
        make_password_from_salt_323(passwd_buff, (ulong *) acl_user->salt);
unknown's avatar
unknown committed
3150
      global.append(" IDENTIFIED BY PASSWORD '",25);
3151
      global.append(passwd_buff);
unknown's avatar
unknown committed
3152 3153
      global.append('\'');
    }
unknown's avatar
unknown committed
3154 3155
    /* "show grants" SSL related stuff */
    if (acl_user->ssl_type == SSL_TYPE_ANY)
3156
      global.append(" REQUIRE SSL",12);
3157
    else if (acl_user->ssl_type == SSL_TYPE_X509)
3158
      global.append(" REQUIRE X509",13);
3159
    else if (acl_user->ssl_type == SSL_TYPE_SPECIFIED)
3160
    {
3161
      int ssl_options = 0;
3162
      global.append(" REQUIRE ",9);
unknown's avatar
unknown committed
3163 3164
      if (acl_user->x509_issuer)
      {
3165 3166 3167
	ssl_options++;
	global.append("ISSUER \'",8);
	global.append(acl_user->x509_issuer,strlen(acl_user->x509_issuer));
3168
	global.append('\'');
3169
      }
unknown's avatar
unknown committed
3170 3171
      if (acl_user->x509_subject)
      {
3172 3173 3174 3175
	if (ssl_options++)
	  global.append(' ');
	global.append("SUBJECT \'",9);
	global.append(acl_user->x509_subject,strlen(acl_user->x509_subject));
3176
	global.append('\'');
unknown's avatar
unknown committed
3177
      }
unknown's avatar
unknown committed
3178 3179
      if (acl_user->ssl_cipher)
      {
3180 3181 3182 3183
	if (ssl_options++)
	  global.append(' ');
	global.append("CIPHER '",8);
	global.append(acl_user->ssl_cipher,strlen(acl_user->ssl_cipher));
3184
	global.append('\'');
3185 3186
      }
    }
unknown's avatar
unknown committed
3187 3188 3189
    if ((want_access & GRANT_ACL) ||
	(acl_user->user_resource.questions | acl_user->user_resource.updates |
	 acl_user->user_resource.connections))
3190
    {
unknown's avatar
unknown committed
3191
      global.append(" WITH",5);
unknown's avatar
unknown committed
3192
      if (want_access & GRANT_ACL)
unknown's avatar
unknown committed
3193
	global.append(" GRANT OPTION",13);
3194 3195 3196 3197 3198 3199
      add_user_option(&global, acl_user->user_resource.questions,
		      "MAX_QUERIES_PER_HOUR");
      add_user_option(&global, acl_user->user_resource.updates,
		      "MAX_UPDATES_PER_HOUR");
      add_user_option(&global, acl_user->user_resource.connections,
		      "MAX_CONNECTIONS_PER_HOUR");
unknown's avatar
unknown committed
3200
    }
unknown's avatar
unknown committed
3201
    protocol->prepare_for_resend();
3202
    protocol->store(global.ptr(),global.length(),global.charset());
unknown's avatar
unknown committed
3203
    if (protocol->write())
unknown's avatar
unknown committed
3204
    {
unknown's avatar
unknown committed
3205
      error= -1;
unknown's avatar
unknown committed
3206
      goto end;
unknown's avatar
unknown committed
3207 3208 3209 3210 3211 3212
    }
  }

  /* Add database access */
  for (counter=0 ; counter < acl_dbs.elements ; counter++)
  {
unknown's avatar
unknown committed
3213
    const char *user, *host;
unknown's avatar
unknown committed
3214 3215 3216

    acl_db=dynamic_element(&acl_dbs,counter,ACL_DB*);
    if (!(user=acl_db->user))
unknown's avatar
unknown committed
3217
      user= "";
unknown's avatar
unknown committed
3218
    if (!(host=acl_db->host.hostname))
unknown's avatar
unknown committed
3219
      host= "";
unknown's avatar
unknown committed
3220 3221

    if (!strcmp(lex_user->user.str,user) &&
unknown's avatar
unknown committed
3222
	!my_strcasecmp(&my_charset_latin1, lex_user->host.str, host))
unknown's avatar
unknown committed
3223 3224
    {
      want_access=acl_db->access;
unknown's avatar
unknown committed
3225
      if (want_access)
unknown's avatar
unknown committed
3226
      {
3227
	String db(buff,sizeof(buff),system_charset_info);
unknown's avatar
unknown committed
3228 3229 3230 3231 3232
	db.length(0);
	db.append("GRANT ",6);

	if (test_all_bits(want_access,(DB_ACLS & ~GRANT_ACL)))
	  db.append("ALL PRIVILEGES",14);
unknown's avatar
unknown committed
3233
	else if (!(want_access & ~GRANT_ACL))
3234
	  db.append("USAGE",5);
unknown's avatar
unknown committed
3235 3236 3237
	else
	{
	  int found=0, cnt;
unknown's avatar
unknown committed
3238
	  ulong j,test_access= want_access & ~GRANT_ACL;
unknown's avatar
unknown committed
3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249
	  for (cnt=0, j = SELECT_ACL; j <= DB_ACLS; cnt++,j <<= 1)
	  {
	    if (test_access & j)
	    {
	      if (found)
		db.append(", ",2);
	      found = 1;
	      db.append(command_array[cnt],command_lengths[cnt]);
	    }
	  }
	}
3250 3251 3252
	db.append (" ON ",4);
	append_identifier(thd, &db, acl_db->db, strlen(acl_db->db));
	db.append (".* TO '",7);
3253 3254
	db.append(lex_user->user.str, lex_user->user.length,
		  system_charset_info);
unknown's avatar
unknown committed
3255 3256
	db.append ("'@'",3);
	db.append(lex_user->host.str, lex_user->host.length);
unknown's avatar
unknown committed
3257
	db.append ('\'');
unknown's avatar
unknown committed
3258
	if (want_access & GRANT_ACL)
unknown's avatar
unknown committed
3259
	  db.append(" WITH GRANT OPTION",18);
unknown's avatar
unknown committed
3260
	protocol->prepare_for_resend();
3261
	protocol->store(db.ptr(),db.length(),db.charset());
unknown's avatar
unknown committed
3262
	if (protocol->write())
unknown's avatar
unknown committed
3263
	{
unknown's avatar
unknown committed
3264
	  error= -1;
unknown's avatar
unknown committed
3265 3266 3267 3268 3269 3270
	  goto end;
	}
      }
    }
  }

3271
  /* Add table & column access */
3272
  for (index=0 ; index < column_priv_hash.records ; index++)
unknown's avatar
unknown committed
3273
  {
unknown's avatar
unknown committed
3274
    const char *user;
3275 3276
    GRANT_TABLE *grant_table= (GRANT_TABLE*) hash_element(&column_priv_hash,
							  index);
unknown's avatar
unknown committed
3277 3278

    if (!(user=grant_table->user))
3279
      user= "";
unknown's avatar
unknown committed
3280 3281

    if (!strcmp(lex_user->user.str,user) &&
unknown's avatar
unknown committed
3282 3283
	!my_strcasecmp(&my_charset_latin1, lex_user->host.str,
                       grant_table->orig_host))
unknown's avatar
unknown committed
3284
    {
3285 3286
      ulong table_access= grant_table->privs;
      if ((table_access | grant_table->cols) != 0)
unknown's avatar
unknown committed
3287
      {
3288
	String global(buff, sizeof(buff), system_charset_info);
unknown's avatar
unknown committed
3289 3290
	ulong test_access= (table_access | grant_table->cols) & ~GRANT_ACL;

unknown's avatar
unknown committed
3291 3292 3293
	global.length(0);
	global.append("GRANT ",6);

3294
	if (test_all_bits(table_access, (TABLE_ACLS & ~GRANT_ACL)))
unknown's avatar
unknown committed
3295
	  global.append("ALL PRIVILEGES",14);
unknown's avatar
unknown committed
3296
	else if (!test_access)
3297
 	  global.append("USAGE",5);
unknown's avatar
unknown committed
3298
	else
unknown's avatar
unknown committed
3299
	{
3300
          /* Add specific column access */
3301
	  int found= 0;
unknown's avatar
unknown committed
3302
	  ulong j;
unknown's avatar
unknown committed
3303

3304
	  for (counter= 0, j= SELECT_ACL; j <= TABLE_ACLS; counter++, j<<= 1)
unknown's avatar
unknown committed
3305
	  {
unknown's avatar
unknown committed
3306
	    if (test_access & j)
unknown's avatar
unknown committed
3307 3308 3309
	    {
	      if (found)
		global.append(", ",2);
3310
	      found= 1;
unknown's avatar
unknown committed
3311 3312
	      global.append(command_array[counter],command_lengths[counter]);

unknown's avatar
unknown committed
3313
	      if (grant_table->cols)
unknown's avatar
unknown committed
3314
	      {
3315
		uint found_col= 0;
unknown's avatar
unknown committed
3316 3317 3318 3319 3320 3321
		for (uint col_index=0 ;
		     col_index < grant_table->hash_columns.records ;
		     col_index++)
		{
		  GRANT_COLUMN *grant_column = (GRANT_COLUMN*)
		    hash_element(&grant_table->hash_columns,col_index);
unknown's avatar
unknown committed
3322
		  if (grant_column->rights & j)
unknown's avatar
unknown committed
3323
		  {
unknown's avatar
unknown committed
3324
		    if (!found_col)
unknown's avatar
unknown committed
3325
		    {
3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336
		      found_col= 1;
		      /*
			If we have a duplicated table level privilege, we
			must write the access privilege name again.
		      */
		      if (table_access & j)
		      {
			global.append(", ", 2);
			global.append(command_array[counter],
				      command_lengths[counter]);
		      }
unknown's avatar
unknown committed
3337 3338 3339 3340 3341
		      global.append(" (",2);
		    }
		    else
		      global.append(", ",2);
		    global.append(grant_column->column,
3342 3343
				  grant_column->key_length,
				  system_charset_info);
unknown's avatar
unknown committed
3344 3345 3346 3347 3348 3349 3350 3351
		  }
		}
		if (found_col)
		  global.append(')');
	      }
	    }
	  }
	}
3352 3353 3354 3355 3356 3357 3358
	global.append(" ON ",4);
	append_identifier(thd, &global, grant_table->db,
			  strlen(grant_table->db));
	global.append('.');
	append_identifier(thd, &global, grant_table->tname,
			  strlen(grant_table->tname));
	global.append(" TO '",5);
3359 3360
	global.append(lex_user->user.str, lex_user->user.length,
		      system_charset_info);
unknown's avatar
unknown committed
3361
	global.append("'@'",3);
unknown's avatar
unknown committed
3362
	global.append(lex_user->host.str,lex_user->host.length);
unknown's avatar
unknown committed
3363
	global.append('\'');
3364
	if (table_access & GRANT_ACL)
unknown's avatar
unknown committed
3365
	  global.append(" WITH GRANT OPTION",18);
unknown's avatar
unknown committed
3366
	protocol->prepare_for_resend();
3367
	protocol->store(global.ptr(),global.length(),global.charset());
unknown's avatar
unknown committed
3368
	if (protocol->write())
unknown's avatar
unknown committed
3369
	{
unknown's avatar
unknown committed
3370
	  error= -1;
unknown's avatar
unknown committed
3371
	  break;
unknown's avatar
unknown committed
3372 3373 3374 3375
	}
      }
    }
  }
3376
end:
unknown's avatar
unknown committed
3377
  VOID(pthread_mutex_unlock(&acl_cache->lock));
unknown's avatar
unknown committed
3378
  rw_unlock(&LOCK_grant);
unknown's avatar
unknown committed
3379

3380
  send_eof(thd);
unknown's avatar
unknown committed
3381 3382 3383 3384
  DBUG_RETURN(error);
}


unknown's avatar
unknown committed
3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412
/*
  Make a clear-text version of the requested privilege.
*/

void get_privilege_desc(char *to, uint max_length, ulong access)
{
  uint pos;
  char *start=to;
  DBUG_ASSERT(max_length >= 30);		// For end ',' removal

  if (access)
  {
    max_length--;				// Reserve place for end-zero
    for (pos=0 ; access ; pos++, access>>=1)
    {
      if ((access & 1) &&
	  command_lengths[pos] + (uint) (to-start) < max_length)
      {
	to= strmov(to, command_array[pos]);
	*to++=',';
      }
    }
    to--;					// Remove end ','
  }
  *to=0;
}


3413
void get_mqh(const char *user, const char *host, USER_CONN *uc)
unknown's avatar
unknown committed
3414 3415
{
  ACL_USER *acl_user;
3416 3417 3418 3419
  if (initialized && (acl_user= find_acl_user(host,user)))
    uc->user_resources= acl_user->user_resource;
  else
    bzero((char*) &uc->user_resources, sizeof(uc->user_resources));
unknown's avatar
unknown committed
3420 3421
}

3422 3423 3424 3425 3426 3427
int open_grant_tables(THD *thd, TABLE_LIST *tables)
{
  DBUG_ENTER("open_grant_tables");

  if (!initialized)
  {
unknown's avatar
unknown committed
3428
    net_printf(thd,ER_OPTION_PREVENTS_STATEMENT, "--skip-grant-tables");
3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449
    DBUG_RETURN(-1);
  }

  bzero((char*) tables, 4*sizeof(*tables));
  tables->alias= tables->real_name= (char*) "user";
  (tables+1)->alias= (tables+1)->real_name= (char*) "db";
  (tables+2)->alias= (tables+2)->real_name= (char*) "tables_priv";
  (tables+3)->alias= (tables+3)->real_name= (char*) "columns_priv";
  tables->next= tables+1;
  (tables+1)->next= tables+2;
  (tables+2)->next= tables+3;
  (tables+3)->next= 0;
  tables->lock_type= (tables+1)->lock_type=
    (tables+2)->lock_type= (tables+3)->lock_type= TL_WRITE;
  tables->db= (tables+1)->db= (tables+2)->db= (tables+3)->db=(char*) "mysql";

#ifdef HAVE_REPLICATION
  /*
    GRANT and REVOKE are applied the slave in/exclusion rules as they are
    some kind of updates to the mysql.% tables.
  */
3450 3451
  if (thd->slave_thread && table_rules_on)
  {
unknown's avatar
unknown committed
3452 3453 3454
    /*
      The tables must be marked "updating" so that tables_ok() takes them into
      account in tests.
3455 3456 3457 3458 3459 3460
    */
    tables[0].updating=tables[1].updating=tables[2].updating=tables[3].updating=1;
    if (!tables_ok(0, tables))
      DBUG_RETURN(1);
    tables[0].updating=tables[1].updating=tables[2].updating=tables[3].updating=0;
  }
3461 3462
#endif

3463
  if (simple_open_n_lock_tables(thd, tables))
3464 3465 3466 3467 3468 3469 3470 3471 3472
  {						// This should never happen
    close_thread_tables(thd);
    DBUG_RETURN(-1);
  }

  DBUG_RETURN(0);
}

ACL_USER *check_acl_user(LEX_USER *user_name,
unknown's avatar
merge  
unknown committed
3473
			 uint *acl_acl_userdx)
3474 3475 3476 3477 3478 3479 3480 3481 3482
{
  ACL_USER *acl_user= 0;
  uint counter;

  for (counter= 0 ; counter < acl_users.elements ; counter++)
  {
    const char *user,*host;
    acl_user= dynamic_element(&acl_users, counter, ACL_USER*);
    if (!(user=acl_user->user))
unknown's avatar
unknown committed
3483
      user= "";
3484
    if (!(host=acl_user->host.hostname))
unknown's avatar
unknown committed
3485
      host= "%";
3486 3487 3488 3489 3490 3491 3492
    if (!strcmp(user_name->user.str,user) &&
	!my_strcasecmp(system_charset_info, user_name->host.str, host))
      break;
  }
  if (counter == acl_users.elements)
    return 0;

unknown's avatar
merge  
unknown committed
3493
  *acl_acl_userdx= counter;
unknown's avatar
unknown committed
3494
  return acl_user;
3495 3496
}

unknown's avatar
unknown committed
3497

3498 3499
int mysql_drop_user(THD *thd, List <LEX_USER> &list)
{
unknown's avatar
merge  
unknown committed
3500
  uint counter, acl_userd;
3501 3502 3503 3504 3505 3506 3507 3508
  int result;
  ACL_USER *acl_user;
  ACL_DB *acl_db;
  TABLE_LIST tables[4];

  DBUG_ENTER("mysql_drop_user");

  if ((result= open_grant_tables(thd, tables)))
3509
    DBUG_RETURN(result == 1 ? 0 : 1);
3510 3511 3512 3513 3514 3515 3516 3517 3518 3519

  rw_wrlock(&LOCK_grant);
  VOID(pthread_mutex_lock(&acl_cache->lock));

  LEX_USER *user_name;
  List_iterator <LEX_USER> user_list(list);
  while ((user_name=user_list++))
  {
    if (!(acl_user= check_acl_user(user_name, &counter)))
    {
unknown's avatar
unknown committed
3520
      sql_print_error("DROP USER: Can't drop user: '%s'@'%s'; No such user",
3521 3522 3523 3524 3525 3526 3527
		      user_name->user.str,
		      user_name->host.str);
      result= -1;
      continue;
    }
    if ((acl_user->access & ~0))
    {
unknown's avatar
unknown committed
3528
      sql_print_error("DROP USER: Can't drop user: '%s'@'%s'; Global privileges exists",
3529 3530 3531 3532 3533
		      user_name->user.str,
		      user_name->host.str);
      result= -1;
      continue;
    }
unknown's avatar
merge  
unknown committed
3534
    acl_userd= counter;
3535 3536 3537 3538 3539 3540

    for (counter= 0 ; counter < acl_dbs.elements ; counter++)
    {
      const char *user,*host;
      acl_db=dynamic_element(&acl_dbs,counter,ACL_DB*);
      if (!(user= acl_db->user))
unknown's avatar
unknown committed
3541
	user= "";
3542
      if (!(host= acl_db->host.hostname))
unknown's avatar
unknown committed
3543
	host= "";
3544 3545 3546 3547 3548 3549 3550

      if (!strcmp(user_name->user.str,user) &&
	  !my_strcasecmp(system_charset_info, user_name->host.str, host))
	break;
    }
    if (counter != acl_dbs.elements)
    {
unknown's avatar
unknown committed
3551
      sql_print_error("DROP USER: Can't drop user: '%s'@'%s'; Database privileges exists",
3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563
		      user_name->user.str,
		      user_name->host.str);
      result= -1;
      continue;
    }

    for (counter= 0 ; counter < column_priv_hash.records ; counter++)
    {
      const char *user,*host;
      GRANT_TABLE *grant_table= (GRANT_TABLE*) hash_element(&column_priv_hash,
							    counter);
      if (!(user=grant_table->user))
unknown's avatar
unknown committed
3564
	user= "";
3565
      if (!(host=grant_table->host))
unknown's avatar
unknown committed
3566
	host= "";
3567 3568 3569 3570 3571 3572 3573

      if (!strcmp(user_name->user.str,user) &&
	  !my_strcasecmp(system_charset_info, user_name->host.str, host))
	break;
    }
    if (counter != column_priv_hash.records)
    {
unknown's avatar
unknown committed
3574
      sql_print_error("DROP USER: Can't drop user: '%s'@'%s';  Table privileges exists",
3575 3576 3577 3578 3579 3580
		      user_name->user.str,
		      user_name->host.str);
      result= -1;
      continue;
    }

unknown's avatar
unknown committed
3581
    tables[0].table->field[0]->store(user_name->host.str,(uint)
unknown's avatar
unknown committed
3582 3583
				     user_name->host.length,
				     system_charset_info);
unknown's avatar
unknown committed
3584
    tables[0].table->field[1]->store(user_name->user.str,(uint)
unknown's avatar
unknown committed
3585 3586
				     user_name->user.length,
				     system_charset_info);
unknown's avatar
unknown committed
3587
    tables[0].table->file->extra(HA_EXTRA_RETRIEVE_ALL_COLS);
3588
    if (!tables[0].table->file->index_read_idx(tables[0].table->record[0],0,
unknown's avatar
unknown committed
3589
					       (byte*) tables[0].table->
unknown's avatar
unknown committed
3590 3591 3592
					       field[0]->ptr,
					       tables[0].table->
					       key_info[0].key_length,
3593 3594 3595
					       HA_READ_KEY_EXACT))
    {
      int error;
unknown's avatar
unknown committed
3596 3597
      if ((error = tables[0].table->file->delete_row(tables[0].table->
						     record[0])))
3598 3599 3600 3601
      {
	tables[0].table->file->print_error(error, MYF(0));
	DBUG_RETURN(-1);
      }
unknown's avatar
merge  
unknown committed
3602
      delete_dynamic_element(&acl_users, acl_userd);
3603 3604
    }
  }
unknown's avatar
unknown committed
3605

3606 3607 3608 3609 3610 3611 3612 3613 3614 3615
  VOID(pthread_mutex_unlock(&acl_cache->lock));
  rw_unlock(&LOCK_grant);
  close_thread_tables(thd);
  if (result)
    my_error(ER_DROP_USER, MYF(0));
  DBUG_RETURN(result);
}

int mysql_revoke_all(THD *thd,  List <LEX_USER> &list)
{
unknown's avatar
unknown committed
3616
  uint counter, revoked;
3617
  int result;
unknown's avatar
unknown committed
3618
  ACL_DB *acl_db;
3619 3620 3621 3622
  TABLE_LIST tables[4];
  DBUG_ENTER("mysql_revoke_all");

  if ((result= open_grant_tables(thd, tables)))
3623
    DBUG_RETURN(result == 1 ? 0 : 1);
3624 3625 3626 3627 3628 3629 3630 3631

  rw_wrlock(&LOCK_grant);
  VOID(pthread_mutex_lock(&acl_cache->lock));

  LEX_USER *lex_user;
  List_iterator <LEX_USER> user_list(list);
  while ((lex_user=user_list++))
  {
unknown's avatar
unknown committed
3632
    if (!check_acl_user(lex_user, &counter))
3633 3634 3635 3636 3637 3638 3639
    {
      sql_print_error("REVOKE ALL PRIVILEGES, GRANT: User '%s'@'%s' not exists",
		      lex_user->user.str,
		      lex_user->host.str);
      result= -1;
      continue;
    }
unknown's avatar
unknown committed
3640

3641 3642 3643 3644 3645 3646 3647 3648
    if (replace_user_table(thd, tables[0].table,
			   *lex_user, ~0, 1, 0))
    {
      result= -1;
      continue;
    }

    /* Remove db access privileges */
unknown's avatar
unknown committed
3649 3650 3651 3652 3653
    /*
      Because acl_dbs and column_priv_hash shrink and may re-order
      as privileges are removed, removal occurs in a repeated loop
      until no more privileges are revoked.
     */
unknown's avatar
unknown committed
3654
    do
3655
    {
unknown's avatar
unknown committed
3656
      for (counter= 0, revoked= 0 ; counter < acl_dbs.elements ; )
3657
      {
unknown's avatar
unknown committed
3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668
	const char *user,*host;
	
	acl_db=dynamic_element(&acl_dbs,counter,ACL_DB*);
	if (!(user=acl_db->user))
	  user= "";
	if (!(host=acl_db->host.hostname))
	  host= "";
	
	if (!strcmp(lex_user->user.str,user) &&
	    !my_strcasecmp(system_charset_info, lex_user->host.str, host))
	{
unknown's avatar
unknown committed
3669
	  if (!replace_db_table(tables[1].table, acl_db->db, *lex_user, ~0, 1))
unknown's avatar
unknown committed
3670
	  {
unknown's avatar
unknown committed
3671 3672 3673 3674 3675
	    /*
	      Don't increment counter as replace_db_table deleted the
	      current element in acl_dbs.
	     */
	    revoked= 1;
unknown's avatar
unknown committed
3676 3677
	    continue;
	  }
unknown's avatar
unknown committed
3678
	  result= -1; // Something went wrong
unknown's avatar
unknown committed
3679
	}
unknown's avatar
unknown committed
3680
	counter++;
3681
      }
unknown's avatar
unknown committed
3682
    } while (revoked);
3683 3684

    /* Remove column access */
unknown's avatar
unknown committed
3685
    do
3686
    {
unknown's avatar
unknown committed
3687
      for (counter= 0, revoked= 0 ; counter < column_priv_hash.records ; )
3688
      {
unknown's avatar
unknown committed
3689 3690 3691 3692 3693 3694 3695 3696 3697 3698
	const char *user,*host;
	GRANT_TABLE *grant_table= (GRANT_TABLE*)hash_element(&column_priv_hash,
							     counter);
	if (!(user=grant_table->user))
	  user= "";
	if (!(host=grant_table->host))
	  host= "";
	
	if (!strcmp(lex_user->user.str,user) &&
	    !my_strcasecmp(system_charset_info, lex_user->host.str, host))
3699
	{
unknown's avatar
unknown committed
3700 3701 3702 3703
	  if (replace_table_table(thd,grant_table,tables[2].table,*lex_user,
				  grant_table->db,
				  grant_table->tname,
				  ~0, 0, 1))
unknown's avatar
unknown committed
3704
	  {
unknown's avatar
unknown committed
3705
	    result= -1;
unknown's avatar
unknown committed
3706
	  }
unknown's avatar
unknown committed
3707
	  else
unknown's avatar
unknown committed
3708
	  {
unknown's avatar
unknown committed
3709
	    if (!grant_table->cols)
unknown's avatar
unknown committed
3710
	    {
unknown's avatar
unknown committed
3711 3712
	      revoked= 1;
	      continue;
unknown's avatar
unknown committed
3713
	    }
unknown's avatar
unknown committed
3714 3715
	    List<LEX_COLUMN> columns;
	    if (!replace_column_table(grant_table,tables[3].table, *lex_user,
unknown's avatar
unknown committed
3716 3717 3718 3719
				      columns,
				      grant_table->db,
				      grant_table->tname,
				      ~0, 1))
unknown's avatar
unknown committed
3720
	    {
unknown's avatar
unknown committed
3721
	      revoked= 1;
3722
	      continue;
unknown's avatar
unknown committed
3723
	    }
unknown's avatar
unknown committed
3724
	    result= -1;
unknown's avatar
unknown committed
3725
	  }
3726
	}
unknown's avatar
unknown committed
3727
	counter++;
3728
      }
unknown's avatar
unknown committed
3729
    } while (revoked);
3730
  }
unknown's avatar
unknown committed
3731
  
3732 3733 3734
  VOID(pthread_mutex_unlock(&acl_cache->lock));
  rw_unlock(&LOCK_grant);
  close_thread_tables(thd);
unknown's avatar
unknown committed
3735
  
3736 3737
  if (result)
    my_error(ER_REVOKE_GRANTS, MYF(0));
unknown's avatar
unknown committed
3738
  
3739 3740
  DBUG_RETURN(result);
}
unknown's avatar
unknown committed
3741

3742

unknown's avatar
unknown committed
3743
/*****************************************************************************
unknown's avatar
unknown committed
3744
  Instantiate used templates
unknown's avatar
unknown committed
3745 3746 3747 3748 3749 3750 3751 3752
*****************************************************************************/

#ifdef __GNUC__
template class List_iterator<LEX_COLUMN>;
template class List_iterator<LEX_USER>;
template class List<LEX_COLUMN>;
template class List<LEX_USER>;
#endif
unknown's avatar
SCRUM:  
unknown committed
3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799

#endif /*NO_EMBEDDED_ACCESS_CHECKS */


int wild_case_compare(CHARSET_INFO *cs, const char *str,const char *wildstr)
{
  reg3 int flag;
  DBUG_ENTER("wild_case_compare");
  DBUG_PRINT("enter",("str: '%s'  wildstr: '%s'",str,wildstr));
  while (*wildstr)
  {
    while (*wildstr && *wildstr != wild_many && *wildstr != wild_one)
    {
      if (*wildstr == wild_prefix && wildstr[1])
	wildstr++;
      if (my_toupper(cs, *wildstr++) !=
          my_toupper(cs, *str++)) DBUG_RETURN(1);
    }
    if (! *wildstr ) DBUG_RETURN (*str != 0);
    if (*wildstr++ == wild_one)
    {
      if (! *str++) DBUG_RETURN (1);	/* One char; skip */
    }
    else
    {						/* Found '*' */
      if (!*wildstr) DBUG_RETURN(0);		/* '*' as last char: OK */
      flag=(*wildstr != wild_many && *wildstr != wild_one);
      do
      {
	if (flag)
	{
	  char cmp;
	  if ((cmp= *wildstr) == wild_prefix && wildstr[1])
	    cmp=wildstr[1];
	  cmp=my_toupper(cs, cmp);
	  while (*str && my_toupper(cs, *str) != cmp)
	    str++;
	  if (!*str) DBUG_RETURN (1);
	}
	if (wild_case_compare(cs, str,wildstr) == 0) DBUG_RETURN (0);
      } while (*str++);
      DBUG_RETURN(1);
    }
  }
  DBUG_RETURN (*str != '\0');
}