| 1 | /** |
| 2 | * @file oval_sysInterface.c |
| 3 | * \brief Open Vulnerability and Assessment Language |
| 4 | * |
| 5 | * See more details at http://oval.mitre.org/ |
| 6 | */ |
| 7 | |
| 8 | /* |
| 9 | * Copyright 2009--2013 Red Hat Inc., Durham, North Carolina. |
| 10 | * All Rights Reserved. |
| 11 | * |
| 12 | * This library is free software; you can redistribute it and/or |
| 13 | * modify it under the terms of the GNU Lesser General Public |
| 14 | * License as published by the Free Software Foundation; either |
| 15 | * version 2.1 of the License, or (at your option) any later version. |
| 16 | * |
| 17 | * This library is distributed in the hope that it will be useful, |
| 18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 20 | * Lesser General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU Lesser General Public |
| 23 | * License along with this library; if not, write to the Free Software |
| 24 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 25 | * |
| 26 | * Authors: |
| 27 | * "David Niemoller" <[email protected]> |
| 28 | */ |
| 29 | |
| 30 | #ifdef HAVE_CONFIG_H |
| 31 | #include <config.h> |
| 32 | #endif |
| 33 | |
| 34 | #include <stdlib.h> |
| 35 | #include <stdio.h> |
| 36 | #include <string.h> |
| 37 | |
| 38 | #include "oval_system_characteristics_impl.h" |
| 39 | #include "adt/oval_collection_impl.h" |
| 40 | #include "oval_agent_api_impl.h" |
| 41 | #include "common/util.h" |
| 42 | #include "common/debug_priv.h" |
| 43 | #include "common/elements.h" |
| 44 | |
| 45 | typedef struct oval_sysint { |
| 46 | struct oval_syschar_model *model; |
| 47 | char *name; |
| 48 | char *ipAddress; |
| 49 | char *macAddress; |
| 50 | } oval_sysint_t; |
| 51 | |
| 52 | bool oval_sysint_iterator_has_more(struct oval_sysint_iterator *oc_sysint) |
| 53 | { |
| 54 | return oval_collection_iterator_has_more((struct oval_iterator *) |
| 55 | oc_sysint); |
| 56 | } |
| 57 | |
| 58 | struct oval_sysint *oval_sysint_iterator_next(struct oval_sysint_iterator *oc_sysint) |
| 59 | { |
| 60 | return (struct oval_sysint *) |
| 61 | oval_collection_iterator_next((struct oval_iterator *)oc_sysint); |
| 62 | } |
| 63 | |
| 64 | void oval_sysint_iterator_free(struct oval_sysint_iterator *oc_sysint) |
| 65 | { |
| 66 | oval_collection_iterator_free((struct oval_iterator *)oc_sysint); |
| 67 | } |
| 68 | |
| 69 | char *oval_sysint_get_name(struct oval_sysint *sysint) |
| 70 | { |
| 71 | __attribute__nonnull__(sysint); |
| 72 | |
| 73 | return sysint->name; |
| 74 | } |
| 75 | |
| 76 | void oval_sysint_set_name(struct oval_sysint *sysint, char *name) |
| 77 | { |
| 78 | __attribute__nonnull__(sysint); |
| 79 | if (sysint->name != NULL) |
| 80 | free(sysint->name); |
| 81 | sysint->name = oscap_strdup(name); |
| 82 | } |
| 83 | |
| 84 | char *oval_sysint_get_ip_address(struct oval_sysint *sysint) |
| 85 | { |
| 86 | __attribute__nonnull__(sysint); |
| 87 | |
| 88 | return ((struct oval_sysint *)sysint)->ipAddress; |
| 89 | } |
| 90 | |
| 91 | void oval_sysint_set_ip_address(struct oval_sysint *sysint, char *ip_address) |
| 92 | { |
| 93 | __attribute__nonnull__(sysint); |
| 94 | if (sysint->ipAddress != NULL) |
| 95 | free(sysint->ipAddress); |
| 96 | sysint->ipAddress = oscap_strdup(ip_address); |
| 97 | } |
| 98 | |
| 99 | char *oval_sysint_get_mac_address(struct oval_sysint *sysint) |
| 100 | { |
| 101 | return ((struct oval_sysint *)sysint)->macAddress; |
| 102 | } |
| 103 | |
| 104 | void oval_sysint_set_mac_address(struct oval_sysint *sysint, char *mac_address) |
| 105 | { |
| 106 | __attribute__nonnull__(sysint); |
| 107 | if (sysint->macAddress != NULL) |
| 108 | free(sysint->macAddress); |
| 109 | sysint->macAddress = oscap_strdup(mac_address); |
| 110 | } |
| 111 | |
| 112 | struct oval_sysint *oval_sysint_new(struct oval_syschar_model *model) |
| 113 | { |
| 114 | oval_sysint_t *sysint = (oval_sysint_t *) malloc(sizeof(oval_sysint_t)); |
| 115 | if (sysint == NULL) |
| 116 | return NULL; |
| 117 | |
| 118 | sysint->ipAddress = NULL; |
| 119 | sysint->macAddress = NULL; |
| 120 | sysint->name = NULL; |
| 121 | sysint->model = model; |
| 122 | return sysint; |
| 123 | } |
| 124 | |
| 125 | struct oval_sysint *oval_sysint_clone(struct oval_syschar_model *new_model, struct oval_sysint *old_sysint) |
| 126 | { |
| 127 | struct oval_sysint *new_sysint = oval_sysint_new(new_model); |
| 128 | char *ip_address = oval_sysint_get_ip_address(old_sysint); |
| 129 | if (ip_address) { |
| 130 | oval_sysint_set_ip_address(new_sysint, ip_address); |
| 131 | } |
| 132 | char *mac_address = oval_sysint_get_mac_address(old_sysint); |
| 133 | if (mac_address) { |
| 134 | oval_sysint_set_mac_address(new_sysint, mac_address); |
| 135 | } |
| 136 | char *name = oval_sysint_get_name(old_sysint); |
| 137 | if (name) { |
| 138 | oval_sysint_set_name(new_sysint, name); |
| 139 | } |
| 140 | return new_sysint; |
| 141 | } |
| 142 | |
| 143 | void oval_sysint_free(struct oval_sysint *sysint) |
| 144 | { |
| 145 | if (sysint == NULL) |
| 146 | return; |
| 147 | |
| 148 | if (sysint->ipAddress != NULL) |
| 149 | free(sysint->ipAddress); |
| 150 | if (sysint->macAddress != NULL) |
| 151 | free(sysint->macAddress); |
| 152 | if (sysint->name != NULL) |
| 153 | free(sysint->name); |
| 154 | |
| 155 | sysint->ipAddress = NULL; |
| 156 | sysint->macAddress = NULL; |
| 157 | sysint->name = NULL; |
| 158 | |
| 159 | free(sysint); |
| 160 | } |
| 161 | |
| 162 | static void oval_consume_interface_name(char *text, void *sysint) |
| 163 | { |
| 164 | oval_sysint_set_name(sysint, text); |
| 165 | } |
| 166 | |
| 167 | static void oval_consume_ip_address(char *text, void *sysint) |
| 168 | { |
| 169 | oval_sysint_set_ip_address(sysint, text); |
| 170 | } |
| 171 | |
| 172 | static void oval_consume_mac_address(char *text, void *sysint) |
| 173 | { |
| 174 | oval_sysint_set_mac_address(sysint, text); |
| 175 | } |
| 176 | |
| 177 | static int _oval_sysint_parse_tag(xmlTextReaderPtr reader, struct oval_parser_context *context, void *user) |
| 178 | { |
| 179 | struct oval_sysint *sysint = (struct oval_sysint *)user; |
| 180 | char *tagname = (char *)xmlTextReaderLocalName(reader); |
| 181 | char *namespace = (char *)xmlTextReaderNamespaceUri(reader); |
| 182 | int return_code = 0; |
| 183 | |
| 184 | int is_ovalsys = 0; |
| 185 | if (namespace != NULL) { |
| 186 | is_ovalsys = strcmp((const char *)OVAL_SYSCHAR_NAMESPACE, namespace) == 0; |
| 187 | } |
| 188 | if (is_ovalsys && (strcmp(tagname, "interface_name") == 0)) { |
| 189 | return_code = oscap_parser_text_value(reader, &oval_consume_interface_name, sysint); |
| 190 | } else if (is_ovalsys && (strcmp(tagname, "ip_address") == 0)) { |
| 191 | return_code = oscap_parser_text_value(reader, &oval_consume_ip_address, sysint); |
| 192 | } else if (is_ovalsys && (strcmp(tagname, "mac_address") == 0)) { |
| 193 | return_code = oscap_parser_text_value(reader, &oval_consume_mac_address, sysint); |
| 194 | } else { |
| 195 | dW("Skipping tag: <%s:%s>.", namespace, tagname); |
| 196 | oval_parser_skip_tag(reader, context); |
| 197 | } |
| 198 | |
| 199 | free(tagname); |
| 200 | free(namespace); |
| 201 | |
| 202 | return return_code; |
| 203 | } |
| 204 | |
| 205 | int oval_sysint_parse_tag(xmlTextReaderPtr reader, |
| 206 | struct oval_parser_context *context, oval_sysint_consumer consumer, void *user) |
| 207 | { |
| 208 | __attribute__nonnull__(context); |
| 209 | |
| 210 | struct oval_sysint *sysint = oval_sysint_new(context->syschar_model); |
| 211 | char *tagname = (char *)xmlTextReaderLocalName(reader); |
| 212 | char *namespace = (char *)xmlTextReaderNamespaceUri(reader); |
| 213 | int return_code = 0; |
| 214 | |
| 215 | int is_ovalsys = 0; |
| 216 | if (namespace != NULL) { |
| 217 | is_ovalsys = strcmp((const char *)OVAL_SYSCHAR_NAMESPACE, namespace) == 0; |
| 218 | } |
| 219 | if (is_ovalsys && (strcmp(tagname, "interface") == 0)) { |
| 220 | return_code = oval_parser_parse_tag(reader, context, &_oval_sysint_parse_tag, sysint); |
| 221 | } else { |
| 222 | dW("Skipping tag: <%s:%s>.", namespace, tagname); |
| 223 | oval_parser_skip_tag(reader, context); |
| 224 | } |
| 225 | |
| 226 | (*consumer) (sysint, user); |
| 227 | |
| 228 | free(tagname); |
| 229 | free(namespace); |
| 230 | |
| 231 | return return_code; |
| 232 | } |
| 233 | |
| 234 | void oval_sysint_to_dom(struct oval_sysint *sysint, xmlDoc * doc, xmlNode * tag_parent) |
| 235 | { |
| 236 | if (sysint) { |
| 237 | xmlNs *ns_syschar = xmlSearchNsByHref(doc, tag_parent, OVAL_SYSCHAR_NAMESPACE); |
| 238 | xmlNode *tag_sysint = xmlNewTextChild(tag_parent, ns_syschar, BAD_CAST "interface", NULL); |
| 239 | xmlNewTextChild(tag_sysint, ns_syschar, BAD_CAST "interface_name", BAD_CAST oval_sysint_get_name(sysint)); |
| 240 | xmlNewTextChild(tag_sysint, ns_syschar, BAD_CAST "ip_address", BAD_CAST oval_sysint_get_ip_address(sysint)); |
| 241 | xmlNewTextChild |
| 242 | (tag_sysint, ns_syschar, BAD_CAST "mac_address", BAD_CAST oval_sysint_get_mac_address(sysint)); |
| 243 | } |
| 244 | } |