1 /*-------------------------------------------------------------------------
2  *
3  * libpq_be.h
4  *	  This file contains definitions for structures and externs used
5  *	  by the postmaster during client authentication.
6  *
7  *	  Note that this is backend-internal and is NOT exported to clients.
8  *	  Structs that need to be client-visible are in pqcomm.h.
9  *
10  *
11  * Portions Copyright (c) 1996-2013, PostgreSQL Global Development Group
12  * Portions Copyright (c) 1994, Regents of the University of California
13  *
14  * src/include/libpq/libpq-be.h
15  *
16  *-------------------------------------------------------------------------
17  */
18 #ifndef LIBPQ_BE_H
19 #define LIBPQ_BE_H
20 
21 #ifdef HAVE_SYS_TIME_H
22 #include <sys/time.h>
23 #endif
24 #ifdef USE_SSL
25 #include <openssl/ssl.h>
26 #include <openssl/err.h>
27 #endif
28 #ifdef HAVE_NETINET_TCP_H
29 #include <netinet/tcp.h>
30 #endif
31 
32 #ifdef ENABLE_GSS
33 #if defined(HAVE_GSSAPI_H)
34 #include <gssapi.h>
35 #else
36 #include <gssapi/gssapi.h>
37 #endif   /* HAVE_GSSAPI_H */
38 /*
39  * GSSAPI brings in headers that set a lot of things in the global namespace on win32,
40  * that doesn't match the msvc build. It gives a bunch of compiler warnings that we ignore,
41  * but also defines a symbol that simply does not exist. Undefine it again.
42  */
43 #ifdef WIN32_ONLY_COMPILER
44 #undef HAVE_GETADDRINFO
45 #endif
46 #endif   /* ENABLE_GSS */
47 
48 #ifdef ENABLE_SSPI
49 #define SECURITY_WIN32
50 #if defined(WIN32) && !defined(WIN32_ONLY_COMPILER)
51 #include <ntsecapi.h>
52 #endif
53 #include <security.h>
54 #undef SECURITY_WIN32
55 
56 #ifndef ENABLE_GSS
57 /*
58  * Define a fake structure compatible with GSSAPI on Unix.
59  */
60 typedef struct
61 {
62 	void	   *value;
63 	int			length;
64 } gss_buffer_desc;
65 #endif
66 #endif   /* ENABLE_SSPI */
67 
68 #include "datatype/timestamp.h"
69 #include "libpq/hba.h"
70 #include "libpq/pqcomm.h"
71 
72 
73 typedef enum CAC_state
74 {
75 	CAC_OK, CAC_STARTUP, CAC_SHUTDOWN, CAC_RECOVERY, CAC_TOOMANY,
76 	CAC_WAITBACKUP
77 } CAC_state;
78 
79 
80 /*
81  * GSSAPI specific state information
82  */
83 #if defined(ENABLE_GSS) | defined(ENABLE_SSPI)
84 typedef struct
85 {
86 	gss_buffer_desc outbuf;		/* GSSAPI output token buffer */
87 #ifdef ENABLE_GSS
88 	gss_cred_id_t cred;			/* GSSAPI connection cred's */
89 	gss_ctx_id_t ctx;			/* GSSAPI connection context */
90 	gss_name_t	name;			/* GSSAPI client name */
91 #endif
92 } pg_gssinfo;
93 #endif
94 
95 /*
96  * This is used by the postmaster in its communication with frontends.	It
97  * contains all state information needed during this communication before the
98  * backend is run.	The Port structure is kept in malloc'd memory and is
99  * still available when a backend is running (see MyProcPort).	The data
100  * it points to must also be malloc'd, or else palloc'd in TopMemoryContext,
101  * so that it survives into PostgresMain execution!
102  */
103 
104 typedef struct Port
105 {
106 	pgsocket	sock;			/* File descriptor */
107 	bool		noblock;		/* is the socket in non-blocking mode? */
108 	ProtocolVersion proto;		/* FE/BE protocol version */
109 	SockAddr	laddr;			/* local addr (postmaster) */
110 	SockAddr	raddr;			/* remote addr (client) */
111 	char	   *remote_host;	/* name (or ip addr) of remote host */
112 	char	   *remote_hostname;/* name (not ip addr) of remote host, if
113 								 * available */
114 	int			remote_hostname_resolv; /* +1 = remote_hostname is known to
115 										 * resolve to client's IP address; -1
116 										 * = remote_hostname is known NOT to
117 										 * resolve to client's IP address; 0 =
118 										 * we have not done the forward DNS
119 										 * lookup yet */
120 	char	   *remote_port;	/* text rep of remote port */
121 	CAC_state	canAcceptConnections;	/* postmaster connection status */
122 
123 	/*
124 	 * Information that needs to be saved from the startup packet and passed
125 	 * into backend execution.	"char *" fields are NULL if not set.
126 	 * guc_options points to a List of alternating option names and values.
127 	 */
128 	char	   *database_name;
129 	char	   *user_name;
130 	char	   *cmdline_options;
131 	List	   *guc_options;
132 
133 	/*
134 	 * Information that needs to be held during the authentication cycle.
135 	 */
136 	HbaLine    *hba;
137 	char		md5Salt[4];		/* Password salt */
138 
139 	/*
140 	 * Information that really has no business at all being in struct Port,
141 	 * but since it gets used by elog.c in the same way as database_name and
142 	 * other members of this struct, we may as well keep it here.
143 	 */
144 	TimestampTz SessionStartTime;		/* backend start time */
145 
146 	/*
147 	 * TCP keepalive settings.
148 	 *
149 	 * default values are 0 if AF_UNIX or not yet known; current values are 0
150 	 * if AF_UNIX or using the default. Also, -1 in a default value means we
151 	 * were unable to find out the default (getsockopt failed).
152 	 */
153 	int			default_keepalives_idle;
154 	int			default_keepalives_interval;
155 	int			default_keepalives_count;
156 	int			keepalives_idle;
157 	int			keepalives_interval;
158 	int			keepalives_count;
159 
160 #if defined(ENABLE_GSS) || defined(ENABLE_SSPI)
161 
162 	/*
163 	 * If GSSAPI is supported, store GSSAPI information. Otherwise, store a
164 	 * NULL pointer to make sure offsets in the struct remain the same.
165 	 */
166 	pg_gssinfo *gss;
167 #else
168 	void	   *gss;
169 #endif
170 
171 	/*
172 	 * SSL structures (keep these last so that USE_SSL doesn't affect
173 	 * locations of other fields)
174 	 */
175 #ifdef USE_SSL
176 	SSL		   *ssl;
177 	X509	   *peer;
178 	char	   *peer_cn;
179 	unsigned long count;
180 #endif
181 } Port;
182 
183 
184 extern ProtocolVersion FrontendProtocol;
185 
186 /* TCP keepalives configuration. These are no-ops on an AF_UNIX socket. */
187 
188 extern int	pq_getkeepalivesidle(Port *port);
189 extern int	pq_getkeepalivesinterval(Port *port);
190 extern int	pq_getkeepalivescount(Port *port);
191 
192 extern int	pq_setkeepalivesidle(int idle, Port *port);
193 extern int	pq_setkeepalivesinterval(int interval, Port *port);
194 extern int	pq_setkeepalivescount(int count, Port *port);
195 
196 #endif   /* LIBPQ_BE_H */
197