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