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