]> git.ipfire.org Git - thirdparty/cups.git/blob - scheduler/client.c
Merge changes from CUPS 1.4svn-r8362.
[thirdparty/cups.git] / scheduler / client.c
1 /*
2 * "$Id: client.c 7950 2008-09-17 00:21:59Z mike $"
3 *
4 * Client routines for the Common UNIX Printing System (CUPS) scheduler.
5 *
6 * Copyright 2007-2009 by Apple Inc.
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.
11 *
12 * These coded instructions, statements, and computer programs are the
13 * property of Apple Inc. and are protected by Federal copyright
14 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
15 * which should have been included with this file. If this file is
16 * file is missing or damaged, see the license at "http://www.cups.org/".
17 *
18 * Contents:
19 *
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 programs.
29 * cupsdWriteClient() - Write data to a client as needed.
30 * check_if_modified() - Decode an "If-Modified-Since" line.
31 * compare_clients() - Compare two client connections.
32 * data_ready() - Check whether data is available from a client.
33 * encrypt_client() - Enable encryption for the client...
34 * get_cdsa_certificate() - Convert a keychain name into the CFArrayRef
35 * required by SSLSetCertificate.
36 * get_file() - Get a filename and state info.
37 * install_conf_file() - Install a configuration file.
38 * is_cgi() - Is the resource a CGI script/program?
39 * is_path_absolute() - Is a path absolute and free of relative elements.
40 * make_certificate() - Make a self-signed SSL/TLS certificate.
41 * pipe_command() - Pipe the output of a command to the remote client.
42 * write_file() - Send a file via HTTP.
43 * write_pipe() - Flag that data is available on the CGI pipe.
44 */
45
46 /*
47 * Include necessary headers...
48 */
49
50 #include "cupsd.h"
51
52 #ifdef HAVE_CDSASSL
53 # include <Security/Security.h>
54 # ifdef HAVE_SECIDENTITYSEARCHPRIV_H
55 # include <Security/SecIdentitySearchPriv.h>
56 # else /* Declare prototype for function in that header... */
57 extern OSStatus SecIdentitySearchCreateWithPolicy(SecPolicyRef policy,
58 CFStringRef idString, CSSM_KEYUSE keyUsage,
59 CFTypeRef keychainOrArray,
60 Boolean returnOnlyValidIdentities,
61 SecIdentitySearchRef* searchRef);
62 # endif /* HAVE_SECIDENTITYSEARCHPRIV_H */
63 # ifdef HAVE_SECPOLICYPRIV_H
64 # include <Security/SecPolicyPriv.h>
65 # else /* Declare prototype for function in that header... */
66 extern OSStatus SecPolicySetValue(SecPolicyRef policyRef,
67 const CSSM_DATA *value);
68 # endif /* HAVE_SECPOLICYPRIV_H */
69 # ifdef HAVE_SECBASEPRIV_H
70 # include <Security/SecBasePriv.h>
71 # else /* Declare prototype for function in that header... */
72 extern const char *cssmErrorString(int error);
73 # endif /* HAVE_SECBASEPRIV_H */
74 #endif /* HAVE_CDSASSL */
75
76 #ifdef HAVE_GNUTLS
77 # include <gnutls/x509.h>
78 #endif /* HAVE_GNUTLS */
79
80 #ifdef HAVE_TCPD_H
81 # include <tcpd.h>
82 #endif /* HAVE_TCPD_H */
83
84
85 /*
86 * Local functions...
87 */
88
89 static int check_if_modified(cupsd_client_t *con,
90 struct stat *filestats);
91 static int compare_clients(cupsd_client_t *a, cupsd_client_t *b,
92 void *data);
93 static int data_ready(cupsd_client_t *con);
94 #ifdef HAVE_SSL
95 static int encrypt_client(cupsd_client_t *con);
96 #endif /* HAVE_SSL */
97 #ifdef HAVE_CDSASSL
98 static CFArrayRef get_cdsa_certificate(cupsd_client_t *con);
99 #endif /* HAVE_CDSASSL */
100 static char *get_file(cupsd_client_t *con, struct stat *filestats,
101 char *filename, int len);
102 static http_status_t install_conf_file(cupsd_client_t *con);
103 static int is_cgi(cupsd_client_t *con, const char *filename,
104 struct stat *filestats, mime_type_t *type);
105 static int is_path_absolute(const char *path);
106 #ifdef HAVE_SSL
107 static int make_certificate(cupsd_client_t *con);
108 #endif /* HAVE_SSL */
109 static int pipe_command(cupsd_client_t *con, int infile, int *outfile,
110 char *command, char *options, int root);
111 static int write_file(cupsd_client_t *con, http_status_t code,
112 char *filename, char *type,
113 struct stat *filestats);
114 static void write_pipe(cupsd_client_t *con);
115
116
117 /*
118 * 'cupsdAcceptClient()' - Accept a new client.
119 */
120
121 void
122 cupsdAcceptClient(cupsd_listener_t *lis)/* I - Listener socket */
123 {
124 int count; /* Count of connections on a host */
125 int val; /* Parameter value */
126 cupsd_client_t *con, /* New client pointer */
127 *tempcon; /* Temporary client pointer */
128 http_addrlist_t *addrlist, /* List of adddresses for host */
129 *addr; /* Current address */
130 socklen_t addrlen; /* Length of address */
131 char *hostname; /* Hostname for address */
132 http_addr_t temp; /* Temporary address variable */
133 static time_t last_dos = 0; /* Time of last DoS attack */
134 #ifdef HAVE_TCPD_H
135 struct request_info wrap_req; /* TCP wrappers request information */
136 #endif /* HAVE_TCPD_H */
137
138
139 cupsdLogMessage(CUPSD_LOG_DEBUG2,
140 "cupsdAcceptClient(lis=%p) %d Clients = %d",
141 lis, lis->fd, cupsArrayCount(Clients));
142
143 /*
144 * Make sure we don't have a full set of clients already...
145 */
146
147 if (cupsArrayCount(Clients) == MaxClients)
148 return;
149
150 /*
151 * Get a pointer to the next available client...
152 */
153
154 if (!Clients)
155 Clients = cupsArrayNew(NULL, NULL);
156
157 if (!Clients)
158 {
159 cupsdLogMessage(CUPSD_LOG_ERROR,
160 "Unable to allocate memory for clients array!");
161 cupsdPauseListening();
162 return;
163 }
164
165 if (!ActiveClients)
166 ActiveClients = cupsArrayNew((cups_array_func_t)compare_clients, NULL);
167
168 if (!ActiveClients)
169 {
170 cupsdLogMessage(CUPSD_LOG_ERROR,
171 "Unable to allocate memory for active clients array!");
172 cupsdPauseListening();
173 return;
174 }
175
176 if ((con = calloc(1, sizeof(cupsd_client_t))) == NULL)
177 {
178 cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to allocate memory for client!");
179 cupsdPauseListening();
180 return;
181 }
182
183 con->http.activity = time(NULL);
184 con->file = -1;
185 con->http.hostaddr = &(con->clientaddr);
186
187 /*
188 * Accept the client and get the remote address...
189 */
190
191 addrlen = sizeof(http_addr_t);
192
193 if ((con->http.fd = accept(lis->fd, (struct sockaddr *)con->http.hostaddr,
194 &addrlen)) < 0)
195 {
196 if (errno == ENFILE || errno == EMFILE)
197 cupsdPauseListening();
198
199 cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to accept client connection - %s.",
200 strerror(errno));
201 free(con);
202
203 return;
204 }
205
206 #ifdef AF_INET6
207 if (lis->address.addr.sa_family == AF_INET6)
208 {
209 /*
210 * Save the connected port number...
211 */
212
213 con->http.hostaddr->ipv6.sin6_port = lis->address.ipv6.sin6_port;
214
215 /*
216 * Convert IPv4 over IPv6 addresses (::ffff:n.n.n.n) to IPv4 forms we
217 * can more easily use...
218 */
219
220 if (con->http.hostaddr->ipv6.sin6_addr.s6_addr32[0] == 0 &&
221 con->http.hostaddr->ipv6.sin6_addr.s6_addr32[1] == 0 &&
222 ntohl(con->http.hostaddr->ipv6.sin6_addr.s6_addr32[2]) == 0xffff)
223 con->http.hostaddr->ipv6.sin6_addr.s6_addr32[2] = 0;
224 }
225 else
226 #endif /* AF_INET6 */
227 if (lis->address.addr.sa_family == AF_INET)
228 con->http.hostaddr->ipv4.sin_port = lis->address.ipv4.sin_port;
229
230 /*
231 * Check the number of clients on the same address...
232 */
233
234 for (count = 0, tempcon = (cupsd_client_t *)cupsArrayFirst(Clients);
235 tempcon;
236 tempcon = (cupsd_client_t *)cupsArrayNext(Clients))
237 if (httpAddrEqual(tempcon->http.hostaddr, con->http.hostaddr))
238 {
239 count ++;
240 if (count >= MaxClientsPerHost)
241 break;
242 }
243
244 if (count >= MaxClientsPerHost)
245 {
246 if ((time(NULL) - last_dos) >= 60)
247 {
248 last_dos = time(NULL);
249 cupsdLogMessage(CUPSD_LOG_WARN,
250 "Possible DoS attack - more than %d clients connecting "
251 "from %s!",
252 MaxClientsPerHost,
253 httpAddrString(con->http.hostaddr, con->http.hostname,
254 sizeof(con->http.hostname)));
255 }
256
257 #ifdef WIN32
258 closesocket(con->http.fd);
259 #else
260 close(con->http.fd);
261 #endif /* WIN32 */
262
263 free(con);
264 return;
265 }
266
267 /*
268 * Get the hostname or format the IP address as needed...
269 */
270
271 if (httpAddrLocalhost(con->http.hostaddr))
272 {
273 /*
274 * Map accesses from the loopback interface to "localhost"...
275 */
276
277 strlcpy(con->http.hostname, "localhost", sizeof(con->http.hostname));
278 hostname = con->http.hostname;
279 }
280 else
281 {
282 /*
283 * Map accesses from the same host to the server name.
284 */
285
286 if (HostNameLookups)
287 hostname = httpAddrLookup(con->http.hostaddr, con->http.hostname,
288 sizeof(con->http.hostname));
289 else
290 {
291 hostname = NULL;
292 httpAddrString(con->http.hostaddr, con->http.hostname,
293 sizeof(con->http.hostname));
294 }
295 }
296
297 if (hostname == NULL && HostNameLookups == 2)
298 {
299 /*
300 * Can't have an unresolved IP address with double-lookups enabled...
301 */
302
303 cupsdLogMessage(CUPSD_LOG_DEBUG2,
304 "cupsdAcceptClient: Closing connection %d...",
305 con->http.fd);
306
307 #ifdef WIN32
308 closesocket(con->http.fd);
309 #else
310 close(con->http.fd);
311 #endif /* WIN32 */
312
313 cupsdLogMessage(CUPSD_LOG_WARN,
314 "Name lookup failed - connection from %s closed!",
315 con->http.hostname);
316
317 free(con);
318 return;
319 }
320
321 if (HostNameLookups == 2)
322 {
323 /*
324 * Do double lookups as needed...
325 */
326
327 if ((addrlist = httpAddrGetList(con->http.hostname, AF_UNSPEC, NULL))
328 != NULL)
329 {
330 /*
331 * See if the hostname maps to the same IP address...
332 */
333
334 for (addr = addrlist; addr; addr = addr->next)
335 if (httpAddrEqual(con->http.hostaddr, &(addr->addr)))
336 break;
337 }
338 else
339 addr = NULL;
340
341 httpAddrFreeList(addrlist);
342
343 if (!addr)
344 {
345 /*
346 * Can't have a hostname that doesn't resolve to the same IP address
347 * with double-lookups enabled...
348 */
349
350 cupsdLogMessage(CUPSD_LOG_DEBUG2,
351 "cupsdAcceptClient: Closing connection %d...",
352 con->http.fd);
353
354 #ifdef WIN32
355 closesocket(con->http.fd);
356 #else
357 close(con->http.fd);
358 #endif /* WIN32 */
359
360 cupsdLogMessage(CUPSD_LOG_WARN,
361 "IP lookup failed - connection from %s closed!",
362 con->http.hostname);
363 free(con);
364 return;
365 }
366 }
367
368 #ifdef HAVE_TCPD_H
369 /*
370 * See if the connection is denied by TCP wrappers...
371 */
372
373 request_init(&wrap_req, RQ_DAEMON, "cupsd", RQ_FILE, con->http.fd, NULL);
374 fromhost(&wrap_req);
375
376 if (!hosts_access(&wrap_req))
377 {
378 cupsdLogMessage(CUPSD_LOG_DEBUG2,
379 "cupsdAcceptClient: Closing connection %d...",
380 con->http.fd);
381
382 #ifdef WIN32
383 closesocket(con->http.fd);
384 #else
385 close(con->http.fd);
386 #endif /* WIN32 */
387
388 cupsdLogMessage(CUPSD_LOG_WARN,
389 "Connection from %s refused by /etc/hosts.allow and "
390 "/etc/hosts.deny rules.", con->http.hostname);
391 free(con);
392 return;
393 }
394 #endif /* HAVE_TCPD_H */
395
396 #ifdef AF_INET6
397 if (con->http.hostaddr->addr.sa_family == AF_INET6)
398 cupsdLogMessage(CUPSD_LOG_DEBUG, "cupsdAcceptClient: %d from %s:%d (IPv6)",
399 con->http.fd, con->http.hostname,
400 ntohs(con->http.hostaddr->ipv6.sin6_port));
401 else
402 #endif /* AF_INET6 */
403 #ifdef AF_LOCAL
404 if (con->http.hostaddr->addr.sa_family == AF_LOCAL)
405 cupsdLogMessage(CUPSD_LOG_DEBUG, "cupsdAcceptClient: %d from %s (Domain)",
406 con->http.fd, con->http.hostname);
407 else
408 #endif /* AF_LOCAL */
409 cupsdLogMessage(CUPSD_LOG_DEBUG, "cupsdAcceptClient: %d from %s:%d (IPv4)",
410 con->http.fd, con->http.hostname,
411 ntohs(con->http.hostaddr->ipv4.sin_port));
412
413 /*
414 * Get the local address the client connected to...
415 */
416
417 addrlen = sizeof(temp);
418 if (getsockname(con->http.fd, (struct sockaddr *)&temp, &addrlen))
419 {
420 cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to get local address - %s",
421 strerror(errno));
422
423 strcpy(con->servername, "localhost");
424 con->serverport = LocalPort;
425 }
426 else
427 {
428 #ifdef AF_INET6
429 if (temp.addr.sa_family == AF_INET6)
430 {
431 if (HostNameLookups)
432 httpAddrLookup(&temp, con->servername, sizeof(con->servername));
433 else if (httpAddrLocalhost(&temp))
434 strlcpy(con->servername, "localhost", sizeof(con->servername));
435 else
436 httpAddrString(&temp, con->servername, sizeof(con->servername));
437
438 con->serverport = ntohs(lis->address.ipv6.sin6_port);
439 }
440 else
441 #endif /* AF_INET6 */
442 if (temp.addr.sa_family == AF_INET)
443 {
444 if (HostNameLookups)
445 httpAddrLookup(&temp, con->servername, sizeof(con->servername));
446 else if (httpAddrLocalhost(&temp))
447 strlcpy(con->servername, "localhost", sizeof(con->servername));
448 else
449 httpAddrString(&temp, con->servername, sizeof(con->servername));
450
451 con->serverport = ntohs(lis->address.ipv4.sin_port);
452 }
453 else
454 {
455 strcpy(con->servername, "localhost");
456 con->serverport = LocalPort;
457 }
458 }
459
460 cupsArrayAdd(Clients, con);
461
462 cupsdLogMessage(CUPSD_LOG_DEBUG2,
463 "cupsdAcceptClient: %d connected to server on %s:%d",
464 con->http.fd, con->servername, con->serverport);
465
466 /*
467 * Using TCP_NODELAY improves responsiveness, especially on systems
468 * with a slow loopback interface... Since we write large buffers
469 * when sending print files and requests, there shouldn't be any
470 * performance penalty for this...
471 */
472
473 val = 1;
474 setsockopt(con->http.fd, IPPROTO_TCP, TCP_NODELAY, &val, sizeof(val));
475
476 /*
477 * Close this file on all execs...
478 */
479
480 fcntl(con->http.fd, F_SETFD, fcntl(con->http.fd, F_GETFD) | FD_CLOEXEC);
481
482 /*
483 * Add the socket to the select() input mask.
484 */
485
486 cupsdAddSelect(con->http.fd, (cupsd_selfunc_t)cupsdReadClient, NULL, con);
487
488 /*
489 * Temporarily suspend accept()'s until we lose a client...
490 */
491
492 if (cupsArrayCount(Clients) == MaxClients)
493 cupsdPauseListening();
494
495 #ifdef HAVE_SSL
496 /*
497 * See if we are connecting on a secure port...
498 */
499
500 if (lis->encryption == HTTP_ENCRYPT_ALWAYS)
501 {
502 /*
503 * https connection; go secure...
504 */
505
506 con->http.encryption = HTTP_ENCRYPT_ALWAYS;
507
508 if (!encrypt_client(con))
509 cupsdCloseClient(con);
510 }
511 else
512 con->auto_ssl = 1;
513 #endif /* HAVE_SSL */
514 }
515
516
517 /*
518 * 'cupsdCloseAllClients()' - Close all remote clients immediately.
519 */
520
521 void
522 cupsdCloseAllClients(void)
523 {
524 cupsd_client_t *con; /* Current client */
525
526
527 for (con = (cupsd_client_t *)cupsArrayFirst(Clients);
528 con;
529 con = (cupsd_client_t *)cupsArrayNext(Clients))
530 if (cupsdCloseClient(con))
531 cupsdCloseClient(con);
532 }
533
534
535 /*
536 * 'cupsdCloseClient()' - Close a remote client.
537 */
538
539 int /* O - 1 if partial close, 0 if fully closed */
540 cupsdCloseClient(cupsd_client_t *con) /* I - Client to close */
541 {
542 int partial; /* Do partial close for SSL? */
543 #ifdef HAVE_LIBSSL
544 SSL_CTX *context; /* Context for encryption */
545 SSL *conn; /* Connection for encryption */
546 unsigned long error; /* Error code */
547 #elif defined(HAVE_GNUTLS)
548 http_tls_t *conn; /* TLS connection information */
549 int error; /* Error code */
550 gnutls_certificate_server_credentials *credentials;
551 /* TLS credentials */
552 # elif defined(HAVE_CDSASSL)
553 http_tls_t *conn; /* CDSA connection information */
554 #endif /* HAVE_LIBSSL */
555
556
557 cupsdLogMessage(CUPSD_LOG_DEBUG, "cupsdCloseClient: %d", con->http.fd);
558
559 /*
560 * Flush pending writes before closing...
561 */
562
563 httpFlushWrite(HTTP(con));
564
565 partial = 0;
566
567 #ifdef HAVE_SSL
568 /*
569 * Shutdown encryption as needed...
570 */
571
572 if (con->http.tls)
573 {
574 partial = 1;
575
576 # ifdef HAVE_LIBSSL
577 conn = (SSL *)(con->http.tls);
578 context = SSL_get_SSL_CTX(conn);
579
580 switch (SSL_shutdown(conn))
581 {
582 case 1 :
583 cupsdLogMessage(CUPSD_LOG_DEBUG,
584 "SSL shutdown successful!");
585 break;
586 case -1 :
587 cupsdLogMessage(CUPSD_LOG_ERROR,
588 "Fatal error during SSL shutdown!");
589 default :
590 while ((error = ERR_get_error()) != 0)
591 cupsdLogMessage(CUPSD_LOG_ERROR, "SSL shutdown failed: %s",
592 ERR_error_string(error, NULL));
593 break;
594 }
595
596 SSL_CTX_free(context);
597 SSL_free(conn);
598
599 # elif defined(HAVE_GNUTLS)
600 conn = (http_tls_t *)(con->http.tls);
601 credentials = (gnutls_certificate_server_credentials *)(conn->credentials);
602
603 error = gnutls_bye(conn->session, GNUTLS_SHUT_WR);
604 switch (error)
605 {
606 case GNUTLS_E_SUCCESS:
607 cupsdLogMessage(CUPSD_LOG_DEBUG,
608 "SSL shutdown successful!");
609 break;
610 default:
611 cupsdLogMessage(CUPSD_LOG_ERROR,
612 "SSL shutdown failed: %s", gnutls_strerror(error));
613 break;
614 }
615
616 gnutls_deinit(conn->session);
617 gnutls_certificate_free_credentials(*credentials);
618 free(credentials);
619 free(conn);
620
621 # elif defined(HAVE_CDSASSL)
622 conn = (http_tls_t *)(con->http.tls);
623
624 while (SSLClose(conn->session) == errSSLWouldBlock)
625 usleep(1000);
626
627 SSLDisposeContext(conn->session);
628
629 if (conn->certsArray)
630 CFRelease(conn->certsArray);
631
632 free(conn);
633 # endif /* HAVE_LIBSSL */
634
635 con->http.tls = NULL;
636 }
637 #endif /* HAVE_SSL */
638
639 if (con->pipe_pid != 0)
640 {
641 /*
642 * Stop any CGI process...
643 */
644
645 cupsdLogMessage(CUPSD_LOG_DEBUG2,
646 "cupsdCloseClient: %d Killing process ID %d...",
647 con->http.fd, con->pipe_pid);
648 cupsdEndProcess(con->pipe_pid, 1);
649 con->pipe_pid = 0;
650 }
651
652 if (con->file >= 0)
653 {
654 cupsdRemoveSelect(con->file);
655
656 cupsdLogMessage(CUPSD_LOG_DEBUG2,
657 "cupsdCloseClient: %d Closing data file %d.",
658 con->http.fd, con->file);
659
660 close(con->file);
661 con->file = -1;
662 }
663
664 /*
665 * Close the socket and clear the file from the input set for select()...
666 */
667
668 if (con->http.fd >= 0)
669 {
670 cupsArrayRemove(ActiveClients, con);
671 cupsdSetBusyState();
672
673 if (partial)
674 {
675 /*
676 * Only do a partial close so that the encrypted client gets everything.
677 */
678
679 shutdown(con->http.fd, 0);
680 cupsdAddSelect(con->http.fd, (cupsd_selfunc_t)cupsdReadClient, NULL, con);
681 }
682 else
683 {
684 /*
685 * Shut the socket down fully...
686 */
687
688 cupsdRemoveSelect(con->http.fd);
689 close(con->http.fd);
690 con->http.fd = -1;
691 }
692 }
693
694 if (!partial)
695 {
696 /*
697 * Free memory...
698 */
699
700 if (con->http.input_set)
701 free(con->http.input_set);
702
703 httpClearCookie(HTTP(con));
704 httpClearFields(HTTP(con));
705
706 cupsdClearString(&con->filename);
707 cupsdClearString(&con->command);
708 cupsdClearString(&con->options);
709 cupsdClearString(&con->query_string);
710
711 if (con->request)
712 {
713 ippDelete(con->request);
714 con->request = NULL;
715 }
716
717 if (con->response)
718 {
719 ippDelete(con->response);
720 con->response = NULL;
721 }
722
723 if (con->language)
724 {
725 cupsLangFree(con->language);
726 con->language = NULL;
727 }
728
729 #ifdef HAVE_AUTHORIZATION_H
730 if (con->authref)
731 {
732 AuthorizationFree(con->authref, kAuthorizationFlagDefaults);
733 con->authref = NULL;
734 }
735 #endif /* HAVE_AUTHORIZATION_H */
736
737 /*
738 * Re-enable new client connections if we are going back under the
739 * limit...
740 */
741
742 if (cupsArrayCount(Clients) == MaxClients)
743 cupsdResumeListening();
744
745 /*
746 * Compact the list of clients as necessary...
747 */
748
749 cupsArrayRemove(Clients, con);
750
751 free(con);
752 }
753
754 return (partial);
755 }
756
757
758 /*
759 * 'cupsdFlushHeader()' - Flush the header fields to the client.
760 */
761
762 int /* I - Bytes written or -1 on error */
763 cupsdFlushHeader(cupsd_client_t *con) /* I - Client to flush to */
764 {
765 int bytes = httpFlushWrite(HTTP(con));
766
767 con->http.data_encoding = HTTP_ENCODE_LENGTH;
768
769 return (bytes);
770 }
771
772
773 /*
774 * 'cupsdReadClient()' - Read data from a client.
775 */
776
777 void
778 cupsdReadClient(cupsd_client_t *con) /* I - Client to read from */
779 {
780 char line[32768], /* Line from client... */
781 operation[64], /* Operation code from socket */
782 version[64], /* HTTP version number string */
783 locale[64], /* Locale */
784 *ptr; /* Pointer into strings */
785 int major, minor; /* HTTP version numbers */
786 http_status_t status; /* Transfer status */
787 ipp_state_t ipp_state; /* State of IPP transfer */
788 int bytes; /* Number of bytes to POST */
789 char *filename; /* Name of file for GET/HEAD */
790 char buf[1024]; /* Buffer for real filename */
791 struct stat filestats; /* File information */
792 mime_type_t *type; /* MIME type of file */
793 cupsd_printer_t *p; /* Printer */
794 static unsigned request_id = 0; /* Request ID for temp files */
795
796
797 status = HTTP_CONTINUE;
798
799 cupsdLogMessage(CUPSD_LOG_DEBUG2,
800 "cupsdReadClient: %d, used=%d, file=%d state=%d",
801 con->http.fd, con->http.used, con->file, con->http.state);
802
803 if (con->http.error)
804 {
805 cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdReadClient: http error seen...");
806 cupsdCloseClient(con);
807 return;
808 }
809
810 #ifdef HAVE_SSL
811 if (con->auto_ssl)
812 {
813 /*
814 * Automatically check for a SSL/TLS handshake...
815 */
816
817 con->auto_ssl = 0;
818
819 if (recv(con->http.fd, buf, 1, MSG_PEEK) == 1 &&
820 (!buf[0] || !strchr("DGHOPT", buf[0])))
821 {
822 /*
823 * Encrypt this connection...
824 */
825
826 cupsdLogMessage(CUPSD_LOG_DEBUG2,
827 "cupsdReadClient: Saw first byte %02X, auto-negotiating SSL/TLS session...",
828 buf[0] & 255);
829
830 if (!encrypt_client(con))
831 cupsdCloseClient(con);
832
833 return;
834 }
835 }
836 #endif /* HAVE_SSL */
837
838 switch (con->http.state)
839 {
840 case HTTP_WAITING :
841 /*
842 * See if we've received a request line...
843 */
844
845 if (httpGets(line, sizeof(line) - 1, HTTP(con)) == NULL)
846 {
847 cupsdLogMessage(CUPSD_LOG_DEBUG2,
848 "cupsdReadClient: httpGets returned EOF...");
849 cupsdCloseClient(con);
850 return;
851 }
852
853 /*
854 * Ignore blank request lines...
855 */
856
857 if (line[0] == '\0')
858 break;
859
860 /*
861 * Clear other state variables...
862 */
863
864 httpClearFields(HTTP(con));
865
866 con->http.activity = time(NULL);
867 con->http.version = HTTP_1_0;
868 con->http.keep_alive = HTTP_KEEPALIVE_OFF;
869 con->http.data_encoding = HTTP_ENCODE_LENGTH;
870 con->http.data_remaining = 0;
871 con->http._data_remaining = 0;
872 con->operation = HTTP_WAITING;
873 con->bytes = 0;
874 con->file = -1;
875 con->file_ready = 0;
876 con->pipe_pid = 0;
877 con->username[0] = '\0';
878 con->password[0] = '\0';
879 con->uri[0] = '\0';
880
881 cupsdClearString(&con->command);
882 cupsdClearString(&con->options);
883 cupsdClearString(&con->query_string);
884
885 if (con->request)
886 {
887 ippDelete(con->request);
888 con->request = NULL;
889 }
890
891 if (con->response)
892 {
893 ippDelete(con->response);
894 con->response = NULL;
895 }
896
897 if (con->language)
898 {
899 cupsLangFree(con->language);
900 con->language = NULL;
901 }
902
903 #ifdef HAVE_GSSAPI
904 con->gss_have_creds = 0;
905 #endif /* HAVE_GSSAPI */
906
907 /*
908 * Grab the request line...
909 */
910
911 switch (sscanf(line, "%63s%1023s%63s", operation, con->uri, version))
912 {
913 case 1 :
914 if (line[0])
915 {
916 cupsdLogMessage(CUPSD_LOG_ERROR,
917 "Bad request line \"%s\" from %s!", line,
918 con->http.hostname);
919 cupsdSendError(con, HTTP_BAD_REQUEST, CUPSD_AUTH_NONE);
920 cupsdCloseClient(con);
921 }
922 return;
923 case 2 :
924 con->http.version = HTTP_0_9;
925 break;
926 case 3 :
927 if (sscanf(version, "HTTP/%d.%d", &major, &minor) != 2)
928 {
929 cupsdLogMessage(CUPSD_LOG_ERROR,
930 "Bad request line \"%s\" from %s!", line,
931 con->http.hostname);
932 cupsdSendError(con, HTTP_BAD_REQUEST, CUPSD_AUTH_NONE);
933 cupsdCloseClient(con);
934 return;
935 }
936
937 if (major < 2)
938 {
939 con->http.version = (http_version_t)(major * 100 + minor);
940 if (con->http.version == HTTP_1_1 && KeepAlive)
941 con->http.keep_alive = HTTP_KEEPALIVE_ON;
942 else
943 con->http.keep_alive = HTTP_KEEPALIVE_OFF;
944 }
945 else
946 {
947 cupsdSendError(con, HTTP_NOT_SUPPORTED, CUPSD_AUTH_NONE);
948 cupsdCloseClient(con);
949 return;
950 }
951 break;
952 }
953
954 /*
955 * Handle full URLs in the request line...
956 */
957
958 if (strcmp(con->uri, "*"))
959 {
960 char scheme[HTTP_MAX_URI], /* Method/scheme */
961 userpass[HTTP_MAX_URI], /* Username:password */
962 hostname[HTTP_MAX_URI], /* Hostname */
963 resource[HTTP_MAX_URI]; /* Resource path */
964 int port; /* Port number */
965
966
967 /*
968 * Separate the URI into its components...
969 */
970
971 httpSeparateURI(HTTP_URI_CODING_MOST, con->uri,
972 scheme, sizeof(scheme),
973 userpass, sizeof(userpass),
974 hostname, sizeof(hostname), &port,
975 resource, sizeof(resource));
976
977 /*
978 * Only allow URIs with the servername, localhost, or an IP
979 * address...
980 */
981
982 if (strcmp(scheme, "file") &&
983 strcasecmp(hostname, ServerName) &&
984 strcasecmp(hostname, "localhost") &&
985 !isdigit(hostname[0]) && hostname[0] != '[')
986 {
987 /*
988 * Nope, we don't do proxies...
989 */
990
991 cupsdLogMessage(CUPSD_LOG_ERROR, "Bad URI \"%s\" in request!",
992 con->uri);
993 cupsdSendError(con, HTTP_METHOD_NOT_ALLOWED, CUPSD_AUTH_NONE);
994 cupsdCloseClient(con);
995 return;
996 }
997
998 /*
999 * Copy the resource portion back into the URI; both resource and
1000 * con->uri are HTTP_MAX_URI bytes in size...
1001 */
1002
1003 strcpy(con->uri, resource);
1004 }
1005
1006 /*
1007 * Process the request...
1008 */
1009
1010 if (!strcmp(operation, "GET"))
1011 con->http.state = HTTP_GET;
1012 else if (!strcmp(operation, "PUT"))
1013 con->http.state = HTTP_PUT;
1014 else if (!strcmp(operation, "POST"))
1015 con->http.state = HTTP_POST;
1016 else if (!strcmp(operation, "DELETE"))
1017 con->http.state = HTTP_DELETE;
1018 else if (!strcmp(operation, "TRACE"))
1019 con->http.state = HTTP_TRACE;
1020 else if (!strcmp(operation, "OPTIONS"))
1021 con->http.state = HTTP_OPTIONS;
1022 else if (!strcmp(operation, "HEAD"))
1023 con->http.state = HTTP_HEAD;
1024 else
1025 {
1026 cupsdLogMessage(CUPSD_LOG_ERROR, "Bad operation \"%s\"!", operation);
1027 cupsdSendError(con, HTTP_BAD_REQUEST, CUPSD_AUTH_NONE);
1028 cupsdCloseClient(con);
1029 return;
1030 }
1031
1032 gettimeofday(&(con->start), NULL);
1033 con->operation = con->http.state;
1034
1035 cupsdLogMessage(CUPSD_LOG_DEBUG, "cupsdReadClient: %d %s %s HTTP/%d.%d",
1036 con->http.fd, operation, con->uri,
1037 con->http.version / 100, con->http.version % 100);
1038
1039 con->http.status = HTTP_OK;
1040
1041 if (!cupsArrayFind(ActiveClients, con))
1042 {
1043 cupsArrayAdd(ActiveClients, con);
1044 cupsdSetBusyState();
1045 }
1046
1047 case HTTP_OPTIONS :
1048 case HTTP_DELETE :
1049 case HTTP_GET :
1050 case HTTP_HEAD :
1051 case HTTP_POST :
1052 case HTTP_PUT :
1053 case HTTP_TRACE :
1054 /*
1055 * Parse incoming parameters until the status changes...
1056 */
1057
1058 while ((status = httpUpdate(HTTP(con))) == HTTP_CONTINUE)
1059 if (!data_ready(con))
1060 break;
1061
1062 if (status != HTTP_OK && status != HTTP_CONTINUE)
1063 {
1064 cupsdSendError(con, HTTP_BAD_REQUEST, CUPSD_AUTH_NONE);
1065 cupsdCloseClient(con);
1066 return;
1067 }
1068 break;
1069
1070 default :
1071 if (!data_ready(con) && recv(con->http.fd, buf, 1, MSG_PEEK) < 1)
1072 {
1073 /*
1074 * Connection closed...
1075 */
1076
1077 cupsdCloseClient(con);
1078 return;
1079 }
1080 break; /* Anti-compiler-warning-code */
1081 }
1082
1083 /*
1084 * Handle new transfers...
1085 */
1086
1087 if (status == HTTP_OK)
1088 {
1089 if (con->http.fields[HTTP_FIELD_ACCEPT_LANGUAGE][0])
1090 {
1091 /*
1092 * Figure out the locale from the Accept-Language and Content-Type
1093 * fields...
1094 */
1095
1096 if ((ptr = strchr(con->http.fields[HTTP_FIELD_ACCEPT_LANGUAGE], ',')) != NULL)
1097 *ptr = '\0';
1098
1099 if ((ptr = strchr(con->http.fields[HTTP_FIELD_ACCEPT_LANGUAGE], ';')) != NULL)
1100 *ptr = '\0';
1101
1102 if ((ptr = strstr(con->http.fields[HTTP_FIELD_CONTENT_TYPE], "charset=")) != NULL)
1103 {
1104 /*
1105 * Combine language and charset, and trim any extra params in the
1106 * content-type.
1107 */
1108
1109 snprintf(locale, sizeof(locale), "%s.%s",
1110 con->http.fields[HTTP_FIELD_ACCEPT_LANGUAGE], ptr + 8);
1111
1112 if ((ptr = strchr(locale, ',')) != NULL)
1113 *ptr = '\0';
1114 }
1115 else
1116 snprintf(locale, sizeof(locale), "%s.%s",
1117 con->http.fields[HTTP_FIELD_ACCEPT_LANGUAGE], DefaultCharset);
1118
1119 cupsdLogMessage(CUPSD_LOG_DEBUG2,
1120 "cupsdReadClient: %d Browser asked for language \"%s\"...",
1121 con->http.fd, locale);
1122
1123 con->language = cupsLangGet(locale);
1124 }
1125 else
1126 con->language = cupsLangGet(DefaultLocale);
1127
1128 cupsdAuthorize(con);
1129
1130 if (!strncasecmp(con->http.fields[HTTP_FIELD_CONNECTION], "Keep-Alive", 10) &&
1131 KeepAlive)
1132 con->http.keep_alive = HTTP_KEEPALIVE_ON;
1133 else if (!strncasecmp(con->http.fields[HTTP_FIELD_CONNECTION], "close", 5))
1134 con->http.keep_alive = HTTP_KEEPALIVE_OFF;
1135
1136 if (!con->http.fields[HTTP_FIELD_HOST][0] &&
1137 con->http.version >= HTTP_1_1)
1138 {
1139 /*
1140 * HTTP/1.1 and higher require the "Host:" field...
1141 */
1142
1143 if (!cupsdSendError(con, HTTP_BAD_REQUEST, CUPSD_AUTH_NONE))
1144 {
1145 cupsdCloseClient(con);
1146 return;
1147 }
1148 }
1149 else if (httpAddrLocalhost(con->http.hostaddr) &&
1150 strcasecmp(con->http.fields[HTTP_FIELD_HOST], "localhost") &&
1151 strncasecmp(con->http.fields[HTTP_FIELD_HOST], "localhost:", 10) &&
1152 strcmp(con->http.fields[HTTP_FIELD_HOST], "127.0.0.1") &&
1153 strncmp(con->http.fields[HTTP_FIELD_HOST], "127.0.0.1:", 10) &&
1154 strcmp(con->http.fields[HTTP_FIELD_HOST], "[::1]") &&
1155 strncmp(con->http.fields[HTTP_FIELD_HOST], "[::1]:", 6))
1156 {
1157 /*
1158 * Access to localhost must use "localhost" or the corresponding IPv4
1159 * or IPv6 values in the Host: field.
1160 */
1161
1162 cupsdLogMessage(CUPSD_LOG_WARN,
1163 "Request from \"%s\" using invalid Host: field \"%s\"",
1164 con->http.hostname, con->http.fields[HTTP_FIELD_HOST]);
1165
1166 if (!cupsdSendError(con, HTTP_BAD_REQUEST, CUPSD_AUTH_NONE))
1167 {
1168 cupsdCloseClient(con);
1169 return;
1170 }
1171 }
1172 else if (con->operation == HTTP_OPTIONS)
1173 {
1174 /*
1175 * Do OPTIONS command...
1176 */
1177
1178 if (con->best && con->best->type != CUPSD_AUTH_NONE)
1179 {
1180 if (!cupsdSendHeader(con, HTTP_UNAUTHORIZED, NULL, CUPSD_AUTH_NONE))
1181 {
1182 cupsdCloseClient(con);
1183 return;
1184 }
1185 }
1186
1187 if (!strcasecmp(con->http.fields[HTTP_FIELD_CONNECTION], "Upgrade") &&
1188 con->http.tls == NULL)
1189 {
1190 #ifdef HAVE_SSL
1191 /*
1192 * Do encryption stuff...
1193 */
1194
1195 if (!cupsdSendHeader(con, HTTP_SWITCHING_PROTOCOLS, NULL, CUPSD_AUTH_NONE))
1196 {
1197 cupsdCloseClient(con);
1198 return;
1199 }
1200
1201 httpPrintf(HTTP(con), "Connection: Upgrade\r\n");
1202 httpPrintf(HTTP(con), "Upgrade: TLS/1.0,HTTP/1.1\r\n");
1203 httpPrintf(HTTP(con), "Content-Length: 0\r\n");
1204 httpPrintf(HTTP(con), "\r\n");
1205
1206 if (cupsdFlushHeader(con) < 0)
1207 {
1208 cupsdCloseClient(con);
1209 return;
1210 }
1211
1212 if (!encrypt_client(con))
1213 {
1214 cupsdCloseClient(con);
1215 return;
1216 }
1217 #else
1218 if (!cupsdSendError(con, HTTP_NOT_IMPLEMENTED, CUPSD_AUTH_NONE))
1219 {
1220 cupsdCloseClient(con);
1221 return;
1222 }
1223 #endif /* HAVE_SSL */
1224 }
1225
1226 if (!cupsdSendHeader(con, HTTP_OK, NULL, CUPSD_AUTH_NONE))
1227 {
1228 cupsdCloseClient(con);
1229 return;
1230 }
1231
1232 httpPrintf(HTTP(con), "Allow: GET, HEAD, OPTIONS, POST, PUT\r\n");
1233 httpPrintf(HTTP(con), "Content-Length: 0\r\n");
1234 httpPrintf(HTTP(con), "\r\n");
1235
1236 if (cupsdFlushHeader(con) < 0)
1237 {
1238 cupsdCloseClient(con);
1239 return;
1240 }
1241 }
1242 else if (!is_path_absolute(con->uri))
1243 {
1244 /*
1245 * Protect against malicious users!
1246 */
1247
1248 cupsdLogMessage(CUPSD_LOG_ERROR,
1249 "Request for non-absolute resource \"%s\"!", con->uri);
1250
1251 if (!cupsdSendError(con, HTTP_FORBIDDEN, CUPSD_AUTH_NONE))
1252 {
1253 cupsdCloseClient(con);
1254 return;
1255 }
1256 }
1257 else
1258 {
1259 if (!strcasecmp(con->http.fields[HTTP_FIELD_CONNECTION], "Upgrade") &&
1260 con->http.tls == NULL)
1261 {
1262 #ifdef HAVE_SSL
1263 /*
1264 * Do encryption stuff...
1265 */
1266
1267 if (!cupsdSendHeader(con, HTTP_SWITCHING_PROTOCOLS, NULL, CUPSD_AUTH_NONE))
1268 {
1269 cupsdCloseClient(con);
1270 return;
1271 }
1272
1273 httpPrintf(HTTP(con), "Connection: Upgrade\r\n");
1274 httpPrintf(HTTP(con), "Upgrade: TLS/1.0,HTTP/1.1\r\n");
1275 httpPrintf(HTTP(con), "Content-Length: 0\r\n");
1276 httpPrintf(HTTP(con), "\r\n");
1277
1278 if (cupsdFlushHeader(con) < 0)
1279 {
1280 cupsdCloseClient(con);
1281 return;
1282 }
1283
1284 if (!encrypt_client(con))
1285 {
1286 cupsdCloseClient(con);
1287 return;
1288 }
1289 #else
1290 if (!cupsdSendError(con, HTTP_NOT_IMPLEMENTED, CUPSD_AUTH_NONE))
1291 {
1292 cupsdCloseClient(con);
1293 return;
1294 }
1295 #endif /* HAVE_SSL */
1296 }
1297
1298 if ((status = cupsdIsAuthorized(con, NULL)) != HTTP_OK)
1299 {
1300 cupsdLogMessage(CUPSD_LOG_DEBUG2,
1301 "cupsdReadClient: Unauthorized request for %s...",
1302 con->uri);
1303 cupsdSendError(con, status, CUPSD_AUTH_NONE);
1304 cupsdCloseClient(con);
1305 return;
1306 }
1307
1308 if (con->http.expect &&
1309 (con->operation == HTTP_POST || con->operation == HTTP_PUT))
1310 {
1311 if (con->http.expect == HTTP_CONTINUE)
1312 {
1313 /*
1314 * Send 100-continue header...
1315 */
1316
1317 if (!cupsdSendHeader(con, HTTP_CONTINUE, NULL, CUPSD_AUTH_NONE))
1318 {
1319 cupsdCloseClient(con);
1320 return;
1321 }
1322 }
1323 else
1324 {
1325 /*
1326 * Send 417-expectation-failed header...
1327 */
1328
1329 if (!cupsdSendHeader(con, HTTP_EXPECTATION_FAILED, NULL, CUPSD_AUTH_NONE))
1330 {
1331 cupsdCloseClient(con);
1332 return;
1333 }
1334
1335 httpPrintf(HTTP(con), "Content-Length: 0\r\n");
1336 httpPrintf(HTTP(con), "\r\n");
1337
1338 if (cupsdFlushHeader(con) < 0)
1339 {
1340 cupsdCloseClient(con);
1341 return;
1342 }
1343 }
1344 }
1345
1346 switch (con->http.state)
1347 {
1348 case HTTP_GET_SEND :
1349 if (!strncmp(con->uri, "/printers/", 10) &&
1350 !strcmp(con->uri + strlen(con->uri) - 4, ".ppd"))
1351 {
1352 /*
1353 * Send PPD file - get the real printer name since printer
1354 * names are not case sensitive but filenames can be...
1355 */
1356
1357 con->uri[strlen(con->uri) - 4] = '\0'; /* Drop ".ppd" */
1358
1359 if ((p = cupsdFindPrinter(con->uri + 10)) != NULL)
1360 snprintf(con->uri, sizeof(con->uri), "/ppd/%s.ppd", p->name);
1361 else
1362 {
1363 if (!cupsdSendError(con, HTTP_NOT_FOUND, CUPSD_AUTH_NONE))
1364 {
1365 cupsdCloseClient(con);
1366 return;
1367 }
1368
1369 break;
1370 }
1371 }
1372
1373 if ((!strncmp(con->uri, "/admin", 6) &&
1374 strncmp(con->uri, "/admin/conf/", 12) &&
1375 strncmp(con->uri, "/admin/log/", 11)) ||
1376 !strncmp(con->uri, "/printers", 9) ||
1377 !strncmp(con->uri, "/classes", 8) ||
1378 !strncmp(con->uri, "/help", 5) ||
1379 !strncmp(con->uri, "/jobs", 5))
1380 {
1381 /*
1382 * Send CGI output...
1383 */
1384
1385 if (!strncmp(con->uri, "/admin", 6))
1386 {
1387 cupsdSetStringf(&con->command, "%s/cgi-bin/admin.cgi",
1388 ServerBin);
1389
1390 cupsdSetString(&con->options, strchr(con->uri + 6, '?'));
1391 }
1392 else if (!strncmp(con->uri, "/printers", 9))
1393 {
1394 cupsdSetStringf(&con->command, "%s/cgi-bin/printers.cgi",
1395 ServerBin);
1396
1397 if (con->uri[9] && con->uri[10])
1398 cupsdSetString(&con->options, con->uri + 9);
1399 else
1400 cupsdSetString(&con->options, NULL);
1401 }
1402 else if (!strncmp(con->uri, "/classes", 8))
1403 {
1404 cupsdSetStringf(&con->command, "%s/cgi-bin/classes.cgi",
1405 ServerBin);
1406
1407 if (con->uri[8] && con->uri[9])
1408 cupsdSetString(&con->options, con->uri + 8);
1409 else
1410 cupsdSetString(&con->options, NULL);
1411 }
1412 else if (!strncmp(con->uri, "/jobs", 5))
1413 {
1414 cupsdSetStringf(&con->command, "%s/cgi-bin/jobs.cgi",
1415 ServerBin);
1416
1417 if (con->uri[5] && con->uri[6])
1418 cupsdSetString(&con->options, con->uri + 5);
1419 else
1420 cupsdSetString(&con->options, NULL);
1421 }
1422 else
1423 {
1424 cupsdSetStringf(&con->command, "%s/cgi-bin/help.cgi",
1425 ServerBin);
1426
1427 if (con->uri[5] && con->uri[6])
1428 cupsdSetString(&con->options, con->uri + 5);
1429 else
1430 cupsdSetString(&con->options, NULL);
1431 }
1432
1433 if (!cupsdSendCommand(con, con->command, con->options, 0))
1434 {
1435 if (!cupsdSendError(con, HTTP_NOT_FOUND, CUPSD_AUTH_NONE))
1436 {
1437 cupsdCloseClient(con);
1438 return;
1439 }
1440 }
1441 else
1442 cupsdLogRequest(con, HTTP_OK);
1443
1444 if (con->http.version <= HTTP_1_0)
1445 con->http.keep_alive = HTTP_KEEPALIVE_OFF;
1446 }
1447 else if ((!strncmp(con->uri, "/admin/conf/", 12) &&
1448 (strchr(con->uri + 12, '/') ||
1449 strlen(con->uri) == 12)) ||
1450 (!strncmp(con->uri, "/admin/log/", 11) &&
1451 (strchr(con->uri + 11, '/') ||
1452 strlen(con->uri) == 11)))
1453 {
1454 /*
1455 * GET can only be done to configuration files directly under
1456 * /admin/conf...
1457 */
1458
1459 cupsdLogMessage(CUPSD_LOG_ERROR,
1460 "Request for subdirectory \"%s\"!", con->uri);
1461
1462 if (!cupsdSendError(con, HTTP_FORBIDDEN, CUPSD_AUTH_NONE))
1463 {
1464 cupsdCloseClient(con);
1465 return;
1466 }
1467
1468 break;
1469 }
1470 else
1471 {
1472 /*
1473 * Serve a file...
1474 */
1475
1476 if ((filename = get_file(con, &filestats, buf,
1477 sizeof(buf))) == NULL)
1478 {
1479 if (!cupsdSendError(con, HTTP_NOT_FOUND, CUPSD_AUTH_NONE))
1480 {
1481 cupsdCloseClient(con);
1482 return;
1483 }
1484
1485 break;
1486 }
1487
1488 type = mimeFileType(MimeDatabase, filename, NULL, NULL);
1489
1490 if (is_cgi(con, filename, &filestats, type))
1491 {
1492 /*
1493 * Note: con->command and con->options were set by
1494 * is_cgi()...
1495 */
1496
1497 if (!cupsdSendCommand(con, con->command, con->options, 0))
1498 {
1499 if (!cupsdSendError(con, HTTP_NOT_FOUND, CUPSD_AUTH_NONE))
1500 {
1501 cupsdCloseClient(con);
1502 return;
1503 }
1504 }
1505 else
1506 cupsdLogRequest(con, HTTP_OK);
1507
1508 if (con->http.version <= HTTP_1_0)
1509 con->http.keep_alive = HTTP_KEEPALIVE_OFF;
1510 break;
1511 }
1512
1513 if (!check_if_modified(con, &filestats))
1514 {
1515 if (!cupsdSendError(con, HTTP_NOT_MODIFIED, CUPSD_AUTH_NONE))
1516 {
1517 cupsdCloseClient(con);
1518 return;
1519 }
1520 }
1521 else
1522 {
1523 if (type == NULL)
1524 strcpy(line, "text/plain");
1525 else
1526 snprintf(line, sizeof(line), "%s/%s", type->super, type->type);
1527
1528 if (!write_file(con, HTTP_OK, filename, line, &filestats))
1529 {
1530 cupsdCloseClient(con);
1531 return;
1532 }
1533 }
1534 }
1535 break;
1536
1537 case HTTP_POST_RECV :
1538 /*
1539 * See if the POST request includes a Content-Length field, and if
1540 * so check the length against any limits that are set...
1541 */
1542
1543 cupsdLogMessage(CUPSD_LOG_DEBUG2, "POST %s", con->uri);
1544 cupsdLogMessage(CUPSD_LOG_DEBUG2, "CONTENT_TYPE = %s",
1545 con->http.fields[HTTP_FIELD_CONTENT_TYPE]);
1546
1547 if (con->http.fields[HTTP_FIELD_CONTENT_LENGTH][0] &&
1548 MaxRequestSize > 0 &&
1549 con->http.data_remaining > MaxRequestSize)
1550 {
1551 /*
1552 * Request too large...
1553 */
1554
1555 if (!cupsdSendError(con, HTTP_REQUEST_TOO_LARGE, CUPSD_AUTH_NONE))
1556 {
1557 cupsdCloseClient(con);
1558 return;
1559 }
1560
1561 break;
1562 }
1563 else if (con->http.data_remaining < 0 ||
1564 (!con->http.fields[HTTP_FIELD_CONTENT_LENGTH][0] &&
1565 con->http.data_encoding == HTTP_ENCODE_LENGTH))
1566 {
1567 /*
1568 * Negative content lengths are invalid!
1569 */
1570
1571 if (!cupsdSendError(con, HTTP_BAD_REQUEST, CUPSD_AUTH_NONE))
1572 {
1573 cupsdCloseClient(con);
1574 return;
1575 }
1576
1577 break;
1578 }
1579
1580 /*
1581 * See what kind of POST request this is; for IPP requests the
1582 * content-type field will be "application/ipp"...
1583 */
1584
1585 if (!strcmp(con->http.fields[HTTP_FIELD_CONTENT_TYPE],
1586 "application/ipp"))
1587 con->request = ippNew();
1588 else if ((!strncmp(con->uri, "/admin", 6) &&
1589 strncmp(con->uri, "/admin/conf/", 12) &&
1590 strncmp(con->uri, "/admin/log/", 11)) ||
1591 !strncmp(con->uri, "/printers", 9) ||
1592 !strncmp(con->uri, "/classes", 8) ||
1593 !strncmp(con->uri, "/help", 5) ||
1594 !strncmp(con->uri, "/jobs", 5))
1595 {
1596 /*
1597 * CGI request...
1598 */
1599
1600 if (!strncmp(con->uri, "/admin", 6))
1601 {
1602 cupsdSetStringf(&con->command, "%s/cgi-bin/admin.cgi",
1603 ServerBin);
1604
1605 cupsdSetString(&con->options, strchr(con->uri + 6, '?'));
1606 }
1607 else if (!strncmp(con->uri, "/printers", 9))
1608 {
1609 cupsdSetStringf(&con->command, "%s/cgi-bin/printers.cgi",
1610 ServerBin);
1611
1612 if (con->uri[9] && con->uri[10])
1613 cupsdSetString(&con->options, con->uri + 9);
1614 else
1615 cupsdSetString(&con->options, NULL);
1616 }
1617 else if (!strncmp(con->uri, "/classes", 8))
1618 {
1619 cupsdSetStringf(&con->command, "%s/cgi-bin/classes.cgi",
1620 ServerBin);
1621
1622 if (con->uri[8] && con->uri[9])
1623 cupsdSetString(&con->options, con->uri + 8);
1624 else
1625 cupsdSetString(&con->options, NULL);
1626 }
1627 else if (!strncmp(con->uri, "/jobs", 5))
1628 {
1629 cupsdSetStringf(&con->command, "%s/cgi-bin/jobs.cgi",
1630 ServerBin);
1631
1632 if (con->uri[5] && con->uri[6])
1633 cupsdSetString(&con->options, con->uri + 5);
1634 else
1635 cupsdSetString(&con->options, NULL);
1636 }
1637 else
1638 {
1639 cupsdSetStringf(&con->command, "%s/cgi-bin/help.cgi",
1640 ServerBin);
1641
1642 if (con->uri[5] && con->uri[6])
1643 cupsdSetString(&con->options, con->uri + 5);
1644 else
1645 cupsdSetString(&con->options, NULL);
1646 }
1647
1648 cupsdLogMessage(CUPSD_LOG_DEBUG2,
1649 "cupsdReadClient: %d command=\"%s\", "
1650 "options = \"%s\"",
1651 con->http.fd, con->command,
1652 con->options ? con->options : "(null)");
1653
1654 if (con->http.version <= HTTP_1_0)
1655 con->http.keep_alive = HTTP_KEEPALIVE_OFF;
1656 }
1657 else
1658 {
1659 /*
1660 * POST to a file...
1661 */
1662
1663 if ((filename = get_file(con, &filestats, buf,
1664 sizeof(buf))) == NULL)
1665 {
1666 if (!cupsdSendError(con, HTTP_NOT_FOUND, CUPSD_AUTH_NONE))
1667 {
1668 cupsdCloseClient(con);
1669 return;
1670 }
1671
1672 break;
1673 }
1674
1675 type = mimeFileType(MimeDatabase, filename, NULL, NULL);
1676
1677 if (!is_cgi(con, filename, &filestats, type))
1678 {
1679 /*
1680 * Only POST to CGI's...
1681 */
1682
1683 if (!cupsdSendError(con, HTTP_UNAUTHORIZED, CUPSD_AUTH_NONE))
1684 {
1685 cupsdCloseClient(con);
1686 return;
1687 }
1688 }
1689 }
1690 break;
1691
1692 case HTTP_PUT_RECV :
1693 /*
1694 * Validate the resource name...
1695 */
1696
1697 if (strncmp(con->uri, "/admin/conf/", 12) ||
1698 strchr(con->uri + 12, '/') ||
1699 strlen(con->uri) == 12)
1700 {
1701 /*
1702 * PUT can only be done to configuration files under
1703 * /admin/conf...
1704 */
1705
1706 cupsdLogMessage(CUPSD_LOG_ERROR,
1707 "Request for subdirectory \"%s\"!", con->uri);
1708
1709 if (!cupsdSendError(con, HTTP_FORBIDDEN, CUPSD_AUTH_NONE))
1710 {
1711 cupsdCloseClient(con);
1712 return;
1713 }
1714
1715 break;
1716 }
1717
1718 /*
1719 * See if the PUT request includes a Content-Length field, and if
1720 * so check the length against any limits that are set...
1721 */
1722
1723 cupsdLogMessage(CUPSD_LOG_DEBUG2, "PUT %s", con->uri);
1724 cupsdLogMessage(CUPSD_LOG_DEBUG2, "CONTENT_TYPE = %s",
1725 con->http.fields[HTTP_FIELD_CONTENT_TYPE]);
1726
1727 if (con->http.fields[HTTP_FIELD_CONTENT_LENGTH][0] &&
1728 MaxRequestSize > 0 &&
1729 con->http.data_remaining > MaxRequestSize)
1730 {
1731 /*
1732 * Request too large...
1733 */
1734
1735 if (!cupsdSendError(con, HTTP_REQUEST_TOO_LARGE, CUPSD_AUTH_NONE))
1736 {
1737 cupsdCloseClient(con);
1738 return;
1739 }
1740
1741 break;
1742 }
1743 else if (con->http.data_remaining < 0)
1744 {
1745 /*
1746 * Negative content lengths are invalid!
1747 */
1748
1749 if (!cupsdSendError(con, HTTP_BAD_REQUEST, CUPSD_AUTH_NONE))
1750 {
1751 cupsdCloseClient(con);
1752 return;
1753 }
1754
1755 break;
1756 }
1757
1758 /*
1759 * Open a temporary file to hold the request...
1760 */
1761
1762 cupsdSetStringf(&con->filename, "%s/%08x", RequestRoot,
1763 request_id ++);
1764 con->file = open(con->filename, O_WRONLY | O_CREAT | O_TRUNC, 0640);
1765
1766 if (con->file < 0)
1767 {
1768 cupsdLogMessage(CUPSD_LOG_ERROR,
1769 "Unable to create request file %s: %s",
1770 con->filename, strerror(errno));
1771
1772 if (!cupsdSendError(con, HTTP_REQUEST_TOO_LARGE, CUPSD_AUTH_NONE))
1773 {
1774 cupsdCloseClient(con);
1775 return;
1776 }
1777 }
1778
1779 cupsdLogMessage(CUPSD_LOG_DEBUG2,
1780 "cupsdReadClient: %d REQUEST %s=%d", con->http.fd,
1781 con->filename, con->file);
1782
1783 fchmod(con->file, 0640);
1784 fchown(con->file, RunUser, Group);
1785 fcntl(con->file, F_SETFD, fcntl(con->file, F_GETFD) | FD_CLOEXEC);
1786 break;
1787
1788 case HTTP_DELETE :
1789 case HTTP_TRACE :
1790 cupsdSendError(con, HTTP_NOT_IMPLEMENTED, CUPSD_AUTH_NONE);
1791 cupsdCloseClient(con);
1792 return;
1793
1794 case HTTP_HEAD :
1795 if (!strncmp(con->uri, "/printers/", 10) &&
1796 !strcmp(con->uri + strlen(con->uri) - 4, ".ppd"))
1797 {
1798 /*
1799 * Send PPD file - get the real printer name since printer
1800 * names are not case sensitive but filenames can be...
1801 */
1802
1803 con->uri[strlen(con->uri) - 4] = '\0'; /* Drop ".ppd" */
1804
1805 if ((p = cupsdFindPrinter(con->uri + 10)) != NULL)
1806 snprintf(con->uri, sizeof(con->uri), "/ppd/%s.ppd", p->name);
1807 else
1808 {
1809 if (!cupsdSendError(con, HTTP_NOT_FOUND, CUPSD_AUTH_NONE))
1810 {
1811 cupsdCloseClient(con);
1812 return;
1813 }
1814
1815 break;
1816 }
1817 }
1818
1819 if ((!strncmp(con->uri, "/admin", 6) &&
1820 strncmp(con->uri, "/admin/conf/", 12) &&
1821 strncmp(con->uri, "/admin/log/", 11)) ||
1822 !strncmp(con->uri, "/printers", 9) ||
1823 !strncmp(con->uri, "/classes", 8) ||
1824 !strncmp(con->uri, "/help", 5) ||
1825 !strncmp(con->uri, "/jobs", 5))
1826 {
1827 /*
1828 * CGI output...
1829 */
1830
1831 if (!cupsdSendHeader(con, HTTP_OK, "text/html", CUPSD_AUTH_NONE))
1832 {
1833 cupsdCloseClient(con);
1834 return;
1835 }
1836
1837 if (httpPrintf(HTTP(con), "\r\n") < 0)
1838 {
1839 cupsdCloseClient(con);
1840 return;
1841 }
1842
1843 if (cupsdFlushHeader(con) < 0)
1844 {
1845 cupsdCloseClient(con);
1846 return;
1847 }
1848
1849 cupsdLogRequest(con, HTTP_OK);
1850 }
1851 else if ((!strncmp(con->uri, "/admin/conf/", 12) &&
1852 (strchr(con->uri + 12, '/') ||
1853 strlen(con->uri) == 12)) ||
1854 (!strncmp(con->uri, "/admin/log/", 11) &&
1855 (strchr(con->uri + 11, '/') ||
1856 strlen(con->uri) == 11)))
1857 {
1858 /*
1859 * HEAD can only be done to configuration files under
1860 * /admin/conf...
1861 */
1862
1863 cupsdLogMessage(CUPSD_LOG_ERROR,
1864 "Request for subdirectory \"%s\"!", con->uri);
1865
1866 if (!cupsdSendError(con, HTTP_FORBIDDEN, CUPSD_AUTH_NONE))
1867 {
1868 cupsdCloseClient(con);
1869 return;
1870 }
1871
1872 break;
1873 }
1874 else if ((filename = get_file(con, &filestats, buf,
1875 sizeof(buf))) == NULL)
1876 {
1877 if (!cupsdSendHeader(con, HTTP_NOT_FOUND, "text/html", CUPSD_AUTH_NONE))
1878 {
1879 cupsdCloseClient(con);
1880 return;
1881 }
1882
1883 cupsdLogRequest(con, HTTP_NOT_FOUND);
1884 }
1885 else if (!check_if_modified(con, &filestats))
1886 {
1887 if (!cupsdSendError(con, HTTP_NOT_MODIFIED, CUPSD_AUTH_NONE))
1888 {
1889 cupsdCloseClient(con);
1890 return;
1891 }
1892
1893 cupsdLogRequest(con, HTTP_NOT_MODIFIED);
1894 }
1895 else
1896 {
1897 /*
1898 * Serve a file...
1899 */
1900
1901 type = mimeFileType(MimeDatabase, filename, NULL, NULL);
1902 if (type == NULL)
1903 strcpy(line, "text/plain");
1904 else
1905 snprintf(line, sizeof(line), "%s/%s", type->super, type->type);
1906
1907 if (!cupsdSendHeader(con, HTTP_OK, line, CUPSD_AUTH_NONE))
1908 {
1909 cupsdCloseClient(con);
1910 return;
1911 }
1912
1913 if (httpPrintf(HTTP(con), "Last-Modified: %s\r\n",
1914 httpGetDateString(filestats.st_mtime)) < 0)
1915 {
1916 cupsdCloseClient(con);
1917 return;
1918 }
1919
1920 if (httpPrintf(HTTP(con), "Content-Length: %lu\r\n",
1921 (unsigned long)filestats.st_size) < 0)
1922 {
1923 cupsdCloseClient(con);
1924 return;
1925 }
1926
1927 cupsdLogRequest(con, HTTP_OK);
1928 }
1929
1930 if (httpPrintf(HTTP(con), "\r\n") < 0)
1931 {
1932 cupsdCloseClient(con);
1933 return;
1934 }
1935
1936 if (cupsdFlushHeader(con) < 0)
1937 {
1938 cupsdCloseClient(con);
1939 return;
1940 }
1941
1942 con->http.state = HTTP_WAITING;
1943 break;
1944
1945 default :
1946 break; /* Anti-compiler-warning-code */
1947 }
1948 }
1949 }
1950
1951 /*
1952 * Handle any incoming data...
1953 */
1954
1955 switch (con->http.state)
1956 {
1957 case HTTP_PUT_RECV :
1958 cupsdLogMessage(CUPSD_LOG_DEBUG2,
1959 "cupsdReadClient: %d con->data_encoding=HTTP_ENCODE_%s, "
1960 "con->data_remaining=" CUPS_LLFMT ", con->file=%d",
1961 con->http.fd,
1962 con->http.data_encoding == HTTP_ENCODE_CHUNKED ?
1963 "CHUNKED" : "LENGTH",
1964 CUPS_LLCAST con->http.data_remaining, con->file);
1965
1966 do
1967 {
1968 if ((bytes = httpRead2(HTTP(con), line, sizeof(line))) < 0)
1969 {
1970 cupsdCloseClient(con);
1971 return;
1972 }
1973 else if (bytes > 0)
1974 {
1975 con->bytes += bytes;
1976
1977 cupsdLogMessage(CUPSD_LOG_DEBUG2,
1978 "cupsdReadClient: %d writing %d bytes to %d",
1979 con->http.fd, bytes, con->file);
1980
1981 if (write(con->file, line, bytes) < bytes)
1982 {
1983 cupsdLogMessage(CUPSD_LOG_ERROR,
1984 "cupsdReadClient: Unable to write %d bytes to %s: %s",
1985 bytes, con->filename, strerror(errno));
1986
1987 cupsdLogMessage(CUPSD_LOG_DEBUG2,
1988 "cupsdReadClient: Closing data file %d...",
1989 con->file);
1990
1991 close(con->file);
1992 con->file = -1;
1993 unlink(con->filename);
1994 cupsdClearString(&con->filename);
1995
1996 if (!cupsdSendError(con, HTTP_REQUEST_TOO_LARGE, CUPSD_AUTH_NONE))
1997 {
1998 cupsdCloseClient(con);
1999 return;
2000 }
2001 }
2002 }
2003 }
2004 while (con->http.state == HTTP_PUT_RECV && data_ready(con));
2005
2006 if (con->http.state == HTTP_WAITING)
2007 {
2008 /*
2009 * End of file, see how big it is...
2010 */
2011
2012 fstat(con->file, &filestats);
2013
2014 cupsdLogMessage(CUPSD_LOG_DEBUG2,
2015 "cupsdReadClient: %d Closing data file %d, size="
2016 CUPS_LLFMT ".",
2017 con->http.fd, con->file,
2018 CUPS_LLCAST filestats.st_size);
2019
2020 close(con->file);
2021 con->file = -1;
2022
2023 if (filestats.st_size > MaxRequestSize &&
2024 MaxRequestSize > 0)
2025 {
2026 /*
2027 * Request is too big; remove it and send an error...
2028 */
2029
2030 cupsdLogMessage(CUPSD_LOG_DEBUG2,
2031 "cupsdReadClient: %d Removing temp file %s",
2032 con->http.fd, con->filename);
2033 unlink(con->filename);
2034 cupsdClearString(&con->filename);
2035
2036 if (!cupsdSendError(con, HTTP_REQUEST_TOO_LARGE, CUPSD_AUTH_NONE))
2037 {
2038 cupsdCloseClient(con);
2039 return;
2040 }
2041 }
2042
2043 /*
2044 * Install the configuration file...
2045 */
2046
2047 status = install_conf_file(con);
2048
2049 /*
2050 * Return the status to the client...
2051 */
2052
2053 if (!cupsdSendError(con, status, CUPSD_AUTH_NONE))
2054 {
2055 cupsdCloseClient(con);
2056 return;
2057 }
2058 }
2059 break;
2060
2061 case HTTP_POST_RECV :
2062 cupsdLogMessage(CUPSD_LOG_DEBUG2,
2063 "cupsdReadClient: %d con->data_encoding=HTTP_ENCODE_"
2064 "%s, con->data_remaining=" CUPS_LLFMT ", con->file=%d",
2065 con->http.fd,
2066 con->http.data_encoding == HTTP_ENCODE_CHUNKED ?
2067 "CHUNKED" : "LENGTH",
2068 CUPS_LLCAST con->http.data_remaining, con->file);
2069
2070 do
2071 {
2072 if (con->request)
2073 {
2074 /*
2075 * Grab any request data from the connection...
2076 */
2077
2078 if ((ipp_state = ippRead(&(con->http), con->request)) == IPP_ERROR)
2079 {
2080 cupsdLogMessage(CUPSD_LOG_ERROR,
2081 "cupsdReadClient: %d IPP Read Error!",
2082 con->http.fd);
2083
2084 cupsdSendError(con, HTTP_BAD_REQUEST, CUPSD_AUTH_NONE);
2085 cupsdCloseClient(con);
2086 return;
2087 }
2088 else if (ipp_state != IPP_DATA)
2089 {
2090 if (con->http.state == HTTP_POST_SEND)
2091 {
2092 cupsdSendError(con, HTTP_BAD_REQUEST, CUPSD_AUTH_NONE);
2093 cupsdCloseClient(con);
2094 return;
2095 }
2096
2097 break;
2098 }
2099 else
2100 con->bytes += ippLength(con->request);
2101 }
2102
2103 if (con->file < 0 && con->http.state != HTTP_POST_SEND)
2104 {
2105 /*
2106 * Create a file as needed for the request data...
2107 */
2108
2109 cupsdSetStringf(&con->filename, "%s/%08x", RequestRoot, request_id ++);
2110 con->file = open(con->filename, O_WRONLY | O_CREAT | O_TRUNC, 0640);
2111
2112 if (con->file < 0)
2113 {
2114 cupsdLogMessage(CUPSD_LOG_ERROR,
2115 "Unable to create request file %s: %s",
2116 con->filename, strerror(errno));
2117
2118 if (!cupsdSendError(con, HTTP_REQUEST_TOO_LARGE, CUPSD_AUTH_NONE))
2119 {
2120 cupsdCloseClient(con);
2121 return;
2122 }
2123 }
2124
2125 cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdReadClient: %d REQUEST %s=%d", con->http.fd,
2126 con->filename, con->file);
2127
2128 fchmod(con->file, 0640);
2129 fchown(con->file, RunUser, Group);
2130 fcntl(con->file, F_SETFD, fcntl(con->file, F_GETFD) | FD_CLOEXEC);
2131 }
2132
2133 if (con->http.state != HTTP_POST_SEND)
2134 {
2135 if ((bytes = httpRead2(HTTP(con), line, sizeof(line))) < 0)
2136 {
2137 cupsdCloseClient(con);
2138 return;
2139 }
2140 else if (bytes > 0)
2141 {
2142 con->bytes += bytes;
2143
2144 cupsdLogMessage(CUPSD_LOG_DEBUG2,
2145 "cupsdReadClient: %d writing %d bytes to %d",
2146 con->http.fd, bytes, con->file);
2147
2148 if (write(con->file, line, bytes) < bytes)
2149 {
2150 cupsdLogMessage(CUPSD_LOG_ERROR,
2151 "cupsdReadClient: Unable to write %d bytes to %s: %s",
2152 bytes, con->filename, strerror(errno));
2153
2154 cupsdLogMessage(CUPSD_LOG_DEBUG2,
2155 "cupsdReadClient: Closing file %d...",
2156 con->file);
2157
2158 close(con->file);
2159 con->file = -1;
2160 unlink(con->filename);
2161 cupsdClearString(&con->filename);
2162
2163 if (!cupsdSendError(con, HTTP_REQUEST_TOO_LARGE, CUPSD_AUTH_NONE))
2164 {
2165 cupsdCloseClient(con);
2166 return;
2167 }
2168 }
2169 }
2170 else if (con->http.state == HTTP_POST_RECV)
2171 return;
2172 else if (con->http.state != HTTP_POST_SEND)
2173 {
2174 cupsdCloseClient(con);
2175 return;
2176 }
2177 }
2178 }
2179 while (con->http.state == HTTP_POST_RECV && data_ready(con));
2180
2181 if (con->http.state == HTTP_POST_SEND)
2182 {
2183 if (con->file >= 0)
2184 {
2185 fstat(con->file, &filestats);
2186
2187 cupsdLogMessage(CUPSD_LOG_DEBUG2,
2188 "cupsdReadClient: %d Closing data file %d, "
2189 "size=" CUPS_LLFMT ".",
2190 con->http.fd, con->file,
2191 CUPS_LLCAST filestats.st_size);
2192
2193 close(con->file);
2194 con->file = -1;
2195
2196 if (filestats.st_size > MaxRequestSize &&
2197 MaxRequestSize > 0)
2198 {
2199 /*
2200 * Request is too big; remove it and send an error...
2201 */
2202
2203 cupsdLogMessage(CUPSD_LOG_DEBUG2,
2204 "cupsdReadClient: %d Removing temp file %s",
2205 con->http.fd, con->filename);
2206 unlink(con->filename);
2207 cupsdClearString(&con->filename);
2208
2209 if (con->request)
2210 {
2211 /*
2212 * Delete any IPP request data...
2213 */
2214
2215 ippDelete(con->request);
2216 con->request = NULL;
2217 }
2218
2219 if (!cupsdSendError(con, HTTP_REQUEST_TOO_LARGE, CUPSD_AUTH_NONE))
2220 {
2221 cupsdCloseClient(con);
2222 return;
2223 }
2224 }
2225
2226 if (con->command)
2227 {
2228 if (!cupsdSendCommand(con, con->command, con->options, 0))
2229 {
2230 if (!cupsdSendError(con, HTTP_NOT_FOUND, CUPSD_AUTH_NONE))
2231 {
2232 cupsdCloseClient(con);
2233 return;
2234 }
2235 }
2236 else
2237 cupsdLogRequest(con, HTTP_OK);
2238 }
2239 }
2240
2241 if (con->request)
2242 {
2243 cupsdProcessIPPRequest(con);
2244
2245 if (con->filename)
2246 {
2247 cupsdLogMessage(CUPSD_LOG_DEBUG2,
2248 "cupsdReadClient: %d Removing temp file %s",
2249 con->http.fd, con->filename);
2250 unlink(con->filename);
2251 cupsdClearString(&con->filename);
2252 }
2253
2254 return;
2255 }
2256 }
2257 break;
2258
2259 default :
2260 break; /* Anti-compiler-warning-code */
2261 }
2262
2263 if (con->http.state == HTTP_WAITING)
2264 {
2265 if (!con->http.keep_alive)
2266 cupsdCloseClient(con);
2267 else
2268 {
2269 cupsArrayRemove(ActiveClients, con);
2270 cupsdSetBusyState();
2271 }
2272 }
2273 }
2274
2275
2276 /*
2277 * 'cupsdSendCommand()' - Send output from a command via HTTP.
2278 */
2279
2280 int /* O - 1 on success, 0 on failure */
2281 cupsdSendCommand(
2282 cupsd_client_t *con, /* I - Client connection */
2283 char *command, /* I - Command to run */
2284 char *options, /* I - Command-line options */
2285 int root) /* I - Run as root? */
2286 {
2287 int fd; /* Standard input file descriptor */
2288
2289
2290 if (con->filename)
2291 {
2292 fd = open(con->filename, O_RDONLY);
2293
2294 if (fd < 0)
2295 {
2296 cupsdLogMessage(CUPSD_LOG_ERROR,
2297 "cupsdSendCommand: %d Unable to open \"%s\" for reading: %s",
2298 con->http.fd, con->filename ? con->filename : "/dev/null",
2299 strerror(errno));
2300 return (0);
2301 }
2302
2303 fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC);
2304 }
2305 else
2306 fd = -1;
2307
2308 con->pipe_pid = pipe_command(con, fd, &(con->file), command, options, root);
2309
2310 if (fd >= 0)
2311 close(fd);
2312
2313 cupsdLogMessage(CUPSD_LOG_INFO, "Started \"%s\" (pid=%d)", command,
2314 con->pipe_pid);
2315
2316 cupsdLogMessage(CUPSD_LOG_DEBUG, "cupsdSendCommand: %d file=%d",
2317 con->http.fd, con->file);
2318
2319 if (con->pipe_pid == 0)
2320 return (0);
2321
2322 fcntl(con->file, F_SETFD, fcntl(con->file, F_GETFD) | FD_CLOEXEC);
2323
2324 cupsdAddSelect(con->file, (cupsd_selfunc_t)write_pipe, NULL, con);
2325
2326 con->sent_header = 0;
2327 con->file_ready = 0;
2328 con->got_fields = 0;
2329 con->field_col = 0;
2330
2331 return (1);
2332 }
2333
2334
2335 /*
2336 * 'cupsdSendError()' - Send an error message via HTTP.
2337 */
2338
2339 int /* O - 1 if successful, 0 otherwise */
2340 cupsdSendError(cupsd_client_t *con, /* I - Connection */
2341 http_status_t code, /* I - Error code */
2342 int auth_type)/* I - Authentication type */
2343 {
2344 #ifdef HAVE_SSL
2345 /*
2346 * Force client to upgrade for authentication if that is how the
2347 * server is configured...
2348 */
2349
2350 if (code == HTTP_UNAUTHORIZED &&
2351 DefaultEncryption == HTTP_ENCRYPT_REQUIRED &&
2352 strcasecmp(con->http.hostname, "localhost") &&
2353 !con->http.tls)
2354 {
2355 cupsdLogMessage(CUPSD_LOG_DEBUG2,
2356 "cupsdSendError: Encryption before authentication!");
2357 code = HTTP_UPGRADE_REQUIRED;
2358 }
2359 #endif /* HAVE_SSL */
2360
2361 /*
2362 * Put the request in the access_log file...
2363 */
2364
2365 cupsdLogRequest(con, code);
2366
2367 cupsdLogMessage(CUPSD_LOG_DEBUG, "cupsdSendError: %d code=%d (%s)",
2368 con->http.fd, code, httpStatus(code));
2369
2370 /*
2371 * To work around bugs in some proxies, don't use Keep-Alive for some
2372 * error messages...
2373 *
2374 * Kerberos authentication doesn't work without Keep-Alive, so
2375 * never disable it in that case.
2376 */
2377
2378 if (code >= HTTP_BAD_REQUEST && con->http.auth_type != CUPSD_AUTH_NEGOTIATE)
2379 con->http.keep_alive = HTTP_KEEPALIVE_OFF;
2380
2381 /*
2382 * Send an error message back to the client. If the error code is a
2383 * 400 or 500 series, make sure the message contains some text, too!
2384 */
2385
2386 if (!cupsdSendHeader(con, code, NULL, auth_type))
2387 return (0);
2388
2389 #ifdef HAVE_SSL
2390 if (code == HTTP_UPGRADE_REQUIRED)
2391 if (httpPrintf(HTTP(con), "Connection: Upgrade\r\n") < 0)
2392 return (0);
2393
2394 if (httpPrintf(HTTP(con), "Upgrade: TLS/1.0,HTTP/1.1\r\n") < 0)
2395 return (0);
2396 #endif /* HAVE_SSL */
2397
2398 if (con->http.version >= HTTP_1_1 &&
2399 con->http.keep_alive == HTTP_KEEPALIVE_OFF)
2400 {
2401 if (httpPrintf(HTTP(con), "Connection: close\r\n") < 0)
2402 return (0);
2403 }
2404
2405 if (code >= HTTP_BAD_REQUEST)
2406 {
2407 /*
2408 * Send a human-readable error message.
2409 */
2410
2411 char message[4096], /* Message for user */
2412 urltext[1024], /* URL redirection text */
2413 redirect[1024]; /* Redirection link */
2414 const char *text; /* Status-specific text */
2415
2416
2417 redirect[0] = '\0';
2418
2419 if (code == HTTP_UNAUTHORIZED)
2420 text = _cupsLangString(con->language,
2421 _("Enter your username and password or the "
2422 "root username and password to access this "
2423 "page. If you are using Kerberos authentication, "
2424 "make sure you have a valid Kerberos ticket."));
2425 else if (code == HTTP_UPGRADE_REQUIRED)
2426 {
2427 text = urltext;
2428
2429 snprintf(urltext, sizeof(urltext),
2430 _cupsLangString(con->language,
2431 _("You must access this page using the URL "
2432 "<A HREF=\"https://%s:%d%s\">"
2433 "https://%s:%d%s</A>.")),
2434 con->servername, con->serverport, con->uri,
2435 con->servername, con->serverport, con->uri);
2436
2437 snprintf(redirect, sizeof(redirect),
2438 "<META HTTP-EQUIV=\"Refresh\" "
2439 "CONTENT=\"3;URL=https://%s:%d%s\">\n",
2440 con->servername, con->serverport, con->uri);
2441 }
2442 else
2443 text = "";
2444
2445 snprintf(message, sizeof(message),
2446 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" "
2447 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
2448 "<HTML>\n"
2449 "<HEAD>\n"
2450 "\t<META HTTP-EQUIV=\"Content-Type\" "
2451 "CONTENT=\"text/html; charset=utf-8\">\n"
2452 "\t<TITLE>%d %s</TITLE>\n"
2453 "\t<LINK REL=\"STYLESHEET\" TYPE=\"text/css\" "
2454 "HREF=\"/cups.css\">\n"
2455 "%s"
2456 "</HEAD>\n"
2457 "<BODY>\n"
2458 "<H1>%d %s</H1>\n"
2459 "<P>%s</P>\n"
2460 "</BODY>\n"
2461 "</HTML>\n",
2462 code, httpStatus(code), redirect, code, httpStatus(code), text);
2463
2464 if (httpPrintf(HTTP(con), "Content-Type: text/html; charset=utf-8\r\n") < 0)
2465 return (0);
2466 if (httpPrintf(HTTP(con), "Content-Length: %d\r\n",
2467 (int)strlen(message)) < 0)
2468 return (0);
2469 if (httpPrintf(HTTP(con), "\r\n") < 0)
2470 return (0);
2471 if (httpPrintf(HTTP(con), "%s", message) < 0)
2472 return (0);
2473 }
2474 else if (httpPrintf(HTTP(con), "\r\n") < 0)
2475 return (0);
2476
2477 if (cupsdFlushHeader(con) < 0)
2478 return (0);
2479
2480 con->http.state = HTTP_WAITING;
2481
2482 return (1);
2483 }
2484
2485
2486 /*
2487 * 'cupsdSendHeader()' - Send an HTTP request.
2488 */
2489
2490 int /* O - 1 on success, 0 on failure */
2491 cupsdSendHeader(
2492 cupsd_client_t *con, /* I - Client to send to */
2493 http_status_t code, /* I - HTTP status code */
2494 char *type, /* I - MIME type of document */
2495 int auth_type) /* I - Type of authentication */
2496 {
2497 char auth_str[1024]; /* Authorization string */
2498 #ifdef HAVE_GSSAPI
2499 static char *gss_buf = NULL; /* Kerberos auth data buffer */
2500 static int gss_bufsize = 0; /* Size of Kerberos auth data buffer */
2501 #endif /* HAVE_GSSAPI */
2502
2503
2504 /*
2505 * Send the HTTP status header...
2506 */
2507
2508 if (code == HTTP_CONTINUE)
2509 {
2510 /*
2511 * 100-continue doesn't send any headers...
2512 */
2513
2514 return (httpPrintf(HTTP(con), "HTTP/%d.%d 100 Continue\r\n\r\n",
2515 con->http.version / 100, con->http.version % 100) > 0);
2516 }
2517
2518 httpFlushWrite(HTTP(con));
2519
2520 con->http.data_encoding = HTTP_ENCODE_FIELDS;
2521
2522 if (httpPrintf(HTTP(con), "HTTP/%d.%d %d %s\r\n", con->http.version / 100,
2523 con->http.version % 100, code, httpStatus(code)) < 0)
2524 return (0);
2525 if (httpPrintf(HTTP(con), "Date: %s\r\n", httpGetDateString(time(NULL))) < 0)
2526 return (0);
2527 if (ServerHeader)
2528 if (httpPrintf(HTTP(con), "Server: %s\r\n", ServerHeader) < 0)
2529 return (0);
2530 if (con->http.keep_alive && con->http.version >= HTTP_1_0)
2531 {
2532 if (httpPrintf(HTTP(con), "Connection: Keep-Alive\r\n") < 0)
2533 return (0);
2534 if (httpPrintf(HTTP(con), "Keep-Alive: timeout=%d\r\n",
2535 KeepAliveTimeout) < 0)
2536 return (0);
2537 }
2538 if (code == HTTP_METHOD_NOT_ALLOWED)
2539 if (httpPrintf(HTTP(con), "Allow: GET, HEAD, OPTIONS, POST, PUT\r\n") < 0)
2540 return (0);
2541
2542 if (code == HTTP_UNAUTHORIZED)
2543 {
2544 if (auth_type == CUPSD_AUTH_NONE)
2545 {
2546 if (!con->best || con->best->type <= CUPSD_AUTH_NONE)
2547 auth_type = DefaultAuthType;
2548 else
2549 auth_type = con->best->type;
2550 }
2551
2552 auth_str[0] = '\0';
2553
2554 if (auth_type == CUPSD_AUTH_BASIC || auth_type == CUPSD_AUTH_BASICDIGEST)
2555 strlcpy(auth_str, "Basic realm=\"CUPS\"", sizeof(auth_str));
2556 else if (auth_type == CUPSD_AUTH_DIGEST)
2557 snprintf(auth_str, sizeof(auth_str), "Digest realm=\"CUPS\", nonce=\"%s\"",
2558 con->http.hostname);
2559 #ifdef HAVE_GSSAPI
2560 else if (auth_type == CUPSD_AUTH_NEGOTIATE && con->gss_output_token.length == 0)
2561 strlcpy(auth_str, "Negotiate", sizeof(auth_str));
2562 #endif /* HAVE_GSSAPI */
2563
2564 #ifdef HAVE_AUTHORIZATION_H
2565 if (con->best && auth_type != CUPSD_AUTH_NEGOTIATE)
2566 {
2567 int i; /* Looping var */
2568 char *auth_key; /* Auth key buffer */
2569 size_t auth_size; /* Size of remaining buffer */
2570
2571
2572 auth_key = auth_str + strlen(auth_str);
2573 auth_size = sizeof(auth_str) - (auth_key - auth_str);
2574
2575 for (i = 0; i < con->best->num_names; i ++)
2576 {
2577 if (!strncasecmp(con->best->names[i], "@AUTHKEY(", 9))
2578 {
2579 snprintf(auth_key, auth_size, ", authkey=\"%s\"",
2580 con->best->names[i] + 9);
2581 /* end parenthesis is stripped in conf.c */
2582 break;
2583 }
2584 else if (!strcasecmp(con->best->names[i], "@SYSTEM") &&
2585 SystemGroupAuthKey)
2586 {
2587 snprintf(auth_key, auth_size, ", authkey=\"%s\"", SystemGroupAuthKey);
2588 break;
2589 }
2590 }
2591 }
2592 #endif /* HAVE_AUTHORIZATION_H */
2593
2594 if (auth_str[0])
2595 {
2596 cupsdLogMessage(CUPSD_LOG_DEBUG, "cupsdSendHeader: WWW-Authenticate: %s",
2597 auth_str);
2598
2599 if (httpPrintf(HTTP(con), "WWW-Authenticate: %s\r\n", auth_str) < 0)
2600 return (0);
2601 }
2602 }
2603
2604 #ifdef HAVE_GSSAPI
2605 /*
2606 * WWW-Authenticate: Negotiate can be included even for
2607 * non-401 replies...
2608 */
2609
2610 if (con->gss_output_token.length > 0 && con->gss_output_token.length <= 65536)
2611 {
2612 OM_uint32 minor_status; /* Minor status code */
2613 int bufsize; /* Size of output token buffer */
2614
2615
2616 bufsize = con->gss_output_token.length * 4 / 3 + 2;
2617
2618 if (bufsize > gss_bufsize)
2619 {
2620 char *buf; /* New buffer */
2621
2622
2623 bufsize = (bufsize + 1023) & 1023;/* Round up */
2624
2625 if (gss_buf)
2626 buf = realloc(gss_buf, bufsize);
2627 else
2628 buf = malloc(bufsize);
2629
2630 if (!buf)
2631 {
2632 cupsdLogMessage(CUPSD_LOG_ERROR,
2633 "Unable to allocate %d bytes for Kerberos credentials!",
2634 bufsize);
2635 return (0);
2636 }
2637
2638 gss_buf = buf;
2639 gss_bufsize = bufsize;
2640 }
2641
2642 httpEncode64_2(gss_buf, gss_bufsize,
2643 con->gss_output_token.value,
2644 con->gss_output_token.length);
2645 gss_release_buffer(&minor_status, &con->gss_output_token);
2646
2647 cupsdLogMessage(CUPSD_LOG_DEBUG,
2648 "cupsdSendHeader: WWW-Authenticate: Negotiate %s", gss_buf);
2649
2650 if (httpPrintf(HTTP(con), "WWW-Authenticate: Negotiate %s\r\n",
2651 gss_buf) < 0)
2652 return (0);
2653 }
2654 else if (con->gss_output_token.length > 65536)
2655 {
2656 cupsdLogMessage(CUPSD_LOG_ERROR,
2657 "Kerberos credentials larger than 64k (%d)!",
2658 (int)con->gss_output_token.length);
2659 return (0);
2660 }
2661 #endif /* HAVE_GSSAPI */
2662
2663 if (con->language && strcmp(con->language->language, "C"))
2664 {
2665 if (httpPrintf(HTTP(con), "Content-Language: %s\r\n",
2666 con->language->language) < 0)
2667 return (0);
2668 }
2669
2670 if (type)
2671 {
2672 if (!strcmp(type, "text/html"))
2673 {
2674 if (httpPrintf(HTTP(con),
2675 "Content-Type: text/html; charset=utf-8\r\n") < 0)
2676 return (0);
2677 }
2678 else if (httpPrintf(HTTP(con), "Content-Type: %s\r\n", type) < 0)
2679 return (0);
2680 }
2681
2682 return (1);
2683 }
2684
2685
2686 /*
2687 * 'cupsdUpdateCGI()' - Read status messages from CGI scripts and programs.
2688 */
2689
2690 void
2691 cupsdUpdateCGI(void)
2692 {
2693 char *ptr, /* Pointer to end of line in buffer */
2694 message[1024]; /* Pointer to message text */
2695 int loglevel; /* Log level for message */
2696
2697
2698 while ((ptr = cupsdStatBufUpdate(CGIStatusBuffer, &loglevel,
2699 message, sizeof(message))) != NULL)
2700 {
2701 if (loglevel == CUPSD_LOG_INFO)
2702 cupsdLogMessage(CUPSD_LOG_INFO, "%s", message);
2703
2704 if (!strchr(CGIStatusBuffer->buffer, '\n'))
2705 break;
2706 }
2707
2708 if (ptr == NULL && !CGIStatusBuffer->bufused)
2709 {
2710 /*
2711 * Fatal error on pipe - should never happen!
2712 */
2713
2714 cupsdLogMessage(CUPSD_LOG_CRIT,
2715 "cupsdUpdateCGI: error reading from CGI error pipe - %s",
2716 strerror(errno));
2717 }
2718 }
2719
2720
2721 /*
2722 * 'cupsdWriteClient()' - Write data to a client as needed.
2723 */
2724
2725 void
2726 cupsdWriteClient(cupsd_client_t *con) /* I - Client connection */
2727 {
2728 int bytes; /* Number of bytes written */
2729 char buf[16385]; /* Data buffer */
2730 char *bufptr; /* Pointer into buffer */
2731 ipp_state_t ipp_state; /* IPP state value */
2732
2733
2734 #ifdef DEBUG
2735 cupsdLogMessage(CUPSD_LOG_DEBUG2,
2736 "cupsdWriteClient(con=%p) %d response=%p(%d), file=%d "
2737 "pipe_pid=%d state=%d",
2738 con, con->http.fd, con->response,
2739 con->response ? con->response->state : -1,
2740 con->file, con->pipe_pid, con->http.state);
2741 #endif /* DEBUG */
2742
2743 if (con->http.state != HTTP_GET_SEND &&
2744 con->http.state != HTTP_POST_SEND)
2745 return;
2746
2747 if (con->pipe_pid)
2748 {
2749 /*
2750 * Make sure we select on the CGI output...
2751 */
2752
2753 cupsdAddSelect(con->file, (cupsd_selfunc_t)write_pipe, NULL, con);
2754
2755 if (!con->file_ready)
2756 {
2757 /*
2758 * Try again later when there is CGI output available...
2759 */
2760
2761 cupsdRemoveSelect(con->http.fd);
2762 return;
2763 }
2764
2765 con->file_ready = 0;
2766 }
2767
2768 if (con->response && con->response->state != IPP_DATA)
2769 {
2770 ipp_state = ippWrite(HTTP(con), con->response);
2771 bytes = ipp_state != IPP_ERROR &&
2772 (con->file >= 0 || ipp_state != IPP_DATA);
2773 }
2774 else if ((bytes = read(con->file, buf, sizeof(buf) - 1)) > 0)
2775 {
2776 cupsdLogMessage(CUPSD_LOG_DEBUG2,
2777 "cupsdWriteClient: Read %d bytes from file %d...",
2778 bytes, con->file);
2779
2780 if (con->pipe_pid && !con->got_fields)
2781 {
2782 /*
2783 * Inspect the data for Content-Type and other fields.
2784 */
2785
2786 buf[bytes] = '\0';
2787
2788 for (bufptr = buf; !con->got_fields && *bufptr; bufptr ++)
2789 {
2790 if (*bufptr == '\n')
2791 {
2792 /*
2793 * Send line to client...
2794 */
2795
2796 if (bufptr > buf && bufptr[-1] == '\r')
2797 bufptr[-1] = '\0';
2798 *bufptr++ = '\0';
2799
2800 cupsdLogMessage(CUPSD_LOG_DEBUG, "Script header: %s", buf);
2801
2802 if (!con->sent_header)
2803 {
2804 /*
2805 * Handle redirection and CGI status codes...
2806 */
2807
2808 if (!strncasecmp(buf, "Location:", 9))
2809 {
2810 cupsdSendHeader(con, HTTP_SEE_OTHER, NULL, CUPSD_AUTH_NONE);
2811 con->sent_header = 2;
2812
2813 if (httpPrintf(HTTP(con), "Content-Length: 0\r\n") < 0)
2814 return;
2815 }
2816 else if (!strncasecmp(buf, "Status:", 7))
2817 {
2818 cupsdSendError(con, (http_status_t)atoi(buf + 7), CUPSD_AUTH_NONE);
2819 con->sent_header = 2;
2820 }
2821 else
2822 {
2823 cupsdSendHeader(con, HTTP_OK, NULL, CUPSD_AUTH_NONE);
2824 con->sent_header = 1;
2825
2826 if (con->http.version == HTTP_1_1)
2827 {
2828 if (httpPrintf(HTTP(con), "Transfer-Encoding: chunked\r\n") < 0)
2829 return;
2830 }
2831 }
2832 }
2833
2834 if (strncasecmp(buf, "Status:", 7))
2835 httpPrintf(HTTP(con), "%s\r\n", buf);
2836
2837 cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdWriteClient: %d %s",
2838 con->http.fd, buf);
2839
2840 /*
2841 * Update buffer...
2842 */
2843
2844 bytes -= (bufptr - buf);
2845
2846 if (bytes > 0)
2847 memmove(buf, bufptr, bytes + 1);
2848 else
2849 buf[0] = '\0';
2850
2851 bufptr = buf - 1;
2852
2853 /*
2854 * See if the line was empty...
2855 */
2856
2857 if (con->field_col == 0)
2858 {
2859 con->got_fields = 1;
2860
2861 if (cupsdFlushHeader(con) < 0)
2862 {
2863 cupsdCloseClient(con);
2864 return;
2865 }
2866
2867 if (con->http.version == HTTP_1_1)
2868 con->http.data_encoding = HTTP_ENCODE_CHUNKED;
2869 }
2870 else
2871 con->field_col = 0;
2872 }
2873 else if (*bufptr != '\r')
2874 con->field_col ++;
2875 }
2876
2877 cupsdLogMessage(CUPSD_LOG_DEBUG2,
2878 "cupsdWriteClient: %d bytes=%d, got_fields=%d",
2879 con->http.fd, bytes, con->got_fields);
2880
2881 if (bytes > 0 && !con->got_fields)
2882 {
2883 /*
2884 * Remaining text needs to go out...
2885 */
2886
2887 httpPrintf(HTTP(con), "%s", buf);
2888
2889 con->http.activity = time(NULL);
2890 return;
2891 }
2892 else if (bytes == 0)
2893 {
2894 con->http.activity = time(NULL);
2895 return;
2896 }
2897 }
2898
2899 if (bytes > 0)
2900 {
2901 if (httpWrite2(HTTP(con), buf, bytes) < 0)
2902 {
2903 cupsdLogMessage(CUPSD_LOG_DEBUG2,
2904 "cupsdWriteClient: %d Write of %d bytes failed!",
2905 con->http.fd, bytes);
2906
2907 cupsdCloseClient(con);
2908 return;
2909 }
2910
2911 if (con->http.data_encoding == HTTP_ENCODE_CHUNKED)
2912 httpFlushWrite(HTTP(con));
2913
2914 con->bytes += bytes;
2915
2916 if (con->http.state == HTTP_WAITING)
2917 bytes = 0;
2918 }
2919 }
2920
2921 if (bytes <= 0)
2922 {
2923 cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdWriteClient: %d bytes < 0",
2924 con->http.fd);
2925
2926 cupsdLogRequest(con, HTTP_OK);
2927
2928 httpFlushWrite(HTTP(con));
2929
2930 if (con->http.data_encoding == HTTP_ENCODE_CHUNKED && con->sent_header == 1)
2931 {
2932 if (httpWrite2(HTTP(con), "", 0) < 0)
2933 {
2934 cupsdCloseClient(con);
2935 return;
2936 }
2937 }
2938
2939 con->http.state = HTTP_WAITING;
2940
2941 cupsdAddSelect(con->http.fd, (cupsd_selfunc_t)cupsdReadClient, NULL, con);
2942
2943 if (con->file >= 0)
2944 {
2945 cupsdRemoveSelect(con->file);
2946
2947 if (con->pipe_pid)
2948 cupsdEndProcess(con->pipe_pid, 0);
2949
2950 cupsdLogMessage(CUPSD_LOG_DEBUG2,
2951 "cupsdWriteClient: %d Closing data file %d.",
2952 con->http.fd, con->file);
2953
2954 close(con->file);
2955 con->file = -1;
2956 con->pipe_pid = 0;
2957 }
2958
2959 if (con->filename)
2960 {
2961 cupsdLogMessage(CUPSD_LOG_DEBUG2,
2962 "cupsdWriteClient: %d Removing temp file %s",
2963 con->http.fd, con->filename);
2964 unlink(con->filename);
2965 cupsdClearString(&con->filename);
2966 }
2967
2968 if (con->request)
2969 {
2970 ippDelete(con->request);
2971 con->request = NULL;
2972 }
2973
2974 if (con->response)
2975 {
2976 ippDelete(con->response);
2977 con->response = NULL;
2978 }
2979
2980 cupsdClearString(&con->command);
2981 cupsdClearString(&con->options);
2982 cupsdClearString(&con->query_string);
2983
2984 if (!con->http.keep_alive)
2985 {
2986 cupsdCloseClient(con);
2987 return;
2988 }
2989 }
2990
2991 con->http.activity = time(NULL);
2992 }
2993
2994
2995 /*
2996 * 'check_if_modified()' - Decode an "If-Modified-Since" line.
2997 */
2998
2999 static int /* O - 1 if modified since */
3000 check_if_modified(
3001 cupsd_client_t *con, /* I - Client connection */
3002 struct stat *filestats) /* I - File information */
3003 {
3004 char *ptr; /* Pointer into field */
3005 time_t date; /* Time/date value */
3006 off_t size; /* Size/length value */
3007
3008
3009 size = 0;
3010 date = 0;
3011 ptr = con->http.fields[HTTP_FIELD_IF_MODIFIED_SINCE];
3012
3013 if (*ptr == '\0')
3014 return (1);
3015
3016 cupsdLogMessage(CUPSD_LOG_DEBUG2,
3017 "check_if_modified: %d If-Modified-Since=\"%s\"",
3018 con->http.fd, ptr);
3019
3020 while (*ptr != '\0')
3021 {
3022 while (isspace(*ptr) || *ptr == ';')
3023 ptr ++;
3024
3025 if (strncasecmp(ptr, "length=", 7) == 0)
3026 {
3027 ptr += 7;
3028 size = strtoll(ptr, NULL, 10);
3029
3030 while (isdigit(*ptr))
3031 ptr ++;
3032 }
3033 else if (isalpha(*ptr))
3034 {
3035 date = httpGetDateTime(ptr);
3036 while (*ptr != '\0' && *ptr != ';')
3037 ptr ++;
3038 }
3039 else
3040 ptr ++;
3041 }
3042
3043 cupsdLogMessage(CUPSD_LOG_DEBUG2,
3044 "check_if_modified: %d sizes=" CUPS_LLFMT ","
3045 CUPS_LLFMT " dates=%d,%d",
3046 con->http.fd, CUPS_LLCAST size,
3047 CUPS_LLCAST filestats->st_size, (int)date,
3048 (int)filestats->st_mtime);
3049
3050 return ((size != filestats->st_size && size != 0) ||
3051 (date < filestats->st_mtime && date != 0) ||
3052 (size == 0 && date == 0));
3053 }
3054
3055
3056 /*
3057 * 'compare_clients()' - Compare two client connections.
3058 */
3059
3060 static int /* O - Result of comparison */
3061 compare_clients(cupsd_client_t *a, /* I - First client */
3062 cupsd_client_t *b, /* I - Second client */
3063 void *data) /* I - User data (not used) */
3064 {
3065 (void)data;
3066
3067 if (a == b)
3068 return (0);
3069 else if (a < b)
3070 return (-1);
3071 else
3072 return (1);
3073 }
3074
3075
3076 /*
3077 * 'data_ready()' - Check whether data is available from a client.
3078 */
3079
3080 static int /* O - 1 if data is ready, 0 otherwise */
3081 data_ready(cupsd_client_t *con) /* I - Client */
3082 {
3083 if (con->http.used > 0)
3084 return (1);
3085 #ifdef HAVE_SSL
3086 else if (con->http.tls)
3087 {
3088 # ifdef HAVE_LIBSSL
3089 if (SSL_pending((SSL *)(con->http.tls)))
3090 return (1);
3091 # elif defined(HAVE_GNUTLS)
3092 if (gnutls_record_check_pending(((http_tls_t *)(con->http.tls))->session))
3093 return (1);
3094 # elif defined(HAVE_CDSASSL)
3095 size_t bytes; /* Bytes that are available */
3096
3097 if (!SSLGetBufferedReadSize(((http_tls_t *)(con->http.tls))->session,
3098 &bytes) && bytes > 0)
3099 return (1);
3100 # endif /* HAVE_LIBSSL */
3101 }
3102 #endif /* HAVE_SSL */
3103
3104 return (0);
3105 }
3106
3107
3108 #ifdef HAVE_SSL
3109 /*
3110 * 'encrypt_client()' - Enable encryption for the client...
3111 */
3112
3113 static int /* O - 1 on success, 0 on error */
3114 encrypt_client(cupsd_client_t *con) /* I - Client to encrypt */
3115 {
3116 # ifdef HAVE_LIBSSL
3117 SSL_CTX *context; /* Context for encryption */
3118 SSL *conn; /* Connection for encryption */
3119 BIO *bio; /* BIO data */
3120 unsigned long error; /* Error code */
3121
3122
3123 /*
3124 * Verify that we have a certificate...
3125 */
3126
3127 if (access(ServerKey, 0) || access(ServerCertificate, 0))
3128 {
3129 /*
3130 * Nope, make a self-signed certificate...
3131 */
3132
3133 if (!make_certificate(con))
3134 return (0);
3135 }
3136
3137 /*
3138 * Create the SSL context and accept the connection...
3139 */
3140
3141 context = SSL_CTX_new(SSLv23_server_method());
3142
3143 SSL_CTX_set_options(context, SSL_OP_NO_SSLv2); /* Only use SSLv3 or TLS */
3144 if (SSLOptions & CUPSD_SSL_NOEMPTY)
3145 SSL_CTX_set_options(context, SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS);
3146 SSL_CTX_use_PrivateKey_file(context, ServerKey, SSL_FILETYPE_PEM);
3147 SSL_CTX_use_certificate_chain_file(context, ServerCertificate);
3148
3149 bio = BIO_new(_httpBIOMethods());
3150 BIO_ctrl(bio, BIO_C_SET_FILE_PTR, 0, (char *)HTTP(con));
3151
3152 conn = SSL_new(context);
3153 SSL_set_bio(conn, bio, bio);
3154
3155 if (SSL_accept(conn) != 1)
3156 {
3157 cupsdLogMessage(CUPSD_LOG_ERROR,
3158 "encrypt_client: Unable to encrypt connection from %s!",
3159 con->http.hostname);
3160
3161 while ((error = ERR_get_error()) != 0)
3162 cupsdLogMessage(CUPSD_LOG_ERROR, "encrypt_client: %s",
3163 ERR_error_string(error, NULL));
3164
3165 SSL_CTX_free(context);
3166 SSL_free(conn);
3167 return (0);
3168 }
3169
3170 cupsdLogMessage(CUPSD_LOG_DEBUG,
3171 "encrypt_client: %d Connection from %s now encrypted.",
3172 con->http.fd, con->http.hostname);
3173
3174 con->http.tls = conn;
3175 return (1);
3176
3177 # elif defined(HAVE_GNUTLS)
3178 http_tls_t *conn; /* TLS session object */
3179 int error; /* Error code */
3180 gnutls_certificate_server_credentials *credentials;
3181 /* TLS credentials */
3182
3183
3184 /*
3185 * Verify that we have a certificate...
3186 */
3187
3188 if (access(ServerKey, 0) || access(ServerCertificate, 0))
3189 {
3190 /*
3191 * Nope, make a self-signed certificate...
3192 */
3193
3194 if (!make_certificate(con))
3195 return (0);
3196 }
3197
3198 /*
3199 * Create the SSL object and perform the SSL handshake...
3200 */
3201
3202 conn = (http_tls_t *)malloc(sizeof(http_tls_t));
3203
3204 if (conn == NULL)
3205 return (0);
3206
3207 credentials = (gnutls_certificate_server_credentials *)
3208 malloc(sizeof(gnutls_certificate_server_credentials));
3209 if (credentials == NULL)
3210 {
3211 cupsdLogMessage(CUPSD_LOG_ERROR,
3212 "encrypt_client: Unable to encrypt connection from %s!",
3213 con->http.hostname);
3214 cupsdLogMessage(CUPSD_LOG_ERROR, "encrypt_client: %s", strerror(errno));
3215
3216 free(conn);
3217 return (0);
3218 }
3219
3220 gnutls_certificate_allocate_credentials(credentials);
3221 gnutls_certificate_set_x509_key_file(*credentials, ServerCertificate,
3222 ServerKey, GNUTLS_X509_FMT_PEM);
3223
3224 gnutls_init(&(conn->session), GNUTLS_SERVER);
3225 gnutls_set_default_priority(conn->session);
3226 gnutls_credentials_set(conn->session, GNUTLS_CRD_CERTIFICATE, *credentials);
3227 gnutls_transport_set_ptr(conn->session, (gnutls_transport_ptr)HTTP(con));
3228 gnutls_transport_set_pull_function(conn->session, _httpReadGNUTLS);
3229 gnutls_transport_set_push_function(conn->session, _httpWriteGNUTLS);
3230
3231 error = gnutls_handshake(conn->session);
3232
3233 if (error != GNUTLS_E_SUCCESS)
3234 {
3235 cupsdLogMessage(CUPSD_LOG_ERROR,
3236 "encrypt_client: Unable to encrypt connection from %s!",
3237 con->http.hostname);
3238 cupsdLogMessage(CUPSD_LOG_ERROR, "encrypt_client: %s",
3239 gnutls_strerror(error));
3240
3241 gnutls_deinit(conn->session);
3242 gnutls_certificate_free_credentials(*credentials);
3243 free(conn);
3244 free(credentials);
3245 return (0);
3246 }
3247
3248 cupsdLogMessage(CUPSD_LOG_DEBUG,
3249 "encrypt_client: %d Connection from %s now encrypted.",
3250 con->http.fd, con->http.hostname);
3251
3252 conn->credentials = credentials;
3253 con->http.tls = conn;
3254 return (1);
3255
3256 # elif defined(HAVE_CDSASSL)
3257 OSStatus error; /* Error code */
3258 http_tls_t *conn; /* CDSA connection information */
3259
3260
3261 if ((conn = (http_tls_t *)malloc(sizeof(http_tls_t))) == NULL)
3262 return (0);
3263
3264 error = 0;
3265 conn->session = NULL;
3266 conn->certsArray = get_cdsa_certificate(con);
3267
3268 if (!conn->certsArray)
3269 {
3270 /*
3271 * No keychain (yet), make a self-signed certificate...
3272 */
3273
3274 if (make_certificate(con))
3275 conn->certsArray = get_cdsa_certificate(con);
3276 }
3277
3278 if (!conn->certsArray)
3279 {
3280 cupsdLogMessage(CUPSD_LOG_ERROR,
3281 "encrypt_client: Could not find signing key in keychain "
3282 "\"%s\"", ServerCertificate);
3283 error = errSSLBadCert; /* errSSLBadConfiguration is a better choice, but not available on 10.2.x */
3284 }
3285
3286 if (!error)
3287 error = SSLNewContext(true, &conn->session);
3288
3289 if (!error)
3290 error = SSLSetIOFuncs(conn->session, _httpReadCDSA, _httpWriteCDSA);
3291
3292 if (!error)
3293 error = SSLSetProtocolVersionEnabled(conn->session, kSSLProtocol2, false);
3294
3295 if (!error)
3296 error = SSLSetConnection(conn->session, HTTP(con));
3297
3298 if (!error)
3299 error = SSLSetAllowsExpiredCerts(conn->session, true);
3300
3301 if (!error)
3302 error = SSLSetAllowsAnyRoot(conn->session, true);
3303
3304 if (!error)
3305 error = SSLSetCertificate(conn->session, conn->certsArray);
3306
3307 if (!error)
3308 {
3309 /*
3310 * Perform SSL/TLS handshake
3311 */
3312
3313 while ((error = SSLHandshake(conn->session)) == errSSLWouldBlock)
3314 usleep(1000);
3315 }
3316
3317 if (error)
3318 {
3319 cupsdLogMessage(CUPSD_LOG_ERROR,
3320 "encrypt_client: Unable to encrypt connection from %s!",
3321 con->http.hostname);
3322
3323 cupsdLogMessage(CUPSD_LOG_ERROR, "encrypt_client: %s (%d)",
3324 cssmErrorString(error), (int)error);
3325
3326 con->http.error = error;
3327 con->http.status = HTTP_ERROR;
3328
3329 if (conn->session)
3330 SSLDisposeContext(conn->session);
3331
3332 if (conn->certsArray)
3333 CFRelease(conn->certsArray);
3334
3335 free(conn);
3336
3337 return (0);
3338 }
3339
3340 cupsdLogMessage(CUPSD_LOG_DEBUG,
3341 "encrypt_client: %d Connection from %s now encrypted.",
3342 con->http.fd, con->http.hostname);
3343
3344 con->http.tls = conn;
3345 return (1);
3346
3347 # endif /* HAVE_LIBSSL */
3348 }
3349 #endif /* HAVE_SSL */
3350
3351
3352 #ifdef HAVE_CDSASSL
3353 /*
3354 * 'get_cdsa_certificate()' - Get a SSL/TLS certificate from the System keychain.
3355 */
3356
3357 static CFArrayRef /* O - Array of certificates */
3358 get_cdsa_certificate(cupsd_client_t *con) /* I - Client connection */
3359 {
3360 OSStatus err; /* Error info */
3361 SecKeychainRef keychain; /* Keychain reference */
3362 SecIdentitySearchRef search; /* Search reference */
3363 SecIdentityRef identity; /* Identity */
3364 CFArrayRef certificates = NULL;
3365 /* Certificate array */
3366
3367
3368 if ((err = SecKeychainOpen(ServerCertificate, &keychain)))
3369 {
3370 cupsdLogMessage(CUPSD_LOG_ERROR, "Cannot open keychain \"%s\", %s",
3371 ServerCertificate, cssmErrorString(err));
3372 return (NULL);
3373 }
3374
3375 # if HAVE_SECIDENTITYSEARCHCREATEWITHPOLICY
3376 /*
3377 * Use a policy to search for valid certificates who's common name matches the
3378 * servername...
3379 */
3380
3381 SecPolicySearchRef policy_search; /* Policy search ref */
3382 SecPolicyRef policy; /* Policy ref */
3383 CSSM_DATA options; /* Policy options */
3384 CSSM_APPLE_TP_SSL_OPTIONS
3385 ssl_options; /* SSL Option for hostname */
3386
3387
3388 if (SecPolicySearchCreate(CSSM_CERT_X_509v3, &CSSMOID_APPLE_TP_SSL,
3389 NULL, &policy_search))
3390 {
3391 cupsdLogMessage(CUPSD_LOG_ERROR, "Cannot create a policy search reference");
3392 CFRelease(keychain);
3393 return (NULL);
3394 }
3395
3396 if (SecPolicySearchCopyNext(policy_search, &policy))
3397 {
3398 cupsdLogMessage(CUPSD_LOG_ERROR,
3399 "Cannot find a policy to use for searching");
3400 CFRelease(keychain);
3401 CFRelease(policy_search);
3402 return (NULL);
3403 }
3404
3405 memset(&ssl_options, 0, sizeof(ssl_options));
3406 ssl_options.Version = CSSM_APPLE_TP_SSL_OPTS_VERSION;
3407 ssl_options.ServerName = con->servername;
3408 ssl_options.ServerNameLen = strlen(con->servername);
3409
3410 options.Data = (uint8 *)&ssl_options;
3411 options.Length = sizeof(ssl_options);
3412
3413 if (SecPolicySetValue(policy, &options))
3414 {
3415 cupsdLogMessage(CUPSD_LOG_ERROR,
3416 "Cannot set policy value to use for searching");
3417 CFRelease(keychain);
3418 CFRelease(policy_search);
3419 return (NULL);
3420 }
3421
3422 err = SecIdentitySearchCreateWithPolicy(policy, NULL, CSSM_KEYUSE_SIGN,
3423 keychain, FALSE, &search);
3424 # else
3425 /*
3426 * Assume there is exactly one SecIdentity in the keychain...
3427 */
3428
3429 err = SecIdentitySearchCreate(keychain, CSSM_KEYUSE_SIGN, &search);
3430 # endif /* HAVE_SECIDENTITYSEARCHCREATEWITHPOLICY */
3431
3432 if (err)
3433 cupsdLogMessage(CUPSD_LOG_DEBUG,
3434 "Cannot create keychain search reference: %s",
3435 cssmErrorString(err));
3436 else
3437 {
3438 if ((err = SecIdentitySearchCopyNext(search, &identity)))
3439 {
3440 cupsdLogMessage(CUPSD_LOG_DEBUG,
3441 "Cannot find signing key in keychain \"%s\", error %d",
3442 ServerCertificate, (int)err);
3443 }
3444 else
3445 {
3446 if (CFGetTypeID(identity) != SecIdentityGetTypeID())
3447 cupsdLogMessage(CUPSD_LOG_ERROR,
3448 "SecIdentitySearchCopyNext CFTypeID failure!");
3449 else
3450 {
3451 if ((certificates = CFArrayCreate(NULL, (const void **)&identity,
3452 1, &kCFTypeArrayCallBacks)) == NULL)
3453 cupsdLogMessage(CUPSD_LOG_ERROR, "Cannot create certificate array");
3454 }
3455
3456 CFRelease(identity);
3457 }
3458
3459 CFRelease(search);
3460 }
3461
3462 # if HAVE_SECIDENTITYSEARCHCREATEWITHPOLICY
3463 CFRelease(policy);
3464 CFRelease(policy_search);
3465 # endif /* HAVE_SECIDENTITYSEARCHCREATEWITHPOLICY */
3466
3467 return (certificates);
3468 }
3469 #endif /* HAVE_CDSASSL */
3470
3471
3472 /*
3473 * 'get_file()' - Get a filename and state info.
3474 */
3475
3476 static char * /* O - Real filename */
3477 get_file(cupsd_client_t *con, /* I - Client connection */
3478 struct stat *filestats, /* O - File information */
3479 char *filename, /* IO - Filename buffer */
3480 int len) /* I - Buffer length */
3481 {
3482 int status; /* Status of filesystem calls */
3483 char *ptr; /* Pointer info filename */
3484 int plen; /* Remaining length after pointer */
3485 char language[7]; /* Language subdirectory, if any */
3486
3487
3488 /*
3489 * Figure out the real filename...
3490 */
3491
3492 language[0] = '\0';
3493
3494 if (!strncmp(con->uri, "/ppd/", 5))
3495 snprintf(filename, len, "%s%s", ServerRoot, con->uri);
3496 else if (!strncmp(con->uri, "/rss/", 5) && !strchr(con->uri + 5, '/'))
3497 snprintf(filename, len, "%s/rss/%s", CacheDir, con->uri + 5);
3498 else if (!strncmp(con->uri, "/admin/conf/", 12))
3499 snprintf(filename, len, "%s%s", ServerRoot, con->uri + 11);
3500 else if (!strncmp(con->uri, "/admin/log/", 11))
3501 {
3502 if (!strncmp(con->uri + 11, "access_log", 10) && AccessLog[0] == '/')
3503 strlcpy(filename, AccessLog, len);
3504 else if (!strncmp(con->uri + 11, "error_log", 9) && ErrorLog[0] == '/')
3505 strlcpy(filename, ErrorLog, len);
3506 else if (!strncmp(con->uri + 11, "page_log", 8) && PageLog[0] == '/')
3507 strlcpy(filename, PageLog, len);
3508 else
3509 return (NULL);
3510 }
3511 else if (con->language)
3512 {
3513 snprintf(language, sizeof(language), "/%s", con->language->language);
3514 snprintf(filename, len, "%s%s%s", DocumentRoot, language, con->uri);
3515 }
3516 else
3517 snprintf(filename, len, "%s%s", DocumentRoot, con->uri);
3518
3519 if ((ptr = strchr(filename, '?')) != NULL)
3520 *ptr = '\0';
3521
3522 /*
3523 * Grab the status for this language; if there isn't a language-specific file
3524 * then fallback to the default one...
3525 */
3526
3527 if ((status = stat(filename, filestats)) != 0 && language[0] &&
3528 strncmp(con->uri, "/ppd/", 5) &&
3529 strncmp(con->uri, "/admin/conf/", 12) &&
3530 strncmp(con->uri, "/admin/log/", 11))
3531 {
3532 /*
3533 * Drop the country code...
3534 */
3535
3536 language[3] = '\0';
3537 snprintf(filename, len, "%s%s%s", DocumentRoot, language, con->uri);
3538
3539 if ((ptr = strchr(filename, '?')) != NULL)
3540 *ptr = '\0';
3541
3542 if ((status = stat(filename, filestats)) != 0)
3543 {
3544 /*
3545 * Drop the language prefix and try the root directory...
3546 */
3547
3548 language[0] = '\0';
3549 snprintf(filename, len, "%s%s", DocumentRoot, con->uri);
3550
3551 if ((ptr = strchr(filename, '?')) != NULL)
3552 *ptr = '\0';
3553
3554 status = stat(filename, filestats);
3555 }
3556 }
3557
3558 /*
3559 * If we're found a directory, get the index.html file instead...
3560 */
3561
3562 if (!status && S_ISDIR(filestats->st_mode))
3563 {
3564 /*
3565 * Make sure the URI ends with a slash...
3566 */
3567
3568 if (con->uri[strlen(con->uri) - 1] != '/')
3569 strlcat(con->uri, "/", sizeof(con->uri));
3570
3571 /*
3572 * Find the directory index file, trying every language...
3573 */
3574
3575 do
3576 {
3577 if (status && language[0])
3578 {
3579 /*
3580 * Try a different language subset...
3581 */
3582
3583 if (language[3])
3584 language[0] = '\0'; /* Strip country code */
3585 else
3586 language[0] = '\0'; /* Strip language */
3587 }
3588
3589 /*
3590 * Look for the index file...
3591 */
3592
3593 snprintf(filename, len, "%s%s%s", DocumentRoot, language, con->uri);
3594
3595 if ((ptr = strchr(filename, '?')) != NULL)
3596 *ptr = '\0';
3597
3598 ptr = filename + strlen(filename);
3599 plen = len - (ptr - filename);
3600
3601 strlcpy(ptr, "index.html", plen);
3602 status = stat(filename, filestats);
3603
3604 #ifdef HAVE_JAVA
3605 if (status)
3606 {
3607 strlcpy(ptr, "index.class", plen);
3608 status = stat(filename, filestats);
3609 }
3610 #endif /* HAVE_JAVA */
3611
3612 #ifdef HAVE_PERL
3613 if (status)
3614 {
3615 strlcpy(ptr, "index.pl", plen);
3616 status = stat(filename, filestats);
3617 }
3618 #endif /* HAVE_PERL */
3619
3620 #ifdef HAVE_PHP
3621 if (status)
3622 {
3623 strlcpy(ptr, "index.php", plen);
3624 status = stat(filename, filestats);
3625 }
3626 #endif /* HAVE_PHP */
3627
3628 #ifdef HAVE_PYTHON
3629 if (status)
3630 {
3631 strlcpy(ptr, "index.pyc", plen);
3632 status = stat(filename, filestats);
3633 }
3634
3635 if (status)
3636 {
3637 strlcpy(ptr, "index.py", plen);
3638 status = stat(filename, filestats);
3639 }
3640 #endif /* HAVE_PYTHON */
3641
3642 }
3643 while (status && language[0]);
3644 }
3645
3646 cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_file: %d filename=%s size=%d",
3647 con->http.fd, filename,
3648 status ? -1 : (int)filestats->st_size);
3649
3650 if (!status)
3651 con->http.data_remaining = (int)filestats->st_size;
3652
3653 if (status)
3654 return (NULL);
3655 else
3656 return (filename);
3657 }
3658
3659
3660 /*
3661 * 'install_conf_file()' - Install a configuration file.
3662 */
3663
3664 static http_status_t /* O - Status */
3665 install_conf_file(cupsd_client_t *con) /* I - Connection */
3666 {
3667 cups_file_t *in, /* Input file */
3668 *out; /* Output file */
3669 char buffer[1024]; /* Copy buffer */
3670 int bytes; /* Number of bytes */
3671 char conffile[1024], /* Configuration filename */
3672 newfile[1024], /* New config filename */
3673 oldfile[1024]; /* Old config filename */
3674 struct stat confinfo; /* Config file info */
3675
3676
3677 /*
3678 * First construct the filenames...
3679 */
3680
3681 snprintf(conffile, sizeof(conffile), "%s%s", ServerRoot, con->uri + 11);
3682 snprintf(newfile, sizeof(newfile), "%s%s.N", ServerRoot, con->uri + 11);
3683 snprintf(oldfile, sizeof(oldfile), "%s%s.O", ServerRoot, con->uri + 11);
3684
3685 cupsdLogMessage(CUPSD_LOG_INFO, "Installing config file \"%s\"...", conffile);
3686
3687 /*
3688 * Get the owner, group, and permissions of the configuration file.
3689 * If it doesn't exist, assign it to the User and Group in the
3690 * cupsd.conf file with mode 0640 permissions.
3691 */
3692
3693 if (stat(conffile, &confinfo))
3694 {
3695 confinfo.st_uid = User;
3696 confinfo.st_gid = Group;
3697 confinfo.st_mode = ConfigFilePerm;
3698 }
3699
3700 /*
3701 * Open the request file and new config file...
3702 */
3703
3704 if ((in = cupsFileOpen(con->filename, "rb")) == NULL)
3705 {
3706 cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to open request file \"%s\" - %s",
3707 con->filename, strerror(errno));
3708 return (HTTP_SERVER_ERROR);
3709 }
3710
3711 if ((out = cupsFileOpen(newfile, "wb")) == NULL)
3712 {
3713 cupsFileClose(in);
3714 cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to open config file \"%s\" - %s",
3715 newfile, strerror(errno));
3716 return (HTTP_SERVER_ERROR);
3717 }
3718
3719 fchmod(cupsFileNumber(out), confinfo.st_mode);
3720 fchown(cupsFileNumber(out), confinfo.st_uid, confinfo.st_gid);
3721
3722 /*
3723 * Copy from the request to the new config file...
3724 */
3725
3726 while ((bytes = cupsFileRead(in, buffer, sizeof(buffer))) > 0)
3727 if (cupsFileWrite(out, buffer, bytes) < bytes)
3728 {
3729 cupsdLogMessage(CUPSD_LOG_ERROR,
3730 "Unable to copy to config file \"%s\" - %s",
3731 newfile, strerror(errno));
3732
3733 cupsFileClose(in);
3734 cupsFileClose(out);
3735 unlink(newfile);
3736
3737 return (HTTP_SERVER_ERROR);
3738 }
3739
3740 /*
3741 * Close the files...
3742 */
3743
3744 cupsFileClose(in);
3745 if (cupsFileClose(out))
3746 {
3747 cupsdLogMessage(CUPSD_LOG_ERROR,
3748 "Error file closing config file \"%s\" - %s",
3749 newfile, strerror(errno));
3750
3751 unlink(newfile);
3752
3753 return (HTTP_SERVER_ERROR);
3754 }
3755
3756 /*
3757 * Remove the request file...
3758 */
3759
3760 unlink(con->filename);
3761 cupsdClearString(&con->filename);
3762
3763 /*
3764 * Unlink the old backup, rename the current config file to the backup
3765 * filename, and rename the new config file to the config file name...
3766 */
3767
3768 if (unlink(oldfile))
3769 if (errno != ENOENT)
3770 {
3771 cupsdLogMessage(CUPSD_LOG_ERROR,
3772 "Unable to remove backup config file \"%s\" - %s",
3773 oldfile, strerror(errno));
3774
3775 unlink(newfile);
3776
3777 return (HTTP_SERVER_ERROR);
3778 }
3779
3780 if (rename(conffile, oldfile))
3781 if (errno != ENOENT)
3782 {
3783 cupsdLogMessage(CUPSD_LOG_ERROR,
3784 "Unable to rename old config file \"%s\" - %s",
3785 conffile, strerror(errno));
3786
3787 unlink(newfile);
3788
3789 return (HTTP_SERVER_ERROR);
3790 }
3791
3792 if (rename(newfile, conffile))
3793 {
3794 cupsdLogMessage(CUPSD_LOG_ERROR,
3795 "Unable to rename new config file \"%s\" - %s",
3796 newfile, strerror(errno));
3797
3798 rename(oldfile, conffile);
3799 unlink(newfile);
3800
3801 return (HTTP_SERVER_ERROR);
3802 }
3803
3804 /*
3805 * If the cupsd.conf file was updated, set the NeedReload flag...
3806 */
3807
3808 if (!strcmp(con->uri, "/admin/conf/cupsd.conf"))
3809 NeedReload = RELOAD_CUPSD;
3810 else
3811 NeedReload = RELOAD_ALL;
3812
3813 ReloadTime = time(NULL);
3814
3815 /*
3816 * Return that the file was created successfully...
3817 */
3818
3819 return (HTTP_CREATED);
3820 }
3821
3822
3823 /*
3824 * 'is_cgi()' - Is the resource a CGI script/program?
3825 */
3826
3827 static int /* O - 1 = CGI, 0 = file */
3828 is_cgi(cupsd_client_t *con, /* I - Client connection */
3829 const char *filename, /* I - Real filename */
3830 struct stat *filestats, /* I - File information */
3831 mime_type_t *type) /* I - MIME type */
3832 {
3833 const char *options; /* Options on URL */
3834
3835
3836 cupsdLogMessage(CUPSD_LOG_DEBUG2,
3837 "is_cgi(con=%p, filename=\"%s\", filestats=%p, type=%s/%s)",
3838 con, filename, filestats, type ? type->super : "unknown",
3839 type ? type->type : "unknown");
3840
3841 /*
3842 * Get the options, if any...
3843 */
3844
3845 if ((options = strchr(con->uri, '?')) != NULL)
3846 {
3847 options ++;
3848 cupsdSetStringf(&(con->query_string), "QUERY_STRING=%s", options);
3849 }
3850
3851 /*
3852 * Check for known types...
3853 */
3854
3855 if (!type || strcasecmp(type->super, "application"))
3856 {
3857 cupsdLogMessage(CUPSD_LOG_DEBUG2, "is_cgi: Returning 0...");
3858 return (0);
3859 }
3860
3861 if (!strcasecmp(type->type, "x-httpd-cgi") &&
3862 (filestats->st_mode & 0111))
3863 {
3864 /*
3865 * "application/x-httpd-cgi" is a CGI script.
3866 */
3867
3868 cupsdSetString(&con->command, filename);
3869
3870 if (options)
3871 cupsdSetStringf(&con->options, " %s", options);
3872
3873 cupsdLogMessage(CUPSD_LOG_DEBUG2,
3874 "is_cgi: Returning 1 with command=\"%s\" and options=\"%s\"",
3875 con->command, con->options);
3876
3877 return (1);
3878 }
3879 #ifdef HAVE_JAVA
3880 else if (!strcasecmp(type->type, "x-httpd-java"))
3881 {
3882 /*
3883 * "application/x-httpd-java" is a Java servlet.
3884 */
3885
3886 cupsdSetString(&con->command, CUPS_JAVA);
3887
3888 if (options)
3889 cupsdSetStringf(&con->options, " %s %s", filename, options);
3890 else
3891 cupsdSetStringf(&con->options, " %s", filename);
3892
3893 cupsdLogMessage(CUPSD_LOG_DEBUG2,
3894 "is_cgi: Returning 1 with command=\"%s\" and options=\"%s\"",
3895 con->command, con->options);
3896
3897 return (1);
3898 }
3899 #endif /* HAVE_JAVA */
3900 #ifdef HAVE_PERL
3901 else if (!strcasecmp(type->type, "x-httpd-perl"))
3902 {
3903 /*
3904 * "application/x-httpd-perl" is a Perl page.
3905 */
3906
3907 cupsdSetString(&con->command, CUPS_PERL);
3908
3909 if (options)
3910 cupsdSetStringf(&con->options, " %s %s", filename, options);
3911 else
3912 cupsdSetStringf(&con->options, " %s", filename);
3913
3914 cupsdLogMessage(CUPSD_LOG_DEBUG2,
3915 "is_cgi: Returning 1 with command=\"%s\" and options=\"%s\"",
3916 con->command, con->options);
3917
3918 return (1);
3919 }
3920 #endif /* HAVE_PERL */
3921 #ifdef HAVE_PHP
3922 else if (!strcasecmp(type->type, "x-httpd-php"))
3923 {
3924 /*
3925 * "application/x-httpd-php" is a PHP page.
3926 */
3927
3928 cupsdSetString(&con->command, CUPS_PHP);
3929
3930 if (options)
3931 cupsdSetStringf(&con->options, " %s %s", filename, options);
3932 else
3933 cupsdSetStringf(&con->options, " %s", filename);
3934
3935 cupsdLogMessage(CUPSD_LOG_DEBUG2,
3936 "is_cgi: Returning 1 with command=\"%s\" and options=\"%s\"",
3937 con->command, con->options);
3938
3939 return (1);
3940 }
3941 #endif /* HAVE_PHP */
3942 #ifdef HAVE_PYTHON
3943 else if (!strcasecmp(type->type, "x-httpd-python"))
3944 {
3945 /*
3946 * "application/x-httpd-python" is a Python page.
3947 */
3948
3949 cupsdSetString(&con->command, CUPS_PYTHON);
3950
3951 if (options)
3952 cupsdSetStringf(&con->options, " %s %s", filename, options);
3953 else
3954 cupsdSetStringf(&con->options, " %s", filename);
3955
3956 cupsdLogMessage(CUPSD_LOG_DEBUG2,
3957 "is_cgi: Returning 1 with command=\"%s\" and options=\"%s\"",
3958 con->command, con->options);
3959
3960 return (1);
3961 }
3962 #endif /* HAVE_PYTHON */
3963
3964 cupsdLogMessage(CUPSD_LOG_DEBUG2, "is_cgi: Returning 0...");
3965
3966 return (0);
3967 }
3968
3969
3970 /*
3971 * 'is_path_absolute()' - Is a path absolute and free of relative elements (i.e. "..").
3972 */
3973
3974 static int /* O - 0 if relative, 1 if absolute */
3975 is_path_absolute(const char *path) /* I - Input path */
3976 {
3977 /*
3978 * Check for a leading slash...
3979 */
3980
3981 if (path[0] != '/')
3982 return (0);
3983
3984 /*
3985 * Check for "/.." in the path...
3986 */
3987
3988 while ((path = strstr(path, "/..")) != NULL)
3989 {
3990 if (!path[3] || path[3] == '/')
3991 return (0);
3992
3993 path ++;
3994 }
3995
3996 /*
3997 * If we haven't found any relative paths, return 1 indicating an
3998 * absolute path...
3999 */
4000
4001 return (1);
4002 }
4003
4004
4005 #ifdef HAVE_SSL
4006 /*
4007 * 'make_certificate()' - Make a self-signed SSL/TLS certificate.
4008 */
4009
4010 static int /* O - 1 on success, 0 on failure */
4011 make_certificate(cupsd_client_t *con) /* I - Client connection */
4012 {
4013 #if defined(HAVE_LIBSSL) && defined(HAVE_WAITPID)
4014 int pid, /* Process ID of command */
4015 status; /* Status of command */
4016 char command[1024], /* Command */
4017 *argv[12], /* Command-line arguments */
4018 *envp[MAX_ENV + 1], /* Environment variables */
4019 home[1024], /* HOME environment variable */
4020 infofile[1024], /* Type-in information for cert */
4021 seedfile[1024]; /* Random number seed file */
4022 int envc, /* Number of environment variables */
4023 bytes; /* Bytes written */
4024 cups_file_t *fp; /* Seed/info file */
4025 int infofd; /* Info file descriptor */
4026
4027
4028 /*
4029 * Run the "openssl" command to seed the random number generator and
4030 * generate a self-signed certificate that is good for 10 years:
4031 *
4032 * openssl rand -rand seedfile 1
4033 *
4034 * openssl req -new -x509 -keyout ServerKey \
4035 * -out ServerCertificate -days 3650 -nodes
4036 *
4037 * The seeding step is crucial in ensuring that the openssl command
4038 * does not block on systems without sufficient entropy...
4039 */
4040
4041 if (!cupsFileFind("openssl", getenv("PATH"), 1, command, sizeof(command)))
4042 {
4043 cupsdLogMessage(CUPSD_LOG_ERROR,
4044 "No SSL certificate and openssl command not found!");
4045 return (0);
4046 }
4047
4048 if (access("/dev/urandom", 0))
4049 {
4050 /*
4051 * If the system doesn't provide /dev/urandom, then any random source
4052 * will probably be blocking-style, so generate some random data to
4053 * use as a seed for the certificate. Note that we have already
4054 * seeded the random number generator in cupsdInitCerts()...
4055 */
4056
4057 cupsdLogMessage(CUPSD_LOG_INFO,
4058 "Seeding the random number generator...");
4059
4060 snprintf(home, sizeof(home), "HOME=%s", TempDir);
4061
4062 /*
4063 * Write the seed file...
4064 */
4065
4066 if ((fp = cupsTempFile2(seedfile, sizeof(seedfile))) == NULL)
4067 {
4068 cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to create seed file %s - %s",
4069 seedfile, strerror(errno));
4070 return (0);
4071 }
4072
4073 for (bytes = 0; bytes < 262144; bytes ++)
4074 cupsFilePutChar(fp, random());
4075
4076 cupsFileClose(fp);
4077
4078 /*
4079 * Run the openssl command to seed its random number generator...
4080 */
4081
4082 argv[0] = "openssl";
4083 argv[1] = "rand";
4084 argv[2] = "-rand";
4085 argv[3] = seedfile;
4086 argv[4] = "1";
4087 argv[5] = NULL;
4088
4089 envc = cupsdLoadEnv(envp, MAX_ENV);
4090 envp[envc++] = home;
4091 envp[envc] = NULL;
4092
4093 if (!cupsdStartProcess(command, argv, envp, -1, -1, -1, -1, -1, 1, NULL,
4094 &pid))
4095 {
4096 unlink(seedfile);
4097 return (0);
4098 }
4099
4100 while (waitpid(pid, &status, 0) < 0)
4101 if (errno != EINTR)
4102 {
4103 status = 1;
4104 break;
4105 }
4106
4107 cupsdFinishProcess(pid, command, sizeof(command));
4108
4109 /*
4110 * Remove the seed file, as it is no longer needed...
4111 */
4112
4113 unlink(seedfile);
4114
4115 if (status)
4116 {
4117 if (WIFEXITED(status))
4118 cupsdLogMessage(CUPSD_LOG_ERROR,
4119 "Unable to seed random number generator - "
4120 "the openssl command stopped with status %d!",
4121 WEXITSTATUS(status));
4122 else
4123 cupsdLogMessage(CUPSD_LOG_ERROR,
4124 "Unable to seed random number generator - "
4125 "the openssl command crashed on signal %d!",
4126 WTERMSIG(status));
4127
4128 return (0);
4129 }
4130 }
4131
4132 /*
4133 * Create a file with the certificate information fields...
4134 *
4135 * Note: This assumes that the default questions are asked by the openssl
4136 * command...
4137 */
4138
4139 if ((fp = cupsTempFile2(infofile, sizeof(infofile))) == NULL)
4140 {
4141 cupsdLogMessage(CUPSD_LOG_ERROR,
4142 "Unable to create certificate information file %s - %s",
4143 infofile, strerror(errno));
4144 return (0);
4145 }
4146
4147 cupsFilePrintf(fp, ".\n.\n.\n%s\n.\n%s\n%s\n",
4148 ServerName, ServerName, ServerAdmin);
4149 cupsFileClose(fp);
4150
4151 cupsdLogMessage(CUPSD_LOG_INFO,
4152 "Generating SSL server key and certificate...");
4153
4154 argv[0] = "openssl";
4155 argv[1] = "req";
4156 argv[2] = "-new";
4157 argv[3] = "-x509";
4158 argv[4] = "-keyout";
4159 argv[5] = ServerKey;
4160 argv[6] = "-out";
4161 argv[7] = ServerCertificate;
4162 argv[8] = "-days";
4163 argv[9] = "3650";
4164 argv[10] = "-nodes";
4165 argv[11] = NULL;
4166
4167 cupsdLoadEnv(envp, MAX_ENV);
4168
4169 infofd = open(infofile, O_RDONLY);
4170
4171 if (!cupsdStartProcess(command, argv, envp, infofd, -1, -1, -1, -1, 1, NULL,
4172 &pid))
4173 {
4174 close(infofd);
4175 unlink(infofile);
4176 return (0);
4177 }
4178
4179 close(infofd);
4180 unlink(infofile);
4181
4182 while (waitpid(pid, &status, 0) < 0)
4183 if (errno != EINTR)
4184 {
4185 status = 1;
4186 break;
4187 }
4188
4189 cupsdFinishProcess(pid, command, sizeof(command));
4190
4191 if (status)
4192 {
4193 if (WIFEXITED(status))
4194 cupsdLogMessage(CUPSD_LOG_ERROR,
4195 "Unable to create SSL server key and certificate - "
4196 "the openssl command stopped with status %d!",
4197 WEXITSTATUS(status));
4198 else
4199 cupsdLogMessage(CUPSD_LOG_ERROR,
4200 "Unable to create SSL server key and certificate - "
4201 "the openssl command crashed on signal %d!",
4202 WTERMSIG(status));
4203 }
4204 else
4205 {
4206 cupsdLogMessage(CUPSD_LOG_INFO, "Created SSL server key file \"%s\"...",
4207 ServerKey);
4208 cupsdLogMessage(CUPSD_LOG_INFO,
4209 "Created SSL server certificate file \"%s\"...",
4210 ServerCertificate);
4211 }
4212
4213 return (!status);
4214
4215 #elif defined(HAVE_GNUTLS)
4216 gnutls_x509_crt crt; /* Self-signed certificate */
4217 gnutls_x509_privkey key; /* Encryption key */
4218 cups_lang_t *language; /* Default language info */
4219 cups_file_t *fp; /* Key/cert file */
4220 unsigned char buffer[8192]; /* Buffer for x509 data */
4221 size_t bytes; /* Number of bytes of data */
4222 unsigned char serial[4]; /* Serial number buffer */
4223 time_t curtime; /* Current time */
4224 int result; /* Result of GNU TLS calls */
4225
4226
4227 /*
4228 * Create the encryption key...
4229 */
4230
4231 cupsdLogMessage(CUPSD_LOG_INFO, "Generating SSL server key...");
4232
4233 gnutls_x509_privkey_init(&key);
4234 gnutls_x509_privkey_generate(key, GNUTLS_PK_RSA, 2048, 0);
4235
4236 /*
4237 * Save it...
4238 */
4239
4240 bytes = sizeof(buffer);
4241
4242 if ((result = gnutls_x509_privkey_export(key, GNUTLS_X509_FMT_PEM,
4243 buffer, &bytes)) < 0)
4244 {
4245 cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to export SSL server key - %s",
4246 gnutls_strerror(result));
4247 gnutls_x509_privkey_deinit(key);
4248 return (0);
4249 }
4250 else if ((fp = cupsFileOpen(ServerKey, "w")) != NULL)
4251 {
4252 cupsFileWrite(fp, (char *)buffer, bytes);
4253 cupsFileClose(fp);
4254
4255 cupsdLogMessage(CUPSD_LOG_INFO, "Created SSL server key file \"%s\"...",
4256 ServerKey);
4257 }
4258 else
4259 {
4260 cupsdLogMessage(CUPSD_LOG_ERROR,
4261 "Unable to create SSL server key file \"%s\" - %s",
4262 ServerKey, strerror(errno));
4263 gnutls_x509_privkey_deinit(key);
4264 return (0);
4265 }
4266
4267 /*
4268 * Create the self-signed certificate...
4269 */
4270
4271 cupsdLogMessage(CUPSD_LOG_INFO, "Generating self-signed SSL certificate...");
4272
4273 language = cupsLangDefault();
4274 curtime = time(NULL);
4275 serial[0] = curtime >> 24;
4276 serial[1] = curtime >> 16;
4277 serial[2] = curtime >> 8;
4278 serial[3] = curtime;
4279
4280 gnutls_x509_crt_init(&crt);
4281 if (strlen(language->language) == 5)
4282 gnutls_x509_crt_set_dn_by_oid(crt, GNUTLS_OID_X520_COUNTRY_NAME, 0,
4283 language->language + 3, 2);
4284 else
4285 gnutls_x509_crt_set_dn_by_oid(crt, GNUTLS_OID_X520_COUNTRY_NAME, 0,
4286 "US", 2);
4287 gnutls_x509_crt_set_dn_by_oid(crt, GNUTLS_OID_X520_COMMON_NAME, 0,
4288 ServerName, strlen(ServerName));
4289 gnutls_x509_crt_set_dn_by_oid(crt, GNUTLS_OID_X520_ORGANIZATION_NAME, 0,
4290 ServerName, strlen(ServerName));
4291 gnutls_x509_crt_set_dn_by_oid(crt, GNUTLS_OID_X520_ORGANIZATIONAL_UNIT_NAME,
4292 0, "Unknown", 7);
4293 gnutls_x509_crt_set_dn_by_oid(crt, GNUTLS_OID_X520_STATE_OR_PROVINCE_NAME, 0,
4294 "Unknown", 7);
4295 gnutls_x509_crt_set_dn_by_oid(crt, GNUTLS_OID_X520_LOCALITY_NAME, 0,
4296 "Unknown", 7);
4297 gnutls_x509_crt_set_dn_by_oid(crt, GNUTLS_OID_PKCS9_EMAIL, 0,
4298 ServerAdmin, strlen(ServerAdmin));
4299 gnutls_x509_crt_set_key(crt, key);
4300 gnutls_x509_crt_set_serial(crt, serial, sizeof(serial));
4301 gnutls_x509_crt_set_activation_time(crt, curtime);
4302 gnutls_x509_crt_set_expiration_time(crt, curtime + 10 * 365 * 86400);
4303 gnutls_x509_crt_set_ca_status(crt, 0);
4304 gnutls_x509_crt_set_subject_alternative_name(crt, GNUTLS_SAN_DNSNAME,
4305 ServerName);
4306 gnutls_x509_crt_set_key_purpose_oid(crt, GNUTLS_KP_TLS_WWW_SERVER, 0);
4307 gnutls_x509_crt_set_key_usage(crt, GNUTLS_KEY_KEY_ENCIPHERMENT);
4308 gnutls_x509_crt_set_version(crt, 3);
4309
4310 bytes = sizeof(buffer);
4311 if (gnutls_x509_crt_get_key_id(crt, 0, buffer, &bytes) >= 0)
4312 gnutls_x509_crt_set_subject_key_id(crt, buffer, bytes);
4313
4314 gnutls_x509_crt_sign(crt, crt, key);
4315
4316 /*
4317 * Save it...
4318 */
4319
4320 bytes = sizeof(buffer);
4321 if ((result = gnutls_x509_crt_export(crt, GNUTLS_X509_FMT_PEM,
4322 buffer, &bytes)) < 0)
4323 cupsdLogMessage(CUPSD_LOG_ERROR,
4324 "Unable to export SSL server certificate - %s",
4325 gnutls_strerror(result));
4326 else if ((fp = cupsFileOpen(ServerCertificate, "w")) != NULL)
4327 {
4328 cupsFileWrite(fp, (char *)buffer, bytes);
4329 cupsFileClose(fp);
4330
4331 cupsdLogMessage(CUPSD_LOG_INFO,
4332 "Created SSL server certificate file \"%s\"...",
4333 ServerCertificate);
4334 }
4335 else
4336 cupsdLogMessage(CUPSD_LOG_ERROR,
4337 "Unable to create SSL server certificate file \"%s\" - %s",
4338 ServerCertificate, strerror(errno));
4339
4340 /*
4341 * Cleanup...
4342 */
4343
4344 gnutls_x509_crt_deinit(crt);
4345 gnutls_x509_privkey_deinit(key);
4346
4347 return (1);
4348
4349 #elif defined(HAVE_CDSASSL) && defined(HAVE_WAITPID)
4350 int pid, /* Process ID of command */
4351 status; /* Status of command */
4352 char command[1024], /* Command */
4353 *argv[4], /* Command-line arguments */
4354 *envp[MAX_ENV + 1], /* Environment variables */
4355 keychain[1024], /* Keychain argument */
4356 infofile[1024]; /* Type-in information for cert */
4357 cups_file_t *fp; /* Seed/info file */
4358 int infofd; /* Info file descriptor */
4359
4360
4361 /*
4362 * Run the "certtool" command to generate a self-signed certificate...
4363 */
4364
4365 if (!cupsFileFind("certtool", getenv("PATH"), 1, command, sizeof(command)))
4366 {
4367 cupsdLogMessage(CUPSD_LOG_ERROR,
4368 "No SSL certificate and certtool command not found!");
4369 return (0);
4370 }
4371
4372 /*
4373 * Create a file with the certificate information fields...
4374 *
4375 * Note: This assumes that the default questions are asked by the certtool
4376 * command...
4377 */
4378
4379 if ((fp = cupsTempFile2(infofile, sizeof(infofile))) == NULL)
4380 {
4381 cupsdLogMessage(CUPSD_LOG_ERROR,
4382 "Unable to create certificate information file %s - %s",
4383 infofile, strerror(errno));
4384 return (0);
4385 }
4386
4387 cupsFilePrintf(fp, "%s\nr\n\ny\nb\ns\ny\n%s\n\n\n\n\n%s\ny\n",
4388 con->servername, con->servername, ServerAdmin);
4389 cupsFileClose(fp);
4390
4391 cupsdLogMessage(CUPSD_LOG_INFO,
4392 "Generating SSL server key and certificate...");
4393
4394 snprintf(keychain, sizeof(keychain), "k=%s", ServerCertificate);
4395
4396 argv[0] = "certtool";
4397 argv[1] = "c";
4398 argv[2] = keychain;
4399 argv[3] = NULL;
4400
4401 cupsdLoadEnv(envp, MAX_ENV);
4402
4403 infofd = open(infofile, O_RDONLY);
4404
4405 if (!cupsdStartProcess(command, argv, envp, infofd, -1, -1, -1, -1, 1, NULL,
4406 &pid))
4407 {
4408 close(infofd);
4409 unlink(infofile);
4410 return (0);
4411 }
4412
4413 close(infofd);
4414 unlink(infofile);
4415
4416 while (waitpid(pid, &status, 0) < 0)
4417 if (errno != EINTR)
4418 {
4419 status = 1;
4420 break;
4421 }
4422
4423 cupsdFinishProcess(pid, command, sizeof(command));
4424
4425 if (status)
4426 {
4427 if (WIFEXITED(status))
4428 cupsdLogMessage(CUPSD_LOG_ERROR,
4429 "Unable to create SSL server key and certificate - "
4430 "the certtool command stopped with status %d!",
4431 WEXITSTATUS(status));
4432 else
4433 cupsdLogMessage(CUPSD_LOG_ERROR,
4434 "Unable to create SSL server key and certificate - "
4435 "the certtool command crashed on signal %d!",
4436 WTERMSIG(status));
4437 }
4438 else
4439 {
4440 cupsdLogMessage(CUPSD_LOG_INFO,
4441 "Created SSL server certificate file \"%s\"...",
4442 ServerCertificate);
4443 }
4444
4445 return (!status);
4446
4447 #else
4448 return (0);
4449 #endif /* HAVE_LIBSSL && HAVE_WAITPID */
4450 }
4451 #endif /* HAVE_SSL */
4452
4453
4454 /*
4455 * 'pipe_command()' - Pipe the output of a command to the remote client.
4456 */
4457
4458 static int /* O - Process ID */
4459 pipe_command(cupsd_client_t *con, /* I - Client connection */
4460 int infile, /* I - Standard input for command */
4461 int *outfile, /* O - Standard output for command */
4462 char *command, /* I - Command to run */
4463 char *options, /* I - Options for command */
4464 int root) /* I - Run as root? */
4465 {
4466 int i; /* Looping var */
4467 int pid; /* Process ID */
4468 char *commptr, /* Command string pointer */
4469 commch; /* Command string character */
4470 char *uriptr; /* URI string pointer */
4471 int fds[2]; /* Pipe FDs */
4472 int argc; /* Number of arguments */
4473 int envc; /* Number of environment variables */
4474 char argbuf[10240], /* Argument buffer */
4475 *argv[100], /* Argument strings */
4476 *envp[MAX_ENV + 20]; /* Environment variables */
4477 char auth_type[256], /* CUPSD_AUTH_TYPE environment variable */
4478 content_length[1024], /* CONTENT_LENGTH environment variable */
4479 content_type[1024], /* CONTENT_TYPE environment variable */
4480 http_cookie[32768], /* HTTP_COOKIE environment variable */
4481 http_referer[1024], /* HTTP_REFERER environment variable */
4482 http_user_agent[1024], /* HTTP_USER_AGENT environment variable */
4483 lang[1024], /* LANG environment variable */
4484 path_info[1024], /* PATH_INFO environment variable */
4485 remote_addr[1024], /* REMOTE_ADDR environment variable */
4486 remote_host[1024], /* REMOTE_HOST environment variable */
4487 remote_user[1024], /* REMOTE_USER environment variable */
4488 script_filename[1024], /* SCRIPT_FILENAME environment variable */
4489 script_name[1024], /* SCRIPT_NAME environment variable */
4490 server_name[1024], /* SERVER_NAME environment variable */
4491 server_port[1024]; /* SERVER_PORT environment variable */
4492 ipp_attribute_t *attr; /* attributes-natural-language attribute */
4493 #ifdef HAVE_GSSAPI
4494 krb5_ccache ccache = NULL; /* Kerberos credentials */
4495 # if defined(HAVE_KRB5_CC_NEW_UNIQUE) || defined(HAVE_HEIMDAL)
4496 char krb5ccname[1024]; /* KRB5CCNAME environment variable */
4497 # endif /* HAVE_KRB5_CC_NEW_UNIQUE || HAVE_HEIMDAL */
4498 #endif /* HAVE_GSSAPI */
4499
4500
4501 /*
4502 * Parse a copy of the options string, which is of the form:
4503 *
4504 * argument+argument+argument
4505 * ?argument+argument+argument
4506 * param=value&param=value
4507 * ?param=value&param=value
4508 * /name?argument+argument+argument
4509 * /name?param=value&param=value
4510 *
4511 * If the string contains an "=" character after the initial name,
4512 * then we treat it as a HTTP GET form request and make a copy of
4513 * the remaining string for the environment variable.
4514 *
4515 * The string is always parsed out as command-line arguments, to
4516 * be consistent with Apache...
4517 */
4518
4519 cupsdLogMessage(CUPSD_LOG_DEBUG2,
4520 "pipe_command: command=\"%s\", options=\"%s\"",
4521 command, options ? options : "(null)");
4522
4523 argv[0] = command;
4524
4525 if (options)
4526 strlcpy(argbuf, options, sizeof(argbuf));
4527 else
4528 argbuf[0] = '\0';
4529
4530 if (argbuf[0] == '/')
4531 {
4532 /*
4533 * Found some trailing path information, set PATH_INFO...
4534 */
4535
4536 if ((commptr = strchr(argbuf, '?')) == NULL)
4537 commptr = argbuf + strlen(argbuf);
4538
4539 commch = *commptr;
4540 *commptr = '\0';
4541 snprintf(path_info, sizeof(path_info), "PATH_INFO=%s", argbuf);
4542 *commptr = commch;
4543 }
4544 else
4545 {
4546 commptr = argbuf;
4547 path_info[0] = '\0';
4548
4549 if (*commptr == ' ')
4550 commptr ++;
4551 }
4552
4553 if (*commptr == '?' && con->operation == HTTP_GET && !con->query_string)
4554 {
4555 commptr ++;
4556 cupsdSetStringf(&(con->query_string), "QUERY_STRING=%s", commptr);
4557 }
4558
4559 argc = 1;
4560
4561 if (*commptr)
4562 {
4563 argv[argc ++] = commptr;
4564
4565 for (; *commptr && argc < 99; commptr ++)
4566 {
4567 /*
4568 * Break arguments whenever we see a + or space...
4569 */
4570
4571 if (*commptr == ' ' || *commptr == '+')
4572 {
4573 while (*commptr == ' ' || *commptr == '+')
4574 *commptr++ = '\0';
4575
4576 /*
4577 * If we don't have a blank string, save it as another argument...
4578 */
4579
4580 if (*commptr)
4581 {
4582 argv[argc] = commptr;
4583 argc ++;
4584 }
4585 else
4586 break;
4587 }
4588 else if (*commptr == '%' && isxdigit(commptr[1] & 255) &&
4589 isxdigit(commptr[2] & 255))
4590 {
4591 /*
4592 * Convert the %xx notation to the individual character.
4593 */
4594
4595 if (commptr[1] >= '0' && commptr[1] <= '9')
4596 *commptr = (commptr[1] - '0') << 4;
4597 else
4598 *commptr = (tolower(commptr[1]) - 'a' + 10) << 4;
4599
4600 if (commptr[2] >= '0' && commptr[2] <= '9')
4601 *commptr |= commptr[2] - '0';
4602 else
4603 *commptr |= tolower(commptr[2]) - 'a' + 10;
4604
4605 _cups_strcpy(commptr + 1, commptr + 3);
4606
4607 /*
4608 * Check for a %00 and break if that is the case...
4609 */
4610
4611 if (!*commptr)
4612 break;
4613 }
4614 }
4615 }
4616
4617 argv[argc] = NULL;
4618
4619 /*
4620 * Setup the environment variables as needed...
4621 */
4622
4623 if (con->username[0])
4624 {
4625 snprintf(auth_type, sizeof(auth_type), "CUPSD_AUTH_TYPE=%s",
4626 httpGetField(HTTP(con), HTTP_FIELD_AUTHORIZATION));
4627
4628 if ((uriptr = strchr(auth_type + 10, ' ')) != NULL)
4629 *uriptr = '\0';
4630 }
4631 else
4632 auth_type[0] = '\0';
4633
4634 if (con->request &&
4635 (attr = ippFindAttribute(con->request, "attributes-natural-language",
4636 IPP_TAG_LANGUAGE)) != NULL)
4637 {
4638 switch (strlen(attr->values[0].string.text))
4639 {
4640 default :
4641 /*
4642 * This is an unknown or badly formatted language code; use
4643 * the POSIX locale...
4644 */
4645
4646 strcpy(lang, "LANG=C");
4647 break;
4648
4649 case 2 :
4650 /*
4651 * Just the language code (ll)...
4652 */
4653
4654 snprintf(lang, sizeof(lang), "LANG=%s.UTF8",
4655 attr->values[0].string.text);
4656 break;
4657
4658 case 5 :
4659 /*
4660 * Language and country code (ll-cc)...
4661 */
4662
4663 snprintf(lang, sizeof(lang), "LANG=%c%c_%c%c.UTF8",
4664 attr->values[0].string.text[0],
4665 attr->values[0].string.text[1],
4666 toupper(attr->values[0].string.text[3] & 255),
4667 toupper(attr->values[0].string.text[4] & 255));
4668 break;
4669 }
4670 }
4671 else if (con->language)
4672 snprintf(lang, sizeof(lang), "LANG=%s.UTF8", con->language->language);
4673 else
4674 strcpy(lang, "LANG=C");
4675
4676 strcpy(remote_addr, "REMOTE_ADDR=");
4677 httpAddrString(con->http.hostaddr, remote_addr + 12,
4678 sizeof(remote_addr) - 12);
4679
4680 snprintf(remote_host, sizeof(remote_host), "REMOTE_HOST=%s",
4681 con->http.hostname);
4682
4683 snprintf(script_name, sizeof(script_name), "SCRIPT_NAME=%s", con->uri);
4684 if ((uriptr = strchr(script_name, '?')) != NULL)
4685 *uriptr = '\0';
4686
4687 snprintf(script_filename, sizeof(script_filename), "SCRIPT_FILENAME=%s%s",
4688 DocumentRoot, script_name + 12);
4689
4690 sprintf(server_port, "SERVER_PORT=%d", con->serverport);
4691
4692 snprintf(server_name, sizeof(server_name), "SERVER_NAME=%s",
4693 con->servername);
4694
4695 envc = cupsdLoadEnv(envp, (int)(sizeof(envp) / sizeof(envp[0])));
4696
4697 if (auth_type[0])
4698 envp[envc ++] = auth_type;
4699
4700 envp[envc ++] = lang;
4701 envp[envc ++] = "REDIRECT_STATUS=1";
4702 envp[envc ++] = "GATEWAY_INTERFACE=CGI/1.1";
4703 envp[envc ++] = server_name;
4704 envp[envc ++] = server_port;
4705 envp[envc ++] = remote_addr;
4706 envp[envc ++] = remote_host;
4707 envp[envc ++] = script_name;
4708 envp[envc ++] = script_filename;
4709
4710 if (path_info[0])
4711 envp[envc ++] = path_info;
4712
4713 if (con->username[0])
4714 {
4715 snprintf(remote_user, sizeof(remote_user), "REMOTE_USER=%s", con->username);
4716
4717 envp[envc ++] = remote_user;
4718
4719 /*
4720 * Save Kerberos credentials, if any...
4721 */
4722
4723 #ifdef HAVE_GSSAPI
4724 if (con->gss_have_creds)
4725 {
4726 # if !defined(HAVE_KRB5_CC_NEW_UNIQUE) && !defined(HAVE_HEIMDAL)
4727 cupsdLogMessage(CUPSD_LOG_INFO,
4728 "Sorry, your version of Kerberos does not support "
4729 "delegated credentials!");
4730
4731 # else
4732 krb5_error_code error; /* Kerberos error code */
4733 OM_uint32 major_status, /* Major status code */
4734 minor_status; /* Minor status code */
4735 krb5_principal principal; /* Kerberos principal */
4736
4737
4738 # ifdef __APPLE__
4739 /*
4740 * If the weak-linked GSSAPI/Kerberos library is not present, don't try
4741 * to use it...
4742 */
4743
4744 if (krb5_init_context != NULL)
4745 {
4746 # endif /* __APPLE__ */
4747
4748 if (!KerberosInitialized)
4749 {
4750 /*
4751 * Setup a Kerberos context for the scheduler to use...
4752 */
4753
4754 KerberosInitialized = 1;
4755
4756 if (krb5_init_context(&KerberosContext))
4757 {
4758 KerberosContext = NULL;
4759
4760 cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to initialize Kerberos context");
4761 }
4762 }
4763
4764 /*
4765 * We MUST create a file-based cache because memory-based caches are
4766 * only valid for the current process/address space.
4767 *
4768 * Due to various bugs/features in different versions of Kerberos, we
4769 * need either the krb5_cc_new_unique() function or Heimdal's version
4770 * of krb5_cc_gen_new() to create a new FILE: credential cache that
4771 * can be passed to the backend. These functions create a temporary
4772 * file (typically in /tmp) containing the cached credentials, which
4773 * are removed when we have successfully printed a job.
4774 */
4775
4776 if (KerberosContext)
4777 {
4778 # ifdef HAVE_KRB5_CC_NEW_UNIQUE
4779 if ((error = krb5_cc_new_unique(KerberosContext, "FILE", NULL,
4780 &ccache)) != 0)
4781 # else /* HAVE_HEIMDAL */
4782 if ((error = krb5_cc_gen_new(KerberosContext, &krb5_fcc_ops,
4783 &ccache)) != 0)
4784 # endif /* HAVE_KRB5_CC_NEW_UNIQUE */
4785 {
4786 cupsdLogMessage(CUPSD_LOG_ERROR,
4787 "Unable to create new credentials cache (%d/%s)",
4788 error, strerror(errno));
4789 ccache = NULL;
4790 }
4791 else if ((error = krb5_parse_name(KerberosContext, con->username,
4792 &principal)) != 0)
4793 {
4794 cupsdLogMessage(CUPSD_LOG_ERROR,
4795 "Unable to parse kerberos username (%d/%s)", error,
4796 strerror(errno));
4797 krb5_cc_destroy(KerberosContext, ccache);
4798 ccache = NULL;
4799 }
4800 else if ((error = krb5_cc_initialize(KerberosContext, ccache,
4801 principal)))
4802 {
4803 cupsdLogMessage(CUPSD_LOG_ERROR,
4804 "Unable to initialize credentials cache (%d/%s)", error,
4805 strerror(errno));
4806 krb5_cc_destroy(KerberosContext, ccache);
4807 krb5_free_principal(KerberosContext, principal);
4808 ccache = NULL;
4809 }
4810 else
4811 {
4812 krb5_free_principal(KerberosContext, principal);
4813
4814 /*
4815 * Copy the user's credentials to the new cache file...
4816 */
4817
4818 major_status = gss_krb5_copy_ccache(&minor_status,
4819 con->gss_delegated_cred, ccache);
4820
4821 if (GSS_ERROR(major_status))
4822 {
4823 cupsdLogGSSMessage(CUPSD_LOG_ERROR, major_status, minor_status,
4824 "Unable to import client credentials cache");
4825 krb5_cc_destroy(KerberosContext, ccache);
4826 ccache = NULL;
4827 }
4828 else
4829 {
4830 /*
4831 * Add the KRB5CCNAME environment variable to the job so that the
4832 * backend can use the credentials when printing.
4833 */
4834
4835 snprintf(krb5ccname, sizeof(krb5ccname), "KRB5CCNAME=FILE:%s",
4836 krb5_cc_get_name(KerberosContext, ccache));
4837 envp[envc++] = krb5ccname;
4838
4839 if (!RunUser)
4840 chown(krb5_cc_get_name(KerberosContext, ccache), User, Group);
4841 }
4842 }
4843 }
4844 # ifdef __APPLE__
4845 }
4846 # endif /* __APPLE__ */
4847 # endif /* HAVE_KRB5_CC_NEW_UNIQUE || HAVE_HEIMDAL */
4848 }
4849 #endif /* HAVE_GSSAPI */
4850 }
4851
4852 if (con->http.version == HTTP_1_1)
4853 envp[envc ++] = "SERVER_PROTOCOL=HTTP/1.1";
4854 else if (con->http.version == HTTP_1_0)
4855 envp[envc ++] = "SERVER_PROTOCOL=HTTP/1.0";
4856 else
4857 envp[envc ++] = "SERVER_PROTOCOL=HTTP/0.9";
4858
4859 if (con->http.cookie)
4860 {
4861 snprintf(http_cookie, sizeof(http_cookie), "HTTP_COOKIE=%s",
4862 con->http.cookie);
4863 envp[envc ++] = http_cookie;
4864 }
4865
4866 if (con->http.fields[HTTP_FIELD_USER_AGENT][0])
4867 {
4868 snprintf(http_user_agent, sizeof(http_user_agent), "HTTP_USER_AGENT=%s",
4869 con->http.fields[HTTP_FIELD_USER_AGENT]);
4870 envp[envc ++] = http_user_agent;
4871 }
4872
4873 if (con->http.fields[HTTP_FIELD_REFERER][0])
4874 {
4875 snprintf(http_referer, sizeof(http_referer), "HTTP_REFERER=%s",
4876 con->http.fields[HTTP_FIELD_REFERER]);
4877 envp[envc ++] = http_referer;
4878 }
4879
4880 if (con->operation == HTTP_GET)
4881 {
4882 envp[envc ++] = "REQUEST_METHOD=GET";
4883
4884 if (con->query_string)
4885 {
4886 /*
4887 * Add GET form variables after ?...
4888 */
4889
4890 envp[envc ++] = con->query_string;
4891 }
4892 else
4893 envp[envc ++] = "QUERY_STRING=";
4894 }
4895 else
4896 {
4897 sprintf(content_length, "CONTENT_LENGTH=" CUPS_LLFMT,
4898 CUPS_LLCAST con->bytes);
4899 snprintf(content_type, sizeof(content_type), "CONTENT_TYPE=%s",
4900 con->http.fields[HTTP_FIELD_CONTENT_TYPE]);
4901
4902 envp[envc ++] = "REQUEST_METHOD=POST";
4903 envp[envc ++] = content_length;
4904 envp[envc ++] = content_type;
4905 }
4906
4907 /*
4908 * Tell the CGI if we are using encryption...
4909 */
4910
4911 if (con->http.tls)
4912 envp[envc ++] = "HTTPS=ON";
4913
4914 /*
4915 * Terminate the environment array...
4916 */
4917
4918 envp[envc] = NULL;
4919
4920 if (LogLevel == CUPSD_LOG_DEBUG2)
4921 {
4922 for (i = 0; i < argc; i ++)
4923 cupsdLogMessage(CUPSD_LOG_DEBUG2,
4924 "pipe_command: argv[%d] = \"%s\"", i, argv[i]);
4925 for (i = 0; i < envc; i ++)
4926 cupsdLogMessage(CUPSD_LOG_DEBUG2,
4927 "pipe_command: envp[%d] = \"%s\"", i, envp[i]);
4928 }
4929
4930 /*
4931 * Create a pipe for the output...
4932 */
4933
4934 if (cupsdOpenPipe(fds))
4935 {
4936 cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to create pipes for CGI %s - %s",
4937 argv[0], strerror(errno));
4938 return (0);
4939 }
4940
4941 /*
4942 * Then execute the command...
4943 */
4944
4945 if (cupsdStartProcess(command, argv, envp, infile, fds[1], CGIPipes[1],
4946 -1, -1, root, DefaultProfile, &pid) < 0)
4947 {
4948 /*
4949 * Error - can't fork!
4950 */
4951
4952 cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to fork for CGI %s - %s", argv[0],
4953 strerror(errno));
4954
4955 cupsdClosePipe(fds);
4956 pid = 0;
4957 }
4958 else
4959 {
4960 /*
4961 * Fork successful - return the PID...
4962 */
4963
4964 if (con->username[0])
4965 #ifdef HAVE_GSSAPI
4966 cupsdAddCert(pid, con->username, ccache);
4967 #else
4968 cupsdAddCert(pid, con->username, NULL);
4969 #endif /* HAVE_GSSAPI */
4970
4971 cupsdLogMessage(CUPSD_LOG_DEBUG, "[CGI] %s started - PID = %d",
4972 command, pid);
4973
4974 *outfile = fds[0];
4975 close(fds[1]);
4976 }
4977
4978 return (pid);
4979 }
4980
4981
4982 /*
4983 * 'write_file()' - Send a file via HTTP.
4984 */
4985
4986 static int /* O - 0 on failure, 1 on success */
4987 write_file(cupsd_client_t *con, /* I - Client connection */
4988 http_status_t code, /* I - HTTP status */
4989 char *filename, /* I - Filename */
4990 char *type, /* I - File type */
4991 struct stat *filestats) /* O - File information */
4992 {
4993 con->file = open(filename, O_RDONLY);
4994
4995 cupsdLogMessage(CUPSD_LOG_DEBUG2, "write_file: %d file=%d", con->http.fd,
4996 con->file);
4997
4998 if (con->file < 0)
4999 return (0);
5000
5001 fcntl(con->file, F_SETFD, fcntl(con->file, F_GETFD) | FD_CLOEXEC);
5002
5003 con->pipe_pid = 0;
5004
5005 if (!cupsdSendHeader(con, code, type, CUPSD_AUTH_NONE))
5006 return (0);
5007
5008 if (httpPrintf(HTTP(con), "Last-Modified: %s\r\n",
5009 httpGetDateString(filestats->st_mtime)) < 0)
5010 return (0);
5011 if (httpPrintf(HTTP(con), "Content-Length: " CUPS_LLFMT "\r\n",
5012 CUPS_LLCAST filestats->st_size) < 0)
5013 return (0);
5014 if (httpPrintf(HTTP(con), "\r\n") < 0)
5015 return (0);
5016
5017 if (cupsdFlushHeader(con) < 0)
5018 return (0);
5019
5020 con->http.data_encoding = HTTP_ENCODE_LENGTH;
5021 con->http.data_remaining = filestats->st_size;
5022
5023 if (con->http.data_remaining <= INT_MAX)
5024 con->http._data_remaining = con->http.data_remaining;
5025 else
5026 con->http._data_remaining = INT_MAX;
5027
5028 cupsdAddSelect(con->http.fd, (cupsd_selfunc_t)cupsdReadClient,
5029 (cupsd_selfunc_t)cupsdWriteClient, con);
5030
5031 return (1);
5032 }
5033
5034
5035 /*
5036 * 'write_pipe()' - Flag that data is available on the CGI pipe.
5037 */
5038
5039 static void
5040 write_pipe(cupsd_client_t *con) /* I - Client connection */
5041 {
5042 cupsdLogMessage(CUPSD_LOG_DEBUG2, "write_pipe: CGI output on fd %d...",
5043 con->file);
5044
5045 con->file_ready = 1;
5046
5047 cupsdRemoveSelect(con->file);
5048 cupsdAddSelect(con->http.fd, NULL, (cupsd_selfunc_t)cupsdWriteClient, con);
5049 }
5050
5051
5052 /*
5053 * End of "$Id: client.c 7950 2008-09-17 00:21:59Z mike $".
5054 */