PostgreSQL Source Code git master
fe-connect.c File Reference
#include "postgres_fe.h"
#include <sys/stat.h>
#include <fcntl.h>
#include <ctype.h>
#include <netdb.h>
#include <time.h>
#include <unistd.h>
#include "common/base64.h"
#include "common/ip.h"
#include "common/link-canary.h"
#include "common/scram-common.h"
#include "common/string.h"
#include "fe-auth.h"
#include "libpq-fe.h"
#include "libpq-int.h"
#include "mb/pg_wchar.h"
#include "pg_config_paths.h"
#include "port/pg_bswap.h"
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <pwd.h>
#include <pthread.h>
Include dependency graph for fe-connect.c:

Go to the source code of this file.

Data Structures

struct  _internalPQconninfoOption
 

Macros

#define PGPASSFILE   ".pgpass"
 
#define ERRCODE_APPNAME_UNKNOWN   "42704"
 
#define ERRCODE_INVALID_PASSWORD   "28P01"
 
#define ERRCODE_CANNOT_CONNECT_NOW   "57P03"
 
#define DefaultHost   "localhost"
 
#define DefaultOption   ""
 
#define DefaultChannelBinding   "disable"
 
#define DefaultTargetSessionAttrs   "any"
 
#define DefaultLoadBalanceHosts   "disable"
 
#define DefaultSSLMode   "disable"
 
#define DefaultSSLCertMode   "disable"
 
#define DefaultSSLNegotiation   "postgres"
 
#define DefaultGSSMode   "disable"
 
#define ENCRYPTION_NEGOTIATION_FAILED(msg)
 
#define CONNECTION_FAILED()
 
#define MAX_ERRLEN   30000
 
#define SELECT_NEXT_METHOD(method)
 

Typedefs

typedef struct _internalPQconninfoOption internalPQconninfoOption
 

Functions

static bool connectOptions1 (PGconn *conn, const char *conninfo)
 
static bool init_allowed_encryption_methods (PGconn *conn)
 
static bool connection_failed (PGconn *conn)
 
static bool select_next_encryption_method (PGconn *conn, bool have_valid_connection)
 
static PGPing internal_ping (PGconn *conn)
 
static void pqFreeCommandQueue (PGcmdQueueEntry *queue)
 
static bool fillPGconn (PGconn *conn, PQconninfoOption *connOptions)
 
static void freePGconn (PGconn *conn)
 
static void release_conn_addrinfo (PGconn *conn)
 
static int store_conn_addrinfo (PGconn *conn, struct addrinfo *addrlist)
 
static void sendTerminateConn (PGconn *conn)
 
static PQconninfoOptionconninfo_init (PQExpBuffer errorMessage)
 
static PQconninfoOptionparse_connection_string (const char *connstr, PQExpBuffer errorMessage, bool use_defaults)
 
static int uri_prefix_length (const char *connstr)
 
static bool recognized_connection_string (const char *connstr)
 
static PQconninfoOptionconninfo_parse (const char *conninfo, PQExpBuffer errorMessage, bool use_defaults)
 
static PQconninfoOptionconninfo_array_parse (const char *const *keywords, const char *const *values, PQExpBuffer errorMessage, bool use_defaults, int expand_dbname)
 
static bool conninfo_add_defaults (PQconninfoOption *options, PQExpBuffer errorMessage)
 
static PQconninfoOptionconninfo_uri_parse (const char *uri, PQExpBuffer errorMessage, bool use_defaults)
 
static bool conninfo_uri_parse_options (PQconninfoOption *options, const char *uri, PQExpBuffer errorMessage)
 
static bool conninfo_uri_parse_params (char *params, PQconninfoOption *connOptions, PQExpBuffer errorMessage)
 
static char * conninfo_uri_decode (const char *str, PQExpBuffer errorMessage)
 
static bool get_hexdigit (char digit, int *value)
 
static const char * conninfo_getval (PQconninfoOption *connOptions, const char *keyword)
 
static PQconninfoOptionconninfo_storeval (PQconninfoOption *connOptions, const char *keyword, const char *value, PQExpBuffer errorMessage, bool ignoreMissing, bool uri_decode)
 
static PQconninfoOptionconninfo_find (PQconninfoOption *connOptions, const char *keyword)
 
static void defaultNoticeReceiver (void *arg, const PGresult *res)
 
static void defaultNoticeProcessor (void *arg, const char *message)
 
static int parseServiceInfo (PQconninfoOption *options, PQExpBuffer errorMessage)
 
static int parseServiceFile (const char *serviceFile, const char *service, PQconninfoOption *options, PQExpBuffer errorMessage, bool *group_found)
 
static char * pwdfMatchesString (char *buf, const char *token)
 
static char * passwordFromFile (const char *hostname, const char *port, const char *dbname, const char *username, const char *pgpassfile)
 
static void pgpassfileWarning (PGconn *conn)
 
static void default_threadlock (int acquire)
 
static bool sslVerifyProtocolVersion (const char *version)
 
static bool sslVerifyProtocolRange (const char *min, const char *max)
 
void pqDropConnection (PGconn *conn, bool flushInput)
 
static void pqDropServerData (PGconn *conn)
 
PGconnPQconnectdbParams (const char *const *keywords, const char *const *values, int expand_dbname)
 
PGPing PQpingParams (const char *const *keywords, const char *const *values, int expand_dbname)
 
PGconnPQconnectdb (const char *conninfo)
 
PGPing PQping (const char *conninfo)
 
PGconnPQconnectStartParams (const char *const *keywords, const char *const *values, int expand_dbname)
 
PGconnPQconnectStart (const char *conninfo)
 
bool pqCopyPGconn (PGconn *srcConn, PGconn *dstConn)
 
static int count_comma_separated_elems (const char *input)
 
static char * parse_comma_separated_list (char **startptr, bool *more)
 
static void libpq_prng_init (PGconn *conn)
 
bool pqConnectOptions2 (PGconn *conn)
 
PQconninfoOptionPQconndefaults (void)
 
PGconnPQsetdbLogin (const char *pghost, const char *pgport, const char *pgoptions, const char *pgtty, const char *dbName, const char *login, const char *pwd)
 
static int connectNoDelay (PGconn *conn)
 
static void getHostaddr (PGconn *conn, char *host_addr, int host_addr_len)
 
static void emitHostIdentityInfo (PGconn *conn, const char *host_addr)
 
static void connectFailureMessage (PGconn *conn, int errorno)
 
static int useKeepalives (PGconn *conn)
 
static int setKeepalivesIdle (PGconn *conn)
 
static int setKeepalivesInterval (PGconn *conn)
 
static int setKeepalivesCount (PGconn *conn)
 
static int setTCPUserTimeout (PGconn *conn)
 
int pqConnectDBStart (PGconn *conn)
 
int pqConnectDBComplete (PGconn *conn)
 
PostgresPollingStatusType PQconnectPoll (PGconn *conn)
 
PGconnpqMakeEmptyPGconn (void)
 
void pqReleaseConnHosts (PGconn *conn)
 
void pqClosePGconn (PGconn *conn)
 
void PQfinish (PGconn *conn)
 
void PQreset (PGconn *conn)
 
int PQresetStart (PGconn *conn)
 
PostgresPollingStatusType PQresetPoll (PGconn *conn)
 
int pqPacketSend (PGconn *conn, char pack_type, const void *buf, size_t buf_len)
 
PQconninfoOptionPQconninfoParse (const char *conninfo, char **errmsg)
 
PQconninfoOptionPQconninfo (PGconn *conn)
 
void PQconninfoFree (PQconninfoOption *connOptions)
 
char * PQdb (const PGconn *conn)
 
char * PQservice (const PGconn *conn)
 
char * PQuser (const PGconn *conn)
 
char * PQpass (const PGconn *conn)
 
char * PQhost (const PGconn *conn)
 
char * PQhostaddr (const PGconn *conn)
 
char * PQport (const PGconn *conn)
 
char * PQtty (const PGconn *conn)
 
char * PQoptions (const PGconn *conn)
 
ConnStatusType PQstatus (const PGconn *conn)
 
PGTransactionStatusType PQtransactionStatus (const PGconn *conn)
 
const char * PQparameterStatus (const PGconn *conn, const char *paramName)
 
int PQprotocolVersion (const PGconn *conn)
 
int PQfullProtocolVersion (const PGconn *conn)
 
int PQserverVersion (const PGconn *conn)
 
char * PQerrorMessage (const PGconn *conn)
 
int PQsocket (const PGconn *conn)
 
int PQbackendPID (const PGconn *conn)
 
PGpipelineStatus PQpipelineStatus (const PGconn *conn)
 
int PQconnectionNeedsPassword (const PGconn *conn)
 
int PQconnectionUsedPassword (const PGconn *conn)
 
int PQconnectionUsedGSSAPI (const PGconn *conn)
 
int PQclientEncoding (const PGconn *conn)
 
int PQsetClientEncoding (PGconn *conn, const char *encoding)
 
PGVerbosity PQsetErrorVerbosity (PGconn *conn, PGVerbosity verbosity)
 
PGContextVisibility PQsetErrorContextVisibility (PGconn *conn, PGContextVisibility show_context)
 
PQnoticeReceiver PQsetNoticeReceiver (PGconn *conn, PQnoticeReceiver proc, void *arg)
 
PQnoticeProcessor PQsetNoticeProcessor (PGconn *conn, PQnoticeProcessor proc, void *arg)
 
bool pqGetHomeDirectory (char *buf, int bufsize)
 
bool pqParseIntParam (const char *value, int *result, PGconn *conn, const char *context)
 
pgthreadlock_t PQregisterThreadLock (pgthreadlock_t newhandler)
 

Variables

static const internalPQconninfoOption PQconninfoOptions []
 
static const PQEnvironmentOption EnvironmentOptions []
 
static const char uri_designator [] = "postgresql://"
 
static const char short_uri_designator [] = "postgres://"
 
pgthreadlock_t pg_g_threadlock = default_threadlock
 

Macro Definition Documentation

◆ CONNECTION_FAILED

#define CONNECTION_FAILED ( )
Value:
do { \
{ \
need_new_connection = true; \
goto keep_going; \
} \
else \
goto error_return; \
} while(0);
static bool connection_failed(PGconn *conn)
Definition: fe-connect.c:4440
PGconn * conn
Definition: streamutil.c:53

◆ DefaultChannelBinding

#define DefaultChannelBinding   "disable"

Definition at line 123 of file fe-connect.c.

◆ DefaultGSSMode

#define DefaultGSSMode   "disable"

Definition at line 139 of file fe-connect.c.

◆ DefaultHost

#define DefaultHost   "localhost"

Definition at line 118 of file fe-connect.c.

◆ DefaultLoadBalanceHosts

#define DefaultLoadBalanceHosts   "disable"

Definition at line 126 of file fe-connect.c.

◆ DefaultOption

#define DefaultOption   ""

Definition at line 119 of file fe-connect.c.

◆ DefaultSSLCertMode

#define DefaultSSLCertMode   "disable"

Definition at line 132 of file fe-connect.c.

◆ DefaultSSLMode

#define DefaultSSLMode   "disable"

Definition at line 131 of file fe-connect.c.

◆ DefaultSSLNegotiation

#define DefaultSSLNegotiation   "postgres"

Definition at line 134 of file fe-connect.c.

◆ DefaultTargetSessionAttrs

#define DefaultTargetSessionAttrs   "any"

Definition at line 125 of file fe-connect.c.

◆ ENCRYPTION_NEGOTIATION_FAILED

#define ENCRYPTION_NEGOTIATION_FAILED (   msg)
Value:
do { \
switch (encryption_negotiation_failed(conn)) \
{ \
case 0: \
libpq_append_conn_error(conn, (msg)); \
goto error_return; \
case 1: \
conn->status = CONNECTION_MADE; \
case 2: \
need_new_connection = true; \
goto keep_going; \
} \
} while(0);
@ CONNECTION_MADE
Definition: libpq-fe.h:90
@ PGRES_POLLING_WRITING
Definition: libpq-fe.h:112

◆ ERRCODE_APPNAME_UNKNOWN

#define ERRCODE_APPNAME_UNKNOWN   "42704"

Definition at line 88 of file fe-connect.c.

◆ ERRCODE_CANNOT_CONNECT_NOW

#define ERRCODE_CANNOT_CONNECT_NOW   "57P03"

Definition at line 93 of file fe-connect.c.

◆ ERRCODE_INVALID_PASSWORD

#define ERRCODE_INVALID_PASSWORD   "28P01"

Definition at line 91 of file fe-connect.c.

◆ MAX_ERRLEN

#define MAX_ERRLEN   30000

◆ PGPASSFILE

#define PGPASSFILE   ".pgpass"

Definition at line 77 of file fe-connect.c.

◆ SELECT_NEXT_METHOD

#define SELECT_NEXT_METHOD (   method)
Value:
do { \
if ((remaining_methods & method) != 0) \
{ \
conn->current_enc_method = method; \
return true; \
} \
} while (false)

Typedef Documentation

◆ internalPQconninfoOption

Function Documentation

◆ connectFailureMessage()

static void connectFailureMessage ( PGconn conn,
int  errorno 
)
static

Definition at line 2208 of file fe-connect.c.

2209{
2210 char sebuf[PG_STRERROR_R_BUFLEN];
2211
2213 "%s\n",
2214 SOCK_STRERROR(errorno, sebuf, sizeof(sebuf)));
2215
2216 if (conn->raddr.addr.ss_family == AF_UNIX)
2217 libpq_append_conn_error(conn, "\tIs the server running locally and accepting connections on that socket?");
2218 else
2219 libpq_append_conn_error(conn, "\tIs the server running on that host and accepting TCP/IP connections?");
2220}
void libpq_append_conn_error(PGconn *conn, const char *fmt,...)
Definition: fe-misc.c:1372
#define SOCK_STRERROR
Definition: libpq-int.h:937
#define PG_STRERROR_R_BUFLEN
Definition: port.h:256
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:265
struct sockaddr_storage addr
Definition: pqcomm.h:32
PQExpBufferData errorMessage
Definition: libpq-int.h:652
SockAddr raddr
Definition: libpq-int.h:493

References SockAddr::addr, appendPQExpBuffer(), conn, pg_conn::errorMessage, libpq_append_conn_error(), PG_STRERROR_R_BUFLEN, pg_conn::raddr, and SOCK_STRERROR.

Referenced by PQconnectPoll().

◆ connection_failed()

static bool connection_failed ( PGconn conn)
static

Definition at line 4440 of file fe-connect.c.

4441{
4444
4445 return select_next_encryption_method(conn, false);
4446}
#define Assert(condition)
Definition: c.h:815
static bool select_next_encryption_method(PGconn *conn, bool have_valid_connection)
Definition: fe-connect.c:4455
uint8 failed_enc_methods
Definition: libpq-int.h:585
uint8 current_enc_method
Definition: libpq-int.h:586

References Assert, conn, pg_conn::current_enc_method, pg_conn::failed_enc_methods, and select_next_encryption_method().

◆ connectNoDelay()

static int connectNoDelay ( PGconn conn)
static

Definition at line 2093 of file fe-connect.c.

2094{
2095#ifdef TCP_NODELAY
2096 int on = 1;
2097
2098 if (setsockopt(conn->sock, IPPROTO_TCP, TCP_NODELAY,
2099 (char *) &on,
2100 sizeof(on)) < 0)
2101 {
2102 char sebuf[PG_STRERROR_R_BUFLEN];
2103
2104 libpq_append_conn_error(conn, "could not set socket to TCP no delay mode: %s",
2105 SOCK_STRERROR(SOCK_ERRNO, sebuf, sizeof(sebuf)));
2106 return 0;
2107 }
2108#endif
2109
2110 return 1;
2111}
#define SOCK_ERRNO
Definition: libpq-int.h:936
pgsocket sock
Definition: libpq-int.h:490

References conn, libpq_append_conn_error(), PG_STRERROR_R_BUFLEN, pg_conn::sock, SOCK_ERRNO, and SOCK_STRERROR.

Referenced by PQconnectPoll().

◆ connectOptions1()

static bool connectOptions1 ( PGconn conn,
const char *  conninfo 
)
static

Definition at line 1006 of file fe-connect.c.

1007{
1008 PQconninfoOption *connOptions;
1009
1010 /*
1011 * Parse the conninfo string
1012 */
1013 connOptions = parse_connection_string(conninfo, &conn->errorMessage, true);
1014 if (connOptions == NULL)
1015 {
1017 /* errorMessage is already set */
1018 return false;
1019 }
1020
1021 /*
1022 * Move option values into conn structure
1023 */
1024 if (!fillPGconn(conn, connOptions))
1025 {
1027 PQconninfoFree(connOptions);
1028 return false;
1029 }
1030
1031 /*
1032 * Free the option info - all is in conn now
1033 */
1034 PQconninfoFree(connOptions);
1035
1036 return true;
1037}
static PQconninfoOption * parse_connection_string(const char *connstr, PQExpBuffer errorMessage, bool use_defaults)
Definition: fe-connect.c:5860
void PQconninfoFree(PQconninfoOption *connOptions)
Definition: fe-connect.c:7083
static bool fillPGconn(PGconn *conn, PQconninfoOption *connOptions)
Definition: fe-connect.c:927
@ CONNECTION_BAD
Definition: libpq-fe.h:82
ConnStatusType status
Definition: libpq-int.h:453

References conn, CONNECTION_BAD, pg_conn::errorMessage, fillPGconn(), parse_connection_string(), PQconninfoFree(), and pg_conn::status.

Referenced by PQconnectStart(), and PQsetdbLogin().

◆ conninfo_add_defaults()

static bool conninfo_add_defaults ( PQconninfoOption options,
PQExpBuffer  errorMessage 
)
static

Definition at line 6248 of file fe-connect.c.

6249{
6251 PQconninfoOption *sslmode_default = NULL,
6252 *sslrootcert = NULL;
6253 char *tmp;
6254
6255 /*
6256 * If there's a service spec, use it to obtain any not-explicitly-given
6257 * parameters. Ignore error if no error message buffer is passed because
6258 * there is no way to pass back the failure message.
6259 */
6260 if (parseServiceInfo(options, errorMessage) != 0 && errorMessage)
6261 return false;
6262
6263 /*
6264 * Get the fallback resources for parameters not specified in the conninfo
6265 * string nor the service.
6266 */
6267 for (option = options; option->keyword != NULL; option++)
6268 {
6269 if (strcmp(option->keyword, "sslrootcert") == 0)
6270 sslrootcert = option; /* save for later */
6271
6272 if (option->val != NULL)
6273 continue; /* Value was in conninfo or service */
6274
6275 /*
6276 * Try to get the environment variable fallback
6277 */
6278 if (option->envvar != NULL)
6279 {
6280 if ((tmp = getenv(option->envvar)) != NULL)
6281 {
6282 option->val = strdup(tmp);
6283 if (!option->val)
6284 {
6285 if (errorMessage)
6286 libpq_append_error(errorMessage, "out of memory");
6287 return false;
6288 }
6289 continue;
6290 }
6291 }
6292
6293 /*
6294 * Interpret the deprecated PGREQUIRESSL environment variable. Per
6295 * tradition, translate values starting with "1" to sslmode=require,
6296 * and ignore other values. Given both PGREQUIRESSL=1 and PGSSLMODE,
6297 * PGSSLMODE takes precedence; the opposite was true before v9.3.
6298 */
6299 if (strcmp(option->keyword, "sslmode") == 0)
6300 {
6301 const char *requiresslenv = getenv("PGREQUIRESSL");
6302
6303 if (requiresslenv != NULL && requiresslenv[0] == '1')
6304 {
6305 option->val = strdup("require");
6306 if (!option->val)
6307 {
6308 if (errorMessage)
6309 libpq_append_error(errorMessage, "out of memory");
6310 return false;
6311 }
6312 continue;
6313 }
6314
6315 /*
6316 * sslmode is not specified. Let it be filled in with the compiled
6317 * default for now, but if sslrootcert=system, we'll override the
6318 * default later before returning.
6319 */
6320 sslmode_default = option;
6321 }
6322
6323 /*
6324 * No environment variable specified or the variable isn't set - try
6325 * compiled-in default
6326 */
6327 if (option->compiled != NULL)
6328 {
6329 option->val = strdup(option->compiled);
6330 if (!option->val)
6331 {
6332 if (errorMessage)
6333 libpq_append_error(errorMessage, "out of memory");
6334 return false;
6335 }
6336 continue;
6337 }
6338
6339 /*
6340 * Special handling for "user" option. Note that if pg_fe_getauthname
6341 * fails, we just leave the value as NULL; there's no need for this to
6342 * be an error condition if the caller provides a user name. The only
6343 * reason we do this now at all is so that callers of PQconndefaults
6344 * will see a correct default (barring error, of course).
6345 */
6346 if (strcmp(option->keyword, "user") == 0)
6347 {
6348 option->val = pg_fe_getauthname(NULL);
6349 continue;
6350 }
6351 }
6352
6353 /*
6354 * Special handling for sslrootcert=system with no sslmode explicitly
6355 * defined. In this case we want to strengthen the default sslmode to
6356 * verify-full.
6357 */
6358 if (sslmode_default && sslrootcert)
6359 {
6360 if (sslrootcert->val && strcmp(sslrootcert->val, "system") == 0)
6361 {
6362 free(sslmode_default->val);
6363
6364 sslmode_default->val = strdup("verify-full");
6365 if (!sslmode_default->val)
6366 {
6367 if (errorMessage)
6368 libpq_append_error(errorMessage, "out of memory");
6369 return false;
6370 }
6371 }
6372 }
6373
6374 return true;
6375}
char * pg_fe_getauthname(PQExpBuffer errorMessage)
Definition: fe-auth.c:1255
static int parseServiceInfo(PQconninfoOption *options, PQExpBuffer errorMessage)
Definition: fe-connect.c:5553
void libpq_append_error(PQExpBuffer errorMessage, const char *fmt,...)
Definition: fe-misc.c:1343
#define free(a)
Definition: header.h:65
int val
Definition: getopt_long.h:22

References free, libpq_append_error(), parseServiceInfo(), pg_fe_getauthname(), option::val, and _PQconninfoOption::val.

Referenced by conninfo_array_parse(), conninfo_parse(), conninfo_uri_parse(), and PQconndefaults().

◆ conninfo_array_parse()

static PQconninfoOption * conninfo_array_parse ( const char *const *  keywords,
const char *const *  values,
PQExpBuffer  errorMessage,
bool  use_defaults,
int  expand_dbname 
)
static

