[lib] remove autogenerated file, update .cvsignore
[jleu-quagga.git] / lib / keychain.c
blob10928b11d67a61767a2f64addc0a1792d11c8c2c
1 /* key-chain for authentication.
2 Copyright (C) 2000 Kunihiro Ishiguro
4 This file is part of GNU Zebra.
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.
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.
16 You should have received a copy of the GNU General Public License
17 along with GNU Zebra; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
21 #include <zebra.h>
23 #include "command.h"
24 #include "memory.h"
25 #include "linklist.h"
26 #include "keychain.h"
28 /* Master list of key chain. */
29 struct list *keychain_list;
31 static struct keychain *
32 keychain_new (void)
34 struct keychain *new;
35 new = XMALLOC (MTYPE_KEYCHAIN, sizeof (struct keychain));
36 memset (new, 0, sizeof (struct keychain));
37 return new;
40 static void
41 keychain_free (struct keychain *keychain)
43 XFREE (MTYPE_KEYCHAIN, keychain);
46 static struct key *
47 key_new (void)
49 struct key *new;
50 new = XMALLOC (MTYPE_KEY, sizeof (struct key));
51 memset (new, 0, sizeof (struct key));
52 return new;
55 static void
56 key_free (struct key *key)
58 XFREE (MTYPE_KEY, key);
61 struct keychain *
62 keychain_lookup (const char *name)
64 struct listnode *node;
65 struct keychain *keychain;
67 if (name == NULL)
68 return NULL;
70 for (ALL_LIST_ELEMENTS_RO (keychain_list, node, keychain))
72 if (strcmp (keychain->name, name) == 0)
73 return keychain;
75 return NULL;
78 static int
79 key_cmp_func (void *arg1, void *arg2)
81 const struct key *k1 = arg1;
82 const struct key *k2 = arg2;
84 if (k1->index > k2->index)
85 return 1;
86 if (k1->index < k2->index)
87 return -1;
88 return 0;
91 static void
92 key_delete_func (struct key *key)
94 if (key->string)
95 free (key->string);
96 key_free (key);
99 static struct keychain *
100 keychain_get (const char *name)
102 struct keychain *keychain;
104 keychain = keychain_lookup (name);
106 if (keychain)
107 return keychain;
109 keychain = keychain_new ();
110 keychain->name = strdup (name);
111 keychain->key = list_new ();
112 keychain->key->cmp = (int (*)(void *, void *)) key_cmp_func;
113 keychain->key->del = (void (*)(void *)) key_delete_func;
114 listnode_add (keychain_list, keychain);
116 return keychain;
119 static void
120 keychain_delete (struct keychain *keychain)
122 if (keychain->name)
123 free (keychain->name);
125 list_delete (keychain->key);
126 listnode_delete (keychain_list, keychain);
127 keychain_free (keychain);
130 static struct key *
131 key_lookup (const struct keychain *keychain, u_int32_t index)
133 struct listnode *node;
134 struct key *key;
136 for (ALL_LIST_ELEMENTS_RO (keychain->key, node, key))
138 if (key->index == index)
139 return key;
141 return NULL;
144 struct key *
145 key_lookup_for_accept (const struct keychain *keychain, u_int32_t index)
147 struct listnode *node;
148 struct key *key;
149 time_t now;
151 now = time (NULL);
153 for (ALL_LIST_ELEMENTS_RO (keychain->key, node, key))
155 if (key->index >= index)
157 if (key->accept.start == 0)
158 return key;
160 if (key->accept.start <= now)
161 if (key->accept.end >= now || key->accept.end == -1)
162 return key;
165 return NULL;
168 struct key *
169 key_match_for_accept (const struct keychain *keychain, const char *auth_str)
171 struct listnode *node;
172 struct key *key;
173 time_t now;
175 now = time (NULL);
177 for (ALL_LIST_ELEMENTS_RO (keychain->key, node, key))
179 if (key->accept.start == 0 ||
180 (key->accept.start <= now &&
181 (key->accept.end >= now || key->accept.end == -1)))
182 if (strncmp (key->string, auth_str, 16) == 0)
183 return key;
185 return NULL;
188 struct key *
189 key_lookup_for_send (const struct keychain *keychain)
191 struct listnode *node;
192 struct key *key;
193 time_t now;
195 now = time (NULL);
197 for (ALL_LIST_ELEMENTS_RO (keychain->key, node, key))
199 if (key->send.start == 0)
200 return key;
202 if (key->send.start <= now)
203 if (key->send.end >= now || key->send.end == -1)
204 return key;
206 return NULL;
209 static struct key *
210 key_get (const struct keychain *keychain, u_int32_t index)
212 struct key *key;
214 key = key_lookup (keychain, index);
216 if (key)
217 return key;
219 key = key_new ();
220 key->index = index;
221 listnode_add_sort (keychain->key, key);
223 return key;
226 static void
227 key_delete (struct keychain *keychain, struct key *key)
229 listnode_delete (keychain->key, key);
231 if (key->string)
232 free (key->string);
233 key_free (key);
236 DEFUN (key_chain,
237 key_chain_cmd,
238 "key chain WORD",
239 "Authentication key management\n"
240 "Key-chain management\n"
241 "Key-chain name\n")
243 struct keychain *keychain;
245 keychain = keychain_get (argv[0]);
246 vty->index = keychain;
247 vty->node = KEYCHAIN_NODE;
249 return CMD_SUCCESS;
252 DEFUN (no_key_chain,
253 no_key_chain_cmd,
254 "no key chain WORD",
255 NO_STR
256 "Authentication key management\n"
257 "Key-chain management\n"
258 "Key-chain name\n")
260 struct keychain *keychain;
262 keychain = keychain_lookup (argv[0]);
264 if (! keychain)
266 vty_out (vty, "Can't find keychain %s%s", argv[0], VTY_NEWLINE);
267 return CMD_WARNING;
270 keychain_delete (keychain);
272 return CMD_SUCCESS;
275 DEFUN (key,
276 key_cmd,
277 "key <0-2147483647>",
278 "Configure a key\n"
279 "Key identifier number\n")
281 struct keychain *keychain;
282 struct key *key;
283 u_int32_t index;
285 keychain = vty->index;
287 VTY_GET_INTEGER ("key identifier", index, argv[0]);
288 key = key_get (keychain, index);
289 vty->index_sub = key;
290 vty->node = KEYCHAIN_KEY_NODE;
292 return CMD_SUCCESS;
295 DEFUN (no_key,
296 no_key_cmd,
297 "no key <0-2147483647>",
298 NO_STR
299 "Delete a key\n"
300 "Key identifier number\n")
302 struct keychain *keychain;
303 struct key *key;
304 u_int32_t index;
306 keychain = vty->index;
308 VTY_GET_INTEGER ("key identifier", index, argv[0]);
309 key = key_lookup (keychain, index);
310 if (! key)
312 vty_out (vty, "Can't find key %d%s", index, VTY_NEWLINE);
313 return CMD_WARNING;
316 key_delete (keychain, key);
318 vty->node = KEYCHAIN_NODE;
320 return CMD_SUCCESS;
323 DEFUN (key_string,
324 key_string_cmd,
325 "key-string LINE",
326 "Set key string\n"
327 "The key\n")
329 struct key *key;
331 key = vty->index_sub;
333 if (key->string)
334 free (key->string);
335 key->string = strdup (argv[0]);
337 return CMD_SUCCESS;
340 DEFUN (no_key_string,
341 no_key_string_cmd,
342 "no key-string [LINE]",
343 NO_STR
344 "Unset key string\n"
345 "The key\n")
347 struct key *key;
349 key = vty->index_sub;
351 if (key->string)
353 free (key->string);
354 key->string = NULL;
357 return CMD_SUCCESS;
360 /* Convert HH:MM:SS MON DAY YEAR to time_t value. -1 is returned when
361 given string is malformed. */
362 static time_t
363 key_str2time (const char *time_str, const char *day_str, const char *month_str,
364 const char *year_str)
366 int i = 0;
367 char *colon;
368 struct tm tm;
369 time_t time;
370 unsigned int sec, min, hour;
371 unsigned int day, month, year;
373 const char *month_name[] =
375 "January",
376 "February",
377 "March",
378 "April",
379 "May",
380 "June",
381 "July",
382 "August",
383 "September",
384 "October",
385 "November",
386 "December",
387 NULL
390 #define GET_LONG_RANGE(V,STR,MIN,MAX) \
392 unsigned long tmpl; \
393 char *endptr = NULL; \
394 tmpl = strtoul ((STR), &endptr, 10); \
395 if (*endptr != '\0' || tmpl == ULONG_MAX) \
396 return -1; \
397 if ( tmpl < (MIN) || tmpl > (MAX)) \
398 return -1; \
399 (V) = tmpl; \
402 /* Check hour field of time_str. */
403 colon = strchr (time_str, ':');
404 if (colon == NULL)
405 return -1;
406 *colon = '\0';
408 /* Hour must be between 0 and 23. */
409 GET_LONG_RANGE (hour, time_str, 0, 23);
411 /* Check min field of time_str. */
412 time_str = colon + 1;
413 colon = strchr (time_str, ':');
414 if (*time_str == '\0' || colon == NULL)
415 return -1;
416 *colon = '\0';
418 /* Min must be between 0 and 59. */
419 GET_LONG_RANGE (min, time_str, 0, 59);
421 /* Check sec field of time_str. */
422 time_str = colon + 1;
423 if (*time_str == '\0')
424 return -1;
426 /* Sec must be between 0 and 59. */
427 GET_LONG_RANGE (sec, time_str, 0, 59);
429 /* Check day_str. Day must be <1-31>. */
430 GET_LONG_RANGE (day, day_str, 1, 31);
432 /* Check month_str. Month must match month_name. */
433 month = 0;
434 if (strlen (month_str) >= 3)
435 for (i = 0; month_name[i]; i++)
436 if (strncmp (month_str, month_name[i], strlen (month_str)) == 0)
438 month = i;
439 break;
441 if (! month_name[i])
442 return -1;
444 /* Check year_str. Year must be <1993-2035>. */
445 GET_LONG_RANGE (year, year_str, 1993, 2035);
447 memset (&tm, 0, sizeof (struct tm));
448 tm.tm_sec = sec;
449 tm.tm_min = min;
450 tm.tm_hour = hour;
451 tm.tm_mon = month;
452 tm.tm_mday = day;
453 tm.tm_year = year - 1900;
455 time = mktime (&tm);
457 return time;
458 #undef GET_LONG_RANGE
461 static int
462 key_lifetime_set (struct vty *vty, struct key_range *krange,
463 const char *stime_str, const char *sday_str,
464 const char *smonth_str, const char *syear_str,
465 const char *etime_str, const char *eday_str,
466 const char *emonth_str, const char *eyear_str)
468 time_t time_start;
469 time_t time_end;
471 time_start = key_str2time (stime_str, sday_str, smonth_str, syear_str);
472 if (time_start < 0)
474 vty_out (vty, "Malformed time value%s", VTY_NEWLINE);
475 return CMD_WARNING;
477 time_end = key_str2time (etime_str, eday_str, emonth_str, eyear_str);
479 if (time_end < 0)
481 vty_out (vty, "Malformed time value%s", VTY_NEWLINE);
482 return CMD_WARNING;
485 if (time_end <= time_start)
487 vty_out (vty, "Expire time is not later than start time%s", VTY_NEWLINE);
488 return CMD_WARNING;
491 krange->start = time_start;
492 krange->end = time_end;
494 return CMD_SUCCESS;
497 static int
498 key_lifetime_duration_set (struct vty *vty, struct key_range *krange,
499 const char *stime_str, const char *sday_str,
500 const char *smonth_str, const char *syear_str,
501 const char *duration_str)
503 time_t time_start;
504 u_int32_t duration;
506 time_start = key_str2time (stime_str, sday_str, smonth_str, syear_str);
507 if (time_start < 0)
509 vty_out (vty, "Malformed time value%s", VTY_NEWLINE);
510 return CMD_WARNING;
512 krange->start = time_start;
514 VTY_GET_INTEGER ("duration", duration, duration_str);
515 krange->duration = 1;
516 krange->end = time_start + duration;
518 return CMD_SUCCESS;
521 static int
522 key_lifetime_infinite_set (struct vty *vty, struct key_range *krange,
523 const char *stime_str, const char *sday_str,
524 const char *smonth_str, const char *syear_str)
526 time_t time_start;
528 time_start = key_str2time (stime_str, sday_str, smonth_str, syear_str);
529 if (time_start < 0)
531 vty_out (vty, "Malformed time value%s", VTY_NEWLINE);
532 return CMD_WARNING;
534 krange->start = time_start;
536 krange->end = -1;
538 return CMD_SUCCESS;
541 DEFUN (accept_lifetime_day_month_day_month,
542 accept_lifetime_day_month_day_month_cmd,
543 "accept-lifetime HH:MM:SS <1-31> MONTH <1993-2035> HH:MM:SS <1-31> MONTH <1993-2035>",
544 "Set accept lifetime of the key\n"
545 "Time to start\n"
546 "Day of th month to start\n"
547 "Month of the year to start\n"
548 "Year to start\n"
549 "Time to expire\n"
550 "Day of th month to expire\n"
551 "Month of the year to expire\n"
552 "Year to expire\n")
554 struct key *key;
556 key = vty->index_sub;
558 return key_lifetime_set (vty, &key->accept, argv[0], argv[1], argv[2],
559 argv[3], argv[4], argv[5], argv[6], argv[7]);
562 DEFUN (accept_lifetime_day_month_month_day,
563 accept_lifetime_day_month_month_day_cmd,
564 "accept-lifetime HH:MM:SS <1-31> MONTH <1993-2035> HH:MM:SS MONTH <1-31> <1993-2035>",
565 "Set accept lifetime of the key\n"
566 "Time to start\n"
567 "Day of th month to start\n"
568 "Month of the year to start\n"
569 "Year to start\n"
570 "Time to expire\n"
571 "Month of the year to expire\n"
572 "Day of th month to expire\n"
573 "Year to expire\n")
575 struct key *key;
577 key = vty->index_sub;
579 return key_lifetime_set (vty, &key->accept, argv[0], argv[1], argv[2],
580 argv[3], argv[4], argv[6], argv[5], argv[7]);
583 DEFUN (accept_lifetime_month_day_day_month,
584 accept_lifetime_month_day_day_month_cmd,
585 "accept-lifetime HH:MM:SS MONTH <1-31> <1993-2035> HH:MM:SS <1-31> MONTH <1993-2035>",
586 "Set accept lifetime of the key\n"
587 "Time to start\n"
588 "Month of the year to start\n"
589 "Day of th month to start\n"
590 "Year to start\n"
591 "Time to expire\n"
592 "Day of th month to expire\n"
593 "Month of the year to expire\n"
594 "Year to expire\n")
596 struct key *key;
598 key = vty->index_sub;
600 return key_lifetime_set (vty, &key->accept, argv[0], argv[2], argv[1],
601 argv[3], argv[4], argv[5], argv[6], argv[7]);
604 DEFUN (accept_lifetime_month_day_month_day,
605 accept_lifetime_month_day_month_day_cmd,
606 "accept-lifetime HH:MM:SS MONTH <1-31> <1993-2035> HH:MM:SS MONTH <1-31> <1993-2035>",
607 "Set accept lifetime of the key\n"
608 "Time to start\n"
609 "Month of the year to start\n"
610 "Day of th month to start\n"
611 "Year to start\n"
612 "Time to expire\n"
613 "Month of the year to expire\n"
614 "Day of th month to expire\n"
615 "Year to expire\n")
617 struct key *key;
619 key = vty->index_sub;
621 return key_lifetime_set (vty, &key->accept, argv[0], argv[2], argv[1],
622 argv[3], argv[4], argv[6], argv[5], argv[7]);
625 DEFUN (accept_lifetime_infinite_day_month,
626 accept_lifetime_infinite_day_month_cmd,
627 "accept-lifetime HH:MM:SS <1-31> MONTH <1993-2035> infinite",
628 "Set accept lifetime of the key\n"
629 "Time to start\n"
630 "Day of th month to start\n"
631 "Month of the year to start\n"
632 "Year to start\n"
633 "Never expires")
635 struct key *key;
637 key = vty->index_sub;
639 return key_lifetime_infinite_set (vty, &key->accept, argv[0], argv[1],
640 argv[2], argv[3]);
643 DEFUN (accept_lifetime_infinite_month_day,
644 accept_lifetime_infinite_month_day_cmd,
645 "accept-lifetime HH:MM:SS MONTH <1-31> <1993-2035> infinite",
646 "Set accept lifetime of the key\n"
647 "Time to start\n"
648 "Month of the year to start\n"
649 "Day of th month to start\n"
650 "Year to start\n"
651 "Never expires")
653 struct key *key;
655 key = vty->index_sub;
657 return key_lifetime_infinite_set (vty, &key->accept, argv[0], argv[2],
658 argv[1], argv[3]);
661 DEFUN (accept_lifetime_duration_day_month,
662 accept_lifetime_duration_day_month_cmd,
663 "accept-lifetime HH:MM:SS <1-31> MONTH <1993-2035> duration <1-2147483646>",
664 "Set accept lifetime of the key\n"
665 "Time to start\n"
666 "Day of th month to start\n"
667 "Month of the year to start\n"
668 "Year to start\n"
669 "Duration of the key\n"
670 "Duration seconds\n")
672 struct key *key;
674 key = vty->index_sub;
676 return key_lifetime_duration_set (vty, &key->accept, argv[0], argv[1],
677 argv[2], argv[3], argv[4]);
680 DEFUN (accept_lifetime_duration_month_day,
681 accept_lifetime_duration_month_day_cmd,
682 "accept-lifetime HH:MM:SS MONTH <1-31> <1993-2035> duration <1-2147483646>",
683 "Set accept lifetime of the key\n"
684 "Time to start\n"
685 "Month of the year to start\n"
686 "Day of th month to start\n"
687 "Year to start\n"
688 "Duration of the key\n"
689 "Duration seconds\n")
691 struct key *key;
693 key = vty->index_sub;
695 return key_lifetime_duration_set (vty, &key->accept, argv[0], argv[2],
696 argv[1], argv[3], argv[4]);
699 DEFUN (send_lifetime_day_month_day_month,
700 send_lifetime_day_month_day_month_cmd,
701 "send-lifetime HH:MM:SS <1-31> MONTH <1993-2035> HH:MM:SS <1-31> MONTH <1993-2035>",
702 "Set send lifetime of the key\n"
703 "Time to start\n"
704 "Day of th month to start\n"
705 "Month of the year to start\n"
706 "Year to start\n"
707 "Time to expire\n"
708 "Day of th month to expire\n"
709 "Month of the year to expire\n"
710 "Year to expire\n")
712 struct key *key;
714 key = vty->index_sub;
716 return key_lifetime_set (vty, &key->send, argv[0], argv[1], argv[2], argv[3],
717 argv[4], argv[5], argv[6], argv[7]);
720 DEFUN (send_lifetime_day_month_month_day,
721 send_lifetime_day_month_month_day_cmd,
722 "send-lifetime HH:MM:SS <1-31> MONTH <1993-2035> HH:MM:SS MONTH <1-31> <1993-2035>",
723 "Set send lifetime of the key\n"
724 "Time to start\n"
725 "Day of th month to start\n"
726 "Month of the year to start\n"
727 "Year to start\n"
728 "Time to expire\n"
729 "Month of the year to expire\n"
730 "Day of th month to expire\n"
731 "Year to expire\n")
733 struct key *key;
735 key = vty->index_sub;
737 return key_lifetime_set (vty, &key->send, argv[0], argv[1], argv[2], argv[3],
738 argv[4], argv[6], argv[5], argv[7]);
741 DEFUN (send_lifetime_month_day_day_month,
742 send_lifetime_month_day_day_month_cmd,
743 "send-lifetime HH:MM:SS MONTH <1-31> <1993-2035> HH:MM:SS <1-31> MONTH <1993-2035>",
744 "Set send lifetime of the key\n"
745 "Time to start\n"
746 "Month of the year to start\n"
747 "Day of th month to start\n"
748 "Year to start\n"
749 "Time to expire\n"
750 "Day of th month to expire\n"
751 "Month of the year to expire\n"
752 "Year to expire\n")
754 struct key *key;
756 key = vty->index_sub;
758 return key_lifetime_set (vty, &key->send, argv[0], argv[2], argv[1], argv[3],
759 argv[4], argv[5], argv[6], argv[7]);
762 DEFUN (send_lifetime_month_day_month_day,
763 send_lifetime_month_day_month_day_cmd,
764 "send-lifetime HH:MM:SS MONTH <1-31> <1993-2035> HH:MM:SS MONTH <1-31> <1993-2035>",
765 "Set send lifetime of the key\n"
766 "Time to start\n"
767 "Month of the year to start\n"
768 "Day of th month to start\n"
769 "Year to start\n"
770 "Time to expire\n"
771 "Month of the year to expire\n"
772 "Day of th month to expire\n"
773 "Year to expire\n")
775 struct key *key;
777 key = vty->index_sub;
779 return key_lifetime_set (vty, &key->send, argv[0], argv[2], argv[1], argv[3],
780 argv[4], argv[6], argv[5], argv[7]);
783 DEFUN (send_lifetime_infinite_day_month,
784 send_lifetime_infinite_day_month_cmd,
785 "send-lifetime HH:MM:SS <1-31> MONTH <1993-2035> infinite",
786 "Set send lifetime of the key\n"
787 "Time to start\n"
788 "Day of th month to start\n"
789 "Month of the year to start\n"
790 "Year to start\n"
791 "Never expires")
793 struct key *key;
795 key = vty->index_sub;
797 return key_lifetime_infinite_set (vty, &key->send, argv[0], argv[1], argv[2],
798 argv[3]);
801 DEFUN (send_lifetime_infinite_month_day,
802 send_lifetime_infinite_month_day_cmd,
803 "send-lifetime HH:MM:SS MONTH <1-31> <1993-2035> infinite",
804 "Set send lifetime of the key\n"
805 "Time to start\n"
806 "Month of the year to start\n"
807 "Day of th month to start\n"
808 "Year to start\n"
809 "Never expires")
811 struct key *key;
813 key = vty->index_sub;
815 return key_lifetime_infinite_set (vty, &key->send, argv[0], argv[2], argv[1],
816 argv[3]);
819 DEFUN (send_lifetime_duration_day_month,
820 send_lifetime_duration_day_month_cmd,
821 "send-lifetime HH:MM:SS <1-31> MONTH <1993-2035> duration <1-2147483646>",
822 "Set send lifetime of the key\n"
823 "Time to start\n"
824 "Day of th month to start\n"
825 "Month of the year to start\n"
826 "Year to start\n"
827 "Duration of the key\n"
828 "Duration seconds\n")
830 struct key *key;
832 key = vty->index_sub;
834 return key_lifetime_duration_set (vty, &key->send, argv[0], argv[1], argv[2],
835 argv[3], argv[4]);
838 DEFUN (send_lifetime_duration_month_day,
839 send_lifetime_duration_month_day_cmd,
840 "send-lifetime HH:MM:SS MONTH <1-31> <1993-2035> duration <1-2147483646>",
841 "Set send lifetime of the key\n"
842 "Time to start\n"
843 "Month of the year to start\n"
844 "Day of th month to start\n"
845 "Year to start\n"
846 "Duration of the key\n"
847 "Duration seconds\n")
849 struct key *key;
851 key = vty->index_sub;
853 return key_lifetime_duration_set (vty, &key->send, argv[0], argv[2], argv[1],
854 argv[3], argv[4]);
857 struct cmd_node keychain_node =
859 KEYCHAIN_NODE,
860 "%s(config-keychain)# ",
864 struct cmd_node keychain_key_node =
866 KEYCHAIN_KEY_NODE,
867 "%s(config-keychain-key)# ",
871 static int
872 keychain_strftime (char *buf, int bufsiz, time_t *time)
874 struct tm *tm;
875 size_t len;
877 tm = localtime (time);
879 len = strftime (buf, bufsiz, "%T %b %d %Y", tm);
881 return len;
884 static int
885 keychain_config_write (struct vty *vty)
887 struct keychain *keychain;
888 struct key *key;
889 struct listnode *node;
890 struct listnode *knode;
891 char buf[BUFSIZ];
893 for (ALL_LIST_ELEMENTS_RO (keychain_list, node, keychain))
895 vty_out (vty, "key chain %s%s", keychain->name, VTY_NEWLINE);
897 for (ALL_LIST_ELEMENTS_RO (keychain->key, knode, key))
899 vty_out (vty, " key %d%s", key->index, VTY_NEWLINE);
901 if (key->string)
902 vty_out (vty, " key-string %s%s", key->string, VTY_NEWLINE);
904 if (key->accept.start)
906 keychain_strftime (buf, BUFSIZ, &key->accept.start);
907 vty_out (vty, " accept-lifetime %s", buf);
909 if (key->accept.end == -1)
910 vty_out (vty, " infinite");
911 else if (key->accept.duration)
912 vty_out (vty, " duration %ld",
913 (long)(key->accept.end - key->accept.start));
914 else
916 keychain_strftime (buf, BUFSIZ, &key->accept.end);
917 vty_out (vty, " %s", buf);
919 vty_out (vty, "%s", VTY_NEWLINE);
922 if (key->send.start)
924 keychain_strftime (buf, BUFSIZ, &key->send.start);
925 vty_out (vty, " send-lifetime %s", buf);
927 if (key->send.end == -1)
928 vty_out (vty, " infinite");
929 else if (key->send.duration)
930 vty_out (vty, " duration %ld", (long)(key->send.end - key->send.start));
931 else
933 keychain_strftime (buf, BUFSIZ, &key->send.end);
934 vty_out (vty, " %s", buf);
936 vty_out (vty, "%s", VTY_NEWLINE);
939 vty_out (vty, "!%s", VTY_NEWLINE);
942 return 0;
945 void
946 keychain_init ()
948 keychain_list = list_new ();
950 install_node (&keychain_node, keychain_config_write);
951 install_node (&keychain_key_node, NULL);
953 install_default (KEYCHAIN_NODE);
954 install_default (KEYCHAIN_KEY_NODE);
956 install_element (CONFIG_NODE, &key_chain_cmd);
957 install_element (CONFIG_NODE, &no_key_chain_cmd);
958 install_element (KEYCHAIN_NODE, &key_cmd);
959 install_element (KEYCHAIN_NODE, &no_key_cmd);
961 install_element (KEYCHAIN_NODE, &key_chain_cmd);
962 install_element (KEYCHAIN_NODE, &no_key_chain_cmd);
964 install_element (KEYCHAIN_KEY_NODE, &key_string_cmd);
965 install_element (KEYCHAIN_KEY_NODE, &no_key_string_cmd);
967 install_element (KEYCHAIN_KEY_NODE, &key_chain_cmd);
968 install_element (KEYCHAIN_KEY_NODE, &no_key_chain_cmd);
970 install_element (KEYCHAIN_KEY_NODE, &key_cmd);
971 install_element (KEYCHAIN_KEY_NODE, &no_key_cmd);
973 install_element (KEYCHAIN_KEY_NODE, &accept_lifetime_day_month_day_month_cmd);
974 install_element (KEYCHAIN_KEY_NODE, &accept_lifetime_day_month_month_day_cmd);
975 install_element (KEYCHAIN_KEY_NODE, &accept_lifetime_month_day_day_month_cmd);
976 install_element (KEYCHAIN_KEY_NODE, &accept_lifetime_month_day_month_day_cmd);
977 install_element (KEYCHAIN_KEY_NODE, &accept_lifetime_infinite_day_month_cmd);
978 install_element (KEYCHAIN_KEY_NODE, &accept_lifetime_infinite_month_day_cmd);
979 install_element (KEYCHAIN_KEY_NODE, &accept_lifetime_duration_day_month_cmd);
980 install_element (KEYCHAIN_KEY_NODE, &accept_lifetime_duration_month_day_cmd);
982 install_element (KEYCHAIN_KEY_NODE, &send_lifetime_day_month_day_month_cmd);
983 install_element (KEYCHAIN_KEY_NODE, &send_lifetime_day_month_month_day_cmd);
984 install_element (KEYCHAIN_KEY_NODE, &send_lifetime_month_day_day_month_cmd);
985 install_element (KEYCHAIN_KEY_NODE, &send_lifetime_month_day_month_day_cmd);
986 install_element (KEYCHAIN_KEY_NODE, &send_lifetime_infinite_day_month_cmd);
987 install_element (KEYCHAIN_KEY_NODE, &send_lifetime_infinite_month_day_cmd);
988 install_element (KEYCHAIN_KEY_NODE, &send_lifetime_duration_day_month_cmd);
989 install_element (KEYCHAIN_KEY_NODE, &send_lifetime_duration_month_day_cmd);