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
|
/***************************************************************************
* Copyright (C) 2012 by Timothy Pearson *
* kb9vqf@pearsoncomputing.net *
* *
* 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. *
***************************************************************************/
#include <stdlib.h>
#include <unistd.h>
#include <tqapplication.h>
#include <tqbuffer.h>
#include <tqeventloop.h>
#include <tqtimer.h>
#include <sasl.h>
#include <saslplug.h>
#include <saslutil.h>
#include <klocale.h>
#include "tdekrbclientsocket.h"
#define NET_SEC_BUF_SIZE (2048)
// When control comes back from processEvents() my object may be completely gone! This attempts to mitigate the risk
#define SAFELY_PROCESS_EVENTS if (!m_canary) { \
m_canary = new bool; \
*m_canary = false; \
} \
bool* canary = m_canary; \
tqApp->eventLoop()->processEvents(TQEventLoop::ExcludeUserInput); \
if (*canary == true) { \
delete canary; \
return -1; \
} \
delete m_canary; \
m_canary = NULL;
static bool tde_krb_sasl_client_initialized = false;
static sasl_callback_t tde_krb_sasl_client_callbacks[N_CALLBACKS];
/* exception handling */
struct exit_exception {
int c;
exit_exception(int c):c(c) { }
};
class SASLDataPrivate
{
public:
sasl_conn_t *m_krbConnection;
};
static const char * safe_sasl_errdetail(sasl_conn_t *conn) {
const char * str = sasl_errdetail(conn);
if (str) {
return str;
}
else {
return "unknown error";
}
}
static int logSASLMessages(void *context __attribute__((unused)), int priority, const char *message) {
const char *label;
if (!message) {
return SASL_BADPARAM;
}
switch (priority) {
case SASL_LOG_ERR:
label = "Error";
break;
case SASL_LOG_NOTE:
label = "Info";
break;
default:
label = "Other";
break;
}
printf("[SASL %s] %s\n\r", label, message);
return SASL_OK;
}
TDEKerberosClientSocket::TDEKerberosClientSocket(TQObject *parent, const char *name) : TQSocket(parent, name), m_kerberosRequested(false), m_criticalSection(0), m_bufferLength(0), m_krbInitRunning(false), m_krbInitState(-1), m_dataTimeout(-1), kerberosInitLoopTimer(NULL), m_canary(NULL), m_negotiatedMaxBufferSize(NET_SEC_BUF_SIZE) {
saslData = new SASLDataPrivate;
saslData->m_krbConnection = NULL;
m_buffer = new TQBuffer();
m_buffer->open(IO_ReadWrite|IO_Truncate);
}
TDEKerberosClientSocket::~TDEKerberosClientSocket() {
if (m_canary) {
*m_canary = true;
}
if (kerberosInitLoopTimer) {
kerberosInitLoopTimer->stop();
delete kerberosInitLoopTimer;
kerberosInitLoopTimer = NULL;
}
setUsingKerberos(false);
m_buffer->close();
delete m_buffer;
delete saslData;
}
void TDEKerberosClientSocket::setDataTimeout(int timeoutms) {
m_dataTimeout = timeoutms;
}
bool TDEKerberosClientSocket::open(int mode) {
setStatusMessage(i18n("Establishing initial connection to server"));
bool ret = TQSocket::open(mode);
if (m_kerberosRequested) {
initializeKerberosInterface();
}
return ret;
}
void TDEKerberosClientSocket::close() {
TQSocket::close();
setStatusMessage(i18n("Disconnected"));
}
void TDEKerberosClientSocket::flush() {
if (kerberosStatus() == KerberosInUse) {
// FIXME
// If a write buffer is implemented, it will need to be flushed before the following call is made
TQSocket::flush();
}
else {
TQSocket::flush();
}
}
TQIODevice::Offset TDEKerberosClientSocket::size() const {
TQIODevice::Offset ret;
if (kerberosStatus() == KerberosInUse) {
ret = m_bufferLength;
}
else {
ret = TQSocket::size();
}
return ret;
}
TQIODevice::Offset TDEKerberosClientSocket::at() const {
return TQSocket::at();
}
bool TDEKerberosClientSocket::at(TQIODevice::Offset off, int hidebasehack) {
long i;
bool ret;
Q_UNUSED(hidebasehack);
if (kerberosStatus() == KerberosInUse) {
if (off > 0) {
// Prevent overflow
if (off > (unsigned long)m_bufferLength) {
off = m_bufferLength;
}
// Remove the specified bytes from the buffer
m_bufferLength = m_bufferLength-off;
TQByteArray ba = m_buffer->buffer();
for (i=0; i<m_bufferLength;i++) {
ba[i] = ba[i+off];
}
if (m_bufferLength < 1) {
// Clear the buffer from memory
m_buffer->close();
m_buffer->open(IO_ReadWrite|IO_Truncate);
}
}
return true;
}
else {
ret = TQSocket::at(off);
}
return ret;
}
bool TDEKerberosClientSocket::atEnd() const {
bool ret;
if (kerberosStatus() == KerberosInUse) {
ret = ((m_bufferLength < 1) && TQSocket::atEnd());
}
else {
ret = TQSocket::atEnd();
}
return ret;
}
int TDEKerberosClientSocket::getch() {
int ret;
if (kerberosStatus() == KerberosInUse) {
char data[1];
if (readBlock(data, 1) < 0) {
ret = -1;
}
else {
ret = data[0];
}
}
else {
ret = TQSocket::getch();
}
return ret;
}
int TDEKerberosClientSocket::putch(int ch) {
int ret;
if (kerberosStatus() == KerberosInUse) {
char data[1];
data[0] = ch;
if (writeBlock(data, 1) < 1) {
ret = -1;
}
else {
ret = ch;
}
}
else {
ret = TQSocket::putch(ch);
}
return ret;
}
int TDEKerberosClientSocket::ungetch(int ch) {
int ret;
if (kerberosStatus() == KerberosInUse) {
ret = m_buffer->ungetch(ch);
if (ret >= 0) {
m_bufferLength++;
}
}
else {
ret = TQSocket::ungetch(ch);
}
return ret;
}
TQ_ULONG TDEKerberosClientSocket::bytesAvailable() const {
TQ_ULONG ret;
if (kerberosStatus() == KerberosInUse) {
ret = m_bufferLength;
}
else {
ret = TQSocket::bytesAvailable();
}
return ret;
}
int TDEKerberosClientSocket::processPendingData() {
if (kerberosStatus() == KerberosInUse) {
while (TQSocket::canReadLine() && (TQSocket::state() == TQSocket::Connected)) {
int reclen;
int wrlen;
char* buf = (char*)malloc(m_negotiatedMaxBufferSize);
reclen = receiveEncryptedData(buf, m_negotiatedMaxBufferSize, false);
if (reclen < 0) {
free(buf);
return -1;
}
if (reclen > 0) {
m_buffer->at(m_bufferLength);
wrlen = m_buffer->writeBlock(buf, reclen);
if (wrlen > 0) {
m_bufferLength = m_bufferLength + wrlen;
emit(newDataReceived());
}
}
free(buf);
}
}
return 0;
}
int TDEKerberosClientSocket::setUsingKerberos(bool krbactive) {
int ret = 0;
if (m_serviceName == "") {
printf("[ERROR] No service name set!\n\r"); fflush(stdout);
return -1;
}
if (krbactive) {
m_kerberosRequested = true;
if ((!saslData->m_krbConnection) && (state() == TQSocket::Connected)) {
initializeKerberosInterface();
}
}
else {
m_kerberosRequested = false;
if (saslData->m_krbConnection) {
freeKerberosConnection();
}
}
return ret;
}
void TDEKerberosClientSocket::setServiceName(TQString name) {
m_serviceName = name;
}
void TDEKerberosClientSocket::setServerFQDN(TQString name) {
m_serverFQDN = name;
}
TQ_LONG TDEKerberosClientSocket::readBlock(char *data, TQ_ULONG maxlen) {
long i;
TQ_LONG ret;
if (kerberosStatus() == KerberosInUse) {
int reclen;
int wrlen;
if (m_bufferLength <= 0) {
char* buf = (char*)malloc(m_negotiatedMaxBufferSize);
reclen = receiveEncryptedData(buf, m_negotiatedMaxBufferSize, false);
if (reclen < 0) {
free(buf);
return -1;
}
if (reclen > 0) {
m_buffer->at(m_bufferLength);
wrlen = m_buffer->writeBlock(buf, reclen);
if (wrlen > 0) {
m_bufferLength = m_bufferLength + wrlen;
emit(newDataReceived());
}
}
free(buf);
}
if (maxlen > (unsigned int)m_bufferLength) {
maxlen = m_bufferLength;
}
m_buffer->at(0);
ret = m_buffer->readBlock(data, maxlen);
if (ret > 0) {
// Remove the read bytes from the buffer
m_bufferLength = m_bufferLength-ret;
TQByteArray ba = m_buffer->buffer();
for (i=0; i<m_bufferLength;i++) {
ba[i] = ba[i+ret];
}
if (m_bufferLength < 1) {
// Clear the buffer from memory
m_buffer->close();
m_buffer->open(IO_ReadWrite|IO_Truncate);
}
}
}
else {
ret = TQSocket::readBlock(data, maxlen);
}
return ret;
}
TQ_LONG TDEKerberosClientSocket::writeBlock(const char *data, TQ_ULONG len) {
TQ_LONG ret;
if (kerberosStatus() == KerberosInUse) {
ret = transmitEncryptedData(data, len);
}
else {
ret = TQSocket::writeBlock(data, len);
}
return ret;
}
TQ_LONG TDEKerberosClientSocket::readLine(char *data, TQ_ULONG maxlen) {
long i;
TQ_LONG ret;
if (kerberosStatus() == KerberosInUse) {
int reclen;
int wrlen;
if (m_bufferLength <= 0) {
char* buf = (char*)malloc(m_negotiatedMaxBufferSize);
reclen = receiveEncryptedData(buf, m_negotiatedMaxBufferSize, false);
if (reclen < 0) {
free(buf);
return -1;
}
if (reclen > 0) {
m_buffer->at(m_bufferLength);
wrlen = m_buffer->writeBlock(buf, reclen);
if (wrlen > 0) {
m_bufferLength = m_bufferLength + wrlen;
emit(newDataReceived());
}
}
free(buf);
}
if (maxlen > (unsigned int)m_bufferLength) {
maxlen = m_bufferLength;
}
m_buffer->at(0);
ret = m_buffer->readLine(data, maxlen);
if (ret > 0) {
// Remove the read bytes from the buffer
m_bufferLength = m_bufferLength-ret;
TQByteArray ba = m_buffer->buffer();
for (i=0; i<m_bufferLength;i++) {
ba[i] = ba[i+ret];
}
if (m_bufferLength < 1) {
// Clear the buffer from memory
m_buffer->close();
m_buffer->open(IO_ReadWrite|IO_Truncate);
}
}
}
else {
ret = TQSocket::readLine(data, maxlen);
}
return ret;
}
TQString TDEKerberosClientSocket::readLine() {
long i;
TQString ret;
long maxlen;
if (kerberosStatus() == KerberosInUse) {
int reclen;
int wrlen;
int readlen;
char* buf;
maxlen = m_negotiatedMaxBufferSize;
if (m_bufferLength <= 0) {
buf = (char*)malloc(m_negotiatedMaxBufferSize);
reclen = receiveEncryptedData(buf, m_negotiatedMaxBufferSize, false);
if (reclen < 0) {
free(buf);
return TQString::null;
}
if (reclen > 0) {
m_buffer->at(m_bufferLength);
wrlen = m_buffer->writeBlock(buf, reclen);
if (wrlen > 0) {
m_bufferLength = m_bufferLength + wrlen;
emit(newDataReceived());
}
}
free(buf);
}
if (maxlen > m_bufferLength) {
maxlen = m_bufferLength;
}
m_buffer->at(0);
buf = (char*)malloc(maxlen);
readlen = m_buffer->readLine(buf, maxlen);
if (readlen > 0) {
// Remove the read bytes from the buffer
m_bufferLength = m_bufferLength-readlen;
TQByteArray ba = m_buffer->buffer();
for (i=0; i<m_bufferLength;i++) {
ba[i] = ba[i+readlen];
}
if (m_bufferLength < 1) {
// Clear the buffer from memory
m_buffer->close();
m_buffer->open(IO_ReadWrite|IO_Truncate);
}
ret = TQString(buf);
}
else {
ret == TQString::null;
}
free(buf);
}
else {
ret = TQSocket::readLine();
}
return ret;
}
void TDEKerberosClientSocket::writeLine(TQString str) {
if (kerberosStatus() == KerberosInUse) {
transmitEncryptedData(str.ascii(), str.length());
}
else {
TQSocket::writeBlock(str.ascii(), str.length());
}
}
void TDEKerberosClientSocket::freeKerberosConnection(void) {
if (saslData->m_krbConnection) {
sasl_dispose(&saslData->m_krbConnection);
}
saslData->m_krbConnection = 0;
}
void TDEKerberosClientSocket::sendSASLDataToNetwork(const char *buffer, unsigned length) {
char *buf;
unsigned len, alloclen;
int result;
alloclen = (((length / 3) + 1) * 4) + 1;
buf = (char*)malloc(alloclen+1);
if (!buf) {
printf("[ERROR] Unable to malloc()!\n\r");
return;
}
result = sasl_encode64(buffer, length, buf, alloclen, &len);
if (result != SASL_OK) {
printf("[ERROR] Encoding data in base64 returned %s (%d)\n\r", sasl_errstring(result, NULL, NULL), result);
return;
}
len = strlen(buf);
buf[len] = '\n';
buf[len+1] = 0;
if (TQSocket::writeBlock(buf, len+1) < 0) {
printf("[WARNING] Transmitting data in base64 failed due to short write\n\r");
}
free(buf);
}
int TDEKerberosClientSocket::getSASLDataFromNetwork(char *buf, int trunclen, bool shouldblock) {
m_criticalSection++;
try {
unsigned int len;
int result;
TQCString ba;
if (!shouldblock) {
if ((!TQSocket::canReadLine()) || (state() != TQSocket::Connected)) {
return 0;
}
}
len = 0;
TQTimer dataTimeoutTimer;
if (m_dataTimeout > 0) {
dataTimeoutTimer.start(m_dataTimeout, TRUE);
}
while (dataTimeoutTimer.isActive() || (m_dataTimeout < 0)) {
if (!TQSocket::canReadLine()) {
if ((shouldblock) && (dataTimeoutTimer.isActive() || (m_dataTimeout < 0))) {
SAFELY_PROCESS_EVENTS
}
}
if (state() != TQSocket::Connected) {
m_criticalSection--;
return -1;
}
if (TQSocket::canReadLine()) {
TQString base64string = TQSocket::readLine();
base64string.truncate(base64string.length()-1);
ba = base64string;
break;
}
else {
if (shouldblock) {
usleep(1000);
}
else {
break;
}
}
}
if (!ba.isNull()) {
len = strlen(ba.data());
result = sasl_decode64(ba.data(), len, buf, trunclen, &len);
if (result != SASL_OK) {
printf("[ERROR] Decoding data from base64 returned %s (%d)\n\r", sasl_errstring(result, NULL, NULL), result);
m_criticalSection--;
return -1;
}
buf[len] = '\0';
}
else {
buf[0] = '\0';
}
m_criticalSection--;
return len;
}
catch(exit_exception& e) {
m_criticalSection--;
return -1;
}
}
int TDEKerberosClientSocket::transmitEncryptedData(const char* readbuf, int cc) {
int result = 0;
unsigned int len;
const char *data;
long data_remaining;
long remnant_position;
TQTimer dataTimeoutTimer;
if (m_dataTimeout > 0) {
dataTimeoutTimer.start(m_dataTimeout, TRUE);
}
data_remaining = cc;
remnant_position = 0;
while ((data_remaining > 0) && (dataTimeoutTimer.isActive() || (m_dataTimeout < 0))) {
int data_to_write_len;
if (data_remaining > (m_negotiatedMaxBufferSize/2)) {
data_to_write_len = m_negotiatedMaxBufferSize/2;
}
else {
data_to_write_len = data_remaining;
}
result=sasl_encode(saslData->m_krbConnection, readbuf+remnant_position, data_to_write_len, &data, &len);
if (result != SASL_OK) {
printf("[ERROR] Encrypting data returned %s (%d)\n\r", safe_sasl_errdetail(saslData->m_krbConnection), result);
return -1;
}
sendSASLDataToNetwork(data, len);
data_remaining = data_remaining - data_to_write_len;
remnant_position = remnant_position + data_to_write_len;
if ((data_remaining > 0) && (dataTimeoutTimer.isActive() || (m_dataTimeout < 0))) {
SAFELY_PROCESS_EVENTS
}
}
return cc;
}
int TDEKerberosClientSocket::receiveEncryptedData(char *buf, unsigned int trunclen, bool shouldblock) {
unsigned int recv_len;
const char *recv_data;
int result;
int len;
char *encbuf = (char*)malloc(m_negotiatedMaxBufferSize);
len = getSASLDataFromNetwork(encbuf, m_negotiatedMaxBufferSize, shouldblock);
if (len < 0) {
return -1;
}
if (len >= 0) {
result=sasl_decode(saslData->m_krbConnection, encbuf, len, &recv_data, &recv_len);
if (result != SASL_OK) {
free(encbuf);
printf("[ERROR] Decrypting data returned %s (%d)\n\r", safe_sasl_errdetail(saslData->m_krbConnection), result);
return -1;
}
if (recv_len > trunclen) {
recv_len = trunclen;
}
memcpy(buf, recv_data, recv_len);
}
free(encbuf);
return recv_len;
}
TDEKerberosClientSocket::KerberosStatus TDEKerberosClientSocket::kerberosStatus() const {
if (!m_kerberosRequested) {
return KerberosNotRequested;
}
if (m_krbInitRunning) {
return KerberosInitializing;
}
if (m_krbInitState < 0) {
return KerberosFailure;
}
return KerberosInUse;
}
bool TDEKerberosClientSocket::canReadData() {
return (TQSocket::canReadLine() || (m_bufferLength > 0));
}
void TDEKerberosClientSocket::clearIncomingData() {
char data[64];
processPendingData();
while (canReadData()) {
readBlock(data, 64);
}
}
int TDEKerberosClientSocket::writeEndOfFrame() {
char data[1];
data[0] = 255;
return writeBlock(data, 1);
}
bool TDEKerberosClientSocket::canReadFrame() {
processPendingData();
if (m_buffer->buffer().contains(255) > 0) {
return true;
}
else {
return false;
}
}
void TDEKerberosClientSocket::clearFrameTail() {
int i;
int eofLoc;
if (m_bufferLength > 0) {
eofLoc = m_buffer->buffer().find(255, 0) + 1;
if ((eofLoc > 0) && (eofLoc <= m_bufferLength)) {
// Remove the remaining frame bytes (including the End of Frame marker) from the buffer
m_bufferLength = m_bufferLength-eofLoc;
TQByteArray ba = m_buffer->buffer();
for (i=0; i<m_bufferLength;i++) {
ba[i] = ba[i+eofLoc];
}
if (m_bufferLength < 1) {
// Clear the buffer from memory
m_buffer->close();
m_buffer->open(IO_ReadWrite|IO_Truncate);
}
}
}
}
void TDEKerberosClientSocket::setStatusMessage(TQString message) {
if (message != m_prevStatusMessage) {
emit(statusMessageUpdated(message));
m_prevStatusMessage = message;
}
}
void TDEKerberosClientSocket::continueKerberosInitialization() {
int slen;
char buf[NET_SEC_BUF_SIZE];
unsigned int len;
const char *data;
const char *chosenmech;
sasl_ssf_t *ssf;
if (m_krbInitRunning) {
switch (m_krbInitState) {
case 0:
if (state() == TQSocket::Connected) {
setStatusMessage(i18n("Waiting for mechanism list from server"));
if (canReadLine()) {
printf("[DEBUG] Waiting for mechanism list from server...\n\r");
slen = getSASLDataFromNetwork(buf, NET_SEC_BUF_SIZE);
if (slen < 0) {
m_krbInitState = -2;
m_krbInitRunning = false;
setStatusMessage(i18n("Kerberos connection failed"));
return;
}
len = slen;
printf("Choosing best mechanism from: %s\n", buf);
m_krbInitResult = sasl_client_start(saslData->m_krbConnection, buf, NULL, &data, &len, &chosenmech);
if (m_krbInitResult != SASL_OK && m_krbInitResult != SASL_CONTINUE) {
printf("[ERROR] Starting SASL negotiation returned %s (%d)\n\r", sasl_errstring(m_krbInitResult, NULL, NULL), m_krbInitResult);
freeKerberosConnection();
m_krbInitState = -1;
m_krbInitRunning = false;
setStatusMessage(i18n("Kerberos connection failed"));
return;
}
printf("[DEBUG] Using mechanism %s\n\r", chosenmech);
strcpy(buf, chosenmech);
if (data) {
if (NET_SEC_BUF_SIZE - strlen(buf) - 1 < len) {
printf("[ERROR] Insufficient buffer space to construct initial response!\n\r");
freeKerberosConnection();
m_krbInitState = -1;
m_krbInitRunning = false;
setStatusMessage(i18n("Kerberos connection failed"));
return;
}
printf("[DEBUG] Preparing initial response...\n\r");
memcpy(buf + strlen(buf) + 1, data, len);
len += (unsigned) strlen(buf) + 1;
data = NULL;
}
else {
len = (unsigned) strlen(buf);
}
printf("[DEBUG] Sending initial response...\n\r");
sendSASLDataToNetwork(buf, len);
m_krbInitState = 1;
}
}
else {
m_krbInitState = -3;
m_krbInitRunning = false;
}
break;
case 1:
if (state() == TQSocket::Connected) {
if (m_krbInitResult == SASL_CONTINUE) {
setStatusMessage(i18n("Waiting for server reply"));
if (canReadLine()) {
printf("[DEBUG] Waiting for server reply...\n\r");
slen = getSASLDataFromNetwork(buf, NET_SEC_BUF_SIZE);
if (slen < 0) {
m_krbInitState = -2;
m_krbInitRunning = false;
setStatusMessage(i18n("Kerberos connection failed"));
return;
}
len = slen;
m_krbInitResult = sasl_client_step(saslData->m_krbConnection, buf, len, NULL, &data, &len);
if (m_krbInitResult != SASL_OK && m_krbInitResult != SASL_CONTINUE) {
printf("[ERROR] Performing SASL negotiation returned %s (%d)\n\r", sasl_errstring(m_krbInitResult, NULL, NULL), m_krbInitResult);
freeKerberosConnection();
m_krbInitState = -1;
m_krbInitRunning = false;
setStatusMessage(i18n("Kerberos connection failed"));
return;
}
if (data && len) {
printf("[DEBUG] Sending response...\n\r");
sendSASLDataToNetwork(data, len);
}
else if (m_krbInitResult != SASL_OK || !m_krbInitServerLast) {
sendSASLDataToNetwork("", 0);
}
}
}
else {
printf("[DEBUG] Negotiation complete!\n\r");
m_krbInitState = 2;
}
}
else {
m_krbInitState = -3;
m_krbInitRunning = false;
setStatusMessage(i18n("Kerberos connection failed"));
return;
}
break;
case 2:
if (state() == TQSocket::Connected) {
m_krbInitResult = sasl_getprop(saslData->m_krbConnection, SASL_USERNAME, (const void **)&data);
if (m_krbInitResult != SASL_OK) {
printf("[WARNING] Unable to determine authenticated username!\n\r");
}
else {
printf("[DEBUG] Authenticated username: %s\n\r", data ? data : "(NULL)");
}
#if 0
m_krbInitResult = sasl_getprop(saslData->m_krbConnection, SASL_DEFUSERREALM, (const void **)&data);
if (m_krbInitResult != SASL_OK) {
printf("[WARNING] Unable to determine authenticated realm!\n\r");
}
else {
printf("[DEBUG] Authenticated realm: %s\n\r", data ? data : "(NULL)");
}
#endif
m_krbInitResult = sasl_getprop(saslData->m_krbConnection, SASL_SSF, (const void **)&ssf);
if (m_krbInitResult != SASL_OK) {
printf("[WARNING] Unable to determine SSF!\n\r");
}
else {
printf("[DEBUG] Authenticated SSF: %d\n", *ssf);
}
m_krbInitResult = sasl_getprop(saslData->m_krbConnection, SASL_MAXOUTBUF, (const void **)&m_negotiatedMaxBufferSize);
if (m_krbInitResult != SASL_OK) {
printf("[WARNING] Unable to determine maximum buffer size!\n\r");
m_negotiatedMaxBufferSize = NET_SEC_BUF_SIZE;
}
else {
// For some reason m_negotiatedMaxBufferSize can be set negative under certain circumstances
// Prevent that from happening!
if (m_negotiatedMaxBufferSize < NET_SEC_BUF_SIZE) {
m_negotiatedMaxBufferSize = NET_SEC_BUF_SIZE;
}
printf("[DEBUG] Maximum buffer size: %d\n", m_negotiatedMaxBufferSize);
}
m_krbInitState = 3;
m_krbInitRunning = false;
setStatusMessage(i18n("Kerberos connection established"));
return;
}
else {
m_krbInitState = -3;
m_krbInitRunning = false;
setStatusMessage(i18n("Kerberos connection failed"));
return;
}
break;
}
if (kerberosInitLoopTimer) kerberosInitLoopTimer->start(0, TRUE);
}
}
int TDEKerberosClientSocket::initializeKerberosInterface() {
if (state() != TQSocket::Connected) {
freeKerberosConnection();
return -1;
}
sasl_callback_t *callback;
m_krbInitResult = 0;
m_krbInitServerLast = 0;
sasl_security_properties_t secprops;
char *iplocal = NULL;
char *ipremote = NULL;
const char *service = m_serviceName.ascii();
const char *fqdn = m_serverFQDN.ascii();
callback = tde_krb_sasl_client_callbacks;
// log
callback->id = SASL_CB_LOG;
callback->proc = (sasl_callback_ft)&logSASLMessages;
callback->context = NULL;
++callback;
// end of callback list
callback->id = SASL_CB_LIST_END;
callback->proc = NULL;
callback->context = NULL;
++callback;
// Initialize default data structures
memset(&secprops, 0L, sizeof(secprops));
secprops.maxbufsize = NET_SEC_BUF_SIZE;
secprops.max_ssf = UINT_MAX;
if (!tde_krb_sasl_client_initialized) {
m_krbInitResult = sasl_client_init(tde_krb_sasl_client_callbacks);
if (m_krbInitResult != SASL_OK) {
printf("[ERROR] Initializing libsasl returned %s (%d)\n\r", sasl_errstring(m_krbInitResult, NULL, NULL), m_krbInitResult);
return -1;
}
tde_krb_sasl_client_initialized = true;
}
m_krbInitResult = sasl_client_new(service, fqdn, iplocal, ipremote, NULL, m_krbInitServerLast, &saslData->m_krbConnection);
if (m_krbInitResult != SASL_OK) {
printf("[ERROR] Allocating sasl connection state returned %s (%d)\n\r", sasl_errstring(m_krbInitResult, NULL, NULL), m_krbInitResult);
return -1;
}
m_krbInitResult = sasl_setprop(saslData->m_krbConnection, SASL_SEC_PROPS, &secprops);
if (m_krbInitResult != SASL_OK) {
printf("[ERROR] Setting security properties returned %s (%d)\n\r", sasl_errstring(m_krbInitResult, NULL, NULL), m_krbInitResult);
freeKerberosConnection();
return -1;
}
m_krbInitRunning = true;
m_krbInitState = 0;
if (!kerberosInitLoopTimer) {
kerberosInitLoopTimer = new TQTimer();
connect(kerberosInitLoopTimer, SIGNAL(timeout()), this, SLOT(continueKerberosInitialization()));
}
if (kerberosInitLoopTimer) kerberosInitLoopTimer->start(0, TRUE);
return 0;
}
|