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