/* $Id: dspam_pg2int8.c,v 1.11 2006/05/13 01:13:01 jonz Exp $ */ /* DSPAM COPYRIGHT (C) 2002-2006 JONATHAN A. ZDZIARSKI This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; version 2 of the License. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #ifdef HAVE_CONFIG_H #include #endif #include #include #include #include #include #include #include #include #include #include #include #include "libdspam.h" #include "util.h" #include "read_config.h" #include "config_api.h" #include "language.h" #include "storage_driver.h" #include "pgsql_drv.h" DSPAM_CTX *open_ctx, *open_mtx; int opt_humanfriendly; void dieout (int signal); void usage (void); void GenSQL (PGconn *dbh,const char *file); void OutputMessage(DSPAM_CTX *open_ctx,char *sqlfile); #ifndef PATH_MAX # define PATH_MAX 1024 #endif /* ** Type OIDs; values come from postgresql/include/server/catalog/pg_type.h */ #ifndef NUMERICOID # define NUMERICOID 1700 #endif #ifndef INT8OID # define INT8OID 20 #endif #define BIGINTOID INT8OID int main (int argc, char **argv) { #ifndef _WIN32 #ifdef TRUSTED_USER_SECURITY struct passwd *p = getpwuid (getuid ()); #endif #endif struct _pgsql_drv_storage *store; char file[PATH_MAX+1]; int i, ch; #ifndef HAVE_GETOPT int optind = 1; #endif /* Read dspam.conf */ agent_config = read_config(NULL); if (!agent_config) { LOG(LOG_ERR, ERR_AGENT_READ_CONFIG); exit(EXIT_FAILURE); } if (!_ds_read_attribute(agent_config, "Home")) { LOG(LOG_ERR, ERR_AGENT_DSPAM_HOME); _ds_destroy_config(agent_config); exit(EXIT_FAILURE); } #ifndef _WIN32 #ifdef TRUSTED_USER_SECURITY if (!_ds_match_attribute(agent_config, "Trust", p->pw_name) && p->pw_uid) { fprintf(stderr, ERR_TRUSTED_MODE "\n"); _ds_destroy_config(agent_config); exit(EXIT_FAILURE); } #endif #endif for(i=0;istorage); GenSQL(store->dbh,file); //PQfinish(store->dbh); OutputMessage(open_ctx,file); if (open_ctx != NULL) dspam_destroy (open_ctx); if (open_mtx != NULL) dspam_destroy (open_mtx); _ds_destroy_config(agent_config); exit (EXIT_SUCCESS); } #define TOKEN_TYPE_LEN 20 #define TOKEN_DATA_LEN 30 void GenSQL (PGconn *dbh,const char *file) { int i,ntuples; int reverse=0; int type_check=0; int preamble=0; PGresult *result; Oid col_type; FILE *out; char token_data[TOKEN_DATA_LEN+1]; char token_type[TOKEN_TYPE_LEN+1]; unsigned long long token; memset((void *)token_type, 0, TOKEN_TYPE_LEN+1); memset((void *)token_data, 0, TOKEN_DATA_LEN+1); if (strncmp(file,"-",1)==0) { out=stdout; } else { if ( (out = fopen(file,"w+")) == NULL ) { fprintf(stderr, "Failed to open file %s for writing - %s\n", file, strerror(errno)); PQfinish(dbh); exit(EXIT_FAILURE); } } result = PQexec(dbh, "SELECT uid, token, spam_hits, innocent_hits, last_hit " "FROM dspam_token_data"); if (! result || PQresultStatus(result) != PGRES_TUPLES_OK) { fprintf(stderr, "Failed to run result: %s\n", PQresultErrorMessage(result)); if (result) PQclear(result); PQfinish(dbh); exit(EXIT_FAILURE); } ntuples = PQntuples(result); for (i=0; iconfig->attributes, "PgSQLServer")) { char *p; strlcpy(hostname, _ds_read_attribute(open_ctx->config->attributes, "PgSQLServer"), sizeof(hostname)); if (_ds_read_attribute(open_ctx->config->attributes, "PgSQLPort")) port = atoi(_ds_read_attribute(open_ctx->config->attributes, "PgSQLPort")); else port = 0; if ((p = _ds_read_attribute(open_ctx->config->attributes, "PgSQLUser"))) strlcpy(user, p, sizeof(user)); if ((p = _ds_read_attribute(open_ctx->config->attributes, "PgSQLDb"))) strlcpy(db, p, sizeof(db)); } else { snprintf (filename, MAX_FILENAME_LENGTH, "%s/pgsql.data", open_ctx->home); file = fopen (filename, "r"); if (file == NULL) { fprintf(stderr, "Failed to open config file %s - %s\n", filename, strerror(errno)); dieout(0); } db[0] = 0; while (fgets (buffer, sizeof (buffer), file) != NULL) { chomp (buffer); if (!i) strlcpy (hostname, buffer, sizeof (hostname)); else if (i == 1) port = atoi (buffer); else if (i == 2) strlcpy (user, buffer, sizeof (user)); else if (i == 4) strlcpy (db, buffer, sizeof (db)); i++; } fclose (file); } if (db[0] == 0) { fprintf(stderr, "file %s: incomplete pgsql connect data", filename); dieout(0); } if (port == 0) port = 5432; fprintf(stderr, "Created SQL in %s; run using:\n", sqlfile); if ( strlen(hostname) == 0 ) fprintf(stderr, "\tpsql -q -p %d -U %s -d %s -f %s -v AUTOCOMMIT=off\n", port, user, db, sqlfile); else fprintf(stderr, "\tpsql -q -h %s -p %d -U %s -d %s -f %s -v AUTOCOMMIT=off\n", hostname, port, user, db, sqlfile); } void dieout (int signal) { fprintf (stderr, "terminated.\n"); if (open_ctx != NULL) dspam_destroy (open_ctx); if (open_mtx != NULL) dspam_destroy (open_mtx); _ds_destroy_config(agent_config); exit (EXIT_SUCCESS); } void usage (void) { (void)fprintf (stderr, "Usage: dspam_pg2int8 [-h] file\n" "\tCreates SQL file to migrate from NUMERIC to BIGINT type and vice-versa in PostgreSQL.\n" "\t-h: print this message\n"); _ds_destroy_config(agent_config); exit(EXIT_FAILURE); }