Skip to content
Snippets Groups Projects
Commit 329b38ee authored by Bryan Henderson's avatar Bryan Henderson
Browse files

Add PGUSER environment variable for client to specify Postgres username.

parent ca5f6dba
Branches
Tags
No related merge requests found
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-connect.c,v 1.8 1996/08/19 13:38:42 scrappy Exp $ * $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-connect.c,v 1.9 1996/10/10 08:20:09 bryanh Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -21,8 +21,9 @@ ...@@ -21,8 +21,9 @@
#include <string.h> #include <string.h>
#include <netdb.h> #include <netdb.h>
#include <errno.h> #include <errno.h>
#include "libpq/pqcomm.h" /* for decls of MsgType, PacketBuf, StartupInfo */ #include <signal.h>
#include "fe-auth.h" #include <libpq/pqcomm.h> /* for decls of MsgType, PacketBuf, StartupInfo */
#include <fe-auth.h>
#include "libpq-fe.h" #include "libpq-fe.h"
#if defined(PORTNAME_ultrix4) || defined(PORTNAME_next) #if defined(PORTNAME_ultrix4) || defined(PORTNAME_next)
...@@ -56,19 +57,45 @@ static void closePGconn(PGconn *conn); ...@@ -56,19 +57,45 @@ static void closePGconn(PGconn *conn);
/* ---------------- /* ----------------
* PQsetdb * PQsetdb
* *
* establishes a connectin to a postgres backend through the postmaster * establishes a connection to a postgres backend through the postmaster
* at the specified host and port. * at the specified host and port.
* *
* returns a PGconn* which is needed for all subsequent libpq calls * returns a PGconn* which is needed for all subsequent libpq calls
* if the status field of the connection returned is CONNECTION_BAD, * if the status field of the connection returned is CONNECTION_BAD,
* then some fields may be null'ed out instead of having valid values * then some fields may be null'ed out instead of having valid values
*
* Uses these environment variables:
*
* PGHOST identifies host to which to connect if <pghost> argument
* is NULL or a null string.
*
* PGPORT identifies TCP port to which to connect if <pgport> argument
* is NULL or a null string.
*
* PGTTY identifies tty to which to send messages if <pgtty> argument
* is NULL or a null string.
*
* PGOPTIONS identifies connection options if <pgoptions> argument is
* NULL or a null string.
*
* PGUSER Postgres username to associate with the connection.
*
* PGDATABASE name of database to which to connect if <pgdatabase>
* argument is NULL or a null string
*
* None of the above need be defined. There are defaults for all of them.
*
* ---------------- * ----------------
*/ */
PGconn* PGconn*
PQsetdb(const char *pghost, const char* pgport, const char* pgoptions, const char* pgtty, const char* dbName) PQsetdb(const char *pghost, const char* pgport, const char* pgoptions, const char* pgtty, const char* dbName)
{ {
PGconn *conn; PGconn *conn;
const char *tmp; char *tmp;
char errorMessage[ERROR_MSG_LENGTH];
/* An error message from some service we call. */
bool error;
/* We encountered an error that prevents successful completion */
conn = (PGconn*)malloc(sizeof(PGconn)); conn = (PGconn*)malloc(sizeof(PGconn));
...@@ -113,33 +140,45 @@ PQsetdb(const char *pghost, const char* pgport, const char* pgoptions, const cha ...@@ -113,33 +140,45 @@ PQsetdb(const char *pghost, const char* pgport, const char* pgoptions, const cha
conn->pgoptions = strdup(tmp); conn->pgoptions = strdup(tmp);
} else } else
conn->pgoptions = strdup(pgoptions); conn->pgoptions = strdup(pgoptions);
if (((tmp = dbName) && (dbName[0] != '\0')) ||
((tmp = getenv("PGDATABASE")))) { if (tmp = getenv("PGUSER")) {
conn->dbName = strdup(tmp); error = FALSE;
conn->pguser = strdup(tmp);
} else { } else {
char errorMessage[ERROR_MSG_LENGTH]; tmp = fe_getauthname(errorMessage);
if ((tmp = fe_getauthname(errorMessage)) != 0) { if (tmp == 0) {
conn->dbName = strdup(tmp); error = TRUE;
free((char*)tmp);
} else {
sprintf(conn->errorMessage, sprintf(conn->errorMessage,
"FATAL: PQsetdb: Unable to determine a database name!\n"); "FATAL: PQsetdb: Unable to determine a Postgres username!\n");
conn->dbName = NULL; } else {
return conn; error = FALSE;
conn->pguser = tmp;
} }
} }
conn->status = connectDB(conn);
if (conn->status == CONNECTION_OK) { if (!error) {
PGresult *res; if (((tmp = (char *)dbName) && (dbName[0] != '\0')) ||
/* Send a blank query to make sure everything works; in particular, that ((tmp = getenv("PGDATABASE")))) {
the database exists. conn->dbName = strdup(tmp);
*/ } else conn->dbName = conn->pguser;
res = PQexec(conn," "); } else conn->dbName = NULL;
if (res == NULL || res->resultStatus != PGRES_EMPTY_QUERY) {
/* PQexec has put error message in conn->errorMessage */ if (error) conn->status = CONNECTION_BAD;
closePGconn(conn); else {
conn->status = connectDB(conn);
/* Puts message in conn->errorMessage */
if (conn->status == CONNECTION_OK) {
PGresult *res;
/* Send a blank query to make sure everything works;
in particular, that the database exists.
*/
res = PQexec(conn," ");
if (res == NULL || res->resultStatus != PGRES_EMPTY_QUERY) {
/* PQexec has put error message in conn->errorMessage */
closePGconn(conn);
}
PQclear(res);
} }
PQclear(res);
} }
} }
return conn; return conn;
...@@ -165,7 +204,6 @@ connectDB(PGconn *conn) ...@@ -165,7 +204,6 @@ connectDB(PGconn *conn)
Port *port = conn->port; Port *port = conn->port;
int portno; int portno;
char *user;
/* /*
// //
// Initialize the startup packet. // Initialize the startup packet.
...@@ -175,11 +213,7 @@ connectDB(PGconn *conn) ...@@ -175,11 +213,7 @@ connectDB(PGconn *conn)
// //
// //
*/ */
user = fe_getauthname(conn->errorMessage); strncpy(startup.user,conn->pguser,sizeof(startup.user));
if (!user)
goto connect_errReturn;
strncpy(startup.user,user,sizeof(startup.user));
free(user);
strncpy(startup.database,conn->dbName,sizeof(startup.database)); strncpy(startup.database,conn->dbName,sizeof(startup.database));
strncpy(startup.tty,conn->pgtty,sizeof(startup.tty)); strncpy(startup.tty,conn->pgtty,sizeof(startup.tty));
if (conn->pgoptions) { if (conn->pgoptions) {
...@@ -292,6 +326,7 @@ freePGconn(PGconn *conn) ...@@ -292,6 +326,7 @@ freePGconn(PGconn *conn)
if (conn->pgoptions) free(conn->pgoptions); if (conn->pgoptions) free(conn->pgoptions);
if (conn->pgport) free(conn->pgport); if (conn->pgport) free(conn->pgport);
if (conn->dbName) free(conn->dbName); if (conn->dbName) free(conn->dbName);
if (conn->pguser) free(conn->pguser);
if (conn->notifyList) DLFreeList(conn->notifyList); if (conn->notifyList) DLFreeList(conn->notifyList);
free(conn); free(conn);
} }
...@@ -303,8 +338,16 @@ freePGconn(PGconn *conn) ...@@ -303,8 +338,16 @@ freePGconn(PGconn *conn)
static void static void
closePGconn(PGconn *conn) closePGconn(PGconn *conn)
{ {
const struct sigaction ignore_action = {SIG_IGN, 0, 0, NULL};
struct sigaction oldaction;
/* If connection is already gone, that's cool. No reason for kernel
to kill us when we try to write to it. So ignore SIGPIPE signals.
*/
sigaction(SIGPIPE, (struct sigaction *) &ignore_action, &oldaction);
fputs("X\0", conn->Pfout); fputs("X\0", conn->Pfout);
fflush(conn->Pfout); fflush(conn->Pfout);
sigaction(SIGPIPE, &oldaction, NULL);
if (conn->Pfout) fclose(conn->Pfout); if (conn->Pfout) fclose(conn->Pfout);
if (conn->Pfin) fclose(conn->Pfin); if (conn->Pfin) fclose(conn->Pfin);
if (conn->Pfdebug) fclose(conn->Pfdebug); if (conn->Pfdebug) fclose(conn->Pfdebug);
......
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
* *
* Copyright (c) 1994, Regents of the University of California * Copyright (c) 1994, Regents of the University of California
* *
* $Id: libpq-fe.h,v 1.7 1996/08/13 01:34:29 scrappy Exp $ * $Id: libpq-fe.h,v 1.8 1996/10/10 08:20:11 bryanh Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -112,6 +112,7 @@ typedef struct pg_conn{ ...@@ -112,6 +112,7 @@ typedef struct pg_conn{
void *port; /* really a Port* */ void *port; /* really a Port* */
int asyncNotifyWaiting; int asyncNotifyWaiting;
Dllist* notifyList; Dllist* notifyList;
char *pguser; /* Postgres username of user who is connected */
} PGconn; } PGconn;
#define CMDSTATUS_LEN 40 #define CMDSTATUS_LEN 40
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment