]>
Commit | Line | Data |
---|---|---|
ef416fc2 | 1 | /* |
c07d5b2d | 2 | * "$Id: client.c 181 2006-06-22 20:01:18Z jlovell $" |
ef416fc2 | 3 | * |
4 | * Client routines for the Common UNIX Printing System (CUPS) scheduler. | |
5 | * | |
6 | * Copyright 1997-2006 by Easy Software Products, all rights reserved. | |
7 | * | |
8 | * These coded instructions, statements, and computer programs are the | |
9 | * property of Easy Software Products and are protected by Federal | |
10 | * copyright law. Distribution and use rights are outlined in the file | |
11 | * "LICENSE.txt" which should have been included with this file. If this | |
12 | * file is missing or damaged please contact Easy Software Products | |
13 | * at: | |
14 | * | |
15 | * Attn: CUPS Licensing Information | |
16 | * Easy Software Products | |
17 | * 44141 Airport View Drive, Suite 204 | |
18 | * Hollywood, Maryland 20636 USA | |
19 | * | |
20 | * Voice: (301) 373-9600 | |
21 | * EMail: cups-info@cups.org | |
22 | * WWW: http://www.cups.org | |
23 | * | |
24 | * Contents: | |
25 | * | |
26 | * cupsdAcceptClient() - Accept a new client. | |
27 | * cupsdCloseAllClients() - Close all remote clients immediately. | |
28 | * cupsdCloseClient() - Close a remote client. | |
ef416fc2 | 29 | * cupsdReadClient() - Read data from a client. |
30 | * cupsdSendCommand() - Send output from a command via HTTP. | |
31 | * cupsdSendError() - Send an error message via HTTP. | |
ef416fc2 | 32 | * cupsdSendHeader() - Send an HTTP request. |
33 | * cupsdUpdateCGI() - Read status messages from CGI scripts and programs. | |
34 | * cupsdWriteClient() - Write data to a client as needed. | |
35 | * check_if_modified() - Decode an "If-Modified-Since" line. | |
e1d6a774 | 36 | * encrypt_client() - Enable encryption for the client... |
ef416fc2 | 37 | * get_cdsa_server_certs() - Convert a keychain name into the CFArrayRef |
38 | * required by SSLSetCertificate. | |
39 | * get_file() - Get a filename and state info. | |
40 | * install_conf_file() - Install a configuration file. | |
e1d6a774 | 41 | * is_cgi() - Is the resource a CGI script/program? |
ef416fc2 | 42 | * is_path_absolute() - Is a path absolute and free of relative elements. |
4744bd90 | 43 | * make_certificate() - Make a self-signed SSL/TLS certificate. |
ef416fc2 | 44 | * pipe_command() - Pipe the output of a command to the remote client. |
a74454a7 | 45 | * write_file() - Send a file via HTTP. |
ef416fc2 | 46 | */ |
47 | ||
48 | /* | |
49 | * Include necessary headers... | |
50 | */ | |
51 | ||
52 | #include <cups/http-private.h> | |
53 | #include "cupsd.h" | |
54 | ||
55 | #ifdef HAVE_CDSASSL | |
56 | # include <Security/Security.h> | |
ed486911 | 57 | # ifdef HAVE_SECBASEPRIV_H |
58 | # include <Security/SecBasePriv.h> | |
59 | # else | |
60 | extern const char *cssmErrorString(int error); | |
61 | # endif /* HAVE_SECBASEPRIV_H */ | |
ef416fc2 | 62 | #endif /* HAVE_CDSASSL */ |
4744bd90 | 63 | #ifdef HAVE_GNUTLS |
64 | # include <gnutls/x509.h> | |
65 | #endif /* HAVE_GNUTLS */ | |
ef416fc2 | 66 | |
67 | ||
68 | /* | |
69 | * Local functions... | |
70 | */ | |
71 | ||
72 | static int check_if_modified(cupsd_client_t *con, | |
73 | struct stat *filestats); | |
80ca4592 | 74 | #ifdef HAVE_SSL |
e1d6a774 | 75 | static int encrypt_client(cupsd_client_t *con); |
80ca4592 | 76 | #endif /* HAVE_SSL */ |
ef416fc2 | 77 | #ifdef HAVE_CDSASSL |
78 | static CFArrayRef get_cdsa_server_certs(void); | |
79 | #endif /* HAVE_CDSASSL */ | |
80 | static char *get_file(cupsd_client_t *con, struct stat *filestats, | |
81 | char *filename, int len); | |
82 | static http_status_t install_conf_file(cupsd_client_t *con); | |
e1d6a774 | 83 | static int is_cgi(cupsd_client_t *con, const char *filename, |
84 | struct stat *filestats, mime_type_t *type); | |
ef416fc2 | 85 | static int is_path_absolute(const char *path); |
4744bd90 | 86 | #ifdef HAVE_GNUTLS |
87 | static void make_certificate(void); | |
88 | #endif /* HAVE_GNUTLS */ | |
ef416fc2 | 89 | static int pipe_command(cupsd_client_t *con, int infile, int *outfile, |
90 | char *command, char *options, int root); | |
a74454a7 | 91 | static int write_file(cupsd_client_t *con, http_status_t code, |
92 | char *filename, char *type, | |
93 | struct stat *filestats); | |
ef416fc2 | 94 | |
95 | ||
96 | /* | |
97 | * 'cupsdAcceptClient()' - Accept a new client. | |
98 | */ | |
99 | ||
100 | void | |
101 | cupsdAcceptClient(cupsd_listener_t *lis)/* I - Listener socket */ | |
102 | { | |
ef416fc2 | 103 | int count; /* Count of connections on a host */ |
104 | int val; /* Parameter value */ | |
bd7854cb | 105 | cupsd_client_t *con, /* New client pointer */ |
106 | *tempcon; /* Temporary client pointer */ | |
ef416fc2 | 107 | http_addrlist_t *addrlist, /* List of adddresses for host */ |
108 | *addr; /* Current address */ | |
109 | socklen_t addrlen; /* Length of address */ | |
110 | char *hostname; /* Hostname for address */ | |
111 | http_addr_t temp; /* Temporary address variable */ | |
112 | static time_t last_dos = 0; /* Time of last DoS attack */ | |
113 | ||
114 | ||
115 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
bd7854cb | 116 | "cupsdAcceptClient(lis=%p) %d Clients = %d", |
117 | lis, lis->fd, cupsArrayCount(Clients)); | |
ef416fc2 | 118 | |
119 | /* | |
120 | * Make sure we don't have a full set of clients already... | |
121 | */ | |
122 | ||
bd7854cb | 123 | if (cupsArrayCount(Clients) == MaxClients) |
ef416fc2 | 124 | return; |
125 | ||
126 | /* | |
127 | * Get a pointer to the next available client... | |
128 | */ | |
129 | ||
bd7854cb | 130 | if (!Clients) |
131 | Clients = cupsArrayNew(NULL, NULL); | |
132 | ||
133 | if (!Clients) | |
134 | return; | |
135 | ||
136 | con = calloc(1, sizeof(cupsd_client_t)); | |
ef416fc2 | 137 | |
ef416fc2 | 138 | con->http.activity = time(NULL); |
139 | con->file = -1; | |
140 | con->http.hostaddr = &(con->clientaddr); | |
141 | ||
142 | /* | |
143 | * Accept the client and get the remote address... | |
144 | */ | |
145 | ||
146 | addrlen = sizeof(http_addr_t); | |
147 | ||
148 | if ((con->http.fd = accept(lis->fd, (struct sockaddr *)con->http.hostaddr, | |
149 | &addrlen)) < 0) | |
150 | { | |
151 | cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to accept client connection - %s.", | |
152 | strerror(errno)); | |
bd7854cb | 153 | free(con); |
ef416fc2 | 154 | return; |
155 | } | |
156 | ||
157 | #ifdef AF_INET6 | |
158 | if (lis->address.addr.sa_family == AF_INET6) | |
159 | { | |
160 | /* | |
161 | * Save the connected port number... | |
162 | */ | |
163 | ||
164 | con->http.hostaddr->ipv6.sin6_port = lis->address.ipv6.sin6_port; | |
165 | ||
166 | /* | |
167 | * Convert IPv4 over IPv6 addresses (::ffff:n.n.n.n) to IPv4 forms we | |
168 | * can more easily use... | |
169 | */ | |
170 | ||
171 | if (con->http.hostaddr->ipv6.sin6_addr.s6_addr32[0] == 0 && | |
172 | con->http.hostaddr->ipv6.sin6_addr.s6_addr32[1] == 0 && | |
173 | ntohl(con->http.hostaddr->ipv6.sin6_addr.s6_addr32[2]) == 0xffff) | |
174 | con->http.hostaddr->ipv6.sin6_addr.s6_addr32[2] = 0; | |
175 | } | |
176 | else | |
177 | #endif /* AF_INET6 */ | |
178 | if (lis->address.addr.sa_family == AF_INET) | |
179 | con->http.hostaddr->ipv4.sin_port = lis->address.ipv4.sin_port; | |
180 | ||
181 | /* | |
182 | * Check the number of clients on the same address... | |
183 | */ | |
184 | ||
bd7854cb | 185 | for (count = 0, tempcon = (cupsd_client_t *)cupsArrayFirst(Clients); |
186 | tempcon; | |
187 | tempcon = (cupsd_client_t *)cupsArrayNext(Clients)) | |
188 | if (httpAddrEqual(tempcon->http.hostaddr, con->http.hostaddr)) | |
ef416fc2 | 189 | { |
190 | count ++; | |
191 | if (count >= MaxClientsPerHost) | |
192 | break; | |
193 | } | |
194 | ||
195 | if (count >= MaxClientsPerHost) | |
196 | { | |
197 | if ((time(NULL) - last_dos) >= 60) | |
198 | { | |
199 | last_dos = time(NULL); | |
200 | cupsdLogMessage(CUPSD_LOG_WARN, | |
bd7854cb | 201 | "Possible DoS attack - more than %d clients connecting " |
202 | "from %s!", | |
203 | MaxClientsPerHost, tempcon->http.hostname); | |
ef416fc2 | 204 | } |
205 | ||
206 | #ifdef WIN32 | |
207 | closesocket(con->http.fd); | |
208 | #else | |
209 | close(con->http.fd); | |
210 | #endif /* WIN32 */ | |
211 | ||
bd7854cb | 212 | free(con); |
ef416fc2 | 213 | return; |
214 | } | |
215 | ||
216 | /* | |
217 | * Get the hostname or format the IP address as needed... | |
218 | */ | |
219 | ||
220 | if (httpAddrLocalhost(con->http.hostaddr)) | |
221 | { | |
222 | /* | |
223 | * Map accesses from the loopback interface to "localhost"... | |
224 | */ | |
225 | ||
226 | strlcpy(con->http.hostname, "localhost", sizeof(con->http.hostname)); | |
227 | hostname = con->http.hostname; | |
228 | } | |
229 | else | |
230 | { | |
231 | /* | |
232 | * Map accesses from the same host to the server name. | |
233 | */ | |
234 | ||
235 | for (addr = ServerAddrs; addr; addr = addr->next) | |
236 | if (httpAddrEqual(con->http.hostaddr, &(addr->addr))) | |
237 | break; | |
238 | ||
239 | if (addr) | |
240 | { | |
241 | strlcpy(con->http.hostname, ServerName, sizeof(con->http.hostname)); | |
242 | hostname = con->http.hostname; | |
243 | } | |
244 | else if (HostNameLookups) | |
245 | hostname = httpAddrLookup(con->http.hostaddr, con->http.hostname, | |
246 | sizeof(con->http.hostname)); | |
247 | else | |
248 | { | |
249 | hostname = NULL; | |
250 | httpAddrString(con->http.hostaddr, con->http.hostname, | |
251 | sizeof(con->http.hostname)); | |
252 | } | |
253 | } | |
254 | ||
255 | if (hostname == NULL && HostNameLookups == 2) | |
256 | { | |
257 | /* | |
258 | * Can't have an unresolved IP address with double-lookups enabled... | |
259 | */ | |
260 | ||
261 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
262 | "cupsdAcceptClient: Closing connection %d...", | |
263 | con->http.fd); | |
264 | ||
265 | #ifdef WIN32 | |
266 | closesocket(con->http.fd); | |
267 | #else | |
268 | close(con->http.fd); | |
269 | #endif /* WIN32 */ | |
270 | ||
271 | cupsdLogMessage(CUPSD_LOG_WARN, | |
272 | "Name lookup failed - connection from %s closed!", | |
273 | con->http.hostname); | |
bd7854cb | 274 | |
275 | free(con); | |
ef416fc2 | 276 | return; |
277 | } | |
278 | ||
279 | if (HostNameLookups == 2) | |
280 | { | |
281 | /* | |
282 | * Do double lookups as needed... | |
283 | */ | |
284 | ||
285 | if ((addrlist = httpAddrGetList(con->http.hostname, AF_UNSPEC, NULL)) != NULL) | |
286 | { | |
287 | /* | |
288 | * See if the hostname maps to the same IP address... | |
289 | */ | |
290 | ||
291 | for (addr = addrlist; addr; addr = addr->next) | |
292 | if (httpAddrEqual(con->http.hostaddr, &(addr->addr))) | |
293 | break; | |
294 | } | |
295 | else | |
296 | addr = NULL; | |
297 | ||
298 | httpAddrFreeList(addrlist); | |
299 | ||
300 | if (!addr) | |
301 | { | |
302 | /* | |
303 | * Can't have a hostname that doesn't resolve to the same IP address | |
304 | * with double-lookups enabled... | |
305 | */ | |
306 | ||
307 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
308 | "cupsdAcceptClient: Closing connection %d...", | |
309 | con->http.fd); | |
310 | ||
311 | #ifdef WIN32 | |
312 | closesocket(con->http.fd); | |
313 | #else | |
314 | close(con->http.fd); | |
315 | #endif /* WIN32 */ | |
316 | ||
317 | cupsdLogMessage(CUPSD_LOG_WARN, | |
318 | "IP lookup failed - connection from %s closed!", | |
319 | con->http.hostname); | |
bd7854cb | 320 | free(con); |
ef416fc2 | 321 | return; |
322 | } | |
323 | } | |
324 | ||
325 | #ifdef AF_INET6 | |
326 | if (con->http.hostaddr->addr.sa_family == AF_INET6) | |
327 | cupsdLogMessage(CUPSD_LOG_DEBUG, "cupsdAcceptClient: %d from %s:%d (IPv6)", | |
328 | con->http.fd, con->http.hostname, | |
329 | ntohs(con->http.hostaddr->ipv6.sin6_port)); | |
330 | else | |
331 | #endif /* AF_INET6 */ | |
332 | #ifdef AF_LOCAL | |
333 | if (con->http.hostaddr->addr.sa_family == AF_LOCAL) | |
334 | cupsdLogMessage(CUPSD_LOG_DEBUG, "cupsdAcceptClient: %d from %s (Domain)", | |
335 | con->http.fd, con->http.hostname); | |
336 | else | |
337 | #endif /* AF_LOCAL */ | |
338 | cupsdLogMessage(CUPSD_LOG_DEBUG, "cupsdAcceptClient: %d from %s:%d (IPv4)", | |
339 | con->http.fd, con->http.hostname, | |
340 | ntohs(con->http.hostaddr->ipv4.sin_port)); | |
341 | ||
342 | /* | |
343 | * Get the local address the client connected to... | |
344 | */ | |
345 | ||
346 | addrlen = sizeof(temp); | |
347 | if (getsockname(con->http.fd, (struct sockaddr *)&temp, &addrlen)) | |
348 | { | |
349 | cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to get local address - %s", | |
350 | strerror(errno)); | |
351 | ||
352 | strcpy(con->servername, "localhost"); | |
353 | con->serverport = LocalPort; | |
354 | } | |
355 | else | |
356 | { | |
357 | #ifdef AF_INET6 | |
358 | if (temp.addr.sa_family == AF_INET6) | |
359 | { | |
360 | httpAddrLookup(&temp, con->servername, sizeof(con->servername)); | |
361 | con->serverport = ntohs(lis->address.ipv6.sin6_port); | |
362 | } | |
363 | else | |
364 | #endif /* AF_INET6 */ | |
365 | if (temp.addr.sa_family == AF_INET) | |
366 | { | |
367 | httpAddrLookup(&temp, con->servername, sizeof(con->servername)); | |
368 | con->serverport = ntohs(lis->address.ipv4.sin_port); | |
369 | } | |
370 | else | |
371 | { | |
372 | strcpy(con->servername, "localhost"); | |
373 | con->serverport = LocalPort; | |
374 | } | |
375 | } | |
376 | ||
bd7854cb | 377 | cupsArrayAdd(Clients, con); |
378 | ||
ef416fc2 | 379 | cupsdLogMessage(CUPSD_LOG_DEBUG2, |
380 | "cupsdAcceptClient: %d connected to server on %s:%d", | |
381 | con->http.fd, con->servername, con->serverport); | |
382 | ||
383 | /* | |
384 | * Using TCP_NODELAY improves responsiveness, especially on systems | |
385 | * with a slow loopback interface... Since we write large buffers | |
386 | * when sending print files and requests, there shouldn't be any | |
387 | * performance penalty for this... | |
388 | */ | |
389 | ||
390 | val = 1; | |
391 | setsockopt(con->http.fd, IPPROTO_TCP, TCP_NODELAY, &val, sizeof(val)); | |
392 | ||
393 | /* | |
394 | * Close this file on all execs... | |
395 | */ | |
396 | ||
397 | fcntl(con->http.fd, F_SETFD, fcntl(con->http.fd, F_GETFD) | FD_CLOEXEC); | |
398 | ||
399 | /* | |
400 | * Add the socket to the select() input mask. | |
401 | */ | |
402 | ||
403 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
404 | "cupsdAcceptClient: Adding fd %d to InputSet...", | |
405 | con->http.fd); | |
406 | FD_SET(con->http.fd, InputSet); | |
407 | ||
ef416fc2 | 408 | /* |
409 | * Temporarily suspend accept()'s until we lose a client... | |
410 | */ | |
411 | ||
bd7854cb | 412 | if (cupsArrayCount(Clients) == MaxClients) |
ef416fc2 | 413 | cupsdPauseListening(); |
414 | ||
415 | #ifdef HAVE_SSL | |
416 | /* | |
417 | * See if we are connecting on a secure port... | |
418 | */ | |
419 | ||
420 | if (lis->encryption == HTTP_ENCRYPT_ALWAYS) | |
421 | { | |
422 | /* | |
423 | * https connection; go secure... | |
424 | */ | |
425 | ||
426 | con->http.encryption = HTTP_ENCRYPT_ALWAYS; | |
427 | ||
e1d6a774 | 428 | encrypt_client(con); |
ef416fc2 | 429 | } |
430 | else | |
431 | con->auto_ssl = 1; | |
432 | #endif /* HAVE_SSL */ | |
433 | } | |
434 | ||
435 | ||
436 | /* | |
437 | * 'cupsdCloseAllClients()' - Close all remote clients immediately. | |
438 | */ | |
439 | ||
440 | void | |
441 | cupsdCloseAllClients(void) | |
442 | { | |
bd7854cb | 443 | cupsd_client_t *con; /* Current client */ |
444 | ||
445 | ||
446 | for (con = (cupsd_client_t *)cupsArrayFirst(Clients); | |
447 | con; | |
448 | con = (cupsd_client_t *)cupsArrayNext(Clients)) | |
449 | cupsdCloseClient(con); | |
ef416fc2 | 450 | } |
451 | ||
452 | ||
453 | /* | |
454 | * 'cupsdCloseClient()' - Close a remote client. | |
455 | */ | |
456 | ||
457 | int /* O - 1 if partial close, 0 if fully closed */ | |
458 | cupsdCloseClient(cupsd_client_t *con) /* I - Client to close */ | |
459 | { | |
460 | int partial; /* Do partial close for SSL? */ | |
461 | #ifdef HAVE_LIBSSL | |
462 | SSL_CTX *context; /* Context for encryption */ | |
463 | SSL *conn; /* Connection for encryption */ | |
464 | unsigned long error; /* Error code */ | |
465 | #elif defined(HAVE_GNUTLS) | |
89d46774 | 466 | http_tls_t *conn; /* TLS connection information */ |
467 | int error; /* Error code */ | |
ef416fc2 | 468 | gnutls_certificate_server_credentials *credentials; |
469 | /* TLS credentials */ | |
89d46774 | 470 | # elif defined(HAVE_CDSASSL) |
471 | http_tls_t *conn; /* CDSA connection information */ | |
ef416fc2 | 472 | #endif /* HAVE_LIBSSL */ |
473 | ||
474 | ||
475 | cupsdLogMessage(CUPSD_LOG_DEBUG, "cupsdCloseClient: %d", con->http.fd); | |
476 | ||
477 | /* | |
478 | * Flush pending writes before closing... | |
479 | */ | |
480 | ||
481 | httpFlushWrite(HTTP(con)); | |
482 | ||
483 | partial = 0; | |
484 | ||
485 | #ifdef HAVE_SSL | |
486 | /* | |
487 | * Shutdown encryption as needed... | |
488 | */ | |
489 | ||
490 | if (con->http.tls) | |
491 | { | |
492 | partial = 1; | |
493 | ||
494 | # ifdef HAVE_LIBSSL | |
495 | conn = (SSL *)(con->http.tls); | |
496 | context = SSL_get_SSL_CTX(conn); | |
497 | ||
498 | switch (SSL_shutdown(conn)) | |
499 | { | |
500 | case 1 : | |
501 | cupsdLogMessage(CUPSD_LOG_INFO, | |
502 | "cupsdCloseClient: SSL shutdown successful!"); | |
503 | break; | |
504 | case -1 : | |
505 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
506 | "cupsdCloseClient: Fatal error during SSL shutdown!"); | |
507 | default : | |
508 | while ((error = ERR_get_error()) != 0) | |
509 | cupsdLogMessage(CUPSD_LOG_ERROR, "cupsdCloseClient: %s", | |
510 | ERR_error_string(error, NULL)); | |
511 | break; | |
512 | } | |
513 | ||
514 | SSL_CTX_free(context); | |
515 | SSL_free(conn); | |
516 | ||
517 | # elif defined(HAVE_GNUTLS) | |
518 | conn = (http_tls_t *)(con->http.tls); | |
519 | credentials = (gnutls_certificate_server_credentials *)(conn->credentials); | |
520 | ||
521 | error = gnutls_bye(conn->session, GNUTLS_SHUT_WR); | |
522 | switch (error) | |
523 | { | |
524 | case GNUTLS_E_SUCCESS: | |
525 | cupsdLogMessage(CUPSD_LOG_INFO, | |
526 | "cupsdCloseClient: SSL shutdown successful!"); | |
527 | break; | |
528 | default: | |
529 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
530 | "cupsdCloseClient: %s", gnutls_strerror(error)); | |
531 | break; | |
532 | } | |
533 | ||
534 | gnutls_deinit(conn->session); | |
535 | gnutls_certificate_free_credentials(*credentials); | |
536 | free(credentials); | |
537 | free(conn); | |
538 | ||
539 | # elif defined(HAVE_CDSASSL) | |
89d46774 | 540 | conn = (http_tls_t *)(con->http.tls); |
541 | ||
542 | while (SSLClose(conn->session) == errSSLWouldBlock) | |
b423cd4c | 543 | usleep(1000); |
544 | ||
89d46774 | 545 | SSLDisposeContext(conn->session); |
546 | ||
547 | if (conn->certsArray) | |
548 | CFRelease(conn->certsArray); | |
549 | ||
550 | free(conn); | |
ef416fc2 | 551 | # endif /* HAVE_LIBSSL */ |
552 | ||
553 | con->http.tls = NULL; | |
554 | } | |
555 | #endif /* HAVE_SSL */ | |
556 | ||
557 | if (con->pipe_pid != 0) | |
558 | { | |
559 | /* | |
560 | * Stop any CGI process... | |
561 | */ | |
562 | ||
563 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
564 | "cupsdCloseClient: %d Killing process ID %d...", | |
565 | con->http.fd, con->pipe_pid); | |
566 | cupsdEndProcess(con->pipe_pid, 1); | |
567 | con->pipe_pid = 0; | |
568 | } | |
569 | ||
570 | if (con->file >= 0) | |
571 | { | |
572 | if (FD_ISSET(con->file, InputSet)) | |
573 | { | |
574 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
575 | "cupsdCloseClient: %d Removing fd %d from InputSet...", | |
576 | con->http.fd, con->file); | |
577 | FD_CLR(con->file, InputSet); | |
578 | } | |
579 | ||
580 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
581 | "cupsdCloseClient: %d Closing data file %d.", | |
582 | con->http.fd, con->file); | |
583 | ||
584 | close(con->file); | |
585 | con->file = -1; | |
586 | } | |
587 | ||
588 | /* | |
589 | * Close the socket and clear the file from the input set for select()... | |
590 | */ | |
591 | ||
592 | if (con->http.fd > 0) | |
593 | { | |
594 | if (partial) | |
595 | { | |
596 | /* | |
597 | * Only do a partial close so that the encrypted client gets everything. | |
598 | */ | |
599 | ||
600 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
601 | "cupsdCloseClient: Removing fd %d from OutputSet...", | |
602 | con->http.fd); | |
603 | shutdown(con->http.fd, 0); | |
604 | FD_CLR(con->http.fd, OutputSet); | |
605 | } | |
606 | else | |
607 | { | |
608 | /* | |
609 | * Shut the socket down fully... | |
610 | */ | |
611 | ||
612 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
613 | "cupsdCloseClient: Removing fd %d from InputSet and OutputSet...", | |
614 | con->http.fd); | |
615 | close(con->http.fd); | |
616 | FD_CLR(con->http.fd, InputSet); | |
617 | FD_CLR(con->http.fd, OutputSet); | |
618 | con->http.fd = -1; | |
619 | } | |
620 | } | |
621 | ||
622 | if (!partial) | |
623 | { | |
624 | /* | |
625 | * Free memory... | |
626 | */ | |
627 | ||
628 | if (con->http.input_set) | |
629 | free(con->http.input_set); | |
630 | ||
631 | httpClearCookie(HTTP(con)); | |
632 | ||
633 | cupsdClearString(&con->filename); | |
634 | cupsdClearString(&con->command); | |
635 | cupsdClearString(&con->options); | |
636 | ||
637 | if (con->request) | |
638 | { | |
639 | ippDelete(con->request); | |
640 | con->request = NULL; | |
641 | } | |
642 | ||
643 | if (con->response) | |
644 | { | |
645 | ippDelete(con->response); | |
646 | con->response = NULL; | |
647 | } | |
648 | ||
649 | if (con->language) | |
650 | { | |
651 | cupsLangFree(con->language); | |
652 | con->language = NULL; | |
653 | } | |
654 | ||
655 | /* | |
656 | * Re-enable new client connections if we are going back under the | |
657 | * limit... | |
658 | */ | |
659 | ||
bd7854cb | 660 | if (cupsArrayCount(Clients) == MaxClients) |
ef416fc2 | 661 | cupsdResumeListening(); |
662 | ||
663 | /* | |
664 | * Compact the list of clients as necessary... | |
665 | */ | |
666 | ||
bd7854cb | 667 | cupsArrayRemove(Clients, con); |
ef416fc2 | 668 | |
bd7854cb | 669 | free(con); |
ef416fc2 | 670 | } |
671 | ||
672 | return (partial); | |
673 | } | |
674 | ||
675 | ||
ef416fc2 | 676 | /* |
677 | * 'cupsdReadClient()' - Read data from a client. | |
678 | */ | |
679 | ||
680 | int /* O - 1 on success, 0 on error */ | |
681 | cupsdReadClient(cupsd_client_t *con) /* I - Client to read from */ | |
682 | { | |
683 | char line[32768], /* Line from client... */ | |
684 | operation[64], /* Operation code from socket */ | |
685 | version[64], /* HTTP version number string */ | |
686 | locale[64], /* Locale */ | |
687 | *ptr; /* Pointer into strings */ | |
688 | int major, minor; /* HTTP version numbers */ | |
689 | http_status_t status; /* Transfer status */ | |
690 | ipp_state_t ipp_state; /* State of IPP transfer */ | |
691 | int bytes; /* Number of bytes to POST */ | |
692 | char *filename; /* Name of file for GET/HEAD */ | |
693 | char buf[1024]; /* Buffer for real filename */ | |
694 | struct stat filestats; /* File information */ | |
695 | mime_type_t *type; /* MIME type of file */ | |
696 | cupsd_printer_t *p; /* Printer */ | |
697 | static unsigned request_id = 0; /* Request ID for temp files */ | |
698 | ||
699 | ||
700 | status = HTTP_CONTINUE; | |
701 | ||
702 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
703 | "cupsdReadClient: %d, used=%d, file=%d state=%d", | |
704 | con->http.fd, con->http.used, con->file, con->http.state); | |
705 | ||
706 | if (con->http.error) | |
707 | { | |
708 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdReadClient: http error seen..."); | |
709 | return (cupsdCloseClient(con)); | |
710 | } | |
711 | ||
712 | #ifdef HAVE_SSL | |
713 | if (con->auto_ssl) | |
714 | { | |
715 | /* | |
716 | * Automatically check for a SSL/TLS handshake... | |
717 | */ | |
718 | ||
719 | con->auto_ssl = 0; | |
720 | ||
721 | if (recv(con->http.fd, buf, 1, MSG_PEEK) == 1 && | |
722 | (!buf[0] || !strchr("DGHOPT", buf[0]))) | |
723 | { | |
724 | /* | |
725 | * Encrypt this connection... | |
726 | */ | |
727 | ||
728 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
729 | "cupsdReadClient: Saw first byte %02X, auto-negotiating SSL/TLS session...", | |
730 | buf[0] & 255); | |
731 | ||
e1d6a774 | 732 | encrypt_client(con); |
ef416fc2 | 733 | return (1); |
734 | } | |
735 | } | |
736 | #endif /* HAVE_SSL */ | |
737 | ||
738 | switch (con->http.state) | |
739 | { | |
740 | case HTTP_WAITING : | |
741 | /* | |
742 | * See if we've received a request line... | |
743 | */ | |
744 | ||
745 | if (httpGets(line, sizeof(line) - 1, HTTP(con)) == NULL) | |
746 | { | |
747 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
748 | "cupsdReadClient: httpGets returned EOF..."); | |
749 | return (cupsdCloseClient(con)); | |
750 | } | |
751 | ||
752 | /* | |
753 | * Ignore blank request lines... | |
754 | */ | |
755 | ||
756 | if (line[0] == '\0') | |
757 | break; | |
758 | ||
759 | /* | |
760 | * Clear other state variables... | |
761 | */ | |
762 | ||
763 | httpClearFields(HTTP(con)); | |
764 | ||
765 | con->http.activity = time(NULL); | |
766 | con->http.version = HTTP_1_0; | |
767 | con->http.keep_alive = HTTP_KEEPALIVE_OFF; | |
768 | con->http.data_encoding = HTTP_ENCODE_LENGTH; | |
769 | con->http.data_remaining = 0; | |
770 | con->http._data_remaining = 0; | |
771 | con->operation = HTTP_WAITING; | |
772 | con->bytes = 0; | |
773 | con->file = -1; | |
774 | con->file_ready = 0; | |
775 | con->pipe_pid = 0; | |
776 | con->username[0] = '\0'; | |
777 | con->password[0] = '\0'; | |
778 | con->uri[0] = '\0'; | |
779 | ||
780 | cupsdClearString(&con->command); | |
781 | cupsdClearString(&con->options); | |
782 | ||
783 | if (con->language != NULL) | |
784 | { | |
785 | cupsLangFree(con->language); | |
786 | con->language = NULL; | |
787 | } | |
788 | ||
789 | /* | |
790 | * Grab the request line... | |
791 | */ | |
792 | ||
793 | switch (sscanf(line, "%63s%1023s%63s", operation, con->uri, version)) | |
794 | { | |
795 | case 1 : | |
796 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
797 | "Bad request line \"%s\" from %s!", line, | |
798 | con->http.hostname); | |
799 | cupsdSendError(con, HTTP_BAD_REQUEST); | |
800 | return (cupsdCloseClient(con)); | |
801 | case 2 : | |
802 | con->http.version = HTTP_0_9; | |
803 | break; | |
804 | case 3 : | |
805 | if (sscanf(version, "HTTP/%d.%d", &major, &minor) != 2) | |
806 | { | |
807 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
808 | "Bad request line \"%s\" from %s!", line, | |
809 | con->http.hostname); | |
810 | cupsdSendError(con, HTTP_BAD_REQUEST); | |
811 | return (cupsdCloseClient(con)); | |
812 | } | |
813 | ||
814 | if (major < 2) | |
815 | { | |
816 | con->http.version = (http_version_t)(major * 100 + minor); | |
817 | if (con->http.version == HTTP_1_1 && KeepAlive) | |
818 | con->http.keep_alive = HTTP_KEEPALIVE_ON; | |
819 | else | |
820 | con->http.keep_alive = HTTP_KEEPALIVE_OFF; | |
821 | } | |
822 | else | |
823 | { | |
824 | cupsdSendError(con, HTTP_NOT_SUPPORTED); | |
825 | return (cupsdCloseClient(con)); | |
826 | } | |
827 | break; | |
828 | } | |
829 | ||
830 | /* | |
831 | * Handle full URLs in the request line... | |
832 | */ | |
833 | ||
834 | if (strcmp(con->uri, "*")) | |
835 | { | |
836 | char method[HTTP_MAX_URI], /* Method/scheme */ | |
837 | userpass[HTTP_MAX_URI], /* Username:password */ | |
838 | hostname[HTTP_MAX_URI], /* Hostname */ | |
839 | resource[HTTP_MAX_URI]; /* Resource path */ | |
840 | int port; /* Port number */ | |
841 | ||
842 | ||
843 | /* | |
844 | * Separate the URI into its components... | |
845 | */ | |
846 | ||
a4d04587 | 847 | httpSeparateURI(HTTP_URI_CODING_MOST, con->uri, |
848 | method, sizeof(method), | |
ef416fc2 | 849 | userpass, sizeof(userpass), |
850 | hostname, sizeof(hostname), &port, | |
851 | resource, sizeof(resource)); | |
852 | ||
853 | /* | |
854 | * Only allow URIs with the servername, localhost, or an IP | |
855 | * address... | |
856 | */ | |
857 | ||
858 | if (strcmp(method, "file") && | |
859 | strcasecmp(hostname, ServerName) && | |
860 | strcasecmp(hostname, "localhost") && | |
861 | !isdigit(hostname[0])) | |
862 | { | |
863 | /* | |
864 | * Nope, we don't do proxies... | |
865 | */ | |
866 | ||
867 | cupsdLogMessage(CUPSD_LOG_ERROR, "Bad URI \"%s\" in request!", | |
868 | con->uri); | |
869 | cupsdSendError(con, HTTP_METHOD_NOT_ALLOWED); | |
870 | return (cupsdCloseClient(con)); | |
871 | } | |
872 | ||
873 | /* | |
874 | * Copy the resource portion back into the URI; both resource and | |
875 | * con->uri are HTTP_MAX_URI bytes in size... | |
876 | */ | |
877 | ||
878 | strcpy(con->uri, resource); | |
879 | } | |
880 | ||
881 | /* | |
882 | * Process the request... | |
883 | */ | |
884 | ||
885 | if (!strcmp(operation, "GET")) | |
886 | con->http.state = HTTP_GET; | |
887 | else if (!strcmp(operation, "PUT")) | |
888 | con->http.state = HTTP_PUT; | |
889 | else if (!strcmp(operation, "POST")) | |
890 | con->http.state = HTTP_POST; | |
891 | else if (!strcmp(operation, "DELETE")) | |
892 | con->http.state = HTTP_DELETE; | |
893 | else if (!strcmp(operation, "TRACE")) | |
894 | con->http.state = HTTP_TRACE; | |
895 | else if (!strcmp(operation, "OPTIONS")) | |
896 | con->http.state = HTTP_OPTIONS; | |
897 | else if (!strcmp(operation, "HEAD")) | |
898 | con->http.state = HTTP_HEAD; | |
899 | else | |
900 | { | |
901 | cupsdLogMessage(CUPSD_LOG_ERROR, "Bad operation \"%s\"!", operation); | |
902 | cupsdSendError(con, HTTP_BAD_REQUEST); | |
903 | return (cupsdCloseClient(con)); | |
904 | } | |
905 | ||
906 | con->start = time(NULL); | |
907 | con->operation = con->http.state; | |
908 | ||
89d46774 | 909 | cupsdLogMessage(CUPSD_LOG_DEBUG, "cupsdReadClient: %d %s %s HTTP/%d.%d", |
910 | con->http.fd, operation, con->uri, | |
ef416fc2 | 911 | con->http.version / 100, con->http.version % 100); |
912 | ||
913 | con->http.status = HTTP_OK; | |
914 | ||
915 | case HTTP_OPTIONS : | |
916 | case HTTP_DELETE : | |
917 | case HTTP_GET : | |
918 | case HTTP_HEAD : | |
919 | case HTTP_POST : | |
920 | case HTTP_PUT : | |
921 | case HTTP_TRACE : | |
922 | /* | |
923 | * Parse incoming parameters until the status changes... | |
924 | */ | |
925 | ||
926 | status = httpUpdate(HTTP(con)); | |
927 | ||
928 | if (status != HTTP_OK && status != HTTP_CONTINUE) | |
929 | { | |
930 | cupsdSendError(con, HTTP_BAD_REQUEST); | |
931 | return (cupsdCloseClient(con)); | |
932 | } | |
933 | break; | |
934 | ||
935 | default : | |
936 | break; /* Anti-compiler-warning-code */ | |
937 | } | |
938 | ||
939 | /* | |
940 | * Handle new transfers... | |
941 | */ | |
942 | ||
943 | if (status == HTTP_OK) | |
944 | { | |
945 | if (con->http.fields[HTTP_FIELD_ACCEPT_LANGUAGE][0]) | |
946 | { | |
947 | /* | |
948 | * Figure out the locale from the Accept-Language and Content-Type | |
949 | * fields... | |
950 | */ | |
951 | ||
952 | if ((ptr = strchr(con->http.fields[HTTP_FIELD_ACCEPT_LANGUAGE], ',')) != NULL) | |
953 | *ptr = '\0'; | |
954 | ||
955 | if ((ptr = strchr(con->http.fields[HTTP_FIELD_ACCEPT_LANGUAGE], ';')) != NULL) | |
956 | *ptr = '\0'; | |
957 | ||
958 | if ((ptr = strstr(con->http.fields[HTTP_FIELD_CONTENT_TYPE], "charset=")) != NULL) | |
959 | { | |
960 | /* | |
961 | * Combine language and charset, and trim any extra params in the | |
962 | * content-type. | |
963 | */ | |
964 | ||
965 | snprintf(locale, sizeof(locale), "%s.%s", | |
966 | con->http.fields[HTTP_FIELD_ACCEPT_LANGUAGE], ptr + 8); | |
967 | ||
968 | if ((ptr = strchr(locale, ',')) != NULL) | |
969 | *ptr = '\0'; | |
970 | } | |
971 | else | |
972 | snprintf(locale, sizeof(locale), "%s.%s", | |
973 | con->http.fields[HTTP_FIELD_ACCEPT_LANGUAGE], DefaultCharset); | |
d6ae789d | 974 | |
89d46774 | 975 | cupsdLogMessage(CUPSD_LOG_DEBUG, |
976 | "cupsdReadClient: %d Browser asked for language \"%s\"...", | |
977 | con->http.fd, locale); | |
978 | ||
ef416fc2 | 979 | con->language = cupsLangGet(locale); |
980 | } | |
981 | else | |
982 | con->language = cupsLangGet(DefaultLocale); | |
983 | ||
984 | cupsdAuthorize(con); | |
985 | ||
986 | if (!strncmp(con->http.fields[HTTP_FIELD_CONNECTION], "Keep-Alive", 10) && | |
987 | KeepAlive) | |
988 | con->http.keep_alive = HTTP_KEEPALIVE_ON; | |
989 | ||
990 | if (!con->http.fields[HTTP_FIELD_HOST][0] && | |
991 | con->http.version >= HTTP_1_1) | |
992 | { | |
993 | /* | |
994 | * HTTP/1.1 and higher require the "Host:" field... | |
995 | */ | |
996 | ||
997 | if (!cupsdSendError(con, HTTP_BAD_REQUEST)) | |
998 | return (cupsdCloseClient(con)); | |
999 | } | |
1000 | else if (con->operation == HTTP_OPTIONS) | |
1001 | { | |
1002 | /* | |
1003 | * Do OPTIONS command... | |
1004 | */ | |
1005 | ||
1006 | if (con->best && con->best->type != AUTH_NONE) | |
1007 | { | |
1008 | if (!cupsdSendHeader(con, HTTP_UNAUTHORIZED, NULL)) | |
1009 | return (cupsdCloseClient(con)); | |
1010 | } | |
1011 | ||
1012 | if (!strcasecmp(con->http.fields[HTTP_FIELD_CONNECTION], "Upgrade") && | |
1013 | con->http.tls == NULL) | |
1014 | { | |
1015 | #ifdef HAVE_SSL | |
1016 | /* | |
1017 | * Do encryption stuff... | |
1018 | */ | |
1019 | ||
1020 | if (!cupsdSendHeader(con, HTTP_SWITCHING_PROTOCOLS, NULL)) | |
1021 | return (cupsdCloseClient(con)); | |
1022 | ||
1023 | httpPrintf(HTTP(con), "Connection: Upgrade\r\n"); | |
1024 | httpPrintf(HTTP(con), "Upgrade: TLS/1.0,HTTP/1.1\r\n"); | |
1025 | httpPrintf(HTTP(con), "Content-Length: 0\r\n"); | |
1026 | httpPrintf(HTTP(con), "\r\n"); | |
1027 | ||
e1d6a774 | 1028 | encrypt_client(con); |
ef416fc2 | 1029 | #else |
1030 | if (!cupsdSendError(con, HTTP_NOT_IMPLEMENTED)) | |
1031 | return (cupsdCloseClient(con)); | |
1032 | #endif /* HAVE_SSL */ | |
1033 | } | |
1034 | ||
ef416fc2 | 1035 | if (!cupsdSendHeader(con, HTTP_OK, NULL)) |
1036 | return (cupsdCloseClient(con)); | |
1037 | ||
1038 | httpPrintf(HTTP(con), "Allow: GET, HEAD, OPTIONS, POST, PUT\r\n"); | |
1039 | httpPrintf(HTTP(con), "Content-Length: 0\r\n"); | |
1040 | httpPrintf(HTTP(con), "\r\n"); | |
1041 | } | |
1042 | else if (!is_path_absolute(con->uri)) | |
1043 | { | |
1044 | /* | |
1045 | * Protect against malicious users! | |
1046 | */ | |
1047 | ||
1048 | if (!cupsdSendError(con, HTTP_FORBIDDEN)) | |
1049 | return (cupsdCloseClient(con)); | |
1050 | } | |
1051 | else | |
1052 | { | |
1053 | if (!strcasecmp(con->http.fields[HTTP_FIELD_CONNECTION], "Upgrade") && | |
1054 | con->http.tls == NULL) | |
1055 | { | |
1056 | #ifdef HAVE_SSL | |
1057 | /* | |
1058 | * Do encryption stuff... | |
1059 | */ | |
1060 | ||
1061 | if (!cupsdSendHeader(con, HTTP_SWITCHING_PROTOCOLS, NULL)) | |
1062 | return (cupsdCloseClient(con)); | |
1063 | ||
1064 | httpPrintf(HTTP(con), "Connection: Upgrade\r\n"); | |
1065 | httpPrintf(HTTP(con), "Upgrade: TLS/1.0,HTTP/1.1\r\n"); | |
1066 | httpPrintf(HTTP(con), "Content-Length: 0\r\n"); | |
1067 | httpPrintf(HTTP(con), "\r\n"); | |
1068 | ||
e1d6a774 | 1069 | encrypt_client(con); |
ef416fc2 | 1070 | #else |
1071 | if (!cupsdSendError(con, HTTP_NOT_IMPLEMENTED)) | |
1072 | return (cupsdCloseClient(con)); | |
1073 | #endif /* HAVE_SSL */ | |
1074 | } | |
1075 | ||
1076 | if ((status = cupsdIsAuthorized(con, NULL)) != HTTP_OK) | |
1077 | { | |
1078 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
1079 | "cupsdReadClient: Unauthorized request for %s...\n", | |
1080 | con->uri); | |
1081 | cupsdSendError(con, status); | |
1082 | return (cupsdCloseClient(con)); | |
1083 | } | |
1084 | ||
b423cd4c | 1085 | if (con->http.expect && |
1086 | (con->operation == HTTP_POST || con->operation == HTTP_PUT)) | |
ef416fc2 | 1087 | { |
b423cd4c | 1088 | if (con->http.expect == HTTP_CONTINUE) |
1089 | { | |
1090 | /* | |
1091 | * Send 100-continue header... | |
1092 | */ | |
1093 | ||
1094 | if (!cupsdSendHeader(con, HTTP_CONTINUE, NULL)) | |
1095 | return (cupsdCloseClient(con)); | |
1096 | } | |
1097 | else | |
1098 | { | |
1099 | /* | |
1100 | * Send 417-expectation-failed header... | |
1101 | */ | |
1102 | ||
1103 | if (!cupsdSendHeader(con, HTTP_EXPECTATION_FAILED, NULL)) | |
1104 | return (cupsdCloseClient(con)); | |
1105 | ||
1106 | httpPrintf(HTTP(con), "Content-Length: 0\r\n"); | |
1107 | httpPrintf(HTTP(con), "\r\n"); | |
1108 | } | |
ef416fc2 | 1109 | } |
1110 | ||
1111 | switch (con->http.state) | |
1112 | { | |
1113 | case HTTP_GET_SEND : | |
1114 | if (!strncmp(con->uri, "/printers/", 10) && | |
1115 | !strcmp(con->uri + strlen(con->uri) - 4, ".ppd")) | |
1116 | { | |
1117 | /* | |
1118 | * Send PPD file - get the real printer name since printer | |
1119 | * names are not case sensitive but filenames can be... | |
1120 | */ | |
1121 | ||
1122 | con->uri[strlen(con->uri) - 4] = '\0'; /* Drop ".ppd" */ | |
1123 | ||
1124 | if ((p = cupsdFindPrinter(con->uri + 10)) != NULL) | |
1125 | snprintf(con->uri, sizeof(con->uri), "/ppd/%s.ppd", p->name); | |
1126 | else | |
1127 | { | |
1128 | if (!cupsdSendError(con, HTTP_NOT_FOUND)) | |
1129 | return (cupsdCloseClient(con)); | |
1130 | ||
1131 | break; | |
1132 | } | |
1133 | } | |
1134 | ||
1135 | if ((!strncmp(con->uri, "/admin", 6) && | |
1136 | strncmp(con->uri, "/admin/conf/", 12) && | |
1137 | strncmp(con->uri, "/admin/log/", 11)) || | |
1138 | !strncmp(con->uri, "/printers", 9) || | |
1139 | !strncmp(con->uri, "/classes", 8) || | |
1140 | !strncmp(con->uri, "/help", 5) || | |
1141 | !strncmp(con->uri, "/jobs", 5)) | |
1142 | { | |
1143 | /* | |
1144 | * Send CGI output... | |
1145 | */ | |
1146 | ||
1147 | if (!strncmp(con->uri, "/admin", 6)) | |
1148 | { | |
1149 | cupsdSetStringf(&con->command, "%s/cgi-bin/admin.cgi", | |
1150 | ServerBin); | |
1151 | ||
b423cd4c | 1152 | cupsdSetString(&con->options, strchr(con->uri + 6, '?')); |
ef416fc2 | 1153 | } |
1154 | else if (!strncmp(con->uri, "/printers", 9)) | |
1155 | { | |
1156 | cupsdSetStringf(&con->command, "%s/cgi-bin/printers.cgi", | |
1157 | ServerBin); | |
b423cd4c | 1158 | |
1159 | if (con->uri[9] && con->uri[10]) | |
1160 | cupsdSetString(&con->options, con->uri + 9); | |
1161 | else | |
1162 | cupsdSetString(&con->options, NULL); | |
ef416fc2 | 1163 | } |
1164 | else if (!strncmp(con->uri, "/classes", 8)) | |
1165 | { | |
1166 | cupsdSetStringf(&con->command, "%s/cgi-bin/classes.cgi", | |
1167 | ServerBin); | |
b423cd4c | 1168 | |
1169 | if (con->uri[8] && con->uri[9]) | |
1170 | cupsdSetString(&con->options, con->uri + 8); | |
1171 | else | |
1172 | cupsdSetString(&con->options, NULL); | |
ef416fc2 | 1173 | } |
1174 | else if (!strncmp(con->uri, "/jobs", 5)) | |
1175 | { | |
1176 | cupsdSetStringf(&con->command, "%s/cgi-bin/jobs.cgi", | |
1177 | ServerBin); | |
b423cd4c | 1178 | |
1179 | if (con->uri[5] && con->uri[6]) | |
1180 | cupsdSetString(&con->options, con->uri + 5); | |
1181 | else | |
1182 | cupsdSetString(&con->options, NULL); | |
ef416fc2 | 1183 | } |
1184 | else | |
1185 | { | |
1186 | cupsdSetStringf(&con->command, "%s/cgi-bin/help.cgi", | |
1187 | ServerBin); | |
ef416fc2 | 1188 | |
b423cd4c | 1189 | if (con->uri[5] && con->uri[6]) |
1190 | cupsdSetString(&con->options, con->uri + 5); | |
1191 | else | |
1192 | cupsdSetString(&con->options, NULL); | |
1193 | } | |
ef416fc2 | 1194 | |
1195 | if (!cupsdSendCommand(con, con->command, con->options, 0)) | |
1196 | { | |
1197 | if (!cupsdSendError(con, HTTP_NOT_FOUND)) | |
1198 | return (cupsdCloseClient(con)); | |
1199 | } | |
1200 | else | |
1201 | cupsdLogRequest(con, HTTP_OK); | |
1202 | ||
1203 | if (con->http.version <= HTTP_1_0) | |
1204 | con->http.keep_alive = HTTP_KEEPALIVE_OFF; | |
1205 | } | |
1206 | else if ((!strncmp(con->uri, "/admin/conf/", 12) && | |
1207 | (strchr(con->uri + 12, '/') || | |
1208 | strlen(con->uri) == 12)) || | |
1209 | (!strncmp(con->uri, "/admin/log/", 11) && | |
1210 | (strchr(con->uri + 11, '/') || | |
1211 | strlen(con->uri) == 11))) | |
1212 | { | |
1213 | /* | |
1214 | * GET can only be done to configuration files under | |
1215 | * /admin/conf... | |
1216 | */ | |
1217 | ||
1218 | if (!cupsdSendError(con, HTTP_FORBIDDEN)) | |
1219 | return (cupsdCloseClient(con)); | |
1220 | ||
1221 | break; | |
1222 | } | |
1223 | else | |
1224 | { | |
1225 | /* | |
1226 | * Serve a file... | |
1227 | */ | |
1228 | ||
1229 | if ((filename = get_file(con, &filestats, buf, | |
1230 | sizeof(buf))) == NULL) | |
1231 | { | |
1232 | if (!cupsdSendError(con, HTTP_NOT_FOUND)) | |
1233 | return (cupsdCloseClient(con)); | |
1234 | ||
1235 | break; | |
1236 | } | |
1237 | ||
4400e98d | 1238 | type = mimeFileType(MimeDatabase, filename, NULL, NULL); |
ef416fc2 | 1239 | |
e1d6a774 | 1240 | if (is_cgi(con, filename, &filestats, type)) |
ef416fc2 | 1241 | { |
1242 | /* | |
1243 | * Note: con->command and con->options were set by | |
e1d6a774 | 1244 | * is_cgi()... |
ef416fc2 | 1245 | */ |
1246 | ||
1247 | if (!cupsdSendCommand(con, con->command, con->options, 0)) | |
1248 | { | |
1249 | if (!cupsdSendError(con, HTTP_NOT_FOUND)) | |
1250 | return (cupsdCloseClient(con)); | |
1251 | } | |
1252 | else | |
1253 | cupsdLogRequest(con, HTTP_OK); | |
1254 | ||
1255 | if (con->http.version <= HTTP_1_0) | |
1256 | con->http.keep_alive = HTTP_KEEPALIVE_OFF; | |
1257 | break; | |
1258 | } | |
1259 | ||
1260 | if (!check_if_modified(con, &filestats)) | |
1261 | { | |
1262 | if (!cupsdSendError(con, HTTP_NOT_MODIFIED)) | |
1263 | return (cupsdCloseClient(con)); | |
1264 | } | |
1265 | else | |
1266 | { | |
1267 | if (type == NULL) | |
1268 | strcpy(line, "text/plain"); | |
1269 | else | |
1270 | snprintf(line, sizeof(line), "%s/%s", type->super, type->type); | |
1271 | ||
a74454a7 | 1272 | if (!write_file(con, HTTP_OK, filename, line, &filestats)) |
ef416fc2 | 1273 | return (cupsdCloseClient(con)); |
1274 | } | |
1275 | } | |
1276 | break; | |
1277 | ||
1278 | case HTTP_POST_RECV : | |
1279 | /* | |
1280 | * See if the POST request includes a Content-Length field, and if | |
1281 | * so check the length against any limits that are set... | |
1282 | */ | |
1283 | ||
1284 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "POST %s", con->uri); | |
1285 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "CONTENT_TYPE = %s", | |
1286 | con->http.fields[HTTP_FIELD_CONTENT_TYPE]); | |
1287 | ||
1288 | if (con->http.fields[HTTP_FIELD_CONTENT_LENGTH][0] && | |
1289 | MaxRequestSize > 0 && | |
1290 | con->http.data_remaining > MaxRequestSize) | |
1291 | { | |
1292 | /* | |
1293 | * Request too large... | |
1294 | */ | |
1295 | ||
1296 | if (!cupsdSendError(con, HTTP_REQUEST_TOO_LARGE)) | |
1297 | return (cupsdCloseClient(con)); | |
1298 | ||
1299 | break; | |
1300 | } | |
1301 | else if (con->http.data_remaining < 0) | |
1302 | { | |
1303 | /* | |
1304 | * Negative content lengths are invalid! | |
1305 | */ | |
1306 | ||
1307 | if (!cupsdSendError(con, HTTP_BAD_REQUEST)) | |
1308 | return (cupsdCloseClient(con)); | |
1309 | ||
1310 | break; | |
1311 | } | |
1312 | ||
1313 | /* | |
1314 | * See what kind of POST request this is; for IPP requests the | |
1315 | * content-type field will be "application/ipp"... | |
1316 | */ | |
1317 | ||
1318 | if (!strcmp(con->http.fields[HTTP_FIELD_CONTENT_TYPE], | |
1319 | "application/ipp")) | |
1320 | con->request = ippNew(); | |
1321 | else if ((!strncmp(con->uri, "/admin", 6) && | |
1322 | strncmp(con->uri, "/admin/conf/", 12) && | |
1323 | strncmp(con->uri, "/admin/log/", 11)) || | |
1324 | !strncmp(con->uri, "/printers", 9) || | |
1325 | !strncmp(con->uri, "/classes", 8) || | |
1326 | !strncmp(con->uri, "/help", 5) || | |
1327 | !strncmp(con->uri, "/jobs", 5)) | |
1328 | { | |
1329 | /* | |
1330 | * CGI request... | |
1331 | */ | |
1332 | ||
1333 | if (!strncmp(con->uri, "/admin", 6)) | |
1334 | { | |
1335 | cupsdSetStringf(&con->command, "%s/cgi-bin/admin.cgi", | |
1336 | ServerBin); | |
1337 | ||
b423cd4c | 1338 | cupsdSetString(&con->options, strchr(con->uri + 6, '?')); |
ef416fc2 | 1339 | } |
1340 | else if (!strncmp(con->uri, "/printers", 9)) | |
1341 | { | |
1342 | cupsdSetStringf(&con->command, "%s/cgi-bin/printers.cgi", | |
1343 | ServerBin); | |
b423cd4c | 1344 | |
1345 | if (con->uri[9] && con->uri[10]) | |
1346 | cupsdSetString(&con->options, con->uri + 9); | |
1347 | else | |
1348 | cupsdSetString(&con->options, NULL); | |
ef416fc2 | 1349 | } |
1350 | else if (!strncmp(con->uri, "/classes", 8)) | |
1351 | { | |
1352 | cupsdSetStringf(&con->command, "%s/cgi-bin/classes.cgi", | |
1353 | ServerBin); | |
b423cd4c | 1354 | |
1355 | if (con->uri[8] && con->uri[9]) | |
1356 | cupsdSetString(&con->options, con->uri + 8); | |
1357 | else | |
1358 | cupsdSetString(&con->options, NULL); | |
ef416fc2 | 1359 | } |
1360 | else if (!strncmp(con->uri, "/jobs", 5)) | |
1361 | { | |
1362 | cupsdSetStringf(&con->command, "%s/cgi-bin/jobs.cgi", | |
1363 | ServerBin); | |
b423cd4c | 1364 | |
1365 | if (con->uri[5] && con->uri[6]) | |
1366 | cupsdSetString(&con->options, con->uri + 5); | |
1367 | else | |
1368 | cupsdSetString(&con->options, NULL); | |
ef416fc2 | 1369 | } |
1370 | else | |
1371 | { | |
1372 | cupsdSetStringf(&con->command, "%s/cgi-bin/help.cgi", | |
1373 | ServerBin); | |
ef416fc2 | 1374 | |
b423cd4c | 1375 | if (con->uri[5] && con->uri[6]) |
1376 | cupsdSetString(&con->options, con->uri + 5); | |
1377 | else | |
1378 | cupsdSetString(&con->options, NULL); | |
1379 | } | |
ef416fc2 | 1380 | |
1381 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
b423cd4c | 1382 | "cupsdReadClient: %d command=\"%s\", " |
1383 | "options = \"%s\"", | |
f301802f | 1384 | con->http.fd, con->command, |
1385 | con->options ? con->options : "(null)"); | |
ef416fc2 | 1386 | |
1387 | if (con->http.version <= HTTP_1_0) | |
1388 | con->http.keep_alive = HTTP_KEEPALIVE_OFF; | |
1389 | } | |
1390 | else | |
1391 | { | |
1392 | /* | |
1393 | * POST to a file... | |
1394 | */ | |
1395 | ||
1396 | if ((filename = get_file(con, &filestats, buf, | |
1397 | sizeof(buf))) == NULL) | |
1398 | { | |
1399 | if (!cupsdSendError(con, HTTP_NOT_FOUND)) | |
1400 | return (cupsdCloseClient(con)); | |
1401 | ||
1402 | break; | |
1403 | } | |
1404 | ||
4400e98d | 1405 | type = mimeFileType(MimeDatabase, filename, NULL, NULL); |
ef416fc2 | 1406 | |
e1d6a774 | 1407 | if (!is_cgi(con, filename, &filestats, type)) |
ef416fc2 | 1408 | { |
1409 | /* | |
1410 | * Only POST to CGI's... | |
1411 | */ | |
1412 | ||
1413 | if (!cupsdSendError(con, HTTP_UNAUTHORIZED)) | |
1414 | return (cupsdCloseClient(con)); | |
1415 | } | |
1416 | } | |
1417 | break; | |
1418 | ||
1419 | case HTTP_PUT_RECV : | |
1420 | /* | |
1421 | * Validate the resource name... | |
1422 | */ | |
1423 | ||
1424 | if (strncmp(con->uri, "/admin/conf/", 12) || | |
1425 | strchr(con->uri + 12, '/') || | |
1426 | strlen(con->uri) == 12) | |
1427 | { | |
1428 | /* | |
1429 | * PUT can only be done to configuration files under | |
1430 | * /admin/conf... | |
1431 | */ | |
1432 | ||
1433 | if (!cupsdSendError(con, HTTP_FORBIDDEN)) | |
1434 | return (cupsdCloseClient(con)); | |
1435 | ||
1436 | break; | |
1437 | } | |
1438 | ||
1439 | /* | |
1440 | * See if the PUT request includes a Content-Length field, and if | |
1441 | * so check the length against any limits that are set... | |
1442 | */ | |
1443 | ||
1444 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "PUT %s", con->uri); | |
1445 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "CONTENT_TYPE = %s", | |
1446 | con->http.fields[HTTP_FIELD_CONTENT_TYPE]); | |
1447 | ||
1448 | if (con->http.fields[HTTP_FIELD_CONTENT_LENGTH][0] && | |
1449 | MaxRequestSize > 0 && | |
1450 | con->http.data_remaining > MaxRequestSize) | |
1451 | { | |
1452 | /* | |
1453 | * Request too large... | |
1454 | */ | |
1455 | ||
1456 | if (!cupsdSendError(con, HTTP_REQUEST_TOO_LARGE)) | |
1457 | return (cupsdCloseClient(con)); | |
1458 | ||
1459 | break; | |
1460 | } | |
1461 | else if (con->http.data_remaining < 0) | |
1462 | { | |
1463 | /* | |
1464 | * Negative content lengths are invalid! | |
1465 | */ | |
1466 | ||
1467 | if (!cupsdSendError(con, HTTP_BAD_REQUEST)) | |
1468 | return (cupsdCloseClient(con)); | |
1469 | ||
1470 | break; | |
1471 | } | |
1472 | ||
1473 | /* | |
1474 | * Open a temporary file to hold the request... | |
1475 | */ | |
1476 | ||
1477 | cupsdSetStringf(&con->filename, "%s/%08x", RequestRoot, | |
1478 | request_id ++); | |
1479 | con->file = open(con->filename, O_WRONLY | O_CREAT | O_TRUNC, 0640); | |
1480 | ||
1481 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
1482 | "cupsdReadClient: %d REQUEST %s=%d", con->http.fd, | |
1483 | con->filename, con->file); | |
1484 | ||
1485 | if (con->file < 0) | |
1486 | { | |
1487 | if (!cupsdSendError(con, HTTP_REQUEST_TOO_LARGE)) | |
1488 | return (cupsdCloseClient(con)); | |
1489 | } | |
1490 | ||
1491 | fchmod(con->file, 0640); | |
1492 | fchown(con->file, RunUser, Group); | |
1493 | fcntl(con->file, F_SETFD, fcntl(con->file, F_GETFD) | FD_CLOEXEC); | |
1494 | break; | |
1495 | ||
1496 | case HTTP_DELETE : | |
1497 | case HTTP_TRACE : | |
1498 | cupsdSendError(con, HTTP_NOT_IMPLEMENTED); | |
1499 | return (cupsdCloseClient(con)); | |
1500 | ||
1501 | case HTTP_HEAD : | |
1502 | if (!strncmp(con->uri, "/printers/", 10) && | |
1503 | !strcmp(con->uri + strlen(con->uri) - 4, ".ppd")) | |
1504 | { | |
1505 | /* | |
1506 | * Send PPD file - get the real printer name since printer | |
1507 | * names are not case sensitive but filenames can be... | |
1508 | */ | |
1509 | ||
1510 | con->uri[strlen(con->uri) - 4] = '\0'; /* Drop ".ppd" */ | |
1511 | ||
1512 | if ((p = cupsdFindPrinter(con->uri + 10)) != NULL) | |
1513 | snprintf(con->uri, sizeof(con->uri), "/ppd/%s.ppd", p->name); | |
1514 | else | |
1515 | { | |
1516 | if (!cupsdSendError(con, HTTP_NOT_FOUND)) | |
1517 | return (cupsdCloseClient(con)); | |
1518 | ||
1519 | break; | |
1520 | } | |
1521 | } | |
1522 | ||
1523 | if ((!strncmp(con->uri, "/admin", 6) && | |
1524 | strncmp(con->uri, "/admin/conf/", 12) && | |
1525 | strncmp(con->uri, "/admin/log/", 11)) || | |
1526 | !strncmp(con->uri, "/printers", 9) || | |
1527 | !strncmp(con->uri, "/classes", 8) || | |
1528 | !strncmp(con->uri, "/help", 5) || | |
1529 | !strncmp(con->uri, "/jobs", 5)) | |
1530 | { | |
1531 | /* | |
1532 | * CGI output... | |
1533 | */ | |
1534 | ||
1535 | if (!cupsdSendHeader(con, HTTP_OK, "text/html")) | |
1536 | return (cupsdCloseClient(con)); | |
1537 | ||
1538 | if (httpPrintf(HTTP(con), "\r\n") < 0) | |
1539 | return (cupsdCloseClient(con)); | |
1540 | ||
1541 | cupsdLogRequest(con, HTTP_OK); | |
1542 | } | |
1543 | else if ((!strncmp(con->uri, "/admin/conf/", 12) && | |
1544 | (strchr(con->uri + 12, '/') || | |
1545 | strlen(con->uri) == 12)) || | |
1546 | (!strncmp(con->uri, "/admin/log/", 11) && | |
1547 | (strchr(con->uri + 11, '/') || | |
1548 | strlen(con->uri) == 11))) | |
1549 | { | |
1550 | /* | |
1551 | * HEAD can only be done to configuration files under | |
1552 | * /admin/conf... | |
1553 | */ | |
1554 | ||
1555 | if (!cupsdSendError(con, HTTP_FORBIDDEN)) | |
1556 | return (cupsdCloseClient(con)); | |
1557 | ||
1558 | break; | |
1559 | } | |
1560 | else if ((filename = get_file(con, &filestats, buf, | |
1561 | sizeof(buf))) == NULL) | |
1562 | { | |
1563 | if (!cupsdSendHeader(con, HTTP_NOT_FOUND, "text/html")) | |
1564 | return (cupsdCloseClient(con)); | |
1565 | ||
1566 | cupsdLogRequest(con, HTTP_NOT_FOUND); | |
1567 | } | |
1568 | else if (!check_if_modified(con, &filestats)) | |
1569 | { | |
1570 | if (!cupsdSendError(con, HTTP_NOT_MODIFIED)) | |
1571 | return (cupsdCloseClient(con)); | |
1572 | ||
1573 | cupsdLogRequest(con, HTTP_NOT_MODIFIED); | |
1574 | } | |
1575 | else | |
1576 | { | |
1577 | /* | |
1578 | * Serve a file... | |
1579 | */ | |
1580 | ||
4400e98d | 1581 | type = mimeFileType(MimeDatabase, filename, NULL, NULL); |
ef416fc2 | 1582 | if (type == NULL) |
1583 | strcpy(line, "text/plain"); | |
1584 | else | |
1585 | snprintf(line, sizeof(line), "%s/%s", type->super, type->type); | |
1586 | ||
1587 | if (!cupsdSendHeader(con, HTTP_OK, line)) | |
1588 | return (cupsdCloseClient(con)); | |
1589 | ||
1590 | if (httpPrintf(HTTP(con), "Last-Modified: %s\r\n", | |
1591 | httpGetDateString(filestats.st_mtime)) < 0) | |
1592 | return (cupsdCloseClient(con)); | |
1593 | ||
1594 | if (httpPrintf(HTTP(con), "Content-Length: %lu\r\n", | |
1595 | (unsigned long)filestats.st_size) < 0) | |
1596 | return (cupsdCloseClient(con)); | |
1597 | ||
1598 | cupsdLogRequest(con, HTTP_OK); | |
1599 | } | |
1600 | ||
1601 | if (httpPrintf(HTTP(con), "\r\n") < 0) | |
1602 | return (cupsdCloseClient(con)); | |
1603 | ||
1604 | con->http.state = HTTP_WAITING; | |
1605 | break; | |
1606 | ||
1607 | default : | |
1608 | break; /* Anti-compiler-warning-code */ | |
1609 | } | |
1610 | } | |
1611 | } | |
1612 | ||
1613 | /* | |
1614 | * Handle any incoming data... | |
1615 | */ | |
1616 | ||
1617 | switch (con->http.state) | |
1618 | { | |
1619 | case HTTP_PUT_RECV : | |
1620 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
1621 | "cupsdReadClient: %d con->data_encoding=HTTP_ENCODE_%s, " | |
1622 | "con->data_remaining=" CUPS_LLFMT ", con->file=%d", | |
1623 | con->http.fd, | |
1624 | con->http.data_encoding == HTTP_ENCODE_CHUNKED ? | |
1625 | "CHUNKED" : "LENGTH", | |
1626 | CUPS_LLCAST con->http.data_remaining, con->file); | |
1627 | ||
a4d04587 | 1628 | if ((bytes = httpRead2(HTTP(con), line, sizeof(line))) < 0) |
ef416fc2 | 1629 | return (cupsdCloseClient(con)); |
1630 | else if (bytes > 0) | |
1631 | { | |
1632 | con->bytes += bytes; | |
1633 | ||
1634 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
1635 | "cupsdReadClient: %d writing %d bytes to %d", | |
1636 | con->http.fd, bytes, con->file); | |
1637 | ||
1638 | if (write(con->file, line, bytes) < bytes) | |
1639 | { | |
1640 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
1641 | "cupsdReadClient: Unable to write %d bytes to %s: %s", | |
1642 | bytes, con->filename, strerror(errno)); | |
1643 | ||
1644 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
1645 | "cupsdReadClient: Closing data file %d...", | |
1646 | con->file); | |
1647 | ||
1648 | close(con->file); | |
1649 | con->file = -1; | |
1650 | unlink(con->filename); | |
1651 | cupsdClearString(&con->filename); | |
1652 | ||
1653 | if (!cupsdSendError(con, HTTP_REQUEST_TOO_LARGE)) | |
1654 | return (cupsdCloseClient(con)); | |
1655 | } | |
1656 | } | |
1657 | ||
1658 | if (con->http.state == HTTP_WAITING) | |
1659 | { | |
1660 | /* | |
1661 | * End of file, see how big it is... | |
1662 | */ | |
1663 | ||
1664 | fstat(con->file, &filestats); | |
1665 | ||
1666 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
1667 | "cupsdReadClient: %d Closing data file %d, size=" | |
1668 | CUPS_LLFMT ".", | |
1669 | con->http.fd, con->file, | |
1670 | CUPS_LLCAST filestats.st_size); | |
1671 | ||
1672 | close(con->file); | |
1673 | con->file = -1; | |
1674 | ||
1675 | if (filestats.st_size > MaxRequestSize && | |
1676 | MaxRequestSize > 0) | |
1677 | { | |
1678 | /* | |
1679 | * Request is too big; remove it and send an error... | |
1680 | */ | |
1681 | ||
1682 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
1683 | "cupsdReadClient: %d Removing temp file %s", | |
1684 | con->http.fd, con->filename); | |
1685 | unlink(con->filename); | |
1686 | cupsdClearString(&con->filename); | |
1687 | ||
1688 | if (!cupsdSendError(con, HTTP_REQUEST_TOO_LARGE)) | |
1689 | return (cupsdCloseClient(con)); | |
1690 | } | |
1691 | ||
1692 | /* | |
1693 | * Install the configuration file... | |
1694 | */ | |
1695 | ||
1696 | status = install_conf_file(con); | |
1697 | ||
1698 | /* | |
1699 | * Return the status to the client... | |
1700 | */ | |
1701 | ||
1702 | if (!cupsdSendError(con, status)) | |
1703 | return (cupsdCloseClient(con)); | |
1704 | } | |
1705 | break; | |
1706 | ||
1707 | case HTTP_POST_RECV : | |
1708 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
1709 | "cupsdReadClient: %d con->data_encoding=HTTP_ENCODE_" | |
1710 | "%s, con->data_remaining=" CUPS_LLFMT ", con->file=%d", | |
1711 | con->http.fd, | |
1712 | con->http.data_encoding == HTTP_ENCODE_CHUNKED ? | |
1713 | "CHUNKED" : "LENGTH", | |
1714 | CUPS_LLCAST con->http.data_remaining, con->file); | |
1715 | ||
1716 | if (con->request != NULL) | |
1717 | { | |
1718 | /* | |
1719 | * Grab any request data from the connection... | |
1720 | */ | |
1721 | ||
1722 | if ((ipp_state = ippRead(&(con->http), con->request)) == IPP_ERROR) | |
1723 | { | |
1724 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
1725 | "cupsdReadClient: %d IPP Read Error!", | |
1726 | con->http.fd); | |
1727 | ||
1728 | cupsdSendError(con, HTTP_BAD_REQUEST); | |
1729 | return (cupsdCloseClient(con)); | |
1730 | } | |
1731 | else if (ipp_state != IPP_DATA) | |
1732 | { | |
1733 | if (con->http.state == HTTP_POST_SEND) | |
1734 | { | |
1735 | cupsdSendError(con, HTTP_BAD_REQUEST); | |
1736 | return (cupsdCloseClient(con)); | |
1737 | } | |
1738 | ||
1739 | break; | |
1740 | } | |
1741 | else | |
1742 | con->bytes += ippLength(con->request); | |
1743 | } | |
1744 | ||
1745 | if (con->file < 0 && con->http.state != HTTP_POST_SEND) | |
1746 | { | |
1747 | /* | |
1748 | * Create a file as needed for the request data... | |
1749 | */ | |
1750 | ||
1751 | cupsdSetStringf(&con->filename, "%s/%08x", RequestRoot, request_id ++); | |
1752 | con->file = open(con->filename, O_WRONLY | O_CREAT | O_TRUNC, 0640); | |
1753 | ||
1754 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdReadClient: %d REQUEST %s=%d", con->http.fd, | |
1755 | con->filename, con->file); | |
1756 | ||
1757 | if (con->file < 0) | |
1758 | { | |
1759 | if (!cupsdSendError(con, HTTP_REQUEST_TOO_LARGE)) | |
1760 | return (cupsdCloseClient(con)); | |
1761 | } | |
1762 | ||
1763 | fchmod(con->file, 0640); | |
1764 | fchown(con->file, RunUser, Group); | |
1765 | fcntl(con->file, F_SETFD, fcntl(con->file, F_GETFD) | FD_CLOEXEC); | |
1766 | } | |
1767 | ||
1768 | if (con->http.state != HTTP_POST_SEND) | |
1769 | { | |
a4d04587 | 1770 | if ((bytes = httpRead2(HTTP(con), line, sizeof(line))) < 0) |
ef416fc2 | 1771 | return (cupsdCloseClient(con)); |
1772 | else if (bytes > 0) | |
1773 | { | |
1774 | con->bytes += bytes; | |
1775 | ||
1776 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
1777 | "cupsdReadClient: %d writing %d bytes to %d", | |
1778 | con->http.fd, bytes, con->file); | |
1779 | ||
1780 | if (write(con->file, line, bytes) < bytes) | |
1781 | { | |
1782 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
1783 | "cupsdReadClient: Unable to write %d bytes to %s: %s", | |
1784 | bytes, con->filename, strerror(errno)); | |
1785 | ||
1786 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
1787 | "cupsdReadClient: Closing file %d...", | |
1788 | con->file); | |
1789 | ||
1790 | close(con->file); | |
1791 | con->file = -1; | |
1792 | unlink(con->filename); | |
1793 | cupsdClearString(&con->filename); | |
1794 | ||
1795 | if (!cupsdSendError(con, HTTP_REQUEST_TOO_LARGE)) | |
1796 | return (cupsdCloseClient(con)); | |
1797 | } | |
1798 | } | |
1799 | else if (con->http.state == HTTP_POST_RECV) | |
1800 | return (1); /* ??? */ | |
1801 | else if (con->http.state != HTTP_POST_SEND) | |
1802 | return (cupsdCloseClient(con)); | |
1803 | } | |
1804 | ||
1805 | if (con->http.state == HTTP_POST_SEND) | |
1806 | { | |
1807 | if (con->file >= 0) | |
1808 | { | |
1809 | fstat(con->file, &filestats); | |
1810 | ||
1811 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
1812 | "cupsdReadClient: %d Closing data file %d, " | |
1813 | "size=" CUPS_LLFMT ".", | |
1814 | con->http.fd, con->file, | |
1815 | CUPS_LLCAST filestats.st_size); | |
1816 | ||
1817 | close(con->file); | |
1818 | con->file = -1; | |
1819 | ||
1820 | if (filestats.st_size > MaxRequestSize && | |
1821 | MaxRequestSize > 0) | |
1822 | { | |
1823 | /* | |
1824 | * Request is too big; remove it and send an error... | |
1825 | */ | |
1826 | ||
1827 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
1828 | "cupsdReadClient: %d Removing temp file %s", | |
1829 | con->http.fd, con->filename); | |
1830 | unlink(con->filename); | |
1831 | cupsdClearString(&con->filename); | |
1832 | ||
1833 | if (con->request) | |
1834 | { | |
1835 | /* | |
1836 | * Delete any IPP request data... | |
1837 | */ | |
1838 | ||
1839 | ippDelete(con->request); | |
1840 | con->request = NULL; | |
1841 | } | |
1842 | ||
1843 | if (!cupsdSendError(con, HTTP_REQUEST_TOO_LARGE)) | |
1844 | return (cupsdCloseClient(con)); | |
1845 | } | |
1846 | ||
1847 | if (con->command) | |
1848 | { | |
1849 | if (!cupsdSendCommand(con, con->command, con->options, 0)) | |
1850 | { | |
1851 | if (!cupsdSendError(con, HTTP_NOT_FOUND)) | |
1852 | return (cupsdCloseClient(con)); | |
1853 | } | |
1854 | else | |
1855 | cupsdLogRequest(con, HTTP_OK); | |
1856 | } | |
1857 | } | |
1858 | ||
1859 | if (con->request) | |
1860 | return (cupsdProcessIPPRequest(con)); | |
1861 | } | |
1862 | break; | |
1863 | ||
1864 | default : | |
1865 | break; /* Anti-compiler-warning-code */ | |
1866 | } | |
1867 | ||
1868 | if (!con->http.keep_alive && con->http.state == HTTP_WAITING) | |
1869 | return (cupsdCloseClient(con)); | |
1870 | else | |
1871 | return (1); | |
1872 | } | |
1873 | ||
1874 | ||
1875 | /* | |
1876 | * 'cupsdSendCommand()' - Send output from a command via HTTP. | |
1877 | */ | |
1878 | ||
1879 | int /* O - 1 on success, 0 on failure */ | |
1880 | cupsdSendCommand( | |
1881 | cupsd_client_t *con, /* I - Client connection */ | |
1882 | char *command, /* I - Command to run */ | |
1883 | char *options, /* I - Command-line options */ | |
1884 | int root) /* I - Run as root? */ | |
1885 | { | |
1886 | int fd; /* Standard input file descriptor */ | |
1887 | ||
1888 | ||
1889 | if (con->filename) | |
ed486911 | 1890 | { |
ef416fc2 | 1891 | fd = open(con->filename, O_RDONLY); |
ef416fc2 | 1892 | |
ed486911 | 1893 | if (fd < 0) |
1894 | { | |
1895 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
1896 | "cupsdSendCommand: %d Unable to open \"%s\" for reading: %s", | |
1897 | con->http.fd, con->filename ? con->filename : "/dev/null", | |
1898 | strerror(errno)); | |
1899 | return (0); | |
1900 | } | |
ef416fc2 | 1901 | |
ed486911 | 1902 | fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC); |
1903 | } | |
1904 | else | |
1905 | fd = -1; | |
ef416fc2 | 1906 | |
1907 | con->pipe_pid = pipe_command(con, fd, &(con->file), command, options, root); | |
1908 | ||
ed486911 | 1909 | if (fd >= 0) |
1910 | close(fd); | |
ef416fc2 | 1911 | |
1912 | cupsdLogMessage(CUPSD_LOG_INFO, "Started \"%s\" (pid=%d)", command, | |
1913 | con->pipe_pid); | |
1914 | ||
1915 | cupsdLogMessage(CUPSD_LOG_DEBUG, "cupsdSendCommand: %d file=%d", | |
1916 | con->http.fd, con->file); | |
1917 | ||
1918 | if (con->pipe_pid == 0) | |
1919 | return (0); | |
1920 | ||
1921 | fcntl(con->file, F_SETFD, fcntl(con->file, F_GETFD) | FD_CLOEXEC); | |
1922 | ||
1923 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
1924 | "cupsdSendCommand: Adding fd %d to InputSet...", con->file); | |
1925 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
1926 | "cupsdSendCommand: Adding fd %d to OutputSet...", | |
1927 | con->http.fd); | |
1928 | ||
1929 | FD_SET(con->file, InputSet); | |
1930 | FD_SET(con->http.fd, OutputSet); | |
1931 | ||
1932 | con->sent_header = 0; | |
1933 | con->file_ready = 0; | |
1934 | con->got_fields = 0; | |
1935 | con->field_col = 0; | |
1936 | ||
1937 | return (1); | |
1938 | } | |
1939 | ||
1940 | ||
1941 | /* | |
1942 | * 'cupsdSendError()' - Send an error message via HTTP. | |
1943 | */ | |
1944 | ||
1945 | int /* O - 1 if successful, 0 otherwise */ | |
1946 | cupsdSendError(cupsd_client_t *con, /* I - Connection */ | |
1947 | http_status_t code) /* I - Error code */ | |
1948 | { | |
4744bd90 | 1949 | #ifdef HAVE_SSL |
1950 | /* | |
1951 | * Force client to upgrade for authentication if that is how the | |
1952 | * server is configured... | |
1953 | */ | |
1954 | ||
1955 | if (code == HTTP_UNAUTHORIZED && | |
1956 | DefaultEncryption == HTTP_ENCRYPT_REQUIRED && | |
1957 | strcasecmp(con->http.hostname, "localhost") && | |
1958 | !con->http.tls) | |
1959 | { | |
1960 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
1961 | "cupsdSendError: Encryption before authentication!"); | |
1962 | code = HTTP_UPGRADE_REQUIRED; | |
1963 | } | |
1964 | #endif /* HAVE_SSL */ | |
1965 | ||
ef416fc2 | 1966 | /* |
1967 | * Put the request in the access_log file... | |
1968 | */ | |
1969 | ||
1970 | cupsdLogRequest(con, code); | |
1971 | ||
1972 | cupsdLogMessage(CUPSD_LOG_DEBUG, "cupsdSendError: %d code=%d (%s)", | |
1973 | con->http.fd, code, httpStatus(code)); | |
1974 | ||
1975 | /* | |
1976 | * To work around bugs in some proxies, don't use Keep-Alive for some | |
1977 | * error messages... | |
1978 | */ | |
1979 | ||
1980 | if (code >= HTTP_BAD_REQUEST) | |
1981 | con->http.keep_alive = HTTP_KEEPALIVE_OFF; | |
1982 | ||
1983 | /* | |
1984 | * Send an error message back to the client. If the error code is a | |
1985 | * 400 or 500 series, make sure the message contains some text, too! | |
1986 | */ | |
1987 | ||
1988 | if (!cupsdSendHeader(con, code, NULL)) | |
1989 | return (0); | |
1990 | ||
1991 | #ifdef HAVE_SSL | |
1992 | if (code == HTTP_UPGRADE_REQUIRED) | |
1993 | if (httpPrintf(HTTP(con), "Connection: Upgrade\r\n") < 0) | |
1994 | return (0); | |
1995 | ||
1996 | if (httpPrintf(HTTP(con), "Upgrade: TLS/1.0,HTTP/1.1\r\n") < 0) | |
1997 | return (0); | |
1998 | #endif /* HAVE_SSL */ | |
1999 | ||
2000 | if ((con->http.version >= HTTP_1_1 && !con->http.keep_alive) || | |
2001 | (code >= HTTP_BAD_REQUEST && code != HTTP_UPGRADE_REQUIRED)) | |
2002 | { | |
2003 | if (httpPrintf(HTTP(con), "Connection: close\r\n") < 0) | |
2004 | return (0); | |
2005 | } | |
2006 | ||
2007 | if (code >= HTTP_BAD_REQUEST) | |
2008 | { | |
2009 | /* | |
2010 | * Send a human-readable error message. | |
2011 | */ | |
2012 | ||
80ca4592 | 2013 | char message[4096], /* Message for user */ |
2014 | urltext[1024], /* URL redirection text */ | |
2015 | redirect[1024]; /* Redirection link */ | |
ef416fc2 | 2016 | const char *text; /* Status-specific text */ |
2017 | ||
80ca4592 | 2018 | |
2019 | redirect[0] = '\0'; | |
2020 | ||
ef416fc2 | 2021 | if (code == HTTP_UNAUTHORIZED) |
2022 | text = _cupsLangString(con->language, | |
2023 | _("Enter your username and password or the " | |
2024 | "root username and password to access this " | |
2025 | "page.")); | |
2026 | else if (code == HTTP_UPGRADE_REQUIRED) | |
80ca4592 | 2027 | { |
2028 | text = urltext; | |
2029 | ||
2030 | snprintf(urltext, sizeof(urltext), | |
2031 | _cupsLangString(con->language, | |
2032 | _("You must access this page using the URL " | |
2033 | "<A HREF=\"https://%s:%d%s\">" | |
2034 | "https://%s:%d%s</A>.")), | |
2035 | con->servername, con->serverport, con->uri, | |
2036 | con->servername, con->serverport, con->uri); | |
2037 | ||
2038 | snprintf(redirect, sizeof(redirect), | |
2039 | "<META HTTP-EQUIV=\"Refresh\" CONTENT=\"3;https://%s:%d%s\">\n", | |
2040 | con->servername, con->serverport, con->uri); | |
2041 | } | |
ef416fc2 | 2042 | else |
2043 | text = ""; | |
2044 | ||
2045 | snprintf(message, sizeof(message), | |
2046 | "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\" " | |
2047 | "\"http://www.w3.org/TR/REC-html40/loose.dtd\">\n" | |
2048 | "<HTML>\n" | |
2049 | "<HEAD>\n" | |
2050 | "\t<META HTTP-EQUIV=\"Content-Type\" " | |
2051 | "CONTENT=\"text/html; charset=utf-8\">\n" | |
2052 | "\t<TITLE>%d %s</TITLE>\n" | |
2053 | "\t<LINK REL=\"STYLESHEET\" TYPE=\"text/css\" " | |
2054 | "HREF=\"/cups.css\">\n" | |
80ca4592 | 2055 | "%s" |
ef416fc2 | 2056 | "</HEAD>\n" |
2057 | "<BODY>\n" | |
2058 | "<H1>%d %s</H1>\n" | |
2059 | "<P>%s</P>\n" | |
2060 | "</BODY>\n" | |
2061 | "</HTML>\n", | |
80ca4592 | 2062 | code, httpStatus(code), redirect, code, httpStatus(code), text); |
ef416fc2 | 2063 | |
2064 | if (httpPrintf(HTTP(con), "Content-Type: text/html; charset=utf-8\r\n") < 0) | |
2065 | return (0); | |
2066 | if (httpPrintf(HTTP(con), "Content-Length: %d\r\n", | |
2067 | (int)strlen(message)) < 0) | |
2068 | return (0); | |
2069 | if (httpPrintf(HTTP(con), "\r\n") < 0) | |
2070 | return (0); | |
2071 | if (httpPrintf(HTTP(con), "%s", message) < 0) | |
2072 | return (0); | |
2073 | } | |
2074 | else if (httpPrintf(HTTP(con), "\r\n") < 0) | |
2075 | return (0); | |
2076 | ||
2077 | con->http.state = HTTP_WAITING; | |
2078 | ||
2079 | return (1); | |
2080 | } | |
2081 | ||
2082 | ||
ef416fc2 | 2083 | /* |
2084 | * 'cupsdSendHeader()' - Send an HTTP request. | |
2085 | */ | |
2086 | ||
2087 | int /* O - 1 on success, 0 on failure */ | |
2088 | cupsdSendHeader(cupsd_client_t *con, /* I - Client to send to */ | |
2089 | http_status_t code, /* I - HTTP status code */ | |
2090 | char *type) /* I - MIME type of document */ | |
2091 | { | |
4744bd90 | 2092 | /* |
2093 | * Send the HTTP status header... | |
2094 | */ | |
2095 | ||
ef416fc2 | 2096 | if (httpPrintf(HTTP(con), "HTTP/%d.%d %d %s\r\n", con->http.version / 100, |
2097 | con->http.version % 100, code, httpStatus(code)) < 0) | |
2098 | return (0); | |
b423cd4c | 2099 | |
2100 | if (code == HTTP_CONTINUE) | |
2101 | { | |
4744bd90 | 2102 | /* |
2103 | * 100-continue doesn't send any headers... | |
2104 | */ | |
2105 | ||
b423cd4c | 2106 | if (httpPrintf(HTTP(con), "\r\n") < 0) |
2107 | return (0); | |
2108 | else | |
2109 | return (1); | |
2110 | } | |
2111 | ||
ef416fc2 | 2112 | if (httpPrintf(HTTP(con), "Date: %s\r\n", httpGetDateString(time(NULL))) < 0) |
2113 | return (0); | |
2114 | if (ServerHeader) | |
2115 | if (httpPrintf(HTTP(con), "Server: %s\r\n", ServerHeader) < 0) | |
2116 | return (0); | |
2117 | if (con->http.keep_alive && con->http.version >= HTTP_1_0) | |
2118 | { | |
2119 | if (httpPrintf(HTTP(con), "Connection: Keep-Alive\r\n") < 0) | |
2120 | return (0); | |
2121 | if (httpPrintf(HTTP(con), "Keep-Alive: timeout=%d\r\n", | |
2122 | KeepAliveTimeout) < 0) | |
2123 | return (0); | |
2124 | } | |
2125 | if (code == HTTP_METHOD_NOT_ALLOWED) | |
2126 | if (httpPrintf(HTTP(con), "Allow: GET, HEAD, OPTIONS, POST\r\n") < 0) | |
2127 | return (0); | |
2128 | ||
2129 | if (code == HTTP_UNAUTHORIZED) | |
2130 | { | |
2131 | int auth_type; /* Authentication type */ | |
2132 | ||
2133 | ||
2134 | if (!con->best || con->best->type == AUTH_NONE) | |
2135 | auth_type = DefaultAuthType; | |
2136 | else | |
2137 | auth_type = con->best->type; | |
2138 | ||
2139 | if (auth_type != AUTH_DIGEST) | |
2140 | { | |
2141 | if (httpPrintf(HTTP(con), | |
2142 | "WWW-Authenticate: Basic realm=\"CUPS\"\r\n") < 0) | |
2143 | return (0); | |
2144 | } | |
2145 | else | |
2146 | { | |
2147 | if (httpPrintf(HTTP(con), | |
2148 | "WWW-Authenticate: Digest realm=\"CUPS\", nonce=\"%s\"\r\n", | |
2149 | con->http.hostname) < 0) | |
2150 | return (0); | |
2151 | } | |
2152 | } | |
2153 | ||
e1d6a774 | 2154 | if (con->language && strcmp(con->language->language, "C")) |
ef416fc2 | 2155 | { |
2156 | if (httpPrintf(HTTP(con), "Content-Language: %s\r\n", | |
2157 | con->language->language) < 0) | |
2158 | return (0); | |
2159 | } | |
2160 | ||
e1d6a774 | 2161 | if (type) |
ef416fc2 | 2162 | { |
2163 | if (!strcmp(type, "text/html")) | |
2164 | { | |
2165 | if (httpPrintf(HTTP(con), | |
2166 | "Content-Type: text/html; charset=utf-8\r\n") < 0) | |
2167 | return (0); | |
2168 | } | |
2169 | else if (httpPrintf(HTTP(con), "Content-Type: %s\r\n", type) < 0) | |
2170 | return (0); | |
2171 | } | |
2172 | ||
2173 | return (1); | |
2174 | } | |
2175 | ||
2176 | ||
2177 | /* | |
2178 | * 'cupsdUpdateCGI()' - Read status messages from CGI scripts and programs. | |
2179 | */ | |
2180 | ||
2181 | void | |
2182 | cupsdUpdateCGI(void) | |
2183 | { | |
2184 | char *ptr, /* Pointer to end of line in buffer */ | |
2185 | message[1024]; /* Pointer to message text */ | |
2186 | int loglevel; /* Log level for message */ | |
2187 | ||
2188 | ||
2189 | while ((ptr = cupsdStatBufUpdate(CGIStatusBuffer, &loglevel, | |
2190 | message, sizeof(message))) != NULL) | |
2191 | if (!strchr(CGIStatusBuffer->buffer, '\n')) | |
2192 | break; | |
2193 | ||
2194 | if (ptr == NULL && errno) | |
2195 | { | |
2196 | /* | |
2197 | * Fatal error on pipe - should never happen! | |
2198 | */ | |
2199 | ||
2200 | cupsdLogMessage(CUPSD_LOG_CRIT, | |
2201 | "cupsdUpdateCGI: error reading from CGI error pipe - %s", | |
2202 | strerror(errno)); | |
2203 | } | |
2204 | } | |
2205 | ||
2206 | ||
2207 | /* | |
2208 | * 'cupsdWriteClient()' - Write data to a client as needed. | |
2209 | */ | |
2210 | ||
2211 | int /* O - 1 if success, 0 if fail */ | |
2212 | cupsdWriteClient(cupsd_client_t *con) /* I - Client connection */ | |
2213 | { | |
2214 | int bytes; /* Number of bytes written */ | |
2215 | char buf[16385]; /* Data buffer */ | |
2216 | char *bufptr; /* Pointer into buffer */ | |
2217 | ipp_state_t ipp_state; /* IPP state value */ | |
2218 | ||
2219 | ||
2220 | #ifdef DEBUG | |
2221 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
2222 | "cupsdWriteClient(con=%p) %d response=%p, file=%d " | |
2223 | "pipe_pid=%d state=%d", | |
2224 | con, con->http.fd, con->response, con->file, con->pipe_pid, | |
2225 | con->http.state); | |
2226 | #endif /* DEBUG */ | |
2227 | ||
2228 | if (con->http.state != HTTP_GET_SEND && | |
2229 | con->http.state != HTTP_POST_SEND) | |
2230 | return (1); | |
2231 | ||
2232 | if (con->response != NULL) | |
2233 | { | |
2234 | ipp_state = ippWrite(&(con->http), con->response); | |
2235 | bytes = ipp_state != IPP_ERROR && ipp_state != IPP_DATA; | |
2236 | } | |
2237 | else if ((bytes = read(con->file, buf, sizeof(buf) - 1)) > 0) | |
2238 | { | |
2239 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
2240 | "cupsdWriteClient: Read %d bytes from file %d...", | |
2241 | bytes, con->file); | |
2242 | ||
2243 | if (con->pipe_pid && !con->got_fields) | |
2244 | { | |
2245 | /* | |
2246 | * Inspect the data for Content-Type and other fields. | |
2247 | */ | |
2248 | ||
2249 | buf[bytes] = '\0'; | |
2250 | ||
2251 | for (bufptr = buf; !con->got_fields && *bufptr; bufptr ++) | |
2252 | if (*bufptr == '\n') | |
2253 | { | |
2254 | /* | |
2255 | * Send line to client... | |
2256 | */ | |
2257 | ||
2258 | if (bufptr > buf && bufptr[-1] == '\r') | |
2259 | bufptr[-1] = '\0'; | |
2260 | *bufptr++ = '\0'; | |
2261 | ||
2262 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "Script header: %s", buf); | |
2263 | ||
2264 | if (!con->sent_header) | |
2265 | { | |
2266 | /* | |
2267 | * Handle redirection and CGI status codes... | |
2268 | */ | |
2269 | ||
2270 | if (!strncasecmp(buf, "Location:", 9)) | |
d6ae789d | 2271 | { |
ef416fc2 | 2272 | cupsdSendHeader(con, HTTP_SEE_OTHER, NULL); |
d6ae789d | 2273 | if (httpPrintf(HTTP(con), "Content-Length: 0\r\n") < 0) |
2274 | return (0); | |
2275 | } | |
ef416fc2 | 2276 | else if (!strncasecmp(buf, "Status:", 7)) |
4744bd90 | 2277 | cupsdSendError(con, atoi(buf + 7)); |
ef416fc2 | 2278 | else |
4744bd90 | 2279 | { |
ef416fc2 | 2280 | cupsdSendHeader(con, HTTP_OK, NULL); |
2281 | ||
4744bd90 | 2282 | if (con->http.version == HTTP_1_1) |
2283 | { | |
2284 | con->http.data_encoding = HTTP_ENCODE_CHUNKED; | |
ef416fc2 | 2285 | |
4744bd90 | 2286 | if (httpPrintf(HTTP(con), "Transfer-Encoding: chunked\r\n") < 0) |
2287 | return (0); | |
2288 | } | |
2289 | } | |
ef416fc2 | 2290 | |
2291 | con->sent_header = 1; | |
2292 | } | |
2293 | ||
2294 | if (strncasecmp(buf, "Status:", 7)) | |
2295 | httpPrintf(HTTP(con), "%s\r\n", buf); | |
2296 | ||
2297 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdWriteClient: %d %s", | |
2298 | con->http.fd, buf); | |
2299 | ||
2300 | /* | |
2301 | * Update buffer... | |
2302 | */ | |
2303 | ||
2304 | bytes -= (bufptr - buf); | |
2305 | memmove(buf, bufptr, bytes + 1); | |
2306 | bufptr = buf - 1; | |
2307 | ||
2308 | /* | |
2309 | * See if the line was empty... | |
2310 | */ | |
2311 | ||
2312 | if (con->field_col == 0) | |
2313 | con->got_fields = 1; | |
2314 | else | |
2315 | con->field_col = 0; | |
2316 | } | |
2317 | else if (*bufptr != '\r') | |
2318 | con->field_col ++; | |
2319 | ||
2320 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
2321 | "cupsdWriteClient: %d bytes=%d, got_fields=%d", | |
2322 | con->http.fd, bytes, con->got_fields); | |
2323 | ||
2324 | if (bytes > 0 && !con->got_fields) | |
2325 | { | |
2326 | /* | |
2327 | * Remaining text needs to go out... | |
2328 | */ | |
2329 | ||
2330 | httpPrintf(HTTP(con), "%s", buf); | |
2331 | ||
2332 | con->http.activity = time(NULL); | |
2333 | return (1); | |
2334 | } | |
2335 | else if (bytes == 0) | |
ef416fc2 | 2336 | con->http.activity = time(NULL); |
ef416fc2 | 2337 | } |
2338 | ||
4744bd90 | 2339 | if (bytes > 0) |
ef416fc2 | 2340 | { |
4744bd90 | 2341 | if (httpWrite2(HTTP(con), buf, bytes) < 0) |
2342 | { | |
2343 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
2344 | "cupsdWriteClient: %d Write of %d bytes failed!", | |
2345 | con->http.fd, bytes); | |
ef416fc2 | 2346 | |
4744bd90 | 2347 | cupsdCloseClient(con); |
2348 | return (0); | |
2349 | } | |
ef416fc2 | 2350 | |
4744bd90 | 2351 | con->bytes += bytes; |
ef416fc2 | 2352 | |
4744bd90 | 2353 | if (con->http.state == HTTP_WAITING) |
2354 | bytes = 0; | |
2355 | } | |
ef416fc2 | 2356 | } |
2357 | ||
2358 | if (bytes <= 0) | |
2359 | { | |
2360 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdWriteClient: %d bytes < 0", | |
2361 | con->http.fd); | |
2362 | ||
2363 | cupsdLogRequest(con, HTTP_OK); | |
2364 | ||
2365 | httpFlushWrite(HTTP(con)); | |
2366 | ||
2367 | if (con->http.data_encoding == HTTP_ENCODE_CHUNKED) | |
2368 | { | |
2369 | if (httpPrintf(HTTP(con), "0\r\n\r\n") < 0) | |
2370 | { | |
2371 | cupsdCloseClient(con); | |
2372 | return (0); | |
2373 | } | |
2374 | } | |
2375 | ||
2376 | con->http.state = HTTP_WAITING; | |
2377 | ||
2378 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
2379 | "cupsdWriteClient: Removing fd %d from OutputSet...", | |
2380 | con->http.fd); | |
2381 | ||
2382 | FD_CLR(con->http.fd, OutputSet); | |
2383 | ||
2384 | if (con->file >= 0) | |
2385 | { | |
2386 | if (FD_ISSET(con->file, InputSet)) | |
2387 | { | |
2388 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
2389 | "cupsdWriteClient: Removing fd %d from InputSet...", | |
2390 | con->file); | |
2391 | FD_CLR(con->file, InputSet); | |
2392 | } | |
2393 | ||
2394 | if (con->pipe_pid) | |
2395 | cupsdEndProcess(con->pipe_pid, 0); | |
2396 | ||
2397 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
2398 | "cupsdWriteClient: %d Closing data file %d.", | |
2399 | con->http.fd, con->file); | |
2400 | ||
2401 | close(con->file); | |
2402 | con->file = -1; | |
2403 | con->pipe_pid = 0; | |
2404 | } | |
2405 | ||
2406 | if (con->filename) | |
2407 | { | |
2408 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
2409 | "cupsdWriteClient: %d Removing temp file %s", | |
2410 | con->http.fd, con->filename); | |
2411 | unlink(con->filename); | |
2412 | cupsdClearString(&con->filename); | |
2413 | } | |
2414 | ||
2415 | if (con->request != NULL) | |
2416 | { | |
2417 | ippDelete(con->request); | |
2418 | con->request = NULL; | |
2419 | } | |
2420 | ||
2421 | if (con->response != NULL) | |
2422 | { | |
2423 | ippDelete(con->response); | |
2424 | con->response = NULL; | |
2425 | } | |
2426 | ||
2427 | cupsdClearString(&con->command); | |
2428 | cupsdClearString(&con->options); | |
2429 | ||
2430 | if (!con->http.keep_alive) | |
2431 | { | |
2432 | cupsdCloseClient(con); | |
2433 | return (0); | |
2434 | } | |
2435 | } | |
2436 | else | |
2437 | { | |
2438 | con->file_ready = 0; | |
2439 | ||
2440 | if (con->pipe_pid && !FD_ISSET(con->file, InputSet)) | |
2441 | { | |
2442 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
2443 | "cupsdWriteClient: Adding fd %d to InputSet...", | |
2444 | con->file); | |
2445 | FD_SET(con->file, InputSet); | |
2446 | } | |
2447 | } | |
2448 | ||
2449 | con->http.activity = time(NULL); | |
2450 | ||
2451 | return (1); | |
2452 | } | |
2453 | ||
2454 | ||
e1d6a774 | 2455 | /* |
2456 | * 'check_if_modified()' - Decode an "If-Modified-Since" line. | |
2457 | */ | |
2458 | ||
2459 | static int /* O - 1 if modified since */ | |
2460 | check_if_modified( | |
2461 | cupsd_client_t *con, /* I - Client connection */ | |
2462 | struct stat *filestats) /* I - File information */ | |
2463 | { | |
2464 | char *ptr; /* Pointer into field */ | |
2465 | time_t date; /* Time/date value */ | |
2466 | off_t size; /* Size/length value */ | |
2467 | ||
2468 | ||
2469 | size = 0; | |
2470 | date = 0; | |
2471 | ptr = con->http.fields[HTTP_FIELD_IF_MODIFIED_SINCE]; | |
2472 | ||
2473 | if (*ptr == '\0') | |
2474 | return (1); | |
2475 | ||
2476 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
2477 | "check_if_modified: %d If-Modified-Since=\"%s\"", | |
2478 | con->http.fd, ptr); | |
2479 | ||
2480 | while (*ptr != '\0') | |
2481 | { | |
2482 | while (isspace(*ptr) || *ptr == ';') | |
2483 | ptr ++; | |
2484 | ||
2485 | if (strncasecmp(ptr, "length=", 7) == 0) | |
2486 | { | |
2487 | ptr += 7; | |
2488 | size = strtoll(ptr, NULL, 10); | |
2489 | ||
2490 | while (isdigit(*ptr)) | |
2491 | ptr ++; | |
2492 | } | |
2493 | else if (isalpha(*ptr)) | |
2494 | { | |
2495 | date = httpGetDateTime(ptr); | |
2496 | while (*ptr != '\0' && *ptr != ';') | |
2497 | ptr ++; | |
2498 | } | |
e53920b9 | 2499 | else |
2500 | ptr ++; | |
e1d6a774 | 2501 | } |
2502 | ||
2503 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
2504 | "check_if_modified: %d sizes=" CUPS_LLFMT "," | |
2505 | CUPS_LLFMT " dates=%d,%d", | |
2506 | con->http.fd, CUPS_LLCAST size, | |
2507 | CUPS_LLCAST filestats->st_size, (int)date, | |
2508 | (int)filestats->st_mtime); | |
2509 | ||
2510 | return ((size != filestats->st_size && size != 0) || | |
2511 | (date < filestats->st_mtime && date != 0) || | |
2512 | (size == 0 && date == 0)); | |
2513 | } | |
2514 | ||
2515 | ||
80ca4592 | 2516 | #ifdef HAVE_SSL |
e1d6a774 | 2517 | /* |
2518 | * 'encrypt_client()' - Enable encryption for the client... | |
2519 | */ | |
2520 | ||
2521 | static int /* O - 1 on success, 0 on error */ | |
2522 | encrypt_client(cupsd_client_t *con) /* I - Client to encrypt */ | |
2523 | { | |
80ca4592 | 2524 | # ifdef HAVE_LIBSSL |
e1d6a774 | 2525 | SSL_CTX *context; /* Context for encryption */ |
2526 | SSL *conn; /* Connection for encryption */ | |
2527 | unsigned long error; /* Error code */ | |
2528 | ||
2529 | ||
2530 | /* | |
2531 | * Create the SSL context and accept the connection... | |
2532 | */ | |
2533 | ||
2534 | context = SSL_CTX_new(SSLv23_server_method()); | |
2535 | ||
2536 | SSL_CTX_set_options(context, SSL_OP_NO_SSLv2); /* Only use SSLv3 or TLS */ | |
2537 | SSL_CTX_use_PrivateKey_file(context, ServerKey, SSL_FILETYPE_PEM); | |
2538 | SSL_CTX_use_certificate_file(context, ServerCertificate, SSL_FILETYPE_PEM); | |
2539 | ||
2540 | conn = SSL_new(context); | |
2541 | ||
2542 | SSL_set_fd(conn, con->http.fd); | |
2543 | if (SSL_accept(conn) != 1) | |
2544 | { | |
2545 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
2546 | "encrypt_client: Unable to encrypt connection from %s!", | |
2547 | con->http.hostname); | |
2548 | ||
2549 | while ((error = ERR_get_error()) != 0) | |
2550 | cupsdLogMessage(CUPSD_LOG_ERROR, "encrypt_client: %s", | |
2551 | ERR_error_string(error, NULL)); | |
2552 | ||
2553 | SSL_CTX_free(context); | |
2554 | SSL_free(conn); | |
2555 | return (0); | |
2556 | } | |
2557 | ||
2558 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
2559 | "encrypt_client: %d Connection from %s now encrypted.", | |
2560 | con->http.fd, con->http.hostname); | |
2561 | ||
2562 | con->http.tls = conn; | |
2563 | return (1); | |
2564 | ||
80ca4592 | 2565 | # elif defined(HAVE_GNUTLS) |
e1d6a774 | 2566 | http_tls_t *conn; /* TLS session object */ |
2567 | int error; /* Error code */ | |
2568 | gnutls_certificate_server_credentials *credentials; | |
2569 | /* TLS credentials */ | |
2570 | ||
2571 | ||
2572 | /* | |
2573 | * Verify that we have a certificate... | |
2574 | */ | |
2575 | ||
2576 | if (access(ServerKey, 0) || access(ServerCertificate, 0)) | |
2577 | { | |
2578 | /* | |
2579 | * Nope, make a self-signed certificate... | |
2580 | */ | |
2581 | ||
2582 | make_certificate(); | |
2583 | } | |
2584 | ||
2585 | /* | |
2586 | * Create the SSL object and perform the SSL handshake... | |
2587 | */ | |
2588 | ||
89d46774 | 2589 | conn = (http_tls_t *)malloc(sizeof(http_tls_t)); |
e1d6a774 | 2590 | |
2591 | if (conn == NULL) | |
2592 | return (0); | |
2593 | ||
2594 | credentials = (gnutls_certificate_server_credentials *) | |
2595 | malloc(sizeof(gnutls_certificate_server_credentials)); | |
2596 | if (credentials == NULL) | |
2597 | { | |
2598 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
2599 | "encrypt_client: Unable to encrypt connection from %s!", | |
2600 | con->http.hostname); | |
2601 | cupsdLogMessage(CUPSD_LOG_ERROR, "encrypt_client: %s", strerror(errno)); | |
2602 | ||
2603 | free(conn); | |
2604 | return (0); | |
2605 | } | |
2606 | ||
2607 | gnutls_certificate_allocate_credentials(credentials); | |
2608 | gnutls_certificate_set_x509_key_file(*credentials, ServerCertificate, | |
2609 | ServerKey, GNUTLS_X509_FMT_PEM); | |
2610 | ||
2611 | gnutls_init(&(conn->session), GNUTLS_SERVER); | |
2612 | gnutls_set_default_priority(conn->session); | |
2613 | gnutls_credentials_set(conn->session, GNUTLS_CRD_CERTIFICATE, *credentials); | |
2614 | gnutls_transport_set_ptr(conn->session, | |
2615 | (gnutls_transport_ptr)((long)con->http.fd)); | |
2616 | ||
2617 | error = gnutls_handshake(conn->session); | |
2618 | ||
2619 | if (error != GNUTLS_E_SUCCESS) | |
2620 | { | |
2621 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
2622 | "encrypt_client: Unable to encrypt connection from %s!", | |
2623 | con->http.hostname); | |
2624 | cupsdLogMessage(CUPSD_LOG_ERROR, "encrypt_client: %s", | |
2625 | gnutls_strerror(error)); | |
2626 | ||
2627 | gnutls_deinit(conn->session); | |
2628 | gnutls_certificate_free_credentials(*credentials); | |
2629 | free(conn); | |
2630 | free(credentials); | |
2631 | return (0); | |
2632 | } | |
2633 | ||
2634 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
2635 | "encrypt_client: %d Connection from %s now encrypted.", | |
2636 | con->http.fd, con->http.hostname); | |
2637 | ||
2638 | conn->credentials = credentials; | |
2639 | con->http.tls = conn; | |
2640 | return (1); | |
2641 | ||
80ca4592 | 2642 | # elif defined(HAVE_CDSASSL) |
89d46774 | 2643 | OSStatus error; /* Error code */ |
2644 | http_tls_t *conn; /* CDSA connection information */ | |
2645 | cdsa_conn_ref_t u; /* Connection reference union */ | |
e1d6a774 | 2646 | |
2647 | ||
89d46774 | 2648 | if ((conn = (http_tls_t *)malloc(sizeof(http_tls_t))) == NULL) |
2649 | return (0); | |
e1d6a774 | 2650 | |
89d46774 | 2651 | error = 0; |
2652 | conn->session = NULL; | |
2653 | conn->certsArray = get_cdsa_server_certs(); | |
e1d6a774 | 2654 | |
89d46774 | 2655 | if (!conn->certsArray) |
e1d6a774 | 2656 | { |
2657 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
2658 | "EncryptClient: Could not find signing key in keychain " | |
2659 | "\"%s\"", ServerCertificate); | |
2660 | error = errSSLBadCert; /* errSSLBadConfiguration is a better choice, but not available on 10.2.x */ | |
2661 | } | |
2662 | ||
2663 | if (!error) | |
89d46774 | 2664 | error = SSLNewContext(true, &conn->session); |
e1d6a774 | 2665 | |
2666 | if (!error) | |
89d46774 | 2667 | error = SSLSetIOFuncs(conn->session, _httpReadCDSA, _httpWriteCDSA); |
2668 | ||
2669 | if (!error) | |
ed486911 | 2670 | error = SSLSetProtocolVersionEnabled(conn->session, kSSLProtocol2, false); |
e1d6a774 | 2671 | |
2672 | if (!error) | |
e53920b9 | 2673 | { |
2674 | /* | |
2675 | * Use a union to resolve warnings about int/pointer size mismatches... | |
2676 | */ | |
2677 | ||
2678 | u.connection = NULL; | |
2679 | u.sock = con->http.fd; | |
89d46774 | 2680 | error = SSLSetConnection(conn->session, u.connection); |
e53920b9 | 2681 | } |
e1d6a774 | 2682 | |
2683 | if (!error) | |
89d46774 | 2684 | error = SSLSetAllowsExpiredCerts(conn->session, true); |
e1d6a774 | 2685 | |
2686 | if (!error) | |
89d46774 | 2687 | error = SSLSetAllowsAnyRoot(conn->session, true); |
ef416fc2 | 2688 | |
89d46774 | 2689 | if (!error) |
2690 | error = SSLSetCertificate(conn->session, conn->certsArray); | |
ef416fc2 | 2691 | |
e1d6a774 | 2692 | if (!error) |
2693 | { | |
2694 | /* | |
2695 | * Perform SSL/TLS handshake | |
2696 | */ | |
ef416fc2 | 2697 | |
89d46774 | 2698 | while ((error = SSLHandshake(conn->session)) == errSSLWouldBlock) |
e1d6a774 | 2699 | usleep(1000); |
2700 | } | |
ef416fc2 | 2701 | |
e1d6a774 | 2702 | if (error) |
2703 | { | |
2704 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
2705 | "encrypt_client: Unable to encrypt connection from %s!", | |
2706 | con->http.hostname); | |
ef416fc2 | 2707 | |
ed486911 | 2708 | cupsdLogMessage(CUPSD_LOG_ERROR, "encrypt_client: %s (%d)", |
2709 | cssmErrorString(error), (int)error); | |
ef416fc2 | 2710 | |
e1d6a774 | 2711 | con->http.error = error; |
2712 | con->http.status = HTTP_ERROR; | |
ef416fc2 | 2713 | |
89d46774 | 2714 | if (conn->session) |
2715 | SSLDisposeContext(conn->session); | |
2716 | ||
2717 | if (conn->certsArray) | |
2718 | CFRelease(conn->certsArray); | |
2719 | ||
2720 | free(conn); | |
ef416fc2 | 2721 | |
e1d6a774 | 2722 | return (0); |
ef416fc2 | 2723 | } |
2724 | ||
e1d6a774 | 2725 | cupsdLogMessage(CUPSD_LOG_DEBUG, |
2726 | "encrypt_client: %d Connection from %s now encrypted.", | |
2727 | con->http.fd, con->http.hostname); | |
ef416fc2 | 2728 | |
e1d6a774 | 2729 | con->http.tls = conn; |
2730 | return (1); | |
2731 | ||
80ca4592 | 2732 | # endif /* HAVE_LIBSSL */ |
ef416fc2 | 2733 | } |
80ca4592 | 2734 | #endif /* HAVE_SSL */ |
ef416fc2 | 2735 | |
2736 | ||
2737 | #ifdef HAVE_CDSASSL | |
2738 | /* | |
2739 | * 'get_cdsa_server_certs()' - Convert a keychain name into the CFArrayRef | |
2740 | * required by SSLSetCertificate. | |
2741 | * | |
2742 | * For now we assumes that there is exactly one SecIdentity in the | |
2743 | * keychain - i.e. there is exactly one matching cert/private key pair. | |
2744 | * In the future we will search a keychain for a SecIdentity matching a | |
2745 | * specific criteria. We also skip the operation of adding additional | |
2746 | * non-signing certs from the keychain to the CFArrayRef. | |
2747 | * | |
2748 | * To create a self-signed certificate for testing use the certtool. | |
2749 | * Executing the following as root will do it: | |
2750 | * | |
b423cd4c | 2751 | * certtool c k=/Library/Keychains/System.keychain |
ef416fc2 | 2752 | */ |
2753 | ||
2754 | static CFArrayRef /* O - Array of certificates */ | |
2755 | get_cdsa_server_certs(void) | |
2756 | { | |
2757 | OSStatus err; /* Error info */ | |
2758 | SecKeychainRef kcRef; /* Keychain reference */ | |
2759 | SecIdentitySearchRef srchRef; /* Search reference */ | |
2760 | SecIdentityRef identity; /* Identity */ | |
2761 | CFArrayRef ca; /* Certificate array */ | |
2762 | ||
2763 | ||
2764 | kcRef = NULL; | |
2765 | srchRef = NULL; | |
2766 | identity = NULL; | |
2767 | ca = NULL; | |
2768 | err = SecKeychainOpen(ServerCertificate, &kcRef); | |
2769 | ||
2770 | if (err) | |
2771 | cupsdLogMessage(CUPSD_LOG_ERROR, "Cannot open keychain \"%s\", error %d.", | |
2772 | ServerCertificate, (int)err); | |
2773 | else | |
2774 | { | |
2775 | /* | |
2776 | * Search for "any" identity matching specified key use; | |
2777 | * in this app, we expect there to be exactly one. | |
2778 | */ | |
2779 | ||
2780 | err = SecIdentitySearchCreate(kcRef, CSSM_KEYUSE_SIGN, &srchRef); | |
2781 | ||
2782 | if (err) | |
2783 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
2784 | "Cannot find signing key in keychain \"%s\", error %d", | |
2785 | ServerCertificate, (int)err); | |
2786 | else | |
2787 | { | |
2788 | err = SecIdentitySearchCopyNext(srchRef, &identity); | |
2789 | ||
2790 | if (err) | |
2791 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
2792 | "Cannot find signing key in keychain \"%s\", error %d", | |
2793 | ServerCertificate, (int)err); | |
2794 | else | |
2795 | { | |
2796 | if (CFGetTypeID(identity) != SecIdentityGetTypeID()) | |
2797 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
2798 | "SecIdentitySearchCopyNext CFTypeID failure!"); | |
2799 | else | |
2800 | { | |
2801 | /* | |
2802 | * Found one. Place it in a CFArray. | |
2803 | * TBD: snag other (non-identity) certs from keychain and add them | |
2804 | * to array as well. | |
2805 | */ | |
2806 | ||
b423cd4c | 2807 | ca = CFArrayCreate(NULL, (const void **)&identity, 1, &kCFTypeArrayCallBacks); |
ef416fc2 | 2808 | |
2809 | if (ca == nil) | |
2810 | cupsdLogMessage(CUPSD_LOG_ERROR, "CFArrayCreate error"); | |
2811 | } | |
2812 | ||
b423cd4c | 2813 | CFRelease(identity); |
ef416fc2 | 2814 | } |
2815 | ||
b423cd4c | 2816 | CFRelease(srchRef); |
ef416fc2 | 2817 | } |
2818 | ||
b423cd4c | 2819 | CFRelease(kcRef); |
ef416fc2 | 2820 | } |
2821 | ||
2822 | return (ca); | |
2823 | } | |
2824 | #endif /* HAVE_CDSASSL */ | |
2825 | ||
2826 | ||
2827 | /* | |
2828 | * 'get_file()' - Get a filename and state info. | |
2829 | */ | |
2830 | ||
2831 | static char * /* O - Real filename */ | |
2832 | get_file(cupsd_client_t *con, /* I - Client connection */ | |
2833 | struct stat *filestats, /* O - File information */ | |
2834 | char *filename, /* IO - Filename buffer */ | |
2835 | int len) /* I - Buffer length */ | |
2836 | { | |
2837 | int status; /* Status of filesystem calls */ | |
2838 | char *ptr; /* Pointer info filename */ | |
2839 | int plen; /* Remaining length after pointer */ | |
2840 | ||
2841 | ||
2842 | /* | |
2843 | * Need to add DocumentRoot global... | |
2844 | */ | |
2845 | ||
2846 | if (!strncmp(con->uri, "/ppd/", 5)) | |
2847 | snprintf(filename, len, "%s%s", ServerRoot, con->uri); | |
2848 | else if (!strncmp(con->uri, "/admin/conf/", 12)) | |
2849 | snprintf(filename, len, "%s%s", ServerRoot, con->uri + 11); | |
2850 | else if (!strncmp(con->uri, "/admin/log/", 11)) | |
2851 | { | |
2852 | if (!strcmp(con->uri + 11, "access_log") && AccessLog[0] == '/') | |
2853 | strlcpy(filename, AccessLog, len); | |
2854 | else if (!strcmp(con->uri + 11, "error_log") && ErrorLog[0] == '/') | |
2855 | strlcpy(filename, ErrorLog, len); | |
2856 | else if (!strcmp(con->uri + 11, "page_log") && PageLog[0] == '/') | |
2857 | strlcpy(filename, PageLog, len); | |
2858 | else | |
2859 | return (NULL); | |
2860 | } | |
d6ae789d | 2861 | else if (con->language) |
ef416fc2 | 2862 | snprintf(filename, len, "%s/%s%s", DocumentRoot, con->language->language, |
d6ae789d | 2863 | con->uri); |
ef416fc2 | 2864 | else |
2865 | snprintf(filename, len, "%s%s", DocumentRoot, con->uri); | |
2866 | ||
2867 | if ((ptr = strchr(filename, '?')) != NULL) | |
2868 | *ptr = '\0'; | |
2869 | ||
2870 | /* | |
2871 | * Grab the status for this language; if there isn't a language-specific file | |
2872 | * then fallback to the default one... | |
2873 | */ | |
2874 | ||
d6ae789d | 2875 | if ((status = stat(filename, filestats)) != 0 && con->language && |
2876 | strncmp(con->uri, "/ppd/", 5) && | |
2877 | strncmp(con->uri, "/admin/conf/", 12) && | |
2878 | strncmp(con->uri, "/admin/log/", 11)) | |
ef416fc2 | 2879 | { |
2880 | /* | |
d6ae789d | 2881 | * Drop the country code... |
ef416fc2 | 2882 | */ |
2883 | ||
d6ae789d | 2884 | char ll[3]; /* Short language name */ |
2885 | ||
2886 | ||
2887 | strlcpy(ll, con->language->language, sizeof(ll)); | |
2888 | snprintf(filename, len, "%s/%s%s", DocumentRoot, ll, con->uri); | |
2889 | ||
2890 | if ((ptr = strchr(filename, '?')) != NULL) | |
2891 | *ptr = '\0'; | |
2892 | ||
2893 | if ((status = stat(filename, filestats)) != 0) | |
ef416fc2 | 2894 | { |
d6ae789d | 2895 | /* |
2896 | * Drop the language prefix and try the root directory... | |
2897 | */ | |
2898 | ||
ef416fc2 | 2899 | snprintf(filename, len, "%s%s", DocumentRoot, con->uri); |
2900 | ||
2901 | if ((ptr = strchr(filename, '?')) != NULL) | |
2902 | *ptr = '\0'; | |
2903 | ||
2904 | status = stat(filename, filestats); | |
2905 | } | |
2906 | } | |
2907 | ||
2908 | /* | |
2909 | * If we're found a directory, get the index.html file instead... | |
2910 | */ | |
2911 | ||
2912 | if (!status && S_ISDIR(filestats->st_mode)) | |
2913 | { | |
2914 | if (filename[strlen(filename) - 1] != '/') | |
2915 | strlcat(filename, "/", len); | |
2916 | ||
2917 | ptr = filename + strlen(filename); | |
2918 | plen = len - (ptr - filename); | |
2919 | ||
2920 | strlcpy(ptr, "index.html", plen); | |
2921 | status = stat(filename, filestats); | |
2922 | ||
2923 | #ifdef HAVE_JAVA | |
2924 | if (status) | |
2925 | { | |
2926 | strlcpy(ptr, "index.class", plen); | |
2927 | status = stat(filename, filestats); | |
2928 | } | |
2929 | #endif /* HAVE_JAVA */ | |
2930 | ||
2931 | #ifdef HAVE_PERL | |
2932 | if (status) | |
2933 | { | |
2934 | strlcpy(ptr, "index.pl", plen); | |
2935 | status = stat(filename, filestats); | |
2936 | } | |
2937 | #endif /* HAVE_PERL */ | |
2938 | ||
2939 | #ifdef HAVE_PHP | |
2940 | if (status) | |
2941 | { | |
2942 | strlcpy(ptr, "index.php", plen); | |
2943 | status = stat(filename, filestats); | |
2944 | } | |
2945 | #endif /* HAVE_PHP */ | |
2946 | ||
2947 | #ifdef HAVE_PYTHON | |
2948 | if (status) | |
2949 | { | |
2950 | strlcpy(ptr, "index.pyc", plen); | |
2951 | status = stat(filename, filestats); | |
2952 | } | |
2953 | ||
2954 | if (status) | |
2955 | { | |
2956 | strlcpy(ptr, "index.py", plen); | |
2957 | status = stat(filename, filestats); | |
2958 | } | |
2959 | #endif /* HAVE_PYTHON */ | |
2960 | } | |
2961 | ||
2962 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_file: %d filename=%s size=%d", | |
2963 | con->http.fd, filename, | |
2964 | status ? -1 : (int)filestats->st_size); | |
2965 | ||
2966 | if (!status) | |
2967 | con->http.data_remaining = (int)filestats->st_size; | |
2968 | ||
2969 | if (status) | |
2970 | return (NULL); | |
2971 | else | |
2972 | return (filename); | |
2973 | } | |
2974 | ||
2975 | ||
2976 | /* | |
2977 | * 'install_conf_file()' - Install a configuration file. | |
2978 | */ | |
2979 | ||
2980 | static http_status_t /* O - Status */ | |
2981 | install_conf_file(cupsd_client_t *con) /* I - Connection */ | |
2982 | { | |
2983 | cups_file_t *in, /* Input file */ | |
2984 | *out; /* Output file */ | |
2985 | char buffer[1024]; /* Copy buffer */ | |
2986 | int bytes; /* Number of bytes */ | |
2987 | char conffile[1024], /* Configuration filename */ | |
2988 | newfile[1024], /* New config filename */ | |
2989 | oldfile[1024]; /* Old config filename */ | |
2990 | struct stat confinfo; /* Config file info */ | |
2991 | ||
2992 | ||
2993 | /* | |
2994 | * First construct the filenames... | |
2995 | */ | |
2996 | ||
2997 | snprintf(conffile, sizeof(conffile), "%s%s", ServerRoot, con->uri + 11); | |
2998 | snprintf(newfile, sizeof(newfile), "%s%s.N", ServerRoot, con->uri + 11); | |
2999 | snprintf(oldfile, sizeof(oldfile), "%s%s.O", ServerRoot, con->uri + 11); | |
3000 | ||
3001 | cupsdLogMessage(CUPSD_LOG_INFO, "Installing config file \"%s\"...", conffile); | |
3002 | ||
3003 | /* | |
3004 | * Get the owner, group, and permissions of the configuration file. | |
3005 | * If it doesn't exist, assign it to the User and Group in the | |
3006 | * cupsd.conf file with mode 0640 permissions. | |
3007 | */ | |
3008 | ||
3009 | if (stat(conffile, &confinfo)) | |
3010 | { | |
3011 | confinfo.st_uid = User; | |
3012 | confinfo.st_gid = Group; | |
3013 | confinfo.st_mode = ConfigFilePerm; | |
3014 | } | |
3015 | ||
3016 | /* | |
3017 | * Open the request file and new config file... | |
3018 | */ | |
3019 | ||
3020 | if ((in = cupsFileOpen(con->filename, "rb")) == NULL) | |
3021 | { | |
3022 | cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to open request file \"%s\" - %s", | |
3023 | con->filename, strerror(errno)); | |
3024 | return (HTTP_SERVER_ERROR); | |
3025 | } | |
3026 | ||
3027 | if ((out = cupsFileOpen(newfile, "wb")) == NULL) | |
3028 | { | |
3029 | cupsFileClose(in); | |
3030 | cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to open config file \"%s\" - %s", | |
3031 | newfile, strerror(errno)); | |
3032 | return (HTTP_SERVER_ERROR); | |
3033 | } | |
3034 | ||
3035 | fchmod(cupsFileNumber(out), confinfo.st_mode); | |
3036 | fchown(cupsFileNumber(out), confinfo.st_uid, confinfo.st_gid); | |
3037 | ||
3038 | /* | |
3039 | * Copy from the request to the new config file... | |
3040 | */ | |
3041 | ||
3042 | while ((bytes = cupsFileRead(in, buffer, sizeof(buffer))) > 0) | |
3043 | if (cupsFileWrite(out, buffer, bytes) < bytes) | |
3044 | { | |
3045 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
3046 | "Unable to copy to config file \"%s\" - %s", | |
3047 | newfile, strerror(errno)); | |
3048 | ||
3049 | cupsFileClose(in); | |
3050 | cupsFileClose(out); | |
3051 | unlink(newfile); | |
3052 | ||
3053 | return (HTTP_SERVER_ERROR); | |
3054 | } | |
3055 | ||
3056 | /* | |
3057 | * Close the files... | |
3058 | */ | |
3059 | ||
3060 | cupsFileClose(in); | |
3061 | if (cupsFileClose(out)) | |
3062 | { | |
3063 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
3064 | "Error file closing config file \"%s\" - %s", | |
3065 | newfile, strerror(errno)); | |
3066 | ||
3067 | unlink(newfile); | |
3068 | ||
3069 | return (HTTP_SERVER_ERROR); | |
3070 | } | |
3071 | ||
3072 | /* | |
3073 | * Remove the request file... | |
3074 | */ | |
3075 | ||
3076 | unlink(con->filename); | |
3077 | cupsdClearString(&con->filename); | |
3078 | ||
3079 | /* | |
3080 | * Unlink the old backup, rename the current config file to the backup | |
3081 | * filename, and rename the new config file to the config file name... | |
3082 | */ | |
3083 | ||
3084 | if (unlink(oldfile)) | |
3085 | if (errno != ENOENT) | |
3086 | { | |
3087 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
3088 | "Unable to remove backup config file \"%s\" - %s", | |
3089 | oldfile, strerror(errno)); | |
3090 | ||
3091 | unlink(newfile); | |
3092 | ||
3093 | return (HTTP_SERVER_ERROR); | |
3094 | } | |
3095 | ||
3096 | if (rename(conffile, oldfile)) | |
3097 | if (errno != ENOENT) | |
3098 | { | |
3099 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
3100 | "Unable to rename old config file \"%s\" - %s", | |
3101 | conffile, strerror(errno)); | |
3102 | ||
3103 | unlink(newfile); | |
3104 | ||
3105 | return (HTTP_SERVER_ERROR); | |
3106 | } | |
3107 | ||
3108 | if (rename(newfile, conffile)) | |
3109 | { | |
3110 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
3111 | "Unable to rename new config file \"%s\" - %s", | |
3112 | newfile, strerror(errno)); | |
3113 | ||
3114 | rename(oldfile, conffile); | |
3115 | unlink(newfile); | |
3116 | ||
3117 | return (HTTP_SERVER_ERROR); | |
3118 | } | |
3119 | ||
3120 | /* | |
3121 | * If the cupsd.conf file was updated, set the NeedReload flag... | |
3122 | */ | |
3123 | ||
3124 | if (!strcmp(con->uri, "/admin/conf/cupsd.conf")) | |
3125 | NeedReload = RELOAD_CUPSD; | |
3126 | else | |
3127 | NeedReload = RELOAD_ALL; | |
3128 | ||
3129 | ReloadTime = time(NULL); | |
3130 | ||
3131 | /* | |
3132 | * Return that the file was created successfully... | |
3133 | */ | |
3134 | ||
3135 | return (HTTP_CREATED); | |
3136 | } | |
3137 | ||
3138 | ||
e1d6a774 | 3139 | /* |
3140 | * 'is_cgi()' - Is the resource a CGI script/program? | |
3141 | */ | |
3142 | ||
3143 | static int /* O - 1 = CGI, 0 = file */ | |
3144 | is_cgi(cupsd_client_t *con, /* I - Client connection */ | |
3145 | const char *filename, /* I - Real filename */ | |
3146 | struct stat *filestats, /* I - File information */ | |
3147 | mime_type_t *type) /* I - MIME type */ | |
3148 | { | |
3149 | const char *options; /* Options on URL */ | |
3150 | ||
3151 | ||
3152 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
3153 | "is_cgi(con=%p, filename=\"%s\", filestats=%p, type=%s/%s)\n", | |
3154 | con, filename, filestats, type ? type->super : "unknown", | |
3155 | type ? type->type : "unknown"); | |
3156 | ||
3157 | /* | |
3158 | * Get the options, if any... | |
3159 | */ | |
3160 | ||
3161 | if ((options = strchr(con->uri, '?')) != NULL) | |
3162 | options ++; | |
3163 | ||
3164 | /* | |
3165 | * Check for known types... | |
3166 | */ | |
3167 | ||
3168 | if (!type || strcasecmp(type->super, "application")) | |
3169 | { | |
3170 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "is_cgi: Returning 0..."); | |
3171 | return (0); | |
3172 | } | |
3173 | ||
3174 | if (!strcasecmp(type->type, "x-httpd-cgi") && | |
3175 | (filestats->st_mode & 0111)) | |
3176 | { | |
3177 | /* | |
3178 | * "application/x-httpd-cgi" is a CGI script. | |
3179 | */ | |
3180 | ||
3181 | cupsdSetString(&con->command, filename); | |
3182 | ||
3183 | filename = strrchr(filename, '/') + 1; /* Filename always absolute */ | |
3184 | ||
3185 | cupsdSetString(&con->options, options); | |
3186 | ||
3187 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
3188 | "is_cgi: Returning 1 with command=\"%s\" and options=\"%s\"", | |
3189 | con->command, con->options); | |
3190 | ||
3191 | return (1); | |
3192 | } | |
3193 | #ifdef HAVE_JAVA | |
3194 | else if (!strcasecmp(type->type, "x-httpd-java")) | |
3195 | { | |
3196 | /* | |
3197 | * "application/x-httpd-java" is a Java servlet. | |
3198 | */ | |
3199 | ||
3200 | cupsdSetString(&con->command, CUPS_JAVA); | |
3201 | ||
3202 | if (options) | |
3203 | cupsdSetStringf(&con->options, "%s %s", filename, options); | |
3204 | else | |
3205 | cupsdSetString(&con->options, filename); | |
3206 | ||
3207 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
3208 | "is_cgi: Returning 1 with command=\"%s\" and options=\"%s\"", | |
3209 | con->command, con->options); | |
3210 | ||
3211 | return (1); | |
3212 | } | |
3213 | #endif /* HAVE_JAVA */ | |
3214 | #ifdef HAVE_PERL | |
3215 | else if (!strcasecmp(type->type, "x-httpd-perl")) | |
3216 | { | |
3217 | /* | |
3218 | * "application/x-httpd-perl" is a Perl page. | |
3219 | */ | |
3220 | ||
3221 | cupsdSetString(&con->command, CUPS_PERL); | |
3222 | ||
3223 | if (options) | |
3224 | cupsdSetStringf(&con->options, "%s %s", filename, options); | |
3225 | else | |
3226 | cupsdSetString(&con->options, filename); | |
3227 | ||
3228 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
3229 | "is_cgi: Returning 1 with command=\"%s\" and options=\"%s\"", | |
3230 | con->command, con->options); | |
3231 | ||
3232 | return (1); | |
3233 | } | |
3234 | #endif /* HAVE_PERL */ | |
3235 | #ifdef HAVE_PHP | |
3236 | else if (!strcasecmp(type->type, "x-httpd-php")) | |
3237 | { | |
3238 | /* | |
3239 | * "application/x-httpd-php" is a PHP page. | |
3240 | */ | |
3241 | ||
3242 | cupsdSetString(&con->command, CUPS_PHP); | |
3243 | ||
3244 | if (options) | |
3245 | cupsdSetStringf(&con->options, "%s %s", filename, options); | |
3246 | else | |
3247 | cupsdSetString(&con->options, filename); | |
3248 | ||
3249 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
3250 | "is_cgi: Returning 1 with command=\"%s\" and options=\"%s\"", | |
3251 | con->command, con->options); | |
3252 | ||
3253 | return (1); | |
3254 | } | |
3255 | #endif /* HAVE_PHP */ | |
3256 | #ifdef HAVE_PYTHON | |
3257 | else if (!strcasecmp(type->type, "x-httpd-python")) | |
3258 | { | |
3259 | /* | |
3260 | * "application/x-httpd-python" is a Python page. | |
3261 | */ | |
3262 | ||
3263 | cupsdSetString(&con->command, CUPS_PYTHON); | |
3264 | ||
3265 | if (options) | |
3266 | cupsdSetStringf(&con->options, "%s %s", filename, options); | |
3267 | else | |
3268 | cupsdSetString(&con->options, filename); | |
3269 | ||
3270 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
3271 | "is_cgi: Returning 1 with command=\"%s\" and options=\"%s\"", | |
3272 | con->command, con->options); | |
3273 | ||
3274 | return (1); | |
3275 | } | |
3276 | #endif /* HAVE_PYTHON */ | |
3277 | ||
3278 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "is_cgi: Returning 0..."); | |
3279 | ||
3280 | return (0); | |
3281 | } | |
3282 | ||
3283 | ||
ef416fc2 | 3284 | /* |
3285 | * 'is_path_absolute()' - Is a path absolute and free of relative elements (i.e. ".."). | |
3286 | */ | |
3287 | ||
3288 | static int /* O - 0 if relative, 1 if absolute */ | |
3289 | is_path_absolute(const char *path) /* I - Input path */ | |
3290 | { | |
3291 | /* | |
3292 | * Check for a leading slash... | |
3293 | */ | |
3294 | ||
3295 | if (path[0] != '/') | |
3296 | return (0); | |
3297 | ||
3298 | /* | |
3299 | * Check for "/.." in the path... | |
3300 | */ | |
3301 | ||
3302 | while ((path = strstr(path, "/..")) != NULL) | |
3303 | { | |
3304 | if (!path[3] || path[3] == '/') | |
3305 | return (0); | |
3306 | ||
3307 | path ++; | |
3308 | } | |
3309 | ||
3310 | /* | |
3311 | * If we haven't found any relative paths, return 1 indicating an | |
3312 | * absolute path... | |
3313 | */ | |
3314 | ||
3315 | return (1); | |
3316 | } | |
3317 | ||
3318 | ||
4744bd90 | 3319 | #ifdef HAVE_GNUTLS |
3320 | /* | |
3321 | * 'make_certificate()' - Make a self-signed SSL/TLS certificate. | |
3322 | */ | |
3323 | ||
3324 | static void | |
3325 | make_certificate(void) | |
3326 | { | |
3327 | gnutls_x509_crt crt; /* Self-signed certificate */ | |
3328 | gnutls_x509_privkey key; /* Encryption key */ | |
3329 | cups_lang_t *language; /* Default language info */ | |
3330 | cups_file_t *fp; /* Key/cert file */ | |
3331 | unsigned char buffer[8192]; /* Buffer for x509 data */ | |
3332 | size_t bytes; /* Number of bytes of data */ | |
3333 | unsigned char serial[4]; /* Serial number buffer */ | |
3334 | time_t curtime; /* Current time */ | |
3335 | int result; /* Result of GNU TLS calls */ | |
3336 | ||
3337 | ||
3338 | /* | |
3339 | * Create the encryption key... | |
3340 | */ | |
3341 | ||
3342 | cupsdLogMessage(CUPSD_LOG_INFO, "Generating server key..."); | |
3343 | ||
3344 | gnutls_x509_privkey_init(&key); | |
3345 | gnutls_x509_privkey_generate(key, GNUTLS_PK_RSA, 2048, 0); | |
3346 | ||
3347 | /* | |
3348 | * Save it... | |
3349 | */ | |
3350 | ||
3351 | bytes = sizeof(buffer); | |
3352 | ||
3353 | if ((result = gnutls_x509_privkey_export(key, GNUTLS_X509_FMT_PEM, | |
3354 | buffer, &bytes)) < 0) | |
3355 | { | |
3356 | cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to export server key - %s", | |
3357 | gnutls_strerror(result)); | |
3358 | gnutls_x509_privkey_deinit(key); | |
3359 | return; | |
3360 | } | |
3361 | else if ((fp = cupsFileOpen(ServerKey, "w")) != NULL) | |
3362 | { | |
3363 | cupsFileWrite(fp, (char *)buffer, bytes); | |
3364 | cupsFileClose(fp); | |
3365 | ||
3366 | cupsdLogMessage(CUPSD_LOG_INFO, "Created server key file \"%s\"...", | |
3367 | ServerKey); | |
3368 | } | |
3369 | else | |
3370 | { | |
3371 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
3372 | "Unable to create server key file \"%s\" - %s", | |
3373 | ServerKey, strerror(errno)); | |
3374 | gnutls_x509_privkey_deinit(key); | |
3375 | return; | |
3376 | } | |
3377 | ||
3378 | /* | |
3379 | * Create the self-signed certificate... | |
3380 | */ | |
3381 | ||
3382 | cupsdLogMessage(CUPSD_LOG_INFO, "Generating self-signed certificate..."); | |
3383 | ||
3384 | language = cupsLangDefault(); | |
3385 | curtime = time(NULL); | |
3386 | serial[0] = curtime >> 24; | |
3387 | serial[1] = curtime >> 16; | |
3388 | serial[2] = curtime >> 8; | |
3389 | serial[3] = curtime; | |
3390 | ||
3391 | gnutls_x509_crt_init(&crt); | |
3392 | if (strlen(language->language) == 5) | |
3393 | gnutls_x509_crt_set_dn_by_oid(crt, GNUTLS_OID_X520_COUNTRY_NAME, 0, | |
3394 | language->language + 3, 2); | |
3395 | else | |
3396 | gnutls_x509_crt_set_dn_by_oid(crt, GNUTLS_OID_X520_COUNTRY_NAME, 0, | |
3397 | "US", 2); | |
3398 | gnutls_x509_crt_set_dn_by_oid(crt, GNUTLS_OID_X520_COMMON_NAME, 0, | |
3399 | ServerName, strlen(ServerName)); | |
3400 | gnutls_x509_crt_set_dn_by_oid(crt, GNUTLS_OID_X520_ORGANIZATION_NAME, 0, | |
3401 | ServerName, strlen(ServerName)); | |
3402 | gnutls_x509_crt_set_dn_by_oid(crt, GNUTLS_OID_X520_ORGANIZATIONAL_UNIT_NAME, | |
3403 | 0, "Unknown", 7); | |
3404 | gnutls_x509_crt_set_dn_by_oid(crt, GNUTLS_OID_X520_STATE_OR_PROVINCE_NAME, 0, | |
3405 | "Unknown", 7); | |
3406 | gnutls_x509_crt_set_dn_by_oid(crt, GNUTLS_OID_X520_LOCALITY_NAME, 0, | |
3407 | "Unknown", 7); | |
3408 | gnutls_x509_crt_set_dn_by_oid(crt, GNUTLS_OID_PKCS9_EMAIL, 0, | |
3409 | ServerAdmin, strlen(ServerAdmin)); | |
3410 | gnutls_x509_crt_set_key(crt, key); | |
3411 | gnutls_x509_crt_set_serial(crt, serial, sizeof(serial)); | |
3412 | gnutls_x509_crt_set_activation_time(crt, curtime); | |
3413 | gnutls_x509_crt_set_expiration_time(crt, curtime + 10 * 365 * 86400); | |
3414 | gnutls_x509_crt_set_ca_status(crt, 0); | |
3415 | gnutls_x509_crt_set_subject_alternative_name(crt, GNUTLS_SAN_DNSNAME, | |
3416 | ServerName); | |
3417 | gnutls_x509_crt_set_key_purpose_oid(crt, GNUTLS_KP_TLS_WWW_SERVER, 0); | |
3418 | gnutls_x509_crt_set_key_usage(crt, GNUTLS_KEY_KEY_ENCIPHERMENT); | |
3419 | gnutls_x509_crt_set_version(crt, 3); | |
3420 | ||
3421 | bytes = sizeof(buffer); | |
3422 | if (gnutls_x509_crt_get_key_id(crt, 0, buffer, &bytes) >= 0) | |
3423 | gnutls_x509_crt_set_subject_key_id(crt, buffer, bytes); | |
3424 | ||
3425 | gnutls_x509_crt_sign(crt, crt, key); | |
3426 | ||
3427 | /* | |
3428 | * Save it... | |
3429 | */ | |
3430 | ||
3431 | bytes = sizeof(buffer); | |
3432 | if ((result = gnutls_x509_crt_export(crt, GNUTLS_X509_FMT_PEM, | |
3433 | buffer, &bytes)) < 0) | |
3434 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
3435 | "Unable to export server certificate - %s", | |
3436 | gnutls_strerror(result)); | |
3437 | else if ((fp = cupsFileOpen(ServerCertificate, "w")) != NULL) | |
3438 | { | |
3439 | cupsFileWrite(fp, (char *)buffer, bytes); | |
3440 | cupsFileClose(fp); | |
3441 | ||
3442 | cupsdLogMessage(CUPSD_LOG_INFO, | |
3443 | "Created 10-year server certificate file \"%s\"...", | |
3444 | ServerCertificate); | |
3445 | } | |
3446 | else | |
3447 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
3448 | "Unable to create server certificate file \"%s\" - %s", | |
3449 | ServerCertificate, strerror(errno)); | |
3450 | ||
3451 | /* | |
3452 | * Cleanup... | |
3453 | */ | |
3454 | ||
3455 | gnutls_x509_crt_deinit(crt); | |
3456 | gnutls_x509_privkey_deinit(key); | |
3457 | } | |
3458 | #endif /* HAVE_GNUTLS */ | |
3459 | ||
3460 | ||
ef416fc2 | 3461 | /* |
3462 | * 'pipe_command()' - Pipe the output of a command to the remote client. | |
3463 | */ | |
3464 | ||
3465 | static int /* O - Process ID */ | |
3466 | pipe_command(cupsd_client_t *con, /* I - Client connection */ | |
3467 | int infile, /* I - Standard input for command */ | |
3468 | int *outfile, /* O - Standard output for command */ | |
3469 | char *command, /* I - Command to run */ | |
3470 | char *options, /* I - Options for command */ | |
3471 | int root) /* I - Run as root? */ | |
3472 | { | |
3473 | int i; /* Looping var */ | |
3474 | int pid; /* Process ID */ | |
b423cd4c | 3475 | char *commptr, /* Command string pointer */ |
3476 | commch; /* Command string character */ | |
ef416fc2 | 3477 | char *uriptr; /* URI string pointer */ |
3478 | int fds[2]; /* Pipe FDs */ | |
3479 | int argc; /* Number of arguments */ | |
3480 | int envc; /* Number of environment variables */ | |
3481 | char argbuf[10240], /* Argument buffer */ | |
3482 | *argv[100], /* Argument strings */ | |
b423cd4c | 3483 | *envp[MAX_ENV + 17]; /* Environment variables */ |
ef416fc2 | 3484 | char content_length[1024], /* CONTENT_LENGTH environment variable */ |
3485 | content_type[1024], /* CONTENT_TYPE environment variable */ | |
3486 | http_cookie[32768], /* HTTP_COOKIE environment variable */ | |
3487 | http_user_agent[1024], /* HTTP_USER_AGENT environment variable */ | |
3488 | lang[1024], /* LANG environment variable */ | |
b423cd4c | 3489 | path_info[1024], /* PATH_INFO environment variable */ |
ef416fc2 | 3490 | *query_string, /* QUERY_STRING env variable */ |
3491 | remote_addr[1024], /* REMOTE_ADDR environment variable */ | |
3492 | remote_host[1024], /* REMOTE_HOST environment variable */ | |
3493 | remote_user[1024], /* REMOTE_USER environment variable */ | |
3494 | script_name[1024], /* SCRIPT_NAME environment variable */ | |
3495 | server_name[1024], /* SERVER_NAME environment variable */ | |
3496 | server_port[1024]; /* SERVER_PORT environment variable */ | |
3497 | ||
3498 | ||
3499 | /* | |
3500 | * Parse a copy of the options string, which is of the form: | |
3501 | * | |
b423cd4c | 3502 | * argument+argument+argument |
3503 | * ?argument+argument+argument | |
3504 | * param=value¶m=value | |
3505 | * ?param=value¶m=value | |
3506 | * /name?argument+argument+argument | |
3507 | * /name?param=value¶m=value | |
ef416fc2 | 3508 | * |
3509 | * If the string contains an "=" character after the initial name, | |
3510 | * then we treat it as a HTTP GET form request and make a copy of | |
3511 | * the remaining string for the environment variable. | |
3512 | * | |
3513 | * The string is always parsed out as command-line arguments, to | |
3514 | * be consistent with Apache... | |
3515 | */ | |
3516 | ||
3517 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
3518 | "pipe_command: command=\"%s\", options=\"%s\"", | |
f301802f | 3519 | command, options ? options : "(null)"); |
ef416fc2 | 3520 | |
b423cd4c | 3521 | argv[0] = command; |
ef416fc2 | 3522 | query_string = NULL; |
3523 | ||
b423cd4c | 3524 | if (options) |
3525 | strlcpy(argbuf, options, sizeof(argbuf)); | |
3526 | else | |
3527 | argbuf[0] = '\0'; | |
3528 | ||
3529 | if (argbuf[0] == '/') | |
ef416fc2 | 3530 | { |
3531 | /* | |
b423cd4c | 3532 | * Found some trailing path information, set PATH_INFO... |
ef416fc2 | 3533 | */ |
3534 | ||
b423cd4c | 3535 | if ((commptr = strchr(argbuf, '?')) == NULL) |
3536 | commptr = argbuf + strlen(argbuf); | |
ef416fc2 | 3537 | |
b423cd4c | 3538 | commch = *commptr; |
3539 | *commptr = '\0'; | |
3540 | snprintf(path_info, sizeof(path_info), "PATH_INFO=%s", argbuf); | |
3541 | *commptr = commch; | |
3542 | } | |
3543 | else | |
3544 | { | |
3545 | commptr = argbuf; | |
3546 | path_info[0] = '\0'; | |
3547 | } | |
ef416fc2 | 3548 | |
b423cd4c | 3549 | if (*commptr == '?' && con->operation == HTTP_GET) |
3550 | { | |
3551 | commptr ++; | |
3552 | cupsdSetStringf(&query_string, "QUERY_STRING=%s", commptr); | |
3553 | } | |
ef416fc2 | 3554 | |
b423cd4c | 3555 | argc = 1; |
ef416fc2 | 3556 | |
b423cd4c | 3557 | if (*commptr) |
3558 | { | |
3559 | argv[argc ++] = commptr; | |
ef416fc2 | 3560 | |
b423cd4c | 3561 | for (; *commptr && argc < 99; commptr ++) |
3562 | { | |
ef416fc2 | 3563 | /* |
b423cd4c | 3564 | * Break arguments whenever we see a + or space... |
ef416fc2 | 3565 | */ |
3566 | ||
b423cd4c | 3567 | if (*commptr == ' ' || *commptr == '+') |
3568 | { | |
3569 | while (*commptr == ' ' || *commptr == '+') | |
3570 | *commptr++ = '\0'; | |
ef416fc2 | 3571 | |
b423cd4c | 3572 | /* |
3573 | * If we don't have a blank string, save it as another argument... | |
3574 | */ | |
ef416fc2 | 3575 | |
b423cd4c | 3576 | if (*commptr) |
3577 | { | |
3578 | argv[argc] = commptr; | |
3579 | argc ++; | |
3580 | } | |
3581 | else | |
3582 | break; | |
3583 | } | |
3584 | else if (*commptr == '%' && isxdigit(commptr[1] & 255) && | |
3585 | isxdigit(commptr[2] & 255)) | |
3586 | { | |
3587 | /* | |
3588 | * Convert the %xx notation to the individual character. | |
3589 | */ | |
ef416fc2 | 3590 | |
b423cd4c | 3591 | if (commptr[1] >= '0' && commptr[1] <= '9') |
3592 | *commptr = (commptr[1] - '0') << 4; | |
3593 | else | |
3594 | *commptr = (tolower(commptr[1]) - 'a' + 10) << 4; | |
ef416fc2 | 3595 | |
b423cd4c | 3596 | if (commptr[2] >= '0' && commptr[2] <= '9') |
3597 | *commptr |= commptr[2] - '0'; | |
3598 | else | |
3599 | *commptr |= tolower(commptr[2]) - 'a' + 10; | |
ef416fc2 | 3600 | |
b423cd4c | 3601 | _cups_strcpy(commptr + 1, commptr + 3); |
ef416fc2 | 3602 | |
b423cd4c | 3603 | /* |
3604 | * Check for a %00 and break if that is the case... | |
3605 | */ | |
ef416fc2 | 3606 | |
b423cd4c | 3607 | if (!*commptr) |
3608 | break; | |
3609 | } | |
ef416fc2 | 3610 | } |
3611 | } | |
3612 | ||
3613 | argv[argc] = NULL; | |
3614 | ||
ef416fc2 | 3615 | /* |
3616 | * Setup the environment variables as needed... | |
3617 | */ | |
3618 | ||
3619 | if (con->language) | |
3620 | snprintf(lang, sizeof(lang), "LANG=%s.UTF-8", con->language->language); | |
3621 | else | |
3622 | strcpy(lang, "LANG=C"); | |
3623 | ||
3624 | strcpy(remote_addr, "REMOTE_ADDR="); | |
3625 | httpAddrString(con->http.hostaddr, remote_addr + 12, | |
3626 | sizeof(remote_addr) - 12); | |
3627 | ||
3628 | snprintf(remote_host, sizeof(remote_host), "REMOTE_HOST=%s", | |
3629 | con->http.hostname); | |
3630 | ||
3631 | snprintf(script_name, sizeof(script_name), "SCRIPT_NAME=%s", con->uri); | |
3632 | if ((uriptr = strchr(script_name, '?')) != NULL) | |
3633 | *uriptr = '\0'; | |
3634 | ||
3635 | sprintf(server_port, "SERVER_PORT=%d", con->serverport); | |
3636 | ||
3637 | snprintf(server_name, sizeof(server_name), "SERVER_NAME=%s", | |
3638 | con->servername); | |
3639 | ||
3640 | envc = cupsdLoadEnv(envp, (int)(sizeof(envp) / sizeof(envp[0]))); | |
3641 | ||
3642 | envp[envc ++] = lang; | |
3643 | envp[envc ++] = "REDIRECT_STATUS=1"; | |
3644 | envp[envc ++] = server_name; | |
3645 | envp[envc ++] = server_port; | |
3646 | envp[envc ++] = remote_addr; | |
3647 | envp[envc ++] = remote_host; | |
3648 | envp[envc ++] = script_name; | |
3649 | ||
b423cd4c | 3650 | if (path_info[0]) |
3651 | envp[envc ++] = path_info; | |
3652 | ||
ef416fc2 | 3653 | if (con->username[0]) |
3654 | { | |
3655 | snprintf(remote_user, sizeof(remote_user), "REMOTE_USER=%s", con->username); | |
3656 | ||
3657 | envp[envc ++] = remote_user; | |
3658 | } | |
3659 | ||
3660 | if (con->http.version == HTTP_1_1) | |
3661 | envp[envc ++] = "SERVER_PROTOCOL=HTTP/1.1"; | |
3662 | else if (con->http.version == HTTP_1_0) | |
3663 | envp[envc ++] = "SERVER_PROTOCOL=HTTP/1.0"; | |
3664 | else | |
3665 | envp[envc ++] = "SERVER_PROTOCOL=HTTP/0.9"; | |
3666 | ||
3667 | if (con->http.cookie) | |
3668 | { | |
3669 | snprintf(http_cookie, sizeof(http_cookie), "HTTP_COOKIE=%s", | |
3670 | con->http.cookie); | |
3671 | envp[envc ++] = http_cookie; | |
3672 | } | |
3673 | ||
3674 | if (con->http.fields[HTTP_FIELD_USER_AGENT][0]) | |
3675 | { | |
3676 | snprintf(http_user_agent, sizeof(http_user_agent), "HTTP_USER_AGENT=%s", | |
3677 | con->http.fields[HTTP_FIELD_USER_AGENT]); | |
3678 | envp[envc ++] = http_user_agent; | |
3679 | } | |
3680 | ||
3681 | if (con->operation == HTTP_GET) | |
3682 | { | |
3683 | for (i = 0; i < argc; i ++) | |
3684 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "argv[%d] = \"%s\"", i, argv[i]); | |
3685 | ||
3686 | envp[envc ++] = "REQUEST_METHOD=GET"; | |
3687 | ||
3688 | if (query_string) | |
3689 | { | |
3690 | /* | |
3691 | * Add GET form variables after ?... | |
3692 | */ | |
3693 | ||
3694 | envp[envc ++] = query_string; | |
3695 | } | |
3696 | } | |
3697 | else | |
3698 | { | |
e1d6a774 | 3699 | sprintf(content_length, "CONTENT_LENGTH=" CUPS_LLFMT, |
3700 | CUPS_LLCAST con->bytes); | |
ef416fc2 | 3701 | snprintf(content_type, sizeof(content_type), "CONTENT_TYPE=%s", |
3702 | con->http.fields[HTTP_FIELD_CONTENT_TYPE]); | |
3703 | ||
3704 | envp[envc ++] = "REQUEST_METHOD=POST"; | |
3705 | envp[envc ++] = content_length; | |
3706 | envp[envc ++] = content_type; | |
3707 | } | |
3708 | ||
3709 | /* | |
3710 | * Tell the CGI if we are using encryption... | |
3711 | */ | |
3712 | ||
a74454a7 | 3713 | if (con->http.tls) |
ef416fc2 | 3714 | envp[envc ++] = "HTTPS=ON"; |
3715 | ||
3716 | /* | |
3717 | * Terminate the environment array... | |
3718 | */ | |
3719 | ||
3720 | envp[envc] = NULL; | |
3721 | ||
3722 | if (LogLevel == CUPSD_LOG_DEBUG2) | |
3723 | { | |
3724 | for (i = 0; i < argc; i ++) | |
3725 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
3726 | "pipe_command: argv[%d] = \"%s\"", i, argv[i]); | |
3727 | for (i = 0; i < envc; i ++) | |
3728 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
3729 | "pipe_command: envp[%d] = \"%s\"", i, envp[i]); | |
3730 | } | |
3731 | ||
3732 | /* | |
3733 | * Create a pipe for the output... | |
3734 | */ | |
3735 | ||
3736 | if (cupsdOpenPipe(fds)) | |
3737 | { | |
3738 | cupsdClearString(&query_string); | |
3739 | ||
3740 | cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to create pipes for CGI %s - %s", | |
3741 | argv[0], strerror(errno)); | |
3742 | return (0); | |
3743 | } | |
3744 | ||
3745 | /* | |
3746 | * Then execute the command... | |
3747 | */ | |
3748 | ||
3749 | if (cupsdStartProcess(command, argv, envp, infile, fds[1], CGIPipes[1], | |
3750 | -1, root, &pid) < 0) | |
3751 | { | |
3752 | /* | |
3753 | * Error - can't fork! | |
3754 | */ | |
3755 | ||
3756 | cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to fork for CGI %s - %s", argv[0], | |
3757 | strerror(errno)); | |
3758 | ||
3759 | cupsdClosePipe(fds); | |
3760 | pid = 0; | |
3761 | } | |
3762 | else | |
3763 | { | |
3764 | /* | |
3765 | * Fork successful - return the PID... | |
3766 | */ | |
3767 | ||
3768 | if (con->username[0]) | |
3769 | cupsdAddCert(pid, con->username); | |
3770 | ||
3771 | cupsdLogMessage(CUPSD_LOG_DEBUG, "CGI %s started - PID = %d", command, pid); | |
3772 | ||
3773 | *outfile = fds[0]; | |
3774 | close(fds[1]); | |
3775 | } | |
3776 | ||
3777 | cupsdClearString(&query_string); | |
3778 | ||
3779 | return (pid); | |
3780 | } | |
3781 | ||
3782 | ||
3783 | /* | |
a74454a7 | 3784 | * 'write_file()' - Send a file via HTTP. |
e1d6a774 | 3785 | */ |
3786 | ||
3787 | static int /* O - 0 on failure, 1 on success */ | |
a74454a7 | 3788 | write_file(cupsd_client_t *con, /* I - Client connection */ |
3789 | http_status_t code, /* I - HTTP status */ | |
3790 | char *filename, /* I - Filename */ | |
3791 | char *type, /* I - File type */ | |
3792 | struct stat *filestats) /* O - File information */ | |
e1d6a774 | 3793 | { |
3794 | con->file = open(filename, O_RDONLY); | |
3795 | ||
a74454a7 | 3796 | cupsdLogMessage(CUPSD_LOG_DEBUG, "write_file: %d file=%d", con->http.fd, |
e1d6a774 | 3797 | con->file); |
3798 | ||
3799 | if (con->file < 0) | |
3800 | return (0); | |
3801 | ||
3802 | fcntl(con->file, F_SETFD, fcntl(con->file, F_GETFD) | FD_CLOEXEC); | |
3803 | ||
3804 | con->pipe_pid = 0; | |
3805 | ||
3806 | if (!cupsdSendHeader(con, code, type)) | |
3807 | return (0); | |
3808 | ||
3809 | if (httpPrintf(HTTP(con), "Last-Modified: %s\r\n", | |
3810 | httpGetDateString(filestats->st_mtime)) < 0) | |
3811 | return (0); | |
3812 | if (httpPrintf(HTTP(con), "Content-Length: " CUPS_LLFMT "\r\n", | |
3813 | CUPS_LLCAST filestats->st_size) < 0) | |
3814 | return (0); | |
3815 | if (httpPrintf(HTTP(con), "\r\n") < 0) | |
3816 | return (0); | |
3817 | ||
3818 | con->http.data_encoding = HTTP_ENCODE_LENGTH; | |
3819 | con->http.data_remaining = filestats->st_size; | |
3820 | ||
3821 | if (con->http.data_remaining <= INT_MAX) | |
3822 | con->http._data_remaining = con->http.data_remaining; | |
3823 | else | |
3824 | con->http._data_remaining = INT_MAX; | |
3825 | ||
3826 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
a74454a7 | 3827 | "write_file: Adding fd %d to OutputSet...", con->http.fd); |
e1d6a774 | 3828 | |
3829 | FD_SET(con->http.fd, OutputSet); | |
3830 | ||
3831 | return (1); | |
3832 | } | |
3833 | ||
3834 | ||
3835 | /* | |
c07d5b2d | 3836 | * End of "$Id: client.c 181 2006-06-22 20:01:18Z jlovell $". |
ef416fc2 | 3837 | */ |