Skip to content

Commit 1500bf2

Browse files
authored
Merge pull request #14 from vijs/feature/cli
Initial version of auto renew daemon
2 parents 99c7fbe + fec70f0 commit 1500bf2

File tree

12 files changed

+939
-10
lines changed

12 files changed

+939
-10
lines changed

CMakeLists.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ cmake_policy(SET CMP0048 NEW)
88
# Increment MINOR when API/ABI is backward compatible but different (e.g., new features added)
99
# Increment PATCH when API is unchanged (bug/internal fixes)
1010
set(MAJOR 2)
11-
set(MINOR 1)
11+
set(MINOR 2)
1212
set(PATCH 1)
1313
set(VERSION ${MAJOR}.${MINOR}.${PATCH})
1414
set(CMAKE_XCODE_GENERATE_SCHEME ON)
@@ -26,6 +26,7 @@ option(ENABLE_CMOCKA "Enables CMOCKA for unit tests (requires cmocka)" OFF)
2626
option(ENABLE_COVERAGE "Collect code coverage report with unit tests" OFF)
2727
option(ENABLE_MBEDTLS "Build with mBedTLS support instead of OpenSSL" OFF)
2828
option(ENABLE_MATTER_EXAMPLES "Build Matter SDK libCertifier Examples" OFF)
29+
option(SYSTEMV_DAEMON "Install libCertifier Daemon with SysV Support" OFF)
2930

3031
option(ENABLE_CMAKE_VERBOSE_MAKEFILE OFF)
3132

@@ -246,6 +247,7 @@ set(MEMORYCHECK_COMMAND_OPTIONS "--trace-children=yes --leak-check=full")
246247
message(STATUS "extra cflags: ${CMAKE_C_FLAGS}")
247248

248249
add_subdirectory(tests/keymgr)
250+
add_subdirectory(daemon)
249251

250252
if (${ENABLE_TESTS})
251253
project(certifierTests)
@@ -368,6 +370,7 @@ install(TARGETS certifier LIBRARY DESTINATION lib)
368370
install(DIRECTORY include/certifier DESTINATION include)
369371
install(TARGETS certifierUtil RUNTIME DESTINATION bin)
370372
install(FILES libcertifier.cfg.sample DESTINATION etc/certifier RENAME libcertifier.cfg)
373+
install(FILES libcertifier-cert.crt DESTINATION etc/certifier)
371374

372375
if (ENABLE_TESTS)
373376
install(TARGETS certifierTests RUNTIME DESTINATION bin)

