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