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