From bcf7ceab370b7dcb1ac17fb0e69f0662c907d188 Mon Sep 17 00:00:00 2001 From: Lasse Karstensen Date: Tue, 16 Nov 2004 20:56:51 +0000 Subject: [PATCH] =?UTF-8?q?fjernet=20heng=20ved=20fors=C3=B8k=20p=C3=A5=20?= =?UTF-8?q?drop=20av=20ikke-eksisterende=20databaser.=20fjernet=20heng=20v?= =?UTF-8?q?ed=20fors=C3=B8k=20p=C3=A5=20create=20av=20eksisterende=20datab?= =?UTF-8?q?aser.=20fjernet=20mulighet=20for=20buffer=20overflows=20i=20dat?= =?UTF-8?q?abasenavnet.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mysql-dbadm.c | 64 +++++++++++++++++++++++++++++++++------------------ 1 file changed, 41 insertions(+), 23 deletions(-) diff --git a/mysql-dbadm.c b/mysql-dbadm.c index 6d299e1..f9d01c2 100644 --- a/mysql-dbadm.c +++ b/mysql-dbadm.c @@ -1,5 +1,5 @@ /* - * @(#) $Header: /tmp/cvs/mysql-admutils/mysql-dbadm.c,v 1.11 2003-04-08 18:31:53 lkarsten Exp $ + * @(#) $Header: /tmp/cvs/mysql-admutils/mysql-dbadm.c,v 1.12 2004-11-16 20:56:51 lkarsten Exp $ * * mysql-dbadm.c * @@ -87,9 +87,19 @@ usage() int create(MYSQL *pmysql, char *db) { - if (mysql_create_db(pmysql, db)) - return dberror(pmysql, "Cannot create database '%s'.", db); - + // hvis man forsøker å dra mysql_create_db() på en database som + // allerede finnes, så henger bare hele sql-kallet. Vi må derfor + // forsøke å selecte databasen først. Ettersom man alltid er root, + // går jo dette vanligvis bra. + // finner ut om denne finnes fra før. + if (!mysql_select_db(pmysql, db)) { + return dberror(pmysql, "Database '%s' already exists.", db); + } + mysql_select_db(pmysql, "mysql"); + // oppretter databasen. + if (mysql_create_db(pmysql, db)) { + return dberror(pmysql, "Cannot create database '%s'.", db); + } fprintf(stderr, "Database '%s' created.\n", db); return 0; } @@ -104,6 +114,12 @@ drop(MYSQL *pmysql, char *db) if (mysql_query(pmysql, query)) dberror(pmysql, "Failed to delete permissions for database '%s'.", db); + if (mysql_select_db(pmysql, db)) { + dberror(pmysql, "Database '%s' doesn't exists.", db); + return 0; + } + mysql_select_db(pmysql, "mysql"); + if (mysql_drop_db(pmysql, db)) return dberror(pmysql, "Cannot drop database '%s'.", db); @@ -406,6 +422,7 @@ main(int argc, char *argv[]) enum { c_create, c_drop, c_editperm, c_show } command; MYSQL mysql; char **dblist, **p; + char *db; program_name = argv[0]; @@ -464,37 +481,38 @@ main(int argc, char *argv[]) } free(dblist); } - else - { + else { + db = malloc(64); /* 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; - } + for (i = 2; i < argc; i++) { + // HE HE + strncpy(db, argv[i], 32); + db[33] = '\0'; - switch (command) - { + if (! (owner(db) || member(db))) { + dberror(NULL, "You are not the owner of '%s'. Skipping.", + db); + continue; + } + + switch (command) { case c_create: - create(&mysql, argv[i]); + create(&mysql, db); break; case c_drop: - drop(&mysql, argv[i]); + drop(&mysql, db); break; case c_editperm: - editperm(&mysql, argv[i]); + editperm(&mysql, db); break; case c_show: - show(&mysql, argv[i]); + show(&mysql, db); break; default: return dberror(NULL, "This point should never be reached!"); - } - } - } + } + } // for + } // else mysql_reload(&mysql); mysql_close(&mysql);