2 * "$Id: client.c 7950 2008-09-17 00:21:59Z mike $"
4 * Client routines for the CUPS scheduler.
6 * Copyright 2007-2011 by Apple Inc.
7 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
9 * This file contains Kerberos support code, copyright 2006 by
12 * These coded instructions, statements, and computer programs are the
13 * property of Apple Inc. and are protected by Federal copyright
14 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
15 * which should have been included with this file. If this file is
16 * file is missing or damaged, see the license at "http://www.cups.org/".
20 * cupsdAcceptClient() - Accept a new client.
21 * cupsdCloseAllClients() - Close all remote clients immediately.
22 * cupsdCloseClient() - Close a remote client.
23 * cupsdFlushHeader() - Flush the header fields to the client.
24 * cupsdReadClient() - Read data from a client.
25 * cupsdSendCommand() - Send output from a command via HTTP.
26 * cupsdSendError() - Send an error message via HTTP.
27 * cupsdSendHeader() - Send an HTTP request.
28 * cupsdUpdateCGI() - Read status messages from CGI scripts and
30 * cupsdWriteClient() - Write data to a client as needed.
31 * check_if_modified() - Decode an "If-Modified-Since" line.
32 * compare_clients() - Compare two client connections.
33 * data_ready() - Check whether data is available from a client.
34 * encrypt_client() - Enable encryption for the client...
35 * get_cdsa_certificate() - Get a SSL/TLS certificate from the System
37 * get_file() - Get a filename and state info.
38 * install_conf_file() - Install a configuration file.
39 * is_cgi() - Is the resource a CGI script/program?
40 * is_path_absolute() - Is a path absolute and free of relative elements
42 * make_certificate() - Make a self-signed SSL/TLS certificate.
43 * pipe_command() - Pipe the output of a command to the remote client.
44 * valid_host() - Is the Host: field valid?
45 * write_file() - Send a file via HTTP.
46 * write_pipe() - Flag that data is available on the CGI pipe.
50 * Include necessary headers...
57 #endif /* HAVE_TCPD_H */
64 static int check_if_modified(cupsd_client_t
*con
,
65 struct stat
*filestats
);
66 static int compare_clients(cupsd_client_t
*a
, cupsd_client_t
*b
,
68 static int data_ready(cupsd_client_t
*con
);
70 static int encrypt_client(cupsd_client_t
*con
);
73 static CFArrayRef
get_cdsa_certificate(cupsd_client_t
*con
);
74 #endif /* HAVE_CDSASSL */
75 static char *get_file(cupsd_client_t
*con
, struct stat
*filestats
,
76 char *filename
, int len
);
77 static http_status_t
install_conf_file(cupsd_client_t
*con
);
78 static int is_cgi(cupsd_client_t
*con
, const char *filename
,
79 struct stat
*filestats
, mime_type_t
*type
);
80 static int is_path_absolute(const char *path
);
82 static int make_certificate(cupsd_client_t
*con
);
84 static int pipe_command(cupsd_client_t
*con
, int infile
, int *outfile
,
85 char *command
, char *options
, int root
);
86 static int valid_host(cupsd_client_t
*con
);
87 static int write_file(cupsd_client_t
*con
, http_status_t code
,
88 char *filename
, char *type
,
89 struct stat
*filestats
);
90 static void write_pipe(cupsd_client_t
*con
);
94 * 'cupsdAcceptClient()' - Accept a new client.
98 cupsdAcceptClient(cupsd_listener_t
*lis
)/* I - Listener socket */
100 int count
; /* Count of connections on a host */
101 int val
; /* Parameter value */
102 cupsd_client_t
*con
, /* New client pointer */
103 *tempcon
; /* Temporary client pointer */
104 http_addrlist_t
*addrlist
, /* List of adddresses for host */
105 *addr
; /* Current address */
106 socklen_t addrlen
; /* Length of address */
107 char *hostname
; /* Hostname for address */
108 http_addr_t temp
; /* Temporary address variable */
109 static time_t last_dos
= 0; /* Time of last DoS attack */
111 struct request_info wrap_req
; /* TCP wrappers request information */
112 #endif /* HAVE_TCPD_H */
115 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
116 "cupsdAcceptClient(lis=%p(%d)) Clients=%d",
117 lis
, lis
->fd
, cupsArrayCount(Clients
));
120 * Make sure we don't have a full set of clients already...
123 if (cupsArrayCount(Clients
) == MaxClients
)
127 * Get a pointer to the next available client...
131 Clients
= cupsArrayNew(NULL
, NULL
);
135 cupsdLogMessage(CUPSD_LOG_ERROR
,
136 "Unable to allocate memory for clients array!");
137 cupsdPauseListening();
142 ActiveClients
= cupsArrayNew((cups_array_func_t
)compare_clients
, NULL
);
146 cupsdLogMessage(CUPSD_LOG_ERROR
,
147 "Unable to allocate memory for active clients array!");
148 cupsdPauseListening();
152 if ((con
= calloc(1, sizeof(cupsd_client_t
))) == NULL
)
154 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to allocate memory for client!");
155 cupsdPauseListening();
159 con
->http
.activity
= time(NULL
);
161 con
->http
.hostaddr
= &(con
->clientaddr
);
164 * Accept the client and get the remote address...
167 addrlen
= sizeof(http_addr_t
);
169 if ((con
->http
.fd
= accept(lis
->fd
, (struct sockaddr
*)con
->http
.hostaddr
,
172 if (errno
== ENFILE
|| errno
== EMFILE
)
173 cupsdPauseListening();
175 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to accept client connection - %s.",
183 * Save the connected port number...
186 _httpAddrSetPort(con
->http
.hostaddr
, _httpAddrPort(&(lis
->address
)));
190 * Convert IPv4 over IPv6 addresses (::ffff:n.n.n.n) to IPv4 forms we
191 * can more easily use...
194 if (lis
->address
.addr
.sa_family
== AF_INET6
&&
195 con
->http
.hostaddr
->ipv6
.sin6_addr
.s6_addr32
[0] == 0 &&
196 con
->http
.hostaddr
->ipv6
.sin6_addr
.s6_addr32
[1] == 0 &&
197 ntohl(con
->http
.hostaddr
->ipv6
.sin6_addr
.s6_addr32
[2]) == 0xffff)
198 con
->http
.hostaddr
->ipv6
.sin6_addr
.s6_addr32
[2] = 0;
199 #endif /* AF_INET6 */
202 * Check the number of clients on the same address...
205 for (count
= 0, tempcon
= (cupsd_client_t
*)cupsArrayFirst(Clients
);
207 tempcon
= (cupsd_client_t
*)cupsArrayNext(Clients
))
208 if (httpAddrEqual(tempcon
->http
.hostaddr
, con
->http
.hostaddr
))
211 if (count
>= MaxClientsPerHost
)
215 if (count
>= MaxClientsPerHost
)
217 if ((time(NULL
) - last_dos
) >= 60)
219 last_dos
= time(NULL
);
220 cupsdLogMessage(CUPSD_LOG_WARN
,
221 "Possible DoS attack - more than %d clients connecting "
224 httpAddrString(con
->http
.hostaddr
, con
->http
.hostname
,
225 sizeof(con
->http
.hostname
)));
229 closesocket(con
->http
.fd
);
239 * Get the hostname or format the IP address as needed...
242 if (httpAddrLocalhost(con
->http
.hostaddr
))
245 * Map accesses from the loopback interface to "localhost"...
248 strlcpy(con
->http
.hostname
, "localhost", sizeof(con
->http
.hostname
));
249 hostname
= con
->http
.hostname
;
254 * Map accesses from the same host to the server name.
258 hostname
= httpAddrLookup(con
->http
.hostaddr
, con
->http
.hostname
,
259 sizeof(con
->http
.hostname
));
263 httpAddrString(con
->http
.hostaddr
, con
->http
.hostname
,
264 sizeof(con
->http
.hostname
));
268 if (hostname
== NULL
&& HostNameLookups
== 2)
271 * Can't have an unresolved IP address with double-lookups enabled...
275 closesocket(con
->http
.fd
);
280 cupsdLogMessage(CUPSD_LOG_WARN
,
281 "Name lookup failed - connection from %s closed!",
288 if (HostNameLookups
== 2)
291 * Do double lookups as needed...
294 if ((addrlist
= httpAddrGetList(con
->http
.hostname
, AF_UNSPEC
, NULL
))
298 * See if the hostname maps to the same IP address...
301 for (addr
= addrlist
; addr
; addr
= addr
->next
)
302 if (httpAddrEqual(con
->http
.hostaddr
, &(addr
->addr
)))
308 httpAddrFreeList(addrlist
);
313 * Can't have a hostname that doesn't resolve to the same IP address
314 * with double-lookups enabled...
318 closesocket(con
->http
.fd
);
323 cupsdLogMessage(CUPSD_LOG_WARN
,
324 "IP lookup failed - connection from %s closed!",
333 * See if the connection is denied by TCP wrappers...
336 request_init(&wrap_req
, RQ_DAEMON
, "cupsd", RQ_FILE
, con
->http
.fd
, NULL
);
339 if (!hosts_access(&wrap_req
))
342 closesocket(con
->http
.fd
);
347 cupsdLogMessage(CUPSD_LOG_WARN
,
348 "Connection from %s refused by /etc/hosts.allow and "
349 "/etc/hosts.deny rules.", con
->http
.hostname
);
353 #endif /* HAVE_TCPD_H */
356 if (con
->http
.hostaddr
->addr
.sa_family
== AF_LOCAL
)
357 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdAcceptClient: %d from %s (Domain)",
358 con
->http
.fd
, con
->http
.hostname
);
360 #endif /* AF_LOCAL */
361 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdAcceptClient: %d from %s:%d (IPv%d)",
362 con
->http
.fd
, con
->http
.hostname
,
363 _httpAddrPort(con
->http
.hostaddr
),
364 _httpAddrFamily(con
->http
.hostaddr
) == AF_INET
? 4 : 6);
367 * Get the local address the client connected to...
370 addrlen
= sizeof(temp
);
371 if (getsockname(con
->http
.fd
, (struct sockaddr
*)&temp
, &addrlen
))
373 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to get local address - %s",
376 strcpy(con
->servername
, "localhost");
377 con
->serverport
= LocalPort
;
380 else if (_httpAddrFamily(&temp
) == AF_LOCAL
)
382 strcpy(con
->servername
, "localhost");
383 con
->serverport
= LocalPort
;
385 #endif /* AF_LOCAL */
388 if (httpAddrLocalhost(&temp
))
389 strlcpy(con
->servername
, "localhost", sizeof(con
->servername
));
390 else if (HostNameLookups
|| RemotePort
)
391 httpAddrLookup(&temp
, con
->servername
, sizeof(con
->servername
));
393 httpAddrString(&temp
, con
->servername
, sizeof(con
->servername
));
395 con
->serverport
= _httpAddrPort(&(lis
->address
));
398 cupsArrayAdd(Clients
, con
);
401 * Using TCP_NODELAY improves responsiveness, especially on systems with a slow
402 * loopback interface. Since we write large buffers when sending print files
403 * and requests there shouldn't be any performance penalty for this...
407 setsockopt(con
->http
.fd
, IPPROTO_TCP
, TCP_NODELAY
, &val
, sizeof(val
));
410 * Close this file on all execs...
413 fcntl(con
->http
.fd
, F_SETFD
, fcntl(con
->http
.fd
, F_GETFD
) | FD_CLOEXEC
);
416 * Add the socket to the server select.
419 cupsdAddSelect(con
->http
.fd
, (cupsd_selfunc_t
)cupsdReadClient
, NULL
, con
);
422 * Temporarily suspend accept()'s until we lose a client...
425 if (cupsArrayCount(Clients
) == MaxClients
)
426 cupsdPauseListening();
430 * See if we are connecting on a secure port...
433 if (lis
->encryption
== HTTP_ENCRYPT_ALWAYS
)
436 * https connection; go secure...
439 con
->http
.encryption
= HTTP_ENCRYPT_ALWAYS
;
441 if (!encrypt_client(con
))
442 cupsdCloseClient(con
);
446 #endif /* HAVE_SSL */
451 * 'cupsdCloseAllClients()' - Close all remote clients immediately.
455 cupsdCloseAllClients(void)
457 cupsd_client_t
*con
; /* Current client */
460 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdCloseAllClients() Clients=%d",
461 cupsArrayCount(Clients
));
463 for (con
= (cupsd_client_t
*)cupsArrayFirst(Clients
);
465 con
= (cupsd_client_t
*)cupsArrayNext(Clients
))
466 if (cupsdCloseClient(con
))
467 cupsdCloseClient(con
);
472 * 'cupsdCloseClient()' - Close a remote client.
475 int /* O - 1 if partial close, 0 if fully closed */
476 cupsdCloseClient(cupsd_client_t
*con
) /* I - Client to close */
478 int partial
; /* Do partial close for SSL? */
480 SSL_CTX
*context
; /* Context for encryption */
481 unsigned long error
; /* Error code */
482 #elif defined(HAVE_GNUTLS)
483 int error
; /* Error code */
484 gnutls_certificate_server_credentials
*credentials
;
485 /* TLS credentials */
486 # elif defined(HAVE_CDSASSL)
487 #endif /* HAVE_LIBSSL */
490 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdCloseClient: %d", con
->http
.fd
);
493 * Flush pending writes before closing...
496 httpFlushWrite(HTTP(con
));
502 * Shutdown encryption as needed...
510 context
= SSL_get_SSL_CTX(con
->http
.tls
);
512 switch (SSL_shutdown(con
->http
.tls
))
515 cupsdLogMessage(CUPSD_LOG_DEBUG
,
516 "SSL shutdown successful!");
519 cupsdLogMessage(CUPSD_LOG_ERROR
,
520 "Fatal error during SSL shutdown!");
522 while ((error
= ERR_get_error()) != 0)
523 cupsdLogMessage(CUPSD_LOG_ERROR
, "SSL shutdown failed: %s",
524 ERR_error_string(error
, NULL
));
528 SSL_CTX_free(context
);
529 SSL_free(con
->http
.tls
);
531 # elif defined(HAVE_GNUTLS)
532 credentials
= (gnutls_certificate_server_credentials
*)(con
->http
.tls_credentials
);
534 error
= gnutls_bye(con
->http
.tls
, GNUTLS_SHUT_WR
);
537 case GNUTLS_E_SUCCESS
:
538 cupsdLogMessage(CUPSD_LOG_DEBUG
,
539 "SSL shutdown successful!");
542 cupsdLogMessage(CUPSD_LOG_ERROR
,
543 "SSL shutdown failed: %s", gnutls_strerror(error
));
547 gnutls_deinit(con
->http
.tls
);
548 gnutls_certificate_free_credentials(*credentials
);
551 # elif defined(HAVE_CDSASSL)
552 while (SSLClose(con
->http
.tls
) == errSSLWouldBlock
)
555 SSLDisposeContext(con
->http
.tls
);
557 if (con
->http
.tls_credentials
)
558 CFRelease(con
->http
.tls_credentials
);
560 # endif /* HAVE_LIBSSL */
562 con
->http
.tls
= NULL
;
564 #endif /* HAVE_SSL */
566 if (con
->pipe_pid
!= 0)
569 * Stop any CGI process...
572 cupsdEndProcess(con
->pipe_pid
, 1);
578 cupsdRemoveSelect(con
->file
);
585 * Close the socket and clear the file from the input set for select()...
588 if (con
->http
.fd
>= 0)
590 cupsArrayRemove(ActiveClients
, con
);
596 * Only do a partial close so that the encrypted client gets everything.
599 shutdown(con
->http
.fd
, 0);
600 cupsdAddSelect(con
->http
.fd
, (cupsd_selfunc_t
)cupsdReadClient
, NULL
, con
);
605 * Shut the socket down fully...
608 cupsdRemoveSelect(con
->http
.fd
);
620 if (con
->http
.input_set
)
621 free(con
->http
.input_set
);
623 httpClearCookie(HTTP(con
));
624 httpClearFields(HTTP(con
));
626 cupsdClearString(&con
->filename
);
627 cupsdClearString(&con
->command
);
628 cupsdClearString(&con
->options
);
629 cupsdClearString(&con
->query_string
);
633 ippDelete(con
->request
);
639 ippDelete(con
->response
);
640 con
->response
= NULL
;
645 cupsLangFree(con
->language
);
646 con
->language
= NULL
;
649 #ifdef HAVE_AUTHORIZATION_H
652 AuthorizationFree(con
->authref
, kAuthorizationFlagDefaults
);
655 #endif /* HAVE_AUTHORIZATION_H */
658 * Re-enable new client connections if we are going back under the
662 if (cupsArrayCount(Clients
) == MaxClients
)
663 cupsdResumeListening();
666 * Compact the list of clients as necessary...
669 cupsArrayRemove(Clients
, con
);
679 * 'cupsdFlushHeader()' - Flush the header fields to the client.
682 int /* I - Bytes written or -1 on error */
683 cupsdFlushHeader(cupsd_client_t
*con
) /* I - Client to flush to */
685 int bytes
= httpFlushWrite(HTTP(con
));
687 con
->http
.data_encoding
= HTTP_ENCODE_LENGTH
;
694 * 'cupsdReadClient()' - Read data from a client.
698 cupsdReadClient(cupsd_client_t
*con
) /* I - Client to read from */
700 char line
[32768], /* Line from client... */
701 operation
[64], /* Operation code from socket */
702 version
[64], /* HTTP version number string */
703 locale
[64], /* Locale */
704 *ptr
; /* Pointer into strings */
705 int major
, minor
; /* HTTP version numbers */
706 http_status_t status
; /* Transfer status */
707 ipp_state_t ipp_state
; /* State of IPP transfer */
708 int bytes
; /* Number of bytes to POST */
709 char *filename
; /* Name of file for GET/HEAD */
710 char buf
[1024]; /* Buffer for real filename */
711 struct stat filestats
; /* File information */
712 mime_type_t
*type
; /* MIME type of file */
713 cupsd_printer_t
*p
; /* Printer */
714 static unsigned request_id
= 0; /* Request ID for temp files */
717 status
= HTTP_CONTINUE
;
719 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
720 "cupsdReadClient(con=%p(%d)) "
721 "con->http.error=%d "
722 "con->http.used=%d, "
723 "con->http.state=%d "
724 "con->data_encoding=HTTP_ENCODE_%s, "
725 "con->data_remaining=" CUPS_LLFMT
", "
727 con
, con
->http
.fd
, con
->http
.error
, con
->http
.used
,
729 con
->http
.data_encoding
== HTTP_ENCODE_CHUNKED
?
730 "CHUNKED" : "LENGTH",
731 CUPS_LLCAST con
->http
.data_remaining
, con
->file
);
737 * Automatically check for a SSL/TLS handshake...
742 if (recv(con
->http
.fd
, buf
, 1, MSG_PEEK
) == 1 &&
743 (!buf
[0] || !strchr("DGHOPT", buf
[0])))
746 * Encrypt this connection...
749 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
750 "cupsdReadClient: Saw first byte %02X, auto-negotiating "
751 "SSL/TLS session...", buf
[0] & 255);
753 if (!encrypt_client(con
))
754 cupsdCloseClient(con
);
759 #endif /* HAVE_SSL */
761 switch (con
->http
.state
)
765 * See if we've received a request line...
768 if (httpGets(line
, sizeof(line
) - 1, HTTP(con
)) == NULL
)
770 if (con
->http
.error
&& con
->http
.error
!= EPIPE
)
771 cupsdLogMessage(CUPSD_LOG_DEBUG
,
772 "cupsdReadClient: %d WAITING Closing for error %d "
773 "(%s)", con
->http
.fd
, con
->http
.error
,
774 strerror(con
->http
.error
));
776 cupsdLogMessage(CUPSD_LOG_DEBUG
,
777 "cupsdReadClient: %d WAITING Closing on EOF",
780 cupsdCloseClient(con
);
785 * Ignore blank request lines...
792 * Clear other state variables...
795 httpClearFields(HTTP(con
));
797 con
->http
.activity
= time(NULL
);
798 con
->http
.version
= HTTP_1_0
;
799 con
->http
.keep_alive
= HTTP_KEEPALIVE_OFF
;
800 con
->http
.data_encoding
= HTTP_ENCODE_LENGTH
;
801 con
->http
.data_remaining
= 0;
802 con
->http
._data_remaining
= 0;
803 con
->operation
= HTTP_WAITING
;
808 con
->username
[0] = '\0';
809 con
->password
[0] = '\0';
812 cupsdClearString(&con
->command
);
813 cupsdClearString(&con
->options
);
814 cupsdClearString(&con
->query_string
);
818 ippDelete(con
->request
);
824 ippDelete(con
->response
);
825 con
->response
= NULL
;
830 cupsLangFree(con
->language
);
831 con
->language
= NULL
;
837 #endif /* HAVE_GSSAPI */
840 * Grab the request line...
843 switch (sscanf(line
, "%63s%1023s%63s", operation
, con
->uri
, version
))
848 cupsdLogMessage(CUPSD_LOG_ERROR
,
849 "Bad request line \"%s\" from %s!",
850 _httpEncodeURI(buf
, line
, sizeof(buf
)),
852 cupsdSendError(con
, HTTP_BAD_REQUEST
, CUPSD_AUTH_NONE
);
853 cupsdCloseClient(con
);
857 con
->http
.version
= HTTP_0_9
;
860 if (sscanf(version
, "HTTP/%d.%d", &major
, &minor
) != 2)
862 cupsdLogMessage(CUPSD_LOG_ERROR
,
863 "Bad request line \"%s\" from %s!",
864 _httpEncodeURI(buf
, line
, sizeof(buf
)),
866 cupsdSendError(con
, HTTP_BAD_REQUEST
, CUPSD_AUTH_NONE
);
867 cupsdCloseClient(con
);
873 con
->http
.version
= (http_version_t
)(major
* 100 + minor
);
874 if (con
->http
.version
== HTTP_1_1
&& KeepAlive
)
875 con
->http
.keep_alive
= HTTP_KEEPALIVE_ON
;
877 con
->http
.keep_alive
= HTTP_KEEPALIVE_OFF
;
881 cupsdLogMessage(CUPSD_LOG_ERROR
,
882 "Unsupported request line \"%s\" from %s!",
883 _httpEncodeURI(buf
, line
, sizeof(buf
)),
885 cupsdSendError(con
, HTTP_NOT_SUPPORTED
, CUPSD_AUTH_NONE
);
886 cupsdCloseClient(con
);
893 * Handle full URLs in the request line...
896 if (strcmp(con
->uri
, "*"))
898 char scheme
[HTTP_MAX_URI
], /* Method/scheme */
899 userpass
[HTTP_MAX_URI
], /* Username:password */
900 hostname
[HTTP_MAX_URI
], /* Hostname */
901 resource
[HTTP_MAX_URI
]; /* Resource path */
902 int port
; /* Port number */
906 * Separate the URI into its components...
909 httpSeparateURI(HTTP_URI_CODING_MOST
, con
->uri
,
910 scheme
, sizeof(scheme
),
911 userpass
, sizeof(userpass
),
912 hostname
, sizeof(hostname
), &port
,
913 resource
, sizeof(resource
));
916 * Only allow URIs with the servername, localhost, or an IP
920 if (strcmp(scheme
, "file") &&
921 _cups_strcasecmp(hostname
, ServerName
) &&
922 _cups_strcasecmp(hostname
, "localhost") &&
923 !isdigit(hostname
[0]) && hostname
[0] != '[')
926 * Nope, we don't do proxies...
929 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad URI \"%s\" in request!",
931 cupsdSendError(con
, HTTP_METHOD_NOT_ALLOWED
, CUPSD_AUTH_NONE
);
932 cupsdCloseClient(con
);
937 * Copy the resource portion back into the URI; both resource and
938 * con->uri are HTTP_MAX_URI bytes in size...
941 strcpy(con
->uri
, resource
);
945 * Process the request...
948 if (!strcmp(operation
, "GET"))
949 con
->http
.state
= HTTP_GET
;
950 else if (!strcmp(operation
, "PUT"))
951 con
->http
.state
= HTTP_PUT
;
952 else if (!strcmp(operation
, "POST"))
953 con
->http
.state
= HTTP_POST
;
954 else if (!strcmp(operation
, "DELETE"))
955 con
->http
.state
= HTTP_DELETE
;
956 else if (!strcmp(operation
, "TRACE"))
957 con
->http
.state
= HTTP_TRACE
;
958 else if (!strcmp(operation
, "OPTIONS"))
959 con
->http
.state
= HTTP_OPTIONS
;
960 else if (!strcmp(operation
, "HEAD"))
961 con
->http
.state
= HTTP_HEAD
;
964 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad operation \"%s\"!", operation
);
965 cupsdSendError(con
, HTTP_BAD_REQUEST
, CUPSD_AUTH_NONE
);
966 cupsdCloseClient(con
);
970 gettimeofday(&(con
->start
), NULL
);
971 con
->operation
= con
->http
.state
;
973 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdReadClient: %d %s %s HTTP/%d.%d",
974 con
->http
.fd
, operation
, con
->uri
,
975 con
->http
.version
/ 100, con
->http
.version
% 100);
977 con
->http
.status
= HTTP_OK
;
979 if (!cupsArrayFind(ActiveClients
, con
))
981 cupsArrayAdd(ActiveClients
, con
);
993 * Parse incoming parameters until the status changes...
996 while ((status
= httpUpdate(HTTP(con
))) == HTTP_CONTINUE
)
997 if (!data_ready(con
))
1000 if (status
!= HTTP_OK
&& status
!= HTTP_CONTINUE
)
1002 if (con
->http
.error
&& con
->http
.error
!= EPIPE
)
1003 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1004 "cupsdReadClient: %d FIELDS Closing for error %d "
1005 "(%s)", con
->http
.fd
, con
->http
.error
,
1006 strerror(con
->http
.error
));
1008 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1009 "cupsdReadClient: %d FIELDS Closing on EOF",
1012 cupsdSendError(con
, HTTP_BAD_REQUEST
, CUPSD_AUTH_NONE
);
1013 cupsdCloseClient(con
);
1019 if (!data_ready(con
) && recv(con
->http
.fd
, buf
, 1, MSG_PEEK
) < 1)
1022 * Connection closed...
1025 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1026 "cupsdReadClient: %d Closing on EOF", con
->http
.fd
);
1027 cupsdCloseClient(con
);
1030 break; /* Anti-compiler-warning-code */
1034 * Handle new transfers...
1037 if (status
== HTTP_OK
)
1039 if (con
->http
.fields
[HTTP_FIELD_ACCEPT_LANGUAGE
][0])
1042 * Figure out the locale from the Accept-Language and Content-Type
1046 if ((ptr
= strchr(con
->http
.fields
[HTTP_FIELD_ACCEPT_LANGUAGE
],
1050 if ((ptr
= strchr(con
->http
.fields
[HTTP_FIELD_ACCEPT_LANGUAGE
],
1054 if ((ptr
= strstr(con
->http
.fields
[HTTP_FIELD_CONTENT_TYPE
],
1055 "charset=")) != NULL
)
1058 * Combine language and charset, and trim any extra params in the
1062 snprintf(locale
, sizeof(locale
), "%s.%s",
1063 con
->http
.fields
[HTTP_FIELD_ACCEPT_LANGUAGE
], ptr
+ 8);
1065 if ((ptr
= strchr(locale
, ',')) != NULL
)
1069 snprintf(locale
, sizeof(locale
), "%s.UTF-8",
1070 con
->http
.fields
[HTTP_FIELD_ACCEPT_LANGUAGE
]);
1072 con
->language
= cupsLangGet(locale
);
1075 con
->language
= cupsLangGet(DefaultLocale
);
1077 cupsdAuthorize(con
);
1079 if (!_cups_strncasecmp(con
->http
.fields
[HTTP_FIELD_CONNECTION
], "Keep-Alive",
1081 con
->http
.keep_alive
= HTTP_KEEPALIVE_ON
;
1082 else if (!_cups_strncasecmp(con
->http
.fields
[HTTP_FIELD_CONNECTION
], "close", 5))
1083 con
->http
.keep_alive
= HTTP_KEEPALIVE_OFF
;
1085 if (!con
->http
.fields
[HTTP_FIELD_HOST
][0] &&
1086 con
->http
.version
>= HTTP_1_1
)
1089 * HTTP/1.1 and higher require the "Host:" field...
1092 if (!cupsdSendError(con
, HTTP_BAD_REQUEST
, CUPSD_AUTH_NONE
))
1094 cupsdLogMessage(CUPSD_LOG_ERROR
, "Missing Host: field in request!");
1095 cupsdCloseClient(con
);
1099 else if (!valid_host(con
))
1102 * Access to localhost must use "localhost" or the corresponding IPv4
1103 * or IPv6 values in the Host: field.
1106 cupsdLogMessage(CUPSD_LOG_ERROR
,
1107 "Request from \"%s\" using invalid Host: field \"%s\"",
1108 con
->http
.hostname
, con
->http
.fields
[HTTP_FIELD_HOST
]);
1110 if (!cupsdSendError(con
, HTTP_BAD_REQUEST
, CUPSD_AUTH_NONE
))
1112 cupsdCloseClient(con
);
1116 else if (con
->operation
== HTTP_OPTIONS
)
1119 * Do OPTIONS command...
1122 if (con
->best
&& con
->best
->type
!= CUPSD_AUTH_NONE
)
1124 if (!cupsdSendHeader(con
, HTTP_UNAUTHORIZED
, NULL
, CUPSD_AUTH_NONE
))
1126 cupsdCloseClient(con
);
1131 if (!_cups_strcasecmp(con
->http
.fields
[HTTP_FIELD_CONNECTION
], "Upgrade") &&
1132 con
->http
.tls
== NULL
)
1136 * Do encryption stuff...
1139 if (!cupsdSendHeader(con
, HTTP_SWITCHING_PROTOCOLS
, NULL
, CUPSD_AUTH_NONE
))
1141 cupsdCloseClient(con
);
1145 httpPrintf(HTTP(con
), "Connection: Upgrade\r\n");
1146 httpPrintf(HTTP(con
), "Upgrade: TLS/1.0,HTTP/1.1\r\n");
1147 httpPrintf(HTTP(con
), "Content-Length: 0\r\n");
1148 httpPrintf(HTTP(con
), "\r\n");
1150 if (cupsdFlushHeader(con
) < 0)
1152 cupsdCloseClient(con
);
1156 if (!encrypt_client(con
))
1158 cupsdCloseClient(con
);
1162 if (!cupsdSendError(con
, HTTP_NOT_IMPLEMENTED
, CUPSD_AUTH_NONE
))
1164 cupsdCloseClient(con
);
1167 #endif /* HAVE_SSL */
1170 if (!cupsdSendHeader(con
, HTTP_OK
, NULL
, CUPSD_AUTH_NONE
))
1172 cupsdCloseClient(con
);
1176 httpPrintf(HTTP(con
), "Allow: GET, HEAD, OPTIONS, POST, PUT\r\n");
1177 httpPrintf(HTTP(con
), "Content-Length: 0\r\n");
1178 httpPrintf(HTTP(con
), "\r\n");
1180 if (cupsdFlushHeader(con
) < 0)
1182 cupsdCloseClient(con
);
1186 else if (!is_path_absolute(con
->uri
))
1189 * Protect against malicious users!
1192 cupsdLogMessage(CUPSD_LOG_ERROR
,
1193 "Request for non-absolute resource \"%s\"!", con
->uri
);
1195 if (!cupsdSendError(con
, HTTP_FORBIDDEN
, CUPSD_AUTH_NONE
))
1197 cupsdCloseClient(con
);
1203 if (!_cups_strcasecmp(con
->http
.fields
[HTTP_FIELD_CONNECTION
], "Upgrade") &&
1204 con
->http
.tls
== NULL
)
1208 * Do encryption stuff...
1211 if (!cupsdSendHeader(con
, HTTP_SWITCHING_PROTOCOLS
, NULL
, CUPSD_AUTH_NONE
))
1213 cupsdCloseClient(con
);
1217 httpPrintf(HTTP(con
), "Connection: Upgrade\r\n");
1218 httpPrintf(HTTP(con
), "Upgrade: TLS/1.0,HTTP/1.1\r\n");
1219 httpPrintf(HTTP(con
), "Content-Length: 0\r\n");
1220 httpPrintf(HTTP(con
), "\r\n");
1222 if (cupsdFlushHeader(con
) < 0)
1224 cupsdCloseClient(con
);
1228 if (!encrypt_client(con
))
1230 cupsdCloseClient(con
);
1234 if (!cupsdSendError(con
, HTTP_NOT_IMPLEMENTED
, CUPSD_AUTH_NONE
))
1236 cupsdCloseClient(con
);
1239 #endif /* HAVE_SSL */
1242 if ((status
= cupsdIsAuthorized(con
, NULL
)) != HTTP_OK
)
1244 cupsdSendError(con
, status
, CUPSD_AUTH_NONE
);
1245 cupsdCloseClient(con
);
1249 if (con
->http
.expect
&&
1250 (con
->operation
== HTTP_POST
|| con
->operation
== HTTP_PUT
))
1252 if (con
->http
.expect
== HTTP_CONTINUE
)
1255 * Send 100-continue header...
1258 if (!cupsdSendHeader(con
, HTTP_CONTINUE
, NULL
, CUPSD_AUTH_NONE
))
1260 cupsdCloseClient(con
);
1267 * Send 417-expectation-failed header...
1270 if (!cupsdSendHeader(con
, HTTP_EXPECTATION_FAILED
, NULL
,
1273 cupsdCloseClient(con
);
1277 httpPrintf(HTTP(con
), "Content-Length: 0\r\n");
1278 httpPrintf(HTTP(con
), "\r\n");
1280 if (cupsdFlushHeader(con
) < 0)
1282 cupsdCloseClient(con
);
1288 switch (con
->http
.state
)
1290 case HTTP_GET_SEND
:
1291 if (!strncmp(con
->uri
, "/printers/", 10) &&
1292 !strcmp(con
->uri
+ strlen(con
->uri
) - 4, ".ppd"))
1295 * Send PPD file - get the real printer name since printer
1296 * names are not case sensitive but filenames can be...
1299 con
->uri
[strlen(con
->uri
) - 4] = '\0'; /* Drop ".ppd" */
1301 if ((p
= cupsdFindPrinter(con
->uri
+ 10)) != NULL
)
1302 snprintf(con
->uri
, sizeof(con
->uri
), "/ppd/%s.ppd", p
->name
);
1305 if (!cupsdSendError(con
, HTTP_NOT_FOUND
, CUPSD_AUTH_NONE
))
1307 cupsdCloseClient(con
);
1314 else if ((!strncmp(con
->uri
, "/printers/", 10) ||
1315 !strncmp(con
->uri
, "/classes/", 9)) &&
1316 !strcmp(con
->uri
+ strlen(con
->uri
) - 4, ".png"))
1319 * Send icon file - get the real queue name since queue names are
1320 * not case sensitive but filenames can be...
1323 con
->uri
[strlen(con
->uri
) - 4] = '\0'; /* Drop ".png" */
1325 if (!strncmp(con
->uri
, "/printers/", 10))
1326 p
= cupsdFindPrinter(con
->uri
+ 10);
1328 p
= cupsdFindClass(con
->uri
+ 9);
1331 snprintf(con
->uri
, sizeof(con
->uri
), "/icons/%s.png", p
->name
);
1334 if (!cupsdSendError(con
, HTTP_NOT_FOUND
, CUPSD_AUTH_NONE
))
1336 cupsdCloseClient(con
);
1343 else if (!WebInterface
)
1346 * Web interface is disabled. Show an appropriate message...
1349 if (!cupsdSendError(con
, HTTP_WEBIF_DISABLED
, CUPSD_AUTH_NONE
))
1351 cupsdCloseClient(con
);
1358 if ((!strncmp(con
->uri
, "/admin", 6) &&
1359 strncmp(con
->uri
, "/admin/conf/", 12) &&
1360 strncmp(con
->uri
, "/admin/log/", 11)) ||
1361 !strncmp(con
->uri
, "/printers", 9) ||
1362 !strncmp(con
->uri
, "/classes", 8) ||
1363 !strncmp(con
->uri
, "/help", 5) ||
1364 !strncmp(con
->uri
, "/jobs", 5))
1367 * Send CGI output...
1370 if (!strncmp(con
->uri
, "/admin", 6))
1372 cupsdSetStringf(&con
->command
, "%s/cgi-bin/admin.cgi",
1375 cupsdSetString(&con
->options
, strchr(con
->uri
+ 6, '?'));
1377 else if (!strncmp(con
->uri
, "/printers", 9))
1379 cupsdSetStringf(&con
->command
, "%s/cgi-bin/printers.cgi",
1382 if (con
->uri
[9] && con
->uri
[10])
1383 cupsdSetString(&con
->options
, con
->uri
+ 9);
1385 cupsdSetString(&con
->options
, NULL
);
1387 else if (!strncmp(con
->uri
, "/classes", 8))
1389 cupsdSetStringf(&con
->command
, "%s/cgi-bin/classes.cgi",
1392 if (con
->uri
[8] && con
->uri
[9])
1393 cupsdSetString(&con
->options
, con
->uri
+ 8);
1395 cupsdSetString(&con
->options
, NULL
);
1397 else if (!strncmp(con
->uri
, "/jobs", 5))
1399 cupsdSetStringf(&con
->command
, "%s/cgi-bin/jobs.cgi",
1402 if (con
->uri
[5] && con
->uri
[6])
1403 cupsdSetString(&con
->options
, con
->uri
+ 5);
1405 cupsdSetString(&con
->options
, NULL
);
1409 cupsdSetStringf(&con
->command
, "%s/cgi-bin/help.cgi",
1412 if (con
->uri
[5] && con
->uri
[6])
1413 cupsdSetString(&con
->options
, con
->uri
+ 5);
1415 cupsdSetString(&con
->options
, NULL
);
1418 if (!cupsdSendCommand(con
, con
->command
, con
->options
, 0))
1420 if (!cupsdSendError(con
, HTTP_NOT_FOUND
, CUPSD_AUTH_NONE
))
1422 cupsdCloseClient(con
);
1427 cupsdLogRequest(con
, HTTP_OK
);
1429 if (con
->http
.version
<= HTTP_1_0
)
1430 con
->http
.keep_alive
= HTTP_KEEPALIVE_OFF
;
1432 else if ((!strncmp(con
->uri
, "/admin/conf/", 12) &&
1433 (strchr(con
->uri
+ 12, '/') ||
1434 strlen(con
->uri
) == 12)) ||
1435 (!strncmp(con
->uri
, "/admin/log/", 11) &&
1436 (strchr(con
->uri
+ 11, '/') ||
1437 strlen(con
->uri
) == 11)))
1440 * GET can only be done to configuration files directly under
1444 cupsdLogMessage(CUPSD_LOG_ERROR
,
1445 "Request for subdirectory \"%s\"!", con
->uri
);
1447 if (!cupsdSendError(con
, HTTP_FORBIDDEN
, CUPSD_AUTH_NONE
))
1449 cupsdCloseClient(con
);
1461 if ((filename
= get_file(con
, &filestats
, buf
,
1462 sizeof(buf
))) == NULL
)
1464 if (!cupsdSendError(con
, HTTP_NOT_FOUND
, CUPSD_AUTH_NONE
))
1466 cupsdCloseClient(con
);
1473 type
= mimeFileType(MimeDatabase
, filename
, NULL
, NULL
);
1475 if (is_cgi(con
, filename
, &filestats
, type
))
1478 * Note: con->command and con->options were set by
1482 if (!cupsdSendCommand(con
, con
->command
, con
->options
, 0))
1484 if (!cupsdSendError(con
, HTTP_NOT_FOUND
, CUPSD_AUTH_NONE
))
1486 cupsdCloseClient(con
);
1491 cupsdLogRequest(con
, HTTP_OK
);
1493 if (con
->http
.version
<= HTTP_1_0
)
1494 con
->http
.keep_alive
= HTTP_KEEPALIVE_OFF
;
1498 if (!check_if_modified(con
, &filestats
))
1500 if (!cupsdSendError(con
, HTTP_NOT_MODIFIED
, CUPSD_AUTH_NONE
))
1502 cupsdCloseClient(con
);
1509 strcpy(line
, "text/plain");
1511 snprintf(line
, sizeof(line
), "%s/%s", type
->super
, type
->type
);
1513 if (!write_file(con
, HTTP_OK
, filename
, line
, &filestats
))
1515 cupsdCloseClient(con
);
1522 case HTTP_POST_RECV
:
1524 * See if the POST request includes a Content-Length field, and if
1525 * so check the length against any limits that are set...
1528 if (con
->http
.fields
[HTTP_FIELD_CONTENT_LENGTH
][0] &&
1529 MaxRequestSize
> 0 &&
1530 con
->http
.data_remaining
> MaxRequestSize
)
1533 * Request too large...
1536 if (!cupsdSendError(con
, HTTP_REQUEST_TOO_LARGE
, CUPSD_AUTH_NONE
))
1538 cupsdCloseClient(con
);
1544 else if (con
->http
.data_remaining
< 0 ||
1545 (!con
->http
.fields
[HTTP_FIELD_CONTENT_LENGTH
][0] &&
1546 con
->http
.data_encoding
== HTTP_ENCODE_LENGTH
))
1549 * Negative content lengths are invalid!
1552 if (!cupsdSendError(con
, HTTP_BAD_REQUEST
, CUPSD_AUTH_NONE
))
1554 cupsdCloseClient(con
);
1562 * See what kind of POST request this is; for IPP requests the
1563 * content-type field will be "application/ipp"...
1566 if (!strcmp(con
->http
.fields
[HTTP_FIELD_CONTENT_TYPE
],
1568 con
->request
= ippNew();
1569 else if (!WebInterface
)
1572 * Web interface is disabled. Show an appropriate message...
1575 if (!cupsdSendError(con
, HTTP_WEBIF_DISABLED
, CUPSD_AUTH_NONE
))
1577 cupsdCloseClient(con
);
1583 else if ((!strncmp(con
->uri
, "/admin", 6) &&
1584 strncmp(con
->uri
, "/admin/conf/", 12) &&
1585 strncmp(con
->uri
, "/admin/log/", 11)) ||
1586 !strncmp(con
->uri
, "/printers", 9) ||
1587 !strncmp(con
->uri
, "/classes", 8) ||
1588 !strncmp(con
->uri
, "/help", 5) ||
1589 !strncmp(con
->uri
, "/jobs", 5))
1595 if (!strncmp(con
->uri
, "/admin", 6))
1597 cupsdSetStringf(&con
->command
, "%s/cgi-bin/admin.cgi",
1600 cupsdSetString(&con
->options
, strchr(con
->uri
+ 6, '?'));
1602 else if (!strncmp(con
->uri
, "/printers", 9))
1604 cupsdSetStringf(&con
->command
, "%s/cgi-bin/printers.cgi",
1607 if (con
->uri
[9] && con
->uri
[10])
1608 cupsdSetString(&con
->options
, con
->uri
+ 9);
1610 cupsdSetString(&con
->options
, NULL
);
1612 else if (!strncmp(con
->uri
, "/classes", 8))
1614 cupsdSetStringf(&con
->command
, "%s/cgi-bin/classes.cgi",
1617 if (con
->uri
[8] && con
->uri
[9])
1618 cupsdSetString(&con
->options
, con
->uri
+ 8);
1620 cupsdSetString(&con
->options
, NULL
);
1622 else if (!strncmp(con
->uri
, "/jobs", 5))
1624 cupsdSetStringf(&con
->command
, "%s/cgi-bin/jobs.cgi",
1627 if (con
->uri
[5] && con
->uri
[6])
1628 cupsdSetString(&con
->options
, con
->uri
+ 5);
1630 cupsdSetString(&con
->options
, NULL
);
1634 cupsdSetStringf(&con
->command
, "%s/cgi-bin/help.cgi",
1637 if (con
->uri
[5] && con
->uri
[6])
1638 cupsdSetString(&con
->options
, con
->uri
+ 5);
1640 cupsdSetString(&con
->options
, NULL
);
1643 if (con
->http
.version
<= HTTP_1_0
)
1644 con
->http
.keep_alive
= HTTP_KEEPALIVE_OFF
;
1652 if ((filename
= get_file(con
, &filestats
, buf
,
1653 sizeof(buf
))) == NULL
)
1655 if (!cupsdSendError(con
, HTTP_NOT_FOUND
, CUPSD_AUTH_NONE
))
1657 cupsdCloseClient(con
);
1664 type
= mimeFileType(MimeDatabase
, filename
, NULL
, NULL
);
1666 if (!is_cgi(con
, filename
, &filestats
, type
))
1669 * Only POST to CGI's...
1672 if (!cupsdSendError(con
, HTTP_UNAUTHORIZED
, CUPSD_AUTH_NONE
))
1674 cupsdCloseClient(con
);
1681 case HTTP_PUT_RECV
:
1683 * Validate the resource name...
1686 if (strncmp(con
->uri
, "/admin/conf/", 12) ||
1687 strchr(con
->uri
+ 12, '/') ||
1688 strlen(con
->uri
) == 12)
1691 * PUT can only be done to configuration files under
1695 cupsdLogMessage(CUPSD_LOG_ERROR
,
1696 "Request for subdirectory \"%s\"!", con
->uri
);
1698 if (!cupsdSendError(con
, HTTP_FORBIDDEN
, CUPSD_AUTH_NONE
))
1700 cupsdCloseClient(con
);
1708 * See if the PUT request includes a Content-Length field, and if
1709 * so check the length against any limits that are set...
1712 if (con
->http
.fields
[HTTP_FIELD_CONTENT_LENGTH
][0] &&
1713 MaxRequestSize
> 0 &&
1714 con
->http
.data_remaining
> MaxRequestSize
)
1717 * Request too large...
1720 if (!cupsdSendError(con
, HTTP_REQUEST_TOO_LARGE
, CUPSD_AUTH_NONE
))
1722 cupsdCloseClient(con
);
1728 else if (con
->http
.data_remaining
< 0)
1731 * Negative content lengths are invalid!
1734 if (!cupsdSendError(con
, HTTP_BAD_REQUEST
, CUPSD_AUTH_NONE
))
1736 cupsdCloseClient(con
);
1744 * Open a temporary file to hold the request...
1747 cupsdSetStringf(&con
->filename
, "%s/%08x", RequestRoot
,
1749 con
->file
= open(con
->filename
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0640);
1753 cupsdLogMessage(CUPSD_LOG_ERROR
,
1754 "Unable to create request file %s: %s",
1755 con
->filename
, strerror(errno
));
1757 if (!cupsdSendError(con
, HTTP_REQUEST_TOO_LARGE
, CUPSD_AUTH_NONE
))
1759 cupsdCloseClient(con
);
1764 fchmod(con
->file
, 0640);
1765 fchown(con
->file
, RunUser
, Group
);
1766 fcntl(con
->file
, F_SETFD
, fcntl(con
->file
, F_GETFD
) | FD_CLOEXEC
);
1771 cupsdSendError(con
, HTTP_NOT_IMPLEMENTED
, CUPSD_AUTH_NONE
);
1772 cupsdCloseClient(con
);
1776 if (!strncmp(con
->uri
, "/printers/", 10) &&
1777 !strcmp(con
->uri
+ strlen(con
->uri
) - 4, ".ppd"))
1780 * Send PPD file - get the real printer name since printer
1781 * names are not case sensitive but filenames can be...
1784 con
->uri
[strlen(con
->uri
) - 4] = '\0'; /* Drop ".ppd" */
1786 if ((p
= cupsdFindPrinter(con
->uri
+ 10)) != NULL
)
1787 snprintf(con
->uri
, sizeof(con
->uri
), "/ppd/%s.ppd", p
->name
);
1790 if (!cupsdSendError(con
, HTTP_NOT_FOUND
, CUPSD_AUTH_NONE
))
1792 cupsdCloseClient(con
);
1799 else if (!strncmp(con
->uri
, "/printers/", 10) &&
1800 !strcmp(con
->uri
+ strlen(con
->uri
) - 4, ".png"))
1803 * Send PNG file - get the real printer name since printer
1804 * names are not case sensitive but filenames can be...
1807 con
->uri
[strlen(con
->uri
) - 4] = '\0'; /* Drop ".ppd" */
1809 if ((p
= cupsdFindPrinter(con
->uri
+ 10)) != NULL
)
1810 snprintf(con
->uri
, sizeof(con
->uri
), "/icons/%s.png", p
->name
);
1813 if (!cupsdSendError(con
, HTTP_NOT_FOUND
, CUPSD_AUTH_NONE
))
1815 cupsdCloseClient(con
);
1822 else if (!WebInterface
)
1824 if (!cupsdSendHeader(con
, HTTP_OK
, line
, CUPSD_AUTH_NONE
))
1826 cupsdCloseClient(con
);
1830 if (httpPrintf(HTTP(con
), "\r\n") < 0)
1832 cupsdCloseClient(con
);
1836 if (cupsdFlushHeader(con
) < 0)
1838 cupsdCloseClient(con
);
1842 con
->http
.state
= HTTP_WAITING
;
1846 if ((!strncmp(con
->uri
, "/admin", 6) &&
1847 strncmp(con
->uri
, "/admin/conf/", 12) &&
1848 strncmp(con
->uri
, "/admin/log/", 11)) ||
1849 !strncmp(con
->uri
, "/printers", 9) ||
1850 !strncmp(con
->uri
, "/classes", 8) ||
1851 !strncmp(con
->uri
, "/help", 5) ||
1852 !strncmp(con
->uri
, "/jobs", 5))
1858 if (!cupsdSendHeader(con
, HTTP_OK
, "text/html", CUPSD_AUTH_NONE
))
1860 cupsdCloseClient(con
);
1864 if (httpPrintf(HTTP(con
), "\r\n") < 0)
1866 cupsdCloseClient(con
);
1870 if (cupsdFlushHeader(con
) < 0)
1872 cupsdCloseClient(con
);
1876 cupsdLogRequest(con
, HTTP_OK
);
1878 else if ((!strncmp(con
->uri
, "/admin/conf/", 12) &&
1879 (strchr(con
->uri
+ 12, '/') ||
1880 strlen(con
->uri
) == 12)) ||
1881 (!strncmp(con
->uri
, "/admin/log/", 11) &&
1882 (strchr(con
->uri
+ 11, '/') ||
1883 strlen(con
->uri
) == 11)))
1886 * HEAD can only be done to configuration files under
1890 cupsdLogMessage(CUPSD_LOG_ERROR
,
1891 "Request for subdirectory \"%s\"!", con
->uri
);
1893 if (!cupsdSendError(con
, HTTP_FORBIDDEN
, CUPSD_AUTH_NONE
))
1895 cupsdCloseClient(con
);
1901 else if ((filename
= get_file(con
, &filestats
, buf
,
1902 sizeof(buf
))) == NULL
)
1904 if (!cupsdSendHeader(con
, HTTP_NOT_FOUND
, "text/html",
1907 cupsdCloseClient(con
);
1911 cupsdLogRequest(con
, HTTP_NOT_FOUND
);
1913 else if (!check_if_modified(con
, &filestats
))
1915 if (!cupsdSendError(con
, HTTP_NOT_MODIFIED
, CUPSD_AUTH_NONE
))
1917 cupsdCloseClient(con
);
1921 cupsdLogRequest(con
, HTTP_NOT_MODIFIED
);
1929 type
= mimeFileType(MimeDatabase
, filename
, NULL
, NULL
);
1931 strcpy(line
, "text/plain");
1933 snprintf(line
, sizeof(line
), "%s/%s", type
->super
, type
->type
);
1935 if (!cupsdSendHeader(con
, HTTP_OK
, line
, CUPSD_AUTH_NONE
))
1937 cupsdCloseClient(con
);
1941 if (httpPrintf(HTTP(con
), "Last-Modified: %s\r\n",
1942 httpGetDateString(filestats
.st_mtime
)) < 0)
1944 cupsdCloseClient(con
);
1948 if (httpPrintf(HTTP(con
), "Content-Length: %lu\r\n",
1949 (unsigned long)filestats
.st_size
) < 0)
1951 cupsdCloseClient(con
);
1955 cupsdLogRequest(con
, HTTP_OK
);
1958 if (httpPrintf(HTTP(con
), "\r\n") < 0)
1960 cupsdCloseClient(con
);
1964 if (cupsdFlushHeader(con
) < 0)
1966 cupsdCloseClient(con
);
1970 con
->http
.state
= HTTP_WAITING
;
1974 break; /* Anti-compiler-warning-code */
1980 * Handle any incoming data...
1983 switch (con
->http
.state
)
1985 case HTTP_PUT_RECV
:
1988 if ((bytes
= httpRead2(HTTP(con
), line
, sizeof(line
))) < 0)
1990 if (con
->http
.error
&& con
->http
.error
!= EPIPE
)
1991 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1992 "cupsdReadClient: %d PUT_RECV Closing for error "
1993 "%d (%s)", con
->http
.fd
, con
->http
.error
,
1994 strerror(con
->http
.error
));
1996 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1997 "cupsdReadClient: %d PUT_RECV Closing on EOF",
2000 cupsdCloseClient(con
);
2005 con
->bytes
+= bytes
;
2007 if (write(con
->file
, line
, bytes
) < bytes
)
2009 cupsdLogMessage(CUPSD_LOG_ERROR
,
2010 "cupsdReadClient: Unable to write %d bytes to %s: %s",
2011 bytes
, con
->filename
, strerror(errno
));
2015 unlink(con
->filename
);
2016 cupsdClearString(&con
->filename
);
2018 if (!cupsdSendError(con
, HTTP_REQUEST_TOO_LARGE
, CUPSD_AUTH_NONE
))
2020 cupsdCloseClient(con
);
2026 while (con
->http
.state
== HTTP_PUT_RECV
&& data_ready(con
));
2028 if (con
->http
.state
== HTTP_WAITING
)
2031 * End of file, see how big it is...
2034 fstat(con
->file
, &filestats
);
2039 if (filestats
.st_size
> MaxRequestSize
&&
2043 * Request is too big; remove it and send an error...
2046 unlink(con
->filename
);
2047 cupsdClearString(&con
->filename
);
2049 if (!cupsdSendError(con
, HTTP_REQUEST_TOO_LARGE
, CUPSD_AUTH_NONE
))
2051 cupsdCloseClient(con
);
2057 * Install the configuration file...
2060 status
= install_conf_file(con
);
2063 * Return the status to the client...
2066 if (!cupsdSendError(con
, status
, CUPSD_AUTH_NONE
))
2068 cupsdCloseClient(con
);
2074 case HTTP_POST_RECV
:
2077 if (con
->request
&& con
->file
< 0)
2080 * Grab any request data from the connection...
2083 if ((ipp_state
= ippRead(&(con
->http
), con
->request
)) == IPP_ERROR
)
2085 cupsdLogMessage(CUPSD_LOG_ERROR
,
2086 "cupsdReadClient: %d IPP Read Error!",
2089 cupsdSendError(con
, HTTP_BAD_REQUEST
, CUPSD_AUTH_NONE
);
2090 cupsdCloseClient(con
);
2093 else if (ipp_state
!= IPP_DATA
)
2095 if (con
->http
.state
== HTTP_POST_SEND
)
2097 cupsdSendError(con
, HTTP_BAD_REQUEST
, CUPSD_AUTH_NONE
);
2098 cupsdCloseClient(con
);
2106 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdReadClient: %d %d.%d %s %d",
2107 con
->http
.fd
, con
->request
->request
.op
.version
[0],
2108 con
->request
->request
.op
.version
[1],
2109 ippOpString(con
->request
->request
.op
.operation_id
),
2110 con
->request
->request
.op
.request_id
);
2111 con
->bytes
+= ippLength(con
->request
);
2115 if (con
->file
< 0 && con
->http
.state
!= HTTP_POST_SEND
)
2118 * Create a file as needed for the request data...
2121 cupsdSetStringf(&con
->filename
, "%s/%08x", RequestRoot
,
2123 con
->file
= open(con
->filename
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0640);
2127 cupsdLogMessage(CUPSD_LOG_ERROR
,
2128 "Unable to create request file %s: %s",
2129 con
->filename
, strerror(errno
));
2131 if (!cupsdSendError(con
, HTTP_REQUEST_TOO_LARGE
, CUPSD_AUTH_NONE
))
2133 cupsdCloseClient(con
);
2138 fchmod(con
->file
, 0640);
2139 fchown(con
->file
, RunUser
, Group
);
2140 fcntl(con
->file
, F_SETFD
, fcntl(con
->file
, F_GETFD
) | FD_CLOEXEC
);
2143 if (con
->http
.state
!= HTTP_POST_SEND
)
2145 if ((bytes
= httpRead2(HTTP(con
), line
, sizeof(line
))) < 0)
2147 if (con
->http
.error
&& con
->http
.error
!= EPIPE
)
2148 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2149 "cupsdReadClient: %d POST_SEND Closing for "
2150 "error %d (%s)", con
->http
.fd
, con
->http
.error
,
2151 strerror(con
->http
.error
));
2153 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2154 "cupsdReadClient: %d POST_SEND Closing on EOF",
2157 cupsdCloseClient(con
);
2162 con
->bytes
+= bytes
;
2164 if (write(con
->file
, line
, bytes
) < bytes
)
2166 cupsdLogMessage(CUPSD_LOG_ERROR
,
2167 "cupsdReadClient: Unable to write %d bytes to "
2168 "%s: %s", bytes
, con
->filename
,
2173 unlink(con
->filename
);
2174 cupsdClearString(&con
->filename
);
2176 if (!cupsdSendError(con
, HTTP_REQUEST_TOO_LARGE
,
2179 cupsdCloseClient(con
);
2184 else if (con
->http
.state
== HTTP_POST_RECV
)
2186 else if (con
->http
.state
!= HTTP_POST_SEND
)
2188 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2189 "cupsdReadClient: %d Closing on unknown HTTP "
2190 "state %d", con
->http
.fd
, con
->http
.state
);
2191 cupsdCloseClient(con
);
2196 while (con
->http
.state
== HTTP_POST_RECV
&& data_ready(con
));
2198 if (con
->http
.state
== HTTP_POST_SEND
)
2202 fstat(con
->file
, &filestats
);
2207 if (filestats
.st_size
> MaxRequestSize
&&
2211 * Request is too big; remove it and send an error...
2214 unlink(con
->filename
);
2215 cupsdClearString(&con
->filename
);
2220 * Delete any IPP request data...
2223 ippDelete(con
->request
);
2224 con
->request
= NULL
;
2227 if (!cupsdSendError(con
, HTTP_REQUEST_TOO_LARGE
, CUPSD_AUTH_NONE
))
2229 cupsdCloseClient(con
);
2233 else if (filestats
.st_size
== 0)
2236 * Don't allow empty file...
2239 unlink(con
->filename
);
2240 cupsdClearString(&con
->filename
);
2245 if (!cupsdSendCommand(con
, con
->command
, con
->options
, 0))
2247 if (!cupsdSendError(con
, HTTP_NOT_FOUND
, CUPSD_AUTH_NONE
))
2249 cupsdCloseClient(con
);
2254 cupsdLogRequest(con
, HTTP_OK
);
2260 cupsdProcessIPPRequest(con
);
2264 unlink(con
->filename
);
2265 cupsdClearString(&con
->filename
);
2274 break; /* Anti-compiler-warning-code */
2277 if (con
->http
.state
== HTTP_WAITING
)
2279 if (!con
->http
.keep_alive
)
2281 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2282 "cupsdReadClient: %d Closing because Keep-Alive disabled",
2284 cupsdCloseClient(con
);
2288 cupsArrayRemove(ActiveClients
, con
);
2289 cupsdSetBusyState();
2296 * 'cupsdSendCommand()' - Send output from a command via HTTP.
2299 int /* O - 1 on success, 0 on failure */
2301 cupsd_client_t
*con
, /* I - Client connection */
2302 char *command
, /* I - Command to run */
2303 char *options
, /* I - Command-line options */
2304 int root
) /* I - Run as root? */
2306 int fd
; /* Standard input file descriptor */
2311 fd
= open(con
->filename
, O_RDONLY
);
2315 cupsdLogMessage(CUPSD_LOG_ERROR
,
2316 "cupsdSendCommand: %d Unable to open \"%s\" for reading: %s",
2317 con
->http
.fd
, con
->filename
? con
->filename
: "/dev/null",
2322 fcntl(fd
, F_SETFD
, fcntl(fd
, F_GETFD
) | FD_CLOEXEC
);
2327 con
->pipe_pid
= pipe_command(con
, fd
, &(con
->file
), command
, options
, root
);
2332 cupsdLogMessage(CUPSD_LOG_INFO
, "Started \"%s\" (pid=%d)", command
,
2335 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdSendCommand: %d file=%d",
2336 con
->http
.fd
, con
->file
);
2338 if (con
->pipe_pid
== 0)
2341 fcntl(con
->file
, F_SETFD
, fcntl(con
->file
, F_GETFD
) | FD_CLOEXEC
);
2343 cupsdAddSelect(con
->file
, (cupsd_selfunc_t
)write_pipe
, NULL
, con
);
2345 con
->sent_header
= 0;
2346 con
->file_ready
= 0;
2347 con
->got_fields
= 0;
2348 con
->header_used
= 0;
2355 * 'cupsdSendError()' - Send an error message via HTTP.
2358 int /* O - 1 if successful, 0 otherwise */
2359 cupsdSendError(cupsd_client_t
*con
, /* I - Connection */
2360 http_status_t code
, /* I - Error code */
2361 int auth_type
)/* I - Authentication type */
2363 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2364 "cupsdSendError(con=%p(%d), code=%d, auth_type=%d", con
,
2365 con
->http
.fd
, code
, auth_type
);
2369 * Force client to upgrade for authentication if that is how the
2370 * server is configured...
2373 if (code
== HTTP_UNAUTHORIZED
&&
2374 DefaultEncryption
== HTTP_ENCRYPT_REQUIRED
&&
2375 _cups_strcasecmp(con
->http
.hostname
, "localhost") &&
2378 code
= HTTP_UPGRADE_REQUIRED
;
2380 #endif /* HAVE_SSL */
2383 * Put the request in the access_log file...
2386 cupsdLogRequest(con
, code
);
2389 * To work around bugs in some proxies, don't use Keep-Alive for some
2392 * Kerberos authentication doesn't work without Keep-Alive, so
2393 * never disable it in that case.
2396 if (code
>= HTTP_BAD_REQUEST
&& con
->http
.auth_type
!= CUPSD_AUTH_NEGOTIATE
)
2397 con
->http
.keep_alive
= HTTP_KEEPALIVE_OFF
;
2400 * Send an error message back to the client. If the error code is a
2401 * 400 or 500 series, make sure the message contains some text, too!
2404 if (!cupsdSendHeader(con
, code
, NULL
, auth_type
))
2408 if (code
== HTTP_UPGRADE_REQUIRED
)
2409 if (httpPrintf(HTTP(con
), "Connection: Upgrade\r\n") < 0)
2412 if (httpPrintf(HTTP(con
), "Upgrade: TLS/1.0,HTTP/1.1\r\n") < 0)
2414 #endif /* HAVE_SSL */
2416 if (con
->http
.version
>= HTTP_1_1
&&
2417 con
->http
.keep_alive
== HTTP_KEEPALIVE_OFF
)
2419 if (httpPrintf(HTTP(con
), "Connection: close\r\n") < 0)
2423 if (code
>= HTTP_BAD_REQUEST
)
2426 * Send a human-readable error message.
2429 char message
[4096], /* Message for user */
2430 urltext
[1024], /* URL redirection text */
2431 redirect
[1024]; /* Redirection link */
2432 const char *text
; /* Status-specific text */
2437 if (code
== HTTP_UNAUTHORIZED
)
2438 text
= _cupsLangString(con
->language
,
2439 _("Enter your username and password or the "
2440 "root username and password to access this "
2441 "page. If you are using Kerberos authentication, "
2442 "make sure you have a valid Kerberos ticket."));
2443 else if (code
== HTTP_UPGRADE_REQUIRED
)
2447 snprintf(urltext
, sizeof(urltext
),
2448 _cupsLangString(con
->language
,
2449 _("You must access this page using the URL "
2450 "<A HREF=\"https://%s:%d%s\">"
2451 "https://%s:%d%s</A>.")),
2452 con
->servername
, con
->serverport
, con
->uri
,
2453 con
->servername
, con
->serverport
, con
->uri
);
2455 snprintf(redirect
, sizeof(redirect
),
2456 "<META HTTP-EQUIV=\"Refresh\" "
2457 "CONTENT=\"3;URL=https://%s:%d%s\">\n",
2458 con
->servername
, con
->serverport
, con
->uri
);
2460 else if (code
== HTTP_WEBIF_DISABLED
)
2461 text
= _cupsLangString(con
->language
,
2462 _("The web interface is currently disabled. Run "
2463 "\"cupsctl WebInterface=yes\" to enable it."));
2467 snprintf(message
, sizeof(message
),
2468 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" "
2469 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
2472 "\t<META HTTP-EQUIV=\"Content-Type\" "
2473 "CONTENT=\"text/html; charset=utf-8\">\n"
2474 "\t<TITLE>%s - " CUPS_SVERSION
"</TITLE>\n"
2475 "\t<LINK REL=\"STYLESHEET\" TYPE=\"text/css\" "
2476 "HREF=\"/cups.css\">\n"
2484 httpStatus(code
), redirect
, httpStatus(code
), text
);
2486 if (httpPrintf(HTTP(con
), "Content-Type: text/html; charset=utf-8\r\n") < 0)
2488 if (httpPrintf(HTTP(con
), "Content-Length: %d\r\n",
2489 (int)strlen(message
)) < 0)
2491 if (httpPrintf(HTTP(con
), "\r\n") < 0)
2493 if (httpPrintf(HTTP(con
), "%s", message
) < 0)
2496 else if (httpPrintf(HTTP(con
), "\r\n") < 0)
2499 if (cupsdFlushHeader(con
) < 0)
2502 con
->http
.state
= HTTP_WAITING
;
2509 * 'cupsdSendHeader()' - Send an HTTP request.
2512 int /* O - 1 on success, 0 on failure */
2514 cupsd_client_t
*con
, /* I - Client to send to */
2515 http_status_t code
, /* I - HTTP status code */
2516 char *type
, /* I - MIME type of document */
2517 int auth_type
) /* I - Type of authentication */
2519 char auth_str
[1024]; /* Authorization string */
2520 #if 0 /* def HAVE_GSSAPI */
2521 static char *gss_buf
= NULL
; /* Kerberos auth data buffer */
2522 static int gss_bufsize
= 0; /* Size of Kerberos auth data buffer */
2523 #endif /* HAVE_GSSAPI */
2527 * Send the HTTP status header...
2530 if (code
== HTTP_CONTINUE
)
2533 * 100-continue doesn't send any headers...
2536 return (httpPrintf(HTTP(con
), "HTTP/%d.%d 100 Continue\r\n\r\n",
2537 con
->http
.version
/ 100, con
->http
.version
% 100) > 0);
2539 else if (code
== HTTP_WEBIF_DISABLED
)
2542 * Treat our special "web interface is disabled" status as "200 OK" for web
2549 httpFlushWrite(HTTP(con
));
2551 con
->http
.data_encoding
= HTTP_ENCODE_FIELDS
;
2553 if (httpPrintf(HTTP(con
), "HTTP/%d.%d %d %s\r\n", con
->http
.version
/ 100,
2554 con
->http
.version
% 100, code
, httpStatus(code
)) < 0)
2556 if (httpPrintf(HTTP(con
), "Date: %s\r\n", httpGetDateString(time(NULL
))) < 0)
2559 if (httpPrintf(HTTP(con
), "Server: %s\r\n", ServerHeader
) < 0)
2561 if (con
->http
.keep_alive
&& con
->http
.version
>= HTTP_1_0
)
2563 if (httpPrintf(HTTP(con
), "Connection: Keep-Alive\r\n") < 0)
2565 if (httpPrintf(HTTP(con
), "Keep-Alive: timeout=%d\r\n",
2566 KeepAliveTimeout
) < 0)
2569 if (code
== HTTP_METHOD_NOT_ALLOWED
)
2570 if (httpPrintf(HTTP(con
), "Allow: GET, HEAD, OPTIONS, POST, PUT\r\n") < 0)
2573 if (code
== HTTP_UNAUTHORIZED
)
2575 if (auth_type
== CUPSD_AUTH_NONE
)
2577 if (!con
->best
|| con
->best
->type
<= CUPSD_AUTH_NONE
)
2578 auth_type
= DefaultAuthType
;
2580 auth_type
= con
->best
->type
;
2585 if (auth_type
== CUPSD_AUTH_BASIC
|| auth_type
== CUPSD_AUTH_BASICDIGEST
)
2586 strlcpy(auth_str
, "Basic realm=\"CUPS\"", sizeof(auth_str
));
2587 else if (auth_type
== CUPSD_AUTH_DIGEST
)
2588 snprintf(auth_str
, sizeof(auth_str
), "Digest realm=\"CUPS\", nonce=\"%s\"",
2589 con
->http
.hostname
);
2591 else if (auth_type
== CUPSD_AUTH_NEGOTIATE
)
2594 if (_httpAddrFamily(con
->http
.hostaddr
) == AF_LOCAL
)
2595 strlcpy(auth_str
, "Basic realm=\"CUPS\"", sizeof(auth_str
));
2597 # endif /* AF_LOCAL */
2598 strlcpy(auth_str
, "Negotiate", sizeof(auth_str
));
2600 #endif /* HAVE_GSSAPI */
2602 if (con
->best
&& auth_type
!= CUPSD_AUTH_NEGOTIATE
&&
2603 !_cups_strcasecmp(con
->http
.hostname
, "localhost"))
2606 * Add a "trc" (try root certification) parameter for local non-Kerberos
2607 * requests when the request requires system group membership - then the
2608 * client knows the root certificate can/should be used.
2610 * Also, for Mac OS X we also look for @AUTHKEY and add an "authkey"
2611 * parameter as needed...
2614 char *name
, /* Current user name */
2615 *auth_key
; /* Auth key buffer */
2616 size_t auth_size
; /* Size of remaining buffer */
2618 auth_key
= auth_str
+ strlen(auth_str
);
2619 auth_size
= sizeof(auth_str
) - (auth_key
- auth_str
);
2621 for (name
= (char *)cupsArrayFirst(con
->best
->names
);
2623 name
= (char *)cupsArrayNext(con
->best
->names
))
2625 #ifdef HAVE_AUTHORIZATION_H
2626 if (!_cups_strncasecmp(name
, "@AUTHKEY(", 9))
2628 snprintf(auth_key
, auth_size
, ", authkey=\"%s\"", name
+ 9);
2629 /* end parenthesis is stripped in conf.c */
2633 #endif /* HAVE_AUTHORIZATION_H */
2634 if (!_cups_strcasecmp(name
, "@SYSTEM"))
2636 #ifdef HAVE_AUTHORIZATION_H
2637 if (SystemGroupAuthKey
)
2638 snprintf(auth_key
, auth_size
,
2640 SystemGroupAuthKey
);
2643 strlcpy(auth_key
, ", trc=\"y\"", auth_size
);
2644 #endif /* HAVE_AUTHORIZATION_H */
2652 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2653 "cupsdSendHeader: %d WWW-Authenticate: %s", con
->http
.fd
,
2656 if (httpPrintf(HTTP(con
), "WWW-Authenticate: %s\r\n", auth_str
) < 0)
2661 if (con
->language
&& strcmp(con
->language
->language
, "C"))
2663 if (httpPrintf(HTTP(con
), "Content-Language: %s\r\n",
2664 con
->language
->language
) < 0)
2670 if (!strcmp(type
, "text/html"))
2672 if (httpPrintf(HTTP(con
),
2673 "Content-Type: text/html; charset=utf-8\r\n") < 0)
2676 else if (httpPrintf(HTTP(con
), "Content-Type: %s\r\n", type
) < 0)
2685 * 'cupsdUpdateCGI()' - Read status messages from CGI scripts and programs.
2689 cupsdUpdateCGI(void)
2691 char *ptr
, /* Pointer to end of line in buffer */
2692 message
[1024]; /* Pointer to message text */
2693 int loglevel
; /* Log level for message */
2696 while ((ptr
= cupsdStatBufUpdate(CGIStatusBuffer
, &loglevel
,
2697 message
, sizeof(message
))) != NULL
)
2699 if (loglevel
== CUPSD_LOG_INFO
)
2700 cupsdLogMessage(CUPSD_LOG_INFO
, "%s", message
);
2702 if (!strchr(CGIStatusBuffer
->buffer
, '\n'))
2706 if (ptr
== NULL
&& !CGIStatusBuffer
->bufused
)
2709 * Fatal error on pipe - should never happen!
2712 cupsdLogMessage(CUPSD_LOG_CRIT
,
2713 "cupsdUpdateCGI: error reading from CGI error pipe - %s",
2720 * 'cupsdWriteClient()' - Write data to a client as needed.
2724 cupsdWriteClient(cupsd_client_t
*con
) /* I - Client connection */
2726 int bytes
, /* Number of bytes written */
2727 field_col
; /* Current column */
2728 char *bufptr
, /* Pointer into buffer */
2729 *bufend
; /* Pointer to end of buffer */
2730 ipp_state_t ipp_state
; /* IPP state value */
2733 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2734 "cupsdWriteClient(con=%p(%d)) response=%p(%d), file=%d "
2735 "pipe_pid=%d state=%d",
2736 con
, con
->http
.fd
, con
->response
,
2737 con
->response
? con
->response
->state
: -1,
2738 con
->file
, con
->pipe_pid
, con
->http
.state
);
2740 if (con
->http
.state
!= HTTP_GET_SEND
&&
2741 con
->http
.state
!= HTTP_POST_SEND
)
2744 * If we get called in the wrong state, then something went wrong with the
2745 * connection and we need to shut it down...
2748 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2749 "cupsdWriteClient: %d Closing on unknown HTTP state %d",
2750 con
->http
.fd
, con
->http
.state
);
2751 cupsdCloseClient(con
);
2758 * Make sure we select on the CGI output...
2761 cupsdAddSelect(con
->file
, (cupsd_selfunc_t
)write_pipe
, NULL
, con
);
2763 if (!con
->file_ready
)
2766 * Try again later when there is CGI output available...
2769 cupsdRemoveSelect(con
->http
.fd
);
2773 con
->file_ready
= 0;
2776 if (con
->response
&& con
->response
->state
!= IPP_DATA
)
2778 ipp_state
= ippWrite(HTTP(con
), con
->response
);
2779 bytes
= ipp_state
!= IPP_ERROR
&&
2780 (con
->file
>= 0 || ipp_state
!= IPP_DATA
);
2782 else if ((bytes
= read(con
->file
, con
->header
,
2783 sizeof(con
->header
) - con
->header_used
)) > 0)
2785 con
->header_used
+= bytes
;
2787 if (con
->pipe_pid
&& !con
->got_fields
)
2790 * Inspect the data for Content-Type and other fields.
2793 for (bufptr
= con
->header
, bufend
= con
->header
+ con
->header_used
,
2795 !con
->got_fields
&& bufptr
< bufend
;
2798 if (*bufptr
== '\n')
2801 * Send line to client...
2804 if (bufptr
> con
->header
&& bufptr
[-1] == '\r')
2808 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Script header: %s", con
->header
);
2810 if (!con
->sent_header
)
2813 * Handle redirection and CGI status codes...
2816 if (!_cups_strncasecmp(con
->header
, "Location:", 9))
2818 if (!cupsdSendHeader(con
, HTTP_SEE_OTHER
, NULL
, CUPSD_AUTH_NONE
))
2820 cupsdCloseClient(con
);
2824 con
->sent_header
= 2;
2826 if (httpPrintf(HTTP(con
), "Content-Length: 0\r\n") < 0)
2829 else if (!_cups_strncasecmp(con
->header
, "Status:", 7))
2831 cupsdSendError(con
, (http_status_t
)atoi(con
->header
+ 7),
2833 con
->sent_header
= 2;
2837 if (!cupsdSendHeader(con
, HTTP_OK
, NULL
, CUPSD_AUTH_NONE
))
2839 cupsdCloseClient(con
);
2843 con
->sent_header
= 1;
2845 if (con
->http
.version
== HTTP_1_1
)
2847 if (httpPrintf(HTTP(con
), "Transfer-Encoding: chunked\r\n") < 0)
2853 if (_cups_strncasecmp(con
->header
, "Status:", 7))
2854 httpPrintf(HTTP(con
), "%s\r\n", con
->header
);
2860 con
->header_used
-= bufptr
- con
->header
;
2862 if (con
->header_used
> 0)
2863 memmove(con
->header
, bufptr
, con
->header_used
);
2865 bufptr
= con
->header
- 1;
2868 * See if the line was empty...
2873 con
->got_fields
= 1;
2875 if (cupsdFlushHeader(con
) < 0)
2877 cupsdCloseClient(con
);
2881 if (con
->http
.version
== HTTP_1_1
)
2882 con
->http
.data_encoding
= HTTP_ENCODE_CHUNKED
;
2887 else if (*bufptr
!= '\r')
2891 if (!con
->got_fields
)
2893 con
->http
.activity
= time(NULL
);
2898 if (con
->header_used
> 0)
2900 if (httpWrite2(HTTP(con
), con
->header
, con
->header_used
) < 0)
2902 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2903 "cupsdWriteClient: %d Closing for error %d (%s)",
2904 con
->http
.fd
, con
->http
.error
,
2905 strerror(con
->http
.error
));
2906 cupsdCloseClient(con
);
2910 if (con
->http
.data_encoding
== HTTP_ENCODE_CHUNKED
)
2911 httpFlushWrite(HTTP(con
));
2913 con
->bytes
+= con
->header_used
;
2915 if (con
->http
.state
== HTTP_WAITING
)
2918 bytes
= con
->header_used
;
2920 con
->header_used
= 0;
2925 (con
->http
.state
!= HTTP_GET_SEND
&& con
->http
.state
!= HTTP_POST_SEND
))
2927 if (!con
->sent_header
&& con
->pipe_pid
)
2928 cupsdSendError(con
, HTTP_SERVER_ERROR
, CUPSD_AUTH_NONE
);
2931 cupsdLogRequest(con
, HTTP_OK
);
2933 httpFlushWrite(HTTP(con
));
2935 if (con
->http
.data_encoding
== HTTP_ENCODE_CHUNKED
&& con
->sent_header
== 1)
2937 if (httpWrite2(HTTP(con
), "", 0) < 0)
2939 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2940 "cupsdWriteClient: %d Closing for error %d (%s)",
2941 con
->http
.fd
, con
->http
.error
,
2942 strerror(con
->http
.error
));
2943 cupsdCloseClient(con
);
2949 con
->http
.state
= HTTP_WAITING
;
2951 cupsdAddSelect(con
->http
.fd
, (cupsd_selfunc_t
)cupsdReadClient
, NULL
, con
);
2955 cupsdRemoveSelect(con
->file
);
2958 cupsdEndProcess(con
->pipe_pid
, 0);
2967 unlink(con
->filename
);
2968 cupsdClearString(&con
->filename
);
2973 ippDelete(con
->request
);
2974 con
->request
= NULL
;
2979 ippDelete(con
->response
);
2980 con
->response
= NULL
;
2983 cupsdClearString(&con
->command
);
2984 cupsdClearString(&con
->options
);
2985 cupsdClearString(&con
->query_string
);
2987 if (!con
->http
.keep_alive
)
2989 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2990 "cupsdWriteClient: %d Closing because Keep-Alive disabled",
2992 cupsdCloseClient(con
);
2997 cupsArrayRemove(ActiveClients
, con
);
2998 cupsdSetBusyState();
3002 con
->http
.activity
= time(NULL
);
3007 * 'check_if_modified()' - Decode an "If-Modified-Since" line.
3010 static int /* O - 1 if modified since */
3012 cupsd_client_t
*con
, /* I - Client connection */
3013 struct stat
*filestats
) /* I - File information */
3015 char *ptr
; /* Pointer into field */
3016 time_t date
; /* Time/date value */
3017 off_t size
; /* Size/length value */
3022 ptr
= con
->http
.fields
[HTTP_FIELD_IF_MODIFIED_SINCE
];
3027 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3028 "check_if_modified(con=%p(%d), "
3029 "filestats=%p(" CUPS_LLFMT
", %d)) If-Modified-Since=\"%s\"",
3030 con
, con
->http
.fd
, filestats
, CUPS_LLCAST filestats
->st_size
,
3031 (int)filestats
->st_mtime
, ptr
);
3033 while (*ptr
!= '\0')
3035 while (isspace(*ptr
) || *ptr
== ';')
3038 if (_cups_strncasecmp(ptr
, "length=", 7) == 0)
3041 size
= strtoll(ptr
, NULL
, 10);
3043 while (isdigit(*ptr
))
3046 else if (isalpha(*ptr
))
3048 date
= httpGetDateTime(ptr
);
3049 while (*ptr
!= '\0' && *ptr
!= ';')
3056 return ((size
!= filestats
->st_size
&& size
!= 0) ||
3057 (date
< filestats
->st_mtime
&& date
!= 0) ||
3058 (size
== 0 && date
== 0));
3063 * 'compare_clients()' - Compare two client connections.
3066 static int /* O - Result of comparison */
3067 compare_clients(cupsd_client_t
*a
, /* I - First client */
3068 cupsd_client_t
*b
, /* I - Second client */
3069 void *data
) /* I - User data (not used) */
3083 * 'data_ready()' - Check whether data is available from a client.
3086 static int /* O - 1 if data is ready, 0 otherwise */
3087 data_ready(cupsd_client_t
*con
) /* I - Client */
3089 if (con
->http
.used
> 0)
3092 else if (con
->http
.tls
)
3095 if (SSL_pending((SSL
*)(con
->http
.tls
)))
3097 # elif defined(HAVE_GNUTLS)
3098 if (gnutls_record_check_pending(con
->http
.tls
))
3100 # elif defined(HAVE_CDSASSL)
3101 size_t bytes
; /* Bytes that are available */
3103 if (!SSLGetBufferedReadSize(con
->http
.tls
, &bytes
) && bytes
> 0)
3105 # endif /* HAVE_LIBSSL */
3107 #endif /* HAVE_SSL */
3115 * 'encrypt_client()' - Enable encryption for the client...
3118 static int /* O - 1 on success, 0 on error */
3119 encrypt_client(cupsd_client_t
*con
) /* I - Client to encrypt */
3122 SSL_CTX
*context
; /* Context for encryption */
3123 BIO
*bio
; /* BIO data */
3124 unsigned long error
; /* Error code */
3127 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "encrypt_client(con=%p(%d))", con
,
3131 * Verify that we have a certificate...
3134 if (access(ServerKey
, 0) || access(ServerCertificate
, 0))
3137 * Nope, make a self-signed certificate...
3140 if (!make_certificate(con
))
3145 * Create the SSL context and accept the connection...
3148 context
= SSL_CTX_new(SSLv23_server_method());
3150 SSL_CTX_set_options(context
, SSL_OP_NO_SSLv2
); /* Only use SSLv3 or TLS */
3151 if (SSLOptions
& CUPSD_SSL_NOEMPTY
)
3152 SSL_CTX_set_options(context
, SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS
);
3153 SSL_CTX_use_PrivateKey_file(context
, ServerKey
, SSL_FILETYPE_PEM
);
3154 SSL_CTX_use_certificate_chain_file(context
, ServerCertificate
);
3156 bio
= BIO_new(_httpBIOMethods());
3157 BIO_ctrl(bio
, BIO_C_SET_FILE_PTR
, 0, (char *)HTTP(con
));
3159 con
->http
.tls
= SSL_new(context
);
3160 SSL_set_bio(con
->http
.tls
, bio
, bio
);
3162 if (SSL_accept(con
->http
.tls
) != 1)
3164 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to encrypt connection from %s!",
3165 con
->http
.hostname
);
3167 while ((error
= ERR_get_error()) != 0)
3168 cupsdLogMessage(CUPSD_LOG_ERROR
, "%s", ERR_error_string(error
, NULL
));
3170 SSL_CTX_free(context
);
3171 SSL_free(con
->http
.tls
);
3172 con
->http
.tls
= NULL
;
3176 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Connection from %s now encrypted.",
3177 con
->http
.hostname
);
3181 # elif defined(HAVE_GNUTLS)
3182 int status
; /* Error code */
3183 gnutls_certificate_server_credentials
*credentials
;
3184 /* TLS credentials */
3185 const char *priority
; /* Priority string */
3188 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "encrypt_client(con=%p(%d))", con
,
3192 * Verify that we have a certificate...
3195 if (access(ServerKey
, 0) || access(ServerCertificate
, 0))
3198 * Nope, make a self-signed certificate...
3201 if (!make_certificate(con
))
3206 * Create the SSL object and perform the SSL handshake...
3209 credentials
= (gnutls_certificate_server_credentials
*)
3210 malloc(sizeof(gnutls_certificate_server_credentials
));
3211 if (credentials
== NULL
)
3213 cupsdLogMessage(CUPSD_LOG_ERROR
,
3214 "Unable to encrypt connection from %s - %s",
3215 con
->http
.hostname
, strerror(errno
));
3220 gnutls_certificate_allocate_credentials(credentials
);
3221 gnutls_certificate_set_x509_key_file(*credentials
, ServerCertificate
,
3222 ServerKey
, GNUTLS_X509_FMT_PEM
);
3224 gnutls_init(&con
->http
.tls
, GNUTLS_SERVER
);
3225 gnutls_set_default_priority(con
->http
.tls
);
3226 status
= gnutls_priority_set_direct(con
->http
.tls
,
3227 "NORMAL:-VERS-TLS-ALL:+VERS-TLS1.0:"
3228 "+VERS-SSL3.0:%COMPAT", &priority
);
3229 if (status
!= GNUTLS_E_SUCCESS
)
3231 cupsdLogMessage(CUPSD_LOG_ERROR
,
3232 "Unable to encrypt connection from %s - %s (%s)",
3233 con
->http
.hostname
, gnutls_strerror(status
), priority
);
3235 gnutls_deinit(con
->http
.tls
);
3236 gnutls_certificate_free_credentials(*credentials
);
3237 con
->http
.tls
= NULL
;
3242 gnutls_credentials_set(con
->http
.tls
, GNUTLS_CRD_CERTIFICATE
, *credentials
);
3243 gnutls_transport_set_ptr(con
->http
.tls
, (gnutls_transport_ptr
)HTTP(con
));
3244 gnutls_transport_set_pull_function(con
->http
.tls
, _httpReadGNUTLS
);
3245 gnutls_transport_set_push_function(con
->http
.tls
, _httpWriteGNUTLS
);
3247 while ((status
= gnutls_handshake(con
->http
.tls
)) != GNUTLS_E_SUCCESS
)
3249 if (gnutls_error_is_fatal(status
))
3251 cupsdLogMessage(CUPSD_LOG_ERROR
,
3252 "Unable to encrypt connection from %s - %s",
3253 con
->http
.hostname
, gnutls_strerror(status
));
3255 gnutls_deinit(con
->http
.tls
);
3256 gnutls_certificate_free_credentials(*credentials
);
3257 con
->http
.tls
= NULL
;
3263 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Connection from %s now encrypted.",
3264 con
->http
.hostname
);
3266 con
->http
.tls_credentials
= credentials
;
3269 # elif defined(HAVE_CDSASSL)
3270 OSStatus error
= 0; /* Error code */
3271 CFArrayRef peerCerts
; /* Peer certificates */
3274 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "encrypt_client(con=%p(%d))", con
,
3277 con
->http
.tls_credentials
= get_cdsa_certificate(con
);
3279 if (!con
->http
.tls_credentials
)
3282 * No keychain (yet), make a self-signed certificate...
3285 if (make_certificate(con
))
3286 con
->http
.tls_credentials
= get_cdsa_certificate(con
);
3289 if (!con
->http
.tls_credentials
)
3291 cupsdLogMessage(CUPSD_LOG_ERROR
,
3292 "Could not find signing key in keychain \"%s\"",
3294 error
= errSSLBadCert
; /* errSSLBadConfiguration is a better choice, but not available on 10.2.x */
3298 error
= SSLNewContext(true, &con
->http
.tls
);
3301 error
= SSLSetIOFuncs(con
->http
.tls
, _httpReadCDSA
, _httpWriteCDSA
);
3304 error
= SSLSetConnection(con
->http
.tls
, HTTP(con
));
3307 error
= SSLSetAllowsExpiredCerts(con
->http
.tls
, true);
3310 error
= SSLSetAllowsAnyRoot(con
->http
.tls
, true);
3313 error
= SSLSetCertificate(con
->http
.tls
, con
->http
.tls_credentials
);
3318 * Perform SSL/TLS handshake
3321 while ((error
= SSLHandshake(con
->http
.tls
)) == errSSLWouldBlock
)
3327 cupsdLogMessage(CUPSD_LOG_ERROR
,
3328 "Unable to encrypt connection from %s - %s (%d)",
3329 con
->http
.hostname
, cssmErrorString(error
), (int)error
);
3331 con
->http
.error
= error
;
3332 con
->http
.status
= HTTP_ERROR
;
3336 SSLDisposeContext(con
->http
.tls
);
3337 con
->http
.tls
= NULL
;
3340 if (con
->http
.tls_credentials
)
3342 CFRelease(con
->http
.tls_credentials
);
3343 con
->http
.tls_credentials
= NULL
;
3349 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Connection from %s now encrypted.",
3350 con
->http
.hostname
);
3352 if (!SSLCopyPeerCertificates(con
->http
.tls
, &peerCerts
) && peerCerts
)
3354 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Received %d peer certificates!",
3355 (int)CFArrayGetCount(peerCerts
));
3356 CFRelease(peerCerts
);
3359 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Received NO peer certificates!");
3363 # endif /* HAVE_LIBSSL */
3365 #endif /* HAVE_SSL */
3370 * 'get_cdsa_certificate()' - Get a SSL/TLS certificate from the System keychain.
3373 static CFArrayRef
/* O - Array of certificates */
3374 get_cdsa_certificate(
3375 cupsd_client_t
*con
) /* I - Client connection */
3377 OSStatus err
; /* Error info */
3378 SecKeychainRef keychain
= NULL
;/* Keychain reference */
3379 SecIdentitySearchRef search
= NULL
; /* Search reference */
3380 SecIdentityRef identity
= NULL
;/* Identity */
3381 CFArrayRef certificates
= NULL
;
3382 /* Certificate array */
3383 # if HAVE_SECPOLICYCREATESSL
3384 SecPolicyRef policy
= NULL
; /* Policy ref */
3385 CFStringRef servername
= NULL
;
3387 CFMutableDictionaryRef query
= NULL
; /* Query qualifiers */
3388 char localname
[1024];/* Local hostname */
3389 # elif defined(HAVE_SECIDENTITYSEARCHCREATEWITHPOLICY)
3390 SecPolicyRef policy
= NULL
; /* Policy ref */
3391 SecPolicySearchRef policy_search
= NULL
;
3392 /* Policy search ref */
3393 CSSM_DATA options
; /* Policy options */
3394 CSSM_APPLE_TP_SSL_OPTIONS
3395 ssl_options
; /* SSL Option for hostname */
3396 char localname
[1024];/* Local hostname */
3397 # endif /* HAVE_SECPOLICYCREATESSL */
3400 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3401 "get_cdsa_certificate: Looking for certs for \"%s\"...",
3404 if ((err
= SecKeychainOpen(ServerCertificate
, &keychain
)))
3406 cupsdLogMessage(CUPSD_LOG_ERROR
, "Cannot open keychain \"%s\" - %s (%d)",
3407 ServerCertificate
, cssmErrorString(err
), (int)err
);
3411 # if HAVE_SECPOLICYCREATESSL
3412 servername
= CFStringCreateWithCString(kCFAllocatorDefault
, con
->servername
,
3413 kCFStringEncodingUTF8
);
3415 policy
= SecPolicyCreateSSL(1, servername
);
3418 CFRelease(servername
);
3422 cupsdLogMessage(CUPSD_LOG_ERROR
, "Cannot create ssl policy reference");
3426 if (!(query
= CFDictionaryCreateMutable(kCFAllocatorDefault
, 0,
3427 &kCFTypeDictionaryKeyCallBacks
,
3428 &kCFTypeDictionaryValueCallBacks
)))
3430 cupsdLogMessage(CUPSD_LOG_ERROR
, "Cannot create query dictionary");
3434 CFDictionaryAddValue(query
, kSecClass
, kSecClassIdentity
);
3435 CFDictionaryAddValue(query
, kSecMatchPolicy
, policy
);
3436 CFDictionaryAddValue(query
, kSecReturnRef
, kCFBooleanTrue
);
3437 CFDictionaryAddValue(query
, kSecMatchLimit
, kSecMatchLimitOne
);
3439 err
= SecItemCopyMatching(query
, (CFTypeRef
*)&identity
);
3441 if (err
&& DNSSDHostName
)
3444 * Search for the connection server name failed; try the DNS-SD .local
3445 * hostname instead...
3448 snprintf(localname
, sizeof(localname
), "%s.local", DNSSDHostName
);
3450 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3451 "get_cdsa_certificate: Looking for certs for \"%s\"...",
3454 servername
= CFStringCreateWithCString(kCFAllocatorDefault
, localname
,
3455 kCFStringEncodingUTF8
);
3459 policy
= SecPolicyCreateSSL(1, servername
);
3462 CFRelease(servername
);
3466 cupsdLogMessage(CUPSD_LOG_ERROR
, "Cannot create ssl policy reference");
3470 CFDictionarySetValue(query
, kSecMatchPolicy
, policy
);
3472 err
= SecItemCopyMatching(query
, (CFTypeRef
*)&identity
);
3477 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3478 "Cannot find signing key in keychain \"%s\": %s (%d)",
3479 ServerCertificate
, cssmErrorString(err
), (int)err
);
3483 # elif defined(HAVE_SECIDENTITYSEARCHCREATEWITHPOLICY)
3485 * Use a policy to search for valid certificates whose common name matches the
3489 if (SecPolicySearchCreate(CSSM_CERT_X_509v3
, &CSSMOID_APPLE_TP_SSL
,
3490 NULL
, &policy_search
))
3492 cupsdLogMessage(CUPSD_LOG_ERROR
, "Cannot create a policy search reference");
3496 if (SecPolicySearchCopyNext(policy_search
, &policy
))
3498 cupsdLogMessage(CUPSD_LOG_ERROR
,
3499 "Cannot find a policy to use for searching");
3503 memset(&ssl_options
, 0, sizeof(ssl_options
));
3504 ssl_options
.Version
= CSSM_APPLE_TP_SSL_OPTS_VERSION
;
3505 ssl_options
.ServerName
= con
->servername
;
3506 ssl_options
.ServerNameLen
= strlen(con
->servername
);
3508 options
.Data
= (uint8
*)&ssl_options
;
3509 options
.Length
= sizeof(ssl_options
);
3511 if (SecPolicySetValue(policy
, &options
))
3513 cupsdLogMessage(CUPSD_LOG_ERROR
,
3514 "Cannot set policy value to use for searching");
3518 if ((err
= SecIdentitySearchCreateWithPolicy(policy
, NULL
, CSSM_KEYUSE_SIGN
,
3519 keychain
, FALSE
, &search
)))
3521 cupsdLogMessage(CUPSD_LOG_ERROR
,
3522 "Cannot create identity search reference: %s (%d)",
3523 cssmErrorString(err
), (int)err
);
3527 err
= SecIdentitySearchCopyNext(search
, &identity
);
3529 if (err
&& DNSSDHostName
)
3532 * Search for the connection server name failed; try the DNS-SD .local
3533 * hostname instead...
3536 snprintf(localname
, sizeof(localname
), "%s.local", DNSSDHostName
);
3538 ssl_options
.ServerName
= localname
;
3539 ssl_options
.ServerNameLen
= strlen(localname
);
3541 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3542 "get_cdsa_certificate: Looking for certs for \"%s\"...",
3545 if (SecPolicySetValue(policy
, &options
))
3547 cupsdLogMessage(CUPSD_LOG_ERROR
,
3548 "Cannot set policy value to use for searching");
3554 if ((err
= SecIdentitySearchCreateWithPolicy(policy
, NULL
, CSSM_KEYUSE_SIGN
,
3555 keychain
, FALSE
, &search
)))
3557 cupsdLogMessage(CUPSD_LOG_ERROR
,
3558 "Cannot create identity search reference: %s (%d)",
3559 cssmErrorString(err
), (int)err
);
3563 err
= SecIdentitySearchCopyNext(search
, &identity
);
3569 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3570 "Cannot find signing key in keychain \"%s\": %s (%d)",
3571 ServerCertificate
, cssmErrorString(err
), (int)err
);
3577 * Assume there is exactly one SecIdentity in the keychain...
3580 if ((err
= SecIdentitySearchCreate(keychain
, CSSM_KEYUSE_SIGN
, &search
)))
3582 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3583 "Cannot create identity search reference (%d)", (int)err
);
3587 if ((err
= SecIdentitySearchCopyNext(search
, &identity
)))
3589 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3590 "Cannot find signing key in keychain \"%s\": %s (%d)",
3591 ServerCertificate
, cssmErrorString(err
), (int)err
);
3594 # endif /* HAVE_SECPOLICYCREATESSL */
3596 if (CFGetTypeID(identity
) != SecIdentityGetTypeID())
3598 cupsdLogMessage(CUPSD_LOG_ERROR
, "SecIdentity CFTypeID failure!");
3602 if ((certificates
= CFArrayCreate(NULL
, (const void **)&identity
,
3603 1, &kCFTypeArrayCallBacks
)) == NULL
)
3605 cupsdLogMessage(CUPSD_LOG_ERROR
, "Cannot create certificate array");
3612 CFRelease(keychain
);
3616 CFRelease(identity
);
3618 # if HAVE_SECPOLICYCREATESSL
3623 # elif defined(HAVE_SECIDENTITYSEARCHCREATEWITHPOLICY)
3627 CFRelease(policy_search
);
3628 # endif /* HAVE_SECPOLICYCREATESSL */
3630 return (certificates
);
3632 #endif /* HAVE_CDSASSL */
3636 * 'get_file()' - Get a filename and state info.
3639 static char * /* O - Real filename */
3640 get_file(cupsd_client_t
*con
, /* I - Client connection */
3641 struct stat
*filestats
, /* O - File information */
3642 char *filename
, /* IO - Filename buffer */
3643 int len
) /* I - Buffer length */
3645 int status
; /* Status of filesystem calls */
3646 char *ptr
; /* Pointer info filename */
3647 int plen
; /* Remaining length after pointer */
3648 char language
[7]; /* Language subdirectory, if any */
3652 * Figure out the real filename...
3657 if (!strncmp(con
->uri
, "/ppd/", 5) && !strchr(con
->uri
+ 5, '/'))
3658 snprintf(filename
, len
, "%s%s", ServerRoot
, con
->uri
);
3659 else if (!strncmp(con
->uri
, "/icons/", 7) && !strchr(con
->uri
+ 7, '/'))
3661 snprintf(filename
, len
, "%s/%s", CacheDir
, con
->uri
+ 7);
3662 if (access(filename
, F_OK
) < 0)
3663 snprintf(filename
, len
, "%s/images/generic.png", DocumentRoot
);
3665 else if (!strncmp(con
->uri
, "/rss/", 5) && !strchr(con
->uri
+ 5, '/'))
3666 snprintf(filename
, len
, "%s/rss/%s", CacheDir
, con
->uri
+ 5);
3667 else if (!strncmp(con
->uri
, "/admin/conf/", 12))
3668 snprintf(filename
, len
, "%s%s", ServerRoot
, con
->uri
+ 11);
3669 else if (!strncmp(con
->uri
, "/admin/log/", 11))
3671 if (!strncmp(con
->uri
+ 11, "access_log", 10) && AccessLog
[0] == '/')
3672 strlcpy(filename
, AccessLog
, len
);
3673 else if (!strncmp(con
->uri
+ 11, "error_log", 9) && ErrorLog
[0] == '/')
3674 strlcpy(filename
, ErrorLog
, len
);
3675 else if (!strncmp(con
->uri
+ 11, "page_log", 8) && PageLog
[0] == '/')
3676 strlcpy(filename
, PageLog
, len
);
3680 else if (con
->language
)
3682 snprintf(language
, sizeof(language
), "/%s", con
->language
->language
);
3683 snprintf(filename
, len
, "%s%s%s", DocumentRoot
, language
, con
->uri
);
3686 snprintf(filename
, len
, "%s%s", DocumentRoot
, con
->uri
);
3688 if ((ptr
= strchr(filename
, '?')) != NULL
)
3692 * Grab the status for this language; if there isn't a language-specific file
3693 * then fallback to the default one...
3696 if ((status
= stat(filename
, filestats
)) != 0 && language
[0] &&
3697 strncmp(con
->uri
, "/icons/", 7) &&
3698 strncmp(con
->uri
, "/ppd/", 5) &&
3699 strncmp(con
->uri
, "/rss/", 5) &&
3700 strncmp(con
->uri
, "/admin/conf/", 12) &&
3701 strncmp(con
->uri
, "/admin/log/", 11))
3704 * Drop the country code...
3708 snprintf(filename
, len
, "%s%s%s", DocumentRoot
, language
, con
->uri
);
3710 if ((ptr
= strchr(filename
, '?')) != NULL
)
3713 if ((status
= stat(filename
, filestats
)) != 0)
3716 * Drop the language prefix and try the root directory...
3720 snprintf(filename
, len
, "%s%s", DocumentRoot
, con
->uri
);
3722 if ((ptr
= strchr(filename
, '?')) != NULL
)
3725 status
= stat(filename
, filestats
);
3730 * If we're found a directory, get the index.html file instead...
3733 if (!status
&& S_ISDIR(filestats
->st_mode
))
3736 * Make sure the URI ends with a slash...
3739 if (con
->uri
[strlen(con
->uri
) - 1] != '/')
3740 strlcat(con
->uri
, "/", sizeof(con
->uri
));
3743 * Find the directory index file, trying every language...
3748 if (status
&& language
[0])
3751 * Try a different language subset...
3755 language
[0] = '\0'; /* Strip country code */
3757 language
[0] = '\0'; /* Strip language */
3761 * Look for the index file...
3764 snprintf(filename
, len
, "%s%s%s", DocumentRoot
, language
, con
->uri
);
3766 if ((ptr
= strchr(filename
, '?')) != NULL
)
3769 ptr
= filename
+ strlen(filename
);
3770 plen
= len
- (ptr
- filename
);
3772 strlcpy(ptr
, "index.html", plen
);
3773 status
= stat(filename
, filestats
);
3778 strlcpy(ptr
, "index.class", plen
);
3779 status
= stat(filename
, filestats
);
3781 #endif /* HAVE_JAVA */
3786 strlcpy(ptr
, "index.pl", plen
);
3787 status
= stat(filename
, filestats
);
3789 #endif /* HAVE_PERL */
3794 strlcpy(ptr
, "index.php", plen
);
3795 status
= stat(filename
, filestats
);
3797 #endif /* HAVE_PHP */
3802 strlcpy(ptr
, "index.pyc", plen
);
3803 status
= stat(filename
, filestats
);
3808 strlcpy(ptr
, "index.py", plen
);
3809 status
= stat(filename
, filestats
);
3811 #endif /* HAVE_PYTHON */
3814 while (status
&& language
[0]);
3817 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3818 "get_file(con=%p(%d), filestats=%p, filename=%p, len=%d) = "
3819 "%s", con
, con
->http
.fd
, filestats
, filename
, len
,
3820 status
? "(null)" : filename
);
3830 * 'install_conf_file()' - Install a configuration file.
3833 static http_status_t
/* O - Status */
3834 install_conf_file(cupsd_client_t
*con
) /* I - Connection */
3836 char filename
[1024]; /* Configuration filename */
3837 mode_t mode
; /* Permissions */
3838 cups_file_t
*in
, /* Input file */
3839 *out
; /* Output file */
3840 char buffer
[16384]; /* Copy buffer */
3841 ssize_t bytes
; /* Number of bytes */
3845 * Open the request file...
3848 if ((in
= cupsFileOpen(con
->filename
, "rb")) == NULL
)
3850 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to open request file \"%s\": %s",
3851 con
->filename
, strerror(errno
));
3852 return (HTTP_SERVER_ERROR
);
3856 * Open the new config file...
3859 snprintf(filename
, sizeof(filename
), "%s%s", ServerRoot
, con
->uri
+ 11);
3860 if (!strcmp(con
->uri
, "/admin/conf/printers.conf"))
3861 mode
= ConfigFilePerm
& 0600;
3863 mode
= ConfigFilePerm
;
3865 if ((out
= cupsdCreateConfFile(filename
, mode
)) == NULL
)
3868 return (HTTP_SERVER_ERROR
);
3871 cupsdLogMessage(CUPSD_LOG_INFO
, "Installing config file \"%s\"...", filename
);
3874 * Copy from the request to the new config file...
3877 while ((bytes
= cupsFileRead(in
, buffer
, sizeof(buffer
))) > 0)
3878 if (cupsFileWrite(out
, buffer
, bytes
) < bytes
)
3880 cupsdLogMessage(CUPSD_LOG_ERROR
,
3881 "Unable to copy to config file \"%s\": %s",
3882 filename
, strerror(errno
));
3887 snprintf(filename
, sizeof(filename
), "%s%s.N", ServerRoot
, con
->uri
+ 11);
3888 cupsdRemoveFile(filename
);
3890 return (HTTP_SERVER_ERROR
);
3894 * Close the files...
3899 if (cupsdCloseCreatedConfFile(out
, filename
))
3900 return (HTTP_SERVER_ERROR
);
3903 * Remove the request file...
3906 cupsdRemoveFile(con
->filename
);
3907 cupsdClearString(&con
->filename
);
3910 * If the cupsd.conf file was updated, set the NeedReload flag...
3913 if (!strcmp(con
->uri
, "/admin/conf/cupsd.conf"))
3914 NeedReload
= RELOAD_CUPSD
;
3916 NeedReload
= RELOAD_ALL
;
3918 ReloadTime
= time(NULL
);
3921 * Return that the file was created successfully...
3924 return (HTTP_CREATED
);
3929 * 'is_cgi()' - Is the resource a CGI script/program?
3932 static int /* O - 1 = CGI, 0 = file */
3933 is_cgi(cupsd_client_t
*con
, /* I - Client connection */
3934 const char *filename
, /* I - Real filename */
3935 struct stat
*filestats
, /* I - File information */
3936 mime_type_t
*type
) /* I - MIME type */
3938 const char *options
; /* Options on URL */
3942 * Get the options, if any...
3945 if ((options
= strchr(con
->uri
, '?')) != NULL
)
3948 cupsdSetStringf(&(con
->query_string
), "QUERY_STRING=%s", options
);
3952 * Check for known types...
3955 if (!type
|| _cups_strcasecmp(type
->super
, "application"))
3957 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3958 "is_cgi(con=%p(%d), filename=\"%s\", filestats=%p, "
3959 "type=%s/%s) = 0", con
, con
->http
.fd
, filename
, filestats
,
3960 type
? type
->super
: "unknown",
3961 type
? type
->type
: "unknown");
3965 if (!_cups_strcasecmp(type
->type
, "x-httpd-cgi") &&
3966 (filestats
->st_mode
& 0111))
3969 * "application/x-httpd-cgi" is a CGI script.
3972 cupsdSetString(&con
->command
, filename
);
3975 cupsdSetStringf(&con
->options
, " %s", options
);
3977 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3978 "is_cgi(con=%p(%d), filename=\"%s\", filestats=%p, "
3979 "type=%s/%s) = 1", con
, con
->http
.fd
, filename
, filestats
,
3980 type
->super
, type
->type
);
3984 else if (!_cups_strcasecmp(type
->type
, "x-httpd-java"))
3987 * "application/x-httpd-java" is a Java servlet.
3990 cupsdSetString(&con
->command
, CUPS_JAVA
);
3993 cupsdSetStringf(&con
->options
, " %s %s", filename
, options
);
3995 cupsdSetStringf(&con
->options
, " %s", filename
);
3997 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3998 "is_cgi(con=%p(%d), filename=\"%s\", filestats=%p, "
3999 "type=%s/%s) = 1", con
, con
->http
.fd
, filename
, filestats
,
4000 type
->super
, type
->type
);
4003 #endif /* HAVE_JAVA */
4005 else if (!_cups_strcasecmp(type
->type
, "x-httpd-perl"))
4008 * "application/x-httpd-perl" is a Perl page.
4011 cupsdSetString(&con
->command
, CUPS_PERL
);
4014 cupsdSetStringf(&con
->options
, " %s %s", filename
, options
);
4016 cupsdSetStringf(&con
->options
, " %s", filename
);
4018 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4019 "is_cgi(con=%p(%d), filename=\"%s\", filestats=%p, "
4020 "type=%s/%s) = 1", con
, con
->http
.fd
, filename
, filestats
,
4021 type
->super
, type
->type
);
4024 #endif /* HAVE_PERL */
4026 else if (!_cups_strcasecmp(type
->type
, "x-httpd-php"))
4029 * "application/x-httpd-php" is a PHP page.
4032 cupsdSetString(&con
->command
, CUPS_PHP
);
4035 cupsdSetStringf(&con
->options
, " %s %s", filename
, options
);
4037 cupsdSetStringf(&con
->options
, " %s", filename
);
4039 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4040 "is_cgi(con=%p(%d), filename=\"%s\", filestats=%p, "
4041 "type=%s/%s) = 1", con
, con
->http
.fd
, filename
, filestats
,
4042 type
->super
, type
->type
);
4045 #endif /* HAVE_PHP */
4047 else if (!_cups_strcasecmp(type
->type
, "x-httpd-python"))
4050 * "application/x-httpd-python" is a Python page.
4053 cupsdSetString(&con
->command
, CUPS_PYTHON
);
4056 cupsdSetStringf(&con
->options
, " %s %s", filename
, options
);
4058 cupsdSetStringf(&con
->options
, " %s", filename
);
4060 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4061 "is_cgi(con=%p(%d), filename=\"%s\", filestats=%p, "
4062 "type=%s/%s) = 1", con
, con
->http
.fd
, filename
, filestats
,
4063 type
->super
, type
->type
);
4066 #endif /* HAVE_PYTHON */
4068 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4069 "is_cgi(con=%p(%d), filename=\"%s\", filestats=%p, "
4070 "type=%s/%s) = 0", con
, con
->http
.fd
, filename
, filestats
,
4071 type
->super
, type
->type
);
4077 * 'is_path_absolute()' - Is a path absolute and free of relative elements (i.e. "..").
4080 static int /* O - 0 if relative, 1 if absolute */
4081 is_path_absolute(const char *path
) /* I - Input path */
4084 * Check for a leading slash...
4091 * Check for "/.." in the path...
4094 while ((path
= strstr(path
, "/..")) != NULL
)
4096 if (!path
[3] || path
[3] == '/')
4103 * If we haven't found any relative paths, return 1 indicating an
4113 * 'make_certificate()' - Make a self-signed SSL/TLS certificate.
4116 static int /* O - 1 on success, 0 on failure */
4117 make_certificate(cupsd_client_t
*con
) /* I - Client connection */
4119 #if defined(HAVE_LIBSSL) && defined(HAVE_WAITPID)
4120 int pid
, /* Process ID of command */
4121 status
; /* Status of command */
4122 char command
[1024], /* Command */
4123 *argv
[12], /* Command-line arguments */
4124 *envp
[MAX_ENV
+ 1], /* Environment variables */
4125 infofile
[1024], /* Type-in information for cert */
4126 seedfile
[1024]; /* Random number seed file */
4127 int envc
, /* Number of environment variables */
4128 bytes
; /* Bytes written */
4129 cups_file_t
*fp
; /* Seed/info file */
4130 int infofd
; /* Info file descriptor */
4134 * Run the "openssl" command to seed the random number generator and
4135 * generate a self-signed certificate that is good for 10 years:
4137 * openssl rand -rand seedfile 1
4139 * openssl req -new -x509 -keyout ServerKey \
4140 * -out ServerCertificate -days 3650 -nodes
4142 * The seeding step is crucial in ensuring that the openssl command
4143 * does not block on systems without sufficient entropy...
4146 if (!cupsFileFind("openssl", getenv("PATH"), 1, command
, sizeof(command
)))
4148 cupsdLogMessage(CUPSD_LOG_ERROR
,
4149 "No SSL certificate and openssl command not found!");
4153 if (access("/dev/urandom", 0))
4156 * If the system doesn't provide /dev/urandom, then any random source
4157 * will probably be blocking-style, so generate some random data to
4158 * use as a seed for the certificate. Note that we have already
4159 * seeded the random number generator in cupsdInitCerts()...
4162 cupsdLogMessage(CUPSD_LOG_INFO
,
4163 "Seeding the random number generator...");
4166 * Write the seed file...
4169 if ((fp
= cupsTempFile2(seedfile
, sizeof(seedfile
))) == NULL
)
4171 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to create seed file %s - %s",
4172 seedfile
, strerror(errno
));
4176 for (bytes
= 0; bytes
< 262144; bytes
++)
4177 cupsFilePutChar(fp
, random());
4182 * Run the openssl command to seed its random number generator...
4185 argv
[0] = "openssl";
4192 envc
= cupsdLoadEnv(envp
, MAX_ENV
);
4195 if (!cupsdStartProcess(command
, argv
, envp
, -1, -1, -1, -1, -1, 1, NULL
,
4202 while (waitpid(pid
, &status
, 0) < 0)
4209 cupsdFinishProcess(pid
, command
, sizeof(command
), NULL
);
4212 * Remove the seed file, as it is no longer needed...
4219 if (WIFEXITED(status
))
4220 cupsdLogMessage(CUPSD_LOG_ERROR
,
4221 "Unable to seed random number generator - "
4222 "the openssl command stopped with status %d!",
4223 WEXITSTATUS(status
));
4225 cupsdLogMessage(CUPSD_LOG_ERROR
,
4226 "Unable to seed random number generator - "
4227 "the openssl command crashed on signal %d!",
4235 * Create a file with the certificate information fields...
4237 * Note: This assumes that the default questions are asked by the openssl
4241 if ((fp
= cupsTempFile2(infofile
, sizeof(infofile
))) == NULL
)
4243 cupsdLogMessage(CUPSD_LOG_ERROR
,
4244 "Unable to create certificate information file %s - %s",
4245 infofile
, strerror(errno
));
4249 cupsFilePrintf(fp
, ".\n.\n.\n%s\n.\n%s\n%s\n",
4250 ServerName
, ServerName
, ServerAdmin
);
4253 cupsdLogMessage(CUPSD_LOG_INFO
,
4254 "Generating SSL server key and certificate...");
4256 argv
[0] = "openssl";
4260 argv
[4] = "-keyout";
4261 argv
[5] = ServerKey
;
4263 argv
[7] = ServerCertificate
;
4266 argv
[10] = "-nodes";
4269 cupsdLoadEnv(envp
, MAX_ENV
);
4271 infofd
= open(infofile
, O_RDONLY
);
4273 if (!cupsdStartProcess(command
, argv
, envp
, infofd
, -1, -1, -1, -1, 1, NULL
,
4284 while (waitpid(pid
, &status
, 0) < 0)
4291 cupsdFinishProcess(pid
, command
, sizeof(command
), NULL
);
4295 if (WIFEXITED(status
))
4296 cupsdLogMessage(CUPSD_LOG_ERROR
,
4297 "Unable to create SSL server key and certificate - "
4298 "the openssl command stopped with status %d!",
4299 WEXITSTATUS(status
));
4301 cupsdLogMessage(CUPSD_LOG_ERROR
,
4302 "Unable to create SSL server key and certificate - "
4303 "the openssl command crashed on signal %d!",
4308 cupsdLogMessage(CUPSD_LOG_INFO
, "Created SSL server key file \"%s\"...",
4310 cupsdLogMessage(CUPSD_LOG_INFO
,
4311 "Created SSL server certificate file \"%s\"...",
4317 #elif defined(HAVE_GNUTLS)
4318 gnutls_x509_crt crt
; /* Self-signed certificate */
4319 gnutls_x509_privkey key
; /* Encryption key */
4320 cups_lang_t
*language
; /* Default language info */
4321 cups_file_t
*fp
; /* Key/cert file */
4322 unsigned char buffer
[8192]; /* Buffer for x509 data */
4323 size_t bytes
; /* Number of bytes of data */
4324 unsigned char serial
[4]; /* Serial number buffer */
4325 time_t curtime
; /* Current time */
4326 int result
; /* Result of GNU TLS calls */
4330 * Create the encryption key...
4333 cupsdLogMessage(CUPSD_LOG_INFO
, "Generating SSL server key...");
4335 gnutls_x509_privkey_init(&key
);
4336 gnutls_x509_privkey_generate(key
, GNUTLS_PK_RSA
, 2048, 0);
4342 bytes
= sizeof(buffer
);
4344 if ((result
= gnutls_x509_privkey_export(key
, GNUTLS_X509_FMT_PEM
,
4345 buffer
, &bytes
)) < 0)
4347 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to export SSL server key - %s",
4348 gnutls_strerror(result
));
4349 gnutls_x509_privkey_deinit(key
);
4352 else if ((fp
= cupsFileOpen(ServerKey
, "w")) != NULL
)
4354 cupsFileWrite(fp
, (char *)buffer
, bytes
);
4357 cupsdLogMessage(CUPSD_LOG_INFO
, "Created SSL server key file \"%s\"...",
4362 cupsdLogMessage(CUPSD_LOG_ERROR
,
4363 "Unable to create SSL server key file \"%s\" - %s",
4364 ServerKey
, strerror(errno
));
4365 gnutls_x509_privkey_deinit(key
);
4370 * Create the self-signed certificate...
4373 cupsdLogMessage(CUPSD_LOG_INFO
, "Generating self-signed SSL certificate...");
4375 language
= cupsLangDefault();
4376 curtime
= time(NULL
);
4377 serial
[0] = curtime
>> 24;
4378 serial
[1] = curtime
>> 16;
4379 serial
[2] = curtime
>> 8;
4380 serial
[3] = curtime
;
4382 gnutls_x509_crt_init(&crt
);
4383 if (strlen(language
->language
) == 5)
4384 gnutls_x509_crt_set_dn_by_oid(crt
, GNUTLS_OID_X520_COUNTRY_NAME
, 0,
4385 language
->language
+ 3, 2);
4387 gnutls_x509_crt_set_dn_by_oid(crt
, GNUTLS_OID_X520_COUNTRY_NAME
, 0,
4389 gnutls_x509_crt_set_dn_by_oid(crt
, GNUTLS_OID_X520_COMMON_NAME
, 0,
4390 ServerName
, strlen(ServerName
));
4391 gnutls_x509_crt_set_dn_by_oid(crt
, GNUTLS_OID_X520_ORGANIZATION_NAME
, 0,
4392 ServerName
, strlen(ServerName
));
4393 gnutls_x509_crt_set_dn_by_oid(crt
, GNUTLS_OID_X520_ORGANIZATIONAL_UNIT_NAME
,
4395 gnutls_x509_crt_set_dn_by_oid(crt
, GNUTLS_OID_X520_STATE_OR_PROVINCE_NAME
, 0,
4397 gnutls_x509_crt_set_dn_by_oid(crt
, GNUTLS_OID_X520_LOCALITY_NAME
, 0,
4399 gnutls_x509_crt_set_dn_by_oid(crt
, GNUTLS_OID_PKCS9_EMAIL
, 0,
4400 ServerAdmin
, strlen(ServerAdmin
));
4401 gnutls_x509_crt_set_key(crt
, key
);
4402 gnutls_x509_crt_set_serial(crt
, serial
, sizeof(serial
));
4403 gnutls_x509_crt_set_activation_time(crt
, curtime
);
4404 gnutls_x509_crt_set_expiration_time(crt
, curtime
+ 10 * 365 * 86400);
4405 gnutls_x509_crt_set_ca_status(crt
, 0);
4406 gnutls_x509_crt_set_subject_alternative_name(crt
, GNUTLS_SAN_DNSNAME
,
4408 gnutls_x509_crt_set_key_purpose_oid(crt
, GNUTLS_KP_TLS_WWW_SERVER
, 0);
4409 gnutls_x509_crt_set_key_usage(crt
, GNUTLS_KEY_KEY_ENCIPHERMENT
);
4410 gnutls_x509_crt_set_version(crt
, 3);
4412 bytes
= sizeof(buffer
);
4413 if (gnutls_x509_crt_get_key_id(crt
, 0, buffer
, &bytes
) >= 0)
4414 gnutls_x509_crt_set_subject_key_id(crt
, buffer
, bytes
);
4416 gnutls_x509_crt_sign(crt
, crt
, key
);
4422 bytes
= sizeof(buffer
);
4423 if ((result
= gnutls_x509_crt_export(crt
, GNUTLS_X509_FMT_PEM
,
4424 buffer
, &bytes
)) < 0)
4425 cupsdLogMessage(CUPSD_LOG_ERROR
,
4426 "Unable to export SSL server certificate - %s",
4427 gnutls_strerror(result
));
4428 else if ((fp
= cupsFileOpen(ServerCertificate
, "w")) != NULL
)
4430 cupsFileWrite(fp
, (char *)buffer
, bytes
);
4433 cupsdLogMessage(CUPSD_LOG_INFO
,
4434 "Created SSL server certificate file \"%s\"...",
4438 cupsdLogMessage(CUPSD_LOG_ERROR
,
4439 "Unable to create SSL server certificate file \"%s\" - %s",
4440 ServerCertificate
, strerror(errno
));
4446 gnutls_x509_crt_deinit(crt
);
4447 gnutls_x509_privkey_deinit(key
);
4451 #elif defined(HAVE_CDSASSL) && defined(HAVE_WAITPID)
4452 int pid
, /* Process ID of command */
4453 status
; /* Status of command */
4454 char command
[1024], /* Command */
4455 *argv
[4], /* Command-line arguments */
4456 *envp
[MAX_ENV
+ 1], /* Environment variables */
4457 keychain
[1024], /* Keychain argument */
4458 infofile
[1024], /* Type-in information for cert */
4459 localname
[1024], /* Local hostname */
4460 *servername
; /* Name of server in cert */
4461 cups_file_t
*fp
; /* Seed/info file */
4462 int infofd
; /* Info file descriptor */
4465 if (con
->servername
&& isdigit(con
->servername
[0] & 255) && DNSSDHostName
)
4467 snprintf(localname
, sizeof(localname
), "%s.local", DNSSDHostName
);
4468 servername
= localname
;
4471 servername
= con
->servername
;
4474 * Run the "certtool" command to generate a self-signed certificate...
4477 if (!cupsFileFind("certtool", getenv("PATH"), 1, command
, sizeof(command
)))
4479 cupsdLogMessage(CUPSD_LOG_ERROR
,
4480 "No SSL certificate and certtool command not found!");
4485 * Create a file with the certificate information fields...
4487 * Note: This assumes that the default questions are asked by the certtool
4491 if ((fp
= cupsTempFile2(infofile
, sizeof(infofile
))) == NULL
)
4493 cupsdLogMessage(CUPSD_LOG_ERROR
,
4494 "Unable to create certificate information file %s - %s",
4495 infofile
, strerror(errno
));
4499 cupsFilePrintf(fp
, "%s\nr\n\ny\nb\ns\ny\n%s\n\n\n\n\n%s\ny\n",
4500 servername
, servername
, ServerAdmin
);
4503 cupsdLogMessage(CUPSD_LOG_INFO
,
4504 "Generating SSL server key and certificate...");
4506 snprintf(keychain
, sizeof(keychain
), "k=%s", ServerCertificate
);
4508 argv
[0] = "certtool";
4513 cupsdLoadEnv(envp
, MAX_ENV
);
4515 infofd
= open(infofile
, O_RDONLY
);
4517 if (!cupsdStartProcess(command
, argv
, envp
, infofd
, -1, -1, -1, -1, 1, NULL
,
4528 while (waitpid(pid
, &status
, 0) < 0)
4535 cupsdFinishProcess(pid
, command
, sizeof(command
), NULL
);
4539 if (WIFEXITED(status
))
4540 cupsdLogMessage(CUPSD_LOG_ERROR
,
4541 "Unable to create SSL server key and certificate - "
4542 "the certtool command stopped with status %d!",
4543 WEXITSTATUS(status
));
4545 cupsdLogMessage(CUPSD_LOG_ERROR
,
4546 "Unable to create SSL server key and certificate - "
4547 "the certtool command crashed on signal %d!",
4552 cupsdLogMessage(CUPSD_LOG_INFO
,
4553 "Created SSL server certificate file \"%s\"...",
4561 #endif /* HAVE_LIBSSL && HAVE_WAITPID */
4563 #endif /* HAVE_SSL */
4567 * 'pipe_command()' - Pipe the output of a command to the remote client.
4570 static int /* O - Process ID */
4571 pipe_command(cupsd_client_t
*con
, /* I - Client connection */
4572 int infile
, /* I - Standard input for command */
4573 int *outfile
, /* O - Standard output for command */
4574 char *command
, /* I - Command to run */
4575 char *options
, /* I - Options for command */
4576 int root
) /* I - Run as root? */
4578 int i
; /* Looping var */
4579 int pid
; /* Process ID */
4580 char *commptr
, /* Command string pointer */
4581 commch
; /* Command string character */
4582 char *uriptr
; /* URI string pointer */
4583 int fds
[2]; /* Pipe FDs */
4584 int argc
; /* Number of arguments */
4585 int envc
; /* Number of environment variables */
4586 char argbuf
[10240], /* Argument buffer */
4587 *argv
[100], /* Argument strings */
4588 *envp
[MAX_ENV
+ 20]; /* Environment variables */
4589 char auth_type
[256], /* AUTH_TYPE environment variable */
4590 content_length
[1024], /* CONTENT_LENGTH environment variable */
4591 content_type
[1024], /* CONTENT_TYPE environment variable */
4592 http_cookie
[32768], /* HTTP_COOKIE environment variable */
4593 http_referer
[1024], /* HTTP_REFERER environment variable */
4594 http_user_agent
[1024], /* HTTP_USER_AGENT environment variable */
4595 lang
[1024], /* LANG environment variable */
4596 path_info
[1024], /* PATH_INFO environment variable */
4597 remote_addr
[1024], /* REMOTE_ADDR environment variable */
4598 remote_host
[1024], /* REMOTE_HOST environment variable */
4599 remote_user
[1024], /* REMOTE_USER environment variable */
4600 script_filename
[1024], /* SCRIPT_FILENAME environment variable */
4601 script_name
[1024], /* SCRIPT_NAME environment variable */
4602 server_name
[1024], /* SERVER_NAME environment variable */
4603 server_port
[1024]; /* SERVER_PORT environment variable */
4604 ipp_attribute_t
*attr
; /* attributes-natural-language attribute */
4605 void *ccache
= NULL
; /* Kerberos credentials */
4609 * Parse a copy of the options string, which is of the form:
4611 * argument+argument+argument
4612 * ?argument+argument+argument
4613 * param=value¶m=value
4614 * ?param=value¶m=value
4615 * /name?argument+argument+argument
4616 * /name?param=value¶m=value
4618 * If the string contains an "=" character after the initial name,
4619 * then we treat it as a HTTP GET form request and make a copy of
4620 * the remaining string for the environment variable.
4622 * The string is always parsed out as command-line arguments, to
4623 * be consistent with Apache...
4626 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4627 "pipe_command(con=%p(%d), infile=%d, outfile=%p, "
4628 "command=\"%s\", options=\"%s\", root=%d)",
4629 con
, con
->http
.fd
, infile
, outfile
, command
,
4630 options
? options
: "(null)", root
);
4637 if (*commptr
== ' ')
4639 strlcpy(argbuf
, commptr
, sizeof(argbuf
));
4644 if (argbuf
[0] == '/')
4647 * Found some trailing path information, set PATH_INFO...
4650 if ((commptr
= strchr(argbuf
, '?')) == NULL
)
4651 commptr
= argbuf
+ strlen(argbuf
);
4655 snprintf(path_info
, sizeof(path_info
), "PATH_INFO=%s", argbuf
);
4661 path_info
[0] = '\0';
4663 if (*commptr
== ' ')
4667 if (*commptr
== '?' && con
->operation
== HTTP_GET
&& !con
->query_string
)
4670 cupsdSetStringf(&(con
->query_string
), "QUERY_STRING=%s", commptr
);
4677 argv
[argc
++] = commptr
;
4679 for (; *commptr
&& argc
< 99; commptr
++)
4682 * Break arguments whenever we see a + or space...
4685 if (*commptr
== ' ' || *commptr
== '+')
4687 while (*commptr
== ' ' || *commptr
== '+')
4691 * If we don't have a blank string, save it as another argument...
4696 argv
[argc
] = commptr
;
4702 else if (*commptr
== '%' && isxdigit(commptr
[1] & 255) &&
4703 isxdigit(commptr
[2] & 255))
4706 * Convert the %xx notation to the individual character.
4709 if (commptr
[1] >= '0' && commptr
[1] <= '9')
4710 *commptr
= (commptr
[1] - '0') << 4;
4712 *commptr
= (tolower(commptr
[1]) - 'a' + 10) << 4;
4714 if (commptr
[2] >= '0' && commptr
[2] <= '9')
4715 *commptr
|= commptr
[2] - '0';
4717 *commptr
|= tolower(commptr
[2]) - 'a' + 10;
4719 _cups_strcpy(commptr
+ 1, commptr
+ 3);
4722 * Check for a %00 and break if that is the case...
4734 * Setup the environment variables as needed...
4737 if (con
->username
[0])
4739 snprintf(auth_type
, sizeof(auth_type
), "AUTH_TYPE=%s",
4740 httpGetField(HTTP(con
), HTTP_FIELD_AUTHORIZATION
));
4742 if ((uriptr
= strchr(auth_type
+ 10, ' ')) != NULL
)
4746 auth_type
[0] = '\0';
4749 (attr
= ippFindAttribute(con
->request
, "attributes-natural-language",
4750 IPP_TAG_LANGUAGE
)) != NULL
)
4752 switch (strlen(attr
->values
[0].string
.text
))
4756 * This is an unknown or badly formatted language code; use
4757 * the POSIX locale...
4760 strcpy(lang
, "LANG=C");
4765 * Just the language code (ll)...
4768 snprintf(lang
, sizeof(lang
), "LANG=%s.UTF8",
4769 attr
->values
[0].string
.text
);
4774 * Language and country code (ll-cc)...
4777 snprintf(lang
, sizeof(lang
), "LANG=%c%c_%c%c.UTF8",
4778 attr
->values
[0].string
.text
[0],
4779 attr
->values
[0].string
.text
[1],
4780 toupper(attr
->values
[0].string
.text
[3] & 255),
4781 toupper(attr
->values
[0].string
.text
[4] & 255));
4785 else if (con
->language
)
4786 snprintf(lang
, sizeof(lang
), "LANG=%s.UTF8", con
->language
->language
);
4788 strcpy(lang
, "LANG=C");
4790 strcpy(remote_addr
, "REMOTE_ADDR=");
4791 httpAddrString(con
->http
.hostaddr
, remote_addr
+ 12,
4792 sizeof(remote_addr
) - 12);
4794 snprintf(remote_host
, sizeof(remote_host
), "REMOTE_HOST=%s",
4795 con
->http
.hostname
);
4797 snprintf(script_name
, sizeof(script_name
), "SCRIPT_NAME=%s", con
->uri
);
4798 if ((uriptr
= strchr(script_name
, '?')) != NULL
)
4801 snprintf(script_filename
, sizeof(script_filename
), "SCRIPT_FILENAME=%s%s",
4802 DocumentRoot
, script_name
+ 12);
4804 sprintf(server_port
, "SERVER_PORT=%d", con
->serverport
);
4806 if (con
->http
.fields
[HTTP_FIELD_HOST
][0])
4808 char *nameptr
; /* Pointer to ":port" */
4810 snprintf(server_name
, sizeof(server_name
), "SERVER_NAME=%s",
4811 con
->http
.fields
[HTTP_FIELD_HOST
]);
4812 if ((nameptr
= strrchr(server_name
, ':')) != NULL
&& !strchr(nameptr
, ']'))
4813 *nameptr
= '\0'; /* Strip trailing ":port" */
4816 snprintf(server_name
, sizeof(server_name
), "SERVER_NAME=%s",
4819 envc
= cupsdLoadEnv(envp
, (int)(sizeof(envp
) / sizeof(envp
[0])));
4822 envp
[envc
++] = auth_type
;
4824 envp
[envc
++] = lang
;
4825 envp
[envc
++] = "REDIRECT_STATUS=1";
4826 envp
[envc
++] = "GATEWAY_INTERFACE=CGI/1.1";
4827 envp
[envc
++] = server_name
;
4828 envp
[envc
++] = server_port
;
4829 envp
[envc
++] = remote_addr
;
4830 envp
[envc
++] = remote_host
;
4831 envp
[envc
++] = script_name
;
4832 envp
[envc
++] = script_filename
;
4835 envp
[envc
++] = path_info
;
4837 if (con
->username
[0])
4839 snprintf(remote_user
, sizeof(remote_user
), "REMOTE_USER=%s", con
->username
);
4841 envp
[envc
++] = remote_user
;
4844 if (con
->http
.version
== HTTP_1_1
)
4845 envp
[envc
++] = "SERVER_PROTOCOL=HTTP/1.1";
4846 else if (con
->http
.version
== HTTP_1_0
)
4847 envp
[envc
++] = "SERVER_PROTOCOL=HTTP/1.0";
4849 envp
[envc
++] = "SERVER_PROTOCOL=HTTP/0.9";
4851 if (con
->http
.cookie
)
4853 snprintf(http_cookie
, sizeof(http_cookie
), "HTTP_COOKIE=%s",
4855 envp
[envc
++] = http_cookie
;
4858 if (con
->http
.fields
[HTTP_FIELD_USER_AGENT
][0])
4860 snprintf(http_user_agent
, sizeof(http_user_agent
), "HTTP_USER_AGENT=%s",
4861 con
->http
.fields
[HTTP_FIELD_USER_AGENT
]);
4862 envp
[envc
++] = http_user_agent
;
4865 if (con
->http
.fields
[HTTP_FIELD_REFERER
][0])
4867 snprintf(http_referer
, sizeof(http_referer
), "HTTP_REFERER=%s",
4868 con
->http
.fields
[HTTP_FIELD_REFERER
]);
4869 envp
[envc
++] = http_referer
;
4872 if (con
->operation
== HTTP_GET
)
4874 envp
[envc
++] = "REQUEST_METHOD=GET";
4876 if (con
->query_string
)
4879 * Add GET form variables after ?...
4882 envp
[envc
++] = con
->query_string
;
4885 envp
[envc
++] = "QUERY_STRING=";
4889 sprintf(content_length
, "CONTENT_LENGTH=" CUPS_LLFMT
,
4890 CUPS_LLCAST con
->bytes
);
4891 snprintf(content_type
, sizeof(content_type
), "CONTENT_TYPE=%s",
4892 con
->http
.fields
[HTTP_FIELD_CONTENT_TYPE
]);
4894 envp
[envc
++] = "REQUEST_METHOD=POST";
4895 envp
[envc
++] = content_length
;
4896 envp
[envc
++] = content_type
;
4900 * Tell the CGI if we are using encryption...
4904 envp
[envc
++] = "HTTPS=ON";
4907 * Terminate the environment array...
4912 if (LogLevel
>= CUPSD_LOG_DEBUG
)
4914 for (i
= 0; i
< argc
; i
++)
4915 cupsdLogMessage(CUPSD_LOG_DEBUG
,
4916 "[CGI] argv[%d] = \"%s\"", i
, argv
[i
]);
4917 for (i
= 0; i
< envc
; i
++)
4918 cupsdLogMessage(CUPSD_LOG_DEBUG
,
4919 "[CGI] envp[%d] = \"%s\"", i
, envp
[i
]);
4923 * Create a pipe for the output...
4926 if (cupsdOpenPipe(fds
))
4928 cupsdLogMessage(CUPSD_LOG_ERROR
, "[CGI] Unable to create pipe for %s - %s",
4929 argv
[0], strerror(errno
));
4934 * Then execute the command...
4937 if (cupsdStartProcess(command
, argv
, envp
, infile
, fds
[1], CGIPipes
[1],
4938 -1, -1, root
, DefaultProfile
, NULL
, &pid
) < 0)
4941 * Error - can't fork!
4944 cupsdLogMessage(CUPSD_LOG_ERROR
, "[CGI] Unable to start %s - %s", argv
[0],
4947 cupsdClosePipe(fds
);
4953 * Fork successful - return the PID...
4956 if (con
->username
[0])
4957 cupsdAddCert(pid
, con
->username
, ccache
);
4959 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[CGI] Started %s (PID %d)", command
, pid
);
4970 * 'valid_host()' - Is the Host: field valid?
4973 static int /* O - 1 if valid, 0 if not */
4974 valid_host(cupsd_client_t
*con
) /* I - Client connection */
4976 cupsd_alias_t
*a
; /* Current alias */
4977 cupsd_netif_t
*netif
; /* Current network interface */
4978 const char *host
, /* Host field */
4979 *end
; /* End character */
4982 host
= con
->http
.fields
[HTTP_FIELD_HOST
];
4984 if (httpAddrLocalhost(con
->http
.hostaddr
))
4987 * Only allow "localhost" or the equivalent IPv4 or IPv6 numerical
4988 * addresses when accessing CUPS via the loopback interface...
4991 return (!_cups_strcasecmp(host
, "localhost") ||
4992 !_cups_strncasecmp(host
, "localhost:", 10) ||
4993 !_cups_strcasecmp(host
, "localhost.") ||
4994 !_cups_strncasecmp(host
, "localhost.:", 11) ||
4996 !_cups_strcasecmp(host
, "localhost.localdomain") ||
4997 !_cups_strncasecmp(host
, "localhost.localdomain:", 22) ||
4998 #endif /* __linux */
4999 !strcmp(host
, "127.0.0.1") ||
5000 !strncmp(host
, "127.0.0.1:", 10) ||
5001 !strcmp(host
, "[::1]") ||
5002 !strncmp(host
, "[::1]:", 6));
5007 * Check if the hostname is something.local (Bonjour); if so, allow it.
5010 if ((end
= strrchr(host
, '.')) != NULL
&&
5011 (!_cups_strcasecmp(end
, ".local") || !_cups_strncasecmp(end
, ".local:", 7) ||
5012 !_cups_strcasecmp(end
, ".local.") || !_cups_strncasecmp(end
, ".local.:", 8)))
5014 #endif /* HAVE_DNSSD */
5017 * Check if the hostname is an IP address...
5020 if (isdigit(*host
& 255) || *host
== '[')
5023 * Possible IPv4/IPv6 address...
5026 char temp
[1024], /* Temporary string */
5027 *ptr
; /* Pointer into temporary string */
5028 http_addrlist_t
*addrlist
; /* List of addresses */
5031 strlcpy(temp
, host
, sizeof(temp
));
5032 if ((ptr
= strrchr(temp
, ':')) != NULL
&& !strchr(ptr
, ']'))
5033 *ptr
= '\0'; /* Strip :port from host value */
5035 if ((addrlist
= httpAddrGetList(temp
, AF_UNSPEC
, NULL
)) != NULL
)
5038 * Good IPv4/IPv6 address...
5041 httpAddrFreeList(addrlist
);
5047 * Check for (alias) name matches...
5050 for (a
= (cupsd_alias_t
*)cupsArrayFirst(ServerAlias
);
5052 a
= (cupsd_alias_t
*)cupsArrayNext(ServerAlias
))
5055 * "ServerAlias *" allows all host values through...
5058 if (!strcmp(a
->name
, "*"))
5061 if (!_cups_strncasecmp(host
, a
->name
, a
->namelen
))
5064 * Prefix matches; check the character at the end - it must be ":", ".",
5068 end
= host
+ a
->namelen
;
5070 if (!*end
|| *end
== ':' || (*end
== '.' && (!end
[1] || end
[1] == ':')))
5076 for (a
= (cupsd_alias_t
*)cupsArrayFirst(DNSSDAlias
);
5078 a
= (cupsd_alias_t
*)cupsArrayNext(DNSSDAlias
))
5081 * "ServerAlias *" allows all host values through...
5084 if (!strcmp(a
->name
, "*"))
5087 if (!_cups_strncasecmp(host
, a
->name
, a
->namelen
))
5090 * Prefix matches; check the character at the end - it must be ":", ".",
5094 end
= host
+ a
->namelen
;
5096 if (!*end
|| *end
== ':' || (*end
== '.' && (!end
[1] || end
[1] == ':')))
5100 #endif /* HAVE_DNSSD */
5103 * Check for interface hostname matches...
5106 for (netif
= (cupsd_netif_t
*)cupsArrayFirst(NetIFList
);
5108 netif
= (cupsd_netif_t
*)cupsArrayNext(NetIFList
))
5110 if (!_cups_strncasecmp(host
, netif
->hostname
, netif
->hostlen
))
5113 * Prefix matches; check the character at the end - it must be ":", ".",
5117 end
= host
+ netif
->hostlen
;
5119 if (!*end
|| *end
== ':' || (*end
== '.' && (!end
[1] || end
[1] == ':')))
5129 * 'write_file()' - Send a file via HTTP.
5132 static int /* O - 0 on failure, 1 on success */
5133 write_file(cupsd_client_t
*con
, /* I - Client connection */
5134 http_status_t code
, /* I - HTTP status */
5135 char *filename
, /* I - Filename */
5136 char *type
, /* I - File type */
5137 struct stat
*filestats
) /* O - File information */
5139 con
->file
= open(filename
, O_RDONLY
);
5141 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
5142 "write_file(con=%p(%d), code=%d, filename=\"%s\" (%d), "
5143 "type=\"%s\", filestats=%p)", con
, con
->http
.fd
,
5144 code
, filename
, con
->file
, type
? type
: "(null)", filestats
);
5149 fcntl(con
->file
, F_SETFD
, fcntl(con
->file
, F_GETFD
) | FD_CLOEXEC
);
5153 if (!cupsdSendHeader(con
, code
, type
, CUPSD_AUTH_NONE
))
5156 if (httpPrintf(HTTP(con
), "Last-Modified: %s\r\n",
5157 httpGetDateString(filestats
->st_mtime
)) < 0)
5159 if (httpPrintf(HTTP(con
), "Content-Length: " CUPS_LLFMT
"\r\n",
5160 CUPS_LLCAST filestats
->st_size
) < 0)
5162 if (httpPrintf(HTTP(con
), "\r\n") < 0)
5165 if (cupsdFlushHeader(con
) < 0)
5168 con
->http
.data_encoding
= HTTP_ENCODE_LENGTH
;
5169 con
->http
.data_remaining
= filestats
->st_size
;
5171 if (con
->http
.data_remaining
<= INT_MAX
)
5172 con
->http
._data_remaining
= con
->http
.data_remaining
;
5174 con
->http
._data_remaining
= INT_MAX
;
5176 cupsdAddSelect(con
->http
.fd
, (cupsd_selfunc_t
)cupsdReadClient
,
5177 (cupsd_selfunc_t
)cupsdWriteClient
, con
);
5184 * 'write_pipe()' - Flag that data is available on the CGI pipe.
5188 write_pipe(cupsd_client_t
*con
) /* I - Client connection */
5190 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
5191 "write_pipe(con=%p(%d)) CGI output on fd %d",
5192 con
, con
->http
.fd
, con
->file
);
5194 con
->file_ready
= 1;
5196 cupsdRemoveSelect(con
->file
);
5197 cupsdAddSelect(con
->http
.fd
, NULL
, (cupsd_selfunc_t
)cupsdWriteClient
, con
);
5202 * End of "$Id: client.c 7950 2008-09-17 00:21:59Z mike $".