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