keychain.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984
  1. /* key-chain for authentication.
  2. Copyright (C) 2000 Kunihiro Ishiguro
  3. This file is part of GNU Zebra.
  4. GNU Zebra is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published
  6. by the Free Software Foundation; either version 2, or (at your
  7. option) any later version.
  8. GNU Zebra is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Zebra; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  15. Boston, MA 02111-1307, USA. */
  16. #include <zebra.h>
  17. #include "command.h"
  18. #include "memory.h"
  19. #include "linklist.h"
  20. #include "keychain.h"
  21. /* Master list of key chain. */
  22. struct list *keychain_list;
  23. static struct keychain *
  24. keychain_new (void)
  25. {
  26. return XCALLOC (MTYPE_KEYCHAIN, sizeof (struct keychain));
  27. }
  28. static void
  29. keychain_free (struct keychain *keychain)
  30. {
  31. XFREE (MTYPE_KEYCHAIN, keychain);
  32. }
  33. static struct key *
  34. key_new (void)
  35. {
  36. return XCALLOC (MTYPE_KEY, sizeof (struct key));
  37. }
  38. static void
  39. key_free (struct key *key)
  40. {
  41. XFREE (MTYPE_KEY, key);
  42. }
  43. struct keychain *
  44. keychain_lookup (const char *name)
  45. {
  46. struct listnode *node;
  47. struct keychain *keychain;
  48. if (name == NULL)
  49. return NULL;
  50. for (ALL_LIST_ELEMENTS_RO (keychain_list, node, keychain))
  51. {
  52. if (strcmp (keychain->name, name) == 0)
  53. return keychain;
  54. }
  55. return NULL;
  56. }
  57. static int
  58. key_cmp_func (void *arg1, void *arg2)
  59. {
  60. const struct key *k1 = arg1;
  61. const struct key *k2 = arg2;
  62. if (k1->index > k2->index)
  63. return 1;
  64. if (k1->index < k2->index)
  65. return -1;
  66. return 0;
  67. }
  68. static void
  69. key_delete_func (struct key *key)
  70. {
  71. if (key->string)
  72. free (key->string);
  73. key_free (key);
  74. }
  75. static struct keychain *
  76. keychain_get (const char *name)
  77. {
  78. struct keychain *keychain;
  79. keychain = keychain_lookup (name);
  80. if (keychain)
  81. return keychain;
  82. keychain = keychain_new ();
  83. keychain->name = strdup (name);
  84. keychain->key = list_new ();
  85. keychain->key->cmp = (int (*)(void *, void *)) key_cmp_func;
  86. keychain->key->del = (void (*)(void *)) key_delete_func;
  87. listnode_add (keychain_list, keychain);
  88. return keychain;
  89. }
  90. static void
  91. keychain_delete (struct keychain *keychain)
  92. {
  93. if (keychain->name)
  94. free (keychain->name);
  95. list_delete (keychain->key);
  96. listnode_delete (keychain_list, keychain);
  97. keychain_free (keychain);
  98. }
  99. static struct key *
  100. key_lookup (const struct keychain *keychain, u_int32_t index)
  101. {
  102. struct listnode *node;
  103. struct key *key;
  104. for (ALL_LIST_ELEMENTS_RO (keychain->key, node, key))
  105. {
  106. if (key->index == index)
  107. return key;
  108. }
  109. return NULL;
  110. }
  111. struct key *
  112. key_lookup_for_accept (const struct keychain *keychain, u_int32_t index)
  113. {
  114. struct listnode *node;
  115. struct key *key;
  116. time_t now;
  117. now = time (NULL);
  118. for (ALL_LIST_ELEMENTS_RO (keychain->key, node, key))
  119. {
  120. if (key->index >= index)
  121. {
  122. if (key->accept.start == 0)
  123. return key;
  124. if (key->accept.start <= now)
  125. if (key->accept.end >= now || key->accept.end == -1)
  126. return key;
  127. }
  128. }
  129. return NULL;
  130. }
  131. struct key *
  132. key_match_for_accept (const struct keychain *keychain, const char *auth_str)
  133. {
  134. struct listnode *node;
  135. struct key *key;
  136. time_t now;
  137. now = time (NULL);
  138. for (ALL_LIST_ELEMENTS_RO (keychain->key, node, key))
  139. {
  140. if (key->accept.start == 0 ||
  141. (key->accept.start <= now &&
  142. (key->accept.end >= now || key->accept.end == -1)))
  143. if (strncmp (key->string, auth_str, 16) == 0)
  144. return key;
  145. }
  146. return NULL;
  147. }
  148. struct key *
  149. key_lookup_for_send (const struct keychain *keychain)
  150. {
  151. struct listnode *node;
  152. struct key *key;
  153. time_t now;
  154. now = time (NULL);
  155. for (ALL_LIST_ELEMENTS_RO (keychain->key, node, key))
  156. {
  157. if (key->send.start == 0)
  158. return key;
  159. if (key->send.start <= now)
  160. if (key->send.end >= now || key->send.end == -1)
  161. return key;
  162. }
  163. return NULL;
  164. }
  165. static struct key *
  166. key_get (const struct keychain *keychain, u_int32_t index)
  167. {
  168. struct key *key;
  169. key = key_lookup (keychain, index);
  170. if (key)
  171. return key;
  172. key = key_new ();
  173. key->index = index;
  174. listnode_add_sort (keychain->key, key);
  175. return key;
  176. }
  177. static void
  178. key_delete (struct keychain *keychain, struct key *key)
  179. {
  180. listnode_delete (keychain->key, key);
  181. if (key->string)
  182. free (key->string);
  183. key_free (key);
  184. }
  185. DEFUN (key_chain,
  186. key_chain_cmd,
  187. "key chain WORD",
  188. "Authentication key management\n"
  189. "Key-chain management\n"
  190. "Key-chain name\n")
  191. {
  192. struct keychain *keychain;
  193. keychain = keychain_get (argv[0]);
  194. vty->index = keychain;
  195. vty->node = KEYCHAIN_NODE;
  196. return CMD_SUCCESS;
  197. }
  198. DEFUN (no_key_chain,
  199. no_key_chain_cmd,
  200. "no key chain WORD",
  201. NO_STR
  202. "Authentication key management\n"
  203. "Key-chain management\n"
  204. "Key-chain name\n")
  205. {
  206. struct keychain *keychain;
  207. keychain = keychain_lookup (argv[0]);
  208. if (! keychain)
  209. {
  210. vty_out (vty, "Can't find keychain %s%s", argv[0], VTY_NEWLINE);
  211. return CMD_WARNING;
  212. }
  213. keychain_delete (keychain);
  214. return CMD_SUCCESS;
  215. }
  216. DEFUN (key,
  217. key_cmd,
  218. "key <0-2147483647>",
  219. "Configure a key\n"
  220. "Key identifier number\n")
  221. {
  222. struct keychain *keychain;
  223. struct key *key;
  224. u_int32_t index;
  225. keychain = vty->index;
  226. VTY_GET_INTEGER ("key identifier", index, argv[0]);
  227. key = key_get (keychain, index);
  228. vty->index_sub = key;
  229. vty->node = KEYCHAIN_KEY_NODE;
  230. return CMD_SUCCESS;
  231. }
  232. DEFUN (no_key,
  233. no_key_cmd,
  234. "no key <0-2147483647>",
  235. NO_STR
  236. "Delete a key\n"
  237. "Key identifier number\n")
  238. {
  239. struct keychain *keychain;
  240. struct key *key;
  241. u_int32_t index;
  242. keychain = vty->index;
  243. VTY_GET_INTEGER ("key identifier", index, argv[0]);
  244. key = key_lookup (keychain, index);
  245. if (! key)
  246. {
  247. vty_out (vty, "Can't find key %d%s", index, VTY_NEWLINE);
  248. return CMD_WARNING;
  249. }
  250. key_delete (keychain, key);
  251. vty->node = KEYCHAIN_NODE;
  252. return CMD_SUCCESS;
  253. }
  254. DEFUN (key_string,
  255. key_string_cmd,
  256. "key-string LINE",
  257. "Set key string\n"
  258. "The key\n")
  259. {
  260. struct key *key;
  261. key = vty->index_sub;
  262. if (key->string)
  263. free (key->string);
  264. key->string = strdup (argv[0]);
  265. return CMD_SUCCESS;
  266. }
  267. DEFUN (no_key_string,
  268. no_key_string_cmd,
  269. "no key-string [LINE]",
  270. NO_STR
  271. "Unset key string\n"
  272. "The key\n")
  273. {
  274. struct key *key;
  275. key = vty->index_sub;
  276. if (key->string)
  277. {
  278. free (key->string);
  279. key->string = NULL;
  280. }
  281. return CMD_SUCCESS;
  282. }
  283. /* Convert HH:MM:SS MON DAY YEAR to time_t value. -1 is returned when
  284. given string is malformed. */
  285. static time_t
  286. key_str2time (const char *time_str, const char *day_str, const char *month_str,
  287. const char *year_str)
  288. {
  289. int i = 0;
  290. char *colon;
  291. struct tm tm;
  292. time_t time;
  293. unsigned int sec, min, hour;
  294. unsigned int day, month, year;
  295. const char *month_name[] =
  296. {
  297. "January",
  298. "February",
  299. "March",
  300. "April",
  301. "May",
  302. "June",
  303. "July",
  304. "August",
  305. "September",
  306. "October",
  307. "November",
  308. "December",
  309. NULL
  310. };
  311. #define GET_LONG_RANGE(V,STR,MIN,MAX) \
  312. { \
  313. unsigned long tmpl; \
  314. char *endptr = NULL; \
  315. tmpl = strtoul ((STR), &endptr, 10); \
  316. if (*endptr != '\0' || tmpl == ULONG_MAX) \
  317. return -1; \
  318. if ( tmpl < (MIN) || tmpl > (MAX)) \
  319. return -1; \
  320. (V) = tmpl; \
  321. }
  322. /* Check hour field of time_str. */
  323. colon = strchr (time_str, ':');
  324. if (colon == NULL)
  325. return -1;
  326. *colon = '\0';
  327. /* Hour must be between 0 and 23. */
  328. GET_LONG_RANGE (hour, time_str, 0, 23);
  329. /* Check min field of time_str. */
  330. time_str = colon + 1;
  331. colon = strchr (time_str, ':');
  332. if (*time_str == '\0' || colon == NULL)
  333. return -1;
  334. *colon = '\0';
  335. /* Min must be between 0 and 59. */
  336. GET_LONG_RANGE (min, time_str, 0, 59);
  337. /* Check sec field of time_str. */
  338. time_str = colon + 1;
  339. if (*time_str == '\0')
  340. return -1;
  341. /* Sec must be between 0 and 59. */
  342. GET_LONG_RANGE (sec, time_str, 0, 59);
  343. /* Check day_str. Day must be <1-31>. */
  344. GET_LONG_RANGE (day, day_str, 1, 31);
  345. /* Check month_str. Month must match month_name. */
  346. month = 0;
  347. if (strlen (month_str) >= 3)
  348. for (i = 0; month_name[i]; i++)
  349. if (strncmp (month_str, month_name[i], strlen (month_str)) == 0)
  350. {
  351. month = i;
  352. break;
  353. }
  354. if (! month_name[i])
  355. return -1;
  356. /* Check year_str. Year must be <1993-2035>. */
  357. GET_LONG_RANGE (year, year_str, 1993, 2035);
  358. memset (&tm, 0, sizeof (struct tm));
  359. tm.tm_sec = sec;
  360. tm.tm_min = min;
  361. tm.tm_hour = hour;
  362. tm.tm_mon = month;
  363. tm.tm_mday = day;
  364. tm.tm_year = year - 1900;
  365. time = mktime (&tm);
  366. return time;
  367. #undef GET_LONG_RANGE
  368. }
  369. static int
  370. key_lifetime_set (struct vty *vty, struct key_range *krange,
  371. const char *stime_str, const char *sday_str,
  372. const char *smonth_str, const char *syear_str,
  373. const char *etime_str, const char *eday_str,
  374. const char *emonth_str, const char *eyear_str)
  375. {
  376. time_t time_start;
  377. time_t time_end;
  378. time_start = key_str2time (stime_str, sday_str, smonth_str, syear_str);
  379. if (time_start < 0)
  380. {
  381. vty_out (vty, "Malformed time value%s", VTY_NEWLINE);
  382. return CMD_WARNING;
  383. }
  384. time_end = key_str2time (etime_str, eday_str, emonth_str, eyear_str);
  385. if (time_end < 0)
  386. {
  387. vty_out (vty, "Malformed time value%s", VTY_NEWLINE);
  388. return CMD_WARNING;
  389. }
  390. if (time_end <= time_start)
  391. {
  392. vty_out (vty, "Expire time is not later than start time%s", VTY_NEWLINE);
  393. return CMD_WARNING;
  394. }
  395. krange->start = time_start;
  396. krange->end = time_end;
  397. return CMD_SUCCESS;
  398. }
  399. static int
  400. key_lifetime_duration_set (struct vty *vty, struct key_range *krange,
  401. const char *stime_str, const char *sday_str,
  402. const char *smonth_str, const char *syear_str,
  403. const char *duration_str)
  404. {
  405. time_t time_start;
  406. u_int32_t duration;
  407. time_start = key_str2time (stime_str, sday_str, smonth_str, syear_str);
  408. if (time_start < 0)
  409. {
  410. vty_out (vty, "Malformed time value%s", VTY_NEWLINE);
  411. return CMD_WARNING;
  412. }
  413. krange->start = time_start;
  414. VTY_GET_INTEGER ("duration", duration, duration_str);
  415. krange->duration = 1;
  416. krange->end = time_start + duration;
  417. return CMD_SUCCESS;
  418. }
  419. static int
  420. key_lifetime_infinite_set (struct vty *vty, struct key_range *krange,
  421. const char *stime_str, const char *sday_str,
  422. const char *smonth_str, const char *syear_str)
  423. {
  424. time_t time_start;
  425. time_start = key_str2time (stime_str, sday_str, smonth_str, syear_str);
  426. if (time_start < 0)
  427. {
  428. vty_out (vty, "Malformed time value%s", VTY_NEWLINE);
  429. return CMD_WARNING;
  430. }
  431. krange->start = time_start;
  432. krange->end = -1;
  433. return CMD_SUCCESS;
  434. }
  435. DEFUN (accept_lifetime_day_month_day_month,
  436. accept_lifetime_day_month_day_month_cmd,
  437. "accept-lifetime HH:MM:SS <1-31> MONTH <1993-2035> HH:MM:SS <1-31> MONTH <1993-2035>",
  438. "Set accept lifetime of the key\n"
  439. "Time to start\n"
  440. "Day of th month to start\n"
  441. "Month of the year to start\n"
  442. "Year to start\n"
  443. "Time to expire\n"
  444. "Day of th month to expire\n"
  445. "Month of the year to expire\n"
  446. "Year to expire\n")
  447. {
  448. struct key *key;
  449. key = vty->index_sub;
  450. return key_lifetime_set (vty, &key->accept, argv[0], argv[1], argv[2],
  451. argv[3], argv[4], argv[5], argv[6], argv[7]);
  452. }
  453. DEFUN (accept_lifetime_day_month_month_day,
  454. accept_lifetime_day_month_month_day_cmd,
  455. "accept-lifetime HH:MM:SS <1-31> MONTH <1993-2035> HH:MM:SS MONTH <1-31> <1993-2035>",
  456. "Set accept lifetime of the key\n"
  457. "Time to start\n"
  458. "Day of th month to start\n"
  459. "Month of the year to start\n"
  460. "Year to start\n"
  461. "Time to expire\n"
  462. "Month of the year to expire\n"
  463. "Day of th month to expire\n"
  464. "Year to expire\n")
  465. {
  466. struct key *key;
  467. key = vty->index_sub;
  468. return key_lifetime_set (vty, &key->accept, argv[0], argv[1], argv[2],
  469. argv[3], argv[4], argv[6], argv[5], argv[7]);
  470. }
  471. DEFUN (accept_lifetime_month_day_day_month,
  472. accept_lifetime_month_day_day_month_cmd,
  473. "accept-lifetime HH:MM:SS MONTH <1-31> <1993-2035> HH:MM:SS <1-31> MONTH <1993-2035>",
  474. "Set accept lifetime of the key\n"
  475. "Time to start\n"
  476. "Month of the year to start\n"
  477. "Day of th month to start\n"
  478. "Year to start\n"
  479. "Time to expire\n"
  480. "Day of th month to expire\n"
  481. "Month of the year to expire\n"
  482. "Year to expire\n")
  483. {
  484. struct key *key;
  485. key = vty->index_sub;
  486. return key_lifetime_set (vty, &key->accept, argv[0], argv[2], argv[1],
  487. argv[3], argv[4], argv[5], argv[6], argv[7]);
  488. }
  489. DEFUN (accept_lifetime_month_day_month_day,
  490. accept_lifetime_month_day_month_day_cmd,
  491. "accept-lifetime HH:MM:SS MONTH <1-31> <1993-2035> HH:MM:SS MONTH <1-31> <1993-2035>",
  492. "Set accept lifetime of the key\n"
  493. "Time to start\n"
  494. "Month of the year to start\n"
  495. "Day of th month to start\n"
  496. "Year to start\n"
  497. "Time to expire\n"
  498. "Month of the year to expire\n"
  499. "Day of th month to expire\n"
  500. "Year to expire\n")
  501. {
  502. struct key *key;
  503. key = vty->index_sub;
  504. return key_lifetime_set (vty, &key->accept, argv[0], argv[2], argv[1],
  505. argv[3], argv[4], argv[6], argv[5], argv[7]);
  506. }
  507. DEFUN (accept_lifetime_infinite_day_month,
  508. accept_lifetime_infinite_day_month_cmd,
  509. "accept-lifetime HH:MM:SS <1-31> MONTH <1993-2035> infinite",
  510. "Set accept lifetime of the key\n"
  511. "Time to start\n"
  512. "Day of th month to start\n"
  513. "Month of the year to start\n"
  514. "Year to start\n"
  515. "Never expires")
  516. {
  517. struct key *key;
  518. key = vty->index_sub;
  519. return key_lifetime_infinite_set (vty, &key->accept, argv[0], argv[1],
  520. argv[2], argv[3]);
  521. }
  522. DEFUN (accept_lifetime_infinite_month_day,
  523. accept_lifetime_infinite_month_day_cmd,
  524. "accept-lifetime HH:MM:SS MONTH <1-31> <1993-2035> infinite",
  525. "Set accept lifetime of the key\n"
  526. "Time to start\n"
  527. "Month of the year to start\n"
  528. "Day of th month to start\n"
  529. "Year to start\n"
  530. "Never expires")
  531. {
  532. struct key *key;
  533. key = vty->index_sub;
  534. return key_lifetime_infinite_set (vty, &key->accept, argv[0], argv[2],
  535. argv[1], argv[3]);
  536. }
  537. DEFUN (accept_lifetime_duration_day_month,
  538. accept_lifetime_duration_day_month_cmd,
  539. "accept-lifetime HH:MM:SS <1-31> MONTH <1993-2035> duration <1-2147483646>",
  540. "Set accept lifetime of the key\n"
  541. "Time to start\n"
  542. "Day of th month to start\n"
  543. "Month of the year to start\n"
  544. "Year to start\n"
  545. "Duration of the key\n"
  546. "Duration seconds\n")
  547. {
  548. struct key *key;
  549. key = vty->index_sub;
  550. return key_lifetime_duration_set (vty, &key->accept, argv[0], argv[1],
  551. argv[2], argv[3], argv[4]);
  552. }
  553. DEFUN (accept_lifetime_duration_month_day,
  554. accept_lifetime_duration_month_day_cmd,
  555. "accept-lifetime HH:MM:SS MONTH <1-31> <1993-2035> duration <1-2147483646>",
  556. "Set accept lifetime of the key\n"
  557. "Time to start\n"
  558. "Month of the year to start\n"
  559. "Day of th month to start\n"
  560. "Year to start\n"
  561. "Duration of the key\n"
  562. "Duration seconds\n")
  563. {
  564. struct key *key;
  565. key = vty->index_sub;
  566. return key_lifetime_duration_set (vty, &key->accept, argv[0], argv[2],
  567. argv[1], argv[3], argv[4]);
  568. }
  569. DEFUN (send_lifetime_day_month_day_month,
  570. send_lifetime_day_month_day_month_cmd,
  571. "send-lifetime HH:MM:SS <1-31> MONTH <1993-2035> HH:MM:SS <1-31> MONTH <1993-2035>",
  572. "Set send lifetime of the key\n"
  573. "Time to start\n"
  574. "Day of th month to start\n"
  575. "Month of the year to start\n"
  576. "Year to start\n"
  577. "Time to expire\n"
  578. "Day of th month to expire\n"
  579. "Month of the year to expire\n"
  580. "Year to expire\n")
  581. {
  582. struct key *key;
  583. key = vty->index_sub;
  584. return key_lifetime_set (vty, &key->send, argv[0], argv[1], argv[2], argv[3],
  585. argv[4], argv[5], argv[6], argv[7]);
  586. }
  587. DEFUN (send_lifetime_day_month_month_day,
  588. send_lifetime_day_month_month_day_cmd,
  589. "send-lifetime HH:MM:SS <1-31> MONTH <1993-2035> HH:MM:SS MONTH <1-31> <1993-2035>",
  590. "Set send lifetime of the key\n"
  591. "Time to start\n"
  592. "Day of th month to start\n"
  593. "Month of the year to start\n"
  594. "Year to start\n"
  595. "Time to expire\n"
  596. "Month of the year to expire\n"
  597. "Day of th month to expire\n"
  598. "Year to expire\n")
  599. {
  600. struct key *key;
  601. key = vty->index_sub;
  602. return key_lifetime_set (vty, &key->send, argv[0], argv[1], argv[2], argv[3],
  603. argv[4], argv[6], argv[5], argv[7]);
  604. }
  605. DEFUN (send_lifetime_month_day_day_month,
  606. send_lifetime_month_day_day_month_cmd,
  607. "send-lifetime HH:MM:SS MONTH <1-31> <1993-2035> HH:MM:SS <1-31> MONTH <1993-2035>",
  608. "Set send lifetime of the key\n"
  609. "Time to start\n"
  610. "Month of the year to start\n"
  611. "Day of th month to start\n"
  612. "Year to start\n"
  613. "Time to expire\n"
  614. "Day of th month to expire\n"
  615. "Month of the year to expire\n"
  616. "Year to expire\n")
  617. {
  618. struct key *key;
  619. key = vty->index_sub;
  620. return key_lifetime_set (vty, &key->send, argv[0], argv[2], argv[1], argv[3],
  621. argv[4], argv[5], argv[6], argv[7]);
  622. }
  623. DEFUN (send_lifetime_month_day_month_day,
  624. send_lifetime_month_day_month_day_cmd,
  625. "send-lifetime HH:MM:SS MONTH <1-31> <1993-2035> HH:MM:SS MONTH <1-31> <1993-2035>",
  626. "Set send lifetime of the key\n"
  627. "Time to start\n"
  628. "Month of the year to start\n"
  629. "Day of th month to start\n"
  630. "Year to start\n"
  631. "Time to expire\n"
  632. "Month of the year to expire\n"
  633. "Day of th month to expire\n"
  634. "Year to expire\n")
  635. {
  636. struct key *key;
  637. key = vty->index_sub;
  638. return key_lifetime_set (vty, &key->send, argv[0], argv[2], argv[1], argv[3],
  639. argv[4], argv[6], argv[5], argv[7]);
  640. }
  641. DEFUN (send_lifetime_infinite_day_month,
  642. send_lifetime_infinite_day_month_cmd,
  643. "send-lifetime HH:MM:SS <1-31> MONTH <1993-2035> infinite",
  644. "Set send lifetime of the key\n"
  645. "Time to start\n"
  646. "Day of th month to start\n"
  647. "Month of the year to start\n"
  648. "Year to start\n"
  649. "Never expires")
  650. {
  651. struct key *key;
  652. key = vty->index_sub;
  653. return key_lifetime_infinite_set (vty, &key->send, argv[0], argv[1], argv[2],
  654. argv[3]);
  655. }
  656. DEFUN (send_lifetime_infinite_month_day,
  657. send_lifetime_infinite_month_day_cmd,
  658. "send-lifetime HH:MM:SS MONTH <1-31> <1993-2035> infinite",
  659. "Set send lifetime of the key\n"
  660. "Time to start\n"
  661. "Month of the year to start\n"
  662. "Day of th month to start\n"
  663. "Year to start\n"
  664. "Never expires")
  665. {
  666. struct key *key;
  667. key = vty->index_sub;
  668. return key_lifetime_infinite_set (vty, &key->send, argv[0], argv[2], argv[1],
  669. argv[3]);
  670. }
  671. DEFUN (send_lifetime_duration_day_month,
  672. send_lifetime_duration_day_month_cmd,
  673. "send-lifetime HH:MM:SS <1-31> MONTH <1993-2035> duration <1-2147483646>",
  674. "Set send lifetime of the key\n"
  675. "Time to start\n"
  676. "Day of th month to start\n"
  677. "Month of the year to start\n"
  678. "Year to start\n"
  679. "Duration of the key\n"
  680. "Duration seconds\n")
  681. {
  682. struct key *key;
  683. key = vty->index_sub;
  684. return key_lifetime_duration_set (vty, &key->send, argv[0], argv[1], argv[2],
  685. argv[3], argv[4]);
  686. }
  687. DEFUN (send_lifetime_duration_month_day,
  688. send_lifetime_duration_month_day_cmd,
  689. "send-lifetime HH:MM:SS MONTH <1-31> <1993-2035> duration <1-2147483646>",
  690. "Set send lifetime of the key\n"
  691. "Time to start\n"
  692. "Month of the year to start\n"
  693. "Day of th month to start\n"
  694. "Year to start\n"
  695. "Duration of the key\n"
  696. "Duration seconds\n")
  697. {
  698. struct key *key;
  699. key = vty->index_sub;
  700. return key_lifetime_duration_set (vty, &key->send, argv[0], argv[2], argv[1],
  701. argv[3], argv[4]);
  702. }
  703. static struct cmd_node keychain_node =
  704. {
  705. KEYCHAIN_NODE,
  706. "%s(config-keychain)# ",
  707. 1
  708. };
  709. static struct cmd_node keychain_key_node =
  710. {
  711. KEYCHAIN_KEY_NODE,
  712. "%s(config-keychain-key)# ",
  713. 1
  714. };
  715. static int
  716. keychain_strftime (char *buf, int bufsiz, time_t *time)
  717. {
  718. struct tm *tm;
  719. size_t len;
  720. tm = localtime (time);
  721. len = strftime (buf, bufsiz, "%T %b %d %Y", tm);
  722. return len;
  723. }
  724. static int
  725. keychain_config_write (struct vty *vty)
  726. {
  727. struct keychain *keychain;
  728. struct key *key;
  729. struct listnode *node;
  730. struct listnode *knode;
  731. char buf[BUFSIZ];
  732. for (ALL_LIST_ELEMENTS_RO (keychain_list, node, keychain))
  733. {
  734. vty_out (vty, "key chain %s%s", keychain->name, VTY_NEWLINE);
  735. for (ALL_LIST_ELEMENTS_RO (keychain->key, knode, key))
  736. {
  737. vty_out (vty, " key %d%s", key->index, VTY_NEWLINE);
  738. if (key->string)
  739. vty_out (vty, " key-string %s%s", key->string, VTY_NEWLINE);
  740. if (key->accept.start)
  741. {
  742. keychain_strftime (buf, BUFSIZ, &key->accept.start);
  743. vty_out (vty, " accept-lifetime %s", buf);
  744. if (key->accept.end == -1)
  745. vty_out (vty, " infinite");
  746. else if (key->accept.duration)
  747. vty_out (vty, " duration %ld",
  748. (long)(key->accept.end - key->accept.start));
  749. else
  750. {
  751. keychain_strftime (buf, BUFSIZ, &key->accept.end);
  752. vty_out (vty, " %s", buf);
  753. }
  754. vty_out (vty, "%s", VTY_NEWLINE);
  755. }
  756. if (key->send.start)
  757. {
  758. keychain_strftime (buf, BUFSIZ, &key->send.start);
  759. vty_out (vty, " send-lifetime %s", buf);
  760. if (key->send.end == -1)
  761. vty_out (vty, " infinite");
  762. else if (key->send.duration)
  763. vty_out (vty, " duration %ld", (long)(key->send.end - key->send.start));
  764. else
  765. {
  766. keychain_strftime (buf, BUFSIZ, &key->send.end);
  767. vty_out (vty, " %s", buf);
  768. }
  769. vty_out (vty, "%s", VTY_NEWLINE);
  770. }
  771. }
  772. vty_out (vty, "!%s", VTY_NEWLINE);
  773. }
  774. return 0;
  775. }
  776. void
  777. keychain_init ()
  778. {
  779. keychain_list = list_new ();
  780. install_node (&keychain_node, keychain_config_write);
  781. install_node (&keychain_key_node, NULL);
  782. install_default (KEYCHAIN_NODE);
  783. install_default (KEYCHAIN_KEY_NODE);
  784. install_element (CONFIG_NODE, &key_chain_cmd);
  785. install_element (CONFIG_NODE, &no_key_chain_cmd);
  786. install_element (KEYCHAIN_NODE, &key_cmd);
  787. install_element (KEYCHAIN_NODE, &no_key_cmd);
  788. install_element (KEYCHAIN_NODE, &key_chain_cmd);
  789. install_element (KEYCHAIN_NODE, &no_key_chain_cmd);
  790. install_element (KEYCHAIN_KEY_NODE, &key_string_cmd);
  791. install_element (KEYCHAIN_KEY_NODE, &no_key_string_cmd);
  792. install_element (KEYCHAIN_KEY_NODE, &key_chain_cmd);
  793. install_element (KEYCHAIN_KEY_NODE, &no_key_chain_cmd);
  794. install_element (KEYCHAIN_KEY_NODE, &key_cmd);
  795. install_element (KEYCHAIN_KEY_NODE, &no_key_cmd);
  796. install_element (KEYCHAIN_KEY_NODE, &accept_lifetime_day_month_day_month_cmd);
  797. install_element (KEYCHAIN_KEY_NODE, &accept_lifetime_day_month_month_day_cmd);
  798. install_element (KEYCHAIN_KEY_NODE, &accept_lifetime_month_day_day_month_cmd);
  799. install_element (KEYCHAIN_KEY_NODE, &accept_lifetime_month_day_month_day_cmd);
  800. install_element (KEYCHAIN_KEY_NODE, &accept_lifetime_infinite_day_month_cmd);
  801. install_element (KEYCHAIN_KEY_NODE, &accept_lifetime_infinite_month_day_cmd);
  802. install_element (KEYCHAIN_KEY_NODE, &accept_lifetime_duration_day_month_cmd);
  803. install_element (KEYCHAIN_KEY_NODE, &accept_lifetime_duration_month_day_cmd);
  804. install_element (KEYCHAIN_KEY_NODE, &send_lifetime_day_month_day_month_cmd);
  805. install_element (KEYCHAIN_KEY_NODE, &send_lifetime_day_month_month_day_cmd);
  806. install_element (KEYCHAIN_KEY_NODE, &send_lifetime_month_day_day_month_cmd);
  807. install_element (KEYCHAIN_KEY_NODE, &send_lifetime_month_day_month_day_cmd);
  808. install_element (KEYCHAIN_KEY_NODE, &send_lifetime_infinite_day_month_cmd);
  809. install_element (KEYCHAIN_KEY_NODE, &send_lifetime_infinite_month_day_cmd);
  810. install_element (KEYCHAIN_KEY_NODE, &send_lifetime_duration_day_month_cmd);
  811. install_element (KEYCHAIN_KEY_NODE, &send_lifetime_duration_month_day_cmd);
  812. }