~ubuntu-branches/ubuntu/precise/mysql-5.1/precise

« back to all changes in this revision

Viewing changes to server-tools/instance-manager/user_management_commands.cc

  • Committer: Bazaar Package Importer
  • Author(s): Norbert Tretkowski
  • Date: 2010-03-17 14:56:02 UTC
  • Revision ID: james.westby@ubuntu.com-20100317145602-x7e30l1b2sb5s6w6
Tags: upstream-5.1.45
ImportĀ upstreamĀ versionĀ 5.1.45

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (C) 2006 MySQL AB
 
2
 
 
3
   This program is free software; you can redistribute it and/or modify
 
4
   it under the terms of the GNU General Public License as published by
 
5
   the Free Software Foundation; version 2 of the License.
 
6
 
 
7
   This program is distributed in the hope that it will be useful,
 
8
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
9
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
10
   GNU General Public License for more details.
 
11
 
 
12
   You should have received a copy of the GNU General Public License
 
13
   along with this program; if not, write to the Free Software
 
14
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
 
15
 
 
16
#if defined(__GNUC__) && defined(USE_PRAGMA_IMPLEMENTATION)
 
17
#pragma implementation
 
18
#endif
 
19
 
 
20
#include "user_management_commands.h"
 
21
 
 
22
#include "exit_codes.h"
 
23
#include "options.h"
 
24
#include "user_map.h"
 
25
 
 
26
/*************************************************************************
 
27
  Module-specific (internal) functions.
 
28
*************************************************************************/
 
29
 
 
30
/*
 
31
  The function returns user name. The user name is retrieved from command-line
 
32
  options (if specified) or from console.
 
33
 
 
34
  NOTE
 
35
    This function must not be used in user-management command implementations.
 
36
    Use get_user_name() instead.
 
37
 
 
38
  SYNOPSIS
 
39
    get_user_name_impl()
 
40
 
 
41
  RETURN
 
42
    NULL            on error
 
43
    valid pointer   on success
 
44
*/
 
45
 
 
46
static char *get_user_name_impl()
 
47
{
 
48
  static char user_name_buf[1024];
 
49
  char *ptr;
 
50
 
 
51
  if (Options::User_management::user_name)
 
52
    return Options::User_management::user_name;
 
53
 
 
54
  printf("Enter user name: ");
 
55
  fflush(stdout);
 
56
 
 
57
  if (!fgets(user_name_buf, sizeof (user_name_buf), stdin))
 
58
    return NULL;
 
59
 
 
60
  if ((ptr= strchr(user_name_buf, '\n')))
 
61
    *ptr= 0;
 
62
 
 
63
  if ((ptr= strchr(user_name_buf, '\r')))
 
64
    *ptr= 0;
 
65
 
 
66
  return user_name_buf;
 
67
}
 
68
 
 
69
 
 
70
/*
 
71
  The function is intended to provide user name for user-management
 
72
  operations. It also checks that length of the specified user name is correct
 
73
  (not empty, not exceeds USERNAME_LENGTH). Report to stderr if something is
 
74
  wrong.
 
75
 
 
76
  SYNOPSIS
 
77
    get_user_name()
 
78
    user_name     [OUT] on success contains user name
 
79
 
 
80
  RETURN
 
81
    TRUE    on error
 
82
    FALSE   on success
 
83
*/
 
84
 
 
85
static bool get_user_name(LEX_STRING *user_name)
 
86
{
 
87
  char *user_name_str= get_user_name_impl();
 
88
 
 
89
  if (!user_name_str)
 
90
  {
 
91
    fprintf(stderr, "Error: unable to read user name from stdin.\n");
 
92
    return TRUE;
 
93
  }
 
94
 
 
95
  user_name->str= user_name_str;
 
96
  user_name->length= strlen(user_name->str);
 
97
 
 
98
  if (user_name->length == 0)
 
99
  {
 
100
    fprintf(stderr, "Error: user name can not be empty.\n");
 
101
    return TRUE;
 
102
  }
 
103
 
 
104
  if (user_name->length > USERNAME_LENGTH)
 
105
  {
 
106
    fprintf(stderr, "Error: user name must not exceed %d characters.\n",
 
107
            (int) USERNAME_LENGTH);
 
108
    return TRUE;
 
109
  }
 
110
 
 
111
  return FALSE;
 
112
}
 
113
 
 
114
 
 
115
/*
 
116
  The function is intended to provide password for user-management operations.
 
117
  The password is retrieved from command-line options (if specified) or from
 
118
  console.
 
119
 
 
120
  SYNOPSIS
 
121
    get_password()
 
122
 
 
123
  RETURN
 
124
    NULL            on error
 
125
    valid pointer   on success
 
126
*/
 
