2 * "$Id: client.c,v 1.91.2.2 2001/05/13 18:38:34 mike Exp $"
4 * Client routines for the Common UNIX Printing System (CUPS) scheduler.
6 * Copyright 1997-2001 by Easy Software Products, all rights reserved.
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
15 * Attn: CUPS Licensing Information
16 * Easy Software Products
17 * 44141 Airport View Drive, Suite 204
18 * Hollywood, Maryland 20636-3111 USA
20 * Voice: (301) 373-9603
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
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 * pipe_command() - Pipe the output of a command to the remote client.
43 * Include necessary headers...
51 # include <openssl/err.h>
52 # include <openssl/ssl.h>
53 # include <openssl/rand.h>
54 #endif /* HAVE_LIBSSL */
61 static int check_if_modified(client_t
*con
, struct stat
*filestats
);
62 static void decode_auth(client_t
*con
);
63 static char *get_file(client_t
*con
, struct stat
*filestats
);
64 static int pipe_command(client_t
*con
, int infile
, int *outfile
, char *command
, char *options
);
68 * 'AcceptClient()' - Accept a new client.
72 AcceptClient(listener_t
*lis
) /* I - Listener socket */
74 int i
; /* Looping var */
75 int val
; /* Parameter value */
76 client_t
*con
; /* New client pointer */
77 const struct hostent
*host
; /* Host entry for address */
78 http_addr_t temp
; /* Temporary address variable */
81 LogMessage(L_DEBUG2
, "AcceptClient(%08x) %d NumClients = %d",
82 lis
, lis
->fd
, NumClients
);
85 * Make sure we don't have a full set of clients already...
88 if (NumClients
== MaxClients
)
92 * Get a pointer to the next available client...
95 con
= Clients
+ NumClients
;
97 memset(con
, 0, sizeof(client_t
));
98 con
->http
.activity
= time(NULL
);
101 * Accept the client and get the remote address...
104 val
= sizeof(struct sockaddr_in
);
106 if ((con
->http
.fd
= accept(lis
->fd
, (struct sockaddr
*)&(con
->http
.hostaddr
),
109 LogMessage(L_ERROR
, "Unable to accept client connection - %s.",
115 if (lis
->address
.addr
.sa_family
== AF_INET6
)
116 con
->http
.hostaddr
.ipv6
.sin6_port
= lis
->address
.ipv6
.sin6_port
;
118 #endif /* AF_INET6 */
119 con
->http
.hostaddr
.ipv4
.sin_port
= lis
->address
.ipv4
.sin_port
;
122 host
= httpAddrLookup(&(con
->http
.hostaddr
), con
->http
.hostname
,
123 sizeof(con
->http
.hostname
));
127 httpAddrString(&(con
->http
.hostaddr
), con
->http
.hostname
,
128 sizeof(con
->http
.hostname
));
131 if (httpAddrEqual(&(con
->http
.hostaddr
), &ServerAddr
))
134 * Map accesses from the same host to the server name.
137 strncpy(con
->http
.hostname
, ServerName
, sizeof(con
->http
.hostname
) - 1);
140 if (host
== NULL
&& HostNameLookups
== 2)
143 * Can't have an unresolved IP address with double-lookups enabled...
147 closesocket(con
->http
.fd
);
152 LogMessage(L_WARN
, "Name lookup failed - connection from %s closed!",
157 if (HostNameLookups
== 2)
160 * Do double lookups as needed...
163 if ((host
= gethostbyname(con
->http
.hostname
)) != NULL
)
166 * See if the hostname maps to the same IP address...
169 if (host
->h_addrtype
!= con
->http
.hostaddr
.addr
.sa_family
)
172 * Not the right type of address...
180 * Compare all of the addresses against this one...
183 for (i
= 0; host
->h_addr_list
[i
]; i
++)
185 httpAddrLoad(host
, 0, i
, &temp
);
187 if (httpAddrEqual(&(con
->http
.hostaddr
), &temp
))
191 if (!host
->h_addr_list
[i
])
199 * Can't have a hostname that doesn't resolve to the same IP address
200 * with double-lookups enabled...
204 closesocket(con
->http
.fd
);
209 LogMessage(L_WARN
, "IP lookup failed - connection from %s closed!",
216 if (con
->http
.hostaddr
.addr
.sa_family
== AF_INET6
)
217 LogMessage(L_DEBUG
, "AcceptClient() %d from %s:%d.", con
->http
.fd
,
218 con
->http
.hostname
, ntohs(con
->http
.hostaddr
.ipv6
.sin6_port
));
220 #endif /* AF_INET6 */
221 LogMessage(L_DEBUG
, "AcceptClient() %d from %s:%d.", con
->http
.fd
,
222 con
->http
.hostname
, ntohs(con
->http
.hostaddr
.ipv4
.sin_port
));
225 * Add the socket to the select() input mask.
228 fcntl(con
->http
.fd
, F_SETFD
, fcntl(con
->http
.fd
, F_GETFD
) | FD_CLOEXEC
);
230 LogMessage(L_DEBUG2
, "AcceptClient() Adding fd %d to InputSet...",
232 FD_SET(con
->http
.fd
, &InputSet
);
237 * Temporarily suspend accept()'s until we lose a client...
240 if (NumClients
== MaxClients
)
245 * See if we are connecting on a secure port...
248 if (lis
->encryption
== HTTP_ENCRYPT_ALWAYS
)
251 * https connection; go secure...
256 #endif /* HAVE_LIBSSL */
261 * 'CloseAllClients()' - Close all remote clients immediately.
265 CloseAllClients(void)
267 while (NumClients
> 0)
268 CloseClient(Clients
);
273 * 'CloseClient()' - Close a remote client.
277 CloseClient(client_t
*con
) /* I - Client to close */
279 int status
; /* Exit status of pipe command */
281 SSL_CTX
*context
; /* Context for encryption */
282 SSL
*conn
; /* Connection for encryption */
283 #endif /* HAVE_LIBSSL */
286 LogMessage(L_DEBUG
, "CloseClient() %d", con
->http
.fd
);
290 * Shutdown encryption as needed...
295 conn
= (SSL
*)(con
->http
.tls
);
296 context
= SSL_get_SSL_CTX(conn
);
299 SSL_CTX_free(context
);
302 con
->http
.tls
= NULL
;
304 #endif /* HAVE_LIBSSL */
307 * Close the socket and clear the file from the input set for select()...
310 if (con
->http
.fd
> 0)
312 LogMessage(L_DEBUG2
, "CloseClient: Removing fd %d from InputSet and OutputSet...",
315 FD_CLR(con
->http
.fd
, &InputSet
);
316 FD_CLR(con
->http
.fd
, &OutputSet
);
320 if (con
->pipe_pid
!= 0)
322 LogMessage(L_DEBUG2
, "CloseClient: Removing fd %d from InputSet...",
324 FD_CLR(con
->file
, &InputSet
);
330 * Close the open data file...
335 kill(con
->pipe_pid
, SIGKILL
);
336 waitpid(con
->pipe_pid
, &status
, WNOHANG
);
339 LogMessage(L_DEBUG2
, "CloseClient() %d Closing data file %d.",
340 con
->http
.fd
, con
->file
);
341 LogMessage(L_DEBUG2
, "CloseClient() %d Removing fd %d from InputSet.",
342 con
->http
.fd
, con
->file
);
344 FD_CLR(con
->file
, &InputSet
);
351 ippDelete(con
->request
);
357 ippDelete(con
->response
);
358 con
->response
= NULL
;
363 cupsLangFree(con
->language
);
364 con
->language
= NULL
;
368 * Re-enable new client connections if we are going back under the
372 if (NumClients
== MaxClients
)
376 * Compact the list of clients as necessary...
381 if (con
< (Clients
+ NumClients
))
382 memcpy(con
, con
+ 1, (Clients
+ NumClients
- con
) * sizeof(client_t
));
387 * 'EncryptClient()' - Enable encryption for the client...
390 int /* O - 1 on success, 0 on error */
391 EncryptClient(client_t
*con
) /* I - Client to encrypt */
394 SSL_CTX
*context
; /* Context for encryption */
395 SSL
*conn
; /* Connection for encryption */
396 int error
; /* Error code */
400 * Create the SSL context and accept the connection...
403 context
= SSL_CTX_new(SSLv23_method());
404 conn
= SSL_new(context
);
406 SSL_use_PrivateKey_file(conn
, ServerKey
, SSL_FILETYPE_PEM
);
407 SSL_use_certificate_file(conn
, ServerCertificate
, SSL_FILETYPE_PEM
);
409 SSL_set_fd(conn
, con
->http
.fd
);
410 if (SSL_accept(conn
) != 1)
412 while ((error
= ERR_get_error()) != 0)
413 LogMessage(L_ERROR
, "EncryptClient: %s", ERR_error_string(error
, NULL
));
415 SSL_CTX_free(context
);
420 con
->http
.tls
= conn
;
424 #endif /* HAVE_LIBSSL */
429 * 'ReadClient()' - Read data from a client.
432 int /* O - 1 on success, 0 on error */
433 ReadClient(client_t
*con
) /* I - Client to read from */
435 char line
[8192], /* Line from client... */
436 operation
[64], /* Operation code from socket */
437 version
[64]; /* HTTP version number string */
438 int major
, minor
; /* HTTP version numbers */
439 http_status_t status
; /* Transfer status */
440 ipp_state_t ipp_state
; /* State of IPP transfer */
441 int bytes
; /* Number of bytes to POST */
442 char *filename
; /* Name of file for GET/HEAD */
443 struct stat filestats
; /* File information */
444 mime_type_t
*type
; /* MIME type of file */
445 printer_t
*p
; /* Printer */
446 location_t
*best
; /* Best match for authentication */
447 static unsigned request_id
= 0;/* Request ID for temp files */
450 status
= HTTP_CONTINUE
;
452 switch (con
->http
.state
)
456 * See if we've received a request line...
459 if (httpGets(line
, sizeof(line
) - 1, HTTP(con
)) == NULL
)
466 * Ignore blank request lines...
473 * Clear other state variables...
476 httpClearFields(HTTP(con
));
478 con
->http
.activity
= time(NULL
);
479 con
->http
.version
= HTTP_1_0
;
480 con
->http
.keep_alive
= HTTP_KEEPALIVE_OFF
;
481 con
->http
.data_encoding
= HTTP_ENCODE_LENGTH
;
482 con
->http
.data_remaining
= 0;
483 con
->operation
= HTTP_WAITING
;
487 con
->command
[0] = '\0';
488 con
->username
[0] = '\0';
489 con
->password
[0] = '\0';
492 if (con
->language
!= NULL
)
494 cupsLangFree(con
->language
);
495 con
->language
= NULL
;
499 * Grab the request line...
502 switch (sscanf(line
, "%63s%1023s%63s", operation
, con
->uri
, version
))
505 SendError(con
, HTTP_BAD_REQUEST
);
509 con
->http
.version
= HTTP_0_9
;
512 if (sscanf(version
, "HTTP/%d.%d", &major
, &minor
) != 2)
514 SendError(con
, HTTP_BAD_REQUEST
);
521 con
->http
.version
= (http_version_t
)(major
* 100 + minor
);
522 if (con
->http
.version
== HTTP_1_1
)
523 con
->http
.keep_alive
= HTTP_KEEPALIVE_ON
;
525 con
->http
.keep_alive
= HTTP_KEEPALIVE_OFF
;
529 SendError(con
, HTTP_NOT_SUPPORTED
);
537 * Process the request...
540 if (strcmp(operation
, "GET") == 0)
541 con
->http
.state
= HTTP_GET
;
542 else if (strcmp(operation
, "PUT") == 0)
543 con
->http
.state
= HTTP_PUT
;
544 else if (strcmp(operation
, "POST") == 0)
545 con
->http
.state
= HTTP_POST
;
546 else if (strcmp(operation
, "DELETE") == 0)
547 con
->http
.state
= HTTP_DELETE
;
548 else if (strcmp(operation
, "TRACE") == 0)
549 con
->http
.state
= HTTP_TRACE
;
550 else if (strcmp(operation
, "OPTIONS") == 0)
551 con
->http
.state
= HTTP_OPTIONS
;
552 else if (strcmp(operation
, "HEAD") == 0)
553 con
->http
.state
= HTTP_HEAD
;
556 SendError(con
, HTTP_BAD_REQUEST
);
561 con
->start
= time(NULL
);
562 con
->operation
= con
->http
.state
;
564 LogMessage(L_DEBUG
, "ReadClient() %d %s %s HTTP/%d.%d", con
->http
.fd
,
566 con
->http
.version
/ 100, con
->http
.version
% 100);
568 con
->http
.status
= HTTP_OK
;
578 * Parse incoming parameters until the status changes...
581 status
= httpUpdate(HTTP(con
));
583 if (status
!= HTTP_OK
&& status
!= HTTP_CONTINUE
)
585 SendError(con
, HTTP_BAD_REQUEST
);
592 break; /* Anti-compiler-warning-code */
596 * Handle new transfers...
599 if (status
== HTTP_OK
)
601 con
->language
= cupsLangGet(con
->http
.fields
[HTTP_FIELD_ACCEPT_LANGUAGE
]);
605 if (strncmp(con
->http
.fields
[HTTP_FIELD_CONNECTION
], "Keep-Alive", 10) == 0)
606 con
->http
.keep_alive
= HTTP_KEEPALIVE_ON
;
608 if (con
->http
.fields
[HTTP_FIELD_HOST
][0] == '\0' &&
609 con
->http
.version
>= HTTP_1_1
)
612 * HTTP/1.1 and higher require the "Host:" field...
615 if (!SendError(con
, HTTP_BAD_REQUEST
))
621 else if (con
->operation
== HTTP_OPTIONS
)
624 * Do OPTIONS command...
627 if ((best
= FindBest(con
)) != NULL
&&
628 best
->type
!= AUTH_NONE
)
630 if (!SendHeader(con
, HTTP_UNAUTHORIZED
, NULL
))
637 if (strcasecmp(con
->http
.fields
[HTTP_FIELD_CONNECTION
], "Upgrade") == 0 &&
638 con
->http
.tls
== NULL
)
642 * Do encryption stuff...
645 if (!SendHeader(con
, HTTP_SWITCHING_PROTOCOLS
, NULL
))
651 httpPrintf(HTTP(con
), "Connection: Upgrade\r\n");
652 httpPrintf(HTTP(con
), "Upgrade: TLS/1.0,HTTP/1.1\r\n");
653 httpPrintf(HTTP(con
), "Content-Length: 0\r\n");
654 httpPrintf(HTTP(con
), "\r\n");
658 if (!SendError(con
, HTTP_NOT_IMPLEMENTED
))
663 #endif /* HAVE_LIBSSL */
666 if (!SendHeader(con
, HTTP_OK
, NULL
))
672 httpPrintf(HTTP(con
), "Allow: GET, HEAD, OPTIONS, POST\r\n");
673 httpPrintf(HTTP(con
), "Content-Length: 0\r\n");
674 httpPrintf(HTTP(con
), "\r\n");
676 else if (strstr(con
->uri
, "..") != NULL
)
679 * Protect against malicious users!
682 if (!SendError(con
, HTTP_FORBIDDEN
))
688 else if (con
->uri
[0] != '/')
691 * Don't allow proxying (yet)...
694 if (!SendError(con
, HTTP_METHOD_NOT_ALLOWED
))
702 if (strcasecmp(con
->http
.fields
[HTTP_FIELD_CONNECTION
], "Upgrade") == 0 &&
703 con
->http
.tls
== NULL
)
707 * Do encryption stuff...
710 if (!SendHeader(con
, HTTP_SWITCHING_PROTOCOLS
, NULL
))
716 httpPrintf(HTTP(con
), "Connection: Upgrade\r\n");
717 httpPrintf(HTTP(con
), "Upgrade: TLS/1.0,HTTP/1.1\r\n");
718 httpPrintf(HTTP(con
), "Content-Length: 0\r\n");
719 httpPrintf(HTTP(con
), "\r\n");
723 if (!SendError(con
, HTTP_NOT_IMPLEMENTED
))
728 #endif /* HAVE_LIBSSL */
731 if ((status
= IsAuthorized(con
)) != HTTP_OK
)
733 SendError(con
, status
);
738 switch (con
->http
.state
)
741 if (strncmp(con
->uri
, "/printers/", 10) == 0 &&
742 strcmp(con
->uri
+ strlen(con
->uri
) - 4, ".ppd") == 0)
745 * Send PPD file - get the real printer name since printer
746 * names are not case sensitive but filenames can be...
749 con
->uri
[strlen(con
->uri
) - 4] = '\0'; /* Drop ".ppd" */
751 if ((p
= FindPrinter(con
->uri
+ 10)) != NULL
)
752 snprintf(con
->uri
, sizeof(con
->uri
), "/ppd/%s.ppd", p
->name
);
755 if (!SendError(con
, HTTP_NOT_FOUND
))
765 if (strncmp(con
->uri
, "/admin", 6) == 0 ||
766 strncmp(con
->uri
, "/printers", 9) == 0 ||
767 strncmp(con
->uri
, "/classes", 8) == 0 ||
768 strncmp(con
->uri
, "/jobs", 5) == 0)
774 if (strncmp(con
->uri
, "/admin", 6) == 0)
776 snprintf(con
->command
, sizeof(con
->command
),
777 "%s/cgi-bin/admin.cgi", ServerBin
);
778 con
->options
= con
->uri
+ 6;
780 else if (strncmp(con
->uri
, "/printers", 9) == 0)
782 snprintf(con
->command
, sizeof(con
->command
),
783 "%s/cgi-bin/printers.cgi", ServerBin
);
784 con
->options
= con
->uri
+ 9;
786 else if (strncmp(con
->uri
, "/classes", 8) == 0)
788 snprintf(con
->command
, sizeof(con
->command
),
789 "%s/cgi-bin/classes.cgi", ServerBin
);
790 con
->options
= con
->uri
+ 8;
794 snprintf(con
->command
, sizeof(con
->command
),
795 "%s/cgi-bin/jobs.cgi", ServerBin
);
796 con
->options
= con
->uri
+ 5;
799 if (con
->options
[0] == '/')
802 if (!SendCommand(con
, con
->command
, con
->options
))
804 if (!SendError(con
, HTTP_NOT_FOUND
))
811 LogRequest(con
, HTTP_OK
);
813 if (con
->http
.version
<= HTTP_1_0
)
814 con
->http
.keep_alive
= HTTP_KEEPALIVE_OFF
;
822 if ((filename
= get_file(con
, &filestats
)) == NULL
)
824 if (!SendError(con
, HTTP_NOT_FOUND
))
830 else if (!check_if_modified(con
, &filestats
))
832 if (!SendError(con
, HTTP_NOT_MODIFIED
))
840 type
= mimeFileType(MimeDatabase
, filename
);
842 strcpy(line
, "text/plain");
844 snprintf(line
, sizeof(line
), "%s/%s", type
->super
, type
->type
);
846 if (!SendFile(con
, HTTP_OK
, filename
, line
, &filestats
))
855 case HTTP_POST_RECV
:
857 * See if the POST request includes a Content-Length field, and if
858 * so check the length against any limits that are set...
861 LogMessage(L_DEBUG2
, "POST %s", con
->uri
);
862 LogMessage(L_DEBUG2
, "CONTENT_TYPE = %s", con
->http
.fields
[HTTP_FIELD_CONTENT_TYPE
]);
864 if (con
->http
.fields
[HTTP_FIELD_CONTENT_LENGTH
][0] &&
865 atoi(con
->http
.fields
[HTTP_FIELD_CONTENT_LENGTH
]) > MaxRequestSize
&&
869 * Request too large...
872 if (!SendError(con
, HTTP_REQUEST_TOO_LARGE
))
882 * See what kind of POST request this is; for IPP requests the
883 * content-type field will be "application/ipp"...
886 if (strcmp(con
->http
.fields
[HTTP_FIELD_CONTENT_TYPE
], "application/ipp") == 0)
887 con
->request
= ippNew();
888 else if (strncmp(con
->uri
, "/admin", 6) == 0 ||
889 strncmp(con
->uri
, "/printers", 9) == 0 ||
890 strncmp(con
->uri
, "/classes", 8) == 0 ||
891 strncmp(con
->uri
, "/jobs", 5) == 0)
897 if (strncmp(con
->uri
, "/admin", 6) == 0)
899 snprintf(con
->command
, sizeof(con
->command
),
900 "%s/cgi-bin/admin.cgi", ServerBin
);
901 con
->options
= con
->uri
+ 6;
903 else if (strncmp(con
->uri
, "/printers", 9) == 0)
905 snprintf(con
->command
, sizeof(con
->command
),
906 "%s/cgi-bin/printers.cgi", ServerBin
);
907 con
->options
= con
->uri
+ 9;
909 else if (strncmp(con
->uri
, "/classes", 8) == 0)
911 snprintf(con
->command
, sizeof(con
->command
),
912 "%s/cgi-bin/classes.cgi", ServerBin
);
913 con
->options
= con
->uri
+ 8;
917 snprintf(con
->command
, sizeof(con
->command
),
918 "%s/cgi-bin/jobs.cgi", ServerBin
);
919 con
->options
= con
->uri
+ 5;
922 if (con
->options
[0] == '/')
925 LogMessage(L_DEBUG2
, "ReadClient() %d command=\"%s\", options = \"%s\"",
926 con
->http
.fd
, con
->command
, con
->options
);
928 if (con
->http
.version
<= HTTP_1_0
)
929 con
->http
.keep_alive
= HTTP_KEEPALIVE_OFF
;
931 else if (!SendError(con
, HTTP_UNAUTHORIZED
))
941 SendError(con
, HTTP_NOT_IMPLEMENTED
);
946 if (strncmp(con
->uri
, "/printers/", 10) == 0 &&
947 strcmp(con
->uri
+ strlen(con
->uri
) - 4, ".ppd") == 0)
950 * Send PPD file - get the real printer name since printer
951 * names are not case sensitive but filenames can be...
954 con
->uri
[strlen(con
->uri
) - 4] = '\0'; /* Drop ".ppd" */
956 if ((p
= FindPrinter(con
->uri
+ 10)) != NULL
)
957 snprintf(con
->uri
, sizeof(con
->uri
), "/ppd/%s.ppd", p
->name
);
960 if (!SendError(con
, HTTP_NOT_FOUND
))
970 if (strncmp(con
->uri
, "/admin/", 7) == 0 ||
971 strncmp(con
->uri
, "/printers/", 10) == 0 ||
972 strncmp(con
->uri
, "/classes/", 9) == 0 ||
973 strncmp(con
->uri
, "/jobs/", 6) == 0)
979 if (!SendHeader(con
, HTTP_OK
, "text/html"))
985 if (httpPrintf(HTTP(con
), "\r\n") < 0)
991 LogRequest(con
, HTTP_OK
);
993 else if ((filename
= get_file(con
, &filestats
)) == NULL
)
995 if (!SendHeader(con
, HTTP_NOT_FOUND
, "text/html"))
1001 LogRequest(con
, HTTP_NOT_FOUND
);
1003 else if (!check_if_modified(con
, &filestats
))
1005 if (!SendError(con
, HTTP_NOT_MODIFIED
))
1011 LogRequest(con
, HTTP_NOT_MODIFIED
);
1019 type
= mimeFileType(MimeDatabase
, filename
);
1021 strcpy(line
, "text/plain");
1023 snprintf(line
, sizeof(line
), "%s/%s", type
->super
, type
->type
);
1025 if (!SendHeader(con
, HTTP_OK
, line
))
1031 if (httpPrintf(HTTP(con
), "Last-Modified: %s\r\n",
1032 httpGetDateString(filestats
.st_mtime
)) < 0)
1038 if (httpPrintf(HTTP(con
), "Content-Length: %d\r\n",
1039 filestats
.st_size
) < 0)
1045 LogRequest(con
, HTTP_OK
);
1048 if (httpPrintf(HTTP(con
), "\r\n") < 0)
1054 con
->http
.state
= HTTP_WAITING
;
1058 break; /* Anti-compiler-warning-code */
1064 * Handle any incoming data...
1067 switch (con
->http
.state
)
1069 case HTTP_PUT_RECV
:
1072 case HTTP_POST_RECV
:
1073 LogMessage(L_DEBUG2
, "ReadClient() %d con->data_encoding = %s, con->data_remaining = %d, con->file = %d",
1075 con
->http
.data_encoding
== HTTP_ENCODE_CHUNKED
? "chunked" : "length",
1076 con
->http
.data_remaining
, con
->file
);
1078 if (con
->request
!= NULL
)
1081 * Grab any request data from the connection...
1084 if ((ipp_state
= ippRead(&(con
->http
), con
->request
)) == IPP_ERROR
)
1086 LogMessage(L_ERROR
, "ReadClient() %d IPP Read Error!",
1091 else if (ipp_state
!= IPP_DATA
)
1094 con
->bytes
+= ippLength(con
->request
);
1097 if (con
->file
== 0 && con
->http
.state
!= HTTP_POST_SEND
)
1100 * Create a file as needed for the request data...
1103 snprintf(con
->filename
, sizeof(con
->filename
), "%s/%08x",
1104 RequestRoot
, request_id
++);
1105 con
->file
= open(con
->filename
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0640);
1106 fchmod(con
->file
, 0640);
1107 fchown(con
->file
, User
, Group
);
1109 LogMessage(L_DEBUG2
, "ReadClient() %d REQUEST %s=%d", con
->http
.fd
,
1110 con
->filename
, con
->file
);
1114 if (!SendError(con
, HTTP_REQUEST_TOO_LARGE
))
1122 if (con
->http
.state
!= HTTP_POST_SEND
)
1124 if ((bytes
= httpRead(HTTP(con
), line
, sizeof(line
))) < 0)
1131 con
->bytes
+= bytes
;
1133 LogMessage(L_DEBUG2
, "ReadClient() %d writing %d bytes to %d",
1134 con
->http
.fd
, bytes
, con
->file
);
1136 if (write(con
->file
, line
, bytes
) < bytes
)
1138 LogMessage(L_ERROR
, "ReadClient: Unable to write %d bytes to %s: %s",
1139 bytes
, con
->filename
, strerror(errno
));
1143 unlink(con
->filename
);
1145 if (!SendError(con
, HTTP_REQUEST_TOO_LARGE
))
1152 else if (con
->http
.state
!= HTTP_POST_SEND
)
1159 if (con
->http
.state
== HTTP_POST_SEND
)
1163 fstat(con
->file
, &filestats
);
1165 LogMessage(L_DEBUG2
, "ReadClient() %d Closing data file %d, size = %d.",
1166 con
->http
.fd
, con
->file
, filestats
.st_size
);
1171 if (filestats
.st_size
> MaxRequestSize
&&
1175 * Request is too big; remove it and send an error...
1178 LogMessage(L_DEBUG2
, "ReadClient() %d Removing temp file %s",
1179 con
->http
.fd
, con
->filename
);
1180 unlink(con
->filename
);
1185 * Delete any IPP request data...
1188 ippDelete(con
->request
);
1189 con
->request
= NULL
;
1192 if (!SendError(con
, HTTP_REQUEST_TOO_LARGE
))
1199 if (con
->command
[0])
1201 if (!SendCommand(con
, con
->command
, con
->options
))
1203 if (!SendError(con
, HTTP_NOT_FOUND
))
1210 LogRequest(con
, HTTP_OK
);
1215 ProcessIPPRequest(con
);
1220 break; /* Anti-compiler-warning-code */
1223 if (!con
->http
.keep_alive
&& con
->http
.state
== HTTP_WAITING
)
1234 * 'SendCommand()' - Send output from a command via HTTP.
1238 SendCommand(client_t
*con
,
1245 if (con
->filename
[0])
1246 fd
= open(con
->filename
, O_RDONLY
);
1248 fd
= open("/dev/null", O_RDONLY
);
1250 con
->pipe_pid
= pipe_command(con
, fd
, &(con
->file
), command
, options
);
1254 LogMessage(L_INFO
, "Started \"%s\" (pid=%d)", command
, con
->pipe_pid
);
1256 LogMessage(L_DEBUG
, "SendCommand() %d file=%d", con
->http
.fd
, con
->file
);
1258 if (con
->pipe_pid
== 0)
1261 fcntl(con
->file
, F_SETFD
, fcntl(con
->file
, F_GETFD
) | FD_CLOEXEC
);
1263 LogMessage(L_DEBUG2
, "SendCommand: Adding fd %d to InputSet...", con
->file
);
1264 LogMessage(L_DEBUG2
, "SendCommand: Adding fd %d to OutputSet...",
1267 FD_SET(con
->file
, &InputSet
);
1268 FD_SET(con
->http
.fd
, &OutputSet
);
1270 if (!SendHeader(con
, HTTP_OK
, NULL
))
1273 if (con
->http
.version
== HTTP_1_1
)
1275 con
->http
.data_encoding
= HTTP_ENCODE_CHUNKED
;
1277 if (httpPrintf(HTTP(con
), "Transfer-Encoding: chunked\r\n") < 0)
1281 con
->got_fields
= 0;
1289 * 'SendError()' - Send an error message via HTTP.
1292 int /* O - 1 if successful, 0 otherwise */
1293 SendError(client_t
*con
, /* I - Connection */
1294 http_status_t code
) /* I - Error code */
1296 char message
[1024]; /* Message for user */
1300 * Put the request in the access_log file...
1303 if (con
->operation
> HTTP_WAITING
)
1304 LogRequest(con
, code
);
1306 LogMessage(L_DEBUG2
, "SendError() %d code=%d", con
->http
.fd
, code
);
1309 * To work around bugs in some proxies, don't use Keep-Alive for some
1313 if (code
>= HTTP_BAD_REQUEST
)
1314 con
->http
.keep_alive
= HTTP_KEEPALIVE_OFF
;
1317 * Send an error message back to the client. If the error code is a
1318 * 400 or 500 series, make sure the message contains some text, too!
1321 if (!SendHeader(con
, code
, NULL
))
1325 if (code
== HTTP_UPGRADE_REQUIRED
)
1326 if (httpPrintf(HTTP(con
), "Connection: Upgrade\r\n") < 0)
1329 if (httpPrintf(HTTP(con
), "Upgrade: TLS/1.0,HTTP/1.1\r\n") < 0)
1331 #endif /* HAVE_LIBSSL */
1333 if (con
->http
.version
>= HTTP_1_1
&& !con
->http
.keep_alive
)
1335 if (httpPrintf(HTTP(con
), "Connection: close\r\n") < 0)
1339 if (code
>= HTTP_BAD_REQUEST
)
1342 * Send a human-readable error message.
1345 snprintf(message
, sizeof(message
),
1346 "<HTML><HEAD><TITLE>%d %s</TITLE></HEAD>"
1347 "<BODY><H1>%s</H1>%s</BODY></HTML>\n",
1348 code
, httpStatus(code
), httpStatus(code
),
1349 con
->language
? con
->language
->messages
[code
] :
1352 if (httpPrintf(HTTP(con
), "Content-Type: text/html\r\n") < 0)
1354 if (httpPrintf(HTTP(con
), "Content-Length: %d\r\n", strlen(message
)) < 0)
1356 if (httpPrintf(HTTP(con
), "\r\n") < 0)
1358 if (httpPrintf(HTTP(con
), "%s", message
) < 0)
1361 else if (httpPrintf(HTTP(con
), "\r\n") < 0)
1364 con
->http
.state
= HTTP_WAITING
;
1371 * 'SendFile()' - Send a file via HTTP.
1375 SendFile(client_t
*con
,
1379 struct stat
*filestats
)
1381 con
->file
= open(filename
, O_RDONLY
);
1383 LogMessage(L_DEBUG
, "SendFile() %d file=%d", con
->http
.fd
, con
->file
);
1388 fcntl(con
->file
, F_SETFD
, fcntl(con
->file
, F_GETFD
) | FD_CLOEXEC
);
1392 if (!SendHeader(con
, code
, type
))
1395 if (httpPrintf(HTTP(con
), "Last-Modified: %s\r\n", httpGetDateString(filestats
->st_mtime
)) < 0)
1397 if (httpPrintf(HTTP(con
), "Content-Length: %d\r\n", filestats
->st_size
) < 0)
1399 if (httpPrintf(HTTP(con
), "\r\n") < 0)
1402 LogMessage(L_DEBUG2
, "SendFile: Adding fd %d to OutputSet...", con
->http
.fd
);
1404 FD_SET(con
->http
.fd
, &OutputSet
);
1411 * 'SendHeader()' - Send an HTTP request.
1414 int /* O - 1 on success, 0 on failure */
1415 SendHeader(client_t
*con
, /* I - Client to send to */
1416 http_status_t code
, /* I - HTTP status code */
1417 char *type
) /* I - MIME type of document */
1419 location_t
*loc
; /* Authentication location */
1422 if (httpPrintf(HTTP(con
), "HTTP/%d.%d %d %s\r\n", con
->http
.version
/ 100,
1423 con
->http
.version
% 100, code
, httpStatus(code
)) < 0)
1425 if (httpPrintf(HTTP(con
), "Date: %s\r\n", httpGetDateString(time(NULL
))) < 0)
1427 if (httpPrintf(HTTP(con
), "Server: CUPS/1.1\r\n") < 0)
1429 if (con
->http
.keep_alive
&& con
->http
.version
>= HTTP_1_0
)
1431 if (httpPrintf(HTTP(con
), "Connection: Keep-Alive\r\n") < 0)
1433 if (httpPrintf(HTTP(con
), "Keep-Alive: timeout=%d\r\n", KeepAliveTimeout
) < 0)
1436 if (code
== HTTP_METHOD_NOT_ALLOWED
)
1437 if (httpPrintf(HTTP(con
), "Allow: GET, HEAD, OPTIONS, POST\r\n") < 0)
1440 if (code
== HTTP_UNAUTHORIZED
)
1442 loc
= FindBest(con
); /* This already succeeded in IsAuthorized */
1444 if (loc
->type
== AUTH_BASIC
)
1446 if (httpPrintf(HTTP(con
), "WWW-Authenticate: Basic realm=\"CUPS\"\r\n") < 0)
1451 if (httpPrintf(HTTP(con
), "WWW-Authenticate: Digest realm=\"CUPS\" "
1452 "nonce=\"%s\"\r\n", con
->http
.hostname
) < 0)
1456 if (con
->language
!= NULL
)
1458 if (httpPrintf(HTTP(con
), "Content-Language: %s\r\n",
1459 con
->language
->language
) < 0)
1463 if (httpPrintf(HTTP(con
), "Content-Type: %s; charset=%s\r\n", type
,
1464 cupsLangEncoding(con
->language
)) < 0)
1467 else if (type
!= NULL
)
1468 if (httpPrintf(HTTP(con
), "Content-Type: %s\r\n", type
) < 0)
1476 * 'WriteClient()' - Write data to a client as needed.
1479 int /* O - 1 if success, 0 if fail */
1480 WriteClient(client_t
*con
) /* I - Client connection */
1482 int bytes
; /* Number of bytes written */
1483 char buf
[HTTP_MAX_BUFFER
+ 1];/* Data buffer */
1484 char *bufptr
; /* Pointer into buffer */
1485 ipp_state_t ipp_state
; /* IPP state value */
1488 if (con
->http
.state
!= HTTP_GET_SEND
&&
1489 con
->http
.state
!= HTTP_POST_SEND
)
1492 if (con
->response
!= NULL
)
1494 ipp_state
= ippWrite(&(con
->http
), con
->response
);
1495 bytes
= ipp_state
!= IPP_ERROR
&& ipp_state
!= IPP_DATA
;
1497 else if ((bytes
= read(con
->file
, buf
, HTTP_MAX_BUFFER
)) > 0)
1499 if (con
->pipe_pid
&& !con
->got_fields
)
1502 * Inspect the data for Content-Type and other fields.
1507 for (bufptr
= buf
; !con
->got_fields
&& *bufptr
; bufptr
++)
1508 if (*bufptr
== '\n')
1511 * Send line to client...
1514 if (bufptr
> buf
&& bufptr
[-1] == '\r')
1518 httpPrintf(HTTP(con
), "%s\r\n", buf
);
1519 LogMessage(L_DEBUG2
, "WriteClient() %d %s", con
->http
.fd
, buf
);
1525 bytes
-= (bufptr
- buf
);
1526 memcpy(buf
, bufptr
, bytes
+ 1);
1530 * See if the line was empty...
1533 if (con
->field_col
== 0)
1534 con
->got_fields
= 1;
1538 else if (*bufptr
!= '\r')
1541 if (bytes
> 0 && !con
->got_fields
)
1544 * Remaining text needs to go out...
1547 httpPrintf(HTTP(con
), "%s", buf
);
1549 con
->http
.activity
= time(NULL
);
1552 else if (bytes
== 0)
1554 con
->http
.activity
= time(NULL
);
1559 if (httpWrite(HTTP(con
), buf
, bytes
) < 0)
1565 con
->bytes
+= bytes
;
1570 LogRequest(con
, HTTP_OK
);
1572 if (con
->http
.data_encoding
== HTTP_ENCODE_CHUNKED
)
1574 if (httpPrintf(HTTP(con
), "0\r\n\r\n") < 0)
1581 con
->http
.state
= HTTP_WAITING
;
1583 LogMessage(L_DEBUG2
, "WriteClient() Removing fd %d from OutputSet...",
1586 FD_CLR(con
->http
.fd
, &OutputSet
);
1590 LogMessage(L_DEBUG2
, "WriteClient() Removing fd %d from InputSet...",
1592 FD_CLR(con
->file
, &InputSet
);
1595 kill(con
->pipe_pid
, SIGTERM
);
1597 LogMessage(L_DEBUG2
, "WriteClient() %d Closing data file %d.",
1598 con
->http
.fd
, con
->file
);
1605 if (con
->filename
[0])
1607 LogMessage(L_DEBUG2
, "WriteClient() %d Removing temp file %s",
1608 con
->http
.fd
, con
->filename
);
1609 unlink(con
->filename
);
1612 if (con
->request
!= NULL
)
1614 ippDelete(con
->request
);
1615 con
->request
= NULL
;
1618 if (con
->response
!= NULL
)
1620 ippDelete(con
->response
);
1621 con
->response
= NULL
;
1624 if (!con
->http
.keep_alive
)
1632 LogMessage(L_DEBUG2
, "WriteClient() %d %d bytes", con
->http
.fd
, bytes
);
1634 con
->http
.activity
= time(NULL
);
1641 * 'check_if_modified()' - Decode an "If-Modified-Since" line.
1644 static int /* O - 1 if modified since */
1645 check_if_modified(client_t
*con
, /* I - Client connection */
1646 struct stat
*filestats
) /* I - File information */
1648 char *ptr
; /* Pointer into field */
1649 time_t date
; /* Time/date value */
1650 int size
; /* Size/length value */
1655 ptr
= con
->http
.fields
[HTTP_FIELD_IF_MODIFIED_SINCE
];
1660 LogMessage(L_DEBUG2
, "check_if_modified() %d If-Modified-Since=\"%s\"",
1663 while (*ptr
!= '\0')
1665 while (isspace(*ptr
) || *ptr
== ';')
1668 if (strncasecmp(ptr
, "length=", 7) == 0)
1673 while (isdigit(*ptr
))
1676 else if (isalpha(*ptr
))
1678 date
= httpGetDateTime(ptr
);
1679 while (*ptr
!= '\0' && *ptr
!= ';')
1684 LogMessage(L_DEBUG2
, "check_if_modified() %d sizes=%d,%d dates=%d,%d",
1685 con
->http
.fd
, size
, filestats
->st_size
, date
, filestats
->st_mtime
);
1687 return ((size
!= filestats
->st_size
&& size
!= 0) ||
1688 (date
< filestats
->st_mtime
&& date
!= 0) ||
1689 (size
== 0 && date
== 0));
1694 * 'decode_auth()' - Decode an authorization string.
1698 decode_auth(client_t
*con
) /* I - Client to decode to */
1700 char *s
, /* Authorization string */
1701 value
[1024]; /* Value string */
1702 const char *username
; /* Certificate username */
1706 * Decode the string...
1709 s
= con
->http
.fields
[HTTP_FIELD_AUTHORIZATION
];
1711 LogMessage(L_DEBUG2
, "decode_auth(%08x): Authorization string = \"%s\"",
1714 if (strncmp(s
, "Basic", 5) == 0)
1720 httpDecode64(value
, s
);
1723 * Pull the username and password out...
1726 if ((s
= strchr(value
, ':')) == NULL
)
1728 LogMessage(L_DEBUG
, "decode_auth() %d no colon in auth string \"%s\"",
1729 con
->http
.fd
, value
);
1735 strncpy(con
->username
, value
, sizeof(con
->username
) - 1);
1736 con
->username
[sizeof(con
->username
) - 1] = '\0';
1738 strncpy(con
->password
, s
, sizeof(con
->password
) - 1);
1739 con
->password
[sizeof(con
->password
) - 1] = '\0';
1741 else if (strncmp(s
, "Local", 5) == 0)
1747 if ((username
= FindCert(s
)) != NULL
)
1749 strncpy(con
->username
, username
, sizeof(con
->username
) - 1);
1750 con
->username
[sizeof(con
->username
) - 1] = '\0';
1753 else if (strncmp(s
, "Digest", 5) == 0)
1756 * Get the username and password from the Digest attributes...
1759 if (httpGetSubField(&(con
->http
), HTTP_FIELD_WWW_AUTHENTICATE
, "username",
1762 strncpy(con
->username
, value
, sizeof(con
->username
) - 1);
1763 con
->username
[sizeof(con
->username
) - 1] = '\0';
1766 if (httpGetSubField(&(con
->http
), HTTP_FIELD_WWW_AUTHENTICATE
, "response",
1769 strncpy(con
->password
, value
, sizeof(con
->password
) - 1);
1770 con
->password
[sizeof(con
->password
) - 1] = '\0';
1774 LogMessage(L_DEBUG2
, "decode_auth() %d username=\"%s\"",
1775 con
->http
.fd
, con
->username
);
1780 * 'get_file()' - Get a filename and state info.
1783 static char * /* O - Real filename */
1784 get_file(client_t
*con
, /* I - Client connection */
1785 struct stat
*filestats
)/* O - File information */
1787 int status
; /* Status of filesystem calls */
1788 char *params
; /* Pointer to parameters in URI */
1789 static char filename
[1024]; /* Filename buffer */
1793 * Need to add DocumentRoot global...
1796 if (strncmp(con
->uri
, "/ppd/", 5) == 0)
1797 snprintf(filename
, sizeof(filename
), "%s%s", ServerRoot
, con
->uri
);
1798 else if (con
->language
!= NULL
)
1799 snprintf(filename
, sizeof(filename
), "%s/%s%s", DocumentRoot
, con
->language
->language
,
1802 snprintf(filename
, sizeof(filename
), "%s%s", DocumentRoot
, con
->uri
);
1804 if ((params
= strchr(filename
, '?')) != NULL
)
1808 * Grab the status for this language; if there isn't a language-specific file
1809 * then fallback to the default one...
1812 if ((status
= stat(filename
, filestats
)) != 0 && con
->language
!= NULL
)
1815 * Drop the language prefix and try the current directory...
1818 if (strncmp(con
->uri
, "/ppd/", 5) != 0)
1820 snprintf(filename
, sizeof(filename
), "%s%s", DocumentRoot
, con
->uri
);
1822 status
= stat(filename
, filestats
);
1827 * If we're found a directory, get the index.html file instead...
1830 if (!status
&& S_ISDIR(filestats
->st_mode
))
1832 if (filename
[strlen(filename
) - 1] == '/')
1833 strncat(filename
, "index.html", sizeof(filename
));
1835 strncat(filename
, "/index.html", sizeof(filename
));
1837 filename
[sizeof(filename
) - 1] = '\0';
1839 status
= stat(filename
, filestats
);
1842 LogMessage(L_DEBUG2
, "get_file() %d filename=%s size=%d",
1843 con
->http
.fd
, filename
, status
? -1 : filestats
->st_size
);
1853 * 'pipe_command()' - Pipe the output of a command to the remote client.
1856 static int /* O - Process ID */
1857 pipe_command(client_t
*con
, /* I - Client connection */
1858 int infile
, /* I - Standard input for command */
1859 int *outfile
, /* O - Standard output for command */
1860 char *command
, /* I - Command to run */
1861 char *options
) /* I - Options for command */
1863 int pid
; /* Process ID */
1864 char *commptr
; /* Command string pointer */
1865 int fd
; /* Looping var */
1866 int fds
[2]; /* Pipe FDs */
1867 int argc
; /* Number of arguments */
1868 int envc
; /* Number of environment variables */
1869 char argbuf
[10240], /* Argument buffer */
1870 *argv
[100], /* Argument strings */
1871 *envp
[100]; /* Environment variables */
1872 char lang
[1024], /* LANG env variable */
1873 content_length
[1024], /* CONTENT_LENGTH env variable */
1874 content_type
[1024], /* CONTENT_TYPE env variable */
1875 ipp_port
[1024], /* Default listen port */
1876 server_port
[1024], /* Default server port */
1877 server_name
[1024], /* Default listen hostname */
1878 remote_host
[1024], /* REMOTE_HOST env variable */
1879 remote_user
[1024], /* REMOTE_USER env variable */
1880 tmpdir
[1024], /* TMPDIR environment variable */
1881 ldpath
[1024], /* LD_LIBRARY_PATH environment variable */
1882 datadir
[1024], /* CUPS_DATADIR environment variable */
1883 root
[1024], /* CUPS_SERVERROOT environment variable */
1884 query_string
[10240]; /* QUERY_STRING env variable */
1888 * Copy the command string...
1891 strncpy(argbuf
, options
, sizeof(argbuf
) - 1);
1892 argbuf
[sizeof(argbuf
) - 1] = '\0';
1895 * Parse the string; arguments can be separated by + and are terminated
1901 for (commptr
= argbuf
, argc
= 1; *commptr
!= '\0' && argc
< 99; commptr
++)
1902 if (*commptr
== ' ' || *commptr
== '+')
1906 while (*commptr
== ' ')
1909 if (*commptr
!= '\0')
1911 argv
[argc
] = commptr
;
1917 else if (*commptr
== '%')
1919 if (commptr
[1] >= '0' && commptr
[1] <= '9')
1920 *commptr
= (commptr
[1] - '0') << 4;
1922 *commptr
= (tolower(commptr
[1]) - 'a' + 10) << 4;
1924 if (commptr
[2] >= '0' && commptr
[2] <= '9')
1925 *commptr
|= commptr
[2] - '0';
1927 *commptr
|= tolower(commptr
[2]) - 'a' + 10;
1929 strcpy(commptr
+ 1, commptr
+ 3);
1931 else if (*commptr
== '?')
1936 if (argv
[0][0] == '\0')
1937 argv
[0] = strrchr(command
, '/') + 1;
1940 * Setup the environment variables as needed...
1943 snprintf(lang
, sizeof(lang
), "LANG=%s",
1944 con
->language
? con
->language
->language
: "C");
1946 if (con
->http
.hostaddr
.addr
.sa_family
== AF_INET6
)
1948 sprintf(ipp_port
, "IPP_PORT=%d", ntohs(con
->http
.hostaddr
.ipv6
.sin6_port
));
1949 sprintf(server_port
, "SERVER_PORT=%d",
1950 ntohs(con
->http
.hostaddr
.ipv6
.sin6_port
));
1953 #endif /* AF_INET6 */
1955 sprintf(ipp_port
, "IPP_PORT=%d", ntohs(con
->http
.hostaddr
.ipv4
.sin_port
));
1956 sprintf(server_port
, "SERVER_PORT=%d",
1957 ntohs(con
->http
.hostaddr
.ipv4
.sin_port
));
1960 snprintf(server_name
, sizeof(server_name
), "SERVER_NAME=%s", ServerName
);
1961 snprintf(remote_host
, sizeof(remote_host
), "REMOTE_HOST=%s", con
->http
.hostname
);
1962 snprintf(remote_user
, sizeof(remote_user
), "REMOTE_USER=%s", con
->username
);
1963 snprintf(tmpdir
, sizeof(tmpdir
), "TMPDIR=%s", TempDir
);
1964 snprintf(datadir
, sizeof(datadir
), "CUPS_DATADIR=%s", DataDir
);
1965 snprintf(root
, sizeof(root
), "CUPS_SERVERROOT=%s", ServerRoot
);
1967 if (getenv("LD_LIBRARY_PATH") != NULL
)
1968 snprintf(ldpath
, sizeof(ldpath
), "LD_LIBRARY_PATH=%s", getenv("LD_LIBRARY_PATH"));
1972 envp
[0] = "PATH=/bin:/usr/bin";
1973 envp
[1] = "SERVER_SOFTWARE=CUPS/1.2";
1974 envp
[2] = "GATEWAY_INTERFACE=CGI/1.1";
1975 envp
[3] = "SERVER_PROTOCOL=HTTP/1.1";
1977 envp
[5] = server_name
;
1978 envp
[6] = server_port
;
1979 envp
[7] = remote_host
;
1980 envp
[8] = remote_user
;
1990 envp
[envc
++] = ldpath
;
1992 if (con
->operation
== HTTP_GET
)
1994 envp
[envc
++] = "REQUEST_METHOD=GET";
1999 * Add GET form variables after ?...
2004 snprintf(query_string
, sizeof(query_string
), "QUERY_STRING=%s", commptr
);
2005 envp
[envc
++] = query_string
;
2010 sprintf(content_length
, "CONTENT_LENGTH=%d", con
->bytes
);
2011 snprintf(content_type
, sizeof(content_type
), "CONTENT_TYPE=%s",
2012 con
->http
.fields
[HTTP_FIELD_CONTENT_TYPE
]);
2014 envp
[envc
++] = "REQUEST_METHOD=POST";
2015 envp
[envc
++] = content_length
;
2016 envp
[envc
++] = content_type
;
2020 * Tell the CGI if we are using encryption...
2023 if (con
->http
.encryption
>= HTTP_ENCRYPT_REQUIRED
)
2024 envp
[envc
++] = "HTTPS=ON";
2029 * Create a pipe for the output...
2034 LogMessage(L_ERROR
, "Unable to create pipes for CGI %s - %s",
2035 argv
[0], strerror(errno
));
2040 * Then execute the command...
2043 if ((pid
= fork()) == 0)
2046 * Child comes here... Close stdin if necessary and dup the pipe to stdout.
2052 * Running as root, so change to non-priviledged user...
2063 * Reset group membership to just the main one we belong to.
2069 * Update stdin/stdout...
2082 * Close extra file descriptors...
2085 for (fd
= 3; fd
< MaxFDs
; fd
++)
2089 * Change umask to restrict permissions on created files...
2095 * Execute the pipe program; if an error occurs, exit with status 1...
2098 execve(command
, argv
, envp
);
2099 perror("execve failed");
2106 * Error - can't fork!
2109 LogMessage(L_ERROR
, "Unable to fork for CGI %s - %s", argv
[0],
2119 * Fork successful - return the PID...
2122 AddCert(pid
, con
->username
);
2124 LogMessage(L_DEBUG
, "CGI %s started - PID = %d", argv
[0], pid
);
2135 * End of "$Id: client.c,v 1.91.2.2 2001/05/13 18:38:34 mike Exp $".