Definition at line 6090 of file fe-connect.c.

6093{
6095 PQconninfoOption *dbname_options = NULL;
6097 int i = 0;
6098
6099 /*
6100 * If expand_dbname is non-zero, check keyword "dbname" to see if val is
6101 * actually a recognized connection string.
6102 */
6103 while (expand_dbname && keywords[i])
6104 {
6105 const char *pname = keywords[i];
6106 const char *pvalue = values[i];
6107
6108 /* first find "dbname" if any */
6109 if (strcmp(pname, "dbname") == 0 && pvalue)
6110 {
6111 /*
6112 * If value is a connection string, parse it, but do not use
6113 * defaults here -- those get picked up later. We only want to
6114 * override for those parameters actually passed.
6115 */
6116 if (recognized_connection_string(pvalue))
6117 {
6118 dbname_options = parse_connection_string(pvalue, errorMessage, false);
6119 if (dbname_options == NULL)
6120 return NULL;
6121 }
6122 break;
6123 }
6124 ++i;
6125 }
6126
6127 /* Make a working copy of PQconninfoOptions */
6128 options = conninfo_init(errorMessage);
6129 if (options == NULL)
6130 {
6131 PQconninfoFree(dbname_options);
6132 return NULL;
6133 }
6134
6135 /* Parse the keywords/values arrays */
6136 i = 0;
6137 while (keywords[i])
6138 {
6139 const char *pname = keywords[i];
6140 const char *pvalue = values[i];
6141
6142 if (pvalue != NULL && pvalue[0] != '\0')
6143 {
6144 /* Search for the param record */
6145 for (option = options; option->keyword != NULL; option++)
6146 {
6147 if (strcmp(option->keyword, pname) == 0)
6148 break;
6149 }
6150
6151 /* Check for invalid connection option */
6152 if (option->keyword == NULL)
6153 {
6154 libpq_append_error(errorMessage, "invalid connection option \"%s\"", pname);
6156 PQconninfoFree(dbname_options);
6157 return NULL;
6158 }
6159
6160 /*
6161 * If we are on the first dbname parameter, and we have a parsed
6162 * connection string, copy those parameters across, overriding any
6163 * existing previous settings.
6164 */
6165 if (strcmp(pname, "dbname") == 0 && dbname_options)
6166 {
6167 PQconninfoOption *str_option;
6168
6169 for (str_option = dbname_options; str_option->keyword != NULL; str_option++)
6170 {
6171 if (str_option->val != NULL)
6172 {
6173 int k;
6174
6175 for (k = 0; options[k].keyword; k++)
6176 {
6177 if (strcmp(options[k].keyword, str_option->keyword) == 0)
6178 {
6179 free(options[k].val);
6180 options[k].val = strdup(str_option->val);
6181 if (!options[k].val)
6182 {
6183 libpq_append_error(errorMessage, "out of memory");
6185 PQconninfoFree(dbname_options);
6186 return NULL;
6187 }
6188 break;
6189 }
6190 }
6191 }
6192 }
6193
6194 /*
6195 * Forget the parsed connection string, so that any subsequent
6196 * dbname parameters will not be expanded.
6197 */
6198 PQconninfoFree(dbname_options);
6199 dbname_options = NULL;
6200 }
6201 else
6202 {
6203 /*
6204 * Store the value, overriding previous settings
6205 */
6206 free(option->val);
6207 option->val = strdup(pvalue);
6208 if (!option->val)
6209 {
6210 libpq_append_error(errorMessage, "out of memory");
6212 PQconninfoFree(dbname_options);
6213 return NULL;
6214 }
6215 }
6216 }
6217 ++i;
6218 }
6219 PQconninfoFree(dbname_options);
6220
6221 /*
6222 * Add in defaults if the caller wants that.
6223 */
6224 if (use_defaults)
6225 {
6226 if (!conninfo_add_defaults(options, errorMessage))
6227 {
6229 return NULL;
6230 }
6231 }
6232
6233 return options;
6234}
static Datum values[MAXATTR]
Definition: bootstrap.c:151
static PQconninfoOption * conninfo_init(PQExpBuffer errorMessage)
Definition: fe-connect.c:5821
static bool conninfo_add_defaults(PQconninfoOption *options, PQExpBuffer errorMessage)
Definition: fe-connect.c:6248
static bool recognized_connection_string(const char *connstr)
Definition: fe-connect.c:5903
long val
Definition: informix.c:689
int i
Definition: isn.c:72
static const JsonPathKeyword keywords[]
static char ** options

References conninfo_add_defaults(), conninfo_init(), free, i, _PQconninfoOption::keyword, keywords, libpq_append_error(), options, parse_connection_string(), PQconninfoFree(), recognized_connection_string(), option::val, val, _PQconninfoOption::val, and values.

Referenced by PQconnectStartParams().

◆ conninfo_find()

static PQconninfoOption * conninfo_find ( PQconninfoOption connOptions,
const char *  keyword 
)
static

Definition at line 7021 of file fe-connect.c.

7022{
7024
7025 for (option = connOptions; option->keyword != NULL; option++)
7026 {
7027 if (strcmp(option->keyword, keyword) == 0)
7028 return option;
7029 }
7030
7031 return NULL;
7032}

Referenced by conninfo_getval(), and conninfo_storeval().

◆ conninfo_getval()

static const char * conninfo_getval ( PQconninfoOption connOptions,
const char *  keyword 
)
static

Definition at line 6931 of file fe-connect.c.

6933{
6935
6936 option = conninfo_find(connOptions, keyword);
6937
6938 return option ? option->val : NULL;
6939}
static PQconninfoOption * conninfo_find(PQconninfoOption *connOptions, const char *keyword)
Definition: fe-connect.c:7021

References conninfo_find(), and option::val.

Referenced by fillPGconn(), and parseServiceInfo().

◆ conninfo_init()

static PQconninfoOption * conninfo_init ( PQExpBuffer  errorMessage)
static

Definition at line 5821 of file fe-connect.c.

5822{
5824 PQconninfoOption *opt_dest;
5825 const internalPQconninfoOption *cur_opt;
5826
5827 /*
5828 * Get enough memory for all options in PQconninfoOptions, even if some
5829 * end up being filtered out.
5830 */
5832 if (options == NULL)
5833 {
5834 libpq_append_error(errorMessage, "out of memory");
5835 return NULL;
5836 }
5837 opt_dest = options;
5838
5839 for (cur_opt = PQconninfoOptions; cur_opt->keyword; cur_opt++)
5840 {
5841 /* Only copy the public part of the struct, not the full internal */
5842 memcpy(opt_dest, cur_opt, sizeof(PQconninfoOption));
5843 opt_dest++;
5844 }
5845 MemSet(opt_dest, 0, sizeof(PQconninfoOption));
5846
5847 return options;
5848}
#define MemSet(start, val, len)
Definition: c.h:977
static const internalPQconninfoOption PQconninfoOptions[]
Definition: fe-connect.c:192
#define malloc(a)
Definition: header.h:50

References _internalPQconninfoOption::keyword, libpq_append_error(), malloc, MemSet, options, and PQconninfoOptions.

Referenced by conninfo_array_parse(), conninfo_parse(), conninfo_uri_parse(), PQconndefaults(), and PQconninfo().

◆ conninfo_parse()

static PQconninfoOption * conninfo_parse ( const char *  conninfo,
PQExpBuffer  errorMessage,
bool  use_defaults 
)
static

Definition at line 5914 of file fe-connect.c.

5916{
5917 char *pname;
5918 char *pval;
5919 char *buf;
5920 char *cp;
5921 char *cp2;
5923
5924 /* Make a working copy of PQconninfoOptions */
5925 options = conninfo_init(errorMessage);
5926 if (options == NULL)
5927 return NULL;
5928
5929 /* Need a modifiable copy of the input string */
5930 if ((buf = strdup(conninfo)) == NULL)
5931 {
5932 libpq_append_error(errorMessage, "out of memory");
5934 return NULL;
5935 }
5936 cp = buf;
5937
5938 while (*cp)
5939 {
5940 /* Skip blanks before the parameter name */
5941 if (isspace((unsigned char) *cp))
5942 {
5943 cp++;
5944 continue;
5945 }
5946
5947 /* Get the parameter name */
5948 pname = cp;
5949 while (*cp)
5950 {
5951 if (*cp == '=')
5952 break;
5953 if (isspace((unsigned char) *cp))
5954 {
5955 *cp++ = '\0';
5956 while (*cp)
5957 {
5958 if (!isspace((unsigned char) *cp))
5959 break;
5960 cp++;
5961 }
5962 break;
5963 }
5964 cp++;
5965 }
5966
5967 /* Check that there is a following '=' */
5968 if (*cp != '=')
5969 {
5970 libpq_append_error(errorMessage,
5971 "missing \"=\" after \"%s\" in connection info string",
5972 pname);
5974 free(buf);
5975 return NULL;
5976 }
5977 *cp++ = '\0';
5978
5979 /* Skip blanks after the '=' */
5980 while (*cp)
5981 {
5982 if (!isspace((unsigned char) *cp))
5983 break;
5984 cp++;
5985 }
5986
5987 /* Get the parameter value */
5988 pval = cp;
5989
5990 if (*cp != '\'')
5991 {
5992 cp2 = pval;
5993 while (*cp)
5994 {
5995 if (isspace((unsigned char) *cp))
5996 {
5997 *cp++ = '\0';
5998 break;
5999 }
6000 if (*cp == '\\')
6001 {
6002 cp++;
6003 if (*cp != '\0')
6004 *cp2++ = *cp++;
6005 }
6006 else
6007 *cp2++ = *cp++;
6008 }
6009 *cp2 = '\0';
6010 }
6011 else
6012 {
6013 cp2 = pval;
6014 cp++;
6015 for (;;)
6016 {
6017 if (*cp == '\0')
6018 {
6019 libpq_append_error(errorMessage, "unterminated quoted string in connection info string");
6021 free(buf);
6022 return NULL;
6023 }
6024 if (*cp == '\\')
6025 {
6026 cp++;
6027 if (*cp != '\0')
6028 *cp2++ = *cp++;
6029 continue;
6030 }
6031 if (*cp == '\'')
6032 {
6033 *cp2 = '\0';
6034 cp++;
6035 break;
6036 }
6037 *cp2++ = *cp++;
6038 }
6039 }
6040
6041 /*
6042 * Now that we have the name and the value, store the record.
6043 */
6044 if (!conninfo_storeval(options, pname, pval, errorMessage, false, false))
6045 {
6047 free(buf);
6048 return NULL;
6049 }
6050 }
6051
6052 /* Done with the modifiable input string */
6053 free(buf);
6054
6055 /*
6056 * Add in defaults if the caller wants that.
6057 */
6058 if (use_defaults)
6059 {
6060 if (!conninfo_add_defaults(options, errorMessage))
6061 {
6063 return NULL;
6064 }
6065 }
6066
6067 return options;
6068}
static PQconninfoOption * conninfo_storeval(PQconninfoOption *connOptions, const char *keyword, const char *value, PQExpBuffer errorMessage, bool ignoreMissing, bool uri_decode)
Definition: fe-connect.c:6957
static char * buf
Definition: pg_test_fsync.c:72

References buf, conninfo_add_defaults(), conninfo_init(), conninfo_storeval(), free, libpq_append_error(), options, and PQconninfoFree().

Referenced by parse_connection_string().

◆ conninfo_storeval()

static PQconninfoOption * conninfo_storeval ( PQconninfoOption connOptions,
const char *  keyword,
const char *  value,
PQExpBuffer  errorMessage,
bool  ignoreMissing,
bool  uri_decode 
)
static

Definition at line 6957 of file fe-connect.c.

6961{
6963 char *value_copy;
6964
6965 /*
6966 * For backwards compatibility, requiressl=1 gets translated to
6967 * sslmode=require, and requiressl=0 gets translated to sslmode=prefer
6968 * (which is the default for sslmode).
6969 */
6970 if (strcmp(keyword, "requiressl") == 0)
6971 {
6972 keyword = "sslmode";
6973 if (value[0] == '1')
6974 value = "require";
6975 else
6976 value = "prefer";
6977 }
6978
6979 option = conninfo_find(connOptions, keyword);
6980 if (option == NULL)
6981 {
6982 if (!ignoreMissing)
6983 libpq_append_error(errorMessage,
6984 "invalid connection option \"%s\"",
6985 keyword);
6986 return NULL;
6987 }
6988
6989 if (uri_decode)
6990 {
6991 value_copy = conninfo_uri_decode(value, errorMessage);
6992 if (value_copy == NULL)
6993 /* conninfo_uri_decode already set an error message */
6994 return NULL;
6995 }
6996 else
6997 {
6998 value_copy = strdup(value);
6999 if (value_copy == NULL)
7000 {
7001 libpq_append_error(errorMessage, "out of memory");
7002 return NULL;
7003 }
7004 }
7005
7006 free(option->val);
7007 option->val = value_copy;
7008
7009 return option;
7010}
static char * conninfo_uri_decode(const char *str, PQExpBuffer errorMessage)
Definition: fe-connect.c:6810
static struct @162 value

References conninfo_find(), conninfo_uri_decode(), free, libpq_append_error(), option::val, and value.

Referenced by conninfo_parse(), conninfo_uri_parse_options(), conninfo_uri_parse_params(), and PQconninfo().

◆ conninfo_uri_decode()

static char * conninfo_uri_decode ( const char *  str,
PQExpBuffer  errorMessage 
)
static

Definition at line 6810 of file fe-connect.c.

6811{
6812 char *buf; /* result */
6813 char *p; /* output location */
6814 const char *q = str; /* input location */
6815
6816 buf = malloc(strlen(str) + 1);
6817 if (buf == NULL)
6818 {
6819 libpq_append_error(errorMessage, "out of memory");
6820 return NULL;
6821 }
6822 p = buf;
6823
6824 /* skip leading whitespaces */
6825 for (const char *s = q; *s == ' '; s++)
6826 {
6827 q++;
6828 continue;
6829 }
6830
6831 for (;;)
6832 {
6833 if (*q != '%')
6834 {
6835 /* if found a whitespace or NUL, the string ends */
6836 if (*q == ' ' || *q == '\0')
6837 goto end;
6838
6839 /* copy character */
6840 *(p++) = *(q++);
6841 }
6842 else
6843 {
6844 int hi;
6845 int lo;
6846 int c;
6847
6848 ++q; /* skip the percent sign itself */
6849
6850 /*
6851 * Possible EOL will be caught by the first call to
6852 * get_hexdigit(), so we never dereference an invalid q pointer.
6853 */
6854 if (!(get_hexdigit(*q++, &hi) && get_hexdigit(*q++, &lo)))
6855 {
6856 libpq_append_error(errorMessage,
6857 "invalid percent-encoded token: \"%s\"",
6858 str);
6859 free(buf);
6860 return NULL;
6861 }
6862
6863 c = (hi << 4) | lo;
6864 if (c == 0)
6865 {
6866 libpq_append_error(errorMessage,
6867 "forbidden value %%00 in percent-encoded value: \"%s\"",
6868 str);
6869 free(buf);
6870 return NULL;
6871 }
6872 *(p++) = c;
6873 }
6874 }
6875
6876end:
6877
6878 /* skip trailing whitespaces */
6879 for (const char *s = q; *s == ' '; s++)
6880 {
6881 q++;
6882 continue;
6883 }
6884
6885 /* Not at the end of the string yet? Fail. */
6886 if (*q != '\0')
6887 {
6888 libpq_append_error(errorMessage,
6889 "unexpected spaces found in \"%s\", use percent-encoded spaces (%%20) instead",
6890 str);
6891 free(buf);
6892 return NULL;
6893 }
6894
6895 /* Copy NUL terminator */
6896 *p = '\0';
6897
6898 return buf;
6899}
static bool get_hexdigit(char digit, int *value)
Definition: fe-connect.c:6910
const char * str
char * c

References buf, free, get_hexdigit(), libpq_append_error(), malloc, and str.

Referenced by conninfo_storeval(), and conninfo_uri_parse_params().

◆ conninfo_uri_parse()

static PQconninfoOption * conninfo_uri_parse ( const char *  uri,
PQExpBuffer  errorMessage,
bool  use_defaults 
)
static

Definition at line 6383 of file fe-connect.c.

6385{
6387
6388 /* Make a working copy of PQconninfoOptions */
6389 options = conninfo_init(errorMessage);
6390 if (options == NULL)
6391 return NULL;
6392
6393 if (!conninfo_uri_parse_options(options, uri, errorMessage))
6394 {
6396 return NULL;
6397 }
6398
6399 /*
6400 * Add in defaults if the caller wants that.
6401 */
6402 if (use_defaults)
6403 {
6404 if (!conninfo_add_defaults(options, errorMessage))
6405 {
6407 return NULL;
6408 }
6409 }
6410
6411 return options;
6412}
static bool conninfo_uri_parse_options(PQconninfoOption *options, const char *uri, PQExpBuffer errorMessage)
Definition: fe-connect.c:6436

References conninfo_add_defaults(), conninfo_init(), conninfo_uri_parse_options(), options, and PQconninfoFree().

Referenced by parse_connection_string().

◆ conninfo_uri_parse_options()

static bool conninfo_uri_parse_options ( PQconninfoOption options,
const char *  uri,
PQExpBuffer  errorMessage 
)
static

Definition at line 6436 of file fe-connect.c.

6438{
6439 int prefix_len;
6440 char *p;
6441 char *buf = NULL;
6442 char *start;
6443 char prevchar = '\0';
6444 char *user = NULL;
6445 char *host = NULL;
6446 bool retval = false;
6447 PQExpBufferData hostbuf;
6448 PQExpBufferData portbuf;
6449
6450 initPQExpBuffer(&hostbuf);
6451 initPQExpBuffer(&portbuf);
6452 if (PQExpBufferDataBroken(hostbuf) || PQExpBufferDataBroken(portbuf))
6453 {
6454 libpq_append_error(errorMessage, "out of memory");
6455 goto cleanup;
6456 }
6457
6458 /* need a modifiable copy of the input URI */
6459 buf = strdup(uri);
6460 if (buf == NULL)
6461 {
6462 libpq_append_error(errorMessage, "out of memory");
6463 goto cleanup;
6464 }
6465 start = buf;
6466
6467 /* Skip the URI prefix */
6468 prefix_len = uri_prefix_length(uri);
6469 if (prefix_len == 0)
6470 {
6471 /* Should never happen */
6472 libpq_append_error(errorMessage,
6473 "invalid URI propagated to internal parser routine: \"%s\"",
6474 uri);
6475 goto cleanup;
6476 }
6477 start += prefix_len;
6478 p = start;
6479
6480 /* Look ahead for possible user credentials designator */
6481 while (*p && *p != '@' && *p != '/')
6482 ++p;
6483 if (*p == '@')
6484 {
6485 /*
6486 * Found username/password designator, so URI should be of the form
6487 * "scheme://user[:password]@[netloc]".
6488 */
6489 user = start;
6490
6491 p = user;
6492 while (*p != ':' && *p != '@')
6493 ++p;
6494
6495 /* Save last char and cut off at end of user name */
6496 prevchar = *p;
6497 *p = '\0';
6498
6499 if (*user &&
6500 !conninfo_storeval(options, "user", user,
6501 errorMessage, false, true))
6502 goto cleanup;
6503
6504 if (prevchar == ':')
6505 {
6506 const char *password = p + 1;
6507
6508 while (*p != '@')
6509 ++p;
6510 *p = '\0';
6511
6512 if (*password &&
6513 !conninfo_storeval(options, "password", password,
6514 errorMessage, false, true))
6515 goto cleanup;
6516 }
6517
6518 /* Advance past end of parsed user name or password token */
6519 ++p;
6520 }
6521 else
6522 {
6523 /*
6524 * No username/password designator found. Reset to start of URI.
6525 */
6526 p = start;
6527 }
6528
6529 /*
6530 * There may be multiple netloc[:port] pairs, each separated from the next
6531 * by a comma. When we initially enter this loop, "p" has been
6532 * incremented past optional URI credential information at this point and
6533 * now points at the "netloc" part of the URI. On subsequent loop
6534 * iterations, "p" has been incremented past the comma separator and now
6535 * points at the start of the next "netloc".
6536 */
6537 for (;;)
6538 {
6539 /*
6540 * Look for IPv6 address.
6541 */
6542 if (*p == '[')
6543 {
6544 host = ++p;
6545 while (*p && *p != ']')
6546 ++p;
6547 if (!*p)
6548 {
6549 libpq_append_error(errorMessage,
6550 "end of string reached when looking for matching \"]\" in IPv6 host address in URI: \"%s\"",
6551 uri);
6552 goto cleanup;
6553 }
6554 if (p == host)
6555 {
6556 libpq_append_error(errorMessage,
6557 "IPv6 host address may not be empty in URI: \"%s\"",
6558 uri);
6559 goto cleanup;
6560 }
6561
6562 /* Cut off the bracket and advance */
6563 *(p++) = '\0';
6564
6565 /*
6566 * The address may be followed by a port specifier or a slash or a
6567 * query or a separator comma.
6568 */
6569 if (*p && *p != ':' && *p != '/' && *p != '?' && *p != ',')
6570 {
6571 libpq_append_error(errorMessage,
6572 "unexpected character \"%c\" at position %d in URI (expected \":\" or \"/\"): \"%s\"",
6573 *p, (int) (p - buf + 1), uri);
6574 goto cleanup;
6575 }
6576 }
6577 else
6578 {
6579 /* not an IPv6 address: DNS-named or IPv4 netloc */
6580 host = p;
6581
6582 /*
6583 * Look for port specifier (colon) or end of host specifier
6584 * (slash) or query (question mark) or host separator (comma).
6585 */
6586 while (*p && *p != ':' && *p != '/' && *p != '?' && *p != ',')
6587 ++p;
6588 }
6589
6590 /* Save the hostname terminator before we null it */
6591 prevchar = *p;
6592 *p = '\0';
6593
6594 appendPQExpBufferStr(&hostbuf, host);
6595
6596 if (prevchar == ':')
6597 {
6598 const char *port = ++p; /* advance past host terminator */
6599
6600 while (*p && *p != '/' && *p != '?' && *p != ',')
6601 ++p;
6602
6603 prevchar = *p;
6604 *p = '\0';
6605
6606 appendPQExpBufferStr(&portbuf, port);
6607 }
6608
6609 if (prevchar != ',')
6610 break;
6611 ++p; /* advance past comma separator */
6612 appendPQExpBufferChar(&hostbuf, ',');
6613 appendPQExpBufferChar(&portbuf, ',');
6614 }
6615
6616 /* Save final values for host and port. */
6617 if (PQExpBufferDataBroken(hostbuf) || PQExpBufferDataBroken(portbuf))
6618 goto cleanup;
6619 if (hostbuf.data[0] &&
6620 !conninfo_storeval(options, "host", hostbuf.data,
6621 errorMessage, false, true))
6622 goto cleanup;
6623 if (portbuf.data[0] &&
6624 !conninfo_storeval(options, "port", portbuf.data,
6625 errorMessage, false, true))
6626 goto cleanup;
6627
6628 if (prevchar && prevchar != '?')
6629 {
6630 const char *dbname = ++p; /* advance past host terminator */
6631
6632 /* Look for query parameters */
6633 while (*p && *p != '?')
6634 ++p;
6635
6636 prevchar = *p;
6637 *p = '\0';
6638
6639 /*
6640 * Avoid setting dbname to an empty string, as it forces the default
6641 * value (username) and ignores $PGDATABASE, as opposed to not setting
6642 * it at all.
6643 */
6644 if (*dbname &&
6645 !conninfo_storeval(options, "dbname", dbname,
6646 errorMessage, false, true))
6647 goto cleanup;
6648 }
6649
6650 if (prevchar)
6651 {
6652 ++p; /* advance past terminator */
6653
6654 if (!conninfo_uri_parse_params(p, options, errorMessage))
6655 goto cleanup;
6656 }
6657
6658 /* everything parsed okay */
6659 retval = true;
6660
6661cleanup:
6662 termPQExpBuffer(&hostbuf);
6663 termPQExpBuffer(&portbuf);
6664 free(buf);
6665 return retval;
6666}
static void cleanup(void)
Definition: bootstrap.c:713
static int uri_prefix_length(const char *connstr)
Definition: fe-connect.c:5880
static bool conninfo_uri_parse_params(char *params, PQconninfoOption *connOptions, PQExpBuffer errorMessage)
Definition: fe-connect.c:6677
return str start
static char * user
Definition: pg_regress.c:119
static int port
Definition: pg_regress.c:115
void initPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:90
void appendPQExpBufferChar(PQExpBuffer str, char ch)
Definition: pqexpbuffer.c:378
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:367
void termPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:129
#define PQExpBufferDataBroken(buf)
Definition: pqexpbuffer.h:67
static char * password
Definition: streamutil.c:52
char * dbname
Definition: streamutil.c:50

