~ubuntu-branches/ubuntu/quantal/ufw/quantal-proposed

« back to all changes in this revision

Viewing changes to src/frontend.py

  • Committer: Package Import Robot
  • Author(s): Jamie Strandboge
  • Date: 2012-07-06 11:44:48 UTC
  • mfrom: (30.1.14)
  • Revision ID: package-import@ubuntu.com-20120706114448-0t9kf4s2jdaprwcs
Tags: 0.32-0ubuntu1
* New upstream release featuring Python 3 port (LP: #1001657)
* debian/control:
  - clean up Depends and Build-Depends
  - Build-Depends on python3
  - add python-ufw for installing python2 modules
  - add X-Python3-Version: >= 3.2
  - update Vcs-Bzr
* add debian/python-ufw.install
* debian/rules:
  - use --install-layout=deb
  - adjust PYTHON to use python3
  - adjust PYVERS to use py3versions
  - add PYTHON2
  - run tests for both PYTHON and PYTHON2
  - run setup.py with both PYTHON and PYTHON2
  - use dh_python3 for ufw
  - use dh_python2 for python-ufw
* debian/ufw.lintian-overrides
  - remove old unneeded override
  - add postrm-does-not-call-updaterc.d-for-init.d-script since Ubuntu's
    debhelper adds code to postinst that does nothing on Ubuntu, but doesn't
    add the corresponding code to postrm

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
'''frontend.py: frontend interface for ufw'''
2
2
#
3
 
# Copyright 2008-2011 Canonical Ltd.
 
3
# Copyright 2008-2012 Canonical Ltd.
4
4
#
5
5
#    This program is free software: you can redistribute it and/or modify
6
6
#    it under the terms of the GNU General Public License version 3,
21
21
 
22
22
from ufw.common import UFWError
23
23
import ufw.util
24
 
from ufw.util import error, warn
 
24
from ufw.util import error, warn, msg
25
25
from ufw.backend_iptables import UFWBackendIptables
26
26
import ufw.parser
27
27
 
71
71
            argv.insert(idx, 'rule')
72
72
 
73
73
    if len(argv) < 2 or ('--dry-run' in argv and len(argv) < 3):
74
 
        print >> sys.stderr, "ERROR: not enough args"
75
 
        sys.exit(1)
 
74
        error("not enough args")
76
75
 
77
76
    try:
78
77
        pr = p.parse_command(argv[1:])
79
 
    except UFWError, e:
80
 
        print >> sys.stderr, "ERROR: %s" % (e.value)
81
 
        sys.exit(1)
 
78
    except UFWError as e:
 
79
        error("%s" % (e.value))
82
80
    except Exception:
83
 
        print >> sys.stderr, "Invalid syntax"
 
81
        error("Invalid syntax", do_exit=False)
84
82
        raise
85
83
 
86
84
    return pr
87
85
 
88
86
def get_command_help():
89
87
    '''Print help message'''
90
 
    msg = _('''
 
88
    help_msg = _('''
91
89
Usage: %(progname)s %(command)s
92
90
 
93
91
%(commands)s:
144
142
         'appupdate': "app update PROFILE", \
145
143
         'appdefault': "app default ARG"}))
146
144
 
147
 
    return (msg)
 
145
    return (help_msg)
148
146
 
149
147
 
150
148
class UFWFrontend:
183
181
            try:
184
182
                self.backend.set_default(self.backend.files['conf'], \
185
183
                                         "ENABLED", config_str)
186
 
            except UFWError, e:
 
184
            except UFWError as e:
187
185
                error(e.value)
188
186
 
189
187
        error_str = ""
190
188
        if enabled:
191
189
            try:
192
190
                self.backend.start_firewall()
193
 
            except UFWError, e:
 
191
            except UFWError as e:
194
192
                if changed:
195
193
                    error_str = e.value
196
194
 
200
198
                try:
201
199
                    self.backend.set_default(self.backend.files['conf'], \
202
200
                                             "ENABLED", "no")
203
 
                except UFWError, e:
 
201
                except UFWError as e:
204
202
                    error(e.value)
205
203
 
206
204
                # Report the error
210
208
        else:
211
209
            try:
212
210
                self.backend.stop_firewall()
213
 
            except UFWError, e:
 
211
            except UFWError as e:
214
212
                error(e.value)
215
213
 
216
214
            res = _("Firewall stopped and disabled on system startup")
225
223
            if self.backend.is_enabled():
226
224
                self.backend.stop_firewall()
227
225
                self.backend.start_firewall()
228
 
        except UFWError, e:
 
226
        except UFWError as e:
229
227
            error(e.value)
230
228
 
231
229
        return res
235
233
        res = ""
236
234
        try:
237
235
            res = self.backend.set_loglevel(level)
238
 
        except UFWError, e:
 
236
        except UFWError as e:
239
237
            error(e.value)
240
238
 
241
239
        return res
244
242
        '''Shows status of firewall'''
245
243
        try:
246
244
            out = self.backend.get_status(verbose, show_count)
247
 
        except UFWError, e:
 
245
        except UFWError as e:
248
246
            error(e.value)
249
247
 
250
248
        return out
253
251
        '''Shows raw output of firewall'''
254
252
        try:
255
253
            out = self.backend.get_running_raw(rules_type)
256
 
        except UFWError, e:
 
254
        except UFWError as e:
