2001-11-25 01:41:16 +01:00
|
|
|
|
/*
|
2003-04-07 18:21:50 +02:00
|
|
|
|
* @(#) $Header: /tmp/cvs/mysql-admutils/mysql-useradm.c,v 1.8 2003-04-07 16:21:50 lkarsten Exp $
|
2001-11-25 01:41:16 +01:00
|
|
|
|
*
|
|
|
|
|
* 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");
|
2002-03-10 18:43:53 +01:00
|
|
|
|
printf("Report bugs to drift@stud.ntnu.no\n");
|
2001-11-25 01:41:16 +01:00
|
|
|
|
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!",
|
2002-03-07 21:04:24 +01:00
|
|
|
|
user, rows);
|
2001-11-25 01:41:16 +01:00
|
|
|
|
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)
|
|
|
|
|
{
|
2002-03-06 18:05:05 +01:00
|
|
|
|
char query[4096];
|
|
|
|
|
char **usrgroups, **cp;
|
2001-11-25 01:41:16 +01:00
|
|
|
|
MYSQL_RES *res;
|
2002-03-06 18:05:05 +01:00
|
|
|
|
int rows, numgroups;
|
2001-11-25 01:41:16 +01:00
|
|
|
|
MYSQL_ROW row;
|
|
|
|
|
char **userlist;
|
|
|
|
|
int i;
|
|
|
|
|
struct passwd *p;
|
|
|
|
|
|
|
|
|
|
p = getpwuid(getuid());
|
2003-04-04 00:37:26 +02:00
|
|
|
|
sprintf(query, "select user from user where user='%s' or user like '%s\\_%%'",
|
2001-11-25 01:41:16 +01:00
|
|
|
|
p->pw_name, p->pw_name);
|
2003-04-07 18:21:50 +02:00
|
|
|
|
// sprintf(query, "select user from user where user='myhr' or user like 'myhr\\_%%'");
|
2001-11-25 01:41:16 +01:00
|
|
|
|
|
2002-03-06 18:05:05 +01:00
|
|
|
|
numgroups = 0;
|
|
|
|
|
|
|
|
|
|
usrgroups = get_group_names(&numgroups);
|
|
|
|
|
cp = usrgroups;
|
|
|
|
|
while (*cp) {
|
2003-04-04 00:37:26 +02:00
|
|
|
|
sprintf(&query[strlen(query)], " or user='%s' or user like '%s\\_%%'", *cp, *cp);
|
2002-03-06 18:05:05 +01:00
|
|
|
|
cp++;
|
|
|
|
|
}
|
|
|
|
|
|
2003-04-04 00:30:45 +02:00
|
|
|
|
#ifdef DEBUG
|
2003-04-07 18:21:50 +02:00
|
|
|
|
printf("about to run query: %s\n", query);
|
2003-04-04 00:30:45 +02:00
|
|
|
|
#endif
|
|
|
|
|
|
2001-11-25 01:41:16 +01:00
|
|
|
|
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();
|
|
|
|
|
|
2003-04-07 18:21:50 +02:00
|
|
|
|
#ifdef DEBUG
|
|
|
|
|
printf("NB NB NB: denne versjonen av programmet er kompilert med -DDEBUG, og\n");
|
|
|
|
|
printf("kan komme til <20> skrive ut ekstra informasjon. Dette er ikke farlig,\n");
|
|
|
|
|
printf("og programmet b<>r virke som vanlig.\n");
|
|
|
|
|
#endif
|
|
|
|
|
|
2001-11-25 01:41:16 +01:00
|
|
|
|
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++)
|
|
|
|
|
{
|
2002-02-27 09:49:48 +01:00
|
|
|
|
if (! (owner(argv[i]) || member(argv[i])))
|
2001-11-25 01:41:16 +01:00
|
|
|
|
{
|
|
|
|
|
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;
|
|
|
|
|
}
|