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