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