summaryrefslogtreecommitdiff
path: root/server-tools/instance-manager/user_map.cc
blob: b7a32f1d04718382824f63833cb88ad6bfa52230 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
/* Copyright (C) 2004-2006 MySQL AB

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; version 2 of the License.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */

#if defined(__GNUC__) && defined(USE_PRAGMA_IMPLEMENTATION)
#pragma implementation
#endif

#include "user_map.h"
#include "exit_codes.h"
#include "log.h"
#include "portability.h"

User::User(const LEX_STRING *user_name_arg, const char *password)
{
  user_length= (uint8) (strmake(user, user_name_arg->str,
                                USERNAME_LENGTH) - user);
  set_password(password);
}

int User::init(const char *line)
{
  const char *name_begin, *name_end, *password;
  int password_length;

  if (line[0] == '\'' || line[0] == '"')
  {
    name_begin= line + 1;
    name_end= strchr(name_begin, line[0]);
    if (name_end == 0 || name_end[1] != ':')
    {
      log_error("Invalid format (unmatched quote) of user line (%s).",
                (const char *) line);
      return 1;
    }
    password= name_end + 2;
  }
  else
  {
    name_begin= line;
    name_end= strchr(name_begin, ':');
    if (name_end == 0)
    {
      log_error("Invalid format (no delimiter) of user line (%s).",
                (const char *) line);
      return 1;
    }
    password= name_end + 1;
  }

  user_length= (uint8) (name_end - name_begin);
  if (user_length > USERNAME_LENGTH)
  {
    log_error("User name is too long (%d). Max length: %d. "
              "User line: '%s'.",
              (int) user_length,
              (int) USERNAME_LENGTH,
              (const char *) line);
    return 1;
  }

  password_length= (int) strlen(password);
  if (password_length > SCRAMBLED_PASSWORD_CHAR_LENGTH)
  {
    log_error("Password is too long (%d). Max length: %d."
              "User line: '%s'.",
              (int) password_length,
              (int) SCRAMBLED_PASSWORD_CHAR_LENGTH,
              (const char *) line);
    return 1;
  }

  memcpy(user, name_begin, user_length);
  user[user_length]= 0;

  memcpy(scrambled_password, password, password_length);
  scrambled_password[password_length]= 0;

  get_salt_from_password(salt, password);

  log_info("Loaded user '%s'.", (const char *) user);

  return 0;
}


C_MODE_START

static uchar* get_user_key(const uchar* u, size_t* len,
                           my_bool __attribute__((unused)) t)
{
  const User *user= (const User *) u;
  *len= user->user_length;
  return (uchar *) user->user;
}

static void delete_user(void *u)
{
  User *user= (User *) u;
  delete user;
}

C_MODE_END


void User_map::Iterator::reset()
{
  cur_idx= 0;
}


User *User_map::Iterator::next()
{
  if (cur_idx < user_map->hash.records)
    return (User *) hash_element(&user_map->hash, cur_idx++);

  return NULL;
}


int User_map::init()
{
  enum { START_HASH_SIZE= 16 };
  if (hash_init(&hash, default_charset_info, START_HASH_SIZE, 0, 0,
                get_user_key, delete_user, 0))
    return 1;

  initialized= TRUE;

  return 0;
}


User_map::User_map()
  :initialized(FALSE)
{
}


User_map::~User_map()
{
  if (initialized)
    hash_free(&hash);
}


/*
  Load password database.

  SYNOPSIS
    load()
    password_file_name  [IN] password file path
    err_msg             [OUT] error message

  DESCRIPTION
    Load all users from the password file. Must be called once right after
    construction. In case of failure, puts error message to the log file and
    returns specific error code.

  RETURN
    0   on success
    !0  on error
*/

