dynamicdns.py 14 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
#
# This file is part of Plinth.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

from django import forms
from django.contrib import messages
from django.core import validators
from django.core.urlresolvers import reverse_lazy
22
from django.utils.translation import ugettext as _, ugettext_lazy
23 24 25 26 27
from django.template.response import TemplateResponse
import logging

from plinth import actions
from plinth import cfg
28
from plinth import package
29
from plinth.utils import format_lazy
30

31
logger = logging.getLogger(__name__)
32
EMPTYSTRING = 'none'
33

34
subsubmenu = [{'url': reverse_lazy('dynamicdns:index'),
35
               'text': ugettext_lazy('About')},
36
              {'url': reverse_lazy('dynamicdns:configure'),
37
               'text': ugettext_lazy('Configure')},
38
              {'url': reverse_lazy('dynamicdns:statuspage'),
39
               'text': ugettext_lazy('Status')}]
40

41

42
def init():
43
    """Initialize the dynamicdns module"""
44
    menu = cfg.main_menu.get('apps:index')
45
    menu.add_urlname(ugettext_lazy('Dynamic DNS'), 'glyphicon-refresh',
46
                     'dynamicdns:index', 500)
47

48

49
@package.required(['ez-ipupdate'])
50
def index(request):
51
    """Serve Dynamic DNS page."""
52

53
    return TemplateResponse(request, 'dynamicdns.html',
54
                            {'title': _('Dynamic DNS'),
55
                             'subsubmenu': subsubmenu})
56

57

58
class TrimmedCharField(forms.CharField):
59
    """Trim the contents of a CharField."""
60 61 62 63 64 65
    def clean(self, value):
        """Clean and validate the field value"""
        if value:
            value = value.strip()

        return super(TrimmedCharField, self).clean(value)
66 67


68
class ConfigureForm(forms.Form):
69 70 71 72 73 74 75 76 77 78 79 80 81
    """Form to configure the Dynamic DNS client."""
    help_update_url = \
        ugettext_lazy('The Variables &lt;User&gt;, &lt;Pass&gt;, &lt;Ip&gt;, '
                      '&lt;Domain&gt; may be used within the URL. For details '
                      'see the update URL templates of the example providers.')
    help_services = \
        ugettext_lazy('Please choose an update protocol according to your '
                      'provider. If your provider does not support the GnudIP '
                      'protocol or your provider is not listed you may use the '
                      'update URL of your provider.')
    help_server = \
        ugettext_lazy('Please do not enter a URL here (like '
                      '"https://example.com/") but only the hostname of the '
82 83 84 85
                      'GnuDIP server (like "example.com").')
    help_domain = format_lazy(
        ugettext_lazy('The public domain name you want use to reach your '
                      '{box_name}.'), box_name=ugettext_lazy(cfg.box_name))
86 87 88 89 90 91 92 93 94
    help_disable_ssl = \
        ugettext_lazy('Use this option if your provider uses self signed '
                      'certificates.')
    help_http_auth = \
        ugettext_lazy('If this option is selected, your username and password '
                      'will be used for HTTP basic authentication.')
    help_secret = \
        ugettext_lazy('Leave this field empty if you want to keep your '
                      'previous configured password.')
95 96
    help_ip_url = format_lazy(
        ugettext_lazy('Optional Value. If your {box_name} is not connected '
97 98
                      'directly to the Internet (i.e. connected to a NAT '
                      'router) this URL is used to figure out the real '
99 100 101 102
                      'Internet IP. The URL should simply return the IP where '
                      'the client comes from (example: '
                      'http://myip.datasystems24.de).'),
        box_name=ugettext_lazy(cfg.box_name))
103 104 105
    help_user = \
        ugettext_lazy('You should have been requested to select a username '
                      'when you created the account.')
106 107 108

    """ToDo: sync this list with the html template file"""
    provider_choices = (
109 110 111 112 113
        ('GnuDIP', 'GnuDIP'),
        ('noip', 'noip.com'),
        ('selfhost', 'selfhost.bz'),
        ('freedns', 'freedns.afraid.org'),
        ('other', 'other update URL'))
114

115
    enabled = forms.BooleanField(label=ugettext_lazy('Enable Dynamic DNS'),
116
                                 required=False)
117

118 119
    service_type = forms.ChoiceField(label=ugettext_lazy('Service type'),
                                     help_text=help_services,
120
                                     choices=provider_choices)
121

122
    dynamicdns_server = TrimmedCharField(
123
        label=ugettext_lazy('GnudIP Server Address'),
124
        required=False,
125
        help_text=help_server,
126 127
        validators=[
            validators.RegexValidator(r'^[\w-]{1,63}(\.[\w-]{1,63})*$',
128
                                      ugettext_lazy('Invalid server name'))])
129

130 131 132
    dynamicdns_update_url = TrimmedCharField(
        label=ugettext_lazy('Update URL'), required=False,
        help_text=help_update_url)
