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
|
/***************************************************************************
* 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 <sasl.h>
#include <saslplug.h>
#include <saslutil.h>
#include "tdekrbsocket.h"
#define NET_SEC_BUF_SIZE (2048)
class SASLDataPrivate
{
public:
sasl_callback_t m_callbacks[N_CALLBACKS];
sasl_conn_t *m_krbConnection;
};
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_negotiatedMaxBufferSize(NET_SEC_BUF_SIZE) {
saslData = new SASLDataPrivate;
saslData->m_krbConnection = NULL;
}
TDEKerberosClientSocket::~TDEKerberosClientSocket() {
delete saslData;
}
bool TDEKerberosClientSocket::open(int mode) {
bool ret = TQSocket::open(mode);
if (m_kerberosRequested) {
initializeKerberosInterface();
}
return ret;
}
void TDEKerberosClientSocket::close() {
TQSocket::close();
}
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)) {
ret = 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;
}
Q_LONG TDEKerberosClientSocket::readBlock(char *data, Q_ULONG maxlen) {
Q_LONG ret = TQSocket::readBlock(data, maxlen);
return ret;
}
Q_LONG TDEKerberosClientSocket::writeBlock(const char *data, Q_ULONG len) {
Q_LONG ret = TQSocket::writeBlock(data, len);
return ret;
}
Q_LONG TDEKerberosClientSocket::readLine(char *data, Q_ULONG maxlen) {
Q_LONG ret;
if (m_kerberosRequested) {
ret = getSASLDataFromNetwork(data, maxlen);
}
else {
ret = TQSocket::readLine(data, maxlen);
}
return ret;
}
TQString TDEKerberosClientSocket::readLine() {
TQString ret;
char *buf;
if (m_kerberosRequested) {
buf = (char*)malloc(m_negotiatedMaxBufferSize);
receiveEncryptedData(buf, m_negotiatedMaxBufferSize);
ret = TQString(buf);
free(buf);
}
else {
ret = TQSocket::readLine();
}
return ret;
}
void TDEKerberosClientSocket::writeLine(TQString str) {
if (m_kerberosRequested) {
transmitEncryptedData(socket(), 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, int netfd) {
char *buf;
unsigned len, alloclen;
int result;
alloclen = ((length / 3) + 1) * 4 + 1;
buf = (char*)malloc(alloclen);
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;
write(netfd, buf, len+1);
free(buf);
}
unsigned int TDEKerberosClientSocket::getSASLDataFromNetwork(char *buf, int trunclen) {
unsigned int len;
int result;
TQByteArray ba(2048);
len = 0;
while (1) {
tqApp->processEvents();
if (state() != TQSocket::Connected) {
return -1;
}
if (TQSocket::readBlock(ba.data()+len, 1) > 0) {
if (ba.data()[len] == '\n') {
ba.data()[len] = 0;
break;
}
if (ba.data()[len] != '\r') {
len++;
}
}
if (len >= (ba.size()-1)) {
ba.resize(ba.size()+2048);
break;
}
}
len = strlen(ba.data());
result = sasl_decode64(ba.data(), strlen(ba.data()), buf, trunclen, &len);
if (result != SASL_OK) {
printf("[ERROR] Decoding data from base64 returned %s (%d)\n\r", sasl_errstring(result, NULL, NULL), result);
return -1;
}
buf[len] = '\0';
return len;
}
int TDEKerberosClientSocket::transmitEncryptedData(int fd, const char* readbuf, int cc) {
int result = 0;
unsigned int len;
const char *data;
result=sasl_encode(saslData->m_krbConnection, readbuf, cc, &data, &len);
if (result != SASL_OK) {
printf("[ERROR] Encrypting data returned %s (%d)\n\r", sasl_errdetail(saslData->m_krbConnection), result);
return -1;
}
sendSASLDataToNetwork(data, len, fd);
return 0;
}
int TDEKerberosClientSocket::receiveEncryptedData(char *buf, int trunclen) {
unsigned int recv_len;
const char *recv_data;
int result;
int len;
len = getSASLDataFromNetwork(buf, trunclen);
if (len >= 0) {
result=sasl_decode(saslData->m_krbConnection, buf, len, &recv_data, &recv_len);
if (result != SASL_OK) {
printf("[ERROR] Decrypting data returned %s (%d)\n\r", sasl_errdetail(saslData->m_krbConnection), result);
return -1;
}
strncpy(buf, recv_data, trunclen);
}
return 0;
}
int TDEKerberosClientSocket::initializeKerberosInterface() {
if (state() != TQSocket::Connected) {
saslData->m_krbConnection = false;
return -1;
}
sasl_callback_t *callback;
char buf[NET_SEC_BUF_SIZE];
int result = 0;
int serverlast = 0;
sasl_security_properties_t secprops;
const char *chosenmech;
unsigned int len;
const char *data;
char user_authorized = 0;
sasl_ssf_t *ssf;
char *iplocal = NULL;
char *ipremote = NULL;
const char *service = m_serviceName.ascii();
const char *fqdn = m_serverFQDN.ascii();
callback = saslData->m_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;
result = sasl_client_init(saslData->m_callbacks);
if (result != SASL_OK) {
printf("[ERROR] Initializing libsasl returned %s (%d)\n\r", sasl_errstring(result, NULL, NULL), result);
return -1;
}
result = sasl_client_new(service, fqdn, iplocal, ipremote, NULL, serverlast, &saslData->m_krbConnection);
if (result != SASL_OK) {
printf("[ERROR] Allocating sasl connection state returned %s (%d)\n\r", sasl_errstring(result, NULL, NULL), result);
return -1;
}
result = sasl_setprop(saslData->m_krbConnection, SASL_SEC_PROPS, &secprops);
if (result != SASL_OK) {
printf("[ERROR] Setting security properties returned %s (%d)\n\r", sasl_errstring(result, NULL, NULL), result);
freeKerberosConnection();
return -1;
}
printf("[DEBUG] Waiting for mechanism list from server...\n\r");
len = getSASLDataFromNetwork(buf, NET_SEC_BUF_SIZE);
printf("Choosing best mechanism from: %s\n", buf);
result = sasl_client_start(saslData->m_krbConnection, buf, NULL, &data, &len, &chosenmech);
if (result != SASL_OK && result != SASL_CONTINUE) {
printf("[ERROR] Starting SASL negotiation returned %s (%d)\n\r", sasl_errstring(result, NULL, NULL), result);
freeKerberosConnection();
return -1;
}
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();
return -1;
}
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, socket());
while (result == SASL_CONTINUE) {
printf("[DEBUG] Waiting for server reply...\n\r");
len = getSASLDataFromNetwork(buf, NET_SEC_BUF_SIZE);
if (state() != TQSocket::Connected) {
return -1;
}
result = sasl_client_step(saslData->m_krbConnection, buf, len, NULL, &data, &len);
if (result != SASL_OK && result != SASL_CONTINUE) {
printf("[ERROR] Performing SASL negotiation returned %s (%d)\n\r", sasl_errstring(result, NULL, NULL), result);
freeKerberosConnection();
return -1;
}
if (data && len) {
printf("[DEBUG] Sending response...\n\r");
sendSASLDataToNetwork(data, len, socket());
}
else if (result != SASL_OK || !serverlast) {
sendSASLDataToNetwork("", 0, socket());
}
}
printf("[DEBUG] Negotiation complete!\n\r");
result = sasl_getprop(saslData->m_krbConnection, SASL_USERNAME, (const void **)&data);
if (result != SASL_OK) {
printf("[WARNING] Unable to determine authenticated username!\n\r");
}
else {
printf("[DEBUG] Authenticated username: %s\n\r", data ? data : "(NULL)");
}
result = sasl_getprop(saslData->m_krbConnection, SASL_DEFUSERREALM, (const void **)&data);
if (result != SASL_OK) {
printf("[WARNING] Unable to determine authenticated realm!\n\r");
}
else {
printf("[DEBUG] Authenticated realm: %s\n\r", data ? data : "(NULL)");
}
result = sasl_getprop(saslData->m_krbConnection, SASL_SSF, (const void **)&ssf);
if (result != SASL_OK) {
printf("[WARNING] Unable to determine SSF!\n\r");
}
else {
printf("[DEBUG] Authenticated SSF: %d\n", *ssf);
}
result = sasl_getprop(saslData->m_krbConnection, SASL_MAXOUTBUF, (const void **)&m_negotiatedMaxBufferSize);
if (result != SASL_OK) {
printf("[WARNING] Unable to determine maximum buffer size!\n\r");
m_negotiatedMaxBufferSize = NET_SEC_BUF_SIZE;
}
else {
printf("[DEBUG] Maximum buffer size: %d\n", m_negotiatedMaxBufferSize);
}
return 0;
}
|