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
;
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 (const struct key
*k1
, const struct key
*k2
)
81 if (k1
->index
> k2
->index
)
83 if (k1
->index
< k2
->index
)
89 key_delete_func (struct key
*key
)
97 keychain_get (const char *name
)
99 struct keychain
*keychain
;
101 keychain
= keychain_lookup (name
);
106 keychain
= keychain_new ();
107 keychain
->name
= strdup (name
);
108 keychain
->key
= list_new ();
109 keychain
->key
->cmp
= (int (*)(void *, void *)) key_cmp_func
;
110 keychain
->key
->del
= (void (*)(void *)) key_delete_func
;
111 listnode_add (keychain_list
, keychain
);
117 keychain_delete (struct keychain
*keychain
)
120 free (keychain
->name
);
122 list_delete (keychain
->key
);
123 listnode_delete (keychain_list
, keychain
);
124 keychain_free (keychain
);
128 key_lookup (const struct keychain
*keychain
, u_int32_t index
)
130 struct listnode
*node
;
133 for (ALL_LIST_ELEMENTS_RO (keychain
->key
, node
, key
))
135 if (key
->index
== index
)
142 key_lookup_for_accept (const struct keychain
*keychain
, u_int32_t index
)
144 struct listnode
*node
;
150 for (ALL_LIST_ELEMENTS_RO (keychain
->key
, node
, key
))
152 if (key
->index
>= index
)
154 if (key
->accept
.start
== 0)
157 if (key
->accept
.start
<= now
)
158 if (key
->accept
.end
>= now
|| key
->accept
.end
== -1)
166 key_match_for_accept (const struct keychain
*keychain
, const char *auth_str
)
168 struct listnode
*node
;
174 for (ALL_LIST_ELEMENTS_RO (keychain
->key
, node
, key
))
176 if (key
->accept
.start
== 0 ||
177 (key
->accept
.start
<= now
&&
178 (key
->accept
.end
>= now
|| key
->accept
.end
== -1)))
179 if (strncmp (key
->string
, auth_str
, 16) == 0)
186 key_lookup_for_send (const struct keychain
*keychain
)
188 struct listnode
*node
;
194 for (ALL_LIST_ELEMENTS_RO (keychain
->key
, node
, key
))
196 if (key
->send
.start
== 0)
199 if (key
->send
.start
<= now
)
200 if (key
->send
.end
>= now
|| key
->send
.end
== -1)
207 key_get (const struct keychain
*keychain
, u_int32_t index
)
211 key
= key_lookup (keychain
, index
);
218 listnode_add_sort (keychain
->key
, key
);
224 key_delete (struct keychain
*keychain
, struct key
*key
)
226 listnode_delete (keychain
->key
, key
);
236 "Authentication key management\n"
237 "Key-chain management\n"
240 struct keychain
*keychain
;
242 keychain
= keychain_get (argv
[0]);
243 vty
->index
= keychain
;
244 vty
->node
= KEYCHAIN_NODE
;
253 "Authentication key management\n"
254 "Key-chain management\n"
257 struct keychain
*keychain
;
259 keychain
= keychain_lookup (argv
[0]);
263 vty_out (vty
, "Can't find keychain %s%s", argv
[0], VTY_NEWLINE
);
267 keychain_delete (keychain
);
274 "key <0-2147483647>",
276 "Key identifier number\n")
278 struct keychain
*keychain
;
282 keychain
= vty
->index
;
284 VTY_GET_INTEGER ("key identifier", index
, argv
[0]);
285 key
= key_get (keychain
, index
);
286 vty
->index_sub
= key
;
287 vty
->node
= KEYCHAIN_KEY_NODE
;
294 "no key <0-2147483647>",
297 "Key identifier number\n")
299 struct keychain
*keychain
;
303 keychain
= vty
->index
;
305 VTY_GET_INTEGER ("key identifier", index
, argv
[0]);
306 key
= key_lookup (keychain
, index
);
309 vty_out (vty
, "Can't find key %d%s", index
, VTY_NEWLINE
);
313 key_delete (keychain
, key
);
315 vty
->node
= KEYCHAIN_NODE
;
328 key
= vty
->index_sub
;
332 key
->string
= strdup (argv
[0]);
337 DEFUN (no_key_string
,
339 "no key-string [LINE]",
346 key
= vty
->index_sub
;
357 /* Convert HH:MM:SS MON DAY YEAR to time_t value. -1 is returned when
358 given string is malformed. */
360 key_str2time (const char *time_str
, const char *day_str
, const char *month_str
,
361 const char *year_str
)
367 unsigned int sec
, min
, hour
;
368 unsigned int day
, month
, year
;
371 const char *month_name
[] =
388 /* Check hour field of time_str. */
389 colon
= strchr (time_str
, ':');
394 /* Hour must be between 0 and 23. */
395 hour
= strtoul (time_str
, &endptr
, 10);
396 if (hour
== ULONG_MAX
|| *endptr
!= '\0' || hour
< 0 || hour
> 23)
399 /* Check min field of time_str. */
400 time_str
= colon
+ 1;
401 colon
= strchr (time_str
, ':');
402 if (*time_str
== '\0' || colon
== NULL
)
406 /* Min must be between 0 and 59. */
407 min
= strtoul (time_str
, &endptr
, 10);
408 if (min
== ULONG_MAX
|| *endptr
!= '\0' || min
< 0 || min
> 59)
411 /* Check sec field of time_str. */
412 time_str
= colon
+ 1;
413 if (*time_str
== '\0')
416 /* Sec must be between 0 and 59. */
417 sec
= strtoul (time_str
, &endptr
, 10);
418 if (sec
== ULONG_MAX
|| *endptr
!= '\0' || sec
< 0 || sec
> 59)
421 /* Check day_str. Day must be <1-31>. */
422 day
= strtoul (day_str
, &endptr
, 10);
423 if (day
== ULONG_MAX
|| *endptr
!= '\0' || day
< 0 || day
> 31)
426 /* Check month_str. Month must match month_name. */
428 if (strlen (month_str
) >= 3)
429 for (i
= 0; month_name
[i
]; i
++)
430 if (strncmp (month_str
, month_name
[i
], strlen (month_str
)) == 0)
438 /* Check year_str. Year must be <1993-2035>. */
439 year
= strtoul (year_str
, &endptr
, 10);
440 if (year
== ULONG_MAX
|| *endptr
!= '\0' || year
< 1993 || year
> 2035)
443 memset (&tm
, 0, sizeof (struct tm
));
449 tm
.tm_year
= year
- 1900;
457 key_lifetime_set (struct vty
*vty
, struct key_range
*krange
,
458 const char *stime_str
, const char *sday_str
,
459 const char *smonth_str
, const char *syear_str
,
460 const char *etime_str
, const char *eday_str
,
461 const char *emonth_str
, const char *eyear_str
)
466 time_start
= key_str2time (stime_str
, sday_str
, smonth_str
, syear_str
);
469 vty_out (vty
, "Malformed time value%s", VTY_NEWLINE
);
472 time_end
= key_str2time (etime_str
, eday_str
, emonth_str
, eyear_str
);
476 vty_out (vty
, "Malformed time value%s", VTY_NEWLINE
);
480 if (time_end
<= time_start
)
482 vty_out (vty
, "Expire time is not later than start time%s", VTY_NEWLINE
);
486 krange
->start
= time_start
;
487 krange
->end
= time_end
;
493 key_lifetime_duration_set (struct vty
*vty
, struct key_range
*krange
,
494 const char *stime_str
, const char *sday_str
,
495 const char *smonth_str
, const char *syear_str
,
496 const char *duration_str
)
501 time_start
= key_str2time (stime_str
, sday_str
, smonth_str
, syear_str
);
504 vty_out (vty
, "Malformed time value%s", VTY_NEWLINE
);
507 krange
->start
= time_start
;
509 VTY_GET_INTEGER ("duration", duration
, duration_str
);
510 krange
->duration
= 1;
511 krange
->end
= time_start
+ duration
;
517 key_lifetime_infinite_set (struct vty
*vty
, struct key_range
*krange
,
518 const char *stime_str
, const char *sday_str
,
519 const char *smonth_str
, const char *syear_str
)
523 time_start
= key_str2time (stime_str
, sday_str
, smonth_str
, syear_str
);
526 vty_out (vty
, "Malformed time value%s", VTY_NEWLINE
);
529 krange
->start
= time_start
;
536 DEFUN (accept_lifetime_day_month_day_month
,
537 accept_lifetime_day_month_day_month_cmd
,
538 "accept-lifetime HH:MM:SS <1-31> MONTH <1993-2035> HH:MM:SS <1-31> MONTH <1993-2035>",
539 "Set accept lifetime of the key\n"
541 "Day of th month to start\n"
542 "Month of the year to start\n"
545 "Day of th month to expire\n"
546 "Month of the year to expire\n"
551 key
= vty
->index_sub
;
553 return key_lifetime_set (vty
, &key
->accept
, argv
[0], argv
[1], argv
[2],
554 argv
[3], argv
[4], argv
[5], argv
[6], argv
[7]);
557 DEFUN (accept_lifetime_day_month_month_day
,
558 accept_lifetime_day_month_month_day_cmd
,
559 "accept-lifetime HH:MM:SS <1-31> MONTH <1993-2035> HH:MM:SS MONTH <1-31> <1993-2035>",
560 "Set accept lifetime of the key\n"
562 "Day of th month to start\n"
563 "Month of the year to start\n"
566 "Month of the year to expire\n"
567 "Day of th month to expire\n"
572 key
= vty
->index_sub
;
574 return key_lifetime_set (vty
, &key
->accept
, argv
[0], argv
[1], argv
[2],
575 argv
[3], argv
[4], argv
[6], argv
[5], argv
[7]);
578 DEFUN (accept_lifetime_month_day_day_month
,
579 accept_lifetime_month_day_day_month_cmd
,
580 "accept-lifetime HH:MM:SS MONTH <1-31> <1993-2035> HH:MM:SS <1-31> MONTH <1993-2035>",
581 "Set accept lifetime of the key\n"
583 "Month of the year to start\n"
584 "Day of th month to start\n"
587 "Day of th month to expire\n"
588 "Month of the year to expire\n"
593 key
= vty
->index_sub
;
595 return key_lifetime_set (vty
, &key
->accept
, argv
[0], argv
[2], argv
[1],
596 argv
[3], argv
[4], argv
[5], argv
[6], argv
[7]);
599 DEFUN (accept_lifetime_month_day_month_day
,
600 accept_lifetime_month_day_month_day_cmd
,
601 "accept-lifetime HH:MM:SS MONTH <1-31> <1993-2035> HH:MM:SS MONTH <1-31> <1993-2035>",
602 "Set accept lifetime of the key\n"
604 "Month of the year to start\n"
605 "Day of th month to start\n"
608 "Month of the year to expire\n"
609 "Day of th month to expire\n"
614 key
= vty
->index_sub
;
616 return key_lifetime_set (vty
, &key
->accept
, argv
[0], argv
[2], argv
[1],
617 argv
[3], argv
[4], argv
[6], argv
[5], argv
[7]);
620 DEFUN (accept_lifetime_infinite_day_month
,
621 accept_lifetime_infinite_day_month_cmd
,
622 "accept-lifetime HH:MM:SS <1-31> MONTH <1993-2035> infinite",
623 "Set accept lifetime of the key\n"
625 "Day of th month to start\n"
626 "Month of the year to start\n"
632 key
= vty
->index_sub
;
634 return key_lifetime_infinite_set (vty
, &key
->accept
, argv
[0], argv
[1],
638 DEFUN (accept_lifetime_infinite_month_day
,
639 accept_lifetime_infinite_month_day_cmd
,
640 "accept-lifetime HH:MM:SS MONTH <1-31> <1993-2035> infinite",
641 "Set accept lifetime of the key\n"
643 "Month of the year to start\n"
644 "Day of th month to start\n"
650 key
= vty
->index_sub
;
652 return key_lifetime_infinite_set (vty
, &key
->accept
, argv
[0], argv
[2],
656 DEFUN (accept_lifetime_duration_day_month
,
657 accept_lifetime_duration_day_month_cmd
,
658 "accept-lifetime HH:MM:SS <1-31> MONTH <1993-2035> duration <1-2147483646>",
659 "Set accept lifetime of the key\n"
661 "Day of th month to start\n"
662 "Month of the year to start\n"
664 "Duration of the key\n"
665 "Duration seconds\n")
669 key
= vty
->index_sub
;
671 return key_lifetime_duration_set (vty
, &key
->accept
, argv
[0], argv
[1],
672 argv
[2], argv
[3], argv
[4]);
675 DEFUN (accept_lifetime_duration_month_day
,
676 accept_lifetime_duration_month_day_cmd
,
677 "accept-lifetime HH:MM:SS MONTH <1-31> <1993-2035> duration <1-2147483646>",
678 "Set accept lifetime of the key\n"
680 "Month of the year to start\n"
681 "Day of th month to start\n"
683 "Duration of the key\n"
684 "Duration seconds\n")
688 key
= vty
->index_sub
;
690 return key_lifetime_duration_set (vty
, &key
->accept
, argv
[0], argv
[2],
691 argv
[1], argv
[3], argv
[4]);
694 DEFUN (send_lifetime_day_month_day_month
,
695 send_lifetime_day_month_day_month_cmd
,
696 "send-lifetime HH:MM:SS <1-31> MONTH <1993-2035> HH:MM:SS <1-31> MONTH <1993-2035>",
697 "Set send lifetime of the key\n"
699 "Day of th month to start\n"
700 "Month of the year to start\n"
703 "Day of th month to expire\n"
704 "Month of the year to expire\n"
709 key
= vty
->index_sub
;
711 return key_lifetime_set (vty
, &key
->send
, argv
[0], argv
[1], argv
[2], argv
[3],
712 argv
[4], argv
[5], argv
[6], argv
[7]);
715 DEFUN (send_lifetime_day_month_month_day
,
716 send_lifetime_day_month_month_day_cmd
,
717 "send-lifetime HH:MM:SS <1-31> MONTH <1993-2035> HH:MM:SS MONTH <1-31> <1993-2035>",
718 "Set send lifetime of the key\n"
720 "Day of th month to start\n"
721 "Month of the year to start\n"
724 "Month of the year to expire\n"
725 "Day of th month to expire\n"
730 key
= vty
->index_sub
;
732 return key_lifetime_set (vty
, &key
->send
, argv
[0], argv
[1], argv
[2], argv
[3],
733 argv
[4], argv
[6], argv
[5], argv
[7]);
736 DEFUN (send_lifetime_month_day_day_month
,
737 send_lifetime_month_day_day_month_cmd
,
738 "send-lifetime HH:MM:SS MONTH <1-31> <1993-2035> HH:MM:SS <1-31> MONTH <1993-2035>",
739 "Set send lifetime of the key\n"
741 "Month of the year to start\n"
742 "Day of th month to start\n"
745 "Day of th month to expire\n"
746 "Month of the year to expire\n"
751 key
= vty
->index_sub
;
753 return key_lifetime_set (vty
, &key
->send
, argv
[0], argv
[2], argv
[1], argv
[3],
754 argv
[4], argv
[5], argv
[6], argv
[7]);
757 DEFUN (send_lifetime_month_day_month_day
,
758 send_lifetime_month_day_month_day_cmd
,
759 "send-lifetime HH:MM:SS MONTH <1-31> <1993-2035> HH:MM:SS MONTH <1-31> <1993-2035>",
760 "Set send lifetime of the key\n"
762 "Month of the year to start\n"
763 "Day of th month to start\n"
766 "Month of the year to expire\n"
767 "Day of th month to expire\n"
772 key
= vty
->index_sub
;
774 return key_lifetime_set (vty
, &key
->send
, argv
[0], argv
[2], argv
[1], argv
[3],
775 argv
[4], argv
[6], argv
[5], argv
[7]);
778 DEFUN (send_lifetime_infinite_day_month
,
779 send_lifetime_infinite_day_month_cmd
,
780 "send-lifetime HH:MM:SS <1-31> MONTH <1993-2035> infinite",
781 "Set send lifetime of the key\n"
783 "Day of th month to start\n"
784 "Month of the year to start\n"
790 key
= vty
->index_sub
;
792 return key_lifetime_infinite_set (vty
, &key
->send
, argv
[0], argv
[1], argv
[2],
796 DEFUN (send_lifetime_infinite_month_day
,
797 send_lifetime_infinite_month_day_cmd
,
798 "send-lifetime HH:MM:SS MONTH <1-31> <1993-2035> infinite",
799 "Set send lifetime of the key\n"
801 "Month of the year to start\n"
802 "Day of th month to start\n"
808 key
= vty
->index_sub
;
810 return key_lifetime_infinite_set (vty
, &key
->send
, argv
[0], argv
[2], argv
[1],
814 DEFUN (send_lifetime_duration_day_month
,
815 send_lifetime_duration_day_month_cmd
,
816 "send-lifetime HH:MM:SS <1-31> MONTH <1993-2035> duration <1-2147483646>",
817 "Set send lifetime of the key\n"
819 "Day of th month to start\n"
820 "Month of the year to start\n"
822 "Duration of the key\n"
823 "Duration seconds\n")
827 key
= vty
->index_sub
;
829 return key_lifetime_duration_set (vty
, &key
->send
, argv
[0], argv
[1], argv
[2],
833 DEFUN (send_lifetime_duration_month_day
,
834 send_lifetime_duration_month_day_cmd
,
835 "send-lifetime HH:MM:SS MONTH <1-31> <1993-2035> duration <1-2147483646>",
836 "Set send lifetime of the key\n"
838 "Month of the year to start\n"
839 "Day of th month to start\n"
841 "Duration of the key\n"
842 "Duration seconds\n")
846 key
= vty
->index_sub
;
848 return key_lifetime_duration_set (vty
, &key
->send
, argv
[0], argv
[2], argv
[1],
852 struct cmd_node keychain_node
=
855 "%s(config-keychain)# ",
859 struct cmd_node keychain_key_node
=
862 "%s(config-keychain-key)# ",
867 keychain_strftime (char *buf
, int bufsiz
, time_t *time
)
872 tm
= localtime (time
);
874 len
= strftime (buf
, bufsiz
, "%T %b %d %Y", tm
);
880 keychain_config_write (struct vty
*vty
)
882 struct keychain
*keychain
;
884 struct listnode
*node
;
885 struct listnode
*knode
;
888 for (ALL_LIST_ELEMENTS_RO (keychain_list
, node
, keychain
))
890 vty_out (vty
, "key chain %s%s", keychain
->name
, VTY_NEWLINE
);
892 for (ALL_LIST_ELEMENTS_RO (keychain
->key
, knode
, key
))
894 vty_out (vty
, " key %d%s", key
->index
, VTY_NEWLINE
);
897 vty_out (vty
, " key-string %s%s", key
->string
, VTY_NEWLINE
);
899 if (key
->accept
.start
)
901 keychain_strftime (buf
, BUFSIZ
, &key
->accept
.start
);
902 vty_out (vty
, " accept-lifetime %s", buf
);
904 if (key
->accept
.end
== -1)
905 vty_out (vty
, " infinite");
906 else if (key
->accept
.duration
)
907 vty_out (vty
, " duration %ld",
908 (long)(key
->accept
.end
- key
->accept
.start
));
911 keychain_strftime (buf
, BUFSIZ
, &key
->accept
.end
);
912 vty_out (vty
, " %s", buf
);
914 vty_out (vty
, "%s", VTY_NEWLINE
);
919 keychain_strftime (buf
, BUFSIZ
, &key
->send
.start
);
920 vty_out (vty
, " send-lifetime %s", buf
);
922 if (key
->send
.end
== -1)
923 vty_out (vty
, " infinite");
924 else if (key
->send
.duration
)
925 vty_out (vty
, " duration %ld", (long)(key
->send
.end
- key
->send
.start
));
928 keychain_strftime (buf
, BUFSIZ
, &key
->send
.end
);
929 vty_out (vty
, " %s", buf
);
931 vty_out (vty
, "%s", VTY_NEWLINE
);
934 vty_out (vty
, "!%s", VTY_NEWLINE
);
943 keychain_list
= list_new ();
945 install_node (&keychain_node
, keychain_config_write
);
946 install_node (&keychain_key_node
, NULL
);
948 install_default (KEYCHAIN_NODE
);
949 install_default (KEYCHAIN_KEY_NODE
);
951 install_element (CONFIG_NODE
, &key_chain_cmd
);
952 install_element (CONFIG_NODE
, &no_key_chain_cmd
);
953 install_element (KEYCHAIN_NODE
, &key_cmd
);
954 install_element (KEYCHAIN_NODE
, &no_key_cmd
);
956 install_element (KEYCHAIN_NODE
, &key_chain_cmd
);
957 install_element (KEYCHAIN_NODE
, &no_key_chain_cmd
);
959 install_element (KEYCHAIN_KEY_NODE
, &key_string_cmd
);
960 install_element (KEYCHAIN_KEY_NODE
, &no_key_string_cmd
);
962 install_element (KEYCHAIN_KEY_NODE
, &key_chain_cmd
);
963 install_element (KEYCHAIN_KEY_NODE
, &no_key_chain_cmd
);
965 install_element (KEYCHAIN_KEY_NODE
, &key_cmd
);
966 install_element (KEYCHAIN_KEY_NODE
, &no_key_cmd
);
968 install_element (KEYCHAIN_KEY_NODE
, &accept_lifetime_day_month_day_month_cmd
);
969 install_element (KEYCHAIN_KEY_NODE
, &accept_lifetime_day_month_month_day_cmd
);
970 install_element (KEYCHAIN_KEY_NODE
, &accept_lifetime_month_day_day_month_cmd
);
971 install_element (KEYCHAIN_KEY_NODE
, &accept_lifetime_month_day_month_day_cmd
);
972 install_element (KEYCHAIN_KEY_NODE
, &accept_lifetime_infinite_day_month_cmd
);
973 install_element (KEYCHAIN_KEY_NODE
, &accept_lifetime_infinite_month_day_cmd
);
974 install_element (KEYCHAIN_KEY_NODE
, &accept_lifetime_duration_day_month_cmd
);
975 install_element (KEYCHAIN_KEY_NODE
, &accept_lifetime_duration_month_day_cmd
);
977 install_element (KEYCHAIN_KEY_NODE
, &send_lifetime_day_month_day_month_cmd
);
978 install_element (KEYCHAIN_KEY_NODE
, &send_lifetime_day_month_month_day_cmd
);
979 install_element (KEYCHAIN_KEY_NODE
, &send_lifetime_month_day_day_month_cmd
);
980 install_element (KEYCHAIN_KEY_NODE
, &send_lifetime_month_day_month_day_cmd
);
981 install_element (KEYCHAIN_KEY_NODE
, &send_lifetime_infinite_day_month_cmd
);
982 install_element (KEYCHAIN_KEY_NODE
, &send_lifetime_infinite_month_day_cmd
);
983 install_element (KEYCHAIN_KEY_NODE
, &send_lifetime_duration_day_month_cmd
);
984 install_element (KEYCHAIN_KEY_NODE
, &send_lifetime_duration_month_day_cmd
);