]>
Commit | Line | Data |
---|---|---|
ef416fc2 | 1 | /* |
e00b005a | 2 | * "$Id: client.c 5046 2006-02-01 22:11:58Z mike $" |
ef416fc2 | 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) | |
fa73b229 | 781 | { |
ef416fc2 | 782 | error = SSLSetCertificate(conn, ServerCertificatesArray); |
783 | ||
fa73b229 | 784 | /* |
785 | * Perform SSL/TLS handshake | |
786 | */ | |
787 | ||
788 | if (!error) | |
789 | { | |
790 | do | |
791 | { | |
792 | error = SSLHandshake(conn); | |
793 | } | |
794 | while (error == errSSLWouldBlock); | |
795 | } | |
ef416fc2 | 796 | } |
ef416fc2 | 797 | |
798 | if (error) | |
799 | { | |
800 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
801 | "cupsdEncryptClient: Unable to encrypt connection from %s!", | |
802 | con->http.hostname); | |
803 | ||
804 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
805 | "cupsdEncryptClient: CDSA error code is %d", (int)error); | |
806 | ||
807 | con->http.error = error; | |
808 | con->http.status = HTTP_ERROR; | |
809 | ||
810 | if (conn != NULL) | |
811 | SSLDisposeContext(conn); | |
812 | ||
813 | return (0); | |
814 | } | |
815 | ||
816 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
817 | "cupsdEncryptClient: %d Connection from %s now encrypted.", | |
818 | con->http.fd, con->http.hostname); | |
819 | ||
820 | con->http.tls = conn; | |
821 | return (1); | |
822 | ||
823 | #else | |
824 | return (0); | |
825 | #endif /* HAVE_LIBSSL */ | |
826 | } | |
827 | ||
828 | ||
829 | /* | |
830 | * 'cupsdIsCGI()' - Is the resource a CGI script/program? | |
831 | */ | |
832 | ||
833 | int /* O - 1 = CGI, 0 = file */ | |
834 | cupsdIsCGI(cupsd_client_t *con, /* I - Client connection */ | |
835 | const char *filename, /* I - Real filename */ | |
836 | struct stat *filestats, /* I - File information */ | |
837 | mime_type_t *type) /* I - MIME type */ | |
838 | { | |
839 | const char *options; /* Options on URL */ | |
840 | ||
841 | ||
842 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
843 | "cupsdIsCGI(con=%p, filename=\"%s\", filestats=%p, type=%s/%s)\n", | |
844 | con, filename, filestats, type ? type->super : "unknown", | |
845 | type ? type->type : "unknown"); | |
846 | ||
847 | /* | |
848 | * Get the options, if any... | |
849 | */ | |
850 | ||
851 | if ((options = strchr(con->uri, '?')) != NULL) | |
852 | options ++; | |
853 | ||
854 | /* | |
855 | * Check for known types... | |
856 | */ | |
857 | ||
858 | if (!type || strcasecmp(type->super, "application")) | |
859 | { | |
860 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdIsCGI: Returning 0..."); | |
861 | return (0); | |
862 | } | |
863 | ||
864 | if (!strcasecmp(type->type, "x-httpd-cgi") && | |
865 | (filestats->st_mode & 0111)) | |
866 | { | |
867 | /* | |
868 | * "application/x-httpd-cgi" is a CGI script. | |
869 | */ | |
870 | ||
871 | cupsdSetString(&con->command, filename); | |
872 | ||
873 | filename = strrchr(filename, '/') + 1; /* Filename always absolute */ | |
874 | ||
875 | if (options) | |
876 | cupsdSetStringf(&con->options, "%s %s", filename, options); | |
877 | else | |
878 | cupsdSetStringf(&con->options, "%s", filename); | |
879 | ||
880 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
881 | "cupsdIsCGI: Returning 1 with command=\"%s\" and options=\"%s\"", | |
882 | con->command, con->options); | |
883 | ||
884 | return (1); | |
885 | } | |
886 | #ifdef HAVE_JAVA | |
887 | else if (!strcasecmp(type->type, "x-httpd-java")) | |
888 | { | |
889 | /* | |
890 | * "application/x-httpd-java" is a Java servlet. | |
891 | */ | |
892 | ||
893 | cupsdSetString(&con->command, CUPS_JAVA); | |
894 | ||
895 | if (options) | |
896 | cupsdSetStringf(&con->options, "java %s %s", filename, options); | |
897 | else | |
898 | cupsdSetStringf(&con->options, "java %s", filename); | |
899 | ||
900 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
901 | "cupsdIsCGI: Returning 1 with command=\"%s\" and options=\"%s\"", | |
902 | con->command, con->options); | |
903 | ||
904 | return (1); | |
905 | } | |
906 | #endif /* HAVE_JAVA */ | |
907 | #ifdef HAVE_PERL | |
908 | else if (!strcasecmp(type->type, "x-httpd-perl")) | |
909 | { | |
910 | /* | |
911 | * "application/x-httpd-perl" is a Perl page. | |
912 | */ | |
913 | ||
914 | cupsdSetString(&con->command, CUPS_PERL); | |
915 | ||
916 | if (options) | |
917 | cupsdSetStringf(&con->options, "perl %s %s", filename, options); | |
918 | else | |
919 | cupsdSetStringf(&con->options, "perl %s", filename); | |
920 | ||
921 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
922 | "cupsdIsCGI: Returning 1 with command=\"%s\" and options=\"%s\"", | |
923 | con->command, con->options); | |
924 | ||
925 | return (1); | |
926 | } | |
927 | #endif /* HAVE_PERL */ | |
928 | #ifdef HAVE_PHP | |
929 | else if (!strcasecmp(type->type, "x-httpd-php")) | |
930 | { | |
931 | /* | |
932 | * "application/x-httpd-php" is a PHP page. | |
933 | */ | |
934 | ||
935 | cupsdSetString(&con->command, CUPS_PHP); | |
936 | ||
937 | if (options) | |
938 | cupsdSetStringf(&con->options, "php %s %s", filename, options); | |
939 | else | |
940 | cupsdSetStringf(&con->options, "php %s", filename); | |
941 | ||
942 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
943 | "cupsdIsCGI: Returning 1 with command=\"%s\" and options=\"%s\"", | |
944 | con->command, con->options); | |
945 | ||
946 | return (1); | |
947 | } | |
948 | #endif /* HAVE_PHP */ | |
949 | #ifdef HAVE_PYTHON | |
950 | else if (!strcasecmp(type->type, "x-httpd-python")) | |
951 | { | |
952 | /* | |
953 | * "application/x-httpd-python" is a Python page. | |
954 | */ | |
955 | ||
956 | cupsdSetString(&con->command, CUPS_PYTHON); | |
957 | ||
958 | if (options) | |
959 | cupsdSetStringf(&con->options, "python %s %s", filename, options); | |
960 | else | |
961 | cupsdSetStringf(&con->options, "python %s", filename); | |
962 | ||
963 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
964 | "cupsdIsCGI: Returning 1 with command=\"%s\" and options=\"%s\"", | |
965 | con->command, con->options); | |
966 | ||
967 | return (1); | |
968 | } | |
969 | #endif /* HAVE_PYTHON */ | |
970 | ||
971 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdIsCGI: Returning 0..."); | |
972 | ||
973 | return (0); | |
974 | } | |
975 | ||
976 | ||
977 | /* | |
978 | * 'cupsdReadClient()' - Read data from a client. | |
979 | */ | |
980 | ||
981 | int /* O - 1 on success, 0 on error */ | |
982 | cupsdReadClient(cupsd_client_t *con) /* I - Client to read from */ | |
983 | { | |
984 | char line[32768], /* Line from client... */ | |
985 | operation[64], /* Operation code from socket */ | |
986 | version[64], /* HTTP version number string */ | |
987 | locale[64], /* Locale */ | |
988 | *ptr; /* Pointer into strings */ | |
989 | int major, minor; /* HTTP version numbers */ | |
990 | http_status_t status; /* Transfer status */ | |
991 | ipp_state_t ipp_state; /* State of IPP transfer */ | |
992 | int bytes; /* Number of bytes to POST */ | |
993 | char *filename; /* Name of file for GET/HEAD */ | |
994 | char buf[1024]; /* Buffer for real filename */ | |
995 | struct stat filestats; /* File information */ | |
996 | mime_type_t *type; /* MIME type of file */ | |
997 | cupsd_printer_t *p; /* Printer */ | |
998 | static unsigned request_id = 0; /* Request ID for temp files */ | |
999 | ||
1000 | ||
1001 | status = HTTP_CONTINUE; | |
1002 | ||
1003 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
1004 | "cupsdReadClient: %d, used=%d, file=%d state=%d", | |
1005 | con->http.fd, con->http.used, con->file, con->http.state); | |
1006 | ||
1007 | if (con->http.error) | |
1008 | { | |
1009 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdReadClient: http error seen..."); | |
1010 | return (cupsdCloseClient(con)); | |
1011 | } | |
1012 | ||
1013 | #ifdef HAVE_SSL | |
1014 | if (con->auto_ssl) | |
1015 | { | |
1016 | /* | |
1017 | * Automatically check for a SSL/TLS handshake... | |
1018 | */ | |
1019 | ||
1020 | con->auto_ssl = 0; | |
1021 | ||
1022 | if (recv(con->http.fd, buf, 1, MSG_PEEK) == 1 && | |
1023 | (!buf[0] || !strchr("DGHOPT", buf[0]))) | |
1024 | { | |
1025 | /* | |
1026 | * Encrypt this connection... | |
1027 | */ | |
1028 | ||
1029 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
1030 | "cupsdReadClient: Saw first byte %02X, auto-negotiating SSL/TLS session...", | |
1031 | buf[0] & 255); | |
1032 | ||
1033 | cupsdEncryptClient(con); | |
1034 | return (1); | |
1035 | } | |
1036 | } | |
1037 | #endif /* HAVE_SSL */ | |
1038 | ||
1039 | switch (con->http.state) | |
1040 | { | |
1041 | case HTTP_WAITING : | |
1042 | /* | |
1043 | * See if we've received a request line... | |
1044 | */ | |
1045 | ||
1046 | if (httpGets(line, sizeof(line) - 1, HTTP(con)) == NULL) | |
1047 | { | |
1048 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
1049 | "cupsdReadClient: httpGets returned EOF..."); | |
1050 | return (cupsdCloseClient(con)); | |
1051 | } | |
1052 | ||
1053 | /* | |
1054 | * Ignore blank request lines... | |
1055 | */ | |
1056 | ||
1057 | if (line[0] == '\0') | |
1058 | break; | |
1059 | ||
1060 | /* | |
1061 | * Clear other state variables... | |
1062 | */ | |
1063 | ||
1064 | httpClearFields(HTTP(con)); | |
1065 | ||
1066 | con->http.activity = time(NULL); | |
1067 | con->http.version = HTTP_1_0; | |
1068 | con->http.keep_alive = HTTP_KEEPALIVE_OFF; | |
1069 | con->http.data_encoding = HTTP_ENCODE_LENGTH; | |
1070 | con->http.data_remaining = 0; | |
1071 | con->http._data_remaining = 0; | |
1072 | con->operation = HTTP_WAITING; | |
1073 | con->bytes = 0; | |
1074 | con->file = -1; | |
1075 | con->file_ready = 0; | |
1076 | con->pipe_pid = 0; | |
1077 | con->username[0] = '\0'; | |
1078 | con->password[0] = '\0'; | |
1079 | con->uri[0] = '\0'; | |
1080 | ||
1081 | cupsdClearString(&con->command); | |
1082 | cupsdClearString(&con->options); | |
1083 | ||
1084 | if (con->language != NULL) | |
1085 | { | |
1086 | cupsLangFree(con->language); | |
1087 | con->language = NULL; | |
1088 | } | |
1089 | ||
1090 | /* | |
1091 | * Grab the request line... | |
1092 | */ | |
1093 | ||
1094 | switch (sscanf(line, "%63s%1023s%63s", operation, con->uri, version)) | |
1095 | { | |
1096 | case 1 : | |
1097 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
1098 | "Bad request line \"%s\" from %s!", line, | |
1099 | con->http.hostname); | |
1100 | cupsdSendError(con, HTTP_BAD_REQUEST); | |
1101 | return (cupsdCloseClient(con)); | |
1102 | case 2 : | |
1103 | con->http.version = HTTP_0_9; | |
1104 | break; | |
1105 | case 3 : | |
1106 | if (sscanf(version, "HTTP/%d.%d", &major, &minor) != 2) | |
1107 | { | |
1108 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
1109 | "Bad request line \"%s\" from %s!", line, | |
1110 | con->http.hostname); | |
1111 | cupsdSendError(con, HTTP_BAD_REQUEST); | |
1112 | return (cupsdCloseClient(con)); | |
1113 | } | |
1114 | ||
1115 | if (major < 2) | |
1116 | { | |
1117 | con->http.version = (http_version_t)(major * 100 + minor); | |
1118 | if (con->http.version == HTTP_1_1 && KeepAlive) | |
1119 | con->http.keep_alive = HTTP_KEEPALIVE_ON; | |
1120 | else | |
1121 | con->http.keep_alive = HTTP_KEEPALIVE_OFF; | |
1122 | } | |
1123 | else | |
1124 | { | |
1125 | cupsdSendError(con, HTTP_NOT_SUPPORTED); | |
1126 | return (cupsdCloseClient(con)); | |
1127 | } | |
1128 | break; | |
1129 | } | |
1130 | ||
1131 | /* | |
1132 | * Handle full URLs in the request line... | |
1133 | */ | |
1134 | ||
1135 | if (strcmp(con->uri, "*")) | |
1136 | { | |
1137 | char method[HTTP_MAX_URI], /* Method/scheme */ | |
1138 | userpass[HTTP_MAX_URI], /* Username:password */ | |
1139 | hostname[HTTP_MAX_URI], /* Hostname */ | |
1140 | resource[HTTP_MAX_URI]; /* Resource path */ | |
1141 | int port; /* Port number */ | |
1142 | ||
1143 | ||
1144 | /* | |
1145 | * Separate the URI into its components... | |
1146 | */ | |
1147 | ||
a4d04587 | 1148 | httpSeparateURI(HTTP_URI_CODING_MOST, con->uri, |
1149 | method, sizeof(method), | |
ef416fc2 | 1150 | userpass, sizeof(userpass), |
1151 | hostname, sizeof(hostname), &port, | |
1152 | resource, sizeof(resource)); | |
1153 | ||
1154 | /* | |
1155 | * Only allow URIs with the servername, localhost, or an IP | |
1156 | * address... | |
1157 | */ | |
1158 | ||
1159 | if (strcmp(method, "file") && | |
1160 | strcasecmp(hostname, ServerName) && | |
1161 | strcasecmp(hostname, "localhost") && | |
1162 | !isdigit(hostname[0])) | |
1163 | { | |
1164 | /* | |
1165 | * Nope, we don't do proxies... | |
1166 | */ | |
1167 | ||
1168 | cupsdLogMessage(CUPSD_LOG_ERROR, "Bad URI \"%s\" in request!", | |
1169 | con->uri); | |
1170 | cupsdSendError(con, HTTP_METHOD_NOT_ALLOWED); | |
1171 | return (cupsdCloseClient(con)); | |
1172 | } | |
1173 | ||
1174 | /* | |
1175 | * Copy the resource portion back into the URI; both resource and | |
1176 | * con->uri are HTTP_MAX_URI bytes in size... | |
1177 | */ | |
1178 | ||
1179 | strcpy(con->uri, resource); | |
1180 | } | |
1181 | ||
1182 | /* | |
1183 | * Process the request... | |
1184 | */ | |
1185 | ||
1186 | if (!strcmp(operation, "GET")) | |
1187 | con->http.state = HTTP_GET; | |
1188 | else if (!strcmp(operation, "PUT")) | |
1189 | con->http.state = HTTP_PUT; | |
1190 | else if (!strcmp(operation, "POST")) | |
1191 | con->http.state = HTTP_POST; | |
1192 | else if (!strcmp(operation, "DELETE")) | |
1193 | con->http.state = HTTP_DELETE; | |
1194 | else if (!strcmp(operation, "TRACE")) | |
1195 | con->http.state = HTTP_TRACE; | |
1196 | else if (!strcmp(operation, "OPTIONS")) | |
1197 | con->http.state = HTTP_OPTIONS; | |
1198 | else if (!strcmp(operation, "HEAD")) | |
1199 | con->http.state = HTTP_HEAD; | |
1200 | else | |
1201 | { | |
1202 | cupsdLogMessage(CUPSD_LOG_ERROR, "Bad operation \"%s\"!", operation); | |
1203 | cupsdSendError(con, HTTP_BAD_REQUEST); | |
1204 | return (cupsdCloseClient(con)); | |
1205 | } | |
1206 | ||
1207 | con->start = time(NULL); | |
1208 | con->operation = con->http.state; | |
1209 | ||
1210 | cupsdLogMessage(CUPSD_LOG_DEBUG, "cupsdReadClient: %d %s %s HTTP/%d.%d", con->http.fd, | |
1211 | operation, con->uri, | |
1212 | con->http.version / 100, con->http.version % 100); | |
1213 | ||
1214 | con->http.status = HTTP_OK; | |
1215 | ||
1216 | case HTTP_OPTIONS : | |
1217 | case HTTP_DELETE : | |
1218 | case HTTP_GET : | |
1219 | case HTTP_HEAD : | |
1220 | case HTTP_POST : | |
1221 | case HTTP_PUT : | |
1222 | case HTTP_TRACE : | |
1223 | /* | |
1224 | * Parse incoming parameters until the status changes... | |
1225 | */ | |
1226 | ||
1227 | status = httpUpdate(HTTP(con)); | |
1228 | ||
1229 | if (status != HTTP_OK && status != HTTP_CONTINUE) | |
1230 | { | |
1231 | cupsdSendError(con, HTTP_BAD_REQUEST); | |
1232 | return (cupsdCloseClient(con)); | |
1233 | } | |
1234 | break; | |
1235 | ||
1236 | default : | |
1237 | break; /* Anti-compiler-warning-code */ | |
1238 | } | |
1239 | ||
1240 | /* | |
1241 | * Handle new transfers... | |
1242 | */ | |
1243 | ||
1244 | if (status == HTTP_OK) | |
1245 | { | |
1246 | if (con->http.fields[HTTP_FIELD_ACCEPT_LANGUAGE][0]) | |
1247 | { | |
1248 | /* | |
1249 | * Figure out the locale from the Accept-Language and Content-Type | |
1250 | * fields... | |
1251 | */ | |
1252 | ||
1253 | if ((ptr = strchr(con->http.fields[HTTP_FIELD_ACCEPT_LANGUAGE], ',')) != NULL) | |
1254 | *ptr = '\0'; | |
1255 | ||
1256 | if ((ptr = strchr(con->http.fields[HTTP_FIELD_ACCEPT_LANGUAGE], ';')) != NULL) | |
1257 | *ptr = '\0'; | |
1258 | ||
1259 | if ((ptr = strstr(con->http.fields[HTTP_FIELD_CONTENT_TYPE], "charset=")) != NULL) | |
1260 | { | |
1261 | /* | |
1262 | * Combine language and charset, and trim any extra params in the | |
1263 | * content-type. | |
1264 | */ | |
1265 | ||
1266 | snprintf(locale, sizeof(locale), "%s.%s", | |
1267 | con->http.fields[HTTP_FIELD_ACCEPT_LANGUAGE], ptr + 8); | |
1268 | ||
1269 | if ((ptr = strchr(locale, ',')) != NULL) | |
1270 | *ptr = '\0'; | |
1271 | } | |
1272 | else | |
1273 | snprintf(locale, sizeof(locale), "%s.%s", | |
1274 | con->http.fields[HTTP_FIELD_ACCEPT_LANGUAGE], DefaultCharset); | |
1275 | ||
1276 | con->language = cupsLangGet(locale); | |
1277 | } | |
1278 | else | |
1279 | con->language = cupsLangGet(DefaultLocale); | |
1280 | ||
1281 | cupsdAuthorize(con); | |
1282 | ||
1283 | if (!strncmp(con->http.fields[HTTP_FIELD_CONNECTION], "Keep-Alive", 10) && | |
1284 | KeepAlive) | |
1285 | con->http.keep_alive = HTTP_KEEPALIVE_ON; | |
1286 | ||
1287 | if (!con->http.fields[HTTP_FIELD_HOST][0] && | |
1288 | con->http.version >= HTTP_1_1) | |
1289 | { | |
1290 | /* | |
1291 | * HTTP/1.1 and higher require the "Host:" field... | |
1292 | */ | |
1293 | ||
1294 | if (!cupsdSendError(con, HTTP_BAD_REQUEST)) | |
1295 | return (cupsdCloseClient(con)); | |
1296 | } | |
1297 | else if (con->operation == HTTP_OPTIONS) | |
1298 | { | |
1299 | /* | |
1300 | * Do OPTIONS command... | |
1301 | */ | |
1302 | ||
1303 | if (con->best && con->best->type != AUTH_NONE) | |
1304 | { | |
1305 | if (!cupsdSendHeader(con, HTTP_UNAUTHORIZED, NULL)) | |
1306 | return (cupsdCloseClient(con)); | |
1307 | } | |
1308 | ||
1309 | if (!strcasecmp(con->http.fields[HTTP_FIELD_CONNECTION], "Upgrade") && | |
1310 | con->http.tls == NULL) | |
1311 | { | |
1312 | #ifdef HAVE_SSL | |
1313 | /* | |
1314 | * Do encryption stuff... | |
1315 | */ | |
1316 | ||
1317 | if (!cupsdSendHeader(con, HTTP_SWITCHING_PROTOCOLS, NULL)) | |
1318 | return (cupsdCloseClient(con)); | |
1319 | ||
1320 | httpPrintf(HTTP(con), "Connection: Upgrade\r\n"); | |
1321 | httpPrintf(HTTP(con), "Upgrade: TLS/1.0,HTTP/1.1\r\n"); | |
1322 | httpPrintf(HTTP(con), "Content-Length: 0\r\n"); | |
1323 | httpPrintf(HTTP(con), "\r\n"); | |
1324 | ||
1325 | cupsdEncryptClient(con); | |
1326 | #else | |
1327 | if (!cupsdSendError(con, HTTP_NOT_IMPLEMENTED)) | |
1328 | return (cupsdCloseClient(con)); | |
1329 | #endif /* HAVE_SSL */ | |
1330 | } | |
1331 | ||
1332 | if (con->http.expect) | |
1333 | { | |
1334 | /**** TODO: send expected header ****/ | |
1335 | } | |
1336 | ||
1337 | if (!cupsdSendHeader(con, HTTP_OK, NULL)) | |
1338 | return (cupsdCloseClient(con)); | |
1339 | ||
1340 | httpPrintf(HTTP(con), "Allow: GET, HEAD, OPTIONS, POST, PUT\r\n"); | |
1341 | httpPrintf(HTTP(con), "Content-Length: 0\r\n"); | |
1342 | httpPrintf(HTTP(con), "\r\n"); | |
1343 | } | |
1344 | else if (!is_path_absolute(con->uri)) | |
1345 | { | |
1346 | /* | |
1347 | * Protect against malicious users! | |
1348 | */ | |
1349 | ||
1350 | if (!cupsdSendError(con, HTTP_FORBIDDEN)) | |
1351 | return (cupsdCloseClient(con)); | |
1352 | } | |
1353 | else | |
1354 | { | |
1355 | if (!strcasecmp(con->http.fields[HTTP_FIELD_CONNECTION], "Upgrade") && | |
1356 | con->http.tls == NULL) | |
1357 | { | |
1358 | #ifdef HAVE_SSL | |
1359 | /* | |
1360 | * Do encryption stuff... | |
1361 | */ | |
1362 | ||
1363 | if (!cupsdSendHeader(con, HTTP_SWITCHING_PROTOCOLS, NULL)) | |
1364 | return (cupsdCloseClient(con)); | |
1365 | ||
1366 | httpPrintf(HTTP(con), "Connection: Upgrade\r\n"); | |
1367 | httpPrintf(HTTP(con), "Upgrade: TLS/1.0,HTTP/1.1\r\n"); | |
1368 | httpPrintf(HTTP(con), "Content-Length: 0\r\n"); | |
1369 | httpPrintf(HTTP(con), "\r\n"); | |
1370 | ||
1371 | cupsdEncryptClient(con); | |
1372 | #else | |
1373 | if (!cupsdSendError(con, HTTP_NOT_IMPLEMENTED)) | |
1374 | return (cupsdCloseClient(con)); | |
1375 | #endif /* HAVE_SSL */ | |
1376 | } | |
1377 | ||
1378 | if ((status = cupsdIsAuthorized(con, NULL)) != HTTP_OK) | |
1379 | { | |
1380 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
1381 | "cupsdReadClient: Unauthorized request for %s...\n", | |
1382 | con->uri); | |
1383 | cupsdSendError(con, status); | |
1384 | return (cupsdCloseClient(con)); | |
1385 | } | |
1386 | ||
1387 | if (con->http.expect) | |
1388 | { | |
1389 | /**** TODO: send expected header ****/ | |
1390 | } | |
1391 | ||
1392 | switch (con->http.state) | |
1393 | { | |
1394 | case HTTP_GET_SEND : | |
1395 | if (!strncmp(con->uri, "/printers/", 10) && | |
1396 | !strcmp(con->uri + strlen(con->uri) - 4, ".ppd")) | |
1397 | { | |
1398 | /* | |
1399 | * Send PPD file - get the real printer name since printer | |
1400 | * names are not case sensitive but filenames can be... | |
1401 | */ | |
1402 | ||
1403 | con->uri[strlen(con->uri) - 4] = '\0'; /* Drop ".ppd" */ | |
1404 | ||
1405 | if ((p = cupsdFindPrinter(con->uri + 10)) != NULL) | |
1406 | snprintf(con->uri, sizeof(con->uri), "/ppd/%s.ppd", p->name); | |
1407 | else | |
1408 | { | |
1409 | if (!cupsdSendError(con, HTTP_NOT_FOUND)) | |
1410 | return (cupsdCloseClient(con)); | |
1411 | ||
1412 | break; | |
1413 | } | |
1414 | } | |
1415 | ||
1416 | if ((!strncmp(con->uri, "/admin", 6) && | |
1417 | strncmp(con->uri, "/admin/conf/", 12) && | |
1418 | strncmp(con->uri, "/admin/log/", 11)) || | |
1419 | !strncmp(con->uri, "/printers", 9) || | |
1420 | !strncmp(con->uri, "/classes", 8) || | |
1421 | !strncmp(con->uri, "/help", 5) || | |
1422 | !strncmp(con->uri, "/jobs", 5)) | |
1423 | { | |
1424 | /* | |
1425 | * Send CGI output... | |
1426 | */ | |
1427 | ||
1428 | if (!strncmp(con->uri, "/admin", 6)) | |
1429 | { | |
1430 | cupsdSetStringf(&con->command, "%s/cgi-bin/admin.cgi", | |
1431 | ServerBin); | |
1432 | ||
1433 | if ((ptr = strchr(con->uri + 6, '?')) != NULL) | |
1434 | cupsdSetStringf(&con->options, "admin%s", ptr); | |
1435 | else | |
1436 | cupsdSetString(&con->options, "admin"); | |
1437 | } | |
1438 | else if (!strncmp(con->uri, "/printers", 9)) | |
1439 | { | |
1440 | cupsdSetStringf(&con->command, "%s/cgi-bin/printers.cgi", | |
1441 | ServerBin); | |
1442 | cupsdSetString(&con->options, con->uri + 9); | |
1443 | } | |
1444 | else if (!strncmp(con->uri, "/classes", 8)) | |
1445 | { | |
1446 | cupsdSetStringf(&con->command, "%s/cgi-bin/classes.cgi", | |
1447 | ServerBin); | |
1448 | cupsdSetString(&con->options, con->uri + 8); | |
1449 | } | |
1450 | else if (!strncmp(con->uri, "/jobs", 5)) | |
1451 | { | |
1452 | cupsdSetStringf(&con->command, "%s/cgi-bin/jobs.cgi", | |
1453 | ServerBin); | |
1454 | cupsdSetString(&con->options, con->uri + 5); | |
1455 | } | |
1456 | else | |
1457 | { | |
1458 | cupsdSetStringf(&con->command, "%s/cgi-bin/help.cgi", | |
1459 | ServerBin); | |
1460 | cupsdSetString(&con->options, con->uri + 5); | |
1461 | } | |
1462 | ||
1463 | if (con->options[0] == '/') | |
1464 | _cups_strcpy(con->options, con->options + 1); | |
1465 | ||
1466 | if (!cupsdSendCommand(con, con->command, con->options, 0)) | |
1467 | { | |
1468 | if (!cupsdSendError(con, HTTP_NOT_FOUND)) | |
1469 | return (cupsdCloseClient(con)); | |
1470 | } | |
1471 | else | |
1472 | cupsdLogRequest(con, HTTP_OK); | |
1473 | ||
1474 | if (con->http.version <= HTTP_1_0) | |
1475 | con->http.keep_alive = HTTP_KEEPALIVE_OFF; | |
1476 | } | |
1477 | else if ((!strncmp(con->uri, "/admin/conf/", 12) && | |
1478 | (strchr(con->uri + 12, '/') || | |
1479 | strlen(con->uri) == 12)) || | |
1480 | (!strncmp(con->uri, "/admin/log/", 11) && | |
1481 | (strchr(con->uri + 11, '/') || | |
1482 | strlen(con->uri) == 11))) | |
1483 | { | |
1484 | /* | |
1485 | * GET can only be done to configuration files under | |
1486 | * /admin/conf... | |
1487 | */ | |
1488 | ||
1489 | if (!cupsdSendError(con, HTTP_FORBIDDEN)) | |
1490 | return (cupsdCloseClient(con)); | |
1491 | ||
1492 | break; | |
1493 | } | |
1494 | else | |
1495 | { | |
1496 | /* | |
1497 | * Serve a file... | |
1498 | */ | |
1499 | ||
1500 | if ((filename = get_file(con, &filestats, buf, | |
1501 | sizeof(buf))) == NULL) | |
1502 | { | |
1503 | if (!cupsdSendError(con, HTTP_NOT_FOUND)) | |
1504 | return (cupsdCloseClient(con)); | |
1505 | ||
1506 | break; | |
1507 | } | |
1508 | ||
1509 | type = mimeFileType(MimeDatabase, filename, NULL); | |
1510 | ||
1511 | if (cupsdIsCGI(con, filename, &filestats, type)) | |
1512 | { | |
1513 | /* | |
1514 | * Note: con->command and con->options were set by | |
1515 | * cupsdIsCGI()... | |
1516 | */ | |
1517 | ||
1518 | if (!cupsdSendCommand(con, con->command, con->options, 0)) | |
1519 | { | |
1520 | if (!cupsdSendError(con, HTTP_NOT_FOUND)) | |
1521 | return (cupsdCloseClient(con)); | |
1522 | } | |
1523 | else | |
1524 | cupsdLogRequest(con, HTTP_OK); | |
1525 | ||
1526 | if (con->http.version <= HTTP_1_0) | |
1527 | con->http.keep_alive = HTTP_KEEPALIVE_OFF; | |
1528 | break; | |
1529 | } | |
1530 | ||
1531 | if (!check_if_modified(con, &filestats)) | |
1532 | { | |
1533 | if (!cupsdSendError(con, HTTP_NOT_MODIFIED)) | |
1534 | return (cupsdCloseClient(con)); | |
1535 | } | |
1536 | else | |
1537 | { | |
1538 | if (type == NULL) | |
1539 | strcpy(line, "text/plain"); | |
1540 | else | |
1541 | snprintf(line, sizeof(line), "%s/%s", type->super, type->type); | |
1542 | ||
1543 | if (!cupsdSendFile(con, HTTP_OK, filename, line, &filestats)) | |
1544 | return (cupsdCloseClient(con)); | |
1545 | } | |
1546 | } | |
1547 | break; | |
1548 | ||
1549 | case HTTP_POST_RECV : | |
1550 | /* | |
1551 | * See if the POST request includes a Content-Length field, and if | |
1552 | * so check the length against any limits that are set... | |
1553 | */ | |
1554 | ||
1555 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "POST %s", con->uri); | |
1556 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "CONTENT_TYPE = %s", | |
1557 | con->http.fields[HTTP_FIELD_CONTENT_TYPE]); | |
1558 | ||
1559 | if (con->http.fields[HTTP_FIELD_CONTENT_LENGTH][0] && | |
1560 | MaxRequestSize > 0 && | |
1561 | con->http.data_remaining > MaxRequestSize) | |
1562 | { | |
1563 | /* | |
1564 | * Request too large... | |
1565 | */ | |
1566 | ||
1567 | if (!cupsdSendError(con, HTTP_REQUEST_TOO_LARGE)) | |
1568 | return (cupsdCloseClient(con)); | |
1569 | ||
1570 | break; | |
1571 | } | |
1572 | else if (con->http.data_remaining < 0) | |
1573 | { | |
1574 | /* | |
1575 | * Negative content lengths are invalid! | |
1576 | */ | |
1577 | ||
1578 | if (!cupsdSendError(con, HTTP_BAD_REQUEST)) | |
1579 | return (cupsdCloseClient(con)); | |
1580 | ||
1581 | break; | |
1582 | } | |
1583 | ||
1584 | /* | |
1585 | * See what kind of POST request this is; for IPP requests the | |
1586 | * content-type field will be "application/ipp"... | |
1587 | */ | |
1588 | ||
1589 | if (!strcmp(con->http.fields[HTTP_FIELD_CONTENT_TYPE], | |
1590 | "application/ipp")) | |
1591 | con->request = ippNew(); | |
1592 | else if ((!strncmp(con->uri, "/admin", 6) && | |
1593 | strncmp(con->uri, "/admin/conf/", 12) && | |
1594 | strncmp(con->uri, "/admin/log/", 11)) || | |
1595 | !strncmp(con->uri, "/printers", 9) || | |
1596 | !strncmp(con->uri, "/classes", 8) || | |
1597 | !strncmp(con->uri, "/help", 5) || | |
1598 | !strncmp(con->uri, "/jobs", 5)) | |
1599 | { | |
1600 | /* | |
1601 | * CGI request... | |
1602 | */ | |
1603 | ||
1604 | if (!strncmp(con->uri, "/admin", 6)) | |
1605 | { | |
1606 | cupsdSetStringf(&con->command, "%s/cgi-bin/admin.cgi", | |
1607 | ServerBin); | |
1608 | ||
1609 | if ((ptr = strchr(con->uri + 6, '?')) != NULL) | |
1610 | cupsdSetStringf(&con->options, "admin%s", ptr); | |
1611 | else | |
1612 | cupsdSetString(&con->options, "admin"); | |
1613 | } | |
1614 | else if (!strncmp(con->uri, "/printers", 9)) | |
1615 | { | |
1616 | cupsdSetStringf(&con->command, "%s/cgi-bin/printers.cgi", | |
1617 | ServerBin); | |
1618 | cupsdSetString(&con->options, con->uri + 9); | |
1619 | } | |
1620 | else if (!strncmp(con->uri, "/classes", 8)) | |
1621 | { | |
1622 | cupsdSetStringf(&con->command, "%s/cgi-bin/classes.cgi", | |
1623 | ServerBin); | |
1624 | cupsdSetString(&con->options, con->uri + 8); | |
1625 | } | |
1626 | else if (!strncmp(con->uri, "/jobs", 5)) | |
1627 | { | |
1628 | cupsdSetStringf(&con->command, "%s/cgi-bin/jobs.cgi", | |
1629 | ServerBin); | |
1630 | cupsdSetString(&con->options, con->uri + 5); | |
1631 | } | |
1632 | else | |
1633 | { | |
1634 | cupsdSetStringf(&con->command, "%s/cgi-bin/help.cgi", | |
1635 | ServerBin); | |
1636 | cupsdSetString(&con->options, con->uri + 5); | |
1637 | } | |
1638 | ||
1639 | if (con->options[0] == '/') | |
1640 | _cups_strcpy(con->options, con->options + 1); | |
1641 | ||
1642 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
1643 | "cupsdReadClient: %d command=\"%s\", options = \"%s\"", | |
1644 | con->http.fd, con->command, con->options); | |
1645 | ||
1646 | if (con->http.version <= HTTP_1_0) | |
1647 | con->http.keep_alive = HTTP_KEEPALIVE_OFF; | |
1648 | } | |
1649 | else | |
1650 | { | |
1651 | /* | |
1652 | * POST to a file... | |
1653 | */ | |
1654 | ||
1655 | if ((filename = get_file(con, &filestats, buf, | |
1656 | sizeof(buf))) == NULL) | |
1657 | { | |
1658 | if (!cupsdSendError(con, HTTP_NOT_FOUND)) | |
1659 | return (cupsdCloseClient(con)); | |
1660 | ||
1661 | break; | |
1662 | } | |
1663 | ||
1664 | type = mimeFileType(MimeDatabase, filename, NULL); | |
1665 | ||
1666 | if (!cupsdIsCGI(con, filename, &filestats, type)) | |
1667 | { | |
1668 | /* | |
1669 | * Only POST to CGI's... | |
1670 | */ | |
1671 | ||
1672 | if (!cupsdSendError(con, HTTP_UNAUTHORIZED)) | |
1673 | return (cupsdCloseClient(con)); | |
1674 | } | |
1675 | } | |
1676 | break; | |
1677 | ||
1678 | case HTTP_PUT_RECV : | |
1679 | /* | |
1680 | * Validate the resource name... | |
1681 | */ | |
1682 | ||
1683 | if (strncmp(con->uri, "/admin/conf/", 12) || | |
1684 | strchr(con->uri + 12, '/') || | |
1685 | strlen(con->uri) == 12) | |
1686 | { | |
1687 | /* | |
1688 | * PUT can only be done to configuration files under | |
1689 | * /admin/conf... | |
1690 | */ | |
1691 | ||
1692 | if (!cupsdSendError(con, HTTP_FORBIDDEN)) | |
1693 | return (cupsdCloseClient(con)); | |
1694 | ||
1695 | break; | |
1696 | } | |
1697 | ||
1698 | /* | |
1699 | * See if the PUT request includes a Content-Length field, and if | |
1700 | * so check the length against any limits that are set... | |
1701 | */ | |
1702 | ||
1703 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "PUT %s", con->uri); | |
1704 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "CONTENT_TYPE = %s", | |
1705 | con->http.fields[HTTP_FIELD_CONTENT_TYPE]); | |
1706 | ||
1707 | if (con->http.fields[HTTP_FIELD_CONTENT_LENGTH][0] && | |
1708 | MaxRequestSize > 0 && | |
1709 | con->http.data_remaining > MaxRequestSize) | |
1710 | { | |
1711 | /* | |
1712 | * Request too large... | |
1713 | */ | |
1714 | ||
1715 | if (!cupsdSendError(con, HTTP_REQUEST_TOO_LARGE)) | |
1716 | return (cupsdCloseClient(con)); | |
1717 | ||
1718 | break; | |
1719 | } | |
1720 | else if (con->http.data_remaining < 0) | |
1721 | { | |
1722 | /* | |
1723 | * Negative content lengths are invalid! | |
1724 | */ | |
1725 | ||
1726 | if (!cupsdSendError(con, HTTP_BAD_REQUEST)) | |
1727 | return (cupsdCloseClient(con)); | |
1728 | ||
1729 | break; | |
1730 | } | |
1731 | ||
1732 | /* | |
1733 | * Open a temporary file to hold the request... | |
1734 | */ | |
1735 | ||
1736 | cupsdSetStringf(&con->filename, "%s/%08x", RequestRoot, | |
1737 | request_id ++); | |
1738 | con->file = open(con->filename, O_WRONLY | O_CREAT | O_TRUNC, 0640); | |
1739 | ||
1740 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
1741 | "cupsdReadClient: %d REQUEST %s=%d", con->http.fd, | |
1742 | con->filename, con->file); | |
1743 | ||
1744 | if (con->file < 0) | |
1745 | { | |
1746 | if (!cupsdSendError(con, HTTP_REQUEST_TOO_LARGE)) | |
1747 | return (cupsdCloseClient(con)); | |
1748 | } | |
1749 | ||
1750 | fchmod(con->file, 0640); | |
1751 | fchown(con->file, RunUser, Group); | |
1752 | fcntl(con->file, F_SETFD, fcntl(con->file, F_GETFD) | FD_CLOEXEC); | |
1753 | break; | |
1754 | ||
1755 | case HTTP_DELETE : | |
1756 | case HTTP_TRACE : | |
1757 | cupsdSendError(con, HTTP_NOT_IMPLEMENTED); | |
1758 | return (cupsdCloseClient(con)); | |
1759 | ||
1760 | case HTTP_HEAD : | |
1761 | if (!strncmp(con->uri, "/printers/", 10) && | |
1762 | !strcmp(con->uri + strlen(con->uri) - 4, ".ppd")) | |
1763 | { | |
1764 | /* | |
1765 | * Send PPD file - get the real printer name since printer | |
1766 | * names are not case sensitive but filenames can be... | |
1767 | */ | |
1768 | ||
1769 | con->uri[strlen(con->uri) - 4] = '\0'; /* Drop ".ppd" */ | |
1770 | ||
1771 | if ((p = cupsdFindPrinter(con->uri + 10)) != NULL) | |
1772 | snprintf(con->uri, sizeof(con->uri), "/ppd/%s.ppd", p->name); | |
1773 | else | |
1774 | { | |
1775 | if (!cupsdSendError(con, HTTP_NOT_FOUND)) | |
1776 | return (cupsdCloseClient(con)); | |
1777 | ||
1778 | break; | |
1779 | } | |
1780 | } | |
1781 | ||
1782 | if ((!strncmp(con->uri, "/admin", 6) && | |
1783 | strncmp(con->uri, "/admin/conf/", 12) && | |
1784 | strncmp(con->uri, "/admin/log/", 11)) || | |
1785 | !strncmp(con->uri, "/printers", 9) || | |
1786 | !strncmp(con->uri, "/classes", 8) || | |
1787 | !strncmp(con->uri, "/help", 5) || | |
1788 | !strncmp(con->uri, "/jobs", 5)) | |
1789 | { | |
1790 | /* | |
1791 | * CGI output... | |
1792 | */ | |
1793 | ||
1794 | if (!cupsdSendHeader(con, HTTP_OK, "text/html")) | |
1795 | return (cupsdCloseClient(con)); | |
1796 | ||
1797 | if (httpPrintf(HTTP(con), "\r\n") < 0) | |
1798 | return (cupsdCloseClient(con)); | |
1799 | ||
1800 | cupsdLogRequest(con, HTTP_OK); | |
1801 | } | |
1802 | else if ((!strncmp(con->uri, "/admin/conf/", 12) && | |
1803 | (strchr(con->uri + 12, '/') || | |
1804 | strlen(con->uri) == 12)) || | |
1805 | (!strncmp(con->uri, "/admin/log/", 11) && | |
1806 | (strchr(con->uri + 11, '/') || | |
1807 | strlen(con->uri) == 11))) | |
1808 | { | |
1809 | /* | |
1810 | * HEAD can only be done to configuration files under | |
1811 | * /admin/conf... | |
1812 | */ | |
1813 | ||
1814 | if (!cupsdSendError(con, HTTP_FORBIDDEN)) | |
1815 | return (cupsdCloseClient(con)); | |
1816 | ||
1817 | break; | |
1818 | } | |
1819 | else if ((filename = get_file(con, &filestats, buf, | |
1820 | sizeof(buf))) == NULL) | |
1821 | { | |
1822 | if (!cupsdSendHeader(con, HTTP_NOT_FOUND, "text/html")) | |
1823 | return (cupsdCloseClient(con)); | |
1824 | ||
1825 | cupsdLogRequest(con, HTTP_NOT_FOUND); | |
1826 | } | |
1827 | else if (!check_if_modified(con, &filestats)) | |
1828 | { | |
1829 | if (!cupsdSendError(con, HTTP_NOT_MODIFIED)) | |
1830 | return (cupsdCloseClient(con)); | |
1831 | ||
1832 | cupsdLogRequest(con, HTTP_NOT_MODIFIED); | |
1833 | } | |
1834 | else | |
1835 | { | |
1836 | /* | |
1837 | * Serve a file... | |
1838 | */ | |
1839 | ||
1840 | type = mimeFileType(MimeDatabase, filename, NULL); | |
1841 | if (type == NULL) | |
1842 | strcpy(line, "text/plain"); | |
1843 | else | |
1844 | snprintf(line, sizeof(line), "%s/%s", type->super, type->type); | |
1845 | ||
1846 | if (!cupsdSendHeader(con, HTTP_OK, line)) | |
1847 | return (cupsdCloseClient(con)); | |
1848 | ||
1849 | if (httpPrintf(HTTP(con), "Last-Modified: %s\r\n", | |
1850 | httpGetDateString(filestats.st_mtime)) < 0) | |
1851 | return (cupsdCloseClient(con)); | |
1852 | ||
1853 | if (httpPrintf(HTTP(con), "Content-Length: %lu\r\n", | |
1854 | (unsigned long)filestats.st_size) < 0) | |
1855 | return (cupsdCloseClient(con)); | |
1856 | ||
1857 | cupsdLogRequest(con, HTTP_OK); | |
1858 | } | |
1859 | ||
1860 | if (httpPrintf(HTTP(con), "\r\n") < 0) | |
1861 | return (cupsdCloseClient(con)); | |
1862 | ||
1863 | con->http.state = HTTP_WAITING; | |
1864 | break; | |
1865 | ||
1866 | default : | |
1867 | break; /* Anti-compiler-warning-code */ | |
1868 | } | |
1869 | } | |
1870 | } | |
1871 | ||
1872 | /* | |
1873 | * Handle any incoming data... | |
1874 | */ | |
1875 | ||
1876 | switch (con->http.state) | |
1877 | { | |
1878 | case HTTP_PUT_RECV : | |
1879 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
1880 | "cupsdReadClient: %d con->data_encoding=HTTP_ENCODE_%s, " | |
1881 | "con->data_remaining=" CUPS_LLFMT ", con->file=%d", | |
1882 | con->http.fd, | |
1883 | con->http.data_encoding == HTTP_ENCODE_CHUNKED ? | |
1884 | "CHUNKED" : "LENGTH", | |
1885 | CUPS_LLCAST con->http.data_remaining, con->file); | |
1886 | ||
a4d04587 | 1887 | if ((bytes = httpRead2(HTTP(con), line, sizeof(line))) < 0) |
ef416fc2 | 1888 | return (cupsdCloseClient(con)); |
1889 | else if (bytes > 0) | |
1890 | { | |
1891 | con->bytes += bytes; | |
1892 | ||
1893 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
1894 | "cupsdReadClient: %d writing %d bytes to %d", | |
1895 | con->http.fd, bytes, con->file); | |
1896 | ||
1897 | if (write(con->file, line, bytes) < bytes) | |
1898 | { | |
1899 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
1900 | "cupsdReadClient: Unable to write %d bytes to %s: %s", | |
1901 | bytes, con->filename, strerror(errno)); | |
1902 | ||
1903 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
1904 | "cupsdReadClient: Closing data file %d...", | |
1905 | con->file); | |
1906 | ||
1907 | close(con->file); | |
1908 | con->file = -1; | |
1909 | unlink(con->filename); | |
1910 | cupsdClearString(&con->filename); | |
1911 | ||
1912 | if (!cupsdSendError(con, HTTP_REQUEST_TOO_LARGE)) | |
1913 | return (cupsdCloseClient(con)); | |
1914 | } | |
1915 | } | |
1916 | ||
1917 | if (con->http.state == HTTP_WAITING) | |
1918 | { | |
1919 | /* | |
1920 | * End of file, see how big it is... | |
1921 | */ | |
1922 | ||
1923 | fstat(con->file, &filestats); | |
1924 | ||
1925 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
1926 | "cupsdReadClient: %d Closing data file %d, size=" | |
1927 | CUPS_LLFMT ".", | |
1928 | con->http.fd, con->file, | |
1929 | CUPS_LLCAST filestats.st_size); | |
1930 | ||
1931 | close(con->file); | |
1932 | con->file = -1; | |
1933 | ||
1934 | if (filestats.st_size > MaxRequestSize && | |
1935 | MaxRequestSize > 0) | |
1936 | { | |
1937 | /* | |
1938 | * Request is too big; remove it and send an error... | |
1939 | */ | |
1940 | ||
1941 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
1942 | "cupsdReadClient: %d Removing temp file %s", | |
1943 | con->http.fd, con->filename); | |
1944 | unlink(con->filename); | |
1945 | cupsdClearString(&con->filename); | |
1946 | ||
1947 | if (!cupsdSendError(con, HTTP_REQUEST_TOO_LARGE)) | |
1948 | return (cupsdCloseClient(con)); | |
1949 | } | |
1950 | ||
1951 | /* | |
1952 | * Install the configuration file... | |
1953 | */ | |
1954 | ||
1955 | status = install_conf_file(con); | |
1956 | ||
1957 | /* | |
1958 | * Return the status to the client... | |
1959 | */ | |
1960 | ||
1961 | if (!cupsdSendError(con, status)) | |
1962 | return (cupsdCloseClient(con)); | |
1963 | } | |
1964 | break; | |
1965 | ||
1966 | case HTTP_POST_RECV : | |
1967 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
1968 | "cupsdReadClient: %d con->data_encoding=HTTP_ENCODE_" | |
1969 | "%s, con->data_remaining=" CUPS_LLFMT ", con->file=%d", | |
1970 | con->http.fd, | |
1971 | con->http.data_encoding == HTTP_ENCODE_CHUNKED ? | |
1972 | "CHUNKED" : "LENGTH", | |
1973 | CUPS_LLCAST con->http.data_remaining, con->file); | |
1974 | ||
1975 | if (con->request != NULL) | |
1976 | { | |
1977 | /* | |
1978 | * Grab any request data from the connection... | |
1979 | */ | |
1980 | ||
1981 | if ((ipp_state = ippRead(&(con->http), con->request)) == IPP_ERROR) | |
1982 | { | |
1983 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
1984 | "cupsdReadClient: %d IPP Read Error!", | |
1985 | con->http.fd); | |
1986 | ||
1987 | cupsdSendError(con, HTTP_BAD_REQUEST); | |
1988 | return (cupsdCloseClient(con)); | |
1989 | } | |
1990 | else if (ipp_state != IPP_DATA) | |
1991 | { | |
1992 | if (con->http.state == HTTP_POST_SEND) | |
1993 | { | |
1994 | cupsdSendError(con, HTTP_BAD_REQUEST); | |
1995 | return (cupsdCloseClient(con)); | |
1996 | } | |
1997 | ||
1998 | break; | |
1999 | } | |
2000 | else | |
2001 | con->bytes += ippLength(con->request); | |
2002 | } | |
2003 | ||
2004 | if (con->file < 0 && con->http.state != HTTP_POST_SEND) | |
2005 | { | |
2006 | /* | |
2007 | * Create a file as needed for the request data... | |
2008 | */ | |
2009 | ||
2010 | cupsdSetStringf(&con->filename, "%s/%08x", RequestRoot, request_id ++); | |
2011 | con->file = open(con->filename, O_WRONLY | O_CREAT | O_TRUNC, 0640); | |
2012 | ||
2013 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdReadClient: %d REQUEST %s=%d", con->http.fd, | |
2014 | con->filename, con->file); | |
2015 | ||
2016 | if (con->file < 0) | |
2017 | { | |
2018 | if (!cupsdSendError(con, HTTP_REQUEST_TOO_LARGE)) | |
2019 | return (cupsdCloseClient(con)); | |
2020 | } | |
2021 | ||
2022 | fchmod(con->file, 0640); | |
2023 | fchown(con->file, RunUser, Group); | |
2024 | fcntl(con->file, F_SETFD, fcntl(con->file, F_GETFD) | FD_CLOEXEC); | |
2025 | } | |
2026 | ||
2027 | if (con->http.state != HTTP_POST_SEND) | |
2028 | { | |
a4d04587 | 2029 | if ((bytes = httpRead2(HTTP(con), line, sizeof(line))) < 0) |
ef416fc2 | 2030 | return (cupsdCloseClient(con)); |
2031 | else if (bytes > 0) | |
2032 | { | |
2033 | con->bytes += bytes; | |
2034 | ||
2035 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
2036 | "cupsdReadClient: %d writing %d bytes to %d", | |
2037 | con->http.fd, bytes, con->file); | |
2038 | ||
2039 | if (write(con->file, line, bytes) < bytes) | |
2040 | { | |
2041 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
2042 | "cupsdReadClient: Unable to write %d bytes to %s: %s", | |
2043 | bytes, con->filename, strerror(errno)); | |
2044 | ||
2045 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
2046 | "cupsdReadClient: Closing file %d...", | |
2047 | con->file); | |
2048 | ||
2049 | close(con->file); | |
2050 | con->file = -1; | |
2051 | unlink(con->filename); | |
2052 | cupsdClearString(&con->filename); | |
2053 | ||
2054 | if (!cupsdSendError(con, HTTP_REQUEST_TOO_LARGE)) | |
2055 | return (cupsdCloseClient(con)); | |
2056 | } | |
2057 | } | |
2058 | else if (con->http.state == HTTP_POST_RECV) | |
2059 | return (1); /* ??? */ | |
2060 | else if (con->http.state != HTTP_POST_SEND) | |
2061 | return (cupsdCloseClient(con)); | |
2062 | } | |
2063 | ||
2064 | if (con->http.state == HTTP_POST_SEND) | |
2065 | { | |
2066 | if (con->file >= 0) | |
2067 | { | |
2068 | fstat(con->file, &filestats); | |
2069 | ||
2070 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
2071 | "cupsdReadClient: %d Closing data file %d, " | |
2072 | "size=" CUPS_LLFMT ".", | |
2073 | con->http.fd, con->file, | |
2074 | CUPS_LLCAST filestats.st_size); | |
2075 | ||
2076 | close(con->file); | |
2077 | con->file = -1; | |
2078 | ||
2079 | if (filestats.st_size > MaxRequestSize && | |
2080 | MaxRequestSize > 0) | |
2081 | { | |
2082 | /* | |
2083 | * Request is too big; remove it and send an error... | |
2084 | */ | |
2085 | ||
2086 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
2087 | "cupsdReadClient: %d Removing temp file %s", | |
2088 | con->http.fd, con->filename); | |
2089 | unlink(con->filename); | |
2090 | cupsdClearString(&con->filename); | |
2091 | ||
2092 | if (con->request) | |
2093 | { | |
2094 | /* | |
2095 | * Delete any IPP request data... | |
2096 | */ | |
2097 | ||
2098 | ippDelete(con->request); | |
2099 | con->request = NULL; | |
2100 | } | |
2101 | ||
2102 | if (!cupsdSendError(con, HTTP_REQUEST_TOO_LARGE)) | |
2103 | return (cupsdCloseClient(con)); | |
2104 | } | |
2105 | ||
2106 | if (con->command) | |
2107 | { | |
2108 | if (!cupsdSendCommand(con, con->command, con->options, 0)) | |
2109 | { | |
2110 | if (!cupsdSendError(con, HTTP_NOT_FOUND)) | |
2111 | return (cupsdCloseClient(con)); | |
2112 | } | |
2113 | else | |
2114 | cupsdLogRequest(con, HTTP_OK); | |
2115 | } | |
2116 | } | |
2117 | ||
2118 | if (con->request) | |
2119 | return (cupsdProcessIPPRequest(con)); | |
2120 | } | |
2121 | break; | |
2122 | ||
2123 | default : | |
2124 | break; /* Anti-compiler-warning-code */ | |
2125 | } | |
2126 | ||
2127 | if (!con->http.keep_alive && con->http.state == HTTP_WAITING) | |
2128 | return (cupsdCloseClient(con)); | |
2129 | else | |
2130 | return (1); | |
2131 | } | |
2132 | ||
2133 | ||
2134 | /* | |
2135 | * 'cupsdSendCommand()' - Send output from a command via HTTP. | |
2136 | */ | |
2137 | ||
2138 | int /* O - 1 on success, 0 on failure */ | |
2139 | cupsdSendCommand( | |
2140 | cupsd_client_t *con, /* I - Client connection */ | |
2141 | char *command, /* I - Command to run */ | |
2142 | char *options, /* I - Command-line options */ | |
2143 | int root) /* I - Run as root? */ | |
2144 | { | |
2145 | int fd; /* Standard input file descriptor */ | |
2146 | ||
2147 | ||
2148 | if (con->filename) | |
2149 | fd = open(con->filename, O_RDONLY); | |
2150 | else | |
2151 | fd = open("/dev/null", O_RDONLY); | |
2152 | ||
2153 | if (fd < 0) | |
2154 | { | |
2155 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
2156 | "cupsdSendCommand: %d Unable to open \"%s\" for reading: %s", | |
2157 | con->http.fd, con->filename ? con->filename : "/dev/null", | |
2158 | strerror(errno)); | |
2159 | return (0); | |
2160 | } | |
2161 | ||
2162 | fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC); | |
2163 | ||
2164 | con->pipe_pid = pipe_command(con, fd, &(con->file), command, options, root); | |
2165 | ||
2166 | close(fd); | |
2167 | ||
2168 | cupsdLogMessage(CUPSD_LOG_INFO, "Started \"%s\" (pid=%d)", command, | |
2169 | con->pipe_pid); | |
2170 | ||
2171 | cupsdLogMessage(CUPSD_LOG_DEBUG, "cupsdSendCommand: %d file=%d", | |
2172 | con->http.fd, con->file); | |
2173 | ||
2174 | if (con->pipe_pid == 0) | |
2175 | return (0); | |
2176 | ||
2177 | fcntl(con->file, F_SETFD, fcntl(con->file, F_GETFD) | FD_CLOEXEC); | |
2178 | ||
2179 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
2180 | "cupsdSendCommand: Adding fd %d to InputSet...", con->file); | |
2181 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
2182 | "cupsdSendCommand: Adding fd %d to OutputSet...", | |
2183 | con->http.fd); | |
2184 | ||
2185 | FD_SET(con->file, InputSet); | |
2186 | FD_SET(con->http.fd, OutputSet); | |
2187 | ||
2188 | con->sent_header = 0; | |
2189 | con->file_ready = 0; | |
2190 | con->got_fields = 0; | |
2191 | con->field_col = 0; | |
2192 | ||
2193 | return (1); | |
2194 | } | |
2195 | ||
2196 | ||
2197 | /* | |
2198 | * 'cupsdSendError()' - Send an error message via HTTP. | |
2199 | */ | |
2200 | ||
2201 | int /* O - 1 if successful, 0 otherwise */ | |
2202 | cupsdSendError(cupsd_client_t *con, /* I - Connection */ | |
2203 | http_status_t code) /* I - Error code */ | |
2204 | { | |
2205 | /* | |
2206 | * Put the request in the access_log file... | |
2207 | */ | |
2208 | ||
2209 | cupsdLogRequest(con, code); | |
2210 | ||
2211 | cupsdLogMessage(CUPSD_LOG_DEBUG, "cupsdSendError: %d code=%d (%s)", | |
2212 | con->http.fd, code, httpStatus(code)); | |
2213 | ||
2214 | /* | |
2215 | * To work around bugs in some proxies, don't use Keep-Alive for some | |
2216 | * error messages... | |
2217 | */ | |
2218 | ||
2219 | if (code >= HTTP_BAD_REQUEST) | |
2220 | con->http.keep_alive = HTTP_KEEPALIVE_OFF; | |
2221 | ||
2222 | /* | |
2223 | * Send an error message back to the client. If the error code is a | |
2224 | * 400 or 500 series, make sure the message contains some text, too! | |
2225 | */ | |
2226 | ||
2227 | if (!cupsdSendHeader(con, code, NULL)) | |
2228 | return (0); | |
2229 | ||
2230 | #ifdef HAVE_SSL | |
2231 | if (code == HTTP_UPGRADE_REQUIRED) | |
2232 | if (httpPrintf(HTTP(con), "Connection: Upgrade\r\n") < 0) | |
2233 | return (0); | |
2234 | ||
2235 | if (httpPrintf(HTTP(con), "Upgrade: TLS/1.0,HTTP/1.1\r\n") < 0) | |
2236 | return (0); | |
2237 | #endif /* HAVE_SSL */ | |
2238 | ||
2239 | if ((con->http.version >= HTTP_1_1 && !con->http.keep_alive) || | |
2240 | (code >= HTTP_BAD_REQUEST && code != HTTP_UPGRADE_REQUIRED)) | |
2241 | { | |
2242 | if (httpPrintf(HTTP(con), "Connection: close\r\n") < 0) | |
2243 | return (0); | |
2244 | } | |
2245 | ||
2246 | if (code >= HTTP_BAD_REQUEST) | |
2247 | { | |
2248 | /* | |
2249 | * Send a human-readable error message. | |
2250 | */ | |
2251 | ||
2252 | char message[1024]; /* Message for user */ | |
2253 | const char *text; /* Status-specific text */ | |
2254 | ||
2255 | if (code == HTTP_UNAUTHORIZED) | |
2256 | text = _cupsLangString(con->language, | |
2257 | _("Enter your username and password or the " | |
2258 | "root username and password to access this " | |
2259 | "page.")); | |
2260 | else if (code == HTTP_UPGRADE_REQUIRED) | |
2261 | text = _cupsLangString(con->language, | |
2262 | _("You must use a https: URL to access this " | |
2263 | "page.")); | |
2264 | else | |
2265 | text = ""; | |
2266 | ||
2267 | snprintf(message, sizeof(message), | |
2268 | "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\" " | |
2269 | "\"http://www.w3.org/TR/REC-html40/loose.dtd\">\n" | |
2270 | "<HTML>\n" | |
2271 | "<HEAD>\n" | |
2272 | "\t<META HTTP-EQUIV=\"Content-Type\" " | |
2273 | "CONTENT=\"text/html; charset=utf-8\">\n" | |
2274 | "\t<TITLE>%d %s</TITLE>\n" | |
2275 | "\t<LINK REL=\"STYLESHEET\" TYPE=\"text/css\" " | |
2276 | "HREF=\"/cups.css\">\n" | |
2277 | "</HEAD>\n" | |
2278 | "<BODY>\n" | |
2279 | "<H1>%d %s</H1>\n" | |
2280 | "<P>%s</P>\n" | |
2281 | "</BODY>\n" | |
2282 | "</HTML>\n", | |
2283 | code, httpStatus(code), code, httpStatus(code), text); | |
2284 | ||
2285 | if (httpPrintf(HTTP(con), "Content-Type: text/html; charset=utf-8\r\n") < 0) | |
2286 | return (0); | |
2287 | if (httpPrintf(HTTP(con), "Content-Length: %d\r\n", | |
2288 | (int)strlen(message)) < 0) | |
2289 | return (0); | |
2290 | if (httpPrintf(HTTP(con), "\r\n") < 0) | |
2291 | return (0); | |
2292 | if (httpPrintf(HTTP(con), "%s", message) < 0) | |
2293 | return (0); | |
2294 | } | |
2295 | else if (httpPrintf(HTTP(con), "\r\n") < 0) | |
2296 | return (0); | |
2297 | ||
2298 | con->http.state = HTTP_WAITING; | |
2299 | ||
2300 | return (1); | |
2301 | } | |
2302 | ||
2303 | ||
2304 | /* | |
2305 | * 'cupsdSendFile()' - Send a file via HTTP. | |
2306 | */ | |
2307 | ||
2308 | int /* O - 0 on failure, 1 on success */ | |
2309 | cupsdSendFile(cupsd_client_t *con, /* I - Client connection */ | |
2310 | http_status_t code, /* I - HTTP status */ | |
2311 | char *filename, /* I - Filename */ | |
2312 | char *type, /* I - File type */ | |
2313 | struct stat *filestats)/* O - File information */ | |
2314 | { | |
2315 | con->file = open(filename, O_RDONLY); | |
2316 | ||
2317 | cupsdLogMessage(CUPSD_LOG_DEBUG, "cupsdSendFile: %d file=%d", con->http.fd, | |
2318 | con->file); | |
2319 | ||
2320 | if (con->file < 0) | |
2321 | return (0); | |
2322 | ||
2323 | fcntl(con->file, F_SETFD, fcntl(con->file, F_GETFD) | FD_CLOEXEC); | |
2324 | ||
2325 | con->pipe_pid = 0; | |
2326 | ||
2327 | if (!cupsdSendHeader(con, code, type)) | |
2328 | return (0); | |
2329 | ||
2330 | if (httpPrintf(HTTP(con), "Last-Modified: %s\r\n", | |
2331 | httpGetDateString(filestats->st_mtime)) < 0) | |
2332 | return (0); | |
2333 | if (httpPrintf(HTTP(con), "Content-Length: " CUPS_LLFMT "\r\n", | |
2334 | CUPS_LLCAST filestats->st_size) < 0) | |
2335 | return (0); | |
2336 | if (httpPrintf(HTTP(con), "\r\n") < 0) | |
2337 | return (0); | |
2338 | ||
2339 | con->http.data_encoding = HTTP_ENCODE_LENGTH; | |
2340 | con->http.data_remaining = filestats->st_size; | |
2341 | ||
2342 | if (con->http.data_remaining <= INT_MAX) | |
2343 | con->http._data_remaining = con->http.data_remaining; | |
2344 | else | |
2345 | con->http._data_remaining = INT_MAX; | |
2346 | ||
2347 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
2348 | "cupsdSendFile: Adding fd %d to OutputSet...", con->http.fd); | |
2349 | ||
2350 | FD_SET(con->http.fd, OutputSet); | |
2351 | ||
2352 | return (1); | |
2353 | } | |
2354 | ||
2355 | ||
2356 | /* | |
2357 | * 'cupsdSendHeader()' - Send an HTTP request. | |
2358 | */ | |
2359 | ||
2360 | int /* O - 1 on success, 0 on failure */ | |
2361 | cupsdSendHeader(cupsd_client_t *con, /* I - Client to send to */ | |
2362 | http_status_t code, /* I - HTTP status code */ | |
2363 | char *type) /* I - MIME type of document */ | |
2364 | { | |
2365 | if (httpPrintf(HTTP(con), "HTTP/%d.%d %d %s\r\n", con->http.version / 100, | |
2366 | con->http.version % 100, code, httpStatus(code)) < 0) | |
2367 | return (0); | |
2368 | if (httpPrintf(HTTP(con), "Date: %s\r\n", httpGetDateString(time(NULL))) < 0) | |
2369 | return (0); | |
2370 | if (ServerHeader) | |
2371 | if (httpPrintf(HTTP(con), "Server: %s\r\n", ServerHeader) < 0) | |
2372 | return (0); | |
2373 | if (con->http.keep_alive && con->http.version >= HTTP_1_0) | |
2374 | { | |
2375 | if (httpPrintf(HTTP(con), "Connection: Keep-Alive\r\n") < 0) | |
2376 | return (0); | |
2377 | if (httpPrintf(HTTP(con), "Keep-Alive: timeout=%d\r\n", | |
2378 | KeepAliveTimeout) < 0) | |
2379 | return (0); | |
2380 | } | |
2381 | if (code == HTTP_METHOD_NOT_ALLOWED) | |
2382 | if (httpPrintf(HTTP(con), "Allow: GET, HEAD, OPTIONS, POST\r\n") < 0) | |
2383 | return (0); | |
2384 | ||
2385 | if (code == HTTP_UNAUTHORIZED) | |
2386 | { | |
2387 | int auth_type; /* Authentication type */ | |
2388 | ||
2389 | ||
2390 | if (!con->best || con->best->type == AUTH_NONE) | |
2391 | auth_type = DefaultAuthType; | |
2392 | else | |
2393 | auth_type = con->best->type; | |
2394 | ||
2395 | if (auth_type != AUTH_DIGEST) | |
2396 | { | |
2397 | if (httpPrintf(HTTP(con), | |
2398 | "WWW-Authenticate: Basic realm=\"CUPS\"\r\n") < 0) | |
2399 | return (0); | |
2400 | } | |
2401 | else | |
2402 | { | |
2403 | if (httpPrintf(HTTP(con), | |
2404 | "WWW-Authenticate: Digest realm=\"CUPS\", nonce=\"%s\"\r\n", | |
2405 | con->http.hostname) < 0) | |
2406 | return (0); | |
2407 | } | |
2408 | } | |
2409 | ||
2410 | if (con->language != NULL) | |
2411 | { | |
2412 | if (httpPrintf(HTTP(con), "Content-Language: %s\r\n", | |
2413 | con->language->language) < 0) | |
2414 | return (0); | |
2415 | } | |
2416 | ||
2417 | if (type != NULL) | |
2418 | { | |
2419 | if (!strcmp(type, "text/html")) | |
2420 | { | |
2421 | if (httpPrintf(HTTP(con), | |
2422 | "Content-Type: text/html; charset=utf-8\r\n") < 0) | |
2423 | return (0); | |
2424 | } | |
2425 | else if (httpPrintf(HTTP(con), "Content-Type: %s\r\n", type) < 0) | |
2426 | return (0); | |
2427 | } | |
2428 | ||
2429 | return (1); | |
2430 | } | |
2431 | ||
2432 | ||
2433 | /* | |
2434 | * 'cupsdUpdateCGI()' - Read status messages from CGI scripts and programs. | |
2435 | */ | |
2436 | ||
2437 | void | |
2438 | cupsdUpdateCGI(void) | |
2439 | { | |
2440 | char *ptr, /* Pointer to end of line in buffer */ | |
2441 | message[1024]; /* Pointer to message text */ | |
2442 | int loglevel; /* Log level for message */ | |
2443 | ||
2444 | ||
2445 | while ((ptr = cupsdStatBufUpdate(CGIStatusBuffer, &loglevel, | |
2446 | message, sizeof(message))) != NULL) | |
2447 | if (!strchr(CGIStatusBuffer->buffer, '\n')) | |
2448 | break; | |
2449 | ||
2450 | if (ptr == NULL && errno) | |
2451 | { | |
2452 | /* | |
2453 | * Fatal error on pipe - should never happen! | |
2454 | */ | |
2455 | ||
2456 | cupsdLogMessage(CUPSD_LOG_CRIT, | |
2457 | "cupsdUpdateCGI: error reading from CGI error pipe - %s", | |
2458 | strerror(errno)); | |
2459 | } | |
2460 | } | |
2461 | ||
2462 | ||
2463 | /* | |
2464 | * 'cupsdWriteClient()' - Write data to a client as needed. | |
2465 | */ | |
2466 | ||
2467 | int /* O - 1 if success, 0 if fail */ | |
2468 | cupsdWriteClient(cupsd_client_t *con) /* I - Client connection */ | |
2469 | { | |
2470 | int bytes; /* Number of bytes written */ | |
2471 | char buf[16385]; /* Data buffer */ | |
2472 | char *bufptr; /* Pointer into buffer */ | |
2473 | ipp_state_t ipp_state; /* IPP state value */ | |
2474 | ||
2475 | ||
2476 | #ifdef DEBUG | |
2477 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
2478 | "cupsdWriteClient(con=%p) %d response=%p, file=%d " | |
2479 | "pipe_pid=%d state=%d", | |
2480 | con, con->http.fd, con->response, con->file, con->pipe_pid, | |
2481 | con->http.state); | |
2482 | #endif /* DEBUG */ | |
2483 | ||
2484 | if (con->http.state != HTTP_GET_SEND && | |
2485 | con->http.state != HTTP_POST_SEND) | |
2486 | return (1); | |
2487 | ||
2488 | if (con->response != NULL) | |
2489 | { | |
2490 | ipp_state = ippWrite(&(con->http), con->response); | |
2491 | bytes = ipp_state != IPP_ERROR && ipp_state != IPP_DATA; | |
2492 | } | |
2493 | else if ((bytes = read(con->file, buf, sizeof(buf) - 1)) > 0) | |
2494 | { | |
2495 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
2496 | "cupsdWriteClient: Read %d bytes from file %d...", | |
2497 | bytes, con->file); | |
2498 | ||
2499 | if (con->pipe_pid && !con->got_fields) | |
2500 | { | |
2501 | /* | |
2502 | * Inspect the data for Content-Type and other fields. | |
2503 | */ | |
2504 | ||
2505 | buf[bytes] = '\0'; | |
2506 | ||
2507 | for (bufptr = buf; !con->got_fields && *bufptr; bufptr ++) | |
2508 | if (*bufptr == '\n') | |
2509 | { | |
2510 | /* | |
2511 | * Send line to client... | |
2512 | */ | |
2513 | ||
2514 | if (bufptr > buf && bufptr[-1] == '\r') | |
2515 | bufptr[-1] = '\0'; | |
2516 | *bufptr++ = '\0'; | |
2517 | ||
2518 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "Script header: %s", buf); | |
2519 | ||
2520 | if (!con->sent_header) | |
2521 | { | |
2522 | /* | |
2523 | * Handle redirection and CGI status codes... | |
2524 | */ | |
2525 | ||
2526 | if (!strncasecmp(buf, "Location:", 9)) | |
2527 | cupsdSendHeader(con, HTTP_SEE_OTHER, NULL); | |
2528 | else if (!strncasecmp(buf, "Status:", 7)) | |
2529 | cupsdSendHeader(con, atoi(buf + 7), NULL); | |
2530 | else | |
2531 | cupsdSendHeader(con, HTTP_OK, NULL); | |
2532 | ||
2533 | if (con->http.version == HTTP_1_1) | |
2534 | { | |
2535 | con->http.data_encoding = HTTP_ENCODE_CHUNKED; | |
2536 | ||
2537 | if (httpPrintf(HTTP(con), "Transfer-Encoding: chunked\r\n") < 0) | |
2538 | return (0); | |
2539 | } | |
2540 | ||
2541 | con->sent_header = 1; | |
2542 | } | |
2543 | ||
2544 | if (strncasecmp(buf, "Status:", 7)) | |
2545 | httpPrintf(HTTP(con), "%s\r\n", buf); | |
2546 | ||
2547 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdWriteClient: %d %s", | |
2548 | con->http.fd, buf); | |
2549 | ||
2550 | /* | |
2551 | * Update buffer... | |
2552 | */ | |
2553 | ||
2554 | bytes -= (bufptr - buf); | |
2555 | memmove(buf, bufptr, bytes + 1); | |
2556 | bufptr = buf - 1; | |
2557 | ||
2558 | /* | |
2559 | * See if the line was empty... | |
2560 | */ | |
2561 | ||
2562 | if (con->field_col == 0) | |
2563 | con->got_fields = 1; | |
2564 | else | |
2565 | con->field_col = 0; | |
2566 | } | |
2567 | else if (*bufptr != '\r') | |
2568 | con->field_col ++; | |
2569 | ||
2570 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
2571 | "cupsdWriteClient: %d bytes=%d, got_fields=%d", | |
2572 | con->http.fd, bytes, con->got_fields); | |
2573 | ||
2574 | if (bytes > 0 && !con->got_fields) | |
2575 | { | |
2576 | /* | |
2577 | * Remaining text needs to go out... | |
2578 | */ | |
2579 | ||
2580 | httpPrintf(HTTP(con), "%s", buf); | |
2581 | ||
2582 | con->http.activity = time(NULL); | |
2583 | return (1); | |
2584 | } | |
2585 | else if (bytes == 0) | |
2586 | { | |
2587 | con->http.activity = time(NULL); | |
2588 | return (1); | |
2589 | } | |
2590 | } | |
2591 | ||
a4d04587 | 2592 | if (httpWrite2(HTTP(con), buf, bytes) < 0) |
ef416fc2 | 2593 | { |
2594 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
2595 | "cupsdWriteClient: %d Write of %d bytes failed!", | |
2596 | con->http.fd, bytes); | |
2597 | ||
2598 | cupsdCloseClient(con); | |
2599 | return (0); | |
2600 | } | |
2601 | ||
2602 | con->bytes += bytes; | |
2603 | ||
2604 | if (con->http.state == HTTP_WAITING) | |
2605 | bytes = 0; | |
2606 | } | |
2607 | ||
2608 | if (bytes <= 0) | |
2609 | { | |
2610 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdWriteClient: %d bytes < 0", | |
2611 | con->http.fd); | |
2612 | ||
2613 | cupsdLogRequest(con, HTTP_OK); | |
2614 | ||
2615 | httpFlushWrite(HTTP(con)); | |
2616 | ||
2617 | if (con->http.data_encoding == HTTP_ENCODE_CHUNKED) | |
2618 | { | |
2619 | if (httpPrintf(HTTP(con), "0\r\n\r\n") < 0) | |
2620 | { | |
2621 | cupsdCloseClient(con); | |
2622 | return (0); | |
2623 | } | |
2624 | } | |
2625 | ||
2626 | con->http.state = HTTP_WAITING; | |
2627 | ||
2628 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
2629 | "cupsdWriteClient: Removing fd %d from OutputSet...", | |
2630 | con->http.fd); | |
2631 | ||
2632 | FD_CLR(con->http.fd, OutputSet); | |
2633 | ||
2634 | if (con->file >= 0) | |
2635 | { | |
2636 | if (FD_ISSET(con->file, InputSet)) | |
2637 | { | |
2638 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
2639 | "cupsdWriteClient: Removing fd %d from InputSet...", | |
2640 | con->file); | |
2641 | FD_CLR(con->file, InputSet); | |
2642 | } | |
2643 | ||
2644 | if (con->pipe_pid) | |
2645 | cupsdEndProcess(con->pipe_pid, 0); | |
2646 | ||
2647 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
2648 | "cupsdWriteClient: %d Closing data file %d.", | |
2649 | con->http.fd, con->file); | |
2650 | ||
2651 | close(con->file); | |
2652 | con->file = -1; | |
2653 | con->pipe_pid = 0; | |
2654 | } | |
2655 | ||
2656 | if (con->filename) | |
2657 | { | |
2658 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
2659 | "cupsdWriteClient: %d Removing temp file %s", | |
2660 | con->http.fd, con->filename); | |
2661 | unlink(con->filename); | |
2662 | cupsdClearString(&con->filename); | |
2663 | } | |
2664 | ||
2665 | if (con->request != NULL) | |
2666 | { | |
2667 | ippDelete(con->request); | |
2668 | con->request = NULL; | |
2669 | } | |
2670 | ||
2671 | if (con->response != NULL) | |
2672 | { | |
2673 | ippDelete(con->response); | |
2674 | con->response = NULL; | |
2675 | } | |
2676 | ||
2677 | cupsdClearString(&con->command); | |
2678 | cupsdClearString(&con->options); | |
2679 | ||
2680 | if (!con->http.keep_alive) | |
2681 | { | |
2682 | cupsdCloseClient(con); | |
2683 | return (0); | |
2684 | } | |
2685 | } | |
2686 | else | |
2687 | { | |
2688 | con->file_ready = 0; | |
2689 | ||
2690 | if (con->pipe_pid && !FD_ISSET(con->file, InputSet)) | |
2691 | { | |
2692 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
2693 | "cupsdWriteClient: Adding fd %d to InputSet...", | |
2694 | con->file); | |
2695 | FD_SET(con->file, InputSet); | |
2696 | } | |
2697 | } | |
2698 | ||
2699 | con->http.activity = time(NULL); | |
2700 | ||
2701 | return (1); | |
2702 | } | |
2703 | ||
2704 | ||
2705 | /* | |
2706 | * 'check_if_modified()' - Decode an "If-Modified-Since" line. | |
2707 | */ | |
2708 | ||
2709 | static int /* O - 1 if modified since */ | |
2710 | check_if_modified( | |
2711 | cupsd_client_t *con, /* I - Client connection */ | |
2712 | struct stat *filestats) /* I - File information */ | |
2713 | { | |
2714 | char *ptr; /* Pointer into field */ | |
2715 | time_t date; /* Time/date value */ | |
2716 | off_t size; /* Size/length value */ | |
2717 | ||
2718 | ||
2719 | size = 0; | |
2720 | date = 0; | |
2721 | ptr = con->http.fields[HTTP_FIELD_IF_MODIFIED_SINCE]; | |
2722 | ||
2723 | if (*ptr == '\0') | |
2724 | return (1); | |
2725 | ||
2726 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
2727 | "check_if_modified: %d If-Modified-Since=\"%s\"", | |
2728 | con->http.fd, ptr); | |
2729 | ||
2730 | while (*ptr != '\0') | |
2731 | { | |
2732 | while (isspace(*ptr) || *ptr == ';') | |
2733 | ptr ++; | |
2734 | ||
2735 | if (strncasecmp(ptr, "length=", 7) == 0) | |
2736 | { | |
2737 | ptr += 7; | |
2738 | size = strtoll(ptr, NULL, 10); | |
2739 | ||
2740 | while (isdigit(*ptr)) | |
2741 | ptr ++; | |
2742 | } | |
2743 | else if (isalpha(*ptr)) | |
2744 | { | |
2745 | date = httpGetDateTime(ptr); | |
2746 | while (*ptr != '\0' && *ptr != ';') | |
2747 | ptr ++; | |
2748 | } | |
2749 | } | |
2750 | ||
2751 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
2752 | "check_if_modified: %d sizes=" CUPS_LLFMT "," | |
2753 | CUPS_LLFMT " dates=%d,%d", | |
2754 | con->http.fd, CUPS_LLCAST size, | |
2755 | CUPS_LLCAST filestats->st_size, (int)date, | |
2756 | (int)filestats->st_mtime); | |
2757 | ||
2758 | return ((size != filestats->st_size && size != 0) || | |
2759 | (date < filestats->st_mtime && date != 0) || | |
2760 | (size == 0 && date == 0)); | |
2761 | } | |
2762 | ||
2763 | ||
2764 | #ifdef HAVE_CDSASSL | |
2765 | /* | |
2766 | * 'get_cdsa_server_certs()' - Convert a keychain name into the CFArrayRef | |
2767 | * required by SSLSetCertificate. | |
2768 | * | |
2769 | * For now we assumes that there is exactly one SecIdentity in the | |
2770 | * keychain - i.e. there is exactly one matching cert/private key pair. | |
2771 | * In the future we will search a keychain for a SecIdentity matching a | |
2772 | * specific criteria. We also skip the operation of adding additional | |
2773 | * non-signing certs from the keychain to the CFArrayRef. | |
2774 | * | |
2775 | * To create a self-signed certificate for testing use the certtool. | |
2776 | * Executing the following as root will do it: | |
2777 | * | |
2778 | * certtool c c v k=CUPS | |
2779 | */ | |
2780 | ||
2781 | static CFArrayRef /* O - Array of certificates */ | |
2782 | get_cdsa_server_certs(void) | |
2783 | { | |
2784 | OSStatus err; /* Error info */ | |
2785 | SecKeychainRef kcRef; /* Keychain reference */ | |
2786 | SecIdentitySearchRef srchRef; /* Search reference */ | |
2787 | SecIdentityRef identity; /* Identity */ | |
2788 | CFArrayRef ca; /* Certificate array */ | |
2789 | ||
2790 | ||
2791 | kcRef = NULL; | |
2792 | srchRef = NULL; | |
2793 | identity = NULL; | |
2794 | ca = NULL; | |
2795 | err = SecKeychainOpen(ServerCertificate, &kcRef); | |
2796 | ||
2797 | if (err) | |
2798 | cupsdLogMessage(CUPSD_LOG_ERROR, "Cannot open keychain \"%s\", error %d.", | |
2799 | ServerCertificate, (int)err); | |
2800 | else | |
2801 | { | |
2802 | /* | |
2803 | * Search for "any" identity matching specified key use; | |
2804 | * in this app, we expect there to be exactly one. | |
2805 | */ | |
2806 | ||
2807 | err = SecIdentitySearchCreate(kcRef, CSSM_KEYUSE_SIGN, &srchRef); | |
2808 | ||
2809 | if (err) | |
2810 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
2811 | "Cannot find signing key in keychain \"%s\", error %d", | |
2812 | ServerCertificate, (int)err); | |
2813 | else | |
2814 | { | |
2815 | err = SecIdentitySearchCopyNext(srchRef, &identity); | |
2816 | ||
2817 | if (err) | |
2818 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
2819 | "Cannot find signing key in keychain \"%s\", error %d", | |
2820 | ServerCertificate, (int)err); | |
2821 | else | |
2822 | { | |
2823 | if (CFGetTypeID(identity) != SecIdentityGetTypeID()) | |
2824 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
2825 | "SecIdentitySearchCopyNext CFTypeID failure!"); | |
2826 | else | |
2827 | { | |
2828 | /* | |
2829 | * Found one. Place it in a CFArray. | |
2830 | * TBD: snag other (non-identity) certs from keychain and add them | |
2831 | * to array as well. | |
2832 | */ | |
2833 | ||
2834 | ca = CFArrayCreate(NULL, (const void **)&identity, 1, NULL); | |
2835 | ||
2836 | if (ca == nil) | |
2837 | cupsdLogMessage(CUPSD_LOG_ERROR, "CFArrayCreate error"); | |
2838 | } | |
2839 | ||
2840 | /*CFRelease(identity);*/ | |
2841 | } | |
2842 | ||
2843 | /*CFRelease(srchRef);*/ | |
2844 | } | |
2845 | ||
2846 | /*CFRelease(kcRef);*/ | |
2847 | } | |
2848 | ||
2849 | return (ca); | |
2850 | } | |
2851 | #endif /* HAVE_CDSASSL */ | |
2852 | ||
2853 | ||
2854 | /* | |
2855 | * 'get_file()' - Get a filename and state info. | |
2856 | */ | |
2857 | ||
2858 | static char * /* O - Real filename */ | |
2859 | get_file(cupsd_client_t *con, /* I - Client connection */ | |
2860 | struct stat *filestats, /* O - File information */ | |
2861 | char *filename, /* IO - Filename buffer */ | |
2862 | int len) /* I - Buffer length */ | |
2863 | { | |
2864 | int status; /* Status of filesystem calls */ | |
2865 | char *ptr; /* Pointer info filename */ | |
2866 | int plen; /* Remaining length after pointer */ | |
2867 | ||
2868 | ||
2869 | /* | |
2870 | * Need to add DocumentRoot global... | |
2871 | */ | |
2872 | ||
2873 | if (!strncmp(con->uri, "/ppd/", 5)) | |
2874 | snprintf(filename, len, "%s%s", ServerRoot, con->uri); | |
2875 | else if (!strncmp(con->uri, "/admin/conf/", 12)) | |
2876 | snprintf(filename, len, "%s%s", ServerRoot, con->uri + 11); | |
2877 | else if (!strncmp(con->uri, "/admin/log/", 11)) | |
2878 | { | |
2879 | if (!strcmp(con->uri + 11, "access_log") && AccessLog[0] == '/') | |
2880 | strlcpy(filename, AccessLog, len); | |
2881 | else if (!strcmp(con->uri + 11, "error_log") && ErrorLog[0] == '/') | |
2882 | strlcpy(filename, ErrorLog, len); | |
2883 | else if (!strcmp(con->uri + 11, "page_log") && PageLog[0] == '/') | |
2884 | strlcpy(filename, PageLog, len); | |
2885 | else | |
2886 | return (NULL); | |
2887 | } | |
2888 | else if (con->language != NULL) | |
2889 | snprintf(filename, len, "%s/%s%s", DocumentRoot, con->language->language, | |
2890 | con->uri); | |
2891 | else | |
2892 | snprintf(filename, len, "%s%s", DocumentRoot, con->uri); | |
2893 | ||
2894 | if ((ptr = strchr(filename, '?')) != NULL) | |
2895 | *ptr = '\0'; | |
2896 | ||
2897 | /* | |
2898 | * Grab the status for this language; if there isn't a language-specific file | |
2899 | * then fallback to the default one... | |
2900 | */ | |
2901 | ||
2902 | if ((status = stat(filename, filestats)) != 0 && con->language != NULL) | |
2903 | { | |
2904 | /* | |
2905 | * Drop the language prefix and try the current directory... | |
2906 | */ | |
2907 | ||
2908 | if (strncmp(con->uri, "/ppd/", 5) && | |
2909 | strncmp(con->uri, "/admin/conf/", 12) && | |
2910 | strncmp(con->uri, "/admin/log/", 11)) | |
2911 | { | |
2912 | snprintf(filename, len, "%s%s", DocumentRoot, con->uri); | |
2913 | ||
2914 | if ((ptr = strchr(filename, '?')) != NULL) | |
2915 | *ptr = '\0'; | |
2916 | ||
2917 | status = stat(filename, filestats); | |
2918 | } | |
2919 | } | |
2920 | ||
2921 | /* | |
2922 | * If we're found a directory, get the index.html file instead... | |
2923 | */ | |
2924 | ||
2925 | if (!status && S_ISDIR(filestats->st_mode)) | |
2926 | { | |
2927 | if (filename[strlen(filename) - 1] != '/') | |
2928 | strlcat(filename, "/", len); | |
2929 | ||
2930 | ptr = filename + strlen(filename); | |
2931 | plen = len - (ptr - filename); | |
2932 | ||
2933 | strlcpy(ptr, "index.html", plen); | |
2934 | status = stat(filename, filestats); | |
2935 | ||
2936 | #ifdef HAVE_JAVA | |
2937 | if (status) | |
2938 | { | |
2939 | strlcpy(ptr, "index.class", plen); | |
2940 | status = stat(filename, filestats); | |
2941 | } | |
2942 | #endif /* HAVE_JAVA */ | |
2943 | ||
2944 | #ifdef HAVE_PERL | |
2945 | if (status) | |
2946 | { | |
2947 | strlcpy(ptr, "index.pl", plen); | |
2948 | status = stat(filename, filestats); | |
2949 | } | |
2950 | #endif /* HAVE_PERL */ | |
2951 | ||
2952 | #ifdef HAVE_PHP | |
2953 | if (status) | |
2954 | { | |
2955 | strlcpy(ptr, "index.php", plen); | |
2956 | status = stat(filename, filestats); | |
2957 | } | |
2958 | #endif /* HAVE_PHP */ | |
2959 | ||
2960 | #ifdef HAVE_PYTHON | |
2961 | if (status) | |
2962 | { | |
2963 | strlcpy(ptr, "index.pyc", plen); | |
2964 | status = stat(filename, filestats); | |
2965 | } | |
2966 | ||
2967 | if (status) | |
2968 | { | |
2969 | strlcpy(ptr, "index.py", plen); | |
2970 | status = stat(filename, filestats); | |
2971 | } | |
2972 | #endif /* HAVE_PYTHON */ | |
2973 | } | |
2974 | ||
2975 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_file: %d filename=%s size=%d", | |
2976 | con->http.fd, filename, | |
2977 | status ? -1 : (int)filestats->st_size); | |
2978 | ||
2979 | if (!status) | |
2980 | con->http.data_remaining = (int)filestats->st_size; | |
2981 | ||
2982 | if (status) | |
2983 | return (NULL); | |
2984 | else | |
2985 | return (filename); | |
2986 | } | |
2987 | ||
2988 | ||
2989 | /* | |
2990 | * 'install_conf_file()' - Install a configuration file. | |
2991 | */ | |
2992 | ||
2993 | static http_status_t /* O - Status */ | |
2994 | install_conf_file(cupsd_client_t *con) /* I - Connection */ | |
2995 | { | |
2996 | cups_file_t *in, /* Input file */ | |
2997 | *out; /* Output file */ | |
2998 | char buffer[1024]; /* Copy buffer */ | |
2999 | int bytes; /* Number of bytes */ | |
3000 | char conffile[1024], /* Configuration filename */ | |
3001 | newfile[1024], /* New config filename */ | |
3002 | oldfile[1024]; /* Old config filename */ | |
3003 | struct stat confinfo; /* Config file info */ | |
3004 | ||
3005 | ||
3006 | /* | |
3007 | * First construct the filenames... | |
3008 | */ | |
3009 | ||
3010 | snprintf(conffile, sizeof(conffile), "%s%s", ServerRoot, con->uri + 11); | |
3011 | snprintf(newfile, sizeof(newfile), "%s%s.N", ServerRoot, con->uri + 11); | |
3012 | snprintf(oldfile, sizeof(oldfile), "%s%s.O", ServerRoot, con->uri + 11); | |
3013 | ||
3014 | cupsdLogMessage(CUPSD_LOG_INFO, "Installing config file \"%s\"...", conffile); | |
3015 | ||
3016 | /* | |
3017 | * Get the owner, group, and permissions of the configuration file. | |
3018 | * If it doesn't exist, assign it to the User and Group in the | |
3019 | * cupsd.conf file with mode 0640 permissions. | |
3020 | */ | |
3021 | ||
3022 | if (stat(conffile, &confinfo)) | |
3023 | { | |
3024 | confinfo.st_uid = User; | |
3025 | confinfo.st_gid = Group; | |
3026 | confinfo.st_mode = ConfigFilePerm; | |
3027 | } | |
3028 | ||
3029 | /* | |
3030 | * Open the request file and new config file... | |
3031 | */ | |
3032 | ||
3033 | if ((in = cupsFileOpen(con->filename, "rb")) == NULL) | |
3034 | { | |
3035 | cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to open request file \"%s\" - %s", | |
3036 | con->filename, strerror(errno)); | |
3037 | return (HTTP_SERVER_ERROR); | |
3038 | } | |
3039 | ||
3040 | if ((out = cupsFileOpen(newfile, "wb")) == NULL) | |
3041 | { | |
3042 | cupsFileClose(in); | |
3043 | cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to open config file \"%s\" - %s", | |
3044 | newfile, strerror(errno)); | |
3045 | return (HTTP_SERVER_ERROR); | |
3046 | } | |
3047 | ||
3048 | fchmod(cupsFileNumber(out), confinfo.st_mode); | |
3049 | fchown(cupsFileNumber(out), confinfo.st_uid, confinfo.st_gid); | |
3050 | ||
3051 | /* | |
3052 | * Copy from the request to the new config file... | |
3053 | */ | |
3054 | ||
3055 | while ((bytes = cupsFileRead(in, buffer, sizeof(buffer))) > 0) | |
3056 | if (cupsFileWrite(out, buffer, bytes) < bytes) | |
3057 | { | |
3058 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
3059 | "Unable to copy to config file \"%s\" - %s", | |
3060 | newfile, strerror(errno)); | |
3061 | ||
3062 | cupsFileClose(in); | |
3063 | cupsFileClose(out); | |
3064 | unlink(newfile); | |
3065 | ||
3066 | return (HTTP_SERVER_ERROR); | |
3067 | } | |
3068 | ||
3069 | /* | |
3070 | * Close the files... | |
3071 | */ | |
3072 | ||
3073 | cupsFileClose(in); | |
3074 | if (cupsFileClose(out)) | |
3075 | { | |
3076 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
3077 | "Error file closing config file \"%s\" - %s", | |
3078 | newfile, strerror(errno)); | |
3079 | ||
3080 | unlink(newfile); | |
3081 | ||
3082 | return (HTTP_SERVER_ERROR); | |
3083 | } | |
3084 | ||
3085 | /* | |
3086 | * Remove the request file... | |
3087 | */ | |
3088 | ||
3089 | unlink(con->filename); | |
3090 | cupsdClearString(&con->filename); | |
3091 | ||
3092 | /* | |
3093 | * Unlink the old backup, rename the current config file to the backup | |
3094 | * filename, and rename the new config file to the config file name... | |
3095 | */ | |
3096 | ||
3097 | if (unlink(oldfile)) | |
3098 | if (errno != ENOENT) | |
3099 | { | |
3100 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
3101 | "Unable to remove backup config file \"%s\" - %s", | |
3102 | oldfile, strerror(errno)); | |
3103 | ||
3104 | unlink(newfile); | |
3105 | ||
3106 | return (HTTP_SERVER_ERROR); | |
3107 | } | |
3108 | ||
3109 | if (rename(conffile, oldfile)) | |
3110 | if (errno != ENOENT) | |
3111 | { | |
3112 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
3113 | "Unable to rename old config file \"%s\" - %s", | |
3114 | conffile, strerror(errno)); | |
3115 | ||
3116 | unlink(newfile); | |
3117 | ||
3118 | return (HTTP_SERVER_ERROR); | |
3119 | } | |
3120 | ||
3121 | if (rename(newfile, conffile)) | |
3122 | { | |
3123 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
3124 | "Unable to rename new config file \"%s\" - %s", | |
3125 | newfile, strerror(errno)); | |
3126 | ||
3127 | rename(oldfile, conffile); | |
3128 | unlink(newfile); | |
3129 | ||
3130 | return (HTTP_SERVER_ERROR); | |
3131 | } | |
3132 | ||
3133 | /* | |
3134 | * If the cupsd.conf file was updated, set the NeedReload flag... | |
3135 | */ | |
3136 | ||
3137 | if (!strcmp(con->uri, "/admin/conf/cupsd.conf")) | |
3138 | NeedReload = RELOAD_CUPSD; | |
3139 | else | |
3140 | NeedReload = RELOAD_ALL; | |
3141 | ||
3142 | ReloadTime = time(NULL); | |
3143 | ||
3144 | /* | |
3145 | * Return that the file was created successfully... | |
3146 | */ | |
3147 | ||
3148 | return (HTTP_CREATED); | |
3149 | } | |
3150 | ||
3151 | ||
3152 | /* | |
3153 | * 'is_path_absolute()' - Is a path absolute and free of relative elements (i.e. ".."). | |
3154 | */ | |
3155 | ||
3156 | static int /* O - 0 if relative, 1 if absolute */ | |
3157 | is_path_absolute(const char *path) /* I - Input path */ | |
3158 | { | |
3159 | /* | |
3160 | * Check for a leading slash... | |
3161 | */ | |
3162 | ||
3163 | if (path[0] != '/') | |
3164 | return (0); | |
3165 | ||
3166 | /* | |
3167 | * Check for "/.." in the path... | |
3168 | */ | |
3169 | ||
3170 | while ((path = strstr(path, "/..")) != NULL) | |
3171 | { | |
3172 | if (!path[3] || path[3] == '/') | |
3173 | return (0); | |
3174 | ||
3175 | path ++; | |
3176 | } | |
3177 | ||
3178 | /* | |
3179 | * If we haven't found any relative paths, return 1 indicating an | |
3180 | * absolute path... | |
3181 | */ | |
3182 | ||
3183 | return (1); | |
3184 | } | |
3185 | ||
3186 | ||
3187 | /* | |
3188 | * 'pipe_command()' - Pipe the output of a command to the remote client. | |
3189 | */ | |
3190 | ||
3191 | static int /* O - Process ID */ | |
3192 | pipe_command(cupsd_client_t *con, /* I - Client connection */ | |
3193 | int infile, /* I - Standard input for command */ | |
3194 | int *outfile, /* O - Standard output for command */ | |
3195 | char *command, /* I - Command to run */ | |
3196 | char *options, /* I - Options for command */ | |
3197 | int root) /* I - Run as root? */ | |
3198 | { | |
3199 | int i; /* Looping var */ | |
3200 | int pid; /* Process ID */ | |
3201 | char *commptr; /* Command string pointer */ | |
3202 | char *uriptr; /* URI string pointer */ | |
3203 | int fds[2]; /* Pipe FDs */ | |
3204 | int argc; /* Number of arguments */ | |
3205 | int envc; /* Number of environment variables */ | |
3206 | char argbuf[10240], /* Argument buffer */ | |
3207 | *argv[100], /* Argument strings */ | |
e00b005a | 3208 | *envp[MAX_ENV]; /* Environment variables */ |
ef416fc2 | 3209 | char content_length[1024], /* CONTENT_LENGTH environment variable */ |
3210 | content_type[1024], /* CONTENT_TYPE environment variable */ | |
3211 | http_cookie[32768], /* HTTP_COOKIE environment variable */ | |
3212 | http_user_agent[1024], /* HTTP_USER_AGENT environment variable */ | |
3213 | lang[1024], /* LANG environment variable */ | |
3214 | *query_string, /* QUERY_STRING env variable */ | |
3215 | remote_addr[1024], /* REMOTE_ADDR environment variable */ | |
3216 | remote_host[1024], /* REMOTE_HOST environment variable */ | |
3217 | remote_user[1024], /* REMOTE_USER environment variable */ | |
3218 | script_name[1024], /* SCRIPT_NAME environment variable */ | |
3219 | server_name[1024], /* SERVER_NAME environment variable */ | |
3220 | server_port[1024]; /* SERVER_PORT environment variable */ | |
3221 | ||
3222 | ||
3223 | /* | |
3224 | * Parse a copy of the options string, which is of the form: | |
3225 | * | |
3226 | * name argument+argument+argument | |
3227 | * name?argument+argument+argument | |
3228 | * name param=value¶m=value | |
3229 | * name?param=value¶m=value | |
3230 | * | |
3231 | * If the string contains an "=" character after the initial name, | |
3232 | * then we treat it as a HTTP GET form request and make a copy of | |
3233 | * the remaining string for the environment variable. | |
3234 | * | |
3235 | * The string is always parsed out as command-line arguments, to | |
3236 | * be consistent with Apache... | |
3237 | */ | |
3238 | ||
3239 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
3240 | "pipe_command: command=\"%s\", options=\"%s\"", | |
3241 | command, options); | |
3242 | ||
3243 | strlcpy(argbuf, options, sizeof(argbuf)); | |
3244 | ||
3245 | argv[0] = argbuf; | |
3246 | query_string = NULL; | |
3247 | ||
3248 | for (commptr = argbuf, argc = 1; *commptr != '\0' && argc < 99; commptr ++) | |
3249 | { | |
3250 | /* | |
3251 | * Break arguments whenever we see a + or space... | |
3252 | */ | |
3253 | ||
3254 | if (*commptr == ' ' || *commptr == '+' || (*commptr == '?' && argc == 1)) | |
3255 | { | |
3256 | /* | |
3257 | * Terminate the current string and skip trailing whitespace... | |
3258 | */ | |
3259 | ||
3260 | *commptr++ = '\0'; | |
3261 | ||
3262 | while (*commptr == ' ') | |
3263 | commptr ++; | |
3264 | ||
3265 | /* | |
3266 | * If we don't have a blank string, save it as another argument... | |
3267 | */ | |
3268 | ||
3269 | if (*commptr) | |
3270 | { | |
3271 | argv[argc] = commptr; | |
3272 | argc ++; | |
3273 | } | |
3274 | else | |
3275 | break; | |
3276 | ||
3277 | /* | |
3278 | * If we see an "=" in the remaining string, make a copy of it since | |
3279 | * it will be query data... | |
3280 | */ | |
3281 | ||
3282 | if (argc == 2 && strchr(commptr, '=') && con->operation == HTTP_GET) | |
3283 | cupsdSetStringf(&query_string, "QUERY_STRING=%s", commptr); | |
3284 | ||
3285 | /* | |
3286 | * Don't skip the first non-blank character... | |
3287 | */ | |
3288 | ||
3289 | commptr --; | |
3290 | } | |
3291 | else if (*commptr == '%' && isxdigit(commptr[1] & 255) && | |
3292 | isxdigit(commptr[2] & 255)) | |
3293 | { | |
3294 | /* | |
3295 | * Convert the %xx notation to the individual character. | |
3296 | */ | |
3297 | ||
3298 | if (commptr[1] >= '0' && commptr[1] <= '9') | |
3299 | *commptr = (commptr[1] - '0') << 4; | |
3300 | else | |
3301 | *commptr = (tolower(commptr[1]) - 'a' + 10) << 4; | |
3302 | ||
3303 | if (commptr[2] >= '0' && commptr[2] <= '9') | |
3304 | *commptr |= commptr[2] - '0'; | |
3305 | else | |
3306 | *commptr |= tolower(commptr[2]) - 'a' + 10; | |
3307 | ||
3308 | _cups_strcpy(commptr + 1, commptr + 3); | |
3309 | ||
3310 | /* | |
3311 | * Check for a %00 and break if that is the case... | |
3312 | */ | |
3313 | ||
3314 | if (!*commptr) | |
3315 | break; | |
3316 | } | |
3317 | } | |
3318 | ||
3319 | argv[argc] = NULL; | |
3320 | ||
3321 | if (argv[0][0] == '\0') | |
3322 | argv[0] = strrchr(command, '/') + 1; | |
3323 | ||
3324 | /* | |
3325 | * Setup the environment variables as needed... | |
3326 | */ | |
3327 | ||
3328 | if (con->language) | |
3329 | snprintf(lang, sizeof(lang), "LANG=%s.UTF-8", con->language->language); | |
3330 | else | |
3331 | strcpy(lang, "LANG=C"); | |
3332 | ||
3333 | strcpy(remote_addr, "REMOTE_ADDR="); | |
3334 | httpAddrString(con->http.hostaddr, remote_addr + 12, | |
3335 | sizeof(remote_addr) - 12); | |
3336 | ||
3337 | snprintf(remote_host, sizeof(remote_host), "REMOTE_HOST=%s", | |
3338 | con->http.hostname); | |
3339 | ||
3340 | snprintf(script_name, sizeof(script_name), "SCRIPT_NAME=%s", con->uri); | |
3341 | if ((uriptr = strchr(script_name, '?')) != NULL) | |
3342 | *uriptr = '\0'; | |
3343 | ||
3344 | sprintf(server_port, "SERVER_PORT=%d", con->serverport); | |
3345 | ||
3346 | snprintf(server_name, sizeof(server_name), "SERVER_NAME=%s", | |
3347 | con->servername); | |
3348 | ||
3349 | envc = cupsdLoadEnv(envp, (int)(sizeof(envp) / sizeof(envp[0]))); | |
3350 | ||
3351 | envp[envc ++] = lang; | |
3352 | envp[envc ++] = "REDIRECT_STATUS=1"; | |
3353 | envp[envc ++] = server_name; | |
3354 | envp[envc ++] = server_port; | |
3355 | envp[envc ++] = remote_addr; | |
3356 | envp[envc ++] = remote_host; | |
3357 | envp[envc ++] = script_name; | |
3358 | ||
3359 | if (con->username[0]) | |
3360 | { | |
3361 | snprintf(remote_user, sizeof(remote_user), "REMOTE_USER=%s", con->username); | |
3362 | ||
3363 | envp[envc ++] = remote_user; | |
3364 | } | |
3365 | ||
3366 | if (con->http.version == HTTP_1_1) | |
3367 | envp[envc ++] = "SERVER_PROTOCOL=HTTP/1.1"; | |
3368 | else if (con->http.version == HTTP_1_0) | |
3369 | envp[envc ++] = "SERVER_PROTOCOL=HTTP/1.0"; | |
3370 | else | |
3371 | envp[envc ++] = "SERVER_PROTOCOL=HTTP/0.9"; | |
3372 | ||
3373 | if (con->http.cookie) | |
3374 | { | |
3375 | snprintf(http_cookie, sizeof(http_cookie), "HTTP_COOKIE=%s", | |
3376 | con->http.cookie); | |
3377 | envp[envc ++] = http_cookie; | |
3378 | } | |
3379 | ||
3380 | if (con->http.fields[HTTP_FIELD_USER_AGENT][0]) | |
3381 | { | |
3382 | snprintf(http_user_agent, sizeof(http_user_agent), "HTTP_USER_AGENT=%s", | |
3383 | con->http.fields[HTTP_FIELD_USER_AGENT]); | |
3384 | envp[envc ++] = http_user_agent; | |
3385 | } | |
3386 | ||
3387 | if (con->operation == HTTP_GET) | |
3388 | { | |
3389 | for (i = 0; i < argc; i ++) | |
3390 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "argv[%d] = \"%s\"", i, argv[i]); | |
3391 | ||
3392 | envp[envc ++] = "REQUEST_METHOD=GET"; | |
3393 | ||
3394 | if (query_string) | |
3395 | { | |
3396 | /* | |
3397 | * Add GET form variables after ?... | |
3398 | */ | |
3399 | ||
3400 | envp[envc ++] = query_string; | |
3401 | } | |
3402 | } | |
3403 | else | |
3404 | { | |
3405 | sprintf(content_length, "CONTENT_LENGTH=" CUPS_LLFMT, con->bytes); | |
3406 | snprintf(content_type, sizeof(content_type), "CONTENT_TYPE=%s", | |
3407 | con->http.fields[HTTP_FIELD_CONTENT_TYPE]); | |
3408 | ||
3409 | envp[envc ++] = "REQUEST_METHOD=POST"; | |
3410 | envp[envc ++] = content_length; | |
3411 | envp[envc ++] = content_type; | |
3412 | } | |
3413 | ||
3414 | /* | |
3415 | * Tell the CGI if we are using encryption... | |
3416 | */ | |
3417 | ||
3418 | if (con->http.encryption == HTTP_ENCRYPT_ALWAYS) | |
3419 | envp[envc ++] = "HTTPS=ON"; | |
3420 | ||
3421 | /* | |
3422 | * Terminate the environment array... | |
3423 | */ | |
3424 | ||
3425 | envp[envc] = NULL; | |
3426 | ||
3427 | if (LogLevel == CUPSD_LOG_DEBUG2) | |
3428 | { | |
3429 | for (i = 0; i < argc; i ++) | |
3430 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
3431 | "pipe_command: argv[%d] = \"%s\"", i, argv[i]); | |
3432 | for (i = 0; i < envc; i ++) | |
3433 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
3434 | "pipe_command: envp[%d] = \"%s\"", i, envp[i]); | |
3435 | } | |
3436 | ||
3437 | /* | |
3438 | * Create a pipe for the output... | |
3439 | */ | |
3440 | ||
3441 | if (cupsdOpenPipe(fds)) | |
3442 | { | |
3443 | cupsdClearString(&query_string); | |
3444 | ||
3445 | cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to create pipes for CGI %s - %s", | |
3446 | argv[0], strerror(errno)); | |
3447 | return (0); | |
3448 | } | |
3449 | ||
3450 | /* | |
3451 | * Then execute the command... | |
3452 | */ | |
3453 | ||
3454 | if (cupsdStartProcess(command, argv, envp, infile, fds[1], CGIPipes[1], | |
3455 | -1, root, &pid) < 0) | |
3456 | { | |
3457 | /* | |
3458 | * Error - can't fork! | |
3459 | */ | |
3460 | ||
3461 | cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to fork for CGI %s - %s", argv[0], | |
3462 | strerror(errno)); | |
3463 | ||
3464 | cupsdClosePipe(fds); | |
3465 | pid = 0; | |
3466 | } | |
3467 | else | |
3468 | { | |
3469 | /* | |
3470 | * Fork successful - return the PID... | |
3471 | */ | |
3472 | ||
3473 | if (con->username[0]) | |
3474 | cupsdAddCert(pid, con->username); | |
3475 | ||
3476 | cupsdLogMessage(CUPSD_LOG_DEBUG, "CGI %s started - PID = %d", command, pid); | |
3477 | ||
3478 | *outfile = fds[0]; | |
3479 | close(fds[1]); | |
3480 | } | |
3481 | ||
3482 | cupsdClearString(&query_string); | |
3483 | ||
3484 | return (pid); | |
3485 | } | |
3486 | ||
3487 | ||
3488 | /* | |
e00b005a | 3489 | * End of "$Id: client.c 5046 2006-02-01 22:11:58Z mike $". |
ef416fc2 | 3490 | */ |