2 * "$Id: client.c,v 1.56 2000/05/09 14:10:15 mike Exp $"
4 * Client routines for the Common UNIX Printing System (CUPS) scheduler.
6 * Copyright 1997-2000 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 * ReadClient() - Read data from a client.
30 * SendCommand() - Send output from a command via HTTP.
31 * SendError() - Send an error message via HTTP.
32 * SendFile() - Send a file via HTTP.
33 * SendHeader() - Send an HTTP request.
34 * WriteClient() - Write data to a client as needed.
35 * check_if_modified() - Decode an "If-Modified-Since" line.
36 * decode_auth() - Decode an authorization string.
37 * get_file() - Get a filename and state info.
38 * pipe_command() - Pipe the output of a command to the remote client.
42 * Include necessary headers...
52 static int check_if_modified(client_t
*con
, struct stat
*filestats
);
53 static void decode_auth(client_t
*con
);
54 static char *get_file(client_t
*con
, struct stat
*filestats
);
55 static int pipe_command(client_t
*con
, int infile
, int *outfile
, char *command
, char *options
);
59 * 'AcceptClient()' - Accept a new client.
63 AcceptClient(listener_t
*lis
) /* I - Listener socket */
65 int i
; /* Looping var */
66 int val
; /* Parameter value */
67 client_t
*con
; /* New client pointer */
68 unsigned address
;/* Address of client */
69 struct hostent
*host
; /* Host entry for address */
72 DEBUG_printf(("AcceptClient(%08x) %d NumClients = %d\n",
73 lis
, lis
->fd
, NumClients
));
76 * Get a pointer to the next available client...
79 con
= Clients
+ NumClients
;
81 memset(con
, 0, sizeof(client_t
));
82 con
->http
.activity
= time(NULL
);
85 * Accept the client and get the remote address...
88 val
= sizeof(struct sockaddr_in
);
90 if ((con
->http
.fd
= accept(lis
->fd
, (struct sockaddr
*)&(con
->http
.hostaddr
),
93 LogMessage(L_ERROR
, "accept() failed - %s.", strerror(errno
));
97 con
->http
.hostaddr
.sin_port
= lis
->address
.sin_port
;
100 * Get the hostname or format the IP address as needed...
103 address
= ntohl(con
->http
.hostaddr
.sin_addr
.s_addr
);
107 host
= gethostbyaddr((char *)&(con
->http
.hostaddr
.sin_addr
),
108 sizeof(struct in_addr
), AF_INET
);
110 host
= gethostbyaddr(&(con
->http
.hostaddr
.sin_addr
),
111 sizeof(struct in_addr
), AF_INET
);
117 sprintf(con
->http
.hostname
, "%d.%d.%d.%d", (address
>> 24) & 255,
118 (address
>> 16) & 255, (address
>> 8) & 255, address
& 255);
120 strncpy(con
->http
.hostname
, host
->h_name
, sizeof(con
->http
.hostname
) - 1);
122 LogMessage(L_DEBUG
, "accept() %d from %s:%d.", con
->http
.fd
,
123 con
->http
.hostname
, ntohs(con
->http
.hostaddr
.sin_port
));
126 * Add the socket to the select() input mask.
129 fcntl(con
->http
.fd
, F_SETFD
, fcntl(con
->http
.fd
, F_GETFD
) | FD_CLOEXEC
);
131 DEBUG_printf(("AcceptClient: Adding fd %d to InputSet...\n", con
->http
.fd
));
132 FD_SET(con
->http
.fd
, &InputSet
);
137 * Temporarily suspend accept()'s until we lose a client...
140 if (NumClients
== MaxClients
)
141 for (i
= 0; i
< NumListeners
; i
++)
143 DEBUG_printf(("AcceptClient: Removing fd %d from InputSet...\n", Listeners
[i
].fd
));
144 FD_CLR(Listeners
[i
].fd
, &InputSet
);
150 * 'CloseAllClients()' - Close all remote clients immediately.
154 CloseAllClients(void)
156 while (NumClients
> 0)
157 CloseClient(Clients
);
162 * 'CloseClient()' - Close a remote client.
166 CloseClient(client_t
*con
) /* I - Client to close */
168 int i
; /* Looping var */
169 int status
; /* Exit status of pipe command */
172 LogMessage(L_DEBUG
, "CloseClient() %d", con
->http
.fd
);
175 * Close the socket and clear the file from the input set for select()...
178 if (con
->http
.fd
> 0)
180 DEBUG_printf(("CloseClient: Removing fd %d from InputSet...\n", con
->http
.fd
));
182 FD_CLR(con
->http
.fd
, &InputSet
);
183 FD_CLR(con
->http
.fd
, &OutputSet
);
187 for (i
= 0; i
< NumListeners
; i
++)
189 DEBUG_printf(("CloseClient: Adding fd %d to InputSet...\n", Listeners
[i
].fd
));
190 FD_SET(Listeners
[i
].fd
, &InputSet
);
193 if (con
->pipe_pid
!= 0)
195 DEBUG_printf(("CloseClient: Removing fd %d from InputSet...\n", con
->file
));
196 FD_CLR(con
->file
, &InputSet
);
200 * If we have a data file open, close it...
207 kill(con
->pipe_pid
, SIGKILL
);
208 waitpid(con
->pipe_pid
, &status
, WNOHANG
);
211 FD_CLR(con
->file
, &InputSet
);
217 * Compact the list of clients as necessary...
222 if (con
< (Clients
+ NumClients
))
223 memcpy(con
, con
+ 1, (Clients
+ NumClients
- con
) * sizeof(client_t
));
228 * 'ReadClient()' - Read data from a client.
231 int /* O - 1 on success, 0 on error */
232 ReadClient(client_t
*con
) /* I - Client to read from */
234 char line
[8192], /* Line from client... */
235 operation
[64], /* Operation code from socket */
236 version
[64]; /* HTTP version number string */
237 int major
, minor
; /* HTTP version numbers */
238 http_status_t status
; /* Transfer status */
239 ipp_state_t ipp_state
; /* State of IPP transfer */
240 int bytes
; /* Number of bytes to POST */
241 char *filename
; /* Name of file for GET/HEAD */
242 struct stat filestats
; /* File information */
243 mime_type_t
*type
; /* MIME type of file */
244 printer_t
*p
; /* Printer */
245 static unsigned request_id
= 0;/* Request ID for temp files */
248 status
= HTTP_CONTINUE
;
250 switch (con
->http
.state
)
254 * See if we've received a request line...
257 if (httpGets(line
, sizeof(line
) - 1, HTTP(con
)) == NULL
)
264 * Ignore blank request lines...
271 * Clear other state variables...
274 httpClearFields(HTTP(con
));
276 con
->http
.activity
= time(NULL
);
277 con
->http
.version
= HTTP_1_0
;
278 con
->http
.keep_alive
= HTTP_KEEPALIVE_OFF
;
279 con
->http
.data_encoding
= HTTP_ENCODE_LENGTH
;
280 con
->http
.data_remaining
= 0;
281 con
->operation
= HTTP_WAITING
;
285 con
->command
[0] = '\0';
286 con
->username
[0] = '\0';
287 con
->password
[0] = '\0';
290 if (con
->language
!= NULL
)
292 cupsLangFree(con
->language
);
293 con
->language
= NULL
;
297 * Grab the request line...
300 switch (sscanf(line
, "%63s%1023s%63s", operation
, con
->uri
, version
))
303 SendError(con
, HTTP_BAD_REQUEST
);
307 con
->http
.version
= HTTP_0_9
;
310 if (sscanf(version
, "HTTP/%d.%d", &major
, &minor
) != 2)
312 SendError(con
, HTTP_BAD_REQUEST
);
319 con
->http
.version
= (http_version_t
)(major
* 100 + minor
);
320 if (con
->http
.version
== HTTP_1_1
)
321 con
->http
.keep_alive
= HTTP_KEEPALIVE_ON
;
323 con
->http
.keep_alive
= HTTP_KEEPALIVE_OFF
;
327 SendError(con
, HTTP_NOT_SUPPORTED
);
335 * Process the request...
338 if (strcmp(operation
, "GET") == 0)
339 con
->http
.state
= HTTP_GET
;
340 else if (strcmp(operation
, "PUT") == 0)
341 con
->http
.state
= HTTP_PUT
;
342 else if (strcmp(operation
, "POST") == 0)
343 con
->http
.state
= HTTP_POST
;
344 else if (strcmp(operation
, "DELETE") == 0)
345 con
->http
.state
= HTTP_DELETE
;
346 else if (strcmp(operation
, "TRACE") == 0)
347 con
->http
.state
= HTTP_TRACE
;
348 else if (strcmp(operation
, "CLOSE") == 0)
349 con
->http
.state
= HTTP_CLOSE
;
350 else if (strcmp(operation
, "OPTIONS") == 0)
351 con
->http
.state
= HTTP_OPTIONS
;
352 else if (strcmp(operation
, "HEAD") == 0)
353 con
->http
.state
= HTTP_HEAD
;
356 SendError(con
, HTTP_BAD_REQUEST
);
361 con
->start
= time(NULL
);
362 con
->operation
= con
->http
.state
;
364 LogMessage(L_DEBUG
, "ReadClient() %d %s %s HTTP/%d.%d", con
->http
.fd
,
366 con
->http
.version
/ 100, con
->http
.version
% 100);
368 con
->http
.status
= HTTP_OK
;
379 * Parse incoming parameters until the status changes...
382 status
= httpUpdate(HTTP(con
));
384 if (status
!= HTTP_OK
&& status
!= HTTP_CONTINUE
)
386 SendError(con
, HTTP_BAD_REQUEST
);
393 break; /* Anti-compiler-warning-code */
397 * Handle new transfers...
400 if (status
== HTTP_OK
)
402 con
->language
= cupsLangGet(con
->http
.fields
[HTTP_FIELD_ACCEPT_LANGUAGE
]);
406 if (strncmp(con
->http
.fields
[HTTP_FIELD_CONNECTION
], "Keep-Alive", 10) == 0)
407 con
->http
.keep_alive
= HTTP_KEEPALIVE_ON
;
409 if (con
->http
.fields
[HTTP_FIELD_HOST
][0] == '\0' &&
410 con
->http
.version
>= HTTP_1_1
)
413 * HTTP/1.1 and higher require the "Host:" field...
416 if (!SendError(con
, HTTP_BAD_REQUEST
))
422 else if (strstr(con
->uri
, "..") != NULL
)
425 * Protect against malicious users!
428 if (!SendError(con
, HTTP_FORBIDDEN
))
434 else if (con
->uri
[0] != '/')
437 * Don't allow proxying (yet)...
440 if (!SendError(con
, HTTP_METHOD_NOT_ALLOWED
))
446 else if ((status
= IsAuthorized(con
)) != HTTP_OK
)
448 SendError(con
, status
);
452 else switch (con
->http
.state
)
455 if (strncmp(con
->uri
, "/printers/", 10) == 0 &&
456 strcmp(con
->uri
+ strlen(con
->uri
) - 4, ".ppd") == 0)
459 * Send PPD file - get the real printer name since printer
460 * names are not case sensitive but filename can be...
463 con
->uri
[strlen(con
->uri
) - 4] = '\0'; /* Drop ".ppd" */
465 if ((p
= FindPrinter(con
->uri
+ 10)) != NULL
)
466 sprintf(con
->uri
, "/ppd/%s.ppd", p
->name
);
469 if (!SendError(con
, HTTP_NOT_FOUND
))
479 if (strncmp(con
->uri
, "/admin", 6) == 0 ||
480 strncmp(con
->uri
, "/printers", 9) == 0 ||
481 strncmp(con
->uri
, "/classes", 8) == 0 ||
482 strncmp(con
->uri
, "/jobs", 5) == 0)
488 if (strncmp(con
->uri
, "/admin", 6) == 0)
490 snprintf(con
->command
, sizeof(con
->command
),
491 "%s/cgi-bin/admin.cgi", ServerBin
);
492 con
->options
= con
->uri
+ 6;
494 else if (strncmp(con
->uri
, "/printers", 9) == 0)
496 snprintf(con
->command
, sizeof(con
->command
),
497 "%s/cgi-bin/printers.cgi", ServerBin
);
498 con
->options
= con
->uri
+ 9;
500 else if (strncmp(con
->uri
, "/classes", 8) == 0)
502 snprintf(con
->command
, sizeof(con
->command
),
503 "%s/cgi-bin/classes.cgi", ServerBin
);
504 con
->options
= con
->uri
+ 8;
508 snprintf(con
->command
, sizeof(con
->command
),
509 "%s/cgi-bin/jobs.cgi", ServerBin
);
510 con
->options
= con
->uri
+ 5;
513 if (con
->options
[0] == '/')
516 if (!SendCommand(con
, con
->command
, con
->options
))
518 if (!SendError(con
, HTTP_NOT_FOUND
))
525 LogRequest(con
, HTTP_OK
);
527 if (con
->http
.version
<= HTTP_1_0
)
528 con
->http
.keep_alive
= HTTP_KEEPALIVE_OFF
;
536 if ((filename
= get_file(con
, &filestats
)) == NULL
)
538 if (!SendError(con
, HTTP_NOT_FOUND
))
544 else if (!check_if_modified(con
, &filestats
))
546 if (!SendError(con
, HTTP_NOT_MODIFIED
))
554 type
= mimeFileType(MimeDatabase
, filename
);
556 strcpy(line
, "text/plain");
558 sprintf(line
, "%s/%s", type
->super
, type
->type
);
560 if (!SendFile(con
, HTTP_OK
, filename
, line
, &filestats
))
569 case HTTP_POST_RECV
:
571 * See if the POST request includes a Content-Length field, and if
572 * so check the length against any limits that are set...
575 LogMessage(L_DEBUG
, "POST %s", con
->uri
);
576 LogMessage(L_DEBUG
, "CONTENT_TYPE = %s", con
->http
.fields
[HTTP_FIELD_CONTENT_TYPE
]);
578 if (con
->http
.fields
[HTTP_FIELD_CONTENT_LENGTH
][0] &&
579 atoi(con
->http
.fields
[HTTP_FIELD_CONTENT_LENGTH
]) > MaxRequestSize
&&
583 * Request too large...
586 if (!SendError(con
, HTTP_REQUEST_TOO_LARGE
))
596 * See what kind of POST request this is; for IPP requests the
597 * content-type field will be "application/ipp"...
600 if (strcmp(con
->http
.fields
[HTTP_FIELD_CONTENT_TYPE
], "application/ipp") == 0)
601 con
->request
= ippNew();
602 else if (strncmp(con
->uri
, "/admin", 6) == 0 ||
603 strncmp(con
->uri
, "/printers", 9) == 0 ||
604 strncmp(con
->uri
, "/classes", 8) == 0 ||
605 strncmp(con
->uri
, "/jobs", 5) == 0)
611 if (strncmp(con
->uri
, "/admin", 6) == 0)
613 snprintf(con
->command
, sizeof(con
->command
),
614 "%s/cgi-bin/admin.cgi", ServerBin
);
615 con
->options
= con
->uri
+ 6;
617 else if (strncmp(con
->uri
, "/printers", 9) == 0)
619 snprintf(con
->command
, sizeof(con
->command
),
620 "%s/cgi-bin/printers.cgi", ServerBin
);
621 con
->options
= con
->uri
+ 9;
623 else if (strncmp(con
->uri
, "/classes", 8) == 0)
625 snprintf(con
->command
, sizeof(con
->command
),
626 "%s/cgi-bin/classes.cgi", ServerBin
);
627 con
->options
= con
->uri
+ 8;
631 snprintf(con
->command
, sizeof(con
->command
),
632 "%s/cgi-bin/jobs.cgi", ServerBin
);
633 con
->options
= con
->uri
+ 5;
636 if (con
->options
[0] == '/')
639 LogMessage(L_DEBUG
, "ReadClient() %d command=\"%s\", options = \"%s\"",
640 con
->http
.fd
, con
->command
, con
->options
);
642 if (con
->http
.version
<= HTTP_1_0
)
643 con
->http
.keep_alive
= HTTP_KEEPALIVE_OFF
;
645 else if (!SendError(con
, HTTP_UNAUTHORIZED
))
655 SendError(con
, HTTP_NOT_IMPLEMENTED
);
662 if (strncmp(con
->uri
, "/printers", 9) == 0 &&
663 strcmp(con
->uri
+ strlen(con
->uri
) - 4, ".ppd") == 0)
669 snprintf(con
->command
, sizeof(con
->command
),
670 "/ppd/%s", con
->uri
+ 10);
671 strcpy(con
->uri
, con
->command
);
672 con
->command
[0] = '\0';
675 if (strncmp(con
->uri
, "/admin/", 7) == 0 ||
676 strncmp(con
->uri
, "/printers/", 10) == 0 ||
677 strncmp(con
->uri
, "/classes/", 9) == 0 ||
678 strncmp(con
->uri
, "/jobs/", 6) == 0)
684 if (!SendHeader(con
, HTTP_OK
, "text/html"))
690 if (httpPrintf(HTTP(con
), "\r\n") < 0)
696 LogRequest(con
, HTTP_OK
);
698 else if ((filename
= get_file(con
, &filestats
)) == NULL
)
700 if (!SendHeader(con
, HTTP_NOT_FOUND
, "text/html"))
706 LogRequest(con
, HTTP_NOT_FOUND
);
708 else if (!check_if_modified(con
, &filestats
))
710 if (!SendError(con
, HTTP_NOT_MODIFIED
))
716 LogRequest(con
, HTTP_NOT_MODIFIED
);
724 type
= mimeFileType(MimeDatabase
, filename
);
726 strcpy(line
, "text/plain");
728 sprintf(line
, "%s/%s", type
->super
, type
->type
);
730 if (!SendHeader(con
, HTTP_OK
, line
))
736 if (httpPrintf(HTTP(con
), "Last-Modified: %s\r\n",
737 httpGetDateString(filestats
.st_mtime
)) < 0)
743 if (httpPrintf(HTTP(con
), "Content-Length: %d\r\n",
744 filestats
.st_size
) < 0)
750 LogRequest(con
, HTTP_OK
);
753 if (httpPrintf(HTTP(con
), "\r\n") < 0)
759 con
->http
.state
= HTTP_WAITING
;
763 break; /* Anti-compiler-warning-code */
768 * Handle any incoming data...
771 switch (con
->http
.state
)
776 case HTTP_POST_RECV
:
777 LogMessage(L_DEBUG
, "ReadClient() %d con->data_encoding = %s, con->data_remaining = %d, con->file = %d",
779 con
->http
.data_encoding
== HTTP_ENCODE_CHUNKED
? "chunked" : "length",
780 con
->http
.data_remaining
, con
->file
);
782 if (con
->request
!= NULL
)
785 * Grab any request data from the connection...
788 if ((ipp_state
= ippRead(&(con
->http
), con
->request
)) == IPP_ERROR
)
790 LogMessage(L_ERROR
, "ReadClient() %d IPP Read Error!",
795 else if (ipp_state
!= IPP_DATA
)
799 if (con
->file
== 0 && con
->http
.state
!= HTTP_POST_SEND
)
802 * Create a file as needed for the request data...
805 snprintf(con
->filename
, sizeof(con
->filename
), "%s/%08x",
806 RequestRoot
, request_id
++);
807 con
->file
= open(con
->filename
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0640);
808 fchmod(con
->file
, 0640);
809 fchown(con
->file
, User
, Group
);
811 LogMessage(L_DEBUG
, "ReadClient() %d REQUEST %s", con
->http
.fd
,
816 if (!SendError(con
, HTTP_REQUEST_TOO_LARGE
))
824 if (con
->http
.state
!= HTTP_POST_SEND
)
826 if ((bytes
= httpRead(HTTP(con
), line
, sizeof(line
))) < 0)
835 LogMessage(L_DEBUG
, "ReadClient() %d writing %d bytes",
836 con
->http
.fd
, bytes
);
838 if (write(con
->file
, line
, bytes
) < bytes
)
842 unlink(con
->filename
);
844 if (!SendError(con
, HTTP_REQUEST_TOO_LARGE
))
851 else if (con
->http
.state
!= HTTP_POST_SEND
)
858 if (con
->http
.state
== HTTP_POST_SEND
)
862 fstat(con
->file
, &filestats
);
866 if (filestats
.st_size
> MaxRequestSize
&&
870 * Request is too big; remove it and send an error...
873 unlink(con
->filename
);
878 * Delete any IPP request data...
881 ippDelete(con
->request
);
885 if (!SendError(con
, HTTP_REQUEST_TOO_LARGE
))
894 if (!SendCommand(con
, con
->command
, con
->options
))
896 if (!SendError(con
, HTTP_NOT_FOUND
))
903 LogRequest(con
, HTTP_OK
);
908 ProcessIPPRequest(con
);
913 break; /* Anti-compiler-warning-code */
916 if (!con
->http
.keep_alive
&& con
->http
.state
== HTTP_WAITING
)
927 * 'SendCommand()' - Send output from a command via HTTP.
931 SendCommand(client_t
*con
,
938 if (con
->filename
[0])
939 fd
= open(con
->filename
, O_RDONLY
);
941 fd
= open("/dev/null", O_RDONLY
);
943 con
->pipe_pid
= pipe_command(con
, fd
, &(con
->file
), command
, options
);
947 LogMessage(L_DEBUG
, "SendCommand() %d command=\"%s\" file=%d pipe_pid=%d",
948 con
->http
.fd
, command
, con
->file
, con
->pipe_pid
);
950 if (con
->pipe_pid
== 0)
953 fcntl(con
->file
, F_SETFD
, fcntl(con
->file
, F_GETFD
) | FD_CLOEXEC
);
955 DEBUG_printf(("SendCommand: Adding fd %d to InputSet...\n", con
->file
));
956 FD_SET(con
->file
, &InputSet
);
957 FD_SET(con
->http
.fd
, &OutputSet
);
959 if (!SendHeader(con
, HTTP_OK
, NULL
))
962 if (con
->http
.version
== HTTP_1_1
)
964 con
->http
.data_encoding
= HTTP_ENCODE_CHUNKED
;
966 if (httpPrintf(HTTP(con
), "Transfer-Encoding: chunked\r\n") < 0)
978 * 'SendError()' - Send an error message via HTTP.
981 int /* O - 1 if successful, 0 otherwise */
982 SendError(client_t
*con
, /* I - Connection */
983 http_status_t code
) /* I - Error code */
985 char message
[1024]; /* Message for user */
989 * Put the request in the access_log file...
992 if (con
->operation
> HTTP_WAITING
)
993 LogRequest(con
, code
);
996 * To work around bugs in some proxies, don't use Keep-Alive for some
1000 if (code
>= HTTP_BAD_REQUEST
)
1001 con
->http
.keep_alive
= HTTP_KEEPALIVE_OFF
;
1004 * Send an error message back to the client. If the error code is a
1005 * 400 or 500 series, make sure the message contains some text, too!
1008 if (!SendHeader(con
, code
, NULL
))
1011 if (con
->http
.version
>= HTTP_1_1
&& !con
->http
.keep_alive
)
1013 if (httpPrintf(HTTP(con
), "Connection: close\r\n") < 0)
1017 if (code
>= HTTP_BAD_REQUEST
)
1020 * Send a human-readable error message.
1023 snprintf(message
, sizeof(message
),
1024 "<HTML><HEAD><TITLE>%d %s</TITLE></HEAD>"
1025 "<BODY><H1>%s</H1>%s</BODY></HTML>\n",
1026 code
, httpStatus(code
), httpStatus(code
),
1027 con
->language
? con
->language
->messages
[code
] :
1030 if (httpPrintf(HTTP(con
), "Content-Type: text/html\r\n") < 0)
1032 if (httpPrintf(HTTP(con
), "Content-Length: %d\r\n", strlen(message
)) < 0)
1034 if (httpPrintf(HTTP(con
), "\r\n") < 0)
1036 if (httpPrintf(HTTP(con
), "%s", message
) < 0)
1039 else if (httpPrintf(HTTP(con
), "\r\n") < 0)
1042 con
->http
.state
= HTTP_WAITING
;
1049 * 'SendFile()' - Send a file via HTTP.
1053 SendFile(client_t
*con
,
1057 struct stat
*filestats
)
1059 con
->file
= open(filename
, O_RDONLY
);
1061 LogMessage(L_DEBUG
, "SendFile() %d file=%d", con
->http
.fd
, con
->file
);
1066 fcntl(con
->file
, F_SETFD
, fcntl(con
->file
, F_GETFD
) | FD_CLOEXEC
);
1070 if (!SendHeader(con
, code
, type
))
1073 if (httpPrintf(HTTP(con
), "Last-Modified: %s\r\n", httpGetDateString(filestats
->st_mtime
)) < 0)
1075 if (httpPrintf(HTTP(con
), "Content-Length: %d\r\n", filestats
->st_size
) < 0)
1077 if (httpPrintf(HTTP(con
), "\r\n") < 0)
1080 FD_SET(con
->http
.fd
, &OutputSet
);
1087 * 'SendHeader()' - Send an HTTP request.
1090 int /* O - 1 on success, 0 on failure */
1091 SendHeader(client_t
*con
, /* I - Client to send to */
1092 http_status_t code
, /* I - HTTP status code */
1093 char *type
) /* I - MIME type of document */
1095 location_t
*loc
; /* Authentication location */
1098 if (httpPrintf(HTTP(con
), "HTTP/%d.%d %d %s\r\n", con
->http
.version
/ 100,
1099 con
->http
.version
% 100, code
, httpStatus(code
)) < 0)
1101 if (httpPrintf(HTTP(con
), "Date: %s\r\n", httpGetDateString(time(NULL
))) < 0)
1103 if (httpPrintf(HTTP(con
), "Server: CUPS/1.1\r\n") < 0)
1105 if (con
->http
.keep_alive
&& con
->http
.version
>= HTTP_1_0
)
1107 if (httpPrintf(HTTP(con
), "Connection: Keep-Alive\r\n") < 0)
1109 if (httpPrintf(HTTP(con
), "Keep-Alive: timeout=%d\r\n", KeepAliveTimeout
) < 0)
1112 if (code
== HTTP_UNAUTHORIZED
)
1114 loc
= FindBest(con
); /* This already succeeded in IsAuthorized */
1116 if (loc
->type
== AUTH_BASIC
)
1118 if (httpPrintf(HTTP(con
), "WWW-Authenticate: Basic realm=\"CUPS\"\r\n") < 0)
1123 if (httpPrintf(HTTP(con
), "WWW-Authenticate: Digest realm=\"CUPS\" "
1124 "nonce=\"%s\"\r\n", con
->http
.hostname
) < 0)
1128 if (con
->language
!= NULL
)
1130 if (httpPrintf(HTTP(con
), "Content-Language: %s\r\n",
1131 con
->language
->language
) < 0)
1135 if (httpPrintf(HTTP(con
), "Content-Type: %s; charset=%s\r\n", type
,
1136 cupsLangEncoding(con
->language
)) < 0)
1139 else if (type
!= NULL
)
1140 if (httpPrintf(HTTP(con
), "Content-Type: %s\r\n", type
) < 0)
1148 * 'WriteClient()' - Write data to a client as needed.
1151 int /* O - 1 if success, 0 if fail */
1152 WriteClient(client_t
*con
) /* I - Client connection */
1154 int bytes
; /* Number of bytes written */
1155 char buf
[HTTP_MAX_BUFFER
+ 1];/* Data buffer */
1156 char *bufptr
; /* Pointer into buffer */
1157 ipp_state_t ipp_state
; /* IPP state value */
1160 if (con
->http
.state
!= HTTP_GET_SEND
&&
1161 con
->http
.state
!= HTTP_POST_SEND
)
1164 if (con
->response
!= NULL
)
1166 ipp_state
= ippWrite(&(con
->http
), con
->response
);
1167 bytes
= ipp_state
!= IPP_ERROR
&& ipp_state
!= IPP_DATA
;
1169 else if ((bytes
= read(con
->file
, buf
, HTTP_MAX_BUFFER
)) > 0)
1171 if (con
->pipe_pid
&& !con
->got_fields
)
1174 * Inspect the data for Content-Type and other fields.
1179 for (bufptr
= buf
; !con
->got_fields
&& *bufptr
; bufptr
++)
1180 if (*bufptr
== '\n')
1183 * Send line to client...
1186 if (bufptr
> buf
&& bufptr
[-1] == '\r')
1190 httpPrintf(HTTP(con
), "%s\r\n", buf
);
1191 LogMessage(L_DEBUG
, "WriteClient() %d %s", con
->http
.fd
, buf
);
1197 bytes
-= (bufptr
- buf
);
1198 memcpy(buf
, bufptr
, bytes
+ 1);
1202 * See if the line was empty...
1205 if (con
->field_col
== 0)
1206 con
->got_fields
= 1;
1210 else if (*bufptr
!= '\r')
1213 if (bytes
> 0 && !con
->got_fields
)
1216 * Remaining text needs to go out...
1219 httpPrintf(HTTP(con
), "%s", buf
);
1221 con
->http
.activity
= time(NULL
);
1224 else if (bytes
== 0)
1226 con
->http
.activity
= time(NULL
);
1231 if (httpWrite(HTTP(con
), buf
, bytes
) < 0)
1237 con
->bytes
+= bytes
;
1242 LogRequest(con
, HTTP_OK
);
1244 if (con
->http
.data_encoding
== HTTP_ENCODE_CHUNKED
)
1246 if (httpPrintf(HTTP(con
), "0\r\n\r\n") < 0)
1253 con
->http
.state
= HTTP_WAITING
;
1255 FD_CLR(con
->http
.fd
, &OutputSet
);
1259 DEBUG_printf(("WriteClient: Removing fd %d from InputSet...\n", con
->file
));
1260 FD_CLR(con
->file
, &InputSet
);
1263 kill(con
->pipe_pid
, SIGTERM
);
1270 if (con
->filename
[0])
1271 unlink(con
->filename
);
1273 if (con
->request
!= NULL
)
1275 ippDelete(con
->request
);
1276 con
->request
= NULL
;
1279 if (con
->response
!= NULL
)
1281 ippDelete(con
->response
);
1282 con
->response
= NULL
;
1285 if (!con
->http
.keep_alive
)
1293 LogMessage(L_DEBUG
, "WriteClient() %d %d bytes", con
->http
.fd
, bytes
);
1295 con
->http
.activity
= time(NULL
);
1302 * 'check_if_modified()' - Decode an "If-Modified-Since" line.
1305 static int /* O - 1 if modified since */
1306 check_if_modified(client_t
*con
, /* I - Client connection */
1307 struct stat
*filestats
) /* I - File information */
1309 char *ptr
; /* Pointer into field */
1310 time_t date
; /* Time/date value */
1311 int size
; /* Size/length value */
1316 ptr
= con
->http
.fields
[HTTP_FIELD_IF_MODIFIED_SINCE
];
1321 LogMessage(L_DEBUG
, "check_if_modified() %d If-Modified-Since=\"%s\"",
1324 while (*ptr
!= '\0')
1326 while (isspace(*ptr
) || *ptr
== ';')
1329 if (strncasecmp(ptr
, "length=", 7) == 0)
1334 while (isdigit(*ptr
))
1337 else if (isalpha(*ptr
))
1339 date
= httpGetDateTime(ptr
);
1340 while (*ptr
!= '\0' && *ptr
!= ';')
1345 LogMessage(L_DEBUG
, "check_if_modified() %d sizes=%d,%d dates=%d,%d",
1346 con
->http
.fd
, size
, filestats
->st_size
, date
, filestats
->st_mtime
);
1348 return ((size
!= filestats
->st_size
&& size
!= 0) ||
1349 (date
< filestats
->st_mtime
&& date
!= 0) ||
1350 (size
== 0 && date
== 0));
1355 * 'decode_auth()' - Decode an authorization string.
1359 decode_auth(client_t
*con
) /* I - Client to decode to */
1361 char *s
, /* Authorization string */
1362 value
[1024]; /* Value string */
1363 const char *username
; /* Certificate username */
1367 * Decode the string...
1370 s
= con
->http
.fields
[HTTP_FIELD_AUTHORIZATION
];
1372 DEBUG_printf(("decode_auth(%08x): Authorization string = \"%s\"\n",
1375 if (strncmp(s
, "Basic", 5) == 0)
1381 httpDecode64(value
, s
);
1384 * Pull the username and password out...
1387 if ((s
= strchr(value
, ':')) == NULL
)
1389 LogMessage(L_DEBUG
, "decode_auth() %d no colon in auth string \"%s\"",
1390 con
->http
.fd
, value
);
1396 strncpy(con
->username
, value
, sizeof(con
->username
) - 1);
1397 con
->username
[sizeof(con
->username
) - 1] = '\0';
1399 strncpy(con
->password
, s
, sizeof(con
->password
) - 1);
1400 con
->password
[sizeof(con
->password
) - 1] = '\0';
1402 else if (strncmp(s
, "Local", 5) == 0)
1408 if ((username
= FindCert(s
)) != NULL
)
1409 strcpy(con
->username
, username
);
1411 else if (strncmp(s
, "Digest", 5) == 0)
1414 * Get the username and password from the Digest attributes...
1417 if (httpGetSubField(&(con
->http
), HTTP_FIELD_WWW_AUTHENTICATE
, "username",
1420 strncpy(con
->username
, value
, sizeof(con
->username
) - 1);
1421 con
->username
[sizeof(con
->username
) - 1] = '\0';
1424 if (httpGetSubField(&(con
->http
), HTTP_FIELD_WWW_AUTHENTICATE
, "response",
1427 strncpy(con
->password
, value
, sizeof(con
->password
) - 1);
1428 con
->password
[sizeof(con
->password
) - 1] = '\0';
1432 LogMessage(L_DEBUG
, "decode_auth() %d username=\"%s\"",
1433 con
->http
.fd
, con
->username
);
1434 DEBUG_printf(("decode_auth() %d username=\"%s\" password=\"%s\"\n",
1435 con
->http
.fd
, con
->username
, con
->password
));
1440 * 'get_file()' - Get a filename and state info.
1443 static char * /* O - Real filename */
1444 get_file(client_t
*con
, /* I - Client connection */
1445 struct stat
*filestats
)/* O - File information */
1447 int status
; /* Status of filesystem calls */
1448 char *params
; /* Pointer to parameters in URI */
1449 static char filename
[1024]; /* Filename buffer */
1453 * Need to add DocumentRoot global...
1456 if (strncmp(con
->uri
, "/ppd/", 5) == 0)
1457 snprintf(filename
, sizeof(filename
), "%s%s", ServerRoot
, con
->uri
);
1458 else if (con
->language
!= NULL
)
1459 snprintf(filename
, sizeof(filename
), "%s/%s%s", DocumentRoot
, con
->language
->language
,
1462 snprintf(filename
, sizeof(filename
), "%s%s", DocumentRoot
, con
->uri
);
1464 if ((params
= strchr(filename
, '?')) != NULL
)
1468 * Grab the status for this language; if there isn't a language-specific file
1469 * then fallback to the default one...
1472 if ((status
= stat(filename
, filestats
)) != 0 && con
->language
!= NULL
)
1475 * Drop the language prefix and try the current directory...
1478 if (strncmp(con
->uri
, "/ppd/", 5) != 0)
1480 snprintf(filename
, sizeof(filename
), "%s%s", DocumentRoot
, con
->uri
);
1482 status
= stat(filename
, filestats
);
1487 * If we're found a directory, get the index.html file instead...
1490 if (!status
&& S_ISDIR(filestats
->st_mode
))
1492 if (filename
[strlen(filename
) - 1] == '/')
1493 strcat(filename
, "index.html");
1495 strcat(filename
, "/index.html");
1497 status
= stat(filename
, filestats
);
1500 LogMessage(L_DEBUG
, "get_file() %d filename=%s size=%d",
1501 con
->http
.fd
, filename
, status
? -1 : filestats
->st_size
);
1511 * 'pipe_command()' - Pipe the output of a command to the remote client.
1514 static int /* O - Process ID */
1515 pipe_command(client_t
*con
, /* I - Client connection */
1516 int infile
, /* I - Standard input for command */
1517 int *outfile
, /* O - Standard output for command */
1518 char *command
, /* I - Command to run */
1519 char *options
) /* I - Options for command */
1521 int pid
; /* Process ID */
1522 char *commptr
; /* Command string pointer */
1523 int fds
[2]; /* Pipe FDs */
1524 int argc
; /* Number of arguments */
1525 char argbuf
[10240], /* Argument buffer */
1526 *argv
[100], /* Argument strings */
1527 *envp
[100]; /* Environment variables */
1528 char hostname
[1024]; /* Hostname string */
1529 static char lang
[1024]; /* LANG env variable */
1530 static char content_length
[1024]; /* CONTENT_LENGTH env variable */
1531 static char content_type
[1024]; /* CONTENT_TYPE env variable */
1532 static char ipp_port
[1024]; /* Default listen port */
1533 static char server_port
[1024]; /* Default listen port */
1534 static char server_name
[1024]; /* Default listen hostname */
1535 static char remote_host
[1024]; /* REMOTE_HOST env variable */
1536 static char remote_user
[1024]; /* REMOTE_HOST env variable */
1537 static char tmpdir
[1024]; /* TMPDIR env variable */
1538 static char query_string
[10240]; /* QUERY_STRING env variable */
1542 * Copy the command string...
1545 strncpy(argbuf
, options
, sizeof(argbuf
) - 1);
1546 argbuf
[sizeof(argbuf
) - 1] = '\0';
1549 * Parse the string; arguments can be separated by + and are terminated
1555 for (commptr
= argbuf
, argc
= 1; *commptr
!= '\0' && argc
< 99; commptr
++)
1556 if (*commptr
== ' ' || *commptr
== '+')
1560 while (*commptr
== ' ')
1563 if (*commptr
!= '\0')
1565 argv
[argc
] = commptr
;
1571 else if (*commptr
== '%')
1573 if (commptr
[1] >= '0' && commptr
[1] <= '9')
1574 *commptr
= (commptr
[1] - '0') << 4;
1576 *commptr
= (tolower(commptr
[1]) - 'a' + 10) << 4;
1578 if (commptr
[2] >= '0' && commptr
[2] <= '9')
1579 *commptr
|= commptr
[2] - '0';
1581 *commptr
|= tolower(commptr
[2]) - 'a' + 10;
1583 strcpy(commptr
+ 1, commptr
+ 3);
1585 else if (*commptr
== '?')
1590 if (argv
[0][0] == '\0')
1591 argv
[0] = strrchr(command
, '/') + 1;
1594 * Setup the environment variables as needed...
1597 gethostname(hostname
, sizeof(hostname
) - 1);
1599 sprintf(lang
, "LANG=%s", con
->language
? con
->language
->language
: "C");
1600 sprintf(ipp_port
, "IPP_PORT=%d", ntohs(con
->http
.hostaddr
.sin_port
));
1601 sprintf(server_port
, "SERVER_PORT=%d", ntohs(con
->http
.hostaddr
.sin_port
));
1602 sprintf(server_name
, "SERVER_NAME=%s", hostname
);
1603 sprintf(remote_host
, "REMOTE_HOST=%s", con
->http
.hostname
);
1604 sprintf(remote_user
, "REMOTE_USER=%s", con
->username
);
1605 sprintf(tmpdir
, "TMPDIR=%s", TempDir
);
1607 envp
[0] = "PATH=/bin:/usr/bin";
1608 envp
[1] = "SERVER_SOFTWARE=CUPS/1.1";
1609 envp
[2] = "GATEWAY_INTERFACE=CGI/1.1";
1610 envp
[3] = "SERVER_PROTOCOL=HTTP/1.1";
1612 envp
[5] = server_name
;
1613 envp
[6] = server_port
;
1614 envp
[7] = remote_host
;
1615 envp
[8] = remote_user
;
1620 if (con
->operation
== HTTP_GET
)
1622 envp
[12] = "REQUEST_METHOD=GET";
1627 * Add GET form variables after ?...
1632 snprintf(query_string
, sizeof(query_string
), "QUERY_STRING=%s", commptr
);
1633 envp
[13] = query_string
;
1641 sprintf(content_length
, "CONTENT_LENGTH=%d", con
->bytes
);
1642 snprintf(content_type
, sizeof(content_type
), "CONTENT_TYPE=%s",
1643 con
->http
.fields
[HTTP_FIELD_CONTENT_TYPE
]);
1645 envp
[12] = "REQUEST_METHOD=POST";
1646 envp
[13] = content_length
;
1647 envp
[14] = content_type
;
1652 * Create a pipe for the output...
1657 LogMessage(L_ERROR
, "Unable to create pipes for CGI %s - %s",
1658 argv
[0], strerror(errno
));
1663 * Then execute the command...
1666 if ((pid
= fork()) == 0)
1669 * Child comes here... Close stdin if necessary and dup the pipe to stdout.
1688 * Execute the pipe program; if an error occurs, exit with status 1...
1691 execve(command
, argv
, envp
);
1692 perror("execve failed");
1699 * Error - can't fork!
1702 LogMessage(L_ERROR
, "Unable to fork for CGI %s - %s", argv
[0],
1712 * Fork successful - return the PID...
1715 AddCert(pid
, con
->username
);
1717 LogMessage(L_DEBUG
, "CGI %s started - PID = %d", argv
[0], pid
);
1728 * End of "$Id: client.c,v 1.56 2000/05/09 14:10:15 mike Exp $".