127
 
 
128
static const char *get_password()
 
129
{
 
130
  if (Options::User_management::password)
 
131
    return Options::User_management::password;
 
132
 
 
133
  const char *passwd1= get_tty_password("Enter password: ");
 
134
  const char *passwd2= get_tty_password("Re-type password: ");
 
135
 
 
136
  if (strcmp(passwd1, passwd2))
 
137
  {
 
138
    fprintf(stderr, "Error: passwords do not match.\n");
 
139
    return 0;
 
140
  }
 
141
 
 
142
  return passwd1;
 
143
}
 
144
 
 
145
 
 
146
/*
 
147
  Load password file into user map.
 
148
 
 
149
  SYNOPSIS
 
150
    load_password_file()
 
151
    user_map            target user map
 
152
 
 
153
  RETURN
 
154
    See exit_codes.h for possible values.
 
155
*/
 
156
 
 
157
static int load_password_file(User_map *user_map)
 
158
{
 
159
  int err_code;
 
160
  const char *err_msg;
 
161
 
 
162
  if (user_map->init())
 
163
  {
 
164
    fprintf(stderr, "Error: can not initialize user map.\n");
 
165
    return ERR_OUT_OF_MEMORY;
 
166
  }
 
167
 
 
168
  if ((err_code= user_map->load(Options::Main::password_file_name, &err_msg)))
 
169
    fprintf(stderr, "Error: %s.\n", (const char *) err_msg);
 
170
 
 
171
  return err_code;
 
172
}
 
173
 
 
174
 
 
175
/*
 
176
  Save user map into password file.
 
177
 
 
178
  SYNOPSIS
 
179
    save_password_file()
 
180
    user_map            user map
 
181
 
 
182
  RETURN
 
183
    See exit_codes.h for possible values.
 
184
*/
 
185
 
 
186
static int save_password_file(User_map *user_map)
 
187
{
 
188
  int err_code;
 
189
  const char *err_msg;
 
190
 
 
191
  if ((err_code= user_map->save(Options::Main::password_file_name, &err_msg)))
 
192
    fprintf(stderr, "Error: %s.\n", (const char *) err_msg);
 
193
 
 
194
  return err_code;
 
195
}
 
196
 
 
197
/*************************************************************************
 
198
  Print_password_line_cmd
 
199
*************************************************************************/
 
200
 
 
201
int Print_password_line_cmd::execute()
 
202
{
 
203
  LEX_STRING user_name;
 
204
  const char *password;
 
205
 
 
206
  printf("Creating record for new user.\n");
 
207
 
 
208
  if (get_user_name(&user_name))
 
209
    return ERR_CAN_NOT_READ_USER_NAME;
 
210
 
 
211
  if (!(password= get_password()))
 
212
    return ERR_CAN_NOT_READ_PASSWORD;
 
213
 
 
214
  {
 
215
    User user(&user_name, password);
 
216
 
 
217
    printf("%s:%s\n",
 
218
           (const char *) user.user,
 
219
           (const char *) user.scrambled_password);
 
220
  }
 
221
 
 
222
  return ERR_OK;
 
223
}
 
224
 
 
225
 
 
226
/*************************************************************************
 
227
  Add_user_cmd
 
228
*************************************************************************/
 
229
 
 
230
int Add_user_cmd::execute()
 
231
{
 
232
  LEX_STRING user_name;
 
233
  const char *password;
 
234
 
 
235
  User_map user_map;
 
236
  User *new_user;
 
237
 
 
238
  int err_code;
 
239
 
 
240
  if (get_user_name(&user_name))
 
241
    return ERR_CAN_NOT_READ_USER_NAME;
 
242
 
 
243
  /* Load the password file. */
 
244
 
 
245
  if ((err_code= load_password_file(&user_map)) != ERR_OK)
 
246
    return err_code;
 
247
 
 
248
  /* Check that the user does not exist. */
 
249
 
 
250
  if (user_map.find_user(&user_name))
 
251
  {
 
252
    fprintf(stderr, "Error: user '%s' already exists.\n",
 
253
            (const char *) user_name.str);
 
254
    return ERR_USER_ALREADY_EXISTS;
 
255
  }
 
256
 
 
257
  /* Add the user. */
 
258
 
 
259
  if (!(password= get_password()))
 
260
    return ERR_CAN_NOT_READ_PASSWORD;
 
261
 
 
262
  if (!(new_user= new User(&user_name, password)))
 
263
    return ERR_OUT_OF_MEMORY;
 
264
 
 
265
  if (user_map.add_user(new_user))
 
266
  {
 
267
    delete new_user;
 
268
    return ERR_OUT_OF_MEMORY;
 
269
  }
 
270
 
 
271
  /* Save the password file. */
 
272
 
 
273
  return save_password_file(&user_map);
 
274
}
 
