forked from apple/cups
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.c
3772 lines (3077 loc) · 94.8 KB
/
client.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Client routines for the CUPS scheduler.
*
* Copyright © 2007-2019 by Apple Inc.
* Copyright © 1997-2007 by Easy Software Products, all rights reserved.
*
* This file contains Kerberos support code, copyright 2006 by
* Jelmer Vernooij.
*
* Licensed under Apache License v2.0. See the file "LICENSE" for more
* information.
*/
/*
* Include necessary headers...
*/
#define _CUPS_NO_DEPRECATED
#define _HTTP_NO_PRIVATE
#include "cupsd.h"
#ifdef __APPLE__
# include <libproc.h>
#endif /* __APPLE__ */
#ifdef HAVE_TCPD_H
# include <tcpd.h>
#endif /* HAVE_TCPD_H */
/*
* Local functions...
*/
static int check_if_modified(cupsd_client_t *con,
struct stat *filestats);
static int compare_clients(cupsd_client_t *a, cupsd_client_t *b,
void *data);
#ifdef HAVE_SSL
static int cupsd_start_tls(cupsd_client_t *con, http_encryption_t e);
#endif /* HAVE_SSL */
static char *get_file(cupsd_client_t *con, struct stat *filestats,
char *filename, size_t len);
static http_status_t install_cupsd_conf(cupsd_client_t *con);
static int is_cgi(cupsd_client_t *con, const char *filename,
struct stat *filestats, mime_type_t *type);
static int is_path_absolute(const char *path);
static int pipe_command(cupsd_client_t *con, int infile, int *outfile,
char *command, char *options, int root);
static int valid_host(cupsd_client_t *con);
static int write_file(cupsd_client_t *con, http_status_t code,
char *filename, char *type,
struct stat *filestats);
static void write_pipe(cupsd_client_t *con);
/*
* 'cupsdAcceptClient()' - Accept a new client.
*/
void
cupsdAcceptClient(cupsd_listener_t *lis)/* I - Listener socket */
{
const char *hostname; /* Hostname of client */
char name[256]; /* Hostname of client */
int count; /* Count of connections on a host */
cupsd_client_t *con, /* New client pointer */
*tempcon; /* Temporary client pointer */
socklen_t addrlen; /* Length of address */
http_addr_t temp; /* Temporary address variable */
static time_t last_dos = 0; /* Time of last DoS attack */
#ifdef HAVE_TCPD_H
struct request_info wrap_req; /* TCP wrappers request information */
#endif /* HAVE_TCPD_H */
cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdAcceptClient(lis=%p(%d)) Clients=%d", lis, lis->fd, cupsArrayCount(Clients));
/*
* Make sure we don't have a full set of clients already...
*/
if (cupsArrayCount(Clients) == MaxClients)
return;
cupsdSetBusyState(1);
/*
* Get a pointer to the next available client...
*/
if (!Clients)
Clients = cupsArrayNew(NULL, NULL);
if (!Clients)
{
cupsdLogMessage(CUPSD_LOG_ERROR,
"Unable to allocate memory for clients array!");
cupsdPauseListening();
return;
}
if (!ActiveClients)
ActiveClients = cupsArrayNew((cups_array_func_t)compare_clients, NULL);
if (!ActiveClients)
{
cupsdLogMessage(CUPSD_LOG_ERROR,
"Unable to allocate memory for active clients array!");
cupsdPauseListening();
return;
}
if ((con = calloc(1, sizeof(cupsd_client_t))) == NULL)
{
cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to allocate memory for client!");
cupsdPauseListening();
return;
}
/*
* Accept the client and get the remote address...
*/
con->number = ++ LastClientNumber;
con->file = -1;
if ((con->http = httpAcceptConnection(lis->fd, 0)) == NULL)
{
if (errno == ENFILE || errno == EMFILE)
cupsdPauseListening();
cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to accept client connection - %s.",
strerror(errno));
free(con);
return;
}
/*
* Save the connected address and port number...
*/
addrlen = sizeof(con->clientaddr);
if (getsockname(httpGetFd(con->http), (struct sockaddr *)&con->clientaddr, &addrlen) || addrlen == 0)
con->clientaddr = lis->address;
cupsdLogClient(con, CUPSD_LOG_DEBUG, "Server address is \"%s\".", httpAddrString(&con->clientaddr, name, sizeof(name)));
/*
* Check the number of clients on the same address...
*/
for (count = 0, tempcon = (cupsd_client_t *)cupsArrayFirst(Clients);
tempcon;
tempcon = (cupsd_client_t *)cupsArrayNext(Clients))
if (httpAddrEqual(httpGetAddress(tempcon->http), httpGetAddress(con->http)))
{
count ++;
if (count >= MaxClientsPerHost)
break;
}
if (count >= MaxClientsPerHost)
{
if ((time(NULL) - last_dos) >= 60)
{
last_dos = time(NULL);
cupsdLogMessage(CUPSD_LOG_WARN,
"Possible DoS attack - more than %d clients connecting "
"from %s.",
MaxClientsPerHost,
httpGetHostname(con->http, name, sizeof(name)));
}
httpClose(con->http);
free(con);
return;
}
/*
* Get the hostname or format the IP address as needed...
*/
if (HostNameLookups)
hostname = httpResolveHostname(con->http, NULL, 0);
else
hostname = httpGetHostname(con->http, NULL, 0);
if (hostname == NULL && HostNameLookups == 2)
{
/*
* Can't have an unresolved IP address with double-lookups enabled...
*/
httpClose(con->http);
cupsdLogClient(con, CUPSD_LOG_WARN,
"Name lookup failed - connection from %s closed!",
httpGetHostname(con->http, NULL, 0));
free(con);
return;
}
if (HostNameLookups == 2)
{
/*
* Do double lookups as needed...
*/
http_addrlist_t *addrlist, /* List of addresses */
*addr; /* Current address */
if ((addrlist = httpAddrGetList(hostname, AF_UNSPEC, NULL)) != NULL)
{
/*
* See if the hostname maps to the same IP address...
*/
for (addr = addrlist; addr; addr = addr->next)
if (httpAddrEqual(httpGetAddress(con->http), &(addr->addr)))
break;
}
else
addr = NULL;
httpAddrFreeList(addrlist);
if (!addr)
{
/*
* Can't have a hostname that doesn't resolve to the same IP address
* with double-lookups enabled...
*/
httpClose(con->http);
cupsdLogClient(con, CUPSD_LOG_WARN,
"IP lookup failed - connection from %s closed!",
httpGetHostname(con->http, NULL, 0));
free(con);
return;
}
}
#ifdef HAVE_TCPD_H
/*
* See if the connection is denied by TCP wrappers...
*/
request_init(&wrap_req, RQ_DAEMON, "cupsd", RQ_FILE, httpGetFd(con->http),
NULL);
fromhost(&wrap_req);
if (!hosts_access(&wrap_req))
{
httpClose(con->http);
cupsdLogClient(con, CUPSD_LOG_WARN,
"Connection from %s refused by /etc/hosts.allow and "
"/etc/hosts.deny rules.", httpGetHostname(con->http, NULL, 0));
free(con);
return;
}
#endif /* HAVE_TCPD_H */
#ifdef AF_LOCAL
if (httpAddrFamily(httpGetAddress(con->http)) == AF_LOCAL)
{
# ifdef __APPLE__
socklen_t peersize; /* Size of peer credentials */
pid_t peerpid; /* Peer process ID */
char peername[256]; /* Name of process */
peersize = sizeof(peerpid);
if (!getsockopt(httpGetFd(con->http), SOL_LOCAL, LOCAL_PEERPID, &peerpid,
&peersize))
{
if (!proc_name((int)peerpid, peername, sizeof(peername)))
cupsdLogClient(con, CUPSD_LOG_DEBUG,
"Accepted from %s (Domain ???[%d])",
httpGetHostname(con->http, NULL, 0), (int)peerpid);
else
cupsdLogClient(con, CUPSD_LOG_DEBUG,
"Accepted from %s (Domain %s[%d])",
httpGetHostname(con->http, NULL, 0), peername, (int)peerpid);
}
else
# endif /* __APPLE__ */
cupsdLogClient(con, CUPSD_LOG_DEBUG, "Accepted from %s (Domain)",
httpGetHostname(con->http, NULL, 0));
}
else
#endif /* AF_LOCAL */
cupsdLogClient(con, CUPSD_LOG_DEBUG, "Accepted from %s:%d (IPv%d)",
httpGetHostname(con->http, NULL, 0),
httpAddrPort(httpGetAddress(con->http)),
httpAddrFamily(httpGetAddress(con->http)) == AF_INET ? 4 : 6);
/*
* Get the local address the client connected to...
*/
addrlen = sizeof(temp);
if (getsockname(httpGetFd(con->http), (struct sockaddr *)&temp, &addrlen))
{
cupsdLogClient(con, CUPSD_LOG_ERROR, "Unable to get local address - %s",
strerror(errno));
strlcpy(con->servername, "localhost", sizeof(con->servername));
con->serverport = LocalPort;
}
#ifdef AF_LOCAL
else if (httpAddrFamily(&temp) == AF_LOCAL)
{
strlcpy(con->servername, "localhost", sizeof(con->servername));
con->serverport = LocalPort;
}
#endif /* AF_LOCAL */
else
{
if (httpAddrLocalhost(&temp))
strlcpy(con->servername, "localhost", sizeof(con->servername));
else if (HostNameLookups)
httpAddrLookup(&temp, con->servername, sizeof(con->servername));
else
httpAddrString(&temp, con->servername, sizeof(con->servername));
con->serverport = httpAddrPort(&(lis->address));
}
/*
* Add the connection to the array of active clients...
*/
cupsArrayAdd(Clients, con);
/*
* Add the socket to the server select.
*/
cupsdAddSelect(httpGetFd(con->http), (cupsd_selfunc_t)cupsdReadClient, NULL,
con);
cupsdLogClient(con, CUPSD_LOG_DEBUG, "Waiting for request.");
/*
* Temporarily suspend accept()'s until we lose a client...
*/
if (cupsArrayCount(Clients) == MaxClients)
cupsdPauseListening();
#ifdef HAVE_SSL
/*
* See if we are connecting on a secure port...
*/
if (lis->encryption == HTTP_ENCRYPTION_ALWAYS)
{
/*
* https connection; go secure...
*/
if (cupsd_start_tls(con, HTTP_ENCRYPTION_ALWAYS))
cupsdCloseClient(con);
}
else
con->auto_ssl = 1;
#endif /* HAVE_SSL */
}
/*
* 'cupsdCloseAllClients()' - Close all remote clients immediately.
*/
void
cupsdCloseAllClients(void)
{
cupsd_client_t *con; /* Current client */
cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdCloseAllClients() Clients=%d", cupsArrayCount(Clients));
for (con = (cupsd_client_t *)cupsArrayFirst(Clients);
con;
con = (cupsd_client_t *)cupsArrayNext(Clients))
if (cupsdCloseClient(con))
cupsdCloseClient(con);
}
/*
* 'cupsdCloseClient()' - Close a remote client.
*/
int /* O - 1 if partial close, 0 if fully closed */
cupsdCloseClient(cupsd_client_t *con) /* I - Client to close */
{
int partial; /* Do partial close for SSL? */
cupsdLogClient(con, CUPSD_LOG_DEBUG, "Closing connection.");
/*
* Flush pending writes before closing...
*/
httpFlushWrite(con->http);
partial = 0;
if (con->pipe_pid != 0)
{
/*
* Stop any CGI process...
*/
cupsdEndProcess(con->pipe_pid, 1);
con->pipe_pid = 0;
}
if (con->file >= 0)
{
cupsdRemoveSelect(con->file);
close(con->file);
con->file = -1;
}
/*
* Close the socket and clear the file from the input set for select()...
*/
if (httpGetFd(con->http) >= 0)
{
cupsArrayRemove(ActiveClients, con);
cupsdSetBusyState(0);
#ifdef HAVE_SSL
/*
* Shutdown encryption as needed...
*/
if (httpIsEncrypted(con->http))
partial = 1;
#endif /* HAVE_SSL */
if (partial)
{
/*
* Only do a partial close so that the encrypted client gets everything.
*/
httpShutdown(con->http);
cupsdAddSelect(httpGetFd(con->http), (cupsd_selfunc_t)cupsdReadClient,
NULL, con);
cupsdLogClient(con, CUPSD_LOG_DEBUG, "Waiting for socket close.");
}
else
{
/*
* Shut the socket down fully...
*/
cupsdRemoveSelect(httpGetFd(con->http));
httpClose(con->http);
con->http = NULL;
}
}
if (!partial)
{
/*
* Free memory...
*/
cupsdRemoveSelect(httpGetFd(con->http));
httpClose(con->http);
if (con->filename)
{
unlink(con->filename);
cupsdClearString(&con->filename);
}
cupsdClearString(&con->command);
cupsdClearString(&con->options);
cupsdClearString(&con->query_string);
if (con->request)
{
ippDelete(con->request);
con->request = NULL;
}
if (con->response)
{
ippDelete(con->response);
con->response = NULL;
}
if (con->language)
{
cupsLangFree(con->language);
con->language = NULL;
}
#ifdef HAVE_AUTHORIZATION_H
if (con->authref)
{
AuthorizationFree(con->authref, kAuthorizationFlagDefaults);
con->authref = NULL;
}
#endif /* HAVE_AUTHORIZATION_H */
/*
* Re-enable new client connections if we are going back under the
* limit...
*/
if (cupsArrayCount(Clients) == MaxClients)
cupsdResumeListening();
/*
* Compact the list of clients as necessary...
*/
cupsArrayRemove(Clients, con);
free(con);
}
return (partial);
}
/*
* 'cupsdReadClient()' - Read data from a client.
*/
void
cupsdReadClient(cupsd_client_t *con) /* I - Client to read from */
{
char line[32768], /* Line from client... */
locale[64], /* Locale */
*ptr; /* Pointer into strings */
http_status_t status; /* Transfer status */
ipp_state_t ipp_state; /* State of IPP transfer */
int bytes; /* Number of bytes to POST */
char *filename; /* Name of file for GET/HEAD */
char buf[1024]; /* Buffer for real filename */
struct stat filestats; /* File information */
mime_type_t *type; /* MIME type of file */
static unsigned request_id = 0; /* Request ID for temp files */
status = HTTP_STATUS_CONTINUE;
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);
if (httpError(con->http) == EPIPE && !httpGetReady(con->http) && recv(httpGetFd(con->http), buf, 1, MSG_PEEK) < 1)
{
/*
* Connection closed...
*/
cupsdLogClient(con, CUPSD_LOG_DEBUG, "Closing on EOF.");
cupsdCloseClient(con);
return;
}
if (httpGetState(con->http) == HTTP_STATE_GET_SEND ||
httpGetState(con->http) == HTTP_STATE_POST_SEND ||
httpGetState(con->http) == HTTP_STATE_STATUS)
{
/*
* If we get called in the wrong state, then something went wrong with the
* connection and we need to shut it down...
*/
cupsdLogClient(con, CUPSD_LOG_DEBUG, "Closing on unexpected HTTP read state %s.", httpStateString(httpGetState(con->http)));
cupsdCloseClient(con);
return;
}
#ifdef HAVE_SSL
if (con->auto_ssl)
{
/*
* Automatically check for a SSL/TLS handshake...
*/
con->auto_ssl = 0;
if (recv(httpGetFd(con->http), buf, 1, MSG_PEEK) == 1 &&
(!buf[0] || !strchr("DGHOPT", buf[0])))
{
/*
* Encrypt this connection...
*/
cupsdLogClient(con, CUPSD_LOG_DEBUG2, "Saw first byte %02X, auto-negotiating SSL/TLS session.", buf[0] & 255);
if (cupsd_start_tls(con, HTTP_ENCRYPTION_ALWAYS))
cupsdCloseClient(con);
return;
}
}
#endif /* HAVE_SSL */
switch (httpGetState(con->http))
{
case HTTP_STATE_WAITING :
/*
* See if we've received a request line...
*/
con->operation = httpReadRequest(con->http, con->uri, sizeof(con->uri));
if (con->operation == HTTP_STATE_ERROR ||
con->operation == HTTP_STATE_UNKNOWN_METHOD ||
con->operation == HTTP_STATE_UNKNOWN_VERSION)
{
if (httpError(con->http))
cupsdLogClient(con, CUPSD_LOG_DEBUG,
"HTTP_STATE_WAITING Closing for error %d (%s)",
httpError(con->http), strerror(httpError(con->http)));
else
cupsdLogClient(con, CUPSD_LOG_DEBUG,
"HTTP_STATE_WAITING Closing on error: %s",
cupsLastErrorString());
cupsdCloseClient(con);
return;
}
/*
* Ignore blank request lines...
*/
if (con->operation == HTTP_STATE_WAITING)
break;
/*
* Clear other state variables...
*/
con->bytes = 0;
con->file = -1;
con->file_ready = 0;
con->pipe_pid = 0;
con->username[0] = '\0';
con->password[0] = '\0';
cupsdClearString(&con->command);
cupsdClearString(&con->options);
cupsdClearString(&con->query_string);
if (con->request)
{
ippDelete(con->request);
con->request = NULL;
}
if (con->response)
{
ippDelete(con->response);
con->response = NULL;
}
if (con->language)
{
cupsLangFree(con->language);
con->language = NULL;
}
#ifdef HAVE_GSSAPI
con->have_gss = 0;
con->gss_uid = 0;
#endif /* HAVE_GSSAPI */
/*
* Handle full URLs in the request line...
*/
if (strcmp(con->uri, "*"))
{
char scheme[HTTP_MAX_URI], /* Method/scheme */
userpass[HTTP_MAX_URI], /* Username:password */
hostname[HTTP_MAX_URI], /* Hostname */
resource[HTTP_MAX_URI]; /* Resource path */
int port; /* Port number */
/*
* Separate the URI into its components...
*/
if (httpSeparateURI(HTTP_URI_CODING_MOST, con->uri,
scheme, sizeof(scheme),
userpass, sizeof(userpass),
hostname, sizeof(hostname), &port,
resource, sizeof(resource)) < HTTP_URI_STATUS_OK)
{
cupsdLogClient(con, CUPSD_LOG_ERROR, "Bad URI \"%s\" in request.",
con->uri);
cupsdSendError(con, HTTP_STATUS_METHOD_NOT_ALLOWED, CUPSD_AUTH_NONE);
cupsdCloseClient(con);
return;
}
/*
* Only allow URIs with the servername, localhost, or an IP
* address...
*/
if (strcmp(scheme, "file") &&
_cups_strcasecmp(hostname, ServerName) &&
_cups_strcasecmp(hostname, "localhost") &&
!cupsArrayFind(ServerAlias, hostname) &&
!isdigit(hostname[0]) && hostname[0] != '[')
{
/*
* Nope, we don't do proxies...
*/
cupsdLogClient(con, CUPSD_LOG_ERROR, "Bad URI \"%s\" in request.",
con->uri);
cupsdSendError(con, HTTP_STATUS_METHOD_NOT_ALLOWED, CUPSD_AUTH_NONE);
cupsdCloseClient(con);
return;
}
/*
* Copy the resource portion back into the URI; both resource and
* con->uri are HTTP_MAX_URI bytes in size...
*/
strlcpy(con->uri, resource, sizeof(con->uri));
}
/*
* Process the request...
*/
gettimeofday(&(con->start), NULL);
cupsdLogClient(con, CUPSD_LOG_DEBUG, "%s %s HTTP/%d.%d",
httpStateString(con->operation) + 11, con->uri,
httpGetVersion(con->http) / 100,
httpGetVersion(con->http) % 100);
if (!cupsArrayFind(ActiveClients, con))
{
cupsArrayAdd(ActiveClients, con);
cupsdSetBusyState(0);
}
case HTTP_STATE_OPTIONS :
case HTTP_STATE_DELETE :
case HTTP_STATE_GET :
case HTTP_STATE_HEAD :
case HTTP_STATE_POST :
case HTTP_STATE_PUT :
case HTTP_STATE_TRACE :
/*
* Parse incoming parameters until the status changes...
*/
while ((status = httpUpdate(con->http)) == HTTP_STATUS_CONTINUE)
if (!httpGetReady(con->http))
break;
if (status != HTTP_STATUS_OK && status != HTTP_STATUS_CONTINUE)
{
if (httpError(con->http) && httpError(con->http) != EPIPE)
cupsdLogClient(con, CUPSD_LOG_DEBUG,
"Closing for error %d (%s) while reading headers.",
httpError(con->http), strerror(httpError(con->http)));
else
cupsdLogClient(con, CUPSD_LOG_DEBUG,
"Closing on EOF while reading headers.");
cupsdSendError(con, HTTP_STATUS_BAD_REQUEST, CUPSD_AUTH_NONE);
cupsdCloseClient(con);
return;
}
break;
default :
if (!httpGetReady(con->http) && recv(httpGetFd(con->http), buf, 1, MSG_PEEK) < 1)
{
/*
* Connection closed...
*/
cupsdLogClient(con, CUPSD_LOG_DEBUG, "Closing on EOF.");
cupsdCloseClient(con);
return;
}
break; /* Anti-compiler-warning-code */
}
/*
* Handle new transfers...
*/
cupsdLogClient(con, CUPSD_LOG_DEBUG, "Read: status=%d, state=%d", status, httpGetState(con->http));
if (status == HTTP_STATUS_OK)
{
/*
* Record whether the client is a web browser. "Mozilla" was the original
* and it seems that every web browser in existence now uses that as the
* prefix with additional information identifying *which* browser.
*
* Chrome (at least) has problems with multiple WWW-Authenticate values in
* a single header, so we only report Basic or Negotiate to web browsers and
* leave the multiple choices to the native CUPS client...
*/
con->is_browser = !strncmp(httpGetField(con->http, HTTP_FIELD_USER_AGENT), "Mozilla/", 8);
if (httpGetField(con->http, HTTP_FIELD_ACCEPT_LANGUAGE)[0])
{
/*
* Figure out the locale from the Accept-Language and Content-Type
* fields...
*/
if ((ptr = strchr(httpGetField(con->http, HTTP_FIELD_ACCEPT_LANGUAGE),
',')) != NULL)
*ptr = '\0';
if ((ptr = strchr(httpGetField(con->http, HTTP_FIELD_ACCEPT_LANGUAGE),
';')) != NULL)
*ptr = '\0';
if ((ptr = strstr(httpGetField(con->http, HTTP_FIELD_CONTENT_TYPE),
"charset=")) != NULL)
{
/*
* Combine language and charset, and trim any extra params in the
* content-type.
*/
snprintf(locale, sizeof(locale), "%s.%s",
httpGetField(con->http, HTTP_FIELD_ACCEPT_LANGUAGE), ptr + 8);
if ((ptr = strchr(locale, ',')) != NULL)
*ptr = '\0';
}
else
snprintf(locale, sizeof(locale), "%s.UTF-8",
httpGetField(con->http, HTTP_FIELD_ACCEPT_LANGUAGE));
con->language = cupsLangGet(locale);
}
else
con->language = cupsLangGet(DefaultLocale);
cupsdAuthorize(con);
if (!_cups_strncasecmp(httpGetField(con->http, HTTP_FIELD_CONNECTION),
"Keep-Alive", 10) && KeepAlive)
httpSetKeepAlive(con->http, HTTP_KEEPALIVE_ON);
else if (!_cups_strncasecmp(httpGetField(con->http, HTTP_FIELD_CONNECTION),
"close", 5))
httpSetKeepAlive(con->http, HTTP_KEEPALIVE_OFF);
if (!httpGetField(con->http, HTTP_FIELD_HOST)[0] &&
httpGetVersion(con->http) >= HTTP_VERSION_1_1)
{
/*
* HTTP/1.1 and higher require the "Host:" field...
*/
if (!cupsdSendError(con, HTTP_STATUS_BAD_REQUEST, CUPSD_AUTH_NONE))
{
cupsdLogClient(con, CUPSD_LOG_ERROR, "Missing Host: field in request.");
cupsdCloseClient(con);
return;
}
}
else if (!valid_host(con))
{
/*
* Access to localhost must use "localhost" or the corresponding IPv4
* or IPv6 values in the Host: field.
*/
cupsdLogClient(con, CUPSD_LOG_ERROR,
"Request from \"%s\" using invalid Host: field \"%s\".",
httpGetHostname(con->http, NULL, 0), httpGetField(con->http, HTTP_FIELD_HOST));
if (!cupsdSendError(con, HTTP_STATUS_BAD_REQUEST, CUPSD_AUTH_NONE))
{
cupsdCloseClient(con);
return;
}
}
else if (con->operation == HTTP_STATE_OPTIONS)
{
/*
* Do OPTIONS command...
*/
if (con->best && con->best->type != CUPSD_AUTH_NONE)
{
httpClearFields(con->http);
if (!cupsdSendHeader(con, HTTP_STATUS_UNAUTHORIZED, NULL, CUPSD_AUTH_NONE))
{
cupsdCloseClient(con);
return;
}
}
if (!_cups_strcasecmp(httpGetField(con->http, HTTP_FIELD_CONNECTION), "Upgrade") && strstr(httpGetField(con->http, HTTP_FIELD_UPGRADE), "TLS/") != NULL && !httpIsEncrypted(con->http))
{
#ifdef HAVE_SSL
/*
* Do encryption stuff...
*/
httpClearFields(con->http);
if (!cupsdSendHeader(con, HTTP_STATUS_SWITCHING_PROTOCOLS, NULL, CUPSD_AUTH_NONE))
{
cupsdCloseClient(con);
return;
}
if (cupsd_start_tls(con, HTTP_ENCRYPTION_REQUIRED))
{
cupsdCloseClient(con);
return;
}
#else
if (!cupsdSendError(con, HTTP_STATUS_NOT_IMPLEMENTED, CUPSD_AUTH_NONE))
{
cupsdCloseClient(con);
return;
}
#endif /* HAVE_SSL */
}
httpClearFields(con->http);
httpSetField(con->http, HTTP_FIELD_CONTENT_LENGTH, "0");
if (!cupsdSendHeader(con, HTTP_STATUS_OK, NULL, CUPSD_AUTH_NONE))
{
cupsdCloseClient(con);
return;
}
}
else if (!is_path_absolute(con->uri))
{
/*
* Protect against malicious users!
*/
cupsdLogClient(con, CUPSD_LOG_ERROR,
"Request for non-absolute resource \"%s\".", con->uri);
if (!cupsdSendError(con, HTTP_STATUS_FORBIDDEN, CUPSD_AUTH_NONE))
{
cupsdCloseClient(con);
return;
}
}
else
{
if (!_cups_strcasecmp(httpGetField(con->http, HTTP_FIELD_CONNECTION),
"Upgrade") && !httpIsEncrypted(con->http))
{
#ifdef HAVE_SSL
/*
* Do encryption stuff...
*/
httpClearFields(con->http);
if (!cupsdSendHeader(con, HTTP_STATUS_SWITCHING_PROTOCOLS, NULL,
CUPSD_AUTH_NONE))
{
cupsdCloseClient(con);
return;
}
if (cupsd_start_tls(con, HTTP_ENCRYPTION_REQUIRED))
{
cupsdCloseClient(con);
return;
}