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