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