Integrate parts of meta-intel-iot-security
[AGL/meta-agl.git] / meta-security / recipes-security / cynara / cynara.inc
1 DESCRIPTION = "Cynara service with client libraries"
2 LICENSE = "Apache-2.0"
3 LIC_FILES_CHKSUM = "file://LICENSE;md5=86d3f3a95c324c9479bd8986968f4327;beginline=3"
4
5 DEPENDS = " \
6 dbus \
7 glib-2.0 \
8 systemd \
9 zip \
10 "
11
12 # For testing:
13 # DEPENDS += "gmock"
14
15 PACKAGECONFIG ??= ""
16 # Use debug mode to increase logging. Beware, also compiles with less optimization
17 # and thus has to disable FORTIFY_SOURCE below.
18 PACKAGECONFIG[debug] = "-DCMAKE_BUILD_TYPE=DEBUG,-DCMAKE_BUILD_TYPE=RELEASE,libunwind elfutils"
19
20 inherit cmake
21
22 CXXFLAGS_append = " \
23 -DCYNARA_STATE_PATH=\\\\\"${localstatedir}/cynara/\\\\\" \
24 -DCYNARA_LIB_PATH=\\\\\"${prefix}/lib/cynara/\\\\\" \
25 -DCYNARA_TESTS_DIR=\\\\\"${prefix}/share/cynara/tests/\\\\\" \
26 -DCYNARA_CONFIGURATION_DIR=\\\\\"${sysconfdir}/cynara/\\\\\" \
27 ${@bb.utils.contains('PACKAGECONFIG', 'debug', '-Wp,-U_FORTIFY_SOURCE', '', d)} \
28 "
29
30 EXTRA_OECMAKE += " \
31 -DCMAKE_VERBOSE_MAKEFILE=ON \
32 -DBUILD_WITH_SYSTEMD=ON \
33 -DSYSTEMD_UNIT_DIR=${systemd_unitdir}/system \
34 -DSOCKET_DIR=/run/cynara \
35 "
36
37 # Explicitly package empty directory. Otherwise Cynara prints warnings
38 # at runtime:
39 # cyad[198]: Couldn't scan for plugins in </usr/lib/cynara/plugin/service/> : <No such file or directory>
40 FILES_${PN}_append = " \
41 ${libdir}/cynara/plugin/service \
42 ${libdir}/cynara/plugin/client \
43 "
44
45 # Testing depends on gmock and gtest. They can be found in meta-oe
46 # and are not necessarily available, so this feature is off by default.
47 # If gmock from meta-oe is used, then a workaround is needed to avoid
48 # a link error (libgmock.a calls pthread functions without libpthread
49 # being listed in the .pc file).
50 PACKAGECONFIG[tests] = "-DBUILD_TESTS:BOOL=ON,-DBUILD_TESTS:BOOL=OFF,gmock gtest,"
51 SRC_URI_append = "${@bb.utils.contains('PACKAGECONFIG', 'tests', ' file://gmock-pthread-linking.patch file://run-ptest', '', d)}"
52
53 # Will be empty if no tests were built.
54 inherit ptest
55 FILES_${PN}-ptest += "${bindir}/cynara-tests ${bindir}/cynara-db-migration-tests ${datadir}/cynara/tests"
56 do_install_ptest () {
57     if ${@bb.utils.contains('PACKAGECONFIG', 'tests', 'true', 'false', d)}; then
58         mkdir -p ${D}/${datadir}/cynara/tests
59         cp -r ${S}/test/db/* ${D}/${datadir}/cynara/tests
60     fi
61 }
62
63 do_compile_prepend () {
64     # en_US.UTF8 is not available, causing cynara-tests parser.getKeyAndValue to fail.
65     # Submitted upstream: https://github.com/Samsung/cynara/issues/10
66     sed -i -e 's/std::locale("en_US.UTF8")/std::locale::classic()/g' ${S}/test/credsCommons/parser/Parser.cpp
67 }
68
69 inherit useradd
70 USERADD_PACKAGES = "${PN}"
71 GROUPADD_PARAM_${PN} = "-r cynara"
72 USERADD_PARAM_${PN} = "\
73 --system --home ${localstatedir}/lib/empty \
74 --no-create-home --shell /bin/false \
75 --gid cynara cynara \
76 "
77
78 # Causes deadlock during booting, see workaround in postinst below.
79 #inherit systemd
80 #SYSTEMD_SERVICE_${PN} = "cynara.service"
81
82 do_install_append () {
83    chmod a+rx ${D}/${sbindir}/cynara-db-migration
84
85    install -d ${D}${sysconfdir}/cynara/
86    install -m 644 ${S}/conf/creds.conf ${D}/${sysconfdir}/cynara/creds.conf
87
88    # No need to create empty directories except for those which
89    # Cynara expects to find.
90    # install -d ${D}${localstatedir}/cynara/
91    # install -d ${D}${prefix}/share/cynara/tests/empty_db
92    install -d ${D}${libdir}/cynara/plugin/client
93    install -d ${D}${libdir}/cynara/plugin/service
94
95    # install db* ${D}${prefix}/share/cynara/tests/
96
97    install -d ${D}${systemd_unitdir}/system/sockets.target.wants
98    ln -s ../cynara.socket ${D}${systemd_unitdir}/system/sockets.target.wants/cynara.socket
99    ln -s ../cynara-admin.socket ${D}${systemd_unitdir}/system/sockets.target.wants/cynara-admin.socket
100    ln -s ../cynara-agent.socket ${D}${systemd_unitdir}/system/sockets.target.wants/cynara-agent.socket
101 }
102
103 FILES_${PN} += "${systemd_unitdir}/system"
104
105 # Cynara itself has no dependency on Smack. Only its installation
106 # is Smack-aware in the sense that it sets Smack labels. Do not
107 # depend on smack userspace unless we really need Smack labels.
108 #
109 # The Tizen .spec file calls cynara-db-migration in a %pre section.
110 # That only works when cynara-db-migration is packaged separately
111 # (overly complex) and does not seem necessary: perhaps there is a
112 # time window where cynara might already get activated before
113 # the postinst completes, but that is a general problem. It gets
114 # avoided entirely when calling this script while building the
115 # rootfs.
116 RDEPENDS_${PN}_append_with-lsm-smack = " smack-userspace"
117 DEPENDS_append_with-lsm-smack = " smack-userspace-native"
118 CHSMACK_with-lsm-smack = "chsmack"
119 CHSMACK = "true"
120 pkg_postinst_${PN} () {
121    # Fail on error.
122    set -e
123
124    # It would be nice to run the code below while building an image,
125    # but currently the calls to cynara-db-chsgen (a binary) in
126    # cynara-db-migration (a script) prevent that. Rely instead
127    # on OE's support for running failed postinst scripts at first boot.
128    if [ x"$D" != "x" ]; then
129       exit 1
130    fi
131
132    mkdir -p $D${sysconfdir}/cynara
133    ${CHSMACK} -a System $D${sysconfdir}/cynara
134
135    # Strip git patch level information, the version comparison code
136    # in cynara-db-migration only expect major.minor.patch version numbers.
137    VERSION=${@bb.data.getVar('PV',d,1).split('+git')[0]}
138    if [ -d $D${localstatedir}/cynara ] ; then
139       # upgrade
140       echo "NOTE: updating cynara DB to version $VERSION"
141       $D${sbindir}/cynara-db-migration upgrade -f 0.0.0 -t $VERSION
142    else
143       # install
144       echo "NOTE: creating cynara DB for version $VERSION"
145       mkdir -p $D${localstatedir}/cynara
146       ${CHSMACK} -a System $D${localstatedir}/cynara
147       $D${sbindir}/cynara-db-migration install -t $VERSION
148    fi
149
150    # Workaround for systemd.bbclass issue: it would call
151    # "systemctl start" without "--no-block", but because
152    # the service is not ready to run at the time when
153    # this scripts gets executed by run-postinsts.service,
154    # booting deadlocks.
155    echo "NOTE: enabling and starting cynara service"
156    systemctl enable cynara
157    systemctl start --no-block cynara
158 }