]> git.proxmox.com Git - mirror_frr.git/blame - lib/keychain.c
mpls: add support for LDP LSPs
[mirror_frr.git] / lib / keychain.c
CommitLineData
718e3744 1/* key-chain for authentication.
2 Copyright (C) 2000 Kunihiro Ishiguro
3
4This file is part of GNU Zebra.
5
6GNU Zebra is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published
8by the Free Software Foundation; either version 2, or (at your
9option) any later version.
10
11GNU Zebra is distributed in the hope that it will be useful, but
12WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU Zebra; see the file COPYING. If not, write to the
18Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19Boston, 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
4a1ab8e4
DL
28DEFINE_MTYPE_STATIC(LIB, KEY, "Key")
29DEFINE_MTYPE_STATIC(LIB, KEYCHAIN, "Key chain")
30
718e3744 31/* Master list of key chain. */
32struct list *keychain_list;
33
8cc4198f 34static struct keychain *
35keychain_new (void)
718e3744 36{
393deb9b 37 return XCALLOC (MTYPE_KEYCHAIN, sizeof (struct keychain));
718e3744 38}
39
8cc4198f 40static void
718e3744 41keychain_free (struct keychain *keychain)
42{
43 XFREE (MTYPE_KEYCHAIN, keychain);
44}
45
8cc4198f 46static struct key *
47key_new (void)
718e3744 48{
393deb9b 49 return XCALLOC (MTYPE_KEY, sizeof (struct key));
718e3744 50}
51
8cc4198f 52static void
718e3744 53key_free (struct key *key)
54{
55 XFREE (MTYPE_KEY, key);
56}
57
58struct keychain *
8c328f11 59keychain_lookup (const char *name)
718e3744 60{
1eb8ef25 61 struct listnode *node;
718e3744 62 struct keychain *keychain;
63
64 if (name == NULL)
65 return NULL;
66
1eb8ef25 67 for (ALL_LIST_ELEMENTS_RO (keychain_list, node, keychain))
718e3744 68 {
69 if (strcmp (keychain->name, name) == 0)
70 return keychain;
71 }
72 return NULL;
73}
74
8cc4198f 75static int
76key_cmp_func (void *arg1, void *arg2)
718e3744 77{
8cc4198f 78 const struct key *k1 = arg1;
79 const struct key *k2 = arg2;
80
718e3744 81 if (k1->index > k2->index)
82 return 1;
83 if (k1->index < k2->index)
84 return -1;
85 return 0;
86}
87
8cc4198f 88static void
718e3744 89key_delete_func (struct key *key)
90{
91 if (key->string)
92 free (key->string);
93 key_free (key);
94}
95
8cc4198f 96static struct keychain *
8c328f11 97keychain_get (const char *name)
718e3744 98{
99 struct keychain *keychain;
100
101 keychain = keychain_lookup (name);
102
103 if (keychain)
104 return keychain;
105
106 keychain = keychain_new ();
6e919709 107 keychain->name = XSTRDUP(MTYPE_KEYCHAIN, name);
718e3744 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
8cc4198f 116static void
718e3744 117keychain_delete (struct keychain *keychain)
118{
119 if (keychain->name)
6e919709 120 XFREE(MTYPE_KEYCHAIN, keychain->name);
718e3744 121
122 list_delete (keychain->key);
123 listnode_delete (keychain_list, keychain);
124 keychain_free (keychain);
125}
126
8cc4198f 127static struct key *
8c328f11 128key_lookup (const struct keychain *keychain, u_int32_t index)
718e3744 129{
1eb8ef25 130 struct listnode *node;
718e3744 131 struct key *key;
132
1eb8ef25 133 for (ALL_LIST_ELEMENTS_RO (keychain->key, node, key))
718e3744 134 {
135 if (key->index == index)
136 return key;
137 }
138 return NULL;
139}
140
141struct key *
8c328f11 142key_lookup_for_accept (const struct keychain *keychain, u_int32_t index)
718e3744 143{
1eb8ef25 144 struct listnode *node;
718e3744 145 struct key *key;
146 time_t now;
147
148 now = time (NULL);
149
1eb8ef25 150 for (ALL_LIST_ELEMENTS_RO (keychain->key, node, key))
718e3744 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
165struct key *
8c328f11 166key_match_for_accept (const struct keychain *keychain, const char *auth_str)
718e3744 167{
1eb8ef25 168 struct listnode *node;
718e3744 169 struct key *key;
170 time_t now;
171
172 now = time (NULL);
173
1eb8ef25 174 for (ALL_LIST_ELEMENTS_RO (keychain->key, node, key))
718e3744 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
185struct key *
8c328f11 186key_lookup_for_send (const struct keychain *keychain)
718e3744 187{
1eb8ef25 188 struct listnode *node;
718e3744 189 struct key *key;
190 time_t now;
191
192 now = time (NULL);
193
1eb8ef25 194 for (ALL_LIST_ELEMENTS_RO (keychain->key, node, key))
718e3744 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
8cc4198f 206static struct key *
8c328f11 207key_get (const struct keychain *keychain, u_int32_t index)
718e3744 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
8cc4198f 223static void
718e3744 224key_delete (struct keychain *keychain, struct key *key)
225{
226 listnode_delete (keychain->key, key);
227
228 if (key->string)
6e919709 229 XFREE(MTYPE_KEY, key->string);
718e3744 230 key_free (key);
231}
6b0655a2 232
718e3744 233DEFUN (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[0]);
243 vty->index = keychain;
244 vty->node = KEYCHAIN_NODE;
245
246 return CMD_SUCCESS;
247}
248
249DEFUN (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[0]);
260
261 if (! keychain)
262 {
263 vty_out (vty, "Can't find keychain %s%s", argv[0], VTY_NEWLINE);
264 return CMD_WARNING;
265 }
266
267 keychain_delete (keychain);
268
269 return CMD_SUCCESS;
270}
271
272DEFUN (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;
718e3744 281
282 keychain = vty->index;
283
66cbbceb 284 VTY_GET_INTEGER ("key identifier", index, argv[0]);
718e3744 285 key = key_get (keychain, index);
286 vty->index_sub = key;
287 vty->node = KEYCHAIN_KEY_NODE;
288
289 return CMD_SUCCESS;
290}
291
292DEFUN (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;
718e3744 302
303 keychain = vty->index;
304
66cbbceb 305 VTY_GET_INTEGER ("key identifier", index, argv[0]);
718e3744 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
320DEFUN (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)
6e919709
DS
331 XFREE(MTYPE_KEY, key->string);
332 key->string = XSTRDUP(MTYPE_KEY, argv[0]);
718e3744 333
334 return CMD_SUCCESS;
335}
336
337DEFUN (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 {
6e919709 350 XFREE(MTYPE_KEY, key->string);
718e3744 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. */
8cc4198f 359static time_t
8c328f11 360key_str2time (const char *time_str, const char *day_str, const char *month_str,
361 const char *year_str)
718e3744 362{
363 int i = 0;
364 char *colon;
365 struct tm tm;
366 time_t time;
8c328f11 367 unsigned int sec, min, hour;
368 unsigned int day, month, year;
718e3744 369
8c328f11 370 const char *month_name[] =
718e3744 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
5eafab76 387#define _GET_LONG_RANGE(V,STR,MMCOND) \
8cc4198f 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; \
5eafab76 394 if (MMCOND) \
8cc4198f 395 return -1; \
396 (V) = tmpl; \
397}
5eafab76
DL
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
718e3744 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. */
5eafab76 410 GET_LONG_RANGE0 (hour, time_str, 23);
718e3744 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. */
5eafab76 420 GET_LONG_RANGE0 (min, time_str, 59);
718e3744 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. */
5eafab76 428 GET_LONG_RANGE0 (sec, time_str, 59);
718e3744 429
430 /* Check day_str. Day must be <1-31>. */
8cc4198f 431 GET_LONG_RANGE (day, day_str, 1, 31);
718e3744 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>. */
8cc4198f 446 GET_LONG_RANGE (year, year_str, 1993, 2035);
718e3744 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);
66cbbceb 457
718e3744 458 return time;
8cc4198f 459#undef GET_LONG_RANGE
718e3744 460}
461
8cc4198f 462static int
8c328f11 463key_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)
718e3744 468{
469 time_t time_start;
470 time_t time_end;
66cbbceb 471
718e3744 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
8cc4198f 498static int
718e3744 499key_lifetime_duration_set (struct vty *vty, struct key_range *krange,
8c328f11 500 const char *stime_str, const char *sday_str,
501 const char *smonth_str, const char *syear_str,
502 const char *duration_str)
718e3744 503{
504 time_t time_start;
505 u_int32_t duration;
718e3744 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
66cbbceb 515 VTY_GET_INTEGER ("duration", duration, duration_str);
718e3744 516 krange->duration = 1;
517 krange->end = time_start + duration;
518
519 return CMD_SUCCESS;
520}
521
8cc4198f 522static int
718e3744 523key_lifetime_infinite_set (struct vty *vty, struct key_range *krange,
8c328f11 524 const char *stime_str, const char *sday_str,
525 const char *smonth_str, const char *syear_str)
718e3744 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}
6b0655a2 541
718e3744 542DEFUN (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[0], argv[1], argv[2],
560 argv[3], argv[4], argv[5], argv[6], argv[7]);
561}
562
563DEFUN (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[0], argv[1], argv[2],
581 argv[3], argv[4], argv[6], argv[5], argv[7]);
582}
583
584DEFUN (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[0], argv[2], argv[1],
602 argv[3], argv[4], argv[5], argv[6], argv[7]);
603}
604
605DEFUN (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[0], argv[2], argv[1],
623 argv[3], argv[4], argv[6], argv[5], argv[7]);
624}
625
626DEFUN (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[0], argv[1],
641 argv[2], argv[3]);
642}
643
644DEFUN (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[0], argv[2],
659 argv[1], argv[3]);
660}
661
662DEFUN (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[0], argv[1],
678 argv[2], argv[3], argv[4]);
679}
680
681DEFUN (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[0], argv[2],
697 argv[1], argv[3], argv[4]);
698}
6b0655a2 699
718e3744 700DEFUN (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[0], argv[1], argv[2], argv[3],
718 argv[4], argv[5], argv[6], argv[7]);
719}
720
721DEFUN (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[0], argv[1], argv[2], argv[3],
739 argv[4], argv[6], argv[5], argv[7]);
740}
741
742DEFUN (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[0], argv[2], argv[1], argv[3],
760 argv[4], argv[5], argv[6], argv[7]);
761}
762
763DEFUN (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[0], argv[2], argv[1], argv[3],
781 argv[4], argv[6], argv[5], argv[7]);
782}
783
784DEFUN (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[0], argv[1], argv[2],
799 argv[3]);
800}
801
802DEFUN (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[0], argv[2], argv[1],
817 argv[3]);
818}
819
820DEFUN (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[0], argv[1], argv[2],
836 argv[3], argv[4]);
837}
838
839DEFUN (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[0], argv[2], argv[1],
855 argv[3], argv[4]);
856}
6b0655a2 857
7fc626de 858static struct cmd_node keychain_node =
718e3744 859{
860 KEYCHAIN_NODE,
861 "%s(config-keychain)# ",
862 1
863};
864
7fc626de 865static struct cmd_node keychain_key_node =
718e3744 866{
867 KEYCHAIN_KEY_NODE,
868 "%s(config-keychain-key)# ",
869 1
870};
871
8cc4198f 872static int
718e3744 873keychain_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
8cc4198f 885static int
718e3744 886keychain_config_write (struct vty *vty)
887{
888 struct keychain *keychain;
889 struct key *key;
1eb8ef25 890 struct listnode *node;
891 struct listnode *knode;
718e3744 892 char buf[BUFSIZ];
893
1eb8ef25 894 for (ALL_LIST_ELEMENTS_RO (keychain_list, node, keychain))
718e3744 895 {
896 vty_out (vty, "key chain %s%s", keychain->name, VTY_NEWLINE);
897
1eb8ef25 898 for (ALL_LIST_ELEMENTS_RO (keychain->key, knode, key))
718e3744 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",
fa2b17e3 914 (long)(key->accept.end - key->accept.start));
718e3744 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)
fa2b17e3 931 vty_out (vty, " duration %ld", (long)(key->send.end - key->send.start));
718e3744 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
946void
947keychain_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}