4 * Client routines for the CUPS scheduler.
6 * Copyright 2007-2013 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 * Include necessary headers...
23 #define _CUPS_NO_DEPRECATED
24 #define _HTTP_NO_PRIVATE
29 #endif /* __APPLE__ */
32 #endif /* HAVE_TCPD_H */
39 static int check_if_modified(cupsd_client_t
*con
,
40 struct stat
*filestats
);
41 static int compare_clients(cupsd_client_t
*a
, cupsd_client_t
*b
,
44 static int cupsd_start_tls(cupsd_client_t
*con
, http_encryption_t e
);
46 static char *get_file(cupsd_client_t
*con
, struct stat
*filestats
,
47 char *filename
, int len
);
48 static http_status_t
install_cupsd_conf(cupsd_client_t
*con
);
49 static int is_cgi(cupsd_client_t
*con
, const char *filename
,
50 struct stat
*filestats
, mime_type_t
*type
);
51 static int is_path_absolute(const char *path
);
52 static int pipe_command(cupsd_client_t
*con
, int infile
, int *outfile
,
53 char *command
, char *options
, int root
);
54 static int valid_host(cupsd_client_t
*con
);
55 static int write_file(cupsd_client_t
*con
, http_status_t code
,
56 char *filename
, char *type
,
57 struct stat
*filestats
);
58 static void write_pipe(cupsd_client_t
*con
);
62 * 'cupsdAcceptClient()' - Accept a new client.
66 cupsdAcceptClient(cupsd_listener_t
*lis
)/* I - Listener socket */
68 const char *hostname
; /* Hostname of client */
69 char name
[256]; /* Hostname of client */
70 int count
; /* Count of connections on a host */
71 cupsd_client_t
*con
, /* New client pointer */
72 *tempcon
; /* Temporary client pointer */
73 socklen_t addrlen
; /* Length of address */
74 http_addr_t temp
; /* Temporary address variable */
75 static time_t last_dos
= 0; /* Time of last DoS attack */
77 struct request_info wrap_req
; /* TCP wrappers request information */
78 #endif /* HAVE_TCPD_H */
81 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
82 "cupsdAcceptClient(lis=%p(%d)) Clients=%d",
83 lis
, lis
->fd
, cupsArrayCount(Clients
));
86 * Make sure we don't have a full set of clients already...
89 if (cupsArrayCount(Clients
) == MaxClients
)
93 * Get a pointer to the next available client...
97 Clients
= cupsArrayNew(NULL
, NULL
);
101 cupsdLogMessage(CUPSD_LOG_ERROR
,
102 "Unable to allocate memory for clients array!");
103 cupsdPauseListening();
108 ActiveClients
= cupsArrayNew((cups_array_func_t
)compare_clients
, NULL
);
112 cupsdLogMessage(CUPSD_LOG_ERROR
,
113 "Unable to allocate memory for active clients array!");
114 cupsdPauseListening();
118 if ((con
= calloc(1, sizeof(cupsd_client_t
))) == NULL
)
120 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to allocate memory for client!");
121 cupsdPauseListening();
126 * Accept the client and get the remote address...
129 con
->number
= ++ LastClientNumber
;
132 if ((con
->http
= httpAcceptConnection(lis
->fd
, 0)) == NULL
)
134 if (errno
== ENFILE
|| errno
== EMFILE
)
135 cupsdPauseListening();
137 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to accept client connection - %s.",
145 * Save the connected address and port number...
148 con
->clientaddr
= lis
->address
;
150 #if 0 /* ifdef AF_INET6 */
151 /* FIXME: I don't believe this is recommended any longer, and we specifically
152 * disable IPv4-over-IPv6 when we listen...
155 * Convert IPv4 over IPv6 addresses (::ffff:n.n.n.n) to IPv4 forms we
156 * can more easily use...
159 if (lis
->address
.addr
.sa_family
== AF_INET6
&&
160 httpGetAddress(con
->http
)->ipv6
.sin6_addr
.s6_addr32
[0] == 0 &&
161 httpGetAddress(con
->http
)->ipv6
.sin6_addr
.s6_addr32
[1] == 0 &&
162 ntohl(httpGetAddress(con
->http
)->ipv6
.sin6_addr
.s6_addr32
[2]) == 0xffff)
163 httpGetAddress(con
->http
)->ipv6
.sin6_addr
.s6_addr32
[2] = 0;
164 #endif /* AF_INET6 */
167 * Check the number of clients on the same address...
170 for (count
= 0, tempcon
= (cupsd_client_t
*)cupsArrayFirst(Clients
);
172 tempcon
= (cupsd_client_t
*)cupsArrayNext(Clients
))
173 if (httpAddrEqual(httpGetAddress(tempcon
->http
), httpGetAddress(con
->http
)))
176 if (count
>= MaxClientsPerHost
)
180 if (count
>= MaxClientsPerHost
)
182 if ((time(NULL
) - last_dos
) >= 60)
184 last_dos
= time(NULL
);
185 cupsdLogMessage(CUPSD_LOG_WARN
,
186 "Possible DoS attack - more than %d clients connecting "
189 httpGetHostname(con
->http
, name
, sizeof(name
)));
192 httpClose(con
->http
);
198 * Get the hostname or format the IP address as needed...
202 hostname
= httpResolveHostname(con
->http
, NULL
, 0);
204 hostname
= httpGetHostname(con
->http
, NULL
, 0);
206 if (hostname
== NULL
&& HostNameLookups
== 2)
209 * Can't have an unresolved IP address with double-lookups enabled...
212 httpClose(con
->http
);
214 cupsdLogClient(con
, CUPSD_LOG_WARN
,
215 "Name lookup failed - connection from %s closed!",
216 httpGetHostname(con
->http
, NULL
, 0));
222 if (HostNameLookups
== 2)
225 * Do double lookups as needed...
228 http_addrlist_t
*addrlist
, /* List of addresses */
229 *addr
; /* Current address */
231 if ((addrlist
= httpAddrGetList(hostname
, AF_UNSPEC
, NULL
)) != NULL
)
234 * See if the hostname maps to the same IP address...
237 for (addr
= addrlist
; addr
; addr
= addr
->next
)
238 if (httpAddrEqual(httpGetAddress(con
->http
), &(addr
->addr
)))
244 httpAddrFreeList(addrlist
);
249 * Can't have a hostname that doesn't resolve to the same IP address
250 * with double-lookups enabled...
253 httpClose(con
->http
);
255 cupsdLogClient(con
, CUPSD_LOG_WARN
,
256 "IP lookup failed - connection from %s closed!",
257 httpGetHostname(con
->http
, NULL
, 0));
265 * See if the connection is denied by TCP wrappers...
268 request_init(&wrap_req
, RQ_DAEMON
, "cupsd", RQ_FILE
, httpGetFd(con
->http
),
272 if (!hosts_access(&wrap_req
))
274 httpClose(con
->http
);
276 cupsdLogClient(con
, CUPSD_LOG_WARN
,
277 "Connection from %s refused by /etc/hosts.allow and "
278 "/etc/hosts.deny rules.", httpGetHostname(con
->http
, NULL
, 0));
282 #endif /* HAVE_TCPD_H */
285 if (httpAddrFamily(httpGetAddress(con
->http
)) == AF_LOCAL
)
288 socklen_t peersize
; /* Size of peer credentials */
289 pid_t peerpid
; /* Peer process ID */
290 char peername
[256]; /* Name of process */
292 peersize
= sizeof(peerpid
);
293 if (!getsockopt(con
->number
, SOL_LOCAL
, LOCAL_PEERPID
, &peerpid
,
296 if (!proc_name(peerpid
, peername
, sizeof(peername
)))
297 cupsdLogClient(con
, CUPSD_LOG_DEBUG
,
298 "Accepted from %s (Domain ???[%d])",
299 httpGetHostname(con
->http
, NULL
, 0), (int)peerpid
);
301 cupsdLogClient(con
, CUPSD_LOG_DEBUG
,
302 "Accepted from %s (Domain %s[%d])",
303 httpGetHostname(con
->http
, NULL
, 0), name
, (int)peerpid
);
306 # endif /* __APPLE__ */
308 cupsdLogClient(con
, CUPSD_LOG_DEBUG
, "Accepted from %s (Domain)",
309 httpGetHostname(con
->http
, NULL
, 0));
312 #endif /* AF_LOCAL */
313 cupsdLogClient(con
, CUPSD_LOG_DEBUG
, "Accepted from %s:%d (IPv%d)",
314 httpGetHostname(con
->http
, NULL
, 0),
315 httpAddrPort(httpGetAddress(con
->http
)),
316 httpAddrFamily(httpGetAddress(con
->http
)) == AF_INET
? 4 : 6);
319 * Get the local address the client connected to...
322 addrlen
= sizeof(temp
);
323 if (getsockname(httpGetFd(con
->http
), (struct sockaddr
*)&temp
, &addrlen
))
325 cupsdLogClient(con
, CUPSD_LOG_ERROR
, "Unable to get local address - %s",
328 strlcpy(con
->servername
, "localhost", sizeof(con
->servername
));
329 con
->serverport
= LocalPort
;
332 else if (httpAddrFamily(&temp
) == AF_LOCAL
)
334 strlcpy(con
->servername
, "localhost", sizeof(con
->servername
));
335 con
->serverport
= LocalPort
;
337 #endif /* AF_LOCAL */
340 if (httpAddrLocalhost(&temp
))
341 strlcpy(con
->servername
, "localhost", sizeof(con
->servername
));
342 else if (HostNameLookups
)
343 httpAddrLookup(&temp
, con
->servername
, sizeof(con
->servername
));
345 httpAddrString(&temp
, con
->servername
, sizeof(con
->servername
));
347 con
->serverport
= httpAddrPort(&(lis
->address
));
351 * Add the connection to the array of active clients...
354 cupsArrayAdd(Clients
, con
);
357 * Add the socket to the server select.
360 cupsdAddSelect(httpGetFd(con
->http
), (cupsd_selfunc_t
)cupsdReadClient
, NULL
,
363 cupsdLogClient(con
, CUPSD_LOG_DEBUG
, "Waiting for request.");
366 * Temporarily suspend accept()'s until we lose a client...
369 if (cupsArrayCount(Clients
) == MaxClients
)
370 cupsdPauseListening();
374 * See if we are connecting on a secure port...
377 if (lis
->encryption
== HTTP_ENCRYPTION_ALWAYS
)
380 * https connection; go secure...
383 if (cupsd_start_tls(con
, HTTP_ENCRYPTION_ALWAYS
))
384 cupsdCloseClient(con
);
388 #endif /* HAVE_SSL */
393 * 'cupsdCloseAllClients()' - Close all remote clients immediately.
397 cupsdCloseAllClients(void)
399 cupsd_client_t
*con
; /* Current client */
402 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdCloseAllClients() Clients=%d",
403 cupsArrayCount(Clients
));
405 for (con
= (cupsd_client_t
*)cupsArrayFirst(Clients
);
407 con
= (cupsd_client_t
*)cupsArrayNext(Clients
))
408 if (cupsdCloseClient(con
))
409 cupsdCloseClient(con
);
414 * 'cupsdCloseClient()' - Close a remote client.
417 int /* O - 1 if partial close, 0 if fully closed */
418 cupsdCloseClient(cupsd_client_t
*con
) /* I - Client to close */
420 int partial
; /* Do partial close for SSL? */
423 cupsdLogClient(con
, CUPSD_LOG_DEBUG
, "Closing connection.");
426 * Flush pending writes before closing...
429 httpFlushWrite(con
->http
);
433 if (con
->pipe_pid
!= 0)
436 * Stop any CGI process...
439 cupsdEndProcess(con
->pipe_pid
, 1);
445 cupsdRemoveSelect(con
->file
);
452 * Close the socket and clear the file from the input set for select()...
455 if (httpGetFd(con
->http
) >= 0)
457 cupsArrayRemove(ActiveClients
, con
);
462 * Shutdown encryption as needed...
465 if (httpIsEncrypted(con
->http
))
467 #endif /* HAVE_SSL */
472 * Only do a partial close so that the encrypted client gets everything.
475 httpShutdown(con
->http
);
476 cupsdAddSelect(httpGetFd(con
->http
), (cupsd_selfunc_t
)cupsdReadClient
,
479 cupsdLogClient(con
, CUPSD_LOG_DEBUG
, "Waiting for socket close.");
484 * Shut the socket down fully...
487 cupsdRemoveSelect(httpGetFd(con
->http
));
488 httpClose(con
->http
);
499 cupsdRemoveSelect(httpGetFd(con
->http
));
501 httpClose(con
->http
);
503 cupsdClearString(&con
->filename
);
504 cupsdClearString(&con
->command
);
505 cupsdClearString(&con
->options
);
506 cupsdClearString(&con
->query_string
);
510 ippDelete(con
->request
);
516 ippDelete(con
->response
);
517 con
->response
= NULL
;
522 cupsLangFree(con
->language
);
523 con
->language
= NULL
;
526 #ifdef HAVE_AUTHORIZATION_H
529 AuthorizationFree(con
->authref
, kAuthorizationFlagDefaults
);
532 #endif /* HAVE_AUTHORIZATION_H */
535 * Re-enable new client connections if we are going back under the
539 if (cupsArrayCount(Clients
) == MaxClients
)
540 cupsdResumeListening();
543 * Compact the list of clients as necessary...
546 cupsArrayRemove(Clients
, con
);
556 * 'cupsdReadClient()' - Read data from a client.
560 cupsdReadClient(cupsd_client_t
*con
) /* I - Client to read from */
562 char line
[32768], /* Line from client... */
563 locale
[64], /* Locale */
564 *ptr
; /* Pointer into strings */
565 http_status_t status
; /* Transfer status */
566 ipp_state_t ipp_state
; /* State of IPP transfer */
567 int bytes
; /* Number of bytes to POST */
568 char *filename
; /* Name of file for GET/HEAD */
569 char buf
[1024]; /* Buffer for real filename */
570 struct stat filestats
; /* File information */
571 mime_type_t
*type
; /* MIME type of file */
572 cupsd_printer_t
*p
; /* Printer */
573 static unsigned request_id
= 0; /* Request ID for temp files */
576 status
= HTTP_STATUS_CONTINUE
;
578 cupsdLogClient(con
, CUPSD_LOG_DEBUG2
,
583 "data_encoding=HTTP_ENCODING_%s, "
584 "data_remaining=" CUPS_LLFMT
", "
587 httpError(con
->http
), (int)httpGetReady(con
->http
),
588 httpStateString(httpGetState(con
->http
)),
589 httpIsChunked(con
->http
) ? "CHUNKED" : "LENGTH",
590 CUPS_LLCAST
httpGetRemaining(con
->http
),
592 con
->request
? ippStateString(ippGetState(con
->request
)) : "",
595 if (httpGetState(con
->http
) == HTTP_STATE_GET_SEND
||
596 httpGetState(con
->http
) == HTTP_STATE_POST_SEND
||
597 httpGetState(con
->http
) == HTTP_STATE_STATUS
)
600 * If we get called in the wrong state, then something went wrong with the
601 * connection and we need to shut it down...
604 cupsdLogClient(con
, CUPSD_LOG_DEBUG
, "Closing on unexpected HTTP read state %s.",
605 httpStateString(httpGetState(con
->http
)));
606 cupsdCloseClient(con
);
615 * Automatically check for a SSL/TLS handshake...
620 if (recv(httpGetFd(con
->http
), buf
, 1, MSG_PEEK
) == 1 &&
621 (!buf
[0] || !strchr("DGHOPT", buf
[0])))
624 * Encrypt this connection...
627 cupsdLogClient(con
, CUPSD_LOG_DEBUG2
,
628 "Saw first byte %02X, auto-negotiating "
629 "SSL/TLS session.", buf
[0] & 255);
631 if (cupsd_start_tls(con
, HTTP_ENCRYPTION_ALWAYS
))
632 cupsdCloseClient(con
);
637 #endif /* HAVE_SSL */
639 switch (httpGetState(con
->http
))
641 case HTTP_STATE_WAITING
:
643 * See if we've received a request line...
646 con
->operation
= httpReadRequest(con
->http
, con
->uri
, sizeof(con
->uri
));
647 if (con
->operation
== HTTP_STATE_ERROR
||
648 con
->operation
== HTTP_STATE_UNKNOWN_METHOD
||
649 con
->operation
== HTTP_STATE_UNKNOWN_VERSION
)
651 if (httpError(con
->http
))
652 cupsdLogClient(con
, CUPSD_LOG_DEBUG
,
653 "HTTP_STATE_WAITING Closing for error %d (%s)",
654 httpError(con
->http
), strerror(httpError(con
->http
)));
656 cupsdLogClient(con
, CUPSD_LOG_DEBUG
,
657 "HTTP_STATE_WAITING Closing on error: %s",
658 cupsLastErrorString());
660 cupsdCloseClient(con
);
665 * Ignore blank request lines...
668 if (con
->operation
== HTTP_STATE_WAITING
)
672 * Clear other state variables...
679 con
->username
[0] = '\0';
680 con
->password
[0] = '\0';
682 cupsdClearString(&con
->command
);
683 cupsdClearString(&con
->options
);
684 cupsdClearString(&con
->query_string
);
688 ippDelete(con
->request
);
694 ippDelete(con
->response
);
695 con
->response
= NULL
;
700 cupsLangFree(con
->language
);
701 con
->language
= NULL
;
707 #endif /* HAVE_GSSAPI */
710 * Handle full URLs in the request line...
713 if (strcmp(con
->uri
, "*"))
715 char scheme
[HTTP_MAX_URI
], /* Method/scheme */
716 userpass
[HTTP_MAX_URI
], /* Username:password */
717 hostname
[HTTP_MAX_URI
], /* Hostname */
718 resource
[HTTP_MAX_URI
]; /* Resource path */
719 int port
; /* Port number */
722 * Separate the URI into its components...
725 if (httpSeparateURI(HTTP_URI_CODING_MOST
, con
->uri
,
726 scheme
, sizeof(scheme
),
727 userpass
, sizeof(userpass
),
728 hostname
, sizeof(hostname
), &port
,
729 resource
, sizeof(resource
)) < HTTP_URI_STATUS_OK
)
731 cupsdLogClient(con
, CUPSD_LOG_ERROR
, "Bad URI \"%s\" in request.",
733 cupsdSendError(con
, HTTP_STATUS_METHOD_NOT_ALLOWED
, CUPSD_AUTH_NONE
);
734 cupsdCloseClient(con
);
739 * Only allow URIs with the servername, localhost, or an IP
743 if (strcmp(scheme
, "file") &&
744 _cups_strcasecmp(hostname
, ServerName
) &&
745 _cups_strcasecmp(hostname
, "localhost") &&
746 !cupsArrayFind(ServerAlias
, hostname
) &&
747 !isdigit(hostname
[0]) && hostname
[0] != '[')
750 * Nope, we don't do proxies...
753 cupsdLogClient(con
, CUPSD_LOG_ERROR
, "Bad URI \"%s\" in request.",
755 cupsdSendError(con
, HTTP_STATUS_METHOD_NOT_ALLOWED
, CUPSD_AUTH_NONE
);
756 cupsdCloseClient(con
);
761 * Copy the resource portion back into the URI; both resource and
762 * con->uri are HTTP_MAX_URI bytes in size...
765 strlcpy(con
->uri
, resource
, sizeof(con
->uri
));
769 * Process the request...
772 gettimeofday(&(con
->start
), NULL
);
774 cupsdLogClient(con
, CUPSD_LOG_DEBUG
, "%s %s HTTP/%d.%d",
775 httpStateString(con
->operation
) + 11, con
->uri
,
776 httpGetVersion(con
->http
) / 100,
777 httpGetVersion(con
->http
) % 100);
779 if (!cupsArrayFind(ActiveClients
, con
))
781 cupsArrayAdd(ActiveClients
, con
);
785 case HTTP_STATE_OPTIONS
:
786 case HTTP_STATE_DELETE
:
787 case HTTP_STATE_GET
:
788 case HTTP_STATE_HEAD
:
789 case HTTP_STATE_POST
:
790 case HTTP_STATE_PUT
:
791 case HTTP_STATE_TRACE
:
793 * Parse incoming parameters until the status changes...
796 while ((status
= httpUpdate(con
->http
)) == HTTP_STATUS_CONTINUE
)
797 if (!httpGetReady(con
->http
))
800 if (status
!= HTTP_STATUS_OK
&& status
!= HTTP_STATUS_CONTINUE
)
802 if (httpError(con
->http
) && httpError(con
->http
) != EPIPE
)
803 cupsdLogClient(con
, CUPSD_LOG_DEBUG
,
804 "Closing for error %d (%s) while reading headers.",
805 httpError(con
->http
), strerror(httpError(con
->http
)));
807 cupsdLogClient(con
, CUPSD_LOG_DEBUG
,
808 "Closing on EOF while reading headers.");
810 cupsdSendError(con
, HTTP_STATUS_BAD_REQUEST
, CUPSD_AUTH_NONE
);
811 cupsdCloseClient(con
);
817 if (!httpGetReady(con
->http
) && recv(httpGetFd(con
->http
), buf
, 1, MSG_PEEK
) < 1)
820 * Connection closed...
823 cupsdLogClient(con
, CUPSD_LOG_DEBUG
, "Closing on EOF.");
824 cupsdCloseClient(con
);
827 break; /* Anti-compiler-warning-code */
831 * Handle new transfers...
834 cupsdLogClient(con
, CUPSD_LOG_DEBUG
, "Read: status=%d", status
);
836 if (status
== HTTP_STATUS_OK
)
838 if (httpGetField(con
->http
, HTTP_FIELD_ACCEPT_LANGUAGE
)[0])
841 * Figure out the locale from the Accept-Language and Content-Type
845 if ((ptr
= strchr(httpGetField(con
->http
, HTTP_FIELD_ACCEPT_LANGUAGE
),
849 if ((ptr
= strchr(httpGetField(con
->http
, HTTP_FIELD_ACCEPT_LANGUAGE
),
853 if ((ptr
= strstr(httpGetField(con
->http
, HTTP_FIELD_CONTENT_TYPE
),
854 "charset=")) != NULL
)
857 * Combine language and charset, and trim any extra params in the
861 snprintf(locale
, sizeof(locale
), "%s.%s",
862 httpGetField(con
->http
, HTTP_FIELD_ACCEPT_LANGUAGE
), ptr
+ 8);
864 if ((ptr
= strchr(locale
, ',')) != NULL
)
868 snprintf(locale
, sizeof(locale
), "%s.UTF-8",
869 httpGetField(con
->http
, HTTP_FIELD_ACCEPT_LANGUAGE
));
871 con
->language
= cupsLangGet(locale
);
874 con
->language
= cupsLangGet(DefaultLocale
);
878 if (!_cups_strncasecmp(httpGetField(con
->http
, HTTP_FIELD_CONNECTION
),
879 "Keep-Alive", 10) && KeepAlive
)
880 httpSetKeepAlive(con
->http
, HTTP_KEEPALIVE_ON
);
881 else if (!_cups_strncasecmp(httpGetField(con
->http
, HTTP_FIELD_CONNECTION
),
883 httpSetKeepAlive(con
->http
, HTTP_KEEPALIVE_OFF
);
885 if (!httpGetField(con
->http
, HTTP_FIELD_HOST
)[0] &&
886 httpGetVersion(con
->http
) >= HTTP_VERSION_1_1
)
889 * HTTP/1.1 and higher require the "Host:" field...
892 if (!cupsdSendError(con
, HTTP_STATUS_BAD_REQUEST
, CUPSD_AUTH_NONE
))
894 cupsdLogClient(con
, CUPSD_LOG_ERROR
, "Missing Host: field in request.");
895 cupsdCloseClient(con
);
899 else if (!valid_host(con
))
902 * Access to localhost must use "localhost" or the corresponding IPv4
903 * or IPv6 values in the Host: field.
906 cupsdLogClient(con
, CUPSD_LOG_ERROR
,
907 "Request from \"%s\" using invalid Host: field \"%s\".",
908 httpGetHostname(con
->http
, NULL
, 0), httpGetField(con
->http
, HTTP_FIELD_HOST
));
910 if (!cupsdSendError(con
, HTTP_STATUS_BAD_REQUEST
, CUPSD_AUTH_NONE
))
912 cupsdCloseClient(con
);
916 else if (con
->operation
== HTTP_STATE_OPTIONS
)
919 * Do OPTIONS command...
922 if (con
->best
&& con
->best
->type
!= CUPSD_AUTH_NONE
)
924 httpClearFields(con
->http
);
926 if (!cupsdSendHeader(con
, HTTP_STATUS_UNAUTHORIZED
, NULL
, CUPSD_AUTH_NONE
))
928 cupsdCloseClient(con
);
933 if (!_cups_strcasecmp(httpGetField(con
->http
, HTTP_FIELD_CONNECTION
), "Upgrade") &&
934 !httpIsEncrypted(con
->http
))
938 * Do encryption stuff...
941 httpClearFields(con
->http
);
943 if (!cupsdSendHeader(con
, HTTP_STATUS_SWITCHING_PROTOCOLS
, NULL
, CUPSD_AUTH_NONE
))
945 cupsdCloseClient(con
);
949 if (cupsd_start_tls(con
, HTTP_ENCRYPTION_REQUIRED
))
951 cupsdCloseClient(con
);
955 if (!cupsdSendError(con
, HTTP_NOT_IMPLEMENTED
, CUPSD_AUTH_NONE
))
957 cupsdCloseClient(con
);
960 #endif /* HAVE_SSL */
963 httpClearFields(con
->http
);
964 httpSetField(con
->http
, HTTP_FIELD_ALLOW
,
965 "GET, HEAD, OPTIONS, POST, PUT");
966 httpSetField(con
->http
, HTTP_FIELD_CONTENT_LENGTH
, "0");
968 if (!cupsdSendHeader(con
, HTTP_STATUS_OK
, NULL
, CUPSD_AUTH_NONE
))
970 cupsdCloseClient(con
);
974 else if (!is_path_absolute(con
->uri
))
977 * Protect against malicious users!
980 cupsdLogClient(con
, CUPSD_LOG_ERROR
,
981 "Request for non-absolute resource \"%s\".", con
->uri
);
983 if (!cupsdSendError(con
, HTTP_STATUS_FORBIDDEN
, CUPSD_AUTH_NONE
))
985 cupsdCloseClient(con
);
991 if (!_cups_strcasecmp(httpGetField(con
->http
, HTTP_FIELD_CONNECTION
),
992 "Upgrade") && !httpIsEncrypted(con
->http
))
996 * Do encryption stuff...
999 httpClearFields(con
->http
);
1001 if (!cupsdSendHeader(con
, HTTP_STATUS_SWITCHING_PROTOCOLS
, NULL
,
1004 cupsdCloseClient(con
);
1008 if (cupsd_start_tls(con
, HTTP_ENCRYPTION_REQUIRED
))
1010 cupsdCloseClient(con
);
1014 if (!cupsdSendError(con
, HTTP_NOT_IMPLEMENTED
, CUPSD_AUTH_NONE
))
1016 cupsdCloseClient(con
);
1019 #endif /* HAVE_SSL */
1022 if ((status
= cupsdIsAuthorized(con
, NULL
)) != HTTP_STATUS_OK
)
1024 cupsdSendError(con
, status
, CUPSD_AUTH_NONE
);
1025 cupsdCloseClient(con
);
1029 if (httpGetExpect(con
->http
) &&
1030 (con
->operation
== HTTP_STATE_POST
|| con
->operation
== HTTP_STATE_PUT
))
1032 if (httpGetExpect(con
->http
) == HTTP_STATUS_CONTINUE
)
1035 * Send 100-continue header...
1038 if (httpWriteResponse(con
->http
, HTTP_STATUS_CONTINUE
))
1040 cupsdCloseClient(con
);
1047 * Send 417-expectation-failed header...
1050 httpClearFields(con
->http
);
1051 httpSetField(con
->http
, HTTP_FIELD_CONTENT_LENGTH
, "0");
1053 cupsdSendError(con
, HTTP_STATUS_EXPECTATION_FAILED
, CUPSD_AUTH_NONE
);
1054 cupsdCloseClient(con
);
1059 switch (httpGetState(con
->http
))
1061 case HTTP_STATE_GET_SEND
:
1062 cupsdLogClient(con
, CUPSD_LOG_DEBUG
, "Processing GET %s", con
->uri
);
1064 if ((!strncmp(con
->uri
, "/ppd/", 5) ||
1065 !strncmp(con
->uri
, "/printers/", 10) ||
1066 !strncmp(con
->uri
, "/classes/", 9)) &&
1067 !strcmp(con
->uri
+ strlen(con
->uri
) - 4, ".ppd"))
1070 * Send PPD file - get the real printer name since printer
1071 * names are not case sensitive but filenames can be...
1074 con
->uri
[strlen(con
->uri
) - 4] = '\0'; /* Drop ".ppd" */
1076 if (!strncmp(con
->uri
, "/ppd/", 5))
1077 p
= cupsdFindPrinter(con
->uri
+ 5);
1078 else if (!strncmp(con
->uri
, "/printers/", 10))
1079 p
= cupsdFindPrinter(con
->uri
+ 10);
1082 p
= cupsdFindClass(con
->uri
+ 9);
1086 int i
; /* Looping var */
1088 for (i
= 0; i
< p
->num_printers
; i
++)
1090 if (!(p
->printers
[i
]->type
& CUPS_PRINTER_CLASS
))
1092 char ppdname
[1024];/* PPD filename */
1094 snprintf(ppdname
, sizeof(ppdname
), "%s/ppd/%s.ppd",
1095 ServerRoot
, p
->printers
[i
]->name
);
1096 if (!access(ppdname
, 0))
1104 if (i
>= p
->num_printers
)
1111 snprintf(con
->uri
, sizeof(con
->uri
), "/ppd/%s.ppd", p
->name
);
1115 if (!cupsdSendError(con
, HTTP_STATUS_NOT_FOUND
, CUPSD_AUTH_NONE
))
1117 cupsdCloseClient(con
);
1124 else if ((!strncmp(con
->uri
, "/icons/", 7) ||
1125 !strncmp(con
->uri
, "/printers/", 10) ||
1126 !strncmp(con
->uri
, "/classes/", 9)) &&
1127 !strcmp(con
->uri
+ strlen(con
->uri
) - 4, ".png"))
1130 * Send icon file - get the real queue name since queue names are
1131 * not case sensitive but filenames can be...
1134 con
->uri
[strlen(con
->uri
) - 4] = '\0'; /* Drop ".png" */
1136 if (!strncmp(con
->uri
, "/icons/", 7))
1137 p
= cupsdFindPrinter(con
->uri
+ 7);
1138 else if (!strncmp(con
->uri
, "/printers/", 10))
1139 p
= cupsdFindPrinter(con
->uri
+ 10);
1142 p
= cupsdFindClass(con
->uri
+ 9);
1146 int i
; /* Looping var */
1148 for (i
= 0; i
< p
->num_printers
; i
++)
1150 if (!(p
->printers
[i
]->type
& CUPS_PRINTER_CLASS
))
1152 char ppdname
[1024];/* PPD filename */
1154 snprintf(ppdname
, sizeof(ppdname
), "%s/ppd/%s.ppd",
1155 ServerRoot
, p
->printers
[i
]->name
);
1156 if (!access(ppdname
, 0))
1164 if (i
>= p
->num_printers
)
1170 snprintf(con
->uri
, sizeof(con
->uri
), "/icons/%s.png", p
->name
);
1173 if (!cupsdSendError(con
, HTTP_STATUS_NOT_FOUND
, CUPSD_AUTH_NONE
))
1175 cupsdCloseClient(con
);
1182 else if (!WebInterface
)
1185 * Web interface is disabled. Show an appropriate message...
1188 if (!cupsdSendError(con
, HTTP_STATUS_CUPS_WEBIF_DISABLED
, CUPSD_AUTH_NONE
))
1190 cupsdCloseClient(con
);
1197 if ((!strncmp(con
->uri
, "/admin", 6) &&
1198 strncmp(con
->uri
, "/admin/conf/", 12) &&
1199 strncmp(con
->uri
, "/admin/log/", 11)) ||
1200 !strncmp(con
->uri
, "/printers", 9) ||
1201 !strncmp(con
->uri
, "/classes", 8) ||
1202 !strncmp(con
->uri
, "/help", 5) ||
1203 !strncmp(con
->uri
, "/jobs", 5))
1206 * Send CGI output...
1209 if (!strncmp(con
->uri
, "/admin", 6))
1211 cupsdSetStringf(&con
->command
, "%s/cgi-bin/admin.cgi",
1214 cupsdSetString(&con
->options
, strchr(con
->uri
+ 6, '?'));
1216 else if (!strncmp(con
->uri
, "/printers", 9))
1218 cupsdSetStringf(&con
->command
, "%s/cgi-bin/printers.cgi",
1221 if (con
->uri
[9] && con
->uri
[10])
1222 cupsdSetString(&con
->options
, con
->uri
+ 9);
1224 cupsdSetString(&con
->options
, NULL
);
1226 else if (!strncmp(con
->uri
, "/classes", 8))
1228 cupsdSetStringf(&con
->command
, "%s/cgi-bin/classes.cgi",
1231 if (con
->uri
[8] && con
->uri
[9])
1232 cupsdSetString(&con
->options
, con
->uri
+ 8);
1234 cupsdSetString(&con
->options
, NULL
);
1236 else if (!strncmp(con
->uri
, "/jobs", 5))
1238 cupsdSetStringf(&con
->command
, "%s/cgi-bin/jobs.cgi",
1241 if (con
->uri
[5] && con
->uri
[6])
1242 cupsdSetString(&con
->options
, con
->uri
+ 5);
1244 cupsdSetString(&con
->options
, NULL
);
1248 cupsdSetStringf(&con
->command
, "%s/cgi-bin/help.cgi",
1251 if (con
->uri
[5] && con
->uri
[6])
1252 cupsdSetString(&con
->options
, con
->uri
+ 5);
1254 cupsdSetString(&con
->options
, NULL
);
1257 if (!cupsdSendCommand(con
, con
->command
, con
->options
, 0))
1259 if (!cupsdSendError(con
, HTTP_STATUS_NOT_FOUND
, CUPSD_AUTH_NONE
))
1261 cupsdCloseClient(con
);
1266 cupsdLogRequest(con
, HTTP_STATUS_OK
);
1268 if (httpGetVersion(con
->http
) <= HTTP_VERSION_1_0
)
1269 httpSetKeepAlive(con
->http
, HTTP_KEEPALIVE_OFF
);
1271 else if ((!strncmp(con
->uri
, "/admin/conf/", 12) &&
1272 (strchr(con
->uri
+ 12, '/') ||
1273 strlen(con
->uri
) == 12)) ||
1274 (!strncmp(con
->uri
, "/admin/log/", 11) &&
1275 (strchr(con
->uri
+ 11, '/') ||
1276 strlen(con
->uri
) == 11)))
1279 * GET can only be done to configuration files directly under
1283 cupsdLogClient(con
, CUPSD_LOG_ERROR
,
1284 "Request for subdirectory \"%s\"!", con
->uri
);
1286 if (!cupsdSendError(con
, HTTP_STATUS_FORBIDDEN
, CUPSD_AUTH_NONE
))
1288 cupsdCloseClient(con
);
1300 if ((filename
= get_file(con
, &filestats
, buf
,
1301 sizeof(buf
))) == NULL
)
1303 if (!cupsdSendError(con
, HTTP_STATUS_NOT_FOUND
, CUPSD_AUTH_NONE
))
1305 cupsdCloseClient(con
);
1312 type
= mimeFileType(MimeDatabase
, filename
, NULL
, NULL
);
1314 cupsdLogClient(con
, CUPSD_LOG_DEBUG
, "filename=\"%s\", type=%s/%s", filename
, type
? type
->super
: "", type
? type
->type
: "");
1316 if (is_cgi(con
, filename
, &filestats
, type
))
1319 * Note: con->command and con->options were set by
1323 if (!cupsdSendCommand(con
, con
->command
, con
->options
, 0))
1325 if (!cupsdSendError(con
, HTTP_STATUS_NOT_FOUND
, CUPSD_AUTH_NONE
))
1327 cupsdCloseClient(con
);
1332 cupsdLogRequest(con
, HTTP_STATUS_OK
);
1334 if (httpGetVersion(con
->http
) <= HTTP_VERSION_1_0
)
1335 httpSetKeepAlive(con
->http
, HTTP_KEEPALIVE_OFF
);
1339 if (!check_if_modified(con
, &filestats
))
1341 if (!cupsdSendError(con
, HTTP_STATUS_NOT_MODIFIED
, CUPSD_AUTH_NONE
))
1343 cupsdCloseClient(con
);
1350 strlcpy(line
, "text/plain", sizeof(line
));
1352 snprintf(line
, sizeof(line
), "%s/%s", type
->super
, type
->type
);
1354 if (!write_file(con
, HTTP_STATUS_OK
, filename
, line
, &filestats
))
1356 cupsdCloseClient(con
);
1363 case HTTP_STATE_POST_RECV
:
1365 * See if the POST request includes a Content-Length field, and if
1366 * so check the length against any limits that are set...
1369 if (httpGetField(con
->http
, HTTP_FIELD_CONTENT_LENGTH
)[0] &&
1370 MaxRequestSize
> 0 &&
1371 httpGetLength2(con
->http
) > MaxRequestSize
)
1374 * Request too large...
1377 if (!cupsdSendError(con
, HTTP_STATUS_REQUEST_TOO_LARGE
, CUPSD_AUTH_NONE
))
1379 cupsdCloseClient(con
);
1385 else if (httpGetLength2(con
->http
) < 0)
1388 * Negative content lengths are invalid!
1391 if (!cupsdSendError(con
, HTTP_STATUS_BAD_REQUEST
, CUPSD_AUTH_NONE
))
1393 cupsdCloseClient(con
);
1401 * See what kind of POST request this is; for IPP requests the
1402 * content-type field will be "application/ipp"...
1405 if (!strcmp(httpGetField(con
->http
, HTTP_FIELD_CONTENT_TYPE
),
1407 con
->request
= ippNew();
1408 else if (!WebInterface
)
1411 * Web interface is disabled. Show an appropriate message...
1414 if (!cupsdSendError(con
, HTTP_STATUS_CUPS_WEBIF_DISABLED
, CUPSD_AUTH_NONE
))
1416 cupsdCloseClient(con
);
1422 else if ((!strncmp(con
->uri
, "/admin", 6) &&
1423 strncmp(con
->uri
, "/admin/conf/", 12) &&
1424 strncmp(con
->uri
, "/admin/log/", 11)) ||
1425 !strncmp(con
->uri
, "/printers", 9) ||
1426 !strncmp(con
->uri
, "/classes", 8) ||
1427 !strncmp(con
->uri
, "/help", 5) ||
1428 !strncmp(con
->uri
, "/jobs", 5))
1434 if (!strncmp(con
->uri
, "/admin", 6))
1436 cupsdSetStringf(&con
->command
, "%s/cgi-bin/admin.cgi",
1439 cupsdSetString(&con
->options
, strchr(con
->uri
+ 6, '?'));
1441 else if (!strncmp(con
->uri
, "/printers", 9))
1443 cupsdSetStringf(&con
->command
, "%s/cgi-bin/printers.cgi",
1446 if (con
->uri
[9] && con
->uri
[10])
1447 cupsdSetString(&con
->options
, con
->uri
+ 9);
1449 cupsdSetString(&con
->options
, NULL
);
1451 else if (!strncmp(con
->uri
, "/classes", 8))
1453 cupsdSetStringf(&con
->command
, "%s/cgi-bin/classes.cgi",
1456 if (con
->uri
[8] && con
->uri
[9])
1457 cupsdSetString(&con
->options
, con
->uri
+ 8);
1459 cupsdSetString(&con
->options
, NULL
);
1461 else if (!strncmp(con
->uri
, "/jobs", 5))
1463 cupsdSetStringf(&con
->command
, "%s/cgi-bin/jobs.cgi",
1466 if (con
->uri
[5] && con
->uri
[6])
1467 cupsdSetString(&con
->options
, con
->uri
+ 5);
1469 cupsdSetString(&con
->options
, NULL
);
1473 cupsdSetStringf(&con
->command
, "%s/cgi-bin/help.cgi",
1476 if (con
->uri
[5] && con
->uri
[6])
1477 cupsdSetString(&con
->options
, con
->uri
+ 5);
1479 cupsdSetString(&con
->options
, NULL
);
1482 if (httpGetVersion(con
->http
) <= HTTP_VERSION_1_0
)
1483 httpSetKeepAlive(con
->http
, HTTP_KEEPALIVE_OFF
);
1491 if ((filename
= get_file(con
, &filestats
, buf
,
1492 sizeof(buf
))) == NULL
)
1494 if (!cupsdSendError(con
, HTTP_STATUS_NOT_FOUND
, CUPSD_AUTH_NONE
))
1496 cupsdCloseClient(con
);
1503 type
= mimeFileType(MimeDatabase
, filename
, NULL
, NULL
);
1505 if (!is_cgi(con
, filename
, &filestats
, type
))
1508 * Only POST to CGI's...
1511 if (!cupsdSendError(con
, HTTP_STATUS_UNAUTHORIZED
, CUPSD_AUTH_NONE
))
1513 cupsdCloseClient(con
);
1520 case HTTP_STATE_PUT_RECV
:
1522 * Validate the resource name...
1525 if (strcmp(con
->uri
, "/admin/conf/cupsd.conf"))
1528 * PUT can only be done to the cupsd.conf file...
1531 cupsdLogClient(con
, CUPSD_LOG_ERROR
,
1532 "Disallowed PUT request for \"%s\".", con
->uri
);
1534 if (!cupsdSendError(con
, HTTP_STATUS_FORBIDDEN
, CUPSD_AUTH_NONE
))
1536 cupsdCloseClient(con
);
1544 * See if the PUT request includes a Content-Length field, and if
1545 * so check the length against any limits that are set...
1548 if (httpGetField(con
->http
, HTTP_FIELD_CONTENT_LENGTH
)[0] &&
1549 MaxRequestSize
> 0 &&
1550 httpGetLength2(con
->http
) > MaxRequestSize
)
1553 * Request too large...
1556 if (!cupsdSendError(con
, HTTP_STATUS_REQUEST_TOO_LARGE
, CUPSD_AUTH_NONE
))
1558 cupsdCloseClient(con
);
1564 else if (httpGetLength2(con
->http
) < 0)
1567 * Negative content lengths are invalid!
1570 if (!cupsdSendError(con
, HTTP_STATUS_BAD_REQUEST
, CUPSD_AUTH_NONE
))
1572 cupsdCloseClient(con
);
1580 * Open a temporary file to hold the request...
1583 cupsdSetStringf(&con
->filename
, "%s/%08x", RequestRoot
,
1585 con
->file
= open(con
->filename
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0640);
1589 cupsdLogClient(con
, CUPSD_LOG_ERROR
,
1590 "Unable to create request file \"%s\": %s",
1591 con
->filename
, strerror(errno
));
1593 if (!cupsdSendError(con
, HTTP_STATUS_REQUEST_TOO_LARGE
, CUPSD_AUTH_NONE
))
1595 cupsdCloseClient(con
);
1600 fchmod(con
->file
, 0640);
1601 fchown(con
->file
, RunUser
, Group
);
1602 fcntl(con
->file
, F_SETFD
, fcntl(con
->file
, F_GETFD
) | FD_CLOEXEC
);
1605 case HTTP_STATE_DELETE
:
1606 case HTTP_STATE_TRACE
:
1607 cupsdSendError(con
, HTTP_STATUS_NOT_IMPLEMENTED
, CUPSD_AUTH_NONE
);
1608 cupsdCloseClient(con
);
1611 case HTTP_STATE_HEAD
:
1612 if (!strncmp(con
->uri
, "/printers/", 10) &&
1613 !strcmp(con
->uri
+ strlen(con
->uri
) - 4, ".ppd"))
1616 * Send PPD file - get the real printer name since printer
1617 * names are not case sensitive but filenames can be...
1620 con
->uri
[strlen(con
->uri
) - 4] = '\0'; /* Drop ".ppd" */
1622 if ((p
= cupsdFindPrinter(con
->uri
+ 10)) != NULL
)
1623 snprintf(con
->uri
, sizeof(con
->uri
), "/ppd/%s.ppd", p
->name
);
1626 if (!cupsdSendError(con
, HTTP_STATUS_NOT_FOUND
, CUPSD_AUTH_NONE
))
1628 cupsdCloseClient(con
);
1632 cupsdLogRequest(con
, HTTP_STATUS_NOT_FOUND
);
1636 else if (!strncmp(con
->uri
, "/printers/", 10) &&
1637 !strcmp(con
->uri
+ strlen(con
->uri
) - 4, ".png"))
1640 * Send PNG file - get the real printer name since printer
1641 * names are not case sensitive but filenames can be...
1644 con
->uri
[strlen(con
->uri
) - 4] = '\0'; /* Drop ".ppd" */
1646 if ((p
= cupsdFindPrinter(con
->uri
+ 10)) != NULL
)
1647 snprintf(con
->uri
, sizeof(con
->uri
), "/icons/%s.png", p
->name
);
1650 if (!cupsdSendError(con
, HTTP_STATUS_NOT_FOUND
, CUPSD_AUTH_NONE
))
1652 cupsdCloseClient(con
);
1656 cupsdLogRequest(con
, HTTP_STATUS_NOT_FOUND
);
1660 else if (!WebInterface
)
1662 httpClearFields(con
->http
);
1664 if (!cupsdSendHeader(con
, HTTP_STATUS_OK
, NULL
, CUPSD_AUTH_NONE
))
1666 cupsdCloseClient(con
);
1670 cupsdLogRequest(con
, HTTP_STATUS_OK
);
1674 if ((!strncmp(con
->uri
, "/admin", 6) &&
1675 strncmp(con
->uri
, "/admin/conf/", 12) &&
1676 strncmp(con
->uri
, "/admin/log/", 11)) ||
1677 !strncmp(con
->uri
, "/printers", 9) ||
1678 !strncmp(con
->uri
, "/classes", 8) ||
1679 !strncmp(con
->uri
, "/help", 5) ||
1680 !strncmp(con
->uri
, "/jobs", 5))
1686 httpClearFields(con
->http
);
1688 if (!cupsdSendHeader(con
, HTTP_STATUS_OK
, "text/html", CUPSD_AUTH_NONE
))
1690 cupsdCloseClient(con
);
1694 cupsdLogRequest(con
, HTTP_STATUS_OK
);
1696 else if ((!strncmp(con
->uri
, "/admin/conf/", 12) &&
1697 (strchr(con
->uri
+ 12, '/') ||
1698 strlen(con
->uri
) == 12)) ||
1699 (!strncmp(con
->uri
, "/admin/log/", 11) &&
1700 (strchr(con
->uri
+ 11, '/') ||
1701 strlen(con
->uri
) == 11)))
1704 * HEAD can only be done to configuration files under
1708 cupsdLogClient(con
, CUPSD_LOG_ERROR
,
1709 "Request for subdirectory \"%s\".", con
->uri
);
1711 if (!cupsdSendError(con
, HTTP_STATUS_FORBIDDEN
, CUPSD_AUTH_NONE
))
1713 cupsdCloseClient(con
);
1717 cupsdLogRequest(con
, HTTP_STATUS_FORBIDDEN
);
1720 else if ((filename
= get_file(con
, &filestats
, buf
,
1721 sizeof(buf
))) == NULL
)
1723 httpClearFields(con
->http
);
1725 if (!cupsdSendHeader(con
, HTTP_STATUS_NOT_FOUND
, "text/html",
1728 cupsdCloseClient(con
);
1732 cupsdLogRequest(con
, HTTP_STATUS_NOT_FOUND
);
1734 else if (!check_if_modified(con
, &filestats
))
1736 if (!cupsdSendError(con
, HTTP_STATUS_NOT_MODIFIED
, CUPSD_AUTH_NONE
))
1738 cupsdCloseClient(con
);
1742 cupsdLogRequest(con
, HTTP_STATUS_NOT_MODIFIED
);
1750 type
= mimeFileType(MimeDatabase
, filename
, NULL
, NULL
);
1752 strlcpy(line
, "text/plain", sizeof(line
));
1754 snprintf(line
, sizeof(line
), "%s/%s", type
->super
, type
->type
);
1756 httpClearFields(con
->http
);
1758 httpSetField(con
->http
, HTTP_FIELD_LAST_MODIFIED
,
1759 httpGetDateString(filestats
.st_mtime
));
1760 httpSetLength(con
->http
, filestats
.st_size
);
1762 if (!cupsdSendHeader(con
, HTTP_STATUS_OK
, line
, CUPSD_AUTH_NONE
))
1764 cupsdCloseClient(con
);
1768 cupsdLogRequest(con
, HTTP_STATUS_OK
);
1773 break; /* Anti-compiler-warning-code */
1779 * Handle any incoming data...
1782 switch (httpGetState(con
->http
))
1784 case HTTP_STATE_PUT_RECV
:
1787 if ((bytes
= httpRead2(con
->http
, line
, sizeof(line
))) < 0)
1789 if (httpError(con
->http
) && httpError(con
->http
) != EPIPE
)
1790 cupsdLogClient(con
, CUPSD_LOG_DEBUG
,
1791 "HTTP_STATE_PUT_RECV Closing for error %d (%s)",
1792 httpError(con
->http
), strerror(httpError(con
->http
)));
1794 cupsdLogClient(con
, CUPSD_LOG_DEBUG
,
1795 "HTTP_STATE_PUT_RECV Closing on EOF.");
1797 cupsdCloseClient(con
);
1802 con
->bytes
+= bytes
;
1804 if (write(con
->file
, line
, bytes
) < bytes
)
1806 cupsdLogClient(con
, CUPSD_LOG_ERROR
,
1807 "Unable to write %d bytes to \"%s\": %s", bytes
,
1808 con
->filename
, strerror(errno
));
1812 unlink(con
->filename
);
1813 cupsdClearString(&con
->filename
);
1815 if (!cupsdSendError(con
, HTTP_STATUS_REQUEST_TOO_LARGE
, CUPSD_AUTH_NONE
))
1817 cupsdCloseClient(con
);
1823 while (httpGetState(con
->http
) == HTTP_STATE_PUT_RECV
&& httpGetReady(con
->http
));
1825 if (httpGetState(con
->http
) == HTTP_STATE_STATUS
)
1828 * End of file, see how big it is...
1831 fstat(con
->file
, &filestats
);
1836 if (filestats
.st_size
> MaxRequestSize
&&
1840 * Request is too big; remove it and send an error...
1843 unlink(con
->filename
);
1844 cupsdClearString(&con
->filename
);
1846 if (!cupsdSendError(con
, HTTP_STATUS_REQUEST_TOO_LARGE
, CUPSD_AUTH_NONE
))
1848 cupsdCloseClient(con
);
1854 * Install the configuration file...
1857 status
= install_cupsd_conf(con
);
1860 * Return the status to the client...
1863 if (!cupsdSendError(con
, status
, CUPSD_AUTH_NONE
))
1865 cupsdCloseClient(con
);
1871 case HTTP_STATE_POST_RECV
:
1874 if (con
->request
&& con
->file
< 0)
1877 * Grab any request data from the connection...
1880 if ((ipp_state
= ippRead(con
->http
, con
->request
)) == IPP_STATE_ERROR
)
1882 cupsdLogClient(con
, CUPSD_LOG_ERROR
, "IPP read error: %s",
1883 cupsLastErrorString());
1885 cupsdSendError(con
, HTTP_STATUS_BAD_REQUEST
, CUPSD_AUTH_NONE
);
1886 cupsdCloseClient(con
);
1889 else if (ipp_state
!= IPP_STATE_DATA
)
1891 if (httpGetState(con
->http
) == HTTP_STATE_POST_SEND
)
1893 cupsdSendError(con
, HTTP_STATUS_BAD_REQUEST
, CUPSD_AUTH_NONE
);
1894 cupsdCloseClient(con
);
1898 if (httpGetReady(con
->http
))
1904 cupsdLogClient(con
, CUPSD_LOG_DEBUG
, "%d.%d %s %d",
1905 con
->request
->request
.op
.version
[0],
1906 con
->request
->request
.op
.version
[1],
1907 ippOpString(con
->request
->request
.op
.operation_id
),
1908 con
->request
->request
.op
.request_id
);
1909 con
->bytes
+= ippLength(con
->request
);
1913 if (con
->file
< 0 && httpGetState(con
->http
) != HTTP_STATE_POST_SEND
)
1916 * Create a file as needed for the request data...
1919 cupsdSetStringf(&con
->filename
, "%s/%08x", RequestRoot
,
1921 con
->file
= open(con
->filename
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0640);
1925 cupsdLogClient(con
, CUPSD_LOG_ERROR
,
1926 "Unable to create request file \"%s\": %s",
1927 con
->filename
, strerror(errno
));
1929 if (!cupsdSendError(con
, HTTP_STATUS_REQUEST_TOO_LARGE
, CUPSD_AUTH_NONE
))
1931 cupsdCloseClient(con
);
1936 fchmod(con
->file
, 0640);
1937 fchown(con
->file
, RunUser
, Group
);
1938 fcntl(con
->file
, F_SETFD
, fcntl(con
->file
, F_GETFD
) | FD_CLOEXEC
);
1941 if (httpGetState(con
->http
) != HTTP_STATE_POST_SEND
)
1943 if (!httpWait(con
->http
, 0))
1945 else if ((bytes
= httpRead2(con
->http
, line
, sizeof(line
))) < 0)
1947 if (httpError(con
->http
) && httpError(con
->http
) != EPIPE
)
1948 cupsdLogClient(con
, CUPSD_LOG_DEBUG
,
1949 "HTTP_STATE_POST_SEND Closing for error %d (%s)",
1950 httpError(con
->http
), strerror(httpError(con
->http
)));
1952 cupsdLogClient(con
, CUPSD_LOG_DEBUG
,
1953 "HTTP_STATE_POST_SEND Closing on EOF.");
1955 cupsdCloseClient(con
);
1960 con
->bytes
+= bytes
;
1962 if (write(con
->file
, line
, bytes
) < bytes
)
1964 cupsdLogClient(con
, CUPSD_LOG_ERROR
,
1965 "Unable to write %d bytes to \"%s\": %s",
1966 bytes
, con
->filename
, strerror(errno
));
1970 unlink(con
->filename
);
1971 cupsdClearString(&con
->filename
);
1973 if (!cupsdSendError(con
, HTTP_STATUS_REQUEST_TOO_LARGE
,
1976 cupsdCloseClient(con
);
1981 else if (httpGetState(con
->http
) == HTTP_STATE_POST_RECV
)
1983 else if (httpGetState(con
->http
) != HTTP_STATE_POST_SEND
)
1985 cupsdLogClient(con
, CUPSD_LOG_DEBUG
,
1986 "Closing on unexpected state %s.",
1987 httpStateString(httpGetState(con
->http
)));
1988 cupsdCloseClient(con
);
1993 while (httpGetState(con
->http
) == HTTP_STATE_POST_RECV
&& httpGetReady(con
->http
));
1995 if (httpGetState(con
->http
) == HTTP_STATE_POST_SEND
)
1999 fstat(con
->file
, &filestats
);
2004 if (filestats
.st_size
> MaxRequestSize
&&
2008 * Request is too big; remove it and send an error...
2011 unlink(con
->filename
);
2012 cupsdClearString(&con
->filename
);
2017 * Delete any IPP request data...
2020 ippDelete(con
->request
);
2021 con
->request
= NULL
;
2024 if (!cupsdSendError(con
, HTTP_STATUS_REQUEST_TOO_LARGE
, CUPSD_AUTH_NONE
))
2026 cupsdCloseClient(con
);
2030 else if (filestats
.st_size
== 0)
2033 * Don't allow empty file...
2036 unlink(con
->filename
);
2037 cupsdClearString(&con
->filename
);
2042 if (!cupsdSendCommand(con
, con
->command
, con
->options
, 0))
2044 if (!cupsdSendError(con
, HTTP_STATUS_NOT_FOUND
, CUPSD_AUTH_NONE
))
2046 cupsdCloseClient(con
);
2051 cupsdLogRequest(con
, HTTP_STATUS_OK
);
2057 cupsdProcessIPPRequest(con
);
2061 unlink(con
->filename
);
2062 cupsdClearString(&con
->filename
);
2071 break; /* Anti-compiler-warning-code */
2074 if (httpGetState(con
->http
) == HTTP_STATE_WAITING
)
2076 if (!httpGetKeepAlive(con
->http
))
2078 cupsdLogClient(con
, CUPSD_LOG_DEBUG
,
2079 "Closing because Keep-Alive is disabled.");
2080 cupsdCloseClient(con
);
2084 cupsArrayRemove(ActiveClients
, con
);
2085 cupsdSetBusyState();
2092 * 'cupsdSendCommand()' - Send output from a command via HTTP.
2095 int /* O - 1 on success, 0 on failure */
2097 cupsd_client_t
*con
, /* I - Client connection */
2098 char *command
, /* I - Command to run */
2099 char *options
, /* I - Command-line options */
2100 int root
) /* I - Run as root? */
2102 int fd
; /* Standard input file descriptor */
2107 fd
= open(con
->filename
, O_RDONLY
);
2111 cupsdLogClient(con
, CUPSD_LOG_ERROR
,
2112 "Unable to open \"%s\" for reading: %s",
2113 con
->filename
? con
->filename
: "/dev/null",
2118 fcntl(fd
, F_SETFD
, fcntl(fd
, F_GETFD
) | FD_CLOEXEC
);
2123 con
->pipe_pid
= pipe_command(con
, fd
, &(con
->file
), command
, options
, root
);
2124 con
->pipe_status
= HTTP_STATUS_OK
;
2126 httpClearFields(con
->http
);
2131 cupsdLogClient(con
, CUPSD_LOG_INFO
, "Started \"%s\" (pid=%d, file=%d)",
2132 command
, con
->pipe_pid
, con
->file
);
2134 if (con
->pipe_pid
== 0)
2137 fcntl(con
->file
, F_SETFD
, fcntl(con
->file
, F_GETFD
) | FD_CLOEXEC
);
2139 cupsdAddSelect(con
->file
, (cupsd_selfunc_t
)write_pipe
, NULL
, con
);
2141 cupsdLogClient(con
, CUPSD_LOG_DEBUG
, "Waiting for CGI data.");
2143 con
->sent_header
= 0;
2144 con
->file_ready
= 0;
2145 con
->got_fields
= 0;
2146 con
->header_used
= 0;
2153 * 'cupsdSendError()' - Send an error message via HTTP.
2156 int /* O - 1 if successful, 0 otherwise */
2157 cupsdSendError(cupsd_client_t
*con
, /* I - Connection */
2158 http_status_t code
, /* I - Error code */
2159 int auth_type
)/* I - Authentication type */
2161 cupsdLogClient(con
, CUPSD_LOG_DEBUG2
, "cupsdSendError code=%d, auth_type=%d",
2166 * Force client to upgrade for authentication if that is how the
2167 * server is configured...
2170 if (code
== HTTP_STATUS_UNAUTHORIZED
&&
2171 DefaultEncryption
== HTTP_ENCRYPTION_REQUIRED
&&
2172 _cups_strcasecmp(httpGetHostname(con
->http
, NULL
, 0), "localhost") &&
2173 !httpIsEncrypted(con
->http
))
2175 code
= HTTP_STATUS_UPGRADE_REQUIRED
;
2177 #endif /* HAVE_SSL */
2180 * Put the request in the access_log file...
2183 cupsdLogRequest(con
, code
);
2186 * To work around bugs in some proxies, don't use Keep-Alive for some
2189 * Kerberos authentication doesn't work without Keep-Alive, so
2190 * never disable it in that case.
2193 httpClearFields(con
->http
);
2195 if (code
>= HTTP_STATUS_BAD_REQUEST
&& con
->type
!= CUPSD_AUTH_NEGOTIATE
)
2196 httpSetKeepAlive(con
->http
, HTTP_KEEPALIVE_OFF
);
2198 if (httpGetVersion(con
->http
) >= HTTP_VERSION_1_1
&&
2199 httpGetKeepAlive(con
->http
) == HTTP_KEEPALIVE_OFF
)
2200 httpSetField(con
->http
, HTTP_FIELD_CONNECTION
, "close");
2202 if (code
>= HTTP_STATUS_BAD_REQUEST
)
2205 * Send a human-readable error message.
2208 char message
[4096], /* Message for user */
2209 urltext
[1024], /* URL redirection text */
2210 redirect
[1024]; /* Redirection link */
2211 const char *text
; /* Status-specific text */
2216 if (code
== HTTP_STATUS_UNAUTHORIZED
)
2217 text
= _cupsLangString(con
->language
,
2218 _("Enter your username and password or the "
2219 "root username and password to access this "
2220 "page. If you are using Kerberos authentication, "
2221 "make sure you have a valid Kerberos ticket."));
2222 else if (code
== HTTP_STATUS_UPGRADE_REQUIRED
)
2226 snprintf(urltext
, sizeof(urltext
),
2227 _cupsLangString(con
->language
,
2228 _("You must access this page using the URL "
2229 "<A HREF=\"https://%s:%d%s\">"
2230 "https://%s:%d%s</A>.")),
2231 con
->servername
, con
->serverport
, con
->uri
,
2232 con
->servername
, con
->serverport
, con
->uri
);
2234 snprintf(redirect
, sizeof(redirect
),
2235 "<META HTTP-EQUIV=\"Refresh\" "
2236 "CONTENT=\"3;URL=https://%s:%d%s\">\n",
2237 con
->servername
, con
->serverport
, con
->uri
);
2239 else if (code
== HTTP_STATUS_CUPS_WEBIF_DISABLED
)
2240 text
= _cupsLangString(con
->language
,
2241 _("The web interface is currently disabled. Run "
2242 "\"cupsctl WebInterface=yes\" to enable it."));
2246 snprintf(message
, sizeof(message
),
2247 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" "
2248 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
2251 "\t<META HTTP-EQUIV=\"Content-Type\" "
2252 "CONTENT=\"text/html; charset=utf-8\">\n"
2253 "\t<TITLE>%s - " CUPS_SVERSION
"</TITLE>\n"
2254 "\t<LINK REL=\"STYLESHEET\" TYPE=\"text/css\" "
2255 "HREF=\"/cups.css\">\n"
2263 _httpStatus(con
->language
, code
), redirect
,
2264 _httpStatus(con
->language
, code
), text
);
2267 * Send an error message back to the client. If the error code is a
2268 * 400 or 500 series, make sure the message contains some text, too!
2271 size_t length
= strlen(message
); /* Length of message */
2273 httpSetLength(con
->http
, length
);
2275 if (!cupsdSendHeader(con
, code
, "text/html", auth_type
))
2278 if (httpWrite2(con
->http
, message
, length
) < 0)
2281 if (httpFlushWrite(con
->http
) < 0)
2286 httpSetField(con
->http
, HTTP_FIELD_CONTENT_LENGTH
, "0");
2288 if (!cupsdSendHeader(con
, code
, NULL
, auth_type
))
2297 * 'cupsdSendHeader()' - Send an HTTP request.
2300 int /* O - 1 on success, 0 on failure */
2302 cupsd_client_t
*con
, /* I - Client to send to */
2303 http_status_t code
, /* I - HTTP status code */
2304 char *type
, /* I - MIME type of document */
2305 int auth_type
) /* I - Type of authentication */
2307 char auth_str
[1024]; /* Authorization string */
2311 * Send the HTTP status header...
2314 if (code
== HTTP_STATUS_CUPS_WEBIF_DISABLED
)
2317 * Treat our special "web interface is disabled" status as "200 OK" for web
2321 code
= HTTP_STATUS_OK
;
2325 httpSetField(con
->http
, HTTP_FIELD_SERVER
, ServerHeader
);
2327 if (code
== HTTP_STATUS_METHOD_NOT_ALLOWED
)
2328 httpSetField(con
->http
, HTTP_FIELD_ALLOW
, "GET, HEAD, OPTIONS, POST, PUT");
2330 if (code
== HTTP_STATUS_UNAUTHORIZED
)
2332 if (auth_type
== CUPSD_AUTH_NONE
)
2334 if (!con
->best
|| con
->best
->type
<= CUPSD_AUTH_NONE
)
2335 auth_type
= cupsdDefaultAuthType();
2337 auth_type
= con
->best
->type
;
2342 if (auth_type
== CUPSD_AUTH_BASIC
|| auth_type
== CUPSD_AUTH_BASICDIGEST
)
2343 strlcpy(auth_str
, "Basic realm=\"CUPS\"", sizeof(auth_str
));
2344 else if (auth_type
== CUPSD_AUTH_DIGEST
)
2345 snprintf(auth_str
, sizeof(auth_str
), "Digest realm=\"CUPS\", nonce=\"%s\"",
2346 httpGetHostname(con
->http
, NULL
, 0));
2348 else if (auth_type
== CUPSD_AUTH_NEGOTIATE
)
2351 if (httpAddrFamily(httpGetAddress(con
->http
)) == AF_LOCAL
)
2352 strlcpy(auth_str
, "Basic realm=\"CUPS\"", sizeof(auth_str
));
2354 # endif /* AF_LOCAL */
2355 strlcpy(auth_str
, "Negotiate", sizeof(auth_str
));
2357 #endif /* HAVE_GSSAPI */
2359 if (con
->best
&& auth_type
!= CUPSD_AUTH_NEGOTIATE
&&
2360 !_cups_strcasecmp(httpGetHostname(con
->http
, NULL
, 0), "localhost"))
2363 * Add a "trc" (try root certification) parameter for local non-Kerberos
2364 * requests when the request requires system group membership - then the
2365 * client knows the root certificate can/should be used.
2367 * Also, for OS X we also look for @AUTHKEY and add an "authkey"
2368 * parameter as needed...
2371 char *name
, /* Current user name */
2372 *auth_key
; /* Auth key buffer */
2373 size_t auth_size
; /* Size of remaining buffer */
2375 auth_key
= auth_str
+ strlen(auth_str
);
2376 auth_size
= sizeof(auth_str
) - (auth_key
- auth_str
);
2378 for (name
= (char *)cupsArrayFirst(con
->best
->names
);
2380 name
= (char *)cupsArrayNext(con
->best
->names
))
2382 #ifdef HAVE_AUTHORIZATION_H
2383 if (!_cups_strncasecmp(name
, "@AUTHKEY(", 9))
2385 snprintf(auth_key
, auth_size
, ", authkey=\"%s\"", name
+ 9);
2386 /* end parenthesis is stripped in conf.c */
2390 #endif /* HAVE_AUTHORIZATION_H */
2391 if (!_cups_strcasecmp(name
, "@SYSTEM"))
2393 #ifdef HAVE_AUTHORIZATION_H
2394 if (SystemGroupAuthKey
)
2395 snprintf(auth_key
, auth_size
,
2397 SystemGroupAuthKey
);
2400 strlcpy(auth_key
, ", trc=\"y\"", auth_size
);
2401 #endif /* HAVE_AUTHORIZATION_H */
2409 cupsdLogClient(con
, CUPSD_LOG_DEBUG
, "WWW-Authenticate: %s", auth_str
);
2411 httpSetField(con
->http
, HTTP_FIELD_WWW_AUTHENTICATE
, auth_str
);
2415 if (con
->language
&& strcmp(con
->language
->language
, "C"))
2416 httpSetField(con
->http
, HTTP_FIELD_CONTENT_LANGUAGE
, con
->language
->language
);
2420 if (!strcmp(type
, "text/html"))
2421 httpSetField(con
->http
, HTTP_FIELD_CONTENT_TYPE
, "text/html; charset=utf-8");
2423 httpSetField(con
->http
, HTTP_FIELD_CONTENT_TYPE
, type
);
2426 return (!httpWriteResponse(con
->http
, code
));
2431 * 'cupsdUpdateCGI()' - Read status messages from CGI scripts and programs.
2435 cupsdUpdateCGI(void)
2437 char *ptr
, /* Pointer to end of line in buffer */
2438 message
[1024]; /* Pointer to message text */
2439 int loglevel
; /* Log level for message */
2442 while ((ptr
= cupsdStatBufUpdate(CGIStatusBuffer
, &loglevel
,
2443 message
, sizeof(message
))) != NULL
)
2445 if (loglevel
== CUPSD_LOG_INFO
)
2446 cupsdLogMessage(CUPSD_LOG_INFO
, "%s", message
);
2448 if (!strchr(CGIStatusBuffer
->buffer
, '\n'))
2452 if (ptr
== NULL
&& !CGIStatusBuffer
->bufused
)
2455 * Fatal error on pipe - should never happen!
2458 cupsdLogMessage(CUPSD_LOG_CRIT
,
2459 "cupsdUpdateCGI: error reading from CGI error pipe - %s",
2466 * 'cupsdWriteClient()' - Write data to a client as needed.
2470 cupsdWriteClient(cupsd_client_t
*con
) /* I - Client connection */
2472 int bytes
, /* Number of bytes written */
2473 field_col
; /* Current column */
2474 char *bufptr
, /* Pointer into buffer */
2475 *bufend
; /* Pointer to end of buffer */
2476 ipp_state_t ipp_state
; /* IPP state value */
2479 cupsdLogClient(con
, CUPSD_LOG_DEBUG
, "con->http=%p", con
->http
);
2480 cupsdLogClient(con
, CUPSD_LOG_DEBUG
,
2485 "data_encoding=HTTP_ENCODING_%s, "
2486 "data_remaining=" CUPS_LLFMT
", "
2490 httpError(con
->http
), (int)httpGetReady(con
->http
),
2491 httpStateString(httpGetState(con
->http
)),
2492 httpIsChunked(con
->http
) ? "CHUNKED" : "LENGTH",
2493 CUPS_LLCAST
httpGetLength2(con
->http
),
2495 con
->response
? ippStateString(ippGetState(con
->request
)) : "",
2496 con
->pipe_pid
, con
->file
);
2498 if (httpGetState(con
->http
) != HTTP_STATE_GET_SEND
&&
2499 httpGetState(con
->http
) != HTTP_STATE_POST_SEND
)
2502 * If we get called in the wrong state, then something went wrong with the
2503 * connection and we need to shut it down...
2506 cupsdLogClient(con
, CUPSD_LOG_DEBUG
, "Closing on unexpected HTTP write state %s.",
2507 httpStateString(httpGetState(con
->http
)));
2508 cupsdCloseClient(con
);
2515 * Make sure we select on the CGI output...
2518 cupsdAddSelect(con
->file
, (cupsd_selfunc_t
)write_pipe
, NULL
, con
);
2520 cupsdLogClient(con
, CUPSD_LOG_DEBUG
, "Waiting for CGI data.");
2522 if (!con
->file_ready
)
2525 * Try again later when there is CGI output available...
2528 cupsdRemoveSelect(httpGetFd(con
->http
));
2532 con
->file_ready
= 0;
2535 if (con
->response
&& con
->response
->state
!= IPP_STATE_DATA
)
2537 size_t wused
= httpGetPending(con
->http
); /* Previous write buffer use */
2542 * Write a single attribute or the IPP message header...
2545 ipp_state
= ippWrite(con
->http
, con
->response
);
2548 * If the write buffer has been flushed, stop buffering up attributes...
2551 if (httpGetPending(con
->http
) <= wused
)
2554 while (ipp_state
!= IPP_STATE_DATA
&& ipp_state
!= IPP_STATE_ERROR
);
2556 cupsdLogClient(con
, CUPSD_LOG_DEBUG
,
2557 "Writing IPP response, ipp_state=%s, old "
2558 "wused=" CUPS_LLFMT
", new wused=" CUPS_LLFMT
,
2559 ippStateString(ipp_state
),
2560 CUPS_LLCAST wused
, CUPS_LLCAST
httpGetPending(con
->http
));
2562 if (httpGetPending(con
->http
) > 0)
2563 httpFlushWrite(con
->http
);
2565 bytes
= ipp_state
!= IPP_STATE_ERROR
&&
2566 (con
->file
>= 0 || ipp_state
!= IPP_STATE_DATA
);
2568 cupsdLogClient(con
, CUPSD_LOG_DEBUG
,
2569 "bytes=%d, http_state=%d, data_remaining=" CUPS_LLFMT
,
2570 (int)bytes
, httpGetState(con
->http
),
2571 CUPS_LLCAST
httpGetLength2(con
->http
));
2573 else if ((bytes
= read(con
->file
, con
->header
+ con
->header_used
,
2574 sizeof(con
->header
) - con
->header_used
)) > 0)
2576 con
->header_used
+= bytes
;
2578 if (con
->pipe_pid
&& !con
->got_fields
)
2581 * Inspect the data for Content-Type and other fields.
2584 for (bufptr
= con
->header
, bufend
= con
->header
+ con
->header_used
,
2586 !con
->got_fields
&& bufptr
< bufend
;
2589 if (*bufptr
== '\n')
2592 * Send line to client...
2595 if (bufptr
> con
->header
&& bufptr
[-1] == '\r')
2599 cupsdLogClient(con
, CUPSD_LOG_DEBUG
, "Script header: %s", con
->header
);
2601 if (!con
->sent_header
)
2604 * Handle redirection and CGI status codes...
2607 http_field_t field
; /* HTTP field */
2608 char *value
= strchr(con
->header
, ':');
2609 /* Value of field */
2614 while (isspace(*value
& 255))
2618 field
= httpFieldValue(con
->header
);
2620 if (field
!= HTTP_FIELD_UNKNOWN
&& value
)
2622 httpSetField(con
->http
, field
, value
);
2624 if (field
== HTTP_FIELD_LOCATION
)
2626 con
->pipe_status
= HTTP_STATUS_SEE_OTHER
;
2627 con
->sent_header
= 2;
2630 con
->sent_header
= 1;
2632 else if (!_cups_strcasecmp(con
->header
, "Status") && value
)
2634 con
->pipe_status
= (http_status_t
)atoi(value
);
2635 con
->sent_header
= 2;
2637 else if (!_cups_strcasecmp(con
->header
, "Set-Cookie") && value
)
2639 char *sep
= strchr(value
, ';');
2640 /* Separator between name=value and the rest */
2645 httpSetCookie(con
->http
, value
);
2646 con
->sent_header
= 1;
2654 con
->header_used
-= bufptr
- con
->header
;
2656 if (con
->header_used
> 0)
2657 memmove(con
->header
, bufptr
, con
->header_used
);
2659 bufptr
= con
->header
- 1;
2662 * See if the line was empty...
2667 con
->got_fields
= 1;
2669 if (httpGetVersion(con
->http
) == HTTP_VERSION_1_1
&&
2670 !httpGetField(con
->http
, HTTP_FIELD_CONTENT_LENGTH
)[0])
2671 httpSetLength(con
->http
, 0);
2673 if (!cupsdSendHeader(con
, con
->pipe_status
, NULL
, CUPSD_AUTH_NONE
))
2675 cupsdCloseClient(con
);
2682 else if (*bufptr
!= '\r')
2686 if (!con
->got_fields
)
2690 if (con
->header_used
> 0)
2692 if (httpWrite2(con
->http
, con
->header
, con
->header_used
) < 0)
2694 cupsdLogClient(con
, CUPSD_LOG_DEBUG
, "Closing for error %d (%s)",
2695 httpError(con
->http
), strerror(httpError(con
->http
)));
2696 cupsdCloseClient(con
);
2700 if (httpIsChunked(con
->http
))
2701 httpFlushWrite(con
->http
);
2703 con
->bytes
+= con
->header_used
;
2705 if (httpGetState(con
->http
) == HTTP_STATE_WAITING
)
2708 bytes
= con
->header_used
;
2710 con
->header_used
= 0;
2715 (httpGetState(con
->http
) != HTTP_STATE_GET_SEND
&&
2716 httpGetState(con
->http
) != HTTP_STATE_POST_SEND
))
2718 if (!con
->sent_header
&& con
->pipe_pid
)
2719 cupsdSendError(con
, HTTP_STATUS_SERVER_ERROR
, CUPSD_AUTH_NONE
);
2722 cupsdLogRequest(con
, HTTP_STATUS_OK
);
2724 if (httpIsChunked(con
->http
) && (!con
->pipe_pid
|| con
->sent_header
== 1))
2726 cupsdLogClient(con
, CUPSD_LOG_DEBUG
, "Sending 0-length chunk.");
2728 if (httpWrite2(con
->http
, "", 0) < 0)
2730 cupsdLogClient(con
, CUPSD_LOG_DEBUG
, "Closing for error %d (%s)",
2731 httpError(con
->http
), strerror(httpError(con
->http
)));
2732 cupsdCloseClient(con
);
2737 cupsdLogClient(con
, CUPSD_LOG_DEBUG
, "Flushing write buffer.");
2738 httpFlushWrite(con
->http
);
2739 cupsdLogClient(con
, CUPSD_LOG_DEBUG
, "New state is %s", httpStateString(httpGetState(con
->http
)));
2742 cupsdAddSelect(httpGetFd(con
->http
), (cupsd_selfunc_t
)cupsdReadClient
, NULL
, con
);
2744 cupsdLogClient(con
, CUPSD_LOG_DEBUG
, "Waiting for request.");
2748 cupsdRemoveSelect(con
->file
);
2751 cupsdEndProcess(con
->pipe_pid
, 0);
2760 unlink(con
->filename
);
2761 cupsdClearString(&con
->filename
);
2766 ippDelete(con
->request
);
2767 con
->request
= NULL
;
2772 ippDelete(con
->response
);
2773 con
->response
= NULL
;
2776 cupsdClearString(&con
->command
);
2777 cupsdClearString(&con
->options
);
2778 cupsdClearString(&con
->query_string
);
2780 if (!httpGetKeepAlive(con
->http
))
2782 cupsdLogClient(con
, CUPSD_LOG_DEBUG
,
2783 "Closing because Keep-Alive is disabled.");
2784 cupsdCloseClient(con
);
2789 cupsArrayRemove(ActiveClients
, con
);
2790 cupsdSetBusyState();
2797 * 'check_if_modified()' - Decode an "If-Modified-Since" line.
2800 static int /* O - 1 if modified since */
2802 cupsd_client_t
*con
, /* I - Client connection */
2803 struct stat
*filestats
) /* I - File information */
2805 const char *ptr
; /* Pointer into field */
2806 time_t date
; /* Time/date value */
2807 off_t size
; /* Size/length value */
2812 ptr
= httpGetField(con
->http
, HTTP_FIELD_IF_MODIFIED_SINCE
);
2817 cupsdLogClient(con
, CUPSD_LOG_DEBUG2
,
2818 "check_if_modified "
2819 "filestats=%p(" CUPS_LLFMT
", %d)) If-Modified-Since=\"%s\"",
2820 filestats
, CUPS_LLCAST filestats
->st_size
,
2821 (int)filestats
->st_mtime
, ptr
);
2823 while (*ptr
!= '\0')
2825 while (isspace(*ptr
) || *ptr
== ';')
2828 if (_cups_strncasecmp(ptr
, "length=", 7) == 0)
2831 size
= strtoll(ptr
, NULL
, 10);
2833 while (isdigit(*ptr
))
2836 else if (isalpha(*ptr
))
2838 date
= httpGetDateTime(ptr
);
2839 while (*ptr
!= '\0' && *ptr
!= ';')
2846 return ((size
!= filestats
->st_size
&& size
!= 0) ||
2847 (date
< filestats
->st_mtime
&& date
!= 0) ||
2848 (size
== 0 && date
== 0));
2853 * 'compare_clients()' - Compare two client connections.
2856 static int /* O - Result of comparison */
2857 compare_clients(cupsd_client_t
*a
, /* I - First client */
2858 cupsd_client_t
*b
, /* I - Second client */
2859 void *data
) /* I - User data (not used) */
2874 * 'cupsd_start_tls()' - Start encryption on a connection.
2877 static int /* O - 0 on success, -1 on error */
2878 cupsd_start_tls(cupsd_client_t
*con
, /* I - Client connection */
2879 http_encryption_t e
) /* I - Encryption mode */
2881 if (httpEncryption(con
->http
, e
))
2883 cupsdLogClient(con
, CUPSD_LOG_ERROR
, "Unable to encrypt connection: %s",
2884 cupsLastErrorString());
2888 cupsdLogClient(con
, CUPSD_LOG_INFO
, "Connection now encrypted.");
2891 #endif /* HAVE_SSL */
2895 * 'get_file()' - Get a filename and state info.
2898 static char * /* O - Real filename */
2899 get_file(cupsd_client_t
*con
, /* I - Client connection */
2900 struct stat
*filestats
, /* O - File information */
2901 char *filename
, /* IO - Filename buffer */
2902 int len
) /* I - Buffer length */
2904 int status
; /* Status of filesystem calls */
2905 char *ptr
; /* Pointer info filename */
2906 int plen
; /* Remaining length after pointer */
2907 char language
[7]; /* Language subdirectory, if any */
2911 * Figure out the real filename...
2916 if (!strncmp(con
->uri
, "/ppd/", 5) && !strchr(con
->uri
+ 5, '/'))
2917 snprintf(filename
, len
, "%s%s", ServerRoot
, con
->uri
);
2918 else if (!strncmp(con
->uri
, "/icons/", 7) && !strchr(con
->uri
+ 7, '/'))
2920 snprintf(filename
, len
, "%s/%s", CacheDir
, con
->uri
+ 7);
2921 if (access(filename
, F_OK
) < 0)
2922 snprintf(filename
, len
, "%s/images/generic.png", DocumentRoot
);
2924 else if (!strncmp(con
->uri
, "/rss/", 5) && !strchr(con
->uri
+ 5, '/'))
2925 snprintf(filename
, len
, "%s/rss/%s", CacheDir
, con
->uri
+ 5);
2926 else if (!strncmp(con
->uri
, "/admin/conf/", 12))
2927 snprintf(filename
, len
, "%s%s", ServerRoot
, con
->uri
+ 11);
2928 else if (!strncmp(con
->uri
, "/admin/log/", 11))
2930 if (!strncmp(con
->uri
+ 11, "access_log", 10) && AccessLog
[0] == '/')
2931 strlcpy(filename
, AccessLog
, len
);
2932 else if (!strncmp(con
->uri
+ 11, "error_log", 9) && ErrorLog
[0] == '/')
2933 strlcpy(filename
, ErrorLog
, len
);
2934 else if (!strncmp(con
->uri
+ 11, "page_log", 8) && PageLog
[0] == '/')
2935 strlcpy(filename
, PageLog
, len
);
2939 else if (con
->language
)
2941 snprintf(language
, sizeof(language
), "/%s", con
->language
->language
);
2942 snprintf(filename
, len
, "%s%s%s", DocumentRoot
, language
, con
->uri
);
2945 snprintf(filename
, len
, "%s%s", DocumentRoot
, con
->uri
);
2947 if ((ptr
= strchr(filename
, '?')) != NULL
)
2951 * Grab the status for this language; if there isn't a language-specific file
2952 * then fallback to the default one...
2955 if ((status
= stat(filename
, filestats
)) != 0 && language
[0] &&
2956 strncmp(con
->uri
, "/icons/", 7) &&
2957 strncmp(con
->uri
, "/ppd/", 5) &&
2958 strncmp(con
->uri
, "/rss/", 5) &&
2959 strncmp(con
->uri
, "/admin/conf/", 12) &&
2960 strncmp(con
->uri
, "/admin/log/", 11))
2963 * Drop the country code...
2967 snprintf(filename
, len
, "%s%s%s", DocumentRoot
, language
, con
->uri
);
2969 if ((ptr
= strchr(filename
, '?')) != NULL
)
2972 if ((status
= stat(filename
, filestats
)) != 0)
2975 * Drop the language prefix and try the root directory...
2979 snprintf(filename
, len
, "%s%s", DocumentRoot
, con
->uri
);
2981 if ((ptr
= strchr(filename
, '?')) != NULL
)
2984 status
= stat(filename
, filestats
);
2989 * If we're found a directory, get the index.html file instead...
2992 if (!status
&& S_ISDIR(filestats
->st_mode
))
2995 * Make sure the URI ends with a slash...
2998 if (con
->uri
[strlen(con
->uri
) - 1] != '/')
2999 strlcat(con
->uri
, "/", sizeof(con
->uri
));
3002 * Find the directory index file, trying every language...
3007 if (status
&& language
[0])
3010 * Try a different language subset...
3014 language
[0] = '\0'; /* Strip country code */
3016 language
[0] = '\0'; /* Strip language */
3020 * Look for the index file...
3023 snprintf(filename
, len
, "%s%s%s", DocumentRoot
, language
, con
->uri
);
3025 if ((ptr
= strchr(filename
, '?')) != NULL
)
3028 ptr
= filename
+ strlen(filename
);
3029 plen
= len
- (ptr
- filename
);
3031 strlcpy(ptr
, "index.html", plen
);
3032 status
= stat(filename
, filestats
);
3037 strlcpy(ptr
, "index.class", plen
);
3038 status
= stat(filename
, filestats
);
3040 #endif /* HAVE_JAVA */
3045 strlcpy(ptr
, "index.pl", plen
);
3046 status
= stat(filename
, filestats
);
3048 #endif /* HAVE_PERL */
3053 strlcpy(ptr
, "index.php", plen
);
3054 status
= stat(filename
, filestats
);
3056 #endif /* HAVE_PHP */
3061 strlcpy(ptr
, "index.pyc", plen
);
3062 status
= stat(filename
, filestats
);
3067 strlcpy(ptr
, "index.py", plen
);
3068 status
= stat(filename
, filestats
);
3070 #endif /* HAVE_PYTHON */
3073 while (status
&& language
[0]);
3076 cupsdLogClient(con
, CUPSD_LOG_DEBUG2
,
3077 "get_file filestats=%p, filename=%p, len=%d, "
3078 "returning \"%s\".", filestats
, filename
, len
,
3079 status
? "(null)" : filename
);
3089 * 'install_cupsd_conf()' - Install a configuration file.
3092 static http_status_t
/* O - Status */
3093 install_cupsd_conf(cupsd_client_t
*con
) /* I - Connection */
3095 char filename
[1024]; /* Configuration filename */
3096 cups_file_t
*in
, /* Input file */
3097 *out
; /* Output file */
3098 char buffer
[16384]; /* Copy buffer */
3099 ssize_t bytes
; /* Number of bytes */
3103 * Open the request file...
3106 if ((in
= cupsFileOpen(con
->filename
, "rb")) == NULL
)
3108 cupsdLogClient(con
, CUPSD_LOG_ERROR
, "Unable to open request file \"%s\": %s",
3109 con
->filename
, strerror(errno
));
3110 return (HTTP_STATUS_SERVER_ERROR
);
3114 * Open the new config file...
3117 if ((out
= cupsdCreateConfFile(ConfigurationFile
, ConfigFilePerm
)) == NULL
)
3120 return (HTTP_STATUS_SERVER_ERROR
);
3123 cupsdLogClient(con
, CUPSD_LOG_INFO
, "Installing config file \"%s\"...",
3127 * Copy from the request to the new config file...
3130 while ((bytes
= cupsFileRead(in
, buffer
, sizeof(buffer
))) > 0)
3131 if (cupsFileWrite(out
, buffer
, bytes
) < bytes
)
3133 cupsdLogClient(con
, CUPSD_LOG_ERROR
,
3134 "Unable to copy to config file \"%s\": %s",
3135 ConfigurationFile
, strerror(errno
));
3140 snprintf(filename
, sizeof(filename
), "%s.N", ConfigurationFile
);
3141 cupsdUnlinkOrRemoveFile(filename
);
3143 return (HTTP_STATUS_SERVER_ERROR
);
3147 * Close the files...
3152 if (cupsdCloseCreatedConfFile(out
, ConfigurationFile
))
3153 return (HTTP_STATUS_SERVER_ERROR
);
3156 * Remove the request file...
3159 cupsdUnlinkOrRemoveFile(con
->filename
);
3160 cupsdClearString(&con
->filename
);
3163 * Set the NeedReload flag...
3166 NeedReload
= RELOAD_CUPSD
;
3167 ReloadTime
= time(NULL
);
3170 * Return that the file was created successfully...
3173 return (HTTP_STATUS_CREATED
);
3178 * 'is_cgi()' - Is the resource a CGI script/program?
3181 static int /* O - 1 = CGI, 0 = file */
3182 is_cgi(cupsd_client_t
*con
, /* I - Client connection */
3183 const char *filename
, /* I - Real filename */
3184 struct stat
*filestats
, /* I - File information */
3185 mime_type_t
*type
) /* I - MIME type */
3187 const char *options
; /* Options on URL */
3191 * Get the options, if any...
3194 if ((options
= strchr(con
->uri
, '?')) != NULL
)
3197 cupsdSetStringf(&(con
->query_string
), "QUERY_STRING=%s", options
);
3201 * Check for known types...
3204 if (!type
|| _cups_strcasecmp(type
->super
, "application"))
3206 cupsdLogClient(con
, CUPSD_LOG_DEBUG2
,
3207 "is_cgi filename=\"%s\", filestats=%p, "
3208 "type=%s/%s, returning 0", filename
,
3209 filestats
, type
? type
->super
: "unknown",
3210 type
? type
->type
: "unknown");
3214 if (!_cups_strcasecmp(type
->type
, "x-httpd-cgi") &&
3215 (filestats
->st_mode
& 0111))
3218 * "application/x-httpd-cgi" is a CGI script.
3221 cupsdSetString(&con
->command
, filename
);
3224 cupsdSetStringf(&con
->options
, " %s", options
);
3226 cupsdLogClient(con
, CUPSD_LOG_DEBUG2
,
3227 "is_cgi filename=\"%s\", filestats=%p, "
3228 "type=%s/%s, returning 1", filename
,
3229 filestats
, type
->super
, type
->type
);
3233 else if (!_cups_strcasecmp(type
->type
, "x-httpd-java"))
3236 * "application/x-httpd-java" is a Java servlet.
3239 cupsdSetString(&con
->command
, CUPS_JAVA
);
3242 cupsdSetStringf(&con
->options
, " %s %s", filename
, options
);
3244 cupsdSetStringf(&con
->options
, " %s", filename
);
3246 cupsdLogClient(con
, CUPSD_LOG_DEBUG2
,
3247 "is_cgi filename=\"%s\", filestats=%p, "
3248 "type=%s/%s, returning 1", filename
,
3249 filestats
, type
->super
, type
->type
);
3252 #endif /* HAVE_JAVA */
3254 else if (!_cups_strcasecmp(type
->type
, "x-httpd-perl"))
3257 * "application/x-httpd-perl" is a Perl page.
3260 cupsdSetString(&con
->command
, CUPS_PERL
);
3263 cupsdSetStringf(&con
->options
, " %s %s", filename
, options
);
3265 cupsdSetStringf(&con
->options
, " %s", filename
);
3267 cupsdLogClient(con
, CUPSD_LOG_DEBUG2
,
3268 "is_cgi filename=\"%s\", filestats=%p, "
3269 "type=%s/%s, returning 1", filename
,
3270 filestats
, type
->super
, type
->type
);
3273 #endif /* HAVE_PERL */
3275 else if (!_cups_strcasecmp(type
->type
, "x-httpd-php"))
3278 * "application/x-httpd-php" is a PHP page.
3281 cupsdSetString(&con
->command
, CUPS_PHP
);
3284 cupsdSetStringf(&con
->options
, " %s %s", filename
, options
);
3286 cupsdSetStringf(&con
->options
, " %s", filename
);
3288 cupsdLogClient(con
, CUPSD_LOG_DEBUG2
,
3289 "is_cgi filename=\"%s\", filestats=%p, "
3290 "type=%s/%s, returning 1", filename
,
3291 filestats
, type
->super
, type
->type
);
3294 #endif /* HAVE_PHP */
3296 else if (!_cups_strcasecmp(type
->type
, "x-httpd-python"))
3299 * "application/x-httpd-python" is a Python page.
3302 cupsdSetString(&con
->command
, CUPS_PYTHON
);
3305 cupsdSetStringf(&con
->options
, " %s %s", filename
, options
);
3307 cupsdSetStringf(&con
->options
, " %s", filename
);
3309 cupsdLogClient(con
, CUPSD_LOG_DEBUG2
,
3310 "is_cgi filename=\"%s\", filestats=%p, "
3311 "type=%s/%s, returning 1", filename
,
3312 filestats
, type
->super
, type
->type
);
3315 #endif /* HAVE_PYTHON */
3317 cupsdLogClient(con
, CUPSD_LOG_DEBUG2
,
3318 "is_cgi filename=\"%s\", filestats=%p, "
3319 "type=%s/%s, returning 0", filename
,
3320 filestats
, type
->super
, type
->type
);
3326 * 'is_path_absolute()' - Is a path absolute and free of relative elements (i.e. "..").
3329 static int /* O - 0 if relative, 1 if absolute */
3330 is_path_absolute(const char *path
) /* I - Input path */
3333 * Check for a leading slash...
3340 * Check for "/.." in the path...
3343 while ((path
= strstr(path
, "/..")) != NULL
)
3345 if (!path
[3] || path
[3] == '/')
3352 * If we haven't found any relative paths, return 1 indicating an
3361 * 'pipe_command()' - Pipe the output of a command to the remote client.
3364 static int /* O - Process ID */
3365 pipe_command(cupsd_client_t
*con
, /* I - Client connection */
3366 int infile
, /* I - Standard input for command */
3367 int *outfile
, /* O - Standard output for command */
3368 char *command
, /* I - Command to run */
3369 char *options
, /* I - Options for command */
3370 int root
) /* I - Run as root? */
3372 int i
; /* Looping var */
3373 int pid
; /* Process ID */
3374 char *commptr
, /* Command string pointer */
3375 commch
; /* Command string character */
3376 char *uriptr
; /* URI string pointer */
3377 int fds
[2]; /* Pipe FDs */
3378 int argc
; /* Number of arguments */
3379 int envc
; /* Number of environment variables */
3380 char argbuf
[10240], /* Argument buffer */
3381 *argv
[100], /* Argument strings */
3382 *envp
[MAX_ENV
+ 20]; /* Environment variables */
3383 char auth_type
[256], /* AUTH_TYPE environment variable */
3384 content_length
[1024], /* CONTENT_LENGTH environment variable */
3385 content_type
[1024], /* CONTENT_TYPE environment variable */
3386 http_cookie
[32768], /* HTTP_COOKIE environment variable */
3387 http_referer
[1024], /* HTTP_REFERER environment variable */
3388 http_user_agent
[1024], /* HTTP_USER_AGENT environment variable */
3389 lang
[1024], /* LANG environment variable */
3390 path_info
[1024], /* PATH_INFO environment variable */
3391 remote_addr
[1024], /* REMOTE_ADDR environment variable */
3392 remote_host
[1024], /* REMOTE_HOST environment variable */
3393 remote_user
[1024], /* REMOTE_USER environment variable */
3394 script_filename
[1024], /* SCRIPT_FILENAME environment variable */
3395 script_name
[1024], /* SCRIPT_NAME environment variable */
3396 server_name
[1024], /* SERVER_NAME environment variable */
3397 server_port
[1024]; /* SERVER_PORT environment variable */
3398 ipp_attribute_t
*attr
; /* attributes-natural-language attribute */
3402 * Parse a copy of the options string, which is of the form:
3404 * argument+argument+argument
3405 * ?argument+argument+argument
3406 * param=value¶m=value
3407 * ?param=value¶m=value
3408 * /name?argument+argument+argument
3409 * /name?param=value¶m=value
3411 * If the string contains an "=" character after the initial name,
3412 * then we treat it as a HTTP GET form request and make a copy of
3413 * the remaining string for the environment variable.
3415 * The string is always parsed out as command-line arguments, to
3416 * be consistent with Apache...
3419 cupsdLogClient(con
, CUPSD_LOG_DEBUG2
,
3420 "pipe_command infile=%d, outfile=%p, "
3421 "command=\"%s\", options=\"%s\", root=%d",
3422 infile
, outfile
, command
,
3423 options
? options
: "(null)", root
);
3430 if (*commptr
== ' ')
3432 strlcpy(argbuf
, commptr
, sizeof(argbuf
));
3437 if (argbuf
[0] == '/')
3440 * Found some trailing path information, set PATH_INFO...
3443 if ((commptr
= strchr(argbuf
, '?')) == NULL
)
3444 commptr
= argbuf
+ strlen(argbuf
);
3448 snprintf(path_info
, sizeof(path_info
), "PATH_INFO=%s", argbuf
);
3454 path_info
[0] = '\0';
3456 if (*commptr
== ' ')
3460 if (*commptr
== '?' && con
->operation
== HTTP_STATE_GET
&& !con
->query_string
)
3463 cupsdSetStringf(&(con
->query_string
), "QUERY_STRING=%s", commptr
);
3470 argv
[argc
++] = commptr
;
3472 for (; *commptr
&& argc
< 99; commptr
++)
3475 * Break arguments whenever we see a + or space...
3478 if (*commptr
== ' ' || *commptr
== '+')
3480 while (*commptr
== ' ' || *commptr
== '+')
3484 * If we don't have a blank string, save it as another argument...
3489 argv
[argc
] = commptr
;
3495 else if (*commptr
== '%' && isxdigit(commptr
[1] & 255) &&
3496 isxdigit(commptr
[2] & 255))
3499 * Convert the %xx notation to the individual character.
3502 if (commptr
[1] >= '0' && commptr
[1] <= '9')
3503 *commptr
= (commptr
[1] - '0') << 4;
3505 *commptr
= (tolower(commptr
[1]) - 'a' + 10) << 4;
3507 if (commptr
[2] >= '0' && commptr
[2] <= '9')
3508 *commptr
|= commptr
[2] - '0';
3510 *commptr
|= tolower(commptr
[2]) - 'a' + 10;
3512 _cups_strcpy(commptr
+ 1, commptr
+ 3);
3515 * Check for a %00 and break if that is the case...
3527 * Setup the environment variables as needed...
3530 if (con
->username
[0])
3532 snprintf(auth_type
, sizeof(auth_type
), "AUTH_TYPE=%s",
3533 httpGetField(con
->http
, HTTP_FIELD_AUTHORIZATION
));
3535 if ((uriptr
= strchr(auth_type
+ 10, ' ')) != NULL
)
3539 auth_type
[0] = '\0';
3542 (attr
= ippFindAttribute(con
->request
, "attributes-natural-language",
3543 IPP_TAG_LANGUAGE
)) != NULL
)
3545 switch (strlen(attr
->values
[0].string
.text
))
3549 * This is an unknown or badly formatted language code; use
3550 * the POSIX locale...
3553 strlcpy(lang
, "LANG=C", sizeof(lang
));
3558 * Just the language code (ll)...
3561 snprintf(lang
, sizeof(lang
), "LANG=%s.UTF8",
3562 attr
->values
[0].string
.text
);
3567 * Language and country code (ll-cc)...
3570 snprintf(lang
, sizeof(lang
), "LANG=%c%c_%c%c.UTF8",
3571 attr
->values
[0].string
.text
[0],
3572 attr
->values
[0].string
.text
[1],
3573 toupper(attr
->values
[0].string
.text
[3] & 255),
3574 toupper(attr
->values
[0].string
.text
[4] & 255));
3578 else if (con
->language
)
3579 snprintf(lang
, sizeof(lang
), "LANG=%s.UTF8", con
->language
->language
);
3581 strlcpy(lang
, "LANG=C", sizeof(lang
));
3583 strlcpy(remote_addr
, "REMOTE_ADDR=", sizeof(remote_addr
));
3584 httpAddrString(httpGetAddress(con
->http
), remote_addr
+ 12,
3585 sizeof(remote_addr
) - 12);
3587 snprintf(remote_host
, sizeof(remote_host
), "REMOTE_HOST=%s",
3588 httpGetHostname(con
->http
, NULL
, 0));
3590 snprintf(script_name
, sizeof(script_name
), "SCRIPT_NAME=%s", con
->uri
);
3591 if ((uriptr
= strchr(script_name
, '?')) != NULL
)
3594 snprintf(script_filename
, sizeof(script_filename
), "SCRIPT_FILENAME=%s%s",
3595 DocumentRoot
, script_name
+ 12);
3597 sprintf(server_port
, "SERVER_PORT=%d", con
->serverport
);
3599 if (httpGetField(con
->http
, HTTP_FIELD_HOST
)[0])
3601 char *nameptr
; /* Pointer to ":port" */
3603 snprintf(server_name
, sizeof(server_name
), "SERVER_NAME=%s",
3604 httpGetField(con
->http
, HTTP_FIELD_HOST
));
3605 if ((nameptr
= strrchr(server_name
, ':')) != NULL
&& !strchr(nameptr
, ']'))
3606 *nameptr
= '\0'; /* Strip trailing ":port" */
3609 snprintf(server_name
, sizeof(server_name
), "SERVER_NAME=%s",
3612 envc
= cupsdLoadEnv(envp
, (int)(sizeof(envp
) / sizeof(envp
[0])));
3615 envp
[envc
++] = auth_type
;
3617 envp
[envc
++] = lang
;
3618 envp
[envc
++] = "REDIRECT_STATUS=1";
3619 envp
[envc
++] = "GATEWAY_INTERFACE=CGI/1.1";
3620 envp
[envc
++] = server_name
;
3621 envp
[envc
++] = server_port
;
3622 envp
[envc
++] = remote_addr
;
3623 envp
[envc
++] = remote_host
;
3624 envp
[envc
++] = script_name
;
3625 envp
[envc
++] = script_filename
;
3628 envp
[envc
++] = path_info
;
3630 if (con
->username
[0])
3632 snprintf(remote_user
, sizeof(remote_user
), "REMOTE_USER=%s", con
->username
);
3634 envp
[envc
++] = remote_user
;
3637 if (httpGetVersion(con
->http
) == HTTP_VERSION_1_1
)
3638 envp
[envc
++] = "SERVER_PROTOCOL=HTTP/1.1";
3639 else if (httpGetVersion(con
->http
) == HTTP_VERSION_1_0
)
3640 envp
[envc
++] = "SERVER_PROTOCOL=HTTP/1.0";
3642 envp
[envc
++] = "SERVER_PROTOCOL=HTTP/0.9";
3644 if (httpGetCookie(con
->http
))
3646 snprintf(http_cookie
, sizeof(http_cookie
), "HTTP_COOKIE=%s",
3647 httpGetCookie(con
->http
));
3648 envp
[envc
++] = http_cookie
;
3651 if (httpGetField(con
->http
, HTTP_FIELD_USER_AGENT
)[0])
3653 snprintf(http_user_agent
, sizeof(http_user_agent
), "HTTP_USER_AGENT=%s",
3654 httpGetField(con
->http
, HTTP_FIELD_USER_AGENT
));
3655 envp
[envc
++] = http_user_agent
;
3658 if (httpGetField(con
->http
, HTTP_FIELD_REFERER
)[0])
3660 snprintf(http_referer
, sizeof(http_referer
), "HTTP_REFERER=%s",
3661 httpGetField(con
->http
, HTTP_FIELD_REFERER
));
3662 envp
[envc
++] = http_referer
;
3665 if (con
->operation
== HTTP_STATE_GET
)
3667 envp
[envc
++] = "REQUEST_METHOD=GET";
3669 if (con
->query_string
)
3672 * Add GET form variables after ?...
3675 envp
[envc
++] = con
->query_string
;
3678 envp
[envc
++] = "QUERY_STRING=";
3682 sprintf(content_length
, "CONTENT_LENGTH=" CUPS_LLFMT
,
3683 CUPS_LLCAST con
->bytes
);
3684 snprintf(content_type
, sizeof(content_type
), "CONTENT_TYPE=%s",
3685 httpGetField(con
->http
, HTTP_FIELD_CONTENT_TYPE
));
3687 envp
[envc
++] = "REQUEST_METHOD=POST";
3688 envp
[envc
++] = content_length
;
3689 envp
[envc
++] = content_type
;
3693 * Tell the CGI if we are using encryption...
3696 if (httpIsEncrypted(con
->http
))
3697 envp
[envc
++] = "HTTPS=ON";
3700 * Terminate the environment array...
3705 if (LogLevel
>= CUPSD_LOG_DEBUG
)
3707 for (i
= 0; i
< argc
; i
++)
3708 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3709 "[CGI] argv[%d] = \"%s\"", i
, argv
[i
]);
3710 for (i
= 0; i
< envc
; i
++)
3711 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3712 "[CGI] envp[%d] = \"%s\"", i
, envp
[i
]);
3716 * Create a pipe for the output...
3719 if (cupsdOpenPipe(fds
))
3721 cupsdLogMessage(CUPSD_LOG_ERROR
, "[CGI] Unable to create pipe for %s - %s",
3722 argv
[0], strerror(errno
));
3727 * Then execute the command...
3730 if (cupsdStartProcess(command
, argv
, envp
, infile
, fds
[1], CGIPipes
[1],
3731 -1, -1, root
, DefaultProfile
, NULL
, &pid
) < 0)
3734 * Error - can't fork!
3737 cupsdLogMessage(CUPSD_LOG_ERROR
, "[CGI] Unable to start %s - %s", argv
[0],
3740 cupsdClosePipe(fds
);
3746 * Fork successful - return the PID...
3749 if (con
->username
[0])
3750 cupsdAddCert(pid
, con
->username
, con
->type
);
3752 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[CGI] Started %s (PID %d)", command
, pid
);
3763 * 'valid_host()' - Is the Host: field valid?
3766 static int /* O - 1 if valid, 0 if not */
3767 valid_host(cupsd_client_t
*con
) /* I - Client connection */
3769 cupsd_alias_t
*a
; /* Current alias */
3770 cupsd_netif_t
*netif
; /* Current network interface */
3771 const char *end
; /* End character */
3772 char *ptr
; /* Pointer into host value */
3776 * Copy the Host: header for later use...
3779 strlcpy(con
->clientname
, httpGetField(con
->http
, HTTP_FIELD_HOST
),
3780 sizeof(con
->clientname
));
3781 if ((ptr
= strrchr(con
->clientname
, ':')) != NULL
&& !strchr(ptr
, ']'))
3784 con
->clientport
= atoi(ptr
);
3787 con
->clientport
= con
->serverport
;
3793 if (httpAddrLocalhost(httpGetAddress(con
->http
)))
3796 * Only allow "localhost" or the equivalent IPv4 or IPv6 numerical
3797 * addresses when accessing CUPS via the loopback interface...
3800 return (!_cups_strcasecmp(con
->clientname
, "localhost") ||
3801 !_cups_strcasecmp(con
->clientname
, "localhost.") ||
3803 !_cups_strcasecmp(con
->clientname
, "localhost.localdomain") ||
3804 #endif /* __linux */
3805 !strcmp(con
->clientname
, "127.0.0.1") ||
3806 !strcmp(con
->clientname
, "[::1]"));
3809 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
3811 * Check if the hostname is something.local (Bonjour); if so, allow it.
3814 if ((end
= strrchr(con
->clientname
, '.')) != NULL
&& end
> con
->clientname
&&
3818 * "." on end, work back to second-to-last "."...
3821 for (end
--; end
> con
->clientname
&& *end
!= '.'; end
--);
3824 if (end
&& (!_cups_strcasecmp(end
, ".local") ||
3825 !_cups_strcasecmp(end
, ".local.")))
3827 #endif /* HAVE_DNSSD || HAVE_AVAHI */
3830 * Check if the hostname is an IP address...
3833 if (isdigit(con
->clientname
[0] & 255) || con
->clientname
[0] == '[')
3836 * Possible IPv4/IPv6 address...
3839 http_addrlist_t
*addrlist
; /* List of addresses */
3842 if ((addrlist
= httpAddrGetList(con
->clientname
, AF_UNSPEC
, NULL
)) != NULL
)
3845 * Good IPv4/IPv6 address...
3848 httpAddrFreeList(addrlist
);
3854 * Check for (alias) name matches...
3857 for (a
= (cupsd_alias_t
*)cupsArrayFirst(ServerAlias
);
3859 a
= (cupsd_alias_t
*)cupsArrayNext(ServerAlias
))
3862 * "ServerAlias *" allows all host values through...
3865 if (!strcmp(a
->name
, "*"))
3868 if (!_cups_strncasecmp(con
->clientname
, a
->name
, a
->namelen
))
3871 * Prefix matches; check the character at the end - it must be "." or nul.
3874 end
= con
->clientname
+ a
->namelen
;
3876 if (!*end
|| (*end
== '.' && !end
[1]))
3881 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
3882 for (a
= (cupsd_alias_t
*)cupsArrayFirst(DNSSDAlias
);
3884 a
= (cupsd_alias_t
*)cupsArrayNext(DNSSDAlias
))
3887 * "ServerAlias *" allows all host values through...
3890 if (!strcmp(a
->name
, "*"))
3893 if (!_cups_strncasecmp(con
->clientname
, a
->name
, a
->namelen
))
3896 * Prefix matches; check the character at the end - it must be "." or nul.
3899 end
= con
->clientname
+ a
->namelen
;
3901 if (!*end
|| (*end
== '.' && !end
[1]))
3905 #endif /* HAVE_DNSSD || HAVE_AVAHI */
3908 * Check for interface hostname matches...
3911 for (netif
= (cupsd_netif_t
*)cupsArrayFirst(NetIFList
);
3913 netif
= (cupsd_netif_t
*)cupsArrayNext(NetIFList
))
3915 if (!_cups_strncasecmp(con
->clientname
, netif
->hostname
, netif
->hostlen
))
3918 * Prefix matches; check the character at the end - it must be "." or nul.
3921 end
= con
->clientname
+ netif
->hostlen
;
3923 if (!*end
|| (*end
== '.' && !end
[1]))
3933 * 'write_file()' - Send a file via HTTP.
3936 static int /* O - 0 on failure, 1 on success */
3937 write_file(cupsd_client_t
*con
, /* I - Client connection */
3938 http_status_t code
, /* I - HTTP status */
3939 char *filename
, /* I - Filename */
3940 char *type
, /* I - File type */
3941 struct stat
*filestats
) /* O - File information */
3943 con
->file
= open(filename
, O_RDONLY
);
3945 cupsdLogClient(con
, CUPSD_LOG_DEBUG2
,
3946 "write_file code=%d, filename=\"%s\" (%d), "
3947 "type=\"%s\", filestats=%p",
3948 code
, filename
, con
->file
, type
? type
: "(null)", filestats
);
3953 fcntl(con
->file
, F_SETFD
, fcntl(con
->file
, F_GETFD
) | FD_CLOEXEC
);
3956 con
->sent_header
= 1;
3958 httpClearFields(con
->http
);
3960 httpSetLength(con
->http
, filestats
->st_size
);
3962 httpSetField(con
->http
, HTTP_FIELD_LAST_MODIFIED
,
3963 httpGetDateString(filestats
->st_mtime
));
3965 if (!cupsdSendHeader(con
, code
, type
, CUPSD_AUTH_NONE
))
3968 cupsdAddSelect(httpGetFd(con
->http
), NULL
, (cupsd_selfunc_t
)cupsdWriteClient
, con
);
3970 cupsdLogClient(con
, CUPSD_LOG_DEBUG
, "Sending file.");
3977 * 'write_pipe()' - Flag that data is available on the CGI pipe.
3981 write_pipe(cupsd_client_t
*con
) /* I - Client connection */
3983 cupsdLogClient(con
, CUPSD_LOG_DEBUG2
, "write_pipe CGI output on fd %d",
3986 con
->file_ready
= 1;
3988 cupsdRemoveSelect(con
->file
);
3989 cupsdAddSelect(httpGetFd(con
->http
), NULL
, (cupsd_selfunc_t
)cupsdWriteClient
, con
);
3991 cupsdLogClient(con
, CUPSD_LOG_DEBUG
, "CGI data ready to be sent.");