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