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