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