| 1 | /** |
| 2 | * @file dpkginfo_probe.c |
| 3 | * @brief dpkginfo probe |
| 4 | * @author "Pierre Chifflier" <[email protected]> |
| 5 | */ |
| 6 | |
| 7 | /* |
| 8 | * Copyright 2009 Red Hat Inc., Durham, North Carolina. |
| 9 | * All Rights Reserved. |
| 10 | * |
| 11 | * This library is free software; you can redistribute it and/or |
| 12 | * modify it under the terms of the GNU Lesser General Public |
| 13 | * License as published by the Free Software Foundation; either |
| 14 | * version 2.1 of the License, or (at your option) any later version. |
| 15 | * |
| 16 | * This library is distributed in the hope that it will be useful, |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 19 | * Lesser General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU Lesser General Public |
| 22 | * License along with this library; if not, write to the Free Software |
| 23 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 24 | * |
| 25 | * Authors: |
| 26 | * "Pierre Chifflier <[email protected]>" |
| 27 | */ |
| 28 | |
| 29 | |
| 30 | /* |
| 31 | * dpkginfo probe: |
| 32 | * |
| 33 | * dpkginfo_object(string name) |
| 34 | * |
| 35 | * dpkginfo_state(string name, |
| 36 | * string arch, |
| 37 | * string epoch, |
| 38 | * string release, |
| 39 | * string version, |
| 40 | * string evr, |
| 41 | * string signature_keyid) |
| 42 | */ |
| 43 | |
| 44 | #ifdef HAVE_CONFIG_H |
| 45 | #include <config.h> |
| 46 | #endif |
| 47 | |
| 48 | #include <stdio.h> |
| 49 | #include <string.h> |
| 50 | #include <errno.h> |
| 51 | #include <assert.h> |
| 52 | |
| 53 | /* SEAP */ |
| 54 | #include "_seap.h" |
| 55 | #include <probe-api.h> |
| 56 | |
| 57 | #include "common/debug_priv.h" |
| 58 | #include "public/oval_schema_version.h" |
| 59 | |
| 60 | #include <probe/probe.h> |
| 61 | |
| 62 | #include "dpkginfo-helper.h" |
| 63 | |
| 64 | #include "dpkginfo_probe.h" |
| 65 | |
| 66 | int dpkginfo_probe_offline_mode_supported(void) { |
| 67 | return PROBE_OFFLINE_OWN; |
| 68 | } |
| 69 | |
| 70 | int dpkginfo_probe_main (probe_ctx *ctx, void *arg) |
| 71 | { |
| 72 | SEXP_t *val, *item, *ent, *obj; |
| 73 | char *request_st = NULL; |
| 74 | struct dpkginfo_reply_t *dpkginfo_reply = NULL; |
| 75 | int errflag; |
| 76 | |
| 77 | obj = probe_ctx_getobject(ctx); |
| 78 | ent = probe_obj_getent(obj, "name", 1); |
| 79 | |
| 80 | if (ent == NULL) { |
| 81 | return (PROBE_ENOENT); |
| 82 | } |
| 83 | |
| 84 | val = probe_ent_getval (ent); |
| 85 | |
| 86 | if (val == NULL) { |
| 87 | dD("%s: no value", "name"); |
| 88 | SEXP_free (ent); |
| 89 | return (PROBE_ENOVAL); |
| 90 | } |
| 91 | |
| 92 | request_st = SEXP_string_cstr (val); |
| 93 | SEXP_free (val); |
| 94 | |
| 95 | if (request_st == NULL) { |
| 96 | switch (errno) { |
| 97 | case EINVAL: |
| 98 | dD("%s: invalid value type", "name"); |
| 99 | SEXP_free (ent); |
| 100 | return PROBE_EINVAL; |
| 101 | break; |
| 102 | case EFAULT: |
| 103 | dD("%s: element not found", "name"); |
| 104 | SEXP_free (ent); |
| 105 | return PROBE_ENOELM; |
| 106 | break; |
| 107 | default: |
| 108 | SEXP_free (ent); |
| 109 | return PROBE_EUNKNOWN; |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | /* get info from debian apt cache */ |
| 114 | dpkginfo_reply = dpkginfo_get_by_name(request_st, &errflag); |
| 115 | |
| 116 | if (dpkginfo_reply == NULL) { |
| 117 | switch (errflag) { |
| 118 | case 0: /* Not found */ |
| 119 | { |
| 120 | dD("Package \"%s\" not found.", request_st); |
| 121 | break; |
| 122 | } |
| 123 | case -1: /* Error */ |
| 124 | { |
| 125 | dD("dpkginfo_get_by_name failed."); |
| 126 | item = probe_item_create(OVAL_LINUX_DPKG_INFO, NULL, |
| 127 | "name", OVAL_DATATYPE_STRING, request_st, |
| 128 | NULL); |
| 129 | probe_item_setstatus (item, SYSCHAR_STATUS_ERROR); |
| 130 | probe_item_collect(ctx, item); |
| 131 | break; |
| 132 | } |
| 133 | } |
| 134 | } else { /* Ok */ |
| 135 | int i; |
| 136 | int num_items = 1; /* FIXME */ |
| 137 | oval_datatype_t evr_string_type; |
| 138 | oval_schema_version_t oval_version = probe_obj_get_platform_schema_version(obj); |
| 139 | if (oval_schema_version_cmp(oval_version, OVAL_SCHEMA_VERSION(5.11.1)) >= 0) { |
| 140 | evr_string_type = OVAL_DATATYPE_DEBIAN_EVR_STRING; |
| 141 | } else { |
| 142 | evr_string_type = OVAL_DATATYPE_EVR_STRING; |
| 143 | } |
| 144 | |
| 145 | for (i = 0; i < num_items; ++i) { |
| 146 | dD("%s: element found version %s", dpkginfo_reply->name, dpkginfo_reply->evr); |
| 147 | item = probe_item_create (OVAL_LINUX_DPKG_INFO, NULL, |
| 148 | "name", OVAL_DATATYPE_STRING, dpkginfo_reply->name, |
| 149 | "arch", OVAL_DATATYPE_STRING, dpkginfo_reply->arch, |
| 150 | "epoch", OVAL_DATATYPE_STRING, dpkginfo_reply->epoch, |
| 151 | "release", OVAL_DATATYPE_STRING, dpkginfo_reply->release, |
| 152 | "version", OVAL_DATATYPE_STRING, dpkginfo_reply->version, |
| 153 | "evr", evr_string_type, dpkginfo_reply->evr, |
| 154 | NULL); |
| 155 | |
| 156 | probe_item_collect(ctx, item); |
| 157 | |
| 158 | dpkginfo_free_reply(dpkginfo_reply); |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | SEXP_free(ent); |
| 163 | free(request_st); |
| 164 | |
| 165 | return (0); |
| 166 | } |