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