-
Notifications
You must be signed in to change notification settings - Fork 1
/
packet.c
566 lines (473 loc) · 14.5 KB
/
packet.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
/*
* $Id: packet.c,v 1.22 2009-03-18 21:09:26 heas Exp $
*
* Copyright (c) 1995-1998 by Cisco systems, Inc.
*
* Permission to use, copy, modify, and distribute this software for
* any purpose and without fee is hereby granted, provided that this
* copyright and permission notice appear on all copies of the
* software and supporting documentation, the name of Cisco Systems,
* Inc. not be used in advertising or publicity pertaining to
* distribution of the program without specific prior permission, and
* notice be given in supporting documentation that modification,
* copying and distribution is by permission of Cisco Systems, Inc.
*
* Cisco Systems, Inc. makes no representations about the suitability
* of this software for any purpose. THIS SOFTWARE IS PROVIDED ``AS
* IS'' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
* WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE.
*/
#include "tac_plus.h"
#include <poll.h>
#include <netdb.h>
#include <signal.h>
#include <time.h>
#pragma weak get_authen_continue
#pragma weak read_packet
#pragma weak send_acct_reply
#pragma weak send_authen_error
#pragma weak send_authen_reply
#pragma weak send_author_reply
#pragma weak send_error_reply
/* Everything to do with reading and writing packets */
static int sockread(int, u_char *, int, int);
static int sockwrite(int, u_char *, int, int);
static int write_packet(u_char *);
/* read an authentication GETDATA packet from a NAS. Return NULL on failure */
u_char *
get_authen_continue(void)
{
HDR *hdr;
u_char *pak;
struct authen_cont *cont;
char msg[NI_MAXHOST + 256];
pak = read_packet();
if (!pak)
return(NULL);
hdr = (HDR *)pak;
cont = (struct authen_cont *)(pak + TAC_PLUS_HDR_SIZE);
if ((hdr->type != TAC_PLUS_AUTHEN) || (hdr->seq_no <= 1)) {
if (snprintf(msg, sizeof(msg), "%s: Bad packet type=%d/seq no=%d "
"when expecting authentication cont", session.peer,
hdr->type, hdr->seq_no) == -1)
strcpy(msg, "");
report(LOG_ERR, msg);
send_authen_error(msg);
return(NULL);
}
cont->user_msg_len = ntohs(cont->user_msg_len);
cont->user_data_len = ntohs(cont->user_data_len);
if ((TAC_AUTHEN_CONT_FIXED_FIELDS_SIZE + cont->user_msg_len +
cont->user_data_len) != ntohl(hdr->datalength)) {
char *m = "Illegally sized authentication cont packet";
report(LOG_ERR, "%s: %s", session.peer, m);
send_authen_error(m);
return(NULL);
}
if (debug & DEBUG_PACKET_FLAG)
dump_nas_pak(pak);
return(pak);
}
/*
* read a packet from the wire, and decrypt it. Increment the global
* seq_no return NULL on failure
*/
u_char *
read_packet(void)
{
HDR hdr;
u_char *pkt, *data;
int len;
char *tkey;
if (debug & DEBUG_PACKET_FLAG)
report(LOG_DEBUG, "Waiting for packet");
/* read a packet header */
len = sockread(session.sock, (u_char *)&hdr,
TAC_PLUS_HDR_SIZE, TAC_PLUS_READ_TIMEOUT);
if (len != TAC_PLUS_HDR_SIZE) {
report(LOG_DEBUG, "Read %d bytes from %s %s, expecting %d",
len, session.peer, session.port, TAC_PLUS_HDR_SIZE);
return(NULL);
}
session.peerflags = hdr.flags;
if ((hdr.version & TAC_PLUS_MAJOR_VER_MASK) != TAC_PLUS_MAJOR_VER) {
report(LOG_ERR, "%s: Illegal major version specified: found %d wanted "
"%d\n", session.peer, hdr.version, TAC_PLUS_MAJOR_VER);
return(NULL);
}
/* get memory for the packet */
len = TAC_PLUS_HDR_SIZE + ntohl(hdr.datalength);
if ((ntohl(hdr.datalength) & ~0xffffUL) ||
(len < TAC_PLUS_HDR_SIZE) || (len > 0x10000)) {
report(LOG_ERR, "%s: Illegal data size: %lu\n", session.peer,
(unsigned long)ntohl(hdr.datalength));
return(NULL);
}
pkt = (u_char *)tac_malloc(len);
/* initialise the packet */
memcpy(pkt, &hdr, TAC_PLUS_HDR_SIZE);
/* the data start here */
data = pkt + TAC_PLUS_HDR_SIZE;
/* read the rest of the packet data */
if (sockread(session.sock, data, ntohl(hdr.datalength),
TAC_PLUS_READ_TIMEOUT) != ntohl(hdr.datalength)) {
report(LOG_ERR, "%s: start_session: bad socket read", session.peer);
free(pkt);
return(NULL);
}
session.seq_no++; /* should now equal that of incoming packet */
session.last_exch = time(NULL);
if (session.seq_no != hdr.seq_no) {
report(LOG_ERR, "%s: Illegal session seq #, expecting %d, received %d",
session.peer, session.seq_no, hdr.seq_no);
free(pkt);
return(NULL);
}
/* decrypt the data portion */
tkey = cfg_get_host_key(session.peerip);
if (tkey == NULL && !STREQ(session.peer, session.peerip)) {
tkey = cfg_get_host_prompt(session.peer);
}
if (tkey == NULL)
tkey = session.key;
if (md5_xor((HDR *)pkt, data, tkey)) {
report(LOG_ERR, "%s: start_session error decrypting data",
session.peer);
free(pkt);
return(NULL);
}
if (debug & DEBUG_PACKET_FLAG)
report(LOG_DEBUG, "Read %s size=%d",
summarise_incoming_packet_type(pkt), len);
session.version = hdr.version;
return(pkt);
}
/* send an accounting response packet */
void
send_acct_reply(u_char status, char *msg, char *data)
{
u_char *pak, *p;
HDR *hdr;
int len;
struct acct_reply *reply;
int msg_len, data_len;
msg_len = msg ? strlen(msg) : 0;
data_len = data ? strlen(data) : 0;
len = TAC_PLUS_HDR_SIZE + TAC_ACCT_REPLY_FIXED_FIELDS_SIZE + msg_len +
data_len;
pak = (u_char *)tac_malloc(len);
reply = (struct acct_reply *)(pak + TAC_PLUS_HDR_SIZE);
hdr = (HDR *)pak;
memset(pak, 0, len);
hdr->version = TAC_PLUS_VER_0;
hdr->type = TAC_PLUS_ACCT;
hdr->seq_no = ++session.seq_no;
hdr->flags = TAC_PLUS_UNENCRYPTED;
if (!(session.flags & SESS_NO_SINGLECONN))
hdr->flags |= (session.peerflags & TAC_PLUS_SINGLE_CONNECT_FLAG);
hdr->session_id = htonl(session.session_id);
hdr->datalength = htonl(len - TAC_PLUS_HDR_SIZE);
reply->status = status;
reply->msg_len = msg_len;
reply->data_len = data_len;
p = pak + TAC_PLUS_HDR_SIZE + TAC_ACCT_REPLY_FIXED_FIELDS_SIZE;
memcpy(p, msg, msg_len);
p += msg_len;
memcpy(p, data, data_len);
if (debug & DEBUG_PACKET_FLAG) {
report(LOG_DEBUG, "Writing %s size=%d",
summarise_outgoing_packet_type(pak), len);
dump_tacacs_pak(pak);
}
reply->msg_len = ntohs(reply->msg_len);
reply->data_len = ntohs(reply->data_len);
write_packet(pak);
free(pak);
return;
}
/*
* Send an authentication reply packet indicating an error has occurred.
* msg is a null terminated character string
*/
void
send_authen_error(char *msg)
{
char buf[NI_MAXHOST + 256];
if (snprintf(buf, sizeof(buf), "%s %s: %s", session.peer, session.port,
msg) == -1)
strcpy(buf, "");
report(LOG_ERR, buf);
send_authen_reply(TAC_PLUS_AUTHEN_STATUS_ERROR, buf, strlen(buf), NULL, 0,
0);
}
/* create and send an authentication reply packet from tacacs+ to a NAS */
void
send_authen_reply(int status, char *msg, u_short msg_len, char *data,
u_short data_len, u_char flags)
{
u_char *pak, *p;
HDR *hdr;
struct authen_reply *reply;
int len;
len = TAC_PLUS_HDR_SIZE + TAC_AUTHEN_REPLY_FIXED_FIELDS_SIZE + msg_len +
data_len;
pak = (u_char *)tac_malloc(len);
memset(pak, 0, len);
hdr = (HDR *)pak;
reply = (struct authen_reply *)(pak + TAC_PLUS_HDR_SIZE);
hdr->version = session.version;
hdr->type = TAC_PLUS_AUTHEN;
hdr->seq_no = ++session.seq_no;
hdr->flags = TAC_PLUS_UNENCRYPTED;
if (!(session.flags & SESS_NO_SINGLECONN))
hdr->flags |= (session.peerflags & TAC_PLUS_SINGLE_CONNECT_FLAG);
hdr->session_id = htonl(session.session_id);
hdr->datalength = htonl(TAC_AUTHEN_REPLY_FIXED_FIELDS_SIZE + msg_len +
data_len);
reply->status = status;
reply->msg_len = msg_len;
reply->data_len = data_len;
reply->flags = flags;
p = pak + TAC_PLUS_HDR_SIZE + TAC_AUTHEN_REPLY_FIXED_FIELDS_SIZE;
memcpy(p, msg, msg_len);
p += msg_len;
memcpy(p, data, data_len);
if (debug & DEBUG_PACKET_FLAG) {
report(LOG_DEBUG, "Writing %s size=%d",
summarise_outgoing_packet_type(pak), len);
dump_tacacs_pak(pak);
}
reply->msg_len = htons(reply->msg_len);
reply->data_len = htons(reply->data_len);
write_packet(pak);
free(pak);
return;
}
/* send an authorization reply packet */
void
send_author_reply(u_char status, char *msg, char *data, int arg_cnt,
char **args)
{
u_char *pak, *p;
HDR *hdr;
struct author_reply *reply;
int msg_len;
int len;
int data_len;
int i;
data_len = (data ? strlen(data) : 0);
msg_len = (msg ? strlen(msg) : 0);
/* start calculating final packet size */
len = TAC_PLUS_HDR_SIZE + TAC_AUTHOR_REPLY_FIXED_FIELDS_SIZE + msg_len +
data_len;
for (i = 0; i < arg_cnt; i++) {
/* space for the arg and its length */
len += strlen(args[i]) + 1;
}
pak = (u_char *)tac_malloc(len);
memset(pak, 0, len);
hdr = (HDR *)pak;
reply = (struct author_reply *) (pak + TAC_PLUS_HDR_SIZE);
hdr->version = TAC_PLUS_VER_0;
hdr->type = TAC_PLUS_AUTHOR;
hdr->seq_no = ++session.seq_no;
hdr->flags = TAC_PLUS_UNENCRYPTED;
if (!(session.flags & SESS_NO_SINGLECONN))
hdr->flags |= (session.peerflags & TAC_PLUS_SINGLE_CONNECT_FLAG);
hdr->session_id = htonl(session.session_id);
hdr->datalength = htonl(len - TAC_PLUS_HDR_SIZE);
reply->status = status;
reply->msg_len = msg_len;
reply->data_len = data_len;
reply->arg_cnt = arg_cnt;
p = pak + TAC_PLUS_HDR_SIZE + TAC_AUTHOR_REPLY_FIXED_FIELDS_SIZE;
/* place arg sizes into packet */
for (i = 0; i < arg_cnt; i++) {
*p++ = strlen(args[i]);
}
memcpy(p, msg, msg_len);
p += msg_len;
memcpy(p, data, data_len);
p += data_len;
/* copy arg bodies into packet */
for (i = 0; i < arg_cnt; i++) {
int arglen = strlen(args[i]);
memcpy(p, args[i], arglen);
p += arglen;
}
if (debug & DEBUG_PACKET_FLAG) {
report(LOG_DEBUG, "Writing %s size=%d",
summarise_outgoing_packet_type(pak), len);
dump_tacacs_pak(pak);
}
reply->msg_len = htons(reply->msg_len);
reply->data_len = htons(reply->data_len);
write_packet(pak);
free(pak);
return;
}
void
send_error_reply(int type, char *msg)
{
switch (type) {
case TAC_PLUS_AUTHEN:
send_authen_error(msg);
break;
case TAC_PLUS_AUTHOR:
send_author_reply(AUTHOR_STATUS_ERROR, msg, NULL, 0, NULL);
break;
case TAC_PLUS_ACCT:
send_acct_reply(TAC_PLUS_ACCT_STATUS_ERROR, msg, NULL);
break;
default:
report(LOG_ERR, "Illegal type %d for send_error_reply", type);
break;
}
return;
}
/*
* Read n bytes from descriptor fd into array ptr with timeout t seconds.
* Note the timeout is applied to each read, not for the overall operation.
*
* Return -1 on error, eof or timeout. Otherwise return number of bytes read.
*/
static int
sockread(int fd, u_char *ptr, int nbytes, int timeout)
{
int nleft, nread;
struct pollfd pfds;
pfds.fd = fd;
pfds.events = POLLIN | POLLERR | POLLHUP | POLLNVAL;
nleft = nbytes;
while (nleft > 0) {
int status = poll(&pfds, 1, timeout * 1000);
if (status == 0) {
report(LOG_DEBUG, "%s: timeout reading fd %d", session.peer, fd);
return(-1);
}
if (status < 0) {
if (errno == EINTR)
continue;
status = errno;
report(LOG_DEBUG, "%s: error in poll %s fd %d", session.peer,
strerror(errno), fd);
errno = status;
return(-1);
}
if (pfds.revents & (POLLERR | POLLHUP | POLLNVAL)) {
status = errno;
report(LOG_DEBUG, "%s: exception on fd %d", session.peer, fd);
errno = status;
return(-1);
}
if (!(pfds.revents & POLLIN)) {
status = errno;
report(LOG_DEBUG, "%s: spurious return from poll", session.peer);
errno = status;
continue;
}
again:
nread = read(fd, ptr, nleft);
if (nread < 0) {
if (errno == EINTR)
goto again;
status = errno;
report(LOG_DEBUG, "%s %s: error reading fd %d nread=%d %s",
session.peer, session.port, fd, nread, strerror(errno));
errno = status;
return(-1); /* error */
} else if (nread == 0) {
report(LOG_DEBUG, "%s %s: fd %d eof (connection closed)",
session.peer, session.port, fd);
errno = 0;
return(-1); /* eof */
}
nleft -= nread;
if (nleft)
ptr += nread;
}
return(nbytes - nleft);
}
/*
* Write n bytes to descriptor fd from array ptr with timeout t seconds.
* Note the timeout is applied to each write, not for the overall operation.
*
* Return -1 on error, eof or timeout. Otherwise return number of bytes
* written.
*/
static int
sockwrite(int fd, u_char *ptr, int bytes, int timeout)
{
int remaining, sent;
struct pollfd pfds;
pfds.fd = fd;
pfds.events = POLLOUT | POLLERR | POLLHUP | POLLNVAL;
sent = 0;
remaining = bytes;
while (remaining > 0) {
int status = poll(&pfds, 1, timeout * 1000);
if (status == 0) {
status = errno;
report(LOG_DEBUG, "%s: timeout writing to fd %d", session.peer, fd);
errno = status;
return(-1);
}
if (status < 0) {
status = errno;
report(LOG_DEBUG, "%s: error in poll fd %d", session.peer, fd);
errno = status;
return(-1);
}
if (pfds.revents & (POLLERR | POLLHUP | POLLNVAL)) {
status = errno;
report(LOG_DEBUG, "%s: exception on fd %d", session.peer, fd);
errno = status;
return(-1); /* error */
}
if (!(pfds.revents & POLLOUT)) {
report(LOG_DEBUG, "%s: spurious return from poll", session.peer);
continue;
}
sent = write(fd, ptr, remaining);
if (sent <= 0) {
status = errno;
report(LOG_DEBUG, "%s: error writing fd %d sent=%d", session.peer,
fd, sent);
errno = status;
return(sent); /* error */
}
remaining -= sent;
ptr += sent;
}
return(bytes - remaining);
}
/* write a packet to the wire, encrypting it */
static int
write_packet(u_char *pak)
{
HDR *hdr = (HDR *)pak;
u_char *data;
int len;
char *tkey;
len = TAC_PLUS_HDR_SIZE + ntohl(hdr->datalength);
/* the data start here */
data = pak + TAC_PLUS_HDR_SIZE;
/* encrypt the data portion */
tkey = cfg_get_host_key(session.peerip);
if (tkey == NULL && !STREQ(session.peer, session.peerip)) {
tkey = cfg_get_host_prompt(session.peer);
}
if (tkey == NULL)
tkey = session.key;
if (md5_xor((HDR *)pak, data, tkey)) {
report(LOG_ERR, "%s: write_packet: error encrypting data",
session.peer);
return(-1);
}
if (sockwrite(session.sock, pak, len, TAC_PLUS_WRITE_TIMEOUT) != len) {
return(-1);
}
session.last_exch = time(NULL);
return(0);
}