]> granicus.if.org Git - postgresql/blobdiff - src/interfaces/libpq/libpq-int.h
Fix libpq certificate validation for SSL connections.
[postgresql] / src / interfaces / libpq / libpq-int.h
index 6e14fa8df25ca3399efb33ea33813d91eef2f755..adeaa35d0bf46304667fb3d9f5f71b7b373e1ab5 100644 (file)
@@ -9,10 +9,10 @@
  *       more likely to break across PostgreSQL releases than code that uses
  *       only the official API.
  *
- * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
+ * Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $PostgreSQL: pgsql/src/interfaces/libpq/libpq-int.h,v 1.102 2005/06/12 00:00:21 neilc Exp $
+ * $PostgreSQL: pgsql/src/interfaces/libpq/libpq-int.h,v 1.137 2008/11/13 09:45:25 mha Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -22,6 +22,7 @@
 
 /* We assume libpq-fe.h has already been included. */
 #include "postgres_fe.h"
+#include "libpq-events.h"
 
 #include <time.h>
 #include <sys/types.h>
 #endif
 
 #ifdef ENABLE_THREAD_SAFETY
+#ifdef WIN32
+#include "pthread-win32.h"
+#else
 #include <pthread.h>
-#include <signal.h>
 #endif
-
-#ifdef WIN32_CLIENT_ONLY
-typedef int ssize_t;                   /* ssize_t doesn't exist in VC (at least
-                                                                * not VC6) */
+#include <signal.h>
 #endif
 
 /* include stuff common to fe and be */
@@ -45,6 +45,31 @@ typedef int ssize_t;                 /* ssize_t doesn't exist in VC (at least
 /* include stuff found in fe only */
 #include "pqexpbuffer.h"
 
+#ifdef ENABLE_GSS
+#if defined(HAVE_GSSAPI_H)
+#include <gssapi.h>
+#else
+#include <gssapi/gssapi.h>
+#endif
+#endif
+
+#ifdef ENABLE_SSPI
+#define SECURITY_WIN32
+#include <security.h>
+#undef SECURITY_WIN32
+
+#ifndef ENABLE_GSS
+/*
+ * Define a fake structure compatible with GSSAPI on Unix.
+ */
+typedef struct
+{
+       void       *value;
+       int                     length;
+}      gss_buffer_desc;
+#endif
+#endif   /* ENABLE_SSPI */
+
 #ifdef USE_SSL
 #include <openssl/ssl.h>
 #include <openssl/err.h>
@@ -53,8 +78,7 @@ typedef int ssize_t;                  /* ssize_t doesn't exist in VC (at least
 /*
  * POSTGRES backend dependent Constants.
  */
-#define PQERRORMSG_LENGTH 1024
-#define CMDSTATUS_LEN 40
+#define CMDSTATUS_LEN 64               /* should match COMPLETION_TAG_BUFSIZE */
 
 /*
  * PGresult and the subsidiary types PGresAttDesc, PGresAttValue
@@ -77,18 +101,11 @@ union pgresult_data
        char            space[1];               /* dummy for accessing block as bytes */
 };
 
-/* Data about a single attribute (column) of a query result */
-
-typedef struct pgresAttDesc
+/* Data about a single parameter of a prepared statement */
+typedef struct pgresParamDesc
 {
-       char       *name;                       /* column name */
-       Oid                     tableid;                /* source table, if known */
-       int                     columnid;               /* source column, if known */
-       int                     format;                 /* format code for value (text/binary) */
        Oid                     typid;                  /* type id */
-       int                     typlen;                 /* type size */
-       int                     atttypmod;              /* type-specific modifier info */
-} PGresAttDesc;
+} PGresParamDesc;
 
 /*
  * Data for a single attribute of a single tuple
@@ -113,8 +130,7 @@ typedef struct pgresAttDesc
 typedef struct pgresAttValue
 {
        int                     len;                    /* length in bytes of the value */
-       char       *value;                      /* actual value, plus terminating zero
-                                                                * byte */
+       char       *value;                      /* actual value, plus terminating zero byte */
 } PGresAttValue;
 
 /* Typedef for message-field list entries */
@@ -134,6 +150,15 @@ typedef struct
        void       *noticeProcArg;
 } PGNoticeHooks;
 
