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