References appendPQExpBufferChar(), appendPQExpBufferStr(), buf, cleanup(), conninfo_storeval(), conninfo_uri_parse_params(), PQExpBufferData::data, dbname, free, initPQExpBuffer(), libpq_append_error(), password, port, PQExpBufferDataBroken, start, termPQExpBuffer(), uri_prefix_length(), and user.

Referenced by conninfo_uri_parse().

◆ conninfo_uri_parse_params()

static bool conninfo_uri_parse_params ( char *  params,
PQconninfoOption connOptions,
PQExpBuffer  errorMessage 
)
static

Definition at line 6677 of file fe-connect.c.

6680{
6681 while (*params)
6682 {
6683 char *keyword = params;
6684 char *value = NULL;
6685 char *p = params;
6686 bool malloced = false;
6687 int oldmsglen;
6688
6689 /*
6690 * Scan the params string for '=' and '&', marking the end of keyword
6691 * and value respectively.
6692 */
6693 for (;;)
6694 {
6695 if (*p == '=')
6696 {
6697 /* Was there '=' already? */
6698 if (value != NULL)
6699 {
6700 libpq_append_error(errorMessage,
6701 "extra key/value separator \"=\" in URI query parameter: \"%s\"",
6702 keyword);
6703 return false;
6704 }
6705 /* Cut off keyword, advance to value */
6706 *p++ = '\0';
6707 value = p;
6708 }
6709 else if (*p == '&' || *p == '\0')
6710 {
6711 /*
6712 * If not at the end, cut off value and advance; leave p
6713 * pointing to start of the next parameter, if any.
6714 */
6715 if (*p != '\0')
6716 *p++ = '\0';
6717 /* Was there '=' at all? */
6718 if (value == NULL)
6719 {
6720 libpq_append_error(errorMessage,
6721 "missing key/value separator \"=\" in URI query parameter: \"%s\"",
6722 keyword);
6723 return false;
6724 }
6725 /* Got keyword and value, go process them. */
6726 break;
6727 }
6728 else
6729 ++p; /* Advance over all other bytes. */
6730 }
6731
6732 keyword = conninfo_uri_decode(keyword, errorMessage);
6733 if (keyword == NULL)
6734 {
6735 /* conninfo_uri_decode already set an error message */
6736 return false;
6737 }
6738 value = conninfo_uri_decode(value, errorMessage);
6739 if (value == NULL)
6740 {
6741 /* conninfo_uri_decode already set an error message */
6742 free(keyword);
6743 return false;
6744 }
6745 malloced = true;
6746
6747 /*
6748 * Special keyword handling for improved JDBC compatibility.
6749 */
6750 if (strcmp(keyword, "ssl") == 0 &&
6751 strcmp(value, "true") == 0)
6752 {
6753 free(keyword);
6754 free(value);
6755 malloced = false;
6756
6757 keyword = "sslmode";
6758 value = "require";
6759 }
6760
6761 /*
6762 * Store the value if the corresponding option exists; ignore
6763 * otherwise. At this point both keyword and value are not
6764 * URI-encoded.
6765 */
6766 oldmsglen = errorMessage->len;
6767 if (!conninfo_storeval(connOptions, keyword, value,
6768 errorMessage, true, false))
6769 {
6770 /* Insert generic message if conninfo_storeval didn't give one. */
6771 if (errorMessage->len == oldmsglen)
6772 libpq_append_error(errorMessage,
6773 "invalid URI query parameter: \"%s\"",
6774 keyword);
6775 /* And fail. */
6776 if (malloced)
6777 {
6778 free(keyword);
6779 free(value);
6780 }
6781 return false;
6782 }
6783
6784 if (malloced)
6785 {
6786 free(keyword);
6787 free(value);
6788 }
6789
6790 /* Proceed to next key=value pair, if any */
6791 params = p;
6792 }
6793
6794 return true;
6795}

References conninfo_storeval(), conninfo_uri_decode(), free, PQExpBufferData::len, libpq_append_error(), and value.

Referenced by conninfo_uri_parse_options().

◆ count_comma_separated_elems()

static int count_comma_separated_elems ( const char *  input)
static

Definition at line 1043 of file fe-connect.c.

1044{
1045 int n;
1046
1047 n = 1;
1048 for (; *input != '\0'; input++)
1049 {
1050 if (*input == ',')
1051 n++;
1052 }
1053
1054 return n;
1055}
FILE * input

References input.

Referenced by pqConnectOptions2().

◆ default_threadlock()

static void default_threadlock ( int  acquire)
static

Definition at line 7875 of file fe-connect.c.

7876{
7877 static pthread_mutex_t singlethread_lock = PTHREAD_MUTEX_INITIALIZER;
7878
7879 if (acquire)
7880 {
7881 if (pthread_mutex_lock(&singlethread_lock))
7882 Assert(false);
7883 }
7884 else
7885 {
7886 if (pthread_mutex_unlock(&singlethread_lock))
7887 Assert(false);
7888 }
7889}
int pthread_mutex_unlock(pthread_mutex_t *mp)
Definition: pthread-win32.c:60
int pthread_mutex_lock(pthread_mutex_t *mp)
Definition: pthread-win32.c:42
#define PTHREAD_MUTEX_INITIALIZER
Definition: pthread-win32.h:16

References Assert, PTHREAD_MUTEX_INITIALIZER, pthread_mutex_lock(), and pthread_mutex_unlock().

Referenced by PQregisterThreadLock().

◆ defaultNoticeProcessor()

static void defaultNoticeProcessor ( void *  arg,
const char *  message 
)
static

Definition at line 7485 of file fe-connect.c.

7486{
7487 (void) arg; /* not used */
7488 /* Note: we expect the supplied string to end with a newline already. */
7489 fprintf(stderr, "%s", message);
7490}
#define fprintf(file, fmt, msg)
Definition: cubescan.l:21
void * arg

References arg, and fprintf.

Referenced by pqMakeEmptyPGconn().

◆ defaultNoticeReceiver()

static void defaultNoticeReceiver ( void *  arg,
const PGresult res 
)
static

Definition at line 7470 of file fe-connect.c.

7471{
7472 (void) arg; /* not used */
7473 if (res->noticeHooks.noticeProc != NULL)
7476}
char * PQresultErrorMessage(const PGresult *res)
Definition: fe-exec.c:3427
void * noticeProcArg
Definition: libpq-int.h:160
PQnoticeProcessor noticeProc
Definition: libpq-int.h:159
PGNoticeHooks noticeHooks
Definition: libpq-int.h:191

References arg, pg_result::noticeHooks, PGNoticeHooks::noticeProc, PGNoticeHooks::noticeProcArg, PQresultErrorMessage(), and res.

Referenced by pqMakeEmptyPGconn().

◆ emitHostIdentityInfo()

static void emitHostIdentityInfo ( PGconn conn,
const char *  host_addr 
)
static

Definition at line 2152 of file fe-connect.c.

2153{
2154 if (conn->raddr.addr.ss_family == AF_UNIX)
2155 {
2156 char service[NI_MAXHOST];
2157
2159 NULL, 0,
2160 service, sizeof(service),
2161 NI_NUMERICSERV);
2163 libpq_gettext("connection to server on socket \"%s\" failed: "),
2164 service);
2165 }
2166 else
2167 {
2168 const char *displayed_host;
2169 const char *displayed_port;
2170
2171 /* To which host and port were we actually connecting? */
2173 displayed_host = conn->connhost[conn->whichhost].hostaddr;
2174 else
2175 displayed_host = conn->connhost[conn->whichhost].host;
2176 displayed_port = conn->connhost[conn->whichhost].port;
2177 if (displayed_port == NULL || displayed_port[0] == '\0')
2178 displayed_port = DEF_PGPORT_STR;
2179
2180 /*
2181 * If the user did not supply an IP address using 'hostaddr', and
2182 * 'host' was missing or does not match our lookup, display the
2183 * looked-up IP address.
2184 */
2186 host_addr[0] &&
2187 strcmp(displayed_host, host_addr) != 0)
2189 libpq_gettext("connection to server at \"%s\" (%s), port %s failed: "),
2190 displayed_host, host_addr,
2191 displayed_port);
2192 else
2194 libpq_gettext("connection to server at \"%s\", port %s failed: "),
2195 displayed_host,
2196 displayed_port);
2197 }
2198}
int pg_getnameinfo_all(const struct sockaddr_storage *addr, int salen, char *node, int nodelen, char *service, int servicelen, int flags)
Definition: ip.c:114
#define libpq_gettext(x)
Definition: libpq-int.h:915
@ CHT_HOST_ADDRESS
Definition: libpq-int.h:318
socklen_t salen
Definition: pqcomm.h:33
char * host
Definition: libpq-int.h:366
char * port
Definition: libpq-int.h:368
char * hostaddr
Definition: libpq-int.h:367
pg_conn_host_type type
Definition: libpq-int.h:365
int whichhost
Definition: libpq-int.h:472
pg_conn_host * connhost
Definition: libpq-int.h:473

References SockAddr::addr, appendPQExpBuffer(), CHT_HOST_ADDRESS, conn, pg_conn::connhost, pg_conn::errorMessage, pg_conn_host::host, pg_conn_host::hostaddr, libpq_gettext, pg_getnameinfo_all(), pg_conn_host::port, pg_conn::raddr, SockAddr::salen, pg_conn_host::type, and pg_conn::whichhost.

Referenced by PQconnectPoll().

◆ fillPGconn()

static bool fillPGconn ( PGconn conn,
PQconninfoOption connOptions 
)
static

Definition at line 927 of file fe-connect.c.

928{
930
931 for (option = PQconninfoOptions; option->keyword; option++)
932 {
933 if (option->connofs >= 0)
934 {
935 const char *tmp = conninfo_getval(connOptions, option->keyword);
936
937 if (tmp)
938 {
939 char **connmember = (char **) ((char *) conn + option->connofs);
940
941 free(*connmember);
942 *connmember = strdup(tmp);
943 if (*connmember == NULL)
944 {
945 libpq_append_conn_error(conn, "out of memory");
946 return false;
947 }
948 }
949 }
950 }
951
952 return true;
953}
static const char * conninfo_getval(PQconninfoOption *connOptions, const char *keyword)
Definition: fe-connect.c:6931

References conn, conninfo_getval(), free, libpq_append_conn_error(), and PQconninfoOptions.

Referenced by connectOptions1(), and PQconnectStartParams().

◆ freePGconn()

static void freePGconn ( PGconn conn)
static

Definition at line 4693 of file fe-connect.c.

4694{
4695 /* let any event procs clean up their state data */
4696 for (int i = 0; i < conn->nEvents; i++)
4697 {
4699
4700 evt.conn = conn;
4701 (void) conn->events[i].proc(PGEVT_CONNDESTROY, &evt,
4703 free(conn->events[i].name);
4704 }
4705
4708
4710 free(conn->events);
4711 free(conn->pghost);
4713 free(conn->pgport);
4717 free(conn->appname);
4719 free(conn->dbName);
4721 free(conn->pguser);
4722 if (conn->pgpass)
4723 {
4724 explicit_bzero(conn->pgpass, strlen(conn->pgpass));
4725 free(conn->pgpass);
4726 }
4733 free(conn->sslmode);
4735 free(conn->sslcert);
4736 free(conn->sslkey);
4737 if (conn->sslpassword)
4738 {
4741 }
4744 free(conn->sslcrl);
4747 free(conn->sslsni);
4754 free(conn->gsslib);
4756 free(conn->connip);
4757 /* Note that conn->Pfdebug is not ours to close or free */
4759 free(conn->inBuffer);
4761 free(conn->rowBuf);
4768
4769 free(conn);
4770}
static void release_conn_addrinfo(PGconn *conn)
Definition: fe-connect.c:4845
void pqReleaseConnHosts(PGconn *conn)
Definition: fe-connect.c:4777
@ PGEVT_CONNDESTROY
Definition: libpq-events.h:31
void explicit_bzero(void *buf, size_t len)
void * passThrough
Definition: libpq-int.h:167
char * name
Definition: libpq-int.h:166
PGEventProc proc
Definition: libpq-int.h:165
char * replication
Definition: libpq-int.h:398
char * write_err_msg
Definition: libpq-int.h:502
char * sslrootcert
Definition: libpq-int.h:418
PGdataValue * rowBuf
Definition: libpq-int.h:562
char * sslnegotiation
Definition: libpq-int.h:412
char * sslcompression
Definition: libpq-int.h:413
char * require_auth
Definition: libpq-int.h:431
char * inBuffer
Definition: libpq-int.h:545
char * channel_binding
Definition: libpq-int.h:403
char * sslcrldir
Definition: libpq-int.h:420
char * gssdelegation
Definition: libpq-int.h:427
char * pgoptions
Definition: libpq-int.h:394
char * sslcrl
Definition: libpq-int.h:419
char * pghost
Definition: libpq-int.h:381
char * ssl_max_protocol_version
Definition: libpq-int.h:429
char * pgpass
Definition: libpq-int.h:401
char * dbName
Definition: libpq-int.h:397
char * fbappname
Definition: libpq-int.h:396
char * sslcert
Definition: libpq-int.h:415
char * sslcertmode
Definition: libpq-int.h:417
char * target_session_attrs
Definition: libpq-int.h:430
char * sslpassword
Definition: libpq-int.h:416
PQExpBufferData workBuffer
Definition: libpq-int.h:656
char * keepalives_idle
Definition: libpq-int.h:406
char * connip
Definition: libpq-int.h:474
char * load_balance_hosts
Definition: libpq-int.h:432
char * pguser
Definition: libpq-int.h:400
char * keepalives
Definition: libpq-int.h:405
char * client_encoding_initial
Definition: libpq-int.h:393
char * keepalives_interval
Definition: libpq-int.h:407
char * appname
Definition: libpq-int.h:395
char * sslmode
Definition: libpq-int.h:411
char * pgtcp_user_timeout
Definition: libpq-int.h:392
char * ssl_min_protocol_version
Definition: libpq-int.h:428
char * gssencmode
Definition: libpq-int.h:423
char * scram_server_key
Definition: libpq-int.h:434
int nEvents
Definition: libpq-int.h:449
char * pghostaddr
Definition: libpq-int.h:385
char * sslkey
Definition: libpq-int.h:414
char * scram_client_key
Definition: libpq-int.h:433
char * pgpassfile
Definition: libpq-int.h:402
char * connect_timeout
Definition: libpq-int.h:391
char * krbsrvname
Definition: libpq-int.h:424
char * gsslib
Definition: libpq-int.h:425
char * pgport
Definition: libpq-int.h:389
char * keepalives_count
Definition: libpq-int.h:409
char * requirepeer
Definition: libpq-int.h:422
char * sslsni
Definition: libpq-int.h:421
PGEvent * events
Definition: libpq-int.h:448
char * outBuffer
Definition: libpq-int.h:552

References pg_conn::appname, pg_conn::channel_binding, pg_conn::client_encoding_initial, conn, PGEventConnDestroy::conn, pg_conn::connect_timeout, pg_conn::connip, pg_conn::dbName, pg_conn::errorMessage, pg_conn::events, explicit_bzero(), pg_conn::fbappname, free, pg_conn::gssdelegation, pg_conn::gssencmode, pg_conn::gsslib, i, pg_conn::inBuffer, pg_conn::keepalives, pg_conn::keepalives_count, pg_conn::keepalives_idle, pg_conn::keepalives_interval, pg_conn::krbsrvname, pg_conn::load_balance_hosts, PGEvent::name, pg_conn::nEvents, pg_conn::outBuffer, PGEvent::passThrough, PGEVT_CONNDESTROY, pg_conn::pghost, pg_conn::pghostaddr, pg_conn::pgoptions, pg_conn::pgpass, pg_conn::pgpassfile, pg_conn::pgport, pg_conn::pgtcp_user_timeout, pg_conn::pguser, pqReleaseConnHosts(), PGEvent::proc, release_conn_addrinfo(), pg_conn::replication, pg_conn::require_auth, pg_conn::requirepeer, pg_conn::rowBuf, pg_conn::scram_client_key, pg_conn::scram_server_key, pg_conn::ssl_max_protocol_version, pg_conn::ssl_min_protocol_version, pg_conn::sslcert, pg_conn::sslcertmode, pg_conn::sslcompression, pg_conn::sslcrl, pg_conn::sslcrldir, pg_conn::sslkey, pg_conn::sslmode, pg_conn::sslnegotiation, pg_conn::sslpassword, pg_conn::sslrootcert, pg_conn::sslsni, pg_conn::target_session_attrs, termPQExpBuffer(), pg_conn::workBuffer, and pg_conn::write_err_msg.

Referenced by PQfinish(), and pqMakeEmptyPGconn().

◆ get_hexdigit()

static bool get_hexdigit ( char  digit,
int *  value 
)
static

Definition at line 6910 of file fe-connect.c.

6911{
6912 if ('0' <= digit && digit <= '9')
6913 *value = digit - '0';
6914 else if ('A' <= digit && digit <= 'F')
6915 *value = digit - 'A' + 10;
6916 else if ('a' <= digit && digit <= 'f')
6917 *value = digit - 'a' + 10;
6918 else
6919 return false;
6920
6921 return true;
6922}

References value.

Referenced by conninfo_uri_decode().

◆ getHostaddr()

static void getHostaddr ( PGconn conn,
char *  host_addr,
int  host_addr_len 
)
static

Definition at line 2119 of file fe-connect.c.

2120{
2121 struct sockaddr_storage *addr = &conn->raddr.addr;
2122
2123 if (addr->ss_family == AF_INET)
2124 {
2125 if (pg_inet_net_ntop(AF_INET,
2126 &((struct sockaddr_in *) addr)->sin_addr.s_addr,
2127 32,
2128 host_addr, host_addr_len) == NULL)
2129 host_addr[0] = '\0';
2130 }
2131 else if (addr->ss_family == AF_INET6)
2132 {
2133 if (pg_inet_net_ntop(AF_INET6,
2134 &((struct sockaddr_in6 *) addr)->sin6_addr.s6_addr,
2135 128,
2136 host_addr, host_addr_len) == NULL)
2137 host_addr[0] = '\0';
2138 }
2139 else
2140 host_addr[0] = '\0';
2141}
char * pg_inet_net_ntop(int af, const void *src, int bits, char *dst, size_t size)
Definition: inet_net_ntop.c:77

References SockAddr::addr, conn, pg_inet_net_ntop(), and pg_conn::raddr.

Referenced by PQconnectPoll().

◆ init_allowed_encryption_methods()

static bool init_allowed_encryption_methods ( PGconn conn)
static

Definition at line 4350 of file fe-connect.c.

4351{
4352 if (conn->raddr.addr.ss_family == AF_UNIX)
4353 {
4354 /* Don't request SSL or GSSAPI over Unix sockets */
4356
4357 /*
4358 * XXX: we probably should not do this. sslmode=require works
4359 * differently
4360 */
4361 if (conn->gssencmode[0] == 'r')
4362 {
4364 "GSSAPI encryption required but it is not supported over a local socket");
4367 return false;
4368 }
4369
4372 return true;
4373 }
4374
4375 /* initialize based on sslmode and gssencmode */
4377
4378#ifdef USE_SSL
4379 /* sslmode anything but 'disable', and GSSAPI not required */
4380 if (conn->sslmode[0] != 'd' && conn->gssencmode[0] != 'r')
4381 {
4383 }
4384#endif
4385
4386#ifdef ENABLE_GSS
4387 if (conn->gssencmode[0] != 'd')
4389#endif
4390
4391 if ((conn->sslmode[0] == 'd' || conn->sslmode[0] == 'p' || conn->sslmode[0] == 'a') &&
4392 (conn->gssencmode[0] == 'd' || conn->gssencmode[0] == 'p'))
4393 {
4395 }
4396
4397 return select_next_encryption_method(conn, false);
4398}
#define ENC_PLAINTEXT
Definition: libpq-int.h:238
#define ENC_GSSAPI
Definition: libpq-int.h:239
#define ENC_SSL
Definition: libpq-int.h:240
#define ENC_ERROR
Definition: libpq-int.h:237
uint8 allowed_enc_methods
Definition: libpq-int.h:584

