The config class.
[bpdyndnsd] / src / config.cpp
1 //
2 // C++ Implementation: config
3 //
4 // Description: 
5 //
6 //
7 // Author: Bjoern Sikora <bjoern.sikora@intra2net.com>, (C) 2009
8 //
9 // Copyright: See COPYING file that comes with this distribution
10 //
11 //
12 #include "config.h"
13
14
15 // Define allowed command line and config file options
16 Config::Config()
17 {
18     // Define valid command line parameters
19     opt_desc_cmd = new po::options_description("Command line options");
20     opt_desc_cmd->add_options()
21         ("help,?","Show help.")
22         ("version,v","Show version.")
23         ("protocol,q",po::value<string>(),"Set the service protocol type.")
24         ("host,h",po::value<string>(),"Set the hostname to update.")
25         ("login,l",po::value<string>(),"Set the login.")
26         ("password,p",po::value<string>(),"Set the password.")
27     ;
28
29     // Define valid config file main section parameters
30     opt_desc_conf_main = new po::options_description("Config file main section options");
31     opt_desc_conf_main->add_options()
32         ("main.daemon_mode",po::value<bool>()->default_value(false),"Run as system daemon.")
33         ("main.logfile",po::value<string>()->default_value("/var/log/bpdyndns.log"),"Where to log.")
34         ("main.loglevel",po::value<int>()->default_value(0),"Loglevel.")
35         ("main.syslog",po::value<bool>()->default_value(false),"Use syslog facility.")
36     ;
37
38     // Define valid config file main section parameters
39     opt_desc_conf_service = new po::options_description("Config file service section options");
40     opt_desc_conf_service->add_options()
41         ("service.protocol",po::value<string>(),"The service protocol.")
42         ("service.host",po::value<string>(),"The hostname to update.")
43         ("service.login",po::value<string>(),"Login name.")
44         ("service.password",po::value<string>(),"Corresponding password.")
45     ;
46 }
47
48
49 Config::~Config()
50 {
51     delete opt_desc_cmd;
52     delete opt_desc_conf_main;
53     delete opt_desc_conf_service;
54 }
55
56
57 int Config::parse_cmd_line(int argc, char *argv[])
58 {
59     try
60     {
61         po::variables_map vm;
62         po::store(po::parse_command_line(argc, argv, *this->opt_desc_cmd), vm);
63         po::notify(vm);
64
65         if(vm.count("help"))
66             return 1;
67         else if(vm.count("version"))
68             return 2;
69
70         // Are all needed options set to create a Service Object
71         if(vm.count("protocol") && vm.count("host") && vm.count("login") && vm.count("password"))
72         {
73             // Get the cmd parameter values for protocol host login and password
74             string protocol = vm["protocol"].as<string>();
75             string host = vm["host"].as<string>();
76             string login = vm["login"].as<string>();
77             string password = vm["password"].as<string>();
78
79             //TODO: convert protocol option to lowercase
80
81             // Create corresponding Service object TODO: swap to extra method
82             if(protocol == "dhs")
83             {
84                 Service * service = new DHS(host,login,password);
85                 this->services.push_back(service);
86             }
87             else if(protocol == "ods")
88             {
89                 Service * service = new ODS(host,login,password);
90                 this->services.push_back(service);
91             }
92         }
93         else
94         {
95             cout << "Not all needed options set!\n" << endl;
96             return 1;
97         }
98     }
99     catch(po::unknown_option e)
100     {
101         cout << "Unknown option set." << endl;
102         return 1;
103     }
104     return 0;
105 }
106
107
108 int Config::load_config_from_files(string config_path)
109 {
110     // first load the main config file bpdyndnsd.conf
111     string main_conf_file = config_path.append("/bpdyndnsd.conf");
112     ifstream config_file (main_conf_file.c_str(),ifstream::in);
113     if(config_file.is_open())
114     {
115         try
116         {
117             // parse the config file
118             po::variables_map vm;
119             po::parsed_options parsed = po::parse_config_file(config_file,*this->opt_desc_conf_main,true);
120             po::store(parsed,vm);
121             po::notify(vm);
122
123             if(vm.count("daemon_mode") && vm.count("logfile") && vm.count("loglevel") && vm.count("syslog"))
124             {
125                 // Get the options from main section
126                 this->daemon_mode = vm["main.daemon_mode"].as<bool>();
127                 this->logfile = vm["main.logfile"].as<string>();
128                 this->loglevel = vm["main.loglevel"].as<int>();
129                 this->syslog = vm["main.syslog"].as<bool>();
130             }
131         }
132         catch(po::unknown_option e)
133         {
134             // unknown option in config file detected
135             config_file.close();
136             cout << "Unknown option in config file detected!" << endl;
137             return 3;
138         }
139         config_file.close();
140     }
141     else
142     {
143         cout << "Can't open main config file for reading: " << main_conf_file << endl;
144         return 4;
145     }
146
147     // then load all service definition files in config path
148
149     // TODO: code to load service definition files in config path
150 }
151
152
153 list<Service*> Config::get_services()
154 {
155     return this->services;
156 }
157
158
159 void Config::print_usage()
160 {
161     cout << "Usage: bpdyndnsd [Command line options]" << "\n" << endl;
162     cout << *opt_desc_cmd << endl;
163 }
164
165
166 void Config::print_version(string version)
167 {
168     cout << "Bullet proof dynamic dns daemon.\nbpdyndnsd " << version << endl;
169 }