]>
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) | |
8b116e60 MS |
2648 | { |
2649 | /* | |
2650 | * If we get called in the wrong state, then something went wrong with the | |
2651 | * connection and we need to shut it down... | |
2652 | */ | |
2653 | ||
2654 | cupsdCloseClient(con); | |
f7deaa1a | 2655 | return; |
8b116e60 | 2656 | } |
f7deaa1a | 2657 | |
2658 | if (con->pipe_pid) | |
2659 | { | |
2660 | /* | |
2661 | * Make sure we select on the CGI output... | |
2662 | */ | |
2663 | ||
f899b121 | 2664 | cupsdAddSelect(con->file, (cupsd_selfunc_t)write_pipe, NULL, con); |
f7deaa1a | 2665 | |
2666 | if (!con->file_ready) | |
2667 | { | |
2668 | /* | |
2669 | * Try again later when there is CGI output available... | |
2670 | */ | |
2671 | ||
2672 | cupsdRemoveSelect(con->http.fd); | |
2673 | return; | |
2674 | } | |
2675 | ||
2676 | con->file_ready = 0; | |
2677 | } | |
ef416fc2 | 2678 | |
b94498cf | 2679 | if (con->response && con->response->state != IPP_DATA) |
ef416fc2 | 2680 | { |
07725fee | 2681 | ipp_state = ippWrite(HTTP(con), con->response); |
b94498cf | 2682 | bytes = ipp_state != IPP_ERROR && |
2683 | (con->file >= 0 || ipp_state != IPP_DATA); | |
ef416fc2 | 2684 | } |
2685 | else if ((bytes = read(con->file, buf, sizeof(buf) - 1)) > 0) | |
2686 | { | |
ef416fc2 | 2687 | if (con->pipe_pid && !con->got_fields) |
2688 | { | |
2689 | /* | |
2690 | * Inspect the data for Content-Type and other fields. | |
2691 | */ | |
2692 | ||
2693 | buf[bytes] = '\0'; | |
2694 | ||
2695 | for (bufptr = buf; !con->got_fields && *bufptr; bufptr ++) | |
d1c13e16 | 2696 | { |
ef416fc2 | 2697 | if (*bufptr == '\n') |
2698 | { | |
2699 | /* | |
2700 | * Send line to client... | |
2701 | */ | |
2702 | ||
2703 | if (bufptr > buf && bufptr[-1] == '\r') | |
2704 | bufptr[-1] = '\0'; | |
2705 | *bufptr++ = '\0'; | |
2706 | ||
d1c13e16 | 2707 | cupsdLogMessage(CUPSD_LOG_DEBUG, "Script header: %s", buf); |
ef416fc2 | 2708 | |
2709 | if (!con->sent_header) | |
2710 | { | |
2711 | /* | |
2712 | * Handle redirection and CGI status codes... | |
2713 | */ | |
2714 | ||
2715 | if (!strncasecmp(buf, "Location:", 9)) | |
d6ae789d | 2716 | { |
5bd77a73 | 2717 | cupsdSendHeader(con, HTTP_SEE_OTHER, NULL, CUPSD_AUTH_NONE); |
07725fee | 2718 | con->sent_header = 2; |
2719 | ||
d6ae789d | 2720 | if (httpPrintf(HTTP(con), "Content-Length: 0\r\n") < 0) |
f7deaa1a | 2721 | return; |
d6ae789d | 2722 | } |
ef416fc2 | 2723 | else if (!strncasecmp(buf, "Status:", 7)) |
07725fee | 2724 | { |
5bd77a73 | 2725 | cupsdSendError(con, (http_status_t)atoi(buf + 7), CUPSD_AUTH_NONE); |
07725fee | 2726 | con->sent_header = 2; |
2727 | } | |
ef416fc2 | 2728 | else |
4744bd90 | 2729 | { |
5bd77a73 | 2730 | cupsdSendHeader(con, HTTP_OK, NULL, CUPSD_AUTH_NONE); |
07725fee | 2731 | con->sent_header = 1; |
ef416fc2 | 2732 | |
4744bd90 | 2733 | if (con->http.version == HTTP_1_1) |
2734 | { | |
4744bd90 | 2735 | if (httpPrintf(HTTP(con), "Transfer-Encoding: chunked\r\n") < 0) |
f7deaa1a | 2736 | return; |
4744bd90 | 2737 | } |
2738 | } | |
ef416fc2 | 2739 | } |
2740 | ||
2741 | if (strncasecmp(buf, "Status:", 7)) | |
2742 | httpPrintf(HTTP(con), "%s\r\n", buf); | |
2743 | ||
ef416fc2 | 2744 | /* |
2745 | * Update buffer... | |
2746 | */ | |
2747 | ||
2748 | bytes -= (bufptr - buf); | |
d1c13e16 MS |
2749 | |
2750 | if (bytes > 0) | |
2751 | memmove(buf, bufptr, bytes + 1); | |
2752 | else | |
2753 | buf[0] = '\0'; | |
2754 | ||
ef416fc2 | 2755 | bufptr = buf - 1; |
2756 | ||
2757 | /* | |
2758 | * See if the line was empty... | |
2759 | */ | |
2760 | ||
2761 | if (con->field_col == 0) | |
d09495fa | 2762 | { |
ef416fc2 | 2763 | con->got_fields = 1; |
d09495fa | 2764 | |
07725fee | 2765 | if (cupsdFlushHeader(con) < 0) |
2766 | { | |
2767 | cupsdCloseClient(con); | |
f7deaa1a | 2768 | return; |
07725fee | 2769 | } |
d09495fa | 2770 | |
2771 | if (con->http.version == HTTP_1_1) | |
2772 | con->http.data_encoding = HTTP_ENCODE_CHUNKED; | |
2773 | } | |
ef416fc2 | 2774 | else |
2775 | con->field_col = 0; | |
2776 | } | |
2777 | else if (*bufptr != '\r') | |
2778 | con->field_col ++; | |
d1c13e16 | 2779 | } |
ef416fc2 | 2780 | |
ef416fc2 | 2781 | if (bytes > 0 && !con->got_fields) |
2782 | { | |
2783 | /* | |
2784 | * Remaining text needs to go out... | |
2785 | */ | |
2786 | ||
2787 | httpPrintf(HTTP(con), "%s", buf); | |
2788 | ||
2789 | con->http.activity = time(NULL); | |
f7deaa1a | 2790 | return; |
ef416fc2 | 2791 | } |
2792 | else if (bytes == 0) | |
47879b8b | 2793 | { |
ef416fc2 | 2794 | con->http.activity = time(NULL); |
47879b8b MS |
2795 | return; |
2796 | } | |
ef416fc2 | 2797 | } |
2798 | ||
4744bd90 | 2799 | if (bytes > 0) |
ef416fc2 | 2800 | { |
4744bd90 | 2801 | if (httpWrite2(HTTP(con), buf, bytes) < 0) |
2802 | { | |
4744bd90 | 2803 | cupsdCloseClient(con); |
f7deaa1a | 2804 | return; |
4744bd90 | 2805 | } |
ef416fc2 | 2806 | |
01ce6322 MS |
2807 | if (con->http.data_encoding == HTTP_ENCODE_CHUNKED) |
2808 | httpFlushWrite(HTTP(con)); | |
ae71f5de | 2809 | |
4744bd90 | 2810 | con->bytes += bytes; |
ef416fc2 | 2811 | |
4744bd90 | 2812 | if (con->http.state == HTTP_WAITING) |
2813 | bytes = 0; | |
2814 | } | |
ef416fc2 | 2815 | } |
2816 | ||
8b116e60 MS |
2817 | if (bytes <= 0 || |
2818 | (con->http.state != HTTP_GET_SEND && con->http.state != HTTP_POST_SEND)) | |
ef416fc2 | 2819 | { |
ef416fc2 | 2820 | cupsdLogRequest(con, HTTP_OK); |
2821 | ||
2822 | httpFlushWrite(HTTP(con)); | |
2823 | ||
07725fee | 2824 | if (con->http.data_encoding == HTTP_ENCODE_CHUNKED && con->sent_header == 1) |
ef416fc2 | 2825 | { |
07725fee | 2826 | if (httpWrite2(HTTP(con), "", 0) < 0) |
ef416fc2 | 2827 | { |
2828 | cupsdCloseClient(con); | |
f7deaa1a | 2829 | return; |
ef416fc2 | 2830 | } |
2831 | } | |
2832 | ||
2833 | con->http.state = HTTP_WAITING; | |
2834 | ||
f7deaa1a | 2835 | cupsdAddSelect(con->http.fd, (cupsd_selfunc_t)cupsdReadClient, NULL, con); |
ef416fc2 | 2836 | |
2837 | if (con->file >= 0) | |
2838 | { | |
f7deaa1a | 2839 | cupsdRemoveSelect(con->file); |
ef416fc2 | 2840 | |
2841 | if (con->pipe_pid) | |
2842 | cupsdEndProcess(con->pipe_pid, 0); | |
2843 | ||
ef416fc2 | 2844 | close(con->file); |
2845 | con->file = -1; | |
2846 | con->pipe_pid = 0; | |
2847 | } | |
2848 | ||
2849 | if (con->filename) | |
2850 | { | |
ef416fc2 | 2851 | unlink(con->filename); |
2852 | cupsdClearString(&con->filename); | |
2853 | } | |
2854 | ||
2abf387c | 2855 | if (con->request) |
ef416fc2 | 2856 | { |
2857 | ippDelete(con->request); | |
2858 | con->request = NULL; | |
2859 | } | |
2860 | ||
2abf387c | 2861 | if (con->response) |
ef416fc2 | 2862 | { |
2863 | ippDelete(con->response); | |
2864 | con->response = NULL; | |
2865 | } | |
2866 | ||
2867 | cupsdClearString(&con->command); | |
2868 | cupsdClearString(&con->options); | |
b86bc4cf | 2869 | cupsdClearString(&con->query_string); |
ef416fc2 | 2870 | |
2871 | if (!con->http.keep_alive) | |
2872 | { | |
2873 | cupsdCloseClient(con); | |
f7deaa1a | 2874 | return; |
ef416fc2 | 2875 | } |
2876 | } | |
2877 | ||
2878 | con->http.activity = time(NULL); | |
f7deaa1a | 2879 | } |
ef416fc2 | 2880 | |
f7deaa1a | 2881 | |
e1d6a774 | 2882 | /* |
2883 | * 'check_if_modified()' - Decode an "If-Modified-Since" line. | |
2884 | */ | |
2885 | ||
2886 | static int /* O - 1 if modified since */ | |
2887 | check_if_modified( | |
2888 | cupsd_client_t *con, /* I - Client connection */ | |
2889 | struct stat *filestats) /* I - File information */ | |
2890 | { | |
2891 | char *ptr; /* Pointer into field */ | |
2892 | time_t date; /* Time/date value */ | |
2893 | off_t size; /* Size/length value */ | |
2894 | ||
2895 | ||
2896 | size = 0; | |
2897 | date = 0; | |
2898 | ptr = con->http.fields[HTTP_FIELD_IF_MODIFIED_SINCE]; | |
2899 | ||
2900 | if (*ptr == '\0') | |
2901 | return (1); | |
2902 | ||
2903 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
b9faaae1 MS |
2904 | "check_if_modified(con=%p(%d), " |
2905 | "filestats=%p(" CUPS_LLFMT ", %d)) If-Modified-Since=\"%s\"", | |
2906 | con, con->http.fd, filestats, CUPS_LLCAST filestats->st_size, | |
2907 | (int)filestats->st_mtime, ptr); | |
e1d6a774 | 2908 | |
2909 | while (*ptr != '\0') | |
2910 | { | |
2911 | while (isspace(*ptr) || *ptr == ';') | |
2912 | ptr ++; | |
2913 | ||
2914 | if (strncasecmp(ptr, "length=", 7) == 0) | |
2915 | { | |
2916 | ptr += 7; | |
2917 | size = strtoll(ptr, NULL, 10); | |
2918 | ||
2919 | while (isdigit(*ptr)) | |
2920 | ptr ++; | |
2921 | } | |
2922 | else if (isalpha(*ptr)) | |
2923 | { | |
2924 | date = httpGetDateTime(ptr); | |
2925 | while (*ptr != '\0' && *ptr != ';') | |
2926 | ptr ++; | |
2927 | } | |
e53920b9 | 2928 | else |
2929 | ptr ++; | |
e1d6a774 | 2930 | } |
2931 | ||
e1d6a774 | 2932 | return ((size != filestats->st_size && size != 0) || |
2933 | (date < filestats->st_mtime && date != 0) || | |
2934 | (size == 0 && date == 0)); | |
2935 | } | |
2936 | ||
2937 | ||
3dfe78b3 MS |
2938 | /* |
2939 | * 'compare_clients()' - Compare two client connections. | |
2940 | */ | |
2941 | ||
2942 | static int /* O - Result of comparison */ | |
2943 | compare_clients(cupsd_client_t *a, /* I - First client */ | |
2944 | cupsd_client_t *b, /* I - Second client */ | |
2945 | void *data) /* I - User data (not used) */ | |
2946 | { | |
2947 | (void)data; | |
2948 | ||
2949 | if (a == b) | |
2950 | return (0); | |
2951 | else if (a < b) | |
2952 | return (-1); | |
2953 | else | |
2954 | return (1); | |
2955 | } | |
2956 | ||
2957 | ||
ee571f26 MS |
2958 | /* |
2959 | * 'data_ready()' - Check whether data is available from a client. | |
2960 | */ | |
2961 | ||
2962 | static int /* O - 1 if data is ready, 0 otherwise */ | |
2963 | data_ready(cupsd_client_t *con) /* I - Client */ | |
2964 | { | |
2965 | if (con->http.used > 0) | |
2966 | return (1); | |
2967 | #ifdef HAVE_SSL | |
2968 | else if (con->http.tls) | |
2969 | { | |
2970 | # ifdef HAVE_LIBSSL | |
2971 | if (SSL_pending((SSL *)(con->http.tls))) | |
2972 | return (1); | |
2973 | # elif defined(HAVE_GNUTLS) | |
2974 | if (gnutls_record_check_pending(((http_tls_t *)(con->http.tls))->session)) | |
2975 | return (1); | |
2976 | # elif defined(HAVE_CDSASSL) | |
2977 | size_t bytes; /* Bytes that are available */ | |
2978 | ||
2979 | if (!SSLGetBufferedReadSize(((http_tls_t *)(con->http.tls))->session, | |
2980 | &bytes) && bytes > 0) | |
2981 | return (1); | |
2982 | # endif /* HAVE_LIBSSL */ | |
2983 | } | |
2984 | #endif /* HAVE_SSL */ | |
2985 | ||
2986 | return (0); | |
2987 | } | |
2988 | ||
2989 | ||
80ca4592 | 2990 | #ifdef HAVE_SSL |
e1d6a774 | 2991 | /* |
2992 | * 'encrypt_client()' - Enable encryption for the client... | |
2993 | */ | |
2994 | ||
2995 | static int /* O - 1 on success, 0 on error */ | |
2996 | encrypt_client(cupsd_client_t *con) /* I - Client to encrypt */ | |
2997 | { | |
80ca4592 | 2998 | # ifdef HAVE_LIBSSL |
e1d6a774 | 2999 | SSL_CTX *context; /* Context for encryption */ |
3000 | SSL *conn; /* Connection for encryption */ | |
411affcf | 3001 | BIO *bio; /* BIO data */ |
e1d6a774 | 3002 | unsigned long error; /* Error code */ |
3003 | ||
3004 | ||
b9faaae1 MS |
3005 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "encrypt_client(con=%p(%d))", con, |
3006 | con->http.fd); | |
3007 | ||
8ca02f3c | 3008 | /* |
3009 | * Verify that we have a certificate... | |
3010 | */ | |
3011 | ||
3012 | if (access(ServerKey, 0) || access(ServerCertificate, 0)) | |
3013 | { | |
3014 | /* | |
3015 | * Nope, make a self-signed certificate... | |
3016 | */ | |
3017 | ||
b86bc4cf | 3018 | if (!make_certificate(con)) |
8ca02f3c | 3019 | return (0); |
3020 | } | |
3021 | ||
e1d6a774 | 3022 | /* |
3023 | * Create the SSL context and accept the connection... | |
3024 | */ | |
3025 | ||
3026 | context = SSL_CTX_new(SSLv23_server_method()); | |
3027 | ||
3028 | SSL_CTX_set_options(context, SSL_OP_NO_SSLv2); /* Only use SSLv3 or TLS */ | |
b19ccc9e | 3029 | if (SSLOptions & CUPSD_SSL_NOEMPTY) |
58dc1933 | 3030 | SSL_CTX_set_options(context, SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS); |
e1d6a774 | 3031 | SSL_CTX_use_PrivateKey_file(context, ServerKey, SSL_FILETYPE_PEM); |
58dc1933 | 3032 | SSL_CTX_use_certificate_chain_file(context, ServerCertificate); |
e1d6a774 | 3033 | |
411affcf | 3034 | bio = BIO_new(_httpBIOMethods()); |
3035 | BIO_ctrl(bio, BIO_C_SET_FILE_PTR, 0, (char *)HTTP(con)); | |
3036 | ||
e1d6a774 | 3037 | conn = SSL_new(context); |
411affcf | 3038 | SSL_set_bio(conn, bio, bio); |
e1d6a774 | 3039 | |
e1d6a774 | 3040 | if (SSL_accept(conn) != 1) |
3041 | { | |
b9faaae1 | 3042 | cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to encrypt connection from %s!", |
e1d6a774 | 3043 | con->http.hostname); |
3044 | ||
3045 | while ((error = ERR_get_error()) != 0) | |
b9faaae1 | 3046 | cupsdLogMessage(CUPSD_LOG_ERROR, "%s", ERR_error_string(error, NULL)); |
e1d6a774 | 3047 | |
3048 | SSL_CTX_free(context); | |
3049 | SSL_free(conn); | |
3050 | return (0); | |
3051 | } | |
3052 | ||
b9faaae1 MS |
3053 | cupsdLogMessage(CUPSD_LOG_DEBUG, "Connection from %s now encrypted.", |
3054 | con->http.hostname); | |
e1d6a774 | 3055 | |
3056 | con->http.tls = conn; | |
3057 | return (1); | |
f7deaa1a | 3058 | |
80ca4592 | 3059 | # elif defined(HAVE_GNUTLS) |
e1d6a774 | 3060 | http_tls_t *conn; /* TLS session object */ |
3061 | int error; /* Error code */ | |
3062 | gnutls_certificate_server_credentials *credentials; | |
3063 | /* TLS credentials */ | |
3064 | ||
3065 | ||
b9faaae1 MS |
3066 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "encrypt_client(con=%p(%d))", con, |
3067 | con->http.fd); | |
3068 | ||
e1d6a774 | 3069 | /* |
3070 | * Verify that we have a certificate... | |
3071 | */ | |
3072 | ||
3073 | if (access(ServerKey, 0) || access(ServerCertificate, 0)) | |
3074 | { | |
3075 | /* | |
3076 | * Nope, make a self-signed certificate... | |
3077 | */ | |
3078 | ||
b86bc4cf | 3079 | if (!make_certificate(con)) |
8ca02f3c | 3080 | return (0); |
e1d6a774 | 3081 | } |
3082 | ||
3083 | /* | |
3084 | * Create the SSL object and perform the SSL handshake... | |
3085 | */ | |
3086 | ||
89d46774 | 3087 | conn = (http_tls_t *)malloc(sizeof(http_tls_t)); |
e1d6a774 | 3088 | |
3089 | if (conn == NULL) | |
3090 | return (0); | |
3091 | ||
3092 | credentials = (gnutls_certificate_server_credentials *) | |
3093 | malloc(sizeof(gnutls_certificate_server_credentials)); | |
3094 | if (credentials == NULL) | |
3095 | { | |
3096 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
b9faaae1 MS |
3097 | "Unable to encrypt connection from %s - %s", |
3098 | con->http.hostname, strerror(errno)); | |
e1d6a774 | 3099 | |
3100 | free(conn); | |
3101 | return (0); | |
3102 | } | |
3103 | ||
3104 | gnutls_certificate_allocate_credentials(credentials); | |
f7deaa1a | 3105 | gnutls_certificate_set_x509_key_file(*credentials, ServerCertificate, |
e1d6a774 | 3106 | ServerKey, GNUTLS_X509_FMT_PEM); |
3107 | ||
3108 | gnutls_init(&(conn->session), GNUTLS_SERVER); | |
3109 | gnutls_set_default_priority(conn->session); | |
3110 | gnutls_credentials_set(conn->session, GNUTLS_CRD_CERTIFICATE, *credentials); | |
411affcf | 3111 | gnutls_transport_set_ptr(conn->session, (gnutls_transport_ptr)HTTP(con)); |
3112 | gnutls_transport_set_pull_function(conn->session, _httpReadGNUTLS); | |
3113 | gnutls_transport_set_push_function(conn->session, _httpWriteGNUTLS); | |
e1d6a774 | 3114 | |
3115 | error = gnutls_handshake(conn->session); | |
3116 | ||
3117 | if (error != GNUTLS_E_SUCCESS) | |
3118 | { | |
3119 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
b9faaae1 MS |
3120 | "Unable to encrypt connection from %s - %s", |
3121 | con->http.hostname, gnutls_strerror(error)); | |
e1d6a774 | 3122 | |
3123 | gnutls_deinit(conn->session); | |
3124 | gnutls_certificate_free_credentials(*credentials); | |
3125 | free(conn); | |
3126 | free(credentials); | |
3127 | return (0); | |
3128 | } | |
3129 | ||
b9faaae1 MS |
3130 | cupsdLogMessage(CUPSD_LOG_DEBUG, "Connection from %s now encrypted.", |
3131 | con->http.hostname); | |
e1d6a774 | 3132 | |
3133 | conn->credentials = credentials; | |
3134 | con->http.tls = conn; | |
3135 | return (1); | |
3136 | ||
80ca4592 | 3137 | # elif defined(HAVE_CDSASSL) |
89d46774 | 3138 | OSStatus error; /* Error code */ |
3139 | http_tls_t *conn; /* CDSA connection information */ | |
e1d6a774 | 3140 | |
3141 | ||
b9faaae1 MS |
3142 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "encrypt_client(con=%p(%d))", con, |
3143 | con->http.fd); | |
3144 | ||
89d46774 | 3145 | if ((conn = (http_tls_t *)malloc(sizeof(http_tls_t))) == NULL) |
3146 | return (0); | |
e1d6a774 | 3147 | |
89d46774 | 3148 | error = 0; |
3149 | conn->session = NULL; | |
b86bc4cf | 3150 | conn->certsArray = get_cdsa_certificate(con); |
e1d6a774 | 3151 | |
8ca02f3c | 3152 | if (!conn->certsArray) |
3153 | { | |
3154 | /* | |
3155 | * No keychain (yet), make a self-signed certificate... | |
3156 | */ | |
3157 | ||
b86bc4cf | 3158 | if (make_certificate(con)) |
3159 | conn->certsArray = get_cdsa_certificate(con); | |
8ca02f3c | 3160 | } |
3161 | ||
89d46774 | 3162 | if (!conn->certsArray) |
e1d6a774 | 3163 | { |
3164 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
b9faaae1 MS |
3165 | "Could not find signing key in keychain \"%s\"", |
3166 | ServerCertificate); | |
e1d6a774 | 3167 | error = errSSLBadCert; /* errSSLBadConfiguration is a better choice, but not available on 10.2.x */ |
3168 | } | |
3169 | ||
3170 | if (!error) | |
89d46774 | 3171 | error = SSLNewContext(true, &conn->session); |
e1d6a774 | 3172 | |
3173 | if (!error) | |
89d46774 | 3174 | error = SSLSetIOFuncs(conn->session, _httpReadCDSA, _httpWriteCDSA); |
3175 | ||
3176 | if (!error) | |
ed486911 | 3177 | error = SSLSetProtocolVersionEnabled(conn->session, kSSLProtocol2, false); |
e1d6a774 | 3178 | |
3179 | if (!error) | |
411affcf | 3180 | error = SSLSetConnection(conn->session, HTTP(con)); |
e1d6a774 | 3181 | |
3182 | if (!error) | |
89d46774 | 3183 | error = SSLSetAllowsExpiredCerts(conn->session, true); |
e1d6a774 | 3184 | |
3185 | if (!error) | |
89d46774 | 3186 | error = SSLSetAllowsAnyRoot(conn->session, true); |
ef416fc2 | 3187 | |
89d46774 | 3188 | if (!error) |
3189 | error = SSLSetCertificate(conn->session, conn->certsArray); | |
ef416fc2 | 3190 | |
e1d6a774 | 3191 | if (!error) |
3192 | { | |
3193 | /* | |
3194 | * Perform SSL/TLS handshake | |
3195 | */ | |
ef416fc2 | 3196 | |
89d46774 | 3197 | while ((error = SSLHandshake(conn->session)) == errSSLWouldBlock) |
e1d6a774 | 3198 | usleep(1000); |
3199 | } | |
ef416fc2 | 3200 | |
e1d6a774 | 3201 | if (error) |
3202 | { | |
3203 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
b9faaae1 MS |
3204 | "Unable to encrypt connection from %s - %s (%d)", |
3205 | con->http.hostname, cssmErrorString(error), (int)error); | |
ef416fc2 | 3206 | |
e1d6a774 | 3207 | con->http.error = error; |
3208 | con->http.status = HTTP_ERROR; | |
ef416fc2 | 3209 | |
89d46774 | 3210 | if (conn->session) |
3211 | SSLDisposeContext(conn->session); | |
3212 | ||
3213 | if (conn->certsArray) | |
3214 | CFRelease(conn->certsArray); | |
3215 | ||
3216 | free(conn); | |
ef416fc2 | 3217 | |
e1d6a774 | 3218 | return (0); |
ef416fc2 | 3219 | } |
3220 | ||
b9faaae1 MS |
3221 | cupsdLogMessage(CUPSD_LOG_DEBUG, "Connection from %s now encrypted.", |
3222 | con->http.hostname); | |
ef416fc2 | 3223 | |
e1d6a774 | 3224 | con->http.tls = conn; |
3225 | return (1); | |
3226 | ||
80ca4592 | 3227 | # endif /* HAVE_LIBSSL */ |
ef416fc2 | 3228 | } |
80ca4592 | 3229 | #endif /* HAVE_SSL */ |
ef416fc2 | 3230 | |
3231 | ||
3232 | #ifdef HAVE_CDSASSL | |
3233 | /* | |
b86bc4cf | 3234 | * 'get_cdsa_certificate()' - Get a SSL/TLS certificate from the System keychain. |
ef416fc2 | 3235 | */ |
3236 | ||
b86bc4cf | 3237 | static CFArrayRef /* O - Array of certificates */ |
3238 | get_cdsa_certificate(cupsd_client_t *con) /* I - Client connection */ | |
ef416fc2 | 3239 | { |
3240 | OSStatus err; /* Error info */ | |
b86bc4cf | 3241 | SecKeychainRef keychain; /* Keychain reference */ |
3242 | SecIdentitySearchRef search; /* Search reference */ | |
ef416fc2 | 3243 | SecIdentityRef identity; /* Identity */ |
b86bc4cf | 3244 | CFArrayRef certificates = NULL; |
3245 | /* Certificate array */ | |
ef416fc2 | 3246 | |
3247 | ||
b86bc4cf | 3248 | if ((err = SecKeychainOpen(ServerCertificate, &keychain))) |
3249 | { | |
b9faaae1 MS |
3250 | cupsdLogMessage(CUPSD_LOG_ERROR, "Cannot open keychain \"%s\" - %s (%d)", |
3251 | ServerCertificate, cssmErrorString(err), (int)err); | |
b86bc4cf | 3252 | return (NULL); |
3253 | } | |
ef416fc2 | 3254 | |
b86bc4cf | 3255 | # if HAVE_SECIDENTITYSEARCHCREATEWITHPOLICY |
3256 | /* | |
3257 | * Use a policy to search for valid certificates who's common name matches the | |
3258 | * servername... | |
3259 | */ | |
3260 | ||
3261 | SecPolicySearchRef policy_search; /* Policy search ref */ | |
3262 | SecPolicyRef policy; /* Policy ref */ | |
3263 | CSSM_DATA options; /* Policy options */ | |
3264 | CSSM_APPLE_TP_SSL_OPTIONS | |
3265 | ssl_options; /* SSL Option for hostname */ | |
3266 | ||
3267 | ||
1f0275e3 MS |
3268 | if (SecPolicySearchCreate(CSSM_CERT_X_509v3, &CSSMOID_APPLE_TP_SSL, |
3269 | NULL, &policy_search)) | |
ef416fc2 | 3270 | { |
b86bc4cf | 3271 | cupsdLogMessage(CUPSD_LOG_ERROR, "Cannot create a policy search reference"); |
3272 | CFRelease(keychain); | |
3273 | return (NULL); | |
3274 | } | |
ef416fc2 | 3275 | |
1f0275e3 | 3276 | if (SecPolicySearchCopyNext(policy_search, &policy)) |
b86bc4cf | 3277 | { |
b9faaae1 | 3278 | cupsdLogMessage(CUPSD_LOG_ERROR, |
b86bc4cf | 3279 | "Cannot find a policy to use for searching"); |
3280 | CFRelease(keychain); | |
3281 | CFRelease(policy_search); | |
3282 | return (NULL); | |
3283 | } | |
ef416fc2 | 3284 | |
b86bc4cf | 3285 | memset(&ssl_options, 0, sizeof(ssl_options)); |
3286 | ssl_options.Version = CSSM_APPLE_TP_SSL_OPTS_VERSION; | |
3287 | ssl_options.ServerName = con->servername; | |
3288 | ssl_options.ServerNameLen = strlen(con->servername); | |
ef416fc2 | 3289 | |
b86bc4cf | 3290 | options.Data = (uint8 *)&ssl_options; |
3291 | options.Length = sizeof(ssl_options); | |
3292 | ||
1f0275e3 | 3293 | if (SecPolicySetValue(policy, &options)) |
b86bc4cf | 3294 | { |
b9faaae1 | 3295 | cupsdLogMessage(CUPSD_LOG_ERROR, |
b86bc4cf | 3296 | "Cannot set policy value to use for searching"); |
3297 | CFRelease(keychain); | |
3298 | CFRelease(policy_search); | |
3299 | return (NULL); | |
3300 | } | |
3301 | ||
3302 | err = SecIdentitySearchCreateWithPolicy(policy, NULL, CSSM_KEYUSE_SIGN, | |
3303 | keychain, FALSE, &search); | |
3304 | # else | |
3305 | /* | |
3306 | * Assume there is exactly one SecIdentity in the keychain... | |
3307 | */ | |
3308 | ||
3309 | err = SecIdentitySearchCreate(keychain, CSSM_KEYUSE_SIGN, &search); | |
3310 | # endif /* HAVE_SECIDENTITYSEARCHCREATEWITHPOLICY */ | |
3311 | ||
3312 | if (err) | |
3313 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
b9faaae1 MS |
3314 | "Cannot create keychain search reference: %s (%d)", |
3315 | cssmErrorString(err), (int)err); | |
b86bc4cf | 3316 | else |
3317 | { | |
3318 | if ((err = SecIdentitySearchCopyNext(search, &identity))) | |
3319 | { | |
3320 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
b9faaae1 MS |
3321 | "Cannot find signing key in keychain \"%s\": %s (%d)", |
3322 | ServerCertificate, cssmErrorString(err), (int)err); | |
b86bc4cf | 3323 | } |
3324 | else | |
3325 | { | |
3326 | if (CFGetTypeID(identity) != SecIdentityGetTypeID()) | |
3327 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
3328 | "SecIdentitySearchCopyNext CFTypeID failure!"); | |
ef416fc2 | 3329 | else |
3330 | { | |
b86bc4cf | 3331 | if ((certificates = CFArrayCreate(NULL, (const void **)&identity, |
3332 | 1, &kCFTypeArrayCallBacks)) == NULL) | |
3333 | cupsdLogMessage(CUPSD_LOG_ERROR, "Cannot create certificate array"); | |
ef416fc2 | 3334 | } |
3335 | ||
b86bc4cf | 3336 | CFRelease(identity); |
ef416fc2 | 3337 | } |
3338 | ||
b86bc4cf | 3339 | CFRelease(search); |
ef416fc2 | 3340 | } |
3341 | ||
b86bc4cf | 3342 | # if HAVE_SECIDENTITYSEARCHCREATEWITHPOLICY |
3343 | CFRelease(policy); | |
3344 | CFRelease(policy_search); | |
3345 | # endif /* HAVE_SECIDENTITYSEARCHCREATEWITHPOLICY */ | |
3346 | ||
3347 | return (certificates); | |
ef416fc2 | 3348 | } |
3349 | #endif /* HAVE_CDSASSL */ | |
3350 | ||
3351 | ||
3352 | /* | |
3353 | * 'get_file()' - Get a filename and state info. | |
3354 | */ | |
3355 | ||
3356 | static char * /* O - Real filename */ | |
3357 | get_file(cupsd_client_t *con, /* I - Client connection */ | |
3358 | struct stat *filestats, /* O - File information */ | |
3359 | char *filename, /* IO - Filename buffer */ | |
3360 | int len) /* I - Buffer length */ | |
3361 | { | |
3362 | int status; /* Status of filesystem calls */ | |
3363 | char *ptr; /* Pointer info filename */ | |
3364 | int plen; /* Remaining length after pointer */ | |
db1f069b | 3365 | char language[7]; /* Language subdirectory, if any */ |
ef416fc2 | 3366 | |
3367 | ||
3368 | /* | |
f7deaa1a | 3369 | * Figure out the real filename... |
ef416fc2 | 3370 | */ |
3371 | ||
db1f069b MS |
3372 | language[0] = '\0'; |
3373 | ||
ef416fc2 | 3374 | if (!strncmp(con->uri, "/ppd/", 5)) |
3375 | snprintf(filename, len, "%s%s", ServerRoot, con->uri); | |
f7deaa1a | 3376 | else if (!strncmp(con->uri, "/rss/", 5) && !strchr(con->uri + 5, '/')) |
3377 | snprintf(filename, len, "%s/rss/%s", CacheDir, con->uri + 5); | |
ef416fc2 | 3378 | else if (!strncmp(con->uri, "/admin/conf/", 12)) |
3379 | snprintf(filename, len, "%s%s", ServerRoot, con->uri + 11); | |
3380 | else if (!strncmp(con->uri, "/admin/log/", 11)) | |
3381 | { | |
2e4ff8af | 3382 | if (!strncmp(con->uri + 11, "access_log", 10) && AccessLog[0] == '/') |
ef416fc2 | 3383 | strlcpy(filename, AccessLog, len); |
2e4ff8af | 3384 | else if (!strncmp(con->uri + 11, "error_log", 9) && ErrorLog[0] == '/') |
ef416fc2 | 3385 | strlcpy(filename, ErrorLog, len); |
2e4ff8af | 3386 | else if (!strncmp(con->uri + 11, "page_log", 8) && PageLog[0] == '/') |
ef416fc2 | 3387 | strlcpy(filename, PageLog, len); |
3388 | else | |
3389 | return (NULL); | |
3390 | } | |
d6ae789d | 3391 | else if (con->language) |
db1f069b MS |
3392 | { |
3393 | snprintf(language, sizeof(language), "/%s", con->language->language); | |
3394 | snprintf(filename, len, "%s%s%s", DocumentRoot, language, con->uri); | |
3395 | } | |
ef416fc2 | 3396 | else |
3397 | snprintf(filename, len, "%s%s", DocumentRoot, con->uri); | |
3398 | ||
3399 | if ((ptr = strchr(filename, '?')) != NULL) | |
3400 | *ptr = '\0'; | |
3401 | ||
3402 | /* | |
3403 | * Grab the status for this language; if there isn't a language-specific file | |
3404 | * then fallback to the default one... | |
3405 | */ | |
3406 | ||
db1f069b | 3407 | if ((status = stat(filename, filestats)) != 0 && language[0] && |
d6ae789d | 3408 | strncmp(con->uri, "/ppd/", 5) && |
3409 | strncmp(con->uri, "/admin/conf/", 12) && | |
3410 | strncmp(con->uri, "/admin/log/", 11)) | |
ef416fc2 | 3411 | { |
3412 | /* | |
d6ae789d | 3413 | * Drop the country code... |
ef416fc2 | 3414 | */ |
3415 | ||
db1f069b MS |
3416 | language[3] = '\0'; |
3417 | snprintf(filename, len, "%s%s%s", DocumentRoot, language, con->uri); | |
d6ae789d | 3418 | |
3419 | if ((ptr = strchr(filename, '?')) != NULL) | |
3420 | *ptr = '\0'; | |
3421 | ||
3422 | if ((status = stat(filename, filestats)) != 0) | |
ef416fc2 | 3423 | { |
d6ae789d | 3424 | /* |
3425 | * Drop the language prefix and try the root directory... | |
3426 | */ | |
3427 | ||
db1f069b | 3428 | language[0] = '\0'; |
ef416fc2 | 3429 | snprintf(filename, len, "%s%s", DocumentRoot, con->uri); |
3430 | ||
3431 | if ((ptr = strchr(filename, '?')) != NULL) | |
3432 | *ptr = '\0'; | |
3433 | ||
3434 | status = stat(filename, filestats); | |
3435 | } | |
3436 | } | |
3437 | ||
3438 | /* | |
3439 | * If we're found a directory, get the index.html file instead... | |
3440 | */ | |
3441 | ||
3442 | if (!status && S_ISDIR(filestats->st_mode)) | |
3443 | { | |
db1f069b MS |
3444 | /* |
3445 | * Make sure the URI ends with a slash... | |
3446 | */ | |
ef416fc2 | 3447 | |
db1f069b MS |
3448 | if (con->uri[strlen(con->uri) - 1] != '/') |
3449 | strlcat(con->uri, "/", sizeof(con->uri)); | |
ef416fc2 | 3450 | |
db1f069b MS |
3451 | /* |
3452 | * Find the directory index file, trying every language... | |
3453 | */ | |
ef416fc2 | 3454 | |
db1f069b | 3455 | do |
ef416fc2 | 3456 | { |
db1f069b MS |
3457 | if (status && language[0]) |
3458 | { | |
3459 | /* | |
3460 | * Try a different language subset... | |
3461 | */ | |
3462 | ||
3463 | if (language[3]) | |
3464 | language[0] = '\0'; /* Strip country code */ | |
3465 | else | |
3466 | language[0] = '\0'; /* Strip language */ | |
3467 | } | |
3468 | ||
3469 | /* | |
3470 | * Look for the index file... | |
3471 | */ | |
3472 | ||
3473 | snprintf(filename, len, "%s%s%s", DocumentRoot, language, con->uri); | |
3474 | ||
3475 | if ((ptr = strchr(filename, '?')) != NULL) | |
3476 | *ptr = '\0'; | |
3477 | ||
3478 | ptr = filename + strlen(filename); | |
3479 | plen = len - (ptr - filename); | |
3480 | ||
3481 | strlcpy(ptr, "index.html", plen); | |
ef416fc2 | 3482 | status = stat(filename, filestats); |
db1f069b MS |
3483 | |
3484 | #ifdef HAVE_JAVA | |
3485 | if (status) | |
3486 | { | |
3487 | strlcpy(ptr, "index.class", plen); | |
3488 | status = stat(filename, filestats); | |
3489 | } | |
ef416fc2 | 3490 | #endif /* HAVE_JAVA */ |
3491 | ||
3492 | #ifdef HAVE_PERL | |
db1f069b MS |
3493 | if (status) |
3494 | { | |
3495 | strlcpy(ptr, "index.pl", plen); | |
3496 | status = stat(filename, filestats); | |
3497 | } | |
ef416fc2 | 3498 | #endif /* HAVE_PERL */ |
3499 | ||
3500 | #ifdef HAVE_PHP | |
db1f069b MS |
3501 | if (status) |
3502 | { | |
3503 | strlcpy(ptr, "index.php", plen); | |
3504 | status = stat(filename, filestats); | |
3505 | } | |
ef416fc2 | 3506 | #endif /* HAVE_PHP */ |
3507 | ||
3508 | #ifdef HAVE_PYTHON | |
db1f069b MS |
3509 | if (status) |
3510 | { | |
3511 | strlcpy(ptr, "index.pyc", plen); | |
3512 | status = stat(filename, filestats); | |
3513 | } | |
ef416fc2 | 3514 | |
db1f069b MS |
3515 | if (status) |
3516 | { | |
3517 | strlcpy(ptr, "index.py", plen); | |
3518 | status = stat(filename, filestats); | |
3519 | } | |
ef416fc2 | 3520 | #endif /* HAVE_PYTHON */ |
db1f069b MS |
3521 | |
3522 | } | |
3523 | while (status && language[0]); | |
ef416fc2 | 3524 | } |
3525 | ||
b9faaae1 MS |
3526 | cupsdLogMessage(CUPSD_LOG_DEBUG2, |
3527 | "get_file(con=%p(%d), filestats=%p, filename=%p, len=%d) = " | |
3528 | "%s", con, con->http.fd, filestats, filename, len, | |
3529 | status ? "(null)" : filename); | |
ef416fc2 | 3530 | |
3531 | if (!status) | |
3532 | con->http.data_remaining = (int)filestats->st_size; | |
3533 | ||
3534 | if (status) | |
3535 | return (NULL); | |
3536 | else | |
3537 | return (filename); | |
3538 | } | |
3539 | ||
3540 | ||
3541 | /* | |
3542 | * 'install_conf_file()' - Install a configuration file. | |
3543 | */ | |
3544 | ||
3545 | static http_status_t /* O - Status */ | |
3546 | install_conf_file(cupsd_client_t *con) /* I - Connection */ | |
3547 | { | |
3548 | cups_file_t *in, /* Input file */ | |
3549 | *out; /* Output file */ | |
3550 | char buffer[1024]; /* Copy buffer */ | |
3551 | int bytes; /* Number of bytes */ | |
3552 | char conffile[1024], /* Configuration filename */ | |
3553 | newfile[1024], /* New config filename */ | |
3554 | oldfile[1024]; /* Old config filename */ | |
3555 | struct stat confinfo; /* Config file info */ | |
3556 | ||
3557 | ||
3558 | /* | |
3559 | * First construct the filenames... | |
3560 | */ | |
3561 | ||
3562 | snprintf(conffile, sizeof(conffile), "%s%s", ServerRoot, con->uri + 11); | |
3563 | snprintf(newfile, sizeof(newfile), "%s%s.N", ServerRoot, con->uri + 11); | |
3564 | snprintf(oldfile, sizeof(oldfile), "%s%s.O", ServerRoot, con->uri + 11); | |
3565 | ||
3566 | cupsdLogMessage(CUPSD_LOG_INFO, "Installing config file \"%s\"...", conffile); | |
3567 | ||
3568 | /* | |
3569 | * Get the owner, group, and permissions of the configuration file. | |
3570 | * If it doesn't exist, assign it to the User and Group in the | |
3571 | * cupsd.conf file with mode 0640 permissions. | |
3572 | */ | |
3573 | ||
3574 | if (stat(conffile, &confinfo)) | |
3575 | { | |
3576 | confinfo.st_uid = User; | |
3577 | confinfo.st_gid = Group; | |
3578 | confinfo.st_mode = ConfigFilePerm; | |
3579 | } | |
3580 | ||
3581 | /* | |
3582 | * Open the request file and new config file... | |
3583 | */ | |
3584 | ||
3585 | if ((in = cupsFileOpen(con->filename, "rb")) == NULL) | |
3586 | { | |
3587 | cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to open request file \"%s\" - %s", | |
3588 | con->filename, strerror(errno)); | |
3589 | return (HTTP_SERVER_ERROR); | |
3590 | } | |
3591 | ||
3592 | if ((out = cupsFileOpen(newfile, "wb")) == NULL) | |
3593 | { | |
3594 | cupsFileClose(in); | |
3595 | cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to open config file \"%s\" - %s", | |
3596 | newfile, strerror(errno)); | |
3597 | return (HTTP_SERVER_ERROR); | |
3598 | } | |
3599 | ||
3600 | fchmod(cupsFileNumber(out), confinfo.st_mode); | |
3601 | fchown(cupsFileNumber(out), confinfo.st_uid, confinfo.st_gid); | |
3602 | ||
3603 | /* | |
3604 | * Copy from the request to the new config file... | |
3605 | */ | |
3606 | ||
3607 | while ((bytes = cupsFileRead(in, buffer, sizeof(buffer))) > 0) | |
3608 | if (cupsFileWrite(out, buffer, bytes) < bytes) | |
3609 | { | |
3610 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
3611 | "Unable to copy to config file \"%s\" - %s", | |
3612 | newfile, strerror(errno)); | |
3613 | ||
3614 | cupsFileClose(in); | |
3615 | cupsFileClose(out); | |
3616 | unlink(newfile); | |
3617 | ||
3618 | return (HTTP_SERVER_ERROR); | |
3619 | } | |
3620 | ||
3621 | /* | |
3622 | * Close the files... | |
3623 | */ | |
3624 | ||
3625 | cupsFileClose(in); | |
3626 | if (cupsFileClose(out)) | |
3627 | { | |
3628 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
3629 | "Error file closing config file \"%s\" - %s", | |
3630 | newfile, strerror(errno)); | |
3631 | ||
3632 | unlink(newfile); | |
3633 | ||
3634 | return (HTTP_SERVER_ERROR); | |
3635 | } | |
3636 | ||
3637 | /* | |
3638 | * Remove the request file... | |
3639 | */ | |
3640 | ||
3641 | unlink(con->filename); | |
3642 | cupsdClearString(&con->filename); | |
3643 | ||
3644 | /* | |
3645 | * Unlink the old backup, rename the current config file to the backup | |
3646 | * filename, and rename the new config file to the config file name... | |
3647 | */ | |
3648 | ||
3649 | if (unlink(oldfile)) | |
3650 | if (errno != ENOENT) | |
3651 | { | |
3652 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
3653 | "Unable to remove backup config file \"%s\" - %s", | |
3654 | oldfile, strerror(errno)); | |
3655 | ||
3656 | unlink(newfile); | |
3657 | ||
3658 | return (HTTP_SERVER_ERROR); | |
3659 | } | |
3660 | ||
3661 | if (rename(conffile, oldfile)) | |
3662 | if (errno != ENOENT) | |
3663 | { | |
3664 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
3665 | "Unable to rename old config file \"%s\" - %s", | |
3666 | conffile, strerror(errno)); | |
3667 | ||
3668 | unlink(newfile); | |
3669 | ||
3670 | return (HTTP_SERVER_ERROR); | |
3671 | } | |
3672 | ||
3673 | if (rename(newfile, conffile)) | |
3674 | { | |
3675 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
3676 | "Unable to rename new config file \"%s\" - %s", | |
3677 | newfile, strerror(errno)); | |
3678 | ||
3679 | rename(oldfile, conffile); | |
3680 | unlink(newfile); | |
3681 | ||
3682 | return (HTTP_SERVER_ERROR); | |
3683 | } | |
3684 | ||
3685 | /* | |
3686 | * If the cupsd.conf file was updated, set the NeedReload flag... | |
3687 | */ | |
3688 | ||
3689 | if (!strcmp(con->uri, "/admin/conf/cupsd.conf")) | |
3690 | NeedReload = RELOAD_CUPSD; | |
3691 | else | |
3692 | NeedReload = RELOAD_ALL; | |
3693 | ||
3694 | ReloadTime = time(NULL); | |
3695 | ||
3696 | /* | |
3697 | * Return that the file was created successfully... | |
3698 | */ | |
3699 | ||
3700 | return (HTTP_CREATED); | |
3701 | } | |
3702 | ||
3703 | ||
e1d6a774 | 3704 | /* |
3705 | * 'is_cgi()' - Is the resource a CGI script/program? | |
3706 | */ | |
3707 | ||
3708 | static int /* O - 1 = CGI, 0 = file */ | |
3709 | is_cgi(cupsd_client_t *con, /* I - Client connection */ | |
3710 | const char *filename, /* I - Real filename */ | |
3711 | struct stat *filestats, /* I - File information */ | |
3712 | mime_type_t *type) /* I - MIME type */ | |
3713 | { | |
3714 | const char *options; /* Options on URL */ | |
3715 | ||
3716 | ||
e1d6a774 | 3717 | /* |
3718 | * Get the options, if any... | |
3719 | */ | |
3720 | ||
3721 | if ((options = strchr(con->uri, '?')) != NULL) | |
a0f6818e MS |
3722 | { |
3723 | options ++; | |
3724 | cupsdSetStringf(&(con->query_string), "QUERY_STRING=%s", options); | |
3725 | } | |
b86bc4cf | 3726 | |
e1d6a774 | 3727 | /* |
3728 | * Check for known types... | |
3729 | */ | |
3730 | ||
3731 | if (!type || strcasecmp(type->super, "application")) | |
3732 | { | |
b9faaae1 MS |
3733 | cupsdLogMessage(CUPSD_LOG_DEBUG2, |
3734 | "is_cgi(con=%p(%d), filename=\"%s\", filestats=%p, " | |
3735 | "type=%s/%s) = 0", con, con->http.fd, filename, filestats, | |
3736 | type ? type->super : "unknown", | |
3737 | type ? type->type : "unknown"); | |
e1d6a774 | 3738 | return (0); |
3739 | } | |
3740 | ||
3741 | if (!strcasecmp(type->type, "x-httpd-cgi") && | |
3742 | (filestats->st_mode & 0111)) | |
3743 | { | |
3744 | /* | |
3745 | * "application/x-httpd-cgi" is a CGI script. | |
3746 | */ | |
3747 | ||
3748 | cupsdSetString(&con->command, filename); | |
3749 | ||
a0f6818e MS |
3750 | if (options) |
3751 | cupsdSetStringf(&con->options, " %s", options); | |
e1d6a774 | 3752 | |
3753 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
b9faaae1 MS |
3754 | "is_cgi(con=%p(%d), filename=\"%s\", filestats=%p, " |
3755 | "type=%s/%s) = 1", con, con->http.fd, filename, filestats, | |
3756 | type ? type->super : "unknown", | |
3757 | type ? type->type : "unknown"); | |
e1d6a774 | 3758 | return (1); |
3759 | } | |
3760 | #ifdef HAVE_JAVA | |
3761 | else if (!strcasecmp(type->type, "x-httpd-java")) | |
3762 | { | |
3763 | /* | |
3764 | * "application/x-httpd-java" is a Java servlet. | |
3765 | */ | |
3766 | ||
3767 | cupsdSetString(&con->command, CUPS_JAVA); | |
3768 | ||
3769 | if (options) | |
b94498cf | 3770 | cupsdSetStringf(&con->options, " %s %s", filename, options); |
e1d6a774 | 3771 | else |
b94498cf | 3772 | cupsdSetStringf(&con->options, " %s", filename); |
e1d6a774 | 3773 | |
3774 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
b9faaae1 MS |
3775 | "is_cgi(con=%p(%d), filename=\"%s\", filestats=%p, " |
3776 | "type=%s/%s) = 1", con, con->http.fd, filename, filestats, | |
3777 | type ? type->super : "unknown", | |
3778 | type ? type->type : "unknown"); | |
e1d6a774 | 3779 | return (1); |
3780 | } | |
3781 | #endif /* HAVE_JAVA */ | |
3782 | #ifdef HAVE_PERL | |
3783 | else if (!strcasecmp(type->type, "x-httpd-perl")) | |
3784 | { | |
3785 | /* | |
3786 | * "application/x-httpd-perl" is a Perl page. | |
3787 | */ | |
3788 | ||
3789 | cupsdSetString(&con->command, CUPS_PERL); | |
3790 | ||
3791 | if (options) | |
b94498cf | 3792 | cupsdSetStringf(&con->options, " %s %s", filename, options); |
e1d6a774 | 3793 | else |
b94498cf | 3794 | cupsdSetStringf(&con->options, " %s", filename); |
e1d6a774 | 3795 | |
3796 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
b9faaae1 MS |
3797 | "is_cgi(con=%p(%d), filename=\"%s\", filestats=%p, " |
3798 | "type=%s/%s) = 1", con, con->http.fd, filename, filestats, | |
3799 | type ? type->super : "unknown", | |
3800 | type ? type->type : "unknown"); | |
e1d6a774 | 3801 | return (1); |
3802 | } | |
3803 | #endif /* HAVE_PERL */ | |
3804 | #ifdef HAVE_PHP | |
3805 | else if (!strcasecmp(type->type, "x-httpd-php")) | |
3806 | { | |
3807 | /* | |
3808 | * "application/x-httpd-php" is a PHP page. | |
3809 | */ | |
3810 | ||
3811 | cupsdSetString(&con->command, CUPS_PHP); | |
3812 | ||
3813 | if (options) | |
b94498cf | 3814 | cupsdSetStringf(&con->options, " %s %s", filename, options); |
e1d6a774 | 3815 | else |
b94498cf | 3816 | cupsdSetStringf(&con->options, " %s", filename); |
e1d6a774 | 3817 | |
3818 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
b9faaae1 MS |
3819 | "is_cgi(con=%p(%d), filename=\"%s\", filestats=%p, " |
3820 | "type=%s/%s) = 1", con, con->http.fd, filename, filestats, | |
3821 | type ? type->super : "unknown", | |
3822 | type ? type->type : "unknown"); | |
e1d6a774 | 3823 | return (1); |
3824 | } | |
3825 | #endif /* HAVE_PHP */ | |
3826 | #ifdef HAVE_PYTHON | |
3827 | else if (!strcasecmp(type->type, "x-httpd-python")) | |
3828 | { | |
3829 | /* | |
3830 | * "application/x-httpd-python" is a Python page. | |
3831 | */ | |
3832 | ||
3833 | cupsdSetString(&con->command, CUPS_PYTHON); | |
3834 | ||
3835 | if (options) | |
b94498cf | 3836 | cupsdSetStringf(&con->options, " %s %s", filename, options); |
e1d6a774 | 3837 | else |
b94498cf | 3838 | cupsdSetStringf(&con->options, " %s", filename); |
e1d6a774 | 3839 | |
3840 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
b9faaae1 MS |
3841 | "is_cgi(con=%p(%d), filename=\"%s\", filestats=%p, " |
3842 | "type=%s/%s) = 1", con, con->http.fd, filename, filestats, | |
3843 | type ? type->super : "unknown", | |
3844 | type ? type->type : "unknown"); | |
e1d6a774 | 3845 | return (1); |
3846 | } | |
3847 | #endif /* HAVE_PYTHON */ | |
3848 | ||
b9faaae1 MS |
3849 | cupsdLogMessage(CUPSD_LOG_DEBUG2, |
3850 | "is_cgi(con=%p(%d), filename=\"%s\", filestats=%p, " | |
3851 | "type=%s/%s) = 0", con, con->http.fd, filename, filestats, | |
3852 | type ? type->super : "unknown", | |
3853 | type ? type->type : "unknown"); | |
e1d6a774 | 3854 | return (0); |
3855 | } | |
3856 | ||
3857 | ||
ef416fc2 | 3858 | /* |
3859 | * 'is_path_absolute()' - Is a path absolute and free of relative elements (i.e. ".."). | |
3860 | */ | |
3861 | ||
3862 | static int /* O - 0 if relative, 1 if absolute */ | |
3863 | is_path_absolute(const char *path) /* I - Input path */ | |
3864 | { | |
3865 | /* | |
3866 | * Check for a leading slash... | |
3867 | */ | |
3868 | ||
3869 | if (path[0] != '/') | |
3870 | return (0); | |
3871 | ||
3872 | /* | |
3873 | * Check for "/.." in the path... | |
3874 | */ | |
3875 | ||
3876 | while ((path = strstr(path, "/..")) != NULL) | |
3877 | { | |
3878 | if (!path[3] || path[3] == '/') | |
3879 | return (0); | |
3880 | ||
3881 | path ++; | |
3882 | } | |
3883 | ||
3884 | /* | |
3885 | * If we haven't found any relative paths, return 1 indicating an | |
3886 | * absolute path... | |
3887 | */ | |
3888 | ||
3889 | return (1); | |
3890 | } | |
3891 | ||
3892 | ||
8ca02f3c | 3893 | #ifdef HAVE_SSL |
4744bd90 | 3894 | /* |
3895 | * 'make_certificate()' - Make a self-signed SSL/TLS certificate. | |
3896 | */ | |
3897 | ||
8ca02f3c | 3898 | static int /* O - 1 on success, 0 on failure */ |
b86bc4cf | 3899 | make_certificate(cupsd_client_t *con) /* I - Client connection */ |
4744bd90 | 3900 | { |
8ca02f3c | 3901 | #if defined(HAVE_LIBSSL) && defined(HAVE_WAITPID) |
d09495fa | 3902 | int pid, /* Process ID of command */ |
3903 | status; /* Status of command */ | |
3904 | char command[1024], /* Command */ | |
f7deaa1a | 3905 | *argv[12], /* Command-line arguments */ |
d09495fa | 3906 | *envp[MAX_ENV + 1], /* Environment variables */ |
3907 | home[1024], /* HOME environment variable */ | |
3908 | infofile[1024], /* Type-in information for cert */ | |
3909 | seedfile[1024]; /* Random number seed file */ | |
3910 | int envc, /* Number of environment variables */ | |
3911 | bytes; /* Bytes written */ | |
3912 | cups_file_t *fp; /* Seed/info file */ | |
3913 | int infofd; /* Info file descriptor */ | |
8ca02f3c | 3914 | |
3915 | ||
3916 | /* | |
d09495fa | 3917 | * Run the "openssl" command to seed the random number generator and |
3918 | * generate a self-signed certificate that is good for 10 years: | |
3919 | * | |
3920 | * openssl rand -rand seedfile 1 | |
8ca02f3c | 3921 | * |
3922 | * openssl req -new -x509 -keyout ServerKey \ | |
3923 | * -out ServerCertificate -days 3650 -nodes | |
d09495fa | 3924 | * |
3925 | * The seeding step is crucial in ensuring that the openssl command | |
3926 | * does not block on systems without sufficient entropy... | |
8ca02f3c | 3927 | */ |
3928 | ||
3929 | if (!cupsFileFind("openssl", getenv("PATH"), 1, command, sizeof(command))) | |
3930 | { | |
3931 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
3932 | "No SSL certificate and openssl command not found!"); | |
3933 | return (0); | |
3934 | } | |
3935 | ||
d09495fa | 3936 | if (access("/dev/urandom", 0)) |
3937 | { | |
3938 | /* | |
3939 | * If the system doesn't provide /dev/urandom, then any random source | |
3940 | * will probably be blocking-style, so generate some random data to | |
3941 | * use as a seed for the certificate. Note that we have already | |
3942 | * seeded the random number generator in cupsdInitCerts()... | |
3943 | */ | |
3944 | ||
3945 | cupsdLogMessage(CUPSD_LOG_INFO, | |
3946 | "Seeding the random number generator..."); | |
3947 | ||
3948 | snprintf(home, sizeof(home), "HOME=%s", TempDir); | |
3949 | ||
3950 | /* | |
3951 | * Write the seed file... | |
3952 | */ | |
3953 | ||
3954 | if ((fp = cupsTempFile2(seedfile, sizeof(seedfile))) == NULL) | |
3955 | { | |
3956 | cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to create seed file %s - %s", | |
3957 | seedfile, strerror(errno)); | |
3958 | return (0); | |
3959 | } | |
3960 | ||
3961 | for (bytes = 0; bytes < 262144; bytes ++) | |
3962 | cupsFilePutChar(fp, random()); | |
3963 | ||
3964 | cupsFileClose(fp); | |
3965 | ||
3966 | /* | |
3967 | * Run the openssl command to seed its random number generator... | |
3968 | */ | |
3969 | ||
3970 | argv[0] = "openssl"; | |
3971 | argv[1] = "rand"; | |
3972 | argv[2] = "-rand"; | |
3973 | argv[3] = seedfile; | |
3974 | argv[4] = "1"; | |
3975 | argv[5] = NULL; | |
3976 | ||
3977 | envc = cupsdLoadEnv(envp, MAX_ENV); | |
3978 | envp[envc++] = home; | |
3979 | envp[envc] = NULL; | |
3980 | ||
b9faaae1 | 3981 | if (!cupsdStartProcess(command, argv, envp, -1, -1, -1, -1, -1, 1, NULL, 0, |
a4924f6c | 3982 | &pid)) |
d09495fa | 3983 | { |
3984 | unlink(seedfile); | |
3985 | return (0); | |
3986 | } | |
3987 | ||
3988 | while (waitpid(pid, &status, 0) < 0) | |
3989 | if (errno != EINTR) | |
3990 | { | |
3991 | status = 1; | |
3992 | break; | |
3993 | } | |
3994 | ||
b9faaae1 | 3995 | cupsdFinishProcess(pid, command, sizeof(command), NULL); |
d09495fa | 3996 | |
3997 | /* | |
3998 | * Remove the seed file, as it is no longer needed... | |
3999 | */ | |
4000 | ||
4001 | unlink(seedfile); | |
4002 | ||
4003 | if (status) | |
4004 | { | |
4005 | if (WIFEXITED(status)) | |
4006 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
4007 | "Unable to seed random number generator - " | |
4008 | "the openssl command stopped with status %d!", | |
4009 | WEXITSTATUS(status)); | |
4010 | else | |
4011 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
4012 | "Unable to seed random number generator - " | |
4013 | "the openssl command crashed on signal %d!", | |
4014 | WTERMSIG(status)); | |
4015 | ||
4016 | return (0); | |
4017 | } | |
4018 | } | |
4019 | ||
4020 | /* | |
4021 | * Create a file with the certificate information fields... | |
4022 | * | |
4023 | * Note: This assumes that the default questions are asked by the openssl | |
4024 | * command... | |
4025 | */ | |
4026 | ||
4027 | if ((fp = cupsTempFile2(infofile, sizeof(infofile))) == NULL) | |
4028 | { | |
4029 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
4030 | "Unable to create certificate information file %s - %s", | |
4031 | infofile, strerror(errno)); | |
4032 | return (0); | |
4033 | } | |
4034 | ||
4035 | cupsFilePrintf(fp, ".\n.\n.\n%s\n.\n%s\n%s\n", | |
4036 | ServerName, ServerName, ServerAdmin); | |
4037 | cupsFileClose(fp); | |
4038 | ||
8ca02f3c | 4039 | cupsdLogMessage(CUPSD_LOG_INFO, |
4040 | "Generating SSL server key and certificate..."); | |
4041 | ||
4042 | argv[0] = "openssl"; | |
4043 | argv[1] = "req"; | |
4044 | argv[2] = "-new"; | |
4045 | argv[3] = "-x509"; | |
4046 | argv[4] = "-keyout"; | |
4047 | argv[5] = ServerKey; | |
4048 | argv[6] = "-out"; | |
4049 | argv[7] = ServerCertificate; | |
4050 | argv[8] = "-days"; | |
4051 | argv[9] = "3650"; | |
4052 | argv[10] = "-nodes"; | |
4053 | argv[11] = NULL; | |
4054 | ||
4055 | cupsdLoadEnv(envp, MAX_ENV); | |
4056 | ||
d09495fa | 4057 | infofd = open(infofile, O_RDONLY); |
4058 | ||
a4924f6c | 4059 | if (!cupsdStartProcess(command, argv, envp, infofd, -1, -1, -1, -1, 1, NULL, |
b9faaae1 | 4060 | 0, &pid)) |
d09495fa | 4061 | { |
4062 | close(infofd); | |
4063 | unlink(infofile); | |
8ca02f3c | 4064 | return (0); |
d09495fa | 4065 | } |
4066 | ||
4067 | close(infofd); | |
4068 | unlink(infofile); | |
8ca02f3c | 4069 | |
4070 | while (waitpid(pid, &status, 0) < 0) | |
4071 | if (errno != EINTR) | |
4072 | { | |
4073 | status = 1; | |
4074 | break; | |
4075 | } | |
4076 | ||
b9faaae1 | 4077 | cupsdFinishProcess(pid, command, sizeof(command), NULL); |
8ca02f3c | 4078 | |
4079 | if (status) | |
4080 | { | |
4081 | if (WIFEXITED(status)) | |
4082 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
4083 | "Unable to create SSL server key and certificate - " | |
4084 | "the openssl command stopped with status %d!", | |
4085 | WEXITSTATUS(status)); | |
4086 | else | |
4087 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
4088 | "Unable to create SSL server key and certificate - " | |
4089 | "the openssl command crashed on signal %d!", | |
4090 | WTERMSIG(status)); | |
4091 | } | |
4092 | else | |
4093 | { | |
4094 | cupsdLogMessage(CUPSD_LOG_INFO, "Created SSL server key file \"%s\"...", | |
4095 | ServerKey); | |
4096 | cupsdLogMessage(CUPSD_LOG_INFO, | |
4097 | "Created SSL server certificate file \"%s\"...", | |
4098 | ServerCertificate); | |
4099 | } | |
4100 | ||
4101 | return (!status); | |
4102 | ||
4103 | #elif defined(HAVE_GNUTLS) | |
4744bd90 | 4104 | gnutls_x509_crt crt; /* Self-signed certificate */ |
4105 | gnutls_x509_privkey key; /* Encryption key */ | |
4106 | cups_lang_t *language; /* Default language info */ | |
4107 | cups_file_t *fp; /* Key/cert file */ | |
4108 | unsigned char buffer[8192]; /* Buffer for x509 data */ | |
4109 | size_t bytes; /* Number of bytes of data */ | |
4110 | unsigned char serial[4]; /* Serial number buffer */ | |
4111 | time_t curtime; /* Current time */ | |
4112 | int result; /* Result of GNU TLS calls */ | |
4113 | ||
4114 | ||
4115 | /* | |
4116 | * Create the encryption key... | |
4117 | */ | |
4118 | ||
8ca02f3c | 4119 | cupsdLogMessage(CUPSD_LOG_INFO, "Generating SSL server key..."); |
4744bd90 | 4120 | |
4121 | gnutls_x509_privkey_init(&key); | |
4122 | gnutls_x509_privkey_generate(key, GNUTLS_PK_RSA, 2048, 0); | |
4123 | ||
4124 | /* | |
4125 | * Save it... | |
4126 | */ | |
4127 | ||
4128 | bytes = sizeof(buffer); | |
4129 | ||
4130 | if ((result = gnutls_x509_privkey_export(key, GNUTLS_X509_FMT_PEM, | |
4131 | buffer, &bytes)) < 0) | |
4132 | { | |
8ca02f3c | 4133 | cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to export SSL server key - %s", |
4744bd90 | 4134 | gnutls_strerror(result)); |
4135 | gnutls_x509_privkey_deinit(key); | |
8ca02f3c | 4136 | return (0); |
4744bd90 | 4137 | } |
4138 | else if ((fp = cupsFileOpen(ServerKey, "w")) != NULL) | |
4139 | { | |
4140 | cupsFileWrite(fp, (char *)buffer, bytes); | |
4141 | cupsFileClose(fp); | |
4142 | ||
8ca02f3c | 4143 | cupsdLogMessage(CUPSD_LOG_INFO, "Created SSL server key file \"%s\"...", |
4744bd90 | 4144 | ServerKey); |
4145 | } | |
4146 | else | |
4147 | { | |
4148 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
8ca02f3c | 4149 | "Unable to create SSL server key file \"%s\" - %s", |
4744bd90 | 4150 | ServerKey, strerror(errno)); |
4151 | gnutls_x509_privkey_deinit(key); | |
8ca02f3c | 4152 | return (0); |
4744bd90 | 4153 | } |
4154 | ||
4155 | /* | |
4156 | * Create the self-signed certificate... | |
4157 | */ | |
4158 | ||
8ca02f3c | 4159 | cupsdLogMessage(CUPSD_LOG_INFO, "Generating self-signed SSL certificate..."); |
4744bd90 | 4160 | |
4161 | language = cupsLangDefault(); | |
4162 | curtime = time(NULL); | |
4163 | serial[0] = curtime >> 24; | |
4164 | serial[1] = curtime >> 16; | |
4165 | serial[2] = curtime >> 8; | |
4166 | serial[3] = curtime; | |
4167 | ||
4168 | gnutls_x509_crt_init(&crt); | |
4169 | if (strlen(language->language) == 5) | |
4170 | gnutls_x509_crt_set_dn_by_oid(crt, GNUTLS_OID_X520_COUNTRY_NAME, 0, | |
4171 | language->language + 3, 2); | |
4172 | else | |
4173 | gnutls_x509_crt_set_dn_by_oid(crt, GNUTLS_OID_X520_COUNTRY_NAME, 0, | |
4174 | "US", 2); | |
4175 | gnutls_x509_crt_set_dn_by_oid(crt, GNUTLS_OID_X520_COMMON_NAME, 0, | |
4176 | ServerName, strlen(ServerName)); | |
4177 | gnutls_x509_crt_set_dn_by_oid(crt, GNUTLS_OID_X520_ORGANIZATION_NAME, 0, | |
4178 | ServerName, strlen(ServerName)); | |
4179 | gnutls_x509_crt_set_dn_by_oid(crt, GNUTLS_OID_X520_ORGANIZATIONAL_UNIT_NAME, | |
4180 | 0, "Unknown", 7); | |
4181 | gnutls_x509_crt_set_dn_by_oid(crt, GNUTLS_OID_X520_STATE_OR_PROVINCE_NAME, 0, | |
4182 | "Unknown", 7); | |
4183 | gnutls_x509_crt_set_dn_by_oid(crt, GNUTLS_OID_X520_LOCALITY_NAME, 0, | |
4184 | "Unknown", 7); | |
4185 | gnutls_x509_crt_set_dn_by_oid(crt, GNUTLS_OID_PKCS9_EMAIL, 0, | |
4186 | ServerAdmin, strlen(ServerAdmin)); | |
4187 | gnutls_x509_crt_set_key(crt, key); | |
4188 | gnutls_x509_crt_set_serial(crt, serial, sizeof(serial)); | |
4189 | gnutls_x509_crt_set_activation_time(crt, curtime); | |
4190 | gnutls_x509_crt_set_expiration_time(crt, curtime + 10 * 365 * 86400); | |
4191 | gnutls_x509_crt_set_ca_status(crt, 0); | |
4192 | gnutls_x509_crt_set_subject_alternative_name(crt, GNUTLS_SAN_DNSNAME, | |
4193 | ServerName); | |
4194 | gnutls_x509_crt_set_key_purpose_oid(crt, GNUTLS_KP_TLS_WWW_SERVER, 0); | |
4195 | gnutls_x509_crt_set_key_usage(crt, GNUTLS_KEY_KEY_ENCIPHERMENT); | |
4196 | gnutls_x509_crt_set_version(crt, 3); | |
4197 | ||
4198 | bytes = sizeof(buffer); | |
4199 | if (gnutls_x509_crt_get_key_id(crt, 0, buffer, &bytes) >= 0) | |
4200 | gnutls_x509_crt_set_subject_key_id(crt, buffer, bytes); | |
4201 | ||
4202 | gnutls_x509_crt_sign(crt, crt, key); | |
4203 | ||
4204 | /* | |
4205 | * Save it... | |
4206 | */ | |
4207 | ||
4208 | bytes = sizeof(buffer); | |
4209 | if ((result = gnutls_x509_crt_export(crt, GNUTLS_X509_FMT_PEM, | |
4210 | buffer, &bytes)) < 0) | |
4211 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
8ca02f3c | 4212 | "Unable to export SSL server certificate - %s", |
4744bd90 | 4213 | gnutls_strerror(result)); |
4214 | else if ((fp = cupsFileOpen(ServerCertificate, "w")) != NULL) | |
4215 | { | |
4216 | cupsFileWrite(fp, (char *)buffer, bytes); | |
4217 | cupsFileClose(fp); | |
4218 | ||
4219 | cupsdLogMessage(CUPSD_LOG_INFO, | |
8ca02f3c | 4220 | "Created SSL server certificate file \"%s\"...", |
4744bd90 | 4221 | ServerCertificate); |
4222 | } | |
4223 | else | |
4224 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
8ca02f3c | 4225 | "Unable to create SSL server certificate file \"%s\" - %s", |
4744bd90 | 4226 | ServerCertificate, strerror(errno)); |
4227 | ||
4228 | /* | |
4229 | * Cleanup... | |
4230 | */ | |
4231 | ||
4232 | gnutls_x509_crt_deinit(crt); | |
4233 | gnutls_x509_privkey_deinit(key); | |
8ca02f3c | 4234 | |
4235 | return (1); | |
4236 | ||
4237 | #elif defined(HAVE_CDSASSL) && defined(HAVE_WAITPID) | |
b86bc4cf | 4238 | int pid, /* Process ID of command */ |
4239 | status; /* Status of command */ | |
4240 | char command[1024], /* Command */ | |
4241 | *argv[4], /* Command-line arguments */ | |
4242 | *envp[MAX_ENV + 1], /* Environment variables */ | |
4243 | keychain[1024], /* Keychain argument */ | |
4244 | infofile[1024]; /* Type-in information for cert */ | |
4245 | cups_file_t *fp; /* Seed/info file */ | |
4246 | int infofd; /* Info file descriptor */ | |
8ca02f3c | 4247 | |
4248 | ||
4249 | /* | |
b86bc4cf | 4250 | * Run the "certtool" command to generate a self-signed certificate... |
8ca02f3c | 4251 | */ |
4252 | ||
4253 | if (!cupsFileFind("certtool", getenv("PATH"), 1, command, sizeof(command))) | |
4254 | { | |
4255 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
4256 | "No SSL certificate and certtool command not found!"); | |
4257 | return (0); | |
4258 | } | |
4259 | ||
b86bc4cf | 4260 | /* |
4261 | * Create a file with the certificate information fields... | |
4262 | * | |
4263 | * Note: This assumes that the default questions are asked by the certtool | |
4264 | * command... | |
4265 | */ | |
4266 | ||
4267 | if ((fp = cupsTempFile2(infofile, sizeof(infofile))) == NULL) | |
4268 | { | |
4269 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
4270 | "Unable to create certificate information file %s - %s", | |
4271 | infofile, strerror(errno)); | |
4272 | return (0); | |
4273 | } | |
4274 | ||
4275 | cupsFilePrintf(fp, "%s\nr\n\ny\nb\ns\ny\n%s\n\n\n\n\n%s\ny\n", | |
4276 | con->servername, con->servername, ServerAdmin); | |
4277 | cupsFileClose(fp); | |
4278 | ||
8ca02f3c | 4279 | cupsdLogMessage(CUPSD_LOG_INFO, |
4280 | "Generating SSL server key and certificate..."); | |
4281 | ||
4282 | snprintf(keychain, sizeof(keychain), "k=%s", ServerCertificate); | |
4283 | ||
4284 | argv[0] = "certtool"; | |
4285 | argv[1] = "c"; | |
b86bc4cf | 4286 | argv[2] = keychain; |
4287 | argv[3] = NULL; | |
8ca02f3c | 4288 | |
4289 | cupsdLoadEnv(envp, MAX_ENV); | |
4290 | ||
b86bc4cf | 4291 | infofd = open(infofile, O_RDONLY); |
4292 | ||
a4924f6c | 4293 | if (!cupsdStartProcess(command, argv, envp, infofd, -1, -1, -1, -1, 1, NULL, |
b9faaae1 | 4294 | 0, &pid)) |
b86bc4cf | 4295 | { |
4296 | close(infofd); | |
4297 | unlink(infofile); | |
8ca02f3c | 4298 | return (0); |
b86bc4cf | 4299 | } |
4300 | ||
4301 | close(infofd); | |
4302 | unlink(infofile); | |
8ca02f3c | 4303 | |
4304 | while (waitpid(pid, &status, 0) < 0) | |
4305 | if (errno != EINTR) | |
4306 | { | |
4307 | status = 1; | |
4308 | break; | |
4309 | } | |
4310 | ||
b9faaae1 | 4311 | cupsdFinishProcess(pid, command, sizeof(command), NULL); |
8ca02f3c | 4312 | |
4313 | if (status) | |
4314 | { | |
4315 | if (WIFEXITED(status)) | |
4316 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
4317 | "Unable to create SSL server key and certificate - " | |
4318 | "the certtool command stopped with status %d!", | |
4319 | WEXITSTATUS(status)); | |
4320 | else | |
4321 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
4322 | "Unable to create SSL server key and certificate - " | |
4323 | "the certtool command crashed on signal %d!", | |
4324 | WTERMSIG(status)); | |
4325 | } | |
4326 | else | |
4327 | { | |
4328 | cupsdLogMessage(CUPSD_LOG_INFO, | |
4329 | "Created SSL server certificate file \"%s\"...", | |
4330 | ServerCertificate); | |
4331 | } | |
4332 | ||
4333 | return (!status); | |
4334 | ||
4335 | #else | |
4336 | return (0); | |
4337 | #endif /* HAVE_LIBSSL && HAVE_WAITPID */ | |
4744bd90 | 4338 | } |
8ca02f3c | 4339 | #endif /* HAVE_SSL */ |
4744bd90 | 4340 | |
4341 | ||
ef416fc2 | 4342 | /* |
4343 | * 'pipe_command()' - Pipe the output of a command to the remote client. | |
4344 | */ | |
4345 | ||
4346 | static int /* O - Process ID */ | |
4347 | pipe_command(cupsd_client_t *con, /* I - Client connection */ | |
4348 | int infile, /* I - Standard input for command */ | |
4349 | int *outfile, /* O - Standard output for command */ | |
4350 | char *command, /* I - Command to run */ | |
4351 | char *options, /* I - Options for command */ | |
4352 | int root) /* I - Run as root? */ | |
4353 | { | |
4354 | int i; /* Looping var */ | |
4355 | int pid; /* Process ID */ | |
b423cd4c | 4356 | char *commptr, /* Command string pointer */ |
4357 | commch; /* Command string character */ | |
ef416fc2 | 4358 | char *uriptr; /* URI string pointer */ |
4359 | int fds[2]; /* Pipe FDs */ | |
4360 | int argc; /* Number of arguments */ | |
4361 | int envc; /* Number of environment variables */ | |
4362 | char argbuf[10240], /* Argument buffer */ | |
4363 | *argv[100], /* Argument strings */ | |
b94498cf | 4364 | *envp[MAX_ENV + 20]; /* Environment variables */ |
5bd77a73 | 4365 | char auth_type[256], /* CUPSD_AUTH_TYPE environment variable */ |
b94498cf | 4366 | content_length[1024], /* CONTENT_LENGTH environment variable */ |
ef416fc2 | 4367 | content_type[1024], /* CONTENT_TYPE environment variable */ |
4368 | http_cookie[32768], /* HTTP_COOKIE environment variable */ | |
f7deaa1a | 4369 | http_referer[1024], /* HTTP_REFERER environment variable */ |
ef416fc2 | 4370 | http_user_agent[1024], /* HTTP_USER_AGENT environment variable */ |
4371 | lang[1024], /* LANG environment variable */ | |
b423cd4c | 4372 | path_info[1024], /* PATH_INFO environment variable */ |
ef416fc2 | 4373 | remote_addr[1024], /* REMOTE_ADDR environment variable */ |
4374 | remote_host[1024], /* REMOTE_HOST environment variable */ | |
4375 | remote_user[1024], /* REMOTE_USER environment variable */ | |
b94498cf | 4376 | script_filename[1024], /* SCRIPT_FILENAME environment variable */ |
ef416fc2 | 4377 | script_name[1024], /* SCRIPT_NAME environment variable */ |
4378 | server_name[1024], /* SERVER_NAME environment variable */ | |
4379 | server_port[1024]; /* SERVER_PORT environment variable */ | |
7dfedb92 | 4380 | ipp_attribute_t *attr; /* attributes-natural-language attribute */ |
5bd77a73 MS |
4381 | #ifdef HAVE_GSSAPI |
4382 | krb5_ccache ccache = NULL; /* Kerberos credentials */ | |
e6013cfa | 4383 | # if defined(HAVE_KRB5_CC_NEW_UNIQUE) || defined(HAVE_HEIMDAL) |
5bd77a73 | 4384 | char krb5ccname[1024]; /* KRB5CCNAME environment variable */ |
e6013cfa | 4385 | # endif /* HAVE_KRB5_CC_NEW_UNIQUE || HAVE_HEIMDAL */ |
5bd77a73 | 4386 | #endif /* HAVE_GSSAPI */ |
ef416fc2 | 4387 | |
4388 | ||
4389 | /* | |
4390 | * Parse a copy of the options string, which is of the form: | |
4391 | * | |
b423cd4c | 4392 | * argument+argument+argument |
4393 | * ?argument+argument+argument | |
4394 | * param=value¶m=value | |
4395 | * ?param=value¶m=value | |
4396 | * /name?argument+argument+argument | |
4397 | * /name?param=value¶m=value | |
ef416fc2 | 4398 | * |
4399 | * If the string contains an "=" character after the initial name, | |
4400 | * then we treat it as a HTTP GET form request and make a copy of | |
4401 | * the remaining string for the environment variable. | |
4402 | * | |
4403 | * The string is always parsed out as command-line arguments, to | |
4404 | * be consistent with Apache... | |
4405 | */ | |
4406 | ||
4407 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
b9faaae1 MS |
4408 | "pipe_command(con=%p(%d), infile=%d, outfile=%p, " |
4409 | "command=\"%s\", options=\"%s\", root=%d)", | |
4410 | con, con->http.fd, infile, outfile, command, | |
4411 | options ? options : "(null)", root); | |
ef416fc2 | 4412 | |
b86bc4cf | 4413 | argv[0] = command; |
ef416fc2 | 4414 | |
b423cd4c | 4415 | if (options) |
4416 | strlcpy(argbuf, options, sizeof(argbuf)); | |
4417 | else | |
4418 | argbuf[0] = '\0'; | |
4419 | ||
4420 | if (argbuf[0] == '/') | |
ef416fc2 | 4421 | { |
4422 | /* | |
b423cd4c | 4423 | * Found some trailing path information, set PATH_INFO... |
ef416fc2 | 4424 | */ |
4425 | ||
b423cd4c | 4426 | if ((commptr = strchr(argbuf, '?')) == NULL) |
4427 | commptr = argbuf + strlen(argbuf); | |
ef416fc2 | 4428 | |
b423cd4c | 4429 | commch = *commptr; |
4430 | *commptr = '\0'; | |
4431 | snprintf(path_info, sizeof(path_info), "PATH_INFO=%s", argbuf); | |
4432 | *commptr = commch; | |
4433 | } | |
4434 | else | |
4435 | { | |
4436 | commptr = argbuf; | |
4437 | path_info[0] = '\0'; | |
b94498cf | 4438 | |
4439 | if (*commptr == ' ') | |
4440 | commptr ++; | |
b423cd4c | 4441 | } |
ef416fc2 | 4442 | |
b86bc4cf | 4443 | if (*commptr == '?' && con->operation == HTTP_GET && !con->query_string) |
b423cd4c | 4444 | { |
4445 | commptr ++; | |
b86bc4cf | 4446 | cupsdSetStringf(&(con->query_string), "QUERY_STRING=%s", commptr); |
b423cd4c | 4447 | } |
ef416fc2 | 4448 | |
b423cd4c | 4449 | argc = 1; |
ef416fc2 | 4450 | |
b423cd4c | 4451 | if (*commptr) |
4452 | { | |
4453 | argv[argc ++] = commptr; | |
ef416fc2 | 4454 | |
b423cd4c | 4455 | for (; *commptr && argc < 99; commptr ++) |
4456 | { | |
ef416fc2 | 4457 | /* |
b423cd4c | 4458 | * Break arguments whenever we see a + or space... |
ef416fc2 | 4459 | */ |
4460 | ||
b423cd4c | 4461 | if (*commptr == ' ' || *commptr == '+') |
4462 | { | |
4463 | while (*commptr == ' ' || *commptr == '+') | |
4464 | *commptr++ = '\0'; | |
ef416fc2 | 4465 | |
b423cd4c | 4466 | /* |
4467 | * If we don't have a blank string, save it as another argument... | |
4468 | */ | |
ef416fc2 | 4469 | |
b423cd4c | 4470 | if (*commptr) |
4471 | { | |
4472 | argv[argc] = commptr; | |
4473 | argc ++; | |
4474 | } | |
4475 | else | |
4476 | break; | |
4477 | } | |
4478 | else if (*commptr == '%' && isxdigit(commptr[1] & 255) && | |
4479 | isxdigit(commptr[2] & 255)) | |
4480 | { | |
4481 | /* | |
4482 | * Convert the %xx notation to the individual character. | |
4483 | */ | |
ef416fc2 | 4484 | |
b423cd4c | 4485 | if (commptr[1] >= '0' && commptr[1] <= '9') |
4486 | *commptr = (commptr[1] - '0') << 4; | |
4487 | else | |
4488 | *commptr = (tolower(commptr[1]) - 'a' + 10) << 4; | |
ef416fc2 | 4489 | |
b423cd4c | 4490 | if (commptr[2] >= '0' && commptr[2] <= '9') |
4491 | *commptr |= commptr[2] - '0'; | |
4492 | else | |
4493 | *commptr |= tolower(commptr[2]) - 'a' + 10; | |
ef416fc2 | 4494 | |
b423cd4c | 4495 | _cups_strcpy(commptr + 1, commptr + 3); |
ef416fc2 | 4496 | |
b423cd4c | 4497 | /* |
4498 | * Check for a %00 and break if that is the case... | |
4499 | */ | |
ef416fc2 | 4500 | |
b423cd4c | 4501 | if (!*commptr) |
4502 | break; | |
4503 | } | |
ef416fc2 | 4504 | } |
4505 | } | |
4506 | ||
4507 | argv[argc] = NULL; | |
4508 | ||
ef416fc2 | 4509 | /* |
4510 | * Setup the environment variables as needed... | |
4511 | */ | |
4512 | ||
b94498cf | 4513 | if (con->username[0]) |
4514 | { | |
5bd77a73 | 4515 | snprintf(auth_type, sizeof(auth_type), "CUPSD_AUTH_TYPE=%s", |
b94498cf | 4516 | httpGetField(HTTP(con), HTTP_FIELD_AUTHORIZATION)); |
4517 | ||
4518 | if ((uriptr = strchr(auth_type + 10, ' ')) != NULL) | |
4519 | *uriptr = '\0'; | |
4520 | } | |
4521 | else | |
4522 | auth_type[0] = '\0'; | |
4523 | ||
7dfedb92 MS |
4524 | if (con->request && |
4525 | (attr = ippFindAttribute(con->request, "attributes-natural-language", | |
4526 | IPP_TAG_LANGUAGE)) != NULL) | |
4527 | { | |
4528 | switch (strlen(attr->values[0].string.text)) | |
4529 | { | |
4530 | default : | |
4531 | /* | |
4532 | * This is an unknown or badly formatted language code; use | |
4533 | * the POSIX locale... | |
4534 | */ | |
4535 | ||
4536 | strcpy(lang, "LANG=C"); | |
4537 | break; | |
4538 | ||
4539 | case 2 : | |
4540 | /* | |
4541 | * Just the language code (ll)... | |
4542 | */ | |
4543 | ||
4544 | snprintf(lang, sizeof(lang), "LANG=%s.UTF8", | |
4545 | attr->values[0].string.text); | |
4546 | break; | |
4547 | ||
4548 | case 5 : | |
4549 | /* | |
4550 | * Language and country code (ll-cc)... | |
4551 | */ | |
4552 | ||
4553 | snprintf(lang, sizeof(lang), "LANG=%c%c_%c%c.UTF8", | |
4554 | attr->values[0].string.text[0], | |
4555 | attr->values[0].string.text[1], | |
4556 | toupper(attr->values[0].string.text[3] & 255), | |
4557 | toupper(attr->values[0].string.text[4] & 255)); | |
4558 | break; | |
4559 | } | |
4560 | } | |
4561 | else if (con->language) | |
4562 | snprintf(lang, sizeof(lang), "LANG=%s.UTF8", con->language->language); | |
ef416fc2 | 4563 | else |
4564 | strcpy(lang, "LANG=C"); | |
4565 | ||
4566 | strcpy(remote_addr, "REMOTE_ADDR="); | |
4567 | httpAddrString(con->http.hostaddr, remote_addr + 12, | |
4568 | sizeof(remote_addr) - 12); | |
4569 | ||
4570 | snprintf(remote_host, sizeof(remote_host), "REMOTE_HOST=%s", | |
4571 | con->http.hostname); | |
4572 | ||
4573 | snprintf(script_name, sizeof(script_name), "SCRIPT_NAME=%s", con->uri); | |
4574 | if ((uriptr = strchr(script_name, '?')) != NULL) | |
4575 | *uriptr = '\0'; | |
4576 | ||
b94498cf | 4577 | snprintf(script_filename, sizeof(script_filename), "SCRIPT_FILENAME=%s%s", |
4578 | DocumentRoot, script_name + 12); | |
4579 | ||
ef416fc2 | 4580 | sprintf(server_port, "SERVER_PORT=%d", con->serverport); |
4581 | ||
4582 | snprintf(server_name, sizeof(server_name), "SERVER_NAME=%s", | |
4583 | con->servername); | |
4584 | ||
4585 | envc = cupsdLoadEnv(envp, (int)(sizeof(envp) / sizeof(envp[0]))); | |
4586 | ||
b94498cf | 4587 | if (auth_type[0]) |
4588 | envp[envc ++] = auth_type; | |
4589 | ||
ef416fc2 | 4590 | envp[envc ++] = lang; |
4591 | envp[envc ++] = "REDIRECT_STATUS=1"; | |
b94498cf | 4592 | envp[envc ++] = "GATEWAY_INTERFACE=CGI/1.1"; |
ef416fc2 | 4593 | envp[envc ++] = server_name; |
4594 | envp[envc ++] = server_port; | |
4595 | envp[envc ++] = remote_addr; | |
4596 | envp[envc ++] = remote_host; | |
4597 | envp[envc ++] = script_name; | |
b94498cf | 4598 | envp[envc ++] = script_filename; |
ef416fc2 | 4599 | |
b423cd4c | 4600 | if (path_info[0]) |
4601 | envp[envc ++] = path_info; | |
4602 | ||
ef416fc2 | 4603 | if (con->username[0]) |
4604 | { | |
4605 | snprintf(remote_user, sizeof(remote_user), "REMOTE_USER=%s", con->username); | |
4606 | ||
4607 | envp[envc ++] = remote_user; | |
5bd77a73 MS |
4608 | |
4609 | /* | |
4610 | * Save Kerberos credentials, if any... | |
4611 | */ | |
4612 | ||
4613 | #ifdef HAVE_GSSAPI | |
4614 | if (con->gss_have_creds) | |
4615 | { | |
4616 | # if !defined(HAVE_KRB5_CC_NEW_UNIQUE) && !defined(HAVE_HEIMDAL) | |
4617 | cupsdLogMessage(CUPSD_LOG_INFO, | |
4618 | "Sorry, your version of Kerberos does not support " | |
4619 | "delegated credentials!"); | |
4620 | ||
4621 | # else | |
4622 | krb5_error_code error; /* Kerberos error code */ | |
4623 | OM_uint32 major_status, /* Major status code */ | |
4624 | minor_status; /* Minor status code */ | |
4625 | krb5_principal principal; /* Kerberos principal */ | |
4626 | ||
4627 | ||
4628 | # ifdef __APPLE__ | |
4629 | /* | |
4630 | * If the weak-linked GSSAPI/Kerberos library is not present, don't try | |
4631 | * to use it... | |
4632 | */ | |
4633 | ||
4634 | if (krb5_init_context != NULL) | |
4635 | { | |
4636 | # endif /* __APPLE__ */ | |
4637 | ||
0af14961 MS |
4638 | if (!KerberosInitialized) |
4639 | { | |
4640 | /* | |
4641 | * Setup a Kerberos context for the scheduler to use... | |
4642 | */ | |
4643 | ||
4644 | KerberosInitialized = 1; | |
4645 | ||
4646 | if (krb5_init_context(&KerberosContext)) | |
4647 | { | |
4648 | KerberosContext = NULL; | |
4649 | ||
b9faaae1 MS |
4650 | cupsdLogMessage(CUPSD_LOG_ERROR, |
4651 | "Unable to initialize Kerberos context"); | |
0af14961 MS |
4652 | } |
4653 | } | |
4654 | ||
5bd77a73 MS |
4655 | /* |
4656 | * We MUST create a file-based cache because memory-based caches are | |
4657 | * only valid for the current process/address space. | |
4658 | * | |
4659 | * Due to various bugs/features in different versions of Kerberos, we | |
4660 | * need either the krb5_cc_new_unique() function or Heimdal's version | |
4661 | * of krb5_cc_gen_new() to create a new FILE: credential cache that | |
4662 | * can be passed to the backend. These functions create a temporary | |
4663 | * file (typically in /tmp) containing the cached credentials, which | |
4664 | * are removed when we have successfully printed a job. | |
4665 | */ | |
4666 | ||
0af14961 MS |
4667 | if (KerberosContext) |
4668 | { | |
5bd77a73 | 4669 | # ifdef HAVE_KRB5_CC_NEW_UNIQUE |
0af14961 MS |
4670 | if ((error = krb5_cc_new_unique(KerberosContext, "FILE", NULL, |
4671 | &ccache)) != 0) | |
5bd77a73 | 4672 | # else /* HAVE_HEIMDAL */ |
0af14961 MS |
4673 | if ((error = krb5_cc_gen_new(KerberosContext, &krb5_fcc_ops, |
4674 | &ccache)) != 0) | |
5bd77a73 | 4675 | # endif /* HAVE_KRB5_CC_NEW_UNIQUE */ |
5bd77a73 | 4676 | { |
0af14961 MS |
4677 | cupsdLogMessage(CUPSD_LOG_ERROR, |
4678 | "Unable to create new credentials cache (%d/%s)", | |
4679 | error, strerror(errno)); | |
4680 | ccache = NULL; | |
4681 | } | |
4682 | else if ((error = krb5_parse_name(KerberosContext, con->username, | |
4683 | &principal)) != 0) | |
4684 | { | |
4685 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
4686 | "Unable to parse kerberos username (%d/%s)", error, | |
4687 | strerror(errno)); | |
4688 | krb5_cc_destroy(KerberosContext, ccache); | |
4689 | ccache = NULL; | |
4690 | } | |
4691 | else if ((error = krb5_cc_initialize(KerberosContext, ccache, | |
4692 | principal))) | |
4693 | { | |
4694 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
b9faaae1 MS |
4695 | "Unable to initialize credentials cache (%d/%s)", |
4696 | error, strerror(errno)); | |
5bd77a73 | 4697 | krb5_cc_destroy(KerberosContext, ccache); |
0af14961 | 4698 | krb5_free_principal(KerberosContext, principal); |
5bd77a73 MS |
4699 | ccache = NULL; |
4700 | } | |
4701 | else | |
4702 | { | |
0af14961 MS |
4703 | krb5_free_principal(KerberosContext, principal); |
4704 | ||
5bd77a73 | 4705 | /* |
0af14961 | 4706 | * Copy the user's credentials to the new cache file... |
5bd77a73 MS |
4707 | */ |
4708 | ||
0af14961 MS |
4709 | major_status = gss_krb5_copy_ccache(&minor_status, |
4710 | con->gss_delegated_cred, ccache); | |
5bd77a73 | 4711 | |
0af14961 MS |
4712 | if (GSS_ERROR(major_status)) |
4713 | { | |
4714 | cupsdLogGSSMessage(CUPSD_LOG_ERROR, major_status, minor_status, | |
4715 | "Unable to import client credentials cache"); | |
4716 | krb5_cc_destroy(KerberosContext, ccache); | |
4717 | ccache = NULL; | |
4718 | } | |
4719 | else | |
4720 | { | |
4721 | /* | |
4722 | * Add the KRB5CCNAME environment variable to the job so that the | |
4723 | * backend can use the credentials when printing. | |
4724 | */ | |
4725 | ||
4726 | snprintf(krb5ccname, sizeof(krb5ccname), "KRB5CCNAME=FILE:%s", | |
4727 | krb5_cc_get_name(KerberosContext, ccache)); | |
4728 | envp[envc++] = krb5ccname; | |
4729 | ||
4730 | if (!RunUser) | |
4731 | chown(krb5_cc_get_name(KerberosContext, ccache), User, Group); | |
4732 | } | |
4733 | } | |
5bd77a73 MS |
4734 | } |
4735 | # ifdef __APPLE__ | |
4736 | } | |
4737 | # endif /* __APPLE__ */ | |
4738 | # endif /* HAVE_KRB5_CC_NEW_UNIQUE || HAVE_HEIMDAL */ | |
4739 | } | |
4740 | #endif /* HAVE_GSSAPI */ | |
ef416fc2 | 4741 | } |
4742 | ||
4743 | if (con->http.version == HTTP_1_1) | |
4744 | envp[envc ++] = "SERVER_PROTOCOL=HTTP/1.1"; | |
4745 | else if (con->http.version == HTTP_1_0) | |
4746 | envp[envc ++] = "SERVER_PROTOCOL=HTTP/1.0"; | |
4747 | else | |
4748 | envp[envc ++] = "SERVER_PROTOCOL=HTTP/0.9"; | |
4749 | ||
4750 | if (con->http.cookie) | |
4751 | { | |
4752 | snprintf(http_cookie, sizeof(http_cookie), "HTTP_COOKIE=%s", | |
4753 | con->http.cookie); | |
4754 | envp[envc ++] = http_cookie; | |
4755 | } | |
4756 | ||
4757 | if (con->http.fields[HTTP_FIELD_USER_AGENT][0]) | |
4758 | { | |
4759 | snprintf(http_user_agent, sizeof(http_user_agent), "HTTP_USER_AGENT=%s", | |
4760 | con->http.fields[HTTP_FIELD_USER_AGENT]); | |
4761 | envp[envc ++] = http_user_agent; | |
4762 | } | |
4763 | ||
f7deaa1a | 4764 | if (con->http.fields[HTTP_FIELD_REFERER][0]) |
4765 | { | |
4766 | snprintf(http_referer, sizeof(http_referer), "HTTP_REFERER=%s", | |
4767 | con->http.fields[HTTP_FIELD_REFERER]); | |
4768 | envp[envc ++] = http_referer; | |
4769 | } | |
4770 | ||
ef416fc2 | 4771 | if (con->operation == HTTP_GET) |
4772 | { | |
ef416fc2 | 4773 | envp[envc ++] = "REQUEST_METHOD=GET"; |
4774 | ||
b86bc4cf | 4775 | if (con->query_string) |
ef416fc2 | 4776 | { |
4777 | /* | |
4778 | * Add GET form variables after ?... | |
4779 | */ | |
4780 | ||
b86bc4cf | 4781 | envp[envc ++] = con->query_string; |
ef416fc2 | 4782 | } |
3dfe78b3 MS |
4783 | else |
4784 | envp[envc ++] = "QUERY_STRING="; | |
ef416fc2 | 4785 | } |
4786 | else | |
4787 | { | |
e1d6a774 | 4788 | sprintf(content_length, "CONTENT_LENGTH=" CUPS_LLFMT, |
4789 | CUPS_LLCAST con->bytes); | |
ef416fc2 | 4790 | snprintf(content_type, sizeof(content_type), "CONTENT_TYPE=%s", |
4791 | con->http.fields[HTTP_FIELD_CONTENT_TYPE]); | |
4792 | ||
4793 | envp[envc ++] = "REQUEST_METHOD=POST"; | |
4794 | envp[envc ++] = content_length; | |
4795 | envp[envc ++] = content_type; | |
4796 | } | |
4797 | ||
4798 | /* | |
4799 | * Tell the CGI if we are using encryption... | |
4800 | */ | |
4801 | ||
a74454a7 | 4802 | if (con->http.tls) |
ef416fc2 | 4803 | envp[envc ++] = "HTTPS=ON"; |
4804 | ||
4805 | /* | |
4806 | * Terminate the environment array... | |
4807 | */ | |
4808 | ||
4809 | envp[envc] = NULL; | |
4810 | ||
4811 | if (LogLevel == CUPSD_LOG_DEBUG2) | |
4812 | { | |
4813 | for (i = 0; i < argc; i ++) | |
4814 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
4815 | "pipe_command: argv[%d] = \"%s\"", i, argv[i]); | |
4816 | for (i = 0; i < envc; i ++) | |
4817 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
4818 | "pipe_command: envp[%d] = \"%s\"", i, envp[i]); | |
4819 | } | |
4820 | ||
4821 | /* | |
4822 | * Create a pipe for the output... | |
4823 | */ | |
4824 | ||
4825 | if (cupsdOpenPipe(fds)) | |
4826 | { | |
ef416fc2 | 4827 | cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to create pipes for CGI %s - %s", |
4828 | argv[0], strerror(errno)); | |
4829 | return (0); | |
4830 | } | |
4831 | ||
4832 | /* | |
4833 | * Then execute the command... | |
4834 | */ | |
4835 | ||
4836 | if (cupsdStartProcess(command, argv, envp, infile, fds[1], CGIPipes[1], | |
b9faaae1 | 4837 | -1, -1, root, DefaultProfile, 0, &pid) < 0) |
ef416fc2 | 4838 | { |
4839 | /* | |
4840 | * Error - can't fork! | |
4841 | */ | |
4842 | ||
4843 | cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to fork for CGI %s - %s", argv[0], | |
4844 | strerror(errno)); | |
4845 | ||
4846 | cupsdClosePipe(fds); | |
4847 | pid = 0; | |
4848 | } | |
4849 | else | |
4850 | { | |
4851 | /* | |
4852 | * Fork successful - return the PID... | |
4853 | */ | |
4854 | ||
4855 | if (con->username[0]) | |
5bd77a73 MS |
4856 | #ifdef HAVE_GSSAPI |
4857 | cupsdAddCert(pid, con->username, ccache); | |
4858 | #else | |
4859 | cupsdAddCert(pid, con->username, NULL); | |
4860 | #endif /* HAVE_GSSAPI */ | |
ef416fc2 | 4861 | |
355e94dc MS |
4862 | cupsdLogMessage(CUPSD_LOG_DEBUG, "[CGI] %s started - PID = %d", |
4863 | command, pid); | |
ef416fc2 | 4864 | |
4865 | *outfile = fds[0]; | |
4866 | close(fds[1]); | |
4867 | } | |
4868 | ||
ef416fc2 | 4869 | return (pid); |
4870 | } | |
4871 | ||
4872 | ||
4873 | /* | |
a74454a7 | 4874 | * 'write_file()' - Send a file via HTTP. |
e1d6a774 | 4875 | */ |
4876 | ||
4877 | static int /* O - 0 on failure, 1 on success */ | |
a74454a7 | 4878 | write_file(cupsd_client_t *con, /* I - Client connection */ |
4879 | http_status_t code, /* I - HTTP status */ | |
4880 | char *filename, /* I - Filename */ | |
4881 | char *type, /* I - File type */ | |
4882 | struct stat *filestats) /* O - File information */ | |
e1d6a774 | 4883 | { |
4884 | con->file = open(filename, O_RDONLY); | |
4885 | ||
b9faaae1 MS |
4886 | cupsdLogMessage(CUPSD_LOG_DEBUG2, |
4887 | "write_file(con=%p(%d), code=%d, filename=\"%s\" (%d), " | |
4888 | "type=\"%s\", filestats=%p)", con, con->http.fd, | |
4889 | code, filename, con->file, type ? type : "(null)", filestats); | |
e1d6a774 | 4890 | |
4891 | if (con->file < 0) | |
4892 | return (0); | |
4893 | ||
4894 | fcntl(con->file, F_SETFD, fcntl(con->file, F_GETFD) | FD_CLOEXEC); | |
4895 | ||
4896 | con->pipe_pid = 0; | |
4897 | ||
5bd77a73 | 4898 | if (!cupsdSendHeader(con, code, type, CUPSD_AUTH_NONE)) |
e1d6a774 | 4899 | return (0); |
4900 | ||
4901 | if (httpPrintf(HTTP(con), "Last-Modified: %s\r\n", | |
4902 | httpGetDateString(filestats->st_mtime)) < 0) | |
4903 | return (0); | |
4904 | if (httpPrintf(HTTP(con), "Content-Length: " CUPS_LLFMT "\r\n", | |
4905 | CUPS_LLCAST filestats->st_size) < 0) | |
4906 | return (0); | |
4907 | if (httpPrintf(HTTP(con), "\r\n") < 0) | |
4908 | return (0); | |
4909 | ||
07725fee | 4910 | if (cupsdFlushHeader(con) < 0) |
4911 | return (0); | |
d09495fa | 4912 | |
e1d6a774 | 4913 | con->http.data_encoding = HTTP_ENCODE_LENGTH; |
4914 | con->http.data_remaining = filestats->st_size; | |
4915 | ||
4916 | if (con->http.data_remaining <= INT_MAX) | |
4917 | con->http._data_remaining = con->http.data_remaining; | |
4918 | else | |
4919 | con->http._data_remaining = INT_MAX; | |
4920 | ||
f7deaa1a | 4921 | cupsdAddSelect(con->http.fd, (cupsd_selfunc_t)cupsdReadClient, |
4922 | (cupsd_selfunc_t)cupsdWriteClient, con); | |
e1d6a774 | 4923 | |
4924 | return (1); | |
4925 | } | |
4926 | ||
4927 | ||
4928 | /* | |
f899b121 | 4929 | * 'write_pipe()' - Flag that data is available on the CGI pipe. |
4930 | */ | |
4931 | ||
4932 | static void | |
4933 | write_pipe(cupsd_client_t *con) /* I - Client connection */ | |
4934 | { | |
b9faaae1 MS |
4935 | cupsdLogMessage(CUPSD_LOG_DEBUG2, |
4936 | "write_pipe(con=%p(%d)) CGI output on fd %d", | |
4937 | con, con->http.fd, con->file); | |
f899b121 | 4938 | |
4939 | con->file_ready = 1; | |
4940 | ||
4941 | cupsdRemoveSelect(con->file); | |
4942 | cupsdAddSelect(con->http.fd, NULL, (cupsd_selfunc_t)cupsdWriteClient, con); | |
4943 | } | |
4944 | ||
4945 | ||
4946 | /* | |
b19ccc9e | 4947 | * End of "$Id: client.c 7950 2008-09-17 00:21:59Z mike $". |
ef416fc2 | 4948 | */ |