]> granicus.if.org Git - postgresql/blob - src/include/libpq/pqcomm.h
The attached patch implements the password packet length sanity check
[postgresql] / src / include / libpq / pqcomm.h
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-2002, PostgreSQL Global Development Group
10  * Portions Copyright (c) 1994, Regents of the University of California
11  *
12  * $Id: pqcomm.h,v 1.68 2002/08/27 16:21:51 momjian Exp $
13  *
14  *-------------------------------------------------------------------------
15  */
16 #ifndef PQCOMM_H
17 #define PQCOMM_H
18
19 #include <sys/types.h>
20 #ifdef WIN32
21 #include <winsock.h>
22 /* workaround for clashing defines of "ERROR" */
23 #ifdef ELOG_H
24 #undef ERROR
25 #define ERROR   (-1)
26 #endif
27 #else                                                   /* not WIN32 */
28 #include <sys/socket.h>
29 #ifdef HAVE_SYS_UN_H
30 #include <sys/un.h>
31 #endif
32 #include <netinet/in.h>
33 #endif   /* not WIN32 */
34
35
36 #ifndef HAVE_STRUCT_SOCKADDR_UN
37 struct sockaddr_un
38 {
39         short int       sun_family;             /* AF_UNIX */
40         char            sun_path[108];  /* path name (gag) */
41 };
42 #endif
43
44 /* Define a generic socket address type. */
45
46 typedef union SockAddr
47 {
48         struct sockaddr sa;
49         struct sockaddr_in in;
50         struct sockaddr_un un;
51 } SockAddr;
52
53
54 /* Configure the UNIX socket location for the well known port. */
55
56 #define UNIXSOCK_PATH(sun,port,defpath) \
57                 snprintf((sun).sun_path, sizeof((sun).sun_path), "%s/.s.PGSQL.%d", \
58                                 ((defpath) && *(defpath) != '\0') ? (defpath) : \
59                                 DEFAULT_PGSOCKET_DIR, \
60                                 (port))
61
62 /*
63  *              We do this because sun_len is in BSD's struct, while others don't.
64  *              We never actually set BSD's sun_len, and I can't think of a
65  *              platform-safe way of doing it, but the code still works. bjm
66  */
67 #if defined(SUN_LEN)
68 #define UNIXSOCK_LEN(sun) \
69                 (SUN_LEN(&(sun)))
70 #else
71 #define UNIXSOCK_LEN(sun) \
72                 (strlen((sun).sun_path) + offsetof(struct sockaddr_un, sun_path))
73 #endif
74
75 /*
76  * These manipulate the frontend/backend protocol version number.
77  *
78  * The major number should be incremented for incompatible changes.  The minor
79  * number should be incremented for compatible changes (eg. additional
80  * functionality).
81  *
82  * If a backend supports version m.n of the protocol it must actually support
83  * versions m.0..n].  Backend support for version m-1 can be dropped after a
84  * `reasonable' length of time.
85  *
86  * A frontend isn't required to support anything other than the current
87  * version.
88  */
89
90 #define PG_PROTOCOL_MAJOR(v)    ((v) >> 16)
91 #define PG_PROTOCOL_MINOR(v)    ((v) & 0x0000ffff)
92 #define PG_PROTOCOL(m,n)        (((m) << 16) | (n))
93
94 /* The earliest and latest frontend/backend protocol version supported. */
95
96 #define PG_PROTOCOL_EARLIEST    PG_PROTOCOL(0,0)
97 #define PG_PROTOCOL_LATEST      PG_PROTOCOL(2,0)
98
99 /*
100  * All packets sent to the postmaster start with the length.  This is omitted
101  * from the different packet definitions specified below.
102  */
103
104 typedef uint32 PacketLen;
105
106
107 /*
108  * Startup message parameters sizes.  These must not be changed without changing
109  * the protocol version.  These are all strings that are '\0' terminated only if
110  * there is room.
111  */
112
113 /*
114  * FIXME: remove the fixed size limitations on the database name, user
115  * name, and options fields and use a variable length field instead. The
116  * actual limits on database & user name will then be NAMEDATALEN, which
117  * can be changed without changing the FE/BE protocol. -neilc,2002/08/27
118  */
119  
120 #define SM_DATABASE             64
121 #define SM_USER                 32
122 /* We append database name if db_user_namespace true. */
123 #define SM_DATABASE_USER (SM_DATABASE+SM_USER+1) /* +1 for @ */
124 #define SM_OPTIONS              64
125 #define SM_UNUSED               64
126 #define SM_TTY                  64
127
128 typedef uint32 ProtocolVersion; /* Fe/Be protocol version number */
129
130 typedef struct StartupPacket
131 {
132         ProtocolVersion protoVersion;           /* Protocol version */
133         char            database[SM_DATABASE];  /* Database name */
134                                 /* Db_user_namespace appends dbname */
135         char            user[SM_USER];  /* User name */
136         char            options[SM_OPTIONS];    /* Optional additional args */
137         char            unused[SM_UNUSED];              /* Unused */
138         char            tty[SM_TTY];    /* Tty for debug output */
139 } StartupPacket;
140
141 extern bool Db_user_namespace;
142
143 /* These are the authentication requests sent by the backend. */
144
145 #define AUTH_REQ_OK                     0       /* User is authenticated  */
146 #define AUTH_REQ_KRB4           1       /* Kerberos V4 */
147 #define AUTH_REQ_KRB5           2       /* Kerberos V5 */
148 #define AUTH_REQ_PASSWORD       3       /* Password */
149 #define AUTH_REQ_CRYPT          4       /* crypt password */
150 #define AUTH_REQ_MD5            5       /* md5 password */
151 #define AUTH_REQ_SCM_CREDS      6       /* transfer SCM credentials */
152
153 typedef uint32 AuthRequest;
154
155
156 /* This next section is to maintain compatibility with protocol v0.0. */
157
158 #define STARTUP_MSG             7               /* Initialise a connection */
159 #define STARTUP_KRB4_MSG        10      /* krb4 session follows */
160 #define STARTUP_KRB5_MSG        11      /* krb5 session follows */
161 #define STARTUP_PASSWORD_MSG    14              /* Password follows */
162
163 typedef ProtocolVersion MsgType;
164
165
166 /* A client can also send a cancel-current-operation request to the postmaster.
167  * This is uglier than sending it directly to the client's backend, but it
168  * avoids depending on out-of-band communication facilities.
169  */
170
171 /* The cancel request code must not match any protocol version number
172  * we're ever likely to use.  This random choice should do.
173  */
174 #define CANCEL_REQUEST_CODE PG_PROTOCOL(1234,5678)
175
176 typedef struct CancelRequestPacket
177 {
178         /* Note that each field is stored in network byte order! */
179         MsgType         cancelRequestCode;              /* code to identify a cancel
180                                                                                  * request */
181         uint32          backendPID;             /* PID of client's backend */
182         uint32          cancelAuthCode; /* secret key to authorize cancel */
183 } CancelRequestPacket;
184
185
186 /*
187  * A client can also start by sending a SSL negotiation request, to get a
188  * secure channel.
189  */
190 #define NEGOTIATE_SSL_CODE PG_PROTOCOL(1234,5679)
191
192 #endif   /* PQCOMM_H */