int User_map::load(const char *password_file_name, const char **err_msg)
{
  static const int ERR_MSG_BUF_SIZE = 255;
  static char err_msg_buf[ERR_MSG_BUF_SIZE];

  FILE *file;
  char line[USERNAME_LENGTH + SCRAMBLED_PASSWORD_CHAR_LENGTH +
            2 +                               /* for possible quotes */
            1 +                               /* for ':' */
            2 +                               /* for newline */
            1];                               /* for trailing zero */
  User *user;

  if (my_access(password_file_name, F_OK) != 0)
  {
    if (err_msg)
    {
      snprintf(err_msg_buf, ERR_MSG_BUF_SIZE,
               "password file (%s) does not exist",
               (const char *) password_file_name);
      *err_msg= err_msg_buf;
    }

    return ERR_PASSWORD_FILE_DOES_NOT_EXIST;
  }

  if ((file= my_fopen(password_file_name, O_RDONLY | O_BINARY, MYF(0))) == 0)
  {
    if (err_msg)
    {
      snprintf(err_msg_buf, ERR_MSG_BUF_SIZE,
               "can not open password file (%s): %s",
               (const char *) password_file_name,
               (const char *) strerror(errno));
      *err_msg= err_msg_buf;
    }

    return ERR_IO_ERROR;
  }

  log_info("Loading the password database...");

  while (fgets(line, sizeof(line), file))
  {
    char *user_line= line;

    /*
      We need to skip EOL-symbols also from the beginning of the line, because
      if the previous line was ended by \n\r sequence, we get \r in our line.
    */

    while (user_line[0] == '\r' || user_line[0] == '\n')
      ++user_line;

    /* Skip EOL-symbols in the end of the line. */

    {
      char *ptr;

      if ((ptr= strchr(user_line, '\n')))
        *ptr= 0;

      if ((ptr= strchr(user_line, '\r')))
        *ptr= 0;
    }

    /* skip comments and empty lines */
    if (!user_line[0] || user_line[0] == '#')
      continue;

    if ((user= new User) == 0)
    {
      my_fclose(file, MYF(0));

      if (err_msg)
      {
        snprintf(err_msg_buf, ERR_MSG_BUF_SIZE,
                 "out of memory while parsing password file (%s)",
                 (const char *) password_file_name);
        *err_msg= err_msg_buf;
      }

      return ERR_OUT_OF_MEMORY;
    }

    if (user->init(user_line))
    {
      delete user;
      my_fclose(file, MYF(0));

      if (err_msg)
      {
        snprintf(err_msg_buf, ERR_MSG_BUF_SIZE,
                 "password file (%s) corrupted",
                 (const char *) password_file_name);
        *err_msg= err_msg_buf;
      }

      return ERR_PASSWORD_FILE_CORRUPTED;
    }

    if (my_hash_insert(&hash, (uchar *) user))
    {
      delete user;
      my_fclose(file, MYF(0));

      if (err_msg)
      {
        snprintf(err_msg_buf, ERR_MSG_BUF_SIZE,
                 "out of memory while parsing password file (%s)",
                 (const char *) password_file_name);
        *err_msg= err_msg_buf;
      }

      return ERR_OUT_OF_MEMORY;
    }
  }

  log_info("The password database loaded successfully.");

  my_fclose(file, MYF(0));

  if (err_msg)
    *err_msg= NULL;

  return ERR_OK;
}


int User_map::save(const char *password_file_name, const char **err_msg)
{
  static const int ERR_MSG_BUF_SIZE = 255;
  static char err_msg_buf[ERR_MSG_BUF_SIZE];

  FILE *file;

  if ((file= my_fopen(password_file_name, O_WRONLY | O_TRUNC | O_BINARY,
                      MYF(0))) == 0)
  {
    if (err_msg)
    {
      snprintf(err_msg_buf, ERR_MSG_BUF_SIZE,
               "can not open password file (%s) for writing: %s",
               (const char *) password_file_name,
               (const char *) strerror(errno));
      *err_msg= err_msg_buf;
    }

    return ERR_IO_ERROR;
  }

  {
    User_map::Iterator it(this);
    User *user;

    while ((user= it.next()))
    {
      if (fprintf(file, "%s:%s\n", (const char *) user->user,
                  (const char *) user->scrambled_password) < 0)
      {
        if (err_msg)
        {
          snprintf(err_msg_buf, ERR_MSG_BUF_SIZE,
                   "can not write to password file (%s): %s",
                   (const char *) password_file_name,
                   (const char *) strerror(errno));
          *err_msg= err_msg_buf;
        }

        my_fclose(file, MYF(0));

        return ERR_IO_ERROR;
      }
    }
  }

  my_fclose(file, MYF(0));

  return ERR_OK;
}


/*
    Check if user exists and password is correct
  RETURN VALUE
    0 - user found and password OK
    1 - password mismatch
    2 - user not found
*/

int User_map::authenticate(const LEX_STRING *user_name,
                           const char *scrambled_password,
                           const char *scramble) const
{
  const User *user= find_user(user_name);
  return user ? check_scramble((uchar*)scrambled_password, scramble, user->salt) : 2;
}


User *User_map::find_user(const LEX_STRING *user_name)
{
  return (User*) hash_search(&hash, (uchar*) user_name->str, user_name->length);
}

const User *User_map::find_user(const LEX_STRING *user_name) const
{
  return const_cast<User_map *> (this)->find_user(user_name);
}


bool User_map::add_user(User *user)
{
  return my_hash_insert(&hash, (uchar*) user) == 0 ? FALSE : TRUE;
}


bool User_map::remove_user(User *user)
{
  return hash_delete(&hash, (uchar*) user) == 0 ? FALSE : TRUE;
}