2 * "$Id: client.c,v 1.54 2000/03/21 04:03:33 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 (con
->http
.fields
[HTTP_FIELD_HOST
][0] == '\0' &&
407 con
->http
.version
>= HTTP_1_1
)
410 * HTTP/1.1 and higher require the "Host:" field...
413 if (!SendError(con
, HTTP_BAD_REQUEST
))
419 else if (strstr(con
->uri
, "..") != NULL
)
422 * Protect against malicious users!
425 if (!SendError(con
, HTTP_FORBIDDEN
))
431 else if (con
->uri
[0] != '/')
434 * Don't allow proxying (yet)...
437 if (!SendError(con
, HTTP_METHOD_NOT_ALLOWED
))
443 else if ((status
= IsAuthorized(con
)) != HTTP_OK
)
445 SendError(con
, status
);
449 else switch (con
->http
.state
)
452 if (strncmp(con
->uri
, "/printers/", 10) == 0 &&
453 strcmp(con
->uri
+ strlen(con
->uri
) - 4, ".ppd") == 0)
456 * Send PPD file - get the real printer name since printer
457 * names are not case sensitive but filename can be...
460 con
->uri
[strlen(con
->uri
) - 4] = '\0'; /* Drop ".ppd" */
462 if ((p
= FindPrinter(con
->uri
+ 10)) != NULL
)
463 sprintf(con
->uri
, "/ppd/%s.ppd", p
->name
);
466 if (!SendError(con
, HTTP_NOT_FOUND
))
476 if (strncmp(con
->uri
, "/admin", 6) == 0 ||
477 strncmp(con
->uri
, "/printers", 9) == 0 ||
478 strncmp(con
->uri
, "/classes", 8) == 0 ||
479 strncmp(con
->uri
, "/jobs", 5) == 0)
485 if (strncmp(con
->uri
, "/admin", 6) == 0)
487 snprintf(con
->command
, sizeof(con
->command
),
488 "%s/cgi-bin/admin.cgi", ServerBin
);
489 con
->options
= con
->uri
+ 6;
491 else if (strncmp(con
->uri
, "/printers", 9) == 0)
493 snprintf(con
->command
, sizeof(con
->command
),
494 "%s/cgi-bin/printers.cgi", ServerBin
);
495 con
->options
= con
->uri
+ 9;
497 else if (strncmp(con
->uri
, "/classes", 8) == 0)
499 snprintf(con
->command
, sizeof(con
->command
),
500 "%s/cgi-bin/classes.cgi", ServerBin
);
501 con
->options
= con
->uri
+ 8;
505 snprintf(con
->command
, sizeof(con
->command
),
506 "%s/cgi-bin/jobs.cgi", ServerBin
);
507 con
->options
= con
->uri
+ 5;
510 if (con
->options
[0] == '/')
513 if (!SendCommand(con
, con
->command
, con
->options
))
515 if (!SendError(con
, HTTP_NOT_FOUND
))
522 LogRequest(con
, HTTP_OK
);
524 if (con
->http
.version
<= HTTP_1_0
)
525 con
->http
.keep_alive
= HTTP_KEEPALIVE_OFF
;
533 if ((filename
= get_file(con
, &filestats
)) == NULL
)
535 if (!SendError(con
, HTTP_NOT_FOUND
))
541 else if (!check_if_modified(con
, &filestats
))
543 if (!SendError(con
, HTTP_NOT_MODIFIED
))
551 type
= mimeFileType(MimeDatabase
, filename
);
553 strcpy(line
, "text/plain");
555 sprintf(line
, "%s/%s", type
->super
, type
->type
);
557 if (!SendFile(con
, HTTP_OK
, filename
, line
, &filestats
))
566 case HTTP_POST_RECV
:
568 * See if the POST request includes a Content-Length field, and if
569 * so check the length against any limits that are set...
572 LogMessage(L_DEBUG
, "POST %s", con
->uri
);
573 LogMessage(L_DEBUG
, "CONTENT_TYPE = %s", con
->http
.fields
[HTTP_FIELD_CONTENT_TYPE
]);
575 if (con
->http
.fields
[HTTP_FIELD_CONTENT_LENGTH
][0] &&
576 atoi(con
->http
.fields
[HTTP_FIELD_CONTENT_LENGTH
]) > MaxRequestSize
&&
580 * Request too large...
583 if (!SendError(con
, HTTP_REQUEST_TOO_LARGE
))
593 * See what kind of POST request this is; for IPP requests the
594 * content-type field will be "application/ipp"...
597 if (strcmp(con
->http
.fields
[HTTP_FIELD_CONTENT_TYPE
], "application/ipp") == 0)
598 con
->request
= ippNew();
599 else if (strncmp(con
->uri
, "/admin", 6) == 0 ||
600 strncmp(con
->uri
, "/printers", 9) == 0 ||
601 strncmp(con
->uri
, "/classes", 8) == 0 ||
602 strncmp(con
->uri
, "/jobs", 5) == 0)
608 if (strncmp(con
->uri
, "/admin", 6) == 0)
610 snprintf(con
->command
, sizeof(con
->command
),
611 "%s/cgi-bin/admin.cgi", ServerBin
);
612 con
->options
= con
->uri
+ 6;
614 else if (strncmp(con
->uri
, "/printers", 9) == 0)
616 snprintf(con
->command
, sizeof(con
->command
),
617 "%s/cgi-bin/printers.cgi", ServerBin
);
618 con
->options
= con
->uri
+ 9;
620 else if (strncmp(con
->uri
, "/classes", 8) == 0)
622 snprintf(con
->command
, sizeof(con
->command
),
623 "%s/cgi-bin/classes.cgi", ServerBin
);
624 con
->options
= con
->uri
+ 8;
628 snprintf(con
->command
, sizeof(con
->command
),
629 "%s/cgi-bin/jobs.cgi", ServerBin
);
630 con
->options
= con
->uri
+ 5;
633 if (con
->options
[0] == '/')
636 LogMessage(L_DEBUG
, "ReadClient() %d command=\"%s\", options = \"%s\"",
637 con
->http
.fd
, con
->command
, con
->options
);
639 if (con
->http
.version
<= HTTP_1_0
)
640 con
->http
.keep_alive
= HTTP_KEEPALIVE_OFF
;
642 else if (!SendError(con
, HTTP_UNAUTHORIZED
))
652 SendError(con
, HTTP_NOT_IMPLEMENTED
);
659 if (strncmp(con
->uri
, "/printers", 9) == 0 &&
660 strcmp(con
->uri
+ strlen(con
->uri
) - 4, ".ppd") == 0)
666 snprintf(con
->command
, sizeof(con
->command
),
667 "/ppd/%s", con
->uri
+ 10);
668 strcpy(con
->uri
, con
->command
);
669 con
->command
[0] = '\0';
672 if (strncmp(con
->uri
, "/admin/", 7) == 0 ||
673 strncmp(con
->uri
, "/printers/", 10) == 0 ||
674 strncmp(con
->uri
, "/classes/", 9) == 0 ||
675 strncmp(con
->uri
, "/jobs/", 6) == 0)
681 if (!SendHeader(con
, HTTP_OK
, "text/html"))
687 if (httpPrintf(HTTP(con
), "\r\n") < 0)
693 LogRequest(con
, HTTP_OK
);
695 else if ((filename
= get_file(con
, &filestats
)) == NULL
)
697 if (!SendHeader(con
, HTTP_NOT_FOUND
, "text/html"))
703 LogRequest(con
, HTTP_NOT_FOUND
);
705 else if (!check_if_modified(con
, &filestats
))
707 if (!SendError(con
, HTTP_NOT_MODIFIED
))
713 LogRequest(con
, HTTP_NOT_MODIFIED
);
721 type
= mimeFileType(MimeDatabase
, filename
);
723 strcpy(line
, "text/plain");
725 sprintf(line
, "%s/%s", type
->super
, type
->type
);
727 if (!SendHeader(con
, HTTP_OK
, line
))
733 if (httpPrintf(HTTP(con
), "Last-Modified: %s\r\n",
734 httpGetDateString(filestats
.st_mtime
)) < 0)
740 if (httpPrintf(HTTP(con
), "Content-Length: %d\r\n",
741 filestats
.st_size
) < 0)
747 LogRequest(con
, HTTP_OK
);
750 if (httpPrintf(HTTP(con
), "\r\n") < 0)
756 con
->http
.state
= HTTP_WAITING
;
760 break; /* Anti-compiler-warning-code */
765 * Handle any incoming data...
768 switch (con
->http
.state
)
773 case HTTP_POST_RECV
:
774 LogMessage(L_DEBUG
, "ReadClient() %d con->data_encoding = %s, con->data_remaining = %d, con->file = %d",
776 con
->http
.data_encoding
== HTTP_ENCODE_CHUNKED
? "chunked" : "length",
777 con
->http
.data_remaining
, con
->file
);
779 if (con
->request
!= NULL
)
782 * Grab any request data from the connection...
785 if ((ipp_state
= ippRead(&(con
->http
), con
->request
)) == IPP_ERROR
)
787 LogMessage(L_ERROR
, "ReadClient() %d IPP Read Error!",
792 else if (ipp_state
!= IPP_DATA
)
796 if (con
->file
== 0 && con
->http
.state
!= HTTP_POST_SEND
)
799 * Create a file as needed for the request data...
802 snprintf(con
->filename
, sizeof(con
->filename
), "%s/%08x",
803 RequestRoot
, request_id
++);
804 con
->file
= open(con
->filename
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0640);
805 fchmod(con
->file
, 0640);
806 fchown(con
->file
, User
, Group
);
808 LogMessage(L_DEBUG
, "ReadClient() %d REQUEST %s", con
->http
.fd
,
813 if (!SendError(con
, HTTP_REQUEST_TOO_LARGE
))
821 if (con
->http
.state
!= HTTP_POST_SEND
)
823 if ((bytes
= httpRead(HTTP(con
), line
, sizeof(line
))) < 0)
832 LogMessage(L_DEBUG
, "ReadClient() %d writing %d bytes",
833 con
->http
.fd
, bytes
);
835 if (write(con
->file
, line
, bytes
) < bytes
)
839 unlink(con
->filename
);
841 if (!SendError(con
, HTTP_REQUEST_TOO_LARGE
))
848 else if (con
->http
.state
!= HTTP_POST_SEND
)
855 if (con
->http
.state
== HTTP_POST_SEND
)
859 fstat(con
->file
, &filestats
);
863 if (filestats
.st_size
> MaxRequestSize
&&
867 * Request is too big; remove it and send an error...
870 unlink(con
->filename
);
875 * Delete any IPP request data...
878 ippDelete(con
->request
);
882 if (!SendError(con
, HTTP_REQUEST_TOO_LARGE
))
891 if (!SendCommand(con
, con
->command
, con
->options
))
893 if (!SendError(con
, HTTP_NOT_FOUND
))
900 LogRequest(con
, HTTP_OK
);
905 ProcessIPPRequest(con
);
910 break; /* Anti-compiler-warning-code */
913 if (!con
->http
.keep_alive
&& con
->http
.state
== HTTP_WAITING
)
924 * 'SendCommand()' - Send output from a command via HTTP.
928 SendCommand(client_t
*con
,
935 if (con
->filename
[0])
936 fd
= open(con
->filename
, O_RDONLY
);
938 fd
= open("/dev/null", O_RDONLY
);
940 con
->pipe_pid
= pipe_command(con
, fd
, &(con
->file
), command
, options
);
944 LogMessage(L_DEBUG
, "SendCommand() %d command=\"%s\" file=%d pipe_pid=%d",
945 con
->http
.fd
, command
, con
->file
, con
->pipe_pid
);
947 if (con
->pipe_pid
== 0)
950 fcntl(con
->file
, F_SETFD
, fcntl(con
->file
, F_GETFD
) | FD_CLOEXEC
);
952 DEBUG_printf(("SendCommand: Adding fd %d to InputSet...\n", con
->file
));
953 FD_SET(con
->file
, &InputSet
);
954 FD_SET(con
->http
.fd
, &OutputSet
);
956 if (!SendHeader(con
, HTTP_OK
, NULL
))
959 if (con
->http
.version
== HTTP_1_1
)
961 con
->http
.data_encoding
= HTTP_ENCODE_CHUNKED
;
963 if (httpPrintf(HTTP(con
), "Transfer-Encoding: chunked\r\n") < 0)
975 * 'SendError()' - Send an error message via HTTP.
978 int /* O - 1 if successful, 0 otherwise */
979 SendError(client_t
*con
, /* I - Connection */
980 http_status_t code
) /* I - Error code */
982 char message
[1024]; /* Message for user */
986 * Put the request in the access_log file...
989 if (con
->operation
> HTTP_WAITING
)
990 LogRequest(con
, code
);
993 * To work around bugs in some proxies, don't use Keep-Alive for some
997 if (code
>= HTTP_BAD_REQUEST
)
998 con
->http
.keep_alive
= HTTP_KEEPALIVE_OFF
;
1001 * Send an error message back to the client. If the error code is a
1002 * 400 or 500 series, make sure the message contains some text, too!
1005 if (!SendHeader(con
, code
, NULL
))
1008 if (code
== HTTP_UNAUTHORIZED
)
1010 if (httpPrintf(HTTP(con
), "WWW-Authenticate: Basic realm=\"CUPS\"\r\n") < 0)
1014 if (con
->http
.version
>= HTTP_1_1
&& !con
->http
.keep_alive
)
1016 if (httpPrintf(HTTP(con
), "Connection: close\r\n") < 0)
1020 if (code
>= HTTP_BAD_REQUEST
)
1023 * Send a human-readable error message.
1026 snprintf(message
, sizeof(message
),
1027 "<HTML><HEAD><TITLE>%d %s</TITLE></HEAD>"
1028 "<BODY><H1>%s</H1>%s</BODY></HTML>\n",
1029 code
, httpStatus(code
), httpStatus(code
),
1030 con
->language
? con
->language
->messages
[code
] :
1033 if (httpPrintf(HTTP(con
), "Content-Type: text/html\r\n") < 0)
1035 if (httpPrintf(HTTP(con
), "Content-Length: %d\r\n", strlen(message
)) < 0)
1037 if (httpPrintf(HTTP(con
), "\r\n") < 0)
1039 if (httpPrintf(HTTP(con
), "%s", message
) < 0)
1042 else if (httpPrintf(HTTP(con
), "\r\n") < 0)
1045 con
->http
.state
= HTTP_WAITING
;
1052 * 'SendFile()' - Send a file via HTTP.
1056 SendFile(client_t
*con
,
1060 struct stat
*filestats
)
1062 con
->file
= open(filename
, O_RDONLY
);
1064 LogMessage(L_DEBUG
, "SendFile() %d file=%d", con
->http
.fd
, con
->file
);
1069 fcntl(con
->file
, F_SETFD
, fcntl(con
->file
, F_GETFD
) | FD_CLOEXEC
);
1073 if (!SendHeader(con
, code
, type
))
1076 if (httpPrintf(HTTP(con
), "Last-Modified: %s\r\n", httpGetDateString(filestats
->st_mtime
)) < 0)
1078 if (httpPrintf(HTTP(con
), "Content-Length: %d\r\n", filestats
->st_size
) < 0)
1080 if (httpPrintf(HTTP(con
), "\r\n") < 0)
1083 FD_SET(con
->http
.fd
, &OutputSet
);
1090 * 'SendHeader()' - Send an HTTP request.
1093 int /* O - 1 on success, 0 on failure */
1094 SendHeader(client_t
*con
, /* I - Client to send to */
1095 http_status_t code
, /* I - HTTP status code */
1096 char *type
) /* I - MIME type of document */
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 /**** MRS - Need to store which kind of authentication to perform in the
1113 client structure! ****/
1114 if (code
== HTTP_UNAUTHORIZED
)
1115 httpPrintf(HTTP(con
), "WWW-Authenticate: Basic realm=\"CUPS\"\r\n");
1116 if (con
->language
!= NULL
)
1118 if (httpPrintf(HTTP(con
), "Content-Language: %s\r\n",
1119 con
->language
->language
) < 0)
1123 if (httpPrintf(HTTP(con
), "Content-Type: %s; charset=%s\r\n", type
,
1124 cupsLangEncoding(con
->language
)) < 0)
1127 else if (type
!= NULL
)
1128 if (httpPrintf(HTTP(con
), "Content-Type: %s\r\n", type
) < 0)
1136 * 'WriteClient()' - Write data to a client as needed.
1139 int /* O - 1 if success, 0 if fail */
1140 WriteClient(client_t
*con
) /* I - Client connection */
1142 int bytes
; /* Number of bytes written */
1143 char buf
[HTTP_MAX_BUFFER
+ 1];/* Data buffer */
1144 char *bufptr
; /* Pointer into buffer */
1145 ipp_state_t ipp_state
; /* IPP state value */
1148 if (con
->http
.state
!= HTTP_GET_SEND
&&
1149 con
->http
.state
!= HTTP_POST_SEND
)
1152 if (con
->response
!= NULL
)
1154 ipp_state
= ippWrite(&(con
->http
), con
->response
);
1155 bytes
= ipp_state
!= IPP_ERROR
&& ipp_state
!= IPP_DATA
;
1157 else if ((bytes
= read(con
->file
, buf
, HTTP_MAX_BUFFER
)) > 0)
1159 if (con
->pipe_pid
&& !con
->got_fields
)
1162 * Inspect the data for Content-Type and other fields.
1167 for (bufptr
= buf
; !con
->got_fields
&& *bufptr
; bufptr
++)
1168 if (*bufptr
== '\n')
1171 * Send line to client...
1174 if (bufptr
> buf
&& bufptr
[-1] == '\r')
1178 httpPrintf(HTTP(con
), "%s\r\n", buf
);
1179 LogMessage(L_DEBUG
, "WriteClient() %d %s", con
->http
.fd
, buf
);
1185 bytes
-= (bufptr
- buf
);
1186 memcpy(buf
, bufptr
, bytes
+ 1);
1190 * See if the line was empty...
1193 if (con
->field_col
== 0)
1194 con
->got_fields
= 1;
1198 else if (*bufptr
!= '\r')
1201 if (bytes
> 0 && !con
->got_fields
)
1204 * Remaining text needs to go out...
1207 httpPrintf(HTTP(con
), "%s", buf
);
1209 con
->http
.activity
= time(NULL
);
1212 else if (bytes
== 0)
1214 con
->http
.activity
= time(NULL
);
1219 if (httpWrite(HTTP(con
), buf
, bytes
) < 0)
1225 con
->bytes
+= bytes
;
1230 LogRequest(con
, HTTP_OK
);
1232 if (con
->http
.data_encoding
== HTTP_ENCODE_CHUNKED
)
1234 if (httpPrintf(HTTP(con
), "0\r\n\r\n") < 0)
1241 con
->http
.state
= HTTP_WAITING
;
1243 FD_CLR(con
->http
.fd
, &OutputSet
);
1247 DEBUG_printf(("WriteClient: Removing fd %d from InputSet...\n", con
->file
));
1248 FD_CLR(con
->file
, &InputSet
);
1251 kill(con
->pipe_pid
, SIGTERM
);
1258 if (con
->filename
[0])
1259 unlink(con
->filename
);
1261 if (con
->request
!= NULL
)
1263 ippDelete(con
->request
);
1264 con
->request
= NULL
;
1267 if (con
->response
!= NULL
)
1269 ippDelete(con
->response
);
1270 con
->response
= NULL
;
1273 if (!con
->http
.keep_alive
)
1281 LogMessage(L_DEBUG
, "WriteClient() %d %d bytes", con
->http
.fd
, bytes
);
1283 con
->http
.activity
= time(NULL
);
1290 * 'check_if_modified()' - Decode an "If-Modified-Since" line.
1293 static int /* O - 1 if modified since */
1294 check_if_modified(client_t
*con
, /* I - Client connection */
1295 struct stat
*filestats
) /* I - File information */
1297 char *ptr
; /* Pointer into field */
1298 time_t date
; /* Time/date value */
1299 int size
; /* Size/length value */
1304 ptr
= con
->http
.fields
[HTTP_FIELD_IF_MODIFIED_SINCE
];
1309 LogMessage(L_DEBUG
, "check_if_modified() %d If-Modified-Since=\"%s\"",
1312 while (*ptr
!= '\0')
1314 while (isspace(*ptr
) || *ptr
== ';')
1317 if (strncasecmp(ptr
, "length=", 7) == 0)
1322 while (isdigit(*ptr
))
1325 else if (isalpha(*ptr
))
1327 date
= httpGetDateTime(ptr
);
1328 while (*ptr
!= '\0' && *ptr
!= ';')
1333 LogMessage(L_DEBUG
, "check_if_modified() %d sizes=%d,%d dates=%d,%d",
1334 con
->http
.fd
, size
, filestats
->st_size
, date
, filestats
->st_mtime
);
1336 return ((size
!= filestats
->st_size
&& size
!= 0) ||
1337 (date
< filestats
->st_mtime
&& date
!= 0) ||
1338 (size
== 0 && date
== 0));
1343 * 'decode_auth()' - Decode an authorization string.
1347 decode_auth(client_t
*con
) /* I - Client to decode to */
1349 char *s
, /* Authorization string */
1350 value
[1024]; /* Value string */
1351 const char *username
; /* Certificate username */
1355 * Decode the string...
1358 s
= con
->http
.fields
[HTTP_FIELD_AUTHORIZATION
];
1360 DEBUG_printf(("decode_auth(%08x): Authorization string = \"%s\"\n",
1363 if (strncmp(s
, "Basic", 5) == 0)
1369 httpDecode64(value
, s
);
1372 * Pull the username and password out...
1375 if ((s
= strchr(value
, ':')) == NULL
)
1377 LogMessage(L_DEBUG
, "decode_auth() %d no colon in auth string \"%s\"",
1378 con
->http
.fd
, value
);
1384 strncpy(con
->username
, value
, sizeof(con
->username
) - 1);
1385 con
->username
[sizeof(con
->username
) - 1] = '\0';
1387 strncpy(con
->password
, s
, sizeof(con
->password
) - 1);
1388 con
->password
[sizeof(con
->password
) - 1] = '\0';
1390 else if (strncmp(s
, "Local", 5) == 0)
1396 if ((username
= FindCert(s
)) != NULL
)
1397 strcpy(con
->username
, username
);
1400 LogMessage(L_DEBUG
, "decode_auth() %d username=\"%s\"",
1401 con
->http
.fd
, con
->username
);
1402 DEBUG_printf(("decode_auth() %d username=\"%s\" password=\"%s\"\n",
1403 con
->http
.fd
, con
->username
, con
->password
));
1408 * 'get_file()' - Get a filename and state info.
1411 static char * /* O - Real filename */
1412 get_file(client_t
*con
, /* I - Client connection */
1413 struct stat
*filestats
)/* O - File information */
1415 int status
; /* Status of filesystem calls */
1416 char *params
; /* Pointer to parameters in URI */
1417 static char filename
[1024]; /* Filename buffer */
1421 * Need to add DocumentRoot global...
1424 if (strncmp(con
->uri
, "/ppd/", 5) == 0)
1425 snprintf(filename
, sizeof(filename
), "%s%s", ServerRoot
, con
->uri
);
1426 else if (con
->language
!= NULL
)
1427 snprintf(filename
, sizeof(filename
), "%s/%s%s", DocumentRoot
, con
->language
->language
,
1430 snprintf(filename
, sizeof(filename
), "%s%s", DocumentRoot
, con
->uri
);
1432 if ((params
= strchr(filename
, '?')) != NULL
)
1436 * Grab the status for this language; if there isn't a language-specific file
1437 * then fallback to the default one...
1440 if ((status
= stat(filename
, filestats
)) != 0 && con
->language
!= NULL
)
1443 * Drop the language prefix and try the current directory...
1446 if (strncmp(con
->uri
, "/ppd/", 5) != 0)
1448 snprintf(filename
, sizeof(filename
), "%s%s", DocumentRoot
, con
->uri
);
1450 status
= stat(filename
, filestats
);
1455 * If we're found a directory, get the index.html file instead...
1458 if (!status
&& S_ISDIR(filestats
->st_mode
))
1460 if (filename
[strlen(filename
) - 1] == '/')
1461 strcat(filename
, "index.html");
1463 strcat(filename
, "/index.html");
1465 status
= stat(filename
, filestats
);
1468 LogMessage(L_DEBUG
, "get_file() %d filename=%s size=%d",
1469 con
->http
.fd
, filename
, status
? -1 : filestats
->st_size
);
1479 * 'pipe_command()' - Pipe the output of a command to the remote client.
1482 static int /* O - Process ID */
1483 pipe_command(client_t
*con
, /* I - Client connection */
1484 int infile
, /* I - Standard input for command */
1485 int *outfile
, /* O - Standard output for command */
1486 char *command
, /* I - Command to run */
1487 char *options
) /* I - Options for command */
1489 int pid
; /* Process ID */
1490 char *commptr
; /* Command string pointer */
1491 int fds
[2]; /* Pipe FDs */
1492 int argc
; /* Number of arguments */
1493 char argbuf
[10240], /* Argument buffer */
1494 *argv
[100], /* Argument strings */
1495 *envp
[100]; /* Environment variables */
1496 char hostname
[1024]; /* Hostname string */
1497 static char lang
[1024]; /* LANG env variable */
1498 static char content_length
[1024]; /* CONTENT_LENGTH env variable */
1499 static char content_type
[1024]; /* CONTENT_TYPE env variable */
1500 static char ipp_port
[1024]; /* Default listen port */
1501 static char server_port
[1024]; /* Default listen port */
1502 static char server_name
[1024]; /* Default listen hostname */
1503 static char remote_host
[1024]; /* REMOTE_HOST env variable */
1504 static char remote_user
[1024]; /* REMOTE_HOST env variable */
1505 static char tmpdir
[1024]; /* TMPDIR env variable */
1506 static char query_string
[10240]; /* QUERY_STRING env variable */
1510 * Copy the command string...
1513 strncpy(argbuf
, options
, sizeof(argbuf
) - 1);
1514 argbuf
[sizeof(argbuf
) - 1] = '\0';
1517 * Parse the string; arguments can be separated by + and are terminated
1523 for (commptr
= argbuf
, argc
= 1; *commptr
!= '\0' && argc
< 99; commptr
++)
1524 if (*commptr
== ' ' || *commptr
== '+')
1528 while (*commptr
== ' ')
1531 if (*commptr
!= '\0')
1533 argv
[argc
] = commptr
;
1539 else if (*commptr
== '%')
1541 if (commptr
[1] >= '0' && commptr
[1] <= '9')
1542 *commptr
= (commptr
[1] - '0') << 4;
1544 *commptr
= (tolower(commptr
[1]) - 'a' + 10) << 4;
1546 if (commptr
[2] >= '0' && commptr
[2] <= '9')
1547 *commptr
|= commptr
[2] - '0';
1549 *commptr
|= tolower(commptr
[2]) - 'a' + 10;
1551 strcpy(commptr
+ 1, commptr
+ 3);
1553 else if (*commptr
== '?')
1558 if (argv
[0][0] == '\0')
1559 argv
[0] = strrchr(command
, '/') + 1;
1562 * Setup the environment variables as needed...
1565 gethostname(hostname
, sizeof(hostname
) - 1);
1567 sprintf(lang
, "LANG=%s", con
->language
? con
->language
->language
: "C");
1568 sprintf(ipp_port
, "IPP_PORT=%d", ntohs(con
->http
.hostaddr
.sin_port
));
1569 sprintf(server_port
, "SERVER_PORT=%d", ntohs(con
->http
.hostaddr
.sin_port
));
1570 sprintf(server_name
, "SERVER_NAME=%s", hostname
);
1571 sprintf(remote_host
, "REMOTE_HOST=%s", con
->http
.hostname
);
1572 sprintf(remote_user
, "REMOTE_USER=%s", con
->username
);
1573 sprintf(tmpdir
, "TMPDIR=%s", TempDir
);
1575 envp
[0] = "PATH=/bin:/usr/bin";
1576 envp
[1] = "SERVER_SOFTWARE=CUPS/1.1";
1577 envp
[2] = "GATEWAY_INTERFACE=CGI/1.1";
1578 envp
[3] = "SERVER_PROTOCOL=HTTP/1.1";
1580 envp
[5] = server_name
;
1581 envp
[6] = server_port
;
1582 envp
[7] = remote_host
;
1583 envp
[8] = remote_user
;
1588 if (con
->operation
== HTTP_GET
)
1590 envp
[12] = "REQUEST_METHOD=GET";
1595 * Add GET form variables after ?...
1600 snprintf(query_string
, sizeof(query_string
), "QUERY_STRING=%s", commptr
);
1601 envp
[13] = query_string
;
1609 sprintf(content_length
, "CONTENT_LENGTH=%d", con
->bytes
);
1610 snprintf(content_type
, sizeof(content_type
), "CONTENT_TYPE=%s",
1611 con
->http
.fields
[HTTP_FIELD_CONTENT_TYPE
]);
1613 envp
[12] = "REQUEST_METHOD=POST";
1614 envp
[13] = content_length
;
1615 envp
[14] = content_type
;
1620 * Create a pipe for the output...
1625 LogMessage(L_ERROR
, "Unable to create pipes for CGI %s - %s",
1626 argv
[0], strerror(errno
));
1631 * Then execute the command...
1634 if ((pid
= fork()) == 0)
1637 * Child comes here... Close stdin if necessary and dup the pipe to stdout.
1656 * Execute the pipe program; if an error occurs, exit with status 1...
1659 execve(command
, argv
, envp
);
1660 perror("execve failed");
1667 * Error - can't fork!
1670 LogMessage(L_ERROR
, "Unable to fork for CGI %s - %s", argv
[0],
1680 * Fork successful - return the PID...
1683 AddCert(pid
, con
->username
);
1685 LogMessage(L_DEBUG
, "CGI %s started - PID = %d", argv
[0], pid
);
1696 * End of "$Id: client.c,v 1.54 2000/03/21 04:03:33 mike Exp $".