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