Index: src/backend/libpq/ip.c =================================================================== RCS file: /projects/cvsroot/pgsql-server/src/backend/libpq/ip.c,v retrieving revision 1.1 diff -c -r1.1 ip.c *** src/backend/libpq/ip.c 2003/01/06 03:18:26 1.1 --- src/backend/libpq/ip.c 2003/01/09 15:13:37 *************** *** 44,49 **** --- 44,61 ---- #define LOG stderr #endif + #if !defined HAVE_IPV6 + # if defined HAVE_GETHOSTBYNAME_R && GETHOSTBYNAME_R_NARGS == 6 + # define pqGethostbyname gethostbyname_r + # else + static int pqGethostbyname(const char *name, + struct hostent *resbuf, + char *buf, size_t buflen, + struct hostent **result, + int *herrno); + # endif /* HAVE_GETHOSTBYNAME_R && GETHOSTBYNAME_R_NARGS == 6 */ + #endif /* !defined HAVE_IPV6 */ + #if defined(HAVE_UNIX_SOCKETS) && defined(HAVE_IPV6) static int getaddrinfo_unix(const char *path, const struct addrinfo *hintsp, struct addrinfo **result); *************** *** 80,88 **** result->in.sin_addr.s_addr = htonl(INADDR_ANY); else { ! struct hostent *hp; ! hp = gethostbyname(hostname); if ((hp == NULL) || (hp->h_addrtype != AF_INET)) { elog(LOG, "getaddrinfo2: gethostbyname(%s) failed\n", hostname); --- 92,108 ---- result->in.sin_addr.s_addr = htonl(INADDR_ANY); else { ! struct hostent *hp = NULL; ! #ifdef FRONTEND ! struct hostent hpstr; ! char buf[BUFSIZ]; ! int herrno = 0; ! pqGethostbyname(hostname, &hpstr, buf, sizeof(buf), ! &hp, &herrno); ! #else ! hp = gethostbyname(hostname); ! #endif if ((hp == NULL) || (hp->h_addrtype != AF_INET)) { elog(LOG, "getaddrinfo2: gethostbyname(%s) failed\n", hostname); *************** *** 369,371 **** --- 389,424 ---- SockAddr_ntop(src, addr_str, INET6_ADDRSTRLEN, 0); } #endif + + #if !defined HAVE_IPV6 + # if defined HAVE_GETHOSTBYNAME_R && GETHOSTBYNAME_R_NARGS == 6 + /* macro above */ + # elif defined HAVE_GETHOSTBYNAME_R && GETHOSTBYNAME_R_NARGS == 5 + static int pqGethostbyname(const char *name, + struct hostent *resbuf, + char *buf, size_t buflen, + struct hostent **result, + int *herrno) + { + *result = gethostbyname_r(name, resbuf, buf, buflen, herrno); + return( (*result == NULL) ? -1 : 0 ); + } + # else + /* either we don't have gethostbyname_r, or it's not a version he handle */ + static int pqGethostbyname(const char *name, + struct hostent *resbuf, + char *buf, size_t buflen, + struct hostent **result, + int *herrno) + { + *result = gethostbyname(name); + if( *result != NULL ) + return( 0 ); + else + { + *herrno = h_errno; + return( -1 ); + } + } + # endif /* HAVE_GETHOSTBYNAME_R */ + #endif /* !defined HAVE_IPV6 */ Index: src/interfaces/libpq/fe-auth.c =================================================================== RCS file: /projects/cvsroot/pgsql-server/src/interfaces/libpq/fe-auth.c,v retrieving revision 1.72 diff -c -r1.72 fe-auth.c *** src/interfaces/libpq/fe-auth.c 2002/12/03 22:09:20 1.72 --- src/interfaces/libpq/fe-auth.c 2003/01/09 15:13:37 *************** *** 28,33 **** --- 28,37 ---- * */ + #define _THREAD_SAFE + #define _REENTRANT + #define _POSIX_PTHREAD_SEMANTICS + #include "postgres_fe.h" #ifdef WIN32 *************** *** 391,398 **** flags = fcntl(sock, F_GETFL); if (flags < 0 || fcntl(sock, F_SETFL, (long) (flags & ~O_NONBLOCK))) { snprintf(PQerrormsg, PQERRORMSG_LENGTH, ! libpq_gettext("could not set socket to blocking mode: %s\n"), strerror(errno)); krb5_free_principal(pg_krb5_context, server); return STATUS_ERROR; } --- 395,404 ---- flags = fcntl(sock, F_GETFL); if (flags < 0 || fcntl(sock, F_SETFL, (long) (flags & ~O_NONBLOCK))) { + char sebuf[256]; + snprintf(PQerrormsg, PQERRORMSG_LENGTH, ! libpq_gettext("could not set socket to blocking mode: %s\n"), pqStrError(errno, sebuf, sizeof(sebuf))); krb5_free_principal(pg_krb5_context, server); return STATUS_ERROR; } *************** *** 436,444 **** if (fcntl(sock, F_SETFL, (long) flags)) { snprintf(PQerrormsg, PQERRORMSG_LENGTH, libpq_gettext("could not restore non-blocking mode on socket: %s\n"), ! strerror(errno)); ret = STATUS_ERROR; } --- 442,452 ---- if (fcntl(sock, F_SETFL, (long) flags)) { + char sebuf[256]; + snprintf(PQerrormsg, PQERRORMSG_LENGTH, libpq_gettext("could not restore non-blocking mode on socket: %s\n"), ! pqStrError(errno, sebuf, sizeof(sebuf))); ret = STATUS_ERROR; } *************** *** 495,502 **** if (sendmsg(conn->sock, &msg, 0) == -1) { snprintf(PQerrormsg, PQERRORMSG_LENGTH, ! "pg_local_sendauth: sendmsg: %s\n", strerror(errno)); return STATUS_ERROR; } return STATUS_OK; --- 503,513 ---- if (sendmsg(conn->sock, &msg, 0) == -1) { + char sebuf[256]; + snprintf(PQerrormsg, PQERRORMSG_LENGTH, ! "pg_local_sendauth: sendmsg: %s\n", ! pqStrError(errno, sebuf, sizeof(sebuf))); return STATUS_ERROR; } return STATUS_OK; *************** *** 512,517 **** --- 523,532 ---- { int ret; char *crypt_pwd; + #if defined HAVE_CRYPT_R && defined __USE_GNU + struct crypt_data cryptbuf; + cryptbuf.initialized = 0; + #endif /* Encrypt the password if needed. */ *************** *** 549,555 **** --- 564,574 ---- char salt[3]; StrNCpy(salt, conn->cryptSalt, 3); + #if defined HAVE_CRYPT_R && defined __USE_GNU + crypt_pwd = crypt_r(password, salt, &cryptbuf); + #else crypt_pwd = crypt(password, salt); + #endif break; } case AUTH_REQ_PASSWORD: *************** *** 724,733 **** if (GetUserName(username, &namesize)) name = username; #else ! struct passwd *pw = getpwuid(geteuid()); ! ! if (pw) ! name = pw->pw_name; #endif } --- 743,755 ---- if (GetUserName(username, &namesize)) name = username; #else ! char pwdbuf[BUFSIZ]; ! struct passwd pwdstr; ! struct passwd *pw = NULL; ! ! if( pqGetpwuid(geteuid(), &pwdstr, ! pwdbuf, sizeof(pwdbuf), &pw) == 0 ) ! name = pw->pw_name; #endif } Index: src/interfaces/libpq/fe-connect.c =================================================================== RCS file: /projects/cvsroot/pgsql-server/src/interfaces/libpq/fe-connect.c,v retrieving revision 1.221 diff -c -r1.221 fe-connect.c *** src/interfaces/libpq/fe-connect.c 2003/01/08 21:33:27 1.221 --- src/interfaces/libpq/fe-connect.c 2003/01/09 15:13:37 *************** *** 716,724 **** if (fcntl(conn->sock, F_SETFL, O_NONBLOCK) < 0) #endif { printfPQExpBuffer(&conn->errorMessage, libpq_gettext("could not set socket to non-blocking mode: %s\n"), ! SOCK_STRERROR(SOCK_ERRNO)); return 0; } --- 716,726 ---- if (fcntl(conn->sock, F_SETFL, O_NONBLOCK) < 0) #endif { + char sebuf[256]; + printfPQExpBuffer(&conn->errorMessage, libpq_gettext("could not set socket to non-blocking mode: %s\n"), ! SOCK_STRERROR(SOCK_ERRNO, sebuf, sizeof(sebuf))); return 0; } *************** *** 740,748 **** (char *) &on, sizeof(on)) < 0) { printfPQExpBuffer(&conn->errorMessage, libpq_gettext("could not set socket to TCP no delay mode: %s\n"), ! SOCK_STRERROR(SOCK_ERRNO)); return 0; } --- 742,752 ---- (char *) &on, sizeof(on)) < 0) { + char sebuf[256]; + printfPQExpBuffer(&conn->errorMessage, libpq_gettext("could not set socket to TCP no delay mode: %s\n"), ! SOCK_STRERROR(SOCK_ERRNO, sebuf, sizeof(sebuf))); return 0; } *************** *** 758,763 **** --- 762,769 ---- static void connectFailureMessage(PGconn *conn, int errorno) { + char sebuf[256]; + if (conn->raddr.sa.sa_family == AF_UNIX) printfPQExpBuffer(&conn->errorMessage, libpq_gettext( *************** *** 765,771 **** "\tIs the server running locally and accepting\n" "\tconnections on Unix domain socket \"%s\"?\n" ), ! SOCK_STRERROR(errorno), conn->raddr.un.sun_path); else printfPQExpBuffer(&conn->errorMessage, --- 771,777 ---- "\tIs the server running locally and accepting\n" "\tconnections on Unix domain socket \"%s\"?\n" ), ! SOCK_STRERROR(errorno, sebuf, sizeof(sebuf)), conn->raddr.un.sun_path); else printfPQExpBuffer(&conn->errorMessage, *************** *** 774,780 **** "\tIs the server running on host %s and accepting\n" "\tTCP/IP connections on port %s?\n" ), ! SOCK_STRERROR(errorno), conn->pghost ? conn->pghost : (conn->pghostaddr --- 780,786 ---- "\tIs the server running on host %s and accepting\n" "\tTCP/IP connections on port %s?\n" ), ! SOCK_STRERROR(errorno, sebuf, sizeof(sebuf)), conn->pghost ? conn->pghost : (conn->pghostaddr *************** *** 797,802 **** --- 803,809 ---- int portnum; int sockfd; char portstr[64]; + char sebuf[256]; #ifdef USE_SSL StartupPacket np; /* Used to negotiate SSL connection */ char SSLok; *************** *** 1013,1019 **** { printfPQExpBuffer(&conn->errorMessage, libpq_gettext("could not create socket: %s\n"), ! SOCK_STRERROR(SOCK_ERRNO)); goto connect_errReturn; } else --- 1020,1026 ---- { printfPQExpBuffer(&conn->errorMessage, libpq_gettext("could not create socket: %s\n"), ! SOCK_STRERROR(SOCK_ERRNO, sebuf, sizeof(sebuf))); goto connect_errReturn; } else *************** *** 1036,1042 **** { printfPQExpBuffer(&conn->errorMessage, libpq_gettext("could not send SSL negotiation packet: %s\n"), ! SOCK_STRERROR(SOCK_ERRNO)); goto connect_errReturn; } retry2: --- 1043,1049 ---- { printfPQExpBuffer(&conn->errorMessage, libpq_gettext("could not send SSL negotiation packet: %s\n"), ! SOCK_STRERROR(SOCK_ERRNO, sebuf, sizeof(sebuf))); goto connect_errReturn; } retry2: *************** *** 1049,1055 **** printfPQExpBuffer(&conn->errorMessage, libpq_gettext("could not receive server response to SSL negotiation packet: %s\n"), ! SOCK_STRERROR(SOCK_ERRNO)); goto connect_errReturn; } if (SSLok == 'S') --- 1056,1062 ---- printfPQExpBuffer(&conn->errorMessage, libpq_gettext("could not receive server response to SSL negotiation packet: %s\n"), ! SOCK_STRERROR(SOCK_ERRNO, sebuf, sizeof(sebuf))); goto connect_errReturn; } if (SSLok == 'S') *************** *** 1234,1239 **** --- 1241,1247 ---- PQconnectPoll(PGconn *conn) { PGresult *res; + char sebuf[256]; if (conn == NULL) return PGRES_POLLING_FAILED; *************** *** 1309,1315 **** { printfPQExpBuffer(&conn->errorMessage, libpq_gettext("could not get socket error status: %s\n"), ! SOCK_STRERROR(SOCK_ERRNO)); goto error_return; } else if (optval != 0) --- 1317,1323 ---- { printfPQExpBuffer(&conn->errorMessage, libpq_gettext("could not get socket error status: %s\n"), ! SOCK_STRERROR(SOCK_ERRNO, sebuf, sizeof(sebuf))); goto error_return; } else if (optval != 0) *************** *** 1329,1335 **** { printfPQExpBuffer(&conn->errorMessage, libpq_gettext("could not get client address from socket: %s\n"), ! SOCK_STRERROR(SOCK_ERRNO)); goto error_return; } --- 1337,1343 ---- { printfPQExpBuffer(&conn->errorMessage, libpq_gettext("could not get client address from socket: %s\n"), ! SOCK_STRERROR(SOCK_ERRNO, sebuf, sizeof(sebuf))); goto error_return; } *************** *** 1368,1374 **** { printfPQExpBuffer(&conn->errorMessage, libpq_gettext("could not send startup packet: %s\n"), ! SOCK_STRERROR(SOCK_ERRNO)); goto error_return; } --- 1376,1382 ---- { printfPQExpBuffer(&conn->errorMessage, libpq_gettext("could not send startup packet: %s\n"), ! SOCK_STRERROR(SOCK_ERRNO, sebuf, sizeof(sebuf))); goto error_return; } *************** *** 2186,2191 **** --- 2194,2200 ---- { int save_errno = SOCK_ERRNO; int tmpsock = -1; + char sebuf[256]; struct { uint32 packetlen; *************** *** 2264,2270 **** return TRUE; cancel_errReturn: ! strcat(conn->errorMessage.data, SOCK_STRERROR(SOCK_ERRNO)); strcat(conn->errorMessage.data, "\n"); conn->errorMessage.len = strlen(conn->errorMessage.data); if (tmpsock >= 0) --- 2273,2279 ---- return TRUE; cancel_errReturn: ! strcat(conn->errorMessage.data, SOCK_STRERROR(SOCK_ERRNO, sebuf, sizeof(sebuf))); strcat(conn->errorMessage.data, "\n"); conn->errorMessage.len = strlen(conn->errorMessage.data); if (tmpsock >= 0) *************** *** 2392,2399 **** *val; int found_keyword; ! key = strtok(line, "="); ! if (key == NULL) { printfPQExpBuffer(errorMessage, "ERROR: syntax error in service file '%s', line %d\n", --- 2401,2409 ---- *val; int found_keyword; ! key = line; ! val = strchr(line, '='); ! if( val == NULL ) { printfPQExpBuffer(errorMessage, "ERROR: syntax error in service file '%s', line %d\n", *************** *** 2402,2407 **** --- 2412,2418 ---- fclose(f); return 3; } + *val++ = '\0'; /* * If not already set, set the database name to the *************** *** 2411,2418 **** if (strcmp(options[i].keyword, "dbname") == 0) if (options[i].val == NULL) options[i].val = strdup(service); - - val = line + strlen(line) + 1; found_keyword = 0; for (i = 0; options[i].keyword; i++) --- 2422,2427 ---- Index: src/interfaces/libpq/fe-lobj.c =================================================================== RCS file: /projects/cvsroot/pgsql-server/src/interfaces/libpq/fe-lobj.c,v retrieving revision 1.41 diff -c -r1.41 fe-lobj.c *** src/interfaces/libpq/fe-lobj.c 2002/06/20 20:29:54 1.41 --- src/interfaces/libpq/fe-lobj.c 2003/01/09 15:13:37 *************** *** 396,404 **** fd = open(filename, O_RDONLY | PG_BINARY, 0666); if (fd < 0) { /* error */ printfPQExpBuffer(&conn->errorMessage, libpq_gettext("could not open file \"%s\": %s\n"), ! filename, strerror(errno)); return InvalidOid; } --- 396,405 ---- fd = open(filename, O_RDONLY | PG_BINARY, 0666); if (fd < 0) { /* error */ + char sebuf[256]; printfPQExpBuffer(&conn->errorMessage, libpq_gettext("could not open file \"%s\": %s\n"), ! filename, pqStrError(errno, sebuf, sizeof(sebuf))); return InvalidOid; } *************** *** 479,487 **** fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC | PG_BINARY, 0666); if (fd < 0) { /* error */ printfPQExpBuffer(&conn->errorMessage, libpq_gettext("could not open file \"%s\": %s\n"), ! filename, strerror(errno)); (void) lo_close(conn, lobj); return -1; } --- 480,489 ---- fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC | PG_BINARY, 0666); if (fd < 0) { /* error */ + char sebuf[256]; printfPQExpBuffer(&conn->errorMessage, libpq_gettext("could not open file \"%s\": %s\n"), ! filename, pqStrError(errno, sebuf, sizeof(sebuf))); (void) lo_close(conn, lobj); return -1; } Index: src/interfaces/libpq/fe-misc.c =================================================================== RCS file: /projects/cvsroot/pgsql-server/src/interfaces/libpq/fe-misc.c,v retrieving revision 1.86 diff -c -r1.86 fe-misc.c *** src/interfaces/libpq/fe-misc.c 2003/01/07 22:23:17 1.86 --- src/interfaces/libpq/fe-misc.c 2003/01/09 15:13:37 *************** *** 381,393 **** if (select(conn->sock + 1, &input_mask, (fd_set *) NULL, (fd_set *) NULL, &timeout) < 0) { if (SOCK_ERRNO == EINTR) /* Interrupted system call - we'll just try again */ goto retry1; printfPQExpBuffer(&conn->errorMessage, libpq_gettext("select() failed: %s\n"), ! SOCK_STRERROR(SOCK_ERRNO)); return -1; } --- 381,394 ---- if (select(conn->sock + 1, &input_mask, (fd_set *) NULL, (fd_set *) NULL, &timeout) < 0) { + char sebuf[256]; if (SOCK_ERRNO == EINTR) /* Interrupted system call - we'll just try again */ goto retry1; printfPQExpBuffer(&conn->errorMessage, libpq_gettext("select() failed: %s\n"), ! SOCK_STRERROR(SOCK_ERRNO, sebuf, sizeof(sebuf))); return -1; } *************** *** 415,427 **** if (select(conn->sock + 1, (fd_set *) NULL, &input_mask, (fd_set *) NULL, &timeout) < 0) { if (SOCK_ERRNO == EINTR) /* Interrupted system call - we'll just try again */ goto retry2; printfPQExpBuffer(&conn->errorMessage, libpq_gettext("select() failed: %s\n"), ! SOCK_STRERROR(SOCK_ERRNO)); return -1; } return FD_ISSET(conn->sock, &input_mask) ? 1 : 0; --- 416,429 ---- if (select(conn->sock + 1, (fd_set *) NULL, &input_mask, (fd_set *) NULL, &timeout) < 0) { + char sebuf[256]; if (SOCK_ERRNO == EINTR) /* Interrupted system call - we'll just try again */ goto retry2; printfPQExpBuffer(&conn->errorMessage, libpq_gettext("select() failed: %s\n"), ! SOCK_STRERROR(SOCK_ERRNO, sebuf, sizeof(sebuf))); return -1; } return FD_ISSET(conn->sock, &input_mask) ? 1 : 0; *************** *** 443,448 **** --- 445,451 ---- { int someread = 0; int nread; + char sebuf[256]; if (conn->sock < 0) { *************** *** 513,519 **** #endif printfPQExpBuffer(&conn->errorMessage, libpq_gettext("could not receive data from server: %s\n"), ! SOCK_STRERROR(SOCK_ERRNO)); return -1; } if (nread > 0) --- 516,522 ---- #endif printfPQExpBuffer(&conn->errorMessage, libpq_gettext("could not receive data from server: %s\n"), ! SOCK_STRERROR(SOCK_ERRNO, sebuf, sizeof(sebuf))); return -1; } if (nread > 0) *************** *** 593,599 **** #endif printfPQExpBuffer(&conn->errorMessage, libpq_gettext("could not receive data from server: %s\n"), ! SOCK_STRERROR(SOCK_ERRNO)); return -1; } if (nread > 0) --- 596,602 ---- #endif printfPQExpBuffer(&conn->errorMessage, libpq_gettext("could not receive data from server: %s\n"), ! SOCK_STRERROR(SOCK_ERRNO, sebuf, sizeof(sebuf))); return -1; } if (nread > 0) *************** *** 654,659 **** --- 657,663 ---- while (len > 0) { int sent; + char sebuf[256]; sent = pqsecure_write(conn, ptr, len); *************** *** 700,706 **** default: printfPQExpBuffer(&conn->errorMessage, libpq_gettext("could not send data to server: %s\n"), ! SOCK_STRERROR(SOCK_ERRNO)); /* We don't assume it's a fatal error... */ return -1; } --- 704,710 ---- default: printfPQExpBuffer(&conn->errorMessage, libpq_gettext("could not send data to server: %s\n"), ! SOCK_STRERROR(SOCK_ERRNO, sebuf, sizeof(sebuf))); /* We don't assume it's a fatal error... */ return -1; } *************** *** 850,860 **** &except_mask, ptmp_timeout); if (selresult < 0) { if (SOCK_ERRNO == EINTR) goto retry5; printfPQExpBuffer(&conn->errorMessage, libpq_gettext("select() failed: %s\n"), ! SOCK_STRERROR(SOCK_ERRNO)); return EOF; } if (selresult == 0) --- 854,865 ---- &except_mask, ptmp_timeout); if (selresult < 0) { + char sebuf[256]; if (SOCK_ERRNO == EINTR) goto retry5; printfPQExpBuffer(&conn->errorMessage, libpq_gettext("select() failed: %s\n"), ! SOCK_STRERROR(SOCK_ERRNO, sebuf, sizeof(sebuf))); return EOF; } if (selresult == 0) *************** *** 917,919 **** --- 922,970 ---- } #endif /* ENABLE_NLS */ + + + /* + * Wrapper around strerror and strerror_r to use the former if it is + * available and also return a more useful value (the error string). + */ + char *pqStrError(int errnum, char *strerrbuf, size_t buflen) + { + #if defined HAVE_STRERROR_R && defined STRERROR_R_CHAR_P + /* broken strerror_r which returns 'char *' */ + return( strerror_r(errnum, strerrbuf, buflen) ); + #elif defined HAVE_STRERROR_R + /* reentrant strerror_r */ + (void)strerror_r(errnum, strerrbuf, buflen); + return( strerrbuf ); + #else + /* no strerror_r available, just use strerror */ + return( strerror(errnum) ); + #endif + } + + #if defined HAVE_NONPOSIX_GETPWUID_R + int pqGetpwuid(uid_t uid, struct passwd *resultbuf, char *buffer, + size_t buflen, struct passwd **result) + { + fprintf(stderr, "pqGetpwuid(2)\n"); + *result = getpwuid_r(uid, resultbuf, buffer, buflen); + + if( result == NULL ) + return( -1 ); + else + return( 0 ); + } + #elif !defined HAVE_POSIX_GETPWUID_R + int pqGetpwuid(uid_t uid, struct passwd *resultbuf, char *buffer, + size_t buflen, struct passwd **result) + { + fprintf(stderr, "pqGetpwuid(38)\n"); + *result = getpwuid(uid); + + if( result == NULL ) + return( -1 ); + else + return( 0 ); + } + #endif Index: src/interfaces/libpq/fe-secure.c =================================================================== RCS file: /projects/cvsroot/pgsql-server/src/interfaces/libpq/fe-secure.c,v retrieving revision 1.20 diff -c -r1.20 fe-secure.c *** src/interfaces/libpq/fe-secure.c 2003/01/08 23:18:25 1.20 --- src/interfaces/libpq/fe-secure.c 2003/01/09 15:13:37 *************** *** 272,280 **** break; case SSL_ERROR_SYSCALL: if (n == -1) ! printfPQExpBuffer(&conn->errorMessage, ! libpq_gettext("SSL SYSCALL error: %s\n"), ! SOCK_STRERROR(SOCK_ERRNO)); break; case SSL_ERROR_SSL: printfPQExpBuffer(&conn->errorMessage, --- 272,283 ---- break; case SSL_ERROR_SYSCALL: if (n == -1) ! { ! char sebuf[256]; ! printfPQExpBuffer(&conn->errorMessage, ! libpq_gettext("SSL SYSCALL error: %s\n"), ! SOCK_STRERROR(SOCK_ERRNO, sebuf, sizeof(sebuf))); ! } break; case SSL_ERROR_SSL: printfPQExpBuffer(&conn->errorMessage, *************** *** 319,327 **** break; case SSL_ERROR_SYSCALL: if (n == -1) ! printfPQExpBuffer(&conn->errorMessage, ! libpq_gettext("SSL SYSCALL error: %s\n"), ! SOCK_STRERROR(SOCK_ERRNO)); break; case SSL_ERROR_SSL: printfPQExpBuffer(&conn->errorMessage, --- 322,333 ---- break; case SSL_ERROR_SYSCALL: if (n == -1) ! { ! char sebuf[256]; ! printfPQExpBuffer(&conn->errorMessage, ! libpq_gettext("SSL SYSCALL error: %s\n"), ! SOCK_STRERROR(SOCK_ERRNO, sebuf, sizeof(sebuf))); ! } break; case SSL_ERROR_SSL: printfPQExpBuffer(&conn->errorMessage, *************** *** 385,393 **** len = sizeof(addr); if (getpeername(conn->sock, &addr, &len) == -1) { printfPQExpBuffer(&conn->errorMessage, libpq_gettext("error querying socket: %s\n"), ! SOCK_STRERROR(SOCK_ERRNO)); return -1; } --- 391,400 ---- len = sizeof(addr); if (getpeername(conn->sock, &addr, &len) == -1) { + char sebuf[256]; printfPQExpBuffer(&conn->errorMessage, libpq_gettext("error querying socket: %s\n"), ! SOCK_STRERROR(SOCK_ERRNO, sebuf, sizeof(sebuf))); return -1; } *************** *** 465,478 **** static DH * load_dh_file(int keylength) { ! struct passwd *pwd; FILE *fp; char fnbuf[2048]; DH *dh = NULL; int codes; ! if ((pwd = getpwuid(getuid())) == NULL) ! return NULL; /* attempt to open file. It's not an error if it doesn't exist. */ snprintf(fnbuf, sizeof fnbuf, "%s/.postgresql/dh%d.pem", --- 472,487 ---- static DH * load_dh_file(int keylength) { ! char pwdbuf[BUFSIZ]; ! struct passwd pwdstr; ! struct passwd *pwd = NULL; FILE *fp; char fnbuf[2048]; DH *dh = NULL; int codes; ! if( pqGetpwuid(getuid(), &pwdstr, pwdbuf, sizeof(pwdbuf), &pwd) == 0 ) ! return NULL; /* attempt to open file. It's not an error if it doesn't exist. */ snprintf(fnbuf, sizeof fnbuf, "%s/.postgresql/dh%d.pem", *************** *** 605,619 **** static int client_cert_cb(SSL *ssl, X509 **x509, EVP_PKEY **pkey) { ! struct passwd *pwd; struct stat buf, buf2; char fnbuf[2048]; FILE *fp; PGconn *conn = (PGconn *) SSL_get_app_data(ssl); int (*cb) () = NULL; /* how to read user password */ ! if ((pwd = getpwuid(getuid())) == NULL) { printfPQExpBuffer(&conn->errorMessage, libpq_gettext("could not get user information\n")); --- 614,632 ---- static int client_cert_cb(SSL *ssl, X509 **x509, EVP_PKEY **pkey) { ! char pwdbuf[BUFSIZ]; ! struct passwd pwdstr; ! struct passwd *pwd = NULL; struct stat buf, buf2; char fnbuf[2048]; FILE *fp; PGconn *conn = (PGconn *) SSL_get_app_data(ssl); int (*cb) () = NULL; /* how to read user password */ + char sebuf[256]; + ! if( pqGetpwuid(getuid(), &pwdstr, pwdbuf, sizeof(pwdbuf), &pwd) == 0 ) { printfPQExpBuffer(&conn->errorMessage, libpq_gettext("could not get user information\n")); *************** *** 629,635 **** { printfPQExpBuffer(&conn->errorMessage, libpq_gettext("could not open certificate (%s): %s\n"), ! fnbuf, strerror(errno)); return -1; } if (PEM_read_X509(fp, x509, NULL, NULL) == NULL) --- 642,648 ---- { printfPQExpBuffer(&conn->errorMessage, libpq_gettext("could not open certificate (%s): %s\n"), ! fnbuf, pqStrError(errno, sebuf, sizeof(sebuf))); return -1; } if (PEM_read_X509(fp, x509, NULL, NULL) == NULL) *************** *** 665,671 **** { printfPQExpBuffer(&conn->errorMessage, libpq_gettext("could not open private key file (%s): %s\n"), ! fnbuf, strerror(errno)); X509_free(*x509); return -1; } --- 678,684 ---- { printfPQExpBuffer(&conn->errorMessage, libpq_gettext("could not open private key file (%s): %s\n"), ! fnbuf, pqStrError(errno, sebuf, sizeof(sebuf))); X509_free(*x509); return -1; } *************** *** 709,715 **** initialize_SSL(PGconn *conn) { struct stat buf; ! struct passwd *pwd; char fnbuf[2048]; if (!SSL_context) --- 722,730 ---- initialize_SSL(PGconn *conn) { struct stat buf; ! char pwdbuf[BUFSIZ]; ! struct passwd pwdstr; ! struct passwd *pwd = NULL; char fnbuf[2048]; if (!SSL_context) *************** *** 726,732 **** } } ! if ((pwd = getpwuid(getuid())) != NULL) { snprintf(fnbuf, sizeof fnbuf, "%s/.postgresql/root.crt", pwd->pw_dir); --- 741,747 ---- } } ! if( pqGetpwuid(getuid(), &pwdstr, pwdbuf, sizeof(pwdbuf), &pwd) == 0 ) { snprintf(fnbuf, sizeof fnbuf, "%s/.postgresql/root.crt", pwd->pw_dir); *************** *** 734,743 **** { return 0; #ifdef NOT_USED /* CLIENT CERTIFICATES NOT REQUIRED bjm 2002-09-26 */ printfPQExpBuffer(&conn->errorMessage, libpq_gettext("could not read root certificate list (%s): %s\n"), ! fnbuf, strerror(errno)); return -1; #endif } --- 749,759 ---- { return 0; #ifdef NOT_USED + char sebuf[256]; /* CLIENT CERTIFICATES NOT REQUIRED bjm 2002-09-26 */ printfPQExpBuffer(&conn->errorMessage, libpq_gettext("could not read root certificate list (%s): %s\n"), ! fnbuf, pqStrError(errno, sebuf, sizeof(sebuf))); return -1; #endif } Index: src/interfaces/libpq/libpq-int.h =================================================================== RCS file: /projects/cvsroot/pgsql-server/src/interfaces/libpq/libpq-int.h,v retrieving revision 1.60 diff -c -r1.60 libpq-int.h *** src/interfaces/libpq/libpq-int.h 2002/10/16 02:55:30 1.60 --- src/interfaces/libpq/libpq-int.h 2003/01/09 15:13:37 *************** *** 20,31 **** --- 20,39 ---- #ifndef LIBPQ_INT_H #define LIBPQ_INT_H + #define _THREAD_SAFE + #define _REENTRANT + #define _POSIX_PTHREAD_SEMANTICS + #include #include #ifndef WIN32 #include #endif + #ifdef HAVE_PWD_H + # include + #endif + #if defined(WIN32) && (!defined(ssize_t)) typedef int ssize_t; /* ssize_t doesn't exist in VC (atleast * not VC6) */ *************** *** 345,350 **** --- 353,378 ---- extern int pqReadReady(PGconn *conn); extern int pqWriteReady(PGconn *conn); + /* routines to aid thread safety */ + extern char *pqStrError(int errnum, char *strerrbuf, size_t buflen); + + #if defined HAVE_POSIX_GETPWUID_R + # define pqGetpwuid getpwuid_r + #else + extern int pqGetpwuid(uid_t uid, struct passwd *resultbuf, char *buffer, + size_t buflen, struct passwd **result); + #endif + + #if !defined HAVE_GETHOSTBYNAME_R || (defined HAVE_GETHOSTBYNAME_R && GETHOSTBYNAME_R_NARGS < 6) + extern int pqGethostbyname(const char *name, + struct hostent *resbuf, + char *buf, size_t buflen, + struct hostent **result, + int *herrno); + #else + #define pqGethostbyname gethostbyname_r + #endif + /* === in fe-secure.c === */ extern int pqsecure_initialize(PGconn *); *************** *** 389,395 **** #define SOCK_STRERROR winsock_strerror #else #define SOCK_ERRNO errno ! #define SOCK_STRERROR strerror #endif #endif /* LIBPQ_INT_H */ --- 417,423 ---- #define SOCK_STRERROR winsock_strerror #else #define SOCK_ERRNO errno ! #define SOCK_STRERROR pqStrError #endif #endif /* LIBPQ_INT_H */ Index: src/interfaces/libpq/win32.c =================================================================== RCS file: /projects/cvsroot/pgsql-server/src/interfaces/libpq/win32.c,v retrieving revision 1.4 diff -c -r1.4 win32.c *** src/interfaces/libpq/win32.c 2002/09/04 20:31:47 1.4 --- src/interfaces/libpq/win32.c 2003/01/09 15:13:37 *************** *** 271,283 **** */ const char * ! winsock_strerror(int err) { - static char buf[512]; /* Not threadsafe */ unsigned long flags; int offs, i; ! int success = LookupWSErrorMessage(err, buf); for (i = 0; !success && i < DLLS_SIZE; i++) { --- 271,282 ---- */ const char * ! winsock_strerror(int err, char *strerrbuf, size_t buflen) { unsigned long flags; int offs, i; ! int success = LookupWSErrorMessage(err, strerrbuf); for (i = 0; !success && i < DLLS_SIZE; i++) { *************** *** 302,321 **** flags, dlls[i].handle, err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), ! buf, sizeof(buf) - 64, 0 ); } if (!success) ! sprintf(buf, "Unknown socket error (0x%08X/%lu)", err, err); else { ! buf[sizeof(buf) - 1] = '\0'; ! offs = strlen(buf); ! if (offs > sizeof(buf) - 64) ! offs = sizeof(buf) - 64; ! sprintf(buf + offs, " (0x%08X/%lu)", err, err); } ! return buf; } --- 301,320 ---- flags, dlls[i].handle, err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), ! strerrbuf, buflen - 64, 0 ); } if (!success) ! sprintf(strerrbuf, "Unknown socket error (0x%08X/%lu)", err, err); else { ! strerrbuf[buflen - 1] = '\0'; ! offs = strlen(strerrbuf); ! if (offs > buflen - 64) ! offs = buflen - 64; ! sprintf(strerrbuf + offs, " (0x%08X/%lu)", err, err); } ! return strerrbuf; } Index: src/interfaces/libpq/win32.h =================================================================== RCS file: /projects/cvsroot/pgsql-server/src/interfaces/libpq/win32.h,v retrieving revision 1.21 diff -c -r1.21 win32.h *** src/interfaces/libpq/win32.h 2002/12/30 21:07:26 1.21 --- src/interfaces/libpq/win32.h 2003/01/09 15:13:37 *************** *** 42,48 **** /* * support for handling Windows Socket errors */ ! extern const char *winsock_strerror(int eno); ! #endif --- 42,47 ---- /* * support for handling Windows Socket errors */ ! extern const char *winsock_strerror(int err, char *strerrbuf, size_t buflen); #endif