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