From 5a1f013a3133b48ca61d2f4a5ab1e7545ed88913 Mon Sep 17 00:00:00 2001 From: Geir Hauge Date: Tue, 15 Aug 2006 11:51:59 +0000 Subject: [PATCH] beautifisering (2-space-indent), og tetting av minnehull --- common.c | 110 +++++++++-------- mysql-dbadm.c | 328 +++++++++++++++++++++++++------------------------- 2 files changed, 218 insertions(+), 220 deletions(-) diff --git a/common.c b/common.c index da14617..6ad0d56 100644 --- a/common.c +++ b/common.c @@ -1,5 +1,5 @@ /* - * @(#) $Header: /tmp/cvs/mysql-admutils/common.c,v 1.7 2006-08-15 10:32:42 geirha Exp $ + * @(#) $Header: /tmp/cvs/mysql-admutils/common.c,v 1.8 2006-08-15 11:51:58 geirha Exp $ * * functions used by mysql-dbadm.c and mysql-useradm.c * @@ -22,7 +22,7 @@ char *program_name; -static char *rcsheader = "@(#) " PACKAGE " " VERSION " orakel@ntnu.no $Header: /tmp/cvs/mysql-admutils/common.c,v 1.7 2006-08-15 10:32:42 geirha Exp $"; +static char *rcsheader = "@(#) " PACKAGE " " VERSION " orakel@ntnu.no $Header: /tmp/cvs/mysql-admutils/common.c,v 1.8 2006-08-15 11:51:58 geirha Exp $"; int @@ -38,14 +38,13 @@ 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'); - } + 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); @@ -66,14 +65,13 @@ dberror(MYSQL *pmysql, char *format, ...) va_end(ap); fprintf(stderr, "\n"); - if (pmysql) - { - errmsg = mysql_error(pmysql); - if ((errmsg) && (strcmp(errmsg, "") != 0)) - fprintf(stderr, "mysql: %s\n", errmsg); + if (pmysql) { + errmsg = mysql_error(pmysql); + if ((errmsg) && (strcmp(errmsg, "") != 0)) + fprintf(stderr, "mysql: %s\n", errmsg); - mysql_close(pmysql); - } + mysql_close(pmysql); + } return 1; } @@ -102,11 +100,10 @@ owner(char *name) struct passwd *p; p = getpwuid(getuid()); - if (!p) - { - dberror(NULL, "Failed to look up your UNIX username."); - exit(1); - } + if (!p) { + dberror(NULL, "Failed to look up your UNIX username."); + exit(1); + } if (strcmp(name, p->pw_name) == 0) return 1; /* OK */ @@ -182,8 +179,9 @@ member(char *gr) { printf("You have access to '%s'\n", gr); #endif return 1; /* OK */ - } else + } else { *g->gr_mem++; + } } #if DEBUG printf("You have no access to '%s'\n", gr); @@ -192,15 +190,15 @@ member(char *gr) { // brukeren var ikke med i gruppen, så vi må prøve neste. // finner den siste _-en i navnet. - foo = strrchr(group, '_'); - if (foo == NULL) { + foo = strrchr(group, '_'); + if (foo == NULL) { // hvis vi har kommet helt til bunn, og fremdeles // ikke har blitt avbrutt, fantes det ingen slik gruppe. - return 0; - } - // det som nå er bak siste underscore er passe uinteressant, så - // vi flytter slutten av strengen litt framover. - *foo = '\0'; + return 0; + } + // det som nå er bak siste underscore er passe uinteressant, så + // vi flytter slutten av strengen litt framover. + *foo = '\0'; } // while } @@ -209,40 +207,40 @@ member(char *gr) { /* numgroups is the total number of groups found */ char **get_group_names(int *numgroups) { - char **grouplist; - gid_t gids[33]; - int real_nr_groups, nr_groups, i; - struct group *g; + char **grouplist; + gid_t gids[33]; + int real_nr_groups, nr_groups, i; + struct group *g; - nr_groups = 0; + nr_groups = 0; - nr_groups = getgroups(32, &gids[0]); /* Allow a max of 32 groups */ + 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; - } + if (nr_groups == -1) { + dberror(NULL, "Error while trying to fetch group info"); + return NULL; + } - grouplist = malloc(sizeof(char *)); - real_nr_groups = 0; + grouplist = malloc(sizeof(char *)); + real_nr_groups = 0; - for (i = 0; i < nr_groups; i++) { - g = getgrgid(gids[i]); + for (i = 0; i < nr_groups; i++) { + g = getgrgid(gids[i]); - /* Go to next grp if it doesn't have a name */ - if (g != NULL) { - grouplist = (char **) realloc(grouplist, (real_nr_groups+2) * sizeof(char *)); - grouplist[real_nr_groups] = strdup(g->gr_name); - real_nr_groups++; - } else { - fprintf(stderr, "Omitting gid %d, no entry in group-file.\n", gids[i]); - } - } - grouplist[real_nr_groups] = NULL; + /* Go to next grp if it doesn't have a name */ + if (g != NULL) { + grouplist = (char **) realloc(grouplist, (real_nr_groups+2) * sizeof(char *)); + grouplist[real_nr_groups] = strdup(g->gr_name); + real_nr_groups++; + } else { + fprintf(stderr, "Omitting gid %d, no entry in group-file.\n", gids[i]); + } + } + grouplist[real_nr_groups] = NULL; - *numgroups = real_nr_groups; + *numgroups = real_nr_groups; - return grouplist; + return grouplist; } diff --git a/mysql-dbadm.c b/mysql-dbadm.c index 9dddcdf..332e482 100644 --- a/mysql-dbadm.c +++ b/mysql-dbadm.c @@ -1,5 +1,5 @@ /* - * @(#) $Header: /tmp/cvs/mysql-admutils/mysql-dbadm.c,v 1.15 2006-08-15 10:32:42 geirha Exp $ + * @(#) $Header: /tmp/cvs/mysql-admutils/mysql-dbadm.c,v 1.16 2006-08-15 11:51:59 geirha Exp $ * * mysql-dbadm.c * @@ -20,14 +20,14 @@ /* New database names may only use these characters in their identifier */ const char dbname_validchars[] = - "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-"; + "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-"; /* Returns true if dbname contains only characters in dbname_validchars. */ int dbname_isclean(char* dbname) { - int reallen, cleanlen; - reallen = strlen(dbname); - cleanlen = strspn(dbname, dbname_validchars); - return (reallen == cleanlen); + int reallen, cleanlen; + reallen = strlen(dbname); + cleanlen = strspn(dbname, dbname_validchars); + return (reallen == cleanlen); } char * @@ -55,10 +55,8 @@ strtok_whitespace(char *s) cp = strchr_whitespace(r); if (cp == NULL) return r; - while ((*cp == ' ') || (*cp == '\t')) - { - *cp++ = '\0'; - } + while ((*cp == ' ') || (*cp == '\t')) + *cp++ = '\0'; return r; } @@ -105,14 +103,14 @@ create(MYSQL *pmysql, char *db) // 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); + return dberror(pmysql, "Database '%s' already exists.", db); } mysql_select_db(pmysql, "mysql"); // oppretter databasen. char query[1024]; sprintf(query, "create database `%s`", db); if (mysql_query(pmysql, query)) - return dberror(pmysql, "Cannot create database '%s'.", db); + return dberror(pmysql, "Cannot create database '%s'.", db); fprintf(stderr, "Database '%s' created.\n", db); return 0; } @@ -128,8 +126,8 @@ drop(MYSQL *pmysql, char *db) 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; + dberror(pmysql, "Database '%s' doesn't exists.", db); + return 0; } mysql_select_db(pmysql, "mysql"); @@ -154,16 +152,15 @@ list(MYSQL *pmysql) int i, counter; struct passwd *p; -// variabler som brukes til å escape gruppenavnet til mysqlvennlig format. + // variabler som brukes til å escape gruppenavnet til mysqlvennlig format. char escaped_user[64]; char *cp_kopi; p = getpwuid(getuid()); - if (!p) - { - dberror(NULL, "Failed to lookup your UNIX username."); - exit(1); - } + if (!p) { + dberror(NULL, "Failed to lookup your UNIX username."); + exit(1); + } dblist = malloc(2 * sizeof(char*)); /* one for username (if used), rest is done below */ @@ -174,7 +171,7 @@ list(MYSQL *pmysql) cp = usr_groups; while (*cp) { // itererer over alle grupper en person er med i if (*cp == NULL) - break; + break; #ifdef DEBUG printf("cp er %s\n", *cp); @@ -185,13 +182,13 @@ list(MYSQL *pmysql) // itererer over bokstavene i gruppenavn, og escaper spesialtegn. for(i=0; i<=strlen(cp_kopi); i++) { - // hvis % _ , så skriv \ og så tegn - if ((cp_kopi[i] == '_') || (cp_kopi[i] == '%')) { - strcat(escaped_user, "\\"); - } + // hvis % _ , så skriv \ og så tegn + if ((cp_kopi[i] == '_') || (cp_kopi[i] == '%')) { + strcat(escaped_user, "\\"); + } - escaped_user[strlen(escaped_user) + 1] = '\0'; - escaped_user[strlen(escaped_user)] = cp_kopi[i]; + escaped_user[strlen(escaped_user) + 1] = '\0'; + escaped_user[strlen(escaped_user)] = cp_kopi[i]; } // for wild = malloc(strlen(escaped_user)+4); @@ -209,47 +206,51 @@ list(MYSQL *pmysql) dblist = realloc(dblist, (numgroupdbs+2) * sizeof(char *)); - for (i = 0; i < rows; i++) - if ((row = mysql_fetch_row(res))) { - dblist[counter++] = strdup(row[0]); - } + for (i = 0; i < rows; i++) { + if ((row = mysql_fetch_row(res))) { + dblist[counter++] = strdup(row[0]); + } + } } free(wild); + free(res); + free(cp_kopi); cp++; } wild = malloc(strlen(p->pw_name) + 4); sprintf(wild, "%s\\_%%", p->pw_name); #ifdef DEBUG - printf("dbadm: wildcard: '%s'\n", wild); + printf("dbadm: wildcard: '%s'\n", wild); #endif res = mysql_list_dbs(pmysql, wild); rows = mysql_num_rows(res); dblist = realloc(dblist, (numgroupdbs+rows+2) * sizeof(char *)); - if (!dblist) - { - dberror(NULL, "Out of memory.\n"); - free(wild); - return NULL; - } + 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[counter++] = strdup(row[0]); - } + for (i = 0; i < rows; i++) { + if ((row = mysql_fetch_row(res))) { + dblist[counter++] = strdup(row[0]); + } + } res = mysql_list_dbs(pmysql, p->pw_name); rows = mysql_num_rows(res); if (rows == 1) - dblist[counter++] = strdup(p->pw_name); + dblist[counter++] = strdup(p->pw_name); dblist[counter] = NULL; - + + free(wild); + free(usr_groups); return dblist; } @@ -263,26 +264,27 @@ writeperm(FILE *f, MYSQL *pmysql, const char *db) MYSQL_ROW row; sprintf(query, "select user,select_priv,insert_priv,update_priv," - "delete_priv,create_priv,drop_priv,alter_priv,index_priv from db where db='%s'", db); + "delete_priv,create_priv,drop_priv,alter_priv,index_priv from db where db='%s'", db); if (mysql_query(pmysql, query)) return dberror(pmysql, "Query for permissions failed."); res = mysql_store_result(pmysql); rows = mysql_num_rows(res); fprintf(f, "# User " - "Select Insert Update Delete Create Drop Alter Index\n"); + "Select Insert Update Delete Create Drop Alter Index\n"); fprintf(f, "# ---------------- " - "------ ------ ------ ------ ------ ---- ----- -----\n"); + "------ ------ ------ ------ ------ ---- ----- -----\n"); if (rows == 0) - fprintf(f, "# (no permissions currently granted to any users)\n"); - else - for (i = 0; i < rows; i++) - { - row = mysql_fetch_row(res); - fprintf(f, " %-16s %-7s %-7s %-7s %-7s %-7s %-7s %-7s %s\n", - row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8]); - } + fprintf(f, "# (no permissions currently granted to any users)\n"); + else { + for (i = 0; i < rows; i++) { + row = mysql_fetch_row(res); + fprintf(f, " %-16s %-7s %-7s %-7s %-7s %-7s %-7s %-7s %s\n", + row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8]); + } + } + free(res); return 0; } @@ -334,76 +336,74 @@ editperm(MYSQL *pmysql, const char *db) strcpy(cmd, editor); strcat(cmd, " "); strcat(cmd, fn); - if (system(cmd) == -1) - { - dberror(NULL, "Failed to execute '%s'\n", cmd); - perror("system"); - return 1; - } + if (system(cmd) == -1) { + dberror(NULL, "Failed to execute '%s'\n", cmd); + perror("system"); + return 1; + } /* parse */ f = fopen(fn, "r"); lines = 0; - while (fgets(line, sizeof(line), f)) - { - cp = strchr(line, '\n'); - if (cp) - *cp = '\0'; + while (fgets(line, sizeof(line), f)) { + cp = strchr(line, '\n'); + if (cp) + *cp = '\0'; - cp = line; - while (*cp && ((*cp == ' ') || (*cp == '\t'))) cp++; - if (*cp == '\0') continue; - if (*cp == '#') continue; - if (*cp == '\n') continue; + cp = line; + while (*cp && ((*cp == ' ') || (*cp == '\t'))) cp++; + if (*cp == '\0') continue; + if (*cp == '#') continue; + if (*cp == '\n') continue; - #define STRTOK_WHITESPACE(res, start) \ - { if (!(res = strtok_whitespace(start))) continue; } + #define STRTOK_WHITESPACE(res, start) \ + { if (!(res = strtok_whitespace(start))) continue; } - STRTOK_WHITESPACE(user, cp); - if (strlen(user) < 1) - return dberror(NULL, "Invalid user '%s' in grant line %d.", user, - lines + 1); - if (strcmp(user, "%") == 0) - *user = '\0'; /* ugly, but it works... */ + STRTOK_WHITESPACE(user, cp); + if (strlen(user) < 1) + return dberror(NULL, "Invalid user '%s' in grant line %d.", user, + lines + 1); + if (strcmp(user, "%") == 0) + *user = '\0'; /* ugly, but it works... */ #define CHECK_PRIV(PRIV) \ if (!valid_priv(PRIV)) return dberror(NULL, "Invalid value '%s' " \ "in grant line %d.", PRIV, lines + 1) - STRTOK_WHITESPACE(select_priv, NULL); - CHECK_PRIV(select_priv); - STRTOK_WHITESPACE(insert_priv, NULL); - CHECK_PRIV(insert_priv); - STRTOK_WHITESPACE(update_priv, NULL); - CHECK_PRIV(update_priv); - STRTOK_WHITESPACE(delete_priv, NULL); - CHECK_PRIV(delete_priv); - STRTOK_WHITESPACE(create_priv, NULL); - CHECK_PRIV(create_priv); - STRTOK_WHITESPACE(drop_priv, NULL); - CHECK_PRIV(drop_priv); - STRTOK_WHITESPACE(alter_priv, NULL); - CHECK_PRIV(alter_priv); - STRTOK_WHITESPACE(index_priv, NULL); - CHECK_PRIV(index_priv); + STRTOK_WHITESPACE(select_priv, NULL); + CHECK_PRIV(select_priv); + STRTOK_WHITESPACE(insert_priv, NULL); + CHECK_PRIV(insert_priv); + STRTOK_WHITESPACE(update_priv, NULL); + CHECK_PRIV(update_priv); + STRTOK_WHITESPACE(delete_priv, NULL); + CHECK_PRIV(delete_priv); + STRTOK_WHITESPACE(create_priv, NULL); + CHECK_PRIV(create_priv); + STRTOK_WHITESPACE(drop_priv, NULL); + CHECK_PRIV(drop_priv); + STRTOK_WHITESPACE(alter_priv, NULL); + CHECK_PRIV(alter_priv); + STRTOK_WHITESPACE(index_priv, NULL); + CHECK_PRIV(index_priv); #undef STRTOK_WHITESPACE #undef CHECK_PRIV - sprintf(query, "insert into db (host, db, user, select_priv, insert_priv, " - "update_priv, delete_priv, create_priv, drop_priv, alter_priv, index_priv) values " - "('%%', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s')", - db, user, select_priv, insert_priv, update_priv, delete_priv, - create_priv, drop_priv, alter_priv, index_priv); - queries[lines] = strdup(query); - lines++; - if (lines >= MAX_GRANTS) - { - dberror(NULL, "Warning: Maximum of %d grants reached.\n", - MAX_GRANTS); - continue; - } - } /* while fgets ... */ + sprintf(query, "insert into db (host, db, user, select_priv, insert_priv, " + "update_priv, delete_priv, create_priv, drop_priv, alter_priv, index_priv) values " + "('%%', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s')", + db, user, select_priv, insert_priv, update_priv, delete_priv, + create_priv, drop_priv, alter_priv, index_priv); + queries[lines] = strdup(query); + lines++; + if (lines >= MAX_GRANTS) + { + dberror(NULL, "Warning: Maximum of %d grants reached.\n", + MAX_GRANTS); + continue; + } + } /* while fgets ... */ unlink(fn); fclose(f); @@ -416,14 +416,14 @@ editperm(MYSQL *pmysql, const char *db) dberror(pmysql, "Failed to delete old grants for '%s'.", db); for (i = 0; i < lines; i++) - { - #ifdef DEBUG - puts(queries[i]); - putchar('\n'); - #endif - if (mysql_query(pmysql, queries[i])) - dberror(pmysql, "Failed to insert grant line %d.", i + 1); - } + { +#ifdef DEBUG + puts(queries[i]); + putchar('\n'); +#endif + if (mysql_query(pmysql, queries[i])) + dberror(pmysql, "Failed to insert grant line %d.", i + 1); + } return 0; } @@ -480,60 +480,60 @@ main(int argc, char *argv[]) /* connect to the database server and select the mysql database */ if (!mysql_real_connect(&mysql, db_server, db_user, db_passwd, db_name, 0, NULL, 0)) return dberror(&mysql, "Cannot connect to database server '%s'.", - db_server); + 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); + if ((command == c_show) && (argc == 2)) { + dblist = list(&mysql); + p = dblist; + while (*p) { + show(&mysql, *p); + free(*p); + p++; } + free(*p); + free(dblist); + } else { - db = malloc(64); - /* for each supplied database name, perform the requested action */ - for (i = 2; i < argc; i++) { - // HE HE - strncpy(db, argv[i], 32); - db[33] = '\0'; + db = malloc(64); + /* for each supplied database name, perform the requested action */ + for (i = 2; i < argc; i++) { + // HE HE + strncpy(db, argv[i], 32); + db[32] = '\0'; - if (! (owner(db) || member(db))) { - dberror(NULL, "You are not the owner of '%s'. Skipping.", + if (! (owner(db) || member(db))) { + dberror(NULL, "You are not the owner of '%s'. Skipping.", db); - continue; - } + continue; + } - switch (command) { - case c_create: - // We only check newly created databases. Many old ("unclean") databases are still in use. - if(dbname_isclean(db)) { - create(&mysql, db); - } else { - dberror(NULL, "Database name '%s' contains invalid characters.\n" - "Only A-Z, a-z, 0-9, _ (underscore) and - (dash) permitted. Skipping.", db); - } - break; - case c_drop: - drop(&mysql, db); - break; - case c_editperm: - editperm(&mysql, db); - break; - case c_show: - show(&mysql, db); - break; - default: - return dberror(NULL, "This point should never be reached!"); - } - } // for - } // else + switch (command) { + case c_create: + // We only check newly created databases. Many old ("unclean") databases are still in use. + if(dbname_isclean(db)) { + create(&mysql, db); + } else { + dberror(NULL, "Database name '%s' contains invalid characters.\n" + "Only A-Z, a-z, 0-9, _ (underscore) and - (dash) permitted. Skipping.", db); + } + break; + case c_drop: + drop(&mysql, db); + break; + case c_editperm: + editperm(&mysql, db); + break; + case c_show: + show(&mysql, db); + break; + default: + return dberror(NULL, "This point should never be reached!"); + } + } // for + free(db); + } // else mysql_reload(&mysql); mysql_close(&mysql);