LCOV - differential code coverage report
Current view: top level - src/interfaces/libpq - fe-connect.c (source / functions) Coverage Total Hit UNC UBC GBC GNC CBC DUB DCB
Current: 7a15cff1f11193467898da1c1fabf06fd2caee04 vs 84a3778c79c2d28b4dc281d03ef2ab019b16483b Lines: 67.1 % 2810 1886 12 912 3 25 1858 1 3
Current Date: 2025-12-15 18:36:29 -0500 Functions: 90.9 % 110 100 10 7 93 1
Baseline: lcov-20251216-010103-baseline Branches: 60.4 % 2065 1248 5 812 1 17 1230
Baseline Date: 2025-12-15 13:30:48 -0800 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(1,7] days: 0.0 % 1 0 1
(30,360] days: 79.8 % 228 182 11 35 25 157
(360..) days: 66.0 % 2581 1704 877 3 1701
Function coverage date bins:
(30,360] days: 100.0 % 5 5 1 4
(360..) days: 90.5 % 105 95 10 6 89
Branch coverage date bins:
(30,360] days: 74.6 % 138 103 5 30 17 86
(360..) days: 59.4 % 1927 1145 782 1 1144

 Age         Owner                    Branch data    TLA  Line data    Source code
                                  1                 :                : /*-------------------------------------------------------------------------
                                  2                 :                :  *
                                  3                 :                :  * fe-connect.c
                                  4                 :                :  *    functions related to setting up a connection to the backend
                                  5                 :                :  *
                                  6                 :                :  * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
                                  7                 :                :  * Portions Copyright (c) 1994, Regents of the University of California
                                  8                 :                :  *
                                  9                 :                :  *
                                 10                 :                :  * IDENTIFICATION
                                 11                 :                :  *    src/interfaces/libpq/fe-connect.c
                                 12                 :                :  *
                                 13                 :                :  *-------------------------------------------------------------------------
                                 14                 :                :  */
                                 15                 :                : 
                                 16                 :                : #include "postgres_fe.h"
                                 17                 :                : 
                                 18                 :                : #include <sys/stat.h>
                                 19                 :                : #include <fcntl.h>
                                 20                 :                : #include <ctype.h>
                                 21                 :                : #include <limits.h>
                                 22                 :                : #include <netdb.h>
                                 23                 :                : #include <time.h>
                                 24                 :                : #include <unistd.h>
                                 25                 :                : 
                                 26                 :                : #include "common/base64.h"
                                 27                 :                : #include "common/ip.h"
                                 28                 :                : #include "common/link-canary.h"
                                 29                 :                : #include "common/scram-common.h"
                                 30                 :                : #include "common/string.h"
                                 31                 :                : #include "fe-auth.h"
                                 32                 :                : #include "fe-auth-oauth.h"
                                 33                 :                : #include "libpq-fe.h"
                                 34                 :                : #include "libpq-int.h"
                                 35                 :                : #include "mb/pg_wchar.h"
                                 36                 :                : #include "pg_config_paths.h"
                                 37                 :                : #include "port/pg_bswap.h"
                                 38                 :                : 
                                 39                 :                : #ifdef WIN32
                                 40                 :                : #include "win32.h"
                                 41                 :                : #ifdef _WIN32_IE
                                 42                 :                : #undef _WIN32_IE
                                 43                 :                : #endif
                                 44                 :                : #define _WIN32_IE 0x0500
                                 45                 :                : #ifdef near
                                 46                 :                : #undef near
                                 47                 :                : #endif
                                 48                 :                : #define near
                                 49                 :                : #include <shlobj.h>
                                 50                 :                : #include <mstcpip.h>
                                 51                 :                : #else
                                 52                 :                : #include <sys/socket.h>
                                 53                 :                : #include <netdb.h>
                                 54                 :                : #include <netinet/in.h>
                                 55                 :                : #include <netinet/tcp.h>
                                 56                 :                : #include <pwd.h>
                                 57                 :                : #endif
                                 58                 :                : 
                                 59                 :                : #ifdef WIN32
                                 60                 :                : #include "pthread-win32.h"
                                 61                 :                : #else
                                 62                 :                : #include <pthread.h>
                                 63                 :                : #endif
                                 64                 :                : 
                                 65                 :                : #ifdef USE_LDAP
                                 66                 :                : #ifdef WIN32
                                 67                 :                : #include <winldap.h>
                                 68                 :                : #else
                                 69                 :                : /* OpenLDAP deprecates RFC 1823, but we want standard conformance */
                                 70                 :                : #define LDAP_DEPRECATED 1
                                 71                 :                : #include <ldap.h>
                                 72                 :                : typedef struct timeval LDAP_TIMEVAL;
                                 73                 :                : #endif
                                 74                 :                : static int  ldapServiceLookup(const char *purl, PQconninfoOption *options,
                                 75                 :                :                               PQExpBuffer errorMessage);
                                 76                 :                : #endif
                                 77                 :                : 
                                 78                 :                : #ifndef WIN32
                                 79                 :                : #define PGPASSFILE ".pgpass"
                                 80                 :                : #else
                                 81                 :                : #define PGPASSFILE "pgpass.conf"
                                 82                 :                : #endif
                                 83                 :                : 
                                 84                 :                : /*
                                 85                 :                :  * Pre-9.0 servers will return this SQLSTATE if asked to set
                                 86                 :                :  * application_name in a startup packet.  We hard-wire the value rather
                                 87                 :                :  * than looking into errcodes.h since it reflects historical behavior
                                 88                 :                :  * rather than that of the current code.
                                 89                 :                :  */
                                 90                 :                : #define ERRCODE_APPNAME_UNKNOWN "42704"
                                 91                 :                : 
                                 92                 :                : /* This is part of the protocol so just define it */
                                 93                 :                : #define ERRCODE_INVALID_PASSWORD "28P01"
                                 94                 :                : /* This too */
                                 95                 :                : #define ERRCODE_CANNOT_CONNECT_NOW "57P03"
                                 96                 :                : 
                                 97                 :                : /*
                                 98                 :                :  * Cope with the various platform-specific ways to spell TCP keepalive socket
                                 99                 :                :  * options.  This doesn't cover Windows, which as usual does its own thing.
                                100                 :                :  */
                                101                 :                : #if defined(TCP_KEEPIDLE)
                                102                 :                : /* TCP_KEEPIDLE is the name of this option on Linux and *BSD */
                                103                 :                : #define PG_TCP_KEEPALIVE_IDLE TCP_KEEPIDLE
                                104                 :                : #define PG_TCP_KEEPALIVE_IDLE_STR "TCP_KEEPIDLE"
                                105                 :                : #elif defined(TCP_KEEPALIVE_THRESHOLD)
                                106                 :                : /* TCP_KEEPALIVE_THRESHOLD is the name of this option on Solaris >= 11 */
                                107                 :                : #define PG_TCP_KEEPALIVE_IDLE TCP_KEEPALIVE_THRESHOLD
                                108                 :                : #define PG_TCP_KEEPALIVE_IDLE_STR "TCP_KEEPALIVE_THRESHOLD"
                                109                 :                : #elif defined(TCP_KEEPALIVE) && defined(__darwin__)
                                110                 :                : /* TCP_KEEPALIVE is the name of this option on macOS */
                                111                 :                : /* Caution: Solaris has this symbol but it means something different */
                                112                 :                : #define PG_TCP_KEEPALIVE_IDLE TCP_KEEPALIVE
                                113                 :                : #define PG_TCP_KEEPALIVE_IDLE_STR "TCP_KEEPALIVE"
                                114                 :                : #endif
                                115                 :                : 
                                116                 :                : /*
                                117                 :                :  * fall back options if they are not specified by arguments or defined
                                118                 :                :  * by environment variables
                                119                 :                :  */
                                120                 :                : #define DefaultHost     "localhost"
                                121                 :                : #define DefaultOption   ""
                                122                 :                : #ifdef USE_SSL
                                123                 :                : #define DefaultChannelBinding   "prefer"
                                124                 :                : #else
                                125                 :                : #define DefaultChannelBinding   "disable"
                                126                 :                : #endif
                                127                 :                : #define DefaultTargetSessionAttrs   "any"
                                128                 :                : #define DefaultLoadBalanceHosts "disable"
                                129                 :                : #ifdef USE_SSL
                                130                 :                : #define DefaultSSLMode "prefer"
                                131                 :                : #define DefaultSSLCertMode "allow"
                                132                 :                : #else
                                133                 :                : #define DefaultSSLMode  "disable"
                                134                 :                : #define DefaultSSLCertMode "disable"
                                135                 :                : #endif
                                136                 :                : #define DefaultSSLNegotiation   "postgres"
                                137                 :                : #ifdef ENABLE_GSS
                                138                 :                : #include "fe-gssapi-common.h"
                                139                 :                : #define DefaultGSSMode "prefer"
                                140                 :                : #else
                                141                 :                : #define DefaultGSSMode "disable"
                                142                 :                : #endif
                                143                 :                : 
                                144                 :                : /* ----------
                                145                 :                :  * Definition of the conninfo parameters and their fallback resources.
                                146                 :                :  *
                                147                 :                :  * If Environment-Var and Compiled-in are specified as NULL, no
                                148                 :                :  * fallback is available. If after all no value can be determined
                                149                 :                :  * for an option, an error is returned.
                                150                 :                :  *
                                151                 :                :  * The value for the username is treated specially in conninfo_add_defaults.
                                152                 :                :  * If the value is not obtained any other way, the username is determined
                                153                 :                :  * by pg_fe_getauthname().
                                154                 :                :  *
                                155                 :                :  * The Label and Disp-Char entries are provided for applications that
                                156                 :                :  * want to use PQconndefaults() to create a generic database connection
                                157                 :                :  * dialog. Disp-Char is defined as follows:
                                158                 :                :  *      ""        Normal input field
                                159                 :                :  *      "*"       Password field - hide value
                                160                 :                :  *      "D"       Debug option - don't show by default
                                161                 :                :  *
                                162                 :                :  * NB: Server-side clients -- dblink, postgres_fdw, libpqrcv -- use dispchar to
                                163                 :                :  * determine which options to expose to end users, and how. Changing dispchar
                                164                 :                :  * has compatibility and security implications for those clients. For example,
                                165                 :                :  * postgres_fdw will attach a "*" option to USER MAPPING instead of the default
                                166                 :                :  * SERVER, and it disallows setting "D" options entirely.
                                167                 :                :  *
                                168                 :                :  * PQconninfoOptions[] is a constant static array that we use to initialize
                                169                 :                :  * a dynamically allocated working copy.  All the "val" fields in
                                170                 :                :  * PQconninfoOptions[] *must* be NULL.  In a working copy, non-null "val"
                                171                 :                :  * fields point to malloc'd strings that should be freed when the working
                                172                 :                :  * array is freed (see PQconninfoFree).
                                173                 :                :  *
                                174                 :                :  * The first part of each struct is identical to the one in libpq-fe.h,
                                175                 :                :  * which is required since we memcpy() data between the two!
                                176                 :                :  * ----------
                                177                 :                :  */
                                178                 :                : typedef struct _internalPQconninfoOption
                                179                 :                : {
                                180                 :                :     char       *keyword;        /* The keyword of the option            */
                                181                 :                :     char       *envvar;         /* Fallback environment variable name   */
                                182                 :                :     char       *compiled;       /* Fallback compiled in default value   */
                                183                 :                :     char       *val;            /* Option's current value, or NULL      */
                                184                 :                :     char       *label;          /* Label for field in connect dialog    */
                                185                 :                :     char       *dispchar;       /* Indicates how to display this field in a
                                186                 :                :                                  * connect dialog. Values are: "" Display
                                187                 :                :                                  * entered value as is "*" Password field -
                                188                 :                :                                  * hide value "D"  Debug option - don't show
                                189                 :                :                                  * by default */
                                190                 :                :     int         dispsize;       /* Field size in characters for dialog  */
                                191                 :                :     /* ---
                                192                 :                :      * Anything above this comment must be synchronized with
                                193                 :                :      * PQconninfoOption in libpq-fe.h, since we memcpy() data
                                194                 :                :      * between them!
                                195                 :                :      * ---
                                196                 :                :      */
                                197                 :                :     off_t       connofs;        /* Offset into PGconn struct, -1 if not there */
                                198                 :                : } internalPQconninfoOption;
                                199                 :                : 
                                200                 :                : static const internalPQconninfoOption PQconninfoOptions[] = {
                                201                 :                :     {"service", "PGSERVICE", NULL, NULL,
                                202                 :                :         "Database-Service", "", 20,
                                203                 :                :     offsetof(struct pg_conn, pgservice)},
                                204                 :                : 
                                205                 :                :     {"servicefile", "PGSERVICEFILE", NULL, NULL,
                                206                 :                :         "Database-Service-File", "", 64,
                                207                 :                :     offsetof(struct pg_conn, pgservicefile)},
                                208                 :                : 
                                209                 :                :     {"user", "PGUSER", NULL, NULL,
                                210                 :                :         "Database-User", "", 20,
                                211                 :                :     offsetof(struct pg_conn, pguser)},
                                212                 :                : 
                                213                 :                :     {"password", "PGPASSWORD", NULL, NULL,
                                214                 :                :         "Database-Password", "*", 20,
                                215                 :                :     offsetof(struct pg_conn, pgpass)},
                                216                 :                : 
                                217                 :                :     {"passfile", "PGPASSFILE", NULL, NULL,
                                218                 :                :         "Database-Password-File", "", 64,
                                219                 :                :     offsetof(struct pg_conn, pgpassfile)},
                                220                 :                : 
                                221                 :                :     {"channel_binding", "PGCHANNELBINDING", DefaultChannelBinding, NULL,
                                222                 :                :         "Channel-Binding", "", 8,   /* sizeof("require") == 8 */
                                223                 :                :     offsetof(struct pg_conn, channel_binding)},
                                224                 :                : 
                                225                 :                :     {"connect_timeout", "PGCONNECT_TIMEOUT", NULL, NULL,
                                226                 :                :         "Connect-timeout", "", 10,  /* strlen(INT32_MAX) == 10 */
                                227                 :                :     offsetof(struct pg_conn, connect_timeout)},
                                228                 :                : 
                                229                 :                :     {"dbname", "PGDATABASE", NULL, NULL,
                                230                 :                :         "Database-Name", "", 20,
                                231                 :                :     offsetof(struct pg_conn, dbName)},
                                232                 :                : 
                                233                 :                :     {"host", "PGHOST", NULL, NULL,
                                234                 :                :         "Database-Host", "", 40,
                                235                 :                :     offsetof(struct pg_conn, pghost)},
                                236                 :                : 
                                237                 :                :     {"hostaddr", "PGHOSTADDR", NULL, NULL,
                                238                 :                :         "Database-Host-IP-Address", "", 45,
                                239                 :                :     offsetof(struct pg_conn, pghostaddr)},
                                240                 :                : 
                                241                 :                :     {"port", "PGPORT", DEF_PGPORT_STR, NULL,
                                242                 :                :         "Database-Port", "", 6,
                                243                 :                :     offsetof(struct pg_conn, pgport)},
                                244                 :                : 
                                245                 :                :     {"client_encoding", "PGCLIENTENCODING", NULL, NULL,
                                246                 :                :         "Client-Encoding", "", 10,
                                247                 :                :     offsetof(struct pg_conn, client_encoding_initial)},
                                248                 :                : 
                                249                 :                :     {"options", "PGOPTIONS", DefaultOption, NULL,
                                250                 :                :         "Backend-Options", "", 40,
                                251                 :                :     offsetof(struct pg_conn, pgoptions)},
                                252                 :                : 
                                253                 :                :     {"application_name", "PGAPPNAME", NULL, NULL,
                                254                 :                :         "Application-Name", "", 64,
                                255                 :                :     offsetof(struct pg_conn, appname)},
                                256                 :                : 
                                257                 :                :     {"fallback_application_name", NULL, NULL, NULL,
                                258                 :                :         "Fallback-Application-Name", "", 64,
                                259                 :                :     offsetof(struct pg_conn, fbappname)},
                                260                 :                : 
                                261                 :                :     {"keepalives", NULL, NULL, NULL,
                                262                 :                :         "TCP-Keepalives", "", 1,    /* should be just '0' or '1' */
                                263                 :                :     offsetof(struct pg_conn, keepalives)},
                                264                 :                : 
                                265                 :                :     {"keepalives_idle", NULL, NULL, NULL,
                                266                 :                :         "TCP-Keepalives-Idle", "", 10,  /* strlen(INT32_MAX) == 10 */
                                267                 :                :     offsetof(struct pg_conn, keepalives_idle)},
                                268                 :                : 
                                269                 :                :     {"keepalives_interval", NULL, NULL, NULL,
                                270                 :                :         "TCP-Keepalives-Interval", "", 10,  /* strlen(INT32_MAX) == 10 */
                                271                 :                :     offsetof(struct pg_conn, keepalives_interval)},
                                272                 :                : 
                                273                 :                :     {"keepalives_count", NULL, NULL, NULL,
                                274                 :                :         "TCP-Keepalives-Count", "", 10, /* strlen(INT32_MAX) == 10 */
                                275                 :                :     offsetof(struct pg_conn, keepalives_count)},
                                276                 :                : 
                                277                 :                :     {"tcp_user_timeout", NULL, NULL, NULL,
                                278                 :                :         "TCP-User-Timeout", "", 10, /* strlen(INT32_MAX) == 10 */
                                279                 :                :     offsetof(struct pg_conn, pgtcp_user_timeout)},
                                280                 :                : 
                                281                 :                :     /*
                                282                 :                :      * ssl options are allowed even without client SSL support because the
                                283                 :                :      * client can still handle SSL modes "disable" and "allow". Other
                                284                 :                :      * parameters have no effect on non-SSL connections, so there is no reason
                                285                 :                :      * to exclude them since none of them are mandatory.
                                286                 :                :      */
                                287                 :                :     {"sslmode", "PGSSLMODE", DefaultSSLMode, NULL,
                                288                 :                :         "SSL-Mode", "", 12,     /* sizeof("verify-full") == 12 */
                                289                 :                :     offsetof(struct pg_conn, sslmode)},
                                290                 :                : 
                                291                 :                :     {"sslnegotiation", "PGSSLNEGOTIATION", DefaultSSLNegotiation, NULL,
                                292                 :                :         "SSL-Negotiation", "", 9,   /* sizeof("postgres") == 9  */
                                293                 :                :     offsetof(struct pg_conn, sslnegotiation)},
                                294                 :                : 
                                295                 :                :     {"sslcompression", "PGSSLCOMPRESSION", "0", NULL,
                                296                 :                :         "SSL-Compression", "", 1,
                                297                 :                :     offsetof(struct pg_conn, sslcompression)},
                                298                 :                : 
                                299                 :                :     {"sslcert", "PGSSLCERT", NULL, NULL,
                                300                 :                :         "SSL-Client-Cert", "", 64,
                                301                 :                :     offsetof(struct pg_conn, sslcert)},
                                302                 :                : 
                                303                 :                :     {"sslkey", "PGSSLKEY", NULL, NULL,
                                304                 :                :         "SSL-Client-Key", "", 64,
                                305                 :                :     offsetof(struct pg_conn, sslkey)},
                                306                 :                : 
                                307                 :                :     {"sslcertmode", "PGSSLCERTMODE", NULL, NULL,
                                308                 :                :         "SSL-Client-Cert-Mode", "", 8,  /* sizeof("disable") == 8 */
                                309                 :                :     offsetof(struct pg_conn, sslcertmode)},
                                310                 :                : 
                                311                 :                :     {"sslpassword", NULL, NULL, NULL,
                                312                 :                :         "SSL-Client-Key-Password", "*", 20,
                                313                 :                :     offsetof(struct pg_conn, sslpassword)},
                                314                 :                : 
                                315                 :                :     {"sslrootcert", "PGSSLROOTCERT", NULL, NULL,
                                316                 :                :         "SSL-Root-Certificate", "", 64,
                                317                 :                :     offsetof(struct pg_conn, sslrootcert)},
                                318                 :                : 
                                319                 :                :     {"sslcrl", "PGSSLCRL", NULL, NULL,
                                320                 :                :         "SSL-Revocation-List", "", 64,
                                321                 :                :     offsetof(struct pg_conn, sslcrl)},
                                322                 :                : 
                                323                 :                :     {"sslcrldir", "PGSSLCRLDIR", NULL, NULL,
                                324                 :                :         "SSL-Revocation-List-Dir", "", 64,
                                325                 :                :     offsetof(struct pg_conn, sslcrldir)},
                                326                 :                : 
                                327                 :                :     {"sslsni", "PGSSLSNI", "1", NULL,
                                328                 :                :         "SSL-SNI", "", 1,
                                329                 :                :     offsetof(struct pg_conn, sslsni)},
                                330                 :                : 
                                331                 :                :     {"requirepeer", "PGREQUIREPEER", NULL, NULL,
                                332                 :                :         "Require-Peer", "", 10,
                                333                 :                :     offsetof(struct pg_conn, requirepeer)},
                                334                 :                : 
                                335                 :                :     {"require_auth", "PGREQUIREAUTH", NULL, NULL,
                                336                 :                :         "Require-Auth", "", 14, /* sizeof("scram-sha-256") == 14 */
                                337                 :                :     offsetof(struct pg_conn, require_auth)},
                                338                 :                : 
                                339                 :                :     {"min_protocol_version", "PGMINPROTOCOLVERSION",
                                340                 :                :         NULL, NULL,
                                341                 :                :         "Min-Protocol-Version", "", 6,  /* sizeof("latest") = 6 */
                                342                 :                :     offsetof(struct pg_conn, min_protocol_version)},
                                343                 :                : 
                                344                 :                :     {"max_protocol_version", "PGMAXPROTOCOLVERSION",
                                345                 :                :         NULL, NULL,
                                346                 :                :         "Max-Protocol-Version", "", 6,  /* sizeof("latest") = 6 */
                                347                 :                :     offsetof(struct pg_conn, max_protocol_version)},
                                348                 :                : 
                                349                 :                :     {"ssl_min_protocol_version", "PGSSLMINPROTOCOLVERSION", "TLSv1.2", NULL,
                                350                 :                :         "SSL-Minimum-Protocol-Version", "", 8,  /* sizeof("TLSv1.x") == 8 */
                                351                 :                :     offsetof(struct pg_conn, ssl_min_protocol_version)},
                                352                 :                : 
                                353                 :                :     {"ssl_max_protocol_version", "PGSSLMAXPROTOCOLVERSION", NULL, NULL,
                                354                 :                :         "SSL-Maximum-Protocol-Version", "", 8,  /* sizeof("TLSv1.x") == 8 */
                                355                 :                :     offsetof(struct pg_conn, ssl_max_protocol_version)},
                                356                 :                : 
                                357                 :                :     /*
                                358                 :                :      * As with SSL, all GSS options are exposed even in builds that don't have
                                359                 :                :      * support.
                                360                 :                :      */
                                361                 :                :     {"gssencmode", "PGGSSENCMODE", DefaultGSSMode, NULL,
                                362                 :                :         "GSSENC-Mode", "", 8,   /* sizeof("disable") == 8 */
                                363                 :                :     offsetof(struct pg_conn, gssencmode)},
                                364                 :                : 
                                365                 :                :     /* Kerberos and GSSAPI authentication support specifying the service name */
                                366                 :                :     {"krbsrvname", "PGKRBSRVNAME", PG_KRB_SRVNAM, NULL,
                                367                 :                :         "Kerberos-service-name", "", 20,
                                368                 :                :     offsetof(struct pg_conn, krbsrvname)},
                                369                 :                : 
                                370                 :                :     {"gsslib", "PGGSSLIB", NULL, NULL,
                                371                 :                :         "GSS-library", "", 7,   /* sizeof("gssapi") == 7 */
                                372                 :                :     offsetof(struct pg_conn, gsslib)},
                                373                 :                : 
                                374                 :                :     {"gssdelegation", "PGGSSDELEGATION", "0", NULL,
                                375                 :                :         "GSS-delegation", "", 1,
                                376                 :                :     offsetof(struct pg_conn, gssdelegation)},
                                377                 :                : 
                                378                 :                :     {"replication", NULL, NULL, NULL,
                                379                 :                :         "Replication", "D", 5,
                                380                 :                :     offsetof(struct pg_conn, replication)},
                                381                 :                : 
                                382                 :                :     {"target_session_attrs", "PGTARGETSESSIONATTRS",
                                383                 :                :         DefaultTargetSessionAttrs, NULL,
                                384                 :                :         "Target-Session-Attrs", "", 15, /* sizeof("prefer-standby") = 15 */
                                385                 :                :     offsetof(struct pg_conn, target_session_attrs)},
                                386                 :                : 
                                387                 :                :     {"load_balance_hosts", "PGLOADBALANCEHOSTS",
                                388                 :                :         DefaultLoadBalanceHosts, NULL,
                                389                 :                :         "Load-Balance-Hosts", "", 8,    /* sizeof("disable") = 8 */
                                390                 :                :     offsetof(struct pg_conn, load_balance_hosts)},
                                391                 :                : 
                                392                 :                :     {"scram_client_key", NULL, NULL, NULL, "SCRAM-Client-Key", "D", SCRAM_MAX_KEY_LEN * 2,
                                393                 :                :     offsetof(struct pg_conn, scram_client_key)},
                                394                 :                : 
                                395                 :                :     {"scram_server_key", NULL, NULL, NULL, "SCRAM-Server-Key", "D", SCRAM_MAX_KEY_LEN * 2,
                                396                 :                :     offsetof(struct pg_conn, scram_server_key)},
                                397                 :                : 
                                398                 :                :     /* OAuth v2 */
                                399                 :                :     {"oauth_issuer", NULL, NULL, NULL,
                                400                 :                :         "OAuth-Issuer", "", 40,
                                401                 :                :     offsetof(struct pg_conn, oauth_issuer)},
                                402                 :                : 
                                403                 :                :     {"oauth_client_id", NULL, NULL, NULL,
                                404                 :                :         "OAuth-Client-ID", "", 40,
                                405                 :                :     offsetof(struct pg_conn, oauth_client_id)},
                                406                 :                : 
                                407                 :                :     {"oauth_client_secret", NULL, NULL, NULL,
                                408                 :                :         "OAuth-Client-Secret", "*", 40,
                                409                 :                :     offsetof(struct pg_conn, oauth_client_secret)},
                                410                 :                : 
                                411                 :                :     {"oauth_scope", NULL, NULL, NULL,
                                412                 :                :         "OAuth-Scope", "", 15,
                                413                 :                :     offsetof(struct pg_conn, oauth_scope)},
                                414                 :                : 
                                415                 :                :     {"sslkeylogfile", NULL, NULL, NULL,
                                416                 :                :         "SSL-Key-Log-File", "D", 64,
                                417                 :                :     offsetof(struct pg_conn, sslkeylogfile)},
                                418                 :                : 
                                419                 :                :     /* Terminating entry --- MUST BE LAST */
                                420                 :                :     {NULL, NULL, NULL, NULL,
                                421                 :                :     NULL, NULL, 0}
                                422                 :                : };
                                423                 :                : 
                                424                 :                : static const PQEnvironmentOption EnvironmentOptions[] =
                                425                 :                : {
                                426                 :                :     /* common user-interface settings */
                                427                 :                :     {
                                428                 :                :         "PGDATESTYLE", "datestyle"
                                429                 :                :     },
                                430                 :                :     {
                                431                 :                :         "PGTZ", "timezone"
                                432                 :                :     },
                                433                 :                :     /* internal performance-related settings */
                                434                 :                :     {
                                435                 :                :         "PGGEQO", "geqo"
                                436                 :                :     },
                                437                 :                :     {
                                438                 :                :         NULL, NULL
                                439                 :                :     }
                                440                 :                : };
                                441                 :                : 
                                442                 :                : static const pg_fe_sasl_mech *supported_sasl_mechs[] =
                                443                 :                : {
                                444                 :                :     &pg_scram_mech,
                                445                 :                :     &pg_oauth_mech,
                                446                 :                : };
                                447                 :                : #define SASL_MECHANISM_COUNT lengthof(supported_sasl_mechs)
                                448                 :                : 
                                449                 :                : /* The connection URI must start with either of the following designators: */
                                450                 :                : static const char uri_designator[] = "postgresql://";
                                451                 :                : static const char short_uri_designator[] = "postgres://";
                                452                 :                : 
                                453                 :                : static bool connectOptions1(PGconn *conn, const char *conninfo);
                                454                 :                : static bool init_allowed_encryption_methods(PGconn *conn);
                                455                 :                : #if defined(USE_SSL) || defined(ENABLE_GSS)
                                456                 :                : static int  encryption_negotiation_failed(PGconn *conn);
                                457                 :                : #endif
                                458                 :                : static bool connection_failed(PGconn *conn);
                                459                 :                : static bool select_next_encryption_method(PGconn *conn, bool have_valid_connection);
                                460                 :                : static PGPing internal_ping(PGconn *conn);
                                461                 :                : static void pqFreeCommandQueue(PGcmdQueueEntry *queue);
                                462                 :                : static bool fillPGconn(PGconn *conn, PQconninfoOption *connOptions);
                                463                 :                : static void freePGconn(PGconn *conn);
                                464                 :                : static void release_conn_addrinfo(PGconn *conn);
                                465                 :                : static int  store_conn_addrinfo(PGconn *conn, struct addrinfo *addrlist);
                                466                 :                : static void sendTerminateConn(PGconn *conn);
                                467                 :                : static PQconninfoOption *conninfo_init(PQExpBuffer errorMessage);
                                468                 :                : static PQconninfoOption *parse_connection_string(const char *connstr,
                                469                 :                :                                                  PQExpBuffer errorMessage, bool use_defaults);
                                470                 :                : static int  uri_prefix_length(const char *connstr);
                                471                 :                : static bool recognized_connection_string(const char *connstr);
                                472                 :                : static PQconninfoOption *conninfo_parse(const char *conninfo,
                                473                 :                :                                         PQExpBuffer errorMessage, bool use_defaults);
                                474                 :                : static PQconninfoOption *conninfo_array_parse(const char *const *keywords,
                                475                 :                :                                               const char *const *values, PQExpBuffer errorMessage,
                                476                 :                :                                               bool use_defaults, int expand_dbname);
                                477                 :                : static bool conninfo_add_defaults(PQconninfoOption *options,
                                478                 :                :                                   PQExpBuffer errorMessage);
                                479                 :                : static PQconninfoOption *conninfo_uri_parse(const char *uri,
                                480                 :                :                                             PQExpBuffer errorMessage, bool use_defaults);
                                481                 :                : static bool conninfo_uri_parse_options(PQconninfoOption *options,
                                482                 :                :                                        const char *uri, PQExpBuffer errorMessage);
                                483                 :                : static bool conninfo_uri_parse_params(char *params,
                                484                 :                :                                       PQconninfoOption *connOptions,
                                485                 :                :                                       PQExpBuffer errorMessage);
                                486                 :                : static char *conninfo_uri_decode(const char *str, PQExpBuffer errorMessage);
                                487                 :                : static bool get_hexdigit(char digit, int *value);
                                488                 :                : static const char *conninfo_getval(PQconninfoOption *connOptions,
                                489                 :                :                                    const char *keyword);
                                490                 :                : static PQconninfoOption *conninfo_storeval(PQconninfoOption *connOptions,
                                491                 :                :                                            const char *keyword, const char *value,
                                492                 :                :                                            PQExpBuffer errorMessage, bool ignoreMissing, bool uri_decode);
                                493                 :                : static PQconninfoOption *conninfo_find(PQconninfoOption *connOptions,
                                494                 :                :                                        const char *keyword);
                                495                 :                : static void defaultNoticeReceiver(void *arg, const PGresult *res);
                                496                 :                : static void defaultNoticeProcessor(void *arg, const char *message);
                                497                 :                : static int  parseServiceInfo(PQconninfoOption *options,
                                498                 :                :                              PQExpBuffer errorMessage);
                                499                 :                : static int  parseServiceFile(const char *serviceFile,
                                500                 :                :                              const char *service,
                                501                 :                :                              PQconninfoOption *options,
                                502                 :                :                              PQExpBuffer errorMessage,
                                503                 :                :                              bool *group_found);
                                504                 :                : static char *pwdfMatchesString(char *buf, const char *token);
                                505                 :                : static char *passwordFromFile(const char *hostname, const char *port,
                                506                 :                :                               const char *dbname, const char *username,
                                507                 :                :                               const char *pgpassfile, const char **errmsg);
                                508                 :                : static void pgpassfileWarning(PGconn *conn);
                                509                 :                : static void default_threadlock(int acquire);
                                510                 :                : static bool sslVerifyProtocolVersion(const char *version);
                                511                 :                : static bool sslVerifyProtocolRange(const char *min, const char *max);
                                512                 :                : static bool pqParseProtocolVersion(const char *value, ProtocolVersion *result, PGconn *conn, const char *context);
                                513                 :                : 
                                514                 :                : 
                                515                 :                : /* global variable because fe-auth.c needs to access it */
                                516                 :                : pgthreadlock_t pg_g_threadlock = default_threadlock;
                                517                 :                : 
                                518                 :                : 
                                519                 :                : /*
                                520                 :                :  *      pqDropConnection
                                521                 :                :  *
                                522                 :                :  * Close any physical connection to the server, and reset associated
                                523                 :                :  * state inside the connection object.  We don't release state that
                                524                 :                :  * would be needed to reconnect, though, nor local state that might still
                                525                 :                :  * be useful later.
                                526                 :                :  *
                                527                 :                :  * We can always flush the output buffer, since there's no longer any hope
                                528                 :                :  * of sending that data.  However, unprocessed input data might still be
                                529                 :                :  * valuable, so the caller must tell us whether to flush that or not.
                                530                 :                :  */
                                531                 :                : void
 3687 tgl@sss.pgh.pa.us         532                 :CBC       27497 : pqDropConnection(PGconn *conn, bool flushInput)
                                533                 :                : {
                                534                 :                :     /* Drop any SSL state */
 4848                           535                 :          27497 :     pqsecure_close(conn);
                                536                 :                : 
                                537                 :                :     /* Close the socket itself */
 4262 bruce@momjian.us          538         [ +  + ]:          27497 :     if (conn->sock != PGINVALID_SOCKET)
 4848 tgl@sss.pgh.pa.us         539                 :          13358 :         closesocket(conn->sock);
 4262 bruce@momjian.us          540                 :          27497 :     conn->sock = PGINVALID_SOCKET;
                                541                 :                : 
                                542                 :                :     /* Optionally discard any unread data */
 3687 tgl@sss.pgh.pa.us         543         [ +  + ]:          27497 :     if (flushInput)
                                544                 :          27390 :         conn->inStart = conn->inCursor = conn->inEnd = 0;
                                545                 :                : 
                                546                 :                :     /* Always discard any unsent data */
 4848                           547                 :          27497 :     conn->outCount = 0;
                                548                 :                : 
                                549                 :                :     /* Likewise, discard any pending pipelined commands */
 1314                           550                 :          27497 :     pqFreeCommandQueue(conn->cmd_queue_head);
                                551                 :          27497 :     conn->cmd_queue_head = conn->cmd_queue_tail = NULL;
                                552                 :          27497 :     pqFreeCommandQueue(conn->cmd_queue_recycle);
                                553                 :          27497 :     conn->cmd_queue_recycle = NULL;
                                554                 :                : 
                                555                 :                :     /* Free authentication/encryption state */
  313 dgustafsson@postgres      556         [ +  + ]:          27497 :     if (conn->cleanup_async_auth)
                                557                 :                :     {
                                558                 :                :         /*
                                559                 :                :          * Any in-progress async authentication should be torn down first so
                                560                 :                :          * that cleanup_async_auth() can depend on the other authentication
                                561                 :                :          * state if necessary.
                                562                 :                :          */
                                563                 :             20 :         conn->cleanup_async_auth(conn);
                                564                 :             20 :         conn->cleanup_async_auth = NULL;
                                565                 :                :     }
                                566                 :          27497 :     conn->async_auth = NULL;
                                567                 :                :     /* cleanup_async_auth() should have done this, but make sure */
                                568                 :          27497 :     conn->altsock = PGINVALID_SOCKET;
                                569                 :                : #ifdef ENABLE_GSS
                                570                 :                :     {
                                571                 :                :         OM_uint32   min_s;
                                572                 :                : 
 1982 tgl@sss.pgh.pa.us         573         [ +  + ]:          27497 :         if (conn->gcred != GSS_C_NO_CREDENTIAL)
                                574                 :                :         {
                                575                 :             10 :             gss_release_cred(&min_s, &conn->gcred);
                                576                 :             10 :             conn->gcred = GSS_C_NO_CREDENTIAL;
                                577                 :                :         }
 3114 heikki.linnakangas@i      578         [ +  + ]:          27497 :         if (conn->gctx)
                                579                 :            130 :             gss_delete_sec_context(&min_s, &conn->gctx, GSS_C_NO_BUFFER);
                                580         [ +  + ]:          27497 :         if (conn->gtarg_nam)
                                581                 :            120 :             gss_release_name(&min_s, &conn->gtarg_nam);
 2166 tgl@sss.pgh.pa.us         582         [ +  + ]:          27497 :         if (conn->gss_SendBuffer)
                                583                 :                :         {
                                584                 :            120 :             free(conn->gss_SendBuffer);
                                585                 :            120 :             conn->gss_SendBuffer = NULL;
                                586                 :                :         }
                                587         [ +  + ]:          27497 :         if (conn->gss_RecvBuffer)
                                588                 :                :         {
                                589                 :            120 :             free(conn->gss_RecvBuffer);
                                590                 :            120 :             conn->gss_RecvBuffer = NULL;
                                591                 :                :         }
                                592         [ +  + ]:          27497 :         if (conn->gss_ResultBuffer)
                                593                 :                :         {
                                594                 :            120 :             free(conn->gss_ResultBuffer);
                                595                 :            120 :             conn->gss_ResultBuffer = NULL;
                                596                 :                :         }
 1982                           597                 :          27497 :         conn->gssenc = false;
                                598                 :                :     }
                                599                 :                : #endif
                                600                 :                : #ifdef ENABLE_SSPI
                                601                 :                :     if (conn->sspitarget)
                                602                 :                :     {
                                603                 :                :         free(conn->sspitarget);
                                604                 :                :         conn->sspitarget = NULL;
                                605                 :                :     }
                                606                 :                :     if (conn->sspicred)
                                607                 :                :     {
                                608                 :                :         FreeCredentialsHandle(conn->sspicred);
                                609                 :                :         free(conn->sspicred);
                                610                 :                :         conn->sspicred = NULL;
                                611                 :                :     }
                                612                 :                :     if (conn->sspictx)
                                613                 :                :     {
                                614                 :                :         DeleteSecurityContext(conn->sspictx);
                                615                 :                :         free(conn->sspictx);
                                616                 :                :         conn->sspictx = NULL;
                                617                 :                :     }
                                618                 :                :     conn->usesspi = 0;
                                619                 :                : #endif
 3114 heikki.linnakangas@i      620         [ +  + ]:          27497 :     if (conn->sasl_state)
                                621                 :                :     {
 1623 michael@paquier.xyz       622                 :            151 :         conn->sasl->free(conn->sasl_state);
 3114 heikki.linnakangas@i      623                 :            151 :         conn->sasl_state = NULL;
                                624                 :                :     }
 4848 tgl@sss.pgh.pa.us         625                 :          27497 : }
                                626                 :                : 
                                627                 :                : /*
                                628                 :                :  * pqFreeCommandQueue
                                629                 :                :  * Free all the entries of PGcmdQueueEntry queue passed.
                                630                 :                :  */
                                631                 :                : static void
 1737 alvherre@alvh.no-ip.      632                 :          54994 : pqFreeCommandQueue(PGcmdQueueEntry *queue)
                                633                 :                : {
                                634         [ +  + ]:          68285 :     while (queue != NULL)
                                635                 :                :     {
                                636                 :          13291 :         PGcmdQueueEntry *cur = queue;
                                637                 :                : 
                                638                 :          13291 :         queue = cur->next;
 1279 peter@eisentraut.org      639                 :          13291 :         free(cur->query);
 1737 alvherre@alvh.no-ip.      640                 :          13291 :         free(cur);
                                641                 :                :     }
                                642                 :          54994 : }
                                643                 :                : 
                                644                 :                : /*
                                645                 :                :  *      pqDropServerData
                                646                 :                :  *
                                647                 :                :  * Clear all connection state data that was received from (or deduced about)
                                648                 :                :  * the server.  This is essential to do between connection attempts to
                                649                 :                :  * different servers, else we may incorrectly hold over some data from the
                                650                 :                :  * old server.
                                651                 :                :  *
                                652                 :                :  * It would be better to merge this into pqDropConnection, perhaps, but
                                653                 :                :  * right now we cannot because that function is called immediately on
                                654                 :                :  * detection of connection loss (cf. pqReadData, for instance).  This data
                                655                 :                :  * should be kept until we are actually starting a new connection.
                                656                 :                :  */
                                657                 :                : static void
 2689 tgl@sss.pgh.pa.us         658                 :          27164 : pqDropServerData(PGconn *conn)
                                659                 :                : {
                                660                 :                :     PGnotify   *notify;
                                661                 :                :     pgParameterStatus *pstatus;
                                662                 :                : 
                                663                 :                :     /* Forget pending notifies */
                                664                 :          27164 :     notify = conn->notifyHead;
                                665         [ -  + ]:          27164 :     while (notify != NULL)
                                666                 :                :     {
 2689 tgl@sss.pgh.pa.us         667                 :UBC           0 :         PGnotify   *prev = notify;
                                668                 :                : 
                                669                 :              0 :         notify = notify->next;
                                670                 :              0 :         free(prev);
                                671                 :                :     }
 2689 tgl@sss.pgh.pa.us         672                 :CBC       27164 :     conn->notifyHead = conn->notifyTail = NULL;
                                673                 :                : 
                                674                 :                :     /* Reset ParameterStatus data, as well as variables deduced from it */
                                675                 :          27164 :     pstatus = conn->pstatus;
                                676         [ +  + ]:         216209 :     while (pstatus != NULL)
                                677                 :                :     {
                                678                 :         189045 :         pgParameterStatus *prev = pstatus;
                                679                 :                : 
                                680                 :         189045 :         pstatus = pstatus->next;
                                681                 :         189045 :         free(prev);
                                682                 :                :     }
                                683                 :          27164 :     conn->pstatus = NULL;
                                684                 :          27164 :     conn->client_encoding = PG_SQL_ASCII;
                                685                 :          27164 :     conn->std_strings = false;
 1750                           686                 :          27164 :     conn->default_transaction_read_only = PG_BOOL_UNKNOWN;
                                687                 :          27164 :     conn->in_hot_standby = PG_BOOL_UNKNOWN;
  995 dgustafsson@postgres      688                 :          27164 :     conn->scram_sha_256_iterations = SCRAM_SHA_256_DEFAULT_ITERATIONS;
 2689 tgl@sss.pgh.pa.us         689                 :          27164 :     conn->sversion = 0;
                                690                 :                : 
                                691                 :                :     /* Drop large-object lookup data */
 1279 peter@eisentraut.org      692                 :          27164 :     free(conn->lobjfuncs);
 2689 tgl@sss.pgh.pa.us         693                 :          27164 :     conn->lobjfuncs = NULL;
                                694                 :                : 
                                695                 :                :     /* Reset assorted other per-connection state */
                                696                 :          27164 :     conn->last_sqlstate[0] = '\0';
  258 heikki.linnakangas@i      697                 :          27164 :     conn->pversion_negotiated = false;
 2689 tgl@sss.pgh.pa.us         698                 :          27164 :     conn->auth_req_received = false;
 1008 michael@paquier.xyz       699                 :          27164 :     conn->client_finished_auth = false;
 2689 tgl@sss.pgh.pa.us         700                 :          27164 :     conn->password_needed = false;
  978 sfrost@snowman.net        701                 :          27164 :     conn->gssapi_used = false;
 2464 tgl@sss.pgh.pa.us         702                 :          27164 :     conn->write_failed = false;
 1279 peter@eisentraut.org      703                 :          27164 :     free(conn->write_err_msg);
 2464 tgl@sss.pgh.pa.us         704                 :          27164 :     conn->write_err_msg = NULL;
  299 dgustafsson@postgres      705                 :          27164 :     conn->oauth_want_retry = false;
                                706                 :                : 
                                707                 :                :     /*
                                708                 :                :      * Cancel connections need to retain their be_pid and be_cancel_key across
                                709                 :                :      * PQcancelReset invocations, otherwise they would not have access to the
                                710                 :                :      * secret token of the connection they are supposed to cancel.
                                711                 :                :      */
  644 alvherre@alvh.no-ip.      712         [ +  + ]:          27164 :     if (!conn->cancelRequest)
                                713                 :                :     {
                                714                 :          27155 :         conn->be_pid = 0;
  258 heikki.linnakangas@i      715         [ +  + ]:          27155 :         if (conn->be_cancel_key != NULL)
                                716                 :                :         {
                                717                 :          12603 :             free(conn->be_cancel_key);
                                718                 :          12603 :             conn->be_cancel_key = NULL;
                                719                 :                :         }
                                720                 :          27155 :         conn->be_cancel_key_len = 0;
                                721                 :                :     }
 2689 tgl@sss.pgh.pa.us         722                 :          27164 : }
                                723                 :                : 
                                724                 :                : 
                                725                 :                : /*
                                726                 :                :  *      Connecting to a Database
                                727                 :                :  *
                                728                 :                :  * There are now six different ways a user of this API can connect to the
                                729                 :                :  * database.  Two are not recommended for use in new code, because of their
                                730                 :                :  * lack of extensibility with respect to the passing of options to the
                                731                 :                :  * backend.  These are PQsetdb and PQsetdbLogin (the former now being a macro
                                732                 :                :  * to the latter).
                                733                 :                :  *
                                734                 :                :  * If it is desired to connect in a synchronous (blocking) manner, use the
                                735                 :                :  * function PQconnectdb or PQconnectdbParams. The former accepts a string of
                                736                 :                :  * option = value pairs (or a URI) which must be parsed; the latter takes two
                                737                 :                :  * NULL terminated arrays instead.
                                738                 :                :  *
                                739                 :                :  * To connect in an asynchronous (non-blocking) manner, use the functions
                                740                 :                :  * PQconnectStart or PQconnectStartParams (which differ in the same way as
                                741                 :                :  * PQconnectdb and PQconnectdbParams) and PQconnectPoll.
                                742                 :                :  *
                                743                 :                :  * The non-exported functions pqConnectDBStart, pqConnectDBComplete are
                                744                 :                :  * part of the connection procedure implementation.
                                745                 :                :  */
                                746                 :                : 
                                747                 :                : /*
                                748                 :                :  *      PQconnectdbParams
                                749                 :                :  *
                                750                 :                :  * establishes a connection to a postgres backend through the postmaster
                                751                 :                :  * using connection information in two arrays.
                                752                 :                :  *
                                753                 :                :  * The keywords array is defined as
                                754                 :                :  *
                                755                 :                :  *     const char *params[] = {"option1", "option2", NULL}
                                756                 :                :  *
                                757                 :                :  * The values array is defined as
                                758                 :                :  *
                                759                 :                :  *     const char *values[] = {"value1", "value2", NULL}
                                760                 :                :  *
                                761                 :                :  * Returns a PGconn* which is needed for all subsequent libpq calls, or NULL
                                762                 :                :  * if a memory allocation failed.
                                763                 :                :  * If the status field of the connection returned is CONNECTION_BAD,
                                764                 :                :  * then some fields may be null'ed out instead of having valid values.
                                765                 :                :  *
                                766                 :                :  * You should call PQfinish (if conn is not NULL) regardless of whether this
                                767                 :                :  * call succeeded.
                                768                 :                :  */
                                769                 :                : PGconn *
 3100                           770                 :          10715 : PQconnectdbParams(const char *const *keywords,
                                771                 :                :                   const char *const *values,
                                772                 :                :                   int expand_dbname)
                                773                 :                : {
 5793 mail@joeconway.com        774                 :          10715 :     PGconn     *conn = PQconnectStartParams(keywords, values, expand_dbname);
                                775                 :                : 
 5801                           776   [ +  -  +  + ]:          10715 :     if (conn && conn->status != CONNECTION_BAD)
  681 alvherre@alvh.no-ip.      777                 :          10595 :         (void) pqConnectDBComplete(conn);
                                778                 :                : 
 5801 mail@joeconway.com        779                 :          10715 :     return conn;
                                780                 :                : }
                                781                 :                : 
                                782                 :                : /*
                                783                 :                :  *      PQpingParams
                                784                 :                :  *
                                785                 :                :  * check server status, accepting parameters identical to PQconnectdbParams
                                786                 :                :  */
                                787                 :                : PGPing
 3100 tgl@sss.pgh.pa.us         788                 :            472 : PQpingParams(const char *const *keywords,
                                789                 :                :              const char *const *values,
                                790                 :                :              int expand_dbname)
                                791                 :                : {
 5500 bruce@momjian.us          792                 :            472 :     PGconn     *conn = PQconnectStartParams(keywords, values, expand_dbname);
                                793                 :                :     PGPing      ret;
                                794                 :                : 
                                795                 :            472 :     ret = internal_ping(conn);
                                796                 :            472 :     PQfinish(conn);
                                797                 :                : 
                                798                 :            472 :     return ret;
                                799                 :                : }
                                800                 :                : 
                                801                 :                : /*
                                802                 :                :  *      PQconnectdb
                                803                 :                :  *
                                804                 :                :  * establishes a connection to a postgres backend through the postmaster
                                805                 :                :  * using connection information in a string.
                                806                 :                :  *
                                807                 :                :  * The conninfo string is either a whitespace-separated list of
                                808                 :                :  *
                                809                 :                :  *     option = value
                                810                 :                :  *
                                811                 :                :  * definitions or a URI (refer to the documentation for details.) Value
                                812                 :                :  * might be a single value containing no whitespaces or a single quoted
                                813                 :                :  * string. If a single quote should appear anywhere in the value, it must be
                                814                 :                :  * escaped with a backslash like \'
                                815                 :                :  *
                                816                 :                :  * Returns a PGconn* which is needed for all subsequent libpq calls, or NULL
                                817                 :                :  * if a memory allocation failed.
                                818                 :                :  * If the status field of the connection returned is CONNECTION_BAD,
                                819                 :                :  * then some fields may be null'ed out instead of having valid values.
                                820                 :                :  *
                                821                 :                :  * You should call PQfinish (if conn is not NULL) regardless of whether this
                                822                 :                :  * call succeeded.
                                823                 :                :  */
                                824                 :                : PGconn *
10629 scrappy@hub.org           825                 :            923 : PQconnectdb(const char *conninfo)
                                826                 :                : {
 9379 bruce@momjian.us          827                 :            923 :     PGconn     *conn = PQconnectStart(conninfo);
                                828                 :                : 
 9468 tgl@sss.pgh.pa.us         829   [ +  -  +  - ]:            923 :     if (conn && conn->status != CONNECTION_BAD)
  681 alvherre@alvh.no-ip.      830                 :            923 :         (void) pqConnectDBComplete(conn);
                                831                 :                : 
 9513 bruce@momjian.us          832                 :            923 :     return conn;
                                833                 :                : }
                                834                 :                : 
                                835                 :                : /*
                                836                 :                :  *      PQping
                                837                 :                :  *
                                838                 :                :  * check server status, accepting parameters identical to PQconnectdb
                                839                 :                :  */
                                840                 :                : PGPing
 5500 bruce@momjian.us          841                 :UBC           0 : PQping(const char *conninfo)
                                842                 :                : {
                                843                 :              0 :     PGconn     *conn = PQconnectStart(conninfo);
                                844                 :                :     PGPing      ret;
                                845                 :                : 
                                846                 :              0 :     ret = internal_ping(conn);
                                847                 :              0 :     PQfinish(conn);
                                848                 :                : 
                                849                 :              0 :     return ret;
                                850                 :                : }
                                851                 :                : 
                                852                 :                : /*
                                853                 :                :  *      PQconnectStartParams
                                854                 :                :  *
                                855                 :                :  * Begins the establishment of a connection to a postgres backend through the
                                856                 :                :  * postmaster using connection information in a struct.
                                857                 :                :  *
                                858                 :                :  * See comment for PQconnectdbParams for the definition of the string format.
                                859                 :                :  *
                                860                 :                :  * Returns a PGconn*.  If NULL is returned, a malloc error has occurred, and
                                861                 :                :  * you should not attempt to proceed with this connection.  If the status
                                862                 :                :  * field of the connection returned is CONNECTION_BAD, an error has
                                863                 :                :  * occurred. In this case you should call PQfinish on the result, (perhaps
                                864                 :                :  * inspecting the error message first).  Other fields of the structure may not
                                865                 :                :  * be valid if that occurs.  If the status field is not CONNECTION_BAD, then
                                866                 :                :  * this stage has succeeded - call PQconnectPoll, using select(2) to see when
                                867                 :                :  * this is necessary.
                                868                 :                :  *
                                869                 :                :  * See PQconnectPoll for more info.
                                870                 :                :  */
                                871                 :                : PGconn *
 3100 tgl@sss.pgh.pa.us         872                 :CBC       12352 : PQconnectStartParams(const char *const *keywords,
                                873                 :                :                      const char *const *values,
                                874                 :                :                      int expand_dbname)
                                875                 :                : {
                                876                 :                :     PGconn     *conn;
                                877                 :                :     PQconninfoOption *connOptions;
                                878                 :                : 
                                879                 :                :     /*
                                880                 :                :      * Allocate memory for the conn structure.  Note that we also expect this
                                881                 :                :      * to initialize conn->errorMessage to empty.  All subsequent steps during
                                882                 :                :      * connection initialization will only append to that buffer.
                                883                 :                :      */
  681 alvherre@alvh.no-ip.      884                 :          12352 :     conn = pqMakeEmptyPGconn();
10327 bruce@momjian.us          885         [ -  + ]:          12352 :     if (conn == NULL)
 8014 neilc@samurai.com         886                 :UBC           0 :         return NULL;
                                887                 :                : 
                                888                 :                :     /*
                                889                 :                :      * Parse the conninfo arrays
                                890                 :                :      */
 5801 mail@joeconway.com        891                 :CBC       12352 :     connOptions = conninfo_array_parse(keywords, values,
                                892                 :                :                                        &conn->errorMessage,
                                893                 :                :                                        true, expand_dbname);
                                894         [ +  + ]:          12352 :     if (connOptions == NULL)
                                895                 :                :     {
                                896                 :              8 :         conn->status = CONNECTION_BAD;
                                897                 :                :         /* errorMessage is already set */
 5372 tgl@sss.pgh.pa.us         898                 :              8 :         return conn;
                                899                 :                :     }
                                900                 :                : 
                                901                 :                :     /*
                                902                 :                :      * Move option values into conn structure
                                903                 :                :      */
 4039 heikki.linnakangas@i      904         [ -  + ]:          12344 :     if (!fillPGconn(conn, connOptions))
                                905                 :                :     {
 4039 heikki.linnakangas@i      906                 :UBC           0 :         PQconninfoFree(connOptions);
                                907                 :              0 :         return conn;
                                908                 :                :     }
                                909                 :                : 
                                910                 :                :     /*
                                911                 :                :      * Free the option info - all is in conn now
                                912                 :                :      */
 5801 mail@joeconway.com        913                 :CBC       12344 :     PQconninfoFree(connOptions);
                                914                 :                : 
                                915                 :                :     /*
                                916                 :                :      * Compute derived options
                                917                 :                :      */
  681 alvherre@alvh.no-ip.      918         [ +  + ]:          12344 :     if (!pqConnectOptions2(conn))
 8268 tgl@sss.pgh.pa.us         919                 :            106 :         return conn;
                                920                 :                : 
                                921                 :                :     /*
                                922                 :                :      * Connect to the database
                                923                 :                :      */
  681 alvherre@alvh.no-ip.      924         [ +  + ]:          12238 :     if (!pqConnectDBStart(conn))
                                925                 :                :     {
                                926                 :                :         /* Just in case we failed to set it in pqConnectDBStart */
 8268 tgl@sss.pgh.pa.us         927                 :            226 :         conn->status = CONNECTION_BAD;
                                928                 :                :     }
                                929                 :                : 
                                930                 :          12238 :     return conn;
                                931                 :                : }
                                932                 :                : 
                                933                 :                : /*
                                934                 :                :  *      PQconnectStart
                                935                 :                :  *
                                936                 :                :  * Begins the establishment of a connection to a postgres backend through the
                                937                 :                :  * postmaster using connection information in a string.
                                938                 :                :  *
                                939                 :                :  * See comment for PQconnectdb for the definition of the string format.
                                940                 :                :  *
                                941                 :                :  * Returns a PGconn*.  If NULL is returned, a malloc error has occurred, and
                                942                 :                :  * you should not attempt to proceed with this connection.  If the status
                                943                 :                :  * field of the connection returned is CONNECTION_BAD, an error has
                                944                 :                :  * occurred. In this case you should call PQfinish on the result, (perhaps
                                945                 :                :  * inspecting the error message first).  Other fields of the structure may not
                                946                 :                :  * be valid if that occurs.  If the status field is not CONNECTION_BAD, then
                                947                 :                :  * this stage has succeeded - call PQconnectPoll, using select(2) to see when
                                948                 :                :  * this is necessary.
                                949                 :                :  *
                                950                 :                :  * See PQconnectPoll for more info.
                                951                 :                :  */
                                952                 :                : PGconn *
 5801 mail@joeconway.com        953                 :           1231 : PQconnectStart(const char *conninfo)
                                954                 :                : {
                                955                 :                :     PGconn     *conn;
                                956                 :                : 
                                957                 :                :     /*
                                958                 :                :      * Allocate memory for the conn structure.  Note that we also expect this
                                959                 :                :      * to initialize conn->errorMessage to empty.  All subsequent steps during
                                960                 :                :      * connection initialization will only append to that buffer.
                                961                 :                :      */
  681 alvherre@alvh.no-ip.      962                 :           1231 :     conn = pqMakeEmptyPGconn();
 5801 mail@joeconway.com        963         [ -  + ]:           1231 :     if (conn == NULL)
 5801 mail@joeconway.com        964                 :UBC           0 :         return NULL;
                                965                 :                : 
                                966                 :                :     /*
                                967                 :                :      * Parse the conninfo string
                                968                 :                :      */
 5801 mail@joeconway.com        969         [ +  + ]:CBC        1231 :     if (!connectOptions1(conn, conninfo))
                                970                 :              2 :         return conn;
                                971                 :                : 
                                972                 :                :     /*
                                973                 :                :      * Compute derived options
                                974                 :                :      */
  681 alvherre@alvh.no-ip.      975         [ -  + ]:           1229 :     if (!pqConnectOptions2(conn))
 5801 mail@joeconway.com        976                 :UBC           0 :         return conn;
                                977                 :                : 
                                978                 :                :     /*
                                979                 :                :      * Connect to the database
                                980                 :                :      */
  681 alvherre@alvh.no-ip.      981         [ -  + ]:CBC        1229 :     if (!pqConnectDBStart(conn))
                                982                 :                :     {
                                983                 :                :         /* Just in case we failed to set it in pqConnectDBStart */
10327 bruce@momjian.us          984                 :UBC           0 :         conn->status = CONNECTION_BAD;
                                985                 :                :     }
                                986                 :                : 
 5801 mail@joeconway.com        987                 :CBC        1229 :     return conn;
                                988                 :                : }
                                989                 :                : 
                                990                 :                : /*
                                991                 :                :  * Move option values into conn structure
                                992                 :                :  *
                                993                 :                :  * Don't put anything cute here --- intelligence should be in
                                994                 :                :  * pqConnectOptions2 ...
                                995                 :                :  *
                                996                 :                :  * Returns true on success. On failure, returns false and sets error message.
                                997                 :                :  */
                                998                 :                : static bool
                                999                 :          13573 : fillPGconn(PGconn *conn, PQconninfoOption *connOptions)
                               1000                 :                : {
                               1001                 :                :     const internalPQconninfoOption *option;
                               1002                 :                : 
 4764 magnus@hagander.net      1003         [ +  + ]:         705796 :     for (option = PQconninfoOptions; option->keyword; option++)
                               1004                 :                :     {
 4034 tgl@sss.pgh.pa.us        1005         [ +  - ]:         692223 :         if (option->connofs >= 0)
                               1006                 :                :         {
                               1007                 :         692223 :             const char *tmp = conninfo_getval(connOptions, option->keyword);
                               1008                 :                : 
 4039 heikki.linnakangas@i     1009         [ +  + ]:         692223 :             if (tmp)
                               1010                 :                :             {
 4034 tgl@sss.pgh.pa.us        1011                 :         246076 :                 char      **connmember = (char **) ((char *) conn + option->connofs);
                               1012                 :                : 
 1279 peter@eisentraut.org     1013                 :         246076 :                 free(*connmember);
 4039 heikki.linnakangas@i     1014                 :         246076 :                 *connmember = strdup(tmp);
                               1015         [ -  + ]:         246076 :                 if (*connmember == NULL)
                               1016                 :                :                 {
 1127 peter@eisentraut.org     1017                 :UBC           0 :                     libpq_append_conn_error(conn, "out of memory");
 4039 heikki.linnakangas@i     1018                 :              0 :                     return false;
                               1019                 :                :                 }
                               1020                 :                :             }
                               1021                 :                :         }
                               1022                 :                :     }
                               1023                 :                : 
 4039 heikki.linnakangas@i     1024                 :CBC       13573 :     return true;
                               1025                 :                : }
                               1026                 :                : 
                               1027                 :                : /*
                               1028                 :                :  * Copy over option values from srcConn to dstConn
                               1029                 :                :  *
                               1030                 :                :  * Don't put anything cute here --- intelligence should be in
                               1031                 :                :  * pqConnectOptions2 ...
                               1032                 :                :  *
                               1033                 :                :  * Returns true on success. On failure, returns false and sets error message of
                               1034                 :                :  * dstConn.
                               1035                 :                :  */
                               1036                 :                : bool
  644 alvherre@alvh.no-ip.     1037                 :              7 : pqCopyPGconn(PGconn *srcConn, PGconn *dstConn)
                               1038                 :                : {
                               1039                 :                :     const internalPQconninfoOption *option;
                               1040                 :                : 
                               1041                 :                :     /* copy over connection options */
                               1042         [ +  + ]:            364 :     for (option = PQconninfoOptions; option->keyword; option++)
                               1043                 :                :     {
                               1044         [ +  - ]:            357 :         if (option->connofs >= 0)
                               1045                 :                :         {
                               1046                 :            357 :             const char **tmp = (const char **) ((char *) srcConn + option->connofs);
                               1047                 :                : 
                               1048         [ +  + ]:            357 :             if (*tmp)
                               1049                 :                :             {
                               1050                 :            141 :                 char      **dstConnmember = (char **) ((char *) dstConn + option->connofs);
                               1051                 :                : 
                               1052         [ -  + ]:            141 :                 if (*dstConnmember)
  644 alvherre@alvh.no-ip.     1053                 :UBC           0 :                     free(*dstConnmember);
  644 alvherre@alvh.no-ip.     1054                 :CBC         141 :                 *dstConnmember = strdup(*tmp);
                               1055         [ -  + ]:            141 :                 if (*dstConnmember == NULL)
                               1056                 :                :                 {
  644 alvherre@alvh.no-ip.     1057                 :UBC           0 :                     libpq_append_conn_error(dstConn, "out of memory");
                               1058                 :              0 :                     return false;
                               1059                 :                :                 }
                               1060                 :                :             }
                               1061                 :                :         }
                               1062                 :                :     }
  644 alvherre@alvh.no-ip.     1063                 :CBC           7 :     return true;
                               1064                 :                : }
                               1065                 :                : 
                               1066                 :                : /*
                               1067                 :                :  *      connectOptions1
                               1068                 :                :  *
                               1069                 :                :  * Internal subroutine to set up connection parameters given an already-
                               1070                 :                :  * created PGconn and a conninfo string.  Derived settings should be
                               1071                 :                :  * processed by calling pqConnectOptions2 next.  (We split them because
                               1072                 :                :  * PQsetdbLogin overrides defaults in between.)
                               1073                 :                :  *
                               1074                 :                :  * Returns true if OK, false if trouble (in which case errorMessage is set
                               1075                 :                :  * and so is conn->status).
                               1076                 :                :  */
                               1077                 :                : static bool
 5801 mail@joeconway.com       1078                 :           1231 : connectOptions1(PGconn *conn, const char *conninfo)
                               1079                 :                : {
                               1080                 :                :     PQconninfoOption *connOptions;
                               1081                 :                : 
                               1082                 :                :     /*
                               1083                 :                :      * Parse the conninfo string
                               1084                 :                :      */
 4997 alvherre@alvh.no-ip.     1085                 :           1231 :     connOptions = parse_connection_string(conninfo, &conn->errorMessage, true);
 5801 mail@joeconway.com       1086         [ +  + ]:           1231 :     if (connOptions == NULL)
                               1087                 :                :     {
                               1088                 :              2 :         conn->status = CONNECTION_BAD;
                               1089                 :                :         /* errorMessage is already set */
                               1090                 :              2 :         return false;
                               1091                 :                :     }
                               1092                 :                : 
                               1093                 :                :     /*
                               1094                 :                :      * Move option values into conn structure
                               1095                 :                :      */
 4039 heikki.linnakangas@i     1096         [ -  + ]:           1229 :     if (!fillPGconn(conn, connOptions))
                               1097                 :                :     {
 4039 heikki.linnakangas@i     1098                 :UBC           0 :         conn->status = CONNECTION_BAD;
                               1099                 :              0 :         PQconninfoFree(connOptions);
                               1100                 :              0 :         return false;
                               1101                 :                :     }
                               1102                 :                : 
                               1103                 :                :     /*
                               1104                 :                :      * Free the option info - all is in conn now
                               1105                 :                :      */
 9411 tgl@sss.pgh.pa.us        1106                 :CBC        1229 :     PQconninfoFree(connOptions);
                               1107                 :                : 
 8268                          1108                 :           1229 :     return true;
                               1109                 :                : }
                               1110                 :                : 
                               1111                 :                : /*
                               1112                 :                :  * Count the number of elements in a simple comma-separated list.
                               1113                 :                :  */
                               1114                 :                : static int
 3081 heikki.linnakangas@i     1115                 :          13580 : count_comma_separated_elems(const char *input)
                               1116                 :                : {
                               1117                 :                :     int         n;
                               1118                 :                : 
                               1119                 :          13580 :     n = 1;
                               1120         [ +  + ]:         230975 :     for (; *input != '\0'; input++)
                               1121                 :                :     {
                               1122         [ +  + ]:         217395 :         if (*input == ',')
                               1123                 :            133 :             n++;
                               1124                 :                :     }
                               1125                 :                : 
                               1126                 :          13580 :     return n;
                               1127                 :                : }
                               1128                 :                : 
                               1129                 :                : /*
                               1130                 :                :  * Parse a simple comma-separated list.
                               1131                 :                :  *
                               1132                 :                :  * On each call, returns a malloc'd copy of the next element, and sets *more
                               1133                 :                :  * to indicate whether there are any more elements in the list after this,
                               1134                 :                :  * and updates *startptr to point to the next element, if any.
                               1135                 :                :  *
                               1136                 :                :  * On out of memory, returns NULL.
                               1137                 :                :  */
                               1138                 :                : static char *
                               1139                 :          27946 : parse_comma_separated_list(char **startptr, bool *more)
                               1140                 :                : {
                               1141                 :                :     char       *p;
                               1142                 :          27946 :     char       *s = *startptr;
                               1143                 :                :     char       *e;
                               1144                 :                :     size_t      len;
                               1145                 :                : 
                               1146                 :                :     /*
                               1147                 :                :      * Search for the end of the current element; a comma or end-of-string
                               1148                 :                :      * acts as a terminator.
                               1149                 :                :      */
                               1150                 :          27946 :     e = s;
                               1151   [ +  +  +  + ]:         328607 :     while (*e != '\0' && *e != ',')
                               1152                 :         300661 :         ++e;
                               1153                 :          27946 :     *more = (*e == ',');
                               1154                 :                : 
                               1155                 :          27946 :     len = e - s;
                               1156                 :          27946 :     p = (char *) malloc(sizeof(char) * (len + 1));
                               1157         [ +  - ]:          27946 :     if (p)
                               1158                 :                :     {
                               1159                 :          27946 :         memcpy(p, s, len);
                               1160                 :          27946 :         p[len] = '\0';
                               1161                 :                :     }
                               1162                 :          27946 :     *startptr = e + 1;
                               1163                 :                : 
                               1164                 :          27946 :     return p;
                               1165                 :                : }
                               1166                 :                : 
                               1167                 :                : /*
                               1168                 :                :  * Initializes the prng_state field of the connection. We want something
                               1169                 :                :  * unpredictable, so if possible, use high-quality random bits for the
                               1170                 :                :  * seed. Otherwise, fall back to a seed based on the connection address,
                               1171                 :                :  * timestamp and PID.
                               1172                 :                :  */
                               1173                 :                : static void
  993 dgustafsson@postgres     1174                 :             55 : libpq_prng_init(PGconn *conn)
                               1175                 :                : {
                               1176                 :                :     uint64      rseed;
                               1177                 :             55 :     struct timeval tval = {0};
                               1178                 :                : 
                               1179   [ +  -  +  - ]:             55 :     if (pg_prng_strong_seed(&conn->prng_state))
                               1180                 :             55 :         return;
                               1181                 :                : 
  993 dgustafsson@postgres     1182                 :UBC           0 :     gettimeofday(&tval, NULL);
                               1183                 :                : 
  992                          1184                 :              0 :     rseed = ((uintptr_t) conn) ^
  942 tgl@sss.pgh.pa.us        1185                 :              0 :         ((uint64) getpid()) ^
                               1186                 :              0 :         ((uint64) tval.tv_usec) ^
                               1187                 :              0 :         ((uint64) tval.tv_sec);
                               1188                 :                : 
  993 dgustafsson@postgres     1189                 :              0 :     pg_prng_seed(&conn->prng_state, rseed);
                               1190                 :                : }
                               1191                 :                : 
                               1192                 :                : /*
                               1193                 :                :  * Fills the connection's allowed_sasl_mechs list with all supported SASL
                               1194                 :                :  * mechanisms.
                               1195                 :                :  */
                               1196                 :                : static inline void
  319 dgustafsson@postgres     1197                 :CBC          25 : fill_allowed_sasl_mechs(PGconn *conn)
                               1198                 :                : {
                               1199                 :                :     /*---
                               1200                 :                :      * We only support two mechanisms at the moment, so rather than deal with a
                               1201                 :                :      * linked list, conn->allowed_sasl_mechs is an array of static length. We
                               1202                 :                :      * rely on the compile-time assertion here to keep us honest.
                               1203                 :                :      *
                               1204                 :                :      * To add a new mechanism to require_auth,
                               1205                 :                :      * - add it to supported_sasl_mechs,
                               1206                 :                :      * - update the length of conn->allowed_sasl_mechs,
                               1207                 :                :      * - handle the new mechanism name in the require_auth portion of
                               1208                 :                :      *   pqConnectOptions2(), below.
                               1209                 :                :      */
                               1210                 :                :     StaticAssertDecl(lengthof(conn->allowed_sasl_mechs) == SASL_MECHANISM_COUNT,
                               1211                 :                :                      "conn->allowed_sasl_mechs[] is not sufficiently large for holding all supported SASL mechanisms");
                               1212                 :                : 
                               1213         [ +  + ]:             75 :     for (int i = 0; i < SASL_MECHANISM_COUNT; i++)
                               1214                 :             50 :         conn->allowed_sasl_mechs[i] = supported_sasl_mechs[i];
                               1215                 :             25 : }
                               1216                 :                : 
                               1217                 :                : /*
                               1218                 :                :  * Clears the connection's allowed_sasl_mechs list.
                               1219                 :                :  */
                               1220                 :                : static inline void
                               1221                 :             73 : clear_allowed_sasl_mechs(PGconn *conn)
                               1222                 :                : {
                               1223         [ +  + ]:            219 :     for (int i = 0; i < lengthof(conn->allowed_sasl_mechs); i++)
                               1224                 :            146 :         conn->allowed_sasl_mechs[i] = NULL;
                               1225                 :             73 : }
                               1226                 :                : 
                               1227                 :                : /*
                               1228                 :                :  * Helper routine that searches the static allowed_sasl_mechs list for a
                               1229                 :                :  * specific mechanism.
                               1230                 :                :  */
                               1231                 :                : static inline int
                               1232                 :             65 : index_of_allowed_sasl_mech(PGconn *conn, const pg_fe_sasl_mech *mech)
                               1233                 :                : {
                               1234         [ +  + ]:            121 :     for (int i = 0; i < lengthof(conn->allowed_sasl_mechs); i++)
                               1235                 :                :     {
                               1236         [ +  + ]:             95 :         if (conn->allowed_sasl_mechs[i] == mech)
                               1237                 :             39 :             return i;
                               1238                 :                :     }
                               1239                 :                : 
                               1240                 :             26 :     return -1;
                               1241                 :                : }
                               1242                 :                : 
                               1243                 :                : /*
                               1244                 :                :  *      pqConnectOptions2
                               1245                 :                :  *
                               1246                 :                :  * Compute derived connection options after absorbing all user-supplied info.
                               1247                 :                :  *
                               1248                 :                :  * Returns true if OK, false if trouble (in which case errorMessage is set
                               1249                 :                :  * and so is conn->status).
                               1250                 :                :  */
                               1251                 :                : bool
  681 alvherre@alvh.no-ip.     1252                 :          13580 : pqConnectOptions2(PGconn *conn)
                               1253                 :                : {
                               1254                 :                :     int         i;
                               1255                 :                : 
                               1256                 :                :     /*
                               1257                 :                :      * Allocate memory for details about each host to which we might possibly
                               1258                 :                :      * try to connect.  For that, count the number of elements in the hostaddr
                               1259                 :                :      * or host options.  If neither is given, assume one host.
                               1260                 :                :      */
 3330 rhaas@postgresql.org     1261                 :          13580 :     conn->whichhost = 0;
 3081 heikki.linnakangas@i     1262   [ +  +  +  - ]:          13580 :     if (conn->pghostaddr && conn->pghostaddr[0] != '\0')
                               1263                 :            413 :         conn->nconnhost = count_comma_separated_elems(conn->pghostaddr);
                               1264   [ +  -  +  - ]:          13167 :     else if (conn->pghost && conn->pghost[0] != '\0')
                               1265                 :          13167 :         conn->nconnhost = count_comma_separated_elems(conn->pghost);
                               1266                 :                :     else
 3081 heikki.linnakangas@i     1267                 :UBC           0 :         conn->nconnhost = 1;
 3330 rhaas@postgresql.org     1268                 :CBC       13580 :     conn->connhost = (pg_conn_host *)
                               1269                 :          13580 :         calloc(conn->nconnhost, sizeof(pg_conn_host));
                               1270         [ -  + ]:          13580 :     if (conn->connhost == NULL)
 3330 rhaas@postgresql.org     1271                 :UBC           0 :         goto oom_error;
                               1272                 :                : 
                               1273                 :                :     /*
                               1274                 :                :      * We now have one pg_conn_host structure per possible host.  Fill in the
                               1275                 :                :      * host and hostaddr fields for each, by splitting the parameter strings.
                               1276                 :                :      */
 3330 rhaas@postgresql.org     1277   [ +  +  +  - ]:CBC       13580 :     if (conn->pghostaddr != NULL && conn->pghostaddr[0] != '\0')
                               1278                 :                :     {
 3081 heikki.linnakangas@i     1279                 :            413 :         char       *s = conn->pghostaddr;
                               1280                 :            413 :         bool        more = true;
                               1281                 :                : 
                               1282   [ +  +  +  - ]:            826 :         for (i = 0; i < conn->nconnhost && more; i++)
                               1283                 :                :         {
                               1284                 :            413 :             conn->connhost[i].hostaddr = parse_comma_separated_list(&s, &more);
                               1285         [ -  + ]:            413 :             if (conn->connhost[i].hostaddr == NULL)
 3081 heikki.linnakangas@i     1286                 :UBC           0 :                 goto oom_error;
                               1287                 :                :         }
                               1288                 :                : 
                               1289                 :                :         /*
                               1290                 :                :          * If hostaddr was given, the array was allocated according to the
                               1291                 :                :          * number of elements in the hostaddr list, so it really should be the
                               1292                 :                :          * right size.
                               1293                 :                :          */
 3081 heikki.linnakangas@i     1294         [ -  + ]:CBC         413 :         Assert(!more);
                               1295         [ -  + ]:            413 :         Assert(i == conn->nconnhost);
                               1296                 :                :     }
                               1297                 :                : 
                               1298   [ +  -  +  - ]:          13580 :     if (conn->pghost != NULL && conn->pghost[0] != '\0')
                               1299                 :                :     {
 3135 bruce@momjian.us         1300                 :          13580 :         char       *s = conn->pghost;
 3081 heikki.linnakangas@i     1301                 :          13580 :         bool        more = true;
                               1302                 :                : 
                               1303   [ +  +  +  - ]:          27293 :         for (i = 0; i < conn->nconnhost && more; i++)
                               1304                 :                :         {
                               1305                 :          13713 :             conn->connhost[i].host = parse_comma_separated_list(&s, &more);
 3330 rhaas@postgresql.org     1306         [ -  + ]:          13713 :             if (conn->connhost[i].host == NULL)
 3330 rhaas@postgresql.org     1307                 :UBC           0 :                 goto oom_error;
                               1308                 :                :         }
                               1309                 :                : 
                               1310                 :                :         /* Check for wrong number of host items. */
 3081 heikki.linnakangas@i     1311   [ +  -  -  + ]:CBC       13580 :         if (more || i != conn->nconnhost)
                               1312                 :                :         {
 3081 heikki.linnakangas@i     1313                 :UBC           0 :             conn->status = CONNECTION_BAD;
 1127 peter@eisentraut.org     1314                 :              0 :             libpq_append_conn_error(conn, "could not match %d host names to %d hostaddr values",
 1013 michael@paquier.xyz      1315                 :              0 :                                     count_comma_separated_elems(conn->pghost), conn->nconnhost);
 3081 heikki.linnakangas@i     1316                 :              0 :             return false;
                               1317                 :                :         }
                               1318                 :                :     }
                               1319                 :                : 
                               1320                 :                :     /*
                               1321                 :                :      * Now, for each host slot, identify the type of address spec, and fill in
                               1322                 :                :      * the default address if nothing was given.
                               1323                 :                :      */
 2694 tgl@sss.pgh.pa.us        1324         [ +  + ]:CBC       27293 :     for (i = 0; i < conn->nconnhost; i++)
                               1325                 :                :     {
                               1326                 :          13713 :         pg_conn_host *ch = &conn->connhost[i];
                               1327                 :                : 
                               1328   [ +  +  +  - ]:          13713 :         if (ch->hostaddr != NULL && ch->hostaddr[0] != '\0')
                               1329                 :            413 :             ch->type = CHT_HOST_ADDRESS;
                               1330   [ +  -  +  - ]:          13300 :         else if (ch->host != NULL && ch->host[0] != '\0')
                               1331                 :                :         {
                               1332                 :          13300 :             ch->type = CHT_HOST_NAME;
 1847 peter@eisentraut.org     1333         [ +  - ]:          26600 :             if (is_unixsock_path(ch->host))
 2694 tgl@sss.pgh.pa.us        1334                 :          13300 :                 ch->type = CHT_UNIX_SOCKET;
                               1335                 :                :         }
                               1336                 :                :         else
                               1337                 :                :         {
 1279 peter@eisentraut.org     1338                 :UBC           0 :             free(ch->host);
                               1339                 :                : 
                               1340                 :                :             /*
                               1341                 :                :              * This bit selects the default host location.  If you change
                               1342                 :                :              * this, see also pg_regress.
                               1343                 :                :              */
 2146                          1344         [ #  # ]:              0 :             if (DEFAULT_PGSOCKET_DIR[0])
                               1345                 :                :             {
                               1346                 :              0 :                 ch->host = strdup(DEFAULT_PGSOCKET_DIR);
                               1347                 :              0 :                 ch->type = CHT_UNIX_SOCKET;
                               1348                 :                :             }
                               1349                 :                :             else
                               1350                 :                :             {
                               1351                 :              0 :                 ch->host = strdup(DefaultHost);
                               1352                 :              0 :                 ch->type = CHT_HOST_NAME;
                               1353                 :                :             }
 2694 tgl@sss.pgh.pa.us        1354         [ #  # ]:              0 :             if (ch->host == NULL)
                               1355                 :              0 :                 goto oom_error;
                               1356                 :                :         }
                               1357                 :                :     }
                               1358                 :                : 
                               1359                 :                :     /*
                               1360                 :                :      * Next, work out the port number corresponding to each host name.
                               1361                 :                :      *
                               1362                 :                :      * Note: unlike the above for host names, this could leave the port fields
                               1363                 :                :      * as null or empty strings.  We will substitute DEF_PGPORT whenever we
                               1364                 :                :      * read such a port field.
                               1365                 :                :      */
 3330 rhaas@postgresql.org     1366   [ +  -  +  - ]:CBC       13580 :     if (conn->pgport != NULL && conn->pgport[0] != '\0')
                               1367                 :                :     {
 3135 bruce@momjian.us         1368                 :          13580 :         char       *s = conn->pgport;
 3081 heikki.linnakangas@i     1369                 :          13580 :         bool        more = true;
                               1370                 :                : 
                               1371   [ +  +  +  - ]:          27293 :         for (i = 0; i < conn->nconnhost && more; i++)
                               1372                 :                :         {
                               1373                 :          13713 :             conn->connhost[i].port = parse_comma_separated_list(&s, &more);
                               1374         [ -  + ]:          13713 :             if (conn->connhost[i].port == NULL)
 3081 heikki.linnakangas@i     1375                 :UBC           0 :                 goto oom_error;
                               1376                 :                :         }
                               1377                 :                : 
                               1378                 :                :         /*
                               1379                 :                :          * If exactly one port was given, use it for every host.  Otherwise,
                               1380                 :                :          * there must be exactly as many ports as there were hosts.
                               1381                 :                :          */
 3081 heikki.linnakangas@i     1382   [ +  +  +  - ]:CBC       13580 :         if (i == 1 && !more)
                               1383                 :                :         {
                               1384         [ -  + ]:          13505 :             for (i = 1; i < conn->nconnhost; i++)
                               1385                 :                :             {
 3081 heikki.linnakangas@i     1386                 :UBC           0 :                 conn->connhost[i].port = strdup(conn->connhost[0].port);
 3330 rhaas@postgresql.org     1387         [ #  # ]:              0 :                 if (conn->connhost[i].port == NULL)
                               1388                 :              0 :                     goto oom_error;
                               1389                 :                :             }
                               1390                 :                :         }
 3081 heikki.linnakangas@i     1391   [ +  -  -  + ]:CBC          75 :         else if (more || i != conn->nconnhost)
                               1392                 :                :         {
 3330 rhaas@postgresql.org     1393                 :UBC           0 :             conn->status = CONNECTION_BAD;
 1127 peter@eisentraut.org     1394                 :              0 :             libpq_append_conn_error(conn, "could not match %d port numbers to %d hosts",
 1013 michael@paquier.xyz      1395                 :              0 :                                     count_comma_separated_elems(conn->pgport), conn->nconnhost);
 3330 rhaas@postgresql.org     1396                 :              0 :             return false;
                               1397                 :                :         }
                               1398                 :                :     }
                               1399                 :                : 
                               1400                 :                :     /*
                               1401                 :                :      * If user name was not given, fetch it.  (Most likely, the fetch will
                               1402                 :                :      * fail, since the only way we get here is if pg_fe_getauthname() failed
                               1403                 :                :      * during conninfo_add_defaults().  But now we want an error message.)
                               1404                 :                :      */
 3992 tgl@sss.pgh.pa.us        1405   [ +  -  -  + ]:CBC       13580 :     if (conn->pguser == NULL || conn->pguser[0] == '\0')
                               1406                 :                :     {
 1279 peter@eisentraut.org     1407                 :UBC           0 :         free(conn->pguser);
 3992 tgl@sss.pgh.pa.us        1408                 :              0 :         conn->pguser = pg_fe_getauthname(&conn->errorMessage);
                               1409         [ #  # ]:              0 :         if (!conn->pguser)
                               1410                 :                :         {
                               1411                 :              0 :             conn->status = CONNECTION_BAD;
                               1412                 :              0 :             return false;
                               1413                 :                :         }
                               1414                 :                :     }
                               1415                 :                : 
                               1416                 :                :     /*
                               1417                 :                :      * If database name was not given, default it to equal user name
                               1418                 :                :      */
 3992 tgl@sss.pgh.pa.us        1419   [ +  +  +  + ]:CBC       13580 :     if (conn->dbName == NULL || conn->dbName[0] == '\0')
                               1420                 :                :     {
 1279 peter@eisentraut.org     1421                 :              3 :         free(conn->dbName);
 8261 tgl@sss.pgh.pa.us        1422                 :              3 :         conn->dbName = strdup(conn->pguser);
 4039 heikki.linnakangas@i     1423         [ -  + ]:              3 :         if (!conn->dbName)
 4039 heikki.linnakangas@i     1424                 :UBC           0 :             goto oom_error;
                               1425                 :                :     }
                               1426                 :                : 
                               1427                 :                :     /*
                               1428                 :                :      * If password was not given, try to look it up in password file.  Note
                               1429                 :                :      * that the result might be different for each host/port pair.
                               1430                 :                :      */
 8268 tgl@sss.pgh.pa.us        1431   [ +  +  +  + ]:CBC       13580 :     if (conn->pgpass == NULL || conn->pgpass[0] == '\0')
                               1432                 :                :     {
                               1433                 :                :         /* If password file wasn't specified, use ~/PGPASSFILE */
 3248                          1434   [ +  +  -  + ]:          13394 :         if (conn->pgpassfile == NULL || conn->pgpassfile[0] == '\0')
                               1435                 :                :         {
                               1436                 :                :             char        homedir[MAXPGPATH];
                               1437                 :                : 
 2974                          1438         [ +  - ]:          13199 :             if (pqGetHomeDirectory(homedir, sizeof(homedir)))
                               1439                 :                :             {
 1279 peter@eisentraut.org     1440                 :          13199 :                 free(conn->pgpassfile);
 2974 tgl@sss.pgh.pa.us        1441                 :          13199 :                 conn->pgpassfile = malloc(MAXPGPATH);
                               1442         [ -  + ]:          13199 :                 if (!conn->pgpassfile)
 2974 tgl@sss.pgh.pa.us        1443                 :UBC           0 :                     goto oom_error;
 2974 tgl@sss.pgh.pa.us        1444                 :CBC       13199 :                 snprintf(conn->pgpassfile, MAXPGPATH, "%s/%s",
                               1445                 :                :                          homedir, PGPASSFILE);
                               1446                 :                :             }
                               1447                 :                :         }
                               1448                 :                : 
                               1449   [ +  -  +  - ]:          13394 :         if (conn->pgpassfile != NULL && conn->pgpassfile[0] != '\0')
                               1450                 :                :         {
                               1451         [ +  + ]:          26921 :             for (i = 0; i < conn->nconnhost; i++)
                               1452                 :                :             {
                               1453                 :                :                 /*
                               1454                 :                :                  * Try to get a password for this host from file.  We use host
                               1455                 :                :                  * for the hostname search key if given, else hostaddr (at
                               1456                 :                :                  * least one of them is guaranteed nonempty by now).
                               1457                 :                :                  */
 2694                          1458                 :          13527 :                 const char *pwhost = conn->connhost[i].host;
   42 michael@paquier.xyz      1459                 :GNC       13527 :                 const char *password_errmsg = NULL;
                               1460                 :                : 
 2694 tgl@sss.pgh.pa.us        1461   [ +  -  -  + ]:CBC       13527 :                 if (pwhost == NULL || pwhost[0] == '\0')
 2974 tgl@sss.pgh.pa.us        1462                 :UBC           0 :                     pwhost = conn->connhost[i].hostaddr;
                               1463                 :                : 
 2974 tgl@sss.pgh.pa.us        1464                 :CBC       27054 :                 conn->connhost[i].password =
                               1465                 :          13527 :                     passwordFromFile(pwhost,
                               1466                 :          13527 :                                      conn->connhost[i].port,
                               1467                 :          13527 :                                      conn->dbName,
                               1468                 :          13527 :                                      conn->pguser,
   42 michael@paquier.xyz      1469                 :GNC       13527 :                                      conn->pgpassfile,
                               1470                 :                :                                      &password_errmsg);
                               1471                 :                : 
                               1472         [ -  + ]:          13527 :                 if (password_errmsg != NULL)
                               1473                 :                :                 {
   42 michael@paquier.xyz      1474                 :UNC           0 :                     conn->status = CONNECTION_BAD;
                               1475                 :              0 :                     libpq_append_conn_error(conn, "%s", password_errmsg);
                               1476                 :              0 :                     return false;
                               1477                 :                :                 }
                               1478                 :                :             }
                               1479                 :                :         }
                               1480                 :                :     }
                               1481                 :                : 
                               1482                 :                :     /*
                               1483                 :                :      * parse and validate require_auth option
                               1484                 :                :      */
 1008 michael@paquier.xyz      1485   [ +  +  +  + ]:CBC       13580 :     if (conn->require_auth && conn->require_auth[0])
                               1486                 :                :     {
                               1487                 :             73 :         char       *s = conn->require_auth;
                               1488                 :                :         bool        first,
                               1489                 :                :                     more;
                               1490                 :             73 :         bool        negated = false;
                               1491                 :                : 
                               1492                 :                :         /*
                               1493                 :                :          * By default, start from an empty set of allowed methods and
                               1494                 :                :          * mechanisms, and add to it.
                               1495                 :                :          */
                               1496                 :             73 :         conn->auth_required = true;
                               1497                 :             73 :         conn->allowed_auth_methods = 0;
  319 dgustafsson@postgres     1498                 :             73 :         clear_allowed_sasl_mechs(conn);
                               1499                 :                : 
 1008 michael@paquier.xyz      1500         [ +  + ]:            171 :         for (first = true, more = true; more; first = false)
                               1501                 :             83 :         {
                               1502                 :                :             char       *method,
                               1503                 :                :                        *part;
  319 dgustafsson@postgres     1504                 :            107 :             uint32      bits = 0;
                               1505                 :            107 :             const pg_fe_sasl_mech *mech = NULL;
                               1506                 :                : 
 1008 michael@paquier.xyz      1507                 :            107 :             part = parse_comma_separated_list(&s, &more);
                               1508         [ -  + ]:            107 :             if (part == NULL)
 1008 michael@paquier.xyz      1509                 :UBC           0 :                 goto oom_error;
                               1510                 :                : 
                               1511                 :                :             /*
                               1512                 :                :              * Check for negation, e.g. '!password'. If one element is
                               1513                 :                :              * negated, they all have to be.
                               1514                 :                :              */
 1008 michael@paquier.xyz      1515                 :CBC         107 :             method = part;
                               1516         [ +  + ]:            107 :             if (*method == '!')
                               1517                 :                :             {
                               1518         [ +  + ]:             41 :                 if (first)
                               1519                 :                :                 {
                               1520                 :                :                     /*
                               1521                 :                :                      * Switch to a permissive set of allowed methods and
                               1522                 :                :                      * mechanisms, and subtract from it.
                               1523                 :                :                      */
                               1524                 :             25 :                     conn->auth_required = false;
                               1525                 :             25 :                     conn->allowed_auth_methods = -1;
  319 dgustafsson@postgres     1526                 :             25 :                     fill_allowed_sasl_mechs(conn);
                               1527                 :                :                 }
 1008 michael@paquier.xyz      1528         [ +  + ]:             16 :                 else if (!negated)
                               1529                 :                :                 {
                               1530                 :              1 :                     conn->status = CONNECTION_BAD;
                               1531                 :              1 :                     libpq_append_conn_error(conn, "negative require_auth method \"%s\" cannot be mixed with non-negative methods",
                               1532                 :                :                                             method);
                               1533                 :                : 
                               1534                 :              1 :                     free(part);
                               1535                 :              9 :                     return false;
                               1536                 :                :                 }
                               1537                 :                : 
                               1538                 :             40 :                 negated = true;
                               1539                 :             40 :                 method++;
                               1540                 :                :             }
                               1541         [ +  + ]:             66 :             else if (negated)
                               1542                 :                :             {
                               1543                 :              1 :                 conn->status = CONNECTION_BAD;
                               1544                 :              1 :                 libpq_append_conn_error(conn, "require_auth method \"%s\" cannot be mixed with negative methods",
                               1545                 :                :                                         method);
                               1546                 :                : 
                               1547                 :              1 :                 free(part);
                               1548                 :              1 :                 return false;
                               1549                 :                :             }
                               1550                 :                : 
                               1551                 :                :             /*
                               1552                 :                :              * First group: methods that can be handled solely with the
                               1553                 :                :              * authentication request codes.
                               1554                 :                :              */
                               1555         [ +  + ]:            105 :             if (strcmp(method, "password") == 0)
                               1556                 :                :             {
                               1557                 :             21 :                 bits = (1 << AUTH_REQ_PASSWORD);
                               1558                 :                :             }
                               1559         [ +  + ]:             84 :             else if (strcmp(method, "md5") == 0)
                               1560                 :                :             {
                               1561                 :             16 :                 bits = (1 << AUTH_REQ_MD5);
                               1562                 :                :             }
                               1563         [ +  + ]:             68 :             else if (strcmp(method, "gss") == 0)
                               1564                 :                :             {
                               1565                 :              6 :                 bits = (1 << AUTH_REQ_GSS);
                               1566                 :              6 :                 bits |= (1 << AUTH_REQ_GSS_CONT);
                               1567                 :                :             }
                               1568         [ +  + ]:             62 :             else if (strcmp(method, "sspi") == 0)
                               1569                 :                :             {
                               1570                 :              4 :                 bits = (1 << AUTH_REQ_SSPI);
                               1571                 :              4 :                 bits |= (1 << AUTH_REQ_GSS_CONT);
                               1572                 :                :             }
                               1573                 :                : 
                               1574                 :                :             /*
                               1575                 :                :              * Next group: SASL mechanisms. All of these use the same request
                               1576                 :                :              * codes, so the list of allowed mechanisms is tracked separately.
                               1577                 :                :              *
                               1578                 :                :              * supported_sasl_mechs must contain all mechanisms handled here.
                               1579                 :                :              */
                               1580         [ +  + ]:             58 :             else if (strcmp(method, "scram-sha-256") == 0)
                               1581                 :                :             {
  319 dgustafsson@postgres     1582                 :             33 :                 mech = &pg_scram_mech;
                               1583                 :                :             }
  299                          1584         [ +  + ]:             25 :             else if (strcmp(method, "oauth") == 0)
                               1585                 :                :             {
                               1586                 :              7 :                 mech = &pg_oauth_mech;
                               1587                 :                :             }
                               1588                 :                : 
                               1589                 :                :             /*
                               1590                 :                :              * Final group: meta-options.
                               1591                 :                :              */
 1008 michael@paquier.xyz      1592         [ +  + ]:             18 :             else if (strcmp(method, "none") == 0)
                               1593                 :                :             {
                               1594                 :                :                 /*
                               1595                 :                :                  * Special case: let the user explicitly allow (or disallow)
                               1596                 :                :                  * connections where the server does not send an explicit
                               1597                 :                :                  * authentication challenge, such as "trust" and "cert" auth.
                               1598                 :                :                  */
                               1599         [ +  + ]:             17 :                 if (negated)    /* "!none" */
                               1600                 :                :                 {
                               1601         [ +  + ]:              8 :                     if (conn->auth_required)
                               1602                 :              1 :                         goto duplicate;
                               1603                 :                : 
                               1604                 :              7 :                     conn->auth_required = true;
                               1605                 :                :                 }
                               1606                 :                :                 else            /* "none" */
                               1607                 :                :                 {
                               1608         [ +  + ]:              9 :                     if (!conn->auth_required)
                               1609                 :              1 :                         goto duplicate;
                               1610                 :                : 
                               1611                 :              8 :                     conn->auth_required = false;
                               1612                 :                :                 }
                               1613                 :                : 
                               1614                 :             15 :                 free(part);
                               1615                 :             15 :                 continue;       /* avoid the bitmask manipulation below */
                               1616                 :                :             }
                               1617                 :                :             else
                               1618                 :                :             {
                               1619                 :              1 :                 conn->status = CONNECTION_BAD;
  970 dgustafsson@postgres     1620                 :              1 :                 libpq_append_conn_error(conn, "invalid %s value: \"%s\"",
                               1621                 :                :                                         "require_auth", method);
                               1622                 :                : 
 1008 michael@paquier.xyz      1623                 :              1 :                 free(part);
                               1624                 :              1 :                 return false;
                               1625                 :                :             }
                               1626                 :                : 
  319 dgustafsson@postgres     1627         [ +  + ]:             87 :             if (mech)
                               1628                 :                :             {
                               1629                 :                :                 /*
                               1630                 :                :                  * Update the mechanism set only. The method bitmask will be
                               1631                 :                :                  * updated for SASL further down.
                               1632                 :                :                  */
                               1633         [ -  + ]:             40 :                 Assert(!bits);
                               1634                 :                : 
                               1635         [ +  + ]:             40 :                 if (negated)
                               1636                 :                :                 {
                               1637                 :                :                     /* Remove the existing mechanism from the list. */
                               1638                 :             14 :                     i = index_of_allowed_sasl_mech(conn, mech);
                               1639         [ +  + ]:             14 :                     if (i < 0)
                               1640                 :              1 :                         goto duplicate;
                               1641                 :                : 
                               1642                 :             13 :                     conn->allowed_sasl_mechs[i] = NULL;
                               1643                 :                :                 }
                               1644                 :                :                 else
                               1645                 :                :                 {
                               1646                 :                :                     /*
                               1647                 :                :                      * Find a space to put the new mechanism (after making
                               1648                 :                :                      * sure it's not already there).
                               1649                 :                :                      */
                               1650                 :             26 :                     i = index_of_allowed_sasl_mech(conn, mech);
                               1651         [ +  + ]:             26 :                     if (i >= 0)
                               1652                 :              1 :                         goto duplicate;
                               1653                 :                : 
                               1654                 :             25 :                     i = index_of_allowed_sasl_mech(conn, NULL);
                               1655         [ -  + ]:             25 :                     if (i < 0)
                               1656                 :                :                     {
                               1657                 :                :                         /* Should not happen; the pointer list is corrupted. */
  319 dgustafsson@postgres     1658                 :UBC           0 :                         Assert(false);
                               1659                 :                : 
                               1660                 :                :                         conn->status = CONNECTION_BAD;
                               1661                 :                :                         libpq_append_conn_error(conn,
                               1662                 :                :                                                 "internal error: no space in allowed_sasl_mechs");
                               1663                 :                :                         free(part);
                               1664                 :                :                         return false;
                               1665                 :                :                     }
                               1666                 :                : 
  319 dgustafsson@postgres     1667                 :CBC          25 :                     conn->allowed_sasl_mechs[i] = mech;
                               1668                 :                :                 }
                               1669                 :                :             }
                               1670                 :                :             else
                               1671                 :                :             {
                               1672                 :                :                 /* Update the method bitmask. */
                               1673         [ -  + ]:             47 :                 Assert(bits);
                               1674                 :                : 
                               1675         [ +  + ]:             47 :                 if (negated)
                               1676                 :                :                 {
                               1677         [ +  + ]:             18 :                     if ((conn->allowed_auth_methods & bits) == 0)
                               1678                 :              1 :                         goto duplicate;
                               1679                 :                : 
                               1680                 :             17 :                     conn->allowed_auth_methods &= ~bits;
                               1681                 :                :                 }
                               1682                 :                :                 else
                               1683                 :                :                 {
                               1684         [ +  + ]:             29 :                     if ((conn->allowed_auth_methods & bits) == bits)
                               1685                 :              1 :                         goto duplicate;
                               1686                 :                : 
                               1687                 :             28 :                     conn->allowed_auth_methods |= bits;
                               1688                 :                :                 }
                               1689                 :                :             }
                               1690                 :                : 
 1008 michael@paquier.xyz      1691                 :             83 :             free(part);
                               1692                 :             83 :             continue;
                               1693                 :                : 
                               1694                 :              6 :     duplicate:
                               1695                 :                : 
                               1696                 :                :             /*
                               1697                 :                :              * A duplicated method probably indicates a typo in a setting
                               1698                 :                :              * where typos are extremely risky.
                               1699                 :                :              */
                               1700                 :              6 :             conn->status = CONNECTION_BAD;
                               1701                 :              6 :             libpq_append_conn_error(conn, "require_auth method \"%s\" is specified more than once",
                               1702                 :                :                                     part);
                               1703                 :                : 
                               1704                 :              6 :             free(part);
                               1705                 :              6 :             return false;
                               1706                 :                :         }
                               1707                 :                : 
                               1708                 :                :         /*
                               1709                 :                :          * Finally, allow SASL authentication requests if (and only if) we've
                               1710                 :                :          * allowed any mechanisms.
                               1711                 :                :          */
                               1712                 :                :         {
  319 dgustafsson@postgres     1713                 :             64 :             bool        allowed = false;
                               1714                 :             64 :             const uint32 sasl_bits =
                               1715                 :                :                 (1 << AUTH_REQ_SASL)
                               1716                 :                :                 | (1 << AUTH_REQ_SASL_CONT)
                               1717                 :                :                 | (1 << AUTH_REQ_SASL_FIN);
                               1718                 :                : 
                               1719         [ +  + ]:            116 :             for (i = 0; i < lengthof(conn->allowed_sasl_mechs); i++)
                               1720                 :                :             {
                               1721         [ +  + ]:             94 :                 if (conn->allowed_sasl_mechs[i])
                               1722                 :                :                 {
                               1723                 :             42 :                     allowed = true;
                               1724                 :             42 :                     break;
                               1725                 :                :                 }
                               1726                 :                :             }
                               1727                 :                : 
                               1728                 :                :             /*
                               1729                 :                :              * For the standard case, add the SASL bits to the (default-empty)
                               1730                 :                :              * set if needed. For the negated case, remove them.
                               1731                 :                :              */
                               1732   [ +  +  +  + ]:             64 :             if (!negated && allowed)
                               1733                 :             22 :                 conn->allowed_auth_methods |= sasl_bits;
                               1734   [ +  +  +  + ]:             42 :             else if (negated && !allowed)
                               1735                 :              1 :                 conn->allowed_auth_methods &= ~sasl_bits;
                               1736                 :                :         }
                               1737                 :                :     }
                               1738                 :                : 
                               1739                 :                :     /*
                               1740                 :                :      * validate channel_binding option
                               1741                 :                :      */
 2276 jdavis@postgresql.or     1742         [ +  - ]:          13571 :     if (conn->channel_binding)
                               1743                 :                :     {
                               1744         [ +  + ]:          13571 :         if (strcmp(conn->channel_binding, "disable") != 0
                               1745         [ +  + ]:          13569 :             && strcmp(conn->channel_binding, "prefer") != 0
                               1746         [ +  + ]:              9 :             && strcmp(conn->channel_binding, "require") != 0)
                               1747                 :                :         {
                               1748                 :              1 :             conn->status = CONNECTION_BAD;
 1127 peter@eisentraut.org     1749                 :              1 :             libpq_append_conn_error(conn, "invalid %s value: \"%s\"",
                               1750                 :                :                                     "channel_binding", conn->channel_binding);
 2276 jdavis@postgresql.or     1751                 :              1 :             return false;
                               1752                 :                :         }
                               1753                 :                :     }
                               1754                 :                :     else
                               1755                 :                :     {
 2276 jdavis@postgresql.or     1756                 :UBC           0 :         conn->channel_binding = strdup(DefaultChannelBinding);
                               1757         [ #  # ]:              0 :         if (!conn->channel_binding)
                               1758                 :              0 :             goto oom_error;
                               1759                 :                :     }
                               1760                 :                : 
                               1761                 :                : #ifndef USE_SSL
                               1762                 :                : 
                               1763                 :                :     /*
                               1764                 :                :      * sslrootcert=system is not supported. Since setting this changes the
                               1765                 :                :      * default sslmode, check this _before_ we validate sslmode, to avoid
                               1766                 :                :      * confusing the user with errors for an option they may not have set.
                               1767                 :                :      */
                               1768                 :                :     if (conn->sslrootcert
                               1769                 :                :         && strcmp(conn->sslrootcert, "system") == 0)
                               1770                 :                :     {
                               1771                 :                :         conn->status = CONNECTION_BAD;
                               1772                 :                :         libpq_append_conn_error(conn, "%s value \"%s\" invalid when SSL support is not compiled in",
                               1773                 :                :                                 "sslrootcert", conn->sslrootcert);
                               1774                 :                :         return false;
                               1775                 :                :     }
                               1776                 :                : #endif
                               1777                 :                : 
                               1778                 :                :     /*
                               1779                 :                :      * validate sslmode option
                               1780                 :                :      */
 8179 bruce@momjian.us         1781         [ +  - ]:CBC       13570 :     if (conn->sslmode)
                               1782                 :                :     {
                               1783         [ +  + ]:          13570 :         if (strcmp(conn->sslmode, "disable") != 0
                               1784         [ +  + ]:          13500 :             && strcmp(conn->sslmode, "allow") != 0
                               1785         [ +  + ]:          13440 :             && strcmp(conn->sslmode, "prefer") != 0
 6080 magnus@hagander.net      1786         [ +  + ]:            180 :             && strcmp(conn->sslmode, "require") != 0
                               1787         [ +  + ]:             56 :             && strcmp(conn->sslmode, "verify-ca") != 0
                               1788         [ -  + ]:             39 :             && strcmp(conn->sslmode, "verify-full") != 0)
                               1789                 :                :         {
 8179 bruce@momjian.us         1790                 :UBC           0 :             conn->status = CONNECTION_BAD;
 1127 peter@eisentraut.org     1791                 :              0 :             libpq_append_conn_error(conn, "invalid %s value: \"%s\"",
                               1792                 :                :                                     "sslmode", conn->sslmode);
 8179 bruce@momjian.us         1793                 :              0 :             return false;
                               1794                 :                :         }
                               1795                 :                : 
                               1796                 :                : #ifndef USE_SSL
                               1797                 :                :         switch (conn->sslmode[0])
                               1798                 :                :         {
                               1799                 :                :             case 'a':           /* "allow" */
                               1800                 :                :             case 'p':           /* "prefer" */
                               1801                 :                : 
                               1802                 :                :                 /*
                               1803                 :                :                  * warn user that an SSL connection will never be negotiated
                               1804                 :                :                  * since SSL was not compiled in?
                               1805                 :                :                  */
                               1806                 :                :                 break;
                               1807                 :                : 
                               1808                 :                :             case 'r':           /* "require" */
                               1809                 :                :             case 'v':           /* "verify-ca" or "verify-full" */
                               1810                 :                :                 conn->status = CONNECTION_BAD;
                               1811                 :                :                 libpq_append_conn_error(conn, "%s value \"%s\" invalid when SSL support is not compiled in",
                               1812                 :                :                                         "sslmode", conn->sslmode);
                               1813                 :                :                 return false;
                               1814                 :                :         }
                               1815                 :                : #endif
                               1816                 :                :     }
                               1817                 :                :     else
                               1818                 :                :     {
  611 heikki.linnakangas@i     1819                 :              0 :         conn->sslmode = strdup(DefaultSSLMode);
                               1820         [ #  # ]:              0 :         if (!conn->sslmode)
                               1821                 :              0 :             goto oom_error;
                               1822                 :                :     }
                               1823                 :                : 
                               1824                 :                :     /*
                               1825                 :                :      * validate sslnegotiation option, default is "postgres" for the postgres
                               1826                 :                :      * style negotiated connection with an extra round trip but more options.
                               1827                 :                :      */
  617 heikki.linnakangas@i     1828         [ +  - ]:CBC       13570 :     if (conn->sslnegotiation)
                               1829                 :                :     {
                               1830         [ +  + ]:          13570 :         if (strcmp(conn->sslnegotiation, "postgres") != 0
  579                          1831         [ -  + ]:            120 :             && strcmp(conn->sslnegotiation, "direct") != 0)
                               1832                 :                :         {
  617 heikki.linnakangas@i     1833                 :UBC           0 :             conn->status = CONNECTION_BAD;
                               1834                 :              0 :             libpq_append_conn_error(conn, "invalid %s value: \"%s\"",
                               1835                 :                :                                     "sslnegotiation", conn->sslnegotiation);
                               1836                 :              0 :             return false;
                               1837                 :                :         }
                               1838                 :                : 
                               1839                 :                : #ifndef USE_SSL
                               1840                 :                :         if (conn->sslnegotiation[0] != 'p')
                               1841                 :                :         {
                               1842                 :                :             conn->status = CONNECTION_BAD;
                               1843                 :                :             libpq_append_conn_error(conn, "%s value \"%s\" invalid when SSL support is not compiled in",
                               1844                 :                :                                     "sslnegotiation", conn->sslnegotiation);
                               1845                 :                :             return false;
                               1846                 :                :         }
                               1847                 :                : #endif
                               1848                 :                : 
                               1849                 :                :         /*
                               1850                 :                :          * Don't allow direct SSL negotiation with sslmode='prefer', because
                               1851                 :                :          * that poses a risk of unintentional fallback to plaintext connection
                               1852                 :                :          * when connecting to a pre-v17 server that does not support direct
                               1853                 :                :          * SSL connections. To keep things simple, don't allow it with
                               1854                 :                :          * sslmode='allow' or sslmode='disable' either. If a user goes through
                               1855                 :                :          * the trouble of setting sslnegotiation='direct', they probably
                               1856                 :                :          * intend to use SSL, and sslmode=disable or allow is probably a user
                               1857                 :                :          * mistake anyway.
                               1858                 :                :          */
  579 heikki.linnakangas@i     1859         [ +  + ]:CBC       13570 :         if (conn->sslnegotiation[0] == 'd' &&
                               1860   [ +  +  +  - ]:            120 :             conn->sslmode[0] != 'r' && conn->sslmode[0] != 'v')
                               1861                 :                :         {
                               1862                 :             90 :             conn->status = CONNECTION_BAD;
                               1863                 :             90 :             libpq_append_conn_error(conn, "weak sslmode \"%s\" may not be used with sslnegotiation=direct (use \"require\", \"verify-ca\", or \"verify-full\")",
                               1864                 :                :                                     conn->sslmode);
                               1865                 :             90 :             return false;
                               1866                 :                :         }
                               1867                 :                :     }
                               1868                 :                :     else
                               1869                 :                :     {
  617 heikki.linnakangas@i     1870                 :UBC           0 :         conn->sslnegotiation = strdup(DefaultSSLNegotiation);
                               1871         [ #  # ]:              0 :         if (!conn->sslnegotiation)
                               1872                 :              0 :             goto oom_error;
                               1873                 :                :     }
                               1874                 :                : 
                               1875                 :                : #ifdef USE_SSL
                               1876                 :                : 
                               1877                 :                :     /*
                               1878                 :                :      * If sslrootcert=system, make sure our chosen sslmode is compatible.
                               1879                 :                :      */
  986 dgustafsson@postgres     1880         [ +  + ]:CBC       13480 :     if (conn->sslrootcert
                               1881         [ +  + ]:            126 :         && strcmp(conn->sslrootcert, "system") == 0
                               1882         [ +  + ]:              4 :         && strcmp(conn->sslmode, "verify-full") != 0)
                               1883                 :                :     {
                               1884                 :              1 :         conn->status = CONNECTION_BAD;
  942 peter@eisentraut.org     1885                 :              1 :         libpq_append_conn_error(conn, "weak sslmode \"%s\" may not be used with sslrootcert=system (use \"verify-full\")",
                               1886                 :                :                                 conn->sslmode);
  986 dgustafsson@postgres     1887                 :              1 :         return false;
                               1888                 :                :     }
                               1889                 :                : #endif
                               1890                 :                : 
                               1891                 :                :     /*
                               1892                 :                :      * Validate TLS protocol versions for ssl_min_protocol_version and
                               1893                 :                :      * ssl_max_protocol_version.
                               1894                 :                :      */
 2056 michael@paquier.xyz      1895         [ +  + ]:          13479 :     if (!sslVerifyProtocolVersion(conn->ssl_min_protocol_version))
                               1896                 :                :     {
 2144 tgl@sss.pgh.pa.us        1897                 :              1 :         conn->status = CONNECTION_BAD;
  578 peter@eisentraut.org     1898                 :              1 :         libpq_append_conn_error(conn, "invalid \"%s\" value: \"%s\"",
                               1899                 :                :                                 "ssl_min_protocol_version",
                               1900                 :                :                                 conn->ssl_min_protocol_version);
 2149 michael@paquier.xyz      1901                 :              1 :         return false;
                               1902                 :                :     }
 2056                          1903         [ +  + ]:          13478 :     if (!sslVerifyProtocolVersion(conn->ssl_max_protocol_version))
                               1904                 :                :     {
 2144 tgl@sss.pgh.pa.us        1905                 :              1 :         conn->status = CONNECTION_BAD;
  578 peter@eisentraut.org     1906                 :              1 :         libpq_append_conn_error(conn, "invalid \"%s\" value: \"%s\"",
                               1907                 :                :                                 "ssl_max_protocol_version",
                               1908                 :                :                                 conn->ssl_max_protocol_version);
 2149 michael@paquier.xyz      1909                 :              1 :         return false;
                               1910                 :                :     }
                               1911                 :                : 
                               1912                 :                :     /*
                               1913                 :                :      * Check if the range of SSL protocols defined is correct.  This is done
                               1914                 :                :      * at this early step because this is independent of the SSL
                               1915                 :                :      * implementation used, and this avoids unnecessary cycles with an
                               1916                 :                :      * already-built SSL context when the connection is being established, as
                               1917                 :                :      * it would be doomed anyway.
                               1918                 :                :      */
 2056                          1919         [ +  + ]:          13477 :     if (!sslVerifyProtocolRange(conn->ssl_min_protocol_version,
                               1920                 :          13477 :                                 conn->ssl_max_protocol_version))
                               1921                 :                :     {
 2144 tgl@sss.pgh.pa.us        1922                 :              1 :         conn->status = CONNECTION_BAD;
 1127 peter@eisentraut.org     1923                 :              1 :         libpq_append_conn_error(conn, "invalid SSL protocol version range");
 2149 michael@paquier.xyz      1924                 :              1 :         return false;
                               1925                 :                :     }
                               1926                 :                : 
                               1927                 :                :     /*
                               1928                 :                :      * validate sslcertmode option
                               1929                 :                :      */
  998                          1930         [ +  + ]:          13476 :     if (conn->sslcertmode)
                               1931                 :                :     {
                               1932         [ +  + ]:            195 :         if (strcmp(conn->sslcertmode, "disable") != 0 &&
                               1933         [ +  + ]:            192 :             strcmp(conn->sslcertmode, "allow") != 0 &&
                               1934         [ -  + ]:              3 :             strcmp(conn->sslcertmode, "require") != 0)
                               1935                 :                :         {
  998 michael@paquier.xyz      1936                 :UBC           0 :             conn->status = CONNECTION_BAD;
                               1937                 :              0 :             libpq_append_conn_error(conn, "invalid %s value: \"%s\"",
                               1938                 :                :                                     "sslcertmode", conn->sslcertmode);
                               1939                 :              0 :             return false;
                               1940                 :                :         }
                               1941                 :                : #ifndef USE_SSL
                               1942                 :                :         if (strcmp(conn->sslcertmode, "require") == 0)
                               1943                 :                :         {
                               1944                 :                :             conn->status = CONNECTION_BAD;
                               1945                 :                :             libpq_append_conn_error(conn, "%s value \"%s\" invalid when SSL support is not compiled in",
                               1946                 :                :                                     "sslcertmode", conn->sslcertmode);
                               1947                 :                :             return false;
                               1948                 :                :         }
                               1949                 :                : #endif
                               1950                 :                : #ifndef HAVE_SSL_CTX_SET_CERT_CB
                               1951                 :                : 
                               1952                 :                :         /*
                               1953                 :                :          * Without a certificate callback, the current implementation can't
                               1954                 :                :          * figure out if a certificate was actually requested, so "require" is
                               1955                 :                :          * useless.
                               1956                 :                :          */
                               1957                 :                :         if (strcmp(conn->sslcertmode, "require") == 0)
                               1958                 :                :         {
                               1959                 :                :             conn->status = CONNECTION_BAD;
                               1960                 :                :             libpq_append_conn_error(conn, "%s value \"%s\" is not supported (check OpenSSL version)",
                               1961                 :                :                                     "sslcertmode", conn->sslcertmode);
                               1962                 :                :             return false;
                               1963                 :                :         }
                               1964                 :                : #endif
                               1965                 :                :     }
                               1966                 :                :     else
                               1967                 :                :     {
  998 michael@paquier.xyz      1968                 :CBC       13281 :         conn->sslcertmode = strdup(DefaultSSLCertMode);
                               1969         [ -  + ]:          13281 :         if (!conn->sslcertmode)
  998 michael@paquier.xyz      1970                 :UBC           0 :             goto oom_error;
                               1971                 :                :     }
                               1972                 :                : 
                               1973                 :                :     /*
                               1974                 :                :      * validate gssencmode option
                               1975                 :                :      */
 2449 sfrost@snowman.net       1976         [ +  - ]:CBC       13476 :     if (conn->gssencmode)
                               1977                 :                :     {
                               1978         [ +  + ]:          13476 :         if (strcmp(conn->gssencmode, "disable") != 0 &&
                               1979         [ +  + ]:          13411 :             strcmp(conn->gssencmode, "prefer") != 0 &&
                               1980         [ -  + ]:             67 :             strcmp(conn->gssencmode, "require") != 0)
                               1981                 :                :         {
 2449 sfrost@snowman.net       1982                 :UBC           0 :             conn->status = CONNECTION_BAD;
 1127 peter@eisentraut.org     1983                 :              0 :             libpq_append_conn_error(conn, "invalid %s value: \"%s\"", "gssencmode", conn->gssencmode);
 2449 sfrost@snowman.net       1984                 :              0 :             return false;
                               1985                 :                :         }
                               1986                 :                : #ifndef ENABLE_GSS
                               1987                 :                :         if (strcmp(conn->gssencmode, "require") == 0)
                               1988                 :                :         {
                               1989                 :                :             conn->status = CONNECTION_BAD;
                               1990                 :                :             libpq_append_conn_error(conn, "gssencmode value \"%s\" invalid when GSSAPI support is not compiled in",
                               1991                 :                :                                     conn->gssencmode);
                               1992                 :                :             return false;
                               1993                 :                :         }
                               1994                 :                : #endif
                               1995                 :                :     }
                               1996                 :                :     else
                               1997                 :                :     {
                               1998                 :              0 :         conn->gssencmode = strdup(DefaultGSSMode);
                               1999         [ #  # ]:              0 :         if (!conn->gssencmode)
                               2000                 :              0 :             goto oom_error;
                               2001                 :                :     }
                               2002                 :                : 
                               2003                 :                :     /*
                               2004                 :                :      * validate target_session_attrs option, and set target_server_type
                               2005                 :                :      */
 3304 rhaas@postgresql.org     2006         [ +  - ]:CBC       13476 :     if (conn->target_session_attrs)
                               2007                 :                :     {
 1750 tgl@sss.pgh.pa.us        2008         [ +  + ]:          13476 :         if (strcmp(conn->target_session_attrs, "any") == 0)
                               2009                 :          13461 :             conn->target_server_type = SERVER_TYPE_ANY;
                               2010         [ +  + ]:             15 :         else if (strcmp(conn->target_session_attrs, "read-write") == 0)
                               2011                 :              3 :             conn->target_server_type = SERVER_TYPE_READ_WRITE;
                               2012         [ +  + ]:             12 :         else if (strcmp(conn->target_session_attrs, "read-only") == 0)
                               2013                 :              3 :             conn->target_server_type = SERVER_TYPE_READ_ONLY;
                               2014         [ +  + ]:              9 :         else if (strcmp(conn->target_session_attrs, "primary") == 0)
                               2015                 :              3 :             conn->target_server_type = SERVER_TYPE_PRIMARY;
                               2016         [ +  + ]:              6 :         else if (strcmp(conn->target_session_attrs, "standby") == 0)
                               2017                 :              3 :             conn->target_server_type = SERVER_TYPE_STANDBY;
                               2018         [ +  - ]:              3 :         else if (strcmp(conn->target_session_attrs, "prefer-standby") == 0)
                               2019                 :              3 :             conn->target_server_type = SERVER_TYPE_PREFER_STANDBY;
                               2020                 :                :         else
                               2021                 :                :         {
 3304 rhaas@postgresql.org     2022                 :UBC           0 :             conn->status = CONNECTION_BAD;
 1127 peter@eisentraut.org     2023                 :              0 :             libpq_append_conn_error(conn, "invalid %s value: \"%s\"",
                               2024                 :                :                                     "target_session_attrs",
                               2025                 :                :                                     conn->target_session_attrs);
 3304 rhaas@postgresql.org     2026                 :              0 :             return false;
                               2027                 :                :         }
                               2028                 :                :     }
                               2029                 :                :     else
 1750 tgl@sss.pgh.pa.us        2030                 :              0 :         conn->target_server_type = SERVER_TYPE_ANY;
                               2031                 :                : 
  335 peter@eisentraut.org     2032         [ +  + ]:CBC       13476 :     if (conn->scram_client_key)
                               2033                 :                :     {
                               2034                 :                :         int         len;
                               2035                 :                : 
                               2036                 :              6 :         len = pg_b64_dec_len(strlen(conn->scram_client_key));
  334                          2037                 :              6 :         conn->scram_client_key_binary = malloc(len);
                               2038         [ -  + ]:              6 :         if (!conn->scram_client_key_binary)
  334 peter@eisentraut.org     2039                 :UBC           0 :             goto oom_error;
  334 peter@eisentraut.org     2040                 :CBC           6 :         len = pg_b64_decode(conn->scram_client_key, strlen(conn->scram_client_key),
                               2041                 :                :                             conn->scram_client_key_binary, len);
                               2042         [ -  + ]:              6 :         if (len < 0)
                               2043                 :                :         {
  334 peter@eisentraut.org     2044                 :UBC           0 :             libpq_append_conn_error(conn, "invalid SCRAM client key");
                               2045                 :              0 :             return false;
                               2046                 :                :         }
  334 peter@eisentraut.org     2047         [ -  + ]:CBC           6 :         if (len != SCRAM_MAX_KEY_LEN)
                               2048                 :                :         {
  335 peter@eisentraut.org     2049                 :UBC           0 :             libpq_append_conn_error(conn, "invalid SCRAM client key length: %d", len);
                               2050                 :              0 :             return false;
                               2051                 :                :         }
  335 peter@eisentraut.org     2052                 :CBC           6 :         conn->scram_client_key_len = len;
                               2053                 :                :     }
                               2054                 :                : 
                               2055         [ +  + ]:          13476 :     if (conn->scram_server_key)
                               2056                 :                :     {
                               2057                 :                :         int         len;
                               2058                 :                : 
                               2059                 :              6 :         len = pg_b64_dec_len(strlen(conn->scram_server_key));
  334                          2060                 :              6 :         conn->scram_server_key_binary = malloc(len);
                               2061         [ -  + ]:              6 :         if (!conn->scram_server_key_binary)
  334 peter@eisentraut.org     2062                 :UBC           0 :             goto oom_error;
  334 peter@eisentraut.org     2063                 :CBC           6 :         len = pg_b64_decode(conn->scram_server_key, strlen(conn->scram_server_key),
                               2064                 :                :                             conn->scram_server_key_binary, len);
                               2065         [ -  + ]:              6 :         if (len < 0)
                               2066                 :                :         {
  334 peter@eisentraut.org     2067                 :UBC           0 :             libpq_append_conn_error(conn, "invalid SCRAM server key");
                               2068                 :              0 :             return false;
                               2069                 :                :         }
  334 peter@eisentraut.org     2070         [ -  + ]:CBC           6 :         if (len != SCRAM_MAX_KEY_LEN)
                               2071                 :                :         {
  335 peter@eisentraut.org     2072                 :UBC           0 :             libpq_append_conn_error(conn, "invalid SCRAM server key length: %d", len);
                               2073                 :              0 :             return false;
                               2074                 :                :         }
  335 peter@eisentraut.org     2075                 :CBC           6 :         conn->scram_server_key_len = len;
                               2076                 :                :     }
                               2077                 :                : 
                               2078                 :                :     /*
                               2079                 :                :      * validate load_balance_hosts option, and set load_balance_type
                               2080                 :                :      */
  993 dgustafsson@postgres     2081         [ +  - ]:          13476 :     if (conn->load_balance_hosts)
                               2082                 :                :     {
                               2083         [ +  + ]:          13476 :         if (strcmp(conn->load_balance_hosts, "disable") == 0)
                               2084                 :          13420 :             conn->load_balance_type = LOAD_BALANCE_DISABLE;
                               2085         [ +  + ]:             56 :         else if (strcmp(conn->load_balance_hosts, "random") == 0)
                               2086                 :             55 :             conn->load_balance_type = LOAD_BALANCE_RANDOM;
                               2087                 :                :         else
                               2088                 :                :         {
                               2089                 :              1 :             conn->status = CONNECTION_BAD;
                               2090                 :              1 :             libpq_append_conn_error(conn, "invalid %s value: \"%s\"",
                               2091                 :                :                                     "load_balance_hosts",
                               2092                 :                :                                     conn->load_balance_hosts);
                               2093                 :              1 :             return false;
                               2094                 :                :         }
                               2095                 :                :     }
                               2096                 :                :     else
  993 dgustafsson@postgres     2097                 :UBC           0 :         conn->load_balance_type = LOAD_BALANCE_DISABLE;
                               2098                 :                : 
  993 dgustafsson@postgres     2099         [ +  + ]:CBC       13475 :     if (conn->load_balance_type == LOAD_BALANCE_RANDOM)
                               2100                 :                :     {
                               2101                 :             55 :         libpq_prng_init(conn);
                               2102                 :                : 
                               2103                 :                :         /*
                               2104                 :                :          * This is the "inside-out" variant of the Fisher-Yates shuffle
                               2105                 :                :          * algorithm. Notionally, we append each new value to the array and
                               2106                 :                :          * then swap it with a randomly-chosen array element (possibly
                               2107                 :                :          * including itself, else we fail to generate permutations with the
                               2108                 :                :          * last integer last).  The swap step can be optimized by combining it
                               2109                 :                :          * with the insertion.
                               2110                 :                :          */
                               2111         [ +  + ]:            165 :         for (i = 1; i < conn->nconnhost; i++)
                               2112                 :                :         {
                               2113                 :            110 :             int         j = pg_prng_uint64_range(&conn->prng_state, 0, i);
                               2114                 :            110 :             pg_conn_host temp = conn->connhost[j];
                               2115                 :                : 
                               2116                 :            110 :             conn->connhost[j] = conn->connhost[i];
                               2117                 :            110 :             conn->connhost[i] = temp;
                               2118                 :                :         }
                               2119                 :                :     }
                               2120                 :                : 
  258 heikki.linnakangas@i     2121         [ -  + ]:          13475 :     if (conn->min_protocol_version)
                               2122                 :                :     {
  258 heikki.linnakangas@i     2123         [ #  # ]:UBC           0 :         if (!pqParseProtocolVersion(conn->min_protocol_version, &conn->min_pversion, conn, "min_protocol_version"))
                               2124                 :                :         {
                               2125                 :              0 :             conn->status = CONNECTION_BAD;
                               2126                 :              0 :             return false;
                               2127                 :                :         }
                               2128                 :                :     }
                               2129                 :                :     else
                               2130                 :                :     {
  258 heikki.linnakangas@i     2131                 :CBC       13475 :         conn->min_pversion = PG_PROTOCOL_EARLIEST;
                               2132                 :                :     }
                               2133                 :                : 
                               2134         [ +  + ]:          13475 :     if (conn->max_protocol_version)
                               2135                 :                :     {
                               2136         [ +  + ]:             24 :         if (!pqParseProtocolVersion(conn->max_protocol_version, &conn->max_pversion, conn, "max_protocol_version"))
                               2137                 :                :         {
                               2138                 :              1 :             conn->status = CONNECTION_BAD;
                               2139                 :              1 :             return false;
                               2140                 :                :         }
                               2141                 :                :     }
                               2142                 :                :     else
                               2143                 :                :     {
                               2144                 :                :         /*
                               2145                 :                :          * To not break connecting to older servers/poolers that do not yet
                               2146                 :                :          * support NegotiateProtocolVersion, default to the 3.0 protocol at
                               2147                 :                :          * least for a while longer. Except when min_protocol_version is set
                               2148                 :                :          * to something larger, then we might as well default to the latest.
                               2149                 :                :          */
                               2150         [ -  + ]:          13451 :         if (conn->min_pversion > PG_PROTOCOL(3, 0))
  258 heikki.linnakangas@i     2151                 :UBC           0 :             conn->max_pversion = PG_PROTOCOL_LATEST;
                               2152                 :                :         else
  258 heikki.linnakangas@i     2153                 :CBC       13451 :             conn->max_pversion = PG_PROTOCOL(3, 0);
                               2154                 :                :     }
                               2155                 :                : 
                               2156         [ -  + ]:          13474 :     if (conn->min_pversion > conn->max_pversion)
                               2157                 :                :     {
  258 heikki.linnakangas@i     2158                 :UBC           0 :         conn->status = CONNECTION_BAD;
  173 peter@eisentraut.org     2159                 :              0 :         libpq_append_conn_error(conn, "\"%s\" is greater than \"%s\"", "min_protocol_version", "max_protocol_version");
  258 heikki.linnakangas@i     2160                 :              0 :         return false;
                               2161                 :                :     }
                               2162                 :                : 
                               2163                 :                :     /*
                               2164                 :                :      * Resolve special "auto" client_encoding from the locale
                               2165                 :                :      */
 1750 tgl@sss.pgh.pa.us        2166         [ +  + ]:CBC       13474 :     if (conn->client_encoding_initial &&
                               2167         [ +  + ]:            893 :         strcmp(conn->client_encoding_initial, "auto") == 0)
                               2168                 :                :     {
                               2169                 :              3 :         free(conn->client_encoding_initial);
                               2170                 :              3 :         conn->client_encoding_initial = strdup(pg_encoding_to_char(pg_get_encoding_from_locale(NULL, true)));
                               2171         [ -  + ]:              3 :         if (!conn->client_encoding_initial)
 1750 tgl@sss.pgh.pa.us        2172                 :UBC           0 :             goto oom_error;
                               2173                 :                :     }
                               2174                 :                : 
                               2175                 :                :     /*
                               2176                 :                :      * Only if we get this far is it appropriate to try to connect. (We need a
                               2177                 :                :      * state flag, rather than just the boolean result of this function, in
                               2178                 :                :      * case someone tries to PQreset() the PGconn.)
                               2179                 :                :      */
 7246 tgl@sss.pgh.pa.us        2180                 :CBC       13474 :     conn->options_valid = true;
                               2181                 :                : 
 8268                          2182                 :          13474 :     return true;
                               2183                 :                : 
 4039 heikki.linnakangas@i     2184                 :UBC           0 : oom_error:
                               2185                 :              0 :     conn->status = CONNECTION_BAD;
 1127 peter@eisentraut.org     2186                 :              0 :     libpq_append_conn_error(conn, "out of memory");
 4039 heikki.linnakangas@i     2187                 :              0 :     return false;
                               2188                 :                : }
                               2189                 :                : 
                               2190                 :                : /*
                               2191                 :                :  *      PQconndefaults
                               2192                 :                :  *
                               2193                 :                :  * Construct a default connection options array, which identifies all the
                               2194                 :                :  * available options and shows any default values that are available from the
                               2195                 :                :  * environment etc.  On error (eg out of memory), NULL is returned.
                               2196                 :                :  *
                               2197                 :                :  * Using this function, an application may determine all possible options
                               2198                 :                :  * and their current default values.
                               2199                 :                :  *
                               2200                 :                :  * NOTE: as of PostgreSQL 7.0, the returned array is dynamically allocated
                               2201                 :                :  * and should be freed when no longer needed via PQconninfoFree().  (In prior
                               2202                 :                :  * versions, the returned array was static, but that's not thread-safe.)
                               2203                 :                :  * Pre-7.0 applications that use this function will see a small memory leak
                               2204                 :                :  * until they are updated to call PQconninfoFree.
                               2205                 :                :  */
                               2206                 :                : PQconninfoOption *
10628 bruce@momjian.us         2207                 :CBC         127 : PQconndefaults(void)
                               2208                 :                : {
                               2209                 :                :     PQExpBufferData errorBuf;
                               2210                 :                :     PQconninfoOption *connOptions;
                               2211                 :                : 
                               2212                 :                :     /* We don't actually report any errors here, but callees want a buffer */
 9604 tgl@sss.pgh.pa.us        2213                 :            127 :     initPQExpBuffer(&errorBuf);
 5173                          2214         [ -  + ]:            127 :     if (PQExpBufferDataBroken(errorBuf))
 6294 tgl@sss.pgh.pa.us        2215                 :UBC           0 :         return NULL;            /* out of memory already :-( */
                               2216                 :                : 
 5017 tgl@sss.pgh.pa.us        2217                 :CBC         127 :     connOptions = conninfo_init(&errorBuf);
                               2218         [ +  - ]:            127 :     if (connOptions != NULL)
                               2219                 :                :     {
                               2220                 :                :         /* pass NULL errorBuf to ignore errors */
 4396 bruce@momjian.us         2221         [ -  + ]:            127 :         if (!conninfo_add_defaults(connOptions, NULL))
                               2222                 :                :         {
 5017 tgl@sss.pgh.pa.us        2223                 :UBC           0 :             PQconninfoFree(connOptions);
                               2224                 :              0 :             connOptions = NULL;
                               2225                 :                :         }
                               2226                 :                :     }
                               2227                 :                : 
 9604 tgl@sss.pgh.pa.us        2228                 :CBC         127 :     termPQExpBuffer(&errorBuf);
 9411                          2229                 :            127 :     return connOptions;
                               2230                 :                : }
                               2231                 :                : 
                               2232                 :                : /* ----------------
                               2233                 :                :  *      PQsetdbLogin
                               2234                 :                :  *
                               2235                 :                :  * establishes a connection to a postgres backend through the postmaster
                               2236                 :                :  * at the specified host and port.
                               2237                 :                :  *
                               2238                 :                :  * returns a PGconn* which is needed for all subsequent libpq calls
                               2239                 :                :  *
                               2240                 :                :  * if the status field of the connection returned is CONNECTION_BAD,
                               2241                 :                :  * then only the errorMessage is likely to be useful.
                               2242                 :                :  * ----------------
                               2243                 :                :  */
                               2244                 :                : PGconn *
 9468 tgl@sss.pgh.pa.us        2245                 :UBC           0 : PQsetdbLogin(const char *pghost, const char *pgport, const char *pgoptions,
                               2246                 :                :              const char *pgtty, const char *dbName, const char *login,
                               2247                 :                :              const char *pwd)
                               2248                 :                : {
                               2249                 :                :     PGconn     *conn;
                               2250                 :                : 
                               2251                 :                :     /*
                               2252                 :                :      * Allocate memory for the conn structure.  Note that we also expect this
                               2253                 :                :      * to initialize conn->errorMessage to empty.  All subsequent steps during
                               2254                 :                :      * connection initialization will only append to that buffer.
                               2255                 :                :      */
  681 alvherre@alvh.no-ip.     2256                 :              0 :     conn = pqMakeEmptyPGconn();
10327 bruce@momjian.us         2257         [ #  # ]:              0 :     if (conn == NULL)
 8014 neilc@samurai.com        2258                 :              0 :         return NULL;
                               2259                 :                : 
                               2260                 :                :     /*
                               2261                 :                :      * If the dbName parameter contains what looks like a connection string,
                               2262                 :                :      * parse it into conn struct using connectOptions1.
                               2263                 :                :      */
 3911 rhaas@postgresql.org     2264   [ #  #  #  # ]:              0 :     if (dbName && recognized_connection_string(dbName))
                               2265                 :                :     {
 6606 bruce@momjian.us         2266         [ #  # ]:              0 :         if (!connectOptions1(conn, dbName))
                               2267                 :              0 :             return conn;
                               2268                 :                :     }
                               2269                 :                :     else
                               2270                 :                :     {
                               2271                 :                :         /*
                               2272                 :                :          * Old-style path: first, parse an empty conninfo string in order to
                               2273                 :                :          * set up the same defaults that PQconnectdb() would use.
                               2274                 :                :          */
                               2275         [ #  # ]:              0 :         if (!connectOptions1(conn, ""))
                               2276                 :              0 :             return conn;
                               2277                 :                : 
                               2278                 :                :         /* Insert dbName parameter value into struct */
                               2279   [ #  #  #  # ]:              0 :         if (dbName && dbName[0] != '\0')
                               2280                 :                :         {
 1279 peter@eisentraut.org     2281                 :              0 :             free(conn->dbName);
 6606 bruce@momjian.us         2282                 :              0 :             conn->dbName = strdup(dbName);
 4039 heikki.linnakangas@i     2283         [ #  # ]:              0 :             if (!conn->dbName)
                               2284                 :              0 :                 goto oom_error;
                               2285                 :                :         }
                               2286                 :                :     }
                               2287                 :                : 
                               2288                 :                :     /*
                               2289                 :                :      * Insert remaining parameters into struct, overriding defaults (as well
                               2290                 :                :      * as any conflicting data from dbName taken as a conninfo).
                               2291                 :                :      */
 8268 tgl@sss.pgh.pa.us        2292   [ #  #  #  # ]:              0 :     if (pghost && pghost[0] != '\0')
                               2293                 :                :     {
 1279 peter@eisentraut.org     2294                 :              0 :         free(conn->pghost);
 8268 tgl@sss.pgh.pa.us        2295                 :              0 :         conn->pghost = strdup(pghost);
 4039 heikki.linnakangas@i     2296         [ #  # ]:              0 :         if (!conn->pghost)
                               2297                 :              0 :             goto oom_error;
                               2298                 :                :     }
                               2299                 :                : 
 8268 tgl@sss.pgh.pa.us        2300   [ #  #  #  # ]:              0 :     if (pgport && pgport[0] != '\0')
                               2301                 :                :     {
 1279 peter@eisentraut.org     2302                 :              0 :         free(conn->pgport);
 8268 tgl@sss.pgh.pa.us        2303                 :              0 :         conn->pgport = strdup(pgport);
 4039 heikki.linnakangas@i     2304         [ #  # ]:              0 :         if (!conn->pgport)
                               2305                 :              0 :             goto oom_error;
                               2306                 :                :     }
                               2307                 :                : 
 8268 tgl@sss.pgh.pa.us        2308   [ #  #  #  # ]:              0 :     if (pgoptions && pgoptions[0] != '\0')
                               2309                 :                :     {
 1279 peter@eisentraut.org     2310                 :              0 :         free(conn->pgoptions);
10086 bruce@momjian.us         2311                 :              0 :         conn->pgoptions = strdup(pgoptions);
 4039 heikki.linnakangas@i     2312         [ #  # ]:              0 :         if (!conn->pgoptions)
                               2313                 :              0 :             goto oom_error;
                               2314                 :                :     }
                               2315                 :                : 
 8268 tgl@sss.pgh.pa.us        2316   [ #  #  #  # ]:              0 :     if (login && login[0] != '\0')
                               2317                 :                :     {
 1279 peter@eisentraut.org     2318                 :              0 :         free(conn->pguser);
 8268 tgl@sss.pgh.pa.us        2319                 :              0 :         conn->pguser = strdup(login);
 4039 heikki.linnakangas@i     2320         [ #  # ]:              0 :         if (!conn->pguser)
                               2321                 :              0 :             goto oom_error;
                               2322                 :                :     }
                               2323                 :                : 
 8268 tgl@sss.pgh.pa.us        2324   [ #  #  #  # ]:              0 :     if (pwd && pwd[0] != '\0')
                               2325                 :                :     {
 1279 peter@eisentraut.org     2326                 :              0 :         free(conn->pgpass);
 8524 bruce@momjian.us         2327                 :              0 :         conn->pgpass = strdup(pwd);
 4039 heikki.linnakangas@i     2328         [ #  # ]:              0 :         if (!conn->pgpass)
                               2329                 :              0 :             goto oom_error;
                               2330                 :                :     }
                               2331                 :                : 
                               2332                 :                :     /*
                               2333                 :                :      * Compute derived options
                               2334                 :                :      */
  681 alvherre@alvh.no-ip.     2335         [ #  # ]:              0 :     if (!pqConnectOptions2(conn))
 8268 tgl@sss.pgh.pa.us        2336                 :              0 :         return conn;
                               2337                 :                : 
                               2338                 :                :     /*
                               2339                 :                :      * Connect to the database
                               2340                 :                :      */
  681 alvherre@alvh.no-ip.     2341         [ #  # ]:              0 :     if (pqConnectDBStart(conn))
                               2342                 :              0 :         (void) pqConnectDBComplete(conn);
                               2343                 :                : 
10327 bruce@momjian.us         2344                 :              0 :     return conn;
                               2345                 :                : 
 4039 heikki.linnakangas@i     2346                 :              0 : oom_error:
                               2347                 :              0 :     conn->status = CONNECTION_BAD;
 1127 peter@eisentraut.org     2348                 :              0 :     libpq_append_conn_error(conn, "out of memory");
 4039 heikki.linnakangas@i     2349                 :              0 :     return conn;
                               2350                 :                : }
                               2351                 :                : 
                               2352                 :                : 
                               2353                 :                : /* ----------
                               2354                 :                :  * connectNoDelay -
                               2355                 :                :  * Sets the TCP_NODELAY socket option.
                               2356                 :                :  * Returns 1 if successful, 0 if not.
                               2357                 :                :  * ----------
                               2358                 :                :  */
                               2359                 :                : static int
 9513 bruce@momjian.us         2360                 :CBC         345 : connectNoDelay(PGconn *conn)
                               2361                 :                : {
                               2362                 :                : #ifdef  TCP_NODELAY
                               2363                 :            345 :     int         on = 1;
                               2364                 :                : 
 9340 tgl@sss.pgh.pa.us        2365         [ -  + ]:            345 :     if (setsockopt(conn->sock, IPPROTO_TCP, TCP_NODELAY,
                               2366                 :                :                    (char *) &on,
                               2367                 :                :                    sizeof(on)) < 0)
                               2368                 :                :     {
                               2369                 :                :         char        sebuf[PG_STRERROR_R_BUFLEN];
                               2370                 :                : 
 1127 peter@eisentraut.org     2371                 :UBC           0 :         libpq_append_conn_error(conn, "could not set socket to TCP no delay mode: %s",
 1013 michael@paquier.xyz      2372                 :              0 :                                 SOCK_STRERROR(SOCK_ERRNO, sebuf, sizeof(sebuf)));
 9513 bruce@momjian.us         2373                 :              0 :         return 0;
                               2374                 :                :     }
                               2375                 :                : #endif
                               2376                 :                : 
 9513 bruce@momjian.us         2377                 :CBC         345 :     return 1;
                               2378                 :                : }
                               2379                 :                : 
                               2380                 :                : /* ----------
                               2381                 :                :  * Write currently connected IP address into host_addr (of len host_addr_len).
                               2382                 :                :  * If unable to, set it to the empty string.
                               2383                 :                :  * ----------
                               2384                 :                :  */
                               2385                 :                : static void
 2584 alvherre@alvh.no-ip.     2386                 :          13552 : getHostaddr(PGconn *conn, char *host_addr, int host_addr_len)
                               2387                 :                : {
                               2388                 :          13552 :     struct sockaddr_storage *addr = &conn->raddr.addr;
                               2389                 :                : 
 2377                          2390         [ +  + ]:          13552 :     if (addr->ss_family == AF_INET)
                               2391                 :                :     {
 2312 tgl@sss.pgh.pa.us        2392         [ -  + ]:            345 :         if (pg_inet_net_ntop(AF_INET,
                               2393                 :            345 :                              &((struct sockaddr_in *) addr)->sin_addr.s_addr,
                               2394                 :                :                              32,
                               2395                 :                :                              host_addr, host_addr_len) == NULL)
 2584 alvherre@alvh.no-ip.     2396                 :UBC           0 :             host_addr[0] = '\0';
                               2397                 :                :     }
 2584 alvherre@alvh.no-ip.     2398         [ -  + ]:CBC       13207 :     else if (addr->ss_family == AF_INET6)
                               2399                 :                :     {
 2312 tgl@sss.pgh.pa.us        2400         [ #  # ]:UBC           0 :         if (pg_inet_net_ntop(AF_INET6,
                               2401                 :              0 :                              &((struct sockaddr_in6 *) addr)->sin6_addr.s6_addr,
                               2402                 :                :                              128,
                               2403                 :                :                              host_addr, host_addr_len) == NULL)
 2584 alvherre@alvh.no-ip.     2404                 :              0 :             host_addr[0] = '\0';
                               2405                 :                :     }
                               2406                 :                :     else
 2584 alvherre@alvh.no-ip.     2407                 :CBC       13207 :         host_addr[0] = '\0';
                               2408                 :          13552 : }
                               2409                 :                : 
                               2410                 :                : /*
                               2411                 :                :  * emitHostIdentityInfo -
                               2412                 :                :  * Speculatively append "connection to server so-and-so failed: " to
                               2413                 :                :  * conn->errorMessage once we've identified the current connection target
                               2414                 :                :  * address.  This ensures that any subsequent error message will be properly
                               2415                 :                :  * attributed to the server we couldn't connect to.  conn->raddr must be
                               2416                 :                :  * valid, and the result of getHostaddr() must be supplied.
                               2417                 :                :  */
                               2418                 :                : static void
 1790 tgl@sss.pgh.pa.us        2419                 :          13552 : emitHostIdentityInfo(PGconn *conn, const char *host_addr)
                               2420                 :                : {
 1400 peter@eisentraut.org     2421         [ +  + ]:          13552 :     if (conn->raddr.addr.ss_family == AF_UNIX)
                               2422                 :                :     {
                               2423                 :                :         char        service[NI_MAXHOST];
                               2424                 :                : 
 7365 tgl@sss.pgh.pa.us        2425                 :          13207 :         pg_getnameinfo_all(&conn->raddr.addr, conn->raddr.salen,
                               2426                 :                :                            NULL, 0,
                               2427                 :                :                            service, sizeof(service),
                               2428                 :                :                            NI_NUMERICSERV);
 6259 magnus@hagander.net      2429                 :          13207 :         appendPQExpBuffer(&conn->errorMessage,
 1790 tgl@sss.pgh.pa.us        2430                 :          13207 :                           libpq_gettext("connection to server on socket \"%s\" failed: "),
                               2431                 :                :                           service);
                               2432                 :                :     }
                               2433                 :                :     else
                               2434                 :                :     {
                               2435                 :                :         const char *displayed_host;
                               2436                 :                :         const char *displayed_port;
                               2437                 :                : 
                               2438                 :                :         /* To which host and port were we actually connecting? */
 3081 heikki.linnakangas@i     2439         [ +  - ]:            345 :         if (conn->connhost[conn->whichhost].type == CHT_HOST_ADDRESS)
                               2440                 :            345 :             displayed_host = conn->connhost[conn->whichhost].hostaddr;
                               2441                 :                :         else
 3081 heikki.linnakangas@i     2442                 :UBC           0 :             displayed_host = conn->connhost[conn->whichhost].host;
 3330 rhaas@postgresql.org     2443                 :CBC         345 :         displayed_port = conn->connhost[conn->whichhost].port;
                               2444   [ +  -  -  + ]:            345 :         if (displayed_port == NULL || displayed_port[0] == '\0')
 3330 rhaas@postgresql.org     2445                 :UBC           0 :             displayed_port = DEF_PGPORT_STR;
                               2446                 :                : 
                               2447                 :                :         /*
                               2448                 :                :          * If the user did not supply an IP address using 'hostaddr', and
                               2449                 :                :          * 'host' was missing or does not match our lookup, display the
                               2450                 :                :          * looked-up IP address.
                               2451                 :                :          */
 3081 heikki.linnakangas@i     2452         [ -  + ]:CBC         345 :         if (conn->connhost[conn->whichhost].type != CHT_HOST_ADDRESS &&
 1800 tgl@sss.pgh.pa.us        2453         [ #  # ]:UBC           0 :             host_addr[0] &&
 3081 heikki.linnakangas@i     2454         [ #  # ]:              0 :             strcmp(displayed_host, host_addr) != 0)
 5325 peter_e@gmx.net          2455                 :              0 :             appendPQExpBuffer(&conn->errorMessage,
 1790 tgl@sss.pgh.pa.us        2456                 :              0 :                               libpq_gettext("connection to server at \"%s\" (%s), port %s failed: "),
                               2457                 :                :                               displayed_host, host_addr,
                               2458                 :                :                               displayed_port);
                               2459                 :                :         else
 5325 peter_e@gmx.net          2460                 :CBC         345 :             appendPQExpBuffer(&conn->errorMessage,
 1790 tgl@sss.pgh.pa.us        2461                 :            345 :                               libpq_gettext("connection to server at \"%s\", port %s failed: "),
                               2462                 :                :                               displayed_host,
                               2463                 :                :                               displayed_port);
                               2464                 :                :     }
 9147                          2465                 :          13552 : }
                               2466                 :                : 
                               2467                 :                : /* ----------
                               2468                 :                :  * connectFailureMessage -
                               2469                 :                :  * create a friendly error message on connection failure,
                               2470                 :                :  * using the given errno value.  Use this for error cases that
                               2471                 :                :  * imply that there's no server there.
                               2472                 :                :  * ----------
                               2473                 :                :  */
                               2474                 :                : static void
 1800                          2475                 :            224 : connectFailureMessage(PGconn *conn, int errorno)
                               2476                 :                : {
                               2477                 :                :     char        sebuf[PG_STRERROR_R_BUFLEN];
                               2478                 :                : 
                               2479                 :            224 :     appendPQExpBuffer(&conn->errorMessage,
                               2480                 :                :                       "%s\n",
                               2481                 :                :                       SOCK_STRERROR(errorno, sebuf, sizeof(sebuf)));
                               2482                 :                : 
 1400 peter@eisentraut.org     2483         [ +  - ]:            224 :     if (conn->raddr.addr.ss_family == AF_UNIX)
 1127                          2484                 :            224 :         libpq_append_conn_error(conn, "\tIs the server running locally and accepting connections on that socket?");
                               2485                 :                :     else
 1127 peter@eisentraut.org     2486                 :UBC           0 :         libpq_append_conn_error(conn, "\tIs the server running on that host and accepting TCP/IP connections?");
 1800 tgl@sss.pgh.pa.us        2487                 :CBC         224 : }
                               2488                 :                : 
                               2489                 :                : /*
                               2490                 :                :  * Should we use keepalives?  Returns 1 if yes, 0 if no, and -1 if
                               2491                 :                :  * conn->keepalives is set to a value which is not parseable as an
                               2492                 :                :  * integer.
                               2493                 :                :  */
                               2494                 :                : static int
 5655 rhaas@postgresql.org     2495                 :            345 : useKeepalives(PGconn *conn)
                               2496                 :                : {
                               2497                 :                :     int         val;
                               2498                 :                : 
                               2499         [ +  - ]:            345 :     if (conn->keepalives == NULL)
                               2500                 :            345 :         return 1;
                               2501                 :                : 
  440 tgl@sss.pgh.pa.us        2502         [ #  # ]:UBC           0 :     if (!pqParseIntParam(conn->keepalives, &val, conn, "keepalives"))
 5655 rhaas@postgresql.org     2503                 :              0 :         return -1;
                               2504                 :                : 
                               2505                 :              0 :     return val != 0 ? 1 : 0;
                               2506                 :                : }
                               2507                 :                : 
                               2508                 :                : #ifndef WIN32
                               2509                 :                : /*
                               2510                 :                :  * Set the keepalive idle timer.
                               2511                 :                :  */
                               2512                 :                : static int
 5655 rhaas@postgresql.org     2513                 :CBC         345 : setKeepalivesIdle(PGconn *conn)
                               2514                 :                : {
                               2515                 :                :     int         idle;
                               2516                 :                : 
                               2517         [ +  - ]:            345 :     if (conn->keepalives_idle == NULL)
                               2518                 :            345 :         return 1;
                               2519                 :                : 
  687 alvherre@alvh.no-ip.     2520         [ #  # ]:UBC           0 :     if (!pqParseIntParam(conn->keepalives_idle, &idle, conn,
                               2521                 :                :                          "keepalives_idle"))
 2652 michael@paquier.xyz      2522                 :              0 :         return 0;
 5655 rhaas@postgresql.org     2523         [ #  # ]:              0 :     if (idle < 0)
                               2524                 :              0 :         idle = 0;
                               2525                 :                : 
                               2526                 :                : #ifdef PG_TCP_KEEPALIVE_IDLE
 3093 tgl@sss.pgh.pa.us        2527         [ #  # ]:              0 :     if (setsockopt(conn->sock, IPPROTO_TCP, PG_TCP_KEEPALIVE_IDLE,
                               2528                 :                :                    (char *) &idle, sizeof(idle)) < 0)
                               2529                 :                :     {
                               2530                 :                :         char        sebuf[PG_STRERROR_R_BUFLEN];
                               2531                 :                : 
 1127 peter@eisentraut.org     2532                 :              0 :         libpq_append_conn_error(conn, "%s(%s) failed: %s",
                               2533                 :                :                                 "setsockopt",
                               2534                 :                :                                 PG_TCP_KEEPALIVE_IDLE_STR,
 1013 michael@paquier.xyz      2535                 :              0 :                                 SOCK_STRERROR(SOCK_ERRNO, sebuf, sizeof(sebuf)));
 5642 rhaas@postgresql.org     2536                 :              0 :         return 0;
                               2537                 :                :     }
                               2538                 :                : #endif
                               2539                 :                : 
 5655                          2540                 :              0 :     return 1;
                               2541                 :                : }
                               2542                 :                : 
                               2543                 :                : /*
                               2544                 :                :  * Set the keepalive interval.
                               2545                 :                :  */
                               2546                 :                : static int
 5655 rhaas@postgresql.org     2547                 :CBC         345 : setKeepalivesInterval(PGconn *conn)
                               2548                 :                : {
                               2549                 :                :     int         interval;
                               2550                 :                : 
                               2551         [ +  - ]:            345 :     if (conn->keepalives_interval == NULL)
                               2552                 :            345 :         return 1;
                               2553                 :                : 
  687 alvherre@alvh.no-ip.     2554         [ #  # ]:UBC           0 :     if (!pqParseIntParam(conn->keepalives_interval, &interval, conn,
                               2555                 :                :                          "keepalives_interval"))
 2652 michael@paquier.xyz      2556                 :              0 :         return 0;
 5655 rhaas@postgresql.org     2557         [ #  # ]:              0 :     if (interval < 0)
                               2558                 :              0 :         interval = 0;
                               2559                 :                : 
                               2560                 :                : #ifdef TCP_KEEPINTVL
                               2561         [ #  # ]:              0 :     if (setsockopt(conn->sock, IPPROTO_TCP, TCP_KEEPINTVL,
                               2562                 :                :                    (char *) &interval, sizeof(interval)) < 0)
                               2563                 :                :     {
                               2564                 :                :         char        sebuf[PG_STRERROR_R_BUFLEN];
                               2565                 :                : 
 1127 peter@eisentraut.org     2566                 :              0 :         libpq_append_conn_error(conn, "%s(%s) failed: %s",
                               2567                 :                :                                 "setsockopt",
                               2568                 :                :                                 "TCP_KEEPINTVL",
 1013 michael@paquier.xyz      2569                 :              0 :                                 SOCK_STRERROR(SOCK_ERRNO, sebuf, sizeof(sebuf)));
 5655 rhaas@postgresql.org     2570                 :              0 :         return 0;
                               2571                 :                :     }
                               2572                 :                : #endif
                               2573                 :                : 
                               2574                 :              0 :     return 1;
                               2575                 :                : }
                               2576                 :                : 
                               2577                 :                : /*
                               2578                 :                :  * Set the count of lost keepalive packets that will trigger a connection
                               2579                 :                :  * break.
                               2580                 :                :  */
                               2581                 :                : static int
 5655 rhaas@postgresql.org     2582                 :CBC         345 : setKeepalivesCount(PGconn *conn)
                               2583                 :                : {
                               2584                 :                :     int         count;
                               2585                 :                : 
                               2586         [ +  - ]:            345 :     if (conn->keepalives_count == NULL)
                               2587                 :            345 :         return 1;
                               2588                 :                : 
  687 alvherre@alvh.no-ip.     2589         [ #  # ]:UBC           0 :     if (!pqParseIntParam(conn->keepalives_count, &count, conn,
                               2590                 :                :                          "keepalives_count"))
 2652 michael@paquier.xyz      2591                 :              0 :         return 0;
 5655 rhaas@postgresql.org     2592         [ #  # ]:              0 :     if (count < 0)
                               2593                 :              0 :         count = 0;
                               2594                 :                : 
                               2595                 :                : #ifdef TCP_KEEPCNT
                               2596         [ #  # ]:              0 :     if (setsockopt(conn->sock, IPPROTO_TCP, TCP_KEEPCNT,
                               2597                 :                :                    (char *) &count, sizeof(count)) < 0)
                               2598                 :                :     {
                               2599                 :                :         char        sebuf[PG_STRERROR_R_BUFLEN];
                               2600                 :                : 
 1127 peter@eisentraut.org     2601                 :              0 :         libpq_append_conn_error(conn, "%s(%s) failed: %s",
                               2602                 :                :                                 "setsockopt",
                               2603                 :                :                                 "TCP_KEEPCNT",
 1013 michael@paquier.xyz      2604                 :              0 :                                 SOCK_STRERROR(SOCK_ERRNO, sebuf, sizeof(sebuf)));
 5655 rhaas@postgresql.org     2605                 :              0 :         return 0;
                               2606                 :                :     }
                               2607                 :                : #endif
                               2608                 :                : 
                               2609                 :              0 :     return 1;
                               2610                 :                : }
                               2611                 :                : #else                           /* WIN32 */
                               2612                 :                : #ifdef SIO_KEEPALIVE_VALS
                               2613                 :                : /*
                               2614                 :                :  * Enable keepalives and set the keepalive values on Win32,
                               2615                 :                :  * where they are always set in one batch.
                               2616                 :                :  *
                               2617                 :                :  * CAUTION: This needs to be signal safe, since it's used by PQcancel.
                               2618                 :                :  */
                               2619                 :                : int
                               2620                 :                : pqSetKeepalivesWin32(pgsocket sock, int idle, int interval)
                               2621                 :                : {
                               2622                 :                :     struct tcp_keepalive ka;
                               2623                 :                :     DWORD       retsize;
                               2624                 :                : 
                               2625                 :                :     if (idle <= 0)
                               2626                 :                :         idle = 2 * 60 * 60;     /* 2 hours = default */
                               2627                 :                :     if (interval <= 0)
                               2628                 :                :         interval = 1;           /* 1 second = default */
                               2629                 :                : 
                               2630                 :                :     ka.onoff = 1;
                               2631                 :                :     ka.keepalivetime = idle * 1000;
                               2632                 :                :     ka.keepaliveinterval = interval * 1000;
                               2633                 :                : 
                               2634                 :                :     if (WSAIoctl(sock,
                               2635                 :                :                  SIO_KEEPALIVE_VALS,
                               2636                 :                :                  (LPVOID) &ka,
                               2637                 :                :                  sizeof(ka),
                               2638                 :                :                  NULL,
                               2639                 :                :                  0,
                               2640                 :                :                  &retsize,
                               2641                 :                :                  NULL,
                               2642                 :                :                  NULL)
                               2643                 :                :         != 0)
                               2644                 :                :         return 0;
                               2645                 :                :     return 1;
                               2646                 :                : }
                               2647                 :                : 
                               2648                 :                : static int
                               2649                 :                : prepKeepalivesWin32(PGconn *conn)
                               2650                 :                : {
                               2651                 :                :     int         idle = -1;
                               2652                 :                :     int         interval = -1;
                               2653                 :                : 
                               2654                 :                :     if (conn->keepalives_idle &&
                               2655                 :                :         !pqParseIntParam(conn->keepalives_idle, &idle, conn,
                               2656                 :                :                          "keepalives_idle"))
                               2657                 :                :         return 0;
                               2658                 :                :     if (conn->keepalives_interval &&
                               2659                 :                :         !pqParseIntParam(conn->keepalives_interval, &interval, conn,
                               2660                 :                :                          "keepalives_interval"))
                               2661                 :                :         return 0;
                               2662                 :                : 
                               2663                 :                :     if (!pqSetKeepalivesWin32(conn->sock, idle, interval))
                               2664                 :                :     {
                               2665                 :                :         libpq_append_conn_error(conn, "%s(%s) failed: error code %d",
                               2666                 :                :                                 "WSAIoctl", "SIO_KEEPALIVE_VALS",
                               2667                 :                :                                 WSAGetLastError());
                               2668                 :                :         return 0;
                               2669                 :                :     }
                               2670                 :                :     return 1;
                               2671                 :                : }
                               2672                 :                : #endif                          /* SIO_KEEPALIVE_VALS */
                               2673                 :                : #endif                          /* WIN32 */
                               2674                 :                : 
                               2675                 :                : /*
                               2676                 :                :  * Set the TCP user timeout.
                               2677                 :                :  */
                               2678                 :                : static int
 2446 michael@paquier.xyz      2679                 :CBC         345 : setTCPUserTimeout(PGconn *conn)
                               2680                 :                : {
                               2681                 :                :     int         timeout;
                               2682                 :                : 
                               2683         [ +  - ]:            345 :     if (conn->pgtcp_user_timeout == NULL)
                               2684                 :            345 :         return 1;
                               2685                 :                : 
  687 alvherre@alvh.no-ip.     2686         [ #  # ]:UBC           0 :     if (!pqParseIntParam(conn->pgtcp_user_timeout, &timeout, conn,
                               2687                 :                :                          "tcp_user_timeout"))
 2446 michael@paquier.xyz      2688                 :              0 :         return 0;
                               2689                 :                : 
                               2690         [ #  # ]:              0 :     if (timeout < 0)
                               2691                 :              0 :         timeout = 0;
                               2692                 :                : 
                               2693                 :                : #ifdef TCP_USER_TIMEOUT
                               2694         [ #  # ]:              0 :     if (setsockopt(conn->sock, IPPROTO_TCP, TCP_USER_TIMEOUT,
                               2695                 :                :                    (char *) &timeout, sizeof(timeout)) < 0)
                               2696                 :                :     {
                               2697                 :                :         char        sebuf[256];
                               2698                 :                : 
 1127 peter@eisentraut.org     2699                 :              0 :         libpq_append_conn_error(conn, "%s(%s) failed: %s",
                               2700                 :                :                                 "setsockopt",
                               2701                 :                :                                 "TCP_USER_TIMEOUT",
 1013 michael@paquier.xyz      2702                 :              0 :                                 SOCK_STRERROR(SOCK_ERRNO, sebuf, sizeof(sebuf)));
 2446                          2703                 :              0 :         return 0;
                               2704                 :                :     }
                               2705                 :                : #endif
                               2706                 :                : 
                               2707                 :              0 :     return 1;
                               2708                 :                : }
                               2709                 :                : 
                               2710                 :                : /* ----------
                               2711                 :                :  * pqConnectDBStart -
                               2712                 :                :  *      Begin the process of making a connection to the backend.
                               2713                 :                :  *
                               2714                 :                :  * Returns 1 if successful, 0 if not.
                               2715                 :                :  * ----------
                               2716                 :                :  */
                               2717                 :                : int
  681 alvherre@alvh.no-ip.     2718                 :CBC       13476 : pqConnectDBStart(PGconn *conn)
                               2719                 :                : {
 9513 bruce@momjian.us         2720         [ -  + ]:          13476 :     if (!conn)
 9513 bruce@momjian.us         2721                 :UBC           0 :         return 0;
                               2722                 :                : 
 7246 tgl@sss.pgh.pa.us        2723         [ -  + ]:CBC       13476 :     if (!conn->options_valid)
 7246 tgl@sss.pgh.pa.us        2724                 :UBC           0 :         goto connect_errReturn;
                               2725                 :                : 
                               2726                 :                :     /*
                               2727                 :                :      * Check for bad linking to backend-internal versions of src/common
                               2728                 :                :      * functions (see comments in link-canary.c for the reason we need this).
                               2729                 :                :      * Nobody but developers should see this message, so we don't bother
                               2730                 :                :      * translating it.
                               2731                 :                :      */
 2655 tgl@sss.pgh.pa.us        2732         [ -  + ]:CBC       13476 :     if (!pg_link_canary_is_frontend())
                               2733                 :                :     {
 1800 tgl@sss.pgh.pa.us        2734                 :UBC           0 :         appendPQExpBufferStr(&conn->errorMessage,
                               2735                 :                :                              "libpq is incorrectly linked to backend functions\n");
 2655                          2736                 :              0 :         goto connect_errReturn;
                               2737                 :                :     }
                               2738                 :                : 
                               2739                 :                :     /* Ensure our buffers are empty */
 9513 bruce@momjian.us         2740                 :CBC       13476 :     conn->inStart = conn->inCursor = conn->inEnd = 0;
                               2741                 :          13476 :     conn->outCount = 0;
                               2742                 :                : 
                               2743                 :                :     /*
                               2744                 :                :      * Set up to try to connect to the first host.  (Setting whichhost = -1 is
                               2745                 :                :      * a bit of a cheat, but PQconnectPoll will advance it to 0 before
                               2746                 :                :      * anything else looks at it.)
                               2747                 :                :      *
                               2748                 :                :      * Cancel requests are special though, they should only try one host and
                               2749                 :                :      * address, and these fields have already been set up in PQcancelCreate,
                               2750                 :                :      * so leave these fields alone for cancel requests.
                               2751                 :                :      */
  644 alvherre@alvh.no-ip.     2752         [ +  + ]:          13476 :     if (!conn->cancelRequest)
                               2753                 :                :     {
                               2754                 :          13467 :         conn->whichhost = -1;
                               2755                 :          13467 :         conn->try_next_host = true;
                               2756                 :          13467 :         conn->try_next_addr = false;
                               2757                 :                :     }
                               2758                 :                : 
 8227 tgl@sss.pgh.pa.us        2759                 :          13476 :     conn->status = CONNECTION_NEEDED;
                               2760                 :                : 
                               2761                 :                :     /* Also reset the target_server_type state if needed */
 1750                          2762         [ -  + ]:          13476 :     if (conn->target_server_type == SERVER_TYPE_PREFER_STANDBY_PASS2)
 1750 tgl@sss.pgh.pa.us        2763                 :UBC           0 :         conn->target_server_type = SERVER_TYPE_PREFER_STANDBY;
                               2764                 :                : 
                               2765                 :                :     /*
                               2766                 :                :      * The code for processing CONNECTION_NEEDED state is in PQconnectPoll(),
                               2767                 :                :      * so that it can easily be re-executed if needed again during the
                               2768                 :                :      * asynchronous startup process.  However, we must run it once here,
                               2769                 :                :      * because callers expect a success return from this routine to mean that
                               2770                 :                :      * we are in PGRES_POLLING_WRITING connection state.
                               2771                 :                :      */
 8227 tgl@sss.pgh.pa.us        2772         [ +  + ]:CBC       13476 :     if (PQconnectPoll(conn) == PGRES_POLLING_WRITING)
                               2773                 :          13250 :         return 1;
                               2774                 :                : 
 9513 bruce@momjian.us         2775                 :            226 : connect_errReturn:
                               2776                 :                : 
                               2777                 :                :     /*
                               2778                 :                :      * If we managed to open a socket, close it immediately rather than
                               2779                 :                :      * waiting till PQfinish.  (The application cannot have gotten the socket
                               2780                 :                :      * from PQsocket yet, so this doesn't risk breaking anything.)
                               2781                 :                :      */
 3687 tgl@sss.pgh.pa.us        2782                 :            226 :     pqDropConnection(conn, true);
 9513 bruce@momjian.us         2783                 :            226 :     conn->status = CONNECTION_BAD;
                               2784                 :            226 :     return 0;
                               2785                 :                : }
                               2786                 :                : 
                               2787                 :                : 
                               2788                 :                : /*
                               2789                 :                :  *      pqConnectDBComplete
                               2790                 :                :  *
                               2791                 :                :  * Block and complete a connection.
                               2792                 :                :  *
                               2793                 :                :  * Returns 1 on success, 0 on failure.
                               2794                 :                :  */
                               2795                 :                : int
  681 alvherre@alvh.no-ip.     2796                 :          11830 : pqConnectDBComplete(PGconn *conn)
                               2797                 :                : {
 9466 tgl@sss.pgh.pa.us        2798                 :          11830 :     PostgresPollingStatusType flag = PGRES_POLLING_WRITING;
  551                          2799                 :          11830 :     pg_usec_time_t end_time = -1;
 3133 rhaas@postgresql.org     2800                 :          11830 :     int         timeout = 0;
 2682 tgl@sss.pgh.pa.us        2801                 :          11830 :     int         last_whichhost = -2;    /* certainly different from whichhost */
  993 dgustafsson@postgres     2802                 :          11830 :     int         last_whichaddr = -2;    /* certainly different from whichaddr */
                               2803                 :                : 
 9466 tgl@sss.pgh.pa.us        2804   [ +  -  -  + ]:          11830 :     if (conn == NULL || conn->status == CONNECTION_BAD)
 9466 tgl@sss.pgh.pa.us        2805                 :UBC           0 :         return 0;
                               2806                 :                : 
                               2807                 :                :     /*
                               2808                 :                :      * Set up a time limit, if connect_timeout is greater than zero.
                               2809                 :                :      */
 8504 bruce@momjian.us         2810         [ +  + ]:CBC       11830 :     if (conn->connect_timeout != NULL)
                               2811                 :                :     {
  687 alvherre@alvh.no-ip.     2812         [ +  - ]:              5 :         if (!pqParseIntParam(conn->connect_timeout, &timeout, conn,
                               2813                 :                :                              "connect_timeout"))
                               2814                 :                :         {
                               2815                 :                :             /* mark the connection as bad to report the parsing failure */
 2248 michael@paquier.xyz      2816                 :UBC           0 :             conn->status = CONNECTION_BAD;
 2652                          2817                 :              0 :             return 0;
                               2818                 :                :         }
                               2819                 :                :     }
                               2820                 :                : 
                               2821                 :                :     for (;;)
 8512 bruce@momjian.us         2822                 :CBC       25561 :     {
 3133 rhaas@postgresql.org     2823                 :          37391 :         int         ret = 0;
                               2824                 :                : 
                               2825                 :                :         /*
                               2826                 :                :          * (Re)start the connect_timeout timer if it's active and we are
                               2827                 :                :          * considering a different host than we were last time through.  If
                               2828                 :                :          * we've already succeeded, though, needn't recalculate.
                               2829                 :                :          */
 2682 tgl@sss.pgh.pa.us        2830         [ +  + ]:          37391 :         if (flag != PGRES_POLLING_OK &&
                               2831         [ +  + ]:          25973 :             timeout > 0 &&
                               2832         [ +  + ]:             13 :             (conn->whichhost != last_whichhost ||
  993 dgustafsson@postgres     2833         [ +  + ]:              8 :              conn->whichaddr != last_whichaddr))
                               2834                 :                :         {
  551 tgl@sss.pgh.pa.us        2835                 :              6 :             end_time = PQgetCurrentTimeUSec() + (pg_usec_time_t) timeout * 1000000;
 2682                          2836                 :              6 :             last_whichhost = conn->whichhost;
  993 dgustafsson@postgres     2837                 :              6 :             last_whichaddr = conn->whichaddr;
                               2838                 :                :         }
                               2839                 :                : 
                               2840                 :                :         /*
                               2841                 :                :          * Wait, if necessary.  Note that the initial state (just after
                               2842                 :                :          * PQconnectStart) is to wait for the socket to select for writing.
                               2843                 :                :          */
 9468 tgl@sss.pgh.pa.us        2844   [ +  +  +  + ]:          37391 :         switch (flag)
                               2845                 :                :         {
 9513 bruce@momjian.us         2846                 :          11418 :             case PGRES_POLLING_OK:
 9468 tgl@sss.pgh.pa.us        2847                 :          11418 :                 return 1;       /* success! */
                               2848                 :                : 
 9513 bruce@momjian.us         2849                 :          12920 :             case PGRES_POLLING_READING:
  551 tgl@sss.pgh.pa.us        2850                 :          12920 :                 ret = pqWaitTimed(1, 0, conn, end_time);
 3133 rhaas@postgresql.org     2851         [ -  + ]:          12920 :                 if (ret == -1)
                               2852                 :                :                 {
                               2853                 :                :                     /* hard failure, eg select() problem, aborts everything */
 9468 tgl@sss.pgh.pa.us        2854                 :UBC           0 :                     conn->status = CONNECTION_BAD;
                               2855                 :              0 :                     return 0;
                               2856                 :                :                 }
 9513 bruce@momjian.us         2857                 :CBC       12920 :                 break;
                               2858                 :                : 
                               2859                 :          12641 :             case PGRES_POLLING_WRITING:
  551 tgl@sss.pgh.pa.us        2860                 :          12641 :                 ret = pqWaitTimed(0, 1, conn, end_time);
 3133 rhaas@postgresql.org     2861         [ -  + ]:          12641 :                 if (ret == -1)
                               2862                 :                :                 {
                               2863                 :                :                     /* hard failure, eg select() problem, aborts everything */
 9468 tgl@sss.pgh.pa.us        2864                 :UBC           0 :                     conn->status = CONNECTION_BAD;
                               2865                 :              0 :                     return 0;
                               2866                 :                :                 }
 9513 bruce@momjian.us         2867                 :CBC       12641 :                 break;
                               2868                 :                : 
                               2869                 :            412 :             default:
                               2870                 :                :                 /* Just in case we failed to set it in PQconnectPoll */
                               2871                 :            412 :                 conn->status = CONNECTION_BAD;
                               2872                 :            412 :                 return 0;
                               2873                 :                :         }
                               2874                 :                : 
 3108 tgl@sss.pgh.pa.us        2875         [ +  + ]:          25561 :         if (ret == 1)           /* connect_timeout elapsed */
                               2876                 :                :         {
                               2877                 :                :             /*
                               2878                 :                :              * Give up on current server/address, try the next one.
                               2879                 :                :              */
 2682                          2880                 :              1 :             conn->try_next_addr = true;
 2689                          2881                 :              1 :             conn->status = CONNECTION_NEEDED;
                               2882                 :                :         }
                               2883                 :                : 
                               2884                 :                :         /*
                               2885                 :                :          * Now try to advance the state machine.
                               2886                 :                :          */
  644 alvherre@alvh.no-ip.     2887         [ +  + ]:          25561 :         if (conn->cancelRequest)
                               2888                 :              4 :             flag = PQcancelPoll((PGcancelConn *) conn);
                               2889                 :                :         else
                               2890                 :          25557 :             flag = PQconnectPoll(conn);
                               2891                 :                :     }
                               2892                 :                : }
                               2893                 :                : 
                               2894                 :                : /* ----------------
                               2895                 :                :  *      PQconnectPoll
                               2896                 :                :  *
                               2897                 :                :  * Poll an asynchronous connection.
                               2898                 :                :  *
                               2899                 :                :  * Returns a PostgresPollingStatusType.
                               2900                 :                :  * Before calling this function, use select(2) to determine when data
                               2901                 :                :  * has arrived..
                               2902                 :                :  *
                               2903                 :                :  * You must call PQfinish whether or not this fails.
                               2904                 :                :  *
                               2905                 :                :  * This function and PQconnectStart are intended to allow connections to be
                               2906                 :                :  * made without blocking the execution of your program on remote I/O. However,
                               2907                 :                :  * there are a number of caveats:
                               2908                 :                :  *
                               2909                 :                :  *   o  If you call PQtrace, ensure that the stream object into which you trace
                               2910                 :                :  *      will not block.
                               2911                 :                :  *   o  If you do not supply an IP address for the remote host (i.e. you
                               2912                 :                :  *      supply a host name instead) then PQconnectStart will block on
                               2913                 :                :  *      getaddrinfo.  You will be fine if using Unix sockets (i.e. by
                               2914                 :                :  *      supplying neither a host name nor a host address).
                               2915                 :                :  *   o  If your backend wants to use Kerberos authentication then you must
                               2916                 :                :  *      supply both a host name and a host address, otherwise this function
                               2917                 :                :  *      may block on gethostname.
                               2918                 :                :  *
                               2919                 :                :  * ----------------
                               2920                 :                :  */
                               2921                 :                : PostgresPollingStatusType
 9513 bruce@momjian.us         2922                 :         476509 : PQconnectPoll(PGconn *conn)
                               2923                 :                : {
 2689 tgl@sss.pgh.pa.us        2924                 :         476509 :     bool        reset_connection_state_machine = false;
                               2925                 :         476509 :     bool        need_new_connection = false;
                               2926                 :                :     PGresult   *res;
                               2927                 :                :     char        sebuf[PG_STRERROR_R_BUFLEN];
                               2928                 :                :     int         optval;
                               2929                 :                : 
 9513 bruce@momjian.us         2930         [ -  + ]:         476509 :     if (conn == NULL)
 9513 bruce@momjian.us         2931                 :UBC           0 :         return PGRES_POLLING_FAILED;
                               2932                 :                : 
                               2933                 :                :     /* Get the new data */
 9513 bruce@momjian.us         2934   [ -  -  +  +  :CBC      476509 :     switch (conn->status)
                                              +  - ]
                               2935                 :                :     {
                               2936                 :                :             /*
                               2937                 :                :              * We really shouldn't have been polled in these two cases, but we
                               2938                 :                :              * can handle it.
                               2939                 :                :              */
 9513 bruce@momjian.us         2940                 :UBC           0 :         case CONNECTION_BAD:
                               2941                 :              0 :             return PGRES_POLLING_FAILED;
                               2942                 :              0 :         case CONNECTION_OK:
                               2943                 :              0 :             return PGRES_POLLING_OK;
                               2944                 :                : 
                               2945                 :                :             /* These are reading states */
 9513 bruce@momjian.us         2946                 :CBC       55844 :         case CONNECTION_AWAITING_RESPONSE:
                               2947                 :                :         case CONNECTION_AUTH_OK:
                               2948                 :                :         case CONNECTION_CHECK_WRITABLE:
                               2949                 :                :         case CONNECTION_CONSUME:
                               2950                 :                :         case CONNECTION_CHECK_STANDBY:
                               2951                 :                :             {
                               2952                 :                :                 /* Load waiting data */
 9379                          2953                 :          55844 :                 int         n = pqReadData(conn);
                               2954                 :                : 
                               2955         [ +  + ]:          55844 :                 if (n < 0)
                               2956                 :              6 :                     goto error_return;
                               2957         [ +  + ]:          55838 :                 if (n == 0)
                               2958                 :          42303 :                     return PGRES_POLLING_READING;
                               2959                 :                : 
                               2960                 :          13535 :                 break;
                               2961                 :                :             }
                               2962                 :                : 
                               2963                 :                :             /* These are writing states, so we just proceed. */
 9513                          2964                 :          13943 :         case CONNECTION_STARTED:
                               2965                 :                :         case CONNECTION_MADE:
 9379                          2966                 :          13943 :             break;
                               2967                 :                : 
                               2968                 :                :             /* Special cases: proceed without waiting. */
 8227 tgl@sss.pgh.pa.us        2969                 :         406722 :         case CONNECTION_SSL_STARTUP:
                               2970                 :                :         case CONNECTION_NEEDED:
                               2971                 :                :         case CONNECTION_GSS_STARTUP:
                               2972                 :                :         case CONNECTION_CHECK_TARGET:
                               2973                 :                :         case CONNECTION_AUTHENTICATING:
                               2974                 :         406722 :             break;
                               2975                 :                : 
 9513 bruce@momjian.us         2976                 :UBC           0 :         default:
 1127 peter@eisentraut.org     2977                 :              0 :             libpq_append_conn_error(conn, "invalid connection state, probably indicative of memory corruption");
 9513 bruce@momjian.us         2978                 :              0 :             goto error_return;
                               2979                 :                :     }
                               2980                 :                : 
                               2981                 :                : 
 7367 bruce@momjian.us         2982                 :CBC       39796 : keep_going:                     /* We will come back to here until there is
                               2983                 :                :                                  * nothing left to do. */
                               2984                 :                : 
                               2985                 :                :     /* Time to advance to next address, or next host if no more addresses? */
 2689 tgl@sss.pgh.pa.us        2986         [ +  + ]:         473996 :     if (conn->try_next_addr)
                               2987                 :                :     {
  993 dgustafsson@postgres     2988         [ +  - ]:            225 :         if (conn->whichaddr < conn->naddr)
                               2989                 :                :         {
                               2990                 :            225 :             conn->whichaddr++;
 2689 tgl@sss.pgh.pa.us        2991                 :            225 :             reset_connection_state_machine = true;
                               2992                 :                :         }
                               2993                 :                :         else
 2689 tgl@sss.pgh.pa.us        2994                 :UBC           0 :             conn->try_next_host = true;
 2689 tgl@sss.pgh.pa.us        2995                 :CBC         225 :         conn->try_next_addr = false;
                               2996                 :                :     }
                               2997                 :                : 
                               2998                 :                :     /* Time to advance to next connhost[] entry? */
                               2999         [ +  + ]:         473996 :     if (conn->try_next_host)
                               3000                 :                :     {
                               3001                 :                :         pg_conn_host *ch;
                               3002                 :                :         struct addrinfo hint;
                               3003                 :                :         struct addrinfo *addrlist;
                               3004                 :                :         int         thisport;
                               3005                 :                :         int         ret;
                               3006                 :                :         char        portstr[MAXPGPATH];
                               3007                 :                : 
 1750                          3008         [ +  + ]:          13974 :         if (conn->whichhost + 1 < conn->nconnhost)
                               3009                 :          13484 :             conn->whichhost++;
                               3010                 :                :         else
                               3011                 :                :         {
                               3012                 :                :             /*
                               3013                 :                :              * Oops, no more hosts.
                               3014                 :                :              *
                               3015                 :                :              * If we are trying to connect in "prefer-standby" mode, then drop
                               3016                 :                :              * the standby requirement and start over. Don't do this for
                               3017                 :                :              * cancel requests though, since we are certain the list of
                               3018                 :                :              * servers won't change as the target_server_type option is not
                               3019                 :                :              * applicable to those connections.
                               3020                 :                :              *
                               3021                 :                :              * Otherwise, an appropriate error message is already set up, so
                               3022                 :                :              * we just need to set the right status.
                               3023                 :                :              */
                               3024         [ +  + ]:            490 :             if (conn->target_server_type == SERVER_TYPE_PREFER_STANDBY &&
  644 alvherre@alvh.no-ip.     3025         [ +  - ]:              1 :                 conn->nconnhost > 0 &&
                               3026         [ +  - ]:              1 :                 !conn->cancelRequest)
                               3027                 :                :             {
 1750 tgl@sss.pgh.pa.us        3028                 :              1 :                 conn->target_server_type = SERVER_TYPE_PREFER_STANDBY_PASS2;
                               3029                 :              1 :                 conn->whichhost = 0;
                               3030                 :                :             }
                               3031                 :                :             else
                               3032                 :            489 :                 goto error_return;
                               3033                 :                :         }
                               3034                 :                : 
                               3035                 :                :         /* Drop any address info for previous host */
 2672                          3036                 :          13485 :         release_conn_addrinfo(conn);
                               3037                 :                : 
                               3038                 :                :         /*
                               3039                 :                :          * Look up info for the new host.  On failure, log the problem in
                               3040                 :                :          * conn->errorMessage, then loop around to try the next host.  (Note
                               3041                 :                :          * we don't clear try_next_host until we've succeeded.)
                               3042                 :                :          */
                               3043                 :          13485 :         ch = &conn->connhost[conn->whichhost];
                               3044                 :                : 
                               3045                 :                :         /* Initialize hint structure */
                               3046   [ +  -  +  -  :          94395 :         MemSet(&hint, 0, sizeof(hint));
                                     +  -  +  -  +  
                                                 + ]
                               3047                 :          13485 :         hint.ai_socktype = SOCK_STREAM;
  993 dgustafsson@postgres     3048                 :          13485 :         hint.ai_family = AF_UNSPEC;
                               3049                 :                : 
                               3050                 :                :         /* Figure out the port number we're going to use. */
 2672 tgl@sss.pgh.pa.us        3051   [ +  -  -  + ]:          13485 :         if (ch->port == NULL || ch->port[0] == '\0')
 2672 tgl@sss.pgh.pa.us        3052                 :UBC           0 :             thisport = DEF_PGPORT;
                               3053                 :                :         else
                               3054                 :                :         {
  687 alvherre@alvh.no-ip.     3055         [ -  + ]:CBC       13485 :             if (!pqParseIntParam(ch->port, &thisport, conn, "port"))
 2652 michael@paquier.xyz      3056                 :UBC           0 :                 goto error_return;
                               3057                 :                : 
 2672 tgl@sss.pgh.pa.us        3058   [ +  +  -  + ]:CBC       13485 :             if (thisport < 1 || thisport > 65535)
                               3059                 :                :             {
 1127 peter@eisentraut.org     3060                 :              3 :                 libpq_append_conn_error(conn, "invalid port number: \"%s\"", ch->port);
 2672 tgl@sss.pgh.pa.us        3061                 :              3 :                 goto keep_going;
                               3062                 :                :             }
                               3063                 :                :         }
                               3064                 :          13482 :         snprintf(portstr, sizeof(portstr), "%d", thisport);
                               3065                 :                : 
                               3066                 :                :         /* Use pg_getaddrinfo_all() to resolve the address */
                               3067   [ -  +  +  - ]:          13482 :         switch (ch->type)
                               3068                 :                :         {
 2672 tgl@sss.pgh.pa.us        3069                 :UBC           0 :             case CHT_HOST_NAME:
                               3070                 :              0 :                 ret = pg_getaddrinfo_all(ch->host, portstr, &hint,
                               3071                 :                :                                          &addrlist);
  993 dgustafsson@postgres     3072   [ #  #  #  # ]:              0 :                 if (ret || !addrlist)
                               3073                 :                :                 {
 1127 peter@eisentraut.org     3074                 :              0 :                     libpq_append_conn_error(conn, "could not translate host name \"%s\" to address: %s",
                               3075                 :                :                                             ch->host, gai_strerror(ret));
 2672 tgl@sss.pgh.pa.us        3076                 :              0 :                     goto keep_going;
                               3077                 :                :                 }
                               3078                 :              0 :                 break;
                               3079                 :                : 
 2672 tgl@sss.pgh.pa.us        3080                 :CBC         318 :             case CHT_HOST_ADDRESS:
                               3081                 :            318 :                 hint.ai_flags = AI_NUMERICHOST;
                               3082                 :            318 :                 ret = pg_getaddrinfo_all(ch->hostaddr, portstr, &hint,
                               3083                 :                :                                          &addrlist);
  993 dgustafsson@postgres     3084   [ +  -  -  + ]:            318 :                 if (ret || !addrlist)
                               3085                 :                :                 {
 1127 peter@eisentraut.org     3086                 :UBC           0 :                     libpq_append_conn_error(conn, "could not parse network address \"%s\": %s",
                               3087                 :                :                                             ch->hostaddr, gai_strerror(ret));
 2672 tgl@sss.pgh.pa.us        3088                 :              0 :                     goto keep_going;
                               3089                 :                :                 }
 2672 tgl@sss.pgh.pa.us        3090                 :CBC         318 :                 break;
                               3091                 :                : 
                               3092                 :          13164 :             case CHT_UNIX_SOCKET:
  993 dgustafsson@postgres     3093                 :          13164 :                 hint.ai_family = AF_UNIX;
 2672 tgl@sss.pgh.pa.us        3094   [ -  +  -  + ]:          13164 :                 UNIXSOCK_PATH(portstr, thisport, ch->host);
                               3095         [ -  + ]:          13164 :                 if (strlen(portstr) >= UNIXSOCK_PATH_BUFLEN)
                               3096                 :                :                 {
    7 peter@eisentraut.org     3097                 :UNC           0 :                     libpq_append_conn_error(conn, "Unix-domain socket path \"%s\" is too long (maximum %zu bytes)",
                               3098                 :                :                                             portstr,
                               3099                 :                :                                             (UNIXSOCK_PATH_BUFLEN - 1));
 2672 tgl@sss.pgh.pa.us        3100                 :UBC           0 :                     goto keep_going;
                               3101                 :                :                 }
                               3102                 :                : 
                               3103                 :                :                 /*
                               3104                 :                :                  * NULL hostname tells pg_getaddrinfo_all to parse the service
                               3105                 :                :                  * name as a Unix-domain socket path.
                               3106                 :                :                  */
 2672 tgl@sss.pgh.pa.us        3107                 :CBC       13164 :                 ret = pg_getaddrinfo_all(NULL, portstr, &hint,
                               3108                 :                :                                          &addrlist);
  993 dgustafsson@postgres     3109   [ +  -  -  + ]:          13164 :                 if (ret || !addrlist)
                               3110                 :                :                 {
 1127 peter@eisentraut.org     3111                 :UBC           0 :                     libpq_append_conn_error(conn, "could not translate Unix-domain socket path \"%s\" to address: %s",
                               3112                 :                :                                             portstr, gai_strerror(ret));
 2672 tgl@sss.pgh.pa.us        3113                 :              0 :                     goto keep_going;
                               3114                 :                :                 }
 2672 tgl@sss.pgh.pa.us        3115                 :CBC       13164 :                 break;
                               3116                 :                :         }
                               3117                 :                : 
                               3118                 :                :         /*
                               3119                 :                :          * Store a copy of the addrlist in private memory so we can perform
                               3120                 :                :          * randomization for load balancing.
                               3121                 :                :          */
  993 dgustafsson@postgres     3122                 :          13482 :         ret = store_conn_addrinfo(conn, addrlist);
                               3123                 :          13482 :         pg_freeaddrinfo_all(hint.ai_family, addrlist);
                               3124         [ -  + ]:          13482 :         if (ret)
  993 dgustafsson@postgres     3125                 :UBC           0 :             goto error_return;  /* message already logged */
                               3126                 :                : 
                               3127                 :                :         /*
                               3128                 :                :          * If random load balancing is enabled we shuffle the addresses.
                               3129                 :                :          */
  993 dgustafsson@postgres     3130         [ +  + ]:CBC       13482 :         if (conn->load_balance_type == LOAD_BALANCE_RANDOM)
                               3131                 :                :         {
                               3132                 :                :             /*
                               3133                 :                :              * This is the "inside-out" variant of the Fisher-Yates shuffle
                               3134                 :                :              * algorithm. Notionally, we append each new value to the array
                               3135                 :                :              * and then swap it with a randomly-chosen array element (possibly
                               3136                 :                :              * including itself, else we fail to generate permutations with
                               3137                 :                :              * the last integer last).  The swap step can be optimized by
                               3138                 :                :              * combining it with the insertion.
                               3139                 :                :              *
                               3140                 :                :              * We don't need to initialize conn->prng_state here, because that
                               3141                 :                :              * already happened in pqConnectOptions2.
                               3142                 :                :              */
                               3143         [ -  + ]:             60 :             for (int i = 1; i < conn->naddr; i++)
                               3144                 :                :             {
  993 dgustafsson@postgres     3145                 :UBC           0 :                 int         j = pg_prng_uint64_range(&conn->prng_state, 0, i);
                               3146                 :              0 :                 AddrInfo    temp = conn->addr[j];
                               3147                 :                : 
                               3148                 :              0 :                 conn->addr[j] = conn->addr[i];
                               3149                 :              0 :                 conn->addr[i] = temp;
                               3150                 :                :             }
                               3151                 :                :         }
                               3152                 :                : 
 2689 tgl@sss.pgh.pa.us        3153                 :CBC       13482 :         reset_connection_state_machine = true;
                               3154                 :          13482 :         conn->try_next_host = false;
                               3155                 :                :     }
                               3156                 :                : 
                               3157                 :                :     /* Reset connection state machine? */
                               3158         [ +  + ]:         473504 :     if (reset_connection_state_machine)
                               3159                 :                :     {
                               3160                 :                :         /*
                               3161                 :                :          * (Re) initialize our connection control variables for a set of
                               3162                 :                :          * connection attempts to a single server address.  These variables
                               3163                 :                :          * must persist across individual connection attempts, but we must
                               3164                 :                :          * reset them when we start to consider a new server.
                               3165                 :                :          */
  258 heikki.linnakangas@i     3166                 :          13707 :         conn->pversion = conn->max_pversion;
 2689 tgl@sss.pgh.pa.us        3167                 :          13707 :         conn->send_appname = true;
  617 heikki.linnakangas@i     3168                 :          13707 :         conn->failed_enc_methods = 0;
                               3169                 :          13707 :         conn->current_enc_method = 0;
                               3170                 :          13707 :         conn->allowed_enc_methods = 0;
 2689 tgl@sss.pgh.pa.us        3171                 :          13707 :         reset_connection_state_machine = false;
                               3172                 :          13707 :         need_new_connection = true;
                               3173                 :                :     }
                               3174                 :                : 
                               3175                 :                :     /* Force a new connection (perhaps to the same server as before)? */
                               3176         [ +  + ]:         473504 :     if (need_new_connection)
                               3177                 :                :     {
                               3178                 :                :         /* Drop any existing connection */
                               3179                 :          13774 :         pqDropConnection(conn, true);
                               3180                 :                : 
                               3181                 :                :         /* Reset all state obtained from old server */
                               3182                 :          13774 :         pqDropServerData(conn);
                               3183                 :                : 
                               3184                 :                :         /* Drop any PGresult we might have, too */
                               3185                 :          13774 :         conn->asyncStatus = PGASYNC_IDLE;
                               3186                 :          13774 :         conn->xactStatus = PQTRANS_IDLE;
 1737 alvherre@alvh.no-ip.     3187                 :          13774 :         conn->pipelineStatus = PQ_PIPELINE_OFF;
 2689 tgl@sss.pgh.pa.us        3188                 :          13774 :         pqClearAsyncResult(conn);
                               3189                 :                : 
                               3190                 :                :         /* Reset conn->status to put the state machine in the right state */
                               3191                 :          13774 :         conn->status = CONNECTION_NEEDED;
                               3192                 :                : 
                               3193                 :          13774 :         need_new_connection = false;
                               3194                 :                :     }
                               3195                 :                : 
                               3196                 :                :     /*
                               3197                 :                :      * Decide what to do next, if server rejects SSL or GSS negotiation, but
                               3198                 :                :      * the connection is still valid.  If there are no options left, error out
                               3199                 :                :      * with 'msg'.
                               3200                 :                :      */
                               3201                 :                : #define ENCRYPTION_NEGOTIATION_FAILED(msg) \
                               3202                 :                :     do { \
                               3203                 :                :         switch (encryption_negotiation_failed(conn)) \
                               3204                 :                :         { \
                               3205                 :                :             case 0: \
                               3206                 :                :                 libpq_append_conn_error(conn, (msg)); \
                               3207                 :                :                 goto error_return; \
                               3208                 :                :             case 1: \
                               3209                 :                :                 conn->status = CONNECTION_MADE; \
                               3210                 :                :                 return PGRES_POLLING_WRITING; \
                               3211                 :                :             case 2: \
                               3212                 :                :                 need_new_connection = true; \
                               3213                 :                :                 goto keep_going; \
                               3214                 :                :         } \
                               3215                 :                :     } while(0);
                               3216                 :                : 
                               3217                 :                :     /*
                               3218                 :                :      * Decide what to do next, if connection fails.  If there are no options
                               3219                 :                :      * left, return with an error.  The error message has already been written
                               3220                 :                :      * to the connection's error buffer.
                               3221                 :                :      */
                               3222                 :                : #define CONNECTION_FAILED() \
                               3223                 :                :     do { \
                               3224                 :                :         if (connection_failed(conn)) \
                               3225                 :                :         { \
                               3226                 :                :             need_new_connection = true; \
                               3227                 :                :             goto keep_going; \
                               3228                 :                :         } \
                               3229                 :                :         else \
                               3230                 :                :             goto error_return; \
                               3231                 :                :     } while(0);
                               3232                 :                : 
                               3233                 :                :     /* Now try to advance the state machine for this connection */
 9379 bruce@momjian.us         3234   [ +  +  +  +  :         473504 :     switch (conn->status)
                                     +  +  +  +  +  
                                        -  -  -  - ]
                               3235                 :                :     {
 8227 tgl@sss.pgh.pa.us        3236                 :          13783 :         case CONNECTION_NEEDED:
                               3237                 :                :             {
                               3238                 :                :                 /*
                               3239                 :                :                  * Try to initiate a connection to one of the addresses
                               3240                 :                :                  * returned by pg_getaddrinfo_all().  conn->whichaddr is the
                               3241                 :                :                  * next one to try.
                               3242                 :                :                  *
                               3243                 :                :                  * The extra level of braces here is historical.  It's not
                               3244                 :                :                  * worth reindenting this whole switch case to remove 'em.
                               3245                 :                :                  */
                               3246                 :                :                 {
                               3247                 :                :                     char        host_addr[NI_MAXHOST];
                               3248                 :                :                     int         sock_type;
                               3249                 :                :                     AddrInfo   *addr_cur;
                               3250                 :                : 
                               3251                 :                :                     /*
                               3252                 :                :                      * Advance to next possible host, if we've tried all of
                               3253                 :                :                      * the addresses for the current host.
                               3254                 :                :                      */
  993 dgustafsson@postgres     3255         [ +  + ]:          13783 :                     if (conn->whichaddr == conn->naddr)
                               3256                 :                :                     {
 2689 tgl@sss.pgh.pa.us        3257                 :            225 :                         conn->try_next_host = true;
                               3258                 :          13432 :                         goto keep_going;
                               3259                 :                :                     }
  993 dgustafsson@postgres     3260                 :          13558 :                     addr_cur = &conn->addr[conn->whichaddr];
                               3261                 :                : 
                               3262                 :                :                     /* Remember current address for possible use later */
                               3263                 :          13558 :                     memcpy(&conn->raddr, &addr_cur->addr, sizeof(SockAddr));
                               3264                 :                : 
                               3265                 :                : #ifdef ENABLE_GSS
                               3266                 :                : 
                               3267                 :                :                     /*
                               3268                 :                :                      * Before establishing the connection, check if it's
                               3269                 :                :                      * doomed to fail because gssencmode='require' but GSSAPI
                               3270                 :                :                      * is not available.
                               3271                 :                :                      */
  617 heikki.linnakangas@i     3272         [ +  + ]:          13558 :                     if (conn->gssencmode[0] == 'r')
                               3273                 :                :                     {
                               3274         [ +  + ]:             67 :                         if (conn->raddr.addr.ss_family == AF_UNIX)
                               3275                 :                :                         {
                               3276                 :              1 :                             libpq_append_conn_error(conn,
                               3277                 :                :                                                     "GSSAPI encryption required but it is not supported over a local socket");
                               3278                 :              6 :                             goto error_return;
                               3279                 :                :                         }
                               3280         [ +  - ]:             66 :                         if (conn->gcred == GSS_C_NO_CREDENTIAL)
                               3281                 :                :                         {
                               3282         [ +  + ]:             66 :                             if (!pg_GSS_have_cred_cache(&conn->gcred))
                               3283                 :                :                             {
                               3284                 :              5 :                                 libpq_append_conn_error(conn,
                               3285                 :                :                                                         "GSSAPI encryption required but no credential cache");
                               3286                 :              5 :                                 goto error_return;
                               3287                 :                :                             }
                               3288                 :                :                         }
                               3289                 :                :                     }
                               3290                 :                : #endif
                               3291                 :                : 
                               3292                 :                :                     /*
                               3293                 :                :                      * Choose the encryption method to try first.  Do this
                               3294                 :                :                      * before establishing the connection, so that if none of
                               3295                 :                :                      * the modes allowed by the connections options are
                               3296                 :                :                      * available, we can error out before establishing the
                               3297                 :                :                      * connection.
                               3298                 :                :                      */
                               3299         [ -  + ]:          13552 :                     if (!init_allowed_encryption_methods(conn))
  617 heikki.linnakangas@i     3300                 :UBC           0 :                         goto error_return;
                               3301                 :                : 
                               3302                 :                :                     /*
                               3303                 :                :                      * Set connip, too.  Note we purposely ignore strdup
                               3304                 :                :                      * failure; not a big problem if it fails.
                               3305                 :                :                      */
 2584 alvherre@alvh.no-ip.     3306         [ +  + ]:CBC       13552 :                     if (conn->connip != NULL)
                               3307                 :                :                     {
                               3308                 :             32 :                         free(conn->connip);
                               3309                 :             32 :                         conn->connip = NULL;
                               3310                 :                :                     }
                               3311                 :          13552 :                     getHostaddr(conn, host_addr, NI_MAXHOST);
 1800 tgl@sss.pgh.pa.us        3312         [ +  + ]:          13552 :                     if (host_addr[0])
 2584 alvherre@alvh.no-ip.     3313                 :            345 :                         conn->connip = strdup(host_addr);
                               3314                 :                : 
                               3315                 :                :                     /* Try to create the socket */
 1005 tmunro@postgresql.or     3316                 :          13552 :                     sock_type = SOCK_STREAM;
                               3317                 :                : #ifdef SOCK_CLOEXEC
                               3318                 :                : 
                               3319                 :                :                     /*
                               3320                 :                :                      * Atomically mark close-on-exec, if possible on this
                               3321                 :                :                      * platform, so that there isn't a window where a
                               3322                 :                :                      * subprogram executed by another thread inherits the
                               3323                 :                :                      * socket.  See fallback code below.
                               3324                 :                :                      */
                               3325                 :          13552 :                     sock_type |= SOCK_CLOEXEC;
                               3326                 :                : #endif
                               3327                 :                : #ifdef SOCK_NONBLOCK
                               3328                 :                : 
                               3329                 :                :                     /*
                               3330                 :                :                      * We might as well skip a system call for nonblocking
                               3331                 :                :                      * mode too, if we can.
                               3332                 :                :                      */
                               3333                 :          13552 :                     sock_type |= SOCK_NONBLOCK;
                               3334                 :                : #endif
  993 dgustafsson@postgres     3335                 :          13552 :                     conn->sock = socket(addr_cur->family, sock_type, 0);
 4262 bruce@momjian.us         3336         [ -  + ]:          13552 :                     if (conn->sock == PGINVALID_SOCKET)
                               3337                 :                :                     {
 1800 tgl@sss.pgh.pa.us        3338                 :UBC           0 :                         int         errorno = SOCK_ERRNO;
                               3339                 :                : 
                               3340                 :                :                         /*
                               3341                 :                :                          * Silently ignore socket() failure if we have more
                               3342                 :                :                          * addresses to try; this reduces useless chatter in
                               3343                 :                :                          * cases where the address list includes both IPv4 and
                               3344                 :                :                          * IPv6 but kernel only accepts one family.
                               3345                 :                :                          */
  993 dgustafsson@postgres     3346         [ #  # ]:              0 :                         if (conn->whichaddr < conn->naddr ||
 3330 rhaas@postgresql.org     3347         [ #  # ]:              0 :                             conn->whichhost + 1 < conn->nconnhost)
                               3348                 :                :                         {
 2689 tgl@sss.pgh.pa.us        3349                 :              0 :                             conn->try_next_addr = true;
                               3350                 :              0 :                             goto keep_going;
                               3351                 :                :                         }
 1790                          3352                 :              0 :                         emitHostIdentityInfo(conn, host_addr);
 1127 peter@eisentraut.org     3353                 :              0 :                         libpq_append_conn_error(conn, "could not create socket: %s",
                               3354                 :                :                                                 SOCK_STRERROR(errorno, sebuf, sizeof(sebuf)));
 2689 tgl@sss.pgh.pa.us        3355                 :              0 :                         goto error_return;
                               3356                 :                :                     }
                               3357                 :                : 
                               3358                 :                :                     /*
                               3359                 :                :                      * Once we've identified a target address, all errors
                               3360                 :                :                      * except the preceding socket()-failure case should be
                               3361                 :                :                      * prefixed with host-identity information.  (If the
                               3362                 :                :                      * connection succeeds, the contents of conn->errorMessage
                               3363                 :                :                      * won't matter, so this is harmless.)
                               3364                 :                :                      */
 1790 tgl@sss.pgh.pa.us        3365                 :CBC       13552 :                     emitHostIdentityInfo(conn, host_addr);
                               3366                 :                : 
                               3367                 :                :                     /*
                               3368                 :                :                      * Select socket options: no delay of outgoing data for
                               3369                 :                :                      * TCP sockets, nonblock mode, close-on-exec.  Try the
                               3370                 :                :                      * next address if any of this fails.
                               3371                 :                :                      */
  993 dgustafsson@postgres     3372         [ +  + ]:          13552 :                     if (addr_cur->family != AF_UNIX)
                               3373                 :                :                     {
 8227 tgl@sss.pgh.pa.us        3374         [ -  + ]:            345 :                         if (!connectNoDelay(conn))
                               3375                 :                :                         {
                               3376                 :                :                             /* error message already created */
 2689 tgl@sss.pgh.pa.us        3377                 :UBC           0 :                             conn->try_next_addr = true;
                               3378                 :              0 :                             goto keep_going;
                               3379                 :                :                         }
                               3380                 :                :                     }
                               3381                 :                : #ifndef SOCK_NONBLOCK
                               3382                 :                :                     if (!pg_set_noblock(conn->sock))
                               3383                 :                :                     {
                               3384                 :                :                         libpq_append_conn_error(conn, "could not set socket to nonblocking mode: %s",
                               3385                 :                :                                                 SOCK_STRERROR(SOCK_ERRNO, sebuf, sizeof(sebuf)));
                               3386                 :                :                         conn->try_next_addr = true;
                               3387                 :                :                         goto keep_going;
                               3388                 :                :                     }
                               3389                 :                : #endif
                               3390                 :                : 
                               3391                 :                : #ifndef SOCK_CLOEXEC
                               3392                 :                : #ifdef F_SETFD
                               3393                 :                :                     if (fcntl(conn->sock, F_SETFD, FD_CLOEXEC) == -1)
                               3394                 :                :                     {
                               3395                 :                :                         libpq_append_conn_error(conn, "could not set socket to close-on-exec mode: %s",
                               3396                 :                :                                                 SOCK_STRERROR(SOCK_ERRNO, sebuf, sizeof(sebuf)));
                               3397                 :                :                         conn->try_next_addr = true;
                               3398                 :                :                         goto keep_going;
                               3399                 :                :                     }
                               3400                 :                : #endif                          /* F_SETFD */
                               3401                 :                : #endif
                               3402                 :                : 
  993 dgustafsson@postgres     3403         [ +  + ]:CBC       13552 :                     if (addr_cur->family != AF_UNIX)
                               3404                 :                :                     {
                               3405                 :                : #ifndef WIN32
 5642 bruce@momjian.us         3406                 :            345 :                         int         on = 1;
                               3407                 :                : #endif
                               3408                 :            345 :                         int         usekeepalives = useKeepalives(conn);
                               3409                 :            345 :                         int         err = 0;
                               3410                 :                : 
 5655 rhaas@postgresql.org     3411         [ -  + ]:            345 :                         if (usekeepalives < 0)
                               3412                 :                :                         {
                               3413                 :                :                             /* error is already reported */
 5655 rhaas@postgresql.org     3414                 :UBC           0 :                             err = 1;
                               3415                 :                :                         }
 5655 rhaas@postgresql.org     3416         [ +  - ]:CBC         345 :                         else if (usekeepalives == 0)
                               3417                 :                :                         {
                               3418                 :                :                             /* Do nothing */
                               3419                 :                :                         }
                               3420                 :                : #ifndef WIN32
                               3421         [ -  + ]:            345 :                         else if (setsockopt(conn->sock,
                               3422                 :                :                                             SOL_SOCKET, SO_KEEPALIVE,
                               3423                 :                :                                             (char *) &on, sizeof(on)) < 0)
                               3424                 :                :                         {
 1127 peter@eisentraut.org     3425                 :UBC           0 :                             libpq_append_conn_error(conn, "%s(%s) failed: %s",
                               3426                 :                :                                                     "setsockopt",
                               3427                 :                :                                                     "SO_KEEPALIVE",
 1013 michael@paquier.xyz      3428                 :              0 :                                                     SOCK_STRERROR(SOCK_ERRNO, sebuf, sizeof(sebuf)));
 5655 rhaas@postgresql.org     3429                 :              0 :                             err = 1;
                               3430                 :                :                         }
 5655 rhaas@postgresql.org     3431         [ +  - ]:CBC         345 :                         else if (!setKeepalivesIdle(conn)
                               3432         [ +  - ]:            345 :                                  || !setKeepalivesInterval(conn)
                               3433         [ -  + ]:            345 :                                  || !setKeepalivesCount(conn))
 5655 rhaas@postgresql.org     3434                 :UBC           0 :                             err = 1;
                               3435                 :                : #else                           /* WIN32 */
                               3436                 :                : #ifdef SIO_KEEPALIVE_VALS
                               3437                 :                :                         else if (!prepKeepalivesWin32(conn))
                               3438                 :                :                             err = 1;
                               3439                 :                : #endif                          /* SIO_KEEPALIVE_VALS */
                               3440                 :                : #endif                          /* WIN32 */
 2446 michael@paquier.xyz      3441         [ -  + ]:CBC         345 :                         else if (!setTCPUserTimeout(conn))
 2446 michael@paquier.xyz      3442                 :UBC           0 :                             err = 1;
                               3443                 :                : 
 5655 rhaas@postgresql.org     3444         [ -  + ]:CBC         345 :                         if (err)
                               3445                 :                :                         {
 2689 tgl@sss.pgh.pa.us        3446                 :UBC           0 :                             conn->try_next_addr = true;
                               3447                 :              0 :                             goto keep_going;
                               3448                 :                :                         }
                               3449                 :                :                     }
                               3450                 :                : 
                               3451                 :                :                     /*----------
                               3452                 :                :                      * We have three methods of blocking SIGPIPE during
                               3453                 :                :                      * send() calls to this socket:
                               3454                 :                :                      *
                               3455                 :                :                      *  - setsockopt(sock, SO_NOSIGPIPE)
                               3456                 :                :                      *  - send(sock, ..., MSG_NOSIGNAL)
                               3457                 :                :                      *  - setting the signal mask to SIG_IGN during send()
                               3458                 :                :                      *
                               3459                 :                :                      * The third method requires three syscalls per send,
                               3460                 :                :                      * so we prefer either of the first two, but they are
                               3461                 :                :                      * less portable.  The state is tracked in the following
                               3462                 :                :                      * members of PGconn:
                               3463                 :                :                      *
                               3464                 :                :                      * conn->sigpipe_so      - we have set up SO_NOSIGPIPE
                               3465                 :                :                      * conn->sigpipe_flag    - we're specifying MSG_NOSIGNAL
                               3466                 :                :                      *
                               3467                 :                :                      * If we can use SO_NOSIGPIPE, then set sigpipe_so here
                               3468                 :                :                      * and we're done.  Otherwise, set sigpipe_flag so that
                               3469                 :                :                      * we will try MSG_NOSIGNAL on sends.  If we get an error
                               3470                 :                :                      * with MSG_NOSIGNAL, we'll clear that flag and revert to
                               3471                 :                :                      * signal masking.
                               3472                 :                :                      *----------
                               3473                 :                :                      */
 5989 tgl@sss.pgh.pa.us        3474                 :CBC       13552 :                     conn->sigpipe_so = false;
                               3475                 :                : #ifdef MSG_NOSIGNAL
                               3476                 :          13552 :                     conn->sigpipe_flag = true;
                               3477                 :                : #else
                               3478                 :                :                     conn->sigpipe_flag = false;
                               3479                 :                : #endif                          /* MSG_NOSIGNAL */
                               3480                 :                : 
                               3481                 :                : #ifdef SO_NOSIGPIPE
                               3482                 :                :                     optval = 1;
                               3483                 :                :                     if (setsockopt(conn->sock, SOL_SOCKET, SO_NOSIGPIPE,
                               3484                 :                :                                    (char *) &optval, sizeof(optval)) == 0)
                               3485                 :                :                     {
                               3486                 :                :                         conn->sigpipe_so = true;
                               3487                 :                :                         conn->sigpipe_flag = false;
                               3488                 :                :                     }
                               3489                 :                : #endif                          /* SO_NOSIGPIPE */
                               3490                 :                : 
                               3491                 :                :                     /*
                               3492                 :                :                      * Start/make connection.  This should not block, since we
                               3493                 :                :                      * are in nonblock mode.  If it does, well, too bad.
                               3494                 :                :                      */
  993 dgustafsson@postgres     3495         [ +  + ]:          13552 :                     if (connect(conn->sock, (struct sockaddr *) &addr_cur->addr.addr,
                               3496                 :                :                                 addr_cur->addr.salen) < 0)
                               3497                 :                :                     {
 8227 tgl@sss.pgh.pa.us        3498         [ +  + ]:            569 :                         if (SOCK_ERRNO == EINPROGRESS ||
                               3499                 :                : #ifdef WIN32
                               3500                 :                :                             SOCK_ERRNO == EWOULDBLOCK ||
                               3501                 :                : #endif
 4555                          3502         [ -  + ]:            224 :                             SOCK_ERRNO == EINTR)
                               3503                 :                :                         {
                               3504                 :                :                             /*
                               3505                 :                :                              * This is fine - we're in non-blocking mode, and
                               3506                 :                :                              * the connection is in progress.  Tell caller to
                               3507                 :                :                              * wait for write-ready on socket.
                               3508                 :                :                              */
 8227                          3509                 :            345 :                             conn->status = CONNECTION_STARTED;
                               3510                 :            345 :                             return PGRES_POLLING_WRITING;
                               3511                 :                :                         }
                               3512                 :                :                         /* otherwise, trouble */
                               3513                 :                :                     }
                               3514                 :                :                     else
                               3515                 :                :                     {
                               3516                 :                :                         /*
                               3517                 :                :                          * Hm, we're connected already --- seems the "nonblock
                               3518                 :                :                          * connection" wasn't.  Advance the state machine and
                               3519                 :                :                          * go do the next stuff.
                               3520                 :                :                          */
                               3521                 :          12983 :                         conn->status = CONNECTION_STARTED;
                               3522                 :          12983 :                         goto keep_going;
                               3523                 :                :                     }
                               3524                 :                : 
                               3525                 :                :                     /*
                               3526                 :                :                      * This connection failed.  Add the error report to
                               3527                 :                :                      * conn->errorMessage, then try the next address if any.
                               3528                 :                :                      */
                               3529                 :            224 :                     connectFailureMessage(conn, SOCK_ERRNO);
 2689                          3530                 :            224 :                     conn->try_next_addr = true;
                               3531                 :            224 :                     goto keep_going;
                               3532                 :                :                 }
                               3533                 :                :             }
                               3534                 :                : 
 9513 bruce@momjian.us         3535                 :          13328 :         case CONNECTION_STARTED:
                               3536                 :                :             {
 1498 peter@eisentraut.org     3537                 :          13328 :                 socklen_t   optlen = sizeof(optval);
                               3538                 :                : 
                               3539                 :                :                 /*
                               3540                 :                :                  * Write ready, since we've made it here, so the connection
                               3541                 :                :                  * has been made ... or has failed.
                               3542                 :                :                  */
                               3543                 :                : 
                               3544                 :                :                 /*
                               3545                 :                :                  * Now check (using getsockopt) that there is not an error
                               3546                 :                :                  * state waiting for us on the socket.
                               3547                 :                :                  */
                               3548                 :                : 
 9379 bruce@momjian.us         3549         [ -  + ]:          13328 :                 if (getsockopt(conn->sock, SOL_SOCKET, SO_ERROR,
                               3550                 :                :                                (char *) &optval, &optlen) == -1)
                               3551                 :                :                 {
 1127 peter@eisentraut.org     3552                 :UBC           0 :                     libpq_append_conn_error(conn, "could not get socket error status: %s",
 1013 michael@paquier.xyz      3553                 :              0 :                                             SOCK_STRERROR(SOCK_ERRNO, sebuf, sizeof(sebuf)));
 9379 bruce@momjian.us         3554                 :              0 :                     goto error_return;
                               3555                 :                :                 }
 9379 bruce@momjian.us         3556         [ -  + ]:CBC       13328 :                 else if (optval != 0)
                               3557                 :                :                 {
                               3558                 :                :                     /*
                               3559                 :                :                      * When using a nonblocking connect, we will typically see
                               3560                 :                :                      * connect failures at this point, so provide a friendly
                               3561                 :                :                      * error message.
                               3562                 :                :                      */
 8920 peter_e@gmx.net          3563                 :UBC           0 :                     connectFailureMessage(conn, optval);
                               3564                 :                : 
                               3565                 :                :                     /*
                               3566                 :                :                      * Try the next address if any, just as in the case where
                               3567                 :                :                      * connect() returned failure immediately.
                               3568                 :                :                      */
 2689 tgl@sss.pgh.pa.us        3569                 :              0 :                     conn->try_next_addr = true;
                               3570                 :              0 :                     goto keep_going;
                               3571                 :                :                 }
                               3572                 :                : 
                               3573                 :                :                 /* Fill in the client address */
 8223 bruce@momjian.us         3574                 :CBC       13328 :                 conn->laddr.salen = sizeof(conn->laddr.addr);
 8170                          3575         [ -  + ]:          13328 :                 if (getsockname(conn->sock,
 3100 tgl@sss.pgh.pa.us        3576                 :          13328 :                                 (struct sockaddr *) &conn->laddr.addr,
                               3577                 :                :                                 &conn->laddr.salen) < 0)
                               3578                 :                :                 {
 1127 peter@eisentraut.org     3579                 :UBC           0 :                     libpq_append_conn_error(conn, "could not get client address from socket: %s",
 1013 michael@paquier.xyz      3580                 :              0 :                                             SOCK_STRERROR(SOCK_ERRNO, sebuf, sizeof(sebuf)));
 9379 bruce@momjian.us         3581                 :              0 :                     goto error_return;
                               3582                 :                :                 }
                               3583                 :                : 
                               3584                 :                :                 /*
                               3585                 :                :                  * Implement requirepeer check, if requested and it's a
                               3586                 :                :                  * Unix-domain socket.
                               3587                 :                :                  */
 5313 tgl@sss.pgh.pa.us        3588   [ -  +  -  - ]:CBC       13328 :                 if (conn->requirepeer && conn->requirepeer[0] &&
 1400 peter@eisentraut.org     3589         [ #  # ]:UBC           0 :                     conn->raddr.addr.ss_family == AF_UNIX)
                               3590                 :                :                 {
                               3591                 :                : #ifndef WIN32
                               3592                 :                :                     char       *remote_username;
                               3593                 :                : #endif
                               3594                 :                :                     uid_t       uid;
                               3595                 :                :                     gid_t       gid;
                               3596                 :                : 
 5630 peter_e@gmx.net          3597                 :              0 :                     errno = 0;
      tgl@sss.pgh.pa.us        3598         [ #  # ]:              0 :                     if (getpeereid(conn->sock, &uid, &gid) != 0)
                               3599                 :                :                     {
                               3600                 :                :                         /*
                               3601                 :                :                          * Provide special error message if getpeereid is a
                               3602                 :                :                          * stub
                               3603                 :                :                          */
 5311                          3604         [ #  # ]:              0 :                         if (errno == ENOSYS)
 1127 peter@eisentraut.org     3605                 :              0 :                             libpq_append_conn_error(conn, "requirepeer parameter is not supported on this platform");
                               3606                 :                :                         else
                               3607                 :              0 :                             libpq_append_conn_error(conn, "could not get peer credentials: %s",
 1013 michael@paquier.xyz      3608                 :              0 :                                                     strerror_r(errno, sebuf, sizeof(sebuf)));
 5630 peter_e@gmx.net          3609                 :              0 :                         goto error_return;
                               3610                 :                :                     }
                               3611                 :                : 
                               3612                 :                : #ifndef WIN32
 1435 tgl@sss.pgh.pa.us        3613                 :              0 :                     remote_username = pg_fe_getusername(uid,
                               3614                 :                :                                                         &conn->errorMessage);
                               3615         [ #  # ]:              0 :                     if (remote_username == NULL)
                               3616                 :              0 :                         goto error_return;  /* message already logged */
                               3617                 :                : 
                               3618         [ #  # ]:              0 :                     if (strcmp(remote_username, conn->requirepeer) != 0)
                               3619                 :                :                     {
 1127 peter@eisentraut.org     3620                 :              0 :                         libpq_append_conn_error(conn, "requirepeer specifies \"%s\", but actual peer user name is \"%s\"",
                               3621                 :                :                                                 conn->requirepeer, remote_username);
 1435 tgl@sss.pgh.pa.us        3622                 :              0 :                         free(remote_username);
 5630 peter_e@gmx.net          3623                 :              0 :                         goto error_return;
                               3624                 :                :                     }
 1435 tgl@sss.pgh.pa.us        3625                 :              0 :                     free(remote_username);
                               3626                 :                : #else                           /* WIN32 */
                               3627                 :                :                     /* should have failed with ENOSYS above */
                               3628                 :                :                     Assert(false);
                               3629                 :                : #endif                          /* WIN32 */
                               3630                 :                :                 }
                               3631                 :                : 
                               3632                 :                :                 /*
                               3633                 :                :                  * Make sure we can write before advancing to next step.
                               3634                 :                :                  */
  617 heikki.linnakangas@i     3635                 :CBC       13328 :                 conn->status = CONNECTION_MADE;
                               3636                 :          13328 :                 return PGRES_POLLING_WRITING;
                               3637                 :                :             }
                               3638                 :                : 
                               3639                 :          13598 :         case CONNECTION_MADE:
                               3640                 :                :             {
                               3641                 :                :                 char       *startpacket;
                               3642                 :                :                 int         packetlen;
                               3643                 :                : 
                               3644                 :                : #ifdef ENABLE_GSS
                               3645                 :                : 
                               3646                 :                :                 /*
                               3647                 :                :                  * If GSSAPI encryption is enabled, send a packet to the
                               3648                 :                :                  * server asking for GSSAPI Encryption and proceed with GSSAPI
                               3649                 :                :                  * handshake.  We will come back here after GSSAPI encryption
                               3650                 :                :                  * has been established, with conn->gctx set.
                               3651                 :                :                  */
                               3652   [ +  +  +  + ]:          13598 :                 if (conn->current_enc_method == ENC_GSSAPI && !conn->gctx)
                               3653                 :                :                 {
 2449 sfrost@snowman.net       3654                 :            121 :                     ProtocolVersion pv = pg_hton32(NEGOTIATE_GSS_CODE);
                               3655                 :                : 
                               3656         [ -  + ]:            121 :                     if (pqPacketSend(conn, 0, &pv, sizeof(pv)) != STATUS_OK)
                               3657                 :                :                     {
 1127 peter@eisentraut.org     3658                 :UBC           0 :                         libpq_append_conn_error(conn, "could not send GSSAPI negotiation packet: %s",
 1013 michael@paquier.xyz      3659                 :              0 :                                                 SOCK_STRERROR(SOCK_ERRNO, sebuf, sizeof(sebuf)));
 2449 sfrost@snowman.net       3660                 :              0 :                         goto error_return;
                               3661                 :                :                     }
                               3662                 :                : 
                               3663                 :                :                     /* Ok, wait for response */
 2449 sfrost@snowman.net       3664                 :CBC         121 :                     conn->status = CONNECTION_GSS_STARTUP;
                               3665                 :            121 :                     return PGRES_POLLING_READING;
                               3666                 :                :                 }
                               3667                 :                : #endif
                               3668                 :                : 
                               3669                 :                : #ifdef USE_SSL
                               3670                 :                : 
                               3671                 :                :                 /*
                               3672                 :                :                  * If SSL is enabled, start the SSL negotiation. We will come
                               3673                 :                :                  * back here after SSL encryption has been established, with
                               3674                 :                :                  * ssl_in_use set.
                               3675                 :                :                  */
  579 heikki.linnakangas@i     3676   [ +  +  +  + ]:          13477 :                 if (conn->current_enc_method == ENC_SSL && !conn->ssl_in_use)
                               3677                 :                :                 {
                               3678                 :                :                     /*
                               3679                 :                :                      * If traditional postgres SSL negotiation is used, send
                               3680                 :                :                      * the SSL request.  In direct negotiation, jump straight
                               3681                 :                :                      * into the SSL handshake.
                               3682                 :                :                      */
                               3683         [ +  + ]:            188 :                     if (conn->sslnegotiation[0] == 'p')
                               3684                 :                :                     {
                               3685                 :                :                         ProtocolVersion pv;
                               3686                 :                : 
                               3687                 :                :                         /*
                               3688                 :                :                          * Send the SSL request packet.
                               3689                 :                :                          *
                               3690                 :                :                          * Theoretically, this could block, but it really
                               3691                 :                :                          * shouldn't since we only got here if the socket is
                               3692                 :                :                          * write-ready.
                               3693                 :                :                          */
                               3694                 :            172 :                         pv = pg_hton32(NEGOTIATE_SSL_CODE);
                               3695         [ -  + ]:            172 :                         if (pqPacketSend(conn, 0, &pv, sizeof(pv)) != STATUS_OK)
                               3696                 :                :                         {
  579 heikki.linnakangas@i     3697                 :UBC           0 :                             libpq_append_conn_error(conn, "could not send SSL negotiation packet: %s",
                               3698                 :              0 :                                                     SOCK_STRERROR(SOCK_ERRNO, sebuf, sizeof(sebuf)));
                               3699                 :              0 :                             goto error_return;
                               3700                 :                :                         }
                               3701                 :                :                         /* Ok, wait for response */
  579 heikki.linnakangas@i     3702                 :CBC         172 :                         conn->status = CONNECTION_SSL_STARTUP;
                               3703                 :            172 :                         return PGRES_POLLING_READING;
                               3704                 :                :                     }
                               3705                 :                :                     else
                               3706                 :                :                     {
                               3707         [ -  + ]:             16 :                         Assert(conn->sslnegotiation[0] == 'd');
                               3708                 :             16 :                         conn->status = CONNECTION_SSL_STARTUP;
                               3709                 :             16 :                         return PGRES_POLLING_WRITING;
                               3710                 :                :                     }
                               3711                 :                :                 }
                               3712                 :                : #endif                          /* USE_SSL */
                               3713                 :                : 
                               3714                 :                :                 /*
                               3715                 :                :                  * For cancel requests this is as far as we need to go in the
                               3716                 :                :                  * connection establishment. Now we can actually send our
                               3717                 :                :                  * cancellation request.
                               3718                 :                :                  */
  644 alvherre@alvh.no-ip.     3719         [ +  + ]:          13289 :                 if (conn->cancelRequest)
                               3720                 :                :                 {
  258 heikki.linnakangas@i     3721         [ -  + ]:              9 :                     if (PQsendCancelRequest(conn) != STATUS_OK)
                               3722                 :                :                     {
  644 alvherre@alvh.no-ip.     3723                 :UBC           0 :                         libpq_append_conn_error(conn, "could not send cancel packet: %s",
                               3724                 :              0 :                                                 SOCK_STRERROR(SOCK_ERRNO, sebuf, sizeof(sebuf)));
                               3725                 :              0 :                         goto error_return;
                               3726                 :                :                     }
  644 alvherre@alvh.no-ip.     3727                 :CBC           9 :                     conn->status = CONNECTION_AWAITING_RESPONSE;
                               3728                 :              9 :                     return PGRES_POLLING_READING;
                               3729                 :                :                 }
                               3730                 :                : 
                               3731                 :                :                 /*
                               3732                 :                :                  * We have now established encryption, or we are happy to
                               3733                 :                :                  * proceed without.
                               3734                 :                :                  */
                               3735                 :                : 
                               3736                 :                :                 /* Build the startup packet. */
 1748 heikki.linnakangas@i     3737                 :          13280 :                 startpacket = pqBuildStartupPacket3(conn, &packetlen,
                               3738                 :                :                                                     EnvironmentOptions);
 8279 tgl@sss.pgh.pa.us        3739         [ -  + ]:          13280 :                 if (!startpacket)
                               3740                 :                :                 {
 1127 peter@eisentraut.org     3741                 :UBC           0 :                     libpq_append_conn_error(conn, "out of memory");
 8279 tgl@sss.pgh.pa.us        3742                 :              0 :                     goto error_return;
                               3743                 :                :                 }
                               3744                 :                : 
                               3745                 :                :                 /*
                               3746                 :                :                  * Send the startup packet.
                               3747                 :                :                  *
                               3748                 :                :                  * Theoretically, this could block, but it really shouldn't
                               3749                 :                :                  * since we only got here if the socket is write-ready.
                               3750                 :                :                  */
 8279 tgl@sss.pgh.pa.us        3751         [ -  + ]:CBC       13280 :                 if (pqPacketSend(conn, 0, startpacket, packetlen) != STATUS_OK)
                               3752                 :                :                 {
 1127 peter@eisentraut.org     3753                 :UBC           0 :                     libpq_append_conn_error(conn, "could not send startup packet: %s",
 1013 michael@paquier.xyz      3754                 :              0 :                                             SOCK_STRERROR(SOCK_ERRNO, sebuf, sizeof(sebuf)));
 8279 tgl@sss.pgh.pa.us        3755                 :              0 :                     free(startpacket);
 9379 bruce@momjian.us         3756                 :              0 :                     goto error_return;
                               3757                 :                :                 }
                               3758                 :                : 
 8279 tgl@sss.pgh.pa.us        3759                 :CBC       13280 :                 free(startpacket);
                               3760                 :                : 
 9379 bruce@momjian.us         3761                 :          13280 :                 conn->status = CONNECTION_AWAITING_RESPONSE;
 9513                          3762                 :          13280 :                 return PGRES_POLLING_READING;
                               3763                 :                :             }
                               3764                 :                : 
                               3765                 :                :             /*
                               3766                 :                :              * Handle SSL negotiation: wait for postmaster messages and
                               3767                 :                :              * respond as necessary.
                               3768                 :                :              */
 8227 tgl@sss.pgh.pa.us        3769                 :            331 :         case CONNECTION_SSL_STARTUP:
                               3770                 :                :             {
                               3771                 :                : #ifdef USE_SSL
                               3772                 :                :                 PostgresPollingStatusType pollres;
                               3773                 :                : 
                               3774                 :                :                 /*
                               3775                 :                :                  * On first time through with traditional SSL negotiation, get
                               3776                 :                :                  * the postmaster's response to our SSLRequest packet. With
                               3777                 :                :                  * sslnegotiation='direct', go straight to initiating SSL.
                               3778                 :                :                  */
  579 heikki.linnakangas@i     3779   [ +  +  +  + ]:            331 :                 if (!conn->ssl_in_use && conn->sslnegotiation[0] == 'p')
                               3780                 :                :                 {
                               3781                 :                :                     /*
                               3782                 :                :                      * We use pqReadData here since it has the logic to
                               3783                 :                :                      * distinguish no-data-yet from connection closure. Since
                               3784                 :                :                      * conn->ssl isn't set, a plain recv() will occur.
                               3785                 :                :                      */
                               3786                 :                :                     char        SSLok;
                               3787                 :                :                     int         rdresult;
                               3788                 :                : 
 7649 tgl@sss.pgh.pa.us        3789                 :            172 :                     rdresult = pqReadData(conn);
                               3790         [ -  + ]:            172 :                     if (rdresult < 0)
                               3791                 :                :                     {
                               3792                 :                :                         /* errorMessage is already filled in */
 8227                          3793                 :              7 :                         goto error_return;
                               3794                 :                :                     }
 7649                          3795         [ -  + ]:            172 :                     if (rdresult == 0)
                               3796                 :                :                     {
                               3797                 :                :                         /* caller failed to wait for data */
 8227                          3798                 :             20 :                         return PGRES_POLLING_READING;
                               3799                 :                :                     }
 7649                          3800         [ -  + ]:            172 :                     if (pqGetc(&SSLok, conn) < 0)
                               3801                 :                :                     {
                               3802                 :                :                         /* should not happen really */
 7649 tgl@sss.pgh.pa.us        3803                 :UBC           0 :                         return PGRES_POLLING_READING;
                               3804                 :                :                     }
 8227 tgl@sss.pgh.pa.us        3805         [ +  + ]:CBC         172 :                     if (SSLok == 'S')
                               3806                 :                :                     {
  489 alvherre@alvh.no-ip.     3807         [ -  + ]:            145 :                         if (conn->Pfdebug)
  489 alvherre@alvh.no-ip.     3808                 :UBC           0 :                             pqTraceOutputCharResponse(conn, "SSLResponse",
                               3809                 :                :                                                       SSLok);
                               3810                 :                :                         /* mark byte consumed */
 5225 tgl@sss.pgh.pa.us        3811                 :CBC         145 :                         conn->inStart = conn->inCursor;
                               3812                 :                :                     }
 8227                          3813         [ +  - ]:             27 :                     else if (SSLok == 'N')
                               3814                 :                :                     {
  489 alvherre@alvh.no-ip.     3815         [ -  + ]:             27 :                         if (conn->Pfdebug)
  489 alvherre@alvh.no-ip.     3816                 :UBC           0 :                             pqTraceOutputCharResponse(conn, "SSLResponse",
                               3817                 :                :                                                       SSLok);
                               3818                 :                :                         /* mark byte consumed */
 5225 tgl@sss.pgh.pa.us        3819                 :CBC          27 :                         conn->inStart = conn->inCursor;
                               3820                 :                : 
                               3821                 :                :                         /*
                               3822                 :                :                          * The connection is still valid, so if it's OK to
                               3823                 :                :                          * continue without SSL, we can proceed using this
                               3824                 :                :                          * connection.  Otherwise return with an error.
                               3825                 :                :                          */
  552 peter@eisentraut.org     3826   [ +  +  -  - ]:             27 :                         ENCRYPTION_NEGOTIATION_FAILED(libpq_gettext("server does not support SSL, but SSL was required"));
                               3827                 :                :                     }
 8227 tgl@sss.pgh.pa.us        3828         [ #  # ]:UBC           0 :                     else if (SSLok == 'E')
                               3829                 :                :                     {
                               3830                 :                :                         /*
                               3831                 :                :                          * Server failure of some sort, such as failure to
                               3832                 :                :                          * fork a backend process.  Don't bother retrieving
                               3833                 :                :                          * the error message; we should not trust it as the
                               3834                 :                :                          * server has not been authenticated yet.
                               3835                 :                :                          */
  400 michael@paquier.xyz      3836                 :              0 :                         libpq_append_conn_error(conn, "server sent an error response during SSL exchange");
                               3837                 :              0 :                         goto error_return;
                               3838                 :                :                     }
                               3839                 :                :                     else
                               3840                 :                :                     {
 1127 peter@eisentraut.org     3841                 :              0 :                         libpq_append_conn_error(conn, "received invalid response to SSL negotiation: %c",
                               3842                 :                :                                                 SSLok);
 8227 tgl@sss.pgh.pa.us        3843                 :              0 :                         goto error_return;
                               3844                 :                :                     }
                               3845                 :                :                 }
                               3846                 :                : 
                               3847                 :                :                 /*
                               3848                 :                :                  * Begin or continue the SSL negotiation process.
                               3849                 :                :                  */
 8227 tgl@sss.pgh.pa.us        3850                 :CBC         304 :                 pollres = pqsecure_open_client(conn);
                               3851         [ +  + ]:            304 :                 if (pollres == PGRES_POLLING_OK)
                               3852                 :                :                 {
                               3853                 :                :                     /*
                               3854                 :                :                      * At this point we should have no data already buffered.
                               3855                 :                :                      * If we do, it was received before we performed the SSL
                               3856                 :                :                      * handshake, so it wasn't encrypted and indeed may have
                               3857                 :                :                      * been injected by a man-in-the-middle.
                               3858                 :                :                      */
 1499                          3859         [ -  + ]:            129 :                     if (conn->inCursor != conn->inEnd)
                               3860                 :                :                     {
 1127 peter@eisentraut.org     3861                 :UBC           0 :                         libpq_append_conn_error(conn, "received unencrypted data after SSL response");
 1499 tgl@sss.pgh.pa.us        3862                 :              0 :                         goto error_return;
                               3863                 :                :                     }
                               3864                 :                : 
                               3865                 :                :                     /* SSL handshake done, ready to send startup packet */
 8227 tgl@sss.pgh.pa.us        3866                 :CBC         129 :                     conn->status = CONNECTION_MADE;
                               3867                 :            129 :                     return PGRES_POLLING_WRITING;
                               3868                 :                :                 }
 6965                          3869         [ +  + ]:            175 :                 if (pollres == PGRES_POLLING_FAILED)
                               3870                 :                :                 {
                               3871                 :                :                     /*
                               3872                 :                :                      * SSL handshake failed.  We will retry with a plaintext
                               3873                 :                :                      * connection, if permitted by sslmode.
                               3874                 :                :                      */
  617 heikki.linnakangas@i     3875         [ -  + ]:             32 :                     CONNECTION_FAILED();
                               3876                 :                :                 }
                               3877                 :                :                 /* Else, return POLLING_READING or POLLING_WRITING status */
 8227 tgl@sss.pgh.pa.us        3878                 :            143 :                 return pollres;
                               3879                 :                : #else                           /* !USE_SSL */
                               3880                 :                :                 /* can't get here */
                               3881                 :                :                 goto error_return;
                               3882                 :                : #endif                          /* USE_SSL */
                               3883                 :                :             }
                               3884                 :                : 
 2449 sfrost@snowman.net       3885                 :            352 :         case CONNECTION_GSS_STARTUP:
                               3886                 :                :             {
                               3887                 :                : #ifdef ENABLE_GSS
                               3888                 :                :                 PostgresPollingStatusType pollres;
                               3889                 :                : 
                               3890                 :                :                 /*
                               3891                 :                :                  * If we haven't yet, get the postmaster's response to our
                               3892                 :                :                  * negotiation packet
                               3893                 :                :                  */
  617 heikki.linnakangas@i     3894         [ +  + ]:            352 :                 if (!conn->gctx)
                               3895                 :                :                 {
                               3896                 :                :                     char        gss_ok;
 2449 sfrost@snowman.net       3897                 :            121 :                     int         rdresult = pqReadData(conn);
                               3898                 :                : 
                               3899         [ -  + ]:            121 :                     if (rdresult < 0)
                               3900                 :                :                         /* pqReadData fills in error message */
 2449 sfrost@snowman.net       3901                 :UBC           0 :                         goto error_return;
 2449 sfrost@snowman.net       3902         [ -  + ]:CBC         121 :                     else if (rdresult == 0)
                               3903                 :                :                         /* caller failed to wait for data */
 2449 sfrost@snowman.net       3904                 :UBC           0 :                         return PGRES_POLLING_READING;
 2449 sfrost@snowman.net       3905         [ -  + ]:CBC         121 :                     if (pqGetc(&gss_ok, conn) < 0)
                               3906                 :                :                         /* shouldn't happen... */
 2449 sfrost@snowman.net       3907                 :UBC           0 :                         return PGRES_POLLING_READING;
                               3908                 :                : 
 2449 sfrost@snowman.net       3909         [ -  + ]:CBC         121 :                     if (gss_ok == 'E')
                               3910                 :                :                     {
                               3911                 :                :                         /*
                               3912                 :                :                          * Server failure of some sort, possibly protocol
                               3913                 :                :                          * version support failure.  Don't bother retrieving
                               3914                 :                :                          * the error message; we should not trust it anyway as
                               3915                 :                :                          * the server has not authenticated yet.
                               3916                 :                :                          *
                               3917                 :                :                          * Note that unlike on an error response to
                               3918                 :                :                          * SSLRequest, we allow falling back to SSL or
                               3919                 :                :                          * plaintext connection here.  GSS support was
                               3920                 :                :                          * introduced in PostgreSQL version 12, so an error
                               3921                 :                :                          * response might mean that we are connecting to a
                               3922                 :                :                          * pre-v12 server.
                               3923                 :                :                          */
  400 michael@paquier.xyz      3924                 :UBC           0 :                         libpq_append_conn_error(conn, "server sent an error response during GSS encryption exchange");
                               3925         [ #  # ]:              0 :                         CONNECTION_FAILED();
                               3926                 :                :                     }
                               3927                 :                : 
                               3928                 :                :                     /* mark byte consumed */
 2449 sfrost@snowman.net       3929                 :CBC         121 :                     conn->inStart = conn->inCursor;
                               3930                 :                : 
                               3931         [ -  + ]:            121 :                     if (gss_ok == 'N')
                               3932                 :                :                     {
  489 alvherre@alvh.no-ip.     3933         [ #  # ]:UBC           0 :                         if (conn->Pfdebug)
                               3934                 :              0 :                             pqTraceOutputCharResponse(conn, "GSSENCResponse",
                               3935                 :                :                                                       gss_ok);
                               3936                 :                : 
                               3937                 :                :                         /*
                               3938                 :                :                          * The connection is still valid, so if it's OK to
                               3939                 :                :                          * continue without GSS, we can proceed using this
                               3940                 :                :                          * connection.  Otherwise return with an error.
                               3941                 :                :                          */
  552 peter@eisentraut.org     3942   [ #  #  #  # ]:              0 :                         ENCRYPTION_NEGOTIATION_FAILED(libpq_gettext("server doesn't support GSSAPI encryption, but it was required"));
                               3943                 :                :                     }
 2449 sfrost@snowman.net       3944         [ -  + ]:CBC         121 :                     else if (gss_ok != 'G')
                               3945                 :                :                     {
 1127 peter@eisentraut.org     3946                 :UBC           0 :                         libpq_append_conn_error(conn, "received invalid response to GSSAPI negotiation: %c",
                               3947                 :                :                                                 gss_ok);
 2449 sfrost@snowman.net       3948                 :              0 :                         goto error_return;
                               3949                 :                :                     }
                               3950                 :                : 
  489 alvherre@alvh.no-ip.     3951         [ -  + ]:CBC         121 :                     if (conn->Pfdebug)
  489 alvherre@alvh.no-ip.     3952                 :UBC           0 :                         pqTraceOutputCharResponse(conn, "GSSENCResponse",
                               3953                 :                :                                                   gss_ok);
                               3954                 :                :                 }
                               3955                 :                : 
                               3956                 :                :                 /* Begin or continue GSSAPI negotiation */
 2449 sfrost@snowman.net       3957                 :CBC         352 :                 pollres = pqsecure_open_gss(conn);
                               3958         [ +  + ]:            352 :                 if (pollres == PGRES_POLLING_OK)
                               3959                 :                :                 {
                               3960                 :                :                     /*
                               3961                 :                :                      * At this point we should have no data already buffered.
                               3962                 :                :                      * If we do, it was received before we performed the GSS
                               3963                 :                :                      * handshake, so it wasn't encrypted and indeed may have
                               3964                 :                :                      * been injected by a man-in-the-middle.
                               3965                 :                :                      */
 1499 tgl@sss.pgh.pa.us        3966         [ -  + ]:            121 :                     if (conn->inCursor != conn->inEnd)
                               3967                 :                :                     {
 1127 peter@eisentraut.org     3968                 :UBC           0 :                         libpq_append_conn_error(conn, "received unencrypted data after GSSAPI encryption response");
 1499 tgl@sss.pgh.pa.us        3969                 :              0 :                         goto error_return;
                               3970                 :                :                     }
                               3971                 :                : 
                               3972                 :                :                     /* All set for startup packet */
 2449 sfrost@snowman.net       3973                 :CBC         121 :                     conn->status = CONNECTION_MADE;
                               3974                 :            121 :                     return PGRES_POLLING_WRITING;
                               3975                 :                :                 }
 1009 michael@paquier.xyz      3976         [ -  + ]:            231 :                 else if (pollres == PGRES_POLLING_FAILED)
                               3977                 :                :                 {
                               3978                 :                :                     /*
                               3979                 :                :                      * GSS handshake failed.  We will retry with an SSL or
                               3980                 :                :                      * plaintext connection, if permitted by the options.
                               3981                 :                :                      */
  617 heikki.linnakangas@i     3982         [ #  # ]:UBC           0 :                     CONNECTION_FAILED();
                               3983                 :                :                 }
                               3984                 :                :                 /* Else, return POLLING_READING or POLLING_WRITING status */
 2449 sfrost@snowman.net       3985                 :CBC         231 :                 return pollres;
                               3986                 :                : #else                           /* !ENABLE_GSS */
                               3987                 :                :                 /* unreachable */
                               3988                 :                :                 goto error_return;
                               3989                 :                : #endif                          /* ENABLE_GSS */
                               3990                 :                :             }
                               3991                 :                : 
                               3992                 :                :             /*
                               3993                 :                :              * Handle authentication exchange: wait for postmaster messages
                               3994                 :                :              * and respond as necessary.
                               3995                 :                :              */
 9379 bruce@momjian.us         3996                 :          13879 :         case CONNECTION_AWAITING_RESPONSE:
                               3997                 :                :             {
                               3998                 :                :                 char        beresp;
                               3999                 :                :                 int         msgLength;
                               4000                 :                :                 int         avail;
                               4001                 :                :                 AuthRequest areq;
                               4002                 :                :                 int         res;
                               4003                 :                :                 bool        async;
                               4004                 :                : 
                               4005                 :                :                 /*
                               4006                 :                :                  * Scan the message from current point (note that if we find
                               4007                 :                :                  * the message is incomplete, we will return without advancing
                               4008                 :                :                  * inStart, and resume here next time).
                               4009                 :                :                  */
                               4010                 :          13879 :                 conn->inCursor = conn->inStart;
                               4011                 :                : 
                               4012                 :                :                 /* Read type byte */
                               4013         [ +  + ]:          13879 :                 if (pqGetc(&beresp, conn))
                               4014                 :                :                 {
                               4015                 :                :                     /* We'll come back when there is more data */
 9468 tgl@sss.pgh.pa.us        4016                 :            256 :                     return PGRES_POLLING_READING;
                               4017                 :                :                 }
                               4018                 :                : 
                               4019                 :                :                 /*
                               4020                 :                :                  * Validate message type: we expect only an authentication
                               4021                 :                :                  * request, NegotiateProtocolVersion, or an error here.
                               4022                 :                :                  * Anything else probably means it's not Postgres on the other
                               4023                 :                :                  * end at all.
                               4024                 :                :                  */
  847 nathan@postgresql.or     4025         [ +  + ]:          13623 :                 if (beresp != PqMsg_AuthenticationRequest &&
                               4026         [ -  + ]:            366 :                     beresp != PqMsg_ErrorResponse &&
  847 nathan@postgresql.or     4027         [ #  # ]:UBC           0 :                     beresp != PqMsg_NegotiateProtocolVersion)
                               4028                 :                :                 {
 1127 peter@eisentraut.org     4029                 :              0 :                     libpq_append_conn_error(conn, "expected authentication request from server, but received %c",
                               4030                 :                :                                             beresp);
 8274 tgl@sss.pgh.pa.us        4031                 :CBC         109 :                     goto error_return;
                               4032                 :                :                 }
                               4033                 :                : 
                               4034                 :                :                 /* Read message length word */
 1748 heikki.linnakangas@i     4035         [ -  + ]:          13623 :                 if (pqGetInt(&msgLength, 4, conn))
                               4036                 :                :                 {
                               4037                 :                :                     /* We'll come back when there is more data */
 1748 heikki.linnakangas@i     4038                 :UBC           0 :                     return PGRES_POLLING_READING;
                               4039                 :                :                 }
                               4040                 :                : 
                               4041                 :                :                 /*
                               4042                 :                :                  * Try to validate message length before using it.
                               4043                 :                :                  *
                               4044                 :                :                  * Authentication requests can't be very large, although GSS
                               4045                 :                :                  * auth requests may not be that small.  Same for
                               4046                 :                :                  * NegotiateProtocolVersion.
                               4047                 :                :                  *
                               4048                 :                :                  * Errors can be a little larger, but not huge.  If we see a
                               4049                 :                :                  * large apparent length in an error, it means we're really
                               4050                 :                :                  * talking to a pre-3.0-protocol server; cope.  (Before
                               4051                 :                :                  * version 14, the server also used the old protocol for
                               4052                 :                :                  * errors that happened before processing the startup packet.)
                               4053                 :                :                  */
  847 nathan@postgresql.or     4054         [ +  + ]:CBC       13623 :                 if (beresp == PqMsg_AuthenticationRequest &&
                               4055   [ +  -  -  + ]:          13257 :                     (msgLength < 8 || msgLength > 2000))
                               4056                 :                :                 {
 1028 heikki.linnakangas@i     4057                 :UBC           0 :                     libpq_append_conn_error(conn, "received invalid authentication request");
                               4058                 :              0 :                     goto error_return;
                               4059                 :                :                 }
  847 nathan@postgresql.or     4060         [ -  + ]:CBC       13623 :                 if (beresp == PqMsg_NegotiateProtocolVersion &&
  847 nathan@postgresql.or     4061   [ #  #  #  # ]:UBC           0 :                     (msgLength < 8 || msgLength > 2000))
                               4062                 :                :                 {
 1028 heikki.linnakangas@i     4063                 :              0 :                     libpq_append_conn_error(conn, "received invalid protocol negotiation message");
 8274 tgl@sss.pgh.pa.us        4064                 :              0 :                     goto error_return;
                               4065                 :                :                 }
                               4066                 :                : 
                               4067                 :                : #define MAX_ERRLEN 30000
  847 nathan@postgresql.or     4068         [ +  + ]:CBC       13623 :                 if (beresp == PqMsg_ErrorResponse &&
                               4069   [ +  -  -  + ]:            366 :                     (msgLength < 8 || msgLength > MAX_ERRLEN))
                               4070                 :                :                 {
                               4071                 :                :                     /* Handle error from a pre-3.0 server */
 8170 bruce@momjian.us         4072                 :UBC           0 :                     conn->inCursor = conn->inStart + 1; /* reread data */
 6259 magnus@hagander.net      4073         [ #  # ]:              0 :                     if (pqGets_append(&conn->errorMessage, conn))
                               4074                 :                :                     {
                               4075                 :                :                         /*
                               4076                 :                :                          * We may not have authenticated the server yet, so
                               4077                 :                :                          * don't let the buffer grow forever.
                               4078                 :                :                          */
 1028 heikki.linnakangas@i     4079                 :              0 :                         avail = conn->inEnd - conn->inCursor;
                               4080         [ #  # ]:              0 :                         if (avail > MAX_ERRLEN)
                               4081                 :                :                         {
                               4082                 :              0 :                             libpq_append_conn_error(conn, "received invalid error message");
                               4083                 :              0 :                             goto error_return;
                               4084                 :                :                         }
                               4085                 :                : 
                               4086                 :                :                         /* We'll come back when there is more data */
 9379 bruce@momjian.us         4087                 :              0 :                         return PGRES_POLLING_READING;
                               4088                 :                :                     }
                               4089                 :                :                     /* OK, we read the message; mark data consumed */
  487 alvherre@alvh.no-ip.     4090                 :              0 :                     pqParseDone(conn, conn->inCursor);
                               4091                 :                : 
                               4092                 :                :                     /*
                               4093                 :                :                      * Before 7.2, the postmaster didn't always end its
                               4094                 :                :                      * messages with a newline, so add one if needed to
                               4095                 :                :                      * conform to libpq conventions.
                               4096                 :                :                      */
 1748 heikki.linnakangas@i     4097         [ #  # ]:              0 :                     if (conn->errorMessage.len == 0 ||
                               4098         [ #  # ]:              0 :                         conn->errorMessage.data[conn->errorMessage.len - 1] != '\n')
                               4099                 :                :                     {
                               4100                 :              0 :                         appendPQExpBufferChar(&conn->errorMessage, '\n');
                               4101                 :                :                     }
                               4102                 :                : 
 9379 bruce@momjian.us         4103                 :              0 :                     goto error_return;
                               4104                 :                :                 }
                               4105                 :                : #undef MAX_ERRLEN
                               4106                 :                : 
                               4107                 :                :                 /*
                               4108                 :                :                  * Can't process if message body isn't all here yet.
                               4109                 :                :                  *
                               4110                 :                :                  * After this check passes, any further EOF during parsing
                               4111                 :                :                  * implies that the server sent a bad/truncated message.
                               4112                 :                :                  * Reading more bytes won't help in that case, so don't return
                               4113                 :                :                  * PGRES_POLLING_READING after this point.
                               4114                 :                :                  */
 8274 tgl@sss.pgh.pa.us        4115                 :CBC       13623 :                 msgLength -= 4;
                               4116                 :          13623 :                 avail = conn->inEnd - conn->inCursor;
                               4117         [ -  + ]:          13623 :                 if (avail < msgLength)
                               4118                 :                :                 {
                               4119                 :                :                     /*
                               4120                 :                :                      * Before returning, try to enlarge the input buffer if
                               4121                 :                :                      * needed to hold the whole message; see notes in
                               4122                 :                :                      * pqParseInput3.
                               4123                 :                :                      */
 6410 tgl@sss.pgh.pa.us        4124         [ #  # ]:UBC           0 :                     if (pqCheckInBufferSpace(conn->inCursor + (size_t) msgLength,
                               4125                 :                :                                              conn))
 8274                          4126                 :              0 :                         goto error_return;
                               4127                 :                :                     /* We'll come back when there is more data */
                               4128                 :              0 :                     return PGRES_POLLING_READING;
                               4129                 :                :                 }
                               4130                 :                : 
                               4131                 :                :                 /* Handle errors. */
  847 nathan@postgresql.or     4132         [ +  + ]:CBC       13623 :                 if (beresp == PqMsg_ErrorResponse)
                               4133                 :                :                 {
 1748 heikki.linnakangas@i     4134         [ -  + ]:            366 :                     if (pqGetErrorNotice3(conn, true))
                               4135                 :                :                     {
 1028 heikki.linnakangas@i     4136                 :UBC           0 :                         libpq_append_conn_error(conn, "received invalid error message");
                               4137                 :              0 :                         goto error_return;
                               4138                 :                :                     }
                               4139                 :                :                     /* OK, we read the message; mark data consumed */
  487 alvherre@alvh.no-ip.     4140                 :CBC         366 :                     pqParseDone(conn, conn->inCursor);
                               4141                 :                : 
                               4142                 :                :                     /*
                               4143                 :                :                      * If error is "cannot connect now", try the next host if
                               4144                 :                :                      * any (but we don't want to consider additional addresses
                               4145                 :                :                      * for this host, nor is there much point in changing SSL
                               4146                 :                :                      * or GSS mode).  This is helpful when dealing with
                               4147                 :                :                      * standby servers that might not be in hot-standby state.
                               4148                 :                :                      */
 1800 tgl@sss.pgh.pa.us        4149         [ +  + ]:            366 :                     if (strcmp(conn->last_sqlstate,
                               4150                 :                :                                ERRCODE_CANNOT_CONNECT_NOW) == 0)
                               4151                 :                :                     {
                               4152                 :            264 :                         conn->try_next_host = true;
                               4153                 :          13514 :                         goto keep_going;
                               4154                 :                :                     }
                               4155                 :                : 
                               4156                 :                :                     /* Check to see if we should mention pgpassfile */
 2689                          4157                 :            102 :                     pgpassfileWarning(conn);
                               4158                 :                : 
  617 heikki.linnakangas@i     4159         [ +  + ]:            102 :                     CONNECTION_FAILED();
                               4160                 :                :                 }
                               4161                 :                :                 /* Handle NegotiateProtocolVersion */
  847 nathan@postgresql.or     4162         [ -  + ]:          13257 :                 else if (beresp == PqMsg_NegotiateProtocolVersion)
                               4163                 :                :                 {
  258 heikki.linnakangas@i     4164         [ #  # ]:UBC           0 :                     if (conn->pversion_negotiated)
                               4165                 :                :                     {
                               4166                 :              0 :                         libpq_append_conn_error(conn, "received duplicate protocol negotiation message");
                               4167                 :              0 :                         goto error_return;
                               4168                 :                :                     }
 1125 peter@eisentraut.org     4169         [ #  # ]:              0 :                     if (pqGetNegotiateProtocolVersion3(conn))
                               4170                 :                :                     {
                               4171                 :                :                         /* pqGetNegotiateProtocolVersion3 set error already */
                               4172                 :              0 :                         goto error_return;
                               4173                 :                :                     }
  258 heikki.linnakangas@i     4174                 :              0 :                     conn->pversion_negotiated = true;
                               4175                 :                : 
                               4176                 :                :                     /* OK, we read the message; mark data consumed */
  487 alvherre@alvh.no-ip.     4177                 :              0 :                     pqParseDone(conn, conn->inCursor);
                               4178                 :                : 
  258 heikki.linnakangas@i     4179                 :              0 :                     goto keep_going;
                               4180                 :                :                 }
                               4181                 :                : 
                               4182                 :                :                 /* It is an authentication request. */
 5498 tgl@sss.pgh.pa.us        4183                 :CBC       13257 :                 conn->auth_req_received = true;
                               4184                 :                : 
                               4185                 :                :                 /* Get the type of request. */
 9379 bruce@momjian.us         4186         [ -  + ]:          13257 :                 if (pqGetInt((int *) &areq, 4, conn))
                               4187                 :                :                 {
                               4188                 :                :                     /* can't happen because we checked the length already */
 1028 heikki.linnakangas@i     4189                 :UBC           0 :                     libpq_append_conn_error(conn, "received invalid authentication request");
 1125 peter@eisentraut.org     4190                 :              0 :                     goto error_return;
                               4191                 :                :                 }
 3169 heikki.linnakangas@i     4192                 :CBC       13257 :                 msgLength -= 4;
                               4193                 :                : 
                               4194                 :                :                 /*
                               4195                 :                :                  * Process the rest of the authentication request message, and
                               4196                 :                :                  * respond to it if necessary.
                               4197                 :                :                  *
                               4198                 :                :                  * Note that conn->pghost must be non-NULL if we are going to
                               4199                 :                :                  * avoid the Kerberos code doing a hostname look-up.
                               4200                 :                :                  */
  313 dgustafsson@postgres     4201                 :          13257 :                 res = pg_fe_sendauth(areq, msgLength, conn, &async);
                               4202                 :                : 
                               4203   [ +  +  +  - ]:          13257 :                 if (async && (res == STATUS_OK))
                               4204                 :                :                 {
                               4205                 :                :                     /*
                               4206                 :                :                      * We'll come back later once we're ready to respond.
                               4207                 :                :                      * Don't consume the request yet.
                               4208                 :                :                      */
                               4209                 :             53 :                     conn->status = CONNECTION_AUTHENTICATING;
                               4210                 :             53 :                     goto keep_going;
                               4211                 :                :                 }
                               4212                 :                : 
                               4213                 :                :                 /*
                               4214                 :                :                  * OK, we have processed the message; mark data consumed.  We
                               4215                 :                :                  * don't call pqParseDone here because we already traced this
                               4216                 :                :                  * message inside pg_fe_sendauth.
                               4217                 :                :                  */
 3169 heikki.linnakangas@i     4218                 :          13204 :                 conn->inStart = conn->inCursor;
                               4219                 :                : 
                               4220         [ +  + ]:          13204 :                 if (res != STATUS_OK)
                               4221                 :                :                 {
                               4222                 :                :                     /*
                               4223                 :                :                      * OAuth connections may perform two-step discovery, where
                               4224                 :                :                      * the first connection is a dummy.
                               4225                 :                :                      */
  299 dgustafsson@postgres     4226   [ +  +  +  + ]:             74 :                     if (conn->sasl == &pg_oauth_mech && conn->oauth_want_retry)
                               4227                 :                :                     {
                               4228                 :             35 :                         need_new_connection = true;
                               4229                 :             35 :                         goto keep_going;
                               4230                 :                :                     }
                               4231                 :                : 
 9379 bruce@momjian.us         4232                 :             39 :                     goto error_return;
                               4233                 :                :                 }
                               4234                 :                : 
                               4235                 :                :                 /*
                               4236                 :                :                  * Just make sure that any data sent by pg_fe_sendauth is
                               4237                 :                :                  * flushed out.  Although this theoretically could block, it
                               4238                 :                :                  * really shouldn't since we don't send large auth responses.
                               4239                 :                :                  */
                               4240         [ -  + ]:          13130 :                 if (pqFlush(conn))
 9379 bruce@momjian.us         4241                 :UBC           0 :                     goto error_return;
                               4242                 :                : 
 9379 bruce@momjian.us         4243         [ +  + ]:CBC       13130 :                 if (areq == AUTH_REQ_OK)
                               4244                 :                :                 {
                               4245                 :                :                     /* We are done with authentication exchange */
                               4246                 :          12815 :                     conn->status = CONNECTION_AUTH_OK;
                               4247                 :                : 
                               4248                 :                :                     /*
                               4249                 :                :                      * Set asyncStatus so that PQgetResult will think that
                               4250                 :                :                      * what comes back next is the result of a query.  See
                               4251                 :                :                      * below.
                               4252                 :                :                      */
                               4253                 :          12815 :                     conn->asyncStatus = PGASYNC_BUSY;
                               4254                 :                :                 }
                               4255                 :                : 
                               4256                 :                :                 /* Look to see if we have more data yet. */
                               4257                 :          13130 :                 goto keep_going;
                               4258                 :                :             }
                               4259                 :                : 
  313 dgustafsson@postgres     4260                 :         392615 :         case CONNECTION_AUTHENTICATING:
                               4261                 :                :             {
                               4262                 :                :                 PostgresPollingStatusType status;
                               4263                 :                : 
                               4264   [ +  -  -  + ]:         392615 :                 if (!conn->async_auth || !conn->cleanup_async_auth)
                               4265                 :                :                 {
                               4266                 :                :                     /* programmer error; should not happen */
  313 dgustafsson@postgres     4267                 :UBC           0 :                     libpq_append_conn_error(conn,
                               4268                 :                :                                             "internal error: async authentication has no handler");
                               4269                 :              0 :                     goto error_return;
                               4270                 :                :                 }
                               4271                 :                : 
                               4272                 :                :                 /* Drive some external authentication work. */
  313 dgustafsson@postgres     4273                 :CBC      392615 :                 status = conn->async_auth(conn);
                               4274                 :                : 
                               4275         [ +  + ]:         392615 :                 if (status == PGRES_POLLING_FAILED)
                               4276                 :             18 :                     goto error_return;
                               4277                 :                : 
                               4278         [ +  + ]:         392597 :                 if (status == PGRES_POLLING_OK)
                               4279                 :                :                 {
                               4280                 :                :                     /* Done. Tear down the async implementation. */
                               4281                 :             34 :                     conn->cleanup_async_auth(conn);
                               4282                 :             34 :                     conn->cleanup_async_auth = NULL;
                               4283                 :                : 
                               4284                 :                :                     /*
                               4285                 :                :                      * Cleanup must unset altsock, both as an indication that
                               4286                 :                :                      * it's been released, and to stop pqSocketCheck from
                               4287                 :                :                      * looking at the wrong socket after async auth is done.
                               4288                 :                :                      */
                               4289         [ -  + ]:             34 :                     if (conn->altsock != PGINVALID_SOCKET)
                               4290                 :                :                     {
  313 dgustafsson@postgres     4291                 :UBC           0 :                         Assert(false);
                               4292                 :                :                         libpq_append_conn_error(conn,
                               4293                 :                :                                                 "internal error: async cleanup did not release polling socket");
                               4294                 :                :                         goto error_return;
                               4295                 :                :                     }
                               4296                 :                : 
                               4297                 :                :                     /*
                               4298                 :                :                      * Reenter the authentication exchange with the server. We
                               4299                 :                :                      * didn't consume the message that started external
                               4300                 :                :                      * authentication, so it'll be reprocessed as if we just
                               4301                 :                :                      * received it.
                               4302                 :                :                      */
  313 dgustafsson@postgres     4303                 :CBC          34 :                     conn->status = CONNECTION_AWAITING_RESPONSE;
                               4304                 :                : 
                               4305                 :             34 :                     goto keep_going;
                               4306                 :                :                 }
                               4307                 :                : 
                               4308                 :                :                 /*
                               4309                 :                :                  * Caller needs to poll some more. conn->async_auth() should
                               4310                 :                :                  * have assigned an altsock to poll on.
                               4311                 :                :                  */
                               4312         [ -  + ]:         392563 :                 if (conn->altsock == PGINVALID_SOCKET)
                               4313                 :                :                 {
  313 dgustafsson@postgres     4314                 :UBC           0 :                     Assert(false);
                               4315                 :                :                     libpq_append_conn_error(conn,
                               4316                 :                :                                             "internal error: async authentication did not set a socket for polling");
                               4317                 :                :                     goto error_return;
                               4318                 :                :                 }
                               4319                 :                : 
  313 dgustafsson@postgres     4320                 :CBC      392563 :                 return status;
                               4321                 :                :             }
                               4322                 :                : 
 9513 bruce@momjian.us         4323                 :          12820 :         case CONNECTION_AUTH_OK:
                               4324                 :                :             {
                               4325                 :                :                 /*
                               4326                 :                :                  * Now we expect to hear from the backend. A ReadyForQuery
                               4327                 :                :                  * message indicates that startup is successful, but we might
                               4328                 :                :                  * also get an Error message indicating failure. (Notice
                               4329                 :                :                  * messages indicating nonfatal warnings are also allowed by
                               4330                 :                :                  * the protocol, as are ParameterStatus and BackendKeyData
                               4331                 :                :                  * messages.) Easiest way to handle this is to let
                               4332                 :                :                  * PQgetResult() read the messages. We just have to fake it
                               4333                 :                :                  * out about the state of the connection, by setting
                               4334                 :                :                  * asyncStatus = PGASYNC_BUSY (done above).
                               4335                 :                :                  */
                               4336                 :                : 
 9379                          4337         [ +  + ]:          12820 :                 if (PQisBusy(conn))
                               4338                 :              5 :                     return PGRES_POLLING_READING;
                               4339                 :                : 
                               4340                 :          12815 :                 res = PQgetResult(conn);
                               4341                 :                : 
                               4342                 :                :                 /*
                               4343                 :                :                  * NULL return indicating we have gone to IDLE state is
                               4344                 :                :                  * expected
                               4345                 :                :                  */
                               4346         [ +  + ]:          12815 :                 if (res)
                               4347                 :                :                 {
                               4348         [ -  + ]:             17 :                     if (res->resultStatus != PGRES_FATAL_ERROR)
 1127 peter@eisentraut.org     4349                 :UBC           0 :                         libpq_append_conn_error(conn, "unexpected message from server during startup");
 5858 tgl@sss.pgh.pa.us        4350         [ +  - ]:CBC          17 :                     else if (conn->send_appname &&
                               4351   [ -  +  -  - ]:             17 :                              (conn->appname || conn->fbappname))
                               4352                 :                :                     {
                               4353                 :                :                         /*
                               4354                 :                :                          * If we tried to send application_name, check to see
                               4355                 :                :                          * if the error is about that --- pre-9.0 servers will
                               4356                 :                :                          * reject it at this stage of the process.  If so,
                               4357                 :                :                          * close the connection and retry without sending
                               4358                 :                :                          * application_name.  We could possibly get a false
                               4359                 :                :                          * SQLSTATE match here and retry uselessly, but there
                               4360                 :                :                          * seems no great harm in that; we'll just get the
                               4361                 :                :                          * same error again if it's unrelated.
                               4362                 :                :                          */
                               4363                 :                :                         const char *sqlstate;
                               4364                 :                : 
                               4365                 :             17 :                         sqlstate = PQresultErrorField(res, PG_DIAG_SQLSTATE);
                               4366         [ +  - ]:             17 :                         if (sqlstate &&
                               4367         [ -  + ]:             17 :                             strcmp(sqlstate, ERRCODE_APPNAME_UNKNOWN) == 0)
                               4368                 :                :                         {
 5858 tgl@sss.pgh.pa.us        4369                 :UBC           0 :                             PQclear(res);
                               4370                 :              0 :                             conn->send_appname = false;
 2689                          4371                 :              0 :                             need_new_connection = true;
 5858                          4372                 :              0 :                             goto keep_going;
                               4373                 :                :                         }
                               4374                 :                :                     }
                               4375                 :                : 
                               4376                 :                :                     /*
                               4377                 :                :                      * if the resultStatus is FATAL, then conn->errorMessage
                               4378                 :                :                      * already has a copy of the error; needn't copy it back.
                               4379                 :                :                      * But add a newline if it's not there already, since
                               4380                 :                :                      * postmaster error messages may not have one.
                               4381                 :                :                      */
 9379 bruce@momjian.us         4382         [ +  - ]:CBC          17 :                     if (conn->errorMessage.len <= 0 ||
                               4383         [ -  + ]:             17 :                         conn->errorMessage.data[conn->errorMessage.len - 1] != '\n')
 9379 bruce@momjian.us         4384                 :UBC           0 :                         appendPQExpBufferChar(&conn->errorMessage, '\n');
 9379 bruce@momjian.us         4385                 :CBC          17 :                     PQclear(res);
                               4386                 :             17 :                     goto error_return;
                               4387                 :                :                 }
                               4388                 :                : 
                               4389                 :                :                 /* Almost there now ... */
 2289 alvherre@alvh.no-ip.     4390                 :          12798 :                 conn->status = CONNECTION_CHECK_TARGET;
                               4391                 :          12798 :                 goto keep_going;
                               4392                 :                :             }
                               4393                 :                : 
                               4394                 :          12798 :         case CONNECTION_CHECK_TARGET:
                               4395                 :                :             {
                               4396                 :                :                 /*
                               4397                 :                :                  * If a read-write, read-only, primary, or standby connection
                               4398                 :                :                  * is required, see if we have one.
                               4399                 :                :                  */
 1750 tgl@sss.pgh.pa.us        4400         [ +  + ]:          12798 :                 if (conn->target_server_type == SERVER_TYPE_READ_WRITE ||
                               4401         [ +  + ]:          12793 :                     conn->target_server_type == SERVER_TYPE_READ_ONLY)
 3304 rhaas@postgresql.org     4402                 :              4 :                 {
                               4403                 :                :                     bool        read_only_server;
                               4404                 :                : 
                               4405                 :                :                     /*
                               4406                 :                :                      * If the server didn't report
                               4407                 :                :                      * "default_transaction_read_only" or "in_hot_standby" at
                               4408                 :                :                      * startup, we must determine its state by sending the
                               4409                 :                :                      * query "SHOW transaction_read_only".  This GUC exists in
                               4410                 :                :                      * all server versions that support 3.0 protocol.
                               4411                 :                :                      */
 1750 tgl@sss.pgh.pa.us        4412         [ +  - ]:             10 :                     if (conn->default_transaction_read_only == PG_BOOL_UNKNOWN ||
                               4413         [ -  + ]:             10 :                         conn->in_hot_standby == PG_BOOL_UNKNOWN)
                               4414                 :                :                     {
                               4415                 :                :                         /*
                               4416                 :                :                          * We use PQsendQueryContinue so that
                               4417                 :                :                          * conn->errorMessage does not get cleared.  We need
                               4418                 :                :                          * to preserve any error messages related to previous
                               4419                 :                :                          * hosts we have tried and failed to connect to.
                               4420                 :                :                          */
 1750 tgl@sss.pgh.pa.us        4421                 :UBC           0 :                         conn->status = CONNECTION_OK;
                               4422         [ #  # ]:              0 :                         if (!PQsendQueryContinue(conn,
                               4423                 :                :                                                  "SHOW transaction_read_only"))
                               4424                 :              0 :                             goto error_return;
                               4425                 :                :                         /* We'll return to this state when we have the answer */
                               4426                 :              0 :                         conn->status = CONNECTION_CHECK_WRITABLE;
                               4427                 :              0 :                         return PGRES_POLLING_READING;
                               4428                 :                :                     }
                               4429                 :                : 
                               4430                 :                :                     /* OK, we can make the test */
 1750 tgl@sss.pgh.pa.us        4431                 :CBC          10 :                     read_only_server =
                               4432         [ +  - ]:             20 :                         (conn->default_transaction_read_only == PG_BOOL_YES ||
                               4433         [ +  + ]:             10 :                          conn->in_hot_standby == PG_BOOL_YES);
                               4434                 :                : 
                               4435   [ +  +  +  + ]:             10 :                     if ((conn->target_server_type == SERVER_TYPE_READ_WRITE) ?
                               4436                 :                :                         read_only_server : !read_only_server)
                               4437                 :                :                     {
                               4438                 :                :                         /* Wrong server state, reject and try the next host */
                               4439         [ +  + ]:              6 :                         if (conn->target_server_type == SERVER_TYPE_READ_WRITE)
 1127 peter@eisentraut.org     4440                 :              3 :                             libpq_append_conn_error(conn, "session is read-only");
                               4441                 :                :                         else
                               4442                 :              3 :                             libpq_append_conn_error(conn, "session is not read-only");
                               4443                 :                : 
                               4444                 :                :                         /* Close connection politely. */
 1750 tgl@sss.pgh.pa.us        4445                 :              6 :                         conn->status = CONNECTION_OK;
                               4446                 :              6 :                         sendTerminateConn(conn);
                               4447                 :                : 
                               4448                 :                :                         /*
                               4449                 :                :                          * Try next host if any, but we don't want to consider
                               4450                 :                :                          * additional addresses for this host.
                               4451                 :                :                          */
                               4452                 :              6 :                         conn->try_next_host = true;
                               4453                 :              6 :                         goto keep_going;
                               4454                 :                :                     }
                               4455                 :                :                 }
                               4456         [ +  + ]:          12788 :                 else if (conn->target_server_type == SERVER_TYPE_PRIMARY ||
                               4457         [ +  + ]:          12783 :                          conn->target_server_type == SERVER_TYPE_STANDBY ||
                               4458         [ +  + ]:          12778 :                          conn->target_server_type == SERVER_TYPE_PREFER_STANDBY)
                               4459                 :                :                 {
                               4460                 :                :                     /*
                               4461                 :                :                      * If the server didn't report "in_hot_standby" at
                               4462                 :                :                      * startup, we must determine its state by sending the
                               4463                 :                :                      * query "SELECT pg_catalog.pg_is_in_recovery()".  Servers
                               4464                 :                :                      * before 9.0 don't have that function, but by the same
                               4465                 :                :                      * token they don't have any standby mode, so we may just
                               4466                 :                :                      * assume the result.
                               4467                 :                :                      */
                               4468         [ -  + ]:             15 :                     if (conn->sversion < 90000)
 1750 tgl@sss.pgh.pa.us        4469                 :UBC           0 :                         conn->in_hot_standby = PG_BOOL_NO;
                               4470                 :                : 
 1750 tgl@sss.pgh.pa.us        4471         [ -  + ]:CBC          15 :                     if (conn->in_hot_standby == PG_BOOL_UNKNOWN)
                               4472                 :                :                     {
                               4473                 :                :                         /*
                               4474                 :                :                          * We use PQsendQueryContinue so that
                               4475                 :                :                          * conn->errorMessage does not get cleared.  We need
                               4476                 :                :                          * to preserve any error messages related to previous
                               4477                 :                :                          * hosts we have tried and failed to connect to.
                               4478                 :                :                          */
 1750 tgl@sss.pgh.pa.us        4479                 :UBC           0 :                         conn->status = CONNECTION_OK;
                               4480         [ #  # ]:              0 :                         if (!PQsendQueryContinue(conn,
                               4481                 :                :                                                  "SELECT pg_catalog.pg_is_in_recovery()"))
                               4482                 :              0 :                             goto error_return;
                               4483                 :                :                         /* We'll return to this state when we have the answer */
                               4484                 :              0 :                         conn->status = CONNECTION_CHECK_STANDBY;
                               4485                 :              0 :                         return PGRES_POLLING_READING;
                               4486                 :                :                     }
                               4487                 :                : 
                               4488                 :                :                     /* OK, we can make the test */
 1750 tgl@sss.pgh.pa.us        4489   [ +  +  +  + ]:CBC          30 :                     if ((conn->target_server_type == SERVER_TYPE_PRIMARY) ?
                               4490                 :              5 :                         (conn->in_hot_standby == PG_BOOL_YES) :
                               4491                 :             10 :                         (conn->in_hot_standby == PG_BOOL_NO))
                               4492                 :                :                     {
                               4493                 :                :                         /* Wrong server state, reject and try the next host */
                               4494         [ +  + ]:              9 :                         if (conn->target_server_type == SERVER_TYPE_PRIMARY)
 1127 peter@eisentraut.org     4495                 :              3 :                             libpq_append_conn_error(conn, "server is in hot standby mode");
                               4496                 :                :                         else
                               4497                 :              6 :                             libpq_append_conn_error(conn, "server is not in hot standby mode");
                               4498                 :                : 
                               4499                 :                :                         /* Close connection politely. */
 1750 tgl@sss.pgh.pa.us        4500                 :              9 :                         conn->status = CONNECTION_OK;
                               4501                 :              9 :                         sendTerminateConn(conn);
                               4502                 :                : 
                               4503                 :                :                         /*
                               4504                 :                :                          * Try next host if any, but we don't want to consider
                               4505                 :                :                          * additional addresses for this host.
                               4506                 :                :                          */
                               4507                 :              9 :                         conn->try_next_host = true;
                               4508                 :              9 :                         goto keep_going;
                               4509                 :                :                     }
                               4510                 :                :                 }
                               4511                 :                : 
                               4512                 :                :                 /* Don't hold onto any OAuth tokens longer than necessary. */
  299 dgustafsson@postgres     4513                 :          12783 :                 pqClearOAuthToken(conn);
                               4514                 :                : 
                               4515                 :                :                 /*
                               4516                 :                :                  * For non cancel requests we can release the address list
                               4517                 :                :                  * now. For cancel requests we never actually resolve
                               4518                 :                :                  * addresses and instead the addrinfo exists for the lifetime
                               4519                 :                :                  * of the connection.
                               4520                 :                :                  */
  644 alvherre@alvh.no-ip.     4521         [ +  - ]:          12783 :                 if (!conn->cancelRequest)
                               4522                 :          12783 :                     release_conn_addrinfo(conn);
                               4523                 :                : 
                               4524                 :                :                 /*
                               4525                 :                :                  * Contents of conn->errorMessage are no longer interesting
                               4526                 :                :                  * (and it seems some clients expect it to be empty after a
                               4527                 :                :                  * successful connection).
                               4528                 :                :                  */
 1397 tgl@sss.pgh.pa.us        4529                 :          12783 :                 pqClearConnErrorState(conn);
                               4530                 :                : 
                               4531                 :                :                 /* We are open for business! */
 8271                          4532                 :          12783 :                 conn->status = CONNECTION_OK;
                               4533                 :          12783 :                 return PGRES_POLLING_OK;
                               4534                 :                :             }
                               4535                 :                : 
 3226 rhaas@postgresql.org     4536                 :UBC           0 :         case CONNECTION_CONSUME:
                               4537                 :                :             {
                               4538                 :                :                 /*
                               4539                 :                :                  * This state just makes sure the connection is idle after
                               4540                 :                :                  * we've obtained the result of a SHOW or SELECT query.  Once
                               4541                 :                :                  * we're clear, return to CONNECTION_CHECK_TARGET state to
                               4542                 :                :                  * decide what to do next.  We must transiently set status =
                               4543                 :                :                  * CONNECTION_OK in order to use the result-consuming
                               4544                 :                :                  * subroutines.
                               4545                 :                :                  */
                               4546                 :              0 :                 conn->status = CONNECTION_OK;
                               4547         [ #  # ]:              0 :                 if (!PQconsumeInput(conn))
                               4548                 :              0 :                     goto error_return;
                               4549                 :                : 
                               4550         [ #  # ]:              0 :                 if (PQisBusy(conn))
                               4551                 :                :                 {
                               4552                 :              0 :                     conn->status = CONNECTION_CONSUME;
                               4553                 :              0 :                     return PGRES_POLLING_READING;
                               4554                 :                :                 }
                               4555                 :                : 
                               4556                 :                :                 /* Call PQgetResult() again until we get a NULL result */
                               4557                 :              0 :                 res = PQgetResult(conn);
                               4558         [ #  # ]:              0 :                 if (res != NULL)
                               4559                 :                :                 {
                               4560                 :              0 :                     PQclear(res);
                               4561                 :              0 :                     conn->status = CONNECTION_CONSUME;
 1750 tgl@sss.pgh.pa.us        4562                 :              0 :                     return PGRES_POLLING_READING;
                               4563                 :                :                 }
                               4564                 :                : 
                               4565                 :              0 :                 conn->status = CONNECTION_CHECK_TARGET;
                               4566                 :              0 :                 goto keep_going;
                               4567                 :                :             }
                               4568                 :                : 
 3304 rhaas@postgresql.org     4569                 :              0 :         case CONNECTION_CHECK_WRITABLE:
                               4570                 :                :             {
                               4571                 :                :                 /*
                               4572                 :                :                  * Waiting for result of "SHOW transaction_read_only".  We
                               4573                 :                :                  * must transiently set status = CONNECTION_OK in order to use
                               4574                 :                :                  * the result-consuming subroutines.
                               4575                 :                :                  */
                               4576                 :              0 :                 conn->status = CONNECTION_OK;
                               4577         [ #  # ]:              0 :                 if (!PQconsumeInput(conn))
                               4578                 :              0 :                     goto error_return;
                               4579                 :                : 
                               4580         [ #  # ]:              0 :                 if (PQisBusy(conn))
                               4581                 :                :                 {
                               4582                 :              0 :                     conn->status = CONNECTION_CHECK_WRITABLE;
                               4583                 :              0 :                     return PGRES_POLLING_READING;
                               4584                 :                :                 }
                               4585                 :                : 
                               4586                 :              0 :                 res = PQgetResult(conn);
 1750 tgl@sss.pgh.pa.us        4587   [ #  #  #  #  :              0 :                 if (res && PQresultStatus(res) == PGRES_TUPLES_OK &&
                                              #  # ]
 3304 rhaas@postgresql.org     4588                 :              0 :                     PQntuples(res) == 1)
                               4589                 :                :                 {
 1750 tgl@sss.pgh.pa.us        4590                 :              0 :                     char       *val = PQgetvalue(res, 0, 0);
                               4591                 :                : 
                               4592                 :                :                     /*
                               4593                 :                :                      * "transaction_read_only = on" proves that at least one
                               4594                 :                :                      * of default_transaction_read_only and in_hot_standby is
                               4595                 :                :                      * on, but we don't actually know which.  We don't care
                               4596                 :                :                      * though for the purpose of identifying a read-only
                               4597                 :                :                      * session, so satisfy the CONNECTION_CHECK_TARGET code by
                               4598                 :                :                      * claiming they are both on.  On the other hand, if it's
                               4599                 :                :                      * a read-write session, they are certainly both off.
                               4600                 :                :                      */
 3304 rhaas@postgresql.org     4601         [ #  # ]:              0 :                     if (strncmp(val, "on", 2) == 0)
                               4602                 :                :                     {
 1750 tgl@sss.pgh.pa.us        4603                 :              0 :                         conn->default_transaction_read_only = PG_BOOL_YES;
                               4604                 :              0 :                         conn->in_hot_standby = PG_BOOL_YES;
                               4605                 :                :                     }
                               4606                 :                :                     else
                               4607                 :                :                     {
                               4608                 :              0 :                         conn->default_transaction_read_only = PG_BOOL_NO;
                               4609                 :              0 :                         conn->in_hot_standby = PG_BOOL_NO;
                               4610                 :                :                     }
                               4611                 :              0 :                     PQclear(res);
                               4612                 :                : 
                               4613                 :                :                     /* Finish reading messages before continuing */
                               4614                 :              0 :                     conn->status = CONNECTION_CONSUME;
                               4615                 :              0 :                     goto keep_going;
                               4616                 :                :                 }
                               4617                 :                : 
                               4618                 :                :                 /* Something went wrong with "SHOW transaction_read_only". */
 1262 peter@eisentraut.org     4619                 :              0 :                 PQclear(res);
                               4620                 :                : 
                               4621                 :                :                 /* Append error report to conn->errorMessage. */
 1127                          4622                 :              0 :                 libpq_append_conn_error(conn, "\"%s\" failed",
                               4623                 :                :                                         "SHOW transaction_read_only");
                               4624                 :                : 
                               4625                 :                :                 /* Close connection politely. */
 1750 tgl@sss.pgh.pa.us        4626                 :              0 :                 conn->status = CONNECTION_OK;
                               4627                 :              0 :                 sendTerminateConn(conn);
                               4628                 :                : 
                               4629                 :                :                 /* Try next host. */
                               4630                 :              0 :                 conn->try_next_host = true;
                               4631                 :              0 :                 goto keep_going;
                               4632                 :                :             }
                               4633                 :                : 
                               4634                 :              0 :         case CONNECTION_CHECK_STANDBY:
                               4635                 :                :             {
                               4636                 :                :                 /*
                               4637                 :                :                  * Waiting for result of "SELECT pg_is_in_recovery()".  We
                               4638                 :                :                  * must transiently set status = CONNECTION_OK in order to use
                               4639                 :                :                  * the result-consuming subroutines.
                               4640                 :                :                  */
                               4641                 :              0 :                 conn->status = CONNECTION_OK;
                               4642         [ #  # ]:              0 :                 if (!PQconsumeInput(conn))
                               4643                 :              0 :                     goto error_return;
                               4644                 :                : 
                               4645         [ #  # ]:              0 :                 if (PQisBusy(conn))
                               4646                 :                :                 {
                               4647                 :              0 :                     conn->status = CONNECTION_CHECK_STANDBY;
                               4648                 :              0 :                     return PGRES_POLLING_READING;
                               4649                 :                :                 }
                               4650                 :                : 
                               4651                 :              0 :                 res = PQgetResult(conn);
                               4652   [ #  #  #  #  :              0 :                 if (res && PQresultStatus(res) == PGRES_TUPLES_OK &&
                                              #  # ]
                               4653                 :              0 :                     PQntuples(res) == 1)
                               4654                 :                :                 {
                               4655                 :              0 :                     char       *val = PQgetvalue(res, 0, 0);
                               4656                 :                : 
                               4657         [ #  # ]:              0 :                     if (strncmp(val, "t", 1) == 0)
                               4658                 :              0 :                         conn->in_hot_standby = PG_BOOL_YES;
                               4659                 :                :                     else
                               4660                 :              0 :                         conn->in_hot_standby = PG_BOOL_NO;
 3304 rhaas@postgresql.org     4661                 :              0 :                     PQclear(res);
                               4662                 :                : 
                               4663                 :                :                     /* Finish reading messages before continuing */
 3226                          4664                 :              0 :                     conn->status = CONNECTION_CONSUME;
                               4665                 :              0 :                     goto keep_going;
                               4666                 :                :                 }
                               4667                 :                : 
                               4668                 :                :                 /* Something went wrong with "SELECT pg_is_in_recovery()". */
 1262 peter@eisentraut.org     4669                 :              0 :                 PQclear(res);
                               4670                 :                : 
                               4671                 :                :                 /* Append error report to conn->errorMessage. */
 1127                          4672                 :              0 :                 libpq_append_conn_error(conn, "\"%s\" failed",
                               4673                 :                :                                         "SELECT pg_is_in_recovery()");
                               4674                 :                : 
                               4675                 :                :                 /* Close connection politely. */
 3304 rhaas@postgresql.org     4676                 :              0 :                 conn->status = CONNECTION_OK;
                               4677                 :              0 :                 sendTerminateConn(conn);
                               4678                 :                : 
                               4679                 :                :                 /* Try next host. */
 1750 tgl@sss.pgh.pa.us        4680                 :              0 :                 conn->try_next_host = true;
 2689                          4681                 :              0 :                 goto keep_going;
                               4682                 :                :             }
                               4683                 :                : 
 9513 bruce@momjian.us         4684                 :              0 :         default:
 1127 peter@eisentraut.org     4685                 :              0 :             libpq_append_conn_error(conn,
                               4686                 :                :                                     "invalid connection state %d, probably indicative of memory corruption",
 1013 michael@paquier.xyz      4687                 :              0 :                                     conn->status);
 9513 bruce@momjian.us         4688                 :              0 :             goto error_return;
                               4689                 :                :     }
                               4690                 :                : 
                               4691                 :                :     /* Unreachable */
                               4692                 :                : 
 9513 bruce@momjian.us         4693                 :CBC         684 : error_return:
                               4694                 :                : 
                               4695                 :                :     /*
                               4696                 :                :      * We used to close the socket at this point, but that makes it awkward
                               4697                 :                :      * for those above us if they wish to remove this socket from their own
                               4698                 :                :      * records (an fd_set for example).  We'll just have this socket closed
                               4699                 :                :      * when PQfinish is called (which is compulsory even after an error, since
                               4700                 :                :      * the connection structure must be freed).
                               4701                 :                :      */
 8227 tgl@sss.pgh.pa.us        4702                 :            684 :     conn->status = CONNECTION_BAD;
 9513 bruce@momjian.us         4703                 :            684 :     return PGRES_POLLING_FAILED;
                               4704                 :                : }
                               4705                 :                : 
                               4706                 :                : /*
                               4707                 :                :  * Initialize the state machine for negotiating encryption
                               4708                 :                :  */
                               4709                 :                : static bool
  617 heikki.linnakangas@i     4710                 :          13552 : init_allowed_encryption_methods(PGconn *conn)
                               4711                 :                : {
                               4712         [ +  + ]:          13552 :     if (conn->raddr.addr.ss_family == AF_UNIX)
                               4713                 :                :     {
                               4714                 :                :         /* Don't request SSL or GSSAPI over Unix sockets */
  579                          4715                 :          13207 :         conn->allowed_enc_methods &= ~(ENC_SSL | ENC_GSSAPI);
                               4716                 :                : 
                               4717                 :                :         /*
                               4718                 :                :          * XXX: we probably should not do this. sslmode=require works
                               4719                 :                :          * differently
                               4720                 :                :          */
  617                          4721         [ -  + ]:          13207 :         if (conn->gssencmode[0] == 'r')
                               4722                 :                :         {
  617 heikki.linnakangas@i     4723                 :UBC           0 :             libpq_append_conn_error(conn,
                               4724                 :                :                                     "GSSAPI encryption required but it is not supported over a local socket");
                               4725                 :              0 :             conn->allowed_enc_methods = 0;
                               4726                 :              0 :             conn->current_enc_method = ENC_ERROR;
                               4727                 :              0 :             return false;
                               4728                 :                :         }
                               4729                 :                : 
  617 heikki.linnakangas@i     4730                 :CBC       13207 :         conn->allowed_enc_methods = ENC_PLAINTEXT;
                               4731                 :          13207 :         conn->current_enc_method = ENC_PLAINTEXT;
                               4732                 :          13207 :         return true;
                               4733                 :                :     }
                               4734                 :                : 
                               4735                 :                :     /* initialize based on sslmode and gssencmode */
                               4736                 :            345 :     conn->allowed_enc_methods = 0;
                               4737                 :                : 
                               4738                 :                : #ifdef USE_SSL
                               4739                 :                :     /* sslmode anything but 'disable', and GSSAPI not required */
                               4740   [ +  +  +  + ]:            345 :     if (conn->sslmode[0] != 'd' && conn->gssencmode[0] != 'r')
                               4741                 :                :     {
  579                          4742                 :            259 :         conn->allowed_enc_methods |= ENC_SSL;
                               4743                 :                :     }
                               4744                 :                : #endif
                               4745                 :                : 
                               4746                 :                : #ifdef ENABLE_GSS
  617                          4747         [ +  + ]:            345 :     if (conn->gssencmode[0] != 'd')
                               4748                 :            274 :         conn->allowed_enc_methods |= ENC_GSSAPI;
                               4749                 :                : #endif
                               4750                 :                : 
                               4751   [ +  +  +  +  :            345 :     if ((conn->sslmode[0] == 'd' || conn->sslmode[0] == 'p' || conn->sslmode[0] == 'a') &&
                                              +  + ]
                               4752   [ +  +  +  + ]:            165 :         (conn->gssencmode[0] == 'd' || conn->gssencmode[0] == 'p'))
                               4753                 :                :     {
                               4754                 :            120 :         conn->allowed_enc_methods |= ENC_PLAINTEXT;
                               4755                 :                :     }
                               4756                 :                : 
                               4757                 :            345 :     return select_next_encryption_method(conn, false);
                               4758                 :                : }
                               4759                 :                : 
                               4760                 :                : /*
                               4761                 :                :  * Out-of-line portion of the ENCRYPTION_NEGOTIATION_FAILED() macro in the
                               4762                 :                :  * PQconnectPoll state machine.
                               4763                 :                :  *
                               4764                 :                :  * Return value:
                               4765                 :                :  *  0: connection failed and we are out of encryption methods to try. return an error
                               4766                 :                :  *  1: Retry with next connection method. The TCP connection is still valid and in
                               4767                 :                :  *     known state, so we can proceed with the negotiating next method without
                               4768                 :                :  *     reconnecting.
                               4769                 :                :  *  2: Disconnect, and retry with next connection method.
                               4770                 :                :  *
                               4771                 :                :  * conn->current_enc_method is updated to the next method to try.
                               4772                 :                :  */
                               4773                 :                : #if defined(USE_SSL) || defined(ENABLE_GSS)
                               4774                 :                : static int
                               4775                 :             27 : encryption_negotiation_failed(PGconn *conn)
                               4776                 :                : {
                               4777         [ -  + ]:             27 :     Assert((conn->failed_enc_methods & conn->current_enc_method) == 0);
                               4778                 :             27 :     conn->failed_enc_methods |= conn->current_enc_method;
                               4779                 :                : 
                               4780         [ +  + ]:             27 :     if (select_next_encryption_method(conn, true))
                               4781                 :                :     {
                               4782                 :                :         /* An existing connection cannot be reused for direct SSL */
  579                          4783   [ -  +  -  - ]:             20 :         if (conn->current_enc_method == ENC_SSL && conn->sslnegotiation[0] == 'd')
  617 heikki.linnakangas@i     4784                 :UBC           0 :             return 2;
                               4785                 :                :         else
  617 heikki.linnakangas@i     4786                 :CBC          20 :             return 1;
                               4787                 :                :     }
                               4788                 :                :     else
                               4789                 :              7 :         return 0;
                               4790                 :                : }
                               4791                 :                : #endif
                               4792                 :                : 
                               4793                 :                : /*
                               4794                 :                :  * Out-of-line portion of the CONNECTION_FAILED() macro
                               4795                 :                :  *
                               4796                 :                :  * Returns true, if we should reconnect and retry with a different encryption
                               4797                 :                :  * method.  conn->current_enc_method is updated to the next method to try.
                               4798                 :                :  */
                               4799                 :                : static bool
                               4800                 :            134 : connection_failed(PGconn *conn)
                               4801                 :                : {
                               4802         [ -  + ]:            134 :     Assert((conn->failed_enc_methods & conn->current_enc_method) == 0);
                               4803                 :            134 :     conn->failed_enc_methods |= conn->current_enc_method;
                               4804                 :                : 
                               4805                 :            134 :     return select_next_encryption_method(conn, false);
                               4806                 :                : }
                               4807                 :                : 
                               4808                 :                : /*
                               4809                 :                :  * Choose the next encryption method to try. If this is a retry,
                               4810                 :                :  * conn->failed_enc_methods has already been updated. The function sets
                               4811                 :                :  * conn->current_enc_method to the next method to try. Returns false if no
                               4812                 :                :  * encryption methods remain.
                               4813                 :                :  */
                               4814                 :                : static bool
                               4815                 :            506 : select_next_encryption_method(PGconn *conn, bool have_valid_connection)
                               4816                 :                : {
                               4817                 :                :     int         remaining_methods;
                               4818                 :                : 
                               4819                 :                : #define SELECT_NEXT_METHOD(method) \
                               4820                 :                :     do { \
                               4821                 :                :         if ((remaining_methods & method) != 0) \
                               4822                 :                :         { \
                               4823                 :                :             conn->current_enc_method = method; \
                               4824                 :                :             return true; \
                               4825                 :                :         } \
                               4826                 :                :     } while (false)
                               4827                 :                : 
                               4828                 :            506 :     remaining_methods = conn->allowed_enc_methods & ~conn->failed_enc_methods;
                               4829                 :                : 
                               4830                 :                :     /*
                               4831                 :                :      * Try GSSAPI before SSL
                               4832                 :                :      */
                               4833                 :                : #ifdef ENABLE_GSS
                               4834         [ +  + ]:            506 :     if ((remaining_methods & ENC_GSSAPI) != 0)
                               4835                 :                :     {
                               4836                 :                :         /*
                               4837                 :                :          * If GSSAPI encryption is enabled, then call pg_GSS_have_cred_cache()
                               4838                 :                :          * which will return true if we can acquire credentials (and give us a
                               4839                 :                :          * handle to use in conn->gcred), and then send a packet to the server
                               4840                 :                :          * asking for GSSAPI Encryption (and skip past SSL negotiation and
                               4841                 :                :          * regular startup below).
                               4842                 :                :          */
                               4843         [ +  - ]:            249 :         if (!conn->gctx)
                               4844                 :                :         {
                               4845         [ +  + ]:            249 :             if (!pg_GSS_have_cred_cache(&conn->gcred))
                               4846                 :                :             {
                               4847                 :            128 :                 conn->allowed_enc_methods &= ~ENC_GSSAPI;
                               4848                 :            128 :                 remaining_methods &= ~ENC_GSSAPI;
                               4849                 :                : 
                               4850         [ -  + ]:            128 :                 if (conn->gssencmode[0] == 'r')
                               4851                 :                :                 {
  617 heikki.linnakangas@i     4852                 :UBC           0 :                     libpq_append_conn_error(conn,
                               4853                 :                :                                             "GSSAPI encryption required but no credential cache");
                               4854                 :                :                 }
                               4855                 :                :             }
                               4856                 :                :         }
                               4857                 :                :     }
                               4858                 :                : 
  617 heikki.linnakangas@i     4859         [ +  + ]:CBC         506 :     SELECT_NEXT_METHOD(ENC_GSSAPI);
                               4860                 :                : #endif
                               4861                 :                : 
                               4862                 :                :     /*
                               4863                 :                :      * The order between SSL encryption and plaintext depends on sslmode. With
                               4864                 :                :      * sslmode=allow, try plaintext connection before SSL. With
                               4865                 :                :      * sslmode=prefer, it's the other way round. With other modes, we only try
                               4866                 :                :      * plaintext or SSL connections so the order they're listed here doesn't
                               4867                 :                :      * matter.
                               4868                 :                :      */
  579                          4869         [ +  + ]:            385 :     if (conn->sslmode[0] == 'a')
                               4870         [ +  + ]:             34 :         SELECT_NEXT_METHOD(ENC_PLAINTEXT);
                               4871                 :                : 
                               4872         [ +  + ]:            366 :     SELECT_NEXT_METHOD(ENC_SSL);
                               4873                 :                : 
  617                          4874         [ +  + ]:            155 :     if (conn->sslmode[0] != 'a')
                               4875         [ +  + ]:            150 :         SELECT_NEXT_METHOD(ENC_PLAINTEXT);
                               4876                 :                : 
                               4877                 :                :     /* No more options */
                               4878                 :            109 :     conn->current_enc_method = ENC_ERROR;
                               4879                 :            109 :     return false;
                               4880                 :                : #undef SELECT_NEXT_METHOD
                               4881                 :                : }
                               4882                 :                : 
                               4883                 :                : /*
                               4884                 :                :  * internal_ping
                               4885                 :                :  *      Determine if a server is running and if we can connect to it.
                               4886                 :                :  *
                               4887                 :                :  * The argument is a connection that's been started, but not completed.
                               4888                 :                :  */
                               4889                 :                : static PGPing
 5500 bruce@momjian.us         4890                 :            472 : internal_ping(PGconn *conn)
                               4891                 :                : {
                               4892                 :                :     /* Say "no attempt" if we never got to PQconnectPoll */
 5498 tgl@sss.pgh.pa.us        4893   [ +  -  -  + ]:            472 :     if (!conn || !conn->options_valid)
 5498 tgl@sss.pgh.pa.us        4894                 :UBC           0 :         return PQPING_NO_ATTEMPT;
                               4895                 :                : 
                               4896                 :                :     /* Attempt to complete the connection */
 5498 tgl@sss.pgh.pa.us        4897         [ +  + ]:CBC         472 :     if (conn->status != CONNECTION_BAD)
  681 alvherre@alvh.no-ip.     4898                 :            310 :         (void) pqConnectDBComplete(conn);
                               4899                 :                : 
                               4900                 :                :     /* Definitely OK if we succeeded */
 5498 tgl@sss.pgh.pa.us        4901         [ +  + ]:            472 :     if (conn->status != CONNECTION_BAD)
                               4902                 :             95 :         return PQPING_OK;
                               4903                 :                : 
                               4904                 :                :     /*
                               4905                 :                :      * Here begins the interesting part of "ping": determine the cause of the
                               4906                 :                :      * failure in sufficient detail to decide what to return.  We do not want
                               4907                 :                :      * to report that the server is not up just because we didn't have a valid
                               4908                 :                :      * password, for example.  In fact, any sort of authentication request
                               4909                 :                :      * implies the server is up.  (We need this check since the libpq side of
                               4910                 :                :      * things might have pulled the plug on the connection before getting an
                               4911                 :                :      * error as such from the postmaster.)
                               4912                 :                :      */
                               4913         [ -  + ]:            377 :     if (conn->auth_req_received)
 5498 tgl@sss.pgh.pa.us        4914                 :UBC           0 :         return PQPING_OK;
                               4915                 :                : 
                               4916                 :                :     /*
                               4917                 :                :      * If we failed to get any ERROR response from the postmaster, report
                               4918                 :                :      * PQPING_NO_RESPONSE.  This result could be somewhat misleading for a
                               4919                 :                :      * pre-7.4 server, since it won't send back a SQLSTATE, but those are long
                               4920                 :                :      * out of support.  Another corner case where the server could return a
                               4921                 :                :      * failure without a SQLSTATE is fork failure, but PQPING_NO_RESPONSE
                               4922                 :                :      * isn't totally unreasonable for that anyway.  We expect that every other
                               4923                 :                :      * failure case in a modern server will produce a report with a SQLSTATE.
                               4924                 :                :      *
                               4925                 :                :      * NOTE: whenever we get around to making libpq generate SQLSTATEs for
                               4926                 :                :      * client-side errors, we should either not store those into
                               4927                 :                :      * last_sqlstate, or add an extra flag so we can tell client-side errors
                               4928                 :                :      * apart from server-side ones.
                               4929                 :                :      */
 5498 tgl@sss.pgh.pa.us        4930         [ +  + ]:CBC         377 :     if (strlen(conn->last_sqlstate) != 5)
                               4931                 :            162 :         return PQPING_NO_RESPONSE;
                               4932                 :                : 
                               4933                 :                :     /*
                               4934                 :                :      * Report PQPING_REJECT if server says it's not accepting connections.
                               4935                 :                :      */
                               4936         [ +  - ]:            215 :     if (strcmp(conn->last_sqlstate, ERRCODE_CANNOT_CONNECT_NOW) == 0)
                               4937                 :            215 :         return PQPING_REJECT;
                               4938                 :                : 
                               4939                 :                :     /*
                               4940                 :                :      * Any other SQLSTATE can be taken to indicate that the server is up.
                               4941                 :                :      * Presumably it didn't like our username, password, or database name; or
                               4942                 :                :      * perhaps it had some transient failure, but that should not be taken as
                               4943                 :                :      * meaning "it's down".
                               4944                 :                :      */
 5498 tgl@sss.pgh.pa.us        4945                 :UBC           0 :     return PQPING_OK;
                               4946                 :                : }
                               4947                 :                : 
                               4948                 :                : 
                               4949                 :                : /*
                               4950                 :                :  * pqMakeEmptyPGconn
                               4951                 :                :  *   - create a PGconn data structure with (as yet) no interesting data
                               4952                 :                :  */
                               4953                 :                : PGconn *
  681 alvherre@alvh.no-ip.     4954                 :CBC       13590 : pqMakeEmptyPGconn(void)
                               4955                 :                : {
                               4956                 :                :     PGconn     *conn;
                               4957                 :                : 
                               4958                 :                : #ifdef WIN32
                               4959                 :                : 
                               4960                 :                :     /*
                               4961                 :                :      * Make sure socket support is up and running in this process.
                               4962                 :                :      *
                               4963                 :                :      * Note: the Windows documentation says that we should eventually do a
                               4964                 :                :      * matching WSACleanup() call, but experience suggests that that is at
                               4965                 :                :      * least as likely to cause problems as fix them.  So we don't.
                               4966                 :                :      */
                               4967                 :                :     static bool wsastartup_done = false;
                               4968                 :                : 
                               4969                 :                :     if (!wsastartup_done)
                               4970                 :                :     {
                               4971                 :                :         WSADATA     wsaData;
                               4972                 :                : 
                               4973                 :                :         if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
                               4974                 :                :             return NULL;
                               4975                 :                :         wsastartup_done = true;
                               4976                 :                :     }
                               4977                 :                : 
                               4978                 :                :     /* Forget any earlier error */
                               4979                 :                :     WSASetLastError(0);
                               4980                 :                : #endif                          /* WIN32 */
                               4981                 :                : 
 8173 tgl@sss.pgh.pa.us        4982                 :          13590 :     conn = (PGconn *) malloc(sizeof(PGconn));
                               4983         [ -  + ]:          13590 :     if (conn == NULL)
 8173 tgl@sss.pgh.pa.us        4984                 :UBC           0 :         return conn;
                               4985                 :                : 
                               4986                 :                :     /* Zero all pointers and booleans */
 8014 neilc@samurai.com        4987   [ +  -  +  -  :CBC       13590 :     MemSet(conn, 0, sizeof(PGconn));
                                     +  -  -  +  -  
                                                 - ]
                               4988                 :                : 
                               4989                 :                :     /* install default notice hooks */
 8214 tgl@sss.pgh.pa.us        4990                 :          13590 :     conn->noticeHooks.noticeRec = defaultNoticeReceiver;
                               4991                 :          13590 :     conn->noticeHooks.noticeProc = defaultNoticeProcessor;
                               4992                 :                : 
10086 bruce@momjian.us         4993                 :          13590 :     conn->status = CONNECTION_BAD;
                               4994                 :          13590 :     conn->asyncStatus = PGASYNC_IDLE;
 1737 alvherre@alvh.no-ip.     4995                 :          13590 :     conn->pipelineStatus = PQ_PIPELINE_OFF;
 8214 tgl@sss.pgh.pa.us        4996                 :          13590 :     conn->xactStatus = PQTRANS_IDLE;
 7246                          4997                 :          13590 :     conn->options_valid = false;
                               4998                 :          13590 :     conn->nonblocking = false;
 8227                          4999                 :          13590 :     conn->client_encoding = PG_SQL_ASCII;
 7149                          5000                 :          13590 :     conn->std_strings = false;   /* unless server says differently */
 1750                          5001                 :          13590 :     conn->default_transaction_read_only = PG_BOOL_UNKNOWN;
                               5002                 :          13590 :     conn->in_hot_standby = PG_BOOL_UNKNOWN;
  995 dgustafsson@postgres     5003                 :          13590 :     conn->scram_sha_256_iterations = SCRAM_SHA_256_DEFAULT_ITERATIONS;
 8214 tgl@sss.pgh.pa.us        5004                 :          13590 :     conn->verbosity = PQERRORS_DEFAULT;
 3755                          5005                 :          13590 :     conn->show_context = PQSHOW_CONTEXT_ERRORS;
 4262 bruce@momjian.us         5006                 :          13590 :     conn->sock = PGINVALID_SOCKET;
  313 dgustafsson@postgres     5007                 :          13590 :     conn->altsock = PGINVALID_SOCKET;
 1721 alvherre@alvh.no-ip.     5008                 :          13590 :     conn->Pfdebug = NULL;
                               5009                 :                : 
                               5010                 :                :     /*
                               5011                 :                :      * We try to send at least 8K at a time, which is the usual size of pipe
                               5012                 :                :      * buffers on Unix systems.  That way, when we are sending a large amount
                               5013                 :                :      * of data, we avoid incurring extra kernel context swaps for partial
                               5014                 :                :      * bufferloads.  The output buffer is initially made 16K in size, and we
                               5015                 :                :      * try to dump it after accumulating 8K.
                               5016                 :                :      *
                               5017                 :                :      * With the same goal of minimizing context swaps, the input buffer will
                               5018                 :                :      * be enlarged anytime it has less than 8K free, so we initially allocate
                               5019                 :                :      * twice that.
                               5020                 :                :      */
 9604 tgl@sss.pgh.pa.us        5021                 :          13590 :     conn->inBufSize = 16 * 1024;
10086 bruce@momjian.us         5022                 :          13590 :     conn->inBuffer = (char *) malloc(conn->inBufSize);
 8277 tgl@sss.pgh.pa.us        5023                 :          13590 :     conn->outBufSize = 16 * 1024;
10086 bruce@momjian.us         5024                 :          13590 :     conn->outBuffer = (char *) malloc(conn->outBufSize);
 5004 tgl@sss.pgh.pa.us        5025                 :          13590 :     conn->rowBufLen = 32;
                               5026                 :          13590 :     conn->rowBuf = (PGdataValue *) malloc(conn->rowBufLen * sizeof(PGdataValue));
 9604                          5027                 :          13590 :     initPQExpBuffer(&conn->errorMessage);
                               5028                 :          13590 :     initPQExpBuffer(&conn->workBuffer);
                               5029                 :                : 
                               5030         [ +  - ]:          13590 :     if (conn->inBuffer == NULL ||
                               5031         [ +  - ]:          13590 :         conn->outBuffer == NULL ||
 5004                          5032         [ +  - ]:          13590 :         conn->rowBuf == NULL ||
 6229                          5033   [ +  -  +  - ]:          13590 :         PQExpBufferBroken(&conn->errorMessage) ||
                               5034   [ +  -  -  + ]:          13590 :         PQExpBufferBroken(&conn->workBuffer))
                               5035                 :                :     {
                               5036                 :                :         /* out of memory already :-( */
10086 bruce@momjian.us         5037                 :UBC           0 :         freePGconn(conn);
                               5038                 :              0 :         conn = NULL;
                               5039                 :                :     }
                               5040                 :                : 
10086 bruce@momjian.us         5041                 :CBC       13590 :     return conn;
                               5042                 :                : }
                               5043                 :                : 
                               5044                 :                : /*
                               5045                 :                :  * freePGconn
                               5046                 :                :  *   - free an idle (closed) PGconn data structure
                               5047                 :                :  *
                               5048                 :                :  * NOTE: this should not overlap any functionality with pqClosePGconn().
                               5049                 :                :  * Clearing/resetting of transient state belongs there; what we do here is
                               5050                 :                :  * release data that is to be held for the life of the PGconn structure.
                               5051                 :                :  * If a value ought to be cleared/freed during PQreset(), do it there not here.
                               5052                 :                :  */
                               5053                 :                : static void
10326                          5054                 :          13388 : freePGconn(PGconn *conn)
                               5055                 :                : {
                               5056                 :                :     /* let any event procs clean up their state data */
 1279 peter@eisentraut.org     5057         [ -  + ]:          13388 :     for (int i = 0; i < conn->nEvents; i++)
                               5058                 :                :     {
                               5059                 :                :         PGEventConnDestroy evt;
                               5060                 :                : 
 6299 tgl@sss.pgh.pa.us        5061                 :UBC           0 :         evt.conn = conn;
                               5062                 :              0 :         (void) conn->events[i].proc(PGEVT_CONNDESTROY, &evt,
                               5063                 :              0 :                                     conn->events[i].passThrough);
                               5064                 :              0 :         free(conn->events[i].name);
                               5065                 :                :     }
                               5066                 :                : 
                               5067                 :                :     /* free everything not freed in pqClosePGconn */
 1279 peter@eisentraut.org     5068                 :CBC       13388 :     free(conn->pghost);
                               5069                 :          13388 :     free(conn->pghostaddr);
                               5070                 :          13388 :     free(conn->pgport);
                               5071                 :          13388 :     free(conn->connect_timeout);
                               5072                 :          13388 :     free(conn->pgtcp_user_timeout);
  208 tgl@sss.pgh.pa.us        5073                 :          13388 :     free(conn->client_encoding_initial);
 1279 peter@eisentraut.org     5074                 :          13388 :     free(conn->pgoptions);
                               5075                 :          13388 :     free(conn->appname);
                               5076                 :          13388 :     free(conn->fbappname);
                               5077                 :          13388 :     free(conn->dbName);
                               5078                 :          13388 :     free(conn->replication);
  208 tgl@sss.pgh.pa.us        5079                 :          13388 :     free(conn->pgservice);
  156 michael@paquier.xyz      5080                 :GNC       13388 :     free(conn->pgservicefile);
 1279 peter@eisentraut.org     5081                 :CBC       13388 :     free(conn->pguser);
10101 scrappy@hub.org          5082         [ +  + ]:          13388 :     if (conn->pgpass)
                               5083                 :                :     {
 2294 peter@eisentraut.org     5084                 :            183 :         explicit_bzero(conn->pgpass, strlen(conn->pgpass));
10101 scrappy@hub.org          5085                 :            183 :         free(conn->pgpass);
                               5086                 :                :     }
 1279 peter@eisentraut.org     5087                 :          13388 :     free(conn->pgpassfile);
                               5088                 :          13388 :     free(conn->channel_binding);
                               5089                 :          13388 :     free(conn->keepalives);
                               5090                 :          13388 :     free(conn->keepalives_idle);
                               5091                 :          13388 :     free(conn->keepalives_interval);
                               5092                 :          13388 :     free(conn->keepalives_count);
                               5093                 :          13388 :     free(conn->sslmode);
  617 heikki.linnakangas@i     5094                 :          13388 :     free(conn->sslnegotiation);
  208 tgl@sss.pgh.pa.us        5095                 :          13388 :     free(conn->sslcompression);
 1279 peter@eisentraut.org     5096                 :          13388 :     free(conn->sslkey);
  208 tgl@sss.pgh.pa.us        5097                 :          13388 :     free(conn->sslcert);
 2188                          5098         [ +  + ]:          13388 :     if (conn->sslpassword)
                               5099                 :                :     {
 2035 michael@paquier.xyz      5100                 :              3 :         explicit_bzero(conn->sslpassword, strlen(conn->sslpassword));
 2188 tgl@sss.pgh.pa.us        5101                 :              3 :         free(conn->sslpassword);
                               5102                 :                :     }
  998 michael@paquier.xyz      5103                 :          13388 :     free(conn->sslcertmode);
 1279 peter@eisentraut.org     5104                 :          13388 :     free(conn->sslrootcert);
                               5105                 :          13388 :     free(conn->sslcrl);
                               5106                 :          13388 :     free(conn->sslcrldir);
                               5107                 :          13388 :     free(conn->sslsni);
                               5108                 :          13388 :     free(conn->requirepeer);
                               5109                 :          13388 :     free(conn->gssencmode);
                               5110                 :          13388 :     free(conn->krbsrvname);
                               5111                 :          13388 :     free(conn->gsslib);
  940 tgl@sss.pgh.pa.us        5112                 :          13388 :     free(conn->gssdelegation);
  208                          5113                 :          13388 :     free(conn->min_protocol_version);
                               5114                 :          13388 :     free(conn->max_protocol_version);
                               5115                 :          13388 :     free(conn->ssl_min_protocol_version);
                               5116                 :          13388 :     free(conn->ssl_max_protocol_version);
 1279 peter@eisentraut.org     5117                 :          13388 :     free(conn->target_session_attrs);
  208 tgl@sss.pgh.pa.us        5118                 :          13388 :     free(conn->require_auth);
  993 dgustafsson@postgres     5119                 :          13388 :     free(conn->load_balance_hosts);
  335 peter@eisentraut.org     5120                 :          13388 :     free(conn->scram_client_key);
                               5121                 :          13388 :     free(conn->scram_server_key);
  208 tgl@sss.pgh.pa.us        5122                 :          13388 :     free(conn->sslkeylogfile);
  299 dgustafsson@postgres     5123                 :          13388 :     free(conn->oauth_issuer);
                               5124                 :          13388 :     free(conn->oauth_issuer_id);
                               5125                 :          13388 :     free(conn->oauth_discovery_uri);
                               5126                 :          13388 :     free(conn->oauth_client_id);
                               5127                 :          13388 :     free(conn->oauth_client_secret);
                               5128                 :          13388 :     free(conn->oauth_scope);
                               5129                 :                :     /* Note that conn->Pfdebug is not ours to close or free */
  208 tgl@sss.pgh.pa.us        5130                 :          13388 :     free(conn->events);
                               5131                 :          13388 :     pqReleaseConnHosts(conn);
                               5132                 :          13388 :     free(conn->connip);
                               5133                 :          13388 :     release_conn_addrinfo(conn);
                               5134                 :          13388 :     free(conn->scram_client_key_binary);
                               5135                 :          13388 :     free(conn->scram_server_key_binary);
                               5136                 :                :     /* if this is a cancel connection, be_cancel_key may still be allocated */
                               5137                 :          13388 :     free(conn->be_cancel_key);
                               5138                 :          13388 :     free(conn->inBuffer);
                               5139                 :          13388 :     free(conn->outBuffer);
                               5140                 :          13388 :     free(conn->rowBuf);
 9604                          5141                 :          13388 :     termPQExpBuffer(&conn->errorMessage);
                               5142                 :          13388 :     termPQExpBuffer(&conn->workBuffer);
                               5143                 :                : 
10327 bruce@momjian.us         5144                 :          13388 :     free(conn);
10752 scrappy@hub.org          5145                 :          13388 : }
                               5146                 :                : 
                               5147                 :                : /*
                               5148                 :                :  * pqReleaseConnHosts
                               5149                 :                :  *   - Free the host list in the PGconn.
                               5150                 :                :  */
                               5151                 :                : void
  681 alvherre@alvh.no-ip.     5152                 :          13395 : pqReleaseConnHosts(PGconn *conn)
                               5153                 :                : {
                               5154         [ +  + ]:          13395 :     if (conn->connhost)
                               5155                 :                :     {
                               5156         [ +  + ]:          26903 :         for (int i = 0; i < conn->nconnhost; ++i)
                               5157                 :                :         {
                               5158                 :          13518 :             free(conn->connhost[i].host);
                               5159                 :          13518 :             free(conn->connhost[i].hostaddr);
                               5160                 :          13518 :             free(conn->connhost[i].port);
                               5161         [ +  + ]:          13518 :             if (conn->connhost[i].password != NULL)
                               5162                 :                :             {
                               5163                 :              9 :                 explicit_bzero(conn->connhost[i].password,
                               5164                 :              9 :                                strlen(conn->connhost[i].password));
                               5165                 :              9 :                 free(conn->connhost[i].password);
                               5166                 :                :             }
                               5167                 :                :         }
                               5168                 :          13385 :         free(conn->connhost);
  208 tgl@sss.pgh.pa.us        5169                 :          13385 :         conn->connhost = NULL;
                               5170                 :                :     }
  681 alvherre@alvh.no-ip.     5171                 :          13395 : }
                               5172                 :                : 
                               5173                 :                : /*
                               5174                 :                :  * store_conn_addrinfo
                               5175                 :                :  *   - copy addrinfo to PGconn object
                               5176                 :                :  *
                               5177                 :                :  * Copies the addrinfos from addrlist to the PGconn object such that the
                               5178                 :                :  * addrinfos can be manipulated by libpq. Returns a positive integer on
                               5179                 :                :  * failure, otherwise zero.
                               5180                 :                :  */
                               5181                 :                : static int
  993 dgustafsson@postgres     5182                 :          13482 : store_conn_addrinfo(PGconn *conn, struct addrinfo *addrlist)
                               5183                 :                : {
                               5184                 :          13482 :     struct addrinfo *ai = addrlist;
                               5185                 :                : 
                               5186                 :          13482 :     conn->whichaddr = 0;
                               5187                 :                : 
                               5188                 :          13482 :     conn->naddr = 0;
                               5189         [ +  + ]:          26964 :     while (ai)
                               5190                 :                :     {
                               5191                 :          13482 :         ai = ai->ai_next;
                               5192                 :          13482 :         conn->naddr++;
                               5193                 :                :     }
                               5194                 :                : 
                               5195                 :          13482 :     conn->addr = calloc(conn->naddr, sizeof(AddrInfo));
                               5196         [ -  + ]:          13482 :     if (conn->addr == NULL)
                               5197                 :                :     {
  993 dgustafsson@postgres     5198                 :UBC           0 :         libpq_append_conn_error(conn, "out of memory");
                               5199                 :              0 :         return 1;
                               5200                 :                :     }
                               5201                 :                : 
  993 dgustafsson@postgres     5202                 :CBC       13482 :     ai = addrlist;
                               5203         [ +  + ]:          26964 :     for (int i = 0; i < conn->naddr; i++)
                               5204                 :                :     {
                               5205                 :          13482 :         conn->addr[i].family = ai->ai_family;
                               5206                 :                : 
                               5207                 :          13482 :         memcpy(&conn->addr[i].addr.addr, ai->ai_addr,
                               5208                 :          13482 :                ai->ai_addrlen);
                               5209                 :          13482 :         conn->addr[i].addr.salen = ai->ai_addrlen;
                               5210                 :          13482 :         ai = ai->ai_next;
                               5211                 :                :     }
                               5212                 :                : 
                               5213                 :          13482 :     return 0;
                               5214                 :                : }
                               5215                 :                : 
                               5216                 :                : /*
                               5217                 :                :  * release_conn_addrinfo
                               5218                 :                :  *   - Free any addrinfo list in the PGconn.
                               5219                 :                :  */
                               5220                 :                : static void
 2672 tgl@sss.pgh.pa.us        5221                 :          53037 : release_conn_addrinfo(PGconn *conn)
                               5222                 :                : {
  993 dgustafsson@postgres     5223         [ +  + ]:          53037 :     if (conn->addr)
                               5224                 :                :     {
                               5225                 :          13482 :         free(conn->addr);
                               5226                 :          13482 :         conn->addr = NULL;
                               5227                 :                :     }
 3304 rhaas@postgresql.org     5228                 :          53037 : }
                               5229                 :                : 
                               5230                 :                : /*
                               5231                 :                :  * sendTerminateConn
                               5232                 :                :  *   - Send a terminate message to backend.
                               5233                 :                :  */
                               5234                 :                : static void
                               5235                 :          13405 : sendTerminateConn(PGconn *conn)
                               5236                 :                : {
                               5237                 :                :     /*
                               5238                 :                :      * The Postgres cancellation protocol does not have a notion of a
                               5239                 :                :      * Terminate message, so don't send one.
                               5240                 :                :      */
  644 alvherre@alvh.no-ip.     5241         [ +  + ]:          13405 :     if (conn->cancelRequest)
                               5242                 :              9 :         return;
                               5243                 :                : 
                               5244                 :                :     /*
                               5245                 :                :      * Note that the protocol doesn't allow us to send Terminate messages
                               5246                 :                :      * during the startup phase.
                               5247                 :                :      */
 4262 bruce@momjian.us         5248   [ +  +  +  + ]:          13396 :     if (conn->sock != PGINVALID_SOCKET && conn->status == CONNECTION_OK)
                               5249                 :                :     {
                               5250                 :                :         /*
                               5251                 :                :          * Try to send "close connection" message to backend. Ignore any
                               5252                 :                :          * error.
                               5253                 :                :          */
  847 nathan@postgresql.or     5254                 :          12519 :         pqPutMsgStart(PqMsg_Terminate, conn);
 8277 tgl@sss.pgh.pa.us        5255                 :          12519 :         pqPutMsgEnd(conn);
 4308 sfrost@snowman.net       5256                 :          12519 :         (void) pqFlush(conn);
                               5257                 :                :     }
                               5258                 :                : }
                               5259                 :                : 
                               5260                 :                : /*
                               5261                 :                :  * pqClosePGconn
                               5262                 :                :  *   - properly close a connection to the backend
                               5263                 :                :  *
                               5264                 :                :  * This should reset or release all transient state, but NOT the connection
                               5265                 :                :  * parameters.  On exit, the PGconn should be in condition to start a fresh
                               5266                 :                :  * connection with the same parameters (see PQreset()).
                               5267                 :                :  */
                               5268                 :                : void
  681 alvherre@alvh.no-ip.     5269                 :          13390 : pqClosePGconn(PGconn *conn)
                               5270                 :                : {
                               5271                 :                :     /*
                               5272                 :                :      * If possible, send Terminate message to close the connection politely.
                               5273                 :                :      */
 3304 rhaas@postgresql.org     5274                 :          13390 :     sendTerminateConn(conn);
                               5275                 :                : 
                               5276                 :                :     /*
                               5277                 :                :      * Must reset the blocking status so a possible reconnect will work.
                               5278                 :                :      *
                               5279                 :                :      * Don't call PQsetnonblocking() because it will fail if it's unable to
                               5280                 :                :      * flush the connection.
                               5281                 :                :      */
 3044 peter_e@gmx.net          5282                 :          13390 :     conn->nonblocking = false;
                               5283                 :                : 
                               5284                 :                :     /*
                               5285                 :                :      * Close the connection, reset all transient state, flush I/O buffers.
                               5286                 :                :      * Note that this includes clearing conn's error state; we're no longer
                               5287                 :                :      * interested in any failures associated with the old connection, and we
                               5288                 :                :      * want a clean slate for any new connection attempt.
                               5289                 :                :      */
 3687 tgl@sss.pgh.pa.us        5290                 :          13390 :     pqDropConnection(conn, true);
 3100                          5291                 :          13390 :     conn->status = CONNECTION_BAD;   /* Well, not really _bad_ - just absent */
10086 bruce@momjian.us         5292                 :          13390 :     conn->asyncStatus = PGASYNC_IDLE;
 2689 tgl@sss.pgh.pa.us        5293                 :          13390 :     conn->xactStatus = PQTRANS_IDLE;
 1737 alvherre@alvh.no-ip.     5294                 :          13390 :     conn->pipelineStatus = PQ_PIPELINE_OFF;
  299 dgustafsson@postgres     5295                 :          13390 :     pqClearOAuthToken(conn);
 5004 tgl@sss.pgh.pa.us        5296                 :          13390 :     pqClearAsyncResult(conn);   /* deallocate result */
 1397                          5297                 :          13390 :     pqClearConnErrorState(conn);
                               5298                 :                : 
                               5299                 :                :     /*
                               5300                 :                :      * Release addrinfo, but since cancel requests never change their addrinfo
                               5301                 :                :      * we don't do that. Otherwise we would have to rebuild it during a
                               5302                 :                :      * PQcancelReset.
                               5303                 :                :      */
  644 alvherre@alvh.no-ip.     5304         [ +  + ]:          13390 :     if (!conn->cancelRequest)
                               5305                 :          13381 :         release_conn_addrinfo(conn);
                               5306                 :                : 
                               5307                 :                :     /* Reset all state obtained from server, too */
 2689 tgl@sss.pgh.pa.us        5308                 :          13390 :     pqDropServerData(conn);
10752 scrappy@hub.org          5309                 :          13390 : }
                               5310                 :                : 
                               5311                 :                : /*
                               5312                 :                :  * PQfinish: properly close a connection to the backend. Also frees
                               5313                 :                :  * the PGconn data structure so it shouldn't be re-used after this.
                               5314                 :                :  */
                               5315                 :                : void
10326 bruce@momjian.us         5316                 :          13388 : PQfinish(PGconn *conn)
                               5317                 :                : {
 9991                          5318         [ +  - ]:          13388 :     if (conn)
                               5319                 :                :     {
  681 alvherre@alvh.no-ip.     5320                 :          13388 :         pqClosePGconn(conn);
10327 bruce@momjian.us         5321                 :          13388 :         freePGconn(conn);
                               5322                 :                :     }
10752 scrappy@hub.org          5323                 :          13388 : }
                               5324                 :                : 
                               5325                 :                : /*
                               5326                 :                :  * PQreset: resets the connection to the backend by closing the
                               5327                 :                :  * existing connection and creating a new one.
                               5328                 :                :  */
                               5329                 :                : void
10326 bruce@momjian.us         5330                 :UBC           0 : PQreset(PGconn *conn)
                               5331                 :                : {
 9991                          5332         [ #  # ]:              0 :     if (conn)
                               5333                 :                :     {
  681 alvherre@alvh.no-ip.     5334                 :              0 :         pqClosePGconn(conn);
                               5335                 :                : 
                               5336   [ #  #  #  # ]:              0 :         if (pqConnectDBStart(conn) && pqConnectDBComplete(conn))
                               5337                 :                :         {
                               5338                 :                :             /*
                               5339                 :                :              * Notify event procs of successful reset.
                               5340                 :                :              */
                               5341                 :                :             int         i;
                               5342                 :                : 
 6299 tgl@sss.pgh.pa.us        5343         [ #  # ]:              0 :             for (i = 0; i < conn->nEvents; i++)
                               5344                 :                :             {
                               5345                 :                :                 PGEventConnReset evt;
                               5346                 :                : 
                               5347                 :              0 :                 evt.conn = conn;
 1397                          5348                 :              0 :                 (void) conn->events[i].proc(PGEVT_CONNRESET, &evt,
                               5349                 :              0 :                                             conn->events[i].passThrough);
                               5350                 :                :             }
                               5351                 :                :         }
                               5352                 :                :     }
 9513 bruce@momjian.us         5353                 :              0 : }
                               5354                 :                : 
                               5355                 :                : 
                               5356                 :                : /*
                               5357                 :                :  * PQresetStart:
                               5358                 :                :  * resets the connection to the backend
                               5359                 :                :  * closes the existing connection and makes a new one
                               5360                 :                :  * Returns 1 on success, 0 on failure.
                               5361                 :                :  */
                               5362                 :                : int
                               5363                 :              0 : PQresetStart(PGconn *conn)
                               5364                 :                : {
                               5365         [ #  # ]:              0 :     if (conn)
                               5366                 :                :     {
  681 alvherre@alvh.no-ip.     5367                 :              0 :         pqClosePGconn(conn);
                               5368                 :                : 
                               5369                 :              0 :         return pqConnectDBStart(conn);
                               5370                 :                :     }
                               5371                 :                : 
 9468 tgl@sss.pgh.pa.us        5372                 :              0 :     return 0;
                               5373                 :                : }
                               5374                 :                : 
                               5375                 :                : 
                               5376                 :                : /*
                               5377                 :                :  * PQresetPoll:
                               5378                 :                :  * resets the connection to the backend
                               5379                 :                :  * closes the existing connection and makes a new one
                               5380                 :                :  */
                               5381                 :                : PostgresPollingStatusType
 9513 bruce@momjian.us         5382                 :              0 : PQresetPoll(PGconn *conn)
                               5383                 :                : {
                               5384         [ #  # ]:              0 :     if (conn)
                               5385                 :                :     {
 6299 tgl@sss.pgh.pa.us        5386                 :              0 :         PostgresPollingStatusType status = PQconnectPoll(conn);
                               5387                 :                : 
                               5388         [ #  # ]:              0 :         if (status == PGRES_POLLING_OK)
                               5389                 :                :         {
                               5390                 :                :             /*
                               5391                 :                :              * Notify event procs of successful reset.
                               5392                 :                :              */
                               5393                 :                :             int         i;
                               5394                 :                : 
                               5395         [ #  # ]:              0 :             for (i = 0; i < conn->nEvents; i++)
                               5396                 :                :             {
                               5397                 :                :                 PGEventConnReset evt;
                               5398                 :                : 
                               5399                 :              0 :                 evt.conn = conn;
 1397                          5400                 :              0 :                 (void) conn->events[i].proc(PGEVT_CONNRESET, &evt,
                               5401                 :              0 :                                             conn->events[i].passThrough);
                               5402                 :                :             }
                               5403                 :                :         }
                               5404                 :                : 
 6299                          5405                 :              0 :         return status;
                               5406                 :                :     }
                               5407                 :                : 
 9513 bruce@momjian.us         5408                 :              0 :     return PGRES_POLLING_FAILED;
                               5409                 :                : }
                               5410                 :                : 
                               5411                 :                : /*
                               5412                 :                :  * pqPacketSend() -- convenience routine to send a message to server.
                               5413                 :                :  *
                               5414                 :                :  * pack_type: the single-byte message type code.  (Pass zero for startup
                               5415                 :                :  * packets, which have no message type code.)
                               5416                 :                :  *
                               5417                 :                :  * buf, buf_len: contents of message.  The given length includes only what
                               5418                 :                :  * is in buf; the message type and message length fields are added here.
                               5419                 :                :  *
                               5420                 :                :  * RETURNS: STATUS_ERROR if the write fails, STATUS_OK otherwise.
                               5421                 :                :  * SIDE_EFFECTS: may block.
                               5422                 :                :  */
                               5423                 :                : int
 8279 tgl@sss.pgh.pa.us        5424                 :CBC       13721 : pqPacketSend(PGconn *conn, char pack_type,
                               5425                 :                :              const void *buf, size_t buf_len)
                               5426                 :                : {
                               5427                 :                :     /* Start the message. */
 1748 heikki.linnakangas@i     5428         [ -  + ]:          13721 :     if (pqPutMsgStart(pack_type, conn))
10186 scrappy@hub.org          5429                 :UBC           0 :         return STATUS_ERROR;
                               5430                 :                : 
                               5431                 :                :     /* Send the message body. */
 8279 tgl@sss.pgh.pa.us        5432         [ -  + ]:CBC       13721 :     if (pqPutnchar(buf, buf_len, conn))
10186 scrappy@hub.org          5433                 :UBC           0 :         return STATUS_ERROR;
                               5434                 :                : 
                               5435                 :                :     /* Finish the message. */
 8277 tgl@sss.pgh.pa.us        5436         [ -  + ]:CBC       13721 :     if (pqPutMsgEnd(conn))
 8277 tgl@sss.pgh.pa.us        5437                 :UBC           0 :         return STATUS_ERROR;
                               5438                 :                : 
                               5439                 :                :     /* Flush to ensure backend gets it. */
10086 bruce@momjian.us         5440         [ -  + ]:CBC       13721 :     if (pqFlush(conn))
10086 bruce@momjian.us         5441                 :UBC           0 :         return STATUS_ERROR;
                               5442                 :                : 
10186 scrappy@hub.org          5443                 :CBC       13721 :     return STATUS_OK;
                               5444                 :                : }
                               5445                 :                : 
                               5446                 :                : #ifdef USE_LDAP
                               5447                 :                : 
                               5448                 :                : #define LDAP_URL    "ldap://"
                               5449                 :                : #define LDAP_DEF_PORT   389
                               5450                 :                : #define PGLDAP_TIMEOUT 2
                               5451                 :                : 
                               5452                 :                : #define ld_is_sp_tab(x) ((x) == ' ' || (x) == '\t')
                               5453                 :                : #define ld_is_nl_cr(x) ((x) == '\r' || (x) == '\n')
                               5454                 :                : 
                               5455                 :                : 
                               5456                 :                : /*
                               5457                 :                :  *      ldapServiceLookup
                               5458                 :                :  *
                               5459                 :                :  * Search the LDAP URL passed as first argument, treat the result as a
                               5460                 :                :  * string of connection options that are parsed and added to the array of
                               5461                 :                :  * options passed as second argument.
                               5462                 :                :  *
                               5463                 :                :  * LDAP URLs must conform to RFC 1959 without escape sequences.
                               5464                 :                :  *  ldap://host:port/dn?attributes?scope?filter?extensions
                               5465                 :                :  *
                               5466                 :                :  * Returns
                               5467                 :                :  *  0 if the lookup was successful,
                               5468                 :                :  *  1 if the connection to the LDAP server could be established but
                               5469                 :                :  *    the search was unsuccessful,
                               5470                 :                :  *  2 if a connection could not be established, and
                               5471                 :                :  *  3 if a fatal error occurred.
                               5472                 :                :  *
                               5473                 :                :  * An error message is appended to *errorMessage for return codes 1 and 3.
                               5474                 :                :  */
                               5475                 :                : static int
 7082 bruce@momjian.us         5476                 :              1 : ldapServiceLookup(const char *purl, PQconninfoOption *options,
                               5477                 :                :                   PQExpBuffer errorMessage)
                               5478                 :                : {
 7013                          5479                 :              1 :     int         port = LDAP_DEF_PORT,
                               5480                 :                :                 scope,
                               5481                 :                :                 rc,
                               5482                 :                :                 size,
                               5483                 :                :                 state,
                               5484                 :                :                 oldstate,
                               5485                 :                :                 i;
                               5486                 :                : #ifndef WIN32
                               5487                 :                :     int         msgid;
                               5488                 :                : #endif
                               5489                 :                :     bool        found_keyword;
                               5490                 :                :     char       *url,
                               5491                 :                :                *hostname,
                               5492                 :                :                *portstr,
                               5493                 :                :                *endptr,
                               5494                 :                :                *dn,
                               5495                 :                :                *scopestr,
                               5496                 :                :                *filter,
                               5497                 :                :                *result,
                               5498                 :                :                *p,
                               5499                 :              1 :                *p1 = NULL,
                               5500                 :              1 :                *optname = NULL,
                               5501                 :              1 :                *optval = NULL;
 7082                          5502                 :              1 :     char       *attrs[2] = {NULL, NULL};
                               5503                 :              1 :     LDAP       *ld = NULL;
                               5504                 :                :     LDAPMessage *res,
                               5505                 :                :                *entry;
                               5506                 :                :     struct berval **values;
                               5507                 :              1 :     LDAP_TIMEVAL time = {PGLDAP_TIMEOUT, 0};
  126 peter@eisentraut.org     5508                 :GNC           1 :     int         ldapversion = LDAP_VERSION3;
                               5509                 :                : 
 7082 bruce@momjian.us         5510         [ -  + ]:CBC           1 :     if ((url = strdup(purl)) == NULL)
                               5511                 :                :     {
 1127 peter@eisentraut.org     5512                 :UBC           0 :         libpq_append_error(errorMessage, "out of memory");
 7082 bruce@momjian.us         5513                 :              0 :         return 3;
                               5514                 :                :     }
                               5515                 :                : 
                               5516                 :                :     /*
                               5517                 :                :      * Parse URL components, check for correctness.  Basically, url has '\0'
                               5518                 :                :      * placed at component boundaries and variables are pointed at each
                               5519                 :                :      * component.
                               5520                 :                :      */
                               5521                 :                : 
 7032 tgl@sss.pgh.pa.us        5522         [ -  + ]:CBC           1 :     if (pg_strncasecmp(url, LDAP_URL, strlen(LDAP_URL)) != 0)
                               5523                 :                :     {
 1127 peter@eisentraut.org     5524                 :UBC           0 :         libpq_append_error(errorMessage,
                               5525                 :                :                            "invalid LDAP URL \"%s\": scheme must be ldap://", purl);
 7082 bruce@momjian.us         5526                 :              0 :         free(url);
                               5527                 :              0 :         return 3;
                               5528                 :                :     }
                               5529                 :                : 
                               5530                 :                :     /* hostname */
 7082 bruce@momjian.us         5531                 :CBC           1 :     hostname = url + strlen(LDAP_URL);
 7013                          5532         [ -  + ]:              1 :     if (*hostname == '/')       /* no hostname? */
 5477 bruce@momjian.us         5533                 :UBC           0 :         hostname = DefaultHost; /* the default */
                               5534                 :                : 
                               5535                 :                :     /* dn, "distinguished name" */
 7013 bruce@momjian.us         5536                 :CBC           1 :     p = strchr(url + strlen(LDAP_URL), '/');
 7082                          5537   [ +  -  +  -  :              1 :     if (p == NULL || *(p + 1) == '\0' || *(p + 1) == '?')
                                              -  + ]
                               5538                 :                :     {
 1127 peter@eisentraut.org     5539                 :UBC           0 :         libpq_append_error(errorMessage,
                               5540                 :                :                            "invalid LDAP URL \"%s\": missing distinguished name",
                               5541                 :                :                            purl);
 7082 bruce@momjian.us         5542                 :              0 :         free(url);
                               5543                 :              0 :         return 3;
                               5544                 :                :     }
 7013 bruce@momjian.us         5545                 :CBC           1 :     *p = '\0';                  /* terminate hostname */
 7082                          5546                 :              1 :     dn = p + 1;
                               5547                 :                : 
                               5548                 :                :     /* attribute */
                               5549   [ +  -  +  -  :              1 :     if ((p = strchr(dn, '?')) == NULL || *(p + 1) == '\0' || *(p + 1) == '?')
                                              -  + ]
                               5550                 :                :     {
 1127 peter@eisentraut.org     5551                 :UBC           0 :         libpq_append_error(errorMessage,
                               5552                 :                :                            "invalid LDAP URL \"%s\": must have exactly one attribute",
                               5553                 :                :                            purl);
 7082 bruce@momjian.us         5554                 :              0 :         free(url);
                               5555                 :              0 :         return 3;
                               5556                 :                :     }
 7082 bruce@momjian.us         5557                 :CBC           1 :     *p = '\0';
                               5558                 :              1 :     attrs[0] = p + 1;
                               5559                 :                : 
                               5560                 :                :     /* scope */
                               5561   [ +  -  +  -  :              1 :     if ((p = strchr(attrs[0], '?')) == NULL || *(p + 1) == '\0' || *(p + 1) == '?')
                                              -  + ]
                               5562                 :                :     {
 1127 peter@eisentraut.org     5563                 :UBC           0 :         libpq_append_error(errorMessage,
                               5564                 :                :                            "invalid LDAP URL \"%s\": must have search scope (base/one/sub)",
                               5565                 :                :                            purl);
 7082 bruce@momjian.us         5566                 :              0 :         free(url);
                               5567                 :              0 :         return 3;
                               5568                 :                :     }
 7082 bruce@momjian.us         5569                 :CBC           1 :     *p = '\0';
                               5570                 :              1 :     scopestr = p + 1;
                               5571                 :                : 
                               5572                 :                :     /* filter */
                               5573   [ +  -  +  -  :              1 :     if ((p = strchr(scopestr, '?')) == NULL || *(p + 1) == '\0' || *(p + 1) == '?')
                                              -  + ]
                               5574                 :                :     {
 1127 peter@eisentraut.org     5575                 :UBC           0 :         libpq_append_error(errorMessage,
                               5576                 :                :                            "invalid LDAP URL \"%s\": no filter",
                               5577                 :                :                            purl);
 7082 bruce@momjian.us         5578                 :              0 :         free(url);
                               5579                 :              0 :         return 3;
                               5580                 :                :     }
 7082 bruce@momjian.us         5581                 :CBC           1 :     *p = '\0';
                               5582                 :              1 :     filter = p + 1;
                               5583         [ -  + ]:              1 :     if ((p = strchr(filter, '?')) != NULL)
 7082 bruce@momjian.us         5584                 :UBC           0 :         *p = '\0';
                               5585                 :                : 
                               5586                 :                :     /* port number? */
 7082 bruce@momjian.us         5587         [ +  - ]:CBC           1 :     if ((p1 = strchr(hostname, ':')) != NULL)
                               5588                 :                :     {
                               5589                 :                :         long        lport;
                               5590                 :                : 
                               5591                 :              1 :         *p1 = '\0';
                               5592                 :              1 :         portstr = p1 + 1;
                               5593                 :              1 :         errno = 0;
                               5594                 :              1 :         lport = strtol(portstr, &endptr, 10);
                               5595   [ +  -  +  -  :              1 :         if (*portstr == '\0' || *endptr != '\0' || errno || lport < 0 || lport > 65535)
                                     +  -  +  -  -  
                                                 + ]
                               5596                 :                :         {
 1127 peter@eisentraut.org     5597                 :UBC           0 :             libpq_append_error(errorMessage,
                               5598                 :                :                                "invalid LDAP URL \"%s\": invalid port number",
                               5599                 :                :                                purl);
 7082 bruce@momjian.us         5600                 :              0 :             free(url);
                               5601                 :              0 :             return 3;
                               5602                 :                :         }
 7082 bruce@momjian.us         5603                 :CBC           1 :         port = (int) lport;
                               5604                 :                :     }
                               5605                 :                : 
                               5606                 :                :     /* Allow only one attribute */
                               5607         [ -  + ]:              1 :     if (strchr(attrs[0], ',') != NULL)
                               5608                 :                :     {
 1127 peter@eisentraut.org     5609                 :UBC           0 :         libpq_append_error(errorMessage,
                               5610                 :                :                            "invalid LDAP URL \"%s\": must have exactly one attribute",
                               5611                 :                :                            purl);
 7082 bruce@momjian.us         5612                 :              0 :         free(url);
                               5613                 :              0 :         return 3;
                               5614                 :                :     }
                               5615                 :                : 
                               5616                 :                :     /* set scope */
 7032 tgl@sss.pgh.pa.us        5617         [ -  + ]:CBC           1 :     if (pg_strcasecmp(scopestr, "base") == 0)
 7082 bruce@momjian.us         5618                 :UBC           0 :         scope = LDAP_SCOPE_BASE;
 7032 tgl@sss.pgh.pa.us        5619         [ +  - ]:CBC           1 :     else if (pg_strcasecmp(scopestr, "one") == 0)
 7082 bruce@momjian.us         5620                 :              1 :         scope = LDAP_SCOPE_ONELEVEL;
 7032 tgl@sss.pgh.pa.us        5621         [ #  # ]:UBC           0 :     else if (pg_strcasecmp(scopestr, "sub") == 0)
 7082 bruce@momjian.us         5622                 :              0 :         scope = LDAP_SCOPE_SUBTREE;
                               5623                 :                :     else
                               5624                 :                :     {
 1127 peter@eisentraut.org     5625                 :              0 :         libpq_append_error(errorMessage,
                               5626                 :                :                            "invalid LDAP URL \"%s\": must have search scope (base/one/sub)",
                               5627                 :                :                            purl);
 7082 bruce@momjian.us         5628                 :              0 :         free(url);
                               5629                 :              0 :         return 3;
                               5630                 :                :     }
                               5631                 :                : 
                               5632                 :                :     /* initialize LDAP structure */
 7082 bruce@momjian.us         5633         [ -  + ]:CBC           1 :     if ((ld = ldap_init(hostname, port)) == NULL)
                               5634                 :                :     {
 1127 peter@eisentraut.org     5635                 :UBC           0 :         libpq_append_error(errorMessage, "could not create LDAP structure");
 7082 bruce@momjian.us         5636                 :              0 :         free(url);
                               5637                 :              0 :         return 3;
                               5638                 :                :     }
                               5639                 :                : 
  126 peter@eisentraut.org     5640         [ -  + ]:GNC           1 :     if ((rc = ldap_set_option(ld, LDAP_OPT_PROTOCOL_VERSION, &ldapversion)) != LDAP_SUCCESS)
                               5641                 :                :     {
  126 peter@eisentraut.org     5642                 :UNC           0 :         libpq_append_error(errorMessage, "could not set LDAP protocol version: %s",
                               5643                 :                :                            ldap_err2string(rc));
                               5644                 :              0 :         free(url);
                               5645                 :              0 :         ldap_unbind(ld);
                               5646                 :              0 :         return 3;
                               5647                 :                :     }
                               5648                 :                : 
                               5649                 :                :     /*
                               5650                 :                :      * Perform an explicit anonymous bind.
                               5651                 :                :      *
                               5652                 :                :      * LDAP does not require that an anonymous bind is performed explicitly,
                               5653                 :                :      * but we want to distinguish between the case where LDAP bind does not
                               5654                 :                :      * succeed within PGLDAP_TIMEOUT seconds (return 2 to continue parsing the
                               5655                 :                :      * service control file) and the case where querying the LDAP server fails
                               5656                 :                :      * (return 1 to end parsing).
                               5657                 :                :      *
                               5658                 :                :      * Unfortunately there is no way of setting a timeout that works for both
                               5659                 :                :      * Windows and OpenLDAP.
                               5660                 :                :      */
                               5661                 :                : #ifdef WIN32
                               5662                 :                :     /* the nonstandard ldap_connect function performs an anonymous bind */
                               5663                 :                :     if (ldap_connect(ld, &time) != LDAP_SUCCESS)
                               5664                 :                :     {
                               5665                 :                :         /* error or timeout in ldap_connect */
                               5666                 :                :         free(url);
                               5667                 :                :         ldap_unbind(ld);
                               5668                 :                :         return 2;
                               5669                 :                :     }
                               5670                 :                : #else                           /* !WIN32 */
                               5671                 :                :     /* in OpenLDAP, use the LDAP_OPT_NETWORK_TIMEOUT option */
 4262 magnus@hagander.net      5672         [ -  + ]:CBC           1 :     if (ldap_set_option(ld, LDAP_OPT_NETWORK_TIMEOUT, &time) != LDAP_SUCCESS)
                               5673                 :                :     {
 4262 magnus@hagander.net      5674                 :UBC           0 :         free(url);
                               5675                 :              0 :         ldap_unbind(ld);
                               5676                 :              0 :         return 3;
                               5677                 :                :     }
                               5678                 :                : 
                               5679                 :                :     /* anonymous bind */
 7082 bruce@momjian.us         5680         [ +  - ]:CBC           1 :     if ((msgid = ldap_simple_bind(ld, NULL, NULL)) == -1)
                               5681                 :                :     {
                               5682                 :                :         /* error or network timeout */
                               5683                 :              1 :         free(url);
                               5684                 :              1 :         ldap_unbind(ld);
                               5685                 :              1 :         return 2;
                               5686                 :                :     }
                               5687                 :                : 
                               5688                 :                :     /* wait some time for the connection to succeed */
 7082 bruce@momjian.us         5689                 :UBC           0 :     res = NULL;
                               5690         [ #  # ]:              0 :     if ((rc = ldap_result(ld, msgid, LDAP_MSG_ALL, &time, &res)) == -1 ||
                               5691         [ #  # ]:              0 :         res == NULL)
                               5692                 :                :     {
                               5693                 :                :         /* error or timeout */
                               5694         [ #  # ]:              0 :         if (res != NULL)
                               5695                 :              0 :             ldap_msgfree(res);
                               5696                 :              0 :         free(url);
                               5697                 :              0 :         ldap_unbind(ld);
                               5698                 :              0 :         return 2;
                               5699                 :                :     }
                               5700                 :              0 :     ldap_msgfree(res);
                               5701                 :                : 
                               5702                 :                :     /* reset timeout */
 4262 magnus@hagander.net      5703                 :              0 :     time.tv_sec = -1;
                               5704         [ #  # ]:              0 :     if (ldap_set_option(ld, LDAP_OPT_NETWORK_TIMEOUT, &time) != LDAP_SUCCESS)
                               5705                 :                :     {
                               5706                 :              0 :         free(url);
                               5707                 :              0 :         ldap_unbind(ld);
                               5708                 :              0 :         return 3;
                               5709                 :                :     }
                               5710                 :                : #endif                          /* WIN32 */
                               5711                 :                : 
                               5712                 :                :     /* search */
 7082 bruce@momjian.us         5713                 :              0 :     res = NULL;
                               5714         [ #  # ]:              0 :     if ((rc = ldap_search_st(ld, dn, scope, filter, attrs, 0, &time, &res))
                               5715                 :                :         != LDAP_SUCCESS)
                               5716                 :                :     {
                               5717         [ #  # ]:              0 :         if (res != NULL)
                               5718                 :              0 :             ldap_msgfree(res);
 1127 peter@eisentraut.org     5719                 :              0 :         libpq_append_error(errorMessage, "lookup on LDAP server failed: %s", ldap_err2string(rc));
 7082 bruce@momjian.us         5720                 :              0 :         ldap_unbind(ld);
                               5721                 :              0 :         free(url);
                               5722                 :              0 :         return 1;
                               5723                 :                :     }
                               5724                 :                : 
                               5725                 :                :     /* complain if there was not exactly one result */
                               5726         [ #  # ]:              0 :     if ((rc = ldap_count_entries(ld, res)) != 1)
                               5727                 :                :     {
 1127 peter@eisentraut.org     5728         [ #  # ]:              0 :         if (rc > 1)
                               5729                 :              0 :             libpq_append_error(errorMessage, "more than one entry found on LDAP lookup");
                               5730                 :                :         else
                               5731                 :              0 :             libpq_append_error(errorMessage, "no entry found on LDAP lookup");
 7082 bruce@momjian.us         5732                 :              0 :         ldap_msgfree(res);
                               5733                 :              0 :         ldap_unbind(ld);
                               5734                 :              0 :         free(url);
                               5735                 :              0 :         return 1;
                               5736                 :                :     }
                               5737                 :                : 
                               5738                 :                :     /* get entry */
                               5739         [ #  # ]:              0 :     if ((entry = ldap_first_entry(ld, res)) == NULL)
                               5740                 :                :     {
                               5741                 :                :         /* should never happen */
 1127 peter@eisentraut.org     5742                 :              0 :         libpq_append_error(errorMessage, "no entry found on LDAP lookup");
 7082 bruce@momjian.us         5743                 :              0 :         ldap_msgfree(res);
                               5744                 :              0 :         ldap_unbind(ld);
                               5745                 :              0 :         free(url);
                               5746                 :              0 :         return 1;
                               5747                 :                :     }
                               5748                 :                : 
                               5749                 :                :     /* get values */
                               5750         [ #  # ]:              0 :     if ((values = ldap_get_values_len(ld, entry, attrs[0])) == NULL)
                               5751                 :                :     {
 1127 peter@eisentraut.org     5752                 :              0 :         libpq_append_error(errorMessage, "attribute has no values on LDAP lookup");
 7082 bruce@momjian.us         5753                 :              0 :         ldap_msgfree(res);
                               5754                 :              0 :         ldap_unbind(ld);
                               5755                 :              0 :         free(url);
                               5756                 :              0 :         return 1;
                               5757                 :                :     }
                               5758                 :                : 
                               5759                 :              0 :     ldap_msgfree(res);
                               5760                 :              0 :     free(url);
                               5761                 :                : 
                               5762         [ #  # ]:              0 :     if (values[0] == NULL)
                               5763                 :                :     {
 1127 peter@eisentraut.org     5764                 :              0 :         libpq_append_error(errorMessage, "attribute has no values on LDAP lookup");
 7082 bruce@momjian.us         5765                 :              0 :         ldap_value_free_len(values);
                               5766                 :              0 :         ldap_unbind(ld);
                               5767                 :              0 :         return 1;
                               5768                 :                :     }
                               5769                 :                : 
                               5770                 :                :     /* concatenate values into a single string with newline terminators */
 5332 tgl@sss.pgh.pa.us        5771                 :              0 :     size = 1;                   /* for the trailing null */
                               5772         [ #  # ]:              0 :     for (i = 0; values[i] != NULL; i++)
                               5773                 :                :     {
   36 jchampion@postgresql     5774         [ #  # ]:              0 :         if (values[i]->bv_len >= INT_MAX ||
                               5775         [ #  # ]:              0 :             size > (INT_MAX - (values[i]->bv_len + 1)))
                               5776                 :                :         {
                               5777                 :              0 :             libpq_append_error(errorMessage,
                               5778                 :                :                                "connection info string size exceeds the maximum allowed (%d)",
                               5779                 :                :                                INT_MAX);
                               5780                 :              0 :             ldap_value_free_len(values);
                               5781                 :              0 :             ldap_unbind(ld);
                               5782                 :              0 :             return 3;
                               5783                 :                :         }
                               5784                 :                : 
 7082 bruce@momjian.us         5785                 :              0 :         size += values[i]->bv_len + 1;
                               5786                 :                :     }
                               5787                 :                : 
 5332 tgl@sss.pgh.pa.us        5788         [ #  # ]:              0 :     if ((result = malloc(size)) == NULL)
                               5789                 :                :     {
 1127 peter@eisentraut.org     5790                 :              0 :         libpq_append_error(errorMessage, "out of memory");
 7082 bruce@momjian.us         5791                 :              0 :         ldap_value_free_len(values);
                               5792                 :              0 :         ldap_unbind(ld);
                               5793                 :              0 :         return 3;
                               5794                 :                :     }
 5332 tgl@sss.pgh.pa.us        5795                 :              0 :     p = result;
                               5796         [ #  # ]:              0 :     for (i = 0; values[i] != NULL; i++)
                               5797                 :                :     {
                               5798                 :              0 :         memcpy(p, values[i]->bv_val, values[i]->bv_len);
 7082 bruce@momjian.us         5799                 :              0 :         p += values[i]->bv_len;
                               5800                 :              0 :         *(p++) = '\n';
                               5801                 :                :     }
 5332 tgl@sss.pgh.pa.us        5802                 :              0 :     *p = '\0';
                               5803                 :                : 
 7082 bruce@momjian.us         5804                 :              0 :     ldap_value_free_len(values);
                               5805                 :              0 :     ldap_unbind(ld);
                               5806                 :                : 
                               5807                 :                :     /* parse result string */
                               5808                 :              0 :     oldstate = state = 0;
                               5809         [ #  # ]:              0 :     for (p = result; *p != '\0'; ++p)
                               5810                 :                :     {
                               5811   [ #  #  #  #  :              0 :         switch (state)
                                        #  #  #  # ]
                               5812                 :                :         {
                               5813                 :              0 :             case 0:             /* between entries */
                               5814   [ #  #  #  #  :              0 :                 if (!ld_is_sp_tab(*p) && !ld_is_nl_cr(*p))
                                        #  #  #  # ]
                               5815                 :                :                 {
                               5816                 :              0 :                     optname = p;
                               5817                 :              0 :                     state = 1;
                               5818                 :                :                 }
                               5819                 :              0 :                 break;
                               5820                 :              0 :             case 1:             /* in option name */
                               5821   [ #  #  #  # ]:              0 :                 if (ld_is_sp_tab(*p))
                               5822                 :                :                 {
                               5823                 :              0 :                     *p = '\0';
                               5824                 :              0 :                     state = 2;
                               5825                 :                :                 }
                               5826   [ #  #  #  # ]:              0 :                 else if (ld_is_nl_cr(*p))
                               5827                 :                :                 {
 1127 peter@eisentraut.org     5828                 :              0 :                     libpq_append_error(errorMessage,
                               5829                 :                :                                        "missing \"=\" after \"%s\" in connection info string",
                               5830                 :                :                                        optname);
 5332 tgl@sss.pgh.pa.us        5831                 :              0 :                     free(result);
 7082 bruce@momjian.us         5832                 :              0 :                     return 3;
                               5833                 :                :                 }
                               5834         [ #  # ]:              0 :                 else if (*p == '=')
                               5835                 :                :                 {
                               5836                 :              0 :                     *p = '\0';
                               5837                 :              0 :                     state = 3;
                               5838                 :                :                 }
                               5839                 :              0 :                 break;
                               5840                 :              0 :             case 2:             /* after option name */
                               5841         [ #  # ]:              0 :                 if (*p == '=')
                               5842                 :                :                 {
                               5843                 :              0 :                     state = 3;
                               5844                 :                :                 }
                               5845   [ #  #  #  # ]:              0 :                 else if (!ld_is_sp_tab(*p))
                               5846                 :                :                 {
 1127 peter@eisentraut.org     5847                 :              0 :                     libpq_append_error(errorMessage,
                               5848                 :                :                                        "missing \"=\" after \"%s\" in connection info string",
                               5849                 :                :                                        optname);
 5332 tgl@sss.pgh.pa.us        5850                 :              0 :                     free(result);
 7082 bruce@momjian.us         5851                 :              0 :                     return 3;
                               5852                 :                :                 }
                               5853                 :              0 :                 break;
                               5854                 :              0 :             case 3:             /* before option value */
                               5855         [ #  # ]:              0 :                 if (*p == '\'')
                               5856                 :                :                 {
                               5857                 :              0 :                     optval = p + 1;
                               5858                 :              0 :                     p1 = p + 1;
                               5859                 :              0 :                     state = 5;
                               5860                 :                :                 }
                               5861   [ #  #  #  # ]:              0 :                 else if (ld_is_nl_cr(*p))
                               5862                 :                :                 {
                               5863                 :              0 :                     optval = optname + strlen(optname); /* empty */
                               5864                 :              0 :                     state = 0;
                               5865                 :                :                 }
                               5866   [ #  #  #  # ]:              0 :                 else if (!ld_is_sp_tab(*p))
                               5867                 :                :                 {
                               5868                 :              0 :                     optval = p;
                               5869                 :              0 :                     state = 4;
                               5870                 :                :                 }
                               5871                 :              0 :                 break;
                               5872                 :              0 :             case 4:             /* in unquoted option value */
                               5873   [ #  #  #  #  :              0 :                 if (ld_is_sp_tab(*p) || ld_is_nl_cr(*p))
                                        #  #  #  # ]
                               5874                 :                :                 {
                               5875                 :              0 :                     *p = '\0';
                               5876                 :              0 :                     state = 0;
                               5877                 :                :                 }
                               5878                 :              0 :                 break;
                               5879                 :              0 :             case 5:             /* in quoted option value */
                               5880         [ #  # ]:              0 :                 if (*p == '\'')
                               5881                 :                :                 {
                               5882                 :              0 :                     *p1 = '\0';
                               5883                 :              0 :                     state = 0;
                               5884                 :                :                 }
                               5885         [ #  # ]:              0 :                 else if (*p == '\\')
                               5886                 :              0 :                     state = 6;
                               5887                 :                :                 else
                               5888                 :              0 :                     *(p1++) = *p;
                               5889                 :              0 :                 break;
                               5890                 :              0 :             case 6:             /* in quoted option value after escape */
                               5891                 :              0 :                 *(p1++) = *p;
                               5892                 :              0 :                 state = 5;
                               5893                 :              0 :                 break;
                               5894                 :                :         }
                               5895                 :                : 
                               5896   [ #  #  #  # ]:              0 :         if (state == 0 && oldstate != 0)
                               5897                 :                :         {
                               5898                 :              0 :             found_keyword = false;
                               5899         [ #  # ]:              0 :             for (i = 0; options[i].keyword; i++)
                               5900                 :                :             {
                               5901         [ #  # ]:              0 :                 if (strcmp(options[i].keyword, optname) == 0)
                               5902                 :                :                 {
                               5903         [ #  # ]:              0 :                     if (options[i].val == NULL)
                               5904                 :                :                     {
                               5905                 :              0 :                         options[i].val = strdup(optval);
 4039 heikki.linnakangas@i     5906         [ #  # ]:              0 :                         if (!options[i].val)
                               5907                 :                :                         {
 1127 peter@eisentraut.org     5908                 :              0 :                             libpq_append_error(errorMessage, "out of memory");
 4039 heikki.linnakangas@i     5909                 :              0 :                             free(result);
                               5910                 :              0 :                             return 3;
                               5911                 :                :                         }
                               5912                 :                :                     }
 7082 bruce@momjian.us         5913                 :              0 :                     found_keyword = true;
                               5914                 :              0 :                     break;
                               5915                 :                :                 }
                               5916                 :                :             }
                               5917         [ #  # ]:              0 :             if (!found_keyword)
                               5918                 :                :             {
 1127 peter@eisentraut.org     5919                 :              0 :                 libpq_append_error(errorMessage, "invalid connection option \"%s\"", optname);
 5332 tgl@sss.pgh.pa.us        5920                 :              0 :                 free(result);
 7082 bruce@momjian.us         5921                 :              0 :                 return 1;
                               5922                 :                :             }
                               5923                 :              0 :             optname = NULL;
                               5924                 :              0 :             optval = NULL;
                               5925                 :                :         }
                               5926                 :              0 :         oldstate = state;
                               5927                 :                :     }
                               5928                 :                : 
 5332 tgl@sss.pgh.pa.us        5929                 :              0 :     free(result);
                               5930                 :                : 
 7082 bruce@momjian.us         5931   [ #  #  #  # ]:              0 :     if (state == 5 || state == 6)
                               5932                 :                :     {
 1127 peter@eisentraut.org     5933                 :              0 :         libpq_append_error(errorMessage,
                               5934                 :                :                            "unterminated quoted string in connection info string");
 7082 bruce@momjian.us         5935                 :              0 :         return 3;
                               5936                 :                :     }
                               5937                 :                : 
                               5938                 :              0 :     return 0;
                               5939                 :                : }
                               5940                 :                : 
                               5941                 :                : #endif                          /* USE_LDAP */
                               5942                 :                : 
                               5943                 :                : /*
                               5944                 :                :  * parseServiceInfo: if a service name has been given, look it up and absorb
                               5945                 :                :  * connection options from it into *options.
                               5946                 :                :  *
                               5947                 :                :  * Returns 0 on success, nonzero on failure.  On failure, if errorMessage
                               5948                 :                :  * isn't null, also store an error message there.  (Note: the only reason
                               5949                 :                :  * this function and related ones don't dump core on errorMessage == NULL
                               5950                 :                :  * is the undocumented fact that appendPQExpBuffer does nothing when passed
                               5951                 :                :  * a null PQExpBuffer pointer.)
                               5952                 :                :  */
                               5953                 :                : static int
 9140 tgl@sss.pgh.pa.us        5954                 :CBC       13708 : parseServiceInfo(PQconninfoOption *options, PQExpBuffer errorMessage)
                               5955                 :                : {
 4997 alvherre@alvh.no-ip.     5956                 :          13708 :     const char *service = conninfo_getval(options, "service");
  156 michael@paquier.xyz      5957                 :GNC       13708 :     const char *service_fname = conninfo_getval(options, "servicefile");
                               5958                 :                :     char        serviceFile[MAXPGPATH];
                               5959                 :                :     char       *env;
 8033 bruce@momjian.us         5960                 :CBC       13708 :     bool        group_found = false;
                               5961                 :                :     int         status;
                               5962                 :                :     struct stat stat_buf;
                               5963                 :                : 
                               5964                 :                :     /*
                               5965                 :                :      * We have to special-case the environment variable PGSERVICE here, since
                               5966                 :                :      * this is and should be called before inserting environment defaults for
                               5967                 :                :      * other connection options.
                               5968                 :                :      */
 8268 tgl@sss.pgh.pa.us        5969         [ +  + ]:          13708 :     if (service == NULL)
                               5970                 :          13694 :         service = getenv("PGSERVICE");
                               5971                 :                : 
                               5972                 :                :     /* If no service name given, nothing to do */
 5809 peter_e@gmx.net          5973         [ +  + ]:          13708 :     if (service == NULL)
                               5974                 :          13688 :         return 0;
                               5975                 :                : 
                               5976                 :                :     /*
                               5977                 :                :      * First, try the "servicefile" option in connection string.  Then, try
                               5978                 :                :      * the PGSERVICEFILE environment variable.  Finally, check
                               5979                 :                :      * ~/.pg_service.conf (if that exists).
                               5980                 :                :      */
  156 michael@paquier.xyz      5981         [ +  + ]:GNC          20 :     if (service_fname != NULL)
                               5982                 :              5 :         strlcpy(serviceFile, service_fname, sizeof(serviceFile));
                               5983         [ +  - ]:             15 :     else if ((env = getenv("PGSERVICEFILE")) != NULL)
 5809 peter_e@gmx.net          5984                 :CBC          15 :         strlcpy(serviceFile, env, sizeof(serviceFile));
                               5985                 :                :     else
                               5986                 :                :     {
                               5987                 :                :         char        homedir[MAXPGPATH];
                               5988                 :                : 
 5809 peter_e@gmx.net          5989         [ #  # ]:UBC           0 :         if (!pqGetHomeDirectory(homedir, sizeof(homedir)))
 2974 tgl@sss.pgh.pa.us        5990                 :              0 :             goto next_file;
 5809 peter_e@gmx.net          5991                 :              0 :         snprintf(serviceFile, MAXPGPATH, "%s/%s", homedir, ".pg_service.conf");
 2974 tgl@sss.pgh.pa.us        5992         [ #  # ]:              0 :         if (stat(serviceFile, &stat_buf) != 0)
 5809 peter_e@gmx.net          5993                 :              0 :             goto next_file;
                               5994                 :                :     }
                               5995                 :                : 
 5809 peter_e@gmx.net          5996                 :CBC          20 :     status = parseServiceFile(serviceFile, service, options, errorMessage, &group_found);
                               5997   [ +  +  +  + ]:             20 :     if (group_found || status != 0)
                               5998                 :             13 :         return status;
                               5999                 :                : 
                               6000                 :              7 : next_file:
                               6001                 :                : 
                               6002                 :                :     /*
                               6003                 :                :      * This could be used by any application so we can't use the binary
                               6004                 :                :      * location to find our config files.
                               6005                 :                :      */
 7866 bruce@momjian.us         6006         [ +  - ]:             14 :     snprintf(serviceFile, MAXPGPATH, "%s/pg_service.conf",
 7492 neilc@samurai.com        6007                 :             14 :              getenv("PGSYSCONFDIR") ? getenv("PGSYSCONFDIR") : SYSCONFDIR);
 2974 tgl@sss.pgh.pa.us        6008         [ +  + ]:              7 :     if (stat(serviceFile, &stat_buf) != 0)
 5809 peter_e@gmx.net          6009                 :              2 :         goto last_file;
                               6010                 :                : 
                               6011                 :              5 :     status = parseServiceFile(serviceFile, service, options, errorMessage, &group_found);
                               6012         [ -  + ]:              5 :     if (status != 0)
 5809 peter_e@gmx.net          6013                 :UBC           0 :         return status;
                               6014                 :                : 
 5809 peter_e@gmx.net          6015                 :CBC           5 : last_file:
                               6016         [ +  + ]:              7 :     if (!group_found)
                               6017                 :                :     {
 1127 peter@eisentraut.org     6018                 :              4 :         libpq_append_error(errorMessage, "definition of service \"%s\" not found", service);
 5809 peter_e@gmx.net          6019                 :              4 :         return 3;
                               6020                 :                :     }
                               6021                 :                : 
                               6022                 :              3 :     return 0;
                               6023                 :                : }
                               6024                 :                : 
                               6025                 :                : static int
                               6026                 :             25 : parseServiceFile(const char *serviceFile,
                               6027                 :                :                  const char *service,
                               6028                 :                :                  PQconninfoOption *options,
                               6029                 :                :                  PQExpBuffer errorMessage,
                               6030                 :                :                  bool *group_found)
                               6031                 :                : {
 1911 tgl@sss.pgh.pa.us        6032                 :             25 :     int         result = 0,
                               6033                 :             25 :                 linenr = 0,
                               6034                 :                :                 i;
                               6035                 :                :     FILE       *f;
                               6036                 :                :     char       *line;
                               6037                 :                :     char        buf[1024];
                               6038                 :                : 
                               6039                 :             25 :     *group_found = false;
                               6040                 :                : 
 5809 peter_e@gmx.net          6041                 :             25 :     f = fopen(serviceFile, "r");
                               6042         [ +  + ]:             25 :     if (f == NULL)
                               6043                 :                :     {
 1127 peter@eisentraut.org     6044                 :              2 :         libpq_append_error(errorMessage, "service file \"%s\" not found", serviceFile);
 5809 peter_e@gmx.net          6045                 :              2 :         return 1;
                               6046                 :                :     }
                               6047                 :                : 
 1634 tgl@sss.pgh.pa.us        6048         [ +  + ]:             81 :     while ((line = fgets(buf, sizeof(buf), f)) != NULL)
                               6049                 :                :     {
                               6050                 :                :         int         len;
                               6051                 :                : 
 5809 peter_e@gmx.net          6052                 :             60 :         linenr++;
                               6053                 :                : 
 1634 tgl@sss.pgh.pa.us        6054         [ -  + ]:             60 :         if (strlen(line) >= sizeof(buf) - 1)
                               6055                 :                :         {
 1127 peter@eisentraut.org     6056                 :UBC           0 :             libpq_append_error(errorMessage,
                               6057                 :                :                                "line %d too long in service file \"%s\"",
                               6058                 :                :                                linenr,
                               6059                 :                :                                serviceFile);
 1634 tgl@sss.pgh.pa.us        6060                 :              0 :             result = 2;
                               6061                 :              0 :             goto exit;
                               6062                 :                :         }
                               6063                 :                : 
                               6064                 :                :         /* ignore whitespace at end of line, especially the newline */
 1634 tgl@sss.pgh.pa.us        6065                 :CBC          60 :         len = strlen(line);
                               6066   [ +  +  +  + ]:            120 :         while (len > 0 && isspace((unsigned char) line[len - 1]))
                               6067                 :             60 :             line[--len] = '\0';
                               6068                 :                : 
                               6069                 :                :         /* ignore leading whitespace too */
 5809 peter_e@gmx.net          6070   [ +  +  -  + ]:             60 :         while (*line && isspace((unsigned char) line[0]))
 5809 peter_e@gmx.net          6071                 :UBC           0 :             line++;
                               6072                 :                : 
                               6073                 :                :         /* ignore comments and empty lines */
 2336 tgl@sss.pgh.pa.us        6074   [ +  +  +  + ]:CBC          60 :         if (line[0] == '\0' || line[0] == '#')
 5809 peter_e@gmx.net          6075                 :              5 :             continue;
                               6076                 :                : 
                               6077                 :                :         /* Check for right groupname */
                               6078         [ +  + ]:             55 :         if (line[0] == '[')
                               6079                 :                :         {
                               6080         [ -  + ]:             18 :             if (*group_found)
                               6081                 :                :             {
                               6082                 :                :                 /* end of desired group reached; return success */
 1911 tgl@sss.pgh.pa.us        6083                 :UBC           0 :                 goto exit;
                               6084                 :                :             }
                               6085                 :                : 
 5809 peter_e@gmx.net          6086         [ +  + ]:CBC          18 :             if (strncmp(line + 1, service, strlen(service)) == 0 &&
                               6087         [ +  - ]:             14 :                 line[strlen(service) + 1] == ']')
                               6088                 :             14 :                 *group_found = true;
                               6089                 :                :             else
                               6090                 :              4 :                 *group_found = false;
                               6091                 :                :         }
                               6092                 :                :         else
                               6093                 :                :         {
                               6094         [ +  + ]:             37 :             if (*group_found)
                               6095                 :                :             {
                               6096                 :                :                 /*
                               6097                 :                :                  * Finally, we are in the right group and can parse the line
                               6098                 :                :                  */
                               6099                 :                :                 char       *key,
                               6100                 :                :                            *val;
                               6101                 :                :                 bool        found_keyword;
                               6102                 :                : 
                               6103                 :                : #ifdef USE_LDAP
                               6104         [ +  + ]:             29 :                 if (strncmp(line, "ldap", 4) == 0)
                               6105                 :                :                 {
                               6106                 :              1 :                     int         rc = ldapServiceLookup(line, options, errorMessage);
                               6107                 :                : 
                               6108                 :                :                     /* if rc = 2, go on reading for fallback */
                               6109   [ -  -  +  - ]:              1 :                     switch (rc)
                               6110                 :                :                     {
 5809 peter_e@gmx.net          6111                 :UBC           0 :                         case 0:
 1911 tgl@sss.pgh.pa.us        6112                 :              0 :                             goto exit;
 5809 peter_e@gmx.net          6113                 :              0 :                         case 1:
                               6114                 :                :                         case 3:
 1911 tgl@sss.pgh.pa.us        6115                 :              0 :                             result = 3;
                               6116                 :              0 :                             goto exit;
 5809 peter_e@gmx.net          6117                 :CBC           1 :                         case 2:
                               6118                 :              1 :                             continue;
                               6119                 :                :                     }
                               6120                 :                :                 }
                               6121                 :                : #endif
                               6122                 :                : 
                               6123                 :             28 :                 key = line;
                               6124                 :             28 :                 val = strchr(line, '=');
                               6125         [ -  + ]:             28 :                 if (val == NULL)
                               6126                 :                :                 {
 1127 peter@eisentraut.org     6127                 :UBC           0 :                     libpq_append_error(errorMessage,
                               6128                 :                :                                        "syntax error in service file \"%s\", line %d",
                               6129                 :                :                                        serviceFile,
                               6130                 :                :                                        linenr);
 1911 tgl@sss.pgh.pa.us        6131                 :              0 :                     result = 3;
                               6132                 :              0 :                     goto exit;
                               6133                 :                :                 }
 5809 peter_e@gmx.net          6134                 :CBC          28 :                 *val++ = '\0';
                               6135                 :                : 
 3905 bruce@momjian.us         6136         [ +  + ]:             28 :                 if (strcmp(key, "service") == 0)
                               6137                 :                :                 {
 1127 peter@eisentraut.org     6138                 :GBC           1 :                     libpq_append_error(errorMessage,
                               6139                 :                :                                        "nested \"service\" specifications not supported in service file \"%s\", line %d",
                               6140                 :                :                                        serviceFile,
                               6141                 :                :                                        linenr);
  156 michael@paquier.xyz      6142                 :GNC           1 :                     result = 3;
                               6143                 :              1 :                     goto exit;
                               6144                 :                :                 }
                               6145                 :                : 
                               6146         [ +  + ]:             27 :                 if (strcmp(key, "servicefile") == 0)
                               6147                 :                :                 {
                               6148                 :              1 :                     libpq_append_error(errorMessage,
                               6149                 :                :                                        "nested \"servicefile\" specifications not supported in service file \"%s\", line %d",
                               6150                 :                :                                        serviceFile,
                               6151                 :                :                                        linenr);
 1911 tgl@sss.pgh.pa.us        6152                 :GBC           1 :                     result = 3;
                               6153                 :              1 :                     goto exit;
                               6154                 :                :                 }
                               6155                 :                : 
                               6156                 :                :                 /*
                               6157                 :                :                  * Set the parameter --- but don't override any previous
                               6158                 :                :                  * explicit setting.
                               6159                 :                :                  */
 5809 peter_e@gmx.net          6160                 :CBC          26 :                 found_keyword = false;
                               6161         [ +  - ]:            260 :                 for (i = 0; options[i].keyword; i++)
                               6162                 :                :                 {
                               6163         [ +  + ]:            260 :                     if (strcmp(options[i].keyword, key) == 0)
                               6164                 :                :                     {
                               6165         [ +  - ]:             26 :                         if (options[i].val == NULL)
                               6166                 :             26 :                             options[i].val = strdup(val);
 4039 heikki.linnakangas@i     6167         [ -  + ]:             26 :                         if (!options[i].val)
                               6168                 :                :                         {
 1127 peter@eisentraut.org     6169                 :UBC           0 :                             libpq_append_error(errorMessage, "out of memory");
 1911 tgl@sss.pgh.pa.us        6170                 :              0 :                             result = 3;
                               6171                 :              0 :                             goto exit;
                               6172                 :                :                         }
 5809 peter_e@gmx.net          6173                 :CBC          26 :                         found_keyword = true;
                               6174                 :             26 :                         break;
                               6175                 :                :                     }
                               6176                 :                :                 }
                               6177                 :                : 
                               6178         [ -  + ]:             26 :                 if (!found_keyword)
                               6179                 :                :                 {
 1127 peter@eisentraut.org     6180                 :UBC           0 :                     libpq_append_error(errorMessage,
                               6181                 :                :                                        "syntax error in service file \"%s\", line %d",
                               6182                 :                :                                        serviceFile,
                               6183                 :                :                                        linenr);
 1911 tgl@sss.pgh.pa.us        6184                 :              0 :                     result = 3;
                               6185                 :              0 :                     goto exit;
                               6186                 :                :                 }
                               6187                 :                :             }
                               6188                 :                :         }
                               6189                 :                :     }
                               6190                 :                : 
 1911 tgl@sss.pgh.pa.us        6191                 :CBC          21 : exit:
                               6192                 :                : 
                               6193                 :                :     /*
                               6194                 :                :      * If a service has been successfully found, set the "servicefile" option
                               6195                 :                :      * if not already set.  This matters when we use a default service file or
                               6196                 :                :      * PGSERVICEFILE, where we want to be able track the value.
                               6197                 :                :      */
  156 michael@paquier.xyz      6198   [ +  +  +  + ]:GNC          23 :     if (*group_found && result == 0)
                               6199                 :                :     {
                               6200         [ +  - ]:             24 :         for (i = 0; options[i].keyword; i++)
                               6201                 :                :         {
                               6202         [ +  + ]:             24 :             if (strcmp(options[i].keyword, "servicefile") != 0)
                               6203                 :             12 :                 continue;
                               6204                 :                : 
                               6205                 :                :             /* If value is already set, nothing to do */
                               6206         [ +  + ]:             12 :             if (options[i].val != NULL)
                               6207                 :              5 :                 break;
                               6208                 :                : 
                               6209                 :              7 :             options[i].val = strdup(serviceFile);
                               6210         [ -  + ]:              7 :             if (options[i].val == NULL)
                               6211                 :                :             {
  156 michael@paquier.xyz      6212                 :UNC           0 :                 libpq_append_error(errorMessage, "out of memory");
                               6213                 :              0 :                 result = 3;
                               6214                 :                :             }
  156 michael@paquier.xyz      6215                 :GNC           7 :             break;
                               6216                 :                :         }
                               6217                 :                :     }
                               6218                 :                : 
 5809 peter_e@gmx.net          6219                 :CBC          23 :     fclose(f);
                               6220                 :                : 
 1911 tgl@sss.pgh.pa.us        6221                 :             23 :     return result;
                               6222                 :                : }
                               6223                 :                : 
                               6224                 :                : 
                               6225                 :                : /*
                               6226                 :                :  *      PQconninfoParse
                               6227                 :                :  *
                               6228                 :                :  * Parse a string like PQconnectdb() would do and return the
                               6229                 :                :  * resulting connection options array.  NULL is returned on failure.
                               6230                 :                :  * The result contains only options specified directly in the string,
                               6231                 :                :  * not any possible default values.
                               6232                 :                :  *
                               6233                 :                :  * If errmsg isn't NULL, *errmsg is set to NULL on success, or a malloc'd
                               6234                 :                :  * string on failure (use PQfreemem to free it).  In out-of-memory conditions
                               6235                 :                :  * both *errmsg and the result could be NULL.
                               6236                 :                :  *
                               6237                 :                :  * NOTE: the returned array is dynamically allocated and should
                               6238                 :                :  * be freed when no longer needed via PQconninfoFree().
                               6239                 :                :  */
                               6240                 :                : PQconninfoOption *
 6294                          6241                 :           1279 : PQconninfoParse(const char *conninfo, char **errmsg)
                               6242                 :                : {
                               6243                 :                :     PQExpBufferData errorBuf;
                               6244                 :                :     PQconninfoOption *connOptions;
                               6245                 :                : 
                               6246         [ +  + ]:           1279 :     if (errmsg)
                               6247                 :           1262 :         *errmsg = NULL;         /* default */
                               6248                 :           1279 :     initPQExpBuffer(&errorBuf);
 5173                          6249         [ -  + ]:           1279 :     if (PQExpBufferDataBroken(errorBuf))
 6294 tgl@sss.pgh.pa.us        6250                 :UBC           0 :         return NULL;            /* out of memory already :-( */
 4997 alvherre@alvh.no-ip.     6251                 :CBC        1279 :     connOptions = parse_connection_string(conninfo, &errorBuf, false);
 6294 tgl@sss.pgh.pa.us        6252   [ +  +  +  - ]:           1279 :     if (connOptions == NULL && errmsg)
                               6253                 :             25 :         *errmsg = errorBuf.data;
                               6254                 :                :     else
                               6255                 :           1254 :         termPQExpBuffer(&errorBuf);
                               6256                 :           1279 :     return connOptions;
                               6257                 :                : }
                               6258                 :                : 
                               6259                 :                : /*
                               6260                 :                :  * Build a working copy of the constant PQconninfoOptions array.
                               6261                 :                :  */
                               6262                 :                : static PQconninfoOption *
 5017                          6263                 :          41487 : conninfo_init(PQExpBuffer errorMessage)
                               6264                 :                : {
                               6265                 :                :     PQconninfoOption *options;
                               6266                 :                :     PQconninfoOption *opt_dest;
                               6267                 :                :     const internalPQconninfoOption *cur_opt;
                               6268                 :                : 
                               6269                 :                :     /*
                               6270                 :                :      * Get enough memory for all options in PQconninfoOptions, even if some
                               6271                 :                :      * end up being filtered out.
                               6272                 :                :      */
 4764 magnus@hagander.net      6273                 :          41487 :     options = (PQconninfoOption *) malloc(sizeof(PQconninfoOption) * sizeof(PQconninfoOptions) / sizeof(PQconninfoOptions[0]));
 5017 tgl@sss.pgh.pa.us        6274         [ -  + ]:          41487 :     if (options == NULL)
                               6275                 :                :     {
 1127 peter@eisentraut.org     6276                 :UBC           0 :         libpq_append_error(errorMessage, "out of memory");
 5017 tgl@sss.pgh.pa.us        6277                 :              0 :         return NULL;
                               6278                 :                :     }
 4764 magnus@hagander.net      6279                 :CBC       41487 :     opt_dest = options;
                               6280                 :                : 
                               6281         [ +  + ]:        2157324 :     for (cur_opt = PQconninfoOptions; cur_opt->keyword; cur_opt++)
                               6282                 :                :     {
                               6283                 :                :         /* Only copy the public part of the struct, not the full internal */
                               6284                 :        2115837 :         memcpy(opt_dest, cur_opt, sizeof(PQconninfoOption));
                               6285                 :        2115837 :         opt_dest++;
                               6286                 :                :     }
                               6287   [ +  -  +  -  :         331896 :     MemSet(opt_dest, 0, sizeof(PQconninfoOption));
                                     +  -  +  -  +  
                                                 + ]
                               6288                 :                : 
 5017 tgl@sss.pgh.pa.us        6289                 :          41487 :     return options;
                               6290                 :                : }
                               6291                 :                : 
                               6292                 :                : /*
                               6293                 :                :  * Connection string parser
                               6294                 :                :  *
                               6295                 :                :  * Returns a malloc'd PQconninfoOption array, if parsing is successful.
                               6296                 :                :  * Otherwise, NULL is returned and an error message is added to errorMessage.
                               6297                 :                :  *
                               6298                 :                :  * If use_defaults is true, default values are filled in (from a service file,
                               6299                 :                :  * environment variables, etc).
                               6300                 :                :  */
                               6301                 :                : static PQconninfoOption *
 4997 alvherre@alvh.no-ip.     6302                 :          11251 : parse_connection_string(const char *connstr, PQExpBuffer errorMessage,
                               6303                 :                :                         bool use_defaults)
                               6304                 :                : {
                               6305                 :                :     /* Parse as URI if connection string matches URI prefix */
 3911 rhaas@postgresql.org     6306         [ +  + ]:          11251 :     if (uri_prefix_length(connstr) != 0)
 4997 alvherre@alvh.no-ip.     6307                 :             66 :         return conninfo_uri_parse(connstr, errorMessage, use_defaults);
                               6308                 :                : 
                               6309                 :                :     /* Parse as default otherwise */
                               6310                 :          11185 :     return conninfo_parse(connstr, errorMessage, use_defaults);
                               6311                 :                : }
                               6312                 :                : 
                               6313                 :                : /*
                               6314                 :                :  * Checks if connection string starts with either of the valid URI prefix
                               6315                 :                :  * designators.
                               6316                 :                :  *
                               6317                 :                :  * Returns the URI prefix length, 0 if the string doesn't contain a URI prefix.
                               6318                 :                :  *
                               6319                 :                :  * XXX this is duplicated in psql/common.c.
                               6320                 :                :  */
                               6321                 :                : static int
 3911 rhaas@postgresql.org     6322                 :          23246 : uri_prefix_length(const char *connstr)
                               6323                 :                : {
                               6324         [ +  + ]:          23246 :     if (strncmp(connstr, uri_designator,
                               6325                 :                :                 sizeof(uri_designator) - 1) == 0)
                               6326                 :             92 :         return sizeof(uri_designator) - 1;
                               6327                 :                : 
                               6328         [ +  + ]:          23154 :     if (strncmp(connstr, short_uri_designator,
                               6329                 :                :                 sizeof(short_uri_designator) - 1) == 0)
                               6330                 :             44 :         return sizeof(short_uri_designator) - 1;
                               6331                 :                : 
                               6332                 :          23110 :     return 0;
                               6333                 :                : }
                               6334                 :                : 
                               6335                 :                : /*
                               6336                 :                :  * Recognized connection string either starts with a valid URI prefix or
                               6337                 :                :  * contains a "=" in it.
                               6338                 :                :  *
                               6339                 :                :  * Must be consistent with parse_connection_string: anything for which this
                               6340                 :                :  * returns true should at least look like it's parseable by that routine.
                               6341                 :                :  *
                               6342                 :                :  * XXX this is duplicated in psql/common.c
                               6343                 :                :  */
                               6344                 :                : static bool
                               6345                 :          11929 : recognized_connection_string(const char *connstr)
                               6346                 :                : {
                               6347   [ +  +  +  + ]:          11929 :     return uri_prefix_length(connstr) != 0 || strchr(connstr, '=') != NULL;
                               6348                 :                : }
                               6349                 :                : 
                               6350                 :                : /*
                               6351                 :                :  * Subroutine for parse_connection_string
                               6352                 :                :  *
                               6353                 :                :  * Deal with a string containing key=value pairs.
                               6354                 :                :  */
                               6355                 :                : static PQconninfoOption *
 6582 tgl@sss.pgh.pa.us        6356                 :          11185 : conninfo_parse(const char *conninfo, PQExpBuffer errorMessage,
                               6357                 :                :                bool use_defaults)
                               6358                 :                : {
                               6359                 :                :     char       *pname;
                               6360                 :                :     char       *pval;
                               6361                 :                :     char       *buf;
                               6362                 :                :     char       *cp;
                               6363                 :                :     char       *cp2;
                               6364                 :                :     PQconninfoOption *options;
                               6365                 :                : 
                               6366                 :                :     /* Make a working copy of PQconninfoOptions */
 5017                          6367                 :          11185 :     options = conninfo_init(errorMessage);
 9411                          6368         [ -  + ]:          11185 :     if (options == NULL)
 9411 tgl@sss.pgh.pa.us        6369                 :UBC           0 :         return NULL;
                               6370                 :                : 
                               6371                 :                :     /* Need a modifiable copy of the input string */
10327 bruce@momjian.us         6372         [ -  + ]:CBC       11185 :     if ((buf = strdup(conninfo)) == NULL)
                               6373                 :                :     {
 1127 peter@eisentraut.org     6374                 :UBC           0 :         libpq_append_error(errorMessage, "out of memory");
 9411 tgl@sss.pgh.pa.us        6375                 :              0 :         PQconninfoFree(options);
                               6376                 :              0 :         return NULL;
                               6377                 :                :     }
10327 bruce@momjian.us         6378                 :CBC       11185 :     cp = buf;
                               6379                 :                : 
                               6380         [ +  + ]:          46784 :     while (*cp)
                               6381                 :                :     {
                               6382                 :                :         /* Skip blanks before the parameter name */
 9144 tgl@sss.pgh.pa.us        6383         [ +  + ]:          35611 :         if (isspace((unsigned char) *cp))
                               6384                 :                :         {
10327 bruce@momjian.us         6385                 :            420 :             cp++;
                               6386                 :            420 :             continue;
                               6387                 :                :         }
                               6388                 :                : 
                               6389                 :                :         /* Get the parameter name */
                               6390                 :          35191 :         pname = cp;
                               6391         [ +  + ]:         217650 :         while (*cp)
                               6392                 :                :         {
                               6393         [ +  + ]:         217641 :             if (*cp == '=')
                               6394                 :          35182 :                 break;
 9144 tgl@sss.pgh.pa.us        6395         [ -  + ]:         182459 :             if (isspace((unsigned char) *cp))
                               6396                 :                :             {
10327 bruce@momjian.us         6397                 :UBC           0 :                 *cp++ = '\0';
                               6398         [ #  # ]:              0 :                 while (*cp)
                               6399                 :                :                 {
 9144 tgl@sss.pgh.pa.us        6400         [ #  # ]:              0 :                     if (!isspace((unsigned char) *cp))
10327 bruce@momjian.us         6401                 :              0 :                         break;
                               6402                 :              0 :                     cp++;
                               6403                 :                :                 }
                               6404                 :              0 :                 break;
                               6405                 :                :             }
10327 bruce@momjian.us         6406                 :CBC      182459 :             cp++;
                               6407                 :                :         }
                               6408                 :                : 
                               6409                 :                :         /* Check that there is a following '=' */
                               6410         [ +  + ]:          35191 :         if (*cp != '=')
                               6411                 :                :         {
 1127 peter@eisentraut.org     6412                 :              9 :             libpq_append_error(errorMessage,
                               6413                 :                :                                "missing \"=\" after \"%s\" in connection info string",
                               6414                 :                :                                pname);
 9411 tgl@sss.pgh.pa.us        6415                 :              9 :             PQconninfoFree(options);
10327 bruce@momjian.us         6416                 :              9 :             free(buf);
 9411 tgl@sss.pgh.pa.us        6417                 :              9 :             return NULL;
                               6418                 :                :         }
10327 bruce@momjian.us         6419                 :          35182 :         *cp++ = '\0';
                               6420                 :                : 
                               6421                 :                :         /* Skip blanks after the '=' */
                               6422         [ +  + ]:          35182 :         while (*cp)
                               6423                 :                :         {
 9144 tgl@sss.pgh.pa.us        6424         [ +  - ]:          35167 :             if (!isspace((unsigned char) *cp))
10327 bruce@momjian.us         6425                 :          35167 :                 break;
10327 bruce@momjian.us         6426                 :UBC           0 :             cp++;
                               6427                 :                :         }
                               6428                 :                : 
                               6429                 :                :         /* Get the parameter value */
10327 bruce@momjian.us         6430                 :CBC       35182 :         pval = cp;
                               6431                 :                : 
                               6432         [ +  + ]:          35182 :         if (*cp != '\'')
                               6433                 :                :         {
                               6434                 :          27091 :             cp2 = pval;
                               6435         [ +  + ]:         310900 :             while (*cp)
                               6436                 :                :             {
 9144 tgl@sss.pgh.pa.us        6437         [ +  + ]:         307522 :                 if (isspace((unsigned char) *cp))
                               6438                 :                :                 {
10327 bruce@momjian.us         6439                 :          23713 :                     *cp++ = '\0';
                               6440                 :          23713 :                     break;
                               6441                 :                :                 }
                               6442         [ +  + ]:         283809 :                 if (*cp == '\\')
                               6443                 :                :                 {
                               6444                 :              1 :                     cp++;
                               6445         [ +  - ]:              1 :                     if (*cp != '\0')
                               6446                 :              1 :                         *cp2++ = *cp++;
                               6447                 :                :                 }
                               6448                 :                :                 else
                               6449                 :         283808 :                     *cp2++ = *cp++;
                               6450                 :                :             }
                               6451                 :          27091 :             *cp2 = '\0';
                               6452                 :                :         }
                               6453                 :                :         else
                               6454                 :                :         {
                               6455                 :           8091 :             cp2 = pval;
                               6456                 :           8091 :             cp++;
                               6457                 :                :             for (;;)
                               6458                 :                :             {
                               6459         [ -  + ]:          86926 :                 if (*cp == '\0')
                               6460                 :                :                 {
 1127 peter@eisentraut.org     6461                 :UBC           0 :                     libpq_append_error(errorMessage, "unterminated quoted string in connection info string");
 9411 tgl@sss.pgh.pa.us        6462                 :              0 :                     PQconninfoFree(options);
10327 bruce@momjian.us         6463                 :              0 :                     free(buf);
 9411 tgl@sss.pgh.pa.us        6464                 :              0 :                     return NULL;
                               6465                 :                :                 }
10327 bruce@momjian.us         6466         [ +  + ]:CBC       86926 :                 if (*cp == '\\')
                               6467                 :                :                 {
                               6468                 :            671 :                     cp++;
                               6469         [ +  - ]:            671 :                     if (*cp != '\0')
                               6470                 :            671 :                         *cp2++ = *cp++;
                               6471                 :            671 :                     continue;
                               6472                 :                :                 }
                               6473         [ +  + ]:          86255 :                 if (*cp == '\'')
                               6474                 :                :                 {
                               6475                 :           8091 :                     *cp2 = '\0';
                               6476                 :           8091 :                     cp++;
                               6477                 :           8091 :                     break;
                               6478                 :                :                 }
                               6479                 :          78164 :                 *cp2++ = *cp++;
                               6480                 :                :             }
                               6481                 :                :         }
                               6482                 :                : 
                               6483                 :                :         /*
                               6484                 :                :          * Now that we have the name and the value, store the record.
                               6485                 :                :          */
 4997 alvherre@alvh.no-ip.     6486         [ +  + ]:          35182 :         if (!conninfo_storeval(options, pname, pval, errorMessage, false, false))
                               6487                 :                :         {
 7492 neilc@samurai.com        6488                 :              3 :             PQconninfoFree(options);
                               6489                 :              3 :             free(buf);
                               6490                 :              3 :             return NULL;
                               6491                 :                :         }
                               6492                 :                :     }
                               6493                 :                : 
                               6494                 :                :     /* Done with the modifiable input string */
 8268 tgl@sss.pgh.pa.us        6495                 :          11173 :     free(buf);
                               6496                 :                : 
                               6497                 :                :     /*
                               6498                 :                :      * Add in defaults if the caller wants that.
                               6499                 :                :      */
 5017                          6500         [ +  + ]:          11173 :     if (use_defaults)
                               6501                 :                :     {
                               6502         [ -  + ]:           1229 :         if (!conninfo_add_defaults(options, errorMessage))
                               6503                 :                :         {
 5017 tgl@sss.pgh.pa.us        6504                 :UBC           0 :             PQconninfoFree(options);
                               6505                 :              0 :             return NULL;
                               6506                 :                :         }
                               6507                 :                :     }
                               6508                 :                : 
 9411 tgl@sss.pgh.pa.us        6509                 :CBC       11173 :     return options;
                               6510                 :                : }
                               6511                 :                : 
                               6512                 :                : /*
                               6513                 :                :  * Conninfo array parser routine
                               6514                 :                :  *
                               6515                 :                :  * If successful, a malloc'd PQconninfoOption array is returned.
                               6516                 :                :  * If not successful, NULL is returned and an error message is
                               6517                 :                :  * appended to errorMessage.
                               6518                 :                :  * Defaults are supplied (from a service file, environment variables, etc)
                               6519                 :                :  * for unspecified options, but only if use_defaults is true.
                               6520                 :                :  *
                               6521                 :                :  * If expand_dbname is non-zero, and the value passed for the first occurrence
                               6522                 :                :  * of "dbname" keyword is a connection string (as indicated by
                               6523                 :                :  * recognized_connection_string) then parse and process it, overriding any
                               6524                 :                :  * previously processed conflicting keywords. Subsequent keywords will take
                               6525                 :                :  * precedence, however. In-tree programs generally specify expand_dbname=true,
                               6526                 :                :  * so command-line arguments naming a database can use a connection string.
                               6527                 :                :  * Some code acquires arbitrary database names from known-literal sources like
                               6528                 :                :  * PQdb(), PQconninfoParse() and pg_database.datname.  When connecting to such
                               6529                 :                :  * a database, in-tree code first wraps the name in a connection string.
                               6530                 :                :  */
                               6531                 :                : static PQconninfoOption *
 3100                          6532                 :          12352 : conninfo_array_parse(const char *const *keywords, const char *const *values,
                               6533                 :                :                      PQExpBuffer errorMessage, bool use_defaults,
                               6534                 :                :                      int expand_dbname)
                               6535                 :                : {
                               6536                 :                :     PQconninfoOption *options;
 4997 alvherre@alvh.no-ip.     6537                 :          12352 :     PQconninfoOption *dbname_options = NULL;
                               6538                 :                :     PQconninfoOption *option;
 5772 bruce@momjian.us         6539                 :          12352 :     int         i = 0;
                               6540                 :                : 
                               6541                 :                :     /*
                               6542                 :                :      * If expand_dbname is non-zero, check keyword "dbname" to see if val is
                               6543                 :                :      * actually a recognized connection string.
                               6544                 :                :      */
                               6545   [ +  +  +  + ]:          51190 :     while (expand_dbname && keywords[i])
                               6546                 :                :     {
 5793 mail@joeconway.com       6547                 :          50767 :         const char *pname = keywords[i];
 5772 bruce@momjian.us         6548                 :          50767 :         const char *pvalue = values[i];
                               6549                 :                : 
                               6550                 :                :         /* first find "dbname" if any */
 4997 alvherre@alvh.no-ip.     6551   [ +  +  +  + ]:          50767 :         if (strcmp(pname, "dbname") == 0 && pvalue)
                               6552                 :                :         {
                               6553                 :                :             /*
                               6554                 :                :              * If value is a connection string, parse it, but do not use
                               6555                 :                :              * defaults here -- those get picked up later. We only want to
                               6556                 :                :              * override for those parameters actually passed.
                               6557                 :                :              */
 3911 rhaas@postgresql.org     6558         [ +  + ]:          11929 :             if (recognized_connection_string(pvalue))
                               6559                 :                :             {
 4997 alvherre@alvh.no-ip.     6560                 :           8741 :                 dbname_options = parse_connection_string(pvalue, errorMessage, false);
                               6561         [ -  + ]:           8741 :                 if (dbname_options == NULL)
 5793 mail@joeconway.com       6562                 :UBC           0 :                     return NULL;
                               6563                 :                :             }
 5793 mail@joeconway.com       6564                 :CBC       11929 :             break;
                               6565                 :                :         }
                               6566                 :          38838 :         ++i;
                               6567                 :                :     }
                               6568                 :                : 
                               6569                 :                :     /* Make a working copy of PQconninfoOptions */
 5017 tgl@sss.pgh.pa.us        6570                 :          12352 :     options = conninfo_init(errorMessage);
 5801 mail@joeconway.com       6571         [ -  + ]:          12352 :     if (options == NULL)
                               6572                 :                :     {
 4997 alvherre@alvh.no-ip.     6573                 :UBC           0 :         PQconninfoFree(dbname_options);
 5801 mail@joeconway.com       6574                 :              0 :         return NULL;
                               6575                 :                :     }
                               6576                 :                : 
                               6577                 :                :     /* Parse the keywords/values arrays */
 5017 tgl@sss.pgh.pa.us        6578                 :CBC       12352 :     i = 0;
 5772 bruce@momjian.us         6579         [ +  + ]:          92312 :     while (keywords[i])
                               6580                 :                :     {
 5801 mail@joeconway.com       6581                 :          79960 :         const char *pname = keywords[i];
 5772 bruce@momjian.us         6582                 :          79960 :         const char *pvalue = values[i];
                               6583                 :                : 
 4259                          6584   [ +  +  +  + ]:          79960 :         if (pvalue != NULL && pvalue[0] != '\0')
                               6585                 :                :         {
                               6586                 :                :             /* Search for the param record */
 5801 mail@joeconway.com       6587         [ +  - ]:         457836 :             for (option = options; option->keyword != NULL; option++)
                               6588                 :                :             {
                               6589         [ +  + ]:         457836 :                 if (strcmp(option->keyword, pname) == 0)
                               6590                 :          33053 :                     break;
                               6591                 :                :             }
                               6592                 :                : 
                               6593                 :                :             /* Check for invalid connection option */
                               6594         [ -  + ]:          33053 :             if (option->keyword == NULL)
                               6595                 :                :             {
 1127 peter@eisentraut.org     6596                 :UBC           0 :                 libpq_append_error(errorMessage, "invalid connection option \"%s\"", pname);
 5801 mail@joeconway.com       6597                 :              0 :                 PQconninfoFree(options);
 4997 alvherre@alvh.no-ip.     6598                 :              0 :                 PQconninfoFree(dbname_options);
 5801 mail@joeconway.com       6599                 :              0 :                 return NULL;
                               6600                 :                :             }
                               6601                 :                : 
                               6602                 :                :             /*
                               6603                 :                :              * If we are on the first dbname parameter, and we have a parsed
                               6604                 :                :              * connection string, copy those parameters across, overriding any
                               6605                 :                :              * existing previous settings.
                               6606                 :                :              */
 4997 alvherre@alvh.no-ip.     6607   [ +  +  +  + ]:CBC       33053 :             if (strcmp(pname, "dbname") == 0 && dbname_options)
 5793 mail@joeconway.com       6608                 :           8741 :             {
                               6609                 :                :                 PQconninfoOption *str_option;
                               6610                 :                : 
 4997 alvherre@alvh.no-ip.     6611         [ +  + ]:         454532 :                 for (str_option = dbname_options; str_option->keyword != NULL; str_option++)
                               6612                 :                :                 {
 5793 mail@joeconway.com       6613         [ +  + ]:         445791 :                     if (str_option->val != NULL)
                               6614                 :                :                     {
                               6615                 :                :                         int         k;
                               6616                 :                : 
                               6617         [ +  - ]:         296156 :                         for (k = 0; options[k].keyword; k++)
                               6618                 :                :                         {
                               6619         [ +  + ]:         296156 :                             if (strcmp(options[k].keyword, str_option->keyword) == 0)
                               6620                 :                :                             {
 1279 peter@eisentraut.org     6621                 :          28044 :                                 free(options[k].val);
 5793 mail@joeconway.com       6622                 :          28044 :                                 options[k].val = strdup(str_option->val);
 4039 heikki.linnakangas@i     6623         [ -  + ]:          28044 :                                 if (!options[k].val)
                               6624                 :                :                                 {
 1127 peter@eisentraut.org     6625                 :UBC           0 :                                     libpq_append_error(errorMessage, "out of memory");
 4039 heikki.linnakangas@i     6626                 :              0 :                                     PQconninfoFree(options);
                               6627                 :              0 :                                     PQconninfoFree(dbname_options);
                               6628                 :              0 :                                     return NULL;
                               6629                 :                :                                 }
 5793 mail@joeconway.com       6630                 :CBC       28044 :                                 break;
                               6631                 :                :                             }
                               6632                 :                :                         }
                               6633                 :                :                     }
                               6634                 :                :                 }
                               6635                 :                : 
                               6636                 :                :                 /*
                               6637                 :                :                  * Forget the parsed connection string, so that any subsequent
                               6638                 :                :                  * dbname parameters will not be expanded.
                               6639                 :                :                  */
 4039 heikki.linnakangas@i     6640                 :           8741 :                 PQconninfoFree(dbname_options);
                               6641                 :           8741 :                 dbname_options = NULL;
                               6642                 :                :             }
                               6643                 :                :             else
                               6644                 :                :             {
                               6645                 :                :                 /*
                               6646                 :                :                  * Store the value, overriding previous settings
                               6647                 :                :                  */
 1279 peter@eisentraut.org     6648                 :          24312 :                 free(option->val);
 5793 mail@joeconway.com       6649                 :          24312 :                 option->val = strdup(pvalue);
                               6650         [ -  + ]:          24312 :                 if (!option->val)
                               6651                 :                :                 {
 1127 peter@eisentraut.org     6652                 :UBC           0 :                     libpq_append_error(errorMessage, "out of memory");
 5793 mail@joeconway.com       6653                 :              0 :                     PQconninfoFree(options);
 4997 alvherre@alvh.no-ip.     6654                 :              0 :                     PQconninfoFree(dbname_options);
 5793 mail@joeconway.com       6655                 :              0 :                     return NULL;
                               6656                 :                :                 }
                               6657                 :                :             }
                               6658                 :                :         }
 5801 mail@joeconway.com       6659                 :CBC       79960 :         ++i;
                               6660                 :                :     }
 4997 alvherre@alvh.no-ip.     6661                 :          12352 :     PQconninfoFree(dbname_options);
                               6662                 :                : 
                               6663                 :                :     /*
                               6664                 :                :      * Add in defaults if the caller wants that.
                               6665                 :                :      */
 5017 tgl@sss.pgh.pa.us        6666         [ +  - ]:          12352 :     if (use_defaults)
                               6667                 :                :     {
                               6668         [ +  + ]:          12352 :         if (!conninfo_add_defaults(options, errorMessage))
                               6669                 :                :         {
                               6670                 :              8 :             PQconninfoFree(options);
                               6671                 :              8 :             return NULL;
                               6672                 :                :         }
                               6673                 :                :     }
                               6674                 :                : 
                               6675                 :          12344 :     return options;
                               6676                 :                : }
                               6677                 :                : 
                               6678                 :                : /*
                               6679                 :                :  * Add the default values for any unspecified options to the connection
                               6680                 :                :  * options array.
                               6681                 :                :  *
                               6682                 :                :  * Defaults are obtained from a service file, environment variables, etc.
                               6683                 :                :  *
                               6684                 :                :  * Returns true if successful, otherwise false; errorMessage, if supplied,
                               6685                 :                :  * is filled in upon failure.  Note that failure to locate a default value
                               6686                 :                :  * is not an error condition here --- we just leave the option's value as
                               6687                 :                :  * NULL.
                               6688                 :                :  */
                               6689                 :                : static bool
                               6690                 :          13708 : conninfo_add_defaults(PQconninfoOption *options, PQExpBuffer errorMessage)
                               6691                 :                : {
                               6692                 :                :     PQconninfoOption *option;
  986 dgustafsson@postgres     6693                 :          13708 :     PQconninfoOption *sslmode_default = NULL,
                               6694                 :          13708 :                *sslrootcert = NULL;
                               6695                 :                :     char       *tmp;
                               6696                 :                : 
                               6697                 :                :     /*
                               6698                 :                :      * If there's a service spec, use it to obtain any not-explicitly-given
                               6699                 :                :      * parameters.  Ignore error if no error message buffer is passed because
                               6700                 :                :      * there is no way to pass back the failure message.
                               6701                 :                :      */
 4396 bruce@momjian.us         6702   [ +  +  +  - ]:          13708 :     if (parseServiceInfo(options, errorMessage) != 0 && errorMessage)
 5017 tgl@sss.pgh.pa.us        6703                 :              8 :         return false;
                               6704                 :                : 
                               6705                 :                :     /*
                               6706                 :                :      * Get the fallback resources for parameters not specified in the conninfo
                               6707                 :                :      * string nor the service.
                               6708                 :                :      */
 5801 mail@joeconway.com       6709         [ +  + ]:         712400 :     for (option = options; option->keyword != NULL; option++)
                               6710                 :                :     {
  986 dgustafsson@postgres     6711         [ +  + ]:         698700 :         if (strcmp(option->keyword, "sslrootcert") == 0)
                               6712                 :          13700 :             sslrootcert = option;   /* save for later */
                               6713                 :                : 
 5801 mail@joeconway.com       6714         [ +  + ]:         698700 :         if (option->val != NULL)
                               6715                 :          55496 :             continue;           /* Value was in conninfo or service */
                               6716                 :                : 
                               6717                 :                :         /*
                               6718                 :                :          * Try to get the environment variable fallback
                               6719                 :                :          */
                               6720         [ +  + ]:         643204 :         if (option->envvar != NULL)
                               6721                 :                :         {
                               6722         [ +  + ]:         451166 :             if ((tmp = getenv(option->envvar)) != NULL)
                               6723                 :                :             {
                               6724                 :          23242 :                 option->val = strdup(tmp);
                               6725         [ -  + ]:          23242 :                 if (!option->val)
                               6726                 :                :                 {
 4396 bruce@momjian.us         6727         [ #  # ]:UBC           0 :                     if (errorMessage)
 1127 peter@eisentraut.org     6728                 :              0 :                         libpq_append_error(errorMessage, "out of memory");
 5017 tgl@sss.pgh.pa.us        6729                 :              0 :                     return false;
                               6730                 :                :                 }
 5801 mail@joeconway.com       6731                 :CBC       23242 :                 continue;
                               6732                 :                :             }
                               6733                 :                :         }
                               6734                 :                : 
                               6735                 :                :         /*
                               6736                 :                :          * Interpret the deprecated PGREQUIRESSL environment variable.  Per
                               6737                 :                :          * tradition, translate values starting with "1" to sslmode=require,
                               6738                 :                :          * and ignore other values.  Given both PGREQUIRESSL=1 and PGSSLMODE,
                               6739                 :                :          * PGSSLMODE takes precedence; the opposite was true before v9.3.
                               6740                 :                :          */
 3144 noah@leadboat.com        6741         [ +  + ]:         619962 :         if (strcmp(option->keyword, "sslmode") == 0)
                               6742                 :                :         {
                               6743                 :          13149 :             const char *requiresslenv = getenv("PGREQUIRESSL");
                               6744                 :                : 
                               6745   [ -  +  -  - ]:          13149 :             if (requiresslenv != NULL && requiresslenv[0] == '1')
                               6746                 :                :             {
 3144 noah@leadboat.com        6747                 :UBC           0 :                 option->val = strdup("require");
                               6748         [ #  # ]:              0 :                 if (!option->val)
                               6749                 :                :                 {
                               6750         [ #  # ]:              0 :                     if (errorMessage)
 1127 peter@eisentraut.org     6751                 :              0 :                         libpq_append_error(errorMessage, "out of memory");
 3144 noah@leadboat.com        6752                 :              0 :                     return false;
                               6753                 :                :                 }
                               6754                 :              0 :                 continue;
                               6755                 :                :             }
                               6756                 :                : 
                               6757                 :                :             /*
                               6758                 :                :              * sslmode is not specified. Let it be filled in with the compiled
                               6759                 :                :              * default for now, but if sslrootcert=system, we'll override the
                               6760                 :                :              * default later before returning.
                               6761                 :                :              */
  986 dgustafsson@postgres     6762                 :CBC       13149 :             sslmode_default = option;
                               6763                 :                :         }
                               6764                 :                : 
                               6765                 :                :         /*
                               6766                 :                :          * No environment variable specified or the variable isn't set - try
                               6767                 :                :          * compiled-in default
                               6768                 :                :          */
 5801 mail@joeconway.com       6769         [ +  + ]:         619962 :         if (option->compiled != NULL)
                               6770                 :                :         {
                               6771                 :         157383 :             option->val = strdup(option->compiled);
                               6772         [ -  + ]:         157383 :             if (!option->val)
                               6773                 :                :             {
 4396 bruce@momjian.us         6774         [ #  # ]:UBC           0 :                 if (errorMessage)
 1127 peter@eisentraut.org     6775                 :              0 :                     libpq_append_error(errorMessage, "out of memory");
 5017 tgl@sss.pgh.pa.us        6776                 :              0 :                 return false;
                               6777                 :                :             }
 5801 mail@joeconway.com       6778                 :CBC      157383 :             continue;
                               6779                 :                :         }
                               6780                 :                : 
                               6781                 :                :         /*
                               6782                 :                :          * Special handling for "user" option.  Note that if pg_fe_getauthname
                               6783                 :                :          * fails, we just leave the value as NULL; there's no need for this to
                               6784                 :                :          * be an error condition if the caller provides a user name.  The only
                               6785                 :                :          * reason we do this now at all is so that callers of PQconndefaults
                               6786                 :                :          * will see a correct default (barring error, of course).
                               6787                 :                :          */
                               6788         [ +  + ]:         462579 :         if (strcmp(option->keyword, "user") == 0)
                               6789                 :                :         {
 3992 tgl@sss.pgh.pa.us        6790                 :          11980 :             option->val = pg_fe_getauthname(NULL);
 5801 mail@joeconway.com       6791                 :          11980 :             continue;
                               6792                 :                :         }
                               6793                 :                :     }
                               6794                 :                : 
                               6795                 :                :     /*
                               6796                 :                :      * Special handling for sslrootcert=system with no sslmode explicitly
                               6797                 :                :      * defined. In this case we want to strengthen the default sslmode to
                               6798                 :                :      * verify-full.
                               6799                 :                :      */
  986 dgustafsson@postgres     6800   [ +  +  +  - ]:          13700 :     if (sslmode_default && sslrootcert)
                               6801                 :                :     {
                               6802   [ +  +  +  + ]:          13149 :         if (sslrootcert->val && strcmp(sslrootcert->val, "system") == 0)
                               6803                 :                :         {
                               6804                 :              4 :             free(sslmode_default->val);
                               6805                 :                : 
                               6806                 :              4 :             sslmode_default->val = strdup("verify-full");
                               6807         [ -  + ]:              4 :             if (!sslmode_default->val)
                               6808                 :                :             {
  986 dgustafsson@postgres     6809         [ #  # ]:UBC           0 :                 if (errorMessage)
                               6810                 :              0 :                     libpq_append_error(errorMessage, "out of memory");
                               6811                 :              0 :                 return false;
                               6812                 :                :             }
                               6813                 :                :         }
                               6814                 :                :     }
                               6815                 :                : 
 5017 tgl@sss.pgh.pa.us        6816                 :CBC       13700 :     return true;
                               6817                 :                : }
                               6818                 :                : 
                               6819                 :                : /*
                               6820                 :                :  * Subroutine for parse_connection_string
                               6821                 :                :  *
                               6822                 :                :  * Deal with a URI connection string.
                               6823                 :                :  */
                               6824                 :                : static PQconninfoOption *
 4997 alvherre@alvh.no-ip.     6825                 :             66 : conninfo_uri_parse(const char *uri, PQExpBuffer errorMessage,
                               6826                 :                :                    bool use_defaults)
                               6827                 :                : {
                               6828                 :                :     PQconninfoOption *options;
                               6829                 :                : 
                               6830                 :                :     /* Make a working copy of PQconninfoOptions */
                               6831                 :             66 :     options = conninfo_init(errorMessage);
                               6832         [ -  + ]:             66 :     if (options == NULL)
 4997 alvherre@alvh.no-ip.     6833                 :UBC           0 :         return NULL;
                               6834                 :                : 
 4997 alvherre@alvh.no-ip.     6835         [ +  + ]:CBC          66 :     if (!conninfo_uri_parse_options(options, uri, errorMessage))
                               6836                 :                :     {
                               6837                 :             15 :         PQconninfoFree(options);
                               6838                 :             15 :         return NULL;
                               6839                 :                :     }
                               6840                 :                : 
                               6841                 :                :     /*
                               6842                 :                :      * Add in defaults if the caller wants that.
                               6843                 :                :      */
                               6844         [ -  + ]:             51 :     if (use_defaults)
                               6845                 :                :     {
 4997 alvherre@alvh.no-ip.     6846         [ #  # ]:UBC           0 :         if (!conninfo_add_defaults(options, errorMessage))
                               6847                 :                :         {
                               6848                 :              0 :             PQconninfoFree(options);
                               6849                 :              0 :             return NULL;
                               6850                 :                :         }
                               6851                 :                :     }
                               6852                 :                : 
 4997 alvherre@alvh.no-ip.     6853                 :CBC          51 :     return options;
                               6854                 :                : }
                               6855                 :                : 
                               6856                 :                : /*
                               6857                 :                :  * conninfo_uri_parse_options
                               6858                 :                :  *      Actual URI parser.
                               6859                 :                :  *
                               6860                 :                :  * If successful, returns true while the options array is filled with parsed
                               6861                 :                :  * options from the URI.
                               6862                 :                :  * If not successful, returns false and fills errorMessage accordingly.
                               6863                 :                :  *
                               6864                 :                :  * Parses the connection URI string in 'uri' according to the URI syntax (RFC
                               6865                 :                :  * 3986):
                               6866                 :                :  *
                               6867                 :                :  * postgresql://[user[:password]@][netloc][:port][/dbname][?param1=value1&...]
                               6868                 :                :  *
                               6869                 :                :  * where "netloc" is a hostname, an IPv4 address, or an IPv6 address surrounded
                               6870                 :                :  * by literal square brackets.  As an extension, we also allow multiple
                               6871                 :                :  * netloc[:port] specifications, separated by commas:
                               6872                 :                :  *
                               6873                 :                :  * postgresql://[user[:password]@][netloc][:port][,...][/dbname][?param1=value1&...]
                               6874                 :                :  *
                               6875                 :                :  * Any of the URI parts might use percent-encoding (%xy).
                               6876                 :                :  */
                               6877                 :                : static bool
                               6878                 :             66 : conninfo_uri_parse_options(PQconninfoOption *options, const char *uri,
                               6879                 :                :                            PQExpBuffer errorMessage)
                               6880                 :                : {
                               6881                 :                :     int         prefix_len;
                               6882                 :                :     char       *p;
 3311 rhaas@postgresql.org     6883                 :             66 :     char       *buf = NULL;
                               6884                 :                :     char       *start;
 4937 bruce@momjian.us         6885                 :             66 :     char        prevchar = '\0';
                               6886                 :             66 :     char       *user = NULL;
                               6887                 :             66 :     char       *host = NULL;
                               6888                 :             66 :     bool        retval = false;
                               6889                 :                :     PQExpBufferData hostbuf;
                               6890                 :                :     PQExpBufferData portbuf;
                               6891                 :                : 
 3330 rhaas@postgresql.org     6892                 :             66 :     initPQExpBuffer(&hostbuf);
                               6893                 :             66 :     initPQExpBuffer(&portbuf);
                               6894   [ +  -  -  + ]:             66 :     if (PQExpBufferDataBroken(hostbuf) || PQExpBufferDataBroken(portbuf))
                               6895                 :                :     {
 1127 peter@eisentraut.org     6896                 :UBC           0 :         libpq_append_error(errorMessage, "out of memory");
 3311 rhaas@postgresql.org     6897                 :              0 :         goto cleanup;
                               6898                 :                :     }
                               6899                 :                : 
                               6900                 :                :     /* need a modifiable copy of the input URI */
 4039 heikki.linnakangas@i     6901                 :CBC          66 :     buf = strdup(uri);
 4997 alvherre@alvh.no-ip.     6902         [ -  + ]:             66 :     if (buf == NULL)
                               6903                 :                :     {
 1127 peter@eisentraut.org     6904                 :UBC           0 :         libpq_append_error(errorMessage, "out of memory");
 3311 rhaas@postgresql.org     6905                 :              0 :         goto cleanup;
                               6906                 :                :     }
 4039 heikki.linnakangas@i     6907                 :CBC          66 :     start = buf;
                               6908                 :                : 
                               6909                 :                :     /* Skip the URI prefix */
 3911 rhaas@postgresql.org     6910                 :             66 :     prefix_len = uri_prefix_length(uri);
 4997 alvherre@alvh.no-ip.     6911         [ -  + ]:             66 :     if (prefix_len == 0)
                               6912                 :                :     {
                               6913                 :                :         /* Should never happen */
 1127 peter@eisentraut.org     6914                 :UBC           0 :         libpq_append_error(errorMessage,
                               6915                 :                :                            "invalid URI propagated to internal parser routine: \"%s\"",
                               6916                 :                :                            uri);
 4997 alvherre@alvh.no-ip.     6917                 :              0 :         goto cleanup;
                               6918                 :                :     }
 4997 alvherre@alvh.no-ip.     6919                 :CBC          66 :     start += prefix_len;
                               6920                 :             66 :     p = start;
                               6921                 :                : 
                               6922                 :                :     /* Look ahead for possible user credentials designator */
                               6923   [ +  +  +  +  :            946 :     while (*p && *p != '@' && *p != '/')
                                              +  + ]
                               6924                 :            880 :         ++p;
                               6925         [ +  + ]:             66 :     if (*p == '@')
                               6926                 :                :     {
                               6927                 :                :         /*
                               6928                 :                :          * Found username/password designator, so URI should be of the form
                               6929                 :                :          * "scheme://user[:password]@[netloc]".
                               6930                 :                :          */
                               6931                 :             12 :         user = start;
                               6932                 :                : 
                               6933                 :             12 :         p = user;
                               6934   [ +  +  +  + ]:            104 :         while (*p != ':' && *p != '@')
                               6935                 :             92 :             ++p;
                               6936                 :                : 
                               6937                 :                :         /* Save last char and cut off at end of user name */
                               6938                 :             12 :         prevchar = *p;
                               6939                 :             12 :         *p = '\0';
                               6940                 :                : 
 4950 peter_e@gmx.net          6941   [ +  +  -  + ]:             23 :         if (*user &&
                               6942                 :             11 :             !conninfo_storeval(options, "user", user,
                               6943                 :                :                                errorMessage, false, true))
 4997 alvherre@alvh.no-ip.     6944                 :UBC           0 :             goto cleanup;
                               6945                 :                : 
 4997 alvherre@alvh.no-ip.     6946         [ +  + ]:CBC          12 :         if (prevchar == ':')
                               6947                 :                :         {
                               6948                 :              1 :             const char *password = p + 1;
                               6949                 :                : 
                               6950         [ +  + ]:              8 :             while (*p != '@')
                               6951                 :              7 :                 ++p;
                               6952                 :              1 :             *p = '\0';
                               6953                 :                : 
 4950 peter_e@gmx.net          6954   [ +  -  -  + ]:              2 :             if (*password &&
                               6955                 :              1 :                 !conninfo_storeval(options, "password", password,
                               6956                 :                :                                    errorMessage, false, true))
 4997 alvherre@alvh.no-ip.     6957                 :UBC           0 :                 goto cleanup;
                               6958                 :                :         }
                               6959                 :                : 
                               6960                 :                :         /* Advance past end of parsed user name or password token */
 4997 alvherre@alvh.no-ip.     6961                 :CBC          12 :         ++p;
                               6962                 :                :     }
                               6963                 :                :     else
                               6964                 :                :     {
                               6965                 :                :         /*
                               6966                 :                :          * No username/password designator found.  Reset to start of URI.
                               6967                 :                :          */
                               6968                 :             54 :         p = start;
                               6969                 :                :     }
                               6970                 :                : 
                               6971                 :                :     /*
                               6972                 :                :      * There may be multiple netloc[:port] pairs, each separated from the next
                               6973                 :                :      * by a comma.  When we initially enter this loop, "p" has been
                               6974                 :                :      * incremented past optional URI credential information at this point and
                               6975                 :                :      * now points at the "netloc" part of the URI.  On subsequent loop
                               6976                 :                :      * iterations, "p" has been incremented past the comma separator and now
                               6977                 :                :      * points at the start of the next "netloc".
                               6978                 :                :      */
                               6979                 :                :     for (;;)
                               6980                 :                :     {
                               6981                 :                :         /*
                               6982                 :                :          * Look for IPv6 address.
                               6983                 :                :          */
 3330 rhaas@postgresql.org     6984         [ +  + ]:             66 :         if (*p == '[')
                               6985                 :                :         {
                               6986                 :              8 :             host = ++p;
                               6987   [ +  +  +  + ]:             51 :             while (*p && *p != ']')
                               6988                 :             43 :                 ++p;
                               6989         [ +  + ]:              8 :             if (!*p)
                               6990                 :                :             {
 1127 peter@eisentraut.org     6991                 :              1 :                 libpq_append_error(errorMessage,
                               6992                 :                :                                    "end of string reached when looking for matching \"]\" in IPv6 host address in URI: \"%s\"",
                               6993                 :                :                                    uri);
 3330 rhaas@postgresql.org     6994                 :              1 :                 goto cleanup;
                               6995                 :                :             }
                               6996         [ +  + ]:              7 :             if (p == host)
                               6997                 :                :             {
 1127 peter@eisentraut.org     6998                 :              1 :                 libpq_append_error(errorMessage,
                               6999                 :                :                                    "IPv6 host address may not be empty in URI: \"%s\"",
                               7000                 :                :                                    uri);
 3330 rhaas@postgresql.org     7001                 :              1 :                 goto cleanup;
                               7002                 :                :             }
                               7003                 :                : 
                               7004                 :                :             /* Cut off the bracket and advance */
                               7005                 :              6 :             *(p++) = '\0';
                               7006                 :                : 
                               7007                 :                :             /*
                               7008                 :                :              * The address may be followed by a port specifier or a slash or a
                               7009                 :                :              * query or a separator comma.
                               7010                 :                :              */
                               7011   [ +  +  +  +  :              6 :             if (*p && *p != ':' && *p != '/' && *p != '?' && *p != ',')
                                     +  +  +  -  +  
                                                 - ]
                               7012                 :                :             {
 1127 peter@eisentraut.org     7013                 :              1 :                 libpq_append_error(errorMessage,
                               7014                 :                :                                    "unexpected character \"%c\" at position %d in URI (expected \":\" or \"/\"): \"%s\"",
                               7015                 :              1 :                                    *p, (int) (p - buf + 1), uri);
 3330 rhaas@postgresql.org     7016                 :              1 :                 goto cleanup;
                               7017                 :                :             }
                               7018                 :                :         }
                               7019                 :                :         else
                               7020                 :                :         {
                               7021                 :                :             /* not an IPv6 address: DNS-named or IPv4 netloc */
                               7022                 :             58 :             host = p;
                               7023                 :                : 
                               7024                 :                :             /*
                               7025                 :                :              * Look for port specifier (colon) or end of host specifier
                               7026                 :                :              * (slash) or query (question mark) or host separator (comma).
                               7027                 :                :              */
                               7028   [ +  +  +  +  :            239 :             while (*p && *p != ':' && *p != '/' && *p != '?' && *p != ',')
                                     +  +  +  +  +  
                                                 - ]
                               7029                 :            181 :                 ++p;
                               7030                 :                :         }
                               7031                 :                : 
                               7032                 :                :         /* Save the hostname terminator before we null it */
                               7033                 :             63 :         prevchar = *p;
                               7034                 :             63 :         *p = '\0';
                               7035                 :                : 
                               7036                 :             63 :         appendPQExpBufferStr(&hostbuf, host);
                               7037                 :                : 
                               7038         [ +  + ]:             63 :         if (prevchar == ':')
                               7039                 :                :         {
 3100 tgl@sss.pgh.pa.us        7040                 :             14 :             const char *port = ++p; /* advance past host terminator */
                               7041                 :                : 
 3330 rhaas@postgresql.org     7042   [ +  +  +  +  :             79 :             while (*p && *p != '/' && *p != '?' && *p != ',')
                                        +  +  +  - ]
                               7043                 :             65 :                 ++p;
                               7044                 :                : 
                               7045                 :             14 :             prevchar = *p;
                               7046                 :             14 :             *p = '\0';
                               7047                 :                : 
                               7048                 :             14 :             appendPQExpBufferStr(&portbuf, port);
                               7049                 :                :         }
                               7050                 :                : 
                               7051         [ +  - ]:             63 :         if (prevchar != ',')
                               7052                 :             63 :             break;
 3135 bruce@momjian.us         7053                 :UBC           0 :         ++p;                    /* advance past comma separator */
 3045 peter_e@gmx.net          7054                 :              0 :         appendPQExpBufferChar(&hostbuf, ',');
                               7055                 :              0 :         appendPQExpBufferChar(&portbuf, ',');
                               7056                 :                :     }
                               7057                 :                : 
                               7058                 :                :     /* Save final values for host and port. */
 3330 rhaas@postgresql.org     7059   [ +  -  -  + ]:CBC          63 :     if (PQExpBufferDataBroken(hostbuf) || PQExpBufferDataBroken(portbuf))
 3330 rhaas@postgresql.org     7060                 :UBC           0 :         goto cleanup;
 3330 rhaas@postgresql.org     7061   [ +  +  +  + ]:CBC         107 :     if (hostbuf.data[0] &&
                               7062                 :             44 :         !conninfo_storeval(options, "host", hostbuf.data,
                               7063                 :                :                            errorMessage, false, true))
                               7064                 :              4 :         goto cleanup;
                               7065   [ +  +  -  + ]:             72 :     if (portbuf.data[0] &&
                               7066                 :             13 :         !conninfo_storeval(options, "port", portbuf.data,
                               7067                 :                :                            errorMessage, false, true))
 3330 rhaas@postgresql.org     7068                 :UBC           0 :         goto cleanup;
                               7069                 :                : 
 4997 alvherre@alvh.no-ip.     7070   [ +  +  +  + ]:CBC          59 :     if (prevchar && prevchar != '?')
                               7071                 :                :     {
 3100 tgl@sss.pgh.pa.us        7072                 :             29 :         const char *dbname = ++p;   /* advance past host terminator */
                               7073                 :                : 
                               7074                 :                :         /* Look for query parameters */
 4997 alvherre@alvh.no-ip.     7075   [ +  +  +  + ]:             67 :         while (*p && *p != '?')
                               7076                 :             38 :             ++p;
                               7077                 :                : 
                               7078                 :             29 :         prevchar = *p;
                               7079                 :             29 :         *p = '\0';
                               7080                 :                : 
                               7081                 :                :         /*
                               7082                 :                :          * Avoid setting dbname to an empty string, as it forces the default
                               7083                 :                :          * value (username) and ignores $PGDATABASE, as opposed to not setting
                               7084                 :                :          * it at all.
                               7085                 :                :          */
                               7086   [ +  +  -  + ]:             46 :         if (*dbname &&
                               7087                 :             17 :             !conninfo_storeval(options, "dbname", dbname,
                               7088                 :                :                                errorMessage, false, true))
 4997 alvherre@alvh.no-ip.     7089                 :UBC           0 :             goto cleanup;
                               7090                 :                :     }
                               7091                 :                : 
 4997 alvherre@alvh.no-ip.     7092         [ +  + ]:CBC          59 :     if (prevchar)
                               7093                 :                :     {
 4937 bruce@momjian.us         7094                 :             31 :         ++p;                    /* advance past terminator */
                               7095                 :                : 
 4997 alvherre@alvh.no-ip.     7096         [ +  + ]:             31 :         if (!conninfo_uri_parse_params(p, options, errorMessage))
                               7097                 :              8 :             goto cleanup;
                               7098                 :                :     }
                               7099                 :                : 
                               7100                 :                :     /* everything parsed okay */
                               7101                 :             51 :     retval = true;
                               7102                 :                : 
                               7103                 :             66 : cleanup:
 3330 rhaas@postgresql.org     7104                 :             66 :     termPQExpBuffer(&hostbuf);
                               7105                 :             66 :     termPQExpBuffer(&portbuf);
 1279 peter@eisentraut.org     7106                 :             66 :     free(buf);
 4997 alvherre@alvh.no-ip.     7107                 :             66 :     return retval;
                               7108                 :                : }
                               7109                 :                : 
                               7110                 :                : /*
                               7111                 :                :  * Connection URI parameters parser routine
                               7112                 :                :  *
                               7113                 :                :  * If successful, returns true while connOptions is filled with parsed
                               7114                 :                :  * parameters.  Otherwise, returns false and fills errorMessage appropriately.
                               7115                 :                :  *
                               7116                 :                :  * Destructively modifies 'params' buffer.
                               7117                 :                :  */
                               7118                 :                : static bool
                               7119                 :             31 : conninfo_uri_parse_params(char *params,
                               7120                 :                :                           PQconninfoOption *connOptions,
                               7121                 :                :                           PQExpBuffer errorMessage)
                               7122                 :                : {
                               7123         [ +  + ]:             58 :     while (*params)
                               7124                 :                :     {
 4937 bruce@momjian.us         7125                 :             35 :         char       *keyword = params;
                               7126                 :             35 :         char       *value = NULL;
                               7127                 :             35 :         char       *p = params;
                               7128                 :             35 :         bool        malloced = false;
                               7129                 :                :         int         oldmsglen;
                               7130                 :                : 
                               7131                 :                :         /*
                               7132                 :                :          * Scan the params string for '=' and '&', marking the end of keyword
                               7133                 :                :          * and value respectively.
                               7134                 :                :          */
                               7135                 :                :         for (;;)
                               7136                 :                :         {
 4997 alvherre@alvh.no-ip.     7137         [ +  + ]:            931 :             if (*p == '=')
                               7138                 :                :             {
                               7139                 :                :                 /* Was there '=' already? */
                               7140         [ +  + ]:             34 :                 if (value != NULL)
                               7141                 :                :                 {
 1127 peter@eisentraut.org     7142                 :              1 :                     libpq_append_error(errorMessage,
                               7143                 :                :                                        "extra key/value separator \"=\" in URI query parameter: \"%s\"",
                               7144                 :                :                                        keyword);
 4997 alvherre@alvh.no-ip.     7145                 :              1 :                     return false;
                               7146                 :                :                 }
                               7147                 :                :                 /* Cut off keyword, advance to value */
 3951 tgl@sss.pgh.pa.us        7148                 :             33 :                 *p++ = '\0';
                               7149                 :             33 :                 value = p;
                               7150                 :                :             }
 4997 alvherre@alvh.no-ip.     7151   [ +  +  +  + ]:            897 :             else if (*p == '&' || *p == '\0')
                               7152                 :                :             {
                               7153                 :                :                 /*
                               7154                 :                :                  * If not at the end, cut off value and advance; leave p
                               7155                 :                :                  * pointing to start of the next parameter, if any.
                               7156                 :                :                  */
 3951 tgl@sss.pgh.pa.us        7157         [ +  + ]:             34 :                 if (*p != '\0')
                               7158                 :              8 :                     *p++ = '\0';
                               7159                 :                :                 /* Was there '=' at all? */
 4997 alvherre@alvh.no-ip.     7160         [ +  + ]:             34 :                 if (value == NULL)
                               7161                 :                :                 {
 1127 peter@eisentraut.org     7162                 :              2 :                     libpq_append_error(errorMessage,
                               7163                 :                :                                        "missing key/value separator \"=\" in URI query parameter: \"%s\"",
                               7164                 :                :                                        keyword);
 4997 alvherre@alvh.no-ip.     7165                 :              2 :                     return false;
                               7166                 :                :                 }
                               7167                 :                :                 /* Got keyword and value, go process them. */
                               7168                 :             32 :                 break;
                               7169                 :                :             }
                               7170                 :                :             else
 3951 tgl@sss.pgh.pa.us        7171                 :            863 :                 ++p;            /* Advance over all other bytes. */
                               7172                 :                :         }
                               7173                 :                : 
 4950 peter_e@gmx.net          7174                 :             32 :         keyword = conninfo_uri_decode(keyword, errorMessage);
                               7175         [ +  + ]:             32 :         if (keyword == NULL)
                               7176                 :                :         {
                               7177                 :                :             /* conninfo_uri_decode already set an error message */
                               7178                 :              1 :             return false;
                               7179                 :                :         }
                               7180                 :             31 :         value = conninfo_uri_decode(value, errorMessage);
                               7181         [ +  + ]:             31 :         if (value == NULL)
                               7182                 :                :         {
                               7183                 :                :             /* conninfo_uri_decode already set an error message */
                               7184                 :              2 :             free(keyword);
                               7185                 :              2 :             return false;
                               7186                 :                :         }
                               7187                 :             29 :         malloced = true;
                               7188                 :                : 
                               7189                 :                :         /*
                               7190                 :                :          * Special keyword handling for improved JDBC compatibility.
                               7191                 :                :          */
 4997 alvherre@alvh.no-ip.     7192         [ -  + ]:             29 :         if (strcmp(keyword, "ssl") == 0 &&
 4997 alvherre@alvh.no-ip.     7193         [ #  # ]:UBC           0 :             strcmp(value, "true") == 0)
                               7194                 :                :         {
 4950 peter_e@gmx.net          7195                 :              0 :             free(keyword);
                               7196                 :              0 :             free(value);
                               7197                 :              0 :             malloced = false;
                               7198                 :                : 
 4997 alvherre@alvh.no-ip.     7199                 :              0 :             keyword = "sslmode";
                               7200                 :              0 :             value = "require";
                               7201                 :                :         }
                               7202                 :                : 
                               7203                 :                :         /*
                               7204                 :                :          * Store the value if the corresponding option exists; ignore
                               7205                 :                :          * otherwise.  At this point both keyword and value are not
                               7206                 :                :          * URI-encoded.
                               7207                 :                :          */
 1800 tgl@sss.pgh.pa.us        7208                 :CBC          29 :         oldmsglen = errorMessage->len;
 4997 alvherre@alvh.no-ip.     7209         [ +  + ]:             29 :         if (!conninfo_storeval(connOptions, keyword, value,
                               7210                 :                :                                errorMessage, true, false))
                               7211                 :                :         {
                               7212                 :                :             /* Insert generic message if conninfo_storeval didn't give one. */
 1800 tgl@sss.pgh.pa.us        7213         [ +  - ]:              2 :             if (errorMessage->len == oldmsglen)
 1127 peter@eisentraut.org     7214                 :              2 :                 libpq_append_error(errorMessage,
                               7215                 :                :                                    "invalid URI query parameter: \"%s\"",
                               7216                 :                :                                    keyword);
                               7217                 :                :             /* And fail. */
 4863 peter_e@gmx.net          7218         [ +  - ]:              2 :             if (malloced)
                               7219                 :                :             {
                               7220                 :              2 :                 free(keyword);
                               7221                 :              2 :                 free(value);
                               7222                 :                :             }
 4939 rhaas@postgresql.org     7223                 :              2 :             return false;
                               7224                 :                :         }
                               7225                 :                : 
 4950 peter_e@gmx.net          7226         [ +  - ]:             27 :         if (malloced)
                               7227                 :                :         {
                               7228                 :             27 :             free(keyword);
                               7229                 :             27 :             free(value);
                               7230                 :                :         }
                               7231                 :                : 
                               7232                 :                :         /* Proceed to next key=value pair, if any */
 4997 alvherre@alvh.no-ip.     7233                 :             27 :         params = p;
                               7234                 :                :     }
                               7235                 :                : 
                               7236                 :             23 :     return true;
                               7237                 :                : }
                               7238                 :                : 
                               7239                 :                : /*
                               7240                 :                :  * Connection URI decoder routine
                               7241                 :                :  *
                               7242                 :                :  * If successful, returns the malloc'd decoded string.
                               7243                 :                :  * If not successful, returns NULL and fills errorMessage accordingly.
                               7244                 :                :  *
                               7245                 :                :  * The string is decoded by replacing any percent-encoded tokens with
                               7246                 :                :  * corresponding characters, while preserving any non-encoded characters.  A
                               7247                 :                :  * percent-encoded token is a character triplet: a percent sign, followed by a
                               7248                 :                :  * pair of hexadecimal digits (0-9A-F), where lower- and upper-case letters are
                               7249                 :                :  * treated identically.
                               7250                 :                :  */
                               7251                 :                : static char *
                               7252                 :            149 : conninfo_uri_decode(const char *str, PQExpBuffer errorMessage)
                               7253                 :                : {
                               7254                 :                :     char       *buf;            /* result */
                               7255                 :                :     char       *p;              /* output location */
  436 michael@paquier.xyz      7256                 :            149 :     const char *q = str;        /* input location */
                               7257                 :                : 
 4039 heikki.linnakangas@i     7258                 :            149 :     buf = malloc(strlen(str) + 1);
 4997 alvherre@alvh.no-ip.     7259         [ -  + ]:            149 :     if (buf == NULL)
                               7260                 :                :     {
 1127 peter@eisentraut.org     7261                 :UBC           0 :         libpq_append_error(errorMessage, "out of memory");
 4997 alvherre@alvh.no-ip.     7262                 :              0 :         return NULL;
                               7263                 :                :     }
 4039 heikki.linnakangas@i     7264                 :CBC         149 :     p = buf;
                               7265                 :                : 
                               7266                 :                :     /* skip leading whitespaces */
  436 michael@paquier.xyz      7267         [ +  + ]:            162 :     for (const char *s = q; *s == ' '; s++)
                               7268                 :                :     {
                               7269                 :             13 :         q++;
                               7270                 :             13 :         continue;
                               7271                 :                :     }
                               7272                 :                : 
                               7273                 :                :     for (;;)
                               7274                 :                :     {
 4997 alvherre@alvh.no-ip.     7275         [ +  + ]:           1299 :         if (*q != '%')
                               7276                 :                :         {
                               7277                 :                :             /* if found a whitespace or NUL, the string ends */
  436 michael@paquier.xyz      7278   [ +  +  +  + ]:           1264 :             if (*q == ' ' || *q == '\0')
                               7279                 :            144 :                 goto end;
                               7280                 :                : 
                               7281                 :                :             /* copy character */
                               7282                 :           1120 :             *(p++) = *(q++);
                               7283                 :                :         }
                               7284                 :                :         else
                               7285                 :                :         {
                               7286                 :                :             int         hi;
                               7287                 :                :             int         lo;
                               7288                 :                :             int         c;
                               7289                 :                : 
 4937 bruce@momjian.us         7290                 :             35 :             ++q;                /* skip the percent sign itself */
                               7291                 :                : 
                               7292                 :                :             /*
                               7293                 :                :              * Possible EOL will be caught by the first call to
                               7294                 :                :              * get_hexdigit(), so we never dereference an invalid q pointer.
                               7295                 :                :              */
 4997 alvherre@alvh.no-ip.     7296   [ +  +  +  + ]:             35 :             if (!(get_hexdigit(*q++, &hi) && get_hexdigit(*q++, &lo)))
                               7297                 :                :             {
 1127 peter@eisentraut.org     7298                 :              4 :                 libpq_append_error(errorMessage,
                               7299                 :                :                                    "invalid percent-encoded token: \"%s\"",
                               7300                 :                :                                    str);
 4997 alvherre@alvh.no-ip.     7301                 :              4 :                 free(buf);
                               7302                 :              5 :                 return NULL;
                               7303                 :                :             }
                               7304                 :                : 
                               7305                 :             31 :             c = (hi << 4) | lo;
                               7306         [ +  + ]:             31 :             if (c == 0)
                               7307                 :                :             {
 1127 peter@eisentraut.org     7308                 :              1 :                 libpq_append_error(errorMessage,
                               7309                 :                :                                    "forbidden value %%00 in percent-encoded value: \"%s\"",
                               7310                 :                :                                    str);
 4997 alvherre@alvh.no-ip.     7311                 :              1 :                 free(buf);
                               7312                 :              1 :                 return NULL;
                               7313                 :                :             }
                               7314                 :             30 :             *(p++) = c;
                               7315                 :                :         }
                               7316                 :                :     }
                               7317                 :                : 
  436 michael@paquier.xyz      7318                 :            144 : end:
                               7319                 :                : 
                               7320                 :                :     /* skip trailing whitespaces */
                               7321         [ +  + ]:            156 :     for (const char *s = q; *s == ' '; s++)
                               7322                 :                :     {
                               7323                 :             12 :         q++;
                               7324                 :             12 :         continue;
                               7325                 :                :     }
                               7326                 :                : 
                               7327                 :                :     /* Not at the end of the string yet?  Fail. */
                               7328         [ +  + ]:            144 :     if (*q != '\0')
                               7329                 :                :     {
  392                          7330                 :              2 :         libpq_append_error(errorMessage,
                               7331                 :                :                            "unexpected spaces found in \"%s\", use percent-encoded spaces (%%20) instead",
                               7332                 :                :                            str);
  436                          7333                 :              2 :         free(buf);
                               7334                 :              2 :         return NULL;
                               7335                 :                :     }
                               7336                 :                : 
                               7337                 :                :     /* Copy NUL terminator */
                               7338                 :            142 :     *p = '\0';
                               7339                 :                : 
 4997 alvherre@alvh.no-ip.     7340                 :            142 :     return buf;
                               7341                 :                : }
                               7342                 :                : 
                               7343                 :                : /*
                               7344                 :                :  * Convert hexadecimal digit character to its integer value.
                               7345                 :                :  *
                               7346                 :                :  * If successful, returns true and value is filled with digit's base 16 value.
                               7347                 :                :  * If not successful, returns false.
                               7348                 :                :  *
                               7349                 :                :  * Lower- and upper-case letters in the range A-F are treated identically.
                               7350                 :                :  */
                               7351                 :                : static bool
                               7352                 :             67 : get_hexdigit(char digit, int *value)
                               7353                 :                : {
                               7354   [ +  +  +  + ]:             67 :     if ('0' <= digit && digit <= '9')
                               7355                 :             35 :         *value = digit - '0';
                               7356   [ +  +  +  + ]:             32 :     else if ('A' <= digit && digit <= 'F')
                               7357                 :             27 :         *value = digit - 'A' + 10;
                               7358   [ +  +  +  + ]:              5 :     else if ('a' <= digit && digit <= 'f')
                               7359                 :              1 :         *value = digit - 'a' + 10;
                               7360                 :                :     else
                               7361                 :              4 :         return false;
                               7362                 :                : 
                               7363                 :             63 :     return true;
                               7364                 :                : }
                               7365                 :                : 
                               7366                 :                : /*
                               7367                 :                :  * Find an option value corresponding to the keyword in the connOptions array.
                               7368                 :                :  *
                               7369                 :                :  * If successful, returns a pointer to the corresponding option's value.
                               7370                 :                :  * If not successful, returns NULL.
                               7371                 :                :  */
                               7372                 :                : static const char *
 9411 tgl@sss.pgh.pa.us        7373                 :         719639 : conninfo_getval(PQconninfoOption *connOptions,
                               7374                 :                :                 const char *keyword)
                               7375                 :                : {
                               7376                 :                :     PQconninfoOption *option;
                               7377                 :                : 
 4997 alvherre@alvh.no-ip.     7378                 :         719639 :     option = conninfo_find(connOptions, keyword);
                               7379                 :                : 
                               7380         [ +  - ]:         719639 :     return option ? option->val : NULL;
                               7381                 :                : }
                               7382                 :                : 
                               7383                 :                : /*
                               7384                 :                :  * Store a (new) value for an option corresponding to the keyword in
                               7385                 :                :  * connOptions array.
                               7386                 :                :  *
                               7387                 :                :  * If uri_decode is true, the value is URI-decoded.  The keyword is always
                               7388                 :                :  * assumed to be non URI-encoded.
                               7389                 :                :  *
                               7390                 :                :  * If successful, returns a pointer to the corresponding PQconninfoOption,
                               7391                 :                :  * which value is replaced with a strdup'd copy of the passed value string.
                               7392                 :                :  * The existing value for the option is free'd before replacing, if any.
                               7393                 :                :  *
                               7394                 :                :  * If not successful, returns NULL and fills errorMessage accordingly.
                               7395                 :                :  * However, if the reason of failure is an invalid keyword being passed and
                               7396                 :                :  * ignoreMissing is true, errorMessage will be left untouched.
                               7397                 :                :  */
                               7398                 :                : static PQconninfoOption *
                               7399                 :         392044 : conninfo_storeval(PQconninfoOption *connOptions,
                               7400                 :                :                   const char *keyword, const char *value,
                               7401                 :                :                   PQExpBuffer errorMessage, bool ignoreMissing,
                               7402                 :                :                   bool uri_decode)
                               7403                 :                : {
                               7404                 :                :     PQconninfoOption *option;
                               7405                 :                :     char       *value_copy;
                               7406                 :                : 
                               7407                 :                :     /*
                               7408                 :                :      * For backwards compatibility, requiressl=1 gets translated to
                               7409                 :                :      * sslmode=require, and requiressl=0 gets translated to sslmode=prefer
                               7410                 :                :      * (which is the default for sslmode).
                               7411                 :                :      */
 4764 magnus@hagander.net      7412         [ -  + ]:         392044 :     if (strcmp(keyword, "requiressl") == 0)
                               7413                 :                :     {
 4764 magnus@hagander.net      7414                 :UBC           0 :         keyword = "sslmode";
                               7415         [ #  # ]:              0 :         if (value[0] == '1')
                               7416                 :              0 :             value = "require";
                               7417                 :                :         else
                               7418                 :              0 :             value = "prefer";
                               7419                 :                :     }
                               7420                 :                : 
 4950 peter_e@gmx.net          7421                 :CBC      392044 :     option = conninfo_find(connOptions, keyword);
 4997 alvherre@alvh.no-ip.     7422         [ +  + ]:         392044 :     if (option == NULL)
                               7423                 :                :     {
                               7424         [ +  + ]:              5 :         if (!ignoreMissing)
 1127 peter@eisentraut.org     7425                 :              3 :             libpq_append_error(errorMessage,
                               7426                 :                :                                "invalid connection option \"%s\"",
                               7427                 :                :                                keyword);
 4950 peter_e@gmx.net          7428                 :              5 :         return NULL;
                               7429                 :                :     }
                               7430                 :                : 
 4997 alvherre@alvh.no-ip.     7431         [ +  + ]:         392039 :     if (uri_decode)
                               7432                 :                :     {
                               7433                 :             86 :         value_copy = conninfo_uri_decode(value, errorMessage);
                               7434         [ +  + ]:             86 :         if (value_copy == NULL)
                               7435                 :                :             /* conninfo_uri_decode already set an error message */
 4950 peter_e@gmx.net          7436                 :              4 :             return NULL;
                               7437                 :                :     }
                               7438                 :                :     else
                               7439                 :                :     {
 4997 alvherre@alvh.no-ip.     7440                 :         391953 :         value_copy = strdup(value);
                               7441         [ -  + ]:         391953 :         if (value_copy == NULL)
                               7442                 :                :         {
 1127 peter@eisentraut.org     7443                 :UBC           0 :             libpq_append_error(errorMessage, "out of memory");
 4950 peter_e@gmx.net          7444                 :              0 :             return NULL;
                               7445                 :                :         }
                               7446                 :                :     }
                               7447                 :                : 
 1279 peter@eisentraut.org     7448                 :CBC      392035 :     free(option->val);
 4997 alvherre@alvh.no-ip.     7449                 :         392035 :     option->val = value_copy;
                               7450                 :                : 
                               7451                 :         392035 :     return option;
                               7452                 :                : }
                               7453                 :                : 
                               7454                 :                : /*
                               7455                 :                :  * Find a PQconninfoOption option corresponding to the keyword in the
                               7456                 :                :  * connOptions array.
                               7457                 :                :  *
                               7458                 :                :  * If successful, returns a pointer to the corresponding PQconninfoOption
                               7459                 :                :  * structure.
                               7460                 :                :  * If not successful, returns NULL.
                               7461                 :                :  */
                               7462                 :                : static PQconninfoOption *
                               7463                 :        1111683 : conninfo_find(PQconninfoOption *connOptions, const char *keyword)
                               7464                 :                : {
                               7465                 :                :     PQconninfoOption *option;
                               7466                 :                : 
 9411 tgl@sss.pgh.pa.us        7467         [ +  + ]:       26402902 :     for (option = connOptions; option->keyword != NULL; option++)
                               7468                 :                :     {
                               7469         [ +  + ]:       26402897 :         if (strcmp(option->keyword, keyword) == 0)
 4997 alvherre@alvh.no-ip.     7470                 :        1111678 :             return option;
                               7471                 :                :     }
                               7472                 :                : 
10327 bruce@momjian.us         7473                 :              5 :     return NULL;
                               7474                 :                : }
                               7475                 :                : 
                               7476                 :                : 
                               7477                 :                : /*
                               7478                 :                :  * Return the connection options used for the connection
                               7479                 :                :  */
                               7480                 :                : PQconninfoOption *
 4764 magnus@hagander.net      7481                 :          17757 : PQconninfo(PGconn *conn)
                               7482                 :                : {
                               7483                 :                :     PQExpBufferData errorBuf;
                               7484                 :                :     PQconninfoOption *connOptions;
                               7485                 :                : 
                               7486         [ -  + ]:          17757 :     if (conn == NULL)
 4764 magnus@hagander.net      7487                 :UBC           0 :         return NULL;
                               7488                 :                : 
                               7489                 :                :     /*
                               7490                 :                :      * We don't actually report any errors here, but callees want a buffer,
                               7491                 :                :      * and we prefer not to trash the conn's errorMessage.
                               7492                 :                :      */
 4764 magnus@hagander.net      7493                 :CBC       17757 :     initPQExpBuffer(&errorBuf);
                               7494         [ -  + ]:          17757 :     if (PQExpBufferDataBroken(errorBuf))
 4764 magnus@hagander.net      7495                 :UBC           0 :         return NULL;            /* out of memory already :-( */
                               7496                 :                : 
 4764 magnus@hagander.net      7497                 :CBC       17757 :     connOptions = conninfo_init(&errorBuf);
                               7498                 :                : 
                               7499         [ +  - ]:          17757 :     if (connOptions != NULL)
                               7500                 :                :     {
                               7501                 :                :         const internalPQconninfoOption *option;
                               7502                 :                : 
                               7503         [ +  + ]:         923364 :         for (option = PQconninfoOptions; option->keyword; option++)
                               7504                 :                :         {
                               7505                 :                :             char      **connmember;
                               7506                 :                : 
                               7507         [ -  + ]:         905607 :             if (option->connofs < 0)
 4764 magnus@hagander.net      7508                 :UBC           0 :                 continue;
                               7509                 :                : 
 4764 magnus@hagander.net      7510                 :CBC      905607 :             connmember = (char **) ((char *) conn + option->connofs);
                               7511                 :                : 
                               7512         [ +  + ]:         905607 :             if (*connmember)
                               7513                 :         356747 :                 conninfo_storeval(connOptions, option->keyword, *connmember,
                               7514                 :                :                                   &errorBuf, true, false);
                               7515                 :                :         }
                               7516                 :                :     }
                               7517                 :                : 
                               7518                 :          17757 :     termPQExpBuffer(&errorBuf);
                               7519                 :                : 
                               7520                 :          17757 :     return connOptions;
                               7521                 :                : }
                               7522                 :                : 
                               7523                 :                : 
                               7524                 :                : void
 9411 tgl@sss.pgh.pa.us        7525                 :          55086 : PQconninfoFree(PQconninfoOption *connOptions)
                               7526                 :                : {
                               7527         [ +  + ]:          55086 :     if (connOptions == NULL)
                               7528                 :          13721 :         return;
                               7529                 :                : 
 1279 peter@eisentraut.org     7530         [ +  + ]:        2150980 :     for (PQconninfoOption *option = connOptions; option->keyword != NULL; option++)
                               7531                 :        2109615 :         free(option->val);
 9411 tgl@sss.pgh.pa.us        7532                 :          41365 :     free(connOptions);
                               7533                 :                : }
                               7534                 :                : 
                               7535                 :                : 
                               7536                 :                : /* =========== accessor functions for PGconn ========= */
                               7537                 :                : char *
 9532 bruce@momjian.us         7538                 :          17478 : PQdb(const PGconn *conn)
                               7539                 :                : {
10327                          7540         [ +  + ]:          17478 :     if (!conn)
 8014 neilc@samurai.com        7541                 :              1 :         return NULL;
10327 bruce@momjian.us         7542                 :          17477 :     return conn->dbName;
                               7543                 :                : }
                               7544                 :                : 
                               7545                 :                : char *
 9532                          7546                 :           8734 : PQuser(const PGconn *conn)
                               7547                 :                : {
10327                          7548         [ -  + ]:           8734 :     if (!conn)
 8014 neilc@samurai.com        7549                 :UBC           0 :         return NULL;
10327 bruce@momjian.us         7550                 :CBC        8734 :     return conn->pguser;
                               7551                 :                : }
                               7552                 :                : 
                               7553                 :                : char *
 9532                          7554                 :            310 : PQpass(const PGconn *conn)
                               7555                 :                : {
 3135                          7556                 :            310 :     char       *password = NULL;
                               7557                 :                : 
 9966                          7558         [ -  + ]:            310 :     if (!conn)
 8014 neilc@samurai.com        7559                 :UBC           0 :         return NULL;
 3330 rhaas@postgresql.org     7560         [ +  + ]:CBC         310 :     if (conn->connhost != NULL)
                               7561                 :            302 :         password = conn->connhost[conn->whichhost].password;
                               7562         [ +  + ]:            310 :     if (password == NULL)
                               7563                 :            308 :         password = conn->pgpass;
                               7564                 :                :     /* Historically we've returned "" not NULL for no password specified */
 3248 tgl@sss.pgh.pa.us        7565         [ +  + ]:            310 :     if (password == NULL)
                               7566                 :            260 :         password = "";
 3330 rhaas@postgresql.org     7567                 :            310 :     return password;
                               7568                 :                : }
                               7569                 :                : 
                               7570                 :                : char *
 9532 bruce@momjian.us         7571                 :           9006 : PQhost(const PGconn *conn)
                               7572                 :                : {
10327                          7573         [ -  + ]:           9006 :     if (!conn)
 8014 neilc@samurai.com        7574                 :UBC           0 :         return NULL;
                               7575                 :                : 
 2821 peter_e@gmx.net          7576         [ +  - ]:CBC        9006 :     if (conn->connhost != NULL)
                               7577                 :                :     {
                               7578                 :                :         /*
                               7579                 :                :          * Return the verbatim host value provided by user, or hostaddr in its
                               7580                 :                :          * lack.
                               7581                 :                :          */
                               7582         [ +  - ]:           9006 :         if (conn->connhost[conn->whichhost].host != NULL &&
                               7583         [ +  - ]:           9006 :             conn->connhost[conn->whichhost].host[0] != '\0')
                               7584                 :           9006 :             return conn->connhost[conn->whichhost].host;
 2821 peter_e@gmx.net          7585         [ #  # ]:UBC           0 :         else if (conn->connhost[conn->whichhost].hostaddr != NULL &&
                               7586         [ #  # ]:              0 :                  conn->connhost[conn->whichhost].hostaddr[0] != '\0')
                               7587                 :              0 :             return conn->connhost[conn->whichhost].hostaddr;
                               7588                 :                :     }
                               7589                 :                : 
                               7590                 :              0 :     return "";
                               7591                 :                : }
                               7592                 :                : 
                               7593                 :                : char *
 2584 alvherre@alvh.no-ip.     7594                 :              0 : PQhostaddr(const PGconn *conn)
                               7595                 :                : {
                               7596         [ #  # ]:              0 :     if (!conn)
                               7597                 :              0 :         return NULL;
                               7598                 :                : 
                               7599                 :                :     /* Return the parsed IP address */
 2377                          7600   [ #  #  #  # ]:              0 :     if (conn->connhost != NULL && conn->connip != NULL)
                               7601                 :              0 :         return conn->connip;
                               7602                 :                : 
 2584                          7603                 :              0 :     return "";
                               7604                 :                : }
                               7605                 :                : 
                               7606                 :                : char *
 9532 bruce@momjian.us         7607                 :CBC        8873 : PQport(const PGconn *conn)
                               7608                 :                : {
10327                          7609         [ -  + ]:           8873 :     if (!conn)
 8014 neilc@samurai.com        7610                 :UBC           0 :         return NULL;
                               7611                 :                : 
  152 tgl@sss.pgh.pa.us        7612         [ +  - ]:CBC        8873 :     if (conn->connhost != NULL &&
                               7613         [ +  - ]:           8873 :         conn->connhost[conn->whichhost].port != NULL &&
                               7614         [ +  - ]:           8873 :         conn->connhost[conn->whichhost].port[0] != '\0')
 3330 rhaas@postgresql.org     7615                 :           8873 :         return conn->connhost[conn->whichhost].port;
                               7616                 :                : 
  152 tgl@sss.pgh.pa.us        7617                 :UBC           0 :     return DEF_PGPORT_STR;
                               7618                 :                : }
                               7619                 :                : 
                               7620                 :                : /*
                               7621                 :                :  * No longer does anything, but the function remains for API backwards
                               7622                 :                :  * compatibility.
                               7623                 :                :  */
                               7624                 :                : char *
 9532 bruce@momjian.us         7625                 :              0 : PQtty(const PGconn *conn)
                               7626                 :                : {
10327                          7627         [ #  # ]:              0 :     if (!conn)
 8014 neilc@samurai.com        7628                 :              0 :         return NULL;
 1743 peter@eisentraut.org     7629                 :              0 :     return "";
                               7630                 :                : }
                               7631                 :                : 
                               7632                 :                : char *
 9532 bruce@momjian.us         7633                 :              0 : PQoptions(const PGconn *conn)
                               7634                 :                : {
10327                          7635         [ #  # ]:              0 :     if (!conn)
 8014 neilc@samurai.com        7636                 :              0 :         return NULL;
 9966 bruce@momjian.us         7637                 :              0 :     return conn->pgoptions;
                               7638                 :                : }
                               7639                 :                : 
                               7640                 :                : ConnStatusType
 9532 bruce@momjian.us         7641                 :CBC      246780 : PQstatus(const PGconn *conn)
                               7642                 :                : {
10327                          7643         [ -  + ]:         246780 :     if (!conn)
10327 bruce@momjian.us         7644                 :UBC           0 :         return CONNECTION_BAD;
10327 bruce@momjian.us         7645                 :CBC      246780 :     return conn->status;
                               7646                 :                : }
                               7647                 :                : 
                               7648                 :                : PGTransactionStatusType
 8214 tgl@sss.pgh.pa.us        7649                 :         200264 : PQtransactionStatus(const PGconn *conn)
                               7650                 :                : {
                               7651   [ +  -  -  + ]:         200264 :     if (!conn || conn->status != CONNECTION_OK)
 8214 tgl@sss.pgh.pa.us        7652                 :UBC           0 :         return PQTRANS_UNKNOWN;
 8214 tgl@sss.pgh.pa.us        7653         [ +  + ]:CBC      200264 :     if (conn->asyncStatus != PGASYNC_IDLE)
                               7654                 :            503 :         return PQTRANS_ACTIVE;
                               7655                 :         199761 :     return conn->xactStatus;
                               7656                 :                : }
                               7657                 :                : 
                               7658                 :                : const char *
                               7659                 :         399511 : PQparameterStatus(const PGconn *conn, const char *paramName)
                               7660                 :                : {
                               7661                 :                :     const pgParameterStatus *pstatus;
                               7662                 :                : 
                               7663   [ +  -  -  + ]:         399511 :     if (!conn || !paramName)
 8214 tgl@sss.pgh.pa.us        7664                 :UBC           0 :         return NULL;
 8214 tgl@sss.pgh.pa.us        7665         [ +  - ]:CBC     4810238 :     for (pstatus = conn->pstatus; pstatus != NULL; pstatus = pstatus->next)
                               7666                 :                :     {
                               7667         [ +  + ]:        4810238 :         if (strcmp(pstatus->name, paramName) == 0)
                               7668                 :         399511 :             return pstatus->value;
                               7669                 :                :     }
 8214 tgl@sss.pgh.pa.us        7670                 :UBC           0 :     return NULL;
                               7671                 :                : }
                               7672                 :                : 
                               7673                 :                : int
                               7674                 :              0 : PQprotocolVersion(const PGconn *conn)
                               7675                 :                : {
                               7676         [ #  # ]:              0 :     if (!conn)
                               7677                 :              0 :         return 0;
                               7678         [ #  # ]:              0 :     if (conn->status == CONNECTION_BAD)
                               7679                 :              0 :         return 0;
                               7680                 :              0 :     return PG_PROTOCOL_MAJOR(conn->pversion);
                               7681                 :                : }
                               7682                 :                : 
                               7683                 :                : int
  463 rhaas@postgresql.org     7684                 :CBC           3 : PQfullProtocolVersion(const PGconn *conn)
                               7685                 :                : {
                               7686         [ -  + ]:              3 :     if (!conn)
  463 rhaas@postgresql.org     7687                 :UBC           0 :         return 0;
  463 rhaas@postgresql.org     7688         [ -  + ]:CBC           3 :     if (conn->status == CONNECTION_BAD)
  463 rhaas@postgresql.org     7689                 :UBC           0 :         return 0;
  463 rhaas@postgresql.org     7690                 :CBC           3 :     return PG_PROTOCOL_FULL(conn->pversion);
                               7691                 :                : }
                               7692                 :                : 
                               7693                 :                : int
 7797 tgl@sss.pgh.pa.us        7694                 :          25143 : PQserverVersion(const PGconn *conn)
                               7695                 :                : {
                               7696         [ -  + ]:          25143 :     if (!conn)
 7797 tgl@sss.pgh.pa.us        7697                 :UBC           0 :         return 0;
 7797 tgl@sss.pgh.pa.us        7698         [ -  + ]:CBC       25143 :     if (conn->status == CONNECTION_BAD)
 7797 tgl@sss.pgh.pa.us        7699                 :UBC           0 :         return 0;
 7797 tgl@sss.pgh.pa.us        7700                 :CBC       25143 :     return conn->sversion;
                               7701                 :                : }
                               7702                 :                : 
                               7703                 :                : char *
 9532 bruce@momjian.us         7704                 :            890 : PQerrorMessage(const PGconn *conn)
                               7705                 :                : {
10327                          7706         [ -  + ]:            890 :     if (!conn)
 8920 peter_e@gmx.net          7707                 :UBC           0 :         return libpq_gettext("connection pointer is NULL\n");
                               7708                 :                : 
                               7709                 :                :     /*
                               7710                 :                :      * The errorMessage buffer might be marked "broken" due to having
                               7711                 :                :      * previously failed to allocate enough memory for the message.  In that
                               7712                 :                :      * case, tell the application we ran out of memory.
                               7713                 :                :      */
 1601 tgl@sss.pgh.pa.us        7714   [ +  -  -  + ]:CBC         890 :     if (PQExpBufferBroken(&conn->errorMessage))
 1601 tgl@sss.pgh.pa.us        7715                 :UBC           0 :         return libpq_gettext("out of memory\n");
                               7716                 :                : 
 9604 tgl@sss.pgh.pa.us        7717                 :CBC         890 :     return conn->errorMessage.data;
                               7718                 :                : }
                               7719                 :                : 
                               7720                 :                : /*
                               7721                 :                :  * In Windows, socket values are unsigned, and an invalid socket value
                               7722                 :                :  * (INVALID_SOCKET) is ~0, which equals -1 in comparisons (with no compiler
                               7723                 :                :  * warning). Ideally we would return an unsigned value for PQsocket() on
                               7724                 :                :  * Windows, but that would cause the function's return value to differ from
                               7725                 :                :  * Unix, so we just return -1 for invalid sockets.
                               7726                 :                :  * http://msdn.microsoft.com/en-us/library/windows/desktop/cc507522%28v=vs.85%29.aspx
                               7727                 :                :  * http://stackoverflow.com/questions/10817252/why-is-invalid-socket-defined-as-0-in-winsock2-h-c
                               7728                 :                :  */
                               7729                 :                : int
 9532 bruce@momjian.us         7730                 :         230454 : PQsocket(const PGconn *conn)
                               7731                 :                : {
10086                          7732         [ -  + ]:         230454 :     if (!conn)
10086 bruce@momjian.us         7733                 :UBC           0 :         return -1;
  313 dgustafsson@postgres     7734         [ -  + ]:CBC      230454 :     if (conn->altsock != PGINVALID_SOCKET)
  313 dgustafsson@postgres     7735                 :UBC           0 :         return conn->altsock;
 4262 bruce@momjian.us         7736                 :CBC      230454 :     return (conn->sock != PGINVALID_SOCKET) ? conn->sock : -1;
                               7737                 :                : }
                               7738                 :                : 
                               7739                 :                : int
 9532                          7740                 :            794 : PQbackendPID(const PGconn *conn)
                               7741                 :                : {
 9966                          7742   [ +  -  -  + ]:            794 :     if (!conn || conn->status != CONNECTION_OK)
 9966 bruce@momjian.us         7743                 :UBC           0 :         return 0;
 9966 bruce@momjian.us         7744                 :CBC         794 :     return conn->be_pid;
                               7745                 :                : }
                               7746                 :                : 
                               7747                 :                : PGpipelineStatus
 1737 alvherre@alvh.no-ip.     7748                 :         748660 : PQpipelineStatus(const PGconn *conn)
                               7749                 :                : {
                               7750         [ -  + ]:         748660 :     if (!conn)
 1737 alvherre@alvh.no-ip.     7751                 :UBC           0 :         return PQ_PIPELINE_OFF;
                               7752                 :                : 
 1737 alvherre@alvh.no-ip.     7753                 :CBC      748660 :     return conn->pipelineStatus;
                               7754                 :                : }
                               7755                 :                : 
                               7756                 :                : int
 6582 tgl@sss.pgh.pa.us        7757                 :            310 : PQconnectionNeedsPassword(const PGconn *conn)
                               7758                 :                : {
                               7759                 :                :     char       *password;
                               7760                 :                : 
                               7761         [ -  + ]:            310 :     if (!conn)
 6582 tgl@sss.pgh.pa.us        7762                 :UBC           0 :         return false;
 3330 rhaas@postgresql.org     7763                 :CBC         310 :     password = PQpass(conn);
 6582 tgl@sss.pgh.pa.us        7764   [ +  +  +  - ]:            310 :     if (conn->password_needed &&
 3330 rhaas@postgresql.org     7765         [ +  + ]:             11 :         (password == NULL || password[0] == '\0'))
 6582 tgl@sss.pgh.pa.us        7766                 :              1 :         return true;
                               7767                 :                :     else
                               7768                 :            309 :         return false;
                               7769                 :                : }
                               7770                 :                : 
                               7771                 :                : int
 6736                          7772                 :            379 : PQconnectionUsedPassword(const PGconn *conn)
                               7773                 :                : {
                               7774         [ -  + ]:            379 :     if (!conn)
 6736 tgl@sss.pgh.pa.us        7775                 :UBC           0 :         return false;
 6294 tgl@sss.pgh.pa.us        7776         [ +  + ]:CBC         379 :     if (conn->password_needed)
 6736                          7777                 :             10 :         return true;
                               7778                 :                :     else
                               7779                 :            369 :         return false;
                               7780                 :                : }
                               7781                 :                : 
                               7782                 :                : int
  978 sfrost@snowman.net       7783                 :             14 : PQconnectionUsedGSSAPI(const PGconn *conn)
                               7784                 :                : {
                               7785         [ -  + ]:             14 :     if (!conn)
  978 sfrost@snowman.net       7786                 :UBC           0 :         return false;
  978 sfrost@snowman.net       7787         [ +  + ]:CBC          14 :     if (conn->gssapi_used)
                               7788                 :              4 :         return true;
                               7789                 :                :     else
                               7790                 :             10 :         return false;
                               7791                 :                : }
                               7792                 :                : 
                               7793                 :                : int
 9446 ishii@postgresql.org     7794                 :         207941 : PQclientEncoding(const PGconn *conn)
                               7795                 :                : {
 9467                          7796   [ +  -  -  + ]:         207941 :     if (!conn || conn->status != CONNECTION_OK)
 9467 ishii@postgresql.org     7797                 :UBC           0 :         return -1;
 9467 ishii@postgresql.org     7798                 :CBC      207941 :     return conn->client_encoding;
                               7799                 :                : }
                               7800                 :                : 
                               7801                 :                : int
 9446                          7802                 :             94 : PQsetClientEncoding(PGconn *conn, const char *encoding)
                               7803                 :                : {
                               7804                 :                :     char        qbuf[128];
                               7805                 :                :     static const char query[] = "set client_encoding to '%s'";
                               7806                 :                :     PGresult   *res;
                               7807                 :                :     int         status;
                               7808                 :                : 
                               7809   [ +  -  -  + ]:             94 :     if (!conn || conn->status != CONNECTION_OK)
 9446 ishii@postgresql.org     7810                 :UBC           0 :         return -1;
                               7811                 :                : 
 9432 ishii@postgresql.org     7812         [ -  + ]:CBC          94 :     if (!encoding)
 9432 ishii@postgresql.org     7813                 :UBC           0 :         return -1;
                               7814                 :                : 
                               7815                 :                :     /* Resolve special "auto" value from the locale */
 5414 peter_e@gmx.net          7816         [ -  + ]:CBC          94 :     if (strcmp(encoding, "auto") == 0)
 5414 peter_e@gmx.net          7817                 :UBC           0 :         encoding = pg_encoding_to_char(pg_get_encoding_from_locale(NULL, true));
                               7818                 :                : 
                               7819                 :                :     /* check query buffer overflow */
 9446 ishii@postgresql.org     7820         [ -  + ]:CBC          94 :     if (sizeof(qbuf) < (sizeof(query) + strlen(encoding)))
 9446 ishii@postgresql.org     7821                 :UBC           0 :         return -1;
                               7822                 :                : 
                               7823                 :                :     /* ok, now send a query */
 9446 ishii@postgresql.org     7824                 :CBC          94 :     sprintf(qbuf, query, encoding);
                               7825                 :             94 :     res = PQexec(conn, qbuf);
                               7826                 :                : 
 8014 neilc@samurai.com        7827         [ -  + ]:             94 :     if (res == NULL)
 9446 ishii@postgresql.org     7828                 :UBC           0 :         return -1;
 9446 ishii@postgresql.org     7829         [ -  + ]:CBC          94 :     if (res->resultStatus != PGRES_COMMAND_OK)
 9446 ishii@postgresql.org     7830                 :UBC           0 :         status = -1;
                               7831                 :                :     else
                               7832                 :                :     {
                               7833                 :                :         /*
                               7834                 :                :          * We rely on the backend to report the parameter value, and we'll
                               7835                 :                :          * change state at that time.
                               7836                 :                :          */
 9379 bruce@momjian.us         7837                 :CBC          94 :         status = 0;             /* everything is ok */
                               7838                 :                :     }
 9446 ishii@postgresql.org     7839                 :             94 :     PQclear(res);
 7279 neilc@samurai.com        7840                 :             94 :     return status;
                               7841                 :                : }
                               7842                 :                : 
                               7843                 :                : PGVerbosity
 8214 tgl@sss.pgh.pa.us        7844                 :           8814 : PQsetErrorVerbosity(PGconn *conn, PGVerbosity verbosity)
                               7845                 :                : {
                               7846                 :                :     PGVerbosity old;
                               7847                 :                : 
                               7848         [ -  + ]:           8814 :     if (!conn)
 8214 tgl@sss.pgh.pa.us        7849                 :UBC           0 :         return PQERRORS_DEFAULT;
 8214 tgl@sss.pgh.pa.us        7850                 :CBC        8814 :     old = conn->verbosity;
                               7851                 :           8814 :     conn->verbosity = verbosity;
                               7852                 :           8814 :     return old;
                               7853                 :                : }
                               7854                 :                : 
                               7855                 :                : PGContextVisibility
 3755                          7856                 :           8749 : PQsetErrorContextVisibility(PGconn *conn, PGContextVisibility show_context)
                               7857                 :                : {
                               7858                 :                :     PGContextVisibility old;
                               7859                 :                : 
                               7860         [ -  + ]:           8749 :     if (!conn)
 3755 tgl@sss.pgh.pa.us        7861                 :UBC           0 :         return PQSHOW_CONTEXT_ERRORS;
 3755 tgl@sss.pgh.pa.us        7862                 :CBC        8749 :     old = conn->show_context;
                               7863                 :           8749 :     conn->show_context = show_context;
                               7864                 :           8749 :     return old;
                               7865                 :                : }
                               7866                 :                : 
                               7867                 :                : PQnoticeReceiver
 8214                          7868                 :           1041 : PQsetNoticeReceiver(PGconn *conn, PQnoticeReceiver proc, void *arg)
                               7869                 :                : {
                               7870                 :                :     PQnoticeReceiver old;
                               7871                 :                : 
                               7872         [ -  + ]:           1041 :     if (conn == NULL)
 8214 tgl@sss.pgh.pa.us        7873                 :UBC           0 :         return NULL;
                               7874                 :                : 
 8214 tgl@sss.pgh.pa.us        7875                 :CBC        1041 :     old = conn->noticeHooks.noticeRec;
                               7876         [ +  - ]:           1041 :     if (proc)
                               7877                 :                :     {
                               7878                 :           1041 :         conn->noticeHooks.noticeRec = proc;
                               7879                 :           1041 :         conn->noticeHooks.noticeRecArg = arg;
                               7880                 :                :     }
                               7881                 :           1041 :     return old;
                               7882                 :                : }
                               7883                 :                : 
                               7884                 :                : PQnoticeProcessor
 9968 bruce@momjian.us         7885                 :           9587 : PQsetNoticeProcessor(PGconn *conn, PQnoticeProcessor proc, void *arg)
                               7886                 :                : {
                               7887                 :                :     PQnoticeProcessor old;
                               7888                 :                : 
 9991                          7889         [ -  + ]:           9587 :     if (conn == NULL)
 9548 bruce@momjian.us         7890                 :UBC           0 :         return NULL;
                               7891                 :                : 
 8214 tgl@sss.pgh.pa.us        7892                 :CBC        9587 :     old = conn->noticeHooks.noticeProc;
 9411                          7893         [ +  - ]:           9587 :     if (proc)
                               7894                 :                :     {
 8214                          7895                 :           9587 :         conn->noticeHooks.noticeProc = proc;
                               7896                 :           9587 :         conn->noticeHooks.noticeProcArg = arg;
                               7897                 :                :     }
 9548 bruce@momjian.us         7898                 :           9587 :     return old;
                               7899                 :                : }
                               7900                 :                : 
                               7901                 :                : /*
                               7902                 :                :  * The default notice message receiver just gets the standard notice text
                               7903                 :                :  * and sends it to the notice processor.  This two-level setup exists
                               7904                 :                :  * mostly for backwards compatibility; perhaps we should deprecate use of
                               7905                 :                :  * PQsetNoticeProcessor?
                               7906                 :                :  */
                               7907                 :                : static void
 8214 tgl@sss.pgh.pa.us        7908                 :          12314 : defaultNoticeReceiver(void *arg, const PGresult *res)
                               7909                 :                : {
                               7910                 :                :     (void) arg;                 /* not used */
 8212                          7911         [ +  - ]:          12314 :     if (res->noticeHooks.noticeProc != NULL)
 3022 peter_e@gmx.net          7912                 :          12314 :         res->noticeHooks.noticeProc(res->noticeHooks.noticeProcArg,
                               7913                 :          12314 :                                     PQresultErrorMessage(res));
 8214 tgl@sss.pgh.pa.us        7914                 :          12314 : }
                               7915                 :                : 
                               7916                 :                : /*
                               7917                 :                :  * The default notice message processor just prints the
                               7918                 :                :  * message on stderr.  Applications can override this if they
                               7919                 :                :  * want the messages to go elsewhere (a window, for example).
                               7920                 :                :  * Note that simply discarding notices is probably a bad idea.
                               7921                 :                :  */
                               7922                 :                : static void
 9968 bruce@momjian.us         7923                 :             65 : defaultNoticeProcessor(void *arg, const char *message)
                               7924                 :                : {
                               7925                 :                :     (void) arg;                 /* not used */
                               7926                 :                :     /* Note: we expect the supplied string to end with a newline already. */
 9991                          7927                 :             65 :     fprintf(stderr, "%s", message);
                               7928                 :             65 : }
                               7929                 :                : 
                               7930                 :                : /*
                               7931                 :                :  * returns a pointer to the next token or NULL if the current
                               7932                 :                :  * token doesn't match
                               7933                 :                :  */
                               7934                 :                : static char *
 2968 peter_e@gmx.net          7935                 :             54 : pwdfMatchesString(char *buf, const char *token)
                               7936                 :                : {
                               7937                 :                :     char       *tbuf;
                               7938                 :                :     const char *ttok;
 8504 bruce@momjian.us         7939                 :             54 :     bool        bslash = false;
                               7940                 :                : 
 8524                          7941   [ +  -  -  + ]:             54 :     if (buf == NULL || token == NULL)
 8524 bruce@momjian.us         7942                 :UBC           0 :         return NULL;
 8524 bruce@momjian.us         7943                 :CBC          54 :     tbuf = buf;
                               7944                 :             54 :     ttok = token;
 6056 tgl@sss.pgh.pa.us        7945   [ +  +  +  - ]:             54 :     if (tbuf[0] == '*' && tbuf[1] == ':')
 8524 bruce@momjian.us         7946                 :             36 :         return tbuf + 2;
                               7947         [ +  - ]:            146 :     while (*tbuf != 0)
                               7948                 :                :     {
                               7949   [ -  +  -  - ]:            146 :         if (*tbuf == '\\' && !bslash)
                               7950                 :                :         {
 8524 bruce@momjian.us         7951                 :UBC           0 :             tbuf++;
                               7952                 :              0 :             bslash = true;
                               7953                 :                :         }
 8524 bruce@momjian.us         7954   [ +  +  +  -  :CBC         146 :         if (*tbuf == ':' && *ttok == 0 && !bslash)
                                              +  - ]
 8504                          7955                 :             13 :             return tbuf + 1;
 8524                          7956                 :            133 :         bslash = false;
                               7957         [ -  + ]:            133 :         if (*ttok == 0)
 8524 bruce@momjian.us         7958                 :UBC           0 :             return NULL;
 8524 bruce@momjian.us         7959         [ +  + ]:CBC         133 :         if (*tbuf == *ttok)
                               7960                 :                :         {
                               7961                 :            128 :             tbuf++;
                               7962                 :            128 :             ttok++;
                               7963                 :                :         }
                               7964                 :                :         else
                               7965                 :              5 :             return NULL;
                               7966                 :                :     }
 8524 bruce@momjian.us         7967                 :UBC           0 :     return NULL;
                               7968                 :                : }
                               7969                 :                : 
                               7970                 :                : /*
                               7971                 :                :  * Get a password from the password file. Return value is malloc'd.
                               7972                 :                :  *
                               7973                 :                :  * On failure, *errmsg is set to an error to be returned.  It is
                               7974                 :                :  * left NULL on success, or if no password could be found.
                               7975                 :                :  */
                               7976                 :                : static char *
   42 michael@paquier.xyz      7977                 :GNC       13527 : passwordFromFile(const char *hostname, const char *port,
                               7978                 :                :                  const char *dbname, const char *username,
                               7979                 :                :                  const char *pgpassfile, const char **errmsg)
                               7980                 :                : {
                               7981                 :                :     FILE       *fp;
                               7982                 :                : #ifndef WIN32
                               7983                 :                :     struct stat stat_buf;
                               7984                 :                : #endif
                               7985                 :                :     PQExpBufferData buf;
                               7986                 :                : 
                               7987                 :          13527 :     *errmsg = NULL;
                               7988                 :                : 
 2694 tgl@sss.pgh.pa.us        7989   [ +  -  -  + ]:CBC       13527 :     if (dbname == NULL || dbname[0] == '\0')
 8524 bruce@momjian.us         7990                 :UBC           0 :         return NULL;
                               7991                 :                : 
 2694 tgl@sss.pgh.pa.us        7992   [ +  -  -  + ]:CBC       13527 :     if (username == NULL || username[0] == '\0')
 8524 bruce@momjian.us         7993                 :UBC           0 :         return NULL;
                               7994                 :                : 
                               7995                 :                :     /* 'localhost' matches pghost of '' or the default socket directory */
 2694 tgl@sss.pgh.pa.us        7996   [ +  -  -  + ]:CBC       13527 :     if (hostname == NULL || hostname[0] == '\0')
 8524 bruce@momjian.us         7997                 :UBC           0 :         hostname = DefaultHost;
 1847 peter@eisentraut.org     7998         [ +  + ]:CBC       13527 :     else if (is_unixsock_path(hostname))
                               7999                 :                : 
                               8000                 :                :         /*
                               8001                 :                :          * We should probably use canonicalize_path(), but then we have to
                               8002                 :                :          * bring path.c into libpq, and it doesn't seem worth it.
                               8003                 :                :          */
 7152 bruce@momjian.us         8004         [ -  + ]:          13126 :         if (strcmp(hostname, DEFAULT_PGSOCKET_DIR) == 0)
 7153 bruce@momjian.us         8005                 :UBC           0 :             hostname = DefaultHost;
                               8006                 :                : 
 2694 tgl@sss.pgh.pa.us        8007   [ +  -  -  + ]:CBC       13527 :     if (port == NULL || port[0] == '\0')
 8524 bruce@momjian.us         8008                 :UBC           0 :         port = DEF_PGPORT_STR;
                               8009                 :                : 
                               8010                 :                :     /* If password file cannot be opened, ignore it. */
  487 peter@eisentraut.org     8011                 :CBC       13527 :     fp = fopen(pgpassfile, "r");
                               8012         [ +  + ]:          13527 :     if (fp == NULL)
 8510 bruce@momjian.us         8013                 :          13517 :         return NULL;
                               8014                 :                : 
                               8015                 :                : #ifndef WIN32
  487 peter@eisentraut.org     8016         [ -  + ]:             10 :     if (fstat(fileno(fp), &stat_buf) != 0)
                               8017                 :                :     {
  443 tgl@sss.pgh.pa.us        8018                 :UBC           0 :         fclose(fp);
  487 peter@eisentraut.org     8019                 :              0 :         return NULL;
                               8020                 :                :     }
                               8021                 :                : 
 7494 bruce@momjian.us         8022         [ -  + ]:CBC          10 :     if (!S_ISREG(stat_buf.st_mode))
                               8023                 :                :     {
 7494 bruce@momjian.us         8024                 :UBC           0 :         fprintf(stderr,
 3100 tgl@sss.pgh.pa.us        8025                 :              0 :                 libpq_gettext("WARNING: password file \"%s\" is not a plain file\n"),
                               8026                 :                :                 pgpassfile);
  443                          8027                 :              0 :         fclose(fp);
 7494 bruce@momjian.us         8028                 :              0 :         return NULL;
                               8029                 :                :     }
                               8030                 :                : 
                               8031                 :                :     /* If password file is insecure, alert the user and ignore it. */
 8510 bruce@momjian.us         8032         [ -  + ]:CBC          10 :     if (stat_buf.st_mode & (S_IRWXG | S_IRWXO))
                               8033                 :                :     {
 8510 bruce@momjian.us         8034                 :UBC           0 :         fprintf(stderr,
 6469 tgl@sss.pgh.pa.us        8035                 :              0 :                 libpq_gettext("WARNING: password file \"%s\" has group or world access; permissions should be u=rw (0600) or less\n"),
                               8036                 :                :                 pgpassfile);
  443                          8037                 :              0 :         fclose(fp);
 8510 bruce@momjian.us         8038                 :              0 :         return NULL;
                               8039                 :                :     }
                               8040                 :                : #else
                               8041                 :                : 
                               8042                 :                :     /*
                               8043                 :                :      * On Win32, the directory is protected, so we don't have to check the
                               8044                 :                :      * file.
                               8045                 :                :      */
                               8046                 :                : #endif
                               8047                 :                : 
                               8048                 :                :     /* Use an expansible buffer to accommodate any reasonable line length */
 1932 tgl@sss.pgh.pa.us        8049                 :CBC          10 :     initPQExpBuffer(&buf);
                               8050                 :                : 
 5767                          8051   [ +  -  +  - ]:             42 :     while (!feof(fp) && !ferror(fp))
                               8052                 :                :     {
                               8053                 :                :         /* Make sure there's a reasonable amount of room in the buffer */
 1932                          8054         [ -  + ]:             42 :         if (!enlargePQExpBuffer(&buf, 128))
                               8055                 :                :         {
   42 michael@paquier.xyz      8056                 :UNC           0 :             *errmsg = libpq_gettext("out of memory");
 1932 tgl@sss.pgh.pa.us        8057                 :UBC           0 :             break;
                               8058                 :                :         }
                               8059                 :                : 
                               8060                 :                :         /* Read some data, appending it to what we already have */
 1932 tgl@sss.pgh.pa.us        8061         [ +  + ]:CBC          42 :         if (fgets(buf.data + buf.len, buf.maxlen - buf.len, fp) == NULL)
 5709                          8062                 :              1 :             break;
 1932                          8063                 :             41 :         buf.len += strlen(buf.data + buf.len);
                               8064                 :                : 
                               8065                 :                :         /* If we don't yet have a whole line, loop around to read more */
                               8066   [ +  -  +  +  :             41 :         if (!(buf.len > 0 && buf.data[buf.len - 1] == '\n') && !feof(fp))
                                              +  + ]
                               8067                 :              8 :             continue;
                               8068                 :                : 
                               8069                 :                :         /* ignore comments */
                               8070         [ +  + ]:             33 :         if (buf.data[0] != '#')
                               8071                 :                :         {
                               8072                 :             25 :             char       *t = buf.data;
                               8073                 :                :             int         len;
                               8074                 :                : 
                               8075                 :                :             /* strip trailing newline and carriage return */
                               8076                 :             25 :             len = pg_strip_crlf(t);
                               8077                 :                : 
                               8078   [ +  +  +  - ]:             39 :             if (len > 0 &&
                               8079         [ +  - ]:             28 :                 (t = pwdfMatchesString(t, hostname)) != NULL &&
                               8080         [ +  + ]:             28 :                 (t = pwdfMatchesString(t, port)) != NULL &&
                               8081         [ +  + ]:             26 :                 (t = pwdfMatchesString(t, dbname)) != NULL &&
                               8082                 :             12 :                 (t = pwdfMatchesString(t, username)) != NULL)
                               8083                 :                :             {
                               8084                 :                :                 /* Found a match. */
                               8085                 :                :                 char       *ret,
                               8086                 :                :                            *p1,
                               8087                 :                :                            *p2;
                               8088                 :                : 
                               8089                 :              9 :                 ret = strdup(t);
                               8090                 :                : 
                               8091                 :              9 :                 fclose(fp);
                               8092                 :              9 :                 explicit_bzero(buf.data, buf.maxlen);
                               8093                 :              9 :                 termPQExpBuffer(&buf);
                               8094                 :                : 
                               8095         [ -  + ]:              9 :                 if (!ret)
                               8096                 :                :                 {
   42 michael@paquier.xyz      8097                 :UNC           0 :                     *errmsg = libpq_gettext("out of memory");
 1932 tgl@sss.pgh.pa.us        8098                 :UBC           0 :                     return NULL;
                               8099                 :                :                 }
                               8100                 :                : 
                               8101                 :                :                 /* De-escape password. */
 1932 tgl@sss.pgh.pa.us        8102   [ +  +  +  + ]:CBC          49 :                 for (p1 = p2 = ret; *p1 != ':' && *p1 != '\0'; ++p1, ++p2)
                               8103                 :                :                 {
                               8104   [ +  +  +  - ]:             40 :                     if (*p1 == '\\' && p1[1] != '\0')
                               8105                 :              3 :                         ++p1;
                               8106                 :             40 :                     *p2 = *p1;
                               8107                 :                :                 }
                               8108                 :              9 :                 *p2 = '\0';
                               8109                 :                : 
                               8110                 :              9 :                 return ret;
                               8111                 :                :             }
                               8112                 :                :         }
                               8113                 :                : 
                               8114                 :                :         /* No match, reset buffer to prepare for next line. */
                               8115                 :             24 :         buf.len = 0;
                               8116                 :                :     }
                               8117                 :                : 
 8524 bruce@momjian.us         8118                 :              1 :     fclose(fp);
 1932 tgl@sss.pgh.pa.us        8119                 :              1 :     explicit_bzero(buf.data, buf.maxlen);
                               8120                 :              1 :     termPQExpBuffer(&buf);
 8524 bruce@momjian.us         8121                 :              1 :     return NULL;
                               8122                 :                : }
                               8123                 :                : 
                               8124                 :                : 
                               8125                 :                : /*
                               8126                 :                :  *  If the connection failed due to bad password, we should mention
                               8127                 :                :  *  if we got the password from the pgpassfile.
                               8128                 :                :  */
                               8129                 :                : static void
 3248 tgl@sss.pgh.pa.us        8130                 :            102 : pgpassfileWarning(PGconn *conn)
                               8131                 :                : {
                               8132                 :                :     /* If it was 'invalid authorization', add pgpassfile mention */
                               8133                 :                :     /* only works with >= 9.0 servers */
 2689                          8134         [ +  + ]:            102 :     if (conn->password_needed &&
                               8135         [ -  + ]:              6 :         conn->connhost[conn->whichhost].password != NULL &&
 2689 tgl@sss.pgh.pa.us        8136         [ #  # ]:UBC           0 :         conn->result)
                               8137                 :                :     {
 3239                          8138                 :              0 :         const char *sqlstate = PQresultErrorField(conn->result,
                               8139                 :                :                                                   PG_DIAG_SQLSTATE);
                               8140                 :                : 
                               8141   [ #  #  #  # ]:              0 :         if (sqlstate && strcmp(sqlstate, ERRCODE_INVALID_PASSWORD) == 0)
 1127 peter@eisentraut.org     8142                 :              0 :             libpq_append_conn_error(conn, "password retrieved from file \"%s\"",
                               8143                 :                :                                     conn->pgpassfile);
                               8144                 :                :     }
 5757 bruce@momjian.us         8145                 :CBC         102 : }
                               8146                 :                : 
                               8147                 :                : /*
                               8148                 :                :  * Check if the SSL protocol value given in input is valid or not.
                               8149                 :                :  * This is used as a sanity check routine for the connection parameters
                               8150                 :                :  * ssl_min_protocol_version and ssl_max_protocol_version.
                               8151                 :                :  */
                               8152                 :                : static bool
 2149 michael@paquier.xyz      8153                 :          53911 : sslVerifyProtocolVersion(const char *version)
                               8154                 :                : {
                               8155                 :                :     /*
                               8156                 :                :      * An empty string and a NULL value are considered valid as it is
                               8157                 :                :      * equivalent to ignoring the parameter.
                               8158                 :                :      */
                               8159   [ +  +  -  + ]:          53911 :     if (!version || strlen(version) == 0)
                               8160                 :          26948 :         return true;
                               8161                 :                : 
                               8162   [ +  -  +  + ]:          53926 :     if (pg_strcasecmp(version, "TLSv1") == 0 ||
                               8163         [ +  + ]:          53924 :         pg_strcasecmp(version, "TLSv1.1") == 0 ||
                               8164         [ -  + ]:          26963 :         pg_strcasecmp(version, "TLSv1.2") == 0 ||
                               8165                 :              2 :         pg_strcasecmp(version, "TLSv1.3") == 0)
                               8166                 :          26961 :         return true;
                               8167                 :                : 
                               8168                 :                :     /* anything else is wrong */
                               8169                 :              2 :     return false;
                               8170                 :                : }
                               8171                 :                : 
                               8172                 :                : 
                               8173                 :                : /*
                               8174                 :                :  * Ensure that the SSL protocol range given in input is correct.  The check
                               8175                 :                :  * is performed on the input string to keep it TLS backend agnostic.  Input
                               8176                 :                :  * to this function is expected verified with sslVerifyProtocolVersion().
                               8177                 :                :  */
                               8178                 :                : static bool
                               8179                 :          13477 : sslVerifyProtocolRange(const char *min, const char *max)
                               8180                 :                : {
                               8181   [ +  -  +  - ]:          13477 :     Assert(sslVerifyProtocolVersion(min) &&
                               8182                 :                :            sslVerifyProtocolVersion(max));
                               8183                 :                : 
                               8184                 :                :     /* If at least one of the bounds is not set, the range is valid */
                               8185   [ +  -  +  +  :          13477 :     if (min == NULL || max == NULL || strlen(min) == 0 || strlen(max) == 0)
                                        +  -  -  + ]
                               8186                 :          13474 :         return true;
                               8187                 :                : 
                               8188                 :                :     /*
                               8189                 :                :      * If the minimum version is the lowest one we accept, then all options
                               8190                 :                :      * for the maximum are valid.
                               8191                 :                :      */
                               8192         [ -  + ]:              3 :     if (pg_strcasecmp(min, "TLSv1") == 0)
 2149 michael@paquier.xyz      8193                 :UBC           0 :         return true;
                               8194                 :                : 
                               8195                 :                :     /*
                               8196                 :                :      * The minimum bound is valid, and cannot be TLSv1, so using TLSv1 for the
                               8197                 :                :      * maximum is incorrect.
                               8198                 :                :      */
 2149 michael@paquier.xyz      8199         [ -  + ]:CBC           3 :     if (pg_strcasecmp(max, "TLSv1") == 0)
 2149 michael@paquier.xyz      8200                 :UBC           0 :         return false;
                               8201                 :                : 
                               8202                 :                :     /*
                               8203                 :                :      * At this point we know that we have a mix of TLSv1.1 through 1.3
                               8204                 :                :      * versions.
                               8205                 :                :      */
 2149 michael@paquier.xyz      8206         [ +  + ]:CBC           3 :     if (pg_strcasecmp(min, max) > 0)
                               8207                 :              1 :         return false;
                               8208                 :                : 
                               8209                 :              2 :     return true;
                               8210                 :                : }
                               8211                 :                : 
                               8212                 :                : 
                               8213                 :                : /*
                               8214                 :                :  * Obtain user's home directory, return in given buffer
                               8215                 :                :  *
                               8216                 :                :  * On Unix, this actually returns the user's home directory.  On Windows
                               8217                 :                :  * it returns the PostgreSQL-specific application data folder.
                               8218                 :                :  *
                               8219                 :                :  * This is essentially the same as get_home_path(), but we don't use that
                               8220                 :                :  * because we don't want to pull path.c into libpq (it pollutes application
                               8221                 :                :  * namespace).
                               8222                 :                :  *
                               8223                 :                :  * Returns true on success, false on failure to obtain the directory name.
                               8224                 :                :  *
                               8225                 :                :  * CAUTION: although in most situations failure is unexpected, there are users
                               8226                 :                :  * who like to run applications in a home-directory-less environment.  On
                               8227                 :                :  * failure, you almost certainly DO NOT want to report an error.  Just act as
                               8228                 :                :  * though whatever file you were hoping to find in the home directory isn't
                               8229                 :                :  * there (which it isn't).
                               8230                 :                :  */
                               8231                 :                : bool
 7649 tgl@sss.pgh.pa.us        8232                 :          13249 : pqGetHomeDirectory(char *buf, int bufsize)
                               8233                 :                : {
                               8234                 :                : #ifndef WIN32
                               8235                 :                :     const char *home;
                               8236                 :                : 
 1437                          8237                 :          13249 :     home = getenv("HOME");
  470 peter@eisentraut.org     8238   [ +  -  +  - ]:          13249 :     if (home && home[0])
                               8239                 :                :     {
                               8240                 :          13249 :         strlcpy(buf, home, bufsize);
                               8241                 :          13249 :         return true;
                               8242                 :                :     }
                               8243                 :                :     else
                               8244                 :                :     {
                               8245                 :                :         struct passwd pwbuf;
                               8246                 :                :         struct passwd *pw;
                               8247                 :                :         char        tmpbuf[1024];
                               8248                 :                :         int         rc;
                               8249                 :                : 
  470 peter@eisentraut.org     8250                 :UBC           0 :         rc = getpwuid_r(geteuid(), &pwbuf, tmpbuf, sizeof tmpbuf, &pw);
                               8251   [ #  #  #  # ]:              0 :         if (rc != 0 || !pw)
                               8252                 :              0 :             return false;
                               8253                 :              0 :         strlcpy(buf, pw->pw_dir, bufsize);
                               8254                 :              0 :         return true;
                               8255                 :                :     }
                               8256                 :                : #else
                               8257                 :                :     char        tmppath[MAX_PATH];
                               8258                 :                : 
                               8259                 :                :     ZeroMemory(tmppath, sizeof(tmppath));
                               8260                 :                :     if (SHGetFolderPath(NULL, CSIDL_APPDATA, NULL, 0, tmppath) != S_OK)
                               8261                 :                :         return false;
                               8262                 :                :     snprintf(buf, bufsize, "%s/postgresql", tmppath);
                               8263                 :                :     return true;
                               8264                 :                : #endif
                               8265                 :                : }
                               8266                 :                : 
                               8267                 :                : /*
                               8268                 :                :  * Parse and try to interpret "value" as an integer value, and if successful,
                               8269                 :                :  * store it in *result, complaining if there is any trailing garbage or an
                               8270                 :                :  * overflow.  This allows any number of leading and trailing whitespaces.
                               8271                 :                :  */
                               8272                 :                : bool
  687 alvherre@alvh.no-ip.     8273                 :CBC       13490 : pqParseIntParam(const char *value, int *result, PGconn *conn,
                               8274                 :                :                 const char *context)
                               8275                 :                : {
                               8276                 :                :     char       *end;
                               8277                 :                :     long        numval;
                               8278                 :                : 
                               8279         [ -  + ]:          13490 :     Assert(value != NULL);
                               8280                 :                : 
                               8281                 :          13490 :     *result = 0;
                               8282                 :                : 
                               8283                 :                :     /* strtol(3) skips leading whitespaces */
                               8284                 :          13490 :     errno = 0;
                               8285                 :          13490 :     numval = strtol(value, &end, 10);
                               8286                 :                : 
                               8287                 :                :     /*
                               8288                 :                :      * If no progress was done during the parsing or an error happened, fail.
                               8289                 :                :      * This tests properly for overflows of the result.
                               8290                 :                :      */
                               8291   [ +  -  +  -  :          13490 :     if (value == end || errno != 0 || numval != (int) numval)
                                              -  + ]
  687 alvherre@alvh.no-ip.     8292                 :UBC           0 :         goto error;
                               8293                 :                : 
                               8294                 :                :     /*
                               8295                 :                :      * Skip any trailing whitespace; if anything but whitespace remains before
                               8296                 :                :      * the terminating character, fail
                               8297                 :                :      */
  687 alvherre@alvh.no-ip.     8298   [ -  +  -  - ]:CBC       13490 :     while (*end != '\0' && isspace((unsigned char) *end))
  687 alvherre@alvh.no-ip.     8299                 :UBC           0 :         end++;
                               8300                 :                : 
  687 alvherre@alvh.no-ip.     8301         [ -  + ]:CBC       13490 :     if (*end != '\0')
  687 alvherre@alvh.no-ip.     8302                 :UBC           0 :         goto error;
                               8303                 :                : 
  687 alvherre@alvh.no-ip.     8304                 :CBC       13490 :     *result = numval;
                               8305                 :          13490 :     return true;
                               8306                 :                : 
  687 alvherre@alvh.no-ip.     8307                 :UBC           0 : error:
                               8308                 :              0 :     libpq_append_conn_error(conn, "invalid integer value \"%s\" for connection option \"%s\"",
                               8309                 :                :                             value, context);
                               8310                 :              0 :     return false;
                               8311                 :                : }
                               8312                 :                : 
                               8313                 :                : /*
                               8314                 :                :  * Parse and try to interpret "value" as a ProtocolVersion value, and if
                               8315                 :                :  * successful, store it in *result.
                               8316                 :                :  */
                               8317                 :                : static bool
  258 heikki.linnakangas@i     8318                 :CBC          24 : pqParseProtocolVersion(const char *value, ProtocolVersion *result, PGconn *conn,
                               8319                 :                :                        const char *context)
                               8320                 :                : {
                               8321         [ +  + ]:             24 :     if (strcmp(value, "latest") == 0)
                               8322                 :                :     {
                               8323                 :             17 :         *result = PG_PROTOCOL_LATEST;
                               8324                 :             17 :         return true;
                               8325                 :                :     }
                               8326         [ +  + ]:              7 :     if (strcmp(value, "3.0") == 0)
                               8327                 :                :     {
                               8328                 :              5 :         *result = PG_PROTOCOL(3, 0);
                               8329                 :              5 :         return true;
                               8330                 :                :     }
                               8331                 :                : 
                               8332                 :                :     /* 3.1 never existed, we went straight from 3.0 to 3.2 */
                               8333                 :                : 
                               8334         [ +  + ]:              2 :     if (strcmp(value, "3.2") == 0)
                               8335                 :                :     {
                               8336                 :              1 :         *result = PG_PROTOCOL(3, 2);
                               8337                 :              1 :         return true;
                               8338                 :                :     }
                               8339                 :                : 
                               8340                 :              1 :     libpq_append_conn_error(conn, "invalid %s value: \"%s\"",
                               8341                 :                :                             context, value);
                               8342                 :              1 :     return false;
                               8343                 :                : }
                               8344                 :                : 
                               8345                 :                : /*
                               8346                 :                :  * To keep the API consistent, the locking stubs are always provided, even
                               8347                 :                :  * if they are not required.
                               8348                 :                :  *
                               8349                 :                :  * Since we neglected to provide any error-return convention in the
                               8350                 :                :  * pgthreadlock_t API, we can't do much except Assert upon failure of any
                               8351                 :                :  * mutex primitive.  Fortunately, such failures appear to be nonexistent in
                               8352                 :                :  * the field.
                               8353                 :                :  */
                               8354                 :                : 
                               8355                 :                : static void
 7937 bruce@momjian.us         8356                 :             46 : default_threadlock(int acquire)
                               8357                 :                : {
                               8358                 :                :     static pthread_mutex_t singlethread_lock = PTHREAD_MUTEX_INITIALIZER;
                               8359                 :                : 
                               8360         [ +  + ]:             46 :     if (acquire)
                               8361                 :                :     {
 6423 magnus@hagander.net      8362         [ -  + ]:             23 :         if (pthread_mutex_lock(&singlethread_lock))
 1631 tgl@sss.pgh.pa.us        8363                 :UBC           0 :             Assert(false);
                               8364                 :                :     }
                               8365                 :                :     else
                               8366                 :                :     {
 6423 magnus@hagander.net      8367         [ -  + ]:CBC          23 :         if (pthread_mutex_unlock(&singlethread_lock))
 1631 tgl@sss.pgh.pa.us        8368                 :UBC           0 :             Assert(false);
                               8369                 :                :     }
 7937 bruce@momjian.us         8370                 :CBC          46 : }
                               8371                 :                : 
                               8372                 :                : pgthreadlock_t
 7684 tgl@sss.pgh.pa.us        8373                 :UBC           0 : PQregisterThreadLock(pgthreadlock_t newhandler)
                               8374                 :                : {
                               8375                 :              0 :     pgthreadlock_t prev = pg_g_threadlock;
                               8376                 :                : 
 7937 bruce@momjian.us         8377         [ #  # ]:              0 :     if (newhandler)
 7684 tgl@sss.pgh.pa.us        8378                 :              0 :         pg_g_threadlock = newhandler;
                               8379                 :                :     else
                               8380                 :              0 :         pg_g_threadlock = default_threadlock;
                               8381                 :                : 
 7937 bruce@momjian.us         8382                 :              0 :     return prev;
                               8383                 :                : }
        

Generated by: LCOV version 2.4-beta