]>
Commit | Line | Data |
---|---|---|
1 | #!/usr/bin/python3 | |
2 | ############################################################################### | |
3 | # # | |
4 | # ddns - A dynamic DNS client for IPFire # | |
5 | # Copyright (C) 2012 IPFire development team # | |
6 | # # | |
7 | # This program is free software: you can redistribute it and/or modify # | |
8 | # it under the terms of the GNU General Public License as published by # | |
9 | # the Free Software Foundation, either version 3 of the License, or # | |
10 | # (at your option) any later version. # | |
11 | # # | |
12 | # This program is distributed in the hope that it will be useful, # | |
13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of # | |
14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # | |
15 | # GNU General Public License for more details. # | |
16 | # # | |
17 | # You should have received a copy of the GNU General Public License # | |
18 | # along with this program. If not, see <http://www.gnu.org/licenses/>. # | |
19 | # # | |
20 | ############################################################################### | |
21 | ||
22 | import logging | |
23 | import logging.handlers | |
24 | import configparser | |
25 | ||
26 | from .i18n import _ | |
27 | ||
28 | logger = logging.getLogger("ddns.core") | |
29 | logger.propagate = 1 | |
30 | ||
31 | from . import database | |
32 | from . import providers | |
33 | ||
34 | from .errors import * | |
35 | from .system import DDNSSystem | |
36 | ||
37 | # Setup the logger. | |
38 | def setup_logging(): | |
39 | rootlogger = logging.getLogger("ddns") | |
40 | rootlogger.setLevel(logging.INFO) | |
41 | ||
42 | # Setup a logger that logs to syslog. | |
43 | handler = logging.handlers.SysLogHandler(address="/dev/log", | |
44 | facility=logging.handlers.SysLogHandler.LOG_DAEMON | |
45 | ) | |
46 | formatter = logging.Formatter("ddns[%(process)d]: %(message)s") | |
47 | handler.setFormatter(formatter) | |
48 | handler.setLevel(logging.INFO) | |
49 | rootlogger.addHandler(handler) | |
50 | ||
51 | handler = logging.StreamHandler() | |
52 | rootlogger.addHandler(handler) | |
53 | ||
54 | setup_logging() | |
55 | ||
56 | class DDNSCore(object): | |
57 | def __init__(self, debug=False): | |
58 | # In debug mode, enable debug logging. | |
59 | if debug: | |
60 | rootlogger = logging.getLogger("ddns") | |
61 | rootlogger.setLevel(logging.DEBUG) | |
62 | ||
63 | logger.debug(_("Debugging mode enabled")) | |
64 | ||
65 | # Initialize the settings array. | |
66 | self.settings = {} | |
67 | ||
68 | # Dict with all providers, that are supported. | |
69 | self.providers = providers.get() | |
70 | ||
71 | for handle, provider in sorted(self.providers.items()): | |
72 | logger.debug("Registered new provider: %s (%s)" % (provider.name, provider.handle)) | |
73 | ||
74 | # List of configuration entries. | |
75 | self.entries = [] | |
76 | ||
77 | # Add the system class. | |
78 | self.system = DDNSSystem(self) | |
79 | ||
80 | # Open the database. | |
81 | self.db = database.DDNSDatabase(self, "/var/lib/ddns.db") | |
82 | ||
83 | def get_provider_names(self): | |
84 | """ | |
85 | Returns a list of names of all registered providers. | |
86 | """ | |
87 | return sorted(self.providers.keys()) | |
88 | ||
89 | def get_provider_with_token_support(self): | |
90 | """ | |
91 | Returns a list with names of all registered providers | |
92 | which support token based authtentication. | |
93 | """ | |
94 | ||
95 | token_provider = [] | |
96 | ||
97 | for handle, provider in sorted(self.providers.items()): | |
98 | if provider.supports_token_auth is True: | |
99 | token_provider.append(handle) | |
100 | ||
101 | return sorted(token_provider) | |
102 | ||
103 | def load_configuration(self, filename): | |
104 | logger.debug(_("Loading configuration file %s") % filename) | |
105 | ||
106 | configs = configparser.RawConfigParser() | |
107 | configs.read([filename,]) | |
108 | ||
109 | # First apply all global configuration settings. | |
110 | try: | |
111 | for k, v in configs.items("config"): | |
112 | self.settings[k] = v | |
113 | ||
114 | # Allow missing config section | |
115 | except configparser.NoSectionError: | |
116 | pass | |
117 | ||
118 | for entry in configs.sections(): | |
119 | # Skip the special config section. | |
120 | if entry == "config": | |
121 | continue | |
122 | ||
123 | settings = {} | |
124 | for k, v in configs.items(entry): | |
125 | settings[k] = v | |
126 | settings["hostname"] = entry | |
127 | ||
128 | # Get the name of the provider. | |
129 | provider = settings.get("provider", None) | |
130 | if not provider: | |
131 | logger.warning("Entry '%s' lacks a provider setting. Skipping." % entry) | |
132 | continue | |
133 | ||
134 | # Try to find the provider with the wanted name. | |
135 | try: | |
136 | provider = self.providers[provider] | |
137 | except KeyError: | |
138 | logger.warning("Could not find provider '%s' for entry '%s'." % (provider, entry)) | |
139 | continue | |
140 | ||
141 | # Check if the provider is actually supported and if there are | |
142 | # some dependencies missing on this system. | |
143 | if not provider.supported(): | |
144 | logger.warning("Provider '%s' is known, but not supported on this machine" % provider.name) | |
145 | continue | |
146 | ||
147 | # Create an instance of the provider object with settings from the | |
148 | # configuration file. | |
149 | entry = provider(self, **settings) | |
150 | ||
151 | # Add new entry to list (if not already exists). | |
152 | if not entry in self.entries: | |
153 | self.entries.append(entry) | |
154 | ||
155 | def updateone(self, hostname, **kwargs): | |
156 | for entry in self.entries: | |
157 | if not entry.hostname == hostname: | |
158 | continue | |
159 | ||
160 | return self._update(entry, **kwargs) | |
161 | ||
162 | raise DDNSHostNotFoundError(hostname) | |
163 | ||
164 | def updateall(self, **kwargs): | |
165 | """ | |
166 | Update all configured entries. | |
167 | """ | |
168 | # If there are no entries, there is nothing to do. | |
169 | if not self.entries: | |
170 | logger.debug(_("Found no entries in the configuration file. Exiting.")) | |
171 | return | |
172 | ||
173 | for entry in self.entries: | |
174 | self._update(entry, **kwargs) | |
175 | ||
176 | def _update(self, entry, force=False): | |
177 | try: | |
178 | entry(force=force) | |
179 | ||
180 | except DDNSError as e: | |
181 | logger.error(_("Dynamic DNS update for %(hostname)s (%(provider)s) failed:") % | |
182 | {"hostname": entry.hostname, "provider": entry.name}) | |
183 | logger.error(" %s: %s" % (e.__class__.__name__, e.reason)) | |
184 | if e.message: | |
185 | logger.error(" %s" % e.message) | |
186 | ||
187 | except Exception: | |
188 | logger.error(_("Dynamic DNS update for %(hostname)s (%(provider)s) threw an unhandled exception:") % | |
189 | {"hostname": entry.hostname, "provider": entry.name}, exc_info=True) |