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