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