Nå vil "mysql-dbadm show" også liste ut gruppenes databaser
This commit is contained in:
parent
c4b3c3bd04
commit
fe7ad3f488
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* @(#) $Header: /tmp/cvs/mysql-admutils/mysql-dbadm.c,v 1.3 2002-03-06 02:28:27 tlan Exp $
|
* @(#) $Header: /tmp/cvs/mysql-admutils/mysql-dbadm.c,v 1.4 2002-03-06 16:47:12 tlan Exp $
|
||||||
*
|
*
|
||||||
* mysql-dbadm.c
|
* mysql-dbadm.c
|
||||||
*
|
*
|
||||||
|
@ -13,6 +13,7 @@
|
||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <pwd.h>
|
#include <pwd.h>
|
||||||
|
#include <grp.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include "mysql-admutils.h"
|
#include "mysql-admutils.h"
|
||||||
|
@ -110,17 +111,48 @@ drop(MYSQL *pmysql, char *db)
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* 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;
|
||||||
|
}
|
||||||
|
|
||||||
/* return a list of the user's databases */
|
/* return a list of the user's databases */
|
||||||
char **
|
char **
|
||||||
list(MYSQL *pmysql)
|
list(MYSQL *pmysql)
|
||||||
{
|
{
|
||||||
char *wild;
|
char *wild;
|
||||||
|
char **usr_groups, **cp; /* holds all group names this dude is a member of [tlan]*/
|
||||||
MYSQL_RES *res;
|
MYSQL_RES *res;
|
||||||
int rows;
|
int rows, numgroups, numgroupdbs;
|
||||||
MYSQL_ROW row;
|
MYSQL_ROW row;
|
||||||
char **dblist;
|
char **dblist;
|
||||||
int i;
|
int i, counter;
|
||||||
struct passwd *p;
|
struct passwd *p;
|
||||||
|
|
||||||
p = getpwuid(getuid());
|
p = getpwuid(getuid());
|
||||||
|
@ -129,31 +161,66 @@ list(MYSQL *pmysql)
|
||||||
dberror(NULL, "Failed to lookup your UNIX username.");
|
dberror(NULL, "Failed to lookup your UNIX username.");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
dblist = malloc(2 * sizeof(char*)); /* one for username (if used), rest is done below */
|
||||||
|
|
||||||
|
numgroupdbs = 0;
|
||||||
|
counter = 0;
|
||||||
|
|
||||||
|
usr_groups = get_group_names(&numgroups);
|
||||||
|
cp = usr_groups;
|
||||||
|
while (*cp) {
|
||||||
|
if (*cp == NULL)
|
||||||
|
break;
|
||||||
|
|
||||||
|
wild = malloc(strlen(*cp)+3);
|
||||||
|
sprintf(wild, "%s_%%", *cp);
|
||||||
|
|
||||||
|
res = mysql_list_dbs(pmysql, wild);
|
||||||
|
rows = mysql_num_rows(res);
|
||||||
|
|
||||||
|
if (rows > 0) {
|
||||||
|
numgroupdbs += rows;
|
||||||
|
|
||||||
|
dblist = realloc(dblist, (numgroupdbs+2) * sizeof(char *));
|
||||||
|
|
||||||
|
for (i = 0; i < rows; i++)
|
||||||
|
if ((row = mysql_fetch_row(res))) {
|
||||||
|
dblist[counter++] = strdup(row[0]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
free(wild);
|
||||||
|
cp++;
|
||||||
|
}
|
||||||
|
|
||||||
wild = malloc(strlen(p->pw_name) + 3);
|
wild = malloc(strlen(p->pw_name) + 3);
|
||||||
sprintf(wild, "%s_%%", p->pw_name);
|
sprintf(wild, "%s_%%", p->pw_name);
|
||||||
|
|
||||||
res = mysql_list_dbs(pmysql, wild);
|
res = mysql_list_dbs(pmysql, wild);
|
||||||
rows = mysql_num_rows(res);
|
rows = mysql_num_rows(res);
|
||||||
dblist = malloc((rows + 2) * sizeof(char *)); /* one for the username
|
dblist = realloc(dblist, (numgroupdbs+rows+2) * sizeof(char *));
|
||||||
(might not be used)*/
|
|
||||||
if (!dblist)
|
if (!dblist)
|
||||||
{
|
{
|
||||||
dberror(NULL, "Out of memory.\n");
|
dberror(NULL, "Out of memory.\n");
|
||||||
free(wild);
|
free(wild);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (i = 0; i < rows; i++)
|
for (i = 0; i < rows; i++)
|
||||||
if ((row = mysql_fetch_row(res)))
|
if ((row = mysql_fetch_row(res)))
|
||||||
{
|
{
|
||||||
dblist[i] = strdup(row[0]);
|
dblist[counter++] = strdup(row[0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
res = mysql_list_dbs(pmysql, p->pw_name);
|
res = mysql_list_dbs(pmysql, p->pw_name);
|
||||||
rows = mysql_num_rows(res);
|
rows = mysql_num_rows(res);
|
||||||
if (rows == 1)
|
if (rows == 1)
|
||||||
dblist[i++] = strdup(p->pw_name);
|
dblist[counter++] = strdup(p->pw_name);
|
||||||
|
|
||||||
dblist[i] = NULL;
|
dblist[counter] = NULL;
|
||||||
|
|
||||||
return dblist;
|
return dblist;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue