2 * "$Id: client.c,v 1.91.2.25 2002/10/16 22:13:01 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 * ShutdownClient() - Shutdown the receiving end of a connection.
36 * WriteClient() - Write data to a client as needed.
37 * check_if_modified() - Decode an "If-Modified-Since" line.
38 * decode_auth() - Decode an authorization string.
39 * get_file() - Get a filename and state info.
40 * install_conf_file() - Install a configuration file.
41 * pipe_command() - Pipe the output of a command to the remote client.
45 * Include necessary headers...
53 # include <openssl/err.h>
54 # include <openssl/ssl.h>
55 # include <openssl/rand.h>
56 #endif /* HAVE_LIBSSL */
63 static int check_if_modified(client_t
*con
,
64 struct stat
*filestats
);
65 static void decode_auth(client_t
*con
);
66 static char *get_file(client_t
*con
, struct stat
*filestats
);
67 static http_status_t
install_conf_file(client_t
*con
);
68 static int pipe_command(client_t
*con
, int infile
, int *outfile
,
69 char *command
, char *options
);
73 * 'AcceptClient()' - Accept a new client.
77 AcceptClient(listener_t
*lis
) /* I - Listener socket */
79 int i
; /* Looping var */
80 int val
; /* Parameter value */
81 client_t
*con
; /* New client pointer */
82 const struct hostent
*host
; /* Host entry for address */
83 char *hostname
;/* Hostname for address */
84 http_addr_t temp
; /* Temporary address variable */
87 LogMessage(L_DEBUG2
, "AcceptClient(%p) %d NumClients = %d",
88 lis
, lis
->fd
, NumClients
);
91 * Make sure we don't have a full set of clients already...
94 if (NumClients
== MaxClients
)
98 * Get a pointer to the next available client...
101 con
= Clients
+ NumClients
;
103 memset(con
, 0, sizeof(client_t
));
104 con
->http
.activity
= time(NULL
);
107 * Accept the client and get the remote address...
110 val
= sizeof(struct sockaddr_in
);
112 if ((con
->http
.fd
= accept(lis
->fd
, (struct sockaddr
*)&(con
->http
.hostaddr
),
115 LogMessage(L_ERROR
, "Unable to accept client connection - %s.",
121 if (lis
->address
.addr
.sa_family
== AF_INET6
)
122 con
->http
.hostaddr
.ipv6
.sin6_port
= lis
->address
.ipv6
.sin6_port
;
124 #endif /* AF_INET6 */
125 con
->http
.hostaddr
.ipv4
.sin_port
= lis
->address
.ipv4
.sin_port
;
128 hostname
= httpAddrLookup(&(con
->http
.hostaddr
), con
->http
.hostname
,
129 sizeof(con
->http
.hostname
));
133 httpAddrString(&(con
->http
.hostaddr
), con
->http
.hostname
,
134 sizeof(con
->http
.hostname
));
137 if (httpAddrLocalhost(&(con
->http
.hostaddr
)))
140 * Map accesses from the loopback interface to "localhost"...
143 strlcpy(con
->http
.hostname
, "localhost", sizeof(con
->http
.hostname
));
145 else if (httpAddrEqual(&(con
->http
.hostaddr
), &ServerAddr
))
148 * Map accesses from the same host to the server name.
151 strlcpy(con
->http
.hostname
, ServerName
, sizeof(con
->http
.hostname
));
154 if (hostname
== NULL
&& HostNameLookups
== 2)
157 * Can't have an unresolved IP address with double-lookups enabled...
160 LogMessage(L_DEBUG2
, "AcceptClient: Closing connection %d...",
164 closesocket(con
->http
.fd
);
169 LogMessage(L_WARN
, "Name lookup failed - connection from %s closed!",
174 if (HostNameLookups
== 2)
177 * Do double lookups as needed...
180 if ((host
= httpGetHostByName(con
->http
.hostname
)) != NULL
)
183 * See if the hostname maps to the same IP address...
186 if (host
->h_addrtype
!= con
->http
.hostaddr
.addr
.sa_family
)
189 * Not the right type of address...
197 * Compare all of the addresses against this one...
200 for (i
= 0; host
->h_addr_list
[i
]; i
++)
202 httpAddrLoad(host
, 0, i
, &temp
);
204 if (httpAddrEqual(&(con
->http
.hostaddr
), &temp
))
208 if (!host
->h_addr_list
[i
])
216 * Can't have a hostname that doesn't resolve to the same IP address
217 * with double-lookups enabled...
220 LogMessage(L_DEBUG2
, "AcceptClient: Closing connection %d...",
224 closesocket(con
->http
.fd
);
229 LogMessage(L_WARN
, "IP lookup failed - connection from %s closed!",
236 if (con
->http
.hostaddr
.addr
.sa_family
== AF_INET6
)
237 LogMessage(L_DEBUG
, "AcceptClient: %d from %s:%d.", con
->http
.fd
,
238 con
->http
.hostname
, ntohs(con
->http
.hostaddr
.ipv6
.sin6_port
));
240 #endif /* AF_INET6 */
241 LogMessage(L_DEBUG
, "AcceptClient: %d from %s:%d.", con
->http
.fd
,
242 con
->http
.hostname
, ntohs(con
->http
.hostaddr
.ipv4
.sin_port
));
245 * Using TCP_NODELAY improves responsiveness, especially on systems
246 * with a slow loopback interface... Since we write large buffers
247 * when sending print files and requests, there shouldn't be any
248 * performance penalty for this...
252 setsockopt(con
->http
.fd
, IPPROTO_TCP
, TCP_NODELAY
, &val
, sizeof(val
));
255 * Add the socket to the select() input mask.
258 fcntl(con
->http
.fd
, F_SETFD
, fcntl(con
->http
.fd
, F_GETFD
) | FD_CLOEXEC
);
260 LogMessage(L_DEBUG2
, "AcceptClient: Adding fd %d to InputSet...",
262 FD_SET(con
->http
.fd
, &InputSet
);
267 * Temporarily suspend accept()'s until we lose a client...
270 if (NumClients
== MaxClients
)
275 * See if we are connecting on a secure port...
278 if (lis
->encryption
== HTTP_ENCRYPT_ALWAYS
)
281 * https connection; go secure...
284 con
->http
.encryption
= HTTP_ENCRYPT_ALWAYS
;
288 #endif /* HAVE_LIBSSL */
293 * 'CloseAllClients()' - Close all remote clients immediately.
297 CloseAllClients(void)
299 while (NumClients
> 0)
300 CloseClient(Clients
);
305 * 'CloseClient()' - Close a remote client.
309 CloseClient(client_t
*con
) /* I - Client to close */
311 int status
; /* Exit status of pipe command */
313 SSL_CTX
*context
; /* Context for encryption */
314 SSL
*conn
; /* Connection for encryption */
315 unsigned long error
; /* Error code */
316 #endif /* HAVE_LIBSSL */
319 LogMessage(L_DEBUG
, "CloseClient() %d", con
->http
.fd
);
323 * Shutdown encryption as needed...
328 conn
= (SSL
*)(con
->http
.tls
);
329 context
= SSL_get_SSL_CTX(conn
);
331 switch (SSL_shutdown(conn
))
334 LogMessage(L_INFO
, "CloseClient: SSL shutdown successful!");
337 LogMessage(L_ERROR
, "CloseClient: Fatal error during SSL shutdown!");
339 while ((error
= ERR_get_error()) != 0)
340 LogMessage(L_ERROR
, "CloseClient: %s", ERR_error_string(error
, NULL
));
344 SSL_CTX_free(context
);
347 con
->http
.tls
= NULL
;
349 #endif /* HAVE_LIBSSL */
352 * Close the socket and clear the file from the input set for select()...
355 if (con
->http
.fd
>= 0)
357 LogMessage(L_DEBUG2
, "CloseClient: Removing fd %d from InputSet and OutputSet...",
360 FD_CLR(con
->http
.fd
, &InputSet
);
361 FD_CLR(con
->http
.fd
, &OutputSet
);
365 if (con
->pipe_pid
!= 0)
367 LogMessage(L_DEBUG2
, "CloseClient: Removing fd %d from InputSet...",
369 FD_CLR(con
->file
, &InputSet
);
375 * Close the open data file...
380 kill(con
->pipe_pid
, SIGKILL
);
381 waitpid(con
->pipe_pid
, &status
, WNOHANG
);
384 LogMessage(L_DEBUG2
, "CloseClient() %d Closing data file %d.",
385 con
->http
.fd
, con
->file
);
386 LogMessage(L_DEBUG2
, "CloseClient() %d Removing fd %d from InputSet.",
387 con
->http
.fd
, con
->file
);
389 FD_CLR(con
->file
, &InputSet
);
396 ippDelete(con
->request
);
402 ippDelete(con
->response
);
403 con
->response
= NULL
;
408 cupsLangFree(con
->language
);
409 con
->language
= NULL
;
413 * Re-enable new client connections if we are going back under the
417 if (NumClients
== MaxClients
)
421 * Compact the list of clients as necessary...
426 if (con
< (Clients
+ NumClients
))
427 memcpy(con
, con
+ 1, (Clients
+ NumClients
- con
) * sizeof(client_t
));
432 * 'EncryptClient()' - Enable encryption for the client...
435 int /* O - 1 on success, 0 on error */
436 EncryptClient(client_t
*con
) /* I - Client to encrypt */
439 SSL_CTX
*context
; /* Context for encryption */
440 SSL
*conn
; /* Connection for encryption */
441 unsigned long error
; /* Error code */
445 * Create the SSL context and accept the connection...
448 context
= SSL_CTX_new(SSLv23_method());
449 conn
= SSL_new(context
);
451 SSL_use_PrivateKey_file(conn
, ServerKey
, SSL_FILETYPE_PEM
);
452 SSL_use_certificate_file(conn
, ServerCertificate
, SSL_FILETYPE_PEM
);
454 SSL_set_fd(conn
, con
->http
.fd
);
455 if (SSL_accept(conn
) != 1)
457 while ((error
= ERR_get_error()) != 0)
458 LogMessage(L_ERROR
, "EncryptClient: %s", ERR_error_string(error
, NULL
));
460 SSL_CTX_free(context
);
465 con
->http
.tls
= conn
;
469 #endif /* HAVE_LIBSSL */
474 * 'ReadClient()' - Read data from a client.
477 int /* O - 1 on success, 0 on error */
478 ReadClient(client_t
*con
) /* I - Client to read from */
480 char line
[32768], /* Line from client... */
481 operation
[64], /* Operation code from socket */
482 version
[64]; /* HTTP version number string */
483 int major
, minor
; /* HTTP version numbers */
484 http_status_t status
; /* Transfer status */
485 ipp_state_t ipp_state
; /* State of IPP transfer */
486 int bytes
; /* Number of bytes to POST */
487 char *filename
; /* Name of file for GET/HEAD */
488 struct stat filestats
; /* File information */
489 mime_type_t
*type
; /* MIME type of file */
490 printer_t
*p
; /* Printer */
491 location_t
*best
; /* Best match for authentication */
492 static unsigned request_id
= 0;/* Request ID for temp files */
495 status
= HTTP_CONTINUE
;
497 switch (con
->http
.state
)
501 * See if we've received a request line...
504 if (httpGets(line
, sizeof(line
) - 1, HTTP(con
)) == NULL
)
511 * Ignore blank request lines...
518 * Clear other state variables...
521 httpClearFields(HTTP(con
));
523 con
->http
.activity
= time(NULL
);
524 con
->http
.version
= HTTP_1_0
;
525 con
->http
.keep_alive
= HTTP_KEEPALIVE_OFF
;
526 con
->http
.data_encoding
= HTTP_ENCODE_LENGTH
;
527 con
->http
.data_remaining
= 0;
528 con
->operation
= HTTP_WAITING
;
532 con
->command
[0] = '\0';
533 con
->username
[0] = '\0';
534 con
->password
[0] = '\0';
537 if (con
->language
!= NULL
)
539 cupsLangFree(con
->language
);
540 con
->language
= NULL
;
544 * Grab the request line...
547 switch (sscanf(line
, "%63s%1023s%63s", operation
, con
->uri
, version
))
550 LogMessage(L_ERROR
, "Bad request line \"%s\"!", line
);
551 SendError(con
, HTTP_BAD_REQUEST
);
555 con
->http
.version
= HTTP_0_9
;
558 if (sscanf(version
, "HTTP/%d.%d", &major
, &minor
) != 2)
560 LogMessage(L_ERROR
, "Bad request line \"%s\"!", line
);
561 SendError(con
, HTTP_BAD_REQUEST
);
568 con
->http
.version
= (http_version_t
)(major
* 100 + minor
);
569 if (con
->http
.version
== HTTP_1_1
&& KeepAlive
)
570 con
->http
.keep_alive
= HTTP_KEEPALIVE_ON
;
572 con
->http
.keep_alive
= HTTP_KEEPALIVE_OFF
;
576 SendError(con
, HTTP_NOT_SUPPORTED
);
584 * Process the request...
587 if (strcmp(operation
, "GET") == 0)
588 con
->http
.state
= HTTP_GET
;
589 else if (strcmp(operation
, "PUT") == 0)
590 con
->http
.state
= HTTP_PUT
;
591 else if (strcmp(operation
, "POST") == 0)
592 con
->http
.state
= HTTP_POST
;
593 else if (strcmp(operation
, "DELETE") == 0)
594 con
->http
.state
= HTTP_DELETE
;
595 else if (strcmp(operation
, "TRACE") == 0)
596 con
->http
.state
= HTTP_TRACE
;
597 else if (strcmp(operation
, "OPTIONS") == 0)
598 con
->http
.state
= HTTP_OPTIONS
;
599 else if (strcmp(operation
, "HEAD") == 0)
600 con
->http
.state
= HTTP_HEAD
;
603 LogMessage(L_ERROR
, "Bad operation \"%s\"!", operation
);
604 SendError(con
, HTTP_BAD_REQUEST
);
609 con
->start
= time(NULL
);
610 con
->operation
= con
->http
.state
;
612 LogMessage(L_DEBUG
, "ReadClient() %d %s %s HTTP/%d.%d", con
->http
.fd
,
614 con
->http
.version
/ 100, con
->http
.version
% 100);
616 con
->http
.status
= HTTP_OK
;
626 * Parse incoming parameters until the status changes...
629 status
= httpUpdate(HTTP(con
));
631 if (status
!= HTTP_OK
&& status
!= HTTP_CONTINUE
)
633 SendError(con
, HTTP_BAD_REQUEST
);
640 break; /* Anti-compiler-warning-code */
644 * Handle new transfers...
647 if (status
== HTTP_OK
)
649 con
->language
= cupsLangGet(con
->http
.fields
[HTTP_FIELD_ACCEPT_LANGUAGE
]);
653 if (strncmp(con
->http
.fields
[HTTP_FIELD_CONNECTION
], "Keep-Alive", 10) == 0 &&
655 con
->http
.keep_alive
= HTTP_KEEPALIVE_ON
;
657 if (con
->http
.fields
[HTTP_FIELD_HOST
][0] == '\0' &&
658 con
->http
.version
>= HTTP_1_1
)
661 * HTTP/1.1 and higher require the "Host:" field...
664 if (!SendError(con
, HTTP_BAD_REQUEST
))
670 else if (con
->operation
== HTTP_OPTIONS
)
673 * Do OPTIONS command...
676 if ((best
= FindBest(con
->uri
, con
->http
.state
)) != NULL
&&
677 best
->type
!= AUTH_NONE
)
679 if (!SendHeader(con
, HTTP_UNAUTHORIZED
, NULL
))
686 if (strcasecmp(con
->http
.fields
[HTTP_FIELD_CONNECTION
], "Upgrade") == 0 &&
687 con
->http
.tls
== NULL
)
691 * Do encryption stuff...
694 if (!SendHeader(con
, HTTP_SWITCHING_PROTOCOLS
, NULL
))
700 httpPrintf(HTTP(con
), "Connection: Upgrade\r\n");
701 httpPrintf(HTTP(con
), "Upgrade: TLS/1.0,HTTP/1.1\r\n");
702 httpPrintf(HTTP(con
), "Content-Length: 0\r\n");
703 httpPrintf(HTTP(con
), "\r\n");
707 if (!SendError(con
, HTTP_NOT_IMPLEMENTED
))
712 #endif /* HAVE_LIBSSL */
715 if (!SendHeader(con
, HTTP_OK
, NULL
))
721 httpPrintf(HTTP(con
), "Allow: GET, HEAD, OPTIONS, POST, PUT\r\n");
722 httpPrintf(HTTP(con
), "Content-Length: 0\r\n");
723 httpPrintf(HTTP(con
), "\r\n");
725 else if (strstr(con
->uri
, "..") != NULL
)
728 * Protect against malicious users!
731 if (!SendError(con
, HTTP_FORBIDDEN
))
737 else if (con
->uri
[0] != '/')
740 * Don't allow proxying (yet)...
743 if (!SendError(con
, HTTP_METHOD_NOT_ALLOWED
))
751 if (strcasecmp(con
->http
.fields
[HTTP_FIELD_CONNECTION
], "Upgrade") == 0 &&
752 con
->http
.tls
== NULL
)
756 * Do encryption stuff...
759 if (!SendHeader(con
, HTTP_SWITCHING_PROTOCOLS
, NULL
))
765 httpPrintf(HTTP(con
), "Connection: Upgrade\r\n");
766 httpPrintf(HTTP(con
), "Upgrade: TLS/1.0,HTTP/1.1\r\n");
767 httpPrintf(HTTP(con
), "Content-Length: 0\r\n");
768 httpPrintf(HTTP(con
), "\r\n");
772 status
= IsAuthorized(con
);
774 if (!SendError(con
, HTTP_NOT_IMPLEMENTED
))
779 #endif /* HAVE_LIBSSL */
782 if (status
!= HTTP_OK
)
784 SendError(con
, status
);
789 switch (con
->http
.state
)
792 if (strncmp(con
->uri
, "/printers/", 10) == 0 &&
793 strcmp(con
->uri
+ strlen(con
->uri
) - 4, ".ppd") == 0)
796 * Send PPD file - get the real printer name since printer
797 * names are not case sensitive but filenames can be...
800 con
->uri
[strlen(con
->uri
) - 4] = '\0'; /* Drop ".ppd" */
802 if ((p
= FindPrinter(con
->uri
+ 10)) != NULL
)
803 snprintf(con
->uri
, sizeof(con
->uri
), "/ppd/%s.ppd", p
->name
);
806 if (!SendError(con
, HTTP_NOT_FOUND
))
816 if ((strncmp(con
->uri
, "/admin", 6) == 0 &&
817 strncmp(con
->uri
, "/admin/conf/", 12) != 0) ||
818 strncmp(con
->uri
, "/printers", 9) == 0 ||
819 strncmp(con
->uri
, "/classes", 8) == 0 ||
820 strncmp(con
->uri
, "/jobs", 5) == 0)
826 if (strncmp(con
->uri
, "/admin", 6) == 0)
828 snprintf(con
->command
, sizeof(con
->command
),
829 "%s/cgi-bin/admin.cgi", ServerBin
);
830 con
->options
= con
->uri
+ 6;
832 else if (strncmp(con
->uri
, "/printers", 9) == 0)
834 snprintf(con
->command
, sizeof(con
->command
),
835 "%s/cgi-bin/printers.cgi", ServerBin
);
836 con
->options
= con
->uri
+ 9;
838 else if (strncmp(con
->uri
, "/classes", 8) == 0)
840 snprintf(con
->command
, sizeof(con
->command
),
841 "%s/cgi-bin/classes.cgi", ServerBin
);
842 con
->options
= con
->uri
+ 8;
846 snprintf(con
->command
, sizeof(con
->command
),
847 "%s/cgi-bin/jobs.cgi", ServerBin
);
848 con
->options
= con
->uri
+ 5;
851 if (con
->options
[0] == '/')
854 if (!SendCommand(con
, con
->command
, con
->options
))
856 if (!SendError(con
, HTTP_NOT_FOUND
))
863 LogRequest(con
, HTTP_OK
);
865 if (con
->http
.version
<= HTTP_1_0
)
866 con
->http
.keep_alive
= HTTP_KEEPALIVE_OFF
;
868 else if (strncmp(con
->uri
, "/admin/conf/", 12) == 0 &&
869 (strchr(con
->uri
+ 12, '/') != NULL
||
870 strlen(con
->uri
) == 12))
873 * GET can only be done to configuration files under
877 if (!SendError(con
, HTTP_FORBIDDEN
))
891 if ((filename
= get_file(con
, &filestats
)) == NULL
)
893 if (!SendError(con
, HTTP_NOT_FOUND
))
899 else if (!check_if_modified(con
, &filestats
))
901 if (!SendError(con
, HTTP_NOT_MODIFIED
))
909 type
= mimeFileType(MimeDatabase
, filename
);
911 strcpy(line
, "text/plain");
913 snprintf(line
, sizeof(line
), "%s/%s", type
->super
, type
->type
);
915 if (!SendFile(con
, HTTP_OK
, filename
, line
, &filestats
))
924 case HTTP_POST_RECV
:
926 * See if the POST request includes a Content-Length field, and if
927 * so check the length against any limits that are set...
930 LogMessage(L_DEBUG2
, "POST %s", con
->uri
);
931 LogMessage(L_DEBUG2
, "CONTENT_TYPE = %s", con
->http
.fields
[HTTP_FIELD_CONTENT_TYPE
]);
933 if (con
->http
.fields
[HTTP_FIELD_CONTENT_LENGTH
][0] &&
934 atoi(con
->http
.fields
[HTTP_FIELD_CONTENT_LENGTH
]) > MaxRequestSize
&&
938 * Request too large...
941 if (!SendError(con
, HTTP_REQUEST_TOO_LARGE
))
951 * See what kind of POST request this is; for IPP requests the
952 * content-type field will be "application/ipp"...
955 if (strcmp(con
->http
.fields
[HTTP_FIELD_CONTENT_TYPE
], "application/ipp") == 0)
956 con
->request
= ippNew();
957 else if ((strncmp(con
->uri
, "/admin", 6) == 0 &&
958 strncmp(con
->uri
, "/admin/conf/", 12) != 0) ||
959 strncmp(con
->uri
, "/printers", 9) == 0 ||
960 strncmp(con
->uri
, "/classes", 8) == 0 ||
961 strncmp(con
->uri
, "/jobs", 5) == 0)
967 if (strncmp(con
->uri
, "/admin", 6) == 0)
969 snprintf(con
->command
, sizeof(con
->command
),
970 "%s/cgi-bin/admin.cgi", ServerBin
);
971 con
->options
= con
->uri
+ 6;
973 else if (strncmp(con
->uri
, "/printers", 9) == 0)
975 snprintf(con
->command
, sizeof(con
->command
),
976 "%s/cgi-bin/printers.cgi", ServerBin
);
977 con
->options
= con
->uri
+ 9;
979 else if (strncmp(con
->uri
, "/classes", 8) == 0)
981 snprintf(con
->command
, sizeof(con
->command
),
982 "%s/cgi-bin/classes.cgi", ServerBin
);
983 con
->options
= con
->uri
+ 8;
987 snprintf(con
->command
, sizeof(con
->command
),
988 "%s/cgi-bin/jobs.cgi", ServerBin
);
989 con
->options
= con
->uri
+ 5;
992 if (con
->options
[0] == '/')
995 LogMessage(L_DEBUG2
, "ReadClient() %d command=\"%s\", options = \"%s\"",
996 con
->http
.fd
, con
->command
, con
->options
);
998 if (con
->http
.version
<= HTTP_1_0
)
999 con
->http
.keep_alive
= HTTP_KEEPALIVE_OFF
;
1001 else if (!SendError(con
, HTTP_UNAUTHORIZED
))
1008 case HTTP_PUT_RECV
:
1010 * Validate the resource name...
1013 if (strncmp(con
->uri
, "/admin/conf/", 12) != 0 ||
1014 strchr(con
->uri
+ 12, '/') != NULL
||
1015 strlen(con
->uri
) == 12)
1018 * PUT can only be done to configuration files under
1022 if (!SendError(con
, HTTP_FORBIDDEN
))
1032 * See if the PUT request includes a Content-Length field, and if
1033 * so check the length against any limits that are set...
1036 LogMessage(L_DEBUG2
, "PUT %s", con
->uri
);
1037 LogMessage(L_DEBUG2
, "CONTENT_TYPE = %s", con
->http
.fields
[HTTP_FIELD_CONTENT_TYPE
]);
1039 if (con
->http
.fields
[HTTP_FIELD_CONTENT_LENGTH
][0] &&
1040 atoi(con
->http
.fields
[HTTP_FIELD_CONTENT_LENGTH
]) > MaxRequestSize
&&
1044 * Request too large...
1047 if (!SendError(con
, HTTP_REQUEST_TOO_LARGE
))
1057 * Open a temporary file to hold the request...
1060 snprintf(con
->filename
, sizeof(con
->filename
), "%s/%08x",
1061 RequestRoot
, request_id
++);
1062 con
->file
= open(con
->filename
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0640);
1063 fchmod(con
->file
, 0640);
1064 fchown(con
->file
, User
, Group
);
1066 LogMessage(L_DEBUG2
, "ReadClient() %d REQUEST %s=%d", con
->http
.fd
,
1067 con
->filename
, con
->file
);
1071 if (!SendError(con
, HTTP_REQUEST_TOO_LARGE
))
1081 SendError(con
, HTTP_NOT_IMPLEMENTED
);
1082 ShutdownClient(con
);
1086 if (strncmp(con
->uri
, "/printers/", 10) == 0 &&
1087 strcmp(con
->uri
+ strlen(con
->uri
) - 4, ".ppd") == 0)
1090 * Send PPD file - get the real printer name since printer
1091 * names are not case sensitive but filenames can be...
1094 con
->uri
[strlen(con
->uri
) - 4] = '\0'; /* Drop ".ppd" */
1096 if ((p
= FindPrinter(con
->uri
+ 10)) != NULL
)
1097 snprintf(con
->uri
, sizeof(con
->uri
), "/ppd/%s.ppd", p
->name
);
1100 if (!SendError(con
, HTTP_NOT_FOUND
))
1110 if ((strncmp(con
->uri
, "/admin/", 7) == 0 &&
1111 strncmp(con
->uri
, "/admin/conf/", 12) != 0) ||
1112 strncmp(con
->uri
, "/printers/", 10) == 0 ||
1113 strncmp(con
->uri
, "/classes/", 9) == 0 ||
1114 strncmp(con
->uri
, "/jobs/", 6) == 0)
1120 if (!SendHeader(con
, HTTP_OK
, "text/html"))
1126 if (httpPrintf(HTTP(con
), "\r\n") < 0)
1132 LogRequest(con
, HTTP_OK
);
1134 else if (strncmp(con
->uri
, "/admin/conf/", 12) == 0 &&
1135 (strchr(con
->uri
+ 12, '/') != NULL
||
1136 strlen(con
->uri
) == 12))
1139 * HEAD can only be done to configuration files under
1143 if (!SendError(con
, HTTP_FORBIDDEN
))
1151 else if ((filename
= get_file(con
, &filestats
)) == NULL
)
1153 if (!SendHeader(con
, HTTP_NOT_FOUND
, "text/html"))
1159 LogRequest(con
, HTTP_NOT_FOUND
);
1161 else if (!check_if_modified(con
, &filestats
))
1163 if (!SendError(con
, HTTP_NOT_MODIFIED
))
1169 LogRequest(con
, HTTP_NOT_MODIFIED
);
1177 type
= mimeFileType(MimeDatabase
, filename
);
1179 strcpy(line
, "text/plain");
1181 snprintf(line
, sizeof(line
), "%s/%s", type
->super
, type
->type
);
1183 if (!SendHeader(con
, HTTP_OK
, line
))
1189 if (httpPrintf(HTTP(con
), "Last-Modified: %s\r\n",
1190 httpGetDateString(filestats
.st_mtime
)) < 0)
1196 if (httpPrintf(HTTP(con
), "Content-Length: %lu\r\n",
1197 (unsigned long)filestats
.st_size
) < 0)
1203 LogRequest(con
, HTTP_OK
);
1206 if (httpPrintf(HTTP(con
), "\r\n") < 0)
1212 con
->http
.state
= HTTP_WAITING
;
1216 break; /* Anti-compiler-warning-code */
1222 * Handle any incoming data...
1225 switch (con
->http
.state
)
1227 case HTTP_PUT_RECV
:
1228 LogMessage(L_DEBUG2
, "ReadClient() %d con->data_encoding = %s, con->data_remaining = %d, con->file = %d",
1230 con
->http
.data_encoding
== HTTP_ENCODE_CHUNKED
? "chunked" : "length",
1231 con
->http
.data_remaining
, con
->file
);
1233 if ((bytes
= httpRead(HTTP(con
), line
, sizeof(line
))) < 0)
1240 con
->bytes
+= bytes
;
1242 LogMessage(L_DEBUG2
, "ReadClient() %d writing %d bytes to %d",
1243 con
->http
.fd
, bytes
, con
->file
);
1245 if (write(con
->file
, line
, bytes
) < bytes
)
1247 LogMessage(L_ERROR
, "ReadClient: Unable to write %d bytes to %s: %s",
1248 bytes
, con
->filename
, strerror(errno
));
1250 LogMessage(L_DEBUG2
, "ReadClient: Closing data file %d...",
1255 unlink(con
->filename
);
1256 con
->filename
[0] = '\0';
1258 if (!SendError(con
, HTTP_REQUEST_TOO_LARGE
))
1266 if (con
->http
.state
== HTTP_WAITING
)
1269 * End of file, see how big it is...
1272 fstat(con
->file
, &filestats
);
1274 LogMessage(L_DEBUG2
, "ReadClient() %d Closing data file %d, size = %d.",
1275 con
->http
.fd
, con
->file
, (int)filestats
.st_size
);
1280 if (filestats
.st_size
> MaxRequestSize
&&
1284 * Request is too big; remove it and send an error...
1287 LogMessage(L_DEBUG2
, "ReadClient() %d Removing temp file %s",
1288 con
->http
.fd
, con
->filename
);
1289 unlink(con
->filename
);
1290 con
->filename
[0] = '\0';
1292 if (!SendError(con
, HTTP_REQUEST_TOO_LARGE
))
1300 * Install the configuration file...
1303 status
= install_conf_file(con
);
1306 * Return the status to the client...
1309 if (!SendError(con
, status
))
1317 case HTTP_POST_RECV
:
1318 LogMessage(L_DEBUG2
, "ReadClient() %d con->data_encoding = %s, con->data_remaining = %d, con->file = %d",
1320 con
->http
.data_encoding
== HTTP_ENCODE_CHUNKED
? "chunked" : "length",
1321 con
->http
.data_remaining
, con
->file
);
1323 if (con
->request
!= NULL
)
1326 * Grab any request data from the connection...
1329 if ((ipp_state
= ippRead(&(con
->http
), con
->request
)) == IPP_ERROR
)
1331 LogMessage(L_ERROR
, "ReadClient() %d IPP Read Error!",
1336 else if (ipp_state
!= IPP_DATA
)
1339 con
->bytes
+= ippLength(con
->request
);
1342 if (con
->file
== 0 && con
->http
.state
!= HTTP_POST_SEND
)
1345 * Create a file as needed for the request data...
1348 snprintf(con
->filename
, sizeof(con
->filename
), "%s/%08x",
1349 RequestRoot
, request_id
++);
1350 con
->file
= open(con
->filename
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0640);
1351 fchmod(con
->file
, 0640);
1352 fchown(con
->file
, User
, Group
);
1354 LogMessage(L_DEBUG2
, "ReadClient() %d REQUEST %s=%d", con
->http
.fd
,
1355 con
->filename
, con
->file
);
1359 if (!SendError(con
, HTTP_REQUEST_TOO_LARGE
))
1367 if (con
->http
.state
!= HTTP_POST_SEND
)
1369 if ((bytes
= httpRead(HTTP(con
), line
, sizeof(line
))) < 0)
1376 con
->bytes
+= bytes
;
1378 LogMessage(L_DEBUG2
, "ReadClient() %d writing %d bytes to %d",
1379 con
->http
.fd
, bytes
, con
->file
);
1381 if (write(con
->file
, line
, bytes
) < bytes
)
1383 LogMessage(L_ERROR
, "ReadClient: Unable to write %d bytes to %s: %s",
1384 bytes
, con
->filename
, strerror(errno
));
1386 LogMessage(L_DEBUG2
, "ReadClient: Closing file %d...",
1391 unlink(con
->filename
);
1392 con
->filename
[0] = '\0';
1394 if (!SendError(con
, HTTP_REQUEST_TOO_LARGE
))
1401 else if (con
->http
.state
!= HTTP_POST_SEND
)
1408 if (con
->http
.state
== HTTP_POST_SEND
)
1412 fstat(con
->file
, &filestats
);
1414 LogMessage(L_DEBUG2
, "ReadClient() %d Closing data file %d, size = %d.",
1415 con
->http
.fd
, con
->file
, (int)filestats
.st_size
);
1420 if (filestats
.st_size
> MaxRequestSize
&&
1424 * Request is too big; remove it and send an error...
1427 LogMessage(L_DEBUG2
, "ReadClient() %d Removing temp file %s",
1428 con
->http
.fd
, con
->filename
);
1429 unlink(con
->filename
);
1430 con
->filename
[0] = '\0';
1435 * Delete any IPP request data...
1438 ippDelete(con
->request
);
1439 con
->request
= NULL
;
1442 if (!SendError(con
, HTTP_REQUEST_TOO_LARGE
))
1449 if (con
->command
[0])
1451 if (!SendCommand(con
, con
->command
, con
->options
))
1453 if (!SendError(con
, HTTP_NOT_FOUND
))
1460 LogRequest(con
, HTTP_OK
);
1465 ProcessIPPRequest(con
);
1470 break; /* Anti-compiler-warning-code */
1473 if (!con
->http
.keep_alive
&& con
->http
.state
== HTTP_WAITING
)
1484 * 'SendCommand()' - Send output from a command via HTTP.
1488 SendCommand(client_t
*con
,
1495 if (con
->filename
[0])
1496 fd
= open(con
->filename
, O_RDONLY
);
1498 fd
= open("/dev/null", O_RDONLY
);
1500 con
->pipe_pid
= pipe_command(con
, fd
, &(con
->file
), command
, options
);
1504 LogMessage(L_INFO
, "Started \"%s\" (pid=%d)", command
, con
->pipe_pid
);
1506 LogMessage(L_DEBUG
, "SendCommand() %d file=%d", con
->http
.fd
, con
->file
);
1508 if (con
->pipe_pid
== 0)
1511 fcntl(con
->file
, F_SETFD
, fcntl(con
->file
, F_GETFD
) | FD_CLOEXEC
);
1513 LogMessage(L_DEBUG2
, "SendCommand: Adding fd %d to InputSet...", con
->file
);
1514 LogMessage(L_DEBUG2
, "SendCommand: Adding fd %d to OutputSet...",
1517 FD_SET(con
->file
, &InputSet
);
1518 FD_SET(con
->http
.fd
, &OutputSet
);
1520 if (!SendHeader(con
, HTTP_OK
, NULL
))
1523 if (con
->http
.version
== HTTP_1_1
)
1525 con
->http
.data_encoding
= HTTP_ENCODE_CHUNKED
;
1527 if (httpPrintf(HTTP(con
), "Transfer-Encoding: chunked\r\n") < 0)
1531 con
->got_fields
= 0;
1539 * 'SendError()' - Send an error message via HTTP.
1542 int /* O - 1 if successful, 0 otherwise */
1543 SendError(client_t
*con
, /* I - Connection */
1544 http_status_t code
) /* I - Error code */
1546 char message
[1024]; /* Message for user */
1550 * Put the request in the access_log file...
1553 if (con
->operation
> HTTP_WAITING
)
1554 LogRequest(con
, code
);
1556 LogMessage(L_DEBUG
, "SendError() %d code=%d", con
->http
.fd
, code
);
1559 * To work around bugs in some proxies, don't use Keep-Alive for some
1563 if (code
>= HTTP_BAD_REQUEST
)
1564 con
->http
.keep_alive
= HTTP_KEEPALIVE_OFF
;
1567 * Send an error message back to the client. If the error code is a
1568 * 400 or 500 series, make sure the message contains some text, too!
1571 if (!SendHeader(con
, code
, NULL
))
1575 if (code
== HTTP_UPGRADE_REQUIRED
)
1576 if (httpPrintf(HTTP(con
), "Connection: Upgrade\r\n") < 0)
1579 if (httpPrintf(HTTP(con
), "Upgrade: TLS/1.0,HTTP/1.1\r\n") < 0)
1581 #endif /* HAVE_LIBSSL */
1583 if (con
->http
.version
>= HTTP_1_1
&& !con
->http
.keep_alive
)
1585 if (httpPrintf(HTTP(con
), "Connection: close\r\n") < 0)
1589 if (code
>= HTTP_BAD_REQUEST
)
1592 * Send a human-readable error message.
1595 snprintf(message
, sizeof(message
),
1596 "<HTML><HEAD><TITLE>%d %s</TITLE></HEAD>"
1597 "<BODY><H1>%s</H1>%s</BODY></HTML>\n",
1598 code
, httpStatus(code
), httpStatus(code
),
1599 con
->language
? con
->language
->messages
[code
] :
1602 if (httpPrintf(HTTP(con
), "Content-Type: text/html\r\n") < 0)
1604 if (httpPrintf(HTTP(con
), "Content-Length: %d\r\n",
1605 (int)strlen(message
)) < 0)
1607 if (httpPrintf(HTTP(con
), "\r\n") < 0)
1609 if (httpPrintf(HTTP(con
), "%s", message
) < 0)
1612 else if (httpPrintf(HTTP(con
), "\r\n") < 0)
1615 con
->http
.state
= HTTP_WAITING
;
1622 * 'SendFile()' - Send a file via HTTP.
1626 SendFile(client_t
*con
,
1630 struct stat
*filestats
)
1632 con
->file
= open(filename
, O_RDONLY
);
1634 LogMessage(L_DEBUG
, "SendFile() %d file=%d", con
->http
.fd
, con
->file
);
1639 fcntl(con
->file
, F_SETFD
, fcntl(con
->file
, F_GETFD
) | FD_CLOEXEC
);
1643 if (!SendHeader(con
, code
, type
))
1646 if (httpPrintf(HTTP(con
), "Last-Modified: %s\r\n", httpGetDateString(filestats
->st_mtime
)) < 0)
1648 if (httpPrintf(HTTP(con
), "Content-Length: %lu\r\n",
1649 (unsigned long)filestats
->st_size
) < 0)
1651 if (httpPrintf(HTTP(con
), "\r\n") < 0)
1654 LogMessage(L_DEBUG2
, "SendFile: Adding fd %d to OutputSet...", con
->http
.fd
);
1656 FD_SET(con
->http
.fd
, &OutputSet
);
1663 * 'SendHeader()' - Send an HTTP request.
1666 int /* O - 1 on success, 0 on failure */
1667 SendHeader(client_t
*con
, /* I - Client to send to */
1668 http_status_t code
, /* I - HTTP status code */
1669 char *type
) /* I - MIME type of document */
1671 location_t
*loc
; /* Authentication location */
1674 if (httpPrintf(HTTP(con
), "HTTP/%d.%d %d %s\r\n", con
->http
.version
/ 100,
1675 con
->http
.version
% 100, code
, httpStatus(code
)) < 0)
1677 if (httpPrintf(HTTP(con
), "Date: %s\r\n", httpGetDateString(time(NULL
))) < 0)
1679 if (httpPrintf(HTTP(con
), "Server: CUPS/1.1\r\n") < 0)
1681 if (con
->http
.keep_alive
&& con
->http
.version
>= HTTP_1_0
)
1683 if (httpPrintf(HTTP(con
), "Connection: Keep-Alive\r\n") < 0)
1685 if (httpPrintf(HTTP(con
), "Keep-Alive: timeout=%d\r\n", KeepAliveTimeout
) < 0)
1688 if (code
== HTTP_METHOD_NOT_ALLOWED
)
1689 if (httpPrintf(HTTP(con
), "Allow: GET, HEAD, OPTIONS, POST\r\n") < 0)
1692 if (code
== HTTP_UNAUTHORIZED
)
1695 * This already succeeded in IsAuthorized...
1698 loc
= FindBest(con
->uri
, con
->http
.state
);
1700 if (loc
->type
!= AUTH_DIGEST
)
1702 if (httpPrintf(HTTP(con
), "WWW-Authenticate: Basic realm=\"CUPS\"\r\n") < 0)
1707 if (httpPrintf(HTTP(con
), "WWW-Authenticate: Digest realm=\"CUPS\" "
1708 "nonce=\"%s\"\r\n", con
->http
.hostname
) < 0)
1712 if (con
->language
!= NULL
)
1714 if (httpPrintf(HTTP(con
), "Content-Language: %s\r\n",
1715 con
->language
->language
) < 0)
1719 if (httpPrintf(HTTP(con
), "Content-Type: %s; charset=%s\r\n", type
,
1720 cupsLangEncoding(con
->language
)) < 0)
1723 else if (type
!= NULL
)
1724 if (httpPrintf(HTTP(con
), "Content-Type: %s\r\n", type
) < 0)
1732 * 'ShutdownClient()' - Shutdown the receiving end of a connection.
1736 ShutdownClient(client_t
*con
) /* I - Client connection */
1739 * Shutdown the receiving end of the socket, since the client
1740 * still needs to read the error message...
1743 shutdown(con
->http
.fd
, 0);
1746 LogMessage(L_DEBUG2
, "ShutdownClient: Removing fd %d from InputSet...",
1749 FD_CLR(con
->http
.fd
, &InputSet
);
1754 * 'WriteClient()' - Write data to a client as needed.
1757 int /* O - 1 if success, 0 if fail */
1758 WriteClient(client_t
*con
) /* I - Client connection */
1760 int bytes
; /* Number of bytes written */
1761 char buf
[HTTP_MAX_BUFFER
+ 1];/* Data buffer */
1762 char *bufptr
; /* Pointer into buffer */
1763 ipp_state_t ipp_state
; /* IPP state value */
1766 if (con
->http
.state
!= HTTP_GET_SEND
&&
1767 con
->http
.state
!= HTTP_POST_SEND
)
1770 if (con
->response
!= NULL
)
1772 ipp_state
= ippWrite(&(con
->http
), con
->response
);
1773 bytes
= ipp_state
!= IPP_ERROR
&& ipp_state
!= IPP_DATA
;
1775 else if ((bytes
= read(con
->file
, buf
, HTTP_MAX_BUFFER
)) > 0)
1777 if (con
->pipe_pid
&& !con
->got_fields
)
1780 * Inspect the data for Content-Type and other fields.
1785 for (bufptr
= buf
; !con
->got_fields
&& *bufptr
; bufptr
++)
1786 if (*bufptr
== '\n')
1789 * Send line to client...
1792 if (bufptr
> buf
&& bufptr
[-1] == '\r')
1796 httpPrintf(HTTP(con
), "%s\r\n", buf
);
1797 LogMessage(L_DEBUG2
, "WriteClient() %d %s", con
->http
.fd
, buf
);
1803 bytes
-= (bufptr
- buf
);
1804 memcpy(buf
, bufptr
, bytes
+ 1);
1808 * See if the line was empty...
1811 if (con
->field_col
== 0)
1812 con
->got_fields
= 1;
1816 else if (*bufptr
!= '\r')
1819 if (bytes
> 0 && !con
->got_fields
)
1822 * Remaining text needs to go out...
1825 httpPrintf(HTTP(con
), "%s", buf
);
1827 con
->http
.activity
= time(NULL
);
1830 else if (bytes
== 0)
1832 con
->http
.activity
= time(NULL
);
1837 if (httpWrite(HTTP(con
), buf
, bytes
) < 0)
1843 con
->bytes
+= bytes
;
1848 LogRequest(con
, HTTP_OK
);
1850 if (con
->http
.data_encoding
== HTTP_ENCODE_CHUNKED
)
1852 if (httpPrintf(HTTP(con
), "0\r\n\r\n") < 0)
1859 con
->http
.state
= HTTP_WAITING
;
1861 LogMessage(L_DEBUG2
, "WriteClient() Removing fd %d from OutputSet...",
1864 FD_CLR(con
->http
.fd
, &OutputSet
);
1868 LogMessage(L_DEBUG2
, "WriteClient() Removing fd %d from InputSet...",
1870 FD_CLR(con
->file
, &InputSet
);
1873 kill(con
->pipe_pid
, SIGTERM
);
1875 LogMessage(L_DEBUG2
, "WriteClient() %d Closing data file %d.",
1876 con
->http
.fd
, con
->file
);
1883 if (con
->filename
[0])
1885 LogMessage(L_DEBUG2
, "WriteClient() %d Removing temp file %s",
1886 con
->http
.fd
, con
->filename
);
1887 unlink(con
->filename
);
1888 con
->filename
[0] = '\0';
1891 if (con
->request
!= NULL
)
1893 ippDelete(con
->request
);
1894 con
->request
= NULL
;
1897 if (con
->response
!= NULL
)
1899 ippDelete(con
->response
);
1900 con
->response
= NULL
;
1903 if (!con
->http
.keep_alive
)
1911 LogMessage(L_DEBUG2
, "WriteClient() %d %d bytes", con
->http
.fd
, bytes
);
1913 con
->http
.activity
= time(NULL
);
1920 * 'check_if_modified()' - Decode an "If-Modified-Since" line.
1923 static int /* O - 1 if modified since */
1924 check_if_modified(client_t
*con
, /* I - Client connection */
1925 struct stat
*filestats
) /* I - File information */
1927 char *ptr
; /* Pointer into field */
1928 time_t date
; /* Time/date value */
1929 int size
; /* Size/length value */
1934 ptr
= con
->http
.fields
[HTTP_FIELD_IF_MODIFIED_SINCE
];
1939 LogMessage(L_DEBUG2
, "check_if_modified() %d If-Modified-Since=\"%s\"",
1942 while (*ptr
!= '\0')
1944 while (isspace(*ptr
) || *ptr
== ';')
1947 if (strncasecmp(ptr
, "length=", 7) == 0)
1952 while (isdigit(*ptr
))
1955 else if (isalpha(*ptr
))
1957 date
= httpGetDateTime(ptr
);
1958 while (*ptr
!= '\0' && *ptr
!= ';')
1963 LogMessage(L_DEBUG2
, "check_if_modified() %d sizes=%d,%d dates=%d,%d",
1964 con
->http
.fd
, size
, (int)filestats
->st_size
, (int)date
,
1965 (int)filestats
->st_mtime
);
1967 return ((size
!= filestats
->st_size
&& size
!= 0) ||
1968 (date
< filestats
->st_mtime
&& date
!= 0) ||
1969 (size
== 0 && date
== 0));
1974 * 'decode_auth()' - Decode an authorization string.
1978 decode_auth(client_t
*con
) /* I - Client to decode to */
1980 char *s
, /* Authorization string */
1981 value
[1024]; /* Value string */
1982 const char *username
; /* Certificate username */
1986 * Decode the string...
1989 s
= con
->http
.fields
[HTTP_FIELD_AUTHORIZATION
];
1991 LogMessage(L_DEBUG2
, "decode_auth(%p): Authorization string = \"%s\"",
1994 if (strncmp(s
, "Basic", 5) == 0)
2000 httpDecode64(value
, s
);
2003 * Pull the username and password out...
2006 if ((s
= strchr(value
, ':')) == NULL
)
2008 LogMessage(L_DEBUG
, "decode_auth() %d no colon in auth string \"%s\"",
2009 con
->http
.fd
, value
);
2015 strlcpy(con
->username
, value
, sizeof(con
->username
));
2016 strlcpy(con
->password
, s
, sizeof(con
->password
));
2018 else if (strncmp(s
, "Local", 5) == 0)
2024 if ((username
= FindCert(s
)) != NULL
)
2025 strlcpy(con
->username
, username
, sizeof(con
->username
));
2027 else if (strncmp(s
, "Digest", 5) == 0)
2030 * Get the username and password from the Digest attributes...
2033 if (httpGetSubField(&(con
->http
), HTTP_FIELD_WWW_AUTHENTICATE
, "username",
2035 strlcpy(con
->username
, value
, sizeof(con
->username
));
2037 if (httpGetSubField(&(con
->http
), HTTP_FIELD_WWW_AUTHENTICATE
, "response",
2039 strlcpy(con
->password
, value
, sizeof(con
->password
) - 1);
2042 LogMessage(L_DEBUG2
, "decode_auth() %d username=\"%s\"",
2043 con
->http
.fd
, con
->username
);
2048 * 'get_file()' - Get a filename and state info.
2051 static char * /* O - Real filename */
2052 get_file(client_t
*con
, /* I - Client connection */
2053 struct stat
*filestats
)/* O - File information */
2055 int status
; /* Status of filesystem calls */
2056 char *params
; /* Pointer to parameters in URI */
2057 static char filename
[1024]; /* Filename buffer */
2061 * Need to add DocumentRoot global...
2064 if (strncmp(con
->uri
, "/ppd/", 5) == 0)
2065 snprintf(filename
, sizeof(filename
), "%s%s", ServerRoot
, con
->uri
);
2066 else if (strncmp(con
->uri
, "/admin/conf/", 12) == 0)
2067 snprintf(filename
, sizeof(filename
), "%s%s", ServerRoot
, con
->uri
+ 11);
2068 else if (con
->language
!= NULL
)
2069 snprintf(filename
, sizeof(filename
), "%s/%s%s", DocumentRoot
, con
->language
->language
,
2072 snprintf(filename
, sizeof(filename
), "%s%s", DocumentRoot
, con
->uri
);
2074 if ((params
= strchr(filename
, '?')) != NULL
)
2078 * Grab the status for this language; if there isn't a language-specific file
2079 * then fallback to the default one...
2082 if ((status
= stat(filename
, filestats
)) != 0 && con
->language
!= NULL
)
2085 * Drop the language prefix and try the current directory...
2088 if (strncmp(con
->uri
, "/ppd/", 5) != 0 &&
2089 strncmp(con
->uri
, "/admin/conf/", 12) != 0)
2091 snprintf(filename
, sizeof(filename
), "%s%s", DocumentRoot
, con
->uri
);
2093 status
= stat(filename
, filestats
);
2098 * If we're found a directory, get the index.html file instead...
2101 if (!status
&& S_ISDIR(filestats
->st_mode
))
2103 if (filename
[strlen(filename
) - 1] == '/')
2104 strlcat(filename
, "index.html", sizeof(filename
));
2106 strlcat(filename
, "/index.html", sizeof(filename
));
2108 status
= stat(filename
, filestats
);
2111 LogMessage(L_DEBUG2
, "get_file() %d filename=%s size=%d",
2112 con
->http
.fd
, filename
, status
? -1 : (int)filestats
->st_size
);
2122 * 'install_conf_file()' - Install a configuration file.
2125 static http_status_t
/* O - Status */
2126 install_conf_file(client_t
*con
) /* I - Connection */
2128 FILE *in
, /* Input file */
2129 *out
; /* Output file */
2130 char buffer
[1024]; /* Copy buffer */
2131 int bytes
; /* Number of bytes */
2132 char conffile
[1024], /* Configuration filename */
2133 newfile
[1024], /* New config filename */
2134 oldfile
[1024]; /* Old config filename */
2135 struct stat confinfo
; /* Config file info */
2139 * First construct the filenames...
2142 snprintf(conffile
, sizeof(conffile
), "%s%s", ServerRoot
, con
->uri
+ 11);
2143 snprintf(newfile
, sizeof(newfile
), "%s%s.N", ServerRoot
, con
->uri
+ 11);
2144 snprintf(oldfile
, sizeof(oldfile
), "%s%s.O", ServerRoot
, con
->uri
+ 11);
2146 LogMessage(L_INFO
, "Installing config file \"%s\"...", conffile
);
2149 * Get the owner, group, and permissions of the configuration file.
2150 * If it doesn't exist, assign it to the User and Group in the
2151 * cupsd.conf file with mode 0640 permissions.
2154 if (stat(conffile
, &confinfo
))
2156 confinfo
.st_uid
= User
;
2157 confinfo
.st_gid
= Group
;
2158 confinfo
.st_mode
= 0640;
2162 * Open the request file and new config file...
2165 if ((in
= fopen(con
->filename
, "rb")) == NULL
)
2167 LogMessage(L_ERROR
, "Unable to open request file \"%s\" - %s",
2168 con
->filename
, strerror(errno
));
2169 return (HTTP_SERVER_ERROR
);
2172 if ((out
= fopen(newfile
, "wb")) == NULL
)
2175 LogMessage(L_ERROR
, "Unable to open config file \"%s\" - %s",
2176 newfile
, strerror(errno
));
2177 return (HTTP_SERVER_ERROR
);
2180 fchmod(fileno(out
), confinfo
.st_mode
);
2181 fchown(fileno(out
), confinfo
.st_uid
, confinfo
.st_gid
);
2184 * Copy from the request to the new config file...
2187 while ((bytes
= fread(buffer
, 1, sizeof(buffer
), in
)) > 0)
2188 if (fwrite(buffer
, 1, bytes
, out
) < bytes
)
2190 LogMessage(L_ERROR
, "Unable to copy to config file \"%s\" - %s",
2191 newfile
, strerror(errno
));
2197 return (HTTP_SERVER_ERROR
);
2201 * Close the files...
2207 LogMessage(L_ERROR
, "Error file closing config file \"%s\" - %s",
2208 newfile
, strerror(errno
));
2212 return (HTTP_SERVER_ERROR
);
2216 * Remove the request file...
2219 unlink(con
->filename
);
2220 con
->filename
[0] = '\0';
2223 * Unlink the old backup, rename the current config file to the backup
2224 * filename, and rename the new config file to the config file name...
2227 if (unlink(oldfile
))
2228 if (errno
!= ENOENT
)
2230 LogMessage(L_ERROR
, "Unable to remove backup config file \"%s\" - %s",
2231 oldfile
, strerror(errno
));
2235 return (HTTP_SERVER_ERROR
);
2238 if (rename(conffile
, oldfile
))
2239 if (errno
!= ENOENT
)
2241 LogMessage(L_ERROR
, "Unable to rename old config file \"%s\" - %s",
2242 conffile
, strerror(errno
));
2246 return (HTTP_SERVER_ERROR
);
2249 if (rename(newfile
, conffile
))
2251 LogMessage(L_ERROR
, "Unable to rename new config file \"%s\" - %s",
2252 newfile
, strerror(errno
));
2254 rename(oldfile
, conffile
);
2257 return (HTTP_SERVER_ERROR
);
2261 * If the cupsd.conf file was updated, set the NeedReload flag...
2264 if (strcmp(con
->uri
, "/admin/conf/cupsd.conf") == 0)
2268 * Return that the file was created successfully...
2271 return (HTTP_CREATED
);
2276 * 'pipe_command()' - Pipe the output of a command to the remote client.
2279 static int /* O - Process ID */
2280 pipe_command(client_t
*con
, /* I - Client connection */
2281 int infile
, /* I - Standard input for command */
2282 int *outfile
, /* O - Standard output for command */
2283 char *command
, /* I - Command to run */
2284 char *options
) /* I - Options for command */
2286 int pid
; /* Process ID */
2287 char *commptr
; /* Command string pointer */
2288 int fd
; /* Looping var */
2289 int fds
[2]; /* Pipe FDs */
2290 int argc
; /* Number of arguments */
2291 int envc
; /* Number of environment variables */
2292 char argbuf
[10240], /* Argument buffer */
2293 *argv
[100], /* Argument strings */
2294 *envp
[100]; /* Environment variables */
2295 char lang
[1024], /* LANG env variable */
2296 content_length
[1024], /* CONTENT_LENGTH env variable */
2297 content_type
[1024], /* CONTENT_TYPE env variable */
2298 ipp_port
[1024], /* Default listen port */
2299 server_port
[1024], /* Default server port */
2300 server_name
[1024], /* Default listen hostname */
2301 remote_host
[1024], /* REMOTE_HOST env variable */
2302 remote_user
[1024], /* REMOTE_USER env variable */
2303 tmpdir
[1024], /* TMPDIR environment variable */
2304 ldpath
[1024], /* LD_LIBRARY_PATH environment variable */
2305 nlspath
[1024], /* NLSPATH environment variable */
2306 datadir
[1024], /* CUPS_DATADIR environment variable */
2307 root
[1024], /* CUPS_SERVERROOT environment variable */
2308 query_string
[10240]; /* QUERY_STRING env variable */
2309 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
2310 sigset_t oldmask
, /* POSIX signal masks */
2312 struct sigaction action
; /* POSIX signal handler */
2313 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
2317 * Copy the command string...
2320 strlcpy(argbuf
, options
, sizeof(argbuf
));
2323 * Parse the string; arguments can be separated by + and are terminated
2329 for (commptr
= argbuf
, argc
= 1; *commptr
!= '\0' && argc
< 99; commptr
++)
2330 if (*commptr
== ' ' || *commptr
== '+')
2334 while (*commptr
== ' ')
2337 if (*commptr
!= '\0')
2339 argv
[argc
] = commptr
;
2345 else if (*commptr
== '%')
2347 if (commptr
[1] >= '0' && commptr
[1] <= '9')
2348 *commptr
= (commptr
[1] - '0') << 4;
2350 *commptr
= (tolower(commptr
[1]) - 'a' + 10) << 4;
2352 if (commptr
[2] >= '0' && commptr
[2] <= '9')
2353 *commptr
|= commptr
[2] - '0';
2355 *commptr
|= tolower(commptr
[2]) - 'a' + 10;
2357 strcpy(commptr
+ 1, commptr
+ 3);
2359 else if (*commptr
== '?')
2364 if (argv
[0][0] == '\0')
2365 argv
[0] = strrchr(command
, '/') + 1;
2368 * Setup the environment variables as needed...
2371 snprintf(lang
, sizeof(lang
), "LANG=%s",
2372 con
->language
? con
->language
->language
: "C");
2374 if (con
->http
.hostaddr
.addr
.sa_family
== AF_INET6
)
2376 sprintf(ipp_port
, "IPP_PORT=%d", ntohs(con
->http
.hostaddr
.ipv6
.sin6_port
));
2377 sprintf(server_port
, "SERVER_PORT=%d",
2378 ntohs(con
->http
.hostaddr
.ipv6
.sin6_port
));
2381 #endif /* AF_INET6 */
2383 sprintf(ipp_port
, "IPP_PORT=%d", ntohs(con
->http
.hostaddr
.ipv4
.sin_port
));
2384 sprintf(server_port
, "SERVER_PORT=%d",
2385 ntohs(con
->http
.hostaddr
.ipv4
.sin_port
));
2388 if (strcmp(con
->http
.hostname
, "localhost") == 0)
2389 strlcpy(server_name
, "SERVER_NAME=localhost", sizeof(server_name
));
2391 snprintf(server_name
, sizeof(server_name
), "SERVER_NAME=%s", ServerName
);
2392 snprintf(remote_host
, sizeof(remote_host
), "REMOTE_HOST=%s", con
->http
.hostname
);
2393 snprintf(remote_user
, sizeof(remote_user
), "REMOTE_USER=%s", con
->username
);
2394 snprintf(tmpdir
, sizeof(tmpdir
), "TMPDIR=%s", TempDir
);
2395 snprintf(datadir
, sizeof(datadir
), "CUPS_DATADIR=%s", DataDir
);
2396 snprintf(root
, sizeof(root
), "CUPS_SERVERROOT=%s", ServerRoot
);
2398 if (getenv("LD_LIBRARY_PATH") != NULL
)
2399 snprintf(ldpath
, sizeof(ldpath
), "LD_LIBRARY_PATH=%s",
2400 getenv("LD_LIBRARY_PATH"));
2401 else if (getenv("DYLD_LIBRARY_PATH") != NULL
)
2402 snprintf(ldpath
, sizeof(ldpath
), "DYLD_LIBRARY_PATH=%s",
2403 getenv("DYLD_LIBRARY_PATH"));
2404 else if (getenv("SHLIB_PATH") != NULL
)
2405 snprintf(ldpath
, sizeof(ldpath
), "SHLIB_PATH=%s",
2406 getenv("SHLIB_PATH"));
2410 if (getenv("NLSPATH") != NULL
)
2411 snprintf(nlspath
, sizeof(nlspath
), "NLSPATH=%s", getenv("NLSPATH"));
2415 envp
[0] = "PATH=/bin:/usr/bin";
2416 envp
[1] = "SERVER_SOFTWARE=CUPS/1.2";
2417 envp
[2] = "GATEWAY_INTERFACE=CGI/1.1";
2418 envp
[3] = "SERVER_PROTOCOL=HTTP/1.1";
2420 envp
[5] = server_name
;
2421 envp
[6] = server_port
;
2422 envp
[7] = remote_host
;
2423 envp
[8] = remote_user
;
2433 envp
[envc
++] = ldpath
;
2436 envp
[envc
++] = nlspath
;
2438 if (con
->operation
== HTTP_GET
)
2440 envp
[envc
++] = "REQUEST_METHOD=GET";
2445 * Add GET form variables after ?...
2450 snprintf(query_string
, sizeof(query_string
), "QUERY_STRING=%s", commptr
);
2451 envp
[envc
++] = query_string
;
2456 sprintf(content_length
, "CONTENT_LENGTH=%d", con
->bytes
);
2457 snprintf(content_type
, sizeof(content_type
), "CONTENT_TYPE=%s",
2458 con
->http
.fields
[HTTP_FIELD_CONTENT_TYPE
]);
2460 envp
[envc
++] = "REQUEST_METHOD=POST";
2461 envp
[envc
++] = content_length
;
2462 envp
[envc
++] = content_type
;
2466 * Tell the CGI if we are using encryption...
2469 if (con
->http
.encryption
== HTTP_ENCRYPT_ALWAYS
)
2471 envp
[envc
++] = "HTTPS=ON";
2472 envp
[envc
++] = "CUPS_ENCRYPTION=Always";
2478 * Create a pipe for the output...
2483 LogMessage(L_ERROR
, "Unable to create pipes for CGI %s - %s",
2484 argv
[0], strerror(errno
));
2489 * Block signals before forking...
2495 #elif defined(HAVE_SIGACTION)
2496 sigemptyset(&newmask
);
2497 sigaddset(&newmask
, SIGTERM
);
2498 sigaddset(&newmask
, SIGCHLD
);
2499 sigprocmask(SIG_BLOCK
, &newmask
, &oldmask
);
2500 #endif /* HAVE_SIGSET */
2503 * Then execute the command...
2506 if ((pid
= fork()) == 0)
2509 * Child comes here... Close stdin if necessary and dup the pipe to stdout.
2515 * Running as root, so change to a non-priviledged user...
2526 * Reset group membership to just the main one we belong to.
2532 * Update stdin/stdout/stderr...
2538 if (dup(infile
) < 0)
2543 if (dup(fds
[1]) < 0)
2547 open("/dev/null", O_WRONLY
);
2550 * Close extra file descriptors...
2553 for (fd
= 3; fd
< MaxFDs
; fd
++)
2557 * Change umask to restrict permissions on created files...
2563 * Unblock signals before doing the exec...
2567 sigset(SIGTERM
, SIG_DFL
);
2568 sigset(SIGCHLD
, SIG_DFL
);
2572 #elif defined(HAVE_SIGACTION)
2573 memset(&action
, 0, sizeof(action
));
2575 sigemptyset(&action
.sa_mask
);
2576 action
.sa_handler
= SIG_DFL
;
2578 sigaction(SIGTERM
, &action
, NULL
);
2579 sigaction(SIGCHLD
, &action
, NULL
);
2581 sigprocmask(SIG_SETMASK
, &oldmask
, NULL
);
2583 signal(SIGTERM
, SIG_DFL
);
2584 signal(SIGCHLD
, SIG_DFL
);
2585 #endif /* HAVE_SIGSET */
2589 * Execute the pipe program; if an error occurs, exit with status 1...
2592 execve(command
, argv
, envp
);
2599 * Error - can't fork!
2602 LogMessage(L_ERROR
, "Unable to fork for CGI %s - %s", argv
[0],
2612 * Fork successful - return the PID...
2615 AddCert(pid
, con
->username
);
2617 LogMessage(L_DEBUG
, "CGI %s started - PID = %d", command
, pid
);
2626 #elif defined(HAVE_SIGACTION)
2627 sigprocmask(SIG_SETMASK
, &oldmask
, NULL
);
2628 #endif /* HAVE_SIGSET */
2635 * End of "$Id: client.c,v 1.91.2.25 2002/10/16 22:13:01 mike Exp $".