257
255
            error(e.value)
258
256
 
259
257
        return out
269
267
 
270
268
        rules = self.backend.get_rules()
271
269
 
272
 
        protocols = d.keys()
 
270
        protocols = list(d.keys())
273
271
        protocols.sort()
274
272
        for proto in protocols:
275
273
            if not self.backend.use_ipv6() and proto in ['tcp6', 'udp6']:
276
274
                continue
277
275
            res += "%s:\n" % (proto)
278
 
            ports = d[proto].keys()
 
276
            ports = list(d[proto].keys())
279
277
            ports.sort()
280
278
            for port in ports:
281
279
                for item in d[proto][port]:
465
463
                    else:
466
464
                        err_msg = _("Invalid IP version '%s'") % (ip_version)
467
465
                        raise UFWError(err_msg)
468
 
            except UFWError, e:
 
466
            except UFWError as e:
469
467
                err_msg = e.value
470
468
                set_error = True
471
469
                break
484
482
            # If error and more than one rule, delete the successfully added
485
483
            # rules in reverse order
486
484
            undo_error = False
487
 
            indexes = range(count+1)
 
485
            indexes = list(range(count+1))
488
486
            indexes.reverse()
489
487
            for j in indexes:
490
488
                if count > 0 and rules[j]:
540
538
                       "(%(yes)s|%(no)s)? ") % ({'rule': rstr, \
541
539
                                                 'yes': self.yes, \
542
540
                                                 'no': self.no})
543
 
            os.write(sys.stdout.fileno(), prompt)
 
541
            msg(prompt, output=sys.stdout, newline=False)
544
542
            ans = sys.stdin.readline().lower().strip()
545
543
            if ans != "y" and ans != self.yes and ans != self.yes_full:
546
544
                proceed = False
608
606
                    if tmp != rule.dapp:
609
607
                        rule.dapp = tmp
610
608
                        rule.set_port(tmp, "dst")
611
 
                except UFWError, e:
 
609
                except UFWError as e:
612
610
                    # allow for the profile being deleted (LP: #407810)
613
611
                    if not rule.remove:
614
612
                        error(e.value)
622
620
                    if tmp != rule.sapp:
623
621
                        rule.sapp = tmp
624
622
                        rule.set_port(tmp, "dst")
625
 
                except UFWError, e:
 
623
                except UFWError as e:
626
624
                    # allow for the profile being deleted (LP: #407810)
627
625
                    if not rule.remove:
628
626
                        error(e.value)
642
640
        res = ""
643
641
        try:
644
642
            res = self.backend.set_default_application_policy(policy)
645
 
        except UFWError, e:
 
643
        except UFWError as e:
646
644
            error(e.value)
647
645
 
648
646
        return res
649
647
 
650
648
    def get_application_list(self):
651
649
        '''Display list of known application profiles'''
652
 
        names = self.backend.profiles.keys()
 
650
        names = list(self.backend.profiles.keys())
653
651
        names.sort()
654
652
        rstr = _("Available applications:")
655
653
        for n in names:
660
658
        '''Display information on profile'''
661
659
        names = []
662
660
        if pname == "all":
663
 
            names = self.backend.profiles.keys()
 
661
            names = list(self.backend.profiles.keys())
664
662
            names.sort()
665
663
        else:
666
664
            if not ufw.applications.valid_profile_name(pname):
670
668
 
671
669
        rstr = ""
672
670
        for name in names:
673
 
            if not self.backend.profiles.has_key(name) or \
 
671
            if name not in self.backend.profiles or \
674
672
               not self.backend.profiles[name]:
675
673
                err_msg = _("Could not find profile '%s'") % (name)
676
674
                raise UFWError(err_msg)
719
717
            allow_reload = False
720
718
 
721
719
        if profile == "all":
722
 
            profiles = self.backend.profiles.keys()
 
720
            profiles = list(self.backend.profiles.keys())
723
721
            profiles.sort()
724
722
            for p in profiles:
725
723
                (tmp, found) = self.backend.update_app_rule(p)
779
777
        except Exception:
780
778
            raise
781
779
 
782
 
        if pr.data.has_key('rule'):
 
780
        if 'rule' in pr.data:
783
781
            rstr = self.do_action(pr.action, pr.data['rule'], \
784
782
                                  pr.data['iptype'])
785
783
        else:
826
824
            prompt = _("Command may disrupt existing ssh connections. " \
827
825
                       "Proceed with operation (%(yes)s|%(no)s)? ") % \
828
826
                       ({'yes': self.yes, 'no': self.no})
829
 
            os.write(sys.stdout.fileno(), prompt)
 
827
            msg(prompt, output=sys.stdout, newline=False)
830
828
            ans = sys.stdin.readline().lower().strip()
831
829
            if ans != "y" and ans != self.yes and ans != self.yes_full:
832
830
                proceed = False
846
844
                       ({'yes': self.yes, 'no': self.no})
847
845
 
848
846
        if self.backend.do_checks and not force:
849
 
            os.write(sys.stdout.fileno(), ufw.util.wrap_text(prompt))
 
847
            msg(ufw.util.wrap_text(prompt), output=sys.stdout, newline=False)
850
848
            ans = sys.stdin.readline().lower().strip()
851
849
            if ans != "y" and ans != self.yes and ans != self.yes_full:
852
850
                res = _("Aborted")