2 * "$Id: client.c 7673 2008-06-18 22:31:26Z mike $"
4 * Client routines for the Common UNIX Printing System (CUPS) scheduler.
6 * Copyright 2007-2008 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 programs.
29 * cupsdWriteClient() - Write data to a client as needed.
30 * check_if_modified() - Decode an "If-Modified-Since" line.
31 * compare_clients() - Compare two client connections.
32 * encrypt_client() - Enable encryption for the client...
33 * get_cdsa_certificate() - Convert a keychain name into the CFArrayRef
34 * required by SSLSetCertificate.
35 * get_file() - Get a filename and state info.
36 * install_conf_file() - Install a configuration file.
37 * is_cgi() - Is the resource a CGI script/program?
38 * is_path_absolute() - Is a path absolute and free of relative elements.
39 * make_certificate() - Make a self-signed SSL/TLS certificate.
40 * pipe_command() - Pipe the output of a command to the remote client.
41 * write_file() - Send a file via HTTP.
42 * write_pipe() - Flag that data is available on the CGI pipe.
46 * Include necessary headers...
52 # include <Security/Security.h>
53 # ifdef HAVE_SECIDENTITYSEARCHPRIV_H
54 # include <Security/SecIdentitySearchPriv.h>
55 # else /* Declare prototype for function in that header... */
56 extern OSStatus
SecIdentitySearchCreateWithPolicy(SecPolicyRef policy
,
57 CFStringRef idString
, CSSM_KEYUSE keyUsage
,
58 CFTypeRef keychainOrArray
,
59 Boolean returnOnlyValidIdentities
,
60 SecIdentitySearchRef
* searchRef
);
61 # endif /* HAVE_SECIDENTITYSEARCHPRIV_H */
62 # ifdef HAVE_SECPOLICYPRIV_H
63 # include <Security/SecPolicyPriv.h>
64 # else /* Declare prototype for function in that header... */
65 extern OSStatus
SecPolicySetValue(SecPolicyRef policyRef
,
66 const CSSM_DATA
*value
);
67 # endif /* HAVE_SECPOLICYPRIV_H */
68 # ifdef HAVE_SECBASEPRIV_H
69 # include <Security/SecBasePriv.h>
70 # else /* Declare prototype for function in that header... */
71 extern const char *cssmErrorString(int error
);
72 # endif /* HAVE_SECBASEPRIV_H */
73 #endif /* HAVE_CDSASSL */
76 # include <gnutls/x509.h>
77 #endif /* HAVE_GNUTLS */
81 #endif /* HAVE_TCPD_H */
88 static int check_if_modified(cupsd_client_t
*con
,
89 struct stat
*filestats
);
90 static int compare_clients(cupsd_client_t
*a
, cupsd_client_t
*b
,
93 static int encrypt_client(cupsd_client_t
*con
);
96 static CFArrayRef
get_cdsa_certificate(cupsd_client_t
*con
);
97 #endif /* HAVE_CDSASSL */
98 static char *get_file(cupsd_client_t
*con
, struct stat
*filestats
,
99 char *filename
, int len
);
100 static http_status_t
install_conf_file(cupsd_client_t
*con
);
101 static int is_cgi(cupsd_client_t
*con
, const char *filename
,
102 struct stat
*filestats
, mime_type_t
*type
);
103 static int is_path_absolute(const char *path
);
105 static int make_certificate(cupsd_client_t
*con
);
106 #endif /* HAVE_SSL */
107 static int pipe_command(cupsd_client_t
*con
, int infile
, int *outfile
,
108 char *command
, char *options
, int root
);
109 static int write_file(cupsd_client_t
*con
, http_status_t code
,
110 char *filename
, char *type
,
111 struct stat
*filestats
);
112 static void write_pipe(cupsd_client_t
*con
);
116 * 'cupsdAcceptClient()' - Accept a new client.
120 cupsdAcceptClient(cupsd_listener_t
*lis
)/* I - Listener socket */
122 int count
; /* Count of connections on a host */
123 int val
; /* Parameter value */
124 cupsd_client_t
*con
, /* New client pointer */
125 *tempcon
; /* Temporary client pointer */
126 http_addrlist_t
*addrlist
, /* List of adddresses for host */
127 *addr
; /* Current address */
128 socklen_t addrlen
; /* Length of address */
129 char *hostname
; /* Hostname for address */
130 http_addr_t temp
; /* Temporary address variable */
131 static time_t last_dos
= 0; /* Time of last DoS attack */
133 struct request_info wrap_req
; /* TCP wrappers request information */
134 #endif /* HAVE_TCPD_H */
137 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
138 "cupsdAcceptClient(lis=%p) %d Clients = %d",
139 lis
, lis
->fd
, cupsArrayCount(Clients
));
142 * Make sure we don't have a full set of clients already...
145 if (cupsArrayCount(Clients
) == MaxClients
)
149 * Get a pointer to the next available client...
153 Clients
= cupsArrayNew(NULL
, NULL
);
157 cupsdLogMessage(CUPSD_LOG_ERROR
,
158 "Unable to allocate memory for clients array!");
159 cupsdPauseListening();
164 ActiveClients
= cupsArrayNew((cups_array_func_t
)compare_clients
, NULL
);
168 cupsdLogMessage(CUPSD_LOG_ERROR
,
169 "Unable to allocate memory for active clients array!");
170 cupsdPauseListening();
174 if ((con
= calloc(1, sizeof(cupsd_client_t
))) == NULL
)
176 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to allocate memory for client!");
177 cupsdPauseListening();
181 con
->http
.activity
= time(NULL
);
183 con
->http
.hostaddr
= &(con
->clientaddr
);
186 * Accept the client and get the remote address...
189 addrlen
= sizeof(http_addr_t
);
191 if ((con
->http
.fd
= accept(lis
->fd
, (struct sockaddr
*)con
->http
.hostaddr
,
194 if (errno
== ENFILE
|| errno
== EMFILE
)
195 cupsdPauseListening();
197 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to accept client connection - %s.",
205 if (lis
->address
.addr
.sa_family
== AF_INET6
)
208 * Save the connected port number...
211 con
->http
.hostaddr
->ipv6
.sin6_port
= lis
->address
.ipv6
.sin6_port
;
214 * Convert IPv4 over IPv6 addresses (::ffff:n.n.n.n) to IPv4 forms we
215 * can more easily use...
218 if (con
->http
.hostaddr
->ipv6
.sin6_addr
.s6_addr32
[0] == 0 &&
219 con
->http
.hostaddr
->ipv6
.sin6_addr
.s6_addr32
[1] == 0 &&
220 ntohl(con
->http
.hostaddr
->ipv6
.sin6_addr
.s6_addr32
[2]) == 0xffff)
221 con
->http
.hostaddr
->ipv6
.sin6_addr
.s6_addr32
[2] = 0;
224 #endif /* AF_INET6 */
225 if (lis
->address
.addr
.sa_family
== AF_INET
)
226 con
->http
.hostaddr
->ipv4
.sin_port
= lis
->address
.ipv4
.sin_port
;
229 * Check the number of clients on the same address...
232 for (count
= 0, tempcon
= (cupsd_client_t
*)cupsArrayFirst(Clients
);
234 tempcon
= (cupsd_client_t
*)cupsArrayNext(Clients
))
235 if (httpAddrEqual(tempcon
->http
.hostaddr
, con
->http
.hostaddr
))
238 if (count
>= MaxClientsPerHost
)
242 if (count
>= MaxClientsPerHost
)
244 if ((time(NULL
) - last_dos
) >= 60)
246 last_dos
= time(NULL
);
247 cupsdLogMessage(CUPSD_LOG_WARN
,
248 "Possible DoS attack - more than %d clients connecting "
251 httpAddrString(con
->http
.hostaddr
, con
->http
.hostname
,
252 sizeof(con
->http
.hostname
)));
256 closesocket(con
->http
.fd
);
266 * Get the hostname or format the IP address as needed...
269 if (httpAddrLocalhost(con
->http
.hostaddr
))
272 * Map accesses from the loopback interface to "localhost"...
275 strlcpy(con
->http
.hostname
, "localhost", sizeof(con
->http
.hostname
));
276 hostname
= con
->http
.hostname
;
281 * Map accesses from the same host to the server name.
284 for (addr
= ServerAddrs
; addr
; addr
= addr
->next
)
285 if (httpAddrEqual(con
->http
.hostaddr
, &(addr
->addr
)))
290 strlcpy(con
->http
.hostname
, ServerName
, sizeof(con
->http
.hostname
));
291 hostname
= con
->http
.hostname
;
293 else if (HostNameLookups
)
294 hostname
= httpAddrLookup(con
->http
.hostaddr
, con
->http
.hostname
,
295 sizeof(con
->http
.hostname
));
299 httpAddrString(con
->http
.hostaddr
, con
->http
.hostname
,
300 sizeof(con
->http
.hostname
));
304 if (hostname
== NULL
&& HostNameLookups
== 2)
307 * Can't have an unresolved IP address with double-lookups enabled...
310 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
311 "cupsdAcceptClient: Closing connection %d...",
315 closesocket(con
->http
.fd
);
320 cupsdLogMessage(CUPSD_LOG_WARN
,
321 "Name lookup failed - connection from %s closed!",
328 if (HostNameLookups
== 2)
331 * Do double lookups as needed...
334 if ((addrlist
= httpAddrGetList(con
->http
.hostname
, AF_UNSPEC
, NULL
))
338 * See if the hostname maps to the same IP address...
341 for (addr
= addrlist
; addr
; addr
= addr
->next
)
342 if (httpAddrEqual(con
->http
.hostaddr
, &(addr
->addr
)))
348 httpAddrFreeList(addrlist
);
353 * Can't have a hostname that doesn't resolve to the same IP address
354 * with double-lookups enabled...
357 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
358 "cupsdAcceptClient: Closing connection %d...",
362 closesocket(con
->http
.fd
);
367 cupsdLogMessage(CUPSD_LOG_WARN
,
368 "IP lookup failed - connection from %s closed!",
377 * See if the connection is denied by TCP wrappers...
380 request_init(&wrap_req
, RQ_DAEMON
, "cupsd", RQ_FILE
, con
->http
.fd
, NULL
);
383 if (!hosts_access(&wrap_req
))
385 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
386 "cupsdAcceptClient: Closing connection %d...",
390 closesocket(con
->http
.fd
);
395 cupsdLogMessage(CUPSD_LOG_WARN
,
396 "Connection from %s refused by /etc/hosts.allow and "
397 "/etc/hosts.deny rules.", con
->http
.hostname
);
401 #endif /* HAVE_TCPD_H */
404 if (con
->http
.hostaddr
->addr
.sa_family
== AF_INET6
)
405 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdAcceptClient: %d from %s:%d (IPv6)",
406 con
->http
.fd
, con
->http
.hostname
,
407 ntohs(con
->http
.hostaddr
->ipv6
.sin6_port
));
409 #endif /* AF_INET6 */
411 if (con
->http
.hostaddr
->addr
.sa_family
== AF_LOCAL
)
412 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdAcceptClient: %d from %s (Domain)",
413 con
->http
.fd
, con
->http
.hostname
);
415 #endif /* AF_LOCAL */
416 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdAcceptClient: %d from %s:%d (IPv4)",
417 con
->http
.fd
, con
->http
.hostname
,
418 ntohs(con
->http
.hostaddr
->ipv4
.sin_port
));
421 * Get the local address the client connected to...
424 addrlen
= sizeof(temp
);
425 if (getsockname(con
->http
.fd
, (struct sockaddr
*)&temp
, &addrlen
))
427 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to get local address - %s",
430 strcpy(con
->servername
, "localhost");
431 con
->serverport
= LocalPort
;
436 if (temp
.addr
.sa_family
== AF_INET6
)
438 httpAddrLookup(&temp
, con
->servername
, sizeof(con
->servername
));
439 con
->serverport
= ntohs(lis
->address
.ipv6
.sin6_port
);
442 #endif /* AF_INET6 */
443 if (temp
.addr
.sa_family
== AF_INET
)
445 httpAddrLookup(&temp
, con
->servername
, sizeof(con
->servername
));
446 con
->serverport
= ntohs(lis
->address
.ipv4
.sin_port
);
450 strcpy(con
->servername
, "localhost");
451 con
->serverport
= LocalPort
;
455 cupsArrayAdd(Clients
, con
);
457 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
458 "cupsdAcceptClient: %d connected to server on %s:%d",
459 con
->http
.fd
, con
->servername
, con
->serverport
);
462 * Using TCP_NODELAY improves responsiveness, especially on systems
463 * with a slow loopback interface... Since we write large buffers
464 * when sending print files and requests, there shouldn't be any
465 * performance penalty for this...
469 setsockopt(con
->http
.fd
, IPPROTO_TCP
, TCP_NODELAY
, &val
, sizeof(val
));
472 * Close this file on all execs...
475 fcntl(con
->http
.fd
, F_SETFD
, fcntl(con
->http
.fd
, F_GETFD
) | FD_CLOEXEC
);
478 * Add the socket to the select() input mask.
481 cupsdAddSelect(con
->http
.fd
, (cupsd_selfunc_t
)cupsdReadClient
, NULL
, con
);
484 * Temporarily suspend accept()'s until we lose a client...
487 if (cupsArrayCount(Clients
) == MaxClients
)
488 cupsdPauseListening();
492 * See if we are connecting on a secure port...
495 if (lis
->encryption
== HTTP_ENCRYPT_ALWAYS
)
498 * https connection; go secure...
501 con
->http
.encryption
= HTTP_ENCRYPT_ALWAYS
;
503 if (!encrypt_client(con
))
504 cupsdCloseClient(con
);
508 #endif /* HAVE_SSL */
513 * 'cupsdCloseAllClients()' - Close all remote clients immediately.
517 cupsdCloseAllClients(void)
519 cupsd_client_t
*con
; /* Current client */
522 for (con
= (cupsd_client_t
*)cupsArrayFirst(Clients
);
524 con
= (cupsd_client_t
*)cupsArrayNext(Clients
))
525 cupsdCloseClient(con
);
530 * 'cupsdCloseClient()' - Close a remote client.
533 int /* O - 1 if partial close, 0 if fully closed */
534 cupsdCloseClient(cupsd_client_t
*con
) /* I - Client to close */
536 int partial
; /* Do partial close for SSL? */
538 SSL_CTX
*context
; /* Context for encryption */
539 SSL
*conn
; /* Connection for encryption */
540 unsigned long error
; /* Error code */
541 #elif defined(HAVE_GNUTLS)
542 http_tls_t
*conn
; /* TLS connection information */
543 int error
; /* Error code */
544 gnutls_certificate_server_credentials
*credentials
;
545 /* TLS credentials */
546 # elif defined(HAVE_CDSASSL)
547 http_tls_t
*conn
; /* CDSA connection information */
548 #endif /* HAVE_LIBSSL */
551 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdCloseClient: %d", con
->http
.fd
);
554 * Flush pending writes before closing...
557 httpFlushWrite(HTTP(con
));
563 * Shutdown encryption as needed...
571 conn
= (SSL
*)(con
->http
.tls
);
572 context
= SSL_get_SSL_CTX(conn
);
574 switch (SSL_shutdown(conn
))
577 cupsdLogMessage(CUPSD_LOG_DEBUG
,
578 "SSL shutdown successful!");
581 cupsdLogMessage(CUPSD_LOG_ERROR
,
582 "Fatal error during SSL shutdown!");
584 while ((error
= ERR_get_error()) != 0)
585 cupsdLogMessage(CUPSD_LOG_ERROR
, "SSL shutdown failed: %s",
586 ERR_error_string(error
, NULL
));
590 SSL_CTX_free(context
);
593 # elif defined(HAVE_GNUTLS)
594 conn
= (http_tls_t
*)(con
->http
.tls
);
595 credentials
= (gnutls_certificate_server_credentials
*)(conn
->credentials
);
597 error
= gnutls_bye(conn
->session
, GNUTLS_SHUT_WR
);
600 case GNUTLS_E_SUCCESS
:
601 cupsdLogMessage(CUPSD_LOG_DEBUG
,
602 "SSL shutdown successful!");
605 cupsdLogMessage(CUPSD_LOG_ERROR
,
606 "SSL shutdown failed: %s", gnutls_strerror(error
));
610 gnutls_deinit(conn
->session
);
611 gnutls_certificate_free_credentials(*credentials
);
615 # elif defined(HAVE_CDSASSL)
616 conn
= (http_tls_t
*)(con
->http
.tls
);
618 while (SSLClose(conn
->session
) == errSSLWouldBlock
)
621 SSLDisposeContext(conn
->session
);
623 if (conn
->certsArray
)
624 CFRelease(conn
->certsArray
);
627 # endif /* HAVE_LIBSSL */
629 con
->http
.tls
= NULL
;
631 #endif /* HAVE_SSL */
633 if (con
->pipe_pid
!= 0)
636 * Stop any CGI process...
639 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
640 "cupsdCloseClient: %d Killing process ID %d...",
641 con
->http
.fd
, con
->pipe_pid
);
642 cupsdEndProcess(con
->pipe_pid
, 1);
648 cupsdRemoveSelect(con
->file
);
650 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
651 "cupsdCloseClient: %d Closing data file %d.",
652 con
->http
.fd
, con
->file
);
659 * Close the socket and clear the file from the input set for select()...
662 if (con
->http
.fd
>= 0)
664 cupsArrayRemove(ActiveClients
, con
);
670 * Only do a partial close so that the encrypted client gets everything.
673 shutdown(con
->http
.fd
, 0);
674 cupsdAddSelect(con
->http
.fd
, (cupsd_selfunc_t
)cupsdReadClient
, NULL
, con
);
679 * Shut the socket down fully...
682 cupsdRemoveSelect(con
->http
.fd
);
694 if (con
->http
.input_set
)
695 free(con
->http
.input_set
);
697 httpClearCookie(HTTP(con
));
699 cupsdClearString(&con
->filename
);
700 cupsdClearString(&con
->command
);
701 cupsdClearString(&con
->options
);
702 cupsdClearString(&con
->query_string
);
706 ippDelete(con
->request
);
712 ippDelete(con
->response
);
713 con
->response
= NULL
;
718 cupsLangFree(con
->language
);
719 con
->language
= NULL
;
722 #ifdef HAVE_AUTHORIZATION_H
725 AuthorizationFree(con
->authref
, kAuthorizationFlagDefaults
);
728 #endif /* HAVE_AUTHORIZATION_H */
731 * Re-enable new client connections if we are going back under the
735 if (cupsArrayCount(Clients
) == MaxClients
)
736 cupsdResumeListening();
739 * Compact the list of clients as necessary...
742 cupsArrayRemove(Clients
, con
);
752 * 'cupsdFlushHeader()' - Flush the header fields to the client.
755 int /* I - Bytes written or -1 on error */
756 cupsdFlushHeader(cupsd_client_t
*con
) /* I - Client to flush to */
758 int bytes
= httpFlushWrite(HTTP(con
));
760 con
->http
.data_encoding
= HTTP_ENCODE_LENGTH
;
767 * 'cupsdReadClient()' - Read data from a client.
771 cupsdReadClient(cupsd_client_t
*con
) /* I - Client to read from */
773 char line
[32768], /* Line from client... */
774 operation
[64], /* Operation code from socket */
775 version
[64], /* HTTP version number string */
776 locale
[64], /* Locale */
777 *ptr
; /* Pointer into strings */
778 int major
, minor
; /* HTTP version numbers */
779 http_status_t status
; /* Transfer status */
780 ipp_state_t ipp_state
; /* State of IPP transfer */
781 int bytes
; /* Number of bytes to POST */
782 char *filename
; /* Name of file for GET/HEAD */
783 char buf
[1024]; /* Buffer for real filename */
784 struct stat filestats
; /* File information */
785 mime_type_t
*type
; /* MIME type of file */
786 cupsd_printer_t
*p
; /* Printer */
787 static unsigned request_id
= 0; /* Request ID for temp files */
790 status
= HTTP_CONTINUE
;
792 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
793 "cupsdReadClient: %d, used=%d, file=%d state=%d",
794 con
->http
.fd
, con
->http
.used
, con
->file
, con
->http
.state
);
798 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdReadClient: http error seen...");
799 cupsdCloseClient(con
);
807 * Automatically check for a SSL/TLS handshake...
812 if (recv(con
->http
.fd
, buf
, 1, MSG_PEEK
) == 1 &&
813 (!buf
[0] || !strchr("DGHOPT", buf
[0])))
816 * Encrypt this connection...
819 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
820 "cupsdReadClient: Saw first byte %02X, auto-negotiating SSL/TLS session...",
823 if (!encrypt_client(con
))
824 cupsdCloseClient(con
);
829 #endif /* HAVE_SSL */
831 switch (con
->http
.state
)
835 * See if we've received a request line...
838 if (httpGets(line
, sizeof(line
) - 1, HTTP(con
)) == NULL
)
840 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
841 "cupsdReadClient: httpGets returned EOF...");
842 cupsdCloseClient(con
);
847 * Ignore blank request lines...
854 * Clear other state variables...
857 httpClearFields(HTTP(con
));
859 con
->http
.activity
= time(NULL
);
860 con
->http
.version
= HTTP_1_0
;
861 con
->http
.keep_alive
= HTTP_KEEPALIVE_OFF
;
862 con
->http
.data_encoding
= HTTP_ENCODE_LENGTH
;
863 con
->http
.data_remaining
= 0;
864 con
->http
._data_remaining
= 0;
865 con
->operation
= HTTP_WAITING
;
870 con
->username
[0] = '\0';
871 con
->password
[0] = '\0';
874 cupsdClearString(&con
->command
);
875 cupsdClearString(&con
->options
);
876 cupsdClearString(&con
->query_string
);
880 ippDelete(con
->request
);
886 ippDelete(con
->response
);
887 con
->response
= NULL
;
892 cupsLangFree(con
->language
);
893 con
->language
= NULL
;
897 con
->gss_have_creds
= 0;
898 #endif /* HAVE_GSSAPI */
901 * Grab the request line...
904 switch (sscanf(line
, "%63s%1023s%63s", operation
, con
->uri
, version
))
909 cupsdLogMessage(CUPSD_LOG_ERROR
,
910 "Bad request line \"%s\" from %s!", line
,
912 cupsdSendError(con
, HTTP_BAD_REQUEST
, CUPSD_AUTH_NONE
);
913 cupsdCloseClient(con
);
917 con
->http
.version
= HTTP_0_9
;
920 if (sscanf(version
, "HTTP/%d.%d", &major
, &minor
) != 2)
922 cupsdLogMessage(CUPSD_LOG_ERROR
,
923 "Bad request line \"%s\" from %s!", line
,
925 cupsdSendError(con
, HTTP_BAD_REQUEST
, CUPSD_AUTH_NONE
);
926 cupsdCloseClient(con
);
932 con
->http
.version
= (http_version_t
)(major
* 100 + minor
);
933 if (con
->http
.version
== HTTP_1_1
&& KeepAlive
)
934 con
->http
.keep_alive
= HTTP_KEEPALIVE_ON
;
936 con
->http
.keep_alive
= HTTP_KEEPALIVE_OFF
;
940 cupsdSendError(con
, HTTP_NOT_SUPPORTED
, CUPSD_AUTH_NONE
);
941 cupsdCloseClient(con
);
948 * Handle full URLs in the request line...
951 if (strcmp(con
->uri
, "*"))
953 char method
[HTTP_MAX_URI
], /* Method/scheme */
954 userpass
[HTTP_MAX_URI
], /* Username:password */
955 hostname
[HTTP_MAX_URI
], /* Hostname */
956 resource
[HTTP_MAX_URI
]; /* Resource path */
957 int port
; /* Port number */
961 * Separate the URI into its components...
964 httpSeparateURI(HTTP_URI_CODING_MOST
, con
->uri
,
965 method
, sizeof(method
),
966 userpass
, sizeof(userpass
),
967 hostname
, sizeof(hostname
), &port
,
968 resource
, sizeof(resource
));
971 * Only allow URIs with the servername, localhost, or an IP
975 if (strcmp(method
, "file") &&
976 strcasecmp(hostname
, ServerName
) &&
977 strcasecmp(hostname
, "localhost") &&
978 !isdigit(hostname
[0]))
981 * Nope, we don't do proxies...
984 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad URI \"%s\" in request!",
986 cupsdSendError(con
, HTTP_METHOD_NOT_ALLOWED
, CUPSD_AUTH_NONE
);
987 cupsdCloseClient(con
);
992 * Copy the resource portion back into the URI; both resource and
993 * con->uri are HTTP_MAX_URI bytes in size...
996 strcpy(con
->uri
, resource
);
1000 * Process the request...
1003 if (!strcmp(operation
, "GET"))
1004 con
->http
.state
= HTTP_GET
;
1005 else if (!strcmp(operation
, "PUT"))
1006 con
->http
.state
= HTTP_PUT
;
1007 else if (!strcmp(operation
, "POST"))
1008 con
->http
.state
= HTTP_POST
;
1009 else if (!strcmp(operation
, "DELETE"))
1010 con
->http
.state
= HTTP_DELETE
;
1011 else if (!strcmp(operation
, "TRACE"))
1012 con
->http
.state
= HTTP_TRACE
;
1013 else if (!strcmp(operation
, "OPTIONS"))
1014 con
->http
.state
= HTTP_OPTIONS
;
1015 else if (!strcmp(operation
, "HEAD"))
1016 con
->http
.state
= HTTP_HEAD
;
1019 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad operation \"%s\"!", operation
);
1020 cupsdSendError(con
, HTTP_BAD_REQUEST
, CUPSD_AUTH_NONE
);
1021 cupsdCloseClient(con
);
1025 con
->start
= time(NULL
);
1026 con
->operation
= con
->http
.state
;
1028 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdReadClient: %d %s %s HTTP/%d.%d",
1029 con
->http
.fd
, operation
, con
->uri
,
1030 con
->http
.version
/ 100, con
->http
.version
% 100);
1032 con
->http
.status
= HTTP_OK
;
1034 cupsArrayAdd(ActiveClients
, con
);
1035 cupsdSetBusyState();
1045 * Parse incoming parameters until the status changes...
1048 while ((status
= httpUpdate(HTTP(con
))) == HTTP_CONTINUE
)
1049 if (con
->http
.used
== 0 ||
1050 !memchr(con
->http
.buffer
, '\n', con
->http
.used
))
1053 if (status
!= HTTP_OK
&& status
!= HTTP_CONTINUE
)
1055 cupsdSendError(con
, HTTP_BAD_REQUEST
, CUPSD_AUTH_NONE
);
1056 cupsdCloseClient(con
);
1062 break; /* Anti-compiler-warning-code */
1066 * Handle new transfers...
1069 if (status
== HTTP_OK
)
1071 if (con
->http
.fields
[HTTP_FIELD_ACCEPT_LANGUAGE
][0])
1074 * Figure out the locale from the Accept-Language and Content-Type
1078 if ((ptr
= strchr(con
->http
.fields
[HTTP_FIELD_ACCEPT_LANGUAGE
], ',')) != NULL
)
1081 if ((ptr
= strchr(con
->http
.fields
[HTTP_FIELD_ACCEPT_LANGUAGE
], ';')) != NULL
)
1084 if ((ptr
= strstr(con
->http
.fields
[HTTP_FIELD_CONTENT_TYPE
], "charset=")) != NULL
)
1087 * Combine language and charset, and trim any extra params in the
1091 snprintf(locale
, sizeof(locale
), "%s.%s",
1092 con
->http
.fields
[HTTP_FIELD_ACCEPT_LANGUAGE
], ptr
+ 8);
1094 if ((ptr
= strchr(locale
, ',')) != NULL
)
1098 snprintf(locale
, sizeof(locale
), "%s.%s",
1099 con
->http
.fields
[HTTP_FIELD_ACCEPT_LANGUAGE
], DefaultCharset
);
1101 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1102 "cupsdReadClient: %d Browser asked for language \"%s\"...",
1103 con
->http
.fd
, locale
);
1105 con
->language
= cupsLangGet(locale
);
1108 con
->language
= cupsLangGet(DefaultLocale
);
1110 cupsdAuthorize(con
);
1112 if (!strncmp(con
->http
.fields
[HTTP_FIELD_CONNECTION
], "Keep-Alive", 10) &&
1114 con
->http
.keep_alive
= HTTP_KEEPALIVE_ON
;
1116 if (!con
->http
.fields
[HTTP_FIELD_HOST
][0] &&
1117 con
->http
.version
>= HTTP_1_1
)
1120 * HTTP/1.1 and higher require the "Host:" field...
1123 if (!cupsdSendError(con
, HTTP_BAD_REQUEST
, CUPSD_AUTH_NONE
))
1125 cupsdCloseClient(con
);
1129 else if (con
->operation
== HTTP_OPTIONS
)
1132 * Do OPTIONS command...
1135 if (con
->best
&& con
->best
->type
!= CUPSD_AUTH_NONE
)
1137 if (!cupsdSendHeader(con
, HTTP_UNAUTHORIZED
, NULL
, CUPSD_AUTH_NONE
))
1139 cupsdCloseClient(con
);
1144 if (!strcasecmp(con
->http
.fields
[HTTP_FIELD_CONNECTION
], "Upgrade") &&
1145 con
->http
.tls
== NULL
)
1149 * Do encryption stuff...
1152 if (!cupsdSendHeader(con
, HTTP_SWITCHING_PROTOCOLS
, NULL
, CUPSD_AUTH_NONE
))
1154 cupsdCloseClient(con
);
1158 httpPrintf(HTTP(con
), "Connection: Upgrade\r\n");
1159 httpPrintf(HTTP(con
), "Upgrade: TLS/1.0,HTTP/1.1\r\n");
1160 httpPrintf(HTTP(con
), "Content-Length: 0\r\n");
1161 httpPrintf(HTTP(con
), "\r\n");
1163 if (cupsdFlushHeader(con
) < 0)
1165 cupsdCloseClient(con
);
1169 if (!encrypt_client(con
))
1171 cupsdCloseClient(con
);
1175 if (!cupsdSendError(con
, HTTP_NOT_IMPLEMENTED
, CUPSD_AUTH_NONE
))
1177 cupsdCloseClient(con
);
1180 #endif /* HAVE_SSL */
1183 if (!cupsdSendHeader(con
, HTTP_OK
, NULL
, CUPSD_AUTH_NONE
))
1185 cupsdCloseClient(con
);
1189 httpPrintf(HTTP(con
), "Allow: GET, HEAD, OPTIONS, POST, PUT\r\n");
1190 httpPrintf(HTTP(con
), "Content-Length: 0\r\n");
1191 httpPrintf(HTTP(con
), "\r\n");
1193 if (cupsdFlushHeader(con
) < 0)
1195 cupsdCloseClient(con
);
1199 else if (!is_path_absolute(con
->uri
))
1202 * Protect against malicious users!
1205 if (!cupsdSendError(con
, HTTP_FORBIDDEN
, CUPSD_AUTH_NONE
))
1207 cupsdCloseClient(con
);
1213 if (!strcasecmp(con
->http
.fields
[HTTP_FIELD_CONNECTION
], "Upgrade") &&
1214 con
->http
.tls
== NULL
)
1218 * Do encryption stuff...
1221 if (!cupsdSendHeader(con
, HTTP_SWITCHING_PROTOCOLS
, NULL
, CUPSD_AUTH_NONE
))
1223 cupsdCloseClient(con
);
1227 httpPrintf(HTTP(con
), "Connection: Upgrade\r\n");
1228 httpPrintf(HTTP(con
), "Upgrade: TLS/1.0,HTTP/1.1\r\n");
1229 httpPrintf(HTTP(con
), "Content-Length: 0\r\n");
1230 httpPrintf(HTTP(con
), "\r\n");
1232 if (cupsdFlushHeader(con
) < 0)
1234 cupsdCloseClient(con
);
1238 if (!encrypt_client(con
))
1240 cupsdCloseClient(con
);
1244 if (!cupsdSendError(con
, HTTP_NOT_IMPLEMENTED
, CUPSD_AUTH_NONE
))
1246 cupsdCloseClient(con
);
1249 #endif /* HAVE_SSL */
1252 if ((status
= cupsdIsAuthorized(con
, NULL
)) != HTTP_OK
)
1254 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1255 "cupsdReadClient: Unauthorized request for %s...",
1257 cupsdSendError(con
, status
, CUPSD_AUTH_NONE
);
1258 cupsdCloseClient(con
);
1262 if (con
->http
.expect
&&
1263 (con
->operation
== HTTP_POST
|| con
->operation
== HTTP_PUT
))
1265 if (con
->http
.expect
== HTTP_CONTINUE
)
1268 * Send 100-continue header...
1271 if (!cupsdSendHeader(con
, HTTP_CONTINUE
, NULL
, CUPSD_AUTH_NONE
))
1273 cupsdCloseClient(con
);
1280 * Send 417-expectation-failed header...
1283 if (!cupsdSendHeader(con
, HTTP_EXPECTATION_FAILED
, NULL
, CUPSD_AUTH_NONE
))
1285 cupsdCloseClient(con
);
1289 httpPrintf(HTTP(con
), "Content-Length: 0\r\n");
1290 httpPrintf(HTTP(con
), "\r\n");
1292 if (cupsdFlushHeader(con
) < 0)
1294 cupsdCloseClient(con
);
1300 switch (con
->http
.state
)
1302 case HTTP_GET_SEND
:
1303 if (!strncmp(con
->uri
, "/printers/", 10) &&
1304 !strcmp(con
->uri
+ strlen(con
->uri
) - 4, ".ppd"))
1307 * Send PPD file - get the real printer name since printer
1308 * names are not case sensitive but filenames can be...
1311 con
->uri
[strlen(con
->uri
) - 4] = '\0'; /* Drop ".ppd" */
1313 if ((p
= cupsdFindPrinter(con
->uri
+ 10)) != NULL
)
1314 snprintf(con
->uri
, sizeof(con
->uri
), "/ppd/%s.ppd", p
->name
);
1317 if (!cupsdSendError(con
, HTTP_NOT_FOUND
, CUPSD_AUTH_NONE
))
1319 cupsdCloseClient(con
);
1327 if ((!strncmp(con
->uri
, "/admin", 6) &&
1328 strncmp(con
->uri
, "/admin/conf/", 12) &&
1329 strncmp(con
->uri
, "/admin/log/", 11)) ||
1330 !strncmp(con
->uri
, "/printers", 9) ||
1331 !strncmp(con
->uri
, "/classes", 8) ||
1332 !strncmp(con
->uri
, "/help", 5) ||
1333 !strncmp(con
->uri
, "/jobs", 5))
1336 * Send CGI output...
1339 if (!strncmp(con
->uri
, "/admin", 6))
1341 cupsdSetStringf(&con
->command
, "%s/cgi-bin/admin.cgi",
1344 cupsdSetString(&con
->options
, strchr(con
->uri
+ 6, '?'));
1346 else if (!strncmp(con
->uri
, "/printers", 9))
1348 cupsdSetStringf(&con
->command
, "%s/cgi-bin/printers.cgi",
1351 if (con
->uri
[9] && con
->uri
[10])
1352 cupsdSetString(&con
->options
, con
->uri
+ 9);
1354 cupsdSetString(&con
->options
, NULL
);
1356 else if (!strncmp(con
->uri
, "/classes", 8))
1358 cupsdSetStringf(&con
->command
, "%s/cgi-bin/classes.cgi",
1361 if (con
->uri
[8] && con
->uri
[9])
1362 cupsdSetString(&con
->options
, con
->uri
+ 8);
1364 cupsdSetString(&con
->options
, NULL
);
1366 else if (!strncmp(con
->uri
, "/jobs", 5))
1368 cupsdSetStringf(&con
->command
, "%s/cgi-bin/jobs.cgi",
1371 if (con
->uri
[5] && con
->uri
[6])
1372 cupsdSetString(&con
->options
, con
->uri
+ 5);
1374 cupsdSetString(&con
->options
, NULL
);
1378 cupsdSetStringf(&con
->command
, "%s/cgi-bin/help.cgi",
1381 if (con
->uri
[5] && con
->uri
[6])
1382 cupsdSetString(&con
->options
, con
->uri
+ 5);
1384 cupsdSetString(&con
->options
, NULL
);
1387 if (!cupsdSendCommand(con
, con
->command
, con
->options
, 0))
1389 if (!cupsdSendError(con
, HTTP_NOT_FOUND
, CUPSD_AUTH_NONE
))
1391 cupsdCloseClient(con
);
1396 cupsdLogRequest(con
, HTTP_OK
);
1398 if (con
->http
.version
<= HTTP_1_0
)
1399 con
->http
.keep_alive
= HTTP_KEEPALIVE_OFF
;
1401 else if ((!strncmp(con
->uri
, "/admin/conf/", 12) &&
1402 (strchr(con
->uri
+ 12, '/') ||
1403 strlen(con
->uri
) == 12)) ||
1404 (!strncmp(con
->uri
, "/admin/log/", 11) &&
1405 (strchr(con
->uri
+ 11, '/') ||
1406 strlen(con
->uri
) == 11)))
1409 * GET can only be done to configuration files under
1413 if (!cupsdSendError(con
, HTTP_FORBIDDEN
, CUPSD_AUTH_NONE
))
1415 cupsdCloseClient(con
);
1427 if ((filename
= get_file(con
, &filestats
, buf
,
1428 sizeof(buf
))) == NULL
)
1430 if (!cupsdSendError(con
, HTTP_NOT_FOUND
, CUPSD_AUTH_NONE
))
1432 cupsdCloseClient(con
);
1439 type
= mimeFileType(MimeDatabase
, filename
, NULL
, NULL
);
1441 if (is_cgi(con
, filename
, &filestats
, type
))
1444 * Note: con->command and con->options were set by
1448 if (!cupsdSendCommand(con
, con
->command
, con
->options
, 0))
1450 if (!cupsdSendError(con
, HTTP_NOT_FOUND
, CUPSD_AUTH_NONE
))
1452 cupsdCloseClient(con
);
1457 cupsdLogRequest(con
, HTTP_OK
);
1459 if (con
->http
.version
<= HTTP_1_0
)
1460 con
->http
.keep_alive
= HTTP_KEEPALIVE_OFF
;
1464 if (!check_if_modified(con
, &filestats
))
1466 if (!cupsdSendError(con
, HTTP_NOT_MODIFIED
, CUPSD_AUTH_NONE
))
1468 cupsdCloseClient(con
);
1475 strcpy(line
, "text/plain");
1477 snprintf(line
, sizeof(line
), "%s/%s", type
->super
, type
->type
);
1479 if (!write_file(con
, HTTP_OK
, filename
, line
, &filestats
))
1481 cupsdCloseClient(con
);
1488 case HTTP_POST_RECV
:
1490 * See if the POST request includes a Content-Length field, and if
1491 * so check the length against any limits that are set...
1494 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "POST %s", con
->uri
);
1495 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "CONTENT_TYPE = %s",
1496 con
->http
.fields
[HTTP_FIELD_CONTENT_TYPE
]);
1498 if (con
->http
.fields
[HTTP_FIELD_CONTENT_LENGTH
][0] &&
1499 MaxRequestSize
> 0 &&
1500 con
->http
.data_remaining
> MaxRequestSize
)
1503 * Request too large...
1506 if (!cupsdSendError(con
, HTTP_REQUEST_TOO_LARGE
, CUPSD_AUTH_NONE
))
1508 cupsdCloseClient(con
);
1514 else if (con
->http
.data_remaining
< 0 ||
1515 (!con
->http
.fields
[HTTP_FIELD_CONTENT_LENGTH
][0] &&
1516 con
->http
.data_encoding
== HTTP_ENCODE_LENGTH
))
1519 * Negative content lengths are invalid!
1522 if (!cupsdSendError(con
, HTTP_BAD_REQUEST
, CUPSD_AUTH_NONE
))
1524 cupsdCloseClient(con
);
1532 * See what kind of POST request this is; for IPP requests the
1533 * content-type field will be "application/ipp"...
1536 if (!strcmp(con
->http
.fields
[HTTP_FIELD_CONTENT_TYPE
],
1538 con
->request
= ippNew();
1539 else if ((!strncmp(con
->uri
, "/admin", 6) &&
1540 strncmp(con
->uri
, "/admin/conf/", 12) &&
1541 strncmp(con
->uri
, "/admin/log/", 11)) ||
1542 !strncmp(con
->uri
, "/printers", 9) ||
1543 !strncmp(con
->uri
, "/classes", 8) ||
1544 !strncmp(con
->uri
, "/help", 5) ||
1545 !strncmp(con
->uri
, "/jobs", 5))
1551 if (!strncmp(con
->uri
, "/admin", 6))
1553 cupsdSetStringf(&con
->command
, "%s/cgi-bin/admin.cgi",
1556 cupsdSetString(&con
->options
, strchr(con
->uri
+ 6, '?'));
1558 else if (!strncmp(con
->uri
, "/printers", 9))
1560 cupsdSetStringf(&con
->command
, "%s/cgi-bin/printers.cgi",
1563 if (con
->uri
[9] && con
->uri
[10])
1564 cupsdSetString(&con
->options
, con
->uri
+ 9);
1566 cupsdSetString(&con
->options
, NULL
);
1568 else if (!strncmp(con
->uri
, "/classes", 8))
1570 cupsdSetStringf(&con
->command
, "%s/cgi-bin/classes.cgi",
1573 if (con
->uri
[8] && con
->uri
[9])
1574 cupsdSetString(&con
->options
, con
->uri
+ 8);
1576 cupsdSetString(&con
->options
, NULL
);
1578 else if (!strncmp(con
->uri
, "/jobs", 5))
1580 cupsdSetStringf(&con
->command
, "%s/cgi-bin/jobs.cgi",
1583 if (con
->uri
[5] && con
->uri
[6])
1584 cupsdSetString(&con
->options
, con
->uri
+ 5);
1586 cupsdSetString(&con
->options
, NULL
);
1590 cupsdSetStringf(&con
->command
, "%s/cgi-bin/help.cgi",
1593 if (con
->uri
[5] && con
->uri
[6])
1594 cupsdSetString(&con
->options
, con
->uri
+ 5);
1596 cupsdSetString(&con
->options
, NULL
);
1599 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1600 "cupsdReadClient: %d command=\"%s\", "
1602 con
->http
.fd
, con
->command
,
1603 con
->options
? con
->options
: "(null)");
1605 if (con
->http
.version
<= HTTP_1_0
)
1606 con
->http
.keep_alive
= HTTP_KEEPALIVE_OFF
;
1614 if ((filename
= get_file(con
, &filestats
, buf
,
1615 sizeof(buf
))) == NULL
)
1617 if (!cupsdSendError(con
, HTTP_NOT_FOUND
, CUPSD_AUTH_NONE
))
1619 cupsdCloseClient(con
);
1626 type
= mimeFileType(MimeDatabase
, filename
, NULL
, NULL
);
1628 if (!is_cgi(con
, filename
, &filestats
, type
))
1631 * Only POST to CGI's...
1634 if (!cupsdSendError(con
, HTTP_UNAUTHORIZED
, CUPSD_AUTH_NONE
))
1636 cupsdCloseClient(con
);
1643 case HTTP_PUT_RECV
:
1645 * Validate the resource name...
1648 if (strncmp(con
->uri
, "/admin/conf/", 12) ||
1649 strchr(con
->uri
+ 12, '/') ||
1650 strlen(con
->uri
) == 12)
1653 * PUT can only be done to configuration files under
1657 if (!cupsdSendError(con
, HTTP_FORBIDDEN
, CUPSD_AUTH_NONE
))
1659 cupsdCloseClient(con
);
1667 * See if the PUT request includes a Content-Length field, and if
1668 * so check the length against any limits that are set...
1671 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "PUT %s", con
->uri
);
1672 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "CONTENT_TYPE = %s",
1673 con
->http
.fields
[HTTP_FIELD_CONTENT_TYPE
]);
1675 if (con
->http
.fields
[HTTP_FIELD_CONTENT_LENGTH
][0] &&
1676 MaxRequestSize
> 0 &&
1677 con
->http
.data_remaining
> MaxRequestSize
)
1680 * Request too large...
1683 if (!cupsdSendError(con
, HTTP_REQUEST_TOO_LARGE
, CUPSD_AUTH_NONE
))
1685 cupsdCloseClient(con
);
1691 else if (con
->http
.data_remaining
< 0)
1694 * Negative content lengths are invalid!
1697 if (!cupsdSendError(con
, HTTP_BAD_REQUEST
, CUPSD_AUTH_NONE
))
1699 cupsdCloseClient(con
);
1707 * Open a temporary file to hold the request...
1710 cupsdSetStringf(&con
->filename
, "%s/%08x", RequestRoot
,
1712 con
->file
= open(con
->filename
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0640);
1716 cupsdLogMessage(CUPSD_LOG_ERROR
,
1717 "Unable to create request file %s: %s",
1718 con
->filename
, strerror(errno
));
1720 if (!cupsdSendError(con
, HTTP_REQUEST_TOO_LARGE
, CUPSD_AUTH_NONE
))
1722 cupsdCloseClient(con
);
1727 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1728 "cupsdReadClient: %d REQUEST %s=%d", con
->http
.fd
,
1729 con
->filename
, con
->file
);
1731 fchmod(con
->file
, 0640);
1732 fchown(con
->file
, RunUser
, Group
);
1733 fcntl(con
->file
, F_SETFD
, fcntl(con
->file
, F_GETFD
) | FD_CLOEXEC
);
1738 cupsdSendError(con
, HTTP_NOT_IMPLEMENTED
, CUPSD_AUTH_NONE
);
1739 cupsdCloseClient(con
);
1743 if (!strncmp(con
->uri
, "/printers/", 10) &&
1744 !strcmp(con
->uri
+ strlen(con
->uri
) - 4, ".ppd"))
1747 * Send PPD file - get the real printer name since printer
1748 * names are not case sensitive but filenames can be...
1751 con
->uri
[strlen(con
->uri
) - 4] = '\0'; /* Drop ".ppd" */
1753 if ((p
= cupsdFindPrinter(con
->uri
+ 10)) != NULL
)
1754 snprintf(con
->uri
, sizeof(con
->uri
), "/ppd/%s.ppd", p
->name
);
1757 if (!cupsdSendError(con
, HTTP_NOT_FOUND
, CUPSD_AUTH_NONE
))
1759 cupsdCloseClient(con
);
1767 if ((!strncmp(con
->uri
, "/admin", 6) &&
1768 strncmp(con
->uri
, "/admin/conf/", 12) &&
1769 strncmp(con
->uri
, "/admin/log/", 11)) ||
1770 !strncmp(con
->uri
, "/printers", 9) ||
1771 !strncmp(con
->uri
, "/classes", 8) ||
1772 !strncmp(con
->uri
, "/help", 5) ||
1773 !strncmp(con
->uri
, "/jobs", 5))
1779 if (!cupsdSendHeader(con
, HTTP_OK
, "text/html", CUPSD_AUTH_NONE
))
1781 cupsdCloseClient(con
);
1785 if (httpPrintf(HTTP(con
), "\r\n") < 0)
1787 cupsdCloseClient(con
);
1791 if (cupsdFlushHeader(con
) < 0)
1793 cupsdCloseClient(con
);
1797 cupsdLogRequest(con
, HTTP_OK
);
1799 else if ((!strncmp(con
->uri
, "/admin/conf/", 12) &&
1800 (strchr(con
->uri
+ 12, '/') ||
1801 strlen(con
->uri
) == 12)) ||
1802 (!strncmp(con
->uri
, "/admin/log/", 11) &&
1803 (strchr(con
->uri
+ 11, '/') ||
1804 strlen(con
->uri
) == 11)))
1807 * HEAD can only be done to configuration files under
1811 if (!cupsdSendError(con
, HTTP_FORBIDDEN
, CUPSD_AUTH_NONE
))
1813 cupsdCloseClient(con
);
1819 else if ((filename
= get_file(con
, &filestats
, buf
,
1820 sizeof(buf
))) == NULL
)
1822 if (!cupsdSendHeader(con
, HTTP_NOT_FOUND
, "text/html", CUPSD_AUTH_NONE
))
1824 cupsdCloseClient(con
);
1828 cupsdLogRequest(con
, HTTP_NOT_FOUND
);
1830 else if (!check_if_modified(con
, &filestats
))
1832 if (!cupsdSendError(con
, HTTP_NOT_MODIFIED
, CUPSD_AUTH_NONE
))
1834 cupsdCloseClient(con
);
1838 cupsdLogRequest(con
, HTTP_NOT_MODIFIED
);
1846 type
= mimeFileType(MimeDatabase
, filename
, NULL
, NULL
);
1848 strcpy(line
, "text/plain");
1850 snprintf(line
, sizeof(line
), "%s/%s", type
->super
, type
->type
);
1852 if (!cupsdSendHeader(con
, HTTP_OK
, line
, CUPSD_AUTH_NONE
))
1854 cupsdCloseClient(con
);
1858 if (httpPrintf(HTTP(con
), "Last-Modified: %s\r\n",
1859 httpGetDateString(filestats
.st_mtime
)) < 0)
1861 cupsdCloseClient(con
);
1865 if (httpPrintf(HTTP(con
), "Content-Length: %lu\r\n",
1866 (unsigned long)filestats
.st_size
) < 0)
1868 cupsdCloseClient(con
);
1872 cupsdLogRequest(con
, HTTP_OK
);
1875 if (httpPrintf(HTTP(con
), "\r\n") < 0)
1877 cupsdCloseClient(con
);
1881 if (cupsdFlushHeader(con
) < 0)
1883 cupsdCloseClient(con
);
1887 con
->http
.state
= HTTP_WAITING
;
1891 break; /* Anti-compiler-warning-code */
1897 * Handle any incoming data...
1900 switch (con
->http
.state
)
1902 case HTTP_PUT_RECV
:
1903 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1904 "cupsdReadClient: %d con->data_encoding=HTTP_ENCODE_%s, "
1905 "con->data_remaining=" CUPS_LLFMT
", con->file=%d",
1907 con
->http
.data_encoding
== HTTP_ENCODE_CHUNKED
?
1908 "CHUNKED" : "LENGTH",
1909 CUPS_LLCAST con
->http
.data_remaining
, con
->file
);
1913 if ((bytes
= httpRead2(HTTP(con
), line
, sizeof(line
))) < 0)
1915 cupsdCloseClient(con
);
1920 con
->bytes
+= bytes
;
1922 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1923 "cupsdReadClient: %d writing %d bytes to %d",
1924 con
->http
.fd
, bytes
, con
->file
);
1926 if (write(con
->file
, line
, bytes
) < bytes
)
1928 cupsdLogMessage(CUPSD_LOG_ERROR
,
1929 "cupsdReadClient: Unable to write %d bytes to %s: %s",
1930 bytes
, con
->filename
, strerror(errno
));
1932 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1933 "cupsdReadClient: Closing data file %d...",
1938 unlink(con
->filename
);
1939 cupsdClearString(&con
->filename
);
1941 if (!cupsdSendError(con
, HTTP_REQUEST_TOO_LARGE
, CUPSD_AUTH_NONE
))
1943 cupsdCloseClient(con
);
1949 while (con
->http
.state
== HTTP_PUT_RECV
&& con
->http
.used
> 0);
1951 if (con
->http
.state
== HTTP_WAITING
)
1954 * End of file, see how big it is...
1957 fstat(con
->file
, &filestats
);
1959 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1960 "cupsdReadClient: %d Closing data file %d, size="
1962 con
->http
.fd
, con
->file
,
1963 CUPS_LLCAST filestats
.st_size
);
1968 if (filestats
.st_size
> MaxRequestSize
&&
1972 * Request is too big; remove it and send an error...
1975 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1976 "cupsdReadClient: %d Removing temp file %s",
1977 con
->http
.fd
, con
->filename
);
1978 unlink(con
->filename
);
1979 cupsdClearString(&con
->filename
);
1981 if (!cupsdSendError(con
, HTTP_REQUEST_TOO_LARGE
, CUPSD_AUTH_NONE
))
1983 cupsdCloseClient(con
);
1989 * Install the configuration file...
1992 status
= install_conf_file(con
);
1995 * Return the status to the client...
1998 if (!cupsdSendError(con
, status
, CUPSD_AUTH_NONE
))
2000 cupsdCloseClient(con
);
2006 case HTTP_POST_RECV
:
2007 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2008 "cupsdReadClient: %d con->data_encoding=HTTP_ENCODE_"
2009 "%s, con->data_remaining=" CUPS_LLFMT
", con->file=%d",
2011 con
->http
.data_encoding
== HTTP_ENCODE_CHUNKED
?
2012 "CHUNKED" : "LENGTH",
2013 CUPS_LLCAST con
->http
.data_remaining
, con
->file
);
2020 * Grab any request data from the connection...
2023 if ((ipp_state
= ippRead(&(con
->http
), con
->request
)) == IPP_ERROR
)
2025 cupsdLogMessage(CUPSD_LOG_ERROR
,
2026 "cupsdReadClient: %d IPP Read Error!",
2029 cupsdSendError(con
, HTTP_BAD_REQUEST
, CUPSD_AUTH_NONE
);
2030 cupsdCloseClient(con
);
2033 else if (ipp_state
!= IPP_DATA
)
2035 if (con
->http
.state
== HTTP_POST_SEND
)
2037 cupsdSendError(con
, HTTP_BAD_REQUEST
, CUPSD_AUTH_NONE
);
2038 cupsdCloseClient(con
);
2045 con
->bytes
+= ippLength(con
->request
);
2048 if (con
->file
< 0 && con
->http
.state
!= HTTP_POST_SEND
)
2051 * Create a file as needed for the request data...
2054 cupsdSetStringf(&con
->filename
, "%s/%08x", RequestRoot
, request_id
++);
2055 con
->file
= open(con
->filename
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0640);
2059 cupsdLogMessage(CUPSD_LOG_ERROR
,
2060 "Unable to create request file %s: %s",
2061 con
->filename
, strerror(errno
));
2063 if (!cupsdSendError(con
, HTTP_REQUEST_TOO_LARGE
, CUPSD_AUTH_NONE
))
2065 cupsdCloseClient(con
);
2070 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdReadClient: %d REQUEST %s=%d", con
->http
.fd
,
2071 con
->filename
, con
->file
);
2073 fchmod(con
->file
, 0640);
2074 fchown(con
->file
, RunUser
, Group
);
2075 fcntl(con
->file
, F_SETFD
, fcntl(con
->file
, F_GETFD
) | FD_CLOEXEC
);
2078 if (con
->http
.state
!= HTTP_POST_SEND
)
2080 if ((bytes
= httpRead2(HTTP(con
), line
, sizeof(line
))) < 0)
2082 cupsdCloseClient(con
);
2087 con
->bytes
+= bytes
;
2089 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2090 "cupsdReadClient: %d writing %d bytes to %d",
2091 con
->http
.fd
, bytes
, con
->file
);
2093 if (write(con
->file
, line
, bytes
) < bytes
)
2095 cupsdLogMessage(CUPSD_LOG_ERROR
,
2096 "cupsdReadClient: Unable to write %d bytes to %s: %s",
2097 bytes
, con
->filename
, strerror(errno
));
2099 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2100 "cupsdReadClient: Closing file %d...",
2105 unlink(con
->filename
);
2106 cupsdClearString(&con
->filename
);
2108 if (!cupsdSendError(con
, HTTP_REQUEST_TOO_LARGE
, CUPSD_AUTH_NONE
))
2110 cupsdCloseClient(con
);
2115 else if (con
->http
.state
== HTTP_POST_RECV
)
2117 else if (con
->http
.state
!= HTTP_POST_SEND
)
2119 cupsdCloseClient(con
);
2124 while (con
->http
.state
== HTTP_POST_RECV
&& con
->http
.used
> 0);
2126 if (con
->http
.state
== HTTP_POST_SEND
)
2130 fstat(con
->file
, &filestats
);
2132 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2133 "cupsdReadClient: %d Closing data file %d, "
2134 "size=" CUPS_LLFMT
".",
2135 con
->http
.fd
, con
->file
,
2136 CUPS_LLCAST filestats
.st_size
);
2141 if (filestats
.st_size
> MaxRequestSize
&&
2145 * Request is too big; remove it and send an error...
2148 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2149 "cupsdReadClient: %d Removing temp file %s",
2150 con
->http
.fd
, con
->filename
);
2151 unlink(con
->filename
);
2152 cupsdClearString(&con
->filename
);
2157 * Delete any IPP request data...
2160 ippDelete(con
->request
);
2161 con
->request
= NULL
;
2164 if (!cupsdSendError(con
, HTTP_REQUEST_TOO_LARGE
, CUPSD_AUTH_NONE
))
2166 cupsdCloseClient(con
);
2173 if (!cupsdSendCommand(con
, con
->command
, con
->options
, 0))
2175 if (!cupsdSendError(con
, HTTP_NOT_FOUND
, CUPSD_AUTH_NONE
))
2177 cupsdCloseClient(con
);
2182 cupsdLogRequest(con
, HTTP_OK
);
2188 cupsdProcessIPPRequest(con
);
2192 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2193 "cupsdReadClient: %d Removing temp file %s",
2194 con
->http
.fd
, con
->filename
);
2195 unlink(con
->filename
);
2196 cupsdClearString(&con
->filename
);
2205 break; /* Anti-compiler-warning-code */
2208 if (con
->http
.state
== HTTP_WAITING
)
2210 if (!con
->http
.keep_alive
)
2211 cupsdCloseClient(con
);
2214 cupsArrayRemove(ActiveClients
, con
);
2215 cupsdSetBusyState();
2222 * 'cupsdSendCommand()' - Send output from a command via HTTP.
2225 int /* O - 1 on success, 0 on failure */
2227 cupsd_client_t
*con
, /* I - Client connection */
2228 char *command
, /* I - Command to run */
2229 char *options
, /* I - Command-line options */
2230 int root
) /* I - Run as root? */
2232 int fd
; /* Standard input file descriptor */
2237 fd
= open(con
->filename
, O_RDONLY
);
2241 cupsdLogMessage(CUPSD_LOG_ERROR
,
2242 "cupsdSendCommand: %d Unable to open \"%s\" for reading: %s",
2243 con
->http
.fd
, con
->filename
? con
->filename
: "/dev/null",
2248 fcntl(fd
, F_SETFD
, fcntl(fd
, F_GETFD
) | FD_CLOEXEC
);
2253 con
->pipe_pid
= pipe_command(con
, fd
, &(con
->file
), command
, options
, root
);
2258 cupsdLogMessage(CUPSD_LOG_INFO
, "Started \"%s\" (pid=%d)", command
,
2261 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdSendCommand: %d file=%d",
2262 con
->http
.fd
, con
->file
);
2264 if (con
->pipe_pid
== 0)
2267 fcntl(con
->file
, F_SETFD
, fcntl(con
->file
, F_GETFD
) | FD_CLOEXEC
);
2269 cupsdAddSelect(con
->file
, (cupsd_selfunc_t
)write_pipe
, NULL
, con
);
2271 con
->sent_header
= 0;
2272 con
->file_ready
= 0;
2273 con
->got_fields
= 0;
2281 * 'cupsdSendError()' - Send an error message via HTTP.
2284 int /* O - 1 if successful, 0 otherwise */
2285 cupsdSendError(cupsd_client_t
*con
, /* I - Connection */
2286 http_status_t code
, /* I - Error code */
2287 int auth_type
)/* I - Authentication type */
2291 * Force client to upgrade for authentication if that is how the
2292 * server is configured...
2295 if (code
== HTTP_UNAUTHORIZED
&&
2296 DefaultEncryption
== HTTP_ENCRYPT_REQUIRED
&&
2297 strcasecmp(con
->http
.hostname
, "localhost") &&
2300 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2301 "cupsdSendError: Encryption before authentication!");
2302 code
= HTTP_UPGRADE_REQUIRED
;
2304 #endif /* HAVE_SSL */
2307 * Put the request in the access_log file...
2310 cupsdLogRequest(con
, code
);
2312 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdSendError: %d code=%d (%s)",
2313 con
->http
.fd
, code
, httpStatus(code
));
2316 * To work around bugs in some proxies, don't use Keep-Alive for some
2319 * Kerberos authentication doesn't work without Keep-Alive, so
2320 * never disable it in that case.
2323 if (code
>= HTTP_BAD_REQUEST
&& con
->http
.auth_type
!= CUPSD_AUTH_NEGOTIATE
)
2324 con
->http
.keep_alive
= HTTP_KEEPALIVE_OFF
;
2327 * Send an error message back to the client. If the error code is a
2328 * 400 or 500 series, make sure the message contains some text, too!
2331 if (!cupsdSendHeader(con
, code
, NULL
, auth_type
))
2335 if (code
== HTTP_UPGRADE_REQUIRED
)
2336 if (httpPrintf(HTTP(con
), "Connection: Upgrade\r\n") < 0)
2339 if (httpPrintf(HTTP(con
), "Upgrade: TLS/1.0,HTTP/1.1\r\n") < 0)
2341 #endif /* HAVE_SSL */
2343 if (con
->http
.version
>= HTTP_1_1
&&
2344 con
->http
.keep_alive
== HTTP_KEEPALIVE_OFF
)
2346 if (httpPrintf(HTTP(con
), "Connection: close\r\n") < 0)
2350 if (code
>= HTTP_BAD_REQUEST
)
2353 * Send a human-readable error message.
2356 char message
[4096], /* Message for user */
2357 urltext
[1024], /* URL redirection text */
2358 redirect
[1024]; /* Redirection link */
2359 const char *text
; /* Status-specific text */
2364 if (code
== HTTP_UNAUTHORIZED
)
2365 text
= _cupsLangString(con
->language
,
2366 _("Enter your username and password or the "
2367 "root username and password to access this "
2368 "page. If you are using Kerberos authentication, "
2369 "make sure you have a valid Kerberos ticket."));
2370 else if (code
== HTTP_UPGRADE_REQUIRED
)
2374 snprintf(urltext
, sizeof(urltext
),
2375 _cupsLangString(con
->language
,
2376 _("You must access this page using the URL "
2377 "<A HREF=\"https://%s:%d%s\">"
2378 "https://%s:%d%s</A>.")),
2379 con
->servername
, con
->serverport
, con
->uri
,
2380 con
->servername
, con
->serverport
, con
->uri
);
2382 snprintf(redirect
, sizeof(redirect
),
2383 "<META HTTP-EQUIV=\"Refresh\" "
2384 "CONTENT=\"3;URL=https://%s:%d%s\">\n",
2385 con
->servername
, con
->serverport
, con
->uri
);
2390 snprintf(message
, sizeof(message
),
2391 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\" "
2392 "\"http://www.w3.org/TR/REC-html40/loose.dtd\">\n"
2395 "\t<META HTTP-EQUIV=\"Content-Type\" "
2396 "CONTENT=\"text/html; charset=utf-8\">\n"
2397 "\t<TITLE>%d %s</TITLE>\n"
2398 "\t<LINK REL=\"STYLESHEET\" TYPE=\"text/css\" "
2399 "HREF=\"/cups.css\">\n"
2407 code
, httpStatus(code
), redirect
, code
, httpStatus(code
), text
);
2409 if (httpPrintf(HTTP(con
), "Content-Type: text/html; charset=utf-8\r\n") < 0)
2411 if (httpPrintf(HTTP(con
), "Content-Length: %d\r\n",
2412 (int)strlen(message
)) < 0)
2414 if (httpPrintf(HTTP(con
), "\r\n") < 0)
2416 if (httpPrintf(HTTP(con
), "%s", message
) < 0)
2419 else if (httpPrintf(HTTP(con
), "\r\n") < 0)
2422 if (cupsdFlushHeader(con
) < 0)
2425 con
->http
.state
= HTTP_WAITING
;
2432 * 'cupsdSendHeader()' - Send an HTTP request.
2435 int /* O - 1 on success, 0 on failure */
2437 cupsd_client_t
*con
, /* I - Client to send to */
2438 http_status_t code
, /* I - HTTP status code */
2439 char *type
, /* I - MIME type of document */
2440 int auth_type
) /* I - Type of authentication */
2442 char auth_str
[1024]; /* Authorization string */
2444 static char *gss_buf
= NULL
; /* Kerberos auth data buffer */
2445 static int gss_bufsize
= 0; /* Size of Kerberos auth data buffer */
2446 #endif /* HAVE_GSSAPI */
2450 * Send the HTTP status header...
2453 if (code
== HTTP_CONTINUE
)
2456 * 100-continue doesn't send any headers...
2459 return (httpPrintf(HTTP(con
), "HTTP/%d.%d 100 Continue\r\n\r\n",
2460 con
->http
.version
/ 100, con
->http
.version
% 100) > 0);
2463 httpFlushWrite(HTTP(con
));
2465 con
->http
.data_encoding
= HTTP_ENCODE_FIELDS
;
2467 if (httpPrintf(HTTP(con
), "HTTP/%d.%d %d %s\r\n", con
->http
.version
/ 100,
2468 con
->http
.version
% 100, code
, httpStatus(code
)) < 0)
2470 if (httpPrintf(HTTP(con
), "Date: %s\r\n", httpGetDateString(time(NULL
))) < 0)
2473 if (httpPrintf(HTTP(con
), "Server: %s\r\n", ServerHeader
) < 0)
2475 if (con
->http
.keep_alive
&& con
->http
.version
>= HTTP_1_0
)
2477 if (httpPrintf(HTTP(con
), "Connection: Keep-Alive\r\n") < 0)
2479 if (httpPrintf(HTTP(con
), "Keep-Alive: timeout=%d\r\n",
2480 KeepAliveTimeout
) < 0)
2483 if (code
== HTTP_METHOD_NOT_ALLOWED
)
2484 if (httpPrintf(HTTP(con
), "Allow: GET, HEAD, OPTIONS, POST\r\n") < 0)
2487 if (code
== HTTP_UNAUTHORIZED
)
2489 if (auth_type
== CUPSD_AUTH_NONE
)
2491 if (!con
->best
|| con
->best
->type
<= CUPSD_AUTH_NONE
)
2492 auth_type
= DefaultAuthType
;
2494 auth_type
= con
->best
->type
;
2499 if (auth_type
== CUPSD_AUTH_BASIC
|| auth_type
== CUPSD_AUTH_BASICDIGEST
)
2500 strlcpy(auth_str
, "Basic realm=\"CUPS\"", sizeof(auth_str
));
2501 else if (auth_type
== CUPSD_AUTH_DIGEST
)
2502 snprintf(auth_str
, sizeof(auth_str
), "Digest realm=\"CUPS\", nonce=\"%s\"",
2503 con
->http
.hostname
);
2505 else if (auth_type
== CUPSD_AUTH_NEGOTIATE
&& con
->gss_output_token
.length
== 0)
2506 strlcpy(auth_str
, "Negotiate", sizeof(auth_str
));
2507 #endif /* HAVE_GSSAPI */
2509 #ifdef HAVE_AUTHORIZATION_H
2510 if (con
->best
&& auth_type
!= CUPSD_AUTH_NEGOTIATE
)
2512 int i
; /* Looping var */
2513 char *auth_key
; /* Auth key buffer */
2514 size_t auth_size
; /* Size of remaining buffer */
2517 auth_key
= auth_str
+ strlen(auth_str
);
2518 auth_size
= sizeof(auth_str
) - (auth_key
- auth_str
);
2520 for (i
= 0; i
< con
->best
->num_names
; i
++)
2522 if (!strncasecmp(con
->best
->names
[i
], "@AUTHKEY(", 9))
2524 snprintf(auth_key
, auth_size
, ", authkey=\"%s\"",
2525 con
->best
->names
[i
] + 9);
2526 /* end parenthesis is stripped in conf.c */
2529 else if (!strcasecmp(con
->best
->names
[i
], "@SYSTEM") &&
2532 snprintf(auth_key
, auth_size
, ", authkey=\"%s\"", SystemGroupAuthKey
);
2537 #endif /* HAVE_AUTHORIZATION_H */
2541 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdSendHeader: WWW-Authenticate: %s",
2544 if (httpPrintf(HTTP(con
), "WWW-Authenticate: %s\r\n", auth_str
) < 0)
2551 * WWW-Authenticate: Negotiate can be included even for
2552 * non-401 replies...
2555 if (con
->gss_output_token
.length
> 0 && con
->gss_output_token
.length
<= 65536)
2557 OM_uint32 minor_status
; /* Minor status code */
2558 int bufsize
; /* Size of output token buffer */
2561 bufsize
= con
->gss_output_token
.length
* 4 / 3 + 2;
2563 if (bufsize
> gss_bufsize
)
2565 char *buf
; /* New buffer */
2568 bufsize
= (bufsize
+ 1023) & 1023;/* Round up */
2571 buf
= realloc(gss_buf
, bufsize
);
2573 buf
= malloc(bufsize
);
2577 cupsdLogMessage(CUPSD_LOG_ERROR
,
2578 "Unable to allocate %d bytes for Kerberos credentials!",
2584 gss_bufsize
= bufsize
;
2587 httpEncode64_2(gss_buf
, gss_bufsize
,
2588 con
->gss_output_token
.value
,
2589 con
->gss_output_token
.length
);
2590 gss_release_buffer(&minor_status
, &con
->gss_output_token
);
2592 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2593 "cupsdSendHeader: WWW-Authenticate: Negotiate %s", gss_buf
);
2595 if (httpPrintf(HTTP(con
), "WWW-Authenticate: Negotiate %s\r\n",
2599 else if (con
->gss_output_token
.length
> 65536)
2601 cupsdLogMessage(CUPSD_LOG_ERROR
,
2602 "Kerberos credentials larger than 64k (%d)!",
2603 (int)con
->gss_output_token
.length
);
2606 #endif /* HAVE_GSSAPI */
2608 if (con
->language
&& strcmp(con
->language
->language
, "C"))
2610 if (httpPrintf(HTTP(con
), "Content-Language: %s\r\n",
2611 con
->language
->language
) < 0)
2617 if (!strcmp(type
, "text/html"))
2619 if (httpPrintf(HTTP(con
),
2620 "Content-Type: text/html; charset=utf-8\r\n") < 0)
2623 else if (httpPrintf(HTTP(con
), "Content-Type: %s\r\n", type
) < 0)
2632 * 'cupsdUpdateCGI()' - Read status messages from CGI scripts and programs.
2636 cupsdUpdateCGI(void)
2638 char *ptr
, /* Pointer to end of line in buffer */
2639 message
[1024]; /* Pointer to message text */
2640 int loglevel
; /* Log level for message */
2643 while ((ptr
= cupsdStatBufUpdate(CGIStatusBuffer
, &loglevel
,
2644 message
, sizeof(message
))) != NULL
)
2645 if (!strchr(CGIStatusBuffer
->buffer
, '\n'))
2648 if (ptr
== NULL
&& !CGIStatusBuffer
->bufused
)
2651 * Fatal error on pipe - should never happen!
2654 cupsdLogMessage(CUPSD_LOG_CRIT
,
2655 "cupsdUpdateCGI: error reading from CGI error pipe - %s",
2662 * 'cupsdWriteClient()' - Write data to a client as needed.
2666 cupsdWriteClient(cupsd_client_t
*con
) /* I - Client connection */
2668 int bytes
; /* Number of bytes written */
2669 char buf
[16385]; /* Data buffer */
2670 char *bufptr
; /* Pointer into buffer */
2671 ipp_state_t ipp_state
; /* IPP state value */
2675 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2676 "cupsdWriteClient(con=%p) %d response=%p(%d), file=%d "
2677 "pipe_pid=%d state=%d",
2678 con
, con
->http
.fd
, con
->response
,
2679 con
->response
? con
->response
->state
: -1,
2680 con
->file
, con
->pipe_pid
, con
->http
.state
);
2683 if (con
->http
.state
!= HTTP_GET_SEND
&&
2684 con
->http
.state
!= HTTP_POST_SEND
)
2690 * Make sure we select on the CGI output...
2693 cupsdAddSelect(con
->file
, (cupsd_selfunc_t
)write_pipe
, NULL
, con
);
2695 if (!con
->file_ready
)
2698 * Try again later when there is CGI output available...
2701 cupsdRemoveSelect(con
->http
.fd
);
2705 con
->file_ready
= 0;
2708 if (con
->response
&& con
->response
->state
!= IPP_DATA
)
2710 ipp_state
= ippWrite(HTTP(con
), con
->response
);
2711 bytes
= ipp_state
!= IPP_ERROR
&&
2712 (con
->file
>= 0 || ipp_state
!= IPP_DATA
);
2714 else if ((bytes
= read(con
->file
, buf
, sizeof(buf
) - 1)) > 0)
2716 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2717 "cupsdWriteClient: Read %d bytes from file %d...",
2720 if (con
->pipe_pid
&& !con
->got_fields
)
2723 * Inspect the data for Content-Type and other fields.
2728 for (bufptr
= buf
; !con
->got_fields
&& *bufptr
; bufptr
++)
2729 if (*bufptr
== '\n')
2732 * Send line to client...
2735 if (bufptr
> buf
&& bufptr
[-1] == '\r')
2739 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "Script header: %s", buf
);
2741 if (!con
->sent_header
)
2744 * Handle redirection and CGI status codes...
2747 if (!strncasecmp(buf
, "Location:", 9))
2749 cupsdSendHeader(con
, HTTP_SEE_OTHER
, NULL
, CUPSD_AUTH_NONE
);
2750 con
->sent_header
= 2;
2752 if (httpPrintf(HTTP(con
), "Content-Length: 0\r\n") < 0)
2755 else if (!strncasecmp(buf
, "Status:", 7))
2757 cupsdSendError(con
, (http_status_t
)atoi(buf
+ 7), CUPSD_AUTH_NONE
);
2758 con
->sent_header
= 2;
2762 cupsdSendHeader(con
, HTTP_OK
, NULL
, CUPSD_AUTH_NONE
);
2763 con
->sent_header
= 1;
2765 if (con
->http
.version
== HTTP_1_1
)
2767 if (httpPrintf(HTTP(con
), "Transfer-Encoding: chunked\r\n") < 0)
2773 if (strncasecmp(buf
, "Status:", 7))
2774 httpPrintf(HTTP(con
), "%s\r\n", buf
);
2776 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdWriteClient: %d %s",
2783 bytes
-= (bufptr
- buf
);
2784 memmove(buf
, bufptr
, bytes
+ 1);
2788 * See if the line was empty...
2791 if (con
->field_col
== 0)
2793 con
->got_fields
= 1;
2795 if (cupsdFlushHeader(con
) < 0)
2797 cupsdCloseClient(con
);
2801 if (con
->http
.version
== HTTP_1_1
)
2802 con
->http
.data_encoding
= HTTP_ENCODE_CHUNKED
;
2807 else if (*bufptr
!= '\r')
2810 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2811 "cupsdWriteClient: %d bytes=%d, got_fields=%d",
2812 con
->http
.fd
, bytes
, con
->got_fields
);
2814 if (bytes
> 0 && !con
->got_fields
)
2817 * Remaining text needs to go out...
2820 httpPrintf(HTTP(con
), "%s", buf
);
2822 con
->http
.activity
= time(NULL
);
2825 else if (bytes
== 0)
2827 con
->http
.activity
= time(NULL
);
2834 if (httpWrite2(HTTP(con
), buf
, bytes
) < 0)
2836 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2837 "cupsdWriteClient: %d Write of %d bytes failed!",
2838 con
->http
.fd
, bytes
);
2840 cupsdCloseClient(con
);
2844 if (con
->http
.data_encoding
== HTTP_ENCODE_CHUNKED
)
2845 httpFlushWrite(HTTP(con
));
2847 con
->bytes
+= bytes
;
2849 if (con
->http
.state
== HTTP_WAITING
)
2856 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdWriteClient: %d bytes < 0",
2859 cupsdLogRequest(con
, HTTP_OK
);
2861 httpFlushWrite(HTTP(con
));
2863 if (con
->http
.data_encoding
== HTTP_ENCODE_CHUNKED
&& con
->sent_header
== 1)
2865 if (httpWrite2(HTTP(con
), "", 0) < 0)
2867 cupsdCloseClient(con
);
2872 con
->http
.state
= HTTP_WAITING
;
2874 cupsdAddSelect(con
->http
.fd
, (cupsd_selfunc_t
)cupsdReadClient
, NULL
, con
);
2878 cupsdRemoveSelect(con
->file
);
2881 cupsdEndProcess(con
->pipe_pid
, 0);
2883 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2884 "cupsdWriteClient: %d Closing data file %d.",
2885 con
->http
.fd
, con
->file
);
2894 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2895 "cupsdWriteClient: %d Removing temp file %s",
2896 con
->http
.fd
, con
->filename
);
2897 unlink(con
->filename
);
2898 cupsdClearString(&con
->filename
);
2903 ippDelete(con
->request
);
2904 con
->request
= NULL
;
2909 ippDelete(con
->response
);
2910 con
->response
= NULL
;
2913 cupsdClearString(&con
->command
);
2914 cupsdClearString(&con
->options
);
2915 cupsdClearString(&con
->query_string
);
2917 if (!con
->http
.keep_alive
)
2919 cupsdCloseClient(con
);
2924 con
->http
.activity
= time(NULL
);
2929 * 'check_if_modified()' - Decode an "If-Modified-Since" line.
2932 static int /* O - 1 if modified since */
2934 cupsd_client_t
*con
, /* I - Client connection */
2935 struct stat
*filestats
) /* I - File information */
2937 char *ptr
; /* Pointer into field */
2938 time_t date
; /* Time/date value */
2939 off_t size
; /* Size/length value */
2944 ptr
= con
->http
.fields
[HTTP_FIELD_IF_MODIFIED_SINCE
];
2949 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2950 "check_if_modified: %d If-Modified-Since=\"%s\"",
2953 while (*ptr
!= '\0')
2955 while (isspace(*ptr
) || *ptr
== ';')
2958 if (strncasecmp(ptr
, "length=", 7) == 0)
2961 size
= strtoll(ptr
, NULL
, 10);
2963 while (isdigit(*ptr
))
2966 else if (isalpha(*ptr
))
2968 date
= httpGetDateTime(ptr
);
2969 while (*ptr
!= '\0' && *ptr
!= ';')
2976 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2977 "check_if_modified: %d sizes=" CUPS_LLFMT
","
2978 CUPS_LLFMT
" dates=%d,%d",
2979 con
->http
.fd
, CUPS_LLCAST size
,
2980 CUPS_LLCAST filestats
->st_size
, (int)date
,
2981 (int)filestats
->st_mtime
);
2983 return ((size
!= filestats
->st_size
&& size
!= 0) ||
2984 (date
< filestats
->st_mtime
&& date
!= 0) ||
2985 (size
== 0 && date
== 0));
2990 * 'compare_clients()' - Compare two client connections.
2993 static int /* O - Result of comparison */
2994 compare_clients(cupsd_client_t
*a
, /* I - First client */
2995 cupsd_client_t
*b
, /* I - Second client */
2996 void *data
) /* I - User data (not used) */
3011 * 'encrypt_client()' - Enable encryption for the client...
3014 static int /* O - 1 on success, 0 on error */
3015 encrypt_client(cupsd_client_t
*con
) /* I - Client to encrypt */
3018 SSL_CTX
*context
; /* Context for encryption */
3019 SSL
*conn
; /* Connection for encryption */
3020 BIO
*bio
; /* BIO data */
3021 unsigned long error
; /* Error code */
3025 * Verify that we have a certificate...
3028 if (access(ServerKey
, 0) || access(ServerCertificate
, 0))
3031 * Nope, make a self-signed certificate...
3034 if (!make_certificate(con
))
3039 * Create the SSL context and accept the connection...
3042 context
= SSL_CTX_new(SSLv23_server_method());
3044 SSL_CTX_set_options(context
, SSL_OP_NO_SSLv2
); /* Only use SSLv3 or TLS */
3045 SSL_CTX_use_PrivateKey_file(context
, ServerKey
, SSL_FILETYPE_PEM
);
3046 SSL_CTX_use_certificate_file(context
, ServerCertificate
, SSL_FILETYPE_PEM
);
3048 bio
= BIO_new(_httpBIOMethods());
3049 BIO_ctrl(bio
, BIO_C_SET_FILE_PTR
, 0, (char *)HTTP(con
));
3051 conn
= SSL_new(context
);
3052 SSL_set_bio(conn
, bio
, bio
);
3054 if (SSL_accept(conn
) != 1)
3056 cupsdLogMessage(CUPSD_LOG_ERROR
,
3057 "encrypt_client: Unable to encrypt connection from %s!",
3058 con
->http
.hostname
);
3060 while ((error
= ERR_get_error()) != 0)
3061 cupsdLogMessage(CUPSD_LOG_ERROR
, "encrypt_client: %s",
3062 ERR_error_string(error
, NULL
));
3064 SSL_CTX_free(context
);
3069 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3070 "encrypt_client: %d Connection from %s now encrypted.",
3071 con
->http
.fd
, con
->http
.hostname
);
3073 con
->http
.tls
= conn
;
3076 # elif defined(HAVE_GNUTLS)
3077 http_tls_t
*conn
; /* TLS session object */
3078 int error
; /* Error code */
3079 gnutls_certificate_server_credentials
*credentials
;
3080 /* TLS credentials */
3084 * Verify that we have a certificate...
3087 if (access(ServerKey
, 0) || access(ServerCertificate
, 0))
3090 * Nope, make a self-signed certificate...
3093 if (!make_certificate(con
))
3098 * Create the SSL object and perform the SSL handshake...
3101 conn
= (http_tls_t
*)malloc(sizeof(http_tls_t
));
3106 credentials
= (gnutls_certificate_server_credentials
*)
3107 malloc(sizeof(gnutls_certificate_server_credentials
));
3108 if (credentials
== NULL
)
3110 cupsdLogMessage(CUPSD_LOG_ERROR
,
3111 "encrypt_client: Unable to encrypt connection from %s!",
3112 con
->http
.hostname
);
3113 cupsdLogMessage(CUPSD_LOG_ERROR
, "encrypt_client: %s", strerror(errno
));
3119 gnutls_certificate_allocate_credentials(credentials
);
3120 gnutls_certificate_set_x509_key_file(*credentials
, ServerCertificate
,
3121 ServerKey
, GNUTLS_X509_FMT_PEM
);
3123 gnutls_init(&(conn
->session
), GNUTLS_SERVER
);
3124 gnutls_set_default_priority(conn
->session
);
3125 gnutls_credentials_set(conn
->session
, GNUTLS_CRD_CERTIFICATE
, *credentials
);
3126 gnutls_transport_set_ptr(conn
->session
, (gnutls_transport_ptr
)HTTP(con
));
3127 gnutls_transport_set_pull_function(conn
->session
, _httpReadGNUTLS
);
3128 gnutls_transport_set_push_function(conn
->session
, _httpWriteGNUTLS
);
3130 error
= gnutls_handshake(conn
->session
);
3132 if (error
!= GNUTLS_E_SUCCESS
)
3134 cupsdLogMessage(CUPSD_LOG_ERROR
,
3135 "encrypt_client: Unable to encrypt connection from %s!",
3136 con
->http
.hostname
);
3137 cupsdLogMessage(CUPSD_LOG_ERROR
, "encrypt_client: %s",
3138 gnutls_strerror(error
));
3140 gnutls_deinit(conn
->session
);
3141 gnutls_certificate_free_credentials(*credentials
);
3147 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3148 "encrypt_client: %d Connection from %s now encrypted.",
3149 con
->http
.fd
, con
->http
.hostname
);
3151 conn
->credentials
= credentials
;
3152 con
->http
.tls
= conn
;
3155 # elif defined(HAVE_CDSASSL)
3156 OSStatus error
; /* Error code */
3157 http_tls_t
*conn
; /* CDSA connection information */
3160 if ((conn
= (http_tls_t
*)malloc(sizeof(http_tls_t
))) == NULL
)
3164 conn
->session
= NULL
;
3165 conn
->certsArray
= get_cdsa_certificate(con
);
3167 if (!conn
->certsArray
)
3170 * No keychain (yet), make a self-signed certificate...
3173 if (make_certificate(con
))
3174 conn
->certsArray
= get_cdsa_certificate(con
);
3177 if (!conn
->certsArray
)
3179 cupsdLogMessage(CUPSD_LOG_ERROR
,
3180 "encrypt_client: Could not find signing key in keychain "
3181 "\"%s\"", ServerCertificate
);
3182 error
= errSSLBadCert
; /* errSSLBadConfiguration is a better choice, but not available on 10.2.x */
3186 error
= SSLNewContext(true, &conn
->session
);
3189 error
= SSLSetIOFuncs(conn
->session
, _httpReadCDSA
, _httpWriteCDSA
);
3192 error
= SSLSetProtocolVersionEnabled(conn
->session
, kSSLProtocol2
, false);
3195 error
= SSLSetConnection(conn
->session
, HTTP(con
));
3198 error
= SSLSetAllowsExpiredCerts(conn
->session
, true);
3201 error
= SSLSetAllowsAnyRoot(conn
->session
, true);
3204 error
= SSLSetCertificate(conn
->session
, conn
->certsArray
);
3209 * Perform SSL/TLS handshake
3212 while ((error
= SSLHandshake(conn
->session
)) == errSSLWouldBlock
)
3218 cupsdLogMessage(CUPSD_LOG_ERROR
,
3219 "encrypt_client: Unable to encrypt connection from %s!",
3220 con
->http
.hostname
);
3222 cupsdLogMessage(CUPSD_LOG_ERROR
, "encrypt_client: %s (%d)",
3223 cssmErrorString(error
), (int)error
);
3225 con
->http
.error
= error
;
3226 con
->http
.status
= HTTP_ERROR
;
3229 SSLDisposeContext(conn
->session
);
3231 if (conn
->certsArray
)
3232 CFRelease(conn
->certsArray
);
3239 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3240 "encrypt_client: %d Connection from %s now encrypted.",
3241 con
->http
.fd
, con
->http
.hostname
);
3243 con
->http
.tls
= conn
;
3246 # endif /* HAVE_LIBSSL */
3248 #endif /* HAVE_SSL */
3253 * 'get_cdsa_certificate()' - Get a SSL/TLS certificate from the System keychain.
3256 static CFArrayRef
/* O - Array of certificates */
3257 get_cdsa_certificate(cupsd_client_t
*con
) /* I - Client connection */
3259 OSStatus err
; /* Error info */
3260 SecKeychainRef keychain
; /* Keychain reference */
3261 SecIdentitySearchRef search
; /* Search reference */
3262 SecIdentityRef identity
; /* Identity */
3263 CFArrayRef certificates
= NULL
;
3264 /* Certificate array */
3267 if ((err
= SecKeychainOpen(ServerCertificate
, &keychain
)))
3269 cupsdLogMessage(CUPSD_LOG_ERROR
, "Cannot open keychain \"%s\", %s",
3270 ServerCertificate
, cssmErrorString(err
));
3274 # if HAVE_SECIDENTITYSEARCHCREATEWITHPOLICY
3276 * Use a policy to search for valid certificates who's common name matches the
3280 SecPolicySearchRef policy_search
; /* Policy search ref */
3281 SecPolicyRef policy
; /* Policy ref */
3282 CSSM_DATA options
; /* Policy options */
3283 CSSM_APPLE_TP_SSL_OPTIONS
3284 ssl_options
; /* SSL Option for hostname */
3287 if (SecPolicySearchCreate(CSSM_CERT_X_509v3
, &CSSMOID_APPLE_TP_SSL
,
3288 NULL
, &policy_search
))
3290 cupsdLogMessage(CUPSD_LOG_ERROR
, "Cannot create a policy search reference");
3291 CFRelease(keychain
);
3295 if (SecPolicySearchCopyNext(policy_search
, &policy
))
3297 cupsdLogMessage(CUPSD_LOG_ERROR
,
3298 "Cannot find a policy to use for searching");
3299 CFRelease(keychain
);
3300 CFRelease(policy_search
);
3304 memset(&ssl_options
, 0, sizeof(ssl_options
));
3305 ssl_options
.Version
= CSSM_APPLE_TP_SSL_OPTS_VERSION
;
3306 ssl_options
.ServerName
= con
->servername
;
3307 ssl_options
.ServerNameLen
= strlen(con
->servername
);
3309 options
.Data
= (uint8
*)&ssl_options
;
3310 options
.Length
= sizeof(ssl_options
);
3312 if (SecPolicySetValue(policy
, &options
))
3314 cupsdLogMessage(CUPSD_LOG_ERROR
,
3315 "Cannot set policy value to use for searching");
3316 CFRelease(keychain
);
3317 CFRelease(policy_search
);
3321 err
= SecIdentitySearchCreateWithPolicy(policy
, NULL
, CSSM_KEYUSE_SIGN
,
3322 keychain
, FALSE
, &search
);
3325 * Assume there is exactly one SecIdentity in the keychain...
3328 err
= SecIdentitySearchCreate(keychain
, CSSM_KEYUSE_SIGN
, &search
);
3329 # endif /* HAVE_SECIDENTITYSEARCHCREATEWITHPOLICY */
3332 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3333 "Cannot create keychain search reference: %s",
3334 cssmErrorString(err
));
3337 if ((err
= SecIdentitySearchCopyNext(search
, &identity
)))
3339 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3340 "Cannot find signing key in keychain \"%s\", error %d",
3341 ServerCertificate
, (int)err
);
3345 if (CFGetTypeID(identity
) != SecIdentityGetTypeID())
3346 cupsdLogMessage(CUPSD_LOG_ERROR
,
3347 "SecIdentitySearchCopyNext CFTypeID failure!");
3350 if ((certificates
= CFArrayCreate(NULL
, (const void **)&identity
,
3351 1, &kCFTypeArrayCallBacks
)) == NULL
)
3352 cupsdLogMessage(CUPSD_LOG_ERROR
, "Cannot create certificate array");
3355 CFRelease(identity
);
3361 # if HAVE_SECIDENTITYSEARCHCREATEWITHPOLICY
3363 CFRelease(policy_search
);
3364 # endif /* HAVE_SECIDENTITYSEARCHCREATEWITHPOLICY */
3366 return (certificates
);
3368 #endif /* HAVE_CDSASSL */
3372 * 'get_file()' - Get a filename and state info.
3375 static char * /* O - Real filename */
3376 get_file(cupsd_client_t
*con
, /* I - Client connection */
3377 struct stat
*filestats
, /* O - File information */
3378 char *filename
, /* IO - Filename buffer */
3379 int len
) /* I - Buffer length */
3381 int status
; /* Status of filesystem calls */
3382 char *ptr
; /* Pointer info filename */
3383 int plen
; /* Remaining length after pointer */
3384 char language
[7]; /* Language subdirectory, if any */
3388 * Figure out the real filename...
3393 if (!strncmp(con
->uri
, "/ppd/", 5))
3394 snprintf(filename
, len
, "%s%s", ServerRoot
, con
->uri
);
3395 else if (!strncmp(con
->uri
, "/rss/", 5) && !strchr(con
->uri
+ 5, '/'))
3396 snprintf(filename
, len
, "%s/rss/%s", CacheDir
, con
->uri
+ 5);
3397 else if (!strncmp(con
->uri
, "/admin/conf/", 12))
3398 snprintf(filename
, len
, "%s%s", ServerRoot
, con
->uri
+ 11);
3399 else if (!strncmp(con
->uri
, "/admin/log/", 11))
3401 if (!strncmp(con
->uri
+ 11, "access_log", 10) && AccessLog
[0] == '/')
3402 strlcpy(filename
, AccessLog
, len
);
3403 else if (!strncmp(con
->uri
+ 11, "error_log", 9) && ErrorLog
[0] == '/')
3404 strlcpy(filename
, ErrorLog
, len
);
3405 else if (!strncmp(con
->uri
+ 11, "page_log", 8) && PageLog
[0] == '/')
3406 strlcpy(filename
, PageLog
, len
);
3410 else if (con
->language
)
3412 snprintf(language
, sizeof(language
), "/%s", con
->language
->language
);
3413 snprintf(filename
, len
, "%s%s%s", DocumentRoot
, language
, con
->uri
);
3416 snprintf(filename
, len
, "%s%s", DocumentRoot
, con
->uri
);
3418 if ((ptr
= strchr(filename
, '?')) != NULL
)
3422 * Grab the status for this language; if there isn't a language-specific file
3423 * then fallback to the default one...
3426 if ((status
= stat(filename
, filestats
)) != 0 && language
[0] &&
3427 strncmp(con
->uri
, "/ppd/", 5) &&
3428 strncmp(con
->uri
, "/admin/conf/", 12) &&
3429 strncmp(con
->uri
, "/admin/log/", 11))
3432 * Drop the country code...
3436 snprintf(filename
, len
, "%s%s%s", DocumentRoot
, language
, con
->uri
);
3438 if ((ptr
= strchr(filename
, '?')) != NULL
)
3441 if ((status
= stat(filename
, filestats
)) != 0)
3444 * Drop the language prefix and try the root directory...
3448 snprintf(filename
, len
, "%s%s", DocumentRoot
, con
->uri
);
3450 if ((ptr
= strchr(filename
, '?')) != NULL
)
3453 status
= stat(filename
, filestats
);
3458 * If we're found a directory, get the index.html file instead...
3461 if (!status
&& S_ISDIR(filestats
->st_mode
))
3464 * Make sure the URI ends with a slash...
3467 if (con
->uri
[strlen(con
->uri
) - 1] != '/')
3468 strlcat(con
->uri
, "/", sizeof(con
->uri
));
3471 * Find the directory index file, trying every language...
3476 if (status
&& language
[0])
3479 * Try a different language subset...
3483 language
[0] = '\0'; /* Strip country code */
3485 language
[0] = '\0'; /* Strip language */
3489 * Look for the index file...
3492 snprintf(filename
, len
, "%s%s%s", DocumentRoot
, language
, con
->uri
);
3494 if ((ptr
= strchr(filename
, '?')) != NULL
)
3497 ptr
= filename
+ strlen(filename
);
3498 plen
= len
- (ptr
- filename
);
3500 strlcpy(ptr
, "index.html", plen
);
3501 status
= stat(filename
, filestats
);
3506 strlcpy(ptr
, "index.class", plen
);
3507 status
= stat(filename
, filestats
);
3509 #endif /* HAVE_JAVA */
3514 strlcpy(ptr
, "index.pl", plen
);
3515 status
= stat(filename
, filestats
);
3517 #endif /* HAVE_PERL */
3522 strlcpy(ptr
, "index.php", plen
);
3523 status
= stat(filename
, filestats
);
3525 #endif /* HAVE_PHP */
3530 strlcpy(ptr
, "index.pyc", plen
);
3531 status
= stat(filename
, filestats
);
3536 strlcpy(ptr
, "index.py", plen
);
3537 status
= stat(filename
, filestats
);
3539 #endif /* HAVE_PYTHON */
3542 while (status
&& language
[0]);
3545 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_file: %d filename=%s size=%d",
3546 con
->http
.fd
, filename
,
3547 status
? -1 : (int)filestats
->st_size
);
3550 con
->http
.data_remaining
= (int)filestats
->st_size
;
3560 * 'install_conf_file()' - Install a configuration file.
3563 static http_status_t
/* O - Status */
3564 install_conf_file(cupsd_client_t
*con
) /* I - Connection */
3566 cups_file_t
*in
, /* Input file */
3567 *out
; /* Output file */
3568 char buffer
[1024]; /* Copy buffer */
3569 int bytes
; /* Number of bytes */
3570 char conffile
[1024], /* Configuration filename */
3571 newfile
[1024], /* New config filename */
3572 oldfile
[1024]; /* Old config filename */
3573 struct stat confinfo
; /* Config file info */
3577 * First construct the filenames...
3580 snprintf(conffile
, sizeof(conffile
), "%s%s", ServerRoot
, con
->uri
+ 11);
3581 snprintf(newfile
, sizeof(newfile
), "%s%s.N", ServerRoot
, con
->uri
+ 11);
3582 snprintf(oldfile
, sizeof(oldfile
), "%s%s.O", ServerRoot
, con
->uri
+ 11);
3584 cupsdLogMessage(CUPSD_LOG_INFO
, "Installing config file \"%s\"...", conffile
);
3587 * Get the owner, group, and permissions of the configuration file.
3588 * If it doesn't exist, assign it to the User and Group in the
3589 * cupsd.conf file with mode 0640 permissions.
3592 if (stat(conffile
, &confinfo
))
3594 confinfo
.st_uid
= User
;
3595 confinfo
.st_gid
= Group
;
3596 confinfo
.st_mode
= ConfigFilePerm
;
3600 * Open the request file and new config file...
3603 if ((in
= cupsFileOpen(con
->filename
, "rb")) == NULL
)
3605 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to open request file \"%s\" - %s",
3606 con
->filename
, strerror(errno
));
3607 return (HTTP_SERVER_ERROR
);
3610 if ((out
= cupsFileOpen(newfile
, "wb")) == NULL
)
3613 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to open config file \"%s\" - %s",
3614 newfile
, strerror(errno
));
3615 return (HTTP_SERVER_ERROR
);
3618 fchmod(cupsFileNumber(out
), confinfo
.st_mode
);
3619 fchown(cupsFileNumber(out
), confinfo
.st_uid
, confinfo
.st_gid
);
3622 * Copy from the request to the new config file...
3625 while ((bytes
= cupsFileRead(in
, buffer
, sizeof(buffer
))) > 0)
3626 if (cupsFileWrite(out
, buffer
, bytes
) < bytes
)
3628 cupsdLogMessage(CUPSD_LOG_ERROR
,
3629 "Unable to copy to config file \"%s\" - %s",
3630 newfile
, strerror(errno
));
3636 return (HTTP_SERVER_ERROR
);
3640 * Close the files...
3644 if (cupsFileClose(out
))
3646 cupsdLogMessage(CUPSD_LOG_ERROR
,
3647 "Error file closing config file \"%s\" - %s",
3648 newfile
, strerror(errno
));
3652 return (HTTP_SERVER_ERROR
);
3656 * Remove the request file...
3659 unlink(con
->filename
);
3660 cupsdClearString(&con
->filename
);
3663 * Unlink the old backup, rename the current config file to the backup
3664 * filename, and rename the new config file to the config file name...
3667 if (unlink(oldfile
))
3668 if (errno
!= ENOENT
)
3670 cupsdLogMessage(CUPSD_LOG_ERROR
,
3671 "Unable to remove backup config file \"%s\" - %s",
3672 oldfile
, strerror(errno
));
3676 return (HTTP_SERVER_ERROR
);
3679 if (rename(conffile
, oldfile
))
3680 if (errno
!= ENOENT
)
3682 cupsdLogMessage(CUPSD_LOG_ERROR
,
3683 "Unable to rename old config file \"%s\" - %s",
3684 conffile
, strerror(errno
));
3688 return (HTTP_SERVER_ERROR
);
3691 if (rename(newfile
, conffile
))
3693 cupsdLogMessage(CUPSD_LOG_ERROR
,
3694 "Unable to rename new config file \"%s\" - %s",
3695 newfile
, strerror(errno
));
3697 rename(oldfile
, conffile
);
3700 return (HTTP_SERVER_ERROR
);
3704 * If the cupsd.conf file was updated, set the NeedReload flag...
3707 if (!strcmp(con
->uri
, "/admin/conf/cupsd.conf"))
3708 NeedReload
= RELOAD_CUPSD
;
3710 NeedReload
= RELOAD_ALL
;
3712 ReloadTime
= time(NULL
);
3715 * Return that the file was created successfully...
3718 return (HTTP_CREATED
);
3723 * 'is_cgi()' - Is the resource a CGI script/program?
3726 static int /* O - 1 = CGI, 0 = file */
3727 is_cgi(cupsd_client_t
*con
, /* I - Client connection */
3728 const char *filename
, /* I - Real filename */
3729 struct stat
*filestats
, /* I - File information */
3730 mime_type_t
*type
) /* I - MIME type */
3732 const char *options
; /* Options on URL */
3735 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3736 "is_cgi(con=%p, filename=\"%s\", filestats=%p, type=%s/%s)",
3737 con
, filename
, filestats
, type
? type
->super
: "unknown",
3738 type
? type
->type
: "unknown");
3741 * Get the options, if any...
3744 if ((options
= strchr(con
->uri
, '?')) != NULL
)
3747 cupsdSetStringf(&(con
->query_string
), "QUERY_STRING=%s", options
);
3751 * Check for known types...
3754 if (!type
|| strcasecmp(type
->super
, "application"))
3756 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "is_cgi: Returning 0...");
3760 if (!strcasecmp(type
->type
, "x-httpd-cgi") &&
3761 (filestats
->st_mode
& 0111))
3764 * "application/x-httpd-cgi" is a CGI script.
3767 cupsdSetString(&con
->command
, filename
);
3770 cupsdSetStringf(&con
->options
, " %s", options
);
3772 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3773 "is_cgi: Returning 1 with command=\"%s\" and options=\"%s\"",
3774 con
->command
, con
->options
);
3779 else if (!strcasecmp(type
->type
, "x-httpd-java"))
3782 * "application/x-httpd-java" is a Java servlet.
3785 cupsdSetString(&con
->command
, CUPS_JAVA
);
3788 cupsdSetStringf(&con
->options
, " %s %s", filename
, options
);
3790 cupsdSetStringf(&con
->options
, " %s", filename
);
3792 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3793 "is_cgi: Returning 1 with command=\"%s\" and options=\"%s\"",
3794 con
->command
, con
->options
);
3798 #endif /* HAVE_JAVA */
3800 else if (!strcasecmp(type
->type
, "x-httpd-perl"))
3803 * "application/x-httpd-perl" is a Perl page.
3806 cupsdSetString(&con
->command
, CUPS_PERL
);
3809 cupsdSetStringf(&con
->options
, " %s %s", filename
, options
);
3811 cupsdSetStringf(&con
->options
, " %s", filename
);
3813 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3814 "is_cgi: Returning 1 with command=\"%s\" and options=\"%s\"",
3815 con
->command
, con
->options
);
3819 #endif /* HAVE_PERL */
3821 else if (!strcasecmp(type
->type
, "x-httpd-php"))
3824 * "application/x-httpd-php" is a PHP page.
3827 cupsdSetString(&con
->command
, CUPS_PHP
);
3830 cupsdSetStringf(&con
->options
, " %s %s", filename
, options
);
3832 cupsdSetStringf(&con
->options
, " %s", filename
);
3834 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3835 "is_cgi: Returning 1 with command=\"%s\" and options=\"%s\"",
3836 con
->command
, con
->options
);
3840 #endif /* HAVE_PHP */
3842 else if (!strcasecmp(type
->type
, "x-httpd-python"))
3845 * "application/x-httpd-python" is a Python page.
3848 cupsdSetString(&con
->command
, CUPS_PYTHON
);
3851 cupsdSetStringf(&con
->options
, " %s %s", filename
, options
);
3853 cupsdSetStringf(&con
->options
, " %s", filename
);
3855 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3856 "is_cgi: Returning 1 with command=\"%s\" and options=\"%s\"",
3857 con
->command
, con
->options
);
3861 #endif /* HAVE_PYTHON */
3863 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "is_cgi: Returning 0...");
3870 * 'is_path_absolute()' - Is a path absolute and free of relative elements (i.e. "..").
3873 static int /* O - 0 if relative, 1 if absolute */
3874 is_path_absolute(const char *path
) /* I - Input path */
3877 * Check for a leading slash...
3884 * Check for "/.." in the path...
3887 while ((path
= strstr(path
, "/..")) != NULL
)
3889 if (!path
[3] || path
[3] == '/')
3896 * If we haven't found any relative paths, return 1 indicating an
3906 * 'make_certificate()' - Make a self-signed SSL/TLS certificate.
3909 static int /* O - 1 on success, 0 on failure */
3910 make_certificate(cupsd_client_t
*con
) /* I - Client connection */
3912 #if defined(HAVE_LIBSSL) && defined(HAVE_WAITPID)
3913 int pid
, /* Process ID of command */
3914 status
; /* Status of command */
3915 char command
[1024], /* Command */
3916 *argv
[12], /* Command-line arguments */
3917 *envp
[MAX_ENV
+ 1], /* Environment variables */
3918 home
[1024], /* HOME environment variable */
3919 infofile
[1024], /* Type-in information for cert */
3920 seedfile
[1024]; /* Random number seed file */
3921 int envc
, /* Number of environment variables */
3922 bytes
; /* Bytes written */
3923 cups_file_t
*fp
; /* Seed/info file */
3924 int infofd
; /* Info file descriptor */
3928 * Run the "openssl" command to seed the random number generator and
3929 * generate a self-signed certificate that is good for 10 years:
3931 * openssl rand -rand seedfile 1
3933 * openssl req -new -x509 -keyout ServerKey \
3934 * -out ServerCertificate -days 3650 -nodes
3936 * The seeding step is crucial in ensuring that the openssl command
3937 * does not block on systems without sufficient entropy...
3940 if (!cupsFileFind("openssl", getenv("PATH"), 1, command
, sizeof(command
)))
3942 cupsdLogMessage(CUPSD_LOG_ERROR
,
3943 "No SSL certificate and openssl command not found!");
3947 if (access("/dev/urandom", 0))
3950 * If the system doesn't provide /dev/urandom, then any random source
3951 * will probably be blocking-style, so generate some random data to
3952 * use as a seed for the certificate. Note that we have already
3953 * seeded the random number generator in cupsdInitCerts()...
3956 cupsdLogMessage(CUPSD_LOG_INFO
,
3957 "Seeding the random number generator...");
3959 snprintf(home
, sizeof(home
), "HOME=%s", TempDir
);
3962 * Write the seed file...
3965 if ((fp
= cupsTempFile2(seedfile
, sizeof(seedfile
))) == NULL
)
3967 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to create seed file %s - %s",
3968 seedfile
, strerror(errno
));
3972 for (bytes
= 0; bytes
< 262144; bytes
++)
3973 cupsFilePutChar(fp
, random());
3978 * Run the openssl command to seed its random number generator...
3981 argv
[0] = "openssl";
3988 envc
= cupsdLoadEnv(envp
, MAX_ENV
);
3989 envp
[envc
++] = home
;
3992 if (!cupsdStartProcess(command
, argv
, envp
, -1, -1, -1, -1, -1, 1, NULL
,
3999 while (waitpid(pid
, &status
, 0) < 0)
4006 cupsdFinishProcess(pid
, command
, sizeof(command
));
4009 * Remove the seed file, as it is no longer needed...
4016 if (WIFEXITED(status
))
4017 cupsdLogMessage(CUPSD_LOG_ERROR
,
4018 "Unable to seed random number generator - "
4019 "the openssl command stopped with status %d!",
4020 WEXITSTATUS(status
));
4022 cupsdLogMessage(CUPSD_LOG_ERROR
,
4023 "Unable to seed random number generator - "
4024 "the openssl command crashed on signal %d!",
4032 * Create a file with the certificate information fields...
4034 * Note: This assumes that the default questions are asked by the openssl
4038 if ((fp
= cupsTempFile2(infofile
, sizeof(infofile
))) == NULL
)
4040 cupsdLogMessage(CUPSD_LOG_ERROR
,
4041 "Unable to create certificate information file %s - %s",
4042 infofile
, strerror(errno
));
4046 cupsFilePrintf(fp
, ".\n.\n.\n%s\n.\n%s\n%s\n",
4047 ServerName
, ServerName
, ServerAdmin
);
4050 cupsdLogMessage(CUPSD_LOG_INFO
,
4051 "Generating SSL server key and certificate...");
4053 argv
[0] = "openssl";
4057 argv
[4] = "-keyout";
4058 argv
[5] = ServerKey
;
4060 argv
[7] = ServerCertificate
;
4063 argv
[10] = "-nodes";
4066 cupsdLoadEnv(envp
, MAX_ENV
);
4068 infofd
= open(infofile
, O_RDONLY
);
4070 if (!cupsdStartProcess(command
, argv
, envp
, infofd
, -1, -1, -1, -1, 1, NULL
,
4081 while (waitpid(pid
, &status
, 0) < 0)
4088 cupsdFinishProcess(pid
, command
, sizeof(command
));
4092 if (WIFEXITED(status
))
4093 cupsdLogMessage(CUPSD_LOG_ERROR
,
4094 "Unable to create SSL server key and certificate - "
4095 "the openssl command stopped with status %d!",
4096 WEXITSTATUS(status
));
4098 cupsdLogMessage(CUPSD_LOG_ERROR
,
4099 "Unable to create SSL server key and certificate - "
4100 "the openssl command crashed on signal %d!",
4105 cupsdLogMessage(CUPSD_LOG_INFO
, "Created SSL server key file \"%s\"...",
4107 cupsdLogMessage(CUPSD_LOG_INFO
,
4108 "Created SSL server certificate file \"%s\"...",
4114 #elif defined(HAVE_GNUTLS)
4115 gnutls_x509_crt crt
; /* Self-signed certificate */
4116 gnutls_x509_privkey key
; /* Encryption key */
4117 cups_lang_t
*language
; /* Default language info */
4118 cups_file_t
*fp
; /* Key/cert file */
4119 unsigned char buffer
[8192]; /* Buffer for x509 data */
4120 size_t bytes
; /* Number of bytes of data */
4121 unsigned char serial
[4]; /* Serial number buffer */
4122 time_t curtime
; /* Current time */
4123 int result
; /* Result of GNU TLS calls */
4127 * Create the encryption key...
4130 cupsdLogMessage(CUPSD_LOG_INFO
, "Generating SSL server key...");
4132 gnutls_x509_privkey_init(&key
);
4133 gnutls_x509_privkey_generate(key
, GNUTLS_PK_RSA
, 2048, 0);
4139 bytes
= sizeof(buffer
);
4141 if ((result
= gnutls_x509_privkey_export(key
, GNUTLS_X509_FMT_PEM
,
4142 buffer
, &bytes
)) < 0)
4144 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to export SSL server key - %s",
4145 gnutls_strerror(result
));
4146 gnutls_x509_privkey_deinit(key
);
4149 else if ((fp
= cupsFileOpen(ServerKey
, "w")) != NULL
)
4151 cupsFileWrite(fp
, (char *)buffer
, bytes
);
4154 cupsdLogMessage(CUPSD_LOG_INFO
, "Created SSL server key file \"%s\"...",
4159 cupsdLogMessage(CUPSD_LOG_ERROR
,
4160 "Unable to create SSL server key file \"%s\" - %s",
4161 ServerKey
, strerror(errno
));
4162 gnutls_x509_privkey_deinit(key
);
4167 * Create the self-signed certificate...
4170 cupsdLogMessage(CUPSD_LOG_INFO
, "Generating self-signed SSL certificate...");
4172 language
= cupsLangDefault();
4173 curtime
= time(NULL
);
4174 serial
[0] = curtime
>> 24;
4175 serial
[1] = curtime
>> 16;
4176 serial
[2] = curtime
>> 8;
4177 serial
[3] = curtime
;
4179 gnutls_x509_crt_init(&crt
);
4180 if (strlen(language
->language
) == 5)
4181 gnutls_x509_crt_set_dn_by_oid(crt
, GNUTLS_OID_X520_COUNTRY_NAME
, 0,
4182 language
->language
+ 3, 2);
4184 gnutls_x509_crt_set_dn_by_oid(crt
, GNUTLS_OID_X520_COUNTRY_NAME
, 0,
4186 gnutls_x509_crt_set_dn_by_oid(crt
, GNUTLS_OID_X520_COMMON_NAME
, 0,
4187 ServerName
, strlen(ServerName
));
4188 gnutls_x509_crt_set_dn_by_oid(crt
, GNUTLS_OID_X520_ORGANIZATION_NAME
, 0,
4189 ServerName
, strlen(ServerName
));
4190 gnutls_x509_crt_set_dn_by_oid(crt
, GNUTLS_OID_X520_ORGANIZATIONAL_UNIT_NAME
,
4192 gnutls_x509_crt_set_dn_by_oid(crt
, GNUTLS_OID_X520_STATE_OR_PROVINCE_NAME
, 0,
4194 gnutls_x509_crt_set_dn_by_oid(crt
, GNUTLS_OID_X520_LOCALITY_NAME
, 0,
4196 gnutls_x509_crt_set_dn_by_oid(crt
, GNUTLS_OID_PKCS9_EMAIL
, 0,
4197 ServerAdmin
, strlen(ServerAdmin
));
4198 gnutls_x509_crt_set_key(crt
, key
);
4199 gnutls_x509_crt_set_serial(crt
, serial
, sizeof(serial
));
4200 gnutls_x509_crt_set_activation_time(crt
, curtime
);
4201 gnutls_x509_crt_set_expiration_time(crt
, curtime
+ 10 * 365 * 86400);
4202 gnutls_x509_crt_set_ca_status(crt
, 0);
4203 gnutls_x509_crt_set_subject_alternative_name(crt
, GNUTLS_SAN_DNSNAME
,
4205 gnutls_x509_crt_set_key_purpose_oid(crt
, GNUTLS_KP_TLS_WWW_SERVER
, 0);
4206 gnutls_x509_crt_set_key_usage(crt
, GNUTLS_KEY_KEY_ENCIPHERMENT
);
4207 gnutls_x509_crt_set_version(crt
, 3);
4209 bytes
= sizeof(buffer
);
4210 if (gnutls_x509_crt_get_key_id(crt
, 0, buffer
, &bytes
) >= 0)
4211 gnutls_x509_crt_set_subject_key_id(crt
, buffer
, bytes
);
4213 gnutls_x509_crt_sign(crt
, crt
, key
);
4219 bytes
= sizeof(buffer
);
4220 if ((result
= gnutls_x509_crt_export(crt
, GNUTLS_X509_FMT_PEM
,
4221 buffer
, &bytes
)) < 0)
4222 cupsdLogMessage(CUPSD_LOG_ERROR
,
4223 "Unable to export SSL server certificate - %s",
4224 gnutls_strerror(result
));
4225 else if ((fp
= cupsFileOpen(ServerCertificate
, "w")) != NULL
)
4227 cupsFileWrite(fp
, (char *)buffer
, bytes
);
4230 cupsdLogMessage(CUPSD_LOG_INFO
,
4231 "Created SSL server certificate file \"%s\"...",
4235 cupsdLogMessage(CUPSD_LOG_ERROR
,
4236 "Unable to create SSL server certificate file \"%s\" - %s",
4237 ServerCertificate
, strerror(errno
));
4243 gnutls_x509_crt_deinit(crt
);
4244 gnutls_x509_privkey_deinit(key
);
4248 #elif defined(HAVE_CDSASSL) && defined(HAVE_WAITPID)
4249 int pid
, /* Process ID of command */
4250 status
; /* Status of command */
4251 char command
[1024], /* Command */
4252 *argv
[4], /* Command-line arguments */
4253 *envp
[MAX_ENV
+ 1], /* Environment variables */
4254 keychain
[1024], /* Keychain argument */
4255 infofile
[1024]; /* Type-in information for cert */
4256 cups_file_t
*fp
; /* Seed/info file */
4257 int infofd
; /* Info file descriptor */
4261 * Run the "certtool" command to generate a self-signed certificate...
4264 if (!cupsFileFind("certtool", getenv("PATH"), 1, command
, sizeof(command
)))
4266 cupsdLogMessage(CUPSD_LOG_ERROR
,
4267 "No SSL certificate and certtool command not found!");
4272 * Create a file with the certificate information fields...
4274 * Note: This assumes that the default questions are asked by the certtool
4278 if ((fp
= cupsTempFile2(infofile
, sizeof(infofile
))) == NULL
)
4280 cupsdLogMessage(CUPSD_LOG_ERROR
,
4281 "Unable to create certificate information file %s - %s",
4282 infofile
, strerror(errno
));
4286 cupsFilePrintf(fp
, "%s\nr\n\ny\nb\ns\ny\n%s\n\n\n\n\n%s\ny\n",
4287 con
->servername
, con
->servername
, ServerAdmin
);
4290 cupsdLogMessage(CUPSD_LOG_INFO
,
4291 "Generating SSL server key and certificate...");
4293 snprintf(keychain
, sizeof(keychain
), "k=%s", ServerCertificate
);
4295 argv
[0] = "certtool";
4300 cupsdLoadEnv(envp
, MAX_ENV
);
4302 infofd
= open(infofile
, O_RDONLY
);
4304 if (!cupsdStartProcess(command
, argv
, envp
, infofd
, -1, -1, -1, -1, 1, NULL
,
4315 while (waitpid(pid
, &status
, 0) < 0)
4322 cupsdFinishProcess(pid
, command
, sizeof(command
));
4326 if (WIFEXITED(status
))
4327 cupsdLogMessage(CUPSD_LOG_ERROR
,
4328 "Unable to create SSL server key and certificate - "
4329 "the certtool command stopped with status %d!",
4330 WEXITSTATUS(status
));
4332 cupsdLogMessage(CUPSD_LOG_ERROR
,
4333 "Unable to create SSL server key and certificate - "
4334 "the certtool command crashed on signal %d!",
4339 cupsdLogMessage(CUPSD_LOG_INFO
,
4340 "Created SSL server certificate file \"%s\"...",
4348 #endif /* HAVE_LIBSSL && HAVE_WAITPID */
4350 #endif /* HAVE_SSL */
4354 * 'pipe_command()' - Pipe the output of a command to the remote client.
4357 static int /* O - Process ID */
4358 pipe_command(cupsd_client_t
*con
, /* I - Client connection */
4359 int infile
, /* I - Standard input for command */
4360 int *outfile
, /* O - Standard output for command */
4361 char *command
, /* I - Command to run */
4362 char *options
, /* I - Options for command */
4363 int root
) /* I - Run as root? */
4365 int i
; /* Looping var */
4366 int pid
; /* Process ID */
4367 char *commptr
, /* Command string pointer */
4368 commch
; /* Command string character */
4369 char *uriptr
; /* URI string pointer */
4370 int fds
[2]; /* Pipe FDs */
4371 int argc
; /* Number of arguments */
4372 int envc
; /* Number of environment variables */
4373 char argbuf
[10240], /* Argument buffer */
4374 *argv
[100], /* Argument strings */
4375 *envp
[MAX_ENV
+ 20]; /* Environment variables */
4376 char auth_type
[256], /* CUPSD_AUTH_TYPE environment variable */
4377 content_length
[1024], /* CONTENT_LENGTH environment variable */
4378 content_type
[1024], /* CONTENT_TYPE environment variable */
4379 http_cookie
[32768], /* HTTP_COOKIE environment variable */
4380 http_referer
[1024], /* HTTP_REFERER environment variable */
4381 http_user_agent
[1024], /* HTTP_USER_AGENT environment variable */
4382 lang
[1024], /* LANG environment variable */
4383 path_info
[1024], /* PATH_INFO environment variable */
4384 remote_addr
[1024], /* REMOTE_ADDR environment variable */
4385 remote_host
[1024], /* REMOTE_HOST environment variable */
4386 remote_user
[1024], /* REMOTE_USER environment variable */
4387 script_filename
[1024], /* SCRIPT_FILENAME environment variable */
4388 script_name
[1024], /* SCRIPT_NAME environment variable */
4389 server_name
[1024], /* SERVER_NAME environment variable */
4390 server_port
[1024]; /* SERVER_PORT environment variable */
4391 ipp_attribute_t
*attr
; /* attributes-natural-language attribute */
4393 krb5_ccache ccache
= NULL
; /* Kerberos credentials */
4394 char krb5ccname
[1024]; /* KRB5CCNAME environment variable */
4395 #endif /* HAVE_GSSAPI */
4399 * Parse a copy of the options string, which is of the form:
4401 * argument+argument+argument
4402 * ?argument+argument+argument
4403 * param=value¶m=value
4404 * ?param=value¶m=value
4405 * /name?argument+argument+argument
4406 * /name?param=value¶m=value
4408 * If the string contains an "=" character after the initial name,
4409 * then we treat it as a HTTP GET form request and make a copy of
4410 * the remaining string for the environment variable.
4412 * The string is always parsed out as command-line arguments, to
4413 * be consistent with Apache...
4416 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4417 "pipe_command: command=\"%s\", options=\"%s\"",
4418 command
, options
? options
: "(null)");
4423 strlcpy(argbuf
, options
, sizeof(argbuf
));
4427 if (argbuf
[0] == '/')
4430 * Found some trailing path information, set PATH_INFO...
4433 if ((commptr
= strchr(argbuf
, '?')) == NULL
)
4434 commptr
= argbuf
+ strlen(argbuf
);
4438 snprintf(path_info
, sizeof(path_info
), "PATH_INFO=%s", argbuf
);
4444 path_info
[0] = '\0';
4446 if (*commptr
== ' ')
4450 if (*commptr
== '?' && con
->operation
== HTTP_GET
&& !con
->query_string
)
4453 cupsdSetStringf(&(con
->query_string
), "QUERY_STRING=%s", commptr
);
4460 argv
[argc
++] = commptr
;
4462 for (; *commptr
&& argc
< 99; commptr
++)
4465 * Break arguments whenever we see a + or space...
4468 if (*commptr
== ' ' || *commptr
== '+')
4470 while (*commptr
== ' ' || *commptr
== '+')
4474 * If we don't have a blank string, save it as another argument...
4479 argv
[argc
] = commptr
;
4485 else if (*commptr
== '%' && isxdigit(commptr
[1] & 255) &&
4486 isxdigit(commptr
[2] & 255))
4489 * Convert the %xx notation to the individual character.
4492 if (commptr
[1] >= '0' && commptr
[1] <= '9')
4493 *commptr
= (commptr
[1] - '0') << 4;
4495 *commptr
= (tolower(commptr
[1]) - 'a' + 10) << 4;
4497 if (commptr
[2] >= '0' && commptr
[2] <= '9')
4498 *commptr
|= commptr
[2] - '0';
4500 *commptr
|= tolower(commptr
[2]) - 'a' + 10;
4502 _cups_strcpy(commptr
+ 1, commptr
+ 3);
4505 * Check for a %00 and break if that is the case...
4517 * Setup the environment variables as needed...
4520 if (con
->username
[0])
4522 snprintf(auth_type
, sizeof(auth_type
), "CUPSD_AUTH_TYPE=%s",
4523 httpGetField(HTTP(con
), HTTP_FIELD_AUTHORIZATION
));
4525 if ((uriptr
= strchr(auth_type
+ 10, ' ')) != NULL
)
4529 auth_type
[0] = '\0';
4532 (attr
= ippFindAttribute(con
->request
, "attributes-natural-language",
4533 IPP_TAG_LANGUAGE
)) != NULL
)
4535 switch (strlen(attr
->values
[0].string
.text
))
4539 * This is an unknown or badly formatted language code; use
4540 * the POSIX locale...
4543 strcpy(lang
, "LANG=C");
4548 * Just the language code (ll)...
4551 snprintf(lang
, sizeof(lang
), "LANG=%s.UTF8",
4552 attr
->values
[0].string
.text
);
4557 * Language and country code (ll-cc)...
4560 snprintf(lang
, sizeof(lang
), "LANG=%c%c_%c%c.UTF8",
4561 attr
->values
[0].string
.text
[0],
4562 attr
->values
[0].string
.text
[1],
4563 toupper(attr
->values
[0].string
.text
[3] & 255),
4564 toupper(attr
->values
[0].string
.text
[4] & 255));
4568 else if (con
->language
)
4569 snprintf(lang
, sizeof(lang
), "LANG=%s.UTF8", con
->language
->language
);
4571 strcpy(lang
, "LANG=C");
4573 strcpy(remote_addr
, "REMOTE_ADDR=");
4574 httpAddrString(con
->http
.hostaddr
, remote_addr
+ 12,
4575 sizeof(remote_addr
) - 12);
4577 snprintf(remote_host
, sizeof(remote_host
), "REMOTE_HOST=%s",
4578 con
->http
.hostname
);
4580 snprintf(script_name
, sizeof(script_name
), "SCRIPT_NAME=%s", con
->uri
);
4581 if ((uriptr
= strchr(script_name
, '?')) != NULL
)
4584 snprintf(script_filename
, sizeof(script_filename
), "SCRIPT_FILENAME=%s%s",
4585 DocumentRoot
, script_name
+ 12);
4587 sprintf(server_port
, "SERVER_PORT=%d", con
->serverport
);
4589 snprintf(server_name
, sizeof(server_name
), "SERVER_NAME=%s",
4592 envc
= cupsdLoadEnv(envp
, (int)(sizeof(envp
) / sizeof(envp
[0])));
4595 envp
[envc
++] = auth_type
;
4597 envp
[envc
++] = lang
;
4598 envp
[envc
++] = "REDIRECT_STATUS=1";
4599 envp
[envc
++] = "GATEWAY_INTERFACE=CGI/1.1";
4600 envp
[envc
++] = server_name
;
4601 envp
[envc
++] = server_port
;
4602 envp
[envc
++] = remote_addr
;
4603 envp
[envc
++] = remote_host
;
4604 envp
[envc
++] = script_name
;
4605 envp
[envc
++] = script_filename
;
4608 envp
[envc
++] = path_info
;
4610 if (con
->username
[0])
4612 snprintf(remote_user
, sizeof(remote_user
), "REMOTE_USER=%s", con
->username
);
4614 envp
[envc
++] = remote_user
;
4617 * Save Kerberos credentials, if any...
4621 if (con
->gss_have_creds
)
4623 # if !defined(HAVE_KRB5_CC_NEW_UNIQUE) && !defined(HAVE_HEIMDAL)
4624 cupsdLogMessage(CUPSD_LOG_INFO
,
4625 "Sorry, your version of Kerberos does not support "
4626 "delegated credentials!");
4629 krb5_error_code error
; /* Kerberos error code */
4630 OM_uint32 major_status
, /* Major status code */
4631 minor_status
; /* Minor status code */
4632 krb5_principal principal
; /* Kerberos principal */
4637 * If the weak-linked GSSAPI/Kerberos library is not present, don't try
4641 if (krb5_init_context
!= NULL
)
4643 # endif /* __APPLE__ */
4646 * We MUST create a file-based cache because memory-based caches are
4647 * only valid for the current process/address space.
4649 * Due to various bugs/features in different versions of Kerberos, we
4650 * need either the krb5_cc_new_unique() function or Heimdal's version
4651 * of krb5_cc_gen_new() to create a new FILE: credential cache that
4652 * can be passed to the backend. These functions create a temporary
4653 * file (typically in /tmp) containing the cached credentials, which
4654 * are removed when we have successfully printed a job.
4657 # ifdef HAVE_KRB5_CC_NEW_UNIQUE
4658 if ((error
= krb5_cc_new_unique(KerberosContext
, "FILE", NULL
,
4660 # else /* HAVE_HEIMDAL */
4661 if ((error
= krb5_cc_gen_new(KerberosContext
, &krb5_fcc_ops
,
4663 # endif /* HAVE_KRB5_CC_NEW_UNIQUE */
4665 cupsdLogMessage(CUPSD_LOG_ERROR
,
4666 "Unable to create new credentials cache (%d/%s)",
4667 error
, strerror(errno
));
4670 else if ((error
= krb5_parse_name(KerberosContext
, con
->username
,
4673 cupsdLogMessage(CUPSD_LOG_ERROR
,
4674 "Unable to parse kerberos username (%d/%s)", error
,
4676 krb5_cc_destroy(KerberosContext
, ccache
);
4679 else if ((error
= krb5_cc_initialize(KerberosContext
, ccache
,
4682 cupsdLogMessage(CUPSD_LOG_ERROR
,
4683 "Unable to initialize credentials cache (%d/%s)", error
,
4685 krb5_cc_destroy(KerberosContext
, ccache
);
4686 krb5_free_principal(KerberosContext
, principal
);
4691 krb5_free_principal(KerberosContext
, principal
);
4694 * Copy the user's credentials to the new cache file...
4697 major_status
= gss_krb5_copy_ccache(&minor_status
,
4698 con
->gss_delegated_cred
, ccache
);
4700 if (GSS_ERROR(major_status
))
4702 cupsdLogGSSMessage(CUPSD_LOG_ERROR
, major_status
, minor_status
,
4703 "Unable to import client credentials cache");
4704 krb5_cc_destroy(KerberosContext
, ccache
);
4710 * Add the KRB5CCNAME environment variable to the job so that the
4711 * backend can use the credentials when printing.
4714 snprintf(krb5ccname
, sizeof(krb5ccname
), "KRB5CCNAME=FILE:%s",
4715 krb5_cc_get_name(KerberosContext
, ccache
));
4716 envp
[envc
++] = krb5ccname
;
4719 chown(krb5_cc_get_name(KerberosContext
, ccache
), User
, Group
);
4724 # endif /* __APPLE__ */
4725 # endif /* HAVE_KRB5_CC_NEW_UNIQUE || HAVE_HEIMDAL */
4727 #endif /* HAVE_GSSAPI */
4731 if (con
->http
.version
== HTTP_1_1
)
4732 envp
[envc
++] = "SERVER_PROTOCOL=HTTP/1.1";
4733 else if (con
->http
.version
== HTTP_1_0
)
4734 envp
[envc
++] = "SERVER_PROTOCOL=HTTP/1.0";
4736 envp
[envc
++] = "SERVER_PROTOCOL=HTTP/0.9";
4738 if (con
->http
.cookie
)
4740 snprintf(http_cookie
, sizeof(http_cookie
), "HTTP_COOKIE=%s",
4742 envp
[envc
++] = http_cookie
;
4745 if (con
->http
.fields
[HTTP_FIELD_USER_AGENT
][0])
4747 snprintf(http_user_agent
, sizeof(http_user_agent
), "HTTP_USER_AGENT=%s",
4748 con
->http
.fields
[HTTP_FIELD_USER_AGENT
]);
4749 envp
[envc
++] = http_user_agent
;
4752 if (con
->http
.fields
[HTTP_FIELD_REFERER
][0])
4754 snprintf(http_referer
, sizeof(http_referer
), "HTTP_REFERER=%s",
4755 con
->http
.fields
[HTTP_FIELD_REFERER
]);
4756 envp
[envc
++] = http_referer
;
4759 if (con
->operation
== HTTP_GET
)
4761 envp
[envc
++] = "REQUEST_METHOD=GET";
4763 if (con
->query_string
)
4766 * Add GET form variables after ?...
4769 envp
[envc
++] = con
->query_string
;
4772 envp
[envc
++] = "QUERY_STRING=";
4776 sprintf(content_length
, "CONTENT_LENGTH=" CUPS_LLFMT
,
4777 CUPS_LLCAST con
->bytes
);
4778 snprintf(content_type
, sizeof(content_type
), "CONTENT_TYPE=%s",
4779 con
->http
.fields
[HTTP_FIELD_CONTENT_TYPE
]);
4781 envp
[envc
++] = "REQUEST_METHOD=POST";
4782 envp
[envc
++] = content_length
;
4783 envp
[envc
++] = content_type
;
4787 * Tell the CGI if we are using encryption...
4791 envp
[envc
++] = "HTTPS=ON";
4794 * Terminate the environment array...
4799 if (LogLevel
== CUPSD_LOG_DEBUG2
)
4801 for (i
= 0; i
< argc
; i
++)
4802 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4803 "pipe_command: argv[%d] = \"%s\"", i
, argv
[i
]);
4804 for (i
= 0; i
< envc
; i
++)
4805 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4806 "pipe_command: envp[%d] = \"%s\"", i
, envp
[i
]);
4810 * Create a pipe for the output...
4813 if (cupsdOpenPipe(fds
))
4815 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to create pipes for CGI %s - %s",
4816 argv
[0], strerror(errno
));
4821 * Then execute the command...
4824 if (cupsdStartProcess(command
, argv
, envp
, infile
, fds
[1], CGIPipes
[1],
4825 -1, -1, root
, DefaultProfile
, &pid
) < 0)
4828 * Error - can't fork!
4831 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to fork for CGI %s - %s", argv
[0],
4834 cupsdClosePipe(fds
);
4840 * Fork successful - return the PID...
4843 if (con
->username
[0])
4845 cupsdAddCert(pid
, con
->username
, ccache
);
4847 cupsdAddCert(pid
, con
->username
, NULL
);
4848 #endif /* HAVE_GSSAPI */
4850 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[CGI] %s started - PID = %d",
4862 * 'write_file()' - Send a file via HTTP.
4865 static int /* O - 0 on failure, 1 on success */
4866 write_file(cupsd_client_t
*con
, /* I - Client connection */
4867 http_status_t code
, /* I - HTTP status */
4868 char *filename
, /* I - Filename */
4869 char *type
, /* I - File type */
4870 struct stat
*filestats
) /* O - File information */
4872 con
->file
= open(filename
, O_RDONLY
);
4874 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "write_file: %d file=%d", con
->http
.fd
,
4880 fcntl(con
->file
, F_SETFD
, fcntl(con
->file
, F_GETFD
) | FD_CLOEXEC
);
4884 if (!cupsdSendHeader(con
, code
, type
, CUPSD_AUTH_NONE
))
4887 if (httpPrintf(HTTP(con
), "Last-Modified: %s\r\n",
4888 httpGetDateString(filestats
->st_mtime
)) < 0)
4890 if (httpPrintf(HTTP(con
), "Content-Length: " CUPS_LLFMT
"\r\n",
4891 CUPS_LLCAST filestats
->st_size
) < 0)
4893 if (httpPrintf(HTTP(con
), "\r\n") < 0)
4896 if (cupsdFlushHeader(con
) < 0)
4899 con
->http
.data_encoding
= HTTP_ENCODE_LENGTH
;
4900 con
->http
.data_remaining
= filestats
->st_size
;
4902 if (con
->http
.data_remaining
<= INT_MAX
)
4903 con
->http
._data_remaining
= con
->http
.data_remaining
;
4905 con
->http
._data_remaining
= INT_MAX
;
4907 cupsdAddSelect(con
->http
.fd
, (cupsd_selfunc_t
)cupsdReadClient
,
4908 (cupsd_selfunc_t
)cupsdWriteClient
, con
);
4915 * 'write_pipe()' - Flag that data is available on the CGI pipe.
4919 write_pipe(cupsd_client_t
*con
) /* I - Client connection */
4921 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "write_pipe: CGI output on fd %d...",
4924 con
->file_ready
= 1;
4926 cupsdRemoveSelect(con
->file
);
4927 cupsdAddSelect(con
->http
.fd
, NULL
, (cupsd_selfunc_t
)cupsdWriteClient
, con
);
4932 * End of "$Id: client.c 7673 2008-06-18 22:31:26Z mike $".