? .fe-connect.c.swp ? libpq.so.2.2 Index: fe-connect.c =================================================================== RCS file: /projects/cvsroot/pgsql-server/src/interfaces/libpq/fe-connect.c,v retrieving revision 1.190 diff -c -r1.190 fe-connect.c *** fe-connect.c 2002/07/20 05:43:31 1.190 --- fe-connect.c 2002/08/09 05:10:38 *************** *** 107,113 **** {"user", "PGUSER", NULL, NULL, "Database-User", "", 20}, ! {"password", "PGPASSWORD", DefaultPassword, NULL, "Database-Password", "*", 20}, {"dbname", "PGDATABASE", NULL, NULL, --- 107,113 ---- {"user", "PGUSER", NULL, NULL, "Database-User", "", 20}, ! {"password", NULL, DefaultPassword, NULL, "Database-Password", "*", 20}, {"dbname", "PGDATABASE", NULL, NULL, *************** *** 182,187 **** --- 182,189 ---- static void defaultNoticeProcessor(void *arg, const char *message); static int parseServiceInfo(PQconninfoOption *options, PQExpBuffer errorMessage); + char *getPasswordFromFile(char *hostname, char *port, char *dbname, + char *username, char *pwdfile); /* * Connecting to a Database *************** *** 386,392 **** * * PGUSER Postgres username to associate with the connection. * ! * PGPASSWORD The user's password. * * PGDATABASE name of database to which to connect if * argument is NULL or a null string --- 388,396 ---- * * PGUSER Postgres username to associate with the connection. * ! * PGPASSWORDFILE ! * A file that contains host:port:database:user:password ! * for authentication * * PGDATABASE name of database to which to connect if * argument is NULL or a null string *************** *** 408,413 **** --- 412,418 ---- char *tmp; /* An error message from some service we * call. */ bool error = FALSE; /* We encountered an error. */ + char *pwdFile = NULL; conn = makeEmptyPGconn(); if (conn == NULL) *************** *** 476,488 **** libpq_gettext("could not determine the PostgreSQL user name to use\n")); } - if (pwd) - conn->pgpass = strdup(pwd); - else if ((tmp = getenv("PGPASSWORD")) != NULL) - conn->pgpass = strdup(tmp); - else - conn->pgpass = strdup(DefaultPassword); - if (dbName == NULL) { if ((tmp = getenv("PGDATABASE")) != NULL) --- 481,486 ---- *************** *** 493,499 **** --- 491,508 ---- else conn->dbName = strdup(dbName); + if ((tmp = getenv("PGPASSWORDFILE")) != NULL) + pwdFile = strdup(tmp); + /* getPasswordFromFile mallocs its result, so we don't need strdup here */ + if (pwd) + conn->pgpass = strdup(pwd); + else if ((tmp = getPasswordFromFile(conn->pghost, conn->pgport, + conn->dbName, conn->pguser, pwdFile)) != NULL) + conn->pgpass = tmp; + else + conn->pgpass = strdup(DefaultPassword); + #ifdef USE_SSL if ((tmp = getenv("PGREQUIRESSL")) != NULL) conn->require_ssl = (tmp[0] == '1') ? true : false; *************** *** 2809,2812 **** --- 2818,2872 ---- (void) arg; /* not used */ /* Note: we expect the supplied string to end with a newline already. */ fprintf(stderr, "%s", message); + } + + /* get a password from the password file. */ + char * + getPasswordFromFile(char *hostname, char *port, char *dbname, + char *username, char *pwdfile) + { + FILE *fp; + char *prepended; + int length; + #define LINELENGTH 256 + char buf[LINELENGTH]; + + if (pwdfile == NULL || strcmp(pwdfile, "") == 0) + return NULL; + + if (dbname == NULL || strcmp(dbname, "") == 0) + return NULL; + + if (username == NULL || strcmp(username, "") == 0) + return NULL; + + if (hostname == NULL) + hostname = DefaultHost; + + if (port == NULL) + port = DEF_PGPORT_STR; + + fp = fopen(pwdfile, "r"); + if (fp == NULL) + return NULL; + + length = strlen(hostname) + strlen(port) + strlen(dbname) + + strlen(username) + 5; + + prepended = malloc(sizeof(char) * length); + snprintf(prepended, length, "%s:%s:%s:%s:", + hostname, port, dbname, username); + + fgets(buf, LINELENGTH, fp); + while (!feof(fp)) { + buf[strlen(buf) - 1] = 0; + if (strncasecmp(prepended, buf, length - 1) == 0) { + char *pwd; + pwd = strdup(buf + length - 1); + free(prepended); + return pwd; + } + fgets(buf, LINELENGTH, fp); + } + return NULL; } Index: libpq-int.h =================================================================== RCS file: /projects/cvsroot/pgsql-server/src/interfaces/libpq/libpq-int.h,v retrieving revision 1.52 diff -c -r1.52 libpq-int.h *** libpq-int.h 2002/07/20 05:43:31 1.52 --- libpq-int.h 2002/08/09 05:10:39 *************** *** 354,359 **** --- 354,360 ---- #define DefaultOption "" #define DefaultAuthtype "" #define DefaultPassword "" + #define DefaultPasswordFile "" /* * this is so that we can check is a connection is non-blocking internally