package/klish-plugin-sysrepo: relocate sysrepo functionality here

This is a refactor to ensure sysrepo functionality guarded by NACM works
as intended in Infix.

Relocate infix_rpc() to klish-plugin-sysrepo, ensuring unauthenticated
users are denied access.

Drop infix_commit() in favor of slightly modified srp_commit(), this to
ensure sysrepo audit trail logs are visible at all times.

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
This commit is contained in:
Joachim Wiberg
2024-08-26 20:54:49 +02:00
parent e912ca5d3b
commit ab8783dad4
4 changed files with 8 additions and 113 deletions
@@ -1,3 +1,3 @@
# Locally calculated
sha256 9d9d33b873917ca5d0bdcc47a36d2fd385971ab0c045d1472fcadf95ee5bcf5b LICENCE
sha256 defb5129dc685fe5fc1d04c5b2949cae7e5699ab967315292da0ff7d378c71fc klish-plugin-sysrepo-5414b25d40e097a2341b809fe9a73df53dcf4046-br1.tar.gz
sha256 7c8c1cc2234331bb8123893356562cce6731448dbaf5b80db72e34490da55b48 klish-plugin-sysrepo-261c615b903233f63c695f91c1dbe58ac063ba13-br1.tar.gz
@@ -4,7 +4,7 @@
#
################################################################################
KLISH_PLUGIN_SYSREPO_VERSION = 5414b25d40e097a2341b809fe9a73df53dcf4046
KLISH_PLUGIN_SYSREPO_VERSION = 261c615b903233f63c695f91c1dbe58ac063ba13
KLISH_PLUGIN_SYSREPO_SITE = https://github.com/kernelkit/klish-plugin-sysrepo.git
#KLISH_PLUGIN_SYSREPO_VERSION = cdd3eb51a7f7ee0ed5bd925fa636061d3b1b85fb
#KLISH_PLUGIN_SYSREPO_SITE = https://src.libcode.org/pkun/klish-plugin-sysrepo.git
-105
View File
@@ -589,42 +589,6 @@ err:
return rc;
}
int infix_commit(kcontext_t *ctx)
{
sr_session_ctx_t *sess;
sr_conn_ctx_t *conn;
int err;
(void)ctx;
if (sr_connect(SR_CONN_DEFAULT, &conn)) {
fprintf(stderr, ERRMSG "connection to datastore failed\n");
goto err;
}
if (sr_session_start(conn, SR_DS_RUNNING, &sess)) {
fprintf(stderr, ERRMSG "unable to open transaction to running-config\n");
goto err_disconnect;
}
err = sr_copy_config(sess, NULL, SR_DS_CANDIDATE, 0);
if (err) {
if (err == SR_ERR_CALLBACK_FAILED)
emsg(sess, "Please check your changes, try 'diff' and 'do show interfaces'.\n");
else
emsg(sess, "Failed committing candidate to running: %s\n", sr_strerror(err));
goto err_disconnect;
}
sr_disconnect(conn);
return 0;
err_disconnect:
sr_disconnect(conn);
err:
return -1;
}
int infix_shell(kcontext_t *ctx)
{
const char *user = "root";
@@ -672,73 +636,6 @@ int infix_shell(kcontext_t *ctx)
return rc;
}
int infix_rpc(kcontext_t *ctx)
{
kpargv_pargs_node_t *iter;
size_t icnt = 0, ocnt = 0;
sr_val_t *input = NULL;
sr_session_ctx_t *sess;
sr_conn_ctx_t *conn;
const char *xpath;
sr_val_t *output;
kparg_t *parg;
int err;
xpath = kcontext_script(ctx);
if (!xpath) {
fprintf(stderr, ERRMSG "cannot find rpc xpath\n");
goto err;
}
iter = kpargv_pargs_iter(kcontext_pargv(ctx));
while ((parg = kpargv_pargs_each(&iter))) {
const char *key = kentry_name(kparg_entry(parg));
const char *val = kparg_value(parg);
/* skip leading part of command line: 'set datetime' */
// fprintf(stderr, "%s(): got key %s val %s\n", __func__, key, val ?: "<NIL>");
if (!val || !strcmp(key, val))
continue;
sr_realloc_values(icnt, icnt + 1, &input);
/* e.g. /ietf-system:set-current-datetime/current-datetime */
sr_val_build_xpath(&input[icnt], "%s/%s", xpath, key);
sr_val_set_str_data(&input[icnt++], SR_STRING_T, val);
}
if (sr_connect(SR_CONN_DEFAULT, &conn)) {
fprintf(stderr, ERRMSG "connection to datastore failed\n");
goto err;
}
if (sr_session_start(conn, SR_DS_OPERATIONAL, &sess)) {
emsg(sess, ERRMSG "unable to open transaction to running-config\n");
goto err_disconnect;
}
// fprintf(stderr, "%s(): sending RPC %s, icnt %zu\n", __func__, xpath, icnt);
if ((err = sr_rpc_send(sess, xpath, input, icnt, 0, &output, &ocnt))) {
emsg(sess, ERRMSG "failed sending RPC %s: %s\n", xpath, sr_strerror(err));
goto err_disconnect;
}
for (size_t i = 0; i < ocnt; i++) {
sr_print_val(&output[i]);
puts("");
}
sr_free_values(input, icnt);
sr_free_values(output, ocnt);
sr_disconnect(conn);
return 0;
err_disconnect:
sr_disconnect(conn);
err:
sr_free_values(input, icnt);
return -1;
}
int kplugin_infix_fini(kcontext_t *ctx)
{
(void)ctx;
@@ -751,13 +648,11 @@ int kplugin_infix_init(kcontext_t *ctx)
kplugin_t *plugin = kcontext_plugin(ctx);
kplugin_add_syms(plugin, ksym_new("copy", infix_copy));
kplugin_add_syms(plugin, ksym_new("commit", infix_commit));
kplugin_add_syms(plugin, ksym_new("datastore", infix_datastore));
kplugin_add_syms(plugin, ksym_new("erase", infix_erase));
kplugin_add_syms(plugin, ksym_new("files", infix_files));
kplugin_add_syms(plugin, ksym_new("ifaces", infix_ifaces));
kplugin_add_syms(plugin, ksym_new("shell", infix_shell));
kplugin_add_syms(plugin, ksym_new("rpc", infix_rpc));
return 0;
}
+6 -6
View File
@@ -145,11 +145,11 @@
</COMMAND>
<COMMAND name="poweroff" help="Poweroff system (system policy may yield reboot)">
<ACTION sym="rpc@infix" ptype="STRING">/ietf-system:system-shutdown</ACTION>
<ACTION sym="srp_rpc@sysrepo" ptype="STRING">/ietf-system:system-shutdown</ACTION>
</COMMAND>
<COMMAND name="reboot" help="Reboot system">
<ACTION sym="rpc@infix" ptype="STRING">/ietf-system:system-restart</ACTION>
<ACTION sym="srp_rpc@sysrepo" ptype="STRING">/ietf-system:system-restart</ACTION>
</COMMAND>
<COMMAND name="exit" help="Exit from CLI (log out)">
@@ -219,7 +219,7 @@
<COMMAND name="set" help="Set operations, e.g., current date/time" mode="switch">
<COMMAND name="datetime" help="Set current date and time, ISO-8601 format">
<PARAM name="current-datetime" ptype="/STRING" help="yyyy-mm-ddThh:mm:ss(Z|+/-hh:mm)"/>
<ACTION sym="rpc@infix">/ietf-system:set-current-datetime</ACTION>
<ACTION sym="srp_rpc@sysrepo">/ietf-system:set-current-datetime</ACTION>
</COMMAND>
</COMMAND>
@@ -436,7 +436,7 @@
</COMMAND>
<COMMAND name="factory-reset" help="Restore the system to factory default state">
<ACTION sym="script" in="tty" out="tty" interrupt="true">/bin/yorn "This will restore the device to factory defaults"</ACTION>
<ACTION sym="rpc@infix">/ietf-factory-default:factory-reset</ACTION>
<ACTION sym="srp_rpc@sysrepo">/ietf-factory-default:factory-reset</ACTION>
</COMMAND>
<COMMAND name="follow" help="Monitor a log file, use Ctrl-C to abort">
@@ -545,7 +545,7 @@
</COMMAND>
<COMMAND name="leave" help="Apply candidate to running-config and return to admin-exec">
<ACTION sym="commit@infix"/>
<ACTION sym="srp_commit@sysrepo"/>
<ACTION sym="srp_top@sysrepo"/>
<ACTION sym="nav" exec_on="success">replace main</ACTION>
</COMMAND>
@@ -601,7 +601,7 @@
</COMMAND>
<COMMAND name="commit" help="Commit current candidate to running-config">
<ACTION sym="commit@infix"/>
<ACTION sym="srp_commit@sysrepo"/>
</COMMAND>
<COMMAND name="reset" help="Reset candidate to running-config">