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