|
| 1 | +cmake_minimum_required(VERSION 2.6) |
| 2 | + |
| 3 | +# include custom Modules |
| 4 | +set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/../CMakeModules/") |
| 5 | + |
| 6 | +project(keystored C) |
| 7 | +include(GNUInstallDirs) |
| 8 | + |
| 9 | +# check the supported platform |
| 10 | +if(NOT UNIX) |
| 11 | + message(FATAL_ERROR "Only *nix like systems are supported.") |
| 12 | +endif() |
| 13 | + |
| 14 | +# set default build type if not specified by user |
| 15 | +if(NOT CMAKE_BUILD_TYPE) |
| 16 | + set(CMAKE_BUILD_TYPE debug) |
| 17 | +endif() |
| 18 | + |
| 19 | +set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra") |
| 20 | +set(CMAKE_C_FLAGS_RELEASE "-O2 -DNDEBUG") |
| 21 | +set(CMAKE_C_FLAGS_DEBUG "-g -O0 -DDEBUG") |
| 22 | + |
| 23 | +# set version |
| 24 | +set(KEYSTORED_VERSION 0.1.1) |
| 25 | + |
| 26 | +# config variables |
| 27 | +if (NOT KEYSTORED_KEYS_DIR) |
| 28 | + set(KEYSTORED_KEYS_DIR "${CMAKE_INSTALL_FULL_SYSCONFDIR}/keystored/keys") |
| 29 | +endif() |
| 30 | +if (NOT OPENSSL_EXECUTABLE) |
| 31 | + find_program(OPENSSL_EXECUTABLE openssl) |
| 32 | + if (NOT OPENSSL_EXECUTABLE) |
| 33 | + message(FATAL_ERROR "openssl utility not found.") |
| 34 | + endif() |
| 35 | +endif() |
| 36 | + |
| 37 | +configure_file("${PROJECT_SOURCE_DIR}/config.h.in" "${PROJECT_SOURCE_DIR}/config.h" ESCAPE_QUOTES @ONLY) |
| 38 | + |
| 39 | +# keystored plugin |
| 40 | +add_library(keystored SHARED keystored.c) |
| 41 | + |
| 42 | +# pkgconfig keys directory |
| 43 | +find_package(PkgConfig) |
| 44 | +if (PKG_CONFIG_FOUND) |
| 45 | + # generate and install pkg-config file |
| 46 | + configure_file("keystored.pc.in" "keystored.pc" @ONLY) |
| 47 | + install(FILES "${CMAKE_CURRENT_BINARY_DIR}/keystored.pc" DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig") |
| 48 | + execute_process(COMMAND ${PKG_CONFIG_EXECUTABLE} "--variable=pc_path" "pkg-config" RESULT_VARIABLE RET OUTPUT_VARIABLE OUT) |
| 49 | + if (RET) |
| 50 | + message(WARNING "Failed to check pkg-config search directories.") |
| 51 | + message(WARNING "For netopeer2-server configuration to work, pkg-config search path must include \"${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}/pkgconfig\" or you keystored keys directory will have to be set manually to \"${KEYSTORED_KEYS_DIR}\".") |
| 52 | + else() |
| 53 | + string(REGEX MATCH "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}/pkgconfig" MATCHED "${OUT}") |
| 54 | + if (MATCHED) |
| 55 | + message(STATUS "pkg-config check successful, netopeer2-server configuration should work after installation") |
| 56 | + else() |
| 57 | + message(WARNING "keystored pkg-config file copied into a directory not searched by pkg-config.") |
| 58 | + message(WARNING "For netopeer2-server configuration to work, pkg-config search path must include \"${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}/pkgconfig\" or keystored keys directory will have to be set manually to \"${KEYSTORED_KEYS_DIR}\".") |
| 59 | + endif() |
| 60 | + endif() |
| 61 | +else() |
| 62 | + message(WARNING "pkg-config not detected.") |
| 63 | + message(WARNING "For netopeer2-server configuration to work, keystored keys directory will have to be set manually to \"${KEYSTORED_KEYS_DIR}\".") |
| 64 | +endif() |
| 65 | + |
| 66 | +# dependencies - sysrepo |
| 67 | +find_package(SYSREPO REQUIRED) |
| 68 | +target_link_libraries(keystored ${SYSREPO_LIBRARIES}) |
| 69 | +include_directories(${SYSREPO_INCLUDE_DIRS}) |
| 70 | + |
| 71 | +# get sysrepo plugins directory |
| 72 | +if (NOT SR_PLUGINS_DIR) |
| 73 | + if (PKG_CONFIG_FOUND) |
| 74 | + execute_process(COMMAND ${PKG_CONFIG_EXECUTABLE} "--variable=SR_PLUGINS_DIR" "libsysrepo" OUTPUT_VARIABLE SR_PLUGINS_DIR) |
| 75 | + string(STRIP ${SR_PLUGINS_DIR} SR_PLUGINS_DIR) |
| 76 | + endif() |
| 77 | +endif() |
| 78 | +if (NOT SR_PLUGINS_DIR) |
| 79 | + message(FATAL_ERROR "Cannot get sysrepo plugins directory due to missing pkg-config, set SR_PLUGINS_DIR manually.") |
| 80 | +endif() |
| 81 | + |
| 82 | +# find programs |
| 83 | +if (NOT SYSREPOCTL_EXECUTABLE) |
| 84 | + find_program(SYSREPOCTL_EXECUTABLE sysrepoctl) |
| 85 | +endif() |
| 86 | +if (NOT SYSREPOCTL_EXECUTABLE) |
| 87 | + message(FATAL_ERROR "Unable to find sysrepoctl, set SYSREPOCTL_EXECUTABLE manually.") |
| 88 | +endif() |
| 89 | + |
| 90 | +if (NOT SYSREPOCFG_EXECUTABLE) |
| 91 | + find_program(SYSREPOCFG_EXECUTABLE sysrepocfg) |
| 92 | +endif() |
| 93 | +if (NOT SYSREPOCFG_EXECUTABLE) |
| 94 | + message(FATAL_ERROR "Unable to find sysrepocfg, set SYSREPOCFG_EXECUTABLE manually.") |
| 95 | +endif() |
| 96 | + |
| 97 | +if (NOT CHMOD_EXECUTABLE) |
| 98 | + find_program(CHMOD_EXECUTABLE chmod) |
| 99 | +endif() |
| 100 | +if (NOT CHMOD_EXECUTABLE) |
| 101 | + message(FATAL_ERROR "Unable to find chmod, set CHMOD_EXECUTABLE manually.") |
| 102 | +endif() |
| 103 | + |
| 104 | +# create the keys directory with correct permissions |
| 105 | +install(DIRECTORY DESTINATION ${KEYSTORED_KEYS_DIR} |
| 106 | + DIRECTORY_PERMISSIONS OWNER_WRITE OWNER_READ OWNER_EXECUTE) |
| 107 | + |
| 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-keystore [^\\n]*\" INSTALLED_MODULE_LINE \"\${INSTALLED_MODULES}\") |
| 117 | + if (NOT INSTALLED_MODULE_LINE) |
| 118 | + message(STATUS \"Importing module ietf-keystore into sysrepo...\") |
| 119 | + 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) |
| 120 | + if (RET) |
| 121 | + string(REPLACE \"\\n\" \"\\n \" OUT \${OUT}) |
| 122 | + message(FATAL_ERROR \" Command sysrepoctl install failed:\\n \${OUT}\") |
| 123 | + endif() |
| 124 | +
|
| 125 | + else() |
| 126 | + message(STATUS \"Module ietf-keystore already in sysrepo.\") |
| 127 | + endif()") |
| 128 | + |
| 129 | +# import stock OpenSSH RSA key |
| 130 | +install(CODE " |
| 131 | + execute_process(COMMAND ${SYSREPOCFG_EXECUTABLE} -d startup --export ietf-keystore RESULT_VARIABLE RET OUTPUT_VARIABLE OUT ERROR_VARIABLE OUT) |
| 132 | + if (RET) |
| 133 | + string(REPLACE \"\\n\" \"\\n \" OUT \${OUT}) |
| 134 | + message(FATAL_ERROR \" Command sysrepocfg export failed:\\n \${OUT}\") |
| 135 | + endif() |
| 136 | +
|
| 137 | + if (OUT) |
| 138 | + message(STATUS \"Some ietf-keystore configuration set, no keys will be imported.\") |
| 139 | + elseif(NOT EXISTS \"/etc/ssh/ssh_host_rsa_key\") |
| 140 | + 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.\") |
| 141 | + else() |
| 142 | + message(STATUS \"Importing stock OpenSSH RSA key.\") |
| 143 | + file(READ /etc/ssh/ssh_host_rsa_key RSA_KEY) |
| 144 | + file(WRITE ${KEYSTORED_KEYS_DIR}/ssh_host_rsa_key.pem \${RSA_KEY}) |
| 145 | + execute_process(COMMAND ${CHMOD_EXECUTABLE} go-rw ${KEYSTORED_KEYS_DIR}/ssh_host_rsa_key.pem) |
| 146 | + 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) |
| 147 | + if (RET) |
| 148 | + string(REPLACE \"\\n\" \"\\n \" OUT \${OUT}) |
| 149 | + message(FATAL_ERROR \" Command openssl generate public key failed:\\n \${OUT}\") |
| 150 | + endif() |
| 151 | + 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) |
| 152 | + if (RET) |
| 153 | + string(REPLACE \"\\n\" \"\\n \" OUT \${OUT}) |
| 154 | + message(FATAL_ERROR \" Command sysrepocfg import failed:\\n \${OUT}\") |
| 155 | + endif() |
| 156 | + endif()") |
| 157 | + |
| 158 | +# plugins should be installed into sysrepo plugins dir |
| 159 | +install(TARGETS keystored DESTINATION ${SR_PLUGINS_DIR}) |
0 commit comments