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 * data_ready() - Check whether data is available from a client.
34 * encrypt_client() - Enable encryption for the client...
35 * get_cdsa_certificate() - Get a SSL/TLS certificate from the System
37 * get_file() - Get a filename and state info.
38 * install_conf_file() - Install a configuration file.
39 * is_cgi() - Is the resource a CGI script/program?
40 * is_path_absolute() - Is a path absolute and free of relative elements
42 * make_certificate() - Make a self-signed SSL/TLS certificate.
43 * pipe_command() - Pipe the output of a command to the remote client.
44 * valid_host() - Is the Host: field valid?
45 * write_file() - Send a file via HTTP.
46 * write_pipe() - Flag that data is available on the CGI pipe.
50 * Include necessary headers...
57 #endif /* HAVE_TCPD_H */
64 static int check_if_modified(cupsd_client_t
*con
,
65 struct stat
*filestats
);
66 static int compare_clients(cupsd_client_t
*a
, cupsd_client_t
*b
,
68 static int data_ready(cupsd_client_t
*con
);
70 static int encrypt_client(cupsd_client_t
*con
);
73 static CFArrayRef
get_cdsa_certificate(cupsd_client_t
*con
);
74 #endif /* HAVE_CDSASSL */
75 static char *get_file(cupsd_client_t
*con
, struct stat
*filestats
,
76 char *filename
, int len
);
77 static http_status_t
install_conf_file(cupsd_client_t
*con
);
78 static int is_cgi(cupsd_client_t
*con
, const char *filename
,
79 struct stat
*filestats
, mime_type_t
*type
);
80 static int is_path_absolute(const char *path
);
82 static int make_certificate(cupsd_client_t
*con
);
84 static int pipe_command(cupsd_client_t
*con
, int infile
, int *outfile
,
85 char *command
, char *options
, int root
);
86 static int valid_host(cupsd_client_t
*con
);
87 static int write_file(cupsd_client_t
*con
, http_status_t code
,
88 char *filename
, char *type
,
89 struct stat
*filestats
);
90 static void write_pipe(cupsd_client_t
*con
);
94 * 'cupsdAcceptClient()' - Accept a new client.
98 cupsdAcceptClient(cupsd_listener_t
*lis
)/* I - Listener socket */
100 int count
; /* Count of connections on a host */
101 int val
; /* Parameter value */
102 cupsd_client_t
*con
, /* New client pointer */
103 *tempcon
; /* Temporary client pointer */
104 http_addrlist_t
*addrlist
, /* List of adddresses for host */
105 *addr
; /* Current address */
106 socklen_t addrlen
; /* Length of address */
107 char *hostname
; /* Hostname for address */
108 http_addr_t temp
; /* Temporary address variable */
109 static time_t last_dos
= 0; /* Time of last DoS attack */
111 struct request_info wrap_req
; /* TCP wrappers request information */
112 #endif /* HAVE_TCPD_H */
115 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
116 "cupsdAcceptClient(lis=%p(%d)) Clients=%d",
117 lis
, lis
->fd
, cupsArrayCount(Clients
));
120 * Make sure we don't have a full set of clients already...
123 if (cupsArrayCount(Clients
) == MaxClients
)
127 * Get a pointer to the next available client...
131 Clients
= cupsArrayNew(NULL
, NULL
);
135 cupsdLogMessage(CUPSD_LOG_ERROR
,
136 "Unable to allocate memory for clients array!");
137 cupsdPauseListening();
142 ActiveClients
= cupsArrayNew((cups_array_func_t
)compare_clients
, NULL
);
146 cupsdLogMessage(CUPSD_LOG_ERROR
,
147 "Unable to allocate memory for active clients array!");
148 cupsdPauseListening();
152 if ((con
= calloc(1, sizeof(cupsd_client_t
))) == NULL
)
154 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to allocate memory for client!");
155 cupsdPauseListening();
159 con
->http
.activity
= time(NULL
);
161 con
->http
.hostaddr
= &(con
->clientaddr
);
164 * Accept the client and get the remote address...
167 addrlen
= sizeof(http_addr_t
);
169 if ((con
->http
.fd
= accept(lis
->fd
, (struct sockaddr
*)con
->http
.hostaddr
,
172 if (errno
== ENFILE
|| errno
== EMFILE
)
173 cupsdPauseListening();
175 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to accept client connection - %s.",
183 * Save the connected port number...
186 _httpAddrSetPort(con
->http
.hostaddr
, _httpAddrPort(&(lis
->address
)));
190 * Convert IPv4 over IPv6 addresses (::ffff:n.n.n.n) to IPv4 forms we
191 * can more easily use...
194 if (lis
->address
.addr
.sa_family
== AF_INET6
&&
195 con
->http
.hostaddr
->ipv6
.sin6_addr
.s6_addr32
[0] == 0 &&
196 con
->http
.hostaddr
->ipv6
.sin6_addr
.s6_addr32
[1] == 0 &&
197 ntohl(con
->http
.hostaddr
->ipv6
.sin6_addr
.s6_addr32
[2]) == 0xffff)
198 con
->http
.hostaddr
->ipv6
.sin6_addr
.s6_addr32
[2] = 0;
199 #endif /* AF_INET6 */
202 * Check the number of clients on the same address...
205 for (count
= 0, tempcon
= (cupsd_client_t
*)cupsArrayFirst(Clients
);
207 tempcon
= (cupsd_client_t
*)cupsArrayNext(Clients
))
208 if (httpAddrEqual(tempcon
->http
.hostaddr
, con
->http
.hostaddr
))
211 if (count
>= MaxClientsPerHost
)
215 if (count
>= MaxClientsPerHost
)
217 if ((time(NULL
) - last_dos
) >= 60)
219 last_dos
= time(NULL
);
220 cupsdLogMessage(CUPSD_LOG_WARN
,
221 "Possible DoS attack - more than %d clients connecting "
224 httpAddrString(con
->http
.hostaddr
, con
->http
.hostname
,
225 sizeof(con
->http
.hostname
)));
229 closesocket(con
->http
.fd
);
239 * Get the hostname or format the IP address as needed...
242 if (httpAddrLocalhost(con
->http
.hostaddr
))
245 * Map accesses from the loopback interface to "localhost"...
248 strlcpy(con
->http
.hostname
, "localhost", sizeof(con
->http
.hostname
));
249 hostname
= con
->http
.hostname
;
254 * Map accesses from the same host to the server name.
258 hostname
= httpAddrLookup(con
->http
.hostaddr
, con
->http
.hostname
,
259 sizeof(con
->http
.hostname
));
263 httpAddrString(con
->http
.hostaddr
, con
->http
.hostname
,
264 sizeof(con
->http
.hostname
));
268 if (hostname
== NULL
&& HostNameLookups
== 2)
271 * Can't have an unresolved IP address with double-lookups enabled...
275 closesocket(con
->http
.fd
);
280 cupsdLogMessage(CUPSD_LOG_WARN
,
281 "Name lookup failed - connection from %s closed!",
288 if (HostNameLookups
== 2)
291 * Do double lookups as needed...
294 if ((addrlist
= httpAddrGetList(con
->http
.hostname
, AF_UNSPEC
, NULL
))
298 * See if the hostname maps to the same IP address...
301 for (addr
= addrlist
; addr
; addr
= addr
->next
)
302 if (httpAddrEqual(con
->http
.hostaddr
, &(addr
->addr
)))
308 httpAddrFreeList(addrlist
);
313 * Can't have a hostname that doesn't resolve to the same IP address
314 * with double-lookups enabled...
318 closesocket(con
->http
.fd
);
323 cupsdLogMessage(CUPSD_LOG_WARN
,
324 "IP lookup failed - connection from %s closed!",
333 * See if the connection is denied by TCP wrappers...
336 request_init(&wrap_req
, RQ_DAEMON
, "cupsd", RQ_FILE
, con
->http
.fd
, NULL
);
339 if (!hosts_access(&wrap_req
))
342 closesocket(con
->http
.fd
);
347 cupsdLogMessage(CUPSD_LOG_WARN
,
348 "Connection from %s refused by /etc/hosts.allow and "
349 "/etc/hosts.deny rules.", con
->http
.hostname
);
353 #endif /* HAVE_TCPD_H */
356 if (con
->http
.hostaddr
->addr
.sa_family
== AF_LOCAL
)
357 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdAcceptClient: %d from %s (Domain)",
358 con
->http
.fd
, con
->http
.hostname
);
360 #endif /* AF_LOCAL */
361 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdAcceptClient: %d from %s:%d (IPv%d)",
362 con
->http
.fd
, con
->http
.hostname
,
363 _httpAddrPort(con
->http
.hostaddr
),
364 _httpAddrFamily(con
->http
.hostaddr
) == AF_INET
? 4 : 6);
367 * Get the local address the client connected to...
370 addrlen
= sizeof(temp
);
371 if (getsockname(con
->http
.fd
, (struct sockaddr
*)&temp
, &addrlen
))
373 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to get local address - %s",
376 strcpy(con
->servername
, "localhost");
377 con
->serverport
= LocalPort
;
380 else if (_httpAddrFamily(&temp
) == AF_LOCAL
)
382 strcpy(con
->servername
, "localhost");
383 con
->serverport
= LocalPort
;
385 #endif /* AF_LOCAL */
388 if (httpAddrLocalhost(&temp
))
389 strlcpy(con
->servername
, "localhost", sizeof(con
->servername
));
390 else if (HostNameLookups
|| RemotePort
)
391 httpAddrLookup(&temp
, con
->servername
, sizeof(con
->servername
));
393 httpAddrString(&temp
, con
->servername
, sizeof(con
->servername
));
395 con
->serverport
= _httpAddrPort(&(lis
->address
));
398 cupsArrayAdd(Clients
, con
);
401 * Using TCP_NODELAY improves responsiveness, especially on systems with a slow
402 * loopback interface. Since we write large buffers when sending print files
403 * and requests there shouldn't be any performance penalty for this...
407 setsockopt(con
->http
.fd
, IPPROTO_TCP
, TCP_NODELAY
, &val
, sizeof(val
));
410 * Close this file on all execs...
413 fcntl(con
->http
.fd
, F_SETFD
, fcntl(con
->http
.fd
, F_GETFD
) | FD_CLOEXEC
);
416 * Add the socket to the server select.
419 cupsdAddSelect(con
->http
.fd
, (cupsd_selfunc_t
)cupsdReadClient
, NULL
, con
);
422 * Temporarily suspend accept()'s until we lose a client...
425 if (cupsArrayCount(Clients
) == MaxClients
)
426 cupsdPauseListening();
430 * See if we are connecting on a secure port...
433 if (lis
->encryption
== HTTP_ENCRYPT_ALWAYS
)
436 * https connection; go secure...
439 con
->http
.encryption
= HTTP_ENCRYPT_ALWAYS
;
441 if (!encrypt_client(con
))
442 cupsdCloseClient(con
);
446 #endif /* HAVE_SSL */
451 * 'cupsdCloseAllClients()' - Close all remote clients immediately.
455 cupsdCloseAllClients(void)
457 cupsd_client_t
*con
; /* Current client */
460 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdCloseAllClients() Clients=%d",
461 cupsArrayCount(Clients
));
463 for (con
= (cupsd_client_t
*)cupsArrayFirst(Clients
);
465 con
= (cupsd_client_t
*)cupsArrayNext(Clients
))
466 if (cupsdCloseClient(con
))
467 cupsdCloseClient(con
);
472 * 'cupsdCloseClient()' - Close a remote client.
475 int /* O - 1 if partial close, 0 if fully closed */
476 cupsdCloseClient(cupsd_client_t
*con
) /* I - Client to close */
478 int partial
; /* Do partial close for SSL? */
480 SSL_CTX
*context
; /* Context for encryption */
481 unsigned long error
; /* Error code */
482 #elif defined(HAVE_GNUTLS)
483 int error
; /* Error code */
484 gnutls_certificate_server_credentials
*credentials
;
485 /* TLS credentials */
486 # elif defined(HAVE_CDSASSL)
487 #endif /* HAVE_LIBSSL */
490 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdCloseClient: %d", con
->http
.fd
);
493 * Flush pending writes before closing...
496 httpFlushWrite(HTTP(con
));
502 * Shutdown encryption as needed...
510 context
= SSL_get_SSL_CTX(con
->http
.tls
);
512 switch (SSL_shutdown(con
->http
.tls
))
515 cupsdLogMessage(CUPSD_LOG_DEBUG
,
516 "SSL shutdown successful!");
519 cupsdLogMessage(CUPSD_LOG_ERROR
,
520 "Fatal error during SSL shutdown!");
522 while ((error
= ERR_get_error()) != 0)
523 cupsdLogMessage(CUPSD_LOG_ERROR
, "SSL shutdown failed: %s",
524 ERR_error_string(error
, NULL
));
528 SSL_CTX_free(context
);
529 SSL_free(con
->http
.tls
);
531 # elif defined(HAVE_GNUTLS)
532 credentials
= (gnutls_certificate_server_credentials
*)(con
->http
.tls_credentials
);
534 error
= gnutls_bye(con
->http
.tls
, GNUTLS_SHUT_WR
);
537 case GNUTLS_E_SUCCESS
:
538 cupsdLogMessage(CUPSD_LOG_DEBUG
,
539 "SSL shutdown successful!");
542 cupsdLogMessage(CUPSD_LOG_ERROR
,
543 "SSL shutdown failed: %s", gnutls_strerror(error
));
547 gnutls_deinit(con
->http
.tls
);
548 gnutls_certificate_free_credentials(*credentials
);
551 # elif defined(HAVE_CDSASSL)
552 while (SSLClose(con
->http
.tls
) == errSSLWouldBlock
)
555 SSLDisposeContext(con
->http
.tls
);
557 if (con
->http
.tls_credentials
)
558 CFRelease(con
->http
.tls_credentials
);
560 # endif /* HAVE_LIBSSL */
562 con
->http
.tls
= NULL
;
564 #endif /* HAVE_SSL */
566 if (con
->pipe_pid
!= 0)
569 * Stop any CGI process...
572 cupsdEndProcess(con
->pipe_pid
, 1);
578 cupsdRemoveSelect(con
->file
);
585 * Close the socket and clear the file from the input set for select()...
588 if (con
->http
.fd
>= 0)
590 cupsArrayRemove(ActiveClients
, con
);
596 * Only do a partial close so that the encrypted client gets everything.
599 shutdown(con
->http
.fd
, 0);
600 cupsdAddSelect(con
->http
.fd
, (cupsd_selfunc_t
)cupsdReadClient
, NULL
, con
);
605 * Shut the socket down fully...
608 cupsdRemoveSelect(con
->http
.fd
);
620 if (con
->http
.input_set
)
621 free(con
->http
.input_set
);
623 httpClearCookie(HTTP(con
));
624 httpClearFields(HTTP(con
));
626 cupsdClearString(&con
->filename
);
627 cupsdClearString(&con
->command
);
628 cupsdClearString(&con
->options
);
629 cupsdClearString(&con
->query_string
);
633 ippDelete(con
->request
);
639 ippDelete(con
->response
);
640 con
->response
= NULL
;
645 cupsLangFree(con
->language
);
646 con
->language
= NULL
;
649 #ifdef HAVE_AUTHORIZATION_H
652 AuthorizationFree(con
->authref
, kAuthorizationFlagDefaults
);
655 #endif /* HAVE_AUTHORIZATION_H */
658 * Re-enable new client connections if we are going back under the
662 if (cupsArrayCount(Clients
) == MaxClients
)
663 cupsdResumeListening();
666 * Compact the list of clients as necessary...
669 cupsArrayRemove(Clients
, con
);
679 * 'cupsdFlushHeader()' - Flush the header fields to the client.
682 int /* I - Bytes written or -1 on error */
683 cupsdFlushHeader(cupsd_client_t
*con
) /* I - Client to flush to */
685 int bytes
= httpFlushWrite(HTTP(con
));
687 con
->http
.data_encoding
= HTTP_ENCODE_LENGTH
;
694 * 'cupsdReadClient()' - Read data from a client.
698 cupsdReadClient(cupsd_client_t
*con
) /* I - Client to read from */
700 char line
[32768], /* Line from client... */
701 operation
[64], /* Operation code from socket */
702 version
[64], /* HTTP version number string */
703 locale
[64], /* Locale */
704 *ptr
; /* Pointer into strings */
705 int major
, minor
; /* HTTP version numbers */
706 http_status_t status
; /* Transfer status */
707 ipp_state_t ipp_state
; /* State of IPP transfer */
708 int bytes
; /* Number of bytes to POST */
709 char *filename
; /* Name of file for GET/HEAD */
710 char buf
[1024]; /* Buffer for real filename */
711 struct stat filestats
; /* File information */
712 mime_type_t
*type
; /* MIME type of file */
713 cupsd_printer_t
*p
; /* Printer */
714 static unsigned request_id
= 0; /* Request ID for temp files */
717 status
= HTTP_CONTINUE
;
719 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
720 "cupsdReadClient(con=%p(%d)) "
721 "con->http.error=%d "
722 "con->http.used=%d, "
723 "con->http.state=%d "
724 "con->data_encoding=HTTP_ENCODE_%s, "
725 "con->data_remaining=" CUPS_LLFMT
", "
727 con
, con
->http
.fd
, con
->http
.error
, con
->http
.used
,
729 con
->http
.data_encoding
== HTTP_ENCODE_CHUNKED
?
730 "CHUNKED" : "LENGTH",
731 CUPS_LLCAST con
->http
.data_remaining
, con
->file
);
737 * Automatically check for a SSL/TLS handshake...
742 if (recv(con
->http
.fd
, buf
, 1, MSG_PEEK
) == 1 &&
743 (!buf
[0] || !strchr("DGHOPT", buf
[0])))
746 * Encrypt this connection...
749 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
750 "cupsdReadClient: Saw first byte %02X, auto-negotiating "
751 "SSL/TLS session...", buf
[0] & 255);
753 if (!encrypt_client(con
))
754 cupsdCloseClient(con
);
759 #endif /* HAVE_SSL */
761 switch (con
->http
.state
)
765 * See if we've received a request line...
768 if (httpGets(line
, sizeof(line
) - 1, HTTP(con
)) == NULL
)
770 if (con
->http
.error
&& con
->http
.error
!= EPIPE
)
771 cupsdLogMessage(CUPSD_LOG_DEBUG
,
772 "cupsdReadClient: %d WAITING Closing for error %d "
773 "(%s)", con
->http
.fd
, con
->http
.error
,
774 strerror(con
->http
.error
));
776 cupsdLogMessage(CUPSD_LOG_DEBUG
,
777 "cupsdReadClient: %d WAITING Closing on EOF",
780 cupsdCloseClient(con
);
785 * Ignore blank request lines...
792 * Clear other state variables...
795 httpClearFields(HTTP(con
));
797 con
->http
.activity
= time(NULL
);
798 con
->http
.version
= HTTP_1_0
;
799 con
->http
.keep_alive
= HTTP_KEEPALIVE_OFF
;
800 con
->http
.data_encoding
= HTTP_ENCODE_LENGTH
;
801 con
->http
.data_remaining
= 0;
802 con
->http
._data_remaining
= 0;
803 con
->operation
= HTTP_WAITING
;
808 con
->username
[0] = '\0';
809 con
->password
[0] = '\0';
812 cupsdClearString(&con
->command
);
813 cupsdClearString(&con
->options
);
814 cupsdClearString(&con
->query_string
);
818 ippDelete(con
->request
);
824 ippDelete(con
->response
);
825 con
->response
= NULL
;
830 cupsLangFree(con
->language
);
831 con
->language
= NULL
;
837 #endif /* HAVE_GSSAPI */
840 * Grab the request line...
843 switch (sscanf(line
, "%63s%1023s%63s", operation
, con
->uri
, version
))
848 cupsdLogMessage(CUPSD_LOG_ERROR
,
849 "Bad request line \"%s\" from %s!",
850 _httpEncodeURI(buf
, line
, sizeof(buf
)),
852 cupsdSendError(con
, HTTP_BAD_REQUEST
, CUPSD_AUTH_NONE
);
853 cupsdCloseClient(con
);
857 con
->http
.version
= HTTP_0_9
;
860 if (sscanf(version
, "HTTP/%d.%d", &major
, &minor
) != 2)
862 cupsdLogMessage(CUPSD_LOG_ERROR
,
863 "Bad request line \"%s\" from %s!",
864 _httpEncodeURI(buf
, line
, sizeof(buf
)),
866 cupsdSendError(con
, HTTP_BAD_REQUEST
, CUPSD_AUTH_NONE
);
867 cupsdCloseClient(con
);
873 con
->http
.version
= (http_version_t
)(major
* 100 + minor
);
874 if (con
->http
.version
== HTTP_1_1
&& KeepAlive
)
875 con
->http
.keep_alive
= HTTP_KEEPALIVE_ON
;
877 con
->http
.keep_alive
= HTTP_KEEPALIVE_OFF
;
881 cupsdLogMessage(CUPSD_LOG_ERROR
,
882 "Unsupported request line \"%s\" from %s!",
883 _httpEncodeURI(buf
, line
, sizeof(buf
)),
885 cupsdSendError(con
, HTTP_NOT_SUPPORTED
, CUPSD_AUTH_NONE
);
886 cupsdCloseClient(con
);
893 * Handle full URLs in the request line...
896 if (strcmp(con
->uri
, "*"))
898 char scheme
[HTTP_MAX_URI
], /* Method/scheme */
899 userpass
[HTTP_MAX_URI
], /* Username:password */
900 hostname
[HTTP_MAX_URI
], /* Hostname */
901 resource
[HTTP_MAX_URI
]; /* Resource path */
902 int port
; /* Port number */
906 * Separate the URI into its components...
909 httpSeparateURI(HTTP_URI_CODING_MOST
, con
->uri
,
910 scheme
, sizeof(scheme
),
911 userpass
, sizeof(userpass
),
912 hostname
, sizeof(hostname
), &port
,
913 resource
, sizeof(resource
));
916 * Only allow URIs with the servername, localhost, or an IP
920 if (strcmp(scheme
, "file") &&
921 strcasecmp(hostname
, ServerName
) &&
922 strcasecmp(hostname
, "localhost") &&
923 !isdigit(hostname
[0]) && hostname
[0] != '[')
926 * Nope, we don't do proxies...
929 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad URI \"%s\" in request!",
931 cupsdSendError(con
, HTTP_METHOD_NOT_ALLOWED
, CUPSD_AUTH_NONE
);
932 cupsdCloseClient(con
);
937 * Copy the resource portion back into the URI; both resource and
938 * con->uri are HTTP_MAX_URI bytes in size...
941 strcpy(con
->uri
, resource
);
945 * Process the request...
948 if (!strcmp(operation
, "GET"))
949 con
->http
.state
= HTTP_GET
;
950 else if (!strcmp(operation
, "PUT"))
951 con
->http
.state
= HTTP_PUT
;
952 else if (!strcmp(operation
, "POST"))
953 con
->http
.state
= HTTP_POST
;
954 else if (!strcmp(operation
, "DELETE"))
955 con
->http
.state
= HTTP_DELETE
;
956 else if (!strcmp(operation
, "TRACE"))
957 con
->http
.state
= HTTP_TRACE
;
958 else if (!strcmp(operation
, "OPTIONS"))
959 con
->http
.state
= HTTP_OPTIONS
;
960 else if (!strcmp(operation
, "HEAD"))
961 con
->http
.state
= HTTP_HEAD
;
964 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad operation \"%s\"!", operation
);
965 cupsdSendError(con
, HTTP_BAD_REQUEST
, CUPSD_AUTH_NONE
);
966 cupsdCloseClient(con
);
970 gettimeofday(&(con
->start
), NULL
);
971 con
->operation
= con
->http
.state
;
973 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdReadClient: %d %s %s HTTP/%d.%d",
974 con
->http
.fd
, operation
, con
->uri
,
975 con
->http
.version
/ 100, con
->http
.version
% 100);
977 con
->http
.status
= HTTP_OK
;
979 if (!cupsArrayFind(ActiveClients
, con
))
981 cupsArrayAdd(ActiveClients
, con
);
993 * Parse incoming parameters until the status changes...
996 while ((status
= httpUpdate(HTTP(con
))) == HTTP_CONTINUE
)
997 if (!data_ready(con
))
1000 if (status
!= HTTP_OK
&& status
!= HTTP_CONTINUE
)
1002 if (con
->http
.error
&& con
->http
.error
!= EPIPE
)
1003 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1004 "cupsdReadClient: %d FIELDS Closing for error %d "
1005 "(%s)", con
->http
.fd
, con
->http
.error
,
1006 strerror(con
->http
.error
));
1008 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1009 "cupsdReadClient: %d FIELDS Closing on EOF",
1012 cupsdSendError(con
, HTTP_BAD_REQUEST
, CUPSD_AUTH_NONE
);
1013 cupsdCloseClient(con
);
1019 if (!data_ready(con
) && recv(con
->http
.fd
, buf
, 1, MSG_PEEK
) < 1)
1022 * Connection closed...
1025 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1026 "cupsdReadClient: %d Closing on EOF", con
->http
.fd
);
1027 cupsdCloseClient(con
);
1030 break; /* Anti-compiler-warning-code */
1034 * Handle new transfers...
1037 if (status
== HTTP_OK
)
1039 if (con
->http
.fields
[HTTP_FIELD_ACCEPT_LANGUAGE
][0])
1042 * Figure out the locale from the Accept-Language and Content-Type
1046 if ((ptr
= strchr(con
->http
.fields
[HTTP_FIELD_ACCEPT_LANGUAGE
],
1050 if ((ptr
= strchr(con
->http
.fields
[HTTP_FIELD_ACCEPT_LANGUAGE
],
1054 if ((ptr
= strstr(con
->http
.fields
[HTTP_FIELD_CONTENT_TYPE
],
1055 "charset=")) != NULL
)
1058 * Combine language and charset, and trim any extra params in the
1062 snprintf(locale
, sizeof(locale
), "%s.%s",
1063 con
->http
.fields
[HTTP_FIELD_ACCEPT_LANGUAGE
], ptr
+ 8);
1065 if ((ptr
= strchr(locale
, ',')) != NULL
)
1069 snprintf(locale
, sizeof(locale
), "%s.UTF-8",
1070 con
->http
.fields
[HTTP_FIELD_ACCEPT_LANGUAGE
]);
1072 con
->language
= cupsLangGet(locale
);
1075 con
->language
= cupsLangGet(DefaultLocale
);
1077 cupsdAuthorize(con
);
1079 if (!strncasecmp(con
->http
.fields
[HTTP_FIELD_CONNECTION
], "Keep-Alive",
1081 con
->http
.keep_alive
= HTTP_KEEPALIVE_ON
;
1082 else if (!strncasecmp(con
->http
.fields
[HTTP_FIELD_CONNECTION
], "close", 5))
1083 con
->http
.keep_alive
= HTTP_KEEPALIVE_OFF
;
1085 if (!con
->http
.fields
[HTTP_FIELD_HOST
][0] &&
1086 con
->http
.version
>= HTTP_1_1
)
1089 * HTTP/1.1 and higher require the "Host:" field...
1092 if (!cupsdSendError(con
, HTTP_BAD_REQUEST
, CUPSD_AUTH_NONE
))
1094 cupsdLogMessage(CUPSD_LOG_ERROR
, "Missing Host: field in request!");
1095 cupsdCloseClient(con
);
1099 else if (!valid_host(con
))
1102 * Access to localhost must use "localhost" or the corresponding IPv4
1103 * or IPv6 values in the Host: field.
1106 cupsdLogMessage(CUPSD_LOG_ERROR
,
1107 "Request from \"%s\" using invalid Host: field \"%s\"",
1108 con
->http
.hostname
, con
->http
.fields
[HTTP_FIELD_HOST
]);
1110 if (!cupsdSendError(con
, HTTP_BAD_REQUEST
, CUPSD_AUTH_NONE
))
1112 cupsdCloseClient(con
);
1116 else if (con
->operation
== HTTP_OPTIONS
)
1119 * Do OPTIONS command...
1122 if (con
->best
&& con
->best
->type
!= CUPSD_AUTH_NONE
)
1124 if (!cupsdSendHeader(con
, HTTP_UNAUTHORIZED
, NULL
, CUPSD_AUTH_NONE
))
1126 cupsdCloseClient(con
);
1131 if (!strcasecmp(con
->http
.fields
[HTTP_FIELD_CONNECTION
], "Upgrade") &&
1132 con
->http
.tls
== NULL
)
1136 * Do encryption stuff...
1139 if (!cupsdSendHeader(con
, HTTP_SWITCHING_PROTOCOLS
, NULL
, CUPSD_AUTH_NONE
))
1141 cupsdCloseClient(con
);
1145 httpPrintf(HTTP(con
), "Connection: Upgrade\r\n");
1146 httpPrintf(HTTP(con
), "Upgrade: TLS/1.0,HTTP/1.1\r\n");
1147 httpPrintf(HTTP(con
), "Content-Length: 0\r\n");
1148 httpPrintf(HTTP(con
), "\r\n");
1150 if (cupsdFlushHeader(con
) < 0)
1152 cupsdCloseClient(con
);
1156 if (!encrypt_client(con
))
1158 cupsdCloseClient(con
);
1162 if (!cupsdSendError(con
, HTTP_NOT_IMPLEMENTED
, CUPSD_AUTH_NONE
))
1164 cupsdCloseClient(con
);
1167 #endif /* HAVE_SSL */
1170 if (!cupsdSendHeader(con
, HTTP_OK
, NULL
, CUPSD_AUTH_NONE
))
1172 cupsdCloseClient(con
);
1176 httpPrintf(HTTP(con
), "Allow: GET, HEAD, OPTIONS, POST, PUT\r\n");
1177 httpPrintf(HTTP(con
), "Content-Length: 0\r\n");
1178 httpPrintf(HTTP(con
), "\r\n");
1180 if (cupsdFlushHeader(con
) < 0)
1182 cupsdCloseClient(con
);
1186 else if (!is_path_absolute(con
->uri
))
1189 * Protect against malicious users!
1192 cupsdLogMessage(CUPSD_LOG_ERROR
,
1193 "Request for non-absolute resource \"%s\"!", con
->uri
);
1195 if (!cupsdSendError(con
, HTTP_FORBIDDEN
, CUPSD_AUTH_NONE
))
1197 cupsdCloseClient(con
);
1203 if (!strcasecmp(con
->http
.fields
[HTTP_FIELD_CONNECTION
], "Upgrade") &&
1204 con
->http
.tls
== NULL
)
1208 * Do encryption stuff...
1211 if (!cupsdSendHeader(con
, HTTP_SWITCHING_PROTOCOLS
, NULL
, CUPSD_AUTH_NONE
))
1213 cupsdCloseClient(con
);
1217 httpPrintf(HTTP(con
), "Connection: Upgrade\r\n");
1218 httpPrintf(HTTP(con
), "Upgrade: TLS/1.0,HTTP/1.1\r\n");
1219 httpPrintf(HTTP(con
), "Content-Length: 0\r\n");
1220 httpPrintf(HTTP(con
), "\r\n");
1222 if (cupsdFlushHeader(con
) < 0)
1224 cupsdCloseClient(con
);
1228 if (!encrypt_client(con
))
1230 cupsdCloseClient(con
);
1234 if (!cupsdSendError(con
, HTTP_NOT_IMPLEMENTED
, CUPSD_AUTH_NONE
))
1236 cupsdCloseClient(con
);
1239 #endif /* HAVE_SSL */
1242 if ((status
= cupsdIsAuthorized(con
, NULL
)) != HTTP_OK
)
1244 cupsdSendError(con
, status
, CUPSD_AUTH_NONE
);
1245 cupsdCloseClient(con
);
1249 if (con
->http
.expect
&&
1250 (con
->operation
== HTTP_POST
|| con
->operation
== HTTP_PUT
))
1252 if (con
->http
.expect
== HTTP_CONTINUE
)
1255 * Send 100-continue header...
1258 if (!cupsdSendHeader(con
, HTTP_CONTINUE
, NULL
, CUPSD_AUTH_NONE
))
1260 cupsdCloseClient(con
);
1267 * Send 417-expectation-failed header...
1270 if (!cupsdSendHeader(con
, HTTP_EXPECTATION_FAILED
, NULL
,
1273 cupsdCloseClient(con
);
1277 httpPrintf(HTTP(con
), "Content-Length: 0\r\n");
1278 httpPrintf(HTTP(con
), "\r\n");
1280 if (cupsdFlushHeader(con
) < 0)
1282 cupsdCloseClient(con
);
1288 switch (con
->http
.state
)
1290 case HTTP_GET_SEND
:
1291 if (!strncmp(con
->uri
, "/printers/", 10) &&
1292 !strcmp(con
->uri
+ strlen(con
->uri
) - 4, ".ppd"))
1295 * Send PPD file - get the real printer name since printer
1296 * names are not case sensitive but filenames can be...
1299 con
->uri
[strlen(con
->uri
) - 4] = '\0'; /* Drop ".ppd" */
1301 if ((p
= cupsdFindPrinter(con
->uri
+ 10)) != NULL
)
1302 snprintf(con
->uri
, sizeof(con
->uri
), "/ppd/%s.ppd", p
->name
);
1305 if (!cupsdSendError(con
, HTTP_NOT_FOUND
, CUPSD_AUTH_NONE
))
1307 cupsdCloseClient(con
);
1314 else if ((!strncmp(con
->uri
, "/printers/", 10) ||
1315 !strncmp(con
->uri
, "/classes/", 9)) &&
1316 !strcmp(con
->uri
+ strlen(con
->uri
) - 4, ".png"))
1319 * Send icon file - get the real queue name since queue names are
1320 * not case sensitive but filenames can be...
1323 con
->uri
[strlen(con
->uri
) - 4] = '\0'; /* Drop ".png" */
1325 if (!strncmp(con
->uri
, "/printers/", 10))
1326 p
= cupsdFindPrinter(con
->uri
+ 10);
1328 p
= cupsdFindClass(con
->uri
+ 9);
1331 snprintf(con
->uri
, sizeof(con
->uri
), "/icons/%s.png", p
->name
);
1334 if (!cupsdSendError(con
, HTTP_NOT_FOUND
, CUPSD_AUTH_NONE
))
1336 cupsdCloseClient(con
);
1343 else if (!WebInterface
)
1346 * Web interface is disabled. Show an appropriate message...
1349 if (!cupsdSendError(con
, HTTP_WEBIF_DISABLED
, CUPSD_AUTH_NONE
))
1351 cupsdCloseClient(con
);
1358 if ((!strncmp(con
->uri
, "/admin", 6) &&
1359 strncmp(con
->uri
, "/admin/conf/", 12) &&
1360 strncmp(con
->uri
, "/admin/log/", 11)) ||
1361 !strncmp(con
->uri
, "/printers", 9) ||
1362 !strncmp(con
->uri
, "/classes", 8) ||
1363 !strncmp(con
->uri
, "/help", 5) ||
1364 !strncmp(con
->uri
, "/jobs", 5))
1367 * Send CGI output...
1370 if (!strncmp(con
->uri
, "/admin", 6))
1372 cupsdSetStringf(&con
->command
, "%s/cgi-bin/admin.cgi",
1375 cupsdSetString(&con
->options
, strchr(con
->uri
+ 6, '?'));
1377 else if (!strncmp(con
->uri
, "/printers", 9))
1379 cupsdSetStringf(&con
->command
, "%s/cgi-bin/printers.cgi",
1382 if (con
->uri
[9] && con
->uri
[10])
1383 cupsdSetString(&con
->options
, con
->uri
+ 9);
1385 cupsdSetString(&con
->options
, NULL
);
1387 else if (!strncmp(con
->uri
, "/classes", 8))
1389 cupsdSetStringf(&con
->command
, "%s/cgi-bin/classes.cgi",
1392 if (con
->uri
[8] && con
->uri
[9])
1393 cupsdSetString(&con
->options
, con
->uri
+ 8);
1395 cupsdSetString(&con
->options
, NULL
);
1397 else if (!strncmp(con
->uri
, "/jobs", 5))
1399 cupsdSetStringf(&con
->command
, "%s/cgi-bin/jobs.cgi",
1402 if (con
->uri
[5] && con
->uri
[6])
1403 cupsdSetString(&con
->options
, con
->uri
+ 5);
1405 cupsdSetString(&con
->options
, NULL
);
1409 cupsdSetStringf(&con
->command
, "%s/cgi-bin/help.cgi",
1412 if (con
->uri
[5] && con
->uri
[6])
1413 cupsdSetString(&con
->options
, con
->uri
+ 5);
1415 cupsdSetString(&con
->options
, NULL
);
1418 if (!cupsdSendCommand(con
, con
->command
, con
->options
, 0))
1420 if (!cupsdSendError(con
, HTTP_NOT_FOUND
, CUPSD_AUTH_NONE
))
1422 cupsdCloseClient(con
);
1427 cupsdLogRequest(con
, HTTP_OK
);
1429 if (con
->http
.version
<= HTTP_1_0
)
1430 con
->http
.keep_alive
= HTTP_KEEPALIVE_OFF
;
1432 else if ((!strncmp(con
->uri
, "/admin/conf/", 12) &&
1433 (strchr(con
->uri
+ 12, '/') ||
1434 strlen(con
->uri
) == 12)) ||
1435 (!strncmp(con
->uri
, "/admin/log/", 11) &&
1436 (strchr(con
->uri
+ 11, '/') ||
1437 strlen(con
->uri
) == 11)))
1440 * GET can only be done to configuration files directly under
1444 cupsdLogMessage(CUPSD_LOG_ERROR
,
1445 "Request for subdirectory \"%s\"!", con
->uri
);
1447 if (!cupsdSendError(con
, HTTP_FORBIDDEN
, CUPSD_AUTH_NONE
))
1449 cupsdCloseClient(con
);
1461 if ((filename
= get_file(con
, &filestats
, buf
,
1462 sizeof(buf
))) == NULL
)
1464 if (!cupsdSendError(con
, HTTP_NOT_FOUND
, CUPSD_AUTH_NONE
))
1466 cupsdCloseClient(con
);
1473 type
= mimeFileType(MimeDatabase
, filename
, NULL
, NULL
);
1475 if (is_cgi(con
, filename
, &filestats
, type
))
1478 * Note: con->command and con->options were set by
1482 if (!cupsdSendCommand(con
, con
->command
, con
->options
, 0))
1484 if (!cupsdSendError(con
, HTTP_NOT_FOUND
, CUPSD_AUTH_NONE
))
1486 cupsdCloseClient(con
);
1491 cupsdLogRequest(con
, HTTP_OK
);
1493 if (con
->http
.version
<= HTTP_1_0
)
1494 con
->http
.keep_alive
= HTTP_KEEPALIVE_OFF
;
1498 if (!check_if_modified(con
, &filestats
))
1500 if (!cupsdSendError(con
, HTTP_NOT_MODIFIED
, CUPSD_AUTH_NONE
))
1502 cupsdCloseClient(con
);
1509 strcpy(line
, "text/plain");
1511 snprintf(line
, sizeof(line
), "%s/%s", type
->super
, type
->type
);
1513 if (!write_file(con
, HTTP_OK
, filename
, line
, &filestats
))
1515 cupsdCloseClient(con
);
1522 case HTTP_POST_RECV
:
1524 * See if the POST request includes a Content-Length field, and if
1525 * so check the length against any limits that are set...
1528 if (con
->http
.fields
[HTTP_FIELD_CONTENT_LENGTH
][0] &&
1529 MaxRequestSize
> 0 &&
1530 con
->http
.data_remaining
> MaxRequestSize
)
1533 * Request too large...
1536 if (!cupsdSendError(con
, HTTP_REQUEST_TOO_LARGE
, CUPSD_AUTH_NONE
))
1538 cupsdCloseClient(con
);
1544 else if (con
->http
.data_remaining
< 0 ||
1545 (!con
->http
.fields
[HTTP_FIELD_CONTENT_LENGTH
][0] &&
1546 con
->http
.data_encoding
== HTTP_ENCODE_LENGTH
))
1549 * Negative content lengths are invalid!
1552 if (!cupsdSendError(con
, HTTP_BAD_REQUEST
, CUPSD_AUTH_NONE
))
1554 cupsdCloseClient(con
);
1562 * See what kind of POST request this is; for IPP requests the
1563 * content-type field will be "application/ipp"...
1566 if (!strcmp(con
->http
.fields
[HTTP_FIELD_CONTENT_TYPE
],
1568 con
->request
= ippNew();
1569 else if (!WebInterface
)
1572 * Web interface is disabled. Show an appropriate message...
1575 if (!cupsdSendError(con
, HTTP_WEBIF_DISABLED
, CUPSD_AUTH_NONE
))
1577 cupsdCloseClient(con
);
1583 else if ((!strncmp(con
->uri
, "/admin", 6) &&
1584 strncmp(con
->uri
, "/admin/conf/", 12) &&
1585 strncmp(con
->uri
, "/admin/log/", 11)) ||
1586 !strncmp(con
->uri
, "/printers", 9) ||
1587 !strncmp(con
->uri
, "/classes", 8) ||
1588 !strncmp(con
->uri
, "/help", 5) ||
1589 !strncmp(con
->uri
, "/jobs", 5))
1595 if (!strncmp(con
->uri
, "/admin", 6))
1597 cupsdSetStringf(&con
->command
, "%s/cgi-bin/admin.cgi",
1600 cupsdSetString(&con
->options
, strchr(con
->uri
+ 6, '?'));
1602 else if (!strncmp(con
->uri
, "/printers", 9))
1604 cupsdSetStringf(&con
->command
, "%s/cgi-bin/printers.cgi",
1607 if (con
->uri
[9] && con
->uri
[10])
1608 cupsdSetString(&con
->options
, con
->uri
+ 9);
1610 cupsdSetString(&con
->options
, NULL
);
1612 else if (!strncmp(con
->uri
, "/classes", 8))
1614 cupsdSetStringf(&con
->command
, "%s/cgi-bin/classes.cgi",
1617 if (con
->uri
[8] && con
->uri
[9])
1618 cupsdSetString(&con
->options
, con
->uri
+ 8);
1620 cupsdSetString(&con
->options
, NULL
);
1622 else if (!strncmp(con
->uri
, "/jobs", 5))
1624 cupsdSetStringf(&con
->command
, "%s/cgi-bin/jobs.cgi",
1627 if (con
->uri
[5] && con
->uri
[6])
1628 cupsdSetString(&con
->options
, con
->uri
+ 5);
1630 cupsdSetString(&con
->options
, NULL
);
1634 cupsdSetStringf(&con
->command
, "%s/cgi-bin/help.cgi",
1637 if (con
->uri
[5] && con
->uri
[6])
1638 cupsdSetString(&con
->options
, con
->uri
+ 5);
1640 cupsdSetString(&con
->options
, NULL
);
1643 if (con
->http
.version
<= HTTP_1_0
)
1644 con
->http
.keep_alive
= HTTP_KEEPALIVE_OFF
;
1652 if ((filename
= get_file(con
, &filestats
, buf
,
1653 sizeof(buf
))) == NULL
)
1655 if (!cupsdSendError(con
, HTTP_NOT_FOUND
, CUPSD_AUTH_NONE
))
1657 cupsdCloseClient(con
);
1664 type
= mimeFileType(MimeDatabase
, filename
, NULL
, NULL
);
1666 if (!is_cgi(con
, filename
, &filestats
, type
))
1669 * Only POST to CGI's...
1672 if (!cupsdSendError(con
, HTTP_UNAUTHORIZED
, CUPSD_AUTH_NONE
))
1674 cupsdCloseClient(con
);
1681 case HTTP_PUT_RECV
:
1683 * Validate the resource name...
1686 if (strncmp(con
->uri
, "/admin/conf/", 12) ||
1687 strchr(con
->uri
+ 12, '/') ||
1688 strlen(con
->uri
) == 12)
1691 * PUT can only be done to configuration files under
1695 cupsdLogMessage(CUPSD_LOG_ERROR
,
1696 "Request for subdirectory \"%s\"!", con
->uri
);
1698 if (!cupsdSendError(con
, HTTP_FORBIDDEN
, CUPSD_AUTH_NONE
))
1700 cupsdCloseClient(con
);
1708 * See if the PUT request includes a Content-Length field, and if
1709 * so check the length against any limits that are set...
1712 if (con
->http
.fields
[HTTP_FIELD_CONTENT_LENGTH
][0] &&
1713 MaxRequestSize
> 0 &&
1714 con
->http
.data_remaining
> MaxRequestSize
)
1717 * Request too large...
1720 if (!cupsdSendError(con
, HTTP_REQUEST_TOO_LARGE
, CUPSD_AUTH_NONE
))
1722 cupsdCloseClient(con
);
1728 else if (con
->http
.data_remaining
< 0)
1731 * Negative content lengths are invalid!
1734 if (!cupsdSendError(con
, HTTP_BAD_REQUEST
, CUPSD_AUTH_NONE
))
1736 cupsdCloseClient(con
);
1744 * Open a temporary file to hold the request...
1747 cupsdSetStringf(&con
->filename
, "%s/%08x", RequestRoot
,
1749 con
->file
= open(con
->filename
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0640);
1753 cupsdLogMessage(CUPSD_LOG_ERROR
,
1754 "Unable to create request file %s: %s",
1755 con
->filename
, strerror(errno
));
1757 if (!cupsdSendError(con
, HTTP_REQUEST_TOO_LARGE
, CUPSD_AUTH_NONE
))
1759 cupsdCloseClient(con
);
1764 fchmod(con
->file
, 0640);
1765 fchown(con
->file
, RunUser
, Group
);
1766 fcntl(con
->file
, F_SETFD
, fcntl(con
->file
, F_GETFD
) | FD_CLOEXEC
);
1771 cupsdSendError(con
, HTTP_NOT_IMPLEMENTED
, CUPSD_AUTH_NONE
);
1772 cupsdCloseClient(con
);
1776 if (!strncmp(con
->uri
, "/printers/", 10) &&
1777 !strcmp(con
->uri
+ strlen(con
->uri
) - 4, ".ppd"))
1780 * Send PPD file - get the real printer name since printer
1781 * names are not case sensitive but filenames can be...
1784 con
->uri
[strlen(con
->uri
) - 4] = '\0'; /* Drop ".ppd" */
1786 if ((p
= cupsdFindPrinter(con
->uri
+ 10)) != NULL
)
1787 snprintf(con
->uri
, sizeof(con
->uri
), "/ppd/%s.ppd", p
->name
);
1790 if (!cupsdSendError(con
, HTTP_NOT_FOUND
, CUPSD_AUTH_NONE
))
1792 cupsdCloseClient(con
);
1799 else if (!strncmp(con
->uri
, "/printers/", 10) &&
1800 !strcmp(con
->uri
+ strlen(con
->uri
) - 4, ".png"))
1803 * Send PNG file - get the real printer name since printer
1804 * names are not case sensitive but filenames can be...
1807 con
->uri
[strlen(con
->uri
) - 4] = '\0'; /* Drop ".ppd" */
1809 if ((p
= cupsdFindPrinter(con
->uri
+ 10)) != NULL
)
1810 snprintf(con
->uri
, sizeof(con
->uri
), "/icons/%s.png", p
->name
);
1813 if (!cupsdSendError(con
, HTTP_NOT_FOUND
, CUPSD_AUTH_NONE
))
1815 cupsdCloseClient(con
);
1822 else if (!WebInterface
)
1824 if (!cupsdSendHeader(con
, HTTP_OK
, line
, CUPSD_AUTH_NONE
))
1826 cupsdCloseClient(con
);
1830 if (httpPrintf(HTTP(con
), "\r\n") < 0)
1832 cupsdCloseClient(con
);
1836 if (cupsdFlushHeader(con
) < 0)
1838 cupsdCloseClient(con
);
1842 con
->http
.state
= HTTP_WAITING
;
1846 if ((!strncmp(con
->uri
, "/admin", 6) &&
1847 strncmp(con
->uri
, "/admin/conf/", 12) &&
1848 strncmp(con
->uri
, "/admin/log/", 11)) ||
1849 !strncmp(con
->uri
, "/printers", 9) ||
1850 !strncmp(con
->uri
, "/classes", 8) ||
1851 !strncmp(con
->uri
, "/help", 5) ||
1852 !strncmp(con
->uri
, "/jobs", 5))
1858 if (!cupsdSendHeader(con
, HTTP_OK
, "text/html", CUPSD_AUTH_NONE
))
1860 cupsdCloseClient(con
);
1864 if (httpPrintf(HTTP(con
), "\r\n") < 0)
1866 cupsdCloseClient(con
);
1870 if (cupsdFlushHeader(con
) < 0)
1872 cupsdCloseClient(con
);
1876 cupsdLogRequest(con
, HTTP_OK
);
1878 else if ((!strncmp(con
->uri
, "/admin/conf/", 12) &&
1879 (strchr(con
->uri
+ 12, '/') ||
1880 strlen(con
->uri
) == 12)) ||
1881 (!strncmp(con
->uri
, "/admin/log/", 11) &&
1882 (strchr(con
->uri
+ 11, '/') ||
1883 strlen(con
->uri
) == 11)))
1886 * HEAD can only be done to configuration files under
1890 cupsdLogMessage(CUPSD_LOG_ERROR
,
1891 "Request for subdirectory \"%s\"!", con
->uri
);
1893 if (!cupsdSendError(con
, HTTP_FORBIDDEN
, CUPSD_AUTH_NONE
))
1895 cupsdCloseClient(con
);
1901 else if ((filename
= get_file(con
, &filestats
, buf
,
1902 sizeof(buf
))) == NULL
)
1904 if (!cupsdSendHeader(con
, HTTP_NOT_FOUND
, "text/html",
1907 cupsdCloseClient(con
);
1911 cupsdLogRequest(con
, HTTP_NOT_FOUND
);
1913 else if (!check_if_modified(con
, &filestats
))
1915 if (!cupsdSendError(con
, HTTP_NOT_MODIFIED
, CUPSD_AUTH_NONE
))
1917 cupsdCloseClient(con
);
1921 cupsdLogRequest(con
, HTTP_NOT_MODIFIED
);
1929 type
= mimeFileType(MimeDatabase
, filename
, NULL
, NULL
);
1931 strcpy(line
, "text/plain");
1933 snprintf(line
, sizeof(line
), "%s/%s", type
->super
, type
->type
);
1935 if (!cupsdSendHeader(con
, HTTP_OK
, line
, CUPSD_AUTH_NONE
))
1937 cupsdCloseClient(con
);
1941 if (httpPrintf(HTTP(con
), "Last-Modified: %s\r\n",
1942 httpGetDateString(filestats
.st_mtime
)) < 0)
1944 cupsdCloseClient(con
);
1948 if (httpPrintf(HTTP(con
), "Content-Length: %lu\r\n",
1949 (unsigned long)filestats
.st_size
) < 0)
1951 cupsdCloseClient(con
);
1955 cupsdLogRequest(con
, HTTP_OK
);
1958 if (httpPrintf(HTTP(con
), "\r\n") < 0)
1960 cupsdCloseClient(con
);
1964 if (cupsdFlushHeader(con
) < 0)
1966 cupsdCloseClient(con
);
1970 con
->http
.state
= HTTP_WAITING
;
1974 break; /* Anti-compiler-warning-code */
1980 * Handle any incoming data...
1983 switch (con
->http
.state
)
1985 case HTTP_PUT_RECV
:
1988 if ((bytes
= httpRead2(HTTP(con
), line
, sizeof(line
))) < 0)
1990 if (con
->http
.error
&& con
->http
.error
!= EPIPE
)
1991 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1992 "cupsdReadClient: %d PUT_RECV Closing for error "
1993 "%d (%s)", con
->http
.fd
, con
->http
.error
,
1994 strerror(con
->http
.error
));
1996 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1997 "cupsdReadClient: %d PUT_RECV Closing on EOF",
2000 cupsdCloseClient(con
);
2005 con
->bytes
+= bytes
;
2007 if (write(con
->file
, line
, bytes
) < bytes
)
2009 cupsdLogMessage(CUPSD_LOG_ERROR
,
2010 "cupsdReadClient: Unable to write %d bytes to %s: %s",
2011 bytes
, con
->filename
, strerror(errno
));
2015 unlink(con
->filename
);
2016 cupsdClearString(&con
->filename
);
2018 if (!cupsdSendError(con
, HTTP_REQUEST_TOO_LARGE
, CUPSD_AUTH_NONE
))
2020 cupsdCloseClient(con
);
2026 while (con
->http
.state
== HTTP_PUT_RECV
&& data_ready(con
));
2028 if (con
->http
.state
== HTTP_WAITING
)
2031 * End of file, see how big it is...
2034 fstat(con
->file
, &filestats
);
2039 if (filestats
.st_size
> MaxRequestSize
&&
2043 * Request is too big; remove it and send an error...
2046 unlink(con
->filename
);
2047 cupsdClearString(&con
->filename
);
2049 if (!cupsdSendError(con
, HTTP_REQUEST_TOO_LARGE
, CUPSD_AUTH_NONE
))
2051 cupsdCloseClient(con
);
2057 * Install the configuration file...
2060 status
= install_conf_file(con
);
2063 * Return the status to the client...
2066 if (!cupsdSendError(con
, status
, CUPSD_AUTH_NONE
))
2068 cupsdCloseClient(con
);
2074 case HTTP_POST_RECV
:
2077 if (con
->request
&& con
->file
< 0)
2080 * Grab any request data from the connection...
2083 if ((ipp_state
= ippRead(&(con
->http
), con
->request
)) == IPP_ERROR
)
2085 cupsdLogMessage(CUPSD_LOG_ERROR
,
2086 "cupsdReadClient: %d IPP Read Error!",
2089 cupsdSendError(con
, HTTP_BAD_REQUEST
, CUPSD_AUTH_NONE
);
2090 cupsdCloseClient(con
);
2093 else if (ipp_state
!= IPP_DATA
)
2095 if (con
->http
.state
== HTTP_POST_SEND
)
2097 cupsdSendError(con
, HTTP_BAD_REQUEST
, CUPSD_AUTH_NONE
);
2098 cupsdCloseClient(con
);
2106 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdReadClient: %d %d.%d %s %d",
2107 con
->http
.fd
, con
->request
->request
.op
.version
[0],
2108 con
->request
->request
.op
.version
[1],
2109 ippOpString(con
->request
->request
.op
.operation_id
),
2110 con
->request
->request
.op
.request_id
);
2111 con
->bytes
+= ippLength(con
->request
);
2115 if (con
->file
< 0 && con
->http
.state
!= HTTP_POST_SEND
)
2118 * Create a file as needed for the request data...
2121 cupsdSetStringf(&con
->filename
, "%s/%08x", RequestRoot
,
2123 con
->file
= open(con
->filename
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0640);
2127 cupsdLogMessage(CUPSD_LOG_ERROR
,
2128 "Unable to create request file %s: %s",
2129 con
->filename
, strerror(errno
));
2131 if (!cupsdSendError(con
, HTTP_REQUEST_TOO_LARGE
, CUPSD_AUTH_NONE
))
2133 cupsdCloseClient(con
);
2138 fchmod(con
->file
, 0640);
2139 fchown(con
->file
, RunUser
, Group
);
2140 fcntl(con
->file
, F_SETFD
, fcntl(con
->file
, F_GETFD
) | FD_CLOEXEC
);
2143 if (con
->http
.state
!= HTTP_POST_SEND
)
2145 if ((bytes
= httpRead2(HTTP(con
), line
, sizeof(line
))) < 0)
2147 if (con
->http
.error
&& con
->http
.error
!= EPIPE
)
2148 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2149 "cupsdReadClient: %d POST_SEND Closing for "
2150 "error %d (%s)", con
->http
.fd
, con
->http
.error
,
2151 strerror(con
->http
.error
));
2153 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2154 "cupsdReadClient: %d POST_SEND Closing on EOF",
2157 cupsdCloseClient(con
);
2162 con
->bytes
+= bytes
;
2164 if (write(con
->file
, line
, bytes
) < bytes
)
2166 cupsdLogMessage(CUPSD_LOG_ERROR
,
2167 "cupsdReadClient: Unable to write %d bytes to "
2168 "%s: %s", bytes
, con
->filename
,
2173 unlink(con
->filename
);
2174 cupsdClearString(&con
->filename
);
2176 if (!cupsdSendError(con
, HTTP_REQUEST_TOO_LARGE
,
2179 cupsdCloseClient(con
);
2184 else if (con
->http
.state
== HTTP_POST_RECV
)
2186 else if (con
->http
.state
!= HTTP_POST_SEND
)
2188 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2189 "cupsdReadClient: %d Closing on unknown HTTP "
2190 "state %d", con
->http
.fd
, con
->http
.state
);
2191 cupsdCloseClient(con
);
2196 while (con
->http
.state
== HTTP_POST_RECV
&& data_ready(con
));
2198 if (con
->http
.state
== HTTP_POST_SEND
)
2202 fstat(con
->file
, &filestats
);
2207 if (filestats
.st_size
> MaxRequestSize
&&
2211 * Request is too big; remove it and send an error...
2214 unlink(con
->filename
);
2215 cupsdClearString(&con
->filename
);
2220 * Delete any IPP request data...
2223 ippDelete(con
->request
);
2224 con
->request
= NULL
;
2227 if (!cupsdSendError(con
, HTTP_REQUEST_TOO_LARGE
, CUPSD_AUTH_NONE
))
2229 cupsdCloseClient(con
);
2233 else if (filestats
.st_size
== 0)
2236 * Don't allow empty file...
2239 unlink(con
->filename
);
2240 cupsdClearString(&con
->filename
);
2245 if (!cupsdSendCommand(con
, con
->command
, con
->options
, 0))
2247 if (!cupsdSendError(con
, HTTP_NOT_FOUND
, CUPSD_AUTH_NONE
))
2249 cupsdCloseClient(con
);
2254 cupsdLogRequest(con
, HTTP_OK
);
2260 cupsdProcessIPPRequest(con
);
2264 unlink(con
->filename
);
2265 cupsdClearString(&con
->filename
);
2274 break; /* Anti-compiler-warning-code */
2277 if (con
->http
.state
== HTTP_WAITING
)
2279 if (!con
->http
.keep_alive
)
2281 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2282 "cupsdReadClient: %d Closing because Keep-Alive disabled",
2284 cupsdCloseClient(con
);
2288 cupsArrayRemove(ActiveClients
, con
);
2289 cupsdSetBusyState();
2296 * 'cupsdSendCommand()' - Send output from a command via HTTP.
2299 int /* O - 1 on success, 0 on failure */
2301 cupsd_client_t
*con
, /* I - Client connection */
2302 char *command
, /* I - Command to run */
2303 char *options
, /* I - Command-line options */
2304 int root
) /* I - Run as root? */
2306 int fd
; /* Standard input file descriptor */
2311 fd
= open(con
->filename
, O_RDONLY
);
2315 cupsdLogMessage(CUPSD_LOG_ERROR
,
2316 "cupsdSendCommand: %d Unable to open \"%s\" for reading: %s",
2317 con
->http
.fd
, con
->filename
? con
->filename
: "/dev/null",
2322 fcntl(fd
, F_SETFD
, fcntl(fd
, F_GETFD
) | FD_CLOEXEC
);
2327 con
->pipe_pid
= pipe_command(con
, fd
, &(con
->file
), command
, options
, root
);
2332 cupsdLogMessage(CUPSD_LOG_INFO
, "Started \"%s\" (pid=%d)", command
,
2335 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdSendCommand: %d file=%d",
2336 con
->http
.fd
, con
->file
);
2338 if (con
->pipe_pid
== 0)
2341 fcntl(con
->file
, F_SETFD
, fcntl(con
->file
, F_GETFD
) | FD_CLOEXEC
);
2343 cupsdAddSelect(con
->file
, (cupsd_selfunc_t
)write_pipe
, NULL
, con
);
2345 con
->sent_header
= 0;
2346 con
->file_ready
= 0;
2347 con
->got_fields
= 0;
2348 con
->header_used
= 0;
2355 * 'cupsdSendError()' - Send an error message via HTTP.
2358 int /* O - 1 if successful, 0 otherwise */
2359 cupsdSendError(cupsd_client_t
*con
, /* I - Connection */
2360 http_status_t code
, /* I - Error code */
2361 int auth_type
)/* I - Authentication type */
2363 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2364 "cupsdSendError(con=%p(%d), code=%d, auth_type=%d", con
,
2365 con
->http
.fd
, code
, auth_type
);
2369 * Force client to upgrade for authentication if that is how the
2370 * server is configured...
2373 if (code
== HTTP_UNAUTHORIZED
&&
2374 DefaultEncryption
== HTTP_ENCRYPT_REQUIRED
&&
2375 strcasecmp(con
->http
.hostname
, "localhost") &&
2378 code
= HTTP_UPGRADE_REQUIRED
;
2380 #endif /* HAVE_SSL */
2383 * Put the request in the access_log file...
2386 cupsdLogRequest(con
, code
);
2389 * To work around bugs in some proxies, don't use Keep-Alive for some
2392 * Kerberos authentication doesn't work without Keep-Alive, so
2393 * never disable it in that case.
2396 if (code
>= HTTP_BAD_REQUEST
&& con
->http
.auth_type
!= CUPSD_AUTH_NEGOTIATE
)
2397 con
->http
.keep_alive
= HTTP_KEEPALIVE_OFF
;
2400 * Send an error message back to the client. If the error code is a
2401 * 400 or 500 series, make sure the message contains some text, too!
2404 if (!cupsdSendHeader(con
, code
, NULL
, auth_type
))
2408 if (code
== HTTP_UPGRADE_REQUIRED
)
2409 if (httpPrintf(HTTP(con
), "Connection: Upgrade\r\n") < 0)
2412 if (httpPrintf(HTTP(con
), "Upgrade: TLS/1.0,HTTP/1.1\r\n") < 0)
2414 #endif /* HAVE_SSL */
2416 if (con
->http
.version
>= HTTP_1_1
&&
2417 con
->http
.keep_alive
== HTTP_KEEPALIVE_OFF
)
2419 if (httpPrintf(HTTP(con
), "Connection: close\r\n") < 0)
2423 if (code
>= HTTP_BAD_REQUEST
)
2426 * Send a human-readable error message.
2429 char message
[4096], /* Message for user */
2430 urltext
[1024], /* URL redirection text */
2431 redirect
[1024]; /* Redirection link */
2432 const char *text
; /* Status-specific text */
2437 if (code
== HTTP_UNAUTHORIZED
)
2438 text
= _cupsLangString(con
->language
,
2439 _("Enter your username and password or the "
2440 "root username and password to access this "
2441 "page. If you are using Kerberos authentication, "
2442 "make sure you have a valid Kerberos ticket."));
2443 else if (code
== HTTP_UPGRADE_REQUIRED
)
2447 snprintf(urltext
, sizeof(urltext
),
2448 _cupsLangString(con
->language
,
2449 _("You must access this page using the URL "
2450 "<A HREF=\"https://%s:%d%s\">"
2451 "https://%s:%d%s</A>.")),
2452 con
->servername
, con
->serverport
, con
->uri
,
2453 con
->servername
, con
->serverport
, con
->uri
);
2455 snprintf(redirect
, sizeof(redirect
),
2456 "<META HTTP-EQUIV=\"Refresh\" "
2457 "CONTENT=\"3;URL=https://%s:%d%s\">\n",
2458 con
->servername
, con
->serverport
, con
->uri
);
2460 else if (code
== HTTP_WEBIF_DISABLED
)
2461 text
= _cupsLangString(con
->language
,
2462 _("The web interface is currently disabled. Run "
2463 "\"cupsctl WebInterface=yes\" to enable it."));
2467 snprintf(message
, sizeof(message
),
2468 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" "
2469 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
2472 "\t<META HTTP-EQUIV=\"Content-Type\" "
2473 "CONTENT=\"text/html; charset=utf-8\">\n"
2474 "\t<TITLE>%s - " CUPS_SVERSION
"</TITLE>\n"
2475 "\t<LINK REL=\"STYLESHEET\" TYPE=\"text/css\" "
2476 "HREF=\"/cups.css\">\n"
2484 httpStatus(code
), redirect
, httpStatus(code
), text
);
2486 if (httpPrintf(HTTP(con
), "Content-Type: text/html; charset=utf-8\r\n") < 0)
2488 if (httpPrintf(HTTP(con
), "Content-Length: %d\r\n",
2489 (int)strlen(message
)) < 0)
2491 if (httpPrintf(HTTP(con
), "\r\n") < 0)
2493 if (httpPrintf(HTTP(con
), "%s", message
) < 0)
2496 else if (httpPrintf(HTTP(con
), "\r\n") < 0)
2499 if (cupsdFlushHeader(con
) < 0)
2502 con
->http
.state
= HTTP_WAITING
;
2509 * 'cupsdSendHeader()' - Send an HTTP request.
2512 int /* O - 1 on success, 0 on failure */
2514 cupsd_client_t
*con
, /* I - Client to send to */
2515 http_status_t code
, /* I - HTTP status code */
2516 char *type
, /* I - MIME type of document */
2517 int auth_type
) /* I - Type of authentication */
2519 char auth_str
[1024]; /* Authorization string */
2520 #if 0 /* def HAVE_GSSAPI */
2521 static char *gss_buf
= NULL
; /* Kerberos auth data buffer */
2522 static int gss_bufsize
= 0; /* Size of Kerberos auth data buffer */
2523 #endif /* HAVE_GSSAPI */
2527 * Send the HTTP status header...
2530 if (code
== HTTP_CONTINUE
)
2533 * 100-continue doesn't send any headers...
2536 return (httpPrintf(HTTP(con
), "HTTP/%d.%d 100 Continue\r\n\r\n",
2537 con
->http
.version
/ 100, con
->http
.version
% 100) > 0);
2539 else if (code
== HTTP_WEBIF_DISABLED
)
2542 * Treat our special "web interface is disabled" status as "200 OK" for web
2549 httpFlushWrite(HTTP(con
));
2551 con
->http
.data_encoding
= HTTP_ENCODE_FIELDS
;
2553 if (httpPrintf(HTTP(con
), "HTTP/%d.%d %d %s\r\n", con
->http
.version
/ 100,
2554 con
->http
.version
% 100, code
, httpStatus(code
)) < 0)
2556 if (httpPrintf(HTTP(con
), "Date: %s\r\n", httpGetDateString(time(NULL
))) < 0)
2559 if (httpPrintf(HTTP(con
), "Server: %s\r\n", ServerHeader
) < 0)
2561 if (con
->http
.keep_alive
&& con
->http
.version
>= HTTP_1_0
)
2563 if (httpPrintf(HTTP(con
), "Connection: Keep-Alive\r\n") < 0)
2565 if (httpPrintf(HTTP(con
), "Keep-Alive: timeout=%d\r\n",
2566 KeepAliveTimeout
) < 0)
2569 if (code
== HTTP_METHOD_NOT_ALLOWED
)
2570 if (httpPrintf(HTTP(con
), "Allow: GET, HEAD, OPTIONS, POST, PUT\r\n") < 0)
2573 if (code
== HTTP_UNAUTHORIZED
)
2575 if (auth_type
== CUPSD_AUTH_NONE
)
2577 if (!con
->best
|| con
->best
->type
<= CUPSD_AUTH_NONE
)
2578 auth_type
= DefaultAuthType
;
2580 auth_type
= con
->best
->type
;
2585 if (auth_type
== CUPSD_AUTH_BASIC
|| auth_type
== CUPSD_AUTH_BASICDIGEST
)
2586 strlcpy(auth_str
, "Basic realm=\"CUPS\"", sizeof(auth_str
));
2587 else if (auth_type
== CUPSD_AUTH_DIGEST
)
2588 snprintf(auth_str
, sizeof(auth_str
), "Digest realm=\"CUPS\", nonce=\"%s\"",
2589 con
->http
.hostname
);
2591 else if (auth_type
== CUPSD_AUTH_NEGOTIATE
)
2594 if (_httpAddrFamily(con
->http
.hostaddr
) == AF_LOCAL
)
2595 strlcpy(auth_str
, "Basic realm=\"CUPS\"", sizeof(auth_str
));
2597 # endif /* AF_LOCAL */
2598 strlcpy(auth_str
, "Negotiate", sizeof(auth_str
));
2600 #endif /* HAVE_GSSAPI */
2602 if (con
->best
&& auth_type
!= CUPSD_AUTH_NEGOTIATE
&&
2603 !strcasecmp(con
->http
.hostname
, "localhost"))
2606 * Add a "trc" (try root certification) parameter for local non-Kerberos
2607 * requests when the request requires system group membership - then the
2608 * client knows the root certificate can/should be used.
2610 * Also, for Mac OS X we also look for @AUTHKEY and add an "authkey"
2611 * parameter as needed...
2614 char *name
, /* Current user name */
2615 *auth_key
; /* Auth key buffer */
2616 size_t auth_size
; /* Size of remaining buffer */
2618 auth_key
= auth_str
+ strlen(auth_str
);
2619 auth_size
= sizeof(auth_str
) - (auth_key
- auth_str
);
2621 for (name
= (char *)cupsArrayFirst(con
->best
->names
);
2623 name
= (char *)cupsArrayNext(con
->best
->names
))
2625 #ifdef HAVE_AUTHORIZATION_H
2626 if (!strncasecmp(name
, "@AUTHKEY(", 9))
2628 snprintf(auth_key
, auth_size
, ", authkey=\"%s\"", name
+ 9);
2629 /* end parenthesis is stripped in conf.c */
2633 #endif /* HAVE_AUTHORIZATION_H */
2634 if (!strcasecmp(name
, "@SYSTEM"))
2636 #ifdef HAVE_AUTHORIZATION_H
2637 if (SystemGroupAuthKey
)
2638 snprintf(auth_key
, auth_size
,
2640 SystemGroupAuthKey
);
2643 strlcpy(auth_key
, ", trc=\"y\"", auth_size
);
2644 #endif /* HAVE_AUTHORIZATION_H */
2652 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2653 "cupsdSendHeader: %d WWW-Authenticate: %s", con
->http
.fd
,
2656 if (httpPrintf(HTTP(con
), "WWW-Authenticate: %s\r\n", auth_str
) < 0)
2661 if (con
->language
&& strcmp(con
->language
->language
, "C"))
2663 if (httpPrintf(HTTP(con
), "Content-Language: %s\r\n",
2664 con
->language
->language
) < 0)
2670 if (!strcmp(type
, "text/html"))
2672 if (httpPrintf(HTTP(con
),
2673 "Content-Type: text/html; charset=utf-8\r\n") < 0)
2676 else if (httpPrintf(HTTP(con
), "Content-Type: %s\r\n", type
) < 0)
2685 * 'cupsdUpdateCGI()' - Read status messages from CGI scripts and programs.
2689 cupsdUpdateCGI(void)
2691 char *ptr
, /* Pointer to end of line in buffer */
2692 message
[1024]; /* Pointer to message text */
2693 int loglevel
; /* Log level for message */
2696 while ((ptr
= cupsdStatBufUpdate(CGIStatusBuffer
, &loglevel
,
2697 message
, sizeof(message
))) != NULL
)
2699 if (loglevel
== CUPSD_LOG_INFO
)
2700 cupsdLogMessage(CUPSD_LOG_INFO
, "%s", message
);
2702 if (!strchr(CGIStatusBuffer
->buffer
, '\n'))
2706 if (ptr
== NULL
&& !CGIStatusBuffer
->bufused
)
2709 * Fatal error on pipe - should never happen!
2712 cupsdLogMessage(CUPSD_LOG_CRIT
,
2713 "cupsdUpdateCGI: error reading from CGI error pipe - %s",
2720 * 'cupsdWriteClient()' - Write data to a client as needed.
2724 cupsdWriteClient(cupsd_client_t
*con
) /* I - Client connection */
2726 int bytes
, /* Number of bytes written */
2727 field_col
; /* Current column */
2728 char *bufptr
, /* Pointer into buffer */
2729 *bufend
; /* Pointer to end of buffer */
2730 ipp_state_t ipp_state
; /* IPP state value */
2733 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2734 "cupsdWriteClient(con=%p(%d)) response=%p(%d), file=%d "
2735 "pipe_pid=%d state=%d",
2736 con
, con
->http
.fd
, con
->response
,
2737 con
->response
? con
->response
->state
: -1,
2738 con
->file
, con
->pipe_pid
, con
->http
.state
);
2740 if (con
->http
.state
!= HTTP_GET_SEND
&&
2741 con
->http
.state
!= HTTP_POST_SEND
)
2744 * If we get called in the wrong state, then something went wrong with the
2745 * connection and we need to shut it down...
2748 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2749 "cupsdWriteClient: %d Closing on unknown HTTP state %d",
2750 con
->http
.fd
, con
->http
.state
);
2751 cupsdCloseClient(con
);
2758 * Make sure we select on the CGI output...
2761 cupsdAddSelect(con
->file
, (cupsd_selfunc_t
)write_pipe
, NULL
, con
);
2763 if (!con
->file_ready
)
2766 * Try again later when there is CGI output available...
2769 cupsdRemoveSelect(con
->http
.fd
);
2773 con
->file_ready
= 0;
2776 if (con
->response
&& con
->response
->state
!= IPP_DATA
)
2778 ipp_state
= ippWrite(HTTP(con
), con
->response
);
2779 bytes
= ipp_state
!= IPP_ERROR
&&
2780 (con
->file
>= 0 || ipp_state
!= IPP_DATA
);
2782 else if ((bytes
= read(con
->file
, con
->header
,
2783 sizeof(con
->header
) - con
->header_used
)) > 0)
2785 con
->header_used
+= bytes
;
2787 if (con
->pipe_pid
&& !con
->got_fields
)
2790 * Inspect the data for Content-Type and other fields.
2793 for (bufptr
= con
->header
, bufend
= con
->header
+ con
->header_used
,
2795 !con
->got_fields
&& bufptr
< bufend
;
2798 if (*bufptr
== '\n')
2801 * Send line to client...
2804 if (bufptr
> con
->header
&& bufptr
[-1] == '\r')
2808 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Script header: %s", con
->header
);
2810 if (!con
->sent_header
)
2813 * Handle redirection and CGI status codes...
2816 if (!strncasecmp(con
->header
, "Location:", 9))
2818 cupsdSendHeader(con
, HTTP_SEE_OTHER
, NULL
, CUPSD_AUTH_NONE
);
2819 con
->sent_header
= 2;
2821 if (httpPrintf(HTTP(con
), "Content-Length: 0\r\n") < 0)
2824 else if (!strncasecmp(con
->header
, "Status:", 7))
2826 cupsdSendError(con
, (http_status_t
)atoi(con
->header
+ 7),
2828 con
->sent_header
= 2;
2832 cupsdSendHeader(con
, HTTP_OK
, NULL
, CUPSD_AUTH_NONE
);
2833 con
->sent_header
= 1;
2835 if (con
->http
.version
== HTTP_1_1
)
2837 if (httpPrintf(HTTP(con
), "Transfer-Encoding: chunked\r\n") < 0)
2843 if (strncasecmp(con
->header
, "Status:", 7))
2844 httpPrintf(HTTP(con
), "%s\r\n", con
->header
);
2850 con
->header_used
-= bufptr
- con
->header
;
2852 if (con
->header_used
> 0)
2853 memmove(con
->header
, bufptr
, con
->header_used
);
2855 bufptr
= con
->header
- 1;
2858 * See if the line was empty...
2863 con
->got_fields
= 1;
2865 if (cupsdFlushHeader(con
) < 0)
2867 cupsdCloseClient(con
);
2871 if (con
->http
.version
== HTTP_1_1
)
2872 con
->http
.data_encoding
= HTTP_ENCODE_CHUNKED
;
2877 else if (*bufptr
!= '\r')
2881 if (!con
->got_fields
)
2883 con
->http
.activity
= time(NULL
);
2888 if (con
->header_used
> 0)
2890 if (httpWrite2(HTTP(con
), con
->header
, con
->header_used
) < 0)
2892 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2893 "cupsdWriteClient: %d Closing for error %d (%s)",
2894 con
->http
.fd
, con
->http
.error
,
2895 strerror(con
->http
.error
));
2896 cupsdCloseClient(con
);
2900 if (con
->http
.data_encoding
== HTTP_ENCODE_CHUNKED
)
2901 httpFlushWrite(HTTP(con
));
2903 con
->bytes
+= con
->header_used
;
2905 if (con
->http
.state
== HTTP_WAITING
)
2908 bytes
= con
->header_used
;
2910 con
->header_used
= 0;
2915 (con
->http
.state
!= HTTP_GET_SEND
&& con
->http
.state
!= HTTP_POST_SEND
))
2917 if (!con
->sent_header
&& con
->pipe_pid
)
2918 cupsdSendError(con
, HTTP_SERVER_ERROR
, CUPSD_AUTH_NONE
);
2921 cupsdLogRequest(con
, HTTP_OK
);
2923 httpFlushWrite(HTTP(con
));
2925 if (con
->http
.data_encoding
== HTTP_ENCODE_CHUNKED
&& con
->sent_header
== 1)
2927 if (httpWrite2(HTTP(con
), "", 0) < 0)
2929 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2930 "cupsdWriteClient: %d Closing for error %d (%s)",
2931 con
->http
.fd
, con
->http
.error
,
2932 strerror(con
->http
.error
));
2933 cupsdCloseClient(con
);
2939 con
->http
.state
= HTTP_WAITING
;
2941 cupsdAddSelect(con
->http
.fd
, (cupsd_selfunc_t
)cupsdReadClient
, NULL
, con
);
2945 cupsdRemoveSelect(con
->file
);
2948 cupsdEndProcess(con
->pipe_pid
, 0);
2957 unlink(con
->filename
);
2958 cupsdClearString(&con
->filename
);
2963 ippDelete(con
->request
);
2964 con
->request
= NULL
;
2969 ippDelete(con
->response
);
2970 con
->response
= NULL
;
2973 cupsdClearString(&con
->command
);
2974 cupsdClearString(&con
->options
);
2975 cupsdClearString(&con
->query_string
);
2977 if (!con
->http
.keep_alive
)
2979 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2980 "cupsdWriteClient: %d Closing because Keep-Alive disabled",
2982 cupsdCloseClient(con
);
2987 cupsArrayRemove(ActiveClients
, con
);
2988 cupsdSetBusyState();
2992 con
->http
.activity
= time(NULL
);
2997 * 'check_if_modified()' - Decode an "If-Modified-Since" line.
3000 static int /* O - 1 if modified since */
3002 cupsd_client_t
*con
, /* I - Client connection */
3003 struct stat
*filestats
) /* I - File information */
3005 char *ptr
; /* Pointer into field */
3006 time_t date
; /* Time/date value */
3007 off_t size
; /* Size/length value */
3012 ptr
= con
->http
.fields
[HTTP_FIELD_IF_MODIFIED_SINCE
];
3017 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3018 "check_if_modified(con=%p(%d), "
3019 "filestats=%p(" CUPS_LLFMT
", %d)) If-Modified-Since=\"%s\"",
3020 con
, con
->http
.fd
, filestats
, CUPS_LLCAST filestats
->st_size
,
3021 (int)filestats
->st_mtime
, ptr
);
3023 while (*ptr
!= '\0')
3025 while (isspace(*ptr
) || *ptr
== ';')
3028 if (strncasecmp(ptr
, "length=", 7) == 0)
3031 size
= strtoll(ptr
, NULL
, 10);
3033 while (isdigit(*ptr
))
3036 else if (isalpha(*ptr
))
3038 date
= httpGetDateTime(ptr
);
3039 while (*ptr
!= '\0' && *ptr
!= ';')
3046 return ((size
!= filestats
->st_size
&& size
!= 0) ||
3047 (date
< filestats
->st_mtime
&& date
!= 0) ||
3048 (size
== 0 && date
== 0));
3053 * 'compare_clients()' - Compare two client connections.
3056 static int /* O - Result of comparison */
3057 compare_clients(cupsd_client_t
*a
, /* I - First client */
3058 cupsd_client_t
*b
, /* I - Second client */
3059 void *data
) /* I - User data (not used) */
3073 * 'data_ready()' - Check whether data is available from a client.
3076 static int /* O - 1 if data is ready, 0 otherwise */
3077 data_ready(cupsd_client_t
*con
) /* I - Client */
3079 if (con
->http
.used
> 0)
3082 else if (con
->http
.tls
)
3085 if (SSL_pending((SSL
*)(con
->http
.tls
)))
3087 # elif defined(HAVE_GNUTLS)
3088 if (gnutls_record_check_pending(con
->http
.tls
))
3090 # elif defined(HAVE_CDSASSL)
3091 size_t bytes
; /* Bytes that are available */
3093 if (!SSLGetBufferedReadSize(con
->http
.tls
, &bytes
) && bytes
> 0)
3095 # endif /* HAVE_LIBSSL */
3097 #endif /* HAVE_SSL */
3105 * 'encrypt_client()' - Enable encryption for the client...
3108 static int /* O - 1 on success, 0 on error */
3109 encrypt_client(cupsd_client_t
*con
) /* I - Client to encrypt */
3112 SSL_CTX
*context
; /* Context for encryption */
3113 BIO
*bio
; /* BIO data */
3114 unsigned long error
; /* Error code */
3117 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "encrypt_client(con=%p(%d))", con
,
3121 * Verify that we have a certificate...
3124 if (access(ServerKey
, 0) || access(ServerCertificate
, 0))
3127 * Nope, make a self-signed certificate...
3130 if (!make_certificate(con
))
3135 * Create the SSL context and accept the connection...
3138 context
= SSL_CTX_new(SSLv23_server_method());
3140 SSL_CTX_set_options(context
, SSL_OP_NO_SSLv2
); /* Only use SSLv3 or TLS */
3141 if (SSLOptions
& CUPSD_SSL_NOEMPTY
)
3142 SSL_CTX_set_options(context
, SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS
);
3143 SSL_CTX_use_PrivateKey_file(context
, ServerKey
, SSL_FILETYPE_PEM
);
3144 SSL_CTX_use_certificate_chain_file(context
, ServerCertificate
);
3146 bio
= BIO_new(_httpBIOMethods());
3147 BIO_ctrl(bio
, BIO_C_SET_FILE_PTR
, 0, (char *)HTTP(con
));
3149 con
->http
.tls
= SSL_new(context
);
3150 SSL_set_bio(con
->http
.tls
, bio
, bio
);
3152 if (SSL_accept(con
->http
.tls
) != 1)
3154 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to encrypt connection from %s!",
3155 con
->http
.hostname
);
3157 while ((error
= ERR_get_error()) != 0)
3158 cupsdLogMessage(CUPSD_LOG_ERROR
, "%s", ERR_error_string(error
, NULL
));
3160 SSL_CTX_free(context
);
3161 SSL_free(con
->http
.tls
);
3162 con
->http
.tls
= NULL
;
3166 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Connection from %s now encrypted.",
3167 con
->http
.hostname
);
3171 # elif defined(HAVE_GNUTLS)
3172 int error
; /* Error code */
3173 gnutls_certificate_server_credentials
*credentials
;
3174 /* TLS credentials */
3177 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "encrypt_client(con=%p(%d))", con
,
3181 * Verify that we have a certificate...
3184 if (access(ServerKey
, 0) || access(ServerCertificate
, 0))
3187 * Nope, make a self-signed certificate...
3190 if (!make_certificate(con
))
3195 * Create the SSL object and perform the SSL handshake...
3198 credentials
= (gnutls_certificate_server_credentials
*)
3199 malloc(sizeof(gnutls_certificate_server_credentials
));
3200 if (credentials
== NULL
)
3202 cupsdLogMessage(CUPSD_LOG_ERROR
,
3203 "Unable to encrypt connection from %s - %s",
3204 con
->http
.hostname
, strerror(errno
));
3209 gnutls_certificate_allocate_credentials(credentials
);
3210 gnutls_certificate_set_x509_key_file(*credentials
, ServerCertificate
,
3211 ServerKey
, GNUTLS_X509_FMT_PEM
);
3213 gnutls_init(&con
->http
.tls
), GNUTLS_SERVER
);
3214 gnutls_set_default_priority(con
->http
.tls
);
3215 gnutls_credentials_set(con
->http
.tls
, GNUTLS_CRD_CERTIFICATE
, *credentials
);
3216 gnutls_transport_set_ptr(con
->http
.tls
, (gnutls_transport_ptr
)HTTP(con
));
3217 gnutls_transport_set_pull_function(con
->http
.tls
, _httpReadGNUTLS
);
3218 gnutls_transport_set_push_function(con
->http
.tls
, _httpWriteGNUTLS
);
3220 error
= gnutls_handshake(con
->http
.tls
);
3222 if (error
!= GNUTLS_E_SUCCESS
)
3224 cupsdLogMessage(CUPSD_LOG_ERROR
,
3225 "Unable to encrypt connection from %s - %s",
3226 con
->http
.hostname
, gnutls_strerror(error
));
3228 gnutls_deinit(con
->http
.tls
);
3229 gnutls_certificate_free_credentials(*credentials
);
3230 con
->http
.tls
= NULL
;
3235 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Connection from %s now encrypted.",
3236 con
->http
.hostname
);
3238 con
->http
.tls_credentials
= credentials
;
3241 # elif defined(HAVE_CDSASSL)
3242 OSStatus error
= 0; /* Error code */
3243 CFArrayRef peerCerts
; /* Peer certificates */
3246 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "encrypt_client(con=%p(%d))", con
,
3249 con
->http
.tls_credentials
= get_cdsa_certificate(con
);
3251 if (!con
->http
.tls_credentials
)
3254 * No keychain (yet), make a self-signed certificate...
3257 if (make_certificate(con
))
3258 con
->http
.tls_credentials
= get_cdsa_certificate(con
);
3261 if (!con
->http
.tls_credentials
)
3263 cupsdLogMessage(CUPSD_LOG_ERROR
,
3264 "Could not find signing key in keychain \"%s\"",
3266 error
= errSSLBadCert
; /* errSSLBadConfiguration is a better choice, but not available on 10.2.x */
3270 error
= SSLNewContext(true, &con
->http
.tls
);
3273 error
= SSLSetIOFuncs(con
->http
.tls
, _httpReadCDSA
, _httpWriteCDSA
);
3276 error
= SSLSetProtocolVersionEnabled(con
->http
.tls
, kSSLProtocol2
, false);
3279 error
= SSLSetConnection(con
->http
.tls
, HTTP(con
));
3282 error
= SSLSetAllowsExpiredCerts(con
->http
.tls
, true);
3285 error
= SSLSetAllowsAnyRoot(con
->http
.tls
, true);
3288 error
= SSLSetCertificate(con
->http
.tls
, con
->http
.tls_credentials
);
3293 * Perform SSL/TLS handshake
3296 while ((error
= SSLHandshake(con
->http
.tls
)) == errSSLWouldBlock
)
3302 cupsdLogMessage(CUPSD_LOG_ERROR
,
3303 "Unable to encrypt connection from %s - %s (%d)",
3304 con
->http
.hostname
, cssmErrorString(error
), (int)error
);
3306 con
->http
.error
= error
;
3307 con
->http
.status
= HTTP_ERROR
;
3311 SSLDisposeContext(con
->http
.tls
);
3312 con
->http
.tls
= NULL
;
3315 if (con
->http
.tls_credentials
)
3317 CFRelease(con
->http
.tls_credentials
);
3318 con
->http
.tls_credentials
= NULL
;
3324 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Connection from %s now encrypted.",
3325 con
->http
.hostname
);
3327 if (!SSLCopyPeerCertificates(con
->http
.tls
, &peerCerts
) && peerCerts
)
3329 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Received %d peer certificates!",
3330 (int)CFArrayGetCount(peerCerts
));
3331 CFRelease(peerCerts
);
3334 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Received NO peer certificates!");
3338 # endif /* HAVE_LIBSSL */
3340 #endif /* HAVE_SSL */
3345 * 'get_cdsa_certificate()' - Get a SSL/TLS certificate from the System keychain.
3348 static CFArrayRef
/* O - Array of certificates */
3349 get_cdsa_certificate(
3350 cupsd_client_t
*con
) /* I - Client connection */
3352 OSStatus err
; /* Error info */
3353 SecKeychainRef keychain
= NULL
;/* Keychain reference */
3354 SecIdentitySearchRef search
= NULL
; /* Search reference */
3355 SecIdentityRef identity
= NULL
;/* Identity */
3356 CFArrayRef certificates
= NULL
;
3357 /* Certificate array */
3358 # if HAVE_SECPOLICYCREATESSL
3359 SecPolicyRef policy
= NULL
; /* Policy ref */
3360 CFStringRef servername
= NULL
;
3362 CFMutableDictionaryRef query
= NULL
; /* Query qualifiers */
3363 char localname
[1024];/* Local hostname */
3364 # elif defined(HAVE_SECIDENTITYSEARCHCREATEWITHPOLICY)
3365 SecPolicyRef policy
= NULL
; /* Policy ref */
3366 SecPolicySearchRef policy_search
= NULL
;
3367 /* Policy search ref */
3368 CSSM_DATA options
; /* Policy options */
3369 CSSM_APPLE_TP_SSL_OPTIONS
3370 ssl_options
; /* SSL Option for hostname */
3371 char localname
[1024];/* Local hostname */
3372 # endif /* HAVE_SECPOLICYCREATESSL */
3375 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3376 "get_cdsa_certificate: Looking for certs for \"%s\"...",
3379 if ((err
= SecKeychainOpen(ServerCertificate
, &keychain
)))
3381 cupsdLogMessage(CUPSD_LOG_ERROR
, "Cannot open keychain \"%s\" - %s (%d)",
3382 ServerCertificate
, cssmErrorString(err
), (int)err
);
3386 # if HAVE_SECPOLICYCREATESSL
3387 servername
= CFStringCreateWithCString(kCFAllocatorDefault
, con
->servername
,
3388 kCFStringEncodingUTF8
);
3390 policy
= SecPolicyCreateSSL(1, servername
);
3393 CFRelease(servername
);
3397 cupsdLogMessage(CUPSD_LOG_ERROR
, "Cannot create ssl policy reference");
3401 if (!(query
= CFDictionaryCreateMutable(kCFAllocatorDefault
, 0,
3402 &kCFTypeDictionaryKeyCallBacks
,
3403 &kCFTypeDictionaryValueCallBacks
)))
3405 cupsdLogMessage(CUPSD_LOG_ERROR
, "Cannot create query dictionary");
3409 CFDictionaryAddValue(query
, kSecClass
, kSecClassIdentity
);
3410 CFDictionaryAddValue(query
, kSecMatchPolicy
, policy
);
3411 CFDictionaryAddValue(query
, kSecReturnRef
, kCFBooleanTrue
);
3412 CFDictionaryAddValue(query
, kSecMatchLimit
, kSecMatchLimitOne
);
3414 err
= SecItemCopyMatching(query
, (CFTypeRef
*)&identity
);
3416 if (err
&& DNSSDHostName
)
3419 * Search for the connection server name failed; try the DNS-SD .local
3420 * hostname instead...
3423 snprintf(localname
, sizeof(localname
), "%s.local", DNSSDHostName
);
3425 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3426 "get_cdsa_certificate: Looking for certs for \"%s\"...",
3429 servername
= CFStringCreateWithCString(kCFAllocatorDefault
, localname
,
3430 kCFStringEncodingUTF8
);
3434 policy
= SecPolicyCreateSSL(1, servername
);
3437 CFRelease(servername
);
3441 cupsdLogMessage(CUPSD_LOG_ERROR
, "Cannot create ssl policy reference");
3445 CFDictionarySetValue(query
, kSecMatchPolicy
, policy
);
3447 err
= SecItemCopyMatching(query
, (CFTypeRef
*)&identity
);
3452 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3453 "Cannot find signing key in keychain \"%s\": %s (%d)",
3454 ServerCertificate
, cssmErrorString(err
), (int)err
);
3458 # elif defined(HAVE_SECIDENTITYSEARCHCREATEWITHPOLICY)
3460 * Use a policy to search for valid certificates whose common name matches the
3464 if (SecPolicySearchCreate(CSSM_CERT_X_509v3
, &CSSMOID_APPLE_TP_SSL
,
3465 NULL
, &policy_search
))
3467 cupsdLogMessage(CUPSD_LOG_ERROR
, "Cannot create a policy search reference");
3471 if (SecPolicySearchCopyNext(policy_search
, &policy
))
3473 cupsdLogMessage(CUPSD_LOG_ERROR
,
3474 "Cannot find a policy to use for searching");
3478 memset(&ssl_options
, 0, sizeof(ssl_options
));
3479 ssl_options
.Version
= CSSM_APPLE_TP_SSL_OPTS_VERSION
;
3480 ssl_options
.ServerName
= con
->servername
;
3481 ssl_options
.ServerNameLen
= strlen(con
->servername
);
3483 options
.Data
= (uint8
*)&ssl_options
;
3484 options
.Length
= sizeof(ssl_options
);
3486 if (SecPolicySetValue(policy
, &options
))
3488 cupsdLogMessage(CUPSD_LOG_ERROR
,
3489 "Cannot set policy value to use for searching");
3493 if ((err
= SecIdentitySearchCreateWithPolicy(policy
, NULL
, CSSM_KEYUSE_SIGN
,
3494 keychain
, FALSE
, &search
)))
3496 cupsdLogMessage(CUPSD_LOG_ERROR
,
3497 "Cannot create identity search reference: %s (%d)",
3498 cssmErrorString(err
), (int)err
);
3502 err
= SecIdentitySearchCopyNext(search
, &identity
);
3504 if (err
&& DNSSDHostName
)
3507 * Search for the connection server name failed; try the DNS-SD .local
3508 * hostname instead...
3511 snprintf(localname
, sizeof(localname
), "%s.local", DNSSDHostName
);
3513 ssl_options
.ServerName
= localname
;
3514 ssl_options
.ServerNameLen
= strlen(localname
);
3516 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3517 "get_cdsa_certificate: Looking for certs for \"%s\"...",
3520 if (SecPolicySetValue(policy
, &options
))
3522 cupsdLogMessage(CUPSD_LOG_ERROR
,
3523 "Cannot set policy value to use for searching");
3529 if ((err
= SecIdentitySearchCreateWithPolicy(policy
, NULL
, CSSM_KEYUSE_SIGN
,
3530 keychain
, FALSE
, &search
)))
3532 cupsdLogMessage(CUPSD_LOG_ERROR
,
3533 "Cannot create identity search reference: %s (%d)",
3534 cssmErrorString(err
), (int)err
);
3538 err
= SecIdentitySearchCopyNext(search
, &identity
);
3544 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3545 "Cannot find signing key in keychain \"%s\": %s (%d)",
3546 ServerCertificate
, cssmErrorString(err
), (int)err
);
3552 * Assume there is exactly one SecIdentity in the keychain...
3555 if ((err
= SecIdentitySearchCreate(keychain
, CSSM_KEYUSE_SIGN
, &search
)))
3557 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3558 "Cannot create identity search reference (%d)", (int)err
);
3562 if ((err
= SecIdentitySearchCopyNext(search
, &identity
)))
3564 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3565 "Cannot find signing key in keychain \"%s\": %s (%d)",
3566 ServerCertificate
, cssmErrorString(err
), (int)err
);
3569 # endif /* HAVE_SECPOLICYCREATESSL */
3571 if (CFGetTypeID(identity
) != SecIdentityGetTypeID())
3573 cupsdLogMessage(CUPSD_LOG_ERROR
, "SecIdentity CFTypeID failure!");
3577 if ((certificates
= CFArrayCreate(NULL
, (const void **)&identity
,
3578 1, &kCFTypeArrayCallBacks
)) == NULL
)
3580 cupsdLogMessage(CUPSD_LOG_ERROR
, "Cannot create certificate array");
3587 CFRelease(keychain
);
3591 CFRelease(identity
);
3593 # if HAVE_SECPOLICYCREATESSL
3598 # elif defined(HAVE_SECIDENTITYSEARCHCREATEWITHPOLICY)
3602 CFRelease(policy_search
);
3603 # endif /* HAVE_SECPOLICYCREATESSL */
3605 return (certificates
);
3607 #endif /* HAVE_CDSASSL */
3611 * 'get_file()' - Get a filename and state info.
3614 static char * /* O - Real filename */
3615 get_file(cupsd_client_t
*con
, /* I - Client connection */
3616 struct stat
*filestats
, /* O - File information */
3617 char *filename
, /* IO - Filename buffer */
3618 int len
) /* I - Buffer length */
3620 int status
; /* Status of filesystem calls */
3621 char *ptr
; /* Pointer info filename */
3622 int plen
; /* Remaining length after pointer */
3623 char language
[7]; /* Language subdirectory, if any */
3627 * Figure out the real filename...
3632 if (!strncmp(con
->uri
, "/ppd/", 5) && !strchr(con
->uri
+ 5, '/'))
3633 snprintf(filename
, len
, "%s%s", ServerRoot
, con
->uri
);
3634 else if (!strncmp(con
->uri
, "/icons/", 7) && !strchr(con
->uri
+ 7, '/'))
3636 snprintf(filename
, len
, "%s/%s", CacheDir
, con
->uri
+ 7);
3637 if (access(filename
, F_OK
) < 0)
3638 snprintf(filename
, len
, "%s/images/generic.png", DocumentRoot
);
3640 else if (!strncmp(con
->uri
, "/rss/", 5) && !strchr(con
->uri
+ 5, '/'))
3641 snprintf(filename
, len
, "%s/rss/%s", CacheDir
, con
->uri
+ 5);
3642 else if (!strncmp(con
->uri
, "/admin/conf/", 12))
3643 snprintf(filename
, len
, "%s%s", ServerRoot
, con
->uri
+ 11);
3644 else if (!strncmp(con
->uri
, "/admin/log/", 11))
3646 if (!strncmp(con
->uri
+ 11, "access_log", 10) && AccessLog
[0] == '/')
3647 strlcpy(filename
, AccessLog
, len
);
3648 else if (!strncmp(con
->uri
+ 11, "error_log", 9) && ErrorLog
[0] == '/')
3649 strlcpy(filename
, ErrorLog
, len
);
3650 else if (!strncmp(con
->uri
+ 11, "page_log", 8) && PageLog
[0] == '/')
3651 strlcpy(filename
, PageLog
, len
);
3655 else if (con
->language
)
3657 snprintf(language
, sizeof(language
), "/%s", con
->language
->language
);
3658 snprintf(filename
, len
, "%s%s%s", DocumentRoot
, language
, con
->uri
);
3661 snprintf(filename
, len
, "%s%s", DocumentRoot
, con
->uri
);
3663 if ((ptr
= strchr(filename
, '?')) != NULL
)
3667 * Grab the status for this language; if there isn't a language-specific file
3668 * then fallback to the default one...
3671 if ((status
= stat(filename
, filestats
)) != 0 && language
[0] &&
3672 strncmp(con
->uri
, "/icons/", 7) &&
3673 strncmp(con
->uri
, "/ppd/", 5) &&
3674 strncmp(con
->uri
, "/rss/", 5) &&
3675 strncmp(con
->uri
, "/admin/conf/", 12) &&
3676 strncmp(con
->uri
, "/admin/log/", 11))
3679 * Drop the country code...
3683 snprintf(filename
, len
, "%s%s%s", DocumentRoot
, language
, con
->uri
);
3685 if ((ptr
= strchr(filename
, '?')) != NULL
)
3688 if ((status
= stat(filename
, filestats
)) != 0)
3691 * Drop the language prefix and try the root directory...
3695 snprintf(filename
, len
, "%s%s", DocumentRoot
, con
->uri
);
3697 if ((ptr
= strchr(filename
, '?')) != NULL
)
3700 status
= stat(filename
, filestats
);
3705 * If we're found a directory, get the index.html file instead...
3708 if (!status
&& S_ISDIR(filestats
->st_mode
))
3711 * Make sure the URI ends with a slash...
3714 if (con
->uri
[strlen(con
->uri
) - 1] != '/')
3715 strlcat(con
->uri
, "/", sizeof(con
->uri
));
3718 * Find the directory index file, trying every language...
3723 if (status
&& language
[0])
3726 * Try a different language subset...
3730 language
[0] = '\0'; /* Strip country code */
3732 language
[0] = '\0'; /* Strip language */
3736 * Look for the index file...
3739 snprintf(filename
, len
, "%s%s%s", DocumentRoot
, language
, con
->uri
);
3741 if ((ptr
= strchr(filename
, '?')) != NULL
)
3744 ptr
= filename
+ strlen(filename
);
3745 plen
= len
- (ptr
- filename
);
3747 strlcpy(ptr
, "index.html", plen
);
3748 status
= stat(filename
, filestats
);
3753 strlcpy(ptr
, "index.class", plen
);
3754 status
= stat(filename
, filestats
);
3756 #endif /* HAVE_JAVA */
3761 strlcpy(ptr
, "index.pl", plen
);
3762 status
= stat(filename
, filestats
);
3764 #endif /* HAVE_PERL */
3769 strlcpy(ptr
, "index.php", plen
);
3770 status
= stat(filename
, filestats
);
3772 #endif /* HAVE_PHP */
3777 strlcpy(ptr
, "index.pyc", plen
);
3778 status
= stat(filename
, filestats
);
3783 strlcpy(ptr
, "index.py", plen
);
3784 status
= stat(filename
, filestats
);
3786 #endif /* HAVE_PYTHON */
3789 while (status
&& language
[0]);
3792 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3793 "get_file(con=%p(%d), filestats=%p, filename=%p, len=%d) = "
3794 "%s", con
, con
->http
.fd
, filestats
, filename
, len
,
3795 status
? "(null)" : filename
);
3805 * 'install_conf_file()' - Install a configuration file.
3808 static http_status_t
/* O - Status */
3809 install_conf_file(cupsd_client_t
*con
) /* I - Connection */
3811 cups_file_t
*in
, /* Input file */
3812 *out
; /* Output file */
3813 char buffer
[1024]; /* Copy buffer */
3814 int bytes
; /* Number of bytes */
3815 char conffile
[1024], /* Configuration filename */
3816 newfile
[1024], /* New config filename */
3817 oldfile
[1024]; /* Old config filename */
3818 struct stat confinfo
; /* Config file info */
3822 * First construct the filenames...
3825 snprintf(conffile
, sizeof(conffile
), "%s%s", ServerRoot
, con
->uri
+ 11);
3826 snprintf(newfile
, sizeof(newfile
), "%s%s.N", ServerRoot
, con
->uri
+ 11);
3827 snprintf(oldfile
, sizeof(oldfile
), "%s%s.O", ServerRoot
, con
->uri
+ 11);
3829 cupsdLogMessage(CUPSD_LOG_INFO
, "Installing config file \"%s\"...", conffile
);
3832 * Get the owner, group, and permissions of the configuration file.
3833 * If it doesn't exist, assign it to the User and Group in the
3834 * cupsd.conf file with mode 0640 permissions.
3837 if (stat(conffile
, &confinfo
))
3839 confinfo
.st_uid
= User
;
3840 confinfo
.st_gid
= Group
;
3841 confinfo
.st_mode
= ConfigFilePerm
;
3845 * Open the request file and new config file...
3848 if ((in
= cupsFileOpen(con
->filename
, "rb")) == NULL
)
3850 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to open request file \"%s\" - %s",
3851 con
->filename
, strerror(errno
));
3852 return (HTTP_SERVER_ERROR
);
3855 if ((out
= cupsFileOpen(newfile
, "wb")) == NULL
)
3858 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to open config file \"%s\" - %s",
3859 newfile
, strerror(errno
));
3860 return (HTTP_SERVER_ERROR
);
3863 fchmod(cupsFileNumber(out
), confinfo
.st_mode
);
3864 fchown(cupsFileNumber(out
), confinfo
.st_uid
, confinfo
.st_gid
);
3867 * Copy from the request to the new config file...
3870 while ((bytes
= cupsFileRead(in
, buffer
, sizeof(buffer
))) > 0)
3871 if (cupsFileWrite(out
, buffer
, bytes
) < bytes
)
3873 cupsdLogMessage(CUPSD_LOG_ERROR
,
3874 "Unable to copy to config file \"%s\" - %s",
3875 newfile
, strerror(errno
));
3881 return (HTTP_SERVER_ERROR
);
3885 * Close the files...
3889 if (cupsFileClose(out
))
3891 cupsdLogMessage(CUPSD_LOG_ERROR
,
3892 "Error file closing config file \"%s\" - %s",
3893 newfile
, strerror(errno
));
3897 return (HTTP_SERVER_ERROR
);
3901 * Remove the request file...
3904 unlink(con
->filename
);
3905 cupsdClearString(&con
->filename
);
3908 * Unlink the old backup, rename the current config file to the backup
3909 * filename, and rename the new config file to the config file name...
3912 if (unlink(oldfile
))
3913 if (errno
!= ENOENT
)
3915 cupsdLogMessage(CUPSD_LOG_ERROR
,
3916 "Unable to remove backup config file \"%s\" - %s",
3917 oldfile
, strerror(errno
));
3921 return (HTTP_SERVER_ERROR
);
3924 if (rename(conffile
, oldfile
))
3925 if (errno
!= ENOENT
)
3927 cupsdLogMessage(CUPSD_LOG_ERROR
,
3928 "Unable to rename old config file \"%s\" - %s",
3929 conffile
, strerror(errno
));
3933 return (HTTP_SERVER_ERROR
);
3936 if (rename(newfile
, conffile
))
3938 cupsdLogMessage(CUPSD_LOG_ERROR
,
3939 "Unable to rename new config file \"%s\" - %s",
3940 newfile
, strerror(errno
));
3942 rename(oldfile
, conffile
);
3945 return (HTTP_SERVER_ERROR
);
3949 * If the cupsd.conf file was updated, set the NeedReload flag...
3952 if (!strcmp(con
->uri
, "/admin/conf/cupsd.conf"))
3953 NeedReload
= RELOAD_CUPSD
;
3955 NeedReload
= RELOAD_ALL
;
3957 ReloadTime
= time(NULL
);
3960 * Return that the file was created successfully...
3963 return (HTTP_CREATED
);
3968 * 'is_cgi()' - Is the resource a CGI script/program?
3971 static int /* O - 1 = CGI, 0 = file */
3972 is_cgi(cupsd_client_t
*con
, /* I - Client connection */
3973 const char *filename
, /* I - Real filename */
3974 struct stat
*filestats
, /* I - File information */
3975 mime_type_t
*type
) /* I - MIME type */
3977 const char *options
; /* Options on URL */
3981 * Get the options, if any...
3984 if ((options
= strchr(con
->uri
, '?')) != NULL
)
3987 cupsdSetStringf(&(con
->query_string
), "QUERY_STRING=%s", options
);
3991 * Check for known types...
3994 if (!type
|| strcasecmp(type
->super
, "application"))
3996 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3997 "is_cgi(con=%p(%d), filename=\"%s\", filestats=%p, "
3998 "type=%s/%s) = 0", con
, con
->http
.fd
, filename
, filestats
,
3999 type
? type
->super
: "unknown",
4000 type
? type
->type
: "unknown");
4004 if (!strcasecmp(type
->type
, "x-httpd-cgi") &&
4005 (filestats
->st_mode
& 0111))
4008 * "application/x-httpd-cgi" is a CGI script.
4011 cupsdSetString(&con
->command
, filename
);
4014 cupsdSetStringf(&con
->options
, " %s", options
);
4016 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4017 "is_cgi(con=%p(%d), filename=\"%s\", filestats=%p, "
4018 "type=%s/%s) = 1", con
, con
->http
.fd
, filename
, filestats
,
4019 type
? type
->super
: "unknown",
4020 type
? type
->type
: "unknown");
4024 else if (!strcasecmp(type
->type
, "x-httpd-java"))
4027 * "application/x-httpd-java" is a Java servlet.
4030 cupsdSetString(&con
->command
, CUPS_JAVA
);
4033 cupsdSetStringf(&con
->options
, " %s %s", filename
, options
);
4035 cupsdSetStringf(&con
->options
, " %s", filename
);
4037 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4038 "is_cgi(con=%p(%d), filename=\"%s\", filestats=%p, "
4039 "type=%s/%s) = 1", con
, con
->http
.fd
, filename
, filestats
,
4040 type
? type
->super
: "unknown",
4041 type
? type
->type
: "unknown");
4044 #endif /* HAVE_JAVA */
4046 else if (!strcasecmp(type
->type
, "x-httpd-perl"))
4049 * "application/x-httpd-perl" is a Perl page.
4052 cupsdSetString(&con
->command
, CUPS_PERL
);
4055 cupsdSetStringf(&con
->options
, " %s %s", filename
, options
);
4057 cupsdSetStringf(&con
->options
, " %s", filename
);
4059 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4060 "is_cgi(con=%p(%d), filename=\"%s\", filestats=%p, "
4061 "type=%s/%s) = 1", con
, con
->http
.fd
, filename
, filestats
,
4062 type
? type
->super
: "unknown",
4063 type
? type
->type
: "unknown");
4066 #endif /* HAVE_PERL */
4068 else if (!strcasecmp(type
->type
, "x-httpd-php"))
4071 * "application/x-httpd-php" is a PHP page.
4074 cupsdSetString(&con
->command
, CUPS_PHP
);
4077 cupsdSetStringf(&con
->options
, " %s %s", filename
, options
);
4079 cupsdSetStringf(&con
->options
, " %s", filename
);
4081 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4082 "is_cgi(con=%p(%d), filename=\"%s\", filestats=%p, "
4083 "type=%s/%s) = 1", con
, con
->http
.fd
, filename
, filestats
,
4084 type
? type
->super
: "unknown",
4085 type
? type
->type
: "unknown");
4088 #endif /* HAVE_PHP */
4090 else if (!strcasecmp(type
->type
, "x-httpd-python"))
4093 * "application/x-httpd-python" is a Python page.
4096 cupsdSetString(&con
->command
, CUPS_PYTHON
);
4099 cupsdSetStringf(&con
->options
, " %s %s", filename
, options
);
4101 cupsdSetStringf(&con
->options
, " %s", filename
);
4103 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4104 "is_cgi(con=%p(%d), filename=\"%s\", filestats=%p, "
4105 "type=%s/%s) = 1", con
, con
->http
.fd
, filename
, filestats
,
4106 type
? type
->super
: "unknown",
4107 type
? type
->type
: "unknown");
4110 #endif /* HAVE_PYTHON */
4112 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4113 "is_cgi(con=%p(%d), filename=\"%s\", filestats=%p, "
4114 "type=%s/%s) = 0", con
, con
->http
.fd
, filename
, filestats
,
4115 type
? type
->super
: "unknown",
4116 type
? type
->type
: "unknown");
4122 * 'is_path_absolute()' - Is a path absolute and free of relative elements (i.e. "..").
4125 static int /* O - 0 if relative, 1 if absolute */
4126 is_path_absolute(const char *path
) /* I - Input path */
4129 * Check for a leading slash...
4136 * Check for "/.." in the path...
4139 while ((path
= strstr(path
, "/..")) != NULL
)
4141 if (!path
[3] || path
[3] == '/')
4148 * If we haven't found any relative paths, return 1 indicating an
4158 * 'make_certificate()' - Make a self-signed SSL/TLS certificate.
4161 static int /* O - 1 on success, 0 on failure */
4162 make_certificate(cupsd_client_t
*con
) /* I - Client connection */
4164 #if defined(HAVE_LIBSSL) && defined(HAVE_WAITPID)
4165 int pid
, /* Process ID of command */
4166 status
; /* Status of command */
4167 char command
[1024], /* Command */
4168 *argv
[12], /* Command-line arguments */
4169 *envp
[MAX_ENV
+ 1], /* Environment variables */
4170 infofile
[1024], /* Type-in information for cert */
4171 seedfile
[1024]; /* Random number seed file */
4172 int envc
, /* Number of environment variables */
4173 bytes
; /* Bytes written */
4174 cups_file_t
*fp
; /* Seed/info file */
4175 int infofd
; /* Info file descriptor */
4179 * Run the "openssl" command to seed the random number generator and
4180 * generate a self-signed certificate that is good for 10 years:
4182 * openssl rand -rand seedfile 1
4184 * openssl req -new -x509 -keyout ServerKey \
4185 * -out ServerCertificate -days 3650 -nodes
4187 * The seeding step is crucial in ensuring that the openssl command
4188 * does not block on systems without sufficient entropy...
4191 if (!cupsFileFind("openssl", getenv("PATH"), 1, command
, sizeof(command
)))
4193 cupsdLogMessage(CUPSD_LOG_ERROR
,
4194 "No SSL certificate and openssl command not found!");
4198 if (access("/dev/urandom", 0))
4201 * If the system doesn't provide /dev/urandom, then any random source
4202 * will probably be blocking-style, so generate some random data to
4203 * use as a seed for the certificate. Note that we have already
4204 * seeded the random number generator in cupsdInitCerts()...
4207 cupsdLogMessage(CUPSD_LOG_INFO
,
4208 "Seeding the random number generator...");
4211 * Write the seed file...
4214 if ((fp
= cupsTempFile2(seedfile
, sizeof(seedfile
))) == NULL
)
4216 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to create seed file %s - %s",
4217 seedfile
, strerror(errno
));
4221 for (bytes
= 0; bytes
< 262144; bytes
++)
4222 cupsFilePutChar(fp
, random());
4227 * Run the openssl command to seed its random number generator...
4230 argv
[0] = "openssl";
4237 envc
= cupsdLoadEnv(envp
, MAX_ENV
);
4240 if (!cupsdStartProcess(command
, argv
, envp
, -1, -1, -1, -1, -1, 1, NULL
,
4247 while (waitpid(pid
, &status
, 0) < 0)
4254 cupsdFinishProcess(pid
, command
, sizeof(command
), NULL
);
4257 * Remove the seed file, as it is no longer needed...
4264 if (WIFEXITED(status
))
4265 cupsdLogMessage(CUPSD_LOG_ERROR
,
4266 "Unable to seed random number generator - "
4267 "the openssl command stopped with status %d!",
4268 WEXITSTATUS(status
));
4270 cupsdLogMessage(CUPSD_LOG_ERROR
,
4271 "Unable to seed random number generator - "
4272 "the openssl command crashed on signal %d!",
4280 * Create a file with the certificate information fields...
4282 * Note: This assumes that the default questions are asked by the openssl
4286 if ((fp
= cupsTempFile2(infofile
, sizeof(infofile
))) == NULL
)
4288 cupsdLogMessage(CUPSD_LOG_ERROR
,
4289 "Unable to create certificate information file %s - %s",
4290 infofile
, strerror(errno
));
4294 cupsFilePrintf(fp
, ".\n.\n.\n%s\n.\n%s\n%s\n",
4295 ServerName
, ServerName
, ServerAdmin
);
4298 cupsdLogMessage(CUPSD_LOG_INFO
,
4299 "Generating SSL server key and certificate...");
4301 argv
[0] = "openssl";
4305 argv
[4] = "-keyout";
4306 argv
[5] = ServerKey
;
4308 argv
[7] = ServerCertificate
;
4311 argv
[10] = "-nodes";
4314 cupsdLoadEnv(envp
, MAX_ENV
);
4316 infofd
= open(infofile
, O_RDONLY
);
4318 if (!cupsdStartProcess(command
, argv
, envp
, infofd
, -1, -1, -1, -1, 1, NULL
,
4329 while (waitpid(pid
, &status
, 0) < 0)
4336 cupsdFinishProcess(pid
, command
, sizeof(command
), NULL
);
4340 if (WIFEXITED(status
))
4341 cupsdLogMessage(CUPSD_LOG_ERROR
,
4342 "Unable to create SSL server key and certificate - "
4343 "the openssl command stopped with status %d!",
4344 WEXITSTATUS(status
));
4346 cupsdLogMessage(CUPSD_LOG_ERROR
,
4347 "Unable to create SSL server key and certificate - "
4348 "the openssl command crashed on signal %d!",
4353 cupsdLogMessage(CUPSD_LOG_INFO
, "Created SSL server key file \"%s\"...",
4355 cupsdLogMessage(CUPSD_LOG_INFO
,
4356 "Created SSL server certificate file \"%s\"...",
4362 #elif defined(HAVE_GNUTLS)
4363 gnutls_x509_crt crt
; /* Self-signed certificate */
4364 gnutls_x509_privkey key
; /* Encryption key */
4365 cups_lang_t
*language
; /* Default language info */
4366 cups_file_t
*fp
; /* Key/cert file */
4367 unsigned char buffer
[8192]; /* Buffer for x509 data */
4368 size_t bytes
; /* Number of bytes of data */
4369 unsigned char serial
[4]; /* Serial number buffer */
4370 time_t curtime
; /* Current time */
4371 int result
; /* Result of GNU TLS calls */
4375 * Create the encryption key...
4378 cupsdLogMessage(CUPSD_LOG_INFO
, "Generating SSL server key...");
4380 gnutls_x509_privkey_init(&key
);
4381 gnutls_x509_privkey_generate(key
, GNUTLS_PK_RSA
, 2048, 0);
4387 bytes
= sizeof(buffer
);
4389 if ((result
= gnutls_x509_privkey_export(key
, GNUTLS_X509_FMT_PEM
,
4390 buffer
, &bytes
)) < 0)
4392 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to export SSL server key - %s",
4393 gnutls_strerror(result
));
4394 gnutls_x509_privkey_deinit(key
);
4397 else if ((fp
= cupsFileOpen(ServerKey
, "w")) != NULL
)
4399 cupsFileWrite(fp
, (char *)buffer
, bytes
);
4402 cupsdLogMessage(CUPSD_LOG_INFO
, "Created SSL server key file \"%s\"...",
4407 cupsdLogMessage(CUPSD_LOG_ERROR
,
4408 "Unable to create SSL server key file \"%s\" - %s",
4409 ServerKey
, strerror(errno
));
4410 gnutls_x509_privkey_deinit(key
);
4415 * Create the self-signed certificate...
4418 cupsdLogMessage(CUPSD_LOG_INFO
, "Generating self-signed SSL certificate...");
4420 language
= cupsLangDefault();
4421 curtime
= time(NULL
);
4422 serial
[0] = curtime
>> 24;
4423 serial
[1] = curtime
>> 16;
4424 serial
[2] = curtime
>> 8;
4425 serial
[3] = curtime
;
4427 gnutls_x509_crt_init(&crt
);
4428 if (strlen(language
->language
) == 5)
4429 gnutls_x509_crt_set_dn_by_oid(crt
, GNUTLS_OID_X520_COUNTRY_NAME
, 0,
4430 language
->language
+ 3, 2);
4432 gnutls_x509_crt_set_dn_by_oid(crt
, GNUTLS_OID_X520_COUNTRY_NAME
, 0,
4434 gnutls_x509_crt_set_dn_by_oid(crt
, GNUTLS_OID_X520_COMMON_NAME
, 0,
4435 ServerName
, strlen(ServerName
));
4436 gnutls_x509_crt_set_dn_by_oid(crt
, GNUTLS_OID_X520_ORGANIZATION_NAME
, 0,
4437 ServerName
, strlen(ServerName
));
4438 gnutls_x509_crt_set_dn_by_oid(crt
, GNUTLS_OID_X520_ORGANIZATIONAL_UNIT_NAME
,
4440 gnutls_x509_crt_set_dn_by_oid(crt
, GNUTLS_OID_X520_STATE_OR_PROVINCE_NAME
, 0,
4442 gnutls_x509_crt_set_dn_by_oid(crt
, GNUTLS_OID_X520_LOCALITY_NAME
, 0,
4444 gnutls_x509_crt_set_dn_by_oid(crt
, GNUTLS_OID_PKCS9_EMAIL
, 0,
4445 ServerAdmin
, strlen(ServerAdmin
));
4446 gnutls_x509_crt_set_key(crt
, key
);
4447 gnutls_x509_crt_set_serial(crt
, serial
, sizeof(serial
));
4448 gnutls_x509_crt_set_activation_time(crt
, curtime
);
4449 gnutls_x509_crt_set_expiration_time(crt
, curtime
+ 10 * 365 * 86400);
4450 gnutls_x509_crt_set_ca_status(crt
, 0);
4451 gnutls_x509_crt_set_subject_alternative_name(crt
, GNUTLS_SAN_DNSNAME
,
4453 gnutls_x509_crt_set_key_purpose_oid(crt
, GNUTLS_KP_TLS_WWW_SERVER
, 0);
4454 gnutls_x509_crt_set_key_usage(crt
, GNUTLS_KEY_KEY_ENCIPHERMENT
);
4455 gnutls_x509_crt_set_version(crt
, 3);
4457 bytes
= sizeof(buffer
);
4458 if (gnutls_x509_crt_get_key_id(crt
, 0, buffer
, &bytes
) >= 0)
4459 gnutls_x509_crt_set_subject_key_id(crt
, buffer
, bytes
);
4461 gnutls_x509_crt_sign(crt
, crt
, key
);
4467 bytes
= sizeof(buffer
);
4468 if ((result
= gnutls_x509_crt_export(crt
, GNUTLS_X509_FMT_PEM
,
4469 buffer
, &bytes
)) < 0)
4470 cupsdLogMessage(CUPSD_LOG_ERROR
,
4471 "Unable to export SSL server certificate - %s",
4472 gnutls_strerror(result
));
4473 else if ((fp
= cupsFileOpen(ServerCertificate
, "w")) != NULL
)
4475 cupsFileWrite(fp
, (char *)buffer
, bytes
);
4478 cupsdLogMessage(CUPSD_LOG_INFO
,
4479 "Created SSL server certificate file \"%s\"...",
4483 cupsdLogMessage(CUPSD_LOG_ERROR
,
4484 "Unable to create SSL server certificate file \"%s\" - %s",
4485 ServerCertificate
, strerror(errno
));
4491 gnutls_x509_crt_deinit(crt
);
4492 gnutls_x509_privkey_deinit(key
);
4496 #elif defined(HAVE_CDSASSL) && defined(HAVE_WAITPID)
4497 int pid
, /* Process ID of command */
4498 status
; /* Status of command */
4499 char command
[1024], /* Command */
4500 *argv
[4], /* Command-line arguments */
4501 *envp
[MAX_ENV
+ 1], /* Environment variables */
4502 keychain
[1024], /* Keychain argument */
4503 infofile
[1024], /* Type-in information for cert */
4504 localname
[1024], /* Local hostname */
4505 *servername
; /* Name of server in cert */
4506 cups_file_t
*fp
; /* Seed/info file */
4507 int infofd
; /* Info file descriptor */
4510 if (con
->servername
&& isdigit(con
->servername
[0] & 255) && DNSSDHostName
)
4512 snprintf(localname
, sizeof(localname
), "%s.local", DNSSDHostName
);
4513 servername
= localname
;
4516 servername
= con
->servername
;
4519 * Run the "certtool" command to generate a self-signed certificate...
4522 if (!cupsFileFind("certtool", getenv("PATH"), 1, command
, sizeof(command
)))
4524 cupsdLogMessage(CUPSD_LOG_ERROR
,
4525 "No SSL certificate and certtool command not found!");
4530 * Create a file with the certificate information fields...
4532 * Note: This assumes that the default questions are asked by the certtool
4536 if ((fp
= cupsTempFile2(infofile
, sizeof(infofile
))) == NULL
)
4538 cupsdLogMessage(CUPSD_LOG_ERROR
,
4539 "Unable to create certificate information file %s - %s",
4540 infofile
, strerror(errno
));
4544 cupsFilePrintf(fp
, "%s\nr\n\ny\nb\ns\ny\n%s\n\n\n\n\n%s\ny\n",
4545 servername
, servername
, ServerAdmin
);
4548 cupsdLogMessage(CUPSD_LOG_INFO
,
4549 "Generating SSL server key and certificate...");
4551 snprintf(keychain
, sizeof(keychain
), "k=%s", ServerCertificate
);
4553 argv
[0] = "certtool";
4558 cupsdLoadEnv(envp
, MAX_ENV
);
4560 infofd
= open(infofile
, O_RDONLY
);
4562 if (!cupsdStartProcess(command
, argv
, envp
, infofd
, -1, -1, -1, -1, 1, NULL
,
4573 while (waitpid(pid
, &status
, 0) < 0)
4580 cupsdFinishProcess(pid
, command
, sizeof(command
), NULL
);
4584 if (WIFEXITED(status
))
4585 cupsdLogMessage(CUPSD_LOG_ERROR
,
4586 "Unable to create SSL server key and certificate - "
4587 "the certtool command stopped with status %d!",
4588 WEXITSTATUS(status
));
4590 cupsdLogMessage(CUPSD_LOG_ERROR
,
4591 "Unable to create SSL server key and certificate - "
4592 "the certtool command crashed on signal %d!",
4597 cupsdLogMessage(CUPSD_LOG_INFO
,
4598 "Created SSL server certificate file \"%s\"...",
4606 #endif /* HAVE_LIBSSL && HAVE_WAITPID */
4608 #endif /* HAVE_SSL */
4612 * 'pipe_command()' - Pipe the output of a command to the remote client.
4615 static int /* O - Process ID */
4616 pipe_command(cupsd_client_t
*con
, /* I - Client connection */
4617 int infile
, /* I - Standard input for command */
4618 int *outfile
, /* O - Standard output for command */
4619 char *command
, /* I - Command to run */
4620 char *options
, /* I - Options for command */
4621 int root
) /* I - Run as root? */
4623 int i
; /* Looping var */
4624 int pid
; /* Process ID */
4625 char *commptr
, /* Command string pointer */
4626 commch
; /* Command string character */
4627 char *uriptr
; /* URI string pointer */
4628 int fds
[2]; /* Pipe FDs */
4629 int argc
; /* Number of arguments */
4630 int envc
; /* Number of environment variables */
4631 char argbuf
[10240], /* Argument buffer */
4632 *argv
[100], /* Argument strings */
4633 *envp
[MAX_ENV
+ 20]; /* Environment variables */
4634 char auth_type
[256], /* AUTH_TYPE environment variable */
4635 content_length
[1024], /* CONTENT_LENGTH environment variable */
4636 content_type
[1024], /* CONTENT_TYPE environment variable */
4637 http_cookie
[32768], /* HTTP_COOKIE environment variable */
4638 http_referer
[1024], /* HTTP_REFERER environment variable */
4639 http_user_agent
[1024], /* HTTP_USER_AGENT environment variable */
4640 lang
[1024], /* LANG environment variable */
4641 path_info
[1024], /* PATH_INFO environment variable */
4642 remote_addr
[1024], /* REMOTE_ADDR environment variable */
4643 remote_host
[1024], /* REMOTE_HOST environment variable */
4644 remote_user
[1024], /* REMOTE_USER environment variable */
4645 script_filename
[1024], /* SCRIPT_FILENAME environment variable */
4646 script_name
[1024], /* SCRIPT_NAME environment variable */
4647 server_name
[1024], /* SERVER_NAME environment variable */
4648 server_port
[1024]; /* SERVER_PORT environment variable */
4649 ipp_attribute_t
*attr
; /* attributes-natural-language attribute */
4650 void *ccache
= NULL
; /* Kerberos credentials */
4654 * Parse a copy of the options string, which is of the form:
4656 * argument+argument+argument
4657 * ?argument+argument+argument
4658 * param=value¶m=value
4659 * ?param=value¶m=value
4660 * /name?argument+argument+argument
4661 * /name?param=value¶m=value
4663 * If the string contains an "=" character after the initial name,
4664 * then we treat it as a HTTP GET form request and make a copy of
4665 * the remaining string for the environment variable.
4667 * The string is always parsed out as command-line arguments, to
4668 * be consistent with Apache...
4671 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4672 "pipe_command(con=%p(%d), infile=%d, outfile=%p, "
4673 "command=\"%s\", options=\"%s\", root=%d)",
4674 con
, con
->http
.fd
, infile
, outfile
, command
,
4675 options
? options
: "(null)", root
);
4682 if (*commptr
== ' ')
4684 strlcpy(argbuf
, commptr
, sizeof(argbuf
));
4689 if (argbuf
[0] == '/')
4692 * Found some trailing path information, set PATH_INFO...
4695 if ((commptr
= strchr(argbuf
, '?')) == NULL
)
4696 commptr
= argbuf
+ strlen(argbuf
);
4700 snprintf(path_info
, sizeof(path_info
), "PATH_INFO=%s", argbuf
);
4706 path_info
[0] = '\0';
4708 if (*commptr
== ' ')
4712 if (*commptr
== '?' && con
->operation
== HTTP_GET
&& !con
->query_string
)
4715 cupsdSetStringf(&(con
->query_string
), "QUERY_STRING=%s", commptr
);
4722 argv
[argc
++] = commptr
;
4724 for (; *commptr
&& argc
< 99; commptr
++)
4727 * Break arguments whenever we see a + or space...
4730 if (*commptr
== ' ' || *commptr
== '+')
4732 while (*commptr
== ' ' || *commptr
== '+')
4736 * If we don't have a blank string, save it as another argument...
4741 argv
[argc
] = commptr
;
4747 else if (*commptr
== '%' && isxdigit(commptr
[1] & 255) &&
4748 isxdigit(commptr
[2] & 255))
4751 * Convert the %xx notation to the individual character.
4754 if (commptr
[1] >= '0' && commptr
[1] <= '9')
4755 *commptr
= (commptr
[1] - '0') << 4;
4757 *commptr
= (tolower(commptr
[1]) - 'a' + 10) << 4;
4759 if (commptr
[2] >= '0' && commptr
[2] <= '9')
4760 *commptr
|= commptr
[2] - '0';
4762 *commptr
|= tolower(commptr
[2]) - 'a' + 10;
4764 _cups_strcpy(commptr
+ 1, commptr
+ 3);
4767 * Check for a %00 and break if that is the case...
4779 * Setup the environment variables as needed...
4782 if (con
->username
[0])
4784 snprintf(auth_type
, sizeof(auth_type
), "AUTH_TYPE=%s",
4785 httpGetField(HTTP(con
), HTTP_FIELD_AUTHORIZATION
));
4787 if ((uriptr
= strchr(auth_type
+ 10, ' ')) != NULL
)
4791 auth_type
[0] = '\0';
4794 (attr
= ippFindAttribute(con
->request
, "attributes-natural-language",
4795 IPP_TAG_LANGUAGE
)) != NULL
)
4797 switch (strlen(attr
->values
[0].string
.text
))
4801 * This is an unknown or badly formatted language code; use
4802 * the POSIX locale...
4805 strcpy(lang
, "LANG=C");
4810 * Just the language code (ll)...
4813 snprintf(lang
, sizeof(lang
), "LANG=%s.UTF8",
4814 attr
->values
[0].string
.text
);
4819 * Language and country code (ll-cc)...
4822 snprintf(lang
, sizeof(lang
), "LANG=%c%c_%c%c.UTF8",
4823 attr
->values
[0].string
.text
[0],
4824 attr
->values
[0].string
.text
[1],
4825 toupper(attr
->values
[0].string
.text
[3] & 255),
4826 toupper(attr
->values
[0].string
.text
[4] & 255));
4830 else if (con
->language
)
4831 snprintf(lang
, sizeof(lang
), "LANG=%s.UTF8", con
->language
->language
);
4833 strcpy(lang
, "LANG=C");
4835 strcpy(remote_addr
, "REMOTE_ADDR=");
4836 httpAddrString(con
->http
.hostaddr
, remote_addr
+ 12,
4837 sizeof(remote_addr
) - 12);
4839 snprintf(remote_host
, sizeof(remote_host
), "REMOTE_HOST=%s",
4840 con
->http
.hostname
);
4842 snprintf(script_name
, sizeof(script_name
), "SCRIPT_NAME=%s", con
->uri
);
4843 if ((uriptr
= strchr(script_name
, '?')) != NULL
)
4846 snprintf(script_filename
, sizeof(script_filename
), "SCRIPT_FILENAME=%s%s",
4847 DocumentRoot
, script_name
+ 12);
4849 sprintf(server_port
, "SERVER_PORT=%d", con
->serverport
);
4851 if (con
->http
.fields
[HTTP_FIELD_HOST
][0])
4853 char *nameptr
; /* Pointer to ":port" */
4855 snprintf(server_name
, sizeof(server_name
), "SERVER_NAME=%s",
4856 con
->http
.fields
[HTTP_FIELD_HOST
]);
4857 if ((nameptr
= strrchr(server_name
, ':')) != NULL
&& !strchr(nameptr
, ']'))
4858 *nameptr
= '\0'; /* Strip trailing ":port" */
4861 snprintf(server_name
, sizeof(server_name
), "SERVER_NAME=%s",
4864 envc
= cupsdLoadEnv(envp
, (int)(sizeof(envp
) / sizeof(envp
[0])));
4867 envp
[envc
++] = auth_type
;
4869 envp
[envc
++] = lang
;
4870 envp
[envc
++] = "REDIRECT_STATUS=1";
4871 envp
[envc
++] = "GATEWAY_INTERFACE=CGI/1.1";
4872 envp
[envc
++] = server_name
;
4873 envp
[envc
++] = server_port
;
4874 envp
[envc
++] = remote_addr
;
4875 envp
[envc
++] = remote_host
;
4876 envp
[envc
++] = script_name
;
4877 envp
[envc
++] = script_filename
;
4880 envp
[envc
++] = path_info
;
4882 if (con
->username
[0])
4884 snprintf(remote_user
, sizeof(remote_user
), "REMOTE_USER=%s", con
->username
);
4886 envp
[envc
++] = remote_user
;
4889 if (con
->http
.version
== HTTP_1_1
)
4890 envp
[envc
++] = "SERVER_PROTOCOL=HTTP/1.1";
4891 else if (con
->http
.version
== HTTP_1_0
)
4892 envp
[envc
++] = "SERVER_PROTOCOL=HTTP/1.0";
4894 envp
[envc
++] = "SERVER_PROTOCOL=HTTP/0.9";
4896 if (con
->http
.cookie
)
4898 snprintf(http_cookie
, sizeof(http_cookie
), "HTTP_COOKIE=%s",
4900 envp
[envc
++] = http_cookie
;
4903 if (con
->http
.fields
[HTTP_FIELD_USER_AGENT
][0])
4905 snprintf(http_user_agent
, sizeof(http_user_agent
), "HTTP_USER_AGENT=%s",
4906 con
->http
.fields
[HTTP_FIELD_USER_AGENT
]);
4907 envp
[envc
++] = http_user_agent
;
4910 if (con
->http
.fields
[HTTP_FIELD_REFERER
][0])
4912 snprintf(http_referer
, sizeof(http_referer
), "HTTP_REFERER=%s",
4913 con
->http
.fields
[HTTP_FIELD_REFERER
]);
4914 envp
[envc
++] = http_referer
;
4917 if (con
->operation
== HTTP_GET
)
4919 envp
[envc
++] = "REQUEST_METHOD=GET";
4921 if (con
->query_string
)
4924 * Add GET form variables after ?...
4927 envp
[envc
++] = con
->query_string
;
4930 envp
[envc
++] = "QUERY_STRING=";
4934 sprintf(content_length
, "CONTENT_LENGTH=" CUPS_LLFMT
,
4935 CUPS_LLCAST con
->bytes
);
4936 snprintf(content_type
, sizeof(content_type
), "CONTENT_TYPE=%s",
4937 con
->http
.fields
[HTTP_FIELD_CONTENT_TYPE
]);
4939 envp
[envc
++] = "REQUEST_METHOD=POST";
4940 envp
[envc
++] = content_length
;
4941 envp
[envc
++] = content_type
;
4945 * Tell the CGI if we are using encryption...
4949 envp
[envc
++] = "HTTPS=ON";
4952 * Terminate the environment array...
4957 if (LogLevel
>= CUPSD_LOG_DEBUG
)
4959 for (i
= 0; i
< argc
; i
++)
4960 cupsdLogMessage(CUPSD_LOG_DEBUG
,
4961 "[CGI] argv[%d] = \"%s\"", i
, argv
[i
]);
4962 for (i
= 0; i
< envc
; i
++)
4963 cupsdLogMessage(CUPSD_LOG_DEBUG
,
4964 "[CGI] envp[%d] = \"%s\"", i
, envp
[i
]);
4968 * Create a pipe for the output...
4971 if (cupsdOpenPipe(fds
))
4973 cupsdLogMessage(CUPSD_LOG_ERROR
, "[CGI] Unable to create pipe for %s - %s",
4974 argv
[0], strerror(errno
));
4979 * Then execute the command...
4982 if (cupsdStartProcess(command
, argv
, envp
, infile
, fds
[1], CGIPipes
[1],
4983 -1, -1, root
, DefaultProfile
, NULL
, &pid
) < 0)
4986 * Error - can't fork!
4989 cupsdLogMessage(CUPSD_LOG_ERROR
, "[CGI] Unable to start %s - %s", argv
[0],
4992 cupsdClosePipe(fds
);
4998 * Fork successful - return the PID...
5001 if (con
->username
[0])
5002 cupsdAddCert(pid
, con
->username
, ccache
);
5004 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[CGI] Started %s (PID %d)", command
, pid
);
5015 * 'valid_host()' - Is the Host: field valid?
5018 static int /* O - 1 if valid, 0 if not */
5019 valid_host(cupsd_client_t
*con
) /* I - Client connection */
5021 cupsd_alias_t
*a
; /* Current alias */
5022 cupsd_netif_t
*netif
; /* Current network interface */
5023 const char *host
, /* Host field */
5024 *end
; /* End character */
5027 host
= con
->http
.fields
[HTTP_FIELD_HOST
];
5029 if (httpAddrLocalhost(con
->http
.hostaddr
))
5032 * Only allow "localhost" or the equivalent IPv4 or IPv6 numerical
5033 * addresses when accessing CUPS via the loopback interface...
5036 return (!strcasecmp(host
, "localhost") ||
5037 !strncasecmp(host
, "localhost:", 10) ||
5038 !strcasecmp(host
, "localhost.") ||
5039 !strncasecmp(host
, "localhost.:", 11) ||
5041 !strcasecmp(host
, "localhost.localdomain") ||
5042 !strncasecmp(host
, "localhost.localdomain:", 22) ||
5043 #endif /* __linux */
5044 !strcmp(host
, "127.0.0.1") ||
5045 !strncmp(host
, "127.0.0.1:", 10) ||
5046 !strcmp(host
, "[::1]") ||
5047 !strncmp(host
, "[::1]:", 6));
5052 * Check if the hostname is something.local (Bonjour); if so, allow it.
5055 if ((end
= strrchr(host
, '.')) != NULL
&&
5056 (!strcasecmp(end
, ".local") || !strncasecmp(end
, ".local:", 7) ||
5057 !strcasecmp(end
, ".local.") || !strncasecmp(end
, ".local.:", 8)))
5059 #endif /* HAVE_DNSSD */
5062 * Check if the hostname is an IP address...
5065 if (isdigit(*host
& 255) || *host
== '[')
5068 * Possible IPv4/IPv6 address...
5071 char temp
[1024], /* Temporary string */
5072 *ptr
; /* Pointer into temporary string */
5073 http_addrlist_t
*addrlist
; /* List of addresses */
5076 strlcpy(temp
, host
, sizeof(temp
));
5077 if ((ptr
= strrchr(temp
, ':')) != NULL
&& !strchr(ptr
, ']'))
5078 *ptr
= '\0'; /* Strip :port from host value */
5080 if ((addrlist
= httpAddrGetList(temp
, AF_UNSPEC
, NULL
)) != NULL
)
5083 * Good IPv4/IPv6 address...
5086 httpAddrFreeList(addrlist
);
5092 * Check for (alias) name matches...
5095 for (a
= (cupsd_alias_t
*)cupsArrayFirst(ServerAlias
);
5097 a
= (cupsd_alias_t
*)cupsArrayNext(ServerAlias
))
5100 * "ServerAlias *" allows all host values through...
5103 if (!strcmp(a
->name
, "*"))
5106 if (!strncasecmp(host
, a
->name
, a
->namelen
))
5109 * Prefix matches; check the character at the end - it must be ":", ".",
5113 end
= host
+ a
->namelen
;
5115 if (!*end
|| *end
== ':' || (*end
== '.' && (!end
[1] || end
[1] == ':')))
5121 for (a
= (cupsd_alias_t
*)cupsArrayFirst(DNSSDAlias
);
5123 a
= (cupsd_alias_t
*)cupsArrayNext(DNSSDAlias
))
5126 * "ServerAlias *" allows all host values through...
5129 if (!strcmp(a
->name
, "*"))
5132 if (!strncasecmp(host
, a
->name
, a
->namelen
))
5135 * Prefix matches; check the character at the end - it must be ":", ".",
5139 end
= host
+ a
->namelen
;
5141 if (!*end
|| *end
== ':' || (*end
== '.' && (!end
[1] || end
[1] == ':')))
5145 #endif /* HAVE_DNSSD */
5148 * Check for interface hostname matches...
5151 for (netif
= (cupsd_netif_t
*)cupsArrayFirst(NetIFList
);
5153 netif
= (cupsd_netif_t
*)cupsArrayNext(NetIFList
))
5155 if (!strncasecmp(host
, netif
->hostname
, netif
->hostlen
))
5158 * Prefix matches; check the character at the end - it must be ":", ".",
5162 end
= host
+ netif
->hostlen
;
5164 if (!*end
|| *end
== ':' || (*end
== '.' && (!end
[1] || end
[1] == ':')))
5174 * 'write_file()' - Send a file via HTTP.
5177 static int /* O - 0 on failure, 1 on success */
5178 write_file(cupsd_client_t
*con
, /* I - Client connection */
5179 http_status_t code
, /* I - HTTP status */
5180 char *filename
, /* I - Filename */
5181 char *type
, /* I - File type */
5182 struct stat
*filestats
) /* O - File information */
5184 con
->file
= open(filename
, O_RDONLY
);
5186 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
5187 "write_file(con=%p(%d), code=%d, filename=\"%s\" (%d), "
5188 "type=\"%s\", filestats=%p)", con
, con
->http
.fd
,
5189 code
, filename
, con
->file
, type
? type
: "(null)", filestats
);
5194 fcntl(con
->file
, F_SETFD
, fcntl(con
->file
, F_GETFD
) | FD_CLOEXEC
);
5198 if (!cupsdSendHeader(con
, code
, type
, CUPSD_AUTH_NONE
))
5201 if (httpPrintf(HTTP(con
), "Last-Modified: %s\r\n",
5202 httpGetDateString(filestats
->st_mtime
)) < 0)
5204 if (httpPrintf(HTTP(con
), "Content-Length: " CUPS_LLFMT
"\r\n",
5205 CUPS_LLCAST filestats
->st_size
) < 0)
5207 if (httpPrintf(HTTP(con
), "\r\n") < 0)
5210 if (cupsdFlushHeader(con
) < 0)
5213 con
->http
.data_encoding
= HTTP_ENCODE_LENGTH
;
5214 con
->http
.data_remaining
= filestats
->st_size
;
5216 if (con
->http
.data_remaining
<= INT_MAX
)
5217 con
->http
._data_remaining
= con
->http
.data_remaining
;
5219 con
->http
._data_remaining
= INT_MAX
;
5221 cupsdAddSelect(con
->http
.fd
, (cupsd_selfunc_t
)cupsdReadClient
,
5222 (cupsd_selfunc_t
)cupsdWriteClient
, con
);
5229 * 'write_pipe()' - Flag that data is available on the CGI pipe.
5233 write_pipe(cupsd_client_t
*con
) /* I - Client connection */
5235 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
5236 "write_pipe(con=%p(%d)) CGI output on fd %d",
5237 con
, con
->http
.fd
, con
->file
);
5239 con
->file_ready
= 1;
5241 cupsdRemoveSelect(con
->file
);
5242 cupsdAddSelect(con
->http
.fd
, NULL
, (cupsd_selfunc_t
)cupsdWriteClient
, con
);
5247 * End of "$Id: client.c 7950 2008-09-17 00:21:59Z mike $".