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