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