References SockAddr::addr, pg_conn::allowed_enc_methods, conn, pg_conn::current_enc_method, ENC_ERROR, ENC_GSSAPI, ENC_PLAINTEXT, ENC_SSL, pg_conn::gssencmode, libpq_append_conn_error(), pg_conn::raddr, select_next_encryption_method(), and pg_conn::sslmode.

Referenced by PQconnectPoll().

◆ internal_ping()

static PGPing internal_ping ( PGconn conn)
static

Definition at line 4530 of file fe-connect.c.

4531{
4532 /* Say "no attempt" if we never got to PQconnectPoll */
4533 if (!conn || !conn->options_valid)
4534 return PQPING_NO_ATTEMPT;
4535
4536 /* Attempt to complete the connection */
4537 if (conn->status != CONNECTION_BAD)
4538 (void) pqConnectDBComplete(conn);
4539
4540 /* Definitely OK if we succeeded */
4541 if (conn->status != CONNECTION_BAD)
4542 return PQPING_OK;
4543
4544 /*
4545 * Here begins the interesting part of "ping": determine the cause of the
4546 * failure in sufficient detail to decide what to return. We do not want
4547 * to report that the server is not up just because we didn't have a valid
4548 * password, for example. In fact, any sort of authentication request
4549 * implies the server is up. (We need this check since the libpq side of
4550 * things might have pulled the plug on the connection before getting an
4551 * error as such from the postmaster.)
4552 */
4554 return PQPING_OK;
4555
4556 /*
4557 * If we failed to get any ERROR response from the postmaster, report
4558 * PQPING_NO_RESPONSE. This result could be somewhat misleading for a
4559 * pre-7.4 server, since it won't send back a SQLSTATE, but those are long
4560 * out of support. Another corner case where the server could return a
4561 * failure without a SQLSTATE is fork failure, but PQPING_NO_RESPONSE
4562 * isn't totally unreasonable for that anyway. We expect that every other
4563 * failure case in a modern server will produce a report with a SQLSTATE.
4564 *
4565 * NOTE: whenever we get around to making libpq generate SQLSTATEs for
4566 * client-side errors, we should either not store those into
4567 * last_sqlstate, or add an extra flag so we can tell client-side errors
4568 * apart from server-side ones.
4569 */
4570 if (strlen(conn->last_sqlstate) != 5)
4571 return PQPING_NO_RESPONSE;
4572
4573 /*
4574 * Report PQPING_REJECT if server says it's not accepting connections.
4575 */
4577 return PQPING_REJECT;
4578
4579 /*
4580 * Any other SQLSTATE can be taken to indicate that the server is up.
4581 * Presumably it didn't like our username, password, or database name; or
4582 * perhaps it had some transient failure, but that should not be taken as
4583 * meaning "it's down".
4584 */
4585 return PQPING_OK;
4586}
#define ERRCODE_CANNOT_CONNECT_NOW
Definition: fe-connect.c:93
int pqConnectDBComplete(PGconn *conn)
Definition: fe-connect.c:2529
@ PQPING_OK
Definition: libpq-fe.h:171
@ PQPING_REJECT
Definition: libpq-fe.h:172
@ PQPING_NO_RESPONSE
Definition: libpq-fe.h:173
@ PQPING_NO_ATTEMPT
Definition: libpq-fe.h:174
bool auth_req_received
Definition: libpq-int.h:496
char last_sqlstate[6]
Definition: libpq-int.h:456
bool options_valid
Definition: libpq-int.h:457

References pg_conn::auth_req_received, conn, CONNECTION_BAD, ERRCODE_CANNOT_CONNECT_NOW, pg_conn::last_sqlstate, pg_conn::options_valid, pqConnectDBComplete(), PQPING_NO_ATTEMPT, PQPING_NO_RESPONSE, PQPING_OK, PQPING_REJECT, and pg_conn::status.

Referenced by PQping(), and PQpingParams().

◆ libpq_prng_init()

static void libpq_prng_init ( PGconn conn)
static

Definition at line 1102 of file fe-connect.c.

1103{
1104 uint64 rseed;
1105 struct timeval tval = {0};
1106
1108 return;
1109
1110 gettimeofday(&tval, NULL);
1111
1112 rseed = ((uintptr_t) conn) ^
1113 ((uint64) getpid()) ^
1114 ((uint64) tval.tv_usec) ^
1115 ((uint64) tval.tv_sec);
1116
1117 pg_prng_seed(&conn->prng_state, rseed);
1118}
uint64_t uint64
Definition: c.h:489
void pg_prng_seed(pg_prng_state *state, uint64 seed)
Definition: pg_prng.c:89
#define pg_prng_strong_seed(state)
Definition: pg_prng.h:46
pg_prng_state prng_state
Definition: libpq-int.h:541
int gettimeofday(struct timeval *tp, void *tzp)

References conn, gettimeofday(), pg_prng_seed(), pg_prng_strong_seed, and pg_conn::prng_state.

Referenced by pqConnectOptions2().

◆ parse_comma_separated_list()

static char * parse_comma_separated_list ( char **  startptr,
bool *  more 
)
static

Definition at line 1067 of file fe-connect.c.

1068{
1069 char *p;
1070 char *s = *startptr;
1071 char *e;
1072 int len;
1073
1074 /*
1075 * Search for the end of the current element; a comma or end-of-string
1076 * acts as a terminator.
1077 */
1078 e = s;
1079 while (*e != '\0' && *e != ',')
1080 ++e;
1081 *more = (*e == ',');
1082
1083 len = e - s;
1084 p = (char *) malloc(sizeof(char) * (len + 1));
1085 if (p)
1086 {
1087 memcpy(p, s, len);
1088 p[len] = '\0';
1089 }
1090 *startptr = e + 1;
1091
1092 return p;
1093}
const void size_t len
e
Definition: preproc-init.c:82

References len, and malloc.

Referenced by pqConnectOptions2().

◆ parse_connection_string()

static PQconninfoOption * parse_connection_string ( const char *  connstr,
PQExpBuffer  errorMessage,
bool  use_defaults 
)
static

Definition at line 5860 of file fe-connect.c.

5862{
5863 /* Parse as URI if connection string matches URI prefix */
5864 if (uri_prefix_length(connstr) != 0)
5865 return conninfo_uri_parse(connstr, errorMessage, use_defaults);
5866
5867 /* Parse as default otherwise */
5868 return conninfo_parse(connstr, errorMessage, use_defaults);
5869}
static PQconninfoOption * conninfo_uri_parse(const char *uri, PQExpBuffer errorMessage, bool use_defaults)
Definition: fe-connect.c:6383
static PQconninfoOption * conninfo_parse(const char *conninfo, PQExpBuffer errorMessage, bool use_defaults)
Definition: fe-connect.c:5914
static char * connstr
Definition: pg_dumpall.c:88

References conninfo_parse(), conninfo_uri_parse(), connstr, and uri_prefix_length().

Referenced by connectOptions1(), conninfo_array_parse(), and PQconninfoParse().

◆ parseServiceFile()

static int parseServiceFile ( const char *  serviceFile,
const char *  service,
PQconninfoOption options,
PQExpBuffer  errorMessage,
bool *  group_found 
)
static

Definition at line 5621 of file fe-connect.c.

5626{
5627 int result = 0,
5628 linenr = 0,
5629 i;
5630 FILE *f;
5631 char *line;
5632 char buf[1024];
5633
5634 *group_found = false;
5635
5636 f = fopen(serviceFile, "r");
5637 if (f == NULL)
5638 {
5639 libpq_append_error(errorMessage, "service file \"%s\" not found", serviceFile);
5640 return 1;
5641 }
5642
5643 while ((line = fgets(buf, sizeof(buf), f)) != NULL)
5644 {
5645 int len;
5646
5647 linenr++;
5648
5649 if (strlen(line) >= sizeof(buf) - 1)
5650 {
5651 libpq_append_error(errorMessage,
5652 "line %d too long in service file \"%s\"",
5653 linenr,
5654 serviceFile);
5655 result = 2;
5656 goto exit;
5657 }
5658
5659 /* ignore whitespace at end of line, especially the newline */
5660 len = strlen(line);
5661 while (len > 0 && isspace((unsigned char) line[len - 1]))
5662 line[--len] = '\0';
5663
5664 /* ignore leading whitespace too */
5665 while (*line && isspace((unsigned char) line[0]))
5666 line++;
5667
5668 /* ignore comments and empty lines */
5669 if (line[0] == '\0' || line[0] == '#')
5670 continue;
5671
5672 /* Check for right groupname */
5673 if (line[0] == '[')
5674 {
5675 if (*group_found)
5676 {
5677 /* end of desired group reached; return success */
5678 goto exit;
5679 }
5680
5681 if (strncmp(line + 1, service, strlen(service)) == 0 &&
5682 line[strlen(service) + 1] == ']')
5683 *group_found = true;
5684 else
5685 *group_found = false;
5686 }
5687 else
5688 {
5689 if (*group_found)
5690 {
5691 /*
5692 * Finally, we are in the right group and can parse the line
5693 */
5694 char *key,
5695 *val;
5696 bool found_keyword;
5697
5698#ifdef USE_LDAP
5699 if (strncmp(line, "ldap", 4) == 0)
5700 {
5701 int rc = ldapServiceLookup(line, options, errorMessage);
5702
5703 /* if rc = 2, go on reading for fallback */
5704 switch (rc)
5705 {
5706 case 0:
5707 goto exit;
5708 case 1:
5709 case 3:
5710 result = 3;
5711 goto exit;
5712 case 2:
5713 continue;
5714 }
5715 }
5716#endif
5717
5718 key = line;
5719 val = strchr(line, '=');
5720 if (val == NULL)
5721 {
5722 libpq_append_error(errorMessage,
5723 "syntax error in service file \"%s\", line %d",
5724 serviceFile,
5725 linenr);
5726 result = 3;
5727 goto exit;
5728 }
5729 *val++ = '\0';
5730
5731 if (strcmp(key, "service") == 0)
5732 {
5733 libpq_append_error(errorMessage,
5734 "nested service specifications not supported in service file \"%s\", line %d",
5735 serviceFile,
5736 linenr);
5737 result = 3;
5738 goto exit;
5739 }
5740
5741 /*
5742 * Set the parameter --- but don't override any previous
5743 * explicit setting.
5744 */
5745 found_keyword = false;
5746 for (i = 0; options[i].keyword; i++)
5747 {
5748 if (strcmp(options[i].keyword, key) == 0)
5749 {
5750 if (options[i].val == NULL)
5751 options[i].val = strdup(val);
5752 if (!options[i].val)
5753 {
5754 libpq_append_error(errorMessage, "out of memory");
5755 result = 3;
5756 goto exit;
5757 }
5758 found_keyword = true;
5759 break;
5760 }
5761 }
5762
5763 if (!found_keyword)
5764 {
5765 libpq_append_error(errorMessage,
5766 "syntax error in service file \"%s\", line %d",
5767 serviceFile,
5768 linenr);
5769 result = 3;
5770 goto exit;
5771 }
5772 }
5773 }
5774 }
5775
5776exit:
5777 fclose(f);
5778
5779 return result;
5780}
exit(1)

References buf, exit(), i, sort-test::key, len, libpq_append_error(), and val.

Referenced by parseServiceInfo().

◆ parseServiceInfo()

static int parseServiceInfo ( PQconninfoOption options,
PQExpBuffer  errorMessage 
)
static

Definition at line 5553 of file fe-connect.c.

5554{
5555 const char *service = conninfo_getval(options, "service");
5556 char serviceFile[MAXPGPATH];
5557 char *env;
5558 bool group_found = false;
5559 int status;
5560 struct stat stat_buf;
5561
5562 /*
5563 * We have to special-case the environment variable PGSERVICE here, since
5564 * this is and should be called before inserting environment defaults for
5565 * other connection options.
5566 */
5567 if (service == NULL)
5568 service = getenv("PGSERVICE");
5569
5570 /* If no service name given, nothing to do */
5571 if (service == NULL)
5572 return 0;
5573
5574 /*
5575 * Try PGSERVICEFILE if specified, else try ~/.pg_service.conf (if that
5576 * exists).
5577 */
5578 if ((env = getenv("PGSERVICEFILE")) != NULL)
5579 strlcpy(serviceFile, env, sizeof(serviceFile));
5580 else
5581 {
5582 char homedir[MAXPGPATH];
5583
5584 if (!pqGetHomeDirectory(homedir, sizeof(homedir)))
5585 goto next_file;
5586 snprintf(serviceFile, MAXPGPATH, "%s/%s", homedir, ".pg_service.conf");
5587 if (stat(serviceFile, &stat_buf) != 0)
5588 goto next_file;
5589 }
5590
5591 status = parseServiceFile(serviceFile, service, options, errorMessage, &group_found);
5592 if (group_found || status != 0)
5593 return status;
5594
5595next_file:
5596
5597 /*
5598 * This could be used by any application so we can't use the binary
5599 * location to find our config files.
5600 */
5601 snprintf(serviceFile, MAXPGPATH, "%s/pg_service.conf",
5602 getenv("PGSYSCONFDIR") ? getenv("PGSYSCONFDIR") : SYSCONFDIR);
5603 if (stat(serviceFile, &stat_buf) != 0)
5604 goto last_file;
5605
5606 status = parseServiceFile(serviceFile, service, options, errorMessage, &group_found);
5607 if (status != 0)
5608 return status;
5609
5610last_file:
5611 if (!group_found)
5612 {
5613 libpq_append_error(errorMessage, "definition of service \"%s\" not found", service);
5614 return 3;
5615 }
5616
5617 return 0;
5618}
static int parseServiceFile(const char *serviceFile, const char *service, PQconninfoOption *options, PQExpBuffer errorMessage, bool *group_found)
Definition: fe-connect.c:5621
bool pqGetHomeDirectory(char *buf, int bufsize)
Definition: fe-connect.c:7783
#define MAXPGPATH
#define snprintf
Definition: port.h:238
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45
#define stat
Definition: win32_port.h:274

References conninfo_getval(), libpq_append_error(), MAXPGPATH, parseServiceFile(), pqGetHomeDirectory(), snprintf, stat, and strlcpy().

Referenced by conninfo_add_defaults().

◆ passwordFromFile()

static char * passwordFromFile ( const char *  hostname,
const char *  port,
const char *  dbname,
const char *  username,
const char *  pgpassfile 
)
static

Definition at line 7534 of file fe-connect.c.

7536{
7537 FILE *fp;
7538#ifndef WIN32
7539 struct stat stat_buf;
7540#endif
7542
7543 if (dbname == NULL || dbname[0] == '\0')
7544 return NULL;
7545
7546 if (username == NULL || username[0] == '\0')
7547 return NULL;
7548
7549 /* 'localhost' matches pghost of '' or the default socket directory */
7550 if (hostname == NULL || hostname[0] == '\0')
7552 else if (is_unixsock_path(hostname))
7553
7554 /*
7555 * We should probably use canonicalize_path(), but then we have to
7556 * bring path.c into libpq, and it doesn't seem worth it.
7557 */
7558 if (strcmp(hostname, DEFAULT_PGSOCKET_DIR) == 0)
7560
7561 if (port == NULL || port[0] == '\0')
7562 port = DEF_PGPORT_STR;
7563
7564 /* If password file cannot be opened, ignore it. */
7565 fp = fopen(pgpassfile, "r");
7566 if (fp == NULL)
7567 return NULL;
7568
7569#ifndef WIN32
7570 if (fstat(fileno(fp), &stat_buf) != 0)
7571 {
7572 fclose(fp);
7573 return NULL;
7574 }
7575
7576 if (!S_ISREG(stat_buf.st_mode))
7577 {
7578 fprintf(stderr,
7579 libpq_gettext("WARNING: password file \"%s\" is not a plain file\n"),
7580 pgpassfile);
7581 fclose(fp);
7582 return NULL;
7583 }
7584
7585 /* If password file is insecure, alert the user and ignore it. */
7586 if (stat_buf.st_mode & (S_IRWXG | S_IRWXO))
7587 {
7588 fprintf(stderr,
7589 libpq_gettext("WARNING: password file \"%s\" has group or world access; permissions should be u=rw (0600) or less\n"),
7590 pgpassfile);
7591 fclose(fp);
7592 return NULL;
7593 }
7594#else
7595
7596 /*
7597 * On Win32, the directory is protected, so we don't have to check the
7598 * file.
7599 */
7600#endif
7601
7602 /* Use an expansible buffer to accommodate any reasonable line length */
7604
7605 while (!feof(fp) && !ferror(fp))
7606 {
7607 /* Make sure there's a reasonable amount of room in the buffer */
7608 if (!enlargePQExpBuffer(&buf, 128))
7609 break;
7610
7611 /* Read some data, appending it to what we already have */
7612 if (fgets(buf.data + buf.len, buf.maxlen - buf.len, fp) == NULL)
7613 break;
7614 buf.len += strlen(buf.data + buf.len);
7615
7616 /* If we don't yet have a whole line, loop around to read more */
7617 if (!(buf.len > 0 && buf.data[buf.len - 1] == '\n') && !feof(fp))
7618 continue;
7619
7620 /* ignore comments */
7621 if (buf.data[0] != '#')
7622 {
7623 char *t = buf.data;
7624 int len;
7625
7626 /* strip trailing newline and carriage return */
7627 len = pg_strip_crlf(t);
7628
7629 if (len > 0 &&
7630 (t = pwdfMatchesString(t, hostname)) != NULL &&
7631 (t = pwdfMatchesString(t, port)) != NULL &&
7632 (t = pwdfMatchesString(t, dbname)) != NULL &&
7633 (t = pwdfMatchesString(t, username)) != NULL)
7634 {
7635 /* Found a match. */
7636 char *ret,
7637 *p1,
7638 *p2;
7639
7640 ret = strdup(t);
7641
7642 fclose(fp);
7643 explicit_bzero(buf.data, buf.maxlen);
7645
7646 if (!ret)
7647 {
7648 /* Out of memory. XXX: an error message would be nice. */
7649 return NULL;
7650 }
7651
7652 /* De-escape password. */
7653 for (p1 = p2 = ret; *p1 != ':' && *p1 != '\0'; ++p1, ++p2)
7654 {
7655 if (*p1 == '\\' && p1[1] != '\0')
7656 ++p1;
7657 *p2 = *p1;
7658 }
7659 *p2 = '\0';
7660
7661 return ret;
7662 }
7663 }
7664
7665 /* No match, reset buffer to prepare for next line. */
7666 buf.len = 0;
7667 }
7668
7669 fclose(fp);
7670 explicit_bzero(buf.data, buf.maxlen);
7672 return NULL;
7673}
#define DefaultHost
Definition: fe-connect.c:118
static char * pwdfMatchesString(char *buf, const char *token)
Definition: fe-connect.c:7497
static char * username
Definition: initdb.c:153
#define DEFAULT_PGSOCKET_DIR
static char * hostname
Definition: pg_regress.c:114
static bool is_unixsock_path(const char *path)
Definition: pqcomm.h:67
int enlargePQExpBuffer(PQExpBuffer str, size_t needed)
Definition: pqexpbuffer.c:172
int pg_strip_crlf(char *str)
Definition: string.c:154
#define S_IRWXG
Definition: win32_port.h:300
#define S_IRWXO
Definition: win32_port.h:312
#define fstat
Definition: win32_port.h:273
#define S_ISREG(m)
Definition: win32_port.h:318

References buf, dbname, DEFAULT_PGSOCKET_DIR, DefaultHost, enlargePQExpBuffer(), explicit_bzero(), fprintf, fstat, hostname, initPQExpBuffer(), is_unixsock_path(), len, libpq_gettext, p2, pg_strip_crlf(), port, pwdfMatchesString(), S_IRWXG, S_IRWXO, S_ISREG, stat::st_mode, termPQExpBuffer(), and username.

Referenced by pqConnectOptions2().

◆ pgpassfileWarning()

static void pgpassfileWarning ( PGconn conn)
static

Definition at line 7681 of file fe-connect.c.

