2 * "$Id: client.c,v 1.91.2.43 2003/02/05 21:14:48 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 * Add the socket to the select() input mask.
300 fcntl(con
->http
.fd
, F_SETFD
, fcntl(con
->http
.fd
, F_GETFD
) | FD_CLOEXEC
);
302 LogMessage(L_DEBUG2
, "AcceptClient: Adding fd %d to InputSet...",
304 FD_SET(con
->http
.fd
, InputSet
);
309 * Temporarily suspend accept()'s until we lose a client...
312 if (NumClients
== MaxClients
)
317 * See if we are connecting on a secure port...
320 if (lis
->encryption
== HTTP_ENCRYPT_ALWAYS
)
323 * https connection; go secure...
326 con
->http
.encryption
= HTTP_ENCRYPT_ALWAYS
;
330 #endif /* HAVE_SSL */
335 * 'CloseAllClients()' - Close all remote clients immediately.
339 CloseAllClients(void)
341 while (NumClients
> 0)
342 CloseClient(Clients
);
347 * 'CloseClient()' - Close a remote client.
351 CloseClient(client_t
*con
) /* I - Client to close */
353 int status
; /* Exit status of pipe command */
354 #if defined(HAVE_LIBSSL)
355 SSL_CTX
*context
; /* Context for encryption */
356 SSL
*conn
; /* Connection for encryption */
357 unsigned long error
; /* Error code */
358 #elif defined(HAVE_GNUTLS)
359 http_tls_t
*conn
; /* TLS connection information */
360 int error
; /* Error code */
361 gnutls_certificate_server_credentials
*credentials
;
362 /* TLS credentials */
363 #endif /* HAVE_GNUTLS */
366 LogMessage(L_DEBUG
, "CloseClient() %d", con
->http
.fd
);
370 * Shutdown encryption as needed...
376 conn
= (SSL
*)(con
->http
.tls
);
377 context
= SSL_get_SSL_CTX(conn
);
379 switch (SSL_shutdown(conn
))
382 LogMessage(L_INFO
, "CloseClient: SSL shutdown successful!");
385 LogMessage(L_ERROR
, "CloseClient: Fatal error during SSL shutdown!");
387 while ((error
= ERR_get_error()) != 0)
388 LogMessage(L_ERROR
, "CloseClient: %s", ERR_error_string(error
, NULL
));
392 SSL_CTX_free(context
);
395 # elif defined(HAVE_GNUTLS)
396 conn
= (http_tls_t
*)(con
->http
.tls
);
397 credentials
= (gnutls_certificate_server_credentials
*)(conn
->credentials
);
399 error
= gnutls_bye(conn
->session
, GNUTLS_SHUT_WR
);
402 case GNUTLS_E_SUCCESS
:
403 LogMessage(L_INFO
, "CloseClient: SSL shutdown successful!");
406 LogMessage(L_ERROR
, "CloseClient: %s", gnutls_strerror(error
));
410 gnutls_deinit(conn
->session
);
411 gnutls_certificate_free_credentials(*credentials
);
415 # elif defined(HAVE_CDSASSL)
416 status
= SSLClose((SSLContextRef
)con
->http
.tls
);
417 SSLDisposeContext((SSLContextRef
)con
->http
.tls
);
418 # endif /* HAVE_LIBSSL */
420 con
->http
.tls
= NULL
;
422 #endif /* HAVE_SSL */
425 * Close the socket and clear the file from the input set for select()...
428 if (con
->http
.fd
>= 0)
430 LogMessage(L_DEBUG2
, "CloseClient: Removing fd %d from InputSet and OutputSet...",
433 FD_CLR(con
->http
.fd
, InputSet
);
434 FD_CLR(con
->http
.fd
, OutputSet
);
438 if (con
->pipe_pid
!= 0)
440 LogMessage(L_DEBUG2
, "CloseClient: Removing fd %d from InputSet...",
442 FD_CLR(con
->file
, InputSet
);
448 * Close the open data file...
453 kill(con
->pipe_pid
, SIGKILL
);
454 waitpid(con
->pipe_pid
, &status
, WNOHANG
);
457 LogMessage(L_DEBUG2
, "CloseClient() %d Closing data file %d.",
458 con
->http
.fd
, con
->file
);
459 LogMessage(L_DEBUG2
, "CloseClient() %d Removing fd %d from InputSet.",
460 con
->http
.fd
, con
->file
);
462 FD_CLR(con
->file
, InputSet
);
469 ippDelete(con
->request
);
475 ippDelete(con
->response
);
476 con
->response
= NULL
;
481 cupsLangFree(con
->language
);
482 con
->language
= NULL
;
486 * Re-enable new client connections if we are going back under the
490 if (NumClients
== MaxClients
)
494 * Compact the list of clients as necessary...
499 if (con
< (Clients
+ NumClients
))
500 memcpy(con
, con
+ 1, (Clients
+ NumClients
- con
) * sizeof(client_t
));
505 * 'EncryptClient()' - Enable encryption for the client...
508 int /* O - 1 on success, 0 on error */
509 EncryptClient(client_t
*con
) /* I - Client to encrypt */
511 #if defined HAVE_LIBSSL
512 SSL_CTX
*context
; /* Context for encryption */
513 SSL
*conn
; /* Connection for encryption */
514 unsigned long error
; /* Error code */
518 * Create the SSL context and accept the connection...
521 context
= SSL_CTX_new(SSLv23_server_method());
523 SSL_CTX_use_PrivateKey_file(context
, ServerKey
, SSL_FILETYPE_PEM
);
524 SSL_CTX_use_certificate_file(context
, ServerCertificate
, SSL_FILETYPE_PEM
);
526 conn
= SSL_new(context
);
528 SSL_set_fd(conn
, con
->http
.fd
);
529 if (SSL_accept(conn
) != 1)
531 while ((error
= ERR_get_error()) != 0)
532 LogMessage(L_ERROR
, "EncryptClient: %s", ERR_error_string(error
, NULL
));
534 SSL_CTX_free(context
);
539 con
->http
.tls
= conn
;
542 #elif defined(HAVE_GNUTLS)
543 http_tls_t
*conn
; /* TLS session object */
544 int error
; /* Error code */
545 gnutls_certificate_server_credentials
*credentials
;
546 /* TLS credentials */
549 * Create the SSL object and perform the SSL handshake...
552 conn
= (http_tls_t
*)malloc(sizeof(gnutls_session
));
557 credentials
= (gnutls_certificate_server_credentials
*)
558 malloc(sizeof(gnutls_certificate_server_credentials
));
559 if (credentials
== NULL
)
565 gnutls_certificate_allocate_credentials(credentials
);
566 gnutls_certificate_set_x509_key_file(*credentials
, ServerCertificate
,
567 ServerKey
, GNUTLS_X509_FMT_PEM
);
569 gnutls_init(&(conn
->session
), GNUTLS_SERVER
);
570 gnutls_set_default_priority(conn
->session
);
571 gnutls_credentials_set(conn
->session
, GNUTLS_CRD_CERTIFICATE
, *credentials
);
572 gnutls_transport_set_ptr(conn
->session
, con
->http
.fd
);
574 error
= gnutls_handshake(conn
->session
);
576 if (error
!= GNUTLS_E_SUCCESS
)
578 LogMessage(L_ERROR
, "EncryptClient: %s", gnutls_strerror(error
));
579 gnutls_deinit(conn
->session
);
580 gnutls_certificate_free_credentials(*credentials
);
586 LogMessage(L_DEBUG
, "EncryptClient() %d Connection now encrypted.",
589 conn
->credentials
= credentials
;
590 con
->http
.tls
= conn
;
593 #elif defined(HAVE_CDSASSL)
594 OSStatus error
; /* Error info */
595 SSLContextRef conn
; /* New connection */
596 SSLProtocol tryVersion
; /* Protocol version */
597 const char *hostName
; /* Local hostname */
598 int allowExpired
; /* Allow expired certificates? */
599 int allowAnyRoot
; /* Allow any root certificate? */
600 SSLProtocol
*negVersion
; /* Negotiated protocol version */
601 SSLCipherSuite
*negCipher
; /* Negotiated cypher */
602 CFArrayRef
*peerCerts
; /* Certificates */
606 error
= SSLNewContext(true, &conn
);
611 error
= SSLSetIOFuncs(conn
, CDSAReadFunc
, CDSAWriteFunc
);
614 error
= SSLSetProtocolVersion(conn
, kSSLProtocol3
);
617 error
= SSLSetConnection(conn
, (SSLConnectionRef
)con
->http
.fd
);
621 hostName
= ServerName
; /* MRS: ??? */
622 error
= SSLSetPeerDomainName(conn
, hostName
, strlen(hostName
) + 1);
625 /* have to do these options befor setting server certs */
626 if (!error
&& allowExpired
)
627 error
= SSLSetAllowsExpiredCerts(conn
, true);
629 if (!error
&& allowAnyRoot
)
630 error
= SSLSetAllowsAnyRoot(conn
, true);
632 if (!error
&& ServerCertificatesArray
!= NULL
)
633 error
= SSLSetCertificate(conn
, ServerCertificatesArray
);
636 * Perform SSL/TLS handshake
641 error
= SSLHandshake(conn
);
643 while (error
== errSSLWouldBlock
);
647 LogMessage(L_ERROR
, "EncryptClient: %d", error
);
649 con
->http
.error
= error
;
650 con
->http
.status
= HTTP_ERROR
;
653 SSLDisposeContext(conn
);
658 LogMessage(L_DEBUG
, "EncryptClient() %d Connection now encrypted.",
660 con
->http
.tls
= conn
;
665 #endif /* HAVE_GNUTLS */
670 * 'ReadClient()' - Read data from a client.
673 int /* O - 1 on success, 0 on error */
674 ReadClient(client_t
*con
) /* I - Client to read from */
676 char line
[32768], /* Line from client... */
677 operation
[64], /* Operation code from socket */
678 version
[64]; /* HTTP version number string */
679 int major
, minor
; /* HTTP version numbers */
680 http_status_t status
; /* Transfer status */
681 ipp_state_t ipp_state
; /* State of IPP transfer */
682 int bytes
; /* Number of bytes to POST */
683 char *filename
; /* Name of file for GET/HEAD */
684 char buf
[1024]; /* Buffer for real filename */
685 struct stat filestats
; /* File information */
686 mime_type_t
*type
; /* MIME type of file */
687 printer_t
*p
; /* Printer */
688 location_t
*best
; /* Best match for authentication */
689 static unsigned request_id
= 0;/* Request ID for temp files */
692 status
= HTTP_CONTINUE
;
694 LogMessage(L_DEBUG2
, "ReadClient() %d, used=%d", con
->http
.fd
,
703 switch (con
->http
.state
)
707 * See if we've received a request line...
710 if (httpGets(line
, sizeof(line
) - 1, HTTP(con
)) == NULL
)
717 * Ignore blank request lines...
724 * Clear other state variables...
727 httpClearFields(HTTP(con
));
729 con
->http
.activity
= time(NULL
);
730 con
->http
.version
= HTTP_1_0
;
731 con
->http
.keep_alive
= HTTP_KEEPALIVE_OFF
;
732 con
->http
.data_encoding
= HTTP_ENCODE_LENGTH
;
733 con
->http
.data_remaining
= 0;
734 con
->operation
= HTTP_WAITING
;
738 con
->command
[0] = '\0';
739 con
->username
[0] = '\0';
740 con
->password
[0] = '\0';
743 if (con
->language
!= NULL
)
745 cupsLangFree(con
->language
);
746 con
->language
= NULL
;
750 * Grab the request line...
753 switch (sscanf(line
, "%63s%1023s%63s", operation
, con
->uri
, version
))
756 LogMessage(L_ERROR
, "Bad request line \"%s\"!", line
);
757 SendError(con
, HTTP_BAD_REQUEST
);
761 con
->http
.version
= HTTP_0_9
;
764 if (sscanf(version
, "HTTP/%d.%d", &major
, &minor
) != 2)
766 LogMessage(L_ERROR
, "Bad request line \"%s\"!", line
);
767 SendError(con
, HTTP_BAD_REQUEST
);
774 con
->http
.version
= (http_version_t
)(major
* 100 + minor
);
775 if (con
->http
.version
== HTTP_1_1
&& KeepAlive
)
776 con
->http
.keep_alive
= HTTP_KEEPALIVE_ON
;
778 con
->http
.keep_alive
= HTTP_KEEPALIVE_OFF
;
782 SendError(con
, HTTP_NOT_SUPPORTED
);
790 * Handle full URLs in the request line...
793 if (con
->uri
[0] != '/' && strcmp(con
->uri
, "*"))
795 char method
[HTTP_MAX_URI
], /* Method/scheme */
796 userpass
[HTTP_MAX_URI
], /* Username:password */
797 hostname
[HTTP_MAX_URI
], /* Hostname */
798 resource
[HTTP_MAX_URI
]; /* Resource path */
799 int port
; /* Port number */
803 * Separate the URI into its components...
806 httpSeparate(con
->uri
, method
, userpass
, hostname
, &port
, resource
);
809 * Only allow URIs with the servername, localhost, or an IP
813 if (strcasecmp(hostname
, ServerName
) &&
814 strcasecmp(hostname
, "localhost") &&
815 !isdigit(hostname
[0]))
818 * Nope, we don't do proxies...
821 LogMessage(L_ERROR
, "Bad URI \"%s\" in request!", con
->uri
);
822 SendError(con
, HTTP_METHOD_NOT_ALLOWED
);
828 * Copy the resource portion back into the URI; both resource and
829 * con->uri are HTTP_MAX_URI bytes in size...
832 strcpy(con
->uri
, resource
);
836 * Process the request...
839 if (strcmp(operation
, "GET") == 0)
840 con
->http
.state
= HTTP_GET
;
841 else if (strcmp(operation
, "PUT") == 0)
842 con
->http
.state
= HTTP_PUT
;
843 else if (strcmp(operation
, "POST") == 0)
844 con
->http
.state
= HTTP_POST
;
845 else if (strcmp(operation
, "DELETE") == 0)
846 con
->http
.state
= HTTP_DELETE
;
847 else if (strcmp(operation
, "TRACE") == 0)
848 con
->http
.state
= HTTP_TRACE
;
849 else if (strcmp(operation
, "OPTIONS") == 0)
850 con
->http
.state
= HTTP_OPTIONS
;
851 else if (strcmp(operation
, "HEAD") == 0)
852 con
->http
.state
= HTTP_HEAD
;
855 LogMessage(L_ERROR
, "Bad operation \"%s\"!", operation
);
856 SendError(con
, HTTP_BAD_REQUEST
);
861 con
->start
= time(NULL
);
862 con
->operation
= con
->http
.state
;
864 LogMessage(L_DEBUG
, "ReadClient() %d %s %s HTTP/%d.%d", con
->http
.fd
,
866 con
->http
.version
/ 100, con
->http
.version
% 100);
868 con
->http
.status
= HTTP_OK
;
878 * Parse incoming parameters until the status changes...
881 status
= httpUpdate(HTTP(con
));
883 if (status
!= HTTP_OK
&& status
!= HTTP_CONTINUE
)
885 SendError(con
, HTTP_BAD_REQUEST
);
892 break; /* Anti-compiler-warning-code */
896 * Handle new transfers...
899 if (status
== HTTP_OK
)
901 con
->language
= cupsLangGet(con
->http
.fields
[HTTP_FIELD_ACCEPT_LANGUAGE
]);
905 if (strncmp(con
->http
.fields
[HTTP_FIELD_CONNECTION
], "Keep-Alive", 10) == 0 &&
907 con
->http
.keep_alive
= HTTP_KEEPALIVE_ON
;
909 if (con
->http
.fields
[HTTP_FIELD_HOST
][0] == '\0' &&
910 con
->http
.version
>= HTTP_1_1
)
913 * HTTP/1.1 and higher require the "Host:" field...
916 if (!SendError(con
, HTTP_BAD_REQUEST
))
922 else if (con
->operation
== HTTP_OPTIONS
)
925 * Do OPTIONS command...
928 if ((best
= FindBest(con
->uri
, con
->http
.state
)) != NULL
&&
929 best
->type
!= AUTH_NONE
)
931 if (!SendHeader(con
, HTTP_UNAUTHORIZED
, NULL
))
938 if (strcasecmp(con
->http
.fields
[HTTP_FIELD_CONNECTION
], "Upgrade") == 0 &&
939 con
->http
.tls
== NULL
)
943 * Do encryption stuff...
946 if (!SendHeader(con
, HTTP_SWITCHING_PROTOCOLS
, NULL
))
952 httpPrintf(HTTP(con
), "Connection: Upgrade\r\n");
953 httpPrintf(HTTP(con
), "Upgrade: TLS/1.0,HTTP/1.1\r\n");
954 httpPrintf(HTTP(con
), "Content-Length: 0\r\n");
955 httpPrintf(HTTP(con
), "\r\n");
959 if (!SendError(con
, HTTP_NOT_IMPLEMENTED
))
964 #endif /* HAVE_SSL */
967 if (con
->http
.expect
)
969 /**** TODO: send expected header ****/
972 if (!SendHeader(con
, HTTP_OK
, NULL
))
978 httpPrintf(HTTP(con
), "Allow: GET, HEAD, OPTIONS, POST, PUT\r\n");
979 httpPrintf(HTTP(con
), "Content-Length: 0\r\n");
980 httpPrintf(HTTP(con
), "\r\n");
982 else if (strstr(con
->uri
, "..") != NULL
)
985 * Protect against malicious users!
988 if (!SendError(con
, HTTP_FORBIDDEN
))
996 if (strcasecmp(con
->http
.fields
[HTTP_FIELD_CONNECTION
], "Upgrade") == 0 &&
997 con
->http
.tls
== NULL
)
1001 * Do encryption stuff...
1004 if (!SendHeader(con
, HTTP_SWITCHING_PROTOCOLS
, NULL
))
1010 httpPrintf(HTTP(con
), "Connection: Upgrade\r\n");
1011 httpPrintf(HTTP(con
), "Upgrade: TLS/1.0,HTTP/1.1\r\n");
1012 httpPrintf(HTTP(con
), "Content-Length: 0\r\n");
1013 httpPrintf(HTTP(con
), "\r\n");
1017 status
= IsAuthorized(con
);
1019 if (!SendError(con
, HTTP_NOT_IMPLEMENTED
))
1024 #endif /* HAVE_SSL */
1027 if (status
!= HTTP_OK
)
1029 LogMessage(L_DEBUG2
, "ReadClient: Unauthorized request for %s...\n",
1031 SendError(con
, status
);
1032 ShutdownClient(con
);
1036 if (con
->http
.expect
)
1038 /**** TODO: send expected header ****/
1041 switch (con
->http
.state
)
1043 case HTTP_GET_SEND
:
1044 if (strncmp(con
->uri
, "/printers/", 10) == 0 &&
1045 strcmp(con
->uri
+ strlen(con
->uri
) - 4, ".ppd") == 0)
1048 * Send PPD file - get the real printer name since printer
1049 * names are not case sensitive but filenames can be...
1052 con
->uri
[strlen(con
->uri
) - 4] = '\0'; /* Drop ".ppd" */
1054 if ((p
= FindPrinter(con
->uri
+ 10)) != NULL
)
1055 snprintf(con
->uri
, sizeof(con
->uri
), "/ppd/%s.ppd", p
->name
);
1058 if (!SendError(con
, HTTP_NOT_FOUND
))
1068 if ((strncmp(con
->uri
, "/admin", 6) == 0 &&
1069 strncmp(con
->uri
, "/admin/conf/", 12) != 0) ||
1070 strncmp(con
->uri
, "/printers", 9) == 0 ||
1071 strncmp(con
->uri
, "/classes", 8) == 0 ||
1072 strncmp(con
->uri
, "/jobs", 5) == 0)
1075 * Send CGI output...
1078 if (strncmp(con
->uri
, "/admin", 6) == 0)
1080 snprintf(con
->command
, sizeof(con
->command
),
1081 "%s/cgi-bin/admin.cgi", ServerBin
);
1082 con
->options
= con
->uri
+ 6;
1084 else if (strncmp(con
->uri
, "/printers", 9) == 0)
1086 snprintf(con
->command
, sizeof(con
->command
),
1087 "%s/cgi-bin/printers.cgi", ServerBin
);
1088 con
->options
= con
->uri
+ 9;
1090 else if (strncmp(con
->uri
, "/classes", 8) == 0)
1092 snprintf(con
->command
, sizeof(con
->command
),
1093 "%s/cgi-bin/classes.cgi", ServerBin
);
1094 con
->options
= con
->uri
+ 8;
1098 snprintf(con
->command
, sizeof(con
->command
),
1099 "%s/cgi-bin/jobs.cgi", ServerBin
);
1100 con
->options
= con
->uri
+ 5;
1103 if (con
->options
[0] == '/')
1106 if (!SendCommand(con
, con
->command
, con
->options
))
1108 if (!SendError(con
, HTTP_NOT_FOUND
))
1115 LogRequest(con
, HTTP_OK
);
1117 if (con
->http
.version
<= HTTP_1_0
)
1118 con
->http
.keep_alive
= HTTP_KEEPALIVE_OFF
;
1120 else if (strncmp(con
->uri
, "/admin/conf/", 12) == 0 &&
1121 (strchr(con
->uri
+ 12, '/') != NULL
||
1122 strlen(con
->uri
) == 12))
1125 * GET can only be done to configuration files under
1129 if (!SendError(con
, HTTP_FORBIDDEN
))
1143 if ((filename
= get_file(con
, &filestats
, buf
,
1144 sizeof(buf
))) == NULL
)
1146 if (!SendError(con
, HTTP_NOT_FOUND
))
1152 else if (!check_if_modified(con
, &filestats
))
1154 if (!SendError(con
, HTTP_NOT_MODIFIED
))
1162 type
= mimeFileType(MimeDatabase
, filename
);
1164 strcpy(line
, "text/plain");
1166 snprintf(line
, sizeof(line
), "%s/%s", type
->super
, type
->type
);
1168 if (!SendFile(con
, HTTP_OK
, filename
, line
, &filestats
))
1177 case HTTP_POST_RECV
:
1179 * See if the POST request includes a Content-Length field, and if
1180 * so check the length against any limits that are set...
1183 LogMessage(L_DEBUG2
, "POST %s", con
->uri
);
1184 LogMessage(L_DEBUG2
, "CONTENT_TYPE = %s", con
->http
.fields
[HTTP_FIELD_CONTENT_TYPE
]);
1186 if (con
->http
.fields
[HTTP_FIELD_CONTENT_LENGTH
][0] &&
1187 atoi(con
->http
.fields
[HTTP_FIELD_CONTENT_LENGTH
]) > MaxRequestSize
&&
1191 * Request too large...
1194 if (!SendError(con
, HTTP_REQUEST_TOO_LARGE
))
1202 else if (atoi(con
->http
.fields
[HTTP_FIELD_CONTENT_LENGTH
]) < 0)
1205 * Negative content lengths are invalid!
1208 if (!SendError(con
, HTTP_BAD_REQUEST
))
1218 * See what kind of POST request this is; for IPP requests the
1219 * content-type field will be "application/ipp"...
1222 if (strcmp(con
->http
.fields
[HTTP_FIELD_CONTENT_TYPE
], "application/ipp") == 0)
1223 con
->request
= ippNew();
1224 else if ((strncmp(con
->uri
, "/admin", 6) == 0 &&
1225 strncmp(con
->uri
, "/admin/conf/", 12) != 0) ||
1226 strncmp(con
->uri
, "/printers", 9) == 0 ||
1227 strncmp(con
->uri
, "/classes", 8) == 0 ||
1228 strncmp(con
->uri
, "/jobs", 5) == 0)
1234 if (strncmp(con
->uri
, "/admin", 6) == 0)
1236 snprintf(con
->command
, sizeof(con
->command
),
1237 "%s/cgi-bin/admin.cgi", ServerBin
);
1238 con
->options
= con
->uri
+ 6;
1240 else if (strncmp(con
->uri
, "/printers", 9) == 0)
1242 snprintf(con
->command
, sizeof(con
->command
),
1243 "%s/cgi-bin/printers.cgi", ServerBin
);
1244 con
->options
= con
->uri
+ 9;
1246 else if (strncmp(con
->uri
, "/classes", 8) == 0)
1248 snprintf(con
->command
, sizeof(con
->command
),
1249 "%s/cgi-bin/classes.cgi", ServerBin
);
1250 con
->options
= con
->uri
+ 8;
1254 snprintf(con
->command
, sizeof(con
->command
),
1255 "%s/cgi-bin/jobs.cgi", ServerBin
);
1256 con
->options
= con
->uri
+ 5;
1259 if (con
->options
[0] == '/')
1262 LogMessage(L_DEBUG2
, "ReadClient() %d command=\"%s\", options = \"%s\"",
1263 con
->http
.fd
, con
->command
, con
->options
);
1265 if (con
->http
.version
<= HTTP_1_0
)
1266 con
->http
.keep_alive
= HTTP_KEEPALIVE_OFF
;
1268 else if (!SendError(con
, HTTP_UNAUTHORIZED
))
1275 case HTTP_PUT_RECV
:
1277 * Validate the resource name...
1280 if (strncmp(con
->uri
, "/admin/conf/", 12) != 0 ||
1281 strchr(con
->uri
+ 12, '/') != NULL
||
1282 strlen(con
->uri
) == 12)
1285 * PUT can only be done to configuration files under
1289 if (!SendError(con
, HTTP_FORBIDDEN
))
1299 * See if the PUT request includes a Content-Length field, and if
1300 * so check the length against any limits that are set...
1303 LogMessage(L_DEBUG2
, "PUT %s", con
->uri
);
1304 LogMessage(L_DEBUG2
, "CONTENT_TYPE = %s", con
->http
.fields
[HTTP_FIELD_CONTENT_TYPE
]);
1306 if (con
->http
.fields
[HTTP_FIELD_CONTENT_LENGTH
][0] &&
1307 atoi(con
->http
.fields
[HTTP_FIELD_CONTENT_LENGTH
]) > MaxRequestSize
&&
1311 * Request too large...
1314 if (!SendError(con
, HTTP_REQUEST_TOO_LARGE
))
1324 * Open a temporary file to hold the request...
1327 snprintf(con
->filename
, sizeof(con
->filename
), "%s/%08x",
1328 RequestRoot
, request_id
++);
1329 con
->file
= open(con
->filename
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0640);
1330 fchmod(con
->file
, 0640);
1331 fchown(con
->file
, User
, Group
);
1333 LogMessage(L_DEBUG2
, "ReadClient() %d REQUEST %s=%d", con
->http
.fd
,
1334 con
->filename
, con
->file
);
1338 if (!SendError(con
, HTTP_REQUEST_TOO_LARGE
))
1348 SendError(con
, HTTP_NOT_IMPLEMENTED
);
1349 ShutdownClient(con
);
1353 if (strncmp(con
->uri
, "/printers/", 10) == 0 &&
1354 strcmp(con
->uri
+ strlen(con
->uri
) - 4, ".ppd") == 0)
1357 * Send PPD file - get the real printer name since printer
1358 * names are not case sensitive but filenames can be...
1361 con
->uri
[strlen(con
->uri
) - 4] = '\0'; /* Drop ".ppd" */
1363 if ((p
= FindPrinter(con
->uri
+ 10)) != NULL
)
1364 snprintf(con
->uri
, sizeof(con
->uri
), "/ppd/%s.ppd", p
->name
);
1367 if (!SendError(con
, HTTP_NOT_FOUND
))
1377 if ((strncmp(con
->uri
, "/admin/", 7) == 0 &&
1378 strncmp(con
->uri
, "/admin/conf/", 12) != 0) ||
1379 strncmp(con
->uri
, "/printers/", 10) == 0 ||
1380 strncmp(con
->uri
, "/classes/", 9) == 0 ||
1381 strncmp(con
->uri
, "/jobs/", 6) == 0)
1387 if (!SendHeader(con
, HTTP_OK
, "text/html"))
1393 if (httpPrintf(HTTP(con
), "\r\n") < 0)
1399 LogRequest(con
, HTTP_OK
);
1401 else if (strncmp(con
->uri
, "/admin/conf/", 12) == 0 &&
1402 (strchr(con
->uri
+ 12, '/') != NULL
||
1403 strlen(con
->uri
) == 12))
1406 * HEAD can only be done to configuration files under
1410 if (!SendError(con
, HTTP_FORBIDDEN
))
1418 else if ((filename
= get_file(con
, &filestats
, buf
,
1419 sizeof(buf
))) == NULL
)
1421 if (!SendHeader(con
, HTTP_NOT_FOUND
, "text/html"))
1427 LogRequest(con
, HTTP_NOT_FOUND
);
1429 else if (!check_if_modified(con
, &filestats
))
1431 if (!SendError(con
, HTTP_NOT_MODIFIED
))
1437 LogRequest(con
, HTTP_NOT_MODIFIED
);
1445 type
= mimeFileType(MimeDatabase
, filename
);
1447 strcpy(line
, "text/plain");
1449 snprintf(line
, sizeof(line
), "%s/%s", type
->super
, type
->type
);
1451 if (!SendHeader(con
, HTTP_OK
, line
))
1457 if (httpPrintf(HTTP(con
), "Last-Modified: %s\r\n",
1458 httpGetDateString(filestats
.st_mtime
)) < 0)
1464 if (httpPrintf(HTTP(con
), "Content-Length: %lu\r\n",
1465 (unsigned long)filestats
.st_size
) < 0)
1471 LogRequest(con
, HTTP_OK
);
1474 if (httpPrintf(HTTP(con
), "\r\n") < 0)
1480 con
->http
.state
= HTTP_WAITING
;
1484 break; /* Anti-compiler-warning-code */
1490 * Handle any incoming data...
1493 switch (con
->http
.state
)
1495 case HTTP_PUT_RECV
:
1496 LogMessage(L_DEBUG2
, "ReadClient() %d con->data_encoding = %s, con->data_remaining = %d, con->file = %d",
1498 con
->http
.data_encoding
== HTTP_ENCODE_CHUNKED
? "chunked" : "length",
1499 con
->http
.data_remaining
, con
->file
);
1501 if ((bytes
= httpRead(HTTP(con
), line
, sizeof(line
))) < 0)
1508 con
->bytes
+= bytes
;
1510 LogMessage(L_DEBUG2
, "ReadClient() %d writing %d bytes to %d",
1511 con
->http
.fd
, bytes
, con
->file
);
1513 if (write(con
->file
, line
, bytes
) < bytes
)
1515 LogMessage(L_ERROR
, "ReadClient: Unable to write %d bytes to %s: %s",
1516 bytes
, con
->filename
, strerror(errno
));
1518 LogMessage(L_DEBUG2
, "ReadClient: Closing data file %d...",
1523 unlink(con
->filename
);
1524 con
->filename
[0] = '\0';
1526 if (!SendError(con
, HTTP_REQUEST_TOO_LARGE
))
1534 if (con
->http
.state
== HTTP_WAITING
)
1537 * End of file, see how big it is...
1540 fstat(con
->file
, &filestats
);
1542 LogMessage(L_DEBUG2
, "ReadClient() %d Closing data file %d, size = %d.",
1543 con
->http
.fd
, con
->file
, (int)filestats
.st_size
);
1548 if (filestats
.st_size
> MaxRequestSize
&&
1552 * Request is too big; remove it and send an error...
1555 LogMessage(L_DEBUG2
, "ReadClient() %d Removing temp file %s",
1556 con
->http
.fd
, con
->filename
);
1557 unlink(con
->filename
);
1558 con
->filename
[0] = '\0';
1560 if (!SendError(con
, HTTP_REQUEST_TOO_LARGE
))
1568 * Install the configuration file...
1571 status
= install_conf_file(con
);
1574 * Return the status to the client...
1577 if (!SendError(con
, status
))
1585 case HTTP_POST_RECV
:
1586 LogMessage(L_DEBUG2
, "ReadClient() %d con->data_encoding = %s, con->data_remaining = %d, con->file = %d",
1588 con
->http
.data_encoding
== HTTP_ENCODE_CHUNKED
? "chunked" : "length",
1589 con
->http
.data_remaining
, con
->file
);
1591 if (con
->request
!= NULL
)
1594 * Grab any request data from the connection...
1597 if ((ipp_state
= ippRead(&(con
->http
), con
->request
)) == IPP_ERROR
)
1599 LogMessage(L_ERROR
, "ReadClient() %d IPP Read Error!",
1602 if (!SendError(con
, HTTP_BAD_REQUEST
))
1608 ShutdownClient(con
);
1611 else if (ipp_state
!= IPP_DATA
)
1614 con
->bytes
+= ippLength(con
->request
);
1617 if (con
->file
== 0 && con
->http
.state
!= HTTP_POST_SEND
)
1620 * Create a file as needed for the request data...
1623 snprintf(con
->filename
, sizeof(con
->filename
), "%s/%08x",
1624 RequestRoot
, request_id
++);
1625 con
->file
= open(con
->filename
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0640);
1626 fchmod(con
->file
, 0640);
1627 fchown(con
->file
, User
, Group
);
1629 LogMessage(L_DEBUG2
, "ReadClient() %d REQUEST %s=%d", con
->http
.fd
,
1630 con
->filename
, con
->file
);
1634 if (!SendError(con
, HTTP_REQUEST_TOO_LARGE
))
1642 if (con
->http
.state
!= HTTP_POST_SEND
)
1644 if ((bytes
= httpRead(HTTP(con
), line
, sizeof(line
))) < 0)
1651 con
->bytes
+= bytes
;
1653 LogMessage(L_DEBUG2
, "ReadClient() %d writing %d bytes to %d",
1654 con
->http
.fd
, bytes
, con
->file
);
1656 if (write(con
->file
, line
, bytes
) < bytes
)
1658 LogMessage(L_ERROR
, "ReadClient: Unable to write %d bytes to %s: %s",
1659 bytes
, con
->filename
, strerror(errno
));
1661 LogMessage(L_DEBUG2
, "ReadClient: Closing file %d...",
1666 unlink(con
->filename
);
1667 con
->filename
[0] = '\0';
1669 if (!SendError(con
, HTTP_REQUEST_TOO_LARGE
))
1676 else if (con
->http
.state
!= HTTP_POST_SEND
)
1683 if (con
->http
.state
== HTTP_POST_SEND
)
1687 fstat(con
->file
, &filestats
);
1689 LogMessage(L_DEBUG2
, "ReadClient() %d Closing data file %d, size = %d.",
1690 con
->http
.fd
, con
->file
, (int)filestats
.st_size
);
1695 if (filestats
.st_size
> MaxRequestSize
&&
1699 * Request is too big; remove it and send an error...
1702 LogMessage(L_DEBUG2
, "ReadClient() %d Removing temp file %s",
1703 con
->http
.fd
, con
->filename
);
1704 unlink(con
->filename
);
1705 con
->filename
[0] = '\0';
1710 * Delete any IPP request data...
1713 ippDelete(con
->request
);
1714 con
->request
= NULL
;
1717 if (!SendError(con
, HTTP_REQUEST_TOO_LARGE
))
1724 if (con
->command
[0])
1726 if (!SendCommand(con
, con
->command
, con
->options
))
1728 if (!SendError(con
, HTTP_NOT_FOUND
))
1735 LogRequest(con
, HTTP_OK
);
1740 return (ProcessIPPRequest(con
));
1745 break; /* Anti-compiler-warning-code */
1748 if (!con
->http
.keep_alive
&& con
->http
.state
== HTTP_WAITING
)
1759 * 'SendCommand()' - Send output from a command via HTTP.
1763 SendCommand(client_t
*con
,
1770 if (con
->filename
[0])
1771 fd
= open(con
->filename
, O_RDONLY
);
1773 fd
= open("/dev/null", O_RDONLY
);
1775 con
->pipe_pid
= pipe_command(con
, fd
, &(con
->file
), command
, options
);
1779 LogMessage(L_INFO
, "Started \"%s\" (pid=%d)", command
, con
->pipe_pid
);
1781 LogMessage(L_DEBUG
, "SendCommand() %d file=%d", con
->http
.fd
, con
->file
);
1783 if (con
->pipe_pid
== 0)
1786 fcntl(con
->file
, F_SETFD
, fcntl(con
->file
, F_GETFD
) | FD_CLOEXEC
);
1788 LogMessage(L_DEBUG2
, "SendCommand: Adding fd %d to InputSet...", con
->file
);
1789 LogMessage(L_DEBUG2
, "SendCommand: Adding fd %d to OutputSet...",
1792 FD_SET(con
->file
, InputSet
);
1793 FD_SET(con
->http
.fd
, OutputSet
);
1795 if (!SendHeader(con
, HTTP_OK
, NULL
))
1798 if (con
->http
.version
== HTTP_1_1
)
1800 con
->http
.data_encoding
= HTTP_ENCODE_CHUNKED
;
1802 if (httpPrintf(HTTP(con
), "Transfer-Encoding: chunked\r\n") < 0)
1806 con
->got_fields
= 0;
1814 * 'SendError()' - Send an error message via HTTP.
1817 int /* O - 1 if successful, 0 otherwise */
1818 SendError(client_t
*con
, /* I - Connection */
1819 http_status_t code
) /* I - Error code */
1821 char message
[1024]; /* Message for user */
1825 * Put the request in the access_log file...
1828 if (con
->operation
> HTTP_WAITING
)
1829 LogRequest(con
, code
);
1831 LogMessage(L_DEBUG
, "SendError() %d code=%d", con
->http
.fd
, code
);
1834 * To work around bugs in some proxies, don't use Keep-Alive for some
1838 if (code
>= HTTP_BAD_REQUEST
)
1839 con
->http
.keep_alive
= HTTP_KEEPALIVE_OFF
;
1842 * Send an error message back to the client. If the error code is a
1843 * 400 or 500 series, make sure the message contains some text, too!
1846 if (!SendHeader(con
, code
, NULL
))
1850 if (code
== HTTP_UPGRADE_REQUIRED
)
1851 if (httpPrintf(HTTP(con
), "Connection: Upgrade\r\n") < 0)
1854 if (httpPrintf(HTTP(con
), "Upgrade: TLS/1.0,HTTP/1.1\r\n") < 0)
1856 #endif /* HAVE_SSL */
1858 if ((con
->http
.version
>= HTTP_1_1
&& !con
->http
.keep_alive
) ||
1859 (code
>= HTTP_BAD_REQUEST
&& code
!= HTTP_UPGRADE_REQUIRED
))
1861 if (httpPrintf(HTTP(con
), "Connection: close\r\n") < 0)
1865 if (code
>= HTTP_BAD_REQUEST
)
1868 * Send a human-readable error message.
1871 snprintf(message
, sizeof(message
),
1872 "<HTML><HEAD><TITLE>%d %s</TITLE></HEAD>"
1873 "<BODY><H1>%s</H1>%s</BODY></HTML>\n",
1874 code
, httpStatus(code
), httpStatus(code
),
1875 con
->language
? con
->language
->messages
[code
] :
1878 if (httpPrintf(HTTP(con
), "Content-Type: text/html\r\n") < 0)
1880 if (httpPrintf(HTTP(con
), "Content-Length: %d\r\n",
1881 (int)strlen(message
)) < 0)
1883 if (httpPrintf(HTTP(con
), "\r\n") < 0)
1885 if (httpPrintf(HTTP(con
), "%s", message
) < 0)
1888 else if (httpPrintf(HTTP(con
), "\r\n") < 0)
1891 con
->http
.state
= HTTP_WAITING
;
1898 * 'SendFile()' - Send a file via HTTP.
1902 SendFile(client_t
*con
,
1906 struct stat
*filestats
)
1908 con
->file
= open(filename
, O_RDONLY
);
1910 LogMessage(L_DEBUG
, "SendFile() %d file=%d", con
->http
.fd
, con
->file
);
1915 fcntl(con
->file
, F_SETFD
, fcntl(con
->file
, F_GETFD
) | FD_CLOEXEC
);
1919 if (!SendHeader(con
, code
, type
))
1922 if (httpPrintf(HTTP(con
), "Last-Modified: %s\r\n", httpGetDateString(filestats
->st_mtime
)) < 0)
1924 if (httpPrintf(HTTP(con
), "Content-Length: %lu\r\n",
1925 (unsigned long)filestats
->st_size
) < 0)
1927 if (httpPrintf(HTTP(con
), "\r\n") < 0)
1930 LogMessage(L_DEBUG2
, "SendFile: Adding fd %d to OutputSet...", con
->http
.fd
);
1932 FD_SET(con
->http
.fd
, OutputSet
);
1939 * 'SendHeader()' - Send an HTTP request.
1942 int /* O - 1 on success, 0 on failure */
1943 SendHeader(client_t
*con
, /* I - Client to send to */
1944 http_status_t code
, /* I - HTTP status code */
1945 char *type
) /* I - MIME type of document */
1947 location_t
*loc
; /* Authentication location */
1950 if (httpPrintf(HTTP(con
), "HTTP/%d.%d %d %s\r\n", con
->http
.version
/ 100,
1951 con
->http
.version
% 100, code
, httpStatus(code
)) < 0)
1953 if (httpPrintf(HTTP(con
), "Date: %s\r\n", httpGetDateString(time(NULL
))) < 0)
1955 if (httpPrintf(HTTP(con
), "Server: CUPS/1.1\r\n") < 0)
1957 if (con
->http
.keep_alive
&& con
->http
.version
>= HTTP_1_0
)
1959 if (httpPrintf(HTTP(con
), "Connection: Keep-Alive\r\n") < 0)
1961 if (httpPrintf(HTTP(con
), "Keep-Alive: timeout=%d\r\n", KeepAliveTimeout
) < 0)
1964 if (code
== HTTP_METHOD_NOT_ALLOWED
)
1965 if (httpPrintf(HTTP(con
), "Allow: GET, HEAD, OPTIONS, POST\r\n") < 0)
1968 if (code
== HTTP_UNAUTHORIZED
)
1971 * This already succeeded in IsAuthorized...
1974 loc
= FindBest(con
->uri
, con
->http
.state
);
1976 if (loc
->type
!= AUTH_DIGEST
)
1978 if (httpPrintf(HTTP(con
), "WWW-Authenticate: Basic realm=\"CUPS\"\r\n") < 0)
1983 if (httpPrintf(HTTP(con
), "WWW-Authenticate: Digest realm=\"CUPS\" "
1984 "nonce=\"%s\"\r\n", con
->http
.hostname
) < 0)
1988 if (con
->language
!= NULL
)
1990 if (httpPrintf(HTTP(con
), "Content-Language: %s\r\n",
1991 con
->language
->language
) < 0)
1995 if (httpPrintf(HTTP(con
), "Content-Type: %s; charset=%s\r\n", type
,
1996 cupsLangEncoding(con
->language
)) < 0)
1999 else if (type
!= NULL
)
2000 if (httpPrintf(HTTP(con
), "Content-Type: %s\r\n", type
) < 0)
2008 * 'ShutdownClient()' - Shutdown the receiving end of a connection.
2012 ShutdownClient(client_t
*con
) /* I - Client connection */
2015 * Shutdown the receiving end of the socket, since the client
2016 * still needs to read the error message...
2019 shutdown(con
->http
.fd
, 0);
2023 * Update the activity time so that we timeout after 30 seconds rather
2024 * then the current Timeout setting (300 by default). This prevents
2025 * some DoS situations...
2028 con
->http
.activity
= time(NULL
) - Timeout
+ 30;
2030 LogMessage(L_DEBUG2
, "ShutdownClient: Removing fd %d from InputSet...",
2033 FD_CLR(con
->http
.fd
, InputSet
);
2038 * 'WriteClient()' - Write data to a client as needed.
2041 int /* O - 1 if success, 0 if fail */
2042 WriteClient(client_t
*con
) /* I - Client connection */
2044 int bytes
; /* Number of bytes written */
2045 char buf
[HTTP_MAX_BUFFER
+ 1];/* Data buffer */
2046 char *bufptr
; /* Pointer into buffer */
2047 ipp_state_t ipp_state
; /* IPP state value */
2050 if (con
->http
.state
!= HTTP_GET_SEND
&&
2051 con
->http
.state
!= HTTP_POST_SEND
)
2054 if (con
->response
!= NULL
)
2056 ipp_state
= ippWrite(&(con
->http
), con
->response
);
2057 bytes
= ipp_state
!= IPP_ERROR
&& ipp_state
!= IPP_DATA
;
2059 else if ((bytes
= read(con
->file
, buf
, HTTP_MAX_BUFFER
)) > 0)
2061 if (con
->pipe_pid
&& !con
->got_fields
)
2064 * Inspect the data for Content-Type and other fields.
2069 for (bufptr
= buf
; !con
->got_fields
&& *bufptr
; bufptr
++)
2070 if (*bufptr
== '\n')
2073 * Send line to client...
2076 if (bufptr
> buf
&& bufptr
[-1] == '\r')
2080 httpPrintf(HTTP(con
), "%s\r\n", buf
);
2081 LogMessage(L_DEBUG2
, "WriteClient() %d %s", con
->http
.fd
, buf
);
2087 bytes
-= (bufptr
- buf
);
2088 memcpy(buf
, bufptr
, bytes
+ 1);
2092 * See if the line was empty...
2095 if (con
->field_col
== 0)
2096 con
->got_fields
= 1;
2100 else if (*bufptr
!= '\r')
2103 if (bytes
> 0 && !con
->got_fields
)
2106 * Remaining text needs to go out...
2109 httpPrintf(HTTP(con
), "%s", buf
);
2111 con
->http
.activity
= time(NULL
);
2114 else if (bytes
== 0)
2116 con
->http
.activity
= time(NULL
);
2121 if (httpWrite(HTTP(con
), buf
, bytes
) < 0)
2127 con
->bytes
+= bytes
;
2132 LogRequest(con
, HTTP_OK
);
2134 if (con
->http
.data_encoding
== HTTP_ENCODE_CHUNKED
)
2136 if (httpPrintf(HTTP(con
), "0\r\n\r\n") < 0)
2143 con
->http
.state
= HTTP_WAITING
;
2145 LogMessage(L_DEBUG2
, "WriteClient() Removing fd %d from OutputSet...",
2148 FD_CLR(con
->http
.fd
, OutputSet
);
2152 LogMessage(L_DEBUG2
, "WriteClient() Removing fd %d from InputSet...",
2154 FD_CLR(con
->file
, InputSet
);
2157 kill(con
->pipe_pid
, SIGTERM
);
2159 LogMessage(L_DEBUG2
, "WriteClient() %d Closing data file %d.",
2160 con
->http
.fd
, con
->file
);
2167 if (con
->filename
[0])
2169 LogMessage(L_DEBUG2
, "WriteClient() %d Removing temp file %s",
2170 con
->http
.fd
, con
->filename
);
2171 unlink(con
->filename
);
2172 con
->filename
[0] = '\0';
2175 if (con
->request
!= NULL
)
2177 ippDelete(con
->request
);
2178 con
->request
= NULL
;
2181 if (con
->response
!= NULL
)
2183 ippDelete(con
->response
);
2184 con
->response
= NULL
;
2187 if (!con
->http
.keep_alive
)
2195 LogMessage(L_DEBUG2
, "WriteClient() %d %d bytes", con
->http
.fd
, bytes
);
2197 con
->http
.activity
= time(NULL
);
2204 * 'check_if_modified()' - Decode an "If-Modified-Since" line.
2207 static int /* O - 1 if modified since */
2208 check_if_modified(client_t
*con
, /* I - Client connection */
2209 struct stat
*filestats
) /* I - File information */
2211 char *ptr
; /* Pointer into field */
2212 time_t date
; /* Time/date value */
2213 int size
; /* Size/length value */
2218 ptr
= con
->http
.fields
[HTTP_FIELD_IF_MODIFIED_SINCE
];
2223 LogMessage(L_DEBUG2
, "check_if_modified() %d If-Modified-Since=\"%s\"",
2226 while (*ptr
!= '\0')
2228 while (isspace(*ptr
) || *ptr
== ';')
2231 if (strncasecmp(ptr
, "length=", 7) == 0)
2236 while (isdigit(*ptr
))
2239 else if (isalpha(*ptr
))
2241 date
= httpGetDateTime(ptr
);
2242 while (*ptr
!= '\0' && *ptr
!= ';')
2247 LogMessage(L_DEBUG2
, "check_if_modified() %d sizes=%d,%d dates=%d,%d",
2248 con
->http
.fd
, size
, (int)filestats
->st_size
, (int)date
,
2249 (int)filestats
->st_mtime
);
2251 return ((size
!= filestats
->st_size
&& size
!= 0) ||
2252 (date
< filestats
->st_mtime
&& date
!= 0) ||
2253 (size
== 0 && date
== 0));
2258 * 'decode_auth()' - Decode an authorization string.
2262 decode_auth(client_t
*con
) /* I - Client to decode to */
2264 char *s
, /* Authorization string */
2265 value
[1024]; /* Value string */
2266 const char *username
; /* Certificate username */
2270 * Decode the string...
2273 s
= con
->http
.fields
[HTTP_FIELD_AUTHORIZATION
];
2275 LogMessage(L_DEBUG2
, "decode_auth(%p): Authorization string = \"%s\"",
2278 if (strncmp(s
, "Basic", 5) == 0)
2284 httpDecode64(value
, s
);
2287 * Pull the username and password out...
2290 if ((s
= strchr(value
, ':')) == NULL
)
2292 LogMessage(L_DEBUG
, "decode_auth() %d no colon in auth string \"%s\"",
2293 con
->http
.fd
, value
);
2299 strlcpy(con
->username
, value
, sizeof(con
->username
));
2300 strlcpy(con
->password
, s
, sizeof(con
->password
));
2302 else if (strncmp(s
, "Local", 5) == 0)
2308 if ((username
= FindCert(s
)) != NULL
)
2309 strlcpy(con
->username
, username
, sizeof(con
->username
));
2311 else if (strncmp(s
, "Digest", 5) == 0)
2314 * Get the username and password from the Digest attributes...
2317 if (httpGetSubField(&(con
->http
), HTTP_FIELD_WWW_AUTHENTICATE
, "username",
2319 strlcpy(con
->username
, value
, sizeof(con
->username
));
2321 if (httpGetSubField(&(con
->http
), HTTP_FIELD_WWW_AUTHENTICATE
, "response",
2323 strlcpy(con
->password
, value
, sizeof(con
->password
) - 1);
2326 LogMessage(L_DEBUG2
, "decode_auth() %d username=\"%s\"",
2327 con
->http
.fd
, con
->username
);
2332 * 'get_file()' - Get a filename and state info.
2335 static char * /* O - Real filename */
2336 get_file(client_t
*con
, /* I - Client connection */
2337 struct stat
*filestats
,/* O - File information */
2338 char *filename
, /* IO - Filename buffer */
2339 int len
) /* I - Buffer length */
2341 int status
; /* Status of filesystem calls */
2342 char *params
; /* Pointer to parameters in URI */
2346 * Need to add DocumentRoot global...
2349 if (strncmp(con
->uri
, "/ppd/", 5) == 0)
2350 snprintf(filename
, len
, "%s%s", ServerRoot
, con
->uri
);
2351 else if (strncmp(con
->uri
, "/admin/conf/", 12) == 0)
2352 snprintf(filename
, len
, "%s%s", ServerRoot
, con
->uri
+ 11);
2353 else if (con
->language
!= NULL
)
2354 snprintf(filename
, len
, "%s/%s%s", DocumentRoot
, con
->language
->language
,
2357 snprintf(filename
, len
, "%s%s", DocumentRoot
, con
->uri
);
2359 if ((params
= strchr(filename
, '?')) != NULL
)
2363 * Grab the status for this language; if there isn't a language-specific file
2364 * then fallback to the default one...
2367 if ((status
= stat(filename
, filestats
)) != 0 && con
->language
!= NULL
)
2370 * Drop the language prefix and try the current directory...
2373 if (strncmp(con
->uri
, "/ppd/", 5) != 0 &&
2374 strncmp(con
->uri
, "/admin/conf/", 12) != 0)
2376 snprintf(filename
, len
, "%s%s", DocumentRoot
, con
->uri
);
2378 status
= stat(filename
, filestats
);
2383 * If we're found a directory, get the index.html file instead...
2386 if (!status
&& S_ISDIR(filestats
->st_mode
))
2388 if (filename
[strlen(filename
) - 1] == '/')
2389 strlcat(filename
, "index.html", len
);
2391 strlcat(filename
, "/index.html", len
);
2393 status
= stat(filename
, filestats
);
2396 LogMessage(L_DEBUG2
, "get_file() %d filename=%s size=%d",
2397 con
->http
.fd
, filename
, status
? -1 : (int)filestats
->st_size
);
2407 * 'install_conf_file()' - Install a configuration file.
2410 static http_status_t
/* O - Status */
2411 install_conf_file(client_t
*con
) /* I - Connection */
2413 FILE *in
, /* Input file */
2414 *out
; /* Output file */
2415 char buffer
[1024]; /* Copy buffer */
2416 int bytes
; /* Number of bytes */
2417 char conffile
[1024], /* Configuration filename */
2418 newfile
[1024], /* New config filename */
2419 oldfile
[1024]; /* Old config filename */
2420 struct stat confinfo
; /* Config file info */
2424 * First construct the filenames...
2427 snprintf(conffile
, sizeof(conffile
), "%s%s", ServerRoot
, con
->uri
+ 11);
2428 snprintf(newfile
, sizeof(newfile
), "%s%s.N", ServerRoot
, con
->uri
+ 11);
2429 snprintf(oldfile
, sizeof(oldfile
), "%s%s.O", ServerRoot
, con
->uri
+ 11);
2431 LogMessage(L_INFO
, "Installing config file \"%s\"...", conffile
);
2434 * Get the owner, group, and permissions of the configuration file.
2435 * If it doesn't exist, assign it to the User and Group in the
2436 * cupsd.conf file with mode 0640 permissions.
2439 if (stat(conffile
, &confinfo
))
2441 confinfo
.st_uid
= User
;
2442 confinfo
.st_gid
= Group
;
2443 confinfo
.st_mode
= 0640;
2447 * Open the request file and new config file...
2450 if ((in
= fopen(con
->filename
, "rb")) == NULL
)
2452 LogMessage(L_ERROR
, "Unable to open request file \"%s\" - %s",
2453 con
->filename
, strerror(errno
));
2454 return (HTTP_SERVER_ERROR
);
2457 if ((out
= fopen(newfile
, "wb")) == NULL
)
2460 LogMessage(L_ERROR
, "Unable to open config file \"%s\" - %s",
2461 newfile
, strerror(errno
));
2462 return (HTTP_SERVER_ERROR
);
2465 fchmod(fileno(out
), confinfo
.st_mode
);
2466 fchown(fileno(out
), confinfo
.st_uid
, confinfo
.st_gid
);
2469 * Copy from the request to the new config file...
2472 while ((bytes
= fread(buffer
, 1, sizeof(buffer
), in
)) > 0)
2473 if (fwrite(buffer
, 1, bytes
, out
) < bytes
)
2475 LogMessage(L_ERROR
, "Unable to copy to config file \"%s\" - %s",
2476 newfile
, strerror(errno
));
2482 return (HTTP_SERVER_ERROR
);
2486 * Close the files...
2492 LogMessage(L_ERROR
, "Error file closing config file \"%s\" - %s",
2493 newfile
, strerror(errno
));
2497 return (HTTP_SERVER_ERROR
);
2501 * Remove the request file...
2504 unlink(con
->filename
);
2505 con
->filename
[0] = '\0';
2508 * Unlink the old backup, rename the current config file to the backup
2509 * filename, and rename the new config file to the config file name...
2512 if (unlink(oldfile
))
2513 if (errno
!= ENOENT
)
2515 LogMessage(L_ERROR
, "Unable to remove backup config file \"%s\" - %s",
2516 oldfile
, strerror(errno
));
2520 return (HTTP_SERVER_ERROR
);
2523 if (rename(conffile
, oldfile
))
2524 if (errno
!= ENOENT
)
2526 LogMessage(L_ERROR
, "Unable to rename old config file \"%s\" - %s",
2527 conffile
, strerror(errno
));
2531 return (HTTP_SERVER_ERROR
);
2534 if (rename(newfile
, conffile
))
2536 LogMessage(L_ERROR
, "Unable to rename new config file \"%s\" - %s",
2537 newfile
, strerror(errno
));
2539 rename(oldfile
, conffile
);
2542 return (HTTP_SERVER_ERROR
);
2546 * If the cupsd.conf file was updated, set the NeedReload flag...
2549 if (strcmp(con
->uri
, "/admin/conf/cupsd.conf") == 0)
2553 * Return that the file was created successfully...
2556 return (HTTP_CREATED
);
2561 * 'pipe_command()' - Pipe the output of a command to the remote client.
2564 static int /* O - Process ID */
2565 pipe_command(client_t
*con
, /* I - Client connection */
2566 int infile
, /* I - Standard input for command */
2567 int *outfile
, /* O - Standard output for command */
2568 char *command
, /* I - Command to run */
2569 char *options
) /* I - Options for command */
2571 int pid
; /* Process ID */
2572 char *commptr
; /* Command string pointer */
2573 int fd
; /* Looping var */
2574 int fds
[2]; /* Pipe FDs */
2575 int argc
; /* Number of arguments */
2576 int envc
; /* Number of environment variables */
2577 char argbuf
[10240], /* Argument buffer */
2578 *argv
[100], /* Argument strings */
2579 *envp
[100]; /* Environment variables */
2580 char lang
[1024], /* LANG env variable */
2581 content_length
[1024], /* CONTENT_LENGTH env variable */
2582 content_type
[1024], /* CONTENT_TYPE env variable */
2583 ipp_port
[1024], /* Default listen port */
2584 server_port
[1024], /* Default server port */
2585 server_name
[1024], /* Default listen hostname */
2586 remote_host
[1024], /* REMOTE_HOST env variable */
2587 remote_user
[1024], /* REMOTE_USER env variable */
2588 tmpdir
[1024], /* TMPDIR environment variable */
2589 ldpath
[1024], /* LD_LIBRARY_PATH environment variable */
2590 nlspath
[1024], /* NLSPATH environment variable */
2591 datadir
[1024], /* CUPS_DATADIR environment variable */
2592 root
[1024], /* CUPS_SERVERROOT environment variable */
2593 query_string
[10240]; /* QUERY_STRING env variable */
2594 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
2595 sigset_t oldmask
, /* POSIX signal masks */
2597 struct sigaction action
; /* POSIX signal handler */
2598 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
2602 * Copy the command string...
2605 strlcpy(argbuf
, options
, sizeof(argbuf
));
2608 * Parse the string; arguments can be separated by + and are terminated
2614 for (commptr
= argbuf
, argc
= 1; *commptr
!= '\0' && argc
< 99; commptr
++)
2615 if (*commptr
== ' ' || *commptr
== '+')
2619 while (*commptr
== ' ')
2622 if (*commptr
!= '\0')
2624 argv
[argc
] = commptr
;
2630 else if (*commptr
== '%')
2632 if (commptr
[1] >= '0' && commptr
[1] <= '9')
2633 *commptr
= (commptr
[1] - '0') << 4;
2635 *commptr
= (tolower(commptr
[1]) - 'a' + 10) << 4;
2637 if (commptr
[2] >= '0' && commptr
[2] <= '9')
2638 *commptr
|= commptr
[2] - '0';
2640 *commptr
|= tolower(commptr
[2]) - 'a' + 10;
2642 strcpy(commptr
+ 1, commptr
+ 3);
2644 else if (*commptr
== '?')
2649 if (argv
[0][0] == '\0')
2650 argv
[0] = strrchr(command
, '/') + 1;
2653 * Setup the environment variables as needed...
2656 snprintf(lang
, sizeof(lang
), "LANG=%s",
2657 con
->language
? con
->language
->language
: "C");
2659 if (con
->http
.hostaddr
.addr
.sa_family
== AF_INET6
)
2661 sprintf(ipp_port
, "IPP_PORT=%d", ntohs(con
->http
.hostaddr
.ipv6
.sin6_port
));
2662 sprintf(server_port
, "SERVER_PORT=%d",
2663 ntohs(con
->http
.hostaddr
.ipv6
.sin6_port
));
2666 #endif /* AF_INET6 */
2668 sprintf(ipp_port
, "IPP_PORT=%d", ntohs(con
->http
.hostaddr
.ipv4
.sin_port
));
2669 sprintf(server_port
, "SERVER_PORT=%d",
2670 ntohs(con
->http
.hostaddr
.ipv4
.sin_port
));
2673 if (strcmp(con
->http
.hostname
, "localhost") == 0)
2674 strlcpy(server_name
, "SERVER_NAME=localhost", sizeof(server_name
));
2676 snprintf(server_name
, sizeof(server_name
), "SERVER_NAME=%s", ServerName
);
2677 snprintf(remote_host
, sizeof(remote_host
), "REMOTE_HOST=%s", con
->http
.hostname
);
2678 snprintf(remote_user
, sizeof(remote_user
), "REMOTE_USER=%s", con
->username
);
2679 snprintf(tmpdir
, sizeof(tmpdir
), "TMPDIR=%s", TempDir
);
2680 snprintf(datadir
, sizeof(datadir
), "CUPS_DATADIR=%s", DataDir
);
2681 snprintf(root
, sizeof(root
), "CUPS_SERVERROOT=%s", ServerRoot
);
2683 if (getenv("LD_LIBRARY_PATH") != NULL
)
2684 snprintf(ldpath
, sizeof(ldpath
), "LD_LIBRARY_PATH=%s",
2685 getenv("LD_LIBRARY_PATH"));
2686 else if (getenv("DYLD_LIBRARY_PATH") != NULL
)
2687 snprintf(ldpath
, sizeof(ldpath
), "DYLD_LIBRARY_PATH=%s",
2688 getenv("DYLD_LIBRARY_PATH"));
2689 else if (getenv("SHLIB_PATH") != NULL
)
2690 snprintf(ldpath
, sizeof(ldpath
), "SHLIB_PATH=%s",
2691 getenv("SHLIB_PATH"));
2695 if (getenv("NLSPATH") != NULL
)
2696 snprintf(nlspath
, sizeof(nlspath
), "NLSPATH=%s", getenv("NLSPATH"));
2700 envp
[0] = "PATH=/bin:/usr/bin";
2701 envp
[1] = "SERVER_SOFTWARE=CUPS/1.2";
2702 envp
[2] = "GATEWAY_INTERFACE=CGI/1.1";
2703 envp
[3] = "SERVER_PROTOCOL=HTTP/1.1";
2705 envp
[5] = server_name
;
2706 envp
[6] = server_port
;
2707 envp
[7] = remote_host
;
2708 envp
[8] = remote_user
;
2718 envp
[envc
++] = ldpath
;
2721 envp
[envc
++] = nlspath
;
2723 if (con
->operation
== HTTP_GET
)
2725 envp
[envc
++] = "REQUEST_METHOD=GET";
2730 * Add GET form variables after ?...
2735 snprintf(query_string
, sizeof(query_string
), "QUERY_STRING=%s", commptr
);
2736 envp
[envc
++] = query_string
;
2741 sprintf(content_length
, "CONTENT_LENGTH=%d", con
->bytes
);
2742 snprintf(content_type
, sizeof(content_type
), "CONTENT_TYPE=%s",
2743 con
->http
.fields
[HTTP_FIELD_CONTENT_TYPE
]);
2745 envp
[envc
++] = "REQUEST_METHOD=POST";
2746 envp
[envc
++] = content_length
;
2747 envp
[envc
++] = content_type
;
2751 * Tell the CGI if we are using encryption...
2754 if (con
->http
.encryption
== HTTP_ENCRYPT_ALWAYS
)
2756 envp
[envc
++] = "HTTPS=ON";
2757 envp
[envc
++] = "CUPS_ENCRYPTION=Always";
2763 * Create a pipe for the output...
2768 LogMessage(L_ERROR
, "Unable to create pipes for CGI %s - %s",
2769 argv
[0], strerror(errno
));
2774 * Block signals before forking...
2780 #elif defined(HAVE_SIGACTION)
2781 sigemptyset(&newmask
);
2782 sigaddset(&newmask
, SIGTERM
);
2783 sigaddset(&newmask
, SIGCHLD
);
2784 sigprocmask(SIG_BLOCK
, &newmask
, &oldmask
);
2785 #endif /* HAVE_SIGSET */
2788 * Then execute the command...
2791 if ((pid
= fork()) == 0)
2794 * Child comes here... Close stdin if necessary and dup the pipe to stdout.
2800 * Running as root, so change to a non-priviledged user...
2811 * Reset group membership to just the main one we belong to.
2817 * Update stdin/stdout/stderr...
2823 if (dup(infile
) < 0)
2828 if (dup(fds
[1]) < 0)
2832 open("/dev/null", O_WRONLY
);
2835 * Close extra file descriptors...
2838 for (fd
= 3; fd
< MaxFDs
; fd
++)
2842 * Change umask to restrict permissions on created files...
2848 * Unblock signals before doing the exec...
2852 sigset(SIGTERM
, SIG_DFL
);
2853 sigset(SIGCHLD
, SIG_DFL
);
2857 #elif defined(HAVE_SIGACTION)
2858 memset(&action
, 0, sizeof(action
));
2860 sigemptyset(&action
.sa_mask
);
2861 action
.sa_handler
= SIG_DFL
;
2863 sigaction(SIGTERM
, &action
, NULL
);
2864 sigaction(SIGCHLD
, &action
, NULL
);
2866 sigprocmask(SIG_SETMASK
, &oldmask
, NULL
);
2868 signal(SIGTERM
, SIG_DFL
);
2869 signal(SIGCHLD
, SIG_DFL
);
2870 #endif /* HAVE_SIGSET */
2874 * Execute the pipe program; if an error occurs, exit with status 1...
2877 execve(command
, argv
, envp
);
2884 * Error - can't fork!
2887 LogMessage(L_ERROR
, "Unable to fork for CGI %s - %s", argv
[0],
2897 * Fork successful - return the PID...
2900 AddCert(pid
, con
->username
);
2902 LogMessage(L_DEBUG
, "CGI %s started - PID = %d", command
, pid
);
2911 #elif defined(HAVE_SIGACTION)
2912 sigprocmask(SIG_SETMASK
, &oldmask
, NULL
);
2913 #endif /* HAVE_SIGSET */
2919 #if defined(HAVE_CDSASSL)
2921 * 'CDSAReadFunc()' - Read function for CDSA decryption code.
2924 static OSStatus
/* O - -1 on error, 0 on success */
2925 CDSAReadFunc(SSLConnectionRef connection
, /* I - SSL/TLS connection */
2926 void *data
, /* I - Data buffer */
2927 size_t *dataLength
) /* IO - Number of bytes */
2929 ssize_t bytes
; /* Number of bytes read */
2932 bytes
= recv((int)connection
, data
, *dataLength
, 0);
2935 *dataLength
= bytes
;
2944 * 'CDSAWriteFunc()' - Write function for CDSA encryption code.
2947 static OSStatus
/* O - -1 on error, 0 on success */
2948 CDSAWriteFunc(SSLConnectionRef connection
, /* I - SSL/TLS connection */
2949 const void *data
, /* I - Data buffer */
2950 size_t *dataLength
) /* IO - Number of bytes */
2955 bytes
= write((int)connection
, data
, *dataLength
);
2958 *dataLength
= bytes
;
2964 #endif /* HAVE_CDSASSL */
2968 * End of "$Id: client.c,v 1.91.2.43 2003/02/05 21:14:48 mike Exp $".