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