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