| 1 | /** |
| 2 | * @file xinetd_probe.c |
| 3 | * @brief xinetd probe |
| 4 | * @author "Daniel Kopecek" <[email protected]> |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | /* |
| 9 | * Copyright 2010 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 | * Daniel Kopecek <[email protected]> |
| 28 | */ |
| 29 | |
| 30 | #ifdef HAVE_CONFIG_H |
| 31 | #include <config.h> |
| 32 | #endif |
| 33 | |
| 34 | #include <probe-api.h> |
| 35 | #include <probe/probe.h> |
| 36 | #include <probe/entcmp.h> |
| 37 | #include <probe/option.h> |
| 38 | #include <errno.h> |
| 39 | #include <unistd.h> |
| 40 | #include <stdlib.h> |
| 41 | #include <ctype.h> |
| 42 | #include <string.h> |
| 43 | #include <stddef.h> |
| 44 | #include <stdbool.h> |
| 45 | #include <stdint.h> |
| 46 | #include <limits.h> |
| 47 | #include <fcntl.h> |
| 48 | #include <sys/types.h> |
| 49 | #include <sys/stat.h> |
| 50 | #include <dirent.h> |
| 51 | #include <fnmatch.h> |
| 52 | #include <bfind.h> |
| 53 | #include <common/debug_priv.h> |
| 54 | #include <netdb.h> |
| 55 | |
| 56 | #if defined(OS_FREEBSD) |
| 57 | #include <arpa/inet.h> |
| 58 | #endif |
| 59 | |
| 60 | #include "../SEAP/generic/rbt/rbt.h" |
| 61 | #include "xinetd_probe.h" |
| 62 | |
| 63 | #define PATH_SEPARATOR '/' |
| 64 | |
| 65 | /* |
| 66 | * the code bellow implements a minimal xinetd configuration parser |
| 67 | */ |
| 68 | |
| 69 | #define XICFG_PARSER_IGNORE_UNKNOWN 1 |
| 70 | #define XICFG_PARSER_IGNORE_INVSECT 0 |
| 71 | #define XICFG_PARSER_MAXFILESIZE 655360 /**< hard limit for configuration file size; 640K ought to be enough for anybody */ |
| 72 | #define XICFG_PARSER_MAXFILECOUNT 128 /**< a reasonable high limit for the number of configuration files to be opened at one time */ |
| 73 | |
| 74 | struct xiconf_attr { |
| 75 | char *name; /* name of the attribute */ |
| 76 | size_t offset; /* offset of the corresponding member in the xinetd_service_t structure */ |
| 77 | int (*op_assign)(void *, char *); /* = */ |
| 78 | int (*op_insert)(void *, char *); /* += */ |
| 79 | int (*op_remove)(void *, char *); /* -= */ |
| 80 | int (*op_coerce)(void *, int); /* coerce to another type */ |
| 81 | int (*op_merge) (void *, void *, int); /* merge two values */ |
| 82 | int sections; /* which type of sections are valid */ |
| 83 | int pass_arg; |
| 84 | }; |
| 85 | |
| 86 | #define XIATTR_SECTION_NIL 0x01 |
| 87 | #define XIATTR_SECTION_DEF 0x02 |
| 88 | #define XIATTR_SECTION_SRV 0x04 |
| 89 | |
| 90 | #define XIATTR_OPARG_LOCAL 0 /* local service attribute */ |
| 91 | #define XIATTR_OPARG_GLOBAL 1 /* xiconf_t structure */ |
| 92 | |
| 93 | #define XIATTR_MERGE_DEFAULTS 1 |
| 94 | |
| 95 | #define XICONF_SECTION_DEFAULTS XIATTR_SECTION_DEF |
| 96 | #define XICONF_SECTION_SERVICE XIATTR_SECTION_SRV |
| 97 | |
| 98 | #define OPRES_SUCCESS 0 |
| 99 | #define OPRES_ESYNTAX 1 /* Invalid syntax */ |
| 100 | #define OPRES_EINVAL 2 /* Invalid operation */ |
| 101 | #define OPRES_EFAULT 3 /* Invalid memory access */ |
| 102 | #define OPRES_ELIMIT 4 /* Limit reached */ |
| 103 | |
| 104 | typedef struct xiconf_service { |
| 105 | /* |
| 106 | * Attributes needed by the xinetd probe |
| 107 | */ |
| 108 | char *id; |
| 109 | char *type; |
| 110 | char **flags; |
| 111 | char *socket_type; |
| 112 | char *name; |
| 113 | char *protocol; |
| 114 | char *user; |
| 115 | char *server; |
| 116 | char *server_args; |
| 117 | char **only_from; |
| 118 | char **no_access; |
| 119 | |
| 120 | uint16_t port; |
| 121 | |
| 122 | bool disable; |
| 123 | bool wait; |
| 124 | int def_disabled; |
| 125 | int def_enabled; |
| 126 | |
| 127 | bool internal; |
| 128 | bool unlisted; |
| 129 | bool rpc; |
| 130 | |
| 131 | struct xiconf_service *next; |
| 132 | |
| 133 | #if 0 /* Unused attributes; Maybe we will need them in some future version. */ |
| 134 | bool groups; |
| 135 | bool mdns; |
| 136 | bool enabled; |
| 137 | char *log_on_success; |
| 138 | char *log_on_failure; |
| 139 | int32_t instances; |
| 140 | int32_t nice; |
| 141 | int32_t rpc_number; |
| 142 | int32_t per_source; |
| 143 | float max_load; |
| 144 | int64_t rlimit_as; |
| 145 | int64_t rlimit_cpu; |
| 146 | int64_t rlimit_data; |
| 147 | int64_t rlimit_rss; |
| 148 | int64_t rlimit_stack; |
| 149 | char *group; |
| 150 | char *access_times; |
| 151 | char *log_type; |
| 152 | char *rpc_version; |
| 153 | char *env; |
| 154 | char *passenv; |
| 155 | char *redirect; |
| 156 | char *bind; |
| 157 | char *interface; |
| 158 | char *banner; |
| 159 | char *banner_success; |
| 160 | char *banner_fail; |
| 161 | char *cps; |
| 162 | char *umask; |
| 163 | char *deny_time; |
| 164 | char *libwrap; |
| 165 | #endif /* 0 */ |
| 166 | } xiconf_service_t; |
| 167 | |
| 168 | typedef struct { |
| 169 | xiconf_service_t **srv; |
| 170 | uint32_t cnt; |
| 171 | } xiconf_strans_t; |
| 172 | |
| 173 | #define XICFG_STRANS_MAXKEYLEN 256 |
| 174 | |
| 175 | typedef struct { |
| 176 | int fd; /**< file descriptor */ |
| 177 | char *cpath; /**< path to the configuration file */ |
| 178 | time_t mtime; /**< modification time of the file */ |
| 179 | rbt_str_node_t *nodes; /**< node pointers in the service tree that belong to this file; */ |
| 180 | size_t count; /**< number of node pointers */ |
| 181 | char *inmem; /**< contents of the file mmaped or copied into memory */ |
| 182 | size_t inlen; /**< in-memory size */ |
| 183 | size_t inoff; /**< in-memory offset */ |
| 184 | int flags; /**< flags; see bellow */ |
| 185 | unsigned int depth; /**< include depth */ |
| 186 | } xiconf_file_t; |
| 187 | |
| 188 | #define XICONF_FILE_MMAPED 0x00000001 /**< try to mmap the file */ |
| 189 | #define XICONF_FILE_PERSIST 0x00000002 /**< keep the file open/mmaped */ |
| 190 | #define XICONF_FILE_DEAD 0x00000004 /**< this item can be skipped/deleted/reused for a different file */ |
| 191 | |
| 192 | typedef struct { |
| 193 | xiconf_file_t **cfile; /**< */ |
| 194 | size_t count; /**< */ |
| 195 | rbt_t *stree; /**< service tree */ |
| 196 | rbt_t *ttree; /**< service name & protocol to ID(s) tree */ |
| 197 | xiconf_service_t *defaults; /**< parsed defaults for services */ |
| 198 | } xiconf_t; |
| 199 | |
| 200 | xiconf_t *xiconf_parse(const char *path, unsigned int max_depth); |
| 201 | void xiconf_free(xiconf_t *xiconf); |
| 202 | int xiconf_update(xiconf_t *xiconf); |
| 203 | int xiconf_parse_section(xiconf_t *xiconf, xiconf_file_t *xifile, int type, char *name); |
| 204 | int xiconf_parse_service(xiconf_file_t *file, xiconf_service_t *service); |
| 205 | int xiconf_parse_defaults(xiconf_file_t *file, xiconf_service_t *defaults, rbt_t *stree); |
| 206 | xiconf_strans_t *xiconf_getservice(xiconf_t *xiconf, char *name, char *prot); |
| 207 | xiconf_strans_t *xiconf_dump(xiconf_t *xiconf); |
| 208 | void xiconf_strans_free(xiconf_strans_t *strans); |
| 209 | |
| 210 | int op_assign_bool(void *var, char *val); |
| 211 | int op_assign_u16(void *var, char *val); |
| 212 | int op_merge_u16(void *dst, void *src, int type); |
| 213 | int op_assign_str(void *var, char *val); |
| 214 | int op_merge_str(void *dst, void *src, int type); |
| 215 | int op_assign_strl(void *var, char *val); |
| 216 | int op_insert_strl(void *var, char *val); |
| 217 | int op_remove_strl(void *var, char *val); |
| 218 | int op_assign_disabled(void *var, char *val); |
| 219 | int op_assign_enabled(void *var, char *val); |
| 220 | |
| 221 | #ifndef STR |
| 222 | # define STR(s) #s |
| 223 | #endif |
| 224 | |
| 225 | #define XICONF_ATTR(name, op_a, op_i, op_r) \ |
| 226 | { STR(name), offsetof(xiconf_service_t, name), (op_a), (op_i), (op_r) } |
| 227 | |
| 228 | /* |
| 229 | * Service only attribute. |
| 230 | * |
| 231 | * Flags set: |
| 232 | * - service section flag |
| 233 | * |
| 234 | * Functions implementing the operators get the pointer to the |
| 235 | * corresponding xiconf_service_t structure item pointer (using |
| 236 | * offsetof) as their first argument. |
| 237 | */ |
| 238 | #define XICONF_SO_ATTR(name, op_a, op_i, op_r, op_c, op_m) \ |
| 239 | { STR(name), offsetof(xiconf_service_t, name), (op_a), (op_i), (op_r), (op_c), (op_m), XIATTR_SECTION_SRV, XIATTR_OPARG_LOCAL } |
| 240 | |
| 241 | /* |
| 242 | * Defaults only attribute. |
| 243 | * |
| 244 | * Flags set: |
| 245 | * - defaults section flag |
| 246 | * |
| 247 | * Type of the first argument to be passed to the operator |
| 248 | * functions has to be specified in the last argument. |
| 249 | * Recognized types are: |
| 250 | * XIATTR_OPTARG_LOCAL - ptr(service + offsetof(attr_name)) |
| 251 | * XIATTR_OPTARG_GLOBAL - pointer to the xiconf_t structure |
| 252 | */ |
| 253 | #define XICONF_DO_ATTR(name, op_a, op_i, op_r, op_c, op_m, off, arg) \ |
| 254 | { STR(name), (off), (op_a), (op_i), (op_r), (op_c), (op_m), XIATTR_SECTION_DEF, (arg) } |
| 255 | |
| 256 | /* |
| 257 | * Service attribute which default attribute can be specified |
| 258 | * in the defaults section. |
| 259 | * |
| 260 | * Flags set: |
| 261 | * - service section flag |
| 262 | * - defaults section flag |
| 263 | * |
| 264 | * Functions implementing the operators get the pointer to the |
| 265 | * corresponding xiconf_service_t structure item pointer (using |
| 266 | * offsetof) as their first argument. |
| 267 | */ |
| 268 | #define XICONF_SD_ATTR(name, op_a, op_i, op_r, op_c, op_m) \ |
| 269 | { STR(name), offsetof(xiconf_service_t, name), (op_a), (op_i), (op_r), (op_c), (op_m), XIATTR_SECTION_SRV|XIATTR_SECTION_DEF, XIATTR_OPARG_LOCAL } |
| 270 | |
| 271 | /* |
| 272 | * Service attribute table; keep this table sorted by attribute name |
| 273 | */ |
| 274 | struct xiconf_attr xiattr_table[] = { |
| 275 | /* |
| 276 | * XICONF_ATTR(attr_name, op_assign, op_insert, op_remove) |
| 277 | */ |
| 278 | //XICONF_ATTR(access_times, NULL, NULL, NULL), |
| 279 | //XICONF_ATTR(banner, NULL, NULL, NULL), |
| 280 | //XICONF_ATTR(banner_fail, NULL, NULL, NULL), |
| 281 | //XICONF_ATTR(banner_success, NULL, NULL, NULL), |
| 282 | //XICONF_ATTR(bind, NULL, NULL, NULL), |
| 283 | //XICONF_ATTR(cps, NULL, NULL, NULL), |
| 284 | //XICONF_ATTR(deny_time, NULL, NULL, NULL), |
| 285 | |
| 286 | XICONF_SO_ATTR(disable, |
| 287 | &op_assign_bool, NULL, NULL, NULL, NULL), |
| 288 | XICONF_DO_ATTR(disabled, |
| 289 | &op_assign_disabled, NULL, NULL, NULL, NULL, 0, XIATTR_OPARG_GLOBAL), |
| 290 | XICONF_DO_ATTR(enabled, |
| 291 | &op_assign_enabled, NULL, NULL, NULL, NULL, 0, XIATTR_OPARG_GLOBAL), |
| 292 | |
| 293 | //XICONF_ATTR(env, NULL, NULL, NULL), |
| 294 | |
| 295 | XICONF_SO_ATTR(flags, |
| 296 | &op_assign_strl, &op_insert_strl, &op_remove_strl, NULL, NULL), |
| 297 | |
| 298 | //XICONF_ATTR(group, NULL, NULL, NULL), |
| 299 | //XICONF_ATTR(groups, NULL, NULL, NULL), |
| 300 | |
| 301 | XICONF_SO_ATTR(id, |
| 302 | &op_assign_str, NULL, NULL, NULL, NULL), |
| 303 | |
| 304 | //XICONF_ATTR(instances, NULL, NULL, NULL), |
| 305 | //XICONF_ATTR(interface, NULL, NULL, NULL), |
| 306 | //XICONF_ATTR(libwrap, NULL, NULL, NULL), |
| 307 | //XICONF_ATTR(log_on_failure, NULL, NULL, NULL), |
| 308 | //XICONF_ATTR(log_on_success, NULL, NULL, NULL), |
| 309 | //XICONF_ATTR(log_type, NULL, NULL, NULL), |
| 310 | //XICONF_ATTR(max_load, NULL, NULL, NULL), |
| 311 | //XICONF_ATTR(mdns, NULL, NULL, NULL), |
| 312 | //XICONF_ATTR(nice, NULL, NULL, NULL), |
| 313 | |
| 314 | XICONF_SD_ATTR(no_access, |
| 315 | &op_assign_strl, &op_insert_strl, &op_remove_strl, NULL, NULL), |
| 316 | XICONF_SD_ATTR(only_from, |
| 317 | &op_assign_strl, &op_insert_strl, &op_remove_strl, NULL, NULL), |
| 318 | |
| 319 | //XICONF_ATTR(passenv, NULL, NULL, NULL), |
| 320 | //XICONF_ATTR(per_source, NULL, NULL, NULL), |
| 321 | |
| 322 | XICONF_SO_ATTR(port, |
| 323 | &op_assign_u16, NULL, NULL, NULL, &op_merge_u16), |
| 324 | XICONF_SO_ATTR(protocol, |
| 325 | &op_assign_str, NULL, NULL, NULL, &op_merge_str), |
| 326 | |
| 327 | //XICONF_ATTR(redirect, NULL, NULL, NULL), |
| 328 | //XICONF_ATTR(rlimit_as, NULL, NULL, NULL), |
| 329 | //XICONF_ATTR(rlimit_cpu, NULL, NULL, NULL), |
| 330 | //XICONF_ATTR(rlimit_data, NULL, NULL, NULL), |
| 331 | //XICONF_ATTR(rlimit_rss, NULL, NULL, NULL), |
| 332 | //XICONF_ATTR(rlimit_stack, NULL, NULL, NULL), |
| 333 | //XICONF_ATTR(rpc_number, NULL, NULL, NULL), |
| 334 | //XICONF_ATTR(rpc_version, NULL, NULL, NULL), |
| 335 | |
| 336 | XICONF_SO_ATTR(server, |
| 337 | &op_assign_str, NULL, NULL, NULL, &op_merge_str), |
| 338 | XICONF_SO_ATTR(server_args, |
| 339 | &op_assign_str, NULL, NULL, NULL, &op_merge_str), |
| 340 | XICONF_SO_ATTR(socket_type, |
| 341 | &op_assign_str, NULL, NULL, NULL, NULL), |
| 342 | XICONF_SO_ATTR(type, |
| 343 | &op_assign_str, NULL, NULL, NULL, NULL), |
| 344 | |
| 345 | //XICONF_ATTR(umask, NULL, NULL, NULL), |
| 346 | |
| 347 | XICONF_SO_ATTR(user, |
| 348 | &op_assign_str, NULL, NULL, NULL, NULL), |
| 349 | XICONF_SO_ATTR(wait, |
| 350 | &op_assign_bool, NULL, NULL, NULL, NULL) |
| 351 | }; |
| 352 | |
| 353 | #define XIATTR_TABLE_COUNT (sizeof xiattr_table / sizeof(struct xiconf_attr)) |
| 354 | |
| 355 | #define xiattr_ptr(ptr, off) ((void *)(((uint8_t *)(ptr))+(off))) |
| 356 | |
| 357 | static int xiattr_cmp(void *a, void *b) |
| 358 | { |
| 359 | return strcmp((char *)a, ((struct xiconf_attr *)b)->name); |
| 360 | } |
| 361 | |
| 362 | /* |
| 363 | * Allocate and initialize a xinetd configuration structure |
| 364 | */ |
| 365 | static xiconf_t *xiconf_new(void) |
| 366 | { |
| 367 | xiconf_t *xiconf = malloc(sizeof(xiconf_t)); |
| 368 | xiconf->cfile = malloc(sizeof(xiconf_file_t *)); |
| 369 | xiconf->count = 0; |
| 370 | xiconf->stree = rbt_str_new(); |
| 371 | xiconf->ttree = rbt_str_new(); |
| 372 | xiconf->defaults = NULL; |
| 373 | |
| 374 | return (xiconf); |
| 375 | } |
| 376 | |
| 377 | static xiconf_service_t *xiconf_service_new(void) |
| 378 | { |
| 379 | xiconf_service_t *service = malloc(sizeof(xiconf_service_t)); |
| 380 | |
| 381 | service->id = NULL; |
| 382 | service->type = NULL; |
| 383 | service->flags = NULL; |
| 384 | service->socket_type = NULL; |
| 385 | service->name = NULL; |
| 386 | service->protocol = NULL; |
| 387 | service->user = NULL; |
| 388 | service->server = NULL; |
| 389 | service->server_args = NULL; |
| 390 | service->only_from = NULL; |
| 391 | service->no_access = NULL; |
| 392 | service->port = 0; |
| 393 | service->disable = 0; |
| 394 | service->wait = 0; |
| 395 | service->def_disabled = 0; |
| 396 | service->def_enabled = 0; |
| 397 | |
| 398 | service->internal = 0; |
| 399 | service->unlisted = 0; |
| 400 | service->rpc = 0; |
| 401 | |
| 402 | service->next = NULL; |
| 403 | |
| 404 | return (service); |
| 405 | } |
| 406 | |
| 407 | static void xiconf_service_free(xiconf_service_t *service) |
| 408 | { |
| 409 | if (service == NULL) |
| 410 | return; |
| 411 | |
| 412 | #define NONNULL_FREE(p) if (service->p != NULL) free(service->p) |
| 413 | |
| 414 | if (service->id != service->name) |
| 415 | NONNULL_FREE(id); |
| 416 | |
| 417 | NONNULL_FREE(name); |
| 418 | NONNULL_FREE(type); |
| 419 | NONNULL_FREE(flags); |
| 420 | NONNULL_FREE(socket_type); |
| 421 | NONNULL_FREE(protocol); |
| 422 | NONNULL_FREE(user); |
| 423 | NONNULL_FREE(server); |
| 424 | NONNULL_FREE(server_args); |
| 425 | NONNULL_FREE(only_from); |
| 426 | NONNULL_FREE(no_access); |
| 427 | |
| 428 | if (service->next != NULL) { |
| 429 | xiconf_service_free(service->next); |
| 430 | } |
| 431 | |
| 432 | free(service); |
| 433 | } |
| 434 | |
| 435 | static void xiconf_stree_free_cb(struct rbt_str_node *n) |
| 436 | { |
| 437 | xiconf_service_free(n->data); |
| 438 | } |
| 439 | |
| 440 | static void xiconf_ttree_free_cb(struct rbt_str_node *n) |
| 441 | { |
| 442 | xiconf_strans_free(n->data); |
| 443 | free(n->key); |
| 444 | } |
| 445 | |
| 446 | void xiconf_free(xiconf_t *xiconf) |
| 447 | { |
| 448 | register size_t i; |
| 449 | |
| 450 | if (xiconf == NULL) |
| 451 | return; |
| 452 | |
| 453 | for (i = 0; i < xiconf->count; ++i) { |
| 454 | if (xiconf->cfile[i]) { |
| 455 | if (xiconf->cfile[i]->cpath) |
| 456 | free(xiconf->cfile[i]->cpath); |
| 457 | if (xiconf->cfile[i]->inmem) |
| 458 | free(xiconf->cfile[i]->inmem); |
| 459 | free(xiconf->cfile[i]); |
| 460 | } |
| 461 | } |
| 462 | |
| 463 | free(xiconf->cfile); |
| 464 | |
| 465 | rbt_str_free_cb(xiconf->stree, xiconf_stree_free_cb); |
| 466 | rbt_str_free_cb(xiconf->ttree, xiconf_ttree_free_cb); |
| 467 | |
| 468 | if (xiconf->defaults != NULL) { |
| 469 | free(xiconf->defaults->name); |
| 470 | free(xiconf->defaults); |
| 471 | } |
| 472 | |
| 473 | free(xiconf); |
| 474 | |
| 475 | return; |
| 476 | } |
| 477 | |
| 478 | static xiconf_file_t *xiconf_read(const char *path, int flags) |
| 479 | { |
| 480 | int fd; |
| 481 | struct stat st; |
| 482 | |
| 483 | fd = open(path, O_RDONLY); |
| 484 | |
| 485 | if (fd < 0) |
| 486 | return (NULL); |
| 487 | |
| 488 | if (fstat(fd, &st) < 0) { |
| 489 | close (fd); |
| 490 | return (NULL); |
| 491 | } |
| 492 | |
| 493 | if ((st.st_mode & S_IFMT) != S_IFREG) { |
| 494 | dE("Not a regular file: %s", path); |
| 495 | close (fd); |
| 496 | return (NULL); |
| 497 | } |
| 498 | |
| 499 | if (st.st_size > XICFG_PARSER_MAXFILESIZE) { |
| 500 | /* You're probably trying to parse a wrong file... or 640k is not enough for you. */ |
| 501 | close (fd); |
| 502 | |
| 503 | return (NULL); |
| 504 | } |
| 505 | |
| 506 | xiconf_file_t *file = malloc(sizeof(xiconf_file_t)); |
| 507 | |
| 508 | /* initialize items that don't need to have extra memory allocated for them */ |
| 509 | file->fd = fd; |
| 510 | file->inlen = (size_t)st.st_size; |
| 511 | file->inoff = 0; |
| 512 | file->mtime = st.st_mtime; |
| 513 | file->nodes = NULL; |
| 514 | file->count = 0; |
| 515 | file->flags = flags; |
| 516 | |
| 517 | #if !defined(XICFG_PARSER_WITHOUT_MMAP) && defined(HAVE_MMAP) |
| 518 | file->inmem = mmap (NULL, file->inlen, PROT_READ, MAP_SHARED, file->fd, 0); |
| 519 | |
| 520 | if (file->inmem == MAP_FAILED) |
| 521 | #endif |
| 522 | { |
| 523 | /* fallback method - copy the contents into memory */ |
| 524 | |
| 525 | file->inmem = malloc(file->inlen+1); |
| 526 | file->inmem[file->inlen] = '\0'; |
| 527 | |
| 528 | if (read (file->fd, file->inmem, file->inlen) != (ssize_t)file->inlen) { |
| 529 | /* Can't read the contents of the file */ |
| 530 | close (fd); |
| 531 | free(file); |
| 532 | return (NULL); |
| 533 | } |
| 534 | |
| 535 | /* |
| 536 | * We have the contents of the file and don't need the fd anymore. |
| 537 | * Unless the FILE_PERSIST flag is set, close fd now. The allocated |
| 538 | * memory will be freed after the parsing is done |
| 539 | */ |
| 540 | if (!(flags & XICONF_FILE_PERSIST)) { |
| 541 | close(fd); |
| 542 | file->fd = -1; |
| 543 | } |
| 544 | } |
| 545 | |
| 546 | /* now initialize item that do need extra memory to be allocated */ |
| 547 | file->cpath = strdup(path); |
| 548 | |
| 549 | return (file); |
| 550 | } |
| 551 | |
| 552 | static int xiconf_add_cfile(xiconf_t *xiconf, const char *path, int depth) |
| 553 | { |
| 554 | xiconf_file_t *xifile; |
| 555 | |
| 556 | if (xiconf->count + 1 > XICFG_PARSER_MAXFILECOUNT) { |
| 557 | dE("include count limit reached: %u", XICFG_PARSER_MAXFILECOUNT); |
| 558 | return (-1); |
| 559 | } |
| 560 | |
| 561 | dD("Reading included file: %s", path); |
| 562 | xifile = xiconf_read (path, 0); |
| 563 | |
| 564 | if (xifile == NULL) { |
| 565 | dW("Failed to read file: %s", path); |
| 566 | return (-1); |
| 567 | } |
| 568 | |
| 569 | xifile->depth = depth; |
| 570 | void *cfile = realloc(xiconf->cfile, sizeof(xiconf_file_t *) * ++xiconf->count); |
| 571 | if (cfile == NULL) { |
| 572 | dE("Failed re-allocate memory for cfile"); |
| 573 | xiconf->count--; |
| 574 | if (xifile->cpath) |
| 575 | free(xifile->cpath); |
| 576 | if (xifile->inmem) |
| 577 | free(xifile->inmem); |
| 578 | free(xifile); |
| 579 | return (-1); |
| 580 | } |
| 581 | xiconf->cfile = cfile; |
| 582 | xiconf->cfile[xiconf->count - 1] = xifile; |
| 583 | |
| 584 | dD("Added new file to the cfile queue: %s; fi=%zu", path, xiconf->count - 1); |
| 585 | |
| 586 | return (0); |
| 587 | } |
| 588 | |
| 589 | #define tmpbuf_def(size) char __tmpbuf[size] |
| 590 | #define tmpbuf_get(size) (((sizeof __tmpbuf)/sizeof(char))<(size)?malloc(sizeof(char)*(size)):__tmpbuf) |
| 591 | #define tmpbuf_free(ptr) do { if ((ptr) != __tmpbuf) free(ptr); (ptr) = NULL; } while(0) |
| 592 | |
| 593 | xiconf_t *xiconf_parse(const char *path, unsigned int max_depth) |
| 594 | { |
| 595 | xiconf_t *xiconf; |
| 596 | xiconf_file_t *xifile; /* pointer to the currently parsed file */ |
| 597 | size_t findex; /* index of currently parsed file */ |
| 598 | |
| 599 | tmpbuf_def(512); |
| 600 | |
| 601 | if (path == NULL) |
| 602 | return (NULL); |
| 603 | |
| 604 | xiconf = xiconf_new(); |
| 605 | |
| 606 | if (xiconf == NULL) |
| 607 | return (NULL); |
| 608 | |
| 609 | xifile = xiconf_read(path, 0); |
| 610 | |
| 611 | if (xifile == NULL) { |
| 612 | xiconf_free(xiconf); |
| 613 | return (NULL); |
| 614 | } |
| 615 | |
| 616 | xifile->depth = 0; |
| 617 | xiconf->cfile[0] = xifile; |
| 618 | xiconf->count = 1; |
| 619 | |
| 620 | for (findex = 0; findex < xiconf->count; ++findex) { |
| 621 | char *buffer; |
| 622 | register char *l_pbeg, *l_pend; |
| 623 | register size_t bufidx, l_size; |
| 624 | |
| 625 | /* sanity checks */ |
| 626 | if (xifile->depth > max_depth && xiconf->count > findex) |
| 627 | abort(); |
| 628 | |
| 629 | xifile = xiconf->cfile[findex]; |
| 630 | |
| 631 | for (;xifile->inoff < xifile->inlen;) { |
| 632 | /* locate end-of-line & compute the line lenght */ |
| 633 | l_pbeg = xifile->inmem + xifile->inoff; |
| 634 | l_pend = strchr(l_pbeg, '\n'); |
| 635 | |
| 636 | if (l_pend == NULL) { |
| 637 | l_pend = l_pbeg + xifile->inlen; |
| 638 | l_size = xifile->inlen; |
| 639 | } else |
| 640 | l_size = (size_t)(l_pend - l_pbeg); |
| 641 | |
| 642 | xifile->inoff += l_size + 1; /* update the next line offset */ |
| 643 | |
| 644 | /* |
| 645 | * copy the current line into a separate buffer |
| 646 | * where we can modify the data (change spaces to |
| 647 | * newlines, etc.) |
| 648 | */ |
| 649 | buffer = tmpbuf_get(l_size + 1); |
| 650 | bufidx = 0; |
| 651 | memcpy (buffer, l_pbeg, l_size); |
| 652 | buffer[l_size] = ' '; |
| 653 | *strchr(buffer, ' ') = '\0'; |
| 654 | |
| 655 | /* skip whitespaces before the keyword */ |
| 656 | while(isspace(buffer[bufidx])) ++bufidx; |
| 657 | |
| 658 | switch(buffer[bufidx]) { |
| 659 | case 's': |
| 660 | if (strcmp("ervice", buffer + bufidx + 1) == 0) { |
| 661 | /* skip the service keyword */ |
| 662 | bufidx += 8; |
| 663 | |
| 664 | if (bufidx >= l_size) { |
| 665 | tmpbuf_free(buffer); |
| 666 | xiconf_free(xiconf); |
| 667 | return (NULL); |
| 668 | } |
| 669 | |
| 670 | /* skip whitespaces before the keyword */ |
| 671 | while(isspace(buffer[bufidx])) ++bufidx; |
| 672 | |
| 673 | if (bufidx >= l_size) { |
| 674 | tmpbuf_free(buffer); |
| 675 | xiconf_free(xiconf); |
| 676 | return (NULL); |
| 677 | } |
| 678 | |
| 679 | *strchr(buffer + bufidx, ' ') = '\0'; |
| 680 | |
| 681 | if (xiconf_parse_section (xiconf, xifile, XICONF_SECTION_SERVICE, buffer + bufidx) != 0) { |
| 682 | tmpbuf_free(buffer); |
| 683 | xiconf_free(xiconf); |
| 684 | return (NULL); |
| 685 | } |
| 686 | } |
| 687 | break; |
| 688 | case 'd': |
| 689 | if (strcmp("efaults", buffer + bufidx + 1) == 0) { |
| 690 | if (xiconf_parse_section (xiconf, xifile, XICONF_SECTION_DEFAULTS, buffer + bufidx) != 0) { |
| 691 | tmpbuf_free(buffer); |
| 692 | xiconf_free(xiconf); |
| 693 | return (NULL); |
| 694 | } |
| 695 | } |
| 696 | break; |
| 697 | case '#': |
| 698 | /* comment */ |
| 699 | case '\0': |
| 700 | /* blank line */ |
| 701 | break; |
| 702 | case 'i': |
| 703 | { |
| 704 | #define XICONF_INCTYPE_FILE 0 |
| 705 | #define XICONF_INCTYPE_DIR 1 |
| 706 | int inctype = -1; |
| 707 | char *inclarg; |
| 708 | char pathbuf[PATH_MAX+1]; |
| 709 | size_t incllen; |
| 710 | |
| 711 | incllen = strlen (buffer + bufidx); |
| 712 | |
| 713 | if (strncmp("nclude", buffer + bufidx + 1, 6) != 0) |
| 714 | break; |
| 715 | switch (buffer[bufidx+1+6]) { |
| 716 | case '\0': |
| 717 | inctype = XICONF_INCTYPE_FILE; |
| 718 | break; |
| 719 | case 'd': |
| 720 | if (buffer[bufidx+1+6+1] == 'i' && |
| 721 | buffer[bufidx+1+6+2] == 'r' && |
| 722 | buffer[bufidx+1+6+3] == '\0') |
| 723 | { |
| 724 | inctype = XICONF_INCTYPE_DIR; |
| 725 | } |
| 726 | break; |
| 727 | } |
| 728 | |
| 729 | /* |
| 730 | * Get the include(dir) argument |
| 731 | */ |
| 732 | bufidx += strlen("includedir"); |
| 733 | while(isspace(buffer[bufidx])) ++bufidx; |
| 734 | inclarg = buffer + bufidx + 1; |
| 735 | buffer[l_size] = '\0'; |
| 736 | |
| 737 | if (*inclarg == '\0') { |
| 738 | dW("Found includedir directive without an argument!"); |
| 739 | break; |
| 740 | } |
| 741 | |
| 742 | if (xifile->depth + 1 > max_depth) { |
| 743 | dE("include depth limit reached: %u", max_depth); |
| 744 | break; |
| 745 | } |
| 746 | |
| 747 | switch (inctype) { |
| 748 | case XICONF_INCTYPE_FILE: |
| 749 | strncpy (pathbuf, inclarg, sizeof(pathbuf)-1); |
| 750 | |
| 751 | dD("includefile: %s", pathbuf); |
| 752 | |
| 753 | if (xiconf_add_cfile (xiconf, pathbuf, xifile->depth + 1) != 0) { |
| 754 | tmpbuf_free(buffer); |
| 755 | continue; |
| 756 | } |
| 757 | else |
| 758 | break; |
| 759 | case XICONF_INCTYPE_DIR: |
| 760 | { |
| 761 | DIR *dirfp; |
| 762 | struct dirent *dent = NULL; |
| 763 | |
| 764 | dD("includedir open: %s", inclarg); |
| 765 | dirfp = opendir (inclarg); |
| 766 | |
| 767 | if (dirfp == NULL) { |
| 768 | dW("Can't open includedir: %s; %d, %s.", inclarg, errno, strerror (errno)); |
| 769 | break; |
| 770 | } |
| 771 | |
| 772 | strcpy (pathbuf, inclarg); |
| 773 | incllen = strlen(inclarg); |
| 774 | |
| 775 | if (pathbuf[incllen - 1] != PATH_SEPARATOR) { |
| 776 | if (incllen < PATH_MAX) { |
| 777 | pathbuf[incllen++] = '/'; |
| 778 | pathbuf[incllen ] = '\0'; |
| 779 | } else { |
| 780 | dE("Length of the includedir argument is out of range: len=%zu, max=%zu", |
| 781 | incllen, PATH_MAX); |
| 782 | closedir(dirfp); |
| 783 | break; |
| 784 | } |
| 785 | } |
| 786 | |
| 787 | for (;;) { |
| 788 | errno = 0; |
| 789 | dent = readdir (dirfp); |
| 790 | if (dent == NULL) { |
| 791 | if (errno) |
| 792 | dW("Can't read directory: %s; %d, %s.", inclarg, errno, strerror (errno)); |
| 793 | break; |
| 794 | } |
| 795 | |
| 796 | if (fnmatch ("*~", dent->d_name, FNM_PATHNAME) == 0 || |
| 797 | fnmatch ("*.*", dent->d_name, FNM_PATHNAME) == 0) |
| 798 | { |
| 799 | dD("Skipping: %s", dent->d_name); |
| 800 | continue; |
| 801 | } |
| 802 | |
| 803 | strcpy(pathbuf + incllen, dent->d_name); |
| 804 | |
| 805 | if (xiconf_add_cfile (xiconf, pathbuf, xifile->depth + 1) != 0) |
| 806 | continue; |
| 807 | } |
| 808 | |
| 809 | dD("includedir close: %s", inclarg); |
| 810 | closedir(dirfp); |
| 811 | break; |
| 812 | }} |
| 813 | break; |
| 814 | } |
| 815 | default: |
| 816 | dE("Don't know how to parse the line: %s", buffer); |
| 817 | tmpbuf_free(buffer); |
| 818 | xiconf_free(xiconf); |
| 819 | |
| 820 | return (NULL); |
| 821 | } |
| 822 | |
| 823 | tmpbuf_free(buffer); |
| 824 | } |
| 825 | } |
| 826 | |
| 827 | return (xiconf); |
| 828 | } |
| 829 | |
| 830 | int xiconf_update(xiconf_t *xiconf) |
| 831 | { |
| 832 | dD("Not implemented yet."); |
| 833 | return (0); |
| 834 | } |
| 835 | |
| 836 | static int xiconf_service_merge_defaults(xiconf_service_t *dst, xiconf_service_t *def) |
| 837 | { |
| 838 | register size_t i; |
| 839 | |
| 840 | for (i = 0; i < XIATTR_TABLE_COUNT; ++i) { |
| 841 | if (xiattr_table[i].pass_arg == XIATTR_OPARG_LOCAL && |
| 842 | xiattr_table[i].sections & XIATTR_SECTION_DEF && |
| 843 | xiattr_table[i].op_merge != NULL) |
| 844 | { |
| 845 | if (xiattr_table[i].op_merge (xiattr_ptr(dst, xiattr_table[i].offset), |
| 846 | xiattr_ptr(def, xiattr_table[i].offset), XIATTR_MERGE_DEFAULTS) != 0) |
| 847 | { |
| 848 | dE("Merge failed. Merge operation returned a non-zero value."); |
| 849 | return (-1); |
| 850 | } |
| 851 | } |
| 852 | } |
| 853 | |
| 854 | return (0); |
| 855 | } |
| 856 | |
| 857 | int xiconf_parse_section(xiconf_t *xiconf, xiconf_file_t *xifile, int type, char *name) |
| 858 | { |
| 859 | xiconf_service_t *snew, *scur; |
| 860 | char *buffer; |
| 861 | register size_t bufidx, keyidx; |
| 862 | char *l_pbeg; |
| 863 | char *l_pend; |
| 864 | size_t l_size; |
| 865 | char *key, *op, *opval; |
| 866 | void **opvar; |
| 867 | int (*opfun)(void *, char *); |
| 868 | struct xiconf_attr *xiattr; |
| 869 | tmpbuf_def(128); |
| 870 | |
| 871 | if (type == XICONF_SECTION_DEFAULTS && xiconf->defaults != NULL) { |
| 872 | dE("defaults section was already parsed!"); |
| 873 | return (-1); |
| 874 | } else if (type == XICONF_SECTION_SERVICE && name == NULL) { |
| 875 | dE("Don't know how to handle anonymous service sections!"); |
| 876 | return (-1); |
| 877 | } |
| 878 | |
| 879 | /* |
| 880 | * Find the left brace. Only the left brace, whitespace |
| 881 | * characters before and after it and a newline character |
| 882 | * at the end are allowed here. |
| 883 | */ |
| 884 | while(isspace(xifile->inmem[xifile->inoff])) |
| 885 | ++xifile->inoff; |
| 886 | if (xifile->inmem[xifile->inoff] != '{') |
| 887 | return(-1); |
| 888 | else |
| 889 | ++xifile->inoff; |
| 890 | while(xifile->inmem[xifile->inoff] != '\n' && |
| 891 | isspace(xifile->inmem[xifile->inoff])) |
| 892 | ++xifile->inoff; |
| 893 | if (xifile->inmem[xifile->inoff] != '\n') |
| 894 | return (-1); |
| 895 | else |
| 896 | ++xifile->inoff; |
| 897 | |
| 898 | /* |
| 899 | * Now there should be only <keyword> <op> <value> items, comments |
| 900 | * blank lines or a right brace which signals and of the section. |
| 901 | */ |
| 902 | snew = xiconf_service_new(); |
| 903 | snew->name = strdup(name); |
| 904 | |
| 905 | for (;;) { |
| 906 | /* |
| 907 | * Find out the line boundaries. |
| 908 | */ |
| 909 | l_pbeg = xifile->inmem + xifile->inoff; |
| 910 | l_pend = strchr(l_pbeg, '\n'); |
| 911 | |
| 912 | if (l_pend == NULL) { |
| 913 | l_pend = l_pbeg + xifile->inlen; |
| 914 | l_size = xifile->inlen; |
| 915 | } else |
| 916 | l_size = (size_t)(l_pend - l_pbeg); |
| 917 | |
| 918 | xifile->inoff += l_size + 1; /* update the next line offset */ |
| 919 | |
| 920 | /* |
| 921 | * copy the current line into a separate buffer |
| 922 | * where we can modify the data (change spaces to |
| 923 | * newlines, etc.) |
| 924 | */ |
| 925 | buffer = tmpbuf_get(l_size + 1); |
| 926 | bufidx = 0; |
| 927 | |
| 928 | memcpy (buffer, l_pbeg, l_size); |
| 929 | |
| 930 | buffer[l_size] = '\0'; |
| 931 | |
| 932 | /* skip whitespaces in the line buffer */ |
| 933 | while(isspace(buffer[bufidx])) ++bufidx; |
| 934 | |
| 935 | /* |
| 936 | * now there should be a attribute name, comment |
| 937 | * or the end-of-line. |
| 938 | */ |
| 939 | key = strdup(buffer + bufidx); |
| 940 | if (key == NULL) |
| 941 | exit(ENOMEM); |
| 942 | |
| 943 | for (keyidx = 0; ! isspace(key[keyidx]) && key[keyidx]; keyidx++); |
| 944 | key[keyidx] = '\0'; |
| 945 | |
| 946 | switch (*key) { |
| 947 | case '#': |
| 948 | case '\0': |
| 949 | /* skip this line */ |
| 950 | break; |
| 951 | case '}': |
| 952 | /* end of section reached */ |
| 953 | ++bufidx; |
| 954 | |
| 955 | while(isspace(buffer[bufidx])) |
| 956 | ++bufidx; |
| 957 | if (buffer[bufidx] != '\0') |
| 958 | goto fail; |
| 959 | |
| 960 | goto finish_section; |
| 961 | default: |
| 962 | op = key + strlen(key) + 1; |
| 963 | |
| 964 | while(isspace(*op)) ++op; |
| 965 | |
| 966 | if (op == l_pend) |
| 967 | goto fail; |
| 968 | |
| 969 | xiattr = oscap_bfind(xiattr_table, XIATTR_TABLE_COUNT, sizeof(struct xiconf_attr), key, &xiattr_cmp); |
| 970 | |
| 971 | if (xiattr == NULL) { |
| 972 | dW("Unknown keyword: %s", key); |
| 973 | #if XICFG_PARSER_IGNORE_UNKNOWN != 1 |
| 974 | goto fail; |
| 975 | #else |
| 976 | break; |
| 977 | #endif |
| 978 | } |
| 979 | |
| 980 | if (!(type & xiattr->sections)) { |
| 981 | dW("Don't know how to handle attribute %s in this sections type (%d)", |
| 982 | key, type); |
| 983 | #if XICFG_PARSER_IGNORE_INVSECT != 1 |
| 984 | goto fail; |
| 985 | #else |
| 986 | break; |
| 987 | #endif |
| 988 | } |
| 989 | |
| 990 | switch (xiattr->pass_arg) { |
| 991 | case XIATTR_OPARG_LOCAL: |
| 992 | opvar = (void *)xiattr_ptr(snew, xiattr->offset); |
| 993 | dD("local opvar"); |
| 994 | break; |
| 995 | case XIATTR_OPARG_GLOBAL: |
| 996 | opvar = (void *)xiconf; |
| 997 | dD("global opvar"); |
| 998 | break; |
| 999 | default: |
| 1000 | abort (); |
| 1001 | } |
| 1002 | |
| 1003 | if (*op == '=') { |
| 1004 | opfun = xiattr->op_assign; |
| 1005 | opval = op + 1; |
| 1006 | |
| 1007 | dD("assign(%p): var=%s (at %p+%zu = %p), val=%s", |
| 1008 | opfun, xiattr->name, snew, xiattr->offset, opvar, opval); |
| 1009 | |
| 1010 | } else if (*op == '+' && *(op+1) == '=') { |
| 1011 | opfun = xiattr->op_insert; |
| 1012 | opval = op + 2; |
| 1013 | |
| 1014 | dD("insert(%p): var=%s (at %p+%zu = %p), val=%s", |
| 1015 | opfun, xiattr->name, snew, xiattr->offset, opvar, opval); |
| 1016 | |
| 1017 | } else if (*op == '-' && *(op+1) == '=') { |
| 1018 | opfun = xiattr->op_remove; |
| 1019 | opval = op + 2; |
| 1020 | |
| 1021 | dD("remove(%p): var=%s (at %p+%zu = %p), val=%s", |
| 1022 | opfun, xiattr->name, snew, xiattr->offset, opvar, opval); |
| 1023 | } else |
| 1024 | goto fail; |
| 1025 | |
| 1026 | while (*opval && isspace(*opval)) ++opval; |
| 1027 | if (opfun == NULL) |
| 1028 | break; |
| 1029 | if (opfun(opvar, opval) != 0) |
| 1030 | goto fail; |
| 1031 | } |
| 1032 | |
| 1033 | tmpbuf_free(buffer); |
| 1034 | free(key); |
| 1035 | key = NULL; |
| 1036 | } |
| 1037 | |
| 1038 | finish_section: |
| 1039 | if (buffer != NULL) { |
| 1040 | dW("Line buffer not freed; freeing now..."); |
| 1041 | tmpbuf_free(buffer); |
| 1042 | } |
| 1043 | |
| 1044 | if (key != NULL) { |
| 1045 | dW("key not freed; freeing now..."); |
| 1046 | free(key); |
| 1047 | } |
| 1048 | |
| 1049 | switch (type) { |
| 1050 | case XICONF_SECTION_DEFAULTS: |
| 1051 | xiconf->defaults = snew; |
| 1052 | break; |
| 1053 | case XICONF_SECTION_SERVICE: |
| 1054 | { |
| 1055 | xiconf_strans_t *st; |
| 1056 | char st_key[XICFG_STRANS_MAXKEYLEN+1]; |
| 1057 | |
| 1058 | if (snew->id == NULL) |
| 1059 | snew->id = snew->name; |
| 1060 | |
| 1061 | scur = NULL; |
| 1062 | /* |
| 1063 | * TODO: get a write lock |
| 1064 | */ |
| 1065 | rbt_str_get(xiconf->stree, snew->id, (void *)&scur); |
| 1066 | |
| 1067 | if (scur == NULL) { |
| 1068 | if (rbt_str_add (xiconf->stree, snew->id, snew) != 0) { |
| 1069 | dE("Can't add service (%s) into the service tree (%p)", |
| 1070 | snew->id, xiconf->stree); |
| 1071 | |
| 1072 | xiconf_service_free(snew); |
| 1073 | return (-1); |
| 1074 | } |
| 1075 | } else { |
| 1076 | if (scur->next == NULL) { |
| 1077 | scur->next = snew; |
| 1078 | } else { |
| 1079 | snew->next = scur->next; |
| 1080 | scur->next = snew; |
| 1081 | } |
| 1082 | } |
| 1083 | scur = snew; |
| 1084 | if (scur->protocol == NULL) { |
| 1085 | // First try to guess the protocol from the socket_type setting |
| 1086 | if (scur->socket_type != NULL) { |
| 1087 | // dgram => udp |
| 1088 | if (strcmp(scur->socket_type, "dgram") == 0) { |
| 1089 | scur->protocol = strdup("udp"); |
| 1090 | } |
| 1091 | // stream => tcp |
| 1092 | else if (strcmp(scur->socket_type, "stream") == 0) { |
| 1093 | scur->protocol = strdup("tcp"); |
| 1094 | } |
| 1095 | } |
| 1096 | // If we still don't know the protocol, then get the default from /etc/services |
| 1097 | if (scur->protocol == NULL) { |
| 1098 | struct servent *service = getservbyname(scur->name, NULL); |
| 1099 | dD("protocol is empty, trying to guess from /etc/services for %s", scur->name); |
| 1100 | if (service != NULL) { |
| 1101 | scur->protocol = strdup(service->s_proto); |
| 1102 | dD("service %s has default protocol=%s", scur->name, scur->protocol); |
| 1103 | } |
| 1104 | endservent(); |
| 1105 | } |
| 1106 | } |
| 1107 | if (scur->port == 0) { |
| 1108 | struct servent *service = getservbyname(scur->name, scur->protocol); |
| 1109 | dD("port not set, trying to guess from /etc/services for %s", scur->name); |
| 1110 | if (service != NULL) { |
| 1111 | if (service->s_port > 0 && service->s_port < 65536) { |
| 1112 | scur->port = ntohs((uint16_t)service->s_port); |
| 1113 | dD("service %s has default port=%hu/%s", |
| 1114 | scur->name, scur->port, scur->protocol); |
| 1115 | } |
| 1116 | } |
| 1117 | endservent(); |
| 1118 | } |
| 1119 | if (scur->type != 0) { |
| 1120 | // Check that the type is one of the allowed values by OVAL. |
| 1121 | // The xinetd configuration acually allows any combination of |
| 1122 | // the values. In such case, we set the type to an empty string. |
| 1123 | const char *type_enum[] = { "RPC", "INTERNAL", "UNLISTED", "TCPMUX", "TCPMUXPLUS", NULL }; |
| 1124 | bool type_enum_match = false; |
| 1125 | size_t i; |
| 1126 | for (i = 0; type_enum[i]; ++i) { |
| 1127 | if (strcmp(scur->type, type_enum[i]) == 0) { |
| 1128 | type_enum_match = true; |
| 1129 | break; |
| 1130 | } |
| 1131 | } |
| 1132 | if (!type_enum_match) { |
| 1133 | dE("The value of the type setting does not match" |
| 1134 | " any of the allowed values by OVAL: %s", scur->type); |
| 1135 | scur->type = ""; |
| 1136 | } |
| 1137 | } |
| 1138 | /* |
| 1139 | * Add entry to the ttree for (name, protocol) -> (id) translation |
| 1140 | * (in case it's not already there) |
| 1141 | */ |
| 1142 | st = NULL; |
| 1143 | strcpy(st_key, scur->name); |
| 1144 | strcat(st_key, scur->protocol); |
| 1145 | |
| 1146 | rbt_str_get(xiconf->ttree, st_key, (void *)&st); |
| 1147 | |
| 1148 | if (st == NULL) { |
| 1149 | dD("new strans record: k=%s", st_key); |
| 1150 | |
| 1151 | st = malloc(sizeof(xiconf_strans_t)); |
| 1152 | st->cnt = 1; |
| 1153 | st->srv = malloc (sizeof (xiconf_service_t *)); |
| 1154 | st->srv[0] = scur; |
| 1155 | |
| 1156 | if (rbt_str_add (xiconf->ttree, strdup(st_key), st) != 0) { |
| 1157 | dE("Can't add strans record (k=%s) into the strans tree (%p)", |
| 1158 | st_key, xiconf->ttree); |
| 1159 | return (-1); |
| 1160 | } |
| 1161 | } else { |
| 1162 | dD("adding new strans record to an exiting one: k=%s, cnt=%u+1", |
| 1163 | st_key, st->cnt); |
| 1164 | |
| 1165 | void *new_srv = realloc(st->srv, sizeof (xiconf_service_t *) * ++(st->cnt)); |
| 1166 | if (new_srv == NULL) { |
| 1167 | dE("Failed to re-allocate memory for st->srv"); |
| 1168 | return (-1); |
| 1169 | } else { |
| 1170 | st->srv = new_srv; |
| 1171 | st->srv[st->cnt - 1] = scur; |
| 1172 | } |
| 1173 | } |
| 1174 | |
| 1175 | /* |
| 1176 | * Merge the service entry with defaults so as to fill all unset attributes |
| 1177 | * that happen to have a default value defined. |
| 1178 | */ |
| 1179 | if (xiconf_service_merge_defaults(scur, xiconf->defaults) != 0) { |
| 1180 | dE("Failed to merge service (%p, id=%s) with defaults (%p)", |
| 1181 | scur, scur->id, xiconf->defaults); |
| 1182 | return (-1); |
| 1183 | } |
| 1184 | |
| 1185 | /* TODO: resolve values for all attributes */ |
| 1186 | /* TODO: unlock the service tree */ |
| 1187 | } |
| 1188 | break; |
| 1189 | default: |
| 1190 | abort (); |
| 1191 | } |
| 1192 | |
| 1193 | return (0); |
| 1194 | fail: |
| 1195 | tmpbuf_free(buffer); |
| 1196 | if (key) { |
| 1197 | dW("fail: key not freed; freeing now..."); |
| 1198 | free(key); |
| 1199 | } |
| 1200 | |
| 1201 | if (snew->name) { |
| 1202 | dW("fail: snew->name not freed; freeing now..."); |
| 1203 | free(snew->name); |
| 1204 | } |
| 1205 | free(snew); |
| 1206 | |
| 1207 | return (-1); |
| 1208 | } |
| 1209 | |
| 1210 | xiconf_strans_t *xiconf_getservice(xiconf_t *xiconf, char *name, char *prot) |
| 1211 | { |
| 1212 | char strans_key[XICFG_STRANS_MAXKEYLEN+1]; |
| 1213 | xiconf_strans_t *strans = NULL; |
| 1214 | |
| 1215 | if (xiconf == NULL) { |
| 1216 | return NULL; |
| 1217 | } |
| 1218 | |
| 1219 | if (name == NULL || prot == NULL) |
| 1220 | return (NULL); |
| 1221 | |
| 1222 | if (strlen(name) + strlen(prot) > XICFG_STRANS_MAXKEYLEN) |
| 1223 | return (NULL); |
| 1224 | |
| 1225 | strcpy(strans_key, name); |
| 1226 | strcat(strans_key, prot); |
| 1227 | |
| 1228 | rbt_str_get(xiconf->ttree, strans_key, (void *)&strans); |
| 1229 | |
| 1230 | return (strans); |
| 1231 | } |
| 1232 | |
| 1233 | static int xiconf_dump_cb(struct rbt_str_node *node, void *user) |
| 1234 | { |
| 1235 | xiconf_strans_t *res = (xiconf_strans_t *)user; |
| 1236 | |
| 1237 | if (res->cnt <= 0) { |
| 1238 | return -1; |
| 1239 | } |
| 1240 | |
| 1241 | res->srv[res->cnt - 1] = (xiconf_service_t *)node->data; |
| 1242 | --res->cnt; |
| 1243 | |
| 1244 | return (0); |
| 1245 | } |
| 1246 | |
| 1247 | xiconf_strans_t *xiconf_dump(xiconf_t *xiconf) |
| 1248 | { |
| 1249 | if (xiconf == NULL) { |
| 1250 | return NULL; |
| 1251 | } |
| 1252 | |
| 1253 | xiconf_strans_t *res = malloc(sizeof(xiconf_strans_t)); |
| 1254 | res->cnt = rbt_str_size(xiconf->stree); |
| 1255 | res->srv = malloc(sizeof(xiconf_service_t *) * res->cnt); |
| 1256 | |
| 1257 | rbt_str_walk_inorder2(xiconf->stree, xiconf_dump_cb, (void *)res, 0); |
| 1258 | |
| 1259 | res->cnt = rbt_str_size(xiconf->stree); |
| 1260 | |
| 1261 | return (res); |
| 1262 | } |
| 1263 | |
| 1264 | void xiconf_strans_free(xiconf_strans_t *strans) |
| 1265 | { |
| 1266 | if (strans == NULL) |
| 1267 | return; |
| 1268 | |
| 1269 | free(strans->srv); |
| 1270 | free(strans); |
| 1271 | return; |
| 1272 | } |
| 1273 | |
| 1274 | int op_assign_bool(void *var, char *val) |
| 1275 | { |
| 1276 | if (var == NULL) { |
| 1277 | return -1; |
| 1278 | } |
| 1279 | |
| 1280 | if (strcmp(val, "yes") == 0) { |
| 1281 | *((bool *)(var)) = true; |
| 1282 | } else if (strcmp(val, "no") == 0) { |
| 1283 | *((bool *)(var)) = false; |
| 1284 | } else { |
| 1285 | char *endptr = NULL; |
| 1286 | errno = 0; |
| 1287 | *((bool *)(var)) = (bool) strtol (val, &endptr, 2); |
| 1288 | if (errno == EINVAL || errno == ERANGE) { |
| 1289 | return -1; |
| 1290 | } |
| 1291 | if (endptr != NULL) { |
| 1292 | if (*endptr != '\0' || endptr == val) { |
| 1293 | return -1; |
| 1294 | } |
| 1295 | } |
| 1296 | } |
| 1297 | return 0; |
| 1298 | } |
| 1299 | |
| 1300 | int op_assign_u16(void *var, char *val) |
| 1301 | { |
| 1302 | long num = strtol(val, NULL, 10); |
| 1303 | |
| 1304 | if (num > 0 && num < (1<<16)) { |
| 1305 | *(uint16_t *)(var) = (uint16_t)(num); |
| 1306 | return (0); |
| 1307 | } |
| 1308 | |
| 1309 | return (-1); |
| 1310 | } |
| 1311 | |
| 1312 | int op_merge_u16(void *dst, void *src, int type) |
| 1313 | { |
| 1314 | uint16_t *a = (uint16_t *)dst; |
| 1315 | uint16_t *b = (uint16_t *)src; |
| 1316 | |
| 1317 | if (*a != 0) |
| 1318 | return (-1); |
| 1319 | else |
| 1320 | *a = *b; |
| 1321 | |
| 1322 | return (0); |
| 1323 | } |
| 1324 | |
| 1325 | int op_assign_str(void *var, char *val) |
| 1326 | { |
| 1327 | char *strend = NULL; |
| 1328 | if (var == NULL) { |
| 1329 | return -1; |
| 1330 | } |
| 1331 | |
| 1332 | /* skip whitespaces */ |
| 1333 | while(isspace(*val)) ++val; |
| 1334 | |
| 1335 | if (*val != '\0') { |
| 1336 | strend = strrchr(val, '\0'); |
| 1337 | /* strip trailing whitespaces */ |
| 1338 | do { |
| 1339 | strend--; |
| 1340 | } while(isspace(*strend)); |
| 1341 | if((strend-val) < 0) { |
| 1342 | dE("Error stripping white space from string '%s'", val); |
| 1343 | return (-1); |
| 1344 | } |
| 1345 | *((char **)(var)) = strndup(val, (strend-val+1)); |
| 1346 | return (0); |
| 1347 | } else |
| 1348 | return (-1); |
| 1349 | } |
| 1350 | |
| 1351 | int op_merge_str(void *dst, void *src, int type) |
| 1352 | { |
| 1353 | char **a = (char **)dst; |
| 1354 | char **b = (char **)src; |
| 1355 | |
| 1356 | if (*a != NULL) { |
| 1357 | dE("Cannot merge str value: dst=%p (%s), src=%p (%s)", dst, *a, src, *b); |
| 1358 | return (-1); |
| 1359 | } else |
| 1360 | *a = *b; |
| 1361 | |
| 1362 | return (0); |
| 1363 | } |
| 1364 | |
| 1365 | int op_assign_strl(void *var, char *val) |
| 1366 | { |
| 1367 | char ***aptr = (char ***)var; |
| 1368 | char **string_array = *aptr; |
| 1369 | char *tok, *str; |
| 1370 | size_t string_array_size = 0; |
| 1371 | |
| 1372 | if (string_array != NULL) { |
| 1373 | // Destroy previous array state |
| 1374 | while(string_array[string_array_size]) { |
| 1375 | free(string_array[string_array_size]); |
| 1376 | ++string_array_size; |
| 1377 | } |
| 1378 | free(string_array); |
| 1379 | string_array = NULL; |
| 1380 | string_array_size = 0; |
| 1381 | } |
| 1382 | str = val; |
| 1383 | while ((tok = strsep(&str, " ")) != NULL) { |
| 1384 | while(isspace(*tok)) { |
| 1385 | ++tok; |
| 1386 | } |
| 1387 | if (*tok == '\0') { |
| 1388 | continue; |
| 1389 | } |
| 1390 | dD("Adding new member to string array: %s", tok); |
| 1391 | void *new_string_array = realloc(string_array, sizeof(char *) * (++string_array_size + 1)); |
| 1392 | if (new_string_array == NULL) { |
| 1393 | *aptr = string_array; |
| 1394 | return -1; |
| 1395 | } |
| 1396 | string_array = new_string_array; |
| 1397 | string_array[string_array_size-1] = strdup(tok); |
| 1398 | string_array[string_array_size] = NULL; |
| 1399 | } |
| 1400 | *aptr = string_array; |
| 1401 | return 0; |
| 1402 | } |
| 1403 | |
| 1404 | int op_insert_strl(void *var, char *val) |
| 1405 | { |
| 1406 | char ***aptr = (char ***)var; |
| 1407 | char **string_array = *aptr; |
| 1408 | char *tok, *str; |
| 1409 | size_t string_array_size = 0; |
| 1410 | |
| 1411 | if (string_array != NULL) { |
| 1412 | // Count the number of items in the array |
| 1413 | while(string_array[string_array_size]) { |
| 1414 | ++string_array_size; |
| 1415 | } |
| 1416 | dD("String array has %zu members", string_array_size); |
| 1417 | } |
| 1418 | str = val; |
| 1419 | while ((tok = strsep(&str, " ")) != NULL) { |
| 1420 | while(isspace(*tok)) { |
| 1421 | ++tok; |
| 1422 | } |
| 1423 | if (*tok == '\0') { |
| 1424 | continue; |
| 1425 | } |
| 1426 | dD("Adding new member to string array: %s", tok); |
| 1427 | void *new_string_array = realloc(string_array, sizeof(char *) * (++string_array_size + 1)); |
| 1428 | if (new_string_array == NULL) { |
| 1429 | *aptr = string_array; |
| 1430 | return -1; |
| 1431 | } |
| 1432 | string_array = new_string_array; |
| 1433 | string_array[string_array_size-1] = strdup(tok); |
| 1434 | string_array[string_array_size] = NULL; |
| 1435 | } |
| 1436 | *aptr = string_array; |
| 1437 | return 0; |
| 1438 | } |
| 1439 | |
| 1440 | int op_remove_strl(void *var, char *val) |
| 1441 | { |
| 1442 | char ***aptr = (char ***)var; |
| 1443 | char **string_array = *aptr, **valstr_array; |
| 1444 | char **newstr_array; |
| 1445 | char *tok, *str; |
| 1446 | size_t string_array_size = 0, valstr_array_size; |
| 1447 | size_t string_array_pos, valstr_array_pos; |
| 1448 | size_t newstr_array_size; |
| 1449 | |
| 1450 | if (string_array != NULL) { |
| 1451 | // Count the number of items in the array |
| 1452 | while(string_array[string_array_size]) { |
| 1453 | ++string_array_size; |
| 1454 | } |
| 1455 | dD("String array has %zu members", string_array_size); |
| 1456 | } |
| 1457 | |
| 1458 | if (string_array_size < 1) { |
| 1459 | // Nothing to remove here |
| 1460 | return 0; |
| 1461 | } else { |
| 1462 | // Allocate the new string array |
| 1463 | newstr_array = malloc(sizeof(char *) * (string_array_size + 1)); |
| 1464 | if (newstr_array == NULL) |
| 1465 | return -1; |
| 1466 | } |
| 1467 | |
| 1468 | // Create an array of strings to be removed from the array |
| 1469 | valstr_array = NULL; |
| 1470 | valstr_array_size = 0; |
| 1471 | str = val; |
| 1472 | while ((tok = strsep(&str, " ")) != NULL) { |
| 1473 | while(isspace(*tok)) { |
| 1474 | ++tok; |
| 1475 | } |
| 1476 | if (*tok == '\0') { |
| 1477 | continue; |
| 1478 | } |
| 1479 | dD("Adding new member to string array: %s", tok); |
| 1480 | void *new_valstr_array = realloc(valstr_array, sizeof(char *) * (++valstr_array_size + 1)); |
| 1481 | if (new_valstr_array == NULL) { |
| 1482 | free(newstr_array); |
| 1483 | free(valstr_array); |
| 1484 | return -2; |
| 1485 | } |
| 1486 | valstr_array = new_valstr_array; |
| 1487 | valstr_array[valstr_array_size-1] = tok; |
| 1488 | valstr_array[valstr_array_size] = NULL; |
| 1489 | } |
| 1490 | if (valstr_array == NULL) { |
| 1491 | free(newstr_array); |
| 1492 | return -2; |
| 1493 | } |
| 1494 | |
| 1495 | // Remove the insersection from the string array |
| 1496 | newstr_array_size = 0; |
| 1497 | for (string_array_pos = 0; string_array_pos < string_array_size; ++string_array_pos) { |
| 1498 | char *string_array_val = string_array[string_array_pos]; |
| 1499 | |
| 1500 | for (valstr_array_pos = 0; valstr_array[valstr_array_pos]; ++valstr_array_pos) { |
| 1501 | char *delete_val = valstr_array[valstr_array_pos]; |
| 1502 | dD("Removing: %s", delete_val); |
| 1503 | // Destroy the string if it matches a string from the value string array |
| 1504 | // Otherwise move it to the new string array |
| 1505 | dD("cmp: %s ?= %s", string_array_val, delete_val); |
| 1506 | if (strcmp(string_array_val, delete_val) == 0) { |
| 1507 | free(string_array_val); |
| 1508 | string_array_val = NULL; |
| 1509 | break; |
| 1510 | } |
| 1511 | } |
| 1512 | // If the value wasn't removed, move it to the new string array |
| 1513 | if (string_array_val != NULL) { |
| 1514 | newstr_array[newstr_array_size] = string_array_val; |
| 1515 | ++newstr_array_size; |
| 1516 | } |
| 1517 | string_array[string_array_pos] = NULL; |
| 1518 | } |
| 1519 | |
| 1520 | newstr_array[newstr_array_size] = NULL; |
| 1521 | free(string_array); |
| 1522 | free(valstr_array); |
| 1523 | void *new_newstr_array = realloc(newstr_array, sizeof(char *) * (newstr_array_size + 1)); |
| 1524 | if (new_newstr_array != NULL) |
| 1525 | newstr_array = new_newstr_array; |
| 1526 | *aptr = newstr_array; |
| 1527 | return 0; |
| 1528 | } |
| 1529 | |
| 1530 | int op_assign_disabled(void *var, char *val) |
| 1531 | { |
| 1532 | xiconf_t *xiconf; |
| 1533 | xiconf_service_t *srv; |
| 1534 | char *tok, *str; |
| 1535 | |
| 1536 | xiconf = (xiconf_t *)var; |
| 1537 | str = val; |
| 1538 | srv = NULL; |
| 1539 | |
| 1540 | while ((tok = strsep (&str, " ")) != NULL) { |
| 1541 | while (isspace(*tok)) |
| 1542 | ++tok; |
| 1543 | if (*tok == '\0') |
| 1544 | continue; |
| 1545 | |
| 1546 | rbt_str_get(xiconf->stree, tok, (void *)&srv); |
| 1547 | |
| 1548 | if (srv == NULL) { |
| 1549 | srv = xiconf_service_new(); |
| 1550 | srv->id = strdup (tok); |
| 1551 | srv->def_disabled = 1; |
| 1552 | |
| 1553 | dD("new: def_disabled: = 1: %s", srv->id); |
| 1554 | |
| 1555 | if (rbt_str_add (xiconf->stree, srv->id, srv) != 0) { |
| 1556 | dE("Can't add service (%s) into the service tree (%p)", |
| 1557 | srv->id, xiconf->stree); |
| 1558 | |
| 1559 | xiconf_service_free(srv); |
| 1560 | return (-1); |
| 1561 | } |
| 1562 | } else { |
| 1563 | dD("mod: def_disabled: %u -> 1: %s", srv->def_disabled, srv->id); |
| 1564 | srv->def_disabled = 1; |
| 1565 | } |
| 1566 | } |
| 1567 | |
| 1568 | return (0); |
| 1569 | } |
| 1570 | |
| 1571 | int op_assign_enabled(void *var, char *val) |
| 1572 | { |
| 1573 | xiconf_t *xiconf; |
| 1574 | xiconf_service_t *srv; |
| 1575 | char *tok, *str; |
| 1576 | |
| 1577 | xiconf = (xiconf_t *)var; |
| 1578 | str = val; |
| 1579 | srv = NULL; |
| 1580 | |
| 1581 | while ((tok = strsep (&str, " ")) != NULL) { |
| 1582 | while (isspace(*tok)) |
| 1583 | ++tok; |
| 1584 | if (*tok == '\0') |
| 1585 | continue; |
| 1586 | |
| 1587 | rbt_str_get(xiconf->stree, tok, (void *)&srv); |
| 1588 | |
| 1589 | if (srv == NULL) { |
| 1590 | srv = xiconf_service_new(); |
| 1591 | srv->id = strdup (tok); |
| 1592 | srv->def_enabled = 1; |
| 1593 | |
| 1594 | dD("new: def_enabled: = 1: %s", srv->id); |
| 1595 | |
| 1596 | if (rbt_str_add (xiconf->stree, srv->id, srv) != 0) { |
| 1597 | dE("Can't add service (%s) into the service tree (%p)", |
| 1598 | srv->id, xiconf->stree); |
| 1599 | |
| 1600 | xiconf_service_free(srv); |
| 1601 | return (-1); |
| 1602 | } |
| 1603 | } else { |
| 1604 | dD("mod: def_enabled: %u -> 1: %s", srv->def_disabled, srv->id); |
| 1605 | srv->def_enabled = 1; |
| 1606 | } |
| 1607 | } |
| 1608 | |
| 1609 | return (0); |
| 1610 | } |
| 1611 | |
| 1612 | /* |
| 1613 | * xinetd probe |
| 1614 | */ |
| 1615 | |
| 1616 | #ifndef XINETD_CONFPATH |
| 1617 | #define XINETD_CONFPATH "/etc/xinetd.conf" |
| 1618 | #endif |
| 1619 | |
| 1620 | #ifndef XINETD_CONFDEPTH |
| 1621 | #define XINETD_CONFDEPTH 32 |
| 1622 | #endif |
| 1623 | |
| 1624 | #ifndef XINETD_TEST |
| 1625 | static void xiservice_process_query(probe_ctx *ctx, SEXP_t *service_name, SEXP_t *protocol, const xiconf_service_t *xsrv) |
| 1626 | { |
| 1627 | SEXP_t *xres_service_name, *xres_protocol, *item; |
| 1628 | |
| 1629 | xres_service_name = SEXP_string_new(xsrv->name, strlen(xsrv->name)); |
| 1630 | xres_protocol = SEXP_string_new(xsrv->protocol, strlen(xsrv->protocol)); |
| 1631 | if (probe_entobj_cmp(service_name, xres_service_name) == OVAL_RESULT_TRUE && |
| 1632 | probe_entobj_cmp(protocol, xres_protocol) == OVAL_RESULT_TRUE |
| 1633 | ) { |
| 1634 | item = probe_item_create(OVAL_UNIX_XINETD, NULL, |
| 1635 | "protocol", OVAL_DATATYPE_STRING, xsrv->protocol, |
| 1636 | "service_name", OVAL_DATATYPE_STRING, xsrv->name, |
| 1637 | "flags", OVAL_DATATYPE_STRING_M, xsrv->flags, |
| 1638 | "no_access", OVAL_DATATYPE_STRING_M, xsrv->no_access, |
| 1639 | "only_from", OVAL_DATATYPE_STRING_M, xsrv->only_from, |
| 1640 | "port", OVAL_DATATYPE_INTEGER, (int64_t)xsrv->port, |
| 1641 | "server", OVAL_DATATYPE_STRING, xsrv->server, |
| 1642 | "server_arguments", OVAL_DATATYPE_STRING, xsrv->server_args, |
| 1643 | "socket_type", OVAL_DATATYPE_STRING, xsrv->socket_type, |
| 1644 | "type", OVAL_DATATYPE_STRING, xsrv->type, |
| 1645 | "user", OVAL_DATATYPE_STRING, xsrv->user, |
| 1646 | "wait", OVAL_DATATYPE_BOOLEAN, xsrv->wait, |
| 1647 | "disabled", OVAL_DATATYPE_BOOLEAN, xsrv->disable, |
| 1648 | NULL); |
| 1649 | |
| 1650 | probe_item_collect(ctx, item); |
| 1651 | } |
| 1652 | SEXP_free(xres_service_name); |
| 1653 | SEXP_free(xres_protocol); |
| 1654 | } |
| 1655 | |
| 1656 | int xinetd_probe_offline_mode_supported() |
| 1657 | { |
| 1658 | return PROBE_OFFLINE_CHROOT; |
| 1659 | } |
| 1660 | |
| 1661 | void *xinetd_probe_init(void) |
| 1662 | { |
| 1663 | return xiconf_parse(XINETD_CONFPATH, XINETD_CONFDEPTH); |
| 1664 | } |
| 1665 | |
| 1666 | void xinetd_probe_fini(void *arg) |
| 1667 | { |
| 1668 | if (arg != NULL) |
| 1669 | xiconf_free(arg); |
| 1670 | } |
| 1671 | |
| 1672 | int xinetd_probe_main(probe_ctx *ctx, void *arg) |
| 1673 | { |
| 1674 | SEXP_t *service_name, *protocol, *eval, *object; |
| 1675 | char srv_name[256]; |
| 1676 | char srv_prot[256]; |
| 1677 | int err; |
| 1678 | |
| 1679 | xiconf_service_t *xsrv; |
| 1680 | xiconf_strans_t *xres; |
| 1681 | xiconf_t *xcfg = (xiconf_t *)arg; |
| 1682 | |
| 1683 | if (arg == NULL) { |
| 1684 | probe_cobj_set_flag(probe_ctx_getresult(ctx), SYSCHAR_FLAG_NOT_APPLICABLE); |
| 1685 | return (0); |
| 1686 | } |
| 1687 | |
| 1688 | object = probe_ctx_getobject(ctx); |
| 1689 | |
| 1690 | service_name = probe_obj_getent(object, "service_name", 1); |
| 1691 | |
| 1692 | if (service_name == NULL) { |
| 1693 | err = PROBE_ENOVAL; |
| 1694 | goto fail; |
| 1695 | } |
| 1696 | |
| 1697 | eval = probe_ent_getval(service_name); |
| 1698 | |
| 1699 | if (SEXP_string_cstr_r (eval, srv_name, sizeof srv_name) == (size_t)-1) { |
| 1700 | SEXP_free (service_name); |
| 1701 | SEXP_free (eval); |
| 1702 | err = PROBE_ERANGE; |
| 1703 | goto fail; |
| 1704 | } |
| 1705 | |
| 1706 | SEXP_free (eval); |
| 1707 | |
| 1708 | protocol = probe_obj_getent(object, "protocol", 1); |
| 1709 | |
| 1710 | if (protocol == NULL) { |
| 1711 | SEXP_free (service_name); |
| 1712 | err = PROBE_ENOVAL; |
| 1713 | goto fail; |
| 1714 | } |
| 1715 | |
| 1716 | eval = probe_ent_getval(protocol); |
| 1717 | |
| 1718 | if (SEXP_string_cstr_r (eval, srv_prot, sizeof srv_prot) == (size_t)-1) { |
| 1719 | SEXP_free (service_name); |
| 1720 | SEXP_free (protocol); |
| 1721 | SEXP_free (eval); |
| 1722 | err = PROBE_ERANGE; |
| 1723 | goto fail; |
| 1724 | } |
| 1725 | |
| 1726 | SEXP_free (eval); |
| 1727 | |
| 1728 | dD("Updating xinetd configuration cache"); |
| 1729 | |
| 1730 | if (xiconf_update(arg) != 0) { |
| 1731 | err = PROBE_EUNKNOWN; |
| 1732 | goto fail; |
| 1733 | } |
| 1734 | |
| 1735 | xres = xiconf_dump(xcfg); |
| 1736 | |
| 1737 | if (xres != NULL) { |
| 1738 | register unsigned int l; |
| 1739 | |
| 1740 | for (l = 0; l < xres->cnt; ++l) { |
| 1741 | xsrv = xres->srv[l]; |
| 1742 | while (xsrv != NULL) { |
| 1743 | xiservice_process_query(ctx, service_name, protocol, xsrv); |
| 1744 | xsrv = xsrv->next; |
| 1745 | } |
| 1746 | } |
| 1747 | free(xres->srv); |
| 1748 | free(xres); |
| 1749 | |
| 1750 | } |
| 1751 | SEXP_free(service_name); |
| 1752 | SEXP_free(protocol); |
| 1753 | |
| 1754 | return (0); |
| 1755 | fail: |
| 1756 | probe_cobj_set_flag(probe_ctx_getresult(ctx), SYSCHAR_FLAG_ERROR); |
| 1757 | return err; |
| 1758 | } |
| 1759 | #endif |