From 6e69ff706bca53c5d5b9be5b294904e2fee93557 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Obrembski?= Date: Fri, 22 Jan 2021 12:28:12 +0100 Subject: [PATCH] Added daemon run mode --- src/config.c | 12 ++++++++++++ src/config.h | 1 + src/main.c | 24 +++++++++++++++++++++++- 3 files changed, 36 insertions(+), 1 deletion(-) diff --git a/src/config.c b/src/config.c index 2592a22e..63f5b4cf 100644 --- a/src/config.c +++ b/src/config.c @@ -86,6 +86,7 @@ const struct vpn_config invalid_cfg = { .user_agent = NULL, .hostcheck = NULL, .check_virtual_desktop = NULL, + .daemonize = 0 }; /* @@ -448,6 +449,15 @@ int load_config(struct vpn_config *cfg, const char *filename) } else if (strcmp(key, "check-virtual-desktop") == 0) { free(cfg->check_virtual_desktop); cfg->check_virtual_desktop = strdup(val); + } else if (strcmp(key, "daemonize") == 0) { + int daemonize = strtob(val); + + if (daemonize < 0) { + log_warn("Bad daemonize in config file: \"%s\".\n", + val); + continue; + } + cfg->daemonize = daemonize; } else { log_warn("Bad key in configuration file: \"%s\".\n", key); goto err_close; @@ -613,4 +623,6 @@ void merge_config(struct vpn_config *dst, struct vpn_config *src) dst->hostcheck = src->hostcheck; if (src->check_virtual_desktop != invalid_cfg.check_virtual_desktop) dst->check_virtual_desktop = src->check_virtual_desktop; + if (src->daemonize != invalid_cfg.daemonize) + dst->daemonize = src->daemonize; } diff --git a/src/config.h b/src/config.h index f7783e1f..b5af03a5 100644 --- a/src/config.h +++ b/src/config.h @@ -132,6 +132,7 @@ struct vpn_config { char *user_agent; char *hostcheck; char *check_virtual_desktop; + int daemonize; }; int add_trusted_cert(struct vpn_config *cfg, const char *digest); diff --git a/src/main.c b/src/main.c index 1a12e519..2414936c 100644 --- a/src/main.c +++ b/src/main.c @@ -143,7 +143,8 @@ PPPD_USAGE \ " certificate will be matched against this value.\n" \ " is the X509 certificate's sha256 sum.\n" \ " This option can be used multiple times to trust\n" \ -" several certificates.\n" +" several certificates.\n" \ +" --daemonize Run in daemon mode.\n" #define help_options_part2 \ " --insecure-ssl Do not disable insecure SSL protocols/ciphers.\n" \ @@ -189,6 +190,7 @@ int main(int argc, char **argv) const char *config_file = SYSCONFDIR "/openfortivpn/config"; const char *host; char *port_str; + pid_t process_id = 0; struct vpn_config cfg = { .gateway_host = {'\0'}, @@ -208,6 +210,7 @@ int main(int argc, char **argv) .use_syslog = 0, .half_internet_routes = 0, .persistent = 0, + .daemonize = 0, #if HAVE_RESOLVCONF .use_resolvconf = USE_RESOLVCONF, #endif @@ -270,6 +273,7 @@ int main(int argc, char **argv) {"cipher-list", required_argument, NULL, 0}, {"min-tls", required_argument, NULL, 0}, {"seclevel-1", no_argument, &cli_cfg.seclevel_1, 1}, + {"daemonize", no_argument, &cli_cfg.daemonize, 1}, #if HAVE_USR_SBIN_PPPD {"pppd-use-peerdns", required_argument, NULL, 0}, {"pppd-no-peerdns", no_argument, &cli_cfg.pppd_use_peerdns, 0}, @@ -580,6 +584,24 @@ int main(int argc, char **argv) // Then apply CLI configuration merge_config(&cfg, &cli_cfg); + if (cfg.daemonize) { + if (cfg.use_syslog == 0) { + log_info("Sorry, only syslog is available when running in Daemon mode"); + cfg.use_syslog = 1; + } + process_id = fork(); + // Indication of fork() failure + if (process_id < 0) { + printf("Forking failure! Cannot start daemon!\n"); + exit(1); + } + // PARENT PROCESS. Need to kill it. + if (process_id > 0) { + printf("Started as daemon with PID: %u\n", process_id); + /* Killing parent process */ + exit(0); + } + } set_syslog(cfg.use_syslog); // Set default UA