7682{
7683 /* If it was 'invalid authorization', add pgpassfile mention */
7684 /* only works with >= 9.0 servers */
7685 if (conn->password_needed &&
7686 conn->connhost[conn->whichhost].password != NULL &&
7687 conn->result)
7688 {
7689 const char *sqlstate = PQresultErrorField(conn->result,
7691
7692 if (sqlstate && strcmp(sqlstate, ERRCODE_INVALID_PASSWORD) == 0)
7693 libpq_append_conn_error(conn, "password retrieved from file \"%s\"",
7694 conn->pgpassfile);
7695 }
7696}
#define ERRCODE_INVALID_PASSWORD
Definition: fe-connect.c:91
char * PQresultErrorField(const PGresult *res, int fieldcode)
Definition: fe-exec.c:3466
#define PG_DIAG_SQLSTATE
Definition: postgres_ext.h:57
char * password
Definition: libpq-int.h:369
PGresult * result
Definition: libpq-int.h:575
bool password_needed
Definition: libpq-int.h:497

References conn, pg_conn::connhost, ERRCODE_INVALID_PASSWORD, libpq_append_conn_error(), pg_conn_host::password, pg_conn::password_needed, PG_DIAG_SQLSTATE, pg_conn::pgpassfile, PQresultErrorField(), pg_conn::result, and pg_conn::whichhost.

Referenced by PQconnectPoll().

◆ PQbackendPID()

int PQbackendPID ( const PGconn conn)

Definition at line 7302 of file fe-connect.c.

7303{
7304 if (!conn || conn->status != CONNECTION_OK)
7305 return 0;
7306 return conn->be_pid;
7307}
@ CONNECTION_OK
Definition: libpq-fe.h:81
int be_pid
Definition: libpq-int.h:531

References pg_conn::be_pid, conn, CONNECTION_OK, and pg_conn::status.

Referenced by get_prompt(), libpqrcv_get_backend_pid(), main(), send_cancellable_query_impl(), and StartLogStreamer().

◆ PQclientEncoding()

◆ pqClosePGconn()

void pqClosePGconn ( PGconn conn)

Definition at line 4893 of file fe-connect.c.

4894{
4895 /*
4896 * If possible, send Terminate message to close the connection politely.
4897 */
4899
4900 /*
4901 * Must reset the blocking status so a possible reconnect will work.
4902 *
4903 * Don't call PQsetnonblocking() because it will fail if it's unable to
4904 * flush the connection.
4905 */
4906 conn->nonblocking = false;
4907
4908 /*
4909 * Close the connection, reset all transient state, flush I/O buffers.
4910 * Note that this includes clearing conn's error state; we're no longer
4911 * interested in any failures associated with the old connection, and we
4912 * want a clean slate for any new connection attempt.
4913 */
4914 pqDropConnection(conn, true);
4915 conn->status = CONNECTION_BAD; /* Well, not really _bad_ - just absent */
4919 pqClearAsyncResult(conn); /* deallocate result */
4921
4922 /*
4923 * Release addrinfo, but since cancel requests never change their addrinfo
4924 * we don't do that. Otherwise we would have to rebuild it during a
4925 * PQcancelReset.
4926 */
4927 if (!conn->cancelRequest)
4929
4930 /* Reset all state obtained from server, too */
4932}
void pqDropConnection(PGconn *conn, bool flushInput)
Definition: fe-connect.c:480
static void sendTerminateConn(PGconn *conn)
Definition: fe-connect.c:4859
static void pqDropServerData(PGconn *conn)
Definition: fe-connect.c:593
void pqClearAsyncResult(PGconn *conn)
Definition: fe-exec.c:779
@ PQTRANS_IDLE
Definition: libpq-fe.h:142
@ PQ_PIPELINE_OFF
Definition: libpq-fe.h:182
@ PGASYNC_IDLE
Definition: libpq-int.h:223
#define pqClearConnErrorState(conn)
Definition: libpq-int.h:888
PGTransactionStatusType xactStatus
Definition: libpq-int.h:455
bool cancelRequest
Definition: libpq-int.h:436
bool nonblocking
Definition: libpq-int.h:458
PGAsyncStatusType asyncStatus
Definition: libpq-int.h:454
PGpipelineStatus pipelineStatus
Definition: libpq-int.h:460

References pg_conn::asyncStatus, pg_conn::cancelRequest, conn, CONNECTION_BAD, pg_conn::nonblocking, PGASYNC_IDLE, pg_conn::pipelineStatus, PQ_PIPELINE_OFF, pqClearAsyncResult(), pqClearConnErrorState, pqDropConnection(), pqDropServerData(), PQTRANS_IDLE, release_conn_addrinfo(), sendTerminateConn(), pg_conn::status, and pg_conn::xactStatus.

Referenced by PQcancelReset(), PQfinish(), PQreset(), and PQresetStart().

◆ PQconndefaults()

PQconninfoOption * PQconndefaults ( void  )

Definition at line 1940 of file fe-connect.c.

1941{
1942 PQExpBufferData errorBuf;
1943 PQconninfoOption *connOptions;
1944
1945 /* We don't actually report any errors here, but callees want a buffer */
1946 initPQExpBuffer(&errorBuf);
1947 if (PQExpBufferDataBroken(errorBuf))
1948 return NULL; /* out of memory already :-( */
1949
1950 connOptions = conninfo_init(&errorBuf);
1951 if (connOptions != NULL)
1952 {
1953 /* pass NULL errorBuf to ignore errors */
1954 if (!conninfo_add_defaults(connOptions, NULL))
1955 {
1956 PQconninfoFree(connOptions);
1957 connOptions = NULL;
1958 }
1959 }
1960
1961 termPQExpBuffer(&errorBuf);
1962 return connOptions;
1963}

References conninfo_add_defaults(), conninfo_init(), initPQExpBuffer(), PQconninfoFree(), PQExpBufferDataBroken, and termPQExpBuffer().

Referenced by check_pghost_envvar(), dblink_fdw_validator(), do_connect(), get_connect_string(), GetDbnameFromConnectionOptions(), InitPgFdwOptions(), and main().

◆ PQconnectdb()

PGconn * PQconnectdb ( const char *  conninfo)

Definition at line 753 of file fe-connect.c.

754{
755 PGconn *conn = PQconnectStart(conninfo);
756
757 if (conn && conn->status != CONNECTION_BAD)
759
760 return conn;
761}
PGconn * PQconnectStart(const char *conninfo)
Definition: fe-connect.c:881

References conn, CONNECTION_BAD, pqConnectDBComplete(), PQconnectStart(), and pg_conn::status.

Referenced by connect_database(), get_db_conn(), and main().

◆ pqConnectDBComplete()

int pqConnectDBComplete ( PGconn conn)

Definition at line 2529 of file fe-connect.c.

2530{
2533 int timeout = 0;
2534 int last_whichhost = -2; /* certainly different from whichhost */
2535 int last_whichaddr = -2; /* certainly different from whichaddr */
2536
2537 if (conn == NULL || conn->status == CONNECTION_BAD)
2538 return 0;
2539
2540 /*
2541 * Set up a time limit, if connect_timeout is greater than zero.
2542 */
2543 if (conn->connect_timeout != NULL)
2544 {
2545 if (!pqParseIntParam(conn->connect_timeout, &timeout, conn,
2546 "connect_timeout"))
2547 {
2548 /* mark the connection as bad to report the parsing failure */
2550 return 0;
2551 }
2552 }
2553
2554 for (;;)
2555 {
2556 int ret = 0;
2557
2558 /*
2559 * (Re)start the connect_timeout timer if it's active and we are
2560 * considering a different host than we were last time through. If
2561 * we've already succeeded, though, needn't recalculate.
2562 */
2563 if (flag != PGRES_POLLING_OK &&
2564 timeout > 0 &&
2565 (conn->whichhost != last_whichhost ||
2566 conn->whichaddr != last_whichaddr))
2567 {
2568 end_time = PQgetCurrentTimeUSec() + (pg_usec_time_t) timeout * 1000000;
2569 last_whichhost = conn->whichhost;
2570 last_whichaddr = conn->whichaddr;
2571 }
2572
2573 /*
2574 * Wait, if necessary. Note that the initial state (just after
2575 * PQconnectStart) is to wait for the socket to select for writing.
2576 */
2577 switch (flag)
2578 {
2579 case PGRES_POLLING_OK:
2580 return 1; /* success! */
2581
2583 ret = pqWaitTimed(1, 0, conn, end_time);
2584 if (ret == -1)
2585 {
2586 /* hard failure, eg select() problem, aborts everything */
2588 return 0;
2589 }
2590 break;
2591
2593 ret = pqWaitTimed(0, 1, conn, end_time);
2594 if (ret == -1)
2595 {
2596 /* hard failure, eg select() problem, aborts everything */
2598 return 0;
2599 }
2600 break;
2601
2602 default:
2603 /* Just in case we failed to set it in PQconnectPoll */
2605 return 0;
2606 }
2607
2608 if (ret == 1) /* connect_timeout elapsed */
2609 {
2610 /*
2611 * Give up on current server/address, try the next one.
2612 */
2613 conn->try_next_addr = true;
2615 }
2616
2617 /*
2618 * Now try to advance the state machine.
2619 */
2620 if (conn->cancelRequest)
2622 else
2624 }
2625}
PostgresPollingStatusType PQcancelPoll(PGcancelConn *cancelConn)
Definition: fe-cancel.c:207
PostgresPollingStatusType PQconnectPoll(PGconn *conn)
Definition: fe-connect.c:2655
bool pqParseIntParam(const char *value, int *result, PGconn *conn, const char *context)
Definition: fe-connect.c:7824
pg_usec_time_t PQgetCurrentTimeUSec(void)
Definition: fe-misc.c:1200
int pqWaitTimed(int forRead, int forWrite, PGconn *conn, pg_usec_time_t end_time)
Definition: fe-misc.c:1009
@ CONNECTION_NEEDED
Definition: libpq-fe.h:97
PostgresPollingStatusType
Definition: libpq-fe.h:109
@ PGRES_POLLING_OK
Definition: libpq-fe.h:113
@ PGRES_POLLING_READING
Definition: libpq-fe.h:111
pg_int64 pg_usec_time_t
Definition: libpq-fe.h:226
static int64 end_time
Definition: pgbench.c:175
int whichaddr
Definition: libpq-int.h:521
bool try_next_addr
Definition: libpq-int.h:518
char * flag(int b)
Definition: test-ctype.c:33

References pg_conn::cancelRequest, conn, pg_conn::connect_timeout, CONNECTION_BAD, CONNECTION_NEEDED, end_time, flag(), PGRES_POLLING_OK, PGRES_POLLING_READING, PGRES_POLLING_WRITING, PQcancelPoll(), PQconnectPoll(), PQgetCurrentTimeUSec(), pqParseIntParam(), pqWaitTimed(), pg_conn::status, pg_conn::try_next_addr, pg_conn::whichaddr, and pg_conn::whichhost.

Referenced by internal_ping(), PQcancelBlocking(), PQconnectdb(), PQconnectdbParams(), PQreset(), and PQsetdbLogin().

◆ PQconnectdbParams()

PGconn * PQconnectdbParams ( const char *const *  keywords,
const char *const *  values,
int  expand_dbname 
)

Definition at line 698 of file fe-connect.c.

701{
702 PGconn *conn = PQconnectStartParams(keywords, values, expand_dbname);
703
704 if (conn && conn->status != CONNECTION_BAD)
706
707 return conn;
708}
PGconn * PQconnectStartParams(const char *const *keywords, const char *const *values, int expand_dbname)
Definition: fe-connect.c:800

References conn, CONNECTION_BAD, keywords, pqConnectDBComplete(), PQconnectStartParams(), pg_conn::status, and values.

Referenced by ConnectDatabase(), connectDatabase(), copy_connection(), doConnect(), ECPGconnect(), GetConnection(), main(), sql_conn(), and vacuumlo().

◆ pqConnectDBStart()

int pqConnectDBStart ( PGconn conn)

Definition at line 2451 of file fe-connect.c.

2452{
2453 if (!conn)
2454 return 0;
2455
2456 if (!conn->options_valid)
2457 goto connect_errReturn;
2458
2459 /*
2460 * Check for bad linking to backend-internal versions of src/common
2461 * functions (see comments in link-canary.c for the reason we need this).
2462 * Nobody but developers should see this message, so we don't bother
2463 * translating it.
2464 */
2466 {
2468 "libpq is incorrectly linked to backend functions\n");
2469 goto connect_errReturn;
2470 }
2471
2472 /* Ensure our buffers are empty */
2473 conn->inStart = conn->inCursor = conn->inEnd = 0;
2474 conn->outCount = 0;
2475
2476 /*
2477 * Set up to try to connect to the first host. (Setting whichhost = -1 is
2478 * a bit of a cheat, but PQconnectPoll will advance it to 0 before
2479 * anything else looks at it.)
2480 *
2481 * Cancel requests are special though, they should only try one host and
2482 * address, and these fields have already been set up in PQcancelCreate,
2483 * so leave these fields alone for cancel requests.
2484 */
2485 if (!conn->cancelRequest)
2486 {
2487 conn->whichhost = -1;
2488 conn->try_next_host = true;
2489 conn->try_next_addr = false;
2490 }
2491
2493
2494 /* Also reset the target_server_type state if needed */
2497
2498 /*
2499 * The code for processing CONNECTION_NEEDED state is in PQconnectPoll(),
2500 * so that it can easily be re-executed if needed again during the
2501 * asynchronous startup process. However, we must run it once here,
2502 * because callers expect a success return from this routine to mean that
2503 * we are in PGRES_POLLING_WRITING connection state.
2504 */
2506 return 1;
2507
2508connect_errReturn:
2509
2510 /*
2511 * If we managed to open a socket, close it immediately rather than
2512 * waiting till PQfinish. (The application cannot have gotten the socket
2513 * from PQsocket yet, so this doesn't risk breaking anything.)
2514 */
2515 pqDropConnection(conn, true);
2517 return 0;
2518}
@ SERVER_TYPE_PREFER_STANDBY_PASS2
Definition: libpq-int.h:251
@ SERVER_TYPE_PREFER_STANDBY
Definition: libpq-int.h:250
bool try_next_host
Definition: libpq-int.h:519
int inCursor
Definition: libpq-int.h:548
int inEnd
Definition: libpq-int.h:549
int inStart
Definition: libpq-int.h:547
PGTargetServerType target_server_type
Definition: libpq-int.h:515
int outCount
Definition: libpq-int.h:554

References appendPQExpBufferStr(), pg_conn::cancelRequest, conn, CONNECTION_BAD, CONNECTION_NEEDED, pg_conn::errorMessage, pg_conn::inCursor, pg_conn::inEnd, pg_conn::inStart, pg_conn::options_valid, pg_conn::outCount, pg_link_canary_is_frontend(), PGRES_POLLING_WRITING, PQconnectPoll(), pqDropConnection(), SERVER_TYPE_PREFER_STANDBY, SERVER_TYPE_PREFER_STANDBY_PASS2, pg_conn::status, pg_conn::target_server_type, pg_conn::try_next_addr, pg_conn::try_next_host, and pg_conn::whichhost.

Referenced by PQcancelStart(), PQconnectStart(), PQconnectStartParams(), PQreset(), PQresetStart(), and PQsetdbLogin().

◆ PQconnectionNeedsPassword()

int PQconnectionNeedsPassword ( const PGconn conn)

Definition at line 7319 of file fe-connect.c.

7320{
7321 char *password;
7322
7323 if (!conn)
7324 return false;
7325 password = PQpass(conn);
7326 if (conn->password_needed &&
7327 (password == NULL || password[0] == '\0'))
7328 return true;
7329 else
7330 return false;
7331}
char * PQpass(const PGconn *conn)
Definition: fe-connect.c:7120

References conn, password, pg_conn::password_needed, and PQpass().

Referenced by ConnectDatabase(), connectDatabase(), do_connect(), doConnect(), GetConnection(), main(), sql_conn(), and vacuumlo().

◆ PQconnectionUsedGSSAPI()

int PQconnectionUsedGSSAPI ( const PGconn conn)

Definition at line 7345 of file fe-connect.c.

7346{
7347 if (!conn)
7348 return false;
7349 if (conn->gssapi_used)
7350 return true;
7351 else
7352 return false;
7353}
bool gssapi_used
Definition: libpq-int.h:498

References conn, and pg_conn::gssapi_used.

Referenced by dblink_security_check(), and pgfdw_security_check().

◆ PQconnectionUsedPassword()

int PQconnectionUsedPassword ( const PGconn conn)

Definition at line 7334 of file fe-connect.c.

7335{
7336 if (!conn)
7337 return false;
7338 if (conn->password_needed)
7339 return true;
7340 else
7341 return false;
7342}

References conn, and pg_conn::password_needed.

Referenced by ConnectDatabase(), dblink_security_check(), libpqrcv_connect(), and pgfdw_security_check().

◆ pqConnectOptions2()

bool pqConnectOptions2 ( PGconn conn)

Definition at line 1129 of file fe-connect.c.

