33 #include "pg_config_paths.h" 41 #define _WIN32_IE 0x0500 54 #ifdef HAVE_NETINET_TCP_H 55 #include <netinet/tcp.h> 59 #ifdef ENABLE_THREAD_SAFETY 72 #define LDAP_DEPRECATED 1 74 typedef struct timeval LDAP_TIMEVAL;
81 #define PGPASSFILE ".pgpass" 83 #define PGPASSFILE "pgpass.conf" 92 #define ERRCODE_APPNAME_UNKNOWN "42704" 95 #define ERRCODE_INVALID_PASSWORD "28P01" 97 #define ERRCODE_CANNOT_CONNECT_NOW "57P03" 103 #if defined(TCP_KEEPIDLE) 105 #define PG_TCP_KEEPALIVE_IDLE TCP_KEEPIDLE 106 #define PG_TCP_KEEPALIVE_IDLE_STR "TCP_KEEPIDLE" 107 #elif defined(TCP_KEEPALIVE_THRESHOLD) 109 #define PG_TCP_KEEPALIVE_IDLE TCP_KEEPALIVE_THRESHOLD 110 #define PG_TCP_KEEPALIVE_IDLE_STR "TCP_KEEPALIVE_THRESHOLD" 111 #elif defined(TCP_KEEPALIVE) && defined(__darwin__) 114 #define PG_TCP_KEEPALIVE_IDLE TCP_KEEPALIVE 115 #define PG_TCP_KEEPALIVE_IDLE_STR "TCP_KEEPALIVE" 122 #define DefaultHost "localhost" 123 #define DefaultTty "" 124 #define DefaultOption "" 125 #define DefaultAuthtype "" 127 #define DefaultChannelBinding "prefer" 129 #define DefaultChannelBinding "disable" 131 #define DefaultTargetSessionAttrs "any" 133 #define DefaultSSLMode "prefer" 135 #define DefaultSSLMode "disable" 139 #define DefaultGSSMode "prefer" 141 #define DefaultGSSMode "disable" 201 "Database-Authtype",
"D", 20, -1},
203 {
"service",
"PGSERVICE", NULL, NULL,
204 "Database-Service",
"", 20, -1},
206 {
"user",
"PGUSER", NULL, NULL,
207 "Database-User",
"", 20,
210 {
"password",
"PGPASSWORD", NULL, NULL,
211 "Database-Password",
"*", 20,
214 {
"passfile",
"PGPASSFILE", NULL, NULL,
215 "Database-Password-File",
"", 64,
219 "Channel-Binding",
"", 8,
222 {
"connect_timeout",
"PGCONNECT_TIMEOUT", NULL, NULL,
223 "Connect-timeout",
"", 10,
226 {
"dbname",
"PGDATABASE", NULL, NULL,
227 "Database-Name",
"", 20,
230 {
"host",
"PGHOST", NULL, NULL,
231 "Database-Host",
"", 40,
234 {
"hostaddr",
"PGHOSTADDR", NULL, NULL,
235 "Database-Host-IP-Address",
"", 45,
238 {
"port",
"PGPORT", DEF_PGPORT_STR, NULL,
239 "Database-Port",
"", 6,
242 {
"client_encoding",
"PGCLIENTENCODING", NULL, NULL,
243 "Client-Encoding",
"", 10,
251 "Backend-Debug-TTY",
"D", 40,
255 "Backend-Options",
"", 40,
258 {
"application_name",
"PGAPPNAME", NULL, NULL,
259 "Application-Name",
"", 64,
262 {
"fallback_application_name", NULL, NULL, NULL,
263 "Fallback-Application-Name",
"", 64,
266 {
"keepalives", NULL, NULL, NULL,
267 "TCP-Keepalives",
"", 1,
270 {
"keepalives_idle", NULL, NULL, NULL,
271 "TCP-Keepalives-Idle",
"", 10,
274 {
"keepalives_interval", NULL, NULL, NULL,
275 "TCP-Keepalives-Interval",
"", 10,
278 {
"keepalives_count", NULL, NULL, NULL,
279 "TCP-Keepalives-Count",
"", 10,
282 {
"tcp_user_timeout", NULL, NULL, NULL,
283 "TCP-User-Timeout",
"", 10,
296 {
"sslcompression",
"PGSSLCOMPRESSION",
"0", NULL,
297 "SSL-Compression",
"", 1,
300 {
"sslcert",
"PGSSLCERT", NULL, NULL,
301 "SSL-Client-Cert",
"", 64,
304 {
"sslkey",
"PGSSLKEY", NULL, NULL,
305 "SSL-Client-Key",
"", 64,
308 {
"sslpassword", NULL, NULL, NULL,
309 "SSL-Client-Key-Password",
"*", 20,
312 {
"sslrootcert",
"PGSSLROOTCERT", NULL, NULL,
313 "SSL-Root-Certificate",
"", 64,
316 {
"sslcrl",
"PGSSLCRL", NULL, NULL,
317 "SSL-Revocation-List",
"", 64,
320 {
"sslcrldir",
"PGSSLCRLDIR", NULL, NULL,
321 "SSL-Revocation-List-Dir",
"", 64,
324 {
"requirepeer",
"PGREQUIREPEER", NULL, NULL,
325 "Require-Peer",
"", 10,
328 {
"ssl_min_protocol_version",
"PGSSLMINPROTOCOLVERSION",
"TLSv1.2", NULL,
329 "SSL-Minimum-Protocol-Version",
"", 8,
332 {
"ssl_max_protocol_version",
"PGSSLMAXPROTOCOLVERSION", NULL, NULL,
333 "SSL-Maximum-Protocol-Version",
"", 8,
341 "GSSENC-Mode",
"", 8,
345 {
"krbsrvname",
"PGKRBSRVNAME", PG_KRB_SRVNAM, NULL,
346 "Kerberos-service-name",
"", 20,
349 {
"gsslib",
"PGGSSLIB", NULL, NULL,
350 "GSS-library",
"", 7,
353 {
"replication", NULL, NULL, NULL,
354 "Replication",
"D", 5,
357 {
"target_session_attrs",
"PGTARGETSESSIONATTRS",
359 "Target-Session-Attrs",
"", 11,
363 {NULL, NULL, NULL, NULL,
371 "PGDATESTYLE",
"datestyle" 409 bool use_defaults,
int expand_dbname);
425 PQExpBuffer errorMessage,
bool ignoreMissing,
bool uri_decode);
439 const char *
username,
const char *pgpassfile);
485 if (conn->gcred != GSS_C_NO_CREDENTIAL)
487 gss_release_cred(&min_s, &conn->gcred);
488 conn->gcred = GSS_C_NO_CREDENTIAL;
491 gss_delete_sec_context(&min_s, &conn->gctx, GSS_C_NO_BUFFER);
493 gss_release_name(&min_s, &conn->gtarg_nam);
494 if (conn->gss_SendBuffer)
496 free(conn->gss_SendBuffer);
497 conn->gss_SendBuffer = NULL;
499 if (conn->gss_RecvBuffer)
501 free(conn->gss_RecvBuffer);
502 conn->gss_RecvBuffer = NULL;
504 if (conn->gss_ResultBuffer)
506 free(conn->gss_ResultBuffer);
507 conn->gss_ResultBuffer = NULL;
509 conn->gssenc =
false;
513 if (conn->sspitarget)
515 free(conn->sspitarget);
516 conn->sspitarget = NULL;
520 FreeCredentialsHandle(conn->sspicred);
521 free(conn->sspicred);
522 conn->sspicred = NULL;
526 DeleteSecurityContext(conn->sspictx);
528 conn->sspictx = NULL;
565 while (notify != NULL)
569 notify = notify->
next;
576 while (pstatus != NULL)
580 pstatus = pstatus->
next;
652 const char *
const *
values,
671 const char *
const *
values,
755 const char *
const *
values,
775 true, expand_dbname);
776 if (connOptions == NULL)
885 for (option = PQconninfoOptions; option->
keyword; option++)
893 char **connmember = (
char **) ((
char *) conn + option->
connofs);
897 *connmember = strdup(tmp);
898 if (*connmember == NULL)
931 if (connOptions == NULL)
965 for (; *input !=
'\0'; input++)
996 while (*e !=
'\0' && *e !=
',')
1001 p = (
char *)
malloc(
sizeof(
char) * (len + 1));
1051 for (i = 0; i < conn->
nconnhost && more; i++)
1072 for (i = 0; i < conn->
nconnhost && more; i++)
1084 libpq_gettext(
"could not match %d host names to %d hostaddr values\n"),
1100 else if (ch->
host != NULL && ch->
host[0] !=
'\0')
1103 #ifdef HAVE_UNIX_SOCKETS 1112 #ifdef HAVE_UNIX_SOCKETS 1124 if (ch->
host == NULL)
1141 for (i = 0; i < conn->
nconnhost && more; i++)
1152 if (i == 1 && !more)
1165 libpq_gettext(
"could not match %d port numbers to %d hosts\n"),
1234 if (pwhost == NULL || pwhost[0] ==
'\0')
1275 if (strcmp(conn->
sslmode,
"disable") != 0
1276 && strcmp(conn->
sslmode,
"allow") != 0
1277 && strcmp(conn->
sslmode,
"prefer") != 0
1278 && strcmp(conn->
sslmode,
"require") != 0
1279 && strcmp(conn->
sslmode,
"verify-ca") != 0
1280 && strcmp(conn->
sslmode,
"verify-full") != 0)
1305 libpq_gettext(
"sslmode value \"%s\" invalid when SSL support is not compiled in\n"),
1327 "ssl_min_protocol_version",
1336 "ssl_max_protocol_version",
1362 if (strcmp(conn->
gssencmode,
"disable") != 0 &&
1374 if (strcmp(conn->
gssencmode,
"require") == 0)
1378 libpq_gettext(
"gssencmode value \"%s\" invalid when GSSAPI support is not compiled in\n"),
1414 "target_settion_attrs",
1464 if (connOptions != NULL)
1492 const char *pgtty,
const char *
dbName,
const char *
login,
1525 if (dbName && dbName[0] !=
'\0')
1529 conn->
dbName = strdup(dbName);
1539 if (pghost && pghost[0] !=
'\0')
1543 conn->
pghost = strdup(pghost);
1548 if (pgport && pgport[0] !=
'\0')
1552 conn->
pgport = strdup(pgport);
1557 if (pgoptions && pgoptions[0] !=
'\0')
1566 if (pgtty && pgtty[0] !=
'\0')
1570 conn->
pgtty = strdup(pgtty);
1575 if (login && login[0] !=
'\0')
1579 conn->
pguser = strdup(login);
1584 if (pwd && pwd[0] !=
'\0')
1588 conn->
pgpass = strdup(pwd);
1627 if (setsockopt(conn->
sock, IPPROTO_TCP, TCP_NODELAY,
1634 libpq_gettext(
"could not set socket to TCP no delay mode: %s\n"),
1653 if (addr->ss_family == AF_INET)
1656 &((
struct sockaddr_in *) addr)->sin_addr.s_addr,
1658 host_addr, host_addr_len) == NULL)
1659 host_addr[0] =
'\0';
1662 else if (addr->ss_family == AF_INET6)
1665 &((
struct sockaddr_in6 *) addr)->sin6_addr.s6_addr,
1667 host_addr, host_addr_len) == NULL)
1668 host_addr[0] =
'\0';
1672 host_addr[0] =
'\0';
1686 #ifdef HAVE_UNIX_SOCKETS 1693 service,
sizeof(service),
1696 libpq_gettext(
"connection to server on socket \"%s\" failed: "),
1702 const char *displayed_host;
1703 const char *displayed_port;
1711 if (displayed_port == NULL || displayed_port[0] ==
'\0')
1712 displayed_port = DEF_PGPORT_STR;
1721 strcmp(displayed_host, host_addr) != 0)
1723 libpq_gettext(
"connection to server at \"%s\" (%s), port %s failed: "),
1724 displayed_host, host_addr,
1728 libpq_gettext(
"connection to server at \"%s\", port %s failed: "),
1750 #ifdef HAVE_UNIX_SOCKETS 1753 libpq_gettext(
"\tIs the server running locally and accepting connections on that socket?\n"));
1757 libpq_gettext(
"\tIs the server running on that host and accepting TCP/IP connections?\n"));
1776 return val != 0 ? 1 : 0;
1786 const char *context)
1797 numval = strtol(value, &end, 10);
1803 if (value == end || errno != 0 || numval != (
int) numval)
1810 while (*end !=
'\0' && isspace((
unsigned char) *end))
1821 libpq_gettext(
"invalid integer value \"%s\" for connection option \"%s\"\n"),
1844 #ifdef PG_TCP_KEEPALIVE_IDLE 1845 if (setsockopt(conn->
sock, IPPROTO_TCP, PG_TCP_KEEPALIVE_IDLE,
1846 (
char *) &idle,
sizeof(idle)) < 0)
1852 PG_TCP_KEEPALIVE_IDLE_STR,
1873 "keepalives_interval"))
1878 #ifdef TCP_KEEPINTVL 1879 if (setsockopt(conn->
sock, IPPROTO_TCP, TCP_KEEPINTVL,
1880 (
char *) &interval,
sizeof(interval)) < 0)
1908 "keepalives_count"))
1914 if (setsockopt(conn->
sock, IPPROTO_TCP, TCP_KEEPCNT,
1915 (
char *) &count,
sizeof(count)) < 0)
1930 #ifdef SIO_KEEPALIVE_VALS 1938 struct tcp_keepalive ka;
1952 "keepalives_interval"))
1958 ka.keepalivetime = idle * 1000;
1959 ka.keepaliveinterval = interval * 1000;
1961 if (WSAIoctl(conn->
sock,
1973 libpq_gettext(
"WSAIoctl(SIO_KEEPALIVE_VALS) failed: %ui\n"),
1994 "tcp_user_timeout"))
2000 #ifdef TCP_USER_TIMEOUT 2001 if (setsockopt(conn->
sock, IPPROTO_TCP, TCP_USER_TIMEOUT,
2002 (
char *) &timeout,
sizeof(timeout)) < 0)
2031 goto connect_errReturn;
2042 "libpq is incorrectly linked to backend functions\n");
2043 goto connect_errReturn;
2094 time_t finish_time = ((time_t) -1);
2096 int last_whichhost = -2;
2097 struct addrinfo *last_addr_cur = NULL;
2143 finish_time = time(NULL) + timeout;
2229 bool reset_connection_state_machine =
false;
2230 bool need_new_connection =
false;
2284 libpq_gettext(
"invalid connection state, probably indicative of memory corruption\n"));
2298 reset_connection_state_machine =
true;
2335 MemSet(&hint, 0,
sizeof(hint));
2340 if (ch->
port == NULL || ch->
port[0] ==
'\0')
2341 thisport = DEF_PGPORT;
2347 if (thisport < 1 || thisport > 65535)
2355 snprintf(portstr,
sizeof(portstr),
"%d", thisport);
2366 libpq_gettext(
"could not translate host name \"%s\" to address: %s\n"),
2379 libpq_gettext(
"could not parse network address \"%s\": %s\n"),
2386 #ifdef HAVE_UNIX_SOCKETS 2392 libpq_gettext(
"Unix-domain socket path \"%s\" is too long (maximum %d bytes)\n"),
2407 libpq_gettext(
"could not translate Unix-domain socket path \"%s\" to address: %s\n"),
2419 reset_connection_state_machine =
true;
2424 if (reset_connection_state_machine)
2436 conn->allow_ssl_try = (conn->
sslmode[0] !=
'd');
2437 conn->wait_ssl_try = (conn->
sslmode[0] ==
'a');
2440 conn->try_gss = (conn->
gssencmode[0] !=
'd');
2443 reset_connection_state_machine =
false;
2444 need_new_connection =
true;
2448 if (need_new_connection)
2464 need_new_connection =
false;
2488 if (addr_cur == NULL)
2503 if (conn->
connip != NULL)
2510 conn->
connip = strdup(host_addr);
2524 if (addr_cur->
ai_next != NULL ||
2563 libpq_gettext(
"could not set socket to nonblocking mode: %s\n"),
2570 if (fcntl(conn->
sock, F_SETFD, FD_CLOEXEC) == -1)
2573 libpq_gettext(
"could not set socket to close-on-exec mode: %s\n"),
2588 if (usekeepalives < 0)
2591 libpq_gettext(
"keepalives parameter must be an integer\n"));
2594 else if (usekeepalives == 0)
2599 else if (setsockopt(conn->
sock,
2600 SOL_SOCKET, SO_KEEPALIVE,
2601 (
char *) &on,
sizeof(on)) < 0)
2614 #ifdef SIO_KEEPALIVE_VALS 2615 else if (!setKeepalivesWin32(conn))
2661 if (setsockopt(conn->
sock, SOL_SOCKET, SO_NOSIGPIPE,
2662 (
char *) &optval,
sizeof(optval)) == 0)
2715 ACCEPT_TYPE_ARG3 optlen =
sizeof(optval);
2727 if (getsockopt(conn->
sock, SOL_SOCKET, SO_ERROR,
2728 (
char *) &optval, &optlen) == -1)
2735 else if (optval != 0)
2754 if (getsockname(conn->
sock,
2759 libpq_gettext(
"could not get client address from socket: %s\n"),
2784 char pwdbuf[BUFSIZ];
2785 struct passwd pass_buf;
2786 struct passwd *pass;
2799 if (errno == ENOSYS)
2801 libpq_gettext(
"requirepeer parameter is not supported on this platform\n"));
2810 passerr =
pqGetpwuid(uid, &pass_buf, pwdbuf,
sizeof(pwdbuf), &pass);
2825 if (strcmp(pass->pw_name, conn->
requirepeer) != 0)
2828 libpq_gettext(
"requirepeer specifies \"%s\", but actual peer user name is \"%s\"\n"),
2842 conn->allow_ssl_try =
false;
2845 conn->try_gss =
false;
2859 if (conn->try_gss && !conn->gctx)
2861 if (conn->try_gss && !conn->gctx)
2868 libpq_gettext(
"could not send GSSAPI negotiation packet: %s\n"),
2877 else if (!conn->gctx && conn->
gssencmode[0] ==
'r')
2880 libpq_gettext(
"GSSAPI encryption required but was impossible (possibly no credential cache, no server support, or using a local socket)\n"));
2892 if (conn->allow_ssl_try && !conn->wait_ssl_try &&
2912 libpq_gettext(
"could not send SSL negotiation packet: %s\n"),
2927 EnvironmentOptions);
2930 EnvironmentOptions);
2993 if (
pqGetc(&SSLok, conn) < 0)
3006 else if (SSLok ==
'N')
3011 if (conn->
sslmode[0] ==
'r' ||
3017 libpq_gettext(
"server does not support SSL, but SSL was required\n"));
3021 conn->allow_ssl_try =
false;
3026 else if (SSLok ==
'E')
3044 libpq_gettext(
"received invalid response to SSL negotiation: %c\n"),
3067 && conn->allow_ssl_try
3068 && !conn->wait_ssl_try)
3071 conn->allow_ssl_try =
false;
3072 need_new_connection =
true;
3095 if (conn->try_gss && !conn->gctx)
3103 else if (rdresult == 0)
3106 if (
pqGetc(&gss_ok, conn) < 0)
3119 conn->try_gss =
false;
3120 need_new_connection =
true;
3133 libpq_gettext(
"server doesn't support GSSAPI encryption, but it was required\n"));
3137 conn->try_gss =
false;
3142 else if (gss_ok !=
'G')
3145 libpq_gettext(
"received invalid response to GSSAPI negotiation: %c\n"),
3166 conn->try_gss =
false;
3167 need_new_connection =
true;
3197 if (
pqGetc(&beresp, conn))
3208 if (!(beresp ==
'R' || beresp ==
'E'))
3211 libpq_gettext(
"expected authentication request from server, but received %c\n"),
3239 if (beresp ==
'R' && (msgLength < 8 || msgLength > 2000))
3242 libpq_gettext(
"expected authentication request from server, but received %c\n"),
3247 if (beresp ==
'E' && (msgLength < 8 || msgLength > 30000))
3272 need_new_connection =
true;
3287 if (avail < msgLength)
3346 if (conn->gssenc && conn->
gssencmode[0] ==
'p')
3349 conn->try_gss =
false;
3350 need_new_connection =
true;
3363 && conn->allow_ssl_try
3364 && conn->wait_ssl_try)
3367 conn->wait_ssl_try =
false;
3368 need_new_connection =
true;
3378 && conn->allow_ssl_try
3379 && !conn->wait_ssl_try)
3382 conn->allow_ssl_try =
false;
3383 need_new_connection =
true;
3395 if (
pqGetInt((
int *) &areq, 4, conn))
3496 libpq_gettext(
"unexpected message from server during startup\n"));
3510 const char *sqlstate;
3518 need_new_connection =
true;
3571 "SHOW transaction_read_only"))
3667 if (strncmp(val,
"on", 2) == 0)
3708 libpq_gettext(
"test \"SHOW transaction_read_only\" failed\n"));
3722 "probably indicative of memory corruption\n"),
3828 static bool wsastartup_done =
false;
3830 if (!wsastartup_done)
3834 if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
3836 wsastartup_done =
true;
3915 for (i = 0; i < conn->
nEvents; i++)
4155 for (i = 0; i < conn->
nEvents; i++)
4165 libpq_gettext(
"PGEventProc \"%s\" failed during PGEVT_CONNRESET event\n"),
4215 for (i = 0; i < conn->
nEvents; i++)
4225 libpq_gettext(
"PGEventProc \"%s\" failed during PGEVT_CONNRESET event\n"),
4300 char *errbuf,
int errbufsize)
4318 strlcpy(errbuf,
"PQcancel() -- socket() failed: ", errbufsize);
4319 goto cancel_errReturn;
4322 if (
connect(tmpsock, (
struct sockaddr *) &raddr->
addr,
4328 strlcpy(errbuf,
"PQcancel() -- connect() failed: ", errbufsize);
4329 goto cancel_errReturn;
4341 crp.cp.cancelAuthCode =
pg_hton32(be_key);
4344 if (
send(tmpsock, (
char *) &crp,
sizeof(crp), 0) != (
int)
sizeof(crp))
4349 strlcpy(errbuf,
"PQcancel() -- send() failed: ", errbufsize);
4350 goto cancel_errReturn;
4361 if (
recv(tmpsock, (
char *) &crp, 1, 0) < 0)
4380 maxlen = errbufsize - strlen(errbuf) - 2;
4385 strcat(errbuf,
"\n");
4407 strlcpy(errbuf,
"PQcancel() -- no cancel object supplied", errbufsize);
4412 errbuf, errbufsize);
4439 "PQrequestCancel() -- connection is not open\n",
4473 const void *
buf,
size_t buf_len)
4496 #define LDAP_URL "ldap://" 4497 #define LDAP_DEF_PORT 389 4498 #define PGLDAP_TIMEOUT 2 4500 #define ld_is_sp_tab(x) ((x) == ' ' || (x) == '\t') 4501 #define ld_is_nl_cr(x) ((x) == '\r' || (x) == '\n') 4527 int port = LDAP_DEF_PORT,
4550 char *attrs[2] = {NULL, NULL};
4555 LDAP_TIMEVAL time = {PGLDAP_TIMEOUT, 0};
4557 if ((url = strdup(purl)) == NULL)
4572 libpq_gettext(
"invalid LDAP URL \"%s\": scheme must be ldap://\n"), purl);
4578 hostname = url + strlen(LDAP_URL);
4579 if (*hostname ==
'/')
4583 p = strchr(url + strlen(LDAP_URL),
'/');
4584 if (p == NULL || *(p + 1) ==
'\0' || *(p + 1) ==
'?')
4587 libpq_gettext(
"invalid LDAP URL \"%s\": missing distinguished name\n"),
4596 if ((p = strchr(dn,
'?')) == NULL || *(p + 1) ==
'\0' || *(p + 1) ==
'?')
4599 libpq_gettext(
"invalid LDAP URL \"%s\": must have exactly one attribute\n"),
4608 if ((p = strchr(attrs[0],
'?')) == NULL || *(p + 1) ==
'\0' || *(p + 1) ==
'?')
4611 libpq_gettext(
"invalid LDAP URL \"%s\": must have search scope (base/one/sub)\n"),
4620 if ((p = strchr(scopestr,
'?')) == NULL || *(p + 1) ==
'\0' || *(p + 1) ==
'?')
4630 if ((p = strchr(filter,
'?')) != NULL)
4634 if ((p1 = strchr(hostname,
':')) != NULL)
4641 lport = strtol(portstr, &endptr, 10);
4642 if (*portstr ==
'\0' || *endptr !=
'\0' || errno || lport < 0 || lport > 65535)
4645 libpq_gettext(
"invalid LDAP URL \"%s\": invalid port number\n"),
4654 if (strchr(attrs[0],
',') != NULL)
4657 libpq_gettext(
"invalid LDAP URL \"%s\": must have exactly one attribute\n"),
4665 scope = LDAP_SCOPE_BASE;
4667 scope = LDAP_SCOPE_ONELEVEL;
4669 scope = LDAP_SCOPE_SUBTREE;
4673 libpq_gettext(
"invalid LDAP URL \"%s\": must have search scope (base/one/sub)\n"),
4680 if ((ld = ldap_init(hostname, port)) == NULL)
4702 if (ldap_connect(ld, &time) != LDAP_SUCCESS)
4711 if (ldap_set_option(ld, LDAP_OPT_NETWORK_TIMEOUT, &time) != LDAP_SUCCESS)
4719 if ((msgid = ldap_simple_bind(ld, NULL, NULL)) == -1)
4729 if ((rc = ldap_result(ld, msgid, LDAP_MSG_ALL, &time, &res)) == -1 ||
4743 if (ldap_set_option(ld, LDAP_OPT_NETWORK_TIMEOUT, &time) != LDAP_SUCCESS)
4753 if ((rc = ldap_search_st(ld, dn, scope, filter, attrs, 0, &time, &res))
4760 ldap_err2string(rc));
4767 if ((rc = ldap_count_entries(ld, res)) != 1)
4770 rc ?
libpq_gettext(
"more than one entry found on LDAP lookup\n")
4779 if ((entry = ldap_first_entry(ld, res)) == NULL)
4791 if ((values = ldap_get_values_len(ld, entry, attrs[0])) == NULL)
4804 if (values[0] == NULL)
4808 ldap_value_free_len(values);
4815 for (i = 0; values[
i] != NULL; i++)
4816 size += values[i]->bv_len + 1;
4817 if ((result =
malloc(size)) == NULL)
4821 ldap_value_free_len(values);
4826 for (i = 0; values[
i] != NULL; i++)
4828 memcpy(p, values[i]->bv_val, values[i]->bv_len);
4829 p += values[
i]->bv_len;
4834 ldap_value_free_len(values);
4838 oldstate = state = 0;
4839 for (p = result; *p !=
'\0'; ++p)
4844 if (!ld_is_sp_tab(*p) && !ld_is_nl_cr(*p))
4851 if (ld_is_sp_tab(*p))
4856 else if (ld_is_nl_cr(*p))
4859 libpq_gettext(
"missing \"=\" after \"%s\" in connection info string\n"),
4875 else if (!ld_is_sp_tab(*p))
4878 libpq_gettext(
"missing \"=\" after \"%s\" in connection info string\n"),
4891 else if (ld_is_nl_cr(*p))
4893 optval = optname + strlen(optname);
4896 else if (!ld_is_sp_tab(*p))
4903 if (ld_is_sp_tab(*p) || ld_is_nl_cr(*p))
4915 else if (*p ==
'\\')
4926 if (state == 0 && oldstate != 0)
4928 found_keyword =
false;
4929 for (i = 0; options[
i].
keyword; i++)
4931 if (strcmp(options[i].
keyword, optname) == 0)
4933 if (options[i].
val == NULL)
4935 options[
i].
val = strdup(optval);
4936 if (!options[i].
val)
4944 found_keyword =
true;
4964 if (state == 5 || state == 6)
4967 libpq_gettext(
"unterminated quoted string in connection info string\n"));
4992 bool group_found =
false;
4994 struct stat stat_buf;
5001 if (service == NULL)
5002 service = getenv(
"PGSERVICE");
5005 if (service == NULL)
5012 if ((env = getenv(
"PGSERVICEFILE")) != NULL)
5013 strlcpy(serviceFile, env,
sizeof(serviceFile));
5021 if (
stat(serviceFile, &stat_buf) != 0)
5025 status =
parseServiceFile(serviceFile, service, options, errorMessage, &group_found);
5026 if (group_found || status != 0)
5036 getenv(
"PGSYSCONFDIR") ? getenv(
"PGSYSCONFDIR") : SYSCONFDIR);
5037 if (
stat(serviceFile, &stat_buf) != 0)
5040 status =
parseServiceFile(serviceFile, service, options, errorMessage, &group_found);
5048 libpq_gettext(
"definition of service \"%s\" not found\n"), service);
5057 const char *service,
5069 *group_found =
false;
5071 f = fopen(serviceFile,
"r");
5086 while (linebuf.
len > 0 &&
5087 isspace((
unsigned char) linebuf.
data[linebuf.
len - 1]))
5088 linebuf.
data[--linebuf.
len] =
'\0';
5090 line = linebuf.
data;
5093 while (*line && isspace((
unsigned char) line[0]))
5097 if (line[0] ==
'\0' || line[0] ==
'#')
5109 if (strncmp(line + 1, service, strlen(service)) == 0 &&
5110 line[strlen(service) + 1] ==
']')
5111 *group_found =
true;
5113 *group_found =
false;
5127 if (strncmp(line,
"ldap", 4) == 0)
5129 int rc = ldapServiceLookup(line, options, errorMessage);
5147 val = strchr(line,
'=');
5151 libpq_gettext(
"syntax error in service file \"%s\", line %d\n"),
5159 if (strcmp(key,
"service") == 0)
5162 libpq_gettext(
"nested service specifications not supported in service file \"%s\", line %d\n"),
5173 found_keyword =
false;
5176 if (strcmp(options[
i].
keyword, key) == 0)
5178 if (options[
i].val == NULL)
5179 options[
i].
val = strdup(val);
5180 if (!options[
i].val)
5187 found_keyword =
true;
5195 libpq_gettext(
"syntax error in service file \"%s\", line %d\n"),
5240 if (connOptions == NULL && errmsg)
5241 *errmsg = errorBuf.
data;
5262 if (options == NULL)
5270 for (cur_opt = PQconninfoOptions; cur_opt->
keyword; cur_opt++)
5357 if (options == NULL)
5361 if ((buf = strdup(conninfo)) == NULL)
5373 if (isspace((
unsigned char) *cp))
5385 if (isspace((
unsigned char) *cp))
5390 if (!isspace((
unsigned char) *cp))
5403 libpq_gettext(
"missing \"=\" after \"%s\" in connection info string\n"),
5414 if (!isspace((
unsigned char) *cp))
5427 if (isspace((
unsigned char) *cp))
5452 libpq_gettext(
"unterminated quoted string in connection info string\n"));
5536 while (expand_dbname && keywords[i])
5538 const char *pname = keywords[
i];
5539 const char *pvalue = values[
i];
5542 if (strcmp(pname,
"dbname") == 0 && pvalue)
5552 if (dbname_options == NULL)
5562 if (options == NULL)
5572 const char *pname = keywords[
i];
5573 const char *pvalue = values[
i];
5575 if (pvalue != NULL && pvalue[0] !=
'\0')
5578 for (option = options; option->
keyword != NULL; option++)
5580 if (strcmp(option->
keyword, pname) == 0)
5600 if (strcmp(pname,
"dbname") == 0 && dbname_options)
5604 for (str_option = dbname_options; str_option->
keyword != NULL; str_option++)
5606 if (str_option->
val != NULL)
5610 for (k = 0; options[k].
keyword; k++)
5615 free(options[k].val);
5616 options[k].
val = strdup(str_option->
val);
5617 if (!options[k].val)
5636 dbname_options = NULL;
5645 option->
val = strdup(pvalue);
5704 for (option = options; option->
keyword != NULL; option++)
5706 if (option->
val != NULL)
5712 if (option->
envvar != NULL)
5714 if ((tmp = getenv(option->
envvar)) != NULL)
5716 option->
val = strdup(tmp);
5734 if (strcmp(option->
keyword,
"sslmode") == 0)
5736 const char *requiresslenv = getenv(
"PGREQUIRESSL");
5738 if (requiresslenv != NULL && requiresslenv[0] ==
'1')
5740 option->
val = strdup(
"require");
5776 if (strcmp(option->
keyword,
"user") == 0)
5799 if (options == NULL)
5852 char prevchar =
'\0';
5855 bool retval =
false;
5880 if (prefix_len == 0)
5884 libpq_gettext(
"invalid URI propagated to internal parser routine: \"%s\"\n"),
5888 start += prefix_len;
5892 while (*p && *p !=
'@' && *p !=
'/')
5903 while (*p !=
':' && *p !=
'@')
5912 errorMessage,
false,
true))
5915 if (prevchar ==
':')
5925 errorMessage,
false,
true))
5956 while (*p && *p !=
']')
5961 libpq_gettext(
"end of string reached when looking for matching \"]\" in IPv6 host address in URI: \"%s\"\n"),
5968 libpq_gettext(
"IPv6 host address may not be empty in URI: \"%s\"\n"),
5980 if (*p && *p !=
':' && *p !=
'/' && *p !=
'?' && *p !=
',')
5983 libpq_gettext(
"unexpected character \"%c\" at position %d in URI (expected \":\" or \"/\"): \"%s\"\n"),
5984 *p, (
int) (p - buf + 1), uri);
5997 while (*p && *p !=
':' && *p !=
'/' && *p !=
'?' && *p !=
',')
6007 if (prevchar ==
':')
6009 const char *
port = ++p;
6011 while (*p && *p !=
'/' && *p !=
'?' && *p !=
',')
6020 if (prevchar !=
',')
6030 if (hostbuf.
data[0] &&
6032 errorMessage,
false,
true))
6034 if (portbuf.
data[0] &&
6036 errorMessage,
false,
true))
6039 if (prevchar && prevchar !=
'?')
6041 const char *
dbname = ++p;
6044 while (*p && *p !=
'?')
6057 errorMessage,
false,
true))