2 * "$Id: client.c,v 1.91.2.51 2003/03/13 06:53:46 mike Exp $"
4 * Client routines for the Common UNIX Printing System (CUPS) scheduler.
6 * Copyright 1997-2003 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-3111 USA
20 * Voice: (301) 373-9603
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
26 * AcceptClient() - Accept a new client.
27 * CloseAllClients() - Close all remote clients immediately.
28 * CloseClient() - Close a remote client.
29 * EncryptClient() - Enable encryption for the client...
30 * ReadClient() - Read data from a client.
31 * SendCommand() - Send output from a command via HTTP.
32 * SendError() - Send an error message via HTTP.
33 * SendFile() - Send a file via HTTP.
34 * SendHeader() - Send an HTTP request.
35 * ShutdownClient() - Shutdown the receiving end of a connection.
36 * WriteClient() - Write data to a client as needed.
37 * check_if_modified() - Decode an "If-Modified-Since" line.
38 * decode_auth() - Decode an authorization string.
39 * get_file() - Get a filename and state info.
40 * install_conf_file() - Install a configuration file.
41 * pipe_command() - Pipe the output of a command to the remote client.
42 * CDSAReadFunc() - Read function for CDSA decryption code.
43 * CDSAWriteFunc() - Write function for CDSA encryption code.
47 * Include necessary headers...
52 #include <cups/http-private.h>
59 static int check_if_modified(client_t
*con
,
60 struct stat
*filestats
);
61 static void decode_auth(client_t
*con
);
62 static char *get_file(client_t
*con
, struct stat
*filestats
,
63 char *filename
, int len
);
64 static http_status_t
install_conf_file(client_t
*con
);
65 static int pipe_command(client_t
*con
, int infile
, int *outfile
,
66 char *command
, char *options
);
69 static OSStatus
CDSAReadFunc(SSLConnectionRef connection
, void *data
,
71 static OSStatus
CDSAWriteFunc(SSLConnectionRef connection
,
72 const void *data
, size_t *dataLength
);
73 #endif /* HAVE_CDSASSL */
77 * 'AcceptClient()' - Accept a new client.
81 AcceptClient(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 client_t
*con
; /* New client pointer */
87 const struct hostent
*host
; /* Host entry for address */
88 char *hostname
;/* Hostname for address */
89 http_addr_t temp
; /* Temporary address variable */
90 static time_t last_dos
= 0;
91 /* Time of last DoS attack */
94 LogMessage(L_DEBUG2
, "AcceptClient(%p) %d NumClients = %d",
95 lis
, lis
->fd
, NumClients
);
98 * Make sure we don't have a full set of clients already...
101 if (NumClients
== MaxClients
)
105 * Get a pointer to the next available client...
108 con
= Clients
+ NumClients
;
110 memset(con
, 0, sizeof(client_t
));
111 con
->http
.activity
= time(NULL
);
114 * Accept the client and get the remote address...
117 val
= sizeof(struct sockaddr_in
);
119 if ((con
->http
.fd
= accept(lis
->fd
, (struct sockaddr
*)&(con
->http
.hostaddr
),
122 LogMessage(L_ERROR
, "Unable to accept client connection - %s.",
128 if (lis
->address
.addr
.sa_family
== AF_INET6
)
129 con
->http
.hostaddr
.ipv6
.sin6_port
= lis
->address
.ipv6
.sin6_port
;
131 #endif /* AF_INET6 */
132 con
->http
.hostaddr
.ipv4
.sin_port
= lis
->address
.ipv4
.sin_port
;
135 * Check the number of clients on the same address...
138 for (i
= 0, count
= 0; i
< NumClients
; i
++)
139 if (memcmp(&(Clients
[i
].http
.hostaddr
), &(con
->http
.hostaddr
),
140 sizeof(con
->http
.hostaddr
)) == 0)
143 if (count
>= MaxClientsPerHost
)
147 if (count
>= MaxClientsPerHost
)
149 if ((time(NULL
) - last_dos
) >= 60)
151 last_dos
= time(NULL
);
152 LogMessage(L_WARN
, "Possible DoS attack - more than %d clients connecting from %s!",
153 MaxClientsPerHost
, Clients
[i
].http
.hostname
);
157 closesocket(con
->http
.fd
);
166 * Get the hostname or format the IP address as needed...
170 hostname
= httpAddrLookup(&(con
->http
.hostaddr
), con
->http
.hostname
,
171 sizeof(con
->http
.hostname
));
175 httpAddrString(&(con
->http
.hostaddr
), con
->http
.hostname
,
176 sizeof(con
->http
.hostname
));
179 if (httpAddrLocalhost(&(con
->http
.hostaddr
)))
182 * Map accesses from the loopback interface to "localhost"...
185 strlcpy(con
->http
.hostname
, "localhost", sizeof(con
->http
.hostname
));
187 else if (httpAddrEqual(&(con
->http
.hostaddr
), &ServerAddr
))
190 * Map accesses from the same host to the server name.
193 strlcpy(con
->http
.hostname
, ServerName
, sizeof(con
->http
.hostname
));
196 if (hostname
== NULL
&& HostNameLookups
== 2)
199 * Can't have an unresolved IP address with double-lookups enabled...
202 LogMessage(L_DEBUG2
, "AcceptClient: Closing connection %d...",
206 closesocket(con
->http
.fd
);
211 LogMessage(L_WARN
, "Name lookup failed - connection from %s closed!",
216 if (HostNameLookups
== 2)
219 * Do double lookups as needed...
222 if ((host
= httpGetHostByName(con
->http
.hostname
)) != NULL
)
225 * See if the hostname maps to the same IP address...
228 if (host
->h_addrtype
!= con
->http
.hostaddr
.addr
.sa_family
)
231 * Not the right type of address...
239 * Compare all of the addresses against this one...
242 for (i
= 0; host
->h_addr_list
[i
]; i
++)
244 httpAddrLoad(host
, 0, i
, &temp
);
246 if (httpAddrEqual(&(con
->http
.hostaddr
), &temp
))
250 if (!host
->h_addr_list
[i
])
258 * Can't have a hostname that doesn't resolve to the same IP address
259 * with double-lookups enabled...
262 LogMessage(L_DEBUG2
, "AcceptClient: Closing connection %d...",
266 closesocket(con
->http
.fd
);
271 LogMessage(L_WARN
, "IP lookup failed - connection from %s closed!",
278 if (con
->http
.hostaddr
.addr
.sa_family
== AF_INET6
)
279 LogMessage(L_DEBUG
, "AcceptClient: %d from %s:%d.", con
->http
.fd
,
280 con
->http
.hostname
, ntohs(con
->http
.hostaddr
.ipv6
.sin6_port
));
282 #endif /* AF_INET6 */
283 LogMessage(L_DEBUG
, "AcceptClient: %d from %s:%d.", con
->http
.fd
,
284 con
->http
.hostname
, ntohs(con
->http
.hostaddr
.ipv4
.sin_port
));
287 * Using TCP_NODELAY improves responsiveness, especially on systems
288 * with a slow loopback interface... Since we write large buffers
289 * when sending print files and requests, there shouldn't be any
290 * performance penalty for this...
294 setsockopt(con
->http
.fd
, IPPROTO_TCP
, TCP_NODELAY
, &val
, sizeof(val
));
297 * Close this file on all execs...
300 fcntl(con
->http
.fd
, F_SETFD
, fcntl(con
->http
.fd
, F_GETFD
) | FD_CLOEXEC
);
303 * Add the socket to the select() input mask.
306 LogMessage(L_DEBUG2
, "AcceptClient: Adding fd %d to InputSet...",
308 FD_SET(con
->http
.fd
, InputSet
);
313 * Temporarily suspend accept()'s until we lose a client...
316 if (NumClients
== MaxClients
)
321 * See if we are connecting on a secure port...
324 if (lis
->encryption
== HTTP_ENCRYPT_ALWAYS
)
327 * https connection; go secure...
330 con
->http
.encryption
= HTTP_ENCRYPT_ALWAYS
;
334 #endif /* HAVE_SSL */
339 * 'CloseAllClients()' - Close all remote clients immediately.
343 CloseAllClients(void)
345 while (NumClients
> 0)
346 CloseClient(Clients
);
351 * 'CloseClient()' - Close a remote client.
355 CloseClient(client_t
*con
) /* I - Client to close */
357 int status
; /* Exit status of pipe command */
358 #if defined(HAVE_LIBSSL)
359 SSL_CTX
*context
; /* Context for encryption */
360 SSL
*conn
; /* Connection for encryption */
361 unsigned long error
; /* Error code */
362 #elif defined(HAVE_GNUTLS)
363 http_tls_t
*conn
; /* TLS connection information */
364 int error
; /* Error code */
365 gnutls_certificate_server_credentials
*credentials
;
366 /* TLS credentials */
367 #endif /* HAVE_GNUTLS */
370 LogMessage(L_DEBUG
, "CloseClient() %d", con
->http
.fd
);
372 if (con
->http
.input_set
)
373 free(con
->http
.input_set
);
375 httpClearCookie(HTTP(con
));
377 ClearString(&con
->filename
);
378 ClearString(&con
->command
);
379 ClearString(&con
->options
);
383 * Shutdown encryption as needed...
389 conn
= (SSL
*)(con
->http
.tls
);
390 context
= SSL_get_SSL_CTX(conn
);
392 switch (SSL_shutdown(conn
))
395 LogMessage(L_INFO
, "CloseClient: SSL shutdown successful!");
398 LogMessage(L_ERROR
, "CloseClient: Fatal error during SSL shutdown!");
400 while ((error
= ERR_get_error()) != 0)
401 LogMessage(L_ERROR
, "CloseClient: %s", ERR_error_string(error
, NULL
));
405 SSL_CTX_free(context
);
408 # elif defined(HAVE_GNUTLS)
409 conn
= (http_tls_t
*)(con
->http
.tls
);
410 credentials
= (gnutls_certificate_server_credentials
*)(conn
->credentials
);
412 error
= gnutls_bye(conn
->session
, GNUTLS_SHUT_WR
);
415 case GNUTLS_E_SUCCESS
:
416 LogMessage(L_INFO
, "CloseClient: SSL shutdown successful!");
419 LogMessage(L_ERROR
, "CloseClient: %s", gnutls_strerror(error
));
423 gnutls_deinit(conn
->session
);
424 gnutls_certificate_free_credentials(*credentials
);
428 # elif defined(HAVE_CDSASSL)
429 status
= SSLClose((SSLContextRef
)con
->http
.tls
);
430 SSLDisposeContext((SSLContextRef
)con
->http
.tls
);
431 # endif /* HAVE_LIBSSL */
433 con
->http
.tls
= NULL
;
435 #endif /* HAVE_SSL */
438 * Close the socket and clear the file from the input set for select()...
441 if (con
->http
.fd
>= 0)
443 LogMessage(L_DEBUG2
, "CloseClient: Removing fd %d from InputSet and OutputSet...",
446 FD_CLR(con
->http
.fd
, InputSet
);
447 FD_CLR(con
->http
.fd
, OutputSet
);
451 if (con
->pipe_pid
!= 0)
453 LogMessage(L_DEBUG2
, "CloseClient: Removing fd %d from InputSet...",
455 FD_CLR(con
->file
, InputSet
);
461 * Close the open data file...
466 kill(con
->pipe_pid
, SIGKILL
);
467 waitpid(con
->pipe_pid
, &status
, WNOHANG
);
470 LogMessage(L_DEBUG2
, "CloseClient() %d Closing data file %d.",
471 con
->http
.fd
, con
->file
);
472 LogMessage(L_DEBUG2
, "CloseClient() %d Removing fd %d from InputSet.",
473 con
->http
.fd
, con
->file
);
475 FD_CLR(con
->file
, InputSet
);
482 ippDelete(con
->request
);
488 ippDelete(con
->response
);
489 con
->response
= NULL
;
494 cupsLangFree(con
->language
);
495 con
->language
= NULL
;
499 * Re-enable new client connections if we are going back under the
503 if (NumClients
== MaxClients
)
507 * Compact the list of clients as necessary...
512 if (con
< (Clients
+ NumClients
))
513 memcpy(con
, con
+ 1, (Clients
+ NumClients
- con
) * sizeof(client_t
));
518 * 'EncryptClient()' - Enable encryption for the client...
521 int /* O - 1 on success, 0 on error */
522 EncryptClient(client_t
*con
) /* I - Client to encrypt */
524 #if defined HAVE_LIBSSL
525 SSL_CTX
*context
; /* Context for encryption */
526 SSL
*conn
; /* Connection for encryption */
527 unsigned long error
; /* Error code */
531 * Create the SSL context and accept the connection...
534 context
= SSL_CTX_new(SSLv23_server_method());
536 SSL_CTX_use_PrivateKey_file(context
, ServerKey
, SSL_FILETYPE_PEM
);
537 SSL_CTX_use_certificate_file(context
, ServerCertificate
, SSL_FILETYPE_PEM
);
539 conn
= SSL_new(context
);
541 SSL_set_fd(conn
, con
->http
.fd
);
542 if (SSL_accept(conn
) != 1)
544 while ((error
= ERR_get_error()) != 0)
545 LogMessage(L_ERROR
, "EncryptClient: %s", ERR_error_string(error
, NULL
));
547 SSL_CTX_free(context
);
552 con
->http
.tls
= conn
;
555 #elif defined(HAVE_GNUTLS)
556 http_tls_t
*conn
; /* TLS session object */
557 int error
; /* Error code */
558 gnutls_certificate_server_credentials
*credentials
;
559 /* TLS credentials */
562 * Create the SSL object and perform the SSL handshake...
565 conn
= (http_tls_t
*)malloc(sizeof(gnutls_session
));
570 credentials
= (gnutls_certificate_server_credentials
*)
571 malloc(sizeof(gnutls_certificate_server_credentials
));
572 if (credentials
== NULL
)
578 gnutls_certificate_allocate_credentials(credentials
);
579 gnutls_certificate_set_x509_key_file(*credentials
, ServerCertificate
,
580 ServerKey
, GNUTLS_X509_FMT_PEM
);
582 gnutls_init(&(conn
->session
), GNUTLS_SERVER
);
583 gnutls_set_default_priority(conn
->session
);
584 gnutls_credentials_set(conn
->session
, GNUTLS_CRD_CERTIFICATE
, *credentials
);
585 gnutls_transport_set_ptr(conn
->session
, con
->http
.fd
);
587 error
= gnutls_handshake(conn
->session
);
589 if (error
!= GNUTLS_E_SUCCESS
)
591 LogMessage(L_ERROR
, "EncryptClient: %s", gnutls_strerror(error
));
592 gnutls_deinit(conn
->session
);
593 gnutls_certificate_free_credentials(*credentials
);
599 LogMessage(L_DEBUG
, "EncryptClient() %d Connection now encrypted.",
602 conn
->credentials
= credentials
;
603 con
->http
.tls
= conn
;
606 #elif defined(HAVE_CDSASSL)
607 OSStatus error
; /* Error info */
608 SSLContextRef conn
; /* New connection */
609 SSLProtocol tryVersion
; /* Protocol version */
610 const char *hostName
; /* Local hostname */
611 int allowExpired
; /* Allow expired certificates? */
612 int allowAnyRoot
; /* Allow any root certificate? */
613 SSLProtocol
*negVersion
; /* Negotiated protocol version */
614 SSLCipherSuite
*negCipher
; /* Negotiated cypher */
615 CFArrayRef
*peerCerts
; /* Certificates */
619 error
= SSLNewContext(true, &conn
);
624 error
= SSLSetIOFuncs(conn
, CDSAReadFunc
, CDSAWriteFunc
);
627 error
= SSLSetProtocolVersion(conn
, kSSLProtocol3
);
630 error
= SSLSetConnection(conn
, (SSLConnectionRef
)con
->http
.fd
);
634 hostName
= ServerName
; /* MRS: ??? */
635 error
= SSLSetPeerDomainName(conn
, hostName
, strlen(hostName
) + 1);
638 /* have to do these options befor setting server certs */
639 if (!error
&& allowExpired
)
640 error
= SSLSetAllowsExpiredCerts(conn
, true);
642 if (!error
&& allowAnyRoot
)
643 error
= SSLSetAllowsAnyRoot(conn
, true);
645 if (!error
&& ServerCertificatesArray
!= NULL
)
646 error
= SSLSetCertificate(conn
, ServerCertificatesArray
);
649 * Perform SSL/TLS handshake
654 error
= SSLHandshake(conn
);
656 while (error
== errSSLWouldBlock
);
660 LogMessage(L_ERROR
, "EncryptClient: %d", error
);
662 con
->http
.error
= error
;
663 con
->http
.status
= HTTP_ERROR
;
666 SSLDisposeContext(conn
);
671 LogMessage(L_DEBUG
, "EncryptClient() %d Connection now encrypted.",
673 con
->http
.tls
= conn
;
678 #endif /* HAVE_GNUTLS */
683 * 'ReadClient()' - Read data from a client.
686 int /* O - 1 on success, 0 on error */
687 ReadClient(client_t
*con
) /* I - Client to read from */
689 char line
[32768], /* Line from client... */
690 operation
[64], /* Operation code from socket */
691 version
[64]; /* HTTP version number string */
692 int major
, minor
; /* HTTP version numbers */
693 http_status_t status
; /* Transfer status */
694 ipp_state_t ipp_state
; /* State of IPP transfer */
695 int bytes
; /* Number of bytes to POST */
696 char *filename
; /* Name of file for GET/HEAD */
697 char buf
[1024]; /* Buffer for real filename */
698 struct stat filestats
; /* File information */
699 mime_type_t
*type
; /* MIME type of file */
700 printer_t
*p
; /* Printer */
701 location_t
*best
; /* Best match for authentication */
702 static unsigned request_id
= 0;/* Request ID for temp files */
705 status
= HTTP_CONTINUE
;
707 LogMessage(L_DEBUG2
, "ReadClient() %d, used=%d", con
->http
.fd
,
716 switch (con
->http
.state
)
720 * See if we've received a request line...
723 if (httpGets(line
, sizeof(line
) - 1, HTTP(con
)) == NULL
)
730 * Ignore blank request lines...
737 * Clear other state variables...
740 httpClearFields(HTTP(con
));
742 con
->http
.activity
= time(NULL
);
743 con
->http
.version
= HTTP_1_0
;
744 con
->http
.keep_alive
= HTTP_KEEPALIVE_OFF
;
745 con
->http
.data_encoding
= HTTP_ENCODE_LENGTH
;
746 con
->http
.data_remaining
= 0;
747 con
->operation
= HTTP_WAITING
;
751 con
->username
[0] = '\0';
752 con
->password
[0] = '\0';
755 ClearString(&con
->command
);
756 ClearString(&con
->options
);
758 if (con
->language
!= NULL
)
760 cupsLangFree(con
->language
);
761 con
->language
= NULL
;
765 * Grab the request line...
768 switch (sscanf(line
, "%63s%1023s%63s", operation
, con
->uri
, version
))
771 LogMessage(L_ERROR
, "Bad request line \"%s\"!", line
);
772 SendError(con
, HTTP_BAD_REQUEST
);
776 con
->http
.version
= HTTP_0_9
;
779 if (sscanf(version
, "HTTP/%d.%d", &major
, &minor
) != 2)
781 LogMessage(L_ERROR
, "Bad request line \"%s\"!", line
);
782 SendError(con
, HTTP_BAD_REQUEST
);
789 con
->http
.version
= (http_version_t
)(major
* 100 + minor
);
790 if (con
->http
.version
== HTTP_1_1
&& KeepAlive
)
791 con
->http
.keep_alive
= HTTP_KEEPALIVE_ON
;
793 con
->http
.keep_alive
= HTTP_KEEPALIVE_OFF
;
797 SendError(con
, HTTP_NOT_SUPPORTED
);
805 * Handle full URLs in the request line...
808 if (con
->uri
[0] != '/' && strcmp(con
->uri
, "*"))
810 char method
[HTTP_MAX_URI
], /* Method/scheme */
811 userpass
[HTTP_MAX_URI
], /* Username:password */
812 hostname
[HTTP_MAX_URI
], /* Hostname */
813 resource
[HTTP_MAX_URI
]; /* Resource path */
814 int port
; /* Port number */
818 * Separate the URI into its components...
821 httpSeparate(con
->uri
, method
, userpass
, hostname
, &port
, resource
);
824 * Only allow URIs with the servername, localhost, or an IP
828 if (strcasecmp(hostname
, ServerName
) &&
829 strcasecmp(hostname
, "localhost") &&
830 !isdigit(hostname
[0]))
833 * Nope, we don't do proxies...
836 LogMessage(L_ERROR
, "Bad URI \"%s\" in request!", con
->uri
);
837 SendError(con
, HTTP_METHOD_NOT_ALLOWED
);
843 * Copy the resource portion back into the URI; both resource and
844 * con->uri are HTTP_MAX_URI bytes in size...
847 strcpy(con
->uri
, resource
);
851 * Process the request...
854 if (strcmp(operation
, "GET") == 0)
855 con
->http
.state
= HTTP_GET
;
856 else if (strcmp(operation
, "PUT") == 0)
857 con
->http
.state
= HTTP_PUT
;
858 else if (strcmp(operation
, "POST") == 0)
859 con
->http
.state
= HTTP_POST
;
860 else if (strcmp(operation
, "DELETE") == 0)
861 con
->http
.state
= HTTP_DELETE
;
862 else if (strcmp(operation
, "TRACE") == 0)
863 con
->http
.state
= HTTP_TRACE
;
864 else if (strcmp(operation
, "OPTIONS") == 0)
865 con
->http
.state
= HTTP_OPTIONS
;
866 else if (strcmp(operation
, "HEAD") == 0)
867 con
->http
.state
= HTTP_HEAD
;
870 LogMessage(L_ERROR
, "Bad operation \"%s\"!", operation
);
871 SendError(con
, HTTP_BAD_REQUEST
);
876 con
->start
= time(NULL
);
877 con
->operation
= con
->http
.state
;
879 LogMessage(L_DEBUG
, "ReadClient() %d %s %s HTTP/%d.%d", con
->http
.fd
,
881 con
->http
.version
/ 100, con
->http
.version
% 100);
883 con
->http
.status
= HTTP_OK
;
893 * Parse incoming parameters until the status changes...
896 status
= httpUpdate(HTTP(con
));
898 if (status
!= HTTP_OK
&& status
!= HTTP_CONTINUE
)
900 SendError(con
, HTTP_BAD_REQUEST
);
907 break; /* Anti-compiler-warning-code */
911 * Handle new transfers...
914 if (status
== HTTP_OK
)
916 con
->language
= cupsLangGet(con
->http
.fields
[HTTP_FIELD_ACCEPT_LANGUAGE
]);
920 if (strncmp(con
->http
.fields
[HTTP_FIELD_CONNECTION
], "Keep-Alive", 10) == 0 &&
922 con
->http
.keep_alive
= HTTP_KEEPALIVE_ON
;
924 if (con
->http
.fields
[HTTP_FIELD_HOST
][0] == '\0' &&
925 con
->http
.version
>= HTTP_1_1
)
928 * HTTP/1.1 and higher require the "Host:" field...
931 if (!SendError(con
, HTTP_BAD_REQUEST
))
937 else if (con
->operation
== HTTP_OPTIONS
)
940 * Do OPTIONS command...
943 if ((best
= FindBest(con
->uri
, con
->http
.state
)) != NULL
&&
944 best
->type
!= AUTH_NONE
)
946 if (!SendHeader(con
, HTTP_UNAUTHORIZED
, NULL
))
953 if (strcasecmp(con
->http
.fields
[HTTP_FIELD_CONNECTION
], "Upgrade") == 0 &&
954 con
->http
.tls
== NULL
)
958 * Do encryption stuff...
961 if (!SendHeader(con
, HTTP_SWITCHING_PROTOCOLS
, NULL
))
967 httpPrintf(HTTP(con
), "Connection: Upgrade\r\n");
968 httpPrintf(HTTP(con
), "Upgrade: TLS/1.0,HTTP/1.1\r\n");
969 httpPrintf(HTTP(con
), "Content-Length: 0\r\n");
970 httpPrintf(HTTP(con
), "\r\n");
974 if (!SendError(con
, HTTP_NOT_IMPLEMENTED
))
979 #endif /* HAVE_SSL */
982 if (con
->http
.expect
)
984 /**** TODO: send expected header ****/
987 if (!SendHeader(con
, HTTP_OK
, NULL
))
993 httpPrintf(HTTP(con
), "Allow: GET, HEAD, OPTIONS, POST, PUT\r\n");
994 httpPrintf(HTTP(con
), "Content-Length: 0\r\n");
995 httpPrintf(HTTP(con
), "\r\n");
997 else if (strstr(con
->uri
, "..") != NULL
)
1000 * Protect against malicious users!
1003 if (!SendError(con
, HTTP_FORBIDDEN
))
1011 if (strcasecmp(con
->http
.fields
[HTTP_FIELD_CONNECTION
], "Upgrade") == 0 &&
1012 con
->http
.tls
== NULL
)
1016 * Do encryption stuff...
1019 if (!SendHeader(con
, HTTP_SWITCHING_PROTOCOLS
, NULL
))
1025 httpPrintf(HTTP(con
), "Connection: Upgrade\r\n");
1026 httpPrintf(HTTP(con
), "Upgrade: TLS/1.0,HTTP/1.1\r\n");
1027 httpPrintf(HTTP(con
), "Content-Length: 0\r\n");
1028 httpPrintf(HTTP(con
), "\r\n");
1032 status
= IsAuthorized(con
);
1034 if (!SendError(con
, HTTP_NOT_IMPLEMENTED
))
1039 #endif /* HAVE_SSL */
1042 if (status
!= HTTP_OK
)
1044 LogMessage(L_DEBUG2
, "ReadClient: Unauthorized request for %s...\n",
1046 SendError(con
, status
);
1047 ShutdownClient(con
);
1051 if (con
->http
.expect
)
1053 /**** TODO: send expected header ****/
1056 switch (con
->http
.state
)
1058 case HTTP_GET_SEND
:
1059 if (strncmp(con
->uri
, "/printers/", 10) == 0 &&
1060 strcmp(con
->uri
+ strlen(con
->uri
) - 4, ".ppd") == 0)
1063 * Send PPD file - get the real printer name since printer
1064 * names are not case sensitive but filenames can be...
1067 con
->uri
[strlen(con
->uri
) - 4] = '\0'; /* Drop ".ppd" */
1069 if ((p
= FindPrinter(con
->uri
+ 10)) != NULL
)
1070 snprintf(con
->uri
, sizeof(con
->uri
), "/ppd/%s.ppd", p
->name
);
1073 if (!SendError(con
, HTTP_NOT_FOUND
))
1083 if ((strncmp(con
->uri
, "/admin", 6) == 0 &&
1084 strncmp(con
->uri
, "/admin/conf/", 12) != 0) ||
1085 strncmp(con
->uri
, "/printers", 9) == 0 ||
1086 strncmp(con
->uri
, "/classes", 8) == 0 ||
1087 strncmp(con
->uri
, "/jobs", 5) == 0)
1090 * Send CGI output...
1093 if (strncmp(con
->uri
, "/admin", 6) == 0)
1095 SetStringf(&con
->command
, "%s/cgi-bin/admin.cgi", ServerBin
);
1096 SetString(&con
->options
, con
->uri
+ 6);
1098 else if (strncmp(con
->uri
, "/printers", 9) == 0)
1100 SetStringf(&con
->command
, "%s/cgi-bin/printers.cgi", ServerBin
);
1101 SetString(&con
->options
, con
->uri
+ 9);
1103 else if (strncmp(con
->uri
, "/classes", 8) == 0)
1105 SetStringf(&con
->command
, "%s/cgi-bin/classes.cgi", ServerBin
);
1106 SetString(&con
->options
, con
->uri
+ 8);
1110 SetStringf(&con
->command
, "%s/cgi-bin/jobs.cgi", ServerBin
);
1111 SetString(&con
->options
, con
->uri
+ 5);
1114 if (con
->options
[0] == '/')
1115 strcpy(con
->options
, con
->options
+ 1);
1117 if (!SendCommand(con
, con
->command
, con
->options
))
1119 if (!SendError(con
, HTTP_NOT_FOUND
))
1126 LogRequest(con
, HTTP_OK
);
1128 if (con
->http
.version
<= HTTP_1_0
)
1129 con
->http
.keep_alive
= HTTP_KEEPALIVE_OFF
;
1131 else if (strncmp(con
->uri
, "/admin/conf/", 12) == 0 &&
1132 (strchr(con
->uri
+ 12, '/') != NULL
||
1133 strlen(con
->uri
) == 12))
1136 * GET can only be done to configuration files under
1140 if (!SendError(con
, HTTP_FORBIDDEN
))
1154 if ((filename
= get_file(con
, &filestats
, buf
,
1155 sizeof(buf
))) == NULL
)
1157 if (!SendError(con
, HTTP_NOT_FOUND
))
1166 type
= mimeFileType(MimeDatabase
, filename
);
1168 if (IsCGI(con
, filename
, &filestats
, type
))
1170 if (!SendCommand(con
, con
->command
, con
->options
))
1172 if (!SendError(con
, HTTP_NOT_FOUND
))
1179 LogRequest(con
, HTTP_OK
);
1181 if (con
->http
.version
<= HTTP_1_0
)
1182 con
->http
.keep_alive
= HTTP_KEEPALIVE_OFF
;
1186 if (!check_if_modified(con
, &filestats
))
1188 if (!SendError(con
, HTTP_NOT_MODIFIED
))
1197 strcpy(line
, "text/plain");
1199 snprintf(line
, sizeof(line
), "%s/%s", type
->super
, type
->type
);
1201 if (!SendFile(con
, HTTP_OK
, filename
, line
, &filestats
))
1210 case HTTP_POST_RECV
:
1212 * See if the POST request includes a Content-Length field, and if
1213 * so check the length against any limits that are set...
1216 LogMessage(L_DEBUG2
, "POST %s", con
->uri
);
1217 LogMessage(L_DEBUG2
, "CONTENT_TYPE = %s", con
->http
.fields
[HTTP_FIELD_CONTENT_TYPE
]);
1219 if (con
->http
.fields
[HTTP_FIELD_CONTENT_LENGTH
][0] &&
1220 atoi(con
->http
.fields
[HTTP_FIELD_CONTENT_LENGTH
]) > MaxRequestSize
&&
1224 * Request too large...
1227 if (!SendError(con
, HTTP_REQUEST_TOO_LARGE
))
1235 else if (atoi(con
->http
.fields
[HTTP_FIELD_CONTENT_LENGTH
]) < 0)
1238 * Negative content lengths are invalid!
1241 if (!SendError(con
, HTTP_BAD_REQUEST
))
1251 * See what kind of POST request this is; for IPP requests the
1252 * content-type field will be "application/ipp"...
1255 if (strcmp(con
->http
.fields
[HTTP_FIELD_CONTENT_TYPE
], "application/ipp") == 0)
1256 con
->request
= ippNew();
1257 else if ((strncmp(con
->uri
, "/admin", 6) == 0 &&
1258 strncmp(con
->uri
, "/admin/conf/", 12) != 0) ||
1259 strncmp(con
->uri
, "/printers", 9) == 0 ||
1260 strncmp(con
->uri
, "/classes", 8) == 0 ||
1261 strncmp(con
->uri
, "/jobs", 5) == 0)
1267 if (strncmp(con
->uri
, "/admin", 6) == 0)
1269 SetStringf(&con
->command
, "%s/cgi-bin/admin.cgi", ServerBin
);
1270 SetString(&con
->options
, con
->uri
+ 6);
1272 else if (strncmp(con
->uri
, "/printers", 9) == 0)
1274 SetStringf(&con
->command
, "%s/cgi-bin/printers.cgi", ServerBin
);
1275 SetString(&con
->options
, con
->uri
+ 9);
1277 else if (strncmp(con
->uri
, "/classes", 8) == 0)
1279 SetStringf(&con
->command
, "%s/cgi-bin/classes.cgi", ServerBin
);
1280 SetString(&con
->options
, con
->uri
+ 8);
1284 SetStringf(&con
->command
, "%s/cgi-bin/jobs.cgi", ServerBin
);
1285 SetString(&con
->options
, con
->uri
+ 5);
1288 if (con
->options
[0] == '/')
1289 strcpy(con
->options
, con
->options
+ 1);
1291 LogMessage(L_DEBUG2
, "ReadClient() %d command=\"%s\", options = \"%s\"",
1292 con
->http
.fd
, con
->command
, con
->options
);
1294 if (con
->http
.version
<= HTTP_1_0
)
1295 con
->http
.keep_alive
= HTTP_KEEPALIVE_OFF
;
1303 if ((filename
= get_file(con
, &filestats
, buf
,
1304 sizeof(buf
))) == NULL
)
1306 if (!SendError(con
, HTTP_NOT_FOUND
))
1315 type
= mimeFileType(MimeDatabase
, filename
);
1317 if (!IsCGI(con
, filename
, &filestats
, type
))
1320 * Only POST to CGI's...
1323 if (!SendError(con
, HTTP_UNAUTHORIZED
))
1332 case HTTP_PUT_RECV
:
1334 * Validate the resource name...
1337 if (strncmp(con
->uri
, "/admin/conf/", 12) != 0 ||
1338 strchr(con
->uri
+ 12, '/') != NULL
||
1339 strlen(con
->uri
) == 12)
1342 * PUT can only be done to configuration files under
1346 if (!SendError(con
, HTTP_FORBIDDEN
))
1356 * See if the PUT request includes a Content-Length field, and if
1357 * so check the length against any limits that are set...
1360 LogMessage(L_DEBUG2
, "PUT %s", con
->uri
);
1361 LogMessage(L_DEBUG2
, "CONTENT_TYPE = %s", con
->http
.fields
[HTTP_FIELD_CONTENT_TYPE
]);
1363 if (con
->http
.fields
[HTTP_FIELD_CONTENT_LENGTH
][0] &&
1364 atoi(con
->http
.fields
[HTTP_FIELD_CONTENT_LENGTH
]) > MaxRequestSize
&&
1368 * Request too large...
1371 if (!SendError(con
, HTTP_REQUEST_TOO_LARGE
))
1381 * Open a temporary file to hold the request...
1384 SetStringf(&con
->filename
, "%s/%08x", RequestRoot
, request_id
++);
1385 con
->file
= open(con
->filename
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0640);
1386 fchmod(con
->file
, 0640);
1387 fchown(con
->file
, User
, Group
);
1389 LogMessage(L_DEBUG2
, "ReadClient() %d REQUEST %s=%d", con
->http
.fd
,
1390 con
->filename
, con
->file
);
1394 if (!SendError(con
, HTTP_REQUEST_TOO_LARGE
))
1404 SendError(con
, HTTP_NOT_IMPLEMENTED
);
1405 ShutdownClient(con
);
1409 if (strncmp(con
->uri
, "/printers/", 10) == 0 &&
1410 strcmp(con
->uri
+ strlen(con
->uri
) - 4, ".ppd") == 0)
1413 * Send PPD file - get the real printer name since printer
1414 * names are not case sensitive but filenames can be...
1417 con
->uri
[strlen(con
->uri
) - 4] = '\0'; /* Drop ".ppd" */
1419 if ((p
= FindPrinter(con
->uri
+ 10)) != NULL
)
1420 snprintf(con
->uri
, sizeof(con
->uri
), "/ppd/%s.ppd", p
->name
);
1423 if (!SendError(con
, HTTP_NOT_FOUND
))
1433 if ((strncmp(con
->uri
, "/admin/", 7) == 0 &&
1434 strncmp(con
->uri
, "/admin/conf/", 12) != 0) ||
1435 strncmp(con
->uri
, "/printers/", 10) == 0 ||
1436 strncmp(con
->uri
, "/classes/", 9) == 0 ||
1437 strncmp(con
->uri
, "/jobs/", 6) == 0)
1443 if (!SendHeader(con
, HTTP_OK
, "text/html"))
1449 if (httpPrintf(HTTP(con
), "\r\n") < 0)
1455 LogRequest(con
, HTTP_OK
);
1457 else if (strncmp(con
->uri
, "/admin/conf/", 12) == 0 &&
1458 (strchr(con
->uri
+ 12, '/') != NULL
||
1459 strlen(con
->uri
) == 12))
1462 * HEAD can only be done to configuration files under
1466 if (!SendError(con
, HTTP_FORBIDDEN
))
1474 else if ((filename
= get_file(con
, &filestats
, buf
,
1475 sizeof(buf
))) == NULL
)
1477 if (!SendHeader(con
, HTTP_NOT_FOUND
, "text/html"))
1483 LogRequest(con
, HTTP_NOT_FOUND
);
1485 else if (!check_if_modified(con
, &filestats
))
1487 if (!SendError(con
, HTTP_NOT_MODIFIED
))
1493 LogRequest(con
, HTTP_NOT_MODIFIED
);
1501 type
= mimeFileType(MimeDatabase
, filename
);
1503 strcpy(line
, "text/plain");
1505 snprintf(line
, sizeof(line
), "%s/%s", type
->super
, type
->type
);
1507 if (!SendHeader(con
, HTTP_OK
, line
))
1513 if (httpPrintf(HTTP(con
), "Last-Modified: %s\r\n",
1514 httpGetDateString(filestats
.st_mtime
)) < 0)
1520 if (httpPrintf(HTTP(con
), "Content-Length: %lu\r\n",
1521 (unsigned long)filestats
.st_size
) < 0)
1527 LogRequest(con
, HTTP_OK
);
1530 if (httpPrintf(HTTP(con
), "\r\n") < 0)
1536 con
->http
.state
= HTTP_WAITING
;
1540 break; /* Anti-compiler-warning-code */
1546 * Handle any incoming data...
1549 switch (con
->http
.state
)
1551 case HTTP_PUT_RECV
:
1552 LogMessage(L_DEBUG2
, "ReadClient() %d con->data_encoding = %s, con->data_remaining = %d, con->file = %d",
1554 con
->http
.data_encoding
== HTTP_ENCODE_CHUNKED
? "chunked" : "length",
1555 con
->http
.data_remaining
, con
->file
);
1557 if ((bytes
= httpRead(HTTP(con
), line
, sizeof(line
))) < 0)
1564 con
->bytes
+= bytes
;
1566 LogMessage(L_DEBUG2
, "ReadClient() %d writing %d bytes to %d",
1567 con
->http
.fd
, bytes
, con
->file
);
1569 if (write(con
->file
, line
, bytes
) < bytes
)
1571 LogMessage(L_ERROR
, "ReadClient: Unable to write %d bytes to %s: %s",
1572 bytes
, con
->filename
, strerror(errno
));
1574 LogMessage(L_DEBUG2
, "ReadClient: Closing data file %d...",
1579 unlink(con
->filename
);
1580 ClearString(&con
->filename
);
1582 if (!SendError(con
, HTTP_REQUEST_TOO_LARGE
))
1590 if (con
->http
.state
== HTTP_WAITING
)
1593 * End of file, see how big it is...
1596 fstat(con
->file
, &filestats
);
1598 LogMessage(L_DEBUG2
, "ReadClient() %d Closing data file %d, size = %d.",
1599 con
->http
.fd
, con
->file
, (int)filestats
.st_size
);
1604 if (filestats
.st_size
> MaxRequestSize
&&
1608 * Request is too big; remove it and send an error...
1611 LogMessage(L_DEBUG2
, "ReadClient() %d Removing temp file %s",
1612 con
->http
.fd
, con
->filename
);
1613 unlink(con
->filename
);
1614 ClearString(&con
->filename
);
1616 if (!SendError(con
, HTTP_REQUEST_TOO_LARGE
))
1624 * Install the configuration file...
1627 status
= install_conf_file(con
);
1630 * Return the status to the client...
1633 if (!SendError(con
, status
))
1641 case HTTP_POST_RECV
:
1642 LogMessage(L_DEBUG2
, "ReadClient() %d con->data_encoding = %s, con->data_remaining = %d, con->file = %d",
1644 con
->http
.data_encoding
== HTTP_ENCODE_CHUNKED
? "chunked" : "length",
1645 con
->http
.data_remaining
, con
->file
);
1647 if (con
->request
!= NULL
)
1650 * Grab any request data from the connection...
1653 if ((ipp_state
= ippRead(&(con
->http
), con
->request
)) == IPP_ERROR
)
1655 LogMessage(L_ERROR
, "ReadClient() %d IPP Read Error!",
1658 if (!SendError(con
, HTTP_BAD_REQUEST
))
1664 ShutdownClient(con
);
1667 else if (ipp_state
!= IPP_DATA
)
1670 con
->bytes
+= ippLength(con
->request
);
1673 if (con
->file
== 0 && con
->http
.state
!= HTTP_POST_SEND
)
1676 * Create a file as needed for the request data...
1679 SetStringf(&con
->filename
, "%s/%08x", RequestRoot
, request_id
++);
1680 con
->file
= open(con
->filename
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0640);
1681 fchmod(con
->file
, 0640);
1682 fchown(con
->file
, User
, Group
);
1684 LogMessage(L_DEBUG2
, "ReadClient() %d REQUEST %s=%d", con
->http
.fd
,
1685 con
->filename
, con
->file
);
1689 if (!SendError(con
, HTTP_REQUEST_TOO_LARGE
))
1697 if (con
->http
.state
!= HTTP_POST_SEND
)
1699 if ((bytes
= httpRead(HTTP(con
), line
, sizeof(line
))) < 0)
1706 con
->bytes
+= bytes
;
1708 LogMessage(L_DEBUG2
, "ReadClient() %d writing %d bytes to %d",
1709 con
->http
.fd
, bytes
, con
->file
);
1711 if (write(con
->file
, line
, bytes
) < bytes
)
1713 LogMessage(L_ERROR
, "ReadClient: Unable to write %d bytes to %s: %s",
1714 bytes
, con
->filename
, strerror(errno
));
1716 LogMessage(L_DEBUG2
, "ReadClient: Closing file %d...",
1721 unlink(con
->filename
);
1722 ClearString(&con
->filename
);
1724 if (!SendError(con
, HTTP_REQUEST_TOO_LARGE
))
1731 else if (con
->http
.state
!= HTTP_POST_SEND
)
1738 if (con
->http
.state
== HTTP_POST_SEND
)
1742 fstat(con
->file
, &filestats
);
1744 LogMessage(L_DEBUG2
, "ReadClient() %d Closing data file %d, size = %d.",
1745 con
->http
.fd
, con
->file
, (int)filestats
.st_size
);
1750 if (filestats
.st_size
> MaxRequestSize
&&
1754 * Request is too big; remove it and send an error...
1757 LogMessage(L_DEBUG2
, "ReadClient() %d Removing temp file %s",
1758 con
->http
.fd
, con
->filename
);
1759 unlink(con
->filename
);
1760 ClearString(&con
->filename
);
1765 * Delete any IPP request data...
1768 ippDelete(con
->request
);
1769 con
->request
= NULL
;
1772 if (!SendError(con
, HTTP_REQUEST_TOO_LARGE
))
1781 if (!SendCommand(con
, con
->command
, con
->options
))
1783 if (!SendError(con
, HTTP_NOT_FOUND
))
1790 LogRequest(con
, HTTP_OK
);
1795 return (ProcessIPPRequest(con
));
1800 break; /* Anti-compiler-warning-code */
1803 if (!con
->http
.keep_alive
&& con
->http
.state
== HTTP_WAITING
)
1814 * 'SendCommand()' - Send output from a command via HTTP.
1818 SendCommand(client_t
*con
,
1826 fd
= open(con
->filename
, O_RDONLY
);
1828 fd
= open("/dev/null", O_RDONLY
);
1830 con
->pipe_pid
= pipe_command(con
, fd
, &(con
->file
), command
, options
);
1834 LogMessage(L_INFO
, "Started \"%s\" (pid=%d)", command
, con
->pipe_pid
);
1836 LogMessage(L_DEBUG
, "SendCommand() %d file=%d", con
->http
.fd
, con
->file
);
1838 if (con
->pipe_pid
== 0)
1841 fcntl(con
->file
, F_SETFD
, fcntl(con
->file
, F_GETFD
) | FD_CLOEXEC
);
1843 LogMessage(L_DEBUG2
, "SendCommand: Adding fd %d to InputSet...", con
->file
);
1844 LogMessage(L_DEBUG2
, "SendCommand: Adding fd %d to OutputSet...",
1847 FD_SET(con
->file
, InputSet
);
1848 FD_SET(con
->http
.fd
, OutputSet
);
1850 if (!SendHeader(con
, HTTP_OK
, NULL
))
1853 if (con
->http
.version
== HTTP_1_1
)
1855 con
->http
.data_encoding
= HTTP_ENCODE_CHUNKED
;
1857 if (httpPrintf(HTTP(con
), "Transfer-Encoding: chunked\r\n") < 0)
1861 con
->got_fields
= 0;
1869 * 'SendError()' - Send an error message via HTTP.
1872 int /* O - 1 if successful, 0 otherwise */
1873 SendError(client_t
*con
, /* I - Connection */
1874 http_status_t code
) /* I - Error code */
1876 char message
[1024]; /* Message for user */
1880 * Put the request in the access_log file...
1883 if (con
->operation
> HTTP_WAITING
)
1884 LogRequest(con
, code
);
1886 LogMessage(L_DEBUG
, "SendError() %d code=%d", con
->http
.fd
, code
);
1889 * To work around bugs in some proxies, don't use Keep-Alive for some
1893 if (code
>= HTTP_BAD_REQUEST
)
1894 con
->http
.keep_alive
= HTTP_KEEPALIVE_OFF
;
1897 * Send an error message back to the client. If the error code is a
1898 * 400 or 500 series, make sure the message contains some text, too!
1901 if (!SendHeader(con
, code
, NULL
))
1905 if (code
== HTTP_UPGRADE_REQUIRED
)
1906 if (httpPrintf(HTTP(con
), "Connection: Upgrade\r\n") < 0)
1909 if (httpPrintf(HTTP(con
), "Upgrade: TLS/1.0,HTTP/1.1\r\n") < 0)
1911 #endif /* HAVE_SSL */
1913 if ((con
->http
.version
>= HTTP_1_1
&& !con
->http
.keep_alive
) ||
1914 (code
>= HTTP_BAD_REQUEST
&& code
!= HTTP_UPGRADE_REQUIRED
))
1916 if (httpPrintf(HTTP(con
), "Connection: close\r\n") < 0)
1920 if (code
>= HTTP_BAD_REQUEST
)
1923 * Send a human-readable error message.
1926 snprintf(message
, sizeof(message
),
1927 "<HTML><HEAD><TITLE>%d %s</TITLE></HEAD>"
1928 "<BODY><H1>%s</H1>%s</BODY></HTML>\n",
1929 code
, httpStatus(code
), httpStatus(code
),
1930 con
->language
? con
->language
->messages
[code
] :
1933 if (httpPrintf(HTTP(con
), "Content-Type: text/html\r\n") < 0)
1935 if (httpPrintf(HTTP(con
), "Content-Length: %d\r\n",
1936 (int)strlen(message
)) < 0)
1938 if (httpPrintf(HTTP(con
), "\r\n") < 0)
1940 if (httpPrintf(HTTP(con
), "%s", message
) < 0)
1943 else if (httpPrintf(HTTP(con
), "\r\n") < 0)
1946 con
->http
.state
= HTTP_WAITING
;
1953 * 'SendFile()' - Send a file via HTTP.
1957 SendFile(client_t
*con
,
1961 struct stat
*filestats
)
1963 con
->file
= open(filename
, O_RDONLY
);
1965 LogMessage(L_DEBUG
, "SendFile() %d file=%d", con
->http
.fd
, con
->file
);
1970 fcntl(con
->file
, F_SETFD
, fcntl(con
->file
, F_GETFD
) | FD_CLOEXEC
);
1974 if (!SendHeader(con
, code
, type
))
1977 if (httpPrintf(HTTP(con
), "Last-Modified: %s\r\n", httpGetDateString(filestats
->st_mtime
)) < 0)
1979 if (httpPrintf(HTTP(con
), "Content-Length: %lu\r\n",
1980 (unsigned long)filestats
->st_size
) < 0)
1982 if (httpPrintf(HTTP(con
), "\r\n") < 0)
1985 LogMessage(L_DEBUG2
, "SendFile: Adding fd %d to OutputSet...", con
->http
.fd
);
1987 FD_SET(con
->http
.fd
, OutputSet
);
1994 * 'SendHeader()' - Send an HTTP request.
1997 int /* O - 1 on success, 0 on failure */
1998 SendHeader(client_t
*con
, /* I - Client to send to */
1999 http_status_t code
, /* I - HTTP status code */
2000 char *type
) /* I - MIME type of document */
2002 location_t
*loc
; /* Authentication location */
2005 if (httpPrintf(HTTP(con
), "HTTP/%d.%d %d %s\r\n", con
->http
.version
/ 100,
2006 con
->http
.version
% 100, code
, httpStatus(code
)) < 0)
2008 if (httpPrintf(HTTP(con
), "Date: %s\r\n", httpGetDateString(time(NULL
))) < 0)
2010 if (httpPrintf(HTTP(con
), "Server: CUPS/1.1\r\n") < 0)
2012 if (con
->http
.keep_alive
&& con
->http
.version
>= HTTP_1_0
)
2014 if (httpPrintf(HTTP(con
), "Connection: Keep-Alive\r\n") < 0)
2016 if (httpPrintf(HTTP(con
), "Keep-Alive: timeout=%d\r\n", KeepAliveTimeout
) < 0)
2019 if (code
== HTTP_METHOD_NOT_ALLOWED
)
2020 if (httpPrintf(HTTP(con
), "Allow: GET, HEAD, OPTIONS, POST\r\n") < 0)
2023 if (code
== HTTP_UNAUTHORIZED
)
2026 * This already succeeded in IsAuthorized...
2029 loc
= FindBest(con
->uri
, con
->http
.state
);
2031 if (loc
->type
!= AUTH_DIGEST
)
2033 if (httpPrintf(HTTP(con
), "WWW-Authenticate: Basic realm=\"CUPS\"\r\n") < 0)
2038 if (httpPrintf(HTTP(con
), "WWW-Authenticate: Digest realm=\"CUPS\" "
2039 "nonce=\"%s\"\r\n", con
->http
.hostname
) < 0)
2043 if (con
->language
!= NULL
)
2045 if (httpPrintf(HTTP(con
), "Content-Language: %s\r\n",
2046 con
->language
->language
) < 0)
2050 if (httpPrintf(HTTP(con
), "Content-Type: %s; charset=%s\r\n", type
,
2051 cupsLangEncoding(con
->language
)) < 0)
2054 else if (type
!= NULL
)
2055 if (httpPrintf(HTTP(con
), "Content-Type: %s\r\n", type
) < 0)
2063 * 'ShutdownClient()' - Shutdown the receiving end of a connection.
2067 ShutdownClient(client_t
*con
) /* I - Client connection */
2070 * Shutdown the receiving end of the socket, since the client
2071 * still needs to read the error message...
2074 shutdown(con
->http
.fd
, 0);
2078 * Update the activity time so that we timeout after 30 seconds rather
2079 * then the current Timeout setting (300 by default). This prevents
2080 * some DoS situations...
2083 con
->http
.activity
= time(NULL
) - Timeout
+ 30;
2085 LogMessage(L_DEBUG2
, "ShutdownClient: Removing fd %d from InputSet...",
2088 FD_CLR(con
->http
.fd
, InputSet
);
2093 * 'WriteClient()' - Write data to a client as needed.
2096 int /* O - 1 if success, 0 if fail */
2097 WriteClient(client_t
*con
) /* I - Client connection */
2099 int bytes
; /* Number of bytes written */
2100 char buf
[HTTP_MAX_BUFFER
+ 1];/* Data buffer */
2101 char *bufptr
; /* Pointer into buffer */
2102 ipp_state_t ipp_state
; /* IPP state value */
2105 if (con
->http
.state
!= HTTP_GET_SEND
&&
2106 con
->http
.state
!= HTTP_POST_SEND
)
2109 if (con
->response
!= NULL
)
2111 ipp_state
= ippWrite(&(con
->http
), con
->response
);
2112 bytes
= ipp_state
!= IPP_ERROR
&& ipp_state
!= IPP_DATA
;
2114 else if ((bytes
= read(con
->file
, buf
, HTTP_MAX_BUFFER
)) > 0)
2116 if (con
->pipe_pid
&& !con
->got_fields
)
2119 * Inspect the data for Content-Type and other fields.
2124 for (bufptr
= buf
; !con
->got_fields
&& *bufptr
; bufptr
++)
2125 if (*bufptr
== '\n')
2128 * Send line to client...
2131 if (bufptr
> buf
&& bufptr
[-1] == '\r')
2135 httpPrintf(HTTP(con
), "%s\r\n", buf
);
2136 LogMessage(L_DEBUG2
, "WriteClient() %d %s", con
->http
.fd
, buf
);
2142 bytes
-= (bufptr
- buf
);
2143 memcpy(buf
, bufptr
, bytes
+ 1);
2147 * See if the line was empty...
2150 if (con
->field_col
== 0)
2151 con
->got_fields
= 1;
2155 else if (*bufptr
!= '\r')
2158 if (bytes
> 0 && !con
->got_fields
)
2161 * Remaining text needs to go out...
2164 httpPrintf(HTTP(con
), "%s", buf
);
2166 con
->http
.activity
= time(NULL
);
2169 else if (bytes
== 0)
2171 con
->http
.activity
= time(NULL
);
2176 if (httpWrite(HTTP(con
), buf
, bytes
) < 0)
2182 con
->bytes
+= bytes
;
2187 LogRequest(con
, HTTP_OK
);
2189 if (con
->http
.data_encoding
== HTTP_ENCODE_CHUNKED
)
2191 if (httpPrintf(HTTP(con
), "0\r\n\r\n") < 0)
2198 con
->http
.state
= HTTP_WAITING
;
2200 LogMessage(L_DEBUG2
, "WriteClient() Removing fd %d from OutputSet...",
2203 FD_CLR(con
->http
.fd
, OutputSet
);
2207 LogMessage(L_DEBUG2
, "WriteClient() Removing fd %d from InputSet...",
2209 FD_CLR(con
->file
, InputSet
);
2212 kill(con
->pipe_pid
, SIGTERM
);
2214 LogMessage(L_DEBUG2
, "WriteClient() %d Closing data file %d.",
2215 con
->http
.fd
, con
->file
);
2224 LogMessage(L_DEBUG2
, "WriteClient() %d Removing temp file %s",
2225 con
->http
.fd
, con
->filename
);
2226 unlink(con
->filename
);
2227 ClearString(&con
->filename
);
2230 if (con
->request
!= NULL
)
2232 ippDelete(con
->request
);
2233 con
->request
= NULL
;
2236 if (con
->response
!= NULL
)
2238 ippDelete(con
->response
);
2239 con
->response
= NULL
;
2242 ClearString(&con
->command
);
2243 ClearString(&con
->options
);
2245 if (!con
->http
.keep_alive
)
2253 LogMessage(L_DEBUG2
, "WriteClient() %d %d bytes", con
->http
.fd
, bytes
);
2255 con
->http
.activity
= time(NULL
);
2262 * 'check_if_modified()' - Decode an "If-Modified-Since" line.
2265 static int /* O - 1 if modified since */
2266 check_if_modified(client_t
*con
, /* I - Client connection */
2267 struct stat
*filestats
) /* I - File information */
2269 char *ptr
; /* Pointer into field */
2270 time_t date
; /* Time/date value */
2271 int size
; /* Size/length value */
2276 ptr
= con
->http
.fields
[HTTP_FIELD_IF_MODIFIED_SINCE
];
2281 LogMessage(L_DEBUG2
, "check_if_modified() %d If-Modified-Since=\"%s\"",
2284 while (*ptr
!= '\0')
2286 while (isspace(*ptr
) || *ptr
== ';')
2289 if (strncasecmp(ptr
, "length=", 7) == 0)
2294 while (isdigit(*ptr
))
2297 else if (isalpha(*ptr
))
2299 date
= httpGetDateTime(ptr
);
2300 while (*ptr
!= '\0' && *ptr
!= ';')
2305 LogMessage(L_DEBUG2
, "check_if_modified() %d sizes=%d,%d dates=%d,%d",
2306 con
->http
.fd
, size
, (int)filestats
->st_size
, (int)date
,
2307 (int)filestats
->st_mtime
);
2309 return ((size
!= filestats
->st_size
&& size
!= 0) ||
2310 (date
< filestats
->st_mtime
&& date
!= 0) ||
2311 (size
== 0 && date
== 0));
2316 * 'decode_auth()' - Decode an authorization string.
2320 decode_auth(client_t
*con
) /* I - Client to decode to */
2322 char *s
, /* Authorization string */
2323 value
[1024]; /* Value string */
2324 const char *username
; /* Certificate username */
2328 * Decode the string...
2331 s
= con
->http
.fields
[HTTP_FIELD_AUTHORIZATION
];
2333 LogMessage(L_DEBUG2
, "decode_auth(%p): Authorization string = \"%s\"",
2336 if (strncmp(s
, "Basic", 5) == 0)
2342 httpDecode64(value
, s
);
2345 * Pull the username and password out...
2348 if ((s
= strchr(value
, ':')) == NULL
)
2350 LogMessage(L_DEBUG
, "decode_auth() %d no colon in auth string \"%s\"",
2351 con
->http
.fd
, value
);
2357 strlcpy(con
->username
, value
, sizeof(con
->username
));
2358 strlcpy(con
->password
, s
, sizeof(con
->password
));
2360 else if (strncmp(s
, "Local", 5) == 0)
2366 if ((username
= FindCert(s
)) != NULL
)
2367 strlcpy(con
->username
, username
, sizeof(con
->username
));
2369 else if (strncmp(s
, "Digest", 5) == 0)
2372 * Get the username and password from the Digest attributes...
2375 if (httpGetSubField(&(con
->http
), HTTP_FIELD_WWW_AUTHENTICATE
, "username",
2377 strlcpy(con
->username
, value
, sizeof(con
->username
));
2379 if (httpGetSubField(&(con
->http
), HTTP_FIELD_WWW_AUTHENTICATE
, "response",
2381 strlcpy(con
->password
, value
, sizeof(con
->password
) - 1);
2384 LogMessage(L_DEBUG2
, "decode_auth() %d username=\"%s\"",
2385 con
->http
.fd
, con
->username
);
2390 * 'get_file()' - Get a filename and state info.
2393 static char * /* O - Real filename */
2394 get_file(client_t
*con
, /* I - Client connection */
2395 struct stat
*filestats
,/* O - File information */
2396 char *filename
, /* IO - Filename buffer */
2397 int len
) /* I - Buffer length */
2399 int status
; /* Status of filesystem calls */
2400 char *params
; /* Pointer to parameters in URI */
2404 * Need to add DocumentRoot global...
2407 if (strncmp(con
->uri
, "/ppd/", 5) == 0)
2408 snprintf(filename
, len
, "%s%s", ServerRoot
, con
->uri
);
2409 else if (strncmp(con
->uri
, "/admin/conf/", 12) == 0)
2410 snprintf(filename
, len
, "%s%s", ServerRoot
, con
->uri
+ 11);
2411 else if (con
->language
!= NULL
)
2412 snprintf(filename
, len
, "%s/%s%s", DocumentRoot
, con
->language
->language
,
2415 snprintf(filename
, len
, "%s%s", DocumentRoot
, con
->uri
);
2417 if ((params
= strchr(filename
, '?')) != NULL
)
2421 * Grab the status for this language; if there isn't a language-specific file
2422 * then fallback to the default one...
2425 if ((status
= stat(filename
, filestats
)) != 0 && con
->language
!= NULL
)
2428 * Drop the language prefix and try the current directory...
2431 if (strncmp(con
->uri
, "/ppd/", 5) != 0 &&
2432 strncmp(con
->uri
, "/admin/conf/", 12) != 0)
2434 snprintf(filename
, len
, "%s%s", DocumentRoot
, con
->uri
);
2436 status
= stat(filename
, filestats
);
2441 * If we're found a directory, get the index.html file instead...
2444 if (!status
&& S_ISDIR(filestats
->st_mode
))
2446 if (filename
[strlen(filename
) - 1] == '/')
2447 strlcat(filename
, "index.html", len
);
2449 strlcat(filename
, "/index.html", len
);
2451 status
= stat(filename
, filestats
);
2454 LogMessage(L_DEBUG2
, "get_file() %d filename=%s size=%d",
2455 con
->http
.fd
, filename
, status
? -1 : (int)filestats
->st_size
);
2465 * 'install_conf_file()' - Install a configuration file.
2468 static http_status_t
/* O - Status */
2469 install_conf_file(client_t
*con
) /* I - Connection */
2471 FILE *in
, /* Input file */
2472 *out
; /* Output file */
2473 char buffer
[1024]; /* Copy buffer */
2474 int bytes
; /* Number of bytes */
2475 char conffile
[1024], /* Configuration filename */
2476 newfile
[1024], /* New config filename */
2477 oldfile
[1024]; /* Old config filename */
2478 struct stat confinfo
; /* Config file info */
2482 * First construct the filenames...
2485 snprintf(conffile
, sizeof(conffile
), "%s%s", ServerRoot
, con
->uri
+ 11);
2486 snprintf(newfile
, sizeof(newfile
), "%s%s.N", ServerRoot
, con
->uri
+ 11);
2487 snprintf(oldfile
, sizeof(oldfile
), "%s%s.O", ServerRoot
, con
->uri
+ 11);
2489 LogMessage(L_INFO
, "Installing config file \"%s\"...", conffile
);
2492 * Get the owner, group, and permissions of the configuration file.
2493 * If it doesn't exist, assign it to the User and Group in the
2494 * cupsd.conf file with mode 0640 permissions.
2497 if (stat(conffile
, &confinfo
))
2499 confinfo
.st_uid
= User
;
2500 confinfo
.st_gid
= Group
;
2501 confinfo
.st_mode
= 0640;
2505 * Open the request file and new config file...
2508 if ((in
= fopen(con
->filename
, "rb")) == NULL
)
2510 LogMessage(L_ERROR
, "Unable to open request file \"%s\" - %s",
2511 con
->filename
, strerror(errno
));
2512 return (HTTP_SERVER_ERROR
);
2515 if ((out
= fopen(newfile
, "wb")) == NULL
)
2518 LogMessage(L_ERROR
, "Unable to open config file \"%s\" - %s",
2519 newfile
, strerror(errno
));
2520 return (HTTP_SERVER_ERROR
);
2523 fchmod(fileno(out
), confinfo
.st_mode
);
2524 fchown(fileno(out
), confinfo
.st_uid
, confinfo
.st_gid
);
2527 * Copy from the request to the new config file...
2530 while ((bytes
= fread(buffer
, 1, sizeof(buffer
), in
)) > 0)
2531 if (fwrite(buffer
, 1, bytes
, out
) < bytes
)
2533 LogMessage(L_ERROR
, "Unable to copy to config file \"%s\" - %s",
2534 newfile
, strerror(errno
));
2540 return (HTTP_SERVER_ERROR
);
2544 * Close the files...
2550 LogMessage(L_ERROR
, "Error file closing config file \"%s\" - %s",
2551 newfile
, strerror(errno
));
2555 return (HTTP_SERVER_ERROR
);
2559 * Remove the request file...
2562 unlink(con
->filename
);
2563 ClearString(&con
->filename
);
2566 * Unlink the old backup, rename the current config file to the backup
2567 * filename, and rename the new config file to the config file name...
2570 if (unlink(oldfile
))
2571 if (errno
!= ENOENT
)
2573 LogMessage(L_ERROR
, "Unable to remove backup config file \"%s\" - %s",
2574 oldfile
, strerror(errno
));
2578 return (HTTP_SERVER_ERROR
);
2581 if (rename(conffile
, oldfile
))
2582 if (errno
!= ENOENT
)
2584 LogMessage(L_ERROR
, "Unable to rename old config file \"%s\" - %s",
2585 conffile
, strerror(errno
));
2589 return (HTTP_SERVER_ERROR
);
2592 if (rename(newfile
, conffile
))
2594 LogMessage(L_ERROR
, "Unable to rename new config file \"%s\" - %s",
2595 newfile
, strerror(errno
));
2597 rename(oldfile
, conffile
);
2600 return (HTTP_SERVER_ERROR
);
2604 * If the cupsd.conf file was updated, set the NeedReload flag...
2607 if (strcmp(con
->uri
, "/admin/conf/cupsd.conf") == 0)
2611 * Return that the file was created successfully...
2614 return (HTTP_CREATED
);
2619 * 'pipe_command()' - Pipe the output of a command to the remote client.
2622 static int /* O - Process ID */
2623 pipe_command(client_t
*con
, /* I - Client connection */
2624 int infile
, /* I - Standard input for command */
2625 int *outfile
, /* O - Standard output for command */
2626 char *command
, /* I - Command to run */
2627 char *options
) /* I - Options for command */
2629 int i
; /* Looping var */
2630 int pid
; /* Process ID */
2631 char *commptr
; /* Command string pointer */
2632 char *uriptr
; /* URI string pointer */
2633 int fd
; /* Looping var */
2634 int fds
[2]; /* Pipe FDs */
2635 int argc
; /* Number of arguments */
2636 int envc
; /* Number of environment variables */
2637 char argbuf
[10240], /* Argument buffer */
2638 *argv
[100], /* Argument strings */
2639 *envp
[100]; /* Environment variables */
2640 char content_length
[1024], /* CONTENT_LENGTH environment variable */
2641 content_type
[1024], /* CONTENT_TYPE environment variable */
2642 cups_datadir
[1024], /* CUPS_DATADIR environment variable */
2643 cups_serverroot
[1024], /* CUPS_SERVERROOT environment variable */
2644 http_cookie
[1024], /* HTTP_COOKIE environment variable */
2645 http_user_agent
[1024], /* HTTP_USER_AGENT environment variable */
2646 ipp_port
[1024], /* IPP_PORT environment variable */
2647 lang
[1024], /* LANG environment variable */
2648 ld_library_path
[1024], /* LD_LIBRARY_PATH environment variable */
2649 nlspath
[1024], /* NLSPATH environment variable */
2650 query_string
[10240], /* QUERY_STRING env variable */
2651 remote_addr
[1024], /* REMOTE_ADDR environment variable */
2652 remote_host
[1024], /* REMOTE_HOST environment variable */
2653 remote_user
[1024], /* REMOTE_USER environment variable */
2654 script_name
[1024], /* SCRIPT_NAME environment variable */
2655 server_name
[1024], /* SERVER_NAME environment variable */
2656 server_port
[1024], /* SERVER_PORT environment variable */
2657 tmpdir
[1024]; /* TMPDIR environment variable */
2658 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
2659 struct sigaction action
; /* POSIX signal handler */
2660 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
2664 * Copy the command string...
2667 strlcpy(argbuf
, options
, sizeof(argbuf
));
2670 * Parse the string; arguments can be separated by + and are terminated
2676 for (commptr
= argbuf
, argc
= 1; *commptr
!= '\0' && argc
< 99; commptr
++)
2677 if (*commptr
== ' ' || *commptr
== '+')
2681 while (*commptr
== ' ')
2684 if (*commptr
!= '\0')
2686 argv
[argc
] = commptr
;
2692 else if (*commptr
== '%')
2694 if (commptr
[1] >= '0' && commptr
[1] <= '9')
2695 *commptr
= (commptr
[1] - '0') << 4;
2697 *commptr
= (tolower(commptr
[1]) - 'a' + 10) << 4;
2699 if (commptr
[2] >= '0' && commptr
[2] <= '9')
2700 *commptr
|= commptr
[2] - '0';
2702 *commptr
|= tolower(commptr
[2]) - 'a' + 10;
2704 strcpy(commptr
+ 1, commptr
+ 3);
2706 else if (*commptr
== '?')
2711 if (argv
[0][0] == '\0')
2712 argv
[0] = strrchr(command
, '/') + 1;
2715 * Setup the environment variables as needed...
2718 snprintf(lang
, sizeof(lang
), "LANG=%s",
2719 con
->language
? con
->language
->language
: "C");
2721 if (con
->http
.hostaddr
.addr
.sa_family
== AF_INET6
)
2723 sprintf(ipp_port
, "IPP_PORT=%d", ntohs(con
->http
.hostaddr
.ipv6
.sin6_port
));
2724 sprintf(server_port
, "SERVER_PORT=%d",
2725 ntohs(con
->http
.hostaddr
.ipv6
.sin6_port
));
2728 #endif /* AF_INET6 */
2730 sprintf(ipp_port
, "IPP_PORT=%d", ntohs(con
->http
.hostaddr
.ipv4
.sin_port
));
2731 sprintf(server_port
, "SERVER_PORT=%d",
2732 ntohs(con
->http
.hostaddr
.ipv4
.sin_port
));
2735 if (strcmp(con
->http
.hostname
, "localhost") == 0)
2736 strlcpy(server_name
, "SERVER_NAME=localhost", sizeof(server_name
));
2738 snprintf(server_name
, sizeof(server_name
), "SERVER_NAME=%s", ServerName
);
2739 snprintf(remote_host
, sizeof(remote_host
), "REMOTE_HOST=%s", con
->http
.hostname
);
2740 strcpy(remote_addr
, "REMOTE_ADDR=");
2741 httpAddrString(&(con
->http
.hostaddr
), remote_addr
+ 12,
2742 sizeof(remote_addr
) - 12);
2743 snprintf(remote_user
, sizeof(remote_user
), "REMOTE_USER=%s", con
->username
);
2744 snprintf(tmpdir
, sizeof(tmpdir
), "TMPDIR=%s", TempDir
);
2745 snprintf(cups_datadir
, sizeof(cups_datadir
), "CUPS_DATADIR=%s", DataDir
);
2746 snprintf(cups_serverroot
, sizeof(cups_serverroot
), "CUPS_SERVERROOT=%s", ServerRoot
);
2750 envp
[envc
++] = "PATH=/bin:/usr/bin";
2751 envp
[envc
++] = "SERVER_SOFTWARE=CUPS/1.1";
2752 envp
[envc
++] = "GATEWAY_INTERFACE=CGI/1.1";
2753 if (con
->http
.version
== HTTP_1_1
)
2754 envp
[envc
++] = "SERVER_PROTOCOL=HTTP/1.1";
2755 else if (con
->http
.version
== HTTP_1_0
)
2756 envp
[envc
++] = "SERVER_PROTOCOL=HTTP/1.0";
2758 envp
[envc
++] = "SERVER_PROTOCOL=HTTP/0.9";
2759 envp
[envc
++] = "REDIRECT_STATUS=1";
2760 envp
[envc
++] = ipp_port
;
2761 envp
[envc
++] = server_name
;
2762 envp
[envc
++] = server_port
;
2763 envp
[envc
++] = remote_addr
;
2764 envp
[envc
++] = remote_host
;
2765 envp
[envc
++] = remote_user
;
2766 envp
[envc
++] = lang
;
2768 envp
[envc
++] = tmpdir
;
2769 envp
[envc
++] = cups_datadir
;
2770 envp
[envc
++] = cups_serverroot
;
2772 if (getenv("LD_LIBRARY_PATH") != NULL
)
2774 snprintf(ld_library_path
, sizeof(ld_library_path
), "LD_LIBRARY_PATH=%s",
2775 getenv("LD_LIBRARY_PATH"));
2776 envp
[envc
++] = ld_library_path
;
2778 else if (getenv("DYLD_LIBRARY_PATH") != NULL
)
2780 snprintf(ld_library_path
, sizeof(ld_library_path
), "DYLD_LIBRARY_PATH=%s",
2781 getenv("DYLD_LIBRARY_PATH"));
2782 envp
[envc
++] = ld_library_path
;
2784 else if (getenv("SHLIB_PATH") != NULL
)
2786 snprintf(ld_library_path
, sizeof(ld_library_path
), "SHLIB_PATH=%s",
2787 getenv("SHLIB_PATH"));
2788 envp
[envc
++] = ld_library_path
;
2791 if (getenv("NLSPATH") != NULL
)
2793 snprintf(nlspath
, sizeof(nlspath
), "NLSPATH=%s", getenv("NLSPATH"));
2794 envp
[envc
++] = nlspath
;
2797 if (con
->http
.cookie
)
2799 snprintf(http_cookie
, sizeof(http_cookie
), "HTTP_COOKIE=%s",
2801 envp
[envc
++] = http_cookie
;
2804 if (con
->http
.fields
[HTTP_FIELD_USER_AGENT
][0])
2806 snprintf(http_user_agent
, sizeof(http_user_agent
), "HTTP_USER_AGENT=%s",
2807 con
->http
.fields
[HTTP_FIELD_USER_AGENT
]);
2808 envp
[envc
++] = http_user_agent
;
2811 snprintf(script_name
, sizeof(script_name
), "SCRIPT_NAME=%s", con
->uri
);
2812 if ((uriptr
= strchr(script_name
, '?')) != NULL
)
2814 envp
[envc
++] = script_name
;
2816 if (con
->operation
== HTTP_GET
)
2818 for (i
= 0; i
< argc
; i
++)
2819 LogMessage(L_DEBUG2
, "argv[%d] = \"%s\"", i
, argv
[i
]);
2820 envp
[envc
++] = "REQUEST_METHOD=GET";
2825 * Add GET form variables after ?...
2830 snprintf(query_string
, sizeof(query_string
), "QUERY_STRING=%s", commptr
);
2831 envp
[envc
++] = query_string
;
2836 sprintf(content_length
, "CONTENT_LENGTH=%d", con
->bytes
);
2837 snprintf(content_type
, sizeof(content_type
), "CONTENT_TYPE=%s",
2838 con
->http
.fields
[HTTP_FIELD_CONTENT_TYPE
]);
2840 envp
[envc
++] = "REQUEST_METHOD=POST";
2841 envp
[envc
++] = content_length
;
2842 envp
[envc
++] = content_type
;
2846 * Tell the CGI if we are using encryption...
2849 if (con
->http
.encryption
== HTTP_ENCRYPT_ALWAYS
)
2851 envp
[envc
++] = "HTTPS=ON";
2852 envp
[envc
++] = "CUPS_ENCRYPTION=Always";
2857 if (LogLevel
== L_DEBUG2
)
2859 for (i
= 0; i
< argc
; i
++)
2860 LogMessage(L_DEBUG2
, "argv[%d] = \"%s\"", i
, argv
[i
]);
2861 for (i
= 0; i
< envc
; i
++)
2862 LogMessage(L_DEBUG2
, "envp[%d] = \"%s\"", i
, envp
[i
]);
2866 * Create a pipe for the output...
2871 LogMessage(L_ERROR
, "Unable to create pipes for CGI %s - %s",
2872 argv
[0], strerror(errno
));
2877 * Block signals before forking...
2883 * Then execute the command...
2886 if ((pid
= fork()) == 0)
2889 * Child comes here... Close stdin if necessary and dup the pipe to stdout.
2895 * Running as root, so change to a non-priviledged user...
2906 * Reset group membership to just the main one we belong to.
2912 * Update stdin/stdout/stderr...
2918 if (dup(infile
) < 0)
2923 if (dup(fds
[1]) < 0)
2927 open("/dev/null", O_WRONLY
);
2930 * Close extra file descriptors...
2933 for (fd
= 3; fd
< MaxFDs
; fd
++)
2937 * Change umask to restrict permissions on created files...
2943 * Unblock signals before doing the exec...
2947 sigset(SIGTERM
, SIG_DFL
);
2948 sigset(SIGCHLD
, SIG_DFL
);
2949 #elif defined(HAVE_SIGACTION)
2950 memset(&action
, 0, sizeof(action
));
2952 sigemptyset(&action
.sa_mask
);
2953 action
.sa_handler
= SIG_DFL
;
2955 sigaction(SIGTERM
, &action
, NULL
);
2956 sigaction(SIGCHLD
, &action
, NULL
);
2958 signal(SIGTERM
, SIG_DFL
);
2959 signal(SIGCHLD
, SIG_DFL
);
2960 #endif /* HAVE_SIGSET */
2965 * Execute the pipe program; if an error occurs, exit with status 1...
2968 execve(command
, argv
, envp
);
2975 * Error - can't fork!
2978 LogMessage(L_ERROR
, "Unable to fork for CGI %s - %s", argv
[0],
2988 * Fork successful - return the PID...
2991 AddCert(pid
, con
->username
);
2993 LogMessage(L_DEBUG
, "CGI %s started - PID = %d", command
, pid
);
3005 #if defined(HAVE_CDSASSL)
3007 * 'CDSAReadFunc()' - Read function for CDSA decryption code.
3010 static OSStatus
/* O - -1 on error, 0 on success */
3011 CDSAReadFunc(SSLConnectionRef connection
, /* I - SSL/TLS connection */
3012 void *data
, /* I - Data buffer */
3013 size_t *dataLength
) /* IO - Number of bytes */
3015 ssize_t bytes
; /* Number of bytes read */
3018 bytes
= recv((int)connection
, data
, *dataLength
, 0);
3021 *dataLength
= bytes
;
3030 * 'CDSAWriteFunc()' - Write function for CDSA encryption code.
3033 static OSStatus
/* O - -1 on error, 0 on success */
3034 CDSAWriteFunc(SSLConnectionRef connection
, /* I - SSL/TLS connection */
3035 const void *data
, /* I - Data buffer */
3036 size_t *dataLength
) /* IO - Number of bytes */
3041 bytes
= write((int)connection
, data
, *dataLength
);
3044 *dataLength
= bytes
;
3050 #endif /* HAVE_CDSASSL */
3054 * End of "$Id: client.c,v 1.91.2.51 2003/03/13 06:53:46 mike Exp $".