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