2 * "$Id: client.c,v 1.80 2001/01/22 15:03:58 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...
49 # include <openssl/ssl.h>
50 # include <openssl/rand.h>
51 #endif /* HAVE_LIBSSL */
58 static int check_if_modified(client_t
*con
, struct stat
*filestats
);
59 static void decode_auth(client_t
*con
);
60 static char *get_file(client_t
*con
, struct stat
*filestats
);
61 static int pipe_command(client_t
*con
, int infile
, int *outfile
, char *command
, char *options
);
65 * 'AcceptClient()' - Accept a new client.
69 AcceptClient(listener_t
*lis
) /* I - Listener socket */
71 int val
; /* Parameter value */
72 client_t
*con
; /* New client pointer */
73 unsigned address
;/* Address of client */
74 struct hostent
*host
; /* Host entry for address */
77 DEBUG_printf(("AcceptClient(%08x) %d NumClients = %d\n",
78 lis
, lis
->fd
, NumClients
));
81 * Make sure we don't have a full set of clients already...
84 if (NumClients
== MaxClients
)
88 * Get a pointer to the next available client...
91 con
= Clients
+ NumClients
;
93 memset(con
, 0, sizeof(client_t
));
94 con
->http
.activity
= time(NULL
);
97 * Accept the client and get the remote address...
100 val
= sizeof(struct sockaddr_in
);
102 if ((con
->http
.fd
= accept(lis
->fd
, (struct sockaddr
*)&(con
->http
.hostaddr
),
105 LogMessage(L_ERROR
, "accept() failed - %s.", strerror(errno
));
109 con
->http
.hostaddr
.sin_port
= lis
->address
.sin_port
;
112 * Get the hostname or format the IP address as needed...
115 address
= ntohl(con
->http
.hostaddr
.sin_addr
.s_addr
);
119 host
= gethostbyaddr((char *)&(con
->http
.hostaddr
.sin_addr
),
120 sizeof(struct in_addr
), AF_INET
);
122 host
= gethostbyaddr(&(con
->http
.hostaddr
.sin_addr
),
123 sizeof(struct in_addr
), AF_INET
);
128 if (con
->http
.hostaddr
.sin_addr
.s_addr
== ServerAddr
.sin_addr
.s_addr
)
131 * Map accesses from the same host to the server name.
134 strncpy(con
->http
.hostname
, ServerName
, sizeof(con
->http
.hostname
) - 1);
136 else if (host
== NULL
)
137 sprintf(con
->http
.hostname
, "%d.%d.%d.%d", (address
>> 24) & 255,
138 (address
>> 16) & 255, (address
>> 8) & 255, address
& 255);
140 strncpy(con
->http
.hostname
, host
->h_name
, sizeof(con
->http
.hostname
) - 1);
142 LogMessage(L_DEBUG
, "AcceptClient() %d from %s:%d.", con
->http
.fd
,
143 con
->http
.hostname
, ntohs(con
->http
.hostaddr
.sin_port
));
146 * Add the socket to the select() input mask.
149 fcntl(con
->http
.fd
, F_SETFD
, fcntl(con
->http
.fd
, F_GETFD
) | FD_CLOEXEC
);
151 DEBUG_printf(("AcceptClient: Adding fd %d to InputSet...\n", con
->http
.fd
));
152 FD_SET(con
->http
.fd
, &InputSet
);
157 * Temporarily suspend accept()'s until we lose a client...
160 if (NumClients
== MaxClients
)
165 * See if we are connecting on port 443...
168 if (ntohs(con
->http
.hostaddr
.sin_port
) == 443)
171 * https connection; go secure...
176 #endif /* HAVE_LIBSSL */
181 * 'CloseAllClients()' - Close all remote clients immediately.
185 CloseAllClients(void)
187 while (NumClients
> 0)
188 CloseClient(Clients
);
193 * 'CloseClient()' - Close a remote client.
197 CloseClient(client_t
*con
) /* I - Client to close */
199 int status
; /* Exit status of pipe command */
201 SSL_CTX
*context
; /* Context for encryption */
202 SSL
*conn
; /* Connection for encryption */
203 #endif /* HAVE_LIBSSL */
206 LogMessage(L_DEBUG
, "CloseClient() %d", con
->http
.fd
);
210 * Shutdown encryption as needed...
215 conn
= (SSL
*)(con
->http
.tls
);
216 context
= SSL_get_SSL_CTX(conn
);
219 SSL_CTX_free(context
);
222 con
->http
.tls
= NULL
;
224 #endif /* HAVE_LIBSSL */
227 * Close the socket and clear the file from the input set for select()...
230 if (con
->http
.fd
> 0)
232 DEBUG_printf(("CloseClient: Removing fd %d from InputSet...\n", con
->http
.fd
));
234 FD_CLR(con
->http
.fd
, &InputSet
);
235 FD_CLR(con
->http
.fd
, &OutputSet
);
239 if (con
->pipe_pid
!= 0)
241 DEBUG_printf(("CloseClient: Removing fd %d from InputSet...\n", con
->file
));
242 FD_CLR(con
->file
, &InputSet
);
248 * Close the open data file...
253 kill(con
->pipe_pid
, SIGKILL
);
254 waitpid(con
->pipe_pid
, &status
, WNOHANG
);
257 FD_CLR(con
->file
, &InputSet
);
264 ippDelete(con
->request
);
270 ippDelete(con
->response
);
271 con
->response
= NULL
;
276 cupsLangFree(con
->language
);
277 con
->language
= NULL
;
281 * Re-enable new client connections if we are going back under the
285 if (NumClients
== MaxClients
)
289 * Compact the list of clients as necessary...
294 if (con
< (Clients
+ NumClients
))
295 memcpy(con
, con
+ 1, (Clients
+ NumClients
- con
) * sizeof(client_t
));
300 * 'EncryptClient()' - Enable encryption for the client...
303 int /* O - 1 on success, 0 on error */
304 EncryptClient(client_t
*con
) /* I - Client to encrypt */
307 SSL_CTX
*context
; /* Context for encryption */
308 SSL
*conn
; /* Connection for encryption */
310 context
= SSL_CTX_new(SSLv23_method());
311 conn
= SSL_new(context
);
313 SSL_use_PrivateKey_file(conn
, ServerKey
, SSL_FILETYPE_PEM
);
314 SSL_use_certificate_file(conn
, ServerCertificate
, SSL_FILETYPE_PEM
);
316 SSL_set_fd(conn
, con
->http
.fd
);
317 if (SSL_accept(conn
) != 1)
319 SSL_CTX_free(context
);
324 con
->http
.tls
= conn
;
328 #endif /* HAVE_LIBSSL */
333 * 'ReadClient()' - Read data from a client.
336 int /* O - 1 on success, 0 on error */
337 ReadClient(client_t
*con
) /* I - Client to read from */
339 char line
[8192], /* Line from client... */
340 operation
[64], /* Operation code from socket */
341 version
[64]; /* HTTP version number string */
342 int major
, minor
; /* HTTP version numbers */
343 http_status_t status
; /* Transfer status */
344 ipp_state_t ipp_state
; /* State of IPP transfer */
345 int bytes
; /* Number of bytes to POST */
346 char *filename
; /* Name of file for GET/HEAD */
347 struct stat filestats
; /* File information */
348 mime_type_t
*type
; /* MIME type of file */
349 printer_t
*p
; /* Printer */
350 location_t
*best
; /* Best match for authentication */
351 static unsigned request_id
= 0;/* Request ID for temp files */
354 status
= HTTP_CONTINUE
;
356 switch (con
->http
.state
)
360 * See if we've received a request line...
363 if (httpGets(line
, sizeof(line
) - 1, HTTP(con
)) == NULL
)
370 * Ignore blank request lines...
377 * Clear other state variables...
380 httpClearFields(HTTP(con
));
382 con
->http
.activity
= time(NULL
);
383 con
->http
.version
= HTTP_1_0
;
384 con
->http
.keep_alive
= HTTP_KEEPALIVE_OFF
;
385 con
->http
.data_encoding
= HTTP_ENCODE_LENGTH
;
386 con
->http
.data_remaining
= 0;
387 con
->operation
= HTTP_WAITING
;
391 con
->command
[0] = '\0';
392 con
->username
[0] = '\0';
393 con
->password
[0] = '\0';
396 if (con
->language
!= NULL
)
398 cupsLangFree(con
->language
);
399 con
->language
= NULL
;
403 * Grab the request line...
406 switch (sscanf(line
, "%63s%1023s%63s", operation
, con
->uri
, version
))
409 SendError(con
, HTTP_BAD_REQUEST
);
413 con
->http
.version
= HTTP_0_9
;
416 if (sscanf(version
, "HTTP/%d.%d", &major
, &minor
) != 2)
418 SendError(con
, HTTP_BAD_REQUEST
);
425 con
->http
.version
= (http_version_t
)(major
* 100 + minor
);
426 if (con
->http
.version
== HTTP_1_1
)
427 con
->http
.keep_alive
= HTTP_KEEPALIVE_ON
;
429 con
->http
.keep_alive
= HTTP_KEEPALIVE_OFF
;
433 SendError(con
, HTTP_NOT_SUPPORTED
);
441 * Process the request...
444 if (strcmp(operation
, "GET") == 0)
445 con
->http
.state
= HTTP_GET
;
446 else if (strcmp(operation
, "PUT") == 0)
447 con
->http
.state
= HTTP_PUT
;
448 else if (strcmp(operation
, "POST") == 0)
449 con
->http
.state
= HTTP_POST
;
450 else if (strcmp(operation
, "DELETE") == 0)
451 con
->http
.state
= HTTP_DELETE
;
452 else if (strcmp(operation
, "TRACE") == 0)
453 con
->http
.state
= HTTP_TRACE
;
454 else if (strcmp(operation
, "OPTIONS") == 0)
455 con
->http
.state
= HTTP_OPTIONS
;
456 else if (strcmp(operation
, "HEAD") == 0)
457 con
->http
.state
= HTTP_HEAD
;
460 SendError(con
, HTTP_BAD_REQUEST
);
465 con
->start
= time(NULL
);
466 con
->operation
= con
->http
.state
;
468 LogMessage(L_DEBUG
, "ReadClient() %d %s %s HTTP/%d.%d", con
->http
.fd
,
470 con
->http
.version
/ 100, con
->http
.version
% 100);
472 con
->http
.status
= HTTP_OK
;
482 * Parse incoming parameters until the status changes...
485 status
= httpUpdate(HTTP(con
));
487 if (status
!= HTTP_OK
&& status
!= HTTP_CONTINUE
)
489 SendError(con
, HTTP_BAD_REQUEST
);
496 break; /* Anti-compiler-warning-code */
500 * Handle new transfers...
503 if (status
== HTTP_OK
)
505 con
->language
= cupsLangGet(con
->http
.fields
[HTTP_FIELD_ACCEPT_LANGUAGE
]);
509 if (strncmp(con
->http
.fields
[HTTP_FIELD_CONNECTION
], "Keep-Alive", 10) == 0)
510 con
->http
.keep_alive
= HTTP_KEEPALIVE_ON
;
512 if (con
->http
.fields
[HTTP_FIELD_HOST
][0] == '\0' &&
513 con
->http
.version
>= HTTP_1_1
)
516 * HTTP/1.1 and higher require the "Host:" field...
519 if (!SendError(con
, HTTP_BAD_REQUEST
))
525 else if (con
->operation
== HTTP_OPTIONS
)
528 * Do OPTIONS command...
531 if ((best
= FindBest(con
)) != NULL
&&
532 best
->type
!= AUTH_NONE
)
534 if (!SendHeader(con
, HTTP_UNAUTHORIZED
, NULL
))
541 if ((strcasecmp(con
->http
.fields
[HTTP_FIELD_CONNECTION
], "Upgrade") == 0 ||
542 (best
!= NULL
&& best
->encryption
== HTTP_ENCRYPT_ALWAYS
)) &&
543 con
->http
.tls
== NULL
)
547 * Do encryption stuff...
550 if (!SendHeader(con
, HTTP_SWITCHING_PROTOCOLS
, NULL
))
556 httpPrintf(HTTP(con
), "Connection: Upgrade\r\n");
557 httpPrintf(HTTP(con
), "Upgrade: TLS/1.0,SSL/2.0,SSL/3.0\r\n");
558 httpPrintf(HTTP(con
), "\r\n");
562 if (!SendError(con
, HTTP_NOT_IMPLEMENTED
))
567 #endif /* HAVE_LIBSSL */
570 if (!SendHeader(con
, HTTP_OK
, NULL
))
576 httpPrintf(HTTP(con
), "Allow: GET, HEAD, OPTIONS, POST\r\n");
577 httpPrintf(HTTP(con
), "\r\n");
579 else if (strstr(con
->uri
, "..") != NULL
)
582 * Protect against malicious users!
585 if (!SendError(con
, HTTP_FORBIDDEN
))
591 else if (con
->uri
[0] != '/')
594 * Don't allow proxying (yet)...
597 if (!SendError(con
, HTTP_METHOD_NOT_ALLOWED
))
605 if ((status
= IsAuthorized(con
)) == HTTP_UPGRADE_REQUIRED
)
609 * Do encryption stuff...
612 if (!SendHeader(con
, HTTP_SWITCHING_PROTOCOLS
, NULL
))
618 httpPrintf(HTTP(con
), "Connection: Upgrade\r\n");
619 httpPrintf(HTTP(con
), "Upgrade: TLS/1.0,SSL/2.0,SSL/3.0\r\n");
620 httpPrintf(HTTP(con
), "\r\n");
624 if (!SendError(con
, HTTP_NOT_IMPLEMENTED
))
629 #endif /* HAVE_LIBSSL */
631 else if (status
!= HTTP_OK
)
633 SendError(con
, status
);
638 switch (con
->http
.state
)
641 if (strncmp(con
->uri
, "/printers/", 10) == 0 &&
642 strcmp(con
->uri
+ strlen(con
->uri
) - 4, ".ppd") == 0)
645 * Send PPD file - get the real printer name since printer
646 * names are not case sensitive but filenames can be...
649 con
->uri
[strlen(con
->uri
) - 4] = '\0'; /* Drop ".ppd" */
651 if ((p
= FindPrinter(con
->uri
+ 10)) != NULL
)
652 snprintf(con
->uri
, sizeof(con
->uri
), "/ppd/%s.ppd", p
->name
);
655 if (!SendError(con
, HTTP_NOT_FOUND
))
665 if (strncmp(con
->uri
, "/admin", 6) == 0 ||
666 strncmp(con
->uri
, "/printers", 9) == 0 ||
667 strncmp(con
->uri
, "/classes", 8) == 0 ||
668 strncmp(con
->uri
, "/jobs", 5) == 0)
674 if (strncmp(con
->uri
, "/admin", 6) == 0)
676 snprintf(con
->command
, sizeof(con
->command
),
677 "%s/cgi-bin/admin.cgi", ServerBin
);
678 con
->options
= con
->uri
+ 6;
680 else if (strncmp(con
->uri
, "/printers", 9) == 0)
682 snprintf(con
->command
, sizeof(con
->command
),
683 "%s/cgi-bin/printers.cgi", ServerBin
);
684 con
->options
= con
->uri
+ 9;
686 else if (strncmp(con
->uri
, "/classes", 8) == 0)
688 snprintf(con
->command
, sizeof(con
->command
),
689 "%s/cgi-bin/classes.cgi", ServerBin
);
690 con
->options
= con
->uri
+ 8;
694 snprintf(con
->command
, sizeof(con
->command
),
695 "%s/cgi-bin/jobs.cgi", ServerBin
);
696 con
->options
= con
->uri
+ 5;
699 if (con
->options
[0] == '/')
702 if (!SendCommand(con
, con
->command
, con
->options
))
704 if (!SendError(con
, HTTP_NOT_FOUND
))
711 LogRequest(con
, HTTP_OK
);
713 if (con
->http
.version
<= HTTP_1_0
)
714 con
->http
.keep_alive
= HTTP_KEEPALIVE_OFF
;
722 if ((filename
= get_file(con
, &filestats
)) == NULL
)
724 if (!SendError(con
, HTTP_NOT_FOUND
))
730 else if (!check_if_modified(con
, &filestats
))
732 if (!SendError(con
, HTTP_NOT_MODIFIED
))
740 type
= mimeFileType(MimeDatabase
, filename
);
742 strcpy(line
, "text/plain");
744 snprintf(line
, sizeof(line
), "%s/%s", type
->super
, type
->type
);
746 if (!SendFile(con
, HTTP_OK
, filename
, line
, &filestats
))
755 case HTTP_POST_RECV
:
757 * See if the POST request includes a Content-Length field, and if
758 * so check the length against any limits that are set...
761 LogMessage(L_DEBUG2
, "POST %s", con
->uri
);
762 LogMessage(L_DEBUG2
, "CONTENT_TYPE = %s", con
->http
.fields
[HTTP_FIELD_CONTENT_TYPE
]);
764 if (con
->http
.fields
[HTTP_FIELD_CONTENT_LENGTH
][0] &&
765 atoi(con
->http
.fields
[HTTP_FIELD_CONTENT_LENGTH
]) > MaxRequestSize
&&
769 * Request too large...
772 if (!SendError(con
, HTTP_REQUEST_TOO_LARGE
))
782 * See what kind of POST request this is; for IPP requests the
783 * content-type field will be "application/ipp"...
786 if (strcmp(con
->http
.fields
[HTTP_FIELD_CONTENT_TYPE
], "application/ipp") == 0)
787 con
->request
= ippNew();
788 else if (strncmp(con
->uri
, "/admin", 6) == 0 ||
789 strncmp(con
->uri
, "/printers", 9) == 0 ||
790 strncmp(con
->uri
, "/classes", 8) == 0 ||
791 strncmp(con
->uri
, "/jobs", 5) == 0)
797 if (strncmp(con
->uri
, "/admin", 6) == 0)
799 snprintf(con
->command
, sizeof(con
->command
),
800 "%s/cgi-bin/admin.cgi", ServerBin
);
801 con
->options
= con
->uri
+ 6;
803 else if (strncmp(con
->uri
, "/printers", 9) == 0)
805 snprintf(con
->command
, sizeof(con
->command
),
806 "%s/cgi-bin/printers.cgi", ServerBin
);
807 con
->options
= con
->uri
+ 9;
809 else if (strncmp(con
->uri
, "/classes", 8) == 0)
811 snprintf(con
->command
, sizeof(con
->command
),
812 "%s/cgi-bin/classes.cgi", ServerBin
);
813 con
->options
= con
->uri
+ 8;
817 snprintf(con
->command
, sizeof(con
->command
),
818 "%s/cgi-bin/jobs.cgi", ServerBin
);
819 con
->options
= con
->uri
+ 5;
822 if (con
->options
[0] == '/')
825 LogMessage(L_DEBUG2
, "ReadClient() %d command=\"%s\", options = \"%s\"",
826 con
->http
.fd
, con
->command
, con
->options
);
828 if (con
->http
.version
<= HTTP_1_0
)
829 con
->http
.keep_alive
= HTTP_KEEPALIVE_OFF
;
831 else if (!SendError(con
, HTTP_UNAUTHORIZED
))
841 SendError(con
, HTTP_NOT_IMPLEMENTED
);
846 if (strncmp(con
->uri
, "/printers", 9) == 0 &&
847 strcmp(con
->uri
+ strlen(con
->uri
) - 4, ".ppd") == 0)
853 snprintf(con
->command
, sizeof(con
->command
),
854 "/ppd/%s", con
->uri
+ 10);
855 strcpy(con
->uri
, con
->command
);
856 con
->command
[0] = '\0';
859 if (strncmp(con
->uri
, "/admin/", 7) == 0 ||
860 strncmp(con
->uri
, "/printers/", 10) == 0 ||
861 strncmp(con
->uri
, "/classes/", 9) == 0 ||
862 strncmp(con
->uri
, "/jobs/", 6) == 0)
868 if (!SendHeader(con
, HTTP_OK
, "text/html"))
874 if (httpPrintf(HTTP(con
), "\r\n") < 0)
880 LogRequest(con
, HTTP_OK
);
882 else if ((filename
= get_file(con
, &filestats
)) == NULL
)
884 if (!SendHeader(con
, HTTP_NOT_FOUND
, "text/html"))
890 LogRequest(con
, HTTP_NOT_FOUND
);
892 else if (!check_if_modified(con
, &filestats
))
894 if (!SendError(con
, HTTP_NOT_MODIFIED
))
900 LogRequest(con
, HTTP_NOT_MODIFIED
);
908 type
= mimeFileType(MimeDatabase
, filename
);
910 strcpy(line
, "text/plain");
912 snprintf(line
, sizeof(line
), "%s/%s", type
->super
, type
->type
);
914 if (!SendHeader(con
, HTTP_OK
, line
))
920 if (httpPrintf(HTTP(con
), "Last-Modified: %s\r\n",
921 httpGetDateString(filestats
.st_mtime
)) < 0)
927 if (httpPrintf(HTTP(con
), "Content-Length: %d\r\n",
928 filestats
.st_size
) < 0)
934 LogRequest(con
, HTTP_OK
);
937 if (httpPrintf(HTTP(con
), "\r\n") < 0)
943 con
->http
.state
= HTTP_WAITING
;
947 break; /* Anti-compiler-warning-code */
953 * Handle any incoming data...
956 switch (con
->http
.state
)
961 case HTTP_POST_RECV
:
962 LogMessage(L_DEBUG2
, "ReadClient() %d con->data_encoding = %s, con->data_remaining = %d, con->file = %d",
964 con
->http
.data_encoding
== HTTP_ENCODE_CHUNKED
? "chunked" : "length",
965 con
->http
.data_remaining
, con
->file
);
967 if (con
->request
!= NULL
)
970 * Grab any request data from the connection...
973 if ((ipp_state
= ippRead(&(con
->http
), con
->request
)) == IPP_ERROR
)
975 LogMessage(L_ERROR
, "ReadClient() %d IPP Read Error!",
980 else if (ipp_state
!= IPP_DATA
)
983 con
->bytes
+= ippLength(con
->request
);
986 if (con
->file
== 0 && con
->http
.state
!= HTTP_POST_SEND
)
989 * Create a file as needed for the request data...
992 snprintf(con
->filename
, sizeof(con
->filename
), "%s/%08x",
993 RequestRoot
, request_id
++);
994 con
->file
= open(con
->filename
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0640);
995 fchmod(con
->file
, 0640);
996 fchown(con
->file
, User
, Group
);
998 LogMessage(L_DEBUG2
, "ReadClient() %d REQUEST %s", con
->http
.fd
,
1003 if (!SendError(con
, HTTP_REQUEST_TOO_LARGE
))
1011 if (con
->http
.state
!= HTTP_POST_SEND
)
1013 if ((bytes
= httpRead(HTTP(con
), line
, sizeof(line
))) < 0)
1020 con
->bytes
+= bytes
;
1022 LogMessage(L_DEBUG2
, "ReadClient() %d writing %d bytes",
1023 con
->http
.fd
, bytes
);
1025 if (write(con
->file
, line
, bytes
) < bytes
)
1029 unlink(con
->filename
);
1031 if (!SendError(con
, HTTP_REQUEST_TOO_LARGE
))
1038 else if (con
->http
.state
!= HTTP_POST_SEND
)
1045 if (con
->http
.state
== HTTP_POST_SEND
)
1049 fstat(con
->file
, &filestats
);
1053 if (filestats
.st_size
> MaxRequestSize
&&
1057 * Request is too big; remove it and send an error...
1060 unlink(con
->filename
);
1065 * Delete any IPP request data...
1068 ippDelete(con
->request
);
1069 con
->request
= NULL
;
1072 if (!SendError(con
, HTTP_REQUEST_TOO_LARGE
))
1079 if (con
->command
[0])
1081 if (!SendCommand(con
, con
->command
, con
->options
))
1083 if (!SendError(con
, HTTP_NOT_FOUND
))
1090 LogRequest(con
, HTTP_OK
);
1095 ProcessIPPRequest(con
);
1100 break; /* Anti-compiler-warning-code */
1103 if (!con
->http
.keep_alive
&& con
->http
.state
== HTTP_WAITING
)
1114 * 'SendCommand()' - Send output from a command via HTTP.
1118 SendCommand(client_t
*con
,
1125 if (con
->filename
[0])
1126 fd
= open(con
->filename
, O_RDONLY
);
1128 fd
= open("/dev/null", O_RDONLY
);
1130 con
->pipe_pid
= pipe_command(con
, fd
, &(con
->file
), command
, options
);
1134 LogMessage(L_DEBUG
, "SendCommand() %d command=\"%s\" file=%d pipe_pid=%d",
1135 con
->http
.fd
, command
, con
->file
, con
->pipe_pid
);
1137 if (con
->pipe_pid
== 0)
1140 fcntl(con
->file
, F_SETFD
, fcntl(con
->file
, F_GETFD
) | FD_CLOEXEC
);
1142 DEBUG_printf(("SendCommand: Adding fd %d to InputSet...\n", con
->file
));
1143 FD_SET(con
->file
, &InputSet
);
1144 FD_SET(con
->http
.fd
, &OutputSet
);
1146 if (!SendHeader(con
, HTTP_OK
, NULL
))
1149 if (con
->http
.version
== HTTP_1_1
)
1151 con
->http
.data_encoding
= HTTP_ENCODE_CHUNKED
;
1153 if (httpPrintf(HTTP(con
), "Transfer-Encoding: chunked\r\n") < 0)
1157 con
->got_fields
= 0;
1165 * 'SendError()' - Send an error message via HTTP.
1168 int /* O - 1 if successful, 0 otherwise */
1169 SendError(client_t
*con
, /* I - Connection */
1170 http_status_t code
) /* I - Error code */
1172 char message
[1024]; /* Message for user */
1176 * Put the request in the access_log file...
1179 if (con
->operation
> HTTP_WAITING
)
1180 LogRequest(con
, code
);
1183 * To work around bugs in some proxies, don't use Keep-Alive for some
1187 if (code
>= HTTP_BAD_REQUEST
)
1188 con
->http
.keep_alive
= HTTP_KEEPALIVE_OFF
;
1191 * Send an error message back to the client. If the error code is a
1192 * 400 or 500 series, make sure the message contains some text, too!
1195 if (!SendHeader(con
, code
, NULL
))
1199 if (code
== HTTP_UPGRADE_REQUIRED
)
1201 if (httpPrintf(HTTP(con
), "Connection: Upgrade\r\n") < 0)
1204 if (httpPrintf(HTTP(con
), "Upgrade: TLS/1.0,SSL/2.0,SSL/3.0\r\n") < 0)
1207 #endif /* HAVE_LIBSSL */
1209 if (con
->http
.version
>= HTTP_1_1
&& !con
->http
.keep_alive
)
1211 if (httpPrintf(HTTP(con
), "Connection: close\r\n") < 0)
1215 if (code
>= HTTP_BAD_REQUEST
)
1218 * Send a human-readable error message.
1221 snprintf(message
, sizeof(message
),
1222 "<HTML><HEAD><TITLE>%d %s</TITLE></HEAD>"
1223 "<BODY><H1>%s</H1>%s</BODY></HTML>\n",
1224 code
, httpStatus(code
), httpStatus(code
),
1225 con
->language
? con
->language
->messages
[code
] :
1228 if (httpPrintf(HTTP(con
), "Content-Type: text/html\r\n") < 0)
1230 if (httpPrintf(HTTP(con
), "Content-Length: %d\r\n", strlen(message
)) < 0)
1232 if (httpPrintf(HTTP(con
), "\r\n") < 0)
1234 if (httpPrintf(HTTP(con
), "%s", message
) < 0)
1237 else if (httpPrintf(HTTP(con
), "\r\n") < 0)
1240 con
->http
.state
= HTTP_WAITING
;
1247 * 'SendFile()' - Send a file via HTTP.
1251 SendFile(client_t
*con
,
1255 struct stat
*filestats
)
1257 con
->file
= open(filename
, O_RDONLY
);
1259 LogMessage(L_DEBUG
, "SendFile() %d file=%d", con
->http
.fd
, con
->file
);
1264 fcntl(con
->file
, F_SETFD
, fcntl(con
->file
, F_GETFD
) | FD_CLOEXEC
);
1268 if (!SendHeader(con
, code
, type
))
1271 if (httpPrintf(HTTP(con
), "Last-Modified: %s\r\n", httpGetDateString(filestats
->st_mtime
)) < 0)
1273 if (httpPrintf(HTTP(con
), "Content-Length: %d\r\n", filestats
->st_size
) < 0)
1275 if (httpPrintf(HTTP(con
), "\r\n") < 0)
1278 FD_SET(con
->http
.fd
, &OutputSet
);
1285 * 'SendHeader()' - Send an HTTP request.
1288 int /* O - 1 on success, 0 on failure */
1289 SendHeader(client_t
*con
, /* I - Client to send to */
1290 http_status_t code
, /* I - HTTP status code */
1291 char *type
) /* I - MIME type of document */
1293 location_t
*loc
; /* Authentication location */
1296 if (httpPrintf(HTTP(con
), "HTTP/%d.%d %d %s\r\n", con
->http
.version
/ 100,
1297 con
->http
.version
% 100, code
, httpStatus(code
)) < 0)
1299 if (httpPrintf(HTTP(con
), "Date: %s\r\n", httpGetDateString(time(NULL
))) < 0)
1301 if (httpPrintf(HTTP(con
), "Server: CUPS/1.1\r\n") < 0)
1303 if (con
->http
.keep_alive
&& con
->http
.version
>= HTTP_1_0
)
1305 if (httpPrintf(HTTP(con
), "Connection: Keep-Alive\r\n") < 0)
1307 if (httpPrintf(HTTP(con
), "Keep-Alive: timeout=%d\r\n", KeepAliveTimeout
) < 0)
1310 if (code
== HTTP_METHOD_NOT_ALLOWED
)
1311 if (httpPrintf(HTTP(con
), "Allow: GET, HEAD, OPTIONS, POST\r\n") < 0)
1314 if (code
== HTTP_UNAUTHORIZED
)
1316 loc
= FindBest(con
); /* This already succeeded in IsAuthorized */
1318 if (loc
->type
== AUTH_BASIC
)
1320 if (httpPrintf(HTTP(con
), "WWW-Authenticate: Basic realm=\"CUPS\"\r\n") < 0)
1325 if (httpPrintf(HTTP(con
), "WWW-Authenticate: Digest realm=\"CUPS\" "
1326 "nonce=\"%s\"\r\n", con
->http
.hostname
) < 0)
1330 if (con
->language
!= NULL
)
1332 if (httpPrintf(HTTP(con
), "Content-Language: %s\r\n",
1333 con
->language
->language
) < 0)
1337 if (httpPrintf(HTTP(con
), "Content-Type: %s; charset=%s\r\n", type
,
1338 cupsLangEncoding(con
->language
)) < 0)
1341 else if (type
!= NULL
)
1342 if (httpPrintf(HTTP(con
), "Content-Type: %s\r\n", type
) < 0)
1350 * 'WriteClient()' - Write data to a client as needed.
1353 int /* O - 1 if success, 0 if fail */
1354 WriteClient(client_t
*con
) /* I - Client connection */
1356 int bytes
; /* Number of bytes written */
1357 char buf
[HTTP_MAX_BUFFER
+ 1];/* Data buffer */
1358 char *bufptr
; /* Pointer into buffer */
1359 ipp_state_t ipp_state
; /* IPP state value */
1362 if (con
->http
.state
!= HTTP_GET_SEND
&&
1363 con
->http
.state
!= HTTP_POST_SEND
)
1366 if (con
->response
!= NULL
)
1368 ipp_state
= ippWrite(&(con
->http
), con
->response
);
1369 bytes
= ipp_state
!= IPP_ERROR
&& ipp_state
!= IPP_DATA
;
1371 else if ((bytes
= read(con
->file
, buf
, HTTP_MAX_BUFFER
)) > 0)
1373 if (con
->pipe_pid
&& !con
->got_fields
)
1376 * Inspect the data for Content-Type and other fields.
1381 for (bufptr
= buf
; !con
->got_fields
&& *bufptr
; bufptr
++)
1382 if (*bufptr
== '\n')
1385 * Send line to client...
1388 if (bufptr
> buf
&& bufptr
[-1] == '\r')
1392 httpPrintf(HTTP(con
), "%s\r\n", buf
);
1393 LogMessage(L_DEBUG2
, "WriteClient() %d %s", con
->http
.fd
, buf
);
1399 bytes
-= (bufptr
- buf
);
1400 memcpy(buf
, bufptr
, bytes
+ 1);
1404 * See if the line was empty...
1407 if (con
->field_col
== 0)
1408 con
->got_fields
= 1;
1412 else if (*bufptr
!= '\r')
1415 if (bytes
> 0 && !con
->got_fields
)
1418 * Remaining text needs to go out...
1421 httpPrintf(HTTP(con
), "%s", buf
);
1423 con
->http
.activity
= time(NULL
);
1426 else if (bytes
== 0)
1428 con
->http
.activity
= time(NULL
);
1433 if (httpWrite(HTTP(con
), buf
, bytes
) < 0)
1439 con
->bytes
+= bytes
;
1444 LogRequest(con
, HTTP_OK
);
1446 if (con
->http
.data_encoding
== HTTP_ENCODE_CHUNKED
)
1448 if (httpPrintf(HTTP(con
), "0\r\n\r\n") < 0)
1455 con
->http
.state
= HTTP_WAITING
;
1457 FD_CLR(con
->http
.fd
, &OutputSet
);
1461 DEBUG_printf(("WriteClient: Removing fd %d from InputSet...\n", con
->file
));
1462 FD_CLR(con
->file
, &InputSet
);
1465 kill(con
->pipe_pid
, SIGTERM
);
1472 if (con
->filename
[0])
1473 unlink(con
->filename
);
1475 if (con
->request
!= NULL
)
1477 ippDelete(con
->request
);
1478 con
->request
= NULL
;
1481 if (con
->response
!= NULL
)
1483 ippDelete(con
->response
);
1484 con
->response
= NULL
;
1487 if (!con
->http
.keep_alive
)
1495 LogMessage(L_DEBUG2
, "WriteClient() %d %d bytes", con
->http
.fd
, bytes
);
1497 con
->http
.activity
= time(NULL
);
1504 * 'check_if_modified()' - Decode an "If-Modified-Since" line.
1507 static int /* O - 1 if modified since */
1508 check_if_modified(client_t
*con
, /* I - Client connection */
1509 struct stat
*filestats
) /* I - File information */
1511 char *ptr
; /* Pointer into field */
1512 time_t date
; /* Time/date value */
1513 int size
; /* Size/length value */
1518 ptr
= con
->http
.fields
[HTTP_FIELD_IF_MODIFIED_SINCE
];
1523 LogMessage(L_DEBUG2
, "check_if_modified() %d If-Modified-Since=\"%s\"",
1526 while (*ptr
!= '\0')
1528 while (isspace(*ptr
) || *ptr
== ';')
1531 if (strncasecmp(ptr
, "length=", 7) == 0)
1536 while (isdigit(*ptr
))
1539 else if (isalpha(*ptr
))
1541 date
= httpGetDateTime(ptr
);
1542 while (*ptr
!= '\0' && *ptr
!= ';')
1547 LogMessage(L_DEBUG2
, "check_if_modified() %d sizes=%d,%d dates=%d,%d",
1548 con
->http
.fd
, size
, filestats
->st_size
, date
, filestats
->st_mtime
);
1550 return ((size
!= filestats
->st_size
&& size
!= 0) ||
1551 (date
< filestats
->st_mtime
&& date
!= 0) ||
1552 (size
== 0 && date
== 0));
1557 * 'decode_auth()' - Decode an authorization string.
1561 decode_auth(client_t
*con
) /* I - Client to decode to */
1563 char *s
, /* Authorization string */
1564 value
[1024]; /* Value string */
1565 const char *username
; /* Certificate username */
1569 * Decode the string...
1572 s
= con
->http
.fields
[HTTP_FIELD_AUTHORIZATION
];
1574 DEBUG_printf(("decode_auth(%08x): Authorization string = \"%s\"\n",
1577 if (strncmp(s
, "Basic", 5) == 0)
1583 httpDecode64(value
, s
);
1586 * Pull the username and password out...
1589 if ((s
= strchr(value
, ':')) == NULL
)
1591 LogMessage(L_DEBUG
, "decode_auth() %d no colon in auth string \"%s\"",
1592 con
->http
.fd
, value
);
1598 strncpy(con
->username
, value
, sizeof(con
->username
) - 1);
1599 con
->username
[sizeof(con
->username
) - 1] = '\0';
1601 strncpy(con
->password
, s
, sizeof(con
->password
) - 1);
1602 con
->password
[sizeof(con
->password
) - 1] = '\0';
1604 else if (strncmp(s
, "Local", 5) == 0)
1610 if ((username
= FindCert(s
)) != NULL
)
1612 strncpy(con
->username
, username
, sizeof(con
->username
) - 1);
1613 con
->username
[sizeof(con
->username
) - 1] = '\0';
1616 else if (strncmp(s
, "Digest", 5) == 0)
1619 * Get the username and password from the Digest attributes...
1622 if (httpGetSubField(&(con
->http
), HTTP_FIELD_WWW_AUTHENTICATE
, "username",
1625 strncpy(con
->username
, value
, sizeof(con
->username
) - 1);
1626 con
->username
[sizeof(con
->username
) - 1] = '\0';
1629 if (httpGetSubField(&(con
->http
), HTTP_FIELD_WWW_AUTHENTICATE
, "response",
1632 strncpy(con
->password
, value
, sizeof(con
->password
) - 1);
1633 con
->password
[sizeof(con
->password
) - 1] = '\0';
1637 LogMessage(L_DEBUG
, "decode_auth() %d username=\"%s\"",
1638 con
->http
.fd
, con
->username
);
1639 DEBUG_printf(("decode_auth() %d username=\"%s\" password=\"%s\"\n",
1640 con
->http
.fd
, con
->username
, con
->password
));
1645 * 'get_file()' - Get a filename and state info.
1648 static char * /* O - Real filename */
1649 get_file(client_t
*con
, /* I - Client connection */
1650 struct stat
*filestats
)/* O - File information */
1652 int status
; /* Status of filesystem calls */
1653 char *params
; /* Pointer to parameters in URI */
1654 static char filename
[1024]; /* Filename buffer */
1658 * Need to add DocumentRoot global...
1661 if (strncmp(con
->uri
, "/ppd/", 5) == 0)
1662 snprintf(filename
, sizeof(filename
), "%s%s", ServerRoot
, con
->uri
);
1663 else if (con
->language
!= NULL
)
1664 snprintf(filename
, sizeof(filename
), "%s/%s%s", DocumentRoot
, con
->language
->language
,
1667 snprintf(filename
, sizeof(filename
), "%s%s", DocumentRoot
, con
->uri
);
1669 if ((params
= strchr(filename
, '?')) != NULL
)
1673 * Grab the status for this language; if there isn't a language-specific file
1674 * then fallback to the default one...
1677 if ((status
= stat(filename
, filestats
)) != 0 && con
->language
!= NULL
)
1680 * Drop the language prefix and try the current directory...
1683 if (strncmp(con
->uri
, "/ppd/", 5) != 0)
1685 snprintf(filename
, sizeof(filename
), "%s%s", DocumentRoot
, con
->uri
);
1687 status
= stat(filename
, filestats
);
1692 * If we're found a directory, get the index.html file instead...
1695 if (!status
&& S_ISDIR(filestats
->st_mode
))
1697 if (filename
[strlen(filename
) - 1] == '/')
1698 strcat(filename
, "index.html");
1700 strcat(filename
, "/index.html");
1702 status
= stat(filename
, filestats
);
1705 LogMessage(L_DEBUG2
, "get_file() %d filename=%s size=%d",
1706 con
->http
.fd
, filename
, status
? -1 : filestats
->st_size
);
1716 * 'pipe_command()' - Pipe the output of a command to the remote client.
1719 static int /* O - Process ID */
1720 pipe_command(client_t
*con
, /* I - Client connection */
1721 int infile
, /* I - Standard input for command */
1722 int *outfile
, /* O - Standard output for command */
1723 char *command
, /* I - Command to run */
1724 char *options
) /* I - Options for command */
1726 int pid
; /* Process ID */
1727 char *commptr
; /* Command string pointer */
1728 int fd
; /* Looping var */
1729 int fds
[2]; /* Pipe FDs */
1730 int argc
; /* Number of arguments */
1731 int envc
; /* Number of environment variables */
1732 char argbuf
[10240], /* Argument buffer */
1733 *argv
[100], /* Argument strings */
1734 *envp
[100]; /* Environment variables */
1735 char lang
[1024], /* LANG env variable */
1736 content_length
[1024], /* CONTENT_LENGTH env variable */
1737 content_type
[1024], /* CONTENT_TYPE env variable */
1738 ipp_port
[1024], /* Default listen port */
1739 server_port
[1024], /* Default server port */
1740 server_name
[1024], /* Default listen hostname */
1741 remote_host
[1024], /* REMOTE_HOST env variable */
1742 remote_user
[1024], /* REMOTE_USER env variable */
1743 tmpdir
[1024], /* TMPDIR environment variable */
1744 ldpath
[1024], /* LD_LIBRARY_PATH environment variable */
1745 datadir
[1024], /* CUPS_DATADIR environment variable */
1746 root
[1024], /* CUPS_SERVERROOT environment variable */
1747 query_string
[10240]; /* QUERY_STRING env variable */
1751 * Copy the command string...
1754 strncpy(argbuf
, options
, sizeof(argbuf
) - 1);
1755 argbuf
[sizeof(argbuf
) - 1] = '\0';
1758 * Parse the string; arguments can be separated by + and are terminated
1764 for (commptr
= argbuf
, argc
= 1; *commptr
!= '\0' && argc
< 99; commptr
++)
1765 if (*commptr
== ' ' || *commptr
== '+')
1769 while (*commptr
== ' ')
1772 if (*commptr
!= '\0')
1774 argv
[argc
] = commptr
;
1780 else if (*commptr
== '%')
1782 if (commptr
[1] >= '0' && commptr
[1] <= '9')
1783 *commptr
= (commptr
[1] - '0') << 4;
1785 *commptr
= (tolower(commptr
[1]) - 'a' + 10) << 4;
1787 if (commptr
[2] >= '0' && commptr
[2] <= '9')
1788 *commptr
|= commptr
[2] - '0';
1790 *commptr
|= tolower(commptr
[2]) - 'a' + 10;
1792 strcpy(commptr
+ 1, commptr
+ 3);
1794 else if (*commptr
== '?')
1799 if (argv
[0][0] == '\0')
1800 argv
[0] = strrchr(command
, '/') + 1;
1803 * Setup the environment variables as needed...
1806 snprintf(lang
, sizeof(lang
), "LANG=%s",
1807 con
->language
? con
->language
->language
: "C");
1808 sprintf(ipp_port
, "IPP_PORT=%d", ntohs(con
->http
.hostaddr
.sin_port
));
1809 sprintf(server_port
, "SERVER_PORT=%d", ntohs(con
->http
.hostaddr
.sin_port
));
1810 snprintf(server_name
, sizeof(server_name
), "SERVER_NAME=%s", ServerName
);
1811 snprintf(remote_host
, sizeof(remote_host
), "REMOTE_HOST=%s", con
->http
.hostname
);
1812 snprintf(remote_user
, sizeof(remote_user
), "REMOTE_USER=%s", con
->username
);
1813 snprintf(tmpdir
, sizeof(tmpdir
), "TMPDIR=%s", TempDir
);
1814 snprintf(datadir
, sizeof(datadir
), "CUPS_DATADIR=%s", DataDir
);
1815 snprintf(root
, sizeof(root
), "CUPS_SERVERROOT=%s", ServerRoot
);
1817 if (getenv("LD_LIBRARY_PATH") != NULL
)
1818 snprintf(ldpath
, sizeof(ldpath
), "LD_LIBRARY_PATH=%s", getenv("LD_LIBRARY_PATH"));
1822 envp
[0] = "PATH=/bin:/usr/bin";
1823 envp
[1] = "SERVER_SOFTWARE=CUPS/1.1";
1824 envp
[2] = "GATEWAY_INTERFACE=CGI/1.1";
1825 envp
[3] = "SERVER_PROTOCOL=HTTP/1.1";
1827 envp
[5] = server_name
;
1828 envp
[6] = server_port
;
1829 envp
[7] = remote_host
;
1830 envp
[8] = remote_user
;
1840 envp
[envc
++] = ldpath
;
1842 if (con
->operation
== HTTP_GET
)
1844 envp
[envc
++] = "REQUEST_METHOD=GET";
1849 * Add GET form variables after ?...
1854 snprintf(query_string
, sizeof(query_string
), "QUERY_STRING=%s", commptr
);
1855 envp
[envc
++] = query_string
;
1860 sprintf(content_length
, "CONTENT_LENGTH=%d", con
->bytes
);
1861 snprintf(content_type
, sizeof(content_type
), "CONTENT_TYPE=%s",
1862 con
->http
.fields
[HTTP_FIELD_CONTENT_TYPE
]);
1864 envp
[envc
++] = "REQUEST_METHOD=POST";
1865 envp
[envc
++] = content_length
;
1866 envp
[envc
++] = content_type
;
1872 * Create a pipe for the output...
1877 LogMessage(L_ERROR
, "Unable to create pipes for CGI %s - %s",
1878 argv
[0], strerror(errno
));
1883 * Then execute the command...
1886 if ((pid
= fork()) == 0)
1889 * Child comes here... Close stdin if necessary and dup the pipe to stdout.
1905 * Close extra file descriptors...
1908 for (fd
= 3; fd
< MaxFDs
; fd
++)
1912 * Change umask to restrict permissions on created files...
1918 * Execute the pipe program; if an error occurs, exit with status 1...
1921 execve(command
, argv
, envp
);
1922 perror("execve failed");
1929 * Error - can't fork!
1932 LogMessage(L_ERROR
, "Unable to fork for CGI %s - %s", argv
[0],
1942 * Fork successful - return the PID...
1945 AddCert(pid
, con
->username
);
1947 LogMessage(L_DEBUG
, "CGI %s started - PID = %d", argv
[0], pid
);
1958 * End of "$Id: client.c,v 1.80 2001/01/22 15:03:58 mike Exp $".