+typedef struct PGEvent
+{
+       PGEventProc     proc;                   /* the function to call on events */
+       char       *name;                       /* used only for error messages */
+       void       *passThrough;        /* pointer supplied at registration time */
+       void       *data;                       /* optional state (instance) data */
+       bool            resultInitialized;      /* T if RESULTCREATE/COPY succeeded */
+} PGEvent;
+
 struct pg_result
 {
        int                     ntups;
@@ -142,23 +167,26 @@ struct pg_result
        PGresAttValue **tuples;         /* each PGresTuple is an array of
                                                                 * PGresAttValue's */
        int                     tupArrSize;             /* allocated size of tuples array */
+       int                     numParameters;
+       PGresParamDesc *paramDescs;
        ExecStatusType resultStatus;
-       char            cmdStatus[CMDSTATUS_LEN];               /* cmd status from the
-                                                                                                * query */
+       char            cmdStatus[CMDSTATUS_LEN];               /* cmd status from the query */
        int                     binary;                 /* binary tuple values if binary == 1,
                                                                 * otherwise text */
 
        /*
-        * These fields are copied from the originating PGconn, so that
-        * operations on the PGresult don't have to reference the PGconn.
+        * These fields are copied from the originating PGconn, so that operations
+        * on the PGresult don't have to reference the PGconn.
         */
        PGNoticeHooks noticeHooks;
+       PGEvent    *events;
+       int                     nEvents;
        int                     client_encoding;        /* encoding id */
 
        /*
         * Error information (all NULL if not an error result).  errMsg is the
-        * "overall" error message returned by PQresultErrorMessage.  If we
-        * have per-field info then it is stored in a linked list.
+        * "overall" error message returned by PQresultErrorMessage.  If we have
+        * per-field info then it is stored in a linked list.
         */
        char       *errMsg;                     /* error message, or NULL if no error */
        PGMessageField *errFields;      /* message broken into fields */
@@ -167,8 +195,8 @@ struct pg_result
        char            null_field[1];
 
        /*
-        * Space management information.  Note that attDescs and error stuff,
-        * if not null, point into allocated blocks.  But tuples points to a
+        * Space management information.  Note that attDescs and error stuff, if
+        * not null, point into allocated blocks.  But tuples points to a
         * separately malloc'd block, so that we can realloc it.
         */
        PGresult_data *curBlock;        /* most recently allocated block */
@@ -191,7 +219,8 @@ typedef enum
 {
        PGQUERY_SIMPLE,                         /* simple Query protocol (PQexec) */
        PGQUERY_EXTENDED,                       /* full Extended protocol (PQexecParams) */
-       PGQUERY_PREPARE                         /* Parse only (PQprepare) */
+       PGQUERY_PREPARE,                        /* Parse only (PQprepare) */
+       PGQUERY_DESCRIBE                        /* Describe Statement or Portal */
 } PGQueryClass;
 
 /* PGSetenvStatusType defines the state of the PQSetenv state machine */
@@ -229,9 +258,11 @@ typedef struct pgLobjfuncs
        Oid                     fn_lo_open;             /* OID of backend function lo_open              */
        Oid                     fn_lo_close;    /* OID of backend function lo_close             */
        Oid                     fn_lo_creat;    /* OID of backend function lo_creat             */
+       Oid                     fn_lo_create;   /* OID of backend function lo_create    */
        Oid                     fn_lo_unlink;   /* OID of backend function lo_unlink    */
        Oid                     fn_lo_lseek;    /* OID of backend function lo_lseek             */
        Oid                     fn_lo_tell;             /* OID of backend function lo_tell              */
+       Oid                     fn_lo_truncate; /* OID of backend function lo_truncate  */
        Oid                     fn_lo_read;             /* OID of backend function LOread               */
        Oid                     fn_lo_write;    /* OID of backend function LOwrite              */
 } PGlobjfuncs;
