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