From fe7ad3f4889ddc1ee5eba2e72213f7636b3ea77e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20Lang=C3=A5s?= Date: Wed, 6 Mar 2002 16:47:12 +0000 Subject: [PATCH] =?UTF-8?q?N=C3=A5=20vil=20"mysql-dbadm=20show"=20ogs?= =?UTF-8?q?=C3=A5=20liste=20ut=20gruppenes=20databaser?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mysql-dbadm.c | 83 ++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 75 insertions(+), 8 deletions(-) diff --git a/mysql-dbadm.c b/mysql-dbadm.c index 17f412c..7faf7cd 100644 --- a/mysql-dbadm.c +++ b/mysql-dbadm.c @@ -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 * @@ -13,6 +13,7 @@ #include #include #include +#include #include #include #include "mysql-admutils.h" @@ -110,17 +111,48 @@ drop(MYSQL *pmysql, char *db) 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 */ char ** list(MYSQL *pmysql) { char *wild; + char **usr_groups, **cp; /* holds all group names this dude is a member of [tlan]*/ MYSQL_RES *res; - int rows; + int rows, numgroups, numgroupdbs; MYSQL_ROW row; char **dblist; - int i; + int i, counter; struct passwd *p; p = getpwuid(getuid()); @@ -129,31 +161,66 @@ list(MYSQL *pmysql) dberror(NULL, "Failed to lookup your UNIX username."); 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); sprintf(wild, "%s_%%", p->pw_name); res = mysql_list_dbs(pmysql, wild); rows = mysql_num_rows(res); - dblist = malloc((rows + 2) * sizeof(char *)); /* one for the username - (might not be used)*/ + dblist = realloc(dblist, (numgroupdbs+rows+2) * sizeof(char *)); + if (!dblist) { dberror(NULL, "Out of memory.\n"); free(wild); return NULL; } + for (i = 0; i < rows; i++) if ((row = mysql_fetch_row(res))) { - dblist[i] = strdup(row[0]); + dblist[counter++] = strdup(row[0]); } res = mysql_list_dbs(pmysql, p->pw_name); rows = mysql_num_rows(res); if (rows == 1) - dblist[i++] = strdup(p->pw_name); + dblist[counter++] = strdup(p->pw_name); - dblist[i] = NULL; + dblist[counter] = NULL; return dblist; }