├── .clang-format ├── .codecov.yml ├── .devcontainer ├── Dockerfile └── devcontainer.json ├── .docker ├── prod-oldstable.Dockerfile ├── prod-testing.Dockerfile └── prod.Dockerfile ├── .dockerignore ├── .gersemirc ├── .github ├── CODEOWNERS ├── ISSUE_TEMPLATE │ ├── bug-report.md │ └── config.yml ├── build-dependencies.list ├── dependabot.yml ├── install-dependencies.sh ├── runtime-dependencies.oldstable.list ├── runtime-dependencies.stable.list ├── runtime-dependencies.testing.list └── workflows │ ├── build-docs.yml │ ├── changelog.yml │ ├── ci-c.yml │ ├── codeql-analysis-c.yml │ ├── container.yml │ ├── conventional-commits.yml │ ├── dependency-review.yml │ ├── release.yml │ └── sbom-upload.yml ├── .gitignore ├── .mergify.yml ├── CMakeLists.txt ├── COPYING ├── INSTALL.md ├── README.md ├── VERSION.in ├── agent_controller ├── CMakeLists.txt ├── agent_controller.c ├── agent_controller.h ├── agent_controller_tests.c └── libgvm_agent_controller.pc.in ├── base ├── CMakeLists.txt ├── array.c ├── array.h ├── array_tests.c ├── credentials.c ├── credentials.h ├── cvss.c ├── cvss.h ├── cvss_tests.c ├── drop_privileges.c ├── drop_privileges.h ├── gvm_sentry.c ├── gvm_sentry.h ├── hosts.c ├── hosts.h ├── hosts_tests.c ├── libgvm_base.pc.in ├── logging.c ├── logging.h ├── logging_domain.c ├── logging_domain.h ├── logging_domain_tests.c ├── logging_tests.c ├── networking.c ├── networking.h ├── networking_tests.c ├── nvti.c ├── nvti.h ├── nvti_tests.c ├── pidfile.c ├── pidfile.h ├── prefs.c ├── prefs.h ├── proctitle.c ├── proctitle.h ├── pwpolicy.c ├── pwpolicy.h ├── pwpolicy_tests.c ├── settings.c ├── settings.h ├── strings.c ├── strings.h ├── version.c ├── version.h └── version_tests.c ├── boreas ├── CMakeLists.txt ├── alivedetection.c ├── alivedetection.h ├── alivedetection_tests.c ├── arp.c ├── arp.h ├── boreas_error.c ├── boreas_error.h ├── boreas_error_tests.c ├── boreas_io.c ├── boreas_io.h ├── boreas_io_tests.c ├── cli.c ├── cli.h ├── cli_tests.c ├── libgvm_boreas.pc.in ├── ping.c ├── ping.h ├── ping_tests.c ├── sniffer.c ├── sniffer.h ├── sniffer_tests.c ├── util.c ├── util.h └── util_tests.c ├── cliff.toml ├── cmake ├── MacroAddUnitTest.cmake └── MacroGitGetRevision.cmake ├── doc ├── CMakeLists.txt ├── Doxyfile.in ├── Doxyfile_full.in └── Doxyfile_xml.in ├── gmp ├── CMakeLists.txt ├── gmp.c ├── gmp.h └── libgvm_gmp.pc.in ├── http ├── CMakeLists.txt ├── httputils.c ├── httputils.h ├── httputils_test.c └── libgvm_http.pc.in ├── openvasd ├── CMakeLists.txt ├── libgvm_openvasd.pc.in ├── openvasd.c ├── openvasd.h └── openvasd_tests.c ├── osp ├── CMakeLists.txt ├── libgvm_osp.pc.in ├── osp.c ├── osp.h └── osp_tests.c ├── tests ├── CMakeLists.txt └── test-hosts.c └── util ├── CMakeLists.txt ├── authutils.c ├── authutils.h ├── compressutils.c ├── compressutils.h ├── compressutils_tests.c ├── cpeutils.c ├── cpeutils.h ├── cpeutils_tests.c ├── fileutils.c ├── fileutils.h ├── gpgmeutils.c ├── gpgmeutils.h ├── json.c ├── json.h ├── json_tests.c ├── jsonpull.c ├── jsonpull.h ├── jsonpull_tests.c ├── kb.c ├── kb.h ├── kb_tests.c ├── ldaputils.c ├── ldaputils.h ├── libgvm_util.pc.in ├── mqtt.c ├── mqtt.h ├── mqtt_tests.c ├── nvticache.c ├── nvticache.h ├── passwordbasedauthentication.c ├── passwordbasedauthentication.h ├── passwordbasedauthentication_tests.c ├── radiusutils.c ├── radiusutils.h ├── radiusutils_tests.c ├── serverutils.c ├── serverutils.h ├── serverutils_tests.c ├── sshutils.c ├── sshutils.h ├── uuidutils.c ├── uuidutils.h ├── versionutils.c ├── versionutils.h ├── versionutils_tests.c ├── vtparser.c ├── vtparser.h ├── vtparser_tests.c ├── xmlutils.c ├── xmlutils.h └── xmlutils_tests.c /.clang-format: -------------------------------------------------------------------------------- 1 | # clang-format configuration for Greenbone C code 2 | # 3 | # Minimum required clang-format version: 6.0 4 | 5 | --- 6 | AlignAfterOpenBracket: Align 7 | AlignConsecutiveAssignments: 'false' 8 | AlignConsecutiveDeclarations: 'false' 9 | AlignEscapedNewlines: Left 10 | AlignOperands: 'true' 11 | AlignTrailingComments: 'true' 12 | AllowAllParametersOfDeclarationOnNextLine: 'false' 13 | AllowShortBlocksOnASingleLine: 'false' 14 | AllowShortCaseLabelsOnASingleLine: 'false' 15 | AllowShortFunctionsOnASingleLine: None 16 | AllowShortIfStatementsOnASingleLine: 'false' 17 | AllowShortLoopsOnASingleLine: 'false' 18 | AlwaysBreakAfterReturnType: All 19 | AlwaysBreakBeforeMultilineStrings: 'false' 20 | BinPackArguments: 'true' 21 | BinPackParameters: 'true' 22 | BreakBeforeBinaryOperators: NonAssignment 23 | BreakBeforeBraces: GNU 24 | BreakBeforeTernaryOperators: 'true' 25 | BreakStringLiterals: 'true' 26 | ColumnLimit: '80' 27 | ContinuationIndentWidth: '2' 28 | DerivePointerAlignment: 'false' 29 | IncludeBlocks: Regroup 30 | IndentCaseLabels: 'false' 31 | IndentWidth: '2' 32 | IndentWrappedFunctionNames: 'false' 33 | KeepEmptyLinesAtTheStartOfBlocks: 'false' 34 | Language: Cpp 35 | MaxEmptyLinesToKeep: '1' 36 | PointerAlignment: Right 37 | ReflowComments: 'true' 38 | SortIncludes: 'true' 39 | SpaceAfterCStyleCast: 'true' 40 | SpaceBeforeAssignmentOperators: 'true' 41 | SpaceBeforeParens: Always 42 | SpaceInEmptyParentheses: 'false' 43 | SpacesInCStyleCastParentheses: 'false' 44 | SpacesInParentheses: 'false' 45 | SpacesInSquareBrackets: 'false' 46 | UseTab: Never 47 | 48 | ... 49 | -------------------------------------------------------------------------------- /.codecov.yml: -------------------------------------------------------------------------------- 1 | comment: off 2 | -------------------------------------------------------------------------------- /.devcontainer/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM debian:stable-slim 2 | 3 | ARG DEBIAN_FRONTEND=noninteractive 4 | ARG CGREEN_VERSION=1.6.2 5 | ARG CGREEN_HASH=fe6be434cbe280330420106bd5d667f1bc84ae9468960053100dbf17071036b9 6 | 7 | ARG USERNAME=dev 8 | ARG USER_UID=1000 9 | ARG USER_GID=$USER_UID 10 | 11 | # install dependencies 12 | RUN --mount=type=bind,source=.github,target=/source/ \ 13 | sh /source/install-dependencies.sh /source/build-dependencies.list 14 | RUN apt-get install -y --no-install-recommends \ 15 | clang-format 16 | 17 | # add non-root user 18 | RUN groupadd --gid $USER_GID $USERNAME \ 19 | && useradd --uid $USER_UID --gid $USER_GID -m $USERNAME -s /bin/bash \ 20 | && apt-get update \ 21 | && apt-get install -y --no-install-recommends sudo \ 22 | && echo $USERNAME ALL=\(root\) NOPASSWD:ALL > /etc/sudoers.d/$USERNAME \ 23 | && chmod 0440 /etc/sudoers.d/$USERNAME 24 | 25 | # install cgreen 26 | RUN curl -sSL -o cgreen.tar.gz https://github.com/cgreen-devs/cgreen/archive/refs/tags/$CGREEN_VERSION.tar.gz \ 27 | && echo "$CGREEN_HASH cgreen.tar.gz" | sha256sum -c - \ 28 | && tar -xzf cgreen.tar.gz \ 29 | && cd cgreen-* \ 30 | && cmake -Bbuild -DCMAKE_BUILD_TYPE=Release \ 31 | && cmake --build build -j$(nproc) -- install \ 32 | && ldconfig 33 | 34 | CMD /bin/bash 35 | -------------------------------------------------------------------------------- /.devcontainer/devcontainer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "gvm-libs stable", 3 | // "image": "gvm-libs:stable", 4 | "build": { 5 | "dockerfile": "Dockerfile", 6 | "context": ".." 7 | }, 8 | "remoteUser": "dev", 9 | "customizations": { 10 | "vscode": { 11 | "extensions": [ 12 | "ms-vscode.cpptools-extension-pack" 13 | ] 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /.docker/prod-oldstable.Dockerfile: -------------------------------------------------------------------------------- 1 | FROM debian:oldstable-slim AS build 2 | 3 | ARG DEBIAN_FRONTEND=noninteractive 4 | 5 | # Install 6 | COPY . /source 7 | RUN sh /source/.github/install-dependencies.sh \ 8 | /source/.github/build-dependencies.list \ 9 | && rm -rf /var/lib/apt/lists/* 10 | RUN cmake -DCMAKE_BUILD_TYPE=Release -DOPENVASD=0 -DENABLE_AGENTS=0 -B/build /source \ 11 | && DESTDIR=/install cmake --build /build -j$(nproc) -- install 12 | 13 | FROM debian:oldstable-slim 14 | 15 | ARG DEBIAN_FRONTEND=noninteractive 16 | 17 | RUN --mount=type=bind,source=.github,target=/source/ \ 18 | sh /source/install-dependencies.sh \ 19 | /source/runtime-dependencies.oldstable.list \ 20 | && rm -rf /var/lib/apt/lists/* 21 | 22 | COPY --from=build /install/ / 23 | 24 | RUN ldconfig 25 | -------------------------------------------------------------------------------- /.docker/prod-testing.Dockerfile: -------------------------------------------------------------------------------- 1 | FROM debian:testing-slim AS build 2 | 3 | ARG DEBIAN_FRONTEND=noninteractive 4 | 5 | # Install 6 | COPY . /source 7 | RUN sh /source/.github/install-dependencies.sh \ 8 | /source/.github/build-dependencies.list \ 9 | && rm -rf /var/lib/apt/lists/* 10 | RUN cmake -DCMAKE_BUILD_TYPE=Release -B/build /source \ 11 | && DESTDIR=/install cmake --build /build -j$(nproc) -- install 12 | 13 | FROM debian:testing-slim 14 | 15 | ARG DEBIAN_FRONTEND=noninteractive 16 | 17 | RUN --mount=type=bind,source=.github,target=/source/ \ 18 | sh /source/install-dependencies.sh \ 19 | /source/runtime-dependencies.testing.list \ 20 | && rm -rf /var/lib/apt/lists/* 21 | 22 | COPY --from=build /install/ / 23 | 24 | RUN ldconfig 25 | -------------------------------------------------------------------------------- /.docker/prod.Dockerfile: -------------------------------------------------------------------------------- 1 | FROM debian:stable-slim AS build 2 | 3 | ARG DEBIAN_FRONTEND=noninteractive 4 | 5 | # Install 6 | COPY . /source 7 | RUN sh /source/.github/install-dependencies.sh \ 8 | /source/.github/build-dependencies.list \ 9 | && rm -rf /var/lib/apt/lists/* 10 | RUN cmake -DCMAKE_BUILD_TYPE=Release -B/build /source \ 11 | && DESTDIR=/install cmake --build /build -j$(nproc) -- install 12 | 13 | FROM debian:stable-slim 14 | 15 | ARG DEBIAN_FRONTEND=noninteractive 16 | 17 | RUN --mount=type=bind,source=.github,target=/source/ \ 18 | sh /source/install-dependencies.sh \ 19 | /source/runtime-dependencies.stable.list \ 20 | && rm -rf /var/lib/apt/lists/* 21 | 22 | COPY --from=build /install/ / 23 | 24 | RUN ldconfig 25 | -------------------------------------------------------------------------------- /.dockerignore: -------------------------------------------------------------------------------- 1 | .docker/ 2 | .vscode/ 3 | .mergify.yml 4 | build/ 5 | -------------------------------------------------------------------------------- /.gersemirc: -------------------------------------------------------------------------------- 1 | indent: 2 2 | definitions: [./cmake/] 3 | -------------------------------------------------------------------------------- /.github/CODEOWNERS: -------------------------------------------------------------------------------- 1 | # default reviewers 2 | * @greenbone/scanner-maintainers @greenbone/gea 3 | 4 | # dev ops 5 | .github/ @greenbone/scanner-maintainers @greenbone/gea @greenbone/devops 6 | .docker/ @greenbone/scanner-maintainers @greenbone/gea @greenbone/devops 7 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/bug-report.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Bug Report 3 | about: Report an issue with gvm-libs 4 | title: '' 5 | labels: bug 6 | assignees: '' 7 | 8 | --- 9 | 10 | 27 | 28 | ### Expected behavior 29 | 30 | 34 | 35 | ### Actual behavior 36 | 37 | 45 | 46 | ### Steps to reproduce 47 | 48 | 52 | 53 | 1. 54 | 2. 55 | 3. 56 | 57 | ### GVM versions 58 | 59 | **gsa:** (gsad --version) 60 | 61 | **gvm:** (gvmd --version) 62 | 63 | **openvas:** (openvas --version) 64 | 65 | **gvm-libs:** 66 | 67 | ### Environment 68 | 69 | **Operating system:** 70 | 71 | 72 | 73 | **Installation method / source:** (packages, source installation) 74 | 75 | ### Logfiles 76 | 77 | 78 | ``` 79 | ``` 80 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/config.yml: -------------------------------------------------------------------------------- 1 | blank_issues_enabled: false 2 | contact_links: 3 | - name: Greenbone Community Forum 4 | url: https://community.greenbone.net/c/gse 5 | about: Please ask and answer questions here. 6 | -------------------------------------------------------------------------------- /.github/build-dependencies.list: -------------------------------------------------------------------------------- 1 | build-essential 2 | ca-certificates 3 | cmake 4 | curl 5 | gcovr 6 | git 7 | gnupg 8 | lcov 9 | libcjson-dev 10 | libcurl4-gnutls-dev 11 | libgcrypt-dev 12 | libglib2.0-dev 13 | libgnutls28-dev 14 | libgpgme-dev 15 | libhiredis-dev 16 | libldap2-dev 17 | libnet1-dev 18 | libpaho-mqtt-dev 19 | libpcap-dev 20 | libradcli-dev 21 | libssh-dev 22 | libxml2-dev 23 | pkg-config 24 | uuid-dev 25 | -------------------------------------------------------------------------------- /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | version: 2 2 | updates: 3 | - package-ecosystem: "github-actions" 4 | directory: "/" 5 | schedule: 6 | interval: "weekly" 7 | groups: 8 | github-actions: 9 | patterns: 10 | - "*" 11 | -------------------------------------------------------------------------------- /.github/install-dependencies.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # This script installs gvm-libs dependencies 3 | set -e 4 | 5 | BASEDIR=$(dirname "$0") 6 | DEFAULT_DEPENDENCIES_FILE="$BASEDIR/build-dependencies.list" 7 | DEPENDENCIES_FILE=${1:-$DEFAULT_DEPENDENCIES_FILE} 8 | 9 | if [[ ! -f "$DEPENDENCIES_FILE" ]]; then 10 | echo "Dependencies file not found: $DEPENDENCIES_FILE" 11 | exit 1 12 | fi 13 | 14 | apt-get update && \ 15 | apt-get install -y --no-install-recommends --no-install-suggests \ 16 | $(grep -v '#' "$DEPENDENCIES_FILE") 17 | -------------------------------------------------------------------------------- /.github/runtime-dependencies.oldstable.list: -------------------------------------------------------------------------------- 1 | libcjson1 2 | libgcrypt20 3 | libglib2.0-0 4 | libgnutls30 5 | libgpgme11 6 | libhiredis0.14 7 | libldap-common 8 | libnet1 9 | libpaho-mqtt1.3 10 | libpcap0.8 11 | libradcli4 12 | libssh-4 13 | libuuid1 14 | libxml2 15 | -------------------------------------------------------------------------------- /.github/runtime-dependencies.stable.list: -------------------------------------------------------------------------------- 1 | libcjson1 2 | libcurl3-gnutls 3 | libgcrypt20 4 | libglib2.0-0 5 | libgnutls30 6 | libgpgme11 7 | libhiredis0.14 8 | libldap-common 9 | libnet1 10 | libpaho-mqtt1.3 11 | libpcap0.8 12 | libradcli4 13 | libssh-4 14 | libuuid1 15 | libxml2 16 | -------------------------------------------------------------------------------- /.github/runtime-dependencies.testing.list: -------------------------------------------------------------------------------- 1 | libcjson1 2 | libcurl3t64-gnutls 3 | libgcrypt20 4 | libglib2.0-0 5 | libgnutls30 6 | libgpgme11 7 | libhiredis1.1.0 8 | libldap-common 9 | libnet1 10 | libpaho-mqtt1.3 11 | libpcap0.8 12 | libradcli4 13 | libssh-4 14 | libuuid1 15 | libxml2 16 | -------------------------------------------------------------------------------- /.github/workflows/build-docs.yml: -------------------------------------------------------------------------------- 1 | name: 'Build Documentation' 2 | 3 | on: 4 | push: 5 | branches: 6 | - main 7 | 8 | jobs: 9 | generate-doc-and-upload-coverage: 10 | name: Build XML documentation and upload coverage 11 | runs-on: ubuntu-latest 12 | container: greenbone/doxygen 13 | steps: 14 | - name: Run the c lang coverage action 15 | uses: greenbone/actions/doc-coverage-clang@v3 16 | 17 | generate-xml-doc: 18 | name: Generate documentation (XML) 19 | runs-on: ubuntu-latest 20 | container: greenbone/doxygen 21 | steps: 22 | - name: Check out gvmd 23 | uses: actions/checkout@v4 24 | - name: Generate documentation (XML) 25 | run: | 26 | mkdir build 27 | cd build 28 | cmake -DSKIP_SRC=1 .. 29 | make doc-xml 2> ~/doxygen-stderr.txt 30 | - name: Upload doxygen error output as artifact 31 | uses: actions/upload-artifact@v4 32 | with: 33 | name: doxygen-stderr.txt 34 | path: ~/doxygen-stderr.txt 35 | - name: Upload XML documentation as artifact 36 | uses: actions/upload-artifact@v4 37 | with: 38 | name: xml-doc 39 | path: build/doc/generated/xml/ 40 | -------------------------------------------------------------------------------- /.github/workflows/changelog.yml: -------------------------------------------------------------------------------- 1 | name: Show changelog since last release 2 | 3 | on: 4 | workflow_dispatch: 5 | 6 | jobs: 7 | changelog: 8 | name: Show changelog since last release 9 | runs-on: 'ubuntu-latest' 10 | steps: 11 | - name: Checkout code 12 | uses: actions/checkout@v4 13 | with: 14 | fetch-depth: 0 # for conventional commits and getting all git tags 15 | persist-credentials: false 16 | - name: Install git-cliff 17 | uses: greenbone/actions/uv@v3 18 | with: 19 | install: git-cliff 20 | - name: Determine changelog 21 | env: 22 | GITHUB_REPO: ${{ github.repository }} 23 | GITHUB_TOKEN: ${{ github.token }} 24 | run: | 25 | git-cliff -v --strip header --unreleased -o /tmp/changelog.md 26 | - name: Show changelog 27 | run: | 28 | cat /tmp/changelog.md 29 | cat /tmp/changelog.md >> $GITHUB_STEP_SUMMARY 30 | -------------------------------------------------------------------------------- /.github/workflows/ci-c.yml: -------------------------------------------------------------------------------- 1 | name: Build and test C 2 | 3 | on: 4 | push: 5 | branches: 6 | - main 7 | pull_request: 8 | branches: 9 | - main 10 | 11 | jobs: 12 | c-format-check: 13 | name: Check C Source Code Formatting 14 | runs-on: "ubuntu-latest" 15 | steps: 16 | - uses: actions/checkout@v4 17 | - name: Check Source Format 18 | id: check 19 | run: | 20 | clang-format -i -style=file {agent_controller,base,boreas,gmp,http,openvasd,osp,util}/*.{c,h} 21 | git diff --exit-code 22 | - name: Report Diff 23 | if: ${{ failure() && steps.check.outcome == 'failure' }} 24 | run: | 25 | echo "## Clang Format Check" >> $GITHUB_STEP_SUMMARY 26 | echo "Found formatting issues in the source code. Please run clang-format to fix them." >> $GITHUB_STEP_SUMMARY 27 | echo '```sh' >> $GITHUB_STEP_SUMMARY 28 | echo 'clang-format -i -style=file {agent_controller,base,boreas,gmp,http,openvasd,osp,util}/*.{c,h}' >> $GITHUB_STEP_SUMMARY 29 | echo '```' >> $GITHUB_STEP_SUMMARY 30 | echo "## Clang Format Diff" >> $GITHUB_STEP_SUMMARY 31 | echo '```diff' >> $GITHUB_STEP_SUMMARY 32 | git diff >> $GITHUB_STEP_SUMMARY 33 | echo '```' >> $GITHUB_STEP_SUMMARY 34 | 35 | cmake-format-check: 36 | name: Check CMake Formatting 37 | runs-on: "ubuntu-latest" 38 | steps: 39 | - uses: actions/checkout@v4 40 | - uses: greenbone/actions/uv@v3 41 | with: 42 | install: gersemi 43 | - name: Check CMake Format 44 | id: check 45 | run: | 46 | gersemi --check . cmake 47 | - name: Report Diff 48 | if: ${{ failure() && steps.check.outcome == 'failure' }} 49 | run: | 50 | echo "## CMake Format Check" >> $GITHUB_STEP_SUMMARY 51 | gersemi --check --no-warn-about-unknown-commands . cmake >> $GITHUB_STEP_SUMMARY 2>&1 || true 52 | echo "Found formatting issues in the source code. Please run gersemi to fix them." >> $GITHUB_STEP_SUMMARY 53 | echo '```sh' >> $GITHUB_STEP_SUMMARY 54 | echo 'gersemi -i . cmake' >> $GITHUB_STEP_SUMMARY 55 | echo '```' >> $GITHUB_STEP_SUMMARY 56 | echo "## CMake Format Diff" >> $GITHUB_STEP_SUMMARY 57 | echo '```diff' >> $GITHUB_STEP_SUMMARY 58 | gersemi --diff . cmake >> $GITHUB_STEP_SUMMARY 59 | echo '```' >> $GITHUB_STEP_SUMMARY 60 | 61 | tests: 62 | name: Unit Tests 63 | runs-on: "ubuntu-latest" 64 | container: debian:stable-slim 65 | steps: 66 | - uses: actions/checkout@v4 67 | - name: Install build dependencies 68 | run: sh .github/install-dependencies.sh .github/build-dependencies.list 69 | - name: Set git safe.directory 70 | run: git config --global --add safe.directory '*' 71 | - name: Install cgreen 72 | uses: greenbone/actions/install-cgreen@v3 73 | - name: Configure and Compile gvm-libs 74 | run: | 75 | cmake -B build -DCMAKE_BUILD_TYPE=Debug -DBUILD_TESTS=1 -DENABLE_COVERAGE=1 76 | cmake --build build -- install 77 | - name: Test gvm-libs 78 | run: | 79 | CTEST_OUTPUT_ON_FAILURE=1 cmake --build build -- tests test coverage-xml 80 | - name: Upload test coverage to Codecov 81 | uses: codecov/codecov-action@v5 82 | with: 83 | files: build/coverage/coverage.xml 84 | token: ${{ secrets.CODECOV_TOKEN }} 85 | flags: unittests 86 | 87 | scan-build: 88 | name: Scan-build gvm-libs with clang 89 | runs-on: "ubuntu-latest" 90 | container: debian:stable-slim 91 | steps: 92 | - uses: actions/checkout@v4 93 | - name: Install build dependencies 94 | run: sh .github/install-dependencies.sh .github/build-dependencies.list 95 | - name: Install clang tools 96 | run: | 97 | apt update 98 | apt-get install --no-install-recommends -y clang clang-format clang-tools 99 | - name: Configure and Scan Build gvm-libs 100 | run: | 101 | cmake -B build -DCMAKE_BUILD_TYPE=Release 102 | scan-build -o ~/scan-build-report cmake --build build 103 | - name: Upload scan-build report 104 | if: failure() 105 | uses: actions/upload-artifact@v4 106 | with: 107 | name: scan-build-report 108 | path: ~/scan-build-report/ 109 | retention-days: 7 110 | -------------------------------------------------------------------------------- /.github/workflows/codeql-analysis-c.yml: -------------------------------------------------------------------------------- 1 | name: "CodeQL" 2 | 3 | on: 4 | push: 5 | branches: 6 | - main 7 | pull_request: 8 | branches: 9 | - main 10 | paths-ignore: 11 | - '**/*.md' 12 | - '**/*.txt' 13 | schedule: 14 | - cron: '30 5 * * 0' # 5:30h on Sundays 15 | 16 | jobs: 17 | analyze: 18 | name: Analyze 19 | runs-on: ubuntu-latest 20 | permissions: 21 | actions: read 22 | contents: read 23 | security-events: write 24 | container: 25 | image: registry.community.greenbone.net/community/gvm-libs:stable 26 | 27 | strategy: 28 | fail-fast: false 29 | matrix: 30 | language: [ 'c' ] 31 | 32 | steps: 33 | - name: Checkout repository 34 | uses: actions/checkout@v4 35 | 36 | - name: Install build dependencies 37 | run: sh .github/install-dependencies.sh .github/build-dependencies.list 38 | - name: Initialize CodeQL 39 | uses: github/codeql-action/init@v3 40 | with: 41 | languages: ${{ matrix.language }} 42 | # build between init and analyze ... 43 | - name: Configure and Compile gvm-libs 44 | run: | 45 | mkdir build 46 | cd build 47 | cmake -DCMAKE_BUILD_TYPE=Debug .. 48 | make install 49 | working-directory: ${{ github.WORKSPACE }} 50 | - name: Perform CodeQL Analysis 51 | uses: github/codeql-action/analyze@v3 52 | -------------------------------------------------------------------------------- /.github/workflows/container.yml: -------------------------------------------------------------------------------- 1 | name: Build & Push Container Images 2 | 3 | on: 4 | push: 5 | branches: 6 | - main 7 | tags: 8 | - "v*" 9 | pull_request: 10 | branches: 11 | - main 12 | workflow_dispatch: 13 | inputs: 14 | ref-name: 15 | type: string 16 | description: "The ref to build a container image from. For example a tag v23.0.0." 17 | required: true 18 | 19 | jobs: 20 | build-push-debian-stable-container: 21 | name: Build and Push debian:stable to Greenbone Registry 22 | uses: greenbone/workflows/.github/workflows/container-build-push-2nd-gen.yml@main 23 | with: 24 | base-image-label: "stable" 25 | build-docker-file: .docker/prod.Dockerfile 26 | image-url: community/gvm-libs 27 | image-labels: | 28 | org.opencontainers.image.vendor=Greenbone 29 | org.opencontainers.image.base.name=debian:stable-slim 30 | ref-name: ${{ inputs.ref-name }} 31 | secrets: inherit 32 | 33 | build-push-debian-oldstable-container: 34 | name: Build and Push debian:oldstable to Greenbone Registry 35 | uses: greenbone/workflows/.github/workflows/container-build-push-2nd-gen.yml@main 36 | with: 37 | base-image-label: "oldstable" 38 | build-docker-file: .docker/prod-oldstable.Dockerfile 39 | image-url: community/gvm-libs 40 | image-labels: | 41 | org.opencontainers.image.vendor=Greenbone 42 | org.opencontainers.image.base.name=debian:oldstable-slim 43 | ref-name: ${{ inputs.ref-name }} 44 | secrets: inherit 45 | 46 | build-push-debian-testing-container: 47 | name: Build and Push debian:testing to Greenbone Registry 48 | uses: greenbone/workflows/.github/workflows/container-build-push-2nd-gen.yml@main 49 | with: 50 | base-image-label: "testing" 51 | build-docker-file: .docker/prod-testing.Dockerfile 52 | image-url: community/gvm-libs 53 | image-labels: | 54 | org.opencontainers.image.vendor=Greenbone 55 | org.opencontainers.image.base.name=debian:testing-slim 56 | ref-name: ${{ inputs.ref-name }} 57 | secrets: inherit 58 | -------------------------------------------------------------------------------- /.github/workflows/conventional-commits.yml: -------------------------------------------------------------------------------- 1 | name: Conventional Commits 2 | 3 | on: 4 | pull_request_target: 5 | 6 | jobs: 7 | conventional-commits: 8 | name: Conventional Commits 9 | runs-on: ubuntu-latest 10 | steps: 11 | - name: Report Conventional Commits 12 | uses: greenbone/actions/conventional-commits@v3 13 | -------------------------------------------------------------------------------- /.github/workflows/dependency-review.yml: -------------------------------------------------------------------------------- 1 | name: "Dependency Review" 2 | on: [pull_request] 3 | 4 | permissions: 5 | contents: read 6 | pull-requests: write 7 | 8 | jobs: 9 | dependency-review: 10 | runs-on: ubuntu-latest 11 | steps: 12 | - name: "Dependency Review" 13 | uses: greenbone/actions/dependency-review@v3 14 | -------------------------------------------------------------------------------- /.github/workflows/release.yml: -------------------------------------------------------------------------------- 1 | name: Release gvm-libs 2 | 3 | on: 4 | pull_request: 5 | types: [closed] 6 | workflow_dispatch: 7 | inputs: 8 | release-type: 9 | type: choice 10 | description: What kind of release do you want to do? 11 | options: 12 | - patch 13 | - minor 14 | - major 15 | release-version: 16 | type: string 17 | description: Set an explicit version, that will overwrite release-type. Fails if version is not compliant. 18 | 19 | jobs: 20 | build-and-release: 21 | name: Create a new release 22 | # If the event is a workflow_dispatch or on of the labels 'pre release', 23 | # 'patch release', 'minor release' or 'major release' is set and PR is 24 | # closed because of a merge 25 | # NOTE: priority of set labels will be alpha > release-candidate > patch > minor > major, 26 | # so if 'major' and 'patch' labels are set, it will create a patch release. 27 | if: | 28 | ( github.event_name == 'workflow_dispatch') || ( 29 | ( contains(github.event.pull_request.labels.*.name, 'alpha release') || 30 | contains(github.event.pull_request.labels.*.name, 'rc release') || 31 | contains(github.event.pull_request.labels.*.name, 'patch release') || 32 | contains(github.event.pull_request.labels.*.name, 'minor release') || 33 | contains(github.event.pull_request.labels.*.name, 'major release')) && 34 | github.event.pull_request.merged == true ) 35 | runs-on: "ubuntu-latest" 36 | steps: 37 | - name: Selecting the Release type 38 | id: release-type 39 | uses: greenbone/actions/release-type@v3 40 | with: 41 | release-type-input: ${{ inputs.release-type }} 42 | - name: Checkout code 43 | uses: actions/checkout@v4 44 | with: 45 | fetch-depth: 0 # for conventional commits and getting all git tags 46 | persist-credentials: false 47 | ref: ${{ steps.release-type.outputs.release-ref }} 48 | - name: Determine release version 49 | id: release-version 50 | uses: greenbone/actions/release-version@v3 51 | with: 52 | release-type: ${{ steps.release-type.outputs.release-type }} 53 | release-version: ${{ inputs.release-version }} 54 | versioning-scheme: "semver" 55 | - name: Install git-cliff 56 | uses: greenbone/actions/uv@v3 57 | with: 58 | install: git-cliff 59 | - name: Determine changelog 60 | env: 61 | GITHUB_REPO: ${{ github.repository }} 62 | GITHUB_TOKEN: ${{ github.token }} 63 | run: | 64 | git-cliff -v --strip header -o /tmp/changelog.md --unreleased --tag ${{ steps.release-version.outputs.release-version }} ${{ steps.release-version.outputs.last-release-version }}..HEAD 65 | - name: Release with release action 66 | id: release 67 | uses: greenbone/actions/release@v3 68 | with: 69 | github-user: ${{ secrets.GREENBONE_BOT }} 70 | github-user-mail: ${{ secrets.GREENBONE_BOT_MAIL }} 71 | github-user-token: ${{ secrets.GREENBONE_BOT_TOKEN }} 72 | gpg-key: ${{ secrets.GPG_KEY }} 73 | gpg-fingerprint: ${{ secrets.GPG_FINGERPRINT }} 74 | gpg-passphrase: ${{ secrets.GPG_PASSPHRASE }} 75 | release-version: ${{ steps.release-version.outputs.release-version }} 76 | changelog: /tmp/changelog.md 77 | ref: ${{ steps.release-type.outputs.release-ref }} 78 | versioning-scheme: "semver" 79 | -------------------------------------------------------------------------------- /.github/workflows/sbom-upload.yml: -------------------------------------------------------------------------------- 1 | name: SBOM upload 2 | on: 3 | workflow_dispatch: 4 | push: 5 | branches: 6 | - main 7 | 8 | jobs: 9 | SBOM-upload: 10 | runs-on: ubuntu-latest 11 | permissions: 12 | id-token: write 13 | contents: write 14 | steps: 15 | - name: 'SBOM upload' 16 | uses: greenbone/actions/sbom-upload@v3 17 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | build/ 2 | .ccls 3 | # clangd 4 | # generated by 5 | # cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=1 6 | compile_commands.json 7 | .cache/ 8 | # cmake 9 | CMakeFiles/ 10 | .vscode 11 | -------------------------------------------------------------------------------- /.mergify.yml: -------------------------------------------------------------------------------- 1 | pull_request_rules: 2 | # backports from main branch 3 | - name: backport main patches to stable branch 4 | conditions: 5 | - base=main 6 | - label=backport-to-stable 7 | actions: 8 | backport: 9 | branches: 10 | - stable 11 | 12 | - name: backport main patches to oldstable branch 13 | conditions: 14 | - base=main 15 | - label=backport-to-oldstable 16 | actions: 17 | backport: 18 | branches: 19 | - oldstable 20 | 21 | - name: port main patches to middleware branch 22 | conditions: 23 | - base=main 24 | - label=backport-to-middleware 25 | actions: 26 | backport: 27 | branches: 28 | - middleware 29 | 30 | # backports from stable release branch 31 | - name: port stable patches to main branch 32 | conditions: 33 | - base=stable 34 | - label=backport-to-main 35 | actions: 36 | backport: 37 | branches: 38 | - main 39 | 40 | - name: backport stable patches to oldstable branch 41 | conditions: 42 | - base=stable 43 | - label=backport-to-oldstable 44 | actions: 45 | backport: 46 | branches: 47 | - oldstable 48 | 49 | - name: port stable patches to middleware branch 50 | conditions: 51 | - base=stable 52 | - label=backport-to-middleware 53 | actions: 54 | backport: 55 | branches: 56 | - middleware 57 | 58 | # ports from oldstable release branch 59 | - name: port oldstable patches to main branch 60 | conditions: 61 | - base=oldstable 62 | - label=backport-to-main 63 | actions: 64 | backport: 65 | branches: 66 | - main 67 | 68 | - name: port oldstable patches to stable branch 69 | conditions: 70 | - base=oldstable 71 | - label=backport-to-stable 72 | actions: 73 | backport: 74 | branches: 75 | - stable 76 | 77 | - name: port oldstable patches to middleware branch 78 | conditions: 79 | - base=oldstable 80 | - label=backport-to-middleware 81 | actions: 82 | backport: 83 | branches: 84 | - middleware 85 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ![Greenbone Logo](https://www.greenbone.net/wp-content/uploads/gb_new-logo_horizontal_rgb_small.png) 2 | 3 | # gvm-libs 4 | 5 | [![GitHub releases](https://img.shields.io/github/release/greenbone/gvm-libs.svg)](https://github.com/greenbone/gvm-libs/releases) 6 | [![Build and test C](https://github.com/greenbone/gvm-libs/actions/workflows/ci-c.yml/badge.svg)](https://github.com/greenbone/gvm-libs/actions/workflows/ci-c.yml) 7 | [![codecov](https://codecov.io/gh/greenbone/gvm-libs/graph/badge.svg?token=OUojGTMBgP)](https://codecov.io/gh/greenbone/gvm-libs) 8 | 9 | This is the libraries module for the Greenbone Community Edition. 10 | 11 | It is used for the Greenbone Enterprise appliances and provides various 12 | functionalities to support the integrated service daemons. 13 | 14 | - [Releases](#releases) 15 | - [Installation](#installation) 16 | - [Usage](#usage) 17 | - [Support](#support) 18 | - [Maintainer](#maintainer) 19 | - [Contributing](#contributing) 20 | - [Code style and formatting](#code-style-and-formatting) 21 | - [CMake formatting](#cmake-formatting) 22 | - [Static code analysis with the Clang Static Analyzer](#static-code-analysis-with-the-clang-static-analyzer) 23 | - [License](#license) 24 | 25 | ## Releases 26 | 27 | All [release files](https://github.com/greenbone/gvm-libs/releases) are signed with 28 | the [Greenbone Community Feed integrity key](https://community.greenbone.net/t/gcf-managing-the-digital-signatures/101). 29 | This gpg key can be downloaded at https://www.greenbone.net/GBCommunitySigningKey.asc 30 | and the fingerprint is `8AE4 BE42 9B60 A59B 311C 2E73 9823 FAA6 0ED1 E580`. 31 | 32 | ## Installation 33 | 34 | This module can be configured, built and installed with following commands: 35 | 36 | cmake . 37 | make install 38 | 39 | For detailed installation requirements and instructions, please see the file 40 | [INSTALL.md](INSTALL.md). 41 | 42 | If you are not familiar or comfortable building from source code, we recommend 43 | that you use the Greenbone Security Manager TRIAL (GSM TRIAL), a prepared virtual 44 | machine with a readily available setup. Information regarding the virtual machine 45 | is available at . 46 | 47 | ## Usage 48 | 49 | The `gvm-libs` module consists of the following libraries: 50 | 51 | - `base`: All basic modules which require only the `glib` library as a dependency. 52 | 53 | - `util`: All modules that require more than the `glib` library as dependency. 54 | 55 | - `gmp`: API support for the Greenbone Management Protocol (GMP). 56 | 57 | - `osp`: API support for the Open Scanner Protocol (OSP). 58 | 59 | For more information on using the functionality provided by the `gvm-libs` 60 | module please refer to the source code documentation. 61 | 62 | ## Support 63 | 64 | For any question on the usage of `gvm-libs` please use the [Greenbone Community 65 | Portal](https://community.greenbone.net/). If you found a problem with the 66 | software, please [create an issue](https://github.com/greenbone/gvm-libs/issues) 67 | on GitHub. If you are a Greenbone customer you may alternatively or additionally 68 | forward your issue to the Greenbone Support Portal. 69 | 70 | ## Maintainer 71 | 72 | This project is maintained by [Greenbone AG](https://www.greenbone.net/). 73 | 74 | ## Contributing 75 | 76 | Your contributions are highly appreciated. Please [create a pull 77 | request](https://github.com/greenbone/gvm-libs/pulls) on GitHub. Bigger changes 78 | need to be discussed with the development team via the [issues section at 79 | github](https://github.com/greenbone/gvm-libs/issues) first. 80 | 81 | Before creating a pull request, it is recommended to check the formatting for 82 | source code and cmake files. 83 | 84 | ### Code style and formatting 85 | 86 | All C source and header files are formatted using [clang-format](https://clang.llvm.org/docs/ClangFormat.html). 87 | To install clang-format on a Debian based system the following command can be 88 | used: 89 | 90 | sudo apt install clang-format 91 | 92 | To format all C source and header files run the command: 93 | 94 | make format 95 | 96 | This reformats the new code to ensure that it follows the code style and 97 | formatting guidelines. 98 | 99 | ### CMake formatting 100 | 101 | All CMake files are formatted using [gersemi](https://github.com/BlankSpruce/gersemi). 102 | To install gersemi on a Debian based system the following commands can be used: 103 | 104 | sudo apt install pipx 105 | pipx install gersemi 106 | 107 | To format all CMake files run the command: 108 | 109 | gersemi -i cmake . 110 | 111 | ### Static code analysis with the Clang Static Analyzer 112 | 113 | If you want to use the [Clang Static Analyzer](http://clang-analyzer.llvm.org/) 114 | to do a static code analysis, you can do so by prefixing the configuration and 115 | build commands with `scan-build`: 116 | 117 | scan-build cmake .. 118 | scan-build make 119 | 120 | The tool will provide a hint on how to launch a web browser with the results. 121 | 122 | It is recommended to do this analysis in a separate, empty build directory and 123 | to empty the build directory before `scan-build` call. 124 | 125 | ## License 126 | 127 | Copyright (C) 2009-2025 [Greenbone AG](https://www.greenbone.net/) 128 | 129 | Licensed under the [GNU General Public License v2.0 or later](COPYING). 130 | -------------------------------------------------------------------------------- /VERSION.in: -------------------------------------------------------------------------------- 1 | @CPACK_PACKAGE_VERSION@ -------------------------------------------------------------------------------- /agent_controller/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # SPDX-FileCopyrightText: 2015-2025 Greenbone AG 2 | # 3 | # SPDX-License-Identifier: GPL-2.0-or-later 4 | 5 | ## Library 6 | 7 | include(FindPkgConfig) 8 | 9 | if(NOT PKG_CONFIG_FOUND) 10 | message(FATAL_ERROR "pkg-config executable not found. Aborting.") 11 | endif() 12 | 13 | ## Dependency checks 14 | 15 | pkg_check_modules(GLIB REQUIRED glib-2.0>=2.42) 16 | pkg_check_modules(CURL REQUIRED libcurl>=7.83.0) 17 | pkg_check_modules(CJSON REQUIRED libcjson>=1.7.14) 18 | 19 | include_directories(${GLIB_INCLUDE_DIRS} ${CURL_INCLUDE_DIRS}) 20 | 21 | set(FILES agent_controller.c) 22 | set(HEADERS agent_controller.h) 23 | 24 | if(BUILD_STATIC) 25 | add_library(gvm_agent_controller_static STATIC ${FILES}) 26 | set_target_properties( 27 | gvm_agent_controller_static 28 | PROPERTIES 29 | OUTPUT_NAME "gvm_agent_controller" 30 | CLEAN_DIRECT_OUTPUT 1 31 | PUBLIC_HEADER "${HEADERS}" 32 | ) 33 | endif() 34 | 35 | if(BUILD_SHARED) 36 | add_library(gvm_agent_controller_shared SHARED ${FILES}) 37 | set_target_properties( 38 | gvm_agent_controller_shared 39 | PROPERTIES 40 | OUTPUT_NAME "gvm_agent_controller" 41 | CLEAN_DIRECT_OUTPUT 1 42 | SOVERSION "${PROJECT_VERSION_MAJOR}" 43 | VERSION "${CPACK_PACKAGE_VERSION}" 44 | PUBLIC_HEADER "${HEADERS}" 45 | ) 46 | 47 | target_link_libraries( 48 | gvm_agent_controller_shared 49 | LINK_PRIVATE 50 | ${GLIB_LDFLAGS} 51 | ${CURL_LDFLAGS} 52 | gvm_http_shared 53 | gvm_util_shared 54 | ${LINKER_HARDENING_FLAGS} 55 | ) 56 | endif() 57 | 58 | ## Tests 59 | 60 | if(BUILD_TESTS) 61 | add_unit_test( 62 | agent-controller-test 63 | agent_controller_tests.c 64 | gvm_base_shared 65 | gvm_util_shared 66 | gvm_http_shared 67 | ${GLIB_LDFLAGS} 68 | ${CJSON_LDFLAGS} 69 | ${CURL_LDFLAGS} 70 | ${LINKER_HARDENING_FLAGS} 71 | ) 72 | endif() 73 | 74 | ## Install 75 | 76 | configure_file( 77 | libgvm_agent_controller.pc.in 78 | ${CMAKE_BINARY_DIR}/libgvm_agent_controller.pc 79 | @ONLY 80 | ) 81 | 82 | install( 83 | FILES ${CMAKE_BINARY_DIR}/libgvm_agent_controller.pc 84 | DESTINATION ${LIBDIR}/pkgconfig 85 | ) 86 | 87 | if(BUILD_STATIC) 88 | install( 89 | TARGETS gvm_agent_controller_static 90 | RUNTIME DESTINATION ${BINDIR} 91 | ARCHIVE DESTINATION ${LIBDIR} 92 | PUBLIC_HEADER DESTINATION "${INCLUDEDIR}/gvm/agent_controller" 93 | ) 94 | endif() 95 | 96 | if(BUILD_SHARED) 97 | install( 98 | TARGETS gvm_agent_controller_shared 99 | RUNTIME DESTINATION ${BINDIR} 100 | LIBRARY DESTINATION ${LIBDIR} 101 | ARCHIVE DESTINATION ${LIBDIR} 102 | PUBLIC_HEADER DESTINATION "${INCLUDEDIR}/gvm/agent_controller" 103 | ) 104 | endif() 105 | 106 | ## End 107 | -------------------------------------------------------------------------------- /agent_controller/libgvm_agent_controller.pc.in: -------------------------------------------------------------------------------- 1 | prefix=@CMAKE_INSTALL_PREFIX@ 2 | exec_prefix=@EXEC_PREFIX@ 3 | libdir=@LIBDIR@ 4 | includedir=@INCLUDEDIR@ 5 | 6 | Name: gvmlibs-agent_controller 7 | Description: Greenbone Vulnerability Management Library agent_controller 8 | Version: @LIBGVMCONFIG_VERSION@ 9 | Requires.private: glib-2.0 >= 2.42.0 10 | Cflags: -I${includedir} -I${includedir}/gvm 11 | Libs: -L${libdir} -lgvm_agent_controller 12 | -------------------------------------------------------------------------------- /base/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # SPDX-FileCopyrightText: 2009-2023 Greenbone AG 2 | # 3 | # SPDX-License-Identifier: GPL-2.0-or-later 4 | 5 | include(FindPkgConfig) 6 | 7 | if(NOT PKG_CONFIG_FOUND) 8 | message(FATAL_ERROR "pkg-config executable not found. Aborting.") 9 | endif(NOT PKG_CONFIG_FOUND) 10 | 11 | ## Dependency checks 12 | 13 | pkg_check_modules(GLIB REQUIRED glib-2.0>=2.42) 14 | 15 | if($ENV{BUILD_SENTRY}) 16 | find_package(SENTRY REQUIRED) 17 | if(${SENTRY_FOUND}) 18 | message("Building with Sentry integration") 19 | set(SENTRY_LDFLAGS "-lsentry") 20 | set(SENTRY_INCLUDE_DIR "${CMAKE_INSTALL_PREFIX}/include") 21 | set(SENTRY_CFLAGS "-I${SENTRY_INCLUDE_DIR}") 22 | add_definitions(-DHAVE_SENTRY=1) 23 | endif(${SENTRY_FOUND}) 24 | endif($ENV{BUILD_SENTRY}) 25 | 26 | include_directories(${GLIB_INCLUDE_DIRS} ${SENTRY_INCLUDE_DIR}) 27 | 28 | set( 29 | FILES 30 | array.c 31 | credentials.c 32 | cvss.c 33 | drop_privileges.c 34 | hosts.c 35 | logging.c 36 | logging_domain.c 37 | networking.c 38 | nvti.c 39 | pidfile.c 40 | prefs.c 41 | proctitle.c 42 | pwpolicy.c 43 | gvm_sentry.c 44 | settings.c 45 | strings.c 46 | version.c 47 | ) 48 | 49 | set( 50 | HEADERS 51 | array.h 52 | credentials.h 53 | cvss.h 54 | drop_privileges.h 55 | hosts.h 56 | logging.h 57 | logging_domain.h 58 | networking.h 59 | nvti.h 60 | pidfile.h 61 | prefs.h 62 | proctitle.h 63 | pwpolicy.h 64 | gvm_sentry.h 65 | settings.h 66 | strings.h 67 | version.h 68 | ) 69 | 70 | if(BUILD_STATIC) 71 | set(LIBGVM_BASE_NAME gvm_base_static) 72 | add_library(gvm_base_static STATIC ${FILES}) 73 | target_link_libraries(gvm_base_static m) 74 | set_target_properties(gvm_base_static PROPERTIES OUTPUT_NAME "gvm_base") 75 | set_target_properties(gvm_base_static PROPERTIES CLEAN_DIRECT_OUTPUT 1) 76 | set_target_properties(gvm_base_static PROPERTIES PUBLIC_HEADER "${HEADERS}") 77 | endif(BUILD_STATIC) 78 | 79 | if(BUILD_SHARED) 80 | set(LIBGVM_BASE_NAME gvm_base_shared) 81 | add_library(gvm_base_shared SHARED ${FILES}) 82 | set_target_properties(gvm_base_shared PROPERTIES OUTPUT_NAME "gvm_base") 83 | set_target_properties(gvm_base_shared PROPERTIES CLEAN_DIRECT_OUTPUT 1) 84 | set_target_properties( 85 | gvm_base_shared 86 | PROPERTIES SOVERSION "${PROJECT_VERSION_MAJOR}" 87 | ) 88 | set_target_properties( 89 | gvm_base_shared 90 | PROPERTIES VERSION "${CPACK_PACKAGE_VERSION}" 91 | ) 92 | set_target_properties(gvm_base_shared PROPERTIES PUBLIC_HEADER "${HEADERS}") 93 | 94 | target_link_libraries( 95 | gvm_base_shared 96 | LINK_PRIVATE ${GLIB_LDFLAGS} ${LINKER_HARDENING_FLAGS} ${SENTRY_LDFLAGS} m 97 | ) 98 | endif(BUILD_SHARED) 99 | 100 | set(LIBGVM_BASE_NAME ${LIBGVM_BASE_NAME} PARENT_SCOPE) 101 | 102 | if(GVM_SYSCONF_DIR) 103 | add_definitions(-DGVM_SYSCONF_DIR="${GVM_SYSCONF_DIR}") 104 | endif(GVM_SYSCONF_DIR) 105 | 106 | ## Tests 107 | 108 | if(BUILD_TESTS) 109 | add_link_options(-g -lgcov --coverage) 110 | add_compile_options(-g -ftest-coverage -fprofile-arcs) 111 | 112 | add_unit_test( 113 | array-test 114 | array_tests.c 115 | ${GLIB_LDFLAGS} 116 | ${LINKER_HARDENING_FLAGS} 117 | ) 118 | add_unit_test( 119 | cvss-test 120 | cvss_tests.c 121 | -lm 122 | ${GLIB_LDFLAGS} 123 | ${LINKER_HARDENING_FLAGS} 124 | ) 125 | 126 | set( 127 | NETWORKING_TEST_LINKER_WRAP_OPTIONS 128 | "-Wl,-wrap,g_io_channel_new_file,-wrap,g_io_channel_shutdown" 129 | ) 130 | add_unit_test( 131 | networking-test 132 | networking_tests.c 133 | gvm_base_shared 134 | ${CGREEN_LIBRARIES} 135 | ${GLIB_LDFLAGS} 136 | ${LINKER_HARDENING_FLAGS} 137 | ${NETWORKING_TEST_LINKER_WRAP_OPTIONS} 138 | ) 139 | add_unit_test( 140 | pwpolicy-test 141 | pwpolicy_tests.c 142 | ${GLIB_LDFLAGS} 143 | ${LINKER_HARDENING_FLAGS} 144 | ) 145 | add_unit_test( 146 | version-test 147 | version_tests.c 148 | ${GLIB_LDFLAGS} 149 | ${LINKER_HARDENING_FLAGS} 150 | ) 151 | add_unit_test( 152 | nvti-test 153 | nvti_tests.c 154 | ${GLIB_LDFLAGS} 155 | ${LINKER_HARDENING_FLAGS} 156 | ) 157 | add_unit_test( 158 | hosts-test 159 | hosts_tests.c 160 | gvm_base_shared 161 | gvm_util_shared 162 | ${GLIB_LDFLAGS} 163 | ${LINKER_HARDENING_FLAGS} 164 | ) 165 | add_unit_test( 166 | logging-domain-test 167 | logging_domain_tests.c 168 | gvm_base_shared 169 | ${GLIB_LDFLAGS} 170 | ${LINKER_HARDENING_FLAGS} 171 | ) 172 | add_unit_test( 173 | logging-test 174 | logging_tests.c 175 | gvm_base_shared 176 | ${GLIB_LDFLAGS} 177 | ${LINKER_HARDENING_FLAGS} 178 | ) 179 | endif(BUILD_TESTS) 180 | 181 | ## Install 182 | 183 | configure_file(libgvm_base.pc.in ${CMAKE_BINARY_DIR}/libgvm_base.pc @ONLY) 184 | 185 | install(DIRECTORY DESTINATION ${GVM_RUN_DIR}) 186 | 187 | install( 188 | FILES ${CMAKE_BINARY_DIR}/libgvm_base.pc 189 | DESTINATION ${LIBDIR}/pkgconfig 190 | ) 191 | 192 | if(BUILD_STATIC) 193 | install( 194 | TARGETS gvm_base_static 195 | RUNTIME DESTINATION ${BINDIR} 196 | ARCHIVE DESTINATION ${LIBDIR} 197 | PUBLIC_HEADER DESTINATION "${INCLUDEDIR}/gvm/base" 198 | ) 199 | endif(BUILD_STATIC) 200 | 201 | if(BUILD_SHARED) 202 | install( 203 | TARGETS gvm_base_shared 204 | RUNTIME DESTINATION ${BINDIR} 205 | LIBRARY DESTINATION ${LIBDIR} 206 | ARCHIVE DESTINATION ${LIBDIR} 207 | PUBLIC_HEADER DESTINATION "${INCLUDEDIR}/gvm/base" 208 | ) 209 | endif(BUILD_SHARED) 210 | 211 | ## End 212 | -------------------------------------------------------------------------------- /base/array.c: -------------------------------------------------------------------------------- 1 | /* SPDX-FileCopyrightText: 2009-2023 Greenbone AG 2 | * 3 | * SPDX-License-Identifier: GPL-2.0-or-later 4 | */ 5 | 6 | /** 7 | * @file 8 | * @brief Array utilities. 9 | */ 10 | 11 | #include "array.h" 12 | 13 | #undef G_LOG_DOMAIN 14 | /** 15 | * @brief GLib log domain. 16 | */ 17 | #define G_LOG_DOMAIN "libgvm base" 18 | 19 | /** 20 | * @brief Make a global array. 21 | * 22 | * @return New array. 23 | */ 24 | GPtrArray * 25 | make_array (void) 26 | { 27 | return g_ptr_array_new (); 28 | } 29 | 30 | /** 31 | * @brief Reset an array. 32 | * 33 | * @param[in] array Pointer to array. 34 | */ 35 | void 36 | array_reset (array_t **array) 37 | { 38 | array_free (*array); 39 | *array = make_array (); 40 | } 41 | 42 | /** 43 | * @brief Free global array value. 44 | * 45 | * Also g_free any elements. 46 | * 47 | * @param[in] array Pointer to array. 48 | */ 49 | void 50 | array_free (GPtrArray *array) 51 | { 52 | if (array) 53 | { 54 | guint index = array->len; 55 | while (index--) 56 | g_free (g_ptr_array_index (array, index)); 57 | g_ptr_array_free (array, TRUE); 58 | } 59 | } 60 | 61 | /** 62 | * @brief Push a generic pointer onto an array. 63 | * 64 | * @param[in] array Array. 65 | * @param[in] pointer Pointer. 66 | */ 67 | void 68 | array_add (array_t *array, gpointer pointer) 69 | { 70 | if (array) 71 | g_ptr_array_add (array, pointer); 72 | } 73 | 74 | /** 75 | * @brief Terminate an array. 76 | * 77 | * @param[in] array Array. 78 | */ 79 | void 80 | array_terminate (array_t *array) 81 | { 82 | if (array) 83 | g_ptr_array_add (array, NULL); 84 | } 85 | -------------------------------------------------------------------------------- /base/array.h: -------------------------------------------------------------------------------- 1 | /* SPDX-FileCopyrightText: 2009-2023 Greenbone AG 2 | * 3 | * SPDX-License-Identifier: GPL-2.0-or-later 4 | */ 5 | 6 | /** 7 | * @file 8 | * @brief Array utilities. 9 | */ 10 | 11 | #ifndef _GVM_ARRAY_H 12 | #define _GVM_ARRAY_H 13 | 14 | #include 15 | 16 | typedef GPtrArray array_t; 17 | 18 | GPtrArray * 19 | make_array (void); 20 | 21 | void 22 | array_reset (array_t **array); 23 | 24 | void 25 | array_free (GPtrArray *array); 26 | 27 | void 28 | array_add (array_t *array, gpointer pointer); 29 | 30 | void 31 | array_terminate (array_t *array); 32 | 33 | #endif /* not _GVM_ARRAY_H */ 34 | -------------------------------------------------------------------------------- /base/array_tests.c: -------------------------------------------------------------------------------- 1 | /* SPDX-FileCopyrightText: 2009-2023 Greenbone AG 2 | * 3 | * SPDX-License-Identifier: GPL-2.0-or-later 4 | */ 5 | 6 | #include "array.c" 7 | 8 | #include 9 | #include 10 | 11 | Describe (array); 12 | BeforeEach (array) 13 | { 14 | } 15 | AfterEach (array) 16 | { 17 | } 18 | 19 | /* make_array */ 20 | 21 | Ensure (array, make_array_never_returns_null) 22 | { 23 | assert_that (make_array (), is_not_null); 24 | } 25 | 26 | /* Test suite. */ 27 | 28 | int 29 | main (int argc, char **argv) 30 | { 31 | TestSuite *suite; 32 | 33 | suite = create_test_suite (); 34 | 35 | add_test_with_context (suite, array, make_array_never_returns_null); 36 | 37 | if (argc > 1) 38 | return run_single_test (suite, argv[1], create_text_reporter ()); 39 | 40 | return run_test_suite (suite, create_text_reporter ()); 41 | } 42 | -------------------------------------------------------------------------------- /base/credentials.c: -------------------------------------------------------------------------------- 1 | /* SPDX-FileCopyrightText: 2010-2023 Greenbone AG 2 | * 3 | * SPDX-License-Identifier: GPL-2.0-or-later 4 | */ 5 | 6 | /** 7 | * @file 8 | * @brief Credential pairs and triples. 9 | */ 10 | 11 | #include "credentials.h" 12 | 13 | #include "strings.h" /* for gvm_append_text */ 14 | 15 | #include 16 | 17 | #undef G_LOG_DOMAIN 18 | /** 19 | * @brief GLib log domain. 20 | */ 21 | #define G_LOG_DOMAIN "libgvm base" 22 | 23 | /** 24 | * @brief Free credentials. 25 | * 26 | * Free the members of a credentials pair. 27 | * 28 | * @param[in] credentials Pointer to the credentials. 29 | */ 30 | void 31 | free_credentials (credentials_t *credentials) 32 | { 33 | g_free (credentials->username); 34 | g_free (credentials->password); 35 | g_free (credentials->uuid); 36 | g_free (credentials->timezone); 37 | g_free (credentials->role); 38 | g_free (credentials->severity_class); 39 | memset (credentials, '\0', sizeof (*credentials)); 40 | } 41 | 42 | /** 43 | * @brief Append text to the username of a credential pair. 44 | * 45 | * @param[in] credentials Credentials. 46 | * @param[in] text The text to append. 47 | * @param[in] length Length of the text. 48 | */ 49 | void 50 | append_to_credentials_username (credentials_t *credentials, const char *text, 51 | gsize length) 52 | { 53 | gvm_append_text (&credentials->username, text, length); 54 | } 55 | 56 | /** 57 | * @brief Append text to the password of a credential pair. 58 | * 59 | * @param[in] credentials Credentials. 60 | * @param[in] text The text to append. 61 | * @param[in] length Length of the text. 62 | */ 63 | void 64 | append_to_credentials_password (credentials_t *credentials, const char *text, 65 | gsize length) 66 | { 67 | gvm_append_text (&credentials->password, text, length); 68 | } 69 | -------------------------------------------------------------------------------- /base/credentials.h: -------------------------------------------------------------------------------- 1 | /* SPDX-FileCopyrightText: 2010-2023 Greenbone AG 2 | * 3 | * SPDX-License-Identifier: GPL-2.0-or-later 4 | */ 5 | 6 | /** 7 | * @file 8 | * @brief Credential pairs and triples. 9 | */ 10 | 11 | #ifndef _GVM_CREDENTIALS_H 12 | #define _GVM_CREDENTIALS_H 13 | 14 | #include 15 | 16 | /** 17 | * @brief A username password pair. 18 | */ 19 | typedef struct 20 | { 21 | /*@null@ */ gchar *username; 22 | ///< Login name of user. 23 | /*@null@ */ gchar *password; 24 | ///< Password of user. 25 | /*@null@ */ gchar *uuid; 26 | ///< UUID of user. 27 | /*@null@ */ gchar *timezone; 28 | ///< Timezone of user. 29 | /*@null@ */ double default_severity; 30 | ///< Default Severity setting of user. 31 | /*@null@ */ gchar *severity_class; 32 | ///< Severity Class setting of user. 33 | /*@null@ */ int dynamic_severity; 34 | ///< Dynamic Severity setting of user. 35 | /*@null@ */ gchar *role; 36 | ///< Role of user. 37 | /*@null@ */ int excerpt_size; 38 | ///< Note/Override Excerpt Size setting of user. 39 | } credentials_t; 40 | 41 | void 42 | free_credentials (credentials_t *credentials); 43 | 44 | void 45 | append_to_credentials_username (credentials_t *credentials, const char *text, 46 | gsize length); 47 | 48 | void 49 | append_to_credentials_password (credentials_t *credentials, const char *text, 50 | gsize length); 51 | 52 | #endif /* _GVM_CREDENTIALS_H */ 53 | -------------------------------------------------------------------------------- /base/cvss.h: -------------------------------------------------------------------------------- 1 | /* SPDX-FileCopyrightText: 2012-2023 Greenbone AG 2 | * 3 | * SPDX-License-Identifier: GPL-2.0-or-later 4 | */ 5 | 6 | /** 7 | * @file 8 | * @brief Protos for CVSS utility functions. 9 | * 10 | * This file contains the protos for \ref cvss.c 11 | */ 12 | 13 | #ifndef _GVM_CVSS_H 14 | #define _GVM_CVSS_H 15 | 16 | #include 17 | 18 | double 19 | get_cvss_score_from_base_metrics (const char *); 20 | 21 | #endif /* not _GVM_CVSS_H */ 22 | -------------------------------------------------------------------------------- /base/drop_privileges.c: -------------------------------------------------------------------------------- 1 | /* SPDX-FileCopyrightText: 2010-2023 Greenbone AG 2 | * 3 | * SPDX-License-Identifier: GPL-2.0-or-later 4 | */ 5 | 6 | /** 7 | * @file 8 | * @brief Basic support to drop privileges. 9 | */ 10 | 11 | #include "drop_privileges.h" 12 | 13 | #include /* for initgroups */ 14 | #include /* for passwd, getpwnam */ 15 | #include 16 | #include /* for geteuid, setgid, setuid */ 17 | 18 | #undef G_LOG_DOMAIN 19 | /** 20 | * @brief GLib log domain. 21 | */ 22 | #define G_LOG_DOMAIN "libgvm base" 23 | 24 | /** 25 | * @brief Sets an error and return \p errorcode 26 | * 27 | * @param error Error to set. 28 | * @param errorcode Errorcode (possible values defined in drop_privileges.h), 29 | * will be returned. 30 | * @param message Message to attach to the error. 31 | * 32 | * @return \p errorcode 33 | */ 34 | static gint 35 | drop_privileges_error (GError **error, gint errorcode, const gchar *message) 36 | { 37 | g_set_error (error, GVM_DROP_PRIVILEGES, errorcode, "%s", message); 38 | return errorcode; 39 | } 40 | 41 | /** 42 | * @brief Drop privileges. 43 | * 44 | * We try to drop our (root) privileges and setuid to \p username to 45 | * minimize the risk of privilege escalation. 46 | * The current implementation is linux-specific and may not work on other 47 | * platforms. 48 | * 49 | * @param[in] username The user to become. Its safe to pass "NULL", in which 50 | * case it will default to "nobody". 51 | * @param[out] error Return location for errors or NULL if not interested 52 | * in errors. 53 | * 54 | * @return GVM_DROP_PRIVILEGES_OK in case of success. Sets \p error 55 | * otherwise and returns the error code. 56 | */ 57 | int 58 | drop_privileges (gchar *username, GError **error) 59 | { 60 | g_return_val_if_fail (*error == NULL, GVM_DROP_PRIVILEGES_ERROR_ALREADY_SET); 61 | 62 | if (username == NULL) 63 | username = "nobody"; 64 | 65 | if (geteuid () == 0) 66 | { 67 | struct passwd *user_pw; 68 | 69 | user_pw = getpwnam (username); 70 | if (user_pw) 71 | { 72 | if (initgroups (username, user_pw->pw_gid) != 0) 73 | return drop_privileges_error ( 74 | error, GVM_DROP_PRIVILEGES_FAIL_SUPPLEMENTARY, 75 | "Failed to drop supplementary groups privileges!\n"); 76 | if (setgid (user_pw->pw_gid) != 0) 77 | return drop_privileges_error (error, 78 | GVM_DROP_PRIVILEGES_FAIL_DROP_GID, 79 | "Failed to drop group privileges!\n"); 80 | if (setuid (user_pw->pw_uid) != 0) 81 | return drop_privileges_error (error, 82 | GVM_DROP_PRIVILEGES_FAIL_DROP_UID, 83 | "Failed to drop user privileges!\n"); 84 | } 85 | else 86 | { 87 | g_set_error (error, GVM_DROP_PRIVILEGES, 88 | GVM_DROP_PRIVILEGES_FAIL_UNKNOWN_USER, 89 | "Failed to get gid and uid for user %s.", username); 90 | return GVM_DROP_PRIVILEGES_FAIL_UNKNOWN_USER; 91 | } 92 | return GVM_DROP_PRIVILEGES_OK; 93 | } 94 | else 95 | { 96 | return drop_privileges_error (error, GVM_DROP_PRIVILEGES_FAIL_NOT_ROOT, 97 | "Only root can drop its privileges."); 98 | } 99 | } 100 | -------------------------------------------------------------------------------- /base/drop_privileges.h: -------------------------------------------------------------------------------- 1 | /* SPDX-FileCopyrightText: 2010-2023 Greenbone AG 2 | * 3 | * SPDX-License-Identifier: GPL-2.0-or-later 4 | */ 5 | 6 | /** 7 | * @file 8 | * @brief Privilege dropping header file. 9 | */ 10 | 11 | #ifndef _GVM_DROP_PRIVILEGES_H 12 | #define _GVM_DROP_PRIVILEGES_H 13 | 14 | #include 15 | 16 | /** 17 | * @brief The GQuark for privilege dropping errors. 18 | */ 19 | #define GVM_DROP_PRIVILEGES \ 20 | g_quark_from_static_string ("gvm-drop-privileges-error-quark") 21 | 22 | /** 23 | * @brief Definition of the return code ERROR_ALREADY_SET. 24 | */ 25 | #define GVM_DROP_PRIVILEGES_ERROR_ALREADY_SET -1 26 | 27 | /** 28 | * @brief Definition of the return code OK. 29 | */ 30 | #define GVM_DROP_PRIVILEGES_OK 0 31 | 32 | /** 33 | * @brief Definition of the return code FAIL_NOT_ROOT. 34 | */ 35 | #define GVM_DROP_PRIVILEGES_FAIL_NOT_ROOT 1 36 | 37 | /** 38 | * @brief Definition of the return code FAIL_UNKNOWN_USER. 39 | */ 40 | #define GVM_DROP_PRIVILEGES_FAIL_UNKNOWN_USER 2 41 | 42 | /** 43 | * @brief Definition of the return code FAIL_DROP_GID. 44 | */ 45 | #define GVM_DROP_PRIVILEGES_FAIL_DROP_GID 3 46 | 47 | /** 48 | * @brief Definition of the return code FAIL_DROP_UID. 49 | */ 50 | #define GVM_DROP_PRIVILEGES_FAIL_DROP_UID 4 51 | 52 | /** 53 | * @brief Definition of the return code FAIL_SUPPLEMENTARY. 54 | */ 55 | #define GVM_DROP_PRIVILEGES_FAIL_SUPPLEMENTARY 5 56 | 57 | int 58 | drop_privileges (gchar *username, GError **error); 59 | 60 | #endif 61 | -------------------------------------------------------------------------------- /base/gvm_sentry.c: -------------------------------------------------------------------------------- 1 | /* SPDX-FileCopyrightText: 2017-2023 Greenbone AG 2 | * 3 | * SPDX-License-Identifier: GPL-2.0-or-later 4 | */ 5 | 6 | /** 7 | * @file 8 | * @brief Implementation of sentry methods. 9 | * 10 | * This file contains all methods needed for sentry. To enable sentry and log 11 | * log in the sentry server, methods in this file are called. 12 | * 13 | */ 14 | 15 | #include "gvm_sentry.h" 16 | 17 | #include 18 | 19 | /** 20 | * @brief Check for sentry support 21 | * 22 | * @return 1 if gvm-libs has been built with sentry support, 0 otherwise. 23 | * 24 | */ 25 | int 26 | gvm_has_sentry_support () 27 | { 28 | #ifdef HAVE_SENTRY 29 | return 1; 30 | #endif /* HAVE_SENTRY */ 31 | return 0; 32 | } 33 | 34 | #ifdef HAVE_SENTRY 35 | int global_init_sentry = 0; 36 | 37 | /** 38 | * @brief Set global_init_sentry 39 | */ 40 | static void 41 | set_init_sentry () 42 | { 43 | global_init_sentry = 1; 44 | } 45 | 46 | /** 47 | * @brief Reset global_init_sentry 48 | */ 49 | static void 50 | reset_init_sentry () 51 | { 52 | global_init_sentry = 0; 53 | } 54 | 55 | /** 56 | * @brief Return if sentry was initialized or not 57 | */ 58 | static int 59 | is_sentry_initialized () 60 | { 61 | return global_init_sentry; 62 | } 63 | #endif /* HAVE_SENTRY */ 64 | 65 | /** 66 | * @brief Initialize Sentry 67 | * 68 | * The function does nothing if HAVE_SENTRY is not defined 69 | * 70 | * @param[in] dsn Sentry DSN 71 | * @param[in] release Module release to be sent to Sentry. 72 | */ 73 | void 74 | gvm_sentry_init (const char *dsn, const char *release) 75 | { 76 | #ifdef HAVE_SENTRY 77 | sentry_options_t *options = sentry_options_new (); 78 | sentry_options_set_dsn (options, dsn); 79 | sentry_options_set_release (options, release); 80 | sentry_options_set_sample_rate (options, 1.0); 81 | sentry_init (options); 82 | set_init_sentry (); 83 | #else 84 | (void) dsn; 85 | (void) release; 86 | #endif /* HAVE_SENTRY */ 87 | } 88 | 89 | /** 90 | * @brief Send a message to Sentry server if it was initialized 91 | * 92 | * The function does nothing if HAVE_SENTRY is not defined 93 | * 94 | * @param[in] message Message to send 95 | */ 96 | void 97 | gvm_sentry_log (const char *message) 98 | { 99 | #ifdef HAVE_SENTRY 100 | if (is_sentry_initialized ()) 101 | { 102 | sentry_capture_event (sentry_value_new_message_event ( 103 | /* level */ SENTRY_LEVEL_INFO, 104 | /* logger */ "custom", 105 | /* message */ message)); 106 | } 107 | #else 108 | (void) message; 109 | #endif /* HAVE_SENTRY */ 110 | } 111 | 112 | /** 113 | * @brief Shutdown Sentry if it was initialized. 114 | * 115 | * This function must be called before exiting to ensure that all 116 | * message has been sent to Sentry. 117 | * 118 | * The function does nothing if HAVE_SENTRY is not defined 119 | * 120 | */ 121 | void 122 | gvm_close_sentry (void) 123 | { 124 | #ifdef HAVE_SENTRY 125 | if (is_sentry_initialized ()) 126 | { 127 | sentry_close (); 128 | reset_init_sentry (); 129 | } 130 | #endif /* HAVE_SENTRY */ 131 | } 132 | -------------------------------------------------------------------------------- /base/gvm_sentry.h: -------------------------------------------------------------------------------- 1 | /* SPDX-FileCopyrightText: 2017-2023 Greenbone AG 2 | * 3 | * SPDX-License-Identifier: GPL-2.0-or-later 4 | */ 5 | 6 | /** 7 | * @file 8 | * @brief Implementation of sentry methods. 9 | * 10 | * This file contains all methods needed for sentry. To enable sentry and log 11 | * log in the sentry server, methods in this file are called. 12 | * 13 | */ 14 | 15 | #ifndef _GVM_SENTRY_H 16 | #define _GVM_SENTRY_H 17 | 18 | #ifdef HAVE_SENTRY 19 | #include 20 | #endif /* HAVE_SENTRY*/ 21 | 22 | void 23 | gvm_sentry_init (const char *, const char *); 24 | 25 | void 26 | gvm_sentry_log (const char *); 27 | 28 | void 29 | gvm_close_sentry (void); 30 | 31 | int 32 | gvm_has_sentry_support (void); 33 | 34 | #endif /* not _GVM_SENTRY_H */ 35 | -------------------------------------------------------------------------------- /base/hosts.h: -------------------------------------------------------------------------------- 1 | /* SPDX-FileCopyrightText: 2013-2023 Greenbone AG 2 | * 3 | * SPDX-License-Identifier: GPL-2.0-or-later 4 | */ 5 | 6 | /** 7 | * @file 8 | * @brief Protos and data structures for Hosts collections and single hosts 9 | * objects. 10 | * 11 | * This file contains the protos for \ref hosts.c 12 | */ 13 | 14 | #ifndef _GVM_HOSTS_H 15 | #define _GVM_HOSTS_H 16 | 17 | /** @brief Flag that indecates that this version includes 18 | * the function gvm_hosts_allowed_only() 19 | */ 20 | #define FEATURE_HOSTS_ALLOWED_ONLY 1 21 | 22 | /** @brief Flag that indecates that this version includes 23 | * the functions gvm_reverse_lookup_only_excluded() 24 | * and gvm_reverse_lookup_unify_excluded() 25 | */ 26 | #define FEATURE_REVERSE_LOOKUP_EXCLUDED 1 27 | 28 | #include /* for gchar, GList */ 29 | #include /* for in6_addr, in_addr */ 30 | 31 | /* Static values */ 32 | 33 | enum host_type 34 | { 35 | HOST_TYPE_NAME = 0, /* Hostname eg. foo */ 36 | HOST_TYPE_IPV4, /* eg. 192.168.1.1 */ 37 | HOST_TYPE_CIDR_BLOCK, /* eg. 192.168.15.0/24 */ 38 | HOST_TYPE_RANGE_SHORT, /* eg. 192.168.15.10-20 */ 39 | HOST_TYPE_RANGE_LONG, /* eg. 192.168.15.10-192.168.18.3 */ 40 | HOST_TYPE_IPV6, /* eg. ::1 */ 41 | HOST_TYPE_CIDR6_BLOCK, /* eg. ::ffee/120 */ 42 | HOST_TYPE_RANGE6_LONG, /* eg. ::1:200:7-::1:205:500 */ 43 | HOST_TYPE_RANGE6_SHORT, /* eg. ::1-fe10 */ 44 | HOST_TYPE_MAX /* Boundary checking. */ 45 | }; 46 | 47 | /* Typedefs */ 48 | typedef struct gvm_host gvm_host_t; 49 | typedef struct gvm_vhost gvm_vhost_t; 50 | typedef struct gvm_hosts gvm_hosts_t; 51 | 52 | /* Data structures. */ 53 | 54 | /** 55 | * @brief The structure for a single host object. 56 | * 57 | * The elements of this structure should never be accessed directly. 58 | * Only the functions corresponding to this module should be used. 59 | */ 60 | struct gvm_host 61 | { 62 | union 63 | { 64 | gchar *name; /**< Hostname. */ 65 | struct in_addr addr; /**< IPv4 address */ 66 | struct in6_addr addr6; /**< IPv6 address */ 67 | }; 68 | enum host_type type; /**< HOST_TYPE_NAME, HOST_TYPE_IPV4 or HOST_TYPE_IPV6. */ 69 | GSList *vhosts; /**< List of hostnames/vhosts attached to this host. */ 70 | }; 71 | 72 | /** 73 | * @brief The structure for a single vhost object. 74 | */ 75 | struct gvm_vhost 76 | { 77 | char *value; /**< Hostname string. */ 78 | char *source; /**< Source of the value eg. DNS-Resolution. */ 79 | }; 80 | 81 | /** 82 | * @brief The structure for Hosts collection. 83 | * 84 | * The elements of this structure should never be accessed directly. 85 | * Only the functions corresponding to this module should be used. 86 | */ 87 | struct gvm_hosts 88 | { 89 | gchar *orig_str; /**< Original hosts definition string. */ 90 | gvm_host_t **hosts; /**< Hosts objects list. */ 91 | size_t max_size; /**< Current max size of hosts array entries. */ 92 | size_t current; /**< Current host index in iteration. */ 93 | size_t count; /**< Number of single host objects in hosts list. */ 94 | size_t removed; /**< Number of duplicate/excluded values. */ 95 | size_t duplicated; /**< Number of duplicated values. */ 96 | }; 97 | 98 | /* Function prototypes. */ 99 | 100 | /* gvm_hosts_t related */ 101 | 102 | gvm_hosts_t * 103 | gvm_hosts_new (const gchar *); 104 | 105 | gvm_hosts_t * 106 | gvm_hosts_new_with_max (const gchar *, unsigned int); 107 | 108 | gvm_host_t * 109 | gvm_hosts_next (gvm_hosts_t *); 110 | 111 | void 112 | gvm_hosts_move_current_host_to_end (gvm_hosts_t *); 113 | 114 | void 115 | gvm_hosts_free (gvm_hosts_t *); 116 | 117 | void 118 | gvm_hosts_shuffle (gvm_hosts_t *); 119 | 120 | void 121 | gvm_hosts_reverse (gvm_hosts_t *); 122 | 123 | void 124 | gvm_hosts_add (gvm_hosts_t *, gvm_host_t *); 125 | 126 | GSList * 127 | gvm_hosts_resolve (gvm_hosts_t *); 128 | 129 | int 130 | gvm_hosts_exclude (gvm_hosts_t *, const char *); 131 | 132 | int 133 | gvm_vhosts_exclude (gvm_host_t *, const char *); 134 | 135 | int 136 | gvm_hosts_exclude_with_max (gvm_hosts_t *, const char *, unsigned int); 137 | 138 | GSList * 139 | gvm_hosts_allowed_only (gvm_hosts_t *, const char *, const char *); 140 | 141 | char * 142 | gvm_host_reverse_lookup (gvm_host_t *); 143 | 144 | int 145 | gvm_hosts_reverse_lookup_only (gvm_hosts_t *); 146 | 147 | int 148 | gvm_hosts_reverse_lookup_unify (gvm_hosts_t *); 149 | 150 | gvm_hosts_t * 151 | gvm_hosts_reverse_lookup_only_excluded (gvm_hosts_t *); 152 | 153 | gvm_hosts_t * 154 | gvm_hosts_reverse_lookup_unify_excluded (gvm_hosts_t *); 155 | 156 | unsigned int 157 | gvm_hosts_count (const gvm_hosts_t *); 158 | 159 | unsigned int 160 | gvm_hosts_removed (const gvm_hosts_t *); 161 | 162 | unsigned int 163 | gvm_hosts_duplicated (const gvm_hosts_t *); 164 | 165 | /* gvm_host_t related */ 166 | 167 | gvm_host_t * 168 | gvm_host_from_str (const gchar *hosts_str); 169 | 170 | int 171 | gvm_host_in_hosts (const gvm_host_t *, const struct in6_addr *, 172 | const gvm_hosts_t *); 173 | 174 | gvm_host_t * 175 | gvm_host_find_in_hosts (const gvm_host_t *, const struct in6_addr *, 176 | const gvm_hosts_t *); 177 | 178 | gchar * 179 | gvm_host_type_str (const gvm_host_t *); 180 | 181 | enum host_type 182 | gvm_host_type (const gvm_host_t *); 183 | 184 | gchar * 185 | gvm_host_value_str (const gvm_host_t *); 186 | 187 | int 188 | gvm_host_resolve (const gvm_host_t *, void *, int); 189 | 190 | int 191 | gvm_host_get_addr6 (const gvm_host_t *, struct in6_addr *); 192 | 193 | void 194 | gvm_host_add_reverse_lookup (gvm_host_t *); 195 | 196 | void gvm_host_free (gpointer); 197 | 198 | gpointer gvm_duplicate_vhost (gconstpointer, gpointer); 199 | 200 | gvm_host_t * 201 | gvm_duplicate_host (gvm_host_t *); 202 | 203 | /* Miscellaneous functions */ 204 | 205 | gvm_vhost_t * 206 | gvm_vhost_new (char *, char *); 207 | 208 | int 209 | gvm_get_host_type (const gchar *); 210 | 211 | #endif /* not _GVM_HOSTS_H */ 212 | -------------------------------------------------------------------------------- /base/libgvm_base.pc.in: -------------------------------------------------------------------------------- 1 | prefix=@CMAKE_INSTALL_PREFIX@ 2 | exec_prefix=@EXEC_PREFIX@ 3 | libdir=@LIBDIR@ 4 | includedir=@INCLUDEDIR@ 5 | 6 | Name: gvmlibs-base 7 | Description: Greenbone Vulnerability Management Library base 8 | Version: @LIBGVMCONFIG_VERSION@ 9 | Requires.private: glib-2.0 >= 2.42.1 10 | Cflags: -I${includedir} -I${includedir}/gvm 11 | Libs: -L${libdir} -lgvm_base 12 | -------------------------------------------------------------------------------- /base/logging.h: -------------------------------------------------------------------------------- 1 | /* SPDX-FileCopyrightText: 2017-2023 Greenbone AG 2 | * 3 | * SPDX-License-Identifier: GPL-2.0-or-later 4 | */ 5 | 6 | /** 7 | * @file 8 | * @brief Implementation of logging methods. 9 | */ 10 | 11 | #ifndef _GVM_LOGGING_H 12 | #define _GVM_LOGGING_H 13 | 14 | #include /* for GSList, gchar, GLogLevelFlags, gpointer */ 15 | 16 | /** 17 | * @brief for backward compatibility 18 | * 19 | */ 20 | #define LOG_REFERENCES_AVAILABLE 21 | 22 | GSList * 23 | load_log_configuration (gchar *); 24 | 25 | void 26 | free_log_configuration (GSList *); 27 | 28 | gchar * 29 | get_time (gchar *); 30 | 31 | void 32 | gvm_log_silent (const char *, GLogLevelFlags, const char *, gpointer); 33 | void 34 | gvm_log_func (const char *, GLogLevelFlags, const char *, gpointer); 35 | 36 | void 37 | log_func_for_gnutls (int, const char *); 38 | 39 | int 40 | setup_log_handlers (GSList *); 41 | 42 | void 43 | gvm_log_lock (void); 44 | 45 | void 46 | gvm_log_unlock (void); 47 | 48 | void 49 | set_log_reference (char *); 50 | 51 | char * 52 | get_log_reference (void); 53 | 54 | void 55 | free_log_reference (void); 56 | 57 | void 58 | set_log_tz (const gchar *); 59 | 60 | #endif /* not _GVM_LOGGING_H */ 61 | -------------------------------------------------------------------------------- /base/logging_domain.h: -------------------------------------------------------------------------------- 1 | /* SPDX-FileCopyrightText: 2025 Greenbone AG 2 | * 3 | * SPDX-License-Identifier: GPL-2.0-or-later 4 | */ 5 | /** 6 | * @file 7 | * @brief Implementation of logging domain handling. 8 | */ 9 | 10 | #ifndef _GVM_LOGGING_DOMAIN_H 11 | #define _GVM_LOGGING_DOMAIN_H 12 | 13 | #include 14 | 15 | typedef struct gvm_logging_domain gvm_logging_domain_t; 16 | 17 | gvm_logging_domain_t * 18 | gvm_logging_domain_new (gchar *log_domain); 19 | 20 | void 21 | gvm_logging_domain_free (gvm_logging_domain_t *log_domain); 22 | 23 | gchar * 24 | gvm_logging_domain_get_log_domain (gvm_logging_domain_t *log_domain); 25 | 26 | gchar * 27 | gvm_logging_domain_get_log_file (gvm_logging_domain_t *log_domain); 28 | 29 | void 30 | gvm_logging_domain_set_log_file (gvm_logging_domain_t *log_domain, 31 | gchar *log_file); 32 | 33 | gchar * 34 | gvm_logging_domain_get_prepend_string (gvm_logging_domain_t *log_domain); 35 | 36 | void 37 | gvm_logging_domain_set_prepend_string (gvm_logging_domain_t *log_domain, 38 | gchar *prepend_string); 39 | 40 | gchar * 41 | gvm_logging_domain_get_prepend_time_format (gvm_logging_domain_t *log_domain); 42 | 43 | void 44 | gvm_logging_domain_set_prepend_time_format (gvm_logging_domain_t *log_domain, 45 | gchar *prepend_time_format); 46 | 47 | GLogLevelFlags * 48 | gvm_logging_domain_get_default_level (gvm_logging_domain_t *log_domain); 49 | 50 | void 51 | gvm_logging_domain_set_default_level (gvm_logging_domain_t *log_domain, 52 | GLogLevelFlags default_level); 53 | 54 | gchar * 55 | gvm_logging_domain_get_syslog_facility (gvm_logging_domain_t *log_domain); 56 | 57 | void 58 | gvm_logging_domain_set_syslog_facility (gvm_logging_domain_t *log_domain, 59 | gchar *syslog_facility); 60 | 61 | gchar * 62 | gvm_logging_domain_get_syslog_ident (gvm_logging_domain_t *log_domain); 63 | 64 | void 65 | gvm_logging_domain_set_syslog_ident (gvm_logging_domain_t *log_domain, 66 | gchar *syslog_ident); 67 | 68 | gchar * 69 | gvm_logging_domain_get_prepend_separator (gvm_logging_domain_t *log_domain); 70 | 71 | void 72 | gvm_logging_domain_set_prepend_separator (gvm_logging_domain_t *log_domain, 73 | gchar *prepend_separator); 74 | 75 | GIOChannel * 76 | gvm_logging_domain_get_log_channel (gvm_logging_domain_t *log_domain); 77 | 78 | void 79 | gvm_logging_domain_set_log_channel (gvm_logging_domain_t *log_domain, 80 | GIOChannel *log_channel); 81 | 82 | #endif /* _GVM_LOGGING_DOMAIN_H */ 83 | -------------------------------------------------------------------------------- /base/logging_domain_tests.c: -------------------------------------------------------------------------------- 1 | /* SPDX-FileCopyrightText: 2025 Greenbone AG 2 | * 3 | * SPDX-License-Identifier: GPL-2.0-or-later 4 | */ 5 | 6 | #include "logging_domain.c" 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | Describe (logging_domain); 14 | 15 | BeforeEach (logging_domain) 16 | { 17 | } 18 | 19 | AfterEach (logging_domain) 20 | { 21 | } 22 | 23 | Ensure (logging_domain, should_initalize_logging_domain) 24 | { 25 | gvm_logging_domain_t *log_domain_entry = 26 | gvm_logging_domain_new (g_strdup ("test")); 27 | 28 | assert_that (log_domain_entry, is_not_null); 29 | assert_that (gvm_logging_domain_get_log_domain (log_domain_entry), 30 | is_equal_to_string ("test")); 31 | assert_that (gvm_logging_domain_get_log_file (log_domain_entry), is_null); 32 | assert_that (gvm_logging_domain_get_prepend_string (log_domain_entry), 33 | is_null); 34 | assert_that (gvm_logging_domain_get_prepend_time_format (log_domain_entry), 35 | is_null); 36 | assert_that (gvm_logging_domain_get_default_level (log_domain_entry), 37 | is_null); 38 | assert_that (gvm_logging_domain_get_log_channel (log_domain_entry), is_null); 39 | assert_that (gvm_logging_domain_get_syslog_facility (log_domain_entry), 40 | is_null); 41 | assert_that (gvm_logging_domain_get_syslog_ident (log_domain_entry), is_null); 42 | assert_that (gvm_logging_domain_get_prepend_separator (log_domain_entry), 43 | is_null); 44 | 45 | gvm_logging_domain_free (log_domain_entry); 46 | } 47 | 48 | Ensure (logging_domain, should_allow_setting_properties) 49 | { 50 | gvm_logging_domain_t *log_domain_entry = 51 | gvm_logging_domain_new (g_strdup ("test")); 52 | 53 | assert_that (log_domain_entry, is_not_null); 54 | assert_that (gvm_logging_domain_get_log_domain (log_domain_entry), 55 | is_equal_to_string ("test")); 56 | 57 | gvm_logging_domain_set_log_file (log_domain_entry, g_strdup ("logfile.log")); 58 | assert_that (gvm_logging_domain_get_log_file (log_domain_entry), 59 | is_equal_to_string ("logfile.log")); 60 | 61 | gvm_logging_domain_set_prepend_string (log_domain_entry, 62 | g_strdup ("prepend")); 63 | assert_that (gvm_logging_domain_get_prepend_string (log_domain_entry), 64 | is_equal_to_string ("prepend")); 65 | 66 | gvm_logging_domain_set_prepend_time_format (log_domain_entry, 67 | g_strdup ("%Y-%m-%d")); 68 | assert_that (gvm_logging_domain_get_prepend_time_format (log_domain_entry), 69 | is_equal_to_string ("%Y-%m-%d")); 70 | 71 | gvm_logging_domain_set_default_level (log_domain_entry, G_LOG_LEVEL_DEBUG); 72 | assert_that (*gvm_logging_domain_get_default_level (log_domain_entry), 73 | is_equal_to (G_LOG_LEVEL_DEBUG)); 74 | 75 | GIOChannel *log_channel = 76 | g_io_channel_new_file ("log_channel.log", "w", NULL); 77 | gvm_logging_domain_set_log_channel (log_domain_entry, log_channel); 78 | assert_that (gvm_logging_domain_get_log_channel (log_domain_entry), 79 | is_equal_to (log_channel)); 80 | 81 | gvm_logging_domain_set_syslog_facility (log_domain_entry, g_strdup ("user")); 82 | assert_that (gvm_logging_domain_get_syslog_facility (log_domain_entry), 83 | is_equal_to_string ("user")); 84 | 85 | gvm_logging_domain_set_syslog_ident (log_domain_entry, g_strdup ("ident")); 86 | assert_that (gvm_logging_domain_get_syslog_ident (log_domain_entry), 87 | is_equal_to_string ("ident")); 88 | 89 | gvm_logging_domain_set_prepend_separator (log_domain_entry, g_strdup ("|")); 90 | assert_that (gvm_logging_domain_get_prepend_separator (log_domain_entry), 91 | is_equal_to_string ("|")); 92 | 93 | gvm_logging_domain_free (log_domain_entry); 94 | g_remove ("log_channel.log"); 95 | } 96 | 97 | static TestSuite * 98 | logging_test_suite () 99 | { 100 | TestSuite *suite = create_test_suite (); 101 | add_test_with_context (suite, logging_domain, 102 | should_initalize_logging_domain); 103 | add_test_with_context (suite, logging_domain, 104 | should_allow_setting_properties); 105 | return suite; 106 | } 107 | 108 | int 109 | main (int argc, char **argv) 110 | { 111 | TestSuite *suite; 112 | 113 | suite = create_test_suite (); 114 | add_suite (suite, logging_test_suite ()); 115 | 116 | if (argc > 1) 117 | return run_single_test (suite, argv[1], create_text_reporter ()); 118 | 119 | return run_test_suite (suite, create_text_reporter ()); 120 | } 121 | -------------------------------------------------------------------------------- /base/networking.h: -------------------------------------------------------------------------------- 1 | /* SPDX-FileCopyrightText: 2013-2023 Greenbone AG 2 | * 3 | * SPDX-License-Identifier: GPL-2.0-or-later 4 | */ 5 | 6 | /** 7 | * @file 8 | * @brief GVM Networking related API. 9 | */ 10 | 11 | #ifndef _GVM_NETWORKING_H 12 | #define _GVM_NETWORKING_H 13 | 14 | #include "array.h" /* for array_t */ 15 | 16 | #include /* for struct in6_addr */ 17 | 18 | /** 19 | * @brief Possible port types. 20 | * 21 | * Used in Manager database. If any symbol changes then a migrator must be 22 | * added to update existing data. 23 | */ 24 | typedef enum 25 | { 26 | PORT_PROTOCOL_TCP = 0, 27 | PORT_PROTOCOL_UDP = 1, 28 | PORT_PROTOCOL_OTHER = 2 29 | } port_protocol_t; 30 | 31 | /** 32 | * @brief A port range. 33 | */ 34 | struct range 35 | { 36 | gchar *comment; /**< Comment. */ 37 | gchar *id; /**< UUID. */ 38 | int end; /**< End port. For single port end == start. */ 39 | int exclude; /**< Whether to exclude range. */ 40 | int start; /**< Start port. */ 41 | port_protocol_t type; /**< Port protocol. */ 42 | }; 43 | typedef struct range range_t; 44 | 45 | int 46 | gvm_source_iface_init (const char *); 47 | 48 | int 49 | gvm_source_iface_is_set (void); 50 | 51 | int 52 | gvm_source_set_socket (int, int, int); 53 | 54 | void 55 | gvm_source_addr (void *); 56 | 57 | void 58 | gvm_source_addr6 (void *); 59 | 60 | void 61 | gvm_source_addr_as_addr6 (struct in6_addr *); 62 | 63 | char * 64 | gvm_source_addr_str (void); 65 | 66 | char * 67 | gvm_source_addr6_str (void); 68 | 69 | void 70 | ipv4_as_ipv6 (const struct in_addr *, struct in6_addr *); 71 | 72 | void 73 | addr6_to_str (const struct in6_addr *, char *); 74 | 75 | char * 76 | addr6_as_str (const struct in6_addr *); 77 | 78 | void 79 | sockaddr_as_str (const struct sockaddr_storage *, char *); 80 | 81 | int 82 | gvm_resolve (const char *, void *, int); 83 | 84 | GSList * 85 | gvm_resolve_list (const char *); 86 | 87 | int 88 | gvm_resolve_as_addr6 (const char *, struct in6_addr *); 89 | 90 | int 91 | validate_port_range (const char *); 92 | 93 | array_t * 94 | port_range_ranges (const char *); 95 | 96 | int 97 | port_in_port_ranges (int, port_protocol_t, array_t *); 98 | 99 | int 100 | ipv6_is_enabled (void); 101 | 102 | gchar * 103 | gvm_routethrough (struct sockaddr_storage *, struct sockaddr_storage *); 104 | 105 | char * 106 | gvm_get_outgoing_iface (struct sockaddr_storage *); 107 | 108 | #endif /* not _GVM_NETWORKING_H */ 109 | -------------------------------------------------------------------------------- /base/pidfile.c: -------------------------------------------------------------------------------- 1 | /* SPDX-FileCopyrightText: 2009-2023 Greenbone AG 2 | * 3 | * SPDX-License-Identifier: GPL-2.0-or-later 4 | */ 5 | 6 | /** 7 | * @file 8 | * @brief PID-file management. 9 | */ 10 | 11 | #include "pidfile.h" 12 | 13 | #include /* for errno */ 14 | #include /* for g_free, gchar */ 15 | #include /* for g_unlink, g_fopen */ 16 | #include /* for libgen */ 17 | #include /* for fclose, FILE */ 18 | #include /* for atoi */ 19 | #include /* for strerror */ 20 | #include /* for getpid */ 21 | 22 | #undef G_LOG_DOMAIN 23 | /** 24 | * @brief GLib log domain. 25 | */ 26 | #define G_LOG_DOMAIN "libgvm base" 27 | 28 | /** 29 | * @brief Create a PID-file. 30 | * 31 | * A standard PID file will be created for the 32 | * given path. 33 | * 34 | * @param[in] pid_file_path The full path of the pid file. E.g. 35 | * "/tmp/service1.pid" 36 | * 37 | * @return 0 for success, anything else indicates an error. 38 | */ 39 | int 40 | pidfile_create (gchar *pid_file_path) 41 | { 42 | FILE *pidfile; 43 | gchar *copy, *dir; 44 | 45 | if (pid_file_path == NULL) 46 | return -1; 47 | 48 | copy = g_strdup (pid_file_path); 49 | dir = dirname (copy); 50 | 51 | /* Ensure directory exists. */ 52 | 53 | if (g_mkdir_with_parents (dir, 0755)) /* "rwxr-xr-x" */ 54 | { 55 | g_free (copy); 56 | g_warning ("Failed to create PID file directory %s: %s", dir, 57 | strerror (errno)); 58 | return 1; 59 | } 60 | g_free (copy); 61 | 62 | /* Create file. */ 63 | 64 | pidfile = g_fopen (pid_file_path, "w"); 65 | 66 | if (pidfile == NULL) 67 | { 68 | g_critical ("%s: failed to open pidfile %s: %s\n", __func__, 69 | pid_file_path, strerror (errno)); 70 | return 1; 71 | } 72 | else 73 | { 74 | g_fprintf (pidfile, "%d\n", getpid ()); 75 | fclose (pidfile); 76 | } 77 | return 0; 78 | } 79 | 80 | /** 81 | * @brief Remove PID file. 82 | * 83 | * @param[in] pid_file_path The full path of the pid file. E.g. 84 | * "/tmp/service1.pid" 85 | */ 86 | void 87 | pidfile_remove (gchar *pid_file_path) 88 | { 89 | gchar *pidfile_contents; 90 | 91 | if (g_file_get_contents (pid_file_path, &pidfile_contents, NULL, NULL)) 92 | { 93 | int pid = atoi (pidfile_contents); 94 | 95 | if (pid == getpid ()) 96 | { 97 | g_unlink (pid_file_path); 98 | } 99 | g_free (pidfile_contents); 100 | } 101 | } 102 | -------------------------------------------------------------------------------- /base/pidfile.h: -------------------------------------------------------------------------------- 1 | /* SPDX-FileCopyrightText: 2009-2023 Greenbone AG 2 | * 3 | * SPDX-License-Identifier: GPL-2.0-or-later 4 | */ 5 | 6 | /** 7 | * @file 8 | * @brief PID-file management. 9 | */ 10 | 11 | #ifndef _GVM_PIDFILE_H 12 | #define _GVM_PIDFILE_H 13 | 14 | #include 15 | 16 | int 17 | pidfile_create (gchar *); 18 | void 19 | pidfile_remove (gchar *); 20 | 21 | #endif /* not _GVM_PIDFILE_H */ 22 | -------------------------------------------------------------------------------- /base/prefs.c: -------------------------------------------------------------------------------- 1 | /* SPDX-FileCopyrightText: 2014-2023 Greenbone AG 2 | * 3 | * SPDX-License-Identifier: GPL-2.0-or-later 4 | */ 5 | 6 | /** 7 | * @file 8 | * @brief Implementation of API to handle globally stored preferences. 9 | * 10 | * A global store of preferences to scanner and NVTs is handled by this 11 | * module. 12 | */ 13 | 14 | #include "prefs.h" 15 | 16 | #include "settings.h" /* for init_settings_iterator_from_file */ 17 | 18 | #include /* for gchar */ 19 | #include /* for printf() */ 20 | #include /* for atoi() */ 21 | #include /* for strlen() */ 22 | 23 | #undef G_LOG_DOMAIN 24 | /** 25 | * @brief GLib log domain. 26 | */ 27 | #define G_LOG_DOMAIN "libgvm base" 28 | 29 | static GHashTable *global_prefs = NULL; 30 | 31 | void 32 | prefs_set (const gchar *, const gchar *); 33 | 34 | /** 35 | * @brief Initializes the preferences structure. If it was 36 | * already initialized, remove old settings and start 37 | * from scratch. 38 | */ 39 | static void 40 | prefs_init (void) 41 | { 42 | if (global_prefs) 43 | g_hash_table_destroy (global_prefs); 44 | 45 | global_prefs = 46 | g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free); 47 | prefs_set ("cgi_path", "/cgi-bin:/scripts"); 48 | prefs_set ("checks_read_timeout", "5"); 49 | prefs_set ("unscanned_closed", "yes"); 50 | prefs_set ("unscanned_closed_udp", "yes"); 51 | prefs_set ("timeout_retry", "3"); 52 | prefs_set ("expand_vhosts", "yes"); 53 | prefs_set ("test_empty_vhost", "no"); 54 | prefs_set ("open_sock_max_attempts", "5"); 55 | prefs_set ("time_between_request", "0"); 56 | prefs_set ("nasl_no_signature_check", "yes"); 57 | prefs_set ("max_hosts", "30"); 58 | prefs_set ("max_checks", "10"); 59 | prefs_set ("log_whole_attack", "no"); 60 | prefs_set ("log_plugins_name_at_load", "no"); 61 | prefs_set ("optimize_test", "yes"); 62 | prefs_set ("non_simult_ports", "139, 445, 3389, Services/irc"); 63 | prefs_set ("safe_checks", "yes"); 64 | prefs_set ("auto_enable_dependencies", "yes"); 65 | prefs_set ("drop_privileges", "no"); 66 | prefs_set ("report_host_details", "yes"); 67 | prefs_set ("vendor_version", "\0"); 68 | prefs_set ("test_alive_hosts_only", "yes"); 69 | prefs_set ("test_alive_wait_timeout", "3"); 70 | prefs_set ("debug_tls", "0"); 71 | prefs_set ("allow_simultaneous_ips", "yes"); 72 | } 73 | 74 | /** 75 | * @brief Get the pointer to the global preferences structure. 76 | * Eventually this function should not be used anywhere. 77 | * 78 | * @return Pointer to the global preferences structure. 79 | */ 80 | GHashTable * 81 | preferences_get (void) 82 | { 83 | if (!global_prefs) 84 | prefs_init (); 85 | 86 | return global_prefs; 87 | } 88 | 89 | /** 90 | * @brief Get a string preference value via a key. 91 | * 92 | * @param key The identifier for the preference. 93 | * 94 | * @return A pointer to a string with the value for the preference. 95 | * NULL in case for the key no preference was found or the 96 | * preference is not of type string. 97 | */ 98 | const gchar * 99 | prefs_get (const gchar *key) 100 | { 101 | if (!global_prefs) 102 | prefs_init (); 103 | 104 | return g_hash_table_lookup (global_prefs, key); 105 | } 106 | 107 | /** 108 | * @brief Get a boolean expression of a preference value via a key. 109 | * 110 | * @param key The identifier for the preference. 111 | * 112 | * @return 1 if the value is considered to represent "true" and 113 | * 0 if the value is considered to represent "false". 114 | * If the preference is of type string, value "yes" is true, 115 | * anything else is false. 116 | * Any other type or non-existing key is false. 117 | */ 118 | int 119 | prefs_get_bool (const gchar *key) 120 | { 121 | gchar *str; 122 | 123 | if (!global_prefs) 124 | prefs_init (); 125 | 126 | str = g_hash_table_lookup (global_prefs, key); 127 | if (str && !strcmp (str, "yes")) 128 | return 1; 129 | 130 | return 0; 131 | } 132 | 133 | /** 134 | * @brief Set a string preference value via a key. 135 | * 136 | * @param key The identifier for the preference. A copy of this will 137 | * be created if necessary. 138 | * 139 | * @param value The value to set. A copy of this will be created. 140 | */ 141 | void 142 | prefs_set (const gchar *key, const gchar *value) 143 | { 144 | if (!global_prefs) 145 | prefs_init (); 146 | 147 | g_hash_table_insert (global_prefs, g_strdup (key), g_strdup (value)); 148 | } 149 | 150 | /** 151 | * @brief Apply the configs from given file as preferences. 152 | * 153 | * @param config Filename of the configuration file. 154 | */ 155 | void 156 | prefs_config (const char *config) 157 | { 158 | settings_iterator_t settings; 159 | 160 | if (!global_prefs) 161 | prefs_init (); 162 | 163 | if (!init_settings_iterator_from_file (&settings, config, "Misc")) 164 | { 165 | while (settings_iterator_next (&settings)) 166 | prefs_set (settings_iterator_name (&settings), 167 | settings_iterator_value (&settings)); 168 | 169 | cleanup_settings_iterator (&settings); 170 | } 171 | 172 | prefs_set ("config_file", config); 173 | } 174 | 175 | /** 176 | * @brief Dump the preferences to stdout 177 | */ 178 | void 179 | prefs_dump (void) 180 | { 181 | void *name, *value; 182 | GHashTableIter iter; 183 | 184 | if (global_prefs) 185 | { 186 | g_hash_table_iter_init (&iter, global_prefs); 187 | while (g_hash_table_iter_next (&iter, &name, &value)) 188 | { 189 | printf ("%s = %s\n", (char *) name, (char *) value); 190 | } 191 | } 192 | } 193 | -------------------------------------------------------------------------------- /base/prefs.h: -------------------------------------------------------------------------------- 1 | /* SPDX-FileCopyrightText: 2014-2023 Greenbone AG 2 | * 3 | * SPDX-License-Identifier: GPL-2.0-or-later 4 | */ 5 | 6 | /** 7 | * @file 8 | * @brief Protos and data structures for NVT Information data sets. 9 | * 10 | * This file contains the protos for \ref prefs.c 11 | */ 12 | 13 | #ifndef _GVM_PREFS_H 14 | #define _GVM_PREFS_H 15 | 16 | #include /* for gchar */ 17 | 18 | void 19 | prefs_config (const char *); 20 | const gchar * 21 | prefs_get (const gchar *key); 22 | int 23 | prefs_get_bool (const gchar *key); 24 | void 25 | prefs_set (const gchar *, const gchar *); 26 | void 27 | prefs_dump (void); 28 | int 29 | prefs_nvt_timeout (const char *); 30 | 31 | GHashTable * 32 | preferences_get (void); 33 | 34 | #endif /* not _GVM_PREFS_H */ 35 | -------------------------------------------------------------------------------- /base/proctitle.c: -------------------------------------------------------------------------------- 1 | /* SPDX-FileCopyrightText: 2014-2023 Greenbone AG 2 | * 3 | * SPDX-License-Identifier: GPL-2.0-or-later 4 | */ 5 | 6 | /** 7 | * @file 8 | * @brief Implementation of an API to set process title. 9 | */ 10 | 11 | #include "proctitle.h" 12 | 13 | #include /* for g_free, g_malloc0, g_strdup */ 14 | #include 15 | #include /* for strlen, strdup, bzero, strncpy */ 16 | #include 17 | #include 18 | 19 | #undef G_LOG_DOMAIN 20 | /** 21 | * @brief GLib log domain. 22 | */ 23 | #define G_LOG_DOMAIN "libgvm base" 24 | 25 | /** 26 | * @brief Access to the executable's name. 27 | */ 28 | extern const char *__progname; 29 | #ifndef __FreeBSD__ 30 | extern const char *__progname_full; 31 | #endif 32 | static char **old_argv; 33 | static int old_argc; 34 | extern char **environ; 35 | void *current_environ = NULL; 36 | static int max_prog_name = 0; 37 | 38 | /** 39 | * @brief Initializes the process setting variables. 40 | * 41 | * @param[in] argc Argc argument from main. 42 | * @param[in] argv Argv argument from main. 43 | */ 44 | void 45 | proctitle_init (int argc, char **argv) 46 | { 47 | int i; 48 | char **envp = environ; 49 | #ifndef __FreeBSD__ 50 | char *new_progname, *new_progname_full; 51 | #else 52 | char *new_progname; 53 | #endif 54 | old_argc = argc; 55 | 56 | if (argv == NULL) 57 | return; 58 | // according to c99 argv is defined as when argc is set it follows program 59 | // parameter. Since we will override on set_proctitle we know that this 60 | // memory is modifiable. 61 | // Everything after that is unsafe and can lead to segmentation faults. 62 | // Therefore we iterate through argv and append strlen to gather the maximum 63 | // safe program name. 64 | for (i = 0; i < argc; i++) 65 | { 66 | max_prog_name += strlen (argv[i]) + 1; 67 | } 68 | i = 0; 69 | 70 | new_progname = strdup (__progname); 71 | #ifndef __FreeBSD__ 72 | new_progname_full = strdup (__progname_full); 73 | #endif 74 | 75 | /* Move environ to new memory, to be able to reuse older one. */ 76 | while (envp[i]) 77 | i++; 78 | environ = g_malloc0 (sizeof (char *) * (i + 1)); 79 | if (current_environ) 80 | g_free (current_environ); 81 | current_environ = environ; 82 | for (i = 0; envp[i]; i++) 83 | environ[i] = g_strdup (envp[i]); 84 | environ[i] = NULL; 85 | 86 | old_argv = argv; 87 | /* Seems like these are in the moved environment, so reset them. Idea from 88 | * proctitle.cpp in KDE libs. */ 89 | __progname = new_progname; 90 | #ifndef __FreeBSD__ 91 | __progname_full = new_progname_full; 92 | #endif 93 | } 94 | 95 | /** 96 | * @brief Sets the process' title. 97 | * 98 | * @param[in] new_title Format string for new process title. 99 | * @param[in] args Format string arguments variable list. 100 | */ 101 | static void 102 | proctitle_set_args (const char *new_title, va_list args) 103 | { 104 | char *formatted; 105 | int tmp; 106 | 107 | if (old_argv == NULL) 108 | /* Called setproctitle before initproctitle ? */ 109 | return; 110 | if (max_prog_name == 0) 111 | // there may no program name set 112 | return; 113 | // omit previous additional parameter 114 | 115 | formatted = g_strdup_vprintf (new_title, args); 116 | 117 | tmp = strlen (formatted); 118 | if (tmp >= max_prog_name) 119 | { 120 | formatted[max_prog_name] = '\0'; 121 | tmp = max_prog_name; 122 | } 123 | 124 | // set display name 125 | memset (old_argv[0], 0, max_prog_name); 126 | memcpy (old_argv[0], formatted, tmp); 127 | g_free (formatted); 128 | if (old_argc > 1) 129 | old_argv[1] = NULL; 130 | } 131 | 132 | /** 133 | * @brief Sets the process' title. 134 | * 135 | * @param[in] new_title Format string for new process title. 136 | * @param[in] ... Arguments for format string. 137 | */ 138 | void 139 | proctitle_set (const char *new_title, ...) 140 | { 141 | va_list args; 142 | 143 | va_start (args, new_title); 144 | proctitle_set_args (new_title, args); 145 | va_end (args); 146 | } 147 | -------------------------------------------------------------------------------- /base/proctitle.h: -------------------------------------------------------------------------------- 1 | /* SPDX-FileCopyrightText: 2014-2023 Greenbone AG 2 | * 3 | * SPDX-License-Identifier: GPL-2.0-or-later 4 | */ 5 | 6 | /** 7 | * @file 8 | * @brief API for process title setting. 9 | */ 10 | 11 | #ifndef _GVM_PROCTITLE_H 12 | #define _GVM_PROCTITLE_H 13 | 14 | void 15 | proctitle_init (int, char **); 16 | 17 | void 18 | proctitle_set (const char *, ...); 19 | 20 | #endif /* not _GVM_PROCTITLE_H */ 21 | -------------------------------------------------------------------------------- /base/pwpolicy.h: -------------------------------------------------------------------------------- 1 | /* SPDX-FileCopyrightText: 2013-2023 Greenbone AG 2 | * 3 | * SPDX-License-Identifier: GPL-2.0-or-later 4 | */ 5 | 6 | /** 7 | * @file 8 | * @brief Protos and data structures for pwpolicy checking. 9 | * 10 | * This file contains the protos for \ref pwpolicy.c 11 | */ 12 | 13 | #ifndef _GVM_PWPOLICY_H 14 | #define _GVM_PWPOLICY_H 15 | 16 | char * 17 | gvm_validate_password (const char *, const char *); 18 | void 19 | gvm_disable_password_policy (void); 20 | 21 | #endif /*_GVM_PWPOLICY_H*/ 22 | -------------------------------------------------------------------------------- /base/pwpolicy_tests.c: -------------------------------------------------------------------------------- 1 | /* SPDX-FileCopyrightText: 2019-2023 Greenbone AG 2 | * 3 | * SPDX-License-Identifier: GPL-2.0-or-later 4 | */ 5 | 6 | #include "pwpolicy.c" 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | Describe (pwpolicy); 15 | BeforeEach (pwpolicy) 16 | { 17 | } 18 | 19 | AfterEach (pwpolicy) 20 | { 21 | } 22 | 23 | /* parse_pattern_line */ 24 | 25 | Ensure (pwpolicy, parse_pattern_line_allows) 26 | { 27 | char *desc, *error, *line; 28 | 29 | desc = NULL; 30 | line = g_strdup ("password"); 31 | error = parse_pattern_line (line, "test", 111, &desc, "passw0rd", "name"); 32 | assert_that (error, is_null); 33 | g_free (desc); 34 | g_free (line); 35 | } 36 | 37 | Ensure (pwpolicy, parse_pattern_line_refuses) 38 | { 39 | char *desc, *error, *line; 40 | 41 | desc = NULL; 42 | line = g_strdup ("password"); 43 | error = parse_pattern_line (line, "test", 111, &desc, "password", "name"); 44 | assert_that (error, is_not_null); 45 | g_free (desc); 46 | g_free (error); 47 | g_free (line); 48 | } 49 | 50 | Ensure (pwpolicy, parse_pattern_line_comment) 51 | { 52 | char *desc, *error, *line; 53 | 54 | desc = NULL; 55 | line = g_strdup ("# password"); 56 | error = parse_pattern_line (line, "test", 111, &desc, "password", "name"); 57 | assert_that (error, is_null); 58 | g_free (desc); 59 | g_free (error); 60 | g_free (line); 61 | } 62 | 63 | /* Test suite. */ 64 | int 65 | main (int argc, char **argv) 66 | { 67 | TestSuite *suite; 68 | 69 | suite = create_test_suite (); 70 | 71 | add_test_with_context (suite, pwpolicy, parse_pattern_line_allows); 72 | add_test_with_context (suite, pwpolicy, parse_pattern_line_refuses); 73 | add_test_with_context (suite, pwpolicy, parse_pattern_line_comment); 74 | 75 | if (argc > 1) 76 | return run_single_test (suite, argv[1], create_text_reporter ()); 77 | 78 | return run_test_suite (suite, create_text_reporter ()); 79 | } 80 | -------------------------------------------------------------------------------- /base/settings.c: -------------------------------------------------------------------------------- 1 | /* SPDX-FileCopyrightText: 2010-2023 Greenbone AG 2 | * 3 | * SPDX-License-Identifier: GPL-2.0-or-later 4 | */ 5 | 6 | /** 7 | * @file settings.c 8 | * @brief Implementation of API to handle configuration file management 9 | */ 10 | 11 | #include "settings.h" 12 | 13 | #include 14 | #include /* for strlen */ 15 | 16 | #undef G_LOG_DOMAIN 17 | /** 18 | * @brief GLib log domain. 19 | */ 20 | #define G_LOG_DOMAIN "libgvm base" 21 | 22 | /** 23 | * @brief Initialise a settings struct from a file. 24 | * 25 | * @param[in] settings Settings. 26 | * @param[in] filename Complete name of the configuration file. 27 | * @param[in] group Name of the group in the file. 28 | * 29 | * @return 0 success, -1 error. 30 | */ 31 | static int 32 | settings_init_from_file (settings_t *settings, const gchar *filename, 33 | const gchar *group) 34 | { 35 | GError *error = NULL; 36 | 37 | if (filename == NULL || group == NULL) 38 | return -1; 39 | 40 | gchar *contents = NULL; 41 | 42 | if (!g_file_get_contents (filename, &contents, NULL, &error)) 43 | { 44 | g_error_free (error); 45 | return -1; 46 | } 47 | 48 | if (contents != NULL) 49 | { 50 | gchar *contents_with_group = g_strjoin ("\n", "[Misc]", contents, NULL); 51 | settings->key_file = g_key_file_new (); 52 | 53 | if (!g_key_file_load_from_data ( 54 | settings->key_file, contents_with_group, 55 | strlen (contents_with_group), 56 | G_KEY_FILE_KEEP_COMMENTS | G_KEY_FILE_KEEP_TRANSLATIONS, &error)) 57 | { 58 | g_warning ("Failed to load configuration from %s: %s", filename, 59 | error->message); 60 | g_error_free (error); 61 | g_free (contents_with_group); 62 | g_free (contents); 63 | return -1; 64 | } 65 | g_free (contents_with_group); 66 | g_free (contents); 67 | } 68 | 69 | settings->group_name = g_strdup (group); 70 | settings->file_name = g_strdup (filename); 71 | 72 | return 0; 73 | } 74 | 75 | /** 76 | * @brief Cleanup a settings structure. 77 | * 78 | * @param[in] settings Settings structure. 79 | */ 80 | void 81 | settings_cleanup (settings_t *settings) 82 | { 83 | g_free (settings->group_name); 84 | g_free (settings->file_name); 85 | g_key_file_free (settings->key_file); 86 | } 87 | 88 | /** 89 | * @brief Initialise a settings iterator from a file. 90 | * 91 | * @param[in] iterator Settings iterator. 92 | * @param[in] filename Complete name of the configuration file. 93 | * @param[in] group Name of the group in the file. 94 | * 95 | * @return 0 success, -1 error. 96 | */ 97 | int 98 | init_settings_iterator_from_file (settings_iterator_t *iterator, 99 | const gchar *filename, const gchar *group) 100 | { 101 | int ret; 102 | gsize keys_length; 103 | GError *error = NULL; 104 | 105 | ret = settings_init_from_file (&iterator->settings, filename, group); 106 | if (ret) 107 | return ret; 108 | 109 | iterator->keys = g_key_file_get_keys (iterator->settings.key_file, group, 110 | &keys_length, &error); 111 | 112 | if (iterator->keys == NULL) 113 | { 114 | if (error) 115 | { 116 | g_warning ("Failed to retrieve keys of group %s from %s: %s", group, 117 | filename, error->message); 118 | g_error_free (error); 119 | } 120 | g_key_file_free (iterator->settings.key_file); 121 | return -1; 122 | } 123 | 124 | iterator->current_key = iterator->keys - 1; 125 | iterator->last_key = iterator->keys + keys_length - 1; 126 | 127 | return 0; 128 | } 129 | 130 | /** 131 | * @brief Cleanup a settings iterator. 132 | * 133 | * @param[in] iterator Settings iterator. 134 | */ 135 | void 136 | cleanup_settings_iterator (settings_iterator_t *iterator) 137 | { 138 | g_strfreev (iterator->keys); 139 | settings_cleanup (&iterator->settings); 140 | } 141 | 142 | /** 143 | * @brief Increment an iterator. 144 | * 145 | * @param[in] iterator Settings iterator. 146 | * 147 | * @return TRUE if there was a next item, else FALSE. 148 | */ 149 | gboolean 150 | settings_iterator_next (settings_iterator_t *iterator) 151 | { 152 | if (iterator->current_key == iterator->last_key) 153 | return FALSE; 154 | iterator->current_key++; 155 | return TRUE; 156 | } 157 | 158 | /** 159 | * @brief Get the name from a settings iterator. 160 | * 161 | * @param[in] iterator Settings iterator. 162 | * 163 | * @return Name of current key. 164 | */ 165 | const gchar * 166 | settings_iterator_name (settings_iterator_t *iterator) 167 | { 168 | return *iterator->current_key; 169 | } 170 | 171 | /** 172 | * @brief Get the value from a settings iterator. 173 | * 174 | * @param[in] iterator Settings iterator. 175 | * 176 | * @return Value of current key. 177 | */ 178 | const gchar * 179 | settings_iterator_value (settings_iterator_t *iterator) 180 | { 181 | return g_key_file_get_value (iterator->settings.key_file, 182 | iterator->settings.group_name, 183 | *iterator->current_key, NULL); 184 | } 185 | -------------------------------------------------------------------------------- /base/settings.h: -------------------------------------------------------------------------------- 1 | /* SPDX-FileCopyrightText: 2010-2023 Greenbone AG 2 | * 3 | * SPDX-License-Identifier: GPL-2.0-or-later 4 | */ 5 | 6 | /** 7 | * @file 8 | * @brief Protos and data structures for configuration file management 9 | * 10 | * This file contains the protos for \ref settings.c 11 | */ 12 | 13 | #ifndef _GVM_SETTINGS_H 14 | #define _GVM_SETTINGS_H 15 | 16 | #include 17 | 18 | /** 19 | * @brief Struct holding options for settings taken from a key-value 20 | * config file. 21 | */ 22 | typedef struct 23 | { 24 | gchar *file_name; /**< Filename containing key-value pairs. */ 25 | gchar *group_name; /**< Name of the group containing key-value pairs. */ 26 | GKeyFile *key_file; /**< GKeyFile object where the file is load. */ 27 | } settings_t; 28 | 29 | void 30 | settings_cleanup (settings_t *); 31 | 32 | /** 33 | * @brief Struct holding options to iterate over a GKeyFile. 34 | */ 35 | typedef struct 36 | { 37 | gchar **keys; /**< Keys. */ 38 | settings_t settings; /**< Settings structure. */ 39 | gchar **current_key; /**< Pointer to the current key. */ 40 | gchar **last_key; /**< Pointer to the last keys. */ 41 | } settings_iterator_t; 42 | 43 | int 44 | init_settings_iterator_from_file (settings_iterator_t *, const gchar *, 45 | const gchar *); 46 | void 47 | cleanup_settings_iterator (settings_iterator_t *); 48 | int 49 | settings_iterator_next (settings_iterator_t *); 50 | const gchar * 51 | settings_iterator_name (settings_iterator_t *); 52 | const gchar * 53 | settings_iterator_value (settings_iterator_t *); 54 | 55 | #endif /* not _GVM_SETTINGS_H */ 56 | -------------------------------------------------------------------------------- /base/strings.c: -------------------------------------------------------------------------------- 1 | /* SPDX-FileCopyrightText: 2009-2023 Greenbone AG 2 | * 3 | * SPDX-License-Identifier: GPL-2.0-or-later 4 | */ 5 | 6 | /** 7 | * @file 8 | * @brief String utilities. 9 | */ 10 | 11 | #include "strings.h" 12 | 13 | #include /* for assert */ 14 | #include /* for g_free, g_strconcat, gchar, g_strdup, g_strndup */ 15 | 16 | #undef G_LOG_DOMAIN 17 | /** 18 | * @brief GLib log domain. 19 | */ 20 | #define G_LOG_DOMAIN "libgvm base" 21 | 22 | /** 23 | * @brief Append a string to a string variable. 24 | * 25 | * When the variable is NULL store a copy of the given string in the variable. 26 | * 27 | * When the variable already contains a string replace the string with a new 28 | * string that is the concatenation of the two, freeing the old string. It is 29 | * up to the caller to free the given string if it was dynamically allocated. 30 | * 31 | * @param[in] var The address of a string variable, that is, a pointer to 32 | * a string. 33 | * @param[in] string The string to append to the string in the variable. 34 | */ 35 | void 36 | gvm_append_string (gchar **var, const gchar *string) 37 | { 38 | if (*var) 39 | { 40 | char *old = *var; 41 | *var = g_strconcat (old, string, NULL); 42 | g_free (old); 43 | } 44 | else 45 | *var = g_strdup (string); 46 | } 47 | 48 | /** 49 | * @brief Append a string of a known length to a string variable. 50 | * 51 | * When the variable is NULL store a copy of the given string in the variable. 52 | * 53 | * When the variable already contains a string replace the string with a new 54 | * string that is the concatenation of the two, freeing the old string. It is 55 | * up to the caller to free the given string if it was dynamically allocated. 56 | * 57 | * The string must be NULL terminated, and the given length must be the 58 | * actual length of the string. 59 | * 60 | * @param[in] var The address of a string variable, that is, a pointer to 61 | * a string. 62 | * @param[in] string The string to append to the string in the variable. 63 | * @param[in] length The length of string. 64 | */ 65 | void 66 | gvm_append_text (gchar **var, const gchar *string, gsize length) 67 | { 68 | if (*var) 69 | { 70 | char *old = *var; 71 | *var = g_strconcat (old, string, NULL); 72 | g_free (old); 73 | } 74 | else 75 | *var = g_strndup (string, length); 76 | } 77 | 78 | /** 79 | * @brief Free a string variable. 80 | * 81 | * Free the string in the variable and set the variable to NULL. 82 | * 83 | * @param[in] var The address of a string variable, that is, a pointer to 84 | * a string. 85 | */ 86 | void 87 | gvm_free_string_var (gchar **var) 88 | { 89 | g_free (*var); 90 | *var = NULL; 91 | } 92 | 93 | /** 94 | * @brief "Strip" space and newline characters from either end of some memory. 95 | * 96 | * Return the given pointer moved forward past any spaces, replacing the 97 | * first of any contiguous spaces at or before the end of the memory with 98 | * a terminating NULL. 99 | * 100 | * This is for use when string points into a static buffers. 101 | * 102 | * @param[in,out] string The start of the memory. 103 | * @param[in] end Pointer to the byte after the end of the memory. 104 | * 105 | * @return A new pointer into the string. 106 | */ 107 | char * 108 | gvm_strip_space (char *string, char *end) 109 | { 110 | assert (string <= end); 111 | if (string >= end) 112 | return string; 113 | end--; 114 | while (string[0] == ' ' || string[0] == '\n') 115 | { 116 | string++; 117 | if (string >= end) 118 | { 119 | end[0] = '\0'; 120 | return end; 121 | } 122 | } 123 | 124 | /* Here string is < end. */ 125 | if (end[0] == ' ' || end[0] == '\n') 126 | { 127 | end--; 128 | while (end >= string && (end[0] == ' ' || end[0] == '\n')) 129 | { 130 | end--; 131 | } 132 | end[1] = '\0'; 133 | } 134 | return string; 135 | } 136 | -------------------------------------------------------------------------------- /base/strings.h: -------------------------------------------------------------------------------- 1 | /* SPDX-FileCopyrightText: 2009-2023 Greenbone AG 2 | * 3 | * SPDX-License-Identifier: GPL-2.0-or-later 4 | */ 5 | 6 | /** 7 | * @file 8 | * @brief String utilities. 9 | */ 10 | 11 | #ifndef _GVM_STRINGS_H 12 | #define _GVM_STRINGS_H 13 | 14 | #include 15 | 16 | void 17 | gvm_append_string (gchar **, const gchar *); 18 | void 19 | gvm_append_text (gchar **, const gchar *, gsize); 20 | void 21 | gvm_free_string_var (gchar **); 22 | 23 | char * 24 | gvm_strip_space (char *, char *); 25 | 26 | #endif /* not _GVM_STRINGS_H */ 27 | -------------------------------------------------------------------------------- /base/version.c: -------------------------------------------------------------------------------- 1 | /* SPDX-FileCopyrightText: 2019-2023 Greenbone AG 2 | * 3 | * SPDX-License-Identifier: GPL-2.0-or-later 4 | */ 5 | 6 | #include "version.h" 7 | 8 | #undef G_LOG_DOMAIN 9 | /** 10 | * @brief GLib log domain. 11 | */ 12 | #define G_LOG_DOMAIN "libgvm base" 13 | 14 | const char * 15 | gvm_libs_version (void) 16 | { 17 | return GVM_LIBS_VERSION; 18 | } 19 | -------------------------------------------------------------------------------- /base/version.h: -------------------------------------------------------------------------------- 1 | /* SPDX-FileCopyrightText: 2019-2023 Greenbone AG 2 | * 3 | * SPDX-License-Identifier: GPL-2.0-or-later 4 | */ 5 | 6 | /** 7 | * @file 8 | * @brief Version utilities. 9 | */ 10 | 11 | #ifndef _GVM_VERSION_H 12 | #define _GVM_VERSION_H 13 | 14 | const char * 15 | gvm_libs_version (void); 16 | 17 | #endif 18 | -------------------------------------------------------------------------------- /base/version_tests.c: -------------------------------------------------------------------------------- 1 | /* SPDX-FileCopyrightText: 2019-2023 Greenbone AG 2 | * 3 | * SPDX-License-Identifier: GPL-2.0-or-later 4 | */ 5 | 6 | #include "version.c" 7 | 8 | #include 9 | #include 10 | 11 | Describe (version); 12 | BeforeEach (version) 13 | { 14 | } 15 | AfterEach (version) 16 | { 17 | } 18 | 19 | Ensure (version, gvm_libs_versions_returns_correct_version) 20 | { 21 | assert_that (strcmp (gvm_libs_version (), GVM_LIBS_VERSION) == 0) 22 | } 23 | 24 | int 25 | main (int argc, char **argv) 26 | { 27 | TestSuite *suite; 28 | 29 | suite = create_test_suite (); 30 | 31 | add_test_with_context (suite, version, 32 | gvm_libs_versions_returns_correct_version); 33 | 34 | if (argc > 1) 35 | return run_single_test (suite, argv[1], create_text_reporter ()); 36 | 37 | return run_test_suite (suite, create_text_reporter ()); 38 | } 39 | -------------------------------------------------------------------------------- /boreas/alivedetection.h: -------------------------------------------------------------------------------- 1 | /* SPDX-FileCopyrightText: 2020-2023 Greenbone AG 2 | * 3 | * SPDX-License-Identifier: GPL-2.0-or-later 4 | */ 5 | 6 | #ifndef ALIVE_DETECTION_H 7 | #define ALIVE_DETECTION_H 8 | 9 | #include "../base/hosts.h" 10 | #include "../util/kb.h" 11 | 12 | #include 13 | 14 | /* to how many hosts are packets send to at a time. value <= 0 for no rate limit 15 | */ 16 | #define BURST 100 17 | /* how long (in microseconds) to wait until new BURST of packets is send */ 18 | #define BURST_TIMEOUT 100000 19 | /* Src port of outgoing TCP pings. Used for filtering incoming packets. */ 20 | #define FILTER_PORT 9910 21 | 22 | /* Default port list */ 23 | #define DEFAULT_PORT_LIST \ 24 | "21-23,25,53,80,110-111,135,139,143,443,445," \ 25 | "993,995,1723,3306,3389,5900,8080" 26 | 27 | /* Redis related */ 28 | 29 | /* Queue (Redis list) for communicating with openvas main process. */ 30 | #define ALIVE_DETECTION_QUEUE "alive_detection" 31 | /* Signal to put on ALIVE_DETECTION_QUEUE if alive detection finished. */ 32 | #define ALIVE_DETECTION_FINISHED "alive_detection_finished" 33 | 34 | void * 35 | start_alive_detection (void *); 36 | 37 | typedef struct hosts_data hosts_data_t; 38 | typedef struct scan_restrictions scan_restrictions_t; 39 | 40 | /** 41 | * @brief The scanner struct holds data which is used frequently by the alive 42 | * detection thread. 43 | */ 44 | struct scanner 45 | { 46 | /* sockets */ 47 | int tcpv4soc; 48 | int tcpv6soc; 49 | int icmpv4soc; 50 | int icmpv6soc; 51 | int arpv4soc; 52 | int arpv6soc; 53 | /* UDP socket needed for getting the source IP for the TCP header. */ 54 | int udpv4soc; 55 | int udpv6soc; 56 | /* TH_SYN or TH_ACK */ 57 | uint8_t tcp_flag; 58 | /* ports used for TCP ACK/SYN */ 59 | GArray *ports; 60 | /* redis connection */ 61 | kb_t main_kb; 62 | /* pcap handle */ 63 | pcap_t *pcap_handle; 64 | hosts_data_t *hosts_data; 65 | scan_restrictions_t *scan_restrictions; 66 | /* 0 do not print in stdout, 1 print in stdout used for cmd line cli. */ 67 | int print_results; 68 | }; 69 | 70 | typedef struct scanner scanner_t; 71 | 72 | /** 73 | * @brief The hosts_data struct holds the alive hosts and target hosts in 74 | * separate hashtables. 75 | */ 76 | struct hosts_data 77 | { 78 | /* Set of the form (ip_str, ip_str). 79 | * Target hosts which were detected as alive. */ 80 | GHashTable *alivehosts; 81 | /* Hashtable of the form (ip_str, gvm_host_t *). The gvm_host_t pointers point 82 | * to hosts which are to be freed by the caller of start_alive_detection(). */ 83 | GHashTable *targethosts; 84 | }; 85 | 86 | /* Max_scan_hosts related struct. */ 87 | struct scan_restrictions 88 | { 89 | /* Maximum number of hosts allowed to be scanned. No more alive hosts are put 90 | * on the queue after max_scan_hosts number of alive hosts is reached. 91 | * max_scan_hosts_reached is set to true and the finish signal is put on the 92 | * queue if max_scan_hosts is reached. */ 93 | int max_scan_hosts; 94 | /* Count of unique identified alive hosts. */ 95 | int alive_hosts_count; 96 | gboolean max_scan_hosts_reached; 97 | }; 98 | 99 | /** 100 | * @brief Alive tests. 101 | * 102 | * These numbers are used in the database by gvmd, so if the number associated 103 | * with any symbol changes in gvmd we need to change them here too. 104 | */ 105 | typedef enum 106 | { 107 | ALIVE_TEST_TCP_ACK_SERVICE = 1, 108 | ALIVE_TEST_ICMP = 2, 109 | ALIVE_TEST_ARP = 4, 110 | ALIVE_TEST_CONSIDER_ALIVE = 8, 111 | ALIVE_TEST_TCP_SYN_SERVICE = 16 112 | } alive_test_t; 113 | 114 | /** 115 | * @brief Type of socket. 116 | */ 117 | typedef enum 118 | { 119 | TCPV4, 120 | TCPV6, 121 | ICMPV4, 122 | ICMPV6, 123 | ARPV4, 124 | ARPV6, 125 | UDPV4, 126 | UDPV6, 127 | } socket_type_t; 128 | 129 | #endif /* not ALIVE_DETECTION_H */ 130 | -------------------------------------------------------------------------------- /boreas/alivedetection_tests.c: -------------------------------------------------------------------------------- 1 | /* SPDX-FileCopyrightText: 2020-2023 Greenbone AG 2 | * 3 | * SPDX-License-Identifier: GPL-2.0-or-later 4 | */ 5 | 6 | #include "alivedetection.c" 7 | 8 | #include 9 | #include 10 | 11 | Describe (alivedetection); 12 | BeforeEach (alivedetection) 13 | { 14 | } 15 | AfterEach (alivedetection) 16 | { 17 | } 18 | 19 | Ensure (alivedetection, dummy_test) 20 | { 21 | assert_that (0, is_equal_to (0)); 22 | } 23 | 24 | int 25 | main (int argc, char **argv) 26 | { 27 | TestSuite *suite; 28 | 29 | suite = create_test_suite (); 30 | 31 | add_test_with_context (suite, alivedetection, dummy_test); 32 | 33 | if (argc > 1) 34 | return run_single_test (suite, argv[1], create_text_reporter ()); 35 | 36 | return run_test_suite (suite, create_text_reporter ()); 37 | } 38 | -------------------------------------------------------------------------------- /boreas/arp.h: -------------------------------------------------------------------------------- 1 | /* SPDX-FileCopyrightText: 2021-2023 Greenbone AG 2 | * 3 | * SPDX-License-Identifier: GPL-2.0-or-later 4 | */ 5 | 6 | #ifndef ARP_H 7 | #define ARP_H 8 | 9 | void 10 | send_arp_v4 (const char *); 11 | 12 | #endif /* not ARP_H */ 13 | -------------------------------------------------------------------------------- /boreas/boreas_error.c: -------------------------------------------------------------------------------- 1 | /* SPDX-FileCopyrightText: 2020-2023 Greenbone AG 2 | * 3 | * SPDX-License-Identifier: GPL-2.0-or-later 4 | */ 5 | 6 | #include "boreas_error.h" 7 | 8 | #include 9 | 10 | #undef G_LOG_DOMAIN 11 | /** 12 | * @brief GLib log domain. 13 | */ 14 | #define G_LOG_DOMAIN "libgvm boreas" 15 | 16 | /** 17 | * @brief Transform Boreas error code into human readable error message. 18 | * 19 | * @param boreas_error Boreas error code. 20 | * 21 | * @return String representation of supplied error code. 22 | */ 23 | const char * 24 | str_boreas_error (boreas_error_t boreas_error) 25 | { 26 | const gchar *msg; 27 | 28 | msg = NULL; 29 | switch (boreas_error) 30 | { 31 | case BOREAS_OPENING_SOCKET_FAILED: 32 | msg = "Boreas was not able to open a new socket"; 33 | break; 34 | case BOREAS_SETTING_SOCKET_OPTION_FAILED: 35 | msg = "Boreas was not able to set socket option for socket"; 36 | break; 37 | case BOREAS_NO_VALID_ALIVE_TEST_SPECIFIED: 38 | msg = 39 | "No valid alive detection method was specified for Boreas by the user"; 40 | break; 41 | case BOREAS_CLEANUP_ERROR: 42 | msg = "Boreas encountered an error during clean up."; 43 | break; 44 | case BOREAS_NO_SRC_ADDR_FOUND: 45 | msg = "Boreas was not able to determine a source address for the given " 46 | "destination."; 47 | break; 48 | case NO_ERROR: 49 | msg = "No error was encountered by Boreas"; 50 | break; 51 | default: 52 | break; 53 | } 54 | return msg; 55 | } 56 | -------------------------------------------------------------------------------- /boreas/boreas_error.h: -------------------------------------------------------------------------------- 1 | /* SPDX-FileCopyrightText: 2020-2023 Greenbone AG 2 | * 3 | * SPDX-License-Identifier: GPL-2.0-or-later 4 | */ 5 | 6 | #ifndef BOREAS_ERROR_H 7 | #define BOREAS_ERROR_H 8 | 9 | /** 10 | * @brief Alive detection error codes. 11 | */ 12 | typedef enum 13 | { 14 | BOREAS_OPENING_SOCKET_FAILED = -100, 15 | BOREAS_SETTING_SOCKET_OPTION_FAILED, 16 | BOREAS_NO_VALID_ALIVE_TEST_SPECIFIED, 17 | BOREAS_CLEANUP_ERROR, 18 | BOREAS_NO_SRC_ADDR_FOUND, 19 | NO_ERROR = 0, 20 | } boreas_error_t; 21 | 22 | const char *str_boreas_error (boreas_error_t); 23 | 24 | #endif /* not BOREAS_ERROR_H */ 25 | -------------------------------------------------------------------------------- /boreas/boreas_error_tests.c: -------------------------------------------------------------------------------- 1 | /* SPDX-FileCopyrightText: 2020-2023 Greenbone AG 2 | * 3 | * SPDX-License-Identifier: GPL-2.0-or-later 4 | */ 5 | 6 | #include "boreas_error.c" 7 | 8 | #include 9 | #include 10 | 11 | Describe (boreas_error); 12 | BeforeEach (boreas_error) 13 | { 14 | } 15 | AfterEach (boreas_error) 16 | { 17 | } 18 | 19 | Ensure (boreas_error, dummy_test) 20 | { 21 | assert_that (0, is_equal_to (0)); 22 | } 23 | 24 | int 25 | main (int argc, char **argv) 26 | { 27 | TestSuite *suite; 28 | 29 | suite = create_test_suite (); 30 | 31 | add_test_with_context (suite, boreas_error, dummy_test); 32 | 33 | if (argc > 1) 34 | return run_single_test (suite, argv[1], create_text_reporter ()); 35 | 36 | return run_test_suite (suite, create_text_reporter ()); 37 | } 38 | -------------------------------------------------------------------------------- /boreas/boreas_io.h: -------------------------------------------------------------------------------- 1 | /* SPDX-FileCopyrightText: 2020-2023 Greenbone AG 2 | * 3 | * SPDX-License-Identifier: GPL-2.0-or-later 4 | */ 5 | 6 | #ifndef BOREAS_IO_H 7 | #define BOREAS_IO_H 8 | 9 | #include "../base/hosts.h" 10 | #include "../util/kb.h" 11 | #include "alivedetection.h" 12 | #include "boreas_error.h" 13 | 14 | gvm_host_t * 15 | get_host_from_queue (kb_t, gboolean *); 16 | 17 | void 18 | put_host_on_queue (kb_t, char *); 19 | 20 | void 21 | put_finish_signal_on_queue (void *); 22 | 23 | void realloc_finish_signal_on_queue (kb_t); 24 | 25 | int finish_signal_on_queue (kb_t); 26 | 27 | void 28 | send_dead_hosts_to_ospd_openvas (int); 29 | 30 | void 31 | init_scan_restrictions (scanner_t *, int); 32 | 33 | void 34 | handle_scan_restrictions (scanner_t *, gchar *); 35 | 36 | gchar * 37 | get_openvas_scan_id (const gchar *, int); 38 | 39 | boreas_error_t 40 | get_alive_test_methods (alive_test_t *); 41 | 42 | const gchar * 43 | get_alive_test_ports (void); 44 | 45 | unsigned int 46 | get_alive_test_wait_timeout (void); 47 | 48 | int 49 | get_alive_hosts_count (void); 50 | 51 | #endif /* not BOREAS_IO_H */ 52 | -------------------------------------------------------------------------------- /boreas/boreas_io_tests.c: -------------------------------------------------------------------------------- 1 | /* SPDX-FileCopyrightText: 2020-2023 Greenbone AG 2 | * 3 | * SPDX-License-Identifier: GPL-2.0-or-later 4 | */ 5 | 6 | #include "boreas_io.c" 7 | 8 | #include 9 | #include 10 | 11 | Describe (boreas_io); 12 | BeforeEach (boreas_io) 13 | { 14 | } 15 | AfterEach (boreas_io) 16 | { 17 | } 18 | 19 | Ensure (boreas_io, dummy_test) 20 | { 21 | assert_that (0, is_equal_to (0)); 22 | } 23 | 24 | int 25 | main (int argc, char **argv) 26 | { 27 | TestSuite *suite; 28 | 29 | suite = create_test_suite (); 30 | 31 | add_test_with_context (suite, boreas_io, dummy_test); 32 | 33 | if (argc > 1) 34 | return run_single_test (suite, argv[1], create_text_reporter ()); 35 | 36 | return run_test_suite (suite, create_text_reporter ()); 37 | } 38 | -------------------------------------------------------------------------------- /boreas/cli.h: -------------------------------------------------------------------------------- 1 | /* SPDX-FileCopyrightText: 2020-2023 Greenbone AG 2 | * 3 | * SPDX-License-Identifier: GPL-2.0-or-later 4 | */ 5 | 6 | #ifndef CLI_H 7 | #define CLI_H 8 | 9 | #include "alivedetection.h" 10 | #include "boreas_error.h" 11 | 12 | boreas_error_t 13 | run_cli_extended (gvm_hosts_t *, alive_test_t, const gchar *, 14 | const unsigned int); 15 | 16 | boreas_error_t 17 | run_cli (gvm_hosts_t *, alive_test_t, const gchar *); 18 | 19 | boreas_error_t 20 | is_host_alive (const char *, int *); 21 | 22 | #endif /* not CLI_H */ 23 | -------------------------------------------------------------------------------- /boreas/cli_tests.c: -------------------------------------------------------------------------------- 1 | /* SPDX-FileCopyrightText: 2020-2023 Greenbone AG 2 | * 3 | * SPDX-License-Identifier: GPL-2.0-or-later 4 | */ 5 | 6 | #include "ping.c" 7 | 8 | #include 9 | #include 10 | 11 | Describe (ping); 12 | BeforeEach (ping) 13 | { 14 | } 15 | AfterEach (ping) 16 | { 17 | } 18 | 19 | Ensure (ping, dummy_test) 20 | { 21 | assert_that (0, is_equal_to (0)); 22 | } 23 | 24 | int 25 | main (int argc, char **argv) 26 | { 27 | TestSuite *suite; 28 | 29 | suite = create_test_suite (); 30 | 31 | add_test_with_context (suite, ping, dummy_test); 32 | 33 | if (argc > 1) 34 | return run_single_test (suite, argv[1], create_text_reporter ()); 35 | 36 | return run_test_suite (suite, create_text_reporter ()); 37 | } 38 | -------------------------------------------------------------------------------- /boreas/libgvm_boreas.pc.in: -------------------------------------------------------------------------------- 1 | prefix=@CMAKE_INSTALL_PREFIX@ 2 | exec_prefix=@EXEC_PREFIX@ 3 | libdir=@LIBDIR@ 4 | includedir=@INCLUDEDIR@ 5 | 6 | Name: gvmlibs-boreas 7 | Description: Greenbone ALive Detection Module Boreas 8 | Version: @LIBGVMCONFIG_VERSION@ 9 | Requires.private: glib-2.0 >= 2.42.0 10 | Cflags: -I${includedir} -I${includedir}/gvm 11 | Libs: -L${libdir} -lgvm_boreas 12 | -------------------------------------------------------------------------------- /boreas/ping.h: -------------------------------------------------------------------------------- 1 | /* SPDX-FileCopyrightText: 2020-2023 Greenbone AG 2 | * 3 | * SPDX-License-Identifier: GPL-2.0-or-later 4 | */ 5 | 6 | #ifndef BOREAS_PING_H 7 | #define BOREAS_PING_H 8 | 9 | #include 10 | 11 | void send_icmp (gpointer, gpointer, gpointer); 12 | 13 | void send_tcp (gpointer, gpointer, gpointer); 14 | 15 | void send_arp (gpointer, gpointer, gpointer); 16 | 17 | #endif /* not BOREAS_PING_H */ 18 | -------------------------------------------------------------------------------- /boreas/ping_tests.c: -------------------------------------------------------------------------------- 1 | /* SPDX-FileCopyrightText: 2020-2023 Greenbone AG 2 | * 3 | * SPDX-License-Identifier: GPL-2.0-or-later 4 | */ 5 | 6 | #include "ping.c" 7 | 8 | #include 9 | #include 10 | 11 | Describe (ping); 12 | BeforeEach (ping) 13 | { 14 | } 15 | AfterEach (ping) 16 | { 17 | } 18 | 19 | Ensure (ping, dummy_test) 20 | { 21 | assert_that (0, is_equal_to (0)); 22 | } 23 | 24 | int 25 | main (int argc, char **argv) 26 | { 27 | TestSuite *suite; 28 | 29 | suite = create_test_suite (); 30 | 31 | add_test_with_context (suite, ping, dummy_test); 32 | 33 | if (argc > 1) 34 | return run_single_test (suite, argv[1], create_text_reporter ()); 35 | 36 | return run_test_suite (suite, create_text_reporter ()); 37 | } 38 | -------------------------------------------------------------------------------- /boreas/sniffer.h: -------------------------------------------------------------------------------- 1 | /* SPDX-FileCopyrightText: 2020-2023 Greenbone AG 2 | * 3 | * SPDX-License-Identifier: GPL-2.0-or-later 4 | */ 5 | 6 | #ifndef BOREAS_SNIFFER_H 7 | #define BOREAS_SNIFFER_H 8 | 9 | #include "alivedetection.h" 10 | 11 | #include 12 | 13 | int 14 | start_sniffer_thread (scanner_t *, pthread_t *); 15 | 16 | int 17 | stop_sniffer_thread (scanner_t *, pthread_t); 18 | 19 | #endif /* not BOREAS_SNIFFER_H */ 20 | -------------------------------------------------------------------------------- /boreas/sniffer_tests.c: -------------------------------------------------------------------------------- 1 | /* SPDX-FileCopyrightText: 2020-2023 Greenbone AG 2 | * 3 | * SPDX-License-Identifier: GPL-2.0-or-later 4 | */ 5 | 6 | #include "sniffer.c" 7 | 8 | #include 9 | #include 10 | 11 | Describe (sniffer); 12 | BeforeEach (sniffer) 13 | { 14 | } 15 | AfterEach (sniffer) 16 | { 17 | } 18 | 19 | Ensure (sniffer, dummy_test) 20 | { 21 | assert_that (0, is_equal_to (0)); 22 | } 23 | 24 | int 25 | main (int argc, char **argv) 26 | { 27 | TestSuite *suite; 28 | 29 | suite = create_test_suite (); 30 | 31 | add_test_with_context (suite, sniffer, dummy_test); 32 | 33 | if (argc > 1) 34 | return run_single_test (suite, argv[1], create_text_reporter ()); 35 | 36 | return run_test_suite (suite, create_text_reporter ()); 37 | } 38 | -------------------------------------------------------------------------------- /boreas/util.h: -------------------------------------------------------------------------------- 1 | /* SPDX-FileCopyrightText: 2020-2023 Greenbone AG 2 | * 3 | * SPDX-License-Identifier: GPL-2.0-or-later 4 | */ 5 | 6 | #ifndef BOREAS_UTIL_H 7 | #define BOREAS_UTIL_H 8 | 9 | #include "alivedetection.h" 10 | #include "boreas_error.h" 11 | 12 | #include 13 | 14 | uint16_t 15 | in_cksum (uint16_t *addr, int len); 16 | 17 | int 18 | get_source_mac_addr (char *, uint8_t *); 19 | 20 | boreas_error_t 21 | get_source_addr_v6 (int *, struct in6_addr *, struct in6_addr *); 22 | 23 | boreas_error_t 24 | get_source_addr_v4 (int *, struct in_addr *, struct in_addr *); 25 | 26 | void fill_ports_array (gpointer, gpointer); 27 | 28 | boreas_error_t 29 | set_all_needed_sockets (scanner_t *, alive_test_t); 30 | 31 | boreas_error_t 32 | close_all_needed_sockets (scanner_t *, alive_test_t); 33 | 34 | void 35 | wait_until_so_sndbuf_empty (int, int); 36 | 37 | /* Misc hashtable functions. */ 38 | 39 | int 40 | count_difference (GHashTable *, GHashTable *); 41 | 42 | #endif /* not BOREAS_UTIL_H */ 43 | -------------------------------------------------------------------------------- /cliff.toml: -------------------------------------------------------------------------------- 1 | [changelog] 2 | # template for the changelog header 3 | header = """ 4 | # Changelog\n 5 | All notable changes to this project will be documented in this file. 6 | 7 | The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), 8 | and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).\n 9 | """ 10 | # template for the changelog body 11 | # https://keats.github.io/tera/docs/#introduction 12 | body = """ 13 | {%- macro remote_url() -%} 14 | https://github.com/{{ remote.github.owner }}/{{ remote.github.repo }} 15 | {%- endmacro -%} 16 | 17 | {% if version -%} 18 | ## [{{ version | trim_start_matches(pat="v") }}] - {{ timestamp | date(format="%Y-%m-%d") }} 19 | {% else -%} 20 | ## [Unreleased] 21 | {% endif -%} 22 | 23 | {% for group, commits in commits | group_by(attribute="group") %} 24 | ### {{ group | upper_first }} 25 | {% for commit in commits %} 26 | - {{ commit.message | split(pat="\n") | first | upper_first | trim }}\ 27 | {% if commit.remote.username %} by [@{{ commit.remote.username }}](https://github.com/{{ commit.remote.username }}){%- endif -%} 28 | {% if commit.remote.pr_number %} in \ 29 | [#{{ commit.remote.pr_number }}]({{ self::remote_url() }}/pull/{{ commit.remote.pr_number }}) \ 30 | {% elif commit.id %} in \ 31 | [{{ commit.id | truncate(length=7, end="") }}]({{ self::remote_url() }}/commit/{{ commit.id }})\ 32 | {%- endif -%} 33 | {% endfor %} 34 | {% endfor -%} 35 | """ 36 | # template for the changelog footer 37 | footer = """ 38 | {%- macro remote_url() -%} 39 | https://github.com/{{ remote.github.owner }}/{{ remote.github.repo }} 40 | {%- endmacro -%} 41 | 42 | {% for release in releases %} 43 | {% if release.version -%} 44 | {% if release.previous.version -%} 45 | [{{ release.version | trim_start_matches(pat="v") }}]: \ 46 | {{ self::remote_url() }}/compare/{{ release.previous.version }}..{{ release.version }} 47 | {% endif -%} 48 | {% else -%} 49 | [unreleased]: {{ self::remote_url() }}/compare/{{ release.previous.version }}..HEAD 50 | {% endif -%} 51 | {%- endfor -%} 52 | """ 53 | # remove the leading and trailing whitespace from the templates 54 | trim = true 55 | 56 | [git] 57 | # parse the commits based on https://www.conventionalcommits.org 58 | conventional_commits = true 59 | # filter out the commits that are not following the conventional commits format 60 | filter_unconventional = false 61 | # process each line of a commit as an individual commit 62 | split_commits = false 63 | # regex for preprocessing the commit messages 64 | commit_preprocessors = [ 65 | # remove issue numbers from commits 66 | { pattern = '\((\w+\s)?#([0-9]+)\)', replace = "" }, 67 | ] 68 | # regex for parsing and grouping commits 69 | commit_parsers = [ 70 | { message = "^[a|A]dd", group = ":sparkles: Added" }, 71 | { message = "^[c|C]hange", group = ":construction_worker: Changed" }, 72 | { message = "^[f|F]ix", group = ":bug: Bug Fixes" }, 73 | { message = "^[r|R]emove", group = ":fire: Removed" }, 74 | { message = "^[d|D]rop", group = ":fire: Removed" }, 75 | { message = "^[d|D]oc", group = ":books: Documentation" }, 76 | { message = "^[t|T]est", group = ":white_check_mark: Testing" }, 77 | { message = "^[c|C]hore", group = ":wrench: Miscellaneous" }, 78 | { message = "^[c|C]i", group = "️:wrench: Miscellaneous" }, 79 | { message = "^[m|M]isc", group = ":wrench: Miscellaneous" }, 80 | { message = "^[d|D]eps", group = ":ship: Dependencies" }, 81 | ] 82 | # filter out the commits that are not matched by commit parsers 83 | filter_commits = true 84 | # sort the tags topologically 85 | topo_order = false 86 | # sort the commits inside sections by oldest/newest order 87 | sort_commits = "oldest" 88 | -------------------------------------------------------------------------------- /cmake/MacroAddUnitTest.cmake: -------------------------------------------------------------------------------- 1 | macro(add_unit_test _testName _testSource) 2 | add_executable(${_testName} ${_testSource}) 3 | target_link_libraries(${_testName} ${CGREEN_LIBRARIES} ${ARGN}) 4 | target_include_directories(${_testName} PRIVATE ${CGREEN_INCLUDE_DIRS}) 5 | add_test(NAME ${_testName} COMMAND ${CMAKE_CURRENT_BINARY_DIR}/${_testName}) 6 | endmacro() 7 | -------------------------------------------------------------------------------- /cmake/MacroGitGetRevision.cmake: -------------------------------------------------------------------------------- 1 | macro(git_get_revision dir variable) 2 | execute_process( 3 | COMMAND ${GIT_EXECUTABLE} rev-parse --abbrev-ref HEAD 4 | WORKING_DIRECTORY ${dir} 5 | OUTPUT_VARIABLE GIT_BRANCH 6 | OUTPUT_STRIP_TRAILING_WHITESPACE 7 | ) 8 | execute_process( 9 | COMMAND ${GIT_EXECUTABLE} log -1 --format=%h 10 | WORKING_DIRECTORY ${dir} 11 | OUTPUT_VARIABLE GIT_COMMIT_HASH 12 | OUTPUT_STRIP_TRAILING_WHITESPACE 13 | ) 14 | string(REPLACE "/" "_" GIT_BRANCH ${GIT_BRANCH}) 15 | set(${variable} "${GIT_COMMIT_HASH}-${GIT_BRANCH}") 16 | endmacro() 17 | -------------------------------------------------------------------------------- /doc/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # SPDX-FileCopyrightText: 2011-2023 Greenbone AG 2 | # 3 | # SPDX-License-Identifier: GPL-2.0-or-later 4 | 5 | ## build 6 | 7 | include(FindDoxygen) 8 | 9 | if(NOT DOXYGEN_EXECUTABLE) 10 | message(STATUS "WARNING: Doxygen is required to build the HTML docs.") 11 | else(NOT DOXYGEN_EXECUTABLE) 12 | add_custom_target( 13 | doc 14 | COMMENT "Building documentation..." 15 | DEPENDS 16 | ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile 17 | ${CMAKE_CURRENT_BINARY_DIR}/.built-html 18 | ) 19 | 20 | add_custom_command( 21 | OUTPUT .built-html 22 | COMMAND sh 23 | ARGS 24 | -c \"${DOXYGEN_EXECUTABLE} ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile && touch 25 | ${CMAKE_CURRENT_BINARY_DIR}/.built-html\;\" 26 | DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile 27 | ) 28 | 29 | add_custom_target( 30 | doc-full 31 | COMMENT "Building documentation..." 32 | DEPENDS 33 | ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile_full 34 | ${CMAKE_CURRENT_BINARY_DIR}/.built-html_full 35 | ) 36 | 37 | add_custom_command( 38 | OUTPUT .built-html_full 39 | COMMAND sh 40 | ARGS 41 | -c \"${DOXYGEN_EXECUTABLE} ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile_full && 42 | touch ${CMAKE_CURRENT_BINARY_DIR}/.built-html_full\;\" 43 | DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile_full 44 | ) 45 | 46 | add_custom_target( 47 | doc-xml 48 | COMMENT "Building documentation (XML)..." 49 | DEPENDS 50 | ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile_xml 51 | ${CMAKE_CURRENT_BINARY_DIR}/.built-xml 52 | ) 53 | 54 | add_custom_command( 55 | OUTPUT .built-xml 56 | COMMAND sh 57 | ARGS 58 | -c \"${DOXYGEN_EXECUTABLE} ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile_xml && 59 | touch ${CMAKE_CURRENT_BINARY_DIR}/.built-xml\;\" 60 | DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile_xml 61 | ) 62 | endif(NOT DOXYGEN_EXECUTABLE) 63 | -------------------------------------------------------------------------------- /gmp/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # SPDX-FileCopyrightText: 2017-2023 Greenbone AG 2 | # 3 | # SPDX-License-Identifier: GPL-2.0-or-later 4 | 5 | ## Library 6 | 7 | include(FindPkgConfig) 8 | 9 | if(NOT PKG_CONFIG_FOUND) 10 | message(FATAL_ERROR "pkg-config executable not found. Aborting.") 11 | endif(NOT PKG_CONFIG_FOUND) 12 | 13 | ## Dependency checks 14 | 15 | pkg_check_modules(GLIB REQUIRED glib-2.0>=2.42) 16 | 17 | include_directories(${GLIB_INCLUDE_DIRS}) 18 | 19 | set(FILES gmp.c) 20 | 21 | set(HEADERS gmp.h) 22 | 23 | if(BUILD_STATIC) 24 | add_library(gvm_gmp_static STATIC ${FILES}) 25 | set_target_properties(gvm_gmp_static PROPERTIES OUTPUT_NAME "gvm_gmp") 26 | set_target_properties(gvm_gmp_static PROPERTIES CLEAN_DIRECT_OUTPUT 1) 27 | set_target_properties(gvm_gmp_static PROPERTIES PUBLIC_HEADER "${HEADERS}") 28 | endif(BUILD_STATIC) 29 | 30 | if(BUILD_SHARED) 31 | add_library(gvm_gmp_shared SHARED ${FILES}) 32 | set_target_properties(gvm_gmp_shared PROPERTIES OUTPUT_NAME "gvm_gmp") 33 | set_target_properties(gvm_gmp_shared PROPERTIES CLEAN_DIRECT_OUTPUT 1) 34 | set_target_properties( 35 | gvm_gmp_shared 36 | PROPERTIES SOVERSION "${PROJECT_VERSION_MAJOR}" 37 | ) 38 | set_target_properties( 39 | gvm_gmp_shared 40 | PROPERTIES VERSION "${CPACK_PACKAGE_VERSION}" 41 | ) 42 | set_target_properties(gvm_gmp_shared PROPERTIES PUBLIC_HEADER "${HEADERS}") 43 | 44 | target_link_libraries( 45 | gvm_gmp_shared 46 | LINK_PRIVATE ${GLIB_LDFLAGS} ${LINKER_HARDENING_FLAGS} 47 | ) 48 | endif(BUILD_SHARED) 49 | 50 | ## Install 51 | configure_file(libgvm_gmp.pc.in ${CMAKE_BINARY_DIR}/libgvm_gmp.pc @ONLY) 52 | 53 | install(FILES ${CMAKE_BINARY_DIR}/libgvm_gmp.pc DESTINATION ${LIBDIR}/pkgconfig) 54 | 55 | if(BUILD_STATIC) 56 | install( 57 | TARGETS gvm_gmp_static 58 | RUNTIME DESTINATION ${BINDIR} 59 | ARCHIVE DESTINATION ${LIBDIR} 60 | PUBLIC_HEADER DESTINATION "${INCLUDEDIR}/gvm/gmp" 61 | ) 62 | endif(BUILD_STATIC) 63 | if(BUILD_SHARED) 64 | install( 65 | TARGETS gvm_gmp_shared 66 | RUNTIME DESTINATION ${BINDIR} 67 | LIBRARY DESTINATION ${LIBDIR} 68 | ARCHIVE DESTINATION ${LIBDIR} 69 | PUBLIC_HEADER DESTINATION "${INCLUDEDIR}/gvm/gmp" 70 | ) 71 | endif(BUILD_SHARED) 72 | 73 | ## End 74 | -------------------------------------------------------------------------------- /gmp/libgvm_gmp.pc.in: -------------------------------------------------------------------------------- 1 | prefix=@CMAKE_INSTALL_PREFIX@ 2 | exec_prefix=@EXEC_PREFIX@ 3 | libdir=@LIBDIR@ 4 | includedir=@INCLUDEDIR@ 5 | 6 | Name: gvmlibs-gmp 7 | Description: Greenbone Vulnerability Management Library gmp 8 | Version: @LIBGVMCONFIG_VERSION@ 9 | Requires.private: glib-2.0 >= 2.42.0 10 | Cflags: -I${includedir} -I${includedir}/gvm 11 | Libs: -L${libdir} -lgvm_gmp 12 | -------------------------------------------------------------------------------- /http/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # SPDX-FileCopyrightText: 2015-2025 Greenbone AG 2 | # 3 | # SPDX-License-Identifier: GPL-2.0-or-later 4 | 5 | ## Library 6 | 7 | include(FindPkgConfig) 8 | 9 | if(NOT PKG_CONFIG_FOUND) 10 | message(FATAL_ERROR "pkg-config executable not found. Aborting.") 11 | endif(NOT PKG_CONFIG_FOUND) 12 | 13 | ## Dependency checks 14 | 15 | pkg_check_modules(GLIB REQUIRED glib-2.0>=2.42) 16 | pkg_check_modules(CURL REQUIRED libcurl>=7.83.0) 17 | 18 | include_directories(${GLIB_INCLUDE_DIRS} ${CURL_INCLUDE_DIRS}) 19 | 20 | set(FILES httputils.c) 21 | set(HEADERS httputils.h) 22 | 23 | if(BUILD_STATIC) 24 | add_library(gvm_http_static STATIC ${FILES}) 25 | set_target_properties(gvm_http_static PROPERTIES OUTPUT_NAME "gvm_http") 26 | set_target_properties(gvm_http_static PROPERTIES CLEAN_DIRECT_OUTPUT 1) 27 | set_target_properties(gvm_http_static PROPERTIES PUBLIC_HEADER "${HEADERS}") 28 | endif(BUILD_STATIC) 29 | 30 | if(BUILD_SHARED) 31 | add_library(gvm_http_shared SHARED ${FILES}) 32 | set_target_properties(gvm_http_shared PROPERTIES OUTPUT_NAME "gvm_http") 33 | set_target_properties(gvm_http_shared PROPERTIES CLEAN_DIRECT_OUTPUT 1) 34 | set_target_properties( 35 | gvm_http_shared 36 | PROPERTIES SOVERSION "${PROJECT_VERSION_MAJOR}" 37 | ) 38 | set_target_properties( 39 | gvm_http_shared 40 | PROPERTIES VERSION "${CPACK_PACKAGE_VERSION}" 41 | ) 42 | set_target_properties(gvm_http_shared PROPERTIES PUBLIC_HEADER "${HEADERS}") 43 | 44 | target_link_libraries( 45 | gvm_http_shared 46 | LINK_PRIVATE ${GLIB_LDFLAGS} ${CURL_LDFLAGS} ${LINKER_HARDENING_FLAGS} 47 | ) 48 | endif(BUILD_SHARED) 49 | 50 | ## Tests 51 | 52 | if(BUILD_TESTS) 53 | add_unit_test( 54 | httputils-test 55 | httputils_test.c 56 | gvm_base_shared 57 | gvm_util_shared 58 | ${GLIB_LDFLAGS} 59 | ${CJSON_LDFLAGS} 60 | ${CURL_LDFLAGS} 61 | ${LINKER_HARDENING_FLAGS} 62 | ) 63 | endif(BUILD_TESTS) 64 | 65 | ## Install 66 | configure_file(libgvm_http.pc.in ${CMAKE_BINARY_DIR}/libgvm_http.pc @ONLY) 67 | 68 | install( 69 | FILES ${CMAKE_BINARY_DIR}/libgvm_http.pc 70 | DESTINATION ${LIBDIR}/pkgconfig 71 | ) 72 | 73 | if(BUILD_STATIC) 74 | install( 75 | TARGETS gvm_http_static 76 | RUNTIME DESTINATION ${BINDIR} 77 | ARCHIVE DESTINATION ${LIBDIR} 78 | PUBLIC_HEADER DESTINATION "${INCLUDEDIR}/gvm/http" 79 | ) 80 | endif(BUILD_STATIC) 81 | if(BUILD_SHARED) 82 | install( 83 | TARGETS gvm_http_shared 84 | RUNTIME DESTINATION ${BINDIR} 85 | LIBRARY DESTINATION ${LIBDIR} 86 | ARCHIVE DESTINATION ${LIBDIR} 87 | PUBLIC_HEADER DESTINATION "${INCLUDEDIR}/gvm/http" 88 | ) 89 | endif(BUILD_SHARED) 90 | 91 | ## End 92 | -------------------------------------------------------------------------------- /http/httputils.h: -------------------------------------------------------------------------------- 1 | /* SPDX-FileCopyrightText: 2019-2025 Greenbone AG 2 | * 3 | * SPDX-License-Identifier: GPL-2.0-or-later 4 | */ 5 | 6 | /** 7 | * @file httputils.h 8 | * @brief HTTP(S) utility API built on top of libcurl for the Greenbone 9 | * framework. 10 | * 11 | * This module provides a high-level wrapper around libcurl to simplify the 12 | * process of performing HTTP and HTTPS requests in both synchronous and 13 | * asynchronous (multi) modes. It offers abstractions for: 14 | * 15 | * - Sending requests with various HTTP methods (GET, POST, PUT, DELETE, etc.) 16 | * - Managing custom request headers 17 | * - SSL/TLS authentication with CA, client certificates, and private keys 18 | * - Accumulating and handling response data via callback streams 19 | * - Managing and cleaning up single and multi-handle CURL resources 20 | * 21 | * Core data structures: 22 | * - `gvm_http_t`: encapsulates a single HTTP request configuration and state 23 | * - `gvm_http_response_t`: represents the HTTP response including status, data, 24 | * and associated request 25 | * - `gvm_http_headers_t`: stores custom headers for use in requests 26 | * - `gvm_http_response_stream_t`: used internally for accumulating response 27 | * data during transfers 28 | * - `gvm_http_multi_t`: manages multiple concurrent transfers using libcurl's 29 | * multi interface 30 | */ 31 | 32 | #ifndef HTTPUTILS_H 33 | #define HTTPUTILS_H 34 | 35 | #include 36 | #include 37 | #include 38 | #include 39 | 40 | /** 41 | * @brief Request methods 42 | */ 43 | typedef enum 44 | { 45 | GET, 46 | POST, 47 | PUT, 48 | DELETE, 49 | HEAD, 50 | PATCH 51 | } gvm_http_method_t; 52 | 53 | typedef enum 54 | { 55 | GVM_HTTP_OK, 56 | GVM_HTTP_MULTI_BAD_HANDLE, 57 | GVM_HTTP_MULTI_FAILED, 58 | GVM_HTTP_MULTI_UNKNOWN_ERROR 59 | } gvm_http_multi_result_t; 60 | 61 | typedef struct gvm_http_headers 62 | { 63 | struct curl_slist *custom_headers; 64 | 65 | } gvm_http_headers_t; 66 | 67 | /** 68 | * @brief Wraps a CURLM * handler and the custom headers. 69 | */ 70 | typedef struct gvm_http_multi 71 | { 72 | void *handler; ///< Opaque pointer to the internal CURLM handle. 73 | 74 | gvm_http_headers_t *headers; ///< The wrapped headers type. 75 | 76 | } gvm_http_multi_t; 77 | 78 | /** 79 | * @brief Defines a struct for storing the response and http multi-handler. 80 | */ 81 | struct gvm_http_response_stream 82 | { 83 | gchar *data; ///< Pointer to the accumulated response data buffer. 84 | 85 | size_t length; ///< Length of the response data buffer. 86 | 87 | gvm_http_multi_t *multi_handler; ///< Pointer to the associated http 88 | ///< multi-handle and headers. 89 | }; 90 | 91 | typedef struct gvm_http_response_stream *gvm_http_response_stream_t; 92 | 93 | typedef struct 94 | { 95 | CURL *handler; 96 | 97 | } gvm_http_t; 98 | 99 | /** 100 | * @brief Represents the result of a http request. 101 | */ 102 | typedef struct 103 | { 104 | gchar *data; ///< The actual response content as a string. 105 | 106 | gsize size; ///< Size of the response content. 107 | 108 | glong http_status; ///< HTTP status code returned by the server. 109 | 110 | gvm_http_t *http; ///< The HTTP request (easy handle wrapper). 111 | } gvm_http_response_t; 112 | 113 | void 114 | gvm_http_free (gvm_http_t *http); 115 | 116 | gvm_http_t * 117 | gvm_http_new (const gchar *url, gvm_http_method_t method, const gchar *payload, 118 | gvm_http_headers_t *headers, const gchar *ca_cert, 119 | const gchar *client_cert, const gchar *client_key, 120 | gvm_http_response_stream_t res); 121 | 122 | gvm_http_response_t * 123 | gvm_http_request (const gchar *url, gvm_http_method_t method, 124 | const gchar *payload, gvm_http_headers_t *headers, 125 | const gchar *ca_cert, const gchar *client_cert, 126 | const gchar *client_key, gvm_http_response_stream_t response); 127 | 128 | gvm_http_headers_t * 129 | gvm_http_headers_new (void); 130 | 131 | gboolean 132 | gvm_http_add_header (gvm_http_headers_t *headers, const gchar *header); 133 | 134 | void 135 | gvm_http_headers_free (gvm_http_headers_t *headers); 136 | 137 | void 138 | gvm_http_response_cleanup (gvm_http_response_t *response); 139 | 140 | gvm_http_multi_t * 141 | gvm_http_multi_new (void); 142 | 143 | gvm_http_multi_result_t 144 | gvm_http_multi_add_handler (gvm_http_multi_t *multi, gvm_http_t *http); 145 | 146 | gvm_http_multi_result_t 147 | gvm_http_multi_perform (gvm_http_multi_t *multi, int *running_handles); 148 | 149 | void 150 | gvm_http_multi_handler_free (gvm_http_multi_t *multi, gvm_http_t *http); 151 | 152 | void 153 | gvm_http_multi_free (gvm_http_multi_t *multi); 154 | 155 | gvm_http_response_stream_t 156 | gvm_http_response_stream_new (void); 157 | 158 | void 159 | gvm_http_response_stream_free (gvm_http_response_stream_t s); 160 | 161 | void 162 | gvm_http_response_stream_reset (gvm_http_response_stream_t s); 163 | 164 | #endif // HTTPUTILS_H 165 | -------------------------------------------------------------------------------- /http/libgvm_http.pc.in: -------------------------------------------------------------------------------- 1 | prefix=@CMAKE_INSTALL_PREFIX@ 2 | exec_prefix=@EXEC_PREFIX@ 3 | libdir=@LIBDIR@ 4 | includedir=@INCLUDEDIR@ 5 | 6 | Name: gvmlibs-http 7 | Description: Greenbone Vulnerability Management Library HTTP 8 | Version: @LIBGVMCONFIG_VERSION@ 9 | Requires.private: glib-2.0 >= 2.42.0 10 | Cflags: -I${includedir} -I${includedir}/gvm 11 | Libs: -L${libdir} -lgvm_http 12 | -------------------------------------------------------------------------------- /openvasd/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # SPDX-FileCopyrightText: 2015-2023 Greenbone AG 2 | # 3 | # SPDX-License-Identifier: GPL-2.0-or-later 4 | 5 | ## Library 6 | 7 | include(FindPkgConfig) 8 | 9 | if(NOT PKG_CONFIG_FOUND) 10 | message(FATAL_ERROR "pkg-config executable not found. Aborting.") 11 | endif(NOT PKG_CONFIG_FOUND) 12 | 13 | ## Dependency checks 14 | 15 | pkg_check_modules(GLIB REQUIRED glib-2.0>=2.42) 16 | pkg_check_modules(CURL REQUIRED libcurl>=7.83.0) 17 | 18 | # for json parsing we need cJSON 19 | pkg_check_modules(CJSON REQUIRED libcjson>=1.7.14) 20 | 21 | include_directories(${GLIB_INCLUDE_DIRS} ${CURL_INCLUDE_DIRS}) 22 | 23 | set(FILES openvasd.c) 24 | set(HEADERS openvasd.h) 25 | 26 | if(BUILD_STATIC) 27 | add_library(gvm_openvasd_static STATIC ${FILES}) 28 | set_target_properties( 29 | gvm_openvasd_static 30 | PROPERTIES OUTPUT_NAME "gvm_openvasd" 31 | ) 32 | set_target_properties(gvm_openvasd_static PROPERTIES CLEAN_DIRECT_OUTPUT 1) 33 | set_target_properties( 34 | gvm_openvasd_static 35 | PROPERTIES PUBLIC_HEADER "${HEADERS}" 36 | ) 37 | endif(BUILD_STATIC) 38 | 39 | if(BUILD_SHARED) 40 | add_library(gvm_openvasd_shared SHARED ${FILES}) 41 | set_target_properties( 42 | gvm_openvasd_shared 43 | PROPERTIES OUTPUT_NAME "gvm_openvasd" 44 | ) 45 | set_target_properties(gvm_openvasd_shared PROPERTIES CLEAN_DIRECT_OUTPUT 1) 46 | set_target_properties( 47 | gvm_openvasd_shared 48 | PROPERTIES SOVERSION "${PROJECT_VERSION_MAJOR}" 49 | ) 50 | set_target_properties( 51 | gvm_openvasd_shared 52 | PROPERTIES VERSION "${CPACK_PACKAGE_VERSION}" 53 | ) 54 | set_target_properties( 55 | gvm_openvasd_shared 56 | PROPERTIES PUBLIC_HEADER "${HEADERS}" 57 | ) 58 | 59 | target_link_libraries( 60 | gvm_openvasd_shared 61 | LINK_PRIVATE ${GLIB_LDFLAGS} ${CURL_LDFLAGS} ${LINKER_HARDENING_FLAGS} 62 | ) 63 | endif(BUILD_SHARED) 64 | 65 | ## Tests 66 | 67 | if(BUILD_TESTS) 68 | add_unit_test( 69 | openvasd-test 70 | openvasd_tests.c 71 | gvm_http_shared 72 | gvm_base_shared 73 | gvm_util_shared 74 | ${GLIB_LDFLAGS} 75 | ${CJSON_LDFLAGS} 76 | ${CURL_LDFLAGS} 77 | ${LINKER_HARDENING_FLAGS} 78 | ) 79 | endif(BUILD_TESTS) 80 | 81 | ## Install 82 | configure_file( 83 | libgvm_openvasd.pc.in 84 | ${CMAKE_BINARY_DIR}/libgvm_openvasd.pc 85 | @ONLY 86 | ) 87 | 88 | install( 89 | FILES ${CMAKE_BINARY_DIR}/libgvm_openvasd.pc 90 | DESTINATION ${LIBDIR}/pkgconfig 91 | ) 92 | 93 | if(BUILD_STATIC) 94 | install( 95 | TARGETS gvm_openvasd_static 96 | RUNTIME DESTINATION ${BINDIR} 97 | ARCHIVE DESTINATION ${LIBDIR} 98 | PUBLIC_HEADER DESTINATION "${INCLUDEDIR}/gvm/openvasd" 99 | ) 100 | endif(BUILD_STATIC) 101 | if(BUILD_SHARED) 102 | install( 103 | TARGETS gvm_openvasd_shared 104 | RUNTIME DESTINATION ${BINDIR} 105 | LIBRARY DESTINATION ${LIBDIR} 106 | ARCHIVE DESTINATION ${LIBDIR} 107 | PUBLIC_HEADER DESTINATION "${INCLUDEDIR}/gvm/openvasd" 108 | ) 109 | endif(BUILD_SHARED) 110 | 111 | ## End 112 | -------------------------------------------------------------------------------- /openvasd/libgvm_openvasd.pc.in: -------------------------------------------------------------------------------- 1 | prefix=@CMAKE_INSTALL_PREFIX@ 2 | exec_prefix=@EXEC_PREFIX@ 3 | libdir=@LIBDIR@ 4 | includedir=@INCLUDEDIR@ 5 | 6 | Name: gvmlibs-openvasd 7 | Description: Greenbone Vulnerability Management Library openvasd 8 | Version: @LIBGVMCONFIG_VERSION@ 9 | Requires.private: glib-2.0 >= 2.42.0 10 | Cflags: -I${includedir} -I${includedir}/gvm 11 | Libs: -L${libdir} -lgvm_openvasd 12 | -------------------------------------------------------------------------------- /osp/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # SPDX-FileCopyrightText: 2015-2023 Greenbone AG 2 | # 3 | # SPDX-License-Identifier: GPL-2.0-or-later 4 | 5 | ## Library 6 | 7 | include(FindPkgConfig) 8 | 9 | if(NOT PKG_CONFIG_FOUND) 10 | message(FATAL_ERROR "pkg-config executable not found. Aborting.") 11 | endif(NOT PKG_CONFIG_FOUND) 12 | 13 | ## Dependency checks 14 | 15 | pkg_check_modules(GLIB REQUIRED glib-2.0>=2.42) 16 | 17 | include_directories(${GLIB_INCLUDE_DIRS}) 18 | 19 | set(FILES osp.c) 20 | 21 | set(HEADERS osp.h) 22 | 23 | if(BUILD_STATIC) 24 | add_library(gvm_osp_static STATIC ${FILES}) 25 | set_target_properties(gvm_osp_static PROPERTIES OUTPUT_NAME "gvm_osp") 26 | set_target_properties(gvm_osp_static PROPERTIES CLEAN_DIRECT_OUTPUT 1) 27 | set_target_properties(gvm_osp_static PROPERTIES PUBLIC_HEADER "${HEADERS}") 28 | endif(BUILD_STATIC) 29 | 30 | if(BUILD_SHARED) 31 | add_library(gvm_osp_shared SHARED ${FILES}) 32 | set_target_properties(gvm_osp_shared PROPERTIES OUTPUT_NAME "gvm_osp") 33 | set_target_properties(gvm_osp_shared PROPERTIES CLEAN_DIRECT_OUTPUT 1) 34 | set_target_properties( 35 | gvm_osp_shared 36 | PROPERTIES SOVERSION "${PROJECT_VERSION_MAJOR}" 37 | ) 38 | set_target_properties( 39 | gvm_osp_shared 40 | PROPERTIES VERSION "${CPACK_PACKAGE_VERSION}" 41 | ) 42 | set_target_properties(gvm_osp_shared PROPERTIES PUBLIC_HEADER "${HEADERS}") 43 | 44 | target_link_libraries( 45 | gvm_osp_shared 46 | LINK_PRIVATE ${GLIB_LDFLAGS} ${LINKER_HARDENING_FLAGS} 47 | ) 48 | endif(BUILD_SHARED) 49 | 50 | ## Tests 51 | 52 | if(BUILD_TESTS) 53 | add_unit_test( 54 | osp-test 55 | osp_tests.c 56 | gvm_base_shared 57 | gvm_util_shared 58 | ${GLIB_LDFLAGS} 59 | ${LINKER_HARDENING_FLAGS} 60 | ) 61 | endif(BUILD_TESTS) 62 | 63 | ## Install 64 | configure_file(libgvm_osp.pc.in ${CMAKE_BINARY_DIR}/libgvm_osp.pc @ONLY) 65 | 66 | install(FILES ${CMAKE_BINARY_DIR}/libgvm_osp.pc DESTINATION ${LIBDIR}/pkgconfig) 67 | 68 | if(BUILD_STATIC) 69 | install( 70 | TARGETS gvm_osp_static 71 | RUNTIME DESTINATION ${BINDIR} 72 | ARCHIVE DESTINATION ${LIBDIR} 73 | PUBLIC_HEADER DESTINATION "${INCLUDEDIR}/gvm/osp" 74 | ) 75 | endif(BUILD_STATIC) 76 | if(BUILD_SHARED) 77 | install( 78 | TARGETS gvm_osp_shared 79 | RUNTIME DESTINATION ${BINDIR} 80 | LIBRARY DESTINATION ${LIBDIR} 81 | ARCHIVE DESTINATION ${LIBDIR} 82 | PUBLIC_HEADER DESTINATION "${INCLUDEDIR}/gvm/osp" 83 | ) 84 | endif(BUILD_SHARED) 85 | 86 | ## End 87 | -------------------------------------------------------------------------------- /osp/libgvm_osp.pc.in: -------------------------------------------------------------------------------- 1 | prefix=@CMAKE_INSTALL_PREFIX@ 2 | exec_prefix=@EXEC_PREFIX@ 3 | libdir=@LIBDIR@ 4 | includedir=@INCLUDEDIR@ 5 | 6 | Name: gvmlibs-osp 7 | Description: Greenbone Vulnerability Management Library osp 8 | Version: @LIBGVMCONFIG_VERSION@ 9 | Requires.private: glib-2.0 >= 2.42.0 10 | Cflags: -I${includedir} -I${includedir}/gvm 11 | Libs: -L${libdir} -lgvm_osp 12 | -------------------------------------------------------------------------------- /osp/osp_tests.c: -------------------------------------------------------------------------------- 1 | /* SPDX-FileCopyrightText: 2009-2023 Greenbone AG 2 | * 3 | * SPDX-License-Identifier: GPL-2.0-or-later 4 | */ 5 | 6 | #include "osp.c" 7 | 8 | #include 9 | #include 10 | 11 | Describe (osp); 12 | BeforeEach (osp) 13 | { 14 | } 15 | AfterEach (osp) 16 | { 17 | } 18 | 19 | Ensure (osp, osp_new_target_never_returns_null) 20 | { 21 | assert_that (osp_target_new (NULL, NULL, NULL, 0, 0, 0), is_not_null); 22 | } 23 | 24 | Ensure (osp, osp_new_conn_ret_null) 25 | { 26 | assert_that (osp_connection_new ("/my/socket", 0, NULL, NULL, NULL), is_null); 27 | } 28 | 29 | Ensure (osp, osp_get_vts_no_vts_ret_error) 30 | { 31 | osp_connection_t *conn = g_malloc0 (sizeof (*conn)); 32 | assert_that (osp_get_vts (conn, NULL), is_equal_to (1)); 33 | } 34 | 35 | Ensure (osp, osp_get_vts_no_conn_ret_error) 36 | { 37 | assert_that (osp_get_vts (NULL, NULL), is_equal_to (1)); 38 | } 39 | 40 | Ensure (osp, osp_target_add_alive_test_methods) 41 | { 42 | osp_target_t *target; 43 | 44 | target = osp_target_new ("127.0.0.1", "123", NULL, 0, 0, 0); 45 | osp_target_add_alive_test_methods (target, TRUE, TRUE, TRUE, TRUE, TRUE); 46 | 47 | assert_true (target->icmp); 48 | assert_true (target->tcp_syn); 49 | assert_true (target->tcp_ack); 50 | assert_true (target->arp); 51 | assert_true (target->consider_alive); 52 | 53 | osp_target_free (target); 54 | } 55 | 56 | Ensure (osp, target_append_as_xml) 57 | { 58 | osp_target_t *target; 59 | GString *target_xml; 60 | gchar *expected_xml_string; 61 | 62 | target = osp_target_new ("127.0.0.1", "123", NULL, 0, 0, 0); 63 | osp_target_add_alive_test_methods (target, TRUE, TRUE, TRUE, TRUE, FALSE); 64 | 65 | target_xml = g_string_sized_new (10240); 66 | 67 | target_append_as_xml (target, target_xml); 68 | expected_xml_string = "" 69 | "127.0.0.1" 70 | "" 71 | "" 72 | "123" 73 | "" 74 | "1" 75 | "1" 76 | "1" 77 | "1" 78 | "0" 79 | "" 80 | ""; 81 | 82 | assert_that (target_xml->str, is_equal_to_string (expected_xml_string)); 83 | 84 | osp_target_free (target); 85 | } 86 | 87 | /* Test suite. */ 88 | 89 | int 90 | main (int argc, char **argv) 91 | { 92 | TestSuite *suite; 93 | 94 | suite = create_test_suite (); 95 | 96 | add_test_with_context (suite, osp, osp_new_target_never_returns_null); 97 | add_test_with_context (suite, osp, osp_get_vts_no_conn_ret_error); 98 | add_test_with_context (suite, osp, osp_get_vts_no_vts_ret_error); 99 | add_test_with_context (suite, osp, osp_new_conn_ret_null); 100 | add_test_with_context (suite, osp, osp_target_add_alive_test_methods); 101 | add_test_with_context (suite, osp, target_append_as_xml); 102 | 103 | if (argc > 1) 104 | return run_single_test (suite, argv[1], create_text_reporter ()); 105 | 106 | return run_test_suite (suite, create_text_reporter ()); 107 | } 108 | -------------------------------------------------------------------------------- /tests/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # SPDX-FileCopyrightText: 2017-2023 Greenbone AG 2 | # 3 | # SPDX-License-Identifier: GPL-2.0-or-later 4 | 5 | # test-hosts executable 6 | 7 | include_directories(${GLIB_INCLUDE_DIRS}) 8 | 9 | if(BUILD_SHARED) 10 | add_executable(test-hosts test-hosts.c) 11 | set_target_properties(test-hosts PROPERTIES LINKER_LANGUAGE C) 12 | target_link_libraries(test-hosts ${LIBGVM_BASE_NAME} -lm ${GLIB_LDFLAGS}) 13 | endif(BUILD_SHARED) 14 | 15 | ## End 16 | -------------------------------------------------------------------------------- /tests/test-hosts.c: -------------------------------------------------------------------------------- 1 | /* SPDX-FileCopyrightText: 2013-2023 Greenbone AG 2 | * 3 | * SPDX-License-Identifier: GPL-2.0-or-later 4 | */ 5 | 6 | /** 7 | * @file 8 | * @brief Stand-alone tool to test module "hosts". 9 | * 10 | * This file offers a command line interface to test the functionalities 11 | * of the hosts object. 12 | */ 13 | 14 | #include "../base/hosts.h" /* for gvm_host_type_str, gvm_host_resolve, gvm_... */ 15 | 16 | #include /* for inet_ntop */ 17 | #include /* for g_free */ 18 | #include /* for INET6_ADDRSTRLEN, INET_ADDRSTRLEN, in6_addr */ 19 | #include /* for printf, fprintf, NULL, stderr */ 20 | #include /* for AF_INET, AF_INET6 */ 21 | 22 | static void 23 | print_vhosts (gvm_host_t *host) 24 | { 25 | GSList *tmp = host->vhosts; 26 | while (tmp) 27 | { 28 | gvm_vhost_t *vhost = tmp->data; 29 | printf (" %s [%s]", vhost->value, vhost->source); 30 | tmp = tmp->next; 31 | } 32 | if (host->vhosts) 33 | printf ("\n"); 34 | } 35 | int 36 | main (int argc, char **argv) 37 | { 38 | gvm_hosts_t *hosts; 39 | gvm_host_t *host; 40 | int i; 41 | 42 | if (argc < 2) 43 | return 1; 44 | hosts = gvm_hosts_new (argv[1]); 45 | if (hosts == NULL) 46 | return 1; 47 | if (argv[2]) 48 | { 49 | if (gvm_hosts_exclude (hosts, argv[2]) == -1) 50 | return 2; 51 | } 52 | gvm_hosts_resolve (hosts); 53 | 54 | printf ("Count: %u\n", gvm_hosts_count (hosts)); 55 | printf ("Removed: %u\n", gvm_hosts_removed (hosts)); 56 | 57 | i = 1; 58 | while ((host = gvm_hosts_next (hosts))) 59 | { 60 | char *str; 61 | 62 | gvm_host_add_reverse_lookup (host); 63 | str = gvm_host_value_str (host); 64 | if (gvm_host_type (host) == HOST_TYPE_NAME) 65 | { 66 | char name[INET_ADDRSTRLEN], name6[INET6_ADDRSTRLEN]; 67 | struct in_addr addr; 68 | struct in6_addr addr6; 69 | 70 | if (gvm_host_resolve (host, &addr, AF_INET) == -1) 71 | { 72 | fprintf (stderr, "ERROR - %s: Couldn't resolve IPv4 address.\n", 73 | host->name); 74 | printf ("#%d %s %s\n", i, gvm_host_type_str (host), str); 75 | i++; 76 | g_free (str); 77 | continue; 78 | } 79 | if (inet_ntop (AF_INET, &addr, name, sizeof (name)) == NULL) 80 | { 81 | printf ("inet_ntop() error.\n"); 82 | break; 83 | } 84 | 85 | if (gvm_host_resolve (host, &addr6, AF_INET6) == -1) 86 | { 87 | fprintf (stderr, "ERROR - %s: Couldn't resolve IPv6 address.\n", 88 | host->name); 89 | printf ("#%d %s %s (%s)\n", i, gvm_host_type_str (host), str, 90 | name); 91 | i++; 92 | g_free (str); 93 | continue; 94 | } 95 | if (inet_ntop (AF_INET6, &addr6, name6, sizeof (name6)) == NULL) 96 | { 97 | printf ("inet_ntop() error.\n"); 98 | break; 99 | } 100 | 101 | printf ("#%d %s %s (%s / %s)\n", i, gvm_host_type_str (host), str, 102 | name, name6); 103 | } 104 | else 105 | printf ("#%d %s %s\n", i, gvm_host_type_str (host), str); 106 | print_vhosts (host); 107 | 108 | i++; 109 | g_free (str); 110 | } 111 | 112 | gvm_hosts_free (hosts); 113 | return 0; 114 | } 115 | -------------------------------------------------------------------------------- /util/authutils.h: -------------------------------------------------------------------------------- 1 | /* SPDX-FileCopyrightText: 2009-2023 Greenbone AG 2 | * 3 | * SPDX-License-Identifier: GPL-2.0-or-later 4 | */ 5 | 6 | /** 7 | * @file 8 | * @brief Authentication mechanism(s). 9 | */ 10 | 11 | #ifndef _GVM_AUTHUTILS_H 12 | #define _GVM_AUTHUTILS_H 13 | 14 | #include 15 | 16 | /** 17 | * @brief Numerical representation of the supported authentication methods. 18 | * Beware to have it in sync with \p authentication_methods in 19 | * \ref authutils.c. 20 | */ 21 | enum authentication_method 22 | { 23 | AUTHENTICATION_METHOD_FILE = 0, 24 | AUTHENTICATION_METHOD_LDAP_CONNECT, 25 | AUTHENTICATION_METHOD_RADIUS_CONNECT, 26 | AUTHENTICATION_METHOD_LAST 27 | }; 28 | 29 | /** @brief Type for the numerical representation of the supported 30 | * authentication methods. */ 31 | typedef enum authentication_method auth_method_t; 32 | 33 | const gchar *auth_method_name (auth_method_t); 34 | 35 | int 36 | auth_method_name_valid (const gchar *); 37 | 38 | int 39 | gvm_auth_init (void); 40 | 41 | int 42 | gvm_authenticate_classic (const gchar *, const gchar *, const gchar *); 43 | 44 | gchar * 45 | get_md5_hash_from_string (const gchar *); 46 | 47 | gchar * 48 | get_password_hashes (const gchar *); 49 | 50 | gchar * 51 | digest_hex (int, const guchar *); 52 | 53 | int 54 | gvm_auth_ldap_enabled (void); 55 | 56 | int 57 | gvm_auth_radius_enabled (void); 58 | 59 | #endif /* not _GVM_AUTHUTILS_H */ 60 | -------------------------------------------------------------------------------- /util/compressutils.h: -------------------------------------------------------------------------------- 1 | /* SPDX-FileCopyrightText: 2013-2023 Greenbone AG 2 | * 3 | * SPDX-License-Identifier: GPL-2.0-or-later 4 | */ 5 | 6 | /** 7 | * @file 8 | * @brief API related to data compression (gzip format.) 9 | */ 10 | 11 | #ifndef _GVM_COMPRESSUTILS_H 12 | #define _GVM_COMPRESSUTILS_H 13 | 14 | #include 15 | 16 | void * 17 | gvm_compress (const void *, unsigned long, unsigned long *); 18 | 19 | void * 20 | gvm_compress_gzipheader (const void *, unsigned long, unsigned long *); 21 | 22 | void * 23 | gvm_uncompress (const void *, unsigned long, unsigned long *); 24 | 25 | FILE * 26 | gvm_gzip_open_file_reader (const char *); 27 | 28 | FILE * 29 | gvm_gzip_open_file_reader_fd (int); 30 | 31 | #endif /* not _GVM_COMPRESSUTILS_H */ 32 | -------------------------------------------------------------------------------- /util/compressutils_tests.c: -------------------------------------------------------------------------------- 1 | /* SPDX-FileCopyrightText: 2019-2023 Greenbone AG 2 | * 3 | * SPDX-License-Identifier: GPL-2.0-or-later 4 | */ 5 | 6 | #include "compressutils.c" 7 | 8 | #include 9 | #include 10 | #include 11 | 12 | Describe (compressutils); 13 | BeforeEach (compressutils) 14 | { 15 | } 16 | 17 | AfterEach (compressutils) 18 | { 19 | } 20 | 21 | Ensure (compressutils, can_compress_and_uncompress_without_header) 22 | { 23 | const char *testdata = "TEST-12345-12345-TEST"; 24 | 25 | unsigned long compressed_len = 0; 26 | char *compressed = 27 | gvm_compress (testdata, strlen (testdata) + 1, &compressed_len); 28 | assert_that (compressed_len, is_greater_than (0)); 29 | assert_that (compressed, is_not_null); 30 | assert_that (compressed, is_not_equal_to_string (testdata)); 31 | 32 | unsigned long uncompressed_len; 33 | char *uncompressed = 34 | gvm_uncompress (compressed, compressed_len, &uncompressed_len); 35 | assert_that (uncompressed_len, is_equal_to (strlen (testdata) + 1)); 36 | assert_that (uncompressed, is_equal_to_string (testdata)); 37 | } 38 | 39 | Ensure (compressutils, can_compress_and_uncompress_with_header) 40 | { 41 | const char *testdata = "TEST-12345-12345-TEST"; 42 | 43 | unsigned long compressed_len; 44 | char *compressed = 45 | gvm_compress_gzipheader (testdata, strlen (testdata) + 1, &compressed_len); 46 | assert_that (compressed_len, is_greater_than (0)); 47 | assert_that (compressed, is_not_null); 48 | assert_that (compressed, is_not_equal_to_string (testdata)); 49 | // Check for gzip magic number and deflate compression mode byte 50 | assert_that (compressed[0], is_equal_to ((char) 0x1f)); 51 | assert_that (compressed[1], is_equal_to ((char) 0x8b)); 52 | assert_that (compressed[2], is_equal_to (8)); 53 | 54 | unsigned long uncompressed_len; 55 | char *uncompressed = 56 | gvm_uncompress (compressed, compressed_len, &uncompressed_len); 57 | assert_that (uncompressed_len, is_equal_to (strlen (testdata) + 1)); 58 | assert_that (uncompressed, is_equal_to_string (testdata)); 59 | } 60 | 61 | Ensure (compressutils, can_uncompress_using_reader) 62 | { 63 | const char *testdata = "TEST-12345-12345-TEST"; 64 | unsigned long compressed_len; 65 | char *compressed = 66 | gvm_compress_gzipheader (testdata, strlen (testdata) + 1, &compressed_len); 67 | 68 | char compressed_filename[35] = "/tmp/gvm_gzip_test_XXXXXX"; 69 | int compressed_fd = mkstemp (compressed_filename); 70 | (void) !write (compressed_fd, compressed, compressed_len); 71 | close (compressed_fd); 72 | 73 | FILE *stream = gvm_gzip_open_file_reader (compressed_filename); 74 | assert_that (stream, is_not_null); 75 | 76 | gchar *uncompressed = g_malloc0 (30); 77 | (void) !fread (uncompressed, 1, 30, stream); 78 | assert_that (uncompressed, is_equal_to_string (testdata)); 79 | 80 | assert_that (fclose (stream), is_equal_to (0)); 81 | } 82 | 83 | Ensure (compressutils, can_uncompress_using_fd_reader) 84 | { 85 | const char *testdata = "TEST-12345-12345-TEST"; 86 | unsigned long compressed_len; 87 | char *compressed = 88 | gvm_compress_gzipheader (testdata, strlen (testdata) + 1, &compressed_len); 89 | 90 | char compressed_filename[35] = "/tmp/gvm_gzip_test_XXXXXX"; 91 | int compressed_fd = mkstemp (compressed_filename); 92 | (void) !write (compressed_fd, compressed, compressed_len); 93 | close (compressed_fd); 94 | 95 | compressed_fd = open (compressed_filename, O_RDONLY); 96 | 97 | FILE *stream = gvm_gzip_open_file_reader_fd (compressed_fd); 98 | assert_that (stream, is_not_null); 99 | 100 | gchar *uncompressed = g_malloc0 (30); 101 | (void) !fread (uncompressed, 1, 30, stream); 102 | assert_that (uncompressed, is_equal_to_string (testdata)); 103 | 104 | assert_that (fclose (stream), is_equal_to (0)); 105 | } 106 | 107 | /* Test suite. */ 108 | int 109 | main (int argc, char **argv) 110 | { 111 | TestSuite *suite; 112 | 113 | suite = create_test_suite (); 114 | 115 | add_test_with_context (suite, compressutils, 116 | can_compress_and_uncompress_without_header); 117 | add_test_with_context (suite, compressutils, 118 | can_compress_and_uncompress_with_header); 119 | add_test_with_context (suite, compressutils, can_uncompress_using_reader); 120 | add_test_with_context (suite, compressutils, can_uncompress_using_fd_reader); 121 | 122 | if (argc > 1) 123 | return run_single_test (suite, argv[1], create_text_reporter ()); 124 | 125 | return run_test_suite (suite, create_text_reporter ()); 126 | } 127 | -------------------------------------------------------------------------------- /util/cpeutils.h: -------------------------------------------------------------------------------- 1 | /* SPDX-FileCopyrightText: 2009-2024 Greenbone AG 2 | * 3 | * SPDX-License-Identifier: GPL-2.0-or-later 4 | */ 5 | 6 | /** 7 | * @file 8 | * @brief Headers for CPE utils. 9 | */ 10 | 11 | #ifndef _GVM_CPEUTILS_H 12 | #define _GVM_CPEUTILS_H 13 | 14 | #include 15 | #include 16 | 17 | /** 18 | * @brief XML context. 19 | * 20 | * This structure is used to represent the WFN naming of a CPE. 21 | */ 22 | typedef struct 23 | { 24 | char *part; 25 | char *vendor; 26 | char *product; 27 | char *version; 28 | char *update; 29 | char *edition; 30 | char *sw_edition; 31 | char *target_sw; 32 | char *target_hw; 33 | char *other; 34 | char *language; 35 | } cpe_struct_t; 36 | 37 | char * 38 | uri_cpe_to_fs_cpe (const char *); 39 | 40 | char * 41 | uri_cpe_to_fs_product (const char *); 42 | 43 | char * 44 | uri_cpe_to_uri_product (const char *); 45 | 46 | char * 47 | fs_cpe_to_uri_cpe (const char *); 48 | 49 | char * 50 | fs_cpe_to_uri_product (const char *); 51 | 52 | void 53 | uri_cpe_to_cpe_struct (const char *, cpe_struct_t *); 54 | 55 | char * 56 | cpe_struct_to_uri_cpe (const cpe_struct_t *); 57 | 58 | char * 59 | cpe_struct_to_uri_product (const cpe_struct_t *); 60 | 61 | char * 62 | get_version_from_uri_cpe (const char *); 63 | 64 | void 65 | fs_cpe_to_cpe_struct (const char *, cpe_struct_t *); 66 | 67 | char * 68 | cpe_struct_to_fs_cpe (const cpe_struct_t *); 69 | 70 | char * 71 | cpe_struct_to_fs_product (const cpe_struct_t *); 72 | 73 | void 74 | cpe_struct_init (cpe_struct_t *); 75 | 76 | void 77 | cpe_struct_free (cpe_struct_t *); 78 | 79 | gboolean 80 | cpe_struct_match (cpe_struct_t *, cpe_struct_t *); 81 | 82 | gboolean 83 | cpe_struct_match_tail (cpe_struct_t *, cpe_struct_t *); 84 | 85 | enum set_relation 86 | { 87 | DISJOINT, 88 | EQUAL, 89 | SUBSET, 90 | SUPERSET, 91 | UNDEFINED 92 | }; 93 | 94 | #define CPE_COMPONENT_IS_ANY(component) (component[0] == 'A') 95 | 96 | #endif 97 | -------------------------------------------------------------------------------- /util/fileutils.h: -------------------------------------------------------------------------------- 1 | /* SPDX-FileCopyrightText: 2009-2023 Greenbone AG 2 | * 3 | * SPDX-License-Identifier: GPL-2.0-or-later 4 | */ 5 | 6 | /** 7 | * @file 8 | * @brief Protos for file utility functions. 9 | * 10 | * This file contains the protos for \ref fileutils.c 11 | */ 12 | 13 | #ifndef _GVM_FILEUTILS_H 14 | #define _GVM_FILEUTILS_H 15 | 16 | #include 17 | 18 | int 19 | gvm_file_exists (const char *name); 20 | 21 | int 22 | gvm_file_is_executable (const char *name); 23 | 24 | int 25 | gvm_file_is_readable (const char *name); 26 | 27 | int 28 | gvm_file_check_is_dir (const char *name); 29 | 30 | int 31 | gvm_file_remove_recurse (const gchar *pathname); 32 | 33 | gboolean 34 | gvm_file_copy (const gchar *, const gchar *); 35 | 36 | gboolean 37 | gvm_file_move (const gchar *, const gchar *); 38 | 39 | char * 40 | gvm_file_as_base64 (const char *); 41 | 42 | gchar * 43 | gvm_export_file_name (const char *, const char *, const char *, const char *, 44 | const char *, const char *, const char *, const char *); 45 | 46 | #endif /* not _GVM_FILEUTILS_H */ 47 | -------------------------------------------------------------------------------- /util/gpgmeutils.h: -------------------------------------------------------------------------------- 1 | /* SPDX-FileCopyrightText: 2013-2023 Greenbone AG 2 | * 3 | * SPDX-License-Identifier: GPL-2.0-or-later 4 | */ 5 | 6 | /** 7 | * @file 8 | * @brief Protos and data structures for GPGME utilities. 9 | * 10 | * This file contains the protos for \ref gpgmeutils.c 11 | */ 12 | 13 | #ifndef _GVM_GPGMEUTILS_H 14 | #define _GVM_GPGMEUTILS_H 15 | 16 | #include /* for gchar */ 17 | #include /* for gpgme_ctx_t */ 18 | 19 | void 20 | log_gpgme (GLogLevelFlags, gpg_error_t, const char *, ...); 21 | 22 | gpgme_ctx_t 23 | gvm_init_gpgme_ctx_from_dir (const gchar *); 24 | 25 | int 26 | gvm_gpg_import_many_types_from_string (gpgme_ctx_t, const char *, ssize_t, 27 | GArray *); 28 | 29 | int 30 | gvm_gpg_import_from_string (gpgme_ctx_t, const char *, ssize_t, 31 | gpgme_data_type_t); 32 | 33 | int 34 | gvm_pgp_pubkey_encrypt_stream (FILE *, FILE *, const char *, const char *, 35 | ssize_t); 36 | 37 | int 38 | gvm_smime_encrypt_stream (FILE *, FILE *, const char *, const char *, ssize_t); 39 | 40 | #endif /*_GVM_GPGMEUTILS_H*/ 41 | -------------------------------------------------------------------------------- /util/json.c: -------------------------------------------------------------------------------- 1 | /* SPDX-FileCopyrightText: 2024 Greenbone AG 2 | * 3 | * SPDX-License-Identifier: GPL-2.0-or-later 4 | */ 5 | 6 | #include "json.h" 7 | 8 | /** 9 | * @brief Escapes a string according to the JSON or JSONPath standard 10 | * 11 | * @param[in] string The string to escape 12 | * @param[in] single_quote Whether to escape single quotes 13 | * 14 | * @return The escaped string 15 | */ 16 | gchar * 17 | gvm_json_string_escape (const char *string, gboolean single_quote) 18 | { 19 | gchar *point; 20 | if (string == NULL) 21 | return NULL; 22 | 23 | GString *escaped = g_string_sized_new (strlen (string)); 24 | for (point = (char *) string; *point != 0; point++) 25 | { 26 | unsigned char character = *point; 27 | 28 | if ((character > 31) && (character != '\\') 29 | && (single_quote ? (character != '\'') : (character != '\"'))) 30 | { 31 | g_string_append_c (escaped, character); 32 | } 33 | else 34 | { 35 | g_string_append_c (escaped, '\\'); 36 | switch (*point) 37 | { 38 | case '\\': 39 | case '\'': 40 | case '\"': 41 | g_string_append_c (escaped, *point); 42 | break; 43 | case '\b': 44 | g_string_append_c (escaped, 'b'); 45 | break; 46 | case '\f': 47 | g_string_append_c (escaped, 'f'); 48 | break; 49 | case '\n': 50 | g_string_append_c (escaped, 'n'); 51 | break; 52 | case '\r': 53 | g_string_append_c (escaped, 'r'); 54 | break; 55 | case '\t': 56 | g_string_append_c (escaped, 't'); 57 | break; 58 | default: 59 | g_string_append_printf (escaped, "u%04x", character); 60 | } 61 | } 62 | } 63 | return g_string_free (escaped, FALSE); 64 | } 65 | 66 | /** 67 | * @brief Get a double field from a JSON object. 68 | * 69 | * @param[in] obj Object 70 | * @param[in] key Field name. 71 | * 72 | * @return A double. 73 | */ 74 | double 75 | gvm_json_obj_double (cJSON *obj, const gchar *key) 76 | { 77 | cJSON *item; 78 | 79 | item = cJSON_GetObjectItem (obj, key); 80 | if (item && cJSON_IsNumber (item)) 81 | return item->valuedouble; 82 | 83 | return 0; 84 | } 85 | 86 | /** 87 | * @brief Get an int field from a JSON object. 88 | * 89 | * @param[in] obj Object 90 | * @param[in] key Field name. 91 | * @param[out] val Either NULL or a return location for the int (only set if 92 | * int field exists). 93 | * 94 | * @return 0 if such an int field exists, else 1. 95 | */ 96 | int 97 | gvm_json_obj_check_int (cJSON *obj, const gchar *key, int *val) 98 | { 99 | cJSON *item; 100 | 101 | item = cJSON_GetObjectItem (obj, key); 102 | if (item && cJSON_IsNumber (item)) 103 | { 104 | if (val) 105 | *val = item->valueint; 106 | return 0; 107 | } 108 | return 1; 109 | } 110 | 111 | /** 112 | * @brief Get an int field from a JSON object. 113 | * 114 | * @param[in] obj Object 115 | * @param[in] key Field name. 116 | * 117 | * @return An int. 118 | */ 119 | int 120 | gvm_json_obj_int (cJSON *obj, const gchar *key) 121 | { 122 | cJSON *item; 123 | 124 | item = cJSON_GetObjectItem (obj, key); 125 | if (item && cJSON_IsNumber (item)) 126 | return item->valueint; 127 | 128 | return 0; 129 | } 130 | 131 | /** 132 | * @brief Get a string field from a JSON object. 133 | * 134 | * @param[in] obj Object 135 | * @param[in] key Field name. 136 | * @param[out] val Either NULL or a return location for the string (only set 137 | * if string field exists). Freed by cJSON_Delete. 138 | * 139 | * @return 0 if such a field exists, else 1. 140 | */ 141 | int 142 | gvm_json_obj_check_str (cJSON *obj, const gchar *key, gchar **val) 143 | { 144 | cJSON *item; 145 | 146 | item = cJSON_GetObjectItem (obj, key); 147 | if (item && cJSON_IsString (item)) 148 | { 149 | if (val) 150 | *val = item->valuestring; 151 | return 0; 152 | } 153 | return 1; 154 | } 155 | 156 | /** 157 | * @brief Get a string field from a JSON object. 158 | * 159 | * @param[in] obj Object 160 | * @param[in] key Field name. 161 | * 162 | * @return A string. Will be freed by cJSON_Delete. 163 | */ 164 | gchar * 165 | gvm_json_obj_str (cJSON *obj, const gchar *key) 166 | { 167 | cJSON *item; 168 | 169 | item = cJSON_GetObjectItem (obj, key); 170 | if (item && cJSON_IsString (item)) 171 | return item->valuestring; 172 | 173 | return 0; 174 | } 175 | -------------------------------------------------------------------------------- /util/json.h: -------------------------------------------------------------------------------- 1 | /* SPDX-FileCopyrightText: 2024 Greenbone AG 2 | * 3 | * SPDX-License-Identifier: GPL-2.0-or-later 4 | */ 5 | 6 | #ifndef _GVM_JSON_H 7 | #define _GVM_JSON_H 8 | 9 | #define _GNU_SOURCE 10 | 11 | #include 12 | #include 13 | 14 | gchar * 15 | gvm_json_string_escape (const char *, gboolean); 16 | 17 | double 18 | gvm_json_obj_double (cJSON *, const gchar *); 19 | 20 | int 21 | gvm_json_obj_check_int (cJSON *, const gchar *, int *); 22 | 23 | int 24 | gvm_json_obj_int (cJSON *, const gchar *); 25 | 26 | int 27 | gvm_json_obj_check_str (cJSON *, const gchar *, gchar **); 28 | 29 | gchar * 30 | gvm_json_obj_str (cJSON *, const gchar *); 31 | 32 | #endif /* _GVM_JSON_H */ 33 | -------------------------------------------------------------------------------- /util/jsonpull.h: -------------------------------------------------------------------------------- 1 | /* SPDX-FileCopyrightText: 2024 Greenbone AG 2 | * 3 | * SPDX-License-Identifier: GPL-2.0-or-later 4 | */ 5 | 6 | #ifndef _GVM_JSONPULL_H 7 | #define _GVM_JSONPULL_H 8 | 9 | #define _GNU_SOURCE 10 | 11 | #include "json.h" 12 | 13 | #include 14 | #include 15 | 16 | /** 17 | * @brief Type of container the parser is currently in 18 | */ 19 | typedef enum 20 | { 21 | GVM_JSON_PULL_CONTAINER_NONE = 0, ///< No container / document root 22 | GVM_JSON_PULL_CONTAINER_ARRAY, ///< Array 23 | GVM_JSON_PULL_CONTAINER_OBJECT, ///< Object 24 | } gvm_json_pull_container_type_t; 25 | 26 | /** 27 | * @brief Path element types for the JSON pull parser. 28 | */ 29 | typedef struct gvm_json_path_elem 30 | { 31 | gvm_json_pull_container_type_t parent_type; ///< parent container type 32 | int index; ///< Index of the element within the parent 33 | char *key; ///< Key if element is in an object 34 | int depth; ///< Number of ancestor elements 35 | } gvm_json_path_elem_t; 36 | 37 | /** 38 | * @brief Event types for the JSON pull parser 39 | */ 40 | typedef enum 41 | { 42 | GVM_JSON_PULL_EVENT_UNDEFINED = 0, 43 | GVM_JSON_PULL_EVENT_ARRAY_START, 44 | GVM_JSON_PULL_EVENT_ARRAY_END, 45 | GVM_JSON_PULL_EVENT_OBJECT_START, 46 | GVM_JSON_PULL_EVENT_OBJECT_END, 47 | GVM_JSON_PULL_EVENT_STRING, 48 | GVM_JSON_PULL_EVENT_NUMBER, 49 | GVM_JSON_PULL_EVENT_BOOLEAN, 50 | GVM_JSON_PULL_EVENT_NULL, 51 | GVM_JSON_PULL_EVENT_EOF, 52 | GVM_JSON_PULL_EVENT_ERROR, 53 | } gvm_json_pull_event_type_t; 54 | 55 | /** 56 | * @brief Event generated by the JSON pull parser. 57 | */ 58 | typedef struct 59 | { 60 | gvm_json_pull_event_type_t type; ///< Type of event 61 | GQueue *path; ///< Path to the event value 62 | cJSON *value; ///< Value for non-container value events 63 | gchar *error_message; ///< Error message, NULL on success 64 | } gvm_json_pull_event_t; 65 | 66 | /** 67 | * @brief Expected token state for the JSON pull parser 68 | */ 69 | typedef enum 70 | { 71 | GVM_JSON_PULL_EXPECT_UNDEFINED = 0, ///< Undefined state 72 | GVM_JSON_PULL_EXPECT_VALUE, ///< Expect start of a value 73 | GVM_JSON_PULL_EXPECT_KEY, ///< Expect start of a key 74 | GVM_JSON_PULL_EXPECT_COMMA, ///< Expect comma or container end brace 75 | GVM_JSON_PULL_EXPECT_EOF ///< Expect end of file 76 | } gvm_json_pull_expect_t; 77 | 78 | #define GVM_JSON_PULL_PARSE_BUFFER_LIMIT 10485760 79 | 80 | #define GVM_JSON_PULL_READ_BUFFER_SIZE 4096 81 | 82 | /** 83 | * @brief A json pull parser 84 | */ 85 | typedef struct 86 | { 87 | GQueue *path; ///< Path to the current value 88 | gvm_json_path_elem_t *path_add; ///< Path elem to add in next step 89 | gvm_json_pull_expect_t expect; ///< Current expected token 90 | int keyword_pos; ///< Position in a keyword like "true" or "null" 91 | FILE *input_stream; ///< Input stream 92 | char *read_buffer; ///< Stream reading buffer 93 | size_t read_buffer_size; ///< Size of the stream reading buffer 94 | size_t last_read_size; ///< Size of last stream read 95 | int last_read_char; ///< Character last read from stream 96 | size_t read_pos; ///< Position in current read 97 | GString *parse_buffer; ///< Buffer for parsing values and object keys 98 | size_t parse_buffer_limit; ///< Maximum parse buffer size 99 | } gvm_json_pull_parser_t; 100 | 101 | gvm_json_path_elem_t * 102 | gvm_json_pull_path_elem_new (gvm_json_pull_container_type_t, int); 103 | 104 | void 105 | gvm_json_pull_path_elem_free (gvm_json_path_elem_t *); 106 | 107 | void 108 | gvm_json_pull_event_init (gvm_json_pull_event_t *); 109 | 110 | void 111 | gvm_json_pull_event_cleanup (gvm_json_pull_event_t *); 112 | 113 | void 114 | gvm_json_pull_parser_init_full (gvm_json_pull_parser_t *, FILE *, size_t, 115 | size_t); 116 | 117 | void 118 | gvm_json_pull_parser_init (gvm_json_pull_parser_t *, FILE *); 119 | 120 | void 121 | gvm_json_pull_parser_cleanup (gvm_json_pull_parser_t *); 122 | 123 | void 124 | gvm_json_pull_parser_next (gvm_json_pull_parser_t *, gvm_json_pull_event_t *); 125 | 126 | cJSON * 127 | gvm_json_pull_expand_container (gvm_json_pull_parser_t *, gchar **); 128 | 129 | gchar * 130 | gvm_json_path_to_string (GQueue *path); 131 | 132 | #endif /* _GVM_JSONPULL_H */ 133 | -------------------------------------------------------------------------------- /util/kb_tests.c: -------------------------------------------------------------------------------- 1 | /* SPDX-FileCopyrightText: 2019-2025 Greenbone AG 2 | * 3 | * SPDX-License-Identifier: GPL-2.0-or-later 4 | */ 5 | 6 | #include "kb.c" 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | #define TCP "tcp://" 15 | 16 | Describe (kb); 17 | BeforeEach (kb) 18 | { 19 | } 20 | 21 | AfterEach (kb) 22 | { 23 | } 24 | 25 | /* parse_port_of_addr */ 26 | 27 | Ensure (kb, parse_port_of_addr) 28 | { 29 | char *addr, *port; 30 | 31 | addr = TCP "xxx:5"; 32 | port = parse_port_of_addr (addr, strlen (TCP)); 33 | assert_that (port, is_equal_to_string ("5")); 34 | } 35 | 36 | Ensure (kb, parse_port_of_addr_missing) 37 | { 38 | char *addr, *port; 39 | 40 | addr = TCP "xxx"; 41 | port = parse_port_of_addr (addr, strlen (TCP)); 42 | assert_that (port, is_null); 43 | } 44 | 45 | Ensure (kb, parse_port_of_addr_v6) 46 | { 47 | char *addr, *port; 48 | 49 | addr = TCP "[2001:db8::1]:8080"; 50 | port = parse_port_of_addr (addr, strlen (TCP)); 51 | assert_that (port, is_equal_to_string ("8080")); 52 | } 53 | 54 | /* Test suite. */ 55 | int 56 | main (int argc, char **argv) 57 | { 58 | TestSuite *suite; 59 | 60 | suite = create_test_suite (); 61 | 62 | add_test_with_context (suite, kb, parse_port_of_addr); 63 | add_test_with_context (suite, kb, parse_port_of_addr_missing); 64 | add_test_with_context (suite, kb, parse_port_of_addr_v6); 65 | 66 | if (argc > 1) 67 | return run_single_test (suite, argv[1], create_text_reporter ()); 68 | 69 | return run_test_suite (suite, create_text_reporter ()); 70 | } 71 | -------------------------------------------------------------------------------- /util/ldaputils.h: -------------------------------------------------------------------------------- 1 | /* SPDX-FileCopyrightText: 2012-2023 Greenbone AG 2 | * 3 | * SPDX-License-Identifier: GPL-2.0-or-later 4 | */ 5 | 6 | /** 7 | * @file 8 | * @brief Header for LDAP-Connect Authentication module. 9 | */ 10 | 11 | #ifndef _GVM_LDAPUTILS_H 12 | #define _GVM_LDAPUTILS_H 13 | 14 | #include 15 | 16 | /** @brief Authentication schema and address type. */ 17 | typedef struct ldap_auth_info *ldap_auth_info_t; 18 | 19 | /** 20 | * @brief Schema (dn) and info to use for a basic ldap authentication. 21 | * 22 | * Use like an opaque struct, create with ldap_auth_schema_new, do not modify, 23 | * free with ldap_auth_schema_free. 24 | */ 25 | struct ldap_auth_info 26 | { 27 | gchar *ldap_host; ///< Address of the ldap server, might include port. 28 | gchar *auth_dn; ///< DN to authenticate with. 29 | gboolean allow_plaintext; ///< !Whether or not StartTLS or LDAPS is required. 30 | gboolean ldaps_only; ///< Whether to try LDAPS before StartTLS. 31 | }; 32 | 33 | int 34 | ldap_enable_debug (void); 35 | 36 | int 37 | ldap_connect_authenticate (const gchar *, const gchar *, 38 | /* ldap_auth_info_t */ void *, const gchar *); 39 | 40 | void ldap_auth_info_free (ldap_auth_info_t); 41 | 42 | ldap_auth_info_t 43 | ldap_auth_info_new (const gchar *, const gchar *, gboolean); 44 | 45 | ldap_auth_info_t 46 | ldap_auth_info_new_2 (const gchar *, const gchar *, gboolean, gboolean); 47 | 48 | #ifdef ENABLE_LDAP_AUTH 49 | 50 | #include 51 | 52 | gchar * 53 | ldap_auth_info_auth_dn (const ldap_auth_info_t, const gchar *); 54 | 55 | LDAP * 56 | ldap_auth_bind (const gchar *, const gchar *, const gchar *, gboolean, 57 | const gchar *); 58 | 59 | LDAP * 60 | ldap_auth_bind_2 (const gchar *, const gchar *, const gchar *, gboolean, 61 | const gchar *, gboolean); 62 | 63 | gboolean 64 | ldap_auth_dn_is_good (const gchar *); 65 | 66 | #endif /* ENABLE_LDAP_AUTH */ 67 | 68 | #endif /* not _GVM_LDAPUTILS_H */ 69 | -------------------------------------------------------------------------------- /util/libgvm_util.pc.in: -------------------------------------------------------------------------------- 1 | prefix=@CMAKE_INSTALL_PREFIX@ 2 | exec_prefix=@EXEC_PREFIX@ 3 | libdir=@LIBDIR@ 4 | includedir=@INCLUDEDIR@ 5 | 6 | Name: gvmlibs-util 7 | Description: Greenbone Vulnerability Management Library util 8 | Version: @LIBGVMCONFIG_VERSION@ 9 | Requires.private: glib-2.0 >= 2.42.1, gio-2.0 >= 2.42.1, zlib >= 1.2.8, gnutls >= 3.2.15, uuid >= 2.25.0 10 | Cflags: -I${includedir} -I${includedir}/gvm 11 | Libs: -L${libdir} -lgvm_util 12 | -------------------------------------------------------------------------------- /util/mqtt.h: -------------------------------------------------------------------------------- 1 | /* SPDX-FileCopyrightText: 2021-2023 Greenbone AG 2 | * 3 | * SPDX-License-Identifier: GPL-2.0-or-later 4 | */ 5 | 6 | /** 7 | * @file 8 | * @brief Protos for MQTT handling. 9 | */ 10 | 11 | #ifndef _GVM_MQTT_H 12 | #define _GVM_MQTT_H 13 | 14 | #include 15 | #include 16 | 17 | #define AUTH_MQTT 1 18 | 19 | int 20 | mqtt_init (const char *); 21 | 22 | int 23 | mqtt_init_auth (const char *, const char *, const char *); 24 | 25 | gboolean 26 | mqtt_is_initialized (void); 27 | 28 | void 29 | mqtt_reset (void); 30 | 31 | int 32 | mqtt_publish (const char *, const char *); 33 | 34 | int 35 | mqtt_publish_single_message_auth (const char *, const char *, const char *, 36 | const char *, const char *); 37 | 38 | int 39 | mqtt_publish_single_message (const char *, const char *, const char *); 40 | 41 | int 42 | mqtt_subscribe (const char *); 43 | 44 | int 45 | mqtt_retrieve_message (char **, int *, char **, int *, const unsigned int); 46 | 47 | int 48 | mqtt_unsubscribe (const char *); 49 | 50 | #endif /* _GVM_MQTT_H */ 51 | -------------------------------------------------------------------------------- /util/mqtt_tests.c: -------------------------------------------------------------------------------- 1 | /* SPDX-FileCopyrightText: 2019-2025 Greenbone AG 2 | * 3 | * SPDX-License-Identifier: GPL-2.0-or-later 4 | */ 5 | 6 | #include "mqtt.c" 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | void 15 | MQTTClient_destroy (MQTTClient *client) 16 | { 17 | (void) client; 18 | } 19 | 20 | Describe (mqtt); 21 | BeforeEach (mqtt) 22 | { 23 | } 24 | 25 | AfterEach (mqtt) 26 | { 27 | } 28 | 29 | /* mqtt_client_destroy */ 30 | 31 | Ensure (mqtt, mqtt_client_destroy_nulls_client) 32 | { 33 | MQTTClient client; 34 | mqtt_t *mqtt; 35 | 36 | mqtt = g_malloc0 (sizeof (*mqtt)); 37 | mqtt_set_client_id (mqtt); 38 | client = mqtt_create (mqtt, "address"); 39 | mqtt_set_client (mqtt, client); 40 | assert_that (mqtt->client, is_not_null); 41 | 42 | mqtt_client_destroy (mqtt); 43 | assert_that (mqtt->client, is_null); 44 | 45 | // Cleanup 46 | mqtt_client_data_destroy (&mqtt); 47 | assert_that (mqtt, is_null); 48 | } 49 | 50 | /* Test suite. */ 51 | int 52 | main (int argc, char **argv) 53 | { 54 | TestSuite *suite; 55 | 56 | suite = create_test_suite (); 57 | 58 | add_test_with_context (suite, mqtt, mqtt_client_destroy_nulls_client); 59 | 60 | if (argc > 1) 61 | return run_single_test (suite, argv[1], create_text_reporter ()); 62 | 63 | return run_test_suite (suite, create_text_reporter ()); 64 | } 65 | -------------------------------------------------------------------------------- /util/nvticache.h: -------------------------------------------------------------------------------- 1 | /* SPDX-FileCopyrightText: 2009-2023 Greenbone AG 2 | * 3 | * SPDX-License-Identifier: GPL-2.0-or-later 4 | */ 5 | 6 | /** 7 | * @file 8 | * @brief Protos and data structures for NVT Information Cache. 9 | * 10 | * This file contains the protos for \ref nvticache.c 11 | */ 12 | 13 | #ifndef _GVM_NVTICACHE_H 14 | #define _GVM_NVTICACHE_H 15 | 16 | #include "../base/nvti.h" /* for nvti_t */ 17 | #include "kb.h" /* for kb_t */ 18 | 19 | #include /* for gchar */ 20 | 21 | #ifndef NVTICACHE_STR 22 | #define NVTICACHE_STR "nvticache" 23 | #endif 24 | 25 | int 26 | nvticache_init (const char *, const char *); 27 | 28 | void 29 | nvticache_reset (void); 30 | 31 | kb_t 32 | nvticache_get_kb (void); 33 | 34 | void 35 | nvticache_save (void); 36 | 37 | int 38 | nvticache_initialized (void); 39 | 40 | int 41 | nvticache_check (const gchar *); 42 | 43 | int 44 | nvticache_add (const nvti_t *, const char *); 45 | 46 | char * 47 | nvticache_get_src (const char *); 48 | 49 | char * 50 | nvticache_get_oid (const char *); 51 | 52 | char * 53 | nvticache_get_name (const char *); 54 | 55 | char * 56 | nvticache_get_tags (const char *); 57 | 58 | GSList * 59 | nvticache_get_prefs (const char *); 60 | 61 | char * 62 | nvticache_get_cves (const char *); 63 | 64 | char * 65 | nvticache_get_bids (const char *); 66 | 67 | char * 68 | nvticache_get_xrefs (const char *); 69 | 70 | char * 71 | nvticache_get_family (const char *); 72 | 73 | char * 74 | nvticache_get_filename (const char *); 75 | 76 | char * 77 | nvticache_get_required_keys (const char *); 78 | 79 | char * 80 | nvticache_get_mandatory_keys (const char *); 81 | 82 | char * 83 | nvticache_get_excluded_keys (const char *); 84 | 85 | char * 86 | nvticache_get_required_ports (const char *); 87 | 88 | char * 89 | nvticache_get_required_udp_ports (const char *); 90 | 91 | int 92 | nvticache_get_category (const char *); 93 | 94 | char * 95 | nvticache_get_dependencies (const char *); 96 | 97 | nvti_t * 98 | nvticache_get_nvt (const char *); 99 | 100 | GSList * 101 | nvticache_get_oids (void); 102 | 103 | size_t 104 | nvticache_count (void); 105 | 106 | void 107 | nvticache_delete (const char *); 108 | 109 | char * 110 | nvticache_feed_version (void); 111 | 112 | int 113 | nvticache_check_feed (void); 114 | 115 | #endif /* not _GVM_NVTICACHE_H */ 116 | -------------------------------------------------------------------------------- /util/passwordbasedauthentication.h: -------------------------------------------------------------------------------- 1 | /* SPDX-FileCopyrightText: 2020-2023 Greenbone AG 2 | * 3 | * SPDX-License-Identifier: GPL-2.0-or-later 4 | */ 5 | 6 | #ifndef _GVM_PASSWORDBASEDAUTHENTICATION_H 7 | #define _GVM_PASSWORDBASEDAUTHENTICATION_H 8 | 9 | /* max amount of applied pepper */ 10 | #define MAX_PEPPER_SIZE 4 11 | /* is used when count is 0 on init*/ 12 | #define COUNT_DEFAULT 20000 13 | /* sha512 */ 14 | #define PREFIX_DEFAULT "$6$" 15 | 16 | /** 17 | * 18 | * PBASettings is used by pba_hash to control SALT, HASH function and 19 | * computional costs. 20 | * 21 | * */ 22 | struct PBASettings 23 | { 24 | char pepper[MAX_PEPPER_SIZE]; /* is statically applied to the random salt */ 25 | unsigned int count; /* controls the computational cost of the hash */ 26 | char *prefix; /* controls which hash function will be used */ 27 | }; 28 | /** 29 | * Intitializes PBASettings with given PEPPER, PREFIX, COUNT. 30 | * 31 | * PEPPER_SIZE must be lower or equal MAX_PEPPER_SIZE when PEPPER is set, when 32 | * PEPPER is a NULL pointer, no pepper will be used and PEPPER_SIZE is ignored. 33 | * 34 | * COUNT is set to COUNT_DEFAULT when it is 0, PREFIX is set to PREFIX_DEFAULT 35 | * when prefix is a nullpointer. 36 | * 37 | * Returns a pointer to PBASettings on success or NULL on failure. 38 | * 39 | * */ 40 | struct PBASettings * 41 | pba_init (const char *pepper, unsigned int pepper_size, unsigned int count, 42 | char *prefix); 43 | 44 | /* return values for pba pba_verify_hash */ 45 | enum pba_rc 46 | { 47 | VALID, /* hash and password are correct */ 48 | UPDATE_RECOMMENDED, /* password is correct but in an outdated format*/ 49 | INVALID, /* password is incorrect */ 50 | ERR, /* unexpected error */ 51 | }; 52 | 53 | /** 54 | * pba_hash tries to create a hash based SETTING and PASSWORD. 55 | * Returns a hash on success or a NULL pointer on failure 56 | */ 57 | char * 58 | pba_hash (struct PBASettings *setting, const char *password); 59 | 60 | /** 61 | * pba_verify_hash tries to create hash based on PASSWORD and settings found via 62 | * HASH and compares that with HASH. 63 | * 64 | * Returns VALID if HASH and PASSWORD are correct; 65 | * UPDATE_RECOMMENDED when the HASH and PASSWORD are correct but based on a 66 | * deprecated algorithm; IVALID if HASH does not match PASSWORD; ERR if an 67 | * unexpected error occurs. 68 | */ 69 | enum pba_rc 70 | pba_verify_hash (const struct PBASettings *settings, const char *hash, 71 | const char *password); 72 | 73 | void 74 | pba_finalize (struct PBASettings *settings); 75 | 76 | #endif 77 | -------------------------------------------------------------------------------- /util/passwordbasedauthentication_tests.c: -------------------------------------------------------------------------------- 1 | /* SPDX-FileCopyrightText: 2019-2023 Greenbone AG 2 | * 3 | * SPDX-License-Identifier: GPL-2.0-or-later 4 | */ 5 | 6 | #include "authutils.h" 7 | #include "passwordbasedauthentication.c" 8 | 9 | #include 10 | #include 11 | #include 12 | Describe (PBA); 13 | BeforeEach (PBA) 14 | { 15 | } 16 | AfterEach (PBA) 17 | { 18 | } 19 | 20 | Ensure (PBA, returns_false_on_not_phc_compliant_setting) 21 | { 22 | assert_false (pba_is_phc_compliant ("$")); 23 | assert_false (pba_is_phc_compliant ("password")); 24 | } 25 | Ensure (PBA, returns_true_on_phc_compliant_setting) 26 | { 27 | assert_true (pba_is_phc_compliant ("$password")); 28 | } 29 | Ensure (PBA, returns_NULL_on_unsupport_settings) 30 | { 31 | struct PBASettings setting = {"0000", 20000, "$6$"}; 32 | assert_false (pba_hash (NULL, "*password")); 33 | assert_false (pba_hash (&setting, NULL)); 34 | setting.prefix = "$1$"; 35 | assert_false (pba_hash (&setting, "*password")); 36 | } 37 | Ensure (PBA, unique_hash_without_adding_used_pepper) 38 | { 39 | struct PBASettings setting = {"4242", 20000, "$6$"}; 40 | char *cmp_hash, *hash; 41 | hash = pba_hash (&setting, "*password"); 42 | assert_not_equal (hash, NULL); 43 | assert_false (string_contains (hash, setting.pepper)); 44 | cmp_hash = pba_hash (&setting, "*password"); 45 | assert_string_not_equal (hash, cmp_hash); 46 | free (hash); 47 | free (cmp_hash); 48 | } 49 | Ensure (PBA, verify_hash) 50 | { 51 | struct PBASettings setting = {"4242", 20000, "$6$"}; 52 | char *hash; 53 | hash = pba_hash (&setting, "*password"); 54 | assert_not_equal (hash, NULL); 55 | assert_equal (pba_verify_hash (&setting, hash, "*password"), VALID); 56 | assert_equal (pba_verify_hash (&setting, hash, "*password1"), INVALID); 57 | free (hash); 58 | struct PBASettings setting_wo_pepper = {"\0\0\0\0", 20000, "$6$"}; 59 | hash = pba_hash (&setting_wo_pepper, "*password"); 60 | assert_equal (pba_verify_hash (&setting_wo_pepper, hash, "*password"), VALID); 61 | free (hash); 62 | } 63 | 64 | Ensure (PBA, verify_hash_returns_invalid_on_np_hash_np_password) 65 | { 66 | struct PBASettings setting = {"4242", 20000, "$6$"}; 67 | char *hash; 68 | hash = pba_hash (&setting, "*password"); 69 | assert_not_equal (hash, NULL); 70 | assert_equal (pba_verify_hash (&setting, NULL, "*password"), INVALID); 71 | assert_equal (pba_verify_hash (&setting, hash, NULL), INVALID); 72 | } 73 | 74 | Ensure (PBA, defaults) 75 | { 76 | int i; 77 | struct PBASettings *settings = pba_init (NULL, 0, 0, NULL); 78 | assert_equal (settings->count, 20000); 79 | for (i = 0; i < MAX_PEPPER_SIZE; i++) 80 | assert_equal_with_message (settings->pepper[i], 0, 81 | "init_without_pepper_should_not_have_pepper"); 82 | assert_string_equal (settings->prefix, "$6$"); 83 | pba_finalize (settings); 84 | } 85 | Ensure (PBA, initialization) 86 | { 87 | int i; 88 | struct PBASettings *settings = pba_init ("444", 3, 1, "$6$"); 89 | assert_equal (settings->count, 1); 90 | for (i = 0; i < MAX_PEPPER_SIZE - 1; i++) 91 | assert_equal_with_message (settings->pepper[i], '4', 92 | "init_with_pepper_should_be_set"); 93 | assert_equal_with_message (settings->pepper[MAX_PEPPER_SIZE - 1], '\0', 94 | "last_pepper_should_be_unset_by_pepper_3"); 95 | assert_string_equal (settings->prefix, "$6$"); 96 | pba_finalize (settings); 97 | settings = pba_init ("444", MAX_PEPPER_SIZE + 1, 1, "$6$"); 98 | assert_equal_with_message (settings, NULL, 99 | "should_fail_due_to_too_much_pepper"); 100 | settings = pba_init ("444", MAX_PEPPER_SIZE, 1, "$WALDFEE$"); 101 | assert_equal_with_message (settings, NULL, 102 | "should_fail_due_to_unknown_prefix"); 103 | } 104 | 105 | Ensure (PBA, handle_md5_hash) 106 | { 107 | struct PBASettings *settings = pba_init (NULL, 0, 0, NULL); 108 | char *hash; 109 | assert_equal (gvm_auth_init (), 0); 110 | hash = get_password_hashes ("admin"); 111 | assert_equal (pba_verify_hash (settings, hash, "admin"), UPDATE_RECOMMENDED); 112 | pba_finalize (settings); 113 | } 114 | 115 | int 116 | main (int argc, char **argv) 117 | { 118 | TestSuite *suite; 119 | 120 | suite = create_test_suite (); 121 | 122 | add_test_with_context (suite, PBA, 123 | returns_false_on_not_phc_compliant_setting); 124 | add_test_with_context (suite, PBA, returns_true_on_phc_compliant_setting); 125 | add_test_with_context (suite, PBA, returns_NULL_on_unsupport_settings); 126 | add_test_with_context (suite, PBA, unique_hash_without_adding_used_pepper); 127 | add_test_with_context (suite, PBA, verify_hash); 128 | add_test_with_context (suite, PBA, 129 | verify_hash_returns_invalid_on_np_hash_np_password); 130 | add_test_with_context (suite, PBA, handle_md5_hash); 131 | add_test_with_context (suite, PBA, defaults); 132 | add_test_with_context (suite, PBA, initialization); 133 | if (argc > 1) 134 | return run_single_test (suite, argv[1], create_text_reporter ()); 135 | return run_test_suite (suite, create_text_reporter ()); 136 | } 137 | -------------------------------------------------------------------------------- /util/radiusutils.h: -------------------------------------------------------------------------------- 1 | /* SPDX-FileCopyrightText: 2015-2023 Greenbone AG 2 | * 3 | * SPDX-License-Identifier: GPL-2.0-or-later 4 | */ 5 | 6 | /** 7 | * @file 8 | * @brief Headers of an API for Radius authentication. 9 | */ 10 | 11 | #ifndef _GVM_RADIUSUTILS_H 12 | #define _GVM_RADIUSUTILS_H 13 | 14 | int 15 | radius_authenticate (const char *, const char *, const char *, const char *); 16 | 17 | #endif /* not _GVM_RADIUSUTILS_H */ 18 | -------------------------------------------------------------------------------- /util/radiusutils_tests.c: -------------------------------------------------------------------------------- 1 | /* SPDX-FileCopyrightText: 2019-2025 Greenbone AG 2 | * 3 | * SPDX-License-Identifier: GPL-2.0-or-later 4 | */ 5 | 6 | #include "radiusutils.c" 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | Describe (radiusutils); 15 | BeforeEach (radiusutils) 16 | { 17 | } 18 | 19 | AfterEach (radiusutils) 20 | { 21 | } 22 | 23 | #define HOST "eghost" 24 | #define SECRET "the_secret" 25 | 26 | #ifdef ENABLE_RADIUS_AUTH 27 | 28 | /* radius_init */ 29 | 30 | Ensure (radiusutils, radius_init) 31 | { 32 | rc_handle *rh; 33 | 34 | rh = radius_init (HOST, SECRET); 35 | assert_that (rh, is_not_null); 36 | } 37 | 38 | #else 39 | 40 | /* radius_authenticate */ 41 | 42 | Ensure (radiusutils, radius_authenticate_returns_minus1) 43 | { 44 | assert_that (radius_authenticate ("h", "s", "u", "p"), is_equal_to (-1)); 45 | } 46 | 47 | #endif 48 | 49 | /* Test suite. */ 50 | int 51 | main (int argc, char **argv) 52 | { 53 | TestSuite *suite; 54 | 55 | suite = create_test_suite (); 56 | 57 | #ifdef ENABLE_RADIUS_AUTH 58 | add_test_with_context (suite, radiusutils, radius_init); 59 | #else 60 | add_test_with_context (suite, radiusutils, 61 | radius_authenticate_returns_minus1); 62 | #endif 63 | 64 | if (argc > 1) 65 | return run_single_test (suite, argv[1], create_text_reporter ()); 66 | 67 | return run_test_suite (suite, create_text_reporter ()); 68 | } 69 | -------------------------------------------------------------------------------- /util/serverutils.h: -------------------------------------------------------------------------------- 1 | /* SPDX-FileCopyrightText: 2009-2023 Greenbone AG 2 | * 3 | * SPDX-License-Identifier: GPL-2.0-or-later 4 | */ 5 | 6 | /** 7 | * @file 8 | * @brief GnuTLS based functions for server communication - header file. 9 | * 10 | * This module supplies low-level communication functions for communication 11 | * with a server over GnuTLS. 12 | */ 13 | 14 | #ifndef _GVM_SERVERUTILS_H 15 | #define _GVM_SERVERUTILS_H 16 | 17 | #include /* for gchar, gboolean, gint */ 18 | #include /* for gnutls_session_t, gnutls_certificate_cred... */ 19 | #include /* for va_list */ 20 | #include 21 | #ifdef __FreeBSD__ 22 | #include 23 | #endif 24 | #include 25 | 26 | /** 27 | * @brief Connection. 28 | */ 29 | typedef struct 30 | { 31 | int tls; ///< Whether uses TCP-TLS (vs UNIX socket). 32 | int socket; ///< Socket. 33 | gnutls_session_t session; ///< Session. 34 | gnutls_certificate_credentials_t credentials; ///< Credentials. 35 | gchar *username; ///< Username with which to connect. 36 | gchar *password; ///< Password for user with which to connect. 37 | gchar *host_string; ///< Server host string. 38 | gchar *port_string; ///< Server port string. 39 | gint port; ///< Port of server. 40 | gboolean use_certs; ///< Whether to use certs. 41 | gchar *ca_cert; ///< CA certificate. 42 | gchar *pub_key; ///< The public key. 43 | gchar *priv_key; ///< The private key. 44 | } gvm_connection_t; 45 | 46 | void 47 | gvm_connection_free (gvm_connection_t *); 48 | 49 | void 50 | gvm_connection_close (gvm_connection_t *); 51 | 52 | int gvm_server_verify (gnutls_session_t); 53 | 54 | int 55 | gvm_server_open (gnutls_session_t *, const char *, int); 56 | 57 | int 58 | gvm_server_open_verify (gnutls_session_t *, const char *, int, const char *, 59 | const char *, const char *, int); 60 | 61 | int 62 | gvm_server_open_with_cert (gnutls_session_t *, const char *, int, const char *, 63 | const char *, const char *); 64 | 65 | int 66 | gvm_server_close (int, gnutls_session_t); 67 | 68 | int 69 | gvm_server_attach (int, gnutls_session_t *); 70 | 71 | int 72 | gvm_server_sendf (gnutls_session_t *, const char *, ...) 73 | __attribute__ ((format (printf, 2, 3))); 74 | 75 | int 76 | gvm_server_vsendf (gnutls_session_t *, const char *, va_list); 77 | int 78 | gvm_socket_vsendf (int, const char *, va_list); 79 | 80 | int 81 | gvm_server_sendf_xml (gnutls_session_t *, const char *, ...); 82 | int 83 | gvm_server_sendf_xml_quiet (gnutls_session_t *, const char *, ...); 84 | 85 | int 86 | gvm_connection_sendf_xml (gvm_connection_t *, const char *, ...); 87 | int 88 | gvm_connection_sendf_xml_quiet (gvm_connection_t *, const char *, ...); 89 | 90 | int 91 | gvm_connection_sendf (gvm_connection_t *, const char *, ...); 92 | 93 | int 94 | gvm_server_new (unsigned int, gchar *, gchar *, gchar *, gnutls_session_t *, 95 | gnutls_certificate_credentials_t *); 96 | 97 | int 98 | gvm_server_new_mem (unsigned int, const char *, const char *, const char *, 99 | gnutls_session_t *, gnutls_certificate_credentials_t *); 100 | 101 | int 102 | gvm_server_free (int, gnutls_session_t, gnutls_certificate_credentials_t); 103 | 104 | int gvm_server_session_free (gnutls_session_t, 105 | gnutls_certificate_credentials_t); 106 | 107 | int 108 | load_gnutls_file (const char *, gnutls_datum_t *); 109 | 110 | void 111 | unload_gnutls_file (gnutls_datum_t *); 112 | 113 | int 114 | set_gnutls_dhparams (gnutls_certificate_credentials_t, const char *); 115 | 116 | #endif /* not _GVM_SERVERUTILS_H */ 117 | -------------------------------------------------------------------------------- /util/serverutils_tests.c: -------------------------------------------------------------------------------- 1 | /* SPDX-FileCopyrightText: 2019-2025 Greenbone AG 2 | * 3 | * SPDX-License-Identifier: GPL-2.0-or-later 4 | */ 5 | 6 | #include "serverutils.c" 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | Describe (serverutils); 15 | BeforeEach (serverutils) 16 | { 17 | } 18 | 19 | AfterEach (serverutils) 20 | { 21 | } 22 | 23 | static void 24 | chk (int ret) 25 | { 26 | assert_that (ret, is_equal_to (GNUTLS_E_SUCCESS)); 27 | } 28 | 29 | /* server_new_gnutls_set */ 30 | 31 | Ensure (serverutils, server_new_gnutls_set) 32 | { 33 | int ret; 34 | unsigned len; 35 | gnutls_session_t session; 36 | gnutls_certificate_credentials_t cred; 37 | gnutls_x509_privkey_t pkey; 38 | gnutls_x509_crt_t cert, *certs; 39 | gnutls_datum_t pkey_data, cert_data; 40 | unsigned char serial[] = {0x99, 0x99, 0x99, 0x99}; 41 | 42 | // Setup credential. 43 | 44 | chk (gnutls_certificate_allocate_credentials (&cred)); 45 | 46 | chk (gnutls_x509_privkey_init (&pkey)); 47 | chk (gnutls_x509_privkey_generate (pkey, GNUTLS_PK_RSA, 2048, 0)); 48 | 49 | chk (gnutls_x509_crt_init (&cert)); 50 | chk (gnutls_x509_crt_set_key (cert, pkey)); 51 | 52 | chk (gnutls_x509_crt_set_version (cert, 3)); 53 | chk (gnutls_x509_crt_set_serial (cert, serial, sizeof (serial))); 54 | chk (gnutls_x509_crt_set_dn_by_oid (cert, GNUTLS_OID_X520_COMMON_NAME, 0, 55 | "Eg", strlen ("Eg"))); 56 | chk (gnutls_x509_crt_set_issuer_dn (cert, "CN=Self-Signed Certificate", 0)); 57 | 58 | chk (gnutls_x509_crt_set_activation_time (cert, time (NULL))); 59 | chk (gnutls_x509_crt_set_expiration_time (cert, 60 | time (NULL) + 365 * 24 * 60 * 60)); 61 | 62 | chk (gnutls_x509_crt_sign2 (cert, cert, pkey, GNUTLS_DIG_SHA256, 0)); 63 | 64 | chk (gnutls_init (&session, GNUTLS_CLIENT)); 65 | 66 | chk (gnutls_x509_privkey_export2 (pkey, GNUTLS_X509_FMT_PEM, &pkey_data)); 67 | chk (gnutls_x509_crt_export2 (cert, GNUTLS_X509_FMT_PEM, &cert_data)); 68 | 69 | chk (gnutls_certificate_set_x509_key_mem (cred, &cert_data, &pkey_data, 70 | GNUTLS_X509_FMT_PEM)); 71 | 72 | // Setup session with credential. 73 | 74 | ret = server_new_gnutls_set (GNUTLS_CLIENT, "NORMAL", &session, &cred); 75 | assert_that (ret, is_equal_to (0)); 76 | 77 | // Confirm that credential was set. 78 | 79 | chk (gnutls_certificate_get_x509_crt (cred, 0, &certs, &len)); 80 | assert_that (len, is_equal_to (1)); 81 | assert_that (gnutls_x509_crt_equals (cert, certs[0]), is_true); 82 | 83 | // Clean up. 84 | 85 | gnutls_free (pkey_data.data); 86 | gnutls_free (cert_data.data); 87 | gnutls_x509_crt_deinit (cert); 88 | for (unsigned i = 0; i < len; i++) 89 | gnutls_x509_crt_deinit (certs[i]); 90 | gnutls_free (certs); 91 | gnutls_x509_privkey_deinit (pkey); 92 | gnutls_certificate_free_credentials (cred); 93 | } 94 | 95 | /* Test suite. */ 96 | int 97 | main (int argc, char **argv) 98 | { 99 | TestSuite *suite; 100 | 101 | suite = create_test_suite (); 102 | 103 | add_test_with_context (suite, serverutils, server_new_gnutls_set); 104 | 105 | if (argc > 1) 106 | return run_single_test (suite, argv[1], create_text_reporter ()); 107 | 108 | return run_test_suite (suite, create_text_reporter ()); 109 | } 110 | -------------------------------------------------------------------------------- /util/sshutils.c: -------------------------------------------------------------------------------- 1 | /* SPDX-FileCopyrightText: 2015-2023 Greenbone AG 2 | * 3 | * SPDX-License-Identifier: GPL-2.0-or-later 4 | */ 5 | 6 | /** 7 | * @file 8 | * @brief Implementation of SSH related API. 9 | */ 10 | 11 | #include "sshutils.h" 12 | 13 | #include /* for g_free, g_strdup, g_strdup_printf */ 14 | #include /* for gnutls_datum_t */ 15 | #include /* for gnutls_x509_privkey_deinit, gnutls_x509_p... */ 16 | #include /* for ssh_key_free, ssh_key_type, ssh_key_type_... */ 17 | #include /* for strcmp, strlen */ 18 | 19 | #undef G_LOG_DOMAIN 20 | /** 21 | * @brief GLib logging domain. 22 | */ 23 | #define G_LOG_DOMAIN "libgvm util" 24 | 25 | /** 26 | * @brief Decrypts a base64 encrypted ssh private key. 27 | * 28 | * @param[in] pkcs8_key PKCS#8 encrypted private key. 29 | * @param[in] passphrase Passphrase for the private key. 30 | * 31 | * @return Decrypted private key if success, NULL otherwise. 32 | */ 33 | char * 34 | gvm_ssh_pkcs8_decrypt (const char *pkcs8_key, const char *passphrase) 35 | { 36 | gnutls_datum_t data; 37 | gnutls_x509_privkey_t key; 38 | char buffer[16 * 2048]; 39 | int rc; 40 | size_t size = sizeof (buffer); 41 | 42 | rc = gnutls_x509_privkey_init (&key); 43 | if (rc) 44 | return NULL; 45 | data.size = strlen (pkcs8_key); 46 | data.data = (void *) g_strdup (pkcs8_key); 47 | rc = gnutls_x509_privkey_import_pkcs8 (key, &data, GNUTLS_X509_FMT_PEM, 48 | passphrase ? passphrase : "", 0); 49 | if (rc) 50 | { 51 | gnutls_x509_privkey_deinit (key); 52 | return NULL; 53 | } 54 | g_free (data.data); 55 | rc = gnutls_x509_privkey_export (key, GNUTLS_X509_FMT_PEM, buffer, &size); 56 | gnutls_x509_privkey_deinit (key); 57 | if (rc) 58 | return NULL; 59 | return g_strdup (buffer); 60 | } 61 | 62 | /** 63 | * @brief Exports a base64 encoded public key from a private key and its 64 | * passphrase. 65 | * 66 | * @param[in] private_key Private key to export. 67 | * @param[in] passphrase Passphrase for the private key. 68 | * 69 | * @return Allocated base64 encoded public key if success, NULL otherwise. 70 | */ 71 | char * 72 | gvm_ssh_public_from_private (const char *private_key, const char *passphrase) 73 | { 74 | ssh_key priv; 75 | char *pub_key, *decrypted_priv, *pub_str = NULL; 76 | const char *type; 77 | int ret; 78 | 79 | if (private_key == NULL) 80 | return NULL; 81 | decrypted_priv = gvm_ssh_pkcs8_decrypt (private_key, passphrase); 82 | ret = ssh_pki_import_privkey_base64 (decrypted_priv ? decrypted_priv 83 | : private_key, 84 | passphrase, NULL, NULL, &priv); 85 | g_free (decrypted_priv); 86 | if (ret) 87 | return NULL; 88 | ret = ssh_pki_export_pubkey_base64 (priv, &pub_key); 89 | type = ssh_key_type_to_char (ssh_key_type (priv)); 90 | #if LIBSSH_VERSION_INT >= SSH_VERSION_INT(0, 6, 4) 91 | if (!strcmp (type, "ssh-ecdsa")) 92 | type = ssh_pki_key_ecdsa_name (priv); 93 | #endif 94 | ssh_key_free (priv); 95 | if (ret) 96 | return NULL; 97 | pub_str = g_strdup_printf ("%s %s", type, pub_key); 98 | g_free (pub_key); 99 | return pub_str; 100 | } 101 | -------------------------------------------------------------------------------- /util/sshutils.h: -------------------------------------------------------------------------------- 1 | /* SPDX-FileCopyrightText: 2015-2023 Greenbone AG 2 | * 3 | * SPDX-License-Identifier: GPL-2.0-or-later 4 | */ 5 | 6 | /** 7 | * @file 8 | * @brief SSH related API. 9 | */ 10 | 11 | #ifndef _GVM_SSHUTILS_H 12 | #define _GVM_SSHUTILS_H 13 | 14 | char * 15 | gvm_ssh_pkcs8_decrypt (const char *, const char *); 16 | 17 | char * 18 | gvm_ssh_public_from_private (const char *, const char *); 19 | 20 | #endif /* not _GVM_SSHUTILS_H */ 21 | -------------------------------------------------------------------------------- /util/uuidutils.c: -------------------------------------------------------------------------------- 1 | /* SPDX-FileCopyrightText: 2009-2023 Greenbone AG 2 | * 3 | * SPDX-License-Identifier: GPL-2.0-or-later 4 | */ 5 | 6 | /** 7 | * @file 8 | * @brief UUID creation. 9 | */ 10 | 11 | #include "uuidutils.h" 12 | 13 | #include 14 | #include 15 | #include 16 | 17 | #undef G_LOG_DOMAIN 18 | /** 19 | * @brief GLib logging domain. 20 | */ 21 | #define G_LOG_DOMAIN "libgvm util" 22 | 23 | /** 24 | * @brief Make a new universal identifier. 25 | * 26 | * @return A newly allocated string holding the identifier, which the 27 | * caller must free, or NULL on failure. 28 | */ 29 | char * 30 | gvm_uuid_make (void) 31 | { 32 | char *id; 33 | uuid_t uuid; 34 | 35 | /* Generate an UUID. */ 36 | uuid_generate (uuid); 37 | if (uuid_is_null (uuid) == 1) 38 | { 39 | g_warning ("%s: failed to generate UUID", __func__); 40 | return NULL; 41 | } 42 | 43 | /* Allocate mem for string to hold UUID. */ 44 | id = g_malloc0 (sizeof (char) * 37); 45 | if (id == NULL) 46 | { 47 | g_warning ("%s: Cannot export UUID to text: out of memory", __func__); 48 | return NULL; 49 | } 50 | 51 | /* Export the UUID to text. */ 52 | uuid_unparse (uuid, id); 53 | 54 | return id; 55 | } 56 | -------------------------------------------------------------------------------- /util/uuidutils.h: -------------------------------------------------------------------------------- 1 | /* SPDX-FileCopyrightText: 2009-2023 Greenbone AG 2 | * 3 | * SPDX-License-Identifier: GPL-2.0-or-later 4 | */ 5 | 6 | /** 7 | * @file 8 | * @brief UUID creation. 9 | */ 10 | 11 | #ifndef _GVM_UUIDUTILS_H 12 | #define _GVM_UUIDUTILS_H 13 | 14 | char * 15 | gvm_uuid_make (void); 16 | 17 | #endif /* not _GVM_UUIDUTILS_H */ 18 | -------------------------------------------------------------------------------- /util/versionutils.h: -------------------------------------------------------------------------------- 1 | /* SPDX-FileCopyrightText: 2009-2024 Greenbone AG 2 | * 3 | * SPDX-License-Identifier: GPL-2.0-or-later 4 | */ 5 | 6 | /** 7 | * @file 8 | * @brief Headers for version utils. 9 | */ 10 | 11 | #ifndef _GVM_VERSIONUTILS_H 12 | #define _GVM_VERSIONUTILS_H 13 | 14 | #include 15 | #include 16 | 17 | int 18 | cmp_versions (const char *, const char *); 19 | 20 | #endif 21 | -------------------------------------------------------------------------------- /util/versionutils_tests.c: -------------------------------------------------------------------------------- 1 | /* SPDX-FileCopyrightText: 2019-2023 Greenbone AG 2 | * 3 | * SPDX-License-Identifier: GPL-2.0-or-later 4 | */ 5 | 6 | #include "versionutils.c" 7 | 8 | #include 9 | #include 10 | 11 | Describe (versionutils); 12 | BeforeEach (versionutils) 13 | { 14 | } 15 | 16 | AfterEach (versionutils) 17 | { 18 | } 19 | 20 | /* parse_entity */ 21 | 22 | Ensure (versionutils, cmp_versions) 23 | { 24 | char *version1, *version2; 25 | int result; 26 | 27 | version1 = "test"; 28 | version2 = "test-1"; 29 | result = cmp_versions (version1, version2); 30 | assert_that (result, is_less_than (0)); 31 | assert_that (result, is_greater_than (-5)); 32 | 33 | version1 = "beta-test-2"; 34 | version2 = "test_1"; 35 | result = cmp_versions (version1, version2); 36 | assert_that (result, is_greater_than (0)); 37 | 38 | version1 = "beta-test-2"; 39 | version2 = "test-2.beta"; 40 | result = cmp_versions (version1, version2); 41 | assert_that (result, is_equal_to (0)); 42 | 43 | version1 = "test-2.beta"; 44 | version2 = "test-2.a"; 45 | result = cmp_versions (version1, version2); 46 | assert_that (result, is_equal_to (-5)); 47 | 48 | version1 = "test-2.beta"; 49 | version2 = "test-2.1"; 50 | result = cmp_versions (version1, version2); 51 | assert_that (result, is_equal_to (-1)); 52 | 53 | version1 = "test-2.release_candidate"; 54 | version2 = "test-2"; 55 | result = cmp_versions (version1, version2); 56 | assert_that (result, is_equal_to (-1)); 57 | 58 | version1 = "test-2.release_candidate2"; 59 | version2 = "test-2.release_candidate1"; 60 | result = cmp_versions (version1, version2); 61 | assert_that (result, is_greater_than (0)); 62 | 63 | version1 = "test-2.release_candidatea"; 64 | version2 = "test-2.release_candidateb"; 65 | result = cmp_versions (version1, version2); 66 | assert_that (result, is_equal_to (-5)); 67 | 68 | version1 = "2024-06-24"; 69 | version2 = "2024-06-23"; 70 | result = cmp_versions (version1, version2); 71 | assert_that (result, is_greater_than (0)); 72 | } 73 | 74 | /* Test suite. */ 75 | int 76 | main (int argc, char **argv) 77 | { 78 | TestSuite *suite; 79 | 80 | suite = create_test_suite (); 81 | 82 | add_test_with_context (suite, versionutils, cmp_versions); 83 | 84 | if (argc > 1) 85 | return run_single_test (suite, argv[1], create_text_reporter ()); 86 | 87 | return run_test_suite (suite, create_text_reporter ()); 88 | } 89 | -------------------------------------------------------------------------------- /util/vtparser.h: -------------------------------------------------------------------------------- 1 | /* SPDX-FileCopyrightText: 2024 Greenbone AG 2 | * 3 | * SPDX-License-Identifier: GPL-2.0-or-later 4 | */ 5 | 6 | /** 7 | * @file 8 | * @brief Simple JSON reader. 9 | */ 10 | 11 | #ifndef _GVM_VTPARSER_H 12 | #define _GVM_VTPARSER_H 13 | 14 | #define _GNU_SOURCE /* See feature_test_macros(7) */ 15 | #define _FILE_OFFSET_BITS 64 16 | #include "../base/cvss.h" 17 | #include "../base/nvti.h" /* for nvti_t */ 18 | #include "../util/jsonpull.h" 19 | 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | 26 | /** 27 | * @brief VT categories 28 | */ 29 | typedef enum 30 | { 31 | ACT_INIT = 0, 32 | ACT_SCANNER, 33 | ACT_SETTINGS, 34 | ACT_GATHER_INFO, 35 | ACT_ATTACK, 36 | ACT_MIXED_ATTACK, 37 | ACT_DESTRUCTIVE_ATTACK, 38 | ACT_DENIAL, 39 | ACT_KILL_HOST, 40 | ACT_FLOOD, 41 | ACT_END, 42 | } nvt_category; 43 | 44 | int 45 | parse_vt_json (gvm_json_pull_parser_t *, gvm_json_pull_event_t *, nvti_t **); 46 | 47 | #endif /* _GVM_VTPARSER_H */ -------------------------------------------------------------------------------- /util/xmlutils.h: -------------------------------------------------------------------------------- 1 | /* SPDX-FileCopyrightText: 2009-2023 Greenbone AG 2 | * 3 | * SPDX-License-Identifier: GPL-2.0-or-later 4 | */ 5 | 6 | /** 7 | * @file 8 | * @brief Headers for simple XML reader. 9 | */ 10 | 11 | #ifndef _GVM_XMLUTILS_H 12 | #define _GVM_XMLUTILS_H 13 | 14 | #include "serverutils.h" 15 | 16 | #include 17 | #include 18 | #include 19 | 20 | /** 21 | * @brief XML context. 22 | * 23 | * This structure is used to pass data between XML event handlers and the 24 | * caller of the XML parser. 25 | */ 26 | typedef struct 27 | { 28 | GSList *first; ///< The very first entity. 29 | GSList *current; ///< The element currently being parsed. 30 | gboolean done; ///< Flag which is true when the first element is closed. 31 | } context_data_t; 32 | 33 | void 34 | xml_handle_start_element (context_data_t *, const gchar *, const gchar **, 35 | const gchar **); 36 | 37 | void 38 | xml_handle_end_element (context_data_t *, const gchar *); 39 | 40 | void 41 | xml_handle_text (context_data_t *, const gchar *, gsize); 42 | 43 | /** 44 | * @brief Entities. 45 | */ 46 | typedef GSList *entities_t; 47 | 48 | /** 49 | * @brief XML element. 50 | */ 51 | struct entity_s 52 | { 53 | char *name; ///< Name. 54 | char *text; ///< Text. 55 | GHashTable *attributes; ///< Attributes. 56 | entities_t entities; ///< Children. 57 | }; 58 | typedef struct entity_s *entity_t; 59 | 60 | /** 61 | * @brief Data for xml search functions. 62 | */ 63 | typedef struct 64 | { 65 | int found; /**< Founded.*/ 66 | int done; /**< Done. */ 67 | gchar *find_element; /**< Element to be find. */ 68 | GHashTable *find_attributes; /**< Attributes to find. */ 69 | } xml_search_data_t; 70 | 71 | entities_t next_entities (entities_t); 72 | 73 | entity_t first_entity (entities_t); 74 | 75 | entity_t 76 | add_entity (entities_t *, const char *, const char *); 77 | 78 | int compare_entities (entity_t, entity_t); 79 | 80 | entity_t 81 | entity_child (entity_t, const char *); 82 | 83 | const char * 84 | entity_attribute (entity_t, const char *); 85 | 86 | char * 87 | entity_name (entity_t entity); 88 | 89 | char * 90 | entity_text (entity_t entity); 91 | 92 | void free_entity (entity_t); 93 | 94 | void 95 | print_entity (FILE *, entity_t); 96 | 97 | void 98 | print_entity_format (entity_t, gpointer indentation); 99 | 100 | int 101 | try_read_entity_and_string (gnutls_session_t *, int, entity_t *, GString **); 102 | 103 | int 104 | read_entity_and_string (gnutls_session_t *, entity_t *, GString **); 105 | 106 | int 107 | read_entity_and_string_c (gvm_connection_t *, entity_t *, GString **); 108 | 109 | int 110 | read_entity_and_text (gnutls_session_t *, entity_t *, char **); 111 | 112 | int 113 | read_entity_and_text_c (gvm_connection_t *, entity_t *, char **); 114 | 115 | int 116 | try_read_entity (gnutls_session_t *, int, entity_t *); 117 | 118 | int 119 | try_read_entity_c (gvm_connection_t *, int, entity_t *); 120 | 121 | int 122 | read_entity (gnutls_session_t *, entity_t *); 123 | 124 | int 125 | read_entity_s (int, entity_t *); 126 | 127 | int 128 | read_entity_c (gvm_connection_t *, entity_t *); 129 | 130 | int 131 | read_string (gnutls_session_t *, GString **); 132 | 133 | int 134 | read_string_c (gvm_connection_t *, GString **); 135 | 136 | int 137 | read_text_c (gvm_connection_t *, char **); 138 | 139 | int 140 | parse_entity (const char *, entity_t *); 141 | 142 | void 143 | print_entity_to_string (entity_t entity, GString *string); 144 | 145 | int xml_count_entities (entities_t); 146 | 147 | void 148 | xml_string_append (GString *, const char *, ...); 149 | 150 | /* XML file utilities */ 151 | 152 | int 153 | find_element_in_xml_file (gchar *, gchar *, GHashTable *); 154 | 155 | /* The new faster parser that uses libxml2. */ 156 | 157 | typedef struct _xmlNode *element_t; 158 | 159 | int 160 | parse_element (const gchar *, element_t *); 161 | 162 | void element_free (element_t); 163 | 164 | const gchar *element_name (element_t); 165 | 166 | gchar * 167 | element_attribute (element_t, const gchar *); 168 | 169 | gchar *element_text (element_t); 170 | 171 | element_t 172 | element_child (element_t, const gchar *); 173 | 174 | element_t element_first_child (element_t); 175 | 176 | element_t element_next (element_t); 177 | 178 | gchar * 179 | element_to_string (element_t element); 180 | 181 | void 182 | print_element_to_string (element_t element, GString *string); 183 | 184 | /* XML file iterator 185 | * for reading subelements from large files without building the whole DOM 186 | */ 187 | 188 | typedef struct xml_file_iterator_struct *xml_file_iterator_t; 189 | 190 | xml_file_iterator_t 191 | xml_file_iterator_new (void); 192 | 193 | int 194 | xml_file_iterator_init_from_file_path (xml_file_iterator_t, const char *, int); 195 | 196 | void xml_file_iterator_free (xml_file_iterator_t); 197 | 198 | int xml_file_iterator_rewind (xml_file_iterator_t); 199 | 200 | element_t 201 | xml_file_iterator_next (xml_file_iterator_t, gchar **); 202 | 203 | #endif /* not _GVM_XMLUTILS_H */ 204 | --------------------------------------------------------------------------------