133

134
    disable_SSL_cert_check = forms.BooleanField(
135
        label=ugettext_lazy('Accept all SSL certificates'),
136
        help_text=help_disable_ssl, required=False)
137

138
    use_http_basic_auth = forms.BooleanField(
139
        label=ugettext_lazy('Use HTTP basic authentication'),
140
        help_text=help_http_auth, required=False)
141

142
    dynamicdns_domain = TrimmedCharField(
143 144
        label=ugettext_lazy('Domain Name'),
        help_text=help_domain,
145
        required=False,
146 147
        validators=[
            validators.RegexValidator(r'^[\w-]{1,63}(\.[\w-]{1,63})*$',
148
                                      ugettext_lazy('Invalid domain name'))])
149

150
    dynamicdns_user = TrimmedCharField(
151
        label=ugettext_lazy('Username'), required=False, help_text=help_user)
152

153
    dynamicdns_secret = TrimmedCharField(
154 155
        label=ugettext_lazy('Password'), widget=forms.PasswordInput(),
        required=False, help_text=help_secret)
156

157
    showpw = forms.BooleanField(label=ugettext_lazy('Show password'),
158
                                required=False)
159

160
    dynamicdns_ipurl = TrimmedCharField(
161
        label=ugettext_lazy('IP check URL'),
162
        required=False,
163
        help_text=help_ip_url,
164
        validators=[
165 166
            validators.URLValidator(schemes=['http', 'https', 'ftp'])])

167 168
    def clean(self):
        cleaned_data = super(ConfigureForm, self).clean()
169
        dynamicdns_secret = cleaned_data.get('dynamicdns_secret')
170
        dynamicdns_update_url = cleaned_data.get('dynamicdns_update_url')
171 172
        dynamicdns_user = cleaned_data.get('dynamicdns_user')
        dynamicdns_domain = cleaned_data.get('dynamicdns_domain')
173
        dynamicdns_server = cleaned_data.get('dynamicdns_server')
174
        service_type = cleaned_data.get('service_type')
175
        old_dynamicdns_secret = self.initial['dynamicdns_secret']
176

177
        # Clear the fields which are not in use
178
        if service_type == 'GnuDIP':
179
            dynamicdns_update_url = ''
180
        else:
181
            dynamicdns_server = ''
182

183
        if cleaned_data.get('enabled'):
184
            # Check if gnudip server or update URL is filled
185
            if not dynamicdns_update_url and not dynamicdns_server:
186 187
                raise forms.ValidationError(
                    _('Please provide update URL or a GnuDIP Server'))
188

189
            if dynamicdns_server and not dynamicdns_user:
190
                raise forms.ValidationError(_('Please provide GnuDIP username'))
191 192

            if dynamicdns_server and not dynamicdns_domain:
193
                raise forms.ValidationError(_('Please provide GnuDIP domain'))
194

195 196 197 198
            # Check if a password was set before or a password is set now
            if dynamicdns_server and \
               not dynamicdns_secret and not old_dynamicdns_secret:
                raise forms.ValidationError(_('Please provide a password'))
199

200

201
@package.required(['ez-ipupdate'])
202
def configure(request):
203
    """Serve the configuration form."""
204 205 206 207
    status = get_status()
    form = None

    if request.method == 'POST':
208
        form = ConfigureForm(request.POST, initial=status)
209 210 211
        if form.is_valid():
            _apply_changes(request, status, form.cleaned_data)
            status = get_status()
212
            form = ConfigureForm(initial=status)
213
    else:
214
        form = ConfigureForm(initial=status)
215

216
    return TemplateResponse(request, 'dynamicdns_configure.html',
217
                            {'title': _('Configure Dynamic DNS'),
218 219 220
                             'form': form,
                             'subsubmenu': subsubmenu})

221

222
@package.required(['ez-ipupdate'])
223
def statuspage(request):
224
    """Serve the status page."""
225 226
    check_nat = actions.run('dynamicdns', ['get-nat'])
    last_update = actions.run('dynamicdns', ['get-last-success'])
227

228 229
    no_nat = check_nat.strip() == 'no'
    nat_unchecked = check_nat.strip() == 'unknown'
230
    timer = actions.run('dynamicdns', ['get-timer'])
231

232
    if no_nat:
233
        logger.info('Not behind a NAT')
234

235
    if nat_unchecked:
236
        logger.info('Did not check if we are behind a NAT')
237

238
    return TemplateResponse(request, 'dynamicdns_status.html',
239
                            {'title': _('Status of Dynamic DNS'),
240 241
                             'no_nat': no_nat,
                             'nat_unchecked': nat_unchecked,
242 243
                             'timer': timer,
                             'last_update': last_update,
244 245
                             'subsubmenu': subsubmenu})

246

247
def get_status():
248 249
    """Return the current status."""
    # TODO: use key/value instead of hard coded value list
250
    status = {}
251
    output = actions.run('dynamicdns', ['status'])
