2 * "$Id: client.c 4912 2006-01-10 21:43:56Z 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 i
; /* Looping var */
84 int count
; /* Count of connections on a host */
85 int val
; /* Parameter value */
86 cupsd_client_t
*con
; /* New 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 NumClients = %d",
97 lis
, lis
->fd
, NumClients
);
100 * Make sure we don't have a full set of clients already...
103 if (NumClients
== MaxClients
)
107 * Get a pointer to the next available client...
110 con
= Clients
+ NumClients
;
112 memset(con
, 0, sizeof(cupsd_client_t
));
113 con
->http
.activity
= time(NULL
);
115 con
->http
.hostaddr
= &(con
->clientaddr
);
118 * Accept the client and get the remote address...
121 addrlen
= sizeof(http_addr_t
);
123 if ((con
->http
.fd
= accept(lis
->fd
, (struct sockaddr
*)con
->http
.hostaddr
,
126 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to accept client connection - %s.",
132 if (lis
->address
.addr
.sa_family
== AF_INET6
)
135 * Save the connected port number...
138 con
->http
.hostaddr
->ipv6
.sin6_port
= lis
->address
.ipv6
.sin6_port
;
141 * Convert IPv4 over IPv6 addresses (::ffff:n.n.n.n) to IPv4 forms we
142 * can more easily use...
145 if (con
->http
.hostaddr
->ipv6
.sin6_addr
.s6_addr32
[0] == 0 &&
146 con
->http
.hostaddr
->ipv6
.sin6_addr
.s6_addr32
[1] == 0 &&
147 ntohl(con
->http
.hostaddr
->ipv6
.sin6_addr
.s6_addr32
[2]) == 0xffff)
148 con
->http
.hostaddr
->ipv6
.sin6_addr
.s6_addr32
[2] = 0;
151 #endif /* AF_INET6 */
152 if (lis
->address
.addr
.sa_family
== AF_INET
)
153 con
->http
.hostaddr
->ipv4
.sin_port
= lis
->address
.ipv4
.sin_port
;
156 * Check the number of clients on the same address...
159 for (i
= 0, count
= 0; i
< NumClients
; i
++)
160 if (httpAddrEqual(Clients
[i
].http
.hostaddr
, con
->http
.hostaddr
))
163 if (count
>= MaxClientsPerHost
)
167 if (count
>= MaxClientsPerHost
)
169 if ((time(NULL
) - last_dos
) >= 60)
171 last_dos
= time(NULL
);
172 cupsdLogMessage(CUPSD_LOG_WARN
,
173 "Possible DoS attack - more than %d clients connecting from %s!",
174 MaxClientsPerHost
, Clients
[i
].http
.hostname
);
178 closesocket(con
->http
.fd
);
187 * Get the hostname or format the IP address as needed...
190 if (httpAddrLocalhost(con
->http
.hostaddr
))
193 * Map accesses from the loopback interface to "localhost"...
196 strlcpy(con
->http
.hostname
, "localhost", sizeof(con
->http
.hostname
));
197 hostname
= con
->http
.hostname
;
202 * Map accesses from the same host to the server name.
205 for (addr
= ServerAddrs
; addr
; addr
= addr
->next
)
206 if (httpAddrEqual(con
->http
.hostaddr
, &(addr
->addr
)))
211 strlcpy(con
->http
.hostname
, ServerName
, sizeof(con
->http
.hostname
));
212 hostname
= con
->http
.hostname
;
214 else if (HostNameLookups
)
215 hostname
= httpAddrLookup(con
->http
.hostaddr
, con
->http
.hostname
,
216 sizeof(con
->http
.hostname
));
220 httpAddrString(con
->http
.hostaddr
, con
->http
.hostname
,
221 sizeof(con
->http
.hostname
));
225 if (hostname
== NULL
&& HostNameLookups
== 2)
228 * Can't have an unresolved IP address with double-lookups enabled...
231 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
232 "cupsdAcceptClient: Closing connection %d...",
236 closesocket(con
->http
.fd
);
241 cupsdLogMessage(CUPSD_LOG_WARN
,
242 "Name lookup failed - connection from %s closed!",
247 if (HostNameLookups
== 2)
250 * Do double lookups as needed...
253 if ((addrlist
= httpAddrGetList(con
->http
.hostname
, AF_UNSPEC
, NULL
)) != NULL
)
256 * See if the hostname maps to the same IP address...
259 for (addr
= addrlist
; addr
; addr
= addr
->next
)
260 if (httpAddrEqual(con
->http
.hostaddr
, &(addr
->addr
)))
266 httpAddrFreeList(addrlist
);
271 * Can't have a hostname that doesn't resolve to the same IP address
272 * with double-lookups enabled...
275 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
276 "cupsdAcceptClient: Closing connection %d...",
280 closesocket(con
->http
.fd
);
285 cupsdLogMessage(CUPSD_LOG_WARN
,
286 "IP lookup failed - connection from %s closed!",
293 if (con
->http
.hostaddr
->addr
.sa_family
== AF_INET6
)
294 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdAcceptClient: %d from %s:%d (IPv6)",
295 con
->http
.fd
, con
->http
.hostname
,
296 ntohs(con
->http
.hostaddr
->ipv6
.sin6_port
));
298 #endif /* AF_INET6 */
300 if (con
->http
.hostaddr
->addr
.sa_family
== AF_LOCAL
)
301 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdAcceptClient: %d from %s (Domain)",
302 con
->http
.fd
, con
->http
.hostname
);
304 #endif /* AF_LOCAL */
305 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdAcceptClient: %d from %s:%d (IPv4)",
306 con
->http
.fd
, con
->http
.hostname
,
307 ntohs(con
->http
.hostaddr
->ipv4
.sin_port
));
310 * Get the local address the client connected to...
313 addrlen
= sizeof(temp
);
314 if (getsockname(con
->http
.fd
, (struct sockaddr
*)&temp
, &addrlen
))
316 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to get local address - %s",
319 strcpy(con
->servername
, "localhost");
320 con
->serverport
= LocalPort
;
325 if (temp
.addr
.sa_family
== AF_INET6
)
327 httpAddrLookup(&temp
, con
->servername
, sizeof(con
->servername
));
328 con
->serverport
= ntohs(lis
->address
.ipv6
.sin6_port
);
331 #endif /* AF_INET6 */
332 if (temp
.addr
.sa_family
== AF_INET
)
334 httpAddrLookup(&temp
, con
->servername
, sizeof(con
->servername
));
335 con
->serverport
= ntohs(lis
->address
.ipv4
.sin_port
);
339 strcpy(con
->servername
, "localhost");
340 con
->serverport
= LocalPort
;
344 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
345 "cupsdAcceptClient: %d connected to server on %s:%d",
346 con
->http
.fd
, con
->servername
, con
->serverport
);
349 * Using TCP_NODELAY improves responsiveness, especially on systems
350 * with a slow loopback interface... Since we write large buffers
351 * when sending print files and requests, there shouldn't be any
352 * performance penalty for this...
356 setsockopt(con
->http
.fd
, IPPROTO_TCP
, TCP_NODELAY
, &val
, sizeof(val
));
359 * Close this file on all execs...
362 fcntl(con
->http
.fd
, F_SETFD
, fcntl(con
->http
.fd
, F_GETFD
) | FD_CLOEXEC
);
365 * Add the socket to the select() input mask.
368 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
369 "cupsdAcceptClient: Adding fd %d to InputSet...",
371 FD_SET(con
->http
.fd
, InputSet
);
376 * Temporarily suspend accept()'s until we lose a client...
379 if (NumClients
== MaxClients
)
380 cupsdPauseListening();
384 * See if we are connecting on a secure port...
387 if (lis
->encryption
== HTTP_ENCRYPT_ALWAYS
)
390 * https connection; go secure...
393 con
->http
.encryption
= HTTP_ENCRYPT_ALWAYS
;
395 cupsdEncryptClient(con
);
399 #endif /* HAVE_SSL */
404 * 'cupsdCloseAllClients()' - Close all remote clients immediately.
408 cupsdCloseAllClients(void)
410 while (NumClients
> 0)
411 cupsdCloseClient(Clients
);
416 * 'cupsdCloseClient()' - Close a remote client.
419 int /* O - 1 if partial close, 0 if fully closed */
420 cupsdCloseClient(cupsd_client_t
*con
) /* I - Client to close */
422 int partial
; /* Do partial close for SSL? */
424 SSL_CTX
*context
; /* Context for encryption */
425 SSL
*conn
; /* Connection for encryption */
426 unsigned long error
; /* Error code */
427 #elif defined(HAVE_GNUTLS)
428 http_tls_t
*conn
; /* TLS connection information */
429 int error
; /* Error code */
430 gnutls_certificate_server_credentials
*credentials
;
431 /* TLS credentials */
432 #endif /* HAVE_LIBSSL */
435 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdCloseClient: %d", con
->http
.fd
);
438 * Flush pending writes before closing...
441 httpFlushWrite(HTTP(con
));
447 * Shutdown encryption as needed...
455 conn
= (SSL
*)(con
->http
.tls
);
456 context
= SSL_get_SSL_CTX(conn
);
458 switch (SSL_shutdown(conn
))
461 cupsdLogMessage(CUPSD_LOG_INFO
,
462 "cupsdCloseClient: SSL shutdown successful!");
465 cupsdLogMessage(CUPSD_LOG_ERROR
,
466 "cupsdCloseClient: Fatal error during SSL shutdown!");
468 while ((error
= ERR_get_error()) != 0)
469 cupsdLogMessage(CUPSD_LOG_ERROR
, "cupsdCloseClient: %s",
470 ERR_error_string(error
, NULL
));
474 SSL_CTX_free(context
);
477 # elif defined(HAVE_GNUTLS)
478 conn
= (http_tls_t
*)(con
->http
.tls
);
479 credentials
= (gnutls_certificate_server_credentials
*)(conn
->credentials
);
481 error
= gnutls_bye(conn
->session
, GNUTLS_SHUT_WR
);
484 case GNUTLS_E_SUCCESS
:
485 cupsdLogMessage(CUPSD_LOG_INFO
,
486 "cupsdCloseClient: SSL shutdown successful!");
489 cupsdLogMessage(CUPSD_LOG_ERROR
,
490 "cupsdCloseClient: %s", gnutls_strerror(error
));
494 gnutls_deinit(conn
->session
);
495 gnutls_certificate_free_credentials(*credentials
);
499 # elif defined(HAVE_CDSASSL)
500 SSLClose((SSLContextRef
)con
->http
.tls
);
501 SSLDisposeContext((SSLContextRef
)con
->http
.tls
);
502 # endif /* HAVE_LIBSSL */
504 con
->http
.tls
= NULL
;
506 #endif /* HAVE_SSL */
508 if (con
->pipe_pid
!= 0)
511 * Stop any CGI process...
514 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
515 "cupsdCloseClient: %d Killing process ID %d...",
516 con
->http
.fd
, con
->pipe_pid
);
517 cupsdEndProcess(con
->pipe_pid
, 1);
523 if (FD_ISSET(con
->file
, InputSet
))
525 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
526 "cupsdCloseClient: %d Removing fd %d from InputSet...",
527 con
->http
.fd
, con
->file
);
528 FD_CLR(con
->file
, InputSet
);
531 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
532 "cupsdCloseClient: %d Closing data file %d.",
533 con
->http
.fd
, con
->file
);
540 * Close the socket and clear the file from the input set for select()...
543 if (con
->http
.fd
> 0)
548 * Only do a partial close so that the encrypted client gets everything.
551 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
552 "cupsdCloseClient: Removing fd %d from OutputSet...",
554 shutdown(con
->http
.fd
, 0);
555 FD_CLR(con
->http
.fd
, OutputSet
);
560 * Shut the socket down fully...
563 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
564 "cupsdCloseClient: Removing fd %d from InputSet and OutputSet...",
567 FD_CLR(con
->http
.fd
, InputSet
);
568 FD_CLR(con
->http
.fd
, OutputSet
);
579 if (con
->http
.input_set
)
580 free(con
->http
.input_set
);
582 httpClearCookie(HTTP(con
));
584 cupsdClearString(&con
->filename
);
585 cupsdClearString(&con
->command
);
586 cupsdClearString(&con
->options
);
590 ippDelete(con
->request
);
596 ippDelete(con
->response
);
597 con
->response
= NULL
;
602 cupsLangFree(con
->language
);
603 con
->language
= NULL
;
607 * Re-enable new client connections if we are going back under the
611 if (NumClients
== MaxClients
)
612 cupsdResumeListening();
615 * Compact the list of clients as necessary...
620 if (con
< (Clients
+ NumClients
))
621 memmove(con
, con
+ 1, (Clients
+ NumClients
- con
) * sizeof(cupsd_client_t
));
629 * 'cupsdEncryptClient()' - Enable encryption for the client...
632 int /* O - 1 on success, 0 on error */
633 cupsdEncryptClient(cupsd_client_t
*con
) /* I - Client to encrypt */
636 SSL_CTX
*context
; /* Context for encryption */
637 SSL
*conn
; /* Connection for encryption */
638 unsigned long error
; /* Error code */
642 * Create the SSL context and accept the connection...
645 context
= SSL_CTX_new(SSLv23_server_method());
647 SSL_CTX_set_options(context
, SSL_OP_NO_SSLv2
); /* Only use SSLv3 or TLS */
648 SSL_CTX_use_PrivateKey_file(context
, ServerKey
, SSL_FILETYPE_PEM
);
649 SSL_CTX_use_certificate_file(context
, ServerCertificate
, SSL_FILETYPE_PEM
);
651 conn
= SSL_new(context
);
653 SSL_set_fd(conn
, con
->http
.fd
);
654 if (SSL_accept(conn
) != 1)
656 cupsdLogMessage(CUPSD_LOG_ERROR
,
657 "cupsdEncryptClient: Unable to encrypt connection from %s!",
660 while ((error
= ERR_get_error()) != 0)
661 cupsdLogMessage(CUPSD_LOG_ERROR
, "cupsdEncryptClient: %s",
662 ERR_error_string(error
, NULL
));
664 SSL_CTX_free(context
);
669 cupsdLogMessage(CUPSD_LOG_DEBUG
,
670 "cupsdEncryptClient: %d Connection from %s now encrypted.",
671 con
->http
.fd
, con
->http
.hostname
);
673 con
->http
.tls
= conn
;
676 #elif defined(HAVE_GNUTLS)
677 http_tls_t
*conn
; /* TLS session object */
678 int error
; /* Error code */
679 gnutls_certificate_server_credentials
*credentials
;
680 /* TLS credentials */
683 * Create the SSL object and perform the SSL handshake...
686 conn
= (http_tls_t
*)malloc(sizeof(gnutls_session
));
691 credentials
= (gnutls_certificate_server_credentials
*)
692 malloc(sizeof(gnutls_certificate_server_credentials
));
693 if (credentials
== NULL
)
695 cupsdLogMessage(CUPSD_LOG_ERROR
,
696 "cupsdEncryptClient: Unable to encrypt connection from %s!",
698 cupsdLogMessage(CUPSD_LOG_ERROR
, "cupsdEncryptClient: %s", strerror(errno
));
704 gnutls_certificate_allocate_credentials(credentials
);
705 gnutls_certificate_set_x509_key_file(*credentials
, ServerCertificate
,
706 ServerKey
, GNUTLS_X509_FMT_PEM
);
708 gnutls_init(&(conn
->session
), GNUTLS_SERVER
);
709 gnutls_set_default_priority(conn
->session
);
710 gnutls_credentials_set(conn
->session
, GNUTLS_CRD_CERTIFICATE
, *credentials
);
711 gnutls_transport_set_ptr(conn
->session
, con
->http
.fd
);
713 error
= gnutls_handshake(conn
->session
);
715 if (error
!= GNUTLS_E_SUCCESS
)
717 cupsdLogMessage(CUPSD_LOG_ERROR
,
718 "cupsdEncryptClient: Unable to encrypt connection from %s!",
720 cupsdLogMessage(CUPSD_LOG_ERROR
, "cupsdEncryptClient: %s",
721 gnutls_strerror(error
));
723 gnutls_deinit(conn
->session
);
724 gnutls_certificate_free_credentials(*credentials
);
730 cupsdLogMessage(CUPSD_LOG_DEBUG
,
731 "cupsdEncryptClient: %d Connection from %s now encrypted.",
732 con
->http
.fd
, con
->http
.hostname
);
734 conn
->credentials
= credentials
;
735 con
->http
.tls
= conn
;
738 #elif defined(HAVE_CDSASSL)
739 OSStatus error
; /* Error info */
740 SSLContextRef conn
; /* New connection */
741 int allowExpired
; /* Allow expired certificates? */
742 int allowAnyRoot
; /* Allow any root certificate? */
746 error
= SSLNewContext(true, &conn
);
750 if (!ServerCertificatesArray
)
751 ServerCertificatesArray
= get_cdsa_server_certs();
753 if (!ServerCertificatesArray
)
755 cupsdLogMessage(CUPSD_LOG_ERROR
,
756 "EncryptClient: Could not find signing key in keychain "
757 "\"%s\"", ServerCertificate
);
758 error
= errSSLBadConfiguration
;
762 error
= SSLSetIOFuncs(conn
, _httpReadCDSA
, _httpWriteCDSA
);
765 error
= SSLSetProtocolVersion(conn
, kSSLProtocol3
);
768 error
= SSLSetConnection(conn
, (SSLConnectionRef
)con
->http
.fd
);
771 error
= SSLSetPeerDomainName(conn
, ServerName
, strlen(ServerName
) + 1);
773 /* have to do these options before setting server certs */
774 if (!error
&& allowExpired
)
775 error
= SSLSetAllowsExpiredCerts(conn
, true);
777 if (!error
&& allowAnyRoot
)
778 error
= SSLSetAllowsAnyRoot(conn
, true);
780 if (!error
&& ServerCertificatesArray
)
781 error
= SSLSetCertificate(conn
, ServerCertificatesArray
);
784 * Perform SSL/TLS handshake
789 error
= SSLHandshake(conn
);
791 while (error
== errSSLWouldBlock
);
795 cupsdLogMessage(CUPSD_LOG_ERROR
,
796 "cupsdEncryptClient: Unable to encrypt connection from %s!",
799 cupsdLogMessage(CUPSD_LOG_ERROR
,
800 "cupsdEncryptClient: CDSA error code is %d", (int)error
);
802 con
->http
.error
= error
;
803 con
->http
.status
= HTTP_ERROR
;
806 SSLDisposeContext(conn
);
811 cupsdLogMessage(CUPSD_LOG_DEBUG
,
812 "cupsdEncryptClient: %d Connection from %s now encrypted.",
813 con
->http
.fd
, con
->http
.hostname
);
815 con
->http
.tls
= conn
;
820 #endif /* HAVE_LIBSSL */
825 * 'cupsdIsCGI()' - Is the resource a CGI script/program?
828 int /* O - 1 = CGI, 0 = file */
829 cupsdIsCGI(cupsd_client_t
*con
, /* I - Client connection */
830 const char *filename
, /* I - Real filename */
831 struct stat
*filestats
, /* I - File information */
832 mime_type_t
*type
) /* I - MIME type */
834 const char *options
; /* Options on URL */
837 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
838 "cupsdIsCGI(con=%p, filename=\"%s\", filestats=%p, type=%s/%s)\n",
839 con
, filename
, filestats
, type
? type
->super
: "unknown",
840 type
? type
->type
: "unknown");
843 * Get the options, if any...
846 if ((options
= strchr(con
->uri
, '?')) != NULL
)
850 * Check for known types...
853 if (!type
|| strcasecmp(type
->super
, "application"))
855 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdIsCGI: Returning 0...");
859 if (!strcasecmp(type
->type
, "x-httpd-cgi") &&
860 (filestats
->st_mode
& 0111))
863 * "application/x-httpd-cgi" is a CGI script.
866 cupsdSetString(&con
->command
, filename
);
868 filename
= strrchr(filename
, '/') + 1; /* Filename always absolute */
871 cupsdSetStringf(&con
->options
, "%s %s", filename
, options
);
873 cupsdSetStringf(&con
->options
, "%s", filename
);
875 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
876 "cupsdIsCGI: Returning 1 with command=\"%s\" and options=\"%s\"",
877 con
->command
, con
->options
);
882 else if (!strcasecmp(type
->type
, "x-httpd-java"))
885 * "application/x-httpd-java" is a Java servlet.
888 cupsdSetString(&con
->command
, CUPS_JAVA
);
891 cupsdSetStringf(&con
->options
, "java %s %s", filename
, options
);
893 cupsdSetStringf(&con
->options
, "java %s", filename
);
895 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
896 "cupsdIsCGI: Returning 1 with command=\"%s\" and options=\"%s\"",
897 con
->command
, con
->options
);
901 #endif /* HAVE_JAVA */
903 else if (!strcasecmp(type
->type
, "x-httpd-perl"))
906 * "application/x-httpd-perl" is a Perl page.
909 cupsdSetString(&con
->command
, CUPS_PERL
);
912 cupsdSetStringf(&con
->options
, "perl %s %s", filename
, options
);
914 cupsdSetStringf(&con
->options
, "perl %s", filename
);
916 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
917 "cupsdIsCGI: Returning 1 with command=\"%s\" and options=\"%s\"",
918 con
->command
, con
->options
);
922 #endif /* HAVE_PERL */
924 else if (!strcasecmp(type
->type
, "x-httpd-php"))
927 * "application/x-httpd-php" is a PHP page.
930 cupsdSetString(&con
->command
, CUPS_PHP
);
933 cupsdSetStringf(&con
->options
, "php %s %s", filename
, options
);
935 cupsdSetStringf(&con
->options
, "php %s", filename
);
937 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
938 "cupsdIsCGI: Returning 1 with command=\"%s\" and options=\"%s\"",
939 con
->command
, con
->options
);
943 #endif /* HAVE_PHP */
945 else if (!strcasecmp(type
->type
, "x-httpd-python"))
948 * "application/x-httpd-python" is a Python page.
951 cupsdSetString(&con
->command
, CUPS_PYTHON
);
954 cupsdSetStringf(&con
->options
, "python %s %s", filename
, options
);
956 cupsdSetStringf(&con
->options
, "python %s", filename
);
958 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
959 "cupsdIsCGI: Returning 1 with command=\"%s\" and options=\"%s\"",
960 con
->command
, con
->options
);
964 #endif /* HAVE_PYTHON */
966 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdIsCGI: Returning 0...");
973 * 'cupsdReadClient()' - Read data from a client.
976 int /* O - 1 on success, 0 on error */
977 cupsdReadClient(cupsd_client_t
*con
) /* I - Client to read from */
979 char line
[32768], /* Line from client... */
980 operation
[64], /* Operation code from socket */
981 version
[64], /* HTTP version number string */
982 locale
[64], /* Locale */
983 *ptr
; /* Pointer into strings */
984 int major
, minor
; /* HTTP version numbers */
985 http_status_t status
; /* Transfer status */
986 ipp_state_t ipp_state
; /* State of IPP transfer */
987 int bytes
; /* Number of bytes to POST */
988 char *filename
; /* Name of file for GET/HEAD */
989 char buf
[1024]; /* Buffer for real filename */
990 struct stat filestats
; /* File information */
991 mime_type_t
*type
; /* MIME type of file */
992 cupsd_printer_t
*p
; /* Printer */
993 static unsigned request_id
= 0; /* Request ID for temp files */
996 status
= HTTP_CONTINUE
;
998 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
999 "cupsdReadClient: %d, used=%d, file=%d state=%d",
1000 con
->http
.fd
, con
->http
.used
, con
->file
, con
->http
.state
);
1002 if (con
->http
.error
)
1004 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdReadClient: http error seen...");
1005 return (cupsdCloseClient(con
));
1012 * Automatically check for a SSL/TLS handshake...
1017 if (recv(con
->http
.fd
, buf
, 1, MSG_PEEK
) == 1 &&
1018 (!buf
[0] || !strchr("DGHOPT", buf
[0])))
1021 * Encrypt this connection...
1024 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1025 "cupsdReadClient: Saw first byte %02X, auto-negotiating SSL/TLS session...",
1028 cupsdEncryptClient(con
);
1032 #endif /* HAVE_SSL */
1034 switch (con
->http
.state
)
1038 * See if we've received a request line...
1041 if (httpGets(line
, sizeof(line
) - 1, HTTP(con
)) == NULL
)
1043 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1044 "cupsdReadClient: httpGets returned EOF...");
1045 return (cupsdCloseClient(con
));
1049 * Ignore blank request lines...
1052 if (line
[0] == '\0')
1056 * Clear other state variables...
1059 httpClearFields(HTTP(con
));
1061 con
->http
.activity
= time(NULL
);
1062 con
->http
.version
= HTTP_1_0
;
1063 con
->http
.keep_alive
= HTTP_KEEPALIVE_OFF
;
1064 con
->http
.data_encoding
= HTTP_ENCODE_LENGTH
;
1065 con
->http
.data_remaining
= 0;
1066 con
->http
._data_remaining
= 0;
1067 con
->operation
= HTTP_WAITING
;
1070 con
->file_ready
= 0;
1072 con
->username
[0] = '\0';
1073 con
->password
[0] = '\0';
1076 cupsdClearString(&con
->command
);
1077 cupsdClearString(&con
->options
);
1079 if (con
->language
!= NULL
)
1081 cupsLangFree(con
->language
);
1082 con
->language
= NULL
;
1086 * Grab the request line...
1089 switch (sscanf(line
, "%63s%1023s%63s", operation
, con
->uri
, version
))
1092 cupsdLogMessage(CUPSD_LOG_ERROR
,
1093 "Bad request line \"%s\" from %s!", line
,
1094 con
->http
.hostname
);
1095 cupsdSendError(con
, HTTP_BAD_REQUEST
);
1096 return (cupsdCloseClient(con
));
1098 con
->http
.version
= HTTP_0_9
;
1101 if (sscanf(version
, "HTTP/%d.%d", &major
, &minor
) != 2)
1103 cupsdLogMessage(CUPSD_LOG_ERROR
,
1104 "Bad request line \"%s\" from %s!", line
,
1105 con
->http
.hostname
);
1106 cupsdSendError(con
, HTTP_BAD_REQUEST
);
1107 return (cupsdCloseClient(con
));
1112 con
->http
.version
= (http_version_t
)(major
* 100 + minor
);
1113 if (con
->http
.version
== HTTP_1_1
&& KeepAlive
)
1114 con
->http
.keep_alive
= HTTP_KEEPALIVE_ON
;
1116 con
->http
.keep_alive
= HTTP_KEEPALIVE_OFF
;
1120 cupsdSendError(con
, HTTP_NOT_SUPPORTED
);
1121 return (cupsdCloseClient(con
));
1127 * Handle full URLs in the request line...
1130 if (strcmp(con
->uri
, "*"))
1132 char method
[HTTP_MAX_URI
], /* Method/scheme */
1133 userpass
[HTTP_MAX_URI
], /* Username:password */
1134 hostname
[HTTP_MAX_URI
], /* Hostname */
1135 resource
[HTTP_MAX_URI
]; /* Resource path */
1136 int port
; /* Port number */
1140 * Separate the URI into its components...
1143 httpSeparateURI(con
->uri
, method
, sizeof(method
),
1144 userpass
, sizeof(userpass
),
1145 hostname
, sizeof(hostname
), &port
,
1146 resource
, sizeof(resource
));
1149 * Only allow URIs with the servername, localhost, or an IP
1153 if (strcmp(method
, "file") &&
1154 strcasecmp(hostname
, ServerName
) &&
1155 strcasecmp(hostname
, "localhost") &&
1156 !isdigit(hostname
[0]))
1159 * Nope, we don't do proxies...
1162 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad URI \"%s\" in request!",
1164 cupsdSendError(con
, HTTP_METHOD_NOT_ALLOWED
);
1165 return (cupsdCloseClient(con
));
1169 * Copy the resource portion back into the URI; both resource and
1170 * con->uri are HTTP_MAX_URI bytes in size...
1173 strcpy(con
->uri
, resource
);
1177 * Process the request...
1180 if (!strcmp(operation
, "GET"))
1181 con
->http
.state
= HTTP_GET
;
1182 else if (!strcmp(operation
, "PUT"))
1183 con
->http
.state
= HTTP_PUT
;
1184 else if (!strcmp(operation
, "POST"))
1185 con
->http
.state
= HTTP_POST
;
1186 else if (!strcmp(operation
, "DELETE"))
1187 con
->http
.state
= HTTP_DELETE
;
1188 else if (!strcmp(operation
, "TRACE"))
1189 con
->http
.state
= HTTP_TRACE
;
1190 else if (!strcmp(operation
, "OPTIONS"))
1191 con
->http
.state
= HTTP_OPTIONS
;
1192 else if (!strcmp(operation
, "HEAD"))
1193 con
->http
.state
= HTTP_HEAD
;
1196 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad operation \"%s\"!", operation
);
1197 cupsdSendError(con
, HTTP_BAD_REQUEST
);
1198 return (cupsdCloseClient(con
));
1201 con
->start
= time(NULL
);
1202 con
->operation
= con
->http
.state
;
1204 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdReadClient: %d %s %s HTTP/%d.%d", con
->http
.fd
,
1205 operation
, con
->uri
,
1206 con
->http
.version
/ 100, con
->http
.version
% 100);
1208 con
->http
.status
= HTTP_OK
;
1218 * Parse incoming parameters until the status changes...
1221 status
= httpUpdate(HTTP(con
));
1223 if (status
!= HTTP_OK
&& status
!= HTTP_CONTINUE
)
1225 cupsdSendError(con
, HTTP_BAD_REQUEST
);
1226 return (cupsdCloseClient(con
));
1231 break; /* Anti-compiler-warning-code */
1235 * Handle new transfers...
1238 if (status
== HTTP_OK
)
1240 if (con
->http
.fields
[HTTP_FIELD_ACCEPT_LANGUAGE
][0])
1243 * Figure out the locale from the Accept-Language and Content-Type
1247 if ((ptr
= strchr(con
->http
.fields
[HTTP_FIELD_ACCEPT_LANGUAGE
], ',')) != NULL
)
1250 if ((ptr
= strchr(con
->http
.fields
[HTTP_FIELD_ACCEPT_LANGUAGE
], ';')) != NULL
)
1253 if ((ptr
= strstr(con
->http
.fields
[HTTP_FIELD_CONTENT_TYPE
], "charset=")) != NULL
)
1256 * Combine language and charset, and trim any extra params in the
1260 snprintf(locale
, sizeof(locale
), "%s.%s",
1261 con
->http
.fields
[HTTP_FIELD_ACCEPT_LANGUAGE
], ptr
+ 8);
1263 if ((ptr
= strchr(locale
, ',')) != NULL
)
1267 snprintf(locale
, sizeof(locale
), "%s.%s",
1268 con
->http
.fields
[HTTP_FIELD_ACCEPT_LANGUAGE
], DefaultCharset
);
1270 con
->language
= cupsLangGet(locale
);
1273 con
->language
= cupsLangGet(DefaultLocale
);
1275 cupsdAuthorize(con
);
1277 if (!strncmp(con
->http
.fields
[HTTP_FIELD_CONNECTION
], "Keep-Alive", 10) &&
1279 con
->http
.keep_alive
= HTTP_KEEPALIVE_ON
;
1281 if (!con
->http
.fields
[HTTP_FIELD_HOST
][0] &&
1282 con
->http
.version
>= HTTP_1_1
)
1285 * HTTP/1.1 and higher require the "Host:" field...
1288 if (!cupsdSendError(con
, HTTP_BAD_REQUEST
))
1289 return (cupsdCloseClient(con
));
1291 else if (con
->operation
== HTTP_OPTIONS
)
1294 * Do OPTIONS command...
1297 if (con
->best
&& con
->best
->type
!= AUTH_NONE
)
1299 if (!cupsdSendHeader(con
, HTTP_UNAUTHORIZED
, NULL
))
1300 return (cupsdCloseClient(con
));
1303 if (!strcasecmp(con
->http
.fields
[HTTP_FIELD_CONNECTION
], "Upgrade") &&
1304 con
->http
.tls
== NULL
)
1308 * Do encryption stuff...
1311 if (!cupsdSendHeader(con
, HTTP_SWITCHING_PROTOCOLS
, NULL
))
1312 return (cupsdCloseClient(con
));
1314 httpPrintf(HTTP(con
), "Connection: Upgrade\r\n");
1315 httpPrintf(HTTP(con
), "Upgrade: TLS/1.0,HTTP/1.1\r\n");
1316 httpPrintf(HTTP(con
), "Content-Length: 0\r\n");
1317 httpPrintf(HTTP(con
), "\r\n");
1319 cupsdEncryptClient(con
);
1321 if (!cupsdSendError(con
, HTTP_NOT_IMPLEMENTED
))
1322 return (cupsdCloseClient(con
));
1323 #endif /* HAVE_SSL */
1326 if (con
->http
.expect
)
1328 /**** TODO: send expected header ****/
1331 if (!cupsdSendHeader(con
, HTTP_OK
, NULL
))
1332 return (cupsdCloseClient(con
));
1334 httpPrintf(HTTP(con
), "Allow: GET, HEAD, OPTIONS, POST, PUT\r\n");
1335 httpPrintf(HTTP(con
), "Content-Length: 0\r\n");
1336 httpPrintf(HTTP(con
), "\r\n");
1338 else if (!is_path_absolute(con
->uri
))
1341 * Protect against malicious users!
1344 if (!cupsdSendError(con
, HTTP_FORBIDDEN
))
1345 return (cupsdCloseClient(con
));
1349 if (!strcasecmp(con
->http
.fields
[HTTP_FIELD_CONNECTION
], "Upgrade") &&
1350 con
->http
.tls
== NULL
)
1354 * Do encryption stuff...
1357 if (!cupsdSendHeader(con
, HTTP_SWITCHING_PROTOCOLS
, NULL
))
1358 return (cupsdCloseClient(con
));
1360 httpPrintf(HTTP(con
), "Connection: Upgrade\r\n");
1361 httpPrintf(HTTP(con
), "Upgrade: TLS/1.0,HTTP/1.1\r\n");
1362 httpPrintf(HTTP(con
), "Content-Length: 0\r\n");
1363 httpPrintf(HTTP(con
), "\r\n");
1365 cupsdEncryptClient(con
);
1367 if (!cupsdSendError(con
, HTTP_NOT_IMPLEMENTED
))
1368 return (cupsdCloseClient(con
));
1369 #endif /* HAVE_SSL */
1372 if ((status
= cupsdIsAuthorized(con
, NULL
)) != HTTP_OK
)
1374 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1375 "cupsdReadClient: Unauthorized request for %s...\n",
1377 cupsdSendError(con
, status
);
1378 return (cupsdCloseClient(con
));
1381 if (con
->http
.expect
)
1383 /**** TODO: send expected header ****/
1386 switch (con
->http
.state
)
1388 case HTTP_GET_SEND
:
1389 if (!strncmp(con
->uri
, "/printers/", 10) &&
1390 !strcmp(con
->uri
+ strlen(con
->uri
) - 4, ".ppd"))
1393 * Send PPD file - get the real printer name since printer
1394 * names are not case sensitive but filenames can be...
1397 con
->uri
[strlen(con
->uri
) - 4] = '\0'; /* Drop ".ppd" */
1399 if ((p
= cupsdFindPrinter(con
->uri
+ 10)) != NULL
)
1400 snprintf(con
->uri
, sizeof(con
->uri
), "/ppd/%s.ppd", p
->name
);
1403 if (!cupsdSendError(con
, HTTP_NOT_FOUND
))
1404 return (cupsdCloseClient(con
));
1410 if ((!strncmp(con
->uri
, "/admin", 6) &&
1411 strncmp(con
->uri
, "/admin/conf/", 12) &&
1412 strncmp(con
->uri
, "/admin/log/", 11)) ||
1413 !strncmp(con
->uri
, "/printers", 9) ||
1414 !strncmp(con
->uri
, "/classes", 8) ||
1415 !strncmp(con
->uri
, "/help", 5) ||
1416 !strncmp(con
->uri
, "/jobs", 5))
1419 * Send CGI output...
1422 if (!strncmp(con
->uri
, "/admin", 6))
1424 cupsdSetStringf(&con
->command
, "%s/cgi-bin/admin.cgi",
1427 if ((ptr
= strchr(con
->uri
+ 6, '?')) != NULL
)
1428 cupsdSetStringf(&con
->options
, "admin%s", ptr
);
1430 cupsdSetString(&con
->options
, "admin");
1432 else if (!strncmp(con
->uri
, "/printers", 9))
1434 cupsdSetStringf(&con
->command
, "%s/cgi-bin/printers.cgi",
1436 cupsdSetString(&con
->options
, con
->uri
+ 9);
1438 else if (!strncmp(con
->uri
, "/classes", 8))
1440 cupsdSetStringf(&con
->command
, "%s/cgi-bin/classes.cgi",
1442 cupsdSetString(&con
->options
, con
->uri
+ 8);
1444 else if (!strncmp(con
->uri
, "/jobs", 5))
1446 cupsdSetStringf(&con
->command
, "%s/cgi-bin/jobs.cgi",
1448 cupsdSetString(&con
->options
, con
->uri
+ 5);
1452 cupsdSetStringf(&con
->command
, "%s/cgi-bin/help.cgi",
1454 cupsdSetString(&con
->options
, con
->uri
+ 5);
1457 if (con
->options
[0] == '/')
1458 _cups_strcpy(con
->options
, con
->options
+ 1);
1460 if (!cupsdSendCommand(con
, con
->command
, con
->options
, 0))
1462 if (!cupsdSendError(con
, HTTP_NOT_FOUND
))
1463 return (cupsdCloseClient(con
));
1466 cupsdLogRequest(con
, HTTP_OK
);
1468 if (con
->http
.version
<= HTTP_1_0
)
1469 con
->http
.keep_alive
= HTTP_KEEPALIVE_OFF
;
1471 else if ((!strncmp(con
->uri
, "/admin/conf/", 12) &&
1472 (strchr(con
->uri
+ 12, '/') ||
1473 strlen(con
->uri
) == 12)) ||
1474 (!strncmp(con
->uri
, "/admin/log/", 11) &&
1475 (strchr(con
->uri
+ 11, '/') ||
1476 strlen(con
->uri
) == 11)))
1479 * GET can only be done to configuration files under
1483 if (!cupsdSendError(con
, HTTP_FORBIDDEN
))
1484 return (cupsdCloseClient(con
));
1494 if ((filename
= get_file(con
, &filestats
, buf
,
1495 sizeof(buf
))) == NULL
)
1497 if (!cupsdSendError(con
, HTTP_NOT_FOUND
))
1498 return (cupsdCloseClient(con
));
1503 type
= mimeFileType(MimeDatabase
, filename
, NULL
);
1505 if (cupsdIsCGI(con
, filename
, &filestats
, type
))
1508 * Note: con->command and con->options were set by
1512 if (!cupsdSendCommand(con
, con
->command
, con
->options
, 0))
1514 if (!cupsdSendError(con
, HTTP_NOT_FOUND
))
1515 return (cupsdCloseClient(con
));
1518 cupsdLogRequest(con
, HTTP_OK
);
1520 if (con
->http
.version
<= HTTP_1_0
)
1521 con
->http
.keep_alive
= HTTP_KEEPALIVE_OFF
;
1525 if (!check_if_modified(con
, &filestats
))
1527 if (!cupsdSendError(con
, HTTP_NOT_MODIFIED
))
1528 return (cupsdCloseClient(con
));
1533 strcpy(line
, "text/plain");
1535 snprintf(line
, sizeof(line
), "%s/%s", type
->super
, type
->type
);
1537 if (!cupsdSendFile(con
, HTTP_OK
, filename
, line
, &filestats
))
1538 return (cupsdCloseClient(con
));
1543 case HTTP_POST_RECV
:
1545 * See if the POST request includes a Content-Length field, and if
1546 * so check the length against any limits that are set...
1549 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "POST %s", con
->uri
);
1550 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "CONTENT_TYPE = %s",
1551 con
->http
.fields
[HTTP_FIELD_CONTENT_TYPE
]);
1553 if (con
->http
.fields
[HTTP_FIELD_CONTENT_LENGTH
][0] &&
1554 MaxRequestSize
> 0 &&
1555 con
->http
.data_remaining
> MaxRequestSize
)
1558 * Request too large...
1561 if (!cupsdSendError(con
, HTTP_REQUEST_TOO_LARGE
))
1562 return (cupsdCloseClient(con
));
1566 else if (con
->http
.data_remaining
< 0)
1569 * Negative content lengths are invalid!
1572 if (!cupsdSendError(con
, HTTP_BAD_REQUEST
))
1573 return (cupsdCloseClient(con
));
1579 * See what kind of POST request this is; for IPP requests the
1580 * content-type field will be "application/ipp"...
1583 if (!strcmp(con
->http
.fields
[HTTP_FIELD_CONTENT_TYPE
],
1585 con
->request
= ippNew();
1586 else if ((!strncmp(con
->uri
, "/admin", 6) &&
1587 strncmp(con
->uri
, "/admin/conf/", 12) &&
1588 strncmp(con
->uri
, "/admin/log/", 11)) ||
1589 !strncmp(con
->uri
, "/printers", 9) ||
1590 !strncmp(con
->uri
, "/classes", 8) ||
1591 !strncmp(con
->uri
, "/help", 5) ||
1592 !strncmp(con
->uri
, "/jobs", 5))
1598 if (!strncmp(con
->uri
, "/admin", 6))
1600 cupsdSetStringf(&con
->command
, "%s/cgi-bin/admin.cgi",
1603 if ((ptr
= strchr(con
->uri
+ 6, '?')) != NULL
)
1604 cupsdSetStringf(&con
->options
, "admin%s", ptr
);
1606 cupsdSetString(&con
->options
, "admin");
1608 else if (!strncmp(con
->uri
, "/printers", 9))
1610 cupsdSetStringf(&con
->command
, "%s/cgi-bin/printers.cgi",
1612 cupsdSetString(&con
->options
, con
->uri
+ 9);
1614 else if (!strncmp(con
->uri
, "/classes", 8))
1616 cupsdSetStringf(&con
->command
, "%s/cgi-bin/classes.cgi",
1618 cupsdSetString(&con
->options
, con
->uri
+ 8);
1620 else if (!strncmp(con
->uri
, "/jobs", 5))
1622 cupsdSetStringf(&con
->command
, "%s/cgi-bin/jobs.cgi",
1624 cupsdSetString(&con
->options
, con
->uri
+ 5);
1628 cupsdSetStringf(&con
->command
, "%s/cgi-bin/help.cgi",
1630 cupsdSetString(&con
->options
, con
->uri
+ 5);
1633 if (con
->options
[0] == '/')
1634 _cups_strcpy(con
->options
, con
->options
+ 1);
1636 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1637 "cupsdReadClient: %d command=\"%s\", options = \"%s\"",
1638 con
->http
.fd
, con
->command
, con
->options
);
1640 if (con
->http
.version
<= HTTP_1_0
)
1641 con
->http
.keep_alive
= HTTP_KEEPALIVE_OFF
;
1649 if ((filename
= get_file(con
, &filestats
, buf
,
1650 sizeof(buf
))) == NULL
)
1652 if (!cupsdSendError(con
, HTTP_NOT_FOUND
))
1653 return (cupsdCloseClient(con
));
1658 type
= mimeFileType(MimeDatabase
, filename
, NULL
);
1660 if (!cupsdIsCGI(con
, filename
, &filestats
, type
))
1663 * Only POST to CGI's...
1666 if (!cupsdSendError(con
, HTTP_UNAUTHORIZED
))
1667 return (cupsdCloseClient(con
));
1672 case HTTP_PUT_RECV
:
1674 * Validate the resource name...
1677 if (strncmp(con
->uri
, "/admin/conf/", 12) ||
1678 strchr(con
->uri
+ 12, '/') ||
1679 strlen(con
->uri
) == 12)
1682 * PUT can only be done to configuration files under
1686 if (!cupsdSendError(con
, HTTP_FORBIDDEN
))
1687 return (cupsdCloseClient(con
));
1693 * See if the PUT request includes a Content-Length field, and if
1694 * so check the length against any limits that are set...
1697 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "PUT %s", con
->uri
);
1698 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "CONTENT_TYPE = %s",
1699 con
->http
.fields
[HTTP_FIELD_CONTENT_TYPE
]);
1701 if (con
->http
.fields
[HTTP_FIELD_CONTENT_LENGTH
][0] &&
1702 MaxRequestSize
> 0 &&
1703 con
->http
.data_remaining
> MaxRequestSize
)
1706 * Request too large...
1709 if (!cupsdSendError(con
, HTTP_REQUEST_TOO_LARGE
))
1710 return (cupsdCloseClient(con
));
1714 else if (con
->http
.data_remaining
< 0)
1717 * Negative content lengths are invalid!
1720 if (!cupsdSendError(con
, HTTP_BAD_REQUEST
))
1721 return (cupsdCloseClient(con
));
1727 * Open a temporary file to hold the request...
1730 cupsdSetStringf(&con
->filename
, "%s/%08x", RequestRoot
,
1732 con
->file
= open(con
->filename
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0640);
1734 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1735 "cupsdReadClient: %d REQUEST %s=%d", con
->http
.fd
,
1736 con
->filename
, con
->file
);
1740 if (!cupsdSendError(con
, HTTP_REQUEST_TOO_LARGE
))
1741 return (cupsdCloseClient(con
));
1744 fchmod(con
->file
, 0640);
1745 fchown(con
->file
, RunUser
, Group
);
1746 fcntl(con
->file
, F_SETFD
, fcntl(con
->file
, F_GETFD
) | FD_CLOEXEC
);
1751 cupsdSendError(con
, HTTP_NOT_IMPLEMENTED
);
1752 return (cupsdCloseClient(con
));
1755 if (!strncmp(con
->uri
, "/printers/", 10) &&
1756 !strcmp(con
->uri
+ strlen(con
->uri
) - 4, ".ppd"))
1759 * Send PPD file - get the real printer name since printer
1760 * names are not case sensitive but filenames can be...
1763 con
->uri
[strlen(con
->uri
) - 4] = '\0'; /* Drop ".ppd" */
1765 if ((p
= cupsdFindPrinter(con
->uri
+ 10)) != NULL
)
1766 snprintf(con
->uri
, sizeof(con
->uri
), "/ppd/%s.ppd", p
->name
);
1769 if (!cupsdSendError(con
, HTTP_NOT_FOUND
))
1770 return (cupsdCloseClient(con
));
1776 if ((!strncmp(con
->uri
, "/admin", 6) &&
1777 strncmp(con
->uri
, "/admin/conf/", 12) &&
1778 strncmp(con
->uri
, "/admin/log/", 11)) ||
1779 !strncmp(con
->uri
, "/printers", 9) ||
1780 !strncmp(con
->uri
, "/classes", 8) ||
1781 !strncmp(con
->uri
, "/help", 5) ||
1782 !strncmp(con
->uri
, "/jobs", 5))
1788 if (!cupsdSendHeader(con
, HTTP_OK
, "text/html"))
1789 return (cupsdCloseClient(con
));
1791 if (httpPrintf(HTTP(con
), "\r\n") < 0)
1792 return (cupsdCloseClient(con
));
1794 cupsdLogRequest(con
, HTTP_OK
);
1796 else if ((!strncmp(con
->uri
, "/admin/conf/", 12) &&
1797 (strchr(con
->uri
+ 12, '/') ||
1798 strlen(con
->uri
) == 12)) ||
1799 (!strncmp(con
->uri
, "/admin/log/", 11) &&
1800 (strchr(con
->uri
+ 11, '/') ||
1801 strlen(con
->uri
) == 11)))
1804 * HEAD can only be done to configuration files under
1808 if (!cupsdSendError(con
, HTTP_FORBIDDEN
))
1809 return (cupsdCloseClient(con
));
1813 else if ((filename
= get_file(con
, &filestats
, buf
,
1814 sizeof(buf
))) == NULL
)
1816 if (!cupsdSendHeader(con
, HTTP_NOT_FOUND
, "text/html"))
1817 return (cupsdCloseClient(con
));
1819 cupsdLogRequest(con
, HTTP_NOT_FOUND
);
1821 else if (!check_if_modified(con
, &filestats
))
1823 if (!cupsdSendError(con
, HTTP_NOT_MODIFIED
))
1824 return (cupsdCloseClient(con
));
1826 cupsdLogRequest(con
, HTTP_NOT_MODIFIED
);
1834 type
= mimeFileType(MimeDatabase
, filename
, NULL
);
1836 strcpy(line
, "text/plain");
1838 snprintf(line
, sizeof(line
), "%s/%s", type
->super
, type
->type
);
1840 if (!cupsdSendHeader(con
, HTTP_OK
, line
))
1841 return (cupsdCloseClient(con
));
1843 if (httpPrintf(HTTP(con
), "Last-Modified: %s\r\n",
1844 httpGetDateString(filestats
.st_mtime
)) < 0)
1845 return (cupsdCloseClient(con
));
1847 if (httpPrintf(HTTP(con
), "Content-Length: %lu\r\n",
1848 (unsigned long)filestats
.st_size
) < 0)
1849 return (cupsdCloseClient(con
));
1851 cupsdLogRequest(con
, HTTP_OK
);
1854 if (httpPrintf(HTTP(con
), "\r\n") < 0)
1855 return (cupsdCloseClient(con
));
1857 con
->http
.state
= HTTP_WAITING
;
1861 break; /* Anti-compiler-warning-code */
1867 * Handle any incoming data...
1870 switch (con
->http
.state
)
1872 case HTTP_PUT_RECV
:
1873 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1874 "cupsdReadClient: %d con->data_encoding=HTTP_ENCODE_%s, "
1875 "con->data_remaining=" CUPS_LLFMT
", con->file=%d",
1877 con
->http
.data_encoding
== HTTP_ENCODE_CHUNKED
?
1878 "CHUNKED" : "LENGTH",
1879 CUPS_LLCAST con
->http
.data_remaining
, con
->file
);
1881 if ((bytes
= httpRead(HTTP(con
), line
, sizeof(line
))) < 0)
1882 return (cupsdCloseClient(con
));
1885 con
->bytes
+= bytes
;
1887 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1888 "cupsdReadClient: %d writing %d bytes to %d",
1889 con
->http
.fd
, bytes
, con
->file
);
1891 if (write(con
->file
, line
, bytes
) < bytes
)
1893 cupsdLogMessage(CUPSD_LOG_ERROR
,
1894 "cupsdReadClient: Unable to write %d bytes to %s: %s",
1895 bytes
, con
->filename
, strerror(errno
));
1897 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1898 "cupsdReadClient: Closing data file %d...",
1903 unlink(con
->filename
);
1904 cupsdClearString(&con
->filename
);
1906 if (!cupsdSendError(con
, HTTP_REQUEST_TOO_LARGE
))
1907 return (cupsdCloseClient(con
));
1911 if (con
->http
.state
== HTTP_WAITING
)
1914 * End of file, see how big it is...
1917 fstat(con
->file
, &filestats
);
1919 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1920 "cupsdReadClient: %d Closing data file %d, size="
1922 con
->http
.fd
, con
->file
,
1923 CUPS_LLCAST filestats
.st_size
);
1928 if (filestats
.st_size
> MaxRequestSize
&&
1932 * Request is too big; remove it and send an error...
1935 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1936 "cupsdReadClient: %d Removing temp file %s",
1937 con
->http
.fd
, con
->filename
);
1938 unlink(con
->filename
);
1939 cupsdClearString(&con
->filename
);
1941 if (!cupsdSendError(con
, HTTP_REQUEST_TOO_LARGE
))
1942 return (cupsdCloseClient(con
));
1946 * Install the configuration file...
1949 status
= install_conf_file(con
);
1952 * Return the status to the client...
1955 if (!cupsdSendError(con
, status
))
1956 return (cupsdCloseClient(con
));
1960 case HTTP_POST_RECV
:
1961 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1962 "cupsdReadClient: %d con->data_encoding=HTTP_ENCODE_"
1963 "%s, con->data_remaining=" CUPS_LLFMT
", con->file=%d",
1965 con
->http
.data_encoding
== HTTP_ENCODE_CHUNKED
?
1966 "CHUNKED" : "LENGTH",
1967 CUPS_LLCAST con
->http
.data_remaining
, con
->file
);
1969 if (con
->request
!= NULL
)
1972 * Grab any request data from the connection...
1975 if ((ipp_state
= ippRead(&(con
->http
), con
->request
)) == IPP_ERROR
)
1977 cupsdLogMessage(CUPSD_LOG_ERROR
,
1978 "cupsdReadClient: %d IPP Read Error!",
1981 cupsdSendError(con
, HTTP_BAD_REQUEST
);
1982 return (cupsdCloseClient(con
));
1984 else if (ipp_state
!= IPP_DATA
)
1986 if (con
->http
.state
== HTTP_POST_SEND
)
1988 cupsdSendError(con
, HTTP_BAD_REQUEST
);
1989 return (cupsdCloseClient(con
));
1995 con
->bytes
+= ippLength(con
->request
);
1998 if (con
->file
< 0 && con
->http
.state
!= HTTP_POST_SEND
)
2001 * Create a file as needed for the request data...
2004 cupsdSetStringf(&con
->filename
, "%s/%08x", RequestRoot
, request_id
++);
2005 con
->file
= open(con
->filename
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0640);
2007 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdReadClient: %d REQUEST %s=%d", con
->http
.fd
,
2008 con
->filename
, con
->file
);
2012 if (!cupsdSendError(con
, HTTP_REQUEST_TOO_LARGE
))
2013 return (cupsdCloseClient(con
));
2016 fchmod(con
->file
, 0640);
2017 fchown(con
->file
, RunUser
, Group
);
2018 fcntl(con
->file
, F_SETFD
, fcntl(con
->file
, F_GETFD
) | FD_CLOEXEC
);
2021 if (con
->http
.state
!= HTTP_POST_SEND
)
2023 if ((bytes
= httpRead(HTTP(con
), line
, sizeof(line
))) < 0)
2024 return (cupsdCloseClient(con
));
2027 con
->bytes
+= bytes
;
2029 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2030 "cupsdReadClient: %d writing %d bytes to %d",
2031 con
->http
.fd
, bytes
, con
->file
);
2033 if (write(con
->file
, line
, bytes
) < bytes
)
2035 cupsdLogMessage(CUPSD_LOG_ERROR
,
2036 "cupsdReadClient: Unable to write %d bytes to %s: %s",
2037 bytes
, con
->filename
, strerror(errno
));
2039 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2040 "cupsdReadClient: Closing file %d...",
2045 unlink(con
->filename
);
2046 cupsdClearString(&con
->filename
);
2048 if (!cupsdSendError(con
, HTTP_REQUEST_TOO_LARGE
))
2049 return (cupsdCloseClient(con
));
2052 else if (con
->http
.state
== HTTP_POST_RECV
)
2053 return (1); /* ??? */
2054 else if (con
->http
.state
!= HTTP_POST_SEND
)
2055 return (cupsdCloseClient(con
));
2058 if (con
->http
.state
== HTTP_POST_SEND
)
2062 fstat(con
->file
, &filestats
);
2064 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2065 "cupsdReadClient: %d Closing data file %d, "
2066 "size=" CUPS_LLFMT
".",
2067 con
->http
.fd
, con
->file
,
2068 CUPS_LLCAST filestats
.st_size
);
2073 if (filestats
.st_size
> MaxRequestSize
&&
2077 * Request is too big; remove it and send an error...
2080 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2081 "cupsdReadClient: %d Removing temp file %s",
2082 con
->http
.fd
, con
->filename
);
2083 unlink(con
->filename
);
2084 cupsdClearString(&con
->filename
);
2089 * Delete any IPP request data...
2092 ippDelete(con
->request
);
2093 con
->request
= NULL
;
2096 if (!cupsdSendError(con
, HTTP_REQUEST_TOO_LARGE
))
2097 return (cupsdCloseClient(con
));
2102 if (!cupsdSendCommand(con
, con
->command
, con
->options
, 0))
2104 if (!cupsdSendError(con
, HTTP_NOT_FOUND
))
2105 return (cupsdCloseClient(con
));
2108 cupsdLogRequest(con
, HTTP_OK
);
2113 return (cupsdProcessIPPRequest(con
));
2118 break; /* Anti-compiler-warning-code */
2121 if (!con
->http
.keep_alive
&& con
->http
.state
== HTTP_WAITING
)
2122 return (cupsdCloseClient(con
));
2129 * 'cupsdSendCommand()' - Send output from a command via HTTP.
2132 int /* O - 1 on success, 0 on failure */
2134 cupsd_client_t
*con
, /* I - Client connection */
2135 char *command
, /* I - Command to run */
2136 char *options
, /* I - Command-line options */
2137 int root
) /* I - Run as root? */
2139 int fd
; /* Standard input file descriptor */
2143 fd
= open(con
->filename
, O_RDONLY
);
2145 fd
= open("/dev/null", O_RDONLY
);
2149 cupsdLogMessage(CUPSD_LOG_ERROR
,
2150 "cupsdSendCommand: %d Unable to open \"%s\" for reading: %s",
2151 con
->http
.fd
, con
->filename
? con
->filename
: "/dev/null",
2156 fcntl(fd
, F_SETFD
, fcntl(fd
, F_GETFD
) | FD_CLOEXEC
);
2158 con
->pipe_pid
= pipe_command(con
, fd
, &(con
->file
), command
, options
, root
);
2162 cupsdLogMessage(CUPSD_LOG_INFO
, "Started \"%s\" (pid=%d)", command
,
2165 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdSendCommand: %d file=%d",
2166 con
->http
.fd
, con
->file
);
2168 if (con
->pipe_pid
== 0)
2171 fcntl(con
->file
, F_SETFD
, fcntl(con
->file
, F_GETFD
) | FD_CLOEXEC
);
2173 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2174 "cupsdSendCommand: Adding fd %d to InputSet...", con
->file
);
2175 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2176 "cupsdSendCommand: Adding fd %d to OutputSet...",
2179 FD_SET(con
->file
, InputSet
);
2180 FD_SET(con
->http
.fd
, OutputSet
);
2182 con
->sent_header
= 0;
2183 con
->file_ready
= 0;
2184 con
->got_fields
= 0;
2192 * 'cupsdSendError()' - Send an error message via HTTP.
2195 int /* O - 1 if successful, 0 otherwise */
2196 cupsdSendError(cupsd_client_t
*con
, /* I - Connection */
2197 http_status_t code
) /* I - Error code */
2200 * Put the request in the access_log file...
2203 cupsdLogRequest(con
, code
);
2205 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdSendError: %d code=%d (%s)",
2206 con
->http
.fd
, code
, httpStatus(code
));
2209 * To work around bugs in some proxies, don't use Keep-Alive for some
2213 if (code
>= HTTP_BAD_REQUEST
)
2214 con
->http
.keep_alive
= HTTP_KEEPALIVE_OFF
;
2217 * Send an error message back to the client. If the error code is a
2218 * 400 or 500 series, make sure the message contains some text, too!
2221 if (!cupsdSendHeader(con
, code
, NULL
))
2225 if (code
== HTTP_UPGRADE_REQUIRED
)
2226 if (httpPrintf(HTTP(con
), "Connection: Upgrade\r\n") < 0)
2229 if (httpPrintf(HTTP(con
), "Upgrade: TLS/1.0,HTTP/1.1\r\n") < 0)
2231 #endif /* HAVE_SSL */
2233 if ((con
->http
.version
>= HTTP_1_1
&& !con
->http
.keep_alive
) ||
2234 (code
>= HTTP_BAD_REQUEST
&& code
!= HTTP_UPGRADE_REQUIRED
))
2236 if (httpPrintf(HTTP(con
), "Connection: close\r\n") < 0)
2240 if (code
>= HTTP_BAD_REQUEST
)
2243 * Send a human-readable error message.
2246 char message
[1024]; /* Message for user */
2247 const char *text
; /* Status-specific text */
2249 if (code
== HTTP_UNAUTHORIZED
)
2250 text
= _cupsLangString(con
->language
,
2251 _("Enter your username and password or the "
2252 "root username and password to access this "
2254 else if (code
== HTTP_UPGRADE_REQUIRED
)
2255 text
= _cupsLangString(con
->language
,
2256 _("You must use a https: URL to access this "
2261 snprintf(message
, sizeof(message
),
2262 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\" "
2263 "\"http://www.w3.org/TR/REC-html40/loose.dtd\">\n"
2266 "\t<META HTTP-EQUIV=\"Content-Type\" "
2267 "CONTENT=\"text/html; charset=utf-8\">\n"
2268 "\t<TITLE>%d %s</TITLE>\n"
2269 "\t<LINK REL=\"STYLESHEET\" TYPE=\"text/css\" "
2270 "HREF=\"/cups.css\">\n"
2277 code
, httpStatus(code
), code
, httpStatus(code
), text
);
2279 if (httpPrintf(HTTP(con
), "Content-Type: text/html; charset=utf-8\r\n") < 0)
2281 if (httpPrintf(HTTP(con
), "Content-Length: %d\r\n",
2282 (int)strlen(message
)) < 0)
2284 if (httpPrintf(HTTP(con
), "\r\n") < 0)
2286 if (httpPrintf(HTTP(con
), "%s", message
) < 0)
2289 else if (httpPrintf(HTTP(con
), "\r\n") < 0)
2292 con
->http
.state
= HTTP_WAITING
;
2299 * 'cupsdSendFile()' - Send a file via HTTP.
2302 int /* O - 0 on failure, 1 on success */
2303 cupsdSendFile(cupsd_client_t
*con
, /* I - Client connection */
2304 http_status_t code
, /* I - HTTP status */
2305 char *filename
, /* I - Filename */
2306 char *type
, /* I - File type */
2307 struct stat
*filestats
)/* O - File information */
2309 con
->file
= open(filename
, O_RDONLY
);
2311 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdSendFile: %d file=%d", con
->http
.fd
,
2317 fcntl(con
->file
, F_SETFD
, fcntl(con
->file
, F_GETFD
) | FD_CLOEXEC
);
2321 if (!cupsdSendHeader(con
, code
, type
))
2324 if (httpPrintf(HTTP(con
), "Last-Modified: %s\r\n",
2325 httpGetDateString(filestats
->st_mtime
)) < 0)
2327 if (httpPrintf(HTTP(con
), "Content-Length: " CUPS_LLFMT
"\r\n",
2328 CUPS_LLCAST filestats
->st_size
) < 0)
2330 if (httpPrintf(HTTP(con
), "\r\n") < 0)
2333 con
->http
.data_encoding
= HTTP_ENCODE_LENGTH
;
2334 con
->http
.data_remaining
= filestats
->st_size
;
2336 if (con
->http
.data_remaining
<= INT_MAX
)
2337 con
->http
._data_remaining
= con
->http
.data_remaining
;
2339 con
->http
._data_remaining
= INT_MAX
;
2341 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2342 "cupsdSendFile: Adding fd %d to OutputSet...", con
->http
.fd
);
2344 FD_SET(con
->http
.fd
, OutputSet
);
2351 * 'cupsdSendHeader()' - Send an HTTP request.
2354 int /* O - 1 on success, 0 on failure */
2355 cupsdSendHeader(cupsd_client_t
*con
, /* I - Client to send to */
2356 http_status_t code
, /* I - HTTP status code */
2357 char *type
) /* I - MIME type of document */
2359 if (httpPrintf(HTTP(con
), "HTTP/%d.%d %d %s\r\n", con
->http
.version
/ 100,
2360 con
->http
.version
% 100, code
, httpStatus(code
)) < 0)
2362 if (httpPrintf(HTTP(con
), "Date: %s\r\n", httpGetDateString(time(NULL
))) < 0)
2365 if (httpPrintf(HTTP(con
), "Server: %s\r\n", ServerHeader
) < 0)
2367 if (con
->http
.keep_alive
&& con
->http
.version
>= HTTP_1_0
)
2369 if (httpPrintf(HTTP(con
), "Connection: Keep-Alive\r\n") < 0)
2371 if (httpPrintf(HTTP(con
), "Keep-Alive: timeout=%d\r\n",
2372 KeepAliveTimeout
) < 0)
2375 if (code
== HTTP_METHOD_NOT_ALLOWED
)
2376 if (httpPrintf(HTTP(con
), "Allow: GET, HEAD, OPTIONS, POST\r\n") < 0)
2379 if (code
== HTTP_UNAUTHORIZED
)
2381 int auth_type
; /* Authentication type */
2384 if (!con
->best
|| con
->best
->type
== AUTH_NONE
)
2385 auth_type
= DefaultAuthType
;
2387 auth_type
= con
->best
->type
;
2389 if (auth_type
!= AUTH_DIGEST
)
2391 if (httpPrintf(HTTP(con
),
2392 "WWW-Authenticate: Basic realm=\"CUPS\"\r\n") < 0)
2397 if (httpPrintf(HTTP(con
),
2398 "WWW-Authenticate: Digest realm=\"CUPS\", nonce=\"%s\"\r\n",
2399 con
->http
.hostname
) < 0)
2404 if (con
->language
!= NULL
)
2406 if (httpPrintf(HTTP(con
), "Content-Language: %s\r\n",
2407 con
->language
->language
) < 0)
2413 if (!strcmp(type
, "text/html"))
2415 if (httpPrintf(HTTP(con
),
2416 "Content-Type: text/html; charset=utf-8\r\n") < 0)
2419 else if (httpPrintf(HTTP(con
), "Content-Type: %s\r\n", type
) < 0)
2428 * 'cupsdUpdateCGI()' - Read status messages from CGI scripts and programs.
2432 cupsdUpdateCGI(void)
2434 char *ptr
, /* Pointer to end of line in buffer */
2435 message
[1024]; /* Pointer to message text */
2436 int loglevel
; /* Log level for message */
2439 while ((ptr
= cupsdStatBufUpdate(CGIStatusBuffer
, &loglevel
,
2440 message
, sizeof(message
))) != NULL
)
2441 if (!strchr(CGIStatusBuffer
->buffer
, '\n'))
2444 if (ptr
== NULL
&& errno
)
2447 * Fatal error on pipe - should never happen!
2450 cupsdLogMessage(CUPSD_LOG_CRIT
,
2451 "cupsdUpdateCGI: error reading from CGI error pipe - %s",
2458 * 'cupsdWriteClient()' - Write data to a client as needed.
2461 int /* O - 1 if success, 0 if fail */
2462 cupsdWriteClient(cupsd_client_t
*con
) /* I - Client connection */
2464 int bytes
; /* Number of bytes written */
2465 char buf
[16385]; /* Data buffer */
2466 char *bufptr
; /* Pointer into buffer */
2467 ipp_state_t ipp_state
; /* IPP state value */
2471 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2472 "cupsdWriteClient(con=%p) %d response=%p, file=%d "
2473 "pipe_pid=%d state=%d",
2474 con
, con
->http
.fd
, con
->response
, con
->file
, con
->pipe_pid
,
2478 if (con
->http
.state
!= HTTP_GET_SEND
&&
2479 con
->http
.state
!= HTTP_POST_SEND
)
2482 if (con
->response
!= NULL
)
2484 ipp_state
= ippWrite(&(con
->http
), con
->response
);
2485 bytes
= ipp_state
!= IPP_ERROR
&& ipp_state
!= IPP_DATA
;
2487 else if ((bytes
= read(con
->file
, buf
, sizeof(buf
) - 1)) > 0)
2489 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2490 "cupsdWriteClient: Read %d bytes from file %d...",
2493 if (con
->pipe_pid
&& !con
->got_fields
)
2496 * Inspect the data for Content-Type and other fields.
2501 for (bufptr
= buf
; !con
->got_fields
&& *bufptr
; bufptr
++)
2502 if (*bufptr
== '\n')
2505 * Send line to client...
2508 if (bufptr
> buf
&& bufptr
[-1] == '\r')
2512 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "Script header: %s", buf
);
2514 if (!con
->sent_header
)
2517 * Handle redirection and CGI status codes...
2520 if (!strncasecmp(buf
, "Location:", 9))
2521 cupsdSendHeader(con
, HTTP_SEE_OTHER
, NULL
);
2522 else if (!strncasecmp(buf
, "Status:", 7))
2523 cupsdSendHeader(con
, atoi(buf
+ 7), NULL
);
2525 cupsdSendHeader(con
, HTTP_OK
, NULL
);
2527 if (con
->http
.version
== HTTP_1_1
)
2529 con
->http
.data_encoding
= HTTP_ENCODE_CHUNKED
;
2531 if (httpPrintf(HTTP(con
), "Transfer-Encoding: chunked\r\n") < 0)
2535 con
->sent_header
= 1;
2538 if (strncasecmp(buf
, "Status:", 7))
2539 httpPrintf(HTTP(con
), "%s\r\n", buf
);
2541 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdWriteClient: %d %s",
2548 bytes
-= (bufptr
- buf
);
2549 memmove(buf
, bufptr
, bytes
+ 1);
2553 * See if the line was empty...
2556 if (con
->field_col
== 0)
2557 con
->got_fields
= 1;
2561 else if (*bufptr
!= '\r')
2564 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2565 "cupsdWriteClient: %d bytes=%d, got_fields=%d",
2566 con
->http
.fd
, bytes
, con
->got_fields
);
2568 if (bytes
> 0 && !con
->got_fields
)
2571 * Remaining text needs to go out...
2574 httpPrintf(HTTP(con
), "%s", buf
);
2576 con
->http
.activity
= time(NULL
);
2579 else if (bytes
== 0)
2581 con
->http
.activity
= time(NULL
);
2586 if (httpWrite(HTTP(con
), buf
, bytes
) < 0)
2588 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2589 "cupsdWriteClient: %d Write of %d bytes failed!",
2590 con
->http
.fd
, bytes
);
2592 cupsdCloseClient(con
);
2596 con
->bytes
+= bytes
;
2598 if (con
->http
.state
== HTTP_WAITING
)
2604 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdWriteClient: %d bytes < 0",
2607 cupsdLogRequest(con
, HTTP_OK
);
2609 httpFlushWrite(HTTP(con
));
2611 if (con
->http
.data_encoding
== HTTP_ENCODE_CHUNKED
)
2613 if (httpPrintf(HTTP(con
), "0\r\n\r\n") < 0)
2615 cupsdCloseClient(con
);
2620 con
->http
.state
= HTTP_WAITING
;
2622 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2623 "cupsdWriteClient: Removing fd %d from OutputSet...",
2626 FD_CLR(con
->http
.fd
, OutputSet
);
2630 if (FD_ISSET(con
->file
, InputSet
))
2632 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2633 "cupsdWriteClient: Removing fd %d from InputSet...",
2635 FD_CLR(con
->file
, InputSet
);
2639 cupsdEndProcess(con
->pipe_pid
, 0);
2641 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2642 "cupsdWriteClient: %d Closing data file %d.",
2643 con
->http
.fd
, con
->file
);
2652 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2653 "cupsdWriteClient: %d Removing temp file %s",
2654 con
->http
.fd
, con
->filename
);
2655 unlink(con
->filename
);
2656 cupsdClearString(&con
->filename
);
2659 if (con
->request
!= NULL
)
2661 ippDelete(con
->request
);
2662 con
->request
= NULL
;
2665 if (con
->response
!= NULL
)
2667 ippDelete(con
->response
);
2668 con
->response
= NULL
;
2671 cupsdClearString(&con
->command
);
2672 cupsdClearString(&con
->options
);
2674 if (!con
->http
.keep_alive
)
2676 cupsdCloseClient(con
);
2682 con
->file_ready
= 0;
2684 if (con
->pipe_pid
&& !FD_ISSET(con
->file
, InputSet
))
2686 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2687 "cupsdWriteClient: Adding fd %d to InputSet...",
2689 FD_SET(con
->file
, InputSet
);
2693 con
->http
.activity
= time(NULL
);
2700 * 'check_if_modified()' - Decode an "If-Modified-Since" line.
2703 static int /* O - 1 if modified since */
2705 cupsd_client_t
*con
, /* I - Client connection */
2706 struct stat
*filestats
) /* I - File information */
2708 char *ptr
; /* Pointer into field */
2709 time_t date
; /* Time/date value */
2710 off_t size
; /* Size/length value */
2715 ptr
= con
->http
.fields
[HTTP_FIELD_IF_MODIFIED_SINCE
];
2720 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2721 "check_if_modified: %d If-Modified-Since=\"%s\"",
2724 while (*ptr
!= '\0')
2726 while (isspace(*ptr
) || *ptr
== ';')
2729 if (strncasecmp(ptr
, "length=", 7) == 0)
2732 size
= strtoll(ptr
, NULL
, 10);
2734 while (isdigit(*ptr
))
2737 else if (isalpha(*ptr
))
2739 date
= httpGetDateTime(ptr
);
2740 while (*ptr
!= '\0' && *ptr
!= ';')
2745 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2746 "check_if_modified: %d sizes=" CUPS_LLFMT
","
2747 CUPS_LLFMT
" dates=%d,%d",
2748 con
->http
.fd
, CUPS_LLCAST size
,
2749 CUPS_LLCAST filestats
->st_size
, (int)date
,
2750 (int)filestats
->st_mtime
);
2752 return ((size
!= filestats
->st_size
&& size
!= 0) ||
2753 (date
< filestats
->st_mtime
&& date
!= 0) ||
2754 (size
== 0 && date
== 0));
2760 * 'get_cdsa_server_certs()' - Convert a keychain name into the CFArrayRef
2761 * required by SSLSetCertificate.
2763 * For now we assumes that there is exactly one SecIdentity in the
2764 * keychain - i.e. there is exactly one matching cert/private key pair.
2765 * In the future we will search a keychain for a SecIdentity matching a
2766 * specific criteria. We also skip the operation of adding additional
2767 * non-signing certs from the keychain to the CFArrayRef.
2769 * To create a self-signed certificate for testing use the certtool.
2770 * Executing the following as root will do it:
2772 * certtool c c v k=CUPS
2775 static CFArrayRef
/* O - Array of certificates */
2776 get_cdsa_server_certs(void)
2778 OSStatus err
; /* Error info */
2779 SecKeychainRef kcRef
; /* Keychain reference */
2780 SecIdentitySearchRef srchRef
; /* Search reference */
2781 SecIdentityRef identity
; /* Identity */
2782 CFArrayRef ca
; /* Certificate array */
2789 err
= SecKeychainOpen(ServerCertificate
, &kcRef
);
2792 cupsdLogMessage(CUPSD_LOG_ERROR
, "Cannot open keychain \"%s\", error %d.",
2793 ServerCertificate
, (int)err
);
2797 * Search for "any" identity matching specified key use;
2798 * in this app, we expect there to be exactly one.
2801 err
= SecIdentitySearchCreate(kcRef
, CSSM_KEYUSE_SIGN
, &srchRef
);
2804 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2805 "Cannot find signing key in keychain \"%s\", error %d",
2806 ServerCertificate
, (int)err
);
2809 err
= SecIdentitySearchCopyNext(srchRef
, &identity
);
2812 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2813 "Cannot find signing key in keychain \"%s\", error %d",
2814 ServerCertificate
, (int)err
);
2817 if (CFGetTypeID(identity
) != SecIdentityGetTypeID())
2818 cupsdLogMessage(CUPSD_LOG_ERROR
,
2819 "SecIdentitySearchCopyNext CFTypeID failure!");
2823 * Found one. Place it in a CFArray.
2824 * TBD: snag other (non-identity) certs from keychain and add them
2828 ca
= CFArrayCreate(NULL
, (const void **)&identity
, 1, NULL
);
2831 cupsdLogMessage(CUPSD_LOG_ERROR
, "CFArrayCreate error");
2834 /*CFRelease(identity);*/
2837 /*CFRelease(srchRef);*/
2840 /*CFRelease(kcRef);*/
2845 #endif /* HAVE_CDSASSL */
2849 * 'get_file()' - Get a filename and state info.
2852 static char * /* O - Real filename */
2853 get_file(cupsd_client_t
*con
, /* I - Client connection */
2854 struct stat
*filestats
, /* O - File information */
2855 char *filename
, /* IO - Filename buffer */
2856 int len
) /* I - Buffer length */
2858 int status
; /* Status of filesystem calls */
2859 char *ptr
; /* Pointer info filename */
2860 int plen
; /* Remaining length after pointer */
2864 * Need to add DocumentRoot global...
2867 if (!strncmp(con
->uri
, "/ppd/", 5))
2868 snprintf(filename
, len
, "%s%s", ServerRoot
, con
->uri
);
2869 else if (!strncmp(con
->uri
, "/admin/conf/", 12))
2870 snprintf(filename
, len
, "%s%s", ServerRoot
, con
->uri
+ 11);
2871 else if (!strncmp(con
->uri
, "/admin/log/", 11))
2873 if (!strcmp(con
->uri
+ 11, "access_log") && AccessLog
[0] == '/')
2874 strlcpy(filename
, AccessLog
, len
);
2875 else if (!strcmp(con
->uri
+ 11, "error_log") && ErrorLog
[0] == '/')
2876 strlcpy(filename
, ErrorLog
, len
);
2877 else if (!strcmp(con
->uri
+ 11, "page_log") && PageLog
[0] == '/')
2878 strlcpy(filename
, PageLog
, len
);
2882 else if (con
->language
!= NULL
)
2883 snprintf(filename
, len
, "%s/%s%s", DocumentRoot
, con
->language
->language
,
2886 snprintf(filename
, len
, "%s%s", DocumentRoot
, con
->uri
);
2888 if ((ptr
= strchr(filename
, '?')) != NULL
)
2892 * Grab the status for this language; if there isn't a language-specific file
2893 * then fallback to the default one...
2896 if ((status
= stat(filename
, filestats
)) != 0 && con
->language
!= NULL
)
2899 * Drop the language prefix and try the current directory...
2902 if (strncmp(con
->uri
, "/ppd/", 5) &&
2903 strncmp(con
->uri
, "/admin/conf/", 12) &&
2904 strncmp(con
->uri
, "/admin/log/", 11))
2906 snprintf(filename
, len
, "%s%s", DocumentRoot
, con
->uri
);
2908 if ((ptr
= strchr(filename
, '?')) != NULL
)
2911 status
= stat(filename
, filestats
);
2916 * If we're found a directory, get the index.html file instead...
2919 if (!status
&& S_ISDIR(filestats
->st_mode
))
2921 if (filename
[strlen(filename
) - 1] != '/')
2922 strlcat(filename
, "/", len
);
2924 ptr
= filename
+ strlen(filename
);
2925 plen
= len
- (ptr
- filename
);
2927 strlcpy(ptr
, "index.html", plen
);
2928 status
= stat(filename
, filestats
);
2933 strlcpy(ptr
, "index.class", plen
);
2934 status
= stat(filename
, filestats
);
2936 #endif /* HAVE_JAVA */
2941 strlcpy(ptr
, "index.pl", plen
);
2942 status
= stat(filename
, filestats
);
2944 #endif /* HAVE_PERL */
2949 strlcpy(ptr
, "index.php", plen
);
2950 status
= stat(filename
, filestats
);
2952 #endif /* HAVE_PHP */
2957 strlcpy(ptr
, "index.pyc", plen
);
2958 status
= stat(filename
, filestats
);
2963 strlcpy(ptr
, "index.py", plen
);
2964 status
= stat(filename
, filestats
);
2966 #endif /* HAVE_PYTHON */
2969 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_file: %d filename=%s size=%d",
2970 con
->http
.fd
, filename
,
2971 status
? -1 : (int)filestats
->st_size
);
2974 con
->http
.data_remaining
= (int)filestats
->st_size
;
2984 * 'install_conf_file()' - Install a configuration file.
2987 static http_status_t
/* O - Status */
2988 install_conf_file(cupsd_client_t
*con
) /* I - Connection */
2990 cups_file_t
*in
, /* Input file */
2991 *out
; /* Output file */
2992 char buffer
[1024]; /* Copy buffer */
2993 int bytes
; /* Number of bytes */
2994 char conffile
[1024], /* Configuration filename */
2995 newfile
[1024], /* New config filename */
2996 oldfile
[1024]; /* Old config filename */
2997 struct stat confinfo
; /* Config file info */
3001 * First construct the filenames...
3004 snprintf(conffile
, sizeof(conffile
), "%s%s", ServerRoot
, con
->uri
+ 11);
3005 snprintf(newfile
, sizeof(newfile
), "%s%s.N", ServerRoot
, con
->uri
+ 11);
3006 snprintf(oldfile
, sizeof(oldfile
), "%s%s.O", ServerRoot
, con
->uri
+ 11);
3008 cupsdLogMessage(CUPSD_LOG_INFO
, "Installing config file \"%s\"...", conffile
);
3011 * Get the owner, group, and permissions of the configuration file.
3012 * If it doesn't exist, assign it to the User and Group in the
3013 * cupsd.conf file with mode 0640 permissions.
3016 if (stat(conffile
, &confinfo
))
3018 confinfo
.st_uid
= User
;
3019 confinfo
.st_gid
= Group
;
3020 confinfo
.st_mode
= ConfigFilePerm
;
3024 * Open the request file and new config file...
3027 if ((in
= cupsFileOpen(con
->filename
, "rb")) == NULL
)
3029 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to open request file \"%s\" - %s",
3030 con
->filename
, strerror(errno
));
3031 return (HTTP_SERVER_ERROR
);
3034 if ((out
= cupsFileOpen(newfile
, "wb")) == NULL
)
3037 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to open config file \"%s\" - %s",
3038 newfile
, strerror(errno
));
3039 return (HTTP_SERVER_ERROR
);
3042 fchmod(cupsFileNumber(out
), confinfo
.st_mode
);
3043 fchown(cupsFileNumber(out
), confinfo
.st_uid
, confinfo
.st_gid
);
3046 * Copy from the request to the new config file...
3049 while ((bytes
= cupsFileRead(in
, buffer
, sizeof(buffer
))) > 0)
3050 if (cupsFileWrite(out
, buffer
, bytes
) < bytes
)
3052 cupsdLogMessage(CUPSD_LOG_ERROR
,
3053 "Unable to copy to config file \"%s\" - %s",
3054 newfile
, strerror(errno
));
3060 return (HTTP_SERVER_ERROR
);
3064 * Close the files...
3068 if (cupsFileClose(out
))
3070 cupsdLogMessage(CUPSD_LOG_ERROR
,
3071 "Error file closing config file \"%s\" - %s",
3072 newfile
, strerror(errno
));
3076 return (HTTP_SERVER_ERROR
);
3080 * Remove the request file...
3083 unlink(con
->filename
);
3084 cupsdClearString(&con
->filename
);
3087 * Unlink the old backup, rename the current config file to the backup
3088 * filename, and rename the new config file to the config file name...
3091 if (unlink(oldfile
))
3092 if (errno
!= ENOENT
)
3094 cupsdLogMessage(CUPSD_LOG_ERROR
,
3095 "Unable to remove backup config file \"%s\" - %s",
3096 oldfile
, strerror(errno
));
3100 return (HTTP_SERVER_ERROR
);
3103 if (rename(conffile
, oldfile
))
3104 if (errno
!= ENOENT
)
3106 cupsdLogMessage(CUPSD_LOG_ERROR
,
3107 "Unable to rename old config file \"%s\" - %s",
3108 conffile
, strerror(errno
));
3112 return (HTTP_SERVER_ERROR
);
3115 if (rename(newfile
, conffile
))
3117 cupsdLogMessage(CUPSD_LOG_ERROR
,
3118 "Unable to rename new config file \"%s\" - %s",
3119 newfile
, strerror(errno
));
3121 rename(oldfile
, conffile
);
3124 return (HTTP_SERVER_ERROR
);
3128 * If the cupsd.conf file was updated, set the NeedReload flag...
3131 if (!strcmp(con
->uri
, "/admin/conf/cupsd.conf"))
3132 NeedReload
= RELOAD_CUPSD
;
3134 NeedReload
= RELOAD_ALL
;
3136 ReloadTime
= time(NULL
);
3139 * Return that the file was created successfully...
3142 return (HTTP_CREATED
);
3147 * 'is_path_absolute()' - Is a path absolute and free of relative elements (i.e. "..").
3150 static int /* O - 0 if relative, 1 if absolute */
3151 is_path_absolute(const char *path
) /* I - Input path */
3154 * Check for a leading slash...
3161 * Check for "/.." in the path...
3164 while ((path
= strstr(path
, "/..")) != NULL
)
3166 if (!path
[3] || path
[3] == '/')
3173 * If we haven't found any relative paths, return 1 indicating an
3182 * 'pipe_command()' - Pipe the output of a command to the remote client.
3185 static int /* O - Process ID */
3186 pipe_command(cupsd_client_t
*con
, /* I - Client connection */
3187 int infile
, /* I - Standard input for command */
3188 int *outfile
, /* O - Standard output for command */
3189 char *command
, /* I - Command to run */
3190 char *options
, /* I - Options for command */
3191 int root
) /* I - Run as root? */
3193 int i
; /* Looping var */
3194 int pid
; /* Process ID */
3195 char *commptr
; /* Command string pointer */
3196 char *uriptr
; /* URI string pointer */
3197 int fds
[2]; /* Pipe FDs */
3198 int argc
; /* Number of arguments */
3199 int envc
; /* Number of environment variables */
3200 char argbuf
[10240], /* Argument buffer */
3201 *argv
[100], /* Argument strings */
3202 *envp
[100]; /* Environment variables */
3203 char content_length
[1024], /* CONTENT_LENGTH environment variable */
3204 content_type
[1024], /* CONTENT_TYPE environment variable */
3205 http_cookie
[32768], /* HTTP_COOKIE environment variable */
3206 http_user_agent
[1024], /* HTTP_USER_AGENT environment variable */
3207 lang
[1024], /* LANG environment variable */
3208 *query_string
, /* QUERY_STRING env variable */
3209 remote_addr
[1024], /* REMOTE_ADDR environment variable */
3210 remote_host
[1024], /* REMOTE_HOST environment variable */
3211 remote_user
[1024], /* REMOTE_USER environment variable */
3212 script_name
[1024], /* SCRIPT_NAME environment variable */
3213 server_name
[1024], /* SERVER_NAME environment variable */
3214 server_port
[1024]; /* SERVER_PORT environment variable */
3218 * Parse a copy of the options string, which is of the form:
3220 * name argument+argument+argument
3221 * name?argument+argument+argument
3222 * name param=value¶m=value
3223 * name?param=value¶m=value
3225 * If the string contains an "=" character after the initial name,
3226 * then we treat it as a HTTP GET form request and make a copy of
3227 * the remaining string for the environment variable.
3229 * The string is always parsed out as command-line arguments, to
3230 * be consistent with Apache...
3233 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3234 "pipe_command: command=\"%s\", options=\"%s\"",
3237 strlcpy(argbuf
, options
, sizeof(argbuf
));
3240 query_string
= NULL
;
3242 for (commptr
= argbuf
, argc
= 1; *commptr
!= '\0' && argc
< 99; commptr
++)
3245 * Break arguments whenever we see a + or space...
3248 if (*commptr
== ' ' || *commptr
== '+' || (*commptr
== '?' && argc
== 1))
3251 * Terminate the current string and skip trailing whitespace...
3256 while (*commptr
== ' ')
3260 * If we don't have a blank string, save it as another argument...
3265 argv
[argc
] = commptr
;
3272 * If we see an "=" in the remaining string, make a copy of it since
3273 * it will be query data...
3276 if (argc
== 2 && strchr(commptr
, '=') && con
->operation
== HTTP_GET
)
3277 cupsdSetStringf(&query_string
, "QUERY_STRING=%s", commptr
);
3280 * Don't skip the first non-blank character...
3285 else if (*commptr
== '%' && isxdigit(commptr
[1] & 255) &&
3286 isxdigit(commptr
[2] & 255))
3289 * Convert the %xx notation to the individual character.
3292 if (commptr
[1] >= '0' && commptr
[1] <= '9')
3293 *commptr
= (commptr
[1] - '0') << 4;
3295 *commptr
= (tolower(commptr
[1]) - 'a' + 10) << 4;
3297 if (commptr
[2] >= '0' && commptr
[2] <= '9')
3298 *commptr
|= commptr
[2] - '0';
3300 *commptr
|= tolower(commptr
[2]) - 'a' + 10;
3302 _cups_strcpy(commptr
+ 1, commptr
+ 3);
3305 * Check for a %00 and break if that is the case...
3315 if (argv
[0][0] == '\0')
3316 argv
[0] = strrchr(command
, '/') + 1;
3319 * Setup the environment variables as needed...
3323 snprintf(lang
, sizeof(lang
), "LANG=%s.UTF-8", con
->language
->language
);
3325 strcpy(lang
, "LANG=C");
3327 strcpy(remote_addr
, "REMOTE_ADDR=");
3328 httpAddrString(con
->http
.hostaddr
, remote_addr
+ 12,
3329 sizeof(remote_addr
) - 12);
3331 snprintf(remote_host
, sizeof(remote_host
), "REMOTE_HOST=%s",
3332 con
->http
.hostname
);
3334 snprintf(script_name
, sizeof(script_name
), "SCRIPT_NAME=%s", con
->uri
);
3335 if ((uriptr
= strchr(script_name
, '?')) != NULL
)
3338 sprintf(server_port
, "SERVER_PORT=%d", con
->serverport
);
3340 snprintf(server_name
, sizeof(server_name
), "SERVER_NAME=%s",
3343 envc
= cupsdLoadEnv(envp
, (int)(sizeof(envp
) / sizeof(envp
[0])));
3345 envp
[envc
++] = lang
;
3346 envp
[envc
++] = "REDIRECT_STATUS=1";
3347 envp
[envc
++] = server_name
;
3348 envp
[envc
++] = server_port
;
3349 envp
[envc
++] = remote_addr
;
3350 envp
[envc
++] = remote_host
;
3351 envp
[envc
++] = script_name
;
3353 if (con
->username
[0])
3355 snprintf(remote_user
, sizeof(remote_user
), "REMOTE_USER=%s", con
->username
);
3357 envp
[envc
++] = remote_user
;
3360 if (con
->http
.version
== HTTP_1_1
)
3361 envp
[envc
++] = "SERVER_PROTOCOL=HTTP/1.1";
3362 else if (con
->http
.version
== HTTP_1_0
)
3363 envp
[envc
++] = "SERVER_PROTOCOL=HTTP/1.0";
3365 envp
[envc
++] = "SERVER_PROTOCOL=HTTP/0.9";
3367 if (con
->http
.cookie
)
3369 snprintf(http_cookie
, sizeof(http_cookie
), "HTTP_COOKIE=%s",
3371 envp
[envc
++] = http_cookie
;
3374 if (con
->http
.fields
[HTTP_FIELD_USER_AGENT
][0])
3376 snprintf(http_user_agent
, sizeof(http_user_agent
), "HTTP_USER_AGENT=%s",
3377 con
->http
.fields
[HTTP_FIELD_USER_AGENT
]);
3378 envp
[envc
++] = http_user_agent
;
3381 if (con
->operation
== HTTP_GET
)
3383 for (i
= 0; i
< argc
; i
++)
3384 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "argv[%d] = \"%s\"", i
, argv
[i
]);
3386 envp
[envc
++] = "REQUEST_METHOD=GET";
3391 * Add GET form variables after ?...
3394 envp
[envc
++] = query_string
;
3399 sprintf(content_length
, "CONTENT_LENGTH=" CUPS_LLFMT
, con
->bytes
);
3400 snprintf(content_type
, sizeof(content_type
), "CONTENT_TYPE=%s",
3401 con
->http
.fields
[HTTP_FIELD_CONTENT_TYPE
]);
3403 envp
[envc
++] = "REQUEST_METHOD=POST";
3404 envp
[envc
++] = content_length
;
3405 envp
[envc
++] = content_type
;
3409 * Tell the CGI if we are using encryption...
3412 if (con
->http
.encryption
== HTTP_ENCRYPT_ALWAYS
)
3413 envp
[envc
++] = "HTTPS=ON";
3416 * Terminate the environment array...
3421 if (LogLevel
== CUPSD_LOG_DEBUG2
)
3423 for (i
= 0; i
< argc
; i
++)
3424 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3425 "pipe_command: argv[%d] = \"%s\"", i
, argv
[i
]);
3426 for (i
= 0; i
< envc
; i
++)
3427 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3428 "pipe_command: envp[%d] = \"%s\"", i
, envp
[i
]);
3432 * Create a pipe for the output...
3435 if (cupsdOpenPipe(fds
))
3437 cupsdClearString(&query_string
);
3439 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to create pipes for CGI %s - %s",
3440 argv
[0], strerror(errno
));
3445 * Then execute the command...
3448 if (cupsdStartProcess(command
, argv
, envp
, infile
, fds
[1], CGIPipes
[1],
3449 -1, root
, &pid
) < 0)
3452 * Error - can't fork!
3455 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to fork for CGI %s - %s", argv
[0],
3458 cupsdClosePipe(fds
);
3464 * Fork successful - return the PID...
3467 if (con
->username
[0])
3468 cupsdAddCert(pid
, con
->username
);
3470 cupsdLogMessage(CUPSD_LOG_DEBUG
, "CGI %s started - PID = %d", command
, pid
);
3476 cupsdClearString(&query_string
);
3483 * End of "$Id: client.c 4912 2006-01-10 21:43:56Z mike $".