Skip to content

Commit

Permalink
examples BUGFIX reflect changes in modules and api
Browse files Browse the repository at this point in the history
Fixed the default config in config.json, removed UNIX sock from the config and added an option to create it with API, also made some changes to the description of the examples.

Fixes #472.
  • Loading branch information
Roytak committed Mar 27, 2024
1 parent a55fee0 commit 396f727
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 86 deletions.
20 changes: 11 additions & 9 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,28 @@ There are two examples `server` and `client` demonstrating a simple NETCONF serv
The example server provides `ietf-yang-library` state data that are returned as a reply to `get` RPC. In case an XPath filter is used it is properly applied on these data. If some unsupported parameters are specified, the server replies with a NETCONF error.

### Server Configuration
The server's default configuration can be found in the `config.json` file. The YANG data stored in this file define three endpoints - two for SSH and one for UNIX socket.
The server's default configuration can be found in the `config.json` file. The YANG data stored in this file define two SSH endpoints - they differ in port and in how clients get authenticated.
You can modify this configuration in any way you want, however, configuring the server may fail if the configuration is not valid.

## Example usage
### Server
First start the server:
```
$ server
$ server -u ./example-socket
```
The server will be started and configured per YANG data stored in the file `config.json`. A UNIX socket with the default address `/tmp/.ln2-unix-socket` will be created.
In addition to that two SSH endpoints with the addresses `127.0.0.1:10000` and `127.0.0.1:10001` will be listening.
The server will be started and configured per YANG data stored in the file `config.json`.
Two SSH endpoints with the addresses `127.0.0.1:10000` and `127.0.0.1:10001` will start listening for new connections.
This first endpoint has a single user that can authenticate with a password (which is set to `admin` by default).
The second endpoint has a single user that can authenticate with a publickey (the asymmetric key pair used is stored in `admin_key` and `admin_key.pub`).
The `-u` option specifies that a UNIX socket endpoint will be created and `./example-socket` is the path to where the socket will be listening.

### Client
#### UNIX socket
After the server has been run, in another terminal instance, with the default configuration:
```
$ client -u /tmp/.ln2-unix-socket get "/ietf-yang-library:yang-library/module-set/module[name='ietf-netconf']"
$ client -u ./example-socket get "/ietf-yang-library:yang-library/module-set/module[name='ietf-netconf']"
```
In this case, `-u` means that a connection to an UNIX socket will be attempted and a path to the socket needs to be specified, that is `/tmp/ln2-unix-socket` by default.
In this case, `-u` means that a connection to an UNIX socket will be attempted and a path to the socket needs to be specified.
The `get` parameter is the name of the RPC and `/ietf-yang-library:yang-library/module-set/module[name='ietf-netconf']` is the RPC's optional XPath filter.

##### Server output
Expand Down Expand Up @@ -87,10 +88,10 @@ The client received a single `ietf-yang-library` module based on the used filter
#### SSH
After the server has been run, in another terminal instance, with the default configuration:
```
$ client -p 10000 get-config candidate
$ client -p 10000 get-config startup
```
In this case, `-p 10000` is the port to connect to. By default the endpoint with this port has a single authorized client that needs to authenticate with a password.
The parameter `get-config` is the name of the RPC and `candidate` is the source datastore for the retrieved data of the get-config RPC.
The parameter `get-config` is the name of the RPC and `startup` is the source datastore for the retrieved data of the get-config RPC.

##### Server output
```
Expand Down Expand Up @@ -123,10 +124,11 @@ [email protected] password: <-- prompts for password, type in 'admin'
<ok/>
</rpc-reply>
```
No `startup` configuration is returned, because the example server lacks this functionality.
The _username_ in the `example.h` header file. The _password_ is located in `config.json`.