252 253
    details = output.split()
    status['enabled'] = (output.split()[0] == 'enabled')
254

255
    if len(details) > 1:
256 257 258
        if details[1] == 'disabled':
            status['dynamicdns_server'] = ''
        else:
259
            status['dynamicdns_server'] = details[1].replace("'", "")
260
    else:
261
        status['dynamicdns_server'] = ''
262

263
    if len(details) > 2:
264 265 266
        if details[2] == 'disabled':
            status['dynamicdns_domain'] = ''
        else:
267
            status['dynamicdns_domain'] = details[2].replace("'", "")
268
    else:
269
        status['dynamicdns_domain'] = ''
270

271
    if len(details) > 3:
272 273 274
        if details[3] == 'disabled':
            status['dynamicdns_user'] = ''
        else:
275
            status['dynamicdns_user'] = details[3].replace("'", "")
276
    else:
277
        status['dynamicdns_user'] = ''
278

279
    if len(details) > 4:
280 281 282
        if details[4] == 'disabled':
            status['dynamicdns_secret'] = ''
        else:
283
            status['dynamicdns_secret'] = details[4].replace("'", "")
284
    else:
285
        status['dynamicdns_secret'] = ''
286

287
    if len(details) > 5:
288 289 290
        if details[5] == 'disabled':
            status['dynamicdns_ipurl'] = ''
        else:
291
            status['dynamicdns_ipurl'] = details[5].replace("'", "")
292
    else:
293
        status['dynamicdns_ipurl'] = ''
294

295
    if len(details) > 6:
296 297 298
        if details[6] == 'disabled':
            status['dynamicdns_update_url'] = ''
        else:
299
            status['dynamicdns_update_url'] = details[6].replace("'", "")
300 301
    else:
        status['dynamicdns_update_url'] = ''
302

303
    if len(details) > 7:
304
        status['disable_SSL_cert_check'] = (output.split()[7] == 'enabled')
305
    else:
306
        status['disable_SSL_cert_check'] = False
307

308
    if len(details) > 8:
309
        status['use_http_basic_auth'] = (output.split()[8] == 'enabled')
310
    else:
311
        status['use_http_basic_auth'] = False
312

313
    if not status['dynamicdns_server'] and not status['dynamicdns_update_url']:
314
        status['service_type'] = 'GnuDIP'
315
    elif not status['dynamicdns_server'] and status['dynamicdns_update_url']:
316
        status['service_type'] = 'other'
317
    else:
318
        status['service_type'] = 'GnuDIP'
319

320 321
    return status

322

323
def _apply_changes(request, old_status, new_status):
324 325 326
    """Apply the changes to Dynamic DNS client."""
    logger.info('New status is - %s', new_status)
    logger.info('Old status was - %s', old_status)
327

328 329 330 331
    if new_status['dynamicdns_secret'] == '':
        new_status['dynamicdns_secret'] = old_status['dynamicdns_secret']

    if new_status['dynamicdns_ipurl'] == '':
332
        new_status['dynamicdns_ipurl'] = EMPTYSTRING
333

334 335 336 337 338 339
    if new_status['dynamicdns_update_url'] == '':
        new_status['dynamicdns_update_url'] = EMPTYSTRING

    if new_status['dynamicdns_server'] == '':
        new_status['dynamicdns_server'] = EMPTYSTRING

340
    if new_status['service_type'] == 'GnuDIP':
341 342 343 344
        new_status['dynamicdns_update_url'] = EMPTYSTRING
    else:
        new_status['dynamicdns_server'] = EMPTYSTRING

345
    if old_status != new_status:
346 347 348
        disable_ssl_check = "disabled"
        use_http_basic_auth = "disabled"

349 350
        if new_status['disable_SSL_cert_check']:
            disable_ssl_check = "enabled"
351

352 353
        if new_status['use_http_basic_auth']:
            use_http_basic_auth = "enabled"
354

355 356 357
        _run(['configure', '-s', new_status['dynamicdns_server'],
              '-d', new_status['dynamicdns_domain'],
              '-u', new_status['dynamicdns_user'],
358
              '-p',
359 360
              '-I', new_status['dynamicdns_ipurl'],
              '-U', new_status['dynamicdns_update_url'],
361
              '-c', disable_ssl_check,
362
              '-b', use_http_basic_auth],
363
             input=new_status['dynamicdns_secret'].encode())
364 365 366

        if old_status['enabled']:
            _run(['stop'])
367

368 369 370
        if new_status['enabled']:
            _run(['start'])

371
        messages.success(request, _('Configuration updated'))
372
    else:
373
        logger.info('Nothing changed')
374

375

376
def _run(arguments, superuser=False, input=None):
377
    """Run a given command and raise exception if there was an error."""
378
    command = 'dynamicdns'
379 380

    if superuser:
381
        return actions.superuser_run(command, arguments, input=input)
382
    else:
383
        return actions.run(command, arguments, input=input)