isisd: API: circuit password

This cleans up circuit password configuration a little bit.
(Restructured several times by both Christian Franke and David
Lamparter.)

Signed-off-by: David Lamparter <equinox@opensourcerouting.org>
diff --git a/isisd/isis_circuit.c b/isisd/isis_circuit.c
index 6adda08..707d77a 100644
--- a/isisd/isis_circuit.c
+++ b/isisd/isis_circuit.c
@@ -1332,72 +1332,41 @@
   return 0;
 }
 
-DEFUN (isis_passwd_md5,
-       isis_passwd_md5_cmd,
-       "isis password md5 WORD",
-       "IS-IS commands\n"
-       "Configure the authentication password for a circuit\n"
-       "Authentication type\n"
-       "Circuit password\n")
+int
+isis_circuit_passwd_unset (struct isis_circuit *circuit)
 {
-  int len;
-  struct isis_circuit *circuit = isis_circuit_lookup (vty);
-  if (!circuit)
-    return CMD_ERR_NO_MATCH;
-
-  len = strlen (argv[0]);
-  if (len > 254)
-    {
-      vty_out (vty, "Too long circuit password (>254)%s", VTY_NEWLINE);
-      return CMD_ERR_AMBIGUOUS;
-    }
-  circuit->passwd.len = len;
-  circuit->passwd.type = ISIS_PASSWD_TYPE_HMAC_MD5;
-  strncpy ((char *)circuit->passwd.passwd, argv[0], 255);
-
-  return CMD_SUCCESS;
+  memset(&circuit->passwd, 0, sizeof(circuit->passwd));
+  return 0;
 }
 
-DEFUN (isis_passwd_clear,
-       isis_passwd_clear_cmd,
-       "isis password clear WORD",
-       "IS-IS commands\n"
-       "Configure the authentication password for a circuit\n"
-       "Authentication type\n"
-       "Circuit password\n")
+static int
+isis_circuit_passwd_set (struct isis_circuit *circuit, u_char passwd_type, const char *passwd)
 {
   int len;
-  struct isis_circuit *circuit = isis_circuit_lookup (vty);
-  if (!circuit)
-    return CMD_ERR_NO_MATCH;
 
-  len = strlen (argv[0]);
+  if (!passwd)
+    return -1;
+
+  len = strlen(passwd);
   if (len > 254)
-    {
-      vty_out (vty, "Too long circuit password (>254)%s", VTY_NEWLINE);
-      return CMD_ERR_AMBIGUOUS;
-    }
-  circuit->passwd.len = len;
-  circuit->passwd.type = ISIS_PASSWD_TYPE_CLEARTXT;
-  strncpy ((char *)circuit->passwd.passwd, argv[0], 255);
+    return -1;
 
-  return CMD_SUCCESS;
+  circuit->passwd.len = len;
+  strncpy((char *)circuit->passwd.passwd, passwd, 255);
+  circuit->passwd.type = passwd_type;
+  return 0;
 }
 
-DEFUN (no_isis_passwd,
-       no_isis_passwd_cmd,
-       "no isis password",
-       NO_STR
-       "IS-IS commands\n"
-       "Configure the authentication password for a circuit\n")
+int
+isis_circuit_passwd_cleartext_set (struct isis_circuit *circuit, const char *passwd)
 {
-  struct isis_circuit *circuit = isis_circuit_lookup (vty);
-  if (!circuit)
-    return CMD_ERR_NO_MATCH;
+  return isis_circuit_passwd_set (circuit, ISIS_PASSWD_TYPE_CLEARTXT, passwd);
+}
 
