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 * data_ready() - Check whether data is available from a client.
33 * encrypt_client() - Enable encryption for the client...
34 * get_cdsa_certificate() - Convert a keychain name into the CFArrayRef
35 * required by SSLSetCertificate.
36 * get_file() - Get a filename and state info.
37 * install_conf_file() - Install a configuration file.
38 * is_cgi() - Is the resource a CGI script/program?
39 * is_path_absolute() - Is a path absolute and free of relative elements.
40 * make_certificate() - Make a self-signed SSL/TLS certificate.
41 * pipe_command() - Pipe the output of a command to the remote client.
42 * write_file() - Send a file via HTTP.
43 * write_pipe() - Flag that data is available on the CGI pipe.
47 * Include necessary headers...
53 # include <Security/Security.h>
54 # ifdef HAVE_SECIDENTITYSEARCHPRIV_H
55 # include <Security/SecIdentitySearchPriv.h>
56 # else /* Declare prototype for function in that header... */
57 extern OSStatus
SecIdentitySearchCreateWithPolicy(SecPolicyRef policy
,
58 CFStringRef idString
, CSSM_KEYUSE keyUsage
,
59 CFTypeRef keychainOrArray
,
60 Boolean returnOnlyValidIdentities
,
61 SecIdentitySearchRef
* searchRef
);
62 # endif /* HAVE_SECIDENTITYSEARCHPRIV_H */
63 # ifdef HAVE_SECPOLICYPRIV_H
64 # include <Security/SecPolicyPriv.h>
65 # else /* Declare prototype for function in that header... */
66 extern OSStatus
SecPolicySetValue(SecPolicyRef policyRef
,
67 const CSSM_DATA
*value
);
68 # endif /* HAVE_SECPOLICYPRIV_H */
69 # ifdef HAVE_SECBASEPRIV_H
70 # include <Security/SecBasePriv.h>
71 # else /* Declare prototype for function in that header... */
72 extern const char *cssmErrorString(int error
);
73 # endif /* HAVE_SECBASEPRIV_H */
74 #endif /* HAVE_CDSASSL */
77 # include <gnutls/x509.h>
78 #endif /* HAVE_GNUTLS */
82 #endif /* HAVE_TCPD_H */
89 static int check_if_modified(cupsd_client_t
*con
,
90 struct stat
*filestats
);
91 static int compare_clients(cupsd_client_t
*a
, cupsd_client_t
*b
,
93 static int data_ready(cupsd_client_t
*con
);
95 static int encrypt_client(cupsd_client_t
*con
);
98 static CFArrayRef
get_cdsa_certificate(cupsd_client_t
*con
);
99 #endif /* HAVE_CDSASSL */
100 static char *get_file(cupsd_client_t
*con
, struct stat
*filestats
,
101 char *filename
, int len
);
102 static http_status_t
install_conf_file(cupsd_client_t
*con
);
103 static int is_cgi(cupsd_client_t
*con
, const char *filename
,
104 struct stat
*filestats
, mime_type_t
*type
);
105 static int is_path_absolute(const char *path
);
107 static int make_certificate(cupsd_client_t
*con
);
108 #endif /* HAVE_SSL */
109 static int pipe_command(cupsd_client_t
*con
, int infile
, int *outfile
,
110 char *command
, char *options
, int root
);
111 static int write_file(cupsd_client_t
*con
, http_status_t code
,
112 char *filename
, char *type
,
113 struct stat
*filestats
);
114 static void write_pipe(cupsd_client_t
*con
);
118 * 'cupsdAcceptClient()' - Accept a new client.
122 cupsdAcceptClient(cupsd_listener_t
*lis
)/* I - Listener socket */
124 int count
; /* Count of connections on a host */
125 int val
; /* Parameter value */
126 cupsd_client_t
*con
, /* New client pointer */
127 *tempcon
; /* Temporary client pointer */
128 http_addrlist_t
*addrlist
, /* List of adddresses for host */
129 *addr
; /* Current address */
130 socklen_t addrlen
; /* Length of address */
131 char *hostname
; /* Hostname for address */
132 http_addr_t temp
; /* Temporary address variable */
133 static time_t last_dos
= 0; /* Time of last DoS attack */
135 struct request_info wrap_req
; /* TCP wrappers request information */
136 #endif /* HAVE_TCPD_H */
139 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
140 "cupsdAcceptClient(lis=%p) %d Clients = %d",
141 lis
, lis
->fd
, cupsArrayCount(Clients
));
144 * Make sure we don't have a full set of clients already...
147 if (cupsArrayCount(Clients
) == MaxClients
)
151 * Get a pointer to the next available client...
155 Clients
= cupsArrayNew(NULL
, NULL
);
159 cupsdLogMessage(CUPSD_LOG_ERROR
,
160 "Unable to allocate memory for clients array!");
161 cupsdPauseListening();
166 ActiveClients
= cupsArrayNew((cups_array_func_t
)compare_clients
, NULL
);
170 cupsdLogMessage(CUPSD_LOG_ERROR
,
171 "Unable to allocate memory for active clients array!");
172 cupsdPauseListening();
176 if ((con
= calloc(1, sizeof(cupsd_client_t
))) == NULL
)
178 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to allocate memory for client!");
179 cupsdPauseListening();
183 con
->http
.activity
= time(NULL
);
185 con
->http
.hostaddr
= &(con
->clientaddr
);
188 * Accept the client and get the remote address...
191 addrlen
= sizeof(http_addr_t
);
193 if ((con
->http
.fd
= accept(lis
->fd
, (struct sockaddr
*)con
->http
.hostaddr
,
196 if (errno
== ENFILE
|| errno
== EMFILE
)
197 cupsdPauseListening();
199 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to accept client connection - %s.",
207 if (lis
->address
.addr
.sa_family
== AF_INET6
)
210 * Save the connected port number...
213 con
->http
.hostaddr
->ipv6
.sin6_port
= lis
->address
.ipv6
.sin6_port
;
216 * Convert IPv4 over IPv6 addresses (::ffff:n.n.n.n) to IPv4 forms we
217 * can more easily use...
220 if (con
->http
.hostaddr
->ipv6
.sin6_addr
.s6_addr32
[0] == 0 &&
221 con
->http
.hostaddr
->ipv6
.sin6_addr
.s6_addr32
[1] == 0 &&
222 ntohl(con
->http
.hostaddr
->ipv6
.sin6_addr
.s6_addr32
[2]) == 0xffff)
223 con
->http
.hostaddr
->ipv6
.sin6_addr
.s6_addr32
[2] = 0;
226 #endif /* AF_INET6 */
227 if (lis
->address
.addr
.sa_family
== AF_INET
)
228 con
->http
.hostaddr
->ipv4
.sin_port
= lis
->address
.ipv4
.sin_port
;
231 * Check the number of clients on the same address...
234 for (count
= 0, tempcon
= (cupsd_client_t
*)cupsArrayFirst(Clients
);
236 tempcon
= (cupsd_client_t
*)cupsArrayNext(Clients
))
237 if (httpAddrEqual(tempcon
->http
.hostaddr
, con
->http
.hostaddr
))
240 if (count
>= MaxClientsPerHost
)
244 if (count
>= MaxClientsPerHost
)
246 if ((time(NULL
) - last_dos
) >= 60)
248 last_dos
= time(NULL
);
249 cupsdLogMessage(CUPSD_LOG_WARN
,
250 "Possible DoS attack - more than %d clients connecting "
253 httpAddrString(con
->http
.hostaddr
, con
->http
.hostname
,
254 sizeof(con
->http
.hostname
)));
258 closesocket(con
->http
.fd
);
268 * Get the hostname or format the IP address as needed...
271 if (httpAddrLocalhost(con
->http
.hostaddr
))
274 * Map accesses from the loopback interface to "localhost"...
277 strlcpy(con
->http
.hostname
, "localhost", sizeof(con
->http
.hostname
));
278 hostname
= con
->http
.hostname
;
283 * Map accesses from the same host to the server name.
286 for (addr
= ServerAddrs
; addr
; addr
= addr
->next
)
287 if (httpAddrEqual(con
->http
.hostaddr
, &(addr
->addr
)))
292 strlcpy(con
->http
.hostname
, ServerName
, sizeof(con
->http
.hostname
));
293 hostname
= con
->http
.hostname
;
295 else if (HostNameLookups
)
296 hostname
= httpAddrLookup(con
->http
.hostaddr
, con
->http
.hostname
,
297 sizeof(con
->http
.hostname
));
301 httpAddrString(con
->http
.hostaddr
, con
->http
.hostname
,
302 sizeof(con
->http
.hostname
));
306 if (hostname
== NULL
&& HostNameLookups
== 2)
309 * Can't have an unresolved IP address with double-lookups enabled...
312 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
313 "cupsdAcceptClient: Closing connection %d...",
317 closesocket(con
->http
.fd
);
322 cupsdLogMessage(CUPSD_LOG_WARN
,
323 "Name lookup failed - connection from %s closed!",
330 if (HostNameLookups
== 2)
333 * Do double lookups as needed...
336 if ((addrlist
= httpAddrGetList(con
->http
.hostname
, AF_UNSPEC
, NULL
))
340 * See if the hostname maps to the same IP address...
343 for (addr
= addrlist
; addr
; addr
= addr
->next
)
344 if (httpAddrEqual(con
->http
.hostaddr
, &(addr
->addr
)))
350 httpAddrFreeList(addrlist
);
355 * Can't have a hostname that doesn't resolve to the same IP address
356 * with double-lookups enabled...
359 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
360 "cupsdAcceptClient: Closing connection %d...",
364 closesocket(con
->http
.fd
);
369 cupsdLogMessage(CUPSD_LOG_WARN
,
370 "IP lookup failed - connection from %s closed!",
379 * See if the connection is denied by TCP wrappers...
382 request_init(&wrap_req
, RQ_DAEMON
, "cupsd", RQ_FILE
, con
->http
.fd
, NULL
);
385 if (!hosts_access(&wrap_req
))
387 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
388 "cupsdAcceptClient: Closing connection %d...",
392 closesocket(con
->http
.fd
);
397 cupsdLogMessage(CUPSD_LOG_WARN
,
398 "Connection from %s refused by /etc/hosts.allow and "
399 "/etc/hosts.deny rules.", con
->http
.hostname
);
403 #endif /* HAVE_TCPD_H */
406 if (con
->http
.hostaddr
->addr
.sa_family
== AF_INET6
)
407 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdAcceptClient: %d from %s:%d (IPv6)",
408 con
->http
.fd
, con
->http
.hostname
,
409 ntohs(con
->http
.hostaddr
->ipv6
.sin6_port
));
411 #endif /* AF_INET6 */
413 if (con
->http
.hostaddr
->addr
.sa_family
== AF_LOCAL
)
414 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdAcceptClient: %d from %s (Domain)",
415 con
->http
.fd
, con
->http
.hostname
);
417 #endif /* AF_LOCAL */
418 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdAcceptClient: %d from %s:%d (IPv4)",
419 con
->http
.fd
, con
->http
.hostname
,
420 ntohs(con
->http
.hostaddr
->ipv4
.sin_port
));
423 * Get the local address the client connected to...
426 addrlen
= sizeof(temp
);
427 if (getsockname(con
->http
.fd
, (struct sockaddr
*)&temp
, &addrlen
))
429 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to get local address - %s",
432 strcpy(con
->servername
, "localhost");
433 con
->serverport
= LocalPort
;
438 if (temp
.addr
.sa_family
== AF_INET6
)
440 httpAddrLookup(&temp
, con
->servername
, sizeof(con
->servername
));
441 con
->serverport
= ntohs(lis
->address
.ipv6
.sin6_port
);
444 #endif /* AF_INET6 */
445 if (temp
.addr
.sa_family
== AF_INET
)
447 httpAddrLookup(&temp
, con
->servername
, sizeof(con
->servername
));
448 con
->serverport
= ntohs(lis
->address
.ipv4
.sin_port
);
452 strcpy(con
->servername
, "localhost");
453 con
->serverport
= LocalPort
;
457 cupsArrayAdd(Clients
, con
);
459 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
460 "cupsdAcceptClient: %d connected to server on %s:%d",
461 con
->http
.fd
, con
->servername
, con
->serverport
);
464 * Using TCP_NODELAY improves responsiveness, especially on systems
465 * with a slow loopback interface... Since we write large buffers
466 * when sending print files and requests, there shouldn't be any
467 * performance penalty for this...
471 setsockopt(con
->http
.fd
, IPPROTO_TCP
, TCP_NODELAY
, &val
, sizeof(val
));
474 * Close this file on all execs...
477 fcntl(con
->http
.fd
, F_SETFD
, fcntl(con
->http
.fd
, F_GETFD
) | FD_CLOEXEC
);
480 * Add the socket to the select() input mask.
483 cupsdAddSelect(con
->http
.fd
, (cupsd_selfunc_t
)cupsdReadClient
, NULL
, con
);
486 * Temporarily suspend accept()'s until we lose a client...
489 if (cupsArrayCount(Clients
) == MaxClients
)
490 cupsdPauseListening();
494 * See if we are connecting on a secure port...
497 if (lis
->encryption
== HTTP_ENCRYPT_ALWAYS
)
500 * https connection; go secure...
503 con
->http
.encryption
= HTTP_ENCRYPT_ALWAYS
;
505 if (!encrypt_client(con
))
506 cupsdCloseClient(con
);
510 #endif /* HAVE_SSL */
515 * 'cupsdCloseAllClients()' - Close all remote clients immediately.
519 cupsdCloseAllClients(void)
521 cupsd_client_t
*con
; /* Current client */
524 for (con
= (cupsd_client_t
*)cupsArrayFirst(Clients
);
526 con
= (cupsd_client_t
*)cupsArrayNext(Clients
))
527 cupsdCloseClient(con
);
532 * 'cupsdCloseClient()' - Close a remote client.
535 int /* O - 1 if partial close, 0 if fully closed */
536 cupsdCloseClient(cupsd_client_t
*con
) /* I - Client to close */
538 int partial
; /* Do partial close for SSL? */
540 SSL_CTX
*context
; /* Context for encryption */
541 SSL
*conn
; /* Connection for encryption */
542 unsigned long error
; /* Error code */
543 #elif defined(HAVE_GNUTLS)
544 http_tls_t
*conn
; /* TLS connection information */
545 int error
; /* Error code */
546 gnutls_certificate_server_credentials
*credentials
;
547 /* TLS credentials */
548 # elif defined(HAVE_CDSASSL)
549 http_tls_t
*conn
; /* CDSA connection information */
550 #endif /* HAVE_LIBSSL */
553 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdCloseClient: %d", con
->http
.fd
);
556 * Flush pending writes before closing...
559 httpFlushWrite(HTTP(con
));
565 * Shutdown encryption as needed...
573 conn
= (SSL
*)(con
->http
.tls
);
574 context
= SSL_get_SSL_CTX(conn
);
576 switch (SSL_shutdown(conn
))
579 cupsdLogMessage(CUPSD_LOG_DEBUG
,
580 "SSL shutdown successful!");
583 cupsdLogMessage(CUPSD_LOG_ERROR
,
584 "Fatal error during SSL shutdown!");
586 while ((error
= ERR_get_error()) != 0)
587 cupsdLogMessage(CUPSD_LOG_ERROR
, "SSL shutdown failed: %s",
588 ERR_error_string(error
, NULL
));
592 SSL_CTX_free(context
);
595 # elif defined(HAVE_GNUTLS)
596 conn
= (http_tls_t
*)(con
->http
.tls
);
597 credentials
= (gnutls_certificate_server_credentials
*)(conn
->credentials
);
599 error
= gnutls_bye(conn
->session
, GNUTLS_SHUT_WR
);
602 case GNUTLS_E_SUCCESS
:
603 cupsdLogMessage(CUPSD_LOG_DEBUG
,
604 "SSL shutdown successful!");
607 cupsdLogMessage(CUPSD_LOG_ERROR
,
608 "SSL shutdown failed: %s", gnutls_strerror(error
));
612 gnutls_deinit(conn
->session
);
613 gnutls_certificate_free_credentials(*credentials
);
617 # elif defined(HAVE_CDSASSL)
618 conn
= (http_tls_t
*)(con
->http
.tls
);
620 while (SSLClose(conn
->session
) == errSSLWouldBlock
)
623 SSLDisposeContext(conn
->session
);
625 if (conn
->certsArray
)
626 CFRelease(conn
->certsArray
);
629 # endif /* HAVE_LIBSSL */
631 con
->http
.tls
= NULL
;
633 #endif /* HAVE_SSL */
635 if (con
->pipe_pid
!= 0)
638 * Stop any CGI process...
641 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
642 "cupsdCloseClient: %d Killing process ID %d...",
643 con
->http
.fd
, con
->pipe_pid
);
644 cupsdEndProcess(con
->pipe_pid
, 1);
650 cupsdRemoveSelect(con
->file
);
652 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
653 "cupsdCloseClient: %d Closing data file %d.",
654 con
->http
.fd
, con
->file
);
661 * Close the socket and clear the file from the input set for select()...
664 if (con
->http
.fd
>= 0)
666 cupsArrayRemove(ActiveClients
, con
);
672 * Only do a partial close so that the encrypted client gets everything.
675 shutdown(con
->http
.fd
, 0);
676 cupsdAddSelect(con
->http
.fd
, (cupsd_selfunc_t
)cupsdReadClient
, NULL
, con
);
681 * Shut the socket down fully...
684 cupsdRemoveSelect(con
->http
.fd
);
696 if (con
->http
.input_set
)
697 free(con
->http
.input_set
);
699 httpClearCookie(HTTP(con
));
701 cupsdClearString(&con
->filename
);
702 cupsdClearString(&con
->command
);
703 cupsdClearString(&con
->options
);
704 cupsdClearString(&con
->query_string
);
708 ippDelete(con
->request
);
714 ippDelete(con
->response
);
715 con
->response
= NULL
;
720 cupsLangFree(con
->language
);
721 con
->language
= NULL
;
724 #ifdef HAVE_AUTHORIZATION_H
727 AuthorizationFree(con
->authref
, kAuthorizationFlagDefaults
);
730 #endif /* HAVE_AUTHORIZATION_H */
733 * Re-enable new client connections if we are going back under the
737 if (cupsArrayCount(Clients
) == MaxClients
)
738 cupsdResumeListening();
741 * Compact the list of clients as necessary...
744 cupsArrayRemove(Clients
, con
);
754 * 'cupsdFlushHeader()' - Flush the header fields to the client.
757 int /* I - Bytes written or -1 on error */
758 cupsdFlushHeader(cupsd_client_t
*con
) /* I - Client to flush to */
760 int bytes
= httpFlushWrite(HTTP(con
));
762 con
->http
.data_encoding
= HTTP_ENCODE_LENGTH
;
769 * 'cupsdReadClient()' - Read data from a client.
773 cupsdReadClient(cupsd_client_t
*con
) /* I - Client to read from */
775 char line
[32768], /* Line from client... */
776 operation
[64], /* Operation code from socket */
777 version
[64], /* HTTP version number string */
778 locale
[64], /* Locale */
779 *ptr
; /* Pointer into strings */
780 int major
, minor
; /* HTTP version numbers */
781 http_status_t status
; /* Transfer status */
782 ipp_state_t ipp_state
; /* State of IPP transfer */
783 int bytes
; /* Number of bytes to POST */
784 char *filename
; /* Name of file for GET/HEAD */
785 char buf
[1024]; /* Buffer for real filename */
786 struct stat filestats
; /* File information */
787 mime_type_t
*type
; /* MIME type of file */
788 cupsd_printer_t
*p
; /* Printer */
789 static unsigned request_id
= 0; /* Request ID for temp files */
792 status
= HTTP_CONTINUE
;
794 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
795 "cupsdReadClient: %d, used=%d, file=%d state=%d",
796 con
->http
.fd
, con
->http
.used
, con
->file
, con
->http
.state
);
800 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdReadClient: http error seen...");
801 cupsdCloseClient(con
);
809 * Automatically check for a SSL/TLS handshake...
814 if (recv(con
->http
.fd
, buf
, 1, MSG_PEEK
) == 1 &&
815 (!buf
[0] || !strchr("DGHOPT", buf
[0])))
818 * Encrypt this connection...
821 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
822 "cupsdReadClient: Saw first byte %02X, auto-negotiating SSL/TLS session...",
825 if (!encrypt_client(con
))
826 cupsdCloseClient(con
);
831 #endif /* HAVE_SSL */
833 switch (con
->http
.state
)
837 * See if we've received a request line...
840 if (httpGets(line
, sizeof(line
) - 1, HTTP(con
)) == NULL
)
842 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
843 "cupsdReadClient: httpGets returned EOF...");
844 cupsdCloseClient(con
);
849 * Ignore blank request lines...
856 * Clear other state variables...
859 httpClearFields(HTTP(con
));
861 con
->http
.activity
= time(NULL
);
862 con
->http
.version
= HTTP_1_0
;
863 con
->http
.keep_alive
= HTTP_KEEPALIVE_OFF
;
864 con
->http
.data_encoding
= HTTP_ENCODE_LENGTH
;
865 con
->http
.data_remaining
= 0;
866 con
->http
._data_remaining
= 0;
867 con
->operation
= HTTP_WAITING
;
872 con
->username
[0] = '\0';
873 con
->password
[0] = '\0';
876 cupsdClearString(&con
->command
);
877 cupsdClearString(&con
->options
);
878 cupsdClearString(&con
->query_string
);
882 ippDelete(con
->request
);
888 ippDelete(con
->response
);
889 con
->response
= NULL
;
894 cupsLangFree(con
->language
);
895 con
->language
= NULL
;
899 con
->gss_have_creds
= 0;
900 #endif /* HAVE_GSSAPI */
903 * Grab the request line...
906 switch (sscanf(line
, "%63s%1023s%63s", operation
, con
->uri
, version
))
911 cupsdLogMessage(CUPSD_LOG_ERROR
,
912 "Bad request line \"%s\" from %s!", line
,
914 cupsdSendError(con
, HTTP_BAD_REQUEST
, CUPSD_AUTH_NONE
);
915 cupsdCloseClient(con
);
919 con
->http
.version
= HTTP_0_9
;
922 if (sscanf(version
, "HTTP/%d.%d", &major
, &minor
) != 2)
924 cupsdLogMessage(CUPSD_LOG_ERROR
,
925 "Bad request line \"%s\" from %s!", line
,
927 cupsdSendError(con
, HTTP_BAD_REQUEST
, CUPSD_AUTH_NONE
);
928 cupsdCloseClient(con
);
934 con
->http
.version
= (http_version_t
)(major
* 100 + minor
);
935 if (con
->http
.version
== HTTP_1_1
&& KeepAlive
)
936 con
->http
.keep_alive
= HTTP_KEEPALIVE_ON
;
938 con
->http
.keep_alive
= HTTP_KEEPALIVE_OFF
;
942 cupsdSendError(con
, HTTP_NOT_SUPPORTED
, CUPSD_AUTH_NONE
);
943 cupsdCloseClient(con
);
950 * Handle full URLs in the request line...
953 if (strcmp(con
->uri
, "*"))
955 char method
[HTTP_MAX_URI
], /* Method/scheme */
956 userpass
[HTTP_MAX_URI
], /* Username:password */
957 hostname
[HTTP_MAX_URI
], /* Hostname */
958 resource
[HTTP_MAX_URI
]; /* Resource path */
959 int port
; /* Port number */
963 * Separate the URI into its components...
966 httpSeparateURI(HTTP_URI_CODING_MOST
, con
->uri
,
967 method
, sizeof(method
),
968 userpass
, sizeof(userpass
),
969 hostname
, sizeof(hostname
), &port
,
970 resource
, sizeof(resource
));
973 * Only allow URIs with the servername, localhost, or an IP
977 if (strcmp(method
, "file") &&
978 strcasecmp(hostname
, ServerName
) &&
979 strcasecmp(hostname
, "localhost") &&
980 !isdigit(hostname
[0]))
983 * Nope, we don't do proxies...
986 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad URI \"%s\" in request!",
988 cupsdSendError(con
, HTTP_METHOD_NOT_ALLOWED
, CUPSD_AUTH_NONE
);
989 cupsdCloseClient(con
);
994 * Copy the resource portion back into the URI; both resource and
995 * con->uri are HTTP_MAX_URI bytes in size...
998 strcpy(con
->uri
, resource
);
1002 * Process the request...
1005 if (!strcmp(operation
, "GET"))
1006 con
->http
.state
= HTTP_GET
;
1007 else if (!strcmp(operation
, "PUT"))
1008 con
->http
.state
= HTTP_PUT
;
1009 else if (!strcmp(operation
, "POST"))
1010 con
->http
.state
= HTTP_POST
;
1011 else if (!strcmp(operation
, "DELETE"))
1012 con
->http
.state
= HTTP_DELETE
;
1013 else if (!strcmp(operation
, "TRACE"))
1014 con
->http
.state
= HTTP_TRACE
;
1015 else if (!strcmp(operation
, "OPTIONS"))
1016 con
->http
.state
= HTTP_OPTIONS
;
1017 else if (!strcmp(operation
, "HEAD"))
1018 con
->http
.state
= HTTP_HEAD
;
1021 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad operation \"%s\"!", operation
);
1022 cupsdSendError(con
, HTTP_BAD_REQUEST
, CUPSD_AUTH_NONE
);
1023 cupsdCloseClient(con
);
1027 con
->start
= time(NULL
);
1028 con
->operation
= con
->http
.state
;
1030 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdReadClient: %d %s %s HTTP/%d.%d",
1031 con
->http
.fd
, operation
, con
->uri
,
1032 con
->http
.version
/ 100, con
->http
.version
% 100);
1034 con
->http
.status
= HTTP_OK
;
1036 cupsArrayAdd(ActiveClients
, con
);
1037 cupsdSetBusyState();
1047 * Parse incoming parameters until the status changes...
1050 while ((status
= httpUpdate(HTTP(con
))) == HTTP_CONTINUE
)
1051 if (!data_ready(con
))
1054 if (status
!= HTTP_OK
&& status
!= HTTP_CONTINUE
)
1056 cupsdSendError(con
, HTTP_BAD_REQUEST
, CUPSD_AUTH_NONE
);
1057 cupsdCloseClient(con
);
1063 break; /* Anti-compiler-warning-code */
1067 * Handle new transfers...
1070 if (status
== HTTP_OK
)
1072 if (con
->http
.fields
[HTTP_FIELD_ACCEPT_LANGUAGE
][0])
1075 * Figure out the locale from the Accept-Language and Content-Type
1079 if ((ptr
= strchr(con
->http
.fields
[HTTP_FIELD_ACCEPT_LANGUAGE
], ',')) != NULL
)
1082 if ((ptr
= strchr(con
->http
.fields
[HTTP_FIELD_ACCEPT_LANGUAGE
], ';')) != NULL
)
1085 if ((ptr
= strstr(con
->http
.fields
[HTTP_FIELD_CONTENT_TYPE
], "charset=")) != NULL
)
1088 * Combine language and charset, and trim any extra params in the
1092 snprintf(locale
, sizeof(locale
), "%s.%s",
1093 con
->http
.fields
[HTTP_FIELD_ACCEPT_LANGUAGE
], ptr
+ 8);
1095 if ((ptr
= strchr(locale
, ',')) != NULL
)
1099 snprintf(locale
, sizeof(locale
), "%s.%s",
1100 con
->http
.fields
[HTTP_FIELD_ACCEPT_LANGUAGE
], DefaultCharset
);
1102 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1103 "cupsdReadClient: %d Browser asked for language \"%s\"...",
1104 con
->http
.fd
, locale
);
1106 con
->language
= cupsLangGet(locale
);
1109 con
->language
= cupsLangGet(DefaultLocale
);
1111 cupsdAuthorize(con
);
1113 if (!strncmp(con
->http
.fields
[HTTP_FIELD_CONNECTION
], "Keep-Alive", 10) &&
1115 con
->http
.keep_alive
= HTTP_KEEPALIVE_ON
;
1117 if (!con
->http
.fields
[HTTP_FIELD_HOST
][0] &&
1118 con
->http
.version
>= HTTP_1_1
)
1121 * HTTP/1.1 and higher require the "Host:" field...
1124 if (!cupsdSendError(con
, HTTP_BAD_REQUEST
, CUPSD_AUTH_NONE
))
1126 cupsdCloseClient(con
);
1130 else if (con
->operation
== HTTP_OPTIONS
)
1133 * Do OPTIONS command...
1136 if (con
->best
&& con
->best
->type
!= CUPSD_AUTH_NONE
)
1138 if (!cupsdSendHeader(con
, HTTP_UNAUTHORIZED
, NULL
, CUPSD_AUTH_NONE
))
1140 cupsdCloseClient(con
);
1145 if (!strcasecmp(con
->http
.fields
[HTTP_FIELD_CONNECTION
], "Upgrade") &&
1146 con
->http
.tls
== NULL
)
1150 * Do encryption stuff...
1153 if (!cupsdSendHeader(con
, HTTP_SWITCHING_PROTOCOLS
, NULL
, CUPSD_AUTH_NONE
))
1155 cupsdCloseClient(con
);
1159 httpPrintf(HTTP(con
), "Connection: Upgrade\r\n");
1160 httpPrintf(HTTP(con
), "Upgrade: TLS/1.0,HTTP/1.1\r\n");
1161 httpPrintf(HTTP(con
), "Content-Length: 0\r\n");
1162 httpPrintf(HTTP(con
), "\r\n");
1164 if (cupsdFlushHeader(con
) < 0)
1166 cupsdCloseClient(con
);
1170 if (!encrypt_client(con
))
1172 cupsdCloseClient(con
);
1176 if (!cupsdSendError(con
, HTTP_NOT_IMPLEMENTED
, CUPSD_AUTH_NONE
))
1178 cupsdCloseClient(con
);
1181 #endif /* HAVE_SSL */
1184 if (!cupsdSendHeader(con
, HTTP_OK
, NULL
, CUPSD_AUTH_NONE
))
1186 cupsdCloseClient(con
);
1190 httpPrintf(HTTP(con
), "Allow: GET, HEAD, OPTIONS, POST, PUT\r\n");
1191 httpPrintf(HTTP(con
), "Content-Length: 0\r\n");
1192 httpPrintf(HTTP(con
), "\r\n");
1194 if (cupsdFlushHeader(con
) < 0)
1196 cupsdCloseClient(con
);
1200 else if (!is_path_absolute(con
->uri
))
1203 * Protect against malicious users!
1206 if (!cupsdSendError(con
, HTTP_FORBIDDEN
, CUPSD_AUTH_NONE
))
1208 cupsdCloseClient(con
);
1214 if (!strcasecmp(con
->http
.fields
[HTTP_FIELD_CONNECTION
], "Upgrade") &&
1215 con
->http
.tls
== NULL
)
1219 * Do encryption stuff...
1222 if (!cupsdSendHeader(con
, HTTP_SWITCHING_PROTOCOLS
, NULL
, CUPSD_AUTH_NONE
))
1224 cupsdCloseClient(con
);
1228 httpPrintf(HTTP(con
), "Connection: Upgrade\r\n");
1229 httpPrintf(HTTP(con
), "Upgrade: TLS/1.0,HTTP/1.1\r\n");
1230 httpPrintf(HTTP(con
), "Content-Length: 0\r\n");
1231 httpPrintf(HTTP(con
), "\r\n");
1233 if (cupsdFlushHeader(con
) < 0)
1235 cupsdCloseClient(con
);
1239 if (!encrypt_client(con
))
1241 cupsdCloseClient(con
);
1245 if (!cupsdSendError(con
, HTTP_NOT_IMPLEMENTED
, CUPSD_AUTH_NONE
))
1247 cupsdCloseClient(con
);
1250 #endif /* HAVE_SSL */
1253 if ((status
= cupsdIsAuthorized(con
, NULL
)) != HTTP_OK
)
1255 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1256 "cupsdReadClient: Unauthorized request for %s...",
1258 cupsdSendError(con
, status
, CUPSD_AUTH_NONE
);
1259 cupsdCloseClient(con
);
1263 if (con
->http
.expect
&&
1264 (con
->operation
== HTTP_POST
|| con
->operation
== HTTP_PUT
))
1266 if (con
->http
.expect
== HTTP_CONTINUE
)
1269 * Send 100-continue header...
1272 if (!cupsdSendHeader(con
, HTTP_CONTINUE
, NULL
, CUPSD_AUTH_NONE
))
1274 cupsdCloseClient(con
);
1281 * Send 417-expectation-failed header...
1284 if (!cupsdSendHeader(con
, HTTP_EXPECTATION_FAILED
, NULL
, CUPSD_AUTH_NONE
))
1286 cupsdCloseClient(con
);
1290 httpPrintf(HTTP(con
), "Content-Length: 0\r\n");
1291 httpPrintf(HTTP(con
), "\r\n");
1293 if (cupsdFlushHeader(con
) < 0)
1295 cupsdCloseClient(con
);
1301 switch (con
->http
.state
)
1303 case HTTP_GET_SEND
:
1304 if (!strncmp(con
->uri
, "/printers/", 10) &&
1305 !strcmp(con
->uri
+ strlen(con
->uri
) - 4, ".ppd"))
1308 * Send PPD file - get the real printer name since printer
1309 * names are not case sensitive but filenames can be...
1312 con
->uri
[strlen(con
->uri
) - 4] = '\0'; /* Drop ".ppd" */
1314 if ((p
= cupsdFindPrinter(con
->uri
+ 10)) != NULL
)
1315 snprintf(con
->uri
, sizeof(con
->uri
), "/ppd/%s.ppd", p
->name
);
1318 if (!cupsdSendError(con
, HTTP_NOT_FOUND
, CUPSD_AUTH_NONE
))
1320 cupsdCloseClient(con
);
1328 if ((!strncmp(con
->uri
, "/admin", 6) &&
1329 strncmp(con
->uri
, "/admin/conf/", 12) &&
1330 strncmp(con
->uri
, "/admin/log/", 11)) ||
1331 !strncmp(con
->uri
, "/printers", 9) ||
1332 !strncmp(con
->uri
, "/classes", 8) ||
1333 !strncmp(con
->uri
, "/help", 5) ||
1334 !strncmp(con
->uri
, "/jobs", 5))
1337 * Send CGI output...
1340 if (!strncmp(con
->uri
, "/admin", 6))
1342 cupsdSetStringf(&con
->command
, "%s/cgi-bin/admin.cgi",
1345 cupsdSetString(&con
->options
, strchr(con
->uri
+ 6, '?'));
1347 else if (!strncmp(con
->uri
, "/printers", 9))
1349 cupsdSetStringf(&con
->command
, "%s/cgi-bin/printers.cgi",
1352 if (con
->uri
[9] && con
->uri
[10])
1353 cupsdSetString(&con
->options
, con
->uri
+ 9);
1355 cupsdSetString(&con
->options
, NULL
);
1357 else if (!strncmp(con
->uri
, "/classes", 8))
1359 cupsdSetStringf(&con
->command
, "%s/cgi-bin/classes.cgi",
1362 if (con
->uri
[8] && con
->uri
[9])
1363 cupsdSetString(&con
->options
, con
->uri
+ 8);
1365 cupsdSetString(&con
->options
, NULL
);
1367 else if (!strncmp(con
->uri
, "/jobs", 5))
1369 cupsdSetStringf(&con
->command
, "%s/cgi-bin/jobs.cgi",
1372 if (con
->uri
[5] && con
->uri
[6])
1373 cupsdSetString(&con
->options
, con
->uri
+ 5);
1375 cupsdSetString(&con
->options
, NULL
);
1379 cupsdSetStringf(&con
->command
, "%s/cgi-bin/help.cgi",
1382 if (con
->uri
[5] && con
->uri
[6])
1383 cupsdSetString(&con
->options
, con
->uri
+ 5);
1385 cupsdSetString(&con
->options
, NULL
);
1388 if (!cupsdSendCommand(con
, con
->command
, con
->options
, 0))
1390 if (!cupsdSendError(con
, HTTP_NOT_FOUND
, CUPSD_AUTH_NONE
))
1392 cupsdCloseClient(con
);
1397 cupsdLogRequest(con
, HTTP_OK
);
1399 if (con
->http
.version
<= HTTP_1_0
)
1400 con
->http
.keep_alive
= HTTP_KEEPALIVE_OFF
;
1402 else if ((!strncmp(con
->uri
, "/admin/conf/", 12) &&
1403 (strchr(con
->uri
+ 12, '/') ||
1404 strlen(con
->uri
) == 12)) ||
1405 (!strncmp(con
->uri
, "/admin/log/", 11) &&
1406 (strchr(con
->uri
+ 11, '/') ||
1407 strlen(con
->uri
) == 11)))
1410 * GET can only be done to configuration files under
1414 if (!cupsdSendError(con
, HTTP_FORBIDDEN
, CUPSD_AUTH_NONE
))
1416 cupsdCloseClient(con
);
1428 if ((filename
= get_file(con
, &filestats
, buf
,
1429 sizeof(buf
))) == NULL
)
1431 if (!cupsdSendError(con
, HTTP_NOT_FOUND
, CUPSD_AUTH_NONE
))
1433 cupsdCloseClient(con
);
1440 type
= mimeFileType(MimeDatabase
, filename
, NULL
, NULL
);
1442 if (is_cgi(con
, filename
, &filestats
, type
))
1445 * Note: con->command and con->options were set by
1449 if (!cupsdSendCommand(con
, con
->command
, con
->options
, 0))
1451 if (!cupsdSendError(con
, HTTP_NOT_FOUND
, CUPSD_AUTH_NONE
))
1453 cupsdCloseClient(con
);
1458 cupsdLogRequest(con
, HTTP_OK
);
1460 if (con
->http
.version
<= HTTP_1_0
)
1461 con
->http
.keep_alive
= HTTP_KEEPALIVE_OFF
;
1465 if (!check_if_modified(con
, &filestats
))
1467 if (!cupsdSendError(con
, HTTP_NOT_MODIFIED
, CUPSD_AUTH_NONE
))
1469 cupsdCloseClient(con
);
1476 strcpy(line
, "text/plain");
1478 snprintf(line
, sizeof(line
), "%s/%s", type
->super
, type
->type
);
1480 if (!write_file(con
, HTTP_OK
, filename
, line
, &filestats
))
1482 cupsdCloseClient(con
);
1489 case HTTP_POST_RECV
:
1491 * See if the POST request includes a Content-Length field, and if
1492 * so check the length against any limits that are set...
1495 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "POST %s", con
->uri
);
1496 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "CONTENT_TYPE = %s",
1497 con
->http
.fields
[HTTP_FIELD_CONTENT_TYPE
]);
1499 if (con
->http
.fields
[HTTP_FIELD_CONTENT_LENGTH
][0] &&
1500 MaxRequestSize
> 0 &&
1501 con
->http
.data_remaining
> MaxRequestSize
)
1504 * Request too large...
1507 if (!cupsdSendError(con
, HTTP_REQUEST_TOO_LARGE
, CUPSD_AUTH_NONE
))
1509 cupsdCloseClient(con
);
1515 else if (con
->http
.data_remaining
< 0 ||
1516 (!con
->http
.fields
[HTTP_FIELD_CONTENT_LENGTH
][0] &&
1517 con
->http
.data_encoding
== HTTP_ENCODE_LENGTH
))
1520 * Negative content lengths are invalid!
1523 if (!cupsdSendError(con
, HTTP_BAD_REQUEST
, CUPSD_AUTH_NONE
))
1525 cupsdCloseClient(con
);
1533 * See what kind of POST request this is; for IPP requests the
1534 * content-type field will be "application/ipp"...
1537 if (!strcmp(con
->http
.fields
[HTTP_FIELD_CONTENT_TYPE
],
1539 con
->request
= ippNew();
1540 else if ((!strncmp(con
->uri
, "/admin", 6) &&
1541 strncmp(con
->uri
, "/admin/conf/", 12) &&
1542 strncmp(con
->uri
, "/admin/log/", 11)) ||
1543 !strncmp(con
->uri
, "/printers", 9) ||
1544 !strncmp(con
->uri
, "/classes", 8) ||
1545 !strncmp(con
->uri
, "/help", 5) ||
1546 !strncmp(con
->uri
, "/jobs", 5))
1552 if (!strncmp(con
->uri
, "/admin", 6))
1554 cupsdSetStringf(&con
->command
, "%s/cgi-bin/admin.cgi",
1557 cupsdSetString(&con
->options
, strchr(con
->uri
+ 6, '?'));
1559 else if (!strncmp(con
->uri
, "/printers", 9))
1561 cupsdSetStringf(&con
->command
, "%s/cgi-bin/printers.cgi",
1564 if (con
->uri
[9] && con
->uri
[10])
1565 cupsdSetString(&con
->options
, con
->uri
+ 9);
1567 cupsdSetString(&con
->options
, NULL
);
1569 else if (!strncmp(con
->uri
, "/classes", 8))
1571 cupsdSetStringf(&con
->command
, "%s/cgi-bin/classes.cgi",
1574 if (con
->uri
[8] && con
->uri
[9])
1575 cupsdSetString(&con
->options
, con
->uri
+ 8);
1577 cupsdSetString(&con
->options
, NULL
);
1579 else if (!strncmp(con
->uri
, "/jobs", 5))
1581 cupsdSetStringf(&con
->command
, "%s/cgi-bin/jobs.cgi",
1584 if (con
->uri
[5] && con
->uri
[6])
1585 cupsdSetString(&con
->options
, con
->uri
+ 5);
1587 cupsdSetString(&con
->options
, NULL
);
1591 cupsdSetStringf(&con
->command
, "%s/cgi-bin/help.cgi",
1594 if (con
->uri
[5] && con
->uri
[6])
1595 cupsdSetString(&con
->options
, con
->uri
+ 5);
1597 cupsdSetString(&con
->options
, NULL
);
1600 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1601 "cupsdReadClient: %d command=\"%s\", "
1603 con
->http
.fd
, con
->command
,
1604 con
->options
? con
->options
: "(null)");
1606 if (con
->http
.version
<= HTTP_1_0
)
1607 con
->http
.keep_alive
= HTTP_KEEPALIVE_OFF
;
1615 if ((filename
= get_file(con
, &filestats
, buf
,
1616 sizeof(buf
))) == NULL
)
1618 if (!cupsdSendError(con
, HTTP_NOT_FOUND
, CUPSD_AUTH_NONE
))
1620 cupsdCloseClient(con
);
1627 type
= mimeFileType(MimeDatabase
, filename
, NULL
, NULL
);
1629 if (!is_cgi(con
, filename
, &filestats
, type
))
1632 * Only POST to CGI's...
1635 if (!cupsdSendError(con
, HTTP_UNAUTHORIZED
, CUPSD_AUTH_NONE
))
1637 cupsdCloseClient(con
);
1644 case HTTP_PUT_RECV
:
1646 * Validate the resource name...
1649 if (strncmp(con
->uri
, "/admin/conf/", 12) ||
1650 strchr(con
->uri
+ 12, '/') ||
1651 strlen(con
->uri
) == 12)
1654 * PUT can only be done to configuration files under
1658 if (!cupsdSendError(con
, HTTP_FORBIDDEN
, CUPSD_AUTH_NONE
))
1660 cupsdCloseClient(con
);
1668 * See if the PUT request includes a Content-Length field, and if
1669 * so check the length against any limits that are set...
1672 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "PUT %s", con
->uri
);
1673 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "CONTENT_TYPE = %s",
1674 con
->http
.fields
[HTTP_FIELD_CONTENT_TYPE
]);
1676 if (con
->http
.fields
[HTTP_FIELD_CONTENT_LENGTH
][0] &&
1677 MaxRequestSize
> 0 &&
1678 con
->http
.data_remaining
> MaxRequestSize
)
1681 * Request too large...
1684 if (!cupsdSendError(con
, HTTP_REQUEST_TOO_LARGE
, CUPSD_AUTH_NONE
))
1686 cupsdCloseClient(con
);
1692 else if (con
->http
.data_remaining
< 0)
1695 * Negative content lengths are invalid!
1698 if (!cupsdSendError(con
, HTTP_BAD_REQUEST
, CUPSD_AUTH_NONE
))
1700 cupsdCloseClient(con
);
1708 * Open a temporary file to hold the request...
1711 cupsdSetStringf(&con
->filename
, "%s/%08x", RequestRoot
,
1713 con
->file
= open(con
->filename
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0640);
1717 cupsdLogMessage(CUPSD_LOG_ERROR
,
1718 "Unable to create request file %s: %s",
1719 con
->filename
, strerror(errno
));
1721 if (!cupsdSendError(con
, HTTP_REQUEST_TOO_LARGE
, CUPSD_AUTH_NONE
))
1723 cupsdCloseClient(con
);
1728 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1729 "cupsdReadClient: %d REQUEST %s=%d", con
->http
.fd
,
1730 con
->filename
, con
->file
);
1732 fchmod(con
->file
, 0640);
1733 fchown(con
->file
, RunUser
, Group
);
1734 fcntl(con
->file
, F_SETFD
, fcntl(con
->file
, F_GETFD
) | FD_CLOEXEC
);
1739 cupsdSendError(con
, HTTP_NOT_IMPLEMENTED
, CUPSD_AUTH_NONE
);
1740 cupsdCloseClient(con
);
1744 if (!strncmp(con
->uri
, "/printers/", 10) &&
1745 !strcmp(con
->uri
+ strlen(con
->uri
) - 4, ".ppd"))
1748 * Send PPD file - get the real printer name since printer
1749 * names are not case sensitive but filenames can be...
1752 con
->uri
[strlen(con
->uri
) - 4] = '\0'; /* Drop ".ppd" */
1754 if ((p
= cupsdFindPrinter(con
->uri
+ 10)) != NULL
)
1755 snprintf(con
->uri
, sizeof(con
->uri
), "/ppd/%s.ppd", p
->name
);
1758 if (!cupsdSendError(con
, HTTP_NOT_FOUND
, CUPSD_AUTH_NONE
))
1760 cupsdCloseClient(con
);
1768 if ((!strncmp(con
->uri
, "/admin", 6) &&
1769 strncmp(con
->uri
, "/admin/conf/", 12) &&
1770 strncmp(con
->uri
, "/admin/log/", 11)) ||
1771 !strncmp(con
->uri
, "/printers", 9) ||
1772 !strncmp(con
->uri
, "/classes", 8) ||
1773 !strncmp(con
->uri
, "/help", 5) ||
1774 !strncmp(con
->uri
, "/jobs", 5))
1780 if (!cupsdSendHeader(con
, HTTP_OK
, "text/html", CUPSD_AUTH_NONE
))
1782 cupsdCloseClient(con
);
1786 if (httpPrintf(HTTP(con
), "\r\n") < 0)
1788 cupsdCloseClient(con
);
1792 if (cupsdFlushHeader(con
) < 0)
1794 cupsdCloseClient(con
);
1798 cupsdLogRequest(con
, HTTP_OK
);
1800 else if ((!strncmp(con
->uri
, "/admin/conf/", 12) &&
1801 (strchr(con
->uri
+ 12, '/') ||
1802 strlen(con
->uri
) == 12)) ||
1803 (!strncmp(con
->uri
, "/admin/log/", 11) &&
1804 (strchr(con
->uri
+ 11, '/') ||
1805 strlen(con
->uri
) == 11)))
1808 * HEAD can only be done to configuration files under
1812 if (!cupsdSendError(con
, HTTP_FORBIDDEN
, CUPSD_AUTH_NONE
))
1814 cupsdCloseClient(con
);
1820 else if ((filename
= get_file(con
, &filestats
, buf
,
1821 sizeof(buf
))) == NULL
)
1823 if (!cupsdSendHeader(con
, HTTP_NOT_FOUND
, "text/html", CUPSD_AUTH_NONE
))
1825 cupsdCloseClient(con
);
1829 cupsdLogRequest(con
, HTTP_NOT_FOUND
);
1831 else if (!check_if_modified(con
, &filestats
))
1833 if (!cupsdSendError(con
, HTTP_NOT_MODIFIED
, CUPSD_AUTH_NONE
))
1835 cupsdCloseClient(con
);
1839 cupsdLogRequest(con
, HTTP_NOT_MODIFIED
);
1847 type
= mimeFileType(MimeDatabase
, filename
, NULL
, NULL
);
1849 strcpy(line
, "text/plain");
1851 snprintf(line
, sizeof(line
), "%s/%s", type
->super
, type
->type
);
1853 if (!cupsdSendHeader(con
, HTTP_OK
, line
, CUPSD_AUTH_NONE
))
1855 cupsdCloseClient(con
);
1859 if (httpPrintf(HTTP(con
), "Last-Modified: %s\r\n",
1860 httpGetDateString(filestats
.st_mtime
)) < 0)
1862 cupsdCloseClient(con
);
1866 if (httpPrintf(HTTP(con
), "Content-Length: %lu\r\n",
1867 (unsigned long)filestats
.st_size
) < 0)
1869 cupsdCloseClient(con
);
1873 cupsdLogRequest(con
, HTTP_OK
);
1876 if (httpPrintf(HTTP(con
), "\r\n") < 0)
1878 cupsdCloseClient(con
);
1882 if (cupsdFlushHeader(con
) < 0)
1884 cupsdCloseClient(con
);
1888 con
->http
.state
= HTTP_WAITING
;
1892 break; /* Anti-compiler-warning-code */
1898 * Handle any incoming data...
1901 switch (con
->http
.state
)
1903 case HTTP_PUT_RECV
:
1904 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1905 "cupsdReadClient: %d con->data_encoding=HTTP_ENCODE_%s, "
1906 "con->data_remaining=" CUPS_LLFMT
", con->file=%d",
1908 con
->http
.data_encoding
== HTTP_ENCODE_CHUNKED
?
1909 "CHUNKED" : "LENGTH",
1910 CUPS_LLCAST con
->http
.data_remaining
, con
->file
);
1914 if ((bytes
= httpRead2(HTTP(con
), line
, sizeof(line
))) < 0)
1916 cupsdCloseClient(con
);
1921 con
->bytes
+= bytes
;
1923 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1924 "cupsdReadClient: %d writing %d bytes to %d",
1925 con
->http
.fd
, bytes
, con
->file
);
1927 if (write(con
->file
, line
, bytes
) < bytes
)
1929 cupsdLogMessage(CUPSD_LOG_ERROR
,
1930 "cupsdReadClient: Unable to write %d bytes to %s: %s",
1931 bytes
, con
->filename
, strerror(errno
));
1933 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1934 "cupsdReadClient: Closing data file %d...",
1939 unlink(con
->filename
);
1940 cupsdClearString(&con
->filename
);
1942 if (!cupsdSendError(con
, HTTP_REQUEST_TOO_LARGE
, CUPSD_AUTH_NONE
))
1944 cupsdCloseClient(con
);
1950 while (con
->http
.state
== HTTP_PUT_RECV
&& data_ready(con
));
1952 if (con
->http
.state
== HTTP_WAITING
)
1955 * End of file, see how big it is...
1958 fstat(con
->file
, &filestats
);
1960 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1961 "cupsdReadClient: %d Closing data file %d, size="
1963 con
->http
.fd
, con
->file
,
1964 CUPS_LLCAST filestats
.st_size
);
1969 if (filestats
.st_size
> MaxRequestSize
&&
1973 * Request is too big; remove it and send an error...
1976 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1977 "cupsdReadClient: %d Removing temp file %s",
1978 con
->http
.fd
, con
->filename
);
1979 unlink(con
->filename
);
1980 cupsdClearString(&con
->filename
);
1982 if (!cupsdSendError(con
, HTTP_REQUEST_TOO_LARGE
, CUPSD_AUTH_NONE
))
1984 cupsdCloseClient(con
);
1990 * Install the configuration file...
1993 status
= install_conf_file(con
);
1996 * Return the status to the client...
1999 if (!cupsdSendError(con
, status
, CUPSD_AUTH_NONE
))
2001 cupsdCloseClient(con
);
2007 case HTTP_POST_RECV
:
2008 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2009 "cupsdReadClient: %d con->data_encoding=HTTP_ENCODE_"
2010 "%s, con->data_remaining=" CUPS_LLFMT
", con->file=%d",
2012 con
->http
.data_encoding
== HTTP_ENCODE_CHUNKED
?
2013 "CHUNKED" : "LENGTH",
2014 CUPS_LLCAST con
->http
.data_remaining
, con
->file
);
2021 * Grab any request data from the connection...
2024 if ((ipp_state
= ippRead(&(con
->http
), con
->request
)) == IPP_ERROR
)
2026 cupsdLogMessage(CUPSD_LOG_ERROR
,
2027 "cupsdReadClient: %d IPP Read Error!",
2030 cupsdSendError(con
, HTTP_BAD_REQUEST
, CUPSD_AUTH_NONE
);
2031 cupsdCloseClient(con
);
2034 else if (ipp_state
!= IPP_DATA
)
2036 if (con
->http
.state
== HTTP_POST_SEND
)
2038 cupsdSendError(con
, HTTP_BAD_REQUEST
, CUPSD_AUTH_NONE
);
2039 cupsdCloseClient(con
);
2046 con
->bytes
+= ippLength(con
->request
);
2049 if (con
->file
< 0 && con
->http
.state
!= HTTP_POST_SEND
)
2052 * Create a file as needed for the request data...
2055 cupsdSetStringf(&con
->filename
, "%s/%08x", RequestRoot
, request_id
++);
2056 con
->file
= open(con
->filename
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0640);
2060 cupsdLogMessage(CUPSD_LOG_ERROR
,
2061 "Unable to create request file %s: %s",
2062 con
->filename
, strerror(errno
));
2064 if (!cupsdSendError(con
, HTTP_REQUEST_TOO_LARGE
, CUPSD_AUTH_NONE
))
2066 cupsdCloseClient(con
);
2071 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdReadClient: %d REQUEST %s=%d", con
->http
.fd
,
2072 con
->filename
, con
->file
);
2074 fchmod(con
->file
, 0640);
2075 fchown(con
->file
, RunUser
, Group
);
2076 fcntl(con
->file
, F_SETFD
, fcntl(con
->file
, F_GETFD
) | FD_CLOEXEC
);
2079 if (con
->http
.state
!= HTTP_POST_SEND
)
2081 if ((bytes
= httpRead2(HTTP(con
), line
, sizeof(line
))) < 0)
2083 cupsdCloseClient(con
);
2088 con
->bytes
+= bytes
;
2090 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2091 "cupsdReadClient: %d writing %d bytes to %d",
2092 con
->http
.fd
, bytes
, con
->file
);
2094 if (write(con
->file
, line
, bytes
) < bytes
)
2096 cupsdLogMessage(CUPSD_LOG_ERROR
,
2097 "cupsdReadClient: Unable to write %d bytes to %s: %s",
2098 bytes
, con
->filename
, strerror(errno
));
2100 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2101 "cupsdReadClient: Closing file %d...",
2106 unlink(con
->filename
);
2107 cupsdClearString(&con
->filename
);
2109 if (!cupsdSendError(con
, HTTP_REQUEST_TOO_LARGE
, CUPSD_AUTH_NONE
))
2111 cupsdCloseClient(con
);
2116 else if (con
->http
.state
== HTTP_POST_RECV
)
2118 else if (con
->http
.state
!= HTTP_POST_SEND
)
2120 cupsdCloseClient(con
);
2125 while (con
->http
.state
== HTTP_POST_RECV
&& data_ready(con
));
2127 if (con
->http
.state
== HTTP_POST_SEND
)
2131 fstat(con
->file
, &filestats
);
2133 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2134 "cupsdReadClient: %d Closing data file %d, "
2135 "size=" CUPS_LLFMT
".",
2136 con
->http
.fd
, con
->file
,
2137 CUPS_LLCAST filestats
.st_size
);
2142 if (filestats
.st_size
> MaxRequestSize
&&
2146 * Request is too big; remove it and send an error...
2149 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2150 "cupsdReadClient: %d Removing temp file %s",
2151 con
->http
.fd
, con
->filename
);
2152 unlink(con
->filename
);
2153 cupsdClearString(&con
->filename
);
2158 * Delete any IPP request data...
2161 ippDelete(con
->request
);
2162 con
->request
= NULL
;
2165 if (!cupsdSendError(con
, HTTP_REQUEST_TOO_LARGE
, CUPSD_AUTH_NONE
))
2167 cupsdCloseClient(con
);
2174 if (!cupsdSendCommand(con
, con
->command
, con
->options
, 0))
2176 if (!cupsdSendError(con
, HTTP_NOT_FOUND
, CUPSD_AUTH_NONE
))
2178 cupsdCloseClient(con
);
2183 cupsdLogRequest(con
, HTTP_OK
);
2189 cupsdProcessIPPRequest(con
);
2193 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2194 "cupsdReadClient: %d Removing temp file %s",
2195 con
->http
.fd
, con
->filename
);
2196 unlink(con
->filename
);
2197 cupsdClearString(&con
->filename
);
2206 break; /* Anti-compiler-warning-code */
2209 if (con
->http
.state
== HTTP_WAITING
)
2211 if (!con
->http
.keep_alive
)
2212 cupsdCloseClient(con
);
2215 cupsArrayRemove(ActiveClients
, con
);
2216 cupsdSetBusyState();
2223 * 'cupsdSendCommand()' - Send output from a command via HTTP.
2226 int /* O - 1 on success, 0 on failure */
2228 cupsd_client_t
*con
, /* I - Client connection */
2229 char *command
, /* I - Command to run */
2230 char *options
, /* I - Command-line options */
2231 int root
) /* I - Run as root? */
2233 int fd
; /* Standard input file descriptor */
2238 fd
= open(con
->filename
, O_RDONLY
);
2242 cupsdLogMessage(CUPSD_LOG_ERROR
,
2243 "cupsdSendCommand: %d Unable to open \"%s\" for reading: %s",
2244 con
->http
.fd
, con
->filename
? con
->filename
: "/dev/null",
2249 fcntl(fd
, F_SETFD
, fcntl(fd
, F_GETFD
) | FD_CLOEXEC
);
2254 con
->pipe_pid
= pipe_command(con
, fd
, &(con
->file
), command
, options
, root
);
2259 cupsdLogMessage(CUPSD_LOG_INFO
, "Started \"%s\" (pid=%d)", command
,
2262 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdSendCommand: %d file=%d",
2263 con
->http
.fd
, con
->file
);
2265 if (con
->pipe_pid
== 0)
2268 fcntl(con
->file
, F_SETFD
, fcntl(con
->file
, F_GETFD
) | FD_CLOEXEC
);
2270 cupsdAddSelect(con
->file
, (cupsd_selfunc_t
)write_pipe
, NULL
, con
);
2272 con
->sent_header
= 0;
2273 con
->file_ready
= 0;
2274 con
->got_fields
= 0;
2282 * 'cupsdSendError()' - Send an error message via HTTP.
2285 int /* O - 1 if successful, 0 otherwise */
2286 cupsdSendError(cupsd_client_t
*con
, /* I - Connection */
2287 http_status_t code
, /* I - Error code */
2288 int auth_type
)/* I - Authentication type */
2292 * Force client to upgrade for authentication if that is how the
2293 * server is configured...
2296 if (code
== HTTP_UNAUTHORIZED
&&
2297 DefaultEncryption
== HTTP_ENCRYPT_REQUIRED
&&
2298 strcasecmp(con
->http
.hostname
, "localhost") &&
2301 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2302 "cupsdSendError: Encryption before authentication!");
2303 code
= HTTP_UPGRADE_REQUIRED
;
2305 #endif /* HAVE_SSL */
2308 * Put the request in the access_log file...
2311 cupsdLogRequest(con
, code
);
2313 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdSendError: %d code=%d (%s)",
2314 con
->http
.fd
, code
, httpStatus(code
));
2317 * To work around bugs in some proxies, don't use Keep-Alive for some
2320 * Kerberos authentication doesn't work without Keep-Alive, so
2321 * never disable it in that case.
2324 if (code
>= HTTP_BAD_REQUEST
&& con
->http
.auth_type
!= CUPSD_AUTH_NEGOTIATE
)
2325 con
->http
.keep_alive
= HTTP_KEEPALIVE_OFF
;
2328 * Send an error message back to the client. If the error code is a
2329 * 400 or 500 series, make sure the message contains some text, too!
2332 if (!cupsdSendHeader(con
, code
, NULL
, auth_type
))
2336 if (code
== HTTP_UPGRADE_REQUIRED
)
2337 if (httpPrintf(HTTP(con
), "Connection: Upgrade\r\n") < 0)
2340 if (httpPrintf(HTTP(con
), "Upgrade: TLS/1.0,HTTP/1.1\r\n") < 0)
2342 #endif /* HAVE_SSL */
2344 if (con
->http
.version
>= HTTP_1_1
&&
2345 con
->http
.keep_alive
== HTTP_KEEPALIVE_OFF
)
2347 if (httpPrintf(HTTP(con
), "Connection: close\r\n") < 0)
2351 if (code
>= HTTP_BAD_REQUEST
)
2354 * Send a human-readable error message.
2357 char message
[4096], /* Message for user */
2358 urltext
[1024], /* URL redirection text */
2359 redirect
[1024]; /* Redirection link */
2360 const char *text
; /* Status-specific text */
2365 if (code
== HTTP_UNAUTHORIZED
)
2366 text
= _cupsLangString(con
->language
,
2367 _("Enter your username and password or the "
2368 "root username and password to access this "
2369 "page. If you are using Kerberos authentication, "
2370 "make sure you have a valid Kerberos ticket."));
2371 else if (code
== HTTP_UPGRADE_REQUIRED
)
2375 snprintf(urltext
, sizeof(urltext
),
2376 _cupsLangString(con
->language
,
2377 _("You must access this page using the URL "
2378 "<A HREF=\"https://%s:%d%s\">"
2379 "https://%s:%d%s</A>.")),
2380 con
->servername
, con
->serverport
, con
->uri
,
2381 con
->servername
, con
->serverport
, con
->uri
);
2383 snprintf(redirect
, sizeof(redirect
),
2384 "<META HTTP-EQUIV=\"Refresh\" "
2385 "CONTENT=\"3;URL=https://%s:%d%s\">\n",
2386 con
->servername
, con
->serverport
, con
->uri
);
2391 snprintf(message
, sizeof(message
),
2392 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\" "
2393 "\"http://www.w3.org/TR/REC-html40/loose.dtd\">\n"
2396 "\t<META HTTP-EQUIV=\"Content-Type\" "
2397 "CONTENT=\"text/html; charset=utf-8\">\n"
2398 "\t<TITLE>%d %s</TITLE>\n"
2399 "\t<LINK REL=\"STYLESHEET\" TYPE=\"text/css\" "
2400 "HREF=\"/cups.css\">\n"
2408 code
, httpStatus(code
), redirect
, code
, httpStatus(code
), text
);
2410 if (httpPrintf(HTTP(con
), "Content-Type: text/html; charset=utf-8\r\n") < 0)
2412 if (httpPrintf(HTTP(con
), "Content-Length: %d\r\n",
2413 (int)strlen(message
)) < 0)
2415 if (httpPrintf(HTTP(con
), "\r\n") < 0)
2417 if (httpPrintf(HTTP(con
), "%s", message
) < 0)
2420 else if (httpPrintf(HTTP(con
), "\r\n") < 0)
2423 if (cupsdFlushHeader(con
) < 0)
2426 con
->http
.state
= HTTP_WAITING
;
2433 * 'cupsdSendHeader()' - Send an HTTP request.
2436 int /* O - 1 on success, 0 on failure */
2438 cupsd_client_t
*con
, /* I - Client to send to */
2439 http_status_t code
, /* I - HTTP status code */
2440 char *type
, /* I - MIME type of document */
2441 int auth_type
) /* I - Type of authentication */
2443 char auth_str
[1024]; /* Authorization string */
2445 static char *gss_buf
= NULL
; /* Kerberos auth data buffer */
2446 static int gss_bufsize
= 0; /* Size of Kerberos auth data buffer */
2447 #endif /* HAVE_GSSAPI */
2451 * Send the HTTP status header...
2454 if (code
== HTTP_CONTINUE
)
2457 * 100-continue doesn't send any headers...
2460 return (httpPrintf(HTTP(con
), "HTTP/%d.%d 100 Continue\r\n\r\n",
2461 con
->http
.version
/ 100, con
->http
.version
% 100) > 0);
2464 httpFlushWrite(HTTP(con
));
2466 con
->http
.data_encoding
= HTTP_ENCODE_FIELDS
;
2468 if (httpPrintf(HTTP(con
), "HTTP/%d.%d %d %s\r\n", con
->http
.version
/ 100,
2469 con
->http
.version
% 100, code
, httpStatus(code
)) < 0)
2471 if (httpPrintf(HTTP(con
), "Date: %s\r\n", httpGetDateString(time(NULL
))) < 0)
2474 if (httpPrintf(HTTP(con
), "Server: %s\r\n", ServerHeader
) < 0)
2476 if (con
->http
.keep_alive
&& con
->http
.version
>= HTTP_1_0
)
2478 if (httpPrintf(HTTP(con
), "Connection: Keep-Alive\r\n") < 0)
2480 if (httpPrintf(HTTP(con
), "Keep-Alive: timeout=%d\r\n",
2481 KeepAliveTimeout
) < 0)
2484 if (code
== HTTP_METHOD_NOT_ALLOWED
)
2485 if (httpPrintf(HTTP(con
), "Allow: GET, HEAD, OPTIONS, POST\r\n") < 0)
2488 if (code
== HTTP_UNAUTHORIZED
)
2490 if (auth_type
== CUPSD_AUTH_NONE
)
2492 if (!con
->best
|| con
->best
->type
<= CUPSD_AUTH_NONE
)
2493 auth_type
= DefaultAuthType
;
2495 auth_type
= con
->best
->type
;
2500 if (auth_type
== CUPSD_AUTH_BASIC
|| auth_type
== CUPSD_AUTH_BASICDIGEST
)
2501 strlcpy(auth_str
, "Basic realm=\"CUPS\"", sizeof(auth_str
));
2502 else if (auth_type
== CUPSD_AUTH_DIGEST
)
2503 snprintf(auth_str
, sizeof(auth_str
), "Digest realm=\"CUPS\", nonce=\"%s\"",
2504 con
->http
.hostname
);
2506 else if (auth_type
== CUPSD_AUTH_NEGOTIATE
&& con
->gss_output_token
.length
== 0)
2507 strlcpy(auth_str
, "Negotiate", sizeof(auth_str
));
2508 #endif /* HAVE_GSSAPI */
2510 #ifdef HAVE_AUTHORIZATION_H
2511 if (con
->best
&& auth_type
!= CUPSD_AUTH_NEGOTIATE
)
2513 int i
; /* Looping var */
2514 char *auth_key
; /* Auth key buffer */
2515 size_t auth_size
; /* Size of remaining buffer */
2518 auth_key
= auth_str
+ strlen(auth_str
);
2519 auth_size
= sizeof(auth_str
) - (auth_key
- auth_str
);
2521 for (i
= 0; i
< con
->best
->num_names
; i
++)
2523 if (!strncasecmp(con
->best
->names
[i
], "@AUTHKEY(", 9))
2525 snprintf(auth_key
, auth_size
, ", authkey=\"%s\"",
2526 con
->best
->names
[i
] + 9);
2527 /* end parenthesis is stripped in conf.c */
2530 else if (!strcasecmp(con
->best
->names
[i
], "@SYSTEM") &&
2533 snprintf(auth_key
, auth_size
, ", authkey=\"%s\"", SystemGroupAuthKey
);
2538 #endif /* HAVE_AUTHORIZATION_H */
2542 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdSendHeader: WWW-Authenticate: %s",
2545 if (httpPrintf(HTTP(con
), "WWW-Authenticate: %s\r\n", auth_str
) < 0)
2552 * WWW-Authenticate: Negotiate can be included even for
2553 * non-401 replies...
2556 if (con
->gss_output_token
.length
> 0 && con
->gss_output_token
.length
<= 65536)
2558 OM_uint32 minor_status
; /* Minor status code */
2559 int bufsize
; /* Size of output token buffer */
2562 bufsize
= con
->gss_output_token
.length
* 4 / 3 + 2;
2564 if (bufsize
> gss_bufsize
)
2566 char *buf
; /* New buffer */
2569 bufsize
= (bufsize
+ 1023) & 1023;/* Round up */
2572 buf
= realloc(gss_buf
, bufsize
);
2574 buf
= malloc(bufsize
);
2578 cupsdLogMessage(CUPSD_LOG_ERROR
,
2579 "Unable to allocate %d bytes for Kerberos credentials!",
2585 gss_bufsize
= bufsize
;
2588 httpEncode64_2(gss_buf
, gss_bufsize
,
2589 con
->gss_output_token
.value
,
2590 con
->gss_output_token
.length
);
2591 gss_release_buffer(&minor_status
, &con
->gss_output_token
);
2593 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2594 "cupsdSendHeader: WWW-Authenticate: Negotiate %s", gss_buf
);
2596 if (httpPrintf(HTTP(con
), "WWW-Authenticate: Negotiate %s\r\n",
2600 else if (con
->gss_output_token
.length
> 65536)
2602 cupsdLogMessage(CUPSD_LOG_ERROR
,
2603 "Kerberos credentials larger than 64k (%d)!",
2604 (int)con
->gss_output_token
.length
);
2607 #endif /* HAVE_GSSAPI */
2609 if (con
->language
&& strcmp(con
->language
->language
, "C"))
2611 if (httpPrintf(HTTP(con
), "Content-Language: %s\r\n",
2612 con
->language
->language
) < 0)
2618 if (!strcmp(type
, "text/html"))
2620 if (httpPrintf(HTTP(con
),
2621 "Content-Type: text/html; charset=utf-8\r\n") < 0)
2624 else if (httpPrintf(HTTP(con
), "Content-Type: %s\r\n", type
) < 0)
2633 * 'cupsdUpdateCGI()' - Read status messages from CGI scripts and programs.
2637 cupsdUpdateCGI(void)
2639 char *ptr
, /* Pointer to end of line in buffer */
2640 message
[1024]; /* Pointer to message text */
2641 int loglevel
; /* Log level for message */
2644 while ((ptr
= cupsdStatBufUpdate(CGIStatusBuffer
, &loglevel
,
2645 message
, sizeof(message
))) != NULL
)
2646 if (!strchr(CGIStatusBuffer
->buffer
, '\n'))
2649 if (ptr
== NULL
&& !CGIStatusBuffer
->bufused
)
2652 * Fatal error on pipe - should never happen!
2655 cupsdLogMessage(CUPSD_LOG_CRIT
,
2656 "cupsdUpdateCGI: error reading from CGI error pipe - %s",
2663 * 'cupsdWriteClient()' - Write data to a client as needed.
2667 cupsdWriteClient(cupsd_client_t
*con
) /* I - Client connection */
2669 int bytes
; /* Number of bytes written */
2670 char buf
[16385]; /* Data buffer */
2671 char *bufptr
; /* Pointer into buffer */
2672 ipp_state_t ipp_state
; /* IPP state value */
2676 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2677 "cupsdWriteClient(con=%p) %d response=%p(%d), file=%d "
2678 "pipe_pid=%d state=%d",
2679 con
, con
->http
.fd
, con
->response
,
2680 con
->response
? con
->response
->state
: -1,
2681 con
->file
, con
->pipe_pid
, con
->http
.state
);
2684 if (con
->http
.state
!= HTTP_GET_SEND
&&
2685 con
->http
.state
!= HTTP_POST_SEND
)
2691 * Make sure we select on the CGI output...
2694 cupsdAddSelect(con
->file
, (cupsd_selfunc_t
)write_pipe
, NULL
, con
);
2696 if (!con
->file_ready
)
2699 * Try again later when there is CGI output available...
2702 cupsdRemoveSelect(con
->http
.fd
);
2706 con
->file_ready
= 0;
2709 if (con
->response
&& con
->response
->state
!= IPP_DATA
)
2711 ipp_state
= ippWrite(HTTP(con
), con
->response
);
2712 bytes
= ipp_state
!= IPP_ERROR
&&
2713 (con
->file
>= 0 || ipp_state
!= IPP_DATA
);
2715 else if ((bytes
= read(con
->file
, buf
, sizeof(buf
) - 1)) > 0)
2717 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2718 "cupsdWriteClient: Read %d bytes from file %d...",
2721 if (con
->pipe_pid
&& !con
->got_fields
)
2724 * Inspect the data for Content-Type and other fields.
2729 for (bufptr
= buf
; !con
->got_fields
&& *bufptr
; bufptr
++)
2730 if (*bufptr
== '\n')
2733 * Send line to client...
2736 if (bufptr
> buf
&& bufptr
[-1] == '\r')
2740 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "Script header: %s", buf
);
2742 if (!con
->sent_header
)
2745 * Handle redirection and CGI status codes...
2748 if (!strncasecmp(buf
, "Location:", 9))
2750 cupsdSendHeader(con
, HTTP_SEE_OTHER
, NULL
, CUPSD_AUTH_NONE
);
2751 con
->sent_header
= 2;
2753 if (httpPrintf(HTTP(con
), "Content-Length: 0\r\n") < 0)
2756 else if (!strncasecmp(buf
, "Status:", 7))
2758 cupsdSendError(con
, (http_status_t
)atoi(buf
+ 7), CUPSD_AUTH_NONE
);
2759 con
->sent_header
= 2;
2763 cupsdSendHeader(con
, HTTP_OK
, NULL
, CUPSD_AUTH_NONE
);
2764 con
->sent_header
= 1;
2766 if (con
->http
.version
== HTTP_1_1
)
2768 if (httpPrintf(HTTP(con
), "Transfer-Encoding: chunked\r\n") < 0)
2774 if (strncasecmp(buf
, "Status:", 7))
2775 httpPrintf(HTTP(con
), "%s\r\n", buf
);
2777 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdWriteClient: %d %s",
2784 bytes
-= (bufptr
- buf
);
2785 memmove(buf
, bufptr
, bytes
+ 1);
2789 * See if the line was empty...
2792 if (con
->field_col
== 0)
2794 con
->got_fields
= 1;
2796 if (cupsdFlushHeader(con
) < 0)
2798 cupsdCloseClient(con
);
2802 if (con
->http
.version
== HTTP_1_1
)
2803 con
->http
.data_encoding
= HTTP_ENCODE_CHUNKED
;
2808 else if (*bufptr
!= '\r')
2811 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2812 "cupsdWriteClient: %d bytes=%d, got_fields=%d",
2813 con
->http
.fd
, bytes
, con
->got_fields
);
2815 if (bytes
> 0 && !con
->got_fields
)
2818 * Remaining text needs to go out...
2821 httpPrintf(HTTP(con
), "%s", buf
);
2823 con
->http
.activity
= time(NULL
);
2826 else if (bytes
== 0)
2828 con
->http
.activity
= time(NULL
);
2835 if (httpWrite2(HTTP(con
), buf
, bytes
) < 0)
2837 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2838 "cupsdWriteClient: %d Write of %d bytes failed!",
2839 con
->http
.fd
, bytes
);
2841 cupsdCloseClient(con
);
2845 if (con
->http
.data_encoding
== HTTP_ENCODE_CHUNKED
)
2846 httpFlushWrite(HTTP(con
));
2848 con
->bytes
+= bytes
;
2850 if (con
->http
.state
== HTTP_WAITING
)
2857 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdWriteClient: %d bytes < 0",
2860 cupsdLogRequest(con
, HTTP_OK
);
2862 httpFlushWrite(HTTP(con
));
2864 if (con
->http
.data_encoding
== HTTP_ENCODE_CHUNKED
&& con
->sent_header
== 1)
2866 if (httpWrite2(HTTP(con
), "", 0) < 0)
2868 cupsdCloseClient(con
);
2873 con
->http
.state
= HTTP_WAITING
;
2875 cupsdAddSelect(con
->http
.fd
, (cupsd_selfunc_t
)cupsdReadClient
, NULL
, con
);
2879 cupsdRemoveSelect(con
->file
);
2882 cupsdEndProcess(con
->pipe_pid
, 0);
2884 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2885 "cupsdWriteClient: %d Closing data file %d.",
2886 con
->http
.fd
, con
->file
);
2895 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2896 "cupsdWriteClient: %d Removing temp file %s",
2897 con
->http
.fd
, con
->filename
);
2898 unlink(con
->filename
);
2899 cupsdClearString(&con
->filename
);
2904 ippDelete(con
->request
);
2905 con
->request
= NULL
;
2910 ippDelete(con
->response
);
2911 con
->response
= NULL
;
2914 cupsdClearString(&con
->command
);
2915 cupsdClearString(&con
->options
);
2916 cupsdClearString(&con
->query_string
);
2918 if (!con
->http
.keep_alive
)
2920 cupsdCloseClient(con
);
2925 con
->http
.activity
= time(NULL
);
2930 * 'check_if_modified()' - Decode an "If-Modified-Since" line.
2933 static int /* O - 1 if modified since */
2935 cupsd_client_t
*con
, /* I - Client connection */
2936 struct stat
*filestats
) /* I - File information */
2938 char *ptr
; /* Pointer into field */
2939 time_t date
; /* Time/date value */
2940 off_t size
; /* Size/length value */
2945 ptr
= con
->http
.fields
[HTTP_FIELD_IF_MODIFIED_SINCE
];
2950 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2951 "check_if_modified: %d If-Modified-Since=\"%s\"",
2954 while (*ptr
!= '\0')
2956 while (isspace(*ptr
) || *ptr
== ';')
2959 if (strncasecmp(ptr
, "length=", 7) == 0)
2962 size
= strtoll(ptr
, NULL
, 10);
2964 while (isdigit(*ptr
))
2967 else if (isalpha(*ptr
))
2969 date
= httpGetDateTime(ptr
);
2970 while (*ptr
!= '\0' && *ptr
!= ';')
2977 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2978 "check_if_modified: %d sizes=" CUPS_LLFMT
","
2979 CUPS_LLFMT
" dates=%d,%d",
2980 con
->http
.fd
, CUPS_LLCAST size
,
2981 CUPS_LLCAST filestats
->st_size
, (int)date
,
2982 (int)filestats
->st_mtime
);
2984 return ((size
!= filestats
->st_size
&& size
!= 0) ||
2985 (date
< filestats
->st_mtime
&& date
!= 0) ||
2986 (size
== 0 && date
== 0));
2991 * 'compare_clients()' - Compare two client connections.
2994 static int /* O - Result of comparison */
2995 compare_clients(cupsd_client_t
*a
, /* I - First client */
2996 cupsd_client_t
*b
, /* I - Second client */
2997 void *data
) /* I - User data (not used) */
3011 * 'data_ready()' - Check whether data is available from a client.
3014 static int /* O - 1 if data is ready, 0 otherwise */
3015 data_ready(cupsd_client_t
*con
) /* I - Client */
3017 if (con
->http
.used
> 0)
3020 else if (con
->http
.tls
)
3023 if (SSL_pending((SSL
*)(con
->http
.tls
)))
3025 # elif defined(HAVE_GNUTLS)
3026 if (gnutls_record_check_pending(((http_tls_t
*)(con
->http
.tls
))->session
))
3028 # elif defined(HAVE_CDSASSL)
3029 size_t bytes
; /* Bytes that are available */
3031 if (!SSLGetBufferedReadSize(((http_tls_t
*)(con
->http
.tls
))->session
,
3032 &bytes
) && bytes
> 0)
3034 # endif /* HAVE_LIBSSL */
3036 #endif /* HAVE_SSL */
3044 * 'encrypt_client()' - Enable encryption for the client...
3047 static int /* O - 1 on success, 0 on error */
3048 encrypt_client(cupsd_client_t
*con
) /* I - Client to encrypt */
3051 SSL_CTX
*context
; /* Context for encryption */
3052 SSL
*conn
; /* Connection for encryption */
3053 BIO
*bio
; /* BIO data */
3054 unsigned long error
; /* Error code */
3058 * Verify that we have a certificate...
3061 if (access(ServerKey
, 0) || access(ServerCertificate
, 0))
3064 * Nope, make a self-signed certificate...
3067 if (!make_certificate(con
))
3072 * Create the SSL context and accept the connection...
3075 context
= SSL_CTX_new(SSLv23_server_method());
3077 SSL_CTX_set_options(context
, SSL_OP_NO_SSLv2
); /* Only use SSLv3 or TLS */
3078 SSL_CTX_use_PrivateKey_file(context
, ServerKey
, SSL_FILETYPE_PEM
);
3079 SSL_CTX_use_certificate_chain_file(context
, ServerCertificate
,
3082 bio
= BIO_new(_httpBIOMethods());
3083 BIO_ctrl(bio
, BIO_C_SET_FILE_PTR
, 0, (char *)HTTP(con
));
3085 conn
= SSL_new(context
);
3086 SSL_set_bio(conn
, bio
, bio
);
3088 if (SSL_accept(conn
) != 1)
3090 cupsdLogMessage(CUPSD_LOG_ERROR
,
3091 "encrypt_client: Unable to encrypt connection from %s!",
3092 con
->http
.hostname
);
3094 while ((error
= ERR_get_error()) != 0)
3095 cupsdLogMessage(CUPSD_LOG_ERROR
, "encrypt_client: %s",
3096 ERR_error_string(error
, NULL
));
3098 SSL_CTX_free(context
);
3103 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3104 "encrypt_client: %d Connection from %s now encrypted.",
3105 con
->http
.fd
, con
->http
.hostname
);
3107 con
->http
.tls
= conn
;
3110 # elif defined(HAVE_GNUTLS)
3111 http_tls_t
*conn
; /* TLS session object */
3112 int error
; /* Error code */
3113 gnutls_certificate_server_credentials
*credentials
;
3114 /* TLS credentials */
3118 * Verify that we have a certificate...
3121 if (access(ServerKey
, 0) || access(ServerCertificate
, 0))
3124 * Nope, make a self-signed certificate...
3127 if (!make_certificate(con
))
3132 * Create the SSL object and perform the SSL handshake...
3135 conn
= (http_tls_t
*)malloc(sizeof(http_tls_t
));
3140 credentials
= (gnutls_certificate_server_credentials
*)
3141 malloc(sizeof(gnutls_certificate_server_credentials
));
3142 if (credentials
== NULL
)
3144 cupsdLogMessage(CUPSD_LOG_ERROR
,
3145 "encrypt_client: Unable to encrypt connection from %s!",
3146 con
->http
.hostname
);
3147 cupsdLogMessage(CUPSD_LOG_ERROR
, "encrypt_client: %s", strerror(errno
));
3153 gnutls_certificate_allocate_credentials(credentials
);
3154 gnutls_certificate_set_x509_key_file(*credentials
, ServerCertificate
,
3155 ServerKey
, GNUTLS_X509_FMT_PEM
);
3157 gnutls_init(&(conn
->session
), GNUTLS_SERVER
);
3158 gnutls_set_default_priority(conn
->session
);
3159 gnutls_credentials_set(conn
->session
, GNUTLS_CRD_CERTIFICATE
, *credentials
);
3160 gnutls_transport_set_ptr(conn
->session
, (gnutls_transport_ptr
)HTTP(con
));
3161 gnutls_transport_set_pull_function(conn
->session
, _httpReadGNUTLS
);
3162 gnutls_transport_set_push_function(conn
->session
, _httpWriteGNUTLS
);
3164 error
= gnutls_handshake(conn
->session
);
3166 if (error
!= GNUTLS_E_SUCCESS
)
3168 cupsdLogMessage(CUPSD_LOG_ERROR
,
3169 "encrypt_client: Unable to encrypt connection from %s!",
3170 con
->http
.hostname
);
3171 cupsdLogMessage(CUPSD_LOG_ERROR
, "encrypt_client: %s",
3172 gnutls_strerror(error
));
3174 gnutls_deinit(conn
->session
);
3175 gnutls_certificate_free_credentials(*credentials
);
3181 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3182 "encrypt_client: %d Connection from %s now encrypted.",
3183 con
->http
.fd
, con
->http
.hostname
);
3185 conn
->credentials
= credentials
;
3186 con
->http
.tls
= conn
;
3189 # elif defined(HAVE_CDSASSL)
3190 OSStatus error
; /* Error code */
3191 http_tls_t
*conn
; /* CDSA connection information */
3194 if ((conn
= (http_tls_t
*)malloc(sizeof(http_tls_t
))) == NULL
)
3198 conn
->session
= NULL
;
3199 conn
->certsArray
= get_cdsa_certificate(con
);
3201 if (!conn
->certsArray
)
3204 * No keychain (yet), make a self-signed certificate...
3207 if (make_certificate(con
))
3208 conn
->certsArray
= get_cdsa_certificate(con
);
3211 if (!conn
->certsArray
)
3213 cupsdLogMessage(CUPSD_LOG_ERROR
,
3214 "encrypt_client: Could not find signing key in keychain "
3215 "\"%s\"", ServerCertificate
);
3216 error
= errSSLBadCert
; /* errSSLBadConfiguration is a better choice, but not available on 10.2.x */
3220 error
= SSLNewContext(true, &conn
->session
);
3223 error
= SSLSetIOFuncs(conn
->session
, _httpReadCDSA
, _httpWriteCDSA
);
3226 error
= SSLSetProtocolVersionEnabled(conn
->session
, kSSLProtocol2
, false);
3229 error
= SSLSetConnection(conn
->session
, HTTP(con
));
3232 error
= SSLSetAllowsExpiredCerts(conn
->session
, true);
3235 error
= SSLSetAllowsAnyRoot(conn
->session
, true);
3238 error
= SSLSetCertificate(conn
->session
, conn
->certsArray
);
3243 * Perform SSL/TLS handshake
3246 while ((error
= SSLHandshake(conn
->session
)) == errSSLWouldBlock
)
3252 cupsdLogMessage(CUPSD_LOG_ERROR
,
3253 "encrypt_client: Unable to encrypt connection from %s!",
3254 con
->http
.hostname
);
3256 cupsdLogMessage(CUPSD_LOG_ERROR
, "encrypt_client: %s (%d)",
3257 cssmErrorString(error
), (int)error
);
3259 con
->http
.error
= error
;
3260 con
->http
.status
= HTTP_ERROR
;
3263 SSLDisposeContext(conn
->session
);
3265 if (conn
->certsArray
)
3266 CFRelease(conn
->certsArray
);
3273 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3274 "encrypt_client: %d Connection from %s now encrypted.",
3275 con
->http
.fd
, con
->http
.hostname
);
3277 con
->http
.tls
= conn
;
3280 # endif /* HAVE_LIBSSL */
3282 #endif /* HAVE_SSL */
3287 * 'get_cdsa_certificate()' - Get a SSL/TLS certificate from the System keychain.
3290 static CFArrayRef
/* O - Array of certificates */
3291 get_cdsa_certificate(cupsd_client_t
*con
) /* I - Client connection */
3293 OSStatus err
; /* Error info */
3294 SecKeychainRef keychain
; /* Keychain reference */
3295 SecIdentitySearchRef search
; /* Search reference */
3296 SecIdentityRef identity
; /* Identity */
3297 CFArrayRef certificates
= NULL
;
3298 /* Certificate array */
3301 if ((err
= SecKeychainOpen(ServerCertificate
, &keychain
)))
3303 cupsdLogMessage(CUPSD_LOG_ERROR
, "Cannot open keychain \"%s\", %s",
3304 ServerCertificate
, cssmErrorString(err
));
3308 # if HAVE_SECIDENTITYSEARCHCREATEWITHPOLICY
3310 * Use a policy to search for valid certificates who's common name matches the
3314 SecPolicySearchRef policy_search
; /* Policy search ref */
3315 SecPolicyRef policy
; /* Policy ref */
3316 CSSM_DATA options
; /* Policy options */
3317 CSSM_APPLE_TP_SSL_OPTIONS
3318 ssl_options
; /* SSL Option for hostname */
3321 if (SecPolicySearchCreate(CSSM_CERT_X_509v3
, &CSSMOID_APPLE_TP_SSL
,
3322 NULL
, &policy_search
))
3324 cupsdLogMessage(CUPSD_LOG_ERROR
, "Cannot create a policy search reference");
3325 CFRelease(keychain
);
3329 if (SecPolicySearchCopyNext(policy_search
, &policy
))
3331 cupsdLogMessage(CUPSD_LOG_ERROR
,
3332 "Cannot find a policy to use for searching");
3333 CFRelease(keychain
);
3334 CFRelease(policy_search
);
3338 memset(&ssl_options
, 0, sizeof(ssl_options
));
3339 ssl_options
.Version
= CSSM_APPLE_TP_SSL_OPTS_VERSION
;
3340 ssl_options
.ServerName
= con
->servername
;
3341 ssl_options
.ServerNameLen
= strlen(con
->servername
);
3343 options
.Data
= (uint8
*)&ssl_options
;
3344 options
.Length
= sizeof(ssl_options
);
3346 if (SecPolicySetValue(policy
, &options
))
3348 cupsdLogMessage(CUPSD_LOG_ERROR
,
3349 "Cannot set policy value to use for searching");
3350 CFRelease(keychain
);
3351 CFRelease(policy_search
);
3355 err
= SecIdentitySearchCreateWithPolicy(policy
, NULL
, CSSM_KEYUSE_SIGN
,
3356 keychain
, FALSE
, &search
);
3359 * Assume there is exactly one SecIdentity in the keychain...
3362 err
= SecIdentitySearchCreate(keychain
, CSSM_KEYUSE_SIGN
, &search
);
3363 # endif /* HAVE_SECIDENTITYSEARCHCREATEWITHPOLICY */
3366 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3367 "Cannot create keychain search reference: %s",
3368 cssmErrorString(err
));
3371 if ((err
= SecIdentitySearchCopyNext(search
, &identity
)))
3373 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3374 "Cannot find signing key in keychain \"%s\", error %d",
3375 ServerCertificate
, (int)err
);
3379 if (CFGetTypeID(identity
) != SecIdentityGetTypeID())
3380 cupsdLogMessage(CUPSD_LOG_ERROR
,
3381 "SecIdentitySearchCopyNext CFTypeID failure!");
3384 if ((certificates
= CFArrayCreate(NULL
, (const void **)&identity
,
3385 1, &kCFTypeArrayCallBacks
)) == NULL
)
3386 cupsdLogMessage(CUPSD_LOG_ERROR
, "Cannot create certificate array");
3389 CFRelease(identity
);
3395 # if HAVE_SECIDENTITYSEARCHCREATEWITHPOLICY
3397 CFRelease(policy_search
);
3398 # endif /* HAVE_SECIDENTITYSEARCHCREATEWITHPOLICY */
3400 return (certificates
);
3402 #endif /* HAVE_CDSASSL */
3406 * 'get_file()' - Get a filename and state info.
3409 static char * /* O - Real filename */
3410 get_file(cupsd_client_t
*con
, /* I - Client connection */
3411 struct stat
*filestats
, /* O - File information */
3412 char *filename
, /* IO - Filename buffer */
3413 int len
) /* I - Buffer length */
3415 int status
; /* Status of filesystem calls */
3416 char *ptr
; /* Pointer info filename */
3417 int plen
; /* Remaining length after pointer */
3418 char language
[7]; /* Language subdirectory, if any */
3422 * Figure out the real filename...
3427 if (!strncmp(con
->uri
, "/ppd/", 5))
3428 snprintf(filename
, len
, "%s%s", ServerRoot
, con
->uri
);
3429 else if (!strncmp(con
->uri
, "/rss/", 5) && !strchr(con
->uri
+ 5, '/'))
3430 snprintf(filename
, len
, "%s/rss/%s", CacheDir
, con
->uri
+ 5);
3431 else if (!strncmp(con
->uri
, "/admin/conf/", 12))
3432 snprintf(filename
, len
, "%s%s", ServerRoot
, con
->uri
+ 11);
3433 else if (!strncmp(con
->uri
, "/admin/log/", 11))
3435 if (!strncmp(con
->uri
+ 11, "access_log", 10) && AccessLog
[0] == '/')
3436 strlcpy(filename
, AccessLog
, len
);
3437 else if (!strncmp(con
->uri
+ 11, "error_log", 9) && ErrorLog
[0] == '/')
3438 strlcpy(filename
, ErrorLog
, len
);
3439 else if (!strncmp(con
->uri
+ 11, "page_log", 8) && PageLog
[0] == '/')
3440 strlcpy(filename
, PageLog
, len
);
3444 else if (con
->language
)
3446 snprintf(language
, sizeof(language
), "/%s", con
->language
->language
);
3447 snprintf(filename
, len
, "%s%s%s", DocumentRoot
, language
, con
->uri
);
3450 snprintf(filename
, len
, "%s%s", DocumentRoot
, con
->uri
);
3452 if ((ptr
= strchr(filename
, '?')) != NULL
)
3456 * Grab the status for this language; if there isn't a language-specific file
3457 * then fallback to the default one...
3460 if ((status
= stat(filename
, filestats
)) != 0 && language
[0] &&
3461 strncmp(con
->uri
, "/ppd/", 5) &&
3462 strncmp(con
->uri
, "/admin/conf/", 12) &&
3463 strncmp(con
->uri
, "/admin/log/", 11))
3466 * Drop the country code...
3470 snprintf(filename
, len
, "%s%s%s", DocumentRoot
, language
, con
->uri
);
3472 if ((ptr
= strchr(filename
, '?')) != NULL
)
3475 if ((status
= stat(filename
, filestats
)) != 0)
3478 * Drop the language prefix and try the root directory...
3482 snprintf(filename
, len
, "%s%s", DocumentRoot
, con
->uri
);
3484 if ((ptr
= strchr(filename
, '?')) != NULL
)
3487 status
= stat(filename
, filestats
);
3492 * If we're found a directory, get the index.html file instead...
3495 if (!status
&& S_ISDIR(filestats
->st_mode
))
3498 * Make sure the URI ends with a slash...
3501 if (con
->uri
[strlen(con
->uri
) - 1] != '/')
3502 strlcat(con
->uri
, "/", sizeof(con
->uri
));
3505 * Find the directory index file, trying every language...
3510 if (status
&& language
[0])
3513 * Try a different language subset...
3517 language
[0] = '\0'; /* Strip country code */
3519 language
[0] = '\0'; /* Strip language */
3523 * Look for the index file...
3526 snprintf(filename
, len
, "%s%s%s", DocumentRoot
, language
, con
->uri
);
3528 if ((ptr
= strchr(filename
, '?')) != NULL
)
3531 ptr
= filename
+ strlen(filename
);
3532 plen
= len
- (ptr
- filename
);
3534 strlcpy(ptr
, "index.html", plen
);
3535 status
= stat(filename
, filestats
);
3540 strlcpy(ptr
, "index.class", plen
);
3541 status
= stat(filename
, filestats
);
3543 #endif /* HAVE_JAVA */
3548 strlcpy(ptr
, "index.pl", plen
);
3549 status
= stat(filename
, filestats
);
3551 #endif /* HAVE_PERL */
3556 strlcpy(ptr
, "index.php", plen
);
3557 status
= stat(filename
, filestats
);
3559 #endif /* HAVE_PHP */
3564 strlcpy(ptr
, "index.pyc", plen
);
3565 status
= stat(filename
, filestats
);
3570 strlcpy(ptr
, "index.py", plen
);
3571 status
= stat(filename
, filestats
);
3573 #endif /* HAVE_PYTHON */
3576 while (status
&& language
[0]);
3579 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_file: %d filename=%s size=%d",
3580 con
->http
.fd
, filename
,
3581 status
? -1 : (int)filestats
->st_size
);
3584 con
->http
.data_remaining
= (int)filestats
->st_size
;
3594 * 'install_conf_file()' - Install a configuration file.
3597 static http_status_t
/* O - Status */
3598 install_conf_file(cupsd_client_t
*con
) /* I - Connection */
3600 cups_file_t
*in
, /* Input file */
3601 *out
; /* Output file */
3602 char buffer
[1024]; /* Copy buffer */
3603 int bytes
; /* Number of bytes */
3604 char conffile
[1024], /* Configuration filename */
3605 newfile
[1024], /* New config filename */
3606 oldfile
[1024]; /* Old config filename */
3607 struct stat confinfo
; /* Config file info */
3611 * First construct the filenames...
3614 snprintf(conffile
, sizeof(conffile
), "%s%s", ServerRoot
, con
->uri
+ 11);
3615 snprintf(newfile
, sizeof(newfile
), "%s%s.N", ServerRoot
, con
->uri
+ 11);
3616 snprintf(oldfile
, sizeof(oldfile
), "%s%s.O", ServerRoot
, con
->uri
+ 11);
3618 cupsdLogMessage(CUPSD_LOG_INFO
, "Installing config file \"%s\"...", conffile
);
3621 * Get the owner, group, and permissions of the configuration file.
3622 * If it doesn't exist, assign it to the User and Group in the
3623 * cupsd.conf file with mode 0640 permissions.
3626 if (stat(conffile
, &confinfo
))
3628 confinfo
.st_uid
= User
;
3629 confinfo
.st_gid
= Group
;
3630 confinfo
.st_mode
= ConfigFilePerm
;
3634 * Open the request file and new config file...
3637 if ((in
= cupsFileOpen(con
->filename
, "rb")) == NULL
)
3639 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to open request file \"%s\" - %s",
3640 con
->filename
, strerror(errno
));
3641 return (HTTP_SERVER_ERROR
);
3644 if ((out
= cupsFileOpen(newfile
, "wb")) == NULL
)
3647 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to open config file \"%s\" - %s",
3648 newfile
, strerror(errno
));
3649 return (HTTP_SERVER_ERROR
);
3652 fchmod(cupsFileNumber(out
), confinfo
.st_mode
);
3653 fchown(cupsFileNumber(out
), confinfo
.st_uid
, confinfo
.st_gid
);
3656 * Copy from the request to the new config file...
3659 while ((bytes
= cupsFileRead(in
, buffer
, sizeof(buffer
))) > 0)
3660 if (cupsFileWrite(out
, buffer
, bytes
) < bytes
)
3662 cupsdLogMessage(CUPSD_LOG_ERROR
,
3663 "Unable to copy to config file \"%s\" - %s",
3664 newfile
, strerror(errno
));
3670 return (HTTP_SERVER_ERROR
);
3674 * Close the files...
3678 if (cupsFileClose(out
))
3680 cupsdLogMessage(CUPSD_LOG_ERROR
,
3681 "Error file closing config file \"%s\" - %s",
3682 newfile
, strerror(errno
));
3686 return (HTTP_SERVER_ERROR
);
3690 * Remove the request file...
3693 unlink(con
->filename
);
3694 cupsdClearString(&con
->filename
);
3697 * Unlink the old backup, rename the current config file to the backup
3698 * filename, and rename the new config file to the config file name...
3701 if (unlink(oldfile
))
3702 if (errno
!= ENOENT
)
3704 cupsdLogMessage(CUPSD_LOG_ERROR
,
3705 "Unable to remove backup config file \"%s\" - %s",
3706 oldfile
, strerror(errno
));
3710 return (HTTP_SERVER_ERROR
);
3713 if (rename(conffile
, oldfile
))
3714 if (errno
!= ENOENT
)
3716 cupsdLogMessage(CUPSD_LOG_ERROR
,
3717 "Unable to rename old config file \"%s\" - %s",
3718 conffile
, strerror(errno
));
3722 return (HTTP_SERVER_ERROR
);
3725 if (rename(newfile
, conffile
))
3727 cupsdLogMessage(CUPSD_LOG_ERROR
,
3728 "Unable to rename new config file \"%s\" - %s",
3729 newfile
, strerror(errno
));
3731 rename(oldfile
, conffile
);
3734 return (HTTP_SERVER_ERROR
);
3738 * If the cupsd.conf file was updated, set the NeedReload flag...
3741 if (!strcmp(con
->uri
, "/admin/conf/cupsd.conf"))
3742 NeedReload
= RELOAD_CUPSD
;
3744 NeedReload
= RELOAD_ALL
;
3746 ReloadTime
= time(NULL
);
3749 * Return that the file was created successfully...
3752 return (HTTP_CREATED
);
3757 * 'is_cgi()' - Is the resource a CGI script/program?
3760 static int /* O - 1 = CGI, 0 = file */
3761 is_cgi(cupsd_client_t
*con
, /* I - Client connection */
3762 const char *filename
, /* I - Real filename */
3763 struct stat
*filestats
, /* I - File information */
3764 mime_type_t
*type
) /* I - MIME type */
3766 const char *options
; /* Options on URL */
3769 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3770 "is_cgi(con=%p, filename=\"%s\", filestats=%p, type=%s/%s)",
3771 con
, filename
, filestats
, type
? type
->super
: "unknown",
3772 type
? type
->type
: "unknown");
3775 * Get the options, if any...
3778 if ((options
= strchr(con
->uri
, '?')) != NULL
)
3781 cupsdSetStringf(&(con
->query_string
), "QUERY_STRING=%s", options
);
3785 * Check for known types...
3788 if (!type
|| strcasecmp(type
->super
, "application"))
3790 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "is_cgi: Returning 0...");
3794 if (!strcasecmp(type
->type
, "x-httpd-cgi") &&
3795 (filestats
->st_mode
& 0111))
3798 * "application/x-httpd-cgi" is a CGI script.
3801 cupsdSetString(&con
->command
, filename
);
3804 cupsdSetStringf(&con
->options
, " %s", options
);
3806 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3807 "is_cgi: Returning 1 with command=\"%s\" and options=\"%s\"",
3808 con
->command
, con
->options
);
3813 else if (!strcasecmp(type
->type
, "x-httpd-java"))
3816 * "application/x-httpd-java" is a Java servlet.
3819 cupsdSetString(&con
->command
, CUPS_JAVA
);
3822 cupsdSetStringf(&con
->options
, " %s %s", filename
, options
);
3824 cupsdSetStringf(&con
->options
, " %s", filename
);
3826 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3827 "is_cgi: Returning 1 with command=\"%s\" and options=\"%s\"",
3828 con
->command
, con
->options
);
3832 #endif /* HAVE_JAVA */
3834 else if (!strcasecmp(type
->type
, "x-httpd-perl"))
3837 * "application/x-httpd-perl" is a Perl page.
3840 cupsdSetString(&con
->command
, CUPS_PERL
);
3843 cupsdSetStringf(&con
->options
, " %s %s", filename
, options
);
3845 cupsdSetStringf(&con
->options
, " %s", filename
);
3847 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3848 "is_cgi: Returning 1 with command=\"%s\" and options=\"%s\"",
3849 con
->command
, con
->options
);
3853 #endif /* HAVE_PERL */
3855 else if (!strcasecmp(type
->type
, "x-httpd-php"))
3858 * "application/x-httpd-php" is a PHP page.
3861 cupsdSetString(&con
->command
, CUPS_PHP
);
3864 cupsdSetStringf(&con
->options
, " %s %s", filename
, options
);
3866 cupsdSetStringf(&con
->options
, " %s", filename
);
3868 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3869 "is_cgi: Returning 1 with command=\"%s\" and options=\"%s\"",
3870 con
->command
, con
->options
);
3874 #endif /* HAVE_PHP */
3876 else if (!strcasecmp(type
->type
, "x-httpd-python"))
3879 * "application/x-httpd-python" is a Python page.
3882 cupsdSetString(&con
->command
, CUPS_PYTHON
);
3885 cupsdSetStringf(&con
->options
, " %s %s", filename
, options
);
3887 cupsdSetStringf(&con
->options
, " %s", filename
);
3889 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3890 "is_cgi: Returning 1 with command=\"%s\" and options=\"%s\"",
3891 con
->command
, con
->options
);
3895 #endif /* HAVE_PYTHON */
3897 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "is_cgi: Returning 0...");
3904 * 'is_path_absolute()' - Is a path absolute and free of relative elements (i.e. "..").
3907 static int /* O - 0 if relative, 1 if absolute */
3908 is_path_absolute(const char *path
) /* I - Input path */
3911 * Check for a leading slash...
3918 * Check for "/.." in the path...
3921 while ((path
= strstr(path
, "/..")) != NULL
)
3923 if (!path
[3] || path
[3] == '/')
3930 * If we haven't found any relative paths, return 1 indicating an
3940 * 'make_certificate()' - Make a self-signed SSL/TLS certificate.
3943 static int /* O - 1 on success, 0 on failure */
3944 make_certificate(cupsd_client_t
*con
) /* I - Client connection */
3946 #if defined(HAVE_LIBSSL) && defined(HAVE_WAITPID)
3947 int pid
, /* Process ID of command */
3948 status
; /* Status of command */
3949 char command
[1024], /* Command */
3950 *argv
[12], /* Command-line arguments */
3951 *envp
[MAX_ENV
+ 1], /* Environment variables */
3952 home
[1024], /* HOME environment variable */
3953 infofile
[1024], /* Type-in information for cert */
3954 seedfile
[1024]; /* Random number seed file */
3955 int envc
, /* Number of environment variables */
3956 bytes
; /* Bytes written */
3957 cups_file_t
*fp
; /* Seed/info file */
3958 int infofd
; /* Info file descriptor */
3962 * Run the "openssl" command to seed the random number generator and
3963 * generate a self-signed certificate that is good for 10 years:
3965 * openssl rand -rand seedfile 1
3967 * openssl req -new -x509 -keyout ServerKey \
3968 * -out ServerCertificate -days 3650 -nodes
3970 * The seeding step is crucial in ensuring that the openssl command
3971 * does not block on systems without sufficient entropy...
3974 if (!cupsFileFind("openssl", getenv("PATH"), 1, command
, sizeof(command
)))
3976 cupsdLogMessage(CUPSD_LOG_ERROR
,
3977 "No SSL certificate and openssl command not found!");
3981 if (access("/dev/urandom", 0))
3984 * If the system doesn't provide /dev/urandom, then any random source
3985 * will probably be blocking-style, so generate some random data to
3986 * use as a seed for the certificate. Note that we have already
3987 * seeded the random number generator in cupsdInitCerts()...
3990 cupsdLogMessage(CUPSD_LOG_INFO
,
3991 "Seeding the random number generator...");
3993 snprintf(home
, sizeof(home
), "HOME=%s", TempDir
);
3996 * Write the seed file...
3999 if ((fp
= cupsTempFile2(seedfile
, sizeof(seedfile
))) == NULL
)
4001 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to create seed file %s - %s",
4002 seedfile
, strerror(errno
));
4006 for (bytes
= 0; bytes
< 262144; bytes
++)
4007 cupsFilePutChar(fp
, random());
4012 * Run the openssl command to seed its random number generator...
4015 argv
[0] = "openssl";
4022 envc
= cupsdLoadEnv(envp
, MAX_ENV
);
4023 envp
[envc
++] = home
;
4026 if (!cupsdStartProcess(command
, argv
, envp
, -1, -1, -1, -1, -1, 1, NULL
,
4033 while (waitpid(pid
, &status
, 0) < 0)
4040 cupsdFinishProcess(pid
, command
, sizeof(command
));
4043 * Remove the seed file, as it is no longer needed...
4050 if (WIFEXITED(status
))
4051 cupsdLogMessage(CUPSD_LOG_ERROR
,
4052 "Unable to seed random number generator - "
4053 "the openssl command stopped with status %d!",
4054 WEXITSTATUS(status
));
4056 cupsdLogMessage(CUPSD_LOG_ERROR
,
4057 "Unable to seed random number generator - "
4058 "the openssl command crashed on signal %d!",
4066 * Create a file with the certificate information fields...
4068 * Note: This assumes that the default questions are asked by the openssl
4072 if ((fp
= cupsTempFile2(infofile
, sizeof(infofile
))) == NULL
)
4074 cupsdLogMessage(CUPSD_LOG_ERROR
,
4075 "Unable to create certificate information file %s - %s",
4076 infofile
, strerror(errno
));
4080 cupsFilePrintf(fp
, ".\n.\n.\n%s\n.\n%s\n%s\n",
4081 ServerName
, ServerName
, ServerAdmin
);
4084 cupsdLogMessage(CUPSD_LOG_INFO
,
4085 "Generating SSL server key and certificate...");
4087 argv
[0] = "openssl";
4091 argv
[4] = "-keyout";
4092 argv
[5] = ServerKey
;
4094 argv
[7] = ServerCertificate
;
4097 argv
[10] = "-nodes";
4100 cupsdLoadEnv(envp
, MAX_ENV
);
4102 infofd
= open(infofile
, O_RDONLY
);
4104 if (!cupsdStartProcess(command
, argv
, envp
, infofd
, -1, -1, -1, -1, 1, NULL
,
4115 while (waitpid(pid
, &status
, 0) < 0)
4122 cupsdFinishProcess(pid
, command
, sizeof(command
));
4126 if (WIFEXITED(status
))
4127 cupsdLogMessage(CUPSD_LOG_ERROR
,
4128 "Unable to create SSL server key and certificate - "
4129 "the openssl command stopped with status %d!",
4130 WEXITSTATUS(status
));
4132 cupsdLogMessage(CUPSD_LOG_ERROR
,
4133 "Unable to create SSL server key and certificate - "
4134 "the openssl command crashed on signal %d!",
4139 cupsdLogMessage(CUPSD_LOG_INFO
, "Created SSL server key file \"%s\"...",
4141 cupsdLogMessage(CUPSD_LOG_INFO
,
4142 "Created SSL server certificate file \"%s\"...",
4148 #elif defined(HAVE_GNUTLS)
4149 gnutls_x509_crt crt
; /* Self-signed certificate */
4150 gnutls_x509_privkey key
; /* Encryption key */
4151 cups_lang_t
*language
; /* Default language info */
4152 cups_file_t
*fp
; /* Key/cert file */
4153 unsigned char buffer
[8192]; /* Buffer for x509 data */
4154 size_t bytes
; /* Number of bytes of data */
4155 unsigned char serial
[4]; /* Serial number buffer */
4156 time_t curtime
; /* Current time */
4157 int result
; /* Result of GNU TLS calls */
4161 * Create the encryption key...
4164 cupsdLogMessage(CUPSD_LOG_INFO
, "Generating SSL server key...");
4166 gnutls_x509_privkey_init(&key
);
4167 gnutls_x509_privkey_generate(key
, GNUTLS_PK_RSA
, 2048, 0);
4173 bytes
= sizeof(buffer
);
4175 if ((result
= gnutls_x509_privkey_export(key
, GNUTLS_X509_FMT_PEM
,
4176 buffer
, &bytes
)) < 0)
4178 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to export SSL server key - %s",
4179 gnutls_strerror(result
));
4180 gnutls_x509_privkey_deinit(key
);
4183 else if ((fp
= cupsFileOpen(ServerKey
, "w")) != NULL
)
4185 cupsFileWrite(fp
, (char *)buffer
, bytes
);
4188 cupsdLogMessage(CUPSD_LOG_INFO
, "Created SSL server key file \"%s\"...",
4193 cupsdLogMessage(CUPSD_LOG_ERROR
,
4194 "Unable to create SSL server key file \"%s\" - %s",
4195 ServerKey
, strerror(errno
));
4196 gnutls_x509_privkey_deinit(key
);
4201 * Create the self-signed certificate...
4204 cupsdLogMessage(CUPSD_LOG_INFO
, "Generating self-signed SSL certificate...");
4206 language
= cupsLangDefault();
4207 curtime
= time(NULL
);
4208 serial
[0] = curtime
>> 24;
4209 serial
[1] = curtime
>> 16;
4210 serial
[2] = curtime
>> 8;
4211 serial
[3] = curtime
;
4213 gnutls_x509_crt_init(&crt
);
4214 if (strlen(language
->language
) == 5)
4215 gnutls_x509_crt_set_dn_by_oid(crt
, GNUTLS_OID_X520_COUNTRY_NAME
, 0,
4216 language
->language
+ 3, 2);
4218 gnutls_x509_crt_set_dn_by_oid(crt
, GNUTLS_OID_X520_COUNTRY_NAME
, 0,
4220 gnutls_x509_crt_set_dn_by_oid(crt
, GNUTLS_OID_X520_COMMON_NAME
, 0,
4221 ServerName
, strlen(ServerName
));
4222 gnutls_x509_crt_set_dn_by_oid(crt
, GNUTLS_OID_X520_ORGANIZATION_NAME
, 0,
4223 ServerName
, strlen(ServerName
));
4224 gnutls_x509_crt_set_dn_by_oid(crt
, GNUTLS_OID_X520_ORGANIZATIONAL_UNIT_NAME
,
4226 gnutls_x509_crt_set_dn_by_oid(crt
, GNUTLS_OID_X520_STATE_OR_PROVINCE_NAME
, 0,
4228 gnutls_x509_crt_set_dn_by_oid(crt
, GNUTLS_OID_X520_LOCALITY_NAME
, 0,
4230 gnutls_x509_crt_set_dn_by_oid(crt
, GNUTLS_OID_PKCS9_EMAIL
, 0,
4231 ServerAdmin
, strlen(ServerAdmin
));
4232 gnutls_x509_crt_set_key(crt
, key
);
4233 gnutls_x509_crt_set_serial(crt
, serial
, sizeof(serial
));
4234 gnutls_x509_crt_set_activation_time(crt
, curtime
);
4235 gnutls_x509_crt_set_expiration_time(crt
, curtime
+ 10 * 365 * 86400);
4236 gnutls_x509_crt_set_ca_status(crt
, 0);
4237 gnutls_x509_crt_set_subject_alternative_name(crt
, GNUTLS_SAN_DNSNAME
,
4239 gnutls_x509_crt_set_key_purpose_oid(crt
, GNUTLS_KP_TLS_WWW_SERVER
, 0);
4240 gnutls_x509_crt_set_key_usage(crt
, GNUTLS_KEY_KEY_ENCIPHERMENT
);
4241 gnutls_x509_crt_set_version(crt
, 3);
4243 bytes
= sizeof(buffer
);
4244 if (gnutls_x509_crt_get_key_id(crt
, 0, buffer
, &bytes
) >= 0)
4245 gnutls_x509_crt_set_subject_key_id(crt
, buffer
, bytes
);
4247 gnutls_x509_crt_sign(crt
, crt
, key
);
4253 bytes
= sizeof(buffer
);
4254 if ((result
= gnutls_x509_crt_export(crt
, GNUTLS_X509_FMT_PEM
,
4255 buffer
, &bytes
)) < 0)
4256 cupsdLogMessage(CUPSD_LOG_ERROR
,
4257 "Unable to export SSL server certificate - %s",
4258 gnutls_strerror(result
));
4259 else if ((fp
= cupsFileOpen(ServerCertificate
, "w")) != NULL
)
4261 cupsFileWrite(fp
, (char *)buffer
, bytes
);
4264 cupsdLogMessage(CUPSD_LOG_INFO
,
4265 "Created SSL server certificate file \"%s\"...",
4269 cupsdLogMessage(CUPSD_LOG_ERROR
,
4270 "Unable to create SSL server certificate file \"%s\" - %s",
4271 ServerCertificate
, strerror(errno
));
4277 gnutls_x509_crt_deinit(crt
);
4278 gnutls_x509_privkey_deinit(key
);
4282 #elif defined(HAVE_CDSASSL) && defined(HAVE_WAITPID)
4283 int pid
, /* Process ID of command */
4284 status
; /* Status of command */
4285 char command
[1024], /* Command */
4286 *argv
[4], /* Command-line arguments */
4287 *envp
[MAX_ENV
+ 1], /* Environment variables */
4288 keychain
[1024], /* Keychain argument */
4289 infofile
[1024]; /* Type-in information for cert */
4290 cups_file_t
*fp
; /* Seed/info file */
4291 int infofd
; /* Info file descriptor */
4295 * Run the "certtool" command to generate a self-signed certificate...
4298 if (!cupsFileFind("certtool", getenv("PATH"), 1, command
, sizeof(command
)))
4300 cupsdLogMessage(CUPSD_LOG_ERROR
,
4301 "No SSL certificate and certtool command not found!");
4306 * Create a file with the certificate information fields...
4308 * Note: This assumes that the default questions are asked by the certtool
4312 if ((fp
= cupsTempFile2(infofile
, sizeof(infofile
))) == NULL
)
4314 cupsdLogMessage(CUPSD_LOG_ERROR
,
4315 "Unable to create certificate information file %s - %s",
4316 infofile
, strerror(errno
));
4320 cupsFilePrintf(fp
, "%s\nr\n\ny\nb\ns\ny\n%s\n\n\n\n\n%s\ny\n",
4321 con
->servername
, con
->servername
, ServerAdmin
);
4324 cupsdLogMessage(CUPSD_LOG_INFO
,
4325 "Generating SSL server key and certificate...");
4327 snprintf(keychain
, sizeof(keychain
), "k=%s", ServerCertificate
);
4329 argv
[0] = "certtool";
4334 cupsdLoadEnv(envp
, MAX_ENV
);
4336 infofd
= open(infofile
, O_RDONLY
);
4338 if (!cupsdStartProcess(command
, argv
, envp
, infofd
, -1, -1, -1, -1, 1, NULL
,
4349 while (waitpid(pid
, &status
, 0) < 0)
4356 cupsdFinishProcess(pid
, command
, sizeof(command
));
4360 if (WIFEXITED(status
))
4361 cupsdLogMessage(CUPSD_LOG_ERROR
,
4362 "Unable to create SSL server key and certificate - "
4363 "the certtool command stopped with status %d!",
4364 WEXITSTATUS(status
));
4366 cupsdLogMessage(CUPSD_LOG_ERROR
,
4367 "Unable to create SSL server key and certificate - "
4368 "the certtool command crashed on signal %d!",
4373 cupsdLogMessage(CUPSD_LOG_INFO
,
4374 "Created SSL server certificate file \"%s\"...",
4382 #endif /* HAVE_LIBSSL && HAVE_WAITPID */
4384 #endif /* HAVE_SSL */
4388 * 'pipe_command()' - Pipe the output of a command to the remote client.
4391 static int /* O - Process ID */
4392 pipe_command(cupsd_client_t
*con
, /* I - Client connection */
4393 int infile
, /* I - Standard input for command */
4394 int *outfile
, /* O - Standard output for command */
4395 char *command
, /* I - Command to run */
4396 char *options
, /* I - Options for command */
4397 int root
) /* I - Run as root? */
4399 int i
; /* Looping var */
4400 int pid
; /* Process ID */
4401 char *commptr
, /* Command string pointer */
4402 commch
; /* Command string character */
4403 char *uriptr
; /* URI string pointer */
4404 int fds
[2]; /* Pipe FDs */
4405 int argc
; /* Number of arguments */
4406 int envc
; /* Number of environment variables */
4407 char argbuf
[10240], /* Argument buffer */
4408 *argv
[100], /* Argument strings */
4409 *envp
[MAX_ENV
+ 20]; /* Environment variables */
4410 char auth_type
[256], /* CUPSD_AUTH_TYPE environment variable */
4411 content_length
[1024], /* CONTENT_LENGTH environment variable */
4412 content_type
[1024], /* CONTENT_TYPE environment variable */
4413 http_cookie
[32768], /* HTTP_COOKIE environment variable */
4414 http_referer
[1024], /* HTTP_REFERER environment variable */
4415 http_user_agent
[1024], /* HTTP_USER_AGENT environment variable */
4416 lang
[1024], /* LANG environment variable */
4417 path_info
[1024], /* PATH_INFO environment variable */
4418 remote_addr
[1024], /* REMOTE_ADDR environment variable */
4419 remote_host
[1024], /* REMOTE_HOST environment variable */
4420 remote_user
[1024], /* REMOTE_USER environment variable */
4421 script_filename
[1024], /* SCRIPT_FILENAME environment variable */
4422 script_name
[1024], /* SCRIPT_NAME environment variable */
4423 server_name
[1024], /* SERVER_NAME environment variable */
4424 server_port
[1024]; /* SERVER_PORT environment variable */
4425 ipp_attribute_t
*attr
; /* attributes-natural-language attribute */
4427 krb5_ccache ccache
= NULL
; /* Kerberos credentials */
4428 char krb5ccname
[1024]; /* KRB5CCNAME environment variable */
4429 #endif /* HAVE_GSSAPI */
4433 * Parse a copy of the options string, which is of the form:
4435 * argument+argument+argument
4436 * ?argument+argument+argument
4437 * param=value¶m=value
4438 * ?param=value¶m=value
4439 * /name?argument+argument+argument
4440 * /name?param=value¶m=value
4442 * If the string contains an "=" character after the initial name,
4443 * then we treat it as a HTTP GET form request and make a copy of
4444 * the remaining string for the environment variable.
4446 * The string is always parsed out as command-line arguments, to
4447 * be consistent with Apache...
4450 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4451 "pipe_command: command=\"%s\", options=\"%s\"",
4452 command
, options
? options
: "(null)");
4457 strlcpy(argbuf
, options
, sizeof(argbuf
));
4461 if (argbuf
[0] == '/')
4464 * Found some trailing path information, set PATH_INFO...
4467 if ((commptr
= strchr(argbuf
, '?')) == NULL
)
4468 commptr
= argbuf
+ strlen(argbuf
);
4472 snprintf(path_info
, sizeof(path_info
), "PATH_INFO=%s", argbuf
);
4478 path_info
[0] = '\0';
4480 if (*commptr
== ' ')
4484 if (*commptr
== '?' && con
->operation
== HTTP_GET
&& !con
->query_string
)
4487 cupsdSetStringf(&(con
->query_string
), "QUERY_STRING=%s", commptr
);
4494 argv
[argc
++] = commptr
;
4496 for (; *commptr
&& argc
< 99; commptr
++)
4499 * Break arguments whenever we see a + or space...
4502 if (*commptr
== ' ' || *commptr
== '+')
4504 while (*commptr
== ' ' || *commptr
== '+')
4508 * If we don't have a blank string, save it as another argument...
4513 argv
[argc
] = commptr
;
4519 else if (*commptr
== '%' && isxdigit(commptr
[1] & 255) &&
4520 isxdigit(commptr
[2] & 255))
4523 * Convert the %xx notation to the individual character.
4526 if (commptr
[1] >= '0' && commptr
[1] <= '9')
4527 *commptr
= (commptr
[1] - '0') << 4;
4529 *commptr
= (tolower(commptr
[1]) - 'a' + 10) << 4;
4531 if (commptr
[2] >= '0' && commptr
[2] <= '9')
4532 *commptr
|= commptr
[2] - '0';
4534 *commptr
|= tolower(commptr
[2]) - 'a' + 10;
4536 _cups_strcpy(commptr
+ 1, commptr
+ 3);
4539 * Check for a %00 and break if that is the case...
4551 * Setup the environment variables as needed...
4554 if (con
->username
[0])
4556 snprintf(auth_type
, sizeof(auth_type
), "CUPSD_AUTH_TYPE=%s",
4557 httpGetField(HTTP(con
), HTTP_FIELD_AUTHORIZATION
));
4559 if ((uriptr
= strchr(auth_type
+ 10, ' ')) != NULL
)
4563 auth_type
[0] = '\0';
4566 (attr
= ippFindAttribute(con
->request
, "attributes-natural-language",
4567 IPP_TAG_LANGUAGE
)) != NULL
)
4569 switch (strlen(attr
->values
[0].string
.text
))
4573 * This is an unknown or badly formatted language code; use
4574 * the POSIX locale...
4577 strcpy(lang
, "LANG=C");
4582 * Just the language code (ll)...
4585 snprintf(lang
, sizeof(lang
), "LANG=%s.UTF8",
4586 attr
->values
[0].string
.text
);
4591 * Language and country code (ll-cc)...
4594 snprintf(lang
, sizeof(lang
), "LANG=%c%c_%c%c.UTF8",
4595 attr
->values
[0].string
.text
[0],
4596 attr
->values
[0].string
.text
[1],
4597 toupper(attr
->values
[0].string
.text
[3] & 255),
4598 toupper(attr
->values
[0].string
.text
[4] & 255));
4602 else if (con
->language
)
4603 snprintf(lang
, sizeof(lang
), "LANG=%s.UTF8", con
->language
->language
);
4605 strcpy(lang
, "LANG=C");
4607 strcpy(remote_addr
, "REMOTE_ADDR=");
4608 httpAddrString(con
->http
.hostaddr
, remote_addr
+ 12,
4609 sizeof(remote_addr
) - 12);
4611 snprintf(remote_host
, sizeof(remote_host
), "REMOTE_HOST=%s",
4612 con
->http
.hostname
);
4614 snprintf(script_name
, sizeof(script_name
), "SCRIPT_NAME=%s", con
->uri
);
4615 if ((uriptr
= strchr(script_name
, '?')) != NULL
)
4618 snprintf(script_filename
, sizeof(script_filename
), "SCRIPT_FILENAME=%s%s",
4619 DocumentRoot
, script_name
+ 12);
4621 sprintf(server_port
, "SERVER_PORT=%d", con
->serverport
);
4623 snprintf(server_name
, sizeof(server_name
), "SERVER_NAME=%s",
4626 envc
= cupsdLoadEnv(envp
, (int)(sizeof(envp
) / sizeof(envp
[0])));
4629 envp
[envc
++] = auth_type
;
4631 envp
[envc
++] = lang
;
4632 envp
[envc
++] = "REDIRECT_STATUS=1";
4633 envp
[envc
++] = "GATEWAY_INTERFACE=CGI/1.1";
4634 envp
[envc
++] = server_name
;
4635 envp
[envc
++] = server_port
;
4636 envp
[envc
++] = remote_addr
;
4637 envp
[envc
++] = remote_host
;
4638 envp
[envc
++] = script_name
;
4639 envp
[envc
++] = script_filename
;
4642 envp
[envc
++] = path_info
;
4644 if (con
->username
[0])
4646 snprintf(remote_user
, sizeof(remote_user
), "REMOTE_USER=%s", con
->username
);
4648 envp
[envc
++] = remote_user
;
4651 * Save Kerberos credentials, if any...
4655 if (con
->gss_have_creds
)
4657 # if !defined(HAVE_KRB5_CC_NEW_UNIQUE) && !defined(HAVE_HEIMDAL)
4658 cupsdLogMessage(CUPSD_LOG_INFO
,
4659 "Sorry, your version of Kerberos does not support "
4660 "delegated credentials!");
4663 krb5_error_code error
; /* Kerberos error code */
4664 OM_uint32 major_status
, /* Major status code */
4665 minor_status
; /* Minor status code */
4666 krb5_principal principal
; /* Kerberos principal */
4671 * If the weak-linked GSSAPI/Kerberos library is not present, don't try
4675 if (krb5_init_context
!= NULL
)
4677 # endif /* __APPLE__ */
4680 * We MUST create a file-based cache because memory-based caches are
4681 * only valid for the current process/address space.
4683 * Due to various bugs/features in different versions of Kerberos, we
4684 * need either the krb5_cc_new_unique() function or Heimdal's version
4685 * of krb5_cc_gen_new() to create a new FILE: credential cache that
4686 * can be passed to the backend. These functions create a temporary
4687 * file (typically in /tmp) containing the cached credentials, which
4688 * are removed when we have successfully printed a job.
4691 # ifdef HAVE_KRB5_CC_NEW_UNIQUE
4692 if ((error
= krb5_cc_new_unique(KerberosContext
, "FILE", NULL
,
4694 # else /* HAVE_HEIMDAL */
4695 if ((error
= krb5_cc_gen_new(KerberosContext
, &krb5_fcc_ops
,
4697 # endif /* HAVE_KRB5_CC_NEW_UNIQUE */
4699 cupsdLogMessage(CUPSD_LOG_ERROR
,
4700 "Unable to create new credentials cache (%d/%s)",
4701 error
, strerror(errno
));
4704 else if ((error
= krb5_parse_name(KerberosContext
, con
->username
,
4707 cupsdLogMessage(CUPSD_LOG_ERROR
,
4708 "Unable to parse kerberos username (%d/%s)", error
,
4710 krb5_cc_destroy(KerberosContext
, ccache
);
4713 else if ((error
= krb5_cc_initialize(KerberosContext
, ccache
,
4716 cupsdLogMessage(CUPSD_LOG_ERROR
,
4717 "Unable to initialize credentials cache (%d/%s)", error
,
4719 krb5_cc_destroy(KerberosContext
, ccache
);
4720 krb5_free_principal(KerberosContext
, principal
);
4725 krb5_free_principal(KerberosContext
, principal
);
4728 * Copy the user's credentials to the new cache file...
4731 major_status
= gss_krb5_copy_ccache(&minor_status
,
4732 con
->gss_delegated_cred
, ccache
);
4734 if (GSS_ERROR(major_status
))
4736 cupsdLogGSSMessage(CUPSD_LOG_ERROR
, major_status
, minor_status
,
4737 "Unable to import client credentials cache");
4738 krb5_cc_destroy(KerberosContext
, ccache
);
4744 * Add the KRB5CCNAME environment variable to the job so that the
4745 * backend can use the credentials when printing.
4748 snprintf(krb5ccname
, sizeof(krb5ccname
), "KRB5CCNAME=FILE:%s",
4749 krb5_cc_get_name(KerberosContext
, ccache
));
4750 envp
[envc
++] = krb5ccname
;
4753 chown(krb5_cc_get_name(KerberosContext
, ccache
), User
, Group
);
4758 # endif /* __APPLE__ */
4759 # endif /* HAVE_KRB5_CC_NEW_UNIQUE || HAVE_HEIMDAL */
4761 #endif /* HAVE_GSSAPI */
4765 if (con
->http
.version
== HTTP_1_1
)
4766 envp
[envc
++] = "SERVER_PROTOCOL=HTTP/1.1";
4767 else if (con
->http
.version
== HTTP_1_0
)
4768 envp
[envc
++] = "SERVER_PROTOCOL=HTTP/1.0";
4770 envp
[envc
++] = "SERVER_PROTOCOL=HTTP/0.9";
4772 if (con
->http
.cookie
)
4774 snprintf(http_cookie
, sizeof(http_cookie
), "HTTP_COOKIE=%s",
4776 envp
[envc
++] = http_cookie
;
4779 if (con
->http
.fields
[HTTP_FIELD_USER_AGENT
][0])
4781 snprintf(http_user_agent
, sizeof(http_user_agent
), "HTTP_USER_AGENT=%s",
4782 con
->http
.fields
[HTTP_FIELD_USER_AGENT
]);
4783 envp
[envc
++] = http_user_agent
;
4786 if (con
->http
.fields
[HTTP_FIELD_REFERER
][0])
4788 snprintf(http_referer
, sizeof(http_referer
), "HTTP_REFERER=%s",
4789 con
->http
.fields
[HTTP_FIELD_REFERER
]);
4790 envp
[envc
++] = http_referer
;
4793 if (con
->operation
== HTTP_GET
)
4795 envp
[envc
++] = "REQUEST_METHOD=GET";
4797 if (con
->query_string
)
4800 * Add GET form variables after ?...
4803 envp
[envc
++] = con
->query_string
;
4806 envp
[envc
++] = "QUERY_STRING=";
4810 sprintf(content_length
, "CONTENT_LENGTH=" CUPS_LLFMT
,
4811 CUPS_LLCAST con
->bytes
);
4812 snprintf(content_type
, sizeof(content_type
), "CONTENT_TYPE=%s",
4813 con
->http
.fields
[HTTP_FIELD_CONTENT_TYPE
]);
4815 envp
[envc
++] = "REQUEST_METHOD=POST";
4816 envp
[envc
++] = content_length
;
4817 envp
[envc
++] = content_type
;
4821 * Tell the CGI if we are using encryption...
4825 envp
[envc
++] = "HTTPS=ON";
4828 * Terminate the environment array...
4833 if (LogLevel
== CUPSD_LOG_DEBUG2
)
4835 for (i
= 0; i
< argc
; i
++)
4836 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4837 "pipe_command: argv[%d] = \"%s\"", i
, argv
[i
]);
4838 for (i
= 0; i
< envc
; i
++)
4839 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4840 "pipe_command: envp[%d] = \"%s\"", i
, envp
[i
]);
4844 * Create a pipe for the output...
4847 if (cupsdOpenPipe(fds
))
4849 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to create pipes for CGI %s - %s",
4850 argv
[0], strerror(errno
));
4855 * Then execute the command...
4858 if (cupsdStartProcess(command
, argv
, envp
, infile
, fds
[1], CGIPipes
[1],
4859 -1, -1, root
, DefaultProfile
, &pid
) < 0)
4862 * Error - can't fork!
4865 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to fork for CGI %s - %s", argv
[0],
4868 cupsdClosePipe(fds
);
4874 * Fork successful - return the PID...
4877 if (con
->username
[0])
4879 cupsdAddCert(pid
, con
->username
, ccache
);
4881 cupsdAddCert(pid
, con
->username
, NULL
);
4882 #endif /* HAVE_GSSAPI */
4884 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[CGI] %s started - PID = %d",
4896 * 'write_file()' - Send a file via HTTP.
4899 static int /* O - 0 on failure, 1 on success */
4900 write_file(cupsd_client_t
*con
, /* I - Client connection */
4901 http_status_t code
, /* I - HTTP status */
4902 char *filename
, /* I - Filename */
4903 char *type
, /* I - File type */
4904 struct stat
*filestats
) /* O - File information */
4906 con
->file
= open(filename
, O_RDONLY
);
4908 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "write_file: %d file=%d", con
->http
.fd
,
4914 fcntl(con
->file
, F_SETFD
, fcntl(con
->file
, F_GETFD
) | FD_CLOEXEC
);
4918 if (!cupsdSendHeader(con
, code
, type
, CUPSD_AUTH_NONE
))
4921 if (httpPrintf(HTTP(con
), "Last-Modified: %s\r\n",
4922 httpGetDateString(filestats
->st_mtime
)) < 0)
4924 if (httpPrintf(HTTP(con
), "Content-Length: " CUPS_LLFMT
"\r\n",
4925 CUPS_LLCAST filestats
->st_size
) < 0)
4927 if (httpPrintf(HTTP(con
), "\r\n") < 0)
4930 if (cupsdFlushHeader(con
) < 0)
4933 con
->http
.data_encoding
= HTTP_ENCODE_LENGTH
;
4934 con
->http
.data_remaining
= filestats
->st_size
;
4936 if (con
->http
.data_remaining
<= INT_MAX
)
4937 con
->http
._data_remaining
= con
->http
.data_remaining
;
4939 con
->http
._data_remaining
= INT_MAX
;
4941 cupsdAddSelect(con
->http
.fd
, (cupsd_selfunc_t
)cupsdReadClient
,
4942 (cupsd_selfunc_t
)cupsdWriteClient
, con
);
4949 * 'write_pipe()' - Flag that data is available on the CGI pipe.
4953 write_pipe(cupsd_client_t
*con
) /* I - Client connection */
4955 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "write_pipe: CGI output on fd %d...",
4958 con
->file_ready
= 1;
4960 cupsdRemoveSelect(con
->file
);
4961 cupsdAddSelect(con
->http
.fd
, NULL
, (cupsd_selfunc_t
)cupsdWriteClient
, con
);
4966 * End of "$Id: client.c 7673 2008-06-18 22:31:26Z mike $".