2 * "$Id: client.c,v 1.64 2000/07/17 12:31:59 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 location_t
*best
; /* Best match for authentication */
246 static unsigned request_id
= 0;/* Request ID for temp files */
249 status
= HTTP_CONTINUE
;
251 switch (con
->http
.state
)
255 * See if we've received a request line...
258 if (httpGets(line
, sizeof(line
) - 1, HTTP(con
)) == NULL
)
265 * Ignore blank request lines...
272 * Clear other state variables...
275 httpClearFields(HTTP(con
));
277 con
->http
.activity
= time(NULL
);
278 con
->http
.version
= HTTP_1_0
;
279 con
->http
.keep_alive
= HTTP_KEEPALIVE_OFF
;
280 con
->http
.data_encoding
= HTTP_ENCODE_LENGTH
;
281 con
->http
.data_remaining
= 0;
282 con
->operation
= HTTP_WAITING
;
286 con
->command
[0] = '\0';
287 con
->username
[0] = '\0';
288 con
->password
[0] = '\0';
291 if (con
->language
!= NULL
)
293 cupsLangFree(con
->language
);
294 con
->language
= NULL
;
298 * Grab the request line...
301 switch (sscanf(line
, "%63s%1023s%63s", operation
, con
->uri
, version
))
304 SendError(con
, HTTP_BAD_REQUEST
);
308 con
->http
.version
= HTTP_0_9
;
311 if (sscanf(version
, "HTTP/%d.%d", &major
, &minor
) != 2)
313 SendError(con
, HTTP_BAD_REQUEST
);
320 con
->http
.version
= (http_version_t
)(major
* 100 + minor
);
321 if (con
->http
.version
== HTTP_1_1
)
322 con
->http
.keep_alive
= HTTP_KEEPALIVE_ON
;
324 con
->http
.keep_alive
= HTTP_KEEPALIVE_OFF
;
328 SendError(con
, HTTP_NOT_SUPPORTED
);
336 * Process the request...
339 if (strcmp(operation
, "GET") == 0)
340 con
->http
.state
= HTTP_GET
;
341 else if (strcmp(operation
, "PUT") == 0)
342 con
->http
.state
= HTTP_PUT
;
343 else if (strcmp(operation
, "POST") == 0)
344 con
->http
.state
= HTTP_POST
;
345 else if (strcmp(operation
, "DELETE") == 0)
346 con
->http
.state
= HTTP_DELETE
;
347 else if (strcmp(operation
, "TRACE") == 0)
348 con
->http
.state
= HTTP_TRACE
;
349 else if (strcmp(operation
, "OPTIONS") == 0)
350 con
->http
.state
= HTTP_OPTIONS
;
351 else if (strcmp(operation
, "HEAD") == 0)
352 con
->http
.state
= HTTP_HEAD
;
355 SendError(con
, HTTP_BAD_REQUEST
);
360 con
->start
= time(NULL
);
361 con
->operation
= con
->http
.state
;
363 LogMessage(L_DEBUG
, "ReadClient() %d %s %s HTTP/%d.%d", con
->http
.fd
,
365 con
->http
.version
/ 100, con
->http
.version
% 100);
367 con
->http
.status
= HTTP_OK
;
377 * Parse incoming parameters until the status changes...
380 status
= httpUpdate(HTTP(con
));
382 if (status
!= HTTP_OK
&& status
!= HTTP_CONTINUE
)
384 SendError(con
, HTTP_BAD_REQUEST
);
391 break; /* Anti-compiler-warning-code */
395 * Handle new transfers...
398 if (status
== HTTP_OK
)
400 con
->language
= cupsLangGet(con
->http
.fields
[HTTP_FIELD_ACCEPT_LANGUAGE
]);
404 if (strncmp(con
->http
.fields
[HTTP_FIELD_CONNECTION
], "Keep-Alive", 10) == 0)
405 con
->http
.keep_alive
= HTTP_KEEPALIVE_ON
;
407 if (con
->http
.fields
[HTTP_FIELD_HOST
][0] == '\0' &&
408 con
->http
.version
>= HTTP_1_1
)
411 * HTTP/1.1 and higher require the "Host:" field...
414 if (!SendError(con
, HTTP_BAD_REQUEST
))
420 else if (con
->operation
== HTTP_OPTIONS
)
423 * Do OPTIONS command...
426 if ((best
= FindBest(con
)) != NULL
&&
427 best
->type
!= AUTH_NONE
)
429 if (!SendHeader(con
, HTTP_UNAUTHORIZED
, NULL
))
437 if (!SendHeader(con
, HTTP_OK
, NULL
))
444 httpPrintf(HTTP(con
), "Allow: GET, HEAD, OPTIONS, POST\r\n");
445 httpPrintf(HTTP(con
), "\r\n");
447 else if (strstr(con
->uri
, "..") != NULL
)
450 * Protect against malicious users!
453 if (!SendError(con
, HTTP_FORBIDDEN
))
459 else if (con
->uri
[0] != '/')
462 * Don't allow proxying (yet)...
465 if (!SendError(con
, HTTP_METHOD_NOT_ALLOWED
))
471 else if ((status
= IsAuthorized(con
)) != HTTP_OK
)
473 SendError(con
, status
);
477 else switch (con
->http
.state
)
480 if (strncmp(con
->uri
, "/printers/", 10) == 0 &&
481 strcmp(con
->uri
+ strlen(con
->uri
) - 4, ".ppd") == 0)
484 * Send PPD file - get the real printer name since printer
485 * names are not case sensitive but filename can be...
488 con
->uri
[strlen(con
->uri
) - 4] = '\0'; /* Drop ".ppd" */
490 if ((p
= FindPrinter(con
->uri
+ 10)) != NULL
)
491 sprintf(con
->uri
, "/ppd/%s.ppd", p
->name
);
494 if (!SendError(con
, HTTP_NOT_FOUND
))
504 if (strncmp(con
->uri
, "/admin", 6) == 0 ||
505 strncmp(con
->uri
, "/printers", 9) == 0 ||
506 strncmp(con
->uri
, "/classes", 8) == 0 ||
507 strncmp(con
->uri
, "/jobs", 5) == 0)
513 if (strncmp(con
->uri
, "/admin", 6) == 0)
515 snprintf(con
->command
, sizeof(con
->command
),
516 "%s/cgi-bin/admin.cgi", ServerBin
);
517 con
->options
= con
->uri
+ 6;
519 else if (strncmp(con
->uri
, "/printers", 9) == 0)
521 snprintf(con
->command
, sizeof(con
->command
),
522 "%s/cgi-bin/printers.cgi", ServerBin
);
523 con
->options
= con
->uri
+ 9;
525 else if (strncmp(con
->uri
, "/classes", 8) == 0)
527 snprintf(con
->command
, sizeof(con
->command
),
528 "%s/cgi-bin/classes.cgi", ServerBin
);
529 con
->options
= con
->uri
+ 8;
533 snprintf(con
->command
, sizeof(con
->command
),
534 "%s/cgi-bin/jobs.cgi", ServerBin
);
535 con
->options
= con
->uri
+ 5;
538 if (con
->options
[0] == '/')
541 if (!SendCommand(con
, con
->command
, con
->options
))
543 if (!SendError(con
, HTTP_NOT_FOUND
))
550 LogRequest(con
, HTTP_OK
);
552 if (con
->http
.version
<= HTTP_1_0
)
553 con
->http
.keep_alive
= HTTP_KEEPALIVE_OFF
;
561 if ((filename
= get_file(con
, &filestats
)) == NULL
)
563 if (!SendError(con
, HTTP_NOT_FOUND
))
569 else if (!check_if_modified(con
, &filestats
))
571 if (!SendError(con
, HTTP_NOT_MODIFIED
))
579 type
= mimeFileType(MimeDatabase
, filename
);
581 strcpy(line
, "text/plain");
583 sprintf(line
, "%s/%s", type
->super
, type
->type
);
585 if (!SendFile(con
, HTTP_OK
, filename
, line
, &filestats
))
594 case HTTP_POST_RECV
:
596 * See if the POST request includes a Content-Length field, and if
597 * so check the length against any limits that are set...
600 LogMessage(L_DEBUG
, "POST %s", con
->uri
);
601 LogMessage(L_DEBUG
, "CONTENT_TYPE = %s", con
->http
.fields
[HTTP_FIELD_CONTENT_TYPE
]);
603 if (con
->http
.fields
[HTTP_FIELD_CONTENT_LENGTH
][0] &&
604 atoi(con
->http
.fields
[HTTP_FIELD_CONTENT_LENGTH
]) > MaxRequestSize
&&
608 * Request too large...
611 if (!SendError(con
, HTTP_REQUEST_TOO_LARGE
))
621 * See what kind of POST request this is; for IPP requests the
622 * content-type field will be "application/ipp"...
625 if (strcmp(con
->http
.fields
[HTTP_FIELD_CONTENT_TYPE
], "application/ipp") == 0)
626 con
->request
= ippNew();
627 else if (strncmp(con
->uri
, "/admin", 6) == 0 ||
628 strncmp(con
->uri
, "/printers", 9) == 0 ||
629 strncmp(con
->uri
, "/classes", 8) == 0 ||
630 strncmp(con
->uri
, "/jobs", 5) == 0)
636 if (strncmp(con
->uri
, "/admin", 6) == 0)
638 snprintf(con
->command
, sizeof(con
->command
),
639 "%s/cgi-bin/admin.cgi", ServerBin
);
640 con
->options
= con
->uri
+ 6;
642 else if (strncmp(con
->uri
, "/printers", 9) == 0)
644 snprintf(con
->command
, sizeof(con
->command
),
645 "%s/cgi-bin/printers.cgi", ServerBin
);
646 con
->options
= con
->uri
+ 9;
648 else if (strncmp(con
->uri
, "/classes", 8) == 0)
650 snprintf(con
->command
, sizeof(con
->command
),
651 "%s/cgi-bin/classes.cgi", ServerBin
);
652 con
->options
= con
->uri
+ 8;
656 snprintf(con
->command
, sizeof(con
->command
),
657 "%s/cgi-bin/jobs.cgi", ServerBin
);
658 con
->options
= con
->uri
+ 5;
661 if (con
->options
[0] == '/')
664 LogMessage(L_DEBUG
, "ReadClient() %d command=\"%s\", options = \"%s\"",
665 con
->http
.fd
, con
->command
, con
->options
);
667 if (con
->http
.version
<= HTTP_1_0
)
668 con
->http
.keep_alive
= HTTP_KEEPALIVE_OFF
;
670 else if (!SendError(con
, HTTP_UNAUTHORIZED
))
680 SendError(con
, HTTP_NOT_IMPLEMENTED
);
685 if (strncmp(con
->uri
, "/printers", 9) == 0 &&
686 strcmp(con
->uri
+ strlen(con
->uri
) - 4, ".ppd") == 0)
692 snprintf(con
->command
, sizeof(con
->command
),
693 "/ppd/%s", con
->uri
+ 10);
694 strcpy(con
->uri
, con
->command
);
695 con
->command
[0] = '\0';
698 if (strncmp(con
->uri
, "/admin/", 7) == 0 ||
699 strncmp(con
->uri
, "/printers/", 10) == 0 ||
700 strncmp(con
->uri
, "/classes/", 9) == 0 ||
701 strncmp(con
->uri
, "/jobs/", 6) == 0)
707 if (!SendHeader(con
, HTTP_OK
, "text/html"))
713 if (httpPrintf(HTTP(con
), "\r\n") < 0)
719 LogRequest(con
, HTTP_OK
);
721 else if ((filename
= get_file(con
, &filestats
)) == NULL
)
723 if (!SendHeader(con
, HTTP_NOT_FOUND
, "text/html"))
729 LogRequest(con
, HTTP_NOT_FOUND
);
731 else if (!check_if_modified(con
, &filestats
))
733 if (!SendError(con
, HTTP_NOT_MODIFIED
))
739 LogRequest(con
, HTTP_NOT_MODIFIED
);
747 type
= mimeFileType(MimeDatabase
, filename
);
749 strcpy(line
, "text/plain");
751 sprintf(line
, "%s/%s", type
->super
, type
->type
);
753 if (!SendHeader(con
, HTTP_OK
, line
))
759 if (httpPrintf(HTTP(con
), "Last-Modified: %s\r\n",
760 httpGetDateString(filestats
.st_mtime
)) < 0)
766 if (httpPrintf(HTTP(con
), "Content-Length: %d\r\n",
767 filestats
.st_size
) < 0)
773 LogRequest(con
, HTTP_OK
);
776 if (httpPrintf(HTTP(con
), "\r\n") < 0)
782 con
->http
.state
= HTTP_WAITING
;
786 break; /* Anti-compiler-warning-code */
791 * Handle any incoming data...
794 switch (con
->http
.state
)
799 case HTTP_POST_RECV
:
800 LogMessage(L_DEBUG
, "ReadClient() %d con->data_encoding = %s, con->data_remaining = %d, con->file = %d",
802 con
->http
.data_encoding
== HTTP_ENCODE_CHUNKED
? "chunked" : "length",
803 con
->http
.data_remaining
, con
->file
);
805 if (con
->request
!= NULL
)
808 * Grab any request data from the connection...
811 if ((ipp_state
= ippRead(&(con
->http
), con
->request
)) == IPP_ERROR
)
813 LogMessage(L_ERROR
, "ReadClient() %d IPP Read Error!",
818 else if (ipp_state
!= IPP_DATA
)
821 con
->bytes
+= ippLength(con
->request
);
824 if (con
->file
== 0 && con
->http
.state
!= HTTP_POST_SEND
)
827 * Create a file as needed for the request data...
830 snprintf(con
->filename
, sizeof(con
->filename
), "%s/%08x",
831 RequestRoot
, request_id
++);
832 con
->file
= open(con
->filename
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0640);
833 fchmod(con
->file
, 0640);
834 fchown(con
->file
, User
, Group
);
836 LogMessage(L_DEBUG
, "ReadClient() %d REQUEST %s", con
->http
.fd
,
841 if (!SendError(con
, HTTP_REQUEST_TOO_LARGE
))
849 if (con
->http
.state
!= HTTP_POST_SEND
)
851 if ((bytes
= httpRead(HTTP(con
), line
, sizeof(line
))) < 0)
860 LogMessage(L_DEBUG
, "ReadClient() %d writing %d bytes",
861 con
->http
.fd
, bytes
);
863 if (write(con
->file
, line
, bytes
) < bytes
)
867 unlink(con
->filename
);
869 if (!SendError(con
, HTTP_REQUEST_TOO_LARGE
))
876 else if (con
->http
.state
!= HTTP_POST_SEND
)
883 if (con
->http
.state
== HTTP_POST_SEND
)
887 fstat(con
->file
, &filestats
);
891 if (filestats
.st_size
> MaxRequestSize
&&
895 * Request is too big; remove it and send an error...
898 unlink(con
->filename
);
903 * Delete any IPP request data...
906 ippDelete(con
->request
);
910 if (!SendError(con
, HTTP_REQUEST_TOO_LARGE
))
919 if (!SendCommand(con
, con
->command
, con
->options
))
921 if (!SendError(con
, HTTP_NOT_FOUND
))
928 LogRequest(con
, HTTP_OK
);
933 ProcessIPPRequest(con
);
938 break; /* Anti-compiler-warning-code */
941 if (!con
->http
.keep_alive
&& con
->http
.state
== HTTP_WAITING
)
952 * 'SendCommand()' - Send output from a command via HTTP.
956 SendCommand(client_t
*con
,
963 if (con
->filename
[0])
964 fd
= open(con
->filename
, O_RDONLY
);
966 fd
= open("/dev/null", O_RDONLY
);
968 con
->pipe_pid
= pipe_command(con
, fd
, &(con
->file
), command
, options
);
972 LogMessage(L_DEBUG
, "SendCommand() %d command=\"%s\" file=%d pipe_pid=%d",
973 con
->http
.fd
, command
, con
->file
, con
->pipe_pid
);
975 if (con
->pipe_pid
== 0)
978 fcntl(con
->file
, F_SETFD
, fcntl(con
->file
, F_GETFD
) | FD_CLOEXEC
);
980 DEBUG_printf(("SendCommand: Adding fd %d to InputSet...\n", con
->file
));
981 FD_SET(con
->file
, &InputSet
);
982 FD_SET(con
->http
.fd
, &OutputSet
);
984 if (!SendHeader(con
, HTTP_OK
, NULL
))
987 if (con
->http
.version
== HTTP_1_1
)
989 con
->http
.data_encoding
= HTTP_ENCODE_CHUNKED
;
991 if (httpPrintf(HTTP(con
), "Transfer-Encoding: chunked\r\n") < 0)
1003 * 'SendError()' - Send an error message via HTTP.
1006 int /* O - 1 if successful, 0 otherwise */
1007 SendError(client_t
*con
, /* I - Connection */
1008 http_status_t code
) /* I - Error code */
1010 char message
[1024]; /* Message for user */
1014 * Put the request in the access_log file...
1017 if (con
->operation
> HTTP_WAITING
)
1018 LogRequest(con
, code
);
1021 * To work around bugs in some proxies, don't use Keep-Alive for some
1025 if (code
>= HTTP_BAD_REQUEST
)
1026 con
->http
.keep_alive
= HTTP_KEEPALIVE_OFF
;
1029 * Send an error message back to the client. If the error code is a
1030 * 400 or 500 series, make sure the message contains some text, too!
1033 if (!SendHeader(con
, code
, NULL
))
1036 if (con
->http
.version
>= HTTP_1_1
&& !con
->http
.keep_alive
)
1038 if (httpPrintf(HTTP(con
), "Connection: close\r\n") < 0)
1042 if (code
>= HTTP_BAD_REQUEST
)
1045 * Send a human-readable error message.
1048 snprintf(message
, sizeof(message
),
1049 "<HTML><HEAD><TITLE>%d %s</TITLE></HEAD>"
1050 "<BODY><H1>%s</H1>%s</BODY></HTML>\n",
1051 code
, httpStatus(code
), httpStatus(code
),
1052 con
->language
? con
->language
->messages
[code
] :
1055 if (httpPrintf(HTTP(con
), "Content-Type: text/html\r\n") < 0)
1057 if (httpPrintf(HTTP(con
), "Content-Length: %d\r\n", strlen(message
)) < 0)
1059 if (httpPrintf(HTTP(con
), "\r\n") < 0)
1061 if (httpPrintf(HTTP(con
), "%s", message
) < 0)
1064 else if (httpPrintf(HTTP(con
), "\r\n") < 0)
1067 con
->http
.state
= HTTP_WAITING
;
1074 * 'SendFile()' - Send a file via HTTP.
1078 SendFile(client_t
*con
,
1082 struct stat
*filestats
)
1084 con
->file
= open(filename
, O_RDONLY
);
1086 LogMessage(L_DEBUG
, "SendFile() %d file=%d", con
->http
.fd
, con
->file
);
1091 fcntl(con
->file
, F_SETFD
, fcntl(con
->file
, F_GETFD
) | FD_CLOEXEC
);
1095 if (!SendHeader(con
, code
, type
))
1098 if (httpPrintf(HTTP(con
), "Last-Modified: %s\r\n", httpGetDateString(filestats
->st_mtime
)) < 0)
1100 if (httpPrintf(HTTP(con
), "Content-Length: %d\r\n", filestats
->st_size
) < 0)
1102 if (httpPrintf(HTTP(con
), "\r\n") < 0)
1105 FD_SET(con
->http
.fd
, &OutputSet
);
1112 * 'SendHeader()' - Send an HTTP request.
1115 int /* O - 1 on success, 0 on failure */
1116 SendHeader(client_t
*con
, /* I - Client to send to */
1117 http_status_t code
, /* I - HTTP status code */
1118 char *type
) /* I - MIME type of document */
1120 location_t
*loc
; /* Authentication location */
1123 if (httpPrintf(HTTP(con
), "HTTP/%d.%d %d %s\r\n", con
->http
.version
/ 100,
1124 con
->http
.version
% 100, code
, httpStatus(code
)) < 0)
1126 if (httpPrintf(HTTP(con
), "Date: %s\r\n", httpGetDateString(time(NULL
))) < 0)
1128 if (httpPrintf(HTTP(con
), "Server: CUPS/1.1\r\n") < 0)
1130 if (con
->http
.keep_alive
&& con
->http
.version
>= HTTP_1_0
)
1132 if (httpPrintf(HTTP(con
), "Connection: Keep-Alive\r\n") < 0)
1134 if (httpPrintf(HTTP(con
), "Keep-Alive: timeout=%d\r\n", KeepAliveTimeout
) < 0)
1137 if (code
== HTTP_METHOD_NOT_ALLOWED
)
1138 if (httpPrintf(HTTP(con
), "Allow: GET, HEAD, OPTIONS, POST\r\n") < 0)
1141 if (code
== HTTP_UNAUTHORIZED
)
1143 loc
= FindBest(con
); /* This already succeeded in IsAuthorized */
1145 if (loc
->type
== AUTH_BASIC
)
1147 if (httpPrintf(HTTP(con
), "WWW-Authenticate: Basic realm=\"CUPS\"\r\n") < 0)
1152 if (httpPrintf(HTTP(con
), "WWW-Authenticate: Digest realm=\"CUPS\" "
1153 "nonce=\"%s\"\r\n", con
->http
.hostname
) < 0)
1157 if (con
->language
!= NULL
)
1159 if (httpPrintf(HTTP(con
), "Content-Language: %s\r\n",
1160 con
->language
->language
) < 0)
1164 if (httpPrintf(HTTP(con
), "Content-Type: %s; charset=%s\r\n", type
,
1165 cupsLangEncoding(con
->language
)) < 0)
1168 else if (type
!= NULL
)
1169 if (httpPrintf(HTTP(con
), "Content-Type: %s\r\n", type
) < 0)
1177 * 'WriteClient()' - Write data to a client as needed.
1180 int /* O - 1 if success, 0 if fail */
1181 WriteClient(client_t
*con
) /* I - Client connection */
1183 int bytes
; /* Number of bytes written */
1184 char buf
[HTTP_MAX_BUFFER
+ 1];/* Data buffer */
1185 char *bufptr
; /* Pointer into buffer */
1186 ipp_state_t ipp_state
; /* IPP state value */
1189 if (con
->http
.state
!= HTTP_GET_SEND
&&
1190 con
->http
.state
!= HTTP_POST_SEND
)
1193 if (con
->response
!= NULL
)
1195 ipp_state
= ippWrite(&(con
->http
), con
->response
);
1196 bytes
= ipp_state
!= IPP_ERROR
&& ipp_state
!= IPP_DATA
;
1198 else if ((bytes
= read(con
->file
, buf
, HTTP_MAX_BUFFER
)) > 0)
1200 if (con
->pipe_pid
&& !con
->got_fields
)
1203 * Inspect the data for Content-Type and other fields.
1208 for (bufptr
= buf
; !con
->got_fields
&& *bufptr
; bufptr
++)
1209 if (*bufptr
== '\n')
1212 * Send line to client...
1215 if (bufptr
> buf
&& bufptr
[-1] == '\r')
1219 httpPrintf(HTTP(con
), "%s\r\n", buf
);
1220 LogMessage(L_DEBUG
, "WriteClient() %d %s", con
->http
.fd
, buf
);
1226 bytes
-= (bufptr
- buf
);
1227 memcpy(buf
, bufptr
, bytes
+ 1);
1231 * See if the line was empty...
1234 if (con
->field_col
== 0)
1235 con
->got_fields
= 1;
1239 else if (*bufptr
!= '\r')
1242 if (bytes
> 0 && !con
->got_fields
)
1245 * Remaining text needs to go out...
1248 httpPrintf(HTTP(con
), "%s", buf
);
1250 con
->http
.activity
= time(NULL
);
1253 else if (bytes
== 0)
1255 con
->http
.activity
= time(NULL
);
1260 if (httpWrite(HTTP(con
), buf
, bytes
) < 0)
1266 con
->bytes
+= bytes
;
1271 LogRequest(con
, HTTP_OK
);
1273 if (con
->http
.data_encoding
== HTTP_ENCODE_CHUNKED
)
1275 if (httpPrintf(HTTP(con
), "0\r\n\r\n") < 0)
1282 con
->http
.state
= HTTP_WAITING
;
1284 FD_CLR(con
->http
.fd
, &OutputSet
);
1288 DEBUG_printf(("WriteClient: Removing fd %d from InputSet...\n", con
->file
));
1289 FD_CLR(con
->file
, &InputSet
);
1292 kill(con
->pipe_pid
, SIGTERM
);
1299 if (con
->filename
[0])
1300 unlink(con
->filename
);
1302 if (con
->request
!= NULL
)
1304 ippDelete(con
->request
);
1305 con
->request
= NULL
;
1308 if (con
->response
!= NULL
)
1310 ippDelete(con
->response
);
1311 con
->response
= NULL
;
1314 if (!con
->http
.keep_alive
)
1322 LogMessage(L_DEBUG
, "WriteClient() %d %d bytes", con
->http
.fd
, bytes
);
1324 con
->http
.activity
= time(NULL
);
1331 * 'check_if_modified()' - Decode an "If-Modified-Since" line.
1334 static int /* O - 1 if modified since */
1335 check_if_modified(client_t
*con
, /* I - Client connection */
1336 struct stat
*filestats
) /* I - File information */
1338 char *ptr
; /* Pointer into field */
1339 time_t date
; /* Time/date value */
1340 int size
; /* Size/length value */
1345 ptr
= con
->http
.fields
[HTTP_FIELD_IF_MODIFIED_SINCE
];
1350 LogMessage(L_DEBUG
, "check_if_modified() %d If-Modified-Since=\"%s\"",
1353 while (*ptr
!= '\0')
1355 while (isspace(*ptr
) || *ptr
== ';')
1358 if (strncasecmp(ptr
, "length=", 7) == 0)
1363 while (isdigit(*ptr
))
1366 else if (isalpha(*ptr
))
1368 date
= httpGetDateTime(ptr
);
1369 while (*ptr
!= '\0' && *ptr
!= ';')
1374 LogMessage(L_DEBUG
, "check_if_modified() %d sizes=%d,%d dates=%d,%d",
1375 con
->http
.fd
, size
, filestats
->st_size
, date
, filestats
->st_mtime
);
1377 return ((size
!= filestats
->st_size
&& size
!= 0) ||
1378 (date
< filestats
->st_mtime
&& date
!= 0) ||
1379 (size
== 0 && date
== 0));
1384 * 'decode_auth()' - Decode an authorization string.
1388 decode_auth(client_t
*con
) /* I - Client to decode to */
1390 char *s
, /* Authorization string */
1391 value
[1024]; /* Value string */
1392 const char *username
; /* Certificate username */
1396 * Decode the string...
1399 s
= con
->http
.fields
[HTTP_FIELD_AUTHORIZATION
];
1401 DEBUG_printf(("decode_auth(%08x): Authorization string = \"%s\"\n",
1404 if (strncmp(s
, "Basic", 5) == 0)
1410 httpDecode64(value
, s
);
1413 * Pull the username and password out...
1416 if ((s
= strchr(value
, ':')) == NULL
)
1418 LogMessage(L_DEBUG
, "decode_auth() %d no colon in auth string \"%s\"",
1419 con
->http
.fd
, value
);
1425 strncpy(con
->username
, value
, sizeof(con
->username
) - 1);
1426 con
->username
[sizeof(con
->username
) - 1] = '\0';
1428 strncpy(con
->password
, s
, sizeof(con
->password
) - 1);
1429 con
->password
[sizeof(con
->password
) - 1] = '\0';
1431 else if (strncmp(s
, "Local", 5) == 0)
1437 if ((username
= FindCert(s
)) != NULL
)
1438 strcpy(con
->username
, username
);
1440 else if (strncmp(s
, "Digest", 5) == 0)
1443 * Get the username and password from the Digest attributes...
1446 if (httpGetSubField(&(con
->http
), HTTP_FIELD_WWW_AUTHENTICATE
, "username",
1449 strncpy(con
->username
, value
, sizeof(con
->username
) - 1);
1450 con
->username
[sizeof(con
->username
) - 1] = '\0';
1453 if (httpGetSubField(&(con
->http
), HTTP_FIELD_WWW_AUTHENTICATE
, "response",
1456 strncpy(con
->password
, value
, sizeof(con
->password
) - 1);
1457 con
->password
[sizeof(con
->password
) - 1] = '\0';
1461 LogMessage(L_DEBUG
, "decode_auth() %d username=\"%s\"",
1462 con
->http
.fd
, con
->username
);
1463 DEBUG_printf(("decode_auth() %d username=\"%s\" password=\"%s\"\n",
1464 con
->http
.fd
, con
->username
, con
->password
));
1469 * 'get_file()' - Get a filename and state info.
1472 static char * /* O - Real filename */
1473 get_file(client_t
*con
, /* I - Client connection */
1474 struct stat
*filestats
)/* O - File information */
1476 int status
; /* Status of filesystem calls */
1477 char *params
; /* Pointer to parameters in URI */
1478 static char filename
[1024]; /* Filename buffer */
1482 * Need to add DocumentRoot global...
1485 if (strncmp(con
->uri
, "/ppd/", 5) == 0)
1486 snprintf(filename
, sizeof(filename
), "%s%s", ServerRoot
, con
->uri
);
1487 else if (con
->language
!= NULL
)
1488 snprintf(filename
, sizeof(filename
), "%s/%s%s", DocumentRoot
, con
->language
->language
,
1491 snprintf(filename
, sizeof(filename
), "%s%s", DocumentRoot
, con
->uri
);
1493 if ((params
= strchr(filename
, '?')) != NULL
)
1497 * Grab the status for this language; if there isn't a language-specific file
1498 * then fallback to the default one...
1501 if ((status
= stat(filename
, filestats
)) != 0 && con
->language
!= NULL
)
1504 * Drop the language prefix and try the current directory...
1507 if (strncmp(con
->uri
, "/ppd/", 5) != 0)
1509 snprintf(filename
, sizeof(filename
), "%s%s", DocumentRoot
, con
->uri
);
1511 status
= stat(filename
, filestats
);
1516 * If we're found a directory, get the index.html file instead...
1519 if (!status
&& S_ISDIR(filestats
->st_mode
))
1521 if (filename
[strlen(filename
) - 1] == '/')
1522 strcat(filename
, "index.html");
1524 strcat(filename
, "/index.html");
1526 status
= stat(filename
, filestats
);
1529 LogMessage(L_DEBUG
, "get_file() %d filename=%s size=%d",
1530 con
->http
.fd
, filename
, status
? -1 : filestats
->st_size
);
1540 * 'pipe_command()' - Pipe the output of a command to the remote client.
1543 static int /* O - Process ID */
1544 pipe_command(client_t
*con
, /* I - Client connection */
1545 int infile
, /* I - Standard input for command */
1546 int *outfile
, /* O - Standard output for command */
1547 char *command
, /* I - Command to run */
1548 char *options
) /* I - Options for command */
1550 int pid
; /* Process ID */
1551 char *commptr
; /* Command string pointer */
1552 int fd
; /* Looping var */
1553 int fds
[2]; /* Pipe FDs */
1554 int argc
; /* Number of arguments */
1555 int envc
; /* Number of environment variables */
1556 char argbuf
[10240], /* Argument buffer */
1557 *argv
[100], /* Argument strings */
1558 *envp
[100]; /* Environment variables */
1559 char hostname
[1024], /* Hostname string */
1560 lang
[1024], /* LANG env variable */
1561 content_length
[1024], /* CONTENT_LENGTH env variable */
1562 content_type
[1024], /* CONTENT_TYPE env variable */
1563 ipp_port
[1024], /* Default listen port */
1564 server_port
[1024], /* Default server port */
1565 server_name
[1024], /* Default listen hostname */
1566 remote_host
[1024], /* REMOTE_HOST env variable */
1567 remote_user
[1024], /* REMOTE_USER env variable */
1568 tmpdir
[1024], /* TMPDIR environment variable */
1569 ldpath
[1024], /* LD_LIBRARY_PATH environment variable */
1570 datadir
[1024], /* CUPS_DATADIR environment variable */
1571 root
[1024], /* CUPS_SERVERROOT environment variable */
1572 query_string
[10240]; /* QUERY_STRING env variable */
1576 * Copy the command string...
1579 strncpy(argbuf
, options
, sizeof(argbuf
) - 1);
1580 argbuf
[sizeof(argbuf
) - 1] = '\0';
1583 * Parse the string; arguments can be separated by + and are terminated
1589 for (commptr
= argbuf
, argc
= 1; *commptr
!= '\0' && argc
< 99; commptr
++)
1590 if (*commptr
== ' ' || *commptr
== '+')
1594 while (*commptr
== ' ')
1597 if (*commptr
!= '\0')
1599 argv
[argc
] = commptr
;
1605 else if (*commptr
== '%')
1607 if (commptr
[1] >= '0' && commptr
[1] <= '9')
1608 *commptr
= (commptr
[1] - '0') << 4;
1610 *commptr
= (tolower(commptr
[1]) - 'a' + 10) << 4;
1612 if (commptr
[2] >= '0' && commptr
[2] <= '9')
1613 *commptr
|= commptr
[2] - '0';
1615 *commptr
|= tolower(commptr
[2]) - 'a' + 10;
1617 strcpy(commptr
+ 1, commptr
+ 3);
1619 else if (*commptr
== '?')
1624 if (argv
[0][0] == '\0')
1625 argv
[0] = strrchr(command
, '/') + 1;
1628 * Setup the environment variables as needed...
1631 gethostname(hostname
, sizeof(hostname
) - 1);
1633 sprintf(lang
, "LANG=%s", con
->language
? con
->language
->language
: "C");
1634 sprintf(ipp_port
, "IPP_PORT=%d", ntohs(con
->http
.hostaddr
.sin_port
));
1635 sprintf(server_port
, "SERVER_PORT=%d", ntohs(con
->http
.hostaddr
.sin_port
));
1636 snprintf(server_name
, sizeof(server_name
), "SERVER_NAME=%s", hostname
);
1637 snprintf(remote_host
, sizeof(remote_host
), "REMOTE_HOST=%s", con
->http
.hostname
);
1638 snprintf(remote_user
, sizeof(remote_user
), "REMOTE_USER=%s", con
->username
);
1639 snprintf(tmpdir
, sizeof(tmpdir
), "TMPDIR=%s", TempDir
);
1640 snprintf(datadir
, sizeof(datadir
), "CUPS_DATADIR=%s", DataDir
);
1641 snprintf(root
, sizeof(root
), "CUPS_SERVERROOT=%s", ServerRoot
);
1643 if (getenv("LD_LIBRARY_PATH") != NULL
)
1644 snprintf(ldpath
, sizeof(ldpath
), "LD_LIBRARY_PATH=%s", getenv("LD_LIBRARY_PATH"));
1648 envp
[0] = "PATH=/bin:/usr/bin";
1649 envp
[1] = "SERVER_SOFTWARE=CUPS/1.1";
1650 envp
[2] = "GATEWAY_INTERFACE=CGI/1.1";
1651 envp
[3] = "SERVER_PROTOCOL=HTTP/1.1";
1653 envp
[5] = server_name
;
1654 envp
[6] = server_port
;
1655 envp
[7] = remote_host
;
1656 envp
[8] = remote_user
;
1666 envp
[envc
++] = ldpath
;
1668 if (con
->operation
== HTTP_GET
)
1670 envp
[envc
++] = "REQUEST_METHOD=GET";
1675 * Add GET form variables after ?...
1680 snprintf(query_string
, sizeof(query_string
), "QUERY_STRING=%s", commptr
);
1681 envp
[envc
++] = query_string
;
1686 sprintf(content_length
, "CONTENT_LENGTH=%d", con
->bytes
);
1687 snprintf(content_type
, sizeof(content_type
), "CONTENT_TYPE=%s",
1688 con
->http
.fields
[HTTP_FIELD_CONTENT_TYPE
]);
1690 envp
[envc
++] = "REQUEST_METHOD=POST";
1691 envp
[envc
++] = content_length
;
1692 envp
[envc
++] = content_type
;
1698 * Create a pipe for the output...
1703 LogMessage(L_ERROR
, "Unable to create pipes for CGI %s - %s",
1704 argv
[0], strerror(errno
));
1709 * Then execute the command...
1712 if ((pid
= fork()) == 0)
1715 * Child comes here... Close stdin if necessary and dup the pipe to stdout.
1731 * Close extra file descriptors...
1734 for (fd
= 3; fd
< 1024; fd
++)
1738 * Change umask to restrict permissions on created files...
1744 * Execute the pipe program; if an error occurs, exit with status 1...
1747 execve(command
, argv
, envp
);
1748 perror("execve failed");
1755 * Error - can't fork!
1758 LogMessage(L_ERROR
, "Unable to fork for CGI %s - %s", argv
[0],
1768 * Fork successful - return the PID...
1771 AddCert(pid
, con
->username
);
1773 LogMessage(L_DEBUG
, "CGI %s started - PID = %d", argv
[0], pid
);
1784 * End of "$Id: client.c,v 1.64 2000/07/17 12:31:59 mike Exp $".