-  memset (&circuit->passwd, 0, sizeof (struct isis_passwd));
-
-  return CMD_SUCCESS;
+int
+isis_circuit_passwd_hmac_md5_set (struct isis_circuit *circuit, const char *passwd)
+{
+  return isis_circuit_passwd_set (circuit, ISIS_PASSWD_TYPE_HMAC_MD5, passwd);
 }
 
 DEFUN (isis_hello_interval,
@@ -2143,10 +2112,6 @@
   install_element (INTERFACE_NODE, &interface_desc_cmd);
   install_element (INTERFACE_NODE, &no_interface_desc_cmd);
 
-  install_element (INTERFACE_NODE, &isis_passwd_clear_cmd);
-  install_element (INTERFACE_NODE, &isis_passwd_md5_cmd);
-  install_element (INTERFACE_NODE, &no_isis_passwd_cmd);
-
   install_element (INTERFACE_NODE, &isis_hello_interval_cmd);
   install_element (INTERFACE_NODE, &no_isis_hello_interval_cmd);
   install_element (INTERFACE_NODE, &no_isis_hello_interval_arg_cmd);
diff --git a/isisd/isis_circuit.h b/isisd/isis_circuit.h
index 343b492..9843ef3 100644
--- a/isisd/isis_circuit.h
+++ b/isisd/isis_circuit.h
@@ -180,4 +180,8 @@
 
 int  isis_circuit_metric_set (struct isis_circuit *circuit, int level, int metric);
 
+int  isis_circuit_passwd_unset (struct isis_circuit *circuit);
+int  isis_circuit_passwd_cleartext_set (struct isis_circuit *circuit, const char *passwd);
+int  isis_circuit_passwd_hmac_md5_set (struct isis_circuit *circuit, const char *passwd);
+
 #endif /* _ZEBRA_ISIS_CIRCUIT_H */
diff --git a/isisd/isis_vty.c b/isisd/isis_vty.c
index 06d59a8..c6f2465 100644
--- a/isisd/isis_vty.c
+++ b/isisd/isis_vty.c
@@ -293,6 +293,59 @@
   return CMD_SUCCESS;
 }
 
+DEFUN (isis_passwd,
+       isis_passwd_cmd,
+       "isis password (md5|clear) WORD",
+       "IS-IS commands\n"
+       "Configure the authentication password for a circuit\n"
+       "HMAC-MD5 authentication\n"
+       "Cleartext password\n"
+       "Circuit password\n")
+{
+  struct isis_circuit *circuit = isis_circuit_lookup (vty);
+  int rv;
+  if (!circuit)
+    return CMD_ERR_NO_MATCH;
+
+  if (argv[0][0] == 'm')
+    rv = isis_circuit_passwd_hmac_md5_set(circuit, argv[1]);
+  else
+    rv = isis_circuit_passwd_cleartext_set(circuit, argv[1]);
+  if (rv)
+    {
+      vty_out (vty, "Too long circuit password (>254)%s", VTY_NEWLINE);
+      return CMD_ERR_AMBIGUOUS;
+    }
+
+  return CMD_SUCCESS;
+}
+
+DEFUN (no_isis_passwd,
+       no_isis_passwd_cmd,
+       "no isis password",
+       NO_STR
+       "IS-IS commands\n"
+       "Configure the authentication password for a circuit\n")
+{
+  struct isis_circuit *circuit = isis_circuit_lookup (vty);
+  if (!circuit)
+    return CMD_ERR_NO_MATCH;
+
+  isis_circuit_passwd_unset(circuit);
+
+  return CMD_SUCCESS;
+}
+
+ALIAS (no_isis_passwd,
+       no_isis_passwd_arg_cmd,
+       "no isis password (md5|clear) WORD",
+       NO_STR
+       "IS-IS commands\n"
+       "Configure the authentication password for a circuit\n"
+       "HMAC-MD5 authentication\n"
+       "Cleartext password\n"
+       "Circuit password\n")
+
 DEFUN (isis_priority,
        isis_priority_cmd,
        "isis priority <0-127>",
@@ -822,6 +875,10 @@
   install_element (INTERFACE_NODE, &isis_network_cmd);
   install_element (INTERFACE_NODE, &no_isis_network_cmd);
 
+  install_element (INTERFACE_NODE, &isis_passwd_cmd);
+  install_element (INTERFACE_NODE, &no_isis_passwd_cmd);
+  install_element (INTERFACE_NODE, &no_isis_passwd_arg_cmd);
+
   install_element (INTERFACE_NODE, &isis_priority_cmd);
   install_element (INTERFACE_NODE, &no_isis_priority_cmd);
   install_element (INTERFACE_NODE, &no_isis_priority_arg_cmd);