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