2 * "$Id: client.c 5051 2006-02-02 16:13:16Z 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
= errSSLBadCert
; /* errSSLBadConfiguration is a better choice, but not available on 10.2.x */
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
)
782 error
= SSLSetCertificate(conn
, ServerCertificatesArray
);
785 * Perform SSL/TLS handshake
792 error
= SSLHandshake(conn
);
794 while (error
== errSSLWouldBlock
);
800 cupsdLogMessage(CUPSD_LOG_ERROR
,
801 "cupsdEncryptClient: Unable to encrypt connection from %s!",
804 cupsdLogMessage(CUPSD_LOG_ERROR
,
805 "cupsdEncryptClient: CDSA error code is %d", (int)error
);
807 con
->http
.error
= error
;
808 con
->http
.status
= HTTP_ERROR
;
811 SSLDisposeContext(conn
);
816 cupsdLogMessage(CUPSD_LOG_DEBUG
,
817 "cupsdEncryptClient: %d Connection from %s now encrypted.",
818 con
->http
.fd
, con
->http
.hostname
);
820 con
->http
.tls
= conn
;
825 #endif /* HAVE_LIBSSL */
830 * 'cupsdIsCGI()' - Is the resource a CGI script/program?
833 int /* O - 1 = CGI, 0 = file */
834 cupsdIsCGI(cupsd_client_t
*con
, /* I - Client connection */
835 const char *filename
, /* I - Real filename */
836 struct stat
*filestats
, /* I - File information */
837 mime_type_t
*type
) /* I - MIME type */
839 const char *options
; /* Options on URL */
842 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
843 "cupsdIsCGI(con=%p, filename=\"%s\", filestats=%p, type=%s/%s)\n",
844 con
, filename
, filestats
, type
? type
->super
: "unknown",
845 type
? type
->type
: "unknown");
848 * Get the options, if any...
851 if ((options
= strchr(con
->uri
, '?')) != NULL
)
855 * Check for known types...
858 if (!type
|| strcasecmp(type
->super
, "application"))
860 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdIsCGI: Returning 0...");
864 if (!strcasecmp(type
->type
, "x-httpd-cgi") &&
865 (filestats
->st_mode
& 0111))
868 * "application/x-httpd-cgi" is a CGI script.
871 cupsdSetString(&con
->command
, filename
);
873 filename
= strrchr(filename
, '/') + 1; /* Filename always absolute */
876 cupsdSetStringf(&con
->options
, "%s %s", filename
, options
);
878 cupsdSetStringf(&con
->options
, "%s", filename
);
880 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
881 "cupsdIsCGI: Returning 1 with command=\"%s\" and options=\"%s\"",
882 con
->command
, con
->options
);
887 else if (!strcasecmp(type
->type
, "x-httpd-java"))
890 * "application/x-httpd-java" is a Java servlet.
893 cupsdSetString(&con
->command
, CUPS_JAVA
);
896 cupsdSetStringf(&con
->options
, "java %s %s", filename
, options
);
898 cupsdSetStringf(&con
->options
, "java %s", filename
);
900 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
901 "cupsdIsCGI: Returning 1 with command=\"%s\" and options=\"%s\"",
902 con
->command
, con
->options
);
906 #endif /* HAVE_JAVA */
908 else if (!strcasecmp(type
->type
, "x-httpd-perl"))
911 * "application/x-httpd-perl" is a Perl page.
914 cupsdSetString(&con
->command
, CUPS_PERL
);
917 cupsdSetStringf(&con
->options
, "perl %s %s", filename
, options
);
919 cupsdSetStringf(&con
->options
, "perl %s", filename
);
921 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
922 "cupsdIsCGI: Returning 1 with command=\"%s\" and options=\"%s\"",
923 con
->command
, con
->options
);
927 #endif /* HAVE_PERL */
929 else if (!strcasecmp(type
->type
, "x-httpd-php"))
932 * "application/x-httpd-php" is a PHP page.
935 cupsdSetString(&con
->command
, CUPS_PHP
);
938 cupsdSetStringf(&con
->options
, "php %s %s", filename
, options
);
940 cupsdSetStringf(&con
->options
, "php %s", filename
);
942 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
943 "cupsdIsCGI: Returning 1 with command=\"%s\" and options=\"%s\"",
944 con
->command
, con
->options
);
948 #endif /* HAVE_PHP */
950 else if (!strcasecmp(type
->type
, "x-httpd-python"))
953 * "application/x-httpd-python" is a Python page.
956 cupsdSetString(&con
->command
, CUPS_PYTHON
);
959 cupsdSetStringf(&con
->options
, "python %s %s", filename
, options
);
961 cupsdSetStringf(&con
->options
, "python %s", filename
);
963 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
964 "cupsdIsCGI: Returning 1 with command=\"%s\" and options=\"%s\"",
965 con
->command
, con
->options
);
969 #endif /* HAVE_PYTHON */
971 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdIsCGI: Returning 0...");
978 * 'cupsdReadClient()' - Read data from a client.
981 int /* O - 1 on success, 0 on error */
982 cupsdReadClient(cupsd_client_t
*con
) /* I - Client to read from */
984 char line
[32768], /* Line from client... */
985 operation
[64], /* Operation code from socket */
986 version
[64], /* HTTP version number string */
987 locale
[64], /* Locale */
988 *ptr
; /* Pointer into strings */
989 int major
, minor
; /* HTTP version numbers */
990 http_status_t status
; /* Transfer status */
991 ipp_state_t ipp_state
; /* State of IPP transfer */
992 int bytes
; /* Number of bytes to POST */
993 char *filename
; /* Name of file for GET/HEAD */
994 char buf
[1024]; /* Buffer for real filename */
995 struct stat filestats
; /* File information */
996 mime_type_t
*type
; /* MIME type of file */
997 cupsd_printer_t
*p
; /* Printer */
998 static unsigned request_id
= 0; /* Request ID for temp files */
1001 status
= HTTP_CONTINUE
;
1003 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1004 "cupsdReadClient: %d, used=%d, file=%d state=%d",
1005 con
->http
.fd
, con
->http
.used
, con
->file
, con
->http
.state
);
1007 if (con
->http
.error
)
1009 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdReadClient: http error seen...");
1010 return (cupsdCloseClient(con
));
1017 * Automatically check for a SSL/TLS handshake...
1022 if (recv(con
->http
.fd
, buf
, 1, MSG_PEEK
) == 1 &&
1023 (!buf
[0] || !strchr("DGHOPT", buf
[0])))
1026 * Encrypt this connection...
1029 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1030 "cupsdReadClient: Saw first byte %02X, auto-negotiating SSL/TLS session...",
1033 cupsdEncryptClient(con
);
1037 #endif /* HAVE_SSL */
1039 switch (con
->http
.state
)
1043 * See if we've received a request line...
1046 if (httpGets(line
, sizeof(line
) - 1, HTTP(con
)) == NULL
)
1048 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1049 "cupsdReadClient: httpGets returned EOF...");
1050 return (cupsdCloseClient(con
));
1054 * Ignore blank request lines...
1057 if (line
[0] == '\0')
1061 * Clear other state variables...
1064 httpClearFields(HTTP(con
));
1066 con
->http
.activity
= time(NULL
);
1067 con
->http
.version
= HTTP_1_0
;
1068 con
->http
.keep_alive
= HTTP_KEEPALIVE_OFF
;
1069 con
->http
.data_encoding
= HTTP_ENCODE_LENGTH
;
1070 con
->http
.data_remaining
= 0;
1071 con
->http
._data_remaining
= 0;
1072 con
->operation
= HTTP_WAITING
;
1075 con
->file_ready
= 0;
1077 con
->username
[0] = '\0';
1078 con
->password
[0] = '\0';
1081 cupsdClearString(&con
->command
);
1082 cupsdClearString(&con
->options
);
1084 if (con
->language
!= NULL
)
1086 cupsLangFree(con
->language
);
1087 con
->language
= NULL
;
1091 * Grab the request line...
1094 switch (sscanf(line
, "%63s%1023s%63s", operation
, con
->uri
, version
))
1097 cupsdLogMessage(CUPSD_LOG_ERROR
,
1098 "Bad request line \"%s\" from %s!", line
,
1099 con
->http
.hostname
);
1100 cupsdSendError(con
, HTTP_BAD_REQUEST
);
1101 return (cupsdCloseClient(con
));
1103 con
->http
.version
= HTTP_0_9
;
1106 if (sscanf(version
, "HTTP/%d.%d", &major
, &minor
) != 2)
1108 cupsdLogMessage(CUPSD_LOG_ERROR
,
1109 "Bad request line \"%s\" from %s!", line
,
1110 con
->http
.hostname
);
1111 cupsdSendError(con
, HTTP_BAD_REQUEST
);
1112 return (cupsdCloseClient(con
));
1117 con
->http
.version
= (http_version_t
)(major
* 100 + minor
);
1118 if (con
->http
.version
== HTTP_1_1
&& KeepAlive
)
1119 con
->http
.keep_alive
= HTTP_KEEPALIVE_ON
;
1121 con
->http
.keep_alive
= HTTP_KEEPALIVE_OFF
;
1125 cupsdSendError(con
, HTTP_NOT_SUPPORTED
);
1126 return (cupsdCloseClient(con
));
1132 * Handle full URLs in the request line...
1135 if (strcmp(con
->uri
, "*"))
1137 char method
[HTTP_MAX_URI
], /* Method/scheme */
1138 userpass
[HTTP_MAX_URI
], /* Username:password */
1139 hostname
[HTTP_MAX_URI
], /* Hostname */
1140 resource
[HTTP_MAX_URI
]; /* Resource path */
1141 int port
; /* Port number */
1145 * Separate the URI into its components...
1148 httpSeparateURI(HTTP_URI_CODING_MOST
, con
->uri
,
1149 method
, sizeof(method
),
1150 userpass
, sizeof(userpass
),
1151 hostname
, sizeof(hostname
), &port
,
1152 resource
, sizeof(resource
));
1155 * Only allow URIs with the servername, localhost, or an IP
1159 if (strcmp(method
, "file") &&
1160 strcasecmp(hostname
, ServerName
) &&
1161 strcasecmp(hostname
, "localhost") &&
1162 !isdigit(hostname
[0]))
1165 * Nope, we don't do proxies...
1168 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad URI \"%s\" in request!",
1170 cupsdSendError(con
, HTTP_METHOD_NOT_ALLOWED
);
1171 return (cupsdCloseClient(con
));
1175 * Copy the resource portion back into the URI; both resource and
1176 * con->uri are HTTP_MAX_URI bytes in size...
1179 strcpy(con
->uri
, resource
);
1183 * Process the request...
1186 if (!strcmp(operation
, "GET"))
1187 con
->http
.state
= HTTP_GET
;
1188 else if (!strcmp(operation
, "PUT"))
1189 con
->http
.state
= HTTP_PUT
;
1190 else if (!strcmp(operation
, "POST"))
1191 con
->http
.state
= HTTP_POST
;
1192 else if (!strcmp(operation
, "DELETE"))
1193 con
->http
.state
= HTTP_DELETE
;
1194 else if (!strcmp(operation
, "TRACE"))
1195 con
->http
.state
= HTTP_TRACE
;
1196 else if (!strcmp(operation
, "OPTIONS"))
1197 con
->http
.state
= HTTP_OPTIONS
;
1198 else if (!strcmp(operation
, "HEAD"))
1199 con
->http
.state
= HTTP_HEAD
;
1202 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad operation \"%s\"!", operation
);
1203 cupsdSendError(con
, HTTP_BAD_REQUEST
);
1204 return (cupsdCloseClient(con
));
1207 con
->start
= time(NULL
);
1208 con
->operation
= con
->http
.state
;
1210 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdReadClient: %d %s %s HTTP/%d.%d", con
->http
.fd
,
1211 operation
, con
->uri
,
1212 con
->http
.version
/ 100, con
->http
.version
% 100);
1214 con
->http
.status
= HTTP_OK
;
1224 * Parse incoming parameters until the status changes...
1227 status
= httpUpdate(HTTP(con
));
1229 if (status
!= HTTP_OK
&& status
!= HTTP_CONTINUE
)
1231 cupsdSendError(con
, HTTP_BAD_REQUEST
);
1232 return (cupsdCloseClient(con
));
1237 break; /* Anti-compiler-warning-code */
1241 * Handle new transfers...
1244 if (status
== HTTP_OK
)
1246 if (con
->http
.fields
[HTTP_FIELD_ACCEPT_LANGUAGE
][0])
1249 * Figure out the locale from the Accept-Language and Content-Type
1253 if ((ptr
= strchr(con
->http
.fields
[HTTP_FIELD_ACCEPT_LANGUAGE
], ',')) != NULL
)
1256 if ((ptr
= strchr(con
->http
.fields
[HTTP_FIELD_ACCEPT_LANGUAGE
], ';')) != NULL
)
1259 if ((ptr
= strstr(con
->http
.fields
[HTTP_FIELD_CONTENT_TYPE
], "charset=")) != NULL
)
1262 * Combine language and charset, and trim any extra params in the
1266 snprintf(locale
, sizeof(locale
), "%s.%s",
1267 con
->http
.fields
[HTTP_FIELD_ACCEPT_LANGUAGE
], ptr
+ 8);
1269 if ((ptr
= strchr(locale
, ',')) != NULL
)
1273 snprintf(locale
, sizeof(locale
), "%s.%s",
1274 con
->http
.fields
[HTTP_FIELD_ACCEPT_LANGUAGE
], DefaultCharset
);
1276 con
->language
= cupsLangGet(locale
);
1279 con
->language
= cupsLangGet(DefaultLocale
);
1281 cupsdAuthorize(con
);
1283 if (!strncmp(con
->http
.fields
[HTTP_FIELD_CONNECTION
], "Keep-Alive", 10) &&
1285 con
->http
.keep_alive
= HTTP_KEEPALIVE_ON
;
1287 if (!con
->http
.fields
[HTTP_FIELD_HOST
][0] &&
1288 con
->http
.version
>= HTTP_1_1
)
1291 * HTTP/1.1 and higher require the "Host:" field...
1294 if (!cupsdSendError(con
, HTTP_BAD_REQUEST
))
1295 return (cupsdCloseClient(con
));
1297 else if (con
->operation
== HTTP_OPTIONS
)
1300 * Do OPTIONS command...
1303 if (con
->best
&& con
->best
->type
!= AUTH_NONE
)
1305 if (!cupsdSendHeader(con
, HTTP_UNAUTHORIZED
, NULL
))
1306 return (cupsdCloseClient(con
));
1309 if (!strcasecmp(con
->http
.fields
[HTTP_FIELD_CONNECTION
], "Upgrade") &&
1310 con
->http
.tls
== NULL
)
1314 * Do encryption stuff...
1317 if (!cupsdSendHeader(con
, HTTP_SWITCHING_PROTOCOLS
, NULL
))
1318 return (cupsdCloseClient(con
));
1320 httpPrintf(HTTP(con
), "Connection: Upgrade\r\n");
1321 httpPrintf(HTTP(con
), "Upgrade: TLS/1.0,HTTP/1.1\r\n");
1322 httpPrintf(HTTP(con
), "Content-Length: 0\r\n");
1323 httpPrintf(HTTP(con
), "\r\n");
1325 cupsdEncryptClient(con
);
1327 if (!cupsdSendError(con
, HTTP_NOT_IMPLEMENTED
))
1328 return (cupsdCloseClient(con
));
1329 #endif /* HAVE_SSL */
1332 if (con
->http
.expect
)
1334 /**** TODO: send expected header ****/
1337 if (!cupsdSendHeader(con
, HTTP_OK
, NULL
))
1338 return (cupsdCloseClient(con
));
1340 httpPrintf(HTTP(con
), "Allow: GET, HEAD, OPTIONS, POST, PUT\r\n");
1341 httpPrintf(HTTP(con
), "Content-Length: 0\r\n");
1342 httpPrintf(HTTP(con
), "\r\n");
1344 else if (!is_path_absolute(con
->uri
))
1347 * Protect against malicious users!
1350 if (!cupsdSendError(con
, HTTP_FORBIDDEN
))
1351 return (cupsdCloseClient(con
));
1355 if (!strcasecmp(con
->http
.fields
[HTTP_FIELD_CONNECTION
], "Upgrade") &&
1356 con
->http
.tls
== NULL
)
1360 * Do encryption stuff...
1363 if (!cupsdSendHeader(con
, HTTP_SWITCHING_PROTOCOLS
, NULL
))
1364 return (cupsdCloseClient(con
));
1366 httpPrintf(HTTP(con
), "Connection: Upgrade\r\n");
1367 httpPrintf(HTTP(con
), "Upgrade: TLS/1.0,HTTP/1.1\r\n");
1368 httpPrintf(HTTP(con
), "Content-Length: 0\r\n");
1369 httpPrintf(HTTP(con
), "\r\n");
1371 cupsdEncryptClient(con
);
1373 if (!cupsdSendError(con
, HTTP_NOT_IMPLEMENTED
))
1374 return (cupsdCloseClient(con
));
1375 #endif /* HAVE_SSL */
1378 if ((status
= cupsdIsAuthorized(con
, NULL
)) != HTTP_OK
)
1380 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1381 "cupsdReadClient: Unauthorized request for %s...\n",
1383 cupsdSendError(con
, status
);
1384 return (cupsdCloseClient(con
));
1387 if (con
->http
.expect
)
1389 /**** TODO: send expected header ****/
1392 switch (con
->http
.state
)
1394 case HTTP_GET_SEND
:
1395 if (!strncmp(con
->uri
, "/printers/", 10) &&
1396 !strcmp(con
->uri
+ strlen(con
->uri
) - 4, ".ppd"))
1399 * Send PPD file - get the real printer name since printer
1400 * names are not case sensitive but filenames can be...
1403 con
->uri
[strlen(con
->uri
) - 4] = '\0'; /* Drop ".ppd" */
1405 if ((p
= cupsdFindPrinter(con
->uri
+ 10)) != NULL
)
1406 snprintf(con
->uri
, sizeof(con
->uri
), "/ppd/%s.ppd", p
->name
);
1409 if (!cupsdSendError(con
, HTTP_NOT_FOUND
))
1410 return (cupsdCloseClient(con
));
1416 if ((!strncmp(con
->uri
, "/admin", 6) &&
1417 strncmp(con
->uri
, "/admin/conf/", 12) &&
1418 strncmp(con
->uri
, "/admin/log/", 11)) ||
1419 !strncmp(con
->uri
, "/printers", 9) ||
1420 !strncmp(con
->uri
, "/classes", 8) ||
1421 !strncmp(con
->uri
, "/help", 5) ||
1422 !strncmp(con
->uri
, "/jobs", 5))
1425 * Send CGI output...
1428 if (!strncmp(con
->uri
, "/admin", 6))
1430 cupsdSetStringf(&con
->command
, "%s/cgi-bin/admin.cgi",
1433 if ((ptr
= strchr(con
->uri
+ 6, '?')) != NULL
)
1434 cupsdSetStringf(&con
->options
, "admin%s", ptr
);
1436 cupsdSetString(&con
->options
, "admin");
1438 else if (!strncmp(con
->uri
, "/printers", 9))
1440 cupsdSetStringf(&con
->command
, "%s/cgi-bin/printers.cgi",
1442 cupsdSetString(&con
->options
, con
->uri
+ 9);
1444 else if (!strncmp(con
->uri
, "/classes", 8))
1446 cupsdSetStringf(&con
->command
, "%s/cgi-bin/classes.cgi",
1448 cupsdSetString(&con
->options
, con
->uri
+ 8);
1450 else if (!strncmp(con
->uri
, "/jobs", 5))
1452 cupsdSetStringf(&con
->command
, "%s/cgi-bin/jobs.cgi",
1454 cupsdSetString(&con
->options
, con
->uri
+ 5);
1458 cupsdSetStringf(&con
->command
, "%s/cgi-bin/help.cgi",
1460 cupsdSetString(&con
->options
, con
->uri
+ 5);
1463 if (con
->options
[0] == '/')
1464 _cups_strcpy(con
->options
, con
->options
+ 1);
1466 if (!cupsdSendCommand(con
, con
->command
, con
->options
, 0))
1468 if (!cupsdSendError(con
, HTTP_NOT_FOUND
))
1469 return (cupsdCloseClient(con
));
1472 cupsdLogRequest(con
, HTTP_OK
);
1474 if (con
->http
.version
<= HTTP_1_0
)
1475 con
->http
.keep_alive
= HTTP_KEEPALIVE_OFF
;
1477 else if ((!strncmp(con
->uri
, "/admin/conf/", 12) &&
1478 (strchr(con
->uri
+ 12, '/') ||
1479 strlen(con
->uri
) == 12)) ||
1480 (!strncmp(con
->uri
, "/admin/log/", 11) &&
1481 (strchr(con
->uri
+ 11, '/') ||
1482 strlen(con
->uri
) == 11)))
1485 * GET can only be done to configuration files under
1489 if (!cupsdSendError(con
, HTTP_FORBIDDEN
))
1490 return (cupsdCloseClient(con
));
1500 if ((filename
= get_file(con
, &filestats
, buf
,
1501 sizeof(buf
))) == NULL
)
1503 if (!cupsdSendError(con
, HTTP_NOT_FOUND
))
1504 return (cupsdCloseClient(con
));
1509 type
= mimeFileType(MimeDatabase
, filename
, NULL
, NULL
);
1511 if (cupsdIsCGI(con
, filename
, &filestats
, type
))
1514 * Note: con->command and con->options were set by
1518 if (!cupsdSendCommand(con
, con
->command
, con
->options
, 0))
1520 if (!cupsdSendError(con
, HTTP_NOT_FOUND
))
1521 return (cupsdCloseClient(con
));
1524 cupsdLogRequest(con
, HTTP_OK
);
1526 if (con
->http
.version
<= HTTP_1_0
)
1527 con
->http
.keep_alive
= HTTP_KEEPALIVE_OFF
;
1531 if (!check_if_modified(con
, &filestats
))
1533 if (!cupsdSendError(con
, HTTP_NOT_MODIFIED
))
1534 return (cupsdCloseClient(con
));
1539 strcpy(line
, "text/plain");
1541 snprintf(line
, sizeof(line
), "%s/%s", type
->super
, type
->type
);
1543 if (!cupsdSendFile(con
, HTTP_OK
, filename
, line
, &filestats
))
1544 return (cupsdCloseClient(con
));
1549 case HTTP_POST_RECV
:
1551 * See if the POST request includes a Content-Length field, and if
1552 * so check the length against any limits that are set...
1555 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "POST %s", con
->uri
);
1556 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "CONTENT_TYPE = %s",
1557 con
->http
.fields
[HTTP_FIELD_CONTENT_TYPE
]);
1559 if (con
->http
.fields
[HTTP_FIELD_CONTENT_LENGTH
][0] &&
1560 MaxRequestSize
> 0 &&
1561 con
->http
.data_remaining
> MaxRequestSize
)
1564 * Request too large...
1567 if (!cupsdSendError(con
, HTTP_REQUEST_TOO_LARGE
))
1568 return (cupsdCloseClient(con
));
1572 else if (con
->http
.data_remaining
< 0)
1575 * Negative content lengths are invalid!
1578 if (!cupsdSendError(con
, HTTP_BAD_REQUEST
))
1579 return (cupsdCloseClient(con
));
1585 * See what kind of POST request this is; for IPP requests the
1586 * content-type field will be "application/ipp"...
1589 if (!strcmp(con
->http
.fields
[HTTP_FIELD_CONTENT_TYPE
],
1591 con
->request
= ippNew();
1592 else if ((!strncmp(con
->uri
, "/admin", 6) &&
1593 strncmp(con
->uri
, "/admin/conf/", 12) &&
1594 strncmp(con
->uri
, "/admin/log/", 11)) ||
1595 !strncmp(con
->uri
, "/printers", 9) ||
1596 !strncmp(con
->uri
, "/classes", 8) ||
1597 !strncmp(con
->uri
, "/help", 5) ||
1598 !strncmp(con
->uri
, "/jobs", 5))
1604 if (!strncmp(con
->uri
, "/admin", 6))
1606 cupsdSetStringf(&con
->command
, "%s/cgi-bin/admin.cgi",
1609 if ((ptr
= strchr(con
->uri
+ 6, '?')) != NULL
)
1610 cupsdSetStringf(&con
->options
, "admin%s", ptr
);
1612 cupsdSetString(&con
->options
, "admin");
1614 else if (!strncmp(con
->uri
, "/printers", 9))
1616 cupsdSetStringf(&con
->command
, "%s/cgi-bin/printers.cgi",
1618 cupsdSetString(&con
->options
, con
->uri
+ 9);
1620 else if (!strncmp(con
->uri
, "/classes", 8))
1622 cupsdSetStringf(&con
->command
, "%s/cgi-bin/classes.cgi",
1624 cupsdSetString(&con
->options
, con
->uri
+ 8);
1626 else if (!strncmp(con
->uri
, "/jobs", 5))
1628 cupsdSetStringf(&con
->command
, "%s/cgi-bin/jobs.cgi",
1630 cupsdSetString(&con
->options
, con
->uri
+ 5);
1634 cupsdSetStringf(&con
->command
, "%s/cgi-bin/help.cgi",
1636 cupsdSetString(&con
->options
, con
->uri
+ 5);
1639 if (con
->options
[0] == '/')
1640 _cups_strcpy(con
->options
, con
->options
+ 1);
1642 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1643 "cupsdReadClient: %d command=\"%s\", options = \"%s\"",
1644 con
->http
.fd
, con
->command
, con
->options
);
1646 if (con
->http
.version
<= HTTP_1_0
)
1647 con
->http
.keep_alive
= HTTP_KEEPALIVE_OFF
;
1655 if ((filename
= get_file(con
, &filestats
, buf
,
1656 sizeof(buf
))) == NULL
)
1658 if (!cupsdSendError(con
, HTTP_NOT_FOUND
))
1659 return (cupsdCloseClient(con
));
1664 type
= mimeFileType(MimeDatabase
, filename
, NULL
, NULL
);
1666 if (!cupsdIsCGI(con
, filename
, &filestats
, type
))
1669 * Only POST to CGI's...
1672 if (!cupsdSendError(con
, HTTP_UNAUTHORIZED
))
1673 return (cupsdCloseClient(con
));
1678 case HTTP_PUT_RECV
:
1680 * Validate the resource name...
1683 if (strncmp(con
->uri
, "/admin/conf/", 12) ||
1684 strchr(con
->uri
+ 12, '/') ||
1685 strlen(con
->uri
) == 12)
1688 * PUT can only be done to configuration files under
1692 if (!cupsdSendError(con
, HTTP_FORBIDDEN
))
1693 return (cupsdCloseClient(con
));
1699 * See if the PUT request includes a Content-Length field, and if
1700 * so check the length against any limits that are set...
1703 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "PUT %s", con
->uri
);
1704 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "CONTENT_TYPE = %s",
1705 con
->http
.fields
[HTTP_FIELD_CONTENT_TYPE
]);
1707 if (con
->http
.fields
[HTTP_FIELD_CONTENT_LENGTH
][0] &&
1708 MaxRequestSize
> 0 &&
1709 con
->http
.data_remaining
> MaxRequestSize
)
1712 * Request too large...
1715 if (!cupsdSendError(con
, HTTP_REQUEST_TOO_LARGE
))
1716 return (cupsdCloseClient(con
));
1720 else if (con
->http
.data_remaining
< 0)
1723 * Negative content lengths are invalid!
1726 if (!cupsdSendError(con
, HTTP_BAD_REQUEST
))
1727 return (cupsdCloseClient(con
));
1733 * Open a temporary file to hold the request...
1736 cupsdSetStringf(&con
->filename
, "%s/%08x", RequestRoot
,
1738 con
->file
= open(con
->filename
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0640);
1740 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1741 "cupsdReadClient: %d REQUEST %s=%d", con
->http
.fd
,
1742 con
->filename
, con
->file
);
1746 if (!cupsdSendError(con
, HTTP_REQUEST_TOO_LARGE
))
1747 return (cupsdCloseClient(con
));
1750 fchmod(con
->file
, 0640);
1751 fchown(con
->file
, RunUser
, Group
);
1752 fcntl(con
->file
, F_SETFD
, fcntl(con
->file
, F_GETFD
) | FD_CLOEXEC
);
1757 cupsdSendError(con
, HTTP_NOT_IMPLEMENTED
);
1758 return (cupsdCloseClient(con
));
1761 if (!strncmp(con
->uri
, "/printers/", 10) &&
1762 !strcmp(con
->uri
+ strlen(con
->uri
) - 4, ".ppd"))
1765 * Send PPD file - get the real printer name since printer
1766 * names are not case sensitive but filenames can be...
1769 con
->uri
[strlen(con
->uri
) - 4] = '\0'; /* Drop ".ppd" */
1771 if ((p
= cupsdFindPrinter(con
->uri
+ 10)) != NULL
)
1772 snprintf(con
->uri
, sizeof(con
->uri
), "/ppd/%s.ppd", p
->name
);
1775 if (!cupsdSendError(con
, HTTP_NOT_FOUND
))
1776 return (cupsdCloseClient(con
));
1782 if ((!strncmp(con
->uri
, "/admin", 6) &&
1783 strncmp(con
->uri
, "/admin/conf/", 12) &&
1784 strncmp(con
->uri
, "/admin/log/", 11)) ||
1785 !strncmp(con
->uri
, "/printers", 9) ||
1786 !strncmp(con
->uri
, "/classes", 8) ||
1787 !strncmp(con
->uri
, "/help", 5) ||
1788 !strncmp(con
->uri
, "/jobs", 5))
1794 if (!cupsdSendHeader(con
, HTTP_OK
, "text/html"))
1795 return (cupsdCloseClient(con
));
1797 if (httpPrintf(HTTP(con
), "\r\n") < 0)
1798 return (cupsdCloseClient(con
));
1800 cupsdLogRequest(con
, HTTP_OK
);
1802 else if ((!strncmp(con
->uri
, "/admin/conf/", 12) &&
1803 (strchr(con
->uri
+ 12, '/') ||
1804 strlen(con
->uri
) == 12)) ||
1805 (!strncmp(con
->uri
, "/admin/log/", 11) &&
1806 (strchr(con
->uri
+ 11, '/') ||
1807 strlen(con
->uri
) == 11)))
1810 * HEAD can only be done to configuration files under
1814 if (!cupsdSendError(con
, HTTP_FORBIDDEN
))
1815 return (cupsdCloseClient(con
));
1819 else if ((filename
= get_file(con
, &filestats
, buf
,
1820 sizeof(buf
))) == NULL
)
1822 if (!cupsdSendHeader(con
, HTTP_NOT_FOUND
, "text/html"))
1823 return (cupsdCloseClient(con
));
1825 cupsdLogRequest(con
, HTTP_NOT_FOUND
);
1827 else if (!check_if_modified(con
, &filestats
))
1829 if (!cupsdSendError(con
, HTTP_NOT_MODIFIED
))
1830 return (cupsdCloseClient(con
));
1832 cupsdLogRequest(con
, HTTP_NOT_MODIFIED
);
1840 type
= mimeFileType(MimeDatabase
, filename
, NULL
, NULL
);
1842 strcpy(line
, "text/plain");
1844 snprintf(line
, sizeof(line
), "%s/%s", type
->super
, type
->type
);
1846 if (!cupsdSendHeader(con
, HTTP_OK
, line
))
1847 return (cupsdCloseClient(con
));
1849 if (httpPrintf(HTTP(con
), "Last-Modified: %s\r\n",
1850 httpGetDateString(filestats
.st_mtime
)) < 0)
1851 return (cupsdCloseClient(con
));
1853 if (httpPrintf(HTTP(con
), "Content-Length: %lu\r\n",
1854 (unsigned long)filestats
.st_size
) < 0)
1855 return (cupsdCloseClient(con
));
1857 cupsdLogRequest(con
, HTTP_OK
);
1860 if (httpPrintf(HTTP(con
), "\r\n") < 0)
1861 return (cupsdCloseClient(con
));
1863 con
->http
.state
= HTTP_WAITING
;
1867 break; /* Anti-compiler-warning-code */
1873 * Handle any incoming data...
1876 switch (con
->http
.state
)
1878 case HTTP_PUT_RECV
:
1879 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1880 "cupsdReadClient: %d con->data_encoding=HTTP_ENCODE_%s, "
1881 "con->data_remaining=" CUPS_LLFMT
", con->file=%d",
1883 con
->http
.data_encoding
== HTTP_ENCODE_CHUNKED
?
1884 "CHUNKED" : "LENGTH",
1885 CUPS_LLCAST con
->http
.data_remaining
, con
->file
);
1887 if ((bytes
= httpRead2(HTTP(con
), line
, sizeof(line
))) < 0)
1888 return (cupsdCloseClient(con
));
1891 con
->bytes
+= bytes
;
1893 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1894 "cupsdReadClient: %d writing %d bytes to %d",
1895 con
->http
.fd
, bytes
, con
->file
);
1897 if (write(con
->file
, line
, bytes
) < bytes
)
1899 cupsdLogMessage(CUPSD_LOG_ERROR
,
1900 "cupsdReadClient: Unable to write %d bytes to %s: %s",
1901 bytes
, con
->filename
, strerror(errno
));
1903 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1904 "cupsdReadClient: Closing data file %d...",
1909 unlink(con
->filename
);
1910 cupsdClearString(&con
->filename
);
1912 if (!cupsdSendError(con
, HTTP_REQUEST_TOO_LARGE
))
1913 return (cupsdCloseClient(con
));
1917 if (con
->http
.state
== HTTP_WAITING
)
1920 * End of file, see how big it is...
1923 fstat(con
->file
, &filestats
);
1925 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1926 "cupsdReadClient: %d Closing data file %d, size="
1928 con
->http
.fd
, con
->file
,
1929 CUPS_LLCAST filestats
.st_size
);
1934 if (filestats
.st_size
> MaxRequestSize
&&
1938 * Request is too big; remove it and send an error...
1941 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1942 "cupsdReadClient: %d Removing temp file %s",
1943 con
->http
.fd
, con
->filename
);
1944 unlink(con
->filename
);
1945 cupsdClearString(&con
->filename
);
1947 if (!cupsdSendError(con
, HTTP_REQUEST_TOO_LARGE
))
1948 return (cupsdCloseClient(con
));
1952 * Install the configuration file...
1955 status
= install_conf_file(con
);
1958 * Return the status to the client...
1961 if (!cupsdSendError(con
, status
))
1962 return (cupsdCloseClient(con
));
1966 case HTTP_POST_RECV
:
1967 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1968 "cupsdReadClient: %d con->data_encoding=HTTP_ENCODE_"
1969 "%s, con->data_remaining=" CUPS_LLFMT
", con->file=%d",
1971 con
->http
.data_encoding
== HTTP_ENCODE_CHUNKED
?
1972 "CHUNKED" : "LENGTH",
1973 CUPS_LLCAST con
->http
.data_remaining
, con
->file
);
1975 if (con
->request
!= NULL
)
1978 * Grab any request data from the connection...
1981 if ((ipp_state
= ippRead(&(con
->http
), con
->request
)) == IPP_ERROR
)
1983 cupsdLogMessage(CUPSD_LOG_ERROR
,
1984 "cupsdReadClient: %d IPP Read Error!",
1987 cupsdSendError(con
, HTTP_BAD_REQUEST
);
1988 return (cupsdCloseClient(con
));
1990 else if (ipp_state
!= IPP_DATA
)
1992 if (con
->http
.state
== HTTP_POST_SEND
)
1994 cupsdSendError(con
, HTTP_BAD_REQUEST
);
1995 return (cupsdCloseClient(con
));
2001 con
->bytes
+= ippLength(con
->request
);
2004 if (con
->file
< 0 && con
->http
.state
!= HTTP_POST_SEND
)
2007 * Create a file as needed for the request data...
2010 cupsdSetStringf(&con
->filename
, "%s/%08x", RequestRoot
, request_id
++);
2011 con
->file
= open(con
->filename
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0640);
2013 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdReadClient: %d REQUEST %s=%d", con
->http
.fd
,
2014 con
->filename
, con
->file
);
2018 if (!cupsdSendError(con
, HTTP_REQUEST_TOO_LARGE
))
2019 return (cupsdCloseClient(con
));
2022 fchmod(con
->file
, 0640);
2023 fchown(con
->file
, RunUser
, Group
);
2024 fcntl(con
->file
, F_SETFD
, fcntl(con
->file
, F_GETFD
) | FD_CLOEXEC
);
2027 if (con
->http
.state
!= HTTP_POST_SEND
)
2029 if ((bytes
= httpRead2(HTTP(con
), line
, sizeof(line
))) < 0)
2030 return (cupsdCloseClient(con
));
2033 con
->bytes
+= bytes
;
2035 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2036 "cupsdReadClient: %d writing %d bytes to %d",
2037 con
->http
.fd
, bytes
, con
->file
);
2039 if (write(con
->file
, line
, bytes
) < bytes
)
2041 cupsdLogMessage(CUPSD_LOG_ERROR
,
2042 "cupsdReadClient: Unable to write %d bytes to %s: %s",
2043 bytes
, con
->filename
, strerror(errno
));
2045 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2046 "cupsdReadClient: Closing file %d...",
2051 unlink(con
->filename
);
2052 cupsdClearString(&con
->filename
);
2054 if (!cupsdSendError(con
, HTTP_REQUEST_TOO_LARGE
))
2055 return (cupsdCloseClient(con
));
2058 else if (con
->http
.state
== HTTP_POST_RECV
)
2059 return (1); /* ??? */
2060 else if (con
->http
.state
!= HTTP_POST_SEND
)
2061 return (cupsdCloseClient(con
));
2064 if (con
->http
.state
== HTTP_POST_SEND
)
2068 fstat(con
->file
, &filestats
);
2070 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2071 "cupsdReadClient: %d Closing data file %d, "
2072 "size=" CUPS_LLFMT
".",
2073 con
->http
.fd
, con
->file
,
2074 CUPS_LLCAST filestats
.st_size
);
2079 if (filestats
.st_size
> MaxRequestSize
&&
2083 * Request is too big; remove it and send an error...
2086 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2087 "cupsdReadClient: %d Removing temp file %s",
2088 con
->http
.fd
, con
->filename
);
2089 unlink(con
->filename
);
2090 cupsdClearString(&con
->filename
);
2095 * Delete any IPP request data...
2098 ippDelete(con
->request
);
2099 con
->request
= NULL
;
2102 if (!cupsdSendError(con
, HTTP_REQUEST_TOO_LARGE
))
2103 return (cupsdCloseClient(con
));
2108 if (!cupsdSendCommand(con
, con
->command
, con
->options
, 0))
2110 if (!cupsdSendError(con
, HTTP_NOT_FOUND
))
2111 return (cupsdCloseClient(con
));
2114 cupsdLogRequest(con
, HTTP_OK
);
2119 return (cupsdProcessIPPRequest(con
));
2124 break; /* Anti-compiler-warning-code */
2127 if (!con
->http
.keep_alive
&& con
->http
.state
== HTTP_WAITING
)
2128 return (cupsdCloseClient(con
));
2135 * 'cupsdSendCommand()' - Send output from a command via HTTP.
2138 int /* O - 1 on success, 0 on failure */
2140 cupsd_client_t
*con
, /* I - Client connection */
2141 char *command
, /* I - Command to run */
2142 char *options
, /* I - Command-line options */
2143 int root
) /* I - Run as root? */
2145 int fd
; /* Standard input file descriptor */
2149 fd
= open(con
->filename
, O_RDONLY
);
2151 fd
= open("/dev/null", O_RDONLY
);
2155 cupsdLogMessage(CUPSD_LOG_ERROR
,
2156 "cupsdSendCommand: %d Unable to open \"%s\" for reading: %s",
2157 con
->http
.fd
, con
->filename
? con
->filename
: "/dev/null",
2162 fcntl(fd
, F_SETFD
, fcntl(fd
, F_GETFD
) | FD_CLOEXEC
);
2164 con
->pipe_pid
= pipe_command(con
, fd
, &(con
->file
), command
, options
, root
);
2168 cupsdLogMessage(CUPSD_LOG_INFO
, "Started \"%s\" (pid=%d)", command
,
2171 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdSendCommand: %d file=%d",
2172 con
->http
.fd
, con
->file
);
2174 if (con
->pipe_pid
== 0)
2177 fcntl(con
->file
, F_SETFD
, fcntl(con
->file
, F_GETFD
) | FD_CLOEXEC
);
2179 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2180 "cupsdSendCommand: Adding fd %d to InputSet...", con
->file
);
2181 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2182 "cupsdSendCommand: Adding fd %d to OutputSet...",
2185 FD_SET(con
->file
, InputSet
);
2186 FD_SET(con
->http
.fd
, OutputSet
);
2188 con
->sent_header
= 0;
2189 con
->file_ready
= 0;
2190 con
->got_fields
= 0;
2198 * 'cupsdSendError()' - Send an error message via HTTP.
2201 int /* O - 1 if successful, 0 otherwise */
2202 cupsdSendError(cupsd_client_t
*con
, /* I - Connection */
2203 http_status_t code
) /* I - Error code */
2206 * Put the request in the access_log file...
2209 cupsdLogRequest(con
, code
);
2211 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdSendError: %d code=%d (%s)",
2212 con
->http
.fd
, code
, httpStatus(code
));
2215 * To work around bugs in some proxies, don't use Keep-Alive for some
2219 if (code
>= HTTP_BAD_REQUEST
)
2220 con
->http
.keep_alive
= HTTP_KEEPALIVE_OFF
;
2223 * Send an error message back to the client. If the error code is a
2224 * 400 or 500 series, make sure the message contains some text, too!
2227 if (!cupsdSendHeader(con
, code
, NULL
))
2231 if (code
== HTTP_UPGRADE_REQUIRED
)
2232 if (httpPrintf(HTTP(con
), "Connection: Upgrade\r\n") < 0)
2235 if (httpPrintf(HTTP(con
), "Upgrade: TLS/1.0,HTTP/1.1\r\n") < 0)
2237 #endif /* HAVE_SSL */
2239 if ((con
->http
.version
>= HTTP_1_1
&& !con
->http
.keep_alive
) ||
2240 (code
>= HTTP_BAD_REQUEST
&& code
!= HTTP_UPGRADE_REQUIRED
))
2242 if (httpPrintf(HTTP(con
), "Connection: close\r\n") < 0)
2246 if (code
>= HTTP_BAD_REQUEST
)
2249 * Send a human-readable error message.
2252 char message
[1024]; /* Message for user */
2253 const char *text
; /* Status-specific text */
2255 if (code
== HTTP_UNAUTHORIZED
)
2256 text
= _cupsLangString(con
->language
,
2257 _("Enter your username and password or the "
2258 "root username and password to access this "
2260 else if (code
== HTTP_UPGRADE_REQUIRED
)
2261 text
= _cupsLangString(con
->language
,
2262 _("You must use a https: URL to access this "
2267 snprintf(message
, sizeof(message
),
2268 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\" "
2269 "\"http://www.w3.org/TR/REC-html40/loose.dtd\">\n"
2272 "\t<META HTTP-EQUIV=\"Content-Type\" "
2273 "CONTENT=\"text/html; charset=utf-8\">\n"
2274 "\t<TITLE>%d %s</TITLE>\n"
2275 "\t<LINK REL=\"STYLESHEET\" TYPE=\"text/css\" "
2276 "HREF=\"/cups.css\">\n"
2283 code
, httpStatus(code
), code
, httpStatus(code
), text
);
2285 if (httpPrintf(HTTP(con
), "Content-Type: text/html; charset=utf-8\r\n") < 0)
2287 if (httpPrintf(HTTP(con
), "Content-Length: %d\r\n",
2288 (int)strlen(message
)) < 0)
2290 if (httpPrintf(HTTP(con
), "\r\n") < 0)
2292 if (httpPrintf(HTTP(con
), "%s", message
) < 0)
2295 else if (httpPrintf(HTTP(con
), "\r\n") < 0)
2298 con
->http
.state
= HTTP_WAITING
;
2305 * 'cupsdSendFile()' - Send a file via HTTP.
2308 int /* O - 0 on failure, 1 on success */
2309 cupsdSendFile(cupsd_client_t
*con
, /* I - Client connection */
2310 http_status_t code
, /* I - HTTP status */
2311 char *filename
, /* I - Filename */
2312 char *type
, /* I - File type */
2313 struct stat
*filestats
)/* O - File information */
2315 con
->file
= open(filename
, O_RDONLY
);
2317 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdSendFile: %d file=%d", con
->http
.fd
,
2323 fcntl(con
->file
, F_SETFD
, fcntl(con
->file
, F_GETFD
) | FD_CLOEXEC
);
2327 if (!cupsdSendHeader(con
, code
, type
))
2330 if (httpPrintf(HTTP(con
), "Last-Modified: %s\r\n",
2331 httpGetDateString(filestats
->st_mtime
)) < 0)
2333 if (httpPrintf(HTTP(con
), "Content-Length: " CUPS_LLFMT
"\r\n",
2334 CUPS_LLCAST filestats
->st_size
) < 0)
2336 if (httpPrintf(HTTP(con
), "\r\n") < 0)
2339 con
->http
.data_encoding
= HTTP_ENCODE_LENGTH
;
2340 con
->http
.data_remaining
= filestats
->st_size
;
2342 if (con
->http
.data_remaining
<= INT_MAX
)
2343 con
->http
._data_remaining
= con
->http
.data_remaining
;
2345 con
->http
._data_remaining
= INT_MAX
;
2347 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2348 "cupsdSendFile: Adding fd %d to OutputSet...", con
->http
.fd
);
2350 FD_SET(con
->http
.fd
, OutputSet
);
2357 * 'cupsdSendHeader()' - Send an HTTP request.
2360 int /* O - 1 on success, 0 on failure */
2361 cupsdSendHeader(cupsd_client_t
*con
, /* I - Client to send to */
2362 http_status_t code
, /* I - HTTP status code */
2363 char *type
) /* I - MIME type of document */
2365 if (httpPrintf(HTTP(con
), "HTTP/%d.%d %d %s\r\n", con
->http
.version
/ 100,
2366 con
->http
.version
% 100, code
, httpStatus(code
)) < 0)
2368 if (httpPrintf(HTTP(con
), "Date: %s\r\n", httpGetDateString(time(NULL
))) < 0)
2371 if (httpPrintf(HTTP(con
), "Server: %s\r\n", ServerHeader
) < 0)
2373 if (con
->http
.keep_alive
&& con
->http
.version
>= HTTP_1_0
)
2375 if (httpPrintf(HTTP(con
), "Connection: Keep-Alive\r\n") < 0)
2377 if (httpPrintf(HTTP(con
), "Keep-Alive: timeout=%d\r\n",
2378 KeepAliveTimeout
) < 0)
2381 if (code
== HTTP_METHOD_NOT_ALLOWED
)
2382 if (httpPrintf(HTTP(con
), "Allow: GET, HEAD, OPTIONS, POST\r\n") < 0)
2385 if (code
== HTTP_UNAUTHORIZED
)
2387 int auth_type
; /* Authentication type */
2390 if (!con
->best
|| con
->best
->type
== AUTH_NONE
)
2391 auth_type
= DefaultAuthType
;
2393 auth_type
= con
->best
->type
;
2395 if (auth_type
!= AUTH_DIGEST
)
2397 if (httpPrintf(HTTP(con
),
2398 "WWW-Authenticate: Basic realm=\"CUPS\"\r\n") < 0)
2403 if (httpPrintf(HTTP(con
),
2404 "WWW-Authenticate: Digest realm=\"CUPS\", nonce=\"%s\"\r\n",
2405 con
->http
.hostname
) < 0)
2410 if (con
->language
!= NULL
)
2412 if (httpPrintf(HTTP(con
), "Content-Language: %s\r\n",
2413 con
->language
->language
) < 0)
2419 if (!strcmp(type
, "text/html"))
2421 if (httpPrintf(HTTP(con
),
2422 "Content-Type: text/html; charset=utf-8\r\n") < 0)
2425 else if (httpPrintf(HTTP(con
), "Content-Type: %s\r\n", type
) < 0)
2434 * 'cupsdUpdateCGI()' - Read status messages from CGI scripts and programs.
2438 cupsdUpdateCGI(void)
2440 char *ptr
, /* Pointer to end of line in buffer */
2441 message
[1024]; /* Pointer to message text */
2442 int loglevel
; /* Log level for message */
2445 while ((ptr
= cupsdStatBufUpdate(CGIStatusBuffer
, &loglevel
,
2446 message
, sizeof(message
))) != NULL
)
2447 if (!strchr(CGIStatusBuffer
->buffer
, '\n'))
2450 if (ptr
== NULL
&& errno
)
2453 * Fatal error on pipe - should never happen!
2456 cupsdLogMessage(CUPSD_LOG_CRIT
,
2457 "cupsdUpdateCGI: error reading from CGI error pipe - %s",
2464 * 'cupsdWriteClient()' - Write data to a client as needed.
2467 int /* O - 1 if success, 0 if fail */
2468 cupsdWriteClient(cupsd_client_t
*con
) /* I - Client connection */
2470 int bytes
; /* Number of bytes written */
2471 char buf
[16385]; /* Data buffer */
2472 char *bufptr
; /* Pointer into buffer */
2473 ipp_state_t ipp_state
; /* IPP state value */
2477 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2478 "cupsdWriteClient(con=%p) %d response=%p, file=%d "
2479 "pipe_pid=%d state=%d",
2480 con
, con
->http
.fd
, con
->response
, con
->file
, con
->pipe_pid
,
2484 if (con
->http
.state
!= HTTP_GET_SEND
&&
2485 con
->http
.state
!= HTTP_POST_SEND
)
2488 if (con
->response
!= NULL
)
2490 ipp_state
= ippWrite(&(con
->http
), con
->response
);
2491 bytes
= ipp_state
!= IPP_ERROR
&& ipp_state
!= IPP_DATA
;
2493 else if ((bytes
= read(con
->file
, buf
, sizeof(buf
) - 1)) > 0)
2495 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2496 "cupsdWriteClient: Read %d bytes from file %d...",
2499 if (con
->pipe_pid
&& !con
->got_fields
)
2502 * Inspect the data for Content-Type and other fields.
2507 for (bufptr
= buf
; !con
->got_fields
&& *bufptr
; bufptr
++)
2508 if (*bufptr
== '\n')
2511 * Send line to client...
2514 if (bufptr
> buf
&& bufptr
[-1] == '\r')
2518 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "Script header: %s", buf
);
2520 if (!con
->sent_header
)
2523 * Handle redirection and CGI status codes...
2526 if (!strncasecmp(buf
, "Location:", 9))
2527 cupsdSendHeader(con
, HTTP_SEE_OTHER
, NULL
);
2528 else if (!strncasecmp(buf
, "Status:", 7))
2529 cupsdSendHeader(con
, atoi(buf
+ 7), NULL
);
2531 cupsdSendHeader(con
, HTTP_OK
, NULL
);
2533 if (con
->http
.version
== HTTP_1_1
)
2535 con
->http
.data_encoding
= HTTP_ENCODE_CHUNKED
;
2537 if (httpPrintf(HTTP(con
), "Transfer-Encoding: chunked\r\n") < 0)
2541 con
->sent_header
= 1;
2544 if (strncasecmp(buf
, "Status:", 7))
2545 httpPrintf(HTTP(con
), "%s\r\n", buf
);
2547 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdWriteClient: %d %s",
2554 bytes
-= (bufptr
- buf
);
2555 memmove(buf
, bufptr
, bytes
+ 1);
2559 * See if the line was empty...
2562 if (con
->field_col
== 0)
2563 con
->got_fields
= 1;
2567 else if (*bufptr
!= '\r')
2570 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2571 "cupsdWriteClient: %d bytes=%d, got_fields=%d",
2572 con
->http
.fd
, bytes
, con
->got_fields
);
2574 if (bytes
> 0 && !con
->got_fields
)
2577 * Remaining text needs to go out...
2580 httpPrintf(HTTP(con
), "%s", buf
);
2582 con
->http
.activity
= time(NULL
);
2585 else if (bytes
== 0)
2587 con
->http
.activity
= time(NULL
);
2592 if (httpWrite2(HTTP(con
), buf
, bytes
) < 0)
2594 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2595 "cupsdWriteClient: %d Write of %d bytes failed!",
2596 con
->http
.fd
, bytes
);
2598 cupsdCloseClient(con
);
2602 con
->bytes
+= bytes
;
2604 if (con
->http
.state
== HTTP_WAITING
)
2610 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdWriteClient: %d bytes < 0",
2613 cupsdLogRequest(con
, HTTP_OK
);
2615 httpFlushWrite(HTTP(con
));
2617 if (con
->http
.data_encoding
== HTTP_ENCODE_CHUNKED
)
2619 if (httpPrintf(HTTP(con
), "0\r\n\r\n") < 0)
2621 cupsdCloseClient(con
);
2626 con
->http
.state
= HTTP_WAITING
;
2628 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2629 "cupsdWriteClient: Removing fd %d from OutputSet...",
2632 FD_CLR(con
->http
.fd
, OutputSet
);
2636 if (FD_ISSET(con
->file
, InputSet
))
2638 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2639 "cupsdWriteClient: Removing fd %d from InputSet...",
2641 FD_CLR(con
->file
, InputSet
);
2645 cupsdEndProcess(con
->pipe_pid
, 0);
2647 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2648 "cupsdWriteClient: %d Closing data file %d.",
2649 con
->http
.fd
, con
->file
);
2658 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2659 "cupsdWriteClient: %d Removing temp file %s",
2660 con
->http
.fd
, con
->filename
);
2661 unlink(con
->filename
);
2662 cupsdClearString(&con
->filename
);
2665 if (con
->request
!= NULL
)
2667 ippDelete(con
->request
);
2668 con
->request
= NULL
;
2671 if (con
->response
!= NULL
)
2673 ippDelete(con
->response
);
2674 con
->response
= NULL
;
2677 cupsdClearString(&con
->command
);
2678 cupsdClearString(&con
->options
);
2680 if (!con
->http
.keep_alive
)
2682 cupsdCloseClient(con
);
2688 con
->file_ready
= 0;
2690 if (con
->pipe_pid
&& !FD_ISSET(con
->file
, InputSet
))
2692 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2693 "cupsdWriteClient: Adding fd %d to InputSet...",
2695 FD_SET(con
->file
, InputSet
);
2699 con
->http
.activity
= time(NULL
);
2706 * 'check_if_modified()' - Decode an "If-Modified-Since" line.
2709 static int /* O - 1 if modified since */
2711 cupsd_client_t
*con
, /* I - Client connection */
2712 struct stat
*filestats
) /* I - File information */
2714 char *ptr
; /* Pointer into field */
2715 time_t date
; /* Time/date value */
2716 off_t size
; /* Size/length value */
2721 ptr
= con
->http
.fields
[HTTP_FIELD_IF_MODIFIED_SINCE
];
2726 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2727 "check_if_modified: %d If-Modified-Since=\"%s\"",
2730 while (*ptr
!= '\0')
2732 while (isspace(*ptr
) || *ptr
== ';')
2735 if (strncasecmp(ptr
, "length=", 7) == 0)
2738 size
= strtoll(ptr
, NULL
, 10);
2740 while (isdigit(*ptr
))
2743 else if (isalpha(*ptr
))
2745 date
= httpGetDateTime(ptr
);
2746 while (*ptr
!= '\0' && *ptr
!= ';')
2751 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2752 "check_if_modified: %d sizes=" CUPS_LLFMT
","
2753 CUPS_LLFMT
" dates=%d,%d",
2754 con
->http
.fd
, CUPS_LLCAST size
,
2755 CUPS_LLCAST filestats
->st_size
, (int)date
,
2756 (int)filestats
->st_mtime
);
2758 return ((size
!= filestats
->st_size
&& size
!= 0) ||
2759 (date
< filestats
->st_mtime
&& date
!= 0) ||
2760 (size
== 0 && date
== 0));
2766 * 'get_cdsa_server_certs()' - Convert a keychain name into the CFArrayRef
2767 * required by SSLSetCertificate.
2769 * For now we assumes that there is exactly one SecIdentity in the
2770 * keychain - i.e. there is exactly one matching cert/private key pair.
2771 * In the future we will search a keychain for a SecIdentity matching a
2772 * specific criteria. We also skip the operation of adding additional
2773 * non-signing certs from the keychain to the CFArrayRef.
2775 * To create a self-signed certificate for testing use the certtool.
2776 * Executing the following as root will do it:
2778 * certtool c c v k=CUPS
2781 static CFArrayRef
/* O - Array of certificates */
2782 get_cdsa_server_certs(void)
2784 OSStatus err
; /* Error info */
2785 SecKeychainRef kcRef
; /* Keychain reference */
2786 SecIdentitySearchRef srchRef
; /* Search reference */
2787 SecIdentityRef identity
; /* Identity */
2788 CFArrayRef ca
; /* Certificate array */
2795 err
= SecKeychainOpen(ServerCertificate
, &kcRef
);
2798 cupsdLogMessage(CUPSD_LOG_ERROR
, "Cannot open keychain \"%s\", error %d.",
2799 ServerCertificate
, (int)err
);
2803 * Search for "any" identity matching specified key use;
2804 * in this app, we expect there to be exactly one.
2807 err
= SecIdentitySearchCreate(kcRef
, CSSM_KEYUSE_SIGN
, &srchRef
);
2810 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2811 "Cannot find signing key in keychain \"%s\", error %d",
2812 ServerCertificate
, (int)err
);
2815 err
= SecIdentitySearchCopyNext(srchRef
, &identity
);
2818 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2819 "Cannot find signing key in keychain \"%s\", error %d",
2820 ServerCertificate
, (int)err
);
2823 if (CFGetTypeID(identity
) != SecIdentityGetTypeID())
2824 cupsdLogMessage(CUPSD_LOG_ERROR
,
2825 "SecIdentitySearchCopyNext CFTypeID failure!");
2829 * Found one. Place it in a CFArray.
2830 * TBD: snag other (non-identity) certs from keychain and add them
2834 ca
= CFArrayCreate(NULL
, (const void **)&identity
, 1, NULL
);
2837 cupsdLogMessage(CUPSD_LOG_ERROR
, "CFArrayCreate error");
2840 /*CFRelease(identity);*/
2843 /*CFRelease(srchRef);*/
2846 /*CFRelease(kcRef);*/
2851 #endif /* HAVE_CDSASSL */
2855 * 'get_file()' - Get a filename and state info.
2858 static char * /* O - Real filename */
2859 get_file(cupsd_client_t
*con
, /* I - Client connection */
2860 struct stat
*filestats
, /* O - File information */
2861 char *filename
, /* IO - Filename buffer */
2862 int len
) /* I - Buffer length */
2864 int status
; /* Status of filesystem calls */
2865 char *ptr
; /* Pointer info filename */
2866 int plen
; /* Remaining length after pointer */
2870 * Need to add DocumentRoot global...
2873 if (!strncmp(con
->uri
, "/ppd/", 5))
2874 snprintf(filename
, len
, "%s%s", ServerRoot
, con
->uri
);
2875 else if (!strncmp(con
->uri
, "/admin/conf/", 12))
2876 snprintf(filename
, len
, "%s%s", ServerRoot
, con
->uri
+ 11);
2877 else if (!strncmp(con
->uri
, "/admin/log/", 11))
2879 if (!strcmp(con
->uri
+ 11, "access_log") && AccessLog
[0] == '/')
2880 strlcpy(filename
, AccessLog
, len
);
2881 else if (!strcmp(con
->uri
+ 11, "error_log") && ErrorLog
[0] == '/')
2882 strlcpy(filename
, ErrorLog
, len
);
2883 else if (!strcmp(con
->uri
+ 11, "page_log") && PageLog
[0] == '/')
2884 strlcpy(filename
, PageLog
, len
);
2888 else if (con
->language
!= NULL
)
2889 snprintf(filename
, len
, "%s/%s%s", DocumentRoot
, con
->language
->language
,
2892 snprintf(filename
, len
, "%s%s", DocumentRoot
, con
->uri
);
2894 if ((ptr
= strchr(filename
, '?')) != NULL
)
2898 * Grab the status for this language; if there isn't a language-specific file
2899 * then fallback to the default one...
2902 if ((status
= stat(filename
, filestats
)) != 0 && con
->language
!= NULL
)
2905 * Drop the language prefix and try the current directory...
2908 if (strncmp(con
->uri
, "/ppd/", 5) &&
2909 strncmp(con
->uri
, "/admin/conf/", 12) &&
2910 strncmp(con
->uri
, "/admin/log/", 11))
2912 snprintf(filename
, len
, "%s%s", DocumentRoot
, con
->uri
);
2914 if ((ptr
= strchr(filename
, '?')) != NULL
)
2917 status
= stat(filename
, filestats
);
2922 * If we're found a directory, get the index.html file instead...
2925 if (!status
&& S_ISDIR(filestats
->st_mode
))
2927 if (filename
[strlen(filename
) - 1] != '/')
2928 strlcat(filename
, "/", len
);
2930 ptr
= filename
+ strlen(filename
);
2931 plen
= len
- (ptr
- filename
);
2933 strlcpy(ptr
, "index.html", plen
);
2934 status
= stat(filename
, filestats
);
2939 strlcpy(ptr
, "index.class", plen
);
2940 status
= stat(filename
, filestats
);
2942 #endif /* HAVE_JAVA */
2947 strlcpy(ptr
, "index.pl", plen
);
2948 status
= stat(filename
, filestats
);
2950 #endif /* HAVE_PERL */
2955 strlcpy(ptr
, "index.php", plen
);
2956 status
= stat(filename
, filestats
);
2958 #endif /* HAVE_PHP */
2963 strlcpy(ptr
, "index.pyc", plen
);
2964 status
= stat(filename
, filestats
);
2969 strlcpy(ptr
, "index.py", plen
);
2970 status
= stat(filename
, filestats
);
2972 #endif /* HAVE_PYTHON */
2975 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_file: %d filename=%s size=%d",
2976 con
->http
.fd
, filename
,
2977 status
? -1 : (int)filestats
->st_size
);
2980 con
->http
.data_remaining
= (int)filestats
->st_size
;
2990 * 'install_conf_file()' - Install a configuration file.
2993 static http_status_t
/* O - Status */
2994 install_conf_file(cupsd_client_t
*con
) /* I - Connection */
2996 cups_file_t
*in
, /* Input file */
2997 *out
; /* Output file */
2998 char buffer
[1024]; /* Copy buffer */
2999 int bytes
; /* Number of bytes */
3000 char conffile
[1024], /* Configuration filename */
3001 newfile
[1024], /* New config filename */
3002 oldfile
[1024]; /* Old config filename */
3003 struct stat confinfo
; /* Config file info */
3007 * First construct the filenames...
3010 snprintf(conffile
, sizeof(conffile
), "%s%s", ServerRoot
, con
->uri
+ 11);
3011 snprintf(newfile
, sizeof(newfile
), "%s%s.N", ServerRoot
, con
->uri
+ 11);
3012 snprintf(oldfile
, sizeof(oldfile
), "%s%s.O", ServerRoot
, con
->uri
+ 11);
3014 cupsdLogMessage(CUPSD_LOG_INFO
, "Installing config file \"%s\"...", conffile
);
3017 * Get the owner, group, and permissions of the configuration file.
3018 * If it doesn't exist, assign it to the User and Group in the
3019 * cupsd.conf file with mode 0640 permissions.
3022 if (stat(conffile
, &confinfo
))
3024 confinfo
.st_uid
= User
;
3025 confinfo
.st_gid
= Group
;
3026 confinfo
.st_mode
= ConfigFilePerm
;
3030 * Open the request file and new config file...
3033 if ((in
= cupsFileOpen(con
->filename
, "rb")) == NULL
)
3035 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to open request file \"%s\" - %s",
3036 con
->filename
, strerror(errno
));
3037 return (HTTP_SERVER_ERROR
);
3040 if ((out
= cupsFileOpen(newfile
, "wb")) == NULL
)
3043 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to open config file \"%s\" - %s",
3044 newfile
, strerror(errno
));
3045 return (HTTP_SERVER_ERROR
);
3048 fchmod(cupsFileNumber(out
), confinfo
.st_mode
);
3049 fchown(cupsFileNumber(out
), confinfo
.st_uid
, confinfo
.st_gid
);
3052 * Copy from the request to the new config file...
3055 while ((bytes
= cupsFileRead(in
, buffer
, sizeof(buffer
))) > 0)
3056 if (cupsFileWrite(out
, buffer
, bytes
) < bytes
)
3058 cupsdLogMessage(CUPSD_LOG_ERROR
,
3059 "Unable to copy to config file \"%s\" - %s",
3060 newfile
, strerror(errno
));
3066 return (HTTP_SERVER_ERROR
);
3070 * Close the files...
3074 if (cupsFileClose(out
))
3076 cupsdLogMessage(CUPSD_LOG_ERROR
,
3077 "Error file closing config file \"%s\" - %s",
3078 newfile
, strerror(errno
));
3082 return (HTTP_SERVER_ERROR
);
3086 * Remove the request file...
3089 unlink(con
->filename
);
3090 cupsdClearString(&con
->filename
);
3093 * Unlink the old backup, rename the current config file to the backup
3094 * filename, and rename the new config file to the config file name...
3097 if (unlink(oldfile
))
3098 if (errno
!= ENOENT
)
3100 cupsdLogMessage(CUPSD_LOG_ERROR
,
3101 "Unable to remove backup config file \"%s\" - %s",
3102 oldfile
, strerror(errno
));
3106 return (HTTP_SERVER_ERROR
);
3109 if (rename(conffile
, oldfile
))
3110 if (errno
!= ENOENT
)
3112 cupsdLogMessage(CUPSD_LOG_ERROR
,
3113 "Unable to rename old config file \"%s\" - %s",
3114 conffile
, strerror(errno
));
3118 return (HTTP_SERVER_ERROR
);
3121 if (rename(newfile
, conffile
))
3123 cupsdLogMessage(CUPSD_LOG_ERROR
,
3124 "Unable to rename new config file \"%s\" - %s",
3125 newfile
, strerror(errno
));
3127 rename(oldfile
, conffile
);
3130 return (HTTP_SERVER_ERROR
);
3134 * If the cupsd.conf file was updated, set the NeedReload flag...
3137 if (!strcmp(con
->uri
, "/admin/conf/cupsd.conf"))
3138 NeedReload
= RELOAD_CUPSD
;
3140 NeedReload
= RELOAD_ALL
;
3142 ReloadTime
= time(NULL
);
3145 * Return that the file was created successfully...
3148 return (HTTP_CREATED
);
3153 * 'is_path_absolute()' - Is a path absolute and free of relative elements (i.e. "..").
3156 static int /* O - 0 if relative, 1 if absolute */
3157 is_path_absolute(const char *path
) /* I - Input path */
3160 * Check for a leading slash...
3167 * Check for "/.." in the path...
3170 while ((path
= strstr(path
, "/..")) != NULL
)
3172 if (!path
[3] || path
[3] == '/')
3179 * If we haven't found any relative paths, return 1 indicating an
3188 * 'pipe_command()' - Pipe the output of a command to the remote client.
3191 static int /* O - Process ID */
3192 pipe_command(cupsd_client_t
*con
, /* I - Client connection */
3193 int infile
, /* I - Standard input for command */
3194 int *outfile
, /* O - Standard output for command */
3195 char *command
, /* I - Command to run */
3196 char *options
, /* I - Options for command */
3197 int root
) /* I - Run as root? */
3199 int i
; /* Looping var */
3200 int pid
; /* Process ID */
3201 char *commptr
; /* Command string pointer */
3202 char *uriptr
; /* URI string pointer */
3203 int fds
[2]; /* Pipe FDs */
3204 int argc
; /* Number of arguments */
3205 int envc
; /* Number of environment variables */
3206 char argbuf
[10240], /* Argument buffer */
3207 *argv
[100], /* Argument strings */
3208 *envp
[MAX_ENV
]; /* Environment variables */
3209 char content_length
[1024], /* CONTENT_LENGTH environment variable */
3210 content_type
[1024], /* CONTENT_TYPE environment variable */
3211 http_cookie
[32768], /* HTTP_COOKIE environment variable */
3212 http_user_agent
[1024], /* HTTP_USER_AGENT environment variable */
3213 lang
[1024], /* LANG environment variable */
3214 *query_string
, /* QUERY_STRING env variable */
3215 remote_addr
[1024], /* REMOTE_ADDR environment variable */
3216 remote_host
[1024], /* REMOTE_HOST environment variable */
3217 remote_user
[1024], /* REMOTE_USER environment variable */
3218 script_name
[1024], /* SCRIPT_NAME environment variable */
3219 server_name
[1024], /* SERVER_NAME environment variable */
3220 server_port
[1024]; /* SERVER_PORT environment variable */
3224 * Parse a copy of the options string, which is of the form:
3226 * name argument+argument+argument
3227 * name?argument+argument+argument
3228 * name param=value¶m=value
3229 * name?param=value¶m=value
3231 * If the string contains an "=" character after the initial name,
3232 * then we treat it as a HTTP GET form request and make a copy of
3233 * the remaining string for the environment variable.
3235 * The string is always parsed out as command-line arguments, to
3236 * be consistent with Apache...
3239 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3240 "pipe_command: command=\"%s\", options=\"%s\"",
3243 strlcpy(argbuf
, options
, sizeof(argbuf
));
3246 query_string
= NULL
;
3248 for (commptr
= argbuf
, argc
= 1; *commptr
!= '\0' && argc
< 99; commptr
++)
3251 * Break arguments whenever we see a + or space...
3254 if (*commptr
== ' ' || *commptr
== '+' || (*commptr
== '?' && argc
== 1))
3257 * Terminate the current string and skip trailing whitespace...
3262 while (*commptr
== ' ')
3266 * If we don't have a blank string, save it as another argument...
3271 argv
[argc
] = commptr
;
3278 * If we see an "=" in the remaining string, make a copy of it since
3279 * it will be query data...
3282 if (argc
== 2 && strchr(commptr
, '=') && con
->operation
== HTTP_GET
)
3283 cupsdSetStringf(&query_string
, "QUERY_STRING=%s", commptr
);
3286 * Don't skip the first non-blank character...
3291 else if (*commptr
== '%' && isxdigit(commptr
[1] & 255) &&
3292 isxdigit(commptr
[2] & 255))
3295 * Convert the %xx notation to the individual character.
3298 if (commptr
[1] >= '0' && commptr
[1] <= '9')
3299 *commptr
= (commptr
[1] - '0') << 4;
3301 *commptr
= (tolower(commptr
[1]) - 'a' + 10) << 4;
3303 if (commptr
[2] >= '0' && commptr
[2] <= '9')
3304 *commptr
|= commptr
[2] - '0';
3306 *commptr
|= tolower(commptr
[2]) - 'a' + 10;
3308 _cups_strcpy(commptr
+ 1, commptr
+ 3);
3311 * Check for a %00 and break if that is the case...
3321 if (argv
[0][0] == '\0')
3322 argv
[0] = strrchr(command
, '/') + 1;
3325 * Setup the environment variables as needed...
3329 snprintf(lang
, sizeof(lang
), "LANG=%s.UTF-8", con
->language
->language
);
3331 strcpy(lang
, "LANG=C");
3333 strcpy(remote_addr
, "REMOTE_ADDR=");
3334 httpAddrString(con
->http
.hostaddr
, remote_addr
+ 12,
3335 sizeof(remote_addr
) - 12);
3337 snprintf(remote_host
, sizeof(remote_host
), "REMOTE_HOST=%s",
3338 con
->http
.hostname
);
3340 snprintf(script_name
, sizeof(script_name
), "SCRIPT_NAME=%s", con
->uri
);
3341 if ((uriptr
= strchr(script_name
, '?')) != NULL
)
3344 sprintf(server_port
, "SERVER_PORT=%d", con
->serverport
);
3346 snprintf(server_name
, sizeof(server_name
), "SERVER_NAME=%s",
3349 envc
= cupsdLoadEnv(envp
, (int)(sizeof(envp
) / sizeof(envp
[0])));
3351 envp
[envc
++] = lang
;
3352 envp
[envc
++] = "REDIRECT_STATUS=1";
3353 envp
[envc
++] = server_name
;
3354 envp
[envc
++] = server_port
;
3355 envp
[envc
++] = remote_addr
;
3356 envp
[envc
++] = remote_host
;
3357 envp
[envc
++] = script_name
;
3359 if (con
->username
[0])
3361 snprintf(remote_user
, sizeof(remote_user
), "REMOTE_USER=%s", con
->username
);
3363 envp
[envc
++] = remote_user
;
3366 if (con
->http
.version
== HTTP_1_1
)
3367 envp
[envc
++] = "SERVER_PROTOCOL=HTTP/1.1";
3368 else if (con
->http
.version
== HTTP_1_0
)
3369 envp
[envc
++] = "SERVER_PROTOCOL=HTTP/1.0";
3371 envp
[envc
++] = "SERVER_PROTOCOL=HTTP/0.9";
3373 if (con
->http
.cookie
)
3375 snprintf(http_cookie
, sizeof(http_cookie
), "HTTP_COOKIE=%s",
3377 envp
[envc
++] = http_cookie
;
3380 if (con
->http
.fields
[HTTP_FIELD_USER_AGENT
][0])
3382 snprintf(http_user_agent
, sizeof(http_user_agent
), "HTTP_USER_AGENT=%s",
3383 con
->http
.fields
[HTTP_FIELD_USER_AGENT
]);
3384 envp
[envc
++] = http_user_agent
;
3387 if (con
->operation
== HTTP_GET
)
3389 for (i
= 0; i
< argc
; i
++)
3390 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "argv[%d] = \"%s\"", i
, argv
[i
]);
3392 envp
[envc
++] = "REQUEST_METHOD=GET";
3397 * Add GET form variables after ?...
3400 envp
[envc
++] = query_string
;
3405 sprintf(content_length
, "CONTENT_LENGTH=" CUPS_LLFMT
, con
->bytes
);
3406 snprintf(content_type
, sizeof(content_type
), "CONTENT_TYPE=%s",
3407 con
->http
.fields
[HTTP_FIELD_CONTENT_TYPE
]);
3409 envp
[envc
++] = "REQUEST_METHOD=POST";
3410 envp
[envc
++] = content_length
;
3411 envp
[envc
++] = content_type
;
3415 * Tell the CGI if we are using encryption...
3418 if (con
->http
.encryption
== HTTP_ENCRYPT_ALWAYS
)
3419 envp
[envc
++] = "HTTPS=ON";
3422 * Terminate the environment array...
3427 if (LogLevel
== CUPSD_LOG_DEBUG2
)
3429 for (i
= 0; i
< argc
; i
++)
3430 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3431 "pipe_command: argv[%d] = \"%s\"", i
, argv
[i
]);
3432 for (i
= 0; i
< envc
; i
++)
3433 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3434 "pipe_command: envp[%d] = \"%s\"", i
, envp
[i
]);
3438 * Create a pipe for the output...
3441 if (cupsdOpenPipe(fds
))
3443 cupsdClearString(&query_string
);
3445 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to create pipes for CGI %s - %s",
3446 argv
[0], strerror(errno
));
3451 * Then execute the command...
3454 if (cupsdStartProcess(command
, argv
, envp
, infile
, fds
[1], CGIPipes
[1],
3455 -1, root
, &pid
) < 0)
3458 * Error - can't fork!
3461 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to fork for CGI %s - %s", argv
[0],
3464 cupsdClosePipe(fds
);
3470 * Fork successful - return the PID...
3473 if (con
->username
[0])
3474 cupsdAddCert(pid
, con
->username
);
3476 cupsdLogMessage(CUPSD_LOG_DEBUG
, "CGI %s started - PID = %d", command
, pid
);
3482 cupsdClearString(&query_string
);
3489 * End of "$Id: client.c 5051 2006-02-02 16:13:16Z mike $".