2 * "$Id: client.c 5200 2006-02-28 00:10:32Z mike $"
4 * Client routines for the Common UNIX Printing System (CUPS) scheduler.
6 * Copyright 1997-2006 by Easy Software Products, all rights reserved.
8 * These coded instructions, statements, and computer programs are the
9 * property of Easy Software Products and are protected by Federal
10 * copyright law. Distribution and use rights are outlined in the file
11 * "LICENSE.txt" which should have been included with this file. If this
12 * file is missing or damaged please contact Easy Software Products
15 * Attn: CUPS Licensing Information
16 * Easy Software Products
17 * 44141 Airport View Drive, Suite 204
18 * Hollywood, Maryland 20636 USA
20 * Voice: (301) 373-9600
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
26 * cupsdAcceptClient() - Accept a new client.
27 * cupsdCloseAllClients() - Close all remote clients immediately.
28 * cupsdCloseClient() - Close a remote client.
29 * cupsdEncryptClient() - Enable encryption for the client...
30 * cupsdIsCGI() - Is the resource a CGI script/program?
31 * cupsdReadClient() - Read data from a client.
32 * cupsdSendCommand() - Send output from a command via HTTP.
33 * cupsdSendError() - Send an error message via HTTP.
34 * cupsdSendFile() - Send a file via HTTP.
35 * cupsdSendHeader() - Send an HTTP request.
36 * cupsdUpdateCGI() - Read status messages from CGI scripts and programs.
37 * cupsdWriteClient() - Write data to a client as needed.
38 * check_if_modified() - Decode an "If-Modified-Since" line.
39 * get_cdsa_server_certs() - Convert a keychain name into the CFArrayRef
40 * required by SSLSetCertificate.
41 * get_file() - Get a filename and state info.
42 * install_conf_file() - Install a configuration file.
43 * is_path_absolute() - Is a path absolute and free of relative elements.
44 * pipe_command() - Pipe the output of a command to the remote client.
48 * Include necessary headers...
51 #include <cups/http-private.h>
55 # include <Security/Security.h>
56 #endif /* HAVE_CDSASSL */
63 static int check_if_modified(cupsd_client_t
*con
,
64 struct stat
*filestats
);
66 static CFArrayRef
get_cdsa_server_certs(void);
67 #endif /* HAVE_CDSASSL */
68 static char *get_file(cupsd_client_t
*con
, struct stat
*filestats
,
69 char *filename
, int len
);
70 static http_status_t
install_conf_file(cupsd_client_t
*con
);
71 static int is_path_absolute(const char *path
);
72 static int pipe_command(cupsd_client_t
*con
, int infile
, int *outfile
,
73 char *command
, char *options
, int root
);
77 * 'cupsdAcceptClient()' - Accept a new client.
81 cupsdAcceptClient(cupsd_listener_t
*lis
)/* I - Listener socket */
83 int count
; /* Count of connections on a host */
84 int val
; /* Parameter value */
85 cupsd_client_t
*con
, /* New client pointer */
86 *tempcon
; /* Temporary client pointer */
87 http_addrlist_t
*addrlist
, /* List of adddresses for host */
88 *addr
; /* Current address */
89 socklen_t addrlen
; /* Length of address */
90 char *hostname
; /* Hostname for address */
91 http_addr_t temp
; /* Temporary address variable */
92 static time_t last_dos
= 0; /* Time of last DoS attack */
95 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
96 "cupsdAcceptClient(lis=%p) %d Clients = %d",
97 lis
, lis
->fd
, cupsArrayCount(Clients
));
100 * Make sure we don't have a full set of clients already...
103 if (cupsArrayCount(Clients
) == MaxClients
)
107 * Get a pointer to the next available client...
111 Clients
= cupsArrayNew(NULL
, NULL
);
116 con
= calloc(1, sizeof(cupsd_client_t
));
118 con
->http
.activity
= time(NULL
);
120 con
->http
.hostaddr
= &(con
->clientaddr
);
123 * Accept the client and get the remote address...
126 addrlen
= sizeof(http_addr_t
);
128 if ((con
->http
.fd
= accept(lis
->fd
, (struct sockaddr
*)con
->http
.hostaddr
,
131 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to accept client connection - %s.",
138 if (lis
->address
.addr
.sa_family
== AF_INET6
)
141 * Save the connected port number...
144 con
->http
.hostaddr
->ipv6
.sin6_port
= lis
->address
.ipv6
.sin6_port
;
147 * Convert IPv4 over IPv6 addresses (::ffff:n.n.n.n) to IPv4 forms we
148 * can more easily use...
151 if (con
->http
.hostaddr
->ipv6
.sin6_addr
.s6_addr32
[0] == 0 &&
152 con
->http
.hostaddr
->ipv6
.sin6_addr
.s6_addr32
[1] == 0 &&
153 ntohl(con
->http
.hostaddr
->ipv6
.sin6_addr
.s6_addr32
[2]) == 0xffff)
154 con
->http
.hostaddr
->ipv6
.sin6_addr
.s6_addr32
[2] = 0;
157 #endif /* AF_INET6 */
158 if (lis
->address
.addr
.sa_family
== AF_INET
)
159 con
->http
.hostaddr
->ipv4
.sin_port
= lis
->address
.ipv4
.sin_port
;
162 * Check the number of clients on the same address...
165 for (count
= 0, tempcon
= (cupsd_client_t
*)cupsArrayFirst(Clients
);
167 tempcon
= (cupsd_client_t
*)cupsArrayNext(Clients
))
168 if (httpAddrEqual(tempcon
->http
.hostaddr
, con
->http
.hostaddr
))
171 if (count
>= MaxClientsPerHost
)
175 if (count
>= MaxClientsPerHost
)
177 if ((time(NULL
) - last_dos
) >= 60)
179 last_dos
= time(NULL
);
180 cupsdLogMessage(CUPSD_LOG_WARN
,
181 "Possible DoS attack - more than %d clients connecting "
183 MaxClientsPerHost
, tempcon
->http
.hostname
);
187 closesocket(con
->http
.fd
);
197 * Get the hostname or format the IP address as needed...
200 if (httpAddrLocalhost(con
->http
.hostaddr
))
203 * Map accesses from the loopback interface to "localhost"...
206 strlcpy(con
->http
.hostname
, "localhost", sizeof(con
->http
.hostname
));
207 hostname
= con
->http
.hostname
;
212 * Map accesses from the same host to the server name.
215 for (addr
= ServerAddrs
; addr
; addr
= addr
->next
)
216 if (httpAddrEqual(con
->http
.hostaddr
, &(addr
->addr
)))
221 strlcpy(con
->http
.hostname
, ServerName
, sizeof(con
->http
.hostname
));
222 hostname
= con
->http
.hostname
;
224 else if (HostNameLookups
)
225 hostname
= httpAddrLookup(con
->http
.hostaddr
, con
->http
.hostname
,
226 sizeof(con
->http
.hostname
));
230 httpAddrString(con
->http
.hostaddr
, con
->http
.hostname
,
231 sizeof(con
->http
.hostname
));
235 if (hostname
== NULL
&& HostNameLookups
== 2)
238 * Can't have an unresolved IP address with double-lookups enabled...
241 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
242 "cupsdAcceptClient: Closing connection %d...",
246 closesocket(con
->http
.fd
);
251 cupsdLogMessage(CUPSD_LOG_WARN
,
252 "Name lookup failed - connection from %s closed!",
259 if (HostNameLookups
== 2)
262 * Do double lookups as needed...
265 if ((addrlist
= httpAddrGetList(con
->http
.hostname
, AF_UNSPEC
, NULL
)) != NULL
)
268 * See if the hostname maps to the same IP address...
271 for (addr
= addrlist
; addr
; addr
= addr
->next
)
272 if (httpAddrEqual(con
->http
.hostaddr
, &(addr
->addr
)))
278 httpAddrFreeList(addrlist
);
283 * Can't have a hostname that doesn't resolve to the same IP address
284 * with double-lookups enabled...
287 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
288 "cupsdAcceptClient: Closing connection %d...",
292 closesocket(con
->http
.fd
);
297 cupsdLogMessage(CUPSD_LOG_WARN
,
298 "IP lookup failed - connection from %s closed!",
306 if (con
->http
.hostaddr
->addr
.sa_family
== AF_INET6
)
307 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdAcceptClient: %d from %s:%d (IPv6)",
308 con
->http
.fd
, con
->http
.hostname
,
309 ntohs(con
->http
.hostaddr
->ipv6
.sin6_port
));
311 #endif /* AF_INET6 */
313 if (con
->http
.hostaddr
->addr
.sa_family
== AF_LOCAL
)
314 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdAcceptClient: %d from %s (Domain)",
315 con
->http
.fd
, con
->http
.hostname
);
317 #endif /* AF_LOCAL */
318 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdAcceptClient: %d from %s:%d (IPv4)",
319 con
->http
.fd
, con
->http
.hostname
,
320 ntohs(con
->http
.hostaddr
->ipv4
.sin_port
));
323 * Get the local address the client connected to...
326 addrlen
= sizeof(temp
);
327 if (getsockname(con
->http
.fd
, (struct sockaddr
*)&temp
, &addrlen
))
329 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to get local address - %s",
332 strcpy(con
->servername
, "localhost");
333 con
->serverport
= LocalPort
;
338 if (temp
.addr
.sa_family
== AF_INET6
)
340 httpAddrLookup(&temp
, con
->servername
, sizeof(con
->servername
));
341 con
->serverport
= ntohs(lis
->address
.ipv6
.sin6_port
);
344 #endif /* AF_INET6 */
345 if (temp
.addr
.sa_family
== AF_INET
)
347 httpAddrLookup(&temp
, con
->servername
, sizeof(con
->servername
));
348 con
->serverport
= ntohs(lis
->address
.ipv4
.sin_port
);
352 strcpy(con
->servername
, "localhost");
353 con
->serverport
= LocalPort
;
357 cupsArrayAdd(Clients
, con
);
359 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
360 "cupsdAcceptClient: %d connected to server on %s:%d",
361 con
->http
.fd
, con
->servername
, con
->serverport
);
364 * Using TCP_NODELAY improves responsiveness, especially on systems
365 * with a slow loopback interface... Since we write large buffers
366 * when sending print files and requests, there shouldn't be any
367 * performance penalty for this...
371 setsockopt(con
->http
.fd
, IPPROTO_TCP
, TCP_NODELAY
, &val
, sizeof(val
));
374 * Close this file on all execs...
377 fcntl(con
->http
.fd
, F_SETFD
, fcntl(con
->http
.fd
, F_GETFD
) | FD_CLOEXEC
);
380 * Add the socket to the select() input mask.
383 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
384 "cupsdAcceptClient: Adding fd %d to InputSet...",
386 FD_SET(con
->http
.fd
, InputSet
);
389 * Temporarily suspend accept()'s until we lose a client...
392 if (cupsArrayCount(Clients
) == MaxClients
)
393 cupsdPauseListening();
397 * See if we are connecting on a secure port...
400 if (lis
->encryption
== HTTP_ENCRYPT_ALWAYS
)
403 * https connection; go secure...
406 con
->http
.encryption
= HTTP_ENCRYPT_ALWAYS
;
408 cupsdEncryptClient(con
);
412 #endif /* HAVE_SSL */
417 * 'cupsdCloseAllClients()' - Close all remote clients immediately.
421 cupsdCloseAllClients(void)
423 cupsd_client_t
*con
; /* Current client */
426 for (con
= (cupsd_client_t
*)cupsArrayFirst(Clients
);
428 con
= (cupsd_client_t
*)cupsArrayNext(Clients
))
429 cupsdCloseClient(con
);
434 * 'cupsdCloseClient()' - Close a remote client.
437 int /* O - 1 if partial close, 0 if fully closed */
438 cupsdCloseClient(cupsd_client_t
*con
) /* I - Client to close */
440 int partial
; /* Do partial close for SSL? */
442 SSL_CTX
*context
; /* Context for encryption */
443 SSL
*conn
; /* Connection for encryption */
444 unsigned long error
; /* Error code */
445 #elif defined(HAVE_GNUTLS)
446 http_tls_t
*conn
; /* TLS connection information */
447 int error
; /* Error code */
448 gnutls_certificate_server_credentials
*credentials
;
449 /* TLS credentials */
450 #endif /* HAVE_LIBSSL */
453 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdCloseClient: %d", con
->http
.fd
);
456 * Flush pending writes before closing...
459 httpFlushWrite(HTTP(con
));
465 * Shutdown encryption as needed...
473 conn
= (SSL
*)(con
->http
.tls
);
474 context
= SSL_get_SSL_CTX(conn
);
476 switch (SSL_shutdown(conn
))
479 cupsdLogMessage(CUPSD_LOG_INFO
,
480 "cupsdCloseClient: SSL shutdown successful!");
483 cupsdLogMessage(CUPSD_LOG_ERROR
,
484 "cupsdCloseClient: Fatal error during SSL shutdown!");
486 while ((error
= ERR_get_error()) != 0)
487 cupsdLogMessage(CUPSD_LOG_ERROR
, "cupsdCloseClient: %s",
488 ERR_error_string(error
, NULL
));
492 SSL_CTX_free(context
);
495 # elif defined(HAVE_GNUTLS)
496 conn
= (http_tls_t
*)(con
->http
.tls
);
497 credentials
= (gnutls_certificate_server_credentials
*)(conn
->credentials
);
499 error
= gnutls_bye(conn
->session
, GNUTLS_SHUT_WR
);
502 case GNUTLS_E_SUCCESS
:
503 cupsdLogMessage(CUPSD_LOG_INFO
,
504 "cupsdCloseClient: SSL shutdown successful!");
507 cupsdLogMessage(CUPSD_LOG_ERROR
,
508 "cupsdCloseClient: %s", gnutls_strerror(error
));
512 gnutls_deinit(conn
->session
);
513 gnutls_certificate_free_credentials(*credentials
);
517 # elif defined(HAVE_CDSASSL)
518 while (SSLClose((SSLContextRef
)con
->http
.tls
) == errSSLWouldBlock
)
521 SSLDisposeContext((SSLContextRef
)con
->http
.tls
);
522 # endif /* HAVE_LIBSSL */
524 con
->http
.tls
= NULL
;
526 #endif /* HAVE_SSL */
528 if (con
->pipe_pid
!= 0)
531 * Stop any CGI process...
534 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
535 "cupsdCloseClient: %d Killing process ID %d...",
536 con
->http
.fd
, con
->pipe_pid
);
537 cupsdEndProcess(con
->pipe_pid
, 1);
543 if (FD_ISSET(con
->file
, InputSet
))
545 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
546 "cupsdCloseClient: %d Removing fd %d from InputSet...",
547 con
->http
.fd
, con
->file
);
548 FD_CLR(con
->file
, InputSet
);
551 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
552 "cupsdCloseClient: %d Closing data file %d.",
553 con
->http
.fd
, con
->file
);
560 * Close the socket and clear the file from the input set for select()...
563 if (con
->http
.fd
> 0)
568 * Only do a partial close so that the encrypted client gets everything.
571 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
572 "cupsdCloseClient: Removing fd %d from OutputSet...",
574 shutdown(con
->http
.fd
, 0);
575 FD_CLR(con
->http
.fd
, OutputSet
);
580 * Shut the socket down fully...
583 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
584 "cupsdCloseClient: Removing fd %d from InputSet and OutputSet...",
587 FD_CLR(con
->http
.fd
, InputSet
);
588 FD_CLR(con
->http
.fd
, OutputSet
);
599 if (con
->http
.input_set
)
600 free(con
->http
.input_set
);
602 httpClearCookie(HTTP(con
));
604 cupsdClearString(&con
->filename
);
605 cupsdClearString(&con
->command
);
606 cupsdClearString(&con
->options
);
610 ippDelete(con
->request
);
616 ippDelete(con
->response
);
617 con
->response
= NULL
;
622 cupsLangFree(con
->language
);
623 con
->language
= NULL
;
627 * Re-enable new client connections if we are going back under the
631 if (cupsArrayCount(Clients
) == MaxClients
)
632 cupsdResumeListening();
635 * Compact the list of clients as necessary...
638 cupsArrayRemove(Clients
, con
);
648 * 'cupsdEncryptClient()' - Enable encryption for the client...
651 int /* O - 1 on success, 0 on error */
652 cupsdEncryptClient(cupsd_client_t
*con
) /* I - Client to encrypt */
655 SSL_CTX
*context
; /* Context for encryption */
656 SSL
*conn
; /* Connection for encryption */
657 unsigned long error
; /* Error code */
661 * Create the SSL context and accept the connection...
664 context
= SSL_CTX_new(SSLv23_server_method());
666 SSL_CTX_set_options(context
, SSL_OP_NO_SSLv2
); /* Only use SSLv3 or TLS */
667 SSL_CTX_use_PrivateKey_file(context
, ServerKey
, SSL_FILETYPE_PEM
);
668 SSL_CTX_use_certificate_file(context
, ServerCertificate
, SSL_FILETYPE_PEM
);
670 conn
= SSL_new(context
);
672 SSL_set_fd(conn
, con
->http
.fd
);
673 if (SSL_accept(conn
) != 1)
675 cupsdLogMessage(CUPSD_LOG_ERROR
,
676 "cupsdEncryptClient: Unable to encrypt connection from %s!",
679 while ((error
= ERR_get_error()) != 0)
680 cupsdLogMessage(CUPSD_LOG_ERROR
, "cupsdEncryptClient: %s",
681 ERR_error_string(error
, NULL
));
683 SSL_CTX_free(context
);
688 cupsdLogMessage(CUPSD_LOG_DEBUG
,
689 "cupsdEncryptClient: %d Connection from %s now encrypted.",
690 con
->http
.fd
, con
->http
.hostname
);
692 con
->http
.tls
= conn
;
695 #elif defined(HAVE_GNUTLS)
696 http_tls_t
*conn
; /* TLS session object */
697 int error
; /* Error code */
698 gnutls_certificate_server_credentials
*credentials
;
699 /* TLS credentials */
702 * Create the SSL object and perform the SSL handshake...
705 conn
= (http_tls_t
*)malloc(sizeof(gnutls_session
));
710 credentials
= (gnutls_certificate_server_credentials
*)
711 malloc(sizeof(gnutls_certificate_server_credentials
));
712 if (credentials
== NULL
)
714 cupsdLogMessage(CUPSD_LOG_ERROR
,
715 "cupsdEncryptClient: Unable to encrypt connection from %s!",
717 cupsdLogMessage(CUPSD_LOG_ERROR
, "cupsdEncryptClient: %s", strerror(errno
));
723 gnutls_certificate_allocate_credentials(credentials
);
724 gnutls_certificate_set_x509_key_file(*credentials
, ServerCertificate
,
725 ServerKey
, GNUTLS_X509_FMT_PEM
);
727 gnutls_init(&(conn
->session
), GNUTLS_SERVER
);
728 gnutls_set_default_priority(conn
->session
);
729 gnutls_credentials_set(conn
->session
, GNUTLS_CRD_CERTIFICATE
, *credentials
);
730 gnutls_transport_set_ptr(conn
->session
, con
->http
.fd
);
732 error
= gnutls_handshake(conn
->session
);
734 if (error
!= GNUTLS_E_SUCCESS
)
736 cupsdLogMessage(CUPSD_LOG_ERROR
,
737 "cupsdEncryptClient: Unable to encrypt connection from %s!",
739 cupsdLogMessage(CUPSD_LOG_ERROR
, "cupsdEncryptClient: %s",
740 gnutls_strerror(error
));
742 gnutls_deinit(conn
->session
);
743 gnutls_certificate_free_credentials(*credentials
);
749 cupsdLogMessage(CUPSD_LOG_DEBUG
,
750 "cupsdEncryptClient: %d Connection from %s now encrypted.",
751 con
->http
.fd
, con
->http
.hostname
);
753 conn
->credentials
= credentials
;
754 con
->http
.tls
= conn
;
757 #elif defined(HAVE_CDSASSL)
758 OSStatus error
; /* Error info */
759 SSLContextRef conn
; /* New connection */
760 CFArrayRef certificatesArray
; /* Array containing certificates */
761 int allowExpired
; /* Allow expired certificates? */
762 int allowAnyRoot
; /* Allow any root certificate? */
766 error
= SSLNewContext(true, &conn
);
770 certificatesArray
= get_cdsa_server_certs();
772 if (!certificatesArray
)
774 cupsdLogMessage(CUPSD_LOG_ERROR
,
775 "EncryptClient: Could not find signing key in keychain "
776 "\"%s\"", ServerCertificate
);
777 error
= errSSLBadCert
; /* errSSLBadConfiguration is a better choice, but not available on 10.2.x */
781 error
= SSLSetIOFuncs(conn
, _httpReadCDSA
, _httpWriteCDSA
);
784 error
= SSLSetProtocolVersion(conn
, kSSLProtocol3
);
787 error
= SSLSetConnection(conn
, (SSLConnectionRef
)con
->http
.fd
);
790 error
= SSLSetPeerDomainName(conn
, ServerName
, strlen(ServerName
) + 1);
792 /* have to do these options before setting server certs */
793 if (!error
&& allowExpired
)
794 error
= SSLSetAllowsExpiredCerts(conn
, true);
796 if (!error
&& allowAnyRoot
)
797 error
= SSLSetAllowsAnyRoot(conn
, true);
800 error
= SSLSetCertificate(conn
, certificatesArray
);
802 if (certificatesArray
)
804 CFRelease(certificatesArray
);
805 certificatesArray
= NULL
;
811 * Perform SSL/TLS handshake
814 while ((error
= SSLHandshake(conn
)) == errSSLWouldBlock
)
820 cupsdLogMessage(CUPSD_LOG_ERROR
,
821 "cupsdEncryptClient: Unable to encrypt connection from %s!",
824 cupsdLogMessage(CUPSD_LOG_ERROR
,
825 "cupsdEncryptClient: CDSA error code is %d", (int)error
);
827 con
->http
.error
= error
;
828 con
->http
.status
= HTTP_ERROR
;
831 SSLDisposeContext(conn
);
836 cupsdLogMessage(CUPSD_LOG_DEBUG
,
837 "cupsdEncryptClient: %d Connection from %s now encrypted.",
838 con
->http
.fd
, con
->http
.hostname
);
840 con
->http
.tls
= conn
;
845 #endif /* HAVE_LIBSSL */
850 * 'cupsdIsCGI()' - Is the resource a CGI script/program?
853 int /* O - 1 = CGI, 0 = file */
854 cupsdIsCGI(cupsd_client_t
*con
, /* I - Client connection */
855 const char *filename
, /* I - Real filename */
856 struct stat
*filestats
, /* I - File information */
857 mime_type_t
*type
) /* I - MIME type */
859 const char *options
; /* Options on URL */
862 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
863 "cupsdIsCGI(con=%p, filename=\"%s\", filestats=%p, type=%s/%s)\n",
864 con
, filename
, filestats
, type
? type
->super
: "unknown",
865 type
? type
->type
: "unknown");
868 * Get the options, if any...
871 if ((options
= strchr(con
->uri
, '?')) != NULL
)
875 * Check for known types...
878 if (!type
|| strcasecmp(type
->super
, "application"))
880 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdIsCGI: Returning 0...");
884 if (!strcasecmp(type
->type
, "x-httpd-cgi") &&
885 (filestats
->st_mode
& 0111))
888 * "application/x-httpd-cgi" is a CGI script.
891 cupsdSetString(&con
->command
, filename
);
893 filename
= strrchr(filename
, '/') + 1; /* Filename always absolute */
895 cupsdSetString(&con
->options
, options
);
897 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
898 "cupsdIsCGI: Returning 1 with command=\"%s\" and options=\"%s\"",
899 con
->command
, con
->options
);
904 else if (!strcasecmp(type
->type
, "x-httpd-java"))
907 * "application/x-httpd-java" is a Java servlet.
910 cupsdSetString(&con
->command
, CUPS_JAVA
);
913 cupsdSetStringf(&con
->options
, "%s %s", filename
, options
);
915 cupsdSetString(&con
->options
, filename
);
917 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
918 "cupsdIsCGI: Returning 1 with command=\"%s\" and options=\"%s\"",
919 con
->command
, con
->options
);
923 #endif /* HAVE_JAVA */
925 else if (!strcasecmp(type
->type
, "x-httpd-perl"))
928 * "application/x-httpd-perl" is a Perl page.
931 cupsdSetString(&con
->command
, CUPS_PERL
);
934 cupsdSetStringf(&con
->options
, "%s %s", filename
, options
);
936 cupsdSetString(&con
->options
, filename
);
938 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
939 "cupsdIsCGI: Returning 1 with command=\"%s\" and options=\"%s\"",
940 con
->command
, con
->options
);
944 #endif /* HAVE_PERL */
946 else if (!strcasecmp(type
->type
, "x-httpd-php"))
949 * "application/x-httpd-php" is a PHP page.
952 cupsdSetString(&con
->command
, CUPS_PHP
);
955 cupsdSetStringf(&con
->options
, "%s %s", filename
, options
);
957 cupsdSetString(&con
->options
, filename
);
959 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
960 "cupsdIsCGI: Returning 1 with command=\"%s\" and options=\"%s\"",
961 con
->command
, con
->options
);
965 #endif /* HAVE_PHP */
967 else if (!strcasecmp(type
->type
, "x-httpd-python"))
970 * "application/x-httpd-python" is a Python page.
973 cupsdSetString(&con
->command
, CUPS_PYTHON
);
976 cupsdSetStringf(&con
->options
, "%s %s", filename
, options
);
978 cupsdSetString(&con
->options
, filename
);
980 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
981 "cupsdIsCGI: Returning 1 with command=\"%s\" and options=\"%s\"",
982 con
->command
, con
->options
);
986 #endif /* HAVE_PYTHON */
988 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdIsCGI: Returning 0...");
995 * 'cupsdReadClient()' - Read data from a client.
998 int /* O - 1 on success, 0 on error */
999 cupsdReadClient(cupsd_client_t
*con
) /* I - Client to read from */
1001 char line
[32768], /* Line from client... */
1002 operation
[64], /* Operation code from socket */
1003 version
[64], /* HTTP version number string */
1004 locale
[64], /* Locale */
1005 *ptr
; /* Pointer into strings */
1006 int major
, minor
; /* HTTP version numbers */
1007 http_status_t status
; /* Transfer status */
1008 ipp_state_t ipp_state
; /* State of IPP transfer */
1009 int bytes
; /* Number of bytes to POST */
1010 char *filename
; /* Name of file for GET/HEAD */
1011 char buf
[1024]; /* Buffer for real filename */
1012 struct stat filestats
; /* File information */
1013 mime_type_t
*type
; /* MIME type of file */
1014 cupsd_printer_t
*p
; /* Printer */
1015 static unsigned request_id
= 0; /* Request ID for temp files */
1018 status
= HTTP_CONTINUE
;
1020 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1021 "cupsdReadClient: %d, used=%d, file=%d state=%d",
1022 con
->http
.fd
, con
->http
.used
, con
->file
, con
->http
.state
);
1024 if (con
->http
.error
)
1026 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdReadClient: http error seen...");
1027 return (cupsdCloseClient(con
));
1034 * Automatically check for a SSL/TLS handshake...
1039 if (recv(con
->http
.fd
, buf
, 1, MSG_PEEK
) == 1 &&
1040 (!buf
[0] || !strchr("DGHOPT", buf
[0])))
1043 * Encrypt this connection...
1046 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1047 "cupsdReadClient: Saw first byte %02X, auto-negotiating SSL/TLS session...",
1050 cupsdEncryptClient(con
);
1054 #endif /* HAVE_SSL */
1056 switch (con
->http
.state
)
1060 * See if we've received a request line...
1063 if (httpGets(line
, sizeof(line
) - 1, HTTP(con
)) == NULL
)
1065 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1066 "cupsdReadClient: httpGets returned EOF...");
1067 return (cupsdCloseClient(con
));
1071 * Ignore blank request lines...
1074 if (line
[0] == '\0')
1078 * Clear other state variables...
1081 httpClearFields(HTTP(con
));
1083 con
->http
.activity
= time(NULL
);
1084 con
->http
.version
= HTTP_1_0
;
1085 con
->http
.keep_alive
= HTTP_KEEPALIVE_OFF
;
1086 con
->http
.data_encoding
= HTTP_ENCODE_LENGTH
;
1087 con
->http
.data_remaining
= 0;
1088 con
->http
._data_remaining
= 0;
1089 con
->operation
= HTTP_WAITING
;
1092 con
->file_ready
= 0;
1094 con
->username
[0] = '\0';
1095 con
->password
[0] = '\0';
1098 cupsdClearString(&con
->command
);
1099 cupsdClearString(&con
->options
);
1101 if (con
->language
!= NULL
)
1103 cupsLangFree(con
->language
);
1104 con
->language
= NULL
;
1108 * Grab the request line...
1111 switch (sscanf(line
, "%63s%1023s%63s", operation
, con
->uri
, version
))
1114 cupsdLogMessage(CUPSD_LOG_ERROR
,
1115 "Bad request line \"%s\" from %s!", line
,
1116 con
->http
.hostname
);
1117 cupsdSendError(con
, HTTP_BAD_REQUEST
);
1118 return (cupsdCloseClient(con
));
1120 con
->http
.version
= HTTP_0_9
;
1123 if (sscanf(version
, "HTTP/%d.%d", &major
, &minor
) != 2)
1125 cupsdLogMessage(CUPSD_LOG_ERROR
,
1126 "Bad request line \"%s\" from %s!", line
,
1127 con
->http
.hostname
);
1128 cupsdSendError(con
, HTTP_BAD_REQUEST
);
1129 return (cupsdCloseClient(con
));
1134 con
->http
.version
= (http_version_t
)(major
* 100 + minor
);
1135 if (con
->http
.version
== HTTP_1_1
&& KeepAlive
)
1136 con
->http
.keep_alive
= HTTP_KEEPALIVE_ON
;
1138 con
->http
.keep_alive
= HTTP_KEEPALIVE_OFF
;
1142 cupsdSendError(con
, HTTP_NOT_SUPPORTED
);
1143 return (cupsdCloseClient(con
));
1149 * Handle full URLs in the request line...
1152 if (strcmp(con
->uri
, "*"))
1154 char method
[HTTP_MAX_URI
], /* Method/scheme */
1155 userpass
[HTTP_MAX_URI
], /* Username:password */
1156 hostname
[HTTP_MAX_URI
], /* Hostname */
1157 resource
[HTTP_MAX_URI
]; /* Resource path */
1158 int port
; /* Port number */
1162 * Separate the URI into its components...
1165 httpSeparateURI(HTTP_URI_CODING_MOST
, con
->uri
,
1166 method
, sizeof(method
),
1167 userpass
, sizeof(userpass
),
1168 hostname
, sizeof(hostname
), &port
,
1169 resource
, sizeof(resource
));
1172 * Only allow URIs with the servername, localhost, or an IP
1176 if (strcmp(method
, "file") &&
1177 strcasecmp(hostname
, ServerName
) &&
1178 strcasecmp(hostname
, "localhost") &&
1179 !isdigit(hostname
[0]))
1182 * Nope, we don't do proxies...
1185 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad URI \"%s\" in request!",
1187 cupsdSendError(con
, HTTP_METHOD_NOT_ALLOWED
);
1188 return (cupsdCloseClient(con
));
1192 * Copy the resource portion back into the URI; both resource and
1193 * con->uri are HTTP_MAX_URI bytes in size...
1196 strcpy(con
->uri
, resource
);
1200 * Process the request...
1203 if (!strcmp(operation
, "GET"))
1204 con
->http
.state
= HTTP_GET
;
1205 else if (!strcmp(operation
, "PUT"))
1206 con
->http
.state
= HTTP_PUT
;
1207 else if (!strcmp(operation
, "POST"))
1208 con
->http
.state
= HTTP_POST
;
1209 else if (!strcmp(operation
, "DELETE"))
1210 con
->http
.state
= HTTP_DELETE
;
1211 else if (!strcmp(operation
, "TRACE"))
1212 con
->http
.state
= HTTP_TRACE
;
1213 else if (!strcmp(operation
, "OPTIONS"))
1214 con
->http
.state
= HTTP_OPTIONS
;
1215 else if (!strcmp(operation
, "HEAD"))
1216 con
->http
.state
= HTTP_HEAD
;
1219 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad operation \"%s\"!", operation
);
1220 cupsdSendError(con
, HTTP_BAD_REQUEST
);
1221 return (cupsdCloseClient(con
));
1224 con
->start
= time(NULL
);
1225 con
->operation
= con
->http
.state
;
1227 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdReadClient: %d %s %s HTTP/%d.%d", con
->http
.fd
,
1228 operation
, con
->uri
,
1229 con
->http
.version
/ 100, con
->http
.version
% 100);
1231 con
->http
.status
= HTTP_OK
;
1241 * Parse incoming parameters until the status changes...
1244 status
= httpUpdate(HTTP(con
));
1246 if (status
!= HTTP_OK
&& status
!= HTTP_CONTINUE
)
1248 cupsdSendError(con
, HTTP_BAD_REQUEST
);
1249 return (cupsdCloseClient(con
));
1254 break; /* Anti-compiler-warning-code */
1258 * Handle new transfers...
1261 if (status
== HTTP_OK
)
1263 if (con
->http
.fields
[HTTP_FIELD_ACCEPT_LANGUAGE
][0])
1266 * Figure out the locale from the Accept-Language and Content-Type
1270 if ((ptr
= strchr(con
->http
.fields
[HTTP_FIELD_ACCEPT_LANGUAGE
], ',')) != NULL
)
1273 if ((ptr
= strchr(con
->http
.fields
[HTTP_FIELD_ACCEPT_LANGUAGE
], ';')) != NULL
)
1276 if ((ptr
= strstr(con
->http
.fields
[HTTP_FIELD_CONTENT_TYPE
], "charset=")) != NULL
)
1279 * Combine language and charset, and trim any extra params in the
1283 snprintf(locale
, sizeof(locale
), "%s.%s",
1284 con
->http
.fields
[HTTP_FIELD_ACCEPT_LANGUAGE
], ptr
+ 8);
1286 if ((ptr
= strchr(locale
, ',')) != NULL
)
1290 snprintf(locale
, sizeof(locale
), "%s.%s",
1291 con
->http
.fields
[HTTP_FIELD_ACCEPT_LANGUAGE
], DefaultCharset
);
1293 con
->language
= cupsLangGet(locale
);
1296 con
->language
= cupsLangGet(DefaultLocale
);
1298 cupsdAuthorize(con
);
1300 if (!strncmp(con
->http
.fields
[HTTP_FIELD_CONNECTION
], "Keep-Alive", 10) &&
1302 con
->http
.keep_alive
= HTTP_KEEPALIVE_ON
;
1304 if (!con
->http
.fields
[HTTP_FIELD_HOST
][0] &&
1305 con
->http
.version
>= HTTP_1_1
)
1308 * HTTP/1.1 and higher require the "Host:" field...
1311 if (!cupsdSendError(con
, HTTP_BAD_REQUEST
))
1312 return (cupsdCloseClient(con
));
1314 else if (con
->operation
== HTTP_OPTIONS
)
1317 * Do OPTIONS command...
1320 if (con
->best
&& con
->best
->type
!= AUTH_NONE
)
1322 if (!cupsdSendHeader(con
, HTTP_UNAUTHORIZED
, NULL
))
1323 return (cupsdCloseClient(con
));
1326 if (!strcasecmp(con
->http
.fields
[HTTP_FIELD_CONNECTION
], "Upgrade") &&
1327 con
->http
.tls
== NULL
)
1331 * Do encryption stuff...
1334 if (!cupsdSendHeader(con
, HTTP_SWITCHING_PROTOCOLS
, NULL
))
1335 return (cupsdCloseClient(con
));
1337 httpPrintf(HTTP(con
), "Connection: Upgrade\r\n");
1338 httpPrintf(HTTP(con
), "Upgrade: TLS/1.0,HTTP/1.1\r\n");
1339 httpPrintf(HTTP(con
), "Content-Length: 0\r\n");
1340 httpPrintf(HTTP(con
), "\r\n");
1342 cupsdEncryptClient(con
);
1344 if (!cupsdSendError(con
, HTTP_NOT_IMPLEMENTED
))
1345 return (cupsdCloseClient(con
));
1346 #endif /* HAVE_SSL */
1349 if (!cupsdSendHeader(con
, HTTP_OK
, NULL
))
1350 return (cupsdCloseClient(con
));
1352 httpPrintf(HTTP(con
), "Allow: GET, HEAD, OPTIONS, POST, PUT\r\n");
1353 httpPrintf(HTTP(con
), "Content-Length: 0\r\n");
1354 httpPrintf(HTTP(con
), "\r\n");
1356 else if (!is_path_absolute(con
->uri
))
1359 * Protect against malicious users!
1362 if (!cupsdSendError(con
, HTTP_FORBIDDEN
))
1363 return (cupsdCloseClient(con
));
1367 if (!strcasecmp(con
->http
.fields
[HTTP_FIELD_CONNECTION
], "Upgrade") &&
1368 con
->http
.tls
== NULL
)
1372 * Do encryption stuff...
1375 if (!cupsdSendHeader(con
, HTTP_SWITCHING_PROTOCOLS
, NULL
))
1376 return (cupsdCloseClient(con
));
1378 httpPrintf(HTTP(con
), "Connection: Upgrade\r\n");
1379 httpPrintf(HTTP(con
), "Upgrade: TLS/1.0,HTTP/1.1\r\n");
1380 httpPrintf(HTTP(con
), "Content-Length: 0\r\n");
1381 httpPrintf(HTTP(con
), "\r\n");
1383 cupsdEncryptClient(con
);
1385 if (!cupsdSendError(con
, HTTP_NOT_IMPLEMENTED
))
1386 return (cupsdCloseClient(con
));
1387 #endif /* HAVE_SSL */
1390 if ((status
= cupsdIsAuthorized(con
, NULL
)) != HTTP_OK
)
1392 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1393 "cupsdReadClient: Unauthorized request for %s...\n",
1395 cupsdSendError(con
, status
);
1396 return (cupsdCloseClient(con
));
1399 if (con
->http
.expect
&&
1400 (con
->operation
== HTTP_POST
|| con
->operation
== HTTP_PUT
))
1402 if (con
->http
.expect
== HTTP_CONTINUE
)
1405 * Send 100-continue header...
1408 if (!cupsdSendHeader(con
, HTTP_CONTINUE
, NULL
))
1409 return (cupsdCloseClient(con
));
1414 * Send 417-expectation-failed header...
1417 if (!cupsdSendHeader(con
, HTTP_EXPECTATION_FAILED
, NULL
))
1418 return (cupsdCloseClient(con
));
1420 httpPrintf(HTTP(con
), "Content-Length: 0\r\n");
1421 httpPrintf(HTTP(con
), "\r\n");
1425 switch (con
->http
.state
)
1427 case HTTP_GET_SEND
:
1428 if (!strncmp(con
->uri
, "/printers/", 10) &&
1429 !strcmp(con
->uri
+ strlen(con
->uri
) - 4, ".ppd"))
1432 * Send PPD file - get the real printer name since printer
1433 * names are not case sensitive but filenames can be...
1436 con
->uri
[strlen(con
->uri
) - 4] = '\0'; /* Drop ".ppd" */
1438 if ((p
= cupsdFindPrinter(con
->uri
+ 10)) != NULL
)
1439 snprintf(con
->uri
, sizeof(con
->uri
), "/ppd/%s.ppd", p
->name
);
1442 if (!cupsdSendError(con
, HTTP_NOT_FOUND
))
1443 return (cupsdCloseClient(con
));
1449 if ((!strncmp(con
->uri
, "/admin", 6) &&
1450 strncmp(con
->uri
, "/admin/conf/", 12) &&
1451 strncmp(con
->uri
, "/admin/log/", 11)) ||
1452 !strncmp(con
->uri
, "/printers", 9) ||
1453 !strncmp(con
->uri
, "/classes", 8) ||
1454 !strncmp(con
->uri
, "/help", 5) ||
1455 !strncmp(con
->uri
, "/jobs", 5))
1458 * Send CGI output...
1461 if (!strncmp(con
->uri
, "/admin", 6))
1463 cupsdSetStringf(&con
->command
, "%s/cgi-bin/admin.cgi",
1466 cupsdSetString(&con
->options
, strchr(con
->uri
+ 6, '?'));
1468 else if (!strncmp(con
->uri
, "/printers", 9))
1470 cupsdSetStringf(&con
->command
, "%s/cgi-bin/printers.cgi",
1473 if (con
->uri
[9] && con
->uri
[10])
1474 cupsdSetString(&con
->options
, con
->uri
+ 9);
1476 cupsdSetString(&con
->options
, NULL
);
1478 else if (!strncmp(con
->uri
, "/classes", 8))
1480 cupsdSetStringf(&con
->command
, "%s/cgi-bin/classes.cgi",
1483 if (con
->uri
[8] && con
->uri
[9])
1484 cupsdSetString(&con
->options
, con
->uri
+ 8);
1486 cupsdSetString(&con
->options
, NULL
);
1488 else if (!strncmp(con
->uri
, "/jobs", 5))
1490 cupsdSetStringf(&con
->command
, "%s/cgi-bin/jobs.cgi",
1493 if (con
->uri
[5] && con
->uri
[6])
1494 cupsdSetString(&con
->options
, con
->uri
+ 5);
1496 cupsdSetString(&con
->options
, NULL
);
1500 cupsdSetStringf(&con
->command
, "%s/cgi-bin/help.cgi",
1503 if (con
->uri
[5] && con
->uri
[6])
1504 cupsdSetString(&con
->options
, con
->uri
+ 5);
1506 cupsdSetString(&con
->options
, NULL
);
1509 if (!cupsdSendCommand(con
, con
->command
, con
->options
, 0))
1511 if (!cupsdSendError(con
, HTTP_NOT_FOUND
))
1512 return (cupsdCloseClient(con
));
1515 cupsdLogRequest(con
, HTTP_OK
);
1517 if (con
->http
.version
<= HTTP_1_0
)
1518 con
->http
.keep_alive
= HTTP_KEEPALIVE_OFF
;
1520 else if ((!strncmp(con
->uri
, "/admin/conf/", 12) &&
1521 (strchr(con
->uri
+ 12, '/') ||
1522 strlen(con
->uri
) == 12)) ||
1523 (!strncmp(con
->uri
, "/admin/log/", 11) &&
1524 (strchr(con
->uri
+ 11, '/') ||
1525 strlen(con
->uri
) == 11)))
1528 * GET can only be done to configuration files under
1532 if (!cupsdSendError(con
, HTTP_FORBIDDEN
))
1533 return (cupsdCloseClient(con
));
1543 if ((filename
= get_file(con
, &filestats
, buf
,
1544 sizeof(buf
))) == NULL
)
1546 if (!cupsdSendError(con
, HTTP_NOT_FOUND
))
1547 return (cupsdCloseClient(con
));
1552 type
= mimeFileType(MimeDatabase
, filename
, NULL
, NULL
);
1554 if (cupsdIsCGI(con
, filename
, &filestats
, type
))
1557 * Note: con->command and con->options were set by
1561 if (!cupsdSendCommand(con
, con
->command
, con
->options
, 0))
1563 if (!cupsdSendError(con
, HTTP_NOT_FOUND
))
1564 return (cupsdCloseClient(con
));
1567 cupsdLogRequest(con
, HTTP_OK
);
1569 if (con
->http
.version
<= HTTP_1_0
)
1570 con
->http
.keep_alive
= HTTP_KEEPALIVE_OFF
;
1574 if (!check_if_modified(con
, &filestats
))
1576 if (!cupsdSendError(con
, HTTP_NOT_MODIFIED
))
1577 return (cupsdCloseClient(con
));
1582 strcpy(line
, "text/plain");
1584 snprintf(line
, sizeof(line
), "%s/%s", type
->super
, type
->type
);
1586 if (!cupsdSendFile(con
, HTTP_OK
, filename
, line
, &filestats
))
1587 return (cupsdCloseClient(con
));
1592 case HTTP_POST_RECV
:
1594 * See if the POST request includes a Content-Length field, and if
1595 * so check the length against any limits that are set...
1598 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "POST %s", con
->uri
);
1599 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "CONTENT_TYPE = %s",
1600 con
->http
.fields
[HTTP_FIELD_CONTENT_TYPE
]);
1602 if (con
->http
.fields
[HTTP_FIELD_CONTENT_LENGTH
][0] &&
1603 MaxRequestSize
> 0 &&
1604 con
->http
.data_remaining
> MaxRequestSize
)
1607 * Request too large...
1610 if (!cupsdSendError(con
, HTTP_REQUEST_TOO_LARGE
))
1611 return (cupsdCloseClient(con
));
1615 else if (con
->http
.data_remaining
< 0)
1618 * Negative content lengths are invalid!
1621 if (!cupsdSendError(con
, HTTP_BAD_REQUEST
))
1622 return (cupsdCloseClient(con
));
1628 * See what kind of POST request this is; for IPP requests the
1629 * content-type field will be "application/ipp"...
1632 if (!strcmp(con
->http
.fields
[HTTP_FIELD_CONTENT_TYPE
],
1634 con
->request
= ippNew();
1635 else if ((!strncmp(con
->uri
, "/admin", 6) &&
1636 strncmp(con
->uri
, "/admin/conf/", 12) &&
1637 strncmp(con
->uri
, "/admin/log/", 11)) ||
1638 !strncmp(con
->uri
, "/printers", 9) ||
1639 !strncmp(con
->uri
, "/classes", 8) ||
1640 !strncmp(con
->uri
, "/help", 5) ||
1641 !strncmp(con
->uri
, "/jobs", 5))
1647 if (!strncmp(con
->uri
, "/admin", 6))
1649 cupsdSetStringf(&con
->command
, "%s/cgi-bin/admin.cgi",
1652 cupsdSetString(&con
->options
, strchr(con
->uri
+ 6, '?'));
1654 else if (!strncmp(con
->uri
, "/printers", 9))
1656 cupsdSetStringf(&con
->command
, "%s/cgi-bin/printers.cgi",
1659 if (con
->uri
[9] && con
->uri
[10])
1660 cupsdSetString(&con
->options
, con
->uri
+ 9);
1662 cupsdSetString(&con
->options
, NULL
);
1664 else if (!strncmp(con
->uri
, "/classes", 8))
1666 cupsdSetStringf(&con
->command
, "%s/cgi-bin/classes.cgi",
1669 if (con
->uri
[8] && con
->uri
[9])
1670 cupsdSetString(&con
->options
, con
->uri
+ 8);
1672 cupsdSetString(&con
->options
, NULL
);
1674 else if (!strncmp(con
->uri
, "/jobs", 5))
1676 cupsdSetStringf(&con
->command
, "%s/cgi-bin/jobs.cgi",
1679 if (con
->uri
[5] && con
->uri
[6])
1680 cupsdSetString(&con
->options
, con
->uri
+ 5);
1682 cupsdSetString(&con
->options
, NULL
);
1686 cupsdSetStringf(&con
->command
, "%s/cgi-bin/help.cgi",
1689 if (con
->uri
[5] && con
->uri
[6])
1690 cupsdSetString(&con
->options
, con
->uri
+ 5);
1692 cupsdSetString(&con
->options
, NULL
);
1695 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1696 "cupsdReadClient: %d command=\"%s\", "
1698 con
->http
.fd
, con
->command
, con
->options
);
1700 if (con
->http
.version
<= HTTP_1_0
)
1701 con
->http
.keep_alive
= HTTP_KEEPALIVE_OFF
;
1709 if ((filename
= get_file(con
, &filestats
, buf
,
1710 sizeof(buf
))) == NULL
)
1712 if (!cupsdSendError(con
, HTTP_NOT_FOUND
))
1713 return (cupsdCloseClient(con
));
1718 type
= mimeFileType(MimeDatabase
, filename
, NULL
, NULL
);
1720 if (!cupsdIsCGI(con
, filename
, &filestats
, type
))
1723 * Only POST to CGI's...
1726 if (!cupsdSendError(con
, HTTP_UNAUTHORIZED
))
1727 return (cupsdCloseClient(con
));
1732 case HTTP_PUT_RECV
:
1734 * Validate the resource name...
1737 if (strncmp(con
->uri
, "/admin/conf/", 12) ||
1738 strchr(con
->uri
+ 12, '/') ||
1739 strlen(con
->uri
) == 12)
1742 * PUT can only be done to configuration files under
1746 if (!cupsdSendError(con
, HTTP_FORBIDDEN
))
1747 return (cupsdCloseClient(con
));
1753 * See if the PUT request includes a Content-Length field, and if
1754 * so check the length against any limits that are set...
1757 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "PUT %s", con
->uri
);
1758 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "CONTENT_TYPE = %s",
1759 con
->http
.fields
[HTTP_FIELD_CONTENT_TYPE
]);
1761 if (con
->http
.fields
[HTTP_FIELD_CONTENT_LENGTH
][0] &&
1762 MaxRequestSize
> 0 &&
1763 con
->http
.data_remaining
> MaxRequestSize
)
1766 * Request too large...
1769 if (!cupsdSendError(con
, HTTP_REQUEST_TOO_LARGE
))
1770 return (cupsdCloseClient(con
));
1774 else if (con
->http
.data_remaining
< 0)
1777 * Negative content lengths are invalid!
1780 if (!cupsdSendError(con
, HTTP_BAD_REQUEST
))
1781 return (cupsdCloseClient(con
));
1787 * Open a temporary file to hold the request...
1790 cupsdSetStringf(&con
->filename
, "%s/%08x", RequestRoot
,
1792 con
->file
= open(con
->filename
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0640);
1794 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1795 "cupsdReadClient: %d REQUEST %s=%d", con
->http
.fd
,
1796 con
->filename
, con
->file
);
1800 if (!cupsdSendError(con
, HTTP_REQUEST_TOO_LARGE
))
1801 return (cupsdCloseClient(con
));
1804 fchmod(con
->file
, 0640);
1805 fchown(con
->file
, RunUser
, Group
);
1806 fcntl(con
->file
, F_SETFD
, fcntl(con
->file
, F_GETFD
) | FD_CLOEXEC
);
1811 cupsdSendError(con
, HTTP_NOT_IMPLEMENTED
);
1812 return (cupsdCloseClient(con
));
1815 if (!strncmp(con
->uri
, "/printers/", 10) &&
1816 !strcmp(con
->uri
+ strlen(con
->uri
) - 4, ".ppd"))
1819 * Send PPD file - get the real printer name since printer
1820 * names are not case sensitive but filenames can be...
1823 con
->uri
[strlen(con
->uri
) - 4] = '\0'; /* Drop ".ppd" */
1825 if ((p
= cupsdFindPrinter(con
->uri
+ 10)) != NULL
)
1826 snprintf(con
->uri
, sizeof(con
->uri
), "/ppd/%s.ppd", p
->name
);
1829 if (!cupsdSendError(con
, HTTP_NOT_FOUND
))
1830 return (cupsdCloseClient(con
));
1836 if ((!strncmp(con
->uri
, "/admin", 6) &&
1837 strncmp(con
->uri
, "/admin/conf/", 12) &&
1838 strncmp(con
->uri
, "/admin/log/", 11)) ||
1839 !strncmp(con
->uri
, "/printers", 9) ||
1840 !strncmp(con
->uri
, "/classes", 8) ||
1841 !strncmp(con
->uri
, "/help", 5) ||
1842 !strncmp(con
->uri
, "/jobs", 5))
1848 if (!cupsdSendHeader(con
, HTTP_OK
, "text/html"))
1849 return (cupsdCloseClient(con
));
1851 if (httpPrintf(HTTP(con
), "\r\n") < 0)
1852 return (cupsdCloseClient(con
));
1854 cupsdLogRequest(con
, HTTP_OK
);
1856 else if ((!strncmp(con
->uri
, "/admin/conf/", 12) &&
1857 (strchr(con
->uri
+ 12, '/') ||
1858 strlen(con
->uri
) == 12)) ||
1859 (!strncmp(con
->uri
, "/admin/log/", 11) &&
1860 (strchr(con
->uri
+ 11, '/') ||
1861 strlen(con
->uri
) == 11)))
1864 * HEAD can only be done to configuration files under
1868 if (!cupsdSendError(con
, HTTP_FORBIDDEN
))
1869 return (cupsdCloseClient(con
));
1873 else if ((filename
= get_file(con
, &filestats
, buf
,
1874 sizeof(buf
))) == NULL
)
1876 if (!cupsdSendHeader(con
, HTTP_NOT_FOUND
, "text/html"))
1877 return (cupsdCloseClient(con
));
1879 cupsdLogRequest(con
, HTTP_NOT_FOUND
);
1881 else if (!check_if_modified(con
, &filestats
))
1883 if (!cupsdSendError(con
, HTTP_NOT_MODIFIED
))
1884 return (cupsdCloseClient(con
));
1886 cupsdLogRequest(con
, HTTP_NOT_MODIFIED
);
1894 type
= mimeFileType(MimeDatabase
, filename
, NULL
, NULL
);
1896 strcpy(line
, "text/plain");
1898 snprintf(line
, sizeof(line
), "%s/%s", type
->super
, type
->type
);
1900 if (!cupsdSendHeader(con
, HTTP_OK
, line
))
1901 return (cupsdCloseClient(con
));
1903 if (httpPrintf(HTTP(con
), "Last-Modified: %s\r\n",
1904 httpGetDateString(filestats
.st_mtime
)) < 0)
1905 return (cupsdCloseClient(con
));
1907 if (httpPrintf(HTTP(con
), "Content-Length: %lu\r\n",
1908 (unsigned long)filestats
.st_size
) < 0)
1909 return (cupsdCloseClient(con
));
1911 cupsdLogRequest(con
, HTTP_OK
);
1914 if (httpPrintf(HTTP(con
), "\r\n") < 0)
1915 return (cupsdCloseClient(con
));
1917 con
->http
.state
= HTTP_WAITING
;
1921 break; /* Anti-compiler-warning-code */
1927 * Handle any incoming data...
1930 switch (con
->http
.state
)
1932 case HTTP_PUT_RECV
:
1933 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1934 "cupsdReadClient: %d con->data_encoding=HTTP_ENCODE_%s, "
1935 "con->data_remaining=" CUPS_LLFMT
", con->file=%d",
1937 con
->http
.data_encoding
== HTTP_ENCODE_CHUNKED
?
1938 "CHUNKED" : "LENGTH",
1939 CUPS_LLCAST con
->http
.data_remaining
, con
->file
);
1941 if ((bytes
= httpRead2(HTTP(con
), line
, sizeof(line
))) < 0)
1942 return (cupsdCloseClient(con
));
1945 con
->bytes
+= bytes
;
1947 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1948 "cupsdReadClient: %d writing %d bytes to %d",
1949 con
->http
.fd
, bytes
, con
->file
);
1951 if (write(con
->file
, line
, bytes
) < bytes
)
1953 cupsdLogMessage(CUPSD_LOG_ERROR
,
1954 "cupsdReadClient: Unable to write %d bytes to %s: %s",
1955 bytes
, con
->filename
, strerror(errno
));
1957 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1958 "cupsdReadClient: Closing data file %d...",
1963 unlink(con
->filename
);
1964 cupsdClearString(&con
->filename
);
1966 if (!cupsdSendError(con
, HTTP_REQUEST_TOO_LARGE
))
1967 return (cupsdCloseClient(con
));
1971 if (con
->http
.state
== HTTP_WAITING
)
1974 * End of file, see how big it is...
1977 fstat(con
->file
, &filestats
);
1979 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1980 "cupsdReadClient: %d Closing data file %d, size="
1982 con
->http
.fd
, con
->file
,
1983 CUPS_LLCAST filestats
.st_size
);
1988 if (filestats
.st_size
> MaxRequestSize
&&
1992 * Request is too big; remove it and send an error...
1995 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1996 "cupsdReadClient: %d Removing temp file %s",
1997 con
->http
.fd
, con
->filename
);
1998 unlink(con
->filename
);
1999 cupsdClearString(&con
->filename
);
2001 if (!cupsdSendError(con
, HTTP_REQUEST_TOO_LARGE
))
2002 return (cupsdCloseClient(con
));
2006 * Install the configuration file...
2009 status
= install_conf_file(con
);
2012 * Return the status to the client...
2015 if (!cupsdSendError(con
, status
))
2016 return (cupsdCloseClient(con
));
2020 case HTTP_POST_RECV
:
2021 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2022 "cupsdReadClient: %d con->data_encoding=HTTP_ENCODE_"
2023 "%s, con->data_remaining=" CUPS_LLFMT
", con->file=%d",
2025 con
->http
.data_encoding
== HTTP_ENCODE_CHUNKED
?
2026 "CHUNKED" : "LENGTH",
2027 CUPS_LLCAST con
->http
.data_remaining
, con
->file
);
2029 if (con
->request
!= NULL
)
2032 * Grab any request data from the connection...
2035 if ((ipp_state
= ippRead(&(con
->http
), con
->request
)) == IPP_ERROR
)
2037 cupsdLogMessage(CUPSD_LOG_ERROR
,
2038 "cupsdReadClient: %d IPP Read Error!",
2041 cupsdSendError(con
, HTTP_BAD_REQUEST
);
2042 return (cupsdCloseClient(con
));
2044 else if (ipp_state
!= IPP_DATA
)
2046 if (con
->http
.state
== HTTP_POST_SEND
)
2048 cupsdSendError(con
, HTTP_BAD_REQUEST
);
2049 return (cupsdCloseClient(con
));
2055 con
->bytes
+= ippLength(con
->request
);
2058 if (con
->file
< 0 && con
->http
.state
!= HTTP_POST_SEND
)
2061 * Create a file as needed for the request data...
2064 cupsdSetStringf(&con
->filename
, "%s/%08x", RequestRoot
, request_id
++);
2065 con
->file
= open(con
->filename
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0640);
2067 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdReadClient: %d REQUEST %s=%d", con
->http
.fd
,
2068 con
->filename
, con
->file
);
2072 if (!cupsdSendError(con
, HTTP_REQUEST_TOO_LARGE
))
2073 return (cupsdCloseClient(con
));
2076 fchmod(con
->file
, 0640);
2077 fchown(con
->file
, RunUser
, Group
);
2078 fcntl(con
->file
, F_SETFD
, fcntl(con
->file
, F_GETFD
) | FD_CLOEXEC
);
2081 if (con
->http
.state
!= HTTP_POST_SEND
)
2083 if ((bytes
= httpRead2(HTTP(con
), line
, sizeof(line
))) < 0)
2084 return (cupsdCloseClient(con
));
2087 con
->bytes
+= bytes
;
2089 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2090 "cupsdReadClient: %d writing %d bytes to %d",
2091 con
->http
.fd
, bytes
, con
->file
);
2093 if (write(con
->file
, line
, bytes
) < bytes
)
2095 cupsdLogMessage(CUPSD_LOG_ERROR
,
2096 "cupsdReadClient: Unable to write %d bytes to %s: %s",
2097 bytes
, con
->filename
, strerror(errno
));
2099 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2100 "cupsdReadClient: Closing file %d...",
2105 unlink(con
->filename
);
2106 cupsdClearString(&con
->filename
);
2108 if (!cupsdSendError(con
, HTTP_REQUEST_TOO_LARGE
))
2109 return (cupsdCloseClient(con
));
2112 else if (con
->http
.state
== HTTP_POST_RECV
)
2113 return (1); /* ??? */
2114 else if (con
->http
.state
!= HTTP_POST_SEND
)
2115 return (cupsdCloseClient(con
));
2118 if (con
->http
.state
== HTTP_POST_SEND
)
2122 fstat(con
->file
, &filestats
);
2124 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2125 "cupsdReadClient: %d Closing data file %d, "
2126 "size=" CUPS_LLFMT
".",
2127 con
->http
.fd
, con
->file
,
2128 CUPS_LLCAST filestats
.st_size
);
2133 if (filestats
.st_size
> MaxRequestSize
&&
2137 * Request is too big; remove it and send an error...
2140 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2141 "cupsdReadClient: %d Removing temp file %s",
2142 con
->http
.fd
, con
->filename
);
2143 unlink(con
->filename
);
2144 cupsdClearString(&con
->filename
);
2149 * Delete any IPP request data...
2152 ippDelete(con
->request
);
2153 con
->request
= NULL
;
2156 if (!cupsdSendError(con
, HTTP_REQUEST_TOO_LARGE
))
2157 return (cupsdCloseClient(con
));
2162 if (!cupsdSendCommand(con
, con
->command
, con
->options
, 0))
2164 if (!cupsdSendError(con
, HTTP_NOT_FOUND
))
2165 return (cupsdCloseClient(con
));
2168 cupsdLogRequest(con
, HTTP_OK
);
2173 return (cupsdProcessIPPRequest(con
));
2178 break; /* Anti-compiler-warning-code */
2181 if (!con
->http
.keep_alive
&& con
->http
.state
== HTTP_WAITING
)
2182 return (cupsdCloseClient(con
));
2189 * 'cupsdSendCommand()' - Send output from a command via HTTP.
2192 int /* O - 1 on success, 0 on failure */
2194 cupsd_client_t
*con
, /* I - Client connection */
2195 char *command
, /* I - Command to run */
2196 char *options
, /* I - Command-line options */
2197 int root
) /* I - Run as root? */
2199 int fd
; /* Standard input file descriptor */
2203 fd
= open(con
->filename
, O_RDONLY
);
2205 fd
= open("/dev/null", O_RDONLY
);
2209 cupsdLogMessage(CUPSD_LOG_ERROR
,
2210 "cupsdSendCommand: %d Unable to open \"%s\" for reading: %s",
2211 con
->http
.fd
, con
->filename
? con
->filename
: "/dev/null",
2216 fcntl(fd
, F_SETFD
, fcntl(fd
, F_GETFD
) | FD_CLOEXEC
);
2218 con
->pipe_pid
= pipe_command(con
, fd
, &(con
->file
), command
, options
, root
);
2222 cupsdLogMessage(CUPSD_LOG_INFO
, "Started \"%s\" (pid=%d)", command
,
2225 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdSendCommand: %d file=%d",
2226 con
->http
.fd
, con
->file
);
2228 if (con
->pipe_pid
== 0)
2231 fcntl(con
->file
, F_SETFD
, fcntl(con
->file
, F_GETFD
) | FD_CLOEXEC
);
2233 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2234 "cupsdSendCommand: Adding fd %d to InputSet...", con
->file
);
2235 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2236 "cupsdSendCommand: Adding fd %d to OutputSet...",
2239 FD_SET(con
->file
, InputSet
);
2240 FD_SET(con
->http
.fd
, OutputSet
);
2242 con
->sent_header
= 0;
2243 con
->file_ready
= 0;
2244 con
->got_fields
= 0;
2252 * 'cupsdSendError()' - Send an error message via HTTP.
2255 int /* O - 1 if successful, 0 otherwise */
2256 cupsdSendError(cupsd_client_t
*con
, /* I - Connection */
2257 http_status_t code
) /* I - Error code */
2260 * Put the request in the access_log file...
2263 cupsdLogRequest(con
, code
);
2265 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdSendError: %d code=%d (%s)",
2266 con
->http
.fd
, code
, httpStatus(code
));
2269 * To work around bugs in some proxies, don't use Keep-Alive for some
2273 if (code
>= HTTP_BAD_REQUEST
)
2274 con
->http
.keep_alive
= HTTP_KEEPALIVE_OFF
;
2277 * Send an error message back to the client. If the error code is a
2278 * 400 or 500 series, make sure the message contains some text, too!
2281 if (!cupsdSendHeader(con
, code
, NULL
))
2285 if (code
== HTTP_UPGRADE_REQUIRED
)
2286 if (httpPrintf(HTTP(con
), "Connection: Upgrade\r\n") < 0)
2289 if (httpPrintf(HTTP(con
), "Upgrade: TLS/1.0,HTTP/1.1\r\n") < 0)
2291 #endif /* HAVE_SSL */
2293 if ((con
->http
.version
>= HTTP_1_1
&& !con
->http
.keep_alive
) ||
2294 (code
>= HTTP_BAD_REQUEST
&& code
!= HTTP_UPGRADE_REQUIRED
))
2296 if (httpPrintf(HTTP(con
), "Connection: close\r\n") < 0)
2300 if (code
>= HTTP_BAD_REQUEST
)
2303 * Send a human-readable error message.
2306 char message
[1024]; /* Message for user */
2307 const char *text
; /* Status-specific text */
2309 if (code
== HTTP_UNAUTHORIZED
)
2310 text
= _cupsLangString(con
->language
,
2311 _("Enter your username and password or the "
2312 "root username and password to access this "
2314 else if (code
== HTTP_UPGRADE_REQUIRED
)
2315 text
= _cupsLangString(con
->language
,
2316 _("You must use a https: URL to access this "
2321 snprintf(message
, sizeof(message
),
2322 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\" "
2323 "\"http://www.w3.org/TR/REC-html40/loose.dtd\">\n"
2326 "\t<META HTTP-EQUIV=\"Content-Type\" "
2327 "CONTENT=\"text/html; charset=utf-8\">\n"
2328 "\t<TITLE>%d %s</TITLE>\n"
2329 "\t<LINK REL=\"STYLESHEET\" TYPE=\"text/css\" "
2330 "HREF=\"/cups.css\">\n"
2337 code
, httpStatus(code
), code
, httpStatus(code
), text
);
2339 if (httpPrintf(HTTP(con
), "Content-Type: text/html; charset=utf-8\r\n") < 0)
2341 if (httpPrintf(HTTP(con
), "Content-Length: %d\r\n",
2342 (int)strlen(message
)) < 0)
2344 if (httpPrintf(HTTP(con
), "\r\n") < 0)
2346 if (httpPrintf(HTTP(con
), "%s", message
) < 0)
2349 else if (httpPrintf(HTTP(con
), "\r\n") < 0)
2352 con
->http
.state
= HTTP_WAITING
;
2359 * 'cupsdSendFile()' - Send a file via HTTP.
2362 int /* O - 0 on failure, 1 on success */
2363 cupsdSendFile(cupsd_client_t
*con
, /* I - Client connection */
2364 http_status_t code
, /* I - HTTP status */
2365 char *filename
, /* I - Filename */
2366 char *type
, /* I - File type */
2367 struct stat
*filestats
)/* O - File information */
2369 con
->file
= open(filename
, O_RDONLY
);
2371 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdSendFile: %d file=%d", con
->http
.fd
,
2377 fcntl(con
->file
, F_SETFD
, fcntl(con
->file
, F_GETFD
) | FD_CLOEXEC
);
2381 if (!cupsdSendHeader(con
, code
, type
))
2384 if (httpPrintf(HTTP(con
), "Last-Modified: %s\r\n",
2385 httpGetDateString(filestats
->st_mtime
)) < 0)
2387 if (httpPrintf(HTTP(con
), "Content-Length: " CUPS_LLFMT
"\r\n",
2388 CUPS_LLCAST filestats
->st_size
) < 0)
2390 if (httpPrintf(HTTP(con
), "\r\n") < 0)
2393 con
->http
.data_encoding
= HTTP_ENCODE_LENGTH
;
2394 con
->http
.data_remaining
= filestats
->st_size
;
2396 if (con
->http
.data_remaining
<= INT_MAX
)
2397 con
->http
._data_remaining
= con
->http
.data_remaining
;
2399 con
->http
._data_remaining
= INT_MAX
;
2401 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2402 "cupsdSendFile: Adding fd %d to OutputSet...", con
->http
.fd
);
2404 FD_SET(con
->http
.fd
, OutputSet
);
2411 * 'cupsdSendHeader()' - Send an HTTP request.
2414 int /* O - 1 on success, 0 on failure */
2415 cupsdSendHeader(cupsd_client_t
*con
, /* I - Client to send to */
2416 http_status_t code
, /* I - HTTP status code */
2417 char *type
) /* I - MIME type of document */
2419 if (httpPrintf(HTTP(con
), "HTTP/%d.%d %d %s\r\n", con
->http
.version
/ 100,
2420 con
->http
.version
% 100, code
, httpStatus(code
)) < 0)
2423 if (code
== HTTP_CONTINUE
)
2425 if (httpPrintf(HTTP(con
), "\r\n") < 0)
2431 if (httpPrintf(HTTP(con
), "Date: %s\r\n", httpGetDateString(time(NULL
))) < 0)
2434 if (httpPrintf(HTTP(con
), "Server: %s\r\n", ServerHeader
) < 0)
2436 if (con
->http
.keep_alive
&& con
->http
.version
>= HTTP_1_0
)
2438 if (httpPrintf(HTTP(con
), "Connection: Keep-Alive\r\n") < 0)
2440 if (httpPrintf(HTTP(con
), "Keep-Alive: timeout=%d\r\n",
2441 KeepAliveTimeout
) < 0)
2444 if (code
== HTTP_METHOD_NOT_ALLOWED
)
2445 if (httpPrintf(HTTP(con
), "Allow: GET, HEAD, OPTIONS, POST\r\n") < 0)
2448 if (code
== HTTP_UNAUTHORIZED
)
2450 int auth_type
; /* Authentication type */
2453 if (!con
->best
|| con
->best
->type
== AUTH_NONE
)
2454 auth_type
= DefaultAuthType
;
2456 auth_type
= con
->best
->type
;
2458 if (auth_type
!= AUTH_DIGEST
)
2460 if (httpPrintf(HTTP(con
),
2461 "WWW-Authenticate: Basic realm=\"CUPS\"\r\n") < 0)
2466 if (httpPrintf(HTTP(con
),
2467 "WWW-Authenticate: Digest realm=\"CUPS\", nonce=\"%s\"\r\n",
2468 con
->http
.hostname
) < 0)
2473 if (con
->language
!= NULL
)
2475 if (httpPrintf(HTTP(con
), "Content-Language: %s\r\n",
2476 con
->language
->language
) < 0)
2482 if (!strcmp(type
, "text/html"))
2484 if (httpPrintf(HTTP(con
),
2485 "Content-Type: text/html; charset=utf-8\r\n") < 0)
2488 else if (httpPrintf(HTTP(con
), "Content-Type: %s\r\n", type
) < 0)
2497 * 'cupsdUpdateCGI()' - Read status messages from CGI scripts and programs.
2501 cupsdUpdateCGI(void)
2503 char *ptr
, /* Pointer to end of line in buffer */
2504 message
[1024]; /* Pointer to message text */
2505 int loglevel
; /* Log level for message */
2508 while ((ptr
= cupsdStatBufUpdate(CGIStatusBuffer
, &loglevel
,
2509 message
, sizeof(message
))) != NULL
)
2510 if (!strchr(CGIStatusBuffer
->buffer
, '\n'))
2513 if (ptr
== NULL
&& errno
)
2516 * Fatal error on pipe - should never happen!
2519 cupsdLogMessage(CUPSD_LOG_CRIT
,
2520 "cupsdUpdateCGI: error reading from CGI error pipe - %s",
2527 * 'cupsdWriteClient()' - Write data to a client as needed.
2530 int /* O - 1 if success, 0 if fail */
2531 cupsdWriteClient(cupsd_client_t
*con
) /* I - Client connection */
2533 int bytes
; /* Number of bytes written */
2534 char buf
[16385]; /* Data buffer */
2535 char *bufptr
; /* Pointer into buffer */
2536 ipp_state_t ipp_state
; /* IPP state value */
2540 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2541 "cupsdWriteClient(con=%p) %d response=%p, file=%d "
2542 "pipe_pid=%d state=%d",
2543 con
, con
->http
.fd
, con
->response
, con
->file
, con
->pipe_pid
,
2547 if (con
->http
.state
!= HTTP_GET_SEND
&&
2548 con
->http
.state
!= HTTP_POST_SEND
)
2551 if (con
->response
!= NULL
)
2553 ipp_state
= ippWrite(&(con
->http
), con
->response
);
2554 bytes
= ipp_state
!= IPP_ERROR
&& ipp_state
!= IPP_DATA
;
2556 else if ((bytes
= read(con
->file
, buf
, sizeof(buf
) - 1)) > 0)
2558 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2559 "cupsdWriteClient: Read %d bytes from file %d...",
2562 if (con
->pipe_pid
&& !con
->got_fields
)
2565 * Inspect the data for Content-Type and other fields.
2570 for (bufptr
= buf
; !con
->got_fields
&& *bufptr
; bufptr
++)
2571 if (*bufptr
== '\n')
2574 * Send line to client...
2577 if (bufptr
> buf
&& bufptr
[-1] == '\r')
2581 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "Script header: %s", buf
);
2583 if (!con
->sent_header
)
2586 * Handle redirection and CGI status codes...
2589 if (!strncasecmp(buf
, "Location:", 9))
2590 cupsdSendHeader(con
, HTTP_SEE_OTHER
, NULL
);
2591 else if (!strncasecmp(buf
, "Status:", 7))
2592 cupsdSendHeader(con
, atoi(buf
+ 7), NULL
);
2594 cupsdSendHeader(con
, HTTP_OK
, NULL
);
2596 if (con
->http
.version
== HTTP_1_1
)
2598 con
->http
.data_encoding
= HTTP_ENCODE_CHUNKED
;
2600 if (httpPrintf(HTTP(con
), "Transfer-Encoding: chunked\r\n") < 0)
2604 con
->sent_header
= 1;
2607 if (strncasecmp(buf
, "Status:", 7))
2608 httpPrintf(HTTP(con
), "%s\r\n", buf
);
2610 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdWriteClient: %d %s",
2617 bytes
-= (bufptr
- buf
);
2618 memmove(buf
, bufptr
, bytes
+ 1);
2622 * See if the line was empty...
2625 if (con
->field_col
== 0)
2626 con
->got_fields
= 1;
2630 else if (*bufptr
!= '\r')
2633 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2634 "cupsdWriteClient: %d bytes=%d, got_fields=%d",
2635 con
->http
.fd
, bytes
, con
->got_fields
);
2637 if (bytes
> 0 && !con
->got_fields
)
2640 * Remaining text needs to go out...
2643 httpPrintf(HTTP(con
), "%s", buf
);
2645 con
->http
.activity
= time(NULL
);
2648 else if (bytes
== 0)
2650 con
->http
.activity
= time(NULL
);
2655 if (httpWrite2(HTTP(con
), buf
, bytes
) < 0)
2657 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2658 "cupsdWriteClient: %d Write of %d bytes failed!",
2659 con
->http
.fd
, bytes
);
2661 cupsdCloseClient(con
);
2665 con
->bytes
+= bytes
;
2667 if (con
->http
.state
== HTTP_WAITING
)
2673 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdWriteClient: %d bytes < 0",
2676 cupsdLogRequest(con
, HTTP_OK
);
2678 httpFlushWrite(HTTP(con
));
2680 if (con
->http
.data_encoding
== HTTP_ENCODE_CHUNKED
)
2682 if (httpPrintf(HTTP(con
), "0\r\n\r\n") < 0)
2684 cupsdCloseClient(con
);
2689 con
->http
.state
= HTTP_WAITING
;
2691 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2692 "cupsdWriteClient: Removing fd %d from OutputSet...",
2695 FD_CLR(con
->http
.fd
, OutputSet
);
2699 if (FD_ISSET(con
->file
, InputSet
))
2701 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2702 "cupsdWriteClient: Removing fd %d from InputSet...",
2704 FD_CLR(con
->file
, InputSet
);
2708 cupsdEndProcess(con
->pipe_pid
, 0);
2710 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2711 "cupsdWriteClient: %d Closing data file %d.",
2712 con
->http
.fd
, con
->file
);
2721 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2722 "cupsdWriteClient: %d Removing temp file %s",
2723 con
->http
.fd
, con
->filename
);
2724 unlink(con
->filename
);
2725 cupsdClearString(&con
->filename
);
2728 if (con
->request
!= NULL
)
2730 ippDelete(con
->request
);
2731 con
->request
= NULL
;
2734 if (con
->response
!= NULL
)
2736 ippDelete(con
->response
);
2737 con
->response
= NULL
;
2740 cupsdClearString(&con
->command
);
2741 cupsdClearString(&con
->options
);
2743 if (!con
->http
.keep_alive
)
2745 cupsdCloseClient(con
);
2751 con
->file_ready
= 0;
2753 if (con
->pipe_pid
&& !FD_ISSET(con
->file
, InputSet
))
2755 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2756 "cupsdWriteClient: Adding fd %d to InputSet...",
2758 FD_SET(con
->file
, InputSet
);
2762 con
->http
.activity
= time(NULL
);
2769 * 'check_if_modified()' - Decode an "If-Modified-Since" line.
2772 static int /* O - 1 if modified since */
2774 cupsd_client_t
*con
, /* I - Client connection */
2775 struct stat
*filestats
) /* I - File information */
2777 char *ptr
; /* Pointer into field */
2778 time_t date
; /* Time/date value */
2779 off_t size
; /* Size/length value */
2784 ptr
= con
->http
.fields
[HTTP_FIELD_IF_MODIFIED_SINCE
];
2789 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2790 "check_if_modified: %d If-Modified-Since=\"%s\"",
2793 while (*ptr
!= '\0')
2795 while (isspace(*ptr
) || *ptr
== ';')
2798 if (strncasecmp(ptr
, "length=", 7) == 0)
2801 size
= strtoll(ptr
, NULL
, 10);
2803 while (isdigit(*ptr
))
2806 else if (isalpha(*ptr
))
2808 date
= httpGetDateTime(ptr
);
2809 while (*ptr
!= '\0' && *ptr
!= ';')
2814 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2815 "check_if_modified: %d sizes=" CUPS_LLFMT
","
2816 CUPS_LLFMT
" dates=%d,%d",
2817 con
->http
.fd
, CUPS_LLCAST size
,
2818 CUPS_LLCAST filestats
->st_size
, (int)date
,
2819 (int)filestats
->st_mtime
);
2821 return ((size
!= filestats
->st_size
&& size
!= 0) ||
2822 (date
< filestats
->st_mtime
&& date
!= 0) ||
2823 (size
== 0 && date
== 0));
2829 * 'get_cdsa_server_certs()' - Convert a keychain name into the CFArrayRef
2830 * required by SSLSetCertificate.
2832 * For now we assumes that there is exactly one SecIdentity in the
2833 * keychain - i.e. there is exactly one matching cert/private key pair.
2834 * In the future we will search a keychain for a SecIdentity matching a
2835 * specific criteria. We also skip the operation of adding additional
2836 * non-signing certs from the keychain to the CFArrayRef.
2838 * To create a self-signed certificate for testing use the certtool.
2839 * Executing the following as root will do it:
2841 * certtool c k=/Library/Keychains/System.keychain
2844 static CFArrayRef
/* O - Array of certificates */
2845 get_cdsa_server_certs(void)
2847 OSStatus err
; /* Error info */
2848 SecKeychainRef kcRef
; /* Keychain reference */
2849 SecIdentitySearchRef srchRef
; /* Search reference */
2850 SecIdentityRef identity
; /* Identity */
2851 CFArrayRef ca
; /* Certificate array */
2858 err
= SecKeychainOpen(ServerCertificate
, &kcRef
);
2861 cupsdLogMessage(CUPSD_LOG_ERROR
, "Cannot open keychain \"%s\", error %d.",
2862 ServerCertificate
, (int)err
);
2866 * Search for "any" identity matching specified key use;
2867 * in this app, we expect there to be exactly one.
2870 err
= SecIdentitySearchCreate(kcRef
, CSSM_KEYUSE_SIGN
, &srchRef
);
2873 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2874 "Cannot find signing key in keychain \"%s\", error %d",
2875 ServerCertificate
, (int)err
);
2878 err
= SecIdentitySearchCopyNext(srchRef
, &identity
);
2881 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2882 "Cannot find signing key in keychain \"%s\", error %d",
2883 ServerCertificate
, (int)err
);
2886 if (CFGetTypeID(identity
) != SecIdentityGetTypeID())
2887 cupsdLogMessage(CUPSD_LOG_ERROR
,
2888 "SecIdentitySearchCopyNext CFTypeID failure!");
2892 * Found one. Place it in a CFArray.
2893 * TBD: snag other (non-identity) certs from keychain and add them
2897 ca
= CFArrayCreate(NULL
, (const void **)&identity
, 1, &kCFTypeArrayCallBacks
);
2900 cupsdLogMessage(CUPSD_LOG_ERROR
, "CFArrayCreate error");
2903 CFRelease(identity
);
2914 #endif /* HAVE_CDSASSL */
2918 * 'get_file()' - Get a filename and state info.
2921 static char * /* O - Real filename */
2922 get_file(cupsd_client_t
*con
, /* I - Client connection */
2923 struct stat
*filestats
, /* O - File information */
2924 char *filename
, /* IO - Filename buffer */
2925 int len
) /* I - Buffer length */
2927 int status
; /* Status of filesystem calls */
2928 char *ptr
; /* Pointer info filename */
2929 int plen
; /* Remaining length after pointer */
2933 * Need to add DocumentRoot global...
2936 if (!strncmp(con
->uri
, "/ppd/", 5))
2937 snprintf(filename
, len
, "%s%s", ServerRoot
, con
->uri
);
2938 else if (!strncmp(con
->uri
, "/admin/conf/", 12))
2939 snprintf(filename
, len
, "%s%s", ServerRoot
, con
->uri
+ 11);
2940 else if (!strncmp(con
->uri
, "/admin/log/", 11))
2942 if (!strcmp(con
->uri
+ 11, "access_log") && AccessLog
[0] == '/')
2943 strlcpy(filename
, AccessLog
, len
);
2944 else if (!strcmp(con
->uri
+ 11, "error_log") && ErrorLog
[0] == '/')
2945 strlcpy(filename
, ErrorLog
, len
);
2946 else if (!strcmp(con
->uri
+ 11, "page_log") && PageLog
[0] == '/')
2947 strlcpy(filename
, PageLog
, len
);
2951 else if (con
->language
!= NULL
)
2952 snprintf(filename
, len
, "%s/%s%s", DocumentRoot
, con
->language
->language
,
2955 snprintf(filename
, len
, "%s%s", DocumentRoot
, con
->uri
);
2957 if ((ptr
= strchr(filename
, '?')) != NULL
)
2961 * Grab the status for this language; if there isn't a language-specific file
2962 * then fallback to the default one...
2965 if ((status
= stat(filename
, filestats
)) != 0 && con
->language
!= NULL
)
2968 * Drop the language prefix and try the current directory...
2971 if (strncmp(con
->uri
, "/ppd/", 5) &&
2972 strncmp(con
->uri
, "/admin/conf/", 12) &&
2973 strncmp(con
->uri
, "/admin/log/", 11))
2975 snprintf(filename
, len
, "%s%s", DocumentRoot
, con
->uri
);
2977 if ((ptr
= strchr(filename
, '?')) != NULL
)
2980 status
= stat(filename
, filestats
);
2985 * If we're found a directory, get the index.html file instead...
2988 if (!status
&& S_ISDIR(filestats
->st_mode
))
2990 if (filename
[strlen(filename
) - 1] != '/')
2991 strlcat(filename
, "/", len
);
2993 ptr
= filename
+ strlen(filename
);
2994 plen
= len
- (ptr
- filename
);
2996 strlcpy(ptr
, "index.html", plen
);
2997 status
= stat(filename
, filestats
);
3002 strlcpy(ptr
, "index.class", plen
);
3003 status
= stat(filename
, filestats
);
3005 #endif /* HAVE_JAVA */
3010 strlcpy(ptr
, "index.pl", plen
);
3011 status
= stat(filename
, filestats
);
3013 #endif /* HAVE_PERL */
3018 strlcpy(ptr
, "index.php", plen
);
3019 status
= stat(filename
, filestats
);
3021 #endif /* HAVE_PHP */
3026 strlcpy(ptr
, "index.pyc", plen
);
3027 status
= stat(filename
, filestats
);
3032 strlcpy(ptr
, "index.py", plen
);
3033 status
= stat(filename
, filestats
);
3035 #endif /* HAVE_PYTHON */
3038 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_file: %d filename=%s size=%d",
3039 con
->http
.fd
, filename
,
3040 status
? -1 : (int)filestats
->st_size
);
3043 con
->http
.data_remaining
= (int)filestats
->st_size
;
3053 * 'install_conf_file()' - Install a configuration file.
3056 static http_status_t
/* O - Status */
3057 install_conf_file(cupsd_client_t
*con
) /* I - Connection */
3059 cups_file_t
*in
, /* Input file */
3060 *out
; /* Output file */
3061 char buffer
[1024]; /* Copy buffer */
3062 int bytes
; /* Number of bytes */
3063 char conffile
[1024], /* Configuration filename */
3064 newfile
[1024], /* New config filename */
3065 oldfile
[1024]; /* Old config filename */
3066 struct stat confinfo
; /* Config file info */
3070 * First construct the filenames...
3073 snprintf(conffile
, sizeof(conffile
), "%s%s", ServerRoot
, con
->uri
+ 11);
3074 snprintf(newfile
, sizeof(newfile
), "%s%s.N", ServerRoot
, con
->uri
+ 11);
3075 snprintf(oldfile
, sizeof(oldfile
), "%s%s.O", ServerRoot
, con
->uri
+ 11);
3077 cupsdLogMessage(CUPSD_LOG_INFO
, "Installing config file \"%s\"...", conffile
);
3080 * Get the owner, group, and permissions of the configuration file.
3081 * If it doesn't exist, assign it to the User and Group in the
3082 * cupsd.conf file with mode 0640 permissions.
3085 if (stat(conffile
, &confinfo
))
3087 confinfo
.st_uid
= User
;
3088 confinfo
.st_gid
= Group
;
3089 confinfo
.st_mode
= ConfigFilePerm
;
3093 * Open the request file and new config file...
3096 if ((in
= cupsFileOpen(con
->filename
, "rb")) == NULL
)
3098 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to open request file \"%s\" - %s",
3099 con
->filename
, strerror(errno
));
3100 return (HTTP_SERVER_ERROR
);
3103 if ((out
= cupsFileOpen(newfile
, "wb")) == NULL
)
3106 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to open config file \"%s\" - %s",
3107 newfile
, strerror(errno
));
3108 return (HTTP_SERVER_ERROR
);
3111 fchmod(cupsFileNumber(out
), confinfo
.st_mode
);
3112 fchown(cupsFileNumber(out
), confinfo
.st_uid
, confinfo
.st_gid
);
3115 * Copy from the request to the new config file...
3118 while ((bytes
= cupsFileRead(in
, buffer
, sizeof(buffer
))) > 0)
3119 if (cupsFileWrite(out
, buffer
, bytes
) < bytes
)
3121 cupsdLogMessage(CUPSD_LOG_ERROR
,
3122 "Unable to copy to config file \"%s\" - %s",
3123 newfile
, strerror(errno
));
3129 return (HTTP_SERVER_ERROR
);
3133 * Close the files...
3137 if (cupsFileClose(out
))
3139 cupsdLogMessage(CUPSD_LOG_ERROR
,
3140 "Error file closing config file \"%s\" - %s",
3141 newfile
, strerror(errno
));
3145 return (HTTP_SERVER_ERROR
);
3149 * Remove the request file...
3152 unlink(con
->filename
);
3153 cupsdClearString(&con
->filename
);
3156 * Unlink the old backup, rename the current config file to the backup
3157 * filename, and rename the new config file to the config file name...
3160 if (unlink(oldfile
))
3161 if (errno
!= ENOENT
)
3163 cupsdLogMessage(CUPSD_LOG_ERROR
,
3164 "Unable to remove backup config file \"%s\" - %s",
3165 oldfile
, strerror(errno
));
3169 return (HTTP_SERVER_ERROR
);
3172 if (rename(conffile
, oldfile
))
3173 if (errno
!= ENOENT
)
3175 cupsdLogMessage(CUPSD_LOG_ERROR
,
3176 "Unable to rename old config file \"%s\" - %s",
3177 conffile
, strerror(errno
));
3181 return (HTTP_SERVER_ERROR
);
3184 if (rename(newfile
, conffile
))
3186 cupsdLogMessage(CUPSD_LOG_ERROR
,
3187 "Unable to rename new config file \"%s\" - %s",
3188 newfile
, strerror(errno
));
3190 rename(oldfile
, conffile
);
3193 return (HTTP_SERVER_ERROR
);
3197 * If the cupsd.conf file was updated, set the NeedReload flag...
3200 if (!strcmp(con
->uri
, "/admin/conf/cupsd.conf"))
3201 NeedReload
= RELOAD_CUPSD
;
3203 NeedReload
= RELOAD_ALL
;
3205 ReloadTime
= time(NULL
);
3208 * Return that the file was created successfully...
3211 return (HTTP_CREATED
);
3216 * 'is_path_absolute()' - Is a path absolute and free of relative elements (i.e. "..").
3219 static int /* O - 0 if relative, 1 if absolute */
3220 is_path_absolute(const char *path
) /* I - Input path */
3223 * Check for a leading slash...
3230 * Check for "/.." in the path...
3233 while ((path
= strstr(path
, "/..")) != NULL
)
3235 if (!path
[3] || path
[3] == '/')
3242 * If we haven't found any relative paths, return 1 indicating an
3251 * 'pipe_command()' - Pipe the output of a command to the remote client.
3254 static int /* O - Process ID */
3255 pipe_command(cupsd_client_t
*con
, /* I - Client connection */
3256 int infile
, /* I - Standard input for command */
3257 int *outfile
, /* O - Standard output for command */
3258 char *command
, /* I - Command to run */
3259 char *options
, /* I - Options for command */
3260 int root
) /* I - Run as root? */
3262 int i
; /* Looping var */
3263 int pid
; /* Process ID */
3264 char *commptr
, /* Command string pointer */
3265 commch
; /* Command string character */
3266 char *uriptr
; /* URI string pointer */
3267 int fds
[2]; /* Pipe FDs */
3268 int argc
; /* Number of arguments */
3269 int envc
; /* Number of environment variables */
3270 char argbuf
[10240], /* Argument buffer */
3271 *argv
[100], /* Argument strings */
3272 *envp
[MAX_ENV
+ 17]; /* Environment variables */
3273 char content_length
[1024], /* CONTENT_LENGTH environment variable */
3274 content_type
[1024], /* CONTENT_TYPE environment variable */
3275 http_cookie
[32768], /* HTTP_COOKIE environment variable */
3276 http_user_agent
[1024], /* HTTP_USER_AGENT environment variable */
3277 lang
[1024], /* LANG environment variable */
3278 path_info
[1024], /* PATH_INFO environment variable */
3279 *query_string
, /* QUERY_STRING env variable */
3280 remote_addr
[1024], /* REMOTE_ADDR environment variable */
3281 remote_host
[1024], /* REMOTE_HOST environment variable */
3282 remote_user
[1024], /* REMOTE_USER environment variable */
3283 script_name
[1024], /* SCRIPT_NAME environment variable */
3284 server_name
[1024], /* SERVER_NAME environment variable */
3285 server_port
[1024]; /* SERVER_PORT environment variable */
3289 * Parse a copy of the options string, which is of the form:
3291 * argument+argument+argument
3292 * ?argument+argument+argument
3293 * param=value¶m=value
3294 * ?param=value¶m=value
3295 * /name?argument+argument+argument
3296 * /name?param=value¶m=value
3298 * If the string contains an "=" character after the initial name,
3299 * then we treat it as a HTTP GET form request and make a copy of
3300 * the remaining string for the environment variable.
3302 * The string is always parsed out as command-line arguments, to
3303 * be consistent with Apache...
3306 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3307 "pipe_command: command=\"%s\", options=\"%s\"",
3311 query_string
= NULL
;
3314 strlcpy(argbuf
, options
, sizeof(argbuf
));
3318 if (argbuf
[0] == '/')
3321 * Found some trailing path information, set PATH_INFO...
3324 if ((commptr
= strchr(argbuf
, '?')) == NULL
)
3325 commptr
= argbuf
+ strlen(argbuf
);
3329 snprintf(path_info
, sizeof(path_info
), "PATH_INFO=%s", argbuf
);
3335 path_info
[0] = '\0';
3338 if (*commptr
== '?' && con
->operation
== HTTP_GET
)
3341 cupsdSetStringf(&query_string
, "QUERY_STRING=%s", commptr
);
3348 argv
[argc
++] = commptr
;
3350 for (; *commptr
&& argc
< 99; commptr
++)
3353 * Break arguments whenever we see a + or space...
3356 if (*commptr
== ' ' || *commptr
== '+')
3358 while (*commptr
== ' ' || *commptr
== '+')
3362 * If we don't have a blank string, save it as another argument...
3367 argv
[argc
] = commptr
;
3373 else if (*commptr
== '%' && isxdigit(commptr
[1] & 255) &&
3374 isxdigit(commptr
[2] & 255))
3377 * Convert the %xx notation to the individual character.
3380 if (commptr
[1] >= '0' && commptr
[1] <= '9')
3381 *commptr
= (commptr
[1] - '0') << 4;
3383 *commptr
= (tolower(commptr
[1]) - 'a' + 10) << 4;
3385 if (commptr
[2] >= '0' && commptr
[2] <= '9')
3386 *commptr
|= commptr
[2] - '0';
3388 *commptr
|= tolower(commptr
[2]) - 'a' + 10;
3390 _cups_strcpy(commptr
+ 1, commptr
+ 3);
3393 * Check for a %00 and break if that is the case...
3405 * Setup the environment variables as needed...
3409 snprintf(lang
, sizeof(lang
), "LANG=%s.UTF-8", con
->language
->language
);
3411 strcpy(lang
, "LANG=C");
3413 strcpy(remote_addr
, "REMOTE_ADDR=");
3414 httpAddrString(con
->http
.hostaddr
, remote_addr
+ 12,
3415 sizeof(remote_addr
) - 12);
3417 snprintf(remote_host
, sizeof(remote_host
), "REMOTE_HOST=%s",
3418 con
->http
.hostname
);
3420 snprintf(script_name
, sizeof(script_name
), "SCRIPT_NAME=%s", con
->uri
);
3421 if ((uriptr
= strchr(script_name
, '?')) != NULL
)
3424 sprintf(server_port
, "SERVER_PORT=%d", con
->serverport
);
3426 snprintf(server_name
, sizeof(server_name
), "SERVER_NAME=%s",
3429 envc
= cupsdLoadEnv(envp
, (int)(sizeof(envp
) / sizeof(envp
[0])));
3431 envp
[envc
++] = lang
;
3432 envp
[envc
++] = "REDIRECT_STATUS=1";
3433 envp
[envc
++] = server_name
;
3434 envp
[envc
++] = server_port
;
3435 envp
[envc
++] = remote_addr
;
3436 envp
[envc
++] = remote_host
;
3437 envp
[envc
++] = script_name
;
3440 envp
[envc
++] = path_info
;
3442 if (con
->username
[0])
3444 snprintf(remote_user
, sizeof(remote_user
), "REMOTE_USER=%s", con
->username
);
3446 envp
[envc
++] = remote_user
;
3449 if (con
->http
.version
== HTTP_1_1
)
3450 envp
[envc
++] = "SERVER_PROTOCOL=HTTP/1.1";
3451 else if (con
->http
.version
== HTTP_1_0
)
3452 envp
[envc
++] = "SERVER_PROTOCOL=HTTP/1.0";
3454 envp
[envc
++] = "SERVER_PROTOCOL=HTTP/0.9";
3456 if (con
->http
.cookie
)
3458 snprintf(http_cookie
, sizeof(http_cookie
), "HTTP_COOKIE=%s",
3460 envp
[envc
++] = http_cookie
;
3463 if (con
->http
.fields
[HTTP_FIELD_USER_AGENT
][0])
3465 snprintf(http_user_agent
, sizeof(http_user_agent
), "HTTP_USER_AGENT=%s",
3466 con
->http
.fields
[HTTP_FIELD_USER_AGENT
]);
3467 envp
[envc
++] = http_user_agent
;
3470 if (con
->operation
== HTTP_GET
)
3472 for (i
= 0; i
< argc
; i
++)
3473 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "argv[%d] = \"%s\"", i
, argv
[i
]);
3475 envp
[envc
++] = "REQUEST_METHOD=GET";
3480 * Add GET form variables after ?...
3483 envp
[envc
++] = query_string
;
3488 sprintf(content_length
, "CONTENT_LENGTH=" CUPS_LLFMT
, con
->bytes
);
3489 snprintf(content_type
, sizeof(content_type
), "CONTENT_TYPE=%s",
3490 con
->http
.fields
[HTTP_FIELD_CONTENT_TYPE
]);
3492 envp
[envc
++] = "REQUEST_METHOD=POST";
3493 envp
[envc
++] = content_length
;
3494 envp
[envc
++] = content_type
;
3498 * Tell the CGI if we are using encryption...
3501 if (con
->http
.encryption
== HTTP_ENCRYPT_ALWAYS
)
3502 envp
[envc
++] = "HTTPS=ON";
3505 * Terminate the environment array...
3510 if (LogLevel
== CUPSD_LOG_DEBUG2
)
3512 for (i
= 0; i
< argc
; i
++)
3513 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3514 "pipe_command: argv[%d] = \"%s\"", i
, argv
[i
]);
3515 for (i
= 0; i
< envc
; i
++)
3516 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3517 "pipe_command: envp[%d] = \"%s\"", i
, envp
[i
]);
3521 * Create a pipe for the output...
3524 if (cupsdOpenPipe(fds
))
3526 cupsdClearString(&query_string
);
3528 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to create pipes for CGI %s - %s",
3529 argv
[0], strerror(errno
));
3534 * Then execute the command...
3537 if (cupsdStartProcess(command
, argv
, envp
, infile
, fds
[1], CGIPipes
[1],
3538 -1, root
, &pid
) < 0)
3541 * Error - can't fork!
3544 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to fork for CGI %s - %s", argv
[0],
3547 cupsdClosePipe(fds
);
3553 * Fork successful - return the PID...
3556 if (con
->username
[0])
3557 cupsdAddCert(pid
, con
->username
);
3559 cupsdLogMessage(CUPSD_LOG_DEBUG
, "CGI %s started - PID = %d", command
, pid
);
3565 cupsdClearString(&query_string
);
3572 * End of "$Id: client.c 5200 2006-02-28 00:10:32Z mike $".