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