-
Notifications
You must be signed in to change notification settings - Fork 21
/
main.c
1642 lines (1473 loc) · 40 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 <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>
/*
* 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 */
#ifdef ENABLE_KERBEROS
#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 available proxies and current proxy id for proxy_connect().
*/
int parent_count = 0;
plist_t parent_list = NULL;
/*
* 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() */
/*
* 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++;
}
/*
* Parse proxy parameter and add it to the global list.
*/
int parent_add(char *parent, int port) {
int len, i;
char *proxy;
proxy_t *aux;
/*
* Check format and parse it.
*/
proxy = strdup(parent);
len = strlen(proxy);
i = strcspn(proxy, ": ");
if (i != len) {
proxy[i++] = 0;
while (i < len && (proxy[i] == ' ' || proxy[i] == '\t'))
i++;
if (i >= len) {
free(proxy);
return 0;
}
port = atoi(proxy+i);
}
/*
* No port argument and not parsed from proxy?
*/
if (!port) {
syslog(LOG_ERR, "Invalid proxy specification %s.\n", parent);
free(proxy);
myexit(1);
}
/*
* Try to resolve proxy address
*
if (debug)
syslog(LOG_INFO, "Resolving proxy %s...\n", proxy);
if (!so_resolv(&host, proxy)) {
syslog(LOG_ERR, "Cannot resolve proxy %s, discarding.\n", parent);
free(proxy);
return 0;
}
*/
aux = (proxy_t *)new(sizeof(proxy_t));
strlcpy(aux->hostname, proxy, sizeof(aux->hostname));
aux->port = port;
aux->resolved = 0;
parent_list = plist_add(parent_list, ++parent_count, (char *)aux);
free(proxy);
return 1;
}
/*
* Register and bind new proxy service port.
*/
void listen_add(const char *service, plist_t *list, char *spec, int gateway) {
struct in_addr source;
int i, p, len, port;
char *tmp;
len = strlen(spec);
p = strcspn(spec, ":");
if (p < len-1) {
tmp = substr(spec, 0, p);
if (!so_resolv(&source, tmp)) {
syslog(LOG_ERR, "Cannot resolve listen address %s\n", tmp);
myexit(1);
}
free(tmp);
port = atoi(tmp = spec+p+1);
} else {
source.s_addr = htonl(gateway ? INADDR_ANY : INADDR_LOOPBACK);
port = atoi(tmp = spec);
}
if (!port) {
syslog(LOG_ERR, "Invalid listen port %s.\n", tmp);
myexit(1);
}
i = so_listen(port, source);
if (i > 0) {
*list = plist_add(*list, i, NULL);
syslog(LOG_INFO, "%s listening on %s:%d\n", service, inet_ntoa(source), port);
}
}
/*
* Register a new tunnel definition, bind service port.
*/
void tunnel_add(plist_t *list, char *spec, int gateway) {
struct in_addr source;
int i, len, count, pos, 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) {
if (!so_resolv(&source, field[pos])) {
syslog(LOG_ERR, "Cannot resolve tunel bind address: %s\n", field[pos]);
myexit(1);
}
pos++;
} else
source.s_addr = htonl(gateway ? INADDR_ANY : INADDR_LOOPBACK);
if (count-pos == 3) {
port = atoi(field[pos]);
if (port == 0) {
syslog(LOG_ERR, "Invalid tunnel local port: %s\n", field[pos]);
myexit(1);
}
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);
}
tmp = new(strlen(field[pos+1]) + strlen(field[pos+2]) + 2 + 1);
strcpy(tmp, field[pos+1]);
strcat(tmp, ":");
strcat(tmp, field[pos+2]);
i = so_listen(port, source);
if (i > 0) {
*list = plist_add(*list, i, tmp);
syslog(LOG_INFO, "New tunnel from %s:%d to %s\n", inet_ntoa(source), port, tmp);
} else
free(tmp);
} else {
printf("Tunnel specification incorrect ([laddress:]lport:rserver:rport).\n");
myexit(1);
}
free(spec);
}
/*
* Add no-proxy hostname/IP
*/
plist_t noproxy_add(plist_t list, char *spec) {
char *tok, *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_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
*/
void *proxy_thread(void *thread_data) {
rr_data_t request, 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))
ret = direct_request(thread_data, request);
else
ret = forward_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);
/*
* Add ourselves to the "threads to join" list.
*/
if (!serialize) {
pthread_mutex_lock(&threads_mtx);
threads_list = plist_add(threads_list, (unsigned long)pthread_self(), NULL);
pthread_mutex_unlock(&threads_mtx);
}
free(thread_data);
close(cd);
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, *pos;
char *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
forward_tunnel(thread_data);
free(hostname);
free(thread_data);
/*
* Add ourself to the "threads to join" list.
*/
pthread_mutex_lock(&threads_mtx);
threads_list = plist_add(threads_list, (unsigned long)pthread_self(), NULL);
pthread_mutex_unlock(&threads_mtx);
return NULL;
}
/*
* SOCKS5 thread
*/
void *socks5_thread(void *thread_data) {
char *tmp, *thost, *tport, *uname, *upass;
unsigned short port;
int ver, r, c, i, w;
struct auth_s *tcreds = NULL;
unsigned char *bs = NULL, *auths = NULL, *addr = NULL;
int found = -1;
int sd = -1;
int open = !hlist_count(users_list);
int cd = ((struct thread_arg_s *)thread_data)->fd;
struct sockaddr_in caddr = ((struct thread_arg_s *)thread_data)->addr;
free(thread_data);
/*
* Check client's version, possibly fuck'em
*/
bs = (unsigned char *)new(10);
thost = new(MINIBUF_SIZE);
tport = new(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 *)new(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 && (auths[i] || (found = 0)); ++i);
}
/*
* If not, accept plain auth if offered
*/
if (found < 0) {
for (i = 0; i < c && (auths[i] != 2 || !(found = 2)); ++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] = 0xFF;
w = write(cd, bs, 2);
goto bailout;
} else {
bs[0] = 5;
bs[1] = found;
w = write(cd, bs, 2);
}
/*
* 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) */
w = write(cd, bs, 2);
goto bailout;
}
c = bs[1];
/*
* Read username and pass len
*/
uname = new(c+1);
r = read(cd, uname, c+1);
if (r != c+1) {
free(uname);
goto bailout;
}
i = uname[c];
uname[c] = 0;
c = i;
/*
* Read pass
*/
upass = new(c+1);
r = read(cd, upass, c);
if (r != c) {
free(upass);
free(uname);
goto bailout;
}
upass[c] = 0;
/*
* Check credentials against the list
*/
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(cd, bs, 2);
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);
w = write(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) {
ver = 2; /* FQDN, get string length */
r = read(cd, &c, 1);
if (r != 1)
goto bailout;
} else
goto bailout;
addr = (unsigned char *)new(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) {
sprintf(thost, "%d.%d.%d.%d", addr[0], addr[1], addr[2], addr[3]); /* It's in network byte order */
} else {
strlcpy(thost, (char *)addr, MINIBUF_SIZE);
}
/*
* 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 {
sprintf(tport, "%d", ntohs(port));
strlcat(thost, ":", MINIBUF_SIZE);
strlcat(thost, tport, MINIBUF_SIZE);
tcreds = new_auth();
sd = proxy_connect(tcreds);
if (sd >= 0)
i = prepare_http_connect(sd, tcreds, thost);
}
/*
* 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);
w = write(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(cd, bs, 10);
}
syslog(LOG_DEBUG, "%s SOCKS %s", inet_ntoa(caddr.sin_addr), 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)
close(sd);
close(cd);
return NULL;
}
int main(int argc, char **argv) {
char *tmp, *head;
char *cpassword, *cpassntlm2, *cpassnt, *cpasslm;
char *cuser, *cdomain, *cworkstation, *cuid, *cpidfile, *cauth;
struct passwd *pw;
struct termios termold, termnew;
pthread_attr_t pattr;
pthread_t pthr;
hlist_t list;
int i, w;
int cd = 0;
int help = 0;
int nuid = 0;
int ngid = 0;
int gateway = 0;
int tc = 0;
int tj = 0;
int interactivepwd = 0;
int interactivehash = 0;
int tracefile = 0;
int cflags = 0;
int asdaemon = 1;
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;
g_creds = new_auth();
cuser = new(MINIBUF_SIZE);
cdomain = new(MINIBUF_SIZE);
cpassword = new(MINIBUF_SIZE);
cpassntlm2 = new(MINIBUF_SIZE);
cpassnt = new(MINIBUF_SIZE);
cpasslm = new(MINIBUF_SIZE);
cworkstation = new(MINIBUF_SIZE);
cpidfile = new(MINIBUF_SIZE);
cuid = new(MINIBUF_SIZE);
cauth = new(MINIBUF_SIZE);
openlog("cntlm", LOG_CONS, 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, ":-:a:c:d:fghIl:p:r:su:vw:A:BD:F:G:HL:M:N:O:P:R:S:T:U:")) != -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':
if (!(cf = config_open(optarg))) {
syslog(LOG_ERR, "Cannot access specified config file: %s\n", optarg);
myexit(1);
}
break;
case 'd':
strlcpy(cdomain, optarg, MINIBUF_SIZE);
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 = new(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, MINIBUF_SIZE);
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':
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);
printf("Cntlm debug trace, version " VERSION);
#ifdef __CYGWIN__
printf(" windows/cygwin port");
#endif
printf(".\nCommand line: ");
for (i = 0; i < argc; ++i)
printf("%s ", argv[i]);
printf("\n");
}
break;
case 'U':
strlcpy(cuid, optarg, MINIBUF_SIZE);
break;
case 'u':
i = strcspn(optarg, "@");
if (i != 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;
asdaemon = 0;
openlog("cntlm", LOG_CONS | LOG_PERROR, LOG_DAEMON);
break;
case 'w':
strlcpy(cworkstation, optarg, MINIBUF_SIZE);
break;
case 'h':
default:
help = 1;
}
}
/*
* Help requested?
*/
if (help) {
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");
fprintf(stderr, "Usage: %s [-AaBcDdFfgHhILlMPpSsTUuvw] <proxy_host>[:]<proxy_port> ...\n", argv[0]);
fprintf(stderr, "\t-A <address>[/<net>]\n"
"\t ACL allow rule. IP or hostname, net must be a number (CIDR notation)\n");
fprintf(stderr, "\t-a ntlm | nt | lm\n"
"\t Authentication type - combined NTLM, just LM, or just NT. Default NTLM.\n"
#ifdef ENABLE_KERBEROS
"\t GSS activates kerberos auth: you need a cached credential.\n"
#endif
"\t It is the most versatile setting and likely to work for you.\n");
fprintf(stderr, "\t-B Enable NTLM-to-basic authentication.\n");
fprintf(stderr, "\t-c <config_file>\n"
"\t Configuration file. Other arguments can be used as well, overriding\n"
"\t config file settings.\n");
fprintf(stderr, "\t-D <address>[/<net>]\n"
"\t ACL deny rule. Syntax same as -A.\n");
fprintf(stderr, "\t-d <domain>\n"
"\t Domain/workgroup can be set separately.\n");
fprintf(stderr, "\t-f Run in foreground, do not fork into daemon mode.\n");
fprintf(stderr, "\t-F <flags>\n"
"\t NTLM authentication flags.\n");
fprintf(stderr, "\t-G <pattern>\n"
"\t User-Agent matching for the trans-isa-scan plugin.\n");
fprintf(stderr, "\t-g Gateway mode - listen on all interfaces, not only loopback.\n");
fprintf(stderr, "\t-H Print password hashes for use in config file (NTLMv2 needs -u and -d).\n");
fprintf(stderr, "\t-h Print this help info along with version number.\n");
fprintf(stderr, "\t-I Prompt for the password interactively.\n");
fprintf(stderr, "\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(stderr, "\t-l [<saddr>:]<lport>\n"
"\t Main listening port for the NTLM proxy.\n");
fprintf(stderr, "\t-M <testurl>\n"
"\t Magic autodetection of proxy's NTLM dialect.\n");
fprintf(stderr, "\t-N \"<hostname_wildcard1>[, <hostname_wildcardN>\"\n"
"\t List of URL's to serve direcly as stand-alone proxy (e.g. '*.local')\n");
fprintf(stderr, "\t-O [<saddr>:]<lport>\n"
"\t Enable SOCKS5 proxy on port lport (binding to address saddr)\n");
fprintf(stderr, "\t-P <pidfile>\n"
"\t Create a PID file upon successful start.\n");
fprintf(stderr, "\t-p <password>\n"
"\t Account password. Will not be visible in \"ps\", /proc, etc.\n");
fprintf(stderr, "\t-r \"HeaderName: value\"\n"
"\t Add a header substitution. All such headers will be added/replaced\n"
"\t in the client's requests.\n");
fprintf(stderr, "\t-S <size_in_kb>\n"
"\t Enable automation of GFI WebMonitor ISA scanner for files < size_in_kb.\n");
fprintf(stderr, "\t-s Do not use threads, serialize all requests - for debugging only.\n");
fprintf(stderr, "\t-U <uid>\n"
"\t Run as uid. It is an important security measure not to run as root.\n");
fprintf(stderr, "\t-u <user>[@<domain]\n"
"\t Domain/workgroup can be set separately.\n");
fprintf(stderr, "\t-v Print debugging information.\n");
fprintf(stderr, "\t-w <workstation>\n"
"\t Some proxies require correct NetBIOS hostname.\n\n");
exit(1);
}
/*
* More arguments on the command-line? Must be proxies.
*/
i = optind;
while (i < argc) {
tmp = strchr(argv[i], ':');
parent_add(argv[i], !tmp && i+1 < argc ? atoi(argv[i+1]) : 0);
i += (!tmp ? 2 : 1);
}
/*
* No configuration file yet? Load the default.
*/
#ifdef SYSCONFDIR
if (!cf) {
#ifdef __CYGWIN__
tmp = getenv("PROGRAMFILES(X86)");
if (tmp == NULL || strlen(tmp) == 0)
tmp = getenv("PROGRAMFILES");
if (tmp == NULL)
tmp = "C:\\Program Files";
head = new(MINIBUF_SIZE);
strlcpy(head, tmp, MINIBUF_SIZE);
strlcat(head, "\\Cntlm\\cntlm.ini", MINIBUF_SIZE);
cf = config_open(head);
#else
cf = config_open(SYSCONFDIR "/cntlm.conf");
#endif
if (debug) {
if (cf)
printf("Default config file opened successfully\n");
else
syslog(LOG_ERR, "Could not open default config file\n");
}
}
#endif
/*
* If any configuration file was successfully opened, parse it.
*/
if (cf) {
/*
* Check if gateway mode is requested before actually binding any ports.
*/
tmp = new(MINIBUF_SIZE);
CFG_DEFAULT(cf, "Gateway", tmp, MINIBUF_SIZE);
if (!strcasecmp("yes", tmp))
gateway = 1;
free(tmp);
/*
* Check for NTLM-to-basic settings
*/
tmp = new(MINIBUF_SIZE);
CFG_DEFAULT(cf, "NTLMToBasic", tmp, MINIBUF_SIZE);
if (!strcasecmp("yes", tmp))
ntlmbasic = 1;