| 1 | /*------------------------------------------------------------------------- |
| 2 | * |
| 3 | * pqcomm.h |
| 4 | * Definitions common to frontends and backends. |
| 5 | * |
| 6 | * NOTE: for historical reasons, this does not correspond to pqcomm.c. |
| 7 | * pqcomm.c's routines are declared in libpq.h. |
| 8 | * |
| 9 | * Portions Copyright (c) 1996-2019, PostgreSQL Global Development Group |
| 10 | * Portions Copyright (c) 1994, Regents of the University of California |
| 11 | * |
| 12 | * src/include/libpq/pqcomm.h |
| 13 | * |
| 14 | *------------------------------------------------------------------------- |
| 15 | */ |
| 16 | #ifndef PQCOMM_H |
| 17 | #define PQCOMM_H |
| 18 | |
| 19 | #include <sys/socket.h> |
| 20 | #include <netdb.h> |
| 21 | #ifdef HAVE_SYS_UN_H |
| 22 | #include <sys/un.h> |
| 23 | #endif |
| 24 | #include <netinet/in.h> |
| 25 | |
| 26 | #ifdef HAVE_STRUCT_SOCKADDR_STORAGE |
| 27 | |
| 28 | #ifndef HAVE_STRUCT_SOCKADDR_STORAGE_SS_FAMILY |
| 29 | #ifdef HAVE_STRUCT_SOCKADDR_STORAGE___SS_FAMILY |
| 30 | #define ss_family __ss_family |
| 31 | #else |
| 32 | #error struct sockaddr_storage does not provide an ss_family member |
| 33 | #endif |
| 34 | #endif |
| 35 | |
| 36 | #ifdef HAVE_STRUCT_SOCKADDR_STORAGE___SS_LEN |
| 37 | #define ss_len __ss_len |
| 38 | #define HAVE_STRUCT_SOCKADDR_STORAGE_SS_LEN 1 |
| 39 | #endif |
| 40 | #else /* !HAVE_STRUCT_SOCKADDR_STORAGE */ |
| 41 | |
| 42 | /* Define a struct sockaddr_storage if we don't have one. */ |
| 43 | |
| 44 | struct sockaddr_storage |
| 45 | { |
| 46 | union |
| 47 | { |
| 48 | struct sockaddr sa; /* get the system-dependent fields */ |
| 49 | int64 ss_align; /* ensures struct is properly aligned */ |
| 50 | char ss_pad[128]; /* ensures struct has desired size */ |
| 51 | } ss_stuff; |
| 52 | }; |
| 53 | |
| 54 | #define ss_family ss_stuff.sa.sa_family |
| 55 | /* It should have an ss_len field if sockaddr has sa_len. */ |
| 56 | #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN |
| 57 | #define ss_len ss_stuff.sa.sa_len |
| 58 | #define HAVE_STRUCT_SOCKADDR_STORAGE_SS_LEN 1 |
| 59 | #endif |
| 60 | #endif /* HAVE_STRUCT_SOCKADDR_STORAGE */ |
| 61 | |
| 62 | typedef struct |
| 63 | { |
| 64 | struct sockaddr_storage addr; |
| 65 | ACCEPT_TYPE_ARG3 salen; |
| 66 | } SockAddr; |
| 67 | |
| 68 | /* Configure the UNIX socket location for the well known port. */ |
| 69 | |
| 70 | #define UNIXSOCK_PATH(path, port, sockdir) \ |
| 71 | snprintf(path, sizeof(path), "%s/.s.PGSQL.%d", \ |
| 72 | ((sockdir) && *(sockdir) != '\0') ? (sockdir) : \ |
| 73 | DEFAULT_PGSOCKET_DIR, \ |
| 74 | (port)) |
| 75 | |
| 76 | /* |
| 77 | * The maximum workable length of a socket path is what will fit into |
| 78 | * struct sockaddr_un. This is usually only 100 or so bytes :-(. |
| 79 | * |
| 80 | * For consistency, always pass a MAXPGPATH-sized buffer to UNIXSOCK_PATH(), |
| 81 | * then complain if the resulting string is >= UNIXSOCK_PATH_BUFLEN bytes. |
| 82 | * (Because the standard API for getaddrinfo doesn't allow it to complain in |
| 83 | * a useful way when the socket pathname is too long, we have to test for |
| 84 | * this explicitly, instead of just letting the subroutine return an error.) |
| 85 | */ |
| 86 | #define UNIXSOCK_PATH_BUFLEN sizeof(((struct sockaddr_un *) NULL)->sun_path) |
| 87 | |
| 88 | |
| 89 | /* |
| 90 | * These manipulate the frontend/backend protocol version number. |
| 91 | * |
| 92 | * The major number should be incremented for incompatible changes. The minor |
| 93 | * number should be incremented for compatible changes (eg. additional |
| 94 | * functionality). |
| 95 | * |
| 96 | * If a backend supports version m.n of the protocol it must actually support |
| 97 | * versions m.[0..n]. Backend support for version m-1 can be dropped after a |
| 98 | * `reasonable' length of time. |
| 99 | * |
| 100 | * A frontend isn't required to support anything other than the current |
| 101 | * version. |
| 102 | */ |
| 103 | |
| 104 | #define PG_PROTOCOL_MAJOR(v) ((v) >> 16) |
| 105 | #define PG_PROTOCOL_MINOR(v) ((v) & 0x0000ffff) |
| 106 | #define PG_PROTOCOL(m,n) (((m) << 16) | (n)) |
| 107 | |
| 108 | /* The earliest and latest frontend/backend protocol version supported. */ |
| 109 | |
| 110 | #define PG_PROTOCOL_EARLIEST PG_PROTOCOL(2,0) |
| 111 | #define PG_PROTOCOL_LATEST PG_PROTOCOL(3,0) |
| 112 | |
| 113 | typedef uint32 ProtocolVersion; /* FE/BE protocol version number */ |
| 114 | |
| 115 | typedef ProtocolVersion MsgType; |
| 116 | |
| 117 | |
| 118 | /* |
| 119 | * Packet lengths are 4 bytes in network byte order. |
| 120 | * |
| 121 | * The initial length is omitted from the packet layouts appearing below. |
| 122 | */ |
| 123 | |
| 124 | typedef uint32 PacketLen; |
| 125 | |
| 126 | |
| 127 | /* |
| 128 | * Old-style startup packet layout with fixed-width fields. This is used in |
| 129 | * protocol 1.0 and 2.0, but not in later versions. Note that the fields |
| 130 | * in this layout are '\0' terminated only if there is room. |
| 131 | */ |
| 132 | |
| 133 | #define SM_DATABASE 64 |
| 134 | #define SM_USER 32 |
| 135 | /* We append database name if db_user_namespace true. */ |
| 136 | #define SM_DATABASE_USER (SM_DATABASE+SM_USER+1) /* +1 for @ */ |
| 137 | #define SM_OPTIONS 64 |
| 138 | #define SM_UNUSED 64 |
| 139 | #define SM_TTY 64 |
| 140 | |
| 141 | typedef struct StartupPacket |
| 142 | { |
| 143 | ProtocolVersion protoVersion; /* Protocol version */ |
| 144 | char database[SM_DATABASE]; /* Database name */ |
| 145 | /* Db_user_namespace appends dbname */ |
| 146 | char user[SM_USER]; /* User name */ |
| 147 | char options[SM_OPTIONS]; /* Optional additional args */ |
| 148 | char unused[SM_UNUSED]; /* Unused */ |
| 149 | char tty[SM_TTY]; /* Tty for debug output */ |
| 150 | } StartupPacket; |
| 151 | |
| 152 | extern bool Db_user_namespace; |
| 153 | |
| 154 | /* |
| 155 | * In protocol 3.0 and later, the startup packet length is not fixed, but |
| 156 | * we set an arbitrary limit on it anyway. This is just to prevent simple |
| 157 | * denial-of-service attacks via sending enough data to run the server |
| 158 | * out of memory. |
| 159 | */ |
| 160 | #define MAX_STARTUP_PACKET_LENGTH 10000 |
| 161 | |
| 162 | |
| 163 | /* These are the authentication request codes sent by the backend. */ |
| 164 | |
| 165 | #define AUTH_REQ_OK 0 /* User is authenticated */ |
| 166 | #define AUTH_REQ_KRB4 1 /* Kerberos V4. Not supported any more. */ |
| 167 | #define AUTH_REQ_KRB5 2 /* Kerberos V5. Not supported any more. */ |
| 168 | #define AUTH_REQ_PASSWORD 3 /* Password */ |
| 169 | #define AUTH_REQ_CRYPT 4 /* crypt password. Not supported any more. */ |
| 170 | #define AUTH_REQ_MD5 5 /* md5 password */ |
| 171 | #define AUTH_REQ_SCM_CREDS 6 /* transfer SCM credentials */ |
| 172 | #define AUTH_REQ_GSS 7 /* GSSAPI without wrap() */ |
| 173 | #define AUTH_REQ_GSS_CONT 8 /* Continue GSS exchanges */ |
| 174 | #define AUTH_REQ_SSPI 9 /* SSPI negotiate without wrap() */ |
| 175 | #define AUTH_REQ_SASL 10 /* Begin SASL authentication */ |
| 176 | #define AUTH_REQ_SASL_CONT 11 /* Continue SASL authentication */ |
| 177 | #define AUTH_REQ_SASL_FIN 12 /* Final SASL message */ |
| 178 | |
| 179 | typedef uint32 AuthRequest; |
| 180 | |
| 181 | |
| 182 | /* |
| 183 | * A client can also send a cancel-current-operation request to the postmaster. |
| 184 | * This is uglier than sending it directly to the client's backend, but it |
| 185 | * avoids depending on out-of-band communication facilities. |
| 186 | * |
| 187 | * The cancel request code must not match any protocol version number |
| 188 | * we're ever likely to use. This random choice should do. |
| 189 | */ |
| 190 | #define CANCEL_REQUEST_CODE PG_PROTOCOL(1234,5678) |
| 191 | |
| 192 | typedef struct CancelRequestPacket |
| 193 | { |
| 194 | /* Note that each field is stored in network byte order! */ |
| 195 | MsgType cancelRequestCode; /* code to identify a cancel request */ |
| 196 | uint32 backendPID; /* PID of client's backend */ |
| 197 | uint32 cancelAuthCode; /* secret key to authorize cancel */ |
| 198 | } CancelRequestPacket; |
| 199 | |
| 200 | |
| 201 | /* |
| 202 | * A client can also start by sending a SSL or GSSAPI negotiation request to |
| 203 | * get a secure channel. |
| 204 | */ |
| 205 | #define NEGOTIATE_SSL_CODE PG_PROTOCOL(1234,5679) |
| 206 | #define NEGOTIATE_GSS_CODE PG_PROTOCOL(1234,5680) |
| 207 | |
| 208 | #endif /* PQCOMM_H */ |
| 209 | |