]> git.ipfire.org Git - thirdparty/cups.git/blob - scheduler/client.c
Make sure we send a 0-length chunk for CGI output, even for things like "Status".
[thirdparty/cups.git] / scheduler / client.c
1 /*
2 * "$Id$"
3 *
4 * Client routines for the CUPS scheduler.
5 *
6 * Copyright 2007-2014 by Apple Inc.
7 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
8 *
9 * This file contains Kerberos support code, copyright 2006 by
10 * Jelmer Vernooij.
11 *
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/".
17 */
18
19 /*
20 * Include necessary headers...
21 */
22
23 #define _CUPS_NO_DEPRECATED
24 #define _HTTP_NO_PRIVATE
25 #include "cupsd.h"
26
27 #ifdef __APPLE__
28 # include <libproc.h>
29 #endif /* __APPLE__ */
30 #ifdef HAVE_TCPD_H
31 # include <tcpd.h>
32 #endif /* HAVE_TCPD_H */
33
34
35 /*
36 * Local functions...
37 */
38
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,
42 void *data);
43 #ifdef HAVE_SSL
44 static int cupsd_start_tls(cupsd_client_t *con, http_encryption_t e);
45 #endif /* HAVE_SSL */
46 static char *get_file(cupsd_client_t *con, struct stat *filestats,
47 char *filename, size_t 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);
59
60
61 /*
62 * 'cupsdAcceptClient()' - Accept a new client.
63 */
64
65 void
66 cupsdAcceptClient(cupsd_listener_t *lis)/* I - Listener socket */
67 {
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 */
76 #ifdef HAVE_TCPD_H
77 struct request_info wrap_req; /* TCP wrappers request information */
78 #endif /* HAVE_TCPD_H */
79
80
81 cupsdLogMessage(CUPSD_LOG_DEBUG2,
82 "cupsdAcceptClient(lis=%p(%d)) Clients=%d",
83 lis, lis->fd, cupsArrayCount(Clients));
84
85 /*
86 * Make sure we don't have a full set of clients already...
87 */
88
89 if (cupsArrayCount(Clients) == MaxClients)
90 return;
91
92 /*
93 * Get a pointer to the next available client...
94 */
95
96 if (!Clients)
97 Clients = cupsArrayNew(NULL, NULL);
98
99 if (!Clients)
100 {
101 cupsdLogMessage(CUPSD_LOG_ERROR,
102 "Unable to allocate memory for clients array!");
103 cupsdPauseListening();
104 return;
105 }
106
107 if (!ActiveClients)
108 ActiveClients = cupsArrayNew((cups_array_func_t)compare_clients, NULL);
109
110 if (!ActiveClients)
111 {
112 cupsdLogMessage(CUPSD_LOG_ERROR,
113 "Unable to allocate memory for active clients array!");
114 cupsdPauseListening();
115 return;
116 }
117
118 if ((con = calloc(1, sizeof(cupsd_client_t))) == NULL)
119 {
120 cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to allocate memory for client!");
121 cupsdPauseListening();
122 return;
123 }
124
125 /*
126 * Accept the client and get the remote address...
127 */
128
129 con->number = ++ LastClientNumber;
130 con->file = -1;
131
132 if ((con->http = httpAcceptConnection(lis->fd, 0)) == NULL)
133 {
134 if (errno == ENFILE || errno == EMFILE)
135 cupsdPauseListening();
136
137 cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to accept client connection - %s.",
138 strerror(errno));
139 free(con);
140
141 return;
142 }
143
144 /*
145 * Save the connected address and port number...
146 */
147
148 con->clientaddr = lis->address;
149
150 /*
151 * Check the number of clients on the same address...
152 */
153
154 for (count = 0, tempcon = (cupsd_client_t *)cupsArrayFirst(Clients);
155 tempcon;
156 tempcon = (cupsd_client_t *)cupsArrayNext(Clients))
157 if (httpAddrEqual(httpGetAddress(tempcon->http), httpGetAddress(con->http)))
158 {
159 count ++;
160 if (count >= MaxClientsPerHost)
161 break;
162 }
163
164 if (count >= MaxClientsPerHost)
165 {
166 if ((time(NULL) - last_dos) >= 60)
167 {
168 last_dos = time(NULL);
169 cupsdLogMessage(CUPSD_LOG_WARN,
170 "Possible DoS attack - more than %d clients connecting "
171 "from %s.",
172 MaxClientsPerHost,
173 httpGetHostname(con->http, name, sizeof(name)));
174 }
175
176 httpClose(con->http);
177 free(con);
178 return;
179 }
180
181 /*
182 * Get the hostname or format the IP address as needed...
183 */
184
185 if (HostNameLookups)
186 hostname = httpResolveHostname(con->http, NULL, 0);
187 else
188 hostname = httpGetHostname(con->http, NULL, 0);
189
190 if (hostname == NULL && HostNameLookups == 2)
191 {
192 /*
193 * Can't have an unresolved IP address with double-lookups enabled...
194 */
195
196 httpClose(con->http);
197
198 cupsdLogClient(con, CUPSD_LOG_WARN,
199 "Name lookup failed - connection from %s closed!",
200 httpGetHostname(con->http, NULL, 0));
201
202 free(con);
203 return;
204 }
205
206 if (HostNameLookups == 2)
207 {
208 /*
209 * Do double lookups as needed...
210 */
211
212 http_addrlist_t *addrlist, /* List of addresses */
213 *addr; /* Current address */
214
215 if ((addrlist = httpAddrGetList(hostname, AF_UNSPEC, NULL)) != NULL)
216 {
217 /*
218 * See if the hostname maps to the same IP address...
219 */
220
221 for (addr = addrlist; addr; addr = addr->next)
222 if (httpAddrEqual(httpGetAddress(con->http), &(addr->addr)))
223 break;
224 }
225 else
226 addr = NULL;
227
228 httpAddrFreeList(addrlist);
229
230 if (!addr)
231 {
232 /*
233 * Can't have a hostname that doesn't resolve to the same IP address
234 * with double-lookups enabled...
235 */
236
237 httpClose(con->http);
238
239 cupsdLogClient(con, CUPSD_LOG_WARN,
240 "IP lookup failed - connection from %s closed!",
241 httpGetHostname(con->http, NULL, 0));
242 free(con);
243 return;
244 }
245 }
246
247 #ifdef HAVE_TCPD_H
248 /*
249 * See if the connection is denied by TCP wrappers...
250 */
251
252 request_init(&wrap_req, RQ_DAEMON, "cupsd", RQ_FILE, httpGetFd(con->http),
253 NULL);
254 fromhost(&wrap_req);
255
256 if (!hosts_access(&wrap_req))
257 {
258 httpClose(con->http);
259
260 cupsdLogClient(con, CUPSD_LOG_WARN,
261 "Connection from %s refused by /etc/hosts.allow and "
262 "/etc/hosts.deny rules.", httpGetHostname(con->http, NULL, 0));
263 free(con);
264 return;
265 }
266 #endif /* HAVE_TCPD_H */
267
268 #ifdef AF_LOCAL
269 if (httpAddrFamily(httpGetAddress(con->http)) == AF_LOCAL)
270 {
271 # ifdef __APPLE__
272 socklen_t peersize; /* Size of peer credentials */
273 pid_t peerpid; /* Peer process ID */
274 char peername[256]; /* Name of process */
275
276 peersize = sizeof(peerpid);
277 if (!getsockopt(httpGetFd(con->http), SOL_LOCAL, LOCAL_PEERPID, &peerpid,
278 &peersize))
279 {
280 if (!proc_name((int)peerpid, peername, sizeof(peername)))
281 cupsdLogClient(con, CUPSD_LOG_DEBUG,
282 "Accepted from %s (Domain ???[%d])",
283 httpGetHostname(con->http, NULL, 0), (int)peerpid);
284 else
285 cupsdLogClient(con, CUPSD_LOG_DEBUG,
286 "Accepted from %s (Domain %s[%d])",
287 httpGetHostname(con->http, NULL, 0), peername, (int)peerpid);
288 }
289 else
290 # endif /* __APPLE__ */
291
292 cupsdLogClient(con, CUPSD_LOG_DEBUG, "Accepted from %s (Domain)",
293 httpGetHostname(con->http, NULL, 0));
294 }
295 else
296 #endif /* AF_LOCAL */
297 cupsdLogClient(con, CUPSD_LOG_DEBUG, "Accepted from %s:%d (IPv%d)",
298 httpGetHostname(con->http, NULL, 0),
299 httpAddrPort(httpGetAddress(con->http)),
300 httpAddrFamily(httpGetAddress(con->http)) == AF_INET ? 4 : 6);
301
302 /*
303 * Get the local address the client connected to...
304 */
305
306 addrlen = sizeof(temp);
307 if (getsockname(httpGetFd(con->http), (struct sockaddr *)&temp, &addrlen))
308 {
309 cupsdLogClient(con, CUPSD_LOG_ERROR, "Unable to get local address - %s",
310 strerror(errno));
311
312 strlcpy(con->servername, "localhost", sizeof(con->servername));
313 con->serverport = LocalPort;
314 }
315 #ifdef AF_LOCAL
316 else if (httpAddrFamily(&temp) == AF_LOCAL)
317 {
318 strlcpy(con->servername, "localhost", sizeof(con->servername));
319 con->serverport = LocalPort;
320 }
321 #endif /* AF_LOCAL */
322 else
323 {
324 if (httpAddrLocalhost(&temp))
325 strlcpy(con->servername, "localhost", sizeof(con->servername));
326 else if (HostNameLookups)
327 httpAddrLookup(&temp, con->servername, sizeof(con->servername));
328 else
329 httpAddrString(&temp, con->servername, sizeof(con->servername));
330
331 con->serverport = httpAddrPort(&(lis->address));
332 }
333
334 /*
335 * Add the connection to the array of active clients...
336 */
337
338 cupsArrayAdd(Clients, con);
339
340 /*
341 * Add the socket to the server select.
342 */
343
344 cupsdAddSelect(httpGetFd(con->http), (cupsd_selfunc_t)cupsdReadClient, NULL,
345 con);
346
347 cupsdLogClient(con, CUPSD_LOG_DEBUG, "Waiting for request.");
348
349 /*
350 * Temporarily suspend accept()'s until we lose a client...
351 */
352
353 if (cupsArrayCount(Clients) == MaxClients)
354 cupsdPauseListening();
355
356 #ifdef HAVE_SSL
357 /*
358 * See if we are connecting on a secure port...
359 */
360
361 if (lis->encryption == HTTP_ENCRYPTION_ALWAYS)
362 {
363 /*
364 * https connection; go secure...
365 */
366
367 if (cupsd_start_tls(con, HTTP_ENCRYPTION_ALWAYS))
368 cupsdCloseClient(con);
369 }
370 else
371 con->auto_ssl = 1;
372 #endif /* HAVE_SSL */
373 }
374
375
376 /*
377 * 'cupsdCloseAllClients()' - Close all remote clients immediately.
378 */
379
380 void
381 cupsdCloseAllClients(void)
382 {
383 cupsd_client_t *con; /* Current client */
384
385
386 cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdCloseAllClients() Clients=%d",
387 cupsArrayCount(Clients));
388
389 for (con = (cupsd_client_t *)cupsArrayFirst(Clients);
390 con;
391 con = (cupsd_client_t *)cupsArrayNext(Clients))
392 if (cupsdCloseClient(con))
393 cupsdCloseClient(con);
394 }
395
396
397 /*
398 * 'cupsdCloseClient()' - Close a remote client.
399 */
400
401 int /* O - 1 if partial close, 0 if fully closed */
402 cupsdCloseClient(cupsd_client_t *con) /* I - Client to close */
403 {
404 int partial; /* Do partial close for SSL? */
405
406
407 cupsdLogClient(con, CUPSD_LOG_DEBUG, "Closing connection.");
408
409 /*
410 * Flush pending writes before closing...
411 */
412
413 httpFlushWrite(con->http);
414
415 partial = 0;
416
417 if (con->pipe_pid != 0)
418 {
419 /*
420 * Stop any CGI process...
421 */
422
423 cupsdEndProcess(con->pipe_pid, 1);
424 con->pipe_pid = 0;
425 }
426
427 if (con->file >= 0)
428 {
429 cupsdRemoveSelect(con->file);
430
431 close(con->file);
432 con->file = -1;
433 }
434
435 /*
436 * Close the socket and clear the file from the input set for select()...
437 */
438
439 if (httpGetFd(con->http) >= 0)
440 {
441 cupsArrayRemove(ActiveClients, con);
442 cupsdSetBusyState();
443
444 #ifdef HAVE_SSL
445 /*
446 * Shutdown encryption as needed...
447 */
448
449 if (httpIsEncrypted(con->http))
450 partial = 1;
451 #endif /* HAVE_SSL */
452
453 if (partial)
454 {
455 /*
456 * Only do a partial close so that the encrypted client gets everything.
457 */
458
459 httpShutdown(con->http);
460 cupsdAddSelect(httpGetFd(con->http), (cupsd_selfunc_t)cupsdReadClient,
461 NULL, con);
462
463 cupsdLogClient(con, CUPSD_LOG_DEBUG, "Waiting for socket close.");
464 }
465 else
466 {
467 /*
468 * Shut the socket down fully...
469 */
470
471 cupsdRemoveSelect(httpGetFd(con->http));
472 httpClose(con->http);
473 con->http = NULL;
474 }
475 }
476
477 if (!partial)
478 {
479 /*
480 * Free memory...
481 */
482
483 cupsdRemoveSelect(httpGetFd(con->http));
484
485 httpClose(con->http);
486
487 cupsdClearString(&con->filename);
488 cupsdClearString(&con->command);
489 cupsdClearString(&con->options);
490 cupsdClearString(&con->query_string);
491
492 if (con->request)
493 {
494 ippDelete(con->request);
495 con->request = NULL;
496 }
497
498 if (con->response)
499 {
500 ippDelete(con->response);
501 con->response = NULL;
502 }
503
504 if (con->language)
505 {
506 cupsLangFree(con->language);
507 con->language = NULL;
508 }
509
510 #ifdef HAVE_AUTHORIZATION_H
511 if (con->authref)
512 {
513 AuthorizationFree(con->authref, kAuthorizationFlagDefaults);
514 con->authref = NULL;
515 }
516 #endif /* HAVE_AUTHORIZATION_H */
517
518 /*
519 * Re-enable new client connections if we are going back under the
520 * limit...
521 */
522
523 if (cupsArrayCount(Clients) == MaxClients)
524 cupsdResumeListening();
525
526 /*
527 * Compact the list of clients as necessary...
528 */
529
530 cupsArrayRemove(Clients, con);
531
532 free(con);
533 }
534
535 return (partial);
536 }
537
538
539 /*
540 * 'cupsdReadClient()' - Read data from a client.
541 */
542
543 void
544 cupsdReadClient(cupsd_client_t *con) /* I - Client to read from */
545 {
546 char line[32768], /* Line from client... */
547 locale[64], /* Locale */
548 *ptr; /* Pointer into strings */
549 http_status_t status; /* Transfer status */
550 ipp_state_t ipp_state; /* State of IPP transfer */
551 int bytes; /* Number of bytes to POST */
552 char *filename; /* Name of file for GET/HEAD */
553 char buf[1024]; /* Buffer for real filename */
554 struct stat filestats; /* File information */
555 mime_type_t *type; /* MIME type of file */
556 cupsd_printer_t *p; /* Printer */
557 static unsigned request_id = 0; /* Request ID for temp files */
558
559
560 status = HTTP_STATUS_CONTINUE;
561
562 cupsdLogClient(con, CUPSD_LOG_DEBUG2,
563 "cupsdReadClient "
564 "error=%d, "
565 "used=%d, "
566 "state=%s, "
567 "data_encoding=HTTP_ENCODING_%s, "
568 "data_remaining=" CUPS_LLFMT ", "
569 "request=%p(%s), "
570 "file=%d",
571 httpError(con->http), (int)httpGetReady(con->http),
572 httpStateString(httpGetState(con->http)),
573 httpIsChunked(con->http) ? "CHUNKED" : "LENGTH",
574 CUPS_LLCAST httpGetRemaining(con->http),
575 con->request,
576 con->request ? ippStateString(ippGetState(con->request)) : "",
577 con->file);
578
579 if (httpGetState(con->http) == HTTP_STATE_GET_SEND ||
580 httpGetState(con->http) == HTTP_STATE_POST_SEND ||
581 httpGetState(con->http) == HTTP_STATE_STATUS)
582 {
583 /*
584 * If we get called in the wrong state, then something went wrong with the
585 * connection and we need to shut it down...
586 */
587
588 cupsdLogClient(con, CUPSD_LOG_DEBUG, "Closing on unexpected HTTP read state %s.",
589 httpStateString(httpGetState(con->http)));
590 cupsdCloseClient(con);
591 return;
592 }
593
594 #ifdef HAVE_SSL
595 if (con->auto_ssl)
596 {
597 /*
598 * Automatically check for a SSL/TLS handshake...
599 */
600
601 con->auto_ssl = 0;
602
603 if (recv(httpGetFd(con->http), buf, 1, MSG_PEEK) == 1 &&
604 (!buf[0] || !strchr("DGHOPT", buf[0])))
605 {
606 /*
607 * Encrypt this connection...
608 */
609
610 cupsdLogClient(con, CUPSD_LOG_DEBUG2,
611 "Saw first byte %02X, auto-negotiating "
612 "SSL/TLS session.", buf[0] & 255);
613
614 if (cupsd_start_tls(con, HTTP_ENCRYPTION_ALWAYS))
615 cupsdCloseClient(con);
616
617 return;
618 }
619 }
620 #endif /* HAVE_SSL */
621
622 switch (httpGetState(con->http))
623 {
624 case HTTP_STATE_WAITING :
625 /*
626 * See if we've received a request line...
627 */
628
629 con->operation = httpReadRequest(con->http, con->uri, sizeof(con->uri));
630 if (con->operation == HTTP_STATE_ERROR ||
631 con->operation == HTTP_STATE_UNKNOWN_METHOD ||
632 con->operation == HTTP_STATE_UNKNOWN_VERSION)
633 {
634 if (httpError(con->http))
635 cupsdLogClient(con, CUPSD_LOG_DEBUG,
636 "HTTP_STATE_WAITING Closing for error %d (%s)",
637 httpError(con->http), strerror(httpError(con->http)));
638 else
639 cupsdLogClient(con, CUPSD_LOG_DEBUG,
640 "HTTP_STATE_WAITING Closing on error: %s",
641 cupsLastErrorString());
642
643 cupsdCloseClient(con);
644 return;
645 }
646
647 /*
648 * Ignore blank request lines...
649 */
650
651 if (con->operation == HTTP_STATE_WAITING)
652 break;
653
654 /*
655 * Clear other state variables...
656 */
657
658 con->bytes = 0;
659 con->file = -1;
660 con->file_ready = 0;
661 con->pipe_pid = 0;
662 con->username[0] = '\0';
663 con->password[0] = '\0';
664
665 cupsdClearString(&con->command);
666 cupsdClearString(&con->options);
667 cupsdClearString(&con->query_string);
668
669 if (con->request)
670 {
671 ippDelete(con->request);
672 con->request = NULL;
673 }
674
675 if (con->response)
676 {
677 ippDelete(con->response);
678 con->response = NULL;
679 }
680
681 if (con->language)
682 {
683 cupsLangFree(con->language);
684 con->language = NULL;
685 }
686
687 #ifdef HAVE_GSSAPI
688 con->have_gss = 0;
689 con->gss_uid = 0;
690 #endif /* HAVE_GSSAPI */
691
692 /*
693 * Handle full URLs in the request line...
694 */
695
696 if (strcmp(con->uri, "*"))
697 {
698 char scheme[HTTP_MAX_URI], /* Method/scheme */
699 userpass[HTTP_MAX_URI], /* Username:password */
700 hostname[HTTP_MAX_URI], /* Hostname */
701 resource[HTTP_MAX_URI]; /* Resource path */
702 int port; /* Port number */
703
704 /*
705 * Separate the URI into its components...
706 */
707
708 if (httpSeparateURI(HTTP_URI_CODING_MOST, con->uri,
709 scheme, sizeof(scheme),
710 userpass, sizeof(userpass),
711 hostname, sizeof(hostname), &port,
712 resource, sizeof(resource)) < HTTP_URI_STATUS_OK)
713 {
714 cupsdLogClient(con, CUPSD_LOG_ERROR, "Bad URI \"%s\" in request.",
715 con->uri);
716 cupsdSendError(con, HTTP_STATUS_METHOD_NOT_ALLOWED, CUPSD_AUTH_NONE);
717 cupsdCloseClient(con);
718 return;
719 }
720
721 /*
722 * Only allow URIs with the servername, localhost, or an IP
723 * address...
724 */
725
726 if (strcmp(scheme, "file") &&
727 _cups_strcasecmp(hostname, ServerName) &&
728 _cups_strcasecmp(hostname, "localhost") &&
729 !cupsArrayFind(ServerAlias, hostname) &&
730 !isdigit(hostname[0]) && hostname[0] != '[')
731 {
732 /*
733 * Nope, we don't do proxies...
734 */
735
736 cupsdLogClient(con, CUPSD_LOG_ERROR, "Bad URI \"%s\" in request.",
737 con->uri);
738 cupsdSendError(con, HTTP_STATUS_METHOD_NOT_ALLOWED, CUPSD_AUTH_NONE);
739 cupsdCloseClient(con);
740 return;
741 }
742
743 /*
744 * Copy the resource portion back into the URI; both resource and
745 * con->uri are HTTP_MAX_URI bytes in size...
746 */
747
748 strlcpy(con->uri, resource, sizeof(con->uri));
749 }
750
751 /*
752 * Process the request...
753 */
754
755 gettimeofday(&(con->start), NULL);
756
757 cupsdLogClient(con, CUPSD_LOG_DEBUG, "%s %s HTTP/%d.%d",
758 httpStateString(con->operation) + 11, con->uri,
759 httpGetVersion(con->http) / 100,
760 httpGetVersion(con->http) % 100);
761
762 if (!cupsArrayFind(ActiveClients, con))
763 {
764 cupsArrayAdd(ActiveClients, con);
765 cupsdSetBusyState();
766 }
767
768 case HTTP_STATE_OPTIONS :
769 case HTTP_STATE_DELETE :
770 case HTTP_STATE_GET :
771 case HTTP_STATE_HEAD :
772 case HTTP_STATE_POST :
773 case HTTP_STATE_PUT :
774 case HTTP_STATE_TRACE :
775 /*
776 * Parse incoming parameters until the status changes...
777 */
778
779 while ((status = httpUpdate(con->http)) == HTTP_STATUS_CONTINUE)
780 if (!httpGetReady(con->http))
781 break;
782
783 if (status != HTTP_STATUS_OK && status != HTTP_STATUS_CONTINUE)
784 {
785 if (httpError(con->http) && httpError(con->http) != EPIPE)
786 cupsdLogClient(con, CUPSD_LOG_DEBUG,
787 "Closing for error %d (%s) while reading headers.",
788 httpError(con->http), strerror(httpError(con->http)));
789 else
790 cupsdLogClient(con, CUPSD_LOG_DEBUG,
791 "Closing on EOF while reading headers.");
792
793 cupsdSendError(con, HTTP_STATUS_BAD_REQUEST, CUPSD_AUTH_NONE);
794 cupsdCloseClient(con);
795 return;
796 }
797 break;
798
799 default :
800 if (!httpGetReady(con->http) && recv(httpGetFd(con->http), buf, 1, MSG_PEEK) < 1)
801 {
802 /*
803 * Connection closed...
804 */
805
806 cupsdLogClient(con, CUPSD_LOG_DEBUG, "Closing on EOF.");
807 cupsdCloseClient(con);
808 return;
809 }
810 break; /* Anti-compiler-warning-code */
811 }
812
813 /*
814 * Handle new transfers...
815 */
816
817 cupsdLogClient(con, CUPSD_LOG_DEBUG, "Read: status=%d", status);
818
819 if (status == HTTP_STATUS_OK)
820 {
821 if (httpGetField(con->http, HTTP_FIELD_ACCEPT_LANGUAGE)[0])
822 {
823 /*
824 * Figure out the locale from the Accept-Language and Content-Type
825 * fields...
826 */
827
828 if ((ptr = strchr(httpGetField(con->http, HTTP_FIELD_ACCEPT_LANGUAGE),
829 ',')) != NULL)
830 *ptr = '\0';
831
832 if ((ptr = strchr(httpGetField(con->http, HTTP_FIELD_ACCEPT_LANGUAGE),
833 ';')) != NULL)
834 *ptr = '\0';
835
836 if ((ptr = strstr(httpGetField(con->http, HTTP_FIELD_CONTENT_TYPE),
837 "charset=")) != NULL)
838 {
839 /*
840 * Combine language and charset, and trim any extra params in the
841 * content-type.
842 */
843
844 snprintf(locale, sizeof(locale), "%s.%s",
845 httpGetField(con->http, HTTP_FIELD_ACCEPT_LANGUAGE), ptr + 8);
846
847 if ((ptr = strchr(locale, ',')) != NULL)
848 *ptr = '\0';
849 }
850 else
851 snprintf(locale, sizeof(locale), "%s.UTF-8",
852 httpGetField(con->http, HTTP_FIELD_ACCEPT_LANGUAGE));
853
854 con->language = cupsLangGet(locale);
855 }
856 else
857 con->language = cupsLangGet(DefaultLocale);
858
859 cupsdAuthorize(con);
860
861 if (!_cups_strncasecmp(httpGetField(con->http, HTTP_FIELD_CONNECTION),
862 "Keep-Alive", 10) && KeepAlive)
863 httpSetKeepAlive(con->http, HTTP_KEEPALIVE_ON);
864 else if (!_cups_strncasecmp(httpGetField(con->http, HTTP_FIELD_CONNECTION),
865 "close", 5))
866 httpSetKeepAlive(con->http, HTTP_KEEPALIVE_OFF);
867
868 if (!httpGetField(con->http, HTTP_FIELD_HOST)[0] &&
869 httpGetVersion(con->http) >= HTTP_VERSION_1_1)
870 {
871 /*
872 * HTTP/1.1 and higher require the "Host:" field...
873 */
874
875 if (!cupsdSendError(con, HTTP_STATUS_BAD_REQUEST, CUPSD_AUTH_NONE))
876 {
877 cupsdLogClient(con, CUPSD_LOG_ERROR, "Missing Host: field in request.");
878 cupsdCloseClient(con);
879 return;
880 }
881 }
882 else if (!valid_host(con))
883 {
884 /*
885 * Access to localhost must use "localhost" or the corresponding IPv4
886 * or IPv6 values in the Host: field.
887 */
888
889 cupsdLogClient(con, CUPSD_LOG_ERROR,
890 "Request from \"%s\" using invalid Host: field \"%s\".",
891 httpGetHostname(con->http, NULL, 0), httpGetField(con->http, HTTP_FIELD_HOST));
892
893 if (!cupsdSendError(con, HTTP_STATUS_BAD_REQUEST, CUPSD_AUTH_NONE))
894 {
895 cupsdCloseClient(con);
896 return;
897 }
898 }
899 else if (con->operation == HTTP_STATE_OPTIONS)
900 {
901 /*
902 * Do OPTIONS command...
903 */
904
905 if (con->best && con->best->type != CUPSD_AUTH_NONE)
906 {
907 httpClearFields(con->http);
908
909 if (!cupsdSendHeader(con, HTTP_STATUS_UNAUTHORIZED, NULL, CUPSD_AUTH_NONE))
910 {
911 cupsdCloseClient(con);
912 return;
913 }
914 }
915
916 if (!_cups_strcasecmp(httpGetField(con->http, HTTP_FIELD_CONNECTION), "Upgrade") && strstr(httpGetField(con->http, HTTP_FIELD_UPGRADE), "TLS/") != NULL && !httpIsEncrypted(con->http))
917 {
918 #ifdef HAVE_SSL
919 /*
920 * Do encryption stuff...
921 */
922
923 httpClearFields(con->http);
924
925 if (!cupsdSendHeader(con, HTTP_STATUS_SWITCHING_PROTOCOLS, NULL, CUPSD_AUTH_NONE))
926 {
927 cupsdCloseClient(con);
928 return;
929 }
930
931 if (cupsd_start_tls(con, HTTP_ENCRYPTION_REQUIRED))
932 {
933 cupsdCloseClient(con);
934 return;
935 }
936 #else
937 if (!cupsdSendError(con, HTTP_NOT_IMPLEMENTED, CUPSD_AUTH_NONE))
938 {
939 cupsdCloseClient(con);
940 return;
941 }
942 #endif /* HAVE_SSL */
943 }
944
945 httpClearFields(con->http);
946 httpSetField(con->http, HTTP_FIELD_ALLOW,
947 "GET, HEAD, OPTIONS, POST, PUT");
948 httpSetField(con->http, HTTP_FIELD_CONTENT_LENGTH, "0");
949
950 if (!cupsdSendHeader(con, HTTP_STATUS_OK, NULL, CUPSD_AUTH_NONE))
951 {
952 cupsdCloseClient(con);
953 return;
954 }
955 }
956 else if (!is_path_absolute(con->uri))
957 {
958 /*
959 * Protect against malicious users!
960 */
961
962 cupsdLogClient(con, CUPSD_LOG_ERROR,
963 "Request for non-absolute resource \"%s\".", con->uri);
964
965 if (!cupsdSendError(con, HTTP_STATUS_FORBIDDEN, CUPSD_AUTH_NONE))
966 {
967 cupsdCloseClient(con);
968 return;
969 }
970 }
971 else
972 {
973 if (!_cups_strcasecmp(httpGetField(con->http, HTTP_FIELD_CONNECTION),
974 "Upgrade") && !httpIsEncrypted(con->http))
975 {
976 #ifdef HAVE_SSL
977 /*
978 * Do encryption stuff...
979 */
980
981 httpClearFields(con->http);
982
983 if (!cupsdSendHeader(con, HTTP_STATUS_SWITCHING_PROTOCOLS, NULL,
984 CUPSD_AUTH_NONE))
985 {
986 cupsdCloseClient(con);
987 return;
988 }
989
990 if (cupsd_start_tls(con, HTTP_ENCRYPTION_REQUIRED))
991 {
992 cupsdCloseClient(con);
993 return;
994 }
995 #else
996 if (!cupsdSendError(con, HTTP_NOT_IMPLEMENTED, CUPSD_AUTH_NONE))
997 {
998 cupsdCloseClient(con);
999 return;
1000 }
1001 #endif /* HAVE_SSL */
1002 }
1003
1004 if ((status = cupsdIsAuthorized(con, NULL)) != HTTP_STATUS_OK)
1005 {
1006 cupsdSendError(con, status, CUPSD_AUTH_NONE);
1007 cupsdCloseClient(con);
1008 return;
1009 }
1010
1011 if (httpGetExpect(con->http) &&
1012 (con->operation == HTTP_STATE_POST || con->operation == HTTP_STATE_PUT))
1013 {
1014 if (httpGetExpect(con->http) == HTTP_STATUS_CONTINUE)
1015 {
1016 /*
1017 * Send 100-continue header...
1018 */
1019
1020 if (httpWriteResponse(con->http, HTTP_STATUS_CONTINUE))
1021 {
1022 cupsdCloseClient(con);
1023 return;
1024 }
1025 }
1026 else
1027 {
1028 /*
1029 * Send 417-expectation-failed header...
1030 */
1031
1032 httpClearFields(con->http);
1033 httpSetField(con->http, HTTP_FIELD_CONTENT_LENGTH, "0");
1034
1035 cupsdSendError(con, HTTP_STATUS_EXPECTATION_FAILED, CUPSD_AUTH_NONE);
1036 cupsdCloseClient(con);
1037 return;
1038 }
1039 }
1040
1041 switch (httpGetState(con->http))
1042 {
1043 case HTTP_STATE_GET_SEND :
1044 cupsdLogClient(con, CUPSD_LOG_DEBUG, "Processing GET %s", con->uri);
1045
1046 if ((!strncmp(con->uri, "/ppd/", 5) ||
1047 !strncmp(con->uri, "/printers/", 10) ||
1048 !strncmp(con->uri, "/classes/", 9)) &&
1049 !strcmp(con->uri + strlen(con->uri) - 4, ".ppd"))
1050 {
1051 /*
1052 * Send PPD file - get the real printer name since printer
1053 * names are not case sensitive but filenames can be...
1054 */
1055
1056 con->uri[strlen(con->uri) - 4] = '\0'; /* Drop ".ppd" */
1057
1058 if (!strncmp(con->uri, "/ppd/", 5))
1059 p = cupsdFindPrinter(con->uri + 5);
1060 else if (!strncmp(con->uri, "/printers/", 10))
1061 p = cupsdFindPrinter(con->uri + 10);
1062 else
1063 {
1064 p = cupsdFindClass(con->uri + 9);
1065
1066 if (p)
1067 {
1068 int i; /* Looping var */
1069
1070 for (i = 0; i < p->num_printers; i ++)
1071 {
1072 if (!(p->printers[i]->type & CUPS_PRINTER_CLASS))
1073 {
1074 char ppdname[1024];/* PPD filename */
1075
1076 snprintf(ppdname, sizeof(ppdname), "%s/ppd/%s.ppd",
1077 ServerRoot, p->printers[i]->name);
1078 if (!access(ppdname, 0))
1079 {
1080 p = p->printers[i];
1081 break;
1082 }
1083 }
1084 }
1085
1086 if (i >= p->num_printers)
1087 p = NULL;
1088 }
1089 }
1090
1091 if (p)
1092 {
1093 snprintf(con->uri, sizeof(con->uri), "/ppd/%s.ppd", p->name);
1094 }
1095 else
1096 {
1097 if (!cupsdSendError(con, HTTP_STATUS_NOT_FOUND, CUPSD_AUTH_NONE))
1098 {
1099 cupsdCloseClient(con);
1100 return;
1101 }
1102
1103 break;
1104 }
1105 }
1106 else if ((!strncmp(con->uri, "/icons/", 7) ||
1107 !strncmp(con->uri, "/printers/", 10) ||
1108 !strncmp(con->uri, "/classes/", 9)) &&
1109 !strcmp(con->uri + strlen(con->uri) - 4, ".png"))
1110 {
1111 /*
1112 * Send icon file - get the real queue name since queue names are
1113 * not case sensitive but filenames can be...
1114 */
1115
1116 con->uri[strlen(con->uri) - 4] = '\0'; /* Drop ".png" */
1117
1118 if (!strncmp(con->uri, "/icons/", 7))
1119 p = cupsdFindPrinter(con->uri + 7);
1120 else if (!strncmp(con->uri, "/printers/", 10))
1121 p = cupsdFindPrinter(con->uri + 10);
1122 else
1123 {
1124 p = cupsdFindClass(con->uri + 9);
1125
1126 if (p)
1127 {
1128 int i; /* Looping var */
1129
1130 for (i = 0; i < p->num_printers; i ++)
1131 {
1132 if (!(p->printers[i]->type & CUPS_PRINTER_CLASS))
1133 {
1134 char ppdname[1024];/* PPD filename */
1135
1136 snprintf(ppdname, sizeof(ppdname), "%s/ppd/%s.ppd",
1137 ServerRoot, p->printers[i]->name);
1138 if (!access(ppdname, 0))
1139 {
1140 p = p->printers[i];
1141 break;
1142 }
1143 }
1144 }
1145
1146 if (i >= p->num_printers)
1147 p = NULL;
1148 }
1149 }
1150
1151 if (p)
1152 snprintf(con->uri, sizeof(con->uri), "/icons/%s.png", p->name);
1153 else
1154 {
1155 if (!cupsdSendError(con, HTTP_STATUS_NOT_FOUND, CUPSD_AUTH_NONE))
1156 {
1157 cupsdCloseClient(con);
1158 return;
1159 }
1160
1161 break;
1162 }
1163 }
1164 else if (!WebInterface)
1165 {
1166 /*
1167 * Web interface is disabled. Show an appropriate message...
1168 */
1169
1170 if (!cupsdSendError(con, HTTP_STATUS_CUPS_WEBIF_DISABLED, CUPSD_AUTH_NONE))
1171 {
1172 cupsdCloseClient(con);
1173 return;
1174 }
1175
1176 break;
1177 }
1178
1179 if ((!strncmp(con->uri, "/admin", 6) &&
1180 strncmp(con->uri, "/admin/conf/", 12) &&
1181 strncmp(con->uri, "/admin/log/", 11)) ||
1182 !strncmp(con->uri, "/printers", 9) ||
1183 !strncmp(con->uri, "/classes", 8) ||
1184 !strncmp(con->uri, "/help", 5) ||
1185 !strncmp(con->uri, "/jobs", 5))
1186 {
1187 /*
1188 * Send CGI output...
1189 */
1190
1191 if (!strncmp(con->uri, "/admin", 6))
1192 {
1193 cupsdSetStringf(&con->command, "%s/cgi-bin/admin.cgi",
1194 ServerBin);
1195
1196 cupsdSetString(&con->options, strchr(con->uri + 6, '?'));
1197 }
1198 else if (!strncmp(con->uri, "/printers", 9))
1199 {
1200 cupsdSetStringf(&con->command, "%s/cgi-bin/printers.cgi",
1201 ServerBin);
1202
1203 if (con->uri[9] && con->uri[10])
1204 cupsdSetString(&con->options, con->uri + 9);
1205 else
1206 cupsdSetString(&con->options, NULL);
1207 }
1208 else if (!strncmp(con->uri, "/classes", 8))
1209 {
1210 cupsdSetStringf(&con->command, "%s/cgi-bin/classes.cgi",
1211 ServerBin);
1212
1213 if (con->uri[8] && con->uri[9])
1214 cupsdSetString(&con->options, con->uri + 8);
1215 else
1216 cupsdSetString(&con->options, NULL);
1217 }
1218 else if (!strncmp(con->uri, "/jobs", 5))
1219 {
1220 cupsdSetStringf(&con->command, "%s/cgi-bin/jobs.cgi",
1221 ServerBin);
1222
1223 if (con->uri[5] && con->uri[6])
1224 cupsdSetString(&con->options, con->uri + 5);
1225 else
1226 cupsdSetString(&con->options, NULL);
1227 }
1228 else
1229 {
1230 cupsdSetStringf(&con->command, "%s/cgi-bin/help.cgi",
1231 ServerBin);
1232
1233 if (con->uri[5] && con->uri[6])
1234 cupsdSetString(&con->options, con->uri + 5);
1235 else
1236 cupsdSetString(&con->options, NULL);
1237 }
1238
1239 if (!cupsdSendCommand(con, con->command, con->options, 0))
1240 {
1241 if (!cupsdSendError(con, HTTP_STATUS_NOT_FOUND, CUPSD_AUTH_NONE))
1242 {
1243 cupsdCloseClient(con);
1244 return;
1245 }
1246 }
1247 else
1248 cupsdLogRequest(con, HTTP_STATUS_OK);
1249
1250 if (httpGetVersion(con->http) <= HTTP_VERSION_1_0)
1251 httpSetKeepAlive(con->http, HTTP_KEEPALIVE_OFF);
1252 }
1253 else if ((!strncmp(con->uri, "/admin/conf/", 12) &&
1254 (strchr(con->uri + 12, '/') ||
1255 strlen(con->uri) == 12)) ||
1256 (!strncmp(con->uri, "/admin/log/", 11) &&
1257 (strchr(con->uri + 11, '/') ||
1258 strlen(con->uri) == 11)))
1259 {
1260 /*
1261 * GET can only be done to configuration files directly under
1262 * /admin/conf...
1263 */
1264
1265 cupsdLogClient(con, CUPSD_LOG_ERROR,
1266 "Request for subdirectory \"%s\"!", con->uri);
1267
1268 if (!cupsdSendError(con, HTTP_STATUS_FORBIDDEN, CUPSD_AUTH_NONE))
1269 {
1270 cupsdCloseClient(con);
1271 return;
1272 }
1273
1274 break;
1275 }
1276 else
1277 {
1278 /*
1279 * Serve a file...
1280 */
1281
1282 if ((filename = get_file(con, &filestats, buf,
1283 sizeof(buf))) == NULL)
1284 {
1285 if (!cupsdSendError(con, HTTP_STATUS_NOT_FOUND, CUPSD_AUTH_NONE))
1286 {
1287 cupsdCloseClient(con);
1288 return;
1289 }
1290
1291 break;
1292 }
1293
1294 type = mimeFileType(MimeDatabase, filename, NULL, NULL);
1295
1296 cupsdLogClient(con, CUPSD_LOG_DEBUG, "filename=\"%s\", type=%s/%s", filename, type ? type->super : "", type ? type->type : "");
1297
1298 if (is_cgi(con, filename, &filestats, type))
1299 {
1300 /*
1301 * Note: con->command and con->options were set by
1302 * is_cgi()...
1303 */
1304
1305 if (!cupsdSendCommand(con, con->command, con->options, 0))
1306 {
1307 if (!cupsdSendError(con, HTTP_STATUS_NOT_FOUND, CUPSD_AUTH_NONE))
1308 {
1309 cupsdCloseClient(con);
1310 return;
1311 }
1312 }
1313 else
1314 cupsdLogRequest(con, HTTP_STATUS_OK);
1315
1316 if (httpGetVersion(con->http) <= HTTP_VERSION_1_0)
1317 httpSetKeepAlive(con->http, HTTP_KEEPALIVE_OFF);
1318 break;
1319 }
1320
1321 if (!check_if_modified(con, &filestats))
1322 {
1323 if (!cupsdSendError(con, HTTP_STATUS_NOT_MODIFIED, CUPSD_AUTH_NONE))
1324 {
1325 cupsdCloseClient(con);
1326 return;
1327 }
1328 }
1329 else
1330 {
1331 if (type == NULL)
1332 strlcpy(line, "text/plain", sizeof(line));
1333 else
1334 snprintf(line, sizeof(line), "%s/%s", type->super, type->type);
1335
1336 if (!write_file(con, HTTP_STATUS_OK, filename, line, &filestats))
1337 {
1338 cupsdCloseClient(con);
1339 return;
1340 }
1341 }
1342 }
1343 break;
1344
1345 case HTTP_STATE_POST_RECV :
1346 /*
1347 * See if the POST request includes a Content-Length field, and if
1348 * so check the length against any limits that are set...
1349 */
1350
1351 if (httpGetField(con->http, HTTP_FIELD_CONTENT_LENGTH)[0] &&
1352 MaxRequestSize > 0 &&
1353 httpGetLength2(con->http) > MaxRequestSize)
1354 {
1355 /*
1356 * Request too large...
1357 */
1358
1359 if (!cupsdSendError(con, HTTP_STATUS_REQUEST_TOO_LARGE, CUPSD_AUTH_NONE))
1360 {
1361 cupsdCloseClient(con);
1362 return;
1363 }
1364
1365 break;
1366 }
1367 else if (httpGetLength2(con->http) < 0)
1368 {
1369 /*
1370 * Negative content lengths are invalid!
1371 */
1372
1373 if (!cupsdSendError(con, HTTP_STATUS_BAD_REQUEST, CUPSD_AUTH_NONE))
1374 {
1375 cupsdCloseClient(con);
1376 return;
1377 }
1378
1379 break;
1380 }
1381
1382 /*
1383 * See what kind of POST request this is; for IPP requests the
1384 * content-type field will be "application/ipp"...
1385 */
1386
1387 if (!strcmp(httpGetField(con->http, HTTP_FIELD_CONTENT_TYPE),
1388 "application/ipp"))
1389 con->request = ippNew();
1390 else if (!WebInterface)
1391 {
1392 /*
1393 * Web interface is disabled. Show an appropriate message...
1394 */
1395
1396 if (!cupsdSendError(con, HTTP_STATUS_CUPS_WEBIF_DISABLED, CUPSD_AUTH_NONE))
1397 {
1398 cupsdCloseClient(con);
1399 return;
1400 }
1401
1402 break;
1403 }
1404 else if ((!strncmp(con->uri, "/admin", 6) &&
1405 strncmp(con->uri, "/admin/conf/", 12) &&
1406 strncmp(con->uri, "/admin/log/", 11)) ||
1407 !strncmp(con->uri, "/printers", 9) ||
1408 !strncmp(con->uri, "/classes", 8) ||
1409 !strncmp(con->uri, "/help", 5) ||
1410 !strncmp(con->uri, "/jobs", 5))
1411 {
1412 /*
1413 * CGI request...
1414 */
1415
1416 if (!strncmp(con->uri, "/admin", 6))
1417 {
1418 cupsdSetStringf(&con->command, "%s/cgi-bin/admin.cgi",
1419 ServerBin);
1420
1421 cupsdSetString(&con->options, strchr(con->uri + 6, '?'));
1422 }
1423 else if (!strncmp(con->uri, "/printers", 9))
1424 {
1425 cupsdSetStringf(&con->command, "%s/cgi-bin/printers.cgi",
1426 ServerBin);
1427
1428 if (con->uri[9] && con->uri[10])
1429 cupsdSetString(&con->options, con->uri + 9);
1430 else
1431 cupsdSetString(&con->options, NULL);
1432 }
1433 else if (!strncmp(con->uri, "/classes", 8))
1434 {
1435 cupsdSetStringf(&con->command, "%s/cgi-bin/classes.cgi",
1436 ServerBin);
1437
1438 if (con->uri[8] && con->uri[9])
1439 cupsdSetString(&con->options, con->uri + 8);
1440 else
1441 cupsdSetString(&con->options, NULL);
1442 }
1443 else if (!strncmp(con->uri, "/jobs", 5))
1444 {
1445 cupsdSetStringf(&con->command, "%s/cgi-bin/jobs.cgi",
1446 ServerBin);
1447
1448 if (con->uri[5] && con->uri[6])
1449 cupsdSetString(&con->options, con->uri + 5);
1450 else
1451 cupsdSetString(&con->options, NULL);
1452 }
1453 else
1454 {
1455 cupsdSetStringf(&con->command, "%s/cgi-bin/help.cgi",
1456 ServerBin);
1457
1458 if (con->uri[5] && con->uri[6])
1459 cupsdSetString(&con->options, con->uri + 5);
1460 else
1461 cupsdSetString(&con->options, NULL);
1462 }
1463
1464 if (httpGetVersion(con->http) <= HTTP_VERSION_1_0)
1465 httpSetKeepAlive(con->http, HTTP_KEEPALIVE_OFF);
1466 }
1467 else
1468 {
1469 /*
1470 * POST to a file...
1471 */
1472
1473 if ((filename = get_file(con, &filestats, buf,
1474 sizeof(buf))) == NULL)
1475 {
1476 if (!cupsdSendError(con, HTTP_STATUS_NOT_FOUND, CUPSD_AUTH_NONE))
1477 {
1478 cupsdCloseClient(con);
1479 return;
1480 }
1481
1482 break;
1483 }
1484
1485 type = mimeFileType(MimeDatabase, filename, NULL, NULL);
1486
1487 if (!is_cgi(con, filename, &filestats, type))
1488 {
1489 /*
1490 * Only POST to CGI's...
1491 */
1492
1493 if (!cupsdSendError(con, HTTP_STATUS_UNAUTHORIZED, CUPSD_AUTH_NONE))
1494 {
1495 cupsdCloseClient(con);
1496 return;
1497 }
1498 }
1499 }
1500 break;
1501
1502 case HTTP_STATE_PUT_RECV :
1503 /*
1504 * Validate the resource name...
1505 */
1506
1507 if (strcmp(con->uri, "/admin/conf/cupsd.conf"))
1508 {
1509 /*
1510 * PUT can only be done to the cupsd.conf file...
1511 */
1512
1513 cupsdLogClient(con, CUPSD_LOG_ERROR,
1514 "Disallowed PUT request for \"%s\".", con->uri);
1515
1516 if (!cupsdSendError(con, HTTP_STATUS_FORBIDDEN, CUPSD_AUTH_NONE))
1517 {
1518 cupsdCloseClient(con);
1519 return;
1520 }
1521
1522 break;
1523 }
1524
1525 /*
1526 * See if the PUT request includes a Content-Length field, and if
1527 * so check the length against any limits that are set...
1528 */
1529
1530 if (httpGetField(con->http, HTTP_FIELD_CONTENT_LENGTH)[0] &&
1531 MaxRequestSize > 0 &&
1532 httpGetLength2(con->http) > MaxRequestSize)
1533 {
1534 /*
1535 * Request too large...
1536 */
1537
1538 if (!cupsdSendError(con, HTTP_STATUS_REQUEST_TOO_LARGE, CUPSD_AUTH_NONE))
1539 {
1540 cupsdCloseClient(con);
1541 return;
1542 }
1543
1544 break;
1545 }
1546 else if (httpGetLength2(con->http) < 0)
1547 {
1548 /*
1549 * Negative content lengths are invalid!
1550 */
1551
1552 if (!cupsdSendError(con, HTTP_STATUS_BAD_REQUEST, CUPSD_AUTH_NONE))
1553 {
1554 cupsdCloseClient(con);
1555 return;
1556 }
1557
1558 break;
1559 }
1560
1561 /*
1562 * Open a temporary file to hold the request...
1563 */
1564
1565 cupsdSetStringf(&con->filename, "%s/%08x", RequestRoot,
1566 request_id ++);
1567 con->file = open(con->filename, O_WRONLY | O_CREAT | O_TRUNC, 0640);
1568
1569 if (con->file < 0)
1570 {
1571 cupsdLogClient(con, CUPSD_LOG_ERROR,
1572 "Unable to create request file \"%s\": %s",
1573 con->filename, strerror(errno));
1574
1575 if (!cupsdSendError(con, HTTP_STATUS_REQUEST_TOO_LARGE, CUPSD_AUTH_NONE))
1576 {
1577 cupsdCloseClient(con);
1578 return;
1579 }
1580 }
1581
1582 fchmod(con->file, 0640);
1583 fchown(con->file, RunUser, Group);
1584 fcntl(con->file, F_SETFD, fcntl(con->file, F_GETFD) | FD_CLOEXEC);
1585 break;
1586
1587 case HTTP_STATE_DELETE :
1588 case HTTP_STATE_TRACE :
1589 cupsdSendError(con, HTTP_STATUS_NOT_IMPLEMENTED, CUPSD_AUTH_NONE);
1590 cupsdCloseClient(con);
1591 return;
1592
1593 case HTTP_STATE_HEAD :
1594 if (!strncmp(con->uri, "/printers/", 10) &&
1595 !strcmp(con->uri + strlen(con->uri) - 4, ".ppd"))
1596 {
1597 /*
1598 * Send PPD file - get the real printer name since printer
1599 * names are not case sensitive but filenames can be...
1600 */
1601
1602 con->uri[strlen(con->uri) - 4] = '\0'; /* Drop ".ppd" */
1603
1604 if ((p = cupsdFindPrinter(con->uri + 10)) != NULL)
1605 snprintf(con->uri, sizeof(con->uri), "/ppd/%s.ppd", p->name);
1606 else
1607 {
1608 if (!cupsdSendError(con, HTTP_STATUS_NOT_FOUND, CUPSD_AUTH_NONE))
1609 {
1610 cupsdCloseClient(con);
1611 return;
1612 }
1613
1614 cupsdLogRequest(con, HTTP_STATUS_NOT_FOUND);
1615 break;
1616 }
1617 }
1618 else if (!strncmp(con->uri, "/printers/", 10) &&
1619 !strcmp(con->uri + strlen(con->uri) - 4, ".png"))
1620 {
1621 /*
1622 * Send PNG file - get the real printer name since printer
1623 * names are not case sensitive but filenames can be...
1624 */
1625
1626 con->uri[strlen(con->uri) - 4] = '\0'; /* Drop ".ppd" */
1627
1628 if ((p = cupsdFindPrinter(con->uri + 10)) != NULL)
1629 snprintf(con->uri, sizeof(con->uri), "/icons/%s.png", p->name);
1630 else
1631 {
1632 if (!cupsdSendError(con, HTTP_STATUS_NOT_FOUND, CUPSD_AUTH_NONE))
1633 {
1634 cupsdCloseClient(con);
1635 return;
1636 }
1637
1638 cupsdLogRequest(con, HTTP_STATUS_NOT_FOUND);
1639 break;
1640 }
1641 }
1642 else if (!WebInterface)
1643 {
1644 httpClearFields(con->http);
1645
1646 if (!cupsdSendHeader(con, HTTP_STATUS_OK, NULL, CUPSD_AUTH_NONE))
1647 {
1648 cupsdCloseClient(con);
1649 return;
1650 }
1651
1652 cupsdLogRequest(con, HTTP_STATUS_OK);
1653 break;
1654 }
1655
1656 if ((!strncmp(con->uri, "/admin", 6) &&
1657 strncmp(con->uri, "/admin/conf/", 12) &&
1658 strncmp(con->uri, "/admin/log/", 11)) ||
1659 !strncmp(con->uri, "/printers", 9) ||
1660 !strncmp(con->uri, "/classes", 8) ||
1661 !strncmp(con->uri, "/help", 5) ||
1662 !strncmp(con->uri, "/jobs", 5))
1663 {
1664 /*
1665 * CGI output...
1666 */
1667
1668 httpClearFields(con->http);
1669
1670 if (!cupsdSendHeader(con, HTTP_STATUS_OK, "text/html", CUPSD_AUTH_NONE))
1671 {
1672 cupsdCloseClient(con);
1673 return;
1674 }
1675
1676 cupsdLogRequest(con, HTTP_STATUS_OK);
1677 }
1678 else if ((!strncmp(con->uri, "/admin/conf/", 12) &&
1679 (strchr(con->uri + 12, '/') ||
1680 strlen(con->uri) == 12)) ||
1681 (!strncmp(con->uri, "/admin/log/", 11) &&
1682 (strchr(con->uri + 11, '/') ||
1683 strlen(con->uri) == 11)))
1684 {
1685 /*
1686 * HEAD can only be done to configuration files under
1687 * /admin/conf...
1688 */
1689
1690 cupsdLogClient(con, CUPSD_LOG_ERROR,
1691 "Request for subdirectory \"%s\".", con->uri);
1692
1693 if (!cupsdSendError(con, HTTP_STATUS_FORBIDDEN, CUPSD_AUTH_NONE))
1694 {
1695 cupsdCloseClient(con);
1696 return;
1697 }
1698
1699 cupsdLogRequest(con, HTTP_STATUS_FORBIDDEN);
1700 break;
1701 }
1702 else if ((filename = get_file(con, &filestats, buf,
1703 sizeof(buf))) == NULL)
1704 {
1705 httpClearFields(con->http);
1706
1707 if (!cupsdSendHeader(con, HTTP_STATUS_NOT_FOUND, "text/html",
1708 CUPSD_AUTH_NONE))
1709 {
1710 cupsdCloseClient(con);
1711 return;
1712 }
1713
1714 cupsdLogRequest(con, HTTP_STATUS_NOT_FOUND);
1715 }
1716 else if (!check_if_modified(con, &filestats))
1717 {
1718 if (!cupsdSendError(con, HTTP_STATUS_NOT_MODIFIED, CUPSD_AUTH_NONE))
1719 {
1720 cupsdCloseClient(con);
1721 return;
1722 }
1723
1724 cupsdLogRequest(con, HTTP_STATUS_NOT_MODIFIED);
1725 }
1726 else
1727 {
1728 /*
1729 * Serve a file...
1730 */
1731
1732 type = mimeFileType(MimeDatabase, filename, NULL, NULL);
1733 if (type == NULL)
1734 strlcpy(line, "text/plain", sizeof(line));
1735 else
1736 snprintf(line, sizeof(line), "%s/%s", type->super, type->type);
1737
1738 httpClearFields(con->http);
1739
1740 httpSetField(con->http, HTTP_FIELD_LAST_MODIFIED,
1741 httpGetDateString(filestats.st_mtime));
1742 httpSetLength(con->http, (size_t)filestats.st_size);
1743
1744 if (!cupsdSendHeader(con, HTTP_STATUS_OK, line, CUPSD_AUTH_NONE))
1745 {
1746 cupsdCloseClient(con);
1747 return;
1748 }
1749
1750 cupsdLogRequest(con, HTTP_STATUS_OK);
1751 }
1752 break;
1753
1754 default :
1755 break; /* Anti-compiler-warning-code */
1756 }
1757 }
1758 }
1759
1760 /*
1761 * Handle any incoming data...
1762 */
1763
1764 switch (httpGetState(con->http))
1765 {
1766 case HTTP_STATE_PUT_RECV :
1767 do
1768 {
1769 if ((bytes = httpRead2(con->http, line, sizeof(line))) < 0)
1770 {
1771 if (httpError(con->http) && httpError(con->http) != EPIPE)
1772 cupsdLogClient(con, CUPSD_LOG_DEBUG,
1773 "HTTP_STATE_PUT_RECV Closing for error %d (%s)",
1774 httpError(con->http), strerror(httpError(con->http)));
1775 else
1776 cupsdLogClient(con, CUPSD_LOG_DEBUG,
1777 "HTTP_STATE_PUT_RECV Closing on EOF.");
1778
1779 cupsdCloseClient(con);
1780 return;
1781 }
1782 else if (bytes > 0)
1783 {
1784 con->bytes += bytes;
1785
1786 if (write(con->file, line, (size_t)bytes) < bytes)
1787 {
1788 cupsdLogClient(con, CUPSD_LOG_ERROR,
1789 "Unable to write %d bytes to \"%s\": %s", bytes,
1790 con->filename, strerror(errno));
1791
1792 close(con->file);
1793 con->file = -1;
1794 unlink(con->filename);
1795 cupsdClearString(&con->filename);
1796
1797 if (!cupsdSendError(con, HTTP_STATUS_REQUEST_TOO_LARGE, CUPSD_AUTH_NONE))
1798 {
1799 cupsdCloseClient(con);
1800 return;
1801 }
1802 }
1803 }
1804 }
1805 while (httpGetState(con->http) == HTTP_STATE_PUT_RECV && httpGetReady(con->http));
1806
1807 if (httpGetState(con->http) == HTTP_STATE_STATUS)
1808 {
1809 /*
1810 * End of file, see how big it is...
1811 */
1812
1813 fstat(con->file, &filestats);
1814
1815 close(con->file);
1816 con->file = -1;
1817
1818 if (filestats.st_size > MaxRequestSize &&
1819 MaxRequestSize > 0)
1820 {
1821 /*
1822 * Request is too big; remove it and send an error...
1823 */
1824
1825 unlink(con->filename);
1826 cupsdClearString(&con->filename);
1827
1828 if (!cupsdSendError(con, HTTP_STATUS_REQUEST_TOO_LARGE, CUPSD_AUTH_NONE))
1829 {
1830 cupsdCloseClient(con);
1831 return;
1832 }
1833 }
1834
1835 /*
1836 * Install the configuration file...
1837 */
1838
1839 status = install_cupsd_conf(con);
1840
1841 /*
1842 * Return the status to the client...
1843 */
1844
1845 if (!cupsdSendError(con, status, CUPSD_AUTH_NONE))
1846 {
1847 cupsdCloseClient(con);
1848 return;
1849 }
1850 }
1851 break;
1852
1853 case HTTP_STATE_POST_RECV :
1854 do
1855 {
1856 if (con->request && con->file < 0)
1857 {
1858 /*
1859 * Grab any request data from the connection...
1860 */
1861
1862 if (!httpWait(con->http, 0))
1863 return;
1864
1865 if ((ipp_state = ippRead(con->http, con->request)) == IPP_STATE_ERROR)
1866 {
1867 cupsdLogClient(con, CUPSD_LOG_ERROR, "IPP read error: %s",
1868 cupsLastErrorString());
1869
1870 cupsdSendError(con, HTTP_STATUS_BAD_REQUEST, CUPSD_AUTH_NONE);
1871 cupsdCloseClient(con);
1872 return;
1873 }
1874 else if (ipp_state != IPP_STATE_DATA)
1875 {
1876 if (httpGetState(con->http) == HTTP_STATE_POST_SEND)
1877 {
1878 cupsdSendError(con, HTTP_STATUS_BAD_REQUEST, CUPSD_AUTH_NONE);
1879 cupsdCloseClient(con);
1880 return;
1881 }
1882
1883 if (httpGetReady(con->http))
1884 continue;
1885 break;
1886 }
1887 else
1888 {
1889 cupsdLogClient(con, CUPSD_LOG_DEBUG, "%d.%d %s %d",
1890 con->request->request.op.version[0],
1891 con->request->request.op.version[1],
1892 ippOpString(con->request->request.op.operation_id),
1893 con->request->request.op.request_id);
1894 con->bytes += ippLength(con->request);
1895 }
1896 }
1897
1898 if (con->file < 0 && httpGetState(con->http) != HTTP_STATE_POST_SEND)
1899 {
1900 /*
1901 * Create a file as needed for the request data...
1902 */
1903
1904 cupsdSetStringf(&con->filename, "%s/%08x", RequestRoot,
1905 request_id ++);
1906 con->file = open(con->filename, O_WRONLY | O_CREAT | O_TRUNC, 0640);
1907
1908 if (con->file < 0)
1909 {
1910 cupsdLogClient(con, CUPSD_LOG_ERROR,
1911 "Unable to create request file \"%s\": %s",
1912 con->filename, strerror(errno));
1913
1914 if (!cupsdSendError(con, HTTP_STATUS_REQUEST_TOO_LARGE, CUPSD_AUTH_NONE))
1915 {
1916 cupsdCloseClient(con);
1917 return;
1918 }
1919 }
1920
1921 fchmod(con->file, 0640);
1922 fchown(con->file, RunUser, Group);
1923 fcntl(con->file, F_SETFD, fcntl(con->file, F_GETFD) | FD_CLOEXEC);
1924 }
1925
1926 if (httpGetState(con->http) != HTTP_STATE_POST_SEND)
1927 {
1928 if (!httpWait(con->http, 0))
1929 return;
1930 else if ((bytes = httpRead2(con->http, line, sizeof(line))) < 0)
1931 {
1932 if (httpError(con->http) && httpError(con->http) != EPIPE)
1933 cupsdLogClient(con, CUPSD_LOG_DEBUG,
1934 "HTTP_STATE_POST_SEND Closing for error %d (%s)",
1935 httpError(con->http), strerror(httpError(con->http)));
1936 else
1937 cupsdLogClient(con, CUPSD_LOG_DEBUG,
1938 "HTTP_STATE_POST_SEND Closing on EOF.");
1939
1940 cupsdCloseClient(con);
1941 return;
1942 }
1943 else if (bytes > 0)
1944 {
1945 con->bytes += bytes;
1946
1947 if (write(con->file, line, (size_t)bytes) < bytes)
1948 {
1949 cupsdLogClient(con, CUPSD_LOG_ERROR,
1950 "Unable to write %d bytes to \"%s\": %s",
1951 bytes, con->filename, strerror(errno));
1952
1953 close(con->file);
1954 con->file = -1;
1955 unlink(con->filename);
1956 cupsdClearString(&con->filename);
1957
1958 if (!cupsdSendError(con, HTTP_STATUS_REQUEST_TOO_LARGE,
1959 CUPSD_AUTH_NONE))
1960 {
1961 cupsdCloseClient(con);
1962 return;
1963 }
1964 }
1965 }
1966 else if (httpGetState(con->http) == HTTP_STATE_POST_RECV)
1967 return;
1968 else if (httpGetState(con->http) != HTTP_STATE_POST_SEND)
1969 {
1970 cupsdLogClient(con, CUPSD_LOG_DEBUG,
1971 "Closing on unexpected state %s.",
1972 httpStateString(httpGetState(con->http)));
1973 cupsdCloseClient(con);
1974 return;
1975 }
1976 }
1977 }
1978 while (httpGetState(con->http) == HTTP_STATE_POST_RECV && httpGetReady(con->http));
1979
1980 if (httpGetState(con->http) == HTTP_STATE_POST_SEND)
1981 {
1982 if (con->file >= 0)
1983 {
1984 fstat(con->file, &filestats);
1985
1986 close(con->file);
1987 con->file = -1;
1988
1989 if (filestats.st_size > MaxRequestSize &&
1990 MaxRequestSize > 0)
1991 {
1992 /*
1993 * Request is too big; remove it and send an error...
1994 */
1995
1996 unlink(con->filename);
1997 cupsdClearString(&con->filename);
1998
1999 if (con->request)
2000 {
2001 /*
2002 * Delete any IPP request data...
2003 */
2004
2005 ippDelete(con->request);
2006 con->request = NULL;
2007 }
2008
2009 if (!cupsdSendError(con, HTTP_STATUS_REQUEST_TOO_LARGE, CUPSD_AUTH_NONE))
2010 {
2011 cupsdCloseClient(con);
2012 return;
2013 }
2014 }
2015 else if (filestats.st_size == 0)
2016 {
2017 /*
2018 * Don't allow empty file...
2019 */
2020
2021 unlink(con->filename);
2022 cupsdClearString(&con->filename);
2023 }
2024
2025 if (con->command)
2026 {
2027 if (!cupsdSendCommand(con, con->command, con->options, 0))
2028 {
2029 if (!cupsdSendError(con, HTTP_STATUS_NOT_FOUND, CUPSD_AUTH_NONE))
2030 {
2031 cupsdCloseClient(con);
2032 return;
2033 }
2034 }
2035 else
2036 cupsdLogRequest(con, HTTP_STATUS_OK);
2037 }
2038 }
2039
2040 if (con->request)
2041 {
2042 cupsdProcessIPPRequest(con);
2043
2044 if (con->filename)
2045 {
2046 unlink(con->filename);
2047 cupsdClearString(&con->filename);
2048 }
2049
2050 return;
2051 }
2052 }
2053 break;
2054
2055 default :
2056 break; /* Anti-compiler-warning-code */
2057 }
2058
2059 if (httpGetState(con->http) == HTTP_STATE_WAITING)
2060 {
2061 if (!httpGetKeepAlive(con->http))
2062 {
2063 cupsdLogClient(con, CUPSD_LOG_DEBUG,
2064 "Closing because Keep-Alive is disabled.");
2065 cupsdCloseClient(con);
2066 }
2067 else
2068 {
2069 cupsArrayRemove(ActiveClients, con);
2070 cupsdSetBusyState();
2071 }
2072 }
2073 }
2074
2075
2076 /*
2077 * 'cupsdSendCommand()' - Send output from a command via HTTP.
2078 */
2079
2080 int /* O - 1 on success, 0 on failure */
2081 cupsdSendCommand(
2082 cupsd_client_t *con, /* I - Client connection */
2083 char *command, /* I - Command to run */
2084 char *options, /* I - Command-line options */
2085 int root) /* I - Run as root? */
2086 {
2087 int fd; /* Standard input file descriptor */
2088
2089
2090 if (con->filename)
2091 {
2092 fd = open(con->filename, O_RDONLY);
2093
2094 if (fd < 0)
2095 {
2096 cupsdLogClient(con, CUPSD_LOG_ERROR,
2097 "Unable to open \"%s\" for reading: %s",
2098 con->filename ? con->filename : "/dev/null",
2099 strerror(errno));
2100 return (0);
2101 }
2102
2103 fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC);
2104 }
2105 else
2106 fd = -1;
2107
2108 con->pipe_pid = pipe_command(con, fd, &(con->file), command, options, root);
2109 con->pipe_status = HTTP_STATUS_OK;
2110
2111 httpClearFields(con->http);
2112
2113 if (fd >= 0)
2114 close(fd);
2115
2116 cupsdLogClient(con, CUPSD_LOG_INFO, "Started \"%s\" (pid=%d, file=%d)",
2117 command, con->pipe_pid, con->file);
2118
2119 if (con->pipe_pid == 0)
2120 return (0);
2121
2122 fcntl(con->file, F_SETFD, fcntl(con->file, F_GETFD) | FD_CLOEXEC);
2123
2124 cupsdAddSelect(con->file, (cupsd_selfunc_t)write_pipe, NULL, con);
2125
2126 cupsdLogClient(con, CUPSD_LOG_DEBUG, "Waiting for CGI data.");
2127
2128 con->sent_header = 0;
2129 con->file_ready = 0;
2130 con->got_fields = 0;
2131 con->header_used = 0;
2132
2133 return (1);
2134 }
2135
2136
2137 /*
2138 * 'cupsdSendError()' - Send an error message via HTTP.
2139 */
2140
2141 int /* O - 1 if successful, 0 otherwise */
2142 cupsdSendError(cupsd_client_t *con, /* I - Connection */
2143 http_status_t code, /* I - Error code */
2144 int auth_type)/* I - Authentication type */
2145 {
2146 cupsdLogClient(con, CUPSD_LOG_DEBUG2, "cupsdSendError code=%d, auth_type=%d",
2147 code, auth_type);
2148
2149 #ifdef HAVE_SSL
2150 /*
2151 * Force client to upgrade for authentication if that is how the
2152 * server is configured...
2153 */
2154
2155 if (code == HTTP_STATUS_UNAUTHORIZED &&
2156 DefaultEncryption == HTTP_ENCRYPTION_REQUIRED &&
2157 _cups_strcasecmp(httpGetHostname(con->http, NULL, 0), "localhost") &&
2158 !httpIsEncrypted(con->http))
2159 {
2160 code = HTTP_STATUS_UPGRADE_REQUIRED;
2161 }
2162 #endif /* HAVE_SSL */
2163
2164 /*
2165 * Put the request in the access_log file...
2166 */
2167
2168 cupsdLogRequest(con, code);
2169
2170 /*
2171 * To work around bugs in some proxies, don't use Keep-Alive for some
2172 * error messages...
2173 *
2174 * Kerberos authentication doesn't work without Keep-Alive, so
2175 * never disable it in that case.
2176 */
2177
2178 httpClearFields(con->http);
2179
2180 if (code >= HTTP_STATUS_BAD_REQUEST && con->type != CUPSD_AUTH_NEGOTIATE)
2181 httpSetKeepAlive(con->http, HTTP_KEEPALIVE_OFF);
2182
2183 if (httpGetVersion(con->http) >= HTTP_VERSION_1_1 &&
2184 httpGetKeepAlive(con->http) == HTTP_KEEPALIVE_OFF)
2185 httpSetField(con->http, HTTP_FIELD_CONNECTION, "close");
2186
2187 if (code >= HTTP_STATUS_BAD_REQUEST)
2188 {
2189 /*
2190 * Send a human-readable error message.
2191 */
2192
2193 char message[4096], /* Message for user */
2194 urltext[1024], /* URL redirection text */
2195 redirect[1024]; /* Redirection link */
2196 const char *text; /* Status-specific text */
2197
2198
2199 redirect[0] = '\0';
2200
2201 if (code == HTTP_STATUS_UNAUTHORIZED)
2202 text = _cupsLangString(con->language,
2203 _("Enter your username and password or the "
2204 "root username and password to access this "
2205 "page. If you are using Kerberos authentication, "
2206 "make sure you have a valid Kerberos ticket."));
2207 else if (code == HTTP_STATUS_UPGRADE_REQUIRED)
2208 {
2209 text = urltext;
2210
2211 snprintf(urltext, sizeof(urltext),
2212 _cupsLangString(con->language,
2213 _("You must access this page using the URL "
2214 "<A HREF=\"https://%s:%d%s\">"
2215 "https://%s:%d%s</A>.")),
2216 con->servername, con->serverport, con->uri,
2217 con->servername, con->serverport, con->uri);
2218
2219 snprintf(redirect, sizeof(redirect),
2220 "<META HTTP-EQUIV=\"Refresh\" "
2221 "CONTENT=\"3;URL=https://%s:%d%s\">\n",
2222 con->servername, con->serverport, con->uri);
2223 }
2224 else if (code == HTTP_STATUS_CUPS_WEBIF_DISABLED)
2225 text = _cupsLangString(con->language,
2226 _("The web interface is currently disabled. Run "
2227 "\"cupsctl WebInterface=yes\" to enable it."));
2228 else
2229 text = "";
2230
2231 snprintf(message, sizeof(message),
2232 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" "
2233 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
2234 "<HTML>\n"
2235 "<HEAD>\n"
2236 "\t<META HTTP-EQUIV=\"Content-Type\" "
2237 "CONTENT=\"text/html; charset=utf-8\">\n"
2238 "\t<TITLE>%s - " CUPS_SVERSION "</TITLE>\n"
2239 "\t<LINK REL=\"STYLESHEET\" TYPE=\"text/css\" "
2240 "HREF=\"/cups.css\">\n"
2241 "%s"
2242 "</HEAD>\n"
2243 "<BODY>\n"
2244 "<H1>%s</H1>\n"
2245 "<P>%s</P>\n"
2246 "</BODY>\n"
2247 "</HTML>\n",
2248 _httpStatus(con->language, code), redirect,
2249 _httpStatus(con->language, code), text);
2250
2251 /*
2252 * Send an error message back to the client. If the error code is a
2253 * 400 or 500 series, make sure the message contains some text, too!
2254 */
2255
2256 size_t length = strlen(message); /* Length of message */
2257
2258 httpSetLength(con->http, length);
2259
2260 if (!cupsdSendHeader(con, code, "text/html", auth_type))
2261 return (0);
2262
2263 if (httpWrite2(con->http, message, length) < 0)
2264 return (0);
2265
2266 if (httpFlushWrite(con->http) < 0)
2267 return (0);
2268 }
2269 else
2270 {
2271 httpSetField(con->http, HTTP_FIELD_CONTENT_LENGTH, "0");
2272
2273 if (!cupsdSendHeader(con, code, NULL, auth_type))
2274 return (0);
2275 }
2276
2277 return (1);
2278 }
2279
2280
2281 /*
2282 * 'cupsdSendHeader()' - Send an HTTP request.
2283 */
2284
2285 int /* O - 1 on success, 0 on failure */
2286 cupsdSendHeader(
2287 cupsd_client_t *con, /* I - Client to send to */
2288 http_status_t code, /* I - HTTP status code */
2289 char *type, /* I - MIME type of document */
2290 int auth_type) /* I - Type of authentication */
2291 {
2292 char auth_str[1024]; /* Authorization string */
2293
2294
2295 cupsdLogClient(con, CUPSD_LOG_DEBUG, "cupsdSendHeader: code=%d, type=\"%s\", auth_type=%d", code, type, auth_type);
2296
2297 /*
2298 * Send the HTTP status header...
2299 */
2300
2301 if (code == HTTP_STATUS_CUPS_WEBIF_DISABLED)
2302 {
2303 /*
2304 * Treat our special "web interface is disabled" status as "200 OK" for web
2305 * browsers.
2306 */
2307
2308 code = HTTP_STATUS_OK;
2309 }
2310
2311 if (ServerHeader)
2312 httpSetField(con->http, HTTP_FIELD_SERVER, ServerHeader);
2313
2314 if (code == HTTP_STATUS_METHOD_NOT_ALLOWED)
2315 httpSetField(con->http, HTTP_FIELD_ALLOW, "GET, HEAD, OPTIONS, POST, PUT");
2316
2317 if (code == HTTP_STATUS_UNAUTHORIZED)
2318 {
2319 if (auth_type == CUPSD_AUTH_NONE)
2320 {
2321 if (!con->best || con->best->type <= CUPSD_AUTH_NONE)
2322 auth_type = cupsdDefaultAuthType();
2323 else
2324 auth_type = con->best->type;
2325 }
2326
2327 auth_str[0] = '\0';
2328
2329 if (auth_type == CUPSD_AUTH_BASIC)
2330 strlcpy(auth_str, "Basic realm=\"CUPS\"", sizeof(auth_str));
2331 #ifdef HAVE_GSSAPI
2332 else if (auth_type == CUPSD_AUTH_NEGOTIATE)
2333 {
2334 # ifdef AF_LOCAL
2335 if (httpAddrFamily(httpGetAddress(con->http)) == AF_LOCAL)
2336 strlcpy(auth_str, "Basic realm=\"CUPS\"", sizeof(auth_str));
2337 else
2338 # endif /* AF_LOCAL */
2339 strlcpy(auth_str, "Negotiate", sizeof(auth_str));
2340 }
2341 #endif /* HAVE_GSSAPI */
2342
2343 if (con->best && auth_type != CUPSD_AUTH_NEGOTIATE &&
2344 !_cups_strcasecmp(httpGetHostname(con->http, NULL, 0), "localhost"))
2345 {
2346 /*
2347 * Add a "trc" (try root certification) parameter for local non-Kerberos
2348 * requests when the request requires system group membership - then the
2349 * client knows the root certificate can/should be used.
2350 *
2351 * Also, for OS X we also look for @AUTHKEY and add an "authkey"
2352 * parameter as needed...
2353 */
2354
2355 char *name, /* Current user name */
2356 *auth_key; /* Auth key buffer */
2357 size_t auth_size; /* Size of remaining buffer */
2358
2359 auth_key = auth_str + strlen(auth_str);
2360 auth_size = sizeof(auth_str) - (size_t)(auth_key - auth_str);
2361
2362 for (name = (char *)cupsArrayFirst(con->best->names);
2363 name;
2364 name = (char *)cupsArrayNext(con->best->names))
2365 {
2366 #ifdef HAVE_AUTHORIZATION_H
2367 if (!_cups_strncasecmp(name, "@AUTHKEY(", 9))
2368 {
2369 snprintf(auth_key, auth_size, ", authkey=\"%s\"", name + 9);
2370 /* end parenthesis is stripped in conf.c */
2371 break;
2372 }
2373 else
2374 #endif /* HAVE_AUTHORIZATION_H */
2375 if (!_cups_strcasecmp(name, "@SYSTEM"))
2376 {
2377 #ifdef HAVE_AUTHORIZATION_H
2378 if (SystemGroupAuthKey)
2379 snprintf(auth_key, auth_size,
2380 ", authkey=\"%s\"",
2381 SystemGroupAuthKey);
2382 else
2383 #else
2384 strlcpy(auth_key, ", trc=\"y\"", auth_size);
2385 #endif /* HAVE_AUTHORIZATION_H */
2386 break;
2387 }
2388 }
2389 }
2390
2391 if (auth_str[0])
2392 {
2393 cupsdLogClient(con, CUPSD_LOG_DEBUG, "WWW-Authenticate: %s", auth_str);
2394
2395 httpSetField(con->http, HTTP_FIELD_WWW_AUTHENTICATE, auth_str);
2396 }
2397 }
2398
2399 if (con->language && strcmp(con->language->language, "C"))
2400 httpSetField(con->http, HTTP_FIELD_CONTENT_LANGUAGE, con->language->language);
2401
2402 if (type)
2403 {
2404 if (!strcmp(type, "text/html"))
2405 httpSetField(con->http, HTTP_FIELD_CONTENT_TYPE, "text/html; charset=utf-8");
2406 else
2407 httpSetField(con->http, HTTP_FIELD_CONTENT_TYPE, type);
2408 }
2409
2410 return (!httpWriteResponse(con->http, code));
2411 }
2412
2413
2414 /*
2415 * 'cupsdUpdateCGI()' - Read status messages from CGI scripts and programs.
2416 */
2417
2418 void
2419 cupsdUpdateCGI(void)
2420 {
2421 char *ptr, /* Pointer to end of line in buffer */
2422 message[1024]; /* Pointer to message text */
2423 int loglevel; /* Log level for message */
2424
2425
2426 while ((ptr = cupsdStatBufUpdate(CGIStatusBuffer, &loglevel,
2427 message, sizeof(message))) != NULL)
2428 {
2429 if (loglevel == CUPSD_LOG_INFO)
2430 cupsdLogMessage(CUPSD_LOG_INFO, "%s", message);
2431
2432 if (!strchr(CGIStatusBuffer->buffer, '\n'))
2433 break;
2434 }
2435
2436 if (ptr == NULL && !CGIStatusBuffer->bufused)
2437 {
2438 /*
2439 * Fatal error on pipe - should never happen!
2440 */
2441
2442 cupsdLogMessage(CUPSD_LOG_CRIT,
2443 "cupsdUpdateCGI: error reading from CGI error pipe - %s",
2444 strerror(errno));
2445 }
2446 }
2447
2448
2449 /*
2450 * 'cupsdWriteClient()' - Write data to a client as needed.
2451 */
2452
2453 void
2454 cupsdWriteClient(cupsd_client_t *con) /* I - Client connection */
2455 {
2456 int bytes, /* Number of bytes written */
2457 field_col; /* Current column */
2458 char *bufptr, /* Pointer into buffer */
2459 *bufend; /* Pointer to end of buffer */
2460 ipp_state_t ipp_state; /* IPP state value */
2461
2462
2463 cupsdLogClient(con, CUPSD_LOG_DEBUG, "con->http=%p", con->http);
2464 cupsdLogClient(con, CUPSD_LOG_DEBUG,
2465 "cupsdWriteClient "
2466 "error=%d, "
2467 "used=%d, "
2468 "state=%s, "
2469 "data_encoding=HTTP_ENCODING_%s, "
2470 "data_remaining=" CUPS_LLFMT ", "
2471 "response=%p(%s), "
2472 "pipe_pid=%d, "
2473 "file=%d",
2474 httpError(con->http), (int)httpGetReady(con->http),
2475 httpStateString(httpGetState(con->http)),
2476 httpIsChunked(con->http) ? "CHUNKED" : "LENGTH",
2477 CUPS_LLCAST httpGetLength2(con->http),
2478 con->response,
2479 con->response ? ippStateString(ippGetState(con->request)) : "",
2480 con->pipe_pid, con->file);
2481
2482 if (httpGetState(con->http) != HTTP_STATE_GET_SEND &&
2483 httpGetState(con->http) != HTTP_STATE_POST_SEND)
2484 {
2485 /*
2486 * If we get called in the wrong state, then something went wrong with the
2487 * connection and we need to shut it down...
2488 */
2489
2490 cupsdLogClient(con, CUPSD_LOG_DEBUG, "Closing on unexpected HTTP write state %s.",
2491 httpStateString(httpGetState(con->http)));
2492 cupsdCloseClient(con);
2493 return;
2494 }
2495
2496 if (con->pipe_pid)
2497 {
2498 /*
2499 * Make sure we select on the CGI output...
2500 */
2501
2502 cupsdAddSelect(con->file, (cupsd_selfunc_t)write_pipe, NULL, con);
2503
2504 cupsdLogClient(con, CUPSD_LOG_DEBUG, "Waiting for CGI data.");
2505
2506 if (!con->file_ready)
2507 {
2508 /*
2509 * Try again later when there is CGI output available...
2510 */
2511
2512 cupsdRemoveSelect(httpGetFd(con->http));
2513 return;
2514 }
2515
2516 con->file_ready = 0;
2517 }
2518
2519 bytes = (ssize_t)(sizeof(con->header) - (size_t)con->header_used);
2520
2521 if (!con->pipe_pid && bytes > (ssize_t)httpGetRemaining(con->http))
2522 {
2523 /*
2524 * Limit GET bytes to original size of file (STR #3265)...
2525 */
2526
2527 bytes = (ssize_t)httpGetRemaining(con->http);
2528 }
2529
2530 if (con->response && con->response->state != IPP_STATE_DATA)
2531 {
2532 size_t wused = httpGetPending(con->http); /* Previous write buffer use */
2533
2534 do
2535 {
2536 /*
2537 * Write a single attribute or the IPP message header...
2538 */
2539
2540 ipp_state = ippWrite(con->http, con->response);
2541
2542 /*
2543 * If the write buffer has been flushed, stop buffering up attributes...
2544 */
2545
2546 if (httpGetPending(con->http) <= wused)
2547 break;
2548 }
2549 while (ipp_state != IPP_STATE_DATA && ipp_state != IPP_STATE_ERROR);
2550
2551 cupsdLogClient(con, CUPSD_LOG_DEBUG,
2552 "Writing IPP response, ipp_state=%s, old "
2553 "wused=" CUPS_LLFMT ", new wused=" CUPS_LLFMT,
2554 ippStateString(ipp_state),
2555 CUPS_LLCAST wused, CUPS_LLCAST httpGetPending(con->http));
2556
2557 if (httpGetPending(con->http) > 0)
2558 httpFlushWrite(con->http);
2559
2560 bytes = ipp_state != IPP_STATE_ERROR &&
2561 (con->file >= 0 || ipp_state != IPP_STATE_DATA);
2562
2563 cupsdLogClient(con, CUPSD_LOG_DEBUG,
2564 "bytes=%d, http_state=%d, data_remaining=" CUPS_LLFMT,
2565 (int)bytes, httpGetState(con->http),
2566 CUPS_LLCAST httpGetLength2(con->http));
2567 }
2568 else if ((bytes = read(con->file, con->header + con->header_used, (size_t)bytes)) > 0)
2569 {
2570 con->header_used += bytes;
2571
2572 if (con->pipe_pid && !con->got_fields)
2573 {
2574 /*
2575 * Inspect the data for Content-Type and other fields.
2576 */
2577
2578 for (bufptr = con->header, bufend = con->header + con->header_used,
2579 field_col = 0;
2580 !con->got_fields && bufptr < bufend;
2581 bufptr ++)
2582 {
2583 if (*bufptr == '\n')
2584 {
2585 /*
2586 * Send line to client...
2587 */
2588
2589 if (bufptr > con->header && bufptr[-1] == '\r')
2590 bufptr[-1] = '\0';
2591 *bufptr++ = '\0';
2592
2593 cupsdLogClient(con, CUPSD_LOG_DEBUG, "Script header: %s", con->header);
2594
2595 if (!con->sent_header)
2596 {
2597 /*
2598 * Handle redirection and CGI status codes...
2599 */
2600
2601 http_field_t field; /* HTTP field */
2602 char *value = strchr(con->header, ':');
2603 /* Value of field */
2604
2605 if (value)
2606 {
2607 *value++ = '\0';
2608 while (isspace(*value & 255))
2609 value ++;
2610 }
2611
2612 field = httpFieldValue(con->header);
2613
2614 if (field != HTTP_FIELD_UNKNOWN && value)
2615 {
2616 httpSetField(con->http, field, value);
2617
2618 if (field == HTTP_FIELD_LOCATION)
2619 {
2620 con->pipe_status = HTTP_STATUS_SEE_OTHER;
2621 con->sent_header = 2;
2622 }
2623 else
2624 con->sent_header = 1;
2625 }
2626 else if (!_cups_strcasecmp(con->header, "Status") && value)
2627 {
2628 con->pipe_status = (http_status_t)atoi(value);
2629 con->sent_header = 2;
2630 }
2631 else if (!_cups_strcasecmp(con->header, "Set-Cookie") && value)
2632 {
2633 char *sep = strchr(value, ';');
2634 /* Separator between name=value and the rest */
2635
2636 if (sep)
2637 *sep = '\0';
2638
2639 httpSetCookie(con->http, value);
2640 con->sent_header = 1;
2641 }
2642 }
2643
2644 /*
2645 * Update buffer...
2646 */
2647
2648 con->header_used -= bufptr - con->header;
2649
2650 if (con->header_used > 0)
2651 memmove(con->header, bufptr, (size_t)con->header_used);
2652
2653 bufptr = con->header - 1;
2654
2655 /*
2656 * See if the line was empty...
2657 */
2658
2659 if (field_col == 0)
2660 {
2661 con->got_fields = 1;
2662
2663 if (httpGetVersion(con->http) == HTTP_VERSION_1_1 &&
2664 !httpGetField(con->http, HTTP_FIELD_CONTENT_LENGTH)[0])
2665 httpSetLength(con->http, 0);
2666
2667 cupsdLogClient(con, CUPSD_LOG_DEBUG, "Sending status %d for CGI.", con->pipe_status);
2668
2669 if (!cupsdSendHeader(con, con->pipe_status, NULL, CUPSD_AUTH_NONE))
2670 {
2671 cupsdCloseClient(con);
2672 return;
2673 }
2674 }
2675 else
2676 field_col = 0;
2677 }
2678 else if (*bufptr != '\r')
2679 field_col ++;
2680 }
2681
2682 if (!con->got_fields)
2683 return;
2684 }
2685
2686 if (con->header_used > 0)
2687 {
2688 if (httpWrite2(con->http, con->header, (size_t)con->header_used) < 0)
2689 {
2690 cupsdLogClient(con, CUPSD_LOG_DEBUG, "Closing for error %d (%s)",
2691 httpError(con->http), strerror(httpError(con->http)));
2692 cupsdCloseClient(con);
2693 return;
2694 }
2695
2696 if (httpIsChunked(con->http))
2697 httpFlushWrite(con->http);
2698
2699 con->bytes += con->header_used;
2700
2701 if (httpGetState(con->http) == HTTP_STATE_WAITING)
2702 bytes = 0;
2703 else
2704 bytes = con->header_used;
2705
2706 con->header_used = 0;
2707 }
2708 }
2709
2710 if (bytes <= 0 ||
2711 (httpGetState(con->http) != HTTP_STATE_GET_SEND &&
2712 httpGetState(con->http) != HTTP_STATE_POST_SEND))
2713 {
2714 if (!con->sent_header && con->pipe_pid)
2715 cupsdSendError(con, HTTP_STATUS_SERVER_ERROR, CUPSD_AUTH_NONE);
2716 else
2717 {
2718 cupsdLogRequest(con, HTTP_STATUS_OK);
2719
2720 if (httpIsChunked(con->http) && (!con->pipe_pid || con->sent_header > 0))
2721 {
2722 cupsdLogClient(con, CUPSD_LOG_DEBUG, "Sending 0-length chunk.");
2723
2724 if (httpWrite2(con->http, "", 0) < 0)
2725 {
2726 cupsdLogClient(con, CUPSD_LOG_DEBUG, "Closing for error %d (%s)",
2727 httpError(con->http), strerror(httpError(con->http)));
2728 cupsdCloseClient(con);
2729 return;
2730 }
2731 }
2732
2733 cupsdLogClient(con, CUPSD_LOG_DEBUG, "Flushing write buffer.");
2734 httpFlushWrite(con->http);
2735 cupsdLogClient(con, CUPSD_LOG_DEBUG, "New state is %s", httpStateString(httpGetState(con->http)));
2736 }
2737
2738 cupsdAddSelect(httpGetFd(con->http), (cupsd_selfunc_t)cupsdReadClient, NULL, con);
2739
2740 cupsdLogClient(con, CUPSD_LOG_DEBUG, "Waiting for request.");
2741
2742 if (con->file >= 0)
2743 {
2744 cupsdRemoveSelect(con->file);
2745
2746 if (con->pipe_pid)
2747 cupsdEndProcess(con->pipe_pid, 0);
2748
2749 close(con->file);
2750 con->file = -1;
2751 con->pipe_pid = 0;
2752 }
2753
2754 if (con->filename)
2755 {
2756 unlink(con->filename);
2757 cupsdClearString(&con->filename);
2758 }
2759
2760 if (con->request)
2761 {
2762 ippDelete(con->request);
2763 con->request = NULL;
2764 }
2765
2766 if (con->response)
2767 {
2768 ippDelete(con->response);
2769 con->response = NULL;
2770 }
2771
2772 cupsdClearString(&con->command);
2773 cupsdClearString(&con->options);
2774 cupsdClearString(&con->query_string);
2775
2776 if (!httpGetKeepAlive(con->http))
2777 {
2778 cupsdLogClient(con, CUPSD_LOG_DEBUG,
2779 "Closing because Keep-Alive is disabled.");
2780 cupsdCloseClient(con);
2781 return;
2782 }
2783 else
2784 {
2785 cupsArrayRemove(ActiveClients, con);
2786 cupsdSetBusyState();
2787 }
2788 }
2789 }
2790
2791
2792 /*
2793 * 'check_if_modified()' - Decode an "If-Modified-Since" line.
2794 */
2795
2796 static int /* O - 1 if modified since */
2797 check_if_modified(
2798 cupsd_client_t *con, /* I - Client connection */
2799 struct stat *filestats) /* I - File information */
2800 {
2801 const char *ptr; /* Pointer into field */
2802 time_t date; /* Time/date value */
2803 off_t size; /* Size/length value */
2804
2805
2806 size = 0;
2807 date = 0;
2808 ptr = httpGetField(con->http, HTTP_FIELD_IF_MODIFIED_SINCE);
2809
2810 if (*ptr == '\0')
2811 return (1);
2812
2813 cupsdLogClient(con, CUPSD_LOG_DEBUG2,
2814 "check_if_modified "
2815 "filestats=%p(" CUPS_LLFMT ", %d)) If-Modified-Since=\"%s\"",
2816 filestats, CUPS_LLCAST filestats->st_size,
2817 (int)filestats->st_mtime, ptr);
2818
2819 while (*ptr != '\0')
2820 {
2821 while (isspace(*ptr) || *ptr == ';')
2822 ptr ++;
2823
2824 if (_cups_strncasecmp(ptr, "length=", 7) == 0)
2825 {
2826 ptr += 7;
2827 size = strtoll(ptr, NULL, 10);
2828
2829 while (isdigit(*ptr))
2830 ptr ++;
2831 }
2832 else if (isalpha(*ptr))
2833 {
2834 date = httpGetDateTime(ptr);
2835 while (*ptr != '\0' && *ptr != ';')
2836 ptr ++;
2837 }
2838 else
2839 ptr ++;
2840 }
2841
2842 return ((size != filestats->st_size && size != 0) ||
2843 (date < filestats->st_mtime && date != 0) ||
2844 (size == 0 && date == 0));
2845 }
2846
2847
2848 /*
2849 * 'compare_clients()' - Compare two client connections.
2850 */
2851
2852 static int /* O - Result of comparison */
2853 compare_clients(cupsd_client_t *a, /* I - First client */
2854 cupsd_client_t *b, /* I - Second client */
2855 void *data) /* I - User data (not used) */
2856 {
2857 (void)data;
2858
2859 if (a == b)
2860 return (0);
2861 else if (a < b)
2862 return (-1);
2863 else
2864 return (1);
2865 }
2866
2867
2868 #ifdef HAVE_SSL
2869 /*
2870 * 'cupsd_start_tls()' - Start encryption on a connection.
2871 */
2872
2873 static int /* O - 0 on success, -1 on error */
2874 cupsd_start_tls(cupsd_client_t *con, /* I - Client connection */
2875 http_encryption_t e) /* I - Encryption mode */
2876 {
2877 if (httpEncryption(con->http, e))
2878 {
2879 cupsdLogClient(con, CUPSD_LOG_ERROR, "Unable to encrypt connection: %s",
2880 cupsLastErrorString());
2881 return (-1);
2882 }
2883
2884 cupsdLogClient(con, CUPSD_LOG_INFO, "Connection now encrypted.");
2885 return (0);
2886 }
2887 #endif /* HAVE_SSL */
2888
2889
2890 /*
2891 * 'get_file()' - Get a filename and state info.
2892 */
2893
2894 static char * /* O - Real filename */
2895 get_file(cupsd_client_t *con, /* I - Client connection */
2896 struct stat *filestats, /* O - File information */
2897 char *filename, /* IO - Filename buffer */
2898 size_t len) /* I - Buffer length */
2899 {
2900 int status; /* Status of filesystem calls */
2901 char *ptr; /* Pointer info filename */
2902 size_t plen; /* Remaining length after pointer */
2903 char language[7]; /* Language subdirectory, if any */
2904
2905
2906 /*
2907 * Figure out the real filename...
2908 */
2909
2910 language[0] = '\0';
2911
2912 if (!strncmp(con->uri, "/ppd/", 5) && !strchr(con->uri + 5, '/'))
2913 snprintf(filename, len, "%s%s", ServerRoot, con->uri);
2914 else if (!strncmp(con->uri, "/icons/", 7) && !strchr(con->uri + 7, '/'))
2915 {
2916 snprintf(filename, len, "%s/%s", CacheDir, con->uri + 7);
2917 if (access(filename, F_OK) < 0)
2918 snprintf(filename, len, "%s/images/generic.png", DocumentRoot);
2919 }
2920 else if (!strncmp(con->uri, "/rss/", 5) && !strchr(con->uri + 5, '/'))
2921 snprintf(filename, len, "%s/rss/%s", CacheDir, con->uri + 5);
2922 else if (!strncmp(con->uri, "/admin/conf/", 12))
2923 snprintf(filename, len, "%s%s", ServerRoot, con->uri + 11);
2924 else if (!strncmp(con->uri, "/admin/log/", 11))
2925 {
2926 if (!strncmp(con->uri + 11, "access_log", 10) && AccessLog[0] == '/')
2927 strlcpy(filename, AccessLog, len);
2928 else if (!strncmp(con->uri + 11, "error_log", 9) && ErrorLog[0] == '/')
2929 strlcpy(filename, ErrorLog, len);
2930 else if (!strncmp(con->uri + 11, "page_log", 8) && PageLog[0] == '/')
2931 strlcpy(filename, PageLog, len);
2932 else
2933 return (NULL);
2934 }
2935 else if (con->language)
2936 {
2937 snprintf(language, sizeof(language), "/%s", con->language->language);
2938 snprintf(filename, len, "%s%s%s", DocumentRoot, language, con->uri);
2939 }
2940 else
2941 snprintf(filename, len, "%s%s", DocumentRoot, con->uri);
2942
2943 if ((ptr = strchr(filename, '?')) != NULL)
2944 *ptr = '\0';
2945
2946 /*
2947 * Grab the status for this language; if there isn't a language-specific file
2948 * then fallback to the default one...
2949 */
2950
2951 if ((status = stat(filename, filestats)) != 0 && language[0] &&
2952 strncmp(con->uri, "/icons/", 7) &&
2953 strncmp(con->uri, "/ppd/", 5) &&
2954 strncmp(con->uri, "/rss/", 5) &&
2955 strncmp(con->uri, "/admin/conf/", 12) &&
2956 strncmp(con->uri, "/admin/log/", 11))
2957 {
2958 /*
2959 * Drop the country code...
2960 */
2961
2962 language[3] = '\0';
2963 snprintf(filename, len, "%s%s%s", DocumentRoot, language, con->uri);
2964
2965 if ((ptr = strchr(filename, '?')) != NULL)
2966 *ptr = '\0';
2967
2968 if ((status = lstat(filename, filestats)) != 0)
2969 {
2970 /*
2971 * Drop the language prefix and try the root directory...
2972 */
2973
2974 language[0] = '\0';
2975 snprintf(filename, len, "%s%s", DocumentRoot, con->uri);
2976
2977 if ((ptr = strchr(filename, '?')) != NULL)
2978 *ptr = '\0';
2979
2980 status = lstat(filename, filestats);
2981 }
2982 }
2983
2984 /*
2985 * If we've found a symlink, 404 the sucker to avoid disclosing information.
2986 */
2987
2988 if (!status && S_ISLNK(filestats->st_mode))
2989 {
2990 cupsdLogClient(con, CUPSD_LOG_INFO, "Symlinks such as \"%s\" are not allowed.", filename);
2991 return (NULL);
2992 }
2993
2994 /*
2995 * Similarly, if the file/directory does not have world read permissions, do
2996 * not allow access...
2997 */
2998
2999 if (!status && !(filestats->st_mode & S_IROTH))
3000 {
3001 cupsdLogClient(con, CUPSD_LOG_INFO, "Files/directories such as \"%s\" must be world-readable.", filename);
3002 return (NULL);
3003 }
3004
3005 /*
3006 * If we've found a directory, get the index.html file instead...
3007 */
3008
3009 if (!status && S_ISDIR(filestats->st_mode))
3010 {
3011 /*
3012 * Make sure the URI ends with a slash...
3013 */
3014
3015 if (con->uri[strlen(con->uri) - 1] != '/')
3016 strlcat(con->uri, "/", sizeof(con->uri));
3017
3018 /*
3019 * Find the directory index file, trying every language...
3020 */
3021
3022 do
3023 {
3024 if (status && language[0])
3025 {
3026 /*
3027 * Try a different language subset...
3028 */
3029
3030 if (language[3])
3031 language[0] = '\0'; /* Strip country code */
3032 else
3033 language[0] = '\0'; /* Strip language */
3034 }
3035
3036 /*
3037 * Look for the index file...
3038 */
3039
3040 snprintf(filename, len, "%s%s%s", DocumentRoot, language, con->uri);
3041
3042 if ((ptr = strchr(filename, '?')) != NULL)
3043 *ptr = '\0';
3044
3045 ptr = filename + strlen(filename);
3046 plen = len - (size_t)(ptr - filename);
3047
3048 strlcpy(ptr, "index.html", plen);
3049 status = stat(filename, filestats);
3050
3051 #ifdef HAVE_JAVA
3052 if (status)
3053 {
3054 strlcpy(ptr, "index.class", plen);
3055 status = stat(filename, filestats);
3056 }
3057 #endif /* HAVE_JAVA */
3058
3059 #ifdef HAVE_PERL
3060 if (status)
3061 {
3062 strlcpy(ptr, "index.pl", plen);
3063 status = stat(filename, filestats);
3064 }
3065 #endif /* HAVE_PERL */
3066
3067 #ifdef HAVE_PHP
3068 if (status)
3069 {
3070 strlcpy(ptr, "index.php", plen);
3071 status = stat(filename, filestats);
3072 }
3073 #endif /* HAVE_PHP */
3074
3075 #ifdef HAVE_PYTHON
3076 if (status)
3077 {
3078 strlcpy(ptr, "index.pyc", plen);
3079 status = stat(filename, filestats);
3080 }
3081
3082 if (status)
3083 {
3084 strlcpy(ptr, "index.py", plen);
3085 status = stat(filename, filestats);
3086 }
3087 #endif /* HAVE_PYTHON */
3088
3089 }
3090 while (status && language[0]);
3091 }
3092
3093 cupsdLogClient(con, CUPSD_LOG_DEBUG2, "get_file filestats=%p, filename=%p, len=" CUPS_LLFMT ", returning \"%s\".", filestats, filename, CUPS_LLCAST len, status ? "(null)" : filename);
3094
3095 if (status)
3096 return (NULL);
3097 else
3098 return (filename);
3099 }
3100
3101
3102 /*
3103 * 'install_cupsd_conf()' - Install a configuration file.
3104 */
3105
3106 static http_status_t /* O - Status */
3107 install_cupsd_conf(cupsd_client_t *con) /* I - Connection */
3108 {
3109 char filename[1024]; /* Configuration filename */
3110 cups_file_t *in, /* Input file */
3111 *out; /* Output file */
3112 char buffer[16384]; /* Copy buffer */
3113 ssize_t bytes; /* Number of bytes */
3114
3115
3116 /*
3117 * Open the request file...
3118 */
3119
3120 if ((in = cupsFileOpen(con->filename, "rb")) == NULL)
3121 {
3122 cupsdLogClient(con, CUPSD_LOG_ERROR, "Unable to open request file \"%s\": %s",
3123 con->filename, strerror(errno));
3124 return (HTTP_STATUS_SERVER_ERROR);
3125 }
3126
3127 /*
3128 * Open the new config file...
3129 */
3130
3131 if ((out = cupsdCreateConfFile(ConfigurationFile, ConfigFilePerm)) == NULL)
3132 {
3133 cupsFileClose(in);
3134 return (HTTP_STATUS_SERVER_ERROR);
3135 }
3136
3137 cupsdLogClient(con, CUPSD_LOG_INFO, "Installing config file \"%s\"...",
3138 ConfigurationFile);
3139
3140 /*
3141 * Copy from the request to the new config file...
3142 */
3143
3144 while ((bytes = cupsFileRead(in, buffer, sizeof(buffer))) > 0)
3145 if (cupsFileWrite(out, buffer, (size_t)bytes) < bytes)
3146 {
3147 cupsdLogClient(con, CUPSD_LOG_ERROR,
3148 "Unable to copy to config file \"%s\": %s",
3149 ConfigurationFile, strerror(errno));
3150
3151 cupsFileClose(in);
3152 cupsFileClose(out);
3153
3154 snprintf(filename, sizeof(filename), "%s.N", ConfigurationFile);
3155 cupsdUnlinkOrRemoveFile(filename);
3156
3157 return (HTTP_STATUS_SERVER_ERROR);
3158 }
3159
3160 /*
3161 * Close the files...
3162 */
3163
3164 cupsFileClose(in);
3165
3166 if (cupsdCloseCreatedConfFile(out, ConfigurationFile))
3167 return (HTTP_STATUS_SERVER_ERROR);
3168
3169 /*
3170 * Remove the request file...
3171 */
3172
3173 cupsdUnlinkOrRemoveFile(con->filename);
3174 cupsdClearString(&con->filename);
3175
3176 /*
3177 * Set the NeedReload flag...
3178 */
3179
3180 NeedReload = RELOAD_CUPSD;
3181 ReloadTime = time(NULL);
3182
3183 /*
3184 * Return that the file was created successfully...
3185 */
3186
3187 return (HTTP_STATUS_CREATED);
3188 }
3189
3190
3191 /*
3192 * 'is_cgi()' - Is the resource a CGI script/program?
3193 */
3194
3195 static int /* O - 1 = CGI, 0 = file */
3196 is_cgi(cupsd_client_t *con, /* I - Client connection */
3197 const char *filename, /* I - Real filename */
3198 struct stat *filestats, /* I - File information */
3199 mime_type_t *type) /* I - MIME type */
3200 {
3201 const char *options; /* Options on URL */
3202
3203
3204 /*
3205 * Get the options, if any...
3206 */
3207
3208 if ((options = strchr(con->uri, '?')) != NULL)
3209 {
3210 options ++;
3211 cupsdSetStringf(&(con->query_string), "QUERY_STRING=%s", options);
3212 }
3213
3214 /*
3215 * Check for known types...
3216 */
3217
3218 if (!type || _cups_strcasecmp(type->super, "application"))
3219 {
3220 cupsdLogClient(con, CUPSD_LOG_DEBUG2,
3221 "is_cgi filename=\"%s\", filestats=%p, "
3222 "type=%s/%s, returning 0", filename,
3223 filestats, type ? type->super : "unknown",
3224 type ? type->type : "unknown");
3225 return (0);
3226 }
3227
3228 if (!_cups_strcasecmp(type->type, "x-httpd-cgi") &&
3229 (filestats->st_mode & 0111))
3230 {
3231 /*
3232 * "application/x-httpd-cgi" is a CGI script.
3233 */
3234
3235 cupsdSetString(&con->command, filename);
3236
3237 if (options)
3238 cupsdSetStringf(&con->options, " %s", options);
3239
3240 cupsdLogClient(con, CUPSD_LOG_DEBUG2,
3241 "is_cgi filename=\"%s\", filestats=%p, "
3242 "type=%s/%s, returning 1", filename,
3243 filestats, type->super, type->type);
3244 return (1);
3245 }
3246 #ifdef HAVE_JAVA
3247 else if (!_cups_strcasecmp(type->type, "x-httpd-java"))
3248 {
3249 /*
3250 * "application/x-httpd-java" is a Java servlet.
3251 */
3252
3253 cupsdSetString(&con->command, CUPS_JAVA);
3254
3255 if (options)
3256 cupsdSetStringf(&con->options, " %s %s", filename, options);
3257 else
3258 cupsdSetStringf(&con->options, " %s", filename);
3259
3260 cupsdLogClient(con, CUPSD_LOG_DEBUG2,
3261 "is_cgi filename=\"%s\", filestats=%p, "
3262 "type=%s/%s, returning 1", filename,
3263 filestats, type->super, type->type);
3264 return (1);
3265 }
3266 #endif /* HAVE_JAVA */
3267 #ifdef HAVE_PERL
3268 else if (!_cups_strcasecmp(type->type, "x-httpd-perl"))
3269 {
3270 /*
3271 * "application/x-httpd-perl" is a Perl page.
3272 */
3273
3274 cupsdSetString(&con->command, CUPS_PERL);
3275
3276 if (options)
3277 cupsdSetStringf(&con->options, " %s %s", filename, options);
3278 else
3279 cupsdSetStringf(&con->options, " %s", filename);
3280
3281 cupsdLogClient(con, CUPSD_LOG_DEBUG2,
3282 "is_cgi filename=\"%s\", filestats=%p, "
3283 "type=%s/%s, returning 1", filename,
3284 filestats, type->super, type->type);
3285 return (1);
3286 }
3287 #endif /* HAVE_PERL */
3288 #ifdef HAVE_PHP
3289 else if (!_cups_strcasecmp(type->type, "x-httpd-php"))
3290 {
3291 /*
3292 * "application/x-httpd-php" is a PHP page.
3293 */
3294
3295 cupsdSetString(&con->command, CUPS_PHP);
3296
3297 if (options)
3298 cupsdSetStringf(&con->options, " %s %s", filename, options);
3299 else
3300 cupsdSetStringf(&con->options, " %s", filename);
3301
3302 cupsdLogClient(con, CUPSD_LOG_DEBUG2,
3303 "is_cgi filename=\"%s\", filestats=%p, "
3304 "type=%s/%s, returning 1", filename,
3305 filestats, type->super, type->type);
3306 return (1);
3307 }
3308 #endif /* HAVE_PHP */
3309 #ifdef HAVE_PYTHON
3310 else if (!_cups_strcasecmp(type->type, "x-httpd-python"))
3311 {
3312 /*
3313 * "application/x-httpd-python" is a Python page.
3314 */
3315
3316 cupsdSetString(&con->command, CUPS_PYTHON);
3317
3318 if (options)
3319 cupsdSetStringf(&con->options, " %s %s", filename, options);
3320 else
3321 cupsdSetStringf(&con->options, " %s", filename);
3322
3323 cupsdLogClient(con, CUPSD_LOG_DEBUG2,
3324 "is_cgi filename=\"%s\", filestats=%p, "
3325 "type=%s/%s, returning 1", filename,
3326 filestats, type->super, type->type);
3327 return (1);
3328 }
3329 #endif /* HAVE_PYTHON */
3330
3331 cupsdLogClient(con, CUPSD_LOG_DEBUG2,
3332 "is_cgi filename=\"%s\", filestats=%p, "
3333 "type=%s/%s, returning 0", filename,
3334 filestats, type->super, type->type);
3335 return (0);
3336 }
3337
3338
3339 /*
3340 * 'is_path_absolute()' - Is a path absolute and free of relative elements (i.e. "..").
3341 */
3342
3343 static int /* O - 0 if relative, 1 if absolute */
3344 is_path_absolute(const char *path) /* I - Input path */
3345 {
3346 /*
3347 * Check for a leading slash...
3348 */
3349
3350 if (path[0] != '/')
3351 return (0);
3352
3353 /*
3354 * Check for "<" or quotes in the path and reject since this is probably
3355 * someone trying to inject HTML...
3356 */
3357
3358 if (strchr(path, '<') != NULL || strchr(path, '\"') != NULL || strchr(path, '\'') != NULL)
3359 return (0);
3360
3361 /*
3362 * Check for "/.." in the path...
3363 */
3364
3365 while ((path = strstr(path, "/..")) != NULL)
3366 {
3367 if (!path[3] || path[3] == '/')
3368 return (0);
3369
3370 path ++;
3371 }
3372
3373 /*
3374 * If we haven't found any relative paths, return 1 indicating an
3375 * absolute path...
3376 */
3377
3378 return (1);
3379 }
3380
3381
3382 /*
3383 * 'pipe_command()' - Pipe the output of a command to the remote client.
3384 */
3385
3386 static int /* O - Process ID */
3387 pipe_command(cupsd_client_t *con, /* I - Client connection */
3388 int infile, /* I - Standard input for command */
3389 int *outfile, /* O - Standard output for command */
3390 char *command, /* I - Command to run */
3391 char *options, /* I - Options for command */
3392 int root) /* I - Run as root? */
3393 {
3394 int i; /* Looping var */
3395 int pid; /* Process ID */
3396 char *commptr, /* Command string pointer */
3397 commch; /* Command string character */
3398 char *uriptr; /* URI string pointer */
3399 int fds[2]; /* Pipe FDs */
3400 int argc; /* Number of arguments */
3401 int envc; /* Number of environment variables */
3402 char argbuf[10240], /* Argument buffer */
3403 *argv[100], /* Argument strings */
3404 *envp[MAX_ENV + 20]; /* Environment variables */
3405 char auth_type[256], /* AUTH_TYPE environment variable */
3406 content_length[1024], /* CONTENT_LENGTH environment variable */
3407 content_type[1024], /* CONTENT_TYPE environment variable */
3408 http_cookie[32768], /* HTTP_COOKIE environment variable */
3409 http_referer[1024], /* HTTP_REFERER environment variable */
3410 http_user_agent[1024], /* HTTP_USER_AGENT environment variable */
3411 lang[1024], /* LANG environment variable */
3412 path_info[1024], /* PATH_INFO environment variable */
3413 remote_addr[1024], /* REMOTE_ADDR environment variable */
3414 remote_host[1024], /* REMOTE_HOST environment variable */
3415 remote_user[1024], /* REMOTE_USER environment variable */
3416 script_filename[1024], /* SCRIPT_FILENAME environment variable */
3417 script_name[1024], /* SCRIPT_NAME environment variable */
3418 server_name[1024], /* SERVER_NAME environment variable */
3419 server_port[1024]; /* SERVER_PORT environment variable */
3420 ipp_attribute_t *attr; /* attributes-natural-language attribute */
3421
3422
3423 /*
3424 * Parse a copy of the options string, which is of the form:
3425 *
3426 * argument+argument+argument
3427 * ?argument+argument+argument
3428 * param=value&param=value
3429 * ?param=value&param=value
3430 * /name?argument+argument+argument
3431 * /name?param=value&param=value
3432 *
3433 * If the string contains an "=" character after the initial name,
3434 * then we treat it as a HTTP GET form request and make a copy of
3435 * the remaining string for the environment variable.
3436 *
3437 * The string is always parsed out as command-line arguments, to
3438 * be consistent with Apache...
3439 */
3440
3441 cupsdLogClient(con, CUPSD_LOG_DEBUG2,
3442 "pipe_command infile=%d, outfile=%p, "
3443 "command=\"%s\", options=\"%s\", root=%d",
3444 infile, outfile, command,
3445 options ? options : "(null)", root);
3446
3447 argv[0] = command;
3448
3449 if (options)
3450 {
3451 commptr = options;
3452 if (*commptr == ' ')
3453 commptr ++;
3454 strlcpy(argbuf, commptr, sizeof(argbuf));
3455 }
3456 else
3457 argbuf[0] = '\0';
3458
3459 if (argbuf[0] == '/')
3460 {
3461 /*
3462 * Found some trailing path information, set PATH_INFO...
3463 */
3464
3465 if ((commptr = strchr(argbuf, '?')) == NULL)
3466 commptr = argbuf + strlen(argbuf);
3467
3468 commch = *commptr;
3469 *commptr = '\0';
3470 snprintf(path_info, sizeof(path_info), "PATH_INFO=%s", argbuf);
3471 *commptr = commch;
3472 }
3473 else
3474 {
3475 commptr = argbuf;
3476 path_info[0] = '\0';
3477
3478 if (*commptr == ' ')
3479 commptr ++;
3480 }
3481
3482 if (*commptr == '?' && con->operation == HTTP_STATE_GET && !con->query_string)
3483 {
3484 commptr ++;
3485 cupsdSetStringf(&(con->query_string), "QUERY_STRING=%s", commptr);
3486 }
3487
3488 argc = 1;
3489
3490 if (*commptr)
3491 {
3492 argv[argc ++] = commptr;
3493
3494 for (; *commptr && argc < 99; commptr ++)
3495 {
3496 /*
3497 * Break arguments whenever we see a + or space...
3498 */
3499
3500 if (*commptr == ' ' || *commptr == '+')
3501 {
3502 while (*commptr == ' ' || *commptr == '+')
3503 *commptr++ = '\0';
3504
3505 /*
3506 * If we don't have a blank string, save it as another argument...
3507 */
3508
3509 if (*commptr)
3510 {
3511 argv[argc] = commptr;
3512 argc ++;
3513 }
3514 else
3515 break;
3516 }
3517 else if (*commptr == '%' && isxdigit(commptr[1] & 255) &&
3518 isxdigit(commptr[2] & 255))
3519 {
3520 /*
3521 * Convert the %xx notation to the individual character.
3522 */
3523
3524 if (commptr[1] >= '0' && commptr[1] <= '9')
3525 *commptr = (char)((commptr[1] - '0') << 4);
3526 else
3527 *commptr = (char)((tolower(commptr[1]) - 'a' + 10) << 4);
3528
3529 if (commptr[2] >= '0' && commptr[2] <= '9')
3530 *commptr |= commptr[2] - '0';
3531 else
3532 *commptr |= tolower(commptr[2]) - 'a' + 10;
3533
3534 _cups_strcpy(commptr + 1, commptr + 3);
3535
3536 /*
3537 * Check for a %00 and break if that is the case...
3538 */
3539
3540 if (!*commptr)
3541 break;
3542 }
3543 }
3544 }
3545
3546 argv[argc] = NULL;
3547
3548 /*
3549 * Setup the environment variables as needed...
3550 */
3551
3552 if (con->username[0])
3553 {
3554 snprintf(auth_type, sizeof(auth_type), "AUTH_TYPE=%s",
3555 httpGetField(con->http, HTTP_FIELD_AUTHORIZATION));
3556
3557 if ((uriptr = strchr(auth_type + 10, ' ')) != NULL)
3558 *uriptr = '\0';
3559 }
3560 else
3561 auth_type[0] = '\0';
3562
3563 if (con->request &&
3564 (attr = ippFindAttribute(con->request, "attributes-natural-language",
3565 IPP_TAG_LANGUAGE)) != NULL)
3566 {
3567 switch (strlen(attr->values[0].string.text))
3568 {
3569 default :
3570 /*
3571 * This is an unknown or badly formatted language code; use
3572 * the POSIX locale...
3573 */
3574
3575 strlcpy(lang, "LANG=C", sizeof(lang));
3576 break;
3577
3578 case 2 :
3579 /*
3580 * Just the language code (ll)...
3581 */
3582
3583 snprintf(lang, sizeof(lang), "LANG=%s.UTF8",
3584 attr->values[0].string.text);
3585 break;
3586
3587 case 5 :
3588 /*
3589 * Language and country code (ll-cc)...
3590 */
3591
3592 snprintf(lang, sizeof(lang), "LANG=%c%c_%c%c.UTF8",
3593 attr->values[0].string.text[0],
3594 attr->values[0].string.text[1],
3595 toupper(attr->values[0].string.text[3] & 255),
3596 toupper(attr->values[0].string.text[4] & 255));
3597 break;
3598 }
3599 }
3600 else if (con->language)
3601 snprintf(lang, sizeof(lang), "LANG=%s.UTF8", con->language->language);
3602 else
3603 strlcpy(lang, "LANG=C", sizeof(lang));
3604
3605 strlcpy(remote_addr, "REMOTE_ADDR=", sizeof(remote_addr));
3606 httpAddrString(httpGetAddress(con->http), remote_addr + 12,
3607 sizeof(remote_addr) - 12);
3608
3609 snprintf(remote_host, sizeof(remote_host), "REMOTE_HOST=%s",
3610 httpGetHostname(con->http, NULL, 0));
3611
3612 snprintf(script_name, sizeof(script_name), "SCRIPT_NAME=%s", con->uri);
3613 if ((uriptr = strchr(script_name, '?')) != NULL)
3614 *uriptr = '\0';
3615
3616 snprintf(script_filename, sizeof(script_filename), "SCRIPT_FILENAME=%s%s",
3617 DocumentRoot, script_name + 12);
3618
3619 sprintf(server_port, "SERVER_PORT=%d", con->serverport);
3620
3621 if (httpGetField(con->http, HTTP_FIELD_HOST)[0])
3622 {
3623 char *nameptr; /* Pointer to ":port" */
3624
3625 snprintf(server_name, sizeof(server_name), "SERVER_NAME=%s",
3626 httpGetField(con->http, HTTP_FIELD_HOST));
3627 if ((nameptr = strrchr(server_name, ':')) != NULL && !strchr(nameptr, ']'))
3628 *nameptr = '\0'; /* Strip trailing ":port" */
3629 }
3630 else
3631 snprintf(server_name, sizeof(server_name), "SERVER_NAME=%s",
3632 con->servername);
3633
3634 envc = cupsdLoadEnv(envp, (int)(sizeof(envp) / sizeof(envp[0])));
3635
3636 if (auth_type[0])
3637 envp[envc ++] = auth_type;
3638
3639 envp[envc ++] = lang;
3640 envp[envc ++] = "REDIRECT_STATUS=1";
3641 envp[envc ++] = "GATEWAY_INTERFACE=CGI/1.1";
3642 envp[envc ++] = server_name;
3643 envp[envc ++] = server_port;
3644 envp[envc ++] = remote_addr;
3645 envp[envc ++] = remote_host;
3646 envp[envc ++] = script_name;
3647 envp[envc ++] = script_filename;
3648
3649 if (path_info[0])
3650 envp[envc ++] = path_info;
3651
3652 if (con->username[0])
3653 {
3654 snprintf(remote_user, sizeof(remote_user), "REMOTE_USER=%s", con->username);
3655
3656 envp[envc ++] = remote_user;
3657 }
3658
3659 if (httpGetVersion(con->http) == HTTP_VERSION_1_1)
3660 envp[envc ++] = "SERVER_PROTOCOL=HTTP/1.1";
3661 else if (httpGetVersion(con->http) == HTTP_VERSION_1_0)
3662 envp[envc ++] = "SERVER_PROTOCOL=HTTP/1.0";
3663 else
3664 envp[envc ++] = "SERVER_PROTOCOL=HTTP/0.9";
3665
3666 if (httpGetCookie(con->http))
3667 {
3668 snprintf(http_cookie, sizeof(http_cookie), "HTTP_COOKIE=%s",
3669 httpGetCookie(con->http));
3670 envp[envc ++] = http_cookie;
3671 }
3672
3673 if (httpGetField(con->http, HTTP_FIELD_USER_AGENT)[0])
3674 {
3675 snprintf(http_user_agent, sizeof(http_user_agent), "HTTP_USER_AGENT=%s",
3676 httpGetField(con->http, HTTP_FIELD_USER_AGENT));
3677 envp[envc ++] = http_user_agent;
3678 }
3679
3680 if (httpGetField(con->http, HTTP_FIELD_REFERER)[0])
3681 {
3682 snprintf(http_referer, sizeof(http_referer), "HTTP_REFERER=%s",
3683 httpGetField(con->http, HTTP_FIELD_REFERER));
3684 envp[envc ++] = http_referer;
3685 }
3686
3687 if (con->operation == HTTP_STATE_GET)
3688 {
3689 envp[envc ++] = "REQUEST_METHOD=GET";
3690
3691 if (con->query_string)
3692 {
3693 /*
3694 * Add GET form variables after ?...
3695 */
3696
3697 envp[envc ++] = con->query_string;
3698 }
3699 else
3700 envp[envc ++] = "QUERY_STRING=";
3701 }
3702 else
3703 {
3704 sprintf(content_length, "CONTENT_LENGTH=" CUPS_LLFMT,
3705 CUPS_LLCAST con->bytes);
3706 snprintf(content_type, sizeof(content_type), "CONTENT_TYPE=%s",
3707 httpGetField(con->http, HTTP_FIELD_CONTENT_TYPE));
3708
3709 envp[envc ++] = "REQUEST_METHOD=POST";
3710 envp[envc ++] = content_length;
3711 envp[envc ++] = content_type;
3712 }
3713
3714 /*
3715 * Tell the CGI if we are using encryption...
3716 */
3717
3718 if (httpIsEncrypted(con->http))
3719 envp[envc ++] = "HTTPS=ON";
3720
3721 /*
3722 * Terminate the environment array...
3723 */
3724
3725 envp[envc] = NULL;
3726
3727 if (LogLevel >= CUPSD_LOG_DEBUG)
3728 {
3729 for (i = 0; i < argc; i ++)
3730 cupsdLogMessage(CUPSD_LOG_DEBUG,
3731 "[CGI] argv[%d] = \"%s\"", i, argv[i]);
3732 for (i = 0; i < envc; i ++)
3733 cupsdLogMessage(CUPSD_LOG_DEBUG,
3734 "[CGI] envp[%d] = \"%s\"", i, envp[i]);
3735 }
3736
3737 /*
3738 * Create a pipe for the output...
3739 */
3740
3741 if (cupsdOpenPipe(fds))
3742 {
3743 cupsdLogMessage(CUPSD_LOG_ERROR, "[CGI] Unable to create pipe for %s - %s",
3744 argv[0], strerror(errno));
3745 return (0);
3746 }
3747
3748 /*
3749 * Then execute the command...
3750 */
3751
3752 if (cupsdStartProcess(command, argv, envp, infile, fds[1], CGIPipes[1],
3753 -1, -1, root, DefaultProfile, NULL, &pid) < 0)
3754 {
3755 /*
3756 * Error - can't fork!
3757 */
3758
3759 cupsdLogMessage(CUPSD_LOG_ERROR, "[CGI] Unable to start %s - %s", argv[0],
3760 strerror(errno));
3761
3762 cupsdClosePipe(fds);
3763 pid = 0;
3764 }
3765 else
3766 {
3767 /*
3768 * Fork successful - return the PID...
3769 */
3770
3771 if (con->username[0])
3772 cupsdAddCert(pid, con->username, con->type);
3773
3774 cupsdLogMessage(CUPSD_LOG_DEBUG, "[CGI] Started %s (PID %d)", command, pid);
3775
3776 *outfile = fds[0];
3777 close(fds[1]);
3778 }
3779
3780 return (pid);
3781 }
3782
3783
3784 /*
3785 * 'valid_host()' - Is the Host: field valid?
3786 */
3787
3788 static int /* O - 1 if valid, 0 if not */
3789 valid_host(cupsd_client_t *con) /* I - Client connection */
3790 {
3791 cupsd_alias_t *a; /* Current alias */
3792 cupsd_netif_t *netif; /* Current network interface */
3793 const char *end; /* End character */
3794 char *ptr; /* Pointer into host value */
3795
3796
3797 /*
3798 * Copy the Host: header for later use...
3799 */
3800
3801 strlcpy(con->clientname, httpGetField(con->http, HTTP_FIELD_HOST),
3802 sizeof(con->clientname));
3803 if ((ptr = strrchr(con->clientname, ':')) != NULL && !strchr(ptr, ']'))
3804 {
3805 *ptr++ = '\0';
3806 con->clientport = atoi(ptr);
3807 }
3808 else
3809 con->clientport = con->serverport;
3810
3811 /*
3812 * Then validate...
3813 */
3814
3815 if (httpAddrLocalhost(httpGetAddress(con->http)))
3816 {
3817 /*
3818 * Only allow "localhost" or the equivalent IPv4 or IPv6 numerical
3819 * addresses when accessing CUPS via the loopback interface...
3820 */
3821
3822 return (!_cups_strcasecmp(con->clientname, "localhost") ||
3823 !_cups_strcasecmp(con->clientname, "localhost.") ||
3824 #ifdef __linux
3825 !_cups_strcasecmp(con->clientname, "localhost.localdomain") ||
3826 #endif /* __linux */
3827 !strcmp(con->clientname, "127.0.0.1") ||
3828 !strcmp(con->clientname, "[::1]"));
3829 }
3830
3831 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
3832 /*
3833 * Check if the hostname is something.local (Bonjour); if so, allow it.
3834 */
3835
3836 if ((end = strrchr(con->clientname, '.')) != NULL && end > con->clientname &&
3837 !end[1])
3838 {
3839 /*
3840 * "." on end, work back to second-to-last "."...
3841 */
3842
3843 for (end --; end > con->clientname && *end != '.'; end --);
3844 }
3845
3846 if (end && (!_cups_strcasecmp(end, ".local") ||
3847 !_cups_strcasecmp(end, ".local.")))
3848 return (1);
3849 #endif /* HAVE_DNSSD || HAVE_AVAHI */
3850
3851 /*
3852 * Check if the hostname is an IP address...
3853 */
3854
3855 if (isdigit(con->clientname[0] & 255) || con->clientname[0] == '[')
3856 {
3857 /*
3858 * Possible IPv4/IPv6 address...
3859 */
3860
3861 http_addrlist_t *addrlist; /* List of addresses */
3862
3863
3864 if ((addrlist = httpAddrGetList(con->clientname, AF_UNSPEC, NULL)) != NULL)
3865 {
3866 /*
3867 * Good IPv4/IPv6 address...
3868 */
3869
3870 httpAddrFreeList(addrlist);
3871 return (1);
3872 }
3873 }
3874
3875 /*
3876 * Check for (alias) name matches...
3877 */
3878
3879 for (a = (cupsd_alias_t *)cupsArrayFirst(ServerAlias);
3880 a;
3881 a = (cupsd_alias_t *)cupsArrayNext(ServerAlias))
3882 {
3883 /*
3884 * "ServerAlias *" allows all host values through...
3885 */
3886
3887 if (!strcmp(a->name, "*"))
3888 return (1);
3889
3890 if (!_cups_strncasecmp(con->clientname, a->name, a->namelen))
3891 {
3892 /*
3893 * Prefix matches; check the character at the end - it must be "." or nul.
3894 */
3895
3896 end = con->clientname + a->namelen;
3897
3898 if (!*end || (*end == '.' && !end[1]))
3899 return (1);
3900 }
3901 }
3902
3903 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
3904 for (a = (cupsd_alias_t *)cupsArrayFirst(DNSSDAlias);
3905 a;
3906 a = (cupsd_alias_t *)cupsArrayNext(DNSSDAlias))
3907 {
3908 /*
3909 * "ServerAlias *" allows all host values through...
3910 */
3911
3912 if (!strcmp(a->name, "*"))
3913 return (1);
3914
3915 if (!_cups_strncasecmp(con->clientname, a->name, a->namelen))
3916 {
3917 /*
3918 * Prefix matches; check the character at the end - it must be "." or nul.
3919 */
3920
3921 end = con->clientname + a->namelen;
3922
3923 if (!*end || (*end == '.' && !end[1]))
3924 return (1);
3925 }
3926 }
3927 #endif /* HAVE_DNSSD || HAVE_AVAHI */
3928
3929 /*
3930 * Check for interface hostname matches...
3931 */
3932
3933 for (netif = (cupsd_netif_t *)cupsArrayFirst(NetIFList);
3934 netif;
3935 netif = (cupsd_netif_t *)cupsArrayNext(NetIFList))
3936 {
3937 if (!_cups_strncasecmp(con->clientname, netif->hostname, netif->hostlen))
3938 {
3939 /*
3940 * Prefix matches; check the character at the end - it must be "." or nul.
3941 */
3942
3943 end = con->clientname + netif->hostlen;
3944
3945 if (!*end || (*end == '.' && !end[1]))
3946 return (1);
3947 }
3948 }
3949
3950 return (0);
3951 }
3952
3953
3954 /*
3955 * 'write_file()' - Send a file via HTTP.
3956 */
3957
3958 static int /* O - 0 on failure, 1 on success */
3959 write_file(cupsd_client_t *con, /* I - Client connection */
3960 http_status_t code, /* I - HTTP status */
3961 char *filename, /* I - Filename */
3962 char *type, /* I - File type */
3963 struct stat *filestats) /* O - File information */
3964 {
3965 con->file = open(filename, O_RDONLY);
3966
3967 cupsdLogClient(con, CUPSD_LOG_DEBUG2,
3968 "write_file code=%d, filename=\"%s\" (%d), "
3969 "type=\"%s\", filestats=%p",
3970 code, filename, con->file, type ? type : "(null)", filestats);
3971
3972 if (con->file < 0)
3973 return (0);
3974
3975 fcntl(con->file, F_SETFD, fcntl(con->file, F_GETFD) | FD_CLOEXEC);
3976
3977 con->pipe_pid = 0;
3978 con->sent_header = 1;
3979
3980 httpClearFields(con->http);
3981
3982 httpSetLength(con->http, (size_t)filestats->st_size);
3983
3984 httpSetField(con->http, HTTP_FIELD_LAST_MODIFIED,
3985 httpGetDateString(filestats->st_mtime));
3986
3987 if (!cupsdSendHeader(con, code, type, CUPSD_AUTH_NONE))
3988 return (0);
3989
3990 cupsdAddSelect(httpGetFd(con->http), NULL, (cupsd_selfunc_t)cupsdWriteClient, con);
3991
3992 cupsdLogClient(con, CUPSD_LOG_DEBUG, "Sending file.");
3993
3994 return (1);
3995 }
3996
3997
3998 /*
3999 * 'write_pipe()' - Flag that data is available on the CGI pipe.
4000 */
4001
4002 static void
4003 write_pipe(cupsd_client_t *con) /* I - Client connection */
4004 {
4005 cupsdLogClient(con, CUPSD_LOG_DEBUG2, "write_pipe CGI output on fd %d",
4006 con->file);
4007
4008 con->file_ready = 1;
4009
4010 cupsdRemoveSelect(con->file);
4011 cupsdAddSelect(httpGetFd(con->http), NULL, (cupsd_selfunc_t)cupsdWriteClient, con);
4012
4013 cupsdLogClient(con, CUPSD_LOG_DEBUG, "CGI data ready to be sent.");
4014 }
4015
4016
4017 /*
4018 * End of "$Id$".
4019 */