2 * "$Id: client.c 7950 2008-09-17 00:21:59Z mike $"
4 * Client routines for the Common UNIX Printing System (CUPS) scheduler.
6 * Copyright 2007-2010 by Apple Inc.
7 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
9 * This file contains Kerberos support code, copyright 2006 by
12 * These coded instructions, statements, and computer programs are the
13 * property of Apple Inc. and are protected by Federal copyright
14 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
15 * which should have been included with this file. If this file is
16 * file is missing or damaged, see the license at "http://www.cups.org/".
20 * cupsdAcceptClient() - Accept a new client.
21 * cupsdCloseAllClients() - Close all remote clients immediately.
22 * cupsdCloseClient() - Close a remote client.
23 * cupsdFlushHeader() - Flush the header fields to the client.
24 * cupsdReadClient() - Read data from a client.
25 * cupsdSendCommand() - Send output from a command via HTTP.
26 * cupsdSendError() - Send an error message via HTTP.
27 * cupsdSendHeader() - Send an HTTP request.
28 * cupsdUpdateCGI() - Read status messages from CGI scripts and
30 * cupsdWriteClient() - Write data to a client as needed.
31 * check_if_modified() - Decode an "If-Modified-Since" line.
32 * compare_clients() - Compare two client connections.
33 * data_ready() - Check whether data is available from a client.
34 * encrypt_client() - Enable encryption for the client...
35 * get_cdsa_certificate() - Get a SSL/TLS certificate from the System
37 * get_file() - Get a filename and state info.
38 * install_conf_file() - Install a configuration file.
39 * is_cgi() - Is the resource a CGI script/program?
40 * is_path_absolute() - Is a path absolute and free of relative elements
42 * make_certificate() - Make a self-signed SSL/TLS certificate.
43 * pipe_command() - Pipe the output of a command to the remote client.
44 * valid_host() - Is the Host: field valid?
45 * write_file() - Send a file via HTTP.
46 * write_pipe() - Flag that data is available on the CGI pipe.
50 * Include necessary headers...
56 # include <Security/Security.h>
57 # include <Security/SecItem.h>
58 # ifdef HAVE_SECITEMPRIV_H
59 # include <Security/SecItemPriv.h>
60 # else /* Declare constant from that header... */
61 extern const CFTypeRef kSecClassIdentity
;
62 # endif /* HAVE_SECITEMPRIV_H */
63 # ifdef HAVE_SECIDENTITYSEARCHPRIV_H
64 # include <Security/SecIdentitySearchPriv.h>
65 # else /* Declare prototype for function in that header... */
66 extern OSStatus
SecIdentitySearchCreateWithPolicy(SecPolicyRef policy
,
67 CFStringRef idString
, CSSM_KEYUSE keyUsage
,
68 CFTypeRef keychainOrArray
,
69 Boolean returnOnlyValidIdentities
,
70 SecIdentitySearchRef
* searchRef
);
71 # endif /* HAVE_SECIDENTITYSEARCHPRIV_H */
72 # ifdef HAVE_SECPOLICYPRIV_H
73 # include <Security/SecPolicyPriv.h>
74 # else /* Declare prototype for function in that header... */
75 extern OSStatus
SecPolicySetValue(SecPolicyRef policyRef
,
76 const CSSM_DATA
*value
);
77 # endif /* HAVE_SECPOLICYPRIV_H */
78 # ifdef HAVE_SECBASEPRIV_H
79 # include <Security/SecBasePriv.h>
80 # else /* Declare prototype for function in that header... */
81 extern const char *cssmErrorString(int error
);
82 # endif /* HAVE_SECBASEPRIV_H */
83 #endif /* HAVE_CDSASSL */
86 # include <gnutls/x509.h>
87 #endif /* HAVE_GNUTLS */
91 #endif /* HAVE_TCPD_H */
98 static int check_if_modified(cupsd_client_t
*con
,
99 struct stat
*filestats
);
100 static int compare_clients(cupsd_client_t
*a
, cupsd_client_t
*b
,
102 static int data_ready(cupsd_client_t
*con
);
104 static int encrypt_client(cupsd_client_t
*con
);
105 #endif /* HAVE_SSL */
107 static CFArrayRef
get_cdsa_certificate(cupsd_client_t
*con
);
108 #endif /* HAVE_CDSASSL */
109 static char *get_file(cupsd_client_t
*con
, struct stat
*filestats
,
110 char *filename
, int len
);
111 static http_status_t
install_conf_file(cupsd_client_t
*con
);
112 static int is_cgi(cupsd_client_t
*con
, const char *filename
,
113 struct stat
*filestats
, mime_type_t
*type
);
114 static int is_path_absolute(const char *path
);
116 static int make_certificate(cupsd_client_t
*con
);
117 #endif /* HAVE_SSL */
118 static int pipe_command(cupsd_client_t
*con
, int infile
, int *outfile
,
119 char *command
, char *options
, int root
);
120 static int valid_host(cupsd_client_t
*con
);
121 static int write_file(cupsd_client_t
*con
, http_status_t code
,
122 char *filename
, char *type
,
123 struct stat
*filestats
);
124 static void write_pipe(cupsd_client_t
*con
);
128 * 'cupsdAcceptClient()' - Accept a new client.
132 cupsdAcceptClient(cupsd_listener_t
*lis
)/* I - Listener socket */
134 int count
; /* Count of connections on a host */
135 int val
; /* Parameter value */
136 cupsd_client_t
*con
, /* New client pointer */
137 *tempcon
; /* Temporary client pointer */
138 http_addrlist_t
*addrlist
, /* List of adddresses for host */
139 *addr
; /* Current address */
140 socklen_t addrlen
; /* Length of address */
141 char *hostname
; /* Hostname for address */
142 http_addr_t temp
; /* Temporary address variable */
143 static time_t last_dos
= 0; /* Time of last DoS attack */
145 struct request_info wrap_req
; /* TCP wrappers request information */
146 #endif /* HAVE_TCPD_H */
149 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
150 "cupsdAcceptClient(lis=%p(%d)) Clients=%d",
151 lis
, lis
->fd
, cupsArrayCount(Clients
));
154 * Make sure we don't have a full set of clients already...
157 if (cupsArrayCount(Clients
) == MaxClients
)
161 * Get a pointer to the next available client...
165 Clients
= cupsArrayNew(NULL
, NULL
);
169 cupsdLogMessage(CUPSD_LOG_ERROR
,
170 "Unable to allocate memory for clients array!");
171 cupsdPauseListening();
176 ActiveClients
= cupsArrayNew((cups_array_func_t
)compare_clients
, NULL
);
180 cupsdLogMessage(CUPSD_LOG_ERROR
,
181 "Unable to allocate memory for active clients array!");
182 cupsdPauseListening();
186 if ((con
= calloc(1, sizeof(cupsd_client_t
))) == NULL
)
188 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to allocate memory for client!");
189 cupsdPauseListening();
193 con
->http
.activity
= time(NULL
);
195 con
->http
.hostaddr
= &(con
->clientaddr
);
198 * Accept the client and get the remote address...
201 addrlen
= sizeof(http_addr_t
);
203 if ((con
->http
.fd
= accept(lis
->fd
, (struct sockaddr
*)con
->http
.hostaddr
,
206 if (errno
== ENFILE
|| errno
== EMFILE
)
207 cupsdPauseListening();
209 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to accept client connection - %s.",
217 if (lis
->address
.addr
.sa_family
== AF_INET6
)
220 * Save the connected port number...
223 con
->http
.hostaddr
->ipv6
.sin6_port
= lis
->address
.ipv6
.sin6_port
;
226 * Convert IPv4 over IPv6 addresses (::ffff:n.n.n.n) to IPv4 forms we
227 * can more easily use...
230 if (con
->http
.hostaddr
->ipv6
.sin6_addr
.s6_addr32
[0] == 0 &&
231 con
->http
.hostaddr
->ipv6
.sin6_addr
.s6_addr32
[1] == 0 &&
232 ntohl(con
->http
.hostaddr
->ipv6
.sin6_addr
.s6_addr32
[2]) == 0xffff)
233 con
->http
.hostaddr
->ipv6
.sin6_addr
.s6_addr32
[2] = 0;
236 #endif /* AF_INET6 */
237 if (lis
->address
.addr
.sa_family
== AF_INET
)
238 con
->http
.hostaddr
->ipv4
.sin_port
= lis
->address
.ipv4
.sin_port
;
241 * Check the number of clients on the same address...
244 for (count
= 0, tempcon
= (cupsd_client_t
*)cupsArrayFirst(Clients
);
246 tempcon
= (cupsd_client_t
*)cupsArrayNext(Clients
))
247 if (httpAddrEqual(tempcon
->http
.hostaddr
, con
->http
.hostaddr
))
250 if (count
>= MaxClientsPerHost
)
254 if (count
>= MaxClientsPerHost
)
256 if ((time(NULL
) - last_dos
) >= 60)
258 last_dos
= time(NULL
);
259 cupsdLogMessage(CUPSD_LOG_WARN
,
260 "Possible DoS attack - more than %d clients connecting "
263 httpAddrString(con
->http
.hostaddr
, con
->http
.hostname
,
264 sizeof(con
->http
.hostname
)));
268 closesocket(con
->http
.fd
);
278 * Get the hostname or format the IP address as needed...
281 if (httpAddrLocalhost(con
->http
.hostaddr
))
284 * Map accesses from the loopback interface to "localhost"...
287 strlcpy(con
->http
.hostname
, "localhost", sizeof(con
->http
.hostname
));
288 hostname
= con
->http
.hostname
;
293 * Map accesses from the same host to the server name.
297 hostname
= httpAddrLookup(con
->http
.hostaddr
, con
->http
.hostname
,
298 sizeof(con
->http
.hostname
));
302 httpAddrString(con
->http
.hostaddr
, con
->http
.hostname
,
303 sizeof(con
->http
.hostname
));
307 if (hostname
== NULL
&& HostNameLookups
== 2)
310 * Can't have an unresolved IP address with double-lookups enabled...
314 closesocket(con
->http
.fd
);
319 cupsdLogMessage(CUPSD_LOG_WARN
,
320 "Name lookup failed - connection from %s closed!",
327 if (HostNameLookups
== 2)
330 * Do double lookups as needed...
333 if ((addrlist
= httpAddrGetList(con
->http
.hostname
, AF_UNSPEC
, NULL
))
337 * See if the hostname maps to the same IP address...
340 for (addr
= addrlist
; addr
; addr
= addr
->next
)
341 if (httpAddrEqual(con
->http
.hostaddr
, &(addr
->addr
)))
347 httpAddrFreeList(addrlist
);
352 * Can't have a hostname that doesn't resolve to the same IP address
353 * with double-lookups enabled...
357 closesocket(con
->http
.fd
);
362 cupsdLogMessage(CUPSD_LOG_WARN
,
363 "IP lookup failed - connection from %s closed!",
372 * See if the connection is denied by TCP wrappers...
375 request_init(&wrap_req
, RQ_DAEMON
, "cupsd", RQ_FILE
, con
->http
.fd
, NULL
);
378 if (!hosts_access(&wrap_req
))
381 closesocket(con
->http
.fd
);
386 cupsdLogMessage(CUPSD_LOG_WARN
,
387 "Connection from %s refused by /etc/hosts.allow and "
388 "/etc/hosts.deny rules.", con
->http
.hostname
);
392 #endif /* HAVE_TCPD_H */
395 if (con
->http
.hostaddr
->addr
.sa_family
== AF_INET6
)
396 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdAcceptClient: %d from %s:%d (IPv6)",
397 con
->http
.fd
, con
->http
.hostname
,
398 ntohs(con
->http
.hostaddr
->ipv6
.sin6_port
));
400 #endif /* AF_INET6 */
402 if (con
->http
.hostaddr
->addr
.sa_family
== AF_LOCAL
)
403 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdAcceptClient: %d from %s (Domain)",
404 con
->http
.fd
, con
->http
.hostname
);
406 #endif /* AF_LOCAL */
407 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdAcceptClient: %d from %s:%d (IPv4)",
408 con
->http
.fd
, con
->http
.hostname
,
409 ntohs(con
->http
.hostaddr
->ipv4
.sin_port
));
412 * Get the local address the client connected to...
415 addrlen
= sizeof(temp
);
416 if (getsockname(con
->http
.fd
, (struct sockaddr
*)&temp
, &addrlen
))
418 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to get local address - %s",
421 strcpy(con
->servername
, "localhost");
422 con
->serverport
= LocalPort
;
427 if (temp
.addr
.sa_family
== AF_INET6
)
429 if (httpAddrLocalhost(&temp
))
430 strlcpy(con
->servername
, "localhost", sizeof(con
->servername
));
431 else if (HostNameLookups
|| RemotePort
)
432 httpAddrLookup(&temp
, con
->servername
, sizeof(con
->servername
));
434 httpAddrString(&temp
, con
->servername
, sizeof(con
->servername
));
436 con
->serverport
= ntohs(lis
->address
.ipv6
.sin6_port
);
439 #endif /* AF_INET6 */
440 if (temp
.addr
.sa_family
== AF_INET
)
442 if (httpAddrLocalhost(&temp
))
443 strlcpy(con
->servername
, "localhost", sizeof(con
->servername
));
444 else if (HostNameLookups
|| RemotePort
)
445 httpAddrLookup(&temp
, con
->servername
, sizeof(con
->servername
));
447 httpAddrString(&temp
, con
->servername
, sizeof(con
->servername
));
449 con
->serverport
= ntohs(lis
->address
.ipv4
.sin_port
);
453 strcpy(con
->servername
, "localhost");
454 con
->serverport
= LocalPort
;
458 cupsArrayAdd(Clients
, con
);
461 * Using TCP_NODELAY improves responsiveness, especially on systems with a slow
462 * loopback interface. Since we write large buffers when sending print files
463 * and requests there shouldn't be any performance penalty for this...
467 setsockopt(con
->http
.fd
, IPPROTO_TCP
, TCP_NODELAY
, &val
, sizeof(val
));
470 * Close this file on all execs...
473 fcntl(con
->http
.fd
, F_SETFD
, fcntl(con
->http
.fd
, F_GETFD
) | FD_CLOEXEC
);
476 * Add the socket to the server select.
479 cupsdAddSelect(con
->http
.fd
, (cupsd_selfunc_t
)cupsdReadClient
, NULL
, con
);
482 * Temporarily suspend accept()'s until we lose a client...
485 if (cupsArrayCount(Clients
) == MaxClients
)
486 cupsdPauseListening();
490 * See if we are connecting on a secure port...
493 if (lis
->encryption
== HTTP_ENCRYPT_ALWAYS
)
496 * https connection; go secure...
499 con
->http
.encryption
= HTTP_ENCRYPT_ALWAYS
;
501 if (!encrypt_client(con
))
502 cupsdCloseClient(con
);
506 #endif /* HAVE_SSL */
511 * 'cupsdCloseAllClients()' - Close all remote clients immediately.
515 cupsdCloseAllClients(void)
517 cupsd_client_t
*con
; /* Current client */
520 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdCloseAllClients() Clients=%d",
521 cupsArrayCount(Clients
));
523 for (con
= (cupsd_client_t
*)cupsArrayFirst(Clients
);
525 con
= (cupsd_client_t
*)cupsArrayNext(Clients
))
526 if (cupsdCloseClient(con
))
527 cupsdCloseClient(con
);
532 * 'cupsdCloseClient()' - Close a remote client.
535 int /* O - 1 if partial close, 0 if fully closed */
536 cupsdCloseClient(cupsd_client_t
*con
) /* I - Client to close */
538 int partial
; /* Do partial close for SSL? */
540 SSL_CTX
*context
; /* Context for encryption */
541 SSL
*conn
; /* Connection for encryption */
542 unsigned long error
; /* Error code */
543 #elif defined(HAVE_GNUTLS)
544 http_tls_t
*conn
; /* TLS connection information */
545 int error
; /* Error code */
546 gnutls_certificate_server_credentials
*credentials
;
547 /* TLS credentials */
548 # elif defined(HAVE_CDSASSL)
549 http_tls_t
*conn
; /* CDSA connection information */
550 #endif /* HAVE_LIBSSL */
553 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdCloseClient: %d", con
->http
.fd
);
556 * Flush pending writes before closing...
559 httpFlushWrite(HTTP(con
));
565 * Shutdown encryption as needed...
573 conn
= (SSL
*)(con
->http
.tls
);
574 context
= SSL_get_SSL_CTX(conn
);
576 switch (SSL_shutdown(conn
))
579 cupsdLogMessage(CUPSD_LOG_DEBUG
,
580 "SSL shutdown successful!");
583 cupsdLogMessage(CUPSD_LOG_ERROR
,
584 "Fatal error during SSL shutdown!");
586 while ((error
= ERR_get_error()) != 0)
587 cupsdLogMessage(CUPSD_LOG_ERROR
, "SSL shutdown failed: %s",
588 ERR_error_string(error
, NULL
));
592 SSL_CTX_free(context
);
595 # elif defined(HAVE_GNUTLS)
596 conn
= (http_tls_t
*)(con
->http
.tls
);
597 credentials
= (gnutls_certificate_server_credentials
*)(conn
->credentials
);
599 error
= gnutls_bye(conn
->session
, GNUTLS_SHUT_WR
);
602 case GNUTLS_E_SUCCESS
:
603 cupsdLogMessage(CUPSD_LOG_DEBUG
,
604 "SSL shutdown successful!");
607 cupsdLogMessage(CUPSD_LOG_ERROR
,
608 "SSL shutdown failed: %s", gnutls_strerror(error
));
612 gnutls_deinit(conn
->session
);
613 gnutls_certificate_free_credentials(*credentials
);
617 # elif defined(HAVE_CDSASSL)
618 conn
= (http_tls_t
*)(con
->http
.tls
);
620 while (SSLClose(conn
->session
) == errSSLWouldBlock
)
623 SSLDisposeContext(conn
->session
);
625 if (conn
->certsArray
)
626 CFRelease(conn
->certsArray
);
629 # endif /* HAVE_LIBSSL */
631 con
->http
.tls
= NULL
;
633 #endif /* HAVE_SSL */
635 if (con
->pipe_pid
!= 0)
638 * Stop any CGI process...
641 cupsdEndProcess(con
->pipe_pid
, 1);
647 cupsdRemoveSelect(con
->file
);
654 * Close the socket and clear the file from the input set for select()...
657 if (con
->http
.fd
>= 0)
659 cupsArrayRemove(ActiveClients
, con
);
665 * Only do a partial close so that the encrypted client gets everything.
668 shutdown(con
->http
.fd
, 0);
669 cupsdAddSelect(con
->http
.fd
, (cupsd_selfunc_t
)cupsdReadClient
, NULL
, con
);
674 * Shut the socket down fully...
677 cupsdRemoveSelect(con
->http
.fd
);
689 if (con
->http
.input_set
)
690 free(con
->http
.input_set
);
692 httpClearCookie(HTTP(con
));
693 httpClearFields(HTTP(con
));
695 cupsdClearString(&con
->filename
);
696 cupsdClearString(&con
->command
);
697 cupsdClearString(&con
->options
);
698 cupsdClearString(&con
->query_string
);
702 ippDelete(con
->request
);
708 ippDelete(con
->response
);
709 con
->response
= NULL
;
714 cupsLangFree(con
->language
);
715 con
->language
= NULL
;
718 #ifdef HAVE_AUTHORIZATION_H
721 AuthorizationFree(con
->authref
, kAuthorizationFlagDefaults
);
724 #endif /* HAVE_AUTHORIZATION_H */
727 * Re-enable new client connections if we are going back under the
731 if (cupsArrayCount(Clients
) == MaxClients
)
732 cupsdResumeListening();
735 * Compact the list of clients as necessary...
738 cupsArrayRemove(Clients
, con
);
748 * 'cupsdFlushHeader()' - Flush the header fields to the client.
751 int /* I - Bytes written or -1 on error */
752 cupsdFlushHeader(cupsd_client_t
*con
) /* I - Client to flush to */
754 int bytes
= httpFlushWrite(HTTP(con
));
756 con
->http
.data_encoding
= HTTP_ENCODE_LENGTH
;
763 * 'cupsdReadClient()' - Read data from a client.
767 cupsdReadClient(cupsd_client_t
*con
) /* I - Client to read from */
769 char line
[32768], /* Line from client... */
770 operation
[64], /* Operation code from socket */
771 version
[64], /* HTTP version number string */
772 locale
[64], /* Locale */
773 *ptr
; /* Pointer into strings */
774 int major
, minor
; /* HTTP version numbers */
775 http_status_t status
; /* Transfer status */
776 ipp_state_t ipp_state
; /* State of IPP transfer */
777 int bytes
; /* Number of bytes to POST */
778 char *filename
; /* Name of file for GET/HEAD */
779 char buf
[1024]; /* Buffer for real filename */
780 struct stat filestats
; /* File information */
781 mime_type_t
*type
; /* MIME type of file */
782 cupsd_printer_t
*p
; /* Printer */
783 static unsigned request_id
= 0; /* Request ID for temp files */
786 status
= HTTP_CONTINUE
;
788 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
789 "cupsdReadClient(con=%p(%d)) "
790 "con->http.error=%d "
791 "con->http.used=%d, "
792 "con->http.state=%d "
793 "con->data_encoding=HTTP_ENCODE_%s, "
794 "con->data_remaining=" CUPS_LLFMT
", "
796 con
, con
->http
.fd
, con
->http
.error
, con
->http
.used
,
798 con
->http
.data_encoding
== HTTP_ENCODE_CHUNKED
?
799 "CHUNKED" : "LENGTH",
800 CUPS_LLCAST con
->http
.data_remaining
, con
->file
);
806 * Automatically check for a SSL/TLS handshake...
811 if (recv(con
->http
.fd
, buf
, 1, MSG_PEEK
) == 1 &&
812 (!buf
[0] || !strchr("DGHOPT", buf
[0])))
815 * Encrypt this connection...
818 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
819 "cupsdReadClient: Saw first byte %02X, auto-negotiating "
820 "SSL/TLS session...", buf
[0] & 255);
822 if (!encrypt_client(con
))
823 cupsdCloseClient(con
);
828 #endif /* HAVE_SSL */
830 switch (con
->http
.state
)
834 * See if we've received a request line...
837 if (httpGets(line
, sizeof(line
) - 1, HTTP(con
)) == NULL
)
839 if (con
->http
.error
&& con
->http
.error
!= EPIPE
)
840 cupsdLogMessage(CUPSD_LOG_DEBUG
,
841 "cupsdReadClient: %d WAITING Closing for error %d "
842 "(%s)", con
->http
.fd
, con
->http
.error
,
843 strerror(con
->http
.error
));
845 cupsdLogMessage(CUPSD_LOG_DEBUG
,
846 "cupsdReadClient: %d WAITING Closing on EOF",
849 cupsdCloseClient(con
);
854 * Ignore blank request lines...
861 * Clear other state variables...
864 httpClearFields(HTTP(con
));
866 con
->http
.activity
= time(NULL
);
867 con
->http
.version
= HTTP_1_0
;
868 con
->http
.keep_alive
= HTTP_KEEPALIVE_OFF
;
869 con
->http
.data_encoding
= HTTP_ENCODE_LENGTH
;
870 con
->http
.data_remaining
= 0;
871 con
->http
._data_remaining
= 0;
872 con
->operation
= HTTP_WAITING
;
877 con
->username
[0] = '\0';
878 con
->password
[0] = '\0';
881 cupsdClearString(&con
->command
);
882 cupsdClearString(&con
->options
);
883 cupsdClearString(&con
->query_string
);
887 ippDelete(con
->request
);
893 ippDelete(con
->response
);
894 con
->response
= NULL
;
899 cupsLangFree(con
->language
);
900 con
->language
= NULL
;
908 OM_uint32 minor_status
;
909 gss_release_cred(&minor_status
, &con
->gss_creds
);
911 #endif /* HAVE_GSSAPI */
914 * Grab the request line...
917 switch (sscanf(line
, "%63s%1023s%63s", operation
, con
->uri
, version
))
922 cupsdLogMessage(CUPSD_LOG_ERROR
,
923 "Bad request line \"%s\" from %s!", line
,
925 cupsdSendError(con
, HTTP_BAD_REQUEST
, CUPSD_AUTH_NONE
);
926 cupsdCloseClient(con
);
930 con
->http
.version
= HTTP_0_9
;
933 if (sscanf(version
, "HTTP/%d.%d", &major
, &minor
) != 2)
935 cupsdLogMessage(CUPSD_LOG_ERROR
,
936 "Bad request line \"%s\" from %s!", line
,
938 cupsdSendError(con
, HTTP_BAD_REQUEST
, CUPSD_AUTH_NONE
);
939 cupsdCloseClient(con
);
945 con
->http
.version
= (http_version_t
)(major
* 100 + minor
);
946 if (con
->http
.version
== HTTP_1_1
&& KeepAlive
)
947 con
->http
.keep_alive
= HTTP_KEEPALIVE_ON
;
949 con
->http
.keep_alive
= HTTP_KEEPALIVE_OFF
;
953 cupsdLogMessage(CUPSD_LOG_ERROR
,
954 "Unsupported request line \"%s\" from %s!",
955 line
, con
->http
.hostname
);
956 cupsdSendError(con
, HTTP_NOT_SUPPORTED
, CUPSD_AUTH_NONE
);
957 cupsdCloseClient(con
);
964 * Handle full URLs in the request line...
967 if (strcmp(con
->uri
, "*"))
969 char scheme
[HTTP_MAX_URI
], /* Method/scheme */
970 userpass
[HTTP_MAX_URI
], /* Username:password */
971 hostname
[HTTP_MAX_URI
], /* Hostname */
972 resource
[HTTP_MAX_URI
]; /* Resource path */
973 int port
; /* Port number */
977 * Separate the URI into its components...
980 httpSeparateURI(HTTP_URI_CODING_MOST
, con
->uri
,
981 scheme
, sizeof(scheme
),
982 userpass
, sizeof(userpass
),
983 hostname
, sizeof(hostname
), &port
,
984 resource
, sizeof(resource
));
987 * Only allow URIs with the servername, localhost, or an IP
991 if (strcmp(scheme
, "file") &&
992 strcasecmp(hostname
, ServerName
) &&
993 strcasecmp(hostname
, "localhost") &&
994 !isdigit(hostname
[0]) && hostname
[0] != '[')
997 * Nope, we don't do proxies...
1000 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad URI \"%s\" in request!",
1002 cupsdSendError(con
, HTTP_METHOD_NOT_ALLOWED
, CUPSD_AUTH_NONE
);
1003 cupsdCloseClient(con
);
1008 * Copy the resource portion back into the URI; both resource and
1009 * con->uri are HTTP_MAX_URI bytes in size...
1012 strcpy(con
->uri
, resource
);
1016 * Process the request...
1019 if (!strcmp(operation
, "GET"))
1020 con
->http
.state
= HTTP_GET
;
1021 else if (!strcmp(operation
, "PUT"))
1022 con
->http
.state
= HTTP_PUT
;
1023 else if (!strcmp(operation
, "POST"))
1024 con
->http
.state
= HTTP_POST
;
1025 else if (!strcmp(operation
, "DELETE"))
1026 con
->http
.state
= HTTP_DELETE
;
1027 else if (!strcmp(operation
, "TRACE"))
1028 con
->http
.state
= HTTP_TRACE
;
1029 else if (!strcmp(operation
, "OPTIONS"))
1030 con
->http
.state
= HTTP_OPTIONS
;
1031 else if (!strcmp(operation
, "HEAD"))
1032 con
->http
.state
= HTTP_HEAD
;
1035 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad operation \"%s\"!", operation
);
1036 cupsdSendError(con
, HTTP_BAD_REQUEST
, CUPSD_AUTH_NONE
);
1037 cupsdCloseClient(con
);
1041 gettimeofday(&(con
->start
), NULL
);
1042 con
->operation
= con
->http
.state
;
1044 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdReadClient: %d %s %s HTTP/%d.%d",
1045 con
->http
.fd
, operation
, con
->uri
,
1046 con
->http
.version
/ 100, con
->http
.version
% 100);
1048 con
->http
.status
= HTTP_OK
;
1050 if (!cupsArrayFind(ActiveClients
, con
))
1052 cupsArrayAdd(ActiveClients
, con
);
1053 cupsdSetBusyState();
1064 * Parse incoming parameters until the status changes...
1067 while ((status
= httpUpdate(HTTP(con
))) == HTTP_CONTINUE
)
1068 if (!data_ready(con
))
1071 if (status
!= HTTP_OK
&& status
!= HTTP_CONTINUE
)
1073 if (con
->http
.error
&& con
->http
.error
!= EPIPE
)
1074 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1075 "cupsdReadClient: %d FIELDS Closing for error %d "
1076 "(%s)", con
->http
.fd
, con
->http
.error
,
1077 strerror(con
->http
.error
));
1079 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1080 "cupsdReadClient: %d FIELDS Closing on EOF",
1083 cupsdSendError(con
, HTTP_BAD_REQUEST
, CUPSD_AUTH_NONE
);
1084 cupsdCloseClient(con
);
1090 if (!data_ready(con
) && recv(con
->http
.fd
, buf
, 1, MSG_PEEK
) < 1)
1093 * Connection closed...
1096 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1097 "cupsdReadClient: %d Closing on EOF", con
->http
.fd
);
1098 cupsdCloseClient(con
);
1101 break; /* Anti-compiler-warning-code */
1105 * Handle new transfers...
1108 if (status
== HTTP_OK
)
1110 if (con
->http
.fields
[HTTP_FIELD_ACCEPT_LANGUAGE
][0])
1113 * Figure out the locale from the Accept-Language and Content-Type
1117 if ((ptr
= strchr(con
->http
.fields
[HTTP_FIELD_ACCEPT_LANGUAGE
],
1121 if ((ptr
= strchr(con
->http
.fields
[HTTP_FIELD_ACCEPT_LANGUAGE
],
1125 if ((ptr
= strstr(con
->http
.fields
[HTTP_FIELD_CONTENT_TYPE
],
1126 "charset=")) != NULL
)
1129 * Combine language and charset, and trim any extra params in the
1133 snprintf(locale
, sizeof(locale
), "%s.%s",
1134 con
->http
.fields
[HTTP_FIELD_ACCEPT_LANGUAGE
], ptr
+ 8);
1136 if ((ptr
= strchr(locale
, ',')) != NULL
)
1140 snprintf(locale
, sizeof(locale
), "%s.UTF-8",
1141 con
->http
.fields
[HTTP_FIELD_ACCEPT_LANGUAGE
]);
1143 con
->language
= cupsLangGet(locale
);
1146 con
->language
= cupsLangGet(DefaultLocale
);
1148 cupsdAuthorize(con
);
1150 if (!strncasecmp(con
->http
.fields
[HTTP_FIELD_CONNECTION
], "Keep-Alive",
1152 con
->http
.keep_alive
= HTTP_KEEPALIVE_ON
;
1153 else if (!strncasecmp(con
->http
.fields
[HTTP_FIELD_CONNECTION
], "close", 5))
1154 con
->http
.keep_alive
= HTTP_KEEPALIVE_OFF
;
1156 if (!con
->http
.fields
[HTTP_FIELD_HOST
][0] &&
1157 con
->http
.version
>= HTTP_1_1
)
1160 * HTTP/1.1 and higher require the "Host:" field...
1163 if (!cupsdSendError(con
, HTTP_BAD_REQUEST
, CUPSD_AUTH_NONE
))
1165 cupsdLogMessage(CUPSD_LOG_ERROR
, "Missing Host: field in request!");
1166 cupsdCloseClient(con
);
1170 else if (!valid_host(con
))
1173 * Access to localhost must use "localhost" or the corresponding IPv4
1174 * or IPv6 values in the Host: field.
1177 cupsdLogMessage(CUPSD_LOG_ERROR
,
1178 "Request from \"%s\" using invalid Host: field \"%s\"",
1179 con
->http
.hostname
, con
->http
.fields
[HTTP_FIELD_HOST
]);
1181 if (!cupsdSendError(con
, HTTP_BAD_REQUEST
, CUPSD_AUTH_NONE
))
1183 cupsdCloseClient(con
);
1187 else if (con
->operation
== HTTP_OPTIONS
)
1190 * Do OPTIONS command...
1193 if (con
->best
&& con
->best
->type
!= CUPSD_AUTH_NONE
)
1195 if (!cupsdSendHeader(con
, HTTP_UNAUTHORIZED
, NULL
, CUPSD_AUTH_NONE
))
1197 cupsdCloseClient(con
);
1202 if (!strcasecmp(con
->http
.fields
[HTTP_FIELD_CONNECTION
], "Upgrade") &&
1203 con
->http
.tls
== NULL
)
1207 * Do encryption stuff...
1210 if (!cupsdSendHeader(con
, HTTP_SWITCHING_PROTOCOLS
, NULL
, CUPSD_AUTH_NONE
))
1212 cupsdCloseClient(con
);
1216 httpPrintf(HTTP(con
), "Connection: Upgrade\r\n");
1217 httpPrintf(HTTP(con
), "Upgrade: TLS/1.0,HTTP/1.1\r\n");
1218 httpPrintf(HTTP(con
), "Content-Length: 0\r\n");
1219 httpPrintf(HTTP(con
), "\r\n");
1221 if (cupsdFlushHeader(con
) < 0)
1223 cupsdCloseClient(con
);
1227 if (!encrypt_client(con
))
1229 cupsdCloseClient(con
);
1233 if (!cupsdSendError(con
, HTTP_NOT_IMPLEMENTED
, CUPSD_AUTH_NONE
))
1235 cupsdCloseClient(con
);
1238 #endif /* HAVE_SSL */
1241 if (!cupsdSendHeader(con
, HTTP_OK
, NULL
, CUPSD_AUTH_NONE
))
1243 cupsdCloseClient(con
);
1247 httpPrintf(HTTP(con
), "Allow: GET, HEAD, OPTIONS, POST, PUT\r\n");
1248 httpPrintf(HTTP(con
), "Content-Length: 0\r\n");
1249 httpPrintf(HTTP(con
), "\r\n");
1251 if (cupsdFlushHeader(con
) < 0)
1253 cupsdCloseClient(con
);
1257 else if (!is_path_absolute(con
->uri
))
1260 * Protect against malicious users!
1263 cupsdLogMessage(CUPSD_LOG_ERROR
,
1264 "Request for non-absolute resource \"%s\"!", con
->uri
);
1266 if (!cupsdSendError(con
, HTTP_FORBIDDEN
, CUPSD_AUTH_NONE
))
1268 cupsdCloseClient(con
);
1274 if (!strcasecmp(con
->http
.fields
[HTTP_FIELD_CONNECTION
], "Upgrade") &&
1275 con
->http
.tls
== NULL
)
1279 * Do encryption stuff...
1282 if (!cupsdSendHeader(con
, HTTP_SWITCHING_PROTOCOLS
, NULL
, CUPSD_AUTH_NONE
))
1284 cupsdCloseClient(con
);
1288 httpPrintf(HTTP(con
), "Connection: Upgrade\r\n");
1289 httpPrintf(HTTP(con
), "Upgrade: TLS/1.0,HTTP/1.1\r\n");
1290 httpPrintf(HTTP(con
), "Content-Length: 0\r\n");
1291 httpPrintf(HTTP(con
), "\r\n");
1293 if (cupsdFlushHeader(con
) < 0)
1295 cupsdCloseClient(con
);
1299 if (!encrypt_client(con
))
1301 cupsdCloseClient(con
);
1305 if (!cupsdSendError(con
, HTTP_NOT_IMPLEMENTED
, CUPSD_AUTH_NONE
))
1307 cupsdCloseClient(con
);
1310 #endif /* HAVE_SSL */
1313 if ((status
= cupsdIsAuthorized(con
, NULL
)) != HTTP_OK
)
1315 cupsdSendError(con
, status
, CUPSD_AUTH_NONE
);
1316 cupsdCloseClient(con
);
1320 if (con
->http
.expect
&&
1321 (con
->operation
== HTTP_POST
|| con
->operation
== HTTP_PUT
))
1323 if (con
->http
.expect
== HTTP_CONTINUE
)
1326 * Send 100-continue header...
1329 if (!cupsdSendHeader(con
, HTTP_CONTINUE
, NULL
, CUPSD_AUTH_NONE
))
1331 cupsdCloseClient(con
);
1338 * Send 417-expectation-failed header...
1341 if (!cupsdSendHeader(con
, HTTP_EXPECTATION_FAILED
, NULL
,
1344 cupsdCloseClient(con
);
1348 httpPrintf(HTTP(con
), "Content-Length: 0\r\n");
1349 httpPrintf(HTTP(con
), "\r\n");
1351 if (cupsdFlushHeader(con
) < 0)
1353 cupsdCloseClient(con
);
1359 switch (con
->http
.state
)
1361 case HTTP_GET_SEND
:
1362 if (!strncmp(con
->uri
, "/printers/", 10) &&
1363 !strcmp(con
->uri
+ strlen(con
->uri
) - 4, ".ppd"))
1366 * Send PPD file - get the real printer name since printer
1367 * names are not case sensitive but filenames can be...
1370 con
->uri
[strlen(con
->uri
) - 4] = '\0'; /* Drop ".ppd" */
1372 if ((p
= cupsdFindPrinter(con
->uri
+ 10)) != NULL
)
1373 snprintf(con
->uri
, sizeof(con
->uri
), "/ppd/%s.ppd", p
->name
);
1376 if (!cupsdSendError(con
, HTTP_NOT_FOUND
, CUPSD_AUTH_NONE
))
1378 cupsdCloseClient(con
);
1386 if ((!strncmp(con
->uri
, "/admin", 6) &&
1387 strncmp(con
->uri
, "/admin/conf/", 12) &&
1388 strncmp(con
->uri
, "/admin/log/", 11)) ||
1389 !strncmp(con
->uri
, "/printers", 9) ||
1390 !strncmp(con
->uri
, "/classes", 8) ||
1391 !strncmp(con
->uri
, "/help", 5) ||
1392 !strncmp(con
->uri
, "/jobs", 5))
1395 * Send CGI output...
1398 if (!strncmp(con
->uri
, "/admin", 6))
1400 cupsdSetStringf(&con
->command
, "%s/cgi-bin/admin.cgi",
1403 cupsdSetString(&con
->options
, strchr(con
->uri
+ 6, '?'));
1405 else if (!strncmp(con
->uri
, "/printers", 9))
1407 cupsdSetStringf(&con
->command
, "%s/cgi-bin/printers.cgi",
1410 if (con
->uri
[9] && con
->uri
[10])
1411 cupsdSetString(&con
->options
, con
->uri
+ 9);
1413 cupsdSetString(&con
->options
, NULL
);
1415 else if (!strncmp(con
->uri
, "/classes", 8))
1417 cupsdSetStringf(&con
->command
, "%s/cgi-bin/classes.cgi",
1420 if (con
->uri
[8] && con
->uri
[9])
1421 cupsdSetString(&con
->options
, con
->uri
+ 8);
1423 cupsdSetString(&con
->options
, NULL
);
1425 else if (!strncmp(con
->uri
, "/jobs", 5))
1427 cupsdSetStringf(&con
->command
, "%s/cgi-bin/jobs.cgi",
1430 if (con
->uri
[5] && con
->uri
[6])
1431 cupsdSetString(&con
->options
, con
->uri
+ 5);
1433 cupsdSetString(&con
->options
, NULL
);
1437 cupsdSetStringf(&con
->command
, "%s/cgi-bin/help.cgi",
1440 if (con
->uri
[5] && con
->uri
[6])
1441 cupsdSetString(&con
->options
, con
->uri
+ 5);
1443 cupsdSetString(&con
->options
, NULL
);
1446 if (!cupsdSendCommand(con
, con
->command
, con
->options
, 0))
1448 if (!cupsdSendError(con
, HTTP_NOT_FOUND
, CUPSD_AUTH_NONE
))
1450 cupsdCloseClient(con
);
1455 cupsdLogRequest(con
, HTTP_OK
);
1457 if (con
->http
.version
<= HTTP_1_0
)
1458 con
->http
.keep_alive
= HTTP_KEEPALIVE_OFF
;
1460 else if ((!strncmp(con
->uri
, "/admin/conf/", 12) &&
1461 (strchr(con
->uri
+ 12, '/') ||
1462 strlen(con
->uri
) == 12)) ||
1463 (!strncmp(con
->uri
, "/admin/log/", 11) &&
1464 (strchr(con
->uri
+ 11, '/') ||
1465 strlen(con
->uri
) == 11)))
1468 * GET can only be done to configuration files directly under
1472 cupsdLogMessage(CUPSD_LOG_ERROR
,
1473 "Request for subdirectory \"%s\"!", con
->uri
);
1475 if (!cupsdSendError(con
, HTTP_FORBIDDEN
, CUPSD_AUTH_NONE
))
1477 cupsdCloseClient(con
);
1489 if ((filename
= get_file(con
, &filestats
, buf
,
1490 sizeof(buf
))) == NULL
)
1492 if (!cupsdSendError(con
, HTTP_NOT_FOUND
, CUPSD_AUTH_NONE
))
1494 cupsdCloseClient(con
);
1501 type
= mimeFileType(MimeDatabase
, filename
, NULL
, NULL
);
1503 if (is_cgi(con
, filename
, &filestats
, type
))
1506 * Note: con->command and con->options were set by
1510 if (!cupsdSendCommand(con
, con
->command
, con
->options
, 0))
1512 if (!cupsdSendError(con
, HTTP_NOT_FOUND
, CUPSD_AUTH_NONE
))
1514 cupsdCloseClient(con
);
1519 cupsdLogRequest(con
, HTTP_OK
);
1521 if (con
->http
.version
<= HTTP_1_0
)
1522 con
->http
.keep_alive
= HTTP_KEEPALIVE_OFF
;
1526 if (!check_if_modified(con
, &filestats
))
1528 if (!cupsdSendError(con
, HTTP_NOT_MODIFIED
, CUPSD_AUTH_NONE
))
1530 cupsdCloseClient(con
);
1537 strcpy(line
, "text/plain");
1539 snprintf(line
, sizeof(line
), "%s/%s", type
->super
, type
->type
);
1541 if (!write_file(con
, HTTP_OK
, filename
, line
, &filestats
))
1543 cupsdCloseClient(con
);
1550 case HTTP_POST_RECV
:
1552 * See if the POST request includes a Content-Length field, and if
1553 * so check the length against any limits that are set...
1556 if (con
->http
.fields
[HTTP_FIELD_CONTENT_LENGTH
][0] &&
1557 MaxRequestSize
> 0 &&
1558 con
->http
.data_remaining
> MaxRequestSize
)
1561 * Request too large...
1564 if (!cupsdSendError(con
, HTTP_REQUEST_TOO_LARGE
, CUPSD_AUTH_NONE
))
1566 cupsdCloseClient(con
);
1572 else if (con
->http
.data_remaining
< 0 ||
1573 (!con
->http
.fields
[HTTP_FIELD_CONTENT_LENGTH
][0] &&
1574 con
->http
.data_encoding
== HTTP_ENCODE_LENGTH
))
1577 * Negative content lengths are invalid!
1580 if (!cupsdSendError(con
, HTTP_BAD_REQUEST
, CUPSD_AUTH_NONE
))
1582 cupsdCloseClient(con
);
1590 * See what kind of POST request this is; for IPP requests the
1591 * content-type field will be "application/ipp"...
1594 if (!strcmp(con
->http
.fields
[HTTP_FIELD_CONTENT_TYPE
],
1596 con
->request
= ippNew();
1597 else if ((!strncmp(con
->uri
, "/admin", 6) &&
1598 strncmp(con
->uri
, "/admin/conf/", 12) &&
1599 strncmp(con
->uri
, "/admin/log/", 11)) ||
1600 !strncmp(con
->uri
, "/printers", 9) ||
1601 !strncmp(con
->uri
, "/classes", 8) ||
1602 !strncmp(con
->uri
, "/help", 5) ||
1603 !strncmp(con
->uri
, "/jobs", 5))
1609 if (!strncmp(con
->uri
, "/admin", 6))
1611 cupsdSetStringf(&con
->command
, "%s/cgi-bin/admin.cgi",
1614 cupsdSetString(&con
->options
, strchr(con
->uri
+ 6, '?'));
1616 else if (!strncmp(con
->uri
, "/printers", 9))
1618 cupsdSetStringf(&con
->command
, "%s/cgi-bin/printers.cgi",
1621 if (con
->uri
[9] && con
->uri
[10])
1622 cupsdSetString(&con
->options
, con
->uri
+ 9);
1624 cupsdSetString(&con
->options
, NULL
);
1626 else if (!strncmp(con
->uri
, "/classes", 8))
1628 cupsdSetStringf(&con
->command
, "%s/cgi-bin/classes.cgi",
1631 if (con
->uri
[8] && con
->uri
[9])
1632 cupsdSetString(&con
->options
, con
->uri
+ 8);
1634 cupsdSetString(&con
->options
, NULL
);
1636 else if (!strncmp(con
->uri
, "/jobs", 5))
1638 cupsdSetStringf(&con
->command
, "%s/cgi-bin/jobs.cgi",
1641 if (con
->uri
[5] && con
->uri
[6])
1642 cupsdSetString(&con
->options
, con
->uri
+ 5);
1644 cupsdSetString(&con
->options
, NULL
);
1648 cupsdSetStringf(&con
->command
, "%s/cgi-bin/help.cgi",
1651 if (con
->uri
[5] && con
->uri
[6])
1652 cupsdSetString(&con
->options
, con
->uri
+ 5);
1654 cupsdSetString(&con
->options
, NULL
);
1657 if (con
->http
.version
<= HTTP_1_0
)
1658 con
->http
.keep_alive
= HTTP_KEEPALIVE_OFF
;
1666 if ((filename
= get_file(con
, &filestats
, buf
,
1667 sizeof(buf
))) == NULL
)
1669 if (!cupsdSendError(con
, HTTP_NOT_FOUND
, CUPSD_AUTH_NONE
))
1671 cupsdCloseClient(con
);
1678 type
= mimeFileType(MimeDatabase
, filename
, NULL
, NULL
);
1680 if (!is_cgi(con
, filename
, &filestats
, type
))
1683 * Only POST to CGI's...
1686 if (!cupsdSendError(con
, HTTP_UNAUTHORIZED
, CUPSD_AUTH_NONE
))
1688 cupsdCloseClient(con
);
1695 case HTTP_PUT_RECV
:
1697 * Validate the resource name...
1700 if (strncmp(con
->uri
, "/admin/conf/", 12) ||
1701 strchr(con
->uri
+ 12, '/') ||
1702 strlen(con
->uri
) == 12)
1705 * PUT can only be done to configuration files under
1709 cupsdLogMessage(CUPSD_LOG_ERROR
,
1710 "Request for subdirectory \"%s\"!", con
->uri
);
1712 if (!cupsdSendError(con
, HTTP_FORBIDDEN
, CUPSD_AUTH_NONE
))
1714 cupsdCloseClient(con
);
1722 * See if the PUT request includes a Content-Length field, and if
1723 * so check the length against any limits that are set...
1726 if (con
->http
.fields
[HTTP_FIELD_CONTENT_LENGTH
][0] &&
1727 MaxRequestSize
> 0 &&
1728 con
->http
.data_remaining
> MaxRequestSize
)
1731 * Request too large...
1734 if (!cupsdSendError(con
, HTTP_REQUEST_TOO_LARGE
, CUPSD_AUTH_NONE
))
1736 cupsdCloseClient(con
);
1742 else if (con
->http
.data_remaining
< 0)
1745 * Negative content lengths are invalid!
1748 if (!cupsdSendError(con
, HTTP_BAD_REQUEST
, CUPSD_AUTH_NONE
))
1750 cupsdCloseClient(con
);
1758 * Open a temporary file to hold the request...
1761 cupsdSetStringf(&con
->filename
, "%s/%08x", RequestRoot
,
1763 con
->file
= open(con
->filename
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0640);
1767 cupsdLogMessage(CUPSD_LOG_ERROR
,
1768 "Unable to create request file %s: %s",
1769 con
->filename
, strerror(errno
));
1771 if (!cupsdSendError(con
, HTTP_REQUEST_TOO_LARGE
, CUPSD_AUTH_NONE
))
1773 cupsdCloseClient(con
);
1778 fchmod(con
->file
, 0640);
1779 fchown(con
->file
, RunUser
, Group
);
1780 fcntl(con
->file
, F_SETFD
, fcntl(con
->file
, F_GETFD
) | FD_CLOEXEC
);
1785 cupsdSendError(con
, HTTP_NOT_IMPLEMENTED
, CUPSD_AUTH_NONE
);
1786 cupsdCloseClient(con
);
1790 if (!strncmp(con
->uri
, "/printers/", 10) &&
1791 !strcmp(con
->uri
+ strlen(con
->uri
) - 4, ".ppd"))
1794 * Send PPD file - get the real printer name since printer
1795 * names are not case sensitive but filenames can be...
1798 con
->uri
[strlen(con
->uri
) - 4] = '\0'; /* Drop ".ppd" */
1800 if ((p
= cupsdFindPrinter(con
->uri
+ 10)) != NULL
)
1801 snprintf(con
->uri
, sizeof(con
->uri
), "/ppd/%s.ppd", p
->name
);
1804 if (!cupsdSendError(con
, HTTP_NOT_FOUND
, CUPSD_AUTH_NONE
))
1806 cupsdCloseClient(con
);
1814 if ((!strncmp(con
->uri
, "/admin", 6) &&
1815 strncmp(con
->uri
, "/admin/conf/", 12) &&
1816 strncmp(con
->uri
, "/admin/log/", 11)) ||
1817 !strncmp(con
->uri
, "/printers", 9) ||
1818 !strncmp(con
->uri
, "/classes", 8) ||
1819 !strncmp(con
->uri
, "/help", 5) ||
1820 !strncmp(con
->uri
, "/jobs", 5))
1826 if (!cupsdSendHeader(con
, HTTP_OK
, "text/html", CUPSD_AUTH_NONE
))
1828 cupsdCloseClient(con
);
1832 if (httpPrintf(HTTP(con
), "\r\n") < 0)
1834 cupsdCloseClient(con
);
1838 if (cupsdFlushHeader(con
) < 0)
1840 cupsdCloseClient(con
);
1844 cupsdLogRequest(con
, HTTP_OK
);
1846 else if ((!strncmp(con
->uri
, "/admin/conf/", 12) &&
1847 (strchr(con
->uri
+ 12, '/') ||
1848 strlen(con
->uri
) == 12)) ||
1849 (!strncmp(con
->uri
, "/admin/log/", 11) &&
1850 (strchr(con
->uri
+ 11, '/') ||
1851 strlen(con
->uri
) == 11)))
1854 * HEAD can only be done to configuration files under
1858 cupsdLogMessage(CUPSD_LOG_ERROR
,
1859 "Request for subdirectory \"%s\"!", con
->uri
);
1861 if (!cupsdSendError(con
, HTTP_FORBIDDEN
, CUPSD_AUTH_NONE
))
1863 cupsdCloseClient(con
);
1869 else if ((filename
= get_file(con
, &filestats
, buf
,
1870 sizeof(buf
))) == NULL
)
1872 if (!cupsdSendHeader(con
, HTTP_NOT_FOUND
, "text/html",
1875 cupsdCloseClient(con
);
1879 cupsdLogRequest(con
, HTTP_NOT_FOUND
);
1881 else if (!check_if_modified(con
, &filestats
))
1883 if (!cupsdSendError(con
, HTTP_NOT_MODIFIED
, CUPSD_AUTH_NONE
))
1885 cupsdCloseClient(con
);
1889 cupsdLogRequest(con
, HTTP_NOT_MODIFIED
);
1897 type
= mimeFileType(MimeDatabase
, filename
, NULL
, NULL
);
1899 strcpy(line
, "text/plain");
1901 snprintf(line
, sizeof(line
), "%s/%s", type
->super
, type
->type
);
1903 if (!cupsdSendHeader(con
, HTTP_OK
, line
, CUPSD_AUTH_NONE
))
1905 cupsdCloseClient(con
);
1909 if (httpPrintf(HTTP(con
), "Last-Modified: %s\r\n",
1910 httpGetDateString(filestats
.st_mtime
)) < 0)
1912 cupsdCloseClient(con
);
1916 if (httpPrintf(HTTP(con
), "Content-Length: %lu\r\n",
1917 (unsigned long)filestats
.st_size
) < 0)
1919 cupsdCloseClient(con
);
1923 cupsdLogRequest(con
, HTTP_OK
);
1926 if (httpPrintf(HTTP(con
), "\r\n") < 0)
1928 cupsdCloseClient(con
);
1932 if (cupsdFlushHeader(con
) < 0)
1934 cupsdCloseClient(con
);
1938 con
->http
.state
= HTTP_WAITING
;
1942 break; /* Anti-compiler-warning-code */
1948 * Handle any incoming data...
1951 switch (con
->http
.state
)
1953 case HTTP_PUT_RECV
:
1956 if ((bytes
= httpRead2(HTTP(con
), line
, sizeof(line
))) < 0)
1958 if (con
->http
.error
&& con
->http
.error
!= EPIPE
)
1959 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1960 "cupsdReadClient: %d PUT_RECV Closing for error "
1961 "%d (%s)", con
->http
.fd
, con
->http
.error
,
1962 strerror(con
->http
.error
));
1964 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1965 "cupsdReadClient: %d PUT_RECV Closing on EOF",
1968 cupsdCloseClient(con
);
1973 con
->bytes
+= bytes
;
1975 if (write(con
->file
, line
, bytes
) < bytes
)
1977 cupsdLogMessage(CUPSD_LOG_ERROR
,
1978 "cupsdReadClient: Unable to write %d bytes to %s: %s",
1979 bytes
, con
->filename
, strerror(errno
));
1983 unlink(con
->filename
);
1984 cupsdClearString(&con
->filename
);
1986 if (!cupsdSendError(con
, HTTP_REQUEST_TOO_LARGE
, CUPSD_AUTH_NONE
))
1988 cupsdCloseClient(con
);
1994 while (con
->http
.state
== HTTP_PUT_RECV
&& data_ready(con
));
1996 if (con
->http
.state
== HTTP_WAITING
)
1999 * End of file, see how big it is...
2002 fstat(con
->file
, &filestats
);
2007 if (filestats
.st_size
> MaxRequestSize
&&
2011 * Request is too big; remove it and send an error...
2014 unlink(con
->filename
);
2015 cupsdClearString(&con
->filename
);
2017 if (!cupsdSendError(con
, HTTP_REQUEST_TOO_LARGE
, CUPSD_AUTH_NONE
))
2019 cupsdCloseClient(con
);
2025 * Install the configuration file...
2028 status
= install_conf_file(con
);
2031 * Return the status to the client...
2034 if (!cupsdSendError(con
, status
, CUPSD_AUTH_NONE
))
2036 cupsdCloseClient(con
);
2042 case HTTP_POST_RECV
:
2045 if (con
->request
&& con
->file
< 0)
2048 * Grab any request data from the connection...
2051 if ((ipp_state
= ippRead(&(con
->http
), con
->request
)) == IPP_ERROR
)
2053 cupsdLogMessage(CUPSD_LOG_ERROR
,
2054 "cupsdReadClient: %d IPP Read Error!",
2057 cupsdSendError(con
, HTTP_BAD_REQUEST
, CUPSD_AUTH_NONE
);
2058 cupsdCloseClient(con
);
2061 else if (ipp_state
!= IPP_DATA
)
2063 if (con
->http
.state
== HTTP_POST_SEND
)
2065 cupsdSendError(con
, HTTP_BAD_REQUEST
, CUPSD_AUTH_NONE
);
2066 cupsdCloseClient(con
);
2074 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdReadClient: %d %d.%d %s %d",
2075 con
->http
.fd
, con
->request
->request
.op
.version
[0],
2076 con
->request
->request
.op
.version
[1],
2077 ippOpString(con
->request
->request
.op
.operation_id
),
2078 con
->request
->request
.op
.request_id
);
2079 con
->bytes
+= ippLength(con
->request
);
2083 if (con
->file
< 0 && con
->http
.state
!= HTTP_POST_SEND
)
2086 * Create a file as needed for the request data...
2089 cupsdSetStringf(&con
->filename
, "%s/%08x", RequestRoot
,
2091 con
->file
= open(con
->filename
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0640);
2095 cupsdLogMessage(CUPSD_LOG_ERROR
,
2096 "Unable to create request file %s: %s",
2097 con
->filename
, strerror(errno
));
2099 if (!cupsdSendError(con
, HTTP_REQUEST_TOO_LARGE
, CUPSD_AUTH_NONE
))
2101 cupsdCloseClient(con
);
2106 fchmod(con
->file
, 0640);
2107 fchown(con
->file
, RunUser
, Group
);
2108 fcntl(con
->file
, F_SETFD
, fcntl(con
->file
, F_GETFD
) | FD_CLOEXEC
);
2111 if (con
->http
.state
!= HTTP_POST_SEND
)
2113 if ((bytes
= httpRead2(HTTP(con
), line
, sizeof(line
))) < 0)
2115 if (con
->http
.error
&& con
->http
.error
!= EPIPE
)
2116 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2117 "cupsdReadClient: %d POST_SEND Closing for "
2118 "error %d (%s)", con
->http
.fd
, con
->http
.error
,
2119 strerror(con
->http
.error
));
2121 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2122 "cupsdReadClient: %d POST_SEND Closing on EOF",
2125 cupsdCloseClient(con
);
2130 con
->bytes
+= bytes
;
2132 if (write(con
->file
, line
, bytes
) < bytes
)
2134 cupsdLogMessage(CUPSD_LOG_ERROR
,
2135 "cupsdReadClient: Unable to write %d bytes to "
2136 "%s: %s", bytes
, con
->filename
,
2141 unlink(con
->filename
);
2142 cupsdClearString(&con
->filename
);
2144 if (!cupsdSendError(con
, HTTP_REQUEST_TOO_LARGE
,
2147 cupsdCloseClient(con
);
2152 else if (con
->http
.state
== HTTP_POST_RECV
)
2154 else if (con
->http
.state
!= HTTP_POST_SEND
)
2156 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2157 "cupsdReadClient: %d Closing on unknown HTTP "
2158 "state %d", con
->http
.fd
, con
->http
.state
);
2159 cupsdCloseClient(con
);
2164 while (con
->http
.state
== HTTP_POST_RECV
&& data_ready(con
));
2166 if (con
->http
.state
== HTTP_POST_SEND
)
2170 fstat(con
->file
, &filestats
);
2175 if (filestats
.st_size
> MaxRequestSize
&&
2179 * Request is too big; remove it and send an error...
2182 unlink(con
->filename
);
2183 cupsdClearString(&con
->filename
);
2188 * Delete any IPP request data...
2191 ippDelete(con
->request
);
2192 con
->request
= NULL
;
2195 if (!cupsdSendError(con
, HTTP_REQUEST_TOO_LARGE
, CUPSD_AUTH_NONE
))
2197 cupsdCloseClient(con
);
2201 else if (filestats
.st_size
== 0)
2204 * Don't allow empty file...
2207 unlink(con
->filename
);
2208 cupsdClearString(&con
->filename
);
2213 if (!cupsdSendCommand(con
, con
->command
, con
->options
, 0))
2215 if (!cupsdSendError(con
, HTTP_NOT_FOUND
, CUPSD_AUTH_NONE
))
2217 cupsdCloseClient(con
);
2222 cupsdLogRequest(con
, HTTP_OK
);
2228 cupsdProcessIPPRequest(con
);
2232 unlink(con
->filename
);
2233 cupsdClearString(&con
->filename
);
2242 break; /* Anti-compiler-warning-code */
2245 if (con
->http
.state
== HTTP_WAITING
)
2247 if (!con
->http
.keep_alive
)
2249 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2250 "cupsdReadClient: %d Closing because Keep-Alive disabled",
2252 cupsdCloseClient(con
);
2256 cupsArrayRemove(ActiveClients
, con
);
2257 cupsdSetBusyState();
2264 * 'cupsdSendCommand()' - Send output from a command via HTTP.
2267 int /* O - 1 on success, 0 on failure */
2269 cupsd_client_t
*con
, /* I - Client connection */
2270 char *command
, /* I - Command to run */
2271 char *options
, /* I - Command-line options */
2272 int root
) /* I - Run as root? */
2274 int fd
; /* Standard input file descriptor */
2279 fd
= open(con
->filename
, O_RDONLY
);
2283 cupsdLogMessage(CUPSD_LOG_ERROR
,
2284 "cupsdSendCommand: %d Unable to open \"%s\" for reading: %s",
2285 con
->http
.fd
, con
->filename
? con
->filename
: "/dev/null",
2290 fcntl(fd
, F_SETFD
, fcntl(fd
, F_GETFD
) | FD_CLOEXEC
);
2295 con
->pipe_pid
= pipe_command(con
, fd
, &(con
->file
), command
, options
, root
);
2300 cupsdLogMessage(CUPSD_LOG_INFO
, "Started \"%s\" (pid=%d)", command
,
2303 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdSendCommand: %d file=%d",
2304 con
->http
.fd
, con
->file
);
2306 if (con
->pipe_pid
== 0)
2309 fcntl(con
->file
, F_SETFD
, fcntl(con
->file
, F_GETFD
) | FD_CLOEXEC
);
2311 cupsdAddSelect(con
->file
, (cupsd_selfunc_t
)write_pipe
, NULL
, con
);
2313 con
->sent_header
= 0;
2314 con
->file_ready
= 0;
2315 con
->got_fields
= 0;
2316 con
->header_used
= 0;
2323 * 'cupsdSendError()' - Send an error message via HTTP.
2326 int /* O - 1 if successful, 0 otherwise */
2327 cupsdSendError(cupsd_client_t
*con
, /* I - Connection */
2328 http_status_t code
, /* I - Error code */
2329 int auth_type
)/* I - Authentication type */
2331 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2332 "cupsdSendError(con=%p(%d), code=%d, auth_type=%d", con
,
2333 con
->http
.fd
, code
, auth_type
);
2337 * Force client to upgrade for authentication if that is how the
2338 * server is configured...
2341 if (code
== HTTP_UNAUTHORIZED
&&
2342 DefaultEncryption
== HTTP_ENCRYPT_REQUIRED
&&
2343 strcasecmp(con
->http
.hostname
, "localhost") &&
2346 code
= HTTP_UPGRADE_REQUIRED
;
2348 #endif /* HAVE_SSL */
2351 * Put the request in the access_log file...
2354 cupsdLogRequest(con
, code
);
2357 * To work around bugs in some proxies, don't use Keep-Alive for some
2360 * Kerberos authentication doesn't work without Keep-Alive, so
2361 * never disable it in that case.
2364 if (code
>= HTTP_BAD_REQUEST
&& con
->http
.auth_type
!= CUPSD_AUTH_NEGOTIATE
)
2365 con
->http
.keep_alive
= HTTP_KEEPALIVE_OFF
;
2368 * Send an error message back to the client. If the error code is a
2369 * 400 or 500 series, make sure the message contains some text, too!
2372 if (!cupsdSendHeader(con
, code
, NULL
, auth_type
))
2376 if (code
== HTTP_UPGRADE_REQUIRED
)
2377 if (httpPrintf(HTTP(con
), "Connection: Upgrade\r\n") < 0)
2380 if (httpPrintf(HTTP(con
), "Upgrade: TLS/1.0,HTTP/1.1\r\n") < 0)
2382 #endif /* HAVE_SSL */
2384 if (con
->http
.version
>= HTTP_1_1
&&
2385 con
->http
.keep_alive
== HTTP_KEEPALIVE_OFF
)
2387 if (httpPrintf(HTTP(con
), "Connection: close\r\n") < 0)
2391 if (code
>= HTTP_BAD_REQUEST
)
2394 * Send a human-readable error message.
2397 char message
[4096], /* Message for user */
2398 urltext
[1024], /* URL redirection text */
2399 redirect
[1024]; /* Redirection link */
2400 const char *text
; /* Status-specific text */
2405 if (code
== HTTP_UNAUTHORIZED
)
2406 text
= _cupsLangString(con
->language
,
2407 _("Enter your username and password or the "
2408 "root username and password to access this "
2409 "page. If you are using Kerberos authentication, "
2410 "make sure you have a valid Kerberos ticket."));
2411 else if (code
== HTTP_UPGRADE_REQUIRED
)
2415 snprintf(urltext
, sizeof(urltext
),
2416 _cupsLangString(con
->language
,
2417 _("You must access this page using the URL "
2418 "<A HREF=\"https://%s:%d%s\">"
2419 "https://%s:%d%s</A>.")),
2420 con
->servername
, con
->serverport
, con
->uri
,
2421 con
->servername
, con
->serverport
, con
->uri
);
2423 snprintf(redirect
, sizeof(redirect
),
2424 "<META HTTP-EQUIV=\"Refresh\" "
2425 "CONTENT=\"3;URL=https://%s:%d%s\">\n",
2426 con
->servername
, con
->serverport
, con
->uri
);
2431 snprintf(message
, sizeof(message
),
2432 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" "
2433 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
2436 "\t<META HTTP-EQUIV=\"Content-Type\" "
2437 "CONTENT=\"text/html; charset=utf-8\">\n"
2438 "\t<TITLE>%d %s</TITLE>\n"
2439 "\t<LINK REL=\"STYLESHEET\" TYPE=\"text/css\" "
2440 "HREF=\"/cups.css\">\n"
2448 code
, httpStatus(code
), redirect
, code
, httpStatus(code
), text
);
2450 if (httpPrintf(HTTP(con
), "Content-Type: text/html; charset=utf-8\r\n") < 0)
2452 if (httpPrintf(HTTP(con
), "Content-Length: %d\r\n",
2453 (int)strlen(message
)) < 0)
2455 if (httpPrintf(HTTP(con
), "\r\n") < 0)
2457 if (httpPrintf(HTTP(con
), "%s", message
) < 0)
2460 else if (httpPrintf(HTTP(con
), "\r\n") < 0)
2463 if (cupsdFlushHeader(con
) < 0)
2466 con
->http
.state
= HTTP_WAITING
;
2473 * 'cupsdSendHeader()' - Send an HTTP request.
2476 int /* O - 1 on success, 0 on failure */
2478 cupsd_client_t
*con
, /* I - Client to send to */
2479 http_status_t code
, /* I - HTTP status code */
2480 char *type
, /* I - MIME type of document */
2481 int auth_type
) /* I - Type of authentication */
2483 char auth_str
[1024]; /* Authorization string */
2485 static char *gss_buf
= NULL
; /* Kerberos auth data buffer */
2486 static int gss_bufsize
= 0; /* Size of Kerberos auth data buffer */
2487 #endif /* HAVE_GSSAPI */
2491 * Send the HTTP status header...
2494 if (code
== HTTP_CONTINUE
)
2497 * 100-continue doesn't send any headers...
2500 return (httpPrintf(HTTP(con
), "HTTP/%d.%d 100 Continue\r\n\r\n",
2501 con
->http
.version
/ 100, con
->http
.version
% 100) > 0);
2504 httpFlushWrite(HTTP(con
));
2506 con
->http
.data_encoding
= HTTP_ENCODE_FIELDS
;
2508 if (httpPrintf(HTTP(con
), "HTTP/%d.%d %d %s\r\n", con
->http
.version
/ 100,
2509 con
->http
.version
% 100, code
, httpStatus(code
)) < 0)
2511 if (httpPrintf(HTTP(con
), "Date: %s\r\n", httpGetDateString(time(NULL
))) < 0)
2514 if (httpPrintf(HTTP(con
), "Server: %s\r\n", ServerHeader
) < 0)
2516 if (con
->http
.keep_alive
&& con
->http
.version
>= HTTP_1_0
)
2518 if (httpPrintf(HTTP(con
), "Connection: Keep-Alive\r\n") < 0)
2520 if (httpPrintf(HTTP(con
), "Keep-Alive: timeout=%d\r\n",
2521 KeepAliveTimeout
) < 0)
2524 if (code
== HTTP_METHOD_NOT_ALLOWED
)
2525 if (httpPrintf(HTTP(con
), "Allow: GET, HEAD, OPTIONS, POST, PUT\r\n") < 0)
2528 if (code
== HTTP_UNAUTHORIZED
)
2530 if (auth_type
== CUPSD_AUTH_NONE
)
2532 if (!con
->best
|| con
->best
->type
<= CUPSD_AUTH_NONE
)
2533 auth_type
= DefaultAuthType
;
2535 auth_type
= con
->best
->type
;
2540 if (auth_type
== CUPSD_AUTH_BASIC
|| auth_type
== CUPSD_AUTH_BASICDIGEST
)
2541 strlcpy(auth_str
, "Basic realm=\"CUPS\"", sizeof(auth_str
));
2542 else if (auth_type
== CUPSD_AUTH_DIGEST
)
2543 snprintf(auth_str
, sizeof(auth_str
), "Digest realm=\"CUPS\", nonce=\"%s\"",
2544 con
->http
.hostname
);
2546 else if (auth_type
== CUPSD_AUTH_NEGOTIATE
&& con
->gss_output_token
.length
== 0)
2547 strlcpy(auth_str
, "Negotiate", sizeof(auth_str
));
2548 #endif /* HAVE_GSSAPI */
2550 if (con
->best
&& auth_type
!= CUPSD_AUTH_NEGOTIATE
&&
2551 !strcasecmp(con
->http
.hostname
, "localhost"))
2554 * Add a "trc" (try root certification) parameter for local non-Kerberos
2555 * requests when the request requires system group membership - then the
2556 * client knows the root certificate can/should be used.
2558 * Also, for Mac OS X we also look for @AUTHKEY and add an "authkey"
2559 * parameter as needed...
2562 int i
; /* Looping var */
2563 char *auth_key
; /* Auth key buffer */
2564 size_t auth_size
; /* Size of remaining buffer */
2566 auth_key
= auth_str
+ strlen(auth_str
);
2567 auth_size
= sizeof(auth_str
) - (auth_key
- auth_str
);
2569 for (i
= 0; i
< con
->best
->num_names
; i
++)
2571 #ifdef HAVE_AUTHORIZATION_H
2572 if (!strncasecmp(con
->best
->names
[i
], "@AUTHKEY(", 9))
2574 snprintf(auth_key
, auth_size
, ", authkey=\"%s\"",
2575 con
->best
->names
[i
] + 9);
2576 /* end parenthesis is stripped in conf.c */
2580 #endif /* HAVE_AUTHORIZATION_H */
2581 if (!strcasecmp(con
->best
->names
[i
], "@SYSTEM"))
2583 #ifdef HAVE_AUTHORIZATION_H
2584 if (SystemGroupAuthKey
)
2585 snprintf(auth_key
, auth_size
,
2586 ", authkey=\"%s\", trc=\"y\"",
2587 SystemGroupAuthKey
);
2590 strlcpy(auth_key
, ", trc=\"y\"", auth_size
);
2591 #endif /* HAVE_AUTHORIZATION_H */
2599 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2600 "cupsdSendHeader: %d WWW-Authenticate: %s", con
->http
.fd
,
2603 if (httpPrintf(HTTP(con
), "WWW-Authenticate: %s\r\n", auth_str
) < 0)
2610 * WWW-Authenticate: Negotiate can be included even for
2611 * non-401 replies...
2614 if (con
->gss_output_token
.length
> 0 && con
->gss_output_token
.length
<= 65536)
2616 OM_uint32 minor_status
; /* Minor status code */
2617 int bufsize
; /* Size of output token buffer */
2620 bufsize
= con
->gss_output_token
.length
* 4 / 3 + 2;
2622 if (bufsize
> gss_bufsize
)
2624 char *buf
; /* New buffer */
2627 bufsize
= (bufsize
+ 1023) & 1023;/* Round up */
2630 buf
= realloc(gss_buf
, bufsize
);
2632 buf
= malloc(bufsize
);
2636 cupsdLogMessage(CUPSD_LOG_ERROR
,
2637 "Unable to allocate %d bytes for Kerberos credentials!",
2643 gss_bufsize
= bufsize
;
2646 httpEncode64_2(gss_buf
, gss_bufsize
,
2647 con
->gss_output_token
.value
,
2648 con
->gss_output_token
.length
);
2649 gss_release_buffer(&minor_status
, &con
->gss_output_token
);
2651 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2652 "cupsdSendHeader: WWW-Authenticate: Negotiate %s", gss_buf
);
2654 if (httpPrintf(HTTP(con
), "WWW-Authenticate: Negotiate %s\r\n",
2658 else if (con
->gss_output_token
.length
> 65536)
2660 cupsdLogMessage(CUPSD_LOG_ERROR
,
2661 "Kerberos credentials larger than 64k (%d)!",
2662 (int)con
->gss_output_token
.length
);
2665 #endif /* HAVE_GSSAPI */
2667 if (con
->language
&& strcmp(con
->language
->language
, "C"))
2669 if (httpPrintf(HTTP(con
), "Content-Language: %s\r\n",
2670 con
->language
->language
) < 0)
2676 if (!strcmp(type
, "text/html"))
2678 if (httpPrintf(HTTP(con
),
2679 "Content-Type: text/html; charset=utf-8\r\n") < 0)
2682 else if (httpPrintf(HTTP(con
), "Content-Type: %s\r\n", type
) < 0)
2691 * 'cupsdUpdateCGI()' - Read status messages from CGI scripts and programs.
2695 cupsdUpdateCGI(void)
2697 char *ptr
, /* Pointer to end of line in buffer */
2698 message
[1024]; /* Pointer to message text */
2699 int loglevel
; /* Log level for message */
2702 while ((ptr
= cupsdStatBufUpdate(CGIStatusBuffer
, &loglevel
,
2703 message
, sizeof(message
))) != NULL
)
2705 if (loglevel
== CUPSD_LOG_INFO
)
2706 cupsdLogMessage(CUPSD_LOG_INFO
, "%s", message
);
2708 if (!strchr(CGIStatusBuffer
->buffer
, '\n'))
2712 if (ptr
== NULL
&& !CGIStatusBuffer
->bufused
)
2715 * Fatal error on pipe - should never happen!
2718 cupsdLogMessage(CUPSD_LOG_CRIT
,
2719 "cupsdUpdateCGI: error reading from CGI error pipe - %s",
2726 * 'cupsdWriteClient()' - Write data to a client as needed.
2730 cupsdWriteClient(cupsd_client_t
*con
) /* I - Client connection */
2732 int bytes
, /* Number of bytes written */
2733 field_col
; /* Current column */
2734 char *bufptr
, /* Pointer into buffer */
2735 *bufend
; /* Pointer to end of buffer */
2736 ipp_state_t ipp_state
; /* IPP state value */
2739 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2740 "cupsdWriteClient(con=%p(%d)) response=%p(%d), file=%d "
2741 "pipe_pid=%d state=%d",
2742 con
, con
->http
.fd
, con
->response
,
2743 con
->response
? con
->response
->state
: -1,
2744 con
->file
, con
->pipe_pid
, con
->http
.state
);
2746 if (con
->http
.state
!= HTTP_GET_SEND
&&
2747 con
->http
.state
!= HTTP_POST_SEND
)
2750 * If we get called in the wrong state, then something went wrong with the
2751 * connection and we need to shut it down...
2754 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2755 "cupsdWriteClient: %d Closing on unknown HTTP state %d",
2756 con
->http
.fd
, con
->http
.state
);
2757 cupsdCloseClient(con
);
2764 * Make sure we select on the CGI output...
2767 cupsdAddSelect(con
->file
, (cupsd_selfunc_t
)write_pipe
, NULL
, con
);
2769 if (!con
->file_ready
)
2772 * Try again later when there is CGI output available...
2775 cupsdRemoveSelect(con
->http
.fd
);
2779 con
->file_ready
= 0;
2782 if (con
->response
&& con
->response
->state
!= IPP_DATA
)
2784 ipp_state
= ippWrite(HTTP(con
), con
->response
);
2785 bytes
= ipp_state
!= IPP_ERROR
&&
2786 (con
->file
>= 0 || ipp_state
!= IPP_DATA
);
2788 else if ((bytes
= read(con
->file
, con
->header
,
2789 sizeof(con
->header
) - con
->header_used
)) > 0)
2791 con
->header_used
+= bytes
;
2793 if (con
->pipe_pid
&& !con
->got_fields
)
2796 * Inspect the data for Content-Type and other fields.
2799 for (bufptr
= con
->header
, bufend
= con
->header
+ con
->header_used
,
2801 !con
->got_fields
&& bufptr
< bufend
;
2804 if (*bufptr
== '\n')
2807 * Send line to client...
2810 if (bufptr
> con
->header
&& bufptr
[-1] == '\r')
2814 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Script header: %s", con
->header
);
2816 if (!con
->sent_header
)
2819 * Handle redirection and CGI status codes...
2822 if (!strncasecmp(con
->header
, "Location:", 9))
2824 cupsdSendHeader(con
, HTTP_SEE_OTHER
, NULL
, CUPSD_AUTH_NONE
);
2825 con
->sent_header
= 2;
2827 if (httpPrintf(HTTP(con
), "Content-Length: 0\r\n") < 0)
2830 else if (!strncasecmp(con
->header
, "Status:", 7))
2832 cupsdSendError(con
, (http_status_t
)atoi(con
->header
+ 7),
2834 con
->sent_header
= 2;
2838 cupsdSendHeader(con
, HTTP_OK
, NULL
, CUPSD_AUTH_NONE
);
2839 con
->sent_header
= 1;
2841 if (con
->http
.version
== HTTP_1_1
)
2843 if (httpPrintf(HTTP(con
), "Transfer-Encoding: chunked\r\n") < 0)
2849 if (strncasecmp(con
->header
, "Status:", 7))
2850 httpPrintf(HTTP(con
), "%s\r\n", con
->header
);
2856 con
->header_used
-= bufptr
- con
->header
;
2858 if (con
->header_used
> 0)
2859 memmove(con
->header
, bufptr
, con
->header_used
);
2861 bufptr
= con
->header
- 1;
2864 * See if the line was empty...
2869 con
->got_fields
= 1;
2871 if (cupsdFlushHeader(con
) < 0)
2873 cupsdCloseClient(con
);
2877 if (con
->http
.version
== HTTP_1_1
)
2878 con
->http
.data_encoding
= HTTP_ENCODE_CHUNKED
;
2883 else if (*bufptr
!= '\r')
2887 if (!con
->got_fields
)
2889 con
->http
.activity
= time(NULL
);
2894 if (con
->header_used
> 0)
2896 if (httpWrite2(HTTP(con
), con
->header
, con
->header_used
) < 0)
2898 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2899 "cupsdWriteClient: %d Closing for error %d (%s)",
2900 con
->http
.fd
, con
->http
.error
,
2901 strerror(con
->http
.error
));
2902 cupsdCloseClient(con
);
2906 if (con
->http
.data_encoding
== HTTP_ENCODE_CHUNKED
)
2907 httpFlushWrite(HTTP(con
));
2909 con
->bytes
+= con
->header_used
;
2911 if (con
->http
.state
== HTTP_WAITING
)
2914 bytes
= con
->header_used
;
2916 con
->header_used
= 0;
2921 (con
->http
.state
!= HTTP_GET_SEND
&& con
->http
.state
!= HTTP_POST_SEND
))
2923 if (!con
->sent_header
&& con
->pipe_pid
)
2924 cupsdSendError(con
, HTTP_SERVER_ERROR
, CUPSD_AUTH_NONE
);
2927 cupsdLogRequest(con
, HTTP_OK
);
2929 httpFlushWrite(HTTP(con
));
2931 if (con
->http
.data_encoding
== HTTP_ENCODE_CHUNKED
&& con
->sent_header
== 1)
2933 if (httpWrite2(HTTP(con
), "", 0) < 0)
2935 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2936 "cupsdWriteClient: %d Closing for error %d (%s)",
2937 con
->http
.fd
, con
->http
.error
,
2938 strerror(con
->http
.error
));
2939 cupsdCloseClient(con
);
2945 con
->http
.state
= HTTP_WAITING
;
2947 cupsdAddSelect(con
->http
.fd
, (cupsd_selfunc_t
)cupsdReadClient
, NULL
, con
);
2951 cupsdRemoveSelect(con
->file
);
2954 cupsdEndProcess(con
->pipe_pid
, 0);
2963 unlink(con
->filename
);
2964 cupsdClearString(&con
->filename
);
2969 ippDelete(con
->request
);
2970 con
->request
= NULL
;
2975 ippDelete(con
->response
);
2976 con
->response
= NULL
;
2979 cupsdClearString(&con
->command
);
2980 cupsdClearString(&con
->options
);
2981 cupsdClearString(&con
->query_string
);
2983 if (!con
->http
.keep_alive
)
2985 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2986 "cupsdWriteClient: %d Closing because Keep-Alive disabled",
2988 cupsdCloseClient(con
);
2993 cupsArrayRemove(ActiveClients
, con
);
2994 cupsdSetBusyState();
2998 con
->http
.activity
= time(NULL
);
3003 * 'check_if_modified()' - Decode an "If-Modified-Since" line.
3006 static int /* O - 1 if modified since */
3008 cupsd_client_t
*con
, /* I - Client connection */
3009 struct stat
*filestats
) /* I - File information */
3011 char *ptr
; /* Pointer into field */
3012 time_t date
; /* Time/date value */
3013 off_t size
; /* Size/length value */
3018 ptr
= con
->http
.fields
[HTTP_FIELD_IF_MODIFIED_SINCE
];
3023 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3024 "check_if_modified(con=%p(%d), "
3025 "filestats=%p(" CUPS_LLFMT
", %d)) If-Modified-Since=\"%s\"",
3026 con
, con
->http
.fd
, filestats
, CUPS_LLCAST filestats
->st_size
,
3027 (int)filestats
->st_mtime
, ptr
);
3029 while (*ptr
!= '\0')
3031 while (isspace(*ptr
) || *ptr
== ';')
3034 if (strncasecmp(ptr
, "length=", 7) == 0)
3037 size
= strtoll(ptr
, NULL
, 10);
3039 while (isdigit(*ptr
))
3042 else if (isalpha(*ptr
))
3044 date
= httpGetDateTime(ptr
);
3045 while (*ptr
!= '\0' && *ptr
!= ';')
3052 return ((size
!= filestats
->st_size
&& size
!= 0) ||
3053 (date
< filestats
->st_mtime
&& date
!= 0) ||
3054 (size
== 0 && date
== 0));
3059 * 'compare_clients()' - Compare two client connections.
3062 static int /* O - Result of comparison */
3063 compare_clients(cupsd_client_t
*a
, /* I - First client */
3064 cupsd_client_t
*b
, /* I - Second client */
3065 void *data
) /* I - User data (not used) */
3079 * 'data_ready()' - Check whether data is available from a client.
3082 static int /* O - 1 if data is ready, 0 otherwise */
3083 data_ready(cupsd_client_t
*con
) /* I - Client */
3085 if (con
->http
.used
> 0)
3088 else if (con
->http
.tls
)
3091 if (SSL_pending((SSL
*)(con
->http
.tls
)))
3093 # elif defined(HAVE_GNUTLS)
3094 if (gnutls_record_check_pending(((http_tls_t
*)(con
->http
.tls
))->session
))
3096 # elif defined(HAVE_CDSASSL)
3097 size_t bytes
; /* Bytes that are available */
3099 if (!SSLGetBufferedReadSize(((http_tls_t
*)(con
->http
.tls
))->session
,
3100 &bytes
) && bytes
> 0)
3102 # endif /* HAVE_LIBSSL */
3104 #endif /* HAVE_SSL */
3112 * 'encrypt_client()' - Enable encryption for the client...
3115 static int /* O - 1 on success, 0 on error */
3116 encrypt_client(cupsd_client_t
*con
) /* I - Client to encrypt */
3119 SSL_CTX
*context
; /* Context for encryption */
3120 SSL
*conn
; /* Connection for encryption */
3121 BIO
*bio
; /* BIO data */
3122 unsigned long error
; /* Error code */
3125 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "encrypt_client(con=%p(%d))", con
,
3129 * Verify that we have a certificate...
3132 if (access(ServerKey
, 0) || access(ServerCertificate
, 0))
3135 * Nope, make a self-signed certificate...
3138 if (!make_certificate(con
))
3143 * Create the SSL context and accept the connection...
3146 context
= SSL_CTX_new(SSLv23_server_method());
3148 SSL_CTX_set_options(context
, SSL_OP_NO_SSLv2
); /* Only use SSLv3 or TLS */
3149 if (SSLOptions
& CUPSD_SSL_NOEMPTY
)
3150 SSL_CTX_set_options(context
, SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS
);
3151 SSL_CTX_use_PrivateKey_file(context
, ServerKey
, SSL_FILETYPE_PEM
);
3152 SSL_CTX_use_certificate_chain_file(context
, ServerCertificate
);
3154 bio
= BIO_new(_httpBIOMethods());
3155 BIO_ctrl(bio
, BIO_C_SET_FILE_PTR
, 0, (char *)HTTP(con
));
3157 conn
= SSL_new(context
);
3158 SSL_set_bio(conn
, bio
, bio
);
3160 if (SSL_accept(conn
) != 1)
3162 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to encrypt connection from %s!",
3163 con
->http
.hostname
);
3165 while ((error
= ERR_get_error()) != 0)
3166 cupsdLogMessage(CUPSD_LOG_ERROR
, "%s", ERR_error_string(error
, NULL
));
3168 SSL_CTX_free(context
);
3173 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Connection from %s now encrypted.",
3174 con
->http
.hostname
);
3176 con
->http
.tls
= conn
;
3179 # elif defined(HAVE_GNUTLS)
3180 http_tls_t
*conn
; /* TLS session object */
3181 int error
; /* Error code */
3182 gnutls_certificate_server_credentials
*credentials
;
3183 /* TLS credentials */
3186 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "encrypt_client(con=%p(%d))", con
,
3190 * Verify that we have a certificate...
3193 if (access(ServerKey
, 0) || access(ServerCertificate
, 0))
3196 * Nope, make a self-signed certificate...
3199 if (!make_certificate(con
))
3204 * Create the SSL object and perform the SSL handshake...
3207 conn
= (http_tls_t
*)malloc(sizeof(http_tls_t
));
3212 credentials
= (gnutls_certificate_server_credentials
*)
3213 malloc(sizeof(gnutls_certificate_server_credentials
));
3214 if (credentials
== NULL
)
3216 cupsdLogMessage(CUPSD_LOG_ERROR
,
3217 "Unable to encrypt connection from %s - %s",
3218 con
->http
.hostname
, strerror(errno
));
3224 gnutls_certificate_allocate_credentials(credentials
);
3225 gnutls_certificate_set_x509_key_file(*credentials
, ServerCertificate
,
3226 ServerKey
, GNUTLS_X509_FMT_PEM
);
3228 gnutls_init(&(conn
->session
), GNUTLS_SERVER
);
3229 gnutls_set_default_priority(conn
->session
);
3230 gnutls_credentials_set(conn
->session
, GNUTLS_CRD_CERTIFICATE
, *credentials
);
3231 gnutls_transport_set_ptr(conn
->session
, (gnutls_transport_ptr
)HTTP(con
));
3232 gnutls_transport_set_pull_function(conn
->session
, _httpReadGNUTLS
);
3233 gnutls_transport_set_push_function(conn
->session
, _httpWriteGNUTLS
);
3235 error
= gnutls_handshake(conn
->session
);
3237 if (error
!= GNUTLS_E_SUCCESS
)
3239 cupsdLogMessage(CUPSD_LOG_ERROR
,
3240 "Unable to encrypt connection from %s - %s",
3241 con
->http
.hostname
, gnutls_strerror(error
));
3243 gnutls_deinit(conn
->session
);
3244 gnutls_certificate_free_credentials(*credentials
);
3250 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Connection from %s now encrypted.",
3251 con
->http
.hostname
);
3253 conn
->credentials
= credentials
;
3254 con
->http
.tls
= conn
;
3257 # elif defined(HAVE_CDSASSL)
3258 OSStatus error
; /* Error code */
3259 http_tls_t
*conn
; /* CDSA connection information */
3262 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "encrypt_client(con=%p(%d))", con
,
3265 if ((conn
= (http_tls_t
*)malloc(sizeof(http_tls_t
))) == NULL
)
3269 conn
->session
= NULL
;
3270 conn
->certsArray
= get_cdsa_certificate(con
);
3272 if (!conn
->certsArray
)
3275 * No keychain (yet), make a self-signed certificate...
3278 if (make_certificate(con
))
3279 conn
->certsArray
= get_cdsa_certificate(con
);
3282 if (!conn
->certsArray
)
3284 cupsdLogMessage(CUPSD_LOG_ERROR
,
3285 "Could not find signing key in keychain \"%s\"",
3287 error
= errSSLBadCert
; /* errSSLBadConfiguration is a better choice, but not available on 10.2.x */
3291 error
= SSLNewContext(true, &conn
->session
);
3294 error
= SSLSetIOFuncs(conn
->session
, _httpReadCDSA
, _httpWriteCDSA
);
3297 error
= SSLSetProtocolVersionEnabled(conn
->session
, kSSLProtocol2
, false);
3300 error
= SSLSetConnection(conn
->session
, HTTP(con
));
3303 error
= SSLSetAllowsExpiredCerts(conn
->session
, true);
3306 error
= SSLSetAllowsAnyRoot(conn
->session
, true);
3309 error
= SSLSetCertificate(conn
->session
, conn
->certsArray
);
3314 * Perform SSL/TLS handshake
3317 while ((error
= SSLHandshake(conn
->session
)) == errSSLWouldBlock
)
3323 cupsdLogMessage(CUPSD_LOG_ERROR
,
3324 "Unable to encrypt connection from %s - %s (%d)",
3325 con
->http
.hostname
, cssmErrorString(error
), (int)error
);
3327 con
->http
.error
= error
;
3328 con
->http
.status
= HTTP_ERROR
;
3331 SSLDisposeContext(conn
->session
);
3333 if (conn
->certsArray
)
3334 CFRelease(conn
->certsArray
);
3341 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Connection from %s now encrypted.",
3342 con
->http
.hostname
);
3344 con
->http
.tls
= conn
;
3347 # endif /* HAVE_LIBSSL */
3349 #endif /* HAVE_SSL */
3354 * 'get_cdsa_certificate()' - Get a SSL/TLS certificate from the System keychain.
3357 static CFArrayRef
/* O - Array of certificates */
3358 get_cdsa_certificate(
3359 cupsd_client_t
*con
) /* I - Client connection */
3361 OSStatus err
; /* Error info */
3362 SecKeychainRef keychain
= NULL
;/* Keychain reference */
3363 SecIdentitySearchRef search
= NULL
; /* Search reference */
3364 SecIdentityRef identity
= NULL
;/* Identity */
3365 CFArrayRef certificates
= NULL
;
3366 /* Certificate array */
3367 # if HAVE_SECPOLICYCREATESSL
3368 SecPolicyRef policy
= NULL
; /* Policy ref */
3369 CFStringRef servername
= NULL
;
3371 CFMutableDictionaryRef query
= NULL
; /* Query qualifiers */
3372 char localname
[1024];/* Local hostname */
3373 # elif defined(HAVE_SECIDENTITYSEARCHCREATEWITHPOLICY)
3374 SecPolicyRef policy
= NULL
; /* Policy ref */
3375 SecPolicySearchRef policy_search
= NULL
;
3376 /* Policy search ref */
3377 CSSM_DATA options
; /* Policy options */
3378 CSSM_APPLE_TP_SSL_OPTIONS
3379 ssl_options
; /* SSL Option for hostname */
3380 char localname
[1024];/* Local hostname */
3381 # endif /* HAVE_SECPOLICYCREATESSL */
3384 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3385 "get_cdsa_certificate: Looking for certs for \"%s\"...",
3388 if ((err
= SecKeychainOpen(ServerCertificate
, &keychain
)))
3390 cupsdLogMessage(CUPSD_LOG_ERROR
, "Cannot open keychain \"%s\" - %s (%d)",
3391 ServerCertificate
, cssmErrorString(err
), (int)err
);
3395 # if HAVE_SECPOLICYCREATESSL
3396 servername
= CFStringCreateWithCString(kCFAllocatorDefault
, con
->servername
,
3397 kCFStringEncodingUTF8
);
3399 if ((policy
= SecPolicyCreateSSL(1, servername
)) == NULL
)
3401 cupsdLogMessage(CUPSD_LOG_ERROR
, "Cannot create ssl policy reference");
3406 CFRelease(servername
);
3408 if (!(query
= CFDictionaryCreateMutable(kCFAllocatorDefault
, 0,
3409 &kCFTypeDictionaryKeyCallBacks
,
3410 &kCFTypeDictionaryValueCallBacks
)))
3412 cupsdLogMessage(CUPSD_LOG_ERROR
, "Cannot create query dictionary");
3416 CFDictionaryAddValue(query
, kSecClass
, kSecClassIdentity
);
3417 CFDictionaryAddValue(query
, kSecMatchPolicy
, policy
);
3418 CFDictionaryAddValue(query
, kSecReturnRef
, kCFBooleanTrue
);
3419 CFDictionaryAddValue(query
, kSecMatchLimit
, kSecMatchLimitOne
);
3421 err
= SecItemCopyMatching(query
, (CFTypeRef
*)&identity
);
3423 if (err
&& DNSSDHostName
)
3426 * Search for the connection server name failed; try the DNS-SD .local
3427 * hostname instead...
3430 snprintf(localname
, sizeof(localname
), "%s.local", DNSSDHostName
);
3432 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3433 "get_cdsa_certificate: Looking for certs for \"%s\"...",
3436 servername
= CFStringCreateWithCString(kCFAllocatorDefault
, localname
,
3437 kCFStringEncodingUTF8
);
3441 if ((policy
= SecPolicyCreateSSL(1, servername
)) == NULL
)
3443 cupsdLogMessage(CUPSD_LOG_ERROR
, "Cannot create ssl policy reference");
3448 CFRelease(servername
);
3450 CFDictionarySetValue(query
, kSecMatchPolicy
, policy
);
3452 err
= SecItemCopyMatching(query
, (CFTypeRef
*)&identity
);
3457 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3458 "Cannot find signing key in keychain \"%s\": %s (%d)",
3459 ServerCertificate
, cssmErrorString(err
), (int)err
);
3463 # elif defined(HAVE_SECIDENTITYSEARCHCREATEWITHPOLICY)
3465 * Use a policy to search for valid certificates who's common name matches the
3469 if (SecPolicySearchCreate(CSSM_CERT_X_509v3
, &CSSMOID_APPLE_TP_SSL
,
3470 NULL
, &policy_search
))
3472 cupsdLogMessage(CUPSD_LOG_ERROR
, "Cannot create a policy search reference");
3476 if (SecPolicySearchCopyNext(policy_search
, &policy
))
3478 cupsdLogMessage(CUPSD_LOG_ERROR
,
3479 "Cannot find a policy to use for searching");
3483 memset(&ssl_options
, 0, sizeof(ssl_options
));
3484 ssl_options
.Version
= CSSM_APPLE_TP_SSL_OPTS_VERSION
;
3485 ssl_options
.ServerName
= con
->servername
;
3486 ssl_options
.ServerNameLen
= strlen(con
->servername
);
3488 options
.Data
= (uint8
*)&ssl_options
;
3489 options
.Length
= sizeof(ssl_options
);
3491 if (SecPolicySetValue(policy
, &options
))
3493 cupsdLogMessage(CUPSD_LOG_ERROR
,
3494 "Cannot set policy value to use for searching");
3498 if ((err
= SecIdentitySearchCreateWithPolicy(policy
, NULL
, CSSM_KEYUSE_SIGN
,
3499 keychain
, FALSE
, &search
)))
3501 cupsdLogMessage(CUPSD_LOG_ERROR
,
3502 "Cannot create identity search reference: %s (%d)",
3503 cssmErrorString(err
), (int)err
);
3507 err
= SecIdentitySearchCopyNext(search
, &identity
);
3509 if (err
&& DNSSDHostName
)
3512 * Search for the connection server name failed; try the DNS-SD .local
3513 * hostname instead...
3516 snprintf(localname
, sizeof(localname
), "%s.local", DNSSDHostName
);
3518 ssl_options
.ServerName
= localname
;
3519 ssl_options
.ServerNameLen
= strlen(localname
);
3521 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3522 "get_cdsa_certificate: Looking for certs for \"%s\"...",
3525 if (SecPolicySetValue(policy
, &options
))
3527 cupsdLogMessage(CUPSD_LOG_ERROR
,
3528 "Cannot set policy value to use for searching");
3532 err
= SecIdentitySearchCopyNext(search
, &identity
);
3537 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3538 "Cannot find signing key in keychain \"%s\": %s (%d)",
3539 ServerCertificate
, cssmErrorString(err
), (int)err
);
3545 * Assume there is exactly one SecIdentity in the keychain...
3548 if ((err
= SecIdentitySearchCreate(keychain
, CSSM_KEYUSE_SIGN
, &search
)))
3550 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3551 "Cannot create identity search reference (%d)", (int)err
);
3555 if ((err
= SecIdentitySearchCopyNext(search
, &identity
)))
3557 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3558 "Cannot find signing key in keychain \"%s\": %s (%d)",
3559 ServerCertificate
, cssmErrorString(err
), (int)err
);
3562 # endif /* HAVE_SECPOLICYCREATESSL */
3564 if (CFGetTypeID(identity
) != SecIdentityGetTypeID())
3566 cupsdLogMessage(CUPSD_LOG_ERROR
, "SecIdentity CFTypeID failure!");
3570 if ((certificates
= CFArrayCreate(NULL
, (const void **)&identity
,
3571 1, &kCFTypeArrayCallBacks
)) == NULL
)
3573 cupsdLogMessage(CUPSD_LOG_ERROR
, "Cannot create certificate array");
3580 CFRelease(keychain
);
3584 CFRelease(identity
);
3586 # if HAVE_SECPOLICYCREATESSL
3592 # elif defined(HAVE_SECIDENTITYSEARCHCREATEWITHPOLICY)
3596 CFRelease(policy_search
);
3597 # endif /* HAVE_SECPOLICYCREATESSL */
3599 return (certificates
);
3601 #endif /* HAVE_CDSASSL */
3605 * 'get_file()' - Get a filename and state info.
3608 static char * /* O - Real filename */
3609 get_file(cupsd_client_t
*con
, /* I - Client connection */
3610 struct stat
*filestats
, /* O - File information */
3611 char *filename
, /* IO - Filename buffer */
3612 int len
) /* I - Buffer length */
3614 int status
; /* Status of filesystem calls */
3615 char *ptr
; /* Pointer info filename */
3616 int plen
; /* Remaining length after pointer */
3617 char language
[7]; /* Language subdirectory, if any */
3621 * Figure out the real filename...
3626 if (!strncmp(con
->uri
, "/ppd/", 5))
3627 snprintf(filename
, len
, "%s%s", ServerRoot
, con
->uri
);
3628 else if (!strncmp(con
->uri
, "/rss/", 5) && !strchr(con
->uri
+ 5, '/'))
3629 snprintf(filename
, len
, "%s/rss/%s", CacheDir
, con
->uri
+ 5);
3630 else if (!strncmp(con
->uri
, "/admin/conf/", 12))
3631 snprintf(filename
, len
, "%s%s", ServerRoot
, con
->uri
+ 11);
3632 else if (!strncmp(con
->uri
, "/admin/log/", 11))
3634 if (!strncmp(con
->uri
+ 11, "access_log", 10) && AccessLog
[0] == '/')
3635 strlcpy(filename
, AccessLog
, len
);
3636 else if (!strncmp(con
->uri
+ 11, "error_log", 9) && ErrorLog
[0] == '/')
3637 strlcpy(filename
, ErrorLog
, len
);
3638 else if (!strncmp(con
->uri
+ 11, "page_log", 8) && PageLog
[0] == '/')
3639 strlcpy(filename
, PageLog
, len
);
3643 else if (con
->language
)
3645 snprintf(language
, sizeof(language
), "/%s", con
->language
->language
);
3646 snprintf(filename
, len
, "%s%s%s", DocumentRoot
, language
, con
->uri
);
3649 snprintf(filename
, len
, "%s%s", DocumentRoot
, con
->uri
);
3651 if ((ptr
= strchr(filename
, '?')) != NULL
)
3655 * Grab the status for this language; if there isn't a language-specific file
3656 * then fallback to the default one...
3659 if ((status
= stat(filename
, filestats
)) != 0 && language
[0] &&
3660 strncmp(con
->uri
, "/ppd/", 5) &&
3661 strncmp(con
->uri
, "/admin/conf/", 12) &&
3662 strncmp(con
->uri
, "/admin/log/", 11))
3665 * Drop the country code...
3669 snprintf(filename
, len
, "%s%s%s", DocumentRoot
, language
, con
->uri
);
3671 if ((ptr
= strchr(filename
, '?')) != NULL
)
3674 if ((status
= stat(filename
, filestats
)) != 0)
3677 * Drop the language prefix and try the root directory...
3681 snprintf(filename
, len
, "%s%s", DocumentRoot
, con
->uri
);
3683 if ((ptr
= strchr(filename
, '?')) != NULL
)
3686 status
= stat(filename
, filestats
);
3691 * If we're found a directory, get the index.html file instead...
3694 if (!status
&& S_ISDIR(filestats
->st_mode
))
3697 * Make sure the URI ends with a slash...
3700 if (con
->uri
[strlen(con
->uri
) - 1] != '/')
3701 strlcat(con
->uri
, "/", sizeof(con
->uri
));
3704 * Find the directory index file, trying every language...
3709 if (status
&& language
[0])
3712 * Try a different language subset...
3716 language
[0] = '\0'; /* Strip country code */
3718 language
[0] = '\0'; /* Strip language */
3722 * Look for the index file...
3725 snprintf(filename
, len
, "%s%s%s", DocumentRoot
, language
, con
->uri
);
3727 if ((ptr
= strchr(filename
, '?')) != NULL
)
3730 ptr
= filename
+ strlen(filename
);
3731 plen
= len
- (ptr
- filename
);
3733 strlcpy(ptr
, "index.html", plen
);
3734 status
= stat(filename
, filestats
);
3739 strlcpy(ptr
, "index.class", plen
);
3740 status
= stat(filename
, filestats
);
3742 #endif /* HAVE_JAVA */
3747 strlcpy(ptr
, "index.pl", plen
);
3748 status
= stat(filename
, filestats
);
3750 #endif /* HAVE_PERL */
3755 strlcpy(ptr
, "index.php", plen
);
3756 status
= stat(filename
, filestats
);
3758 #endif /* HAVE_PHP */
3763 strlcpy(ptr
, "index.pyc", plen
);
3764 status
= stat(filename
, filestats
);
3769 strlcpy(ptr
, "index.py", plen
);
3770 status
= stat(filename
, filestats
);
3772 #endif /* HAVE_PYTHON */
3775 while (status
&& language
[0]);
3778 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3779 "get_file(con=%p(%d), filestats=%p, filename=%p, len=%d) = "
3780 "%s", con
, con
->http
.fd
, filestats
, filename
, len
,
3781 status
? "(null)" : filename
);
3791 * 'install_conf_file()' - Install a configuration file.
3794 static http_status_t
/* O - Status */
3795 install_conf_file(cupsd_client_t
*con
) /* I - Connection */
3797 cups_file_t
*in
, /* Input file */
3798 *out
; /* Output file */
3799 char buffer
[1024]; /* Copy buffer */
3800 int bytes
; /* Number of bytes */
3801 char conffile
[1024], /* Configuration filename */
3802 newfile
[1024], /* New config filename */
3803 oldfile
[1024]; /* Old config filename */
3804 struct stat confinfo
; /* Config file info */
3808 * First construct the filenames...
3811 snprintf(conffile
, sizeof(conffile
), "%s%s", ServerRoot
, con
->uri
+ 11);
3812 snprintf(newfile
, sizeof(newfile
), "%s%s.N", ServerRoot
, con
->uri
+ 11);
3813 snprintf(oldfile
, sizeof(oldfile
), "%s%s.O", ServerRoot
, con
->uri
+ 11);
3815 cupsdLogMessage(CUPSD_LOG_INFO
, "Installing config file \"%s\"...", conffile
);
3818 * Get the owner, group, and permissions of the configuration file.
3819 * If it doesn't exist, assign it to the User and Group in the
3820 * cupsd.conf file with mode 0640 permissions.
3823 if (stat(conffile
, &confinfo
))
3825 confinfo
.st_uid
= User
;
3826 confinfo
.st_gid
= Group
;
3827 confinfo
.st_mode
= ConfigFilePerm
;
3831 * Open the request file and new config file...
3834 if ((in
= cupsFileOpen(con
->filename
, "rb")) == NULL
)
3836 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to open request file \"%s\" - %s",
3837 con
->filename
, strerror(errno
));
3838 return (HTTP_SERVER_ERROR
);
3841 if ((out
= cupsFileOpen(newfile
, "wb")) == NULL
)
3844 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to open config file \"%s\" - %s",
3845 newfile
, strerror(errno
));
3846 return (HTTP_SERVER_ERROR
);
3849 fchmod(cupsFileNumber(out
), confinfo
.st_mode
);
3850 fchown(cupsFileNumber(out
), confinfo
.st_uid
, confinfo
.st_gid
);
3853 * Copy from the request to the new config file...
3856 while ((bytes
= cupsFileRead(in
, buffer
, sizeof(buffer
))) > 0)
3857 if (cupsFileWrite(out
, buffer
, bytes
) < bytes
)
3859 cupsdLogMessage(CUPSD_LOG_ERROR
,
3860 "Unable to copy to config file \"%s\" - %s",
3861 newfile
, strerror(errno
));
3867 return (HTTP_SERVER_ERROR
);
3871 * Close the files...
3875 if (cupsFileClose(out
))
3877 cupsdLogMessage(CUPSD_LOG_ERROR
,
3878 "Error file closing config file \"%s\" - %s",
3879 newfile
, strerror(errno
));
3883 return (HTTP_SERVER_ERROR
);
3887 * Remove the request file...
3890 unlink(con
->filename
);
3891 cupsdClearString(&con
->filename
);
3894 * Unlink the old backup, rename the current config file to the backup
3895 * filename, and rename the new config file to the config file name...
3898 if (unlink(oldfile
))
3899 if (errno
!= ENOENT
)
3901 cupsdLogMessage(CUPSD_LOG_ERROR
,
3902 "Unable to remove backup config file \"%s\" - %s",
3903 oldfile
, strerror(errno
));
3907 return (HTTP_SERVER_ERROR
);
3910 if (rename(conffile
, oldfile
))
3911 if (errno
!= ENOENT
)
3913 cupsdLogMessage(CUPSD_LOG_ERROR
,
3914 "Unable to rename old config file \"%s\" - %s",
3915 conffile
, strerror(errno
));
3919 return (HTTP_SERVER_ERROR
);
3922 if (rename(newfile
, conffile
))
3924 cupsdLogMessage(CUPSD_LOG_ERROR
,
3925 "Unable to rename new config file \"%s\" - %s",
3926 newfile
, strerror(errno
));
3928 rename(oldfile
, conffile
);
3931 return (HTTP_SERVER_ERROR
);
3935 * If the cupsd.conf file was updated, set the NeedReload flag...
3938 if (!strcmp(con
->uri
, "/admin/conf/cupsd.conf"))
3939 NeedReload
= RELOAD_CUPSD
;
3941 NeedReload
= RELOAD_ALL
;
3943 ReloadTime
= time(NULL
);
3946 * Return that the file was created successfully...
3949 return (HTTP_CREATED
);
3954 * 'is_cgi()' - Is the resource a CGI script/program?
3957 static int /* O - 1 = CGI, 0 = file */
3958 is_cgi(cupsd_client_t
*con
, /* I - Client connection */
3959 const char *filename
, /* I - Real filename */
3960 struct stat
*filestats
, /* I - File information */
3961 mime_type_t
*type
) /* I - MIME type */
3963 const char *options
; /* Options on URL */
3967 * Get the options, if any...
3970 if ((options
= strchr(con
->uri
, '?')) != NULL
)
3973 cupsdSetStringf(&(con
->query_string
), "QUERY_STRING=%s", options
);
3977 * Check for known types...
3980 if (!type
|| strcasecmp(type
->super
, "application"))
3982 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3983 "is_cgi(con=%p(%d), filename=\"%s\", filestats=%p, "
3984 "type=%s/%s) = 0", con
, con
->http
.fd
, filename
, filestats
,
3985 type
? type
->super
: "unknown",
3986 type
? type
->type
: "unknown");
3990 if (!strcasecmp(type
->type
, "x-httpd-cgi") &&
3991 (filestats
->st_mode
& 0111))
3994 * "application/x-httpd-cgi" is a CGI script.
3997 cupsdSetString(&con
->command
, filename
);
4000 cupsdSetStringf(&con
->options
, " %s", options
);
4002 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4003 "is_cgi(con=%p(%d), filename=\"%s\", filestats=%p, "
4004 "type=%s/%s) = 1", con
, con
->http
.fd
, filename
, filestats
,
4005 type
? type
->super
: "unknown",
4006 type
? type
->type
: "unknown");
4010 else if (!strcasecmp(type
->type
, "x-httpd-java"))
4013 * "application/x-httpd-java" is a Java servlet.
4016 cupsdSetString(&con
->command
, CUPS_JAVA
);
4019 cupsdSetStringf(&con
->options
, " %s %s", filename
, options
);
4021 cupsdSetStringf(&con
->options
, " %s", filename
);
4023 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4024 "is_cgi(con=%p(%d), filename=\"%s\", filestats=%p, "
4025 "type=%s/%s) = 1", con
, con
->http
.fd
, filename
, filestats
,
4026 type
? type
->super
: "unknown",
4027 type
? type
->type
: "unknown");
4030 #endif /* HAVE_JAVA */
4032 else if (!strcasecmp(type
->type
, "x-httpd-perl"))
4035 * "application/x-httpd-perl" is a Perl page.
4038 cupsdSetString(&con
->command
, CUPS_PERL
);
4041 cupsdSetStringf(&con
->options
, " %s %s", filename
, options
);
4043 cupsdSetStringf(&con
->options
, " %s", filename
);
4045 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4046 "is_cgi(con=%p(%d), filename=\"%s\", filestats=%p, "
4047 "type=%s/%s) = 1", con
, con
->http
.fd
, filename
, filestats
,
4048 type
? type
->super
: "unknown",
4049 type
? type
->type
: "unknown");
4052 #endif /* HAVE_PERL */
4054 else if (!strcasecmp(type
->type
, "x-httpd-php"))
4057 * "application/x-httpd-php" is a PHP page.
4060 cupsdSetString(&con
->command
, CUPS_PHP
);
4063 cupsdSetStringf(&con
->options
, " %s %s", filename
, options
);
4065 cupsdSetStringf(&con
->options
, " %s", filename
);
4067 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4068 "is_cgi(con=%p(%d), filename=\"%s\", filestats=%p, "
4069 "type=%s/%s) = 1", con
, con
->http
.fd
, filename
, filestats
,
4070 type
? type
->super
: "unknown",
4071 type
? type
->type
: "unknown");
4074 #endif /* HAVE_PHP */
4076 else if (!strcasecmp(type
->type
, "x-httpd-python"))
4079 * "application/x-httpd-python" is a Python page.
4082 cupsdSetString(&con
->command
, CUPS_PYTHON
);
4085 cupsdSetStringf(&con
->options
, " %s %s", filename
, options
);
4087 cupsdSetStringf(&con
->options
, " %s", filename
);
4089 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4090 "is_cgi(con=%p(%d), filename=\"%s\", filestats=%p, "
4091 "type=%s/%s) = 1", con
, con
->http
.fd
, filename
, filestats
,
4092 type
? type
->super
: "unknown",
4093 type
? type
->type
: "unknown");
4096 #endif /* HAVE_PYTHON */
4098 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4099 "is_cgi(con=%p(%d), filename=\"%s\", filestats=%p, "
4100 "type=%s/%s) = 0", con
, con
->http
.fd
, filename
, filestats
,
4101 type
? type
->super
: "unknown",
4102 type
? type
->type
: "unknown");
4108 * 'is_path_absolute()' - Is a path absolute and free of relative elements (i.e. "..").
4111 static int /* O - 0 if relative, 1 if absolute */
4112 is_path_absolute(const char *path
) /* I - Input path */
4115 * Check for a leading slash...
4122 * Check for "/.." in the path...
4125 while ((path
= strstr(path
, "/..")) != NULL
)
4127 if (!path
[3] || path
[3] == '/')
4134 * If we haven't found any relative paths, return 1 indicating an
4144 * 'make_certificate()' - Make a self-signed SSL/TLS certificate.
4147 static int /* O - 1 on success, 0 on failure */
4148 make_certificate(cupsd_client_t
*con
) /* I - Client connection */
4150 #if defined(HAVE_LIBSSL) && defined(HAVE_WAITPID)
4151 int pid
, /* Process ID of command */
4152 status
; /* Status of command */
4153 char command
[1024], /* Command */
4154 *argv
[12], /* Command-line arguments */
4155 *envp
[MAX_ENV
+ 1], /* Environment variables */
4156 infofile
[1024], /* Type-in information for cert */
4157 seedfile
[1024]; /* Random number seed file */
4158 int envc
, /* Number of environment variables */
4159 bytes
; /* Bytes written */
4160 cups_file_t
*fp
; /* Seed/info file */
4161 int infofd
; /* Info file descriptor */
4165 * Run the "openssl" command to seed the random number generator and
4166 * generate a self-signed certificate that is good for 10 years:
4168 * openssl rand -rand seedfile 1
4170 * openssl req -new -x509 -keyout ServerKey \
4171 * -out ServerCertificate -days 3650 -nodes
4173 * The seeding step is crucial in ensuring that the openssl command
4174 * does not block on systems without sufficient entropy...
4177 if (!cupsFileFind("openssl", getenv("PATH"), 1, command
, sizeof(command
)))
4179 cupsdLogMessage(CUPSD_LOG_ERROR
,
4180 "No SSL certificate and openssl command not found!");
4184 if (access("/dev/urandom", 0))
4187 * If the system doesn't provide /dev/urandom, then any random source
4188 * will probably be blocking-style, so generate some random data to
4189 * use as a seed for the certificate. Note that we have already
4190 * seeded the random number generator in cupsdInitCerts()...
4193 cupsdLogMessage(CUPSD_LOG_INFO
,
4194 "Seeding the random number generator...");
4197 * Write the seed file...
4200 if ((fp
= cupsTempFile2(seedfile
, sizeof(seedfile
))) == NULL
)
4202 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to create seed file %s - %s",
4203 seedfile
, strerror(errno
));
4207 for (bytes
= 0; bytes
< 262144; bytes
++)
4208 cupsFilePutChar(fp
, random());
4213 * Run the openssl command to seed its random number generator...
4216 argv
[0] = "openssl";
4223 envc
= cupsdLoadEnv(envp
, MAX_ENV
);
4226 if (!cupsdStartProcess(command
, argv
, envp
, -1, -1, -1, -1, -1, 1, NULL
,
4233 while (waitpid(pid
, &status
, 0) < 0)
4240 cupsdFinishProcess(pid
, command
, sizeof(command
), NULL
);
4243 * Remove the seed file, as it is no longer needed...
4250 if (WIFEXITED(status
))
4251 cupsdLogMessage(CUPSD_LOG_ERROR
,
4252 "Unable to seed random number generator - "
4253 "the openssl command stopped with status %d!",
4254 WEXITSTATUS(status
));
4256 cupsdLogMessage(CUPSD_LOG_ERROR
,
4257 "Unable to seed random number generator - "
4258 "the openssl command crashed on signal %d!",
4266 * Create a file with the certificate information fields...
4268 * Note: This assumes that the default questions are asked by the openssl
4272 if ((fp
= cupsTempFile2(infofile
, sizeof(infofile
))) == NULL
)
4274 cupsdLogMessage(CUPSD_LOG_ERROR
,
4275 "Unable to create certificate information file %s - %s",
4276 infofile
, strerror(errno
));
4280 cupsFilePrintf(fp
, ".\n.\n.\n%s\n.\n%s\n%s\n",
4281 ServerName
, ServerName
, ServerAdmin
);
4284 cupsdLogMessage(CUPSD_LOG_INFO
,
4285 "Generating SSL server key and certificate...");
4287 argv
[0] = "openssl";
4291 argv
[4] = "-keyout";
4292 argv
[5] = ServerKey
;
4294 argv
[7] = ServerCertificate
;
4297 argv
[10] = "-nodes";
4300 cupsdLoadEnv(envp
, MAX_ENV
);
4302 infofd
= open(infofile
, O_RDONLY
);
4304 if (!cupsdStartProcess(command
, argv
, envp
, infofd
, -1, -1, -1, -1, 1, NULL
,
4315 while (waitpid(pid
, &status
, 0) < 0)
4322 cupsdFinishProcess(pid
, command
, sizeof(command
), NULL
);
4326 if (WIFEXITED(status
))
4327 cupsdLogMessage(CUPSD_LOG_ERROR
,
4328 "Unable to create SSL server key and certificate - "
4329 "the openssl command stopped with status %d!",
4330 WEXITSTATUS(status
));
4332 cupsdLogMessage(CUPSD_LOG_ERROR
,
4333 "Unable to create SSL server key and certificate - "
4334 "the openssl command crashed on signal %d!",
4339 cupsdLogMessage(CUPSD_LOG_INFO
, "Created SSL server key file \"%s\"...",
4341 cupsdLogMessage(CUPSD_LOG_INFO
,
4342 "Created SSL server certificate file \"%s\"...",
4348 #elif defined(HAVE_GNUTLS)
4349 gnutls_x509_crt crt
; /* Self-signed certificate */
4350 gnutls_x509_privkey key
; /* Encryption key */
4351 cups_lang_t
*language
; /* Default language info */
4352 cups_file_t
*fp
; /* Key/cert file */
4353 unsigned char buffer
[8192]; /* Buffer for x509 data */
4354 size_t bytes
; /* Number of bytes of data */
4355 unsigned char serial
[4]; /* Serial number buffer */
4356 time_t curtime
; /* Current time */
4357 int result
; /* Result of GNU TLS calls */
4361 * Create the encryption key...
4364 cupsdLogMessage(CUPSD_LOG_INFO
, "Generating SSL server key...");
4366 gnutls_x509_privkey_init(&key
);
4367 gnutls_x509_privkey_generate(key
, GNUTLS_PK_RSA
, 2048, 0);
4373 bytes
= sizeof(buffer
);
4375 if ((result
= gnutls_x509_privkey_export(key
, GNUTLS_X509_FMT_PEM
,
4376 buffer
, &bytes
)) < 0)
4378 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to export SSL server key - %s",
4379 gnutls_strerror(result
));
4380 gnutls_x509_privkey_deinit(key
);
4383 else if ((fp
= cupsFileOpen(ServerKey
, "w")) != NULL
)
4385 cupsFileWrite(fp
, (char *)buffer
, bytes
);
4388 cupsdLogMessage(CUPSD_LOG_INFO
, "Created SSL server key file \"%s\"...",
4393 cupsdLogMessage(CUPSD_LOG_ERROR
,
4394 "Unable to create SSL server key file \"%s\" - %s",
4395 ServerKey
, strerror(errno
));
4396 gnutls_x509_privkey_deinit(key
);
4401 * Create the self-signed certificate...
4404 cupsdLogMessage(CUPSD_LOG_INFO
, "Generating self-signed SSL certificate...");
4406 language
= cupsLangDefault();
4407 curtime
= time(NULL
);
4408 serial
[0] = curtime
>> 24;
4409 serial
[1] = curtime
>> 16;
4410 serial
[2] = curtime
>> 8;
4411 serial
[3] = curtime
;
4413 gnutls_x509_crt_init(&crt
);
4414 if (strlen(language
->language
) == 5)
4415 gnutls_x509_crt_set_dn_by_oid(crt
, GNUTLS_OID_X520_COUNTRY_NAME
, 0,
4416 language
->language
+ 3, 2);
4418 gnutls_x509_crt_set_dn_by_oid(crt
, GNUTLS_OID_X520_COUNTRY_NAME
, 0,
4420 gnutls_x509_crt_set_dn_by_oid(crt
, GNUTLS_OID_X520_COMMON_NAME
, 0,
4421 ServerName
, strlen(ServerName
));
4422 gnutls_x509_crt_set_dn_by_oid(crt
, GNUTLS_OID_X520_ORGANIZATION_NAME
, 0,
4423 ServerName
, strlen(ServerName
));
4424 gnutls_x509_crt_set_dn_by_oid(crt
, GNUTLS_OID_X520_ORGANIZATIONAL_UNIT_NAME
,
4426 gnutls_x509_crt_set_dn_by_oid(crt
, GNUTLS_OID_X520_STATE_OR_PROVINCE_NAME
, 0,
4428 gnutls_x509_crt_set_dn_by_oid(crt
, GNUTLS_OID_X520_LOCALITY_NAME
, 0,
4430 gnutls_x509_crt_set_dn_by_oid(crt
, GNUTLS_OID_PKCS9_EMAIL
, 0,
4431 ServerAdmin
, strlen(ServerAdmin
));
4432 gnutls_x509_crt_set_key(crt
, key
);
4433 gnutls_x509_crt_set_serial(crt
, serial
, sizeof(serial
));
4434 gnutls_x509_crt_set_activation_time(crt
, curtime
);
4435 gnutls_x509_crt_set_expiration_time(crt
, curtime
+ 10 * 365 * 86400);
4436 gnutls_x509_crt_set_ca_status(crt
, 0);
4437 gnutls_x509_crt_set_subject_alternative_name(crt
, GNUTLS_SAN_DNSNAME
,
4439 gnutls_x509_crt_set_key_purpose_oid(crt
, GNUTLS_KP_TLS_WWW_SERVER
, 0);
4440 gnutls_x509_crt_set_key_usage(crt
, GNUTLS_KEY_KEY_ENCIPHERMENT
);
4441 gnutls_x509_crt_set_version(crt
, 3);
4443 bytes
= sizeof(buffer
);
4444 if (gnutls_x509_crt_get_key_id(crt
, 0, buffer
, &bytes
) >= 0)
4445 gnutls_x509_crt_set_subject_key_id(crt
, buffer
, bytes
);
4447 gnutls_x509_crt_sign(crt
, crt
, key
);
4453 bytes
= sizeof(buffer
);
4454 if ((result
= gnutls_x509_crt_export(crt
, GNUTLS_X509_FMT_PEM
,
4455 buffer
, &bytes
)) < 0)
4456 cupsdLogMessage(CUPSD_LOG_ERROR
,
4457 "Unable to export SSL server certificate - %s",
4458 gnutls_strerror(result
));
4459 else if ((fp
= cupsFileOpen(ServerCertificate
, "w")) != NULL
)
4461 cupsFileWrite(fp
, (char *)buffer
, bytes
);
4464 cupsdLogMessage(CUPSD_LOG_INFO
,
4465 "Created SSL server certificate file \"%s\"...",
4469 cupsdLogMessage(CUPSD_LOG_ERROR
,
4470 "Unable to create SSL server certificate file \"%s\" - %s",
4471 ServerCertificate
, strerror(errno
));
4477 gnutls_x509_crt_deinit(crt
);
4478 gnutls_x509_privkey_deinit(key
);
4482 #elif defined(HAVE_CDSASSL) && defined(HAVE_WAITPID)
4483 int pid
, /* Process ID of command */
4484 status
; /* Status of command */
4485 char command
[1024], /* Command */
4486 *argv
[4], /* Command-line arguments */
4487 *envp
[MAX_ENV
+ 1], /* Environment variables */
4488 keychain
[1024], /* Keychain argument */
4489 infofile
[1024]; /* Type-in information for cert */
4490 cups_file_t
*fp
; /* Seed/info file */
4491 int infofd
; /* Info file descriptor */
4495 * Run the "certtool" command to generate a self-signed certificate...
4498 if (!cupsFileFind("certtool", getenv("PATH"), 1, command
, sizeof(command
)))
4500 cupsdLogMessage(CUPSD_LOG_ERROR
,
4501 "No SSL certificate and certtool command not found!");
4506 * Create a file with the certificate information fields...
4508 * Note: This assumes that the default questions are asked by the certtool
4512 if ((fp
= cupsTempFile2(infofile
, sizeof(infofile
))) == NULL
)
4514 cupsdLogMessage(CUPSD_LOG_ERROR
,
4515 "Unable to create certificate information file %s - %s",
4516 infofile
, strerror(errno
));
4520 cupsFilePrintf(fp
, "%s\nr\n\ny\nb\ns\ny\n%s\n\n\n\n\n%s\ny\n",
4521 con
->servername
, con
->servername
, ServerAdmin
);
4524 cupsdLogMessage(CUPSD_LOG_INFO
,
4525 "Generating SSL server key and certificate...");
4527 snprintf(keychain
, sizeof(keychain
), "k=%s", ServerCertificate
);
4529 argv
[0] = "certtool";
4534 cupsdLoadEnv(envp
, MAX_ENV
);
4536 infofd
= open(infofile
, O_RDONLY
);
4538 if (!cupsdStartProcess(command
, argv
, envp
, infofd
, -1, -1, -1, -1, 1, NULL
,
4549 while (waitpid(pid
, &status
, 0) < 0)
4556 cupsdFinishProcess(pid
, command
, sizeof(command
), NULL
);
4560 if (WIFEXITED(status
))
4561 cupsdLogMessage(CUPSD_LOG_ERROR
,
4562 "Unable to create SSL server key and certificate - "
4563 "the certtool command stopped with status %d!",
4564 WEXITSTATUS(status
));
4566 cupsdLogMessage(CUPSD_LOG_ERROR
,
4567 "Unable to create SSL server key and certificate - "
4568 "the certtool command crashed on signal %d!",
4573 cupsdLogMessage(CUPSD_LOG_INFO
,
4574 "Created SSL server certificate file \"%s\"...",
4582 #endif /* HAVE_LIBSSL && HAVE_WAITPID */
4584 #endif /* HAVE_SSL */
4588 * 'pipe_command()' - Pipe the output of a command to the remote client.
4591 static int /* O - Process ID */
4592 pipe_command(cupsd_client_t
*con
, /* I - Client connection */
4593 int infile
, /* I - Standard input for command */
4594 int *outfile
, /* O - Standard output for command */
4595 char *command
, /* I - Command to run */
4596 char *options
, /* I - Options for command */
4597 int root
) /* I - Run as root? */
4599 int i
; /* Looping var */
4600 int pid
; /* Process ID */
4601 char *commptr
, /* Command string pointer */
4602 commch
; /* Command string character */
4603 char *uriptr
; /* URI string pointer */
4604 int fds
[2]; /* Pipe FDs */
4605 int argc
; /* Number of arguments */
4606 int envc
; /* Number of environment variables */
4607 char argbuf
[10240], /* Argument buffer */
4608 *argv
[100], /* Argument strings */
4609 *envp
[MAX_ENV
+ 20]; /* Environment variables */
4610 char auth_type
[256], /* AUTH_TYPE environment variable */
4611 content_length
[1024], /* CONTENT_LENGTH environment variable */
4612 content_type
[1024], /* CONTENT_TYPE environment variable */
4613 http_cookie
[32768], /* HTTP_COOKIE environment variable */
4614 http_referer
[1024], /* HTTP_REFERER environment variable */
4615 http_user_agent
[1024], /* HTTP_USER_AGENT environment variable */
4616 lang
[1024], /* LANG environment variable */
4617 path_info
[1024], /* PATH_INFO environment variable */
4618 remote_addr
[1024], /* REMOTE_ADDR environment variable */
4619 remote_host
[1024], /* REMOTE_HOST environment variable */
4620 remote_user
[1024], /* REMOTE_USER environment variable */
4621 script_filename
[1024], /* SCRIPT_FILENAME environment variable */
4622 script_name
[1024], /* SCRIPT_NAME environment variable */
4623 server_name
[1024], /* SERVER_NAME environment variable */
4624 server_port
[1024]; /* SERVER_PORT environment variable */
4625 ipp_attribute_t
*attr
; /* attributes-natural-language attribute */
4626 void *ccache
= NULL
; /* Kerberos credentials */
4630 * Parse a copy of the options string, which is of the form:
4632 * argument+argument+argument
4633 * ?argument+argument+argument
4634 * param=value¶m=value
4635 * ?param=value¶m=value
4636 * /name?argument+argument+argument
4637 * /name?param=value¶m=value
4639 * If the string contains an "=" character after the initial name,
4640 * then we treat it as a HTTP GET form request and make a copy of
4641 * the remaining string for the environment variable.
4643 * The string is always parsed out as command-line arguments, to
4644 * be consistent with Apache...
4647 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4648 "pipe_command(con=%p(%d), infile=%d, outfile=%p, "
4649 "command=\"%s\", options=\"%s\", root=%d)",
4650 con
, con
->http
.fd
, infile
, outfile
, command
,
4651 options
? options
: "(null)", root
);
4658 if (*commptr
== ' ')
4660 strlcpy(argbuf
, commptr
, sizeof(argbuf
));
4665 if (argbuf
[0] == '/')
4668 * Found some trailing path information, set PATH_INFO...
4671 if ((commptr
= strchr(argbuf
, '?')) == NULL
)
4672 commptr
= argbuf
+ strlen(argbuf
);
4676 snprintf(path_info
, sizeof(path_info
), "PATH_INFO=%s", argbuf
);
4682 path_info
[0] = '\0';
4684 if (*commptr
== ' ')
4688 if (*commptr
== '?' && con
->operation
== HTTP_GET
&& !con
->query_string
)
4691 cupsdSetStringf(&(con
->query_string
), "QUERY_STRING=%s", commptr
);
4698 argv
[argc
++] = commptr
;
4700 for (; *commptr
&& argc
< 99; commptr
++)
4703 * Break arguments whenever we see a + or space...
4706 if (*commptr
== ' ' || *commptr
== '+')
4708 while (*commptr
== ' ' || *commptr
== '+')
4712 * If we don't have a blank string, save it as another argument...
4717 argv
[argc
] = commptr
;
4723 else if (*commptr
== '%' && isxdigit(commptr
[1] & 255) &&
4724 isxdigit(commptr
[2] & 255))
4727 * Convert the %xx notation to the individual character.
4730 if (commptr
[1] >= '0' && commptr
[1] <= '9')
4731 *commptr
= (commptr
[1] - '0') << 4;
4733 *commptr
= (tolower(commptr
[1]) - 'a' + 10) << 4;
4735 if (commptr
[2] >= '0' && commptr
[2] <= '9')
4736 *commptr
|= commptr
[2] - '0';
4738 *commptr
|= tolower(commptr
[2]) - 'a' + 10;
4740 _cups_strcpy(commptr
+ 1, commptr
+ 3);
4743 * Check for a %00 and break if that is the case...
4755 * Setup the environment variables as needed...
4758 if (con
->username
[0])
4760 snprintf(auth_type
, sizeof(auth_type
), "AUTH_TYPE=%s",
4761 httpGetField(HTTP(con
), HTTP_FIELD_AUTHORIZATION
));
4763 if ((uriptr
= strchr(auth_type
+ 10, ' ')) != NULL
)
4767 auth_type
[0] = '\0';
4770 (attr
= ippFindAttribute(con
->request
, "attributes-natural-language",
4771 IPP_TAG_LANGUAGE
)) != NULL
)
4773 switch (strlen(attr
->values
[0].string
.text
))
4777 * This is an unknown or badly formatted language code; use
4778 * the POSIX locale...
4781 strcpy(lang
, "LANG=C");
4786 * Just the language code (ll)...
4789 snprintf(lang
, sizeof(lang
), "LANG=%s.UTF8",
4790 attr
->values
[0].string
.text
);
4795 * Language and country code (ll-cc)...
4798 snprintf(lang
, sizeof(lang
), "LANG=%c%c_%c%c.UTF8",
4799 attr
->values
[0].string
.text
[0],
4800 attr
->values
[0].string
.text
[1],
4801 toupper(attr
->values
[0].string
.text
[3] & 255),
4802 toupper(attr
->values
[0].string
.text
[4] & 255));
4806 else if (con
->language
)
4807 snprintf(lang
, sizeof(lang
), "LANG=%s.UTF8", con
->language
->language
);
4809 strcpy(lang
, "LANG=C");
4811 strcpy(remote_addr
, "REMOTE_ADDR=");
4812 httpAddrString(con
->http
.hostaddr
, remote_addr
+ 12,
4813 sizeof(remote_addr
) - 12);
4815 snprintf(remote_host
, sizeof(remote_host
), "REMOTE_HOST=%s",
4816 con
->http
.hostname
);
4818 snprintf(script_name
, sizeof(script_name
), "SCRIPT_NAME=%s", con
->uri
);
4819 if ((uriptr
= strchr(script_name
, '?')) != NULL
)
4822 snprintf(script_filename
, sizeof(script_filename
), "SCRIPT_FILENAME=%s%s",
4823 DocumentRoot
, script_name
+ 12);
4825 sprintf(server_port
, "SERVER_PORT=%d", con
->serverport
);
4827 snprintf(server_name
, sizeof(server_name
), "SERVER_NAME=%s",
4830 envc
= cupsdLoadEnv(envp
, (int)(sizeof(envp
) / sizeof(envp
[0])));
4833 envp
[envc
++] = auth_type
;
4835 envp
[envc
++] = lang
;
4836 envp
[envc
++] = "REDIRECT_STATUS=1";
4837 envp
[envc
++] = "GATEWAY_INTERFACE=CGI/1.1";
4838 envp
[envc
++] = server_name
;
4839 envp
[envc
++] = server_port
;
4840 envp
[envc
++] = remote_addr
;
4841 envp
[envc
++] = remote_host
;
4842 envp
[envc
++] = script_name
;
4843 envp
[envc
++] = script_filename
;
4846 envp
[envc
++] = path_info
;
4848 if (con
->username
[0])
4850 snprintf(remote_user
, sizeof(remote_user
), "REMOTE_USER=%s", con
->username
);
4852 envp
[envc
++] = remote_user
;
4855 * Save Kerberos credentials, if any...
4860 ccache
= cupsdCopyKrb5Creds(con
);
4861 #endif /* HAVE_GSSAPI */
4864 if (con
->http
.version
== HTTP_1_1
)
4865 envp
[envc
++] = "SERVER_PROTOCOL=HTTP/1.1";
4866 else if (con
->http
.version
== HTTP_1_0
)
4867 envp
[envc
++] = "SERVER_PROTOCOL=HTTP/1.0";
4869 envp
[envc
++] = "SERVER_PROTOCOL=HTTP/0.9";
4871 if (con
->http
.cookie
)
4873 snprintf(http_cookie
, sizeof(http_cookie
), "HTTP_COOKIE=%s",
4875 envp
[envc
++] = http_cookie
;
4878 if (con
->http
.fields
[HTTP_FIELD_USER_AGENT
][0])
4880 snprintf(http_user_agent
, sizeof(http_user_agent
), "HTTP_USER_AGENT=%s",
4881 con
->http
.fields
[HTTP_FIELD_USER_AGENT
]);
4882 envp
[envc
++] = http_user_agent
;
4885 if (con
->http
.fields
[HTTP_FIELD_REFERER
][0])
4887 snprintf(http_referer
, sizeof(http_referer
), "HTTP_REFERER=%s",
4888 con
->http
.fields
[HTTP_FIELD_REFERER
]);
4889 envp
[envc
++] = http_referer
;
4892 if (con
->operation
== HTTP_GET
)
4894 envp
[envc
++] = "REQUEST_METHOD=GET";
4896 if (con
->query_string
)
4899 * Add GET form variables after ?...
4902 envp
[envc
++] = con
->query_string
;
4905 envp
[envc
++] = "QUERY_STRING=";
4909 sprintf(content_length
, "CONTENT_LENGTH=" CUPS_LLFMT
,
4910 CUPS_LLCAST con
->bytes
);
4911 snprintf(content_type
, sizeof(content_type
), "CONTENT_TYPE=%s",
4912 con
->http
.fields
[HTTP_FIELD_CONTENT_TYPE
]);
4914 envp
[envc
++] = "REQUEST_METHOD=POST";
4915 envp
[envc
++] = content_length
;
4916 envp
[envc
++] = content_type
;
4920 * Tell the CGI if we are using encryption...
4924 envp
[envc
++] = "HTTPS=ON";
4927 * Terminate the environment array...
4932 if (LogLevel
>= CUPSD_LOG_DEBUG
)
4934 for (i
= 0; i
< argc
; i
++)
4935 cupsdLogMessage(CUPSD_LOG_DEBUG
,
4936 "[CGI] argv[%d] = \"%s\"", i
, argv
[i
]);
4937 for (i
= 0; i
< envc
; i
++)
4938 cupsdLogMessage(CUPSD_LOG_DEBUG
,
4939 "[CGI] envp[%d] = \"%s\"", i
, envp
[i
]);
4943 * Create a pipe for the output...
4946 if (cupsdOpenPipe(fds
))
4948 cupsdLogMessage(CUPSD_LOG_ERROR
, "[CGI] Unable to create pipe for %s - %s",
4949 argv
[0], strerror(errno
));
4954 * Then execute the command...
4957 if (cupsdStartProcess(command
, argv
, envp
, infile
, fds
[1], CGIPipes
[1],
4958 -1, -1, root
, DefaultProfile
, NULL
, &pid
) < 0)
4961 * Error - can't fork!
4964 cupsdLogMessage(CUPSD_LOG_ERROR
, "[CGI] Unable to start %s - %s", argv
[0],
4967 cupsdClosePipe(fds
);
4973 * Fork successful - return the PID...
4976 if (con
->username
[0])
4977 cupsdAddCert(pid
, con
->username
, ccache
);
4979 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[CGI] Started %s (PID %d)", command
, pid
);
4990 * 'valid_host()' - Is the Host: field valid?
4993 static int /* O - 1 if valid, 0 if not */
4994 valid_host(cupsd_client_t
*con
) /* I - Client connection */
4996 cupsd_alias_t
*a
; /* Current alias */
4997 cupsd_netif_t
*netif
; /* Current network interface */
4998 const char *host
, /* Host field */
4999 *end
; /* End character */
5002 host
= con
->http
.fields
[HTTP_FIELD_HOST
];
5004 if (httpAddrLocalhost(con
->http
.hostaddr
))
5007 * Only allow "localhost" or the equivalent IPv4 or IPv6 numerical
5008 * addresses when accessing CUPS via the loopback interface...
5011 return (!strcasecmp(host
, "localhost") ||
5012 !strncasecmp(host
, "localhost:", 10) ||
5013 !strcasecmp(host
, "localhost.") ||
5014 !strncasecmp(host
, "localhost.:", 11) ||
5016 !strcasecmp(host
, "localhost.localdomain") ||
5017 !strncasecmp(host
, "localhost.localdomain:", 22) ||
5018 #endif /* __linux */
5019 !strcmp(host
, "127.0.0.1") ||
5020 !strncmp(host
, "127.0.0.1:", 10) ||
5021 !strcmp(host
, "[::1]") ||
5022 !strncmp(host
, "[::1]:", 6));
5027 * Check if the hostname is something.local (Bonjour); if so, allow it.
5030 if ((end
= strrchr(host
, '.')) != NULL
&&
5031 (!strcasecmp(end
, ".local") || !strncasecmp(end
, ".local:", 7) ||
5032 !strcasecmp(end
, ".local.") || !strncasecmp(end
, ".local.:", 8)))
5034 #endif /* HAVE_DNSSD */
5037 * Check if the hostname is an IP address...
5040 if (isdigit(*host
& 255) || *host
== '[')
5043 * Possible IPv4/IPv6 address...
5046 char temp
[1024], /* Temporary string */
5047 *ptr
; /* Pointer into temporary string */
5048 http_addrlist_t
*addrlist
; /* List of addresses */
5051 strlcpy(temp
, host
, sizeof(temp
));
5052 if ((ptr
= strrchr(temp
, ':')) != NULL
&& !strchr(ptr
, ']'))
5053 *ptr
= '\0'; /* Strip :port from host value */
5055 if ((addrlist
= httpAddrGetList(temp
, AF_UNSPEC
, NULL
)) != NULL
)
5058 * Good IPv4/IPv6 address...
5061 httpAddrFreeList(addrlist
);
5067 * Check for (alias) name matches...
5070 for (a
= (cupsd_alias_t
*)cupsArrayFirst(ServerAlias
);
5072 a
= (cupsd_alias_t
*)cupsArrayNext(ServerAlias
))
5075 * "ServerAlias *" allows all host values through...
5078 if (!strcmp(a
->name
, "*"))
5081 if (!strncasecmp(host
, a
->name
, a
->namelen
))
5084 * Prefix matches; check the character at the end - it must be ":", ".",
5088 end
= host
+ a
->namelen
;
5090 if (!*end
|| *end
== ':' || (*end
== '.' && (!end
[1] || end
[1] == ':')))
5096 for (a
= (cupsd_alias_t
*)cupsArrayFirst(DNSSDAlias
);
5098 a
= (cupsd_alias_t
*)cupsArrayNext(DNSSDAlias
))
5101 * "ServerAlias *" allows all host values through...
5104 if (!strcmp(a
->name
, "*"))
5107 if (!strncasecmp(host
, a
->name
, a
->namelen
))
5110 * Prefix matches; check the character at the end - it must be ":", ".",
5114 end
= host
+ a
->namelen
;
5116 if (!*end
|| *end
== ':' || (*end
== '.' && (!end
[1] || end
[1] == ':')))
5120 #endif /* HAVE_DNSSD */
5123 * Check for interface hostname matches...
5126 for (netif
= (cupsd_netif_t
*)cupsArrayFirst(NetIFList
);
5128 netif
= (cupsd_netif_t
*)cupsArrayNext(NetIFList
))
5130 if (!strncasecmp(host
, netif
->hostname
, netif
->hostlen
))
5133 * Prefix matches; check the character at the end - it must be ":", ".",
5137 end
= host
+ netif
->hostlen
;
5139 if (!*end
|| *end
== ':' || (*end
== '.' && (!end
[1] || end
[1] == ':')))
5149 * 'write_file()' - Send a file via HTTP.
5152 static int /* O - 0 on failure, 1 on success */
5153 write_file(cupsd_client_t
*con
, /* I - Client connection */
5154 http_status_t code
, /* I - HTTP status */
5155 char *filename
, /* I - Filename */
5156 char *type
, /* I - File type */
5157 struct stat
*filestats
) /* O - File information */
5159 con
->file
= open(filename
, O_RDONLY
);
5161 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
5162 "write_file(con=%p(%d), code=%d, filename=\"%s\" (%d), "
5163 "type=\"%s\", filestats=%p)", con
, con
->http
.fd
,
5164 code
, filename
, con
->file
, type
? type
: "(null)", filestats
);
5169 fcntl(con
->file
, F_SETFD
, fcntl(con
->file
, F_GETFD
) | FD_CLOEXEC
);
5173 if (!cupsdSendHeader(con
, code
, type
, CUPSD_AUTH_NONE
))
5176 if (httpPrintf(HTTP(con
), "Last-Modified: %s\r\n",
5177 httpGetDateString(filestats
->st_mtime
)) < 0)
5179 if (httpPrintf(HTTP(con
), "Content-Length: " CUPS_LLFMT
"\r\n",
5180 CUPS_LLCAST filestats
->st_size
) < 0)
5182 if (httpPrintf(HTTP(con
), "\r\n") < 0)
5185 if (cupsdFlushHeader(con
) < 0)
5188 con
->http
.data_encoding
= HTTP_ENCODE_LENGTH
;
5189 con
->http
.data_remaining
= filestats
->st_size
;
5191 if (con
->http
.data_remaining
<= INT_MAX
)
5192 con
->http
._data_remaining
= con
->http
.data_remaining
;
5194 con
->http
._data_remaining
= INT_MAX
;
5196 cupsdAddSelect(con
->http
.fd
, (cupsd_selfunc_t
)cupsdReadClient
,
5197 (cupsd_selfunc_t
)cupsdWriteClient
, con
);
5204 * 'write_pipe()' - Flag that data is available on the CGI pipe.
5208 write_pipe(cupsd_client_t
*con
) /* I - Client connection */
5210 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
5211 "write_pipe(con=%p(%d)) CGI output on fd %d",
5212 con
, con
->http
.fd
, con
->file
);
5214 con
->file_ready
= 1;
5216 cupsdRemoveSelect(con
->file
);
5217 cupsdAddSelect(con
->http
.fd
, NULL
, (cupsd_selfunc_t
)cupsdWriteClient
, con
);
5222 * End of "$Id: client.c 7950 2008-09-17 00:21:59Z mike $".