275
 
 
276
 
 
277
/*************************************************************************
 
278
  Drop_user_cmd
 
279
*************************************************************************/
 
280
 
 
281
int Drop_user_cmd::execute()
 
282
{
 
283
  LEX_STRING user_name;
 
284
 
 
285
  User_map user_map;
 
286
  User *user;
 
287
 
 
288
  int err_code;
 
289
 
 
290
  if (get_user_name(&user_name))
 
291
    return ERR_CAN_NOT_READ_USER_NAME;
 
292
 
 
293
  /* Load the password file. */
 
294
 
 
295
  if ((err_code= load_password_file(&user_map)) != ERR_OK)
 
296
    return err_code;
 
297
 
 
298
  /* Find the user. */
 
299
 
 
300
  user= user_map.find_user(&user_name);
 
301
 
 
302
  if (!user)
 
303
  {
 
304
    fprintf(stderr, "Error: user '%s' does not exist.\n",
 
305
            (const char *) user_name.str);
 
306
    return ERR_USER_NOT_FOUND;
 
307
  }
 
308
 
 
309
  /* Remove the user (ignore possible errors). */
 
310
 
 
311
  user_map.remove_user(user);
 
312
 
 
313
  /* Save the password file. */
 
314
 
 
315
  return save_password_file(&user_map);
 
316
}
 
317
 
 
318
 
 
319
/*************************************************************************
 
320
  Edit_user_cmd
 
321
*************************************************************************/
 
322
 
 
323
int Edit_user_cmd::execute()
 
324
{
 
325
  LEX_STRING user_name;
 
326
  const char *password;
 
327
 
 
328
  User_map user_map;
 
329
  User *user;
 
330
 
 
331
  int err_code;
 
332
 
 
333
  if (get_user_name(&user_name))
 
334
    return ERR_CAN_NOT_READ_USER_NAME;
 
335
 
 
336
  /* Load the password file. */
 
337
 
 
338
  if ((err_code= load_password_file(&user_map)) != ERR_OK)
 
339
    return err_code;
 
340
 
 
341
  /* Find the user. */
 
342
 
 
343
  user= user_map.find_user(&user_name);
 
344
 
 
345
  if (!user)
 
346
  {
 
347
    fprintf(stderr, "Error: user '%s' does not exist.\n",
 
348
            (const char *) user_name.str);
 
349
    return ERR_USER_NOT_FOUND;
 
350
  }
 
351
 
 
352
  /* Modify user's password. */
 
353
 
 
354
  if (!(password= get_password()))
 
355
    return ERR_CAN_NOT_READ_PASSWORD;
 
356
 
 
357
  user->set_password(password);
 
358
 
 
359
  /* Save the password file. */
 
360
 
 
361
  return save_password_file(&user_map);
 
362
}
 
363
 
 
364
 
 
365
/*************************************************************************
 
366
  Clean_db_cmd
 
367
*************************************************************************/
 
368
 
 
369
int Clean_db_cmd::execute()
 
370
{
 
371
  User_map user_map;
 
372
 
 
373
  if (user_map.init())
 
374
  {
 
375
    fprintf(stderr, "Error: can not initialize user map.\n");
 
376
    return ERR_OUT_OF_MEMORY;
 
377
  }
 
378
 
 
379
  return save_password_file(&user_map);
 
380
}
 
381
 
 
382
 
 
383
/*************************************************************************
 
384
  Check_db_cmd
 
385
*************************************************************************/
 
386
 
 
387
int Check_db_cmd::execute()
 
388
{
 
389
  User_map user_map;
 
390
 
 
391
  return load_password_file(&user_map);
 
392
}
 
393
 
 
394
 
 
395
/*************************************************************************
 
396
  List_users_cmd
 
397
*************************************************************************/
 
398
 
 
399
int List_users_cmd::execute()
 
400
{
 
401
  User_map user_map;
 
402
 
 
403
  int err_code;
 
404
 
 
405
  /* Load the password file. */
 
406
 
 
407
  if ((err_code= load_password_file(&user_map)))
 
408
    return err_code;
 
409
 
 
410
  /* Print out registered users. */
 
411
 
 
412
  {
 
413
    User_map::Iterator it(&user_map);
 
414
    User *user;
 
415
 
 
416
    while ((user= it.next()))
 
417
      fprintf(stderr, "%s\n", (const char *) user->user);
 
418
  }
 
419
 
 
420
  return ERR_OK;
 
421
}