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. */
28 /* Master list of key chain. */
29 struct list *keychain_list;
31 static struct keychain *
34 return XCALLOC (MTYPE_KEYCHAIN, sizeof (struct keychain));
38 keychain_free (struct keychain *keychain)
40 XFREE (MTYPE_KEYCHAIN, keychain);
46 return XCALLOC (MTYPE_KEY, sizeof (struct key));
50 key_free (struct key *key)
52 XFREE (MTYPE_KEY, key);
56 keychain_lookup (const char *name)
58 struct listnode *node;
59 struct keychain *keychain;
64 for (ALL_LIST_ELEMENTS_RO (keychain_list, node, keychain))
66 if (strcmp (keychain->name, name) == 0)
73 key_cmp_func (void *arg1, void *arg2)
75 const struct key *k1 = arg1;
76 const struct key *k2 = arg2;
78 if (k1->index > k2->index)
80 if (k1->index < k2->index)
86 key_delete_func (struct key *key)
93 static struct keychain *
94 keychain_get (const char *name)
96 struct keychain *keychain;
98 keychain = keychain_lookup (name);
103 keychain = keychain_new ();
104 keychain->name = strdup (name);
105 keychain->key = list_new ();
106 keychain->key->cmp = (int (*)(void *, void *)) key_cmp_func;
107 keychain->key->del = (void (*)(void *)) key_delete_func;
108 listnode_add (keychain_list, keychain);
114 keychain_delete (struct keychain *keychain)
117 free (keychain->name);
119 list_delete (keychain->key);
120 listnode_delete (keychain_list, keychain);
121 keychain_free (keychain);
125 key_lookup (const struct keychain *keychain, u_int32_t index)
127 struct listnode *node;
130 for (ALL_LIST_ELEMENTS_RO (keychain->key, node, key))
132 if (key->index == index)
139 key_lookup_for_accept (const struct keychain *keychain, u_int32_t index)
141 struct listnode *node;
147 for (ALL_LIST_ELEMENTS_RO (keychain->key, node, key))
149 if (key->index >= index)
151 if (key->accept.start == 0)
154 if (key->accept.start <= now)
155 if (key->accept.end >= now || key->accept.end == -1)
163 key_match_for_accept (const struct keychain *keychain, const char *auth_str)
165 struct listnode *node;
171 for (ALL_LIST_ELEMENTS_RO (keychain->key, node, key))
173 if (key->accept.start == 0 ||
174 (key->accept.start <= now &&
175 (key->accept.end >= now || key->accept.end == -1)))
176 if (strncmp (key->string, auth_str, 16) == 0)
183 key_lookup_for_send (const struct keychain *keychain)
185 struct listnode *node;
191 for (ALL_LIST_ELEMENTS_RO (keychain->key, node, key))
193 if (key->send.start == 0)
196 if (key->send.start <= now)
197 if (key->send.end >= now || key->send.end == -1)
204 key_get (const struct keychain *keychain, u_int32_t index)
208 key = key_lookup (keychain, index);
215 listnode_add_sort (keychain->key, key);
221 key_delete (struct keychain *keychain, struct key *key)
223 listnode_delete (keychain->key, key);
233 "Authentication key management\n"
234 "Key-chain management\n"
237 struct keychain *keychain;
239 keychain = keychain_get (argv[0]);
240 vty->index = keychain;
241 vty->node = KEYCHAIN_NODE;
250 "Authentication key management\n"
251 "Key-chain management\n"
254 struct keychain *keychain;
256 keychain = keychain_lookup (argv[0]);
260 vty_out (vty, "Can't find keychain %s%s", argv[0], VTY_NEWLINE);
264 keychain_delete (keychain);
271 "key <0-2147483647>",
273 "Key identifier number\n")
275 struct keychain *keychain;
279 keychain = vty->index;
281 VTY_GET_INTEGER ("key identifier", index, argv[0]);
282 key = key_get (keychain, index);
283 vty->index_sub = key;
284 vty->node = KEYCHAIN_KEY_NODE;
291 "no key <0-2147483647>",
294 "Key identifier number\n")
296 struct keychain *keychain;
300 keychain = vty->index;
302 VTY_GET_INTEGER ("key identifier", index, argv[0]);
303 key = key_lookup (keychain, index);
306 vty_out (vty, "Can't find key %d%s", index, VTY_NEWLINE);
310 key_delete (keychain, key);
312 vty->node = KEYCHAIN_NODE;
325 key = vty->index_sub;
329 key->string = strdup (argv[0]);
334 DEFUN (no_key_string,
336 "no key-string [LINE]",
343 key = vty->index_sub;
354 /* Convert HH:MM:SS MON DAY YEAR to time_t value. -1 is returned when
355 given string is malformed. */
357 key_str2time (const char *time_str, const char *day_str, const char *month_str,
358 const char *year_str)
364 unsigned int sec, min, hour;
365 unsigned int day, month, year;
367 const char *month_name[] =
384 #define _GET_LONG_RANGE(V,STR,MMCOND) \
386 unsigned long tmpl; \
387 char *endptr = NULL; \
388 tmpl = strtoul ((STR), &endptr, 10); \
389 if (*endptr != '\0' || tmpl == ULONG_MAX) \
395 #define GET_LONG_RANGE(V,STR,MIN,MAX) \
396 _GET_LONG_RANGE(V,STR,tmpl < (MIN) || tmpl > (MAX))
397 #define GET_LONG_RANGE0(V,STR,MAX) \
398 _GET_LONG_RANGE(V,STR,tmpl > (MAX))
400 /* Check hour field of time_str. */
401 colon = strchr (time_str, ':');
406 /* Hour must be between 0 and 23. */
407 GET_LONG_RANGE0 (hour, time_str, 23);
409 /* Check min field of time_str. */
410 time_str = colon + 1;
411 colon = strchr (time_str, ':');
412 if (*time_str == '\0' || colon == NULL)
416 /* Min must be between 0 and 59. */
417 GET_LONG_RANGE0 (min, time_str, 59);
419 /* Check sec field of time_str. */
420 time_str = colon + 1;
421 if (*time_str == '\0')
424 /* Sec must be between 0 and 59. */
425 GET_LONG_RANGE0 (sec, time_str, 59);
427 /* Check day_str. Day must be <1-31>. */
428 GET_LONG_RANGE (day, day_str, 1, 31);
430 /* Check month_str. Month must match month_name. */
432 if (strlen (month_str) >= 3)
433 for (i = 0; month_name[i]; i++)
434 if (strncmp (month_str, month_name[i], strlen (month_str)) == 0)
442 /* Check year_str. Year must be <1993-2035>. */
443 GET_LONG_RANGE (year, year_str, 1993, 2035);
445 memset (&tm, 0, sizeof (struct tm));
451 tm.tm_year = year - 1900;
456 #undef GET_LONG_RANGE
460 key_lifetime_set (struct vty *vty, struct key_range *krange,
461 const char *stime_str, const char *sday_str,
462 const char *smonth_str, const char *syear_str,
463 const char *etime_str, const char *eday_str,
464 const char *emonth_str, const char *eyear_str)
469 time_start = key_str2time (stime_str, sday_str, smonth_str, syear_str);
472 vty_out (vty, "Malformed time value%s", VTY_NEWLINE);
475 time_end = key_str2time (etime_str, eday_str, emonth_str, eyear_str);
479 vty_out (vty, "Malformed time value%s", VTY_NEWLINE);
483 if (time_end <= time_start)
485 vty_out (vty, "Expire time is not later than start time%s", VTY_NEWLINE);
489 krange->start = time_start;
490 krange->end = time_end;
496 key_lifetime_duration_set (struct vty *vty, struct key_range *krange,
497 const char *stime_str, const char *sday_str,
498 const char *smonth_str, const char *syear_str,
499 const char *duration_str)
504 time_start = key_str2time (stime_str, sday_str, smonth_str, syear_str);
507 vty_out (vty, "Malformed time value%s", VTY_NEWLINE);
510 krange->start = time_start;
512 VTY_GET_INTEGER ("duration", duration, duration_str);
513 krange->duration = 1;
514 krange->end = time_start + duration;
520 key_lifetime_infinite_set (struct vty *vty, struct key_range *krange,
521 const char *stime_str, const char *sday_str,
522 const char *smonth_str, const char *syear_str)
526 time_start = key_str2time (stime_str, sday_str, smonth_str, syear_str);
529 vty_out (vty, "Malformed time value%s", VTY_NEWLINE);
532 krange->start = time_start;
539 DEFUN (accept_lifetime_day_month_day_month,
540 accept_lifetime_day_month_day_month_cmd,
541 "accept-lifetime HH:MM:SS <1-31> MONTH <1993-2035> HH:MM:SS <1-31> MONTH <1993-2035>",
542 "Set accept lifetime of the key\n"
544 "Day of th month to start\n"
545 "Month of the year to start\n"
548 "Day of th month to expire\n"
549 "Month of the year to expire\n"
554 key = vty->index_sub;
556 return key_lifetime_set (vty, &key->accept, argv[0], argv[1], argv[2],
557 argv[3], argv[4], argv[5], argv[6], argv[7]);
560 DEFUN (accept_lifetime_day_month_month_day,
561 accept_lifetime_day_month_month_day_cmd,
562 "accept-lifetime HH:MM:SS <1-31> MONTH <1993-2035> HH:MM:SS MONTH <1-31> <1993-2035>",
563 "Set accept lifetime of the key\n"
565 "Day of th month to start\n"
566 "Month of the year to start\n"
569 "Month of the year to expire\n"
570 "Day of th month to expire\n"
575 key = vty->index_sub;
577 return key_lifetime_set (vty, &key->accept, argv[0], argv[1], argv[2],
578 argv[3], argv[4], argv[6], argv[5], argv[7]);
581 DEFUN (accept_lifetime_month_day_day_month,
582 accept_lifetime_month_day_day_month_cmd,
583 "accept-lifetime HH:MM:SS MONTH <1-31> <1993-2035> HH:MM:SS <1-31> MONTH <1993-2035>",
584 "Set accept lifetime of the key\n"
586 "Month of the year to start\n"
587 "Day of th month to start\n"
590 "Day of th month to expire\n"
591 "Month of the year to expire\n"
596 key = vty->index_sub;
598 return key_lifetime_set (vty, &key->accept, argv[0], argv[2], argv[1],
599 argv[3], argv[4], argv[5], argv[6], argv[7]);
602 DEFUN (accept_lifetime_month_day_month_day,
603 accept_lifetime_month_day_month_day_cmd,
604 "accept-lifetime HH:MM:SS MONTH <1-31> <1993-2035> HH:MM:SS MONTH <1-31> <1993-2035>",
605 "Set accept lifetime of the key\n"
607 "Month of the year to start\n"
608 "Day of th month to start\n"
611 "Month of the year to expire\n"
612 "Day of th month to expire\n"
617 key = vty->index_sub;
619 return key_lifetime_set (vty, &key->accept, argv[0], argv[2], argv[1],
620 argv[3], argv[4], argv[6], argv[5], argv[7]);
623 DEFUN (accept_lifetime_infinite_day_month,
624 accept_lifetime_infinite_day_month_cmd,
625 "accept-lifetime HH:MM:SS <1-31> MONTH <1993-2035> infinite",
626 "Set accept lifetime of the key\n"
628 "Day of th month to start\n"
629 "Month of the year to start\n"
635 key = vty->index_sub;
637 return key_lifetime_infinite_set (vty, &key->accept, argv[0], argv[1],
641 DEFUN (accept_lifetime_infinite_month_day,
642 accept_lifetime_infinite_month_day_cmd,
643 "accept-lifetime HH:MM:SS MONTH <1-31> <1993-2035> infinite",
644 "Set accept lifetime of the key\n"
646 "Month of the year to start\n"
647 "Day of th month to start\n"
653 key = vty->index_sub;
655 return key_lifetime_infinite_set (vty, &key->accept, argv[0], argv[2],
659 DEFUN (accept_lifetime_duration_day_month,
660 accept_lifetime_duration_day_month_cmd,
661 "accept-lifetime HH:MM:SS <1-31> MONTH <1993-2035> duration <1-2147483646>",
662 "Set accept lifetime of the key\n"
664 "Day of th month to start\n"
665 "Month of the year to start\n"
667 "Duration of the key\n"
668 "Duration seconds\n")
672 key = vty->index_sub;
674 return key_lifetime_duration_set (vty, &key->accept, argv[0], argv[1],
675 argv[2], argv[3], argv[4]);
678 DEFUN (accept_lifetime_duration_month_day,
679 accept_lifetime_duration_month_day_cmd,
680 "accept-lifetime HH:MM:SS MONTH <1-31> <1993-2035> duration <1-2147483646>",
681 "Set accept lifetime of the key\n"
683 "Month of the year to start\n"
684 "Day of th month to start\n"
686 "Duration of the key\n"
687 "Duration seconds\n")
691 key = vty->index_sub;
693 return key_lifetime_duration_set (vty, &key->accept, argv[0], argv[2],
694 argv[1], argv[3], argv[4]);
697 DEFUN (send_lifetime_day_month_day_month,
698 send_lifetime_day_month_day_month_cmd,
699 "send-lifetime HH:MM:SS <1-31> MONTH <1993-2035> HH:MM:SS <1-31> MONTH <1993-2035>",
700 "Set send lifetime of the key\n"
702 "Day of th month to start\n"
703 "Month of the year to start\n"
706 "Day of th month to expire\n"
707 "Month of the year to expire\n"
712 key = vty->index_sub;
714 return key_lifetime_set (vty, &key->send, argv[0], argv[1], argv[2], argv[3],
715 argv[4], argv[5], argv[6], argv[7]);
718 DEFUN (send_lifetime_day_month_month_day,
719 send_lifetime_day_month_month_day_cmd,
720 "send-lifetime HH:MM:SS <1-31> MONTH <1993-2035> HH:MM:SS MONTH <1-31> <1993-2035>",
721 "Set send lifetime of the key\n"
723 "Day of th month to start\n"
724 "Month of the year to start\n"
727 "Month of the year to expire\n"
728 "Day of th month to expire\n"
733 key = vty->index_sub;
735 return key_lifetime_set (vty, &key->send, argv[0], argv[1], argv[2], argv[3],
736 argv[4], argv[6], argv[5], argv[7]);
739 DEFUN (send_lifetime_month_day_day_month,
740 send_lifetime_month_day_day_month_cmd,
741 "send-lifetime HH:MM:SS MONTH <1-31> <1993-2035> HH:MM:SS <1-31> MONTH <1993-2035>",
742 "Set send lifetime of the key\n"
744 "Month of the year to start\n"
745 "Day of th month to start\n"
748 "Day of th month to expire\n"
749 "Month of the year to expire\n"
754 key = vty->index_sub;
756 return key_lifetime_set (vty, &key->send, argv[0], argv[2], argv[1], argv[3],
757 argv[4], argv[5], argv[6], argv[7]);
760 DEFUN (send_lifetime_month_day_month_day,
761 send_lifetime_month_day_month_day_cmd,
762 "send-lifetime HH:MM:SS MONTH <1-31> <1993-2035> HH:MM:SS MONTH <1-31> <1993-2035>",
763 "Set send lifetime of the key\n"
765 "Month of the year to start\n"
766 "Day of th month to start\n"
769 "Month of the year to expire\n"
770 "Day of th month to expire\n"
775 key = vty->index_sub;
777 return key_lifetime_set (vty, &key->send, argv[0], argv[2], argv[1], argv[3],
778 argv[4], argv[6], argv[5], argv[7]);
781 DEFUN (send_lifetime_infinite_day_month,
782 send_lifetime_infinite_day_month_cmd,
783 "send-lifetime HH:MM:SS <1-31> MONTH <1993-2035> infinite",
784 "Set send lifetime of the key\n"
786 "Day of th month to start\n"
787 "Month of the year to start\n"
793 key = vty->index_sub;
795 return key_lifetime_infinite_set (vty, &key->send, argv[0], argv[1], argv[2],
799 DEFUN (send_lifetime_infinite_month_day,
800 send_lifetime_infinite_month_day_cmd,
801 "send-lifetime HH:MM:SS MONTH <1-31> <1993-2035> infinite",
802 "Set send lifetime of the key\n"
804 "Month of the year to start\n"
805 "Day of th month to start\n"
811 key = vty->index_sub;
813 return key_lifetime_infinite_set (vty, &key->send, argv[0], argv[2], argv[1],
817 DEFUN (send_lifetime_duration_day_month,
818 send_lifetime_duration_day_month_cmd,
819 "send-lifetime HH:MM:SS <1-31> MONTH <1993-2035> duration <1-2147483646>",
820 "Set send lifetime of the key\n"
822 "Day of th month to start\n"
823 "Month of the year to start\n"
825 "Duration of the key\n"
826 "Duration seconds\n")
830 key = vty->index_sub;
832 return key_lifetime_duration_set (vty, &key->send, argv[0], argv[1], argv[2],
836 DEFUN (send_lifetime_duration_month_day,
837 send_lifetime_duration_month_day_cmd,
838 "send-lifetime HH:MM:SS MONTH <1-31> <1993-2035> duration <1-2147483646>",
839 "Set send lifetime of the key\n"
841 "Month of the year to start\n"
842 "Day of th month to start\n"
844 "Duration of the key\n"
845 "Duration seconds\n")
849 key = vty->index_sub;
851 return key_lifetime_duration_set (vty, &key->send, argv[0], argv[2], argv[1],
855 static struct cmd_node keychain_node =
858 "%s(config-keychain)# ",
862 static struct cmd_node keychain_key_node =
865 "%s(config-keychain-key)# ",
870 keychain_strftime (char *buf, int bufsiz, time_t *time)
875 tm = localtime (time);
877 len = strftime (buf, bufsiz, "%T %b %d %Y", tm);
883 keychain_config_write (struct vty *vty)
885 struct keychain *keychain;
887 struct listnode *node;
888 struct listnode *knode;
891 for (ALL_LIST_ELEMENTS_RO (keychain_list, node, keychain))
893 vty_out (vty, "key chain %s%s", keychain->name, VTY_NEWLINE);
895 for (ALL_LIST_ELEMENTS_RO (keychain->key, knode, key))
897 vty_out (vty, " key %d%s", key->index, VTY_NEWLINE);
900 vty_out (vty, " key-string %s%s", key->string, VTY_NEWLINE);
902 if (key->accept.start)
904 keychain_strftime (buf, BUFSIZ, &key->accept.start);
905 vty_out (vty, " accept-lifetime %s", buf);
907 if (key->accept.end == -1)
908 vty_out (vty, " infinite");
909 else if (key->accept.duration)
910 vty_out (vty, " duration %ld",
911 (long)(key->accept.end - key->accept.start));
914 keychain_strftime (buf, BUFSIZ, &key->accept.end);
915 vty_out (vty, " %s", buf);
917 vty_out (vty, "%s", VTY_NEWLINE);
922 keychain_strftime (buf, BUFSIZ, &key->send.start);
923 vty_out (vty, " send-lifetime %s", buf);
925 if (key->send.end == -1)
926 vty_out (vty, " infinite");
927 else if (key->send.duration)
928 vty_out (vty, " duration %ld", (long)(key->send.end - key->send.start));
931 keychain_strftime (buf, BUFSIZ, &key->send.end);
932 vty_out (vty, " %s", buf);
934 vty_out (vty, "%s", VTY_NEWLINE);
937 vty_out (vty, "!%s", VTY_NEWLINE);
946 keychain_list = list_new ();
948 install_node (&keychain_node, keychain_config_write);
949 install_node (&keychain_key_node, NULL);
951 install_default (KEYCHAIN_NODE);
952 install_default (KEYCHAIN_KEY_NODE);
954 install_element (CONFIG_NODE, &key_chain_cmd);
955 install_element (CONFIG_NODE, &no_key_chain_cmd);
956 install_element (KEYCHAIN_NODE, &key_cmd);
957 install_element (KEYCHAIN_NODE, &no_key_cmd);
959 install_element (KEYCHAIN_NODE, &key_chain_cmd);
960 install_element (KEYCHAIN_NODE, &no_key_chain_cmd);
962 install_element (KEYCHAIN_KEY_NODE, &key_string_cmd);
963 install_element (KEYCHAIN_KEY_NODE, &no_key_string_cmd);
965 install_element (KEYCHAIN_KEY_NODE, &key_chain_cmd);
966 install_element (KEYCHAIN_KEY_NODE, &no_key_chain_cmd);
968 install_element (KEYCHAIN_KEY_NODE, &key_cmd);
969 install_element (KEYCHAIN_KEY_NODE, &no_key_cmd);
971 install_element (KEYCHAIN_KEY_NODE, &accept_lifetime_day_month_day_month_cmd);
972 install_element (KEYCHAIN_KEY_NODE, &accept_lifetime_day_month_month_day_cmd);
973 install_element (KEYCHAIN_KEY_NODE, &accept_lifetime_month_day_day_month_cmd);
974 install_element (KEYCHAIN_KEY_NODE, &accept_lifetime_month_day_month_day_cmd);
975 install_element (KEYCHAIN_KEY_NODE, &accept_lifetime_infinite_day_month_cmd);
976 install_element (KEYCHAIN_KEY_NODE, &accept_lifetime_infinite_month_day_cmd);
977 install_element (KEYCHAIN_KEY_NODE, &accept_lifetime_duration_day_month_cmd);
978 install_element (KEYCHAIN_KEY_NODE, &accept_lifetime_duration_month_day_cmd);
980 install_element (KEYCHAIN_KEY_NODE, &send_lifetime_day_month_day_month_cmd);
981 install_element (KEYCHAIN_KEY_NODE, &send_lifetime_day_month_month_day_cmd);
982 install_element (KEYCHAIN_KEY_NODE, &send_lifetime_month_day_day_month_cmd);
983 install_element (KEYCHAIN_KEY_NODE, &send_lifetime_month_day_month_day_cmd);
984 install_element (KEYCHAIN_KEY_NODE, &send_lifetime_infinite_day_month_cmd);
985 install_element (KEYCHAIN_KEY_NODE, &send_lifetime_infinite_month_day_cmd);
986 install_element (KEYCHAIN_KEY_NODE, &send_lifetime_duration_day_month_cmd);
987 install_element (KEYCHAIN_KEY_NODE, &send_lifetime_duration_month_day_cmd);