Skip to content

Commit 488edbc

Browse files
committed
Merge branch 'devel-server'
2 parents f397aba + 948cb7e commit 488edbc

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+1514
-922
lines changed

.travis-install-libs.sh

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,16 @@ sudo apt-get update -qq
1010
sudo apt-get install -y zlib1g-dev libssl-dev
1111
sudo apt-get install -y --force-yes libavl-dev libev-dev coreutils acl valgrind
1212

13-
if [ ! -d "cmocka-1.0.1/build" ]; then
13+
if [ ! -d "cmocka-1.1.1/build" ]; then
1414
echo "Building cmocka from source."
15-
wget https://cmocka.org/files/1.0/cmocka-1.0.1.tar.xz
16-
tar -xJvf cmocka-1.0.1.tar.xz
17-
cd cmocka-1.0.1 && mkdir build && cd build
15+
wget https://cmocka.org/files/1.1/cmocka-1.1.1.tar.xz
16+
tar -xJvf cmocka-1.1.1.tar.xz
17+
cd cmocka-1.1.1 && mkdir build && cd build
1818
cmake -DCMAKE_INSTALL_PREFIX:PATH=/usr .. && make -j2 && sudo make install
1919
cd ../..
2020
else
2121
echo "Using cmocka from cache."
22-
cd cmocka-1.0.1/build
22+
cd cmocka-1.1.1/build
2323
sudo make install
2424
cd ../..
2525
fi

.travis.yml

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,23 @@
1-
os: linux
2-
sudo: required
3-
dist: trusty
1+
language: c
42

5-
language:
6-
- c
7-
8-
compiler:
9-
- gcc
10-
- clang
3+
matrix:
4+
include:
5+
- os: linux
6+
dist: trusty
7+
sudo: required
8+
compiler: clang
9+
- os: linux
10+
dist: trusty
11+
sudo: required
12+
compiler: gcc
13+
addons:
14+
apt:
15+
sources:
16+
- ubuntu-toolchain-r-test
17+
packages:
18+
- g++-7
19+
env:
20+
- MATRIX_EVAL="CC=gcc-7 && CXX=g++-7"
1121

1222
branches:
1323
only:
@@ -19,9 +29,10 @@ cache:
1929
- libssh-0.7.5
2030
- protobuf
2131
- protobuf-c
22-
- cmocka-1.0.1
32+
- cmocka-1.1.1
2333

2434
before_install:
35+
- eval "${MATRIX_EVAL}"
2536
- if [ "$TRAVIS_OS_NAME" == "linux" ]; then bash .travis-install-libs.sh; fi
2637
- if [ "$TRAVIS_OS_NAME" = "linux" -a "$CC" = "gcc" ]; then pip install --user codecov; export CFLAGS="-coverage"; fi
2738

cli/CMakeLists.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@ set(CMAKE_C_FLAGS_RELEASE "-O2")
2121
set(CMAKE_C_FLAGS_DEBUG "-g -O0")
2222

2323
# set version
24-
set(NP2CLI_VERSION 2.0.45)
25-
configure_file("${PROJECT_SOURCE_DIR}/version.h.in" "${PROJECT_SOURCE_DIR}/version.h" ESCAPE_QUOTES @ONLY)
24+
set(NP2CLI_VERSION 2.0.47)
25+
configure_file("${PROJECT_SOURCE_DIR}/version.h.in" "${PROJECT_BINARY_DIR}/version.h" ESCAPE_QUOTES @ONLY)
26+
include_directories(${PROJECT_BINARY_DIR})
2627

