]> git.proxmox.com Git - mirror_frr.git/blob - lib/keychain.c
zebra, lib: fix the ZEBRA_INTERFACE_VRF_UPDATE zapi message
[mirror_frr.git] / lib / keychain.c
1 /* key-chain for authentication.
2 * Copyright (C) 2000 Kunihiro Ishiguro
3 *
4 * This file is part of GNU Zebra.
5 *
6 * GNU Zebra is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published
8 * by the Free Software Foundation; either version 2, or (at your
9 * option) any later version.
10 *
11 * GNU Zebra is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #include <zebra.h>
22
23 #include "command.h"
24 #include "memory.h"
25 #include "linklist.h"
26 #include "keychain.h"
27
28 DEFINE_MTYPE_STATIC(LIB, KEY, "Key")
29 DEFINE_MTYPE_STATIC(LIB, KEYCHAIN, "Key chain")
30
31 DEFINE_QOBJ_TYPE(keychain)
32 DEFINE_QOBJ_TYPE(key)
33
34 /* Master list of key chain. */
35 struct list *keychain_list;
36
37 static struct keychain *keychain_new(void)
38 {
39 struct keychain *keychain;
40 keychain = XCALLOC(MTYPE_KEYCHAIN, sizeof(struct keychain));
41 QOBJ_REG(keychain, keychain);
42 return keychain;
43 }
44
45 static void keychain_free(struct keychain *keychain)
46 {
47 QOBJ_UNREG(keychain);
48 XFREE(MTYPE_KEYCHAIN, keychain);
49 }
50
51 static struct key *key_new(void)
52 {
53 struct key *key = XCALLOC(MTYPE_KEY, sizeof(struct key));
54 QOBJ_REG(key, key);
55 return key;
56 }
57
58 static void key_free(struct key *key)
59 {
60 QOBJ_UNREG(key);
61 XFREE(MTYPE_KEY, key);
62 }
63
64 struct keychain *keychain_lookup(const char *name)
65 {
66 struct listnode *node;
67 struct keychain *keychain;
68
69 if (name == NULL)
70 return NULL;
71
72 for (ALL_LIST_ELEMENTS_RO(keychain_list, node, keychain)) {
73 if (strcmp(keychain->name, name) == 0)
74 return keychain;
75 }
76 return NULL;
77 }
78
79 static int key_cmp_func(void *arg1, void *arg2)
80 {
81 const struct key *k1 = arg1;
82 const struct key *k2 = arg2;
83
84 if (k1->index > k2->index)
85 return 1;
86 if (k1->index < k2->index)
87 return -1;
88 return 0;
89 }
90
91 static void key_delete_func(struct key *key)
92 {
93 if (key->string)
94 free(key->string);
95 key_free(key);
96 }
97
98 static struct keychain *keychain_get(const char *name)
99 {
100 struct keychain *keychain;
101
102 keychain = keychain_lookup(name);
103
104 if (keychain)
105 return keychain;
106
107 keychain = keychain_new();
108 keychain->name = XSTRDUP(MTYPE_KEYCHAIN, name);
109 keychain->key = list_new();
110 keychain->key->cmp = (int (*)(void *, void *))key_cmp_func;
111 keychain->key->del = (void (*)(void *))key_delete_func;
112 listnode_add(keychain_list, keychain);
113
114 return keychain;
115 }
116
117 static void keychain_delete(struct keychain *keychain)
118 {
119 if (keychain->name)
120 XFREE(MTYPE_KEYCHAIN, keychain->name);
121
122 list_delete(&keychain->key);
123 listnode_delete(keychain_list, keychain);
124 keychain_free(keychain);
125 }
126
127 static struct key *key_lookup(const struct keychain *keychain, uint32_t index)
128 {
129 struct listnode *node;
130 struct key *key;
131
132 for (ALL_LIST_ELEMENTS_RO(keychain->key, node, key)) {
133 if (key->index == index)
134 return key;
135 }
136 return NULL;
137 }
138
139 struct key *key_lookup_for_accept(const struct keychain *keychain,
140 uint32_t index)
141 {
142 struct listnode *node;
143 struct key *key;
144 time_t now;
145
146 now = time(NULL);
147
148 for (ALL_LIST_ELEMENTS_RO(keychain->key, node, key)) {
149 if (key->index >= index) {
150 if (key->accept.start == 0)
151 return key;
152
153 if (key->accept.start <= now)
154 if (key->accept.end >= now
155 || key->accept.end == -1)
156 return key;
157 }
158 }
159 return NULL;
160 }
161
162 struct key *key_match_for_accept(const struct keychain *keychain,
163 const char *auth_str)
164 {
165 struct listnode *node;
166 struct key *key;
167 time_t now;
168
169 now = time(NULL);
170
171 for (ALL_LIST_ELEMENTS_RO(keychain->key, node, key)) {
172 if (key->accept.start == 0
173 || (key->accept.start <= now
174 && (key->accept.end >= now || key->accept.end == -1)))
175 if (key->string && (strncmp(key->string, auth_str, 16) == 0))
176 return key;
177 }
178 return NULL;
179 }
180
181 struct key *key_lookup_for_send(const struct keychain *keychain)
182 {
183 struct listnode *node;
184 struct key *key;
185 time_t now;
186
187 now = time(NULL);
188
189 for (ALL_LIST_ELEMENTS_RO(keychain->key, node, key)) {
190 if (key->send.start == 0)
191 return key;
192
193 if (key->send.start <= now)
194 if (key->send.end >= now || key->send.end == -1)
195 return key;
196 }
197 return NULL;
198 }
199
200 static struct key *key_get(const struct keychain *keychain, uint32_t index)
201 {
202 struct key *key;
203
204 key = key_lookup(keychain, index);
205
206 if (key)
207 return key;
208
209 key = key_new();
210 key->index = index;
211 listnode_add_sort(keychain->key, key);
212
213 return key;
214 }
215
216 static void key_delete(struct keychain *keychain, struct key *key)
217 {
218 listnode_delete(keychain->key, key);
219
220 if (key->string)
221 XFREE(MTYPE_KEY, key->string);
222 key_free(key);
223 }
224
225 DEFUN_NOSH (key_chain,
226 key_chain_cmd,
227 "key chain WORD",
228 "Authentication key management\n"
229 "Key-chain management\n"
230 "Key-chain name\n")
231 {
232 int idx_word = 2;
233 struct keychain *keychain;
234
235 keychain = keychain_get(argv[idx_word]->arg);
236 VTY_PUSH_CONTEXT(KEYCHAIN_NODE, keychain);
237
238 return CMD_SUCCESS;
239 }
240
241 DEFUN (no_key_chain,
242 no_key_chain_cmd,
243 "no key chain WORD",
244 NO_STR
245 "Authentication key management\n"
246 "Key-chain management\n"
247 "Key-chain name\n")
248 {
249 int idx_word = 3;
250 struct keychain *keychain;
251
252 keychain = keychain_lookup(argv[idx_word]->arg);
253
254 if (!keychain) {
255 vty_out(vty, "Can't find keychain %s\n", argv[idx_word]->arg);
256 return CMD_WARNING_CONFIG_FAILED;
257 }
258
259 keychain_delete(keychain);
260
261 return CMD_SUCCESS;
262 }
263
264 DEFUN_NOSH (key,
265 key_cmd,
266 "key (0-2147483647)",
267 "Configure a key\n"
268 "Key identifier number\n")
269 {
270 int idx_number = 1;
271 VTY_DECLVAR_CONTEXT(keychain, keychain);
272 struct key *key;
273 uint32_t index;
274
275 index = strtoul(argv[idx_number]->arg, NULL, 10);
276 key = key_get(keychain, index);
277 VTY_PUSH_CONTEXT_SUB(KEYCHAIN_KEY_NODE, key);
278
279 return CMD_SUCCESS;
280 }
281
282 DEFUN (no_key,
283 no_key_cmd,
284 "no key (0-2147483647)",
285 NO_STR
286 "Delete a key\n"
287 "Key identifier number\n")
288 {
289 int idx_number = 2;
290 VTY_DECLVAR_CONTEXT(keychain, keychain);
291 struct key *key;
292 uint32_t index;
293
294 index = strtoul(argv[idx_number]->arg, NULL, 10);
295 key = key_lookup(keychain, index);
296 if (!key) {
297 vty_out(vty, "Can't find key %d\n", index);
298 return CMD_WARNING_CONFIG_FAILED;
299 }
300
301 key_delete(keychain, key);
302
303 vty->node = KEYCHAIN_NODE;
304
305 return CMD_SUCCESS;
306 }
307
308 DEFUN (key_string,
309 key_string_cmd,
310 "key-string LINE",
311 "Set key string\n"
312 "The key\n")
313 {
314 int idx_line = 1;
315 VTY_DECLVAR_CONTEXT_SUB(key, key);
316
317 if (key->string)
318 XFREE(MTYPE_KEY, key->string);
319 key->string = XSTRDUP(MTYPE_KEY, argv[idx_line]->arg);
320
321 return CMD_SUCCESS;
322 }
323
324 DEFUN (no_key_string,
325 no_key_string_cmd,
326 "no key-string [LINE]",
327 NO_STR
328 "Unset key string\n"
329 "The key\n")
330 {
331 VTY_DECLVAR_CONTEXT_SUB(key, key);
332
333 if (key->string) {
334 XFREE(MTYPE_KEY, key->string);
335 key->string = NULL;
336 }
337
338 return CMD_SUCCESS;
339 }
340
341 /* Convert HH:MM:SS MON DAY YEAR to time_t value. -1 is returned when
342 given string is malformed. */
343 static time_t key_str2time(const char *time_str, const char *day_str,
344 const char *month_str, const char *year_str)
345 {
346 int i = 0;
347 char *colon;
348 struct tm tm;
349 time_t time;
350 unsigned int sec, min, hour;
351 unsigned int day, month, year;
352
353 const char *month_name[] = {
354 "January", "February", "March", "April", "May",
355 "June", "July", "August", "September", "October",
356 "November", "December", NULL};
357
358 #define _GET_LONG_RANGE(V, STR, MMCOND) \
359 { \
360 unsigned long tmpl; \
361 char *endptr = NULL; \
362 tmpl = strtoul((STR), &endptr, 10); \
363 if (*endptr != '\0' || tmpl == ULONG_MAX) \
364 return -1; \
365 if (MMCOND) \
366 return -1; \
367 (V) = tmpl; \
368 }
369 #define GET_LONG_RANGE(V, STR, MIN, MAX) \
370 _GET_LONG_RANGE(V, STR, tmpl<(MIN) || tmpl>(MAX))
371 #define GET_LONG_RANGE0(V, STR, MAX) _GET_LONG_RANGE(V, STR, tmpl > (MAX))
372
373 /* Check hour field of time_str. */
374 colon = strchr(time_str, ':');
375 if (colon == NULL)
376 return -1;
377 *colon = '\0';
378
379 /* Hour must be between 0 and 23. */
380 GET_LONG_RANGE0(hour, time_str, 23);
381
382 /* Check min field of time_str. */
383 time_str = colon + 1;
384 colon = strchr(time_str, ':');
385 if (*time_str == '\0' || colon == NULL)
386 return -1;
387 *colon = '\0';
388
389 /* Min must be between 0 and 59. */
390 GET_LONG_RANGE0(min, time_str, 59);
391
392 /* Check sec field of time_str. */
393 time_str = colon + 1;
394 if (*time_str == '\0')
395 return -1;
396
397 /* Sec must be between 0 and 59. */
398 GET_LONG_RANGE0(sec, time_str, 59);
399
400 /* Check day_str. Day must be <1-31>. */
401 GET_LONG_RANGE(day, day_str, 1, 31);
402
403 /* Check month_str. Month must match month_name. */
404 month = 0;
405 if (strlen(month_str) >= 3)
406 for (i = 0; month_name[i]; i++)
407 if (strncmp(month_str, month_name[i], strlen(month_str))
408 == 0) {
409 month = i;
410 break;
411 }
412 if (!month_name[i])
413 return -1;
414
415 /* Check year_str. Year must be <1993-2035>. */
416 GET_LONG_RANGE(year, year_str, 1993, 2035);
417
418 memset(&tm, 0, sizeof(struct tm));
419 tm.tm_sec = sec;
420 tm.tm_min = min;
421 tm.tm_hour = hour;
422 tm.tm_mon = month;
423 tm.tm_mday = day;
424 tm.tm_year = year - 1900;
425
426 time = mktime(&tm);
427
428 return time;
429 #undef GET_LONG_RANGE
430 }
431
432 static int key_lifetime_set(struct vty *vty, struct key_range *krange,
433 const char *stime_str, const char *sday_str,
434 const char *smonth_str, const char *syear_str,
435 const char *etime_str, const char *eday_str,
436 const char *emonth_str, const char *eyear_str)
437 {
438 time_t time_start;
439 time_t time_end;
440
441 time_start = key_str2time(stime_str, sday_str, smonth_str, syear_str);
442 if (time_start < 0) {
443 vty_out(vty, "Malformed time value\n");
444 return CMD_WARNING_CONFIG_FAILED;
445 }
446 time_end = key_str2time(etime_str, eday_str, emonth_str, eyear_str);
447
448 if (time_end < 0) {
449 vty_out(vty, "Malformed time value\n");
450 return CMD_WARNING_CONFIG_FAILED;
451 }
452
453 if (time_end <= time_start) {
454 vty_out(vty, "Expire time is not later than start time\n");
455 return CMD_WARNING_CONFIG_FAILED;
456 }
457
458 krange->start = time_start;
459 krange->end = time_end;
460
461 return CMD_SUCCESS;
462 }
463
464 static int key_lifetime_duration_set(struct vty *vty, struct key_range *krange,
465 const char *stime_str,
466 const char *sday_str,
467 const char *smonth_str,
468 const char *syear_str,
469 const char *duration_str)
470 {
471 time_t time_start;
472 uint32_t duration;
473
474 time_start = key_str2time(stime_str, sday_str, smonth_str, syear_str);
475 if (time_start < 0) {
476 vty_out(vty, "Malformed time value\n");
477 return CMD_WARNING_CONFIG_FAILED;
478 }
479 krange->start = time_start;
480
481 duration = strtoul(duration_str, NULL, 10);
482 krange->duration = 1;
483 krange->end = time_start + duration;
484
485 return CMD_SUCCESS;
486 }
487
488 static int key_lifetime_infinite_set(struct vty *vty, struct key_range *krange,
489 const char *stime_str,
490 const char *sday_str,
491 const char *smonth_str,
492 const char *syear_str)
493 {
494 time_t time_start;
495
496 time_start = key_str2time(stime_str, sday_str, smonth_str, syear_str);
497 if (time_start < 0) {
498 vty_out(vty, "Malformed time value\n");
499 return CMD_WARNING_CONFIG_FAILED;
500 }
501 krange->start = time_start;
502
503 krange->end = -1;
504
505 return CMD_SUCCESS;
506 }
507
508 DEFUN (accept_lifetime_day_month_day_month,
509 accept_lifetime_day_month_day_month_cmd,
510 "accept-lifetime HH:MM:SS (1-31) MONTH (1993-2035) HH:MM:SS (1-31) MONTH (1993-2035)",
511 "Set accept lifetime of the key\n"
512 "Time to start\n"
513 "Day of th month to start\n"
514 "Month of the year to start\n"
515 "Year to start\n"
516 "Time to expire\n"
517 "Day of th month to expire\n"
518 "Month of the year to expire\n"
519 "Year to expire\n")
520 {
521 int idx_hhmmss = 1;
522 int idx_number = 2;
523 int idx_month = 3;
524 int idx_number_2 = 4;
525 int idx_hhmmss_2 = 5;
526 int idx_number_3 = 6;
527 int idx_month_2 = 7;
528 int idx_number_4 = 8;
529 VTY_DECLVAR_CONTEXT_SUB(key, key);
530
531 return key_lifetime_set(
532 vty, &key->accept, argv[idx_hhmmss]->arg, argv[idx_number]->arg,
533 argv[idx_month]->arg, argv[idx_number_2]->arg,
534 argv[idx_hhmmss_2]->arg, argv[idx_number_3]->arg,
535 argv[idx_month_2]->arg, argv[idx_number_4]->arg);
536 }
537
538 DEFUN (accept_lifetime_day_month_month_day,
539 accept_lifetime_day_month_month_day_cmd,
540 "accept-lifetime HH:MM:SS (1-31) MONTH (1993-2035) HH:MM:SS MONTH (1-31) (1993-2035)",
541 "Set accept lifetime of the key\n"
542 "Time to start\n"
543 "Day of th month to start\n"
544 "Month of the year to start\n"
545 "Year to start\n"
546 "Time to expire\n"
547 "Month of the year to expire\n"
548 "Day of th month to expire\n"
549 "Year to expire\n")
550 {
551 int idx_hhmmss = 1;
552 int idx_number = 2;
553 int idx_month = 3;
554 int idx_number_2 = 4;
555 int idx_hhmmss_2 = 5;
556 int idx_month_2 = 6;
557 int idx_number_3 = 7;
558 int idx_number_4 = 8;
559 VTY_DECLVAR_CONTEXT_SUB(key, key);
560
561 return key_lifetime_set(
562 vty, &key->accept, argv[idx_hhmmss]->arg, argv[idx_number]->arg,
563 argv[idx_month]->arg, argv[idx_number_2]->arg,
564 argv[idx_hhmmss_2]->arg, argv[idx_number_3]->arg,
565 argv[idx_month_2]->arg, argv[idx_number_4]->arg);
566 }
567
568 DEFUN (accept_lifetime_month_day_day_month,
569 accept_lifetime_month_day_day_month_cmd,
570 "accept-lifetime HH:MM:SS MONTH (1-31) (1993-2035) HH:MM:SS (1-31) MONTH (1993-2035)",
571 "Set accept lifetime of the key\n"
572 "Time to start\n"
573 "Month of the year to start\n"
574 "Day of th month to start\n"
575 "Year to start\n"
576 "Time to expire\n"
577 "Day of th month to expire\n"
578 "Month of the year to expire\n"
579 "Year to expire\n")
580 {
581 int idx_hhmmss = 1;
582 int idx_month = 2;
583 int idx_number = 3;
584 int idx_number_2 = 4;
585 int idx_hhmmss_2 = 5;
586 int idx_number_3 = 6;
587 int idx_month_2 = 7;
588 int idx_number_4 = 8;
589 VTY_DECLVAR_CONTEXT_SUB(key, key);
590
591 return key_lifetime_set(
592 vty, &key->accept, argv[idx_hhmmss]->arg, argv[idx_number]->arg,
593 argv[idx_month]->arg, argv[idx_number_2]->arg,
594 argv[idx_hhmmss_2]->arg, argv[idx_number_3]->arg,
595 argv[idx_month_2]->arg, argv[idx_number_4]->arg);
596 }
597
598 DEFUN (accept_lifetime_month_day_month_day,
599 accept_lifetime_month_day_month_day_cmd,
600 "accept-lifetime HH:MM:SS MONTH (1-31) (1993-2035) HH:MM:SS MONTH (1-31) (1993-2035)",
601 "Set accept lifetime of the key\n"
602 "Time to start\n"
603 "Month of the year to start\n"
604 "Day of th month to start\n"
605 "Year to start\n"
606 "Time to expire\n"
607 "Month of the year to expire\n"
608 "Day of th month to expire\n"
609 "Year to expire\n")
610 {
611 int idx_hhmmss = 1;
612 int idx_month = 2;
613 int idx_number = 3;
614 int idx_number_2 = 4;
615 int idx_hhmmss_2 = 5;
616 int idx_month_2 = 6;
617 int idx_number_3 = 7;
618 int idx_number_4 = 8;
619 VTY_DECLVAR_CONTEXT_SUB(key, key);
620
621 return key_lifetime_set(
622 vty, &key->accept, argv[idx_hhmmss]->arg, argv[idx_number]->arg,
623 argv[idx_month]->arg, argv[idx_number_2]->arg,
624 argv[idx_hhmmss_2]->arg, argv[idx_number_3]->arg,
625 argv[idx_month_2]->arg, argv[idx_number_4]->arg);
626 }
627
628 DEFUN (accept_lifetime_infinite_day_month,
629 accept_lifetime_infinite_day_month_cmd,
630 "accept-lifetime HH:MM:SS (1-31) MONTH (1993-2035) infinite",
631 "Set accept lifetime of the key\n"
632 "Time to start\n"
633 "Day of th month to start\n"
634 "Month of the year to start\n"
635 "Year to start\n"
636 "Never expires\n")
637 {
638 int idx_hhmmss = 1;
639 int idx_number = 2;
640 int idx_month = 3;
641 int idx_number_2 = 4;
642 VTY_DECLVAR_CONTEXT_SUB(key, key);
643
644 return key_lifetime_infinite_set(
645 vty, &key->accept, argv[idx_hhmmss]->arg, argv[idx_number]->arg,
646 argv[idx_month]->arg, argv[idx_number_2]->arg);
647 }
648
649 DEFUN (accept_lifetime_infinite_month_day,
650 accept_lifetime_infinite_month_day_cmd,
651 "accept-lifetime HH:MM:SS MONTH (1-31) (1993-2035) infinite",
652 "Set accept lifetime of the key\n"
653 "Time to start\n"
654 "Month of the year to start\n"
655 "Day of th month to start\n"
656 "Year to start\n"
657 "Never expires\n")
658 {
659 int idx_hhmmss = 1;
660 int idx_month = 2;
661 int idx_number = 3;
662 int idx_number_2 = 4;
663 VTY_DECLVAR_CONTEXT_SUB(key, key);
664
665 return key_lifetime_infinite_set(
666 vty, &key->accept, argv[idx_hhmmss]->arg, argv[idx_number]->arg,
667 argv[idx_month]->arg, argv[idx_number_2]->arg);
668 }
669
670 DEFUN (accept_lifetime_duration_day_month,
671 accept_lifetime_duration_day_month_cmd,
672 "accept-lifetime HH:MM:SS (1-31) MONTH (1993-2035) duration (1-2147483646)",
673 "Set accept lifetime of the key\n"
674 "Time to start\n"
675 "Day of th month to start\n"
676 "Month of the year to start\n"
677 "Year to start\n"
678 "Duration of the key\n"
679 "Duration seconds\n")
680 {
681 int idx_hhmmss = 1;
682 int idx_number = 2;
683 int idx_month = 3;
684 int idx_number_2 = 4;
685 int idx_number_3 = 6;
686 VTY_DECLVAR_CONTEXT_SUB(key, key);
687
688 return key_lifetime_duration_set(
689 vty, &key->accept, argv[idx_hhmmss]->arg, argv[idx_number]->arg,
690 argv[idx_month]->arg, argv[idx_number_2]->arg,
691 argv[idx_number_3]->arg);
692 }
693
694 DEFUN (accept_lifetime_duration_month_day,
695 accept_lifetime_duration_month_day_cmd,
696 "accept-lifetime HH:MM:SS MONTH (1-31) (1993-2035) duration (1-2147483646)",
697 "Set accept lifetime of the key\n"
698 "Time to start\n"
699 "Month of the year to start\n"
700 "Day of th month to start\n"
701 "Year to start\n"
702 "Duration of the key\n"
703 "Duration seconds\n")
704 {
705 int idx_hhmmss = 1;
706 int idx_month = 2;
707 int idx_number = 3;
708 int idx_number_2 = 4;
709 int idx_number_3 = 6;
710 VTY_DECLVAR_CONTEXT_SUB(key, key);
711
712 return key_lifetime_duration_set(
713 vty, &key->accept, argv[idx_hhmmss]->arg, argv[idx_number]->arg,
714 argv[idx_month]->arg, argv[idx_number_2]->arg,
715 argv[idx_number_3]->arg);
716 }
717
718 DEFUN (no_accept_lifetime,
719 no_accept_lifetime_cmd,
720 "no accept-lifetime",
721 NO_STR
722 "Unset accept-lifetime\n")
723 {
724 VTY_DECLVAR_CONTEXT_SUB(key, key);
725
726 if (key->accept.start)
727 key->accept.start = 0;
728 if (key->accept.end)
729 key->accept.end = 0;
730 if (key->accept.duration)
731 key->accept.duration = 0;
732
733 return CMD_SUCCESS;
734 }
735
736 DEFUN (send_lifetime_day_month_day_month,
737 send_lifetime_day_month_day_month_cmd,
738 "send-lifetime HH:MM:SS (1-31) MONTH (1993-2035) HH:MM:SS (1-31) MONTH (1993-2035)",
739 "Set send lifetime of the key\n"
740 "Time to start\n"
741 "Day of th month to start\n"
742 "Month of the year to start\n"
743 "Year to start\n"
744 "Time to expire\n"
745 "Day of th month to expire\n"
746 "Month of the year to expire\n"
747 "Year to expire\n")
748 {
749 int idx_hhmmss = 1;
750 int idx_number = 2;
751 int idx_month = 3;
752 int idx_number_2 = 4;
753 int idx_hhmmss_2 = 5;
754 int idx_number_3 = 6;
755 int idx_month_2 = 7;
756 int idx_number_4 = 8;
757 VTY_DECLVAR_CONTEXT_SUB(key, key);
758
759 return key_lifetime_set(
760 vty, &key->send, argv[idx_hhmmss]->arg, argv[idx_number]->arg,
761 argv[idx_month]->arg, argv[idx_number_2]->arg,
762 argv[idx_hhmmss_2]->arg, argv[idx_number_3]->arg,
763 argv[idx_month_2]->arg, argv[idx_number_4]->arg);
764 }
765
766 DEFUN (send_lifetime_day_month_month_day,
767 send_lifetime_day_month_month_day_cmd,
768 "send-lifetime HH:MM:SS (1-31) MONTH (1993-2035) HH:MM:SS MONTH (1-31) (1993-2035)",
769 "Set send lifetime of the key\n"
770 "Time to start\n"
771 "Day of th month to start\n"
772 "Month of the year to start\n"
773 "Year to start\n"
774 "Time to expire\n"
775 "Month of the year to expire\n"
776 "Day of th month to expire\n"
777 "Year to expire\n")
778 {
779 int idx_hhmmss = 1;
780 int idx_number = 2;
781 int idx_month = 3;
782 int idx_number_2 = 4;
783 int idx_hhmmss_2 = 5;
784 int idx_month_2 = 6;
785 int idx_number_3 = 7;
786 int idx_number_4 = 8;
787 VTY_DECLVAR_CONTEXT_SUB(key, key);
788
789 return key_lifetime_set(
790 vty, &key->send, argv[idx_hhmmss]->arg, argv[idx_number]->arg,
791 argv[idx_month]->arg, argv[idx_number_2]->arg,
792 argv[idx_hhmmss_2]->arg, argv[idx_number_3]->arg,
793 argv[idx_month_2]->arg, argv[idx_number_4]->arg);
794 }
795
796 DEFUN (send_lifetime_month_day_day_month,
797 send_lifetime_month_day_day_month_cmd,
798 "send-lifetime HH:MM:SS MONTH (1-31) (1993-2035) HH:MM:SS (1-31) MONTH (1993-2035)",
799 "Set send lifetime of the key\n"
800 "Time to start\n"
801 "Month of the year to start\n"
802 "Day of th month to start\n"
803 "Year to start\n"
804 "Time to expire\n"
805 "Day of th month to expire\n"
806 "Month of the year to expire\n"
807 "Year to expire\n")
808 {
809 int idx_hhmmss = 1;
810 int idx_month = 2;
811 int idx_number = 3;
812 int idx_number_2 = 4;
813 int idx_hhmmss_2 = 5;
814 int idx_number_3 = 6;
815 int idx_month_2 = 7;
816 int idx_number_4 = 8;
817 VTY_DECLVAR_CONTEXT_SUB(key, key);
818
819 return key_lifetime_set(
820 vty, &key->send, argv[idx_hhmmss]->arg, argv[idx_number]->arg,
821 argv[idx_month]->arg, argv[idx_number_2]->arg,
822 argv[idx_hhmmss_2]->arg, argv[idx_number_3]->arg,
823 argv[idx_month_2]->arg, argv[idx_number_4]->arg);
824 }
825
826 DEFUN (send_lifetime_month_day_month_day,
827 send_lifetime_month_day_month_day_cmd,
828 "send-lifetime HH:MM:SS MONTH (1-31) (1993-2035) HH:MM:SS MONTH (1-31) (1993-2035)",
829 "Set send lifetime of the key\n"
830 "Time to start\n"
831 "Month of the year to start\n"
832 "Day of th month to start\n"
833 "Year to start\n"
834 "Time to expire\n"
835 "Month of the year to expire\n"
836 "Day of th month to expire\n"
837 "Year to expire\n")
838 {
839 int idx_hhmmss = 1;
840 int idx_month = 2;
841 int idx_number = 3;
842 int idx_number_2 = 4;
843 int idx_hhmmss_2 = 5;
844 int idx_month_2 = 6;
845 int idx_number_3 = 7;
846 int idx_number_4 = 8;
847 VTY_DECLVAR_CONTEXT_SUB(key, key);
848
849 return key_lifetime_set(
850 vty, &key->send, argv[idx_hhmmss]->arg, argv[idx_number]->arg,
851 argv[idx_month]->arg, argv[idx_number_2]->arg,
852 argv[idx_hhmmss_2]->arg, argv[idx_number_3]->arg,
853 argv[idx_month_2]->arg, argv[idx_number_4]->arg);
854 }
855
856 DEFUN (send_lifetime_infinite_day_month,
857 send_lifetime_infinite_day_month_cmd,
858 "send-lifetime HH:MM:SS (1-31) MONTH (1993-2035) infinite",
859 "Set send lifetime of the key\n"
860 "Time to start\n"
861 "Day of th month to start\n"
862 "Month of the year to start\n"
863 "Year to start\n"
864 "Never expires\n")
865 {
866 int idx_hhmmss = 1;
867 int idx_number = 2;
868 int idx_month = 3;
869 int idx_number_2 = 4;
870 VTY_DECLVAR_CONTEXT_SUB(key, key);
871
872 return key_lifetime_infinite_set(
873 vty, &key->send, argv[idx_hhmmss]->arg, argv[idx_number]->arg,
874 argv[idx_month]->arg, argv[idx_number_2]->arg);
875 }
876
877 DEFUN (send_lifetime_infinite_month_day,
878 send_lifetime_infinite_month_day_cmd,
879 "send-lifetime HH:MM:SS MONTH (1-31) (1993-2035) infinite",
880 "Set send lifetime of the key\n"
881 "Time to start\n"
882 "Month of the year to start\n"
883 "Day of th month to start\n"
884 "Year to start\n"
885 "Never expires\n")
886 {
887 int idx_hhmmss = 1;
888 int idx_month = 2;
889 int idx_number = 3;
890 int idx_number_2 = 4;
891 VTY_DECLVAR_CONTEXT_SUB(key, key);
892
893 return key_lifetime_infinite_set(
894 vty, &key->send, argv[idx_hhmmss]->arg, argv[idx_number]->arg,
895 argv[idx_month]->arg, argv[idx_number_2]->arg);
896 }
897
898 DEFUN (send_lifetime_duration_day_month,
899 send_lifetime_duration_day_month_cmd,
900 "send-lifetime HH:MM:SS (1-31) MONTH (1993-2035) duration (1-2147483646)",
901 "Set send lifetime of the key\n"
902 "Time to start\n"
903 "Day of th month to start\n"
904 "Month of the year to start\n"
905 "Year to start\n"
906 "Duration of the key\n"
907 "Duration seconds\n")
908 {
909 int idx_hhmmss = 1;
910 int idx_number = 2;
911 int idx_month = 3;
912 int idx_number_2 = 4;
913 int idx_number_3 = 6;
914 VTY_DECLVAR_CONTEXT_SUB(key, key);
915
916 return key_lifetime_duration_set(
917 vty, &key->send, argv[idx_hhmmss]->arg, argv[idx_number]->arg,
918 argv[idx_month]->arg, argv[idx_number_2]->arg,
919 argv[idx_number_3]->arg);
920 }
921
922 DEFUN (send_lifetime_duration_month_day,
923 send_lifetime_duration_month_day_cmd,
924 "send-lifetime HH:MM:SS MONTH (1-31) (1993-2035) duration (1-2147483646)",
925 "Set send lifetime of the key\n"
926 "Time to start\n"
927 "Month of the year to start\n"
928 "Day of th month to start\n"
929 "Year to start\n"
930 "Duration of the key\n"
931 "Duration seconds\n")
932 {
933 int idx_hhmmss = 1;
934 int idx_month = 2;
935 int idx_number = 3;
936 int idx_number_2 = 4;
937 int idx_number_3 = 6;
938 VTY_DECLVAR_CONTEXT_SUB(key, key);
939
940 return key_lifetime_duration_set(
941 vty, &key->send, argv[idx_hhmmss]->arg, argv[idx_number]->arg,
942 argv[idx_month]->arg, argv[idx_number_2]->arg,
943 argv[idx_number_3]->arg);
944 }
945
946 DEFUN (no_send_lifetime,
947 no_send_lifetime_cmd,
948 "no send-lifetime",
949 NO_STR
950 "Unset send-lifetime\n")
951 {
952 VTY_DECLVAR_CONTEXT_SUB(key, key);
953
954 if (key->send.start)
955 key->send.start = 0;
956 if (key->send.end)
957 key->send.end = 0;
958 if (key->send.duration)
959 key->send.duration = 0;
960
961 return CMD_SUCCESS;
962 }
963
964 static struct cmd_node keychain_node = {KEYCHAIN_NODE, "%s(config-keychain)# ",
965 1};
966
967 static struct cmd_node keychain_key_node = {KEYCHAIN_KEY_NODE,
968 "%s(config-keychain-key)# ", 1};
969
970 static int keychain_strftime(char *buf, int bufsiz, time_t *time)
971 {
972 struct tm *tm;
973 size_t len;
974
975 tm = localtime(time);
976
977 len = strftime(buf, bufsiz, "%T %b %d %Y", tm);
978
979 return len;
980 }
981
982 static int keychain_config_write(struct vty *vty)
983 {
984 struct keychain *keychain;
985 struct key *key;
986 struct listnode *node;
987 struct listnode *knode;
988 char buf[BUFSIZ];
989
990 for (ALL_LIST_ELEMENTS_RO(keychain_list, node, keychain)) {
991 vty_out(vty, "key chain %s\n", keychain->name);
992
993 for (ALL_LIST_ELEMENTS_RO(keychain->key, knode, key)) {
994 vty_out(vty, " key %d\n", key->index);
995
996 if (key->string)
997 vty_out(vty, " key-string %s\n", key->string);
998
999 if (key->accept.start) {
1000 keychain_strftime(buf, BUFSIZ,
1001 &key->accept.start);
1002 vty_out(vty, " accept-lifetime %s", buf);
1003
1004 if (key->accept.end == -1)
1005 vty_out(vty, " infinite");
1006 else if (key->accept.duration)
1007 vty_out(vty, " duration %ld",
1008 (long)(key->accept.end
1009 - key->accept.start));
1010 else {
1011 keychain_strftime(buf, BUFSIZ,
1012 &key->accept.end);
1013 vty_out(vty, " %s", buf);
1014 }
1015 vty_out(vty, "\n");
1016 }
1017
1018 if (key->send.start) {
1019 keychain_strftime(buf, BUFSIZ,
1020 &key->send.start);
1021 vty_out(vty, " send-lifetime %s", buf);
1022
1023 if (key->send.end == -1)
1024 vty_out(vty, " infinite");
1025 else if (key->send.duration)
1026 vty_out(vty, " duration %ld",
1027 (long)(key->send.end
1028 - key->send.start));
1029 else {
1030 keychain_strftime(buf, BUFSIZ,
1031 &key->send.end);
1032 vty_out(vty, " %s", buf);
1033 }
1034 vty_out(vty, "\n");
1035 }
1036 }
1037 vty_out(vty, "!\n");
1038 }
1039
1040 return 0;
1041 }
1042
1043 void keychain_init()
1044 {
1045 keychain_list = list_new();
1046
1047 install_node(&keychain_node, keychain_config_write);
1048 install_node(&keychain_key_node, NULL);
1049
1050 install_default(KEYCHAIN_NODE);
1051 install_default(KEYCHAIN_KEY_NODE);
1052
1053 install_element(CONFIG_NODE, &key_chain_cmd);
1054 install_element(CONFIG_NODE, &no_key_chain_cmd);
1055 install_element(KEYCHAIN_NODE, &key_cmd);
1056 install_element(KEYCHAIN_NODE, &no_key_cmd);
1057
1058 install_element(KEYCHAIN_NODE, &key_chain_cmd);
1059 install_element(KEYCHAIN_NODE, &no_key_chain_cmd);
1060
1061 install_element(KEYCHAIN_KEY_NODE, &key_string_cmd);
1062 install_element(KEYCHAIN_KEY_NODE, &no_key_string_cmd);
1063
1064 install_element(KEYCHAIN_KEY_NODE, &key_chain_cmd);
1065 install_element(KEYCHAIN_KEY_NODE, &no_key_chain_cmd);
1066
1067 install_element(KEYCHAIN_KEY_NODE, &key_cmd);
1068 install_element(KEYCHAIN_KEY_NODE, &no_key_cmd);
1069
1070 install_element(KEYCHAIN_KEY_NODE,
1071 &accept_lifetime_day_month_day_month_cmd);
1072 install_element(KEYCHAIN_KEY_NODE,
1073 &accept_lifetime_day_month_month_day_cmd);
1074 install_element(KEYCHAIN_KEY_NODE,
1075 &accept_lifetime_month_day_day_month_cmd);
1076 install_element(KEYCHAIN_KEY_NODE,
1077 &accept_lifetime_month_day_month_day_cmd);
1078 install_element(KEYCHAIN_KEY_NODE,
1079 &accept_lifetime_infinite_day_month_cmd);
1080 install_element(KEYCHAIN_KEY_NODE,
1081 &accept_lifetime_infinite_month_day_cmd);
1082 install_element(KEYCHAIN_KEY_NODE,
1083 &accept_lifetime_duration_day_month_cmd);
1084 install_element(KEYCHAIN_KEY_NODE,
1085 &accept_lifetime_duration_month_day_cmd);
1086 install_element(KEYCHAIN_KEY_NODE, &no_accept_lifetime_cmd);
1087
1088 install_element(KEYCHAIN_KEY_NODE,
1089 &send_lifetime_day_month_day_month_cmd);
1090 install_element(KEYCHAIN_KEY_NODE,
1091 &send_lifetime_day_month_month_day_cmd);
1092 install_element(KEYCHAIN_KEY_NODE,
1093 &send_lifetime_month_day_day_month_cmd);
1094 install_element(KEYCHAIN_KEY_NODE,
1095 &send_lifetime_month_day_month_day_cmd);
1096 install_element(KEYCHAIN_KEY_NODE,
1097 &send_lifetime_infinite_day_month_cmd);
1098 install_element(KEYCHAIN_KEY_NODE,
1099 &send_lifetime_infinite_month_day_cmd);
1100 install_element(KEYCHAIN_KEY_NODE,
1101 &send_lifetime_duration_day_month_cmd);
1102 install_element(KEYCHAIN_KEY_NODE,
1103 &send_lifetime_duration_month_day_cmd);
1104 install_element(KEYCHAIN_KEY_NODE, &no_send_lifetime_cmd);
1105 }