]>
Commit | Line | Data |
---|---|---|
ef416fc2 | 1 | /* |
cb7f98ee | 2 | * "$Id: client.c 10886 2013-03-08 17:56:02Z mike $" |
ef416fc2 | 3 | * |
10d09e33 | 4 | * Client routines for the CUPS scheduler. |
ef416fc2 | 5 | * |
cb7f98ee | 6 | * Copyright 2007-2013 by Apple Inc. |
f7deaa1a | 7 | * Copyright 1997-2007 by Easy Software Products, all rights reserved. |
8 | * | |
9 | * This file contains Kerberos support code, copyright 2006 by | |
10 | * Jelmer Vernooij. | |
ef416fc2 | 11 | * |
12 | * These coded instructions, statements, and computer programs are the | |
bc44d920 | 13 | * property of Apple Inc. and are protected by Federal copyright |
14 | * law. Distribution and use rights are outlined in the file "LICENSE.txt" | |
15 | * which should have been included with this file. If this file is | |
16 | * file is missing or damaged, see the license at "http://www.cups.org/". | |
ef416fc2 | 17 | * |
18 | * Contents: | |
19 | * | |
82cc1f9a MS |
20 | * cupsdAcceptClient() - Accept a new client. |
21 | * cupsdCloseAllClients() - Close all remote clients immediately. | |
22 | * cupsdCloseClient() - Close a remote client. | |
23 | * cupsdFlushHeader() - Flush the header fields to the client. | |
24 | * cupsdReadClient() - Read data from a client. | |
25 | * cupsdSendCommand() - Send output from a command via HTTP. | |
26 | * cupsdSendError() - Send an error message via HTTP. | |
27 | * cupsdSendHeader() - Send an HTTP request. | |
28 | * cupsdUpdateCGI() - Read status messages from CGI scripts and | |
29 | * programs. | |
30 | * cupsdWriteClient() - Write data to a client as needed. | |
31 | * check_if_modified() - Decode an "If-Modified-Since" line. | |
32 | * compare_clients() - Compare two client connections. | |
33 | * data_ready() - Check whether data is available from a client. | |
34 | * get_file() - Get a filename and state info. | |
c41769ff | 35 | * install_cupsd_conf() - Install a configuration file. |
82cc1f9a MS |
36 | * is_cgi() - Is the resource a CGI script/program? |
37 | * is_path_absolute() - Is a path absolute and free of relative elements | |
38 | * (i.e. ".."). | |
39 | * pipe_command() - Pipe the output of a command to the remote | |
40 | * client. | |
41 | * valid_host() - Is the Host: field valid? | |
42 | * write_file() - Send a file via HTTP. | |
43 | * write_pipe() - Flag that data is available on the CGI pipe. | |
ef416fc2 | 44 | */ |
45 | ||
46 | /* | |
47 | * Include necessary headers... | |
48 | */ | |
49 | ||
ef416fc2 | 50 | #include "cupsd.h" |
51 | ||
cb7f98ee MS |
52 | #ifdef __APPLE__ |
53 | # include <libproc.h> | |
54 | #endif /* __APPLE__ */ | |
749b1e90 MS |
55 | #ifdef HAVE_TCPD_H |
56 | # include <tcpd.h> | |
57 | #endif /* HAVE_TCPD_H */ | |
58 | ||
ef416fc2 | 59 | |
85dda01c MS |
60 | /* |
61 | * Local globals... | |
62 | */ | |
63 | ||
64 | static const char * const http_states[] = | |
65 | { /* HTTP state strings */ | |
a469f8a5 MS |
66 | "HTTP_STATE_ERROR", |
67 | "HTTP_STATE_WAITING", | |
68 | "HTTP_STATE_OPTIONS", | |
69 | "HTTP_STATE_GET", | |
70 | "HTTP_STATE_GET_SEND", | |
71 | "HTTP_STATE_HEAD", | |
72 | "HTTP_STATE_POST", | |
73 | "HTTP_STATE_POST_RECV", | |
74 | "HTTP_STATE_POST_SEND", | |
75 | "HTTP_STATE_PUT", | |
76 | "HTTP_STATE_PUT_RECV", | |
77 | "HTTP_STATE_DELETE", | |
78 | "HTTP_STATE_TRACE", | |
79 | "HTTP_STATE_CONNECT", | |
80 | "HTTP_STATE_STATUS", | |
81 | "HTTP_STATE_UNKNOWN_METHOD", | |
82 | "HTTP_STATE_UNKNOWN_VERSION" | |
85dda01c MS |
83 | }; |
84 | static const char * const ipp_states[] = | |
85 | { /* IPP state strings */ | |
86 | "IPP_IDLE", | |
87 | "IPP_HEADER", | |
88 | "IPP_ATTRIBUTE", | |
89 | "IPP_DATA" | |
90 | }; | |
91 | ||
92 | ||
ef416fc2 | 93 | /* |
94 | * Local functions... | |
95 | */ | |
96 | ||
97 | static int check_if_modified(cupsd_client_t *con, | |
98 | struct stat *filestats); | |
3dfe78b3 MS |
99 | static int compare_clients(cupsd_client_t *a, cupsd_client_t *b, |
100 | void *data); | |
ee571f26 | 101 | static int data_ready(cupsd_client_t *con); |
f7deaa1a | 102 | static char *get_file(cupsd_client_t *con, struct stat *filestats, |
ef416fc2 | 103 | char *filename, int len); |
c41769ff | 104 | static http_status_t install_cupsd_conf(cupsd_client_t *con); |
e1d6a774 | 105 | static int is_cgi(cupsd_client_t *con, const char *filename, |
106 | struct stat *filestats, mime_type_t *type); | |
ef416fc2 | 107 | static int is_path_absolute(const char *path); |
108 | static int pipe_command(cupsd_client_t *con, int infile, int *outfile, | |
109 | char *command, char *options, int root); | |
e07d4801 | 110 | static int valid_host(cupsd_client_t *con); |
a74454a7 | 111 | static int write_file(cupsd_client_t *con, http_status_t code, |
112 | char *filename, char *type, | |
113 | struct stat *filestats); | |
f899b121 | 114 | static void write_pipe(cupsd_client_t *con); |
ef416fc2 | 115 | |
116 | ||
117 | /* | |
118 | * 'cupsdAcceptClient()' - Accept a new client. | |
119 | */ | |
120 | ||
121 | void | |
122 | cupsdAcceptClient(cupsd_listener_t *lis)/* I - Listener socket */ | |
123 | { | |
ef416fc2 | 124 | int count; /* Count of connections on a host */ |
125 | int val; /* Parameter value */ | |
bd7854cb | 126 | cupsd_client_t *con, /* New client pointer */ |
127 | *tempcon; /* Temporary client pointer */ | |
ef416fc2 | 128 | http_addrlist_t *addrlist, /* List of adddresses for host */ |
129 | *addr; /* Current address */ | |
130 | socklen_t addrlen; /* Length of address */ | |
131 | char *hostname; /* Hostname for address */ | |
132 | http_addr_t temp; /* Temporary address variable */ | |
133 | static time_t last_dos = 0; /* Time of last DoS attack */ | |
749b1e90 MS |
134 | #ifdef HAVE_TCPD_H |
135 | struct request_info wrap_req; /* TCP wrappers request information */ | |
136 | #endif /* HAVE_TCPD_H */ | |
ef416fc2 | 137 | |
138 | ||
139 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
b9faaae1 | 140 | "cupsdAcceptClient(lis=%p(%d)) Clients=%d", |
bd7854cb | 141 | lis, lis->fd, cupsArrayCount(Clients)); |
ef416fc2 | 142 | |
143 | /* | |
144 | * Make sure we don't have a full set of clients already... | |
145 | */ | |
146 | ||
bd7854cb | 147 | if (cupsArrayCount(Clients) == MaxClients) |
ef416fc2 | 148 | return; |
149 | ||
150 | /* | |
151 | * Get a pointer to the next available client... | |
152 | */ | |
153 | ||
bd7854cb | 154 | if (!Clients) |
155 | Clients = cupsArrayNew(NULL, NULL); | |
156 | ||
157 | if (!Clients) | |
91c84a35 MS |
158 | { |
159 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
3dfe78b3 MS |
160 | "Unable to allocate memory for clients array!"); |
161 | cupsdPauseListening(); | |
162 | return; | |
163 | } | |
164 | ||
165 | if (!ActiveClients) | |
166 | ActiveClients = cupsArrayNew((cups_array_func_t)compare_clients, NULL); | |
167 | ||
168 | if (!ActiveClients) | |
169 | { | |
170 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
171 | "Unable to allocate memory for active clients array!"); | |
91c84a35 | 172 | cupsdPauseListening(); |
bd7854cb | 173 | return; |
91c84a35 | 174 | } |
bd7854cb | 175 | |
91c84a35 MS |
176 | if ((con = calloc(1, sizeof(cupsd_client_t))) == NULL) |
177 | { | |
178 | cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to allocate memory for client!"); | |
179 | cupsdPauseListening(); | |
180 | return; | |
181 | } | |
ef416fc2 | 182 | |
85dda01c | 183 | con->file = -1; |
dcb445bc | 184 | con->http.activity = time(NULL); |
85dda01c MS |
185 | con->http.hostaddr = &(con->clientaddr); |
186 | con->http.wait_value = 10000; | |
c41769ff | 187 | con->http.mode = _HTTP_MODE_SERVER; |
ef416fc2 | 188 | |
189 | /* | |
190 | * Accept the client and get the remote address... | |
191 | */ | |
192 | ||
193 | addrlen = sizeof(http_addr_t); | |
194 | ||
195 | if ((con->http.fd = accept(lis->fd, (struct sockaddr *)con->http.hostaddr, | |
196 | &addrlen)) < 0) | |
197 | { | |
76cd9e37 MS |
198 | if (errno == ENFILE || errno == EMFILE) |
199 | cupsdPauseListening(); | |
200 | ||
ef416fc2 | 201 | cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to accept client connection - %s.", |
202 | strerror(errno)); | |
bd7854cb | 203 | free(con); |
76cd9e37 | 204 | |
ef416fc2 | 205 | return; |
206 | } | |
207 | ||
22c9029b MS |
208 | /* |
209 | * Save the connected port number... | |
210 | */ | |
ef416fc2 | 211 | |
a469f8a5 | 212 | _httpAddrSetPort(con->http.hostaddr, httpAddrPort(&(lis->address))); |
ef416fc2 | 213 | |
22c9029b MS |
214 | #ifdef AF_INET6 |
215 | /* | |
216 | * Convert IPv4 over IPv6 addresses (::ffff:n.n.n.n) to IPv4 forms we | |
217 | * can more easily use... | |
218 | */ | |
ef416fc2 | 219 | |
22c9029b MS |
220 | if (lis->address.addr.sa_family == AF_INET6 && |
221 | con->http.hostaddr->ipv6.sin6_addr.s6_addr32[0] == 0 && | |
222 | con->http.hostaddr->ipv6.sin6_addr.s6_addr32[1] == 0 && | |
223 | ntohl(con->http.hostaddr->ipv6.sin6_addr.s6_addr32[2]) == 0xffff) | |
224 | con->http.hostaddr->ipv6.sin6_addr.s6_addr32[2] = 0; | |
ef416fc2 | 225 | #endif /* AF_INET6 */ |
ef416fc2 | 226 | |
227 | /* | |
228 | * Check the number of clients on the same address... | |
229 | */ | |
230 | ||
bd7854cb | 231 | for (count = 0, tempcon = (cupsd_client_t *)cupsArrayFirst(Clients); |
232 | tempcon; | |
233 | tempcon = (cupsd_client_t *)cupsArrayNext(Clients)) | |
234 | if (httpAddrEqual(tempcon->http.hostaddr, con->http.hostaddr)) | |
ef416fc2 | 235 | { |
236 | count ++; | |
237 | if (count >= MaxClientsPerHost) | |
238 | break; | |
239 | } | |
240 | ||
241 | if (count >= MaxClientsPerHost) | |
242 | { | |
243 | if ((time(NULL) - last_dos) >= 60) | |
244 | { | |
245 | last_dos = time(NULL); | |
246 | cupsdLogMessage(CUPSD_LOG_WARN, | |
bd7854cb | 247 | "Possible DoS attack - more than %d clients connecting " |
248 | "from %s!", | |
749b1e90 MS |
249 | MaxClientsPerHost, |
250 | httpAddrString(con->http.hostaddr, con->http.hostname, | |
251 | sizeof(con->http.hostname))); | |
ef416fc2 | 252 | } |
253 | ||
254 | #ifdef WIN32 | |
255 | closesocket(con->http.fd); | |
256 | #else | |
257 | close(con->http.fd); | |
258 | #endif /* WIN32 */ | |
259 | ||
bd7854cb | 260 | free(con); |
ef416fc2 | 261 | return; |
262 | } | |
263 | ||
264 | /* | |
265 | * Get the hostname or format the IP address as needed... | |
266 | */ | |
267 | ||
268 | if (httpAddrLocalhost(con->http.hostaddr)) | |
269 | { | |
270 | /* | |
271 | * Map accesses from the loopback interface to "localhost"... | |
272 | */ | |
273 | ||
274 | strlcpy(con->http.hostname, "localhost", sizeof(con->http.hostname)); | |
275 | hostname = con->http.hostname; | |
276 | } | |
277 | else | |
278 | { | |
279 | /* | |
280 | * Map accesses from the same host to the server name. | |
281 | */ | |
282 | ||
d1c13e16 | 283 | if (HostNameLookups) |
ef416fc2 | 284 | hostname = httpAddrLookup(con->http.hostaddr, con->http.hostname, |
285 | sizeof(con->http.hostname)); | |
286 | else | |
287 | { | |
288 | hostname = NULL; | |
289 | httpAddrString(con->http.hostaddr, con->http.hostname, | |
290 | sizeof(con->http.hostname)); | |
291 | } | |
292 | } | |
293 | ||
294 | if (hostname == NULL && HostNameLookups == 2) | |
295 | { | |
296 | /* | |
297 | * Can't have an unresolved IP address with double-lookups enabled... | |
298 | */ | |
299 | ||
ef416fc2 | 300 | #ifdef WIN32 |
301 | closesocket(con->http.fd); | |
302 | #else | |
303 | close(con->http.fd); | |
304 | #endif /* WIN32 */ | |
305 | ||
306 | cupsdLogMessage(CUPSD_LOG_WARN, | |
307 | "Name lookup failed - connection from %s closed!", | |
308 | con->http.hostname); | |
bd7854cb | 309 | |
310 | free(con); | |
ef416fc2 | 311 | return; |
312 | } | |
313 | ||
314 | if (HostNameLookups == 2) | |
315 | { | |
316 | /* | |
317 | * Do double lookups as needed... | |
318 | */ | |
319 | ||
749b1e90 MS |
320 | if ((addrlist = httpAddrGetList(con->http.hostname, AF_UNSPEC, NULL)) |
321 | != NULL) | |
ef416fc2 | 322 | { |
323 | /* | |
324 | * See if the hostname maps to the same IP address... | |
325 | */ | |
326 | ||
327 | for (addr = addrlist; addr; addr = addr->next) | |
328 | if (httpAddrEqual(con->http.hostaddr, &(addr->addr))) | |
329 | break; | |
330 | } | |
331 | else | |
332 | addr = NULL; | |
333 | ||
334 | httpAddrFreeList(addrlist); | |
335 | ||
336 | if (!addr) | |
337 | { | |
338 | /* | |
339 | * Can't have a hostname that doesn't resolve to the same IP address | |
340 | * with double-lookups enabled... | |
341 | */ | |
342 | ||
ef416fc2 | 343 | #ifdef WIN32 |
344 | closesocket(con->http.fd); | |
345 | #else | |
346 | close(con->http.fd); | |
347 | #endif /* WIN32 */ | |
348 | ||
349 | cupsdLogMessage(CUPSD_LOG_WARN, | |
350 | "IP lookup failed - connection from %s closed!", | |
351 | con->http.hostname); | |
bd7854cb | 352 | free(con); |
ef416fc2 | 353 | return; |
354 | } | |
355 | } | |
356 | ||
749b1e90 MS |
357 | #ifdef HAVE_TCPD_H |
358 | /* | |
359 | * See if the connection is denied by TCP wrappers... | |
360 | */ | |
361 | ||
362 | request_init(&wrap_req, RQ_DAEMON, "cupsd", RQ_FILE, con->http.fd, NULL); | |
363 | fromhost(&wrap_req); | |
364 | ||
365 | if (!hosts_access(&wrap_req)) | |
366 | { | |
749b1e90 MS |
367 | #ifdef WIN32 |
368 | closesocket(con->http.fd); | |
369 | #else | |
370 | close(con->http.fd); | |
371 | #endif /* WIN32 */ | |
372 | ||
373 | cupsdLogMessage(CUPSD_LOG_WARN, | |
374 | "Connection from %s refused by /etc/hosts.allow and " | |
375 | "/etc/hosts.deny rules.", con->http.hostname); | |
376 | free(con); | |
377 | return; | |
378 | } | |
379 | #endif /* HAVE_TCPD_H */ | |
380 | ||
ef416fc2 | 381 | #ifdef AF_LOCAL |
382 | if (con->http.hostaddr->addr.sa_family == AF_LOCAL) | |
cb7f98ee MS |
383 | { |
384 | # ifdef __APPLE__ | |
385 | socklen_t peersize; /* Size of peer credentials */ | |
386 | pid_t peerpid; /* Peer process ID */ | |
387 | char name[256]; /* Name of process */ | |
388 | ||
389 | peersize = sizeof(peerpid); | |
390 | if (!getsockopt(con->http.fd, SOL_LOCAL, LOCAL_PEERPID, &peerpid, | |
391 | &peersize)) | |
392 | { | |
393 | if (!proc_name(peerpid, name, sizeof(name))) | |
394 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
395 | "[Client %d] Accepted from %s (Domain ???[%d])", | |
396 | con->http.fd, con->http.hostname, (int)peerpid); | |
397 | else | |
398 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
399 | "[Client %d] Accepted from %s (Domain %s[%d])", | |
400 | con->http.fd, con->http.hostname, name, (int)peerpid); | |
401 | } | |
402 | else | |
403 | # endif /* __APPLE__ */ | |
404 | ||
85dda01c | 405 | cupsdLogMessage(CUPSD_LOG_DEBUG, "[Client %d] Accepted from %s (Domain)", |
ef416fc2 | 406 | con->http.fd, con->http.hostname); |
cb7f98ee | 407 | } |
ef416fc2 | 408 | else |
409 | #endif /* AF_LOCAL */ | |
85dda01c | 410 | cupsdLogMessage(CUPSD_LOG_DEBUG, "[Client %d] Accepted from %s:%d (IPv%d)", |
ef416fc2 | 411 | con->http.fd, con->http.hostname, |
a469f8a5 | 412 | httpAddrPort(con->http.hostaddr), |
22c9029b | 413 | _httpAddrFamily(con->http.hostaddr) == AF_INET ? 4 : 6); |
ef416fc2 | 414 | |
415 | /* | |
416 | * Get the local address the client connected to... | |
417 | */ | |
418 | ||
419 | addrlen = sizeof(temp); | |
420 | if (getsockname(con->http.fd, (struct sockaddr *)&temp, &addrlen)) | |
421 | { | |
422 | cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to get local address - %s", | |
423 | strerror(errno)); | |
424 | ||
5a9febac | 425 | strlcpy(con->servername, "localhost", sizeof(con->servername)); |
ef416fc2 | 426 | con->serverport = LocalPort; |
427 | } | |
22c9029b MS |
428 | #ifdef AF_LOCAL |
429 | else if (_httpAddrFamily(&temp) == AF_LOCAL) | |
430 | { | |
5a9febac | 431 | strlcpy(con->servername, "localhost", sizeof(con->servername)); |
22c9029b MS |
432 | con->serverport = LocalPort; |
433 | } | |
434 | #endif /* AF_LOCAL */ | |
ef416fc2 | 435 | else |
436 | { | |
22c9029b MS |
437 | if (httpAddrLocalhost(&temp)) |
438 | strlcpy(con->servername, "localhost", sizeof(con->servername)); | |
439 | else if (HostNameLookups || RemotePort) | |
440 | httpAddrLookup(&temp, con->servername, sizeof(con->servername)); | |
ef416fc2 | 441 | else |
22c9029b | 442 | httpAddrString(&temp, con->servername, sizeof(con->servername)); |
b19ccc9e | 443 | |
a469f8a5 | 444 | con->serverport = httpAddrPort(&(lis->address)); |
ef416fc2 | 445 | } |
446 | ||
dcb445bc MS |
447 | /* |
448 | * Add the connection to the array of active clients... | |
449 | */ | |
450 | ||
bd7854cb | 451 | cupsArrayAdd(Clients, con); |
452 | ||
ef416fc2 | 453 | /* |
b9faaae1 MS |
454 | * Using TCP_NODELAY improves responsiveness, especially on systems with a slow |
455 | * loopback interface. Since we write large buffers when sending print files | |
456 | * and requests there shouldn't be any performance penalty for this... | |
ef416fc2 | 457 | */ |
458 | ||
459 | val = 1; | |
f7deaa1a | 460 | setsockopt(con->http.fd, IPPROTO_TCP, TCP_NODELAY, &val, sizeof(val)); |
ef416fc2 | 461 | |
462 | /* | |
463 | * Close this file on all execs... | |
464 | */ | |
465 | ||
466 | fcntl(con->http.fd, F_SETFD, fcntl(con->http.fd, F_GETFD) | FD_CLOEXEC); | |
467 | ||
468 | /* | |
b9faaae1 | 469 | * Add the socket to the server select. |
ef416fc2 | 470 | */ |
471 | ||
f7deaa1a | 472 | cupsdAddSelect(con->http.fd, (cupsd_selfunc_t)cupsdReadClient, NULL, con); |
ef416fc2 | 473 | |
ef416fc2 | 474 | /* |
475 | * Temporarily suspend accept()'s until we lose a client... | |
476 | */ | |
477 | ||
bd7854cb | 478 | if (cupsArrayCount(Clients) == MaxClients) |
ef416fc2 | 479 | cupsdPauseListening(); |
480 | ||
481 | #ifdef HAVE_SSL | |
482 | /* | |
483 | * See if we are connecting on a secure port... | |
484 | */ | |
485 | ||
486 | if (lis->encryption == HTTP_ENCRYPT_ALWAYS) | |
487 | { | |
488 | /* | |
489 | * https connection; go secure... | |
490 | */ | |
491 | ||
492 | con->http.encryption = HTTP_ENCRYPT_ALWAYS; | |
493 | ||
82cc1f9a | 494 | if (!cupsdStartTLS(con)) |
411affcf | 495 | cupsdCloseClient(con); |
ef416fc2 | 496 | } |
497 | else | |
498 | con->auto_ssl = 1; | |
499 | #endif /* HAVE_SSL */ | |
500 | } | |
501 | ||
502 | ||
503 | /* | |
504 | * 'cupsdCloseAllClients()' - Close all remote clients immediately. | |
505 | */ | |
506 | ||
507 | void | |
508 | cupsdCloseAllClients(void) | |
509 | { | |
bd7854cb | 510 | cupsd_client_t *con; /* Current client */ |
511 | ||
512 | ||
b9faaae1 MS |
513 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdCloseAllClients() Clients=%d", |
514 | cupsArrayCount(Clients)); | |
515 | ||
bd7854cb | 516 | for (con = (cupsd_client_t *)cupsArrayFirst(Clients); |
517 | con; | |
518 | con = (cupsd_client_t *)cupsArrayNext(Clients)) | |
49d87452 MS |
519 | if (cupsdCloseClient(con)) |
520 | cupsdCloseClient(con); | |
ef416fc2 | 521 | } |
522 | ||
523 | ||
524 | /* | |
525 | * 'cupsdCloseClient()' - Close a remote client. | |
526 | */ | |
527 | ||
528 | int /* O - 1 if partial close, 0 if fully closed */ | |
529 | cupsdCloseClient(cupsd_client_t *con) /* I - Client to close */ | |
530 | { | |
531 | int partial; /* Do partial close for SSL? */ | |
532 | #ifdef HAVE_LIBSSL | |
ef416fc2 | 533 | #elif defined(HAVE_GNUTLS) |
89d46774 | 534 | # elif defined(HAVE_CDSASSL) |
ef416fc2 | 535 | #endif /* HAVE_LIBSSL */ |
536 | ||
537 | ||
85dda01c MS |
538 | cupsdLogMessage(CUPSD_LOG_DEBUG, "[Client %d] Closing connection.", |
539 | con->http.fd); | |
ef416fc2 | 540 | |
541 | /* | |
542 | * Flush pending writes before closing... | |
543 | */ | |
544 | ||
545 | httpFlushWrite(HTTP(con)); | |
546 | ||
547 | partial = 0; | |
548 | ||
549 | #ifdef HAVE_SSL | |
550 | /* | |
551 | * Shutdown encryption as needed... | |
552 | */ | |
553 | ||
554 | if (con->http.tls) | |
555 | { | |
556 | partial = 1; | |
557 | ||
82cc1f9a | 558 | cupsdEndTLS(con); |
ef416fc2 | 559 | } |
560 | #endif /* HAVE_SSL */ | |
561 | ||
562 | if (con->pipe_pid != 0) | |
563 | { | |
564 | /* | |
565 | * Stop any CGI process... | |
566 | */ | |
567 | ||
ef416fc2 | 568 | cupsdEndProcess(con->pipe_pid, 1); |
569 | con->pipe_pid = 0; | |
570 | } | |
571 | ||
572 | if (con->file >= 0) | |
573 | { | |
f7deaa1a | 574 | cupsdRemoveSelect(con->file); |
ef416fc2 | 575 | |
ef416fc2 | 576 | close(con->file); |
577 | con->file = -1; | |
578 | } | |
579 | ||
580 | /* | |
581 | * Close the socket and clear the file from the input set for select()... | |
582 | */ | |
583 | ||
3dfe78b3 | 584 | if (con->http.fd >= 0) |
ef416fc2 | 585 | { |
3dfe78b3 MS |
586 | cupsArrayRemove(ActiveClients, con); |
587 | cupsdSetBusyState(); | |
588 | ||
ef416fc2 | 589 | if (partial) |
590 | { | |
591 | /* | |
592 | * Only do a partial close so that the encrypted client gets everything. | |
593 | */ | |
594 | ||
ef416fc2 | 595 | shutdown(con->http.fd, 0); |
f7deaa1a | 596 | cupsdAddSelect(con->http.fd, (cupsd_selfunc_t)cupsdReadClient, NULL, con); |
ef416fc2 | 597 | } |
598 | else | |
599 | { | |
600 | /* | |
601 | * Shut the socket down fully... | |
602 | */ | |
603 | ||
f7deaa1a | 604 | cupsdRemoveSelect(con->http.fd); |
ef416fc2 | 605 | close(con->http.fd); |
ef416fc2 | 606 | con->http.fd = -1; |
607 | } | |
608 | } | |
609 | ||
610 | if (!partial) | |
611 | { | |
612 | /* | |
613 | * Free memory... | |
614 | */ | |
615 | ||
616 | if (con->http.input_set) | |
617 | free(con->http.input_set); | |
618 | ||
619 | httpClearCookie(HTTP(con)); | |
9f5eb9be | 620 | httpClearFields(HTTP(con)); |
ef416fc2 | 621 | |
622 | cupsdClearString(&con->filename); | |
623 | cupsdClearString(&con->command); | |
624 | cupsdClearString(&con->options); | |
b86bc4cf | 625 | cupsdClearString(&con->query_string); |
ef416fc2 | 626 | |
627 | if (con->request) | |
628 | { | |
629 | ippDelete(con->request); | |
630 | con->request = NULL; | |
631 | } | |
632 | ||
633 | if (con->response) | |
634 | { | |
635 | ippDelete(con->response); | |
636 | con->response = NULL; | |
637 | } | |
638 | ||
639 | if (con->language) | |
640 | { | |
641 | cupsLangFree(con->language); | |
642 | con->language = NULL; | |
643 | } | |
644 | ||
f7deaa1a | 645 | #ifdef HAVE_AUTHORIZATION_H |
646 | if (con->authref) | |
647 | { | |
648 | AuthorizationFree(con->authref, kAuthorizationFlagDefaults); | |
649 | con->authref = NULL; | |
650 | } | |
651 | #endif /* HAVE_AUTHORIZATION_H */ | |
652 | ||
ef416fc2 | 653 | /* |
654 | * Re-enable new client connections if we are going back under the | |
655 | * limit... | |
656 | */ | |
657 | ||
bd7854cb | 658 | if (cupsArrayCount(Clients) == MaxClients) |
ef416fc2 | 659 | cupsdResumeListening(); |
660 | ||
661 | /* | |
662 | * Compact the list of clients as necessary... | |
663 | */ | |
664 | ||
bd7854cb | 665 | cupsArrayRemove(Clients, con); |
ef416fc2 | 666 | |
bd7854cb | 667 | free(con); |
ef416fc2 | 668 | } |
669 | ||
670 | return (partial); | |
671 | } | |
672 | ||
673 | ||
d09495fa | 674 | /* |
675 | * 'cupsdFlushHeader()' - Flush the header fields to the client. | |
676 | */ | |
677 | ||
07725fee | 678 | int /* I - Bytes written or -1 on error */ |
d09495fa | 679 | cupsdFlushHeader(cupsd_client_t *con) /* I - Client to flush to */ |
680 | { | |
07725fee | 681 | int bytes = httpFlushWrite(HTTP(con)); |
d09495fa | 682 | |
cb7f98ee | 683 | con->http.data_encoding = HTTP_ENCODING_LENGTH; |
07725fee | 684 | |
685 | return (bytes); | |
d09495fa | 686 | } |
687 | ||
688 | ||
ef416fc2 | 689 | /* |
690 | * 'cupsdReadClient()' - Read data from a client. | |
691 | */ | |
692 | ||
f7deaa1a | 693 | void |
ef416fc2 | 694 | cupsdReadClient(cupsd_client_t *con) /* I - Client to read from */ |
695 | { | |
696 | char line[32768], /* Line from client... */ | |
697 | operation[64], /* Operation code from socket */ | |
698 | version[64], /* HTTP version number string */ | |
699 | locale[64], /* Locale */ | |
700 | *ptr; /* Pointer into strings */ | |
701 | int major, minor; /* HTTP version numbers */ | |
702 | http_status_t status; /* Transfer status */ | |
703 | ipp_state_t ipp_state; /* State of IPP transfer */ | |
704 | int bytes; /* Number of bytes to POST */ | |
705 | char *filename; /* Name of file for GET/HEAD */ | |
706 | char buf[1024]; /* Buffer for real filename */ | |
707 | struct stat filestats; /* File information */ | |
708 | mime_type_t *type; /* MIME type of file */ | |
709 | cupsd_printer_t *p; /* Printer */ | |
710 | static unsigned request_id = 0; /* Request ID for temp files */ | |
711 | ||
712 | ||
713 | status = HTTP_CONTINUE; | |
714 | ||
715 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
85dda01c MS |
716 | "[Client %d] cupsdReadClient " |
717 | "error=%d, " | |
718 | "used=%d, " | |
719 | "state=%s, " | |
a469f8a5 | 720 | "data_encoding=HTTP_ENCODING_%s, " |
85dda01c MS |
721 | "data_remaining=" CUPS_LLFMT ", " |
722 | "request=%p(%s), " | |
723 | "file=%d", | |
724 | con->http.fd, con->http.error, con->http.used, | |
a469f8a5 | 725 | http_states[con->http.state + 1], |
cb7f98ee | 726 | con->http.data_encoding == HTTP_ENCODING_CHUNKED ? |
b9faaae1 | 727 | "CHUNKED" : "LENGTH", |
85dda01c MS |
728 | CUPS_LLCAST con->http.data_remaining, |
729 | con->request, | |
730 | con->request ? ipp_states[con->request->state] : "", | |
731 | con->file); | |
ef416fc2 | 732 | |
ef416fc2 | 733 | #ifdef HAVE_SSL |
734 | if (con->auto_ssl) | |
735 | { | |
736 | /* | |
737 | * Automatically check for a SSL/TLS handshake... | |
738 | */ | |
739 | ||
740 | con->auto_ssl = 0; | |
741 | ||
742 | if (recv(con->http.fd, buf, 1, MSG_PEEK) == 1 && | |
743 | (!buf[0] || !strchr("DGHOPT", buf[0]))) | |
744 | { | |
745 | /* | |
746 | * Encrypt this connection... | |
747 | */ | |
748 | ||
749 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
85dda01c MS |
750 | "[Client %d] Saw first byte %02X, auto-negotiating " |
751 | "SSL/TLS session.", con->http.fd, buf[0] & 255); | |
ef416fc2 | 752 | |
82cc1f9a | 753 | if (!cupsdStartTLS(con)) |
f7deaa1a | 754 | cupsdCloseClient(con); |
411affcf | 755 | |
f7deaa1a | 756 | return; |
ef416fc2 | 757 | } |
758 | } | |
759 | #endif /* HAVE_SSL */ | |
760 | ||
761 | switch (con->http.state) | |
762 | { | |
cb7f98ee | 763 | case HTTP_STATE_WAITING : |
ef416fc2 | 764 | /* |
765 | * See if we've received a request line... | |
766 | */ | |
767 | ||
768 | if (httpGets(line, sizeof(line) - 1, HTTP(con)) == NULL) | |
769 | { | |
f11a948a MS |
770 | if (con->http.error && con->http.error != EPIPE) |
771 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
cb7f98ee | 772 | "[Client %d] HTTP_STATE_WAITING Closing for error %d " |
f11a948a MS |
773 | "(%s)", con->http.fd, con->http.error, |
774 | strerror(con->http.error)); | |
775 | else | |
776 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
cb7f98ee | 777 | "[Client %d] HTTP_STATE_WAITING Closing on EOF", |
f11a948a MS |
778 | con->http.fd); |
779 | ||
f7deaa1a | 780 | cupsdCloseClient(con); |
781 | return; | |
ef416fc2 | 782 | } |
783 | ||
784 | /* | |
785 | * Ignore blank request lines... | |
786 | */ | |
787 | ||
788 | if (line[0] == '\0') | |
789 | break; | |
790 | ||
791 | /* | |
792 | * Clear other state variables... | |
793 | */ | |
794 | ||
795 | httpClearFields(HTTP(con)); | |
796 | ||
797 | con->http.activity = time(NULL); | |
798 | con->http.version = HTTP_1_0; | |
799 | con->http.keep_alive = HTTP_KEEPALIVE_OFF; | |
cb7f98ee | 800 | con->http.data_encoding = HTTP_ENCODING_LENGTH; |
ef416fc2 | 801 | con->http.data_remaining = 0; |
802 | con->http._data_remaining = 0; | |
cb7f98ee | 803 | con->operation = HTTP_STATE_WAITING; |
ef416fc2 | 804 | con->bytes = 0; |
805 | con->file = -1; | |
806 | con->file_ready = 0; | |
807 | con->pipe_pid = 0; | |
808 | con->username[0] = '\0'; | |
809 | con->password[0] = '\0'; | |
810 | con->uri[0] = '\0'; | |
811 | ||
812 | cupsdClearString(&con->command); | |
813 | cupsdClearString(&con->options); | |
b86bc4cf | 814 | cupsdClearString(&con->query_string); |
ef416fc2 | 815 | |
2abf387c | 816 | if (con->request) |
817 | { | |
818 | ippDelete(con->request); | |
819 | con->request = NULL; | |
820 | } | |
821 | ||
822 | if (con->response) | |
823 | { | |
824 | ippDelete(con->response); | |
825 | con->response = NULL; | |
826 | } | |
827 | ||
828 | if (con->language) | |
ef416fc2 | 829 | { |
830 | cupsLangFree(con->language); | |
831 | con->language = NULL; | |
832 | } | |
833 | ||
09a101d6 | 834 | #ifdef HAVE_GSSAPI |
97c9a8d7 | 835 | con->have_gss = 0; |
07ed0e9a | 836 | con->gss_uid = 0; |
09a101d6 | 837 | #endif /* HAVE_GSSAPI */ |
838 | ||
ef416fc2 | 839 | /* |
840 | * Grab the request line... | |
841 | */ | |
842 | ||
843 | switch (sscanf(line, "%63s%1023s%63s", operation, con->uri, version)) | |
844 | { | |
845 | case 1 : | |
c934a06c MS |
846 | if (line[0]) |
847 | { | |
848 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
85dda01c MS |
849 | "[Client %d] Bad request line \"%s\" from %s.", |
850 | con->http.fd, | |
c8fef167 | 851 | _httpEncodeURI(buf, line, sizeof(buf)), |
c934a06c MS |
852 | con->http.hostname); |
853 | cupsdSendError(con, HTTP_BAD_REQUEST, CUPSD_AUTH_NONE); | |
854 | cupsdCloseClient(con); | |
855 | } | |
f7deaa1a | 856 | return; |
ef416fc2 | 857 | case 2 : |
858 | con->http.version = HTTP_0_9; | |
859 | break; | |
860 | case 3 : | |
861 | if (sscanf(version, "HTTP/%d.%d", &major, &minor) != 2) | |
862 | { | |
863 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
85dda01c MS |
864 | "[Client %d] Bad request line \"%s\" from %s.", |
865 | con->http.fd, | |
c8fef167 | 866 | _httpEncodeURI(buf, line, sizeof(buf)), |
ef416fc2 | 867 | con->http.hostname); |
5bd77a73 | 868 | cupsdSendError(con, HTTP_BAD_REQUEST, CUPSD_AUTH_NONE); |
f7deaa1a | 869 | cupsdCloseClient(con); |
870 | return; | |
ef416fc2 | 871 | } |
872 | ||
873 | if (major < 2) | |
874 | { | |
875 | con->http.version = (http_version_t)(major * 100 + minor); | |
876 | if (con->http.version == HTTP_1_1 && KeepAlive) | |
877 | con->http.keep_alive = HTTP_KEEPALIVE_ON; | |
878 | else | |
879 | con->http.keep_alive = HTTP_KEEPALIVE_OFF; | |
880 | } | |
881 | else | |
882 | { | |
f11a948a | 883 | cupsdLogMessage(CUPSD_LOG_ERROR, |
85dda01c MS |
884 | "[Client %d] Unsupported request line \"%s\" " |
885 | "from %s.", con->http.fd, | |
c8fef167 MS |
886 | _httpEncodeURI(buf, line, sizeof(buf)), |
887 | con->http.hostname); | |
5bd77a73 | 888 | cupsdSendError(con, HTTP_NOT_SUPPORTED, CUPSD_AUTH_NONE); |
f7deaa1a | 889 | cupsdCloseClient(con); |
890 | return; | |
ef416fc2 | 891 | } |
892 | break; | |
893 | } | |
894 | ||
895 | /* | |
896 | * Handle full URLs in the request line... | |
897 | */ | |
898 | ||
899 | if (strcmp(con->uri, "*")) | |
900 | { | |
e6013cfa | 901 | char scheme[HTTP_MAX_URI], /* Method/scheme */ |
ef416fc2 | 902 | userpass[HTTP_MAX_URI], /* Username:password */ |
903 | hostname[HTTP_MAX_URI], /* Hostname */ | |
904 | resource[HTTP_MAX_URI]; /* Resource path */ | |
905 | int port; /* Port number */ | |
906 | ||
907 | ||
908 | /* | |
909 | * Separate the URI into its components... | |
910 | */ | |
911 | ||
a4d04587 | 912 | httpSeparateURI(HTTP_URI_CODING_MOST, con->uri, |
e6013cfa | 913 | scheme, sizeof(scheme), |
ef416fc2 | 914 | userpass, sizeof(userpass), |
915 | hostname, sizeof(hostname), &port, | |
916 | resource, sizeof(resource)); | |
917 | ||
918 | /* | |
919 | * Only allow URIs with the servername, localhost, or an IP | |
920 | * address... | |
921 | */ | |
922 | ||
e6013cfa | 923 | if (strcmp(scheme, "file") && |
88f9aafc MS |
924 | _cups_strcasecmp(hostname, ServerName) && |
925 | _cups_strcasecmp(hostname, "localhost") && | |
e6013cfa | 926 | !isdigit(hostname[0]) && hostname[0] != '[') |
ef416fc2 | 927 | { |
928 | /* | |
929 | * Nope, we don't do proxies... | |
930 | */ | |
931 | ||
85dda01c MS |
932 | cupsdLogMessage(CUPSD_LOG_ERROR, |
933 | "[Client %d] Bad URI \"%s\" in request.", | |
934 | con->http.fd, con->uri); | |
5bd77a73 | 935 | cupsdSendError(con, HTTP_METHOD_NOT_ALLOWED, CUPSD_AUTH_NONE); |
f7deaa1a | 936 | cupsdCloseClient(con); |
937 | return; | |
ef416fc2 | 938 | } |
939 | ||
940 | /* | |
941 | * Copy the resource portion back into the URI; both resource and | |
942 | * con->uri are HTTP_MAX_URI bytes in size... | |
943 | */ | |
944 | ||
5a9febac | 945 | strlcpy(con->uri, resource, sizeof(con->uri)); |
ef416fc2 | 946 | } |
947 | ||
948 | /* | |
949 | * Process the request... | |
950 | */ | |
951 | ||
952 | if (!strcmp(operation, "GET")) | |
cb7f98ee | 953 | con->http.state = HTTP_STATE_GET; |
ef416fc2 | 954 | else if (!strcmp(operation, "PUT")) |
cb7f98ee | 955 | con->http.state = HTTP_STATE_PUT; |
ef416fc2 | 956 | else if (!strcmp(operation, "POST")) |
cb7f98ee | 957 | con->http.state = HTTP_STATE_POST; |
ef416fc2 | 958 | else if (!strcmp(operation, "DELETE")) |
cb7f98ee | 959 | con->http.state = HTTP_STATE_DELETE; |
ef416fc2 | 960 | else if (!strcmp(operation, "TRACE")) |
cb7f98ee | 961 | con->http.state = HTTP_STATE_TRACE; |
ef416fc2 | 962 | else if (!strcmp(operation, "OPTIONS")) |
cb7f98ee | 963 | con->http.state = HTTP_STATE_OPTIONS; |
ef416fc2 | 964 | else if (!strcmp(operation, "HEAD")) |
cb7f98ee | 965 | con->http.state = HTTP_STATE_HEAD; |
ef416fc2 | 966 | else |
967 | { | |
85dda01c MS |
968 | cupsdLogMessage(CUPSD_LOG_ERROR, |
969 | "[Client %d] Bad operation \"%s\".", con->http.fd, | |
970 | operation); | |
5bd77a73 | 971 | cupsdSendError(con, HTTP_BAD_REQUEST, CUPSD_AUTH_NONE); |
f7deaa1a | 972 | cupsdCloseClient(con); |
973 | return; | |
ef416fc2 | 974 | } |
975 | ||
dfd5680b | 976 | gettimeofday(&(con->start), NULL); |
ef416fc2 | 977 | con->operation = con->http.state; |
978 | ||
85dda01c | 979 | cupsdLogMessage(CUPSD_LOG_DEBUG, "[Client %d] %s %s HTTP/%d.%d", |
89d46774 | 980 | con->http.fd, operation, con->uri, |
ef416fc2 | 981 | con->http.version / 100, con->http.version % 100); |
982 | ||
983 | con->http.status = HTTP_OK; | |
984 | ||
49d87452 MS |
985 | if (!cupsArrayFind(ActiveClients, con)) |
986 | { | |
987 | cupsArrayAdd(ActiveClients, con); | |
988 | cupsdSetBusyState(); | |
989 | } | |
3dfe78b3 | 990 | |
cb7f98ee MS |
991 | case HTTP_STATE_OPTIONS : |
992 | case HTTP_STATE_DELETE : | |
993 | case HTTP_STATE_GET : | |
994 | case HTTP_STATE_HEAD : | |
995 | case HTTP_STATE_POST : | |
996 | case HTTP_STATE_PUT : | |
997 | case HTTP_STATE_TRACE : | |
ef416fc2 | 998 | /* |
999 | * Parse incoming parameters until the status changes... | |
1000 | */ | |
1001 | ||
f7deaa1a | 1002 | while ((status = httpUpdate(HTTP(con))) == HTTP_CONTINUE) |
ee571f26 | 1003 | if (!data_ready(con)) |
f7deaa1a | 1004 | break; |
ef416fc2 | 1005 | |
1006 | if (status != HTTP_OK && status != HTTP_CONTINUE) | |
1007 | { | |
f11a948a MS |
1008 | if (con->http.error && con->http.error != EPIPE) |
1009 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
85dda01c MS |
1010 | "[Client %d] Closing for error %d (%s) while " |
1011 | "reading headers.", | |
1012 | con->http.fd, con->http.error, | |
f11a948a MS |
1013 | strerror(con->http.error)); |
1014 | else | |
1015 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
85dda01c MS |
1016 | "[Client %d] Closing on EOF while reading headers.", |
1017 | con->http.fd); | |
f11a948a | 1018 | |
5bd77a73 | 1019 | cupsdSendError(con, HTTP_BAD_REQUEST, CUPSD_AUTH_NONE); |
f7deaa1a | 1020 | cupsdCloseClient(con); |
1021 | return; | |
ef416fc2 | 1022 | } |
1023 | break; | |
1024 | ||
1025 | default : | |
e6013cfa MS |
1026 | if (!data_ready(con) && recv(con->http.fd, buf, 1, MSG_PEEK) < 1) |
1027 | { | |
1028 | /* | |
1029 | * Connection closed... | |
1030 | */ | |
1031 | ||
f11a948a | 1032 | cupsdLogMessage(CUPSD_LOG_DEBUG, |
85dda01c | 1033 | "[Client %d] Closing on EOF", con->http.fd); |
e6013cfa MS |
1034 | cupsdCloseClient(con); |
1035 | return; | |
1036 | } | |
ef416fc2 | 1037 | break; /* Anti-compiler-warning-code */ |
1038 | } | |
1039 | ||
1040 | /* | |
1041 | * Handle new transfers... | |
1042 | */ | |
1043 | ||
1044 | if (status == HTTP_OK) | |
1045 | { | |
1046 | if (con->http.fields[HTTP_FIELD_ACCEPT_LANGUAGE][0]) | |
1047 | { | |
1048 | /* | |
1049 | * Figure out the locale from the Accept-Language and Content-Type | |
1050 | * fields... | |
1051 | */ | |
1052 | ||
f11a948a MS |
1053 | if ((ptr = strchr(con->http.fields[HTTP_FIELD_ACCEPT_LANGUAGE], |
1054 | ',')) != NULL) | |
ef416fc2 | 1055 | *ptr = '\0'; |
1056 | ||
f11a948a MS |
1057 | if ((ptr = strchr(con->http.fields[HTTP_FIELD_ACCEPT_LANGUAGE], |
1058 | ';')) != NULL) | |
ef416fc2 | 1059 | *ptr = '\0'; |
1060 | ||
f11a948a MS |
1061 | if ((ptr = strstr(con->http.fields[HTTP_FIELD_CONTENT_TYPE], |
1062 | "charset=")) != NULL) | |
ef416fc2 | 1063 | { |
1064 | /* | |
1065 | * Combine language and charset, and trim any extra params in the | |
1066 | * content-type. | |
1067 | */ | |
1068 | ||
1069 | snprintf(locale, sizeof(locale), "%s.%s", | |
1070 | con->http.fields[HTTP_FIELD_ACCEPT_LANGUAGE], ptr + 8); | |
1071 | ||
1072 | if ((ptr = strchr(locale, ',')) != NULL) | |
1073 | *ptr = '\0'; | |
1074 | } | |
1075 | else | |
f8b3a85b MS |
1076 | snprintf(locale, sizeof(locale), "%s.UTF-8", |
1077 | con->http.fields[HTTP_FIELD_ACCEPT_LANGUAGE]); | |
d6ae789d | 1078 | |
ef416fc2 | 1079 | con->language = cupsLangGet(locale); |
1080 | } | |
1081 | else | |
1082 | con->language = cupsLangGet(DefaultLocale); | |
1083 | ||
1084 | cupsdAuthorize(con); | |
1085 | ||
85dda01c MS |
1086 | if (!_cups_strncasecmp(con->http.fields[HTTP_FIELD_CONNECTION], |
1087 | "Keep-Alive", 10) && KeepAlive) | |
ef416fc2 | 1088 | con->http.keep_alive = HTTP_KEEPALIVE_ON; |
85dda01c MS |
1089 | else if (!_cups_strncasecmp(con->http.fields[HTTP_FIELD_CONNECTION], |
1090 | "close", 5)) | |
52f6f666 | 1091 | con->http.keep_alive = HTTP_KEEPALIVE_OFF; |
ef416fc2 | 1092 | |
1093 | if (!con->http.fields[HTTP_FIELD_HOST][0] && | |
1094 | con->http.version >= HTTP_1_1) | |
1095 | { | |
1096 | /* | |
1097 | * HTTP/1.1 and higher require the "Host:" field... | |
1098 | */ | |
1099 | ||
5bd77a73 | 1100 | if (!cupsdSendError(con, HTTP_BAD_REQUEST, CUPSD_AUTH_NONE)) |
f7deaa1a | 1101 | { |
85dda01c MS |
1102 | cupsdLogMessage(CUPSD_LOG_ERROR, |
1103 | "[Client %d] Missing Host: field in request.", | |
1104 | con->http.fd); | |
f7deaa1a | 1105 | cupsdCloseClient(con); |
1106 | return; | |
1107 | } | |
ef416fc2 | 1108 | } |
e07d4801 | 1109 | else if (!valid_host(con)) |
e6013cfa MS |
1110 | { |
1111 | /* | |
1112 | * Access to localhost must use "localhost" or the corresponding IPv4 | |
1113 | * or IPv6 values in the Host: field. | |
1114 | */ | |
1115 | ||
f11a948a | 1116 | cupsdLogMessage(CUPSD_LOG_ERROR, |
85dda01c MS |
1117 | "[Client %d] Request from \"%s\" using invalid Host: " |
1118 | "field \"%s\"", con->http.fd, con->http.hostname, | |
1119 | con->http.fields[HTTP_FIELD_HOST]); | |
e6013cfa MS |
1120 | |
1121 | if (!cupsdSendError(con, HTTP_BAD_REQUEST, CUPSD_AUTH_NONE)) | |
1122 | { | |
1123 | cupsdCloseClient(con); | |
1124 | return; | |
1125 | } | |
1126 | } | |
cb7f98ee | 1127 | else if (con->operation == HTTP_STATE_OPTIONS) |
ef416fc2 | 1128 | { |
1129 | /* | |
1130 | * Do OPTIONS command... | |
1131 | */ | |
1132 | ||
5bd77a73 | 1133 | if (con->best && con->best->type != CUPSD_AUTH_NONE) |
ef416fc2 | 1134 | { |
5bd77a73 | 1135 | if (!cupsdSendHeader(con, HTTP_UNAUTHORIZED, NULL, CUPSD_AUTH_NONE)) |
f7deaa1a | 1136 | { |
1137 | cupsdCloseClient(con); | |
1138 | return; | |
1139 | } | |
ef416fc2 | 1140 | } |
1141 | ||
88f9aafc | 1142 | if (!_cups_strcasecmp(con->http.fields[HTTP_FIELD_CONNECTION], "Upgrade") && |
ef416fc2 | 1143 | con->http.tls == NULL) |
1144 | { | |
1145 | #ifdef HAVE_SSL | |
1146 | /* | |
1147 | * Do encryption stuff... | |
1148 | */ | |
1149 | ||
5bd77a73 | 1150 | if (!cupsdSendHeader(con, HTTP_SWITCHING_PROTOCOLS, NULL, CUPSD_AUTH_NONE)) |
f7deaa1a | 1151 | { |
1152 | cupsdCloseClient(con); | |
1153 | return; | |
1154 | } | |
ef416fc2 | 1155 | |
1156 | httpPrintf(HTTP(con), "Connection: Upgrade\r\n"); | |
a469f8a5 | 1157 | httpPrintf(HTTP(con), "Upgrade: TLS/1.2,TLS/1.1,TLS/1.0\r\n"); |
ef416fc2 | 1158 | httpPrintf(HTTP(con), "Content-Length: 0\r\n"); |
1159 | httpPrintf(HTTP(con), "\r\n"); | |
07725fee | 1160 | |
1161 | if (cupsdFlushHeader(con) < 0) | |
f7deaa1a | 1162 | { |
1163 | cupsdCloseClient(con); | |
1164 | return; | |
1165 | } | |
ef416fc2 | 1166 | |
82cc1f9a | 1167 | if (!cupsdStartTLS(con)) |
f7deaa1a | 1168 | { |
1169 | cupsdCloseClient(con); | |
1170 | return; | |
1171 | } | |
ef416fc2 | 1172 | #else |
5bd77a73 | 1173 | if (!cupsdSendError(con, HTTP_NOT_IMPLEMENTED, CUPSD_AUTH_NONE)) |
f7deaa1a | 1174 | { |
1175 | cupsdCloseClient(con); | |
1176 | return; | |
1177 | } | |
ef416fc2 | 1178 | #endif /* HAVE_SSL */ |
1179 | } | |
1180 | ||
5bd77a73 | 1181 | if (!cupsdSendHeader(con, HTTP_OK, NULL, CUPSD_AUTH_NONE)) |
f7deaa1a | 1182 | { |
1183 | cupsdCloseClient(con); | |
1184 | return; | |
1185 | } | |
ef416fc2 | 1186 | |
1187 | httpPrintf(HTTP(con), "Allow: GET, HEAD, OPTIONS, POST, PUT\r\n"); | |
1188 | httpPrintf(HTTP(con), "Content-Length: 0\r\n"); | |
1189 | httpPrintf(HTTP(con), "\r\n"); | |
07725fee | 1190 | |
1191 | if (cupsdFlushHeader(con) < 0) | |
f7deaa1a | 1192 | { |
1193 | cupsdCloseClient(con); | |
1194 | return; | |
1195 | } | |
ef416fc2 | 1196 | } |
1197 | else if (!is_path_absolute(con->uri)) | |
1198 | { | |
1199 | /* | |
1200 | * Protect against malicious users! | |
1201 | */ | |
1202 | ||
bf3816c7 | 1203 | cupsdLogMessage(CUPSD_LOG_ERROR, |
85dda01c MS |
1204 | "[Client %d] Request for non-absolute resource \"%s\".", |
1205 | con->http.fd, con->uri); | |
bf3816c7 | 1206 | |
5bd77a73 | 1207 | if (!cupsdSendError(con, HTTP_FORBIDDEN, CUPSD_AUTH_NONE)) |
f7deaa1a | 1208 | { |
1209 | cupsdCloseClient(con); | |
1210 | return; | |
1211 | } | |
ef416fc2 | 1212 | } |
1213 | else | |
1214 | { | |
85dda01c MS |
1215 | if (!_cups_strcasecmp(con->http.fields[HTTP_FIELD_CONNECTION], |
1216 | "Upgrade") && con->http.tls == NULL) | |
ef416fc2 | 1217 | { |
1218 | #ifdef HAVE_SSL | |
1219 | /* | |
1220 | * Do encryption stuff... | |
1221 | */ | |
1222 | ||
85dda01c MS |
1223 | if (!cupsdSendHeader(con, HTTP_SWITCHING_PROTOCOLS, NULL, |
1224 | CUPSD_AUTH_NONE)) | |
f7deaa1a | 1225 | { |
1226 | cupsdCloseClient(con); | |
1227 | return; | |
1228 | } | |
ef416fc2 | 1229 | |
1230 | httpPrintf(HTTP(con), "Connection: Upgrade\r\n"); | |
a469f8a5 | 1231 | httpPrintf(HTTP(con), "Upgrade: TLS/1.2,TLS/1.1,TLS/1.0\r\n"); |
ef416fc2 | 1232 | httpPrintf(HTTP(con), "Content-Length: 0\r\n"); |
1233 | httpPrintf(HTTP(con), "\r\n"); | |
07725fee | 1234 | |
1235 | if (cupsdFlushHeader(con) < 0) | |
f7deaa1a | 1236 | { |
1237 | cupsdCloseClient(con); | |
1238 | return; | |
1239 | } | |
ef416fc2 | 1240 | |
82cc1f9a | 1241 | if (!cupsdStartTLS(con)) |
f7deaa1a | 1242 | { |
1243 | cupsdCloseClient(con); | |
1244 | return; | |
1245 | } | |
ef416fc2 | 1246 | #else |
5bd77a73 | 1247 | if (!cupsdSendError(con, HTTP_NOT_IMPLEMENTED, CUPSD_AUTH_NONE)) |
f7deaa1a | 1248 | { |
1249 | cupsdCloseClient(con); | |
1250 | return; | |
1251 | } | |
ef416fc2 | 1252 | #endif /* HAVE_SSL */ |
1253 | } | |
1254 | ||
1255 | if ((status = cupsdIsAuthorized(con, NULL)) != HTTP_OK) | |
1256 | { | |
5bd77a73 | 1257 | cupsdSendError(con, status, CUPSD_AUTH_NONE); |
f7deaa1a | 1258 | cupsdCloseClient(con); |
1259 | return; | |
ef416fc2 | 1260 | } |
1261 | ||
b423cd4c | 1262 | if (con->http.expect && |
cb7f98ee | 1263 | (con->operation == HTTP_STATE_POST || con->operation == HTTP_STATE_PUT)) |
ef416fc2 | 1264 | { |
b423cd4c | 1265 | if (con->http.expect == HTTP_CONTINUE) |
1266 | { | |
1267 | /* | |
1268 | * Send 100-continue header... | |
1269 | */ | |
1270 | ||
5bd77a73 | 1271 | if (!cupsdSendHeader(con, HTTP_CONTINUE, NULL, CUPSD_AUTH_NONE)) |
f7deaa1a | 1272 | { |
1273 | cupsdCloseClient(con); | |
1274 | return; | |
1275 | } | |
b423cd4c | 1276 | } |
1277 | else | |
1278 | { | |
1279 | /* | |
1280 | * Send 417-expectation-failed header... | |
1281 | */ | |
1282 | ||
f11a948a MS |
1283 | if (!cupsdSendHeader(con, HTTP_EXPECTATION_FAILED, NULL, |
1284 | CUPSD_AUTH_NONE)) | |
f7deaa1a | 1285 | { |
1286 | cupsdCloseClient(con); | |
1287 | return; | |
1288 | } | |
b423cd4c | 1289 | |
1290 | httpPrintf(HTTP(con), "Content-Length: 0\r\n"); | |
1291 | httpPrintf(HTTP(con), "\r\n"); | |
07725fee | 1292 | |
1293 | if (cupsdFlushHeader(con) < 0) | |
f7deaa1a | 1294 | { |
1295 | cupsdCloseClient(con); | |
1296 | return; | |
1297 | } | |
b423cd4c | 1298 | } |
ef416fc2 | 1299 | } |
1300 | ||
1301 | switch (con->http.state) | |
1302 | { | |
cb7f98ee | 1303 | case HTTP_STATE_GET_SEND : |
db8b865d MS |
1304 | if ((!strncmp(con->uri, "/ppd/", 5) || |
1305 | !strncmp(con->uri, "/printers/", 10)) && | |
ef416fc2 | 1306 | !strcmp(con->uri + strlen(con->uri) - 4, ".ppd")) |
1307 | { | |
1308 | /* | |
1309 | * Send PPD file - get the real printer name since printer | |
1310 | * names are not case sensitive but filenames can be... | |
1311 | */ | |
1312 | ||
1313 | con->uri[strlen(con->uri) - 4] = '\0'; /* Drop ".ppd" */ | |
1314 | ||
db8b865d MS |
1315 | if (!strncmp(con->uri, "/ppd/", 5)) |
1316 | p = cupsdFindPrinter(con->uri + 5); | |
1317 | else | |
1318 | p = cupsdFindPrinter(con->uri + 10); | |
1319 | ||
1320 | if (p) | |
1321 | { | |
ef416fc2 | 1322 | snprintf(con->uri, sizeof(con->uri), "/ppd/%s.ppd", p->name); |
db8b865d | 1323 | } |
ef416fc2 | 1324 | else |
1325 | { | |
5bd77a73 | 1326 | if (!cupsdSendError(con, HTTP_NOT_FOUND, CUPSD_AUTH_NONE)) |
f7deaa1a | 1327 | { |
1328 | cupsdCloseClient(con); | |
1329 | return; | |
1330 | } | |
ef416fc2 | 1331 | |
1332 | break; | |
1333 | } | |
1334 | } | |
db8b865d MS |
1335 | else if ((!strncmp(con->uri, "/icons/", 7) || |
1336 | !strncmp(con->uri, "/printers/", 10) || | |
7cf5915e MS |
1337 | !strncmp(con->uri, "/classes/", 9)) && |
1338 | !strcmp(con->uri + strlen(con->uri) - 4, ".png")) | |
1339 | { | |
1340 | /* | |
1341 | * Send icon file - get the real queue name since queue names are | |
1342 | * not case sensitive but filenames can be... | |
1343 | */ | |
1344 | ||
1345 | con->uri[strlen(con->uri) - 4] = '\0'; /* Drop ".png" */ | |
1346 | ||
db8b865d MS |
1347 | if (!strncmp(con->uri, "/icons/", 7)) |
1348 | p = cupsdFindPrinter(con->uri + 7); | |
1349 | else if (!strncmp(con->uri, "/printers/", 10)) | |
7cf5915e MS |
1350 | p = cupsdFindPrinter(con->uri + 10); |
1351 | else | |
1352 | p = cupsdFindClass(con->uri + 9); | |
1353 | ||
1354 | if (p) | |
1355 | snprintf(con->uri, sizeof(con->uri), "/icons/%s.png", p->name); | |
1356 | else | |
1357 | { | |
1358 | if (!cupsdSendError(con, HTTP_NOT_FOUND, CUPSD_AUTH_NONE)) | |
1359 | { | |
1360 | cupsdCloseClient(con); | |
1361 | return; | |
1362 | } | |
1363 | ||
1364 | break; | |
1365 | } | |
1366 | } | |
0268488e | 1367 | else if (!WebInterface) |
229681c1 MS |
1368 | { |
1369 | /* | |
1370 | * Web interface is disabled. Show an appropriate message... | |
1371 | */ | |
1372 | ||
1373 | if (!cupsdSendError(con, HTTP_WEBIF_DISABLED, CUPSD_AUTH_NONE)) | |
1374 | { | |
1375 | cupsdCloseClient(con); | |
1376 | return; | |
1377 | } | |
1378 | ||
1379 | break; | |
1380 | } | |
0268488e MS |
1381 | |
1382 | if ((!strncmp(con->uri, "/admin", 6) && | |
1383 | strncmp(con->uri, "/admin/conf/", 12) && | |
1384 | strncmp(con->uri, "/admin/log/", 11)) || | |
1385 | !strncmp(con->uri, "/printers", 9) || | |
1386 | !strncmp(con->uri, "/classes", 8) || | |
1387 | !strncmp(con->uri, "/help", 5) || | |
1388 | !strncmp(con->uri, "/jobs", 5)) | |
ef416fc2 | 1389 | { |
1390 | /* | |
1391 | * Send CGI output... | |
1392 | */ | |
1393 | ||
1394 | if (!strncmp(con->uri, "/admin", 6)) | |
1395 | { | |
1396 | cupsdSetStringf(&con->command, "%s/cgi-bin/admin.cgi", | |
1397 | ServerBin); | |
1398 | ||
b423cd4c | 1399 | cupsdSetString(&con->options, strchr(con->uri + 6, '?')); |
ef416fc2 | 1400 | } |
1401 | else if (!strncmp(con->uri, "/printers", 9)) | |
1402 | { | |
1403 | cupsdSetStringf(&con->command, "%s/cgi-bin/printers.cgi", | |
1404 | ServerBin); | |
b423cd4c | 1405 | |
1406 | if (con->uri[9] && con->uri[10]) | |
1407 | cupsdSetString(&con->options, con->uri + 9); | |
1408 | else | |
1409 | cupsdSetString(&con->options, NULL); | |
ef416fc2 | 1410 | } |
1411 | else if (!strncmp(con->uri, "/classes", 8)) | |
1412 | { | |
1413 | cupsdSetStringf(&con->command, "%s/cgi-bin/classes.cgi", | |
1414 | ServerBin); | |
b423cd4c | 1415 | |
1416 | if (con->uri[8] && con->uri[9]) | |
1417 | cupsdSetString(&con->options, con->uri + 8); | |
1418 | else | |
1419 | cupsdSetString(&con->options, NULL); | |
ef416fc2 | 1420 | } |
1421 | else if (!strncmp(con->uri, "/jobs", 5)) | |
1422 | { | |
1423 | cupsdSetStringf(&con->command, "%s/cgi-bin/jobs.cgi", | |
1424 | ServerBin); | |
b423cd4c | 1425 | |
1426 | if (con->uri[5] && con->uri[6]) | |
1427 | cupsdSetString(&con->options, con->uri + 5); | |
1428 | else | |
1429 | cupsdSetString(&con->options, NULL); | |
ef416fc2 | 1430 | } |
1431 | else | |
1432 | { | |
1433 | cupsdSetStringf(&con->command, "%s/cgi-bin/help.cgi", | |
1434 | ServerBin); | |
ef416fc2 | 1435 | |
b423cd4c | 1436 | if (con->uri[5] && con->uri[6]) |
1437 | cupsdSetString(&con->options, con->uri + 5); | |
1438 | else | |
1439 | cupsdSetString(&con->options, NULL); | |
1440 | } | |
ef416fc2 | 1441 | |
1442 | if (!cupsdSendCommand(con, con->command, con->options, 0)) | |
1443 | { | |
5bd77a73 | 1444 | if (!cupsdSendError(con, HTTP_NOT_FOUND, CUPSD_AUTH_NONE)) |
f7deaa1a | 1445 | { |
1446 | cupsdCloseClient(con); | |
1447 | return; | |
1448 | } | |
ef416fc2 | 1449 | } |
1450 | else | |
1451 | cupsdLogRequest(con, HTTP_OK); | |
1452 | ||
1453 | if (con->http.version <= HTTP_1_0) | |
1454 | con->http.keep_alive = HTTP_KEEPALIVE_OFF; | |
1455 | } | |
1456 | else if ((!strncmp(con->uri, "/admin/conf/", 12) && | |
1457 | (strchr(con->uri + 12, '/') || | |
1458 | strlen(con->uri) == 12)) || | |
1459 | (!strncmp(con->uri, "/admin/log/", 11) && | |
1460 | (strchr(con->uri + 11, '/') || | |
1461 | strlen(con->uri) == 11))) | |
1462 | { | |
1463 | /* | |
bf3816c7 | 1464 | * GET can only be done to configuration files directly under |
ef416fc2 | 1465 | * /admin/conf... |
1466 | */ | |
1467 | ||
bf3816c7 MS |
1468 | cupsdLogMessage(CUPSD_LOG_ERROR, |
1469 | "Request for subdirectory \"%s\"!", con->uri); | |
1470 | ||
5bd77a73 | 1471 | if (!cupsdSendError(con, HTTP_FORBIDDEN, CUPSD_AUTH_NONE)) |
f7deaa1a | 1472 | { |
1473 | cupsdCloseClient(con); | |
1474 | return; | |
1475 | } | |
ef416fc2 | 1476 | |
1477 | break; | |
1478 | } | |
1479 | else | |
1480 | { | |
1481 | /* | |
1482 | * Serve a file... | |
1483 | */ | |
1484 | ||
1485 | if ((filename = get_file(con, &filestats, buf, | |
1486 | sizeof(buf))) == NULL) | |
1487 | { | |
5bd77a73 | 1488 | if (!cupsdSendError(con, HTTP_NOT_FOUND, CUPSD_AUTH_NONE)) |
f7deaa1a | 1489 | { |
1490 | cupsdCloseClient(con); | |
1491 | return; | |
1492 | } | |
ef416fc2 | 1493 | |
1494 | break; | |
1495 | } | |
1496 | ||
4400e98d | 1497 | type = mimeFileType(MimeDatabase, filename, NULL, NULL); |
ef416fc2 | 1498 | |
e1d6a774 | 1499 | if (is_cgi(con, filename, &filestats, type)) |
ef416fc2 | 1500 | { |
1501 | /* | |
1502 | * Note: con->command and con->options were set by | |
e1d6a774 | 1503 | * is_cgi()... |
ef416fc2 | 1504 | */ |
1505 | ||
1506 | if (!cupsdSendCommand(con, con->command, con->options, 0)) | |
1507 | { | |
5bd77a73 | 1508 | if (!cupsdSendError(con, HTTP_NOT_FOUND, CUPSD_AUTH_NONE)) |
f7deaa1a | 1509 | { |
1510 | cupsdCloseClient(con); | |
1511 | return; | |
1512 | } | |
ef416fc2 | 1513 | } |
1514 | else | |
1515 | cupsdLogRequest(con, HTTP_OK); | |
1516 | ||
1517 | if (con->http.version <= HTTP_1_0) | |
1518 | con->http.keep_alive = HTTP_KEEPALIVE_OFF; | |
1519 | break; | |
1520 | } | |
1521 | ||
1522 | if (!check_if_modified(con, &filestats)) | |
1523 | { | |
5bd77a73 | 1524 | if (!cupsdSendError(con, HTTP_NOT_MODIFIED, CUPSD_AUTH_NONE)) |
f7deaa1a | 1525 | { |
1526 | cupsdCloseClient(con); | |
1527 | return; | |
1528 | } | |
ef416fc2 | 1529 | } |
1530 | else | |
1531 | { | |
1532 | if (type == NULL) | |
5a9febac | 1533 | strlcpy(line, "text/plain", sizeof(line)); |
ef416fc2 | 1534 | else |
1535 | snprintf(line, sizeof(line), "%s/%s", type->super, type->type); | |
1536 | ||
a74454a7 | 1537 | if (!write_file(con, HTTP_OK, filename, line, &filestats)) |
f7deaa1a | 1538 | { |
1539 | cupsdCloseClient(con); | |
1540 | return; | |
1541 | } | |
ef416fc2 | 1542 | } |
1543 | } | |
1544 | break; | |
1545 | ||
cb7f98ee | 1546 | case HTTP_STATE_POST_RECV : |
ef416fc2 | 1547 | /* |
1548 | * See if the POST request includes a Content-Length field, and if | |
1549 | * so check the length against any limits that are set... | |
1550 | */ | |
1551 | ||
ef416fc2 | 1552 | if (con->http.fields[HTTP_FIELD_CONTENT_LENGTH][0] && |
1553 | MaxRequestSize > 0 && | |
1554 | con->http.data_remaining > MaxRequestSize) | |
1555 | { | |
1556 | /* | |
1557 | * Request too large... | |
1558 | */ | |
1559 | ||
5bd77a73 | 1560 | if (!cupsdSendError(con, HTTP_REQUEST_TOO_LARGE, CUPSD_AUTH_NONE)) |
f7deaa1a | 1561 | { |
1562 | cupsdCloseClient(con); | |
1563 | return; | |
1564 | } | |
ef416fc2 | 1565 | |
1566 | break; | |
1567 | } | |
839a51c8 MS |
1568 | else if (con->http.data_remaining < 0 || |
1569 | (!con->http.fields[HTTP_FIELD_CONTENT_LENGTH][0] && | |
cb7f98ee | 1570 | con->http.data_encoding == HTTP_ENCODING_LENGTH)) |
ef416fc2 | 1571 | { |
1572 | /* | |
1573 | * Negative content lengths are invalid! | |
1574 | */ | |
1575 | ||
5bd77a73 | 1576 | if (!cupsdSendError(con, HTTP_BAD_REQUEST, CUPSD_AUTH_NONE)) |
f7deaa1a | 1577 | { |
1578 | cupsdCloseClient(con); | |
1579 | return; | |
1580 | } | |
ef416fc2 | 1581 | |
1582 | break; | |
1583 | } | |
1584 | ||
1585 | /* | |
1586 | * See what kind of POST request this is; for IPP requests the | |
1587 | * content-type field will be "application/ipp"... | |
1588 | */ | |
1589 | ||
1590 | if (!strcmp(con->http.fields[HTTP_FIELD_CONTENT_TYPE], | |
1591 | "application/ipp")) | |
1592 | con->request = ippNew(); | |
229681c1 MS |
1593 | else if (!WebInterface) |
1594 | { | |
1595 | /* | |
1596 | * Web interface is disabled. Show an appropriate message... | |
1597 | */ | |
1598 | ||
1599 | if (!cupsdSendError(con, HTTP_WEBIF_DISABLED, CUPSD_AUTH_NONE)) | |
1600 | { | |
1601 | cupsdCloseClient(con); | |
1602 | return; | |
1603 | } | |
1604 | ||
1605 | break; | |
1606 | } | |
ef416fc2 | 1607 | else if ((!strncmp(con->uri, "/admin", 6) && |
1608 | strncmp(con->uri, "/admin/conf/", 12) && | |
1609 | strncmp(con->uri, "/admin/log/", 11)) || | |
1610 | !strncmp(con->uri, "/printers", 9) || | |
1611 | !strncmp(con->uri, "/classes", 8) || | |
1612 | !strncmp(con->uri, "/help", 5) || | |
1613 | !strncmp(con->uri, "/jobs", 5)) | |
1614 | { | |
1615 | /* | |
1616 | * CGI request... | |
1617 | */ | |
1618 | ||
1619 | if (!strncmp(con->uri, "/admin", 6)) | |
1620 | { | |
1621 | cupsdSetStringf(&con->command, "%s/cgi-bin/admin.cgi", | |
1622 | ServerBin); | |
1623 | ||
b423cd4c | 1624 | cupsdSetString(&con->options, strchr(con->uri + 6, '?')); |
ef416fc2 | 1625 | } |
1626 | else if (!strncmp(con->uri, "/printers", 9)) | |
1627 | { | |
1628 | cupsdSetStringf(&con->command, "%s/cgi-bin/printers.cgi", | |
1629 | ServerBin); | |
b423cd4c | 1630 | |
1631 | if (con->uri[9] && con->uri[10]) | |
1632 | cupsdSetString(&con->options, con->uri + 9); | |
1633 | else | |
1634 | cupsdSetString(&con->options, NULL); | |
ef416fc2 | 1635 | } |
1636 | else if (!strncmp(con->uri, "/classes", 8)) | |
1637 | { | |
1638 | cupsdSetStringf(&con->command, "%s/cgi-bin/classes.cgi", | |
1639 | ServerBin); | |
b423cd4c | 1640 | |
1641 | if (con->uri[8] && con->uri[9]) | |
1642 | cupsdSetString(&con->options, con->uri + 8); | |
1643 | else | |
1644 | cupsdSetString(&con->options, NULL); | |
ef416fc2 | 1645 | } |
1646 | else if (!strncmp(con->uri, "/jobs", 5)) | |
1647 | { | |
1648 | cupsdSetStringf(&con->command, "%s/cgi-bin/jobs.cgi", | |
1649 | ServerBin); | |
b423cd4c | 1650 | |
1651 | if (con->uri[5] && con->uri[6]) | |
1652 | cupsdSetString(&con->options, con->uri + 5); | |
1653 | else | |
1654 | cupsdSetString(&con->options, NULL); | |
ef416fc2 | 1655 | } |
1656 | else | |
1657 | { | |
1658 | cupsdSetStringf(&con->command, "%s/cgi-bin/help.cgi", | |
1659 | ServerBin); | |
ef416fc2 | 1660 | |
b423cd4c | 1661 | if (con->uri[5] && con->uri[6]) |
1662 | cupsdSetString(&con->options, con->uri + 5); | |
1663 | else | |
1664 | cupsdSetString(&con->options, NULL); | |
1665 | } | |
ef416fc2 | 1666 | |
ef416fc2 | 1667 | if (con->http.version <= HTTP_1_0) |
1668 | con->http.keep_alive = HTTP_KEEPALIVE_OFF; | |
1669 | } | |
1670 | else | |
1671 | { | |
1672 | /* | |
1673 | * POST to a file... | |
1674 | */ | |
1675 | ||
1676 | if ((filename = get_file(con, &filestats, buf, | |
1677 | sizeof(buf))) == NULL) | |
1678 | { | |
5bd77a73 | 1679 | if (!cupsdSendError(con, HTTP_NOT_FOUND, CUPSD_AUTH_NONE)) |
f7deaa1a | 1680 | { |
1681 | cupsdCloseClient(con); | |
1682 | return; | |
1683 | } | |
ef416fc2 | 1684 | |
1685 | break; | |
1686 | } | |
1687 | ||
4400e98d | 1688 | type = mimeFileType(MimeDatabase, filename, NULL, NULL); |
ef416fc2 | 1689 | |
e1d6a774 | 1690 | if (!is_cgi(con, filename, &filestats, type)) |
ef416fc2 | 1691 | { |
1692 | /* | |
1693 | * Only POST to CGI's... | |
1694 | */ | |
1695 | ||
5bd77a73 | 1696 | if (!cupsdSendError(con, HTTP_UNAUTHORIZED, CUPSD_AUTH_NONE)) |
f7deaa1a | 1697 | { |
1698 | cupsdCloseClient(con); | |
1699 | return; | |
1700 | } | |
ef416fc2 | 1701 | } |
1702 | } | |
1703 | break; | |
1704 | ||
cb7f98ee | 1705 | case HTTP_STATE_PUT_RECV : |
ef416fc2 | 1706 | /* |
1707 | * Validate the resource name... | |
1708 | */ | |
1709 | ||
c41769ff | 1710 | if (strcmp(con->uri, "/admin/conf/cupsd.conf")) |
ef416fc2 | 1711 | { |
1712 | /* | |
c41769ff | 1713 | * PUT can only be done to the cupsd.conf file... |
ef416fc2 | 1714 | */ |
1715 | ||
bf3816c7 | 1716 | cupsdLogMessage(CUPSD_LOG_ERROR, |
c41769ff | 1717 | "[Client %d] Disallowed PUT request for \"%s\".", |
85dda01c | 1718 | con->http.fd, con->uri); |
bf3816c7 | 1719 | |
5bd77a73 | 1720 | if (!cupsdSendError(con, HTTP_FORBIDDEN, CUPSD_AUTH_NONE)) |
f7deaa1a | 1721 | { |
1722 | cupsdCloseClient(con); | |
1723 | return; | |
1724 | } | |
ef416fc2 | 1725 | |
1726 | break; | |
1727 | } | |
1728 | ||
1729 | /* | |
1730 | * See if the PUT request includes a Content-Length field, and if | |
1731 | * so check the length against any limits that are set... | |
1732 | */ | |
1733 | ||
ef416fc2 | 1734 | if (con->http.fields[HTTP_FIELD_CONTENT_LENGTH][0] && |
1735 | MaxRequestSize > 0 && | |
1736 | con->http.data_remaining > MaxRequestSize) | |
1737 | { | |
1738 | /* | |
1739 | * Request too large... | |
1740 | */ | |
1741 | ||
5bd77a73 | 1742 | if (!cupsdSendError(con, HTTP_REQUEST_TOO_LARGE, CUPSD_AUTH_NONE)) |
f7deaa1a | 1743 | { |
1744 | cupsdCloseClient(con); | |
1745 | return; | |
1746 | } | |
ef416fc2 | 1747 | |
1748 | break; | |
1749 | } | |
1750 | else if (con->http.data_remaining < 0) | |
1751 | { | |
1752 | /* | |
1753 | * Negative content lengths are invalid! | |
1754 | */ | |
1755 | ||
5bd77a73 | 1756 | if (!cupsdSendError(con, HTTP_BAD_REQUEST, CUPSD_AUTH_NONE)) |
f7deaa1a | 1757 | { |
1758 | cupsdCloseClient(con); | |
1759 | return; | |
1760 | } | |
ef416fc2 | 1761 | |
1762 | break; | |
1763 | } | |
1764 | ||
1765 | /* | |
1766 | * Open a temporary file to hold the request... | |
1767 | */ | |
1768 | ||
1769 | cupsdSetStringf(&con->filename, "%s/%08x", RequestRoot, | |
1770 | request_id ++); | |
1771 | con->file = open(con->filename, O_WRONLY | O_CREAT | O_TRUNC, 0640); | |
1772 | ||
ef416fc2 | 1773 | if (con->file < 0) |
1774 | { | |
a4924f6c | 1775 | cupsdLogMessage(CUPSD_LOG_ERROR, |
85dda01c MS |
1776 | "[Client %d] Unable to create request file " |
1777 | "\"%s\": %s", con->http.fd, con->filename, | |
1778 | strerror(errno)); | |
a4924f6c | 1779 | |
5bd77a73 | 1780 | if (!cupsdSendError(con, HTTP_REQUEST_TOO_LARGE, CUPSD_AUTH_NONE)) |
f7deaa1a | 1781 | { |
1782 | cupsdCloseClient(con); | |
1783 | return; | |
1784 | } | |
ef416fc2 | 1785 | } |
1786 | ||
1787 | fchmod(con->file, 0640); | |
1788 | fchown(con->file, RunUser, Group); | |
1789 | fcntl(con->file, F_SETFD, fcntl(con->file, F_GETFD) | FD_CLOEXEC); | |
1790 | break; | |
1791 | ||
cb7f98ee MS |
1792 | case HTTP_STATE_DELETE : |
1793 | case HTTP_STATE_TRACE : | |
5bd77a73 | 1794 | cupsdSendError(con, HTTP_NOT_IMPLEMENTED, CUPSD_AUTH_NONE); |
f7deaa1a | 1795 | cupsdCloseClient(con); |
1796 | return; | |
ef416fc2 | 1797 | |
cb7f98ee | 1798 | case HTTP_STATE_HEAD : |
ef416fc2 | 1799 | if (!strncmp(con->uri, "/printers/", 10) && |
1800 | !strcmp(con->uri + strlen(con->uri) - 4, ".ppd")) | |
1801 | { | |
1802 | /* | |
1803 | * Send PPD file - get the real printer name since printer | |
1804 | * names are not case sensitive but filenames can be... | |
1805 | */ | |
1806 | ||
1807 | con->uri[strlen(con->uri) - 4] = '\0'; /* Drop ".ppd" */ | |
1808 | ||
1809 | if ((p = cupsdFindPrinter(con->uri + 10)) != NULL) | |
1810 | snprintf(con->uri, sizeof(con->uri), "/ppd/%s.ppd", p->name); | |
1811 | else | |
1812 | { | |
5bd77a73 | 1813 | if (!cupsdSendError(con, HTTP_NOT_FOUND, CUPSD_AUTH_NONE)) |
f7deaa1a | 1814 | { |
1815 | cupsdCloseClient(con); | |
1816 | return; | |
1817 | } | |
ef416fc2 | 1818 | |
1819 | break; | |
1820 | } | |
1821 | } | |
229681c1 MS |
1822 | else if (!strncmp(con->uri, "/printers/", 10) && |
1823 | !strcmp(con->uri + strlen(con->uri) - 4, ".png")) | |
1824 | { | |
1825 | /* | |
1826 | * Send PNG file - get the real printer name since printer | |
1827 | * names are not case sensitive but filenames can be... | |
1828 | */ | |
1829 | ||
1830 | con->uri[strlen(con->uri) - 4] = '\0'; /* Drop ".ppd" */ | |
1831 | ||
1832 | if ((p = cupsdFindPrinter(con->uri + 10)) != NULL) | |
1833 | snprintf(con->uri, sizeof(con->uri), "/icons/%s.png", p->name); | |
1834 | else | |
1835 | { | |
1836 | if (!cupsdSendError(con, HTTP_NOT_FOUND, CUPSD_AUTH_NONE)) | |
1837 | { | |
1838 | cupsdCloseClient(con); | |
1839 | return; | |
1840 | } | |
1841 | ||
1842 | break; | |
1843 | } | |
1844 | } | |
1845 | else if (!WebInterface) | |
1846 | { | |
1847 | if (!cupsdSendHeader(con, HTTP_OK, line, CUPSD_AUTH_NONE)) | |
1848 | { | |
1849 | cupsdCloseClient(con); | |
1850 | return; | |
1851 | } | |
1852 | ||
1853 | if (httpPrintf(HTTP(con), "\r\n") < 0) | |
1854 | { | |
1855 | cupsdCloseClient(con); | |
1856 | return; | |
1857 | } | |
1858 | ||
1859 | if (cupsdFlushHeader(con) < 0) | |
1860 | { | |
1861 | cupsdCloseClient(con); | |
1862 | return; | |
1863 | } | |
1864 | ||
a469f8a5 MS |
1865 | con->http.state = HTTP_STATE_WAITING; |
1866 | DEBUG_puts("cupsdReadClient: Set state to HTTP_STATE_WAITING " | |
1867 | "after HEAD."); | |
229681c1 MS |
1868 | break; |
1869 | } | |
ef416fc2 | 1870 | |
1871 | if ((!strncmp(con->uri, "/admin", 6) && | |
1872 | strncmp(con->uri, "/admin/conf/", 12) && | |
1873 | strncmp(con->uri, "/admin/log/", 11)) || | |
1874 | !strncmp(con->uri, "/printers", 9) || | |
1875 | !strncmp(con->uri, "/classes", 8) || | |
1876 | !strncmp(con->uri, "/help", 5) || | |
1877 | !strncmp(con->uri, "/jobs", 5)) | |
1878 | { | |
1879 | /* | |
1880 | * CGI output... | |
1881 | */ | |
1882 | ||
5bd77a73 | 1883 | if (!cupsdSendHeader(con, HTTP_OK, "text/html", CUPSD_AUTH_NONE)) |
f7deaa1a | 1884 | { |
1885 | cupsdCloseClient(con); | |
1886 | return; | |
1887 | } | |
ef416fc2 | 1888 | |
1889 | if (httpPrintf(HTTP(con), "\r\n") < 0) | |
f7deaa1a | 1890 | { |
1891 | cupsdCloseClient(con); | |
1892 | return; | |
1893 | } | |
ef416fc2 | 1894 | |
07725fee | 1895 | if (cupsdFlushHeader(con) < 0) |
f7deaa1a | 1896 | { |
1897 | cupsdCloseClient(con); | |
1898 | return; | |
1899 | } | |
d09495fa | 1900 | |
ef416fc2 | 1901 | cupsdLogRequest(con, HTTP_OK); |
1902 | } | |
1903 | else if ((!strncmp(con->uri, "/admin/conf/", 12) && | |
1904 | (strchr(con->uri + 12, '/') || | |
1905 | strlen(con->uri) == 12)) || | |
1906 | (!strncmp(con->uri, "/admin/log/", 11) && | |
1907 | (strchr(con->uri + 11, '/') || | |
1908 | strlen(con->uri) == 11))) | |
1909 | { | |
1910 | /* | |
1911 | * HEAD can only be done to configuration files under | |
1912 | * /admin/conf... | |
1913 | */ | |
1914 | ||
bf3816c7 | 1915 | cupsdLogMessage(CUPSD_LOG_ERROR, |
85dda01c MS |
1916 | "[Client %d] Request for subdirectory \"%s\".", |
1917 | con->http.fd, con->uri); | |
bf3816c7 | 1918 | |
5bd77a73 | 1919 | if (!cupsdSendError(con, HTTP_FORBIDDEN, CUPSD_AUTH_NONE)) |
f7deaa1a | 1920 | { |
1921 | cupsdCloseClient(con); | |
1922 | return; | |
1923 | } | |
ef416fc2 | 1924 | |
1925 | break; | |
1926 | } | |
1927 | else if ((filename = get_file(con, &filestats, buf, | |
1928 | sizeof(buf))) == NULL) | |
1929 | { | |
f11a948a MS |
1930 | if (!cupsdSendHeader(con, HTTP_NOT_FOUND, "text/html", |
1931 | CUPSD_AUTH_NONE)) | |
f7deaa1a | 1932 | { |
1933 | cupsdCloseClient(con); | |
1934 | return; | |
1935 | } | |
ef416fc2 | 1936 | |
1937 | cupsdLogRequest(con, HTTP_NOT_FOUND); | |
1938 | } | |
1939 | else if (!check_if_modified(con, &filestats)) | |
1940 | { | |
5bd77a73 | 1941 | if (!cupsdSendError(con, HTTP_NOT_MODIFIED, CUPSD_AUTH_NONE)) |
f7deaa1a | 1942 | { |
1943 | cupsdCloseClient(con); | |
1944 | return; | |
1945 | } | |
ef416fc2 | 1946 | |
1947 | cupsdLogRequest(con, HTTP_NOT_MODIFIED); | |
1948 | } | |
1949 | else | |
1950 | { | |
1951 | /* | |
1952 | * Serve a file... | |
1953 | */ | |
1954 | ||
4400e98d | 1955 | type = mimeFileType(MimeDatabase, filename, NULL, NULL); |
ef416fc2 | 1956 | if (type == NULL) |
5a9febac | 1957 | strlcpy(line, "text/plain", sizeof(line)); |
ef416fc2 | 1958 | else |
1959 | snprintf(line, sizeof(line), "%s/%s", type->super, type->type); | |
1960 | ||
5bd77a73 | 1961 | if (!cupsdSendHeader(con, HTTP_OK, line, CUPSD_AUTH_NONE)) |
f7deaa1a | 1962 | { |
1963 | cupsdCloseClient(con); | |
1964 | return; | |
1965 | } | |
ef416fc2 | 1966 | |
1967 | if (httpPrintf(HTTP(con), "Last-Modified: %s\r\n", | |
1968 | httpGetDateString(filestats.st_mtime)) < 0) | |
f7deaa1a | 1969 | { |
1970 | cupsdCloseClient(con); | |
1971 | return; | |
1972 | } | |
ef416fc2 | 1973 | |
1974 | if (httpPrintf(HTTP(con), "Content-Length: %lu\r\n", | |
1975 | (unsigned long)filestats.st_size) < 0) | |
f7deaa1a | 1976 | { |
1977 | cupsdCloseClient(con); | |
1978 | return; | |
1979 | } | |
ef416fc2 | 1980 | |
1981 | cupsdLogRequest(con, HTTP_OK); | |
1982 | } | |
1983 | ||
1984 | if (httpPrintf(HTTP(con), "\r\n") < 0) | |
f7deaa1a | 1985 | { |
1986 | cupsdCloseClient(con); | |
1987 | return; | |
1988 | } | |
ef416fc2 | 1989 | |
07725fee | 1990 | if (cupsdFlushHeader(con) < 0) |
f7deaa1a | 1991 | { |
1992 | cupsdCloseClient(con); | |
1993 | return; | |
1994 | } | |
d09495fa | 1995 | |
a469f8a5 MS |
1996 | con->http.state = HTTP_STATE_WAITING; |
1997 | DEBUG_puts("cupsdReadClient: Set state to HTTP_STATE_WAITING " | |
1998 | "after HEAD."); | |
ef416fc2 | 1999 | break; |
2000 | ||
2001 | default : | |
2002 | break; /* Anti-compiler-warning-code */ | |
2003 | } | |
2004 | } | |
2005 | } | |
2006 | ||
2007 | /* | |
2008 | * Handle any incoming data... | |
2009 | */ | |
2010 | ||
2011 | switch (con->http.state) | |
2012 | { | |
cb7f98ee | 2013 | case HTTP_STATE_PUT_RECV : |
f7deaa1a | 2014 | do |
ef416fc2 | 2015 | { |
f7deaa1a | 2016 | if ((bytes = httpRead2(HTTP(con), line, sizeof(line))) < 0) |
2017 | { | |
f11a948a MS |
2018 | if (con->http.error && con->http.error != EPIPE) |
2019 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
cb7f98ee | 2020 | "[Client %d] HTTP_STATE_PUT_RECV Closing for error " |
f11a948a MS |
2021 | "%d (%s)", con->http.fd, con->http.error, |
2022 | strerror(con->http.error)); | |
2023 | else | |
2024 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
cb7f98ee | 2025 | "[Client %d] HTTP_STATE_PUT_RECV Closing on EOF", |
f11a948a MS |
2026 | con->http.fd); |
2027 | ||
f7deaa1a | 2028 | cupsdCloseClient(con); |
2029 | return; | |
2030 | } | |
2031 | else if (bytes > 0) | |
2032 | { | |
2033 | con->bytes += bytes; | |
ef416fc2 | 2034 | |
f7deaa1a | 2035 | if (write(con->file, line, bytes) < bytes) |
2036 | { | |
2037 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
85dda01c MS |
2038 | "[Client %d] Unable to write %d bytes to " |
2039 | "\"%s\": %s", con->http.fd, bytes, con->filename, | |
2040 | strerror(errno)); | |
ef416fc2 | 2041 | |
f7deaa1a | 2042 | close(con->file); |
2043 | con->file = -1; | |
2044 | unlink(con->filename); | |
2045 | cupsdClearString(&con->filename); | |
ef416fc2 | 2046 | |
5bd77a73 | 2047 | if (!cupsdSendError(con, HTTP_REQUEST_TOO_LARGE, CUPSD_AUTH_NONE)) |
f7deaa1a | 2048 | { |
2049 | cupsdCloseClient(con); | |
2050 | return; | |
2051 | } | |
2052 | } | |
ef416fc2 | 2053 | } |
f7deaa1a | 2054 | } |
cb7f98ee | 2055 | while (con->http.state == HTTP_STATE_PUT_RECV && data_ready(con)); |
ef416fc2 | 2056 | |
cb7f98ee | 2057 | if (con->http.state == HTTP_STATE_STATUS) |
ef416fc2 | 2058 | { |
2059 | /* | |
2060 | * End of file, see how big it is... | |
2061 | */ | |
2062 | ||
2063 | fstat(con->file, &filestats); | |
2064 | ||
ef416fc2 | 2065 | close(con->file); |
2066 | con->file = -1; | |
2067 | ||
2068 | if (filestats.st_size > MaxRequestSize && | |
2069 | MaxRequestSize > 0) | |
2070 | { | |
2071 | /* | |
2072 | * Request is too big; remove it and send an error... | |
2073 | */ | |
2074 | ||
ef416fc2 | 2075 | unlink(con->filename); |
2076 | cupsdClearString(&con->filename); | |
2077 | ||
5bd77a73 | 2078 | if (!cupsdSendError(con, HTTP_REQUEST_TOO_LARGE, CUPSD_AUTH_NONE)) |
f7deaa1a | 2079 | { |
2080 | cupsdCloseClient(con); | |
2081 | return; | |
2082 | } | |
ef416fc2 | 2083 | } |
2084 | ||
2085 | /* | |
2086 | * Install the configuration file... | |
2087 | */ | |
2088 | ||
c41769ff | 2089 | status = install_cupsd_conf(con); |
ef416fc2 | 2090 | |
2091 | /* | |
2092 | * Return the status to the client... | |
2093 | */ | |
2094 | ||
5bd77a73 | 2095 | if (!cupsdSendError(con, status, CUPSD_AUTH_NONE)) |
f7deaa1a | 2096 | { |
2097 | cupsdCloseClient(con); | |
2098 | return; | |
2099 | } | |
ef416fc2 | 2100 | } |
2101 | break; | |
2102 | ||
cb7f98ee | 2103 | case HTTP_STATE_POST_RECV : |
f7deaa1a | 2104 | do |
ef416fc2 | 2105 | { |
f11a948a | 2106 | if (con->request && con->file < 0) |
ef416fc2 | 2107 | { |
f7deaa1a | 2108 | /* |
2109 | * Grab any request data from the connection... | |
2110 | */ | |
ef416fc2 | 2111 | |
f7deaa1a | 2112 | if ((ipp_state = ippRead(&(con->http), con->request)) == IPP_ERROR) |
ef416fc2 | 2113 | { |
f7deaa1a | 2114 | cupsdLogMessage(CUPSD_LOG_ERROR, |
85dda01c MS |
2115 | "[Client %d] IPP read error: %s", con->http.fd, |
2116 | cupsLastErrorString()); | |
f7deaa1a | 2117 | |
5bd77a73 | 2118 | cupsdSendError(con, HTTP_BAD_REQUEST, CUPSD_AUTH_NONE); |
f7deaa1a | 2119 | cupsdCloseClient(con); |
2120 | return; | |
ef416fc2 | 2121 | } |
f7deaa1a | 2122 | else if (ipp_state != IPP_DATA) |
2123 | { | |
cb7f98ee | 2124 | if (con->http.state == HTTP_STATE_POST_SEND) |
f7deaa1a | 2125 | { |
5bd77a73 | 2126 | cupsdSendError(con, HTTP_BAD_REQUEST, CUPSD_AUTH_NONE); |
f7deaa1a | 2127 | cupsdCloseClient(con); |
2128 | return; | |
2129 | } | |
ef416fc2 | 2130 | |
f7deaa1a | 2131 | break; |
2132 | } | |
2133 | else | |
f11a948a | 2134 | { |
85dda01c | 2135 | cupsdLogMessage(CUPSD_LOG_DEBUG, "[Client %d] %d.%d %s %d", |
f11a948a MS |
2136 | con->http.fd, con->request->request.op.version[0], |
2137 | con->request->request.op.version[1], | |
2138 | ippOpString(con->request->request.op.operation_id), | |
2139 | con->request->request.op.request_id); | |
f7deaa1a | 2140 | con->bytes += ippLength(con->request); |
f11a948a | 2141 | } |
f7deaa1a | 2142 | } |
ef416fc2 | 2143 | |
cb7f98ee | 2144 | if (con->file < 0 && con->http.state != HTTP_STATE_POST_SEND) |
f7deaa1a | 2145 | { |
2146 | /* | |
2147 | * Create a file as needed for the request data... | |
2148 | */ | |
ef416fc2 | 2149 | |
f11a948a MS |
2150 | cupsdSetStringf(&con->filename, "%s/%08x", RequestRoot, |
2151 | request_id ++); | |
f7deaa1a | 2152 | con->file = open(con->filename, O_WRONLY | O_CREAT | O_TRUNC, 0640); |
ef416fc2 | 2153 | |
f7deaa1a | 2154 | if (con->file < 0) |
2155 | { | |
a4924f6c | 2156 | cupsdLogMessage(CUPSD_LOG_ERROR, |
85dda01c MS |
2157 | "[Client %d] Unable to create request file " |
2158 | "\"%s\": %s", con->http.fd, con->filename, | |
2159 | strerror(errno)); | |
a4924f6c | 2160 | |
5bd77a73 | 2161 | if (!cupsdSendError(con, HTTP_REQUEST_TOO_LARGE, CUPSD_AUTH_NONE)) |
f7deaa1a | 2162 | { |
2163 | cupsdCloseClient(con); | |
2164 | return; | |
2165 | } | |
2166 | } | |
ef416fc2 | 2167 | |
f7deaa1a | 2168 | fchmod(con->file, 0640); |
2169 | fchown(con->file, RunUser, Group); | |
2170 | fcntl(con->file, F_SETFD, fcntl(con->file, F_GETFD) | FD_CLOEXEC); | |
2171 | } | |
ef416fc2 | 2172 | |
cb7f98ee | 2173 | if (con->http.state != HTTP_STATE_POST_SEND) |
ef416fc2 | 2174 | { |
cb7f98ee MS |
2175 | if (!httpWait(HTTP(con), 0)) |
2176 | return; | |
2177 | else if ((bytes = httpRead2(HTTP(con), line, sizeof(line))) < 0) | |
f7deaa1a | 2178 | { |
f11a948a MS |
2179 | if (con->http.error && con->http.error != EPIPE) |
2180 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
cb7f98ee | 2181 | "[Client %d] HTTP_STATE_POST_SEND Closing for " |
f11a948a MS |
2182 | "error %d (%s)", con->http.fd, con->http.error, |
2183 | strerror(con->http.error)); | |
2184 | else | |
2185 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
cb7f98ee | 2186 | "[Client %d] HTTP_STATE_POST_SEND Closing on EOF", |
f11a948a MS |
2187 | con->http.fd); |
2188 | ||
f7deaa1a | 2189 | cupsdCloseClient(con); |
2190 | return; | |
2191 | } | |
2192 | else if (bytes > 0) | |
2193 | { | |
2194 | con->bytes += bytes; | |
ef416fc2 | 2195 | |
f7deaa1a | 2196 | if (write(con->file, line, bytes) < bytes) |
2197 | { | |
2198 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
85dda01c MS |
2199 | "[Client %d] Unable to write %d bytes to " |
2200 | "\"%s\": %s", con->http.fd, bytes, | |
2201 | con->filename, strerror(errno)); | |
ef416fc2 | 2202 | |
f7deaa1a | 2203 | close(con->file); |
2204 | con->file = -1; | |
2205 | unlink(con->filename); | |
2206 | cupsdClearString(&con->filename); | |
ef416fc2 | 2207 | |
f11a948a MS |
2208 | if (!cupsdSendError(con, HTTP_REQUEST_TOO_LARGE, |
2209 | CUPSD_AUTH_NONE)) | |
f7deaa1a | 2210 | { |
2211 | cupsdCloseClient(con); | |
2212 | return; | |
2213 | } | |
2214 | } | |
2215 | } | |
cb7f98ee | 2216 | else if (con->http.state == HTTP_STATE_POST_RECV) |
f7deaa1a | 2217 | return; |
cb7f98ee | 2218 | else if (con->http.state != HTTP_STATE_POST_SEND) |
f7deaa1a | 2219 | { |
f11a948a | 2220 | cupsdLogMessage(CUPSD_LOG_DEBUG, |
85dda01c | 2221 | "[Client %d] Closing on unexpected state %s.", |
a469f8a5 | 2222 | con->http.fd, http_states[con->http.state + 1]); |
f7deaa1a | 2223 | cupsdCloseClient(con); |
2224 | return; | |
ef416fc2 | 2225 | } |
2226 | } | |
f7deaa1a | 2227 | } |
cb7f98ee | 2228 | while (con->http.state == HTTP_STATE_POST_RECV && data_ready(con)); |
ef416fc2 | 2229 | |
cb7f98ee | 2230 | if (con->http.state == HTTP_STATE_POST_SEND) |
ef416fc2 | 2231 | { |
2232 | if (con->file >= 0) | |
2233 | { | |
2234 | fstat(con->file, &filestats); | |
2235 | ||
ef416fc2 | 2236 | close(con->file); |
2237 | con->file = -1; | |
2238 | ||
2239 | if (filestats.st_size > MaxRequestSize && | |
2240 | MaxRequestSize > 0) | |
2241 | { | |
2242 | /* | |
2243 | * Request is too big; remove it and send an error... | |
2244 | */ | |
2245 | ||
ef416fc2 | 2246 | unlink(con->filename); |
2247 | cupsdClearString(&con->filename); | |
2248 | ||
2249 | if (con->request) | |
2250 | { | |
2251 | /* | |
2252 | * Delete any IPP request data... | |
2253 | */ | |
2254 | ||
2255 | ippDelete(con->request); | |
2256 | con->request = NULL; | |
2257 | } | |
2258 | ||
5bd77a73 | 2259 | if (!cupsdSendError(con, HTTP_REQUEST_TOO_LARGE, CUPSD_AUTH_NONE)) |
f7deaa1a | 2260 | { |
2261 | cupsdCloseClient(con); | |
2262 | return; | |
2263 | } | |
ef416fc2 | 2264 | } |
f8b3a85b MS |
2265 | else if (filestats.st_size == 0) |
2266 | { | |
2267 | /* | |
2268 | * Don't allow empty file... | |
2269 | */ | |
2270 | ||
2271 | unlink(con->filename); | |
2272 | cupsdClearString(&con->filename); | |
2273 | } | |
ef416fc2 | 2274 | |
2275 | if (con->command) | |
2276 | { | |
2277 | if (!cupsdSendCommand(con, con->command, con->options, 0)) | |
2278 | { | |
5bd77a73 | 2279 | if (!cupsdSendError(con, HTTP_NOT_FOUND, CUPSD_AUTH_NONE)) |
f7deaa1a | 2280 | { |
2281 | cupsdCloseClient(con); | |
2282 | return; | |
2283 | } | |
ef416fc2 | 2284 | } |
2285 | else | |
2286 | cupsdLogRequest(con, HTTP_OK); | |
2287 | } | |
2288 | } | |
2289 | ||
2290 | if (con->request) | |
f7deaa1a | 2291 | { |
2292 | cupsdProcessIPPRequest(con); | |
bc44d920 | 2293 | |
2294 | if (con->filename) | |
2295 | { | |
bc44d920 | 2296 | unlink(con->filename); |
2297 | cupsdClearString(&con->filename); | |
2298 | } | |
2299 | ||
f7deaa1a | 2300 | return; |
2301 | } | |
ef416fc2 | 2302 | } |
2303 | break; | |
2304 | ||
2305 | default : | |
2306 | break; /* Anti-compiler-warning-code */ | |
2307 | } | |
2308 | ||
cb7f98ee | 2309 | if (con->http.state == HTTP_STATE_WAITING) |
3dfe78b3 MS |
2310 | { |
2311 | if (!con->http.keep_alive) | |
f11a948a MS |
2312 | { |
2313 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
85dda01c | 2314 | "[Client %d] Closing because Keep-Alive disabled", |
f11a948a | 2315 | con->http.fd); |
3dfe78b3 | 2316 | cupsdCloseClient(con); |
f11a948a | 2317 | } |
3dfe78b3 MS |
2318 | else |
2319 | { | |
2320 | cupsArrayRemove(ActiveClients, con); | |
2321 | cupsdSetBusyState(); | |
2322 | } | |
2323 | } | |
ef416fc2 | 2324 | } |
2325 | ||
2326 | ||
2327 | /* | |
2328 | * 'cupsdSendCommand()' - Send output from a command via HTTP. | |
2329 | */ | |
2330 | ||
2331 | int /* O - 1 on success, 0 on failure */ | |
2332 | cupsdSendCommand( | |
2333 | cupsd_client_t *con, /* I - Client connection */ | |
2334 | char *command, /* I - Command to run */ | |
2335 | char *options, /* I - Command-line options */ | |
2336 | int root) /* I - Run as root? */ | |
2337 | { | |
2338 | int fd; /* Standard input file descriptor */ | |
2339 | ||
2340 | ||
2341 | if (con->filename) | |
ed486911 | 2342 | { |
ef416fc2 | 2343 | fd = open(con->filename, O_RDONLY); |
ef416fc2 | 2344 | |
ed486911 | 2345 | if (fd < 0) |
2346 | { | |
2347 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
85dda01c | 2348 | "[Client %d] Unable to open \"%s\" for reading: %s", |
ed486911 | 2349 | con->http.fd, con->filename ? con->filename : "/dev/null", |
2350 | strerror(errno)); | |
2351 | return (0); | |
2352 | } | |
ef416fc2 | 2353 | |
ed486911 | 2354 | fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC); |
2355 | } | |
2356 | else | |
2357 | fd = -1; | |
ef416fc2 | 2358 | |
2359 | con->pipe_pid = pipe_command(con, fd, &(con->file), command, options, root); | |
2360 | ||
ed486911 | 2361 | if (fd >= 0) |
2362 | close(fd); | |
ef416fc2 | 2363 | |
85dda01c MS |
2364 | cupsdLogMessage(CUPSD_LOG_INFO, "[Client %d] Started \"%s\" (pid=%d)", |
2365 | con->http.fd, command, con->pipe_pid); | |
ef416fc2 | 2366 | |
85dda01c MS |
2367 | cupsdLogMessage(CUPSD_LOG_DEBUG, "[Client %d] file=%d", con->http.fd, |
2368 | con->file); | |
ef416fc2 | 2369 | |
2370 | if (con->pipe_pid == 0) | |
2371 | return (0); | |
2372 | ||
2373 | fcntl(con->file, F_SETFD, fcntl(con->file, F_GETFD) | FD_CLOEXEC); | |
2374 | ||
f899b121 | 2375 | cupsdAddSelect(con->file, (cupsd_selfunc_t)write_pipe, NULL, con); |
ef416fc2 | 2376 | |
2377 | con->sent_header = 0; | |
2378 | con->file_ready = 0; | |
2379 | con->got_fields = 0; | |
68b10830 | 2380 | con->header_used = 0; |
ef416fc2 | 2381 | |
2382 | return (1); | |
2383 | } | |
2384 | ||
2385 | ||
2386 | /* | |
2387 | * 'cupsdSendError()' - Send an error message via HTTP. | |
2388 | */ | |
2389 | ||
2390 | int /* O - 1 if successful, 0 otherwise */ | |
2391 | cupsdSendError(cupsd_client_t *con, /* I - Connection */ | |
f899b121 | 2392 | http_status_t code, /* I - Error code */ |
2393 | int auth_type)/* I - Authentication type */ | |
ef416fc2 | 2394 | { |
b9faaae1 | 2395 | cupsdLogMessage(CUPSD_LOG_DEBUG2, |
85dda01c | 2396 | "[Client %d] cupsdSendError code=%d, auth_type=%d", |
b9faaae1 MS |
2397 | con->http.fd, code, auth_type); |
2398 | ||
4744bd90 | 2399 | #ifdef HAVE_SSL |
2400 | /* | |
2401 | * Force client to upgrade for authentication if that is how the | |
2402 | * server is configured... | |
2403 | */ | |
2404 | ||
2405 | if (code == HTTP_UNAUTHORIZED && | |
2406 | DefaultEncryption == HTTP_ENCRYPT_REQUIRED && | |
88f9aafc | 2407 | _cups_strcasecmp(con->http.hostname, "localhost") && |
4744bd90 | 2408 | !con->http.tls) |
2409 | { | |
4744bd90 | 2410 | code = HTTP_UPGRADE_REQUIRED; |
2411 | } | |
2412 | #endif /* HAVE_SSL */ | |
2413 | ||
ef416fc2 | 2414 | /* |
2415 | * Put the request in the access_log file... | |
2416 | */ | |
2417 | ||
2418 | cupsdLogRequest(con, code); | |
2419 | ||
ef416fc2 | 2420 | /* |
2421 | * To work around bugs in some proxies, don't use Keep-Alive for some | |
2422 | * error messages... | |
f7deaa1a | 2423 | * |
2424 | * Kerberos authentication doesn't work without Keep-Alive, so | |
2425 | * never disable it in that case. | |
ef416fc2 | 2426 | */ |
2427 | ||
5bd77a73 | 2428 | if (code >= HTTP_BAD_REQUEST && con->http.auth_type != CUPSD_AUTH_NEGOTIATE) |
ef416fc2 | 2429 | con->http.keep_alive = HTTP_KEEPALIVE_OFF; |
2430 | ||
2431 | /* | |
2432 | * Send an error message back to the client. If the error code is a | |
2433 | * 400 or 500 series, make sure the message contains some text, too! | |
2434 | */ | |
2435 | ||
f899b121 | 2436 | if (!cupsdSendHeader(con, code, NULL, auth_type)) |
ef416fc2 | 2437 | return (0); |
2438 | ||
2439 | #ifdef HAVE_SSL | |
2440 | if (code == HTTP_UPGRADE_REQUIRED) | |
2441 | if (httpPrintf(HTTP(con), "Connection: Upgrade\r\n") < 0) | |
2442 | return (0); | |
2443 | ||
a469f8a5 | 2444 | if (httpPrintf(HTTP(con), "Upgrade: TLS/1.2,TLS/1.1,TLS/1.0\r\n") < 0) |
ef416fc2 | 2445 | return (0); |
2446 | #endif /* HAVE_SSL */ | |
2447 | ||
f7deaa1a | 2448 | if (con->http.version >= HTTP_1_1 && |
2449 | con->http.keep_alive == HTTP_KEEPALIVE_OFF) | |
ef416fc2 | 2450 | { |
2451 | if (httpPrintf(HTTP(con), "Connection: close\r\n") < 0) | |
2452 | return (0); | |
2453 | } | |
2454 | ||
2455 | if (code >= HTTP_BAD_REQUEST) | |
2456 | { | |
2457 | /* | |
2458 | * Send a human-readable error message. | |
2459 | */ | |
2460 | ||
80ca4592 | 2461 | char message[4096], /* Message for user */ |
2462 | urltext[1024], /* URL redirection text */ | |
2463 | redirect[1024]; /* Redirection link */ | |
ef416fc2 | 2464 | const char *text; /* Status-specific text */ |
2465 | ||
80ca4592 | 2466 | |
2467 | redirect[0] = '\0'; | |
2468 | ||
ef416fc2 | 2469 | if (code == HTTP_UNAUTHORIZED) |
2470 | text = _cupsLangString(con->language, | |
2471 | _("Enter your username and password or the " | |
2472 | "root username and password to access this " | |
f7deaa1a | 2473 | "page. If you are using Kerberos authentication, " |
2474 | "make sure you have a valid Kerberos ticket.")); | |
ef416fc2 | 2475 | else if (code == HTTP_UPGRADE_REQUIRED) |
80ca4592 | 2476 | { |
2477 | text = urltext; | |
2478 | ||
2479 | snprintf(urltext, sizeof(urltext), | |
2480 | _cupsLangString(con->language, | |
2481 | _("You must access this page using the URL " | |
2482 | "<A HREF=\"https://%s:%d%s\">" | |
2483 | "https://%s:%d%s</A>.")), | |
2484 | con->servername, con->serverport, con->uri, | |
2485 | con->servername, con->serverport, con->uri); | |
2486 | ||
2487 | snprintf(redirect, sizeof(redirect), | |
b86bc4cf | 2488 | "<META HTTP-EQUIV=\"Refresh\" " |
2489 | "CONTENT=\"3;URL=https://%s:%d%s\">\n", | |
80ca4592 | 2490 | con->servername, con->serverport, con->uri); |
2491 | } | |
229681c1 MS |
2492 | else if (code == HTTP_WEBIF_DISABLED) |
2493 | text = _cupsLangString(con->language, | |
2494 | _("The web interface is currently disabled. Run " | |
2495 | "\"cupsctl WebInterface=yes\" to enable it.")); | |
ef416fc2 | 2496 | else |
2497 | text = ""; | |
2498 | ||
2499 | snprintf(message, sizeof(message), | |
745129be MS |
2500 | "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" " |
2501 | "\"http://www.w3.org/TR/html4/loose.dtd\">\n" | |
ef416fc2 | 2502 | "<HTML>\n" |
2503 | "<HEAD>\n" | |
2504 | "\t<META HTTP-EQUIV=\"Content-Type\" " | |
2505 | "CONTENT=\"text/html; charset=utf-8\">\n" | |
229681c1 | 2506 | "\t<TITLE>%s - " CUPS_SVERSION "</TITLE>\n" |
ef416fc2 | 2507 | "\t<LINK REL=\"STYLESHEET\" TYPE=\"text/css\" " |
2508 | "HREF=\"/cups.css\">\n" | |
80ca4592 | 2509 | "%s" |
ef416fc2 | 2510 | "</HEAD>\n" |
2511 | "<BODY>\n" | |
229681c1 | 2512 | "<H1>%s</H1>\n" |
ef416fc2 | 2513 | "<P>%s</P>\n" |
2514 | "</BODY>\n" | |
2515 | "</HTML>\n", | |
229681c1 | 2516 | httpStatus(code), redirect, httpStatus(code), text); |
ef416fc2 | 2517 | |
2518 | if (httpPrintf(HTTP(con), "Content-Type: text/html; charset=utf-8\r\n") < 0) | |
2519 | return (0); | |
2520 | if (httpPrintf(HTTP(con), "Content-Length: %d\r\n", | |
2521 | (int)strlen(message)) < 0) | |
2522 | return (0); | |
2523 | if (httpPrintf(HTTP(con), "\r\n") < 0) | |
2524 | return (0); | |
2525 | if (httpPrintf(HTTP(con), "%s", message) < 0) | |
2526 | return (0); | |
2527 | } | |
2528 | else if (httpPrintf(HTTP(con), "\r\n") < 0) | |
2529 | return (0); | |
2530 | ||
07725fee | 2531 | if (cupsdFlushHeader(con) < 0) |
2532 | return (0); | |
d09495fa | 2533 | |
a469f8a5 MS |
2534 | con->http.state = HTTP_STATE_WAITING; |
2535 | ||
2536 | DEBUG_puts("cupsdSendError: Set state to HTTP_STATE_WAITING."); | |
ef416fc2 | 2537 | |
2538 | return (1); | |
2539 | } | |
2540 | ||
2541 | ||
ef416fc2 | 2542 | /* |
2543 | * 'cupsdSendHeader()' - Send an HTTP request. | |
2544 | */ | |
2545 | ||
2546 | int /* O - 1 on success, 0 on failure */ | |
f899b121 | 2547 | cupsdSendHeader( |
2548 | cupsd_client_t *con, /* I - Client to send to */ | |
2549 | http_status_t code, /* I - HTTP status code */ | |
2550 | char *type, /* I - MIME type of document */ | |
2551 | int auth_type) /* I - Type of authentication */ | |
ef416fc2 | 2552 | { |
5a738aea | 2553 | char auth_str[1024]; /* Authorization string */ |
f7deaa1a | 2554 | |
2555 | ||
4744bd90 | 2556 | /* |
2557 | * Send the HTTP status header... | |
2558 | */ | |
2559 | ||
b423cd4c | 2560 | if (code == HTTP_CONTINUE) |
2561 | { | |
4744bd90 | 2562 | /* |
2563 | * 100-continue doesn't send any headers... | |
2564 | */ | |
2565 | ||
07725fee | 2566 | return (httpPrintf(HTTP(con), "HTTP/%d.%d 100 Continue\r\n\r\n", |
2567 | con->http.version / 100, con->http.version % 100) > 0); | |
b423cd4c | 2568 | } |
229681c1 MS |
2569 | else if (code == HTTP_WEBIF_DISABLED) |
2570 | { | |
2571 | /* | |
2572 | * Treat our special "web interface is disabled" status as "200 OK" for web | |
2573 | * browsers. | |
2574 | */ | |
2575 | ||
2576 | code = HTTP_OK; | |
2577 | } | |
b423cd4c | 2578 | |
07725fee | 2579 | httpFlushWrite(HTTP(con)); |
2580 | ||
cb7f98ee | 2581 | con->http.data_encoding = HTTP_ENCODING_FIELDS; |
07725fee | 2582 | |
2583 | if (httpPrintf(HTTP(con), "HTTP/%d.%d %d %s\r\n", con->http.version / 100, | |
2584 | con->http.version % 100, code, httpStatus(code)) < 0) | |
2585 | return (0); | |
ef416fc2 | 2586 | if (httpPrintf(HTTP(con), "Date: %s\r\n", httpGetDateString(time(NULL))) < 0) |
2587 | return (0); | |
2588 | if (ServerHeader) | |
2589 | if (httpPrintf(HTTP(con), "Server: %s\r\n", ServerHeader) < 0) | |
2590 | return (0); | |
2591 | if (con->http.keep_alive && con->http.version >= HTTP_1_0) | |
2592 | { | |
2593 | if (httpPrintf(HTTP(con), "Connection: Keep-Alive\r\n") < 0) | |
2594 | return (0); | |
2595 | if (httpPrintf(HTTP(con), "Keep-Alive: timeout=%d\r\n", | |
2596 | KeepAliveTimeout) < 0) | |
2597 | return (0); | |
2598 | } | |
2599 | if (code == HTTP_METHOD_NOT_ALLOWED) | |
e6013cfa | 2600 | if (httpPrintf(HTTP(con), "Allow: GET, HEAD, OPTIONS, POST, PUT\r\n") < 0) |
ef416fc2 | 2601 | return (0); |
2602 | ||
2603 | if (code == HTTP_UNAUTHORIZED) | |
2604 | { | |
5bd77a73 | 2605 | if (auth_type == CUPSD_AUTH_NONE) |
f899b121 | 2606 | { |
5bd77a73 | 2607 | if (!con->best || con->best->type <= CUPSD_AUTH_NONE) |
dcb445bc | 2608 | auth_type = cupsdDefaultAuthType(); |
f899b121 | 2609 | else |
2610 | auth_type = con->best->type; | |
2611 | } | |
f7deaa1a | 2612 | |
2613 | auth_str[0] = '\0'; | |
2614 | ||
5bd77a73 | 2615 | if (auth_type == CUPSD_AUTH_BASIC || auth_type == CUPSD_AUTH_BASICDIGEST) |
f7deaa1a | 2616 | strlcpy(auth_str, "Basic realm=\"CUPS\"", sizeof(auth_str)); |
5bd77a73 | 2617 | else if (auth_type == CUPSD_AUTH_DIGEST) |
f7deaa1a | 2618 | snprintf(auth_str, sizeof(auth_str), "Digest realm=\"CUPS\", nonce=\"%s\"", |
2619 | con->http.hostname); | |
2620 | #ifdef HAVE_GSSAPI | |
eac3a0a0 | 2621 | else if (auth_type == CUPSD_AUTH_NEGOTIATE) |
f7deaa1a | 2622 | strlcpy(auth_str, "Negotiate", sizeof(auth_str)); |
2623 | #endif /* HAVE_GSSAPI */ | |
2624 | ||
e07d4801 | 2625 | if (con->best && auth_type != CUPSD_AUTH_NEGOTIATE && |
88f9aafc | 2626 | !_cups_strcasecmp(con->http.hostname, "localhost")) |
ef416fc2 | 2627 | { |
e07d4801 MS |
2628 | /* |
2629 | * Add a "trc" (try root certification) parameter for local non-Kerberos | |
2630 | * requests when the request requires system group membership - then the | |
2631 | * client knows the root certificate can/should be used. | |
2632 | * | |
f3c17241 | 2633 | * Also, for OS X we also look for @AUTHKEY and add an "authkey" |
e07d4801 MS |
2634 | * parameter as needed... |
2635 | */ | |
2636 | ||
10d09e33 MS |
2637 | char *name, /* Current user name */ |
2638 | *auth_key; /* Auth key buffer */ | |
f7deaa1a | 2639 | size_t auth_size; /* Size of remaining buffer */ |
2640 | ||
f7deaa1a | 2641 | auth_key = auth_str + strlen(auth_str); |
2642 | auth_size = sizeof(auth_str) - (auth_key - auth_str); | |
2643 | ||
10d09e33 MS |
2644 | for (name = (char *)cupsArrayFirst(con->best->names); |
2645 | name; | |
2646 | name = (char *)cupsArrayNext(con->best->names)) | |
f7deaa1a | 2647 | { |
e07d4801 | 2648 | #ifdef HAVE_AUTHORIZATION_H |
88f9aafc | 2649 | if (!_cups_strncasecmp(name, "@AUTHKEY(", 9)) |
f7deaa1a | 2650 | { |
10d09e33 | 2651 | snprintf(auth_key, auth_size, ", authkey=\"%s\"", name + 9); |
f7deaa1a | 2652 | /* end parenthesis is stripped in conf.c */ |
2653 | break; | |
2654 | } | |
e07d4801 MS |
2655 | else |
2656 | #endif /* HAVE_AUTHORIZATION_H */ | |
88f9aafc | 2657 | if (!_cups_strcasecmp(name, "@SYSTEM")) |
f7deaa1a | 2658 | { |
e07d4801 MS |
2659 | #ifdef HAVE_AUTHORIZATION_H |
2660 | if (SystemGroupAuthKey) | |
2661 | snprintf(auth_key, auth_size, | |
07ed0e9a | 2662 | ", authkey=\"%s\"", |
e07d4801 MS |
2663 | SystemGroupAuthKey); |
2664 | else | |
2665 | #else | |
f11a948a | 2666 | strlcpy(auth_key, ", trc=\"y\"", auth_size); |
e07d4801 | 2667 | #endif /* HAVE_AUTHORIZATION_H */ |
f7deaa1a | 2668 | break; |
2669 | } | |
2670 | } | |
ef416fc2 | 2671 | } |
f7deaa1a | 2672 | |
bc44d920 | 2673 | if (auth_str[0]) |
2674 | { | |
68b10830 | 2675 | cupsdLogMessage(CUPSD_LOG_DEBUG, |
85dda01c | 2676 | "[Client %d] WWW-Authenticate: %s", con->http.fd, |
bc44d920 | 2677 | auth_str); |
2678 | ||
2679 | if (httpPrintf(HTTP(con), "WWW-Authenticate: %s\r\n", auth_str) < 0) | |
2680 | return (0); | |
2681 | } | |
ef416fc2 | 2682 | } |
2683 | ||
e1d6a774 | 2684 | if (con->language && strcmp(con->language->language, "C")) |
ef416fc2 | 2685 | { |
2686 | if (httpPrintf(HTTP(con), "Content-Language: %s\r\n", | |
2687 | con->language->language) < 0) | |
2688 | return (0); | |
2689 | } | |
2690 | ||
e1d6a774 | 2691 | if (type) |
ef416fc2 | 2692 | { |
2693 | if (!strcmp(type, "text/html")) | |
2694 | { | |
2695 | if (httpPrintf(HTTP(con), | |
2696 | "Content-Type: text/html; charset=utf-8\r\n") < 0) | |
2697 | return (0); | |
2698 | } | |
2699 | else if (httpPrintf(HTTP(con), "Content-Type: %s\r\n", type) < 0) | |
2700 | return (0); | |
2701 | } | |
2702 | ||
2703 | return (1); | |
2704 | } | |
2705 | ||
2706 | ||
2707 | /* | |
2708 | * 'cupsdUpdateCGI()' - Read status messages from CGI scripts and programs. | |
2709 | */ | |
2710 | ||
2711 | void | |
2712 | cupsdUpdateCGI(void) | |
2713 | { | |
2714 | char *ptr, /* Pointer to end of line in buffer */ | |
2715 | message[1024]; /* Pointer to message text */ | |
2716 | int loglevel; /* Log level for message */ | |
2717 | ||
2718 | ||
2719 | while ((ptr = cupsdStatBufUpdate(CGIStatusBuffer, &loglevel, | |
2720 | message, sizeof(message))) != NULL) | |
f0ab5bff MS |
2721 | { |
2722 | if (loglevel == CUPSD_LOG_INFO) | |
2723 | cupsdLogMessage(CUPSD_LOG_INFO, "%s", message); | |
2724 | ||
ef416fc2 | 2725 | if (!strchr(CGIStatusBuffer->buffer, '\n')) |
2726 | break; | |
f0ab5bff | 2727 | } |
ef416fc2 | 2728 | |
d09495fa | 2729 | if (ptr == NULL && !CGIStatusBuffer->bufused) |
ef416fc2 | 2730 | { |
2731 | /* | |
2732 | * Fatal error on pipe - should never happen! | |
2733 | */ | |
2734 | ||
2735 | cupsdLogMessage(CUPSD_LOG_CRIT, | |
2736 | "cupsdUpdateCGI: error reading from CGI error pipe - %s", | |
2737 | strerror(errno)); | |
2738 | } | |
2739 | } | |
2740 | ||
2741 | ||
2742 | /* | |
2743 | * 'cupsdWriteClient()' - Write data to a client as needed. | |
2744 | */ | |
2745 | ||
f7deaa1a | 2746 | void |
ef416fc2 | 2747 | cupsdWriteClient(cupsd_client_t *con) /* I - Client connection */ |
2748 | { | |
68b10830 MS |
2749 | int bytes, /* Number of bytes written */ |
2750 | field_col; /* Current column */ | |
2751 | char *bufptr, /* Pointer into buffer */ | |
2752 | *bufend; /* Pointer to end of buffer */ | |
ef416fc2 | 2753 | ipp_state_t ipp_state; /* IPP state value */ |
2754 | ||
2755 | ||
ef416fc2 | 2756 | cupsdLogMessage(CUPSD_LOG_DEBUG2, |
85dda01c MS |
2757 | "[Client %d] cupsdWriteClient " |
2758 | "error=%d, " | |
2759 | "used=%d, " | |
2760 | "state=%s, " | |
cb7f98ee | 2761 | "data_encoding=HTTP_ENCODING_%s, " |
85dda01c MS |
2762 | "data_remaining=" CUPS_LLFMT ", " |
2763 | "response=%p(%s), " | |
2764 | "pipe_pid=%d, " | |
2765 | "file=%d", | |
2766 | con->http.fd, con->http.error, con->http.used, | |
a469f8a5 | 2767 | http_states[con->http.state + 1], |
cb7f98ee | 2768 | con->http.data_encoding == HTTP_ENCODING_CHUNKED ? |
85dda01c MS |
2769 | "CHUNKED" : "LENGTH", |
2770 | CUPS_LLCAST con->http.data_remaining, | |
2771 | con->response, | |
2772 | con->response ? ipp_states[con->response->state] : "", | |
2773 | con->pipe_pid, con->file); | |
ef416fc2 | 2774 | |
cb7f98ee MS |
2775 | if (con->http.state != HTTP_STATE_GET_SEND && |
2776 | con->http.state != HTTP_STATE_POST_SEND) | |
8b116e60 MS |
2777 | { |
2778 | /* | |
2779 | * If we get called in the wrong state, then something went wrong with the | |
2780 | * connection and we need to shut it down... | |
2781 | */ | |
2782 | ||
f11a948a | 2783 | cupsdLogMessage(CUPSD_LOG_DEBUG, |
85dda01c | 2784 | "[Client %d] Closing on unexpected HTTP state %s.", |
a469f8a5 | 2785 | con->http.fd, http_states[con->http.state + 1]); |
8b116e60 | 2786 | cupsdCloseClient(con); |
f7deaa1a | 2787 | return; |
8b116e60 | 2788 | } |
f7deaa1a | 2789 | |
2790 | if (con->pipe_pid) | |
2791 | { | |
2792 | /* | |
2793 | * Make sure we select on the CGI output... | |
2794 | */ | |
2795 | ||
f899b121 | 2796 | cupsdAddSelect(con->file, (cupsd_selfunc_t)write_pipe, NULL, con); |
f7deaa1a | 2797 | |
2798 | if (!con->file_ready) | |
2799 | { | |
2800 | /* | |
2801 | * Try again later when there is CGI output available... | |
2802 | */ | |
2803 | ||
2804 | cupsdRemoveSelect(con->http.fd); | |
2805 | return; | |
2806 | } | |
2807 | ||
2808 | con->file_ready = 0; | |
2809 | } | |
ef416fc2 | 2810 | |
b94498cf | 2811 | if (con->response && con->response->state != IPP_DATA) |
ef416fc2 | 2812 | { |
07725fee | 2813 | ipp_state = ippWrite(HTTP(con), con->response); |
b94498cf | 2814 | bytes = ipp_state != IPP_ERROR && |
2815 | (con->file >= 0 || ipp_state != IPP_DATA); | |
ef416fc2 | 2816 | } |
a2326b5b | 2817 | else if ((bytes = read(con->file, con->header + con->header_used, |
68b10830 | 2818 | sizeof(con->header) - con->header_used)) > 0) |
ef416fc2 | 2819 | { |
68b10830 MS |
2820 | con->header_used += bytes; |
2821 | ||
ef416fc2 | 2822 | if (con->pipe_pid && !con->got_fields) |
2823 | { | |
2824 | /* | |
2825 | * Inspect the data for Content-Type and other fields. | |
2826 | */ | |
2827 | ||
68b10830 MS |
2828 | for (bufptr = con->header, bufend = con->header + con->header_used, |
2829 | field_col = 0; | |
2830 | !con->got_fields && bufptr < bufend; | |
2831 | bufptr ++) | |
d1c13e16 | 2832 | { |
ef416fc2 | 2833 | if (*bufptr == '\n') |
2834 | { | |
2835 | /* | |
2836 | * Send line to client... | |
2837 | */ | |
2838 | ||
68b10830 | 2839 | if (bufptr > con->header && bufptr[-1] == '\r') |
ef416fc2 | 2840 | bufptr[-1] = '\0'; |
2841 | *bufptr++ = '\0'; | |
2842 | ||
68b10830 | 2843 | cupsdLogMessage(CUPSD_LOG_DEBUG, "Script header: %s", con->header); |
ef416fc2 | 2844 | |
2845 | if (!con->sent_header) | |
2846 | { | |
2847 | /* | |
2848 | * Handle redirection and CGI status codes... | |
2849 | */ | |
2850 | ||
88f9aafc | 2851 | if (!_cups_strncasecmp(con->header, "Location:", 9)) |
d6ae789d | 2852 | { |
321d8d57 MS |
2853 | if (!cupsdSendHeader(con, HTTP_SEE_OTHER, NULL, CUPSD_AUTH_NONE)) |
2854 | { | |
2855 | cupsdCloseClient(con); | |
2856 | return; | |
2857 | } | |
2858 | ||
07725fee | 2859 | con->sent_header = 2; |
2860 | ||
d6ae789d | 2861 | if (httpPrintf(HTTP(con), "Content-Length: 0\r\n") < 0) |
f7deaa1a | 2862 | return; |
d6ae789d | 2863 | } |
88f9aafc | 2864 | else if (!_cups_strncasecmp(con->header, "Status:", 7)) |
07725fee | 2865 | { |
68b10830 MS |
2866 | cupsdSendError(con, (http_status_t)atoi(con->header + 7), |
2867 | CUPSD_AUTH_NONE); | |
07725fee | 2868 | con->sent_header = 2; |
2869 | } | |
ef416fc2 | 2870 | else |
4744bd90 | 2871 | { |
321d8d57 MS |
2872 | if (!cupsdSendHeader(con, HTTP_OK, NULL, CUPSD_AUTH_NONE)) |
2873 | { | |
2874 | cupsdCloseClient(con); | |
2875 | return; | |
2876 | } | |
2877 | ||
07725fee | 2878 | con->sent_header = 1; |
ef416fc2 | 2879 | |
4744bd90 | 2880 | if (con->http.version == HTTP_1_1) |
2881 | { | |
4744bd90 | 2882 | if (httpPrintf(HTTP(con), "Transfer-Encoding: chunked\r\n") < 0) |
f7deaa1a | 2883 | return; |
4744bd90 | 2884 | } |
2885 | } | |
ef416fc2 | 2886 | } |
2887 | ||
88f9aafc | 2888 | if (_cups_strncasecmp(con->header, "Status:", 7)) |
68b10830 | 2889 | httpPrintf(HTTP(con), "%s\r\n", con->header); |
ef416fc2 | 2890 | |
ef416fc2 | 2891 | /* |
2892 | * Update buffer... | |
2893 | */ | |
2894 | ||
68b10830 | 2895 | con->header_used -= bufptr - con->header; |
d1c13e16 | 2896 | |
68b10830 MS |
2897 | if (con->header_used > 0) |
2898 | memmove(con->header, bufptr, con->header_used); | |
d1c13e16 | 2899 | |
68b10830 | 2900 | bufptr = con->header - 1; |
ef416fc2 | 2901 | |
2902 | /* | |
2903 | * See if the line was empty... | |
2904 | */ | |
2905 | ||
68b10830 | 2906 | if (field_col == 0) |
d09495fa | 2907 | { |
ef416fc2 | 2908 | con->got_fields = 1; |
d09495fa | 2909 | |
07725fee | 2910 | if (cupsdFlushHeader(con) < 0) |
2911 | { | |
2912 | cupsdCloseClient(con); | |
f7deaa1a | 2913 | return; |
07725fee | 2914 | } |
d09495fa | 2915 | |
2916 | if (con->http.version == HTTP_1_1) | |
cb7f98ee | 2917 | con->http.data_encoding = HTTP_ENCODING_CHUNKED; |
d09495fa | 2918 | } |
ef416fc2 | 2919 | else |
68b10830 | 2920 | field_col = 0; |
ef416fc2 | 2921 | } |
2922 | else if (*bufptr != '\r') | |
68b10830 | 2923 | field_col ++; |
d1c13e16 | 2924 | } |
ef416fc2 | 2925 | |
68b10830 | 2926 | if (!con->got_fields) |
47879b8b | 2927 | { |
ef416fc2 | 2928 | con->http.activity = time(NULL); |
47879b8b MS |
2929 | return; |
2930 | } | |
ef416fc2 | 2931 | } |
2932 | ||
68b10830 | 2933 | if (con->header_used > 0) |
ef416fc2 | 2934 | { |
68b10830 | 2935 | if (httpWrite2(HTTP(con), con->header, con->header_used) < 0) |
4744bd90 | 2936 | { |
f11a948a | 2937 | cupsdLogMessage(CUPSD_LOG_DEBUG, |
85dda01c | 2938 | "[Client %d] Closing for error %d (%s)", |
f11a948a MS |
2939 | con->http.fd, con->http.error, |
2940 | strerror(con->http.error)); | |
4744bd90 | 2941 | cupsdCloseClient(con); |
f7deaa1a | 2942 | return; |
4744bd90 | 2943 | } |
ef416fc2 | 2944 | |
cb7f98ee | 2945 | if (con->http.data_encoding == HTTP_ENCODING_CHUNKED) |
01ce6322 | 2946 | httpFlushWrite(HTTP(con)); |
ae71f5de | 2947 | |
68b10830 | 2948 | con->bytes += con->header_used; |
ef416fc2 | 2949 | |
cb7f98ee | 2950 | if (con->http.state == HTTP_STATE_WAITING) |
4744bd90 | 2951 | bytes = 0; |
68b10830 MS |
2952 | else |
2953 | bytes = con->header_used; | |
2954 | ||
2955 | con->header_used = 0; | |
4744bd90 | 2956 | } |
ef416fc2 | 2957 | } |
2958 | ||
8b116e60 | 2959 | if (bytes <= 0 || |
cb7f98ee | 2960 | (con->http.state != HTTP_STATE_GET_SEND && con->http.state != HTTP_STATE_POST_SEND)) |
ef416fc2 | 2961 | { |
38e73f87 | 2962 | if (!con->sent_header && con->pipe_pid) |
94da7e34 MS |
2963 | cupsdSendError(con, HTTP_SERVER_ERROR, CUPSD_AUTH_NONE); |
2964 | else | |
2965 | { | |
2966 | cupsdLogRequest(con, HTTP_OK); | |
ef416fc2 | 2967 | |
94da7e34 | 2968 | httpFlushWrite(HTTP(con)); |
ef416fc2 | 2969 | |
cb7f98ee | 2970 | if (con->http.data_encoding == HTTP_ENCODING_CHUNKED && con->sent_header == 1) |
ef416fc2 | 2971 | { |
94da7e34 MS |
2972 | if (httpWrite2(HTTP(con), "", 0) < 0) |
2973 | { | |
f11a948a | 2974 | cupsdLogMessage(CUPSD_LOG_DEBUG, |
85dda01c | 2975 | "[Client %d] Closing for error %d (%s)", |
f11a948a MS |
2976 | con->http.fd, con->http.error, |
2977 | strerror(con->http.error)); | |
94da7e34 MS |
2978 | cupsdCloseClient(con); |
2979 | return; | |
2980 | } | |
ef416fc2 | 2981 | } |
2982 | } | |
2983 | ||
cb7f98ee | 2984 | con->http.state = HTTP_STATE_WAITING; |
ef416fc2 | 2985 | |
f7deaa1a | 2986 | cupsdAddSelect(con->http.fd, (cupsd_selfunc_t)cupsdReadClient, NULL, con); |
ef416fc2 | 2987 | |
2988 | if (con->file >= 0) | |
2989 | { | |
f7deaa1a | 2990 | cupsdRemoveSelect(con->file); |
ef416fc2 | 2991 | |
2992 | if (con->pipe_pid) | |
2993 | cupsdEndProcess(con->pipe_pid, 0); | |
2994 | ||
ef416fc2 | 2995 | close(con->file); |
2996 | con->file = -1; | |
2997 | con->pipe_pid = 0; | |
2998 | } | |
2999 | ||
3000 | if (con->filename) | |
3001 | { | |
ef416fc2 | 3002 | unlink(con->filename); |
3003 | cupsdClearString(&con->filename); | |
3004 | } | |
3005 | ||
2abf387c | 3006 | if (con->request) |
ef416fc2 | 3007 | { |
3008 | ippDelete(con->request); | |
3009 | con->request = NULL; | |
3010 | } | |
3011 | ||
2abf387c | 3012 | if (con->response) |
ef416fc2 | 3013 | { |
3014 | ippDelete(con->response); | |
3015 | con->response = NULL; | |
3016 | } | |
3017 | ||
3018 | cupsdClearString(&con->command); | |
3019 | cupsdClearString(&con->options); | |
b86bc4cf | 3020 | cupsdClearString(&con->query_string); |
ef416fc2 | 3021 | |
3022 | if (!con->http.keep_alive) | |
3023 | { | |
f11a948a | 3024 | cupsdLogMessage(CUPSD_LOG_DEBUG, |
85dda01c | 3025 | "[Client %d] Closing because Keep-Alive disabled.", |
f11a948a | 3026 | con->http.fd); |
ef416fc2 | 3027 | cupsdCloseClient(con); |
f7deaa1a | 3028 | return; |
ef416fc2 | 3029 | } |
f11a948a MS |
3030 | else |
3031 | { | |
3032 | cupsArrayRemove(ActiveClients, con); | |
3033 | cupsdSetBusyState(); | |
3034 | } | |
ef416fc2 | 3035 | } |
3036 | ||
3037 | con->http.activity = time(NULL); | |
f7deaa1a | 3038 | } |
ef416fc2 | 3039 | |
f7deaa1a | 3040 | |
e1d6a774 | 3041 | /* |
3042 | * 'check_if_modified()' - Decode an "If-Modified-Since" line. | |
3043 | */ | |
3044 | ||
3045 | static int /* O - 1 if modified since */ | |
3046 | check_if_modified( | |
3047 | cupsd_client_t *con, /* I - Client connection */ | |
3048 | struct stat *filestats) /* I - File information */ | |
3049 | { | |
3050 | char *ptr; /* Pointer into field */ | |
3051 | time_t date; /* Time/date value */ | |
3052 | off_t size; /* Size/length value */ | |
3053 | ||
3054 | ||
3055 | size = 0; | |
3056 | date = 0; | |
3057 | ptr = con->http.fields[HTTP_FIELD_IF_MODIFIED_SINCE]; | |
3058 | ||
3059 | if (*ptr == '\0') | |
3060 | return (1); | |
3061 | ||
3062 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
85dda01c | 3063 | "[Client %d] check_if_modified " |
b9faaae1 | 3064 | "filestats=%p(" CUPS_LLFMT ", %d)) If-Modified-Since=\"%s\"", |
85dda01c | 3065 | con->http.fd, filestats, CUPS_LLCAST filestats->st_size, |
b9faaae1 | 3066 | (int)filestats->st_mtime, ptr); |
e1d6a774 | 3067 | |
3068 | while (*ptr != '\0') | |
3069 | { | |
3070 | while (isspace(*ptr) || *ptr == ';') | |
3071 | ptr ++; | |
3072 | ||
88f9aafc | 3073 | if (_cups_strncasecmp(ptr, "length=", 7) == 0) |
e1d6a774 | 3074 | { |
3075 | ptr += 7; | |
3076 | size = strtoll(ptr, NULL, 10); | |
3077 | ||
3078 | while (isdigit(*ptr)) | |
3079 | ptr ++; | |
3080 | } | |
3081 | else if (isalpha(*ptr)) | |
3082 | { | |
3083 | date = httpGetDateTime(ptr); | |
3084 | while (*ptr != '\0' && *ptr != ';') | |
3085 | ptr ++; | |
3086 | } | |
e53920b9 | 3087 | else |
3088 | ptr ++; | |
e1d6a774 | 3089 | } |
3090 | ||
e1d6a774 | 3091 | return ((size != filestats->st_size && size != 0) || |
3092 | (date < filestats->st_mtime && date != 0) || | |
3093 | (size == 0 && date == 0)); | |
3094 | } | |
3095 | ||
3096 | ||
3dfe78b3 MS |
3097 | /* |
3098 | * 'compare_clients()' - Compare two client connections. | |
3099 | */ | |
3100 | ||
3101 | static int /* O - Result of comparison */ | |
3102 | compare_clients(cupsd_client_t *a, /* I - First client */ | |
3103 | cupsd_client_t *b, /* I - Second client */ | |
3104 | void *data) /* I - User data (not used) */ | |
3105 | { | |
3106 | (void)data; | |
3107 | ||
3108 | if (a == b) | |
3109 | return (0); | |
3110 | else if (a < b) | |
3111 | return (-1); | |
3112 | else | |
3113 | return (1); | |
3114 | } | |
3115 | ||
3116 | ||
85dda01c MS |
3117 | /* |
3118 | * 'data_ready()' - Check whether data is available from a client. | |
3119 | */ | |
ef416fc2 | 3120 | |
85dda01c MS |
3121 | static int /* O - 1 if data is ready, 0 otherwise */ |
3122 | data_ready(cupsd_client_t *con) /* I - Client */ | |
3123 | { | |
3124 | if (con->http.used > 0) | |
3125 | return (1); | |
3126 | #ifdef HAVE_SSL | |
3127 | else if (con->http.tls) | |
e1d6a774 | 3128 | { |
85dda01c MS |
3129 | # ifdef HAVE_LIBSSL |
3130 | if (SSL_pending((SSL *)(con->http.tls))) | |
3131 | return (1); | |
3132 | # elif defined(HAVE_GNUTLS) | |
3133 | if (gnutls_record_check_pending(con->http.tls)) | |
3134 | return (1); | |
3135 | # elif defined(HAVE_CDSASSL) | |
3136 | size_t bytes; /* Bytes that are available */ | |
ef416fc2 | 3137 | |
85dda01c MS |
3138 | if (!SSLGetBufferedReadSize(con->http.tls, &bytes) && bytes > 0) |
3139 | return (1); | |
3140 | # endif /* HAVE_LIBSSL */ | |
e1d6a774 | 3141 | } |
85dda01c | 3142 | #endif /* HAVE_SSL */ |
ef416fc2 | 3143 | |
85dda01c MS |
3144 | return (0); |
3145 | } | |
ef416fc2 | 3146 | |
ef416fc2 | 3147 | |
ef416fc2 | 3148 | /* |
3149 | * 'get_file()' - Get a filename and state info. | |
3150 | */ | |
3151 | ||
3152 | static char * /* O - Real filename */ | |
3153 | get_file(cupsd_client_t *con, /* I - Client connection */ | |
3154 | struct stat *filestats, /* O - File information */ | |
3155 | char *filename, /* IO - Filename buffer */ | |
3156 | int len) /* I - Buffer length */ | |
3157 | { | |
3158 | int status; /* Status of filesystem calls */ | |
3159 | char *ptr; /* Pointer info filename */ | |
3160 | int plen; /* Remaining length after pointer */ | |
db1f069b | 3161 | char language[7]; /* Language subdirectory, if any */ |
ef416fc2 | 3162 | |
3163 | ||
3164 | /* | |
f7deaa1a | 3165 | * Figure out the real filename... |
ef416fc2 | 3166 | */ |
3167 | ||
db1f069b MS |
3168 | language[0] = '\0'; |
3169 | ||
7cf5915e | 3170 | if (!strncmp(con->uri, "/ppd/", 5) && !strchr(con->uri + 5, '/')) |
ef416fc2 | 3171 | snprintf(filename, len, "%s%s", ServerRoot, con->uri); |
7cf5915e MS |
3172 | else if (!strncmp(con->uri, "/icons/", 7) && !strchr(con->uri + 7, '/')) |
3173 | { | |
3174 | snprintf(filename, len, "%s/%s", CacheDir, con->uri + 7); | |
3175 | if (access(filename, F_OK) < 0) | |
3176 | snprintf(filename, len, "%s/images/generic.png", DocumentRoot); | |
3177 | } | |
f7deaa1a | 3178 | else if (!strncmp(con->uri, "/rss/", 5) && !strchr(con->uri + 5, '/')) |
3179 | snprintf(filename, len, "%s/rss/%s", CacheDir, con->uri + 5); | |
ef416fc2 | 3180 | else if (!strncmp(con->uri, "/admin/conf/", 12)) |
3181 | snprintf(filename, len, "%s%s", ServerRoot, con->uri + 11); | |
3182 | else if (!strncmp(con->uri, "/admin/log/", 11)) | |
3183 | { | |
2e4ff8af | 3184 | if (!strncmp(con->uri + 11, "access_log", 10) && AccessLog[0] == '/') |
ef416fc2 | 3185 | strlcpy(filename, AccessLog, len); |
2e4ff8af | 3186 | else if (!strncmp(con->uri + 11, "error_log", 9) && ErrorLog[0] == '/') |
ef416fc2 | 3187 | strlcpy(filename, ErrorLog, len); |
2e4ff8af | 3188 | else if (!strncmp(con->uri + 11, "page_log", 8) && PageLog[0] == '/') |
ef416fc2 | 3189 | strlcpy(filename, PageLog, len); |
3190 | else | |
3191 | return (NULL); | |
3192 | } | |
d6ae789d | 3193 | else if (con->language) |
db1f069b MS |
3194 | { |
3195 | snprintf(language, sizeof(language), "/%s", con->language->language); | |
3196 | snprintf(filename, len, "%s%s%s", DocumentRoot, language, con->uri); | |
3197 | } | |
ef416fc2 | 3198 | else |
3199 | snprintf(filename, len, "%s%s", DocumentRoot, con->uri); | |
3200 | ||
3201 | if ((ptr = strchr(filename, '?')) != NULL) | |
3202 | *ptr = '\0'; | |
3203 | ||
3204 | /* | |
3205 | * Grab the status for this language; if there isn't a language-specific file | |
3206 | * then fallback to the default one... | |
3207 | */ | |
3208 | ||
db1f069b | 3209 | if ((status = stat(filename, filestats)) != 0 && language[0] && |
7cf5915e | 3210 | strncmp(con->uri, "/icons/", 7) && |
d6ae789d | 3211 | strncmp(con->uri, "/ppd/", 5) && |
7cf5915e | 3212 | strncmp(con->uri, "/rss/", 5) && |
d6ae789d | 3213 | strncmp(con->uri, "/admin/conf/", 12) && |
3214 | strncmp(con->uri, "/admin/log/", 11)) | |
ef416fc2 | 3215 | { |
3216 | /* | |
d6ae789d | 3217 | * Drop the country code... |
ef416fc2 | 3218 | */ |
3219 | ||
db1f069b MS |
3220 | language[3] = '\0'; |
3221 | snprintf(filename, len, "%s%s%s", DocumentRoot, language, con->uri); | |
d6ae789d | 3222 | |
3223 | if ((ptr = strchr(filename, '?')) != NULL) | |
3224 | *ptr = '\0'; | |
3225 | ||
3226 | if ((status = stat(filename, filestats)) != 0) | |
ef416fc2 | 3227 | { |
d6ae789d | 3228 | /* |
3229 | * Drop the language prefix and try the root directory... | |
3230 | */ | |
3231 | ||
db1f069b | 3232 | language[0] = '\0'; |
ef416fc2 | 3233 | snprintf(filename, len, "%s%s", DocumentRoot, con->uri); |
3234 | ||
3235 | if ((ptr = strchr(filename, '?')) != NULL) | |
3236 | *ptr = '\0'; | |
3237 | ||
3238 | status = stat(filename, filestats); | |
3239 | } | |
3240 | } | |
3241 | ||
3242 | /* | |
3243 | * If we're found a directory, get the index.html file instead... | |
3244 | */ | |
3245 | ||
3246 | if (!status && S_ISDIR(filestats->st_mode)) | |
3247 | { | |
db1f069b MS |
3248 | /* |
3249 | * Make sure the URI ends with a slash... | |
3250 | */ | |
ef416fc2 | 3251 | |
db1f069b MS |
3252 | if (con->uri[strlen(con->uri) - 1] != '/') |
3253 | strlcat(con->uri, "/", sizeof(con->uri)); | |
ef416fc2 | 3254 | |
db1f069b MS |
3255 | /* |
3256 | * Find the directory index file, trying every language... | |
3257 | */ | |
ef416fc2 | 3258 | |
db1f069b | 3259 | do |
ef416fc2 | 3260 | { |
db1f069b MS |
3261 | if (status && language[0]) |
3262 | { | |
3263 | /* | |
3264 | * Try a different language subset... | |
3265 | */ | |
3266 | ||
3267 | if (language[3]) | |
3268 | language[0] = '\0'; /* Strip country code */ | |
3269 | else | |
3270 | language[0] = '\0'; /* Strip language */ | |
3271 | } | |
3272 | ||
3273 | /* | |
3274 | * Look for the index file... | |
3275 | */ | |
3276 | ||
3277 | snprintf(filename, len, "%s%s%s", DocumentRoot, language, con->uri); | |
3278 | ||
3279 | if ((ptr = strchr(filename, '?')) != NULL) | |
3280 | *ptr = '\0'; | |
3281 | ||
3282 | ptr = filename + strlen(filename); | |
3283 | plen = len - (ptr - filename); | |
3284 | ||
3285 | strlcpy(ptr, "index.html", plen); | |
ef416fc2 | 3286 | status = stat(filename, filestats); |
db1f069b MS |
3287 | |
3288 | #ifdef HAVE_JAVA | |
3289 | if (status) | |
3290 | { | |
3291 | strlcpy(ptr, "index.class", plen); | |
3292 | status = stat(filename, filestats); | |
3293 | } | |
ef416fc2 | 3294 | #endif /* HAVE_JAVA */ |
3295 | ||
3296 | #ifdef HAVE_PERL | |
db1f069b MS |
3297 | if (status) |
3298 | { | |
3299 | strlcpy(ptr, "index.pl", plen); | |
3300 | status = stat(filename, filestats); | |
3301 | } | |
ef416fc2 | 3302 | #endif /* HAVE_PERL */ |
3303 | ||
3304 | #ifdef HAVE_PHP | |
db1f069b MS |
3305 | if (status) |
3306 | { | |
3307 | strlcpy(ptr, "index.php", plen); | |
3308 | status = stat(filename, filestats); | |
3309 | } | |
ef416fc2 | 3310 | #endif /* HAVE_PHP */ |
3311 | ||
3312 | #ifdef HAVE_PYTHON | |
db1f069b MS |
3313 | if (status) |
3314 | { | |
3315 | strlcpy(ptr, "index.pyc", plen); | |
3316 | status = stat(filename, filestats); | |
3317 | } | |
ef416fc2 | 3318 | |
db1f069b MS |
3319 | if (status) |
3320 | { | |
3321 | strlcpy(ptr, "index.py", plen); | |
3322 | status = stat(filename, filestats); | |
3323 | } | |
ef416fc2 | 3324 | #endif /* HAVE_PYTHON */ |
db1f069b MS |
3325 | |
3326 | } | |
3327 | while (status && language[0]); | |
ef416fc2 | 3328 | } |
3329 | ||
b9faaae1 | 3330 | cupsdLogMessage(CUPSD_LOG_DEBUG2, |
85dda01c MS |
3331 | "[Client %d] get_file filestats=%p, filename=%p, len=%d, " |
3332 | "returning \"%s\".", con->http.fd, filestats, filename, len, | |
b9faaae1 | 3333 | status ? "(null)" : filename); |
ef416fc2 | 3334 | |
ef416fc2 | 3335 | if (status) |
3336 | return (NULL); | |
3337 | else | |
3338 | return (filename); | |
3339 | } | |
3340 | ||
3341 | ||
3342 | /* | |
c41769ff | 3343 | * 'install_cupsd_conf()' - Install a configuration file. |
ef416fc2 | 3344 | */ |
3345 | ||
3346 | static http_status_t /* O - Status */ | |
c41769ff | 3347 | install_cupsd_conf(cupsd_client_t *con) /* I - Connection */ |
ef416fc2 | 3348 | { |
321d8d57 | 3349 | char filename[1024]; /* Configuration filename */ |
ef416fc2 | 3350 | cups_file_t *in, /* Input file */ |
3351 | *out; /* Output file */ | |
321d8d57 MS |
3352 | char buffer[16384]; /* Copy buffer */ |
3353 | ssize_t bytes; /* Number of bytes */ | |
ef416fc2 | 3354 | |
3355 | ||
3356 | /* | |
321d8d57 | 3357 | * Open the request file... |
ef416fc2 | 3358 | */ |
3359 | ||
321d8d57 | 3360 | if ((in = cupsFileOpen(con->filename, "rb")) == NULL) |
ef416fc2 | 3361 | { |
321d8d57 MS |
3362 | cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to open request file \"%s\": %s", |
3363 | con->filename, strerror(errno)); | |
3364 | return (HTTP_SERVER_ERROR); | |
ef416fc2 | 3365 | } |
3366 | ||
3367 | /* | |
321d8d57 | 3368 | * Open the new config file... |
ef416fc2 | 3369 | */ |
3370 | ||
cb7f98ee | 3371 | if ((out = cupsdCreateConfFile(ConfigurationFile, ConfigFilePerm)) == NULL) |
ef416fc2 | 3372 | { |
3373 | cupsFileClose(in); | |
ef416fc2 | 3374 | return (HTTP_SERVER_ERROR); |
3375 | } | |
3376 | ||
cb7f98ee MS |
3377 | cupsdLogMessage(CUPSD_LOG_INFO, "Installing config file \"%s\"...", |
3378 | ConfigurationFile); | |
ef416fc2 | 3379 | |
3380 | /* | |
3381 | * Copy from the request to the new config file... | |
3382 | */ | |
3383 | ||
3384 | while ((bytes = cupsFileRead(in, buffer, sizeof(buffer))) > 0) | |
3385 | if (cupsFileWrite(out, buffer, bytes) < bytes) | |
3386 | { | |
3387 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
321d8d57 | 3388 | "Unable to copy to config file \"%s\": %s", |
cb7f98ee | 3389 | ConfigurationFile, strerror(errno)); |
ef416fc2 | 3390 | |
3391 | cupsFileClose(in); | |
3392 | cupsFileClose(out); | |
321d8d57 | 3393 | |
cb7f98ee MS |
3394 | snprintf(filename, sizeof(filename), "%s.N", ConfigurationFile); |
3395 | cupsdUnlinkOrRemoveFile(filename); | |
ef416fc2 | 3396 | |
3397 | return (HTTP_SERVER_ERROR); | |
3398 | } | |
3399 | ||
3400 | /* | |
3401 | * Close the files... | |
3402 | */ | |
3403 | ||
3404 | cupsFileClose(in); | |
ef416fc2 | 3405 | |
cb7f98ee | 3406 | if (cupsdCloseCreatedConfFile(out, ConfigurationFile)) |
ef416fc2 | 3407 | return (HTTP_SERVER_ERROR); |
ef416fc2 | 3408 | |
3409 | /* | |
3410 | * Remove the request file... | |
3411 | */ | |
3412 | ||
cb7f98ee | 3413 | cupsdUnlinkOrRemoveFile(con->filename); |
ef416fc2 | 3414 | cupsdClearString(&con->filename); |
3415 | ||
ef416fc2 | 3416 | /* |
c41769ff | 3417 | * Set the NeedReload flag... |
ef416fc2 | 3418 | */ |
3419 | ||
c41769ff | 3420 | NeedReload = RELOAD_CUPSD; |
ef416fc2 | 3421 | ReloadTime = time(NULL); |
3422 | ||
3423 | /* | |
3424 | * Return that the file was created successfully... | |
3425 | */ | |
3426 | ||
3427 | return (HTTP_CREATED); | |
3428 | } | |
3429 | ||
3430 | ||
e1d6a774 | 3431 | /* |
3432 | * 'is_cgi()' - Is the resource a CGI script/program? | |
3433 | */ | |
3434 | ||
3435 | static int /* O - 1 = CGI, 0 = file */ | |
3436 | is_cgi(cupsd_client_t *con, /* I - Client connection */ | |
3437 | const char *filename, /* I - Real filename */ | |
3438 | struct stat *filestats, /* I - File information */ | |
3439 | mime_type_t *type) /* I - MIME type */ | |
3440 | { | |
3441 | const char *options; /* Options on URL */ | |
3442 | ||
3443 | ||
e1d6a774 | 3444 | /* |
3445 | * Get the options, if any... | |
3446 | */ | |
3447 | ||
3448 | if ((options = strchr(con->uri, '?')) != NULL) | |
a0f6818e MS |
3449 | { |
3450 | options ++; | |
3451 | cupsdSetStringf(&(con->query_string), "QUERY_STRING=%s", options); | |
3452 | } | |
b86bc4cf | 3453 | |
e1d6a774 | 3454 | /* |
3455 | * Check for known types... | |
3456 | */ | |
3457 | ||
88f9aafc | 3458 | if (!type || _cups_strcasecmp(type->super, "application")) |
e1d6a774 | 3459 | { |
b9faaae1 | 3460 | cupsdLogMessage(CUPSD_LOG_DEBUG2, |
85dda01c MS |
3461 | "[Client %d] is_cgi filename=\"%s\", filestats=%p, " |
3462 | "type=%s/%s, returning 0", con->http.fd, filename, | |
3463 | filestats, type ? type->super : "unknown", | |
b9faaae1 | 3464 | type ? type->type : "unknown"); |
e1d6a774 | 3465 | return (0); |
3466 | } | |
3467 | ||
88f9aafc | 3468 | if (!_cups_strcasecmp(type->type, "x-httpd-cgi") && |
e1d6a774 | 3469 | (filestats->st_mode & 0111)) |
3470 | { | |
3471 | /* | |
3472 | * "application/x-httpd-cgi" is a CGI script. | |
3473 | */ | |
3474 | ||
3475 | cupsdSetString(&con->command, filename); | |
3476 | ||
a0f6818e MS |
3477 | if (options) |
3478 | cupsdSetStringf(&con->options, " %s", options); | |
e1d6a774 | 3479 | |
3480 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
85dda01c MS |
3481 | "[Client %d] is_cgi filename=\"%s\", filestats=%p, " |
3482 | "type=%s/%s, returning 1", con->http.fd, filename, | |
3483 | filestats, type->super, type->type); | |
e1d6a774 | 3484 | return (1); |
3485 | } | |
3486 | #ifdef HAVE_JAVA | |
88f9aafc | 3487 | else if (!_cups_strcasecmp(type->type, "x-httpd-java")) |
e1d6a774 | 3488 | { |
3489 | /* | |
3490 | * "application/x-httpd-java" is a Java servlet. | |
3491 | */ | |
3492 | ||
3493 | cupsdSetString(&con->command, CUPS_JAVA); | |
3494 | ||
3495 | if (options) | |
b94498cf | 3496 | cupsdSetStringf(&con->options, " %s %s", filename, options); |
e1d6a774 | 3497 | else |
b94498cf | 3498 | cupsdSetStringf(&con->options, " %s", filename); |
e1d6a774 | 3499 | |
3500 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
85dda01c MS |
3501 | "[Client %d] is_cgi filename=\"%s\", filestats=%p, " |
3502 | "type=%s/%s, returning 1", con->http.fd, filename, | |
3503 | filestats, type->super, type->type); | |
e1d6a774 | 3504 | return (1); |
3505 | } | |
3506 | #endif /* HAVE_JAVA */ | |
3507 | #ifdef HAVE_PERL | |
88f9aafc | 3508 | else if (!_cups_strcasecmp(type->type, "x-httpd-perl")) |
e1d6a774 | 3509 | { |
3510 | /* | |
3511 | * "application/x-httpd-perl" is a Perl page. | |
3512 | */ | |
3513 | ||
3514 | cupsdSetString(&con->command, CUPS_PERL); | |
3515 | ||
3516 | if (options) | |
b94498cf | 3517 | cupsdSetStringf(&con->options, " %s %s", filename, options); |
e1d6a774 | 3518 | else |
b94498cf | 3519 | cupsdSetStringf(&con->options, " %s", filename); |
e1d6a774 | 3520 | |
3521 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
85dda01c MS |
3522 | "[Client %d] is_cgi filename=\"%s\", filestats=%p, " |
3523 | "type=%s/%s, returning 1", con->http.fd, filename, | |
3524 | filestats, type->super, type->type); | |
e1d6a774 | 3525 | return (1); |
3526 | } | |
3527 | #endif /* HAVE_PERL */ | |
3528 | #ifdef HAVE_PHP | |
88f9aafc | 3529 | else if (!_cups_strcasecmp(type->type, "x-httpd-php")) |
e1d6a774 | 3530 | { |
3531 | /* | |
3532 | * "application/x-httpd-php" is a PHP page. | |
3533 | */ | |
3534 | ||
3535 | cupsdSetString(&con->command, CUPS_PHP); | |
3536 | ||
3537 | if (options) | |
b94498cf | 3538 | cupsdSetStringf(&con->options, " %s %s", filename, options); |
e1d6a774 | 3539 | else |
b94498cf | 3540 | cupsdSetStringf(&con->options, " %s", filename); |
e1d6a774 | 3541 | |
3542 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
85dda01c MS |
3543 | "[Client %d] is_cgi filename=\"%s\", filestats=%p, " |
3544 | "type=%s/%s, returning 1", con->http.fd, filename, | |
3545 | filestats, type->super, type->type); | |
e1d6a774 | 3546 | return (1); |
3547 | } | |
3548 | #endif /* HAVE_PHP */ | |
3549 | #ifdef HAVE_PYTHON | |
88f9aafc | 3550 | else if (!_cups_strcasecmp(type->type, "x-httpd-python")) |
e1d6a774 | 3551 | { |
3552 | /* | |
3553 | * "application/x-httpd-python" is a Python page. | |
3554 | */ | |
3555 | ||
3556 | cupsdSetString(&con->command, CUPS_PYTHON); | |
3557 | ||
3558 | if (options) | |
b94498cf | 3559 | cupsdSetStringf(&con->options, " %s %s", filename, options); |
e1d6a774 | 3560 | else |
b94498cf | 3561 | cupsdSetStringf(&con->options, " %s", filename); |
e1d6a774 | 3562 | |
3563 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
85dda01c MS |
3564 | "[Client %d] is_cgi filename=\"%s\", filestats=%p, " |
3565 | "type=%s/%s, returning 1", con->http.fd, filename, | |
3566 | filestats, type->super, type->type); | |
e1d6a774 | 3567 | return (1); |
3568 | } | |
3569 | #endif /* HAVE_PYTHON */ | |
3570 | ||
b9faaae1 | 3571 | cupsdLogMessage(CUPSD_LOG_DEBUG2, |
85dda01c MS |
3572 | "[Client %d] is_cgi filename=\"%s\", filestats=%p, " |
3573 | "type=%s/%s, returning 0", con->http.fd, filename, | |
3574 | filestats, type->super, type->type); | |
e1d6a774 | 3575 | return (0); |
3576 | } | |
3577 | ||
3578 | ||
ef416fc2 | 3579 | /* |
3580 | * 'is_path_absolute()' - Is a path absolute and free of relative elements (i.e. ".."). | |
3581 | */ | |
3582 | ||
3583 | static int /* O - 0 if relative, 1 if absolute */ | |
3584 | is_path_absolute(const char *path) /* I - Input path */ | |
3585 | { | |
3586 | /* | |
3587 | * Check for a leading slash... | |
3588 | */ | |
3589 | ||
3590 | if (path[0] != '/') | |
3591 | return (0); | |
3592 | ||
3593 | /* | |
3594 | * Check for "/.." in the path... | |
3595 | */ | |
3596 | ||
3597 | while ((path = strstr(path, "/..")) != NULL) | |
3598 | { | |
3599 | if (!path[3] || path[3] == '/') | |
3600 | return (0); | |
3601 | ||
3602 | path ++; | |
3603 | } | |
3604 | ||
3605 | /* | |
3606 | * If we haven't found any relative paths, return 1 indicating an | |
3607 | * absolute path... | |
3608 | */ | |
3609 | ||
3610 | return (1); | |
3611 | } | |
3612 | ||
3613 | ||
3614 | /* | |
3615 | * 'pipe_command()' - Pipe the output of a command to the remote client. | |
3616 | */ | |
3617 | ||
3618 | static int /* O - Process ID */ | |
3619 | pipe_command(cupsd_client_t *con, /* I - Client connection */ | |
3620 | int infile, /* I - Standard input for command */ | |
3621 | int *outfile, /* O - Standard output for command */ | |
3622 | char *command, /* I - Command to run */ | |
3623 | char *options, /* I - Options for command */ | |
3624 | int root) /* I - Run as root? */ | |
3625 | { | |
3626 | int i; /* Looping var */ | |
3627 | int pid; /* Process ID */ | |
b423cd4c | 3628 | char *commptr, /* Command string pointer */ |
3629 | commch; /* Command string character */ | |
ef416fc2 | 3630 | char *uriptr; /* URI string pointer */ |
3631 | int fds[2]; /* Pipe FDs */ | |
3632 | int argc; /* Number of arguments */ | |
3633 | int envc; /* Number of environment variables */ | |
3634 | char argbuf[10240], /* Argument buffer */ | |
3635 | *argv[100], /* Argument strings */ | |
b94498cf | 3636 | *envp[MAX_ENV + 20]; /* Environment variables */ |
c7017ecc | 3637 | char auth_type[256], /* AUTH_TYPE environment variable */ |
b94498cf | 3638 | content_length[1024], /* CONTENT_LENGTH environment variable */ |
ef416fc2 | 3639 | content_type[1024], /* CONTENT_TYPE environment variable */ |
3640 | http_cookie[32768], /* HTTP_COOKIE environment variable */ | |
f7deaa1a | 3641 | http_referer[1024], /* HTTP_REFERER environment variable */ |
ef416fc2 | 3642 | http_user_agent[1024], /* HTTP_USER_AGENT environment variable */ |
3643 | lang[1024], /* LANG environment variable */ | |
b423cd4c | 3644 | path_info[1024], /* PATH_INFO environment variable */ |
ef416fc2 | 3645 | remote_addr[1024], /* REMOTE_ADDR environment variable */ |
3646 | remote_host[1024], /* REMOTE_HOST environment variable */ | |
3647 | remote_user[1024], /* REMOTE_USER environment variable */ | |
b94498cf | 3648 | script_filename[1024], /* SCRIPT_FILENAME environment variable */ |
ef416fc2 | 3649 | script_name[1024], /* SCRIPT_NAME environment variable */ |
3650 | server_name[1024], /* SERVER_NAME environment variable */ | |
3651 | server_port[1024]; /* SERVER_PORT environment variable */ | |
7dfedb92 | 3652 | ipp_attribute_t *attr; /* attributes-natural-language attribute */ |
ef416fc2 | 3653 | |
3654 | ||
3655 | /* | |
3656 | * Parse a copy of the options string, which is of the form: | |
3657 | * | |
b423cd4c | 3658 | * argument+argument+argument |
3659 | * ?argument+argument+argument | |
3660 | * param=value¶m=value | |
3661 | * ?param=value¶m=value | |
3662 | * /name?argument+argument+argument | |
3663 | * /name?param=value¶m=value | |
ef416fc2 | 3664 | * |
3665 | * If the string contains an "=" character after the initial name, | |
3666 | * then we treat it as a HTTP GET form request and make a copy of | |
3667 | * the remaining string for the environment variable. | |
3668 | * | |
3669 | * The string is always parsed out as command-line arguments, to | |
3670 | * be consistent with Apache... | |
3671 | */ | |
3672 | ||
3673 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
85dda01c MS |
3674 | "[Client %d] pipe_command infile=%d, outfile=%p, " |
3675 | "command=\"%s\", options=\"%s\", root=%d", | |
3676 | con->http.fd, infile, outfile, command, | |
b9faaae1 | 3677 | options ? options : "(null)", root); |
ef416fc2 | 3678 | |
b86bc4cf | 3679 | argv[0] = command; |
ef416fc2 | 3680 | |
b423cd4c | 3681 | if (options) |
c7017ecc MS |
3682 | { |
3683 | commptr = options; | |
3684 | if (*commptr == ' ') | |
3685 | commptr ++; | |
3686 | strlcpy(argbuf, commptr, sizeof(argbuf)); | |
3687 | } | |
b423cd4c | 3688 | else |
3689 | argbuf[0] = '\0'; | |
3690 | ||
3691 | if (argbuf[0] == '/') | |
ef416fc2 | 3692 | { |
3693 | /* | |
b423cd4c | 3694 | * Found some trailing path information, set PATH_INFO... |
ef416fc2 | 3695 | */ |
3696 | ||
b423cd4c | 3697 | if ((commptr = strchr(argbuf, '?')) == NULL) |
3698 | commptr = argbuf + strlen(argbuf); | |
ef416fc2 | 3699 | |
b423cd4c | 3700 | commch = *commptr; |
3701 | *commptr = '\0'; | |
3702 | snprintf(path_info, sizeof(path_info), "PATH_INFO=%s", argbuf); | |
3703 | *commptr = commch; | |
3704 | } | |
3705 | else | |
3706 | { | |
3707 | commptr = argbuf; | |
3708 | path_info[0] = '\0'; | |
b94498cf | 3709 | |
3710 | if (*commptr == ' ') | |
3711 | commptr ++; | |
b423cd4c | 3712 | } |
ef416fc2 | 3713 | |
cb7f98ee | 3714 | if (*commptr == '?' && con->operation == HTTP_STATE_GET && !con->query_string) |
b423cd4c | 3715 | { |
3716 | commptr ++; | |
b86bc4cf | 3717 | cupsdSetStringf(&(con->query_string), "QUERY_STRING=%s", commptr); |
b423cd4c | 3718 | } |
ef416fc2 | 3719 | |
b423cd4c | 3720 | argc = 1; |
ef416fc2 | 3721 | |
b423cd4c | 3722 | if (*commptr) |
3723 | { | |
3724 | argv[argc ++] = commptr; | |
ef416fc2 | 3725 | |
b423cd4c | 3726 | for (; *commptr && argc < 99; commptr ++) |
3727 | { | |
ef416fc2 | 3728 | /* |
b423cd4c | 3729 | * Break arguments whenever we see a + or space... |
ef416fc2 | 3730 | */ |
3731 | ||
b423cd4c | 3732 | if (*commptr == ' ' || *commptr == '+') |
3733 | { | |
3734 | while (*commptr == ' ' || *commptr == '+') | |
3735 | *commptr++ = '\0'; | |
ef416fc2 | 3736 | |
b423cd4c | 3737 | /* |
3738 | * If we don't have a blank string, save it as another argument... | |
3739 | */ | |
ef416fc2 | 3740 | |
b423cd4c | 3741 | if (*commptr) |
3742 | { | |
3743 | argv[argc] = commptr; | |
3744 | argc ++; | |
3745 | } | |
3746 | else | |
3747 | break; | |
3748 | } | |
3749 | else if (*commptr == '%' && isxdigit(commptr[1] & 255) && | |
3750 | isxdigit(commptr[2] & 255)) | |
3751 | { | |
3752 | /* | |
3753 | * Convert the %xx notation to the individual character. | |
3754 | */ | |
ef416fc2 | 3755 | |
b423cd4c | 3756 | if (commptr[1] >= '0' && commptr[1] <= '9') |
3757 | *commptr = (commptr[1] - '0') << 4; | |
3758 | else | |
3759 | *commptr = (tolower(commptr[1]) - 'a' + 10) << 4; | |
ef416fc2 | 3760 | |
b423cd4c | 3761 | if (commptr[2] >= '0' && commptr[2] <= '9') |
3762 | *commptr |= commptr[2] - '0'; | |
3763 | else | |
3764 | *commptr |= tolower(commptr[2]) - 'a' + 10; | |
ef416fc2 | 3765 | |
b423cd4c | 3766 | _cups_strcpy(commptr + 1, commptr + 3); |
ef416fc2 | 3767 | |
b423cd4c | 3768 | /* |
3769 | * Check for a %00 and break if that is the case... | |
3770 | */ | |
ef416fc2 | 3771 | |
b423cd4c | 3772 | if (!*commptr) |
3773 | break; | |
3774 | } | |
ef416fc2 | 3775 | } |
3776 | } | |
3777 | ||
3778 | argv[argc] = NULL; | |
3779 | ||
ef416fc2 | 3780 | /* |
3781 | * Setup the environment variables as needed... | |
3782 | */ | |
3783 | ||
b94498cf | 3784 | if (con->username[0]) |
3785 | { | |
c7017ecc | 3786 | snprintf(auth_type, sizeof(auth_type), "AUTH_TYPE=%s", |
b94498cf | 3787 | httpGetField(HTTP(con), HTTP_FIELD_AUTHORIZATION)); |
3788 | ||
3789 | if ((uriptr = strchr(auth_type + 10, ' ')) != NULL) | |
3790 | *uriptr = '\0'; | |
3791 | } | |
3792 | else | |
3793 | auth_type[0] = '\0'; | |
3794 | ||
7dfedb92 MS |
3795 | if (con->request && |
3796 | (attr = ippFindAttribute(con->request, "attributes-natural-language", | |
3797 | IPP_TAG_LANGUAGE)) != NULL) | |
3798 | { | |
3799 | switch (strlen(attr->values[0].string.text)) | |
3800 | { | |
3801 | default : | |
3802 | /* | |
3803 | * This is an unknown or badly formatted language code; use | |
3804 | * the POSIX locale... | |
3805 | */ | |
3806 | ||
5a9febac | 3807 | strlcpy(lang, "LANG=C", sizeof(lang)); |
7dfedb92 MS |
3808 | break; |
3809 | ||
3810 | case 2 : | |
3811 | /* | |
3812 | * Just the language code (ll)... | |
3813 | */ | |
3814 | ||
3815 | snprintf(lang, sizeof(lang), "LANG=%s.UTF8", | |
3816 | attr->values[0].string.text); | |
3817 | break; | |
3818 | ||
3819 | case 5 : | |
3820 | /* | |
3821 | * Language and country code (ll-cc)... | |
3822 | */ | |
3823 | ||
3824 | snprintf(lang, sizeof(lang), "LANG=%c%c_%c%c.UTF8", | |
3825 | attr->values[0].string.text[0], | |
3826 | attr->values[0].string.text[1], | |
3827 | toupper(attr->values[0].string.text[3] & 255), | |
3828 | toupper(attr->values[0].string.text[4] & 255)); | |
3829 | break; | |
3830 | } | |
3831 | } | |
3832 | else if (con->language) | |
3833 | snprintf(lang, sizeof(lang), "LANG=%s.UTF8", con->language->language); | |
ef416fc2 | 3834 | else |
5a9febac | 3835 | strlcpy(lang, "LANG=C", sizeof(lang)); |
ef416fc2 | 3836 | |
5a9febac | 3837 | strlcpy(remote_addr, "REMOTE_ADDR=", sizeof(remote_addr)); |
ef416fc2 | 3838 | httpAddrString(con->http.hostaddr, remote_addr + 12, |
3839 | sizeof(remote_addr) - 12); | |
3840 | ||
3841 | snprintf(remote_host, sizeof(remote_host), "REMOTE_HOST=%s", | |
3842 | con->http.hostname); | |
3843 | ||
3844 | snprintf(script_name, sizeof(script_name), "SCRIPT_NAME=%s", con->uri); | |
3845 | if ((uriptr = strchr(script_name, '?')) != NULL) | |
3846 | *uriptr = '\0'; | |
3847 | ||
b94498cf | 3848 | snprintf(script_filename, sizeof(script_filename), "SCRIPT_FILENAME=%s%s", |
3849 | DocumentRoot, script_name + 12); | |
3850 | ||
ef416fc2 | 3851 | sprintf(server_port, "SERVER_PORT=%d", con->serverport); |
3852 | ||
0268488e MS |
3853 | if (con->http.fields[HTTP_FIELD_HOST][0]) |
3854 | { | |
3855 | char *nameptr; /* Pointer to ":port" */ | |
3856 | ||
3857 | snprintf(server_name, sizeof(server_name), "SERVER_NAME=%s", | |
3858 | con->http.fields[HTTP_FIELD_HOST]); | |
3859 | if ((nameptr = strrchr(server_name, ':')) != NULL && !strchr(nameptr, ']')) | |
3860 | *nameptr = '\0'; /* Strip trailing ":port" */ | |
3861 | } | |
3862 | else | |
3863 | snprintf(server_name, sizeof(server_name), "SERVER_NAME=%s", | |
3864 | con->servername); | |
ef416fc2 | 3865 | |
3866 | envc = cupsdLoadEnv(envp, (int)(sizeof(envp) / sizeof(envp[0]))); | |
3867 | ||
b94498cf | 3868 | if (auth_type[0]) |
3869 | envp[envc ++] = auth_type; | |
3870 | ||
ef416fc2 | 3871 | envp[envc ++] = lang; |
3872 | envp[envc ++] = "REDIRECT_STATUS=1"; | |
b94498cf | 3873 | envp[envc ++] = "GATEWAY_INTERFACE=CGI/1.1"; |
ef416fc2 | 3874 | envp[envc ++] = server_name; |
3875 | envp[envc ++] = server_port; | |
3876 | envp[envc ++] = remote_addr; | |
3877 | envp[envc ++] = remote_host; | |
3878 | envp[envc ++] = script_name; | |
b94498cf | 3879 | envp[envc ++] = script_filename; |
ef416fc2 | 3880 | |
b423cd4c | 3881 | if (path_info[0]) |
3882 | envp[envc ++] = path_info; | |
3883 | ||
ef416fc2 | 3884 | if (con->username[0]) |
3885 | { | |
3886 | snprintf(remote_user, sizeof(remote_user), "REMOTE_USER=%s", con->username); | |
3887 | ||
3888 | envp[envc ++] = remote_user; | |
3889 | } | |
3890 | ||
3891 | if (con->http.version == HTTP_1_1) | |
3892 | envp[envc ++] = "SERVER_PROTOCOL=HTTP/1.1"; | |
3893 | else if (con->http.version == HTTP_1_0) | |
3894 | envp[envc ++] = "SERVER_PROTOCOL=HTTP/1.0"; | |
3895 | else | |
3896 | envp[envc ++] = "SERVER_PROTOCOL=HTTP/0.9"; | |
3897 | ||
3898 | if (con->http.cookie) | |
3899 | { | |
3900 | snprintf(http_cookie, sizeof(http_cookie), "HTTP_COOKIE=%s", | |
3901 | con->http.cookie); | |
3902 | envp[envc ++] = http_cookie; | |
3903 | } | |
3904 | ||
3905 | if (con->http.fields[HTTP_FIELD_USER_AGENT][0]) | |
3906 | { | |
3907 | snprintf(http_user_agent, sizeof(http_user_agent), "HTTP_USER_AGENT=%s", | |
3908 | con->http.fields[HTTP_FIELD_USER_AGENT]); | |
3909 | envp[envc ++] = http_user_agent; | |
3910 | } | |
3911 | ||
f7deaa1a | 3912 | if (con->http.fields[HTTP_FIELD_REFERER][0]) |
3913 | { | |
3914 | snprintf(http_referer, sizeof(http_referer), "HTTP_REFERER=%s", | |
3915 | con->http.fields[HTTP_FIELD_REFERER]); | |
3916 | envp[envc ++] = http_referer; | |
3917 | } | |
3918 | ||
cb7f98ee | 3919 | if (con->operation == HTTP_STATE_GET) |
ef416fc2 | 3920 | { |
ef416fc2 | 3921 | envp[envc ++] = "REQUEST_METHOD=GET"; |
3922 | ||
b86bc4cf | 3923 | if (con->query_string) |
ef416fc2 | 3924 | { |
3925 | /* | |
3926 | * Add GET form variables after ?... | |
3927 | */ | |
3928 | ||
b86bc4cf | 3929 | envp[envc ++] = con->query_string; |
ef416fc2 | 3930 | } |
3dfe78b3 MS |
3931 | else |
3932 | envp[envc ++] = "QUERY_STRING="; | |
ef416fc2 | 3933 | } |
3934 | else | |
3935 | { | |
e1d6a774 | 3936 | sprintf(content_length, "CONTENT_LENGTH=" CUPS_LLFMT, |
3937 | CUPS_LLCAST con->bytes); | |
ef416fc2 | 3938 | snprintf(content_type, sizeof(content_type), "CONTENT_TYPE=%s", |
3939 | con->http.fields[HTTP_FIELD_CONTENT_TYPE]); | |
3940 | ||
3941 | envp[envc ++] = "REQUEST_METHOD=POST"; | |
3942 | envp[envc ++] = content_length; | |
3943 | envp[envc ++] = content_type; | |
3944 | } | |
3945 | ||
3946 | /* | |
3947 | * Tell the CGI if we are using encryption... | |
3948 | */ | |
3949 | ||
a74454a7 | 3950 | if (con->http.tls) |
ef416fc2 | 3951 | envp[envc ++] = "HTTPS=ON"; |
3952 | ||
3953 | /* | |
3954 | * Terminate the environment array... | |
3955 | */ | |
3956 | ||
3957 | envp[envc] = NULL; | |
3958 | ||
38e73f87 | 3959 | if (LogLevel >= CUPSD_LOG_DEBUG) |
ef416fc2 | 3960 | { |
3961 | for (i = 0; i < argc; i ++) | |
38e73f87 MS |
3962 | cupsdLogMessage(CUPSD_LOG_DEBUG, |
3963 | "[CGI] argv[%d] = \"%s\"", i, argv[i]); | |
ef416fc2 | 3964 | for (i = 0; i < envc; i ++) |
38e73f87 MS |
3965 | cupsdLogMessage(CUPSD_LOG_DEBUG, |
3966 | "[CGI] envp[%d] = \"%s\"", i, envp[i]); | |
ef416fc2 | 3967 | } |
3968 | ||
3969 | /* | |
3970 | * Create a pipe for the output... | |
3971 | */ | |
3972 | ||
3973 | if (cupsdOpenPipe(fds)) | |
3974 | { | |
38e73f87 | 3975 | cupsdLogMessage(CUPSD_LOG_ERROR, "[CGI] Unable to create pipe for %s - %s", |
ef416fc2 | 3976 | argv[0], strerror(errno)); |
3977 | return (0); | |
3978 | } | |
3979 | ||
3980 | /* | |
3981 | * Then execute the command... | |
3982 | */ | |
3983 | ||
3984 | if (cupsdStartProcess(command, argv, envp, infile, fds[1], CGIPipes[1], | |
38e73f87 | 3985 | -1, -1, root, DefaultProfile, NULL, &pid) < 0) |
ef416fc2 | 3986 | { |
3987 | /* | |
3988 | * Error - can't fork! | |
3989 | */ | |
3990 | ||
38e73f87 | 3991 | cupsdLogMessage(CUPSD_LOG_ERROR, "[CGI] Unable to start %s - %s", argv[0], |
ef416fc2 | 3992 | strerror(errno)); |
3993 | ||
3994 | cupsdClosePipe(fds); | |
3995 | pid = 0; | |
3996 | } | |
3997 | else | |
3998 | { | |
3999 | /* | |
4000 | * Fork successful - return the PID... | |
4001 | */ | |
4002 | ||
4003 | if (con->username[0]) | |
0fa6c7fa | 4004 | cupsdAddCert(pid, con->username, con->type); |
ef416fc2 | 4005 | |
38e73f87 | 4006 | cupsdLogMessage(CUPSD_LOG_DEBUG, "[CGI] Started %s (PID %d)", command, pid); |
ef416fc2 | 4007 | |
4008 | *outfile = fds[0]; | |
4009 | close(fds[1]); | |
4010 | } | |
4011 | ||
ef416fc2 | 4012 | return (pid); |
4013 | } | |
4014 | ||
4015 | ||
e07d4801 MS |
4016 | /* |
4017 | * 'valid_host()' - Is the Host: field valid? | |
4018 | */ | |
4019 | ||
4020 | static int /* O - 1 if valid, 0 if not */ | |
4021 | valid_host(cupsd_client_t *con) /* I - Client connection */ | |
4022 | { | |
4023 | cupsd_alias_t *a; /* Current alias */ | |
4024 | cupsd_netif_t *netif; /* Current network interface */ | |
4025 | const char *host, /* Host field */ | |
4026 | *end; /* End character */ | |
4027 | ||
4028 | ||
4029 | host = con->http.fields[HTTP_FIELD_HOST]; | |
4030 | ||
4031 | if (httpAddrLocalhost(con->http.hostaddr)) | |
4032 | { | |
4033 | /* | |
4034 | * Only allow "localhost" or the equivalent IPv4 or IPv6 numerical | |
4035 | * addresses when accessing CUPS via the loopback interface... | |
4036 | */ | |
4037 | ||
88f9aafc MS |
4038 | return (!_cups_strcasecmp(host, "localhost") || |
4039 | !_cups_strncasecmp(host, "localhost:", 10) || | |
4040 | !_cups_strcasecmp(host, "localhost.") || | |
4041 | !_cups_strncasecmp(host, "localhost.:", 11) || | |
e07d4801 | 4042 | #ifdef __linux |
88f9aafc MS |
4043 | !_cups_strcasecmp(host, "localhost.localdomain") || |
4044 | !_cups_strncasecmp(host, "localhost.localdomain:", 22) || | |
e07d4801 MS |
4045 | #endif /* __linux */ |
4046 | !strcmp(host, "127.0.0.1") || | |
4047 | !strncmp(host, "127.0.0.1:", 10) || | |
4048 | !strcmp(host, "[::1]") || | |
4049 | !strncmp(host, "[::1]:", 6)); | |
4050 | } | |
4051 | ||
37e7e6e0 | 4052 | #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI) |
e07d4801 MS |
4053 | /* |
4054 | * Check if the hostname is something.local (Bonjour); if so, allow it. | |
4055 | */ | |
4056 | ||
a29fd7dd MS |
4057 | if ((end = strrchr(host, '.')) != NULL && end > host && |
4058 | (!end[1] || end[1] == ':')) | |
4059 | { | |
4060 | /* | |
4061 | * "." on end, work back to second-to-last "."... | |
4062 | */ | |
4063 | for (end --; end > host && *end != '.'; end --); | |
4064 | } | |
4065 | ||
4066 | if (end && (!_cups_strcasecmp(end, ".local") || | |
4067 | !_cups_strncasecmp(end, ".local:", 7) || | |
4068 | !_cups_strcasecmp(end, ".local.") || | |
4069 | !_cups_strncasecmp(end, ".local.:", 8))) | |
e07d4801 | 4070 | return (1); |
37e7e6e0 | 4071 | #endif /* HAVE_DNSSD || HAVE_AVAHI */ |
e07d4801 MS |
4072 | |
4073 | /* | |
4074 | * Check if the hostname is an IP address... | |
4075 | */ | |
4076 | ||
4077 | if (isdigit(*host & 255) || *host == '[') | |
4078 | { | |
4079 | /* | |
4080 | * Possible IPv4/IPv6 address... | |
4081 | */ | |
4082 | ||
4083 | char temp[1024], /* Temporary string */ | |
4084 | *ptr; /* Pointer into temporary string */ | |
4085 | http_addrlist_t *addrlist; /* List of addresses */ | |
4086 | ||
4087 | ||
4088 | strlcpy(temp, host, sizeof(temp)); | |
4089 | if ((ptr = strrchr(temp, ':')) != NULL && !strchr(ptr, ']')) | |
4090 | *ptr = '\0'; /* Strip :port from host value */ | |
4091 | ||
4092 | if ((addrlist = httpAddrGetList(temp, AF_UNSPEC, NULL)) != NULL) | |
4093 | { | |
4094 | /* | |
4095 | * Good IPv4/IPv6 address... | |
4096 | */ | |
4097 | ||
4098 | httpAddrFreeList(addrlist); | |
4099 | return (1); | |
4100 | } | |
4101 | } | |
4102 | ||
4103 | /* | |
4104 | * Check for (alias) name matches... | |
4105 | */ | |
4106 | ||
4107 | for (a = (cupsd_alias_t *)cupsArrayFirst(ServerAlias); | |
4108 | a; | |
4109 | a = (cupsd_alias_t *)cupsArrayNext(ServerAlias)) | |
4110 | { | |
4111 | /* | |
4112 | * "ServerAlias *" allows all host values through... | |
4113 | */ | |
4114 | ||
4115 | if (!strcmp(a->name, "*")) | |
4116 | return (1); | |
4117 | ||
88f9aafc | 4118 | if (!_cups_strncasecmp(host, a->name, a->namelen)) |
e07d4801 MS |
4119 | { |
4120 | /* | |
4121 | * Prefix matches; check the character at the end - it must be ":", ".", | |
4122 | * ".:", or nul... | |
4123 | */ | |
4124 | ||
4125 | end = host + a->namelen; | |
4126 | ||
4127 | if (!*end || *end == ':' || (*end == '.' && (!end[1] || end[1] == ':'))) | |
4128 | return (1); | |
4129 | } | |
4130 | } | |
4131 | ||
37e7e6e0 | 4132 | #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI) |
e07d4801 MS |
4133 | for (a = (cupsd_alias_t *)cupsArrayFirst(DNSSDAlias); |
4134 | a; | |
4135 | a = (cupsd_alias_t *)cupsArrayNext(DNSSDAlias)) | |
4136 | { | |
4137 | /* | |
4138 | * "ServerAlias *" allows all host values through... | |
4139 | */ | |
4140 | ||
4141 | if (!strcmp(a->name, "*")) | |
4142 | return (1); | |
4143 | ||
88f9aafc | 4144 | if (!_cups_strncasecmp(host, a->name, a->namelen)) |
e07d4801 MS |
4145 | { |
4146 | /* | |
4147 | * Prefix matches; check the character at the end - it must be ":", ".", | |
4148 | * ".:", or nul... | |
4149 | */ | |
4150 | ||
4151 | end = host + a->namelen; | |
4152 | ||
4153 | if (!*end || *end == ':' || (*end == '.' && (!end[1] || end[1] == ':'))) | |
4154 | return (1); | |
4155 | } | |
4156 | } | |
37e7e6e0 | 4157 | #endif /* HAVE_DNSSD || HAVE_AVAHI */ |
e07d4801 MS |
4158 | |
4159 | /* | |
4160 | * Check for interface hostname matches... | |
4161 | */ | |
4162 | ||
4163 | for (netif = (cupsd_netif_t *)cupsArrayFirst(NetIFList); | |
4164 | netif; | |
4165 | netif = (cupsd_netif_t *)cupsArrayNext(NetIFList)) | |
4166 | { | |
88f9aafc | 4167 | if (!_cups_strncasecmp(host, netif->hostname, netif->hostlen)) |
e07d4801 MS |
4168 | { |
4169 | /* | |
4170 | * Prefix matches; check the character at the end - it must be ":", ".", | |
4171 | * ".:", or nul... | |
4172 | */ | |
4173 | ||
4174 | end = host + netif->hostlen; | |
4175 | ||
4176 | if (!*end || *end == ':' || (*end == '.' && (!end[1] || end[1] == ':'))) | |
4177 | return (1); | |
4178 | } | |
4179 | } | |
4180 | ||
4181 | return (0); | |
4182 | } | |
4183 | ||
4184 | ||
ef416fc2 | 4185 | /* |
a74454a7 | 4186 | * 'write_file()' - Send a file via HTTP. |
e1d6a774 | 4187 | */ |
4188 | ||
4189 | static int /* O - 0 on failure, 1 on success */ | |
a74454a7 | 4190 | write_file(cupsd_client_t *con, /* I - Client connection */ |
4191 | http_status_t code, /* I - HTTP status */ | |
4192 | char *filename, /* I - Filename */ | |
4193 | char *type, /* I - File type */ | |
4194 | struct stat *filestats) /* O - File information */ | |
e1d6a774 | 4195 | { |
4196 | con->file = open(filename, O_RDONLY); | |
4197 | ||
b9faaae1 | 4198 | cupsdLogMessage(CUPSD_LOG_DEBUG2, |
85dda01c MS |
4199 | "[Client %d] write_file code=%d, filename=\"%s\" (%d), " |
4200 | "type=\"%s\", filestats=%p", con->http.fd, | |
b9faaae1 | 4201 | code, filename, con->file, type ? type : "(null)", filestats); |
e1d6a774 | 4202 | |
4203 | if (con->file < 0) | |
4204 | return (0); | |
4205 | ||
4206 | fcntl(con->file, F_SETFD, fcntl(con->file, F_GETFD) | FD_CLOEXEC); | |
4207 | ||
4208 | con->pipe_pid = 0; | |
4209 | ||
5bd77a73 | 4210 | if (!cupsdSendHeader(con, code, type, CUPSD_AUTH_NONE)) |
e1d6a774 | 4211 | return (0); |
4212 | ||
4213 | if (httpPrintf(HTTP(con), "Last-Modified: %s\r\n", | |
4214 | httpGetDateString(filestats->st_mtime)) < 0) | |
4215 | return (0); | |
4216 | if (httpPrintf(HTTP(con), "Content-Length: " CUPS_LLFMT "\r\n", | |
4217 | CUPS_LLCAST filestats->st_size) < 0) | |
4218 | return (0); | |
4219 | if (httpPrintf(HTTP(con), "\r\n") < 0) | |
4220 | return (0); | |
4221 | ||
07725fee | 4222 | if (cupsdFlushHeader(con) < 0) |
4223 | return (0); | |
d09495fa | 4224 | |
cb7f98ee | 4225 | con->http.data_encoding = HTTP_ENCODING_LENGTH; |
e1d6a774 | 4226 | con->http.data_remaining = filestats->st_size; |
4227 | ||
4228 | if (con->http.data_remaining <= INT_MAX) | |
4229 | con->http._data_remaining = con->http.data_remaining; | |
4230 | else | |
4231 | con->http._data_remaining = INT_MAX; | |
4232 | ||
f7deaa1a | 4233 | cupsdAddSelect(con->http.fd, (cupsd_selfunc_t)cupsdReadClient, |
4234 | (cupsd_selfunc_t)cupsdWriteClient, con); | |
e1d6a774 | 4235 | |
4236 | return (1); | |
4237 | } | |
4238 | ||
4239 | ||
4240 | /* | |
f899b121 | 4241 | * 'write_pipe()' - Flag that data is available on the CGI pipe. |
4242 | */ | |
4243 | ||
4244 | static void | |
4245 | write_pipe(cupsd_client_t *con) /* I - Client connection */ | |
4246 | { | |
b9faaae1 | 4247 | cupsdLogMessage(CUPSD_LOG_DEBUG2, |
85dda01c MS |
4248 | "[Client %d] write_pipe CGI output on fd %d", |
4249 | con->http.fd, con->file); | |
f899b121 | 4250 | |
4251 | con->file_ready = 1; | |
4252 | ||
4253 | cupsdRemoveSelect(con->file); | |
4254 | cupsdAddSelect(con->http.fd, NULL, (cupsd_selfunc_t)cupsdWriteClient, con); | |
4255 | } | |
4256 | ||
4257 | ||
4258 | /* | |
cb7f98ee | 4259 | * End of "$Id: client.c 10886 2013-03-08 17:56:02Z mike $". |
ef416fc2 | 4260 | */ |