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