4 * Client routines for the Common UNIX Printing System (CUPS) scheduler.
6 * Copyright 1997-2005 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 * decode_auth() - Decode an authorization string.
40 * get_file() - Get a filename and state info.
41 * install_conf_file() - Install a configuration file.
42 * is_path_absolute() - Is a path absolute and free of relative elements.
43 * pipe_command() - Pipe the output of a command to the remote client.
44 * CDSAReadFunc() - Read function for CDSA decryption code.
45 * CDSAWriteFunc() - Write function for CDSA encryption code.
49 * Include necessary headers...
52 #include <cups/http-private.h>
60 static int check_if_modified(cupsd_client_t
*con
,
61 struct stat
*filestats
);
62 static void decode_auth(cupsd_client_t
*con
);
63 static char *get_file(cupsd_client_t
*con
, struct stat
*filestats
,
64 char *filename
, int len
);
65 static http_status_t
install_conf_file(cupsd_client_t
*con
);
66 static int is_path_absolute(const char *path
);
67 static int pipe_command(cupsd_client_t
*con
, int infile
, int *outfile
,
68 char *command
, char *options
, int root
);
71 static OSStatus
CDSAReadFunc(SSLConnectionRef connection
, void *data
,
73 static OSStatus
CDSAWriteFunc(SSLConnectionRef connection
,
74 const void *data
, size_t *dataLength
);
75 #endif /* HAVE_CDSASSL */
79 * 'cupsdAcceptClient()' - Accept a new client.
83 cupsdAcceptClient(cupsd_listener_t
*lis
)/* I - Listener socket */
85 int i
; /* Looping var */
86 int count
; /* Count of connections on a host */
87 int val
; /* Parameter value */
88 cupsd_client_t
*con
; /* New client pointer */
89 const struct hostent
*host
; /* Host entry for 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
);
117 * Accept the client and get the remote address...
120 val
= sizeof(struct sockaddr_in
);
122 if ((con
->http
.fd
= accept(lis
->fd
, (struct sockaddr
*)&(con
->http
.hostaddr
),
125 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to accept client connection - %s.",
131 if (lis
->address
.addr
.sa_family
== AF_INET6
)
132 con
->http
.hostaddr
.ipv6
.sin6_port
= lis
->address
.ipv6
.sin6_port
;
134 #endif /* AF_INET6 */
135 if (lis
->address
.addr
.sa_family
== AF_INET
)
136 con
->http
.hostaddr
.ipv4
.sin_port
= lis
->address
.ipv4
.sin_port
;
139 * Check the number of clients on the same address...
142 for (i
= 0, count
= 0; i
< NumClients
; i
++)
143 if (httpAddrEqual(&(Clients
[i
].http
.hostaddr
), &(con
->http
.hostaddr
)))
146 if (count
>= MaxClientsPerHost
)
150 if (count
>= MaxClientsPerHost
)
152 if ((time(NULL
) - last_dos
) >= 60)
154 last_dos
= time(NULL
);
155 cupsdLogMessage(CUPSD_LOG_WARN
,
156 "Possible DoS attack - more than %d clients connecting from %s!",
157 MaxClientsPerHost
, Clients
[i
].http
.hostname
);
161 closesocket(con
->http
.fd
);
170 * Get the hostname or format the IP address as needed...
173 if (httpAddrLocalhost(&(con
->http
.hostaddr
)))
176 * Map accesses from the loopback interface to "localhost"...
179 strlcpy(con
->http
.hostname
, "localhost", sizeof(con
->http
.hostname
));
180 hostname
= con
->http
.hostname
;
182 else if (httpAddrEqual(&(con
->http
.hostaddr
), &ServerAddr
))
185 * Map accesses from the same host to the server name.
188 strlcpy(con
->http
.hostname
, ServerName
, sizeof(con
->http
.hostname
));
189 hostname
= con
->http
.hostname
;
191 else if (HostNameLookups
)
192 hostname
= httpAddrLookup(&(con
->http
.hostaddr
), con
->http
.hostname
,
193 sizeof(con
->http
.hostname
));
197 httpAddrString(&(con
->http
.hostaddr
), con
->http
.hostname
,
198 sizeof(con
->http
.hostname
));
201 if (hostname
== NULL
&& HostNameLookups
== 2)
204 * Can't have an unresolved IP address with double-lookups enabled...
207 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
208 "cupsdAcceptClient: Closing connection %d...",
212 closesocket(con
->http
.fd
);
217 cupsdLogMessage(CUPSD_LOG_WARN
,
218 "Name lookup failed - connection from %s closed!",
223 if (HostNameLookups
== 2)
226 * Do double lookups as needed...
229 if ((host
= httpGetHostByName(con
->http
.hostname
)) != NULL
)
232 * See if the hostname maps to the same IP address...
235 if (host
->h_addrtype
!= con
->http
.hostaddr
.addr
.sa_family
)
238 * Not the right type of address...
246 * Compare all of the addresses against this one...
249 for (i
= 0; host
->h_addr_list
[i
]; i
++)
251 httpAddrLoad(host
, 0, i
, &temp
);
253 if (httpAddrEqual(&(con
->http
.hostaddr
), &temp
))
257 if (!host
->h_addr_list
[i
])
265 * Can't have a hostname that doesn't resolve to the same IP address
266 * with double-lookups enabled...
269 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
270 "cupsdAcceptClient: Closing connection %d...",
274 closesocket(con
->http
.fd
);
279 cupsdLogMessage(CUPSD_LOG_WARN
,
280 "IP lookup failed - connection from %s closed!",
287 if (con
->http
.hostaddr
.addr
.sa_family
== AF_INET6
)
288 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdAcceptClient: %d from %s:%d (IPv6)",
289 con
->http
.fd
, con
->http
.hostname
,
290 ntohs(con
->http
.hostaddr
.ipv6
.sin6_port
));
292 #endif /* AF_INET6 */
294 if (con
->http
.hostaddr
.addr
.sa_family
== AF_LOCAL
)
295 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdAcceptClient: %d from %s (Domain)",
296 con
->http
.fd
, con
->http
.hostname
);
298 #endif /* AF_LOCAL */
299 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdAcceptClient: %d from %s:%d (IPv4)",
300 con
->http
.fd
, con
->http
.hostname
,
301 ntohs(con
->http
.hostaddr
.ipv4
.sin_port
));
304 * Get the local address the client connected to...
308 if (getsockname(con
->http
.fd
, (struct sockaddr
*)&temp
, &i
))
310 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to get local address - %s",
313 strcpy(con
->servername
, "localhost");
314 con
->serverport
= LocalPort
;
319 if (temp
.addr
.sa_family
== AF_INET6
)
321 httpAddrLookup(&temp
, con
->servername
, sizeof(con
->servername
));
322 con
->serverport
= ntohs(lis
->address
.ipv6
.sin6_port
);
325 #endif /* AF_INET6 */
326 if (temp
.addr
.sa_family
== AF_INET
)
328 httpAddrLookup(&temp
, con
->servername
, sizeof(con
->servername
));
329 con
->serverport
= ntohs(lis
->address
.ipv4
.sin_port
);
333 strcpy(con
->servername
, "localhost");
334 con
->serverport
= LocalPort
;
338 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
339 "cupsdAcceptClient: %d connected to server on %s:%d",
340 con
->http
.fd
, con
->servername
, con
->serverport
);
343 * Using TCP_NODELAY improves responsiveness, especially on systems
344 * with a slow loopback interface... Since we write large buffers
345 * when sending print files and requests, there shouldn't be any
346 * performance penalty for this...
350 setsockopt(con
->http
.fd
, IPPROTO_TCP
, TCP_NODELAY
, &val
, sizeof(val
));
353 * Close this file on all execs...
356 fcntl(con
->http
.fd
, F_SETFD
, fcntl(con
->http
.fd
, F_GETFD
) | FD_CLOEXEC
);
359 * Add the socket to the select() input mask.
362 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
363 "cupsdAcceptClient: Adding fd %d to InputSet...",
365 FD_SET(con
->http
.fd
, InputSet
);
370 * Temporarily suspend accept()'s until we lose a client...
373 if (NumClients
== MaxClients
)
374 cupsdPauseListening();
378 * See if we are connecting on a secure port...
381 if (lis
->encryption
== HTTP_ENCRYPT_ALWAYS
)
384 * https connection; go secure...
387 con
->http
.encryption
= HTTP_ENCRYPT_ALWAYS
;
389 cupsdEncryptClient(con
);
393 #endif /* HAVE_SSL */
398 * 'cupsdCloseAllClients()' - Close all remote clients immediately.
402 cupsdCloseAllClients(void)
404 while (NumClients
> 0)
405 cupsdCloseClient(Clients
);
410 * 'cupsdCloseClient()' - Close a remote client.
413 int /* O - 1 if partial close, 0 if fully closed */
414 cupsdCloseClient(cupsd_client_t
*con
) /* I - Client to close */
416 int partial
; /* Do partial close for SSL? */
417 #if defined(HAVE_LIBSSL)
418 SSL_CTX
*context
; /* Context for encryption */
419 SSL
*conn
; /* Connection for encryption */
420 unsigned long error
; /* Error code */
421 #elif defined(HAVE_GNUTLS)
422 http_tls_t
*conn
; /* TLS connection information */
423 int error
; /* Error code */
424 gnutls_certificate_server_credentials
*credentials
;
425 /* TLS credentials */
426 #elif defined(HAVE_CDSASSL)
427 int status
; /* Error status */
428 #endif /* HAVE_LIBSSL */
431 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdCloseClient: %d", con
->http
.fd
);
434 * Flush pending writes before closing...
437 httpFlushWrite(HTTP(con
));
443 * Shutdown encryption as needed...
451 conn
= (SSL
*)(con
->http
.tls
);
452 context
= SSL_get_SSL_CTX(conn
);
454 switch (SSL_shutdown(conn
))
457 cupsdLogMessage(CUPSD_LOG_INFO
,
458 "cupsdCloseClient: SSL shutdown successful!");
461 cupsdLogMessage(CUPSD_LOG_ERROR
,
462 "cupsdCloseClient: Fatal error during SSL shutdown!");
464 while ((error
= ERR_get_error()) != 0)
465 cupsdLogMessage(CUPSD_LOG_ERROR
, "cupsdCloseClient: %s",
466 ERR_error_string(error
, NULL
));
470 SSL_CTX_free(context
);
473 # elif defined(HAVE_GNUTLS)
474 conn
= (http_tls_t
*)(con
->http
.tls
);
475 credentials
= (gnutls_certificate_server_credentials
*)(conn
->credentials
);
477 error
= gnutls_bye(conn
->session
, GNUTLS_SHUT_WR
);
480 case GNUTLS_E_SUCCESS
:
481 cupsdLogMessage(CUPSD_LOG_INFO
,
482 "cupsdCloseClient: SSL shutdown successful!");
485 cupsdLogMessage(CUPSD_LOG_ERROR
,
486 "cupsdCloseClient: %s", gnutls_strerror(error
));
490 gnutls_deinit(conn
->session
);
491 gnutls_certificate_free_credentials(*credentials
);
495 # elif defined(HAVE_CDSASSL)
496 status
= SSLClose((SSLContextRef
)con
->http
.tls
);
497 SSLDisposeContext((SSLContextRef
)con
->http
.tls
);
498 # endif /* HAVE_LIBSSL */
500 con
->http
.tls
= NULL
;
502 #endif /* HAVE_SSL */
504 if (con
->pipe_pid
!= 0)
507 * Stop any CGI process...
510 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
511 "cupsdCloseClient: %d Killing process ID %d...",
512 con
->http
.fd
, con
->pipe_pid
);
513 cupsdEndProcess(con
->pipe_pid
, 1);
519 if (FD_ISSET(con
->file
, InputSet
))
521 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
522 "cupsdCloseClient: %d Removing fd %d from InputSet...",
523 con
->http
.fd
, con
->file
);
524 FD_CLR(con
->file
, InputSet
);
527 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
528 "cupsdCloseClient: %d Closing data file %d.",
529 con
->http
.fd
, con
->file
);
536 * Close the socket and clear the file from the input set for select()...
539 if (con
->http
.fd
> 0)
544 * Only do a partial close so that the encrypted client gets everything.
547 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
548 "cupsdCloseClient: Removing fd %d from OutputSet...",
550 shutdown(con
->http
.fd
, 0);
551 FD_CLR(con
->http
.fd
, OutputSet
);
556 * Shut the socket down fully...
559 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
560 "cupsdCloseClient: Removing fd %d from InputSet and OutputSet...",
563 FD_CLR(con
->http
.fd
, InputSet
);
564 FD_CLR(con
->http
.fd
, OutputSet
);
575 if (con
->http
.input_set
)
576 free(con
->http
.input_set
);
578 httpClearCookie(HTTP(con
));
580 cupsdClearString(&con
->filename
);
581 cupsdClearString(&con
->command
);
582 cupsdClearString(&con
->options
);
586 ippDelete(con
->request
);
592 ippDelete(con
->response
);
593 con
->response
= NULL
;
598 cupsLangFree(con
->language
);
599 con
->language
= NULL
;
603 * Re-enable new client connections if we are going back under the
607 if (NumClients
== MaxClients
)
608 cupsdResumeListening();
611 * Compact the list of clients as necessary...
616 if (con
< (Clients
+ NumClients
))
617 memmove(con
, con
+ 1, (Clients
+ NumClients
- con
) * sizeof(cupsd_client_t
));
625 * 'cupsdEncryptClient()' - Enable encryption for the client...
628 int /* O - 1 on success, 0 on error */
629 cupsdEncryptClient(cupsd_client_t
*con
) /* I - Client to encrypt */
631 #if defined HAVE_LIBSSL
632 SSL_CTX
*context
; /* Context for encryption */
633 SSL
*conn
; /* Connection for encryption */
634 unsigned long error
; /* Error code */
638 * Create the SSL context and accept the connection...
641 context
= SSL_CTX_new(SSLv23_server_method());
643 SSL_CTX_set_options(context
, SSL_OP_NO_SSLv2
); /* Only use SSLv3 or TLS */
644 SSL_CTX_use_PrivateKey_file(context
, ServerKey
, SSL_FILETYPE_PEM
);
645 SSL_CTX_use_certificate_file(context
, ServerCertificate
, SSL_FILETYPE_PEM
);
647 conn
= SSL_new(context
);
649 SSL_set_fd(conn
, con
->http
.fd
);
650 if (SSL_accept(conn
) != 1)
652 cupsdLogMessage(CUPSD_LOG_ERROR
,
653 "cupsdEncryptClient: Unable to encrypt connection from %s!",
656 while ((error
= ERR_get_error()) != 0)
657 cupsdLogMessage(CUPSD_LOG_ERROR
, "cupsdEncryptClient: %s",
658 ERR_error_string(error
, NULL
));
660 SSL_CTX_free(context
);
665 cupsdLogMessage(CUPSD_LOG_DEBUG
,
666 "cupsdEncryptClient: %d Connection from %s now encrypted.",
667 con
->http
.fd
, con
->http
.hostname
);
669 con
->http
.tls
= conn
;
672 #elif defined(HAVE_GNUTLS)
673 http_tls_t
*conn
; /* TLS session object */
674 int error
; /* Error code */
675 gnutls_certificate_server_credentials
*credentials
;
676 /* TLS credentials */
679 * Create the SSL object and perform the SSL handshake...
682 conn
= (http_tls_t
*)malloc(sizeof(gnutls_session
));
687 credentials
= (gnutls_certificate_server_credentials
*)
688 malloc(sizeof(gnutls_certificate_server_credentials
));
689 if (credentials
== NULL
)
691 cupsdLogMessage(CUPSD_LOG_ERROR
,
692 "cupsdEncryptClient: Unable to encrypt connection from %s!",
694 cupsdLogMessage(CUPSD_LOG_ERROR
, "cupsdEncryptClient: %s", strerror(errno
));
700 gnutls_certificate_allocate_credentials(credentials
);
701 gnutls_certificate_set_x509_key_file(*credentials
, ServerCertificate
,
702 ServerKey
, GNUTLS_X509_FMT_PEM
);
704 gnutls_init(&(conn
->session
), GNUTLS_SERVER
);
705 gnutls_set_default_priority(conn
->session
);
706 gnutls_credentials_set(conn
->session
, GNUTLS_CRD_CERTIFICATE
, *credentials
);
707 gnutls_transport_set_ptr(conn
->session
, con
->http
.fd
);
709 error
= gnutls_handshake(conn
->session
);
711 if (error
!= GNUTLS_E_SUCCESS
)
713 cupsdLogMessage(CUPSD_LOG_ERROR
,
714 "cupsdEncryptClient: Unable to encrypt connection from %s!",
716 cupsdLogMessage(CUPSD_LOG_ERROR
, "cupsdEncryptClient: %s",
717 gnutls_strerror(error
));
719 gnutls_deinit(conn
->session
);
720 gnutls_certificate_free_credentials(*credentials
);
726 cupsdLogMessage(CUPSD_LOG_DEBUG
,
727 "cupsdEncryptClient: %d Connection from %s now encrypted.",
728 con
->http
.fd
, con
->http
.hostname
);
730 conn
->credentials
= credentials
;
731 con
->http
.tls
= conn
;
734 #elif defined(HAVE_CDSASSL)
735 OSStatus error
; /* Error info */
736 SSLContextRef conn
; /* New connection */
737 SSLProtocol tryVersion
; /* Protocol version */
738 const char *hostName
; /* Local hostname */
739 int allowExpired
; /* Allow expired certificates? */
740 int allowAnyRoot
; /* Allow any root certificate? */
741 SSLProtocol
*negVersion
; /* Negotiated protocol version */
742 SSLCipherSuite
*negCipher
; /* Negotiated cypher */
743 CFArrayRef
*peerCerts
; /* Certificates */
747 error
= SSLNewContext(true, &conn
);
752 error
= SSLSetIOFuncs(conn
, CDSAReadFunc
, CDSAWriteFunc
);
755 error
= SSLSetProtocolVersion(conn
, kSSLProtocol3
);
758 error
= SSLSetConnection(conn
, (SSLConnectionRef
)con
->http
.fd
);
762 hostName
= ServerName
; /* MRS: ??? */
763 error
= SSLSetPeerDomainName(conn
, hostName
, strlen(hostName
) + 1);
766 /* have to do these options befor setting server certs */
767 if (!error
&& allowExpired
)
768 error
= SSLSetAllowsExpiredCerts(conn
, true);
770 if (!error
&& allowAnyRoot
)
771 error
= SSLSetAllowsAnyRoot(conn
, true);
773 if (!error
&& ServerCertificatesArray
!= NULL
)
774 error
= SSLSetCertificate(conn
, ServerCertificatesArray
);
777 * Perform SSL/TLS handshake
782 error
= SSLHandshake(conn
);
784 while (error
== errSSLWouldBlock
);
788 cupsdLogMessage(CUPSD_LOG_ERROR
,
789 "cupsdEncryptClient: Unable to encrypt connection from %s!",
792 cupsdLogMessage(CUPSD_LOG_ERROR
,
793 "cupsdEncryptClient: CDSA error code is %d", error
);
795 con
->http
.error
= error
;
796 con
->http
.status
= HTTP_ERROR
;
799 SSLDisposeContext(conn
);
804 cupsdLogMessage(CUPSD_LOG_DEBUG
,
805 "cupsdEncryptClient: %d Connection from %s now encrypted.",
806 con
->http
.fd
, con
->http
.hostname
);
808 con
->http
.tls
= conn
;
813 #endif /* HAVE_GNUTLS */
818 * 'cupsdIsCGI()' - Is the resource a CGI script/program?
821 int /* O - 1 = CGI, 0 = file */
822 cupsdIsCGI(cupsd_client_t
*con
, /* I - Client connection */
823 const char *filename
, /* I - Real filename */
824 struct stat
*filestats
, /* I - File information */
825 mime_type_t
*type
) /* I - MIME type */
827 const char *options
; /* Options on URL */
830 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
831 "cupsdIsCGI(con=%p, filename=\"%s\", filestats=%p, type=%s/%s)\n",
832 con
, filename
, filestats
, type
? type
->super
: "unknown",
833 type
? type
->type
: "unknown");
836 * Get the options, if any...
839 if ((options
= strchr(con
->uri
, '?')) != NULL
)
843 * Check for known types...
846 if (!type
|| strcasecmp(type
->super
, "application"))
848 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdIsCGI: Returning 0...");
852 if (!strcasecmp(type
->type
, "x-httpd-cgi") &&
853 (filestats
->st_mode
& 0111))
856 * "application/x-httpd-cgi" is a CGI script.
859 cupsdSetString(&con
->command
, filename
);
861 filename
= strrchr(filename
, '/') + 1; /* Filename always absolute */
864 cupsdSetStringf(&con
->options
, "%s %s", filename
, options
);
866 cupsdSetStringf(&con
->options
, "%s", filename
);
868 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
869 "cupsdIsCGI: Returning 1 with command=\"%s\" and options=\"%s\"",
870 con
->command
, con
->options
);
875 else if (!strcasecmp(type
->type
, "x-httpd-java"))
878 * "application/x-httpd-java" is a Java servlet.
881 cupsdSetString(&con
->command
, CUPS_JAVA
);
884 cupsdSetStringf(&con
->options
, "java %s %s", filename
, options
);
886 cupsdSetStringf(&con
->options
, "java %s", filename
);
888 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
889 "cupsdIsCGI: Returning 1 with command=\"%s\" and options=\"%s\"",
890 con
->command
, con
->options
);
894 #endif /* HAVE_JAVA */
896 else if (!strcasecmp(type
->type
, "x-httpd-perl"))
899 * "application/x-httpd-perl" is a Perl page.
902 cupsdSetString(&con
->command
, CUPS_PERL
);
905 cupsdSetStringf(&con
->options
, "perl %s %s", filename
, options
);
907 cupsdSetStringf(&con
->options
, "perl %s", filename
);
909 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
910 "cupsdIsCGI: Returning 1 with command=\"%s\" and options=\"%s\"",
911 con
->command
, con
->options
);
915 #endif /* HAVE_PERL */
917 else if (!strcasecmp(type
->type
, "x-httpd-php"))
920 * "application/x-httpd-php" is a PHP page.
923 cupsdSetString(&con
->command
, CUPS_PHP
);
926 cupsdSetStringf(&con
->options
, "php %s %s", filename
, options
);
928 cupsdSetStringf(&con
->options
, "php %s", filename
);
930 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
931 "cupsdIsCGI: Returning 1 with command=\"%s\" and options=\"%s\"",
932 con
->command
, con
->options
);
936 #endif /* HAVE_PHP */
938 else if (!strcasecmp(type
->type
, "x-httpd-python"))
941 * "application/x-httpd-python" is a Python page.
944 cupsdSetString(&con
->command
, CUPS_PYTHON
);
947 cupsdSetStringf(&con
->options
, "python %s %s", filename
, options
);
949 cupsdSetStringf(&con
->options
, "python %s", filename
);
951 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
952 "cupsdIsCGI: Returning 1 with command=\"%s\" and options=\"%s\"",
953 con
->command
, con
->options
);
957 #endif /* HAVE_PYTHON */
959 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdIsCGI: Returning 0...");
966 * 'cupsdReadClient()' - Read data from a client.
969 int /* O - 1 on success, 0 on error */
970 cupsdReadClient(cupsd_client_t
*con
) /* I - Client to read from */
972 char line
[32768], /* Line from client... */
973 operation
[64], /* Operation code from socket */
974 version
[64], /* HTTP version number string */
975 locale
[64], /* Locale */
976 *ptr
; /* Pointer into strings */
977 int major
, minor
; /* HTTP version numbers */
978 http_status_t status
; /* Transfer status */
979 ipp_state_t ipp_state
; /* State of IPP transfer */
980 int bytes
; /* Number of bytes to POST */
981 char *filename
; /* Name of file for GET/HEAD */
982 char buf
[1024]; /* Buffer for real filename */
983 struct stat filestats
; /* File information */
984 mime_type_t
*type
; /* MIME type of file */
985 cupsd_printer_t
*p
; /* Printer */
986 static unsigned request_id
= 0; /* Request ID for temp files */
989 status
= HTTP_CONTINUE
;
991 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdReadClient: %d, used=%d, file=%d",
992 con
->http
.fd
, con
->http
.used
, con
->file
);
996 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdReadClient: http error seen...");
997 return (cupsdCloseClient(con
));
1004 * Automatically check for a SSL/TLS handshake...
1009 if (recv(con
->http
.fd
, buf
, 1, MSG_PEEK
) == 1 &&
1010 (!buf
[0] || !strchr("DGHOPT", buf
[0])))
1013 * Encrypt this connection...
1016 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1017 "cupsdReadClient: Saw first byte %02X, auto-negotiating SSL/TLS session...",
1020 cupsdEncryptClient(con
);
1024 #endif /* HAVE_SSL */
1026 switch (con
->http
.state
)
1030 * See if we've received a request line...
1033 if (httpGets(line
, sizeof(line
) - 1, HTTP(con
)) == NULL
)
1035 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1036 "cupsdReadClient: httpGets returned EOF...");
1037 return (cupsdCloseClient(con
));
1041 * Ignore blank request lines...
1044 if (line
[0] == '\0')
1048 * Clear other state variables...
1051 httpClearFields(HTTP(con
));
1053 con
->http
.activity
= time(NULL
);
1054 con
->http
.version
= HTTP_1_0
;
1055 con
->http
.keep_alive
= HTTP_KEEPALIVE_OFF
;
1056 con
->http
.data_encoding
= HTTP_ENCODE_LENGTH
;
1057 con
->http
.data_remaining
= 0;
1058 con
->http
._data_remaining
= 0;
1059 con
->operation
= HTTP_WAITING
;
1062 con
->file_ready
= 0;
1064 con
->username
[0] = '\0';
1065 con
->password
[0] = '\0';
1068 cupsdClearString(&con
->command
);
1069 cupsdClearString(&con
->options
);
1071 if (con
->language
!= NULL
)
1073 cupsLangFree(con
->language
);
1074 con
->language
= NULL
;
1078 * Grab the request line...
1081 switch (sscanf(line
, "%63s%1023s%63s", operation
, con
->uri
, version
))
1084 cupsdLogMessage(CUPSD_LOG_ERROR
,
1085 "Bad request line \"%s\" from %s!", line
,
1086 con
->http
.hostname
);
1087 cupsdSendError(con
, HTTP_BAD_REQUEST
);
1088 return (cupsdCloseClient(con
));
1090 con
->http
.version
= HTTP_0_9
;
1093 if (sscanf(version
, "HTTP/%d.%d", &major
, &minor
) != 2)
1095 cupsdLogMessage(CUPSD_LOG_ERROR
,
1096 "Bad request line \"%s\" from %s!", line
,
1097 con
->http
.hostname
);
1098 cupsdSendError(con
, HTTP_BAD_REQUEST
);
1099 return (cupsdCloseClient(con
));
1104 con
->http
.version
= (http_version_t
)(major
* 100 + minor
);
1105 if (con
->http
.version
== HTTP_1_1
&& KeepAlive
)
1106 con
->http
.keep_alive
= HTTP_KEEPALIVE_ON
;
1108 con
->http
.keep_alive
= HTTP_KEEPALIVE_OFF
;
1112 cupsdSendError(con
, HTTP_NOT_SUPPORTED
);
1113 return (cupsdCloseClient(con
));
1119 * Handle full URLs in the request line...
1122 if (con
->uri
[0] != '/' && strcmp(con
->uri
, "*"))
1124 char method
[HTTP_MAX_URI
], /* Method/scheme */
1125 userpass
[HTTP_MAX_URI
], /* Username:password */
1126 hostname
[HTTP_MAX_URI
], /* Hostname */
1127 resource
[HTTP_MAX_URI
]; /* Resource path */
1128 int port
; /* Port number */
1132 * Separate the URI into its components...
1135 httpSeparate(con
->uri
, method
, userpass
, hostname
, &port
, resource
);
1138 * Only allow URIs with the servername, localhost, or an IP
1142 if (strcasecmp(hostname
, ServerName
) &&
1143 strcasecmp(hostname
, "localhost") &&
1144 !isdigit(hostname
[0]))
1147 * Nope, we don't do proxies...
1150 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad URI \"%s\" in request!",
1152 cupsdSendError(con
, HTTP_METHOD_NOT_ALLOWED
);
1153 return (cupsdCloseClient(con
));
1157 * Copy the resource portion back into the URI; both resource and
1158 * con->uri are HTTP_MAX_URI bytes in size...
1161 strcpy(con
->uri
, resource
);
1165 * Process the request...
1168 if (!strcmp(operation
, "GET"))
1169 con
->http
.state
= HTTP_GET
;
1170 else if (!strcmp(operation
, "PUT"))
1171 con
->http
.state
= HTTP_PUT
;
1172 else if (!strcmp(operation
, "POST"))
1173 con
->http
.state
= HTTP_POST
;
1174 else if (!strcmp(operation
, "DELETE"))
1175 con
->http
.state
= HTTP_DELETE
;
1176 else if (!strcmp(operation
, "TRACE"))
1177 con
->http
.state
= HTTP_TRACE
;
1178 else if (!strcmp(operation
, "OPTIONS"))
1179 con
->http
.state
= HTTP_OPTIONS
;
1180 else if (!strcmp(operation
, "HEAD"))
1181 con
->http
.state
= HTTP_HEAD
;
1184 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad operation \"%s\"!", operation
);
1185 cupsdSendError(con
, HTTP_BAD_REQUEST
);
1186 return (cupsdCloseClient(con
));
1189 con
->start
= time(NULL
);
1190 con
->operation
= con
->http
.state
;
1192 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdReadClient: %d %s %s HTTP/%d.%d", con
->http
.fd
,
1193 operation
, con
->uri
,
1194 con
->http
.version
/ 100, con
->http
.version
% 100);
1196 con
->http
.status
= HTTP_OK
;
1206 * Parse incoming parameters until the status changes...
1209 status
= httpUpdate(HTTP(con
));
1211 if (status
!= HTTP_OK
&& status
!= HTTP_CONTINUE
)
1213 cupsdSendError(con
, HTTP_BAD_REQUEST
);
1214 return (cupsdCloseClient(con
));
1219 break; /* Anti-compiler-warning-code */
1223 * Handle new transfers...
1226 if (status
== HTTP_OK
)
1228 if (con
->http
.fields
[HTTP_FIELD_ACCEPT_LANGUAGE
][0])
1231 * Figure out the locale from the Accept-Language and Content-Type
1235 if ((ptr
= strchr(con
->http
.fields
[HTTP_FIELD_ACCEPT_LANGUAGE
], ',')) != NULL
)
1238 if ((ptr
= strchr(con
->http
.fields
[HTTP_FIELD_ACCEPT_LANGUAGE
], ';')) != NULL
)
1241 if ((ptr
= strstr(con
->http
.fields
[HTTP_FIELD_CONTENT_TYPE
], "charset=")) != NULL
)
1244 * Combine language and charset, and trim any extra params in the
1248 snprintf(locale
, sizeof(locale
), "%s.%s",
1249 con
->http
.fields
[HTTP_FIELD_ACCEPT_LANGUAGE
], ptr
+ 8);
1251 if ((ptr
= strchr(locale
, ',')) != NULL
)
1255 snprintf(locale
, sizeof(locale
), "%s.%s",
1256 con
->http
.fields
[HTTP_FIELD_ACCEPT_LANGUAGE
], DefaultCharset
);
1258 con
->language
= cupsLangGet(locale
);
1261 con
->language
= cupsLangGet(DefaultLocale
);
1265 if (!strncmp(con
->http
.fields
[HTTP_FIELD_CONNECTION
], "Keep-Alive", 10) &&
1267 con
->http
.keep_alive
= HTTP_KEEPALIVE_ON
;
1269 if (!con
->http
.fields
[HTTP_FIELD_HOST
][0] &&
1270 con
->http
.version
>= HTTP_1_1
)
1273 * HTTP/1.1 and higher require the "Host:" field...
1276 if (!cupsdSendError(con
, HTTP_BAD_REQUEST
))
1277 return (cupsdCloseClient(con
));
1279 else if (con
->operation
== HTTP_OPTIONS
)
1282 * Do OPTIONS command...
1285 if ((con
->best
= cupsdFindBest(con
->uri
, con
->http
.state
)) != NULL
&&
1286 con
->best
->type
!= AUTH_NONE
)
1288 if (!cupsdSendHeader(con
, HTTP_UNAUTHORIZED
, NULL
))
1289 return (cupsdCloseClient(con
));
1292 if (!strcasecmp(con
->http
.fields
[HTTP_FIELD_CONNECTION
], "Upgrade") &&
1293 con
->http
.tls
== NULL
)
1297 * Do encryption stuff...
1300 if (!cupsdSendHeader(con
, HTTP_SWITCHING_PROTOCOLS
, NULL
))
1301 return (cupsdCloseClient(con
));
1303 httpPrintf(HTTP(con
), "Connection: Upgrade\r\n");
1304 httpPrintf(HTTP(con
), "Upgrade: TLS/1.0,HTTP/1.1\r\n");
1305 httpPrintf(HTTP(con
), "Content-Length: 0\r\n");
1306 httpPrintf(HTTP(con
), "\r\n");
1308 cupsdEncryptClient(con
);
1310 if (!cupsdSendError(con
, HTTP_NOT_IMPLEMENTED
))
1311 return (cupsdCloseClient(con
));
1312 #endif /* HAVE_SSL */
1315 if (con
->http
.expect
)
1317 /**** TODO: send expected header ****/
1320 if (!cupsdSendHeader(con
, HTTP_OK
, NULL
))
1321 return (cupsdCloseClient(con
));
1323 httpPrintf(HTTP(con
), "Allow: GET, HEAD, OPTIONS, POST, PUT\r\n");
1324 httpPrintf(HTTP(con
), "Content-Length: 0\r\n");
1325 httpPrintf(HTTP(con
), "\r\n");
1327 else if (!is_path_absolute(con
->uri
))
1330 * Protect against malicious users!
1333 if (!cupsdSendError(con
, HTTP_FORBIDDEN
))
1334 return (cupsdCloseClient(con
));
1338 if (!strcasecmp(con
->http
.fields
[HTTP_FIELD_CONNECTION
], "Upgrade") &&
1339 con
->http
.tls
== NULL
)
1343 * Do encryption stuff...
1346 if (!cupsdSendHeader(con
, HTTP_SWITCHING_PROTOCOLS
, NULL
))
1347 return (cupsdCloseClient(con
));
1349 httpPrintf(HTTP(con
), "Connection: Upgrade\r\n");
1350 httpPrintf(HTTP(con
), "Upgrade: TLS/1.0,HTTP/1.1\r\n");
1351 httpPrintf(HTTP(con
), "Content-Length: 0\r\n");
1352 httpPrintf(HTTP(con
), "\r\n");
1354 cupsdEncryptClient(con
);
1356 if (!cupsdSendError(con
, HTTP_NOT_IMPLEMENTED
))
1357 return (cupsdCloseClient(con
));
1358 #endif /* HAVE_SSL */
1361 con
->best
= cupsdFindBest(con
->uri
, con
->http
.state
);
1363 if ((status
= cupsdIsAuthorized(con
, NULL
)) != HTTP_OK
)
1365 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1366 "cupsdReadClient: Unauthorized request for %s...\n",
1368 cupsdSendError(con
, status
);
1369 return (cupsdCloseClient(con
));
1372 if (con
->http
.expect
)
1374 /**** TODO: send expected header ****/
1377 switch (con
->http
.state
)
1379 case HTTP_GET_SEND
:
1380 if (!strncmp(con
->uri
, "/printers/", 10) &&
1381 !strcmp(con
->uri
+ strlen(con
->uri
) - 4, ".ppd"))
1384 * Send PPD file - get the real printer name since printer
1385 * names are not case sensitive but filenames can be...
1388 con
->uri
[strlen(con
->uri
) - 4] = '\0'; /* Drop ".ppd" */
1390 if ((p
= cupsdFindPrinter(con
->uri
+ 10)) != NULL
)
1391 snprintf(con
->uri
, sizeof(con
->uri
), "/ppd/%s.ppd", p
->name
);
1394 if (!cupsdSendError(con
, HTTP_NOT_FOUND
))
1395 return (cupsdCloseClient(con
));
1401 if ((!strncmp(con
->uri
, "/admin", 6) &&
1402 strncmp(con
->uri
, "/admin/conf/", 12) &&
1403 strncmp(con
->uri
, "/admin/log/", 11)) ||
1404 !strncmp(con
->uri
, "/printers", 9) ||
1405 !strncmp(con
->uri
, "/classes", 8) ||
1406 !strncmp(con
->uri
, "/help", 5) ||
1407 !strncmp(con
->uri
, "/jobs", 5))
1410 * Send CGI output...
1413 if (!strncmp(con
->uri
, "/admin", 6))
1415 cupsdSetStringf(&con
->command
, "%s/cgi-bin/admin.cgi",
1418 if ((ptr
= strchr(con
->uri
+ 6, '?')) != NULL
)
1419 cupsdSetStringf(&con
->options
, "admin%s", ptr
);
1421 cupsdSetString(&con
->options
, "admin");
1423 else if (!strncmp(con
->uri
, "/printers", 9))
1425 cupsdSetStringf(&con
->command
, "%s/cgi-bin/printers.cgi",
1427 cupsdSetString(&con
->options
, con
->uri
+ 9);
1429 else if (!strncmp(con
->uri
, "/classes", 8))
1431 cupsdSetStringf(&con
->command
, "%s/cgi-bin/classes.cgi",
1433 cupsdSetString(&con
->options
, con
->uri
+ 8);
1435 else if (!strncmp(con
->uri
, "/jobs", 5))
1437 cupsdSetStringf(&con
->command
, "%s/cgi-bin/jobs.cgi",
1439 cupsdSetString(&con
->options
, con
->uri
+ 5);
1443 cupsdSetStringf(&con
->command
, "%s/cgi-bin/help.cgi",
1445 cupsdSetString(&con
->options
, con
->uri
+ 5);
1448 if (con
->options
[0] == '/')
1449 _cups_strcpy(con
->options
, con
->options
+ 1);
1451 if (!cupsdSendCommand(con
, con
->command
, con
->options
, 0))
1453 if (!cupsdSendError(con
, HTTP_NOT_FOUND
))
1454 return (cupsdCloseClient(con
));
1457 cupsdLogRequest(con
, HTTP_OK
);
1459 if (con
->http
.version
<= HTTP_1_0
)
1460 con
->http
.keep_alive
= HTTP_KEEPALIVE_OFF
;
1462 else if ((!strncmp(con
->uri
, "/admin/conf/", 12) &&
1463 (strchr(con
->uri
+ 12, '/') ||
1464 strlen(con
->uri
) == 12)) ||
1465 (!strncmp(con
->uri
, "/admin/log/", 11) &&
1466 (strchr(con
->uri
+ 11, '/') ||
1467 strlen(con
->uri
) == 11)))
1470 * GET can only be done to configuration files under
1474 if (!cupsdSendError(con
, HTTP_FORBIDDEN
))
1475 return (cupsdCloseClient(con
));
1485 if ((filename
= get_file(con
, &filestats
, buf
,
1486 sizeof(buf
))) == NULL
)
1488 if (!cupsdSendError(con
, HTTP_NOT_FOUND
))
1489 return (cupsdCloseClient(con
));
1494 type
= mimeFileType(MimeDatabase
, filename
, NULL
);
1496 if (cupsdIsCGI(con
, filename
, &filestats
, type
))
1499 * Note: con->command and con->options were set by
1503 if (!cupsdSendCommand(con
, con
->command
, con
->options
, 0))
1505 if (!cupsdSendError(con
, HTTP_NOT_FOUND
))
1506 return (cupsdCloseClient(con
));
1509 cupsdLogRequest(con
, HTTP_OK
);
1511 if (con
->http
.version
<= HTTP_1_0
)
1512 con
->http
.keep_alive
= HTTP_KEEPALIVE_OFF
;
1516 if (!check_if_modified(con
, &filestats
))
1518 if (!cupsdSendError(con
, HTTP_NOT_MODIFIED
))
1519 return (cupsdCloseClient(con
));
1524 strcpy(line
, "text/plain");
1526 snprintf(line
, sizeof(line
), "%s/%s", type
->super
, type
->type
);
1528 if (!cupsdSendFile(con
, HTTP_OK
, filename
, line
, &filestats
))
1529 return (cupsdCloseClient(con
));
1534 case HTTP_POST_RECV
:
1536 * See if the POST request includes a Content-Length field, and if
1537 * so check the length against any limits that are set...
1540 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "POST %s", con
->uri
);
1541 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "CONTENT_TYPE = %s",
1542 con
->http
.fields
[HTTP_FIELD_CONTENT_TYPE
]);
1544 if (con
->http
.fields
[HTTP_FIELD_CONTENT_LENGTH
][0] &&
1545 MaxRequestSize
> 0 &&
1546 con
->http
.data_remaining
> MaxRequestSize
)
1549 * Request too large...
1552 if (!cupsdSendError(con
, HTTP_REQUEST_TOO_LARGE
))
1553 return (cupsdCloseClient(con
));
1557 else if (con
->http
.data_remaining
< 0)
1560 * Negative content lengths are invalid!
1563 if (!cupsdSendError(con
, HTTP_BAD_REQUEST
))
1564 return (cupsdCloseClient(con
));
1570 * See what kind of POST request this is; for IPP requests the
1571 * content-type field will be "application/ipp"...
1574 if (!strcmp(con
->http
.fields
[HTTP_FIELD_CONTENT_TYPE
],
1576 con
->request
= ippNew();
1577 else if ((!strncmp(con
->uri
, "/admin", 6) &&
1578 strncmp(con
->uri
, "/admin/conf/", 12) &&
1579 strncmp(con
->uri
, "/admin/log/", 11)) ||
1580 !strncmp(con
->uri
, "/printers", 9) ||
1581 !strncmp(con
->uri
, "/classes", 8) ||
1582 !strncmp(con
->uri
, "/help", 5) ||
1583 !strncmp(con
->uri
, "/jobs", 5))
1589 if (!strncmp(con
->uri
, "/admin", 6))
1591 cupsdSetStringf(&con
->command
, "%s/cgi-bin/admin.cgi",
1594 if ((ptr
= strchr(con
->uri
+ 6, '?')) != NULL
)
1595 cupsdSetStringf(&con
->options
, "admin%s", ptr
);
1597 cupsdSetString(&con
->options
, "admin");
1599 else if (!strncmp(con
->uri
, "/printers", 9))
1601 cupsdSetStringf(&con
->command
, "%s/cgi-bin/printers.cgi",
1603 cupsdSetString(&con
->options
, con
->uri
+ 9);
1605 else if (!strncmp(con
->uri
, "/classes", 8))
1607 cupsdSetStringf(&con
->command
, "%s/cgi-bin/classes.cgi",
1609 cupsdSetString(&con
->options
, con
->uri
+ 8);
1611 else if (!strncmp(con
->uri
, "/jobs", 5))
1613 cupsdSetStringf(&con
->command
, "%s/cgi-bin/jobs.cgi",
1615 cupsdSetString(&con
->options
, con
->uri
+ 5);
1619 cupsdSetStringf(&con
->command
, "%s/cgi-bin/help.cgi",
1621 cupsdSetString(&con
->options
, con
->uri
+ 5);
1624 if (con
->options
[0] == '/')
1625 _cups_strcpy(con
->options
, con
->options
+ 1);
1627 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1628 "cupsdReadClient: %d command=\"%s\", options = \"%s\"",
1629 con
->http
.fd
, con
->command
, con
->options
);
1631 if (con
->http
.version
<= HTTP_1_0
)
1632 con
->http
.keep_alive
= HTTP_KEEPALIVE_OFF
;
1640 if ((filename
= get_file(con
, &filestats
, buf
,
1641 sizeof(buf
))) == NULL
)
1643 if (!cupsdSendError(con
, HTTP_NOT_FOUND
))
1644 return (cupsdCloseClient(con
));
1649 type
= mimeFileType(MimeDatabase
, filename
, NULL
);
1651 if (!cupsdIsCGI(con
, filename
, &filestats
, type
))
1654 * Only POST to CGI's...
1657 if (!cupsdSendError(con
, HTTP_UNAUTHORIZED
))
1658 return (cupsdCloseClient(con
));
1663 case HTTP_PUT_RECV
:
1665 * Validate the resource name...
1668 if (strncmp(con
->uri
, "/admin/conf/", 12) ||
1669 strchr(con
->uri
+ 12, '/') ||
1670 strlen(con
->uri
) == 12)
1673 * PUT can only be done to configuration files under
1677 if (!cupsdSendError(con
, HTTP_FORBIDDEN
))
1678 return (cupsdCloseClient(con
));
1684 * See if the PUT request includes a Content-Length field, and if
1685 * so check the length against any limits that are set...
1688 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "PUT %s", con
->uri
);
1689 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "CONTENT_TYPE = %s",
1690 con
->http
.fields
[HTTP_FIELD_CONTENT_TYPE
]);
1692 if (con
->http
.fields
[HTTP_FIELD_CONTENT_LENGTH
][0] &&
1693 MaxRequestSize
> 0 &&
1694 con
->http
.data_remaining
> MaxRequestSize
)
1697 * Request too large...
1700 if (!cupsdSendError(con
, HTTP_REQUEST_TOO_LARGE
))
1701 return (cupsdCloseClient(con
));
1705 else if (con
->http
.data_remaining
< 0)
1708 * Negative content lengths are invalid!
1711 if (!cupsdSendError(con
, HTTP_BAD_REQUEST
))
1712 return (cupsdCloseClient(con
));
1718 * Open a temporary file to hold the request...
1721 cupsdSetStringf(&con
->filename
, "%s/%08x", RequestRoot
,
1723 con
->file
= open(con
->filename
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0640);
1725 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1726 "cupsdReadClient: %d REQUEST %s=%d", con
->http
.fd
,
1727 con
->filename
, con
->file
);
1731 if (!cupsdSendError(con
, HTTP_REQUEST_TOO_LARGE
))
1732 return (cupsdCloseClient(con
));
1735 fchmod(con
->file
, 0640);
1736 fchown(con
->file
, RunUser
, Group
);
1737 fcntl(con
->file
, F_SETFD
, fcntl(con
->file
, F_GETFD
) | FD_CLOEXEC
);
1742 cupsdSendError(con
, HTTP_NOT_IMPLEMENTED
);
1743 return (cupsdCloseClient(con
));
1746 if (!strncmp(con
->uri
, "/printers/", 10) &&
1747 !strcmp(con
->uri
+ strlen(con
->uri
) - 4, ".ppd"))
1750 * Send PPD file - get the real printer name since printer
1751 * names are not case sensitive but filenames can be...
1754 con
->uri
[strlen(con
->uri
) - 4] = '\0'; /* Drop ".ppd" */
1756 if ((p
= cupsdFindPrinter(con
->uri
+ 10)) != NULL
)
1757 snprintf(con
->uri
, sizeof(con
->uri
), "/ppd/%s.ppd", p
->name
);
1760 if (!cupsdSendError(con
, HTTP_NOT_FOUND
))
1761 return (cupsdCloseClient(con
));
1767 if ((!strncmp(con
->uri
, "/admin", 6) &&
1768 strncmp(con
->uri
, "/admin/conf/", 12) &&
1769 strncmp(con
->uri
, "/admin/log/", 11)) ||
1770 !strncmp(con
->uri
, "/printers", 9) ||
1771 !strncmp(con
->uri
, "/classes", 8) ||
1772 !strncmp(con
->uri
, "/help", 5) ||
1773 !strncmp(con
->uri
, "/jobs", 5))
1779 if (!cupsdSendHeader(con
, HTTP_OK
, "text/html"))
1780 return (cupsdCloseClient(con
));
1782 if (httpPrintf(HTTP(con
), "\r\n") < 0)
1783 return (cupsdCloseClient(con
));
1785 cupsdLogRequest(con
, HTTP_OK
);
1787 else if ((!strncmp(con
->uri
, "/admin/conf/", 12) &&
1788 (strchr(con
->uri
+ 12, '/') ||
1789 strlen(con
->uri
) == 12)) ||
1790 (!strncmp(con
->uri
, "/admin/log/", 11) &&
1791 (strchr(con
->uri
+ 11, '/') ||
1792 strlen(con
->uri
) == 11)))
1795 * HEAD can only be done to configuration files under
1799 if (!cupsdSendError(con
, HTTP_FORBIDDEN
))
1800 return (cupsdCloseClient(con
));
1804 else if ((filename
= get_file(con
, &filestats
, buf
,
1805 sizeof(buf
))) == NULL
)
1807 if (!cupsdSendHeader(con
, HTTP_NOT_FOUND
, "text/html"))
1808 return (cupsdCloseClient(con
));
1810 cupsdLogRequest(con
, HTTP_NOT_FOUND
);
1812 else if (!check_if_modified(con
, &filestats
))
1814 if (!cupsdSendError(con
, HTTP_NOT_MODIFIED
))
1815 return (cupsdCloseClient(con
));
1817 cupsdLogRequest(con
, HTTP_NOT_MODIFIED
);
1825 type
= mimeFileType(MimeDatabase
, filename
, NULL
);
1827 strcpy(line
, "text/plain");
1829 snprintf(line
, sizeof(line
), "%s/%s", type
->super
, type
->type
);
1831 if (!cupsdSendHeader(con
, HTTP_OK
, line
))
1832 return (cupsdCloseClient(con
));
1834 if (httpPrintf(HTTP(con
), "Last-Modified: %s\r\n",
1835 httpGetDateString(filestats
.st_mtime
)) < 0)
1836 return (cupsdCloseClient(con
));
1838 if (httpPrintf(HTTP(con
), "Content-Length: %lu\r\n",
1839 (unsigned long)filestats
.st_size
) < 0)
1840 return (cupsdCloseClient(con
));
1842 cupsdLogRequest(con
, HTTP_OK
);
1845 if (httpPrintf(HTTP(con
), "\r\n") < 0)
1846 return (cupsdCloseClient(con
));
1848 con
->http
.state
= HTTP_WAITING
;
1852 break; /* Anti-compiler-warning-code */
1858 * Handle any incoming data...
1861 switch (con
->http
.state
)
1863 case HTTP_PUT_RECV
:
1864 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1865 "cupsdReadClient: %d con->data_encoding=HTTP_ENCODE_%s, "
1866 "con->data_remaining=" CUPS_LLFMT
", con->file=%d",
1868 con
->http
.data_encoding
== HTTP_ENCODE_CHUNKED
?
1869 "CHUNKED" : "LENGTH",
1870 CUPS_LLCAST con
->http
.data_remaining
, con
->file
);
1872 if ((bytes
= httpRead(HTTP(con
), line
, sizeof(line
))) < 0)
1873 return (cupsdCloseClient(con
));
1876 con
->bytes
+= bytes
;
1878 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1879 "cupsdReadClient: %d writing %d bytes to %d",
1880 con
->http
.fd
, bytes
, con
->file
);
1882 if (write(con
->file
, line
, bytes
) < bytes
)
1884 cupsdLogMessage(CUPSD_LOG_ERROR
,
1885 "cupsdReadClient: Unable to write %d bytes to %s: %s",
1886 bytes
, con
->filename
, strerror(errno
));
1888 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1889 "cupsdReadClient: Closing data file %d...",
1894 unlink(con
->filename
);
1895 cupsdClearString(&con
->filename
);
1897 if (!cupsdSendError(con
, HTTP_REQUEST_TOO_LARGE
))
1898 return (cupsdCloseClient(con
));
1902 if (con
->http
.state
== HTTP_WAITING
)
1905 * End of file, see how big it is...
1908 fstat(con
->file
, &filestats
);
1910 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1911 "cupsdReadClient: %d Closing data file %d, size="
1913 con
->http
.fd
, con
->file
,
1914 CUPS_LLCAST filestats
.st_size
);
1919 if (filestats
.st_size
> MaxRequestSize
&&
1923 * Request is too big; remove it and send an error...
1926 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1927 "cupsdReadClient: %d Removing temp file %s",
1928 con
->http
.fd
, con
->filename
);
1929 unlink(con
->filename
);
1930 cupsdClearString(&con
->filename
);
1932 if (!cupsdSendError(con
, HTTP_REQUEST_TOO_LARGE
))
1933 return (cupsdCloseClient(con
));
1937 * Install the configuration file...
1940 status
= install_conf_file(con
);
1943 * Return the status to the client...
1946 if (!cupsdSendError(con
, status
))
1947 return (cupsdCloseClient(con
));
1951 case HTTP_POST_RECV
:
1952 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1953 "cupsdReadClient: %d con->data_encoding=HTTP_ENCODE_"
1954 "%s, con->data_remaining=" CUPS_LLFMT
", con->file=%d",
1956 con
->http
.data_encoding
== HTTP_ENCODE_CHUNKED
?
1957 "CHUNKED" : "LENGTH",
1958 CUPS_LLCAST con
->http
.data_remaining
, con
->file
);
1960 if (con
->request
!= NULL
)
1963 * Grab any request data from the connection...
1966 if ((ipp_state
= ippRead(&(con
->http
), con
->request
)) == IPP_ERROR
)
1968 cupsdLogMessage(CUPSD_LOG_ERROR
,
1969 "cupsdReadClient: %d IPP Read Error!",
1972 cupsdSendError(con
, HTTP_BAD_REQUEST
);
1973 return (cupsdCloseClient(con
));
1975 else if (ipp_state
!= IPP_DATA
)
1977 if (con
->http
.state
== HTTP_POST_SEND
)
1979 cupsdSendError(con
, HTTP_BAD_REQUEST
);
1980 return (cupsdCloseClient(con
));
1986 con
->bytes
+= ippLength(con
->request
);
1989 if (con
->file
< 0 && con
->http
.state
!= HTTP_POST_SEND
)
1992 * Create a file as needed for the request data...
1995 cupsdSetStringf(&con
->filename
, "%s/%08x", RequestRoot
, request_id
++);
1996 con
->file
= open(con
->filename
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0640);
1998 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdReadClient: %d REQUEST %s=%d", con
->http
.fd
,
1999 con
->filename
, con
->file
);
2003 if (!cupsdSendError(con
, HTTP_REQUEST_TOO_LARGE
))
2004 return (cupsdCloseClient(con
));
2007 fchmod(con
->file
, 0640);
2008 fchown(con
->file
, RunUser
, Group
);
2009 fcntl(con
->file
, F_SETFD
, fcntl(con
->file
, F_GETFD
) | FD_CLOEXEC
);
2012 if (con
->http
.state
!= HTTP_POST_SEND
)
2014 if ((bytes
= httpRead(HTTP(con
), line
, sizeof(line
))) < 0)
2015 return (cupsdCloseClient(con
));
2018 con
->bytes
+= bytes
;
2020 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2021 "cupsdReadClient: %d writing %d bytes to %d",
2022 con
->http
.fd
, bytes
, con
->file
);
2024 if (write(con
->file
, line
, bytes
) < bytes
)
2026 cupsdLogMessage(CUPSD_LOG_ERROR
,
2027 "cupsdReadClient: Unable to write %d bytes to %s: %s",
2028 bytes
, con
->filename
, strerror(errno
));
2030 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2031 "cupsdReadClient: Closing file %d...",
2036 unlink(con
->filename
);
2037 cupsdClearString(&con
->filename
);
2039 if (!cupsdSendError(con
, HTTP_REQUEST_TOO_LARGE
))
2040 return (cupsdCloseClient(con
));
2043 else if (con
->http
.state
== HTTP_POST_RECV
)
2044 return (1); /* ??? */
2045 else if (con
->http
.state
!= HTTP_POST_SEND
)
2046 return (cupsdCloseClient(con
));
2049 if (con
->http
.state
== HTTP_POST_SEND
)
2053 fstat(con
->file
, &filestats
);
2055 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2056 "cupsdReadClient: %d Closing data file %d, "
2057 "size=" CUPS_LLFMT
".",
2058 con
->http
.fd
, con
->file
,
2059 CUPS_LLCAST filestats
.st_size
);
2064 if (filestats
.st_size
> MaxRequestSize
&&
2068 * Request is too big; remove it and send an error...
2071 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2072 "cupsdReadClient: %d Removing temp file %s",
2073 con
->http
.fd
, con
->filename
);
2074 unlink(con
->filename
);
2075 cupsdClearString(&con
->filename
);
2080 * Delete any IPP request data...
2083 ippDelete(con
->request
);
2084 con
->request
= NULL
;
2087 if (!cupsdSendError(con
, HTTP_REQUEST_TOO_LARGE
))
2088 return (cupsdCloseClient(con
));
2093 if (!cupsdSendCommand(con
, con
->command
, con
->options
, 0))
2095 if (!cupsdSendError(con
, HTTP_NOT_FOUND
))
2096 return (cupsdCloseClient(con
));
2099 cupsdLogRequest(con
, HTTP_OK
);
2104 return (cupsdProcessIPPRequest(con
));
2109 break; /* Anti-compiler-warning-code */
2112 if (!con
->http
.keep_alive
&& con
->http
.state
== HTTP_WAITING
)
2113 return (cupsdCloseClient(con
));
2120 * 'cupsdSendCommand()' - Send output from a command via HTTP.
2123 int /* O - 1 on success, 0 on failure */
2125 cupsd_client_t
*con
, /* I - Client connection */
2126 char *command
, /* I - Command to run */
2127 char *options
, /* I - Command-line options */
2128 int root
) /* I - Run as root? */
2130 int fd
; /* Standard input file descriptor */
2134 fd
= open(con
->filename
, O_RDONLY
);
2136 fd
= open("/dev/null", O_RDONLY
);
2140 cupsdLogMessage(CUPSD_LOG_ERROR
,
2141 "cupsdSendCommand: %d Unable to open \"%s\" for reading: %s",
2142 con
->http
.fd
, con
->filename
? con
->filename
: "/dev/null",
2147 fcntl(fd
, F_SETFD
, fcntl(fd
, F_GETFD
) | FD_CLOEXEC
);
2149 con
->pipe_pid
= pipe_command(con
, fd
, &(con
->file
), command
, options
, root
);
2153 cupsdLogMessage(CUPSD_LOG_INFO
, "Started \"%s\" (pid=%d)", command
,
2156 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdSendCommand: %d file=%d",
2157 con
->http
.fd
, con
->file
);
2159 if (con
->pipe_pid
== 0)
2162 fcntl(con
->file
, F_SETFD
, fcntl(con
->file
, F_GETFD
) | FD_CLOEXEC
);
2164 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2165 "cupsdSendCommand: Adding fd %d to InputSet...", con
->file
);
2166 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2167 "cupsdSendCommand: Adding fd %d to OutputSet...",
2170 FD_SET(con
->file
, InputSet
);
2171 FD_SET(con
->http
.fd
, OutputSet
);
2173 con
->sent_header
= 0;
2174 con
->file_ready
= 0;
2175 con
->got_fields
= 0;
2183 * 'cupsdSendError()' - Send an error message via HTTP.
2186 int /* O - 1 if successful, 0 otherwise */
2187 cupsdSendError(cupsd_client_t
*con
, /* I - Connection */
2188 http_status_t code
) /* I - Error code */
2190 char message
[1024]; /* Message for user */
2194 * Put the request in the access_log file...
2197 cupsdLogRequest(con
, code
);
2199 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdSendError: %d code=%d (%s)",
2200 con
->http
.fd
, code
, httpStatus(code
));
2203 * To work around bugs in some proxies, don't use Keep-Alive for some
2207 if (code
>= HTTP_BAD_REQUEST
)
2208 con
->http
.keep_alive
= HTTP_KEEPALIVE_OFF
;
2211 * Send an error message back to the client. If the error code is a
2212 * 400 or 500 series, make sure the message contains some text, too!
2215 if (!cupsdSendHeader(con
, code
, NULL
))
2219 if (code
== HTTP_UPGRADE_REQUIRED
)
2220 if (httpPrintf(HTTP(con
), "Connection: Upgrade\r\n") < 0)
2223 if (httpPrintf(HTTP(con
), "Upgrade: TLS/1.0,HTTP/1.1\r\n") < 0)
2225 #endif /* HAVE_SSL */
2227 if ((con
->http
.version
>= HTTP_1_1
&& !con
->http
.keep_alive
) ||
2228 (code
>= HTTP_BAD_REQUEST
&& code
!= HTTP_UPGRADE_REQUIRED
))
2230 if (httpPrintf(HTTP(con
), "Connection: close\r\n") < 0)
2234 if (code
>= HTTP_BAD_REQUEST
)
2237 * Send a human-readable error message.
2240 snprintf(message
, sizeof(message
),
2241 "<HTML><HEAD><TITLE>%d %s</TITLE></HEAD>"
2242 "<BODY><H1>%s</H1>%s</BODY></HTML>\n",
2243 code
, httpStatus(code
), httpStatus(code
),
2244 con
->language
? con
->language
->messages
[code
] :
2247 if (httpPrintf(HTTP(con
), "Content-Type: text/html; charset=utf-8\r\n") < 0)
2249 if (httpPrintf(HTTP(con
), "Content-Length: %d\r\n",
2250 (int)strlen(message
)) < 0)
2252 if (httpPrintf(HTTP(con
), "\r\n") < 0)
2254 if (httpPrintf(HTTP(con
), "%s", message
) < 0)
2257 else if (httpPrintf(HTTP(con
), "\r\n") < 0)
2260 con
->http
.state
= HTTP_WAITING
;
2267 * 'cupsdSendFile()' - Send a file via HTTP.
2270 int /* O - 0 on failure, 1 on success */
2271 cupsdSendFile(cupsd_client_t
*con
, /* I - Client connection */
2272 http_status_t code
, /* I - HTTP status */
2273 char *filename
, /* I - Filename */
2274 char *type
, /* I - File type */
2275 struct stat
*filestats
)/* O - File information */
2277 con
->file
= open(filename
, O_RDONLY
);
2279 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdSendFile: %d file=%d", con
->http
.fd
,
2285 fcntl(con
->file
, F_SETFD
, fcntl(con
->file
, F_GETFD
) | FD_CLOEXEC
);
2289 if (!cupsdSendHeader(con
, code
, type
))
2292 if (httpPrintf(HTTP(con
), "Last-Modified: %s\r\n",
2293 httpGetDateString(filestats
->st_mtime
)) < 0)
2295 if (httpPrintf(HTTP(con
), "Content-Length: " CUPS_LLFMT
"\r\n",
2296 CUPS_LLCAST filestats
->st_size
) < 0)
2298 if (httpPrintf(HTTP(con
), "\r\n") < 0)
2301 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2302 "cupsdSendFile: Adding fd %d to OutputSet...", con
->http
.fd
);
2304 FD_SET(con
->http
.fd
, OutputSet
);
2311 * 'cupsdSendHeader()' - Send an HTTP request.
2314 int /* O - 1 on success, 0 on failure */
2315 cupsdSendHeader(cupsd_client_t
*con
, /* I - Client to send to */
2316 http_status_t code
, /* I - HTTP status code */
2317 char *type
) /* I - MIME type of document */
2319 if (httpPrintf(HTTP(con
), "HTTP/%d.%d %d %s\r\n", con
->http
.version
/ 100,
2320 con
->http
.version
% 100, code
, httpStatus(code
)) < 0)
2322 if (httpPrintf(HTTP(con
), "Date: %s\r\n", httpGetDateString(time(NULL
))) < 0)
2325 if (httpPrintf(HTTP(con
), "Server: %s\r\n", ServerHeader
) < 0)
2327 if (con
->http
.keep_alive
&& con
->http
.version
>= HTTP_1_0
)
2329 if (httpPrintf(HTTP(con
), "Connection: Keep-Alive\r\n") < 0)
2331 if (httpPrintf(HTTP(con
), "Keep-Alive: timeout=%d\r\n",
2332 KeepAliveTimeout
) < 0)
2335 if (code
== HTTP_METHOD_NOT_ALLOWED
)
2336 if (httpPrintf(HTTP(con
), "Allow: GET, HEAD, OPTIONS, POST\r\n") < 0)
2339 if (code
== HTTP_UNAUTHORIZED
)
2341 int auth_type
; /* Authentication type */
2344 if (!con
->best
|| con
->best
->type
== AUTH_NONE
)
2345 auth_type
= DefaultAuthType
;
2347 auth_type
= con
->best
->type
;
2349 if (auth_type
!= AUTH_DIGEST
)
2351 if (httpPrintf(HTTP(con
),
2352 "WWW-Authenticate: Basic realm=\"CUPS\"\r\n") < 0)
2357 if (httpPrintf(HTTP(con
),
2358 "WWW-Authenticate: Digest realm=\"CUPS\", nonce=\"%s\"\r\n",
2359 con
->http
.hostname
) < 0)
2364 if (con
->language
!= NULL
)
2366 if (httpPrintf(HTTP(con
), "Content-Language: %s\r\n",
2367 con
->language
->language
) < 0)
2373 if (!strcmp(type
, "text/html"))
2375 if (httpPrintf(HTTP(con
),
2376 "Content-Type: text/html; charset=utf-8\r\n") < 0)
2379 else if (httpPrintf(HTTP(con
), "Content-Type: %s\r\n", type
) < 0)
2388 * 'cupsdUpdateCGI()' - Read status messages from CGI scripts and programs.
2392 cupsdUpdateCGI(void)
2394 char *ptr
, /* Pointer to end of line in buffer */
2395 message
[1024]; /* Pointer to message text */
2396 int loglevel
; /* Log level for message */
2399 while ((ptr
= cupsdStatBufUpdate(CGIStatusBuffer
, &loglevel
,
2400 message
, sizeof(message
))) != NULL
)
2401 if (!strchr(CGIStatusBuffer
->buffer
, '\n'))
2407 * Fatal error on pipe - should never happen!
2410 cupsdLogMessage(CUPSD_LOG_CRIT
,
2411 "cupsdUpdateCGI: error reading from CGI error pipe - %s",
2418 * 'cupsdWriteClient()' - Write data to a client as needed.
2421 int /* O - 1 if success, 0 if fail */
2422 cupsdWriteClient(cupsd_client_t
*con
) /* I - Client connection */
2424 int bytes
; /* Number of bytes written */
2425 char buf
[16385]; /* Data buffer */
2426 char *bufptr
; /* Pointer into buffer */
2427 ipp_state_t ipp_state
; /* IPP state value */
2431 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2432 "cupsdWriteClient(con=%p) %d response=%p, file=%d pipe_pid=%d",
2433 con
, con
->http
.fd
, con
->response
, con
->file
, con
->pipe_pid
);
2436 if (con
->http
.state
!= HTTP_GET_SEND
&&
2437 con
->http
.state
!= HTTP_POST_SEND
)
2440 if (con
->response
!= NULL
)
2442 ipp_state
= ippWrite(&(con
->http
), con
->response
);
2443 bytes
= ipp_state
!= IPP_ERROR
&& ipp_state
!= IPP_DATA
;
2445 else if ((bytes
= read(con
->file
, buf
, sizeof(buf
) - 1)) > 0)
2447 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2448 "cupsdWriteClient: Read %d bytes from file %d...",
2451 if (con
->pipe_pid
&& !con
->got_fields
)
2454 * Inspect the data for Content-Type and other fields.
2459 for (bufptr
= buf
; !con
->got_fields
&& *bufptr
; bufptr
++)
2460 if (*bufptr
== '\n')
2463 * Send line to client...
2466 if (bufptr
> buf
&& bufptr
[-1] == '\r')
2470 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "Script header: %s", buf
);
2472 if (!con
->sent_header
)
2475 * Handle redirection and CGI status codes...
2478 if (!strncasecmp(buf
, "Location:", 9))
2479 cupsdSendHeader(con
, HTTP_SEE_OTHER
, NULL
);
2480 else if (!strncasecmp(buf
, "Status:", 7))
2481 cupsdSendHeader(con
, atoi(buf
+ 7), NULL
);
2483 cupsdSendHeader(con
, HTTP_OK
, NULL
);
2485 if (con
->http
.version
== HTTP_1_1
)
2487 con
->http
.data_encoding
= HTTP_ENCODE_CHUNKED
;
2489 if (httpPrintf(HTTP(con
), "Transfer-Encoding: chunked\r\n") < 0)
2493 con
->sent_header
= 1;
2496 if (strncasecmp(buf
, "Status:", 7))
2497 httpPrintf(HTTP(con
), "%s\r\n", buf
);
2499 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdWriteClient: %d %s",
2506 bytes
-= (bufptr
- buf
);
2507 memmove(buf
, bufptr
, bytes
+ 1);
2511 * See if the line was empty...
2514 if (con
->field_col
== 0)
2515 con
->got_fields
= 1;
2519 else if (*bufptr
!= '\r')
2522 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2523 "cupsdWriteClient: %d bytes=%d, got_fields=%d",
2524 con
->http
.fd
, bytes
, con
->got_fields
);
2526 if (bytes
> 0 && !con
->got_fields
)
2529 * Remaining text needs to go out...
2532 httpPrintf(HTTP(con
), "%s", buf
);
2534 con
->http
.activity
= time(NULL
);
2537 else if (bytes
== 0)
2539 con
->http
.activity
= time(NULL
);
2544 if (httpWrite(HTTP(con
), buf
, bytes
) < 0)
2546 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2547 "cupsdWriteClient: %d Write of %d bytes failed!",
2548 con
->http
.fd
, bytes
);
2550 cupsdCloseClient(con
);
2554 con
->bytes
+= bytes
;
2559 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdWriteClient: %d bytes < 0",
2562 cupsdLogRequest(con
, HTTP_OK
);
2564 httpFlushWrite(HTTP(con
));
2566 if (con
->http
.data_encoding
== HTTP_ENCODE_CHUNKED
)
2568 if (httpPrintf(HTTP(con
), "0\r\n\r\n") < 0)
2570 cupsdCloseClient(con
);
2575 con
->http
.state
= HTTP_WAITING
;
2577 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2578 "cupsdWriteClient: Removing fd %d from OutputSet...",
2581 FD_CLR(con
->http
.fd
, OutputSet
);
2585 if (FD_ISSET(con
->file
, InputSet
))
2587 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2588 "cupsdWriteClient: Removing fd %d from InputSet...",
2590 FD_CLR(con
->file
, InputSet
);
2594 cupsdEndProcess(con
->pipe_pid
, 0);
2596 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2597 "cupsdWriteClient: %d Closing data file %d.",
2598 con
->http
.fd
, con
->file
);
2607 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2608 "cupsdWriteClient: %d Removing temp file %s",
2609 con
->http
.fd
, con
->filename
);
2610 unlink(con
->filename
);
2611 cupsdClearString(&con
->filename
);
2614 if (con
->request
!= NULL
)
2616 ippDelete(con
->request
);
2617 con
->request
= NULL
;
2620 if (con
->response
!= NULL
)
2622 ippDelete(con
->response
);
2623 con
->response
= NULL
;
2626 cupsdClearString(&con
->command
);
2627 cupsdClearString(&con
->options
);
2629 if (!con
->http
.keep_alive
)
2631 cupsdCloseClient(con
);
2637 con
->file_ready
= 0;
2639 if (con
->pipe_pid
&& !FD_ISSET(con
->file
, InputSet
))
2641 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2642 "cupsdWriteClient: Adding fd %d to InputSet...",
2644 FD_SET(con
->file
, InputSet
);
2648 con
->http
.activity
= time(NULL
);
2655 * 'check_if_modified()' - Decode an "If-Modified-Since" line.
2658 static int /* O - 1 if modified since */
2660 cupsd_client_t
*con
, /* I - Client connection */
2661 struct stat
*filestats
) /* I - File information */
2663 char *ptr
; /* Pointer into field */
2664 time_t date
; /* Time/date value */
2665 off_t size
; /* Size/length value */
2670 ptr
= con
->http
.fields
[HTTP_FIELD_IF_MODIFIED_SINCE
];
2675 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2676 "check_if_modified: %d If-Modified-Since=\"%s\"",
2679 while (*ptr
!= '\0')
2681 while (isspace(*ptr
) || *ptr
== ';')
2684 if (strncasecmp(ptr
, "length=", 7) == 0)
2687 size
= strtoll(ptr
, NULL
, 10);
2689 while (isdigit(*ptr
))
2692 else if (isalpha(*ptr
))
2694 date
= httpGetDateTime(ptr
);
2695 while (*ptr
!= '\0' && *ptr
!= ';')
2700 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2701 "check_if_modified: %d sizes=" CUPS_LLFMT
","
2702 CUPS_LLFMT
" dates=%d,%d",
2703 con
->http
.fd
, CUPS_LLCAST size
,
2704 CUPS_LLCAST filestats
->st_size
, (int)date
,
2705 (int)filestats
->st_mtime
);
2707 return ((size
!= filestats
->st_size
&& size
!= 0) ||
2708 (date
< filestats
->st_mtime
&& date
!= 0) ||
2709 (size
== 0 && date
== 0));
2714 * 'decode_auth()' - Decode an authorization string.
2718 decode_auth(cupsd_client_t
*con
) /* I - Client to decode to */
2720 char *s
, /* Authorization string */
2721 value
[1024]; /* Value string */
2722 const char *username
; /* Certificate username */
2726 * Decode the string...
2729 s
= con
->http
.fields
[HTTP_FIELD_AUTHORIZATION
];
2731 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2732 "decode_auth(%p): Authorization string = \"%s\"", con
, s
);
2734 if (!strncmp(s
, "Basic", 5))
2740 httpDecode64(value
, s
);
2743 * Pull the username and password out...
2746 if ((s
= strchr(value
, ':')) == NULL
)
2748 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2749 "decode_auth: %d no colon in auth string \"%s\"",
2750 con
->http
.fd
, value
);
2756 strlcpy(con
->username
, value
, sizeof(con
->username
));
2757 strlcpy(con
->password
, s
, sizeof(con
->password
));
2759 else if (!strncmp(s
, "Local", 5))
2765 if ((username
= cupsdFindCert(s
)) != NULL
)
2766 strlcpy(con
->username
, username
, sizeof(con
->username
));
2768 else if (!strncmp(s
, "Digest", 6))
2771 * Get the username and password from the Digest attributes...
2774 if (httpGetSubField(&(con
->http
), HTTP_FIELD_AUTHORIZATION
, "username",
2776 strlcpy(con
->username
, value
, sizeof(con
->username
));
2778 if (httpGetSubField(&(con
->http
), HTTP_FIELD_AUTHORIZATION
, "response",
2780 strlcpy(con
->password
, value
, sizeof(con
->password
));
2783 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "decode_auth: %d username=\"%s\"",
2784 con
->http
.fd
, con
->username
);
2789 * 'get_file()' - Get a filename and state info.
2792 static char * /* O - Real filename */
2793 get_file(cupsd_client_t
*con
, /* I - Client connection */
2794 struct stat
*filestats
, /* O - File information */
2795 char *filename
, /* IO - Filename buffer */
2796 int len
) /* I - Buffer length */
2798 int status
; /* Status of filesystem calls */
2799 char *ptr
; /* Pointer info filename */
2800 int plen
; /* Remaining length after pointer */
2804 * Need to add DocumentRoot global...
2807 if (!strncmp(con
->uri
, "/ppd/", 5))
2808 snprintf(filename
, len
, "%s%s", ServerRoot
, con
->uri
);
2809 else if (!strncmp(con
->uri
, "/admin/conf/", 12))
2810 snprintf(filename
, len
, "%s%s", ServerRoot
, con
->uri
+ 11);
2811 else if (!strncmp(con
->uri
, "/admin/log/", 11))
2813 if (!strcmp(con
->uri
+ 11, "access_log") && AccessLog
[0] == '/')
2814 strlcpy(filename
, AccessLog
, len
);
2815 else if (!strcmp(con
->uri
+ 11, "error_log") && ErrorLog
[0] == '/')
2816 strlcpy(filename
, ErrorLog
, len
);
2817 else if (!strcmp(con
->uri
+ 11, "page_log") && PageLog
[0] == '/')
2818 strlcpy(filename
, PageLog
, len
);
2822 else if (con
->language
!= NULL
)
2823 snprintf(filename
, len
, "%s/%s%s", DocumentRoot
, con
->language
->language
,
2826 snprintf(filename
, len
, "%s%s", DocumentRoot
, con
->uri
);
2828 if ((ptr
= strchr(filename
, '?')) != NULL
)
2832 * Grab the status for this language; if there isn't a language-specific file
2833 * then fallback to the default one...
2836 if ((status
= stat(filename
, filestats
)) != 0 && con
->language
!= NULL
)
2839 * Drop the language prefix and try the current directory...
2842 if (strncmp(con
->uri
, "/ppd/", 5) &&
2843 strncmp(con
->uri
, "/admin/conf/", 12) &&
2844 strncmp(con
->uri
, "/admin/log/", 11))
2846 snprintf(filename
, len
, "%s%s", DocumentRoot
, con
->uri
);
2848 if ((ptr
= strchr(filename
, '?')) != NULL
)
2851 status
= stat(filename
, filestats
);
2856 * If we're found a directory, get the index.html file instead...
2859 if (!status
&& S_ISDIR(filestats
->st_mode
))
2861 if (filename
[strlen(filename
) - 1] != '/')
2862 strlcat(filename
, "/", len
);
2864 ptr
= filename
+ strlen(filename
);
2865 plen
= len
- (ptr
- filename
);
2867 strlcpy(ptr
, "index.html", plen
);
2868 status
= stat(filename
, filestats
);
2873 strlcpy(ptr
, "index.class", plen
);
2874 status
= stat(filename
, filestats
);
2876 #endif /* HAVE_JAVA */
2881 strlcpy(ptr
, "index.pl", plen
);
2882 status
= stat(filename
, filestats
);
2884 #endif /* HAVE_PERL */
2889 strlcpy(ptr
, "index.php", plen
);
2890 status
= stat(filename
, filestats
);
2892 #endif /* HAVE_PHP */
2897 strlcpy(ptr
, "index.pyc", plen
);
2898 status
= stat(filename
, filestats
);
2903 strlcpy(ptr
, "index.py", plen
);
2904 status
= stat(filename
, filestats
);
2906 #endif /* HAVE_PYTHON */
2909 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_file: %d filename=%s size=%d",
2910 con
->http
.fd
, filename
,
2911 status
? -1 : (int)filestats
->st_size
);
2914 con
->http
.data_remaining
= (int)filestats
->st_size
;
2924 * 'install_conf_file()' - Install a configuration file.
2927 static http_status_t
/* O - Status */
2928 install_conf_file(cupsd_client_t
*con
) /* I - Connection */
2930 cups_file_t
*in
, /* Input file */
2931 *out
; /* Output file */
2932 char buffer
[1024]; /* Copy buffer */
2933 int bytes
; /* Number of bytes */
2934 char conffile
[1024], /* Configuration filename */
2935 newfile
[1024], /* New config filename */
2936 oldfile
[1024]; /* Old config filename */
2937 struct stat confinfo
; /* Config file info */
2941 * First construct the filenames...
2944 snprintf(conffile
, sizeof(conffile
), "%s%s", ServerRoot
, con
->uri
+ 11);
2945 snprintf(newfile
, sizeof(newfile
), "%s%s.N", ServerRoot
, con
->uri
+ 11);
2946 snprintf(oldfile
, sizeof(oldfile
), "%s%s.O", ServerRoot
, con
->uri
+ 11);
2948 cupsdLogMessage(CUPSD_LOG_INFO
, "Installing config file \"%s\"...", conffile
);
2951 * Get the owner, group, and permissions of the configuration file.
2952 * If it doesn't exist, assign it to the User and Group in the
2953 * cupsd.conf file with mode 0640 permissions.
2956 if (stat(conffile
, &confinfo
))
2958 confinfo
.st_uid
= User
;
2959 confinfo
.st_gid
= Group
;
2960 confinfo
.st_mode
= ConfigFilePerm
;
2964 * Open the request file and new config file...
2967 if ((in
= cupsFileOpen(con
->filename
, "rb")) == NULL
)
2969 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to open request file \"%s\" - %s",
2970 con
->filename
, strerror(errno
));
2971 return (HTTP_SERVER_ERROR
);
2974 if ((out
= cupsFileOpen(newfile
, "wb")) == NULL
)
2977 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to open config file \"%s\" - %s",
2978 newfile
, strerror(errno
));
2979 return (HTTP_SERVER_ERROR
);
2982 fchmod(cupsFileNumber(out
), confinfo
.st_mode
);
2983 fchown(cupsFileNumber(out
), confinfo
.st_uid
, confinfo
.st_gid
);
2986 * Copy from the request to the new config file...
2989 while ((bytes
= cupsFileRead(in
, buffer
, sizeof(buffer
))) > 0)
2990 if (cupsFileWrite(out
, buffer
, bytes
) < bytes
)
2992 cupsdLogMessage(CUPSD_LOG_ERROR
,
2993 "Unable to copy to config file \"%s\" - %s",
2994 newfile
, strerror(errno
));
3000 return (HTTP_SERVER_ERROR
);
3004 * Close the files...
3008 if (cupsFileClose(out
))
3010 cupsdLogMessage(CUPSD_LOG_ERROR
,
3011 "Error file closing config file \"%s\" - %s",
3012 newfile
, strerror(errno
));
3016 return (HTTP_SERVER_ERROR
);
3020 * Remove the request file...
3023 unlink(con
->filename
);
3024 cupsdClearString(&con
->filename
);
3027 * Unlink the old backup, rename the current config file to the backup
3028 * filename, and rename the new config file to the config file name...
3031 if (unlink(oldfile
))
3032 if (errno
!= ENOENT
)
3034 cupsdLogMessage(CUPSD_LOG_ERROR
,
3035 "Unable to remove backup config file \"%s\" - %s",
3036 oldfile
, strerror(errno
));
3040 return (HTTP_SERVER_ERROR
);
3043 if (rename(conffile
, oldfile
))
3044 if (errno
!= ENOENT
)
3046 cupsdLogMessage(CUPSD_LOG_ERROR
,
3047 "Unable to rename old config file \"%s\" - %s",
3048 conffile
, strerror(errno
));
3052 return (HTTP_SERVER_ERROR
);
3055 if (rename(newfile
, conffile
))
3057 cupsdLogMessage(CUPSD_LOG_ERROR
,
3058 "Unable to rename new config file \"%s\" - %s",
3059 newfile
, strerror(errno
));
3061 rename(oldfile
, conffile
);
3064 return (HTTP_SERVER_ERROR
);
3068 * If the cupsd.conf file was updated, set the NeedReload flag...
3071 if (!strcmp(con
->uri
, "/admin/conf/cupsd.conf"))
3072 NeedReload
= RELOAD_CUPSD
;
3074 NeedReload
= RELOAD_ALL
;
3076 ReloadTime
= time(NULL
);
3079 * Return that the file was created successfully...
3082 return (HTTP_CREATED
);
3087 * 'is_path_absolute()' - Is a path absolute and free of relative elements (i.e. "..").
3090 static int /* O - 0 if relative, 1 if absolute */
3091 is_path_absolute(const char *path
) /* I - Input path */
3094 * Check for a leading slash...
3101 * Check for "/.." in the path...
3104 while ((path
= strstr(path
, "/..")) != NULL
)
3106 if (!path
[3] || path
[3] == '/')
3113 * If we haven't found any relative paths, return 1 indicating an
3122 * 'pipe_command()' - Pipe the output of a command to the remote client.
3125 static int /* O - Process ID */
3126 pipe_command(cupsd_client_t
*con
, /* I - Client connection */
3127 int infile
, /* I - Standard input for command */
3128 int *outfile
, /* O - Standard output for command */
3129 char *command
, /* I - Command to run */
3130 char *options
, /* I - Options for command */
3131 int root
) /* I - Run as root? */
3133 int i
; /* Looping var */
3134 int pid
; /* Process ID */
3135 char *commptr
; /* Command string pointer */
3136 char *uriptr
; /* URI string pointer */
3137 int fds
[2]; /* Pipe FDs */
3138 int argc
; /* Number of arguments */
3139 int envc
; /* Number of environment variables */
3140 char argbuf
[10240], /* Argument buffer */
3141 *argv
[100], /* Argument strings */
3142 *envp
[100]; /* Environment variables */
3143 char content_length
[1024], /* CONTENT_LENGTH environment variable */
3144 content_type
[1024], /* CONTENT_TYPE environment variable */
3145 http_cookie
[32768], /* HTTP_COOKIE environment variable */
3146 http_user_agent
[1024], /* HTTP_USER_AGENT environment variable */
3147 lang
[1024], /* LANG environment variable */
3148 *query_string
, /* QUERY_STRING env variable */
3149 remote_addr
[1024], /* REMOTE_ADDR environment variable */
3150 remote_host
[1024], /* REMOTE_HOST environment variable */
3151 remote_user
[1024], /* REMOTE_USER environment variable */
3152 script_name
[1024], /* SCRIPT_NAME environment variable */
3153 server_name
[1024], /* SERVER_NAME environment variable */
3154 server_port
[1024]; /* SERVER_PORT environment variable */
3158 * Parse a copy of the options string, which is of the form:
3160 * name argument+argument+argument
3161 * name?argument+argument+argument
3162 * name param=value¶m=value
3163 * name?param=value¶m=value
3165 * If the string contains an "=" character after the initial name,
3166 * then we treat it as a HTTP GET form request and make a copy of
3167 * the remaining string for the environment variable.
3169 * The string is always parsed out as command-line arguments, to
3170 * be consistent with Apache...
3173 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3174 "pipe_command: command=\"%s\", options=\"%s\"",
3177 strlcpy(argbuf
, options
, sizeof(argbuf
));
3180 query_string
= NULL
;
3182 for (commptr
= argbuf
, argc
= 1; *commptr
!= '\0' && argc
< 99; commptr
++)
3185 * Break arguments whenever we see a + or space...
3188 if (*commptr
== ' ' || *commptr
== '+' || (*commptr
== '?' && argc
== 1))
3191 * Terminate the current string and skip trailing whitespace...
3196 while (*commptr
== ' ')
3200 * If we don't have a blank string, save it as another argument...
3205 argv
[argc
] = commptr
;
3212 * If we see an "=" in the remaining string, make a copy of it since
3213 * it will be query data...
3216 if (argc
== 2 && strchr(commptr
, '=') && con
->operation
== HTTP_GET
)
3217 cupsdSetStringf(&query_string
, "QUERY_STRING=%s", commptr
);
3220 * Don't skip the first non-blank character...
3225 else if (*commptr
== '%' && isxdigit(commptr
[1] & 255) &&
3226 isxdigit(commptr
[2] & 255))
3229 * Convert the %xx notation to the individual character.
3232 if (commptr
[1] >= '0' && commptr
[1] <= '9')
3233 *commptr
= (commptr
[1] - '0') << 4;
3235 *commptr
= (tolower(commptr
[1]) - 'a' + 10) << 4;
3237 if (commptr
[2] >= '0' && commptr
[2] <= '9')
3238 *commptr
|= commptr
[2] - '0';
3240 *commptr
|= tolower(commptr
[2]) - 'a' + 10;
3242 _cups_strcpy(commptr
+ 1, commptr
+ 3);
3245 * Check for a %00 and break if that is the case...
3255 if (argv
[0][0] == '\0')
3256 argv
[0] = strrchr(command
, '/') + 1;
3259 * Setup the environment variables as needed...
3263 snprintf(lang
, sizeof(lang
), "LANG=%s.UTF-8", con
->language
->language
);
3265 strcpy(lang
, "LANG=C");
3267 strcpy(remote_addr
, "REMOTE_ADDR=");
3268 httpAddrString(&(con
->http
.hostaddr
), remote_addr
+ 12,
3269 sizeof(remote_addr
) - 12);
3271 snprintf(remote_host
, sizeof(remote_host
), "REMOTE_HOST=%s",
3272 con
->http
.hostname
);
3274 snprintf(script_name
, sizeof(script_name
), "SCRIPT_NAME=%s", con
->uri
);
3275 if ((uriptr
= strchr(script_name
, '?')) != NULL
)
3278 sprintf(server_port
, "SERVER_PORT=%d", con
->serverport
);
3280 snprintf(server_name
, sizeof(server_name
), "SERVER_NAME=%s",
3283 envc
= cupsdLoadEnv(envp
, (int)(sizeof(envp
) / sizeof(envp
[0])));
3285 envp
[envc
++] = lang
;
3286 envp
[envc
++] = "REDIRECT_STATUS=1";
3287 envp
[envc
++] = server_name
;
3288 envp
[envc
++] = server_port
;
3289 envp
[envc
++] = remote_addr
;
3290 envp
[envc
++] = remote_host
;
3291 envp
[envc
++] = script_name
;
3293 if (con
->username
[0])
3295 snprintf(remote_user
, sizeof(remote_user
), "REMOTE_USER=%s", con
->username
);
3297 envp
[envc
++] = remote_user
;
3300 if (con
->http
.version
== HTTP_1_1
)
3301 envp
[envc
++] = "SERVER_PROTOCOL=HTTP/1.1";
3302 else if (con
->http
.version
== HTTP_1_0
)
3303 envp
[envc
++] = "SERVER_PROTOCOL=HTTP/1.0";
3305 envp
[envc
++] = "SERVER_PROTOCOL=HTTP/0.9";
3307 if (con
->http
.cookie
)
3309 snprintf(http_cookie
, sizeof(http_cookie
), "HTTP_COOKIE=%s",
3311 envp
[envc
++] = http_cookie
;
3314 if (con
->http
.fields
[HTTP_FIELD_USER_AGENT
][0])
3316 snprintf(http_user_agent
, sizeof(http_user_agent
), "HTTP_USER_AGENT=%s",
3317 con
->http
.fields
[HTTP_FIELD_USER_AGENT
]);
3318 envp
[envc
++] = http_user_agent
;
3321 if (con
->operation
== HTTP_GET
)
3323 for (i
= 0; i
< argc
; i
++)
3324 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "argv[%d] = \"%s\"", i
, argv
[i
]);
3326 envp
[envc
++] = "REQUEST_METHOD=GET";
3331 * Add GET form variables after ?...
3334 envp
[envc
++] = query_string
;
3339 sprintf(content_length
, "CONTENT_LENGTH=" CUPS_LLFMT
, con
->bytes
);
3340 snprintf(content_type
, sizeof(content_type
), "CONTENT_TYPE=%s",
3341 con
->http
.fields
[HTTP_FIELD_CONTENT_TYPE
]);
3343 envp
[envc
++] = "REQUEST_METHOD=POST";
3344 envp
[envc
++] = content_length
;
3345 envp
[envc
++] = content_type
;
3349 * Tell the CGI if we are using encryption...
3352 if (con
->http
.encryption
== HTTP_ENCRYPT_ALWAYS
)
3353 envp
[envc
++] = "HTTPS=ON";
3356 * Terminate the environment array...
3361 if (LogLevel
== CUPSD_LOG_DEBUG2
)
3363 for (i
= 0; i
< argc
; i
++)
3364 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3365 "pipe_command: argv[%d] = \"%s\"", i
, argv
[i
]);
3366 for (i
= 0; i
< envc
; i
++)
3367 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3368 "pipe_command: envp[%d] = \"%s\"", i
, envp
[i
]);
3372 * Create a pipe for the output...
3375 if (cupsdOpenPipe(fds
))
3377 cupsdClearString(&query_string
);
3379 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to create pipes for CGI %s - %s",
3380 argv
[0], strerror(errno
));
3385 * Then execute the command...
3388 if (cupsdStartProcess(command
, argv
, envp
, infile
, fds
[1], CGIPipes
[1],
3389 -1, root
, &pid
) < 0)
3392 * Error - can't fork!
3395 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to fork for CGI %s - %s", argv
[0],
3398 cupsdClosePipe(fds
);
3404 * Fork successful - return the PID...
3407 if (con
->username
[0])
3408 cupsdAddCert(pid
, con
->username
);
3410 cupsdLogMessage(CUPSD_LOG_DEBUG
, "CGI %s started - PID = %d", command
, pid
);
3416 cupsdClearString(&query_string
);
3422 #if defined(HAVE_CDSASSL)
3424 * 'CDSAReadFunc()' - Read function for CDSA decryption code.
3427 static OSStatus
/* O - -1 on error, 0 on success */
3429 SSLConnectionRef connection
, /* I - SSL/TLS connection */
3430 void *data
, /* I - Data buffer */
3431 size_t *dataLength
) /* IO - Number of bytes */
3433 ssize_t bytes
; /* Number of bytes read */
3436 bytes
= recv((int)connection
, data
, *dataLength
, 0);
3439 *dataLength
= bytes
;
3448 * 'CDSAWriteFunc()' - Write function for CDSA encryption code.
3451 static OSStatus
/* O - -1 on error, 0 on success */
3453 SSLConnectionRef connection
, /* I - SSL/TLS connection */
3454 const void *data
, /* I - Data buffer */
3455 size_t *dataLength
) /* IO - Number of bytes */
3460 bytes
= write((int)connection
, data
, *dataLength
);
3463 *dataLength
= bytes
;
3469 #endif /* HAVE_CDSASSL */