/* * Usurpation – server daemon settings handling * * Copyright (C) 2019 Gediminas Jakutis * Copyright (C) 2019 Ramūnas Mažeikis * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; version 2.1 * of the License. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA */ #define _GNU_SOURCE #include #include #include #include #include #include #include #include #include "settings.h" #include "settings_private.h" /* TODO: should be, in this order: looking at options provided via command * line arguments, looking at at /etc/conf.d/usurpation.conf or * using default. Right now just uses defaults. */ void settings_init(void) { atexit(free_strings); unset_flag(flag_daemonize); settings.port = 6996; settings.verboselevel = USURP_VERBOSITY; settings.progname = strdup(program_invocation_short_name); settings.im_user = strdup("user"); settings.im_password = strdup("password"); settings.im_proto = strdup("prpl-irc"); #define macro2str(a) _macro2str(a) #define _macro2str(a) #a setting_readconf("/" macro2str(SYSCONFDIR) "/usurpation.conf"); #undef macro2str #undef _macro2str } int setting_detach(void) { return test_flag(flag_daemonize); } int setting_verbose(void) { return settings.verboselevel; } unsigned short int setting_port(void) { return settings.port; } char *setting_progname(void) { return strdup(settings.progname); } char *setting_im_user(void) { return strdup(settings.im_user); } char *setting_im_password(void) { return strdup(settings.im_password); } char *setting_im_proto(void) { return strdup(settings.im_proto); } /* could be a one-liner, but let's make the logic more obvious */ static int test_flag(unsigned int flag) { int ret; ret = settings.flags & flag; ret = !!ret; return ret; } static void set_flag(unsigned int flag) { settings.flags |= flag; } static void unset_flag(unsigned int flag) { settings.flags &= ~flag; } /* TODO: semi-stub! */ static int setting_readconf(const char * const path) { struct stat filestat; char *buf; char *line; char *entry; char *value; char *line_state; char *token_state; int fd; if ((fd = open(path, O_RDONLY)) == -1) { if (USURP_VERBOSITY >= 2) { perror("error while opening the configuration file"); return 1; } } fstat(fd, &filestat); buf = malloc(filestat.st_size+1); buf[filestat.st_size] = 0; if (read(fd, buf, filestat.st_size) != filestat.st_size) { if (USURP_VERBOSITY >= 2) { perror("partial read of configuration file detected"); free(buf); return 1; } } line = strtok_r(buf, "\n", &line_state); while (line) { if (line[0] != '#') { entry = strtok_r(line, "=", &token_state); value = strtok_r(NULL, "=", &token_state); setting_handle_config_entry(entry, value); } line = strtok_r(NULL, "\n", &line_state); } free(buf); return 0; } static int setting_handle_config_entry(const char * const entry, const char * const value) { size_t i; for (i = 0; i < sizeof(ent_table)/sizeof(*ent_table); ++i) { if (!(strcmp(entry, ent_table[i].name))) { ent_table[i].set(value); return 0; } } return 1; } static void set_daemonize(const void * const arg) { const char * const a = arg; atoi(a) ? set_flag(flag_daemonize) : unset_flag(flag_daemonize); } static void set_port(const void * const arg) { const char * const a = arg; settings.port = strtoul(a, NULL, 0); } static void set_verbosity(const void * const arg) { const char * const a = arg; settings.verboselevel = strtol(a, NULL, 0); } static void set_progname(const void * const arg) { const char * const a = arg; free(settings.progname); settings.progname = strdup(a); } static void set_im_user(const void * const arg) { const char * const a = arg; free(settings.im_user); settings.im_user = strdup(a); } static void set_im_password(const void * const arg) { const char * const a = arg; free(settings.im_password); settings.im_password = strdup(a); } static void set_im_proto(const void * const arg) { const char * const a = arg; free(settings.im_proto); settings.im_proto = strdup(a); } static void free_strings(void) { free(settings.progname); free(settings.im_user); free(settings.im_password); free(settings.im_proto); }