]> git.ipfire.org Git - thirdparty/cups.git/blob - scheduler/client.c
Mirror 1.1.x changes to use strlcpy() and strlcat().
[thirdparty/cups.git] / scheduler / client.c
1 /*
2 * "$Id: client.c,v 1.91.2.12 2002/05/16 14:00:07 mike Exp $"
3 *
4 * Client routines for the Common UNIX Printing System (CUPS) scheduler.
5 *
6 * Copyright 1997-2002 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 * EncryptClient() - Enable encryption for the client...
30 * ReadClient() - Read data from a client.
31 * SendCommand() - Send output from a command via HTTP.
32 * SendError() - Send an error message via HTTP.
33 * SendFile() - Send a file via HTTP.
34 * SendHeader() - Send an HTTP request.
35 * WriteClient() - Write data to a client as needed.
36 * check_if_modified() - Decode an "If-Modified-Since" line.
37 * decode_auth() - Decode an authorization string.
38 * get_file() - Get a filename and state info.
39 * install_conf_file() - Install a configuration file.
40 * pipe_command() - Pipe the output of a command to the remote client.
41 */
42
43 /*
44 * Include necessary headers...
45 */
46
47 #include "cupsd.h"
48
49 #include <grp.h>
50
51 #ifdef HAVE_LIBSSL
52 # include <openssl/err.h>
53 # include <openssl/ssl.h>
54 # include <openssl/rand.h>
55 #endif /* HAVE_LIBSSL */
56
57
58 /*
59 * Local functions...
60 */
61
62 static int check_if_modified(client_t *con,
63 struct stat *filestats);
64 static void decode_auth(client_t *con);
65 static char *get_file(client_t *con, struct stat *filestats);
66 static http_status_t install_conf_file(client_t *con);
67 static int pipe_command(client_t *con, int infile, int *outfile,
68 char *command, char *options);
69
70
71 /*
72 * 'AcceptClient()' - Accept a new client.
73 */
74
75 void
76 AcceptClient(listener_t *lis) /* I - Listener socket */
77 {
78 int i; /* Looping var */
79 int val; /* Parameter value */
80 client_t *con; /* New client pointer */
81 const struct hostent *host; /* Host entry for address */
82 char *hostname;/* Hostname for address */
83 http_addr_t temp; /* Temporary address variable */
84
85
86 LogMessage(L_DEBUG2, "AcceptClient(%p) %d NumClients = %d",
87 lis, lis->fd, NumClients);
88
89 /*
90 * Make sure we don't have a full set of clients already...
91 */
92
93 if (NumClients == MaxClients)
94 return;
95
96 /*
97 * Get a pointer to the next available client...
98 */
99
100 con = Clients + NumClients;
101
102 memset(con, 0, sizeof(client_t));
103 con->http.activity = time(NULL);
104
105 /*
106 * Accept the client and get the remote address...
107 */
108
109 val = sizeof(struct sockaddr_in);
110
111 if ((con->http.fd = accept(lis->fd, (struct sockaddr *)&(con->http.hostaddr),
112 &val)) < 0)
113 {
114 LogMessage(L_ERROR, "Unable to accept client connection - %s.",
115 strerror(errno));
116 return;
117 }
118
119 #ifdef AF_INET6
120 if (lis->address.addr.sa_family == AF_INET6)
121 con->http.hostaddr.ipv6.sin6_port = lis->address.ipv6.sin6_port;
122 else
123 #endif /* AF_INET6 */
124 con->http.hostaddr.ipv4.sin_port = lis->address.ipv4.sin_port;
125
126 if (HostNameLookups)
127 hostname = httpAddrLookup(&(con->http.hostaddr), con->http.hostname,
128 sizeof(con->http.hostname));
129 else
130 {
131 hostname = NULL;
132 httpAddrString(&(con->http.hostaddr), con->http.hostname,
133 sizeof(con->http.hostname));
134 }
135
136 if (httpAddrEqual(&(con->http.hostaddr), &ServerAddr))
137 {
138 /*
139 * Map accesses from the same host to the server name.
140 */
141
142 strlcpy(con->http.hostname, ServerName, sizeof(con->http.hostname));
143 }
144
145 if (hostname == NULL && HostNameLookups == 2)
146 {
147 /*
148 * Can't have an unresolved IP address with double-lookups enabled...
149 */
150
151 LogMessage(L_DEBUG2, "AcceptClient: Closing connection %d...",
152 con->http.fd);
153
154 #ifdef WIN32
155 closesocket(con->http.fd);
156 #else
157 close(con->http.fd);
158 #endif /* WIN32 */
159
160 LogMessage(L_WARN, "Name lookup failed - connection from %s closed!",
161 con->http.hostname);
162 return;
163 }
164
165 if (HostNameLookups == 2)
166 {
167 /*
168 * Do double lookups as needed...
169 */
170
171 if ((host = httpGetHostByName(con->http.hostname)) != NULL)
172 {
173 /*
174 * See if the hostname maps to the same IP address...
175 */
176
177 if (host->h_addrtype != con->http.hostaddr.addr.sa_family)
178 {
179 /*
180 * Not the right type of address...
181 */
182
183 host = NULL;
184 }
185 else
186 {
187 /*
188 * Compare all of the addresses against this one...
189 */
190
191 for (i = 0; host->h_addr_list[i]; i ++)
192 {
193 httpAddrLoad(host, 0, i, &temp);
194
195 if (httpAddrEqual(&(con->http.hostaddr), &temp))
196 break;
197 }
198
199 if (!host->h_addr_list[i])
200 host = NULL;
201 }
202 }
203
204 if (host == NULL)
205 {
206 /*
207 * Can't have a hostname that doesn't resolve to the same IP address
208 * with double-lookups enabled...
209 */
210
211 LogMessage(L_DEBUG2, "AcceptClient: Closing connection %d...",
212 con->http.fd);
213
214 #ifdef WIN32
215 closesocket(con->http.fd);
216 #else
217 close(con->http.fd);
218 #endif /* WIN32 */
219
220 LogMessage(L_WARN, "IP lookup failed - connection from %s closed!",
221 con->http.hostname);
222 return;
223 }
224 }
225
226 #ifdef AF_INET6
227 if (con->http.hostaddr.addr.sa_family == AF_INET6)
228 LogMessage(L_DEBUG, "AcceptClient: %d from %s:%d.", con->http.fd,
229 con->http.hostname, ntohs(con->http.hostaddr.ipv6.sin6_port));
230 else
231 #endif /* AF_INET6 */
232 LogMessage(L_DEBUG, "AcceptClient: %d from %s:%d.", con->http.fd,
233 con->http.hostname, ntohs(con->http.hostaddr.ipv4.sin_port));
234
235 /*
236 * Using TCP_NODELAY improves responsiveness, especially on systems
237 * with a slow loopback interface... Since we write large buffers
238 * when sending print files and requests, there shouldn't be any
239 * performance penalty for this...
240 */
241
242 val = 1;
243 setsockopt(con->http.fd, IPPROTO_TCP, TCP_NODELAY, &val, sizeof(val));
244
245 /*
246 * Add the socket to the select() input mask.
247 */
248
249 fcntl(con->http.fd, F_SETFD, fcntl(con->http.fd, F_GETFD) | FD_CLOEXEC);
250
251 LogMessage(L_DEBUG2, "AcceptClient: Adding fd %d to InputSet...",
252 con->http.fd);
253 FD_SET(con->http.fd, &InputSet);
254
255 NumClients ++;
256
257 /*
258 * Temporarily suspend accept()'s until we lose a client...
259 */
260
261 if (NumClients == MaxClients)
262 PauseListening();
263
264 #ifdef HAVE_LIBSSL
265 /*
266 * See if we are connecting on a secure port...
267 */
268
269 if (lis->encryption == HTTP_ENCRYPT_ALWAYS)
270 {
271 /*
272 * https connection; go secure...
273 */
274
275 EncryptClient(con);
276 }
277 #endif /* HAVE_LIBSSL */
278 }
279
280
281 /*
282 * 'CloseAllClients()' - Close all remote clients immediately.
283 */
284
285 void
286 CloseAllClients(void)
287 {
288 while (NumClients > 0)
289 CloseClient(Clients);
290 }
291
292
293 /*
294 * 'CloseClient()' - Close a remote client.
295 */
296
297 void
298 CloseClient(client_t *con) /* I - Client to close */
299 {
300 int status; /* Exit status of pipe command */
301 #ifdef HAVE_LIBSSL
302 SSL_CTX *context; /* Context for encryption */
303 SSL *conn; /* Connection for encryption */
304 #endif /* HAVE_LIBSSL */
305
306
307 LogMessage(L_DEBUG, "CloseClient() %d", con->http.fd);
308
309 #ifdef HAVE_LIBSSL
310 /*
311 * Shutdown encryption as needed...
312 */
313
314 if (con->http.tls)
315 {
316 conn = (SSL *)(con->http.tls);
317 context = SSL_get_SSL_CTX(conn);
318
319 SSL_shutdown(conn);
320 SSL_CTX_free(context);
321 SSL_free(conn);
322
323 con->http.tls = NULL;
324 }
325 #endif /* HAVE_LIBSSL */
326
327 /*
328 * Close the socket and clear the file from the input set for select()...
329 */
330
331 if (con->http.fd >= 0)
332 {
333 LogMessage(L_DEBUG2, "CloseClient: Removing fd %d from InputSet and OutputSet...",
334 con->http.fd);
335 close(con->http.fd);
336 FD_CLR(con->http.fd, &InputSet);
337 FD_CLR(con->http.fd, &OutputSet);
338 con->http.fd = 0;
339 }
340
341 if (con->pipe_pid != 0)
342 {
343 LogMessage(L_DEBUG2, "CloseClient: Removing fd %d from InputSet...",
344 con->file);
345 FD_CLR(con->file, &InputSet);
346 }
347
348 if (con->file)
349 {
350 /*
351 * Close the open data file...
352 */
353
354 if (con->pipe_pid)
355 {
356 kill(con->pipe_pid, SIGKILL);
357 waitpid(con->pipe_pid, &status, WNOHANG);
358 }
359
360 LogMessage(L_DEBUG2, "CloseClient() %d Closing data file %d.",
361 con->http.fd, con->file);
362 LogMessage(L_DEBUG2, "CloseClient() %d Removing fd %d from InputSet.",
363 con->http.fd, con->file);
364
365 FD_CLR(con->file, &InputSet);
366 close(con->file);
367 con->file = 0;
368 }
369
370 if (con->request)
371 {
372 ippDelete(con->request);
373 con->request = NULL;
374 }
375
376 if (con->response)
377 {
378 ippDelete(con->response);
379 con->response = NULL;
380 }
381
382 if (con->language)
383 {
384 cupsLangFree(con->language);
385 con->language = NULL;
386 }
387
388 /*
389 * Re-enable new client connections if we are going back under the
390 * limit...
391 */
392
393 if (NumClients == MaxClients)
394 ResumeListening();
395
396 /*
397 * Compact the list of clients as necessary...
398 */
399
400 NumClients --;
401
402 if (con < (Clients + NumClients))
403 memcpy(con, con + 1, (Clients + NumClients - con) * sizeof(client_t));
404 }
405
406
407 /*
408 * 'EncryptClient()' - Enable encryption for the client...
409 */
410
411 int /* O - 1 on success, 0 on error */
412 EncryptClient(client_t *con) /* I - Client to encrypt */
413 {
414 #ifdef HAVE_LIBSSL
415 SSL_CTX *context; /* Context for encryption */
416 SSL *conn; /* Connection for encryption */
417 unsigned long error; /* Error code */
418
419
420 /*
421 * Create the SSL context and accept the connection...
422 */
423
424 context = SSL_CTX_new(SSLv23_method());
425 conn = SSL_new(context);
426
427 SSL_use_PrivateKey_file(conn, ServerKey, SSL_FILETYPE_PEM);
428 SSL_use_certificate_file(conn, ServerCertificate, SSL_FILETYPE_PEM);
429
430 SSL_set_fd(conn, con->http.fd);
431 if (SSL_accept(conn) != 1)
432 {
433 while ((error = ERR_get_error()) != 0)
434 LogMessage(L_ERROR, "EncryptClient: %s", ERR_error_string(error, NULL));
435
436 SSL_CTX_free(context);
437 SSL_free(conn);
438 return (0);
439 }
440
441 con->http.tls = conn;
442 return (1);
443 #else
444 return (0);
445 #endif /* HAVE_LIBSSL */
446 }
447
448
449 /*
450 * 'ReadClient()' - Read data from a client.
451 */
452
453 int /* O - 1 on success, 0 on error */
454 ReadClient(client_t *con) /* I - Client to read from */
455 {
456 char line[32768], /* Line from client... */
457 operation[64], /* Operation code from socket */
458 version[64]; /* HTTP version number string */
459 int major, minor; /* HTTP version numbers */
460 http_status_t status; /* Transfer status */
461 ipp_state_t ipp_state; /* State of IPP transfer */
462 int bytes; /* Number of bytes to POST */
463 char *filename; /* Name of file for GET/HEAD */
464 struct stat filestats; /* File information */
465 mime_type_t *type; /* MIME type of file */
466 printer_t *p; /* Printer */
467 location_t *best; /* Best match for authentication */
468 static unsigned request_id = 0;/* Request ID for temp files */
469
470
471 status = HTTP_CONTINUE;
472
473 switch (con->http.state)
474 {
475 case HTTP_WAITING :
476 /*
477 * See if we've received a request line...
478 */
479
480 if (httpGets(line, sizeof(line) - 1, HTTP(con)) == NULL)
481 {
482 CloseClient(con);
483 return (0);
484 }
485
486 /*
487 * Ignore blank request lines...
488 */
489
490 if (line[0] == '\0')
491 break;
492
493 /*
494 * Clear other state variables...
495 */
496
497 httpClearFields(HTTP(con));
498
499 con->http.activity = time(NULL);
500 con->http.version = HTTP_1_0;
501 con->http.keep_alive = HTTP_KEEPALIVE_OFF;
502 con->http.data_encoding = HTTP_ENCODE_LENGTH;
503 con->http.data_remaining = 0;
504 con->operation = HTTP_WAITING;
505 con->bytes = 0;
506 con->file = 0;
507 con->pipe_pid = 0;
508 con->command[0] = '\0';
509 con->username[0] = '\0';
510 con->password[0] = '\0';
511 con->uri[0] = '\0';
512
513 if (con->language != NULL)
514 {
515 cupsLangFree(con->language);
516 con->language = NULL;
517 }
518
519 /*
520 * Grab the request line...
521 */
522
523 switch (sscanf(line, "%63s%1023s%63s", operation, con->uri, version))
524 {
525 case 1 :
526 SendError(con, HTTP_BAD_REQUEST);
527 CloseClient(con);
528 return (0);
529 case 2 :
530 con->http.version = HTTP_0_9;
531 break;
532 case 3 :
533 if (sscanf(version, "HTTP/%d.%d", &major, &minor) != 2)
534 {
535 SendError(con, HTTP_BAD_REQUEST);
536 CloseClient(con);
537 return (0);
538 }
539
540 if (major < 2)
541 {
542 con->http.version = (http_version_t)(major * 100 + minor);
543 if (con->http.version == HTTP_1_1 && KeepAlive)
544 con->http.keep_alive = HTTP_KEEPALIVE_ON;
545 else
546 con->http.keep_alive = HTTP_KEEPALIVE_OFF;
547 }
548 else
549 {
550 SendError(con, HTTP_NOT_SUPPORTED);
551 CloseClient(con);
552 return (0);
553 }
554 break;
555 }
556
557 /*
558 * Process the request...
559 */
560
561 if (strcmp(operation, "GET") == 0)
562 con->http.state = HTTP_GET;
563 else if (strcmp(operation, "PUT") == 0)
564 con->http.state = HTTP_PUT;
565 else if (strcmp(operation, "POST") == 0)
566 con->http.state = HTTP_POST;
567 else if (strcmp(operation, "DELETE") == 0)
568 con->http.state = HTTP_DELETE;
569 else if (strcmp(operation, "TRACE") == 0)
570 con->http.state = HTTP_TRACE;
571 else if (strcmp(operation, "OPTIONS") == 0)
572 con->http.state = HTTP_OPTIONS;
573 else if (strcmp(operation, "HEAD") == 0)
574 con->http.state = HTTP_HEAD;
575 else
576 {
577 SendError(con, HTTP_BAD_REQUEST);
578 CloseClient(con);
579 return (0);
580 }
581
582 con->start = time(NULL);
583 con->operation = con->http.state;
584
585 LogMessage(L_DEBUG, "ReadClient() %d %s %s HTTP/%d.%d", con->http.fd,
586 operation, con->uri,
587 con->http.version / 100, con->http.version % 100);
588
589 con->http.status = HTTP_OK;
590
591 case HTTP_OPTIONS :
592 case HTTP_DELETE :
593 case HTTP_GET :
594 case HTTP_HEAD :
595 case HTTP_POST :
596 case HTTP_PUT :
597 case HTTP_TRACE :
598 /*
599 * Parse incoming parameters until the status changes...
600 */
601
602 status = httpUpdate(HTTP(con));
603
604 if (status != HTTP_OK && status != HTTP_CONTINUE)
605 {
606 SendError(con, HTTP_BAD_REQUEST);
607 CloseClient(con);
608 return (0);
609 }
610 break;
611
612 default :
613 break; /* Anti-compiler-warning-code */
614 }
615
616 /*
617 * Handle new transfers...
618 */
619
620 if (status == HTTP_OK)
621 {
622 con->language = cupsLangGet(con->http.fields[HTTP_FIELD_ACCEPT_LANGUAGE]);
623
624 decode_auth(con);
625
626 if (strncmp(con->http.fields[HTTP_FIELD_CONNECTION], "Keep-Alive", 10) == 0 &&
627 KeepAlive)
628 con->http.keep_alive = HTTP_KEEPALIVE_ON;
629
630 if (con->http.fields[HTTP_FIELD_HOST][0] == '\0' &&
631 con->http.version >= HTTP_1_1)
632 {
633 /*
634 * HTTP/1.1 and higher require the "Host:" field...
635 */
636
637 if (!SendError(con, HTTP_BAD_REQUEST))
638 {
639 CloseClient(con);
640 return (0);
641 }
642 }
643 else if (con->operation == HTTP_OPTIONS)
644 {
645 /*
646 * Do OPTIONS command...
647 */
648
649 if ((best = FindBest(con->uri, con->http.state)) != NULL &&
650 best->type != AUTH_NONE)
651 {
652 if (!SendHeader(con, HTTP_UNAUTHORIZED, NULL))
653 {
654 CloseClient(con);
655 return (0);
656 }
657 }
658
659 if (strcasecmp(con->http.fields[HTTP_FIELD_CONNECTION], "Upgrade") == 0 &&
660 con->http.tls == NULL)
661 {
662 #ifdef HAVE_LIBSSL
663 /*
664 * Do encryption stuff...
665 */
666
667 if (!SendHeader(con, HTTP_SWITCHING_PROTOCOLS, NULL))
668 {
669 CloseClient(con);
670 return (0);
671 }
672
673 httpPrintf(HTTP(con), "Connection: Upgrade\r\n");
674 httpPrintf(HTTP(con), "Upgrade: TLS/1.0,HTTP/1.1\r\n");
675 httpPrintf(HTTP(con), "Content-Length: 0\r\n");
676 httpPrintf(HTTP(con), "\r\n");
677
678 EncryptClient(con);
679 #else
680 if (!SendError(con, HTTP_NOT_IMPLEMENTED))
681 {
682 CloseClient(con);
683 return (0);
684 }
685 #endif /* HAVE_LIBSSL */
686 }
687
688 if (!SendHeader(con, HTTP_OK, NULL))
689 {
690 CloseClient(con);
691 return (0);
692 }
693
694 httpPrintf(HTTP(con), "Allow: GET, HEAD, OPTIONS, POST, PUT\r\n");
695 httpPrintf(HTTP(con), "Content-Length: 0\r\n");
696 httpPrintf(HTTP(con), "\r\n");
697 }
698 else if (strstr(con->uri, "..") != NULL)
699 {
700 /*
701 * Protect against malicious users!
702 */
703
704 if (!SendError(con, HTTP_FORBIDDEN))
705 {
706 CloseClient(con);
707 return (0);
708 }
709 }
710 else if (con->uri[0] != '/')
711 {
712 /*
713 * Don't allow proxying (yet)...
714 */
715
716 if (!SendError(con, HTTP_METHOD_NOT_ALLOWED))
717 {
718 CloseClient(con);
719 return (0);
720 }
721 }
722 else
723 {
724 if (strcasecmp(con->http.fields[HTTP_FIELD_CONNECTION], "Upgrade") == 0 &&
725 con->http.tls == NULL)
726 {
727 #ifdef HAVE_LIBSSL
728 /*
729 * Do encryption stuff...
730 */
731
732 if (!SendHeader(con, HTTP_SWITCHING_PROTOCOLS, NULL))
733 {
734 CloseClient(con);
735 return (0);
736 }
737
738 httpPrintf(HTTP(con), "Connection: Upgrade\r\n");
739 httpPrintf(HTTP(con), "Upgrade: TLS/1.0,HTTP/1.1\r\n");
740 httpPrintf(HTTP(con), "Content-Length: 0\r\n");
741 httpPrintf(HTTP(con), "\r\n");
742
743 EncryptClient(con);
744
745 status = IsAuthorized(con);
746 #else
747 if (!SendError(con, HTTP_NOT_IMPLEMENTED))
748 {
749 CloseClient(con);
750 return (0);
751 }
752 #endif /* HAVE_LIBSSL */
753 }
754
755 if (status != HTTP_OK)
756 {
757 SendError(con, status);
758 CloseClient(con);
759 return (0);
760 }
761
762 switch (con->http.state)
763 {
764 case HTTP_GET_SEND :
765 if (strncmp(con->uri, "/printers/", 10) == 0 &&
766 strcmp(con->uri + strlen(con->uri) - 4, ".ppd") == 0)
767 {
768 /*
769 * Send PPD file - get the real printer name since printer
770 * names are not case sensitive but filenames can be...
771 */
772
773 con->uri[strlen(con->uri) - 4] = '\0'; /* Drop ".ppd" */
774
775 if ((p = FindPrinter(con->uri + 10)) != NULL)
776 snprintf(con->uri, sizeof(con->uri), "/ppd/%s.ppd", p->name);
777 else
778 {
779 if (!SendError(con, HTTP_NOT_FOUND))
780 {
781 CloseClient(con);
782 return (0);
783 }
784
785 break;
786 }
787 }
788
789 if ((strncmp(con->uri, "/admin", 6) == 0 &&
790 strncmp(con->uri, "/admin/conf/", 12) != 0) ||
791 strncmp(con->uri, "/printers", 9) == 0 ||
792 strncmp(con->uri, "/classes", 8) == 0 ||
793 strncmp(con->uri, "/jobs", 5) == 0)
794 {
795 /*
796 * Send CGI output...
797 */
798
799 if (strncmp(con->uri, "/admin", 6) == 0)
800 {
801 snprintf(con->command, sizeof(con->command),
802 "%s/cgi-bin/admin.cgi", ServerBin);
803 con->options = con->uri + 6;
804 }
805 else if (strncmp(con->uri, "/printers", 9) == 0)
806 {
807 snprintf(con->command, sizeof(con->command),
808 "%s/cgi-bin/printers.cgi", ServerBin);
809 con->options = con->uri + 9;
810 }
811 else if (strncmp(con->uri, "/classes", 8) == 0)
812 {
813 snprintf(con->command, sizeof(con->command),
814 "%s/cgi-bin/classes.cgi", ServerBin);
815 con->options = con->uri + 8;
816 }
817 else
818 {
819 snprintf(con->command, sizeof(con->command),
820 "%s/cgi-bin/jobs.cgi", ServerBin);
821 con->options = con->uri + 5;
822 }
823
824 if (con->options[0] == '/')
825 con->options ++;
826
827 if (!SendCommand(con, con->command, con->options))
828 {
829 if (!SendError(con, HTTP_NOT_FOUND))
830 {
831 CloseClient(con);
832 return (0);
833 }
834 }
835 else
836 LogRequest(con, HTTP_OK);
837
838 if (con->http.version <= HTTP_1_0)
839 con->http.keep_alive = HTTP_KEEPALIVE_OFF;
840 }
841 else if (strncmp(con->uri, "/admin/conf/", 12) == 0 &&
842 (strchr(con->uri + 12, '/') != NULL ||
843 strlen(con->uri) == 12))
844 {
845 /*
846 * GET can only be done to configuration files under
847 * /admin/conf...
848 */
849
850 if (!SendError(con, HTTP_FORBIDDEN))
851 {
852 CloseClient(con);
853 return (0);
854 }
855
856 break;
857 }
858 else
859 {
860 /*
861 * Serve a file...
862 */
863
864 if ((filename = get_file(con, &filestats)) == NULL)
865 {
866 if (!SendError(con, HTTP_NOT_FOUND))
867 {
868 CloseClient(con);
869 return (0);
870 }
871 }
872 else if (!check_if_modified(con, &filestats))
873 {
874 if (!SendError(con, HTTP_NOT_MODIFIED))
875 {
876 CloseClient(con);
877 return (0);
878 }
879 }
880 else
881 {
882 type = mimeFileType(MimeDatabase, filename);
883 if (type == NULL)
884 strcpy(line, "text/plain");
885 else
886 snprintf(line, sizeof(line), "%s/%s", type->super, type->type);
887
888 if (!SendFile(con, HTTP_OK, filename, line, &filestats))
889 {
890 CloseClient(con);
891 return (0);
892 }
893 }
894 }
895 break;
896
897 case HTTP_POST_RECV :
898 /*
899 * See if the POST request includes a Content-Length field, and if
900 * so check the length against any limits that are set...
901 */
902
903 LogMessage(L_DEBUG2, "POST %s", con->uri);
904 LogMessage(L_DEBUG2, "CONTENT_TYPE = %s", con->http.fields[HTTP_FIELD_CONTENT_TYPE]);
905
906 if (con->http.fields[HTTP_FIELD_CONTENT_LENGTH][0] &&
907 atoi(con->http.fields[HTTP_FIELD_CONTENT_LENGTH]) > MaxRequestSize &&
908 MaxRequestSize > 0)
909 {
910 /*
911 * Request too large...
912 */
913
914 if (!SendError(con, HTTP_REQUEST_TOO_LARGE))
915 {
916 CloseClient(con);
917 return (0);
918 }
919
920 break;
921 }
922
923 /*
924 * See what kind of POST request this is; for IPP requests the
925 * content-type field will be "application/ipp"...
926 */
927
928 if (strcmp(con->http.fields[HTTP_FIELD_CONTENT_TYPE], "application/ipp") == 0)
929 con->request = ippNew();
930 else if ((strncmp(con->uri, "/admin", 6) == 0 &&
931 strncmp(con->uri, "/admin/conf/", 12) != 0) ||
932 strncmp(con->uri, "/printers", 9) == 0 ||
933 strncmp(con->uri, "/classes", 8) == 0 ||
934 strncmp(con->uri, "/jobs", 5) == 0)
935 {
936 /*
937 * CGI request...
938 */
939
940 if (strncmp(con->uri, "/admin", 6) == 0)
941 {
942 snprintf(con->command, sizeof(con->command),
943 "%s/cgi-bin/admin.cgi", ServerBin);
944 con->options = con->uri + 6;
945 }
946 else if (strncmp(con->uri, "/printers", 9) == 0)
947 {
948 snprintf(con->command, sizeof(con->command),
949 "%s/cgi-bin/printers.cgi", ServerBin);
950 con->options = con->uri + 9;
951 }
952 else if (strncmp(con->uri, "/classes", 8) == 0)
953 {
954 snprintf(con->command, sizeof(con->command),
955 "%s/cgi-bin/classes.cgi", ServerBin);
956 con->options = con->uri + 8;
957 }
958 else
959 {
960 snprintf(con->command, sizeof(con->command),
961 "%s/cgi-bin/jobs.cgi", ServerBin);
962 con->options = con->uri + 5;
963 }
964
965 if (con->options[0] == '/')
966 con->options ++;
967
968 LogMessage(L_DEBUG2, "ReadClient() %d command=\"%s\", options = \"%s\"",
969 con->http.fd, con->command, con->options);
970
971 if (con->http.version <= HTTP_1_0)
972 con->http.keep_alive = HTTP_KEEPALIVE_OFF;
973 }
974 else if (!SendError(con, HTTP_UNAUTHORIZED))
975 {
976 CloseClient(con);
977 return (0);
978 }
979 break;
980
981 case HTTP_PUT_RECV :
982 /*
983 * Validate the resource name...
984 */
985
986 if (strncmp(con->uri, "/admin/conf/", 12) != 0 ||
987 strchr(con->uri + 12, '/') != NULL ||
988 strlen(con->uri) == 12)
989 {
990 /*
991 * PUT can only be done to configuration files under
992 * /admin/conf...
993 */
994
995 if (!SendError(con, HTTP_FORBIDDEN))
996 {
997 CloseClient(con);
998 return (0);
999 }
1000
1001 break;
1002 }
1003
1004 /*
1005 * See if the PUT request includes a Content-Length field, and if
1006 * so check the length against any limits that are set...
1007 */
1008
1009 LogMessage(L_DEBUG2, "PUT %s", con->uri);
1010 LogMessage(L_DEBUG2, "CONTENT_TYPE = %s", con->http.fields[HTTP_FIELD_CONTENT_TYPE]);
1011
1012 if (con->http.fields[HTTP_FIELD_CONTENT_LENGTH][0] &&
1013 atoi(con->http.fields[HTTP_FIELD_CONTENT_LENGTH]) > MaxRequestSize &&
1014 MaxRequestSize > 0)
1015 {
1016 /*
1017 * Request too large...
1018 */
1019
1020 if (!SendError(con, HTTP_REQUEST_TOO_LARGE))
1021 {
1022 CloseClient(con);
1023 return (0);
1024 }
1025
1026 break;
1027 }
1028
1029 /*
1030 * Open a temporary file to hold the request...
1031 */
1032
1033 snprintf(con->filename, sizeof(con->filename), "%s/%08x",
1034 RequestRoot, request_id ++);
1035 con->file = open(con->filename, O_WRONLY | O_CREAT | O_TRUNC, 0640);
1036 fchmod(con->file, 0640);
1037 fchown(con->file, User, Group);
1038
1039 LogMessage(L_DEBUG2, "ReadClient() %d REQUEST %s=%d", con->http.fd,
1040 con->filename, con->file);
1041
1042 if (con->file < 0)
1043 {
1044 if (!SendError(con, HTTP_REQUEST_TOO_LARGE))
1045 {
1046 CloseClient(con);
1047 return (0);
1048 }
1049 }
1050 break;
1051
1052 case HTTP_DELETE :
1053 case HTTP_TRACE :
1054 SendError(con, HTTP_NOT_IMPLEMENTED);
1055 CloseClient(con);
1056 return (0);
1057
1058 case HTTP_HEAD :
1059 if (strncmp(con->uri, "/printers/", 10) == 0 &&
1060 strcmp(con->uri + strlen(con->uri) - 4, ".ppd") == 0)
1061 {
1062 /*
1063 * Send PPD file - get the real printer name since printer
1064 * names are not case sensitive but filenames can be...
1065 */
1066
1067 con->uri[strlen(con->uri) - 4] = '\0'; /* Drop ".ppd" */
1068
1069 if ((p = FindPrinter(con->uri + 10)) != NULL)
1070 snprintf(con->uri, sizeof(con->uri), "/ppd/%s.ppd", p->name);
1071 else
1072 {
1073 if (!SendError(con, HTTP_NOT_FOUND))
1074 {
1075 CloseClient(con);
1076 return (0);
1077 }
1078
1079 break;
1080 }
1081 }
1082
1083 if ((strncmp(con->uri, "/admin/", 7) == 0 &&
1084 strncmp(con->uri, "/admin/conf/", 12) != 0) ||
1085 strncmp(con->uri, "/printers/", 10) == 0 ||
1086 strncmp(con->uri, "/classes/", 9) == 0 ||
1087 strncmp(con->uri, "/jobs/", 6) == 0)
1088 {
1089 /*
1090 * CGI output...
1091 */
1092
1093 if (!SendHeader(con, HTTP_OK, "text/html"))
1094 {
1095 CloseClient(con);
1096 return (0);
1097 }
1098
1099 if (httpPrintf(HTTP(con), "\r\n") < 0)
1100 {
1101 CloseClient(con);
1102 return (0);
1103 }
1104
1105 LogRequest(con, HTTP_OK);
1106 }
1107 else if (strncmp(con->uri, "/admin/conf/", 12) == 0 &&
1108 (strchr(con->uri + 12, '/') != NULL ||
1109 strlen(con->uri) == 12))
1110 {
1111 /*
1112 * HEAD can only be done to configuration files under
1113 * /admin/conf...
1114 */
1115
1116 if (!SendError(con, HTTP_FORBIDDEN))
1117 {
1118 CloseClient(con);
1119 return (0);
1120 }
1121
1122 break;
1123 }
1124 else if ((filename = get_file(con, &filestats)) == NULL)
1125 {
1126 if (!SendHeader(con, HTTP_NOT_FOUND, "text/html"))
1127 {
1128 CloseClient(con);
1129 return (0);
1130 }
1131
1132 LogRequest(con, HTTP_NOT_FOUND);
1133 }
1134 else if (!check_if_modified(con, &filestats))
1135 {
1136 if (!SendError(con, HTTP_NOT_MODIFIED))
1137 {
1138 CloseClient(con);
1139 return (0);
1140 }
1141
1142 LogRequest(con, HTTP_NOT_MODIFIED);
1143 }
1144 else
1145 {
1146 /*
1147 * Serve a file...
1148 */
1149
1150 type = mimeFileType(MimeDatabase, filename);
1151 if (type == NULL)
1152 strcpy(line, "text/plain");
1153 else
1154 snprintf(line, sizeof(line), "%s/%s", type->super, type->type);
1155
1156 if (!SendHeader(con, HTTP_OK, line))
1157 {
1158 CloseClient(con);
1159 return (0);
1160 }
1161
1162 if (httpPrintf(HTTP(con), "Last-Modified: %s\r\n",
1163 httpGetDateString(filestats.st_mtime)) < 0)
1164 {
1165 CloseClient(con);
1166 return (0);
1167 }
1168
1169 if (httpPrintf(HTTP(con), "Content-Length: %lu\r\n",
1170 (unsigned long)filestats.st_size) < 0)
1171 {
1172 CloseClient(con);
1173 return (0);
1174 }
1175
1176 LogRequest(con, HTTP_OK);
1177 }
1178
1179 if (httpPrintf(HTTP(con), "\r\n") < 0)
1180 {
1181 CloseClient(con);
1182 return (0);
1183 }
1184
1185 con->http.state = HTTP_WAITING;
1186 break;
1187
1188 default :
1189 break; /* Anti-compiler-warning-code */
1190 }
1191 }
1192 }
1193
1194 /*
1195 * Handle any incoming data...
1196 */
1197
1198 switch (con->http.state)
1199 {
1200 case HTTP_PUT_RECV :
1201 LogMessage(L_DEBUG2, "ReadClient() %d con->data_encoding = %s, con->data_remaining = %d, con->file = %d",
1202 con->http.fd,
1203 con->http.data_encoding == HTTP_ENCODE_CHUNKED ? "chunked" : "length",
1204 con->http.data_remaining, con->file);
1205
1206 if ((bytes = httpRead(HTTP(con), line, sizeof(line))) < 0)
1207 {
1208 CloseClient(con);
1209 return (0);
1210 }
1211 else if (bytes > 0)
1212 {
1213 con->bytes += bytes;
1214
1215 LogMessage(L_DEBUG2, "ReadClient() %d writing %d bytes to %d",
1216 con->http.fd, bytes, con->file);
1217
1218 if (write(con->file, line, bytes) < bytes)
1219 {
1220 LogMessage(L_ERROR, "ReadClient: Unable to write %d bytes to %s: %s",
1221 bytes, con->filename, strerror(errno));
1222
1223 LogMessage(L_DEBUG2, "ReadClient: Closing data file %d...",
1224 con->file);
1225
1226 close(con->file);
1227 con->file = 0;
1228 unlink(con->filename);
1229 con->filename[0] = '\0';
1230
1231 if (!SendError(con, HTTP_REQUEST_TOO_LARGE))
1232 {
1233 CloseClient(con);
1234 return (0);
1235 }
1236 }
1237 }
1238
1239 if (con->http.state == HTTP_WAITING)
1240 {
1241 /*
1242 * End of file, see how big it is...
1243 */
1244
1245 fstat(con->file, &filestats);
1246
1247 LogMessage(L_DEBUG2, "ReadClient() %d Closing data file %d, size = %d.",
1248 con->http.fd, con->file, (int)filestats.st_size);
1249
1250 close(con->file);
1251 con->file = 0;
1252
1253 if (filestats.st_size > MaxRequestSize &&
1254 MaxRequestSize > 0)
1255 {
1256 /*
1257 * Request is too big; remove it and send an error...
1258 */
1259
1260 LogMessage(L_DEBUG2, "ReadClient() %d Removing temp file %s",
1261 con->http.fd, con->filename);
1262 unlink(con->filename);
1263 con->filename[0] = '\0';
1264
1265 if (!SendError(con, HTTP_REQUEST_TOO_LARGE))
1266 {
1267 CloseClient(con);
1268 return (0);
1269 }
1270 }
1271
1272 /*
1273 * Install the configuration file...
1274 */
1275
1276 status = install_conf_file(con);
1277
1278 /*
1279 * Return the status to the client...
1280 */
1281
1282 if (!SendError(con, status))
1283 {
1284 CloseClient(con);
1285 return (0);
1286 }
1287 }
1288 break;
1289
1290 case HTTP_POST_RECV :
1291 LogMessage(L_DEBUG2, "ReadClient() %d con->data_encoding = %s, con->data_remaining = %d, con->file = %d",
1292 con->http.fd,
1293 con->http.data_encoding == HTTP_ENCODE_CHUNKED ? "chunked" : "length",
1294 con->http.data_remaining, con->file);
1295
1296 if (con->request != NULL)
1297 {
1298 /*
1299 * Grab any request data from the connection...
1300 */
1301
1302 if ((ipp_state = ippRead(&(con->http), con->request)) == IPP_ERROR)
1303 {
1304 LogMessage(L_ERROR, "ReadClient() %d IPP Read Error!",
1305 con->http.fd);
1306 CloseClient(con);
1307 return (0);
1308 }
1309 else if (ipp_state != IPP_DATA)
1310 break;
1311 else
1312 con->bytes += ippLength(con->request);
1313 }
1314
1315 if (con->file == 0 && con->http.state != HTTP_POST_SEND)
1316 {
1317 /*
1318 * Create a file as needed for the request data...
1319 */
1320
1321 snprintf(con->filename, sizeof(con->filename), "%s/%08x",
1322 RequestRoot, request_id ++);
1323 con->file = open(con->filename, O_WRONLY | O_CREAT | O_TRUNC, 0640);
1324 fchmod(con->file, 0640);
1325 fchown(con->file, User, Group);
1326
1327 LogMessage(L_DEBUG2, "ReadClient() %d REQUEST %s=%d", con->http.fd,
1328 con->filename, con->file);
1329
1330 if (con->file < 0)
1331 {
1332 if (!SendError(con, HTTP_REQUEST_TOO_LARGE))
1333 {
1334 CloseClient(con);
1335 return (0);
1336 }
1337 }
1338 }
1339
1340 if (con->http.state != HTTP_POST_SEND)
1341 {
1342 if ((bytes = httpRead(HTTP(con), line, sizeof(line))) < 0)
1343 {
1344 CloseClient(con);
1345 return (0);
1346 }
1347 else if (bytes > 0)
1348 {
1349 con->bytes += bytes;
1350
1351 LogMessage(L_DEBUG2, "ReadClient() %d writing %d bytes to %d",
1352 con->http.fd, bytes, con->file);
1353
1354 if (write(con->file, line, bytes) < bytes)
1355 {
1356 LogMessage(L_ERROR, "ReadClient: Unable to write %d bytes to %s: %s",
1357 bytes, con->filename, strerror(errno));
1358
1359 LogMessage(L_DEBUG2, "ReadClient: Closing file %d...",
1360 con->file);
1361
1362 close(con->file);
1363 con->file = 0;
1364 unlink(con->filename);
1365 con->filename[0] = '\0';
1366
1367 if (!SendError(con, HTTP_REQUEST_TOO_LARGE))
1368 {
1369 CloseClient(con);
1370 return (0);
1371 }
1372 }
1373 }
1374 else if (con->http.state != HTTP_POST_SEND)
1375 {
1376 CloseClient(con);
1377 return (0);
1378 }
1379 }
1380
1381 if (con->http.state == HTTP_POST_SEND)
1382 {
1383 if (con->file)
1384 {
1385 fstat(con->file, &filestats);
1386
1387 LogMessage(L_DEBUG2, "ReadClient() %d Closing data file %d, size = %d.",
1388 con->http.fd, con->file, (int)filestats.st_size);
1389
1390 close(con->file);
1391 con->file = 0;
1392
1393 if (filestats.st_size > MaxRequestSize &&
1394 MaxRequestSize > 0)
1395 {
1396 /*
1397 * Request is too big; remove it and send an error...
1398 */
1399
1400 LogMessage(L_DEBUG2, "ReadClient() %d Removing temp file %s",
1401 con->http.fd, con->filename);
1402 unlink(con->filename);
1403 con->filename[0] = '\0';
1404
1405 if (con->request)
1406 {
1407 /*
1408 * Delete any IPP request data...
1409 */
1410
1411 ippDelete(con->request);
1412 con->request = NULL;
1413 }
1414
1415 if (!SendError(con, HTTP_REQUEST_TOO_LARGE))
1416 {
1417 CloseClient(con);
1418 return (0);
1419 }
1420 }
1421
1422 if (con->command[0])
1423 {
1424 if (!SendCommand(con, con->command, con->options))
1425 {
1426 if (!SendError(con, HTTP_NOT_FOUND))
1427 {
1428 CloseClient(con);
1429 return (0);
1430 }
1431 }
1432 else
1433 LogRequest(con, HTTP_OK);
1434 }
1435 }
1436
1437 if (con->request)
1438 ProcessIPPRequest(con);
1439 }
1440 break;
1441
1442 default :
1443 break; /* Anti-compiler-warning-code */
1444 }
1445
1446 if (!con->http.keep_alive && con->http.state == HTTP_WAITING)
1447 {
1448 CloseClient(con);
1449 return (0);
1450 }
1451 else
1452 return (1);
1453 }
1454
1455
1456 /*
1457 * 'SendCommand()' - Send output from a command via HTTP.
1458 */
1459
1460 int
1461 SendCommand(client_t *con,
1462 char *command,
1463 char *options)
1464 {
1465 int fd;
1466
1467
1468 if (con->filename[0])
1469 fd = open(con->filename, O_RDONLY);
1470 else
1471 fd = open("/dev/null", O_RDONLY);
1472
1473 con->pipe_pid = pipe_command(con, fd, &(con->file), command, options);
1474
1475 close(fd);
1476
1477 LogMessage(L_INFO, "Started \"%s\" (pid=%d)", command, con->pipe_pid);
1478
1479 LogMessage(L_DEBUG, "SendCommand() %d file=%d", con->http.fd, con->file);
1480
1481 if (con->pipe_pid == 0)
1482 return (0);
1483
1484 fcntl(con->file, F_SETFD, fcntl(con->file, F_GETFD) | FD_CLOEXEC);
1485
1486 LogMessage(L_DEBUG2, "SendCommand: Adding fd %d to InputSet...", con->file);
1487 LogMessage(L_DEBUG2, "SendCommand: Adding fd %d to OutputSet...",
1488 con->http.fd);
1489
1490 FD_SET(con->file, &InputSet);
1491 FD_SET(con->http.fd, &OutputSet);
1492
1493 if (!SendHeader(con, HTTP_OK, NULL))
1494 return (0);
1495
1496 if (con->http.version == HTTP_1_1)
1497 {
1498 con->http.data_encoding = HTTP_ENCODE_CHUNKED;
1499
1500 if (httpPrintf(HTTP(con), "Transfer-Encoding: chunked\r\n") < 0)
1501 return (0);
1502 }
1503
1504 con->got_fields = 0;
1505 con->field_col = 0;
1506
1507 return (1);
1508 }
1509
1510
1511 /*
1512 * 'SendError()' - Send an error message via HTTP.
1513 */
1514
1515 int /* O - 1 if successful, 0 otherwise */
1516 SendError(client_t *con, /* I - Connection */
1517 http_status_t code) /* I - Error code */
1518 {
1519 char message[1024]; /* Message for user */
1520
1521
1522 /*
1523 * Put the request in the access_log file...
1524 */
1525
1526 if (con->operation > HTTP_WAITING)
1527 LogRequest(con, code);
1528
1529 LogMessage(L_DEBUG, "SendError() %d code=%d", con->http.fd, code);
1530
1531 /*
1532 * To work around bugs in some proxies, don't use Keep-Alive for some
1533 * error messages...
1534 */
1535
1536 if (code >= HTTP_BAD_REQUEST)
1537 con->http.keep_alive = HTTP_KEEPALIVE_OFF;
1538
1539 /*
1540 * Send an error message back to the client. If the error code is a
1541 * 400 or 500 series, make sure the message contains some text, too!
1542 */
1543
1544 if (!SendHeader(con, code, NULL))
1545 return (0);
1546
1547 #ifdef HAVE_LIBSSL
1548 if (code == HTTP_UPGRADE_REQUIRED)
1549 if (httpPrintf(HTTP(con), "Connection: Upgrade\r\n") < 0)
1550 return (0);
1551
1552 if (httpPrintf(HTTP(con), "Upgrade: TLS/1.0,HTTP/1.1\r\n") < 0)
1553 return (0);
1554 #endif /* HAVE_LIBSSL */
1555
1556 if (con->http.version >= HTTP_1_1 && !con->http.keep_alive)
1557 {
1558 if (httpPrintf(HTTP(con), "Connection: close\r\n") < 0)
1559 return (0);
1560 }
1561
1562 if (code >= HTTP_BAD_REQUEST)
1563 {
1564 /*
1565 * Send a human-readable error message.
1566 */
1567
1568 snprintf(message, sizeof(message),
1569 "<HTML><HEAD><TITLE>%d %s</TITLE></HEAD>"
1570 "<BODY><H1>%s</H1>%s</BODY></HTML>\n",
1571 code, httpStatus(code), httpStatus(code),
1572 con->language ? con->language->messages[code] :
1573 httpStatus(code));
1574
1575 if (httpPrintf(HTTP(con), "Content-Type: text/html\r\n") < 0)
1576 return (0);
1577 if (httpPrintf(HTTP(con), "Content-Length: %d\r\n", strlen(message)) < 0)
1578 return (0);
1579 if (httpPrintf(HTTP(con), "\r\n") < 0)
1580 return (0);
1581 if (httpPrintf(HTTP(con), "%s", message) < 0)
1582 return (0);
1583 }
1584 else if (httpPrintf(HTTP(con), "\r\n") < 0)
1585 return (0);
1586
1587 con->http.state = HTTP_WAITING;
1588
1589 return (1);
1590 }
1591
1592
1593 /*
1594 * 'SendFile()' - Send a file via HTTP.
1595 */
1596
1597 int
1598 SendFile(client_t *con,
1599 http_status_t code,
1600 char *filename,
1601 char *type,
1602 struct stat *filestats)
1603 {
1604 con->file = open(filename, O_RDONLY);
1605
1606 LogMessage(L_DEBUG, "SendFile() %d file=%d", con->http.fd, con->file);
1607
1608 if (con->file < 0)
1609 return (0);
1610
1611 fcntl(con->file, F_SETFD, fcntl(con->file, F_GETFD) | FD_CLOEXEC);
1612
1613 con->pipe_pid = 0;
1614
1615 if (!SendHeader(con, code, type))
1616 return (0);
1617
1618 if (httpPrintf(HTTP(con), "Last-Modified: %s\r\n", httpGetDateString(filestats->st_mtime)) < 0)
1619 return (0);
1620 if (httpPrintf(HTTP(con), "Content-Length: %lu\r\n",
1621 (unsigned long)filestats->st_size) < 0)
1622 return (0);
1623 if (httpPrintf(HTTP(con), "\r\n") < 0)
1624 return (0);
1625
1626 LogMessage(L_DEBUG2, "SendFile: Adding fd %d to OutputSet...", con->http.fd);
1627
1628 FD_SET(con->http.fd, &OutputSet);
1629
1630 return (1);
1631 }
1632
1633
1634 /*
1635 * 'SendHeader()' - Send an HTTP request.
1636 */
1637
1638 int /* O - 1 on success, 0 on failure */
1639 SendHeader(client_t *con, /* I - Client to send to */
1640 http_status_t code, /* I - HTTP status code */
1641 char *type) /* I - MIME type of document */
1642 {
1643 location_t *loc; /* Authentication location */
1644
1645
1646 if (httpPrintf(HTTP(con), "HTTP/%d.%d %d %s\r\n", con->http.version / 100,
1647 con->http.version % 100, code, httpStatus(code)) < 0)
1648 return (0);
1649 if (httpPrintf(HTTP(con), "Date: %s\r\n", httpGetDateString(time(NULL))) < 0)
1650 return (0);
1651 if (httpPrintf(HTTP(con), "Server: CUPS/1.1\r\n") < 0)
1652 return (0);
1653 if (con->http.keep_alive && con->http.version >= HTTP_1_0)
1654 {
1655 if (httpPrintf(HTTP(con), "Connection: Keep-Alive\r\n") < 0)
1656 return (0);
1657 if (httpPrintf(HTTP(con), "Keep-Alive: timeout=%d\r\n", KeepAliveTimeout) < 0)
1658 return (0);
1659 }
1660 if (code == HTTP_METHOD_NOT_ALLOWED)
1661 if (httpPrintf(HTTP(con), "Allow: GET, HEAD, OPTIONS, POST\r\n") < 0)
1662 return (0);
1663
1664 if (code == HTTP_UNAUTHORIZED)
1665 {
1666 /*
1667 * This already succeeded in IsAuthorized...
1668 */
1669
1670 loc = FindBest(con->uri, con->http.state);
1671
1672 if (loc->type != AUTH_DIGEST)
1673 {
1674 if (httpPrintf(HTTP(con), "WWW-Authenticate: Basic realm=\"CUPS\"\r\n") < 0)
1675 return (0);
1676 }
1677 else
1678 {
1679 if (httpPrintf(HTTP(con), "WWW-Authenticate: Digest realm=\"CUPS\" "
1680 "nonce=\"%s\"\r\n", con->http.hostname) < 0)
1681 return (0);
1682 }
1683 }
1684 if (con->language != NULL)
1685 {
1686 if (httpPrintf(HTTP(con), "Content-Language: %s\r\n",
1687 con->language->language) < 0)
1688 return (0);
1689
1690 if (type != NULL)
1691 if (httpPrintf(HTTP(con), "Content-Type: %s; charset=%s\r\n", type,
1692 cupsLangEncoding(con->language)) < 0)
1693 return (0);
1694 }
1695 else if (type != NULL)
1696 if (httpPrintf(HTTP(con), "Content-Type: %s\r\n", type) < 0)
1697 return (0);
1698
1699 return (1);
1700 }
1701
1702
1703 /*
1704 * 'WriteClient()' - Write data to a client as needed.
1705 */
1706
1707 int /* O - 1 if success, 0 if fail */
1708 WriteClient(client_t *con) /* I - Client connection */
1709 {
1710 int bytes; /* Number of bytes written */
1711 char buf[HTTP_MAX_BUFFER + 1];/* Data buffer */
1712 char *bufptr; /* Pointer into buffer */
1713 ipp_state_t ipp_state; /* IPP state value */
1714
1715
1716 if (con->http.state != HTTP_GET_SEND &&
1717 con->http.state != HTTP_POST_SEND)
1718 return (1);
1719
1720 if (con->response != NULL)
1721 {
1722 ipp_state = ippWrite(&(con->http), con->response);
1723 bytes = ipp_state != IPP_ERROR && ipp_state != IPP_DATA;
1724 }
1725 else if ((bytes = read(con->file, buf, HTTP_MAX_BUFFER)) > 0)
1726 {
1727 if (con->pipe_pid && !con->got_fields)
1728 {
1729 /*
1730 * Inspect the data for Content-Type and other fields.
1731 */
1732
1733 buf[bytes] = '\0';
1734
1735 for (bufptr = buf; !con->got_fields && *bufptr; bufptr ++)
1736 if (*bufptr == '\n')
1737 {
1738 /*
1739 * Send line to client...
1740 */
1741
1742 if (bufptr > buf && bufptr[-1] == '\r')
1743 bufptr[-1] = '\0';
1744 *bufptr++ = '\0';
1745
1746 httpPrintf(HTTP(con), "%s\r\n", buf);
1747 LogMessage(L_DEBUG2, "WriteClient() %d %s", con->http.fd, buf);
1748
1749 /*
1750 * Update buffer...
1751 */
1752
1753 bytes -= (bufptr - buf);
1754 memcpy(buf, bufptr, bytes + 1);
1755 bufptr = buf - 1;
1756
1757 /*
1758 * See if the line was empty...
1759 */
1760
1761 if (con->field_col == 0)
1762 con->got_fields = 1;
1763 else
1764 con->field_col = 0;
1765 }
1766 else if (*bufptr != '\r')
1767 con->field_col ++;
1768
1769 if (bytes > 0 && !con->got_fields)
1770 {
1771 /*
1772 * Remaining text needs to go out...
1773 */
1774
1775 httpPrintf(HTTP(con), "%s", buf);
1776
1777 con->http.activity = time(NULL);
1778 return (1);
1779 }
1780 else if (bytes == 0)
1781 {
1782 con->http.activity = time(NULL);
1783 return (1);
1784 }
1785 }
1786
1787 if (httpWrite(HTTP(con), buf, bytes) < 0)
1788 {
1789 CloseClient(con);
1790 return (0);
1791 }
1792
1793 con->bytes += bytes;
1794 }
1795
1796 if (bytes <= 0)
1797 {
1798 LogRequest(con, HTTP_OK);
1799
1800 if (con->http.data_encoding == HTTP_ENCODE_CHUNKED)
1801 {
1802 if (httpPrintf(HTTP(con), "0\r\n\r\n") < 0)
1803 {
1804 CloseClient(con);
1805 return (0);
1806 }
1807 }
1808
1809 con->http.state = HTTP_WAITING;
1810
1811 LogMessage(L_DEBUG2, "WriteClient() Removing fd %d from OutputSet...",
1812 con->http.fd);
1813
1814 FD_CLR(con->http.fd, &OutputSet);
1815
1816 if (con->file)
1817 {
1818 LogMessage(L_DEBUG2, "WriteClient() Removing fd %d from InputSet...",
1819 con->file);
1820 FD_CLR(con->file, &InputSet);
1821
1822 if (con->pipe_pid)
1823 kill(con->pipe_pid, SIGTERM);
1824
1825 LogMessage(L_DEBUG2, "WriteClient() %d Closing data file %d.",
1826 con->http.fd, con->file);
1827
1828 close(con->file);
1829 con->file = 0;
1830 con->pipe_pid = 0;
1831 }
1832
1833 if (con->filename[0])
1834 {
1835 LogMessage(L_DEBUG2, "WriteClient() %d Removing temp file %s",
1836 con->http.fd, con->filename);
1837 unlink(con->filename);
1838 con->filename[0] = '\0';
1839 }
1840
1841 if (con->request != NULL)
1842 {
1843 ippDelete(con->request);
1844 con->request = NULL;
1845 }
1846
1847 if (con->response != NULL)
1848 {
1849 ippDelete(con->response);
1850 con->response = NULL;
1851 }
1852
1853 if (!con->http.keep_alive)
1854 {
1855 CloseClient(con);
1856 return (0);
1857 }
1858 }
1859
1860 if (bytes >= 1024)
1861 LogMessage(L_DEBUG2, "WriteClient() %d %d bytes", con->http.fd, bytes);
1862
1863 con->http.activity = time(NULL);
1864
1865 return (1);
1866 }
1867
1868
1869 /*
1870 * 'check_if_modified()' - Decode an "If-Modified-Since" line.
1871 */
1872
1873 static int /* O - 1 if modified since */
1874 check_if_modified(client_t *con, /* I - Client connection */
1875 struct stat *filestats) /* I - File information */
1876 {
1877 char *ptr; /* Pointer into field */
1878 time_t date; /* Time/date value */
1879 int size; /* Size/length value */
1880
1881
1882 size = 0;
1883 date = 0;
1884 ptr = con->http.fields[HTTP_FIELD_IF_MODIFIED_SINCE];
1885
1886 if (*ptr == '\0')
1887 return (1);
1888
1889 LogMessage(L_DEBUG2, "check_if_modified() %d If-Modified-Since=\"%s\"",
1890 con->http.fd, ptr);
1891
1892 while (*ptr != '\0')
1893 {
1894 while (isspace(*ptr) || *ptr == ';')
1895 ptr ++;
1896
1897 if (strncasecmp(ptr, "length=", 7) == 0)
1898 {
1899 ptr += 7;
1900 size = atoi(ptr);
1901
1902 while (isdigit(*ptr))
1903 ptr ++;
1904 }
1905 else if (isalpha(*ptr))
1906 {
1907 date = httpGetDateTime(ptr);
1908 while (*ptr != '\0' && *ptr != ';')
1909 ptr ++;
1910 }
1911 }
1912
1913 LogMessage(L_DEBUG2, "check_if_modified() %d sizes=%d,%d dates=%d,%d",
1914 con->http.fd, size, (int)filestats->st_size, (int)date,
1915 (int)filestats->st_mtime);
1916
1917 return ((size != filestats->st_size && size != 0) ||
1918 (date < filestats->st_mtime && date != 0) ||
1919 (size == 0 && date == 0));
1920 }
1921
1922
1923 /*
1924 * 'decode_auth()' - Decode an authorization string.
1925 */
1926
1927 static void
1928 decode_auth(client_t *con) /* I - Client to decode to */
1929 {
1930 char *s, /* Authorization string */
1931 value[1024]; /* Value string */
1932 const char *username; /* Certificate username */
1933
1934
1935 /*
1936 * Decode the string...
1937 */
1938
1939 s = con->http.fields[HTTP_FIELD_AUTHORIZATION];
1940
1941 LogMessage(L_DEBUG2, "decode_auth(%p): Authorization string = \"%s\"",
1942 con, s);
1943
1944 if (strncmp(s, "Basic", 5) == 0)
1945 {
1946 s += 5;
1947 while (isspace(*s))
1948 s ++;
1949
1950 httpDecode64(value, s);
1951
1952 /*
1953 * Pull the username and password out...
1954 */
1955
1956 if ((s = strchr(value, ':')) == NULL)
1957 {
1958 LogMessage(L_DEBUG, "decode_auth() %d no colon in auth string \"%s\"",
1959 con->http.fd, value);
1960 return;
1961 }
1962
1963 *s++ = '\0';
1964
1965 strlcpy(con->username, value, sizeof(con->username));
1966 strlcpy(con->password, s, sizeof(con->password));
1967 }
1968 else if (strncmp(s, "Local", 5) == 0)
1969 {
1970 s += 5;
1971 while (isspace(*s))
1972 s ++;
1973
1974 if ((username = FindCert(s)) != NULL)
1975 strlcpy(con->username, username, sizeof(con->username));
1976 }
1977 else if (strncmp(s, "Digest", 5) == 0)
1978 {
1979 /*
1980 * Get the username and password from the Digest attributes...
1981 */
1982
1983 if (httpGetSubField(&(con->http), HTTP_FIELD_WWW_AUTHENTICATE, "username",
1984 value))
1985 strlcpy(con->username, value, sizeof(con->username));
1986
1987 if (httpGetSubField(&(con->http), HTTP_FIELD_WWW_AUTHENTICATE, "response",
1988 value))
1989 strlcpy(con->password, value, sizeof(con->password) - 1);
1990 }
1991
1992 LogMessage(L_DEBUG2, "decode_auth() %d username=\"%s\"",
1993 con->http.fd, con->username);
1994 }
1995
1996
1997 /*
1998 * 'get_file()' - Get a filename and state info.
1999 */
2000
2001 static char * /* O - Real filename */
2002 get_file(client_t *con, /* I - Client connection */
2003 struct stat *filestats)/* O - File information */
2004 {
2005 int status; /* Status of filesystem calls */
2006 char *params; /* Pointer to parameters in URI */
2007 static char filename[1024]; /* Filename buffer */
2008
2009
2010 /*
2011 * Need to add DocumentRoot global...
2012 */
2013
2014 if (strncmp(con->uri, "/ppd/", 5) == 0)
2015 snprintf(filename, sizeof(filename), "%s%s", ServerRoot, con->uri);
2016 else if (strncmp(con->uri, "/admin/conf/", 12) == 0)
2017 snprintf(filename, sizeof(filename), "%s%s", ServerRoot, con->uri + 11);
2018 else if (con->language != NULL)
2019 snprintf(filename, sizeof(filename), "%s/%s%s", DocumentRoot, con->language->language,
2020 con->uri);
2021 else
2022 snprintf(filename, sizeof(filename), "%s%s", DocumentRoot, con->uri);
2023
2024 if ((params = strchr(filename, '?')) != NULL)
2025 *params = '\0';
2026
2027 /*
2028 * Grab the status for this language; if there isn't a language-specific file
2029 * then fallback to the default one...
2030 */
2031
2032 if ((status = stat(filename, filestats)) != 0 && con->language != NULL)
2033 {
2034 /*
2035 * Drop the language prefix and try the current directory...
2036 */
2037
2038 if (strncmp(con->uri, "/ppd/", 5) != 0 &&
2039 strncmp(con->uri, "/admin/conf/", 12) != 0)
2040 {
2041 snprintf(filename, sizeof(filename), "%s%s", DocumentRoot, con->uri);
2042
2043 status = stat(filename, filestats);
2044 }
2045 }
2046
2047 /*
2048 * If we're found a directory, get the index.html file instead...
2049 */
2050
2051 if (!status && S_ISDIR(filestats->st_mode))
2052 {
2053 if (filename[strlen(filename) - 1] == '/')
2054 strlcat(filename, "index.html", sizeof(filename));
2055 else
2056 strlcat(filename, "/index.html", sizeof(filename));
2057
2058 status = stat(filename, filestats);
2059 }
2060
2061 LogMessage(L_DEBUG2, "get_file() %d filename=%s size=%d",
2062 con->http.fd, filename, status ? -1 : (int)filestats->st_size);
2063
2064 if (status)
2065 return (NULL);
2066 else
2067 return (filename);
2068 }
2069
2070
2071 /*
2072 * 'install_conf_file()' - Install a configuration file.
2073 */
2074
2075 static http_status_t /* O - Status */
2076 install_conf_file(client_t *con) /* I - Connection */
2077 {
2078 FILE *in, /* Input file */
2079 *out; /* Output file */
2080 char buffer[1024]; /* Copy buffer */
2081 int bytes; /* Number of bytes */
2082 char conffile[1024], /* Configuration filename */
2083 newfile[1024], /* New config filename */
2084 oldfile[1024]; /* Old config filename */
2085 struct stat confinfo; /* Config file info */
2086
2087
2088 /*
2089 * First construct the filenames...
2090 */
2091
2092 snprintf(conffile, sizeof(conffile), "%s%s", ServerRoot, con->uri + 11);
2093 snprintf(newfile, sizeof(newfile), "%s%s.N", ServerRoot, con->uri + 11);
2094 snprintf(oldfile, sizeof(oldfile), "%s%s.O", ServerRoot, con->uri + 11);
2095
2096 LogMessage(L_INFO, "Installing config file \"%s\"...", conffile);
2097
2098 /*
2099 * Get the owner, group, and permissions of the configuration file.
2100 * If it doesn't exist, assign it to the User and Group in the
2101 * cupsd.conf file with mode 0640 permissions.
2102 */
2103
2104 if (stat(conffile, &confinfo))
2105 {
2106 confinfo.st_uid = User;
2107 confinfo.st_gid = Group;
2108 confinfo.st_mode = 0640;
2109 }
2110
2111 /*
2112 * Open the request file and new config file...
2113 */
2114
2115 if ((in = fopen(con->filename, "rb")) == NULL)
2116 {
2117 LogMessage(L_ERROR, "Unable to open request file \"%s\" - %s",
2118 con->filename, strerror(errno));
2119 return (HTTP_SERVER_ERROR);
2120 }
2121
2122 if ((out = fopen(newfile, "wb")) == NULL)
2123 {
2124 fclose(in);
2125 LogMessage(L_ERROR, "Unable to open config file \"%s\" - %s",
2126 newfile, strerror(errno));
2127 return (HTTP_SERVER_ERROR);
2128 }
2129
2130 fchmod(fileno(out), confinfo.st_mode);
2131 fchown(fileno(out), confinfo.st_uid, confinfo.st_gid);
2132
2133 /*
2134 * Copy from the request to the new config file...
2135 */
2136
2137 while ((bytes = fread(buffer, 1, sizeof(buffer), in)) > 0)
2138 if (fwrite(buffer, 1, bytes, out) < bytes)
2139 {
2140 LogMessage(L_ERROR, "Unable to copy to config file \"%s\" - %s",
2141 newfile, strerror(errno));
2142
2143 fclose(in);
2144 fclose(out);
2145 unlink(newfile);
2146
2147 return (HTTP_SERVER_ERROR);
2148 }
2149
2150 /*
2151 * Close the files...
2152 */
2153
2154 fclose(in);
2155 if (fclose(out))
2156 {
2157 LogMessage(L_ERROR, "Error file closing config file \"%s\" - %s",
2158 newfile, strerror(errno));
2159
2160 unlink(newfile);
2161
2162 return (HTTP_SERVER_ERROR);
2163 }
2164
2165 /*
2166 * Remove the request file...
2167 */
2168
2169 unlink(con->filename);
2170 con->filename[0] = '\0';
2171
2172 /*
2173 * Unlink the old backup, rename the current config file to the backup
2174 * filename, and rename the new config file to the config file name...
2175 */
2176
2177 if (unlink(oldfile))
2178 if (errno != ENOENT)
2179 {
2180 LogMessage(L_ERROR, "Unable to remove backup config file \"%s\" - %s",
2181 oldfile, strerror(errno));
2182
2183 unlink(newfile);
2184
2185 return (HTTP_SERVER_ERROR);
2186 }
2187
2188 if (rename(conffile, oldfile))
2189 if (errno != ENOENT)
2190 {
2191 LogMessage(L_ERROR, "Unable to rename old config file \"%s\" - %s",
2192 conffile, strerror(errno));
2193
2194 unlink(newfile);
2195
2196 return (HTTP_SERVER_ERROR);
2197 }
2198
2199 if (rename(newfile, conffile))
2200 {
2201 LogMessage(L_ERROR, "Unable to rename new config file \"%s\" - %s",
2202 newfile, strerror(errno));
2203
2204 rename(oldfile, conffile);
2205 unlink(newfile);
2206
2207 return (HTTP_SERVER_ERROR);
2208 }
2209
2210 /*
2211 * If the cupsd.conf file was updated, set the NeedReload flag...
2212 */
2213
2214 if (strcmp(con->uri, "/admin/conf/cupsd.conf") == 0)
2215 NeedReload = TRUE;
2216
2217 /*
2218 * Return that the file was created successfully...
2219 */
2220
2221 return (HTTP_CREATED);
2222 }
2223
2224
2225 /*
2226 * 'pipe_command()' - Pipe the output of a command to the remote client.
2227 */
2228
2229 static int /* O - Process ID */
2230 pipe_command(client_t *con, /* I - Client connection */
2231 int infile, /* I - Standard input for command */
2232 int *outfile, /* O - Standard output for command */
2233 char *command, /* I - Command to run */
2234 char *options) /* I - Options for command */
2235 {
2236 int pid; /* Process ID */
2237 char *commptr; /* Command string pointer */
2238 int fd; /* Looping var */
2239 int fds[2]; /* Pipe FDs */
2240 int argc; /* Number of arguments */
2241 int envc; /* Number of environment variables */
2242 char argbuf[10240], /* Argument buffer */
2243 *argv[100], /* Argument strings */
2244 *envp[100]; /* Environment variables */
2245 char lang[1024], /* LANG env variable */
2246 content_length[1024], /* CONTENT_LENGTH env variable */
2247 content_type[1024], /* CONTENT_TYPE env variable */
2248 ipp_port[1024], /* Default listen port */
2249 server_port[1024], /* Default server port */
2250 server_name[1024], /* Default listen hostname */
2251 remote_host[1024], /* REMOTE_HOST env variable */
2252 remote_user[1024], /* REMOTE_USER env variable */
2253 tmpdir[1024], /* TMPDIR environment variable */
2254 ldpath[1024], /* LD_LIBRARY_PATH environment variable */
2255 nlspath[1024], /* NLSPATH environment variable */
2256 datadir[1024], /* CUPS_DATADIR environment variable */
2257 root[1024], /* CUPS_SERVERROOT environment variable */
2258 query_string[10240]; /* QUERY_STRING env variable */
2259
2260
2261 /*
2262 * Copy the command string...
2263 */
2264
2265 strlcpy(argbuf, options, sizeof(argbuf));
2266
2267 /*
2268 * Parse the string; arguments can be separated by + and are terminated
2269 * by ?...
2270 */
2271
2272 argv[0] = argbuf;
2273
2274 for (commptr = argbuf, argc = 1; *commptr != '\0' && argc < 99; commptr ++)
2275 if (*commptr == ' ' || *commptr == '+')
2276 {
2277 *commptr++ = '\0';
2278
2279 while (*commptr == ' ')
2280 commptr ++;
2281
2282 if (*commptr != '\0')
2283 {
2284 argv[argc] = commptr;
2285 argc ++;
2286 }
2287
2288 commptr --;
2289 }
2290 else if (*commptr == '%')
2291 {
2292 if (commptr[1] >= '0' && commptr[1] <= '9')
2293 *commptr = (commptr[1] - '0') << 4;
2294 else
2295 *commptr = (tolower(commptr[1]) - 'a' + 10) << 4;
2296
2297 if (commptr[2] >= '0' && commptr[2] <= '9')
2298 *commptr |= commptr[2] - '0';
2299 else
2300 *commptr |= tolower(commptr[2]) - 'a' + 10;
2301
2302 strcpy(commptr + 1, commptr + 3);
2303 }
2304 else if (*commptr == '?')
2305 break;
2306
2307 argv[argc] = NULL;
2308
2309 if (argv[0][0] == '\0')
2310 argv[0] = strrchr(command, '/') + 1;
2311
2312 /*
2313 * Setup the environment variables as needed...
2314 */
2315
2316 snprintf(lang, sizeof(lang), "LANG=%s",
2317 con->language ? con->language->language : "C");
2318 #ifdef AF_INET6
2319 if (con->http.hostaddr.addr.sa_family == AF_INET6)
2320 {
2321 sprintf(ipp_port, "IPP_PORT=%d", ntohs(con->http.hostaddr.ipv6.sin6_port));
2322 sprintf(server_port, "SERVER_PORT=%d",
2323 ntohs(con->http.hostaddr.ipv6.sin6_port));
2324 }
2325 else
2326 #endif /* AF_INET6 */
2327 {
2328 sprintf(ipp_port, "IPP_PORT=%d", ntohs(con->http.hostaddr.ipv4.sin_port));
2329 sprintf(server_port, "SERVER_PORT=%d",
2330 ntohs(con->http.hostaddr.ipv4.sin_port));
2331 }
2332
2333 snprintf(server_name, sizeof(server_name), "SERVER_NAME=%s", ServerName);
2334 snprintf(remote_host, sizeof(remote_host), "REMOTE_HOST=%s", con->http.hostname);
2335 snprintf(remote_user, sizeof(remote_user), "REMOTE_USER=%s", con->username);
2336 snprintf(tmpdir, sizeof(tmpdir), "TMPDIR=%s", TempDir);
2337 snprintf(datadir, sizeof(datadir), "CUPS_DATADIR=%s", DataDir);
2338 snprintf(root, sizeof(root), "CUPS_SERVERROOT=%s", ServerRoot);
2339
2340 if (getenv("LD_LIBRARY_PATH") != NULL)
2341 snprintf(ldpath, sizeof(ldpath), "LD_LIBRARY_PATH=%s",
2342 getenv("LD_LIBRARY_PATH"));
2343 else if (getenv("DYLD_LIBRARY_PATH") != NULL)
2344 snprintf(ldpath, sizeof(ldpath), "DYLD_LIBRARY_PATH=%s",
2345 getenv("DYLD_LIBRARY_PATH"));
2346 else
2347 ldpath[0] = '\0';
2348
2349 if (getenv("NLSPATH") != NULL)
2350 snprintf(nlspath, sizeof(nlspath), "NLSPATH=%s", getenv("NLSPATH"));
2351 else
2352 nlspath[0] = '\0';
2353
2354 envp[0] = "PATH=/bin:/usr/bin";
2355 envp[1] = "SERVER_SOFTWARE=CUPS/1.2";
2356 envp[2] = "GATEWAY_INTERFACE=CGI/1.1";
2357 envp[3] = "SERVER_PROTOCOL=HTTP/1.1";
2358 envp[4] = ipp_port;
2359 envp[5] = server_name;
2360 envp[6] = server_port;
2361 envp[7] = remote_host;
2362 envp[8] = remote_user;
2363 envp[9] = lang;
2364 envp[10] = TZ;
2365 envp[11] = tmpdir;
2366 envp[12] = datadir;
2367 envp[13] = root;
2368
2369 envc = 14;
2370
2371 if (ldpath[0])
2372 envp[envc ++] = ldpath;
2373
2374 if (nlspath[0])
2375 envp[envc ++] = nlspath;
2376
2377 if (con->operation == HTTP_GET)
2378 {
2379 envp[envc ++] = "REQUEST_METHOD=GET";
2380
2381 if (*commptr)
2382 {
2383 /*
2384 * Add GET form variables after ?...
2385 */
2386
2387 *commptr++ = '\0';
2388
2389 snprintf(query_string, sizeof(query_string), "QUERY_STRING=%s", commptr);
2390 envp[envc ++] = query_string;
2391 }
2392 }
2393 else
2394 {
2395 sprintf(content_length, "CONTENT_LENGTH=%d", con->bytes);
2396 snprintf(content_type, sizeof(content_type), "CONTENT_TYPE=%s",
2397 con->http.fields[HTTP_FIELD_CONTENT_TYPE]);
2398
2399 envp[envc ++] = "REQUEST_METHOD=POST";
2400 envp[envc ++] = content_length;
2401 envp[envc ++] = content_type;
2402 }
2403
2404 /*
2405 * Tell the CGI if we are using encryption...
2406 */
2407
2408 if (con->http.encryption >= HTTP_ENCRYPT_REQUIRED)
2409 envp[envc ++] = "HTTPS=ON";
2410
2411 envp[envc] = NULL;
2412
2413 /*
2414 * Create a pipe for the output...
2415 */
2416
2417 if (pipe(fds))
2418 {
2419 LogMessage(L_ERROR, "Unable to create pipes for CGI %s - %s",
2420 argv[0], strerror(errno));
2421 return (0);
2422 }
2423
2424 /*
2425 * Then execute the command...
2426 */
2427
2428 if ((pid = fork()) == 0)
2429 {
2430 /*
2431 * Child comes here... Close stdin if necessary and dup the pipe to stdout.
2432 */
2433
2434 if (getuid() == 0)
2435 {
2436 /*
2437 * Running as root, so change to a non-priviledged user...
2438 */
2439
2440 if (setgid(Group))
2441 exit(errno);
2442
2443 if (setuid(User))
2444 exit(errno);
2445 }
2446
2447 /*
2448 * Reset group membership to just the main one we belong to.
2449 */
2450
2451 setgroups(0, NULL);
2452
2453 /*
2454 * Update stdin/stdout/stderr...
2455 */
2456
2457 if (infile)
2458 {
2459 close(0);
2460 if (dup(infile) < 0)
2461 exit(errno);
2462 }
2463
2464 close(1);
2465 if (dup(fds[1]) < 0)
2466 exit(errno);
2467
2468 close(2);
2469 open("/dev/null", O_WRONLY);
2470
2471 /*
2472 * Close extra file descriptors...
2473 */
2474
2475 for (fd = 3; fd < MaxFDs; fd ++)
2476 close(fd);
2477
2478 /*
2479 * Change umask to restrict permissions on created files...
2480 */
2481
2482 umask(077);
2483
2484 /*
2485 * Execute the pipe program; if an error occurs, exit with status 1...
2486 */
2487
2488 execve(command, argv, envp);
2489 exit(errno);
2490 return (0);
2491 }
2492 else if (pid < 0)
2493 {
2494 /*
2495 * Error - can't fork!
2496 */
2497
2498 LogMessage(L_ERROR, "Unable to fork for CGI %s - %s", argv[0],
2499 strerror(errno));
2500
2501 close(fds[0]);
2502 close(fds[1]);
2503 return (0);
2504 }
2505 else
2506 {
2507 /*
2508 * Fork successful - return the PID...
2509 */
2510
2511 AddCert(pid, con->username);
2512
2513 LogMessage(L_DEBUG, "CGI %s started - PID = %d", command, pid);
2514
2515 *outfile = fds[0];
2516 close(fds[1]);
2517
2518 return (pid);
2519 }
2520 }
2521
2522
2523 /*
2524 * End of "$Id: client.c,v 1.91.2.12 2002/05/16 14:00:07 mike Exp $".
2525 */