The FreeRADIUS server $Id: a4e7f8c94e8038b7627bcf52356b7ed15955c1d7 $
Loading...
Searching...
No Matches
encode.c
Go to the documentation of this file.
1/*
2 * This library is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU Lesser General Public
4 * License as published by the Free Software Foundation; either
5 * version 2.1 of the License, or (at your option) any later version.
6 *
7 * This library is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 * Lesser General Public License for more details.
11 *
12 * You should have received a copy of the GNU Lesser General Public
13 * License along with this library; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
15 */
16
17/**
18 * $Id: 82e17b7f31074060e5d7d850a2f451a2f3f5bb7d $
19 *
20 * @file protocols/tacacs/encode.c
21 * @brief Low-Level TACACS+ encode functions
22 *
23 * @copyright 2017 The FreeRADIUS server project
24 * @copyright 2017 Network RADIUS SAS (legal@networkradius.com)
25 */
26
27#include <freeradius-devel/io/test_point.h>
28#include <freeradius-devel/protocol/tacacs/tacacs.h>
29#include <freeradius-devel/util/dbuff.h>
30#include <freeradius-devel/util/rand.h>
31#include <freeradius-devel/util/struct.h>
32
33#include "tacacs.h"
34#include "attrs.h"
35
37{
38 switch (code) {
39 case FR_PACKET_TYPE_VALUE_AUTHENTICATION_START:
41 pkt->hdr.seq_no = 1;
42 break;
43
44 case FR_PACKET_TYPE_VALUE_AUTHENTICATION_PASS:
46 pkt->authen_reply.status = FR_TAC_PLUS_AUTHEN_STATUS_PASS;
47 break;
48
49 case FR_PACKET_TYPE_VALUE_AUTHENTICATION_FAIL:
51 pkt->authen_reply.status = FR_TAC_PLUS_AUTHEN_STATUS_FAIL;
52 break;
53
54 case FR_PACKET_TYPE_VALUE_AUTHENTICATION_GETDATA:
56 pkt->authen_reply.status = FR_TAC_PLUS_AUTHEN_STATUS_GETDATA;
57 break;
58
59 case FR_PACKET_TYPE_VALUE_AUTHENTICATION_GETUSER:
61 pkt->authen_reply.status = FR_TAC_PLUS_AUTHEN_STATUS_GETUSER;
62 break;
63
64 case FR_PACKET_TYPE_VALUE_AUTHENTICATION_GETPASS:
66 pkt->authen_reply.status = FR_TAC_PLUS_AUTHEN_STATUS_GETPASS;
67 break;
68
69 case FR_PACKET_TYPE_VALUE_AUTHENTICATION_RESTART:
71 pkt->authen_reply.status = FR_TAC_PLUS_AUTHEN_STATUS_RESTART;
72 break;
73
74 case FR_PACKET_TYPE_VALUE_AUTHENTICATION_ERROR:
76 pkt->authen_reply.status = FR_TAC_PLUS_AUTHEN_STATUS_ERROR;
77 break;
78
79 case FR_PACKET_TYPE_VALUE_AUTHENTICATION_CONTINUE:
81 pkt->authen_cont.flags = FR_TAC_PLUS_CONTINUE_FLAG_UNSET;
82 break;
83
84 case FR_PACKET_TYPE_VALUE_AUTHENTICATION_CONTINUE_ABORT:
86 pkt->authen_cont.flags = FR_TAC_PLUS_CONTINUE_FLAG_ABORT;
87 break;
88
89 case FR_PACKET_TYPE_VALUE_AUTHORIZATION_REQUEST:
91 break;
92
93 case FR_PACKET_TYPE_VALUE_AUTHORIZATION_PASS_ADD:
95 pkt->author_reply.status = FR_TAC_PLUS_AUTHOR_STATUS_PASS_ADD;
96 break;
97
98 case FR_PACKET_TYPE_VALUE_AUTHORIZATION_PASS_REPLACE:
100 pkt->author_reply.status = FR_TAC_PLUS_AUTHOR_STATUS_PASS_REPL;
101 break;
102
103 case FR_PACKET_TYPE_VALUE_AUTHORIZATION_FAIL:
105 pkt->author_reply.status = FR_TAC_PLUS_AUTHOR_STATUS_FAIL;
106 break;
107
108 case FR_PACKET_TYPE_VALUE_AUTHORIZATION_ERROR:
110 pkt->author_reply.status = FR_TAC_PLUS_AUTHOR_STATUS_ERROR;
111 break;
112
113 case FR_PACKET_TYPE_VALUE_ACCOUNTING_REQUEST:
115 break;
116
117 case FR_PACKET_TYPE_VALUE_ACCOUNTING_SUCCESS:
119 pkt->acct_reply.status = FR_TAC_PLUS_ACCT_STATUS_SUCCESS;
120 break;
121
122 case FR_PACKET_TYPE_VALUE_ACCOUNTING_ERROR:
124 pkt->acct_reply.status = FR_TAC_PLUS_ACCT_STATUS_ERROR;
125 break;
126
127 default:
128 fr_strerror_const("Invalid TACACS+ packet type");
129 return -1;
130 }
131
132 return 0;
133}
134
135/**
136 * Encode a TACACS+ 'arg_N' fields.
137 */
139{
140 int arg_cnt = 0;
141 fr_pair_t *vp;
142
143 for (vp = fr_pair_list_head(vps);
144 vp;
145 vp = fr_pair_list_next(vps, vp)) {
146 if (arg_cnt == 255) break;
147
148 if (vp->da->flags.internal) continue;
149
150 if (vp->da == attr_tacacs_packet) continue;
151
152 /*
153 * Argument-List = "foo=bar"
154 */
155 if (vp->da == da) {
156 if (vp->vp_length > 0xff) continue;
157 arg_cnt++;
158 continue;
159 }
160
162
163 /*
164 * Recurse into children.
165 */
166 if (vp->vp_type == FR_TYPE_VENDOR) {
167 arg_cnt += tacacs_encode_body_arg_cnt(&vp->vp_group, NULL);
168 if (arg_cnt > UINT8_MAX) arg_cnt = UINT8_MAX;
169 continue;
170 }
171
172 /*
173 * RFC 8907 attributes.
174 */
175 if (vp->da->parent->flags.is_root) {
176 arg_cnt++;
177 continue;
178 }
179
180 if (vp->da->parent->type != FR_TYPE_VENDOR) continue;
181
182 arg_cnt++;
183 }
184
185 return arg_cnt;
186}
187
189{
190 fr_pair_t *vp;
191 uint8_t i = 0;
192 fr_dbuff_t work_dbuff = FR_DBUFF(dbuff);
193
194 for (vp = fr_pair_list_head(vps);
195 vp;
196 vp = fr_pair_list_next(vps, vp)) {
197 size_t len;
198
199 if (i >= arg_cnt) break;
200
201 if (vp->da->flags.internal) continue;
202
203 if (vp->da == attr_tacacs_packet) continue;
204
205 /*
206 * Argument-List = "foo=bar"
207 */
208 if (vp->da == da) {
209 if (vp->vp_length > 0xff) continue;
210
211 /* Append the <arg_N> field */
212 FR_DBUFF_IN_MEMCPY_RETURN(&work_dbuff, vp->vp_strvalue, vp->vp_length);
213
214 FR_PROTO_TRACE("arg[%d] --> %s", i, vp->vp_strvalue);
215 len = vp->vp_length;
216
217 } else if (vp->vp_type == FR_TYPE_VENDOR) {
218 ssize_t slen;
219 uint8_t child_argc;
220
221 /*
222 * Nested attribute: just recurse.
223 */
224 child_argc = tacacs_encode_body_arg_cnt(&vp->vp_group, NULL);
225 if (child_argc > (arg_cnt - i)) child_argc = arg_cnt - i;
226
227 slen = tacacs_encode_body_arg_n(&work_dbuff, child_argc, &arg_len[i], &vp->vp_group, vp->da);
228 if (slen < 0) return FR_DBUFF_ERROR_OFFSET(slen, fr_dbuff_used(&work_dbuff));
229
230 i += child_argc;
231 continue;
232
233 } else if (!vp->da->parent || (!vp->da->parent->flags.is_root && (vp->da->parent->type != FR_TYPE_VENDOR))) {
234 continue;
235
236 } else {
237 ssize_t slen;
238 fr_sbuff_t sbuff;
239 fr_dbuff_t arg_dbuff = FR_DBUFF_MAX(&work_dbuff, 255);
240 fr_value_box_t box;
241 char buffer[256];
242
243#if defined(__clang_analyzer__) || defined(__COVERITY__)
244 /*
245 * Neither the clang analyser nor Coverity sees the
246 * value printed into the buffer through the sbuff,
247 * and both report the copy out of the buffer as a
248 * read of uninitialised memory.
249 */
250 memset(buffer, 0, sizeof(buffer));
251#endif
252
253 /*
254 * Print it as "name=value"
255 */
256 FR_DBUFF_IN_MEMCPY_RETURN(&arg_dbuff, vp->da->name, strlen(vp->da->name));
257 FR_DBUFF_IN_BYTES_RETURN(&arg_dbuff, (uint8_t) '=');
258
259 sbuff = FR_SBUFF_OUT(buffer, sizeof(buffer));
260
261 switch (vp->vp_type) {
262 /*
263 * For now, we always print time deltas and dates as integers.
264 *
265 * Because everyone else's date formats are insane.
266 */
267 case FR_TYPE_DATE:
269 fr_value_box_init(&box, FR_TYPE_UINT64, vp->data.enumv, vp->vp_tainted);
270 if (fr_value_box_cast(NULL, &box, FR_TYPE_UINT64, NULL, &vp->data) < 0) {
271 buffer[0] = '\0';
272 slen = 0;
273 break;
274 }
275
276 slen = fr_sbuff_in_sprintf(&sbuff, "%lu", box.vb_uint64);
277 if (slen < 0) {
278 error:
279 fr_strerror_printf("Failed encoding %s", vp->da->name);
280 return -1;
281 }
282 break;
283
284 default:
286 if (slen < 0) goto error;
287 }
288
289 FR_DBUFF_IN_MEMCPY_RETURN(&arg_dbuff, buffer, (size_t) slen);
290
291 len = fr_dbuff_used(&arg_dbuff);
292
293 FR_PROTO_TRACE("arg[%d] --> %.*s", i, (int)len, fr_dbuff_start(&arg_dbuff));
294
295 fr_dbuff_set(&work_dbuff, &arg_dbuff);
296 }
297
299
300 FR_PROTO_TRACE("len(arg[%d]) = %zu", i, len);
301 arg_len[i++] = len;
302 }
303
304 return fr_dbuff_set(dbuff, &work_dbuff);
305}
306
307/*
308 * Encode a TACACS+ field.
309 */
310static ssize_t tacacs_encode_field(fr_dbuff_t *dbuff, fr_pair_list_t *vps, fr_dict_attr_t const *da, size_t max_len)
311{
312 fr_pair_t *vp;
313 fr_dbuff_t work_dbuff = FR_DBUFF(dbuff);
314
315 vp = fr_pair_find_by_da(vps, NULL, da);
316 if (!vp || !vp->vp_length || (vp->vp_length > max_len)) return 0;
317
318 if (da->type == FR_TYPE_STRING) {
319 FR_DBUFF_IN_MEMCPY_RETURN(&work_dbuff, vp->vp_strvalue, vp->vp_length);
320 } else {
321 FR_DBUFF_IN_MEMCPY_RETURN(&work_dbuff, vp->vp_octets, vp->vp_length);
322 }
323
324 FR_PROTO_HEX_DUMP(fr_dbuff_start(&work_dbuff), fr_dbuff_used(&work_dbuff), da->name);
325
326 return fr_dbuff_set(dbuff, &work_dbuff);
327}
328
329static ssize_t tacacs_encode_chap(fr_dbuff_t *dbuff, fr_tacacs_packet_t *packet, fr_pair_list_t *vps, fr_dict_attr_t const *da_chap, fr_dict_attr_t const *da_challenge)
330{
331 fr_pair_t *chap, *challenge;
332 fr_dbuff_t work_dbuff = FR_DBUFF(dbuff);
333
334 chap = fr_pair_find_by_da(vps, NULL, da_chap);
335 if (!chap) {
336 packet->authen_start.data_len = 0;
337 return 0;
338 }
339
340 challenge = fr_pair_find_by_da(vps, NULL, da_challenge);
341 if (!challenge) {
342 fr_strerror_printf("Packet contains %s but no %s", da_chap->name, da_challenge->name);
343 return -1;
344 }
345
346 if (!challenge->vp_length) {
347 fr_strerror_printf("%s is empty", da_challenge->name);
348 return -1;
349 }
350
351 if (chap->vp_length <= 1) {
352 fr_strerror_printf("%s is too small", da_chap->name);
353 return -1;
354 }
355
356 if ((chap->vp_length + challenge->vp_length) > 255) {
357 fr_strerror_printf("%s and %s are longer than 255 octets", da_chap->name, da_challenge->name);
358 return -1;
359 }
360
361 FR_DBUFF_IN_MEMCPY_RETURN(&work_dbuff, chap->vp_octets, 1);
362 FR_DBUFF_IN_MEMCPY_RETURN(&work_dbuff, challenge->vp_octets, challenge->vp_length);
363 FR_DBUFF_IN_MEMCPY_RETURN(&work_dbuff, chap->vp_octets + 1, chap->vp_length - 1);
364
365 packet->authen_start.data_len = chap->vp_length + challenge->vp_length;
366
367 return fr_dbuff_set(dbuff, &work_dbuff);
368}
369
370/*
371 * Magic macros to keep things happy.
372 *
373 * Note that the various fields are optional. If the caller
374 * doesn't specify them, then they don't get encoded.
375 */
376#define ENCODE_FIELD_UINT8(_field, _da) do { \
377 vp = fr_pair_find_by_da(vps, NULL, _da); \
378 _field = (typeof(_field))((vp) ? vp->vp_uint8 : 0); \
379} while (0)
380
381#define ENCODE_FIELD_STRING8(_field, _da) do { \
382 ssize_t _slen = tacacs_encode_field(&work_dbuff, vps, _da, 0xff); \
383 if (_slen < 0) { \
384 fr_strerror_printf("Failed encoding %s", (_da)->name); \
385 return -1; \
386 } \
387 _field = (uint8_t) _slen; \
388} while (0)
389
390#define ENCODE_FIELD_STRING16(_field, _da) do { \
391 ssize_t _slen = tacacs_encode_field(&work_dbuff, vps, _da, 0xffff); \
392 if (_slen < 0) { \
393 fr_strerror_printf("Failed encoding %s", (_da)->name); \
394 return -1; \
395 } \
396 _field = htons((uint16_t) _slen); \
397} while (0)
398
399/**
400 * Encode VPS into a raw TACACS packet.
401 */
402ssize_t fr_tacacs_encode(fr_dbuff_t *dbuff, uint8_t const *original_packet, char const *secret, size_t secret_len,
404{
405 fr_pair_t *vp;
406 fr_tacacs_packet_t *packet;
407 fr_dcursor_t cursor;
408 fr_da_stack_t da_stack;
409 ssize_t len = 0;
410 size_t body_len, packet_len;
411 fr_dbuff_t work_dbuff = FR_DBUFF(dbuff);
412 fr_dbuff_marker_t hdr, body, hdr_io;
413 uint8_t version_byte = 0;
414
415 fr_tacacs_packet_hdr_t const *original = (fr_tacacs_packet_hdr_t const *) original_packet;
416
417 if (!vps) {
418 fr_strerror_const("Cannot encode empty packet");
419 return -1;
420 }
421
422 /*
423 * Verify space for the packet...
424 */
425 FR_DBUFF_REMAINING_RETURN(&work_dbuff, sizeof(fr_tacacs_packet_t));
426
427 /*
428 * 3.4. The TACACS+ Packet Header
429 *
430 * 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8
431 * +----------------+----------------+----------------+----------------+
432 * |major | minor | | | |
433 * |version| version| type | seq_no | flags |
434 * +----------------+----------------+----------------+----------------+
435 * | |
436 * | session_id |
437 * +----------------+----------------+----------------+----------------+
438 * | |
439 * | length |
440 * +----------------+----------------+----------------+----------------+
441 */
442
443 /*
444 * Let's keep reference for packet header.
445 */
446 fr_dbuff_marker(&hdr, &work_dbuff);
447 /*
448 * Add marker letting us read/write header bytes without moving hdr.
449 */
450 fr_dbuff_marker(&hdr_io, &work_dbuff);
451
452 /*
453 * Handle the fields in-place.
454 */
455 packet = (fr_tacacs_packet_t *)fr_dbuff_start(&work_dbuff);
456
457 /*
458 * Find the first attribute which is parented by TACACS-Packet.
459 */
460 for (vp = fr_pair_dcursor_init(&cursor, vps);
461 vp;
462 vp = fr_dcursor_next(&cursor)) {
463 if (vp->da == attr_tacacs_packet) break;
464 if (vp->da->parent == attr_tacacs_packet) break;
465 }
466
467 /*
468 * No "Packet" struct to encode. We MUST have an original packet to copy the various fields
469 * from.
470 */
471 if (!vp) {
472 if (!original) {
473 fr_strerror_printf("%s: No TACACS+ %s in the attribute list",
474 __FUNCTION__, attr_tacacs_packet->name);
475 return -1;
476 }
477
478 /*
479 * Initialize the buffer avoiding invalid values.
480 */
481 memset(packet, 0, sizeof(fr_tacacs_packet_t));
482
483 /*
484 * Initialize the reply from the request.
485 *
486 * Make room and fill up the original header. We shouldn't just copy the original packet,
487 * because the fields 'seq_no' and 'length' are not the same.
488 */
490
491 } else if (vp->da == attr_tacacs_packet) {
492 fr_dcursor_t child_cursor;
493
495 FR_PROTO_STACK_PRINT(&da_stack, 0);
496
497 fr_pair_dcursor_init(&child_cursor, &vp->vp_group);
498
499 /*
500 * Call the struct encoder to do the actual work,
501 * which fills the struct fields with zero if the member VP is not used.
502 */
503 len = fr_struct_to_network(&work_dbuff, &da_stack, 0, &child_cursor, NULL, NULL, NULL);
504 if (len != sizeof(fr_tacacs_packet_hdr_t)) {
505 fr_strerror_printf("%s: Failed encoding %s using fr_struct_to_network()",
506 __FUNCTION__, attr_tacacs_packet->name);
507 return -1;
508 }
509 fr_dcursor_next(&cursor);
510
511 } else {
513 FR_PROTO_STACK_PRINT(&da_stack, 0);
514
515 /*
516 * Call the struct encoder to do the actual work,
517 * which fills the struct fields with zero if the member VP is not used.
518 */
519 len = fr_struct_to_network(&work_dbuff, &da_stack, 0, &cursor, NULL, NULL, NULL);
520 if (len != sizeof(fr_tacacs_packet_hdr_t)) {
521 fr_strerror_printf("%s: Failed encoding %s using fr_struct_to_network()",
522 __FUNCTION__, attr_tacacs_packet->name);
523 return -1;
524 }
525 }
526
527 /*
528 * Ensure that we send a sane reply to a request.
529 */
530 if (original) {
531 packet->hdr.version = original->version;
532 packet->hdr.type = original->type;
533 packet->hdr.flags = original->flags; /* encrypted && single connection */
534 packet->hdr.session_id = original->session_id;
535
536 /*
537 * The client may not set SINGLE_CONNECT flag. So if the administrator has set it in the reply,
538 * we allow setting the flag. This lets the server tell the client that it supports "single
539 * connection" mode.
540 */
542 if (vp) packet->hdr.flags |= (vp->vp_uint8 & FR_TAC_PLUS_SINGLE_CONNECT_FLAG);
543 }
544
545 /*
546 * Starting here is a 'body' that may require encryption.
547 */
548 fr_dbuff_marker(&body, &work_dbuff);
549
550 /*
551 * Encode 8 octets of various fields not members of STRUCT
552 */
553 switch (packet->hdr.type) {
555 /*
556 * seq_no
557 *
558 * This is the sequence number of the current packet for the current session.
559 * The first packet in a session MUST have the sequence number 1 and each
560 * subsequent packet will increment the sequence number by one. Thus clients
561 * only send packets containing odd sequence numbers, and TACACS+ servers only
562 * send packets containing even sequence numbers.
563 */
564 if (packet_is_authen_start_request(packet)) { /* Start */
565 /**
566 * 4.1. The Authentication START Packet Body
567 *
568 * 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8
569 * +----------------+----------------+----------------+----------------+
570 * | action | priv_lvl | authen_type | authen_service |
571 * +----------------+----------------+----------------+----------------+
572 * | user_len | port_len | rem_addr_len | data_len |
573 * +----------------+----------------+----------------+----------------+
574 * | user ...
575 * +----------------+----------------+----------------+----------------+
576 * | port ...
577 * +----------------+----------------+----------------+----------------+
578 * | rem_addr ...
579 * +----------------+----------------+----------------+----------------+
580 * | data...
581 * +----------------+----------------+----------------+----------------+
582 */
583
584 /*
585 * Make room for such body request.
586 */
587 FR_DBUFF_ADVANCE_RETURN(&work_dbuff, sizeof(packet->authen_start));
588
589 /*
590 * Encode 4 octets of various flags.
591 */
592 ENCODE_FIELD_UINT8(packet->authen_start.action, attr_tacacs_action);
593 ENCODE_FIELD_UINT8(packet->authen_start.priv_lvl, attr_tacacs_privilege_level);
594 ENCODE_FIELD_UINT8(packet->authen_start.authen_type, attr_tacacs_authentication_type);
595 ENCODE_FIELD_UINT8(packet->authen_start.authen_service, attr_tacacs_authentication_service);
596
597 /*
598 * Encode 4 mandatory fields.
599 */
600 ENCODE_FIELD_STRING8(packet->authen_start.user_len, attr_tacacs_user_name);
601 ENCODE_FIELD_STRING8(packet->authen_start.port_len, attr_tacacs_client_port);
602 ENCODE_FIELD_STRING8(packet->authen_start.rem_addr_len, attr_tacacs_remote_address);
603
604 /*
605 * No explicit "Data" attribute, try to automatically determine what to do.
606 */
608 ENCODE_FIELD_STRING8(packet->authen_start.data_len, attr_tacacs_data);
609
610 } else switch (packet->authen_start.authen_type) {
611 default:
612 break;
613
614 case FR_AUTHENTICATION_TYPE_VALUE_PAP:
615 ENCODE_FIELD_STRING8(packet->authen_start.data_len, attr_tacacs_user_password);
616 break;
617
618 case FR_AUTHENTICATION_TYPE_VALUE_CHAP:
619 if (tacacs_encode_chap(&work_dbuff, packet, vps, attr_tacacs_chap_password, attr_tacacs_chap_challenge) < 0) return -1;
620 break;
621
622 case FR_AUTHENTICATION_TYPE_VALUE_MSCHAP: {
623 ssize_t slen;
624
626 if (slen < 0) return slen;
627
628 if (slen > 0) break;
629
630 if (tacacs_encode_chap(&work_dbuff, packet, vps, attr_tacacs_mschap2_response, attr_tacacs_mschap_challenge) < 0) return -1;
631 }
632 break;
633 }
634
635 goto check_request;
636
637 } else if (packet_is_authen_continue(packet)) {
638 /*
639 * 4.3. The Authentication CONTINUE Packet Body
640 *
641 * This packet is sent from the client to the server following the receipt of
642 * a REPLY packet.
643 *
644 * 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8
645 * +----------------+----------------+----------------+----------------+
646 * | user_msg len | data_len |
647 * +----------------+----------------+----------------+----------------+
648 * | flags | user_msg ...
649 * +----------------+----------------+----------------+----------------+
650 * | data ...
651 * +----------------+
652 */
653
654 /*
655 * Make room for such body request.
656 */
657 FR_DBUFF_ADVANCE_RETURN(&work_dbuff, sizeof(packet->authen_cont));
658
659 /*
660 * Encode 2 mandatory fields.
661 */
662 ENCODE_FIELD_STRING16(packet->authen_cont.user_msg_len, attr_tacacs_user_message);
663 ENCODE_FIELD_STRING16(packet->authen_cont.data_len, attr_tacacs_data);
664
665 /*
666 * Look at the abort flag after encoding the fields.
667 */
669
670 goto check_request;
671
672 } else if (packet_is_authen_reply(packet)) {
673 /*
674 * 4.2. The Authentication REPLY Packet Body
675 *
676 * 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8
677 * +----------------+----------------+----------------+----------------+
678 * | status | flags | server_msg_len |
679 * +----------------+----------------+----------------+----------------+
680 * | data_len | server_msg ...
681 * +----------------+----------------+----------------+----------------+
682 * | data ...
683 * +----------------+----------------+
684 */
685
686 /*
687 * Make room for such body request.
688 */
689 FR_DBUFF_ADVANCE_RETURN(&work_dbuff, sizeof(packet->authen_reply));
690
691 ENCODE_FIELD_UINT8(packet->authen_reply.status, attr_tacacs_authentication_status);
692 ENCODE_FIELD_UINT8(packet->authen_reply.flags, attr_tacacs_authentication_flags);
693
694 /*
695 * Encode 2 mandatory fields.
696 */
697 ENCODE_FIELD_STRING16(packet->authen_reply.server_msg_len, attr_tacacs_server_message);
698 ENCODE_FIELD_STRING16(packet->authen_reply.data_len, attr_tacacs_data);
699
700 goto check_reply;
701 }
702
703 fr_strerror_const("encode: Unknown authentication packet type");
704 return -1;
705
707 if (packet_is_author_request(packet)) {
708 /*
709 * 5.1. The Authorization REQUEST Packet Body
710 *
711 * 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8
712 * +----------------+----------------+----------------+----------------+
713 * | authen_method | priv_lvl | authen_type | authen_service |
714 * +----------------+----------------+----------------+----------------+
715 * | user_len | port_len | rem_addr_len | arg_cnt |
716 * +----------------+----------------+----------------+----------------+
717 * | arg_1_len | arg_2_len | ... | arg_N_len |
718 * +----------------+----------------+----------------+----------------+
719 * | user ...
720 * +----------------+----------------+----------------+----------------+
721 * | port ...
722 * +----------------+----------------+----------------+----------------+
723 * | rem_addr ...
724 * +----------------+----------------+----------------+----------------+
725 * | arg_1 ...
726 * +----------------+----------------+----------------+----------------+
727 * | arg_2 ...
728 * +----------------+----------------+----------------+----------------+
729 * | ...
730 * +----------------+----------------+----------------+----------------+
731 * | arg_N ...
732 * +----------------+----------------+----------------+----------------+
733 */
734
735 /*
736 * Make room for such body request.
737 */
738 FR_DBUFF_ADVANCE_RETURN(&work_dbuff, sizeof(packet->author_req));
739
740 /*
741 * Encode 4 octets of various flags.
742 */
743 ENCODE_FIELD_UINT8(packet->author_req.authen_method, attr_tacacs_authentication_method);
744 ENCODE_FIELD_UINT8(packet->author_req.priv_lvl, attr_tacacs_privilege_level);
745 ENCODE_FIELD_UINT8(packet->author_req.authen_type, attr_tacacs_authentication_type);
746 ENCODE_FIELD_UINT8(packet->author_req.authen_service, attr_tacacs_authentication_service);
747
748 /*
749 * Encode 'arg_N' arguments (horrible format)
750 */
751 packet->author_req.arg_cnt = tacacs_encode_body_arg_cnt(vps, attr_tacacs_argument_list);
752 if (packet->author_req.arg_cnt) FR_DBUFF_MEMSET_RETURN(&work_dbuff, 0, packet->author_req.arg_cnt);
753
754 /*
755 * Encode 3 mandatory fields.
756 */
757 ENCODE_FIELD_STRING8(packet->author_req.user_len, attr_tacacs_user_name);
758 ENCODE_FIELD_STRING8(packet->author_req.port_len, attr_tacacs_client_port);
759 ENCODE_FIELD_STRING8(packet->author_req.rem_addr_len, attr_tacacs_remote_address);
760
761 /*
762 * Append 'args_body' to the end of buffer
763 */
764 if (packet->author_req.arg_cnt > 0) {
765 if (tacacs_encode_body_arg_n(&work_dbuff, packet->author_req.arg_cnt, &packet->author_req.arg_len[0], vps, attr_tacacs_argument_list) < 0) return -1;
766 }
767
768 goto check_request;
769 } else if (packet_is_author_reply(packet)) {
770 /*
771 * 5.2. The Authorization RESPONSE Packet Body
772 *
773 * 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8
774 * +----------------+----------------+----------------+----------------+
775 * | status | arg_cnt | server_msg len |
776 * +----------------+----------------+----------------+----------------+
777 * + data_len | arg_1_len | arg_2_len |
778 * +----------------+----------------+----------------+----------------+
779 * | ... | arg_N_len | server_msg ...
780 * +----------------+----------------+----------------+----------------+
781 * | data ...
782 * +----------------+----------------+----------------+----------------+
783 * | arg_1 ...
784 * +----------------+----------------+----------------+----------------+
785 * | arg_2 ...
786 * +----------------+----------------+----------------+----------------+
787 * | ...
788 * +----------------+----------------+----------------+----------------+
789 * | arg_N ...
790 * +----------------+----------------+----------------+----------------+
791 */
792
793 /*
794 * Make room for such body request.
795 */
796 FR_DBUFF_ADVANCE_RETURN(&work_dbuff, sizeof(packet->author_reply));
797
798 /*
799 * Encode 1 mandatory field.
800 */
801 ENCODE_FIELD_UINT8(packet->author_reply.status, attr_tacacs_authorization_status);
802
803 /*
804 * Encode 'arg_N' arguments (horrible format)
805 *
806 * For ERRORs, we don't encode arguments.
807 *
808 * 5.2
809 *
810 * A status of TAC_PLUS_AUTHOR_STATUS_ERROR indicates an error occurred
811 * on the server. For the differences between ERROR and FAIL, refer to
812 * section Session Completion (Section 3.4) . None of the arg values
813 * have any relevance if an ERROR is set, and must be ignored.
814 *
815 * When the status equals TAC_PLUS_AUTHOR_STATUS_FOLLOW, then the
816 * arg_cnt MUST be 0.
817 */
818 if (!((packet->author_reply.status == FR_AUTHORIZATION_STATUS_VALUE_ERROR) ||
819 (packet->author_reply.status == FR_AUTHORIZATION_STATUS_VALUE_FOLLOW))) {
820 packet->author_reply.arg_cnt = tacacs_encode_body_arg_cnt(vps, attr_tacacs_argument_list);
821 if (packet->author_reply.arg_cnt) FR_DBUFF_MEMSET_RETURN(&work_dbuff, 0, packet->author_reply.arg_cnt);
822 } else {
823 packet->author_reply.arg_cnt = 0;
824 }
825
826 /*
827 * Encode 2 mandatory fields.
828 */
829 ENCODE_FIELD_STRING16(packet->author_reply.server_msg_len, attr_tacacs_server_message);
830 ENCODE_FIELD_STRING16(packet->author_reply.data_len, attr_tacacs_data);
831
832 /*
833 * Append 'args_body' to the end of buffer
834 */
835 if (packet->author_reply.arg_cnt > 0) {
836 if (tacacs_encode_body_arg_n(&work_dbuff, packet->author_reply.arg_cnt, &packet->author_reply.arg_len[0], vps, attr_tacacs_argument_list) < 0) return -1;
837 }
838
839 goto check_reply;
840
841 }
842
843 fr_strerror_const("encode: Unknown authorization packet type");
844 return -1;
845
846 case FR_TAC_PLUS_ACCT:
847 if (packet_is_acct_request(packet)) {
848 /**
849 * 6.1. The Account REQUEST Packet Body
850 *
851 * 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8
852 * +----------------+----------------+----------------+----------------+
853 * | flags | authen_method | priv_lvl | authen_type |
854 * +----------------+----------------+----------------+----------------+
855 * | authen_service | user_len | port_len | rem_addr_len |
856 * +----------------+----------------+----------------+----------------+
857 * | arg_cnt | arg_1_len | arg_2_len | ... |
858 * +----------------+----------------+----------------+----------------+
859 * | arg_N_len | user ...
860 * +----------------+----------------+----------------+----------------+
861 * | port ...
862 * +----------------+----------------+----------------+----------------+
863 * | rem_addr ...
864 * +----------------+----------------+----------------+----------------+
865 * | arg_1 ...
866 * +----------------+----------------+----------------+----------------+
867 * | arg_2 ...
868 * +----------------+----------------+----------------+----------------+
869 * | ...
870 * +----------------+----------------+----------------+----------------+
871 * | arg_N ...
872 * +----------------+----------------+----------------+----------------+
873 */
874
875 /*
876 * Make room for such body request.
877 */
878 FR_DBUFF_ADVANCE_RETURN(&work_dbuff, sizeof(packet->acct_req));
879
880 /*
881 * Encode 5 octets of various flags.
882 */
883 ENCODE_FIELD_UINT8(packet->acct_req.flags, attr_tacacs_accounting_flags);
884 ENCODE_FIELD_UINT8(packet->acct_req.authen_method, attr_tacacs_authentication_method);
885 ENCODE_FIELD_UINT8(packet->acct_req.priv_lvl, attr_tacacs_privilege_level);
886 ENCODE_FIELD_UINT8(packet->acct_req.authen_type, attr_tacacs_authentication_type);
887 ENCODE_FIELD_UINT8(packet->acct_req.authen_service, attr_tacacs_authentication_service);
888
889 /*
890 * Encode 'arg_N' arguments (horrible format)
891 */
892 packet->acct_req.arg_cnt = tacacs_encode_body_arg_cnt(vps, attr_tacacs_argument_list);
893 if (packet->acct_req.arg_cnt) FR_DBUFF_MEMSET_RETURN(&work_dbuff, 0, packet->acct_req.arg_cnt);
894
895 /*
896 * Encode 3 mandatory fields.
897 */
898 ENCODE_FIELD_STRING8(packet->acct_req.user_len, attr_tacacs_user_name);
899 ENCODE_FIELD_STRING8(packet->acct_req.port_len, attr_tacacs_client_port);
900 ENCODE_FIELD_STRING8(packet->acct_req.rem_addr_len, attr_tacacs_remote_address);
901
902 /*
903 * Append 'args_body' to the end of buffer
904 */
905 if (packet->acct_req.arg_cnt > 0) {
906 if (tacacs_encode_body_arg_n(&work_dbuff, packet->acct_req.arg_cnt, &packet->acct_req.arg_len[0], vps, attr_tacacs_argument_list) < 0) return -1;
907 }
908
909 check_request:
910 /*
911 * Just to avoid malformed packet.
912 */
913 fr_dbuff_set(&hdr_io, &hdr);
914 (void) fr_dbuff_out(&version_byte, &hdr_io);
915 if (!version_byte) {
916 version_byte = 0xc1; /* version 12.1 */
917 fr_dbuff_set(&hdr_io, &hdr);
918 FR_DBUFF_IN_RETURN(&hdr_io, version_byte);
919 }
920 /*
921 * If the caller didn't set a session ID, use a random one.
922 */
924 packet->hdr.session_id = fr_rand();
925 }
926
927 /*
928 * Requests have odd sequence numbers.
929 */
930 packet->hdr.seq_no |= 0x01;
931 break;
932
933 } else if (packet_is_acct_reply(packet)) {
934 /**
935 * 6.2. The Accounting REPLY Packet Body
936 *
937 * 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8
938 * +----------------+----------------+----------------+----------------+
939 * | server_msg len | data_len |
940 * +----------------+----------------+----------------+----------------+
941 * | status | server_msg ...
942 * +----------------+----------------+----------------+----------------+
943 * | data ...
944 * +----------------+
945 */
946
947 /*
948 * Make room for such body request.
949 */
950 FR_DBUFF_ADVANCE_RETURN(&work_dbuff, sizeof(packet->acct_reply));
951
952 /*
953 * Encode 2 mandatory fields.
954 */
955 ENCODE_FIELD_STRING16(packet->acct_reply.server_msg_len, attr_tacacs_server_message);
956 ENCODE_FIELD_STRING16(packet->acct_reply.data_len, attr_tacacs_data);
957
958 /*
959 * And also, the status field.
960 */
961 ENCODE_FIELD_UINT8(packet->acct_reply.status, attr_tacacs_accounting_status);
962
963 check_reply:
964 /*
965 * fr_struct_to_network() fills the struct fields with 0
966 * if there is no matching VP. In the interest of making
967 * things easier for the user, we don't require them to
968 * copy all of the fields from the request to the reply.
969 *
970 * Instead, we copy the fields manually, and ensure that
971 * they have the correct values.
972 */
973 if (original) {
974 fr_dbuff_set(&hdr_io, &hdr);
975 fr_dbuff_out(&version_byte, &hdr_io);
976 if (!version_byte) {
977 packet->hdr.version = original->version;
978 }
979
980 if (!packet->hdr.seq_no) {
981 packet->hdr.seq_no = original->seq_no + 1; /* uint8_t */
982 }
983
984 if (!packet->hdr.session_id) {
985 packet->hdr.session_id = original->session_id;
986 }
987 }
988
989 /*
990 * Replies have even sequence numbers.
991 */
992 packet->hdr.seq_no &= 0xfe;
993 break;
994 }
995
996 fr_strerror_const("encode: Unknown accounting packet type");
997 return -1;
998
999 default:
1000 fr_strerror_printf("encode: unknown packet type %d", packet->hdr.type);
1001 return -1;
1002 }
1003
1004 /*
1005 * Force the correct header type, and randomly-placed
1006 * status fields. But only if there's no code field.
1007 * Only the unit tests pass a zero code field, as that's
1008 * normally invalid. The unit tests ensure that all of
1009 * the VPs are passed to encode a packet, and they all
1010 * must be correct
1011 */
1012 if (code && (fr_tacacs_code_to_packet(packet, code) < 0)) return -1;
1013
1014 /*
1015 * The packet length we store in the header doesn't
1016 * include the size of the header. But we tell the
1017 * caller about the total length of the packet.
1018 */
1019 packet_len = fr_dbuff_used(&work_dbuff);
1020 fr_assert(packet_len >= sizeof(fr_tacacs_packet_hdr_t));
1021
1022 body_len = (packet_len - sizeof(fr_tacacs_packet_hdr_t));
1023 fr_assert(packet_len < FR_MAX_PACKET_SIZE);
1024 packet->hdr.length = htonl(body_len);
1025
1026 /*
1027 * If the original packet is encrypted, then the reply
1028 * MUST be encrypted too.
1029 *
1030 * On the other hand, if the request is unencrypted,
1031 * we're OK with sending an encrypted reply. Because,
1032 * whatever.
1033 */
1034 if (original &&
1035 ((original->flags & FR_TAC_PLUS_UNENCRYPTED_FLAG) == 0)) {
1036 packet->hdr.flags &= ~FR_TAC_PLUS_UNENCRYPTED_FLAG;
1037 }
1038
1039#ifndef NDEBUG
1040 if (fr_debug_lvl >= L_DBG_LVL_4) {
1041 fr_tacacs_flags_t flags = packet->hdr.flags;
1042
1044 fr_tacacs_packet_log_hex(&default_log, packet, packet_len);
1045 packet->hdr.flags = flags;
1046 }
1047#endif
1048
1049 /*
1050 * 3.6. Encryption
1051 *
1052 * Packets are encrypted if the unencrypted flag is clear.
1053 */
1054 if (secret) {
1055 if (!secret_len) {
1056 fr_strerror_const("Packet should be decrypted, but the secret has zero length");
1057 return -1;
1058 }
1059
1060 FR_PROTO_HEX_DUMP(fr_dbuff_start(&work_dbuff), packet_len, "fr_tacacs_packet_t (unencrypted)");
1061
1062 if (fr_tacacs_body_xor(packet, fr_dbuff_current(&body), body_len, secret, secret_len) != 0) return -1;
1063
1064 packet->hdr.flags &= ~FR_TAC_PLUS_UNENCRYPTED_FLAG;
1065 } else {
1066 /*
1067 * Packets which have no secret cannot be encrypted.
1068 */
1070
1071 }
1072
1073 FR_PROTO_HEX_DUMP(fr_dbuff_start(&work_dbuff), packet_len, "fr_tacacs_packet_t (encoded)");
1074
1075 return fr_dbuff_set(dbuff, &work_dbuff);
1076}
1077
1078/*
1079 * Test points for protocol encode
1080 */
1082 uint8_t *data, size_t data_len, void *proto_ctx)
1083{
1084 fr_tacacs_ctx_t *test_ctx = talloc_get_type_abort(proto_ctx, fr_tacacs_ctx_t);
1085 fr_pair_t *vp;
1086
1087 /*
1088 * A Packet-Type in the input sets the packet code, as
1089 * request->reply->code does for proto_tacacs.
1090 */
1092
1093 return fr_tacacs_encode(&FR_DBUFF_TMP(data, data_len), NULL, test_ctx->secret,
1095 vp ? (fr_tacacs_packet_code_t) vp->vp_uint32 : FR_TACACS_CODE_INVALID, vps);
1096}
1097
1099{
1101
1103
1104 return 0;
1105}
1106
1107static int encode_test_ctx(void **out, TALLOC_CTX *ctx, UNUSED fr_dict_t const *dict,
1109{
1111
1112 if (fr_tacacs_global_init() < 0) return -1;
1113
1114 test_ctx = talloc_zero(ctx, fr_tacacs_ctx_t);
1115 if (!test_ctx) return -1;
1116
1118 talloc_set_destructor(test_ctx, _encode_test_ctx);
1119
1120 *out = test_ctx;
1121
1122 return 0;
1123}
1124
1125/*
1126 * Test points
1127 */
static int const char char buffer[256]
Definition acutest.h:641
#define UNUSED
Definition build.h:415
fr_dict_attr_t const * root_da
Definition common.c:32
#define fr_dbuff_used(_dbuff_or_marker)
Return the number of bytes remaining between the start of the dbuff or marker and the current positio...
Definition dbuff.h:819
#define FR_DBUFF_ADVANCE_RETURN(_dbuff_or_marker, _len)
Advance the 'current' position in dbuff or marker by _len bytes returning if _len is out of range.
Definition dbuff.h:1137
struct fr_dbuff_marker_s fr_dbuff_marker_t
A position marker associated with a dbuff.
Definition dbuff.h:81
#define fr_dbuff_current(_dbuff_or_marker)
Return the 'current' position of a dbuff or marker.
Definition dbuff.h:963
#define fr_dbuff_set(_dst, _src)
Set the 'current' position in a dbuff or marker using another dbuff or marker, a char pointer,...
Definition dbuff.h:1056
#define fr_dbuff_start(_dbuff_or_marker)
Return the 'start' position of a dbuff or marker.
Definition dbuff.h:950
#define FR_DBUFF_MEMSET_RETURN(_dbuff_or_marker, _c, _inlen)
Set _inlen bytes of a dbuff or marker to _c returning if there is insufficient space.
Definition dbuff.h:1561
static uint8_t * fr_dbuff_marker(fr_dbuff_marker_t *m, fr_dbuff_t *dbuff)
Initialises a new marker pointing to the 'current' position of the dbuff.
Definition dbuff.h:1245
#define FR_DBUFF_IN_MEMCPY_RETURN(_dbuff_or_marker, _in, _inlen)
Copy exactly _inlen bytes into dbuff or marker returning if there's insufficient space.
Definition dbuff.h:1435
#define FR_DBUFF_ERROR_OFFSET(_slen, _offset)
Generic wrapper to return an error and an offset from encoding.
Definition dbuff.h:196
#define FR_DBUFF_REMAINING_RETURN(_dbuff_or_marker, _len)
Check if _len bytes are available in the dbuff and if not return the number of bytes we'd need.
Definition dbuff.h:813
#define FR_DBUFF_IN_RETURN(_dbuff_or_marker, _in)
Copy data from a fixed sized C type into a dbuff returning if there is insufficient space.
Definition dbuff.h:1638
#define FR_DBUFF(_dbuff_or_marker)
Create a new dbuff pointing to the same underlying buffer.
Definition dbuff.h:230
#define FR_DBUFF_MAX(_dbuff_or_marker, _max)
Limit the maximum number of bytes available in the dbuff when passing it to another function.
Definition dbuff.h:334
#define fr_dbuff_out(_out, _dbuff_or_marker)
Copy data from a dbuff or marker to a fixed sized C type.
Definition dbuff.h:1852
#define FR_DBUFF_IN_BYTES_RETURN(_dbuff_or_marker,...)
Copy a byte sequence into a dbuff or marker returning if there's insufficient space.
Definition dbuff.h:1525
#define FR_DBUFF_TMP(_start, _len_or_end)
Creates a compound literal to pass into functions which accept a dbuff.
Definition dbuff.h:556
static void * fr_dcursor_next(fr_dcursor_t *cursor)
Advanced the cursor to the next item.
Definition dcursor.h:288
fr_dict_t const * fr_dict_by_da(fr_dict_attr_t const *da)
Attempt to locate the protocol dictionary containing an attribute.
Definition dict_util.c:2935
fr_dict_attr_t const * fr_dict_root(fr_dict_t const *dict)
Return the root attribute of a dictionary.
Definition dict_util.c:2717
int fr_debug_lvl
Definition log.c:41
fr_log_t default_log
Definition log.c:308
@ L_DBG_LVL_4
4th highest priority debug messages (-xxxx | -Xxx).
Definition log.h:70
@ FR_TYPE_TIME_DELTA
A period of time measured in nanoseconds.
@ FR_TYPE_STRING
String of printable characters.
@ FR_TYPE_DATE
Unix time stamp, always has value >2^31.
@ FR_TYPE_VENDOR
Attribute that represents a vendor in the attribute tree.
@ FR_TYPE_UINT64
64 Bit unsigned integer.
long int ssize_t
unsigned char uint8_t
#define UINT8_MAX
fr_pair_t * fr_pair_find_by_da_nested(fr_pair_list_t const *list, fr_dict_attr_t const *da)
Find a pair with a matching fr_dict_attr_t, by walking the nested fr_dict_attr_t tree.
Definition pair.c:791
fr_pair_t * fr_pair_find_by_da(fr_pair_list_t const *list, fr_pair_t const *prev, fr_dict_attr_t const *da)
Find the first pair with a matching da.
Definition pair.c:716
static fr_dict_attr_t const * attr_tacacs_authentication_flags
Definition base.c:53
static fr_dict_attr_t const * attr_tacacs_user_message
Definition base.c:70
static fr_dict_attr_t const * attr_tacacs_session_id
Definition base.c:67
static fr_dict_attr_t const * attr_tacacs_server_message
Definition base.c:66
static fr_dict_attr_t const * attr_tacacs_privilege_level
Definition base.c:64
static fr_dict_attr_t const * attr_tacacs_authentication_type
Definition base.c:54
static fr_dict_attr_t const * attr_tacacs_accounting_status
Definition base.c:59
static fr_dict_attr_t const * attr_tacacs_authentication_service
Definition base.c:55
static fr_dict_attr_t const * attr_tacacs_authentication_status
Definition base.c:56
static fr_dict_attr_t const * attr_tacacs_client_port
Definition base.c:62
static fr_dict_attr_t const * attr_tacacs_remote_address
Definition base.c:65
static fr_dict_attr_t const * attr_tacacs_action
Definition base.c:51
static fr_dict_attr_t const * attr_tacacs_authorization_status
Definition base.c:58
static fr_dict_attr_t const * attr_tacacs_accounting_flags
Definition base.c:60
static fr_dict_attr_t const * attr_tacacs_data
Definition base.c:63
void fr_proto_da_stack_build(fr_da_stack_t *stack, fr_dict_attr_t const *da)
Build a complete DA stack from the da back to the root.
Definition proto.c:118
static fr_dict_t const * dict_tacacs
static fr_dict_attr_t const * attr_tacacs_user_name
static int encode_test_ctx(void **out, TALLOC_CTX *ctx, UNUSED fr_dict_t const *dict, UNUSED fr_dict_attr_t const *root_da)
Definition encode.c:165
uint8_t const * secret
Definition bio.h:34
HIDDEN fr_dict_attr_t const * attr_tacacs_argument_list
Definition base.c:55
HIDDEN fr_dict_attr_t const * attr_tacacs_chap_password
Definition base.c:74
HIDDEN fr_dict_attr_t const * attr_tacacs_mschap_challenge
Definition base.c:78
HIDDEN fr_dict_attr_t const * attr_tacacs_packet_type
Definition base.c:62
HIDDEN fr_dict_attr_t const * attr_tacacs_packet
Definition base.c:60
HIDDEN fr_dict_attr_t const * attr_tacacs_mschap2_response
Definition base.c:77
HIDDEN fr_dict_attr_t const * attr_tacacs_authentication_continue_flags
Definition base.c:49
HIDDEN fr_dict_attr_t const * attr_tacacs_user_password
Definition base.c:73
HIDDEN fr_dict_attr_t const * attr_tacacs_chap_challenge
Definition base.c:75
HIDDEN fr_dict_attr_t const * attr_tacacs_mschap_response
Definition base.c:76
HIDDEN fr_dict_attr_t const * attr_tacacs_authentication_method
Definition base.c:50
HIDDEN fr_dict_attr_t const * attr_tacacs_flags
Definition base.c:58
int fr_tacacs_body_xor(fr_tacacs_packet_t const *pkt, uint8_t *body, size_t body_len, char const *secret, size_t secret_len)
XOR the body based on the secret key.
Definition base.c:148
void fr_tacacs_global_free(void)
Definition base.c:610
int fr_tacacs_global_init(void)
Definition base.c:586
static ssize_t tacacs_encode_chap(fr_dbuff_t *dbuff, fr_tacacs_packet_t *packet, fr_pair_list_t *vps, fr_dict_attr_t const *da_chap, fr_dict_attr_t const *da_challenge)
Definition encode.c:329
static ssize_t tacacs_encode_field(fr_dbuff_t *dbuff, fr_pair_list_t *vps, fr_dict_attr_t const *da, size_t max_len)
Definition encode.c:310
#define ENCODE_FIELD_UINT8(_field, _da)
Definition encode.c:376
static uint8_t tacacs_encode_body_arg_cnt(fr_pair_list_t *vps, fr_dict_attr_t const *da)
Encode a TACACS+ 'arg_N' fields.
Definition encode.c:138
static ssize_t fr_tacacs_encode_proto(UNUSED TALLOC_CTX *ctx, fr_pair_list_t *vps, uint8_t *data, size_t data_len, void *proto_ctx)
Definition encode.c:1081
#define ENCODE_FIELD_STRING8(_field, _da)
Definition encode.c:381
fr_test_point_proto_encode_t tacacs_tp_encode_proto
Definition encode.c:1129
ssize_t fr_tacacs_encode(fr_dbuff_t *dbuff, uint8_t const *original_packet, char const *secret, size_t secret_len, fr_tacacs_packet_code_t code, fr_pair_list_t *vps)
Encode VPS into a raw TACACS packet.
Definition encode.c:402
#define ENCODE_FIELD_STRING16(_field, _da)
Definition encode.c:390
static ssize_t tacacs_encode_body_arg_n(fr_dbuff_t *dbuff, uint8_t arg_cnt, uint8_t *arg_len, fr_pair_list_t *vps, fr_dict_attr_t const *da)
Definition encode.c:188
int fr_tacacs_code_to_packet(fr_tacacs_packet_t *pkt, fr_tacacs_packet_code_t code)
Definition encode.c:36
static int _encode_test_ctx(UNUSED fr_tftp_ctx_t *proto_ctx)
Definition encode.c:250
VQP attributes.
#define fr_assert(_expr)
Definition rad_assert.h:37
static char * secret
static fr_radius_bio_verify_t test_ctx(void)
Make a verify context which allows Access-Request and Access-Accept.
uint32_t fr_rand(void)
Return a 32-bit random number.
Definition rand.c:128
ssize_t fr_sbuff_in_sprintf(fr_sbuff_t *sbuff, char const *fmt,...)
Print using a fmt string to an sbuff.
Definition sbuff.c:1680
#define FR_SBUFF_OUT(_start, _len_or_end)
fr_pair_t * vp
ssize_t fr_struct_to_network(fr_dbuff_t *dbuff, fr_da_stack_t *da_stack, unsigned int depth, fr_dcursor_t *parent_cursor, void *encode_ctx, fr_encode_dbuff_t encode_value, fr_encode_dbuff_t encode_pair)
Definition struct.c:760
Stores an attribute, a value and various bits of other data.
Definition pair.h:68
fr_dict_attr_t const *_CONST da
Dictionary attribute defines the attribute number, vendor and type of the pair.
Definition pair.h:69
#define packet_is_authen_reply(p)
Definition tacacs.h:51
@ FR_TAC_PLUS_ACCT_STATUS_SUCCESS
Definition tacacs.h:255
@ FR_TAC_PLUS_ACCT_STATUS_ERROR
Definition tacacs.h:256
fr_tacacs_packet_code_t
Definition tacacs.h:292
@ FR_TACACS_CODE_INVALID
Definition tacacs.h:293
#define packet_is_authen_continue(p)
Definition tacacs.h:50
#define fr_tacacs_packet_log_hex(_log, _packet, _size)
Definition tacacs.h:355
#define packet_is_acct_reply(p)
Definition tacacs.h:57
@ FR_TAC_PLUS_AUTHEN_STATUS_PASS
Definition tacacs.h:151
@ FR_TAC_PLUS_AUTHEN_STATUS_GETDATA
Definition tacacs.h:153
@ FR_TAC_PLUS_AUTHEN_STATUS_ERROR
Definition tacacs.h:157
@ FR_TAC_PLUS_AUTHEN_STATUS_GETUSER
Definition tacacs.h:154
@ FR_TAC_PLUS_AUTHEN_STATUS_FAIL
Definition tacacs.h:152
@ FR_TAC_PLUS_AUTHEN_STATUS_RESTART
Definition tacacs.h:156
@ FR_TAC_PLUS_AUTHEN_STATUS_GETPASS
Definition tacacs.h:155
fr_tacacs_flags_t
Definition tacacs.h:77
@ FR_TAC_PLUS_SINGLE_CONNECT_FLAG
Definition tacacs.h:80
@ FR_TAC_PLUS_UNENCRYPTED_FLAG
Definition tacacs.h:79
#define packet_is_author_request(p)
Definition tacacs.h:53
#define packet_is_acct_request(p)
Definition tacacs.h:56
#define packet_is_authen_start_request(p)
3.4.
Definition tacacs.h:49
@ FR_TAC_PLUS_CONTINUE_FLAG_UNSET
Definition tacacs.h:174
@ FR_TAC_PLUS_CONTINUE_FLAG_ABORT
Definition tacacs.h:175
fr_tacacs_packet_hdr_t hdr
Definition tacacs.h:277
fr_tacacs_flags_t flags
Definition tacacs.h:99
fr_tacacs_type_t type
Definition tacacs.h:97
#define FR_MAX_PACKET_SIZE
Definition tacacs.h:27
@ FR_TAC_PLUS_ACCT
Definition tacacs.h:67
@ FR_TAC_PLUS_AUTHEN
Definition tacacs.h:65
@ FR_TAC_PLUS_AUTHOR
Definition tacacs.h:66
@ FR_TAC_PLUS_AUTHOR_STATUS_PASS_ADD
Definition tacacs.h:211
@ FR_TAC_PLUS_AUTHOR_STATUS_ERROR
Definition tacacs.h:214
@ FR_TAC_PLUS_AUTHOR_STATUS_FAIL
Definition tacacs.h:213
@ FR_TAC_PLUS_AUTHOR_STATUS_PASS_REPL
Definition tacacs.h:212
#define packet_is_author_reply(p)
Definition tacacs.h:54
Used as the decoder ctx.
Definition tacacs.h:329
static int talloc_const_free(void const *ptr)
Free const'd memory.
Definition talloc.h:289
static size_t talloc_strlen(char const *s)
Returns the length of a talloc array containing a string.
Definition talloc.h:143
fr_test_point_ctx_alloc_t test_ctx
Allocate a test ctx for the encoder.
Definition test_point.h:76
Entry point for protocol encoders.
Definition test_point.h:75
@ T_BARE_WORD
Definition token.h:118
fr_pair_t * fr_pair_list_next(fr_pair_list_t const *list, fr_pair_t const *item))
Get the next item in a valuepair list after a specific entry.
Definition pair_inline.c:69
ssize_t fr_pair_print_value_quoted(fr_sbuff_t *out, fr_pair_t const *vp, fr_token_t quote)
Print the value of an attribute to a string.
Definition pair_print.c:59
#define fr_pair_dcursor_init(_cursor, _list)
Initialises a special dcursor with callbacks that will maintain the attr sublists correctly.
Definition pair.h:605
fr_pair_t * fr_pair_list_head(fr_pair_list_t const *list)
Get the head of a valuepair list.
Definition pair_inline.c:42
#define FR_PROTO_HEX_DUMP(_data, _data_len, _fmt,...)
Definition proto.h:42
#define FR_PROTO_TRACE(_fmt,...)
Definition proto.h:41
#define FR_PROTO_STACK_PRINT(_stack, _depth)
Definition proto.h:44
Structure for holding the stack of dictionary attributes being encoded.
Definition proto.h:55
#define fr_strerror_printf(_fmt,...)
Log to thread local error buffer.
Definition strerror.h:64
#define fr_strerror_const(_msg)
Definition strerror.h:223
int fr_value_box_cast(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_type_t dst_type, fr_dict_attr_t const *dst_enumv, fr_value_box_t const *src)
Convert one type of fr_value_box_t to another.
Definition value.c:4071
static fr_slen_t data
Definition value.h:1366
static fr_sbuff_err_t char ** out
Definition value.h:1061
static fr_sbuff_err_t char size_t * len
Definition value.h:1061
#define fr_value_box_init(_vb, _type, _enumv, _tainted)
Initialise a fr_value_box_t.
Definition value.h:634