If you wish to connect to the SSH public key endpoint, you need to specify its port and the asymmetric key pair to use.
By default the command to connect would look like so:
```
$ ./examples/client -p 10001 -P /home/roman/libnetconf2/examples/admin_key.pub -i /home/roman/libnetconf2/examples/admin_key get
$ client -p 10001 -P ~/libnetconf2/examples/admin_key.pub -i ~/libnetconf2/examples/admin_key get
```
140 changes: 68 additions & 72 deletions examples/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,96 +2,92 @@
"ietf-netconf-server:netconf-server": {
"listen": {
"idle-timeout": 10,
"endpoint": [
{
"name": "ssh-password-auth-endpt",
"ssh": {
"tcp-server-parameters": {
"local-address": "127.0.0.1",
"local-port": 10000
},
"ssh-server-parameters": {
"server-identity": {
"host-key": [
{
"name": "key",
"public-key": {
"inline-definition": {
"public-key-format": "ietf-crypto-types:ssh-public-key-format",
"public-key": "AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBDRIB2eNSRWU+HNWRUGKr76ghCLg8RaMlUCps9lBjnc6ggaJl2Q+TOLn8se2wAdK3lYBMz3dcqR+SlU7eB8wJAc=",
"private-key-format": "ietf-crypto-types:ec-private-key-format",
"cleartext-private-key": "MHcCAQEEICQ2fr9Jt2xluom0YQQ7HseE8YTo5reZRVcQENKUWOrooAoGCCqGSM49AwEHoUQDQgAENEgHZ41JFZT4c1ZFQYqvvqCEIuDxFoyVQKmz2UGOdzqCBomXZD5M4ufyx7bAB0reVgEzPd1ypH5KVTt4HzAkBw=="
}
}
}
]
"endpoints": {
"endpoint": [
{
"name": "ssh-password-auth-endpt",
"ssh": {
"tcp-server-parameters": {
"local-address": "127.0.0.1",
"local-port": 10000
},
"client-authentication": {
"users": {
"user": [
"ssh-server-parameters": {
"server-identity": {
"host-key": [
{
"name": "admin",
"password": "$0$admin"
"name": "key",
"public-key": {
"inline-definition": {
"public-key-format": "ietf-crypto-types:ssh-public-key-format",
"public-key": "AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBDRIB2eNSRWU+HNWRUGKr76ghCLg8RaMlUCps9lBjnc6ggaJl2Q+TOLn8se2wAdK3lYBMz3dcqR+SlU7eB8wJAc=",
"private-key-format": "ietf-crypto-types:ec-private-key-format",
"cleartext-private-key": "MHcCAQEEICQ2fr9Jt2xluom0YQQ7HseE8YTo5reZRVcQENKUWOrooAoGCCqGSM49AwEHoUQDQgAENEgHZ41JFZT4c1ZFQYqvvqCEIuDxFoyVQKmz2UGOdzqCBomXZD5M4ufyx7bAB0reVgEzPd1ypH5KVTt4HzAkBw=="
}
}
}
]
},
"client-authentication": {
"users": {
"user": [
{
"name": "admin",
"password": "$0$admin"
}
]
}
}
}
}
}
},
{
"name": "ssh-pubkey-auth-endpt",
"ssh": {
"tcp-server-parameters": {
"local-address": "127.0.0.1",
"local-port": 10001
},
"ssh-server-parameters": {
"server-identity": {
"host-key": [
{
"name": "key",
"public-key": {
"inline-definition": {
"public-key-format": "ietf-crypto-types:ssh-public-key-format",
"public-key": "AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBDRIB2eNSRWU+HNWRUGKr76ghCLg8RaMlUCps9lBjnc6ggaJl2Q+TOLn8se2wAdK3lYBMz3dcqR+SlU7eB8wJAc=",
"private-key-format": "ietf-crypto-types:ec-private-key-format",
"cleartext-private-key": "MHcCAQEEICQ2fr9Jt2xluom0YQQ7HseE8YTo5reZRVcQENKUWOrooAoGCCqGSM49AwEHoUQDQgAENEgHZ41JFZT4c1ZFQYqvvqCEIuDxFoyVQKmz2UGOdzqCBomXZD5M4ufyx7bAB0reVgEzPd1ypH5KVTt4HzAkBw=="
}
}
}
]
},
{
"name": "ssh-pubkey-auth-endpt",
"ssh": {
"tcp-server-parameters": {
"local-address": "127.0.0.1",
"local-port": 10001
},
"client-authentication": {
"users": {
"user": [
"ssh-server-parameters": {
"server-identity": {
"host-key": [
{
"name": "admin",
"public-keys": {
"name": "key",
"public-key": {
"inline-definition": {
"public-key": [
{
"name": "admin_key.pub",
"public-key-format": "ietf-crypto-types:ssh-public-key-format",
"public-key": "AAAAC3NzaC1lZDI1NTE5AAAAIOr46rptg6BsWhO1JMomuh3cuCYmeuO6JfOUPs/YO35w"
}
]
"public-key-format": "ietf-crypto-types:ssh-public-key-format",
"public-key": "AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBDRIB2eNSRWU+HNWRUGKr76ghCLg8RaMlUCps9lBjnc6ggaJl2Q+TOLn8se2wAdK3lYBMz3dcqR+SlU7eB8wJAc=",
"private-key-format": "ietf-crypto-types:ec-private-key-format",
"cleartext-private-key": "MHcCAQEEICQ2fr9Jt2xluom0YQQ7HseE8YTo5reZRVcQENKUWOrooAoGCCqGSM49AwEHoUQDQgAENEgHZ41JFZT4c1ZFQYqvvqCEIuDxFoyVQKmz2UGOdzqCBomXZD5M4ufyx7bAB0reVgEzPd1ypH5KVTt4HzAkBw=="
}
}
}
]
},
"client-authentication": {
"users": {
"user": [
{
"name": "admin",
"public-keys": {
"inline-definition": {
"public-key": [
{
"name": "admin_key.pub",
"public-key-format": "ietf-crypto-types:ssh-public-key-format",
"public-key": "AAAAC3NzaC1lZDI1NTE5AAAAIOr46rptg6BsWhO1JMomuh3cuCYmeuO6JfOUPs/YO35w"
}
]
}
}
}
]
}
}
}
}
}
},
{
"name": "unix-socket-endpt",
"libnetconf2-netconf-server:unix-socket": {
"path": "/tmp/.ln2-unix-socket"
}
}
]
]
}
}
}
}
23 changes: 18 additions & 5 deletions examples/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -198,12 +198,11 @@ help_print()
"\n"
" Available options:\n"
" -h, --help\t \tPrint usage help.\n"
" -u, --unix\t<path>\tCreate a UNIX socket at the place specified by <path>.\n"
" -s, --ssh\t<path>\tCreate a SSH server with the host SSH key located at <path>.\n\n");
" -u, --unix\t<path>\tCreate a UNIX socket endpoint at the place specified by <path>.\n\n");
}

static int
init(struct ly_ctx **context, struct nc_pollsession **ps)
init(const char *unix_socket_path, struct ly_ctx **context, struct nc_pollsession **ps)
{
int rc = 0;
struct lyd_node *config = NULL;
Expand Down Expand Up @@ -237,6 +236,14 @@ init(struct ly_ctx **context, struct nc_pollsession **ps)
ERR_MSG_CLEANUP("Error occurred while initializing the server.\n");
}

/* create unix socket endpoint if path was set */
if (unix_socket_path) {
rc = nc_server_add_endpt_unix_socket_listen("unix-socket-endpt", unix_socket_path, -1, -1, -1);
if (rc) {
ERR_MSG_CLEANUP("Creating UNIX socket endpoint failed.\n");
}
}

/* create a new poll session structure, which is used for polling RPCs sent by clients */
*ps = nc_ps_new();
if (!*ps) {
Expand All @@ -261,16 +268,18 @@ main(int argc, char **argv)
struct ly_ctx *context = NULL;
struct nc_session *session, *new_session;
struct nc_pollsession *ps = NULL;
const char *unix_socket_path = NULL;

struct option options[] = {
{"help", no_argument, NULL, 'h'},
{"debug", no_argument, NULL, 'd'},
{"unix", required_argument, NULL, 'u'},
{NULL, 0, NULL, 0}
};

opterr = 0;

while ((opt = getopt_long(argc, argv, "hd", options, NULL)) != -1) {
while ((opt = getopt_long(argc, argv, "hdu:", options, NULL)) != -1) {
switch (opt) {
case 'h':
help_print();
Expand All @@ -280,13 +289,17 @@ main(int argc, char **argv)
nc_verbosity(NC_VERB_DEBUG);
break;

case 'u':
unix_socket_path = optarg;
break;

default:
ERR_MSG_CLEANUP("Invalid option or missing argument\n");
}
}

/* initialize the server */
r = init(&context, &ps);
r = init(unix_socket_path, &context, &ps);
if (r) {
ERR_MSG_CLEANUP("Initializing the server failed.");
}
Expand Down

0 comments on commit 396f727

Please sign in to comment.