mysql-admutils/mysql-useradm.c
Knut Petter Svendsen ff52786682 With alter table patches and the undocumented feature for administering
mysql users and databases with same name as unix groups for the user.
2002-02-27 08:49:48 +00:00

305 lines
7.2 KiB
C

/*
* @(#) $Header: /tmp/cvs/mysql-admutils/mysql-useradm.c,v 1.2 2002-02-27 08:49:48 knutpett Exp $
*
* mysql-useradm.c
*
*/
#include <stdio.h>
#include <stdarg.h>
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <pwd.h>
#include <sys/types.h>
#include <unistd.h>
#include <mysql.h>
#include "mysql-admutils.h"
int
usage()
{
printf("Usage: %s COMMAND [USER]...\n", program_name);
printf("Create, delete or change password for the USER(s),\n");
printf("as determined by the COMMAND. Valid COMMANDs:\n");
printf("\n");
printf(" create create the USER(s).\n");
printf(" delete delete the USER(s).\n");
printf(" passwd change the MySQL password for the USER(s). You\n");
printf(" will be promptet for the old and the new password.\n");
printf(" show give information about the USERS(s), or, if\n");
printf(" none are given, all the users you have.\n");
printf("\n");
printf("Report bugs to ljosa@initio.no\n");
return 0;
}
int
is_password_set(MYSQL *pmysql, const char *user)
{
char query[1024];
MYSQL_RES *res;
int rows;
MYSQL_ROW row;
sprintf(query, "select password from user where user = '%s'", user);
if (mysql_query(pmysql, query))
dberror(pmysql, "Failed to look up password for user '%s'.", user);
res = mysql_store_result(pmysql);
rows = mysql_num_rows(res);
if (rows == 0)
return -1;
if (rows > 1)
return dberror(NULL, "Query for password for user '%s' gave %d results!",
rows);
row = mysql_fetch_row(res);
return (row[0] && (strlen(row[0]) > 0));
}
int
create(MYSQL *pmysql, const char *user)
{
char query[1024];
sprintf(query, "insert into user (host, user) values ('%%', '%s')", user);
if (mysql_query(pmysql, query))
return dberror(pmysql, "Failed to create user '%s'.", user);
return 0;
}
int
delete(MYSQL *pmysql, const char *user)
{
char query[1024];
sprintf(query, "delete from user where user = '%s'", user);
if (mysql_query(pmysql, query))
return dberror(pmysql, "Failed to delete user '%s'.", user);
return 0;
}
int
passwd(MYSQL *pmysql, const char *user)
{
char prompt[1024];
char query[1024];
char *password, *confirm_password;
MYSQL_RES *res;
int rows;
MYSQL_ROW row;
if (is_password_set(pmysql, user))
{
sprintf(prompt, "(current) MySQL password for user '%s': ", user);
password = getpass(prompt);
sprintf(query, "select password = password('%s') from user "
"where user = '%s'", password, user);
if (mysql_query(pmysql, query))
return dberror(pmysql, "Failed to check old password for user '%s'.",
user);
res = mysql_store_result(pmysql);
rows = mysql_num_rows(res);
if (rows == 0)
return dberror(NULL, "Check for old password for user '%s' "
"returned empty.", user);
if (rows > 1)
return dberror(NULL, "Check for old password for user '%s' "
"returned more than one row!", user);
row = mysql_fetch_row(res);
if (strcmp(row[0], "1") != 0)
{
fprintf(stderr, "%s: Wrong password entered for user '%s'.\n",
program_name, user);
return 1;
}
}
sprintf(prompt, "New MySQL password for user '%s': ", user);
password = getpass(prompt);
confirm_password = strdup(password);
sprintf(prompt, "Retype new MySQL password for user '%s': ", user);
password = getpass(prompt);
if (strcmp(password, confirm_password) != 0)
{
free(confirm_password);
return dberror(NULL, "Sorry, passwords do not match.");
}
free(confirm_password);
sprintf(query, "update user set password = password('%s') "
"where user = '%s'", password, user);
if (mysql_query(pmysql, query))
return dberror(pmysql, "Failed to set new password for user '%s'.", user);
if (mysql_affected_rows(pmysql) > 1)
dberror(NULL, "%d rows affected by password update for user '%s'!",
mysql_affected_rows(pmysql), user);
fprintf(stderr, "Password updated for user '%s'.\n", user);
return 0;
}
int
show(MYSQL *pmysql, const char *user)
{
switch (is_password_set(pmysql, user))
{
case -1:
break;
case 0:
printf("User '%s': ", user);
printf("no password set.\n");
break;
case 1:
printf("User '%s': ", user);
printf("password set.\n");
break;
}
return 0;
}
/* return a list of the user's databases */
char **
list(MYSQL *pmysql)
{
char query[1024];
MYSQL_RES *res;
int rows;
MYSQL_ROW row;
char **userlist;
int i;
struct passwd *p;
p = getpwuid(getuid());
sprintf(query, "select user from user where user='%s' or user like '%s_%%'",
p->pw_name, p->pw_name);
if (mysql_query(pmysql, query))
{
dberror(pmysql, "Failed to look up %s's users.", p->pw_name);
return NULL;
}
res = mysql_store_result(pmysql);
rows = mysql_num_rows(res);
userlist = malloc((rows + 1) * sizeof(char *));
if (!userlist)
{
dberror(NULL, "%s: Out of memory.\n", program_name);
return NULL;
}
for (i = 0; i < rows; i++)
if ((row = mysql_fetch_row(res)))
{
userlist[i] = strdup(row[0]);
}
userlist[i] = NULL;
return userlist;
}
int
main(int argc, char *argv[])
{
int i;
enum { c_create, c_delete, c_passwd, c_show } command;
MYSQL mysql;
char **dblist, **p;
program_name = argv[0];
for (i = 1; i < argc; i++)
if (strcmp(argv[i], "--help") == 0)
return usage();
for (i = 1; i < argc; i++)
if (strcmp(argv[i], "--version") == 0)
return version();
if (argc < 2)
return wrong_use(NULL);
/* check that the supplied command is valid */
if (strcmp(argv[1], "create") == 0)
command = c_create;
else if (strcmp(argv[1], "delete") == 0)
command = c_delete;
else if (strcmp(argv[1], "passwd") == 0)
command = c_passwd;
else if (strcmp(argv[1], "show") == 0)
command = c_show;
else
return wrong_use("unrecognized command '%s'.", argv[1]); /* XXX */
/* all other than show requires at lease one USER argument. */
if ((command != c_show) && (argc < 3))
return wrong_use(NULL);
read_config_file();
/* connect to the database server and select the mysql database */
if (!mysql_connect(&mysql, db_server, db_user, db_passwd))
return dberror(&mysql, "Cannot connect to database server '%s'.",
db_server);
if (mysql_select_db(&mysql, db_name))
return dberror(&mysql, "Cannot select database '%s'.", db_name);
if ((command == c_show) && (argc == 2))
{
dblist = list(&mysql);
p = dblist;
while (*p)
{
show(&mysql, *p);
free(*p);
p++;
}
free(dblist);
}
else
{
/* for each supplied database name, perform the requested action */
for (i = 2; i < argc; i++)
{
if (! (owner(argv[i]) || member(argv[i])))
{
dberror(NULL, "You are not the owner of '%s'. Skipping.",
argv[i]);
continue;
}
switch (command)
{
case c_create:
create(&mysql, argv[i]);
break;
case c_delete:
delete(&mysql, argv[i]);
break;
case c_passwd:
passwd(&mysql, argv[i]);
break;
case c_show:
show(&mysql, argv[i]);
break;
default:
fprintf(stderr, "This point should never be reached.\n");
exit(1);
}
}
}
reload(&mysql);
mysql_close(&mysql);
return 0;
}