]> git.ipfire.org Git - thirdparty/cups.git/blob - scheduler/client.c
Added timestamp to request file to avoid 26 temp files limit under HP-UX.
[thirdparty/cups.git] / scheduler / client.c
1 /*
2 * "$Id: client.c,v 1.44 2000/01/06 15:01:00 mike Exp $"
3 *
4 * Client routines for the Common UNIX Printing System (CUPS) scheduler.
5 *
6 * Copyright 1997-2000 by Easy Software Products, all rights reserved.
7 *
8 * These coded instructions, statements, and computer programs are the
9 * property of Easy Software Products and are protected by Federal
10 * copyright law. Distribution and use rights are outlined in the file
11 * "LICENSE.txt" which should have been included with this file. If this
12 * file is missing or damaged please contact Easy Software Products
13 * at:
14 *
15 * Attn: CUPS Licensing Information
16 * Easy Software Products
17 * 44141 Airport View Drive, Suite 204
18 * Hollywood, Maryland 20636-3111 USA
19 *
20 * Voice: (301) 373-9603
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
23 *
24 * Contents:
25 *
26 * AcceptClient() - Accept a new client.
27 * CloseAllClients() - Close all remote clients immediately.
28 * CloseClient() - Close a remote client.
29 * ReadClient() - Read data from a client.
30 * SendCommand() - Send output from a command via HTTP.
31 * SendError() - Send an error message via HTTP.
32 * SendFile() - Send a file via HTTP.
33 * SendHeader() - Send an HTTP request.
34 * WriteClient() - Write data to a client as needed.
35 * check_if_modified() - Decode an "If-Modified-Since" line.
36 * decode_auth() - Decode an authorization string.
37 * get_file() - Get a filename and state info.
38 * pipe_command() - Pipe the output of a command to the remote client.
39 */
40
41 /*
42 * Include necessary headers...
43 */
44
45 #include "cupsd.h"
46
47
48 /*
49 * Local functions...
50 */
51
52 static int check_if_modified(client_t *con, struct stat *filestats);
53 static void decode_auth(client_t *con);
54 static char *get_file(client_t *con, struct stat *filestats);
55 static int pipe_command(client_t *con, int infile, int *outfile, char *command, char *options);
56
57
58 /*
59 * 'AcceptClient()' - Accept a new client.
60 */
61
62 void
63 AcceptClient(listener_t *lis) /* I - Listener socket */
64 {
65 int i; /* Looping var */
66 int val; /* Parameter value */
67 client_t *con; /* New client pointer */
68 unsigned address;/* Address of client */
69 struct hostent *host; /* Host entry for address */
70
71
72 DEBUG_printf(("AcceptClient(%08x) %d NumClients = %d\n",
73 lis, lis->fd, NumClients));
74
75 /*
76 * Get a pointer to the next available client...
77 */
78
79 con = Clients + NumClients;
80
81 memset(con, 0, sizeof(client_t));
82 con->http.activity = time(NULL);
83
84 /*
85 * Accept the client and get the remote address...
86 */
87
88 val = sizeof(struct sockaddr_in);
89
90 if ((con->http.fd = accept(lis->fd, (struct sockaddr *)&(con->http.hostaddr),
91 &val)) < 0)
92 {
93 LogMessage(LOG_ERROR, "accept() failed - %s.", strerror(errno));
94 return;
95 }
96
97 con->http.hostaddr.sin_port = lis->address.sin_port;
98
99 /*
100 * Get the hostname or format the IP address as needed...
101 */
102
103 address = ntohl(con->http.hostaddr.sin_addr.s_addr);
104
105 if (HostNameLookups)
106 #ifndef __sgi
107 host = gethostbyaddr((char *)&address, sizeof(address), AF_INET);
108 #else
109 host = gethostbyaddr(&address, sizeof(address), AF_INET);
110 #endif /* !__sgi */
111 else
112 host = NULL;
113
114 if (host == NULL)
115 sprintf(con->http.hostname, "%d.%d.%d.%d", (address >> 24) & 255,
116 (address >> 16) & 255, (address >> 8) & 255, address & 255);
117 else
118 strncpy(con->http.hostname, host->h_name, sizeof(con->http.hostname) - 1);
119
120 LogMessage(LOG_DEBUG, "accept() %d from %s:%d.", con->http.fd,
121 con->http.hostname, ntohs(con->http.hostaddr.sin_port));
122
123 /*
124 * Add the socket to the select() input mask.
125 */
126
127 fcntl(con->http.fd, F_SETFD, fcntl(con->http.fd, F_GETFD) | FD_CLOEXEC);
128
129 DEBUG_printf(("AcceptClient: Adding fd %d to InputSet...\n", con->http.fd));
130 FD_SET(con->http.fd, &InputSet);
131
132 NumClients ++;
133
134 /*
135 * Temporarily suspend accept()'s until we lose a client...
136 */
137
138 if (NumClients == MaxClients)
139 for (i = 0; i < NumListeners; i ++)
140 {
141 DEBUG_printf(("AcceptClient: Removing fd %d from InputSet...\n", Listeners[i].fd));
142 FD_CLR(Listeners[i].fd, &InputSet);
143 }
144 }
145
146
147 /*
148 * 'CloseAllClients()' - Close all remote clients immediately.
149 */
150
151 void
152 CloseAllClients(void)
153 {
154 while (NumClients > 0)
155 CloseClient(Clients);
156 }
157
158
159 /*
160 * 'CloseClient()' - Close a remote client.
161 */
162
163 void
164 CloseClient(client_t *con) /* I - Client to close */
165 {
166 int i; /* Looping var */
167 int status; /* Exit status of pipe command */
168
169
170 LogMessage(LOG_DEBUG, "CloseClient() %d", con->http.fd);
171
172 /*
173 * Close the socket and clear the file from the input set for select()...
174 */
175
176 if (con->http.fd > 0)
177 {
178 DEBUG_printf(("CloseClient: Removing fd %d from InputSet...\n", con->http.fd));
179 close(con->http.fd);
180 FD_CLR(con->http.fd, &InputSet);
181 FD_CLR(con->http.fd, &OutputSet);
182 con->http.fd = 0;
183 }
184
185 for (i = 0; i < NumListeners; i ++)
186 {
187 DEBUG_printf(("CloseClient: Adding fd %d to InputSet...\n", Listeners[i].fd));
188 FD_SET(Listeners[i].fd, &InputSet);
189 }
190
191 if (con->pipe_pid != 0)
192 {
193 DEBUG_printf(("CloseClient: Removing fd %d from InputSet...\n", con->file));
194 FD_CLR(con->file, &InputSet);
195 }
196
197 /*
198 * If we have a data file open, close it...
199 */
200
201 if (con->file)
202 {
203 if (con->pipe_pid)
204 {
205 kill(con->pipe_pid, SIGKILL);
206 waitpid(con->pipe_pid, &status, WNOHANG);
207 }
208
209 FD_CLR(con->file, &InputSet);
210 close(con->file);
211 con->file = 0;
212 }
213
214 /*
215 * Compact the list of clients as necessary...
216 */
217
218 NumClients --;
219
220 if (con < (Clients + NumClients))
221 memcpy(con, con + 1, (Clients + NumClients - con) * sizeof(client_t));
222 }
223
224
225 /*
226 * 'ReadClient()' - Read data from a client.
227 */
228
229 int /* O - 1 on success, 0 on error */
230 ReadClient(client_t *con) /* I - Client to read from */
231 {
232 char line[8192], /* Line from client... */
233 operation[64], /* Operation code from socket */
234 version[64]; /* HTTP version number string */
235 int major, minor; /* HTTP version numbers */
236 http_status_t status; /* Transfer status */
237 ipp_state_t ipp_state; /* State of IPP transfer */
238 int bytes; /* Number of bytes to POST */
239 char *filename; /* Name of file for GET/HEAD */
240 struct stat filestats; /* File information */
241 mime_type_t *type; /* MIME type of file */
242 char command[1024], /* Command to run */
243 *options; /* Options/CGI data */
244 printer_t *p; /* Printer */
245
246
247 status = HTTP_CONTINUE;
248
249 switch (con->http.state)
250 {
251 case HTTP_WAITING :
252 /*
253 * See if we've received a request line...
254 */
255
256 if (httpGets(line, sizeof(line) - 1, HTTP(con)) == NULL)
257 {
258 CloseClient(con);
259 return (0);
260 }
261
262 /*
263 * Ignore blank request lines...
264 */
265
266 if (line[0] == '\0')
267 break;
268
269 /*
270 * Clear other state variables...
271 */
272
273 httpClearFields(HTTP(con));
274
275 con->http.activity = time(NULL);
276 con->http.version = HTTP_1_0;
277 con->http.keep_alive = HTTP_KEEPALIVE_OFF;
278 con->http.data_encoding = HTTP_ENCODE_LENGTH;
279 con->http.data_remaining = 0;
280 con->operation = HTTP_WAITING;
281 con->bytes = 0;
282 con->file = 0;
283 con->pipe_pid = 0;
284 con->username[0] = '\0';
285 con->password[0] = '\0';
286 con->uri[0] = '\0';
287
288 if (con->language != NULL)
289 {
290 cupsLangFree(con->language);
291 con->language = NULL;
292 }
293
294 /*
295 * Grab the request line...
296 */
297
298 switch (sscanf(line, "%63s%1023s%63s", operation, con->uri, version))
299 {
300 case 1 :
301 SendError(con, HTTP_BAD_REQUEST);
302 CloseClient(con);
303 return (0);
304 case 2 :
305 con->http.version = HTTP_0_9;
306 break;
307 case 3 :
308 if (sscanf(version, "HTTP/%d.%d", &major, &minor) != 2)
309 {
310 SendError(con, HTTP_BAD_REQUEST);
311 CloseClient(con);
312 return (0);
313 }
314
315 if (major < 2)
316 {
317 con->http.version = (http_version_t)(major * 100 + minor);
318 if (con->http.version == HTTP_1_1)
319 con->http.keep_alive = HTTP_KEEPALIVE_ON;
320 else
321 con->http.keep_alive = HTTP_KEEPALIVE_OFF;
322 }
323 else
324 {
325 SendError(con, HTTP_NOT_SUPPORTED);
326 CloseClient(con);
327 return (0);
328 }
329 break;
330 }
331
332 /*
333 * Process the request...
334 */
335
336 if (strcmp(operation, "GET") == 0)
337 con->http.state = HTTP_GET;
338 else if (strcmp(operation, "PUT") == 0)
339 con->http.state = HTTP_PUT;
340 else if (strcmp(operation, "POST") == 0)
341 con->http.state = HTTP_POST;
342 else if (strcmp(operation, "DELETE") == 0)
343 con->http.state = HTTP_DELETE;
344 else if (strcmp(operation, "TRACE") == 0)
345 con->http.state = HTTP_TRACE;
346 else if (strcmp(operation, "CLOSE") == 0)
347 con->http.state = HTTP_CLOSE;
348 else if (strcmp(operation, "OPTIONS") == 0)
349 con->http.state = HTTP_OPTIONS;
350 else if (strcmp(operation, "HEAD") == 0)
351 con->http.state = HTTP_HEAD;
352 else
353 {
354 SendError(con, HTTP_BAD_REQUEST);
355 CloseClient(con);
356 return (0);
357 }
358
359 con->start = time(NULL);
360 con->operation = con->http.state;
361
362 LogMessage(LOG_DEBUG, "ReadClient() %d %s %s HTTP/%d.%d", con->http.fd,
363 operation, con->uri,
364 con->http.version / 100, con->http.version % 100);
365
366 con->http.status = HTTP_OK;
367 break;
368
369 case HTTP_CLOSE :
370 case HTTP_DELETE :
371 case HTTP_GET :
372 case HTTP_HEAD :
373 case HTTP_POST :
374 case HTTP_PUT :
375 case HTTP_TRACE :
376 /*
377 * Parse incoming parameters until the status changes...
378 */
379
380 status = httpUpdate(HTTP(con));
381
382 if (status != HTTP_OK && status != HTTP_CONTINUE)
383 {
384 SendError(con, HTTP_BAD_REQUEST);
385 CloseClient(con);
386 return (0);
387 }
388 break;
389 }
390
391 /*
392 * Handle new transfers...
393 */
394
395 if (status == HTTP_OK)
396 {
397 con->language = cupsLangGet(con->http.fields[HTTP_FIELD_ACCEPT_LANGUAGE]);
398
399 decode_auth(con);
400
401 if (con->http.fields[HTTP_FIELD_HOST][0] == '\0' &&
402 con->http.version >= HTTP_1_0)
403 {
404 if (!SendError(con, HTTP_BAD_REQUEST))
405 {
406 CloseClient(con);
407 return (0);
408 }
409 }
410 else if (strstr(con->uri, "..") != NULL)
411 {
412 /*
413 * Protect against malicious users!
414 */
415
416 if (!SendError(con, HTTP_FORBIDDEN))
417 {
418 CloseClient(con);
419 return (0);
420 }
421 }
422 else if (con->uri[0] != '/')
423 {
424 /*
425 * Don't allow proxying (yet)...
426 */
427
428 if (!SendError(con, HTTP_METHOD_NOT_ALLOWED))
429 {
430 CloseClient(con);
431 return (0);
432 }
433 }
434 else if ((status = IsAuthorized(con)) != HTTP_OK)
435 {
436 SendError(con, status);
437 CloseClient(con);
438 return (0);
439 }
440 else switch (con->http.state)
441 {
442 case HTTP_GET_SEND :
443 if (strncmp(con->uri, "/printers/", 10) == 0 &&
444 strcmp(con->uri + strlen(con->uri) - 4, ".ppd") == 0)
445 {
446 /*
447 * Send PPD file - get the real printer name since printer
448 * names are not case sensitive but filename can be...
449 */
450
451 con->uri[strlen(con->uri) - 4] = '\0'; /* Drop ".ppd" */
452
453 if ((p = FindPrinter(con->uri + 10)) != NULL)
454 sprintf(con->uri, "/ppd/%s.ppd", p->name);
455 else
456 {
457 if (!SendError(con, HTTP_NOT_FOUND))
458 {
459 CloseClient(con);
460 return (0);
461 }
462
463 break;
464 }
465 }
466
467 if (strncmp(con->uri, "/admin", 6) == 0 ||
468 strncmp(con->uri, "/printers", 9) == 0 ||
469 strncmp(con->uri, "/classes", 8) == 0 ||
470 strncmp(con->uri, "/jobs", 5) == 0)
471 {
472 /*
473 * Send CGI output...
474 */
475
476 if (strncmp(con->uri, "/admin", 6) == 0)
477 {
478 snprintf(command, sizeof(command), "%s/cgi-bin/admin.cgi", ServerBin);
479 options = con->uri + 9;
480 }
481 else if (strncmp(con->uri, "/printers", 9) == 0)
482 {
483 snprintf(command, sizeof(command), "%s/cgi-bin/printers.cgi", ServerBin);
484 options = con->uri + 9;
485 }
486 else if (strncmp(con->uri, "/classes", 8) == 0)
487 {
488 snprintf(command, sizeof(command), "%s/cgi-bin/classes.cgi", ServerBin);
489 options = con->uri + 8;
490 }
491 else
492 {
493 snprintf(command, sizeof(command), "%s/cgi-bin/jobs.cgi", ServerBin);
494 options = con->uri + 5;
495 }
496
497 if (*options == '/')
498 options ++;
499
500 if (!SendCommand(con, command, options))
501 {
502 if (!SendError(con, HTTP_NOT_FOUND))
503 {
504 CloseClient(con);
505 return (0);
506 }
507 }
508 else
509 LogRequest(con, HTTP_OK);
510
511 if (con->http.version <= HTTP_1_0)
512 con->http.keep_alive = HTTP_KEEPALIVE_OFF;
513 }
514 else
515 {
516 /*
517 * Serve a file...
518 */
519
520 if ((filename = get_file(con, &filestats)) == NULL)
521 {
522 if (!SendError(con, HTTP_NOT_FOUND))
523 {
524 CloseClient(con);
525 return (0);
526 }
527 }
528 else if (!check_if_modified(con, &filestats))
529 {
530 if (!SendError(con, HTTP_NOT_MODIFIED))
531 {
532 CloseClient(con);
533 return (0);
534 }
535 }
536 else
537 {
538 type = mimeFileType(MimeDatabase, filename);
539 if (type == NULL)
540 strcpy(line, "text/plain");
541 else
542 sprintf(line, "%s/%s", type->super, type->type);
543
544 if (!SendFile(con, HTTP_OK, filename, line, &filestats))
545 {
546 CloseClient(con);
547 return (0);
548 }
549 }
550 }
551 break;
552
553 case HTTP_POST_RECV :
554 /*
555 * See if the POST request includes a Content-Length field, and if
556 * so check the length against any limits that are set...
557 */
558
559 if (con->http.fields[HTTP_FIELD_CONTENT_LENGTH][0] &&
560 atoi(con->http.fields[HTTP_FIELD_CONTENT_LENGTH]) > MaxRequestSize &&
561 MaxRequestSize > 0)
562 {
563 /*
564 * Request too large...
565 */
566
567 if (!SendError(con, HTTP_REQUEST_TOO_LARGE))
568 {
569 CloseClient(con);
570 return (0);
571 }
572
573 break;
574 }
575
576 /*
577 * See what kind of POST request this is; for IPP requests the
578 * content-type field will be "application/ipp"...
579 */
580
581 if (strcmp(con->http.fields[HTTP_FIELD_CONTENT_TYPE], "application/ipp") == 0)
582 con->request = ippNew();
583 else if (strcmp(con->http.fields[HTTP_FIELD_CONTENT_TYPE], "application/x-www-form-urlencoded") == 0 &&
584 (strncmp(con->uri, "/admin", 6) == 0 ||
585 strncmp(con->uri, "/printers", 9) == 0 ||
586 strncmp(con->uri, "/classes", 8) == 0 ||
587 strncmp(con->uri, "/jobs", 5) == 0))
588 {
589 /*
590 * CGI request...
591 */
592
593 if (strncmp(con->uri, "/admin", 9) == 0)
594 {
595 snprintf(command, sizeof(command), "%s/cgi-bin/admin.cgi", ServerBin);
596 options = con->uri + 9;
597 }
598 else if (strncmp(con->uri, "/printers", 9) == 0)
599 {
600 snprintf(command, sizeof(command), "%s/cgi-bin/printers.cgi", ServerBin);
601 options = con->uri + 9;
602 }
603 else if (strncmp(con->uri, "/classes", 8) == 0)
604 {
605 snprintf(command, sizeof(command), "%s/cgi-bin/classes.cgi", ServerBin);
606 options = con->uri + 8;
607 }
608 else
609 {
610 snprintf(command, sizeof(command), "%s/cgi-bin/jobs.cgi", ServerBin);
611 options = con->uri + 5;
612 }
613
614 if (*options == '/')
615 options ++;
616
617 if (!SendCommand(con, command, options))
618 {
619 if (!SendError(con, HTTP_NOT_FOUND))
620 {
621 CloseClient(con);
622 return (0);
623 }
624 }
625 else
626 LogRequest(con, HTTP_OK);
627
628 if (con->http.version <= HTTP_1_0)
629 con->http.keep_alive = HTTP_KEEPALIVE_OFF;
630 }
631 else if (!SendError(con, HTTP_UNAUTHORIZED))
632 {
633 CloseClient(con);
634 return (0);
635 }
636 break;
637
638 case HTTP_PUT_RECV :
639 case HTTP_DELETE :
640 case HTTP_TRACE :
641 SendError(con, HTTP_NOT_IMPLEMENTED);
642
643 case HTTP_CLOSE :
644 CloseClient(con);
645 return (0);
646
647 case HTTP_HEAD :
648 if (strncmp(con->uri, "/printers", 9) == 0 &&
649 strcmp(con->uri + strlen(con->uri) - 4, ".ppd") == 0)
650 {
651 /*
652 * Send PPD file...
653 */
654
655 snprintf(command, sizeof(command), "/ppd/%s", con->uri + 10);
656 strcpy(con->uri, command);
657 }
658
659 if (strncmp(con->uri, "/admin/", 7) == 0 ||
660 strncmp(con->uri, "/printers/", 10) == 0 ||
661 strncmp(con->uri, "/classes/", 9) == 0 ||
662 strncmp(con->uri, "/jobs/", 6) == 0)
663 {
664 /*
665 * CGI output...
666 */
667
668 if (!SendHeader(con, HTTP_OK, "text/html"))
669 {
670 CloseClient(con);
671 return (0);
672 }
673
674 if (httpPrintf(HTTP(con), "\r\n") < 0)
675 {
676 CloseClient(con);
677 return (0);
678 }
679
680 LogRequest(con, HTTP_OK);
681 }
682 else if ((filename = get_file(con, &filestats)) == NULL)
683 {
684 if (!SendHeader(con, HTTP_NOT_FOUND, "text/html"))
685 {
686 CloseClient(con);
687 return (0);
688 }
689
690 LogRequest(con, HTTP_NOT_FOUND);
691 }
692 else if (!check_if_modified(con, &filestats))
693 {
694 if (!SendError(con, HTTP_NOT_MODIFIED))
695 {
696 CloseClient(con);
697 return (0);
698 }
699
700 LogRequest(con, HTTP_NOT_MODIFIED);
701 }
702 else
703 {
704 /*
705 * Serve a file...
706 */
707
708 type = mimeFileType(MimeDatabase, filename);
709 if (type == NULL)
710 strcpy(line, "text/plain");
711 else
712 sprintf(line, "%s/%s", type->super, type->type);
713
714 if (!SendHeader(con, HTTP_OK, line))
715 {
716 CloseClient(con);
717 return (0);
718 }
719
720 if (httpPrintf(HTTP(con), "Last-Modified: %s\r\n",
721 httpGetDateString(filestats.st_mtime)) < 0)
722 {
723 CloseClient(con);
724 return (0);
725 }
726
727 if (httpPrintf(HTTP(con), "Content-Length: %d\r\n",
728 filestats.st_size) < 0)
729 {
730 CloseClient(con);
731 return (0);
732 }
733
734 LogRequest(con, HTTP_OK);
735 }
736
737 if (httpPrintf(HTTP(con), "\r\n") < 0)
738 {
739 CloseClient(con);
740 return (0);
741 }
742
743 con->http.state = HTTP_WAITING;
744 break;
745 }
746 }
747
748 /*
749 * Handle any incoming data...
750 */
751
752 switch (con->http.state)
753 {
754 case HTTP_PUT_RECV :
755 break;
756
757 case HTTP_POST_RECV :
758 LogMessage(LOG_DEBUG, "ReadClient() %d con->data_encoding = %s con->data_remaining = %d",
759 con->http.fd,
760 con->http.data_encoding == HTTP_ENCODE_CHUNKED ? "chunked" : "length",
761 con->http.data_remaining);
762 DEBUG_printf(("ReadClient() %d con->data_encoding = %s con->data_remaining = %d\n",
763 con->http.fd,
764 con->http.data_encoding == HTTP_ENCODE_CHUNKED ? "chunked" : "length",
765 con->http.data_remaining));
766
767 if (con->request != NULL)
768 {
769 /*
770 * Grab any request data from the connection...
771 */
772
773 if ((ipp_state = ippRead(&(con->http), con->request)) == IPP_ERROR)
774 {
775 LogMessage(LOG_ERROR, "ReadClient() %d IPP Read Error!",
776 con->http.fd);
777 CloseClient(con);
778 return (0);
779 }
780 else if (ipp_state != IPP_DATA)
781 break;
782
783 if (con->file == 0 && con->http.state != HTTP_POST_SEND)
784 {
785 /*
786 * Create a file as needed for the request data...
787 */
788
789 snprintf(con->filename, sizeof(con->filename), "%s/%08xXXXXXX",
790 RequestRoot, time(NULL));
791 con->file = mkstemp(con->filename);
792 fchmod(con->file, 0640);
793 fchown(con->file, User, Group);
794
795 LogMessage(LOG_DEBUG, "ReadClient() %d REQUEST %s", con->http.fd,
796 con->filename);
797
798 if (con->file < 0)
799 {
800 if (!SendError(con, HTTP_REQUEST_TOO_LARGE))
801 {
802 CloseClient(con);
803 return (0);
804 }
805 }
806 }
807 }
808
809 if (con->http.state != HTTP_POST_SEND)
810 {
811 if ((bytes = httpRead(HTTP(con), line, sizeof(line))) < 0)
812 {
813 CloseClient(con);
814 return (0);
815 }
816 else if (bytes > 0)
817 {
818 con->bytes += bytes;
819
820 if (bytes >= 1024)
821 LogMessage(LOG_DEBUG, "ReadClient() %d writing %d bytes", bytes);
822
823 if (write(con->file, line, bytes) < bytes)
824 {
825 close(con->file);
826 con->file = 0;
827 unlink(con->filename);
828
829 if (!SendError(con, HTTP_REQUEST_TOO_LARGE))
830 {
831 CloseClient(con);
832 return (0);
833 }
834 }
835 }
836 else if (con->http.state != HTTP_POST_SEND)
837 {
838 CloseClient(con);
839 return (0);
840 }
841 }
842
843 if (con->http.state == HTTP_POST_SEND)
844 {
845 if (con->file)
846 {
847 fstat(con->file, &filestats);
848 close(con->file);
849 con->file = 0;
850
851 if (filestats.st_size > MaxRequestSize &&
852 MaxRequestSize > 0)
853 {
854 /*
855 * Request is too big; remove it and send an error...
856 */
857
858 unlink(con->filename);
859
860 if (con->request)
861 {
862 /*
863 * Delete any IPP request data...
864 */
865
866 ippDelete(con->request);
867 con->request = NULL;
868 }
869
870 if (!SendError(con, HTTP_REQUEST_TOO_LARGE))
871 {
872 CloseClient(con);
873 return (0);
874 }
875 }
876 }
877
878 if (con->request)
879 ProcessIPPRequest(con);
880 }
881 break;
882 }
883
884 if (!con->http.keep_alive && con->http.state == HTTP_WAITING)
885 {
886 CloseClient(con);
887 return (0);
888 }
889 else
890 return (1);
891 }
892
893
894 /*
895 * 'SendCommand()' - Send output from a command via HTTP.
896 */
897
898 int
899 SendCommand(client_t *con,
900 char *command,
901 char *options)
902 {
903 con->pipe_pid = pipe_command(con, 0, &(con->file), command, options);
904
905 LogMessage(LOG_DEBUG, "SendCommand() %d command=\"%s\" file=%d pipe_pid=%d",
906 con->http.fd, command, con->file, con->pipe_pid);
907
908 if (con->pipe_pid == 0)
909 return (0);
910
911 fcntl(con->file, F_SETFD, fcntl(con->file, F_GETFD) | FD_CLOEXEC);
912
913 DEBUG_printf(("SendCommand: Adding fd %d to InputSet...\n", con->file));
914 FD_SET(con->file, &InputSet);
915 FD_SET(con->http.fd, &OutputSet);
916
917 if (!SendHeader(con, HTTP_OK, NULL))
918 return (0);
919
920 if (con->http.version == HTTP_1_1)
921 {
922 con->http.data_encoding = HTTP_ENCODE_CHUNKED;
923
924 if (httpPrintf(HTTP(con), "Transfer-Encoding: chunked\r\n") < 0)
925 return (0);
926 }
927
928 return (1);
929 }
930
931
932 /*
933 * 'SendError()' - Send an error message via HTTP.
934 */
935
936 int /* O - 1 if successful, 0 otherwise */
937 SendError(client_t *con, /* I - Connection */
938 http_status_t code) /* I - Error code */
939 {
940 char message[1024]; /* Message for user */
941
942
943 /*
944 * Put the request in the access_log file...
945 */
946
947 if (con->operation > HTTP_WAITING)
948 LogRequest(con, code);
949
950 /*
951 * To work around bugs in some proxies, don't use Keep-Alive for some
952 * error messages...
953 */
954
955 if (code >= HTTP_BAD_REQUEST)
956 con->http.keep_alive = HTTP_KEEPALIVE_OFF;
957
958 /*
959 * Send an error message back to the client. If the error code is a
960 * 400 or 500 series, make sure the message contains some text, too!
961 */
962
963 if (!SendHeader(con, code, NULL))
964 return (0);
965
966 if (code == HTTP_UNAUTHORIZED)
967 {
968 if (httpPrintf(HTTP(con), "WWW-Authenticate: Basic realm=\"CUPS\"\r\n") < 0)
969 return (0);
970 }
971
972 if (con->http.version >= HTTP_1_1 && !con->http.keep_alive)
973 {
974 if (httpPrintf(HTTP(con), "Connection: close\r\n") < 0)
975 return (0);
976 }
977
978 if (code >= HTTP_BAD_REQUEST)
979 {
980 /*
981 * Send a human-readable error message.
982 */
983
984 snprintf(message, sizeof(message),
985 "<HTML><HEAD><TITLE>%d %s</TITLE></HEAD>"
986 "<BODY><H1>%s</H1>%s</BODY></HTML>\n",
987 code, httpStatus(code), httpStatus(code),
988 con->language ? con->language->messages[code] :
989 httpStatus(code));
990
991 if (httpPrintf(HTTP(con), "Content-Type: text/html\r\n") < 0)
992 return (0);
993 if (httpPrintf(HTTP(con), "Content-Length: %d\r\n", strlen(message)) < 0)
994 return (0);
995 if (httpPrintf(HTTP(con), "\r\n") < 0)
996 return (0);
997 if (httpPrintf(HTTP(con), "%s", message) < 0)
998 return (0);
999 }
1000 else if (httpPrintf(HTTP(con), "\r\n") < 0)
1001 return (0);
1002
1003 con->http.state = HTTP_WAITING;
1004
1005 return (1);
1006 }
1007
1008
1009 /*
1010 * 'SendFile()' - Send a file via HTTP.
1011 */
1012
1013 int
1014 SendFile(client_t *con,
1015 http_status_t code,
1016 char *filename,
1017 char *type,
1018 struct stat *filestats)
1019 {
1020 con->file = open(filename, O_RDONLY);
1021
1022 LogMessage(LOG_DEBUG, "SendFile() %d file=%d", con->http.fd, con->file);
1023
1024 if (con->file < 0)
1025 return (0);
1026
1027 fcntl(con->file, F_SETFD, fcntl(con->file, F_GETFD) | FD_CLOEXEC);
1028
1029 con->pipe_pid = 0;
1030
1031 if (!SendHeader(con, code, type))
1032 return (0);
1033
1034 if (httpPrintf(HTTP(con), "Last-Modified: %s\r\n", httpGetDateString(filestats->st_mtime)) < 0)
1035 return (0);
1036 if (httpPrintf(HTTP(con), "Content-Length: %d\r\n", filestats->st_size) < 0)
1037 return (0);
1038 if (httpPrintf(HTTP(con), "\r\n") < 0)
1039 return (0);
1040
1041 FD_SET(con->http.fd, &OutputSet);
1042
1043 return (1);
1044 }
1045
1046
1047 /*
1048 * 'SendHeader()' - Send an HTTP request.
1049 */
1050
1051 int /* O - 1 on success, 0 on failure */
1052 SendHeader(client_t *con, /* I - Client to send to */
1053 http_status_t code, /* I - HTTP status code */
1054 char *type) /* I - MIME type of document */
1055 {
1056 if (httpPrintf(HTTP(con), "HTTP/%d.%d %d %s\r\n", con->http.version / 100,
1057 con->http.version % 100, code, httpStatus(code)) < 0)
1058 return (0);
1059 if (httpPrintf(HTTP(con), "Date: %s\r\n", httpGetDateString(time(NULL))) < 0)
1060 return (0);
1061 if (httpPrintf(HTTP(con), "Server: CUPS/1.1\r\n") < 0)
1062 return (0);
1063 if (con->http.keep_alive && con->http.version >= HTTP_1_0)
1064 {
1065 if (httpPrintf(HTTP(con), "Connection: Keep-Alive\r\n") < 0)
1066 return (0);
1067 if (httpPrintf(HTTP(con), "Keep-Alive: timeout=%d\r\n", KeepAliveTimeout) < 0)
1068 return (0);
1069 }
1070 if (con->language != NULL)
1071 {
1072 if (httpPrintf(HTTP(con), "Content-Language: %s\r\n",
1073 con->language->language) < 0)
1074 return (0);
1075
1076 if (type != NULL)
1077 if (httpPrintf(HTTP(con), "Content-Type: %s; charset=%s\r\n", type,
1078 cupsLangEncoding(con->language)) < 0)
1079 return (0);
1080 }
1081 else if (type != NULL)
1082 if (httpPrintf(HTTP(con), "Content-Type: %s\r\n", type) < 0)
1083 return (0);
1084
1085 return (1);
1086 }
1087
1088
1089 /*
1090 * 'WriteClient()' - Write data to a client as needed.
1091 */
1092
1093 int /* O - 1 if success, 0 if fail */
1094 WriteClient(client_t *con) /* I - Client connection */
1095 {
1096 int bytes; /* Number of bytes written */
1097 char buf[HTTP_MAX_BUFFER]; /* Data buffer */
1098 ipp_state_t ipp_state; /* IPP state value */
1099
1100
1101 if (con->http.state != HTTP_GET_SEND &&
1102 con->http.state != HTTP_POST_SEND)
1103 return (1);
1104
1105 if (con->response != NULL)
1106 {
1107 ipp_state = ippWrite(&(con->http), con->response);
1108 bytes = ipp_state != IPP_ERROR && ipp_state != IPP_DATA;
1109 }
1110 else if ((bytes = read(con->file, buf, sizeof(buf))) > 0)
1111 {
1112 if (httpWrite(HTTP(con), buf, bytes) < 0)
1113 {
1114 CloseClient(con);
1115 return (0);
1116 }
1117
1118 con->bytes += bytes;
1119 }
1120
1121 if (bytes <= 0)
1122 {
1123 LogRequest(con, HTTP_OK);
1124
1125 if (con->http.data_encoding == HTTP_ENCODE_CHUNKED)
1126 {
1127 if (httpPrintf(HTTP(con), "0\r\n\r\n") < 0)
1128 {
1129 CloseClient(con);
1130 return (0);
1131 }
1132 }
1133
1134 con->http.state = HTTP_WAITING;
1135
1136 FD_CLR(con->http.fd, &OutputSet);
1137
1138 if (con->file)
1139 {
1140 DEBUG_printf(("WriteClient: Removing fd %d from InputSet...\n", con->file));
1141 FD_CLR(con->file, &InputSet);
1142
1143 if (con->pipe_pid)
1144 kill(con->pipe_pid, SIGTERM);
1145
1146 close(con->file);
1147 con->file = 0;
1148 con->pipe_pid = 0;
1149 }
1150
1151 if (con->request != NULL)
1152 {
1153 ippDelete(con->request);
1154 con->request = NULL;
1155 }
1156
1157 if (con->response != NULL)
1158 {
1159 ippDelete(con->response);
1160 con->response = NULL;
1161 }
1162
1163 if (!con->http.keep_alive)
1164 {
1165 CloseClient(con);
1166 return (0);
1167 }
1168 }
1169
1170 if (bytes >= 1024)
1171 LogMessage(LOG_DEBUG, "WriteClient() %d %d bytes", con->http.fd, bytes);
1172
1173 con->http.activity = time(NULL);
1174
1175 return (1);
1176 }
1177
1178
1179 /*
1180 * 'check_if_modified()' - Decode an "If-Modified-Since" line.
1181 */
1182
1183 static int /* O - 1 if modified since */
1184 check_if_modified(client_t *con, /* I - Client connection */
1185 struct stat *filestats) /* I - File information */
1186 {
1187 char *ptr; /* Pointer into field */
1188 time_t date; /* Time/date value */
1189 int size; /* Size/length value */
1190
1191
1192 size = 0;
1193 date = 0;
1194 ptr = con->http.fields[HTTP_FIELD_IF_MODIFIED_SINCE];
1195
1196 if (*ptr == '\0')
1197 return (1);
1198
1199 LogMessage(LOG_DEBUG, "check_if_modified() %d If-Modified-Since=\"%s\"",
1200 con->http.fd, ptr);
1201
1202 while (*ptr != '\0')
1203 {
1204 while (isspace(*ptr) || *ptr == ';')
1205 ptr ++;
1206
1207 if (strncasecmp(ptr, "length=", 7) == 0)
1208 {
1209 ptr += 7;
1210 size = atoi(ptr);
1211
1212 while (isdigit(*ptr))
1213 ptr ++;
1214 }
1215 else if (isalpha(*ptr))
1216 {
1217 date = httpGetDateTime(ptr);
1218 while (*ptr != '\0' && *ptr != ';')
1219 ptr ++;
1220 }
1221 }
1222
1223 LogMessage(LOG_DEBUG, "check_if_modified() %d sizes=%d,%d dates=%d,%d",
1224 con->http.fd, size, filestats->st_size, date, filestats->st_mtime);
1225
1226 return ((size != filestats->st_size && size != 0) ||
1227 (date < filestats->st_mtime && date != 0) ||
1228 (size == 0 && date == 0));
1229 }
1230
1231
1232 /*
1233 * 'decode_auth()' - Decode an authorization string.
1234 */
1235
1236 static void
1237 decode_auth(client_t *con) /* I - Client to decode to */
1238 {
1239 char *s, /* Authorization string */
1240 value[1024]; /* Value string */
1241 const char *username; /* Certificate username */
1242
1243
1244 /*
1245 * Decode the string...
1246 */
1247
1248 s = con->http.fields[HTTP_FIELD_AUTHORIZATION];
1249
1250 if (strncmp(s, "Basic", 5) == 0)
1251 {
1252 s += 5;
1253 while (isspace(*s))
1254 s ++;
1255
1256 httpDecode64(value, s);
1257
1258 /*
1259 * Pull the username and password out...
1260 */
1261
1262 if ((s = strchr(value, ':')) == NULL)
1263 {
1264 LogMessage(LOG_DEBUG, "decode_auth() %d no colon in auth string \"%s\"",
1265 con->http.fd, value);
1266 return;
1267 }
1268
1269 *s++ = '\0';
1270
1271 strncpy(con->username, value, sizeof(con->username) - 1);
1272 con->username[sizeof(con->username) - 1] = '\0';
1273
1274 strncpy(con->password, s, sizeof(con->password) - 1);
1275 con->password[sizeof(con->password) - 1] = '\0';
1276 }
1277 else if (strncmp(s, "Local", 5) == 0)
1278 {
1279 s += 5;
1280 while (isspace(*s))
1281 s ++;
1282
1283 if ((username = FindCert(s)) != NULL)
1284 strcpy(con->username, username);
1285 }
1286
1287 LogMessage(LOG_DEBUG, "decode_auth() %d username=\"%s\"",
1288 con->http.fd, con->username);
1289 }
1290
1291
1292 /*
1293 * 'get_file()' - Get a filename and state info.
1294 */
1295
1296 static char * /* O - Real filename */
1297 get_file(client_t *con, /* I - Client connection */
1298 struct stat *filestats)/* O - File information */
1299 {
1300 int status; /* Status of filesystem calls */
1301 char *params; /* Pointer to parameters in URI */
1302 static char filename[1024]; /* Filename buffer */
1303
1304
1305 /*
1306 * Need to add DocumentRoot global...
1307 */
1308
1309 if (strncmp(con->uri, "/ppd/", 5) == 0)
1310 snprintf(filename, sizeof(filename), "%s%s", ServerRoot, con->uri);
1311 else if (con->language != NULL)
1312 snprintf(filename, sizeof(filename), "%s/%s%s", DocumentRoot, con->language->language,
1313 con->uri);
1314 else
1315 snprintf(filename, sizeof(filename), "%s%s", DocumentRoot, con->uri);
1316
1317 if ((params = strchr(filename, '?')) != NULL)
1318 *params = '\0';
1319
1320 /*
1321 * Grab the status for this language; if there isn't a language-specific file
1322 * then fallback to the default one...
1323 */
1324
1325 if ((status = stat(filename, filestats)) != 0 && con->language != NULL)
1326 {
1327 /*
1328 * Drop the language prefix and try the current directory...
1329 */
1330
1331 if (strncmp(con->uri, "/ppd/", 5) != 0)
1332 {
1333 snprintf(filename, sizeof(filename), "%s%s", DocumentRoot, con->uri);
1334
1335 status = stat(filename, filestats);
1336 }
1337 }
1338
1339 /*
1340 * If we're found a directory, get the index.html file instead...
1341 */
1342
1343 if (!status && S_ISDIR(filestats->st_mode))
1344 {
1345 if (filename[strlen(filename) - 1] == '/')
1346 strcat(filename, "index.html");
1347 else
1348 strcat(filename, "/index.html");
1349
1350 status = stat(filename, filestats);
1351 }
1352
1353 LogMessage(LOG_DEBUG, "get_file() %d filename=%s size=%d",
1354 con->http.fd, filename, status ? -1 : filestats->st_size);
1355
1356 if (status)
1357 return (NULL);
1358 else
1359 return (filename);
1360 }
1361
1362
1363 /*
1364 * 'pipe_command()' - Pipe the output of a command to the remote client.
1365 */
1366
1367 static int /* O - Process ID */
1368 pipe_command(client_t *con, /* I - Client connection */
1369 int infile, /* I - Standard input for command */
1370 int *outfile, /* O - Standard output for command */
1371 char *command, /* I - Command to run */
1372 char *options) /* I - Options for command */
1373 {
1374 int pid; /* Process ID */
1375 char *commptr; /* Command string pointer */
1376 int fds[2]; /* Pipe FDs */
1377 int argc; /* Number of arguments */
1378 char argbuf[1024], /* Argument buffer */
1379 *argv[100], /* Argument strings */
1380 *envp[100]; /* Environment variables */
1381 char hostname[1024]; /* Hostname string */
1382 static char lang[1024]; /* LANG env variable */
1383 static char content_length[1024]; /* CONTENT_LENGTH env variable */
1384 static char content_type[1024]; /* CONTENT_TYPE env variable */
1385 static char ipp_port[1024]; /* Default listen port */
1386 static char server_port[1024]; /* Default listen port */
1387 static char server_name[1024]; /* Default listen hostname */
1388 static char remote_host[1024]; /* REMOTE_HOST env variable */
1389 static char remote_user[1024]; /* REMOTE_HOST env variable */
1390 static char tmpdir[1024]; /* TMPDIR env variable */
1391
1392
1393 /*
1394 * Copy the command string...
1395 */
1396
1397 strncpy(argbuf, options, sizeof(argbuf) - 1);
1398 argbuf[sizeof(argbuf) - 1] = '\0';
1399
1400 /*
1401 * Parse the string; arguments can be separated by spaces or by ? or +...
1402 */
1403
1404 argv[0] = argbuf;
1405
1406 for (commptr = argbuf, argc = 1; *commptr != '\0' && argc < 99; commptr ++)
1407 if (*commptr == ' ' || *commptr == '?' || *commptr == '+')
1408 {
1409 *commptr++ = '\0';
1410
1411 while (*commptr == ' ')
1412 commptr ++;
1413
1414 if (*commptr != '\0')
1415 {
1416 argv[argc] = commptr;
1417 argc ++;
1418 }
1419
1420 commptr --;
1421 }
1422 else if (*commptr == '%')
1423 {
1424 if (commptr[1] >= '0' && commptr[1] <= '9')
1425 *commptr = (commptr[1] - '0') << 4;
1426 else
1427 *commptr = (tolower(commptr[1]) - 'a' + 10) << 4;
1428
1429 if (commptr[2] >= '0' && commptr[2] <= '9')
1430 *commptr |= commptr[2] - '0';
1431 else
1432 *commptr |= tolower(commptr[2]) - 'a' + 10;
1433
1434 strcpy(commptr + 1, commptr + 3);
1435 }
1436
1437 argv[argc] = NULL;
1438
1439 if (argv[0][0] == '\0')
1440 argv[0] = strrchr(command, '/') + 1;
1441
1442 /*
1443 * Setup the environment variables as needed...
1444 */
1445
1446 gethostname(hostname, sizeof(hostname) - 1);
1447
1448 sprintf(lang, "LANG=%s", con->language ? con->language->language : "C");
1449 sprintf(ipp_port, "IPP_PORT=%d", ntohs(con->http.hostaddr.sin_port));
1450 sprintf(server_port, "SERVER_PORT=%d", ntohs(con->http.hostaddr.sin_port));
1451 sprintf(server_name, "SERVER_NAME=%s", hostname);
1452 sprintf(remote_host, "REMOTE_HOST=%s", con->http.hostname);
1453 sprintf(remote_user, "REMOTE_USER=%s", con->username);
1454 sprintf(tmpdir, "TMPDIR=%s", TempDir);
1455
1456 envp[0] = "PATH=/bin:/usr/bin";
1457 envp[1] = "SERVER_SOFTWARE=CUPS/1.1";
1458 envp[2] = "GATEWAY_INTERFACE=CGI/1.1";
1459 envp[3] = "SERVER_PROTOCOL=HTTP/1.1";
1460 envp[4] = ipp_port;
1461 envp[5] = server_name;
1462 envp[6] = server_port;
1463 envp[7] = remote_host;
1464 envp[8] = remote_user;
1465 envp[9] = lang;
1466 envp[10] = TZ;
1467 envp[11] = tmpdir;
1468
1469 if (con->operation == HTTP_GET)
1470 {
1471 envp[12] = "REQUEST_METHOD=GET";
1472 envp[13] = NULL;
1473 }
1474 else
1475 {
1476 sprintf(content_length, "CONTENT_LENGTH=%d", con->http.data_remaining);
1477 snprintf(content_type, sizeof(content_type), "CONTENT_TYPE=%s",
1478 con->http.fields[HTTP_FIELD_CONTENT_TYPE]);
1479
1480 envp[12] = "REQUEST_METHOD=POST";
1481 envp[13] = content_length;
1482 envp[14] = content_type;
1483 envp[15] = NULL;
1484 }
1485
1486 /*
1487 * Create a pipe for the output...
1488 */
1489
1490 if (pipe(fds))
1491 {
1492 LogMessage(LOG_ERROR, "Unable to create pipes for CGI %s - %s",
1493 argv[0], strerror(errno));
1494 return (0);
1495 }
1496
1497 /*
1498 * Then execute the command...
1499 */
1500
1501 if ((pid = fork()) == 0)
1502 {
1503 /*
1504 * Child comes here... Close stdin if necessary and dup the pipe to stdout.
1505 */
1506
1507 setgid(Group);
1508 setuid(User);
1509
1510 if (infile)
1511 {
1512 close(0);
1513 dup(infile);
1514 }
1515
1516 close(1);
1517 dup(fds[1]);
1518
1519 close(fds[0]);
1520 close(fds[1]);
1521
1522 /*
1523 * Execute the pipe program; if an error occurs, exit with status 1...
1524 */
1525
1526 execve(command, argv, envp);
1527 perror("execve failed");
1528 exit(errno);
1529 return (0);
1530 }
1531 else if (pid < 0)
1532 {
1533 /*
1534 * Error - can't fork!
1535 */
1536
1537 LogMessage(LOG_ERROR, "Unable to fork for CGI %s - %s", argv[0],
1538 strerror(errno));
1539
1540 close(fds[0]);
1541 close(fds[1]);
1542 return (0);
1543 }
1544 else
1545 {
1546 /*
1547 * Fork successful - return the PID...
1548 */
1549
1550 LogMessage(LOG_DEBUG, "CGI %s started - PID = %d", argv[0], pid);
1551
1552 *outfile = fds[0];
1553 close(fds[1]);
1554
1555 AddCert(pid, con->username);
1556
1557 return (pid);
1558 }
1559 }
1560
1561
1562 /*
1563 * End of "$Id: client.c,v 1.44 2000/01/06 15:01:00 mike Exp $".
1564 */