]>
Commit | Line | Data |
---|---|---|
ef416fc2 | 1 | /* |
2 | * "$Id: client.c 4912 2006-01-10 21:43:56Z mike $" | |
3 | * | |
4 | * Client routines for the Common UNIX Printing System (CUPS) scheduler. | |
5 | * | |
6 | * Copyright 1997-2006 by Easy Software Products, all rights reserved. | |
7 | * | |
8 | * These coded instructions, statements, and computer programs are the | |
9 | * property of Easy Software Products and are protected by Federal | |
10 | * copyright law. Distribution and use rights are outlined in the file | |
11 | * "LICENSE.txt" which should have been included with this file. If this | |
12 | * file is missing or damaged please contact Easy Software Products | |
13 | * at: | |
14 | * | |
15 | * Attn: CUPS Licensing Information | |
16 | * Easy Software Products | |
17 | * 44141 Airport View Drive, Suite 204 | |
18 | * Hollywood, Maryland 20636 USA | |
19 | * | |
20 | * Voice: (301) 373-9600 | |
21 | * EMail: cups-info@cups.org | |
22 | * WWW: http://www.cups.org | |
23 | * | |
24 | * Contents: | |
25 | * | |
26 | * cupsdAcceptClient() - Accept a new client. | |
27 | * cupsdCloseAllClients() - Close all remote clients immediately. | |
28 | * cupsdCloseClient() - Close a remote client. | |
29 | * cupsdEncryptClient() - Enable encryption for the client... | |
30 | * cupsdIsCGI() - Is the resource a CGI script/program? | |
31 | * cupsdReadClient() - Read data from a client. | |
32 | * cupsdSendCommand() - Send output from a command via HTTP. | |
33 | * cupsdSendError() - Send an error message via HTTP. | |
34 | * cupsdSendFile() - Send a file via HTTP. | |
35 | * cupsdSendHeader() - Send an HTTP request. | |
36 | * cupsdUpdateCGI() - Read status messages from CGI scripts and programs. | |
37 | * cupsdWriteClient() - Write data to a client as needed. | |
38 | * check_if_modified() - Decode an "If-Modified-Since" line. | |
39 | * get_cdsa_server_certs() - Convert a keychain name into the CFArrayRef | |
40 | * required by SSLSetCertificate. | |
41 | * get_file() - Get a filename and state info. | |
42 | * install_conf_file() - Install a configuration file. | |
43 | * is_path_absolute() - Is a path absolute and free of relative elements. | |
44 | * pipe_command() - Pipe the output of a command to the remote client. | |
45 | */ | |
46 | ||
47 | /* | |
48 | * Include necessary headers... | |
49 | */ | |
50 | ||
51 | #include <cups/http-private.h> | |
52 | #include "cupsd.h" | |
53 | ||
54 | #ifdef HAVE_CDSASSL | |
55 | # include <Security/Security.h> | |
56 | #endif /* HAVE_CDSASSL */ | |
57 | ||
58 | ||
59 | /* | |
60 | * Local functions... | |
61 | */ | |
62 | ||
63 | static int check_if_modified(cupsd_client_t *con, | |
64 | struct stat *filestats); | |
65 | #ifdef HAVE_CDSASSL | |
66 | static CFArrayRef get_cdsa_server_certs(void); | |
67 | #endif /* HAVE_CDSASSL */ | |
68 | static char *get_file(cupsd_client_t *con, struct stat *filestats, | |
69 | char *filename, int len); | |
70 | static http_status_t install_conf_file(cupsd_client_t *con); | |
71 | static int is_path_absolute(const char *path); | |
72 | static int pipe_command(cupsd_client_t *con, int infile, int *outfile, | |
73 | char *command, char *options, int root); | |
74 | ||
75 | ||
76 | /* | |
77 | * 'cupsdAcceptClient()' - Accept a new client. | |
78 | */ | |
79 | ||
80 | void | |
81 | cupsdAcceptClient(cupsd_listener_t *lis)/* I - Listener socket */ | |
82 | { | |
83 | int i; /* Looping var */ | |
84 | int count; /* Count of connections on a host */ | |
85 | int val; /* Parameter value */ | |
86 | cupsd_client_t *con; /* New client pointer */ | |
87 | http_addrlist_t *addrlist, /* List of adddresses for host */ | |
88 | *addr; /* Current address */ | |
89 | socklen_t addrlen; /* Length of address */ | |
90 | char *hostname; /* Hostname for address */ | |
91 | http_addr_t temp; /* Temporary address variable */ | |
92 | static time_t last_dos = 0; /* Time of last DoS attack */ | |
93 | ||
94 | ||
95 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
96 | "cupsdAcceptClient(lis=%p) %d NumClients = %d", | |
97 | lis, lis->fd, NumClients); | |
98 | ||
99 | /* | |
100 | * Make sure we don't have a full set of clients already... | |
101 | */ | |
102 | ||
103 | if (NumClients == MaxClients) | |
104 | return; | |
105 | ||
106 | /* | |
107 | * Get a pointer to the next available client... | |
108 | */ | |
109 | ||
110 | con = Clients + NumClients; | |
111 | ||
112 | memset(con, 0, sizeof(cupsd_client_t)); | |
113 | con->http.activity = time(NULL); | |
114 | con->file = -1; | |
115 | con->http.hostaddr = &(con->clientaddr); | |
116 | ||
117 | /* | |
118 | * Accept the client and get the remote address... | |
119 | */ | |
120 | ||
121 | addrlen = sizeof(http_addr_t); | |
122 | ||
123 | if ((con->http.fd = accept(lis->fd, (struct sockaddr *)con->http.hostaddr, | |
124 | &addrlen)) < 0) | |
125 | { | |
126 | cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to accept client connection - %s.", | |
127 | strerror(errno)); | |
128 | return; | |
129 | } | |
130 | ||
131 | #ifdef AF_INET6 | |
132 | if (lis->address.addr.sa_family == AF_INET6) | |
133 | { | |
134 | /* | |
135 | * Save the connected port number... | |
136 | */ | |
137 | ||
138 | con->http.hostaddr->ipv6.sin6_port = lis->address.ipv6.sin6_port; | |
139 | ||
140 | /* | |
141 | * Convert IPv4 over IPv6 addresses (::ffff:n.n.n.n) to IPv4 forms we | |
142 | * can more easily use... | |
143 | */ | |
144 | ||
145 | if (con->http.hostaddr->ipv6.sin6_addr.s6_addr32[0] == 0 && | |
146 | con->http.hostaddr->ipv6.sin6_addr.s6_addr32[1] == 0 && | |
147 | ntohl(con->http.hostaddr->ipv6.sin6_addr.s6_addr32[2]) == 0xffff) | |
148 | con->http.hostaddr->ipv6.sin6_addr.s6_addr32[2] = 0; | |
149 | } | |
150 | else | |
151 | #endif /* AF_INET6 */ | |
152 | if (lis->address.addr.sa_family == AF_INET) | |
153 | con->http.hostaddr->ipv4.sin_port = lis->address.ipv4.sin_port; | |
154 | ||
155 | /* | |
156 | * Check the number of clients on the same address... | |
157 | */ | |
158 | ||
159 | for (i = 0, count = 0; i < NumClients; i ++) | |
160 | if (httpAddrEqual(Clients[i].http.hostaddr, con->http.hostaddr)) | |
161 | { | |
162 | count ++; | |
163 | if (count >= MaxClientsPerHost) | |
164 | break; | |
165 | } | |
166 | ||
167 | if (count >= MaxClientsPerHost) | |
168 | { | |
169 | if ((time(NULL) - last_dos) >= 60) | |
170 | { | |
171 | last_dos = time(NULL); | |
172 | cupsdLogMessage(CUPSD_LOG_WARN, | |
173 | "Possible DoS attack - more than %d clients connecting from %s!", | |
174 | MaxClientsPerHost, Clients[i].http.hostname); | |
175 | } | |
176 | ||
177 | #ifdef WIN32 | |
178 | closesocket(con->http.fd); | |
179 | #else | |
180 | close(con->http.fd); | |
181 | #endif /* WIN32 */ | |
182 | ||
183 | return; | |
184 | } | |
185 | ||
186 | /* | |
187 | * Get the hostname or format the IP address as needed... | |
188 | */ | |
189 | ||
190 | if (httpAddrLocalhost(con->http.hostaddr)) | |
191 | { | |
192 | /* | |
193 | * Map accesses from the loopback interface to "localhost"... | |
194 | */ | |
195 | ||
196 | strlcpy(con->http.hostname, "localhost", sizeof(con->http.hostname)); | |
197 | hostname = con->http.hostname; | |
198 | } | |
199 | else | |
200 | { | |
201 | /* | |
202 | * Map accesses from the same host to the server name. | |
203 | */ | |
204 | ||
205 | for (addr = ServerAddrs; addr; addr = addr->next) | |
206 | if (httpAddrEqual(con->http.hostaddr, &(addr->addr))) | |
207 | break; | |
208 | ||
209 | if (addr) | |
210 | { | |
211 | strlcpy(con->http.hostname, ServerName, sizeof(con->http.hostname)); | |
212 | hostname = con->http.hostname; | |
213 | } | |
214 | else if (HostNameLookups) | |
215 | hostname = httpAddrLookup(con->http.hostaddr, con->http.hostname, | |
216 | sizeof(con->http.hostname)); | |
217 | else | |
218 | { | |
219 | hostname = NULL; | |
220 | httpAddrString(con->http.hostaddr, con->http.hostname, | |
221 | sizeof(con->http.hostname)); | |
222 | } | |
223 | } | |
224 | ||
225 | if (hostname == NULL && HostNameLookups == 2) | |
226 | { | |
227 | /* | |
228 | * Can't have an unresolved IP address with double-lookups enabled... | |
229 | */ | |
230 | ||
231 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
232 | "cupsdAcceptClient: Closing connection %d...", | |
233 | con->http.fd); | |
234 | ||
235 | #ifdef WIN32 | |
236 | closesocket(con->http.fd); | |
237 | #else | |
238 | close(con->http.fd); | |
239 | #endif /* WIN32 */ | |
240 | ||
241 | cupsdLogMessage(CUPSD_LOG_WARN, | |
242 | "Name lookup failed - connection from %s closed!", | |
243 | con->http.hostname); | |
244 | return; | |
245 | } | |
246 | ||
247 | if (HostNameLookups == 2) | |
248 | { | |
249 | /* | |
250 | * Do double lookups as needed... | |
251 | */ | |
252 | ||
253 | if ((addrlist = httpAddrGetList(con->http.hostname, AF_UNSPEC, NULL)) != NULL) | |
254 | { | |
255 | /* | |
256 | * See if the hostname maps to the same IP address... | |
257 | */ | |
258 | ||
259 | for (addr = addrlist; addr; addr = addr->next) | |
260 | if (httpAddrEqual(con->http.hostaddr, &(addr->addr))) | |
261 | break; | |
262 | } | |
263 | else | |
264 | addr = NULL; | |
265 | ||
266 | httpAddrFreeList(addrlist); | |
267 | ||
268 | if (!addr) | |
269 | { | |
270 | /* | |
271 | * Can't have a hostname that doesn't resolve to the same IP address | |
272 | * with double-lookups enabled... | |
273 | */ | |
274 | ||
275 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
276 | "cupsdAcceptClient: Closing connection %d...", | |
277 | con->http.fd); | |
278 | ||
279 | #ifdef WIN32 | |
280 | closesocket(con->http.fd); | |
281 | #else | |
282 | close(con->http.fd); | |
283 | #endif /* WIN32 */ | |
284 | ||
285 | cupsdLogMessage(CUPSD_LOG_WARN, | |
286 | "IP lookup failed - connection from %s closed!", | |
287 | con->http.hostname); | |
288 | return; | |
289 | } | |
290 | } | |
291 | ||
292 | #ifdef AF_INET6 | |
293 | if (con->http.hostaddr->addr.sa_family == AF_INET6) | |
294 | cupsdLogMessage(CUPSD_LOG_DEBUG, "cupsdAcceptClient: %d from %s:%d (IPv6)", | |
295 | con->http.fd, con->http.hostname, | |
296 | ntohs(con->http.hostaddr->ipv6.sin6_port)); | |
297 | else | |
298 | #endif /* AF_INET6 */ | |
299 | #ifdef AF_LOCAL | |
300 | if (con->http.hostaddr->addr.sa_family == AF_LOCAL) | |
301 | cupsdLogMessage(CUPSD_LOG_DEBUG, "cupsdAcceptClient: %d from %s (Domain)", | |
302 | con->http.fd, con->http.hostname); | |
303 | else | |
304 | #endif /* AF_LOCAL */ | |
305 | cupsdLogMessage(CUPSD_LOG_DEBUG, "cupsdAcceptClient: %d from %s:%d (IPv4)", | |
306 | con->http.fd, con->http.hostname, | |
307 | ntohs(con->http.hostaddr->ipv4.sin_port)); | |
308 | ||
309 | /* | |
310 | * Get the local address the client connected to... | |
311 | */ | |
312 | ||
313 | addrlen = sizeof(temp); | |
314 | if (getsockname(con->http.fd, (struct sockaddr *)&temp, &addrlen)) | |
315 | { | |
316 | cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to get local address - %s", | |
317 | strerror(errno)); | |
318 | ||
319 | strcpy(con->servername, "localhost"); | |
320 | con->serverport = LocalPort; | |
321 | } | |
322 | else | |
323 | { | |
324 | #ifdef AF_INET6 | |
325 | if (temp.addr.sa_family == AF_INET6) | |
326 | { | |
327 | httpAddrLookup(&temp, con->servername, sizeof(con->servername)); | |
328 | con->serverport = ntohs(lis->address.ipv6.sin6_port); | |
329 | } | |
330 | else | |
331 | #endif /* AF_INET6 */ | |
332 | if (temp.addr.sa_family == AF_INET) | |
333 | { | |
334 | httpAddrLookup(&temp, con->servername, sizeof(con->servername)); | |
335 | con->serverport = ntohs(lis->address.ipv4.sin_port); | |
336 | } | |
337 | else | |
338 | { | |
339 | strcpy(con->servername, "localhost"); | |
340 | con->serverport = LocalPort; | |
341 | } | |
342 | } | |
343 | ||
344 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
345 | "cupsdAcceptClient: %d connected to server on %s:%d", | |
346 | con->http.fd, con->servername, con->serverport); | |
347 | ||
348 | /* | |
349 | * Using TCP_NODELAY improves responsiveness, especially on systems | |
350 | * with a slow loopback interface... Since we write large buffers | |
351 | * when sending print files and requests, there shouldn't be any | |
352 | * performance penalty for this... | |
353 | */ | |
354 | ||
355 | val = 1; | |
356 | setsockopt(con->http.fd, IPPROTO_TCP, TCP_NODELAY, &val, sizeof(val)); | |
357 | ||
358 | /* | |
359 | * Close this file on all execs... | |
360 | */ | |
361 | ||
362 | fcntl(con->http.fd, F_SETFD, fcntl(con->http.fd, F_GETFD) | FD_CLOEXEC); | |
363 | ||
364 | /* | |
365 | * Add the socket to the select() input mask. | |
366 | */ | |
367 | ||
368 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
369 | "cupsdAcceptClient: Adding fd %d to InputSet...", | |
370 | con->http.fd); | |
371 | FD_SET(con->http.fd, InputSet); | |
372 | ||
373 | NumClients ++; | |
374 | ||
375 | /* | |
376 | * Temporarily suspend accept()'s until we lose a client... | |
377 | */ | |
378 | ||
379 | if (NumClients == MaxClients) | |
380 | cupsdPauseListening(); | |
381 | ||
382 | #ifdef HAVE_SSL | |
383 | /* | |
384 | * See if we are connecting on a secure port... | |
385 | */ | |
386 | ||
387 | if (lis->encryption == HTTP_ENCRYPT_ALWAYS) | |
388 | { | |
389 | /* | |
390 | * https connection; go secure... | |
391 | */ | |
392 | ||
393 | con->http.encryption = HTTP_ENCRYPT_ALWAYS; | |
394 | ||
395 | cupsdEncryptClient(con); | |
396 | } | |
397 | else | |
398 | con->auto_ssl = 1; | |
399 | #endif /* HAVE_SSL */ | |
400 | } | |
401 | ||
402 | ||
403 | /* | |
404 | * 'cupsdCloseAllClients()' - Close all remote clients immediately. | |
405 | */ | |
406 | ||
407 | void | |
408 | cupsdCloseAllClients(void) | |
409 | { | |
410 | while (NumClients > 0) | |
411 | cupsdCloseClient(Clients); | |
412 | } | |
413 | ||
414 | ||
415 | /* | |
416 | * 'cupsdCloseClient()' - Close a remote client. | |
417 | */ | |
418 | ||
419 | int /* O - 1 if partial close, 0 if fully closed */ | |
420 | cupsdCloseClient(cupsd_client_t *con) /* I - Client to close */ | |
421 | { | |
422 | int partial; /* Do partial close for SSL? */ | |
423 | #ifdef HAVE_LIBSSL | |
424 | SSL_CTX *context; /* Context for encryption */ | |
425 | SSL *conn; /* Connection for encryption */ | |
426 | unsigned long error; /* Error code */ | |
427 | #elif defined(HAVE_GNUTLS) | |
428 | http_tls_t *conn; /* TLS connection information */ | |
429 | int error; /* Error code */ | |
430 | gnutls_certificate_server_credentials *credentials; | |
431 | /* TLS credentials */ | |
432 | #endif /* HAVE_LIBSSL */ | |
433 | ||
434 | ||
435 | cupsdLogMessage(CUPSD_LOG_DEBUG, "cupsdCloseClient: %d", con->http.fd); | |
436 | ||
437 | /* | |
438 | * Flush pending writes before closing... | |
439 | */ | |
440 | ||
441 | httpFlushWrite(HTTP(con)); | |
442 | ||
443 | partial = 0; | |
444 | ||
445 | #ifdef HAVE_SSL | |
446 | /* | |
447 | * Shutdown encryption as needed... | |
448 | */ | |
449 | ||
450 | if (con->http.tls) | |
451 | { | |
452 | partial = 1; | |
453 | ||
454 | # ifdef HAVE_LIBSSL | |
455 | conn = (SSL *)(con->http.tls); | |
456 | context = SSL_get_SSL_CTX(conn); | |
457 | ||
458 | switch (SSL_shutdown(conn)) | |
459 | { | |
460 | case 1 : | |
461 | cupsdLogMessage(CUPSD_LOG_INFO, | |
462 | "cupsdCloseClient: SSL shutdown successful!"); | |
463 | break; | |
464 | case -1 : | |
465 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
466 | "cupsdCloseClient: Fatal error during SSL shutdown!"); | |
467 | default : | |
468 | while ((error = ERR_get_error()) != 0) | |
469 | cupsdLogMessage(CUPSD_LOG_ERROR, "cupsdCloseClient: %s", | |
470 | ERR_error_string(error, NULL)); | |
471 | break; | |
472 | } | |
473 | ||
474 | SSL_CTX_free(context); | |
475 | SSL_free(conn); | |
476 | ||
477 | # elif defined(HAVE_GNUTLS) | |
478 | conn = (http_tls_t *)(con->http.tls); | |
479 | credentials = (gnutls_certificate_server_credentials *)(conn->credentials); | |
480 | ||
481 | error = gnutls_bye(conn->session, GNUTLS_SHUT_WR); | |
482 | switch (error) | |
483 | { | |
484 | case GNUTLS_E_SUCCESS: | |
485 | cupsdLogMessage(CUPSD_LOG_INFO, | |
486 | "cupsdCloseClient: SSL shutdown successful!"); | |
487 | break; | |
488 | default: | |
489 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
490 | "cupsdCloseClient: %s", gnutls_strerror(error)); | |
491 | break; | |
492 | } | |
493 | ||
494 | gnutls_deinit(conn->session); | |
495 | gnutls_certificate_free_credentials(*credentials); | |
496 | free(credentials); | |
497 | free(conn); | |
498 | ||
499 | # elif defined(HAVE_CDSASSL) | |
500 | SSLClose((SSLContextRef)con->http.tls); | |
501 | SSLDisposeContext((SSLContextRef)con->http.tls); | |
502 | # endif /* HAVE_LIBSSL */ | |
503 | ||
504 | con->http.tls = NULL; | |
505 | } | |
506 | #endif /* HAVE_SSL */ | |
507 | ||
508 | if (con->pipe_pid != 0) | |
509 | { | |
510 | /* | |
511 | * Stop any CGI process... | |
512 | */ | |
513 | ||
514 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
515 | "cupsdCloseClient: %d Killing process ID %d...", | |
516 | con->http.fd, con->pipe_pid); | |
517 | cupsdEndProcess(con->pipe_pid, 1); | |
518 | con->pipe_pid = 0; | |
519 | } | |
520 | ||
521 | if (con->file >= 0) | |
522 | { | |
523 | if (FD_ISSET(con->file, InputSet)) | |
524 | { | |
525 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
526 | "cupsdCloseClient: %d Removing fd %d from InputSet...", | |
527 | con->http.fd, con->file); | |
528 | FD_CLR(con->file, InputSet); | |
529 | } | |
530 | ||
531 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
532 | "cupsdCloseClient: %d Closing data file %d.", | |
533 | con->http.fd, con->file); | |
534 | ||
535 | close(con->file); | |
536 | con->file = -1; | |
537 | } | |
538 | ||
539 | /* | |
540 | * Close the socket and clear the file from the input set for select()... | |
541 | */ | |
542 | ||
543 | if (con->http.fd > 0) | |
544 | { | |
545 | if (partial) | |
546 | { | |
547 | /* | |
548 | * Only do a partial close so that the encrypted client gets everything. | |
549 | */ | |
550 | ||
551 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
552 | "cupsdCloseClient: Removing fd %d from OutputSet...", | |
553 | con->http.fd); | |
554 | shutdown(con->http.fd, 0); | |
555 | FD_CLR(con->http.fd, OutputSet); | |
556 | } | |
557 | else | |
558 | { | |
559 | /* | |
560 | * Shut the socket down fully... | |
561 | */ | |
562 | ||
563 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
564 | "cupsdCloseClient: Removing fd %d from InputSet and OutputSet...", | |
565 | con->http.fd); | |
566 | close(con->http.fd); | |
567 | FD_CLR(con->http.fd, InputSet); | |
568 | FD_CLR(con->http.fd, OutputSet); | |
569 | con->http.fd = -1; | |
570 | } | |
571 | } | |
572 | ||
573 | if (!partial) | |
574 | { | |
575 | /* | |
576 | * Free memory... | |
577 | */ | |
578 | ||
579 | if (con->http.input_set) | |
580 | free(con->http.input_set); | |
581 | ||
582 | httpClearCookie(HTTP(con)); | |
583 | ||
584 | cupsdClearString(&con->filename); | |
585 | cupsdClearString(&con->command); | |
586 | cupsdClearString(&con->options); | |
587 | ||
588 | if (con->request) | |
589 | { | |
590 | ippDelete(con->request); | |
591 | con->request = NULL; | |
592 | } | |
593 | ||
594 | if (con->response) | |
595 | { | |
596 | ippDelete(con->response); | |
597 | con->response = NULL; | |
598 | } | |
599 | ||
600 | if (con->language) | |
601 | { | |
602 | cupsLangFree(con->language); | |
603 | con->language = NULL; | |
604 | } | |
605 | ||
606 | /* | |
607 | * Re-enable new client connections if we are going back under the | |
608 | * limit... | |
609 | */ | |
610 | ||
611 | if (NumClients == MaxClients) | |
612 | cupsdResumeListening(); | |
613 | ||
614 | /* | |
615 | * Compact the list of clients as necessary... | |
616 | */ | |
617 | ||
618 | NumClients --; | |
619 | ||
620 | if (con < (Clients + NumClients)) | |
621 | memmove(con, con + 1, (Clients + NumClients - con) * sizeof(cupsd_client_t)); | |
622 | } | |
623 | ||
624 | return (partial); | |
625 | } | |
626 | ||
627 | ||
628 | /* | |
629 | * 'cupsdEncryptClient()' - Enable encryption for the client... | |
630 | */ | |
631 | ||
632 | int /* O - 1 on success, 0 on error */ | |
633 | cupsdEncryptClient(cupsd_client_t *con) /* I - Client to encrypt */ | |
634 | { | |
635 | #ifdef HAVE_LIBSSL | |
636 | SSL_CTX *context; /* Context for encryption */ | |
637 | SSL *conn; /* Connection for encryption */ | |
638 | unsigned long error; /* Error code */ | |
639 | ||
640 | ||
641 | /* | |
642 | * Create the SSL context and accept the connection... | |
643 | */ | |
644 | ||
645 | context = SSL_CTX_new(SSLv23_server_method()); | |
646 | ||
647 | SSL_CTX_set_options(context, SSL_OP_NO_SSLv2); /* Only use SSLv3 or TLS */ | |
648 | SSL_CTX_use_PrivateKey_file(context, ServerKey, SSL_FILETYPE_PEM); | |
649 | SSL_CTX_use_certificate_file(context, ServerCertificate, SSL_FILETYPE_PEM); | |
650 | ||
651 | conn = SSL_new(context); | |
652 | ||
653 | SSL_set_fd(conn, con->http.fd); | |
654 | if (SSL_accept(conn) != 1) | |
655 | { | |
656 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
657 | "cupsdEncryptClient: Unable to encrypt connection from %s!", | |
658 | con->http.hostname); | |
659 | ||
660 | while ((error = ERR_get_error()) != 0) | |
661 | cupsdLogMessage(CUPSD_LOG_ERROR, "cupsdEncryptClient: %s", | |
662 | ERR_error_string(error, NULL)); | |
663 | ||
664 | SSL_CTX_free(context); | |
665 | SSL_free(conn); | |
666 | return (0); | |
667 | } | |
668 | ||
669 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
670 | "cupsdEncryptClient: %d Connection from %s now encrypted.", | |
671 | con->http.fd, con->http.hostname); | |
672 | ||
673 | con->http.tls = conn; | |
674 | return (1); | |
675 | ||
676 | #elif defined(HAVE_GNUTLS) | |
677 | http_tls_t *conn; /* TLS session object */ | |
678 | int error; /* Error code */ | |
679 | gnutls_certificate_server_credentials *credentials; | |
680 | /* TLS credentials */ | |
681 | ||
682 | /* | |
683 | * Create the SSL object and perform the SSL handshake... | |
684 | */ | |
685 | ||
686 | conn = (http_tls_t *)malloc(sizeof(gnutls_session)); | |
687 | ||
688 | if (conn == NULL) | |
689 | return (0); | |
690 | ||
691 | credentials = (gnutls_certificate_server_credentials *) | |
692 | malloc(sizeof(gnutls_certificate_server_credentials)); | |
693 | if (credentials == NULL) | |
694 | { | |
695 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
696 | "cupsdEncryptClient: Unable to encrypt connection from %s!", | |
697 | con->http.hostname); | |
698 | cupsdLogMessage(CUPSD_LOG_ERROR, "cupsdEncryptClient: %s", strerror(errno)); | |
699 | ||
700 | free(conn); | |
701 | return (0); | |
702 | } | |
703 | ||
704 | gnutls_certificate_allocate_credentials(credentials); | |
705 | gnutls_certificate_set_x509_key_file(*credentials, ServerCertificate, | |
706 | ServerKey, GNUTLS_X509_FMT_PEM); | |
707 | ||
708 | gnutls_init(&(conn->session), GNUTLS_SERVER); | |
709 | gnutls_set_default_priority(conn->session); | |
710 | gnutls_credentials_set(conn->session, GNUTLS_CRD_CERTIFICATE, *credentials); | |
711 | gnutls_transport_set_ptr(conn->session, con->http.fd); | |
712 | ||
713 | error = gnutls_handshake(conn->session); | |
714 | ||
715 | if (error != GNUTLS_E_SUCCESS) | |
716 | { | |
717 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
718 | "cupsdEncryptClient: Unable to encrypt connection from %s!", | |
719 | con->http.hostname); | |
720 | cupsdLogMessage(CUPSD_LOG_ERROR, "cupsdEncryptClient: %s", | |
721 | gnutls_strerror(error)); | |
722 | ||
723 | gnutls_deinit(conn->session); | |
724 | gnutls_certificate_free_credentials(*credentials); | |
725 | free(conn); | |
726 | free(credentials); | |
727 | return (0); | |
728 | } | |
729 | ||
730 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
731 | "cupsdEncryptClient: %d Connection from %s now encrypted.", | |
732 | con->http.fd, con->http.hostname); | |
733 | ||
734 | conn->credentials = credentials; | |
735 | con->http.tls = conn; | |
736 | return (1); | |
737 | ||
738 | #elif defined(HAVE_CDSASSL) | |
739 | OSStatus error; /* Error info */ | |
740 | SSLContextRef conn; /* New connection */ | |
741 | int allowExpired; /* Allow expired certificates? */ | |
742 | int allowAnyRoot; /* Allow any root certificate? */ | |
743 | ||
744 | ||
745 | conn = NULL; | |
746 | error = SSLNewContext(true, &conn); | |
747 | allowExpired = 1; | |
748 | allowAnyRoot = 1; | |
749 | ||
750 | if (!ServerCertificatesArray) | |
751 | ServerCertificatesArray = get_cdsa_server_certs(); | |
752 | ||
753 | if (!ServerCertificatesArray) | |
754 | { | |
755 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
756 | "EncryptClient: Could not find signing key in keychain " | |
757 | "\"%s\"", ServerCertificate); | |
758 | error = errSSLBadConfiguration; | |
759 | } | |
760 | ||
761 | if (!error) | |
762 | error = SSLSetIOFuncs(conn, _httpReadCDSA, _httpWriteCDSA); | |
763 | ||
764 | if (!error) | |
765 | error = SSLSetProtocolVersion(conn, kSSLProtocol3); | |
766 | ||
767 | if (!error) | |
768 | error = SSLSetConnection(conn, (SSLConnectionRef)con->http.fd); | |
769 | ||
770 | if (!error) | |
771 | error = SSLSetPeerDomainName(conn, ServerName, strlen(ServerName) + 1); | |
772 | ||
773 | /* have to do these options before setting server certs */ | |
774 | if (!error && allowExpired) | |
775 | error = SSLSetAllowsExpiredCerts(conn, true); | |
776 | ||
777 | if (!error && allowAnyRoot) | |
778 | error = SSLSetAllowsAnyRoot(conn, true); | |
779 | ||
780 | if (!error && ServerCertificatesArray) | |
781 | error = SSLSetCertificate(conn, ServerCertificatesArray); | |
782 | ||
783 | /* | |
784 | * Perform SSL/TLS handshake | |
785 | */ | |
786 | ||
787 | do | |
788 | { | |
789 | error = SSLHandshake(conn); | |
790 | } | |
791 | while (error == errSSLWouldBlock); | |
792 | ||
793 | if (error) | |
794 | { | |
795 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
796 | "cupsdEncryptClient: Unable to encrypt connection from %s!", | |
797 | con->http.hostname); | |
798 | ||
799 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
800 | "cupsdEncryptClient: CDSA error code is %d", (int)error); | |
801 | ||
802 | con->http.error = error; | |
803 | con->http.status = HTTP_ERROR; | |
804 | ||
805 | if (conn != NULL) | |
806 | SSLDisposeContext(conn); | |
807 | ||
808 | return (0); | |
809 | } | |
810 | ||
811 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
812 | "cupsdEncryptClient: %d Connection from %s now encrypted.", | |
813 | con->http.fd, con->http.hostname); | |
814 | ||
815 | con->http.tls = conn; | |
816 | return (1); | |
817 | ||
818 | #else | |
819 | return (0); | |
820 | #endif /* HAVE_LIBSSL */ | |
821 | } | |
822 | ||
823 | ||
824 | /* | |
825 | * 'cupsdIsCGI()' - Is the resource a CGI script/program? | |
826 | */ | |
827 | ||
828 | int /* O - 1 = CGI, 0 = file */ | |
829 | cupsdIsCGI(cupsd_client_t *con, /* I - Client connection */ | |
830 | const char *filename, /* I - Real filename */ | |
831 | struct stat *filestats, /* I - File information */ | |
832 | mime_type_t *type) /* I - MIME type */ | |
833 | { | |
834 | const char *options; /* Options on URL */ | |
835 | ||
836 | ||
837 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
838 | "cupsdIsCGI(con=%p, filename=\"%s\", filestats=%p, type=%s/%s)\n", | |
839 | con, filename, filestats, type ? type->super : "unknown", | |
840 | type ? type->type : "unknown"); | |
841 | ||
842 | /* | |
843 | * Get the options, if any... | |
844 | */ | |
845 | ||
846 | if ((options = strchr(con->uri, '?')) != NULL) | |
847 | options ++; | |
848 | ||
849 | /* | |
850 | * Check for known types... | |
851 | */ | |
852 | ||
853 | if (!type || strcasecmp(type->super, "application")) | |
854 | { | |
855 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdIsCGI: Returning 0..."); | |
856 | return (0); | |
857 | } | |
858 | ||
859 | if (!strcasecmp(type->type, "x-httpd-cgi") && | |
860 | (filestats->st_mode & 0111)) | |
861 | { | |
862 | /* | |
863 | * "application/x-httpd-cgi" is a CGI script. | |
864 | */ | |
865 | ||
866 | cupsdSetString(&con->command, filename); | |
867 | ||
868 | filename = strrchr(filename, '/') + 1; /* Filename always absolute */ | |
869 | ||
870 | if (options) | |
871 | cupsdSetStringf(&con->options, "%s %s", filename, options); | |
872 | else | |
873 | cupsdSetStringf(&con->options, "%s", filename); | |
874 | ||
875 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
876 | "cupsdIsCGI: Returning 1 with command=\"%s\" and options=\"%s\"", | |
877 | con->command, con->options); | |
878 | ||
879 | return (1); | |
880 | } | |
881 | #ifdef HAVE_JAVA | |
882 | else if (!strcasecmp(type->type, "x-httpd-java")) | |
883 | { | |
884 | /* | |
885 | * "application/x-httpd-java" is a Java servlet. | |
886 | */ | |
887 | ||
888 | cupsdSetString(&con->command, CUPS_JAVA); | |
889 | ||
890 | if (options) | |
891 | cupsdSetStringf(&con->options, "java %s %s", filename, options); | |
892 | else | |
893 | cupsdSetStringf(&con->options, "java %s", filename); | |
894 | ||
895 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
896 | "cupsdIsCGI: Returning 1 with command=\"%s\" and options=\"%s\"", | |
897 | con->command, con->options); | |
898 | ||
899 | return (1); | |
900 | } | |
901 | #endif /* HAVE_JAVA */ | |
902 | #ifdef HAVE_PERL | |
903 | else if (!strcasecmp(type->type, "x-httpd-perl")) | |
904 | { | |
905 | /* | |
906 | * "application/x-httpd-perl" is a Perl page. | |
907 | */ | |
908 | ||
909 | cupsdSetString(&con->command, CUPS_PERL); | |
910 | ||
911 | if (options) | |
912 | cupsdSetStringf(&con->options, "perl %s %s", filename, options); | |
913 | else | |
914 | cupsdSetStringf(&con->options, "perl %s", filename); | |
915 | ||
916 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
917 | "cupsdIsCGI: Returning 1 with command=\"%s\" and options=\"%s\"", | |
918 | con->command, con->options); | |
919 | ||
920 | return (1); | |
921 | } | |
922 | #endif /* HAVE_PERL */ | |
923 | #ifdef HAVE_PHP | |
924 | else if (!strcasecmp(type->type, "x-httpd-php")) | |
925 | { | |
926 | /* | |
927 | * "application/x-httpd-php" is a PHP page. | |
928 | */ | |
929 | ||
930 | cupsdSetString(&con->command, CUPS_PHP); | |
931 | ||
932 | if (options) | |
933 | cupsdSetStringf(&con->options, "php %s %s", filename, options); | |
934 | else | |
935 | cupsdSetStringf(&con->options, "php %s", filename); | |
936 | ||
937 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
938 | "cupsdIsCGI: Returning 1 with command=\"%s\" and options=\"%s\"", | |
939 | con->command, con->options); | |
940 | ||
941 | return (1); | |
942 | } | |
943 | #endif /* HAVE_PHP */ | |
944 | #ifdef HAVE_PYTHON | |
945 | else if (!strcasecmp(type->type, "x-httpd-python")) | |
946 | { | |
947 | /* | |
948 | * "application/x-httpd-python" is a Python page. | |
949 | */ | |
950 | ||
951 | cupsdSetString(&con->command, CUPS_PYTHON); | |
952 | ||
953 | if (options) | |
954 | cupsdSetStringf(&con->options, "python %s %s", filename, options); | |
955 | else | |
956 | cupsdSetStringf(&con->options, "python %s", filename); | |
957 | ||
958 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
959 | "cupsdIsCGI: Returning 1 with command=\"%s\" and options=\"%s\"", | |
960 | con->command, con->options); | |
961 | ||
962 | return (1); | |
963 | } | |
964 | #endif /* HAVE_PYTHON */ | |
965 | ||
966 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdIsCGI: Returning 0..."); | |
967 | ||
968 | return (0); | |
969 | } | |
970 | ||
971 | ||
972 | /* | |
973 | * 'cupsdReadClient()' - Read data from a client. | |
974 | */ | |
975 | ||
976 | int /* O - 1 on success, 0 on error */ | |
977 | cupsdReadClient(cupsd_client_t *con) /* I - Client to read from */ | |
978 | { | |
979 | char line[32768], /* Line from client... */ | |
980 | operation[64], /* Operation code from socket */ | |
981 | version[64], /* HTTP version number string */ | |
982 | locale[64], /* Locale */ | |
983 | *ptr; /* Pointer into strings */ | |
984 | int major, minor; /* HTTP version numbers */ | |
985 | http_status_t status; /* Transfer status */ | |
986 | ipp_state_t ipp_state; /* State of IPP transfer */ | |
987 | int bytes; /* Number of bytes to POST */ | |
988 | char *filename; /* Name of file for GET/HEAD */ | |
989 | char buf[1024]; /* Buffer for real filename */ | |
990 | struct stat filestats; /* File information */ | |
991 | mime_type_t *type; /* MIME type of file */ | |
992 | cupsd_printer_t *p; /* Printer */ | |
993 | static unsigned request_id = 0; /* Request ID for temp files */ | |
994 | ||
995 | ||
996 | status = HTTP_CONTINUE; | |
997 | ||
998 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
999 | "cupsdReadClient: %d, used=%d, file=%d state=%d", | |
1000 | con->http.fd, con->http.used, con->file, con->http.state); | |
1001 | ||
1002 | if (con->http.error) | |
1003 | { | |
1004 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdReadClient: http error seen..."); | |
1005 | return (cupsdCloseClient(con)); | |
1006 | } | |
1007 | ||
1008 | #ifdef HAVE_SSL | |
1009 | if (con->auto_ssl) | |
1010 | { | |
1011 | /* | |
1012 | * Automatically check for a SSL/TLS handshake... | |
1013 | */ | |
1014 | ||
1015 | con->auto_ssl = 0; | |
1016 | ||
1017 | if (recv(con->http.fd, buf, 1, MSG_PEEK) == 1 && | |
1018 | (!buf[0] || !strchr("DGHOPT", buf[0]))) | |
1019 | { | |
1020 | /* | |
1021 | * Encrypt this connection... | |
1022 | */ | |
1023 | ||
1024 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
1025 | "cupsdReadClient: Saw first byte %02X, auto-negotiating SSL/TLS session...", | |
1026 | buf[0] & 255); | |
1027 | ||
1028 | cupsdEncryptClient(con); | |
1029 | return (1); | |
1030 | } | |
1031 | } | |
1032 | #endif /* HAVE_SSL */ | |
1033 | ||
1034 | switch (con->http.state) | |
1035 | { | |
1036 | case HTTP_WAITING : | |
1037 | /* | |
1038 | * See if we've received a request line... | |
1039 | */ | |
1040 | ||
1041 | if (httpGets(line, sizeof(line) - 1, HTTP(con)) == NULL) | |
1042 | { | |
1043 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
1044 | "cupsdReadClient: httpGets returned EOF..."); | |
1045 | return (cupsdCloseClient(con)); | |
1046 | } | |
1047 | ||
1048 | /* | |
1049 | * Ignore blank request lines... | |
1050 | */ | |
1051 | ||
1052 | if (line[0] == '\0') | |
1053 | break; | |
1054 | ||
1055 | /* | |
1056 | * Clear other state variables... | |
1057 | */ | |
1058 | ||
1059 | httpClearFields(HTTP(con)); | |
1060 | ||
1061 | con->http.activity = time(NULL); | |
1062 | con->http.version = HTTP_1_0; | |
1063 | con->http.keep_alive = HTTP_KEEPALIVE_OFF; | |
1064 | con->http.data_encoding = HTTP_ENCODE_LENGTH; | |
1065 | con->http.data_remaining = 0; | |
1066 | con->http._data_remaining = 0; | |
1067 | con->operation = HTTP_WAITING; | |
1068 | con->bytes = 0; | |
1069 | con->file = -1; | |
1070 | con->file_ready = 0; | |
1071 | con->pipe_pid = 0; | |
1072 | con->username[0] = '\0'; | |
1073 | con->password[0] = '\0'; | |
1074 | con->uri[0] = '\0'; | |
1075 | ||
1076 | cupsdClearString(&con->command); | |
1077 | cupsdClearString(&con->options); | |
1078 | ||
1079 | if (con->language != NULL) | |
1080 | { | |
1081 | cupsLangFree(con->language); | |
1082 | con->language = NULL; | |
1083 | } | |
1084 | ||
1085 | /* | |
1086 | * Grab the request line... | |
1087 | */ | |
1088 | ||
1089 | switch (sscanf(line, "%63s%1023s%63s", operation, con->uri, version)) | |
1090 | { | |
1091 | case 1 : | |
1092 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
1093 | "Bad request line \"%s\" from %s!", line, | |
1094 | con->http.hostname); | |
1095 | cupsdSendError(con, HTTP_BAD_REQUEST); | |
1096 | return (cupsdCloseClient(con)); | |
1097 | case 2 : | |
1098 | con->http.version = HTTP_0_9; | |
1099 | break; | |
1100 | case 3 : | |
1101 | if (sscanf(version, "HTTP/%d.%d", &major, &minor) != 2) | |
1102 | { | |
1103 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
1104 | "Bad request line \"%s\" from %s!", line, | |
1105 | con->http.hostname); | |
1106 | cupsdSendError(con, HTTP_BAD_REQUEST); | |
1107 | return (cupsdCloseClient(con)); | |
1108 | } | |
1109 | ||
1110 | if (major < 2) | |
1111 | { | |
1112 | con->http.version = (http_version_t)(major * 100 + minor); | |
1113 | if (con->http.version == HTTP_1_1 && KeepAlive) | |
1114 | con->http.keep_alive = HTTP_KEEPALIVE_ON; | |
1115 | else | |
1116 | con->http.keep_alive = HTTP_KEEPALIVE_OFF; | |
1117 | } | |
1118 | else | |
1119 | { | |
1120 | cupsdSendError(con, HTTP_NOT_SUPPORTED); | |
1121 | return (cupsdCloseClient(con)); | |
1122 | } | |
1123 | break; | |
1124 | } | |
1125 | ||
1126 | /* | |
1127 | * Handle full URLs in the request line... | |
1128 | */ | |
1129 | ||
1130 | if (strcmp(con->uri, "*")) | |
1131 | { | |
1132 | char method[HTTP_MAX_URI], /* Method/scheme */ | |
1133 | userpass[HTTP_MAX_URI], /* Username:password */ | |
1134 | hostname[HTTP_MAX_URI], /* Hostname */ | |
1135 | resource[HTTP_MAX_URI]; /* Resource path */ | |
1136 | int port; /* Port number */ | |
1137 | ||
1138 | ||
1139 | /* | |
1140 | * Separate the URI into its components... | |
1141 | */ | |
1142 | ||
1143 | httpSeparateURI(con->uri, method, sizeof(method), | |
1144 | userpass, sizeof(userpass), | |
1145 | hostname, sizeof(hostname), &port, | |
1146 | resource, sizeof(resource)); | |
1147 | ||
1148 | /* | |
1149 | * Only allow URIs with the servername, localhost, or an IP | |
1150 | * address... | |
1151 | */ | |
1152 | ||
1153 | if (strcmp(method, "file") && | |
1154 | strcasecmp(hostname, ServerName) && | |
1155 | strcasecmp(hostname, "localhost") && | |
1156 | !isdigit(hostname[0])) | |
1157 | { | |
1158 | /* | |
1159 | * Nope, we don't do proxies... | |
1160 | */ | |
1161 | ||
1162 | cupsdLogMessage(CUPSD_LOG_ERROR, "Bad URI \"%s\" in request!", | |
1163 | con->uri); | |
1164 | cupsdSendError(con, HTTP_METHOD_NOT_ALLOWED); | |
1165 | return (cupsdCloseClient(con)); | |
1166 | } | |
1167 | ||
1168 | /* | |
1169 | * Copy the resource portion back into the URI; both resource and | |
1170 | * con->uri are HTTP_MAX_URI bytes in size... | |
1171 | */ | |
1172 | ||
1173 | strcpy(con->uri, resource); | |
1174 | } | |
1175 | ||
1176 | /* | |
1177 | * Process the request... | |
1178 | */ | |
1179 | ||
1180 | if (!strcmp(operation, "GET")) | |
1181 | con->http.state = HTTP_GET; | |
1182 | else if (!strcmp(operation, "PUT")) | |
1183 | con->http.state = HTTP_PUT; | |
1184 | else if (!strcmp(operation, "POST")) | |
1185 | con->http.state = HTTP_POST; | |
1186 | else if (!strcmp(operation, "DELETE")) | |
1187 | con->http.state = HTTP_DELETE; | |
1188 | else if (!strcmp(operation, "TRACE")) | |
1189 | con->http.state = HTTP_TRACE; | |
1190 | else if (!strcmp(operation, "OPTIONS")) | |
1191 | con->http.state = HTTP_OPTIONS; | |
1192 | else if (!strcmp(operation, "HEAD")) | |
1193 | con->http.state = HTTP_HEAD; | |
1194 | else | |
1195 | { | |
1196 | cupsdLogMessage(CUPSD_LOG_ERROR, "Bad operation \"%s\"!", operation); | |
1197 | cupsdSendError(con, HTTP_BAD_REQUEST); | |
1198 | return (cupsdCloseClient(con)); | |
1199 | } | |
1200 | ||
1201 | con->start = time(NULL); | |
1202 | con->operation = con->http.state; | |
1203 | ||
1204 | cupsdLogMessage(CUPSD_LOG_DEBUG, "cupsdReadClient: %d %s %s HTTP/%d.%d", con->http.fd, | |
1205 | operation, con->uri, | |
1206 | con->http.version / 100, con->http.version % 100); | |
1207 | ||
1208 | con->http.status = HTTP_OK; | |
1209 | ||
1210 | case HTTP_OPTIONS : | |
1211 | case HTTP_DELETE : | |
1212 | case HTTP_GET : | |
1213 | case HTTP_HEAD : | |
1214 | case HTTP_POST : | |
1215 | case HTTP_PUT : | |
1216 | case HTTP_TRACE : | |
1217 | /* | |
1218 | * Parse incoming parameters until the status changes... | |
1219 | */ | |
1220 | ||
1221 | status = httpUpdate(HTTP(con)); | |
1222 | ||
1223 | if (status != HTTP_OK && status != HTTP_CONTINUE) | |
1224 | { | |
1225 | cupsdSendError(con, HTTP_BAD_REQUEST); | |
1226 | return (cupsdCloseClient(con)); | |
1227 | } | |
1228 | break; | |
1229 | ||
1230 | default : | |
1231 | break; /* Anti-compiler-warning-code */ | |
1232 | } | |
1233 | ||
1234 | /* | |
1235 | * Handle new transfers... | |
1236 | */ | |
1237 | ||
1238 | if (status == HTTP_OK) | |
1239 | { | |
1240 | if (con->http.fields[HTTP_FIELD_ACCEPT_LANGUAGE][0]) | |
1241 | { | |
1242 | /* | |
1243 | * Figure out the locale from the Accept-Language and Content-Type | |
1244 | * fields... | |
1245 | */ | |
1246 | ||
1247 | if ((ptr = strchr(con->http.fields[HTTP_FIELD_ACCEPT_LANGUAGE], ',')) != NULL) | |
1248 | *ptr = '\0'; | |
1249 | ||
1250 | if ((ptr = strchr(con->http.fields[HTTP_FIELD_ACCEPT_LANGUAGE], ';')) != NULL) | |
1251 | *ptr = '\0'; | |
1252 | ||
1253 | if ((ptr = strstr(con->http.fields[HTTP_FIELD_CONTENT_TYPE], "charset=")) != NULL) | |
1254 | { | |
1255 | /* | |
1256 | * Combine language and charset, and trim any extra params in the | |
1257 | * content-type. | |
1258 | */ | |
1259 | ||
1260 | snprintf(locale, sizeof(locale), "%s.%s", | |
1261 | con->http.fields[HTTP_FIELD_ACCEPT_LANGUAGE], ptr + 8); | |
1262 | ||
1263 | if ((ptr = strchr(locale, ',')) != NULL) | |
1264 | *ptr = '\0'; | |
1265 | } | |
1266 | else | |
1267 | snprintf(locale, sizeof(locale), "%s.%s", | |
1268 | con->http.fields[HTTP_FIELD_ACCEPT_LANGUAGE], DefaultCharset); | |
1269 | ||
1270 | con->language = cupsLangGet(locale); | |
1271 | } | |
1272 | else | |
1273 | con->language = cupsLangGet(DefaultLocale); | |
1274 | ||
1275 | cupsdAuthorize(con); | |
1276 | ||
1277 | if (!strncmp(con->http.fields[HTTP_FIELD_CONNECTION], "Keep-Alive", 10) && | |
1278 | KeepAlive) | |
1279 | con->http.keep_alive = HTTP_KEEPALIVE_ON; | |
1280 | ||
1281 | if (!con->http.fields[HTTP_FIELD_HOST][0] && | |
1282 | con->http.version >= HTTP_1_1) | |
1283 | { | |
1284 | /* | |
1285 | * HTTP/1.1 and higher require the "Host:" field... | |
1286 | */ | |
1287 | ||
1288 | if (!cupsdSendError(con, HTTP_BAD_REQUEST)) | |
1289 | return (cupsdCloseClient(con)); | |
1290 | } | |
1291 | else if (con->operation == HTTP_OPTIONS) | |
1292 | { | |
1293 | /* | |
1294 | * Do OPTIONS command... | |
1295 | */ | |
1296 | ||
1297 | if (con->best && con->best->type != AUTH_NONE) | |
1298 | { | |
1299 | if (!cupsdSendHeader(con, HTTP_UNAUTHORIZED, NULL)) | |
1300 | return (cupsdCloseClient(con)); | |
1301 | } | |
1302 | ||
1303 | if (!strcasecmp(con->http.fields[HTTP_FIELD_CONNECTION], "Upgrade") && | |
1304 | con->http.tls == NULL) | |
1305 | { | |
1306 | #ifdef HAVE_SSL | |
1307 | /* | |
1308 | * Do encryption stuff... | |
1309 | */ | |
1310 | ||
1311 | if (!cupsdSendHeader(con, HTTP_SWITCHING_PROTOCOLS, NULL)) | |
1312 | return (cupsdCloseClient(con)); | |
1313 | ||
1314 | httpPrintf(HTTP(con), "Connection: Upgrade\r\n"); | |
1315 | httpPrintf(HTTP(con), "Upgrade: TLS/1.0,HTTP/1.1\r\n"); | |
1316 | httpPrintf(HTTP(con), "Content-Length: 0\r\n"); | |
1317 | httpPrintf(HTTP(con), "\r\n"); | |
1318 | ||
1319 | cupsdEncryptClient(con); | |
1320 | #else | |
1321 | if (!cupsdSendError(con, HTTP_NOT_IMPLEMENTED)) | |
1322 | return (cupsdCloseClient(con)); | |
1323 | #endif /* HAVE_SSL */ | |
1324 | } | |
1325 | ||
1326 | if (con->http.expect) | |
1327 | { | |
1328 | /**** TODO: send expected header ****/ | |
1329 | } | |
1330 | ||
1331 | if (!cupsdSendHeader(con, HTTP_OK, NULL)) | |
1332 | return (cupsdCloseClient(con)); | |
1333 | ||
1334 | httpPrintf(HTTP(con), "Allow: GET, HEAD, OPTIONS, POST, PUT\r\n"); | |
1335 | httpPrintf(HTTP(con), "Content-Length: 0\r\n"); | |
1336 | httpPrintf(HTTP(con), "\r\n"); | |
1337 | } | |
1338 | else if (!is_path_absolute(con->uri)) | |
1339 | { | |
1340 | /* | |
1341 | * Protect against malicious users! | |
1342 | */ | |
1343 | ||
1344 | if (!cupsdSendError(con, HTTP_FORBIDDEN)) | |
1345 | return (cupsdCloseClient(con)); | |
1346 | } | |
1347 | else | |
1348 | { | |
1349 | if (!strcasecmp(con->http.fields[HTTP_FIELD_CONNECTION], "Upgrade") && | |
1350 | con->http.tls == NULL) | |
1351 | { | |
1352 | #ifdef HAVE_SSL | |
1353 | /* | |
1354 | * Do encryption stuff... | |
1355 | */ | |
1356 | ||
1357 | if (!cupsdSendHeader(con, HTTP_SWITCHING_PROTOCOLS, NULL)) | |
1358 | return (cupsdCloseClient(con)); | |
1359 | ||
1360 | httpPrintf(HTTP(con), "Connection: Upgrade\r\n"); | |
1361 | httpPrintf(HTTP(con), "Upgrade: TLS/1.0,HTTP/1.1\r\n"); | |
1362 | httpPrintf(HTTP(con), "Content-Length: 0\r\n"); | |
1363 | httpPrintf(HTTP(con), "\r\n"); | |
1364 | ||
1365 | cupsdEncryptClient(con); | |
1366 | #else | |
1367 | if (!cupsdSendError(con, HTTP_NOT_IMPLEMENTED)) | |
1368 | return (cupsdCloseClient(con)); | |
1369 | #endif /* HAVE_SSL */ | |
1370 | } | |
1371 | ||
1372 | if ((status = cupsdIsAuthorized(con, NULL)) != HTTP_OK) | |
1373 | { | |
1374 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
1375 | "cupsdReadClient: Unauthorized request for %s...\n", | |
1376 | con->uri); | |
1377 | cupsdSendError(con, status); | |
1378 | return (cupsdCloseClient(con)); | |
1379 | } | |
1380 | ||
1381 | if (con->http.expect) | |
1382 | { | |
1383 | /**** TODO: send expected header ****/ | |
1384 | } | |
1385 | ||
1386 | switch (con->http.state) | |
1387 | { | |
1388 | case HTTP_GET_SEND : | |
1389 | if (!strncmp(con->uri, "/printers/", 10) && | |
1390 | !strcmp(con->uri + strlen(con->uri) - 4, ".ppd")) | |
1391 | { | |
1392 | /* | |
1393 | * Send PPD file - get the real printer name since printer | |
1394 | * names are not case sensitive but filenames can be... | |
1395 | */ | |
1396 | ||
1397 | con->uri[strlen(con->uri) - 4] = '\0'; /* Drop ".ppd" */ | |
1398 | ||
1399 | if ((p = cupsdFindPrinter(con->uri + 10)) != NULL) | |
1400 | snprintf(con->uri, sizeof(con->uri), "/ppd/%s.ppd", p->name); | |
1401 | else | |
1402 | { | |
1403 | if (!cupsdSendError(con, HTTP_NOT_FOUND)) | |
1404 | return (cupsdCloseClient(con)); | |
1405 | ||
1406 | break; | |
1407 | } | |
1408 | } | |
1409 | ||
1410 | if ((!strncmp(con->uri, "/admin", 6) && | |
1411 | strncmp(con->uri, "/admin/conf/", 12) && | |
1412 | strncmp(con->uri, "/admin/log/", 11)) || | |
1413 | !strncmp(con->uri, "/printers", 9) || | |
1414 | !strncmp(con->uri, "/classes", 8) || | |
1415 | !strncmp(con->uri, "/help", 5) || | |
1416 | !strncmp(con->uri, "/jobs", 5)) | |
1417 | { | |
1418 | /* | |
1419 | * Send CGI output... | |
1420 | */ | |
1421 | ||
1422 | if (!strncmp(con->uri, "/admin", 6)) | |
1423 | { | |
1424 | cupsdSetStringf(&con->command, "%s/cgi-bin/admin.cgi", | |
1425 | ServerBin); | |
1426 | ||
1427 | if ((ptr = strchr(con->uri + 6, '?')) != NULL) | |
1428 | cupsdSetStringf(&con->options, "admin%s", ptr); | |
1429 | else | |
1430 | cupsdSetString(&con->options, "admin"); | |
1431 | } | |
1432 | else if (!strncmp(con->uri, "/printers", 9)) | |
1433 | { | |
1434 | cupsdSetStringf(&con->command, "%s/cgi-bin/printers.cgi", | |
1435 | ServerBin); | |
1436 | cupsdSetString(&con->options, con->uri + 9); | |
1437 | } | |
1438 | else if (!strncmp(con->uri, "/classes", 8)) | |
1439 | { | |
1440 | cupsdSetStringf(&con->command, "%s/cgi-bin/classes.cgi", | |
1441 | ServerBin); | |
1442 | cupsdSetString(&con->options, con->uri + 8); | |
1443 | } | |
1444 | else if (!strncmp(con->uri, "/jobs", 5)) | |
1445 | { | |
1446 | cupsdSetStringf(&con->command, "%s/cgi-bin/jobs.cgi", | |
1447 | ServerBin); | |
1448 | cupsdSetString(&con->options, con->uri + 5); | |
1449 | } | |
1450 | else | |
1451 | { | |
1452 | cupsdSetStringf(&con->command, "%s/cgi-bin/help.cgi", | |
1453 | ServerBin); | |
1454 | cupsdSetString(&con->options, con->uri + 5); | |
1455 | } | |
1456 | ||
1457 | if (con->options[0] == '/') | |
1458 | _cups_strcpy(con->options, con->options + 1); | |
1459 | ||
1460 | if (!cupsdSendCommand(con, con->command, con->options, 0)) | |
1461 | { | |
1462 | if (!cupsdSendError(con, HTTP_NOT_FOUND)) | |
1463 | return (cupsdCloseClient(con)); | |
1464 | } | |
1465 | else | |
1466 | cupsdLogRequest(con, HTTP_OK); | |
1467 | ||
1468 | if (con->http.version <= HTTP_1_0) | |
1469 | con->http.keep_alive = HTTP_KEEPALIVE_OFF; | |
1470 | } | |
1471 | else if ((!strncmp(con->uri, "/admin/conf/", 12) && | |
1472 | (strchr(con->uri + 12, '/') || | |
1473 | strlen(con->uri) == 12)) || | |
1474 | (!strncmp(con->uri, "/admin/log/", 11) && | |
1475 | (strchr(con->uri + 11, '/') || | |
1476 | strlen(con->uri) == 11))) | |
1477 | { | |
1478 | /* | |
1479 | * GET can only be done to configuration files under | |
1480 | * /admin/conf... | |
1481 | */ | |
1482 | ||
1483 | if (!cupsdSendError(con, HTTP_FORBIDDEN)) | |
1484 | return (cupsdCloseClient(con)); | |
1485 | ||
1486 | break; | |
1487 | } | |
1488 | else | |
1489 | { | |
1490 | /* | |
1491 | * Serve a file... | |
1492 | */ | |
1493 | ||
1494 | if ((filename = get_file(con, &filestats, buf, | |
1495 | sizeof(buf))) == NULL) | |
1496 | { | |
1497 | if (!cupsdSendError(con, HTTP_NOT_FOUND)) | |
1498 | return (cupsdCloseClient(con)); | |
1499 | ||
1500 | break; | |
1501 | } | |
1502 | ||
1503 | type = mimeFileType(MimeDatabase, filename, NULL); | |
1504 | ||
1505 | if (cupsdIsCGI(con, filename, &filestats, type)) | |
1506 | { | |
1507 | /* | |
1508 | * Note: con->command and con->options were set by | |
1509 | * cupsdIsCGI()... | |
1510 | */ | |
1511 | ||
1512 | if (!cupsdSendCommand(con, con->command, con->options, 0)) | |
1513 | { | |
1514 | if (!cupsdSendError(con, HTTP_NOT_FOUND)) | |
1515 | return (cupsdCloseClient(con)); | |
1516 | } | |
1517 | else | |
1518 | cupsdLogRequest(con, HTTP_OK); | |
1519 | ||
1520 | if (con->http.version <= HTTP_1_0) | |
1521 | con->http.keep_alive = HTTP_KEEPALIVE_OFF; | |
1522 | break; | |
1523 | } | |
1524 | ||
1525 | if (!check_if_modified(con, &filestats)) | |
1526 | { | |
1527 | if (!cupsdSendError(con, HTTP_NOT_MODIFIED)) | |
1528 | return (cupsdCloseClient(con)); | |
1529 | } | |
1530 | else | |
1531 | { | |
1532 | if (type == NULL) | |
1533 | strcpy(line, "text/plain"); | |
1534 | else | |
1535 | snprintf(line, sizeof(line), "%s/%s", type->super, type->type); | |
1536 | ||
1537 | if (!cupsdSendFile(con, HTTP_OK, filename, line, &filestats)) | |
1538 | return (cupsdCloseClient(con)); | |
1539 | } | |
1540 | } | |
1541 | break; | |
1542 | ||
1543 | case HTTP_POST_RECV : | |
1544 | /* | |
1545 | * See if the POST request includes a Content-Length field, and if | |
1546 | * so check the length against any limits that are set... | |
1547 | */ | |
1548 | ||
1549 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "POST %s", con->uri); | |
1550 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "CONTENT_TYPE = %s", | |
1551 | con->http.fields[HTTP_FIELD_CONTENT_TYPE]); | |
1552 | ||
1553 | if (con->http.fields[HTTP_FIELD_CONTENT_LENGTH][0] && | |
1554 | MaxRequestSize > 0 && | |
1555 | con->http.data_remaining > MaxRequestSize) | |
1556 | { | |
1557 | /* | |
1558 | * Request too large... | |
1559 | */ | |
1560 | ||
1561 | if (!cupsdSendError(con, HTTP_REQUEST_TOO_LARGE)) | |
1562 | return (cupsdCloseClient(con)); | |
1563 | ||
1564 | break; | |
1565 | } | |
1566 | else if (con->http.data_remaining < 0) | |
1567 | { | |
1568 | /* | |
1569 | * Negative content lengths are invalid! | |
1570 | */ | |
1571 | ||
1572 | if (!cupsdSendError(con, HTTP_BAD_REQUEST)) | |
1573 | return (cupsdCloseClient(con)); | |
1574 | ||
1575 | break; | |
1576 | } | |
1577 | ||
1578 | /* | |
1579 | * See what kind of POST request this is; for IPP requests the | |
1580 | * content-type field will be "application/ipp"... | |
1581 | */ | |
1582 | ||
1583 | if (!strcmp(con->http.fields[HTTP_FIELD_CONTENT_TYPE], | |
1584 | "application/ipp")) | |
1585 | con->request = ippNew(); | |
1586 | else if ((!strncmp(con->uri, "/admin", 6) && | |
1587 | strncmp(con->uri, "/admin/conf/", 12) && | |
1588 | strncmp(con->uri, "/admin/log/", 11)) || | |
1589 | !strncmp(con->uri, "/printers", 9) || | |
1590 | !strncmp(con->uri, "/classes", 8) || | |
1591 | !strncmp(con->uri, "/help", 5) || | |
1592 | !strncmp(con->uri, "/jobs", 5)) | |
1593 | { | |
1594 | /* | |
1595 | * CGI request... | |
1596 | */ | |
1597 | ||
1598 | if (!strncmp(con->uri, "/admin", 6)) | |
1599 | { | |
1600 | cupsdSetStringf(&con->command, "%s/cgi-bin/admin.cgi", | |
1601 | ServerBin); | |
1602 | ||
1603 | if ((ptr = strchr(con->uri + 6, '?')) != NULL) | |
1604 | cupsdSetStringf(&con->options, "admin%s", ptr); | |
1605 | else | |
1606 | cupsdSetString(&con->options, "admin"); | |
1607 | } | |
1608 | else if (!strncmp(con->uri, "/printers", 9)) | |
1609 | { | |
1610 | cupsdSetStringf(&con->command, "%s/cgi-bin/printers.cgi", | |
1611 | ServerBin); | |
1612 | cupsdSetString(&con->options, con->uri + 9); | |
1613 | } | |
1614 | else if (!strncmp(con->uri, "/classes", 8)) | |
1615 | { | |
1616 | cupsdSetStringf(&con->command, "%s/cgi-bin/classes.cgi", | |
1617 | ServerBin); | |
1618 | cupsdSetString(&con->options, con->uri + 8); | |
1619 | } | |
1620 | else if (!strncmp(con->uri, "/jobs", 5)) | |
1621 | { | |
1622 | cupsdSetStringf(&con->command, "%s/cgi-bin/jobs.cgi", | |
1623 | ServerBin); | |
1624 | cupsdSetString(&con->options, con->uri + 5); | |
1625 | } | |
1626 | else | |
1627 | { | |
1628 | cupsdSetStringf(&con->command, "%s/cgi-bin/help.cgi", | |
1629 | ServerBin); | |
1630 | cupsdSetString(&con->options, con->uri + 5); | |
1631 | } | |
1632 | ||
1633 | if (con->options[0] == '/') | |
1634 | _cups_strcpy(con->options, con->options + 1); | |
1635 | ||
1636 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
1637 | "cupsdReadClient: %d command=\"%s\", options = \"%s\"", | |
1638 | con->http.fd, con->command, con->options); | |
1639 | ||
1640 | if (con->http.version <= HTTP_1_0) | |
1641 | con->http.keep_alive = HTTP_KEEPALIVE_OFF; | |
1642 | } | |
1643 | else | |
1644 | { | |
1645 | /* | |
1646 | * POST to a file... | |
1647 | */ | |
1648 | ||
1649 | if ((filename = get_file(con, &filestats, buf, | |
1650 | sizeof(buf))) == NULL) | |
1651 | { | |
1652 | if (!cupsdSendError(con, HTTP_NOT_FOUND)) | |
1653 | return (cupsdCloseClient(con)); | |
1654 | ||
1655 | break; | |
1656 | } | |
1657 | ||
1658 | type = mimeFileType(MimeDatabase, filename, NULL); | |
1659 | ||
1660 | if (!cupsdIsCGI(con, filename, &filestats, type)) | |
1661 | { | |
1662 | /* | |
1663 | * Only POST to CGI's... | |
1664 | */ | |
1665 | ||
1666 | if (!cupsdSendError(con, HTTP_UNAUTHORIZED)) | |
1667 | return (cupsdCloseClient(con)); | |
1668 | } | |
1669 | } | |
1670 | break; | |
1671 | ||
1672 | case HTTP_PUT_RECV : | |
1673 | /* | |
1674 | * Validate the resource name... | |
1675 | */ | |
1676 | ||
1677 | if (strncmp(con->uri, "/admin/conf/", 12) || | |
1678 | strchr(con->uri + 12, '/') || | |
1679 | strlen(con->uri) == 12) | |
1680 | { | |
1681 | /* | |
1682 | * PUT can only be done to configuration files under | |
1683 | * /admin/conf... | |
1684 | */ | |
1685 | ||
1686 | if (!cupsdSendError(con, HTTP_FORBIDDEN)) | |
1687 | return (cupsdCloseClient(con)); | |
1688 | ||
1689 | break; | |
1690 | } | |
1691 | ||
1692 | /* | |
1693 | * See if the PUT request includes a Content-Length field, and if | |
1694 | * so check the length against any limits that are set... | |
1695 | */ | |
1696 | ||
1697 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "PUT %s", con->uri); | |
1698 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "CONTENT_TYPE = %s", | |
1699 | con->http.fields[HTTP_FIELD_CONTENT_TYPE]); | |
1700 | ||
1701 | if (con->http.fields[HTTP_FIELD_CONTENT_LENGTH][0] && | |
1702 | MaxRequestSize > 0 && | |
1703 | con->http.data_remaining > MaxRequestSize) | |
1704 | { | |
1705 | /* | |
1706 | * Request too large... | |
1707 | */ | |
1708 | ||
1709 | if (!cupsdSendError(con, HTTP_REQUEST_TOO_LARGE)) | |
1710 | return (cupsdCloseClient(con)); | |
1711 | ||
1712 | break; | |
1713 | } | |
1714 | else if (con->http.data_remaining < 0) | |
1715 | { | |
1716 | /* | |
1717 | * Negative content lengths are invalid! | |
1718 | */ | |
1719 | ||
1720 | if (!cupsdSendError(con, HTTP_BAD_REQUEST)) | |
1721 | return (cupsdCloseClient(con)); | |
1722 | ||
1723 | break; | |
1724 | } | |
1725 | ||
1726 | /* | |
1727 | * Open a temporary file to hold the request... | |
1728 | */ | |
1729 | ||
1730 | cupsdSetStringf(&con->filename, "%s/%08x", RequestRoot, | |
1731 | request_id ++); | |
1732 | con->file = open(con->filename, O_WRONLY | O_CREAT | O_TRUNC, 0640); | |
1733 | ||
1734 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
1735 | "cupsdReadClient: %d REQUEST %s=%d", con->http.fd, | |
1736 | con->filename, con->file); | |
1737 | ||
1738 | if (con->file < 0) | |
1739 | { | |
1740 | if (!cupsdSendError(con, HTTP_REQUEST_TOO_LARGE)) | |
1741 | return (cupsdCloseClient(con)); | |
1742 | } | |
1743 | ||
1744 | fchmod(con->file, 0640); | |
1745 | fchown(con->file, RunUser, Group); | |
1746 | fcntl(con->file, F_SETFD, fcntl(con->file, F_GETFD) | FD_CLOEXEC); | |
1747 | break; | |
1748 | ||
1749 | case HTTP_DELETE : | |
1750 | case HTTP_TRACE : | |
1751 | cupsdSendError(con, HTTP_NOT_IMPLEMENTED); | |
1752 | return (cupsdCloseClient(con)); | |
1753 | ||
1754 | case HTTP_HEAD : | |
1755 | if (!strncmp(con->uri, "/printers/", 10) && | |
1756 | !strcmp(con->uri + strlen(con->uri) - 4, ".ppd")) | |
1757 | { | |
1758 | /* | |
1759 | * Send PPD file - get the real printer name since printer | |
1760 | * names are not case sensitive but filenames can be... | |
1761 | */ | |
1762 | ||
1763 | con->uri[strlen(con->uri) - 4] = '\0'; /* Drop ".ppd" */ | |
1764 | ||
1765 | if ((p = cupsdFindPrinter(con->uri + 10)) != NULL) | |
1766 | snprintf(con->uri, sizeof(con->uri), "/ppd/%s.ppd", p->name); | |
1767 | else | |
1768 | { | |
1769 | if (!cupsdSendError(con, HTTP_NOT_FOUND)) | |
1770 | return (cupsdCloseClient(con)); | |
1771 | ||
1772 | break; | |
1773 | } | |
1774 | } | |
1775 | ||
1776 | if ((!strncmp(con->uri, "/admin", 6) && | |
1777 | strncmp(con->uri, "/admin/conf/", 12) && | |
1778 | strncmp(con->uri, "/admin/log/", 11)) || | |
1779 | !strncmp(con->uri, "/printers", 9) || | |
1780 | !strncmp(con->uri, "/classes", 8) || | |
1781 | !strncmp(con->uri, "/help", 5) || | |
1782 | !strncmp(con->uri, "/jobs", 5)) | |
1783 | { | |
1784 | /* | |
1785 | * CGI output... | |
1786 | */ | |
1787 | ||
1788 | if (!cupsdSendHeader(con, HTTP_OK, "text/html")) | |
1789 | return (cupsdCloseClient(con)); | |
1790 | ||
1791 | if (httpPrintf(HTTP(con), "\r\n") < 0) | |
1792 | return (cupsdCloseClient(con)); | |
1793 | ||
1794 | cupsdLogRequest(con, HTTP_OK); | |
1795 | } | |
1796 | else if ((!strncmp(con->uri, "/admin/conf/", 12) && | |
1797 | (strchr(con->uri + 12, '/') || | |
1798 | strlen(con->uri) == 12)) || | |
1799 | (!strncmp(con->uri, "/admin/log/", 11) && | |
1800 | (strchr(con->uri + 11, '/') || | |
1801 | strlen(con->uri) == 11))) | |
1802 | { | |
1803 | /* | |
1804 | * HEAD can only be done to configuration files under | |
1805 | * /admin/conf... | |
1806 | */ | |
1807 | ||
1808 | if (!cupsdSendError(con, HTTP_FORBIDDEN)) | |
1809 | return (cupsdCloseClient(con)); | |
1810 | ||
1811 | break; | |
1812 | } | |
1813 | else if ((filename = get_file(con, &filestats, buf, | |
1814 | sizeof(buf))) == NULL) | |
1815 | { | |
1816 | if (!cupsdSendHeader(con, HTTP_NOT_FOUND, "text/html")) | |
1817 | return (cupsdCloseClient(con)); | |
1818 | ||
1819 | cupsdLogRequest(con, HTTP_NOT_FOUND); | |
1820 | } | |
1821 | else if (!check_if_modified(con, &filestats)) | |
1822 | { | |
1823 | if (!cupsdSendError(con, HTTP_NOT_MODIFIED)) | |
1824 | return (cupsdCloseClient(con)); | |
1825 | ||
1826 | cupsdLogRequest(con, HTTP_NOT_MODIFIED); | |
1827 | } | |
1828 | else | |
1829 | { | |
1830 | /* | |
1831 | * Serve a file... | |
1832 | */ | |
1833 | ||
1834 | type = mimeFileType(MimeDatabase, filename, NULL); | |
1835 | if (type == NULL) | |
1836 | strcpy(line, "text/plain"); | |
1837 | else | |
1838 | snprintf(line, sizeof(line), "%s/%s", type->super, type->type); | |
1839 | ||
1840 | if (!cupsdSendHeader(con, HTTP_OK, line)) | |
1841 | return (cupsdCloseClient(con)); | |
1842 | ||
1843 | if (httpPrintf(HTTP(con), "Last-Modified: %s\r\n", | |
1844 | httpGetDateString(filestats.st_mtime)) < 0) | |
1845 | return (cupsdCloseClient(con)); | |
1846 | ||
1847 | if (httpPrintf(HTTP(con), "Content-Length: %lu\r\n", | |
1848 | (unsigned long)filestats.st_size) < 0) | |
1849 | return (cupsdCloseClient(con)); | |
1850 | ||
1851 | cupsdLogRequest(con, HTTP_OK); | |
1852 | } | |
1853 | ||
1854 | if (httpPrintf(HTTP(con), "\r\n") < 0) | |
1855 | return (cupsdCloseClient(con)); | |
1856 | ||
1857 | con->http.state = HTTP_WAITING; | |
1858 | break; | |
1859 | ||
1860 | default : | |
1861 | break; /* Anti-compiler-warning-code */ | |
1862 | } | |
1863 | } | |
1864 | } | |
1865 | ||
1866 | /* | |
1867 | * Handle any incoming data... | |
1868 | */ | |
1869 | ||
1870 | switch (con->http.state) | |
1871 | { | |
1872 | case HTTP_PUT_RECV : | |
1873 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
1874 | "cupsdReadClient: %d con->data_encoding=HTTP_ENCODE_%s, " | |
1875 | "con->data_remaining=" CUPS_LLFMT ", con->file=%d", | |
1876 | con->http.fd, | |
1877 | con->http.data_encoding == HTTP_ENCODE_CHUNKED ? | |
1878 | "CHUNKED" : "LENGTH", | |
1879 | CUPS_LLCAST con->http.data_remaining, con->file); | |
1880 | ||
1881 | if ((bytes = httpRead(HTTP(con), line, sizeof(line))) < 0) | |
1882 | return (cupsdCloseClient(con)); | |
1883 | else if (bytes > 0) | |
1884 | { | |
1885 | con->bytes += bytes; | |
1886 | ||
1887 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
1888 | "cupsdReadClient: %d writing %d bytes to %d", | |
1889 | con->http.fd, bytes, con->file); | |
1890 | ||
1891 | if (write(con->file, line, bytes) < bytes) | |
1892 | { | |
1893 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
1894 | "cupsdReadClient: Unable to write %d bytes to %s: %s", | |
1895 | bytes, con->filename, strerror(errno)); | |
1896 | ||
1897 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
1898 | "cupsdReadClient: Closing data file %d...", | |
1899 | con->file); | |
1900 | ||
1901 | close(con->file); | |
1902 | con->file = -1; | |
1903 | unlink(con->filename); | |
1904 | cupsdClearString(&con->filename); | |
1905 | ||
1906 | if (!cupsdSendError(con, HTTP_REQUEST_TOO_LARGE)) | |
1907 | return (cupsdCloseClient(con)); | |
1908 | } | |
1909 | } | |
1910 | ||
1911 | if (con->http.state == HTTP_WAITING) | |
1912 | { | |
1913 | /* | |
1914 | * End of file, see how big it is... | |
1915 | */ | |
1916 | ||
1917 | fstat(con->file, &filestats); | |
1918 | ||
1919 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
1920 | "cupsdReadClient: %d Closing data file %d, size=" | |
1921 | CUPS_LLFMT ".", | |
1922 | con->http.fd, con->file, | |
1923 | CUPS_LLCAST filestats.st_size); | |
1924 | ||
1925 | close(con->file); | |
1926 | con->file = -1; | |
1927 | ||
1928 | if (filestats.st_size > MaxRequestSize && | |
1929 | MaxRequestSize > 0) | |
1930 | { | |
1931 | /* | |
1932 | * Request is too big; remove it and send an error... | |
1933 | */ | |
1934 | ||
1935 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
1936 | "cupsdReadClient: %d Removing temp file %s", | |
1937 | con->http.fd, con->filename); | |
1938 | unlink(con->filename); | |
1939 | cupsdClearString(&con->filename); | |
1940 | ||
1941 | if (!cupsdSendError(con, HTTP_REQUEST_TOO_LARGE)) | |
1942 | return (cupsdCloseClient(con)); | |
1943 | } | |
1944 | ||
1945 | /* | |
1946 | * Install the configuration file... | |
1947 | */ | |
1948 | ||
1949 | status = install_conf_file(con); | |
1950 | ||
1951 | /* | |
1952 | * Return the status to the client... | |
1953 | */ | |
1954 | ||
1955 | if (!cupsdSendError(con, status)) | |
1956 | return (cupsdCloseClient(con)); | |
1957 | } | |
1958 | break; | |
1959 | ||
1960 | case HTTP_POST_RECV : | |
1961 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
1962 | "cupsdReadClient: %d con->data_encoding=HTTP_ENCODE_" | |
1963 | "%s, con->data_remaining=" CUPS_LLFMT ", con->file=%d", | |
1964 | con->http.fd, | |
1965 | con->http.data_encoding == HTTP_ENCODE_CHUNKED ? | |
1966 | "CHUNKED" : "LENGTH", | |
1967 | CUPS_LLCAST con->http.data_remaining, con->file); | |
1968 | ||
1969 | if (con->request != NULL) | |
1970 | { | |
1971 | /* | |
1972 | * Grab any request data from the connection... | |
1973 | */ | |
1974 | ||
1975 | if ((ipp_state = ippRead(&(con->http), con->request)) == IPP_ERROR) | |
1976 | { | |
1977 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
1978 | "cupsdReadClient: %d IPP Read Error!", | |
1979 | con->http.fd); | |
1980 | ||
1981 | cupsdSendError(con, HTTP_BAD_REQUEST); | |
1982 | return (cupsdCloseClient(con)); | |
1983 | } | |
1984 | else if (ipp_state != IPP_DATA) | |
1985 | { | |
1986 | if (con->http.state == HTTP_POST_SEND) | |
1987 | { | |
1988 | cupsdSendError(con, HTTP_BAD_REQUEST); | |
1989 | return (cupsdCloseClient(con)); | |
1990 | } | |
1991 | ||
1992 | break; | |
1993 | } | |
1994 | else | |
1995 | con->bytes += ippLength(con->request); | |
1996 | } | |
1997 | ||
1998 | if (con->file < 0 && con->http.state != HTTP_POST_SEND) | |
1999 | { | |
2000 | /* | |
2001 | * Create a file as needed for the request data... | |
2002 | */ | |
2003 | ||
2004 | cupsdSetStringf(&con->filename, "%s/%08x", RequestRoot, request_id ++); | |
2005 | con->file = open(con->filename, O_WRONLY | O_CREAT | O_TRUNC, 0640); | |
2006 | ||
2007 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdReadClient: %d REQUEST %s=%d", con->http.fd, | |
2008 | con->filename, con->file); | |
2009 | ||
2010 | if (con->file < 0) | |
2011 | { | |
2012 | if (!cupsdSendError(con, HTTP_REQUEST_TOO_LARGE)) | |
2013 | return (cupsdCloseClient(con)); | |
2014 | } | |
2015 | ||
2016 | fchmod(con->file, 0640); | |
2017 | fchown(con->file, RunUser, Group); | |
2018 | fcntl(con->file, F_SETFD, fcntl(con->file, F_GETFD) | FD_CLOEXEC); | |
2019 | } | |
2020 | ||
2021 | if (con->http.state != HTTP_POST_SEND) | |
2022 | { | |
2023 | if ((bytes = httpRead(HTTP(con), line, sizeof(line))) < 0) | |
2024 | return (cupsdCloseClient(con)); | |
2025 | else if (bytes > 0) | |
2026 | { | |
2027 | con->bytes += bytes; | |
2028 | ||
2029 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
2030 | "cupsdReadClient: %d writing %d bytes to %d", | |
2031 | con->http.fd, bytes, con->file); | |
2032 | ||
2033 | if (write(con->file, line, bytes) < bytes) | |
2034 | { | |
2035 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
2036 | "cupsdReadClient: Unable to write %d bytes to %s: %s", | |
2037 | bytes, con->filename, strerror(errno)); | |
2038 | ||
2039 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
2040 | "cupsdReadClient: Closing file %d...", | |
2041 | con->file); | |
2042 | ||
2043 | close(con->file); | |
2044 | con->file = -1; | |
2045 | unlink(con->filename); | |
2046 | cupsdClearString(&con->filename); | |
2047 | ||
2048 | if (!cupsdSendError(con, HTTP_REQUEST_TOO_LARGE)) | |
2049 | return (cupsdCloseClient(con)); | |
2050 | } | |
2051 | } | |
2052 | else if (con->http.state == HTTP_POST_RECV) | |
2053 | return (1); /* ??? */ | |
2054 | else if (con->http.state != HTTP_POST_SEND) | |
2055 | return (cupsdCloseClient(con)); | |
2056 | } | |
2057 | ||
2058 | if (con->http.state == HTTP_POST_SEND) | |
2059 | { | |
2060 | if (con->file >= 0) | |
2061 | { | |
2062 | fstat(con->file, &filestats); | |
2063 | ||
2064 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
2065 | "cupsdReadClient: %d Closing data file %d, " | |
2066 | "size=" CUPS_LLFMT ".", | |
2067 | con->http.fd, con->file, | |
2068 | CUPS_LLCAST filestats.st_size); | |
2069 | ||
2070 | close(con->file); | |
2071 | con->file = -1; | |
2072 | ||
2073 | if (filestats.st_size > MaxRequestSize && | |
2074 | MaxRequestSize > 0) | |
2075 | { | |
2076 | /* | |
2077 | * Request is too big; remove it and send an error... | |
2078 | */ | |
2079 | ||
2080 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
2081 | "cupsdReadClient: %d Removing temp file %s", | |
2082 | con->http.fd, con->filename); | |
2083 | unlink(con->filename); | |
2084 | cupsdClearString(&con->filename); | |
2085 | ||
2086 | if (con->request) | |
2087 | { | |
2088 | /* | |
2089 | * Delete any IPP request data... | |
2090 | */ | |
2091 | ||
2092 | ippDelete(con->request); | |
2093 | con->request = NULL; | |
2094 | } | |
2095 | ||
2096 | if (!cupsdSendError(con, HTTP_REQUEST_TOO_LARGE)) | |
2097 | return (cupsdCloseClient(con)); | |
2098 | } | |
2099 | ||
2100 | if (con->command) | |
2101 | { | |
2102 | if (!cupsdSendCommand(con, con->command, con->options, 0)) | |
2103 | { | |
2104 | if (!cupsdSendError(con, HTTP_NOT_FOUND)) | |
2105 | return (cupsdCloseClient(con)); | |
2106 | } | |
2107 | else | |
2108 | cupsdLogRequest(con, HTTP_OK); | |
2109 | } | |
2110 | } | |
2111 | ||
2112 | if (con->request) | |
2113 | return (cupsdProcessIPPRequest(con)); | |
2114 | } | |
2115 | break; | |
2116 | ||
2117 | default : | |
2118 | break; /* Anti-compiler-warning-code */ | |
2119 | } | |
2120 | ||
2121 | if (!con->http.keep_alive && con->http.state == HTTP_WAITING) | |
2122 | return (cupsdCloseClient(con)); | |
2123 | else | |
2124 | return (1); | |
2125 | } | |
2126 | ||
2127 | ||
2128 | /* | |
2129 | * 'cupsdSendCommand()' - Send output from a command via HTTP. | |
2130 | */ | |
2131 | ||
2132 | int /* O - 1 on success, 0 on failure */ | |
2133 | cupsdSendCommand( | |
2134 | cupsd_client_t *con, /* I - Client connection */ | |
2135 | char *command, /* I - Command to run */ | |
2136 | char *options, /* I - Command-line options */ | |
2137 | int root) /* I - Run as root? */ | |
2138 | { | |
2139 | int fd; /* Standard input file descriptor */ | |
2140 | ||
2141 | ||
2142 | if (con->filename) | |
2143 | fd = open(con->filename, O_RDONLY); | |
2144 | else | |
2145 | fd = open("/dev/null", O_RDONLY); | |
2146 | ||
2147 | if (fd < 0) | |
2148 | { | |
2149 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
2150 | "cupsdSendCommand: %d Unable to open \"%s\" for reading: %s", | |
2151 | con->http.fd, con->filename ? con->filename : "/dev/null", | |
2152 | strerror(errno)); | |
2153 | return (0); | |
2154 | } | |
2155 | ||
2156 | fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC); | |
2157 | ||
2158 | con->pipe_pid = pipe_command(con, fd, &(con->file), command, options, root); | |
2159 | ||
2160 | close(fd); | |
2161 | ||
2162 | cupsdLogMessage(CUPSD_LOG_INFO, "Started \"%s\" (pid=%d)", command, | |
2163 | con->pipe_pid); | |
2164 | ||
2165 | cupsdLogMessage(CUPSD_LOG_DEBUG, "cupsdSendCommand: %d file=%d", | |
2166 | con->http.fd, con->file); | |
2167 | ||
2168 | if (con->pipe_pid == 0) | |
2169 | return (0); | |
2170 | ||
2171 | fcntl(con->file, F_SETFD, fcntl(con->file, F_GETFD) | FD_CLOEXEC); | |
2172 | ||
2173 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
2174 | "cupsdSendCommand: Adding fd %d to InputSet...", con->file); | |
2175 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
2176 | "cupsdSendCommand: Adding fd %d to OutputSet...", | |
2177 | con->http.fd); | |
2178 | ||
2179 | FD_SET(con->file, InputSet); | |
2180 | FD_SET(con->http.fd, OutputSet); | |
2181 | ||
2182 | con->sent_header = 0; | |
2183 | con->file_ready = 0; | |
2184 | con->got_fields = 0; | |
2185 | con->field_col = 0; | |
2186 | ||
2187 | return (1); | |
2188 | } | |
2189 | ||
2190 | ||
2191 | /* | |
2192 | * 'cupsdSendError()' - Send an error message via HTTP. | |
2193 | */ | |
2194 | ||
2195 | int /* O - 1 if successful, 0 otherwise */ | |
2196 | cupsdSendError(cupsd_client_t *con, /* I - Connection */ | |
2197 | http_status_t code) /* I - Error code */ | |
2198 | { | |
2199 | /* | |
2200 | * Put the request in the access_log file... | |
2201 | */ | |
2202 | ||
2203 | cupsdLogRequest(con, code); | |
2204 | ||
2205 | cupsdLogMessage(CUPSD_LOG_DEBUG, "cupsdSendError: %d code=%d (%s)", | |
2206 | con->http.fd, code, httpStatus(code)); | |
2207 | ||
2208 | /* | |
2209 | * To work around bugs in some proxies, don't use Keep-Alive for some | |
2210 | * error messages... | |
2211 | */ | |
2212 | ||
2213 | if (code >= HTTP_BAD_REQUEST) | |
2214 | con->http.keep_alive = HTTP_KEEPALIVE_OFF; | |
2215 | ||
2216 | /* | |
2217 | * Send an error message back to the client. If the error code is a | |
2218 | * 400 or 500 series, make sure the message contains some text, too! | |
2219 | */ | |
2220 | ||
2221 | if (!cupsdSendHeader(con, code, NULL)) | |
2222 | return (0); | |
2223 | ||
2224 | #ifdef HAVE_SSL | |
2225 | if (code == HTTP_UPGRADE_REQUIRED) | |
2226 | if (httpPrintf(HTTP(con), "Connection: Upgrade\r\n") < 0) | |
2227 | return (0); | |
2228 | ||
2229 | if (httpPrintf(HTTP(con), "Upgrade: TLS/1.0,HTTP/1.1\r\n") < 0) | |
2230 | return (0); | |
2231 | #endif /* HAVE_SSL */ | |
2232 | ||
2233 | if ((con->http.version >= HTTP_1_1 && !con->http.keep_alive) || | |
2234 | (code >= HTTP_BAD_REQUEST && code != HTTP_UPGRADE_REQUIRED)) | |
2235 | { | |
2236 | if (httpPrintf(HTTP(con), "Connection: close\r\n") < 0) | |
2237 | return (0); | |
2238 | } | |
2239 | ||
2240 | if (code >= HTTP_BAD_REQUEST) | |
2241 | { | |
2242 | /* | |
2243 | * Send a human-readable error message. | |
2244 | */ | |
2245 | ||
2246 | char message[1024]; /* Message for user */ | |
2247 | const char *text; /* Status-specific text */ | |
2248 | ||
2249 | if (code == HTTP_UNAUTHORIZED) | |
2250 | text = _cupsLangString(con->language, | |
2251 | _("Enter your username and password or the " | |
2252 | "root username and password to access this " | |
2253 | "page.")); | |
2254 | else if (code == HTTP_UPGRADE_REQUIRED) | |
2255 | text = _cupsLangString(con->language, | |
2256 | _("You must use a https: URL to access this " | |
2257 | "page.")); | |
2258 | else | |
2259 | text = ""; | |
2260 | ||
2261 | snprintf(message, sizeof(message), | |
2262 | "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\" " | |
2263 | "\"http://www.w3.org/TR/REC-html40/loose.dtd\">\n" | |
2264 | "<HTML>\n" | |
2265 | "<HEAD>\n" | |
2266 | "\t<META HTTP-EQUIV=\"Content-Type\" " | |
2267 | "CONTENT=\"text/html; charset=utf-8\">\n" | |
2268 | "\t<TITLE>%d %s</TITLE>\n" | |
2269 | "\t<LINK REL=\"STYLESHEET\" TYPE=\"text/css\" " | |
2270 | "HREF=\"/cups.css\">\n" | |
2271 | "</HEAD>\n" | |
2272 | "<BODY>\n" | |
2273 | "<H1>%d %s</H1>\n" | |
2274 | "<P>%s</P>\n" | |
2275 | "</BODY>\n" | |
2276 | "</HTML>\n", | |
2277 | code, httpStatus(code), code, httpStatus(code), text); | |
2278 | ||
2279 | if (httpPrintf(HTTP(con), "Content-Type: text/html; charset=utf-8\r\n") < 0) | |
2280 | return (0); | |
2281 | if (httpPrintf(HTTP(con), "Content-Length: %d\r\n", | |
2282 | (int)strlen(message)) < 0) | |
2283 | return (0); | |
2284 | if (httpPrintf(HTTP(con), "\r\n") < 0) | |
2285 | return (0); | |
2286 | if (httpPrintf(HTTP(con), "%s", message) < 0) | |
2287 | return (0); | |
2288 | } | |
2289 | else if (httpPrintf(HTTP(con), "\r\n") < 0) | |
2290 | return (0); | |
2291 | ||
2292 | con->http.state = HTTP_WAITING; | |
2293 | ||
2294 | return (1); | |
2295 | } | |
2296 | ||
2297 | ||
2298 | /* | |
2299 | * 'cupsdSendFile()' - Send a file via HTTP. | |
2300 | */ | |
2301 | ||
2302 | int /* O - 0 on failure, 1 on success */ | |
2303 | cupsdSendFile(cupsd_client_t *con, /* I - Client connection */ | |
2304 | http_status_t code, /* I - HTTP status */ | |
2305 | char *filename, /* I - Filename */ | |
2306 | char *type, /* I - File type */ | |
2307 | struct stat *filestats)/* O - File information */ | |
2308 | { | |
2309 | con->file = open(filename, O_RDONLY); | |
2310 | ||
2311 | cupsdLogMessage(CUPSD_LOG_DEBUG, "cupsdSendFile: %d file=%d", con->http.fd, | |
2312 | con->file); | |
2313 | ||
2314 | if (con->file < 0) | |
2315 | return (0); | |
2316 | ||
2317 | fcntl(con->file, F_SETFD, fcntl(con->file, F_GETFD) | FD_CLOEXEC); | |
2318 | ||
2319 | con->pipe_pid = 0; | |
2320 | ||
2321 | if (!cupsdSendHeader(con, code, type)) | |
2322 | return (0); | |
2323 | ||
2324 | if (httpPrintf(HTTP(con), "Last-Modified: %s\r\n", | |
2325 | httpGetDateString(filestats->st_mtime)) < 0) | |
2326 | return (0); | |
2327 | if (httpPrintf(HTTP(con), "Content-Length: " CUPS_LLFMT "\r\n", | |
2328 | CUPS_LLCAST filestats->st_size) < 0) | |
2329 | return (0); | |
2330 | if (httpPrintf(HTTP(con), "\r\n") < 0) | |
2331 | return (0); | |
2332 | ||
2333 | con->http.data_encoding = HTTP_ENCODE_LENGTH; | |
2334 | con->http.data_remaining = filestats->st_size; | |
2335 | ||
2336 | if (con->http.data_remaining <= INT_MAX) | |
2337 | con->http._data_remaining = con->http.data_remaining; | |
2338 | else | |
2339 | con->http._data_remaining = INT_MAX; | |
2340 | ||
2341 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
2342 | "cupsdSendFile: Adding fd %d to OutputSet...", con->http.fd); | |
2343 | ||
2344 | FD_SET(con->http.fd, OutputSet); | |
2345 | ||
2346 | return (1); | |
2347 | } | |
2348 | ||
2349 | ||
2350 | /* | |
2351 | * 'cupsdSendHeader()' - Send an HTTP request. | |
2352 | */ | |
2353 | ||
2354 | int /* O - 1 on success, 0 on failure */ | |
2355 | cupsdSendHeader(cupsd_client_t *con, /* I - Client to send to */ | |
2356 | http_status_t code, /* I - HTTP status code */ | |
2357 | char *type) /* I - MIME type of document */ | |
2358 | { | |
2359 | if (httpPrintf(HTTP(con), "HTTP/%d.%d %d %s\r\n", con->http.version / 100, | |
2360 | con->http.version % 100, code, httpStatus(code)) < 0) | |
2361 | return (0); | |
2362 | if (httpPrintf(HTTP(con), "Date: %s\r\n", httpGetDateString(time(NULL))) < 0) | |
2363 | return (0); | |
2364 | if (ServerHeader) | |
2365 | if (httpPrintf(HTTP(con), "Server: %s\r\n", ServerHeader) < 0) | |
2366 | return (0); | |
2367 | if (con->http.keep_alive && con->http.version >= HTTP_1_0) | |
2368 | { | |
2369 | if (httpPrintf(HTTP(con), "Connection: Keep-Alive\r\n") < 0) | |
2370 | return (0); | |
2371 | if (httpPrintf(HTTP(con), "Keep-Alive: timeout=%d\r\n", | |
2372 | KeepAliveTimeout) < 0) | |
2373 | return (0); | |
2374 | } | |
2375 | if (code == HTTP_METHOD_NOT_ALLOWED) | |
2376 | if (httpPrintf(HTTP(con), "Allow: GET, HEAD, OPTIONS, POST\r\n") < 0) | |
2377 | return (0); | |
2378 | ||
2379 | if (code == HTTP_UNAUTHORIZED) | |
2380 | { | |
2381 | int auth_type; /* Authentication type */ | |
2382 | ||
2383 | ||
2384 | if (!con->best || con->best->type == AUTH_NONE) | |
2385 | auth_type = DefaultAuthType; | |
2386 | else | |
2387 | auth_type = con->best->type; | |
2388 | ||
2389 | if (auth_type != AUTH_DIGEST) | |
2390 | { | |
2391 | if (httpPrintf(HTTP(con), | |
2392 | "WWW-Authenticate: Basic realm=\"CUPS\"\r\n") < 0) | |
2393 | return (0); | |
2394 | } | |
2395 | else | |
2396 | { | |
2397 | if (httpPrintf(HTTP(con), | |
2398 | "WWW-Authenticate: Digest realm=\"CUPS\", nonce=\"%s\"\r\n", | |
2399 | con->http.hostname) < 0) | |
2400 | return (0); | |
2401 | } | |
2402 | } | |
2403 | ||
2404 | if (con->language != NULL) | |
2405 | { | |
2406 | if (httpPrintf(HTTP(con), "Content-Language: %s\r\n", | |
2407 | con->language->language) < 0) | |
2408 | return (0); | |
2409 | } | |
2410 | ||
2411 | if (type != NULL) | |
2412 | { | |
2413 | if (!strcmp(type, "text/html")) | |
2414 | { | |
2415 | if (httpPrintf(HTTP(con), | |
2416 | "Content-Type: text/html; charset=utf-8\r\n") < 0) | |
2417 | return (0); | |
2418 | } | |
2419 | else if (httpPrintf(HTTP(con), "Content-Type: %s\r\n", type) < 0) | |
2420 | return (0); | |
2421 | } | |
2422 | ||
2423 | return (1); | |
2424 | } | |
2425 | ||
2426 | ||
2427 | /* | |
2428 | * 'cupsdUpdateCGI()' - Read status messages from CGI scripts and programs. | |
2429 | */ | |
2430 | ||
2431 | void | |
2432 | cupsdUpdateCGI(void) | |
2433 | { | |
2434 | char *ptr, /* Pointer to end of line in buffer */ | |
2435 | message[1024]; /* Pointer to message text */ | |
2436 | int loglevel; /* Log level for message */ | |
2437 | ||
2438 | ||
2439 | while ((ptr = cupsdStatBufUpdate(CGIStatusBuffer, &loglevel, | |
2440 | message, sizeof(message))) != NULL) | |
2441 | if (!strchr(CGIStatusBuffer->buffer, '\n')) | |
2442 | break; | |
2443 | ||
2444 | if (ptr == NULL && errno) | |
2445 | { | |
2446 | /* | |
2447 | * Fatal error on pipe - should never happen! | |
2448 | */ | |
2449 | ||
2450 | cupsdLogMessage(CUPSD_LOG_CRIT, | |
2451 | "cupsdUpdateCGI: error reading from CGI error pipe - %s", | |
2452 | strerror(errno)); | |
2453 | } | |
2454 | } | |
2455 | ||
2456 | ||
2457 | /* | |
2458 | * 'cupsdWriteClient()' - Write data to a client as needed. | |
2459 | */ | |
2460 | ||
2461 | int /* O - 1 if success, 0 if fail */ | |
2462 | cupsdWriteClient(cupsd_client_t *con) /* I - Client connection */ | |
2463 | { | |
2464 | int bytes; /* Number of bytes written */ | |
2465 | char buf[16385]; /* Data buffer */ | |
2466 | char *bufptr; /* Pointer into buffer */ | |
2467 | ipp_state_t ipp_state; /* IPP state value */ | |
2468 | ||
2469 | ||
2470 | #ifdef DEBUG | |
2471 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
2472 | "cupsdWriteClient(con=%p) %d response=%p, file=%d " | |
2473 | "pipe_pid=%d state=%d", | |
2474 | con, con->http.fd, con->response, con->file, con->pipe_pid, | |
2475 | con->http.state); | |
2476 | #endif /* DEBUG */ | |
2477 | ||
2478 | if (con->http.state != HTTP_GET_SEND && | |
2479 | con->http.state != HTTP_POST_SEND) | |
2480 | return (1); | |
2481 | ||
2482 | if (con->response != NULL) | |
2483 | { | |
2484 | ipp_state = ippWrite(&(con->http), con->response); | |
2485 | bytes = ipp_state != IPP_ERROR && ipp_state != IPP_DATA; | |
2486 | } | |
2487 | else if ((bytes = read(con->file, buf, sizeof(buf) - 1)) > 0) | |
2488 | { | |
2489 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
2490 | "cupsdWriteClient: Read %d bytes from file %d...", | |
2491 | bytes, con->file); | |
2492 | ||
2493 | if (con->pipe_pid && !con->got_fields) | |
2494 | { | |
2495 | /* | |
2496 | * Inspect the data for Content-Type and other fields. | |
2497 | */ | |
2498 | ||
2499 | buf[bytes] = '\0'; | |
2500 | ||
2501 | for (bufptr = buf; !con->got_fields && *bufptr; bufptr ++) | |
2502 | if (*bufptr == '\n') | |
2503 | { | |
2504 | /* | |
2505 | * Send line to client... | |
2506 | */ | |
2507 | ||
2508 | if (bufptr > buf && bufptr[-1] == '\r') | |
2509 | bufptr[-1] = '\0'; | |
2510 | *bufptr++ = '\0'; | |
2511 | ||
2512 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "Script header: %s", buf); | |
2513 | ||
2514 | if (!con->sent_header) | |
2515 | { | |
2516 | /* | |
2517 | * Handle redirection and CGI status codes... | |
2518 | */ | |
2519 | ||
2520 | if (!strncasecmp(buf, "Location:", 9)) | |
2521 | cupsdSendHeader(con, HTTP_SEE_OTHER, NULL); | |
2522 | else if (!strncasecmp(buf, "Status:", 7)) | |
2523 | cupsdSendHeader(con, atoi(buf + 7), NULL); | |
2524 | else | |
2525 | cupsdSendHeader(con, HTTP_OK, NULL); | |
2526 | ||
2527 | if (con->http.version == HTTP_1_1) | |
2528 | { | |
2529 | con->http.data_encoding = HTTP_ENCODE_CHUNKED; | |
2530 | ||
2531 | if (httpPrintf(HTTP(con), "Transfer-Encoding: chunked\r\n") < 0) | |
2532 | return (0); | |
2533 | } | |
2534 | ||
2535 | con->sent_header = 1; | |
2536 | } | |
2537 | ||
2538 | if (strncasecmp(buf, "Status:", 7)) | |
2539 | httpPrintf(HTTP(con), "%s\r\n", buf); | |
2540 | ||
2541 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdWriteClient: %d %s", | |
2542 | con->http.fd, buf); | |
2543 | ||
2544 | /* | |
2545 | * Update buffer... | |
2546 | */ | |
2547 | ||
2548 | bytes -= (bufptr - buf); | |
2549 | memmove(buf, bufptr, bytes + 1); | |
2550 | bufptr = buf - 1; | |
2551 | ||
2552 | /* | |
2553 | * See if the line was empty... | |
2554 | */ | |
2555 | ||
2556 | if (con->field_col == 0) | |
2557 | con->got_fields = 1; | |
2558 | else | |
2559 | con->field_col = 0; | |
2560 | } | |
2561 | else if (*bufptr != '\r') | |
2562 | con->field_col ++; | |
2563 | ||
2564 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
2565 | "cupsdWriteClient: %d bytes=%d, got_fields=%d", | |
2566 | con->http.fd, bytes, con->got_fields); | |
2567 | ||
2568 | if (bytes > 0 && !con->got_fields) | |
2569 | { | |
2570 | /* | |
2571 | * Remaining text needs to go out... | |
2572 | */ | |
2573 | ||
2574 | httpPrintf(HTTP(con), "%s", buf); | |
2575 | ||
2576 | con->http.activity = time(NULL); | |
2577 | return (1); | |
2578 | } | |
2579 | else if (bytes == 0) | |
2580 | { | |
2581 | con->http.activity = time(NULL); | |
2582 | return (1); | |
2583 | } | |
2584 | } | |
2585 | ||
2586 | if (httpWrite(HTTP(con), buf, bytes) < 0) | |
2587 | { | |
2588 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
2589 | "cupsdWriteClient: %d Write of %d bytes failed!", | |
2590 | con->http.fd, bytes); | |
2591 | ||
2592 | cupsdCloseClient(con); | |
2593 | return (0); | |
2594 | } | |
2595 | ||
2596 | con->bytes += bytes; | |
2597 | ||
2598 | if (con->http.state == HTTP_WAITING) | |
2599 | bytes = 0; | |
2600 | } | |
2601 | ||
2602 | if (bytes <= 0) | |
2603 | { | |
2604 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdWriteClient: %d bytes < 0", | |
2605 | con->http.fd); | |
2606 | ||
2607 | cupsdLogRequest(con, HTTP_OK); | |
2608 | ||
2609 | httpFlushWrite(HTTP(con)); | |
2610 | ||
2611 | if (con->http.data_encoding == HTTP_ENCODE_CHUNKED) | |
2612 | { | |
2613 | if (httpPrintf(HTTP(con), "0\r\n\r\n") < 0) | |
2614 | { | |
2615 | cupsdCloseClient(con); | |
2616 | return (0); | |
2617 | } | |
2618 | } | |
2619 | ||
2620 | con->http.state = HTTP_WAITING; | |
2621 | ||
2622 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
2623 | "cupsdWriteClient: Removing fd %d from OutputSet...", | |
2624 | con->http.fd); | |
2625 | ||
2626 | FD_CLR(con->http.fd, OutputSet); | |
2627 | ||
2628 | if (con->file >= 0) | |
2629 | { | |
2630 | if (FD_ISSET(con->file, InputSet)) | |
2631 | { | |
2632 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
2633 | "cupsdWriteClient: Removing fd %d from InputSet...", | |
2634 | con->file); | |
2635 | FD_CLR(con->file, InputSet); | |
2636 | } | |
2637 | ||
2638 | if (con->pipe_pid) | |
2639 | cupsdEndProcess(con->pipe_pid, 0); | |
2640 | ||
2641 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
2642 | "cupsdWriteClient: %d Closing data file %d.", | |
2643 | con->http.fd, con->file); | |
2644 | ||
2645 | close(con->file); | |
2646 | con->file = -1; | |
2647 | con->pipe_pid = 0; | |
2648 | } | |
2649 | ||
2650 | if (con->filename) | |
2651 | { | |
2652 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
2653 | "cupsdWriteClient: %d Removing temp file %s", | |
2654 | con->http.fd, con->filename); | |
2655 | unlink(con->filename); | |
2656 | cupsdClearString(&con->filename); | |
2657 | } | |
2658 | ||
2659 | if (con->request != NULL) | |
2660 | { | |
2661 | ippDelete(con->request); | |
2662 | con->request = NULL; | |
2663 | } | |
2664 | ||
2665 | if (con->response != NULL) | |
2666 | { | |
2667 | ippDelete(con->response); | |
2668 | con->response = NULL; | |
2669 | } | |
2670 | ||
2671 | cupsdClearString(&con->command); | |
2672 | cupsdClearString(&con->options); | |
2673 | ||
2674 | if (!con->http.keep_alive) | |
2675 | { | |
2676 | cupsdCloseClient(con); | |
2677 | return (0); | |
2678 | } | |
2679 | } | |
2680 | else | |
2681 | { | |
2682 | con->file_ready = 0; | |
2683 | ||
2684 | if (con->pipe_pid && !FD_ISSET(con->file, InputSet)) | |
2685 | { | |
2686 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
2687 | "cupsdWriteClient: Adding fd %d to InputSet...", | |
2688 | con->file); | |
2689 | FD_SET(con->file, InputSet); | |
2690 | } | |
2691 | } | |
2692 | ||
2693 | con->http.activity = time(NULL); | |
2694 | ||
2695 | return (1); | |
2696 | } | |
2697 | ||
2698 | ||
2699 | /* | |
2700 | * 'check_if_modified()' - Decode an "If-Modified-Since" line. | |
2701 | */ | |
2702 | ||
2703 | static int /* O - 1 if modified since */ | |
2704 | check_if_modified( | |
2705 | cupsd_client_t *con, /* I - Client connection */ | |
2706 | struct stat *filestats) /* I - File information */ | |
2707 | { | |
2708 | char *ptr; /* Pointer into field */ | |
2709 | time_t date; /* Time/date value */ | |
2710 | off_t size; /* Size/length value */ | |
2711 | ||
2712 | ||
2713 | size = 0; | |
2714 | date = 0; | |
2715 | ptr = con->http.fields[HTTP_FIELD_IF_MODIFIED_SINCE]; | |
2716 | ||
2717 | if (*ptr == '\0') | |
2718 | return (1); | |
2719 | ||
2720 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
2721 | "check_if_modified: %d If-Modified-Since=\"%s\"", | |
2722 | con->http.fd, ptr); | |
2723 | ||
2724 | while (*ptr != '\0') | |
2725 | { | |
2726 | while (isspace(*ptr) || *ptr == ';') | |
2727 | ptr ++; | |
2728 | ||
2729 | if (strncasecmp(ptr, "length=", 7) == 0) | |
2730 | { | |
2731 | ptr += 7; | |
2732 | size = strtoll(ptr, NULL, 10); | |
2733 | ||
2734 | while (isdigit(*ptr)) | |
2735 | ptr ++; | |
2736 | } | |
2737 | else if (isalpha(*ptr)) | |
2738 | { | |
2739 | date = httpGetDateTime(ptr); | |
2740 | while (*ptr != '\0' && *ptr != ';') | |
2741 | ptr ++; | |
2742 | } | |
2743 | } | |
2744 | ||
2745 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
2746 | "check_if_modified: %d sizes=" CUPS_LLFMT "," | |
2747 | CUPS_LLFMT " dates=%d,%d", | |
2748 | con->http.fd, CUPS_LLCAST size, | |
2749 | CUPS_LLCAST filestats->st_size, (int)date, | |
2750 | (int)filestats->st_mtime); | |
2751 | ||
2752 | return ((size != filestats->st_size && size != 0) || | |
2753 | (date < filestats->st_mtime && date != 0) || | |
2754 | (size == 0 && date == 0)); | |
2755 | } | |
2756 | ||
2757 | ||
2758 | #ifdef HAVE_CDSASSL | |
2759 | /* | |
2760 | * 'get_cdsa_server_certs()' - Convert a keychain name into the CFArrayRef | |
2761 | * required by SSLSetCertificate. | |
2762 | * | |
2763 | * For now we assumes that there is exactly one SecIdentity in the | |
2764 | * keychain - i.e. there is exactly one matching cert/private key pair. | |
2765 | * In the future we will search a keychain for a SecIdentity matching a | |
2766 | * specific criteria. We also skip the operation of adding additional | |
2767 | * non-signing certs from the keychain to the CFArrayRef. | |
2768 | * | |
2769 | * To create a self-signed certificate for testing use the certtool. | |
2770 | * Executing the following as root will do it: | |
2771 | * | |
2772 | * certtool c c v k=CUPS | |
2773 | */ | |
2774 | ||
2775 | static CFArrayRef /* O - Array of certificates */ | |
2776 | get_cdsa_server_certs(void) | |
2777 | { | |
2778 | OSStatus err; /* Error info */ | |
2779 | SecKeychainRef kcRef; /* Keychain reference */ | |
2780 | SecIdentitySearchRef srchRef; /* Search reference */ | |
2781 | SecIdentityRef identity; /* Identity */ | |
2782 | CFArrayRef ca; /* Certificate array */ | |
2783 | ||
2784 | ||
2785 | kcRef = NULL; | |
2786 | srchRef = NULL; | |
2787 | identity = NULL; | |
2788 | ca = NULL; | |
2789 | err = SecKeychainOpen(ServerCertificate, &kcRef); | |
2790 | ||
2791 | if (err) | |
2792 | cupsdLogMessage(CUPSD_LOG_ERROR, "Cannot open keychain \"%s\", error %d.", | |
2793 | ServerCertificate, (int)err); | |
2794 | else | |
2795 | { | |
2796 | /* | |
2797 | * Search for "any" identity matching specified key use; | |
2798 | * in this app, we expect there to be exactly one. | |
2799 | */ | |
2800 | ||
2801 | err = SecIdentitySearchCreate(kcRef, CSSM_KEYUSE_SIGN, &srchRef); | |
2802 | ||
2803 | if (err) | |
2804 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
2805 | "Cannot find signing key in keychain \"%s\", error %d", | |
2806 | ServerCertificate, (int)err); | |
2807 | else | |
2808 | { | |
2809 | err = SecIdentitySearchCopyNext(srchRef, &identity); | |
2810 | ||
2811 | if (err) | |
2812 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
2813 | "Cannot find signing key in keychain \"%s\", error %d", | |
2814 | ServerCertificate, (int)err); | |
2815 | else | |
2816 | { | |
2817 | if (CFGetTypeID(identity) != SecIdentityGetTypeID()) | |
2818 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
2819 | "SecIdentitySearchCopyNext CFTypeID failure!"); | |
2820 | else | |
2821 | { | |
2822 | /* | |
2823 | * Found one. Place it in a CFArray. | |
2824 | * TBD: snag other (non-identity) certs from keychain and add them | |
2825 | * to array as well. | |
2826 | */ | |
2827 | ||
2828 | ca = CFArrayCreate(NULL, (const void **)&identity, 1, NULL); | |
2829 | ||
2830 | if (ca == nil) | |
2831 | cupsdLogMessage(CUPSD_LOG_ERROR, "CFArrayCreate error"); | |
2832 | } | |
2833 | ||
2834 | /*CFRelease(identity);*/ | |
2835 | } | |
2836 | ||
2837 | /*CFRelease(srchRef);*/ | |
2838 | } | |
2839 | ||
2840 | /*CFRelease(kcRef);*/ | |
2841 | } | |
2842 | ||
2843 | return (ca); | |
2844 | } | |
2845 | #endif /* HAVE_CDSASSL */ | |
2846 | ||
2847 | ||
2848 | /* | |
2849 | * 'get_file()' - Get a filename and state info. | |
2850 | */ | |
2851 | ||
2852 | static char * /* O - Real filename */ | |
2853 | get_file(cupsd_client_t *con, /* I - Client connection */ | |
2854 | struct stat *filestats, /* O - File information */ | |
2855 | char *filename, /* IO - Filename buffer */ | |
2856 | int len) /* I - Buffer length */ | |
2857 | { | |
2858 | int status; /* Status of filesystem calls */ | |
2859 | char *ptr; /* Pointer info filename */ | |
2860 | int plen; /* Remaining length after pointer */ | |
2861 | ||
2862 | ||
2863 | /* | |
2864 | * Need to add DocumentRoot global... | |
2865 | */ | |
2866 | ||
2867 | if (!strncmp(con->uri, "/ppd/", 5)) | |
2868 | snprintf(filename, len, "%s%s", ServerRoot, con->uri); | |
2869 | else if (!strncmp(con->uri, "/admin/conf/", 12)) | |
2870 | snprintf(filename, len, "%s%s", ServerRoot, con->uri + 11); | |
2871 | else if (!strncmp(con->uri, "/admin/log/", 11)) | |
2872 | { | |
2873 | if (!strcmp(con->uri + 11, "access_log") && AccessLog[0] == '/') | |
2874 | strlcpy(filename, AccessLog, len); | |
2875 | else if (!strcmp(con->uri + 11, "error_log") && ErrorLog[0] == '/') | |
2876 | strlcpy(filename, ErrorLog, len); | |
2877 | else if (!strcmp(con->uri + 11, "page_log") && PageLog[0] == '/') | |
2878 | strlcpy(filename, PageLog, len); | |
2879 | else | |
2880 | return (NULL); | |
2881 | } | |
2882 | else if (con->language != NULL) | |
2883 | snprintf(filename, len, "%s/%s%s", DocumentRoot, con->language->language, | |
2884 | con->uri); | |
2885 | else | |
2886 | snprintf(filename, len, "%s%s", DocumentRoot, con->uri); | |
2887 | ||
2888 | if ((ptr = strchr(filename, '?')) != NULL) | |
2889 | *ptr = '\0'; | |
2890 | ||
2891 | /* | |
2892 | * Grab the status for this language; if there isn't a language-specific file | |
2893 | * then fallback to the default one... | |
2894 | */ | |
2895 | ||
2896 | if ((status = stat(filename, filestats)) != 0 && con->language != NULL) | |
2897 | { | |
2898 | /* | |
2899 | * Drop the language prefix and try the current directory... | |
2900 | */ | |
2901 | ||
2902 | if (strncmp(con->uri, "/ppd/", 5) && | |
2903 | strncmp(con->uri, "/admin/conf/", 12) && | |
2904 | strncmp(con->uri, "/admin/log/", 11)) | |
2905 | { | |
2906 | snprintf(filename, len, "%s%s", DocumentRoot, con->uri); | |
2907 | ||
2908 | if ((ptr = strchr(filename, '?')) != NULL) | |
2909 | *ptr = '\0'; | |
2910 | ||
2911 | status = stat(filename, filestats); | |
2912 | } | |
2913 | } | |
2914 | ||
2915 | /* | |
2916 | * If we're found a directory, get the index.html file instead... | |
2917 | */ | |
2918 | ||
2919 | if (!status && S_ISDIR(filestats->st_mode)) | |
2920 | { | |
2921 | if (filename[strlen(filename) - 1] != '/') | |
2922 | strlcat(filename, "/", len); | |
2923 | ||
2924 | ptr = filename + strlen(filename); | |
2925 | plen = len - (ptr - filename); | |
2926 | ||
2927 | strlcpy(ptr, "index.html", plen); | |
2928 | status = stat(filename, filestats); | |
2929 | ||
2930 | #ifdef HAVE_JAVA | |
2931 | if (status) | |
2932 | { | |
2933 | strlcpy(ptr, "index.class", plen); | |
2934 | status = stat(filename, filestats); | |
2935 | } | |
2936 | #endif /* HAVE_JAVA */ | |
2937 | ||
2938 | #ifdef HAVE_PERL | |
2939 | if (status) | |
2940 | { | |
2941 | strlcpy(ptr, "index.pl", plen); | |
2942 | status = stat(filename, filestats); | |
2943 | } | |
2944 | #endif /* HAVE_PERL */ | |
2945 | ||
2946 | #ifdef HAVE_PHP | |
2947 | if (status) | |
2948 | { | |
2949 | strlcpy(ptr, "index.php", plen); | |
2950 | status = stat(filename, filestats); | |
2951 | } | |
2952 | #endif /* HAVE_PHP */ | |
2953 | ||
2954 | #ifdef HAVE_PYTHON | |
2955 | if (status) | |
2956 | { | |
2957 | strlcpy(ptr, "index.pyc", plen); | |
2958 | status = stat(filename, filestats); | |
2959 | } | |
2960 | ||
2961 | if (status) | |
2962 | { | |
2963 | strlcpy(ptr, "index.py", plen); | |
2964 | status = stat(filename, filestats); | |
2965 | } | |
2966 | #endif /* HAVE_PYTHON */ | |
2967 | } | |
2968 | ||
2969 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_file: %d filename=%s size=%d", | |
2970 | con->http.fd, filename, | |
2971 | status ? -1 : (int)filestats->st_size); | |
2972 | ||
2973 | if (!status) | |
2974 | con->http.data_remaining = (int)filestats->st_size; | |
2975 | ||
2976 | if (status) | |
2977 | return (NULL); | |
2978 | else | |
2979 | return (filename); | |
2980 | } | |
2981 | ||
2982 | ||
2983 | /* | |
2984 | * 'install_conf_file()' - Install a configuration file. | |
2985 | */ | |
2986 | ||
2987 | static http_status_t /* O - Status */ | |
2988 | install_conf_file(cupsd_client_t *con) /* I - Connection */ | |
2989 | { | |
2990 | cups_file_t *in, /* Input file */ | |
2991 | *out; /* Output file */ | |
2992 | char buffer[1024]; /* Copy buffer */ | |
2993 | int bytes; /* Number of bytes */ | |
2994 | char conffile[1024], /* Configuration filename */ | |
2995 | newfile[1024], /* New config filename */ | |
2996 | oldfile[1024]; /* Old config filename */ | |
2997 | struct stat confinfo; /* Config file info */ | |
2998 | ||
2999 | ||
3000 | /* | |
3001 | * First construct the filenames... | |
3002 | */ | |
3003 | ||
3004 | snprintf(conffile, sizeof(conffile), "%s%s", ServerRoot, con->uri + 11); | |
3005 | snprintf(newfile, sizeof(newfile), "%s%s.N", ServerRoot, con->uri + 11); | |
3006 | snprintf(oldfile, sizeof(oldfile), "%s%s.O", ServerRoot, con->uri + 11); | |
3007 | ||
3008 | cupsdLogMessage(CUPSD_LOG_INFO, "Installing config file \"%s\"...", conffile); | |
3009 | ||
3010 | /* | |
3011 | * Get the owner, group, and permissions of the configuration file. | |
3012 | * If it doesn't exist, assign it to the User and Group in the | |
3013 | * cupsd.conf file with mode 0640 permissions. | |
3014 | */ | |
3015 | ||
3016 | if (stat(conffile, &confinfo)) | |
3017 | { | |
3018 | confinfo.st_uid = User; | |
3019 | confinfo.st_gid = Group; | |
3020 | confinfo.st_mode = ConfigFilePerm; | |
3021 | } | |
3022 | ||
3023 | /* | |
3024 | * Open the request file and new config file... | |
3025 | */ | |
3026 | ||
3027 | if ((in = cupsFileOpen(con->filename, "rb")) == NULL) | |
3028 | { | |
3029 | cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to open request file \"%s\" - %s", | |
3030 | con->filename, strerror(errno)); | |
3031 | return (HTTP_SERVER_ERROR); | |
3032 | } | |
3033 | ||
3034 | if ((out = cupsFileOpen(newfile, "wb")) == NULL) | |
3035 | { | |
3036 | cupsFileClose(in); | |
3037 | cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to open config file \"%s\" - %s", | |
3038 | newfile, strerror(errno)); | |
3039 | return (HTTP_SERVER_ERROR); | |
3040 | } | |
3041 | ||
3042 | fchmod(cupsFileNumber(out), confinfo.st_mode); | |
3043 | fchown(cupsFileNumber(out), confinfo.st_uid, confinfo.st_gid); | |
3044 | ||
3045 | /* | |
3046 | * Copy from the request to the new config file... | |
3047 | */ | |
3048 | ||
3049 | while ((bytes = cupsFileRead(in, buffer, sizeof(buffer))) > 0) | |
3050 | if (cupsFileWrite(out, buffer, bytes) < bytes) | |
3051 | { | |
3052 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
3053 | "Unable to copy to config file \"%s\" - %s", | |
3054 | newfile, strerror(errno)); | |
3055 | ||
3056 | cupsFileClose(in); | |
3057 | cupsFileClose(out); | |
3058 | unlink(newfile); | |
3059 | ||
3060 | return (HTTP_SERVER_ERROR); | |
3061 | } | |
3062 | ||
3063 | /* | |
3064 | * Close the files... | |
3065 | */ | |
3066 | ||
3067 | cupsFileClose(in); | |
3068 | if (cupsFileClose(out)) | |
3069 | { | |
3070 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
3071 | "Error file closing config file \"%s\" - %s", | |
3072 | newfile, strerror(errno)); | |
3073 | ||
3074 | unlink(newfile); | |
3075 | ||
3076 | return (HTTP_SERVER_ERROR); | |
3077 | } | |
3078 | ||
3079 | /* | |
3080 | * Remove the request file... | |
3081 | */ | |
3082 | ||
3083 | unlink(con->filename); | |
3084 | cupsdClearString(&con->filename); | |
3085 | ||
3086 | /* | |
3087 | * Unlink the old backup, rename the current config file to the backup | |
3088 | * filename, and rename the new config file to the config file name... | |
3089 | */ | |
3090 | ||
3091 | if (unlink(oldfile)) | |
3092 | if (errno != ENOENT) | |
3093 | { | |
3094 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
3095 | "Unable to remove backup config file \"%s\" - %s", | |
3096 | oldfile, strerror(errno)); | |
3097 | ||
3098 | unlink(newfile); | |
3099 | ||
3100 | return (HTTP_SERVER_ERROR); | |
3101 | } | |
3102 | ||
3103 | if (rename(conffile, oldfile)) | |
3104 | if (errno != ENOENT) | |
3105 | { | |
3106 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
3107 | "Unable to rename old config file \"%s\" - %s", | |
3108 | conffile, strerror(errno)); | |
3109 | ||
3110 | unlink(newfile); | |
3111 | ||
3112 | return (HTTP_SERVER_ERROR); | |
3113 | } | |
3114 | ||
3115 | if (rename(newfile, conffile)) | |
3116 | { | |
3117 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
3118 | "Unable to rename new config file \"%s\" - %s", | |
3119 | newfile, strerror(errno)); | |
3120 | ||
3121 | rename(oldfile, conffile); | |
3122 | unlink(newfile); | |
3123 | ||
3124 | return (HTTP_SERVER_ERROR); | |
3125 | } | |
3126 | ||
3127 | /* | |
3128 | * If the cupsd.conf file was updated, set the NeedReload flag... | |
3129 | */ | |
3130 | ||
3131 | if (!strcmp(con->uri, "/admin/conf/cupsd.conf")) | |
3132 | NeedReload = RELOAD_CUPSD; | |
3133 | else | |
3134 | NeedReload = RELOAD_ALL; | |
3135 | ||
3136 | ReloadTime = time(NULL); | |
3137 | ||
3138 | /* | |
3139 | * Return that the file was created successfully... | |
3140 | */ | |
3141 | ||
3142 | return (HTTP_CREATED); | |
3143 | } | |
3144 | ||
3145 | ||
3146 | /* | |
3147 | * 'is_path_absolute()' - Is a path absolute and free of relative elements (i.e. ".."). | |
3148 | */ | |
3149 | ||
3150 | static int /* O - 0 if relative, 1 if absolute */ | |
3151 | is_path_absolute(const char *path) /* I - Input path */ | |
3152 | { | |
3153 | /* | |
3154 | * Check for a leading slash... | |
3155 | */ | |
3156 | ||
3157 | if (path[0] != '/') | |
3158 | return (0); | |
3159 | ||
3160 | /* | |
3161 | * Check for "/.." in the path... | |
3162 | */ | |
3163 | ||
3164 | while ((path = strstr(path, "/..")) != NULL) | |
3165 | { | |
3166 | if (!path[3] || path[3] == '/') | |
3167 | return (0); | |
3168 | ||
3169 | path ++; | |
3170 | } | |
3171 | ||
3172 | /* | |
3173 | * If we haven't found any relative paths, return 1 indicating an | |
3174 | * absolute path... | |
3175 | */ | |
3176 | ||
3177 | return (1); | |
3178 | } | |
3179 | ||
3180 | ||
3181 | /* | |
3182 | * 'pipe_command()' - Pipe the output of a command to the remote client. | |
3183 | */ | |
3184 | ||
3185 | static int /* O - Process ID */ | |
3186 | pipe_command(cupsd_client_t *con, /* I - Client connection */ | |
3187 | int infile, /* I - Standard input for command */ | |
3188 | int *outfile, /* O - Standard output for command */ | |
3189 | char *command, /* I - Command to run */ | |
3190 | char *options, /* I - Options for command */ | |
3191 | int root) /* I - Run as root? */ | |
3192 | { | |
3193 | int i; /* Looping var */ | |
3194 | int pid; /* Process ID */ | |
3195 | char *commptr; /* Command string pointer */ | |
3196 | char *uriptr; /* URI string pointer */ | |
3197 | int fds[2]; /* Pipe FDs */ | |
3198 | int argc; /* Number of arguments */ | |
3199 | int envc; /* Number of environment variables */ | |
3200 | char argbuf[10240], /* Argument buffer */ | |
3201 | *argv[100], /* Argument strings */ | |
3202 | *envp[100]; /* Environment variables */ | |
3203 | char content_length[1024], /* CONTENT_LENGTH environment variable */ | |
3204 | content_type[1024], /* CONTENT_TYPE environment variable */ | |
3205 | http_cookie[32768], /* HTTP_COOKIE environment variable */ | |
3206 | http_user_agent[1024], /* HTTP_USER_AGENT environment variable */ | |
3207 | lang[1024], /* LANG environment variable */ | |
3208 | *query_string, /* QUERY_STRING env variable */ | |
3209 | remote_addr[1024], /* REMOTE_ADDR environment variable */ | |
3210 | remote_host[1024], /* REMOTE_HOST environment variable */ | |
3211 | remote_user[1024], /* REMOTE_USER environment variable */ | |
3212 | script_name[1024], /* SCRIPT_NAME environment variable */ | |
3213 | server_name[1024], /* SERVER_NAME environment variable */ | |
3214 | server_port[1024]; /* SERVER_PORT environment variable */ | |
3215 | ||
3216 | ||
3217 | /* | |
3218 | * Parse a copy of the options string, which is of the form: | |
3219 | * | |
3220 | * name argument+argument+argument | |
3221 | * name?argument+argument+argument | |
3222 | * name param=value¶m=value | |
3223 | * name?param=value¶m=value | |
3224 | * | |
3225 | * If the string contains an "=" character after the initial name, | |
3226 | * then we treat it as a HTTP GET form request and make a copy of | |
3227 | * the remaining string for the environment variable. | |
3228 | * | |
3229 | * The string is always parsed out as command-line arguments, to | |
3230 | * be consistent with Apache... | |
3231 | */ | |
3232 | ||
3233 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
3234 | "pipe_command: command=\"%s\", options=\"%s\"", | |
3235 | command, options); | |
3236 | ||
3237 | strlcpy(argbuf, options, sizeof(argbuf)); | |
3238 | ||
3239 | argv[0] = argbuf; | |
3240 | query_string = NULL; | |
3241 | ||
3242 | for (commptr = argbuf, argc = 1; *commptr != '\0' && argc < 99; commptr ++) | |
3243 | { | |
3244 | /* | |
3245 | * Break arguments whenever we see a + or space... | |
3246 | */ | |
3247 | ||
3248 | if (*commptr == ' ' || *commptr == '+' || (*commptr == '?' && argc == 1)) | |
3249 | { | |
3250 | /* | |
3251 | * Terminate the current string and skip trailing whitespace... | |
3252 | */ | |
3253 | ||
3254 | *commptr++ = '\0'; | |
3255 | ||
3256 | while (*commptr == ' ') | |
3257 | commptr ++; | |
3258 | ||
3259 | /* | |
3260 | * If we don't have a blank string, save it as another argument... | |
3261 | */ | |
3262 | ||
3263 | if (*commptr) | |
3264 | { | |
3265 | argv[argc] = commptr; | |
3266 | argc ++; | |
3267 | } | |
3268 | else | |
3269 | break; | |
3270 | ||
3271 | /* | |
3272 | * If we see an "=" in the remaining string, make a copy of it since | |
3273 | * it will be query data... | |
3274 | */ | |
3275 | ||
3276 | if (argc == 2 && strchr(commptr, '=') && con->operation == HTTP_GET) | |
3277 | cupsdSetStringf(&query_string, "QUERY_STRING=%s", commptr); | |
3278 | ||
3279 | /* | |
3280 | * Don't skip the first non-blank character... | |
3281 | */ | |
3282 | ||
3283 | commptr --; | |
3284 | } | |
3285 | else if (*commptr == '%' && isxdigit(commptr[1] & 255) && | |
3286 | isxdigit(commptr[2] & 255)) | |
3287 | { | |
3288 | /* | |
3289 | * Convert the %xx notation to the individual character. | |
3290 | */ | |
3291 | ||
3292 | if (commptr[1] >= '0' && commptr[1] <= '9') | |
3293 | *commptr = (commptr[1] - '0') << 4; | |
3294 | else | |
3295 | *commptr = (tolower(commptr[1]) - 'a' + 10) << 4; | |
3296 | ||
3297 | if (commptr[2] >= '0' && commptr[2] <= '9') | |
3298 | *commptr |= commptr[2] - '0'; | |
3299 | else | |
3300 | *commptr |= tolower(commptr[2]) - 'a' + 10; | |
3301 | ||
3302 | _cups_strcpy(commptr + 1, commptr + 3); | |
3303 | ||
3304 | /* | |
3305 | * Check for a %00 and break if that is the case... | |
3306 | */ | |
3307 | ||
3308 | if (!*commptr) | |
3309 | break; | |
3310 | } | |
3311 | } | |
3312 | ||
3313 | argv[argc] = NULL; | |
3314 | ||
3315 | if (argv[0][0] == '\0') | |
3316 | argv[0] = strrchr(command, '/') + 1; | |
3317 | ||
3318 | /* | |
3319 | * Setup the environment variables as needed... | |
3320 | */ | |
3321 | ||
3322 | if (con->language) | |
3323 | snprintf(lang, sizeof(lang), "LANG=%s.UTF-8", con->language->language); | |
3324 | else | |
3325 | strcpy(lang, "LANG=C"); | |
3326 | ||
3327 | strcpy(remote_addr, "REMOTE_ADDR="); | |
3328 | httpAddrString(con->http.hostaddr, remote_addr + 12, | |
3329 | sizeof(remote_addr) - 12); | |
3330 | ||
3331 | snprintf(remote_host, sizeof(remote_host), "REMOTE_HOST=%s", | |
3332 | con->http.hostname); | |
3333 | ||
3334 | snprintf(script_name, sizeof(script_name), "SCRIPT_NAME=%s", con->uri); | |
3335 | if ((uriptr = strchr(script_name, '?')) != NULL) | |
3336 | *uriptr = '\0'; | |
3337 | ||
3338 | sprintf(server_port, "SERVER_PORT=%d", con->serverport); | |
3339 | ||
3340 | snprintf(server_name, sizeof(server_name), "SERVER_NAME=%s", | |
3341 | con->servername); | |
3342 | ||
3343 | envc = cupsdLoadEnv(envp, (int)(sizeof(envp) / sizeof(envp[0]))); | |
3344 | ||
3345 | envp[envc ++] = lang; | |
3346 | envp[envc ++] = "REDIRECT_STATUS=1"; | |
3347 | envp[envc ++] = server_name; | |
3348 | envp[envc ++] = server_port; | |
3349 | envp[envc ++] = remote_addr; | |
3350 | envp[envc ++] = remote_host; | |
3351 | envp[envc ++] = script_name; | |
3352 | ||
3353 | if (con->username[0]) | |
3354 | { | |
3355 | snprintf(remote_user, sizeof(remote_user), "REMOTE_USER=%s", con->username); | |
3356 | ||
3357 | envp[envc ++] = remote_user; | |
3358 | } | |
3359 | ||
3360 | if (con->http.version == HTTP_1_1) | |
3361 | envp[envc ++] = "SERVER_PROTOCOL=HTTP/1.1"; | |
3362 | else if (con->http.version == HTTP_1_0) | |
3363 | envp[envc ++] = "SERVER_PROTOCOL=HTTP/1.0"; | |
3364 | else | |
3365 | envp[envc ++] = "SERVER_PROTOCOL=HTTP/0.9"; | |
3366 | ||
3367 | if (con->http.cookie) | |
3368 | { | |
3369 | snprintf(http_cookie, sizeof(http_cookie), "HTTP_COOKIE=%s", | |
3370 | con->http.cookie); | |
3371 | envp[envc ++] = http_cookie; | |
3372 | } | |
3373 | ||
3374 | if (con->http.fields[HTTP_FIELD_USER_AGENT][0]) | |
3375 | { | |
3376 | snprintf(http_user_agent, sizeof(http_user_agent), "HTTP_USER_AGENT=%s", | |
3377 | con->http.fields[HTTP_FIELD_USER_AGENT]); | |
3378 | envp[envc ++] = http_user_agent; | |
3379 | } | |
3380 | ||
3381 | if (con->operation == HTTP_GET) | |
3382 | { | |
3383 | for (i = 0; i < argc; i ++) | |
3384 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "argv[%d] = \"%s\"", i, argv[i]); | |
3385 | ||
3386 | envp[envc ++] = "REQUEST_METHOD=GET"; | |
3387 | ||
3388 | if (query_string) | |
3389 | { | |
3390 | /* | |
3391 | * Add GET form variables after ?... | |
3392 | */ | |
3393 | ||
3394 | envp[envc ++] = query_string; | |
3395 | } | |
3396 | } | |
3397 | else | |
3398 | { | |
3399 | sprintf(content_length, "CONTENT_LENGTH=" CUPS_LLFMT, con->bytes); | |
3400 | snprintf(content_type, sizeof(content_type), "CONTENT_TYPE=%s", | |
3401 | con->http.fields[HTTP_FIELD_CONTENT_TYPE]); | |
3402 | ||
3403 | envp[envc ++] = "REQUEST_METHOD=POST"; | |
3404 | envp[envc ++] = content_length; | |
3405 | envp[envc ++] = content_type; | |
3406 | } | |
3407 | ||
3408 | /* | |
3409 | * Tell the CGI if we are using encryption... | |
3410 | */ | |
3411 | ||
3412 | if (con->http.encryption == HTTP_ENCRYPT_ALWAYS) | |
3413 | envp[envc ++] = "HTTPS=ON"; | |
3414 | ||
3415 | /* | |
3416 | * Terminate the environment array... | |
3417 | */ | |
3418 | ||
3419 | envp[envc] = NULL; | |
3420 | ||
3421 | if (LogLevel == CUPSD_LOG_DEBUG2) | |
3422 | { | |
3423 | for (i = 0; i < argc; i ++) | |
3424 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
3425 | "pipe_command: argv[%d] = \"%s\"", i, argv[i]); | |
3426 | for (i = 0; i < envc; i ++) | |
3427 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
3428 | "pipe_command: envp[%d] = \"%s\"", i, envp[i]); | |
3429 | } | |
3430 | ||
3431 | /* | |
3432 | * Create a pipe for the output... | |
3433 | */ | |
3434 | ||
3435 | if (cupsdOpenPipe(fds)) | |
3436 | { | |
3437 | cupsdClearString(&query_string); | |
3438 | ||
3439 | cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to create pipes for CGI %s - %s", | |
3440 | argv[0], strerror(errno)); | |
3441 | return (0); | |
3442 | } | |
3443 | ||
3444 | /* | |
3445 | * Then execute the command... | |
3446 | */ | |
3447 | ||
3448 | if (cupsdStartProcess(command, argv, envp, infile, fds[1], CGIPipes[1], | |
3449 | -1, root, &pid) < 0) | |
3450 | { | |
3451 | /* | |
3452 | * Error - can't fork! | |
3453 | */ | |
3454 | ||
3455 | cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to fork for CGI %s - %s", argv[0], | |
3456 | strerror(errno)); | |
3457 | ||
3458 | cupsdClosePipe(fds); | |
3459 | pid = 0; | |
3460 | } | |
3461 | else | |
3462 | { | |
3463 | /* | |
3464 | * Fork successful - return the PID... | |
3465 | */ | |
3466 | ||
3467 | if (con->username[0]) | |
3468 | cupsdAddCert(pid, con->username); | |
3469 | ||
3470 | cupsdLogMessage(CUPSD_LOG_DEBUG, "CGI %s started - PID = %d", command, pid); | |
3471 | ||
3472 | *outfile = fds[0]; | |
3473 | close(fds[1]); | |
3474 | } | |
3475 | ||
3476 | cupsdClearString(&query_string); | |
3477 | ||
3478 | return (pid); | |
3479 | } | |
3480 | ||
3481 | ||
3482 | /* | |
3483 | * End of "$Id: client.c 4912 2006-01-10 21:43:56Z mike $". | |
3484 | */ |