-
Notifications
You must be signed in to change notification settings - Fork 40
/
main.c
1853 lines (1676 loc) · 45.9 KB
/
main.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
/*
* This is the main module of the CNTLM
*
* CNTLM is free software; you can redistribute it and/or modify it under the
* terms of the GNU General Public License as published by the Free Software
* Foundation; either version 2 of the License, or (at your option) any later
* version.
*
* CNTLM is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc., 51 Franklin
* St, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Copyright (c) 2007 David Kubicek
*
*/
#include <sys/types.h>
#include <sys/time.h>
#include <sys/select.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <pthread.h>
#include <limits.h>
#include <stdio.h>
#include <errno.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <signal.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <netdb.h>
#include <ctype.h>
#include <pwd.h>
#include <fcntl.h>
#include <syslog.h>
#include <termios.h>
#include <fnmatch.h>
#include <assert.h>
#ifdef __CYGWIN__
#include <windows.h>
#endif
/*
* Some helping routines like linked list manipulation substr(), memory
* allocation, NTLM authentication routines, etc.
*/
#include "config/config.h"
#include "socket.h"
#include "utils.h"
#include "ntlm.h"
#include "swap.h"
#include "config.h"
#include "acl.h"
#include "auth.h"
#include "http.h"
#include "globals.h"
#include "pages.h"
#include "forward.h" /* code serving via parent proxy */
#include "direct.h" /* code serving directly without proxy */
#include "proxy.h"
#include "pac.h"
#ifdef __CYGWIN__
#include "sspi.h" /* code for SSPI management */
#endif
#if config_gss == 1
#include "kerberos.h"
#endif
#define STACK_SIZE sizeof(void *)*8*1024
/*
* Global "read-only" data initialized in main(). Comments list funcs. which use
* them. Having these global avoids the need to pass them to each thread and
* from there again a few times to inner calls.
*/
int debug = 0; /* all debug printf's and possibly external modules */
struct auth_s *g_creds = NULL; /* throughout the whole module */
int quit = 0; /* sighandler() */
int ntlmbasic = 0; /* forward_request() */
int serialize = 0;
int scanner_plugin = 0;
long scanner_plugin_maxsize = 0;
/*
* List of finished threads. Each forward_request() thread adds itself to it when
* finished. Main regularly joins and removes all tid's in there.
*/
plist_t threads_list = NULL;
pthread_mutex_t threads_mtx = PTHREAD_MUTEX_INITIALIZER;
/*
* List of cached connections. Accessed by each thread forward_request().
*/
plist_t connection_list = NULL;
pthread_mutex_t connection_mtx = PTHREAD_MUTEX_INITIALIZER;
/*
* List of custom header substitutions, SOCKS5 proxy users and
* UserAgents for the scanner plugin.
*/
hlist_t header_list = NULL; /* forward_request() */
hlist_t users_list = NULL; /* socks5_thread() */
plist_t scanner_agent_list = NULL; /* scanner_hook() */
plist_t noproxy_list = NULL; /* proxy_thread() */
/* 1 = Pac engine is initialized and in use. */
int pac_initialized = 0;
/*
* General signal handler. If in debug mode, quit immediately.
*/
void sighandler(int p) {
if (!quit)
syslog(LOG_INFO, "Signal %d received, issuing clean shutdown\n", p);
else
syslog(LOG_INFO, "Signal %d received, forcing shutdown\n", p);
if (quit++ || debug)
quit++;
}
/*
* Register and bind new proxy service port.
*/
void listen_add(const char *service, plist_t *list, char *spec, int gateway) {
struct addrinfo *addresses;
int i;
int p;
int port;
char *tmp;
char *q = strrchr(spec, ':');
if (q != NULL) {
p = (int)(q - spec);
if(spec[0] == '[' && spec[p-1] == ']') {
tmp = substr(spec, 1, p-2);
} else {
tmp = substr(spec, 0, p);
}
port = atoi(spec+p+1);
if (!port || !so_resolv(&addresses, tmp, port)) {
syslog(LOG_ERR, "Cannot resolve listen address %s\n", spec);
myexit(1);
}
free(tmp);
} else {
port = atoi(spec);
if (!port) {
syslog(LOG_ERR, "Cannot resolve listen address %s\n", spec);
myexit(1);
}
so_resolv_wildcard(&addresses, port, gateway);
}
i = so_listen(list, addresses, NULL);
if (i > 0) {
syslog(LOG_INFO, "New %s service on %s\n", service, spec);
}
freeaddrinfo(addresses);
}
/*
* Register a new tunnel definition, bind service port.
*/
void tunnel_add(plist_t *list, char *spec, int gateway) {
struct addrinfo *addresses;
int i;
int len;
int count;
int pos;
int port;
char *field[4];
char *tmp;
spec = strdup(spec);
len = strlen(spec);
field[0] = spec;
for (count = 1, i = 0; count < 4 && i < len; ++i)
if (spec[i] == ':') {
spec[i] = 0;
field[count++] = spec+i+1;
}
pos = 0;
if (count == 4) {
port = atoi(field[pos+1]);
if (!port || !so_resolv(&addresses, field[pos], port)) {
syslog(LOG_ERR, "Cannot resolve tunnel bind address: %s:%s\n", field[pos], field[pos+1]);
myexit(1);
}
pos++;
} else {
port = atoi(field[pos]);
if(!port) {
syslog(LOG_ERR, "Invalid tunnel local port: %s\n", field[pos]);
myexit(1);
}
so_resolv_wildcard(&addresses, port, gateway);
}
if (count-pos == 3) {
if (!strlen(field[pos+1]) || !strlen(field[pos+2])) {
syslog(LOG_ERR, "Invalid tunnel target: %s:%s\n", field[pos+1], field[pos+2]);
myexit(1);
}
const size_t tmp_len = strlen(field[pos+1]) + strlen(field[pos+2]) + 2 + 1;
tmp = zmalloc(tmp_len);
strlcpy(tmp, field[pos+1], tmp_len);
strlcat(tmp, ":", tmp_len);
strlcat(tmp, field[pos+2], tmp_len);
i = so_listen(list, addresses, tmp);
if (i > 0) {
syslog(LOG_INFO, "New tunnel to %s\n", tmp);
} else {
syslog(LOG_ERR, "Unable to bind tunnel");
free(tmp);
}
} else {
printf("Tunnel specification incorrect ([laddress:]lport:rserver:rport).\n");
myexit(1);
}
free(spec);
freeaddrinfo(addresses);
}
/*
* Add no-proxy hostname/IP
*/
plist_t noproxy_add(plist_t list, char *spec) {
char *tok;
char *save;
tok = strtok_r(spec, ", ", &save);
while ( tok != NULL ) {
if (debug)
printf("Adding no-proxy for: '%s'\n", tok);
list = plist_add(list, 0, strdup(tok));
tok = strtok_r(NULL, ", ", &save);
}
return list;
}
int noproxy_match(const char *addr) {
plist_const_t list;
list = noproxy_list;
while (list) {
if (list->aux && strlen(list->aux)
&& fnmatch(list->aux, addr, 0) == 0) {
if (debug)
printf("MATCH: %s (%s)\n", addr, (char *)list->aux);
return 1;
} else if (debug)
printf(" NO: %s (%s)\n", addr, (char *)list->aux);
list = list->next;
}
return 0;
}
/*
* Proxy thread - decide between direct and forward based on NoProxy
* TODO: update
*/
void *proxy_thread(void *thread_data) {
rr_data_t request;
rr_data_t ret;
int keep_alive; /* Proxy-Connection */
int cd = ((struct thread_arg_s *)thread_data)->fd;
do {
ret = NULL;
keep_alive = 0;
if (debug) {
printf("\n******* Round 1 C: %d *******\n", cd);
printf("Reading headers (%d)...\n", cd);
}
request = new_rr_data();
if (!headers_recv(cd, request)) {
free_rr_data(&request);
break;
}
do {
/*
* Are we being returned a request by forward_request or direct_request?
*/
if (ret) {
free_rr_data(&request);
request = ret;
}
keep_alive = hlist_subcmp(request->headers, "Proxy-Connection", "keep-alive");
if (noproxy_match(request->hostname)) {
/* No-proxy-list has highest precedence */
ret = direct_request(thread_data, request);
} else {
ret = forward_request(thread_data, request);
if (ret == (void *)-2)
ret = direct_request(thread_data, request);
}
if (debug)
printf("proxy_thread: request rc = %p\n", (void *)ret);
} while (ret != NULL && ret != (void *)-1);
free_rr_data(&request);
/*
* If client asked for proxy keep-alive, loop unless the last server response
* requested (Proxy-)Connection: close.
*/
} while (keep_alive && ret != (void *)-1 && !serialize);
free(thread_data);
close(cd);
/*
* Add ourself to the "threads to join" list.
*/
if (!serialize) {
pthread_mutex_lock(&threads_mtx);
pthread_t thread_id = pthread_self();
threads_list = plist_add(threads_list, (unsigned long)thread_id, NULL);
pthread_mutex_unlock(&threads_mtx);
}
return NULL;
}
/*
* Tunnel/port forward thread - this method is obviously better solution than using extra
* tools like "corkscrew" which after all require us for authentication and tunneling
* their HTTP CONNECT in the first place.
*/
void *tunnel_thread(void *thread_data) {
char *hostname;
char *pos;
assert(thread_data != NULL);
const char * const thost = ((struct thread_arg_s *)thread_data)->target;
hostname = strdup(thost);
if ((pos = strchr(hostname, ':')) != NULL)
*pos = 0;
if (noproxy_match(hostname)) {
direct_tunnel(thread_data);
} else {
if (forward_tunnel(thread_data) == -2)
direct_tunnel(thread_data);
}
free(hostname);
free(thread_data);
/*
* Add ourself to the "threads to join" list.
*/
if (!serialize) {
pthread_mutex_lock(&threads_mtx);
pthread_t thread_id = pthread_self();
threads_list = plist_add(threads_list, (unsigned long)thread_id, NULL);
pthread_mutex_unlock(&threads_mtx);
}
return NULL;
}
/*
* SOCKS5 thread
*/
void *socks5_thread(void *thread_data) {
static const uint8_t SOCKS5_AUTH_NO_AUTHENTICATION_REQUIRED = 0x00;
static const uint8_t SOCKS5_AUTH_USERNAME_PASSWORD = 0x02;
static const uint8_t SOCKS5_AUTH_NO_ACCEPTABLE_METHODS = 0xFF;
char *thost;
char *tport;
char *uname;
char *upass;
unsigned short port;
int ver;
int r;
int c;
int i;
int w;
struct auth_s *tcreds = NULL;
unsigned char *bs = NULL;
unsigned char *auths = NULL;
unsigned char *addr = NULL;
int found = -1;
int sd = -1;
int open = !hlist_count(users_list);
int cd = ((struct thread_arg_s *)thread_data)->fd;
char saddr[INET6_ADDRSTRLEN] = {0};
INET_NTOP(&((struct thread_arg_s *)thread_data)->addr, saddr, INET6_ADDRSTRLEN);
free(thread_data);
/*
* Check client's version, possibly fuck'em
*/
bs = (unsigned char *)zmalloc(10);
thost = zmalloc(HOST_BUFSIZE);
tport = zmalloc(MINIBUF_SIZE);
r = read(cd, bs, 2);
if (r != 2 || bs[0] != 5)
goto bailout;
/*
* Read offered auth schemes
*/
c = bs[1];
auths = (unsigned char *)zmalloc(c+1);
r = read(cd, auths, c);
if (r != c)
goto bailout;
/*
* Are we wide open and client is OK with no auth?
*/
if (open) {
for (i = 0; i < c && found < 0; ++i) {
if (auths[i] == SOCKS5_AUTH_NO_AUTHENTICATION_REQUIRED) {
found = auths[i];
}
}
}
/*
* If not, accept plain auth if offered
*/
if (found < 0) {
for (i = 0; i < c && found < 0; ++i) {
if (auths[i] == SOCKS5_AUTH_USERNAME_PASSWORD) {
found = auths[i];
}
}
}
/*
* If not open and no auth offered or open and auth requested, fuck'em
* and complete the handshake
*/
if (found < 0) {
bs[0] = 5;
bs[1] = SOCKS5_AUTH_NO_ACCEPTABLE_METHODS;
(void) write_wrapper(cd, bs, 2); // We don't really care about the result
goto bailout;
} else {
bs[0] = 5;
bs[1] = found;
w = write_wrapper(cd, bs, 2);
if (w != 2) {
syslog(LOG_ERR, "SOCKS5: write() for accepting AUTH method failed.\n");
}
}
/*
* Plain auth negotiated?
*/
if (found != 0) {
/*
* Check ver and read username len
*/
r = read(cd, bs, 2);
if (r != 2) {
bs[0] = 1;
bs[1] = 0xFF; /* Unsuccessful (not supported) */
(void) write_wrapper(cd, bs, 2);
goto bailout;
}
c = bs[1]; // ULEN
/*
* Read username and pass len
*/
uname = zmalloc(c+1);
r = read(cd, uname, c+1);
if (r != c+1) {
free(uname);
goto bailout;
}
i = uname[c]; // PLEN
uname[c] = 0;
c = i;
/*
* Read pass
*/
upass = zmalloc(c+1);
r = read(cd, upass, c);
if (r != c) {
free(upass);
free(uname);
goto bailout;
}
upass[c] = 0;
/*
* Check credentials against the list
*/
const char * tmp = hlist_get(users_list, uname);
if (!hlist_count(users_list) || (tmp && !strcmp(tmp, upass))) {
bs[0] = 1;
bs[1] = 0; /* Success */
} else {
bs[0] = 1;
bs[1] = 0xFF; /* Failed */
}
/*
* Send response
*/
w = write_wrapper(cd, bs, 2);
if (w != 2) {
syslog(LOG_ERR, "SOCKS5: write() for response of credentials check failed.\n");
}
free(upass);
free(uname);
/*
* Fuck'em if auth failed
*/
if (bs[1])
goto bailout;
}
/*
* Read request type
*/
r = read(cd, bs, 4);
if (r != 4)
goto bailout;
/*
* Is it connect for supported address type (IPv4 or DNS)? If not, fuck'em
*/
if (bs[1] != 1 || (bs[3] != 1 && bs[3] != 3)) {
bs[0] = 5;
bs[1] = 2; /* Not allowed */
bs[2] = 0;
bs[3] = 1; /* Dummy IPv4 */
memset(bs+4, 0, 6);
(void) write_wrapper(cd, bs, 10);
goto bailout;
}
/*
* Ok, it's connect to a domain or IP
* Let's read dest address
*/
if (bs[3] == 1) {
ver = 1; /* IPv4, we know the length */
c = 4;
} else if (bs[3] == 3) {
uint8_t string_length;
ver = 2; /* FQDN, get string length */
r = read(cd, &string_length, 1);
if (r != 1)
goto bailout;
c = string_length;
} else
goto bailout;
addr = (unsigned char *)zmalloc(c + 10 + 1);
r = read(cd, addr, c);
if (r != c)
goto bailout;
addr[c] = 0;
/*
* Convert the address to character string
*/
if (ver == 1) {
snprintf(thost, HOST_BUFSIZE, "%d.%d.%d.%d", addr[0], addr[1], addr[2], addr[3]); /* It's in network byte order */
} else {
strlcpy(thost, (char *)addr, HOST_BUFSIZE);
}
/*
* Read port number and convert to host byte order int
*/
r = read(cd, &port, 2);
if (r != 2)
goto bailout;
i = 0;
if (noproxy_match(thost)) {
sd = host_connect(thost, ntohs(port));
i = (sd >= 0);
} else {
snprintf(tport, MINIBUF_SIZE, "%d", ntohs(port));
char *hostname = strdup(thost);
strlcat(thost, ":", HOST_BUFSIZE);
strlcat(thost, tport, HOST_BUFSIZE);
tcreds = new_auth();
sd = proxy_connect(tcreds, thost, hostname);
if (sd == -2) {
sd = host_connect(hostname, ntohs(port));
i = (sd >= 0);
} else if (sd >= 0) {
i = prepare_http_connect(sd, tcreds, thost);
}
free(hostname);
}
/*
* Direct or proxy connect?
*/
if (!i) {
/*
* Connect/tunnel failed, report
*/
bs[0] = 5;
bs[1] = 1; /* General failure */
bs[2] = 0;
bs[3] = 1; /* Dummy IPv4 */
memset(bs+4, 0, 6);
(void) write_wrapper(cd, bs, 10);
goto bailout;
} else {
/*
* All right
*/
bs[0] = 5;
bs[1] = 0; /* Success */
bs[2] = 0;
bs[3] = 1; /* Dummy IPv4 */
memset(bs+4, 0, 6);
w = write_wrapper(cd, bs, 10);
if (w != 10) {
syslog(LOG_ERR, "SOCKS5: write() for reporting success for connect failed.\n");
}
}
syslog(LOG_DEBUG, "%s SOCKS %s", saddr, thost);
/*
* Let's give them bi-directional connection they asked for
*/
tunnel(cd, sd);
bailout:
if (addr)
free(addr);
if (auths)
free(auths);
if (thost)
free(thost);
if (tport)
free(tport);
if (bs)
free(bs);
if (tcreds)
free(tcreds);
if (sd >= 0)
close(sd);
close(cd);
/*
* Add ourself to the "threads to join" list.
*/
if (!serialize) {
pthread_mutex_lock(&threads_mtx);
pthread_t thread_id = pthread_self();
threads_list = plist_add(threads_list, (unsigned long)thread_id, NULL);
pthread_mutex_unlock(&threads_mtx);
}
return NULL;
}
int main(int argc, char **argv) {
char *tmp;
char *head;
char *cpassword;
char *cpassntlm2;
char *cpassnt;
char *cpasslm;
char *cuser;
char *cdomain;
char *cworkstation;
char *cuid;
char *cpidfile;
char *cauth;
struct termios termold;
struct termios termnew;
pthread_attr_t pattr;
pthread_t pthr;
hlist_const_t list;
int i;
int w;
int cd = 0;
int help = 0;
int nuid = 0;
int ngid = 0;
int gateway = 0;
unsigned int tc = 0; ///< Total number of created threads
unsigned int tj = 0; ///< Total number of terminated threads
int interactivepwd = 0;
int interactivehash = 0;
int tracefile = 0;
int cflags = 0;
int asdaemon = 1;
char *myconfig = NULL;
plist_t tunneld_list = NULL;
plist_t proxyd_list = NULL;
plist_t socksd_list = NULL;
plist_t rules = NULL;
config_t cf = NULL;
char *magic_detect = NULL;
int pac = 0;
char *pac_file;
pac_file = zmalloc(PATH_MAX);
g_creds = new_auth();
cuser = zmalloc(MINIBUF_SIZE);
cdomain = zmalloc(MINIBUF_SIZE);
cpassword = zmalloc(PASSWORD_BUFSIZE);
cpassntlm2 = zmalloc(MINIBUF_SIZE);
cpassnt = zmalloc(MINIBUF_SIZE);
cpasslm = zmalloc(MINIBUF_SIZE);
cworkstation = zmalloc(MINIBUF_SIZE);
cpidfile = zmalloc(MINIBUF_SIZE);
cuid = zmalloc(MINIBUF_SIZE);
cauth = zmalloc(MINIBUF_SIZE);
int syslog_debug = 0;
openlog("cntlm", LOG_CONS | LOG_PERROR, LOG_DAEMON);
#if config_endian == 0
syslog(LOG_INFO, "Starting cntlm version " VERSION " for BIG endian\n");
#else
syslog(LOG_INFO, "Starting cntlm version " VERSION " for LITTLE endian\n");
#endif
while ((i = getopt(argc, argv, ":-:T:a:c:d:fghIl:p:r:su:vw:x:A:BD:F:G:HL:M:N:O:P:R:S:U:X:q")) != -1) {
switch (i) {
case 'A':
case 'D':
if (!acl_add(&rules, optarg, (i == 'A' ? ACL_ALLOW : ACL_DENY)))
myexit(1);
break;
case 'a':
strlcpy(cauth, optarg, MINIBUF_SIZE);
break;
case 'B':
ntlmbasic = 1;
break;
case 'c':
myconfig = strdup(optarg);
break;
case 'd':
strlcpy(cdomain, optarg, MINIBUF_SIZE);
break;
case 'x':
pac = 1;
/*
* Resolve relative paths if necessary.
* Don't care if the named file does not exist (ENOENT) because
* later on we check the file's availability anyway.
*/
if (!realpath(optarg, pac_file) && errno != ENOENT) {
syslog(LOG_ERR, "Resolving path to PAC file failed: %s\n", strerror(errno));
myexit(1);
}
break;
case 'F':
cflags = swap32(strtoul(optarg, &tmp, 0));
break;
case 'f':
asdaemon = 0;
break;
case 'G':
if (strlen(optarg)) {
scanner_plugin = 1;
if (!scanner_plugin_maxsize)
scanner_plugin_maxsize = 1;
i = strlen(optarg) + 3;
tmp = zmalloc(i);
snprintf(tmp, i, "*%s*", optarg);
scanner_agent_list = plist_add(scanner_agent_list, 0, tmp);
}
break;
case 'g':
gateway = 1;
break;
case 'H':
interactivehash = 1;
break;
case 'I':
interactivepwd = 1;
break;
case 'L':
/*
* Parse and validate the argument.
* Create a listening socket for tunneling.
*/
tunnel_add(&tunneld_list, optarg, gateway);
break;
case 'l':
/*
* Create a listening socket for proxy function.
*/
listen_add("Proxy", &proxyd_list, optarg, gateway);
break;
case 'M':
magic_detect = strdup(optarg);
break;
case 'N':
noproxy_list = noproxy_add(noproxy_list, tmp=strdup(optarg));
free(tmp);
break;
case 'O':
listen_add("SOCKS5 proxy", &socksd_list, optarg, gateway);
break;
case 'P':
strlcpy(cpidfile, optarg, MINIBUF_SIZE);
break;
case 'p':
/*
* Overwrite the password parameter with '*'s to make it
* invisible in "ps", /proc, etc.
*/
strlcpy(cpassword, optarg, PASSWORD_BUFSIZE);
for (i = strlen(optarg)-1; i >= 0; --i)
optarg[i] = '*';
break;
case 'R':
tmp = strdup(optarg);
head = strchr(tmp, ':');
if (!head) {
fprintf(stderr, "Invalid username:password format for -R: %s\n", tmp);
} else {
head[0] = 0;
users_list = hlist_add(users_list, tmp, head+1,
HLIST_ALLOC, HLIST_ALLOC);
}
break;
case 'r':
if (is_http_header(optarg))
header_list = hlist_add(header_list,
get_http_header_name(optarg),
get_http_header_value(optarg),
HLIST_NOALLOC, HLIST_NOALLOC);
break;
case 'S':
scanner_plugin = 1;
scanner_plugin_maxsize = atol(optarg);
break;
case 's':
/*
* Do not use threads - for debugging purposes only
*/
serialize = 1;
break;
case 'T':
debug = 1;
syslog_debug = 1;
asdaemon = 0;
tracefile = open(optarg, O_CREAT | O_TRUNC | O_WRONLY, 0600);
if (tracefile < 0) {
fprintf(stderr, "Cannot create trace file.\n");
myexit(1);
} else {
printf("Redirecting all output to %s\n", optarg);
dup2(tracefile, 1);
dup2(tracefile, 2);
}
break;
case 'U':
strlcpy(cuid, optarg, MINIBUF_SIZE);
break;
case 'u':
i = strcspn(optarg, "@");
if (i != (int)strlen(optarg)) {
strlcpy(cuser, optarg, MIN(MINIBUF_SIZE, i+1));
strlcpy(cdomain, optarg+i+1, MINIBUF_SIZE);
} else {
strlcpy(cuser, optarg, MINIBUF_SIZE);
}
break;
case 'v':
debug = 1;
syslog_debug = 1;
asdaemon = 0;
break;
case 'w':
strlcpy(cworkstation, optarg, MINIBUF_SIZE);
break;
case 'X':
#ifdef __CYGWIN__
if (!sspi_set(optarg))
{
fprintf(stderr, "SSPI initialize failed (%s)! Proceeding with SSPI disabled.\n", optarg);
} else {
syslog(LOG_INFO, "SSPI %s mode enabled!", optarg);
}
#else
fprintf(stderr, "This feature is available under Windows only!\n");
help = 1;
#endif
break;
case 'q':
syslog_debug = 1;
break;
case 'h':
help = 1;
break;
default:
help = 2;
}
}
/*
* Help requested?
*/
if (help > 0) {
printf("CNTLM - Accelerating NTLM Authentication Proxy version " VERSION "\n");
printf("Copyright (c) 2oo7-2o1o David Kubicek\n\n"
"This program comes with NO WARRANTY, to the extent permitted by law. You\n"
"may redistribute copies of it under the terms of the GNU GPL Version 2 or\n"
"newer. For more information about these matters, see the file LICENSE.\n"
"For copyright holders of included encryption routines see headers.\n\n");
FILE * stream = stdout;
int exit_code = 0;
if (help > 1) {
stream = stderr;
exit_code = 1;
}
fprintf(stream, "Usage: %s [-AaBcDdFfGgHhILlMNOPpqRrSsTUuvwXx] <proxy_host>[:]<proxy_port> ...\n", argv[0]);
fprintf(stream, "\t-A <address>[/<net>]\n"
"\t ACL allow rule. IP or hostname, net must be a number (CIDR notation)\n");
fprintf(stream, "\t-a ntlm | nt | lm"
#if config_gss == 1
" | gss\n"
"\t Authentication type - combined NTLM, just LM, just NT, or GSS. Default NTLM.\n"
"\t GSS activates kerberos auth: you need a cached credential.\n"
#else
"\n"
"\t Authentication type - combined NTLM, just LM, or just NT. Default NTLM.\n"
#endif
"\t NTLM is the most versatile setting and likely to work for you.\n");
fprintf(stream, "\t-B Enable NTLM-to-basic authentication.\n");
fprintf(stream, "\t-c <config_file>\n"
"\t Configuration file. Other arguments can be used as well, overriding\n"
"\t config file settings.\n");
fprintf(stream, "\t-D <address>[/<net>]\n"
"\t ACL deny rule. Syntax same as -A.\n");
fprintf(stream, "\t-d <domain>\n"
"\t Domain/workgroup can be set separately.\n");
fprintf(stream, "\t-F <flags>\n"
"\t NTLM authentication flags.\n");
fprintf(stream, "\t-f Run in foreground, do not fork into daemon mode.\n");
fprintf(stream, "\t-G <pattern>\n"
"\t User-Agent matching for the trans-isa-scan plugin.\n");
fprintf(stream, "\t-g Gateway mode - listen on all interfaces, not only loopback.\n");
fprintf(stream, "\t-H Print password hashes for use in config file (NTLMv2 needs -u and -d).\n");
fprintf(stream, "\t-h Print this help info along with version number.\n");
fprintf(stream, "\t-I Prompt for the password interactively.\n");
fprintf(stream, "\t-L [<saddr>:]<lport>:<rhost>:<rport>\n"
"\t Forwarding/tunneling a la OpenSSH. Same syntax - listen on lport\n"
"\t and forward all connections through the proxy to rhost:rport.\n"
"\t Can be used for direct tunneling without corkscrew, etc.\n");
fprintf(stream, "\t-l [<saddr>:]<lport>\n"
"\t Main listening port for the NTLM proxy.\n");
fprintf(stream, "\t-M <testurl>\n"
"\t Magic autodetection of proxy's NTLM dialect.\n");
fprintf(stream, "\t-N \"<hostname_wildcard1>[, <hostname_wildcardN>\"\n"
"\t List of URL's to serve directly as stand-alone proxy (e.g. '*.local')\n");
fprintf(stream, "\t-O [<saddr>:]<lport>\n"
"\t Enable SOCKS5 proxy on port lport (binding to address saddr)\n");
fprintf(stream, "\t-P <pidfile>\n"
"\t Create a PID file upon successful start.\n");
fprintf(stream, "\t-p <password>\n"