1130{
1131 int i;
1132
1133 /*
1134 * Allocate memory for details about each host to which we might possibly
1135 * try to connect. For that, count the number of elements in the hostaddr
1136 * or host options. If neither is given, assume one host.
1137 */
1138 conn->whichhost = 0;
1139 if (conn->pghostaddr && conn->pghostaddr[0] != '\0')
1141 else if (conn->pghost && conn->pghost[0] != '\0')
1143 else
1144 conn->nconnhost = 1;
1146 calloc(conn->nconnhost, sizeof(pg_conn_host));
1147 if (conn->connhost == NULL)
1148 goto oom_error;
1149
1150 /*
1151 * We now have one pg_conn_host structure per possible host. Fill in the
1152 * host and hostaddr fields for each, by splitting the parameter strings.
1153 */
1154 if (conn->pghostaddr != NULL && conn->pghostaddr[0] != '\0')
1155 {
1156 char *s = conn->pghostaddr;
1157 bool more = true;
1158
1159 for (i = 0; i < conn->nconnhost && more; i++)
1160 {
1162 if (conn->connhost[i].hostaddr == NULL)
1163 goto oom_error;
1164 }
1165
1166 /*
1167 * If hostaddr was given, the array was allocated according to the
1168 * number of elements in the hostaddr list, so it really should be the
1169 * right size.
1170 */
1171 Assert(!more);
1172 Assert(i == conn->nconnhost);
1173 }
1174
1175 if (conn->pghost != NULL && conn->pghost[0] != '\0')
1176 {
1177 char *s = conn->pghost;
1178 bool more = true;
1179
1180 for (i = 0; i < conn->nconnhost && more; i++)
1181 {
1183 if (conn->connhost[i].host == NULL)
1184 goto oom_error;
1185 }
1186
1187 /* Check for wrong number of host items. */
1188 if (more || i != conn->nconnhost)
1189 {
1191 libpq_append_conn_error(conn, "could not match %d host names to %d hostaddr values",
1193 return false;
1194 }
1195 }
1196
1197 /*
1198 * Now, for each host slot, identify the type of address spec, and fill in
1199 * the default address if nothing was given.
1200 */
1201 for (i = 0; i < conn->nconnhost; i++)
1202 {
1203 pg_conn_host *ch = &conn->connhost[i];
1204
1205 if (ch->hostaddr != NULL && ch->hostaddr[0] != '\0')
1206 ch->type = CHT_HOST_ADDRESS;
1207 else if (ch->host != NULL && ch->host[0] != '\0')
1208 {
1209 ch->type = CHT_HOST_NAME;
1210 if (is_unixsock_path(ch->host))
1211 ch->type = CHT_UNIX_SOCKET;
1212 }
1213 else
1214 {
1215 free(ch->host);
1216
1217 /*
1218 * This bit selects the default host location. If you change
1219 * this, see also pg_regress.
1220 */
1221 if (DEFAULT_PGSOCKET_DIR[0])
1222 {
1223 ch->host = strdup(DEFAULT_PGSOCKET_DIR);
1224 ch->type = CHT_UNIX_SOCKET;
1225 }
1226 else
1227 {
1228 ch->host = strdup(DefaultHost);
1229 ch->type = CHT_HOST_NAME;
1230 }
1231 if (ch->host == NULL)
1232 goto oom_error;
1233 }
1234 }
1235
1236 /*
1237 * Next, work out the port number corresponding to each host name.
1238 *
1239 * Note: unlike the above for host names, this could leave the port fields
1240 * as null or empty strings. We will substitute DEF_PGPORT whenever we
1241 * read such a port field.
1242 */
1243 if (conn->pgport != NULL && conn->pgport[0] != '\0')
1244 {
1245 char *s = conn->pgport;
1246 bool more = true;
1247
1248 for (i = 0; i < conn->nconnhost && more; i++)
1249 {
1251 if (conn->connhost[i].port == NULL)
1252 goto oom_error;
1253 }
1254
1255 /*
1256 * If exactly one port was given, use it for every host. Otherwise,
1257 * there must be exactly as many ports as there were hosts.
1258 */
1259 if (i == 1 && !more)
1260 {
1261 for (i = 1; i < conn->nconnhost; i++)
1262 {
1263 conn->connhost[i].port = strdup(conn->connhost[0].port);
1264 if (conn->connhost[i].port == NULL)
1265 goto oom_error;
1266 }
1267 }
1268 else if (more || i != conn->nconnhost)
1269 {
1271 libpq_append_conn_error(conn, "could not match %d port numbers to %d hosts",
1273 return false;
1274 }
1275 }
1276
1277 /*
1278 * If user name was not given, fetch it. (Most likely, the fetch will
1279 * fail, since the only way we get here is if pg_fe_getauthname() failed
1280 * during conninfo_add_defaults(). But now we want an error message.)
1281 */
1282 if (conn->pguser == NULL || conn->pguser[0] == '\0')
1283 {
1284 free(conn->pguser);
1286 if (!conn->pguser)
1287 {
1289 return false;
1290 }
1291 }
1292
1293 /*
1294 * If database name was not given, default it to equal user name
1295 */
1296 if (conn->dbName == NULL || conn->dbName[0] == '\0')
1297 {
1298 free(conn->dbName);
1299 conn->dbName = strdup(conn->pguser);
1300 if (!conn->dbName)
1301 goto oom_error;
1302 }
1303
1304 /*
1305 * If password was not given, try to look it up in password file. Note
1306 * that the result might be different for each host/port pair.
1307 */
1308 if (conn->pgpass == NULL || conn->pgpass[0] == '\0')
1309 {
1310 /* If password file wasn't specified, use ~/PGPASSFILE */
1311 if (conn->pgpassfile == NULL || conn->pgpassfile[0] == '\0')
1312 {
1313 char homedir[MAXPGPATH];
1314
1315 if (pqGetHomeDirectory(homedir, sizeof(homedir)))
1316 {
1319 if (!conn->pgpassfile)
1320 goto oom_error;
1321 snprintf(conn->pgpassfile, MAXPGPATH, "%s/%s",
1322 homedir, PGPASSFILE);
1323 }
1324 }
1325
1326 if (conn->pgpassfile != NULL && conn->pgpassfile[0] != '\0')
1327 {
1328 for (i = 0; i < conn->nconnhost; i++)
1329 {
1330 /*
1331 * Try to get a password for this host from file. We use host
1332 * for the hostname search key if given, else hostaddr (at
1333 * least one of them is guaranteed nonempty by now).
1334 */
1335 const char *pwhost = conn->connhost[i].host;
1336
1337 if (pwhost == NULL || pwhost[0] == '\0')
1338 pwhost = conn->connhost[i].hostaddr;
1339
1341 passwordFromFile(pwhost,
1342 conn->connhost[i].port,
1343 conn->dbName,
1344 conn->pguser,
1345 conn->pgpassfile);
1346 }
1347 }
1348 }
1349
1350 /*
1351 * parse and validate require_auth option
1352 */
1353 if (conn->require_auth && conn->require_auth[0])
1354 {
1355 char *s = conn->require_auth;
1356 bool first,
1357 more;
1358 bool negated = false;
1359
1360 /*
1361 * By default, start from an empty set of allowed options and add to
1362 * it.
1363 */
1364 conn->auth_required = true;
1366
1367 for (first = true, more = true; more; first = false)
1368 {
1369 char *method,
1370 *part;
1371 uint32 bits;
1372
1373 part = parse_comma_separated_list(&s, &more);
1374 if (part == NULL)
1375 goto oom_error;
1376
1377 /*
1378 * Check for negation, e.g. '!password'. If one element is
1379 * negated, they all have to be.
1380 */
1381 method = part;
1382 if (*method == '!')
1383 {
1384 if (first)
1385 {
1386 /*
1387 * Switch to a permissive set of allowed options, and
1388 * subtract from it.
1389 */
1390 conn->auth_required = false;
1392 }
1393 else if (!negated)
1394 {
1396 libpq_append_conn_error(conn, "negative require_auth method \"%s\" cannot be mixed with non-negative methods",
1397 method);
1398
1399 free(part);
1400 return false;
1401 }
1402
1403 negated = true;
1404 method++;
1405 }
1406 else if (negated)
1407 {
1409 libpq_append_conn_error(conn, "require_auth method \"%s\" cannot be mixed with negative methods",
1410 method);
1411
1412 free(part);
1413 return false;
1414 }
1415
1416 if (strcmp(method, "password") == 0)
1417 {
1418 bits = (1 << AUTH_REQ_PASSWORD);
1419 }
1420 else if (strcmp(method, "md5") == 0)
1421 {
1422 bits = (1 << AUTH_REQ_MD5);
1423 }
1424 else if (strcmp(method, "gss") == 0)
1425 {
1426 bits = (1 << AUTH_REQ_GSS);
1427 bits |= (1 << AUTH_REQ_GSS_CONT);
1428 }
1429 else if (strcmp(method, "sspi") == 0)
1430 {
1431 bits = (1 << AUTH_REQ_SSPI);
1432 bits |= (1 << AUTH_REQ_GSS_CONT);
1433 }
1434 else if (strcmp(method, "scram-sha-256") == 0)
1435 {
1436 /* This currently assumes that SCRAM is the only SASL method. */
1437 bits = (1 << AUTH_REQ_SASL);
1438 bits |= (1 << AUTH_REQ_SASL_CONT);
1439 bits |= (1 << AUTH_REQ_SASL_FIN);
1440 }
1441 else if (strcmp(method, "none") == 0)
1442 {
1443 /*
1444 * Special case: let the user explicitly allow (or disallow)
1445 * connections where the server does not send an explicit
1446 * authentication challenge, such as "trust" and "cert" auth.
1447 */
1448 if (negated) /* "!none" */
1449 {
1450 if (conn->auth_required)
1451 goto duplicate;
1452
1453 conn->auth_required = true;
1454 }
1455 else /* "none" */
1456 {
1457 if (!conn->auth_required)
1458 goto duplicate;
1459
1460 conn->auth_required = false;
1461 }
1462
1463 free(part);
1464 continue; /* avoid the bitmask manipulation below */
1465 }
1466 else
1467 {
1469 libpq_append_conn_error(conn, "invalid %s value: \"%s\"",
1470 "require_auth", method);
1471
1472 free(part);
1473 return false;
1474 }
1475
1476 /* Update the bitmask. */
1477 if (negated)
1478 {
1479 if ((conn->allowed_auth_methods & bits) == 0)
1480 goto duplicate;
1481
1482 conn->allowed_auth_methods &= ~bits;
1483 }
1484 else
1485 {
1486 if ((conn->allowed_auth_methods & bits) == bits)
1487 goto duplicate;
1488
1489 conn->allowed_auth_methods |= bits;
1490 }
1491
1492 free(part);
1493 continue;
1494
1495 duplicate:
1496
1497 /*
1498 * A duplicated method probably indicates a typo in a setting
1499 * where typos are extremely risky.
1500 */
1502 libpq_append_conn_error(conn, "require_auth method \"%s\" is specified more than once",
1503 part);
1504
1505 free(part);
1506 return false;
1507 }
1508 }
1509
1510 /*
1511 * validate channel_binding option
1512 */
1513 if (conn->channel_binding)
1514 {
1515 if (strcmp(conn->channel_binding, "disable") != 0
1516 && strcmp(conn->channel_binding, "prefer") != 0
1517 && strcmp(conn->channel_binding, "require") != 0)
1518 {
1520 libpq_append_conn_error(conn, "invalid %s value: \"%s\"",
1521 "channel_binding", conn->channel_binding);
1522 return false;
1523 }
1524 }
1525 else
1526 {
1528 if (!conn->channel_binding)
1529 goto oom_error;
1530 }
1531
1532#ifndef USE_SSL
1533
1534 /*
1535 * sslrootcert=system is not supported. Since setting this changes the
1536 * default sslmode, check this _before_ we validate sslmode, to avoid
1537 * confusing the user with errors for an option they may not have set.
1538 */
1539 if (conn->sslrootcert
1540 && strcmp(conn->sslrootcert, "system") == 0)
1541 {
1543 libpq_append_conn_error(conn, "%s value \"%s\" invalid when SSL support is not compiled in",
1544 "sslrootcert", conn->sslrootcert);
1545 return false;
1546 }
1547#endif
1548
1549 /*
1550 * validate sslmode option
1551 */
1552 if (conn->sslmode)
1553 {
1554 if (strcmp(conn->sslmode, "disable") != 0
1555 && strcmp(conn->sslmode, "allow") != 0
1556 && strcmp(conn->sslmode, "prefer") != 0
1557 && strcmp(conn->sslmode, "require") != 0
1558 && strcmp(conn->sslmode, "verify-ca") != 0
1559 && strcmp(conn->sslmode, "verify-full") != 0)
1560 {
1562 libpq_append_conn_error(conn, "invalid %s value: \"%s\"",
1563 "sslmode", conn->sslmode);
1564 return false;
1565 }
1566
1567#ifndef USE_SSL
1568 switch (conn->sslmode[0])
1569 {
1570 case 'a': /* "allow" */
1571 case 'p': /* "prefer" */
1572
1573 /*
1574 * warn user that an SSL connection will never be negotiated
1575 * since SSL was not compiled in?
1576 */
1577 break;
1578
1579 case 'r': /* "require" */
1580 case 'v': /* "verify-ca" or "verify-full" */
1582 libpq_append_conn_error(conn, "%s value \"%s\" invalid when SSL support is not compiled in",
1583 "sslmode", conn->sslmode);
1584 return false;
1585 }
1586#endif
1587 }
1588 else
1589 {
1590 conn->sslmode = strdup(DefaultSSLMode);
1591 if (!conn->sslmode)
1592 goto oom_error;
1593 }
1594
1595 /*
1596 * validate sslnegotiation option, default is "postgres" for the postgres
1597 * style negotiated connection with an extra round trip but more options.
1598 */
1599 if (conn->sslnegotiation)
1600 {
1601 if (strcmp(conn->sslnegotiation, "postgres") != 0
1602 && strcmp(conn->sslnegotiation, "direct") != 0)
1603 {
1605 libpq_append_conn_error(conn, "invalid %s value: \"%s\"",
1606 "sslnegotiation", conn->sslnegotiation);
1607 return false;
1608 }
1609
1610#ifndef USE_SSL
1611 if (conn->sslnegotiation[0] != 'p')
1612 {
1614 libpq_append_conn_error(conn, "%s value \"%s\" invalid when SSL support is not compiled in",
1615 "sslnegotiation", conn->sslnegotiation);
1616 return false;
1617 }
1618#endif
1619
1620 /*
1621 * Don't allow direct SSL negotiation with sslmode='prefer', because
1622 * that poses a risk of unintentional fallback to plaintext connection
1623 * when connecting to a pre-v17 server that does not support direct
1624 * SSL connections. To keep things simple, don't allow it with
1625 * sslmode='allow' or sslmode='disable' either. If a user goes through
1626 * the trouble of setting sslnegotiation='direct', they probably
1627 * intend to use SSL, and sslmode=disable or allow is probably a user
1628 * user mistake anyway.
1629 */
1630 if (conn->sslnegotiation[0] == 'd' &&
1631 conn->sslmode[0] != 'r' && conn->sslmode[0] != 'v')
1632 {
1634 libpq_append_conn_error(conn, "weak sslmode \"%s\" may not be used with sslnegotiation=direct (use \"require\", \"verify-ca\", or \"verify-full\")",
1635 conn->sslmode);
1636 return false;
1637 }
1638 }
1639 else
1640 {
1642 if (!conn->sslnegotiation)
1643 goto oom_error;
1644 }
1645
1646#ifdef USE_SSL
1647
1648 /*
1649 * If sslrootcert=system, make sure our chosen sslmode is compatible.
1650 */
1651 if (conn->sslrootcert
1652 && strcmp(conn->sslrootcert, "system") == 0
1653 && strcmp(conn->sslmode, "verify-full") != 0)
1654 {
1656 libpq_append_conn_error(conn, "weak sslmode \"%s\" may not be used with sslrootcert=system (use \"verify-full\")",
1657 conn->sslmode);
1658 return false;
1659 }
1660#endif
1661
1662 /*
1663 * Validate TLS protocol versions for ssl_min_protocol_version and
1664 * ssl_max_protocol_version.
1665 */
1667 {
1669 libpq_append_conn_error(conn, "invalid \"%s\" value: \"%s\"",
1670 "ssl_min_protocol_version",
1672 return false;
1673 }
1675 {
1677 libpq_append_conn_error(conn, "invalid \"%s\" value: \"%s\"",
1678 "ssl_max_protocol_version",
1680 return false;
1681 }
1682
1683 /*
1684 * Check if the range of SSL protocols defined is correct. This is done
1685 * at this early step because this is independent of the SSL
1686 * implementation used, and this avoids unnecessary cycles with an
1687 * already-built SSL context when the connection is being established, as
1688 * it would be doomed anyway.
1689 */
1692 {
1694 libpq_append_conn_error(conn, "invalid SSL protocol version range");
1695 return false;
1696 }
1697
1698 /*
1699 * validate sslcertmode option
1700 */
1701 if (conn->sslcertmode)
1702 {
1703 if (strcmp(conn->sslcertmode, "disable") != 0 &&
1704 strcmp(conn->sslcertmode, "allow") != 0 &&
1705 strcmp(conn->sslcertmode, "require") != 0)
1706 {
1708 libpq_append_conn_error(conn, "invalid %s value: \"%s\"",
1709 "sslcertmode", conn->sslcertmode);
1710 return false;
1711 }
1712#ifndef USE_SSL
1713 if (strcmp(conn->sslcertmode, "require") == 0)
1714 {
1716 libpq_append_conn_error(conn, "%s value \"%s\" invalid when SSL support is not compiled in",
1717 "sslcertmode", conn->sslcertmode);
1718 return false;
1719 }
1720#endif
1721#ifndef HAVE_SSL_CTX_SET_CERT_CB
1722
1723 /*
1724 * Without a certificate callback, the current implementation can't
1725 * figure out if a certificate was actually requested, so "require" is
1726 * useless.
1727 */
1728 if (strcmp(conn->sslcertmode, "require") == 0)
1729 {
1731 libpq_append_conn_error(conn, "%s value \"%s\" is not supported (check OpenSSL version)",
1732 "sslcertmode", conn->sslcertmode);
1733 return false;
1734 }
1735#endif
1736 }
1737 else
1738 {
1740 if (!conn->sslcertmode)
1741 goto oom_error;
1742 }
1743
1744 /*
1745 * validate gssencmode option
1746 */
1747 if (conn->gssencmode)
1748 {
1749 if (strcmp(conn->gssencmode, "disable") != 0 &&
1750 strcmp(conn->gssencmode, "prefer") != 0 &&
1751 strcmp(conn->gssencmode, "require") != 0)
1752 {
1754 libpq_append_conn_error(conn, "invalid %s value: \"%s\"", "gssencmode", conn->gssencmode);
1755 return false;
1756 }
1757#ifndef ENABLE_GSS
1758 if (strcmp(conn->gssencmode, "require") == 0)
1759 {
1761 libpq_append_conn_error(conn, "gssencmode value \"%s\" invalid when GSSAPI support is not compiled in",
1762 conn->gssencmode);
1763 return false;
1764 }
1765#endif
1766 }
1767 else
1768 {
1769 conn->gssencmode = strdup(DefaultGSSMode);
1770 if (!conn->gssencmode)
1771 goto oom_error;
1772 }
1773
1774 /*
1775 * validate target_session_attrs option, and set target_server_type
1776 */
1778 {
1779 if (strcmp(conn->target_session_attrs, "any") == 0)
1781 else if (strcmp(conn->target_session_attrs, "read-write") == 0)
1783 else if (strcmp(conn->target_session_attrs, "read-only") == 0)
1785 else if (strcmp(conn->target_session_attrs, "primary") == 0)
1787 else if (strcmp(conn->target_session_attrs, "standby") == 0)
1789 else if (strcmp(conn->target_session_attrs, "prefer-standby") == 0)
1791 else
1792 {
1794 libpq_append_conn_error(conn, "invalid %s value: \"%s\"",
1795 "target_session_attrs",
1797 return false;
1798 }
1799 }
1800 else
1802
1804 {
1805 int len;
1806
1810 goto oom_error;
1813 if (len < 0)
1814 {
1815 libpq_append_conn_error(conn, "invalid SCRAM client key");
1817 return false;
1818 }
1819 if (len != SCRAM_MAX_KEY_LEN)
1820 {
1821 libpq_append_conn_error(conn, "invalid SCRAM client key length: %d", len);
1823 return false;
1824 }
1826 }
1827
1829 {
1830 int len;
1831
1835 goto oom_error;
1838 if (len < 0)
1839 {
1840 libpq_append_conn_error(conn, "invalid SCRAM server key");
1842 return false;
1843 }
1844 if (len != SCRAM_MAX_KEY_LEN)
1845 {
1846 libpq_append_conn_error(conn, "invalid SCRAM server key length: %d", len);
1848 return false;
1849 }
1851 }
1852
1853 /*
1854 * validate load_balance_hosts option, and set load_balance_type
1855 */
1857 {
1858 if (strcmp(conn->load_balance_hosts, "disable") == 0)
1860 else if (strcmp(conn->load_balance_hosts, "random") == 0)
1862 else
1863 {
1865 libpq_append_conn_error(conn, "invalid %s value: \"%s\"",
1866 "load_balance_hosts",
1868 return false;
1869 }
1870 }
1871 else
1873
1875 {
1877
1878 /*
1879 * This is the "inside-out" variant of the Fisher-Yates shuffle
1880 * algorithm. Notionally, we append each new value to the array and
1881 * then swap it with a randomly-chosen array element (possibly
1882 * including itself, else we fail to generate permutations with the
1883 * last integer last). The swap step can be optimized by combining it
1884 * with the insertion.
1885 */
1886 for (i = 1; i < conn->nconnhost; i++)
1887 {
1888 int j = pg_prng_uint64_range(&conn->prng_state, 0, i);
1889 pg_conn_host temp = conn->connhost[j];
1890
1891 conn->connhost[j] = conn->connhost[i];
1892 conn->connhost[i] = temp;
1893 }
1894 }
1895
1896 /*
1897 * Resolve special "auto" client_encoding from the locale
1898 */
1900 strcmp(conn->client_encoding_initial, "auto") == 0)
1901 {
1905 goto oom_error;
1906 }
1907
1908 /*
1909 * Only if we get this far is it appropriate to try to connect. (We need a
1910 * state flag, rather than just the boolean result of this function, in
1911 * case someone tries to PQreset() the PGconn.)
1912 */
1913 conn->options_valid = true;
1914
1915 return true;
1916
1917oom_error:
1919 libpq_append_conn_error(conn, "out of memory");
1920 return false;
1921}
int pg_b64_decode(const char *src, int len, char *dst, int dstlen)
Definition: base64.c:116
int pg_b64_dec_len(int srclen)
Definition: base64.c:239
uint32_t uint32
Definition: c.h:488
static char * passwordFromFile(const char *hostname, const char *port, const char *dbname, const char *username, const char *pgpassfile)
Definition: fe-connect.c:7534
static bool sslVerifyProtocolRange(const char *min, const char *max)
Definition: fe-connect.c:7730
static void libpq_prng_init(PGconn *conn)
Definition: fe-connect.c:1102
#define DefaultSSLMode
Definition: fe-connect.c:131
#define DefaultGSSMode
Definition: fe-connect.c:139
static int count_comma_separated_elems(const char *input)
Definition: fe-connect.c:1043
#define DefaultChannelBinding
Definition: fe-connect.c:123
static bool sslVerifyProtocolVersion(const char *version)
Definition: fe-connect.c:7704
#define DefaultSSLNegotiation
Definition: fe-connect.c:134
#define PGPASSFILE
Definition: fe-connect.c:77
#define DefaultSSLCertMode
Definition: fe-connect.c:132
static char * parse_comma_separated_list(char **startptr, bool *more)
Definition: fe-connect.c:1067
#define calloc(a, b)
Definition: header.h:55
int j
Definition: isn.c:73
@ LOAD_BALANCE_DISABLE
Definition: libpq-int.h:257
@ LOAD_BALANCE_RANDOM
Definition: libpq-int.h:258
@ CHT_UNIX_SOCKET
Definition: libpq-int.h:319
@ CHT_HOST_NAME
Definition: libpq-int.h:317
@ SERVER_TYPE_STANDBY
Definition: libpq-int.h:249
@ SERVER_TYPE_PRIMARY
Definition: libpq-int.h:248
@ SERVER_TYPE_ANY
Definition: libpq-int.h:245
@ SERVER_TYPE_READ_WRITE
Definition: libpq-int.h:246
@ SERVER_TYPE_READ_ONLY
Definition: libpq-int.h:247
uint64 pg_prng_uint64_range(pg_prng_state *state, uint64 rmin, uint64 rmax)
Definition: pg_prng.c:144
#define pg_encoding_to_char
Definition: pg_wchar.h:630
int pg_get_encoding_from_locale(const char *ctype, bool write_message)
Definition: chklocale.c:301
#define AUTH_REQ_SSPI
Definition: protocol.h:83
#define AUTH_REQ_SASL_CONT
Definition: protocol.h:85
#define AUTH_REQ_GSS
Definition: protocol.h:81
#define AUTH_REQ_MD5
Definition: protocol.h:79
#define AUTH_REQ_PASSWORD
Definition: protocol.h:77
#define AUTH_REQ_GSS_CONT
Definition: protocol.h:82
#define AUTH_REQ_SASL
Definition: protocol.h:84
#define AUTH_REQ_SASL_FIN
Definition: protocol.h:86
#define SCRAM_MAX_KEY_LEN
Definition: scram-common.h:30
int nconnhost
Definition: libpq-int.h:471
void * scram_server_key_binary
Definition: libpq-int.h:528
size_t scram_client_key_len
Definition: libpq-int.h:525
uint32 allowed_auth_methods
Definition: libpq-int.h:506
bool auth_required
Definition: libpq-int.h:504
void * scram_client_key_binary
Definition: libpq-int.h:526
PGLoadBalanceType load_balance_type
Definition: libpq-int.h:516
size_t scram_server_key_len
Definition: libpq-int.h:527

References pg_conn::allowed_auth_methods, Assert, AUTH_REQ_GSS, AUTH_REQ_GSS_CONT, AUTH_REQ_MD5, AUTH_REQ_PASSWORD, AUTH_REQ_SASL, AUTH_REQ_SASL_CONT, AUTH_REQ_SASL_FIN, AUTH_REQ_SSPI, pg_conn::auth_required, calloc, pg_conn::channel_binding, CHT_HOST_ADDRESS, CHT_HOST_NAME, CHT_UNIX_SOCKET, pg_conn::client_encoding_initial, conn, CONNECTION_BAD, pg_conn::connhost, count_comma_separated_elems(), pg_conn::dbName, DEFAULT_PGSOCKET_DIR, DefaultChannelBinding, DefaultGSSMode, DefaultHost, DefaultSSLCertMode, DefaultSSLMode, DefaultSSLNegotiation, pg_conn::errorMessage, free, pg_conn::gssencmode, pg_conn_host::host, pg_conn_host::hostaddr, i, is_unixsock_path(), j, len, libpq_append_conn_error(), libpq_prng_init(), LOAD_BALANCE_DISABLE, pg_conn::load_balance_hosts, LOAD_BALANCE_RANDOM, pg_conn::load_balance_type, malloc, MAXPGPATH, pg_conn::nconnhost, pg_conn::options_valid, parse_comma_separated_list(), pg_conn_host::password, passwordFromFile(), pg_b64_dec_len(), pg_b64_decode(), pg_encoding_to_char, pg_fe_getauthname(), pg_get_encoding_from_locale(), pg_prng_uint64_range(), pg_conn::pghost, pg_conn::pghostaddr, pg_conn::pgpass, PGPASSFILE, pg_conn::pgpassfile, pg_conn::pgport, pg_conn::pguser, pg_conn_host::port, pqGetHomeDirectory(), pg_conn::prng_state, pg_conn::require_auth, pg_conn::scram_client_key, pg_conn::scram_client_key_binary, pg_conn::scram_client_key_len, SCRAM_MAX_KEY_LEN, pg_conn::scram_server_key, pg_conn::scram_server_key_binary, pg_conn::scram_server_key_len, SERVER_TYPE_ANY, SERVER_TYPE_PREFER_STANDBY, SERVER_TYPE_PRIMARY, SERVER_TYPE_READ_ONLY, SERVER_TYPE_READ_WRITE, SERVER_TYPE_STANDBY, snprintf, pg_conn::ssl_max_protocol_version, pg_conn::ssl_min_protocol_version, pg_conn::sslcertmode, pg_conn::sslmode, pg_conn::sslnegotiation, pg_conn::sslrootcert, sslVerifyProtocolRange(), sslVerifyProtocolVersion(), pg_conn::status, pg_conn::target_server_type, pg_conn::target_session_attrs, pg_conn_host::type, and pg_conn::whichhost.

Referenced by PQcancelCreate(), PQconnectStart(), PQconnectStartParams(), and PQsetdbLogin().

◆ PQconnectPoll()

PostgresPollingStatusType PQconnectPoll ( PGconn conn)

Definition at line 2655 of file fe-connect.c.

2656{
2657 bool reset_connection_state_machine = false;
2658 bool need_new_connection = false;
2659 PGresult *res;
2660 char sebuf[PG_STRERROR_R_BUFLEN];
2661 int optval;
2662
2663 if (conn == NULL)
2664 return PGRES_POLLING_FAILED;
2665
2666 /* Get the new data */
2667 switch (conn->status)
2668 {
2669 /*
2670 * We really shouldn't have been polled in these two cases, but we
2671 * can handle it.
2672 */
2673 case CONNECTION_BAD:
2674 return PGRES_POLLING_FAILED;
2675 case CONNECTION_OK:
2676 return PGRES_POLLING_OK;
2677
2678 /* These are reading states */
2680 case CONNECTION_AUTH_OK:
2682 case CONNECTION_CONSUME:
2684 {
2685 /* Load waiting data */
2686 int n = pqReadData(conn);
2687
2688 if (n < 0)
2689 goto error_return;
2690 if (n == 0)
2691 return PGRES_POLLING_READING;
2692
2693 break;
2694 }
2695
2696 /* These are writing states, so we just proceed. */
2697 case CONNECTION_STARTED:
2698 case CONNECTION_MADE:
2699 break;
2700
2701 /* Special cases: proceed without waiting. */
2703 case CONNECTION_NEEDED:
2706 break;
2707
2708 default:
2709 libpq_append_conn_error(conn, "invalid connection state, probably indicative of memory corruption");
2710 goto error_return;
2711 }
2712
2713
2714keep_going: /* We will come back to here until there is
2715 * nothing left to do. */
2716
2717 /* Time to advance to next address, or next host if no more addresses? */
2718 if (conn->try_next_addr)
2719 {
2720 if (conn->whichaddr < conn->naddr)
2721 {
2722 conn->whichaddr++;
2723 reset_connection_state_machine = true;
2724 }
2725 else
2726 conn->try_next_host = true;
2727 conn->try_next_addr = false;
2728 }
2729
2730 /* Time to advance to next connhost[] entry? */
2731 if (conn->try_next_host)
2732 {
2733 pg_conn_host *ch;
2734 struct addrinfo hint;
2735 struct addrinfo *addrlist;
2736 int thisport;
2737 int ret;
2738 char portstr[MAXPGPATH];
2739
2740 if (conn->whichhost + 1 < conn->nconnhost)
2741 conn->whichhost++;
2742 else
2743 {
2744 /*
2745 * Oops, no more hosts.
2746 *
2747 * If we are trying to connect in "prefer-standby" mode, then drop
2748 * the standby requirement and start over. Don't do this for
2749 * cancel requests though, since we are certain the list of
2750 * servers won't change as the target_server_type option is not
2751 * applicable to those connections.
2752 *
2753 * Otherwise, an appropriate error message is already set up, so
2754 * we just need to set the right status.
2755 */
2757 conn->nconnhost > 0 &&
2759 {
2761 conn->whichhost = 0;
2762 }
2763 else
2764 goto error_return;
2765 }
2766
2767 /* Drop any address info for previous host */
2769
2770 /*
2771 * Look up info for the new host. On failure, log the problem in
2772 * conn->errorMessage, then loop around to try the next host. (Note
2773 * we don't clear try_next_host until we've succeeded.)
2774 */
2775 ch = &conn->connhost[conn->whichhost];
2776
2777 /* Initialize hint structure */
2778 MemSet(&hint, 0, sizeof(hint));
2779 hint.ai_socktype = SOCK_STREAM;
2780 hint.ai_family = AF_UNSPEC;
2781
2782 /* Figure out the port number we're going to use. */
2783 if (ch->port == NULL || ch->port[0] == '\0')
2784 thisport = DEF_PGPORT;
2785 else
2786 {
2787 if (!pqParseIntParam(ch->port, &thisport, conn, "port"))
2788 goto error_return;
2789
2790 if (thisport < 1 || thisport > 65535)
2791 {
2792 libpq_append_conn_error(conn, "invalid port number: \"%s\"", ch->port);
2793 goto keep_going;
2794 }
2795 }
2796 snprintf(portstr, sizeof(portstr), "%d", thisport);
2797
2798 /* Use pg_getaddrinfo_all() to resolve the address */
2799 switch (ch->type)
2800 {
2801 case CHT_HOST_NAME:
2802 ret = pg_getaddrinfo_all(ch->host, portstr, &hint,
2803 &addrlist);
2804 if (ret || !addrlist)
2805 {
2806 libpq_append_conn_error(conn, "could not translate host name \"%s\" to address: %s",
2807 ch->host, gai_strerror(ret));
2808 goto keep_going;
2809 }
2810 break;
2811
2812 case CHT_HOST_ADDRESS:
2813 hint.ai_flags = AI_NUMERICHOST;
2814 ret = pg_getaddrinfo_all(ch->hostaddr, portstr, &hint,
2815 &addrlist);
2816 if (ret || !addrlist)
2817 {
2818 libpq_append_conn_error(conn, "could not parse network address \"%s\": %s",
2819 ch->hostaddr, gai_strerror(ret));
2820 goto keep_going;
2821 }
2822 break;
2823
2824 case CHT_UNIX_SOCKET:
2825 hint.ai_family = AF_UNIX;
2826 UNIXSOCK_PATH(portstr, thisport, ch->host);
2827 if (strlen(portstr) >= UNIXSOCK_PATH_BUFLEN)
2828 {
2829 libpq_append_conn_error(conn, "Unix-domain socket path \"%s\" is too long (maximum %d bytes)",
2830 portstr,
2831 (int) (UNIXSOCK_PATH_BUFLEN - 1));
2832 goto keep_going;
2833 }
2834
2835 /*
2836 * NULL hostname tells pg_getaddrinfo_all to parse the service
2837 * name as a Unix-domain socket path.
2838 */
2839 ret = pg_getaddrinfo_all(NULL, portstr, &hint,
2840 &addrlist);
2841 if (ret || !addrlist)
2842 {
2843 libpq_append_conn_error(conn, "could not translate Unix-domain socket path \"%s\" to address: %s",
2844 portstr, gai_strerror(ret));
2845 goto keep_going;
2846 }
2847 break;
2848 }
2849
2850 /*
2851 * Store a copy of the addrlist in private memory so we can perform
2852 * randomization for load balancing.
2853 */
2854 ret = store_conn_addrinfo(conn, addrlist);
2855 pg_freeaddrinfo_all(hint.ai_family, addrlist);
2856 if (ret)
2857 goto error_return; /* message already logged */
2858
2859 /*
2860 * If random load balancing is enabled we shuffle the addresses.
2861 */
2863 {
2864 /*
2865 * This is the "inside-out" variant of the Fisher-Yates shuffle
2866 * algorithm. Notionally, we append each new value to the array
2867 * and then swap it with a randomly-chosen array element (possibly
2868 * including itself, else we fail to generate permutations with
2869 * the last integer last). The swap step can be optimized by
2870 * combining it with the insertion.
2871 *
2872 * We don't need to initialize conn->prng_state here, because that
2873 * already happened in pqConnectOptions2.
2874 */
2875 for (int i = 1; i < conn->naddr; i++)
2876 {
2877 int j = pg_prng_uint64_range(&conn->prng_state, 0, i);
2878 AddrInfo temp = conn->addr[j];
2879
2880 conn->addr[j] = conn->addr[i];
2881 conn->addr[i] = temp;
2882 }
2883 }
2884
2885 reset_connection_state_machine = true;
2886 conn->try_next_host = false;
2887 }
2888
2889 /* Reset connection state machine? */
2890 if (reset_connection_state_machine)
2891 {
2892 /*
2893 * (Re) initialize our connection control variables for a set of
2894 * connection attempts to a single server address. These variables
2895 * must persist across individual connection attempts, but we must
2896 * reset them when we start to consider a new server.
2897 */
2898 conn->pversion = PG_PROTOCOL(3, 0);
2899 conn->send_appname = true;
2903 reset_connection_state_machine = false;
2904 need_new_connection = true;
2905 }
2906
2907 /* Force a new connection (perhaps to the same server as before)? */
2908 if (need_new_connection)
2909 {
2910 /* Drop any existing connection */
2911 pqDropConnection(conn, true);
2912
2913 /* Reset all state obtained from old server */
2915
2916 /* Drop any PGresult we might have, too */
2921
2922 /* Reset conn->status to put the state machine in the right state */
2924
2925 need_new_connection = false;
2926 }
2927
2928 /*
2929 * Decide what to do next, if server rejects SSL or GSS negotiation, but
2930 * the connection is still valid. If there are no options left, error out
2931 * with 'msg'.
2932 */
2933#define ENCRYPTION_NEGOTIATION_FAILED(msg) \
2934 do { \
2935 switch (encryption_negotiation_failed(conn)) \
2936 { \
2937 case 0: \
2938 libpq_append_conn_error(conn, (msg)); \
2939 goto error_return; \
2940 case 1: \
2941 conn->status = CONNECTION_MADE; \
2942 return PGRES_POLLING_WRITING; \
2943 case 2: \
2944 need_new_connection = true; \
2945 goto keep_going; \
2946 } \
2947 } while(0);
2948
2949 /*
2950 * Decide what to do next, if connection fails. If there are no options
2951 * left, return with an error. The error message has already been written
2952 * to the connection's error buffer.
2953 */
2954#define CONNECTION_FAILED() \
2955 do { \
2956 if (connection_failed(conn)) \
2957 { \
2958 need_new_connection = true; \
2959 goto keep_going; \
2960 } \
2961 else \
2962 goto error_return; \
2963 } while(0);
2964
2965 /* Now try to advance the state machine for this connection */
2966 switch (conn->status)
2967 {
2968 case CONNECTION_NEEDED:
2969 {
2970 /*
2971 * Try to initiate a connection to one of the addresses
2972 * returned by pg_getaddrinfo_all(). conn->whichaddr is the
2973 * next one to try.
2974 *
2975 * The extra level of braces here is historical. It's not
2976 * worth reindenting this whole switch case to remove 'em.
2977 */
2978 {
2979 char host_addr[NI_MAXHOST];
2980 int sock_type;
2981 AddrInfo *addr_cur;
2982
2983 /*
2984 * Advance to next possible host, if we've tried all of
2985 * the addresses for the current host.
2986 */
2987 if (conn->whichaddr == conn->naddr)
2988 {
2989 conn->try_next_host = true;
2990 goto keep_going;
2991 }
2992 addr_cur = &conn->addr[conn->whichaddr];
2993
2994 /* Remember current address for possible use later */
2995 memcpy(&conn->raddr, &addr_cur->addr, sizeof(SockAddr));
2996
2997#ifdef ENABLE_GSS
2998
2999 /*
3000 * Before establishing the connection, check if it's
3001 * doomed to fail because gssencmode='require' but GSSAPI
3002 * is not available.
3003 */
3004 if (conn->gssencmode[0] == 'r')
3005 {
3006 if (conn->raddr.addr.ss_family == AF_UNIX)
3007 {
3009 "GSSAPI encryption required but it is not supported over a local socket");
3010 goto error_return;
3011 }
3012 if (conn->gcred == GSS_C_NO_CREDENTIAL)
3013 {
3014 if (!pg_GSS_have_cred_cache(&conn->gcred))
3015 {
3017 "GSSAPI encryption required but no credential cache");
3018 goto error_return;
3019 }
3020 }
3021 }
3022#endif
3023
3024 /*
3025 * Choose the encryption method to try first. Do this
3026 * before establishing the connection, so that if none of
3027 * the modes allowed by the connections options are
3028 * available, we can error out before establishing the
3029 * connection.
3030 */
3032 goto error_return;
3033
3034 /*
3035 * Set connip, too. Note we purposely ignore strdup
3036 * failure; not a big problem if it fails.
3037 */
3038 if (conn->connip != NULL)
3039 {
3040 free(conn->connip);
3041 conn->connip = NULL;
3042 }
3043 getHostaddr(conn, host_addr, NI_MAXHOST);
3044 if (host_addr[0])
3045 conn->connip = strdup(host_addr);
3046
3047 /* Try to create the socket */
3048 sock_type = SOCK_STREAM;
3049#ifdef SOCK_CLOEXEC
3050
3051 /*
3052 * Atomically mark close-on-exec, if possible on this
3053 * platform, so that there isn't a window where a
3054 * subprogram executed by another thread inherits the
3055 * socket. See fallback code below.
3056 */
3057 sock_type |= SOCK_CLOEXEC;
3058#endif
3059#ifdef SOCK_NONBLOCK
3060
3061 /*
3062 * We might as well skip a system call for nonblocking
3063 * mode too, if we can.
3064 */
3065 sock_type |= SOCK_NONBLOCK;
3066#endif
3067 conn->sock = socket(addr_cur->family, sock_type, 0);
3068 if (conn->sock == PGINVALID_SOCKET)
3069 {
3070 int errorno = SOCK_ERRNO;
3071
3072 /*
3073 * Silently ignore socket() failure if we have more
3074 * addresses to try; this reduces useless chatter in
3075 * cases where the address list includes both IPv4 and
3076 * IPv6 but kernel only accepts one family.
3077 */
3078 if (conn->whichaddr < conn->naddr ||
3079 conn->whichhost + 1 < conn->nconnhost)
3080 {
3081 conn->try_next_addr = true;
3082 goto keep_going;
3083 }
3084 emitHostIdentityInfo(conn, host_addr);
3085 libpq_append_conn_error(conn, "could not create socket: %s",
3086 SOCK_STRERROR(errorno, sebuf, sizeof(sebuf)));
3087 goto error_return;
3088 }
3089
3090 /*
3091 * Once we've identified a target address, all errors
3092 * except the preceding socket()-failure case should be
3093 * prefixed with host-identity information. (If the
3094 * connection succeeds, the contents of conn->errorMessage
3095 * won't matter, so this is harmless.)
3096 */
3097 emitHostIdentityInfo(conn, host_addr);
3098
3099 /*
3100 * Select socket options: no delay of outgoing data for
3101 * TCP sockets, nonblock mode, close-on-exec. Try the
3102 * next address if any of this fails.
3103 */
3104 if (addr_cur->family != AF_UNIX)
3105 {
3106 if (!connectNoDelay(conn))
3107 {
3108 /* error message already created */
3109 conn->try_next_addr = true;
3110 goto keep_going;
3111 }
3112 }
3113#ifndef SOCK_NONBLOCK
3114 if (!pg_set_noblock(conn->sock))
3115 {
3116 libpq_append_conn_error(conn, "could not set socket to nonblocking mode: %s",
3117 SOCK_STRERROR(SOCK_ERRNO, sebuf, sizeof(sebuf)));
3118 conn->try_next_addr = true;
3119 goto keep_going;
3120 }
3121#endif
3122
3123#ifndef SOCK_CLOEXEC
3124#ifdef F_SETFD
3125 if (fcntl(conn->sock, F_SETFD, FD_CLOEXEC) == -1)
3126 {
3127 libpq_append_conn_error(conn, "could not set socket to close-on-exec mode: %s",
3128 SOCK_STRERROR(SOCK_ERRNO, sebuf, sizeof(sebuf)));
3129 conn->try_next_addr = true;
3130 goto keep_going;
3131 }
3132#endif /* F_SETFD */
3133#endif
3134
3135 if (addr_cur->family != AF_UNIX)
3136 {
3137#ifndef WIN32
3138 int on = 1;
3139#endif
3140 int usekeepalives = useKeepalives(conn);
3141 int err = 0;
3142
3143 if (usekeepalives < 0)
3144 {
3145 /* error is already reported */
3146 err = 1;
3147 }
3148 else if (usekeepalives == 0)
3149 {
3150 /* Do nothing */
3151 }
3152#ifndef WIN32
3153 else if (setsockopt(conn->sock,
3154 SOL_SOCKET, SO_KEEPALIVE,
3155 (char *) &on, sizeof(on)) < 0)
3156 {
3157 libpq_append_conn_error(conn, "%s(%s) failed: %s",
3158 "setsockopt",
3159 "SO_KEEPALIVE",
3160 SOCK_STRERROR(SOCK_ERRNO, sebuf, sizeof(sebuf)));
3161 err = 1;
3162 }
3163 else if (!setKeepalivesIdle(conn)
3166 err = 1;
3167#else /* WIN32 */
3168#ifdef SIO_KEEPALIVE_VALS
3169 else if (!prepKeepalivesWin32(conn))
3170 err = 1;
3171#endif /* SIO_KEEPALIVE_VALS */
3172#endif /* WIN32 */
3173 else if (!setTCPUserTimeout(conn))
3174 err = 1;
3175
3176 if (err)
3177 {
3178 conn->try_next_addr = true;
3179 goto keep_going;
3180 }
3181 }
3182
3183 /*----------
3184 * We have three methods of blocking SIGPIPE during
3185 * send() calls to this socket:
3186 *
3187 * - setsockopt(sock, SO_NOSIGPIPE)
3188 * - send(sock, ..., MSG_NOSIGNAL)
3189 * - setting the signal mask to SIG_IGN during send()
3190 *
3191 * The third method requires three syscalls per send,
3192 * so we prefer either of the first two, but they are
3193 * less portable. The state is tracked in the following
3194 * members of PGconn:
3195 *
3196 * conn->sigpipe_so - we have set up SO_NOSIGPIPE
3197 * conn->sigpipe_flag - we're specifying MSG_NOSIGNAL
3198 *
3199 * If we can use SO_NOSIGPIPE, then set sigpipe_so here
3200 * and we're done. Otherwise, set sigpipe_flag so that
3201 * we will try MSG_NOSIGNAL on sends. If we get an error
3202 * with MSG_NOSIGNAL, we'll clear that flag and revert to
3203 * signal masking.
3204 *----------
3205 */
3206 conn->sigpipe_so = false;
3207#ifdef MSG_NOSIGNAL
3208 conn->sigpipe_flag = true;
3209#else
3210 conn->sigpipe_flag = false;
3211#endif /* MSG_NOSIGNAL */
3212
3213#ifdef SO_NOSIGPIPE
3214 optval = 1;
3215 if (setsockopt(conn->sock, SOL_SOCKET, SO_NOSIGPIPE,
3216 (char *) &optval, sizeof(optval)) == 0)
3217 {
3218 conn->sigpipe_so = true;
3219 conn->sigpipe_flag = false;
3220 }
3221#endif /* SO_NOSIGPIPE */
3222
3223 /*
3224 * Start/make connection. This should not block, since we
3225 * are in nonblock mode. If it does, well, too bad.
3226 */
3227 if (connect(conn->sock, (struct sockaddr *) &addr_cur->addr.addr,
3228 addr_cur->addr.salen) < 0)
3229 {
3230 if (SOCK_ERRNO == EINPROGRESS ||
3231#ifdef WIN32
3233#endif
3234 SOCK_ERRNO == EINTR)
3235 {
3236 /*
3237 * This is fine - we're in non-blocking mode, and
3238 * the connection is in progress. Tell caller to
3239 * wait for write-ready on socket.
3240 */
3242 return PGRES_POLLING_WRITING;
3243 }
3244 /* otherwise, trouble */
3245 }
3246 else
3247 {
3248 /*
3249 * Hm, we're connected already --- seems the "nonblock
3250 * connection" wasn't. Advance the state machine and
3251 * go do the next stuff.
3252 */
3254 goto keep_going;
3255 }
3256
3257 /*
3258 * This connection failed. Add the error report to
3259 * conn->errorMessage, then try the next address if any.
3260 */
3262 conn->try_next_addr = true;
3263 goto keep_going;
3264 }
3265 }
3266
3267 case CONNECTION_STARTED:
3268 {
3269 socklen_t optlen = sizeof(optval);
3270
3271 /*
3272 * Write ready, since we've made it here, so the connection
3273 * has been made ... or has failed.
3274 */
3275
3276 /*
3277 * Now check (using getsockopt) that there is not an error
3278 * state waiting for us on the socket.
3279 */
3280
3281 if (getsockopt(conn->sock, SOL_SOCKET, SO_ERROR,
3282 (char *) &optval, &optlen) == -1)
3283 {
3284 libpq_append_conn_error(conn, "could not get socket error status: %s",
3285 SOCK_STRERROR(SOCK_ERRNO, sebuf, sizeof(sebuf)));
3286 goto error_return;
3287 }
3288 else if (optval != 0)
3289 {
3290 /*
3291 * When using a nonblocking connect, we will typically see
3292 * connect failures at this point, so provide a friendly
3293 * error message.
3294 */
3295 connectFailureMessage(conn, optval);
3296
3297 /*
3298 * Try the next address if any, just as in the case where
3299 * connect() returned failure immediately.
3300 */
3301 conn->try_next_addr = true;
3302 goto keep_going;
3303 }
3304
3305 /* Fill in the client address */
3306 conn->laddr.salen = sizeof(conn->laddr.addr);
3307 if (getsockname(conn->sock,
3308 (struct sockaddr *) &conn->laddr.addr,
3309 &conn->laddr.salen) < 0)
3310 {
3311 libpq_append_conn_error(conn, "could not get client address from socket: %s",
3312 SOCK_STRERROR(SOCK_ERRNO, sebuf, sizeof(sebuf)));
3313 goto error_return;
3314 }
3315
3316 /*
3317 * Implement requirepeer check, if requested and it's a
3318 * Unix-domain socket.
3319 */
3320 if (conn->requirepeer && conn->requirepeer[0] &&
3321 conn->raddr.addr.ss_family == AF_UNIX)
3322 {
3323#ifndef WIN32
3324 char *remote_username;
3325#endif
3326 uid_t uid;
3327 gid_t gid;
3328
3329 errno = 0;
3330 if (getpeereid(conn->sock, &uid, &gid) != 0)
3331 {
3332 /*
3333 * Provide special error message if getpeereid is a
3334 * stub
3335 */
3336 if (errno == ENOSYS)
3337 libpq_append_conn_error(conn, "requirepeer parameter is not supported on this platform");
3338 else
3339 libpq_append_conn_error(conn, "could not get peer credentials: %s",
3340 strerror_r(errno, sebuf, sizeof(sebuf)));
3341 goto error_return;
3342 }
3343
3344#ifndef WIN32
3345 remote_username = pg_fe_getusername(uid,
3346 &conn->errorMessage);
3347 if (remote_username == NULL)
3348 goto error_return; /* message already logged */
3349
3350 if (strcmp(remote_username, conn->requirepeer) != 0)
3351 {
3352 libpq_append_conn_error(conn, "requirepeer specifies \"%s\", but actual peer user name is \"%s\"",
3353 conn->requirepeer, remote_username);
3354 free(remote_username);
3355 goto error_return;
3356 }
3357 free(remote_username);
3358#else /* WIN32 */
3359 /* should have failed with ENOSYS above */
3360 Assert(false);
3361#endif /* WIN32 */
3362 }
3363
3364 /*
3365 * Make sure we can write before advancing to next step.
3366 */
3368 return PGRES_POLLING_WRITING;
3369 }
3370
3371 case CONNECTION_MADE:
3372 {
3373 char *startpacket;
3374 int packetlen;
3375
3376#ifdef ENABLE_GSS
3377
3378 /*
3379 * If GSSAPI encryption is enabled, send a packet to the
3380 * server asking for GSSAPI Encryption and proceed with GSSAPI
3381 * handshake. We will come back here after GSSAPI encryption
3382 * has been established, with conn->gctx set.
3383 */
3384 if (conn->current_enc_method == ENC_GSSAPI && !conn->gctx)
3385 {
3387
3388 if (pqPacketSend(conn, 0, &pv, sizeof(pv)) != STATUS_OK)
3389 {
3390 libpq_append_conn_error(conn, "could not send GSSAPI negotiation packet: %s",
3391 SOCK_STRERROR(SOCK_ERRNO, sebuf, sizeof(sebuf)));
3392 goto error_return;
3393 }
3394
3395 /* Ok, wait for response */
3397 return PGRES_POLLING_READING;
3398 }
3399#endif
3400
3401#ifdef USE_SSL
3402
3403 /*
3404 * If SSL is enabled, start the SSL negotiation. We will come
3405 * back here after SSL encryption has been established, with
3406 * ssl_in_use set.
3407 */
3409 {
3410 /*
3411 * If traditional postgres SSL negotiation is used, send
3412 * the SSL request. In direct negotiation, jump straight
3413 * into the SSL handshake.
3414 */
3415 if (conn->sslnegotiation[0] == 'p')
3416 {
3417 ProtocolVersion pv;
3418
3419 /*
3420 * Send the SSL request packet.
3421 *
3422 * Theoretically, this could block, but it really
3423 * shouldn't since we only got here if the socket is
3424 * write-ready.
3425 */
3427 if (pqPacketSend(conn, 0, &pv, sizeof(pv)) != STATUS_OK)
3428 {
3429 libpq_append_conn_error(conn, "could not send SSL negotiation packet: %s",
3430 SOCK_STRERROR(SOCK_ERRNO, sebuf, sizeof(sebuf)));
3431 goto error_return;
3432 }
3433 /* Ok, wait for response */
3435 return PGRES_POLLING_READING;
3436 }
3437 else
3438 {
3439 Assert(conn->sslnegotiation[0] == 'd');
3441 return PGRES_POLLING_WRITING;
3442 }
3443 }
3444#endif /* USE_SSL */
3445
3446 /*
3447 * For cancel requests this is as far as we need to go in the
3448 * connection establishment. Now we can actually send our
3449 * cancellation request.
3450 */
3451 if (conn->cancelRequest)
3452 {
3453 CancelRequestPacket cancelpacket;
3454
3455 packetlen = sizeof(cancelpacket);
3457 cancelpacket.backendPID = pg_hton32(conn->be_pid);
3458 cancelpacket.cancelAuthCode = pg_hton32(conn->be_key);
3459 if (pqPacketSend(conn, 0, &cancelpacket, packetlen) != STATUS_OK)
3460 {
3461 libpq_append_conn_error(conn, "could not send cancel packet: %s",
3462 SOCK_STRERROR(SOCK_ERRNO, sebuf, sizeof(sebuf)));
3463 goto error_return;
3464 }
3466 return PGRES_POLLING_READING;
3467 }
3468
3469 /*
3470 * We have now established encryption, or we are happy to
3471 * proceed without.
3472 */
3473
3474 /* Build the startup packet. */
3475 startpacket = pqBuildStartupPacket3(conn, &packetlen,
3477 if (!startpacket)
3478 {
3479 libpq_append_conn_error(conn, "out of memory");
3480 goto error_return;
3481 }
3482
3483 /*
3484 * Send the startup packet.
3485 *
3486 * Theoretically, this could block, but it really shouldn't
3487 * since we only got here if the socket is write-ready.
3488 */
3489 if (pqPacketSend(conn, 0, startpacket, packetlen) != STATUS_OK)
3490 {
3491 libpq_append_conn_error(conn, "could not send startup packet: %s",
3492 SOCK_STRERROR(SOCK_ERRNO, sebuf, sizeof(sebuf)));
3493 free(startpacket);
3494 goto error_return;
3495 }
3496
3497 free(startpacket);
3498
3500 return PGRES_POLLING_READING;
3501 }
3502
3503 /*
3504 * Handle SSL negotiation: wait for postmaster messages and
3505 * respond as necessary.
3506 */
3508 {
3509#ifdef USE_SSL
3511
3512 /*
3513 * On first time through with traditional SSL negotiation, get
3514 * the postmaster's response to our SSLRequest packet. With
3515 * sslnegotiation='direct', go straight to initiating SSL.
3516 */
3517 if (!conn->ssl_in_use && conn->sslnegotiation[0] == 'p')
3518 {
3519 /*
3520 * We use pqReadData here since it has the logic to
3521 * distinguish no-data-yet from connection closure. Since
3522 * conn->ssl isn't set, a plain recv() will occur.
3523 */
3524 char SSLok;
3525 int rdresult;
3526
3527 rdresult = pqReadData(conn);
3528 if (rdresult < 0)
3529 {
3530 /* errorMessage is already filled in */
3531 goto error_return;
3532 }
3533 if (rdresult == 0)
3534 {
3535 /* caller failed to wait for data */
3536 return PGRES_POLLING_READING;
3537 }
3538 if (pqGetc(&SSLok, conn) < 0)
3539 {
3540 /* should not happen really */
3541 return PGRES_POLLING_READING;
3542 }
3543 if (SSLok == 'S')
3544 {
3545 if (conn->Pfdebug)
3546 pqTraceOutputCharResponse(conn, "SSLResponse",
3547 SSLok);
3548 /* mark byte consumed */
3550 }
3551 else if (SSLok == 'N')
3552 {
3553 if (conn->Pfdebug)
3554 pqTraceOutputCharResponse(conn, "SSLResponse",
3555 SSLok);
3556 /* mark byte consumed */
3558
3559 /*
3560 * The connection is still valid, so if it's OK to
3561 * continue without SSL, we can proceed using this
3562 * connection. Otherwise return with an error.
3563 */
3564 ENCRYPTION_NEGOTIATION_FAILED(libpq_gettext("server does not support SSL, but SSL was required"));
3565 }
3566 else if (SSLok == 'E')
3567 {
3568 /*
3569 * Server failure of some sort, such as failure to
3570 * fork a backend process. Don't bother retrieving
3571 * the error message; we should not trust it as the
3572 * server has not been authenticated yet.
3573 */
3574 libpq_append_conn_error(conn, "server sent an error response during SSL exchange");
3575 goto error_return;
3576 }
3577 else
3578 {
3579 libpq_append_conn_error(conn, "received invalid response to SSL negotiation: %c",
3580 SSLok);
3581 goto error_return;
3582 }
3583 }
3584
3585 /*
3586 * Begin or continue the SSL negotiation process.
3587 */
3588 pollres = pqsecure_open_client(conn);
3589 if (pollres == PGRES_POLLING_OK)
3590 {
3591 /*
3592 * At this point we should have no data already buffered.
3593 * If we do, it was received before we performed the SSL
3594 * handshake, so it wasn't encrypted and indeed may have
3595 * been injected by a man-in-the-middle.
3596 */
3597 if (conn->inCursor != conn->inEnd)
3598 {
3599 libpq_append_conn_error(conn, "received unencrypted data after SSL response");
3600 goto error_return;
3601 }
3602
3603 /* SSL handshake done, ready to send startup packet */
3605 return PGRES_POLLING_WRITING;
3606 }
3607 if (pollres == PGRES_POLLING_FAILED)
3608 {
3609 /*
3610 * SSL handshake failed. We will retry with a plaintext
3611 * connection, if permitted by sslmode.
3612 */
3614 }
3615 /* Else, return POLLING_READING or POLLING_WRITING status */
3616 return pollres;
3617#else /* !USE_SSL */
3618 /* can't get here */
3619 goto error_return;
3620#endif /* USE_SSL */
3621 }
3622
3624 {
3625#ifdef ENABLE_GSS
3627
3628 /*
3629 * If we haven't yet, get the postmaster's response to our
3630 * negotiation packet
3631 */
3632 if (!conn->gctx)
3633 {
3634 char gss_ok;
3635 int rdresult = pqReadData(conn);
3636
3637 if (rdresult < 0)
3638 /* pqReadData fills in error message */
3639 goto error_return;
3640 else if (rdresult == 0)
3641 /* caller failed to wait for data */
3642 return PGRES_POLLING_READING;
3643 if (pqGetc(&gss_ok, conn) < 0)
3644 /* shouldn't happen... */
3645 return PGRES_POLLING_READING;
3646
3647 if (gss_ok == 'E')
3648 {
3649 /*
3650 * Server failure of some sort, possibly protocol
3651 * version support failure. Don't bother retrieving
3652 * the error message; we should not trust it anyway as
3653 * the server has not authenticated yet.
3654 *
3655 * Note that unlike on an error response to
3656 * SSLRequest, we allow falling back to SSL or
3657 * plaintext connection here. GSS support was
3658 * introduced in PostgreSQL version 12, so an error
3659 * response might mean that we are connecting to a
3660 * pre-v12 server.
3661 */
3662 libpq_append_conn_error(conn, "server sent an error response during GSS encryption exchange");
3664 }
3665
3666 /* mark byte consumed */
3668
3669 if (gss_ok == 'N')
3670 {
3671 if (conn->Pfdebug)
3672 pqTraceOutputCharResponse(conn, "GSSENCResponse",
3673 gss_ok);
3674
3675 /*
3676 * The connection is still valid, so if it's OK to
3677 * continue without GSS, we can proceed using this
3678 * connection. Otherwise return with an error.
3679 */
3680 ENCRYPTION_NEGOTIATION_FAILED(libpq_gettext("server doesn't support GSSAPI encryption, but it was required"));
3681 }
3682 else if (gss_ok != 'G')
3683 {
3684 libpq_append_conn_error(conn, "received invalid response to GSSAPI negotiation: %c",
3685 gss_ok);
3686 goto error_return;
3687 }
3688
3689 if (conn->Pfdebug)
3690 pqTraceOutputCharResponse(conn, "GSSENCResponse",
3691 gss_ok);
3692 }
3693
3694 /* Begin or continue GSSAPI negotiation */
3695 pollres = pqsecure_open_gss(conn);
3696 if (pollres == PGRES_POLLING_OK)
3697 {
3698 /*
3699 * At this point we should have no data already buffered.
3700 * If we do, it was received before we performed the GSS
3701 * handshake, so it wasn't encrypted and indeed may have
3702 * been injected by a man-in-the-middle.
3703 */
3704 if (conn->inCursor != conn->inEnd)
3705 {
3706 libpq_append_conn_error(conn, "received unencrypted data after GSSAPI encryption response");
3707 goto error_return;
3708 }
3709
3710 /* All set for startup packet */
3712 return PGRES_POLLING_WRITING;
3713 }
3714 else if (pollres == PGRES_POLLING_FAILED)
3715 {
3716 /*
3717 * GSS handshake failed. We will retry with an SSL or
3718 * plaintext connection, if permitted by the options.
3719 */
3721 }
3722 /* Else, return POLLING_READING or POLLING_WRITING status */
3723 return pollres;
3724#else /* !ENABLE_GSS */
3725 /* unreachable */
3726 goto error_return;
3727#endif /* ENABLE_GSS */
3728 }
3729
3730 /*
3731 * Handle authentication exchange: wait for postmaster messages
3732 * and respond as necessary.
3733 */
3735 {
3736 char beresp;
3737 int msgLength;
3738 int avail;
3739 AuthRequest areq;
3740 int res;
3741
3742 /*
3743 * Scan the message from current point (note that if we find
3744 * the message is incomplete, we will return without advancing
3745 * inStart, and resume here next time).
3746 */
3748
3749 /* Read type byte */
3750 if (pqGetc(&beresp, conn))
3751 {
3752 /* We'll come back when there is more data */
3753 return PGRES_POLLING_READING;
3754 }
3755
3756 /*
3757 * Validate message type: we expect only an authentication
3758 * request, NegotiateProtocolVersion, or an error here.
3759 * Anything else probably means it's not Postgres on the other
3760 * end at all.
3761 */
3762 if (beresp != PqMsg_AuthenticationRequest &&
3763 beresp != PqMsg_ErrorResponse &&
3765 {
3766 libpq_append_conn_error(conn, "expected authentication request from server, but received %c",
3767 beresp);
3768 goto error_return;
3769 }
3770
3771 /* Read message length word */
3772 if (pqGetInt(&msgLength, 4, conn))
3773 {
3774 /* We'll come back when there is more data */
3775 return PGRES_POLLING_READING;
3776 }
3777
3778 /*
3779 * Try to validate message length before using it.
3780 *
3781 * Authentication requests can't be very large, although GSS
3782 * auth requests may not be that small. Same for
3783 * NegotiateProtocolVersion.
3784 *
3785 * Errors can be a little larger, but not huge. If we see a
3786 * large apparent length in an error, it means we're really
3787 * talking to a pre-3.0-protocol server; cope. (Before
3788 * version 14, the server also used the old protocol for
3789 * errors that happened before processing the startup packet.)
3790 */
3791 if (beresp == PqMsg_AuthenticationRequest &&
3792 (msgLength < 8 || msgLength > 2000))
3793 {
3794 libpq_append_conn_error(conn, "received invalid authentication request");
3795 goto error_return;
3796 }
3797 if (beresp == PqMsg_NegotiateProtocolVersion &&
3798 (msgLength < 8 || msgLength > 2000))
3799 {
3800 libpq_append_conn_error(conn, "received invalid protocol negotiation message");
3801 goto error_return;
3802 }
3803
3804#define MAX_ERRLEN 30000
3805 if (beresp == PqMsg_ErrorResponse &&
3806 (msgLength < 8 || msgLength > MAX_ERRLEN))
3807 {
3808 /* Handle error from a pre-3.0 server */
3809 conn->inCursor = conn->inStart + 1; /* reread data */
3811 {
3812 /*
3813 * We may not have authenticated the server yet, so
3814 * don't let the buffer grow forever.
3815 */
3816 avail = conn->inEnd - conn->inCursor;
3817 if (avail > MAX_ERRLEN)
3818 {
3819 libpq_append_conn_error(conn, "received invalid error message");
3820 goto error_return;
3821 }
3822
3823 /* We'll come back when there is more data */
3824 return PGRES_POLLING_READING;
3825 }
3826 /* OK, we read the message; mark data consumed */
3828
3829 /*
3830 * Before 7.2, the postmaster didn't always end its
3831 * messages with a newline, so add one if needed to
3832 * conform to libpq conventions.
3833 */
3834 if (conn->errorMessage.len == 0 ||
3835 conn->errorMessage.data[conn->errorMessage.len - 1] != '\n')
3836 {
3838 }
3839
3840 goto error_return;
3841 }
3842#undef MAX_ERRLEN
3843
3844 /*
3845 * Can't process if message body isn't all here yet.
3846 *
3847 * After this check passes, any further EOF during parsing
3848 * implies that the server sent a bad/truncated message.
3849 * Reading more bytes won't help in that case, so don't return
3850 * PGRES_POLLING_READING after this point.
3851 */
3852 msgLength -= 4;
3853 avail = conn->inEnd - conn->inCursor;
3854 if (avail < msgLength)
3855 {
3856 /*
3857 * Before returning, try to enlarge the input buffer if
3858 * needed to hold the whole message; see notes in
3859 * pqParseInput3.
3860 */
3861 if (pqCheckInBufferSpace(conn->inCursor + (size_t) msgLength,
3862 conn))
3863 goto error_return;
3864 /* We'll come back when there is more data */
3865 return PGRES_POLLING_READING;
3866 }
3867
3868 /* Handle errors. */
3869 if (beresp == PqMsg_ErrorResponse)
3870 {
3871 if (pqGetErrorNotice3(conn, true))
3872 {
3873 libpq_append_conn_error(conn, "received invalid error message");
3874 goto error_return;
3875 }
3876 /* OK, we read the message; mark data consumed */
3878
3879 /*
3880 * If error is "cannot connect now", try the next host if
3881 * any (but we don't want to consider additional addresses
3882 * for this host, nor is there much point in changing SSL
3883 * or GSS mode). This is helpful when dealing with
3884 * standby servers that might not be in hot-standby state.
3885 */
3886 if (strcmp(conn->last_sqlstate,
3888 {
3889 conn->try_next_host = true;
3890 goto keep_going;
3891 }
3892
3893 /* Check to see if we should mention pgpassfile */
3895
3897 }
3898 else if (beresp == PqMsg_NegotiateProtocolVersion)
3899 {
3901 {
3902 libpq_append_conn_error(conn, "received invalid protocol negotiation message");
3903 goto error_return;
3904 }
3905 /* OK, we read the message; mark data consumed */
3907 goto error_return;
3908 }
3909
3910 /* It is an authentication request. */
3911 conn->auth_req_received = true;
3912
3913 /* Get the type of request. */
3914 if (pqGetInt((int *) &areq, 4, conn))
3915 {
3916 /* can't happen because we checked the length already */
3917 libpq_append_conn_error(conn, "received invalid authentication request");
3918 goto error_return;
3919 }
3920 msgLength -= 4;
3921
3922 /*
3923 * Process the rest of the authentication request message, and
3924 * respond to it if necessary.
3925 *
3926 * Note that conn->pghost must be non-NULL if we are going to
3927 * avoid the Kerberos code doing a hostname look-up.
3928 */
3929 res = pg_fe_sendauth(areq, msgLength, conn);
3930
3931 /*
3932 * OK, we have processed the message; mark data consumed. We
3933 * don't call pqParseDone here because we already traced this
3934 * message inside pg_fe_sendauth.
3935 */
3937
3938 if (res != STATUS_OK)
3939 goto error_return;
3940
3941 /*
3942 * Just make sure that any data sent by pg_fe_sendauth is
3943 * flushed out. Although this theoretically could block, it
3944 * really shouldn't since we don't send large auth responses.
3945 */
3946 if (pqFlush(conn))
3947 goto error_return;
3948
3949 if (areq == AUTH_REQ_OK)
3950 {
3951 /* We are done with authentication exchange */
3953
3954 /*
3955 * Set asyncStatus so that PQgetResult will think that
3956 * what comes back next is the result of a query. See
3957 * below.
3958 */
3960 }
3961
3962 /* Look to see if we have more data yet. */
3963 goto keep_going;
3964 }
3965
3966 case CONNECTION_AUTH_OK:
3967 {
3968 /*
3969 * Now we expect to hear from the backend. A ReadyForQuery
3970 * message indicates that startup is successful, but we might
3971 * also get an Error message indicating failure. (Notice
3972 * messages indicating nonfatal warnings are also allowed by
3973 * the protocol, as are ParameterStatus and BackendKeyData
3974 * messages.) Easiest way to handle this is to let
3975 * PQgetResult() read the messages. We just have to fake it
3976 * out about the state of the connection, by setting
3977 * asyncStatus = PGASYNC_BUSY (done above).
3978 */
3979
3980 if (PQisBusy(conn))
3981 return PGRES_POLLING_READING;
3982
3983 res = PQgetResult(conn);
3984
3985 /*
3986 * NULL return indicating we have gone to IDLE state is
3987 * expected
3988 */
3989 if (res)
3990 {
3992 libpq_append_conn_error(conn, "unexpected message from server during startup");
3993 else if (conn->send_appname &&
3994 (conn->appname || conn->fbappname))
3995 {
3996 /*
3997 * If we tried to send application_name, check to see
3998 * if the error is about that --- pre-9.0 servers will
3999 * reject it at this stage of the process. If so,
4000 * close the connection and retry without sending
4001 * application_name. We could possibly get a false
4002 * SQLSTATE match here and retry uselessly, but there
4003 * seems no great harm in that; we'll just get the
4004 * same error again if it's unrelated.
4005 */
4006 const char *sqlstate;
4007
4009 if (sqlstate &&
4010 strcmp(sqlstate, ERRCODE_APPNAME_UNKNOWN) == 0)
4011 {
4012 PQclear(res);
4013 conn->send_appname = false;
4014 need_new_connection = true;
4015 goto keep_going;
4016 }
4017 }
4018
4019 /*
4020 * if the resultStatus is FATAL, then conn->errorMessage
4021 * already has a copy of the error; needn't copy it back.
4022 * But add a newline if it's not there already, since
4023 * postmaster error messages may not have one.
4024 */
4025 if (conn->errorMessage.len <= 0 ||
4026 conn->errorMessage.data[conn->errorMessage.len - 1] != '\n')
4028 PQclear(res);
4029 goto error_return;
4030 }
4031
4032 /* Almost there now ... */
4034 goto keep_going;
4035 }
4036
4038 {
4039 /*
4040 * If a read-write, read-only, primary, or standby connection
4041 * is required, see if we have one.
4042 */
4045 {
4046 bool read_only_server;
4047
4048 /*
4049 * If the server didn't report
4050 * "default_transaction_read_only" or "in_hot_standby" at
4051 * startup, we must determine its state by sending the
4052 * query "SHOW transaction_read_only". This GUC exists in
4053 * all server versions that support 3.0 protocol.
4054 */
4057 {
4058 /*
4059 * We use PQsendQueryContinue so that
4060 * conn->errorMessage does not get cleared. We need
4061 * to preserve any error messages related to previous
4062 * hosts we have tried and failed to connect to.
4063 */
4066 "SHOW transaction_read_only"))
4067 goto error_return;
4068 /* We'll return to this state when we have the answer */
4070 return PGRES_POLLING_READING;
4071 }
4072
4073 /* OK, we can make the test */
4074 read_only_server =
4077
4079 read_only_server : !read_only_server)
4080 {
4081 /* Wrong server state, reject and try the next host */
4083 libpq_append_conn_error(conn, "session is read-only");
4084 else
4085 libpq_append_conn_error(conn, "session is not read-only");
4086
4087 /* Close connection politely. */
4090
4091 /*
4092 * Try next host if any, but we don't want to consider
4093 * additional addresses for this host.
4094 */
4095 conn->try_next_host = true;
4096 goto keep_going;
4097 }
4098 }
4102 {
4103 /*
4104 * If the server didn't report "in_hot_standby" at
4105 * startup, we must determine its state by sending the
4106 * query "SELECT pg_catalog.pg_is_in_recovery()". Servers
4107 * before 9.0 don't have that function, but by the same
4108 * token they don't have any standby mode, so we may just
4109 * assume the result.
4110 */
4111 if (conn->sversion < 90000)
4113
4115 {
4116 /*
4117 * We use PQsendQueryContinue so that
4118 * conn->errorMessage does not get cleared. We need
4119 * to preserve any error messages related to previous
4120 * hosts we have tried and failed to connect to.
4121 */
4124 "SELECT pg_catalog.pg_is_in_recovery()"</