daemon/CMakeLists.txt

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
project(certifierd)
2+
3+
file(GLOB SOURCES "*.c")
4+
5+
add_executable(${PROJECT_NAME} ${SOURCES})
6+
target_link_libraries(${PROJECT_NAME} certifier)
7+
target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_SOURCE_DIR}/include ${CMAKE_SOURCE_DIR}/internal_headers)
8+
9+
# The rule to install daemon binary
10+
install (TARGETS ${PROJECT_NAME} RUNTIME DESTINATION bin)
11+
12+
# Directory with systemd unit files
13+
set (SYSTEMD_UNIT_DIR "/usr/lib/systemd/system")
14+
15+
set (SYSTEMV_UNIT_DIR "/etc/init.d")
16+
17+
# Default directory for log file
18+
set (DAEMON_LOG_DIR "/var/log/certifier")
19+
20+
# Default directory for PID file
21+
set (DAEMON_PID_DIR "/run/certifier")
22+
23+
# Default directory for certificates
24+
set (DAEMON_CERTS_DIR "/etc/certifier/certificates")
25+
26+
# Macro for installing configuration files
27+
function(install_conf src dest)
28+
if(NOT IS_ABSOLUTE "${src}")
29+
set(src "${CMAKE_CURRENT_SOURCE_DIR}/${src}")
30+
endif()
31+
get_filename_component(src_name "${src}" NAME)
32+
if (NOT IS_ABSOLUTE "${dest}")
33+
set(dest "${CMAKE_INSTALL_PREFIX}/${dest}")
34+
endif()
35+
install(CODE "
36+
if(NOT EXISTS \"\$ENV{DESTDIR}${dest}/${src_name}\")
37+
#file(INSTALL \"${src}\" DESTINATION \"${dest}\")
38+
message(STATUS \"Installing: \$ENV{DESTDIR}${dest}/${src_name}\")
39+
execute_process(COMMAND \${CMAKE_COMMAND} -E copy \"${src}\"
40+
\"\$ENV{DESTDIR}${dest}/${src_name}\"
41+
RESULT_VARIABLE copy_result
42+
ERROR_VARIABLE error_output)
43+
if(copy_result)
44+
message(FATAL_ERROR \${error_output})
45+
endif()
46+
else()
47+
message(STATUS \"Skipping : \$ENV{DESTDIR}${dest}/${src_name}\")
48+
endif()
49+
")
50+
endfunction(install_conf)
51+
52+
if (NOT ${SYSTEMV_DAEMON})
53+
# Install systemd unit files
54+
install_conf (./certifierd.service ${SYSTEMD_UNIT_DIR})
55+
elseif(${SYSTEMV_DAEMON})
56+
set(PROGRAM_PERMISSIONS_DEFAULT
57+
OWNER_WRITE OWNER_READ OWNER_EXECUTE
58+
GROUP_READ GROUP_EXECUTE
59+
WORLD_READ WORLD_EXECUTE)
60+
61+
# Install systemv unit files
62+
install(FILES certifierd.init DESTINATION ${SYSTEMV_UNIT_DIR} PERMISSIONS ${PROGRAM_PERMISSIONS_DEFAULT} RENAME certifierd)
63+
64+
install(CODE "set(CMAKE_INSTALL_PREFIX \"${CMAKE_INSTALL_PREFIX}\")")
65+
install(SCRIPT InstallScript.cmake ${SYSTEMV_UNIT_DIR})
66+
endif()
67+
68+
# Create empty directory for default log file
69+
install(DIRECTORY DESTINATION ${DAEMON_LOG_DIR})
70+
71+
# Create empty directory for default PID file
72+
install(DIRECTORY DESTINATION ${DAEMON_PID_DIR})
73+
74+
# Create empty directory for certificates to be renewed
75+
install(DIRECTORY DESTINATION ${DAEMON_CERTS_DIR})

daemon/InstallScript.cmake

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
if("${CMAKE_INSTALL_PREFIX}" STREQUAL "/")
2+
execute_process(COMMAND update-rc.d certifierd defaults
3+
RESULT_VARIABLE Result
4+
OUTPUT_VARIABLE Output
5+
ERROR_VARIABLE Error)
6+
if(Result EQUAL 0)
7+
message(STATUS "Ran update-rc.d as CMAKE_INSTALL_PREFIX == \"/\"")
8+
else()
9+
message(FATAL_ERROR "Result - ${Result}\nOutput - ${Output}\nError - Error")
10+
endif()
11+
else()
12+
message(STATUS "Not running update-rc.d as CMAKE_INSTALL_PREFIX != \"/\"")
13+
endif()

daemon/certifierd.init

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
#!/bin/sh
2+
#
3+
# /etc/init.d/certifierd
4+
#
5+
# Init script for Certifier daemon
6+
#
7+
# chkconfig: 2345 20 80
8+
# description: LibCertifier Daemon that renews certificates automatically once a day
9+
10+
### BEGIN INIT INFO
11+
# Provides: certifierd
12+
# Required-Start: $rsyslog
13+
# Required-Stop:
14+
# Should-Start:
15+
# Should-Stop:
16+
# Default-Start: 2 3 4 5
17+
# Default-Stop: 0 1 6
18+
# Short-Description: start and stop example of daemon
19+
# Description: Example of UNIX daemon
20+
### END INIT INFO
21+
22+
# Source function library.
23+
. /lib/lsb/init-functions
24+
25+
prog="certifierd"
26+
app="/usr/bin/$prog"
27+
lock_file="/var/lock/subsys/$prog"
28+
log_file="/var/log/$prog.log"
29+
conf_file="/etc/certifier/libcertifier.cfg"
30+
31+
start() {
32+
echo -n $"Starting $prog: "
33+
start_daemon -p $lock_file $app --conf-file $conf_file --log-file $log_file --daemon
34+
RETVAL=$?
35+
[ $RETVAL -eq 0 ] && touch $lock_file
36+
echo
37+
return $RETVAL
38+
}
39+
40+
stop() {
41+
echo -n $"Stopping $prog: "
42+
killproc -p $lock_file $prog -INT
43+
RETVAL=$?
44+
echo
45+
[ $RETVAL -eq 0 ] && rm -f $lock_file
46+
return $RETVAL
47+
}
48+
49+
restart() {
50+
stop
51+
start
52+
}
53+
54+
reload() {
55+
restart
56+
}
57+
58+
force_reload() {
59+
restart
60+
}
61+
62+
rh_status() {
63+
status_of_proc -p $lock_file "$prog process"
64+
}
65+
66+
rh_status_q() {
67+
rh_status >/dev/null 2>&1
68+
}
69+
70+
case "$1" in
71+
start)
72+
rh_status_q && exit 0
73+
$1
74+
;;
75+
stop)
76+
rh_status_q || exit 0
77+
$1
78+
;;
79+
restart)
80+
$1
81+
;;
82+
reload)
83+
rh_status_q || exit 7
84+
$1
85+
;;
86+
force-reload)
87+
force_reload
88+
;;
89+
status)
90+
rh_status
91+
;;
92+
condrestart|try-restart)
93+
rh_status_q || exit 0
94+
restart
95+
;;
96+
*)
97+
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload}"
98+
exit 2
99+
esac
100+
101+
exit $?

daemon/certifierd.service

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[Unit]
2+
Description=LibCertifier Daemon that renews certificates automatically once a day
3+
4+
[Service]
5+
Type=simple
6+
ExecStart=/usr/bin/certifierd \
7+
--conf-file /etc/certifier/libcertifier.cfg \
8+
--log-file /var/log/certifier/certifierd.log
9+
ExecReload=/bin/kill -HUP $MAINPID
10+
11+
[Install]
12+
WantedBy=multi-user.target

0 commit comments

Comments
 (0)