mirror of
https://gitlab.com/openconnect/ocserv.git
synced 2026-10-14 02:22:03 +08:00
Read configuration file
This commit is contained in:
+141
-1
@@ -23,10 +23,146 @@
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/types.h>
|
||||
#include <pwd.h>
|
||||
#include <grp.h>
|
||||
#include <fcntl.h>
|
||||
#include <ocserv-args.h>
|
||||
#include <autoopts/options.h>
|
||||
|
||||
#include <vpn.h>
|
||||
|
||||
#define MAX_ENTRIES 64
|
||||
|
||||
#define READ_MULTI_LINE(name, s_name, num, mand) \
|
||||
val = optionGetValue(pov, name); \
|
||||
if (val != NULL && val->valType == OPARG_TYPE_STRING) { \
|
||||
if (s_name == NULL) { \
|
||||
num = 0; \
|
||||
s_name = malloc(sizeof(char*)*MAX_ENTRIES); \
|
||||
do { \
|
||||
if (val && !strcmp(val->pzName, name)==0) \
|
||||
continue; \
|
||||
s_name[num] = strdup(val->v.strVal); \
|
||||
num++; \
|
||||
if (num>=MAX_ENTRIES) \
|
||||
break; \
|
||||
} while((val = optionNextValue(pov, val)) != NULL); \
|
||||
s_name[num] = NULL; \
|
||||
} \
|
||||
} else if (mand != 0) { \
|
||||
fprintf(stderr, "Configuration option %s is mandatory.\n", name); \
|
||||
}
|
||||
|
||||
#define READ_BOOLEAN(name, s_name, mand) \
|
||||
val = optionGetValue(pov, name); \
|
||||
if (val != NULL) { \
|
||||
s_name = 1; \
|
||||
} else if (mand != 0) { \
|
||||
fprintf(stderr, "Configuration option %s is mandatory.\n", name); \
|
||||
}
|
||||
|
||||
|
||||
#define READ_STRING(name, s_name, mand) \
|
||||
val = optionGetValue(pov, name); \
|
||||
if (val != NULL && val->valType == OPARG_TYPE_STRING) \
|
||||
s_name = strdup(val->v.strVal); \
|
||||
else if (mand != 0) { \
|
||||
fprintf(stderr, "Configuration option %s is mandatory.\n", name); \
|
||||
}
|
||||
|
||||
#define READ_NUMERIC(name, s_name, mand) \
|
||||
val = optionGetValue(pov, name); \
|
||||
if (val != NULL) { \
|
||||
if (val->valType == OPARG_TYPE_NUMERIC) \
|
||||
s_name = val->v.longVal; \
|
||||
else if (val->valType == OPARG_TYPE_STRING) \
|
||||
s_name = atoi(val->v.strVal); \
|
||||
} else if (mand != 0) { \
|
||||
fprintf(stderr, "Configuration option %s is mandatory.\n", name); \
|
||||
}
|
||||
|
||||
|
||||
static void parse_cfg_file(const char* file, struct cfg_st *config)
|
||||
{
|
||||
tOptionValue const * pov;
|
||||
const tOptionValue* val;
|
||||
char tmpstr[256];
|
||||
char** auth;
|
||||
unsigned auth_size;
|
||||
unsigned j;
|
||||
|
||||
pov = configFileLoad(file);
|
||||
if (pov == NULL) {
|
||||
fprintf(stderr, "Error loading config file %s\n", file);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
READ_MULTI_LINE("auth", auth, auth_size, 1);
|
||||
for (j=0;j<auth_size;j++) {
|
||||
if (strcasecmp(auth[j], "pam") == 0) {
|
||||
config->auth_types |= AUTH_TYPE_PAM;
|
||||
} else if (strcasecmp(auth[j], "certificate") == 0) {
|
||||
config->auth_types |= AUTH_TYPE_CERTIFICATE;
|
||||
config->cert_req = GNUTLS_CERT_REQUIRE;
|
||||
} else {
|
||||
fprintf(stderr, "Unknown auth method: %s\n", auth[j]);
|
||||
exit(1);
|
||||
}
|
||||
free(auth[j]);
|
||||
}
|
||||
free(auth);
|
||||
|
||||
READ_STRING("listen-host", config->name, 0);
|
||||
|
||||
READ_NUMERIC("tcp-port", config->port, 1);
|
||||
READ_NUMERIC("udp-port", config->udp_port, 1);
|
||||
|
||||
READ_STRING("server-cert", config->cert, 1);
|
||||
READ_STRING("server-key", config->key, 1);
|
||||
|
||||
READ_STRING("ca-cert", config->ca, 0);
|
||||
READ_STRING("crl", config->crl, 0);
|
||||
READ_STRING("cert-user-oid", config->cert_user_oid, 0);
|
||||
|
||||
READ_STRING("tls-priorities", config->priorities, 0);
|
||||
READ_STRING("chroot-dir", config->chroot_dir, 0);
|
||||
|
||||
READ_NUMERIC("cookie-validity", config->cookie_validity, 1);
|
||||
READ_STRING("cookie-db", config->db_file, 1);
|
||||
|
||||
val = optionGetValue(pov, "run-as-user"); \
|
||||
if (val != NULL && val->valType == OPARG_TYPE_STRING) {
|
||||
const struct passwd* pwd = getpwnam(val->v.strVal);
|
||||
if (pwd == NULL) {
|
||||
fprintf(stderr, "Unknown user: %s\n", val->v.strVal);
|
||||
exit(1);
|
||||
}
|
||||
config->uid = pwd->pw_uid;
|
||||
}
|
||||
|
||||
val = optionGetValue(pov, "run-as-group"); \
|
||||
if (val != NULL && val->valType == OPARG_TYPE_STRING) {
|
||||
const struct group *grp = getgrnam(val->v.strVal);
|
||||
if (grp == NULL) {
|
||||
fprintf(stderr, "Unknown group: %s\n", val->v.strVal);
|
||||
exit(1);
|
||||
}
|
||||
config->gid = grp->gr_gid;
|
||||
}
|
||||
|
||||
READ_STRING("device", config->network.name, 1);
|
||||
|
||||
READ_STRING("ipv4-network", config->network.ipv4, 0);
|
||||
READ_STRING("ipv4-netmask", config->network.ipv4_netmask, 0);
|
||||
READ_STRING("ipv4-dns", config->network.ipv4_dns, 0);
|
||||
|
||||
READ_STRING("ipv6-network", config->network.ipv6, 0);
|
||||
READ_STRING("ipv6-netmask", config->network.ipv6_netmask, 0);
|
||||
READ_STRING("ipv6-dns", config->network.ipv6_dns, 0);
|
||||
|
||||
READ_MULTI_LINE("route", config->network.routes, config->network.routes_size, 0);
|
||||
|
||||
}
|
||||
|
||||
int cmd_parser (int argc, char **argv, struct cfg_st* config)
|
||||
{
|
||||
@@ -42,8 +178,12 @@ int cmd_parser (int argc, char **argv, struct cfg_st* config)
|
||||
if (HAVE_OPT(CONFIG)) {
|
||||
cfg_file = OPT_ARG(CONFIG);
|
||||
} else {
|
||||
fprintf(stderr, "Please use the -c option to specify a configuration file, or --help for more information.\n");
|
||||
fprintf(stderr, "%s -c [config]\nUse %s --help for more information.\n", argv[0], argv[0]);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
parse_cfg_file(cfg_file, config);
|
||||
|
||||
return 0;
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user