@@ -243,16 +274,15 @@ typedef struct pgLobjfuncs
 struct pg_conn
 {
        /* Saved values of connection options */
-       char       *pghost;                     /* the machine on which the server is
-                                                                * running */
-       char       *pghostaddr;         /* the IPv4 address of the machine on
-                                                                * which the server is running, in IPv4
-                                                                * numbers-and-dots notation. Takes
-                                                                * precedence over above. */
+       char       *pghost;                     /* the machine on which the server is running */
+       char       *pghostaddr;         /* the IPv4 address of the machine on which
+                                                                * the server is running, in IPv4
+                                                                * numbers-and-dots notation. Takes precedence
+                                                                * over above. */
        char       *pgport;                     /* the server's communication port */
-       char       *pgunixsocket;       /* the Unix-domain socket that the server
-                                                                * is listening on; if NULL, uses a
-                                                                * default constructed from pgport */
+       char       *pgunixsocket;       /* the Unix-domain socket that the server is
+                                                                * listening on; if NULL, uses a default
+                                                                * constructed from pgport */
        char       *pgtty;                      /* tty on which the backend messages is
                                                                 * displayed (OBSOLETE, NOT USED) */
        char       *connect_timeout;    /* connection timeout (numeric string) */
@@ -261,8 +291,9 @@ struct pg_conn
        char       *pguser;                     /* Postgres username and password, if any */
        char       *pgpass;
        char       *sslmode;            /* SSL mode (require,prefer,allow,disable) */
-#if defined(KRB5) || defined(KRB4)
-       char       *krbsrvname;     /* Kerberos service name */
+       char       *sslverify;          /* Verify server SSL certificate (none,chain,cn) */
+#if defined(KRB5) || defined(ENABLE_GSS) || defined(ENABLE_SSPI)
+       char       *krbsrvname;         /* Kerberos service name */
 #endif
 
        /* Optional file to write trace info to */
@@ -271,17 +302,23 @@ struct pg_conn
        /* Callback procedures for notice message processing */
        PGNoticeHooks noticeHooks;
 
+       /* Event procs registered via PQregisterEventProc */
+       PGEvent    *events;                     /* expandable array of event data */
+       int                     nEvents;                /* number of active events */
+       int                     eventArraySize; /* allocated array size */
+
        /* Status indicators */
        ConnStatusType status;
        PGAsyncStatusType asyncStatus;
-       PGTransactionStatusType xactStatus;
-       /* note: xactStatus never changes to ACTIVE */
+       PGTransactionStatusType xactStatus; /* never changes to ACTIVE */
        PGQueryClass queryclass;
-       bool            nonblocking;    /* whether this connection is using
-                                                                * nonblock sending semantics */
+       char       *last_query;         /* last SQL command, or NULL if unknown */
+       bool            options_valid;  /* true if OK to attempt connection */
+       bool            nonblocking;    /* whether this connection is using nonblock
+                                                                * sending semantics */
        char            copy_is_binary; /* 1 = copy binary, 0 = copy text */
-       int                     copy_already_done;              /* # bytes already returned in
-                                                                                * COPY OUT */
+       int                     copy_already_done;              /* # bytes already returned in COPY
+                                                                                * OUT */
        PGnotify   *notifyHead;         /* oldest unreported Notify msg */
        PGnotify   *notifyTail;         /* newest unreported Notify msg */
 
@@ -291,6 +328,7 @@ struct pg_conn
        SockAddr        raddr;                  /* Remote address */
        ProtocolVersion pversion;       /* FE/BE protocol version in use */
        int                     sversion;               /* server version, e.g. 70401 for 7.4.1 */
+       bool            password_needed;        /* true if server demanded a password */
 
        /* Transient state needed while establishing connection */
        struct addrinfo *addrlist;      /* list of possible backend addresses */
@@ -303,21 +341,18 @@ struct pg_conn
        int                     be_pid;                 /* PID of backend --- needed for cancels */
        int                     be_key;                 /* key of backend --- needed for cancels */
        char            md5Salt[4];             /* password salt received from backend */
-       char            cryptSalt[2];   /* password salt received from backend */
        pgParameterStatus *pstatus; /* ParameterStatus data */
        int                     client_encoding;        /* encoding id */
+       bool            std_strings;    /* standard_conforming_strings */
        PGVerbosity verbosity;          /* error/notice message verbosity */
-       PGlobjfuncs *lobjfuncs;         /* private state for large-object access
-                                                                * fns */
+       PGlobjfuncs *lobjfuncs;         /* private state for large-object access fns */
 
        /* Buffer for data received from backend and not yet processed */
        char       *inBuffer;           /* currently allocated buffer */
        int                     inBufSize;              /* allocated size of buffer */
-       int                     inStart;                /* offset to first unconsumed data in
-                                                                * buffer */
+       int                     inStart;                /* offset to first unconsumed data in buffer */
        int                     inCursor;               /* next byte to tentatively consume */
-       int                     inEnd;                  /* offset to first position after avail
-                                                                * data */
+       int                     inEnd;                  /* offset to first position after avail data */
 
        /* Buffer for data not yet sent to backend */
        char       *outBuffer;          /* currently allocated buffer */
@@ -325,8 +360,8 @@ struct pg_conn
        int                     outCount;               /* number of chars waiting in buffer */
 
        /* State for constructing messages in outBuffer */
-       int                     outMsgStart;    /* offset to msg start (length word); if
-                                                                * -1, msg has no length word */
+       int                     outMsgStart;    /* offset to msg start (length word); if -1,
+                                                                * msg has no length word */
        int                     outMsgEnd;              /* offset to msg end (so far) */
 
        /* Status for asynchronous result construction */
@@ -343,6 +378,28 @@ struct pg_conn
        char            peer_cn[SM_USER + 1];   /* peer common name */
 #endif
 
+#ifdef ENABLE_GSS
+       gss_ctx_id_t gctx;                      /* GSS context */
+       gss_name_t      gtarg_nam;              /* GSS target name */
+       gss_buffer_desc ginbuf;         /* GSS input token */
+       gss_buffer_desc goutbuf;        /* GSS output token */
+#endif
+
+#ifdef ENABLE_SSPI
+#ifndef ENABLE_GSS
+       gss_buffer_desc ginbuf;         /* GSS input token */
+#else
+       char       *gsslib;                     /* What GSS librart to use ("gssapi" or
+                                                                * "sspi") */
+#endif
+       CredHandle *sspicred;           /* SSPI credentials handle */
+       CtxtHandle *sspictx;            /* SSPI context */
+       char       *sspitarget;         /* SSPI target name */
+       int                     usesspi;                /* Indicate if SSPI is in use on the
+                                                                * connection */
+#endif
+
+
        /* Buffer for current error message */
        PQExpBufferData errorMessage;           /* expansible string */
 
@@ -385,6 +442,13 @@ extern bool pqGetHomeDirectory(char *buf, int bufsize);
 #ifdef ENABLE_THREAD_SAFETY
 extern pgthreadlock_t pg_g_threadlock;
 
+#define PGTHREAD_ERROR(msg) \
+       do { \
+               fprintf(stderr, "%s\n", msg); \
+               exit(1); \
+       } while (0)
+
+
 #define pglock_thread()                pg_g_threadlock(true)
 #define pgunlock_thread()      pg_g_threadlock(false)
 #else
@@ -392,7 +456,6 @@ extern pgthreadlock_t pg_g_threadlock;
 #define pgunlock_thread()      ((void) 0)
 #endif
 
-
 /* === in fe-exec.c === */
 
 extern void pqSetResultError(PGresult *res, const char *msg);
@@ -403,7 +466,7 @@ extern void pqClearAsyncResult(PGconn *conn);
 extern void pqSaveErrorResult(PGconn *conn);
 extern PGresult *pqPrepareAsyncResult(PGconn *conn);
 extern void
-pqInternalNotice(const PGNoticeHooks *hooks, const char *fmt, ...)
+pqInternalNotice(const PGNoticeHooks *hooks, const char *fmt,...)
 /* This lets gcc check the format string for consistency. */
 __attribute__((format(printf, 2, 3)));
 extern int     pqAddTuple(PGresult *res, PGresAttValue *tup);
@@ -447,15 +510,16 @@ extern PGresult *pqFunctionCall3(PGconn *conn, Oid fnid,
 /* === in fe-misc.c === */
 
  /*
-  * "Get" and "Put" routines return 0 if successful, EOF if not. Note that
-  * for Get, EOF merely means the buffer is exhausted, not that there is
+  * "Get" and "Put" routines return 0 if successful, EOF if not. Note that for
+  * Get, EOF merely means the buffer is exhausted, not that there is
   * necessarily any error.
   */
-extern int     pqCheckOutBufferSpace(int bytes_needed, PGconn *conn);
-extern int     pqCheckInBufferSpace(int bytes_needed, PGconn *conn);
+extern int     pqCheckOutBufferSpace(size_t bytes_needed, PGconn *conn);
+extern int     pqCheckInBufferSpace(size_t bytes_needed, PGconn *conn);
 extern int     pqGetc(char *result, PGconn *conn);
 extern int     pqPutc(char c, PGconn *conn);
 extern int     pqGets(PQExpBuffer buf, PGconn *conn);
+extern int     pqGets_append(PQExpBuffer buf, PGconn *conn);
 extern int     pqPuts(const char *s, PGconn *conn);
 extern int     pqGetnchar(char *s, size_t len, PGconn *conn);
 extern int     pqPutnchar(const char *s, size_t len, PGconn *conn);
@@ -480,10 +544,10 @@ extern void pqsecure_close(PGconn *);
 extern ssize_t pqsecure_read(PGconn *, void *ptr, size_t len);
 extern ssize_t pqsecure_write(PGconn *, const void *ptr, size_t len);
 
-#ifdef ENABLE_THREAD_SAFETY
+#if defined(ENABLE_THREAD_SAFETY) && !defined(WIN32)
 extern int     pq_block_sigpipe(sigset_t *osigset, bool *sigpipe_pending);
 extern void pq_reset_sigpipe(sigset_t *osigset, bool sigpipe_pending,
-                                                        bool got_epipe);
+                                bool got_epipe);
 #endif
 
 /*
@@ -496,7 +560,6 @@ extern void pq_reset_sigpipe(sigset_t *osigset, bool sigpipe_pending,
 extern char *
 libpq_gettext(const char *msgid)
 __attribute__((format_arg(1)));
-
 #else
 #define libpq_gettext(x) (x)
 #endif