Skip to content

Commit

Permalink
Added daemon run mode
Browse files Browse the repository at this point in the history
  • Loading branch information
mobrembski authored and DimitriPapadopoulos committed Nov 18, 2021
1 parent d1b2801 commit 6e69ff7
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
12 changes: 12 additions & 0 deletions src/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ const struct vpn_config invalid_cfg = {
.user_agent = NULL,
.hostcheck = NULL,
.check_virtual_desktop = NULL,
.daemonize = 0
};

/*
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
1 change: 1 addition & 0 deletions src/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
24 changes: 23 additions & 1 deletion src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,8 @@ PPPD_USAGE \
" certificate will be matched against this value.\n" \
" <digest> 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" \
Expand Down Expand Up @@ -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'},
Expand All @@ -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
Expand Down Expand Up @@ -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},
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 6e69ff7

Please sign in to comment.