2 * "$Id: client.c 7950 2008-09-17 00:21:59Z mike $"
4 * Client routines for the CUPS scheduler.
6 * Copyright 2007-2011 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 * copy_cdsa_certificate() - Copy a SSL/TLS certificate from the System
35 * data_ready() - Check whether data is available from a client.
36 * encrypt_client() - Enable encryption for the client...
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
45 * valid_host() - Is the Host: field valid?
46 * write_file() - Send a file via HTTP.
47 * write_pipe() - Flag that data is available on the CGI pipe.
51 * Include necessary headers...
58 #endif /* HAVE_TCPD_H */
65 static const char * const http_states
[] =
66 { /* HTTP state strings */
82 static const char * const ipp_states
[] =
83 { /* IPP state strings */
95 static int check_if_modified(cupsd_client_t
*con
,
96 struct stat
*filestats
);
97 static int compare_clients(cupsd_client_t
*a
, cupsd_client_t
*b
,
100 static CFArrayRef
copy_cdsa_certificate(cupsd_client_t
*con
);
101 #endif /* HAVE_CDSASSL */
102 static int data_ready(cupsd_client_t
*con
);
104 static int encrypt_client(cupsd_client_t
*con
);
105 #endif /* HAVE_SSL */
106 static char *get_file(cupsd_client_t
*con
, struct stat
*filestats
,
107 char *filename
, int len
);
108 static http_status_t
install_conf_file(cupsd_client_t
*con
);
109 static int is_cgi(cupsd_client_t
*con
, const char *filename
,
110 struct stat
*filestats
, mime_type_t
*type
);
111 static int is_path_absolute(const char *path
);
113 static int make_certificate(cupsd_client_t
*con
);
114 #endif /* HAVE_SSL */
115 static int pipe_command(cupsd_client_t
*con
, int infile
, int *outfile
,
116 char *command
, char *options
, int root
);
117 static int valid_host(cupsd_client_t
*con
);
118 static int write_file(cupsd_client_t
*con
, http_status_t code
,
119 char *filename
, char *type
,
120 struct stat
*filestats
);
121 static void write_pipe(cupsd_client_t
*con
);
125 * 'cupsdAcceptClient()' - Accept a new client.
129 cupsdAcceptClient(cupsd_listener_t
*lis
)/* I - Listener socket */
131 int count
; /* Count of connections on a host */
132 int val
; /* Parameter value */
133 cupsd_client_t
*con
, /* New client pointer */
134 *tempcon
; /* Temporary client pointer */
135 http_addrlist_t
*addrlist
, /* List of adddresses for host */
136 *addr
; /* Current address */
137 socklen_t addrlen
; /* Length of address */
138 char *hostname
; /* Hostname for address */
139 http_addr_t temp
; /* Temporary address variable */
140 static time_t last_dos
= 0; /* Time of last DoS attack */
142 struct request_info wrap_req
; /* TCP wrappers request information */
143 #endif /* HAVE_TCPD_H */
146 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
147 "cupsdAcceptClient(lis=%p(%d)) Clients=%d",
148 lis
, lis
->fd
, cupsArrayCount(Clients
));
151 * Make sure we don't have a full set of clients already...
154 if (cupsArrayCount(Clients
) == MaxClients
)
158 * Get a pointer to the next available client...
162 Clients
= cupsArrayNew(NULL
, NULL
);
166 cupsdLogMessage(CUPSD_LOG_ERROR
,
167 "Unable to allocate memory for clients array!");
168 cupsdPauseListening();
173 ActiveClients
= cupsArrayNew((cups_array_func_t
)compare_clients
, NULL
);
177 cupsdLogMessage(CUPSD_LOG_ERROR
,
178 "Unable to allocate memory for active clients array!");
179 cupsdPauseListening();
183 if ((con
= calloc(1, sizeof(cupsd_client_t
))) == NULL
)
185 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to allocate memory for client!");
186 cupsdPauseListening();
190 con
->http
.activity
= time(NULL
);
192 con
->http
.hostaddr
= &(con
->clientaddr
);
193 con
->http
.wait_value
= 10000;
196 * Accept the client and get the remote address...
199 addrlen
= sizeof(http_addr_t
);
201 if ((con
->http
.fd
= accept(lis
->fd
, (struct sockaddr
*)con
->http
.hostaddr
,
204 if (errno
== ENFILE
|| errno
== EMFILE
)
205 cupsdPauseListening();
207 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to accept client connection - %s.",
215 * Save the connected port number...
218 _httpAddrSetPort(con
->http
.hostaddr
, _httpAddrPort(&(lis
->address
)));
222 * Convert IPv4 over IPv6 addresses (::ffff:n.n.n.n) to IPv4 forms we
223 * can more easily use...
226 if (lis
->address
.addr
.sa_family
== AF_INET6
&&
227 con
->http
.hostaddr
->ipv6
.sin6_addr
.s6_addr32
[0] == 0 &&
228 con
->http
.hostaddr
->ipv6
.sin6_addr
.s6_addr32
[1] == 0 &&
229 ntohl(con
->http
.hostaddr
->ipv6
.sin6_addr
.s6_addr32
[2]) == 0xffff)
230 con
->http
.hostaddr
->ipv6
.sin6_addr
.s6_addr32
[2] = 0;
231 #endif /* AF_INET6 */
234 * Check the number of clients on the same address...
237 for (count
= 0, tempcon
= (cupsd_client_t
*)cupsArrayFirst(Clients
);
239 tempcon
= (cupsd_client_t
*)cupsArrayNext(Clients
))
240 if (httpAddrEqual(tempcon
->http
.hostaddr
, con
->http
.hostaddr
))
243 if (count
>= MaxClientsPerHost
)
247 if (count
>= MaxClientsPerHost
)
249 if ((time(NULL
) - last_dos
) >= 60)
251 last_dos
= time(NULL
);
252 cupsdLogMessage(CUPSD_LOG_WARN
,
253 "Possible DoS attack - more than %d clients connecting "
256 httpAddrString(con
->http
.hostaddr
, con
->http
.hostname
,
257 sizeof(con
->http
.hostname
)));
261 closesocket(con
->http
.fd
);
271 * Get the hostname or format the IP address as needed...
274 if (httpAddrLocalhost(con
->http
.hostaddr
))
277 * Map accesses from the loopback interface to "localhost"...
280 strlcpy(con
->http
.hostname
, "localhost", sizeof(con
->http
.hostname
));
281 hostname
= con
->http
.hostname
;
286 * Map accesses from the same host to the server name.
290 hostname
= httpAddrLookup(con
->http
.hostaddr
, con
->http
.hostname
,
291 sizeof(con
->http
.hostname
));
295 httpAddrString(con
->http
.hostaddr
, con
->http
.hostname
,
296 sizeof(con
->http
.hostname
));
300 if (hostname
== NULL
&& HostNameLookups
== 2)
303 * Can't have an unresolved IP address with double-lookups enabled...
307 closesocket(con
->http
.fd
);
312 cupsdLogMessage(CUPSD_LOG_WARN
,
313 "Name lookup failed - connection from %s closed!",
320 if (HostNameLookups
== 2)
323 * Do double lookups as needed...
326 if ((addrlist
= httpAddrGetList(con
->http
.hostname
, AF_UNSPEC
, NULL
))
330 * See if the hostname maps to the same IP address...
333 for (addr
= addrlist
; addr
; addr
= addr
->next
)
334 if (httpAddrEqual(con
->http
.hostaddr
, &(addr
->addr
)))
340 httpAddrFreeList(addrlist
);
345 * Can't have a hostname that doesn't resolve to the same IP address
346 * with double-lookups enabled...
350 closesocket(con
->http
.fd
);
355 cupsdLogMessage(CUPSD_LOG_WARN
,
356 "IP lookup failed - connection from %s closed!",
365 * See if the connection is denied by TCP wrappers...
368 request_init(&wrap_req
, RQ_DAEMON
, "cupsd", RQ_FILE
, con
->http
.fd
, NULL
);
371 if (!hosts_access(&wrap_req
))
374 closesocket(con
->http
.fd
);
379 cupsdLogMessage(CUPSD_LOG_WARN
,
380 "Connection from %s refused by /etc/hosts.allow and "
381 "/etc/hosts.deny rules.", con
->http
.hostname
);
385 #endif /* HAVE_TCPD_H */
388 if (con
->http
.hostaddr
->addr
.sa_family
== AF_LOCAL
)
389 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Client %d] Accepted from %s (Domain)",
390 con
->http
.fd
, con
->http
.hostname
);
392 #endif /* AF_LOCAL */
393 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Client %d] Accepted from %s:%d (IPv%d)",
394 con
->http
.fd
, con
->http
.hostname
,
395 _httpAddrPort(con
->http
.hostaddr
),
396 _httpAddrFamily(con
->http
.hostaddr
) == AF_INET
? 4 : 6);
399 * Get the local address the client connected to...
402 addrlen
= sizeof(temp
);
403 if (getsockname(con
->http
.fd
, (struct sockaddr
*)&temp
, &addrlen
))
405 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to get local address - %s",
408 strcpy(con
->servername
, "localhost");
409 con
->serverport
= LocalPort
;
412 else if (_httpAddrFamily(&temp
) == AF_LOCAL
)
414 strcpy(con
->servername
, "localhost");
415 con
->serverport
= LocalPort
;
417 #endif /* AF_LOCAL */
420 if (httpAddrLocalhost(&temp
))
421 strlcpy(con
->servername
, "localhost", sizeof(con
->servername
));
422 else if (HostNameLookups
|| RemotePort
)
423 httpAddrLookup(&temp
, con
->servername
, sizeof(con
->servername
));
425 httpAddrString(&temp
, con
->servername
, sizeof(con
->servername
));
427 con
->serverport
= _httpAddrPort(&(lis
->address
));
430 cupsArrayAdd(Clients
, con
);
433 * Using TCP_NODELAY improves responsiveness, especially on systems with a slow
434 * loopback interface. Since we write large buffers when sending print files
435 * and requests there shouldn't be any performance penalty for this...
439 setsockopt(con
->http
.fd
, IPPROTO_TCP
, TCP_NODELAY
, &val
, sizeof(val
));
442 * Close this file on all execs...
445 fcntl(con
->http
.fd
, F_SETFD
, fcntl(con
->http
.fd
, F_GETFD
) | FD_CLOEXEC
);
448 * Add the socket to the server select.
451 cupsdAddSelect(con
->http
.fd
, (cupsd_selfunc_t
)cupsdReadClient
, NULL
, con
);
454 * Temporarily suspend accept()'s until we lose a client...
457 if (cupsArrayCount(Clients
) == MaxClients
)
458 cupsdPauseListening();
462 * See if we are connecting on a secure port...
465 if (lis
->encryption
== HTTP_ENCRYPT_ALWAYS
)
468 * https connection; go secure...
471 con
->http
.encryption
= HTTP_ENCRYPT_ALWAYS
;
473 if (!encrypt_client(con
))
474 cupsdCloseClient(con
);
478 #endif /* HAVE_SSL */
483 * 'cupsdCloseAllClients()' - Close all remote clients immediately.
487 cupsdCloseAllClients(void)
489 cupsd_client_t
*con
; /* Current client */
492 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdCloseAllClients() Clients=%d",
493 cupsArrayCount(Clients
));
495 for (con
= (cupsd_client_t
*)cupsArrayFirst(Clients
);
497 con
= (cupsd_client_t
*)cupsArrayNext(Clients
))
498 if (cupsdCloseClient(con
))
499 cupsdCloseClient(con
);
504 * 'cupsdCloseClient()' - Close a remote client.
507 int /* O - 1 if partial close, 0 if fully closed */
508 cupsdCloseClient(cupsd_client_t
*con
) /* I - Client to close */
510 int partial
; /* Do partial close for SSL? */
512 SSL_CTX
*context
; /* Context for encryption */
513 unsigned long error
; /* Error code */
514 #elif defined(HAVE_GNUTLS)
515 int error
; /* Error code */
516 gnutls_certificate_server_credentials
*credentials
;
517 /* TLS credentials */
518 # elif defined(HAVE_CDSASSL)
519 #endif /* HAVE_LIBSSL */
522 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Client %d] Closing connection.",
526 * Flush pending writes before closing...
529 httpFlushWrite(HTTP(con
));
535 * Shutdown encryption as needed...
543 context
= SSL_get_SSL_CTX(con
->http
.tls
);
545 switch (SSL_shutdown(con
->http
.tls
))
548 cupsdLogMessage(CUPSD_LOG_DEBUG
,
549 "SSL shutdown successful!");
552 cupsdLogMessage(CUPSD_LOG_ERROR
,
553 "Fatal error during SSL shutdown!");
555 while ((error
= ERR_get_error()) != 0)
556 cupsdLogMessage(CUPSD_LOG_ERROR
, "SSL shutdown failed: %s",
557 ERR_error_string(error
, NULL
));
561 SSL_CTX_free(context
);
562 SSL_free(con
->http
.tls
);
564 # elif defined(HAVE_GNUTLS)
565 credentials
= (gnutls_certificate_server_credentials
*)(con
->http
.tls_credentials
);
567 error
= gnutls_bye(con
->http
.tls
, GNUTLS_SHUT_WR
);
570 case GNUTLS_E_SUCCESS
:
571 cupsdLogMessage(CUPSD_LOG_DEBUG
,
572 "SSL shutdown successful!");
575 cupsdLogMessage(CUPSD_LOG_ERROR
,
576 "SSL shutdown failed: %s", gnutls_strerror(error
));
580 gnutls_deinit(con
->http
.tls
);
581 gnutls_certificate_free_credentials(*credentials
);
584 # elif defined(HAVE_CDSASSL)
585 while (SSLClose(con
->http
.tls
) == errSSLWouldBlock
)
588 SSLDisposeContext(con
->http
.tls
);
590 if (con
->http
.tls_credentials
)
591 CFRelease(con
->http
.tls_credentials
);
593 # endif /* HAVE_LIBSSL */
595 con
->http
.tls
= NULL
;
597 #endif /* HAVE_SSL */
599 if (con
->pipe_pid
!= 0)
602 * Stop any CGI process...
605 cupsdEndProcess(con
->pipe_pid
, 1);
611 cupsdRemoveSelect(con
->file
);
618 * Close the socket and clear the file from the input set for select()...
621 if (con
->http
.fd
>= 0)
623 cupsArrayRemove(ActiveClients
, con
);
629 * Only do a partial close so that the encrypted client gets everything.
632 shutdown(con
->http
.fd
, 0);
633 cupsdAddSelect(con
->http
.fd
, (cupsd_selfunc_t
)cupsdReadClient
, NULL
, con
);
638 * Shut the socket down fully...
641 cupsdRemoveSelect(con
->http
.fd
);
653 if (con
->http
.input_set
)
654 free(con
->http
.input_set
);
656 httpClearCookie(HTTP(con
));
657 httpClearFields(HTTP(con
));
659 cupsdClearString(&con
->filename
);
660 cupsdClearString(&con
->command
);
661 cupsdClearString(&con
->options
);
662 cupsdClearString(&con
->query_string
);
666 ippDelete(con
->request
);
672 ippDelete(con
->response
);
673 con
->response
= NULL
;
678 cupsLangFree(con
->language
);
679 con
->language
= NULL
;
682 #ifdef HAVE_AUTHORIZATION_H
685 AuthorizationFree(con
->authref
, kAuthorizationFlagDefaults
);
688 #endif /* HAVE_AUTHORIZATION_H */
691 * Re-enable new client connections if we are going back under the
695 if (cupsArrayCount(Clients
) == MaxClients
)
696 cupsdResumeListening();
699 * Compact the list of clients as necessary...
702 cupsArrayRemove(Clients
, con
);
712 * 'cupsdFlushHeader()' - Flush the header fields to the client.
715 int /* I - Bytes written or -1 on error */
716 cupsdFlushHeader(cupsd_client_t
*con
) /* I - Client to flush to */
718 int bytes
= httpFlushWrite(HTTP(con
));
720 con
->http
.data_encoding
= HTTP_ENCODE_LENGTH
;
727 * 'cupsdReadClient()' - Read data from a client.
731 cupsdReadClient(cupsd_client_t
*con
) /* I - Client to read from */
733 char line
[32768], /* Line from client... */
734 operation
[64], /* Operation code from socket */
735 version
[64], /* HTTP version number string */
736 locale
[64], /* Locale */
737 *ptr
; /* Pointer into strings */
738 int major
, minor
; /* HTTP version numbers */
739 http_status_t status
; /* Transfer status */
740 ipp_state_t ipp_state
; /* State of IPP transfer */
741 int bytes
; /* Number of bytes to POST */
742 char *filename
; /* Name of file for GET/HEAD */
743 char buf
[1024]; /* Buffer for real filename */
744 struct stat filestats
; /* File information */
745 mime_type_t
*type
; /* MIME type of file */
746 cupsd_printer_t
*p
; /* Printer */
747 static unsigned request_id
= 0; /* Request ID for temp files */
750 status
= HTTP_CONTINUE
;
752 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
753 "[Client %d] cupsdReadClient "
757 "data_encoding=HTTP_ENCODE_%s, "
758 "data_remaining=" CUPS_LLFMT
", "
761 con
->http
.fd
, con
->http
.error
, con
->http
.used
,
762 http_states
[con
->http
.state
],
763 con
->http
.data_encoding
== HTTP_ENCODE_CHUNKED
?
764 "CHUNKED" : "LENGTH",
765 CUPS_LLCAST con
->http
.data_remaining
,
767 con
->request
? ipp_states
[con
->request
->state
] : "",
774 * Automatically check for a SSL/TLS handshake...
779 if (recv(con
->http
.fd
, buf
, 1, MSG_PEEK
) == 1 &&
780 (!buf
[0] || !strchr("DGHOPT", buf
[0])))
783 * Encrypt this connection...
786 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
787 "[Client %d] Saw first byte %02X, auto-negotiating "
788 "SSL/TLS session.", con
->http
.fd
, buf
[0] & 255);
790 if (!encrypt_client(con
))
791 cupsdCloseClient(con
);
796 #endif /* HAVE_SSL */
798 switch (con
->http
.state
)
802 * See if we've received a request line...
805 if (httpGets(line
, sizeof(line
) - 1, HTTP(con
)) == NULL
)
807 if (con
->http
.error
&& con
->http
.error
!= EPIPE
)
808 cupsdLogMessage(CUPSD_LOG_DEBUG
,
809 "[Client %d] HTTP_WAITING Closing for error %d "
810 "(%s)", con
->http
.fd
, con
->http
.error
,
811 strerror(con
->http
.error
));
813 cupsdLogMessage(CUPSD_LOG_DEBUG
,
814 "[Client %d] HTTP_WAITING Closing on EOF",
817 cupsdCloseClient(con
);
822 * Ignore blank request lines...
829 * Clear other state variables...
832 httpClearFields(HTTP(con
));
834 con
->http
.activity
= time(NULL
);
835 con
->http
.version
= HTTP_1_0
;
836 con
->http
.keep_alive
= HTTP_KEEPALIVE_OFF
;
837 con
->http
.data_encoding
= HTTP_ENCODE_LENGTH
;
838 con
->http
.data_remaining
= 0;
839 con
->http
._data_remaining
= 0;
840 con
->operation
= HTTP_WAITING
;
845 con
->username
[0] = '\0';
846 con
->password
[0] = '\0';
849 cupsdClearString(&con
->command
);
850 cupsdClearString(&con
->options
);
851 cupsdClearString(&con
->query_string
);
855 ippDelete(con
->request
);
861 ippDelete(con
->response
);
862 con
->response
= NULL
;
867 cupsLangFree(con
->language
);
868 con
->language
= NULL
;
874 #endif /* HAVE_GSSAPI */
877 * Grab the request line...
880 switch (sscanf(line
, "%63s%1023s%63s", operation
, con
->uri
, version
))
885 cupsdLogMessage(CUPSD_LOG_ERROR
,
886 "[Client %d] Bad request line \"%s\" from %s.",
888 _httpEncodeURI(buf
, line
, sizeof(buf
)),
890 cupsdSendError(con
, HTTP_BAD_REQUEST
, CUPSD_AUTH_NONE
);
891 cupsdCloseClient(con
);
895 con
->http
.version
= HTTP_0_9
;
898 if (sscanf(version
, "HTTP/%d.%d", &major
, &minor
) != 2)
900 cupsdLogMessage(CUPSD_LOG_ERROR
,
901 "[Client %d] Bad request line \"%s\" from %s.",
903 _httpEncodeURI(buf
, line
, sizeof(buf
)),
905 cupsdSendError(con
, HTTP_BAD_REQUEST
, CUPSD_AUTH_NONE
);
906 cupsdCloseClient(con
);
912 con
->http
.version
= (http_version_t
)(major
* 100 + minor
);
913 if (con
->http
.version
== HTTP_1_1
&& KeepAlive
)
914 con
->http
.keep_alive
= HTTP_KEEPALIVE_ON
;
916 con
->http
.keep_alive
= HTTP_KEEPALIVE_OFF
;
920 cupsdLogMessage(CUPSD_LOG_ERROR
,
921 "[Client %d] Unsupported request line \"%s\" "
922 "from %s.", con
->http
.fd
,
923 _httpEncodeURI(buf
, line
, sizeof(buf
)),
925 cupsdSendError(con
, HTTP_NOT_SUPPORTED
, CUPSD_AUTH_NONE
);
926 cupsdCloseClient(con
);
933 * Handle full URLs in the request line...
936 if (strcmp(con
->uri
, "*"))
938 char scheme
[HTTP_MAX_URI
], /* Method/scheme */
939 userpass
[HTTP_MAX_URI
], /* Username:password */
940 hostname
[HTTP_MAX_URI
], /* Hostname */
941 resource
[HTTP_MAX_URI
]; /* Resource path */
942 int port
; /* Port number */
946 * Separate the URI into its components...
949 httpSeparateURI(HTTP_URI_CODING_MOST
, con
->uri
,
950 scheme
, sizeof(scheme
),
951 userpass
, sizeof(userpass
),
952 hostname
, sizeof(hostname
), &port
,
953 resource
, sizeof(resource
));
956 * Only allow URIs with the servername, localhost, or an IP
960 if (strcmp(scheme
, "file") &&
961 _cups_strcasecmp(hostname
, ServerName
) &&
962 _cups_strcasecmp(hostname
, "localhost") &&
963 !isdigit(hostname
[0]) && hostname
[0] != '[')
966 * Nope, we don't do proxies...
969 cupsdLogMessage(CUPSD_LOG_ERROR
,
970 "[Client %d] Bad URI \"%s\" in request.",
971 con
->http
.fd
, con
->uri
);
972 cupsdSendError(con
, HTTP_METHOD_NOT_ALLOWED
, CUPSD_AUTH_NONE
);
973 cupsdCloseClient(con
);
978 * Copy the resource portion back into the URI; both resource and
979 * con->uri are HTTP_MAX_URI bytes in size...
982 strcpy(con
->uri
, resource
);
986 * Process the request...
989 if (!strcmp(operation
, "GET"))
990 con
->http
.state
= HTTP_GET
;
991 else if (!strcmp(operation
, "PUT"))
992 con
->http
.state
= HTTP_PUT
;
993 else if (!strcmp(operation
, "POST"))
994 con
->http
.state
= HTTP_POST
;
995 else if (!strcmp(operation
, "DELETE"))
996 con
->http
.state
= HTTP_DELETE
;
997 else if (!strcmp(operation
, "TRACE"))
998 con
->http
.state
= HTTP_TRACE
;
999 else if (!strcmp(operation
, "OPTIONS"))
1000 con
->http
.state
= HTTP_OPTIONS
;
1001 else if (!strcmp(operation
, "HEAD"))
1002 con
->http
.state
= HTTP_HEAD
;
1005 cupsdLogMessage(CUPSD_LOG_ERROR
,
1006 "[Client %d] Bad operation \"%s\".", con
->http
.fd
,
1008 cupsdSendError(con
, HTTP_BAD_REQUEST
, CUPSD_AUTH_NONE
);
1009 cupsdCloseClient(con
);
1013 gettimeofday(&(con
->start
), NULL
);
1014 con
->operation
= con
->http
.state
;
1016 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Client %d] %s %s HTTP/%d.%d",
1017 con
->http
.fd
, operation
, con
->uri
,
1018 con
->http
.version
/ 100, con
->http
.version
% 100);
1020 con
->http
.status
= HTTP_OK
;
1022 if (!cupsArrayFind(ActiveClients
, con
))
1024 cupsArrayAdd(ActiveClients
, con
);
1025 cupsdSetBusyState();
1036 * Parse incoming parameters until the status changes...
1039 while ((status
= httpUpdate(HTTP(con
))) == HTTP_CONTINUE
)
1040 if (!data_ready(con
))
1043 if (status
!= HTTP_OK
&& status
!= HTTP_CONTINUE
)
1045 if (con
->http
.error
&& con
->http
.error
!= EPIPE
)
1046 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1047 "[Client %d] Closing for error %d (%s) while "
1049 con
->http
.fd
, con
->http
.error
,
1050 strerror(con
->http
.error
));
1052 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1053 "[Client %d] Closing on EOF while reading headers.",
1056 cupsdSendError(con
, HTTP_BAD_REQUEST
, CUPSD_AUTH_NONE
);
1057 cupsdCloseClient(con
);
1063 if (!data_ready(con
) && recv(con
->http
.fd
, buf
, 1, MSG_PEEK
) < 1)
1066 * Connection closed...
1069 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1070 "[Client %d] Closing on EOF", con
->http
.fd
);
1071 cupsdCloseClient(con
);
1074 break; /* Anti-compiler-warning-code */
1078 * Handle new transfers...
1081 if (status
== HTTP_OK
)
1083 if (con
->http
.fields
[HTTP_FIELD_ACCEPT_LANGUAGE
][0])
1086 * Figure out the locale from the Accept-Language and Content-Type
1090 if ((ptr
= strchr(con
->http
.fields
[HTTP_FIELD_ACCEPT_LANGUAGE
],
1094 if ((ptr
= strchr(con
->http
.fields
[HTTP_FIELD_ACCEPT_LANGUAGE
],
1098 if ((ptr
= strstr(con
->http
.fields
[HTTP_FIELD_CONTENT_TYPE
],
1099 "charset=")) != NULL
)
1102 * Combine language and charset, and trim any extra params in the
1106 snprintf(locale
, sizeof(locale
), "%s.%s",
1107 con
->http
.fields
[HTTP_FIELD_ACCEPT_LANGUAGE
], ptr
+ 8);
1109 if ((ptr
= strchr(locale
, ',')) != NULL
)
1113 snprintf(locale
, sizeof(locale
), "%s.UTF-8",
1114 con
->http
.fields
[HTTP_FIELD_ACCEPT_LANGUAGE
]);
1116 con
->language
= cupsLangGet(locale
);
1119 con
->language
= cupsLangGet(DefaultLocale
);
1121 cupsdAuthorize(con
);
1123 if (!_cups_strncasecmp(con
->http
.fields
[HTTP_FIELD_CONNECTION
],
1124 "Keep-Alive", 10) && KeepAlive
)
1125 con
->http
.keep_alive
= HTTP_KEEPALIVE_ON
;
1126 else if (!_cups_strncasecmp(con
->http
.fields
[HTTP_FIELD_CONNECTION
],
1128 con
->http
.keep_alive
= HTTP_KEEPALIVE_OFF
;
1130 if (!con
->http
.fields
[HTTP_FIELD_HOST
][0] &&
1131 con
->http
.version
>= HTTP_1_1
)
1134 * HTTP/1.1 and higher require the "Host:" field...
1137 if (!cupsdSendError(con
, HTTP_BAD_REQUEST
, CUPSD_AUTH_NONE
))
1139 cupsdLogMessage(CUPSD_LOG_ERROR
,
1140 "[Client %d] Missing Host: field in request.",
1142 cupsdCloseClient(con
);
1146 else if (!valid_host(con
))
1149 * Access to localhost must use "localhost" or the corresponding IPv4
1150 * or IPv6 values in the Host: field.
1153 cupsdLogMessage(CUPSD_LOG_ERROR
,
1154 "[Client %d] Request from \"%s\" using invalid Host: "
1155 "field \"%s\"", con
->http
.fd
, con
->http
.hostname
,
1156 con
->http
.fields
[HTTP_FIELD_HOST
]);
1158 if (!cupsdSendError(con
, HTTP_BAD_REQUEST
, CUPSD_AUTH_NONE
))
1160 cupsdCloseClient(con
);
1164 else if (con
->operation
== HTTP_OPTIONS
)
1167 * Do OPTIONS command...
1170 if (con
->best
&& con
->best
->type
!= CUPSD_AUTH_NONE
)
1172 if (!cupsdSendHeader(con
, HTTP_UNAUTHORIZED
, NULL
, CUPSD_AUTH_NONE
))
1174 cupsdCloseClient(con
);
1179 if (!_cups_strcasecmp(con
->http
.fields
[HTTP_FIELD_CONNECTION
], "Upgrade") &&
1180 con
->http
.tls
== NULL
)
1184 * Do encryption stuff...
1187 if (!cupsdSendHeader(con
, HTTP_SWITCHING_PROTOCOLS
, NULL
, CUPSD_AUTH_NONE
))
1189 cupsdCloseClient(con
);
1193 httpPrintf(HTTP(con
), "Connection: Upgrade\r\n");
1194 httpPrintf(HTTP(con
), "Upgrade: TLS/1.0,HTTP/1.1\r\n");
1195 httpPrintf(HTTP(con
), "Content-Length: 0\r\n");
1196 httpPrintf(HTTP(con
), "\r\n");
1198 if (cupsdFlushHeader(con
) < 0)
1200 cupsdCloseClient(con
);
1204 if (!encrypt_client(con
))
1206 cupsdCloseClient(con
);
1210 if (!cupsdSendError(con
, HTTP_NOT_IMPLEMENTED
, CUPSD_AUTH_NONE
))
1212 cupsdCloseClient(con
);
1215 #endif /* HAVE_SSL */
1218 if (!cupsdSendHeader(con
, HTTP_OK
, NULL
, CUPSD_AUTH_NONE
))
1220 cupsdCloseClient(con
);
1224 httpPrintf(HTTP(con
), "Allow: GET, HEAD, OPTIONS, POST, PUT\r\n");
1225 httpPrintf(HTTP(con
), "Content-Length: 0\r\n");
1226 httpPrintf(HTTP(con
), "\r\n");
1228 if (cupsdFlushHeader(con
) < 0)
1230 cupsdCloseClient(con
);
1234 else if (!is_path_absolute(con
->uri
))
1237 * Protect against malicious users!
1240 cupsdLogMessage(CUPSD_LOG_ERROR
,
1241 "[Client %d] Request for non-absolute resource \"%s\".",
1242 con
->http
.fd
, con
->uri
);
1244 if (!cupsdSendError(con
, HTTP_FORBIDDEN
, CUPSD_AUTH_NONE
))
1246 cupsdCloseClient(con
);
1252 if (!_cups_strcasecmp(con
->http
.fields
[HTTP_FIELD_CONNECTION
],
1253 "Upgrade") && con
->http
.tls
== NULL
)
1257 * Do encryption stuff...
1260 if (!cupsdSendHeader(con
, HTTP_SWITCHING_PROTOCOLS
, NULL
,
1263 cupsdCloseClient(con
);
1267 httpPrintf(HTTP(con
), "Connection: Upgrade\r\n");
1268 httpPrintf(HTTP(con
), "Upgrade: TLS/1.0,HTTP/1.1\r\n");
1269 httpPrintf(HTTP(con
), "Content-Length: 0\r\n");
1270 httpPrintf(HTTP(con
), "\r\n");
1272 if (cupsdFlushHeader(con
) < 0)
1274 cupsdCloseClient(con
);
1278 if (!encrypt_client(con
))
1280 cupsdCloseClient(con
);
1284 if (!cupsdSendError(con
, HTTP_NOT_IMPLEMENTED
, CUPSD_AUTH_NONE
))
1286 cupsdCloseClient(con
);
1289 #endif /* HAVE_SSL */
1292 if ((status
= cupsdIsAuthorized(con
, NULL
)) != HTTP_OK
)
1294 cupsdSendError(con
, status
, CUPSD_AUTH_NONE
);
1295 cupsdCloseClient(con
);
1299 if (con
->http
.expect
&&
1300 (con
->operation
== HTTP_POST
|| con
->operation
== HTTP_PUT
))
1302 if (con
->http
.expect
== HTTP_CONTINUE
)
1305 * Send 100-continue header...
1308 if (!cupsdSendHeader(con
, HTTP_CONTINUE
, NULL
, CUPSD_AUTH_NONE
))
1310 cupsdCloseClient(con
);
1317 * Send 417-expectation-failed header...
1320 if (!cupsdSendHeader(con
, HTTP_EXPECTATION_FAILED
, NULL
,
1323 cupsdCloseClient(con
);
1327 httpPrintf(HTTP(con
), "Content-Length: 0\r\n");
1328 httpPrintf(HTTP(con
), "\r\n");
1330 if (cupsdFlushHeader(con
) < 0)
1332 cupsdCloseClient(con
);
1338 switch (con
->http
.state
)
1340 case HTTP_GET_SEND
:
1341 if (!strncmp(con
->uri
, "/printers/", 10) &&
1342 !strcmp(con
->uri
+ strlen(con
->uri
) - 4, ".ppd"))
1345 * Send PPD file - get the real printer name since printer
1346 * names are not case sensitive but filenames can be...
1349 con
->uri
[strlen(con
->uri
) - 4] = '\0'; /* Drop ".ppd" */
1351 if ((p
= cupsdFindPrinter(con
->uri
+ 10)) != NULL
)
1352 snprintf(con
->uri
, sizeof(con
->uri
), "/ppd/%s.ppd", p
->name
);
1355 if (!cupsdSendError(con
, HTTP_NOT_FOUND
, CUPSD_AUTH_NONE
))
1357 cupsdCloseClient(con
);
1364 else if ((!strncmp(con
->uri
, "/printers/", 10) ||
1365 !strncmp(con
->uri
, "/classes/", 9)) &&
1366 !strcmp(con
->uri
+ strlen(con
->uri
) - 4, ".png"))
1369 * Send icon file - get the real queue name since queue names are
1370 * not case sensitive but filenames can be...
1373 con
->uri
[strlen(con
->uri
) - 4] = '\0'; /* Drop ".png" */
1375 if (!strncmp(con
->uri
, "/printers/", 10))
1376 p
= cupsdFindPrinter(con
->uri
+ 10);
1378 p
= cupsdFindClass(con
->uri
+ 9);
1381 snprintf(con
->uri
, sizeof(con
->uri
), "/icons/%s.png", p
->name
);
1384 if (!cupsdSendError(con
, HTTP_NOT_FOUND
, CUPSD_AUTH_NONE
))
1386 cupsdCloseClient(con
);
1393 else if (!WebInterface
)
1396 * Web interface is disabled. Show an appropriate message...
1399 if (!cupsdSendError(con
, HTTP_WEBIF_DISABLED
, CUPSD_AUTH_NONE
))
1401 cupsdCloseClient(con
);
1408 if ((!strncmp(con
->uri
, "/admin", 6) &&
1409 strncmp(con
->uri
, "/admin/conf/", 12) &&
1410 strncmp(con
->uri
, "/admin/log/", 11)) ||
1411 !strncmp(con
->uri
, "/printers", 9) ||
1412 !strncmp(con
->uri
, "/classes", 8) ||
1413 !strncmp(con
->uri
, "/help", 5) ||
1414 !strncmp(con
->uri
, "/jobs", 5))
1417 * Send CGI output...
1420 if (!strncmp(con
->uri
, "/admin", 6))
1422 cupsdSetStringf(&con
->command
, "%s/cgi-bin/admin.cgi",
1425 cupsdSetString(&con
->options
, strchr(con
->uri
+ 6, '?'));
1427 else if (!strncmp(con
->uri
, "/printers", 9))
1429 cupsdSetStringf(&con
->command
, "%s/cgi-bin/printers.cgi",
1432 if (con
->uri
[9] && con
->uri
[10])
1433 cupsdSetString(&con
->options
, con
->uri
+ 9);
1435 cupsdSetString(&con
->options
, NULL
);
1437 else if (!strncmp(con
->uri
, "/classes", 8))
1439 cupsdSetStringf(&con
->command
, "%s/cgi-bin/classes.cgi",
1442 if (con
->uri
[8] && con
->uri
[9])
1443 cupsdSetString(&con
->options
, con
->uri
+ 8);
1445 cupsdSetString(&con
->options
, NULL
);
1447 else if (!strncmp(con
->uri
, "/jobs", 5))
1449 cupsdSetStringf(&con
->command
, "%s/cgi-bin/jobs.cgi",
1452 if (con
->uri
[5] && con
->uri
[6])
1453 cupsdSetString(&con
->options
, con
->uri
+ 5);
1455 cupsdSetString(&con
->options
, NULL
);
1459 cupsdSetStringf(&con
->command
, "%s/cgi-bin/help.cgi",
1462 if (con
->uri
[5] && con
->uri
[6])
1463 cupsdSetString(&con
->options
, con
->uri
+ 5);
1465 cupsdSetString(&con
->options
, NULL
);
1468 if (!cupsdSendCommand(con
, con
->command
, con
->options
, 0))
1470 if (!cupsdSendError(con
, HTTP_NOT_FOUND
, CUPSD_AUTH_NONE
))
1472 cupsdCloseClient(con
);
1477 cupsdLogRequest(con
, HTTP_OK
);
1479 if (con
->http
.version
<= HTTP_1_0
)
1480 con
->http
.keep_alive
= HTTP_KEEPALIVE_OFF
;
1482 else if ((!strncmp(con
->uri
, "/admin/conf/", 12) &&
1483 (strchr(con
->uri
+ 12, '/') ||
1484 strlen(con
->uri
) == 12)) ||
1485 (!strncmp(con
->uri
, "/admin/log/", 11) &&
1486 (strchr(con
->uri
+ 11, '/') ||
1487 strlen(con
->uri
) == 11)))
1490 * GET can only be done to configuration files directly under
1494 cupsdLogMessage(CUPSD_LOG_ERROR
,
1495 "Request for subdirectory \"%s\"!", con
->uri
);
1497 if (!cupsdSendError(con
, HTTP_FORBIDDEN
, CUPSD_AUTH_NONE
))
1499 cupsdCloseClient(con
);
1511 if ((filename
= get_file(con
, &filestats
, buf
,
1512 sizeof(buf
))) == NULL
)
1514 if (!cupsdSendError(con
, HTTP_NOT_FOUND
, CUPSD_AUTH_NONE
))
1516 cupsdCloseClient(con
);
1523 type
= mimeFileType(MimeDatabase
, filename
, NULL
, NULL
);
1525 if (is_cgi(con
, filename
, &filestats
, type
))
1528 * Note: con->command and con->options were set by
1532 if (!cupsdSendCommand(con
, con
->command
, con
->options
, 0))
1534 if (!cupsdSendError(con
, HTTP_NOT_FOUND
, CUPSD_AUTH_NONE
))
1536 cupsdCloseClient(con
);
1541 cupsdLogRequest(con
, HTTP_OK
);
1543 if (con
->http
.version
<= HTTP_1_0
)
1544 con
->http
.keep_alive
= HTTP_KEEPALIVE_OFF
;
1548 if (!check_if_modified(con
, &filestats
))
1550 if (!cupsdSendError(con
, HTTP_NOT_MODIFIED
, CUPSD_AUTH_NONE
))
1552 cupsdCloseClient(con
);
1559 strcpy(line
, "text/plain");
1561 snprintf(line
, sizeof(line
), "%s/%s", type
->super
, type
->type
);
1563 if (!write_file(con
, HTTP_OK
, filename
, line
, &filestats
))
1565 cupsdCloseClient(con
);
1572 case HTTP_POST_RECV
:
1574 * See if the POST request includes a Content-Length field, and if
1575 * so check the length against any limits that are set...
1578 if (con
->http
.fields
[HTTP_FIELD_CONTENT_LENGTH
][0] &&
1579 MaxRequestSize
> 0 &&
1580 con
->http
.data_remaining
> MaxRequestSize
)
1583 * Request too large...
1586 if (!cupsdSendError(con
, HTTP_REQUEST_TOO_LARGE
, CUPSD_AUTH_NONE
))
1588 cupsdCloseClient(con
);
1594 else if (con
->http
.data_remaining
< 0 ||
1595 (!con
->http
.fields
[HTTP_FIELD_CONTENT_LENGTH
][0] &&
1596 con
->http
.data_encoding
== HTTP_ENCODE_LENGTH
))
1599 * Negative content lengths are invalid!
1602 if (!cupsdSendError(con
, HTTP_BAD_REQUEST
, CUPSD_AUTH_NONE
))
1604 cupsdCloseClient(con
);
1612 * See what kind of POST request this is; for IPP requests the
1613 * content-type field will be "application/ipp"...
1616 if (!strcmp(con
->http
.fields
[HTTP_FIELD_CONTENT_TYPE
],
1618 con
->request
= ippNew();
1619 else if (!WebInterface
)
1622 * Web interface is disabled. Show an appropriate message...
1625 if (!cupsdSendError(con
, HTTP_WEBIF_DISABLED
, CUPSD_AUTH_NONE
))
1627 cupsdCloseClient(con
);
1633 else if ((!strncmp(con
->uri
, "/admin", 6) &&
1634 strncmp(con
->uri
, "/admin/conf/", 12) &&
1635 strncmp(con
->uri
, "/admin/log/", 11)) ||
1636 !strncmp(con
->uri
, "/printers", 9) ||
1637 !strncmp(con
->uri
, "/classes", 8) ||
1638 !strncmp(con
->uri
, "/help", 5) ||
1639 !strncmp(con
->uri
, "/jobs", 5))
1645 if (!strncmp(con
->uri
, "/admin", 6))
1647 cupsdSetStringf(&con
->command
, "%s/cgi-bin/admin.cgi",
1650 cupsdSetString(&con
->options
, strchr(con
->uri
+ 6, '?'));
1652 else if (!strncmp(con
->uri
, "/printers", 9))
1654 cupsdSetStringf(&con
->command
, "%s/cgi-bin/printers.cgi",
1657 if (con
->uri
[9] && con
->uri
[10])
1658 cupsdSetString(&con
->options
, con
->uri
+ 9);
1660 cupsdSetString(&con
->options
, NULL
);
1662 else if (!strncmp(con
->uri
, "/classes", 8))
1664 cupsdSetStringf(&con
->command
, "%s/cgi-bin/classes.cgi",
1667 if (con
->uri
[8] && con
->uri
[9])
1668 cupsdSetString(&con
->options
, con
->uri
+ 8);
1670 cupsdSetString(&con
->options
, NULL
);
1672 else if (!strncmp(con
->uri
, "/jobs", 5))
1674 cupsdSetStringf(&con
->command
, "%s/cgi-bin/jobs.cgi",
1677 if (con
->uri
[5] && con
->uri
[6])
1678 cupsdSetString(&con
->options
, con
->uri
+ 5);
1680 cupsdSetString(&con
->options
, NULL
);
1684 cupsdSetStringf(&con
->command
, "%s/cgi-bin/help.cgi",
1687 if (con
->uri
[5] && con
->uri
[6])
1688 cupsdSetString(&con
->options
, con
->uri
+ 5);
1690 cupsdSetString(&con
->options
, NULL
);
1693 if (con
->http
.version
<= HTTP_1_0
)
1694 con
->http
.keep_alive
= HTTP_KEEPALIVE_OFF
;
1702 if ((filename
= get_file(con
, &filestats
, buf
,
1703 sizeof(buf
))) == NULL
)
1705 if (!cupsdSendError(con
, HTTP_NOT_FOUND
, CUPSD_AUTH_NONE
))
1707 cupsdCloseClient(con
);
1714 type
= mimeFileType(MimeDatabase
, filename
, NULL
, NULL
);
1716 if (!is_cgi(con
, filename
, &filestats
, type
))
1719 * Only POST to CGI's...
1722 if (!cupsdSendError(con
, HTTP_UNAUTHORIZED
, CUPSD_AUTH_NONE
))
1724 cupsdCloseClient(con
);
1731 case HTTP_PUT_RECV
:
1733 * Validate the resource name...
1736 if (strncmp(con
->uri
, "/admin/conf/", 12) ||
1737 strchr(con
->uri
+ 12, '/') ||
1738 strlen(con
->uri
) == 12)
1741 * PUT can only be done to configuration files under
1745 cupsdLogMessage(CUPSD_LOG_ERROR
,
1746 "[Client %d] Request for subdirectory \"%s\".",
1747 con
->http
.fd
, con
->uri
);
1749 if (!cupsdSendError(con
, HTTP_FORBIDDEN
, CUPSD_AUTH_NONE
))
1751 cupsdCloseClient(con
);
1759 * See if the PUT request includes a Content-Length field, and if
1760 * so check the length against any limits that are set...
1763 if (con
->http
.fields
[HTTP_FIELD_CONTENT_LENGTH
][0] &&
1764 MaxRequestSize
> 0 &&
1765 con
->http
.data_remaining
> MaxRequestSize
)
1768 * Request too large...
1771 if (!cupsdSendError(con
, HTTP_REQUEST_TOO_LARGE
, CUPSD_AUTH_NONE
))
1773 cupsdCloseClient(con
);
1779 else if (con
->http
.data_remaining
< 0)
1782 * Negative content lengths are invalid!
1785 if (!cupsdSendError(con
, HTTP_BAD_REQUEST
, CUPSD_AUTH_NONE
))
1787 cupsdCloseClient(con
);
1795 * Open a temporary file to hold the request...
1798 cupsdSetStringf(&con
->filename
, "%s/%08x", RequestRoot
,
1800 con
->file
= open(con
->filename
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0640);
1804 cupsdLogMessage(CUPSD_LOG_ERROR
,
1805 "[Client %d] Unable to create request file "
1806 "\"%s\": %s", con
->http
.fd
, con
->filename
,
1809 if (!cupsdSendError(con
, HTTP_REQUEST_TOO_LARGE
, CUPSD_AUTH_NONE
))
1811 cupsdCloseClient(con
);
1816 fchmod(con
->file
, 0640);
1817 fchown(con
->file
, RunUser
, Group
);
1818 fcntl(con
->file
, F_SETFD
, fcntl(con
->file
, F_GETFD
) | FD_CLOEXEC
);
1823 cupsdSendError(con
, HTTP_NOT_IMPLEMENTED
, CUPSD_AUTH_NONE
);
1824 cupsdCloseClient(con
);
1828 if (!strncmp(con
->uri
, "/printers/", 10) &&
1829 !strcmp(con
->uri
+ strlen(con
->uri
) - 4, ".ppd"))
1832 * Send PPD file - get the real printer name since printer
1833 * names are not case sensitive but filenames can be...
1836 con
->uri
[strlen(con
->uri
) - 4] = '\0'; /* Drop ".ppd" */
1838 if ((p
= cupsdFindPrinter(con
->uri
+ 10)) != NULL
)
1839 snprintf(con
->uri
, sizeof(con
->uri
), "/ppd/%s.ppd", p
->name
);
1842 if (!cupsdSendError(con
, HTTP_NOT_FOUND
, CUPSD_AUTH_NONE
))
1844 cupsdCloseClient(con
);
1851 else if (!strncmp(con
->uri
, "/printers/", 10) &&
1852 !strcmp(con
->uri
+ strlen(con
->uri
) - 4, ".png"))
1855 * Send PNG file - get the real printer name since printer
1856 * names are not case sensitive but filenames can be...
1859 con
->uri
[strlen(con
->uri
) - 4] = '\0'; /* Drop ".ppd" */
1861 if ((p
= cupsdFindPrinter(con
->uri
+ 10)) != NULL
)
1862 snprintf(con
->uri
, sizeof(con
->uri
), "/icons/%s.png", p
->name
);
1865 if (!cupsdSendError(con
, HTTP_NOT_FOUND
, CUPSD_AUTH_NONE
))
1867 cupsdCloseClient(con
);
1874 else if (!WebInterface
)
1876 if (!cupsdSendHeader(con
, HTTP_OK
, line
, CUPSD_AUTH_NONE
))
1878 cupsdCloseClient(con
);
1882 if (httpPrintf(HTTP(con
), "\r\n") < 0)
1884 cupsdCloseClient(con
);
1888 if (cupsdFlushHeader(con
) < 0)
1890 cupsdCloseClient(con
);
1894 con
->http
.state
= HTTP_WAITING
;
1898 if ((!strncmp(con
->uri
, "/admin", 6) &&
1899 strncmp(con
->uri
, "/admin/conf/", 12) &&
1900 strncmp(con
->uri
, "/admin/log/", 11)) ||
1901 !strncmp(con
->uri
, "/printers", 9) ||
1902 !strncmp(con
->uri
, "/classes", 8) ||
1903 !strncmp(con
->uri
, "/help", 5) ||
1904 !strncmp(con
->uri
, "/jobs", 5))
1910 if (!cupsdSendHeader(con
, HTTP_OK
, "text/html", CUPSD_AUTH_NONE
))
1912 cupsdCloseClient(con
);
1916 if (httpPrintf(HTTP(con
), "\r\n") < 0)
1918 cupsdCloseClient(con
);
1922 if (cupsdFlushHeader(con
) < 0)
1924 cupsdCloseClient(con
);
1928 cupsdLogRequest(con
, HTTP_OK
);
1930 else if ((!strncmp(con
->uri
, "/admin/conf/", 12) &&
1931 (strchr(con
->uri
+ 12, '/') ||
1932 strlen(con
->uri
) == 12)) ||
1933 (!strncmp(con
->uri
, "/admin/log/", 11) &&
1934 (strchr(con
->uri
+ 11, '/') ||
1935 strlen(con
->uri
) == 11)))
1938 * HEAD can only be done to configuration files under
1942 cupsdLogMessage(CUPSD_LOG_ERROR
,
1943 "[Client %d] Request for subdirectory \"%s\".",
1944 con
->http
.fd
, con
->uri
);
1946 if (!cupsdSendError(con
, HTTP_FORBIDDEN
, CUPSD_AUTH_NONE
))
1948 cupsdCloseClient(con
);
1954 else if ((filename
= get_file(con
, &filestats
, buf
,
1955 sizeof(buf
))) == NULL
)
1957 if (!cupsdSendHeader(con
, HTTP_NOT_FOUND
, "text/html",
1960 cupsdCloseClient(con
);
1964 cupsdLogRequest(con
, HTTP_NOT_FOUND
);
1966 else if (!check_if_modified(con
, &filestats
))
1968 if (!cupsdSendError(con
, HTTP_NOT_MODIFIED
, CUPSD_AUTH_NONE
))
1970 cupsdCloseClient(con
);
1974 cupsdLogRequest(con
, HTTP_NOT_MODIFIED
);
1982 type
= mimeFileType(MimeDatabase
, filename
, NULL
, NULL
);
1984 strcpy(line
, "text/plain");
1986 snprintf(line
, sizeof(line
), "%s/%s", type
->super
, type
->type
);
1988 if (!cupsdSendHeader(con
, HTTP_OK
, line
, CUPSD_AUTH_NONE
))
1990 cupsdCloseClient(con
);
1994 if (httpPrintf(HTTP(con
), "Last-Modified: %s\r\n",
1995 httpGetDateString(filestats
.st_mtime
)) < 0)
1997 cupsdCloseClient(con
);
2001 if (httpPrintf(HTTP(con
), "Content-Length: %lu\r\n",
2002 (unsigned long)filestats
.st_size
) < 0)
2004 cupsdCloseClient(con
);
2008 cupsdLogRequest(con
, HTTP_OK
);
2011 if (httpPrintf(HTTP(con
), "\r\n") < 0)
2013 cupsdCloseClient(con
);
2017 if (cupsdFlushHeader(con
) < 0)
2019 cupsdCloseClient(con
);
2023 con
->http
.state
= HTTP_WAITING
;
2027 break; /* Anti-compiler-warning-code */
2033 * Handle any incoming data...
2036 switch (con
->http
.state
)
2038 case HTTP_PUT_RECV
:
2041 if ((bytes
= httpRead2(HTTP(con
), line
, sizeof(line
))) < 0)
2043 if (con
->http
.error
&& con
->http
.error
!= EPIPE
)
2044 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2045 "[Client %d] HTTP_PUT_RECV Closing for error "
2046 "%d (%s)", con
->http
.fd
, con
->http
.error
,
2047 strerror(con
->http
.error
));
2049 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2050 "[Client %d] HTTP_PUT_RECV Closing on EOF",
2053 cupsdCloseClient(con
);
2058 con
->bytes
+= bytes
;
2060 if (write(con
->file
, line
, bytes
) < bytes
)
2062 cupsdLogMessage(CUPSD_LOG_ERROR
,
2063 "[Client %d] Unable to write %d bytes to "
2064 "\"%s\": %s", con
->http
.fd
, bytes
, con
->filename
,
2069 unlink(con
->filename
);
2070 cupsdClearString(&con
->filename
);
2072 if (!cupsdSendError(con
, HTTP_REQUEST_TOO_LARGE
, CUPSD_AUTH_NONE
))
2074 cupsdCloseClient(con
);
2080 while (con
->http
.state
== HTTP_PUT_RECV
&& data_ready(con
));
2082 if (con
->http
.state
== HTTP_WAITING
)
2085 * End of file, see how big it is...
2088 fstat(con
->file
, &filestats
);
2093 if (filestats
.st_size
> MaxRequestSize
&&
2097 * Request is too big; remove it and send an error...
2100 unlink(con
->filename
);
2101 cupsdClearString(&con
->filename
);
2103 if (!cupsdSendError(con
, HTTP_REQUEST_TOO_LARGE
, CUPSD_AUTH_NONE
))
2105 cupsdCloseClient(con
);
2111 * Install the configuration file...
2114 status
= install_conf_file(con
);
2117 * Return the status to the client...
2120 if (!cupsdSendError(con
, status
, CUPSD_AUTH_NONE
))
2122 cupsdCloseClient(con
);
2128 case HTTP_POST_RECV
:
2131 if (con
->request
&& con
->file
< 0)
2134 * Grab any request data from the connection...
2137 if ((ipp_state
= ippRead(&(con
->http
), con
->request
)) == IPP_ERROR
)
2139 cupsdLogMessage(CUPSD_LOG_ERROR
,
2140 "[Client %d] IPP read error: %s", con
->http
.fd
,
2141 cupsLastErrorString());
2143 cupsdSendError(con
, HTTP_BAD_REQUEST
, CUPSD_AUTH_NONE
);
2144 cupsdCloseClient(con
);
2147 else if (ipp_state
!= IPP_DATA
)
2149 if (con
->http
.state
== HTTP_POST_SEND
)
2151 cupsdSendError(con
, HTTP_BAD_REQUEST
, CUPSD_AUTH_NONE
);
2152 cupsdCloseClient(con
);
2160 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Client %d] %d.%d %s %d",
2161 con
->http
.fd
, con
->request
->request
.op
.version
[0],
2162 con
->request
->request
.op
.version
[1],
2163 ippOpString(con
->request
->request
.op
.operation_id
),
2164 con
->request
->request
.op
.request_id
);
2165 con
->bytes
+= ippLength(con
->request
);
2169 if (con
->file
< 0 && con
->http
.state
!= HTTP_POST_SEND
)
2172 * Create a file as needed for the request data...
2175 cupsdSetStringf(&con
->filename
, "%s/%08x", RequestRoot
,
2177 con
->file
= open(con
->filename
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0640);
2181 cupsdLogMessage(CUPSD_LOG_ERROR
,
2182 "[Client %d] Unable to create request file "
2183 "\"%s\": %s", con
->http
.fd
, con
->filename
,
2186 if (!cupsdSendError(con
, HTTP_REQUEST_TOO_LARGE
, CUPSD_AUTH_NONE
))
2188 cupsdCloseClient(con
);
2193 fchmod(con
->file
, 0640);
2194 fchown(con
->file
, RunUser
, Group
);
2195 fcntl(con
->file
, F_SETFD
, fcntl(con
->file
, F_GETFD
) | FD_CLOEXEC
);
2198 if (con
->http
.state
!= HTTP_POST_SEND
)
2200 if ((bytes
= httpRead2(HTTP(con
), line
, sizeof(line
))) < 0)
2202 if (con
->http
.error
&& con
->http
.error
!= EPIPE
)
2203 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2204 "[Client %d] HTTP_POST_SEND Closing for "
2205 "error %d (%s)", con
->http
.fd
, con
->http
.error
,
2206 strerror(con
->http
.error
));
2208 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2209 "[Client %d] HTTP_POST_SEND Closing on EOF",
2212 cupsdCloseClient(con
);
2217 con
->bytes
+= bytes
;
2219 if (write(con
->file
, line
, bytes
) < bytes
)
2221 cupsdLogMessage(CUPSD_LOG_ERROR
,
2222 "[Client %d] Unable to write %d bytes to "
2223 "\"%s\": %s", con
->http
.fd
, bytes
,
2224 con
->filename
, strerror(errno
));
2228 unlink(con
->filename
);
2229 cupsdClearString(&con
->filename
);
2231 if (!cupsdSendError(con
, HTTP_REQUEST_TOO_LARGE
,
2234 cupsdCloseClient(con
);
2239 else if (con
->http
.state
== HTTP_POST_RECV
)
2241 else if (con
->http
.state
!= HTTP_POST_SEND
)
2243 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2244 "[Client %d] Closing on unexpected state %s.",
2245 con
->http
.fd
, http_states
[con
->http
.state
]);
2246 cupsdCloseClient(con
);
2251 while (con
->http
.state
== HTTP_POST_RECV
&& data_ready(con
));
2253 if (con
->http
.state
== HTTP_POST_SEND
)
2257 fstat(con
->file
, &filestats
);
2262 if (filestats
.st_size
> MaxRequestSize
&&
2266 * Request is too big; remove it and send an error...
2269 unlink(con
->filename
);
2270 cupsdClearString(&con
->filename
);
2275 * Delete any IPP request data...
2278 ippDelete(con
->request
);
2279 con
->request
= NULL
;
2282 if (!cupsdSendError(con
, HTTP_REQUEST_TOO_LARGE
, CUPSD_AUTH_NONE
))
2284 cupsdCloseClient(con
);
2288 else if (filestats
.st_size
== 0)
2291 * Don't allow empty file...
2294 unlink(con
->filename
);
2295 cupsdClearString(&con
->filename
);
2300 if (!cupsdSendCommand(con
, con
->command
, con
->options
, 0))
2302 if (!cupsdSendError(con
, HTTP_NOT_FOUND
, CUPSD_AUTH_NONE
))
2304 cupsdCloseClient(con
);
2309 cupsdLogRequest(con
, HTTP_OK
);
2315 cupsdProcessIPPRequest(con
);
2319 unlink(con
->filename
);
2320 cupsdClearString(&con
->filename
);
2329 break; /* Anti-compiler-warning-code */
2332 if (con
->http
.state
== HTTP_WAITING
)
2334 if (!con
->http
.keep_alive
)
2336 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2337 "[Client %d] Closing because Keep-Alive disabled",
2339 cupsdCloseClient(con
);
2343 cupsArrayRemove(ActiveClients
, con
);
2344 cupsdSetBusyState();
2351 * 'cupsdSendCommand()' - Send output from a command via HTTP.
2354 int /* O - 1 on success, 0 on failure */
2356 cupsd_client_t
*con
, /* I - Client connection */
2357 char *command
, /* I - Command to run */
2358 char *options
, /* I - Command-line options */
2359 int root
) /* I - Run as root? */
2361 int fd
; /* Standard input file descriptor */
2366 fd
= open(con
->filename
, O_RDONLY
);
2370 cupsdLogMessage(CUPSD_LOG_ERROR
,
2371 "[Client %d] Unable to open \"%s\" for reading: %s",
2372 con
->http
.fd
, con
->filename
? con
->filename
: "/dev/null",
2377 fcntl(fd
, F_SETFD
, fcntl(fd
, F_GETFD
) | FD_CLOEXEC
);
2382 con
->pipe_pid
= pipe_command(con
, fd
, &(con
->file
), command
, options
, root
);
2387 cupsdLogMessage(CUPSD_LOG_INFO
, "[Client %d] Started \"%s\" (pid=%d)",
2388 con
->http
.fd
, command
, con
->pipe_pid
);
2390 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Client %d] file=%d", con
->http
.fd
,
2393 if (con
->pipe_pid
== 0)
2396 fcntl(con
->file
, F_SETFD
, fcntl(con
->file
, F_GETFD
) | FD_CLOEXEC
);
2398 cupsdAddSelect(con
->file
, (cupsd_selfunc_t
)write_pipe
, NULL
, con
);
2400 con
->sent_header
= 0;
2401 con
->file_ready
= 0;
2402 con
->got_fields
= 0;
2403 con
->header_used
= 0;
2410 * 'cupsdSendError()' - Send an error message via HTTP.
2413 int /* O - 1 if successful, 0 otherwise */
2414 cupsdSendError(cupsd_client_t
*con
, /* I - Connection */
2415 http_status_t code
, /* I - Error code */
2416 int auth_type
)/* I - Authentication type */
2418 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2419 "[Client %d] cupsdSendError code=%d, auth_type=%d",
2420 con
->http
.fd
, code
, auth_type
);
2424 * Force client to upgrade for authentication if that is how the
2425 * server is configured...
2428 if (code
== HTTP_UNAUTHORIZED
&&
2429 DefaultEncryption
== HTTP_ENCRYPT_REQUIRED
&&
2430 _cups_strcasecmp(con
->http
.hostname
, "localhost") &&
2433 code
= HTTP_UPGRADE_REQUIRED
;
2435 #endif /* HAVE_SSL */
2438 * Put the request in the access_log file...
2441 cupsdLogRequest(con
, code
);
2444 * To work around bugs in some proxies, don't use Keep-Alive for some
2447 * Kerberos authentication doesn't work without Keep-Alive, so
2448 * never disable it in that case.
2451 if (code
>= HTTP_BAD_REQUEST
&& con
->http
.auth_type
!= CUPSD_AUTH_NEGOTIATE
)
2452 con
->http
.keep_alive
= HTTP_KEEPALIVE_OFF
;
2455 * Send an error message back to the client. If the error code is a
2456 * 400 or 500 series, make sure the message contains some text, too!
2459 if (!cupsdSendHeader(con
, code
, NULL
, auth_type
))
2463 if (code
== HTTP_UPGRADE_REQUIRED
)
2464 if (httpPrintf(HTTP(con
), "Connection: Upgrade\r\n") < 0)
2467 if (httpPrintf(HTTP(con
), "Upgrade: TLS/1.0,HTTP/1.1\r\n") < 0)
2469 #endif /* HAVE_SSL */
2471 if (con
->http
.version
>= HTTP_1_1
&&
2472 con
->http
.keep_alive
== HTTP_KEEPALIVE_OFF
)
2474 if (httpPrintf(HTTP(con
), "Connection: close\r\n") < 0)
2478 if (code
>= HTTP_BAD_REQUEST
)
2481 * Send a human-readable error message.
2484 char message
[4096], /* Message for user */
2485 urltext
[1024], /* URL redirection text */
2486 redirect
[1024]; /* Redirection link */
2487 const char *text
; /* Status-specific text */
2492 if (code
== HTTP_UNAUTHORIZED
)
2493 text
= _cupsLangString(con
->language
,
2494 _("Enter your username and password or the "
2495 "root username and password to access this "
2496 "page. If you are using Kerberos authentication, "
2497 "make sure you have a valid Kerberos ticket."));
2498 else if (code
== HTTP_UPGRADE_REQUIRED
)
2502 snprintf(urltext
, sizeof(urltext
),
2503 _cupsLangString(con
->language
,
2504 _("You must access this page using the URL "
2505 "<A HREF=\"https://%s:%d%s\">"
2506 "https://%s:%d%s</A>.")),
2507 con
->servername
, con
->serverport
, con
->uri
,
2508 con
->servername
, con
->serverport
, con
->uri
);
2510 snprintf(redirect
, sizeof(redirect
),
2511 "<META HTTP-EQUIV=\"Refresh\" "
2512 "CONTENT=\"3;URL=https://%s:%d%s\">\n",
2513 con
->servername
, con
->serverport
, con
->uri
);
2515 else if (code
== HTTP_WEBIF_DISABLED
)
2516 text
= _cupsLangString(con
->language
,
2517 _("The web interface is currently disabled. Run "
2518 "\"cupsctl WebInterface=yes\" to enable it."));
2522 snprintf(message
, sizeof(message
),
2523 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" "
2524 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
2527 "\t<META HTTP-EQUIV=\"Content-Type\" "
2528 "CONTENT=\"text/html; charset=utf-8\">\n"
2529 "\t<TITLE>%s - " CUPS_SVERSION
"</TITLE>\n"
2530 "\t<LINK REL=\"STYLESHEET\" TYPE=\"text/css\" "
2531 "HREF=\"/cups.css\">\n"
2539 httpStatus(code
), redirect
, httpStatus(code
), text
);
2541 if (httpPrintf(HTTP(con
), "Content-Type: text/html; charset=utf-8\r\n") < 0)
2543 if (httpPrintf(HTTP(con
), "Content-Length: %d\r\n",
2544 (int)strlen(message
)) < 0)
2546 if (httpPrintf(HTTP(con
), "\r\n") < 0)
2548 if (httpPrintf(HTTP(con
), "%s", message
) < 0)
2551 else if (httpPrintf(HTTP(con
), "\r\n") < 0)
2554 if (cupsdFlushHeader(con
) < 0)
2557 con
->http
.state
= HTTP_WAITING
;
2564 * 'cupsdSendHeader()' - Send an HTTP request.
2567 int /* O - 1 on success, 0 on failure */
2569 cupsd_client_t
*con
, /* I - Client to send to */
2570 http_status_t code
, /* I - HTTP status code */
2571 char *type
, /* I - MIME type of document */
2572 int auth_type
) /* I - Type of authentication */
2574 char auth_str
[1024]; /* Authorization string */
2578 * Send the HTTP status header...
2581 if (code
== HTTP_CONTINUE
)
2584 * 100-continue doesn't send any headers...
2587 return (httpPrintf(HTTP(con
), "HTTP/%d.%d 100 Continue\r\n\r\n",
2588 con
->http
.version
/ 100, con
->http
.version
% 100) > 0);
2590 else if (code
== HTTP_WEBIF_DISABLED
)
2593 * Treat our special "web interface is disabled" status as "200 OK" for web
2600 httpFlushWrite(HTTP(con
));
2602 con
->http
.data_encoding
= HTTP_ENCODE_FIELDS
;
2604 if (httpPrintf(HTTP(con
), "HTTP/%d.%d %d %s\r\n", con
->http
.version
/ 100,
2605 con
->http
.version
% 100, code
, httpStatus(code
)) < 0)
2607 if (httpPrintf(HTTP(con
), "Date: %s\r\n", httpGetDateString(time(NULL
))) < 0)
2610 if (httpPrintf(HTTP(con
), "Server: %s\r\n", ServerHeader
) < 0)
2612 if (con
->http
.keep_alive
&& con
->http
.version
>= HTTP_1_0
)
2614 if (httpPrintf(HTTP(con
), "Connection: Keep-Alive\r\n") < 0)
2616 if (httpPrintf(HTTP(con
), "Keep-Alive: timeout=%d\r\n",
2617 KeepAliveTimeout
) < 0)
2620 if (code
== HTTP_METHOD_NOT_ALLOWED
)
2621 if (httpPrintf(HTTP(con
), "Allow: GET, HEAD, OPTIONS, POST, PUT\r\n") < 0)
2624 if (code
== HTTP_UNAUTHORIZED
)
2626 if (auth_type
== CUPSD_AUTH_NONE
)
2628 if (!con
->best
|| con
->best
->type
<= CUPSD_AUTH_NONE
)
2629 auth_type
= DefaultAuthType
;
2631 auth_type
= con
->best
->type
;
2636 if (auth_type
== CUPSD_AUTH_BASIC
|| auth_type
== CUPSD_AUTH_BASICDIGEST
)
2637 strlcpy(auth_str
, "Basic realm=\"CUPS\"", sizeof(auth_str
));
2638 else if (auth_type
== CUPSD_AUTH_DIGEST
)
2639 snprintf(auth_str
, sizeof(auth_str
), "Digest realm=\"CUPS\", nonce=\"%s\"",
2640 con
->http
.hostname
);
2642 else if (auth_type
== CUPSD_AUTH_NEGOTIATE
)
2645 if (_httpAddrFamily(con
->http
.hostaddr
) == AF_LOCAL
)
2646 strlcpy(auth_str
, "Basic realm=\"CUPS\"", sizeof(auth_str
));
2648 # endif /* AF_LOCAL */
2649 strlcpy(auth_str
, "Negotiate", sizeof(auth_str
));
2651 #endif /* HAVE_GSSAPI */
2653 if (con
->best
&& auth_type
!= CUPSD_AUTH_NEGOTIATE
&&
2654 !_cups_strcasecmp(con
->http
.hostname
, "localhost"))
2657 * Add a "trc" (try root certification) parameter for local non-Kerberos
2658 * requests when the request requires system group membership - then the
2659 * client knows the root certificate can/should be used.
2661 * Also, for Mac OS X we also look for @AUTHKEY and add an "authkey"
2662 * parameter as needed...
2665 char *name
, /* Current user name */
2666 *auth_key
; /* Auth key buffer */
2667 size_t auth_size
; /* Size of remaining buffer */
2669 auth_key
= auth_str
+ strlen(auth_str
);
2670 auth_size
= sizeof(auth_str
) - (auth_key
- auth_str
);
2672 for (name
= (char *)cupsArrayFirst(con
->best
->names
);
2674 name
= (char *)cupsArrayNext(con
->best
->names
))
2676 #ifdef HAVE_AUTHORIZATION_H
2677 if (!_cups_strncasecmp(name
, "@AUTHKEY(", 9))
2679 snprintf(auth_key
, auth_size
, ", authkey=\"%s\"", name
+ 9);
2680 /* end parenthesis is stripped in conf.c */
2684 #endif /* HAVE_AUTHORIZATION_H */
2685 if (!_cups_strcasecmp(name
, "@SYSTEM"))
2687 #ifdef HAVE_AUTHORIZATION_H
2688 if (SystemGroupAuthKey
)
2689 snprintf(auth_key
, auth_size
,
2691 SystemGroupAuthKey
);
2694 strlcpy(auth_key
, ", trc=\"y\"", auth_size
);
2695 #endif /* HAVE_AUTHORIZATION_H */
2703 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2704 "[Client %d] WWW-Authenticate: %s", con
->http
.fd
,
2707 if (httpPrintf(HTTP(con
), "WWW-Authenticate: %s\r\n", auth_str
) < 0)
2712 if (con
->language
&& strcmp(con
->language
->language
, "C"))
2714 if (httpPrintf(HTTP(con
), "Content-Language: %s\r\n",
2715 con
->language
->language
) < 0)
2721 if (!strcmp(type
, "text/html"))
2723 if (httpPrintf(HTTP(con
),
2724 "Content-Type: text/html; charset=utf-8\r\n") < 0)
2727 else if (httpPrintf(HTTP(con
), "Content-Type: %s\r\n", type
) < 0)
2736 * 'cupsdUpdateCGI()' - Read status messages from CGI scripts and programs.
2740 cupsdUpdateCGI(void)
2742 char *ptr
, /* Pointer to end of line in buffer */
2743 message
[1024]; /* Pointer to message text */
2744 int loglevel
; /* Log level for message */
2747 while ((ptr
= cupsdStatBufUpdate(CGIStatusBuffer
, &loglevel
,
2748 message
, sizeof(message
))) != NULL
)
2750 if (loglevel
== CUPSD_LOG_INFO
)
2751 cupsdLogMessage(CUPSD_LOG_INFO
, "%s", message
);
2753 if (!strchr(CGIStatusBuffer
->buffer
, '\n'))
2757 if (ptr
== NULL
&& !CGIStatusBuffer
->bufused
)
2760 * Fatal error on pipe - should never happen!
2763 cupsdLogMessage(CUPSD_LOG_CRIT
,
2764 "cupsdUpdateCGI: error reading from CGI error pipe - %s",
2771 * 'cupsdWriteClient()' - Write data to a client as needed.
2775 cupsdWriteClient(cupsd_client_t
*con
) /* I - Client connection */
2777 int bytes
, /* Number of bytes written */
2778 field_col
; /* Current column */
2779 char *bufptr
, /* Pointer into buffer */
2780 *bufend
; /* Pointer to end of buffer */
2781 ipp_state_t ipp_state
; /* IPP state value */
2784 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2785 "[Client %d] cupsdWriteClient "
2789 "data_encoding=HTTP_ENCODE_%s, "
2790 "data_remaining=" CUPS_LLFMT
", "
2794 con
->http
.fd
, con
->http
.error
, con
->http
.used
,
2795 http_states
[con
->http
.state
],
2796 con
->http
.data_encoding
== HTTP_ENCODE_CHUNKED
?
2797 "CHUNKED" : "LENGTH",
2798 CUPS_LLCAST con
->http
.data_remaining
,
2800 con
->response
? ipp_states
[con
->response
->state
] : "",
2801 con
->pipe_pid
, con
->file
);
2803 if (con
->http
.state
!= HTTP_GET_SEND
&&
2804 con
->http
.state
!= HTTP_POST_SEND
)
2807 * If we get called in the wrong state, then something went wrong with the
2808 * connection and we need to shut it down...
2811 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2812 "[Client %d] Closing on unexpected HTTP state %s.",
2813 con
->http
.fd
, http_states
[con
->http
.state
]);
2814 cupsdCloseClient(con
);
2821 * Make sure we select on the CGI output...
2824 cupsdAddSelect(con
->file
, (cupsd_selfunc_t
)write_pipe
, NULL
, con
);
2826 if (!con
->file_ready
)
2829 * Try again later when there is CGI output available...
2832 cupsdRemoveSelect(con
->http
.fd
);
2836 con
->file_ready
= 0;
2839 if (con
->response
&& con
->response
->state
!= IPP_DATA
)
2841 ipp_state
= ippWrite(HTTP(con
), con
->response
);
2842 bytes
= ipp_state
!= IPP_ERROR
&&
2843 (con
->file
>= 0 || ipp_state
!= IPP_DATA
);
2845 else if ((bytes
= read(con
->file
, con
->header
+ con
->header_used
,
2846 sizeof(con
->header
) - con
->header_used
)) > 0)
2848 con
->header_used
+= bytes
;
2850 if (con
->pipe_pid
&& !con
->got_fields
)
2853 * Inspect the data for Content-Type and other fields.
2856 for (bufptr
= con
->header
, bufend
= con
->header
+ con
->header_used
,
2858 !con
->got_fields
&& bufptr
< bufend
;
2861 if (*bufptr
== '\n')
2864 * Send line to client...
2867 if (bufptr
> con
->header
&& bufptr
[-1] == '\r')
2871 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Script header: %s", con
->header
);
2873 if (!con
->sent_header
)
2876 * Handle redirection and CGI status codes...
2879 if (!_cups_strncasecmp(con
->header
, "Location:", 9))
2881 if (!cupsdSendHeader(con
, HTTP_SEE_OTHER
, NULL
, CUPSD_AUTH_NONE
))
2883 cupsdCloseClient(con
);
2887 con
->sent_header
= 2;
2889 if (httpPrintf(HTTP(con
), "Content-Length: 0\r\n") < 0)
2892 else if (!_cups_strncasecmp(con
->header
, "Status:", 7))
2894 cupsdSendError(con
, (http_status_t
)atoi(con
->header
+ 7),
2896 con
->sent_header
= 2;
2900 if (!cupsdSendHeader(con
, HTTP_OK
, NULL
, CUPSD_AUTH_NONE
))
2902 cupsdCloseClient(con
);
2906 con
->sent_header
= 1;
2908 if (con
->http
.version
== HTTP_1_1
)
2910 if (httpPrintf(HTTP(con
), "Transfer-Encoding: chunked\r\n") < 0)
2916 if (_cups_strncasecmp(con
->header
, "Status:", 7))
2917 httpPrintf(HTTP(con
), "%s\r\n", con
->header
);
2923 con
->header_used
-= bufptr
- con
->header
;
2925 if (con
->header_used
> 0)
2926 memmove(con
->header
, bufptr
, con
->header_used
);
2928 bufptr
= con
->header
- 1;
2931 * See if the line was empty...
2936 con
->got_fields
= 1;
2938 if (cupsdFlushHeader(con
) < 0)
2940 cupsdCloseClient(con
);
2944 if (con
->http
.version
== HTTP_1_1
)
2945 con
->http
.data_encoding
= HTTP_ENCODE_CHUNKED
;
2950 else if (*bufptr
!= '\r')
2954 if (!con
->got_fields
)
2956 con
->http
.activity
= time(NULL
);
2961 if (con
->header_used
> 0)
2963 if (httpWrite2(HTTP(con
), con
->header
, con
->header_used
) < 0)
2965 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2966 "[Client %d] Closing for error %d (%s)",
2967 con
->http
.fd
, con
->http
.error
,
2968 strerror(con
->http
.error
));
2969 cupsdCloseClient(con
);
2973 if (con
->http
.data_encoding
== HTTP_ENCODE_CHUNKED
)
2974 httpFlushWrite(HTTP(con
));
2976 con
->bytes
+= con
->header_used
;
2978 if (con
->http
.state
== HTTP_WAITING
)
2981 bytes
= con
->header_used
;
2983 con
->header_used
= 0;
2988 (con
->http
.state
!= HTTP_GET_SEND
&& con
->http
.state
!= HTTP_POST_SEND
))
2990 if (!con
->sent_header
&& con
->pipe_pid
)
2991 cupsdSendError(con
, HTTP_SERVER_ERROR
, CUPSD_AUTH_NONE
);
2994 cupsdLogRequest(con
, HTTP_OK
);
2996 httpFlushWrite(HTTP(con
));
2998 if (con
->http
.data_encoding
== HTTP_ENCODE_CHUNKED
&& con
->sent_header
== 1)
3000 if (httpWrite2(HTTP(con
), "", 0) < 0)
3002 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3003 "[Client %d] Closing for error %d (%s)",
3004 con
->http
.fd
, con
->http
.error
,
3005 strerror(con
->http
.error
));
3006 cupsdCloseClient(con
);
3012 con
->http
.state
= HTTP_WAITING
;
3014 cupsdAddSelect(con
->http
.fd
, (cupsd_selfunc_t
)cupsdReadClient
, NULL
, con
);
3018 cupsdRemoveSelect(con
->file
);
3021 cupsdEndProcess(con
->pipe_pid
, 0);
3030 unlink(con
->filename
);
3031 cupsdClearString(&con
->filename
);
3036 ippDelete(con
->request
);
3037 con
->request
= NULL
;
3042 ippDelete(con
->response
);
3043 con
->response
= NULL
;
3046 cupsdClearString(&con
->command
);
3047 cupsdClearString(&con
->options
);
3048 cupsdClearString(&con
->query_string
);
3050 if (!con
->http
.keep_alive
)
3052 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3053 "[Client %d] Closing because Keep-Alive disabled.",
3055 cupsdCloseClient(con
);
3060 cupsArrayRemove(ActiveClients
, con
);
3061 cupsdSetBusyState();
3065 con
->http
.activity
= time(NULL
);
3070 * 'check_if_modified()' - Decode an "If-Modified-Since" line.
3073 static int /* O - 1 if modified since */
3075 cupsd_client_t
*con
, /* I - Client connection */
3076 struct stat
*filestats
) /* I - File information */
3078 char *ptr
; /* Pointer into field */
3079 time_t date
; /* Time/date value */
3080 off_t size
; /* Size/length value */
3085 ptr
= con
->http
.fields
[HTTP_FIELD_IF_MODIFIED_SINCE
];
3090 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3091 "[Client %d] check_if_modified "
3092 "filestats=%p(" CUPS_LLFMT
", %d)) If-Modified-Since=\"%s\"",
3093 con
->http
.fd
, filestats
, CUPS_LLCAST filestats
->st_size
,
3094 (int)filestats
->st_mtime
, ptr
);
3096 while (*ptr
!= '\0')
3098 while (isspace(*ptr
) || *ptr
== ';')
3101 if (_cups_strncasecmp(ptr
, "length=", 7) == 0)
3104 size
= strtoll(ptr
, NULL
, 10);
3106 while (isdigit(*ptr
))
3109 else if (isalpha(*ptr
))
3111 date
= httpGetDateTime(ptr
);
3112 while (*ptr
!= '\0' && *ptr
!= ';')
3119 return ((size
!= filestats
->st_size
&& size
!= 0) ||
3120 (date
< filestats
->st_mtime
&& date
!= 0) ||
3121 (size
== 0 && date
== 0));
3126 * 'compare_clients()' - Compare two client connections.
3129 static int /* O - Result of comparison */
3130 compare_clients(cupsd_client_t
*a
, /* I - First client */
3131 cupsd_client_t
*b
, /* I - Second client */
3132 void *data
) /* I - User data (not used) */
3147 * 'copy_cdsa_certificate()' - Copy a SSL/TLS certificate from the System
3151 static CFArrayRef
/* O - Array of certificates */
3152 copy_cdsa_certificate(
3153 cupsd_client_t
*con
) /* I - Client connection */
3155 OSStatus err
; /* Error info */
3156 SecKeychainRef keychain
= NULL
;/* Keychain reference */
3157 SecIdentitySearchRef search
= NULL
; /* Search reference */
3158 SecIdentityRef identity
= NULL
;/* Identity */
3159 CFArrayRef certificates
= NULL
;
3160 /* Certificate array */
3161 # if HAVE_SECPOLICYCREATESSL
3162 SecPolicyRef policy
= NULL
; /* Policy ref */
3163 CFStringRef servername
= NULL
;
3165 CFMutableDictionaryRef query
= NULL
; /* Query qualifiers */
3166 char localname
[1024];/* Local hostname */
3167 # elif defined(HAVE_SECIDENTITYSEARCHCREATEWITHPOLICY)
3168 SecPolicyRef policy
= NULL
; /* Policy ref */
3169 SecPolicySearchRef policy_search
= NULL
;
3170 /* Policy search ref */
3171 CSSM_DATA options
; /* Policy options */
3172 CSSM_APPLE_TP_SSL_OPTIONS
3173 ssl_options
; /* SSL Option for hostname */
3174 char localname
[1024];/* Local hostname */
3175 # endif /* HAVE_SECPOLICYCREATESSL */
3178 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3179 "copy_cdsa_certificate: Looking for certs for \"%s\"...",
3182 if ((err
= SecKeychainOpen(ServerCertificate
, &keychain
)))
3184 cupsdLogMessage(CUPSD_LOG_ERROR
, "Cannot open keychain \"%s\" - %s (%d)",
3185 ServerCertificate
, cssmErrorString(err
), (int)err
);
3189 # if HAVE_SECPOLICYCREATESSL
3190 servername
= CFStringCreateWithCString(kCFAllocatorDefault
, con
->servername
,
3191 kCFStringEncodingUTF8
);
3193 policy
= SecPolicyCreateSSL(1, servername
);
3196 CFRelease(servername
);
3200 cupsdLogMessage(CUPSD_LOG_ERROR
, "Cannot create ssl policy reference");
3204 if (!(query
= CFDictionaryCreateMutable(kCFAllocatorDefault
, 0,
3205 &kCFTypeDictionaryKeyCallBacks
,
3206 &kCFTypeDictionaryValueCallBacks
)))
3208 cupsdLogMessage(CUPSD_LOG_ERROR
, "Cannot create query dictionary");
3212 CFDictionaryAddValue(query
, kSecClass
, kSecClassIdentity
);
3213 CFDictionaryAddValue(query
, kSecMatchPolicy
, policy
);
3214 CFDictionaryAddValue(query
, kSecReturnRef
, kCFBooleanTrue
);
3215 CFDictionaryAddValue(query
, kSecMatchLimit
, kSecMatchLimitOne
);
3217 err
= SecItemCopyMatching(query
, (CFTypeRef
*)&identity
);
3219 if (err
&& DNSSDHostName
)
3222 * Search for the connection server name failed; try the DNS-SD .local
3223 * hostname instead...
3226 snprintf(localname
, sizeof(localname
), "%s.local", DNSSDHostName
);
3228 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3229 "copy_cdsa_certificate: Looking for certs for \"%s\"...",
3232 servername
= CFStringCreateWithCString(kCFAllocatorDefault
, localname
,
3233 kCFStringEncodingUTF8
);
3237 policy
= SecPolicyCreateSSL(1, servername
);
3240 CFRelease(servername
);
3244 cupsdLogMessage(CUPSD_LOG_ERROR
, "Cannot create ssl policy reference");
3248 CFDictionarySetValue(query
, kSecMatchPolicy
, policy
);
3250 err
= SecItemCopyMatching(query
, (CFTypeRef
*)&identity
);
3255 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3256 "Cannot find signing key in keychain \"%s\": %s (%d)",
3257 ServerCertificate
, cssmErrorString(err
), (int)err
);
3261 # elif defined(HAVE_SECIDENTITYSEARCHCREATEWITHPOLICY)
3263 * Use a policy to search for valid certificates whose common name matches the
3267 if (SecPolicySearchCreate(CSSM_CERT_X_509v3
, &CSSMOID_APPLE_TP_SSL
,
3268 NULL
, &policy_search
))
3270 cupsdLogMessage(CUPSD_LOG_ERROR
, "Cannot create a policy search reference");
3274 if (SecPolicySearchCopyNext(policy_search
, &policy
))
3276 cupsdLogMessage(CUPSD_LOG_ERROR
,
3277 "Cannot find a policy to use for searching");
3281 memset(&ssl_options
, 0, sizeof(ssl_options
));
3282 ssl_options
.Version
= CSSM_APPLE_TP_SSL_OPTS_VERSION
;
3283 ssl_options
.ServerName
= con
->servername
;
3284 ssl_options
.ServerNameLen
= strlen(con
->servername
);
3286 options
.Data
= (uint8
*)&ssl_options
;
3287 options
.Length
= sizeof(ssl_options
);
3289 if (SecPolicySetValue(policy
, &options
))
3291 cupsdLogMessage(CUPSD_LOG_ERROR
,
3292 "Cannot set policy value to use for searching");
3296 if ((err
= SecIdentitySearchCreateWithPolicy(policy
, NULL
, CSSM_KEYUSE_SIGN
,
3297 keychain
, FALSE
, &search
)))
3299 cupsdLogMessage(CUPSD_LOG_ERROR
,
3300 "Cannot create identity search reference: %s (%d)",
3301 cssmErrorString(err
), (int)err
);
3305 err
= SecIdentitySearchCopyNext(search
, &identity
);
3307 if (err
&& DNSSDHostName
)
3310 * Search for the connection server name failed; try the DNS-SD .local
3311 * hostname instead...
3314 snprintf(localname
, sizeof(localname
), "%s.local", DNSSDHostName
);
3316 ssl_options
.ServerName
= localname
;
3317 ssl_options
.ServerNameLen
= strlen(localname
);
3319 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3320 "copy_cdsa_certificate: Looking for certs for \"%s\"...",
3323 if (SecPolicySetValue(policy
, &options
))
3325 cupsdLogMessage(CUPSD_LOG_ERROR
,
3326 "Cannot set policy value to use for searching");
3332 if ((err
= SecIdentitySearchCreateWithPolicy(policy
, NULL
, CSSM_KEYUSE_SIGN
,
3333 keychain
, FALSE
, &search
)))
3335 cupsdLogMessage(CUPSD_LOG_ERROR
,
3336 "Cannot create identity search reference: %s (%d)",
3337 cssmErrorString(err
), (int)err
);
3341 err
= SecIdentitySearchCopyNext(search
, &identity
);
3347 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3348 "Cannot find signing key in keychain \"%s\": %s (%d)",
3349 ServerCertificate
, cssmErrorString(err
), (int)err
);
3355 * Assume there is exactly one SecIdentity in the keychain...
3358 if ((err
= SecIdentitySearchCreate(keychain
, CSSM_KEYUSE_SIGN
, &search
)))
3360 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3361 "Cannot create identity search reference (%d)", (int)err
);
3365 if ((err
= SecIdentitySearchCopyNext(search
, &identity
)))
3367 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3368 "Cannot find signing key in keychain \"%s\": %s (%d)",
3369 ServerCertificate
, cssmErrorString(err
), (int)err
);
3372 # endif /* HAVE_SECPOLICYCREATESSL */
3374 if (CFGetTypeID(identity
) != SecIdentityGetTypeID())
3376 cupsdLogMessage(CUPSD_LOG_ERROR
, "SecIdentity CFTypeID failure!");
3380 if ((certificates
= CFArrayCreate(NULL
, (const void **)&identity
,
3381 1, &kCFTypeArrayCallBacks
)) == NULL
)
3383 cupsdLogMessage(CUPSD_LOG_ERROR
, "Cannot create certificate array");
3390 CFRelease(keychain
);
3394 CFRelease(identity
);
3396 # if HAVE_SECPOLICYCREATESSL
3401 # elif defined(HAVE_SECIDENTITYSEARCHCREATEWITHPOLICY)
3405 CFRelease(policy_search
);
3406 # endif /* HAVE_SECPOLICYCREATESSL */
3408 return (certificates
);
3410 #endif /* HAVE_CDSASSL */
3414 * 'data_ready()' - Check whether data is available from a client.
3417 static int /* O - 1 if data is ready, 0 otherwise */
3418 data_ready(cupsd_client_t
*con
) /* I - Client */
3420 if (con
->http
.used
> 0)
3423 else if (con
->http
.tls
)
3426 if (SSL_pending((SSL
*)(con
->http
.tls
)))
3428 # elif defined(HAVE_GNUTLS)
3429 if (gnutls_record_check_pending(con
->http
.tls
))
3431 # elif defined(HAVE_CDSASSL)
3432 size_t bytes
; /* Bytes that are available */
3434 if (!SSLGetBufferedReadSize(con
->http
.tls
, &bytes
) && bytes
> 0)
3436 # endif /* HAVE_LIBSSL */
3438 #endif /* HAVE_SSL */
3446 * 'encrypt_client()' - Enable encryption for the client...
3449 static int /* O - 1 on success, 0 on error */
3450 encrypt_client(cupsd_client_t
*con
) /* I - Client to encrypt */
3453 SSL_CTX
*context
; /* Context for encryption */
3454 BIO
*bio
; /* BIO data */
3455 unsigned long error
; /* Error code */
3458 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Client %d] Encrypting connection.",
3462 * Verify that we have a certificate...
3465 if (access(ServerKey
, 0) || access(ServerCertificate
, 0))
3468 * Nope, make a self-signed certificate...
3471 if (!make_certificate(con
))
3476 * Create the SSL context and accept the connection...
3479 context
= SSL_CTX_new(SSLv23_server_method());
3481 SSL_CTX_set_options(context
, SSL_OP_NO_SSLv2
); /* Only use SSLv3 or TLS */
3482 if (SSLOptions
& CUPSD_SSL_NOEMPTY
)
3483 SSL_CTX_set_options(context
, SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS
);
3484 SSL_CTX_use_PrivateKey_file(context
, ServerKey
, SSL_FILETYPE_PEM
);
3485 SSL_CTX_use_certificate_chain_file(context
, ServerCertificate
);
3487 bio
= BIO_new(_httpBIOMethods());
3488 BIO_ctrl(bio
, BIO_C_SET_FILE_PTR
, 0, (char *)HTTP(con
));
3490 con
->http
.tls
= SSL_new(context
);
3491 SSL_set_bio(con
->http
.tls
, bio
, bio
);
3493 if (SSL_accept(con
->http
.tls
) != 1)
3495 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to encrypt connection from %s!",
3496 con
->http
.hostname
);
3498 while ((error
= ERR_get_error()) != 0)
3499 cupsdLogMessage(CUPSD_LOG_ERROR
, "%s", ERR_error_string(error
, NULL
));
3501 SSL_CTX_free(context
);
3502 SSL_free(con
->http
.tls
);
3503 con
->http
.tls
= NULL
;
3507 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Connection from %s now encrypted.",
3508 con
->http
.hostname
);
3512 # elif defined(HAVE_GNUTLS)
3513 int status
; /* Error code */
3514 gnutls_certificate_server_credentials
*credentials
;
3515 /* TLS credentials */
3516 const char *priority
; /* Priority string */
3519 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Client %d] Encrypting connection.",
3523 * Verify that we have a certificate...
3526 if (access(ServerKey
, 0) || access(ServerCertificate
, 0))
3529 * Nope, make a self-signed certificate...
3532 if (!make_certificate(con
))
3537 * Create the SSL object and perform the SSL handshake...
3540 credentials
= (gnutls_certificate_server_credentials
*)
3541 malloc(sizeof(gnutls_certificate_server_credentials
));
3542 if (credentials
== NULL
)
3544 cupsdLogMessage(CUPSD_LOG_ERROR
,
3545 "Unable to encrypt connection from %s - %s",
3546 con
->http
.hostname
, strerror(errno
));
3551 gnutls_certificate_allocate_credentials(credentials
);
3552 gnutls_certificate_set_x509_key_file(*credentials
, ServerCertificate
,
3553 ServerKey
, GNUTLS_X509_FMT_PEM
);
3555 gnutls_init(&con
->http
.tls
, GNUTLS_SERVER
);
3556 gnutls_set_default_priority(con
->http
.tls
);
3557 status
= gnutls_priority_set_direct(con
->http
.tls
,
3558 "NORMAL:-VERS-TLS-ALL:+VERS-TLS1.0:"
3559 "+VERS-SSL3.0:%COMPAT", &priority
);
3560 if (status
!= GNUTLS_E_SUCCESS
)
3562 cupsdLogMessage(CUPSD_LOG_ERROR
,
3563 "Unable to encrypt connection from %s - %s (%s)",
3564 con
->http
.hostname
, gnutls_strerror(status
), priority
);
3566 gnutls_deinit(con
->http
.tls
);
3567 gnutls_certificate_free_credentials(*credentials
);
3568 con
->http
.tls
= NULL
;
3573 gnutls_credentials_set(con
->http
.tls
, GNUTLS_CRD_CERTIFICATE
, *credentials
);
3574 gnutls_transport_set_ptr(con
->http
.tls
, (gnutls_transport_ptr
)HTTP(con
));
3575 gnutls_transport_set_pull_function(con
->http
.tls
, _httpReadGNUTLS
);
3576 gnutls_transport_set_push_function(con
->http
.tls
, _httpWriteGNUTLS
);
3578 while ((status
= gnutls_handshake(con
->http
.tls
)) != GNUTLS_E_SUCCESS
)
3580 if (gnutls_error_is_fatal(status
))
3582 cupsdLogMessage(CUPSD_LOG_ERROR
,
3583 "Unable to encrypt connection from %s - %s",
3584 con
->http
.hostname
, gnutls_strerror(status
));
3586 gnutls_deinit(con
->http
.tls
);
3587 gnutls_certificate_free_credentials(*credentials
);
3588 con
->http
.tls
= NULL
;
3594 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Connection from %s now encrypted.",
3595 con
->http
.hostname
);
3597 con
->http
.tls_credentials
= credentials
;
3600 # elif defined(HAVE_CDSASSL)
3601 OSStatus error
= 0; /* Error code */
3602 CFArrayRef peerCerts
; /* Peer certificates */
3605 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Client %d] Encrypting connection.",
3608 con
->http
.tls_credentials
= copy_cdsa_certificate(con
);
3610 if (!con
->http
.tls_credentials
)
3613 * No keychain (yet), make a self-signed certificate...
3616 if (make_certificate(con
))
3617 con
->http
.tls_credentials
= copy_cdsa_certificate(con
);
3620 if (!con
->http
.tls_credentials
)
3622 cupsdLogMessage(CUPSD_LOG_ERROR
,
3623 "Could not find signing key in keychain \"%s\"",
3625 error
= errSSLBadCert
; /* errSSLBadConfiguration is a better choice, but not available on 10.2.x */
3629 error
= SSLNewContext(true, &con
->http
.tls
);
3632 error
= SSLSetIOFuncs(con
->http
.tls
, _httpReadCDSA
, _httpWriteCDSA
);
3635 error
= SSLSetConnection(con
->http
.tls
, HTTP(con
));
3638 error
= SSLSetAllowsExpiredCerts(con
->http
.tls
, true);
3641 error
= SSLSetAllowsAnyRoot(con
->http
.tls
, true);
3644 error
= SSLSetCertificate(con
->http
.tls
, con
->http
.tls_credentials
);
3649 * Perform SSL/TLS handshake
3652 while ((error
= SSLHandshake(con
->http
.tls
)) == errSSLWouldBlock
)
3658 cupsdLogMessage(CUPSD_LOG_ERROR
,
3659 "Unable to encrypt connection from %s - %s (%d)",
3660 con
->http
.hostname
, cssmErrorString(error
), (int)error
);
3662 con
->http
.error
= error
;
3663 con
->http
.status
= HTTP_ERROR
;
3667 SSLDisposeContext(con
->http
.tls
);
3668 con
->http
.tls
= NULL
;
3671 if (con
->http
.tls_credentials
)
3673 CFRelease(con
->http
.tls_credentials
);
3674 con
->http
.tls_credentials
= NULL
;
3680 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Connection from %s now encrypted.",
3681 con
->http
.hostname
);
3683 if (!SSLCopyPeerCertificates(con
->http
.tls
, &peerCerts
) && peerCerts
)
3685 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Received %d peer certificates!",
3686 (int)CFArrayGetCount(peerCerts
));
3687 CFRelease(peerCerts
);
3690 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Received NO peer certificates!");
3694 # endif /* HAVE_LIBSSL */
3696 #endif /* HAVE_SSL */
3700 * 'get_file()' - Get a filename and state info.
3703 static char * /* O - Real filename */
3704 get_file(cupsd_client_t
*con
, /* I - Client connection */
3705 struct stat
*filestats
, /* O - File information */
3706 char *filename
, /* IO - Filename buffer */
3707 int len
) /* I - Buffer length */
3709 int status
; /* Status of filesystem calls */
3710 char *ptr
; /* Pointer info filename */
3711 int plen
; /* Remaining length after pointer */
3712 char language
[7]; /* Language subdirectory, if any */
3716 * Figure out the real filename...
3721 if (!strncmp(con
->uri
, "/ppd/", 5) && !strchr(con
->uri
+ 5, '/'))
3722 snprintf(filename
, len
, "%s%s", ServerRoot
, con
->uri
);
3723 else if (!strncmp(con
->uri
, "/icons/", 7) && !strchr(con
->uri
+ 7, '/'))
3725 snprintf(filename
, len
, "%s/%s", CacheDir
, con
->uri
+ 7);
3726 if (access(filename
, F_OK
) < 0)
3727 snprintf(filename
, len
, "%s/images/generic.png", DocumentRoot
);
3729 else if (!strncmp(con
->uri
, "/rss/", 5) && !strchr(con
->uri
+ 5, '/'))
3730 snprintf(filename
, len
, "%s/rss/%s", CacheDir
, con
->uri
+ 5);
3731 else if (!strncmp(con
->uri
, "/admin/conf/", 12))
3732 snprintf(filename
, len
, "%s%s", ServerRoot
, con
->uri
+ 11);
3733 else if (!strncmp(con
->uri
, "/admin/log/", 11))
3735 if (!strncmp(con
->uri
+ 11, "access_log", 10) && AccessLog
[0] == '/')
3736 strlcpy(filename
, AccessLog
, len
);
3737 else if (!strncmp(con
->uri
+ 11, "error_log", 9) && ErrorLog
[0] == '/')
3738 strlcpy(filename
, ErrorLog
, len
);
3739 else if (!strncmp(con
->uri
+ 11, "page_log", 8) && PageLog
[0] == '/')
3740 strlcpy(filename
, PageLog
, len
);
3744 else if (con
->language
)
3746 snprintf(language
, sizeof(language
), "/%s", con
->language
->language
);
3747 snprintf(filename
, len
, "%s%s%s", DocumentRoot
, language
, con
->uri
);
3750 snprintf(filename
, len
, "%s%s", DocumentRoot
, con
->uri
);
3752 if ((ptr
= strchr(filename
, '?')) != NULL
)
3756 * Grab the status for this language; if there isn't a language-specific file
3757 * then fallback to the default one...
3760 if ((status
= stat(filename
, filestats
)) != 0 && language
[0] &&
3761 strncmp(con
->uri
, "/icons/", 7) &&
3762 strncmp(con
->uri
, "/ppd/", 5) &&
3763 strncmp(con
->uri
, "/rss/", 5) &&
3764 strncmp(con
->uri
, "/admin/conf/", 12) &&
3765 strncmp(con
->uri
, "/admin/log/", 11))
3768 * Drop the country code...
3772 snprintf(filename
, len
, "%s%s%s", DocumentRoot
, language
, con
->uri
);
3774 if ((ptr
= strchr(filename
, '?')) != NULL
)
3777 if ((status
= stat(filename
, filestats
)) != 0)
3780 * Drop the language prefix and try the root directory...
3784 snprintf(filename
, len
, "%s%s", DocumentRoot
, con
->uri
);
3786 if ((ptr
= strchr(filename
, '?')) != NULL
)
3789 status
= stat(filename
, filestats
);
3794 * If we're found a directory, get the index.html file instead...
3797 if (!status
&& S_ISDIR(filestats
->st_mode
))
3800 * Make sure the URI ends with a slash...
3803 if (con
->uri
[strlen(con
->uri
) - 1] != '/')
3804 strlcat(con
->uri
, "/", sizeof(con
->uri
));
3807 * Find the directory index file, trying every language...
3812 if (status
&& language
[0])
3815 * Try a different language subset...
3819 language
[0] = '\0'; /* Strip country code */
3821 language
[0] = '\0'; /* Strip language */
3825 * Look for the index file...
3828 snprintf(filename
, len
, "%s%s%s", DocumentRoot
, language
, con
->uri
);
3830 if ((ptr
= strchr(filename
, '?')) != NULL
)
3833 ptr
= filename
+ strlen(filename
);
3834 plen
= len
- (ptr
- filename
);
3836 strlcpy(ptr
, "index.html", plen
);
3837 status
= stat(filename
, filestats
);
3842 strlcpy(ptr
, "index.class", plen
);
3843 status
= stat(filename
, filestats
);
3845 #endif /* HAVE_JAVA */
3850 strlcpy(ptr
, "index.pl", plen
);
3851 status
= stat(filename
, filestats
);
3853 #endif /* HAVE_PERL */
3858 strlcpy(ptr
, "index.php", plen
);
3859 status
= stat(filename
, filestats
);
3861 #endif /* HAVE_PHP */
3866 strlcpy(ptr
, "index.pyc", plen
);
3867 status
= stat(filename
, filestats
);
3872 strlcpy(ptr
, "index.py", plen
);
3873 status
= stat(filename
, filestats
);
3875 #endif /* HAVE_PYTHON */
3878 while (status
&& language
[0]);
3881 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3882 "[Client %d] get_file filestats=%p, filename=%p, len=%d, "
3883 "returning \"%s\".", con
->http
.fd
, filestats
, filename
, len
,
3884 status
? "(null)" : filename
);
3894 * 'install_conf_file()' - Install a configuration file.
3897 static http_status_t
/* O - Status */
3898 install_conf_file(cupsd_client_t
*con
) /* I - Connection */
3900 char filename
[1024]; /* Configuration filename */
3901 mode_t mode
; /* Permissions */
3902 cups_file_t
*in
, /* Input file */
3903 *out
; /* Output file */
3904 char buffer
[16384]; /* Copy buffer */
3905 ssize_t bytes
; /* Number of bytes */
3909 * Open the request file...
3912 if ((in
= cupsFileOpen(con
->filename
, "rb")) == NULL
)
3914 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to open request file \"%s\": %s",
3915 con
->filename
, strerror(errno
));
3916 return (HTTP_SERVER_ERROR
);
3920 * Open the new config file...
3923 snprintf(filename
, sizeof(filename
), "%s%s", ServerRoot
, con
->uri
+ 11);
3924 if (!strcmp(con
->uri
, "/admin/conf/printers.conf"))
3925 mode
= ConfigFilePerm
& 0600;
3927 mode
= ConfigFilePerm
;
3929 if ((out
= cupsdCreateConfFile(filename
, mode
)) == NULL
)
3932 return (HTTP_SERVER_ERROR
);
3935 cupsdLogMessage(CUPSD_LOG_INFO
, "Installing config file \"%s\"...", filename
);
3938 * Copy from the request to the new config file...
3941 while ((bytes
= cupsFileRead(in
, buffer
, sizeof(buffer
))) > 0)
3942 if (cupsFileWrite(out
, buffer
, bytes
) < bytes
)
3944 cupsdLogMessage(CUPSD_LOG_ERROR
,
3945 "Unable to copy to config file \"%s\": %s",
3946 filename
, strerror(errno
));
3951 snprintf(filename
, sizeof(filename
), "%s%s.N", ServerRoot
, con
->uri
+ 11);
3952 cupsdRemoveFile(filename
);
3954 return (HTTP_SERVER_ERROR
);
3958 * Close the files...
3963 if (cupsdCloseCreatedConfFile(out
, filename
))
3964 return (HTTP_SERVER_ERROR
);
3967 * Remove the request file...
3970 cupsdRemoveFile(con
->filename
);
3971 cupsdClearString(&con
->filename
);
3974 * If the cupsd.conf file was updated, set the NeedReload flag...
3977 if (!strcmp(con
->uri
, "/admin/conf/cupsd.conf"))
3978 NeedReload
= RELOAD_CUPSD
;
3980 NeedReload
= RELOAD_ALL
;
3982 ReloadTime
= time(NULL
);
3985 * Return that the file was created successfully...
3988 return (HTTP_CREATED
);
3993 * 'is_cgi()' - Is the resource a CGI script/program?
3996 static int /* O - 1 = CGI, 0 = file */
3997 is_cgi(cupsd_client_t
*con
, /* I - Client connection */
3998 const char *filename
, /* I - Real filename */
3999 struct stat
*filestats
, /* I - File information */
4000 mime_type_t
*type
) /* I - MIME type */
4002 const char *options
; /* Options on URL */
4006 * Get the options, if any...
4009 if ((options
= strchr(con
->uri
, '?')) != NULL
)
4012 cupsdSetStringf(&(con
->query_string
), "QUERY_STRING=%s", options
);
4016 * Check for known types...
4019 if (!type
|| _cups_strcasecmp(type
->super
, "application"))
4021 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4022 "[Client %d] is_cgi filename=\"%s\", filestats=%p, "
4023 "type=%s/%s, returning 0", con
->http
.fd
, filename
,
4024 filestats
, type
? type
->super
: "unknown",
4025 type
? type
->type
: "unknown");
4029 if (!_cups_strcasecmp(type
->type
, "x-httpd-cgi") &&
4030 (filestats
->st_mode
& 0111))
4033 * "application/x-httpd-cgi" is a CGI script.
4036 cupsdSetString(&con
->command
, filename
);
4039 cupsdSetStringf(&con
->options
, " %s", options
);
4041 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4042 "[Client %d] is_cgi filename=\"%s\", filestats=%p, "
4043 "type=%s/%s, returning 1", con
->http
.fd
, filename
,
4044 filestats
, type
->super
, type
->type
);
4048 else if (!_cups_strcasecmp(type
->type
, "x-httpd-java"))
4051 * "application/x-httpd-java" is a Java servlet.
4054 cupsdSetString(&con
->command
, CUPS_JAVA
);
4057 cupsdSetStringf(&con
->options
, " %s %s", filename
, options
);
4059 cupsdSetStringf(&con
->options
, " %s", filename
);
4061 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4062 "[Client %d] is_cgi filename=\"%s\", filestats=%p, "
4063 "type=%s/%s, returning 1", con
->http
.fd
, filename
,
4064 filestats
, type
->super
, type
->type
);
4067 #endif /* HAVE_JAVA */
4069 else if (!_cups_strcasecmp(type
->type
, "x-httpd-perl"))
4072 * "application/x-httpd-perl" is a Perl page.
4075 cupsdSetString(&con
->command
, CUPS_PERL
);
4078 cupsdSetStringf(&con
->options
, " %s %s", filename
, options
);
4080 cupsdSetStringf(&con
->options
, " %s", filename
);
4082 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4083 "[Client %d] is_cgi filename=\"%s\", filestats=%p, "
4084 "type=%s/%s, returning 1", con
->http
.fd
, filename
,
4085 filestats
, type
->super
, type
->type
);
4088 #endif /* HAVE_PERL */
4090 else if (!_cups_strcasecmp(type
->type
, "x-httpd-php"))
4093 * "application/x-httpd-php" is a PHP page.
4096 cupsdSetString(&con
->command
, CUPS_PHP
);
4099 cupsdSetStringf(&con
->options
, " %s %s", filename
, options
);
4101 cupsdSetStringf(&con
->options
, " %s", filename
);
4103 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4104 "[Client %d] is_cgi filename=\"%s\", filestats=%p, "
4105 "type=%s/%s, returning 1", con
->http
.fd
, filename
,
4106 filestats
, type
->super
, type
->type
);
4109 #endif /* HAVE_PHP */
4111 else if (!_cups_strcasecmp(type
->type
, "x-httpd-python"))
4114 * "application/x-httpd-python" is a Python page.
4117 cupsdSetString(&con
->command
, CUPS_PYTHON
);
4120 cupsdSetStringf(&con
->options
, " %s %s", filename
, options
);
4122 cupsdSetStringf(&con
->options
, " %s", filename
);
4124 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4125 "[Client %d] is_cgi filename=\"%s\", filestats=%p, "
4126 "type=%s/%s, returning 1", con
->http
.fd
, filename
,
4127 filestats
, type
->super
, type
->type
);
4130 #endif /* HAVE_PYTHON */
4132 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4133 "[Client %d] is_cgi filename=\"%s\", filestats=%p, "
4134 "type=%s/%s, returning 0", con
->http
.fd
, filename
,
4135 filestats
, type
->super
, type
->type
);
4141 * 'is_path_absolute()' - Is a path absolute and free of relative elements (i.e. "..").
4144 static int /* O - 0 if relative, 1 if absolute */
4145 is_path_absolute(const char *path
) /* I - Input path */
4148 * Check for a leading slash...
4155 * Check for "/.." in the path...
4158 while ((path
= strstr(path
, "/..")) != NULL
)
4160 if (!path
[3] || path
[3] == '/')
4167 * If we haven't found any relative paths, return 1 indicating an
4177 * 'make_certificate()' - Make a self-signed SSL/TLS certificate.
4180 static int /* O - 1 on success, 0 on failure */
4181 make_certificate(cupsd_client_t
*con
) /* I - Client connection */
4183 #if defined(HAVE_LIBSSL) && defined(HAVE_WAITPID)
4184 int pid
, /* Process ID of command */
4185 status
; /* Status of command */
4186 char command
[1024], /* Command */
4187 *argv
[12], /* Command-line arguments */
4188 *envp
[MAX_ENV
+ 1], /* Environment variables */
4189 infofile
[1024], /* Type-in information for cert */
4190 seedfile
[1024]; /* Random number seed file */
4191 int envc
, /* Number of environment variables */
4192 bytes
; /* Bytes written */
4193 cups_file_t
*fp
; /* Seed/info file */
4194 int infofd
; /* Info file descriptor */
4198 * Run the "openssl" command to seed the random number generator and
4199 * generate a self-signed certificate that is good for 10 years:
4201 * openssl rand -rand seedfile 1
4203 * openssl req -new -x509 -keyout ServerKey \
4204 * -out ServerCertificate -days 3650 -nodes
4206 * The seeding step is crucial in ensuring that the openssl command
4207 * does not block on systems without sufficient entropy...
4210 if (!cupsFileFind("openssl", getenv("PATH"), 1, command
, sizeof(command
)))
4212 cupsdLogMessage(CUPSD_LOG_ERROR
,
4213 "No SSL certificate and openssl command not found!");
4217 if (access("/dev/urandom", 0))
4220 * If the system doesn't provide /dev/urandom, then any random source
4221 * will probably be blocking-style, so generate some random data to
4222 * use as a seed for the certificate. Note that we have already
4223 * seeded the random number generator in cupsdInitCerts()...
4226 cupsdLogMessage(CUPSD_LOG_INFO
,
4227 "Seeding the random number generator...");
4230 * Write the seed file...
4233 if ((fp
= cupsTempFile2(seedfile
, sizeof(seedfile
))) == NULL
)
4235 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to create seed file %s - %s",
4236 seedfile
, strerror(errno
));
4240 for (bytes
= 0; bytes
< 262144; bytes
++)
4241 cupsFilePutChar(fp
, random());
4246 * Run the openssl command to seed its random number generator...
4249 argv
[0] = "openssl";
4256 envc
= cupsdLoadEnv(envp
, MAX_ENV
);
4259 if (!cupsdStartProcess(command
, argv
, envp
, -1, -1, -1, -1, -1, 1, NULL
,
4266 while (waitpid(pid
, &status
, 0) < 0)
4273 cupsdFinishProcess(pid
, command
, sizeof(command
), NULL
);
4276 * Remove the seed file, as it is no longer needed...
4283 if (WIFEXITED(status
))
4284 cupsdLogMessage(CUPSD_LOG_ERROR
,
4285 "Unable to seed random number generator - "
4286 "the openssl command stopped with status %d!",
4287 WEXITSTATUS(status
));
4289 cupsdLogMessage(CUPSD_LOG_ERROR
,
4290 "Unable to seed random number generator - "
4291 "the openssl command crashed on signal %d!",
4299 * Create a file with the certificate information fields...
4301 * Note: This assumes that the default questions are asked by the openssl
4305 if ((fp
= cupsTempFile2(infofile
, sizeof(infofile
))) == NULL
)
4307 cupsdLogMessage(CUPSD_LOG_ERROR
,
4308 "Unable to create certificate information file %s - %s",
4309 infofile
, strerror(errno
));
4313 cupsFilePrintf(fp
, ".\n.\n.\n%s\n.\n%s\n%s\n",
4314 ServerName
, ServerName
, ServerAdmin
);
4317 cupsdLogMessage(CUPSD_LOG_INFO
,
4318 "Generating SSL server key and certificate...");
4320 argv
[0] = "openssl";
4324 argv
[4] = "-keyout";
4325 argv
[5] = ServerKey
;
4327 argv
[7] = ServerCertificate
;
4330 argv
[10] = "-nodes";
4333 cupsdLoadEnv(envp
, MAX_ENV
);
4335 infofd
= open(infofile
, O_RDONLY
);
4337 if (!cupsdStartProcess(command
, argv
, envp
, infofd
, -1, -1, -1, -1, 1, NULL
,
4348 while (waitpid(pid
, &status
, 0) < 0)
4355 cupsdFinishProcess(pid
, command
, sizeof(command
), NULL
);
4359 if (WIFEXITED(status
))
4360 cupsdLogMessage(CUPSD_LOG_ERROR
,
4361 "Unable to create SSL server key and certificate - "
4362 "the openssl command stopped with status %d!",
4363 WEXITSTATUS(status
));
4365 cupsdLogMessage(CUPSD_LOG_ERROR
,
4366 "Unable to create SSL server key and certificate - "
4367 "the openssl command crashed on signal %d!",
4372 cupsdLogMessage(CUPSD_LOG_INFO
, "Created SSL server key file \"%s\"...",
4374 cupsdLogMessage(CUPSD_LOG_INFO
,
4375 "Created SSL server certificate file \"%s\"...",
4381 #elif defined(HAVE_GNUTLS)
4382 gnutls_x509_crt crt
; /* Self-signed certificate */
4383 gnutls_x509_privkey key
; /* Encryption key */
4384 cups_lang_t
*language
; /* Default language info */
4385 cups_file_t
*fp
; /* Key/cert file */
4386 unsigned char buffer
[8192]; /* Buffer for x509 data */
4387 size_t bytes
; /* Number of bytes of data */
4388 unsigned char serial
[4]; /* Serial number buffer */
4389 time_t curtime
; /* Current time */
4390 int result
; /* Result of GNU TLS calls */
4394 * Create the encryption key...
4397 cupsdLogMessage(CUPSD_LOG_INFO
, "Generating SSL server key...");
4399 gnutls_x509_privkey_init(&key
);
4400 gnutls_x509_privkey_generate(key
, GNUTLS_PK_RSA
, 2048, 0);
4406 bytes
= sizeof(buffer
);
4408 if ((result
= gnutls_x509_privkey_export(key
, GNUTLS_X509_FMT_PEM
,
4409 buffer
, &bytes
)) < 0)
4411 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to export SSL server key - %s",
4412 gnutls_strerror(result
));
4413 gnutls_x509_privkey_deinit(key
);
4416 else if ((fp
= cupsFileOpen(ServerKey
, "w")) != NULL
)
4418 cupsFileWrite(fp
, (char *)buffer
, bytes
);
4421 cupsdLogMessage(CUPSD_LOG_INFO
, "Created SSL server key file \"%s\"...",
4426 cupsdLogMessage(CUPSD_LOG_ERROR
,
4427 "Unable to create SSL server key file \"%s\" - %s",
4428 ServerKey
, strerror(errno
));
4429 gnutls_x509_privkey_deinit(key
);
4434 * Create the self-signed certificate...
4437 cupsdLogMessage(CUPSD_LOG_INFO
, "Generating self-signed SSL certificate...");
4439 language
= cupsLangDefault();
4440 curtime
= time(NULL
);
4441 serial
[0] = curtime
>> 24;
4442 serial
[1] = curtime
>> 16;
4443 serial
[2] = curtime
>> 8;
4444 serial
[3] = curtime
;
4446 gnutls_x509_crt_init(&crt
);
4447 if (strlen(language
->language
) == 5)
4448 gnutls_x509_crt_set_dn_by_oid(crt
, GNUTLS_OID_X520_COUNTRY_NAME
, 0,
4449 language
->language
+ 3, 2);
4451 gnutls_x509_crt_set_dn_by_oid(crt
, GNUTLS_OID_X520_COUNTRY_NAME
, 0,
4453 gnutls_x509_crt_set_dn_by_oid(crt
, GNUTLS_OID_X520_COMMON_NAME
, 0,
4454 ServerName
, strlen(ServerName
));
4455 gnutls_x509_crt_set_dn_by_oid(crt
, GNUTLS_OID_X520_ORGANIZATION_NAME
, 0,
4456 ServerName
, strlen(ServerName
));
4457 gnutls_x509_crt_set_dn_by_oid(crt
, GNUTLS_OID_X520_ORGANIZATIONAL_UNIT_NAME
,
4459 gnutls_x509_crt_set_dn_by_oid(crt
, GNUTLS_OID_X520_STATE_OR_PROVINCE_NAME
, 0,
4461 gnutls_x509_crt_set_dn_by_oid(crt
, GNUTLS_OID_X520_LOCALITY_NAME
, 0,
4463 gnutls_x509_crt_set_dn_by_oid(crt
, GNUTLS_OID_PKCS9_EMAIL
, 0,
4464 ServerAdmin
, strlen(ServerAdmin
));
4465 gnutls_x509_crt_set_key(crt
, key
);
4466 gnutls_x509_crt_set_serial(crt
, serial
, sizeof(serial
));
4467 gnutls_x509_crt_set_activation_time(crt
, curtime
);
4468 gnutls_x509_crt_set_expiration_time(crt
, curtime
+ 10 * 365 * 86400);
4469 gnutls_x509_crt_set_ca_status(crt
, 0);
4470 gnutls_x509_crt_set_subject_alternative_name(crt
, GNUTLS_SAN_DNSNAME
,
4472 gnutls_x509_crt_set_key_purpose_oid(crt
, GNUTLS_KP_TLS_WWW_SERVER
, 0);
4473 gnutls_x509_crt_set_key_usage(crt
, GNUTLS_KEY_KEY_ENCIPHERMENT
);
4474 gnutls_x509_crt_set_version(crt
, 3);
4476 bytes
= sizeof(buffer
);
4477 if (gnutls_x509_crt_get_key_id(crt
, 0, buffer
, &bytes
) >= 0)
4478 gnutls_x509_crt_set_subject_key_id(crt
, buffer
, bytes
);
4480 gnutls_x509_crt_sign(crt
, crt
, key
);
4486 bytes
= sizeof(buffer
);
4487 if ((result
= gnutls_x509_crt_export(crt
, GNUTLS_X509_FMT_PEM
,
4488 buffer
, &bytes
)) < 0)
4489 cupsdLogMessage(CUPSD_LOG_ERROR
,
4490 "Unable to export SSL server certificate - %s",
4491 gnutls_strerror(result
));
4492 else if ((fp
= cupsFileOpen(ServerCertificate
, "w")) != NULL
)
4494 cupsFileWrite(fp
, (char *)buffer
, bytes
);
4497 cupsdLogMessage(CUPSD_LOG_INFO
,
4498 "Created SSL server certificate file \"%s\"...",
4502 cupsdLogMessage(CUPSD_LOG_ERROR
,
4503 "Unable to create SSL server certificate file \"%s\" - %s",
4504 ServerCertificate
, strerror(errno
));
4510 gnutls_x509_crt_deinit(crt
);
4511 gnutls_x509_privkey_deinit(key
);
4515 #elif defined(HAVE_CDSASSL) && defined(HAVE_WAITPID)
4516 int pid
, /* Process ID of command */
4517 status
; /* Status of command */
4518 char command
[1024], /* Command */
4519 *argv
[4], /* Command-line arguments */
4520 *envp
[MAX_ENV
+ 1], /* Environment variables */
4521 keychain
[1024], /* Keychain argument */
4522 infofile
[1024], /* Type-in information for cert */
4523 localname
[1024], /* Local hostname */
4524 *servername
; /* Name of server in cert */
4525 cups_file_t
*fp
; /* Seed/info file */
4526 int infofd
; /* Info file descriptor */
4529 if (con
->servername
&& isdigit(con
->servername
[0] & 255) && DNSSDHostName
)
4531 snprintf(localname
, sizeof(localname
), "%s.local", DNSSDHostName
);
4532 servername
= localname
;
4535 servername
= con
->servername
;
4538 * Run the "certtool" command to generate a self-signed certificate...
4541 if (!cupsFileFind("certtool", getenv("PATH"), 1, command
, sizeof(command
)))
4543 cupsdLogMessage(CUPSD_LOG_ERROR
,
4544 "No SSL certificate and certtool command not found!");
4549 * Create a file with the certificate information fields...
4551 * Note: This assumes that the default questions are asked by the certtool
4555 if ((fp
= cupsTempFile2(infofile
, sizeof(infofile
))) == NULL
)
4557 cupsdLogMessage(CUPSD_LOG_ERROR
,
4558 "Unable to create certificate information file %s - %s",
4559 infofile
, strerror(errno
));
4563 cupsFilePrintf(fp
, "%s\nr\n\ny\nb\ns\ny\n%s\n\n\n\n\n%s\ny\n",
4564 servername
, servername
, ServerAdmin
);
4567 cupsdLogMessage(CUPSD_LOG_INFO
,
4568 "Generating SSL server key and certificate...");
4570 snprintf(keychain
, sizeof(keychain
), "k=%s", ServerCertificate
);
4572 argv
[0] = "certtool";
4577 cupsdLoadEnv(envp
, MAX_ENV
);
4579 infofd
= open(infofile
, O_RDONLY
);
4581 if (!cupsdStartProcess(command
, argv
, envp
, infofd
, -1, -1, -1, -1, 1, NULL
,
4592 while (waitpid(pid
, &status
, 0) < 0)
4599 cupsdFinishProcess(pid
, command
, sizeof(command
), NULL
);
4603 if (WIFEXITED(status
))
4604 cupsdLogMessage(CUPSD_LOG_ERROR
,
4605 "Unable to create SSL server key and certificate - "
4606 "the certtool command stopped with status %d!",
4607 WEXITSTATUS(status
));
4609 cupsdLogMessage(CUPSD_LOG_ERROR
,
4610 "Unable to create SSL server key and certificate - "
4611 "the certtool command crashed on signal %d!",
4616 cupsdLogMessage(CUPSD_LOG_INFO
,
4617 "Created SSL server certificate file \"%s\"...",
4625 #endif /* HAVE_LIBSSL && HAVE_WAITPID */
4627 #endif /* HAVE_SSL */
4631 * 'pipe_command()' - Pipe the output of a command to the remote client.
4634 static int /* O - Process ID */
4635 pipe_command(cupsd_client_t
*con
, /* I - Client connection */
4636 int infile
, /* I - Standard input for command */
4637 int *outfile
, /* O - Standard output for command */
4638 char *command
, /* I - Command to run */
4639 char *options
, /* I - Options for command */
4640 int root
) /* I - Run as root? */
4642 int i
; /* Looping var */
4643 int pid
; /* Process ID */
4644 char *commptr
, /* Command string pointer */
4645 commch
; /* Command string character */
4646 char *uriptr
; /* URI string pointer */
4647 int fds
[2]; /* Pipe FDs */
4648 int argc
; /* Number of arguments */
4649 int envc
; /* Number of environment variables */
4650 char argbuf
[10240], /* Argument buffer */
4651 *argv
[100], /* Argument strings */
4652 *envp
[MAX_ENV
+ 20]; /* Environment variables */
4653 char auth_type
[256], /* AUTH_TYPE environment variable */
4654 content_length
[1024], /* CONTENT_LENGTH environment variable */
4655 content_type
[1024], /* CONTENT_TYPE environment variable */
4656 http_cookie
[32768], /* HTTP_COOKIE environment variable */
4657 http_referer
[1024], /* HTTP_REFERER environment variable */
4658 http_user_agent
[1024], /* HTTP_USER_AGENT environment variable */
4659 lang
[1024], /* LANG environment variable */
4660 path_info
[1024], /* PATH_INFO environment variable */
4661 remote_addr
[1024], /* REMOTE_ADDR environment variable */
4662 remote_host
[1024], /* REMOTE_HOST environment variable */
4663 remote_user
[1024], /* REMOTE_USER environment variable */
4664 script_filename
[1024], /* SCRIPT_FILENAME environment variable */
4665 script_name
[1024], /* SCRIPT_NAME environment variable */
4666 server_name
[1024], /* SERVER_NAME environment variable */
4667 server_port
[1024]; /* SERVER_PORT environment variable */
4668 ipp_attribute_t
*attr
; /* attributes-natural-language attribute */
4669 void *ccache
= NULL
; /* Kerberos credentials */
4673 * Parse a copy of the options string, which is of the form:
4675 * argument+argument+argument
4676 * ?argument+argument+argument
4677 * param=value¶m=value
4678 * ?param=value¶m=value
4679 * /name?argument+argument+argument
4680 * /name?param=value¶m=value
4682 * If the string contains an "=" character after the initial name,
4683 * then we treat it as a HTTP GET form request and make a copy of
4684 * the remaining string for the environment variable.
4686 * The string is always parsed out as command-line arguments, to
4687 * be consistent with Apache...
4690 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4691 "[Client %d] pipe_command infile=%d, outfile=%p, "
4692 "command=\"%s\", options=\"%s\", root=%d",
4693 con
->http
.fd
, infile
, outfile
, command
,
4694 options
? options
: "(null)", root
);
4701 if (*commptr
== ' ')
4703 strlcpy(argbuf
, commptr
, sizeof(argbuf
));
4708 if (argbuf
[0] == '/')
4711 * Found some trailing path information, set PATH_INFO...
4714 if ((commptr
= strchr(argbuf
, '?')) == NULL
)
4715 commptr
= argbuf
+ strlen(argbuf
);
4719 snprintf(path_info
, sizeof(path_info
), "PATH_INFO=%s", argbuf
);
4725 path_info
[0] = '\0';
4727 if (*commptr
== ' ')
4731 if (*commptr
== '?' && con
->operation
== HTTP_GET
&& !con
->query_string
)
4734 cupsdSetStringf(&(con
->query_string
), "QUERY_STRING=%s", commptr
);
4741 argv
[argc
++] = commptr
;
4743 for (; *commptr
&& argc
< 99; commptr
++)
4746 * Break arguments whenever we see a + or space...
4749 if (*commptr
== ' ' || *commptr
== '+')
4751 while (*commptr
== ' ' || *commptr
== '+')
4755 * If we don't have a blank string, save it as another argument...
4760 argv
[argc
] = commptr
;
4766 else if (*commptr
== '%' && isxdigit(commptr
[1] & 255) &&
4767 isxdigit(commptr
[2] & 255))
4770 * Convert the %xx notation to the individual character.
4773 if (commptr
[1] >= '0' && commptr
[1] <= '9')
4774 *commptr
= (commptr
[1] - '0') << 4;
4776 *commptr
= (tolower(commptr
[1]) - 'a' + 10) << 4;
4778 if (commptr
[2] >= '0' && commptr
[2] <= '9')
4779 *commptr
|= commptr
[2] - '0';
4781 *commptr
|= tolower(commptr
[2]) - 'a' + 10;
4783 _cups_strcpy(commptr
+ 1, commptr
+ 3);
4786 * Check for a %00 and break if that is the case...
4798 * Setup the environment variables as needed...
4801 if (con
->username
[0])
4803 snprintf(auth_type
, sizeof(auth_type
), "AUTH_TYPE=%s",
4804 httpGetField(HTTP(con
), HTTP_FIELD_AUTHORIZATION
));
4806 if ((uriptr
= strchr(auth_type
+ 10, ' ')) != NULL
)
4810 auth_type
[0] = '\0';
4813 (attr
= ippFindAttribute(con
->request
, "attributes-natural-language",
4814 IPP_TAG_LANGUAGE
)) != NULL
)
4816 switch (strlen(attr
->values
[0].string
.text
))
4820 * This is an unknown or badly formatted language code; use
4821 * the POSIX locale...
4824 strcpy(lang
, "LANG=C");
4829 * Just the language code (ll)...
4832 snprintf(lang
, sizeof(lang
), "LANG=%s.UTF8",
4833 attr
->values
[0].string
.text
);
4838 * Language and country code (ll-cc)...
4841 snprintf(lang
, sizeof(lang
), "LANG=%c%c_%c%c.UTF8",
4842 attr
->values
[0].string
.text
[0],
4843 attr
->values
[0].string
.text
[1],
4844 toupper(attr
->values
[0].string
.text
[3] & 255),
4845 toupper(attr
->values
[0].string
.text
[4] & 255));
4849 else if (con
->language
)
4850 snprintf(lang
, sizeof(lang
), "LANG=%s.UTF8", con
->language
->language
);
4852 strcpy(lang
, "LANG=C");
4854 strcpy(remote_addr
, "REMOTE_ADDR=");
4855 httpAddrString(con
->http
.hostaddr
, remote_addr
+ 12,
4856 sizeof(remote_addr
) - 12);
4858 snprintf(remote_host
, sizeof(remote_host
), "REMOTE_HOST=%s",
4859 con
->http
.hostname
);
4861 snprintf(script_name
, sizeof(script_name
), "SCRIPT_NAME=%s", con
->uri
);
4862 if ((uriptr
= strchr(script_name
, '?')) != NULL
)
4865 snprintf(script_filename
, sizeof(script_filename
), "SCRIPT_FILENAME=%s%s",
4866 DocumentRoot
, script_name
+ 12);
4868 sprintf(server_port
, "SERVER_PORT=%d", con
->serverport
);
4870 if (con
->http
.fields
[HTTP_FIELD_HOST
][0])
4872 char *nameptr
; /* Pointer to ":port" */
4874 snprintf(server_name
, sizeof(server_name
), "SERVER_NAME=%s",
4875 con
->http
.fields
[HTTP_FIELD_HOST
]);
4876 if ((nameptr
= strrchr(server_name
, ':')) != NULL
&& !strchr(nameptr
, ']'))
4877 *nameptr
= '\0'; /* Strip trailing ":port" */
4880 snprintf(server_name
, sizeof(server_name
), "SERVER_NAME=%s",
4883 envc
= cupsdLoadEnv(envp
, (int)(sizeof(envp
) / sizeof(envp
[0])));
4886 envp
[envc
++] = auth_type
;
4888 envp
[envc
++] = lang
;
4889 envp
[envc
++] = "REDIRECT_STATUS=1";
4890 envp
[envc
++] = "GATEWAY_INTERFACE=CGI/1.1";
4891 envp
[envc
++] = server_name
;
4892 envp
[envc
++] = server_port
;
4893 envp
[envc
++] = remote_addr
;
4894 envp
[envc
++] = remote_host
;
4895 envp
[envc
++] = script_name
;
4896 envp
[envc
++] = script_filename
;
4899 envp
[envc
++] = path_info
;
4901 if (con
->username
[0])
4903 snprintf(remote_user
, sizeof(remote_user
), "REMOTE_USER=%s", con
->username
);
4905 envp
[envc
++] = remote_user
;
4908 if (con
->http
.version
== HTTP_1_1
)
4909 envp
[envc
++] = "SERVER_PROTOCOL=HTTP/1.1";
4910 else if (con
->http
.version
== HTTP_1_0
)
4911 envp
[envc
++] = "SERVER_PROTOCOL=HTTP/1.0";
4913 envp
[envc
++] = "SERVER_PROTOCOL=HTTP/0.9";
4915 if (con
->http
.cookie
)
4917 snprintf(http_cookie
, sizeof(http_cookie
), "HTTP_COOKIE=%s",
4919 envp
[envc
++] = http_cookie
;
4922 if (con
->http
.fields
[HTTP_FIELD_USER_AGENT
][0])
4924 snprintf(http_user_agent
, sizeof(http_user_agent
), "HTTP_USER_AGENT=%s",
4925 con
->http
.fields
[HTTP_FIELD_USER_AGENT
]);
4926 envp
[envc
++] = http_user_agent
;
4929 if (con
->http
.fields
[HTTP_FIELD_REFERER
][0])
4931 snprintf(http_referer
, sizeof(http_referer
), "HTTP_REFERER=%s",
4932 con
->http
.fields
[HTTP_FIELD_REFERER
]);
4933 envp
[envc
++] = http_referer
;
4936 if (con
->operation
== HTTP_GET
)
4938 envp
[envc
++] = "REQUEST_METHOD=GET";
4940 if (con
->query_string
)
4943 * Add GET form variables after ?...
4946 envp
[envc
++] = con
->query_string
;
4949 envp
[envc
++] = "QUERY_STRING=";
4953 sprintf(content_length
, "CONTENT_LENGTH=" CUPS_LLFMT
,
4954 CUPS_LLCAST con
->bytes
);
4955 snprintf(content_type
, sizeof(content_type
), "CONTENT_TYPE=%s",
4956 con
->http
.fields
[HTTP_FIELD_CONTENT_TYPE
]);
4958 envp
[envc
++] = "REQUEST_METHOD=POST";
4959 envp
[envc
++] = content_length
;
4960 envp
[envc
++] = content_type
;
4964 * Tell the CGI if we are using encryption...
4968 envp
[envc
++] = "HTTPS=ON";
4971 * Terminate the environment array...
4976 if (LogLevel
>= CUPSD_LOG_DEBUG
)
4978 for (i
= 0; i
< argc
; i
++)
4979 cupsdLogMessage(CUPSD_LOG_DEBUG
,
4980 "[CGI] argv[%d] = \"%s\"", i
, argv
[i
]);
4981 for (i
= 0; i
< envc
; i
++)
4982 cupsdLogMessage(CUPSD_LOG_DEBUG
,
4983 "[CGI] envp[%d] = \"%s\"", i
, envp
[i
]);
4987 * Create a pipe for the output...
4990 if (cupsdOpenPipe(fds
))
4992 cupsdLogMessage(CUPSD_LOG_ERROR
, "[CGI] Unable to create pipe for %s - %s",
4993 argv
[0], strerror(errno
));
4998 * Then execute the command...
5001 if (cupsdStartProcess(command
, argv
, envp
, infile
, fds
[1], CGIPipes
[1],
5002 -1, -1, root
, DefaultProfile
, NULL
, &pid
) < 0)
5005 * Error - can't fork!
5008 cupsdLogMessage(CUPSD_LOG_ERROR
, "[CGI] Unable to start %s - %s", argv
[0],
5011 cupsdClosePipe(fds
);
5017 * Fork successful - return the PID...
5020 if (con
->username
[0])
5021 cupsdAddCert(pid
, con
->username
, ccache
);
5023 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[CGI] Started %s (PID %d)", command
, pid
);
5034 * 'valid_host()' - Is the Host: field valid?
5037 static int /* O - 1 if valid, 0 if not */
5038 valid_host(cupsd_client_t
*con
) /* I - Client connection */
5040 cupsd_alias_t
*a
; /* Current alias */
5041 cupsd_netif_t
*netif
; /* Current network interface */
5042 const char *host
, /* Host field */
5043 *end
; /* End character */
5046 host
= con
->http
.fields
[HTTP_FIELD_HOST
];
5048 if (httpAddrLocalhost(con
->http
.hostaddr
))
5051 * Only allow "localhost" or the equivalent IPv4 or IPv6 numerical
5052 * addresses when accessing CUPS via the loopback interface...
5055 return (!_cups_strcasecmp(host
, "localhost") ||
5056 !_cups_strncasecmp(host
, "localhost:", 10) ||
5057 !_cups_strcasecmp(host
, "localhost.") ||
5058 !_cups_strncasecmp(host
, "localhost.:", 11) ||
5060 !_cups_strcasecmp(host
, "localhost.localdomain") ||
5061 !_cups_strncasecmp(host
, "localhost.localdomain:", 22) ||
5062 #endif /* __linux */
5063 !strcmp(host
, "127.0.0.1") ||
5064 !strncmp(host
, "127.0.0.1:", 10) ||
5065 !strcmp(host
, "[::1]") ||
5066 !strncmp(host
, "[::1]:", 6));
5071 * Check if the hostname is something.local (Bonjour); if so, allow it.
5074 if ((end
= strrchr(host
, '.')) != NULL
&&
5075 (!_cups_strcasecmp(end
, ".local") || !_cups_strncasecmp(end
, ".local:", 7) ||
5076 !_cups_strcasecmp(end
, ".local.") || !_cups_strncasecmp(end
, ".local.:", 8)))
5078 #endif /* HAVE_DNSSD */
5081 * Check if the hostname is an IP address...
5084 if (isdigit(*host
& 255) || *host
== '[')
5087 * Possible IPv4/IPv6 address...
5090 char temp
[1024], /* Temporary string */
5091 *ptr
; /* Pointer into temporary string */
5092 http_addrlist_t
*addrlist
; /* List of addresses */
5095 strlcpy(temp
, host
, sizeof(temp
));
5096 if ((ptr
= strrchr(temp
, ':')) != NULL
&& !strchr(ptr
, ']'))
5097 *ptr
= '\0'; /* Strip :port from host value */
5099 if ((addrlist
= httpAddrGetList(temp
, AF_UNSPEC
, NULL
)) != NULL
)
5102 * Good IPv4/IPv6 address...
5105 httpAddrFreeList(addrlist
);
5111 * Check for (alias) name matches...
5114 for (a
= (cupsd_alias_t
*)cupsArrayFirst(ServerAlias
);
5116 a
= (cupsd_alias_t
*)cupsArrayNext(ServerAlias
))
5119 * "ServerAlias *" allows all host values through...
5122 if (!strcmp(a
->name
, "*"))
5125 if (!_cups_strncasecmp(host
, a
->name
, a
->namelen
))
5128 * Prefix matches; check the character at the end - it must be ":", ".",
5132 end
= host
+ a
->namelen
;
5134 if (!*end
|| *end
== ':' || (*end
== '.' && (!end
[1] || end
[1] == ':')))
5140 for (a
= (cupsd_alias_t
*)cupsArrayFirst(DNSSDAlias
);
5142 a
= (cupsd_alias_t
*)cupsArrayNext(DNSSDAlias
))
5145 * "ServerAlias *" allows all host values through...
5148 if (!strcmp(a
->name
, "*"))
5151 if (!_cups_strncasecmp(host
, a
->name
, a
->namelen
))
5154 * Prefix matches; check the character at the end - it must be ":", ".",
5158 end
= host
+ a
->namelen
;
5160 if (!*end
|| *end
== ':' || (*end
== '.' && (!end
[1] || end
[1] == ':')))
5164 #endif /* HAVE_DNSSD */
5167 * Check for interface hostname matches...
5170 for (netif
= (cupsd_netif_t
*)cupsArrayFirst(NetIFList
);
5172 netif
= (cupsd_netif_t
*)cupsArrayNext(NetIFList
))
5174 if (!_cups_strncasecmp(host
, netif
->hostname
, netif
->hostlen
))
5177 * Prefix matches; check the character at the end - it must be ":", ".",
5181 end
= host
+ netif
->hostlen
;
5183 if (!*end
|| *end
== ':' || (*end
== '.' && (!end
[1] || end
[1] == ':')))
5193 * 'write_file()' - Send a file via HTTP.
5196 static int /* O - 0 on failure, 1 on success */
5197 write_file(cupsd_client_t
*con
, /* I - Client connection */
5198 http_status_t code
, /* I - HTTP status */
5199 char *filename
, /* I - Filename */
5200 char *type
, /* I - File type */
5201 struct stat
*filestats
) /* O - File information */
5203 con
->file
= open(filename
, O_RDONLY
);
5205 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
5206 "[Client %d] write_file code=%d, filename=\"%s\" (%d), "
5207 "type=\"%s\", filestats=%p", con
->http
.fd
,
5208 code
, filename
, con
->file
, type
? type
: "(null)", filestats
);
5213 fcntl(con
->file
, F_SETFD
, fcntl(con
->file
, F_GETFD
) | FD_CLOEXEC
);
5217 if (!cupsdSendHeader(con
, code
, type
, CUPSD_AUTH_NONE
))
5220 if (httpPrintf(HTTP(con
), "Last-Modified: %s\r\n",
5221 httpGetDateString(filestats
->st_mtime
)) < 0)
5223 if (httpPrintf(HTTP(con
), "Content-Length: " CUPS_LLFMT
"\r\n",
5224 CUPS_LLCAST filestats
->st_size
) < 0)
5226 if (httpPrintf(HTTP(con
), "\r\n") < 0)
5229 if (cupsdFlushHeader(con
) < 0)
5232 con
->http
.data_encoding
= HTTP_ENCODE_LENGTH
;
5233 con
->http
.data_remaining
= filestats
->st_size
;
5235 if (con
->http
.data_remaining
<= INT_MAX
)
5236 con
->http
._data_remaining
= con
->http
.data_remaining
;
5238 con
->http
._data_remaining
= INT_MAX
;
5240 cupsdAddSelect(con
->http
.fd
, (cupsd_selfunc_t
)cupsdReadClient
,
5241 (cupsd_selfunc_t
)cupsdWriteClient
, con
);
5248 * 'write_pipe()' - Flag that data is available on the CGI pipe.
5252 write_pipe(cupsd_client_t
*con
) /* I - Client connection */
5254 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
5255 "[Client %d] write_pipe CGI output on fd %d",
5256 con
->http
.fd
, con
->file
);
5258 con
->file_ready
= 1;
5260 cupsdRemoveSelect(con
->file
);
5261 cupsdAddSelect(con
->http
.fd
, NULL
, (cupsd_selfunc_t
)cupsdWriteClient
, con
);
5266 * End of "$Id: client.c 7950 2008-09-17 00:21:59Z mike $".