2001-11-25 01:41:16 +01:00
|
|
|
/*
|
2002-03-06 18:05:05 +01:00
|
|
|
* @(#) $Header: /tmp/cvs/mysql-admutils/common.c,v 1.3 2002-03-06 17:05:05 tlan Exp $
|
2001-11-25 01:41:16 +01:00
|
|
|
*
|
|
|
|
* functions used by mysql-dbadm.c and mysql-useradm.c
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <mysql.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <pwd.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/resource.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <string.h>
|
2002-02-27 09:49:48 +01:00
|
|
|
#include <grp.h>
|
2001-11-25 01:41:16 +01:00
|
|
|
#include "mysql-admutils.h"
|
|
|
|
|
|
|
|
char *program_name;
|
|
|
|
|
2002-03-06 18:05:05 +01:00
|
|
|
static char *rcsheader = "@(#) " PACKAGE " " VERSION " ljosa@initio.no $Header: /tmp/cvs/mysql-admutils/common.c,v 1.3 2002-03-06 17:05:05 tlan Exp $";
|
2001-11-25 01:41:16 +01:00
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
version()
|
|
|
|
{
|
|
|
|
printf("%s %s\n", program_name, rcsheader);
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
wrong_use(char *format, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
if (format)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "%s: ", program_name);
|
|
|
|
va_start(ap, format);
|
|
|
|
vfprintf(stderr, format, ap);
|
|
|
|
va_end(ap);
|
|
|
|
putchar('\n');
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(stderr, "Try `%s --help' for more information.\n", program_name);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* always returns 1. */
|
|
|
|
int
|
|
|
|
dberror(MYSQL *pmysql, char *format, ...)
|
|
|
|
{
|
|
|
|
char *errmsg;
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
fprintf(stderr, "%s: ", program_name);
|
|
|
|
va_start(ap, format);
|
|
|
|
vfprintf(stderr, format, ap);
|
|
|
|
va_end(ap);
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
|
|
|
|
if (pmysql)
|
|
|
|
{
|
|
|
|
errmsg = mysql_error(pmysql);
|
|
|
|
if ((errmsg) && (strcmp(errmsg, "") != 0))
|
|
|
|
fprintf(stderr, "mysql: %s\n", errmsg);
|
|
|
|
|
|
|
|
mysql_close(pmysql);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* always returns 1. */
|
|
|
|
int
|
|
|
|
fatal_error(char *format, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
fprintf(stderr, "%s: ", program_name);
|
|
|
|
va_start(ap, format);
|
|
|
|
vfprintf(stderr, format, ap);
|
|
|
|
va_end(ap);
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* decides if the UNIX user is entitled to the MySQL database or MySQL user. */
|
|
|
|
int
|
|
|
|
owner(char *name)
|
|
|
|
{
|
|
|
|
struct passwd *p;
|
|
|
|
|
|
|
|
p = getpwuid(getuid());
|
|
|
|
if (!p)
|
|
|
|
{
|
|
|
|
dberror(NULL, "Failed to look up your UNIX username.");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strcmp(name, p->pw_name) == 0)
|
|
|
|
return 1; /* OK */
|
|
|
|
|
|
|
|
if ((strncmp(name, p->pw_name, strlen(p->pw_name)) == 0) &&
|
|
|
|
(*(name + strlen(p->pw_name)) == '_'))
|
|
|
|
return 1; /* OK */
|
|
|
|
|
|
|
|
return 0; /* not owner if we get as far as this */
|
|
|
|
}
|
|
|
|
|
2002-02-27 09:49:48 +01:00
|
|
|
/**
|
|
|
|
* Decides if the user is member of a group. The Unix group can't contain any
|
|
|
|
* '_'. i.e 'fidi_s' won't be accepted.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
member(char *gr) {
|
|
|
|
char *username;
|
|
|
|
|
|
|
|
char *group;
|
|
|
|
struct group *g;
|
|
|
|
|
|
|
|
struct passwd *p;
|
|
|
|
char *foo;
|
|
|
|
|
|
|
|
/* size_t i = 0; */
|
|
|
|
|
|
|
|
/* Get username */
|
|
|
|
p = getpwuid(getuid());
|
|
|
|
if (!p) {
|
|
|
|
fprintf(stderr, "Failed to look up your UNIX username.");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
username = p->pw_name;
|
|
|
|
|
|
|
|
/* Copy string, but cut at '_' */
|
|
|
|
group = strdup(gr);
|
|
|
|
if (group == NULL) {
|
|
|
|
fprintf(stderr, "Couldn't allocate memory. Terminating.");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
foo = strchr(group, '_');
|
|
|
|
if (foo) {
|
|
|
|
#if DEBUG
|
|
|
|
printf("gr = %s, group = %s, foo = %s\n", gr, group, foo);
|
|
|
|
#endif
|
|
|
|
*foo = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get group */
|
|
|
|
g = getgrnam(group);
|
|
|
|
if (g == NULL) {
|
|
|
|
fprintf(stderr, "No such group: %s\n", group);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if user is member of group */
|
|
|
|
while(*g->gr_mem != NULL) {
|
|
|
|
char * member = *g->gr_mem;
|
|
|
|
#if DEBUG
|
|
|
|
printf("Medlem: %s\n", *g->gr_mem);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (strcmp(member,username) == 0) {
|
|
|
|
#if DEBUG
|
|
|
|
printf("You have access to '%s'\n", gr);
|
|
|
|
#endif
|
|
|
|
return 1; /* OK */
|
|
|
|
} else
|
|
|
|
*g->gr_mem++;
|
|
|
|
}
|
|
|
|
#if DEBUG
|
|
|
|
printf("You have no access to '%s'\n", gr);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2002-03-06 18:05:05 +01:00
|
|
|
/* return a list of the user's groupnames */
|
|
|
|
/* numgroups is the total number of groups found */
|
|
|
|
char **get_group_names(int *numgroups)
|
|
|
|
{
|
|
|
|
char **grouplist;
|
|
|
|
gid_t gids[33];
|
|
|
|
int nr_groups, i;
|
|
|
|
struct group *g;
|
|
|
|
|
|
|
|
nr_groups = 0;
|
|
|
|
|
|
|
|
nr_groups = getgroups(32, &gids[0]); /* Allow a max of 32 groups */
|
|
|
|
|
|
|
|
if (nr_groups == -1) {
|
|
|
|
dberror(NULL, "Error while trying to fetch group info");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
*numgroups = nr_groups;
|
|
|
|
|
|
|
|
grouplist = malloc((nr_groups+1) * sizeof(char *));
|
|
|
|
|
|
|
|
for (i = 0; i < nr_groups; i++) {
|
|
|
|
g = getgrgid(gids[i]);
|
|
|
|
|
|
|
|
grouplist[i] = strdup(g->gr_name);
|
|
|
|
}
|
|
|
|
grouplist[i] = NULL;
|
|
|
|
|
|
|
|
return grouplist;
|
|
|
|
}
|
|
|
|
|
2001-11-25 01:41:16 +01:00
|
|
|
|
|
|
|
int
|
|
|
|
reload(MYSQL *pmysql)
|
|
|
|
{
|
|
|
|
return mysql_reload(pmysql);
|
|
|
|
}
|
|
|
|
|
|
|
|
|