| 1 |
= OpenSCAP Developer Manual |
| 2 |
:oscap_git: https://github.com/OpenSCAP/openscap |
| 3 |
:toc: preamble |
| 4 |
:numbered: |
| 5 |
|
| 6 |
This part of documentation is meant to serve mainly to developers who want to |
| 7 |
contribute to OpenSCAP, help to fix bugs, or take an advantage of |
| 8 |
the OpenSCAP library and create own projects on top of it. |
| 9 |
|
| 10 |
|
| 11 |
== Building OpenSCAP on Linux |
| 12 |
If you want to build the `libopenscap` library and the `oscap` tool from |
| 13 |
the {oscap_git}[source code] then follow these instructions: |
| 14 |
|
| 15 |
. *Get the source code* |
| 16 |
+ |
| 17 |
Choose *1a* or *1b* depending on whether you want sources from a release tarball or the git repository. |
| 18 |
|
| 19 |
.. Use a release tarball: |
| 20 |
+ |
| 21 |
---- |
| 22 |
# replace ${version} with the desired version |
| 23 |
wget https://github.com/OpenSCAP/openscap/releases/download/${version}/openscap-${version}.tar.gz |
| 24 |
tar -xzpf openscap-${version}.tar.gz |
| 25 |
cd openscap-${version} |
| 26 |
mkdir -p build |
| 27 |
---- |
| 28 |
+ |
| 29 |
**OR** |
| 30 |
|
| 31 |
.. Use fresh sources from git repository. |
| 32 |
+ |
| 33 |
---- |
| 34 |
$ git clone --recurse-submodules https://github.com/OpenSCAP/openscap.git |
| 35 |
$ cd openscap |
| 36 |
---- |
| 37 |
+ |
| 38 |
|
| 39 |
NOTE: We include https://github.com/OpenSCAP/yaml-filter[yaml-filter] library |
| 40 |
as a git submodule. To get more information about using git submodules, read |
| 41 |
https://git-scm.com/book/en/v2/Git-Tools-Submodules[Git Tools - Submodules]. |
| 42 |
|
| 43 |
. *Get the build dependencies* |
| 44 |
+ |
| 45 |
-- |
| 46 |
To build the library you will also need to install the build dependencies. |
| 47 |
|
| 48 |
Build dependencies may vary depending on enabled features (by the `cmake` command). |
| 49 |
Some of the dependencies are optional, if they are not detected, openscap will be compiled |
| 50 |
without respective optional features. |
| 51 |
|
| 52 |
On Fedora 24+, the command to install the build dependencies is: |
| 53 |
|
| 54 |
---- |
| 55 |
sudo dnf install \ |
| 56 |
cmake dbus-devel GConf2-devel libacl-devel libblkid-devel libcap-devel libcurl-devel \ |
| 57 |
libgcrypt-devel libselinux-devel libxml2-devel libxslt-devel libattr-devel make openldap-devel \ |
| 58 |
pcre2-devel perl-XML-Parser perl-XML-XPath perl-devel python3-devel python3-dbusmock rpm-devel swig \ |
| 59 |
bzip2-devel gcc-c++ libyaml-devel xmlsec1-devel xmlsec1-openssl-devel |
| 60 |
---- |
| 61 |
|
| 62 |
On RHEL 8+ / CentOS 8+, the command to install the build dependencies is: |
| 63 |
|
| 64 |
---- |
| 65 |
sudo dnf install \ |
| 66 |
cmake dbus-devel libacl-devel libblkid-devel libcap-devel libcurl-devel \ |
| 67 |
libgcrypt-devel libselinux-devel libxml2-devel libxslt-devel libattr-devel make openldap-devel \ |
| 68 |
pcre2-devel perl-XML-Parser perl-XML-XPath perl-devel python36-devel rpm-devel swig \ |
| 69 |
bzip2-devel gcc-c++ libyaml-devel xmlsec1-devel xmlsec1-openssl-devel |
| 70 |
---- |
| 71 |
|
| 72 |
On Ubuntu 16.04, Debian 8 or Debian 9, the command to install the build dependencies is: |
| 73 |
|
| 74 |
---- |
| 75 |
sudo apt-get install -y cmake libdbus-1-dev libdbus-glib-1-dev libcurl4-openssl-dev \ |
| 76 |
libgcrypt20-dev libselinux1-dev libxslt1-dev libgconf2-dev libacl1-dev libblkid-dev \ |
| 77 |
libcap-dev libxml2-dev libldap2-dev libpcre3-dev python-dev swig libxml-parser-perl \ |
| 78 |
libxml-xpath-perl libperl-dev libbz2-dev librpm-dev g++ libyaml-dev \ |
| 79 |
libxmlsec1-dev libxmlsec1-openssl |
| 80 |
---- |
| 81 |
|
| 82 |
Since version 1.4.0 OpenSCAP uses PCRE2 library to handle regular |
| 83 |
expressions. Dependencies: Debian/Ubuntu — pcre2-dev, |
| 84 |
Fedora/RHEL — pcre2-devel. |
| 85 |
|
| 86 |
When you have all the build dependencies installed you can build the library. |
| 87 |
-- |
| 88 |
|
| 89 |
. *Build the library* |
| 90 |
+ |
| 91 |
-- |
| 92 |
Run the following commands to build the library: |
| 93 |
|
| 94 |
---- |
| 95 |
$ cd build/ |
| 96 |
$ cmake ../ |
| 97 |
$ make |
| 98 |
---- |
| 99 |
|
| 100 |
On Ubuntu 18.04 and potentially other distro, the python3 dist-packages path is wrong. |
| 101 |
If the following command: |
| 102 |
|
| 103 |
---- |
| 104 |
$ python3 -c "import sysconfig; print(sysconfig.get_path('platlib'))" |
| 105 |
---- |
| 106 |
|
| 107 |
returns "/usr/local/lib/python3/dist-packages" instead of a path like |
| 108 |
"/usr/local/lib/python3.6/dist-packages", you must override this path, |
| 109 |
otherwise you will not be able to import openscap_api. |
| 110 |
|
| 111 |
---- |
| 112 |
$ cmake ../ -DPYTHON_SITE_PACKAGES_INSTALL_DIR=/usr/local/lib/python3.6/dist-packages |
| 113 |
---- |
| 114 |
|
| 115 |
-- |
| 116 |
|
| 117 |
. *Build the HTML documentation* |
| 118 |
+ |
| 119 |
-- |
| 120 |
It is possible to generate a complete API documentation, User manual, |
| 121 |
Developer manual and contribute documents in HTML format. |
| 122 |
|
| 123 |
If you want to build the HTML documentation you will need to install Doxygen |
| 124 |
and AsciiDoc. |
| 125 |
To install AsciiDoc, you can run `dnf install asciidoc`. |
| 126 |
To install Doxygen, you can run `dnf install doxygen`. |
| 127 |
|
| 128 |
Run the following command to build the documentation: |
| 129 |
|
| 130 |
---- |
| 131 |
$ cmake -DENABLE_DOCS=TRUE ../ |
| 132 |
$ make docs |
| 133 |
---- |
| 134 |
|
| 135 |
The resulting documentation is located in `build/docs` directory in these |
| 136 |
subdirectories: |
| 137 |
-- |
| 138 |
* `html` - contains the API documentation generated by Doxygen |
| 139 |
* `manual` - contains OpenSCAP User Manual |
| 140 |
* `developer` - contains OpenSCAP Developer Manual |
| 141 |
* `contribute` - contains contribute documents |
| 142 |
|
| 143 |
. *Run the tests* |
| 144 |
+ |
| 145 |
-- |
| 146 |
After building the library you might want to run library self-checks. To do |
| 147 |
that you need to have these additional packages installed: |
| 148 |
|
| 149 |
---- |
| 150 |
wget lua which procps-ng initscripts chkconfig sendmail bzip2 rpm-build strace |
| 151 |
---- |
| 152 |
|
| 153 |
On Ubuntu 18.04, also install: |
| 154 |
|
| 155 |
---- |
| 156 |
rpm-common |
| 157 |
---- |
| 158 |
|
| 159 |
It is also required to have `sendmail` service running on the system: |
| 160 |
|
| 161 |
---- |
| 162 |
$ systemctl start sendmail.service |
| 163 |
---- |
| 164 |
|
| 165 |
Now you can execute the following command to run library self-checks: |
| 166 |
|
| 167 |
---- |
| 168 |
$ ctest |
| 169 |
---- |
| 170 |
|
| 171 |
Note that using the `--jobs/-j` flag is currently not supported. |
| 172 |
It will cause unexpected test failures. |
| 173 |
See link:https://github.com/OpenSCAP/openscap/issues/2057[#2057] for more details. |
| 174 |
|
| 175 |
It's also possible to use `ctest` to test any other oscap binary present in the system. You just have to set the path of the binary to the CUSTOM_OSCAP variable: |
| 176 |
|
| 177 |
---- |
| 178 |
$ export CUSTOM_OSCAP=/usr/bin/oscap; ctest |
| 179 |
---- |
| 180 |
|
| 181 |
Some tests that use the so-called offline mode of probes need to chroot during the test execution. |
| 182 |
Some of those probes use the chroot syscall, which an unprivileged process is not allowed to do. |
| 183 |
This is not a problem during the scanning itself, as oscap is usually scanning as root. |
| 184 |
However, we don't want to run oscap as root during tests, as the whole test suite would have to use root privileges to clean up. |
| 185 |
|
| 186 |
Instead, build the `oscap-chrootable` target as superuser, or build `oscap-chrootable-nocap` first and then grant the capability manually. |
| 187 |
This target creates the binary that the test suite will use for some of those offline tests. |
| 188 |
In offline tests, use the `set_offline_test_mode [chroot directory]` and `unset_offline_test_mode` functions from the common test module - those will set variables in such way that the unquoted `$OSCAP` invocation will use the chroot-capable binary, or it will exit with an error code, aborting the test. |
| 189 |
Therefore, it is recommended to run |
| 190 |
|
| 191 |
---- |
| 192 |
$ sudo make oscap-chrootable |
| 193 |
---- |
| 194 |
|
| 195 |
Not every check tests the oscap tool, however, when the `CUSTOM_OSCAP` variable is set, only the checks which do are executed. |
| 196 |
|
| 197 |
To enable the MITRE tests, use the `ENABLE_MITRE` flag: |
| 198 |
|
| 199 |
---- |
| 200 |
$ cmake -DENABLE_MITRE=TRUE .. |
| 201 |
---- |
| 202 |
|
| 203 |
These test require specific features of the environment to function properly; most notably, a MTA needs to be listening on port 25. We suggest using our container `mitre_tests` to test MITRE functionality if possible: |
| 204 |
|
| 205 |
---- |
| 206 |
$ docker build --tag openscap_mitre_tests:latest -f Dockerfiles/mitre_tests . && docker run openscap_mitre_tests:latest |
| 207 |
---- |
| 208 |
|
| 209 |
-- |
| 210 |
|
| 211 |
. *Install* |
| 212 |
+ |
| 213 |
-- |
| 214 |
Run the installation procedure by executing the following command: |
| 215 |
|
| 216 |
---- |
| 217 |
$ make install |
| 218 |
---- |
| 219 |
-- |
| 220 |
|
| 221 |
== Running oscap |
| 222 |
It is important to use your compiled `libopenscap.so` library with your `oscap` tool. |
| 223 |
The easiest way how to achieve that without need to install `libopenscap.so` to the system path, is to use a shell script called *oscap_wrapper* or *run* in the OpenSCAP build directory. |
| 224 |
|
| 225 |
------------------------------------------------- |
| 226 |
$ cd build/ |
| 227 |
$ ./oscap_wrapper xccdf eval ... whatever |
| 228 |
$ ./run valgrind utils/oscap xccdf eval ... whatever |
| 229 |
------------------------------------------------- |
| 230 |
|
| 231 |
The *run* script is generated at configure time by CMake and it sets the following environment variables: |
| 232 |
|
| 233 |
* *LD_LIBRARY_PATH* - path to `libopenscap.so` |
| 234 |
* *OSCAP_SCHEMA_PATH* - path to XCCDF, OVAL, CPE, ... XSD schemas and schematrons |
| 235 |
(required for correct SCAP content validation) |
| 236 |
* *OSCAP_XSLT_PATH* - path to XSLT transformations. (required if you want |
| 237 |
to generate html documents from xml) |
| 238 |
* *OSCAP_CPE_PATH* - path to the OpenSCAP CPE dictionary. |
| 239 |
|
| 240 |
The *oscap_wrapper* script is a convenience shortcut for `run utils/oscap` call. |
| 241 |
|
| 242 |
|
| 243 |
== Debugging |
| 244 |
Developers and users who intend to help find and fix possible bugs in OpenSCAP |
| 245 |
or possible bugs in their security policies have these possibilities: |
| 246 |
|
| 247 |
=== Verbose mode |
| 248 |
The verbose mode provides user additional information about process of system |
| 249 |
scanning. The mode is useful for diagnostics of SCAP content evaluation |
| 250 |
and also for debugging. It produces a detailed report log with various messages. |
| 251 |
There is no need to special compilation, the feature is available for all |
| 252 |
OpenSCAP users. |
| 253 |
|
| 254 |
To turn the verbose mode on, run `oscap` with this option: |
| 255 |
|
| 256 |
* `--verbose VERBOSITY_LEVEL` - Turn on verbose mode at specified |
| 257 |
verbosity level. |
| 258 |
|
| 259 |
The `VERBOSITY_LEVEL` can be one of: |
| 260 |
|
| 261 |
1. *DEVEL* - the most detailed information for developers and bug hunters |
| 262 |
2. *INFO* - reports content processing and system scanning |
| 263 |
3. *WARNING* - possible failures which OpenSCAP can recover from |
| 264 |
4. *ERROR* - shows only serious errors |
| 265 |
|
| 266 |
The verbose messages will be written on standard error output (stderr). |
| 267 |
Optionally, you can write the log into a file using |
| 268 |
`--verbose-log-file FILE`. |
| 269 |
|
| 270 |
This is an example describing how to run OpenSCAP in verbose mode: |
| 271 |
|
| 272 |
---- |
| 273 |
$ oscap oval eval --results results.xml --verbose INFO --verbose-log-file log.txt oval.xml |
| 274 |
---- |
| 275 |
|
| 276 |
Then see the log using eg.: |
| 277 |
|
| 278 |
---- |
| 279 |
$ less log.txt |
| 280 |
---- |
| 281 |
|
| 282 |
=== Debug mode |
| 283 |
Debug mode is useful for programmers. You need to build OpenSCAP from source code |
| 284 |
with a custom configuration to enable the debug mode. Use this command: |
| 285 |
|
| 286 |
------------------------------------ |
| 287 |
$ cmake -DCMAKE_BUILD_TYPE=Debug .. && make |
| 288 |
------------------------------------ |
| 289 |
|
| 290 |
Debug mode provides: |
| 291 |
|
| 292 |
* debug symbols on and optimization off - you can use `gdb`, |
| 293 |
every process that was run. |
| 294 |
* http://www.gnu.org/software/gawk/manual/html_node/Assert-Function.html[assertions] |
| 295 |
are evaluated. |
| 296 |
|
| 297 |
|
| 298 |
==== Example |
| 299 |
|
| 300 |
---- |
| 301 |
$ bash ./run gdb --args utils/oscap xccdf eval \ |
| 302 |
--profile hard --results xccdf-results.xml \ |
| 303 |
--oval-results my-favourite-xccdf-checklist.xml |
| 304 |
---- |
| 305 |
|
| 306 |
|
| 307 |
The `--oval-results` option force `oscap` tool to generate OVAL Result file |
| 308 |
for each OVAL session used for evaluation. It's also very useful for |
| 309 |
debugging! |
| 310 |
|
| 311 |
=== Environment variables |
| 312 |
There are few more environment variables that control `oscap` tool |
| 313 |
behaviour. |
| 314 |
|
| 315 |
* *OSCAP_FULL_VALIDATION=1* - validate all exported documents (slower) |
| 316 |
* *SEXP_VALIDATE_DISABLE=1* - do not validate SEXP expressions (faster) |
| 317 |
* *OSCAP_PCRE_EXEC_RECURSION_LIMIT* - override default recursion limit |
| 318 |
for match in pcre_exec/pcre2_match calls in textfilecontent(54) probes. |
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
== Generating of code coverage |
| 323 |
Code coverage can be useful during writing of test or performance profiling. |
| 324 |
We could separate the process into five phases. |
| 325 |
|
| 326 |
1) *Get dependencies* |
| 327 |
|
| 328 |
---- |
| 329 |
# dnf install lcov |
| 330 |
---- |
| 331 |
|
| 332 |
2) *Run CMake & make* |
| 333 |
|
| 334 |
To allow code to generate statistics, we need to compile it with specific flags. |
| 335 |
|
| 336 |
---- |
| 337 |
$ CFLAGS="--coverage -ftest-coverage -fprofile-arcs" LDFLAGS=-lgcov cmake -DCMAKE_BUILD_TYPE=Debug ../ |
| 338 |
$ make |
| 339 |
---- |
| 340 |
|
| 341 |
3) *Run code* |
| 342 |
|
| 343 |
In this phase we should run code. We can run it directly or via test suite. |
| 344 |
|
| 345 |
---- |
| 346 |
$ bash ./run utils/oscap |
| 347 |
---- |
| 348 |
|
| 349 |
4) *Generate and browse results* |
| 350 |
|
| 351 |
---- |
| 352 |
$ lcov -t "OpenSCAP coverage" -o ./coverage.info -c -d . |
| 353 |
$ genhtml -o ./coverage ./coverage.info |
| 354 |
$ xdg-open ./coverage/index.html # open results in browser |
| 355 |
---- |
| 356 |
|
| 357 |
5) *Clean stats* |
| 358 |
|
| 359 |
Every run only modify our current statistics and not rewrite them completely. |
| 360 |
If we want to generate new statistics, we should remove the old ones. |
| 361 |
|
| 362 |
---- |
| 363 |
$ lcov --directory ./ --zerocounters ; find ./ -name "*.gcno" | xargs rm |
| 364 |
$ rm -rf ./coverage |
| 365 |
---- |
| 366 |
|
| 367 |
== OpenSCAP Reference Manual |
| 368 |
For more information about OpenSCAP library, you can refer to this online |
| 369 |
reference manual: http://static.open-scap.org/openscap-1.2/[OpenSCAP |
| 370 |
reference manual]. This manual is included in a release tarball and can be |
| 371 |
regenerated from project sources by Doxygen documentation system. |
| 372 |
|