163 lines
5.4 KiB
C
163 lines
5.4 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <yaml.h>
|
|
#include "rdp_broker.h"
|
|
|
|
int load_config(broker_config_t *config) {
|
|
const char *env_value;
|
|
|
|
/* Initialize config with defaults */
|
|
memset(config, 0, sizeof(broker_config_t));
|
|
|
|
/* Load from environment variables */
|
|
env_value = getenv("SAMBA_AD_SERVER");
|
|
if (env_value) {
|
|
strncpy(config->samba_server, env_value, MAX_HOSTNAME_LEN - 1);
|
|
} else {
|
|
LOG(LOG_ERROR, "SAMBA_AD_SERVER environment variable not set");
|
|
return -1;
|
|
}
|
|
|
|
env_value = getenv("SAMBA_AD_PORT");
|
|
config->samba_port = env_value ? atoi(env_value) : 389;
|
|
|
|
env_value = getenv("SAMBA_AD_BASE_DN");
|
|
if (env_value) {
|
|
strncpy(config->base_dn, env_value, MAX_PATH_LEN - 1);
|
|
} else {
|
|
LOG(LOG_ERROR, "SAMBA_AD_BASE_DN environment variable not set");
|
|
return -1;
|
|
}
|
|
|
|
env_value = getenv("RDP_LISTEN_PORT");
|
|
config->rdp_listen_port = env_value ? atoi(env_value) : 3389;
|
|
|
|
env_value = getenv("TARGETS_CONFIG_PATH");
|
|
if (env_value) {
|
|
strncpy(config->targets_config_path, env_value, MAX_PATH_LEN - 1);
|
|
} else {
|
|
strncpy(config->targets_config_path, "/etc/rdpbroker/targets.yaml",
|
|
MAX_PATH_LEN - 1);
|
|
}
|
|
|
|
env_value = getenv("LOG_LEVEL");
|
|
if (env_value) {
|
|
if (strcmp(env_value, "DEBUG") == 0) {
|
|
config->log_level = LOG_DEBUG;
|
|
} else if (strcmp(env_value, "INFO") == 0) {
|
|
config->log_level = LOG_INFO;
|
|
} else if (strcmp(env_value, "WARN") == 0) {
|
|
config->log_level = LOG_WARN;
|
|
} else if (strcmp(env_value, "ERROR") == 0) {
|
|
config->log_level = LOG_ERROR;
|
|
} else {
|
|
config->log_level = LOG_INFO;
|
|
}
|
|
} else {
|
|
config->log_level = LOG_INFO;
|
|
}
|
|
|
|
global_log_level = config->log_level;
|
|
|
|
/* Load targets configuration */
|
|
return load_targets(config, config->targets_config_path);
|
|
}
|
|
|
|
int load_targets(broker_config_t *config, const char *path) {
|
|
FILE *file;
|
|
yaml_parser_t parser;
|
|
yaml_event_t event;
|
|
int done = 0;
|
|
int in_targets = 0;
|
|
int in_target = 0;
|
|
char key[256] = {0};
|
|
rdp_target_t current_target;
|
|
|
|
memset(¤t_target, 0, sizeof(rdp_target_t));
|
|
|
|
file = fopen(path, "r");
|
|
if (!file) {
|
|
LOG(LOG_ERROR, "Failed to open targets file: %s", path);
|
|
return -1;
|
|
}
|
|
|
|
if (!yaml_parser_initialize(&parser)) {
|
|
LOG(LOG_ERROR, "Failed to initialize YAML parser");
|
|
fclose(file);
|
|
return -1;
|
|
}
|
|
|
|
yaml_parser_set_input_file(&parser, file);
|
|
config->target_count = 0;
|
|
|
|
/* Simple YAML parsing - this is a basic implementation */
|
|
/* In production, use a more robust YAML library */
|
|
while (!done) {
|
|
if (!yaml_parser_parse(&parser, &event)) {
|
|
LOG(LOG_ERROR, "YAML parser error");
|
|
break;
|
|
}
|
|
|
|
switch (event.type) {
|
|
case YAML_SCALAR_EVENT:
|
|
if (strcmp((char *)event.data.scalar.value, "targets") == 0) {
|
|
in_targets = 1;
|
|
} else if (in_targets && strcmp(key, "name") == 0) {
|
|
strncpy(current_target.name,
|
|
(char *)event.data.scalar.value,
|
|
MAX_HOSTNAME_LEN - 1);
|
|
key[0] = '\0';
|
|
} else if (in_targets && strcmp(key, "host") == 0) {
|
|
strncpy(current_target.host,
|
|
(char *)event.data.scalar.value,
|
|
MAX_HOSTNAME_LEN - 1);
|
|
key[0] = '\0';
|
|
} else if (in_targets && strcmp(key, "port") == 0) {
|
|
current_target.port = atoi((char *)event.data.scalar.value);
|
|
key[0] = '\0';
|
|
} else if (in_targets && strcmp(key, "description") == 0) {
|
|
strncpy(current_target.description,
|
|
(char *)event.data.scalar.value,
|
|
MAX_DESCRIPTION_LEN - 1);
|
|
key[0] = '\0';
|
|
|
|
/* Target is complete, add it */
|
|
if (config->target_count < MAX_TARGETS) {
|
|
memcpy(&config->targets[config->target_count],
|
|
¤t_target,
|
|
sizeof(rdp_target_t));
|
|
config->target_count++;
|
|
LOG(LOG_DEBUG, "Loaded target: %s (%s:%d)",
|
|
current_target.name, current_target.host,
|
|
current_target.port);
|
|
}
|
|
memset(¤t_target, 0, sizeof(rdp_target_t));
|
|
} else if (in_targets) {
|
|
strncpy(key, (char *)event.data.scalar.value, sizeof(key) - 1);
|
|
}
|
|
break;
|
|
|
|
case YAML_STREAM_END_EVENT:
|
|
done = 1;
|
|
break;
|
|
|
|
default:
|
|
break;
|
|
}
|
|
|
|
yaml_event_delete(&event);
|
|
}
|
|
|
|
yaml_parser_delete(&parser);
|
|
fclose(file);
|
|
|
|
LOG(LOG_INFO, "Loaded %d targets from %s", config->target_count, path);
|
|
return 0;
|
|
}
|
|
|
|
void free_config(broker_config_t *config) {
|
|
/* Nothing to free for now, but placeholder for future use */
|
|
(void)config;
|
|
}
|