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
*
35 new = XMALLOC (MTYPE_KEYCHAIN
, sizeof (struct keychain
));
36 memset (new, 0, sizeof (struct keychain
));
41 keychain_free (struct keychain
*keychain
)
43 XFREE (MTYPE_KEYCHAIN
, keychain
);
50 new = XMALLOC (MTYPE_KEY
, sizeof (struct key
));
51 memset (new, 0, sizeof (struct key
));
56 key_free (struct key
*key
)
58 XFREE (MTYPE_KEY
, key
);
62 keychain_lookup (const char *name
)
64 struct listnode
*node
;
65 struct keychain
*keychain
;
70 for (ALL_LIST_ELEMENTS_RO (keychain_list
, node
, keychain
))
72 if (strcmp (keychain
->name
, name
) == 0)
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
)
86 if (k1
->index
< k2
->index
)
92 key_delete_func (struct key
*key
)
99 static struct keychain
*
100 keychain_get (const char *name
)
102 struct keychain
*keychain
;
104 keychain
= keychain_lookup (name
);
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
);
120 keychain_delete (struct keychain
*keychain
)
123 free (keychain
->name
);
125 list_delete (keychain
->key
);
126 listnode_delete (keychain_list
, keychain
);
127 keychain_free (keychain
);
131 key_lookup (const struct keychain
*keychain
, u_int32_t index
)
133 struct listnode
*node
;
136 for (ALL_LIST_ELEMENTS_RO (keychain
->key
, node
, key
))
138 if (key
->index
== index
)
145 key_lookup_for_accept (const struct keychain
*keychain
, u_int32_t index
)
147 struct listnode
*node
;
153 for (ALL_LIST_ELEMENTS_RO (keychain
->key
, node
, key
))
155 if (key
->index
>= index
)
157 if (key
->accept
.start
== 0)
160 if (key
->accept
.start
<= now
)
161 if (key
->accept
.end
>= now
|| key
->accept
.end
== -1)
169 key_match_for_accept (const struct keychain
*keychain
, const char *auth_str
)
171 struct listnode
*node
;
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)
189 key_lookup_for_send (const struct keychain
*keychain
)
191 struct listnode
*node
;
197 for (ALL_LIST_ELEMENTS_RO (keychain
->key
, node
, key
))
199 if (key
->send
.start
== 0)
202 if (key
->send
.start
<= now
)
203 if (key
->send
.end
>= now
|| key
->send
.end
== -1)
210 key_get (const struct keychain
*keychain
, u_int32_t index
)
214 key
= key_lookup (keychain
, index
);
221 listnode_add_sort (keychain
->key
, key
);
227 key_delete (struct keychain
*keychain
, struct key
*key
)
229 listnode_delete (keychain
->key
, key
);
239 "Authentication key management\n"
240 "Key-chain management\n"
243 struct keychain
*keychain
;
245 keychain
= keychain_get (argv
[0]);
246 vty
->index
= keychain
;
247 vty
->node
= KEYCHAIN_NODE
;
256 "Authentication key management\n"
257 "Key-chain management\n"
260 struct keychain
*keychain
;
262 keychain
= keychain_lookup (argv
[0]);
266 vty_out (vty
, "Can't find keychain %s%s", argv
[0], VTY_NEWLINE
);
270 keychain_delete (keychain
);
277 "key <0-2147483647>",
279 "Key identifier number\n")
281 struct keychain
*keychain
;
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
;
297 "no key <0-2147483647>",
300 "Key identifier number\n")
302 struct keychain
*keychain
;
306 keychain
= vty
->index
;
308 VTY_GET_INTEGER ("key identifier", index
, argv
[0]);
309 key
= key_lookup (keychain
, index
);
312 vty_out (vty
, "Can't find key %d%s", index
, VTY_NEWLINE
);
316 key_delete (keychain
, key
);
318 vty
->node
= KEYCHAIN_NODE
;
331 key
= vty
->index_sub
;
335 key
->string
= strdup (argv
[0]);
340 DEFUN (no_key_string
,
342 "no key-string [LINE]",
349 key
= vty
->index_sub
;
360 /* Convert HH:MM:SS MON DAY YEAR to time_t value. -1 is returned when
361 given string is malformed. */
363 key_str2time (const char *time_str
, const char *day_str
, const char *month_str
,
364 const char *year_str
)
370 unsigned int sec
, min
, hour
;
371 unsigned int day
, month
, year
;
373 const char *month_name
[] =
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) \
397 if ( tmpl < (MIN) || tmpl > (MAX)) \
402 /* Check hour field of time_str. */
403 colon
= strchr (time_str
, ':');
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
)
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')
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. */
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)
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
));
453 tm
.tm_year
= year
- 1900;
458 #undef GET_LONG_RANGE
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
)
471 time_start
= key_str2time (stime_str
, sday_str
, smonth_str
, syear_str
);
474 vty_out (vty
, "Malformed time value%s", VTY_NEWLINE
);
477 time_end
= key_str2time (etime_str
, eday_str
, emonth_str
, eyear_str
);
481 vty_out (vty
, "Malformed time value%s", VTY_NEWLINE
);
485 if (time_end
<= time_start
)
487 vty_out (vty
, "Expire time is not later than start time%s", VTY_NEWLINE
);
491 krange
->start
= time_start
;
492 krange
->end
= time_end
;
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
)
506 time_start
= key_str2time (stime_str
, sday_str
, smonth_str
, syear_str
);
509 vty_out (vty
, "Malformed time value%s", VTY_NEWLINE
);
512 krange
->start
= time_start
;
514 VTY_GET_INTEGER ("duration", duration
, duration_str
);
515 krange
->duration
= 1;
516 krange
->end
= time_start
+ duration
;
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
)
528 time_start
= key_str2time (stime_str
, sday_str
, smonth_str
, syear_str
);
531 vty_out (vty
, "Malformed time value%s", VTY_NEWLINE
);
534 krange
->start
= time_start
;
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"
546 "Day of th month to start\n"
547 "Month of the year to start\n"
550 "Day of th month to expire\n"
551 "Month of the year to expire\n"
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"
567 "Day of th month to start\n"
568 "Month of the year to start\n"
571 "Month of the year to expire\n"
572 "Day of th month to expire\n"
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"
588 "Month of the year to start\n"
589 "Day of th month to start\n"
592 "Day of th month to expire\n"
593 "Month of the year to expire\n"
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"
609 "Month of the year to start\n"
610 "Day of th month to start\n"
613 "Month of the year to expire\n"
614 "Day of th month to expire\n"
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"
630 "Day of th month to start\n"
631 "Month of the year to start\n"
637 key
= vty
->index_sub
;
639 return key_lifetime_infinite_set (vty
, &key
->accept
, argv
[0], argv
[1],
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"
648 "Month of the year to start\n"
649 "Day of th month to start\n"
655 key
= vty
->index_sub
;
657 return key_lifetime_infinite_set (vty
, &key
->accept
, argv
[0], argv
[2],
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"
666 "Day of th month to start\n"
667 "Month of the year to start\n"
669 "Duration of the key\n"
670 "Duration seconds\n")
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"
685 "Month of the year to start\n"
686 "Day of th month to start\n"
688 "Duration of the key\n"
689 "Duration seconds\n")
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"
704 "Day of th month to start\n"
705 "Month of the year to start\n"
708 "Day of th month to expire\n"
709 "Month of the year to expire\n"
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"
725 "Day of th month to start\n"
726 "Month of the year to start\n"
729 "Month of the year to expire\n"
730 "Day of th month to expire\n"
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"
746 "Month of the year to start\n"
747 "Day of th month to start\n"
750 "Day of th month to expire\n"
751 "Month of the year to expire\n"
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"
767 "Month of the year to start\n"
768 "Day of th month to start\n"
771 "Month of the year to expire\n"
772 "Day of th month to expire\n"
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"
788 "Day of th month to start\n"
789 "Month of the year to start\n"
795 key
= vty
->index_sub
;
797 return key_lifetime_infinite_set (vty
, &key
->send
, argv
[0], argv
[1], argv
[2],
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"
806 "Month of the year to start\n"
807 "Day of th month to start\n"
813 key
= vty
->index_sub
;
815 return key_lifetime_infinite_set (vty
, &key
->send
, argv
[0], argv
[2], argv
[1],
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"
824 "Day of th month to start\n"
825 "Month of the year to start\n"
827 "Duration of the key\n"
828 "Duration seconds\n")
832 key
= vty
->index_sub
;
834 return key_lifetime_duration_set (vty
, &key
->send
, argv
[0], argv
[1], argv
[2],
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"
843 "Month of the year to start\n"
844 "Day of th month to start\n"
846 "Duration of the key\n"
847 "Duration seconds\n")
851 key
= vty
->index_sub
;
853 return key_lifetime_duration_set (vty
, &key
->send
, argv
[0], argv
[2], argv
[1],
857 struct cmd_node keychain_node
=
860 "%s(config-keychain)# ",
864 struct cmd_node keychain_key_node
=
867 "%s(config-keychain-key)# ",
872 keychain_strftime (char *buf
, int bufsiz
, time_t *time
)
877 tm
= localtime (time
);
879 len
= strftime (buf
, bufsiz
, "%T %b %d %Y", tm
);
885 keychain_config_write (struct vty
*vty
)
887 struct keychain
*keychain
;
889 struct listnode
*node
;
890 struct listnode
*knode
;
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
);
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
));
916 keychain_strftime (buf
, BUFSIZ
, &key
->accept
.end
);
917 vty_out (vty
, " %s", buf
);
919 vty_out (vty
, "%s", VTY_NEWLINE
);
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
));
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
);
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
);