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