diff --git a/doc/src/sgml/libpq.sgml b/doc/src/sgml/libpq.sgml index d67212b831..caab9700b8 100644 --- a/doc/src/sgml/libpq.sgml +++ b/doc/src/sgml/libpq.sgml @@ -938,8 +938,8 @@ postgresql://%2Fvar%2Flib%2Fpostgresql/dbname If a password file is used, you can have different passwords for different hosts. All the other connection options are the same for every - host, it is not possible to e.g. specify a different username for - different hosts. + host in the list; it is not possible to e.g. specify different + usernames for different hosts. @@ -961,7 +961,7 @@ postgresql://%2Fvar%2Flib%2Fpostgresql/dbname name of the directory in which the socket file is stored. If multiple host names are specified, each will be tried in turn in the order given. The default behavior when host is - not specified is to connect to a Unix-domain + not specified, or is empty, is to connect to a Unix-domain socketUnix domain socket in /tmp (or whatever socket directory was specified when PostgreSQL was built). On machines without @@ -969,7 +969,8 @@ postgresql://%2Fvar%2Flib%2Fpostgresql/dbname A comma-separated list of host names is also accepted, in which case - each host name in the list is tried in order. See + each host name in the list is tried in order; an empty item in the + list selects the default behavior as explained above. See for details. @@ -1020,14 +1021,17 @@ postgresql://%2Fvar%2Flib%2Fpostgresql/dbname Note that authentication is likely to fail if host is not the name of the server at network address hostaddr. - Also, note that host rather than hostaddr + Also, when both host and hostaddr + are specified, host is used to identify the connection in a password file (see ). A comma-separated list of hostaddr values is also - accepted, in which case each host in the list is tried in order. See + accepted, in which case each host in the list is tried in order. + An empty item in the list causes the corresponding host name to be + used, or the default host name if that is empty as well. See for details. @@ -1047,9 +1051,12 @@ postgresql://%2Fvar%2Flib%2Fpostgresql/dbname name extension for Unix-domain connections.port If multiple hosts were given in the host or - hostaddr parameters, this parameter may specify a list - of ports of equal length, or it may specify a single port number to - be used for all hosts. + hostaddr parameters, this parameter may specify a + comma-separated list of ports of the same length as the host list, or + it may specify a single port number to be used for all hosts. + An empty string, or an empty item in a comma-separated list, + specifies the default port number established + when PostgreSQL was built. @@ -1683,6 +1690,17 @@ char *PQuser(const PGconn *conn); char *PQpass(const PGconn *conn); + + + PQpass will return either the password specified + in the connection parameters, or if there was none and the password + was obtained from the password + file, it will return that. In the latter case, + if multiple hosts were specified in the connection parameters, it is + not possible to rely on the result of PQpass until + the connection is established. The status of the connection can be + checked using the function PQstatus. + @@ -7521,13 +7539,18 @@ myEventProc(PGEventId evtId, void *evtInfo, void *passThrough) used. (Therefore, put more-specific entries first when you are using wildcards.) If an entry needs to contain : or \, escape this character with \. - A host name of localhost matches both TCP (host name - localhost) and Unix domain socket (pghost empty - or the default socket directory) connections coming from the local - machine. In a standby server, a database name of replication + The host name field is matched to the host connection + parameter if that is specified, otherwise to + the hostaddr parameter if that is specified; if neither + are given then the host name localhost is searched for. + The host name localhost is also searched for when + the connection is a Unix-domain socket connection and + the host parameter + matches libpq's default socket directory path. + In a standby server, a database field of replication matches streaming replication connections made to the master server. - The database field is of limited usefulness because - users have the same password for all databases in the same cluster. + The database field is of limited usefulness otherwise, because users have + the same password for all databases in the same cluster. diff --git a/src/interfaces/libpq/fe-connect.c b/src/interfaces/libpq/fe-connect.c index bd7dac120d..4b35994394 100644 --- a/src/interfaces/libpq/fe-connect.c +++ b/src/interfaces/libpq/fe-connect.c @@ -901,6 +901,8 @@ parse_comma_separated_list(char **startptr, bool *more) static bool connectOptions2(PGconn *conn) { + int i; + /* * Allocate memory for details about each host to which we might possibly * try to connect. For that, count the number of elements in the hostaddr @@ -920,11 +922,10 @@ connectOptions2(PGconn *conn) /* * We now have one pg_conn_host structure per possible host. Fill in the - * host details for each one. + * host and hostaddr fields for each, by splitting the parameter strings. */ if (conn->pghostaddr != NULL && conn->pghostaddr[0] != '\0') { - int i; char *s = conn->pghostaddr; bool more = true; @@ -933,8 +934,6 @@ connectOptions2(PGconn *conn) conn->connhost[i].hostaddr = parse_comma_separated_list(&s, &more); if (conn->connhost[i].hostaddr == NULL) goto oom_error; - - conn->connhost[i].type = CHT_HOST_ADDRESS; } /* @@ -948,7 +947,6 @@ connectOptions2(PGconn *conn) if (conn->pghost != NULL && conn->pghost[0] != '\0') { - int i; char *s = conn->pghost; bool more = true; @@ -957,17 +955,9 @@ connectOptions2(PGconn *conn) conn->connhost[i].host = parse_comma_separated_list(&s, &more); if (conn->connhost[i].host == NULL) goto oom_error; - - /* Identify the type of host. */ - if (conn->pghostaddr == NULL || conn->pghostaddr[0] == '\0') - { - conn->connhost[i].type = CHT_HOST_NAME; -#ifdef HAVE_UNIX_SOCKETS - if (is_absolute_path(conn->connhost[i].host)) - conn->connhost[i].type = CHT_UNIX_SOCKET; -#endif - } } + + /* Check for wrong number of host items. */ if (more || i != conn->nconnhost) { conn->status = CONNECTION_BAD; @@ -979,29 +969,48 @@ connectOptions2(PGconn *conn) } /* - * If neither host or hostaddr options was given, connect to default host. + * Now, for each host slot, identify the type of address spec, and fill in + * the default address if nothing was given. */ - if ((conn->pghostaddr == NULL || conn->pghostaddr[0] == '\0') && - (conn->pghost == NULL || conn->pghost[0] == '\0')) + for (i = 0; i < conn->nconnhost; i++) { - Assert(conn->nconnhost == 1); + pg_conn_host *ch = &conn->connhost[i]; + + if (ch->hostaddr != NULL && ch->hostaddr[0] != '\0') + ch->type = CHT_HOST_ADDRESS; + else if (ch->host != NULL && ch->host[0] != '\0') + { + ch->type = CHT_HOST_NAME; #ifdef HAVE_UNIX_SOCKETS - conn->connhost[0].host = strdup(DEFAULT_PGSOCKET_DIR); - conn->connhost[0].type = CHT_UNIX_SOCKET; -#else - conn->connhost[0].host = strdup(DefaultHost); - conn->connhost[0].type = CHT_HOST_NAME; + if (is_absolute_path(ch->host)) + ch->type = CHT_UNIX_SOCKET; #endif - if (conn->connhost[0].host == NULL) - goto oom_error; + } + else + { + if (ch->host) + free(ch->host); +#ifdef HAVE_UNIX_SOCKETS + ch->host = strdup(DEFAULT_PGSOCKET_DIR); + ch->type = CHT_UNIX_SOCKET; +#else + ch->host = strdup(DefaultHost); + ch->type = CHT_HOST_NAME; +#endif + if (ch->host == NULL) + goto oom_error; + } } /* * Next, work out the port number corresponding to each host name. + * + * Note: unlike the above for host names, this could leave the port fields + * as null or empty strings. We will substitute DEF_PGPORT whenever we + * read such a port field. */ if (conn->pgport != NULL && conn->pgport[0] != '\0') { - int i; char *s = conn->pgport; bool more = true; @@ -1065,8 +1074,8 @@ connectOptions2(PGconn *conn) } /* - * Supply default password if none given. Note that the password might be - * different for each host/port pair. + * If password was not given, try to look it up in password file. Note + * that the result might be different for each host/port pair. */ if (conn->pgpass == NULL || conn->pgpass[0] == '\0') { @@ -1089,20 +1098,16 @@ connectOptions2(PGconn *conn) if (conn->pgpassfile != NULL && conn->pgpassfile[0] != '\0') { - int i; - for (i = 0; i < conn->nconnhost; i++) { /* - * Try to get a password for this host from pgpassfile. We use - * host name rather than host address in the same manner as - * PQhost(). + * Try to get a password for this host from file. We use host + * for the hostname search key if given, else hostaddr (at + * least one of them is guaranteed nonempty by now). */ - char *pwhost = conn->connhost[i].host; + const char *pwhost = conn->connhost[i].host; - if (conn->connhost[i].type == CHT_HOST_ADDRESS && - conn->connhost[i].host != NULL && - conn->connhost[i].host[0] != '\0') + if (pwhost == NULL || pwhost[0] == '\0') pwhost = conn->connhost[i].hostaddr; conn->connhost[i].password = @@ -6385,14 +6390,14 @@ passwordFromFile(const char *hostname, const char *port, const char *dbname, #define LINELEN NAMEDATALEN*5 char buf[LINELEN]; - if (dbname == NULL || strlen(dbname) == 0) + if (dbname == NULL || dbname[0] == '\0') return NULL; - if (username == NULL || strlen(username) == 0) + if (username == NULL || username[0] == '\0') return NULL; /* 'localhost' matches pghost of '' or the default socket directory */ - if (hostname == NULL) + if (hostname == NULL || hostname[0] == '\0') hostname = DefaultHost; else if (is_absolute_path(hostname)) @@ -6403,7 +6408,7 @@ passwordFromFile(const char *hostname, const char *port, const char *dbname, if (strcmp(hostname, DEFAULT_PGSOCKET_DIR) == 0) hostname = DefaultHost; - if (port == NULL) + if (port == NULL || port[0] == '\0') port = DEF_PGPORT_STR; /* If password file cannot be opened, ignore it. */