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