2 * "$Id: client.c,v 1.91.2.12 2002/05/16 14:00:07 mike Exp $"
4 * Client routines for the Common UNIX Printing System (CUPS) scheduler.
6 * Copyright 1997-2002 by Easy Software Products, all rights reserved.
8 * These coded instructions, statements, and computer programs are the
9 * property of Easy Software Products and are protected by Federal
10 * copyright law. Distribution and use rights are outlined in the file
11 * "LICENSE.txt" which should have been included with this file. If this
12 * file is missing or damaged please contact Easy Software Products
15 * Attn: CUPS Licensing Information
16 * Easy Software Products
17 * 44141 Airport View Drive, Suite 204
18 * Hollywood, Maryland 20636-3111 USA
20 * Voice: (301) 373-9603
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
26 * AcceptClient() - Accept a new client.
27 * CloseAllClients() - Close all remote clients immediately.
28 * CloseClient() - Close a remote client.
29 * EncryptClient() - Enable encryption for the client...
30 * ReadClient() - Read data from a client.
31 * SendCommand() - Send output from a command via HTTP.
32 * SendError() - Send an error message via HTTP.
33 * SendFile() - Send a file via HTTP.
34 * SendHeader() - Send an HTTP request.
35 * WriteClient() - Write data to a client as needed.
36 * check_if_modified() - Decode an "If-Modified-Since" line.
37 * decode_auth() - Decode an authorization string.
38 * get_file() - Get a filename and state info.
39 * install_conf_file() - Install a configuration file.
40 * pipe_command() - Pipe the output of a command to the remote client.
44 * Include necessary headers...
52 # include <openssl/err.h>
53 # include <openssl/ssl.h>
54 # include <openssl/rand.h>
55 #endif /* HAVE_LIBSSL */
62 static int check_if_modified(client_t
*con
,
63 struct stat
*filestats
);
64 static void decode_auth(client_t
*con
);
65 static char *get_file(client_t
*con
, struct stat
*filestats
);
66 static http_status_t
install_conf_file(client_t
*con
);
67 static int pipe_command(client_t
*con
, int infile
, int *outfile
,
68 char *command
, char *options
);
72 * 'AcceptClient()' - Accept a new client.
76 AcceptClient(listener_t
*lis
) /* I - Listener socket */
78 int i
; /* Looping var */
79 int val
; /* Parameter value */
80 client_t
*con
; /* New client pointer */
81 const struct hostent
*host
; /* Host entry for address */
82 char *hostname
;/* Hostname for address */
83 http_addr_t temp
; /* Temporary address variable */
86 LogMessage(L_DEBUG2
, "AcceptClient(%p) %d NumClients = %d",
87 lis
, lis
->fd
, NumClients
);
90 * Make sure we don't have a full set of clients already...
93 if (NumClients
== MaxClients
)
97 * Get a pointer to the next available client...
100 con
= Clients
+ NumClients
;
102 memset(con
, 0, sizeof(client_t
));
103 con
->http
.activity
= time(NULL
);
106 * Accept the client and get the remote address...
109 val
= sizeof(struct sockaddr_in
);
111 if ((con
->http
.fd
= accept(lis
->fd
, (struct sockaddr
*)&(con
->http
.hostaddr
),
114 LogMessage(L_ERROR
, "Unable to accept client connection - %s.",
120 if (lis
->address
.addr
.sa_family
== AF_INET6
)
121 con
->http
.hostaddr
.ipv6
.sin6_port
= lis
->address
.ipv6
.sin6_port
;
123 #endif /* AF_INET6 */
124 con
->http
.hostaddr
.ipv4
.sin_port
= lis
->address
.ipv4
.sin_port
;
127 hostname
= httpAddrLookup(&(con
->http
.hostaddr
), con
->http
.hostname
,
128 sizeof(con
->http
.hostname
));
132 httpAddrString(&(con
->http
.hostaddr
), con
->http
.hostname
,
133 sizeof(con
->http
.hostname
));
136 if (httpAddrEqual(&(con
->http
.hostaddr
), &ServerAddr
))
139 * Map accesses from the same host to the server name.
142 strlcpy(con
->http
.hostname
, ServerName
, sizeof(con
->http
.hostname
));
145 if (hostname
== NULL
&& HostNameLookups
== 2)
148 * Can't have an unresolved IP address with double-lookups enabled...
151 LogMessage(L_DEBUG2
, "AcceptClient: Closing connection %d...",
155 closesocket(con
->http
.fd
);
160 LogMessage(L_WARN
, "Name lookup failed - connection from %s closed!",
165 if (HostNameLookups
== 2)
168 * Do double lookups as needed...
171 if ((host
= httpGetHostByName(con
->http
.hostname
)) != NULL
)
174 * See if the hostname maps to the same IP address...
177 if (host
->h_addrtype
!= con
->http
.hostaddr
.addr
.sa_family
)
180 * Not the right type of address...
188 * Compare all of the addresses against this one...
191 for (i
= 0; host
->h_addr_list
[i
]; i
++)
193 httpAddrLoad(host
, 0, i
, &temp
);
195 if (httpAddrEqual(&(con
->http
.hostaddr
), &temp
))
199 if (!host
->h_addr_list
[i
])
207 * Can't have a hostname that doesn't resolve to the same IP address
208 * with double-lookups enabled...
211 LogMessage(L_DEBUG2
, "AcceptClient: Closing connection %d...",
215 closesocket(con
->http
.fd
);
220 LogMessage(L_WARN
, "IP lookup failed - connection from %s closed!",
227 if (con
->http
.hostaddr
.addr
.sa_family
== AF_INET6
)
228 LogMessage(L_DEBUG
, "AcceptClient: %d from %s:%d.", con
->http
.fd
,
229 con
->http
.hostname
, ntohs(con
->http
.hostaddr
.ipv6
.sin6_port
));
231 #endif /* AF_INET6 */
232 LogMessage(L_DEBUG
, "AcceptClient: %d from %s:%d.", con
->http
.fd
,
233 con
->http
.hostname
, ntohs(con
->http
.hostaddr
.ipv4
.sin_port
));
236 * Using TCP_NODELAY improves responsiveness, especially on systems
237 * with a slow loopback interface... Since we write large buffers
238 * when sending print files and requests, there shouldn't be any
239 * performance penalty for this...
243 setsockopt(con
->http
.fd
, IPPROTO_TCP
, TCP_NODELAY
, &val
, sizeof(val
));
246 * Add the socket to the select() input mask.
249 fcntl(con
->http
.fd
, F_SETFD
, fcntl(con
->http
.fd
, F_GETFD
) | FD_CLOEXEC
);
251 LogMessage(L_DEBUG2
, "AcceptClient: Adding fd %d to InputSet...",
253 FD_SET(con
->http
.fd
, &InputSet
);
258 * Temporarily suspend accept()'s until we lose a client...
261 if (NumClients
== MaxClients
)
266 * See if we are connecting on a secure port...
269 if (lis
->encryption
== HTTP_ENCRYPT_ALWAYS
)
272 * https connection; go secure...
277 #endif /* HAVE_LIBSSL */
282 * 'CloseAllClients()' - Close all remote clients immediately.
286 CloseAllClients(void)
288 while (NumClients
> 0)
289 CloseClient(Clients
);
294 * 'CloseClient()' - Close a remote client.
298 CloseClient(client_t
*con
) /* I - Client to close */
300 int status
; /* Exit status of pipe command */
302 SSL_CTX
*context
; /* Context for encryption */
303 SSL
*conn
; /* Connection for encryption */
304 #endif /* HAVE_LIBSSL */
307 LogMessage(L_DEBUG
, "CloseClient() %d", con
->http
.fd
);
311 * Shutdown encryption as needed...
316 conn
= (SSL
*)(con
->http
.tls
);
317 context
= SSL_get_SSL_CTX(conn
);
320 SSL_CTX_free(context
);
323 con
->http
.tls
= NULL
;
325 #endif /* HAVE_LIBSSL */
328 * Close the socket and clear the file from the input set for select()...
331 if (con
->http
.fd
>= 0)
333 LogMessage(L_DEBUG2
, "CloseClient: Removing fd %d from InputSet and OutputSet...",
336 FD_CLR(con
->http
.fd
, &InputSet
);
337 FD_CLR(con
->http
.fd
, &OutputSet
);
341 if (con
->pipe_pid
!= 0)
343 LogMessage(L_DEBUG2
, "CloseClient: Removing fd %d from InputSet...",
345 FD_CLR(con
->file
, &InputSet
);
351 * Close the open data file...
356 kill(con
->pipe_pid
, SIGKILL
);
357 waitpid(con
->pipe_pid
, &status
, WNOHANG
);
360 LogMessage(L_DEBUG2
, "CloseClient() %d Closing data file %d.",
361 con
->http
.fd
, con
->file
);
362 LogMessage(L_DEBUG2
, "CloseClient() %d Removing fd %d from InputSet.",
363 con
->http
.fd
, con
->file
);
365 FD_CLR(con
->file
, &InputSet
);
372 ippDelete(con
->request
);
378 ippDelete(con
->response
);
379 con
->response
= NULL
;
384 cupsLangFree(con
->language
);
385 con
->language
= NULL
;
389 * Re-enable new client connections if we are going back under the
393 if (NumClients
== MaxClients
)
397 * Compact the list of clients as necessary...
402 if (con
< (Clients
+ NumClients
))
403 memcpy(con
, con
+ 1, (Clients
+ NumClients
- con
) * sizeof(client_t
));
408 * 'EncryptClient()' - Enable encryption for the client...
411 int /* O - 1 on success, 0 on error */
412 EncryptClient(client_t
*con
) /* I - Client to encrypt */
415 SSL_CTX
*context
; /* Context for encryption */
416 SSL
*conn
; /* Connection for encryption */
417 unsigned long error
; /* Error code */
421 * Create the SSL context and accept the connection...
424 context
= SSL_CTX_new(SSLv23_method());
425 conn
= SSL_new(context
);
427 SSL_use_PrivateKey_file(conn
, ServerKey
, SSL_FILETYPE_PEM
);
428 SSL_use_certificate_file(conn
, ServerCertificate
, SSL_FILETYPE_PEM
);
430 SSL_set_fd(conn
, con
->http
.fd
);
431 if (SSL_accept(conn
) != 1)
433 while ((error
= ERR_get_error()) != 0)
434 LogMessage(L_ERROR
, "EncryptClient: %s", ERR_error_string(error
, NULL
));
436 SSL_CTX_free(context
);
441 con
->http
.tls
= conn
;
445 #endif /* HAVE_LIBSSL */
450 * 'ReadClient()' - Read data from a client.
453 int /* O - 1 on success, 0 on error */
454 ReadClient(client_t
*con
) /* I - Client to read from */
456 char line
[32768], /* Line from client... */
457 operation
[64], /* Operation code from socket */
458 version
[64]; /* HTTP version number string */
459 int major
, minor
; /* HTTP version numbers */
460 http_status_t status
; /* Transfer status */
461 ipp_state_t ipp_state
; /* State of IPP transfer */
462 int bytes
; /* Number of bytes to POST */
463 char *filename
; /* Name of file for GET/HEAD */
464 struct stat filestats
; /* File information */
465 mime_type_t
*type
; /* MIME type of file */
466 printer_t
*p
; /* Printer */
467 location_t
*best
; /* Best match for authentication */
468 static unsigned request_id
= 0;/* Request ID for temp files */
471 status
= HTTP_CONTINUE
;
473 switch (con
->http
.state
)
477 * See if we've received a request line...
480 if (httpGets(line
, sizeof(line
) - 1, HTTP(con
)) == NULL
)
487 * Ignore blank request lines...
494 * Clear other state variables...
497 httpClearFields(HTTP(con
));
499 con
->http
.activity
= time(NULL
);
500 con
->http
.version
= HTTP_1_0
;
501 con
->http
.keep_alive
= HTTP_KEEPALIVE_OFF
;
502 con
->http
.data_encoding
= HTTP_ENCODE_LENGTH
;
503 con
->http
.data_remaining
= 0;
504 con
->operation
= HTTP_WAITING
;
508 con
->command
[0] = '\0';
509 con
->username
[0] = '\0';
510 con
->password
[0] = '\0';
513 if (con
->language
!= NULL
)
515 cupsLangFree(con
->language
);
516 con
->language
= NULL
;
520 * Grab the request line...
523 switch (sscanf(line
, "%63s%1023s%63s", operation
, con
->uri
, version
))
526 SendError(con
, HTTP_BAD_REQUEST
);
530 con
->http
.version
= HTTP_0_9
;
533 if (sscanf(version
, "HTTP/%d.%d", &major
, &minor
) != 2)
535 SendError(con
, HTTP_BAD_REQUEST
);
542 con
->http
.version
= (http_version_t
)(major
* 100 + minor
);
543 if (con
->http
.version
== HTTP_1_1
&& KeepAlive
)
544 con
->http
.keep_alive
= HTTP_KEEPALIVE_ON
;
546 con
->http
.keep_alive
= HTTP_KEEPALIVE_OFF
;
550 SendError(con
, HTTP_NOT_SUPPORTED
);
558 * Process the request...
561 if (strcmp(operation
, "GET") == 0)
562 con
->http
.state
= HTTP_GET
;
563 else if (strcmp(operation
, "PUT") == 0)
564 con
->http
.state
= HTTP_PUT
;
565 else if (strcmp(operation
, "POST") == 0)
566 con
->http
.state
= HTTP_POST
;
567 else if (strcmp(operation
, "DELETE") == 0)
568 con
->http
.state
= HTTP_DELETE
;
569 else if (strcmp(operation
, "TRACE") == 0)
570 con
->http
.state
= HTTP_TRACE
;
571 else if (strcmp(operation
, "OPTIONS") == 0)
572 con
->http
.state
= HTTP_OPTIONS
;
573 else if (strcmp(operation
, "HEAD") == 0)
574 con
->http
.state
= HTTP_HEAD
;
577 SendError(con
, HTTP_BAD_REQUEST
);
582 con
->start
= time(NULL
);
583 con
->operation
= con
->http
.state
;
585 LogMessage(L_DEBUG
, "ReadClient() %d %s %s HTTP/%d.%d", con
->http
.fd
,
587 con
->http
.version
/ 100, con
->http
.version
% 100);
589 con
->http
.status
= HTTP_OK
;
599 * Parse incoming parameters until the status changes...
602 status
= httpUpdate(HTTP(con
));
604 if (status
!= HTTP_OK
&& status
!= HTTP_CONTINUE
)
606 SendError(con
, HTTP_BAD_REQUEST
);
613 break; /* Anti-compiler-warning-code */
617 * Handle new transfers...
620 if (status
== HTTP_OK
)
622 con
->language
= cupsLangGet(con
->http
.fields
[HTTP_FIELD_ACCEPT_LANGUAGE
]);
626 if (strncmp(con
->http
.fields
[HTTP_FIELD_CONNECTION
], "Keep-Alive", 10) == 0 &&
628 con
->http
.keep_alive
= HTTP_KEEPALIVE_ON
;
630 if (con
->http
.fields
[HTTP_FIELD_HOST
][0] == '\0' &&
631 con
->http
.version
>= HTTP_1_1
)
634 * HTTP/1.1 and higher require the "Host:" field...
637 if (!SendError(con
, HTTP_BAD_REQUEST
))
643 else if (con
->operation
== HTTP_OPTIONS
)
646 * Do OPTIONS command...
649 if ((best
= FindBest(con
->uri
, con
->http
.state
)) != NULL
&&
650 best
->type
!= AUTH_NONE
)
652 if (!SendHeader(con
, HTTP_UNAUTHORIZED
, NULL
))
659 if (strcasecmp(con
->http
.fields
[HTTP_FIELD_CONNECTION
], "Upgrade") == 0 &&
660 con
->http
.tls
== NULL
)
664 * Do encryption stuff...
667 if (!SendHeader(con
, HTTP_SWITCHING_PROTOCOLS
, NULL
))
673 httpPrintf(HTTP(con
), "Connection: Upgrade\r\n");
674 httpPrintf(HTTP(con
), "Upgrade: TLS/1.0,HTTP/1.1\r\n");
675 httpPrintf(HTTP(con
), "Content-Length: 0\r\n");
676 httpPrintf(HTTP(con
), "\r\n");
680 if (!SendError(con
, HTTP_NOT_IMPLEMENTED
))
685 #endif /* HAVE_LIBSSL */
688 if (!SendHeader(con
, HTTP_OK
, NULL
))
694 httpPrintf(HTTP(con
), "Allow: GET, HEAD, OPTIONS, POST, PUT\r\n");
695 httpPrintf(HTTP(con
), "Content-Length: 0\r\n");
696 httpPrintf(HTTP(con
), "\r\n");
698 else if (strstr(con
->uri
, "..") != NULL
)
701 * Protect against malicious users!
704 if (!SendError(con
, HTTP_FORBIDDEN
))
710 else if (con
->uri
[0] != '/')
713 * Don't allow proxying (yet)...
716 if (!SendError(con
, HTTP_METHOD_NOT_ALLOWED
))
724 if (strcasecmp(con
->http
.fields
[HTTP_FIELD_CONNECTION
], "Upgrade") == 0 &&
725 con
->http
.tls
== NULL
)
729 * Do encryption stuff...
732 if (!SendHeader(con
, HTTP_SWITCHING_PROTOCOLS
, NULL
))
738 httpPrintf(HTTP(con
), "Connection: Upgrade\r\n");
739 httpPrintf(HTTP(con
), "Upgrade: TLS/1.0,HTTP/1.1\r\n");
740 httpPrintf(HTTP(con
), "Content-Length: 0\r\n");
741 httpPrintf(HTTP(con
), "\r\n");
745 status
= IsAuthorized(con
);
747 if (!SendError(con
, HTTP_NOT_IMPLEMENTED
))
752 #endif /* HAVE_LIBSSL */
755 if (status
!= HTTP_OK
)
757 SendError(con
, status
);
762 switch (con
->http
.state
)
765 if (strncmp(con
->uri
, "/printers/", 10) == 0 &&
766 strcmp(con
->uri
+ strlen(con
->uri
) - 4, ".ppd") == 0)
769 * Send PPD file - get the real printer name since printer
770 * names are not case sensitive but filenames can be...
773 con
->uri
[strlen(con
->uri
) - 4] = '\0'; /* Drop ".ppd" */
775 if ((p
= FindPrinter(con
->uri
+ 10)) != NULL
)
776 snprintf(con
->uri
, sizeof(con
->uri
), "/ppd/%s.ppd", p
->name
);
779 if (!SendError(con
, HTTP_NOT_FOUND
))
789 if ((strncmp(con
->uri
, "/admin", 6) == 0 &&
790 strncmp(con
->uri
, "/admin/conf/", 12) != 0) ||
791 strncmp(con
->uri
, "/printers", 9) == 0 ||
792 strncmp(con
->uri
, "/classes", 8) == 0 ||
793 strncmp(con
->uri
, "/jobs", 5) == 0)
799 if (strncmp(con
->uri
, "/admin", 6) == 0)
801 snprintf(con
->command
, sizeof(con
->command
),
802 "%s/cgi-bin/admin.cgi", ServerBin
);
803 con
->options
= con
->uri
+ 6;
805 else if (strncmp(con
->uri
, "/printers", 9) == 0)
807 snprintf(con
->command
, sizeof(con
->command
),
808 "%s/cgi-bin/printers.cgi", ServerBin
);
809 con
->options
= con
->uri
+ 9;
811 else if (strncmp(con
->uri
, "/classes", 8) == 0)
813 snprintf(con
->command
, sizeof(con
->command
),
814 "%s/cgi-bin/classes.cgi", ServerBin
);
815 con
->options
= con
->uri
+ 8;
819 snprintf(con
->command
, sizeof(con
->command
),
820 "%s/cgi-bin/jobs.cgi", ServerBin
);
821 con
->options
= con
->uri
+ 5;
824 if (con
->options
[0] == '/')
827 if (!SendCommand(con
, con
->command
, con
->options
))
829 if (!SendError(con
, HTTP_NOT_FOUND
))
836 LogRequest(con
, HTTP_OK
);
838 if (con
->http
.version
<= HTTP_1_0
)
839 con
->http
.keep_alive
= HTTP_KEEPALIVE_OFF
;
841 else if (strncmp(con
->uri
, "/admin/conf/", 12) == 0 &&
842 (strchr(con
->uri
+ 12, '/') != NULL
||
843 strlen(con
->uri
) == 12))
846 * GET can only be done to configuration files under
850 if (!SendError(con
, HTTP_FORBIDDEN
))
864 if ((filename
= get_file(con
, &filestats
)) == NULL
)
866 if (!SendError(con
, HTTP_NOT_FOUND
))
872 else if (!check_if_modified(con
, &filestats
))
874 if (!SendError(con
, HTTP_NOT_MODIFIED
))
882 type
= mimeFileType(MimeDatabase
, filename
);
884 strcpy(line
, "text/plain");
886 snprintf(line
, sizeof(line
), "%s/%s", type
->super
, type
->type
);
888 if (!SendFile(con
, HTTP_OK
, filename
, line
, &filestats
))
897 case HTTP_POST_RECV
:
899 * See if the POST request includes a Content-Length field, and if
900 * so check the length against any limits that are set...
903 LogMessage(L_DEBUG2
, "POST %s", con
->uri
);
904 LogMessage(L_DEBUG2
, "CONTENT_TYPE = %s", con
->http
.fields
[HTTP_FIELD_CONTENT_TYPE
]);
906 if (con
->http
.fields
[HTTP_FIELD_CONTENT_LENGTH
][0] &&
907 atoi(con
->http
.fields
[HTTP_FIELD_CONTENT_LENGTH
]) > MaxRequestSize
&&
911 * Request too large...
914 if (!SendError(con
, HTTP_REQUEST_TOO_LARGE
))
924 * See what kind of POST request this is; for IPP requests the
925 * content-type field will be "application/ipp"...
928 if (strcmp(con
->http
.fields
[HTTP_FIELD_CONTENT_TYPE
], "application/ipp") == 0)
929 con
->request
= ippNew();
930 else if ((strncmp(con
->uri
, "/admin", 6) == 0 &&
931 strncmp(con
->uri
, "/admin/conf/", 12) != 0) ||
932 strncmp(con
->uri
, "/printers", 9) == 0 ||
933 strncmp(con
->uri
, "/classes", 8) == 0 ||
934 strncmp(con
->uri
, "/jobs", 5) == 0)
940 if (strncmp(con
->uri
, "/admin", 6) == 0)
942 snprintf(con
->command
, sizeof(con
->command
),
943 "%s/cgi-bin/admin.cgi", ServerBin
);
944 con
->options
= con
->uri
+ 6;
946 else if (strncmp(con
->uri
, "/printers", 9) == 0)
948 snprintf(con
->command
, sizeof(con
->command
),
949 "%s/cgi-bin/printers.cgi", ServerBin
);
950 con
->options
= con
->uri
+ 9;
952 else if (strncmp(con
->uri
, "/classes", 8) == 0)
954 snprintf(con
->command
, sizeof(con
->command
),
955 "%s/cgi-bin/classes.cgi", ServerBin
);
956 con
->options
= con
->uri
+ 8;
960 snprintf(con
->command
, sizeof(con
->command
),
961 "%s/cgi-bin/jobs.cgi", ServerBin
);
962 con
->options
= con
->uri
+ 5;
965 if (con
->options
[0] == '/')
968 LogMessage(L_DEBUG2
, "ReadClient() %d command=\"%s\", options = \"%s\"",
969 con
->http
.fd
, con
->command
, con
->options
);
971 if (con
->http
.version
<= HTTP_1_0
)
972 con
->http
.keep_alive
= HTTP_KEEPALIVE_OFF
;
974 else if (!SendError(con
, HTTP_UNAUTHORIZED
))
983 * Validate the resource name...
986 if (strncmp(con
->uri
, "/admin/conf/", 12) != 0 ||
987 strchr(con
->uri
+ 12, '/') != NULL
||
988 strlen(con
->uri
) == 12)
991 * PUT can only be done to configuration files under
995 if (!SendError(con
, HTTP_FORBIDDEN
))
1005 * See if the PUT request includes a Content-Length field, and if
1006 * so check the length against any limits that are set...
1009 LogMessage(L_DEBUG2
, "PUT %s", con
->uri
);
1010 LogMessage(L_DEBUG2
, "CONTENT_TYPE = %s", con
->http
.fields
[HTTP_FIELD_CONTENT_TYPE
]);
1012 if (con
->http
.fields
[HTTP_FIELD_CONTENT_LENGTH
][0] &&
1013 atoi(con
->http
.fields
[HTTP_FIELD_CONTENT_LENGTH
]) > MaxRequestSize
&&
1017 * Request too large...
1020 if (!SendError(con
, HTTP_REQUEST_TOO_LARGE
))
1030 * Open a temporary file to hold the request...
1033 snprintf(con
->filename
, sizeof(con
->filename
), "%s/%08x",
1034 RequestRoot
, request_id
++);
1035 con
->file
= open(con
->filename
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0640);
1036 fchmod(con
->file
, 0640);
1037 fchown(con
->file
, User
, Group
);
1039 LogMessage(L_DEBUG2
, "ReadClient() %d REQUEST %s=%d", con
->http
.fd
,
1040 con
->filename
, con
->file
);
1044 if (!SendError(con
, HTTP_REQUEST_TOO_LARGE
))
1054 SendError(con
, HTTP_NOT_IMPLEMENTED
);
1059 if (strncmp(con
->uri
, "/printers/", 10) == 0 &&
1060 strcmp(con
->uri
+ strlen(con
->uri
) - 4, ".ppd") == 0)
1063 * Send PPD file - get the real printer name since printer
1064 * names are not case sensitive but filenames can be...
1067 con
->uri
[strlen(con
->uri
) - 4] = '\0'; /* Drop ".ppd" */
1069 if ((p
= FindPrinter(con
->uri
+ 10)) != NULL
)
1070 snprintf(con
->uri
, sizeof(con
->uri
), "/ppd/%s.ppd", p
->name
);
1073 if (!SendError(con
, HTTP_NOT_FOUND
))
1083 if ((strncmp(con
->uri
, "/admin/", 7) == 0 &&
1084 strncmp(con
->uri
, "/admin/conf/", 12) != 0) ||
1085 strncmp(con
->uri
, "/printers/", 10) == 0 ||
1086 strncmp(con
->uri
, "/classes/", 9) == 0 ||
1087 strncmp(con
->uri
, "/jobs/", 6) == 0)
1093 if (!SendHeader(con
, HTTP_OK
, "text/html"))
1099 if (httpPrintf(HTTP(con
), "\r\n") < 0)
1105 LogRequest(con
, HTTP_OK
);
1107 else if (strncmp(con
->uri
, "/admin/conf/", 12) == 0 &&
1108 (strchr(con
->uri
+ 12, '/') != NULL
||
1109 strlen(con
->uri
) == 12))
1112 * HEAD can only be done to configuration files under
1116 if (!SendError(con
, HTTP_FORBIDDEN
))
1124 else if ((filename
= get_file(con
, &filestats
)) == NULL
)
1126 if (!SendHeader(con
, HTTP_NOT_FOUND
, "text/html"))
1132 LogRequest(con
, HTTP_NOT_FOUND
);
1134 else if (!check_if_modified(con
, &filestats
))
1136 if (!SendError(con
, HTTP_NOT_MODIFIED
))
1142 LogRequest(con
, HTTP_NOT_MODIFIED
);
1150 type
= mimeFileType(MimeDatabase
, filename
);
1152 strcpy(line
, "text/plain");
1154 snprintf(line
, sizeof(line
), "%s/%s", type
->super
, type
->type
);
1156 if (!SendHeader(con
, HTTP_OK
, line
))
1162 if (httpPrintf(HTTP(con
), "Last-Modified: %s\r\n",
1163 httpGetDateString(filestats
.st_mtime
)) < 0)
1169 if (httpPrintf(HTTP(con
), "Content-Length: %lu\r\n",
1170 (unsigned long)filestats
.st_size
) < 0)
1176 LogRequest(con
, HTTP_OK
);
1179 if (httpPrintf(HTTP(con
), "\r\n") < 0)
1185 con
->http
.state
= HTTP_WAITING
;
1189 break; /* Anti-compiler-warning-code */
1195 * Handle any incoming data...
1198 switch (con
->http
.state
)
1200 case HTTP_PUT_RECV
:
1201 LogMessage(L_DEBUG2
, "ReadClient() %d con->data_encoding = %s, con->data_remaining = %d, con->file = %d",
1203 con
->http
.data_encoding
== HTTP_ENCODE_CHUNKED
? "chunked" : "length",
1204 con
->http
.data_remaining
, con
->file
);
1206 if ((bytes
= httpRead(HTTP(con
), line
, sizeof(line
))) < 0)
1213 con
->bytes
+= bytes
;
1215 LogMessage(L_DEBUG2
, "ReadClient() %d writing %d bytes to %d",
1216 con
->http
.fd
, bytes
, con
->file
);
1218 if (write(con
->file
, line
, bytes
) < bytes
)
1220 LogMessage(L_ERROR
, "ReadClient: Unable to write %d bytes to %s: %s",
1221 bytes
, con
->filename
, strerror(errno
));
1223 LogMessage(L_DEBUG2
, "ReadClient: Closing data file %d...",
1228 unlink(con
->filename
);
1229 con
->filename
[0] = '\0';
1231 if (!SendError(con
, HTTP_REQUEST_TOO_LARGE
))
1239 if (con
->http
.state
== HTTP_WAITING
)
1242 * End of file, see how big it is...
1245 fstat(con
->file
, &filestats
);
1247 LogMessage(L_DEBUG2
, "ReadClient() %d Closing data file %d, size = %d.",
1248 con
->http
.fd
, con
->file
, (int)filestats
.st_size
);
1253 if (filestats
.st_size
> MaxRequestSize
&&
1257 * Request is too big; remove it and send an error...
1260 LogMessage(L_DEBUG2
, "ReadClient() %d Removing temp file %s",
1261 con
->http
.fd
, con
->filename
);
1262 unlink(con
->filename
);
1263 con
->filename
[0] = '\0';
1265 if (!SendError(con
, HTTP_REQUEST_TOO_LARGE
))
1273 * Install the configuration file...
1276 status
= install_conf_file(con
);
1279 * Return the status to the client...
1282 if (!SendError(con
, status
))
1290 case HTTP_POST_RECV
:
1291 LogMessage(L_DEBUG2
, "ReadClient() %d con->data_encoding = %s, con->data_remaining = %d, con->file = %d",
1293 con
->http
.data_encoding
== HTTP_ENCODE_CHUNKED
? "chunked" : "length",
1294 con
->http
.data_remaining
, con
->file
);
1296 if (con
->request
!= NULL
)
1299 * Grab any request data from the connection...
1302 if ((ipp_state
= ippRead(&(con
->http
), con
->request
)) == IPP_ERROR
)
1304 LogMessage(L_ERROR
, "ReadClient() %d IPP Read Error!",
1309 else if (ipp_state
!= IPP_DATA
)
1312 con
->bytes
+= ippLength(con
->request
);
1315 if (con
->file
== 0 && con
->http
.state
!= HTTP_POST_SEND
)
1318 * Create a file as needed for the request data...
1321 snprintf(con
->filename
, sizeof(con
->filename
), "%s/%08x",
1322 RequestRoot
, request_id
++);
1323 con
->file
= open(con
->filename
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0640);
1324 fchmod(con
->file
, 0640);
1325 fchown(con
->file
, User
, Group
);
1327 LogMessage(L_DEBUG2
, "ReadClient() %d REQUEST %s=%d", con
->http
.fd
,
1328 con
->filename
, con
->file
);
1332 if (!SendError(con
, HTTP_REQUEST_TOO_LARGE
))
1340 if (con
->http
.state
!= HTTP_POST_SEND
)
1342 if ((bytes
= httpRead(HTTP(con
), line
, sizeof(line
))) < 0)
1349 con
->bytes
+= bytes
;
1351 LogMessage(L_DEBUG2
, "ReadClient() %d writing %d bytes to %d",
1352 con
->http
.fd
, bytes
, con
->file
);
1354 if (write(con
->file
, line
, bytes
) < bytes
)
1356 LogMessage(L_ERROR
, "ReadClient: Unable to write %d bytes to %s: %s",
1357 bytes
, con
->filename
, strerror(errno
));
1359 LogMessage(L_DEBUG2
, "ReadClient: Closing file %d...",
1364 unlink(con
->filename
);
1365 con
->filename
[0] = '\0';
1367 if (!SendError(con
, HTTP_REQUEST_TOO_LARGE
))
1374 else if (con
->http
.state
!= HTTP_POST_SEND
)
1381 if (con
->http
.state
== HTTP_POST_SEND
)
1385 fstat(con
->file
, &filestats
);
1387 LogMessage(L_DEBUG2
, "ReadClient() %d Closing data file %d, size = %d.",
1388 con
->http
.fd
, con
->file
, (int)filestats
.st_size
);
1393 if (filestats
.st_size
> MaxRequestSize
&&
1397 * Request is too big; remove it and send an error...
1400 LogMessage(L_DEBUG2
, "ReadClient() %d Removing temp file %s",
1401 con
->http
.fd
, con
->filename
);
1402 unlink(con
->filename
);
1403 con
->filename
[0] = '\0';
1408 * Delete any IPP request data...
1411 ippDelete(con
->request
);
1412 con
->request
= NULL
;
1415 if (!SendError(con
, HTTP_REQUEST_TOO_LARGE
))
1422 if (con
->command
[0])
1424 if (!SendCommand(con
, con
->command
, con
->options
))
1426 if (!SendError(con
, HTTP_NOT_FOUND
))
1433 LogRequest(con
, HTTP_OK
);
1438 ProcessIPPRequest(con
);
1443 break; /* Anti-compiler-warning-code */
1446 if (!con
->http
.keep_alive
&& con
->http
.state
== HTTP_WAITING
)
1457 * 'SendCommand()' - Send output from a command via HTTP.
1461 SendCommand(client_t
*con
,
1468 if (con
->filename
[0])
1469 fd
= open(con
->filename
, O_RDONLY
);
1471 fd
= open("/dev/null", O_RDONLY
);
1473 con
->pipe_pid
= pipe_command(con
, fd
, &(con
->file
), command
, options
);
1477 LogMessage(L_INFO
, "Started \"%s\" (pid=%d)", command
, con
->pipe_pid
);
1479 LogMessage(L_DEBUG
, "SendCommand() %d file=%d", con
->http
.fd
, con
->file
);
1481 if (con
->pipe_pid
== 0)
1484 fcntl(con
->file
, F_SETFD
, fcntl(con
->file
, F_GETFD
) | FD_CLOEXEC
);
1486 LogMessage(L_DEBUG2
, "SendCommand: Adding fd %d to InputSet...", con
->file
);
1487 LogMessage(L_DEBUG2
, "SendCommand: Adding fd %d to OutputSet...",
1490 FD_SET(con
->file
, &InputSet
);
1491 FD_SET(con
->http
.fd
, &OutputSet
);
1493 if (!SendHeader(con
, HTTP_OK
, NULL
))
1496 if (con
->http
.version
== HTTP_1_1
)
1498 con
->http
.data_encoding
= HTTP_ENCODE_CHUNKED
;
1500 if (httpPrintf(HTTP(con
), "Transfer-Encoding: chunked\r\n") < 0)
1504 con
->got_fields
= 0;
1512 * 'SendError()' - Send an error message via HTTP.
1515 int /* O - 1 if successful, 0 otherwise */
1516 SendError(client_t
*con
, /* I - Connection */
1517 http_status_t code
) /* I - Error code */
1519 char message
[1024]; /* Message for user */
1523 * Put the request in the access_log file...
1526 if (con
->operation
> HTTP_WAITING
)
1527 LogRequest(con
, code
);
1529 LogMessage(L_DEBUG
, "SendError() %d code=%d", con
->http
.fd
, code
);
1532 * To work around bugs in some proxies, don't use Keep-Alive for some
1536 if (code
>= HTTP_BAD_REQUEST
)
1537 con
->http
.keep_alive
= HTTP_KEEPALIVE_OFF
;
1540 * Send an error message back to the client. If the error code is a
1541 * 400 or 500 series, make sure the message contains some text, too!
1544 if (!SendHeader(con
, code
, NULL
))
1548 if (code
== HTTP_UPGRADE_REQUIRED
)
1549 if (httpPrintf(HTTP(con
), "Connection: Upgrade\r\n") < 0)
1552 if (httpPrintf(HTTP(con
), "Upgrade: TLS/1.0,HTTP/1.1\r\n") < 0)
1554 #endif /* HAVE_LIBSSL */
1556 if (con
->http
.version
>= HTTP_1_1
&& !con
->http
.keep_alive
)
1558 if (httpPrintf(HTTP(con
), "Connection: close\r\n") < 0)
1562 if (code
>= HTTP_BAD_REQUEST
)
1565 * Send a human-readable error message.
1568 snprintf(message
, sizeof(message
),
1569 "<HTML><HEAD><TITLE>%d %s</TITLE></HEAD>"
1570 "<BODY><H1>%s</H1>%s</BODY></HTML>\n",
1571 code
, httpStatus(code
), httpStatus(code
),
1572 con
->language
? con
->language
->messages
[code
] :
1575 if (httpPrintf(HTTP(con
), "Content-Type: text/html\r\n") < 0)
1577 if (httpPrintf(HTTP(con
), "Content-Length: %d\r\n", strlen(message
)) < 0)
1579 if (httpPrintf(HTTP(con
), "\r\n") < 0)
1581 if (httpPrintf(HTTP(con
), "%s", message
) < 0)
1584 else if (httpPrintf(HTTP(con
), "\r\n") < 0)
1587 con
->http
.state
= HTTP_WAITING
;
1594 * 'SendFile()' - Send a file via HTTP.
1598 SendFile(client_t
*con
,
1602 struct stat
*filestats
)
1604 con
->file
= open(filename
, O_RDONLY
);
1606 LogMessage(L_DEBUG
, "SendFile() %d file=%d", con
->http
.fd
, con
->file
);
1611 fcntl(con
->file
, F_SETFD
, fcntl(con
->file
, F_GETFD
) | FD_CLOEXEC
);
1615 if (!SendHeader(con
, code
, type
))
1618 if (httpPrintf(HTTP(con
), "Last-Modified: %s\r\n", httpGetDateString(filestats
->st_mtime
)) < 0)
1620 if (httpPrintf(HTTP(con
), "Content-Length: %lu\r\n",
1621 (unsigned long)filestats
->st_size
) < 0)
1623 if (httpPrintf(HTTP(con
), "\r\n") < 0)
1626 LogMessage(L_DEBUG2
, "SendFile: Adding fd %d to OutputSet...", con
->http
.fd
);
1628 FD_SET(con
->http
.fd
, &OutputSet
);
1635 * 'SendHeader()' - Send an HTTP request.
1638 int /* O - 1 on success, 0 on failure */
1639 SendHeader(client_t
*con
, /* I - Client to send to */
1640 http_status_t code
, /* I - HTTP status code */
1641 char *type
) /* I - MIME type of document */
1643 location_t
*loc
; /* Authentication location */
1646 if (httpPrintf(HTTP(con
), "HTTP/%d.%d %d %s\r\n", con
->http
.version
/ 100,
1647 con
->http
.version
% 100, code
, httpStatus(code
)) < 0)
1649 if (httpPrintf(HTTP(con
), "Date: %s\r\n", httpGetDateString(time(NULL
))) < 0)
1651 if (httpPrintf(HTTP(con
), "Server: CUPS/1.1\r\n") < 0)
1653 if (con
->http
.keep_alive
&& con
->http
.version
>= HTTP_1_0
)
1655 if (httpPrintf(HTTP(con
), "Connection: Keep-Alive\r\n") < 0)
1657 if (httpPrintf(HTTP(con
), "Keep-Alive: timeout=%d\r\n", KeepAliveTimeout
) < 0)
1660 if (code
== HTTP_METHOD_NOT_ALLOWED
)
1661 if (httpPrintf(HTTP(con
), "Allow: GET, HEAD, OPTIONS, POST\r\n") < 0)
1664 if (code
== HTTP_UNAUTHORIZED
)
1667 * This already succeeded in IsAuthorized...
1670 loc
= FindBest(con
->uri
, con
->http
.state
);
1672 if (loc
->type
!= AUTH_DIGEST
)
1674 if (httpPrintf(HTTP(con
), "WWW-Authenticate: Basic realm=\"CUPS\"\r\n") < 0)
1679 if (httpPrintf(HTTP(con
), "WWW-Authenticate: Digest realm=\"CUPS\" "
1680 "nonce=\"%s\"\r\n", con
->http
.hostname
) < 0)
1684 if (con
->language
!= NULL
)
1686 if (httpPrintf(HTTP(con
), "Content-Language: %s\r\n",
1687 con
->language
->language
) < 0)
1691 if (httpPrintf(HTTP(con
), "Content-Type: %s; charset=%s\r\n", type
,
1692 cupsLangEncoding(con
->language
)) < 0)
1695 else if (type
!= NULL
)
1696 if (httpPrintf(HTTP(con
), "Content-Type: %s\r\n", type
) < 0)
1704 * 'WriteClient()' - Write data to a client as needed.
1707 int /* O - 1 if success, 0 if fail */
1708 WriteClient(client_t
*con
) /* I - Client connection */
1710 int bytes
; /* Number of bytes written */
1711 char buf
[HTTP_MAX_BUFFER
+ 1];/* Data buffer */
1712 char *bufptr
; /* Pointer into buffer */
1713 ipp_state_t ipp_state
; /* IPP state value */
1716 if (con
->http
.state
!= HTTP_GET_SEND
&&
1717 con
->http
.state
!= HTTP_POST_SEND
)
1720 if (con
->response
!= NULL
)
1722 ipp_state
= ippWrite(&(con
->http
), con
->response
);
1723 bytes
= ipp_state
!= IPP_ERROR
&& ipp_state
!= IPP_DATA
;
1725 else if ((bytes
= read(con
->file
, buf
, HTTP_MAX_BUFFER
)) > 0)
1727 if (con
->pipe_pid
&& !con
->got_fields
)
1730 * Inspect the data for Content-Type and other fields.
1735 for (bufptr
= buf
; !con
->got_fields
&& *bufptr
; bufptr
++)
1736 if (*bufptr
== '\n')
1739 * Send line to client...
1742 if (bufptr
> buf
&& bufptr
[-1] == '\r')
1746 httpPrintf(HTTP(con
), "%s\r\n", buf
);
1747 LogMessage(L_DEBUG2
, "WriteClient() %d %s", con
->http
.fd
, buf
);
1753 bytes
-= (bufptr
- buf
);
1754 memcpy(buf
, bufptr
, bytes
+ 1);
1758 * See if the line was empty...
1761 if (con
->field_col
== 0)
1762 con
->got_fields
= 1;
1766 else if (*bufptr
!= '\r')
1769 if (bytes
> 0 && !con
->got_fields
)
1772 * Remaining text needs to go out...
1775 httpPrintf(HTTP(con
), "%s", buf
);
1777 con
->http
.activity
= time(NULL
);
1780 else if (bytes
== 0)
1782 con
->http
.activity
= time(NULL
);
1787 if (httpWrite(HTTP(con
), buf
, bytes
) < 0)
1793 con
->bytes
+= bytes
;
1798 LogRequest(con
, HTTP_OK
);
1800 if (con
->http
.data_encoding
== HTTP_ENCODE_CHUNKED
)
1802 if (httpPrintf(HTTP(con
), "0\r\n\r\n") < 0)
1809 con
->http
.state
= HTTP_WAITING
;
1811 LogMessage(L_DEBUG2
, "WriteClient() Removing fd %d from OutputSet...",
1814 FD_CLR(con
->http
.fd
, &OutputSet
);
1818 LogMessage(L_DEBUG2
, "WriteClient() Removing fd %d from InputSet...",
1820 FD_CLR(con
->file
, &InputSet
);
1823 kill(con
->pipe_pid
, SIGTERM
);
1825 LogMessage(L_DEBUG2
, "WriteClient() %d Closing data file %d.",
1826 con
->http
.fd
, con
->file
);
1833 if (con
->filename
[0])
1835 LogMessage(L_DEBUG2
, "WriteClient() %d Removing temp file %s",
1836 con
->http
.fd
, con
->filename
);
1837 unlink(con
->filename
);
1838 con
->filename
[0] = '\0';
1841 if (con
->request
!= NULL
)
1843 ippDelete(con
->request
);
1844 con
->request
= NULL
;
1847 if (con
->response
!= NULL
)
1849 ippDelete(con
->response
);
1850 con
->response
= NULL
;
1853 if (!con
->http
.keep_alive
)
1861 LogMessage(L_DEBUG2
, "WriteClient() %d %d bytes", con
->http
.fd
, bytes
);
1863 con
->http
.activity
= time(NULL
);
1870 * 'check_if_modified()' - Decode an "If-Modified-Since" line.
1873 static int /* O - 1 if modified since */
1874 check_if_modified(client_t
*con
, /* I - Client connection */
1875 struct stat
*filestats
) /* I - File information */
1877 char *ptr
; /* Pointer into field */
1878 time_t date
; /* Time/date value */
1879 int size
; /* Size/length value */
1884 ptr
= con
->http
.fields
[HTTP_FIELD_IF_MODIFIED_SINCE
];
1889 LogMessage(L_DEBUG2
, "check_if_modified() %d If-Modified-Since=\"%s\"",
1892 while (*ptr
!= '\0')
1894 while (isspace(*ptr
) || *ptr
== ';')
1897 if (strncasecmp(ptr
, "length=", 7) == 0)
1902 while (isdigit(*ptr
))
1905 else if (isalpha(*ptr
))
1907 date
= httpGetDateTime(ptr
);
1908 while (*ptr
!= '\0' && *ptr
!= ';')
1913 LogMessage(L_DEBUG2
, "check_if_modified() %d sizes=%d,%d dates=%d,%d",
1914 con
->http
.fd
, size
, (int)filestats
->st_size
, (int)date
,
1915 (int)filestats
->st_mtime
);
1917 return ((size
!= filestats
->st_size
&& size
!= 0) ||
1918 (date
< filestats
->st_mtime
&& date
!= 0) ||
1919 (size
== 0 && date
== 0));
1924 * 'decode_auth()' - Decode an authorization string.
1928 decode_auth(client_t
*con
) /* I - Client to decode to */
1930 char *s
, /* Authorization string */
1931 value
[1024]; /* Value string */
1932 const char *username
; /* Certificate username */
1936 * Decode the string...
1939 s
= con
->http
.fields
[HTTP_FIELD_AUTHORIZATION
];
1941 LogMessage(L_DEBUG2
, "decode_auth(%p): Authorization string = \"%s\"",
1944 if (strncmp(s
, "Basic", 5) == 0)
1950 httpDecode64(value
, s
);
1953 * Pull the username and password out...
1956 if ((s
= strchr(value
, ':')) == NULL
)
1958 LogMessage(L_DEBUG
, "decode_auth() %d no colon in auth string \"%s\"",
1959 con
->http
.fd
, value
);
1965 strlcpy(con
->username
, value
, sizeof(con
->username
));
1966 strlcpy(con
->password
, s
, sizeof(con
->password
));
1968 else if (strncmp(s
, "Local", 5) == 0)
1974 if ((username
= FindCert(s
)) != NULL
)
1975 strlcpy(con
->username
, username
, sizeof(con
->username
));
1977 else if (strncmp(s
, "Digest", 5) == 0)
1980 * Get the username and password from the Digest attributes...
1983 if (httpGetSubField(&(con
->http
), HTTP_FIELD_WWW_AUTHENTICATE
, "username",
1985 strlcpy(con
->username
, value
, sizeof(con
->username
));
1987 if (httpGetSubField(&(con
->http
), HTTP_FIELD_WWW_AUTHENTICATE
, "response",
1989 strlcpy(con
->password
, value
, sizeof(con
->password
) - 1);
1992 LogMessage(L_DEBUG2
, "decode_auth() %d username=\"%s\"",
1993 con
->http
.fd
, con
->username
);
1998 * 'get_file()' - Get a filename and state info.
2001 static char * /* O - Real filename */
2002 get_file(client_t
*con
, /* I - Client connection */
2003 struct stat
*filestats
)/* O - File information */
2005 int status
; /* Status of filesystem calls */
2006 char *params
; /* Pointer to parameters in URI */
2007 static char filename
[1024]; /* Filename buffer */
2011 * Need to add DocumentRoot global...
2014 if (strncmp(con
->uri
, "/ppd/", 5) == 0)
2015 snprintf(filename
, sizeof(filename
), "%s%s", ServerRoot
, con
->uri
);
2016 else if (strncmp(con
->uri
, "/admin/conf/", 12) == 0)
2017 snprintf(filename
, sizeof(filename
), "%s%s", ServerRoot
, con
->uri
+ 11);
2018 else if (con
->language
!= NULL
)
2019 snprintf(filename
, sizeof(filename
), "%s/%s%s", DocumentRoot
, con
->language
->language
,
2022 snprintf(filename
, sizeof(filename
), "%s%s", DocumentRoot
, con
->uri
);
2024 if ((params
= strchr(filename
, '?')) != NULL
)
2028 * Grab the status for this language; if there isn't a language-specific file
2029 * then fallback to the default one...
2032 if ((status
= stat(filename
, filestats
)) != 0 && con
->language
!= NULL
)
2035 * Drop the language prefix and try the current directory...
2038 if (strncmp(con
->uri
, "/ppd/", 5) != 0 &&
2039 strncmp(con
->uri
, "/admin/conf/", 12) != 0)
2041 snprintf(filename
, sizeof(filename
), "%s%s", DocumentRoot
, con
->uri
);
2043 status
= stat(filename
, filestats
);
2048 * If we're found a directory, get the index.html file instead...
2051 if (!status
&& S_ISDIR(filestats
->st_mode
))
2053 if (filename
[strlen(filename
) - 1] == '/')
2054 strlcat(filename
, "index.html", sizeof(filename
));
2056 strlcat(filename
, "/index.html", sizeof(filename
));
2058 status
= stat(filename
, filestats
);
2061 LogMessage(L_DEBUG2
, "get_file() %d filename=%s size=%d",
2062 con
->http
.fd
, filename
, status
? -1 : (int)filestats
->st_size
);
2072 * 'install_conf_file()' - Install a configuration file.
2075 static http_status_t
/* O - Status */
2076 install_conf_file(client_t
*con
) /* I - Connection */
2078 FILE *in
, /* Input file */
2079 *out
; /* Output file */
2080 char buffer
[1024]; /* Copy buffer */
2081 int bytes
; /* Number of bytes */
2082 char conffile
[1024], /* Configuration filename */
2083 newfile
[1024], /* New config filename */
2084 oldfile
[1024]; /* Old config filename */
2085 struct stat confinfo
; /* Config file info */
2089 * First construct the filenames...
2092 snprintf(conffile
, sizeof(conffile
), "%s%s", ServerRoot
, con
->uri
+ 11);
2093 snprintf(newfile
, sizeof(newfile
), "%s%s.N", ServerRoot
, con
->uri
+ 11);
2094 snprintf(oldfile
, sizeof(oldfile
), "%s%s.O", ServerRoot
, con
->uri
+ 11);
2096 LogMessage(L_INFO
, "Installing config file \"%s\"...", conffile
);
2099 * Get the owner, group, and permissions of the configuration file.
2100 * If it doesn't exist, assign it to the User and Group in the
2101 * cupsd.conf file with mode 0640 permissions.
2104 if (stat(conffile
, &confinfo
))
2106 confinfo
.st_uid
= User
;
2107 confinfo
.st_gid
= Group
;
2108 confinfo
.st_mode
= 0640;
2112 * Open the request file and new config file...
2115 if ((in
= fopen(con
->filename
, "rb")) == NULL
)
2117 LogMessage(L_ERROR
, "Unable to open request file \"%s\" - %s",
2118 con
->filename
, strerror(errno
));
2119 return (HTTP_SERVER_ERROR
);
2122 if ((out
= fopen(newfile
, "wb")) == NULL
)
2125 LogMessage(L_ERROR
, "Unable to open config file \"%s\" - %s",
2126 newfile
, strerror(errno
));
2127 return (HTTP_SERVER_ERROR
);
2130 fchmod(fileno(out
), confinfo
.st_mode
);
2131 fchown(fileno(out
), confinfo
.st_uid
, confinfo
.st_gid
);
2134 * Copy from the request to the new config file...
2137 while ((bytes
= fread(buffer
, 1, sizeof(buffer
), in
)) > 0)
2138 if (fwrite(buffer
, 1, bytes
, out
) < bytes
)
2140 LogMessage(L_ERROR
, "Unable to copy to config file \"%s\" - %s",
2141 newfile
, strerror(errno
));
2147 return (HTTP_SERVER_ERROR
);
2151 * Close the files...
2157 LogMessage(L_ERROR
, "Error file closing config file \"%s\" - %s",
2158 newfile
, strerror(errno
));
2162 return (HTTP_SERVER_ERROR
);
2166 * Remove the request file...
2169 unlink(con
->filename
);
2170 con
->filename
[0] = '\0';
2173 * Unlink the old backup, rename the current config file to the backup
2174 * filename, and rename the new config file to the config file name...
2177 if (unlink(oldfile
))
2178 if (errno
!= ENOENT
)
2180 LogMessage(L_ERROR
, "Unable to remove backup config file \"%s\" - %s",
2181 oldfile
, strerror(errno
));
2185 return (HTTP_SERVER_ERROR
);
2188 if (rename(conffile
, oldfile
))
2189 if (errno
!= ENOENT
)
2191 LogMessage(L_ERROR
, "Unable to rename old config file \"%s\" - %s",
2192 conffile
, strerror(errno
));
2196 return (HTTP_SERVER_ERROR
);
2199 if (rename(newfile
, conffile
))
2201 LogMessage(L_ERROR
, "Unable to rename new config file \"%s\" - %s",
2202 newfile
, strerror(errno
));
2204 rename(oldfile
, conffile
);
2207 return (HTTP_SERVER_ERROR
);
2211 * If the cupsd.conf file was updated, set the NeedReload flag...
2214 if (strcmp(con
->uri
, "/admin/conf/cupsd.conf") == 0)
2218 * Return that the file was created successfully...
2221 return (HTTP_CREATED
);
2226 * 'pipe_command()' - Pipe the output of a command to the remote client.
2229 static int /* O - Process ID */
2230 pipe_command(client_t
*con
, /* I - Client connection */
2231 int infile
, /* I - Standard input for command */
2232 int *outfile
, /* O - Standard output for command */
2233 char *command
, /* I - Command to run */
2234 char *options
) /* I - Options for command */
2236 int pid
; /* Process ID */
2237 char *commptr
; /* Command string pointer */
2238 int fd
; /* Looping var */
2239 int fds
[2]; /* Pipe FDs */
2240 int argc
; /* Number of arguments */
2241 int envc
; /* Number of environment variables */
2242 char argbuf
[10240], /* Argument buffer */
2243 *argv
[100], /* Argument strings */
2244 *envp
[100]; /* Environment variables */
2245 char lang
[1024], /* LANG env variable */
2246 content_length
[1024], /* CONTENT_LENGTH env variable */
2247 content_type
[1024], /* CONTENT_TYPE env variable */
2248 ipp_port
[1024], /* Default listen port */
2249 server_port
[1024], /* Default server port */
2250 server_name
[1024], /* Default listen hostname */
2251 remote_host
[1024], /* REMOTE_HOST env variable */
2252 remote_user
[1024], /* REMOTE_USER env variable */
2253 tmpdir
[1024], /* TMPDIR environment variable */
2254 ldpath
[1024], /* LD_LIBRARY_PATH environment variable */
2255 nlspath
[1024], /* NLSPATH environment variable */
2256 datadir
[1024], /* CUPS_DATADIR environment variable */
2257 root
[1024], /* CUPS_SERVERROOT environment variable */
2258 query_string
[10240]; /* QUERY_STRING env variable */
2262 * Copy the command string...
2265 strlcpy(argbuf
, options
, sizeof(argbuf
));
2268 * Parse the string; arguments can be separated by + and are terminated
2274 for (commptr
= argbuf
, argc
= 1; *commptr
!= '\0' && argc
< 99; commptr
++)
2275 if (*commptr
== ' ' || *commptr
== '+')
2279 while (*commptr
== ' ')
2282 if (*commptr
!= '\0')
2284 argv
[argc
] = commptr
;
2290 else if (*commptr
== '%')
2292 if (commptr
[1] >= '0' && commptr
[1] <= '9')
2293 *commptr
= (commptr
[1] - '0') << 4;
2295 *commptr
= (tolower(commptr
[1]) - 'a' + 10) << 4;
2297 if (commptr
[2] >= '0' && commptr
[2] <= '9')
2298 *commptr
|= commptr
[2] - '0';
2300 *commptr
|= tolower(commptr
[2]) - 'a' + 10;
2302 strcpy(commptr
+ 1, commptr
+ 3);
2304 else if (*commptr
== '?')
2309 if (argv
[0][0] == '\0')
2310 argv
[0] = strrchr(command
, '/') + 1;
2313 * Setup the environment variables as needed...
2316 snprintf(lang
, sizeof(lang
), "LANG=%s",
2317 con
->language
? con
->language
->language
: "C");
2319 if (con
->http
.hostaddr
.addr
.sa_family
== AF_INET6
)
2321 sprintf(ipp_port
, "IPP_PORT=%d", ntohs(con
->http
.hostaddr
.ipv6
.sin6_port
));
2322 sprintf(server_port
, "SERVER_PORT=%d",
2323 ntohs(con
->http
.hostaddr
.ipv6
.sin6_port
));
2326 #endif /* AF_INET6 */
2328 sprintf(ipp_port
, "IPP_PORT=%d", ntohs(con
->http
.hostaddr
.ipv4
.sin_port
));
2329 sprintf(server_port
, "SERVER_PORT=%d",
2330 ntohs(con
->http
.hostaddr
.ipv4
.sin_port
));
2333 snprintf(server_name
, sizeof(server_name
), "SERVER_NAME=%s", ServerName
);
2334 snprintf(remote_host
, sizeof(remote_host
), "REMOTE_HOST=%s", con
->http
.hostname
);
2335 snprintf(remote_user
, sizeof(remote_user
), "REMOTE_USER=%s", con
->username
);
2336 snprintf(tmpdir
, sizeof(tmpdir
), "TMPDIR=%s", TempDir
);
2337 snprintf(datadir
, sizeof(datadir
), "CUPS_DATADIR=%s", DataDir
);
2338 snprintf(root
, sizeof(root
), "CUPS_SERVERROOT=%s", ServerRoot
);
2340 if (getenv("LD_LIBRARY_PATH") != NULL
)
2341 snprintf(ldpath
, sizeof(ldpath
), "LD_LIBRARY_PATH=%s",
2342 getenv("LD_LIBRARY_PATH"));
2343 else if (getenv("DYLD_LIBRARY_PATH") != NULL
)
2344 snprintf(ldpath
, sizeof(ldpath
), "DYLD_LIBRARY_PATH=%s",
2345 getenv("DYLD_LIBRARY_PATH"));
2349 if (getenv("NLSPATH") != NULL
)
2350 snprintf(nlspath
, sizeof(nlspath
), "NLSPATH=%s", getenv("NLSPATH"));
2354 envp
[0] = "PATH=/bin:/usr/bin";
2355 envp
[1] = "SERVER_SOFTWARE=CUPS/1.2";
2356 envp
[2] = "GATEWAY_INTERFACE=CGI/1.1";
2357 envp
[3] = "SERVER_PROTOCOL=HTTP/1.1";
2359 envp
[5] = server_name
;
2360 envp
[6] = server_port
;
2361 envp
[7] = remote_host
;
2362 envp
[8] = remote_user
;
2372 envp
[envc
++] = ldpath
;
2375 envp
[envc
++] = nlspath
;
2377 if (con
->operation
== HTTP_GET
)
2379 envp
[envc
++] = "REQUEST_METHOD=GET";
2384 * Add GET form variables after ?...
2389 snprintf(query_string
, sizeof(query_string
), "QUERY_STRING=%s", commptr
);
2390 envp
[envc
++] = query_string
;
2395 sprintf(content_length
, "CONTENT_LENGTH=%d", con
->bytes
);
2396 snprintf(content_type
, sizeof(content_type
), "CONTENT_TYPE=%s",
2397 con
->http
.fields
[HTTP_FIELD_CONTENT_TYPE
]);
2399 envp
[envc
++] = "REQUEST_METHOD=POST";
2400 envp
[envc
++] = content_length
;
2401 envp
[envc
++] = content_type
;
2405 * Tell the CGI if we are using encryption...
2408 if (con
->http
.encryption
>= HTTP_ENCRYPT_REQUIRED
)
2409 envp
[envc
++] = "HTTPS=ON";
2414 * Create a pipe for the output...
2419 LogMessage(L_ERROR
, "Unable to create pipes for CGI %s - %s",
2420 argv
[0], strerror(errno
));
2425 * Then execute the command...
2428 if ((pid
= fork()) == 0)
2431 * Child comes here... Close stdin if necessary and dup the pipe to stdout.
2437 * Running as root, so change to a non-priviledged user...
2448 * Reset group membership to just the main one we belong to.
2454 * Update stdin/stdout/stderr...
2460 if (dup(infile
) < 0)
2465 if (dup(fds
[1]) < 0)
2469 open("/dev/null", O_WRONLY
);
2472 * Close extra file descriptors...
2475 for (fd
= 3; fd
< MaxFDs
; fd
++)
2479 * Change umask to restrict permissions on created files...
2485 * Execute the pipe program; if an error occurs, exit with status 1...
2488 execve(command
, argv
, envp
);
2495 * Error - can't fork!
2498 LogMessage(L_ERROR
, "Unable to fork for CGI %s - %s", argv
[0],
2508 * Fork successful - return the PID...
2511 AddCert(pid
, con
->username
);
2513 LogMessage(L_DEBUG
, "CGI %s started - PID = %d", command
, pid
);
2524 * End of "$Id: client.c,v 1.91.2.12 2002/05/16 14:00:07 mike Exp $".