]> git.ipfire.org Git - thirdparty/cups.git/blob - cups/http-private.h
Merge pull request #5410 from AOSC-Dev/master
[thirdparty/cups.git] / cups / http-private.h
1 /*
2 * Private HTTP definitions for CUPS.
3 *
4 * Copyright 2007-2018 by Apple Inc.
5 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
6 *
7 * Licensed under Apache License v2.0. See the file "LICENSE" for more
8 * information.
9 */
10
11 #ifndef _CUPS_HTTP_PRIVATE_H_
12 # define _CUPS_HTTP_PRIVATE_H_
13
14 /*
15 * Include necessary headers...
16 */
17
18 # include "config.h"
19 # include <cups/language.h>
20 # include <stddef.h>
21 # include <stdlib.h>
22
23 # ifdef __sun
24 # include <sys/select.h>
25 # endif /* __sun */
26
27 # include <limits.h>
28 # ifdef _WIN32
29 # define _WINSOCK_DEPRECATED_NO_WARNINGS 1
30 # include <io.h>
31 # include <winsock2.h>
32 # define CUPS_SOCAST (const char *)
33 # else
34 # include <unistd.h>
35 # include <fcntl.h>
36 # include <sys/socket.h>
37 # define CUPS_SOCAST
38 # endif /* _WIN32 */
39
40 # ifdef HAVE_GSSAPI
41 # ifdef HAVE_GSS_GSSAPI_H
42 # include <GSS/gssapi.h>
43 # elif defined(HAVE_GSSAPI_GSSAPI_H)
44 # include <gssapi/gssapi.h>
45 # elif defined(HAVE_GSSAPI_H)
46 # include <gssapi.h>
47 # endif /* HAVE_GSS_GSSAPI_H */
48 # ifndef HAVE_GSS_C_NT_HOSTBASED_SERVICE
49 # define GSS_C_NT_HOSTBASED_SERVICE gss_nt_service_name
50 # endif /* !HAVE_GSS_C_NT_HOSTBASED_SERVICE */
51 # endif /* HAVE_GSSAPI */
52
53 # ifdef HAVE_AUTHORIZATION_H
54 # include <Security/Authorization.h>
55 # endif /* HAVE_AUTHORIZATION_H */
56
57 # if defined(__APPLE__) && !defined(_SOCKLEN_T)
58 /*
59 * macOS 10.2.x does not define socklen_t, and in fact uses an int instead of
60 * unsigned type for length values...
61 */
62
63 typedef int socklen_t;
64 # endif /* __APPLE__ && !_SOCKLEN_T */
65
66 # include <cups/http.h>
67 # include "ipp-private.h"
68
69 # ifdef HAVE_GNUTLS
70 # include <gnutls/gnutls.h>
71 # include <gnutls/x509.h>
72 # elif defined(HAVE_CDSASSL)
73 # include <CoreFoundation/CoreFoundation.h>
74 # include <Security/Security.h>
75 # include <Security/SecureTransport.h>
76 # ifdef HAVE_SECITEM_H
77 # include <Security/SecItem.h>
78 # endif /* HAVE_SECITEM_H */
79 # ifdef HAVE_SECCERTIFICATE_H
80 # include <Security/SecCertificate.h>
81 # include <Security/SecIdentity.h>
82 # endif /* HAVE_SECCERTIFICATE_H */
83 # elif defined(HAVE_SSPISSL)
84 # include <wincrypt.h>
85 # include <wintrust.h>
86 # include <schannel.h>
87 # define SECURITY_WIN32
88 # include <security.h>
89 # include <sspi.h>
90 # endif /* HAVE_GNUTLS */
91
92 # ifndef _WIN32
93 # include <net/if.h>
94 # include <resolv.h>
95 # ifdef HAVE_GETIFADDRS
96 # include <ifaddrs.h>
97 # else
98 # include <sys/ioctl.h>
99 # ifdef HAVE_SYS_SOCKIO_H
100 # include <sys/sockio.h>
101 # endif /* HAVE_SYS_SOCKIO_H */
102 # endif /* HAVE_GETIFADDRS */
103 # endif /* !_WIN32 */
104
105
106 /*
107 * C++ magic...
108 */
109
110 # ifdef __cplusplus
111 extern "C" {
112 # endif /* __cplusplus */
113
114
115 /*
116 * Constants...
117 */
118
119 # define _HTTP_MAX_SBUFFER 65536 /* Size of (de)compression buffer */
120 # define _HTTP_RESOLVE_DEFAULT 0 /* Just resolve with default options */
121 # define _HTTP_RESOLVE_STDERR 1 /* Log resolve progress to stderr */
122 # define _HTTP_RESOLVE_FQDN 2 /* Resolve to a FQDN */
123 # define _HTTP_RESOLVE_FAXOUT 4 /* Resolve FaxOut service? */
124
125 # define _HTTP_TLS_NONE 0 /* No TLS options */
126 # define _HTTP_TLS_ALLOW_RC4 1 /* Allow RC4 cipher suites */
127 # define _HTTP_TLS_ALLOW_DH 2 /* Allow DH/DHE key negotiation */
128 # define _HTTP_TLS_DENY_CBC 4 /* Deny CBC cipher suites */
129 # define _HTTP_TLS_SET_DEFAULT 128 /* Setting the default TLS options */
130
131 # define _HTTP_TLS_SSL3 0 /* Min/max version is SSL/3.0 */
132 # define _HTTP_TLS_1_0 1 /* Min/max version is TLS/1.0 */
133 # define _HTTP_TLS_1_1 2 /* Min/max version is TLS/1.1 */
134 # define _HTTP_TLS_1_2 3 /* Min/max version is TLS/1.2 */
135 # define _HTTP_TLS_1_3 4 /* Min/max version is TLS/1.3 */
136 # define _HTTP_TLS_MAX 5 /* Highest known TLS version */
137
138
139 /*
140 * Types and functions for SSL support...
141 */
142
143 # ifdef HAVE_GNUTLS
144 /*
145 * The GNU TLS library is more of a "bare metal" SSL/TLS library...
146 */
147
148 typedef gnutls_session_t http_tls_t;
149 typedef gnutls_certificate_credentials_t *http_tls_credentials_t;
150
151 # elif defined(HAVE_CDSASSL)
152 /*
153 * Darwin's Security framework provides its own SSL/TLS context structure
154 * for its IO and protocol management...
155 */
156
157 # if !defined(HAVE_SECBASEPRIV_H) && defined(HAVE_CSSMERRORSTRING) /* Declare prototype for function in that header... */
158 extern const char *cssmErrorString(int error);
159 # endif /* !HAVE_SECBASEPRIV_H && HAVE_CSSMERRORSTRING */
160 # if !defined(HAVE_SECIDENTITYSEARCHPRIV_H) && defined(HAVE_SECIDENTITYSEARCHCREATEWITHPOLICY) /* Declare prototype for function in that header... */
161 extern OSStatus SecIdentitySearchCreateWithPolicy(SecPolicyRef policy,
162 CFStringRef idString, CSSM_KEYUSE keyUsage,
163 CFTypeRef keychainOrArray,
164 Boolean returnOnlyValidIdentities,
165 SecIdentitySearchRef* searchRef);
166 # endif /* !HAVE_SECIDENTITYSEARCHPRIV_H && HAVE_SECIDENTITYSEARCHCREATEWITHPOLICY */
167 # if !defined(HAVE_SECPOLICYPRIV_H) && defined(HAVE_SECPOLICYSETVALUE) /* Declare prototype for function in that header... */
168 extern OSStatus SecPolicySetValue(SecPolicyRef policyRef,
169 const CSSM_DATA *value);
170 # endif /* !HAVE_SECPOLICYPRIV_H && HAVE_SECPOLICYSETVALUE */
171
172 typedef SSLContextRef http_tls_t;
173 typedef CFArrayRef http_tls_credentials_t;
174
175 # elif defined(HAVE_SSPISSL)
176 /*
177 * Windows' SSPI library gets a CUPS wrapper...
178 */
179
180 typedef struct _http_sspi_s /**** SSPI/SSL data structure ****/
181 {
182 CredHandle creds; /* Credentials */
183 CtxtHandle context; /* SSL context */
184 BOOL contextInitialized; /* Is context init'd? */
185 SecPkgContext_StreamSizes streamSizes;/* SSL data stream sizes */
186 BYTE *decryptBuffer; /* Data pre-decryption*/
187 size_t decryptBufferLength; /* Length of decrypt buffer */
188 size_t decryptBufferUsed; /* Bytes used in buffer */
189 BYTE *readBuffer; /* Data post-decryption */
190 int readBufferLength; /* Length of read buffer */
191 int readBufferUsed; /* Bytes used in buffer */
192 BYTE *writeBuffer; /* Data pre-encryption */
193 int writeBufferLength; /* Length of write buffer */
194 PCCERT_CONTEXT localCert, /* Local certificate */
195 remoteCert; /* Remote (peer's) certificate */
196 char error[256]; /* Most recent error message */
197 } _http_sspi_t;
198 typedef _http_sspi_t *http_tls_t;
199 typedef PCCERT_CONTEXT http_tls_credentials_t;
200
201 # else
202 /*
203 * Otherwise define stub types since we have no SSL support...
204 */
205
206 typedef void *http_tls_t;
207 typedef void *http_tls_credentials_t;
208 # endif /* HAVE_GNUTLS */
209
210 typedef enum _http_coding_e /**** HTTP content coding enumeration ****/
211 {
212 _HTTP_CODING_IDENTITY, /* No content coding */
213 _HTTP_CODING_GZIP, /* LZ77+gzip decompression */
214 _HTTP_CODING_DEFLATE, /* LZ77+zlib compression */
215 _HTTP_CODING_GUNZIP, /* LZ77+gzip decompression */
216 _HTTP_CODING_INFLATE /* LZ77+zlib decompression */
217 } _http_coding_t;
218
219 typedef enum _http_mode_e /**** HTTP mode enumeration ****/
220 {
221 _HTTP_MODE_CLIENT, /* Client connected to server */
222 _HTTP_MODE_SERVER /* Server connected (accepted) from client */
223 } _http_mode_t;
224
225 # ifndef _HTTP_NO_PRIVATE
226 struct _http_s /**** HTTP connection structure ****/
227 {
228 int fd; /* File descriptor for this socket */
229 int blocking; /* To block or not to block */
230 int error; /* Last error on read */
231 time_t activity; /* Time since last read/write */
232 http_state_t state; /* State of client */
233 http_status_t status; /* Status of last request */
234 http_version_t version; /* Protocol version */
235 http_keepalive_t keep_alive; /* Keep-alive supported? */
236 struct sockaddr_in _hostaddr; /* Address of connected host (deprecated) */
237 char hostname[HTTP_MAX_HOST],
238 /* Name of connected host */
239 _fields[HTTP_FIELD_ACCEPT_ENCODING][HTTP_MAX_VALUE];
240 /* Field values up to Accept-Encoding (deprecated) */
241 char *data; /* Pointer to data buffer */
242 http_encoding_t data_encoding; /* Chunked or not */
243 int _data_remaining;/* Number of bytes left (deprecated) */
244 int used; /* Number of bytes used in buffer */
245 char buffer[HTTP_MAX_BUFFER];
246 /* Buffer for incoming data */
247 int _auth_type; /* Authentication in use (deprecated) */
248 unsigned char _md5_state[88]; /* MD5 state (deprecated) */
249 char nonce[HTTP_MAX_VALUE];
250 /* Nonce value */
251 unsigned nonce_count; /* Nonce count */
252 http_tls_t tls; /* TLS state information */
253 http_encryption_t encryption; /* Encryption requirements */
254
255 /**** New in CUPS 1.1.19 ****/
256 fd_set *input_set; /* select() set for httpWait() (deprecated) */
257 http_status_t expect; /* Expect: header */
258 char *cookie; /* Cookie value(s) */
259
260 /**** New in CUPS 1.1.20 ****/
261 char _authstring[HTTP_MAX_VALUE],
262 /* Current Authorization value (deprecated) */
263 userpass[HTTP_MAX_VALUE];
264 /* Username:password string */
265 int digest_tries; /* Number of tries for digest auth */
266
267 /**** New in CUPS 1.2 ****/
268 off_t data_remaining; /* Number of bytes left */
269 http_addr_t *hostaddr; /* Current host address and port */
270 http_addrlist_t *addrlist; /* List of valid addresses */
271 char wbuffer[HTTP_MAX_BUFFER];
272 /* Buffer for outgoing data */
273 int wused; /* Write buffer bytes used */
274
275 /**** New in CUPS 1.3 ****/
276 char *authstring; /* Current Authorization field */
277 # ifdef HAVE_GSSAPI
278 gss_OID gssmech; /* Authentication mechanism */
279 gss_ctx_id_t gssctx; /* Authentication context */
280 gss_name_t gssname; /* Authentication server name */
281 # endif /* HAVE_GSSAPI */
282 # ifdef HAVE_AUTHORIZATION_H
283 AuthorizationRef auth_ref; /* Authorization ref */
284 # endif /* HAVE_AUTHORIZATION_H */
285
286 /**** New in CUPS 1.5 ****/
287 http_tls_credentials_t tls_credentials;
288 /* TLS credentials */
289 http_timeout_cb_t timeout_cb; /* Timeout callback */
290 void *timeout_data; /* User data pointer */
291 double timeout_value; /* Timeout in seconds */
292 int wait_value; /* httpWait value for timeout */
293 # ifdef HAVE_GSSAPI
294 char gsshost[256]; /* Hostname for Kerberos */
295 # endif /* HAVE_GSSAPI */
296
297 /**** New in CUPS 1.7 ****/
298 int tls_upgrade; /* Non-zero if we are doing an upgrade */
299 _http_mode_t mode; /* _HTTP_MODE_CLIENT or _HTTP_MODE_SERVER */
300 # ifdef HAVE_LIBZ
301 _http_coding_t coding; /* _HTTP_CODING_xxx */
302 void *stream; /* (De)compression stream */
303 unsigned char *sbuffer; /* (De)compression buffer */
304 # endif /* HAVE_LIBZ */
305
306 /**** New in CUPS 2.2.9 ****/
307 char algorithm[65], /* Algorithm from WWW-Authenticate */
308 nextnonce[HTTP_MAX_VALUE],
309 /* Next nonce value from Authentication-Info */
310 opaque[HTTP_MAX_VALUE],
311 /* Opaque value from WWW-Authenticate */
312 realm[HTTP_MAX_VALUE];
313 /* Realm from WWW-Authenticate */
314
315 /**** New in CUPS 2.3 ****/
316 char *fields[HTTP_FIELD_MAX],
317 /* Allocated field values */
318 *default_fields[HTTP_FIELD_MAX];
319 /* Default field values, if any */
320 };
321 # endif /* !_HTTP_NO_PRIVATE */
322
323
324 /*
325 * Some OS's don't have hstrerror(), most notably Solaris...
326 */
327
328 # ifndef HAVE_HSTRERROR
329 extern const char *_cups_hstrerror(int error);
330 # define hstrerror _cups_hstrerror
331 # endif /* !HAVE_HSTRERROR */
332
333
334 /*
335 * Some OS's don't have getifaddrs() and freeifaddrs()...
336 */
337
338 # if !defined(_WIN32) && !defined(HAVE_GETIFADDRS)
339 # ifdef ifa_dstaddr
340 # undef ifa_dstaddr
341 # endif /* ifa_dstaddr */
342 # ifndef ifr_netmask
343 # define ifr_netmask ifr_addr
344 # endif /* !ifr_netmask */
345
346 struct ifaddrs /**** Interface Structure ****/
347 {
348 struct ifaddrs *ifa_next; /* Next interface in list */
349 char *ifa_name; /* Name of interface */
350 unsigned int ifa_flags; /* Flags (up, point-to-point, etc.) */
351 struct sockaddr *ifa_addr, /* Network address */
352 *ifa_netmask; /* Address mask */
353 union
354 {
355 struct sockaddr *ifu_broadaddr; /* Broadcast address of this interface. */
356 struct sockaddr *ifu_dstaddr; /* Point-to-point destination address. */
357 } ifa_ifu;
358
359 void *ifa_data; /* Interface statistics */
360 };
361
362 # ifndef ifa_broadaddr
363 # define ifa_broadaddr ifa_ifu.ifu_broadaddr
364 # endif /* !ifa_broadaddr */
365 # ifndef ifa_dstaddr
366 # define ifa_dstaddr ifa_ifu.ifu_dstaddr
367 # endif /* !ifa_dstaddr */
368
369 extern int _cups_getifaddrs(struct ifaddrs **addrs) _CUPS_PRIVATE;
370 # define getifaddrs _cups_getifaddrs
371 extern void _cups_freeifaddrs(struct ifaddrs *addrs) _CUPS_PRIVATE;
372 # define freeifaddrs _cups_freeifaddrs
373 # endif /* !_WIN32 && !HAVE_GETIFADDRS */
374
375
376 /*
377 * Prototypes...
378 */
379
380 extern void _httpAddrSetPort(http_addr_t *addr, int port) _CUPS_PRIVATE;
381 extern http_tls_credentials_t
382 _httpCreateCredentials(cups_array_t *credentials) _CUPS_PRIVATE;
383 extern char *_httpDecodeURI(char *dst, const char *src,
384 size_t dstsize) _CUPS_PRIVATE;
385 extern void _httpDisconnect(http_t *http) _CUPS_PRIVATE;
386 extern char *_httpEncodeURI(char *dst, const char *src,
387 size_t dstsize) _CUPS_PRIVATE;
388 extern void _httpFreeCredentials(http_tls_credentials_t credentials) _CUPS_PRIVATE;
389 extern const char *_httpResolveURI(const char *uri, char *resolved_uri,
390 size_t resolved_size, int options,
391 int (*cb)(void *context),
392 void *context) _CUPS_PRIVATE;
393 extern int _httpSetDigestAuthString(http_t *http, const char *nonce, const char *method, const char *resource) _CUPS_PRIVATE;
394 extern const char *_httpStatus(cups_lang_t *lang, http_status_t status) _CUPS_PRIVATE;
395 extern void _httpTLSInitialize(void) _CUPS_PRIVATE;
396 extern size_t _httpTLSPending(http_t *http) _CUPS_PRIVATE;
397 extern int _httpTLSRead(http_t *http, char *buf, int len) _CUPS_PRIVATE;
398 extern void _httpTLSSetOptions(int options, int min_version, int max_version) _CUPS_PRIVATE;
399 extern int _httpTLSStart(http_t *http) _CUPS_PRIVATE;
400 extern void _httpTLSStop(http_t *http) _CUPS_PRIVATE;
401 extern int _httpTLSWrite(http_t *http, const char *buf, int len) _CUPS_PRIVATE;
402 extern int _httpUpdate(http_t *http, http_status_t *status) _CUPS_PRIVATE;
403 extern int _httpWait(http_t *http, int msec, int usessl) _CUPS_PRIVATE;
404
405
406 /*
407 * C++ magic...
408 */
409
410 # ifdef __cplusplus
411 }
412 # endif /* __cplusplus */
413
414 #endif /* !_CUPS_HTTP_PRIVATE_H_ */