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