2728
# source files
2829
set(srcs

cli/commands.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ static void
201201
cli_ntf_clb(struct nc_session *session, const struct nc_notif *notif)
202202
{
203203
FILE *output = nc_session_get_data(session);
204-
int was_rawmode;
204+
int was_rawmode = 0;
205205

206206
if (output == stdout) {
207207
if (ls.rawmode) {
@@ -504,7 +504,7 @@ static char *
504504
trim_top_elem(char *data, const char *top_elem, const char *top_elem_ns)
505505
{
506506
char *ptr, *prefix = NULL, *buf;
507-
int pref_len, state = 0, quote;
507+
int pref_len = 0, state = 0, quote;
508508

509509
/* state: -2 - syntax error,
510510
* -1 - top_elem not found,
@@ -1207,7 +1207,9 @@ cmd_auth(const char *arg, char **UNUSED(tmp_config_file))
12071207
ERROR("auth keys add", "Missing the public key path");
12081208
return EXIT_FAILURE;
12091209
}
1210-
if (nc_client_ssh_add_keypair(str, cmd) != EXIT_SUCCESS) {
1210+
1211+
if (nc_client_ssh_ch_add_keypair(str, cmd) != EXIT_SUCCESS ||
1212+
nc_client_ssh_add_keypair(str, cmd) != EXIT_SUCCESS) {
12111213
ERROR("auth keys add", "Failed to add keys");
12121214
return EXIT_FAILURE;
12131215
}
@@ -1227,7 +1229,7 @@ cmd_auth(const char *arg, char **UNUSED(tmp_config_file))
12271229
}
12281230

12291231
i = strtol(cmd, &ptr, 10);
1230-
if (ptr[0] || nc_client_ssh_del_keypair(i)) {
1232+
if (ptr[0] || nc_client_ssh_ch_del_keypair(i) || nc_client_ssh_del_keypair(i)) {
12311233
ERROR("auth keys remove", "Wrong index");
12321234
return EXIT_FAILURE;
12331235
}

cli/completion.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* This source code is licensed under BSD 3-Clause License (the "License").
99
* You may not use this file except in compliance with the License.
1010
* You may obtain a copy of the License at
11-
*
11+
*
1212
* https://opensource.org/licenses/BSD-3-Clause
1313
*/
1414

@@ -115,9 +115,11 @@ complete_cmd(const char *buf, const char *hint, linenoiseCompletions *lc)
115115
char *
116116
readinput(const char *instruction, const char *old_tmp, char **new_tmp)
117117
{
118-
int tmpfd = -1, ret, size, oldfd;
118+
volatile int tmpfd = -1;
119+
int ret, size, oldfd;
119120
pid_t pid, wait_pid;
120-
char* tmpname = NULL, *input = NULL, *old_content = NULL, *ptr, *ptr2;
121+
char* volatile input = NULL, * volatile old_content = NULL;
122+
char *tmpname = NULL, *ptr, *ptr2;
121123

122124
/* Create a unique temporary file */
123125
#ifdef HAVE_MKSTEMPS
@@ -226,7 +228,7 @@ readinput(const char *instruction, const char *old_tmp, char **new_tmp)
226228
lseek(tmpfd, 0, SEEK_SET);
227229

228230
/* Read the input */
229-
input = malloc(size+1);
231+
input = malloc(size + 1);
230232
ret = read(tmpfd, input, size);
231233
if (ret < size) {
232234
ERROR(__func__, "Failed to read from the temporary file (%s).", strerror(errno));
@@ -244,7 +246,7 @@ readinput(const char *instruction, const char *old_tmp, char **new_tmp)
244246
/* The user could have deleted or modified the comment, ignore it then */
245247
if (ptr2) {
246248
ptr2 += 5;
247-
memmove(ptr, ptr2, strlen(ptr2)+1);
249+
memmove(ptr, ptr2, strlen(ptr2) + 1);
248250

249251
/* Save the modified content */
250252
if (ftruncate(tmpfd, 0) == -1) {

cli/configuration.c

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -67,21 +67,13 @@ get_netconf_dir(void)
6767
return NULL;
6868
}
6969

70-
ret = eaccess(netconf_dir, R_OK | X_OK);
71-
if (ret == -1) {
72-
if (errno == ENOENT) {
73-
/* directory does not exist */
74-
ERROR("get_netconf_dir", "Configuration directory \"%s\" does not exist, creating it.", netconf_dir);
75-
if (mkdir(netconf_dir, 00700)) {
76-
ERROR("get_netconf_dir", "Configuration directory \"%s\" cannot be created: %s", netconf_dir, strerror(errno));
77-
free(netconf_dir);
78-
return NULL;
79-
}
80-
} else {
81-
ERROR("get_netconf_dir", "Configuration directory \"%s\" exists but something else failed: %s", netconf_dir, strerror(errno));
82-
free(netconf_dir);
83-
return NULL;
84-
}
70+
ret = mkdir(netconf_dir, 00700);
71+
if (!ret) {
72+
ERROR("get_netconf_dir", "Configuration directory \"%s\" did not exist, created.", netconf_dir);
73+
} else if (errno != EEXIST) {
74+
ERROR("get_netconf_dir", "Configuration directory \"%s\" cannot be created: %s", netconf_dir, strerror(errno));
75+
free(netconf_dir);
76+
return NULL;
8577
}
8678

8779
return netconf_dir;
@@ -310,15 +302,13 @@ load_config(void)
310302
} else if (!strcmp(child->name, "searchpath")) {
311303
/* doc -> <netconf-client> -> <searchpath> */
312304
errno = 0;
313-
if (eaccess(child->content, R_OK | W_OK | X_OK) && (errno == ENOENT)) {
314-
ERROR(__func__, "Search path \"%s\" does not exist, creating it.", child->content);
315-
if (mkdir(child->content, 00700)) {
316-
ERROR(__func__, "Search path \"%s\" cannot be created: %s", child->content, strerror(errno));
317-
} else {
318-
nc_client_set_schema_searchpath(child->content);
305+
if (!mkdir(child->content, 00700) || (errno == EEXIST)) {
306+
if (errno == 0) {
307+
ERROR(__func__, "Search path \"%s\" did not exist, created.", child->content);
319308
}
320-
} else {
321309
nc_client_set_schema_searchpath(child->content);
310+
} else {
311+
ERROR(__func__, "Search path \"%s\" cannot be created: %s", child->content, strerror(errno));
322312
}
323313
} else if (!strcmp(child->name, "output-format")) {
324314
/* doc -> <netconf-client> -> <output-format> */
@@ -360,6 +350,7 @@ load_config(void)
360350
}
361351
}
362352
if (key_pub && key_priv) {
353+
nc_client_ssh_ch_add_keypair(key_pub, key_priv);
363354
nc_client_ssh_add_keypair(key_pub, key_priv);
364355
}
365356
}

keystored/CMakeLists.txt

Lines changed: 37 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ if (NOT OPENSSL_EXECUTABLE)
3434
endif()
3535
endif()
3636

37-
configure_file("${PROJECT_SOURCE_DIR}/config.h.in" "${PROJECT_SOURCE_DIR}/config.h" ESCAPE_QUOTES @ONLY)
37+
configure_file("${PROJECT_SOURCE_DIR}/config.h.in" "${PROJECT_BINARY_DIR}/config.h" ESCAPE_QUOTES @ONLY)
38+
include_directories(${PROJECT_BINARY_DIR})
3839

3940
# keystored plugin
4041
add_library(keystored SHARED keystored.c)
@@ -101,74 +102,48 @@ if (NOT CHMOD_EXECUTABLE)
101102
message(FATAL_ERROR "Unable to find chmod, set CHMOD_EXECUTABLE manually.")
102103
endif()
103104

105+
# Command line options to be passed to `sysrepoctl` when working with modules
106+
# which should only be accessible by an administrator
107+
if (NOT SYSREPOCTL_ROOT_PERMS)
108+
set(SYSREPOCTL_ROOT_PERMS "-o root:root -p 600")
109+
endif()
110+
104111
# create the keys directory with correct permissions
105112
install(DIRECTORY DESTINATION ${KEYSTORED_KEYS_DIR}
106113
DIRECTORY_PERMISSIONS OWNER_WRITE OWNER_READ OWNER_EXECUTE)
107114

108-
# install all the required modules and enable features
109-
install(CODE "
110-
execute_process(COMMAND ${SYSREPOCTL_EXECUTABLE} -l RESULT_VARIABLE RET OUTPUT_VARIABLE INSTALLED_MODULES ERROR_VARIABLE OUT)
111-
if (RET)
112-
string(REPLACE \"\\n\" \"\\n \" OUT \"\${OUT}\")
113-
message(FATAL_ERROR \" Command sysrepoctl list failed:\n \${OUT}\")
114-
endif()
115-
116-
string(REGEX MATCH \"ietf-x509-cert-to-name[^|]*[|][^|]*[|] Installed[^\n]*\" INSTALLED_MODULE_LINE \"\${INSTALLED_MODULES}\")
117-
if (NOT INSTALLED_MODULE_LINE)
118-
message(STATUS \"Importing module ietf-x509-cert-to-name into sysrepo...\")
119-
120-
execute_process(COMMAND ${SYSREPOCTL_EXECUTABLE} -i -g ${CMAKE_SOURCE_DIR}/../modules/ietf-x509-cert-to-name.yang -o root:root -p 600 RESULT_VARIABLE RET OUTPUT_VARIABLE OUT ERROR_VARIABLE OUT)
121-
if (RET)
122-
string(REPLACE \"\\n\" \"\\n \" OUT \"\${OUT}\")
123-
message(FATAL_ERROR \" Command sysrepoctl install failed:\\n \${OUT}\")
124-
endif()
125-
126-
else()
127-
message(STATUS \"Module ietf-x509-cert-to-name already in sysrepo.\")
128-
endif()
129-
130-
string(REGEX MATCH \"ietf-keystore [^\n]*\" INSTALLED_MODULE_LINE \"\${INSTALLED_MODULES}\")
131-
if (NOT INSTALLED_MODULE_LINE)
132-
message(STATUS \"Importing module ietf-keystore into sysrepo...\")
133-
134-
execute_process(COMMAND ${SYSREPOCTL_EXECUTABLE} -i -g ${CMAKE_SOURCE_DIR}/../modules/ietf-keystore.yang -o root:root -p 600 RESULT_VARIABLE RET OUTPUT_VARIABLE OUT ERROR_VARIABLE OUT)
135-
if (RET)
136-
string(REPLACE \"\\n\" \"\\n \" OUT \"\${OUT}\")
137-
message(FATAL_ERROR \" Command sysrepoctl install failed:\\n \${OUT}\")
138-
endif()
115+
option(MODEL_INSTALL "Enable yang model installation" ON)
116+
if (MODEL_INSTALL)
117+
install(CODE "
118+
set(ENV{SYSREPOCTL} ${SYSREPOCTL_EXECUTABLE})
119+
set(ENV{SYSREPOCTL_ROOT_PERMS} SYSREPOCTL_ROOT_PERMS)
120+
execute_process(COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/scripts/model-install.sh)")
121+
endif()
139122

140-
else()
141-
message(STATUS \"Module ietf-keystore already in sysrepo.\")
142-
endif()")
123+
# Use KEYSTORED_DEFER_SSH_KEY=ON to skip automatic key conversion.
124+
# Some external build/deploy script is then responsible for providing an SSH
125+
# host key in a PEM format at runtime.
126+
if (NOT KEYSTORED_DEFER_SSH_KEY)
127+
set(KEYSTORED_CHECK_SSH_KEY 1)
128+
else()
129+
set(KEYSTORED_CHECK_SSH_KEY 0)
130+
endif()
143131

144-
# import stock OpenSSH RSA key
145-
install(CODE "
146-
execute_process(COMMAND ${SYSREPOCFG_EXECUTABLE} -d startup --export ietf-keystore RESULT_VARIABLE RET OUTPUT_VARIABLE OUT ERROR_VARIABLE OUT)
147-
if (RET)
148-
string(REPLACE \"\\n\" \"\\n \" OUT \"\${OUT}\")
149-
message(FATAL_ERROR \" Command sysrepocfg export failed:\\n \${OUT}\")
150-
endif()
132+
option(SSH_KEY_INSTALL "Enable ssh key import" ON)
133+
if (SSH_KEY_INSTALL)
134+
install(CODE "
135+
set(ENV{SYSREPOCFG} ${SYSREPOCFG_EXECUTABLE})
136+
set(ENV{CHMOD} ${CHMOD_EXECUTABLE})
137+
set(ENV{OPENSSL} ${OPENSSL_EXECUTABLE})
138+
set(ENV{KEYSTORED_KEYS_DIR} ${KEYSTORED_KEYS_DIR})
139+
set(ENV{KEYSTORED_CHECK_SSH_KEY} ${KEYSTORED_CHECK_SSH_KEY})
140+
execute_process(COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/scripts/ssh-key-import.sh)")
141+
endif()
151142

152-
if (OUT)
153-
message(STATUS \"Some ietf-keystore configuration set, no keys will be imported.\")
154-
elseif(NOT EXISTS \"/etc/ssh/ssh_host_rsa_key\")
155-
message(WARNING \"Default OpenSSH RSA host key \\\"/etc/ssh/ssh_host_rsa_key\\\" not found so a key will have to be imported or generated manually for netopeer2-server to use.\")
156-
else()
157-
message(STATUS \"Importing stock OpenSSH RSA key.\")
158-
file(READ /etc/ssh/ssh_host_rsa_key RSA_KEY)
159-
file(WRITE ${KEYSTORED_KEYS_DIR}/ssh_host_rsa_key.pem \${RSA_KEY})
160-
execute_process(COMMAND ${CHMOD_EXECUTABLE} go-rw ${KEYSTORED_KEYS_DIR}/ssh_host_rsa_key.pem)
161-
execute_process(COMMAND ${OPENSSL_EXECUTABLE} rsa -pubout -in ${KEYSTORED_KEYS_DIR}/ssh_host_rsa_key.pem -out ${KEYSTORED_KEYS_DIR}/ssh_host_rsa_key.pub.pem RESULT_VARIABLE RET OUTPUT_VARIABLE OUT ERROR_VARIABLE OUT)
162-
if (RET)
163-
string(REPLACE \"\\n\" \"\\n \" OUT \"\${OUT}\")
164-
message(FATAL_ERROR \" Command openssl generate public key failed:\\n \${OUT}\")
165-
endif()
166-
execute_process(COMMAND ${SYSREPOCFG_EXECUTABLE} -d startup -i ${CMAKE_SOURCE_DIR}/stock_key_config.xml ietf-keystore RESULT_VARIABLE RET OUTPUT_VARIABLE OUT ERROR_VARIABLE OUT)
167-
if (RET)
168-
string(REPLACE \"\\n\" \"\\n \" OUT \"\${OUT}\")
169-
message(FATAL_ERROR \" Command sysrepocfg import failed:\\n \${OUT}\")
170-
endif()
171-
endif()")
143+
add_custom_target(install-scripts-ide
144+
scripts/model-install.sh
145+
scripts/ssh-key-import.sh
146+
)
172147

173148
# plugins should be installed into sysrepo plugins dir
174149
install(TARGETS keystored DESTINATION ${SR_PLUGINS_DIR})

keystored/scripts/model-install.sh

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/bin/bash
2+
3+
set -eux -o pipefail
4+
5+
shopt -s failglob
6+
7+
local_path=$(dirname $0)
8+
9+
: ${SYSREPOCTL:=sysrepoctl}
10+
: ${SYSREPOCTL_ROOT_PERMS:=-o root:root -p 600}
11+
: ${YANG_DIR:=$local_path/../../modules}
12+
13+
install_yang_module() {
14+
module=$1
15+
16+
if ! $SYSREPOCTL -l | grep "$module[^|]*|[^|]*| Installed[^\\n]*"; then
17+
$SYSREPOCTL -i -g ${YANG_DIR}/$module.yang $SYSREPOCTL_ROOT_PERMS
18+
fi
19+
}
20+
21+
install_yang_module ietf-x509-cert-to-name
22+
install_yang_module ietf-keystore

keystored/scripts/ssh-key-import.sh

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/bin/bash
2+
3+
set -eux -o pipefail
4+
5+
shopt -s failglob
6+
7+
local_path=$(dirname $0)
8+
9+
: ${SYSREPOCFG:=sysrepocfg}
10+
: ${CHMOD:=chmod}
11+
: ${OPENSSL:=openssl}
12+
: ${STOCK_KEY_CONFIG:=$local_path/../stock_key_config.xml}
13+
: ${KEYSTORED_KEYS_DIR:=/etc/keystored/keys}
14+
15+
if [ -n "$($SYSREPOCFG -d startup --export ietf-keystore)" ]; then
16+
exit 0
17+
fi
18+
19+
if [ $KEYSTORED_CHECK_SSH_KEY -eq 0 ]; then
20+
echo "Warning: Assuming that an external script will provide the SSH key in a PEM format at \"${KEYSTORED_KEYS_DIR}/ssh_host_rsa_key.pem\"."
21+
$SYSREPOCFG -d startup -i ${STOCK_KEY_CONFIG} ietf-keystore
22+
elif [ -r /etc/ssh/ssh_host_rsa_key ]; then
23+
cp /etc/ssh/ssh_host_rsa_key ${KEYSTORED_KEYS_DIR}/ssh_host_rsa_key.pem
24+
$CHMOD go-rw ${KEYSTORED_KEYS_DIR}/ssh_host_rsa_key.pem
25+
$OPENSSL rsa -pubout -in ${KEYSTORED_KEYS_DIR}/ssh_host_rsa_key.pem \
26+
-out ${KEYSTORED_KEYS_DIR}/ssh_host_rsa_key.pub.pem
27+
$SYSREPOCFG -d startup -i ${STOCK_KEY_CONFIG} ietf-keystore
28+
else
29+
echo "Warning: Cannot read the SSH hostkey at /etc/ssh/ssh_host_rsa_key, skipping"
30+
fi

0 commit comments

Comments
 (0)