keychain.c 24 KB

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