1
/* Copyright (C) 2006 MySQL AB
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.
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.
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 */
16
#if defined(__GNUC__) && defined(USE_PRAGMA_IMPLEMENTATION)
17
#pragma implementation
20
#include "user_management_commands.h"
22
#include "exit_codes.h"
26
/*************************************************************************
27
Module-specific (internal) functions.
28
*************************************************************************/
31
The function returns user name. The user name is retrieved from command-line
32
options (if specified) or from console.
35
This function must not be used in user-management command implementations.
36
Use get_user_name() instead.
43
valid pointer on success
46
static char *get_user_name_impl()
48
static char user_name_buf[1024];
51
if (Options::User_management::user_name)
52
return Options::User_management::user_name;
54
printf("Enter user name: ");
57
if (!fgets(user_name_buf, sizeof (user_name_buf), stdin))
60
if ((ptr= strchr(user_name_buf, '\n')))
63
if ((ptr= strchr(user_name_buf, '\r')))
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
78
user_name [OUT] on success contains user name
85
static bool get_user_name(LEX_STRING *user_name)
87
char *user_name_str= get_user_name_impl();
91
fprintf(stderr, "Error: unable to read user name from stdin.\n");
95
user_name->str= user_name_str;
96
user_name->length= strlen(user_name->str);
98
if (user_name->length == 0)
100
fprintf(stderr, "Error: user name can not be empty.\n");
104
if (user_name->length > USERNAME_LENGTH)
106
fprintf(stderr, "Error: user name must not exceed %d characters.\n",
107
(int) USERNAME_LENGTH);
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
125
valid pointer on success
128
static const char *get_password()
130
if (Options::User_management::password)
131
return Options::User_management::password;
133
const char *passwd1= get_tty_password("Enter password: ");
134
const char *passwd2= get_tty_password("Re-type password: ");
136
if (strcmp(passwd1, passwd2))
138
fprintf(stderr, "Error: passwords do not match.\n");
147
Load password file into user map.
151
user_map target user map
154
See exit_codes.h for possible values.
157
static int load_password_file(User_map *user_map)
162
if (user_map->init())
164
fprintf(stderr, "Error: can not initialize user map.\n");
165
return ERR_OUT_OF_MEMORY;
168
if ((err_code= user_map->load(Options::Main::password_file_name, &err_msg)))
169
fprintf(stderr, "Error: %s.\n", (const char *) err_msg);
176
Save user map into password file.
183
See exit_codes.h for possible values.
186
static int save_password_file(User_map *user_map)
191
if ((err_code= user_map->save(Options::Main::password_file_name, &err_msg)))
192
fprintf(stderr, "Error: %s.\n", (const char *) err_msg);
197
/*************************************************************************
198
Print_password_line_cmd
199
*************************************************************************/
201
int Print_password_line_cmd::execute()
203
LEX_STRING user_name;
204
const char *password;
206
printf("Creating record for new user.\n");
208
if (get_user_name(&user_name))
209
return ERR_CAN_NOT_READ_USER_NAME;
211
if (!(password= get_password()))
212
return ERR_CAN_NOT_READ_PASSWORD;
215
User user(&user_name, password);
218
(const char *) user.user,
219
(const char *) user.scrambled_password);
226
/*************************************************************************
228
*************************************************************************/
230
int Add_user_cmd::execute()
232
LEX_STRING user_name;
233
const char *password;
240
if (get_user_name(&user_name))
241
return ERR_CAN_NOT_READ_USER_NAME;
243
/* Load the password file. */
245
if ((err_code= load_password_file(&user_map)) != ERR_OK)
248
/* Check that the user does not exist. */
250
if (user_map.find_user(&user_name))
252
fprintf(stderr, "Error: user '%s' already exists.\n",
253
(const char *) user_name.str);
254
return ERR_USER_ALREADY_EXISTS;
259
if (!(password= get_password()))
260
return ERR_CAN_NOT_READ_PASSWORD;
262
if (!(new_user= new User(&user_name, password)))
263
return ERR_OUT_OF_MEMORY;
265
if (user_map.add_user(new_user))
268
return ERR_OUT_OF_MEMORY;
271
/* Save the password file. */
273
return save_password_file(&user_map);
277
/*************************************************************************
279
*************************************************************************/
281
int Drop_user_cmd::execute()
283
LEX_STRING user_name;
290
if (get_user_name(&user_name))
291
return ERR_CAN_NOT_READ_USER_NAME;
293
/* Load the password file. */
295
if ((err_code= load_password_file(&user_map)) != ERR_OK)
300
user= user_map.find_user(&user_name);
304
fprintf(stderr, "Error: user '%s' does not exist.\n",
305
(const char *) user_name.str);
306
return ERR_USER_NOT_FOUND;
309
/* Remove the user (ignore possible errors). */
311
user_map.remove_user(user);
313
/* Save the password file. */
315
return save_password_file(&user_map);
319
/*************************************************************************
321
*************************************************************************/
323
int Edit_user_cmd::execute()
325
LEX_STRING user_name;
326
const char *password;
333
if (get_user_name(&user_name))
334
return ERR_CAN_NOT_READ_USER_NAME;
336
/* Load the password file. */
338
if ((err_code= load_password_file(&user_map)) != ERR_OK)
343
user= user_map.find_user(&user_name);
347
fprintf(stderr, "Error: user '%s' does not exist.\n",
348
(const char *) user_name.str);
349
return ERR_USER_NOT_FOUND;
352
/* Modify user's password. */
354
if (!(password= get_password()))
355
return ERR_CAN_NOT_READ_PASSWORD;
357
user->set_password(password);
359
/* Save the password file. */
361
return save_password_file(&user_map);
365
/*************************************************************************
367
*************************************************************************/
369
int Clean_db_cmd::execute()
375
fprintf(stderr, "Error: can not initialize user map.\n");
376
return ERR_OUT_OF_MEMORY;
379
return save_password_file(&user_map);
383
/*************************************************************************
385
*************************************************************************/
387
int Check_db_cmd::execute()
391
return load_password_file(&user_map);
395
/*************************************************************************
397
*************************************************************************/
399
int List_users_cmd::execute()
405
/* Load the password file. */
407
if ((err_code= load_password_file(&user_map)))
410
/* Print out registered users. */
413
User_map::Iterator it(&user_map);
416
while ((user= it.next()))
417
fprintf(stderr, "%s\n", (const char *) user->user);