2 * "$Id: client.c 4950 2006-01-19 16:07:57Z 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
)
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(con
->uri
, method
, sizeof(method
),
1149 userpass
, sizeof(userpass
),
1150 hostname
, sizeof(hostname
), &port
,
1151 resource
, sizeof(resource
));
1154 * Only allow URIs with the servername, localhost, or an IP
1158 if (strcmp(method
, "file") &&
1159 strcasecmp(hostname
, ServerName
) &&
1160 strcasecmp(hostname
, "localhost") &&
1161 !isdigit(hostname
[0]))
1164 * Nope, we don't do proxies...
1167 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad URI \"%s\" in request!",
1169 cupsdSendError(con
, HTTP_METHOD_NOT_ALLOWED
);
1170 return (cupsdCloseClient(con
));
1174 * Copy the resource portion back into the URI; both resource and
1175 * con->uri are HTTP_MAX_URI bytes in size...
1178 strcpy(con
->uri
, resource
);
1182 * Process the request...
1185 if (!strcmp(operation
, "GET"))
1186 con
->http
.state
= HTTP_GET
;
1187 else if (!strcmp(operation
, "PUT"))
1188 con
->http
.state
= HTTP_PUT
;
1189 else if (!strcmp(operation
, "POST"))
1190 con
->http
.state
= HTTP_POST
;
1191 else if (!strcmp(operation
, "DELETE"))
1192 con
->http
.state
= HTTP_DELETE
;
1193 else if (!strcmp(operation
, "TRACE"))
1194 con
->http
.state
= HTTP_TRACE
;
1195 else if (!strcmp(operation
, "OPTIONS"))
1196 con
->http
.state
= HTTP_OPTIONS
;
1197 else if (!strcmp(operation
, "HEAD"))
1198 con
->http
.state
= HTTP_HEAD
;
1201 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad operation \"%s\"!", operation
);
1202 cupsdSendError(con
, HTTP_BAD_REQUEST
);
1203 return (cupsdCloseClient(con
));
1206 con
->start
= time(NULL
);
1207 con
->operation
= con
->http
.state
;
1209 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdReadClient: %d %s %s HTTP/%d.%d", con
->http
.fd
,
1210 operation
, con
->uri
,
1211 con
->http
.version
/ 100, con
->http
.version
% 100);
1213 con
->http
.status
= HTTP_OK
;
1223 * Parse incoming parameters until the status changes...
1226 status
= httpUpdate(HTTP(con
));
1228 if (status
!= HTTP_OK
&& status
!= HTTP_CONTINUE
)
1230 cupsdSendError(con
, HTTP_BAD_REQUEST
);
1231 return (cupsdCloseClient(con
));
1236 break; /* Anti-compiler-warning-code */
1240 * Handle new transfers...
1243 if (status
== HTTP_OK
)
1245 if (con
->http
.fields
[HTTP_FIELD_ACCEPT_LANGUAGE
][0])
1248 * Figure out the locale from the Accept-Language and Content-Type
1252 if ((ptr
= strchr(con
->http
.fields
[HTTP_FIELD_ACCEPT_LANGUAGE
], ',')) != NULL
)
1255 if ((ptr
= strchr(con
->http
.fields
[HTTP_FIELD_ACCEPT_LANGUAGE
], ';')) != NULL
)
1258 if ((ptr
= strstr(con
->http
.fields
[HTTP_FIELD_CONTENT_TYPE
], "charset=")) != NULL
)
1261 * Combine language and charset, and trim any extra params in the
1265 snprintf(locale
, sizeof(locale
), "%s.%s",
1266 con
->http
.fields
[HTTP_FIELD_ACCEPT_LANGUAGE
], ptr
+ 8);
1268 if ((ptr
= strchr(locale
, ',')) != NULL
)
1272 snprintf(locale
, sizeof(locale
), "%s.%s",
1273 con
->http
.fields
[HTTP_FIELD_ACCEPT_LANGUAGE
], DefaultCharset
);
1275 con
->language
= cupsLangGet(locale
);
1278 con
->language
= cupsLangGet(DefaultLocale
);
1280 cupsdAuthorize(con
);
1282 if (!strncmp(con
->http
.fields
[HTTP_FIELD_CONNECTION
], "Keep-Alive", 10) &&
1284 con
->http
.keep_alive
= HTTP_KEEPALIVE_ON
;
1286 if (!con
->http
.fields
[HTTP_FIELD_HOST
][0] &&
1287 con
->http
.version
>= HTTP_1_1
)
1290 * HTTP/1.1 and higher require the "Host:" field...
1293 if (!cupsdSendError(con
, HTTP_BAD_REQUEST
))
1294 return (cupsdCloseClient(con
));
1296 else if (con
->operation
== HTTP_OPTIONS
)
1299 * Do OPTIONS command...
1302 if (con
->best
&& con
->best
->type
!= AUTH_NONE
)
1304 if (!cupsdSendHeader(con
, HTTP_UNAUTHORIZED
, NULL
))
1305 return (cupsdCloseClient(con
));
1308 if (!strcasecmp(con
->http
.fields
[HTTP_FIELD_CONNECTION
], "Upgrade") &&
1309 con
->http
.tls
== NULL
)
1313 * Do encryption stuff...
1316 if (!cupsdSendHeader(con
, HTTP_SWITCHING_PROTOCOLS
, NULL
))
1317 return (cupsdCloseClient(con
));
1319 httpPrintf(HTTP(con
), "Connection: Upgrade\r\n");
1320 httpPrintf(HTTP(con
), "Upgrade: TLS/1.0,HTTP/1.1\r\n");
1321 httpPrintf(HTTP(con
), "Content-Length: 0\r\n");
1322 httpPrintf(HTTP(con
), "\r\n");
1324 cupsdEncryptClient(con
);
1326 if (!cupsdSendError(con
, HTTP_NOT_IMPLEMENTED
))
1327 return (cupsdCloseClient(con
));
1328 #endif /* HAVE_SSL */
1331 if (con
->http
.expect
)
1333 /**** TODO: send expected header ****/
1336 if (!cupsdSendHeader(con
, HTTP_OK
, NULL
))
1337 return (cupsdCloseClient(con
));
1339 httpPrintf(HTTP(con
), "Allow: GET, HEAD, OPTIONS, POST, PUT\r\n");
1340 httpPrintf(HTTP(con
), "Content-Length: 0\r\n");
1341 httpPrintf(HTTP(con
), "\r\n");
1343 else if (!is_path_absolute(con
->uri
))
1346 * Protect against malicious users!
1349 if (!cupsdSendError(con
, HTTP_FORBIDDEN
))
1350 return (cupsdCloseClient(con
));
1354 if (!strcasecmp(con
->http
.fields
[HTTP_FIELD_CONNECTION
], "Upgrade") &&
1355 con
->http
.tls
== NULL
)
1359 * Do encryption stuff...
1362 if (!cupsdSendHeader(con
, HTTP_SWITCHING_PROTOCOLS
, NULL
))
1363 return (cupsdCloseClient(con
));
1365 httpPrintf(HTTP(con
), "Connection: Upgrade\r\n");
1366 httpPrintf(HTTP(con
), "Upgrade: TLS/1.0,HTTP/1.1\r\n");
1367 httpPrintf(HTTP(con
), "Content-Length: 0\r\n");
1368 httpPrintf(HTTP(con
), "\r\n");
1370 cupsdEncryptClient(con
);
1372 if (!cupsdSendError(con
, HTTP_NOT_IMPLEMENTED
))
1373 return (cupsdCloseClient(con
));
1374 #endif /* HAVE_SSL */
1377 if ((status
= cupsdIsAuthorized(con
, NULL
)) != HTTP_OK
)
1379 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1380 "cupsdReadClient: Unauthorized request for %s...\n",
1382 cupsdSendError(con
, status
);
1383 return (cupsdCloseClient(con
));
1386 if (con
->http
.expect
)
1388 /**** TODO: send expected header ****/
1391 switch (con
->http
.state
)
1393 case HTTP_GET_SEND
:
1394 if (!strncmp(con
->uri
, "/printers/", 10) &&
1395 !strcmp(con
->uri
+ strlen(con
->uri
) - 4, ".ppd"))
1398 * Send PPD file - get the real printer name since printer
1399 * names are not case sensitive but filenames can be...
1402 con
->uri
[strlen(con
->uri
) - 4] = '\0'; /* Drop ".ppd" */
1404 if ((p
= cupsdFindPrinter(con
->uri
+ 10)) != NULL
)
1405 snprintf(con
->uri
, sizeof(con
->uri
), "/ppd/%s.ppd", p
->name
);
1408 if (!cupsdSendError(con
, HTTP_NOT_FOUND
))
1409 return (cupsdCloseClient(con
));
1415 if ((!strncmp(con
->uri
, "/admin", 6) &&
1416 strncmp(con
->uri
, "/admin/conf/", 12) &&
1417 strncmp(con
->uri
, "/admin/log/", 11)) ||
1418 !strncmp(con
->uri
, "/printers", 9) ||
1419 !strncmp(con
->uri
, "/classes", 8) ||
1420 !strncmp(con
->uri
, "/help", 5) ||
1421 !strncmp(con
->uri
, "/jobs", 5))
1424 * Send CGI output...
1427 if (!strncmp(con
->uri
, "/admin", 6))
1429 cupsdSetStringf(&con
->command
, "%s/cgi-bin/admin.cgi",
1432 if ((ptr
= strchr(con
->uri
+ 6, '?')) != NULL
)
1433 cupsdSetStringf(&con
->options
, "admin%s", ptr
);
1435 cupsdSetString(&con
->options
, "admin");
1437 else if (!strncmp(con
->uri
, "/printers", 9))
1439 cupsdSetStringf(&con
->command
, "%s/cgi-bin/printers.cgi",
1441 cupsdSetString(&con
->options
, con
->uri
+ 9);
1443 else if (!strncmp(con
->uri
, "/classes", 8))
1445 cupsdSetStringf(&con
->command
, "%s/cgi-bin/classes.cgi",
1447 cupsdSetString(&con
->options
, con
->uri
+ 8);
1449 else if (!strncmp(con
->uri
, "/jobs", 5))
1451 cupsdSetStringf(&con
->command
, "%s/cgi-bin/jobs.cgi",
1453 cupsdSetString(&con
->options
, con
->uri
+ 5);
1457 cupsdSetStringf(&con
->command
, "%s/cgi-bin/help.cgi",
1459 cupsdSetString(&con
->options
, con
->uri
+ 5);
1462 if (con
->options
[0] == '/')
1463 _cups_strcpy(con
->options
, con
->options
+ 1);
1465 if (!cupsdSendCommand(con
, con
->command
, con
->options
, 0))
1467 if (!cupsdSendError(con
, HTTP_NOT_FOUND
))
1468 return (cupsdCloseClient(con
));
1471 cupsdLogRequest(con
, HTTP_OK
);
1473 if (con
->http
.version
<= HTTP_1_0
)
1474 con
->http
.keep_alive
= HTTP_KEEPALIVE_OFF
;
1476 else if ((!strncmp(con
->uri
, "/admin/conf/", 12) &&
1477 (strchr(con
->uri
+ 12, '/') ||
1478 strlen(con
->uri
) == 12)) ||
1479 (!strncmp(con
->uri
, "/admin/log/", 11) &&
1480 (strchr(con
->uri
+ 11, '/') ||
1481 strlen(con
->uri
) == 11)))
1484 * GET can only be done to configuration files under
1488 if (!cupsdSendError(con
, HTTP_FORBIDDEN
))
1489 return (cupsdCloseClient(con
));
1499 if ((filename
= get_file(con
, &filestats
, buf
,
1500 sizeof(buf
))) == NULL
)
1502 if (!cupsdSendError(con
, HTTP_NOT_FOUND
))
1503 return (cupsdCloseClient(con
));
1508 type
= mimeFileType(MimeDatabase
, filename
, NULL
);
1510 if (cupsdIsCGI(con
, filename
, &filestats
, type
))
1513 * Note: con->command and con->options were set by
1517 if (!cupsdSendCommand(con
, con
->command
, con
->options
, 0))
1519 if (!cupsdSendError(con
, HTTP_NOT_FOUND
))
1520 return (cupsdCloseClient(con
));
1523 cupsdLogRequest(con
, HTTP_OK
);
1525 if (con
->http
.version
<= HTTP_1_0
)
1526 con
->http
.keep_alive
= HTTP_KEEPALIVE_OFF
;
1530 if (!check_if_modified(con
, &filestats
))
1532 if (!cupsdSendError(con
, HTTP_NOT_MODIFIED
))
1533 return (cupsdCloseClient(con
));
1538 strcpy(line
, "text/plain");
1540 snprintf(line
, sizeof(line
), "%s/%s", type
->super
, type
->type
);
1542 if (!cupsdSendFile(con
, HTTP_OK
, filename
, line
, &filestats
))
1543 return (cupsdCloseClient(con
));
1548 case HTTP_POST_RECV
:
1550 * See if the POST request includes a Content-Length field, and if
1551 * so check the length against any limits that are set...
1554 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "POST %s", con
->uri
);
1555 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "CONTENT_TYPE = %s",
1556 con
->http
.fields
[HTTP_FIELD_CONTENT_TYPE
]);
1558 if (con
->http
.fields
[HTTP_FIELD_CONTENT_LENGTH
][0] &&
1559 MaxRequestSize
> 0 &&
1560 con
->http
.data_remaining
> MaxRequestSize
)
1563 * Request too large...
1566 if (!cupsdSendError(con
, HTTP_REQUEST_TOO_LARGE
))
1567 return (cupsdCloseClient(con
));
1571 else if (con
->http
.data_remaining
< 0)
1574 * Negative content lengths are invalid!
1577 if (!cupsdSendError(con
, HTTP_BAD_REQUEST
))
1578 return (cupsdCloseClient(con
));
1584 * See what kind of POST request this is; for IPP requests the
1585 * content-type field will be "application/ipp"...
1588 if (!strcmp(con
->http
.fields
[HTTP_FIELD_CONTENT_TYPE
],
1590 con
->request
= ippNew();
1591 else if ((!strncmp(con
->uri
, "/admin", 6) &&
1592 strncmp(con
->uri
, "/admin/conf/", 12) &&
1593 strncmp(con
->uri
, "/admin/log/", 11)) ||
1594 !strncmp(con
->uri
, "/printers", 9) ||
1595 !strncmp(con
->uri
, "/classes", 8) ||
1596 !strncmp(con
->uri
, "/help", 5) ||
1597 !strncmp(con
->uri
, "/jobs", 5))
1603 if (!strncmp(con
->uri
, "/admin", 6))
1605 cupsdSetStringf(&con
->command
, "%s/cgi-bin/admin.cgi",
1608 if ((ptr
= strchr(con
->uri
+ 6, '?')) != NULL
)
1609 cupsdSetStringf(&con
->options
, "admin%s", ptr
);
1611 cupsdSetString(&con
->options
, "admin");
1613 else if (!strncmp(con
->uri
, "/printers", 9))
1615 cupsdSetStringf(&con
->command
, "%s/cgi-bin/printers.cgi",
1617 cupsdSetString(&con
->options
, con
->uri
+ 9);
1619 else if (!strncmp(con
->uri
, "/classes", 8))
1621 cupsdSetStringf(&con
->command
, "%s/cgi-bin/classes.cgi",
1623 cupsdSetString(&con
->options
, con
->uri
+ 8);
1625 else if (!strncmp(con
->uri
, "/jobs", 5))
1627 cupsdSetStringf(&con
->command
, "%s/cgi-bin/jobs.cgi",
1629 cupsdSetString(&con
->options
, con
->uri
+ 5);
1633 cupsdSetStringf(&con
->command
, "%s/cgi-bin/help.cgi",
1635 cupsdSetString(&con
->options
, con
->uri
+ 5);
1638 if (con
->options
[0] == '/')
1639 _cups_strcpy(con
->options
, con
->options
+ 1);
1641 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1642 "cupsdReadClient: %d command=\"%s\", options = \"%s\"",
1643 con
->http
.fd
, con
->command
, con
->options
);
1645 if (con
->http
.version
<= HTTP_1_0
)
1646 con
->http
.keep_alive
= HTTP_KEEPALIVE_OFF
;
1654 if ((filename
= get_file(con
, &filestats
, buf
,
1655 sizeof(buf
))) == NULL
)
1657 if (!cupsdSendError(con
, HTTP_NOT_FOUND
))
1658 return (cupsdCloseClient(con
));
1663 type
= mimeFileType(MimeDatabase
, filename
, NULL
);
1665 if (!cupsdIsCGI(con
, filename
, &filestats
, type
))
1668 * Only POST to CGI's...
1671 if (!cupsdSendError(con
, HTTP_UNAUTHORIZED
))
1672 return (cupsdCloseClient(con
));
1677 case HTTP_PUT_RECV
:
1679 * Validate the resource name...
1682 if (strncmp(con
->uri
, "/admin/conf/", 12) ||
1683 strchr(con
->uri
+ 12, '/') ||
1684 strlen(con
->uri
) == 12)
1687 * PUT can only be done to configuration files under
1691 if (!cupsdSendError(con
, HTTP_FORBIDDEN
))
1692 return (cupsdCloseClient(con
));
1698 * See if the PUT request includes a Content-Length field, and if
1699 * so check the length against any limits that are set...
1702 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "PUT %s", con
->uri
);
1703 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "CONTENT_TYPE = %s",
1704 con
->http
.fields
[HTTP_FIELD_CONTENT_TYPE
]);
1706 if (con
->http
.fields
[HTTP_FIELD_CONTENT_LENGTH
][0] &&
1707 MaxRequestSize
> 0 &&
1708 con
->http
.data_remaining
> MaxRequestSize
)
1711 * Request too large...
1714 if (!cupsdSendError(con
, HTTP_REQUEST_TOO_LARGE
))
1715 return (cupsdCloseClient(con
));
1719 else if (con
->http
.data_remaining
< 0)
1722 * Negative content lengths are invalid!
1725 if (!cupsdSendError(con
, HTTP_BAD_REQUEST
))
1726 return (cupsdCloseClient(con
));
1732 * Open a temporary file to hold the request...
1735 cupsdSetStringf(&con
->filename
, "%s/%08x", RequestRoot
,
1737 con
->file
= open(con
->filename
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0640);
1739 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1740 "cupsdReadClient: %d REQUEST %s=%d", con
->http
.fd
,
1741 con
->filename
, con
->file
);
1745 if (!cupsdSendError(con
, HTTP_REQUEST_TOO_LARGE
))
1746 return (cupsdCloseClient(con
));
1749 fchmod(con
->file
, 0640);
1750 fchown(con
->file
, RunUser
, Group
);
1751 fcntl(con
->file
, F_SETFD
, fcntl(con
->file
, F_GETFD
) | FD_CLOEXEC
);
1756 cupsdSendError(con
, HTTP_NOT_IMPLEMENTED
);
1757 return (cupsdCloseClient(con
));
1760 if (!strncmp(con
->uri
, "/printers/", 10) &&
1761 !strcmp(con
->uri
+ strlen(con
->uri
) - 4, ".ppd"))
1764 * Send PPD file - get the real printer name since printer
1765 * names are not case sensitive but filenames can be...
1768 con
->uri
[strlen(con
->uri
) - 4] = '\0'; /* Drop ".ppd" */
1770 if ((p
= cupsdFindPrinter(con
->uri
+ 10)) != NULL
)
1771 snprintf(con
->uri
, sizeof(con
->uri
), "/ppd/%s.ppd", p
->name
);
1774 if (!cupsdSendError(con
, HTTP_NOT_FOUND
))
1775 return (cupsdCloseClient(con
));
1781 if ((!strncmp(con
->uri
, "/admin", 6) &&
1782 strncmp(con
->uri
, "/admin/conf/", 12) &&
1783 strncmp(con
->uri
, "/admin/log/", 11)) ||
1784 !strncmp(con
->uri
, "/printers", 9) ||
1785 !strncmp(con
->uri
, "/classes", 8) ||
1786 !strncmp(con
->uri
, "/help", 5) ||
1787 !strncmp(con
->uri
, "/jobs", 5))
1793 if (!cupsdSendHeader(con
, HTTP_OK
, "text/html"))
1794 return (cupsdCloseClient(con
));
1796 if (httpPrintf(HTTP(con
), "\r\n") < 0)
1797 return (cupsdCloseClient(con
));
1799 cupsdLogRequest(con
, HTTP_OK
);
1801 else if ((!strncmp(con
->uri
, "/admin/conf/", 12) &&
1802 (strchr(con
->uri
+ 12, '/') ||
1803 strlen(con
->uri
) == 12)) ||
1804 (!strncmp(con
->uri
, "/admin/log/", 11) &&
1805 (strchr(con
->uri
+ 11, '/') ||
1806 strlen(con
->uri
) == 11)))
1809 * HEAD can only be done to configuration files under
1813 if (!cupsdSendError(con
, HTTP_FORBIDDEN
))
1814 return (cupsdCloseClient(con
));
1818 else if ((filename
= get_file(con
, &filestats
, buf
,
1819 sizeof(buf
))) == NULL
)
1821 if (!cupsdSendHeader(con
, HTTP_NOT_FOUND
, "text/html"))
1822 return (cupsdCloseClient(con
));
1824 cupsdLogRequest(con
, HTTP_NOT_FOUND
);
1826 else if (!check_if_modified(con
, &filestats
))
1828 if (!cupsdSendError(con
, HTTP_NOT_MODIFIED
))
1829 return (cupsdCloseClient(con
));
1831 cupsdLogRequest(con
, HTTP_NOT_MODIFIED
);
1839 type
= mimeFileType(MimeDatabase
, filename
, NULL
);
1841 strcpy(line
, "text/plain");
1843 snprintf(line
, sizeof(line
), "%s/%s", type
->super
, type
->type
);
1845 if (!cupsdSendHeader(con
, HTTP_OK
, line
))
1846 return (cupsdCloseClient(con
));
1848 if (httpPrintf(HTTP(con
), "Last-Modified: %s\r\n",
1849 httpGetDateString(filestats
.st_mtime
)) < 0)
1850 return (cupsdCloseClient(con
));
1852 if (httpPrintf(HTTP(con
), "Content-Length: %lu\r\n",
1853 (unsigned long)filestats
.st_size
) < 0)
1854 return (cupsdCloseClient(con
));
1856 cupsdLogRequest(con
, HTTP_OK
);
1859 if (httpPrintf(HTTP(con
), "\r\n") < 0)
1860 return (cupsdCloseClient(con
));
1862 con
->http
.state
= HTTP_WAITING
;
1866 break; /* Anti-compiler-warning-code */
1872 * Handle any incoming data...
1875 switch (con
->http
.state
)
1877 case HTTP_PUT_RECV
:
1878 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1879 "cupsdReadClient: %d con->data_encoding=HTTP_ENCODE_%s, "
1880 "con->data_remaining=" CUPS_LLFMT
", con->file=%d",
1882 con
->http
.data_encoding
== HTTP_ENCODE_CHUNKED
?
1883 "CHUNKED" : "LENGTH",
1884 CUPS_LLCAST con
->http
.data_remaining
, con
->file
);
1886 if ((bytes
= httpRead(HTTP(con
), line
, sizeof(line
))) < 0)
1887 return (cupsdCloseClient(con
));
1890 con
->bytes
+= bytes
;
1892 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1893 "cupsdReadClient: %d writing %d bytes to %d",
1894 con
->http
.fd
, bytes
, con
->file
);
1896 if (write(con
->file
, line
, bytes
) < bytes
)
1898 cupsdLogMessage(CUPSD_LOG_ERROR
,
1899 "cupsdReadClient: Unable to write %d bytes to %s: %s",
1900 bytes
, con
->filename
, strerror(errno
));
1902 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1903 "cupsdReadClient: Closing data file %d...",
1908 unlink(con
->filename
);
1909 cupsdClearString(&con
->filename
);
1911 if (!cupsdSendError(con
, HTTP_REQUEST_TOO_LARGE
))
1912 return (cupsdCloseClient(con
));
1916 if (con
->http
.state
== HTTP_WAITING
)
1919 * End of file, see how big it is...
1922 fstat(con
->file
, &filestats
);
1924 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1925 "cupsdReadClient: %d Closing data file %d, size="
1927 con
->http
.fd
, con
->file
,
1928 CUPS_LLCAST filestats
.st_size
);
1933 if (filestats
.st_size
> MaxRequestSize
&&
1937 * Request is too big; remove it and send an error...
1940 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1941 "cupsdReadClient: %d Removing temp file %s",
1942 con
->http
.fd
, con
->filename
);
1943 unlink(con
->filename
);
1944 cupsdClearString(&con
->filename
);
1946 if (!cupsdSendError(con
, HTTP_REQUEST_TOO_LARGE
))
1947 return (cupsdCloseClient(con
));
1951 * Install the configuration file...
1954 status
= install_conf_file(con
);
1957 * Return the status to the client...
1960 if (!cupsdSendError(con
, status
))
1961 return (cupsdCloseClient(con
));
1965 case HTTP_POST_RECV
:
1966 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1967 "cupsdReadClient: %d con->data_encoding=HTTP_ENCODE_"
1968 "%s, con->data_remaining=" CUPS_LLFMT
", con->file=%d",
1970 con
->http
.data_encoding
== HTTP_ENCODE_CHUNKED
?
1971 "CHUNKED" : "LENGTH",
1972 CUPS_LLCAST con
->http
.data_remaining
, con
->file
);
1974 if (con
->request
!= NULL
)
1977 * Grab any request data from the connection...
1980 if ((ipp_state
= ippRead(&(con
->http
), con
->request
)) == IPP_ERROR
)
1982 cupsdLogMessage(CUPSD_LOG_ERROR
,
1983 "cupsdReadClient: %d IPP Read Error!",
1986 cupsdSendError(con
, HTTP_BAD_REQUEST
);
1987 return (cupsdCloseClient(con
));
1989 else if (ipp_state
!= IPP_DATA
)
1991 if (con
->http
.state
== HTTP_POST_SEND
)
1993 cupsdSendError(con
, HTTP_BAD_REQUEST
);
1994 return (cupsdCloseClient(con
));
2000 con
->bytes
+= ippLength(con
->request
);
2003 if (con
->file
< 0 && con
->http
.state
!= HTTP_POST_SEND
)
2006 * Create a file as needed for the request data...
2009 cupsdSetStringf(&con
->filename
, "%s/%08x", RequestRoot
, request_id
++);
2010 con
->file
= open(con
->filename
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0640);
2012 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdReadClient: %d REQUEST %s=%d", con
->http
.fd
,
2013 con
->filename
, con
->file
);
2017 if (!cupsdSendError(con
, HTTP_REQUEST_TOO_LARGE
))
2018 return (cupsdCloseClient(con
));
2021 fchmod(con
->file
, 0640);
2022 fchown(con
->file
, RunUser
, Group
);
2023 fcntl(con
->file
, F_SETFD
, fcntl(con
->file
, F_GETFD
) | FD_CLOEXEC
);
2026 if (con
->http
.state
!= HTTP_POST_SEND
)
2028 if ((bytes
= httpRead(HTTP(con
), line
, sizeof(line
))) < 0)
2029 return (cupsdCloseClient(con
));
2032 con
->bytes
+= bytes
;
2034 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2035 "cupsdReadClient: %d writing %d bytes to %d",
2036 con
->http
.fd
, bytes
, con
->file
);
2038 if (write(con
->file
, line
, bytes
) < bytes
)
2040 cupsdLogMessage(CUPSD_LOG_ERROR
,
2041 "cupsdReadClient: Unable to write %d bytes to %s: %s",
2042 bytes
, con
->filename
, strerror(errno
));
2044 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2045 "cupsdReadClient: Closing file %d...",
2050 unlink(con
->filename
);
2051 cupsdClearString(&con
->filename
);
2053 if (!cupsdSendError(con
, HTTP_REQUEST_TOO_LARGE
))
2054 return (cupsdCloseClient(con
));
2057 else if (con
->http
.state
== HTTP_POST_RECV
)
2058 return (1); /* ??? */
2059 else if (con
->http
.state
!= HTTP_POST_SEND
)
2060 return (cupsdCloseClient(con
));
2063 if (con
->http
.state
== HTTP_POST_SEND
)
2067 fstat(con
->file
, &filestats
);
2069 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2070 "cupsdReadClient: %d Closing data file %d, "
2071 "size=" CUPS_LLFMT
".",
2072 con
->http
.fd
, con
->file
,
2073 CUPS_LLCAST filestats
.st_size
);
2078 if (filestats
.st_size
> MaxRequestSize
&&
2082 * Request is too big; remove it and send an error...
2085 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2086 "cupsdReadClient: %d Removing temp file %s",
2087 con
->http
.fd
, con
->filename
);
2088 unlink(con
->filename
);
2089 cupsdClearString(&con
->filename
);
2094 * Delete any IPP request data...
2097 ippDelete(con
->request
);
2098 con
->request
= NULL
;
2101 if (!cupsdSendError(con
, HTTP_REQUEST_TOO_LARGE
))
2102 return (cupsdCloseClient(con
));
2107 if (!cupsdSendCommand(con
, con
->command
, con
->options
, 0))
2109 if (!cupsdSendError(con
, HTTP_NOT_FOUND
))
2110 return (cupsdCloseClient(con
));
2113 cupsdLogRequest(con
, HTTP_OK
);
2118 return (cupsdProcessIPPRequest(con
));
2123 break; /* Anti-compiler-warning-code */
2126 if (!con
->http
.keep_alive
&& con
->http
.state
== HTTP_WAITING
)
2127 return (cupsdCloseClient(con
));
2134 * 'cupsdSendCommand()' - Send output from a command via HTTP.
2137 int /* O - 1 on success, 0 on failure */
2139 cupsd_client_t
*con
, /* I - Client connection */
2140 char *command
, /* I - Command to run */
2141 char *options
, /* I - Command-line options */
2142 int root
) /* I - Run as root? */
2144 int fd
; /* Standard input file descriptor */
2148 fd
= open(con
->filename
, O_RDONLY
);
2150 fd
= open("/dev/null", O_RDONLY
);
2154 cupsdLogMessage(CUPSD_LOG_ERROR
,
2155 "cupsdSendCommand: %d Unable to open \"%s\" for reading: %s",
2156 con
->http
.fd
, con
->filename
? con
->filename
: "/dev/null",
2161 fcntl(fd
, F_SETFD
, fcntl(fd
, F_GETFD
) | FD_CLOEXEC
);
2163 con
->pipe_pid
= pipe_command(con
, fd
, &(con
->file
), command
, options
, root
);
2167 cupsdLogMessage(CUPSD_LOG_INFO
, "Started \"%s\" (pid=%d)", command
,
2170 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdSendCommand: %d file=%d",
2171 con
->http
.fd
, con
->file
);
2173 if (con
->pipe_pid
== 0)
2176 fcntl(con
->file
, F_SETFD
, fcntl(con
->file
, F_GETFD
) | FD_CLOEXEC
);
2178 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2179 "cupsdSendCommand: Adding fd %d to InputSet...", con
->file
);
2180 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2181 "cupsdSendCommand: Adding fd %d to OutputSet...",
2184 FD_SET(con
->file
, InputSet
);
2185 FD_SET(con
->http
.fd
, OutputSet
);
2187 con
->sent_header
= 0;
2188 con
->file_ready
= 0;
2189 con
->got_fields
= 0;
2197 * 'cupsdSendError()' - Send an error message via HTTP.
2200 int /* O - 1 if successful, 0 otherwise */
2201 cupsdSendError(cupsd_client_t
*con
, /* I - Connection */
2202 http_status_t code
) /* I - Error code */
2205 * Put the request in the access_log file...
2208 cupsdLogRequest(con
, code
);
2210 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdSendError: %d code=%d (%s)",
2211 con
->http
.fd
, code
, httpStatus(code
));
2214 * To work around bugs in some proxies, don't use Keep-Alive for some
2218 if (code
>= HTTP_BAD_REQUEST
)
2219 con
->http
.keep_alive
= HTTP_KEEPALIVE_OFF
;
2222 * Send an error message back to the client. If the error code is a
2223 * 400 or 500 series, make sure the message contains some text, too!
2226 if (!cupsdSendHeader(con
, code
, NULL
))
2230 if (code
== HTTP_UPGRADE_REQUIRED
)
2231 if (httpPrintf(HTTP(con
), "Connection: Upgrade\r\n") < 0)
2234 if (httpPrintf(HTTP(con
), "Upgrade: TLS/1.0,HTTP/1.1\r\n") < 0)
2236 #endif /* HAVE_SSL */
2238 if ((con
->http
.version
>= HTTP_1_1
&& !con
->http
.keep_alive
) ||
2239 (code
>= HTTP_BAD_REQUEST
&& code
!= HTTP_UPGRADE_REQUIRED
))
2241 if (httpPrintf(HTTP(con
), "Connection: close\r\n") < 0)
2245 if (code
>= HTTP_BAD_REQUEST
)
2248 * Send a human-readable error message.
2251 char message
[1024]; /* Message for user */
2252 const char *text
; /* Status-specific text */
2254 if (code
== HTTP_UNAUTHORIZED
)
2255 text
= _cupsLangString(con
->language
,
2256 _("Enter your username and password or the "
2257 "root username and password to access this "
2259 else if (code
== HTTP_UPGRADE_REQUIRED
)
2260 text
= _cupsLangString(con
->language
,
2261 _("You must use a https: URL to access this "
2266 snprintf(message
, sizeof(message
),
2267 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\" "
2268 "\"http://www.w3.org/TR/REC-html40/loose.dtd\">\n"
2271 "\t<META HTTP-EQUIV=\"Content-Type\" "
2272 "CONTENT=\"text/html; charset=utf-8\">\n"
2273 "\t<TITLE>%d %s</TITLE>\n"
2274 "\t<LINK REL=\"STYLESHEET\" TYPE=\"text/css\" "
2275 "HREF=\"/cups.css\">\n"
2282 code
, httpStatus(code
), code
, httpStatus(code
), text
);
2284 if (httpPrintf(HTTP(con
), "Content-Type: text/html; charset=utf-8\r\n") < 0)
2286 if (httpPrintf(HTTP(con
), "Content-Length: %d\r\n",
2287 (int)strlen(message
)) < 0)
2289 if (httpPrintf(HTTP(con
), "\r\n") < 0)
2291 if (httpPrintf(HTTP(con
), "%s", message
) < 0)
2294 else if (httpPrintf(HTTP(con
), "\r\n") < 0)
2297 con
->http
.state
= HTTP_WAITING
;
2304 * 'cupsdSendFile()' - Send a file via HTTP.
2307 int /* O - 0 on failure, 1 on success */
2308 cupsdSendFile(cupsd_client_t
*con
, /* I - Client connection */
2309 http_status_t code
, /* I - HTTP status */
2310 char *filename
, /* I - Filename */
2311 char *type
, /* I - File type */
2312 struct stat
*filestats
)/* O - File information */
2314 con
->file
= open(filename
, O_RDONLY
);
2316 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdSendFile: %d file=%d", con
->http
.fd
,
2322 fcntl(con
->file
, F_SETFD
, fcntl(con
->file
, F_GETFD
) | FD_CLOEXEC
);
2326 if (!cupsdSendHeader(con
, code
, type
))
2329 if (httpPrintf(HTTP(con
), "Last-Modified: %s\r\n",
2330 httpGetDateString(filestats
->st_mtime
)) < 0)
2332 if (httpPrintf(HTTP(con
), "Content-Length: " CUPS_LLFMT
"\r\n",
2333 CUPS_LLCAST filestats
->st_size
) < 0)
2335 if (httpPrintf(HTTP(con
), "\r\n") < 0)
2338 con
->http
.data_encoding
= HTTP_ENCODE_LENGTH
;
2339 con
->http
.data_remaining
= filestats
->st_size
;
2341 if (con
->http
.data_remaining
<= INT_MAX
)
2342 con
->http
._data_remaining
= con
->http
.data_remaining
;
2344 con
->http
._data_remaining
= INT_MAX
;
2346 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2347 "cupsdSendFile: Adding fd %d to OutputSet...", con
->http
.fd
);
2349 FD_SET(con
->http
.fd
, OutputSet
);
2356 * 'cupsdSendHeader()' - Send an HTTP request.
2359 int /* O - 1 on success, 0 on failure */
2360 cupsdSendHeader(cupsd_client_t
*con
, /* I - Client to send to */
2361 http_status_t code
, /* I - HTTP status code */
2362 char *type
) /* I - MIME type of document */
2364 if (httpPrintf(HTTP(con
), "HTTP/%d.%d %d %s\r\n", con
->http
.version
/ 100,
2365 con
->http
.version
% 100, code
, httpStatus(code
)) < 0)
2367 if (httpPrintf(HTTP(con
), "Date: %s\r\n", httpGetDateString(time(NULL
))) < 0)
2370 if (httpPrintf(HTTP(con
), "Server: %s\r\n", ServerHeader
) < 0)
2372 if (con
->http
.keep_alive
&& con
->http
.version
>= HTTP_1_0
)
2374 if (httpPrintf(HTTP(con
), "Connection: Keep-Alive\r\n") < 0)
2376 if (httpPrintf(HTTP(con
), "Keep-Alive: timeout=%d\r\n",
2377 KeepAliveTimeout
) < 0)
2380 if (code
== HTTP_METHOD_NOT_ALLOWED
)
2381 if (httpPrintf(HTTP(con
), "Allow: GET, HEAD, OPTIONS, POST\r\n") < 0)
2384 if (code
== HTTP_UNAUTHORIZED
)
2386 int auth_type
; /* Authentication type */
2389 if (!con
->best
|| con
->best
->type
== AUTH_NONE
)
2390 auth_type
= DefaultAuthType
;
2392 auth_type
= con
->best
->type
;
2394 if (auth_type
!= AUTH_DIGEST
)
2396 if (httpPrintf(HTTP(con
),
2397 "WWW-Authenticate: Basic realm=\"CUPS\"\r\n") < 0)
2402 if (httpPrintf(HTTP(con
),
2403 "WWW-Authenticate: Digest realm=\"CUPS\", nonce=\"%s\"\r\n",
2404 con
->http
.hostname
) < 0)
2409 if (con
->language
!= NULL
)
2411 if (httpPrintf(HTTP(con
), "Content-Language: %s\r\n",
2412 con
->language
->language
) < 0)
2418 if (!strcmp(type
, "text/html"))
2420 if (httpPrintf(HTTP(con
),
2421 "Content-Type: text/html; charset=utf-8\r\n") < 0)
2424 else if (httpPrintf(HTTP(con
), "Content-Type: %s\r\n", type
) < 0)
2433 * 'cupsdUpdateCGI()' - Read status messages from CGI scripts and programs.
2437 cupsdUpdateCGI(void)
2439 char *ptr
, /* Pointer to end of line in buffer */
2440 message
[1024]; /* Pointer to message text */
2441 int loglevel
; /* Log level for message */
2444 while ((ptr
= cupsdStatBufUpdate(CGIStatusBuffer
, &loglevel
,
2445 message
, sizeof(message
))) != NULL
)
2446 if (!strchr(CGIStatusBuffer
->buffer
, '\n'))
2449 if (ptr
== NULL
&& errno
)
2452 * Fatal error on pipe - should never happen!
2455 cupsdLogMessage(CUPSD_LOG_CRIT
,
2456 "cupsdUpdateCGI: error reading from CGI error pipe - %s",
2463 * 'cupsdWriteClient()' - Write data to a client as needed.
2466 int /* O - 1 if success, 0 if fail */
2467 cupsdWriteClient(cupsd_client_t
*con
) /* I - Client connection */
2469 int bytes
; /* Number of bytes written */
2470 char buf
[16385]; /* Data buffer */
2471 char *bufptr
; /* Pointer into buffer */
2472 ipp_state_t ipp_state
; /* IPP state value */
2476 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2477 "cupsdWriteClient(con=%p) %d response=%p, file=%d "
2478 "pipe_pid=%d state=%d",
2479 con
, con
->http
.fd
, con
->response
, con
->file
, con
->pipe_pid
,
2483 if (con
->http
.state
!= HTTP_GET_SEND
&&
2484 con
->http
.state
!= HTTP_POST_SEND
)
2487 if (con
->response
!= NULL
)
2489 ipp_state
= ippWrite(&(con
->http
), con
->response
);
2490 bytes
= ipp_state
!= IPP_ERROR
&& ipp_state
!= IPP_DATA
;
2492 else if ((bytes
= read(con
->file
, buf
, sizeof(buf
) - 1)) > 0)
2494 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2495 "cupsdWriteClient: Read %d bytes from file %d...",
2498 if (con
->pipe_pid
&& !con
->got_fields
)
2501 * Inspect the data for Content-Type and other fields.
2506 for (bufptr
= buf
; !con
->got_fields
&& *bufptr
; bufptr
++)
2507 if (*bufptr
== '\n')
2510 * Send line to client...
2513 if (bufptr
> buf
&& bufptr
[-1] == '\r')
2517 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "Script header: %s", buf
);
2519 if (!con
->sent_header
)
2522 * Handle redirection and CGI status codes...
2525 if (!strncasecmp(buf
, "Location:", 9))
2526 cupsdSendHeader(con
, HTTP_SEE_OTHER
, NULL
);
2527 else if (!strncasecmp(buf
, "Status:", 7))
2528 cupsdSendHeader(con
, atoi(buf
+ 7), NULL
);
2530 cupsdSendHeader(con
, HTTP_OK
, NULL
);
2532 if (con
->http
.version
== HTTP_1_1
)
2534 con
->http
.data_encoding
= HTTP_ENCODE_CHUNKED
;
2536 if (httpPrintf(HTTP(con
), "Transfer-Encoding: chunked\r\n") < 0)
2540 con
->sent_header
= 1;
2543 if (strncasecmp(buf
, "Status:", 7))
2544 httpPrintf(HTTP(con
), "%s\r\n", buf
);
2546 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdWriteClient: %d %s",
2553 bytes
-= (bufptr
- buf
);
2554 memmove(buf
, bufptr
, bytes
+ 1);
2558 * See if the line was empty...
2561 if (con
->field_col
== 0)
2562 con
->got_fields
= 1;
2566 else if (*bufptr
!= '\r')
2569 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2570 "cupsdWriteClient: %d bytes=%d, got_fields=%d",
2571 con
->http
.fd
, bytes
, con
->got_fields
);
2573 if (bytes
> 0 && !con
->got_fields
)
2576 * Remaining text needs to go out...
2579 httpPrintf(HTTP(con
), "%s", buf
);
2581 con
->http
.activity
= time(NULL
);
2584 else if (bytes
== 0)
2586 con
->http
.activity
= time(NULL
);
2591 if (httpWrite(HTTP(con
), buf
, bytes
) < 0)
2593 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2594 "cupsdWriteClient: %d Write of %d bytes failed!",
2595 con
->http
.fd
, bytes
);
2597 cupsdCloseClient(con
);
2601 con
->bytes
+= bytes
;
2603 if (con
->http
.state
== HTTP_WAITING
)
2609 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdWriteClient: %d bytes < 0",
2612 cupsdLogRequest(con
, HTTP_OK
);
2614 httpFlushWrite(HTTP(con
));
2616 if (con
->http
.data_encoding
== HTTP_ENCODE_CHUNKED
)
2618 if (httpPrintf(HTTP(con
), "0\r\n\r\n") < 0)
2620 cupsdCloseClient(con
);
2625 con
->http
.state
= HTTP_WAITING
;
2627 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2628 "cupsdWriteClient: Removing fd %d from OutputSet...",
2631 FD_CLR(con
->http
.fd
, OutputSet
);
2635 if (FD_ISSET(con
->file
, InputSet
))
2637 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2638 "cupsdWriteClient: Removing fd %d from InputSet...",
2640 FD_CLR(con
->file
, InputSet
);
2644 cupsdEndProcess(con
->pipe_pid
, 0);
2646 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2647 "cupsdWriteClient: %d Closing data file %d.",
2648 con
->http
.fd
, con
->file
);
2657 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2658 "cupsdWriteClient: %d Removing temp file %s",
2659 con
->http
.fd
, con
->filename
);
2660 unlink(con
->filename
);
2661 cupsdClearString(&con
->filename
);
2664 if (con
->request
!= NULL
)
2666 ippDelete(con
->request
);
2667 con
->request
= NULL
;
2670 if (con
->response
!= NULL
)
2672 ippDelete(con
->response
);
2673 con
->response
= NULL
;
2676 cupsdClearString(&con
->command
);
2677 cupsdClearString(&con
->options
);
2679 if (!con
->http
.keep_alive
)
2681 cupsdCloseClient(con
);
2687 con
->file_ready
= 0;
2689 if (con
->pipe_pid
&& !FD_ISSET(con
->file
, InputSet
))
2691 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2692 "cupsdWriteClient: Adding fd %d to InputSet...",
2694 FD_SET(con
->file
, InputSet
);
2698 con
->http
.activity
= time(NULL
);
2705 * 'check_if_modified()' - Decode an "If-Modified-Since" line.
2708 static int /* O - 1 if modified since */
2710 cupsd_client_t
*con
, /* I - Client connection */
2711 struct stat
*filestats
) /* I - File information */
2713 char *ptr
; /* Pointer into field */
2714 time_t date
; /* Time/date value */
2715 off_t size
; /* Size/length value */
2720 ptr
= con
->http
.fields
[HTTP_FIELD_IF_MODIFIED_SINCE
];
2725 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2726 "check_if_modified: %d If-Modified-Since=\"%s\"",
2729 while (*ptr
!= '\0')
2731 while (isspace(*ptr
) || *ptr
== ';')
2734 if (strncasecmp(ptr
, "length=", 7) == 0)
2737 size
= strtoll(ptr
, NULL
, 10);
2739 while (isdigit(*ptr
))
2742 else if (isalpha(*ptr
))
2744 date
= httpGetDateTime(ptr
);
2745 while (*ptr
!= '\0' && *ptr
!= ';')
2750 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2751 "check_if_modified: %d sizes=" CUPS_LLFMT
","
2752 CUPS_LLFMT
" dates=%d,%d",
2753 con
->http
.fd
, CUPS_LLCAST size
,
2754 CUPS_LLCAST filestats
->st_size
, (int)date
,
2755 (int)filestats
->st_mtime
);
2757 return ((size
!= filestats
->st_size
&& size
!= 0) ||
2758 (date
< filestats
->st_mtime
&& date
!= 0) ||
2759 (size
== 0 && date
== 0));
2765 * 'get_cdsa_server_certs()' - Convert a keychain name into the CFArrayRef
2766 * required by SSLSetCertificate.
2768 * For now we assumes that there is exactly one SecIdentity in the
2769 * keychain - i.e. there is exactly one matching cert/private key pair.
2770 * In the future we will search a keychain for a SecIdentity matching a
2771 * specific criteria. We also skip the operation of adding additional
2772 * non-signing certs from the keychain to the CFArrayRef.
2774 * To create a self-signed certificate for testing use the certtool.
2775 * Executing the following as root will do it:
2777 * certtool c c v k=CUPS
2780 static CFArrayRef
/* O - Array of certificates */
2781 get_cdsa_server_certs(void)
2783 OSStatus err
; /* Error info */
2784 SecKeychainRef kcRef
; /* Keychain reference */
2785 SecIdentitySearchRef srchRef
; /* Search reference */
2786 SecIdentityRef identity
; /* Identity */
2787 CFArrayRef ca
; /* Certificate array */
2794 err
= SecKeychainOpen(ServerCertificate
, &kcRef
);
2797 cupsdLogMessage(CUPSD_LOG_ERROR
, "Cannot open keychain \"%s\", error %d.",
2798 ServerCertificate
, (int)err
);
2802 * Search for "any" identity matching specified key use;
2803 * in this app, we expect there to be exactly one.
2806 err
= SecIdentitySearchCreate(kcRef
, CSSM_KEYUSE_SIGN
, &srchRef
);
2809 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2810 "Cannot find signing key in keychain \"%s\", error %d",
2811 ServerCertificate
, (int)err
);
2814 err
= SecIdentitySearchCopyNext(srchRef
, &identity
);
2817 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2818 "Cannot find signing key in keychain \"%s\", error %d",
2819 ServerCertificate
, (int)err
);
2822 if (CFGetTypeID(identity
) != SecIdentityGetTypeID())
2823 cupsdLogMessage(CUPSD_LOG_ERROR
,
2824 "SecIdentitySearchCopyNext CFTypeID failure!");
2828 * Found one. Place it in a CFArray.
2829 * TBD: snag other (non-identity) certs from keychain and add them
2833 ca
= CFArrayCreate(NULL
, (const void **)&identity
, 1, NULL
);
2836 cupsdLogMessage(CUPSD_LOG_ERROR
, "CFArrayCreate error");
2839 /*CFRelease(identity);*/
2842 /*CFRelease(srchRef);*/
2845 /*CFRelease(kcRef);*/
2850 #endif /* HAVE_CDSASSL */
2854 * 'get_file()' - Get a filename and state info.
2857 static char * /* O - Real filename */
2858 get_file(cupsd_client_t
*con
, /* I - Client connection */
2859 struct stat
*filestats
, /* O - File information */
2860 char *filename
, /* IO - Filename buffer */
2861 int len
) /* I - Buffer length */
2863 int status
; /* Status of filesystem calls */
2864 char *ptr
; /* Pointer info filename */
2865 int plen
; /* Remaining length after pointer */
2869 * Need to add DocumentRoot global...
2872 if (!strncmp(con
->uri
, "/ppd/", 5))
2873 snprintf(filename
, len
, "%s%s", ServerRoot
, con
->uri
);
2874 else if (!strncmp(con
->uri
, "/admin/conf/", 12))
2875 snprintf(filename
, len
, "%s%s", ServerRoot
, con
->uri
+ 11);
2876 else if (!strncmp(con
->uri
, "/admin/log/", 11))
2878 if (!strcmp(con
->uri
+ 11, "access_log") && AccessLog
[0] == '/')
2879 strlcpy(filename
, AccessLog
, len
);
2880 else if (!strcmp(con
->uri
+ 11, "error_log") && ErrorLog
[0] == '/')
2881 strlcpy(filename
, ErrorLog
, len
);
2882 else if (!strcmp(con
->uri
+ 11, "page_log") && PageLog
[0] == '/')
2883 strlcpy(filename
, PageLog
, len
);
2887 else if (con
->language
!= NULL
)
2888 snprintf(filename
, len
, "%s/%s%s", DocumentRoot
, con
->language
->language
,
2891 snprintf(filename
, len
, "%s%s", DocumentRoot
, con
->uri
);
2893 if ((ptr
= strchr(filename
, '?')) != NULL
)
2897 * Grab the status for this language; if there isn't a language-specific file
2898 * then fallback to the default one...
2901 if ((status
= stat(filename
, filestats
)) != 0 && con
->language
!= NULL
)
2904 * Drop the language prefix and try the current directory...
2907 if (strncmp(con
->uri
, "/ppd/", 5) &&
2908 strncmp(con
->uri
, "/admin/conf/", 12) &&
2909 strncmp(con
->uri
, "/admin/log/", 11))
2911 snprintf(filename
, len
, "%s%s", DocumentRoot
, con
->uri
);
2913 if ((ptr
= strchr(filename
, '?')) != NULL
)
2916 status
= stat(filename
, filestats
);
2921 * If we're found a directory, get the index.html file instead...
2924 if (!status
&& S_ISDIR(filestats
->st_mode
))
2926 if (filename
[strlen(filename
) - 1] != '/')
2927 strlcat(filename
, "/", len
);
2929 ptr
= filename
+ strlen(filename
);
2930 plen
= len
- (ptr
- filename
);
2932 strlcpy(ptr
, "index.html", plen
);
2933 status
= stat(filename
, filestats
);
2938 strlcpy(ptr
, "index.class", plen
);
2939 status
= stat(filename
, filestats
);
2941 #endif /* HAVE_JAVA */
2946 strlcpy(ptr
, "index.pl", plen
);
2947 status
= stat(filename
, filestats
);
2949 #endif /* HAVE_PERL */
2954 strlcpy(ptr
, "index.php", plen
);
2955 status
= stat(filename
, filestats
);
2957 #endif /* HAVE_PHP */
2962 strlcpy(ptr
, "index.pyc", plen
);
2963 status
= stat(filename
, filestats
);
2968 strlcpy(ptr
, "index.py", plen
);
2969 status
= stat(filename
, filestats
);
2971 #endif /* HAVE_PYTHON */
2974 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_file: %d filename=%s size=%d",
2975 con
->http
.fd
, filename
,
2976 status
? -1 : (int)filestats
->st_size
);
2979 con
->http
.data_remaining
= (int)filestats
->st_size
;
2989 * 'install_conf_file()' - Install a configuration file.
2992 static http_status_t
/* O - Status */
2993 install_conf_file(cupsd_client_t
*con
) /* I - Connection */
2995 cups_file_t
*in
, /* Input file */
2996 *out
; /* Output file */
2997 char buffer
[1024]; /* Copy buffer */
2998 int bytes
; /* Number of bytes */
2999 char conffile
[1024], /* Configuration filename */
3000 newfile
[1024], /* New config filename */
3001 oldfile
[1024]; /* Old config filename */
3002 struct stat confinfo
; /* Config file info */
3006 * First construct the filenames...
3009 snprintf(conffile
, sizeof(conffile
), "%s%s", ServerRoot
, con
->uri
+ 11);
3010 snprintf(newfile
, sizeof(newfile
), "%s%s.N", ServerRoot
, con
->uri
+ 11);
3011 snprintf(oldfile
, sizeof(oldfile
), "%s%s.O", ServerRoot
, con
->uri
+ 11);
3013 cupsdLogMessage(CUPSD_LOG_INFO
, "Installing config file \"%s\"...", conffile
);
3016 * Get the owner, group, and permissions of the configuration file.
3017 * If it doesn't exist, assign it to the User and Group in the
3018 * cupsd.conf file with mode 0640 permissions.
3021 if (stat(conffile
, &confinfo
))
3023 confinfo
.st_uid
= User
;
3024 confinfo
.st_gid
= Group
;
3025 confinfo
.st_mode
= ConfigFilePerm
;
3029 * Open the request file and new config file...
3032 if ((in
= cupsFileOpen(con
->filename
, "rb")) == NULL
)
3034 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to open request file \"%s\" - %s",
3035 con
->filename
, strerror(errno
));
3036 return (HTTP_SERVER_ERROR
);
3039 if ((out
= cupsFileOpen(newfile
, "wb")) == NULL
)
3042 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to open config file \"%s\" - %s",
3043 newfile
, strerror(errno
));
3044 return (HTTP_SERVER_ERROR
);
3047 fchmod(cupsFileNumber(out
), confinfo
.st_mode
);
3048 fchown(cupsFileNumber(out
), confinfo
.st_uid
, confinfo
.st_gid
);
3051 * Copy from the request to the new config file...
3054 while ((bytes
= cupsFileRead(in
, buffer
, sizeof(buffer
))) > 0)
3055 if (cupsFileWrite(out
, buffer
, bytes
) < bytes
)
3057 cupsdLogMessage(CUPSD_LOG_ERROR
,
3058 "Unable to copy to config file \"%s\" - %s",
3059 newfile
, strerror(errno
));
3065 return (HTTP_SERVER_ERROR
);
3069 * Close the files...
3073 if (cupsFileClose(out
))
3075 cupsdLogMessage(CUPSD_LOG_ERROR
,
3076 "Error file closing config file \"%s\" - %s",
3077 newfile
, strerror(errno
));
3081 return (HTTP_SERVER_ERROR
);
3085 * Remove the request file...
3088 unlink(con
->filename
);
3089 cupsdClearString(&con
->filename
);
3092 * Unlink the old backup, rename the current config file to the backup
3093 * filename, and rename the new config file to the config file name...
3096 if (unlink(oldfile
))
3097 if (errno
!= ENOENT
)
3099 cupsdLogMessage(CUPSD_LOG_ERROR
,
3100 "Unable to remove backup config file \"%s\" - %s",
3101 oldfile
, strerror(errno
));
3105 return (HTTP_SERVER_ERROR
);
3108 if (rename(conffile
, oldfile
))
3109 if (errno
!= ENOENT
)
3111 cupsdLogMessage(CUPSD_LOG_ERROR
,
3112 "Unable to rename old config file \"%s\" - %s",
3113 conffile
, strerror(errno
));
3117 return (HTTP_SERVER_ERROR
);
3120 if (rename(newfile
, conffile
))
3122 cupsdLogMessage(CUPSD_LOG_ERROR
,
3123 "Unable to rename new config file \"%s\" - %s",
3124 newfile
, strerror(errno
));
3126 rename(oldfile
, conffile
);
3129 return (HTTP_SERVER_ERROR
);
3133 * If the cupsd.conf file was updated, set the NeedReload flag...
3136 if (!strcmp(con
->uri
, "/admin/conf/cupsd.conf"))
3137 NeedReload
= RELOAD_CUPSD
;
3139 NeedReload
= RELOAD_ALL
;
3141 ReloadTime
= time(NULL
);
3144 * Return that the file was created successfully...
3147 return (HTTP_CREATED
);
3152 * 'is_path_absolute()' - Is a path absolute and free of relative elements (i.e. "..").
3155 static int /* O - 0 if relative, 1 if absolute */
3156 is_path_absolute(const char *path
) /* I - Input path */
3159 * Check for a leading slash...
3166 * Check for "/.." in the path...
3169 while ((path
= strstr(path
, "/..")) != NULL
)
3171 if (!path
[3] || path
[3] == '/')
3178 * If we haven't found any relative paths, return 1 indicating an
3187 * 'pipe_command()' - Pipe the output of a command to the remote client.
3190 static int /* O - Process ID */
3191 pipe_command(cupsd_client_t
*con
, /* I - Client connection */
3192 int infile
, /* I - Standard input for command */
3193 int *outfile
, /* O - Standard output for command */
3194 char *command
, /* I - Command to run */
3195 char *options
, /* I - Options for command */
3196 int root
) /* I - Run as root? */
3198 int i
; /* Looping var */
3199 int pid
; /* Process ID */
3200 char *commptr
; /* Command string pointer */
3201 char *uriptr
; /* URI string pointer */
3202 int fds
[2]; /* Pipe FDs */
3203 int argc
; /* Number of arguments */
3204 int envc
; /* Number of environment variables */
3205 char argbuf
[10240], /* Argument buffer */
3206 *argv
[100], /* Argument strings */
3207 *envp
[100]; /* Environment variables */
3208 char content_length
[1024], /* CONTENT_LENGTH environment variable */
3209 content_type
[1024], /* CONTENT_TYPE environment variable */
3210 http_cookie
[32768], /* HTTP_COOKIE environment variable */
3211 http_user_agent
[1024], /* HTTP_USER_AGENT environment variable */
3212 lang
[1024], /* LANG environment variable */
3213 *query_string
, /* QUERY_STRING env variable */
3214 remote_addr
[1024], /* REMOTE_ADDR environment variable */
3215 remote_host
[1024], /* REMOTE_HOST environment variable */
3216 remote_user
[1024], /* REMOTE_USER environment variable */
3217 script_name
[1024], /* SCRIPT_NAME environment variable */
3218 server_name
[1024], /* SERVER_NAME environment variable */
3219 server_port
[1024]; /* SERVER_PORT environment variable */
3223 * Parse a copy of the options string, which is of the form:
3225 * name argument+argument+argument
3226 * name?argument+argument+argument
3227 * name param=value¶m=value
3228 * name?param=value¶m=value
3230 * If the string contains an "=" character after the initial name,
3231 * then we treat it as a HTTP GET form request and make a copy of
3232 * the remaining string for the environment variable.
3234 * The string is always parsed out as command-line arguments, to
3235 * be consistent with Apache...
3238 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3239 "pipe_command: command=\"%s\", options=\"%s\"",
3242 strlcpy(argbuf
, options
, sizeof(argbuf
));
3245 query_string
= NULL
;
3247 for (commptr
= argbuf
, argc
= 1; *commptr
!= '\0' && argc
< 99; commptr
++)
3250 * Break arguments whenever we see a + or space...
3253 if (*commptr
== ' ' || *commptr
== '+' || (*commptr
== '?' && argc
== 1))
3256 * Terminate the current string and skip trailing whitespace...
3261 while (*commptr
== ' ')
3265 * If we don't have a blank string, save it as another argument...
3270 argv
[argc
] = commptr
;
3277 * If we see an "=" in the remaining string, make a copy of it since
3278 * it will be query data...
3281 if (argc
== 2 && strchr(commptr
, '=') && con
->operation
== HTTP_GET
)
3282 cupsdSetStringf(&query_string
, "QUERY_STRING=%s", commptr
);
3285 * Don't skip the first non-blank character...
3290 else if (*commptr
== '%' && isxdigit(commptr
[1] & 255) &&
3291 isxdigit(commptr
[2] & 255))
3294 * Convert the %xx notation to the individual character.
3297 if (commptr
[1] >= '0' && commptr
[1] <= '9')
3298 *commptr
= (commptr
[1] - '0') << 4;
3300 *commptr
= (tolower(commptr
[1]) - 'a' + 10) << 4;
3302 if (commptr
[2] >= '0' && commptr
[2] <= '9')
3303 *commptr
|= commptr
[2] - '0';
3305 *commptr
|= tolower(commptr
[2]) - 'a' + 10;
3307 _cups_strcpy(commptr
+ 1, commptr
+ 3);
3310 * Check for a %00 and break if that is the case...
3320 if (argv
[0][0] == '\0')
3321 argv
[0] = strrchr(command
, '/') + 1;
3324 * Setup the environment variables as needed...
3328 snprintf(lang
, sizeof(lang
), "LANG=%s.UTF-8", con
->language
->language
);
3330 strcpy(lang
, "LANG=C");
3332 strcpy(remote_addr
, "REMOTE_ADDR=");
3333 httpAddrString(con
->http
.hostaddr
, remote_addr
+ 12,
3334 sizeof(remote_addr
) - 12);
3336 snprintf(remote_host
, sizeof(remote_host
), "REMOTE_HOST=%s",
3337 con
->http
.hostname
);
3339 snprintf(script_name
, sizeof(script_name
), "SCRIPT_NAME=%s", con
->uri
);
3340 if ((uriptr
= strchr(script_name
, '?')) != NULL
)
3343 sprintf(server_port
, "SERVER_PORT=%d", con
->serverport
);
3345 snprintf(server_name
, sizeof(server_name
), "SERVER_NAME=%s",
3348 envc
= cupsdLoadEnv(envp
, (int)(sizeof(envp
) / sizeof(envp
[0])));
3350 envp
[envc
++] = lang
;
3351 envp
[envc
++] = "REDIRECT_STATUS=1";
3352 envp
[envc
++] = server_name
;
3353 envp
[envc
++] = server_port
;
3354 envp
[envc
++] = remote_addr
;
3355 envp
[envc
++] = remote_host
;
3356 envp
[envc
++] = script_name
;
3358 if (con
->username
[0])
3360 snprintf(remote_user
, sizeof(remote_user
), "REMOTE_USER=%s", con
->username
);
3362 envp
[envc
++] = remote_user
;
3365 if (con
->http
.version
== HTTP_1_1
)
3366 envp
[envc
++] = "SERVER_PROTOCOL=HTTP/1.1";
3367 else if (con
->http
.version
== HTTP_1_0
)
3368 envp
[envc
++] = "SERVER_PROTOCOL=HTTP/1.0";
3370 envp
[envc
++] = "SERVER_PROTOCOL=HTTP/0.9";
3372 if (con
->http
.cookie
)
3374 snprintf(http_cookie
, sizeof(http_cookie
), "HTTP_COOKIE=%s",
3376 envp
[envc
++] = http_cookie
;
3379 if (con
->http
.fields
[HTTP_FIELD_USER_AGENT
][0])
3381 snprintf(http_user_agent
, sizeof(http_user_agent
), "HTTP_USER_AGENT=%s",
3382 con
->http
.fields
[HTTP_FIELD_USER_AGENT
]);
3383 envp
[envc
++] = http_user_agent
;
3386 if (con
->operation
== HTTP_GET
)
3388 for (i
= 0; i
< argc
; i
++)
3389 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "argv[%d] = \"%s\"", i
, argv
[i
]);
3391 envp
[envc
++] = "REQUEST_METHOD=GET";
3396 * Add GET form variables after ?...
3399 envp
[envc
++] = query_string
;
3404 sprintf(content_length
, "CONTENT_LENGTH=" CUPS_LLFMT
, con
->bytes
);
3405 snprintf(content_type
, sizeof(content_type
), "CONTENT_TYPE=%s",
3406 con
->http
.fields
[HTTP_FIELD_CONTENT_TYPE
]);
3408 envp
[envc
++] = "REQUEST_METHOD=POST";
3409 envp
[envc
++] = content_length
;
3410 envp
[envc
++] = content_type
;
3414 * Tell the CGI if we are using encryption...
3417 if (con
->http
.encryption
== HTTP_ENCRYPT_ALWAYS
)
3418 envp
[envc
++] = "HTTPS=ON";
3421 * Terminate the environment array...
3426 if (LogLevel
== CUPSD_LOG_DEBUG2
)
3428 for (i
= 0; i
< argc
; i
++)
3429 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3430 "pipe_command: argv[%d] = \"%s\"", i
, argv
[i
]);
3431 for (i
= 0; i
< envc
; i
++)
3432 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3433 "pipe_command: envp[%d] = \"%s\"", i
, envp
[i
]);
3437 * Create a pipe for the output...
3440 if (cupsdOpenPipe(fds
))
3442 cupsdClearString(&query_string
);
3444 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to create pipes for CGI %s - %s",
3445 argv
[0], strerror(errno
));
3450 * Then execute the command...
3453 if (cupsdStartProcess(command
, argv
, envp
, infile
, fds
[1], CGIPipes
[1],
3454 -1, root
, &pid
) < 0)
3457 * Error - can't fork!
3460 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to fork for CGI %s - %s", argv
[0],
3463 cupsdClosePipe(fds
);
3469 * Fork successful - return the PID...
3472 if (con
->username
[0])
3473 cupsdAddCert(pid
, con
->username
);
3475 cupsdLogMessage(CUPSD_LOG_DEBUG
, "CGI %s started - PID = %d", command
, pid
);
3481 cupsdClearString(&query_string
);
3488 * End of "$Id: client.c 4950 2006-01-19 16:07:57Z mike $".