Commit 972f4acc authored by Jakob Olsson's avatar Jakob Olsson

add gitlab-ci unit tests

parent 73811f1d
Pipeline #930 passed with stages
in 1 minute and 15 seconds
include:
- project: 'iopsys/gitlab-ci-pipeline'
file: '/static-code-analysis.yml'
stages:
- static_code_analysis
- unit_test
variables:
DEBUG: 'TRUE'
SOURCE_FOLDER: "."
run_unit_test:
stage: unit_test
image: iopsys/code-analysis:0.13
allow_failure: true
script:
- "./gitlab-ci/setup.sh"
- "./gitlab-ci/unit-test.sh"
artifacts:
when: always
paths:
- unit-coverage.tar.gz
......@@ -2,10 +2,12 @@ cmake_minimum_required(VERSION 2.6)
INCLUDE(CheckFunctionExists)
PROJECT(rpcd C)
PROJECT(rpcd)
ADD_DEFINITIONS(-Os -Wall -Werror --std=gnu99 -g3 -Wmissing-declarations)
INCLUDE_DIRECTORIES(include)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/modules")
OPTION(FILE_SUPPORT "File plugin support" ON)
OPTION(IWINFO_SUPPORT "libiwinfo plugin support" ON)
OPTION(RPCSYS_SUPPORT "rpc-sys plugin support" ON)
......@@ -34,7 +36,17 @@ INCLUDE_DIRECTORIES(${ubus_include_dir})
FIND_PATH(ubox_include_dir libubox/blobmsg_json.h)
INCLUDE_DIRECTORIES(${ubox_include_dir})
ADD_EXECUTABLE(rpcd main.c exec.c session.c uci.c plugin.c login_guard.c uci_granular.c)
set(SOURCES
main.c
exec.c
session.c
uci.c
plugin.c
login_guard.c
uci_granular.c
)
ADD_EXECUTABLE(rpcd ${SOURCES} )
TARGET_LINK_LIBRARIES(rpcd ubox ubus uci dl blobmsg_json ${json} ${crypt})
SET(PLUGINS "")
......@@ -60,6 +72,29 @@ IF (IWINFO_SUPPORT)
SET_TARGET_PROPERTIES(iwinfo_plugin PROPERTIES OUTPUT_NAME iwinfo PREFIX "")
ENDIF()
IF (CMAKE_BUILD_TYPE STREQUAL Debug)
OPTION(ENABLE_BUILD_TESTS "Build tests" ON)
OPTION(ENABLE_VALGRIND_TESTS "Build tests with valgrind" ON)
ELSE()
OPTION(ENABLE_BUILD_TESTS "Build tests" OFF)
OPTION(ENABLE_VALGRIND_TESTS "Build tests with valgrind" OFF)
ENDIF()
IF(ENABLE_BUILD_TESTS)
FIND_PACKAGE(CMocka)
if(CMOCKA_FOUND)
INCLUDE(CodeCoverage)
SET(COVERAGE_EXCLUDES '*/main.c' '/usr/include/*' '*/unit_tests_*.c' '*/functional_tests_*.c' '*/exec.c' '*/file.c' '*/login_guard.c' '*/plugin.c' '*/sys.c' ''*/session.c)
APPEND_COVERAGE_COMPILER_FLAGS()
ADD_LIBRARY(${PROJECT_NAME}-api SHARED ${SOURCES})
MESSAGE("-- Building tests")
ENABLE_TESTING()
ADD_SUBDIRECTORY(test/cmocka)
ELSE(CMOCKA_FOUND)
MESSAGE("-- CMocka not found")
ENDIF(CMOCKA_FOUND)
ENDIF(ENABLE_BUILD_TESTS)
INSTALL(TARGETS rpcd ${PLUGINS}
RUNTIME DESTINATION sbin
LIBRARY DESTINATION lib
......
FROM ubuntu:16.04
LABEL maintainer="jakob.olsson@iopsys.eu"
LABEL build="docker build -t iopsys/rpcd ."
LABEL run="docker run -d --name rpcd --privileged --rm -v ${PWD}:/opt/work -p 2222:22 -e LOCAL_USER_ID=`id -u $USER` iopsys/rpcd:latest"
LABEL exec="docker exec --user=user -it rpcd bash"
LABEL stop="docker stop rpcd"
RUN \
apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y \
# general tools
git \
cmake \
wget \
build-essential \
lcov \
apt-utils \
autoconf \
automake \
pkg-config \
libtool \
vim \
valgrind \
gdb \
cppcheck \
python3 \
python3-setuptools \
openssh-server \
clang-format \
sudo \
strace \
supervisor \
net-tools \
iputils-ping
# Configure OpenSSH server
RUN mkdir /var/run/sshd
RUN echo 'root:root' | chpasswd
RUN sed -i 's/PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config
RUN sed -i 's/PermitEmptyPasswords no/PermitEmptyPasswords yes/' /etc/ssh/sshd_config
RUN sed 's@session\s*required\s*pam_loginuid.so@session optional pam_loginuid.so@g' -i /etc/pam.d/sshd
# Configure gdb-dashboard
RUN \
easy_install3 pip && \
pip3 install Pygments
RUN wget -P ~ git.io/.gdbinit
RUN \
mkdir ~/.gdbinit.d && \
touch ~/.gdbinit.d/init && \
echo "dashboard -layout source" >> ~/.gdbinit.d/init && \
echo "dashboard source -style context 20" >> ~/.gdbinit.d/init && \
echo "dashboard -style syntax_highlighting 'monokai'" >> ~/.gdbinit.d/init
# Install dependent libraries
RUN \
apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y \
lua5.1-dev \
lua5.1 \
#libjson0 \
#libjson0-dev \
libssl-dev \
libuv1-dev \
cmocka-doc \
libcmocka-dev \
libcmocka0
# Install CMocka
#RUN \
# git clone --branch cmocka-1.1.1 git://git.cryptomilk.org/projects/cmocka.git && \
# cd cmocka && \
# mkdir build && \
# cd build && \
# cmake .. && \
# make && \
# make install
# Remove cached packages.
RUN rm -rf /var/lib/apt/lists/*
RUN mkdir /opt/dev
# Install JSON-C
RUN \
cd /opt/dev && \
git clone https://github.com/json-c/json-c.git && \
cd json-c && \
sh autogen.sh && \
./configure && \
make && \
make install && \
sudo ldconfig
# ubox
RUN \
cd /opt/dev && \
git clone git://git.openwrt.org/project/libubox.git && \
cd libubox && mkdir build && cd build && \
git checkout ecf56174da9614a0b3107d33def463eefb4d7785 && \
cmake -DCMAKE_INSTALL_PREFIX=/usr -DCMAKE_BUILD_TYPE:String="Release" .. && \
make -j2 && \
make install
# uci
RUN \
cd /opt/dev && \
git clone https://git.openwrt.org/project/uci.git && \
cd uci && \
git checkout f199b961c2970b63cc83947ad49b327b3f48f05f && \
cmake -DCMAKE_INSTALL_PREFIX=/usr -DCMAKE_BUILD_TYPE:String="Release" -DBUILD_LUA=OFF . && \
make -j2 && \
make install
# ubus
RUN \
cd /opt/dev && \
git clone https://git.openwrt.org/project/ubus.git && \
cd ubus && \
git checkout 221ce7e7ff1bd1a0c9995fa9d32f58e865f7207f && \
cmake -DCMAKE_INSTALL_PREFIX=/usr -DCMAKE_BUILD_TYPE:String="Release" -DBUILD_LUA=OFF -DBUILD_EXAMPLES=OFF . && \
make -j2 && \
make install
# rpcd
#RUN \
# cd /opt/dev && \
# git clone https://git.openwrt.org/project/rpcd.git && \
# cd rpcd && \
# git checkout cfe1e75c91bc1bac82e6caab3e652b0ebee59524 && \
# cmake -DCMAKE_INSTALL_PREFIX=/usr -DCMAKE_BUILD_TYPE:String="Release" -DIWINFO_SUPPORT=NO . && \
# make -j2 && \
# make install && \
# mkdir /usr/lib/rpcd && \
# cp file.so /usr/lib/rpcd
# json-editor
RUN \
cd /opt/dev && \
git clone https://dev.iopsys.eu/iopsys/json-editor.git && \
cd json-editor && \
git checkout 44b32937a062ec4ffc9f7355841dc94ab6efa50f && \
cmake -DCMAKE_INSTALL_PREFIX=/usr -DCMAKE_BUILD_TYPE:String="Release" . && \
make && \
make install
RUN mkdir /etc/config
WORKDIR /opt/work
# Expose ports
EXPOSE 22
# Prepare supervisor
RUN mkdir -p /var/log/supervisor
COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf
RUN mkdir -p /etc/config/
COPY test/files/etc/config/* /etc/config/
RUN mkdir -p /usr/share/rpcd/acl.d
COPY test/files/usr/share/rpcd/acl.d/* /usr/share/rpcd/acl.d/
# Start entrypoint
COPY entrypoint.sh /usr/local/bin/entrypoint.sh
ENTRYPOINT ["/usr/local/bin/entrypoint.sh"]
# Copyright (c) 2012 - 2017, Lars Bilke
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification,
# are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# 3. Neither the name of the copyright holder nor the names of its contributors
# may be used to endorse or promote products derived from this software without
# specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
# ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# CHANGES:
#
# 2012-01-31, Lars Bilke
# - Enable Code Coverage
#
# 2013-09-17, Joakim Söderberg
# - Added support for Clang.
# - Some additional usage instructions.
#
# 2016-02-03, Lars Bilke
# - Refactored functions to use named parameters
#
# 2017-06-02, Lars Bilke
# - Merged with modified version from github.com/ufz/ogs
#
#
# USAGE:
#
# 1. Copy this file into your cmake modules path.
#
# 2. Add the following line to your CMakeLists.txt:
# include(CodeCoverage)
#
# 3. Append necessary compiler flags:
# APPEND_COVERAGE_COMPILER_FLAGS()
#
# 4. If you need to exclude additional directories from the report, specify them
# using the COVERAGE_EXCLUDES variable before calling SETUP_TARGET_FOR_COVERAGE.
# Example:
# set(COVERAGE_EXCLUDES 'dir1/*' 'dir2/*')
#
# 5. Use the functions described below to create a custom make target which
# runs your test executable and produces a code coverage report.
#
# 6. Build a Debug build:
# cmake -DCMAKE_BUILD_TYPE=Debug ..
# make
# make my_coverage_target
#
include(CMakeParseArguments)
# Check prereqs
find_program( GCOV_PATH gcov )
find_program( LCOV_PATH NAMES lcov lcov.bat lcov.exe lcov.perl)
find_program( GENHTML_PATH NAMES genhtml genhtml.perl genhtml.bat )
find_program( GCOVR_PATH gcovr PATHS ${CMAKE_SOURCE_DIR}/scripts/test)
find_program( SIMPLE_PYTHON_EXECUTABLE python )
if(NOT GCOV_PATH)
message(FATAL_ERROR "gcov not found! Aborting...")
endif() # NOT GCOV_PATH
if("${CMAKE_CXX_COMPILER_ID}" MATCHES "(Apple)?[Cc]lang")
if("${CMAKE_CXX_COMPILER_VERSION}" VERSION_LESS 3)
message(FATAL_ERROR "Clang version must be 3.0.0 or greater! Aborting...")
endif()
elseif(NOT CMAKE_COMPILER_IS_GNUCXX)
message(FATAL_ERROR "Compiler is not GNU gcc! Aborting...")
endif()
set(COVERAGE_COMPILER_FLAGS "-g -O0 --coverage -fprofile-arcs -ftest-coverage"
CACHE INTERNAL "")
set(CMAKE_CXX_FLAGS_COVERAGE
${COVERAGE_COMPILER_FLAGS}
CACHE STRING "Flags used by the C++ compiler during coverage builds."
FORCE )
set(CMAKE_C_FLAGS_COVERAGE
${COVERAGE_COMPILER_FLAGS}
CACHE STRING "Flags used by the C compiler during coverage builds."
FORCE )
set(CMAKE_EXE_LINKER_FLAGS_COVERAGE
""
CACHE STRING "Flags used for linking binaries during coverage builds."
FORCE )
set(CMAKE_SHARED_LINKER_FLAGS_COVERAGE
""
CACHE STRING "Flags used by the shared libraries linker during coverage builds."
FORCE )
mark_as_advanced(
CMAKE_CXX_FLAGS_COVERAGE
CMAKE_C_FLAGS_COVERAGE
CMAKE_EXE_LINKER_FLAGS_COVERAGE
CMAKE_SHARED_LINKER_FLAGS_COVERAGE )
if(NOT CMAKE_BUILD_TYPE STREQUAL "Debug")
message(WARNING "Code coverage results with an optimised (non-Debug) build may be misleading")
endif() # NOT CMAKE_BUILD_TYPE STREQUAL "Debug"
if(CMAKE_C_COMPILER_ID STREQUAL "GNU")
link_libraries(gcov)
else()
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} --coverage")
endif()
# Defines a target for running and collection code coverage information
# Builds dependencies, runs the given executable and outputs reports.
# NOTE! The executable should always have a ZERO as exit code otherwise
# the coverage generation will not complete.
#
# SETUP_TARGET_FOR_COVERAGE(
# NAME testrunner_coverage # New target name
# EXECUTABLE testrunner -j ${PROCESSOR_COUNT} # Executable in PROJECT_BINARY_DIR
# DEPENDENCIES testrunner # Dependencies to build first
# )
function(SETUP_TARGET_FOR_COVERAGE)
set(options NONE)
set(oneValueArgs NAME)
set(multiValueArgs EXECUTABLE EXECUTABLE_ARGS DEPENDENCIES)
cmake_parse_arguments(Coverage "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
if(NOT LCOV_PATH)
message(FATAL_ERROR "lcov not found! Aborting...")
endif() # NOT LCOV_PATH
if(NOT GENHTML_PATH)
message(FATAL_ERROR "genhtml not found! Aborting...")
endif() # NOT GENHTML_PATH
# Setup target
add_custom_target(${Coverage_NAME}
# Cleanup lcov
COMMAND ${LCOV_PATH} --rc lcov_branch_coverage=1 --directory . --zerocounters
# Create baseline to make sure untouched files show up in the report
COMMAND ${LCOV_PATH} --rc lcov_branch_coverage=1 -c -i -d . -o ${Coverage_NAME}.base
# Run tests
COMMAND ${Coverage_EXECUTABLE}
# Capturing lcov counters and generating report
COMMAND ${LCOV_PATH} --rc lcov_branch_coverage=1 --directory . --capture --output-file ${Coverage_NAME}.info
# add baseline counters
COMMAND ${LCOV_PATH} --rc lcov_branch_coverage=1 -a ${Coverage_NAME}.base -a ${Coverage_NAME}.info --output-file ${Coverage_NAME}.total
COMMAND ${LCOV_PATH} --rc lcov_branch_coverage=1 --remove ${Coverage_NAME}.total ${COVERAGE_EXCLUDES} --output-file ${PROJECT_BINARY_DIR}/${Coverage_NAME}.info.cleaned
COMMAND ${GENHTML_PATH} --legend --branch-coverage -o ${Coverage_NAME} ${PROJECT_BINARY_DIR}/${Coverage_NAME}.info.cleaned
COMMAND ${CMAKE_COMMAND} -E remove ${Coverage_NAME}.base ${Coverage_NAME}.info ${Coverage_NAME}.total ${PROJECT_BINARY_DIR}/${Coverage_NAME}.info.cleaned
WORKING_DIRECTORY ${PROJECT_BINARY_DIR}
DEPENDS ${Coverage_DEPENDENCIES}
COMMENT "Resetting code coverage counters to zero.\nProcessing code coverage counters and generating report."
)
# Show info where to find the report
add_custom_command(TARGET ${Coverage_NAME} POST_BUILD
COMMAND ;
COMMENT "Open ./${Coverage_NAME}/index.html in your browser to view the coverage report."
)
endfunction() # SETUP_TARGET_FOR_COVERAGE
# Defines a target for running and collection code coverage information
# Builds dependencies, runs the given executable and outputs reports.
# NOTE! The executable should always have a ZERO as exit code otherwise
# the coverage generation will not complete.
#
# SETUP_TARGET_FOR_COVERAGE_COBERTURA(
# NAME ctest_coverage # New target name
# EXECUTABLE ctest -j ${PROCESSOR_COUNT} # Executable in PROJECT_BINARY_DIR
# DEPENDENCIES executable_target # Dependencies to build first
# )
function(SETUP_TARGET_FOR_COVERAGE_COBERTURA)
set(options NONE)
set(oneValueArgs NAME)
set(multiValueArgs EXECUTABLE EXECUTABLE_ARGS DEPENDENCIES)
cmake_parse_arguments(Coverage "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
if(NOT SIMPLE_PYTHON_EXECUTABLE)
message(FATAL_ERROR "python not found! Aborting...")
endif() # NOT SIMPLE_PYTHON_EXECUTABLE
if(NOT GCOVR_PATH)
message(FATAL_ERROR "gcovr not found! Aborting...")
endif() # NOT GCOVR_PATH
# Combine excludes to several -e arguments
set(COBERTURA_EXCLUDES "")
foreach(EXCLUDE ${COVERAGE_EXCLUDES})
set(COBERTURA_EXCLUDES "-e ${EXCLUDE} ${COBERTURA_EXCLUDES}")
endforeach()
add_custom_target(${Coverage_NAME}
# Run tests
${Coverage_EXECUTABLE}
# Running gcovr
COMMAND ${GCOVR_PATH} -x -r ${CMAKE_SOURCE_DIR} ${COBERTURA_EXCLUDES}
-o ${Coverage_NAME}.xml
WORKING_DIRECTORY ${PROJECT_BINARY_DIR}
DEPENDS ${Coverage_DEPENDENCIES}
COMMENT "Running gcovr to produce Cobertura code coverage report."
)
# Show info where to find the report
add_custom_command(TARGET ${Coverage_NAME} POST_BUILD
COMMAND ;
COMMENT "Cobertura code coverage report saved in ${Coverage_NAME}.xml."
)
endfunction() # SETUP_TARGET_FOR_COVERAGE_COBERTURA
function(APPEND_COVERAGE_COMPILER_FLAGS)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${COVERAGE_COMPILER_FLAGS}" PARENT_SCOPE)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${COVERAGE_COMPILER_FLAGS}" PARENT_SCOPE)
message(STATUS "Appending code coverage compiler flags: ${COVERAGE_COMPILER_FLAGS}")
endfunction() # APPEND_COVERAGE_COMPILER_FLAGS
\ No newline at end of file
# CMOCKA_FOUND - System has CMocka
# CMOCKA_INCLUDE_DIRS - The CMocka include directories
# CMOCKA_LIBRARIES - The libraries needed to use CMocka
# CMOCKA_DEFINITIONS - Compiler switches required for using CMocka
find_package(PkgConfig QUIET)
if(PKG_CONFIG_FOUND)
pkg_check_modules(PC_CMOCKA QUIET cmocka)
set(CMOCKA_DEFINITIONS ${PC_CMOCKA_CFLAGS_OTHER})
endif()
find_path(CMOCKA_INCLUDE_DIR cmocka.h
HINTS ${PC_CMOCKA_INCLUDEDIR} ${PC_CMOCKA_INCLUDE_DIRS}
PATH_SUFFIXES cmocka)
find_library(CMOCKA_LIBRARY NAMES cmocka
HINTS ${PC_CMOCKA_LIBDIR} ${PC_CMOCKA_LIBRARY_DIRS})
set(CMOCKA_LIBRARIES ${CMOCKA_LIBRARY})
set(CMOCKA_INCLUDE_DIRS ${CMOCKA_INCLUDE_DIR})
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(cmocka DEFAULT_MSG
CMOCKA_LIBRARY CMOCKA_INCLUDE_DIR)
mark_as_advanced(CMOCKA_INCLUDE_DIR CMOCKA_LIBRARY)
#!/bin/bash
# Create user
USER_ID=${LOCAL_USER_ID:-9001}
useradd --shell /bin/bash -u $USER_ID -G sudo -o -c "" -m user
adduser --disabled-password user
export HOME=/home/user
echo "user ALL = NOPASSWD : ALL" >> /etc/sudoers
# Configure OpenSSH to allow login without password
sed -i -re 's/^user:[^:]+:/user::/' /etc/passwd /etc/shadow
pam_config="auth [success=1 default=ignore] pam_unix.so nullok\nauth requisite pam_deny.so\nauth required pam_permit.so"
sed -i "s/@include common-auth/$pam_config/" /etc/pam.d/sshd
# Start supervisor
/usr/bin/supervisord -c /etc/supervisor/conf.d/supervisord.conf -l /var/log/supervisord.log -j /var/run/supervisord.pid
#!/bin/bash
echo "preparation script"
pwd
adduser --disabled-password --gecos "" user
echo "user:user"|chpasswd
adduser --disabled-password --gecos "" admin
echo "admin:admin"|chpasswd
mkdir build
pushd build
cmake .. -DCMAKE_BUILD_TYPE=Debug -DIWINFO_SUPPORT=OFF
make
make install
popd
cp -r ./test/cmocka/files/* /
mkdir -p /var/run/rpcd
#!/bin/bash
echo "preparation script"
pwd
make unit-test -C ./build
#report part
#GitLab-CI output
make unit-coverage -C ./build
tar -zcvf unit-coverage.tar.gz ./build/unit-coverage
......@@ -30,6 +30,8 @@
#include <libubox/avl.h>
#include <libubox/blobmsg_json.h>
#include <rpcd/uci.h>
#define RPC_SID_LEN 32
#define RPC_DEFAULT_SESSION_TIMEOUT 300
#define RPC_DEFAULT_SESSION_ID "00000000000000000000000000000000"
......@@ -84,4 +86,18 @@ void rpc_session_thaw(void);
const char *rpc_session_get_username(struct blob_attr *sid);
struct rpc_session *rpc_session_get(const char *id);
struct rpc_session *rpc_session_create(int timeout);
void rpc_session_set(struct rpc_session *ses, const char *key, struct blob_attr *val);
void rpc_login_setup_acls(struct rpc_session *ses, struct uci_section *login);
int rpc_handle_login(struct ubus_context *ctx, struct ubus_object *obj,
struct ubus_request_data *req, const char *method,
struct blob_attr *msg);
void rpc_login_setup_acls(struct rpc_session *ses, struct uci_section *login);
struct uci_section *rpc_login_test_login(struct uci_context *uci,
const char *username, const char *password, const char *listen_source);
bool rpc_user_login_enabled(const char *username);
void rpc_session_destroy(struct rpc_session *ses);
void rpc_init_default_session(void);
#endif
......@@ -41,4 +41,32 @@ int rpc_uci_api_init(struct ubus_context *ctx);
void rpc_uci_purge_savedirs(void);
struct uci_granular_ctx_s *rpc_uci_load_granular(const char *username,
const char *config_name, const char *section_name,
const char *section_type, const char *sid);
int rpc_uci_check_granular_is_allowed(const char *option);
struct blob_buf *rpc_uci_get_glob_bb(void);
int rpc_uci_dump_package(struct uci_package *p, const char *name,
struct blob_attr *type, struct blob_attr *matches, const char *username,
const char *sid);
int rpc_uci_dump_section(struct uci_section *s, const char *name, int index, const char *username);
int rpc_uci_dump_option(struct uci_option *o, const char *name);
void rpc_uci_merge_set(struct blob_attr *opt, struct uci_ptr *ptr, const char *username);
int rpc_uci_init_uci(void);
void rpc_uci_commit_cursor(struct uci_package *p);
int rpc_uci_set(struct ubus_context *ctx, struct ubus_object *obj,
struct ubus_request_data *req, const char *method,
struct blob_attr *msg);
int rpc_uci_revert_commit(struct ubus_context *ctx, struct blob_attr *msg,
bool commit);
int rpc_uci_add(struct ubus_context *ctx, struct ubus_object *obj,
struct ubus_request_data *req, const char *method,
struct blob_attr *msg);
int rpc_uci_delete(struct ubus_context *ctx, struct ubus_object *obj,
struct ubus_request_data *req, const char *method,
struct blob_attr *msg);
int rpc_uci_rename(struct ubus_context *ctx, struct ubus_object *obj,
struct ubus_request_data *req, const char *method,
struct blob_attr *msg);
void rpc_uci_clean(void);
#endif
......@@ -83,3 +83,5 @@ int rpc_uci_granular_init();
int rpc_uci_granular_setup(struct rpc_session *ses, struct blob_attr *acl_top, const char *username);
void rpc_uci_granular_get_allowed(struct rpc_session *ses, struct uci_granular_query_s *query, struct uci_granular_ctx_s *granular_ctx);
void uci_granular_exit(struct rpc_session *ses);
int uci_granular_parse_match_definition(struct blob_attr *acl_match_definition, struct match_definition_s *match_definition);
int rpc_uci_granular_destroy(void);
......@@ -24,8 +24,8 @@
#include <signal.h>
#include <sys/stat.h>
#include <rpcd/session.h>
#include <rpcd/uci.h>
#include <rpcd/session.h>
#include <rpcd/plugin.h>
#include <rpcd/exec.h>
#include <rpcd/uci_granular.h>
......
......@@ -260,7 +260,7 @@ rpc_touch_session(struct rpc_session *ses)
uloop_timeout_set(&ses->t, ses->timeout * 1000);
}
static void
void
rpc_session_destroy(struct rpc_session *ses)
{
struct rpc_session_acl *acl, *nacl;
......@@ -318,7 +318,7 @@ rpc_session_new(void)
return ses;
}
static struct rpc_session *
struct rpc_session *
rpc_session_create(int timeout)
{
struct rpc_session *ses;
......@@ -633,7 +633,7 @@ rpc_handle_access(struct ubus_context *ctx, struct ubus_object *obj,
return 0;
}
static void
void
rpc_session_set(struct rpc_session *ses, const char *key, struct blob_attr *val)
{
struct rpc_session_data *data;
......@@ -832,7 +832,7 @@ rpc_login_test_password(const char *hash, const char *password)
return !strcmp(crypt_hash, hash);
}
static struct uci_section *
struct uci_section *
rpc_login_test_login(struct uci_context *uci,
const char *username, const char *password, const char *listen_source)
{
......@@ -1118,7 +1118,7 @@ out:
blob_buf_free(&acl);
}
static void
void
rpc_login_setup_acls(struct rpc_session *ses, struct uci_section *login)
{
int i;
......@@ -1133,7 +1133,7 @@ rpc_login_setup_acls(struct rpc_session *ses, struct uci_section *login)
globfree(&gl);
}
static bool
bool
rpc_user_login_enabled(const char *username)
{
struct uci_package *p = NULL;
......@@ -1176,7 +1176,7 @@ out:
return enabled;
}
static int
int
rpc_handle_login(struct ubus_context *ctx, struct ubus_object *obj,
struct ubus_request_data *req, const char *method,
struct blob_attr *msg)
......@@ -1370,10 +1370,25 @@ rpc_session_from_blob(struct uci_context *uci, struct blob_attr *attr)
return true;
}
int rpc_session_api_init(struct ubus_context *ctx)
void rpc_init_default_session(void)
{
struct rpc_session *ses;
avl_init(&sessions, avl_strcmp, false, NULL);
/* setup the default session */
ses = rpc_session_new();
if (ses) {
strcpy(ses->id, RPC_DEFAULT_SESSION_ID);
rpc_login_setup_acls(ses, NULL);
avl_insert(&sessions, &ses->avl);
rpc_uci_granular_init(ses);
}
}