2 * "$Id: client.c 5083 2006-02-06 02:57:43Z 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 SSLClose((SSLContextRef
)con
->http
.tls
);
519 SSLDisposeContext((SSLContextRef
)con
->http
.tls
);
520 # endif /* HAVE_LIBSSL */
522 con
->http
.tls
= NULL
;
524 #endif /* HAVE_SSL */
526 if (con
->pipe_pid
!= 0)
529 * Stop any CGI process...
532 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
533 "cupsdCloseClient: %d Killing process ID %d...",
534 con
->http
.fd
, con
->pipe_pid
);
535 cupsdEndProcess(con
->pipe_pid
, 1);
541 if (FD_ISSET(con
->file
, InputSet
))
543 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
544 "cupsdCloseClient: %d Removing fd %d from InputSet...",
545 con
->http
.fd
, con
->file
);
546 FD_CLR(con
->file
, InputSet
);
549 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
550 "cupsdCloseClient: %d Closing data file %d.",
551 con
->http
.fd
, con
->file
);
558 * Close the socket and clear the file from the input set for select()...
561 if (con
->http
.fd
> 0)
566 * Only do a partial close so that the encrypted client gets everything.
569 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
570 "cupsdCloseClient: Removing fd %d from OutputSet...",
572 shutdown(con
->http
.fd
, 0);
573 FD_CLR(con
->http
.fd
, OutputSet
);
578 * Shut the socket down fully...
581 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
582 "cupsdCloseClient: Removing fd %d from InputSet and OutputSet...",
585 FD_CLR(con
->http
.fd
, InputSet
);
586 FD_CLR(con
->http
.fd
, OutputSet
);
597 if (con
->http
.input_set
)
598 free(con
->http
.input_set
);
600 httpClearCookie(HTTP(con
));
602 cupsdClearString(&con
->filename
);
603 cupsdClearString(&con
->command
);
604 cupsdClearString(&con
->options
);
608 ippDelete(con
->request
);
614 ippDelete(con
->response
);
615 con
->response
= NULL
;
620 cupsLangFree(con
->language
);
621 con
->language
= NULL
;
625 * Re-enable new client connections if we are going back under the
629 if (cupsArrayCount(Clients
) == MaxClients
)
630 cupsdResumeListening();
633 * Compact the list of clients as necessary...
636 cupsArrayRemove(Clients
, con
);
646 * 'cupsdEncryptClient()' - Enable encryption for the client...
649 int /* O - 1 on success, 0 on error */
650 cupsdEncryptClient(cupsd_client_t
*con
) /* I - Client to encrypt */
653 SSL_CTX
*context
; /* Context for encryption */
654 SSL
*conn
; /* Connection for encryption */
655 unsigned long error
; /* Error code */
659 * Create the SSL context and accept the connection...
662 context
= SSL_CTX_new(SSLv23_server_method());
664 SSL_CTX_set_options(context
, SSL_OP_NO_SSLv2
); /* Only use SSLv3 or TLS */
665 SSL_CTX_use_PrivateKey_file(context
, ServerKey
, SSL_FILETYPE_PEM
);
666 SSL_CTX_use_certificate_file(context
, ServerCertificate
, SSL_FILETYPE_PEM
);
668 conn
= SSL_new(context
);
670 SSL_set_fd(conn
, con
->http
.fd
);
671 if (SSL_accept(conn
) != 1)
673 cupsdLogMessage(CUPSD_LOG_ERROR
,
674 "cupsdEncryptClient: Unable to encrypt connection from %s!",
677 while ((error
= ERR_get_error()) != 0)
678 cupsdLogMessage(CUPSD_LOG_ERROR
, "cupsdEncryptClient: %s",
679 ERR_error_string(error
, NULL
));
681 SSL_CTX_free(context
);
686 cupsdLogMessage(CUPSD_LOG_DEBUG
,
687 "cupsdEncryptClient: %d Connection from %s now encrypted.",
688 con
->http
.fd
, con
->http
.hostname
);
690 con
->http
.tls
= conn
;
693 #elif defined(HAVE_GNUTLS)
694 http_tls_t
*conn
; /* TLS session object */
695 int error
; /* Error code */
696 gnutls_certificate_server_credentials
*credentials
;
697 /* TLS credentials */
700 * Create the SSL object and perform the SSL handshake...
703 conn
= (http_tls_t
*)malloc(sizeof(gnutls_session
));
708 credentials
= (gnutls_certificate_server_credentials
*)
709 malloc(sizeof(gnutls_certificate_server_credentials
));
710 if (credentials
== NULL
)
712 cupsdLogMessage(CUPSD_LOG_ERROR
,
713 "cupsdEncryptClient: Unable to encrypt connection from %s!",
715 cupsdLogMessage(CUPSD_LOG_ERROR
, "cupsdEncryptClient: %s", strerror(errno
));
721 gnutls_certificate_allocate_credentials(credentials
);
722 gnutls_certificate_set_x509_key_file(*credentials
, ServerCertificate
,
723 ServerKey
, GNUTLS_X509_FMT_PEM
);
725 gnutls_init(&(conn
->session
), GNUTLS_SERVER
);
726 gnutls_set_default_priority(conn
->session
);
727 gnutls_credentials_set(conn
->session
, GNUTLS_CRD_CERTIFICATE
, *credentials
);
728 gnutls_transport_set_ptr(conn
->session
, con
->http
.fd
);
730 error
= gnutls_handshake(conn
->session
);
732 if (error
!= GNUTLS_E_SUCCESS
)
734 cupsdLogMessage(CUPSD_LOG_ERROR
,
735 "cupsdEncryptClient: Unable to encrypt connection from %s!",
737 cupsdLogMessage(CUPSD_LOG_ERROR
, "cupsdEncryptClient: %s",
738 gnutls_strerror(error
));
740 gnutls_deinit(conn
->session
);
741 gnutls_certificate_free_credentials(*credentials
);
747 cupsdLogMessage(CUPSD_LOG_DEBUG
,
748 "cupsdEncryptClient: %d Connection from %s now encrypted.",
749 con
->http
.fd
, con
->http
.hostname
);
751 conn
->credentials
= credentials
;
752 con
->http
.tls
= conn
;
755 #elif defined(HAVE_CDSASSL)
756 OSStatus error
; /* Error info */
757 SSLContextRef conn
; /* New connection */
758 int allowExpired
; /* Allow expired certificates? */
759 int allowAnyRoot
; /* Allow any root certificate? */
763 error
= SSLNewContext(true, &conn
);
767 if (!ServerCertificatesArray
)
768 ServerCertificatesArray
= get_cdsa_server_certs();
770 if (!ServerCertificatesArray
)
772 cupsdLogMessage(CUPSD_LOG_ERROR
,
773 "EncryptClient: Could not find signing key in keychain "
774 "\"%s\"", ServerCertificate
);
775 error
= errSSLBadCert
; /* errSSLBadConfiguration is a better choice, but not available on 10.2.x */
779 error
= SSLSetIOFuncs(conn
, _httpReadCDSA
, _httpWriteCDSA
);
782 error
= SSLSetProtocolVersion(conn
, kSSLProtocol3
);
785 error
= SSLSetConnection(conn
, (SSLConnectionRef
)con
->http
.fd
);
788 error
= SSLSetPeerDomainName(conn
, ServerName
, strlen(ServerName
) + 1);
790 /* have to do these options before setting server certs */
791 if (!error
&& allowExpired
)
792 error
= SSLSetAllowsExpiredCerts(conn
, true);
794 if (!error
&& allowAnyRoot
)
795 error
= SSLSetAllowsAnyRoot(conn
, true);
797 if (!error
&& ServerCertificatesArray
)
799 error
= SSLSetCertificate(conn
, ServerCertificatesArray
);
802 * Perform SSL/TLS handshake
809 error
= SSLHandshake(conn
);
811 while (error
== errSSLWouldBlock
);
817 cupsdLogMessage(CUPSD_LOG_ERROR
,
818 "cupsdEncryptClient: Unable to encrypt connection from %s!",
821 cupsdLogMessage(CUPSD_LOG_ERROR
,
822 "cupsdEncryptClient: CDSA error code is %d", (int)error
);
824 con
->http
.error
= error
;
825 con
->http
.status
= HTTP_ERROR
;
828 SSLDisposeContext(conn
);
833 cupsdLogMessage(CUPSD_LOG_DEBUG
,
834 "cupsdEncryptClient: %d Connection from %s now encrypted.",
835 con
->http
.fd
, con
->http
.hostname
);
837 con
->http
.tls
= conn
;
842 #endif /* HAVE_LIBSSL */
847 * 'cupsdIsCGI()' - Is the resource a CGI script/program?
850 int /* O - 1 = CGI, 0 = file */
851 cupsdIsCGI(cupsd_client_t
*con
, /* I - Client connection */
852 const char *filename
, /* I - Real filename */
853 struct stat
*filestats
, /* I - File information */
854 mime_type_t
*type
) /* I - MIME type */
856 const char *options
; /* Options on URL */
859 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
860 "cupsdIsCGI(con=%p, filename=\"%s\", filestats=%p, type=%s/%s)\n",
861 con
, filename
, filestats
, type
? type
->super
: "unknown",
862 type
? type
->type
: "unknown");
865 * Get the options, if any...
868 if ((options
= strchr(con
->uri
, '?')) != NULL
)
872 * Check for known types...
875 if (!type
|| strcasecmp(type
->super
, "application"))
877 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdIsCGI: Returning 0...");
881 if (!strcasecmp(type
->type
, "x-httpd-cgi") &&
882 (filestats
->st_mode
& 0111))
885 * "application/x-httpd-cgi" is a CGI script.
888 cupsdSetString(&con
->command
, filename
);
890 filename
= strrchr(filename
, '/') + 1; /* Filename always absolute */
893 cupsdSetStringf(&con
->options
, "%s %s", filename
, options
);
895 cupsdSetStringf(&con
->options
, "%s", filename
);
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
, "java %s %s", filename
, options
);
915 cupsdSetStringf(&con
->options
, "java %s", 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
, "perl %s %s", filename
, options
);
936 cupsdSetStringf(&con
->options
, "perl %s", 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
, "php %s %s", filename
, options
);
957 cupsdSetStringf(&con
->options
, "php %s", 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
, "python %s %s", filename
, options
);
978 cupsdSetStringf(&con
->options
, "python %s", 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 (con
->http
.expect
)
1351 /**** TODO: send expected header ****/
1354 if (!cupsdSendHeader(con
, HTTP_OK
, NULL
))
1355 return (cupsdCloseClient(con
));
1357 httpPrintf(HTTP(con
), "Allow: GET, HEAD, OPTIONS, POST, PUT\r\n");
1358 httpPrintf(HTTP(con
), "Content-Length: 0\r\n");
1359 httpPrintf(HTTP(con
), "\r\n");
1361 else if (!is_path_absolute(con
->uri
))
1364 * Protect against malicious users!
1367 if (!cupsdSendError(con
, HTTP_FORBIDDEN
))
1368 return (cupsdCloseClient(con
));
1372 if (!strcasecmp(con
->http
.fields
[HTTP_FIELD_CONNECTION
], "Upgrade") &&
1373 con
->http
.tls
== NULL
)
1377 * Do encryption stuff...
1380 if (!cupsdSendHeader(con
, HTTP_SWITCHING_PROTOCOLS
, NULL
))
1381 return (cupsdCloseClient(con
));
1383 httpPrintf(HTTP(con
), "Connection: Upgrade\r\n");
1384 httpPrintf(HTTP(con
), "Upgrade: TLS/1.0,HTTP/1.1\r\n");
1385 httpPrintf(HTTP(con
), "Content-Length: 0\r\n");
1386 httpPrintf(HTTP(con
), "\r\n");
1388 cupsdEncryptClient(con
);
1390 if (!cupsdSendError(con
, HTTP_NOT_IMPLEMENTED
))
1391 return (cupsdCloseClient(con
));
1392 #endif /* HAVE_SSL */
1395 if ((status
= cupsdIsAuthorized(con
, NULL
)) != HTTP_OK
)
1397 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1398 "cupsdReadClient: Unauthorized request for %s...\n",
1400 cupsdSendError(con
, status
);
1401 return (cupsdCloseClient(con
));
1404 if (con
->http
.expect
)
1406 /**** TODO: send expected header ****/
1409 switch (con
->http
.state
)
1411 case HTTP_GET_SEND
:
1412 if (!strncmp(con
->uri
, "/printers/", 10) &&
1413 !strcmp(con
->uri
+ strlen(con
->uri
) - 4, ".ppd"))
1416 * Send PPD file - get the real printer name since printer
1417 * names are not case sensitive but filenames can be...
1420 con
->uri
[strlen(con
->uri
) - 4] = '\0'; /* Drop ".ppd" */
1422 if ((p
= cupsdFindPrinter(con
->uri
+ 10)) != NULL
)
1423 snprintf(con
->uri
, sizeof(con
->uri
), "/ppd/%s.ppd", p
->name
);
1426 if (!cupsdSendError(con
, HTTP_NOT_FOUND
))
1427 return (cupsdCloseClient(con
));
1433 if ((!strncmp(con
->uri
, "/admin", 6) &&
1434 strncmp(con
->uri
, "/admin/conf/", 12) &&
1435 strncmp(con
->uri
, "/admin/log/", 11)) ||
1436 !strncmp(con
->uri
, "/printers", 9) ||
1437 !strncmp(con
->uri
, "/classes", 8) ||
1438 !strncmp(con
->uri
, "/help", 5) ||
1439 !strncmp(con
->uri
, "/jobs", 5))
1442 * Send CGI output...
1445 if (!strncmp(con
->uri
, "/admin", 6))
1447 cupsdSetStringf(&con
->command
, "%s/cgi-bin/admin.cgi",
1450 if ((ptr
= strchr(con
->uri
+ 6, '?')) != NULL
)
1451 cupsdSetStringf(&con
->options
, "admin%s", ptr
);
1453 cupsdSetString(&con
->options
, "admin");
1455 else if (!strncmp(con
->uri
, "/printers", 9))
1457 cupsdSetStringf(&con
->command
, "%s/cgi-bin/printers.cgi",
1459 cupsdSetString(&con
->options
, con
->uri
+ 9);
1461 else if (!strncmp(con
->uri
, "/classes", 8))
1463 cupsdSetStringf(&con
->command
, "%s/cgi-bin/classes.cgi",
1465 cupsdSetString(&con
->options
, con
->uri
+ 8);
1467 else if (!strncmp(con
->uri
, "/jobs", 5))
1469 cupsdSetStringf(&con
->command
, "%s/cgi-bin/jobs.cgi",
1471 cupsdSetString(&con
->options
, con
->uri
+ 5);
1475 cupsdSetStringf(&con
->command
, "%s/cgi-bin/help.cgi",
1477 cupsdSetString(&con
->options
, con
->uri
+ 5);
1480 if (con
->options
[0] == '/')
1481 _cups_strcpy(con
->options
, con
->options
+ 1);
1483 if (!cupsdSendCommand(con
, con
->command
, con
->options
, 0))
1485 if (!cupsdSendError(con
, HTTP_NOT_FOUND
))
1486 return (cupsdCloseClient(con
));
1489 cupsdLogRequest(con
, HTTP_OK
);
1491 if (con
->http
.version
<= HTTP_1_0
)
1492 con
->http
.keep_alive
= HTTP_KEEPALIVE_OFF
;
1494 else if ((!strncmp(con
->uri
, "/admin/conf/", 12) &&
1495 (strchr(con
->uri
+ 12, '/') ||
1496 strlen(con
->uri
) == 12)) ||
1497 (!strncmp(con
->uri
, "/admin/log/", 11) &&
1498 (strchr(con
->uri
+ 11, '/') ||
1499 strlen(con
->uri
) == 11)))
1502 * GET can only be done to configuration files under
1506 if (!cupsdSendError(con
, HTTP_FORBIDDEN
))
1507 return (cupsdCloseClient(con
));
1517 if ((filename
= get_file(con
, &filestats
, buf
,
1518 sizeof(buf
))) == NULL
)
1520 if (!cupsdSendError(con
, HTTP_NOT_FOUND
))
1521 return (cupsdCloseClient(con
));
1526 type
= mimeFileType(MimeDatabase
, filename
, NULL
, NULL
);
1528 if (cupsdIsCGI(con
, filename
, &filestats
, type
))
1531 * Note: con->command and con->options were set by
1535 if (!cupsdSendCommand(con
, con
->command
, con
->options
, 0))
1537 if (!cupsdSendError(con
, HTTP_NOT_FOUND
))
1538 return (cupsdCloseClient(con
));
1541 cupsdLogRequest(con
, HTTP_OK
);
1543 if (con
->http
.version
<= HTTP_1_0
)
1544 con
->http
.keep_alive
= HTTP_KEEPALIVE_OFF
;
1548 if (!check_if_modified(con
, &filestats
))
1550 if (!cupsdSendError(con
, HTTP_NOT_MODIFIED
))
1551 return (cupsdCloseClient(con
));
1556 strcpy(line
, "text/plain");
1558 snprintf(line
, sizeof(line
), "%s/%s", type
->super
, type
->type
);
1560 if (!cupsdSendFile(con
, HTTP_OK
, filename
, line
, &filestats
))
1561 return (cupsdCloseClient(con
));
1566 case HTTP_POST_RECV
:
1568 * See if the POST request includes a Content-Length field, and if
1569 * so check the length against any limits that are set...
1572 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "POST %s", con
->uri
);
1573 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "CONTENT_TYPE = %s",
1574 con
->http
.fields
[HTTP_FIELD_CONTENT_TYPE
]);
1576 if (con
->http
.fields
[HTTP_FIELD_CONTENT_LENGTH
][0] &&
1577 MaxRequestSize
> 0 &&
1578 con
->http
.data_remaining
> MaxRequestSize
)
1581 * Request too large...
1584 if (!cupsdSendError(con
, HTTP_REQUEST_TOO_LARGE
))
1585 return (cupsdCloseClient(con
));
1589 else if (con
->http
.data_remaining
< 0)
1592 * Negative content lengths are invalid!
1595 if (!cupsdSendError(con
, HTTP_BAD_REQUEST
))
1596 return (cupsdCloseClient(con
));
1602 * See what kind of POST request this is; for IPP requests the
1603 * content-type field will be "application/ipp"...
1606 if (!strcmp(con
->http
.fields
[HTTP_FIELD_CONTENT_TYPE
],
1608 con
->request
= ippNew();
1609 else if ((!strncmp(con
->uri
, "/admin", 6) &&
1610 strncmp(con
->uri
, "/admin/conf/", 12) &&
1611 strncmp(con
->uri
, "/admin/log/", 11)) ||
1612 !strncmp(con
->uri
, "/printers", 9) ||
1613 !strncmp(con
->uri
, "/classes", 8) ||
1614 !strncmp(con
->uri
, "/help", 5) ||
1615 !strncmp(con
->uri
, "/jobs", 5))
1621 if (!strncmp(con
->uri
, "/admin", 6))
1623 cupsdSetStringf(&con
->command
, "%s/cgi-bin/admin.cgi",
1626 if ((ptr
= strchr(con
->uri
+ 6, '?')) != NULL
)
1627 cupsdSetStringf(&con
->options
, "admin%s", ptr
);
1629 cupsdSetString(&con
->options
, "admin");
1631 else if (!strncmp(con
->uri
, "/printers", 9))
1633 cupsdSetStringf(&con
->command
, "%s/cgi-bin/printers.cgi",
1635 cupsdSetString(&con
->options
, con
->uri
+ 9);
1637 else if (!strncmp(con
->uri
, "/classes", 8))
1639 cupsdSetStringf(&con
->command
, "%s/cgi-bin/classes.cgi",
1641 cupsdSetString(&con
->options
, con
->uri
+ 8);
1643 else if (!strncmp(con
->uri
, "/jobs", 5))
1645 cupsdSetStringf(&con
->command
, "%s/cgi-bin/jobs.cgi",
1647 cupsdSetString(&con
->options
, con
->uri
+ 5);
1651 cupsdSetStringf(&con
->command
, "%s/cgi-bin/help.cgi",
1653 cupsdSetString(&con
->options
, con
->uri
+ 5);
1656 if (con
->options
[0] == '/')
1657 _cups_strcpy(con
->options
, con
->options
+ 1);
1659 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1660 "cupsdReadClient: %d command=\"%s\", options = \"%s\"",
1661 con
->http
.fd
, con
->command
, con
->options
);
1663 if (con
->http
.version
<= HTTP_1_0
)
1664 con
->http
.keep_alive
= HTTP_KEEPALIVE_OFF
;
1672 if ((filename
= get_file(con
, &filestats
, buf
,
1673 sizeof(buf
))) == NULL
)
1675 if (!cupsdSendError(con
, HTTP_NOT_FOUND
))
1676 return (cupsdCloseClient(con
));
1681 type
= mimeFileType(MimeDatabase
, filename
, NULL
, NULL
);
1683 if (!cupsdIsCGI(con
, filename
, &filestats
, type
))
1686 * Only POST to CGI's...
1689 if (!cupsdSendError(con
, HTTP_UNAUTHORIZED
))
1690 return (cupsdCloseClient(con
));
1695 case HTTP_PUT_RECV
:
1697 * Validate the resource name...
1700 if (strncmp(con
->uri
, "/admin/conf/", 12) ||
1701 strchr(con
->uri
+ 12, '/') ||
1702 strlen(con
->uri
) == 12)
1705 * PUT can only be done to configuration files under
1709 if (!cupsdSendError(con
, HTTP_FORBIDDEN
))
1710 return (cupsdCloseClient(con
));
1716 * See if the PUT request includes a Content-Length field, and if
1717 * so check the length against any limits that are set...
1720 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "PUT %s", con
->uri
);
1721 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "CONTENT_TYPE = %s",
1722 con
->http
.fields
[HTTP_FIELD_CONTENT_TYPE
]);
1724 if (con
->http
.fields
[HTTP_FIELD_CONTENT_LENGTH
][0] &&
1725 MaxRequestSize
> 0 &&
1726 con
->http
.data_remaining
> MaxRequestSize
)
1729 * Request too large...
1732 if (!cupsdSendError(con
, HTTP_REQUEST_TOO_LARGE
))
1733 return (cupsdCloseClient(con
));
1737 else if (con
->http
.data_remaining
< 0)
1740 * Negative content lengths are invalid!
1743 if (!cupsdSendError(con
, HTTP_BAD_REQUEST
))
1744 return (cupsdCloseClient(con
));
1750 * Open a temporary file to hold the request...
1753 cupsdSetStringf(&con
->filename
, "%s/%08x", RequestRoot
,
1755 con
->file
= open(con
->filename
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0640);
1757 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1758 "cupsdReadClient: %d REQUEST %s=%d", con
->http
.fd
,
1759 con
->filename
, con
->file
);
1763 if (!cupsdSendError(con
, HTTP_REQUEST_TOO_LARGE
))
1764 return (cupsdCloseClient(con
));
1767 fchmod(con
->file
, 0640);
1768 fchown(con
->file
, RunUser
, Group
);
1769 fcntl(con
->file
, F_SETFD
, fcntl(con
->file
, F_GETFD
) | FD_CLOEXEC
);
1774 cupsdSendError(con
, HTTP_NOT_IMPLEMENTED
);
1775 return (cupsdCloseClient(con
));
1778 if (!strncmp(con
->uri
, "/printers/", 10) &&
1779 !strcmp(con
->uri
+ strlen(con
->uri
) - 4, ".ppd"))
1782 * Send PPD file - get the real printer name since printer
1783 * names are not case sensitive but filenames can be...
1786 con
->uri
[strlen(con
->uri
) - 4] = '\0'; /* Drop ".ppd" */
1788 if ((p
= cupsdFindPrinter(con
->uri
+ 10)) != NULL
)
1789 snprintf(con
->uri
, sizeof(con
->uri
), "/ppd/%s.ppd", p
->name
);
1792 if (!cupsdSendError(con
, HTTP_NOT_FOUND
))
1793 return (cupsdCloseClient(con
));
1799 if ((!strncmp(con
->uri
, "/admin", 6) &&
1800 strncmp(con
->uri
, "/admin/conf/", 12) &&
1801 strncmp(con
->uri
, "/admin/log/", 11)) ||
1802 !strncmp(con
->uri
, "/printers", 9) ||
1803 !strncmp(con
->uri
, "/classes", 8) ||
1804 !strncmp(con
->uri
, "/help", 5) ||
1805 !strncmp(con
->uri
, "/jobs", 5))
1811 if (!cupsdSendHeader(con
, HTTP_OK
, "text/html"))
1812 return (cupsdCloseClient(con
));
1814 if (httpPrintf(HTTP(con
), "\r\n") < 0)
1815 return (cupsdCloseClient(con
));
1817 cupsdLogRequest(con
, HTTP_OK
);
1819 else if ((!strncmp(con
->uri
, "/admin/conf/", 12) &&
1820 (strchr(con
->uri
+ 12, '/') ||
1821 strlen(con
->uri
) == 12)) ||
1822 (!strncmp(con
->uri
, "/admin/log/", 11) &&
1823 (strchr(con
->uri
+ 11, '/') ||
1824 strlen(con
->uri
) == 11)))
1827 * HEAD can only be done to configuration files under
1831 if (!cupsdSendError(con
, HTTP_FORBIDDEN
))
1832 return (cupsdCloseClient(con
));
1836 else if ((filename
= get_file(con
, &filestats
, buf
,
1837 sizeof(buf
))) == NULL
)
1839 if (!cupsdSendHeader(con
, HTTP_NOT_FOUND
, "text/html"))
1840 return (cupsdCloseClient(con
));
1842 cupsdLogRequest(con
, HTTP_NOT_FOUND
);
1844 else if (!check_if_modified(con
, &filestats
))
1846 if (!cupsdSendError(con
, HTTP_NOT_MODIFIED
))
1847 return (cupsdCloseClient(con
));
1849 cupsdLogRequest(con
, HTTP_NOT_MODIFIED
);
1857 type
= mimeFileType(MimeDatabase
, filename
, NULL
, NULL
);
1859 strcpy(line
, "text/plain");
1861 snprintf(line
, sizeof(line
), "%s/%s", type
->super
, type
->type
);
1863 if (!cupsdSendHeader(con
, HTTP_OK
, line
))
1864 return (cupsdCloseClient(con
));
1866 if (httpPrintf(HTTP(con
), "Last-Modified: %s\r\n",
1867 httpGetDateString(filestats
.st_mtime
)) < 0)
1868 return (cupsdCloseClient(con
));
1870 if (httpPrintf(HTTP(con
), "Content-Length: %lu\r\n",
1871 (unsigned long)filestats
.st_size
) < 0)
1872 return (cupsdCloseClient(con
));
1874 cupsdLogRequest(con
, HTTP_OK
);
1877 if (httpPrintf(HTTP(con
), "\r\n") < 0)
1878 return (cupsdCloseClient(con
));
1880 con
->http
.state
= HTTP_WAITING
;
1884 break; /* Anti-compiler-warning-code */
1890 * Handle any incoming data...
1893 switch (con
->http
.state
)
1895 case HTTP_PUT_RECV
:
1896 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1897 "cupsdReadClient: %d con->data_encoding=HTTP_ENCODE_%s, "
1898 "con->data_remaining=" CUPS_LLFMT
", con->file=%d",
1900 con
->http
.data_encoding
== HTTP_ENCODE_CHUNKED
?
1901 "CHUNKED" : "LENGTH",
1902 CUPS_LLCAST con
->http
.data_remaining
, con
->file
);
1904 if ((bytes
= httpRead2(HTTP(con
), line
, sizeof(line
))) < 0)
1905 return (cupsdCloseClient(con
));
1908 con
->bytes
+= bytes
;
1910 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1911 "cupsdReadClient: %d writing %d bytes to %d",
1912 con
->http
.fd
, bytes
, con
->file
);
1914 if (write(con
->file
, line
, bytes
) < bytes
)
1916 cupsdLogMessage(CUPSD_LOG_ERROR
,
1917 "cupsdReadClient: Unable to write %d bytes to %s: %s",
1918 bytes
, con
->filename
, strerror(errno
));
1920 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1921 "cupsdReadClient: Closing data file %d...",
1926 unlink(con
->filename
);
1927 cupsdClearString(&con
->filename
);
1929 if (!cupsdSendError(con
, HTTP_REQUEST_TOO_LARGE
))
1930 return (cupsdCloseClient(con
));
1934 if (con
->http
.state
== HTTP_WAITING
)
1937 * End of file, see how big it is...
1940 fstat(con
->file
, &filestats
);
1942 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1943 "cupsdReadClient: %d Closing data file %d, size="
1945 con
->http
.fd
, con
->file
,
1946 CUPS_LLCAST filestats
.st_size
);
1951 if (filestats
.st_size
> MaxRequestSize
&&
1955 * Request is too big; remove it and send an error...
1958 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1959 "cupsdReadClient: %d Removing temp file %s",
1960 con
->http
.fd
, con
->filename
);
1961 unlink(con
->filename
);
1962 cupsdClearString(&con
->filename
);
1964 if (!cupsdSendError(con
, HTTP_REQUEST_TOO_LARGE
))
1965 return (cupsdCloseClient(con
));
1969 * Install the configuration file...
1972 status
= install_conf_file(con
);
1975 * Return the status to the client...
1978 if (!cupsdSendError(con
, status
))
1979 return (cupsdCloseClient(con
));
1983 case HTTP_POST_RECV
:
1984 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1985 "cupsdReadClient: %d con->data_encoding=HTTP_ENCODE_"
1986 "%s, con->data_remaining=" CUPS_LLFMT
", con->file=%d",
1988 con
->http
.data_encoding
== HTTP_ENCODE_CHUNKED
?
1989 "CHUNKED" : "LENGTH",
1990 CUPS_LLCAST con
->http
.data_remaining
, con
->file
);
1992 if (con
->request
!= NULL
)
1995 * Grab any request data from the connection...
1998 if ((ipp_state
= ippRead(&(con
->http
), con
->request
)) == IPP_ERROR
)
2000 cupsdLogMessage(CUPSD_LOG_ERROR
,
2001 "cupsdReadClient: %d IPP Read Error!",
2004 cupsdSendError(con
, HTTP_BAD_REQUEST
);
2005 return (cupsdCloseClient(con
));
2007 else if (ipp_state
!= IPP_DATA
)
2009 if (con
->http
.state
== HTTP_POST_SEND
)
2011 cupsdSendError(con
, HTTP_BAD_REQUEST
);
2012 return (cupsdCloseClient(con
));
2018 con
->bytes
+= ippLength(con
->request
);
2021 if (con
->file
< 0 && con
->http
.state
!= HTTP_POST_SEND
)
2024 * Create a file as needed for the request data...
2027 cupsdSetStringf(&con
->filename
, "%s/%08x", RequestRoot
, request_id
++);
2028 con
->file
= open(con
->filename
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0640);
2030 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdReadClient: %d REQUEST %s=%d", con
->http
.fd
,
2031 con
->filename
, con
->file
);
2035 if (!cupsdSendError(con
, HTTP_REQUEST_TOO_LARGE
))
2036 return (cupsdCloseClient(con
));
2039 fchmod(con
->file
, 0640);
2040 fchown(con
->file
, RunUser
, Group
);
2041 fcntl(con
->file
, F_SETFD
, fcntl(con
->file
, F_GETFD
) | FD_CLOEXEC
);
2044 if (con
->http
.state
!= HTTP_POST_SEND
)
2046 if ((bytes
= httpRead2(HTTP(con
), line
, sizeof(line
))) < 0)
2047 return (cupsdCloseClient(con
));
2050 con
->bytes
+= bytes
;
2052 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2053 "cupsdReadClient: %d writing %d bytes to %d",
2054 con
->http
.fd
, bytes
, con
->file
);
2056 if (write(con
->file
, line
, bytes
) < bytes
)
2058 cupsdLogMessage(CUPSD_LOG_ERROR
,
2059 "cupsdReadClient: Unable to write %d bytes to %s: %s",
2060 bytes
, con
->filename
, strerror(errno
));
2062 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2063 "cupsdReadClient: Closing file %d...",
2068 unlink(con
->filename
);
2069 cupsdClearString(&con
->filename
);
2071 if (!cupsdSendError(con
, HTTP_REQUEST_TOO_LARGE
))
2072 return (cupsdCloseClient(con
));
2075 else if (con
->http
.state
== HTTP_POST_RECV
)
2076 return (1); /* ??? */
2077 else if (con
->http
.state
!= HTTP_POST_SEND
)
2078 return (cupsdCloseClient(con
));
2081 if (con
->http
.state
== HTTP_POST_SEND
)
2085 fstat(con
->file
, &filestats
);
2087 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2088 "cupsdReadClient: %d Closing data file %d, "
2089 "size=" CUPS_LLFMT
".",
2090 con
->http
.fd
, con
->file
,
2091 CUPS_LLCAST filestats
.st_size
);
2096 if (filestats
.st_size
> MaxRequestSize
&&
2100 * Request is too big; remove it and send an error...
2103 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2104 "cupsdReadClient: %d Removing temp file %s",
2105 con
->http
.fd
, con
->filename
);
2106 unlink(con
->filename
);
2107 cupsdClearString(&con
->filename
);
2112 * Delete any IPP request data...
2115 ippDelete(con
->request
);
2116 con
->request
= NULL
;
2119 if (!cupsdSendError(con
, HTTP_REQUEST_TOO_LARGE
))
2120 return (cupsdCloseClient(con
));
2125 if (!cupsdSendCommand(con
, con
->command
, con
->options
, 0))
2127 if (!cupsdSendError(con
, HTTP_NOT_FOUND
))
2128 return (cupsdCloseClient(con
));
2131 cupsdLogRequest(con
, HTTP_OK
);
2136 return (cupsdProcessIPPRequest(con
));
2141 break; /* Anti-compiler-warning-code */
2144 if (!con
->http
.keep_alive
&& con
->http
.state
== HTTP_WAITING
)
2145 return (cupsdCloseClient(con
));
2152 * 'cupsdSendCommand()' - Send output from a command via HTTP.
2155 int /* O - 1 on success, 0 on failure */
2157 cupsd_client_t
*con
, /* I - Client connection */
2158 char *command
, /* I - Command to run */
2159 char *options
, /* I - Command-line options */
2160 int root
) /* I - Run as root? */
2162 int fd
; /* Standard input file descriptor */
2166 fd
= open(con
->filename
, O_RDONLY
);
2168 fd
= open("/dev/null", O_RDONLY
);
2172 cupsdLogMessage(CUPSD_LOG_ERROR
,
2173 "cupsdSendCommand: %d Unable to open \"%s\" for reading: %s",
2174 con
->http
.fd
, con
->filename
? con
->filename
: "/dev/null",
2179 fcntl(fd
, F_SETFD
, fcntl(fd
, F_GETFD
) | FD_CLOEXEC
);
2181 con
->pipe_pid
= pipe_command(con
, fd
, &(con
->file
), command
, options
, root
);
2185 cupsdLogMessage(CUPSD_LOG_INFO
, "Started \"%s\" (pid=%d)", command
,
2188 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdSendCommand: %d file=%d",
2189 con
->http
.fd
, con
->file
);
2191 if (con
->pipe_pid
== 0)
2194 fcntl(con
->file
, F_SETFD
, fcntl(con
->file
, F_GETFD
) | FD_CLOEXEC
);
2196 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2197 "cupsdSendCommand: Adding fd %d to InputSet...", con
->file
);
2198 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2199 "cupsdSendCommand: Adding fd %d to OutputSet...",
2202 FD_SET(con
->file
, InputSet
);
2203 FD_SET(con
->http
.fd
, OutputSet
);
2205 con
->sent_header
= 0;
2206 con
->file_ready
= 0;
2207 con
->got_fields
= 0;
2215 * 'cupsdSendError()' - Send an error message via HTTP.
2218 int /* O - 1 if successful, 0 otherwise */
2219 cupsdSendError(cupsd_client_t
*con
, /* I - Connection */
2220 http_status_t code
) /* I - Error code */
2223 * Put the request in the access_log file...
2226 cupsdLogRequest(con
, code
);
2228 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdSendError: %d code=%d (%s)",
2229 con
->http
.fd
, code
, httpStatus(code
));
2232 * To work around bugs in some proxies, don't use Keep-Alive for some
2236 if (code
>= HTTP_BAD_REQUEST
)
2237 con
->http
.keep_alive
= HTTP_KEEPALIVE_OFF
;
2240 * Send an error message back to the client. If the error code is a
2241 * 400 or 500 series, make sure the message contains some text, too!
2244 if (!cupsdSendHeader(con
, code
, NULL
))
2248 if (code
== HTTP_UPGRADE_REQUIRED
)
2249 if (httpPrintf(HTTP(con
), "Connection: Upgrade\r\n") < 0)
2252 if (httpPrintf(HTTP(con
), "Upgrade: TLS/1.0,HTTP/1.1\r\n") < 0)
2254 #endif /* HAVE_SSL */
2256 if ((con
->http
.version
>= HTTP_1_1
&& !con
->http
.keep_alive
) ||
2257 (code
>= HTTP_BAD_REQUEST
&& code
!= HTTP_UPGRADE_REQUIRED
))
2259 if (httpPrintf(HTTP(con
), "Connection: close\r\n") < 0)
2263 if (code
>= HTTP_BAD_REQUEST
)
2266 * Send a human-readable error message.
2269 char message
[1024]; /* Message for user */
2270 const char *text
; /* Status-specific text */
2272 if (code
== HTTP_UNAUTHORIZED
)
2273 text
= _cupsLangString(con
->language
,
2274 _("Enter your username and password or the "
2275 "root username and password to access this "
2277 else if (code
== HTTP_UPGRADE_REQUIRED
)
2278 text
= _cupsLangString(con
->language
,
2279 _("You must use a https: URL to access this "
2284 snprintf(message
, sizeof(message
),
2285 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\" "
2286 "\"http://www.w3.org/TR/REC-html40/loose.dtd\">\n"
2289 "\t<META HTTP-EQUIV=\"Content-Type\" "
2290 "CONTENT=\"text/html; charset=utf-8\">\n"
2291 "\t<TITLE>%d %s</TITLE>\n"
2292 "\t<LINK REL=\"STYLESHEET\" TYPE=\"text/css\" "
2293 "HREF=\"/cups.css\">\n"
2300 code
, httpStatus(code
), code
, httpStatus(code
), text
);
2302 if (httpPrintf(HTTP(con
), "Content-Type: text/html; charset=utf-8\r\n") < 0)
2304 if (httpPrintf(HTTP(con
), "Content-Length: %d\r\n",
2305 (int)strlen(message
)) < 0)
2307 if (httpPrintf(HTTP(con
), "\r\n") < 0)
2309 if (httpPrintf(HTTP(con
), "%s", message
) < 0)
2312 else if (httpPrintf(HTTP(con
), "\r\n") < 0)
2315 con
->http
.state
= HTTP_WAITING
;
2322 * 'cupsdSendFile()' - Send a file via HTTP.
2325 int /* O - 0 on failure, 1 on success */
2326 cupsdSendFile(cupsd_client_t
*con
, /* I - Client connection */
2327 http_status_t code
, /* I - HTTP status */
2328 char *filename
, /* I - Filename */
2329 char *type
, /* I - File type */
2330 struct stat
*filestats
)/* O - File information */
2332 con
->file
= open(filename
, O_RDONLY
);
2334 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdSendFile: %d file=%d", con
->http
.fd
,
2340 fcntl(con
->file
, F_SETFD
, fcntl(con
->file
, F_GETFD
) | FD_CLOEXEC
);
2344 if (!cupsdSendHeader(con
, code
, type
))
2347 if (httpPrintf(HTTP(con
), "Last-Modified: %s\r\n",
2348 httpGetDateString(filestats
->st_mtime
)) < 0)
2350 if (httpPrintf(HTTP(con
), "Content-Length: " CUPS_LLFMT
"\r\n",
2351 CUPS_LLCAST filestats
->st_size
) < 0)
2353 if (httpPrintf(HTTP(con
), "\r\n") < 0)
2356 con
->http
.data_encoding
= HTTP_ENCODE_LENGTH
;
2357 con
->http
.data_remaining
= filestats
->st_size
;
2359 if (con
->http
.data_remaining
<= INT_MAX
)
2360 con
->http
._data_remaining
= con
->http
.data_remaining
;
2362 con
->http
._data_remaining
= INT_MAX
;
2364 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2365 "cupsdSendFile: Adding fd %d to OutputSet...", con
->http
.fd
);
2367 FD_SET(con
->http
.fd
, OutputSet
);
2374 * 'cupsdSendHeader()' - Send an HTTP request.
2377 int /* O - 1 on success, 0 on failure */
2378 cupsdSendHeader(cupsd_client_t
*con
, /* I - Client to send to */
2379 http_status_t code
, /* I - HTTP status code */
2380 char *type
) /* I - MIME type of document */
2382 if (httpPrintf(HTTP(con
), "HTTP/%d.%d %d %s\r\n", con
->http
.version
/ 100,
2383 con
->http
.version
% 100, code
, httpStatus(code
)) < 0)
2385 if (httpPrintf(HTTP(con
), "Date: %s\r\n", httpGetDateString(time(NULL
))) < 0)
2388 if (httpPrintf(HTTP(con
), "Server: %s\r\n", ServerHeader
) < 0)
2390 if (con
->http
.keep_alive
&& con
->http
.version
>= HTTP_1_0
)
2392 if (httpPrintf(HTTP(con
), "Connection: Keep-Alive\r\n") < 0)
2394 if (httpPrintf(HTTP(con
), "Keep-Alive: timeout=%d\r\n",
2395 KeepAliveTimeout
) < 0)
2398 if (code
== HTTP_METHOD_NOT_ALLOWED
)
2399 if (httpPrintf(HTTP(con
), "Allow: GET, HEAD, OPTIONS, POST\r\n") < 0)
2402 if (code
== HTTP_UNAUTHORIZED
)
2404 int auth_type
; /* Authentication type */
2407 if (!con
->best
|| con
->best
->type
== AUTH_NONE
)
2408 auth_type
= DefaultAuthType
;
2410 auth_type
= con
->best
->type
;
2412 if (auth_type
!= AUTH_DIGEST
)
2414 if (httpPrintf(HTTP(con
),
2415 "WWW-Authenticate: Basic realm=\"CUPS\"\r\n") < 0)
2420 if (httpPrintf(HTTP(con
),
2421 "WWW-Authenticate: Digest realm=\"CUPS\", nonce=\"%s\"\r\n",
2422 con
->http
.hostname
) < 0)
2427 if (con
->language
!= NULL
)
2429 if (httpPrintf(HTTP(con
), "Content-Language: %s\r\n",
2430 con
->language
->language
) < 0)
2436 if (!strcmp(type
, "text/html"))
2438 if (httpPrintf(HTTP(con
),
2439 "Content-Type: text/html; charset=utf-8\r\n") < 0)
2442 else if (httpPrintf(HTTP(con
), "Content-Type: %s\r\n", type
) < 0)
2451 * 'cupsdUpdateCGI()' - Read status messages from CGI scripts and programs.
2455 cupsdUpdateCGI(void)
2457 char *ptr
, /* Pointer to end of line in buffer */
2458 message
[1024]; /* Pointer to message text */
2459 int loglevel
; /* Log level for message */
2462 while ((ptr
= cupsdStatBufUpdate(CGIStatusBuffer
, &loglevel
,
2463 message
, sizeof(message
))) != NULL
)
2464 if (!strchr(CGIStatusBuffer
->buffer
, '\n'))
2467 if (ptr
== NULL
&& errno
)
2470 * Fatal error on pipe - should never happen!
2473 cupsdLogMessage(CUPSD_LOG_CRIT
,
2474 "cupsdUpdateCGI: error reading from CGI error pipe - %s",
2481 * 'cupsdWriteClient()' - Write data to a client as needed.
2484 int /* O - 1 if success, 0 if fail */
2485 cupsdWriteClient(cupsd_client_t
*con
) /* I - Client connection */
2487 int bytes
; /* Number of bytes written */
2488 char buf
[16385]; /* Data buffer */
2489 char *bufptr
; /* Pointer into buffer */
2490 ipp_state_t ipp_state
; /* IPP state value */
2494 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2495 "cupsdWriteClient(con=%p) %d response=%p, file=%d "
2496 "pipe_pid=%d state=%d",
2497 con
, con
->http
.fd
, con
->response
, con
->file
, con
->pipe_pid
,
2501 if (con
->http
.state
!= HTTP_GET_SEND
&&
2502 con
->http
.state
!= HTTP_POST_SEND
)
2505 if (con
->response
!= NULL
)
2507 ipp_state
= ippWrite(&(con
->http
), con
->response
);
2508 bytes
= ipp_state
!= IPP_ERROR
&& ipp_state
!= IPP_DATA
;
2510 else if ((bytes
= read(con
->file
, buf
, sizeof(buf
) - 1)) > 0)
2512 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2513 "cupsdWriteClient: Read %d bytes from file %d...",
2516 if (con
->pipe_pid
&& !con
->got_fields
)
2519 * Inspect the data for Content-Type and other fields.
2524 for (bufptr
= buf
; !con
->got_fields
&& *bufptr
; bufptr
++)
2525 if (*bufptr
== '\n')
2528 * Send line to client...
2531 if (bufptr
> buf
&& bufptr
[-1] == '\r')
2535 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "Script header: %s", buf
);
2537 if (!con
->sent_header
)
2540 * Handle redirection and CGI status codes...
2543 if (!strncasecmp(buf
, "Location:", 9))
2544 cupsdSendHeader(con
, HTTP_SEE_OTHER
, NULL
);
2545 else if (!strncasecmp(buf
, "Status:", 7))
2546 cupsdSendHeader(con
, atoi(buf
+ 7), NULL
);
2548 cupsdSendHeader(con
, HTTP_OK
, NULL
);
2550 if (con
->http
.version
== HTTP_1_1
)
2552 con
->http
.data_encoding
= HTTP_ENCODE_CHUNKED
;
2554 if (httpPrintf(HTTP(con
), "Transfer-Encoding: chunked\r\n") < 0)
2558 con
->sent_header
= 1;
2561 if (strncasecmp(buf
, "Status:", 7))
2562 httpPrintf(HTTP(con
), "%s\r\n", buf
);
2564 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdWriteClient: %d %s",
2571 bytes
-= (bufptr
- buf
);
2572 memmove(buf
, bufptr
, bytes
+ 1);
2576 * See if the line was empty...
2579 if (con
->field_col
== 0)
2580 con
->got_fields
= 1;
2584 else if (*bufptr
!= '\r')
2587 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2588 "cupsdWriteClient: %d bytes=%d, got_fields=%d",
2589 con
->http
.fd
, bytes
, con
->got_fields
);
2591 if (bytes
> 0 && !con
->got_fields
)
2594 * Remaining text needs to go out...
2597 httpPrintf(HTTP(con
), "%s", buf
);
2599 con
->http
.activity
= time(NULL
);
2602 else if (bytes
== 0)
2604 con
->http
.activity
= time(NULL
);
2609 if (httpWrite2(HTTP(con
), buf
, bytes
) < 0)
2611 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2612 "cupsdWriteClient: %d Write of %d bytes failed!",
2613 con
->http
.fd
, bytes
);
2615 cupsdCloseClient(con
);
2619 con
->bytes
+= bytes
;
2621 if (con
->http
.state
== HTTP_WAITING
)
2627 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdWriteClient: %d bytes < 0",
2630 cupsdLogRequest(con
, HTTP_OK
);
2632 httpFlushWrite(HTTP(con
));
2634 if (con
->http
.data_encoding
== HTTP_ENCODE_CHUNKED
)
2636 if (httpPrintf(HTTP(con
), "0\r\n\r\n") < 0)
2638 cupsdCloseClient(con
);
2643 con
->http
.state
= HTTP_WAITING
;
2645 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2646 "cupsdWriteClient: Removing fd %d from OutputSet...",
2649 FD_CLR(con
->http
.fd
, OutputSet
);
2653 if (FD_ISSET(con
->file
, InputSet
))
2655 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2656 "cupsdWriteClient: Removing fd %d from InputSet...",
2658 FD_CLR(con
->file
, InputSet
);
2662 cupsdEndProcess(con
->pipe_pid
, 0);
2664 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2665 "cupsdWriteClient: %d Closing data file %d.",
2666 con
->http
.fd
, con
->file
);
2675 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2676 "cupsdWriteClient: %d Removing temp file %s",
2677 con
->http
.fd
, con
->filename
);
2678 unlink(con
->filename
);
2679 cupsdClearString(&con
->filename
);
2682 if (con
->request
!= NULL
)
2684 ippDelete(con
->request
);
2685 con
->request
= NULL
;
2688 if (con
->response
!= NULL
)
2690 ippDelete(con
->response
);
2691 con
->response
= NULL
;
2694 cupsdClearString(&con
->command
);
2695 cupsdClearString(&con
->options
);
2697 if (!con
->http
.keep_alive
)
2699 cupsdCloseClient(con
);
2705 con
->file_ready
= 0;
2707 if (con
->pipe_pid
&& !FD_ISSET(con
->file
, InputSet
))
2709 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2710 "cupsdWriteClient: Adding fd %d to InputSet...",
2712 FD_SET(con
->file
, InputSet
);
2716 con
->http
.activity
= time(NULL
);
2723 * 'check_if_modified()' - Decode an "If-Modified-Since" line.
2726 static int /* O - 1 if modified since */
2728 cupsd_client_t
*con
, /* I - Client connection */
2729 struct stat
*filestats
) /* I - File information */
2731 char *ptr
; /* Pointer into field */
2732 time_t date
; /* Time/date value */
2733 off_t size
; /* Size/length value */
2738 ptr
= con
->http
.fields
[HTTP_FIELD_IF_MODIFIED_SINCE
];
2743 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2744 "check_if_modified: %d If-Modified-Since=\"%s\"",
2747 while (*ptr
!= '\0')
2749 while (isspace(*ptr
) || *ptr
== ';')
2752 if (strncasecmp(ptr
, "length=", 7) == 0)
2755 size
= strtoll(ptr
, NULL
, 10);
2757 while (isdigit(*ptr
))
2760 else if (isalpha(*ptr
))
2762 date
= httpGetDateTime(ptr
);
2763 while (*ptr
!= '\0' && *ptr
!= ';')
2768 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2769 "check_if_modified: %d sizes=" CUPS_LLFMT
","
2770 CUPS_LLFMT
" dates=%d,%d",
2771 con
->http
.fd
, CUPS_LLCAST size
,
2772 CUPS_LLCAST filestats
->st_size
, (int)date
,
2773 (int)filestats
->st_mtime
);
2775 return ((size
!= filestats
->st_size
&& size
!= 0) ||
2776 (date
< filestats
->st_mtime
&& date
!= 0) ||
2777 (size
== 0 && date
== 0));
2783 * 'get_cdsa_server_certs()' - Convert a keychain name into the CFArrayRef
2784 * required by SSLSetCertificate.
2786 * For now we assumes that there is exactly one SecIdentity in the
2787 * keychain - i.e. there is exactly one matching cert/private key pair.
2788 * In the future we will search a keychain for a SecIdentity matching a
2789 * specific criteria. We also skip the operation of adding additional
2790 * non-signing certs from the keychain to the CFArrayRef.
2792 * To create a self-signed certificate for testing use the certtool.
2793 * Executing the following as root will do it:
2795 * certtool c c v k=CUPS
2798 static CFArrayRef
/* O - Array of certificates */
2799 get_cdsa_server_certs(void)
2801 OSStatus err
; /* Error info */
2802 SecKeychainRef kcRef
; /* Keychain reference */
2803 SecIdentitySearchRef srchRef
; /* Search reference */
2804 SecIdentityRef identity
; /* Identity */
2805 CFArrayRef ca
; /* Certificate array */
2812 err
= SecKeychainOpen(ServerCertificate
, &kcRef
);
2815 cupsdLogMessage(CUPSD_LOG_ERROR
, "Cannot open keychain \"%s\", error %d.",
2816 ServerCertificate
, (int)err
);
2820 * Search for "any" identity matching specified key use;
2821 * in this app, we expect there to be exactly one.
2824 err
= SecIdentitySearchCreate(kcRef
, CSSM_KEYUSE_SIGN
, &srchRef
);
2827 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2828 "Cannot find signing key in keychain \"%s\", error %d",
2829 ServerCertificate
, (int)err
);
2832 err
= SecIdentitySearchCopyNext(srchRef
, &identity
);
2835 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2836 "Cannot find signing key in keychain \"%s\", error %d",
2837 ServerCertificate
, (int)err
);
2840 if (CFGetTypeID(identity
) != SecIdentityGetTypeID())
2841 cupsdLogMessage(CUPSD_LOG_ERROR
,
2842 "SecIdentitySearchCopyNext CFTypeID failure!");
2846 * Found one. Place it in a CFArray.
2847 * TBD: snag other (non-identity) certs from keychain and add them
2851 ca
= CFArrayCreate(NULL
, (const void **)&identity
, 1, NULL
);
2854 cupsdLogMessage(CUPSD_LOG_ERROR
, "CFArrayCreate error");
2857 /*CFRelease(identity);*/
2860 /*CFRelease(srchRef);*/
2863 /*CFRelease(kcRef);*/
2868 #endif /* HAVE_CDSASSL */
2872 * 'get_file()' - Get a filename and state info.
2875 static char * /* O - Real filename */
2876 get_file(cupsd_client_t
*con
, /* I - Client connection */
2877 struct stat
*filestats
, /* O - File information */
2878 char *filename
, /* IO - Filename buffer */
2879 int len
) /* I - Buffer length */
2881 int status
; /* Status of filesystem calls */
2882 char *ptr
; /* Pointer info filename */
2883 int plen
; /* Remaining length after pointer */
2887 * Need to add DocumentRoot global...
2890 if (!strncmp(con
->uri
, "/ppd/", 5))
2891 snprintf(filename
, len
, "%s%s", ServerRoot
, con
->uri
);
2892 else if (!strncmp(con
->uri
, "/admin/conf/", 12))
2893 snprintf(filename
, len
, "%s%s", ServerRoot
, con
->uri
+ 11);
2894 else if (!strncmp(con
->uri
, "/admin/log/", 11))
2896 if (!strcmp(con
->uri
+ 11, "access_log") && AccessLog
[0] == '/')
2897 strlcpy(filename
, AccessLog
, len
);
2898 else if (!strcmp(con
->uri
+ 11, "error_log") && ErrorLog
[0] == '/')
2899 strlcpy(filename
, ErrorLog
, len
);
2900 else if (!strcmp(con
->uri
+ 11, "page_log") && PageLog
[0] == '/')
2901 strlcpy(filename
, PageLog
, len
);
2905 else if (con
->language
!= NULL
)
2906 snprintf(filename
, len
, "%s/%s%s", DocumentRoot
, con
->language
->language
,
2909 snprintf(filename
, len
, "%s%s", DocumentRoot
, con
->uri
);
2911 if ((ptr
= strchr(filename
, '?')) != NULL
)
2915 * Grab the status for this language; if there isn't a language-specific file
2916 * then fallback to the default one...
2919 if ((status
= stat(filename
, filestats
)) != 0 && con
->language
!= NULL
)
2922 * Drop the language prefix and try the current directory...
2925 if (strncmp(con
->uri
, "/ppd/", 5) &&
2926 strncmp(con
->uri
, "/admin/conf/", 12) &&
2927 strncmp(con
->uri
, "/admin/log/", 11))
2929 snprintf(filename
, len
, "%s%s", DocumentRoot
, con
->uri
);
2931 if ((ptr
= strchr(filename
, '?')) != NULL
)
2934 status
= stat(filename
, filestats
);
2939 * If we're found a directory, get the index.html file instead...
2942 if (!status
&& S_ISDIR(filestats
->st_mode
))
2944 if (filename
[strlen(filename
) - 1] != '/')
2945 strlcat(filename
, "/", len
);
2947 ptr
= filename
+ strlen(filename
);
2948 plen
= len
- (ptr
- filename
);
2950 strlcpy(ptr
, "index.html", plen
);
2951 status
= stat(filename
, filestats
);
2956 strlcpy(ptr
, "index.class", plen
);
2957 status
= stat(filename
, filestats
);
2959 #endif /* HAVE_JAVA */
2964 strlcpy(ptr
, "index.pl", plen
);
2965 status
= stat(filename
, filestats
);
2967 #endif /* HAVE_PERL */
2972 strlcpy(ptr
, "index.php", plen
);
2973 status
= stat(filename
, filestats
);
2975 #endif /* HAVE_PHP */
2980 strlcpy(ptr
, "index.pyc", plen
);
2981 status
= stat(filename
, filestats
);
2986 strlcpy(ptr
, "index.py", plen
);
2987 status
= stat(filename
, filestats
);
2989 #endif /* HAVE_PYTHON */
2992 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_file: %d filename=%s size=%d",
2993 con
->http
.fd
, filename
,
2994 status
? -1 : (int)filestats
->st_size
);
2997 con
->http
.data_remaining
= (int)filestats
->st_size
;
3007 * 'install_conf_file()' - Install a configuration file.
3010 static http_status_t
/* O - Status */
3011 install_conf_file(cupsd_client_t
*con
) /* I - Connection */
3013 cups_file_t
*in
, /* Input file */
3014 *out
; /* Output file */
3015 char buffer
[1024]; /* Copy buffer */
3016 int bytes
; /* Number of bytes */
3017 char conffile
[1024], /* Configuration filename */
3018 newfile
[1024], /* New config filename */
3019 oldfile
[1024]; /* Old config filename */
3020 struct stat confinfo
; /* Config file info */
3024 * First construct the filenames...
3027 snprintf(conffile
, sizeof(conffile
), "%s%s", ServerRoot
, con
->uri
+ 11);
3028 snprintf(newfile
, sizeof(newfile
), "%s%s.N", ServerRoot
, con
->uri
+ 11);
3029 snprintf(oldfile
, sizeof(oldfile
), "%s%s.O", ServerRoot
, con
->uri
+ 11);
3031 cupsdLogMessage(CUPSD_LOG_INFO
, "Installing config file \"%s\"...", conffile
);
3034 * Get the owner, group, and permissions of the configuration file.
3035 * If it doesn't exist, assign it to the User and Group in the
3036 * cupsd.conf file with mode 0640 permissions.
3039 if (stat(conffile
, &confinfo
))
3041 confinfo
.st_uid
= User
;
3042 confinfo
.st_gid
= Group
;
3043 confinfo
.st_mode
= ConfigFilePerm
;
3047 * Open the request file and new config file...
3050 if ((in
= cupsFileOpen(con
->filename
, "rb")) == NULL
)
3052 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to open request file \"%s\" - %s",
3053 con
->filename
, strerror(errno
));
3054 return (HTTP_SERVER_ERROR
);
3057 if ((out
= cupsFileOpen(newfile
, "wb")) == NULL
)
3060 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to open config file \"%s\" - %s",
3061 newfile
, strerror(errno
));
3062 return (HTTP_SERVER_ERROR
);
3065 fchmod(cupsFileNumber(out
), confinfo
.st_mode
);
3066 fchown(cupsFileNumber(out
), confinfo
.st_uid
, confinfo
.st_gid
);
3069 * Copy from the request to the new config file...
3072 while ((bytes
= cupsFileRead(in
, buffer
, sizeof(buffer
))) > 0)
3073 if (cupsFileWrite(out
, buffer
, bytes
) < bytes
)
3075 cupsdLogMessage(CUPSD_LOG_ERROR
,
3076 "Unable to copy to config file \"%s\" - %s",
3077 newfile
, strerror(errno
));
3083 return (HTTP_SERVER_ERROR
);
3087 * Close the files...
3091 if (cupsFileClose(out
))
3093 cupsdLogMessage(CUPSD_LOG_ERROR
,
3094 "Error file closing config file \"%s\" - %s",
3095 newfile
, strerror(errno
));
3099 return (HTTP_SERVER_ERROR
);
3103 * Remove the request file...
3106 unlink(con
->filename
);
3107 cupsdClearString(&con
->filename
);
3110 * Unlink the old backup, rename the current config file to the backup
3111 * filename, and rename the new config file to the config file name...
3114 if (unlink(oldfile
))
3115 if (errno
!= ENOENT
)
3117 cupsdLogMessage(CUPSD_LOG_ERROR
,
3118 "Unable to remove backup config file \"%s\" - %s",
3119 oldfile
, strerror(errno
));
3123 return (HTTP_SERVER_ERROR
);
3126 if (rename(conffile
, oldfile
))
3127 if (errno
!= ENOENT
)
3129 cupsdLogMessage(CUPSD_LOG_ERROR
,
3130 "Unable to rename old config file \"%s\" - %s",
3131 conffile
, strerror(errno
));
3135 return (HTTP_SERVER_ERROR
);
3138 if (rename(newfile
, conffile
))
3140 cupsdLogMessage(CUPSD_LOG_ERROR
,
3141 "Unable to rename new config file \"%s\" - %s",
3142 newfile
, strerror(errno
));
3144 rename(oldfile
, conffile
);
3147 return (HTTP_SERVER_ERROR
);
3151 * If the cupsd.conf file was updated, set the NeedReload flag...
3154 if (!strcmp(con
->uri
, "/admin/conf/cupsd.conf"))
3155 NeedReload
= RELOAD_CUPSD
;
3157 NeedReload
= RELOAD_ALL
;
3159 ReloadTime
= time(NULL
);
3162 * Return that the file was created successfully...
3165 return (HTTP_CREATED
);
3170 * 'is_path_absolute()' - Is a path absolute and free of relative elements (i.e. "..").
3173 static int /* O - 0 if relative, 1 if absolute */
3174 is_path_absolute(const char *path
) /* I - Input path */
3177 * Check for a leading slash...
3184 * Check for "/.." in the path...
3187 while ((path
= strstr(path
, "/..")) != NULL
)
3189 if (!path
[3] || path
[3] == '/')
3196 * If we haven't found any relative paths, return 1 indicating an
3205 * 'pipe_command()' - Pipe the output of a command to the remote client.
3208 static int /* O - Process ID */
3209 pipe_command(cupsd_client_t
*con
, /* I - Client connection */
3210 int infile
, /* I - Standard input for command */
3211 int *outfile
, /* O - Standard output for command */
3212 char *command
, /* I - Command to run */
3213 char *options
, /* I - Options for command */
3214 int root
) /* I - Run as root? */
3216 int i
; /* Looping var */
3217 int pid
; /* Process ID */
3218 char *commptr
; /* Command string pointer */
3219 char *uriptr
; /* URI string pointer */
3220 int fds
[2]; /* Pipe FDs */
3221 int argc
; /* Number of arguments */
3222 int envc
; /* Number of environment variables */
3223 char argbuf
[10240], /* Argument buffer */
3224 *argv
[100], /* Argument strings */
3225 *envp
[MAX_ENV
+ 16]; /* Environment variables */
3226 char content_length
[1024], /* CONTENT_LENGTH environment variable */
3227 content_type
[1024], /* CONTENT_TYPE environment variable */
3228 http_cookie
[32768], /* HTTP_COOKIE environment variable */
3229 http_user_agent
[1024], /* HTTP_USER_AGENT environment variable */
3230 lang
[1024], /* LANG environment variable */
3231 *query_string
, /* QUERY_STRING env variable */
3232 remote_addr
[1024], /* REMOTE_ADDR environment variable */
3233 remote_host
[1024], /* REMOTE_HOST environment variable */
3234 remote_user
[1024], /* REMOTE_USER environment variable */
3235 script_name
[1024], /* SCRIPT_NAME environment variable */
3236 server_name
[1024], /* SERVER_NAME environment variable */
3237 server_port
[1024]; /* SERVER_PORT environment variable */
3241 * Parse a copy of the options string, which is of the form:
3243 * name argument+argument+argument
3244 * name?argument+argument+argument
3245 * name param=value¶m=value
3246 * name?param=value¶m=value
3248 * If the string contains an "=" character after the initial name,
3249 * then we treat it as a HTTP GET form request and make a copy of
3250 * the remaining string for the environment variable.
3252 * The string is always parsed out as command-line arguments, to
3253 * be consistent with Apache...
3256 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3257 "pipe_command: command=\"%s\", options=\"%s\"",
3260 strlcpy(argbuf
, options
, sizeof(argbuf
));
3263 query_string
= NULL
;
3265 for (commptr
= argbuf
, argc
= 1; *commptr
!= '\0' && argc
< 99; commptr
++)
3268 * Break arguments whenever we see a + or space...
3271 if (*commptr
== ' ' || *commptr
== '+' || (*commptr
== '?' && argc
== 1))
3274 * Terminate the current string and skip trailing whitespace...
3279 while (*commptr
== ' ')
3283 * If we don't have a blank string, save it as another argument...
3288 argv
[argc
] = commptr
;
3295 * If we see an "=" in the remaining string, make a copy of it since
3296 * it will be query data...
3299 if (argc
== 2 && strchr(commptr
, '=') && con
->operation
== HTTP_GET
)
3300 cupsdSetStringf(&query_string
, "QUERY_STRING=%s", commptr
);
3303 * Don't skip the first non-blank character...
3308 else if (*commptr
== '%' && isxdigit(commptr
[1] & 255) &&
3309 isxdigit(commptr
[2] & 255))
3312 * Convert the %xx notation to the individual character.
3315 if (commptr
[1] >= '0' && commptr
[1] <= '9')
3316 *commptr
= (commptr
[1] - '0') << 4;
3318 *commptr
= (tolower(commptr
[1]) - 'a' + 10) << 4;
3320 if (commptr
[2] >= '0' && commptr
[2] <= '9')
3321 *commptr
|= commptr
[2] - '0';
3323 *commptr
|= tolower(commptr
[2]) - 'a' + 10;
3325 _cups_strcpy(commptr
+ 1, commptr
+ 3);
3328 * Check for a %00 and break if that is the case...
3338 if (argv
[0][0] == '\0')
3339 argv
[0] = strrchr(command
, '/') + 1;
3342 * Setup the environment variables as needed...
3346 snprintf(lang
, sizeof(lang
), "LANG=%s.UTF-8", con
->language
->language
);
3348 strcpy(lang
, "LANG=C");
3350 strcpy(remote_addr
, "REMOTE_ADDR=");
3351 httpAddrString(con
->http
.hostaddr
, remote_addr
+ 12,
3352 sizeof(remote_addr
) - 12);
3354 snprintf(remote_host
, sizeof(remote_host
), "REMOTE_HOST=%s",
3355 con
->http
.hostname
);
3357 snprintf(script_name
, sizeof(script_name
), "SCRIPT_NAME=%s", con
->uri
);
3358 if ((uriptr
= strchr(script_name
, '?')) != NULL
)
3361 sprintf(server_port
, "SERVER_PORT=%d", con
->serverport
);
3363 snprintf(server_name
, sizeof(server_name
), "SERVER_NAME=%s",
3366 envc
= cupsdLoadEnv(envp
, (int)(sizeof(envp
) / sizeof(envp
[0])));
3368 envp
[envc
++] = lang
;
3369 envp
[envc
++] = "REDIRECT_STATUS=1";
3370 envp
[envc
++] = server_name
;
3371 envp
[envc
++] = server_port
;
3372 envp
[envc
++] = remote_addr
;
3373 envp
[envc
++] = remote_host
;
3374 envp
[envc
++] = script_name
;
3376 if (con
->username
[0])
3378 snprintf(remote_user
, sizeof(remote_user
), "REMOTE_USER=%s", con
->username
);
3380 envp
[envc
++] = remote_user
;
3383 if (con
->http
.version
== HTTP_1_1
)
3384 envp
[envc
++] = "SERVER_PROTOCOL=HTTP/1.1";
3385 else if (con
->http
.version
== HTTP_1_0
)
3386 envp
[envc
++] = "SERVER_PROTOCOL=HTTP/1.0";
3388 envp
[envc
++] = "SERVER_PROTOCOL=HTTP/0.9";
3390 if (con
->http
.cookie
)
3392 snprintf(http_cookie
, sizeof(http_cookie
), "HTTP_COOKIE=%s",
3394 envp
[envc
++] = http_cookie
;
3397 if (con
->http
.fields
[HTTP_FIELD_USER_AGENT
][0])
3399 snprintf(http_user_agent
, sizeof(http_user_agent
), "HTTP_USER_AGENT=%s",
3400 con
->http
.fields
[HTTP_FIELD_USER_AGENT
]);
3401 envp
[envc
++] = http_user_agent
;
3404 if (con
->operation
== HTTP_GET
)
3406 for (i
= 0; i
< argc
; i
++)
3407 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "argv[%d] = \"%s\"", i
, argv
[i
]);
3409 envp
[envc
++] = "REQUEST_METHOD=GET";
3414 * Add GET form variables after ?...
3417 envp
[envc
++] = query_string
;
3422 sprintf(content_length
, "CONTENT_LENGTH=" CUPS_LLFMT
, con
->bytes
);
3423 snprintf(content_type
, sizeof(content_type
), "CONTENT_TYPE=%s",
3424 con
->http
.fields
[HTTP_FIELD_CONTENT_TYPE
]);
3426 envp
[envc
++] = "REQUEST_METHOD=POST";
3427 envp
[envc
++] = content_length
;
3428 envp
[envc
++] = content_type
;
3432 * Tell the CGI if we are using encryption...
3435 if (con
->http
.encryption
== HTTP_ENCRYPT_ALWAYS
)
3436 envp
[envc
++] = "HTTPS=ON";
3439 * Terminate the environment array...
3444 if (LogLevel
== CUPSD_LOG_DEBUG2
)
3446 for (i
= 0; i
< argc
; i
++)
3447 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3448 "pipe_command: argv[%d] = \"%s\"", i
, argv
[i
]);
3449 for (i
= 0; i
< envc
; i
++)
3450 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3451 "pipe_command: envp[%d] = \"%s\"", i
, envp
[i
]);
3455 * Create a pipe for the output...
3458 if (cupsdOpenPipe(fds
))
3460 cupsdClearString(&query_string
);
3462 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to create pipes for CGI %s - %s",
3463 argv
[0], strerror(errno
));
3468 * Then execute the command...
3471 if (cupsdStartProcess(command
, argv
, envp
, infile
, fds
[1], CGIPipes
[1],
3472 -1, root
, &pid
) < 0)
3475 * Error - can't fork!
3478 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to fork for CGI %s - %s", argv
[0],
3481 cupsdClosePipe(fds
);
3487 * Fork successful - return the PID...
3490 if (con
->username
[0])
3491 cupsdAddCert(pid
, con
->username
);
3493 cupsdLogMessage(CUPSD_LOG_DEBUG
, "CGI %s started - PID = %d", command
, pid
);
3499 cupsdClearString(&query_string
);
3506 * End of "$Id: client.c 5083 2006-02-06 02:57:43Z mike $".