~ubuntu-branches/ubuntu/trusty/ufw/trusty-proposed

« back to all changes in this revision

Viewing changes to src/applications.py

  • Committer: Package Import Robot
  • Author(s): Jamie Strandboge
  • Date: 2012-04-04 12:12:25 UTC
  • mfrom: (30.1.13)
  • mto: This revision was merged to the branch mainline in revision 65.
  • Revision ID: package-import@ubuntu.com-20120404121225-pjt6j7hm3ua0q580
Tags: 0.31.1-1
* New upstream release (Closes: 663677, Closes: 625681)
* debian/control: update to standards 3.9.3
* convert to source format 3.0 (quilt)
* 0001-optimize-boot.patch: only read in /etc/ufw/ufw.conf when disabled
* debian/rules: adjust to only install the application profiles when not
  Ubuntu
* debian/po/nl.po: add Dutch translation of debconf templates. Thanks to
  Jeroen Schot (Closes: 658495)
* debian/po/da.po: add Danish translation of debconf templates. Thanks to
  Joe Dalton (Closes: 666557)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#
2
 
# applications.py: common classes for ufw
3
 
#
4
 
# Copyright 2008-2009 Canonical Ltd.
 
1
'''applications.py: common classes for ufw'''
 
2
#
 
3
# Copyright 2008-2011 Canonical Ltd.
5
4
#
6
5
#    This program is free software: you can redistribute it and/or modify
7
6
#    it under the terms of the GNU General Public License version 3,
16
15
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
16
#
18
17
 
19
 
from ConfigParser import *
 
18
import ConfigParser
20
19
import os
21
20
import re
22
 
from stat import *
 
21
import stat
23
22
import ufw.util
24
23
from ufw.util import debug, warn
25
24
from ufw.common import UFWError
26
25
 
27
 
def get_profiles(dir):
 
26
def get_profiles(profiles_dir):
28
27
    '''Get profiles found in profiles database.  Returns dictionary with
29
28
       profile name as key and tuples for fields
30
29
    '''
31
 
    if not os.path.isdir(dir):
32
 
        err_msg = _("Profiles directory does not exist") % (dir)
33
 
        raise UFWError("Error: profiles directory does not exist")
 
30
    if not os.path.isdir(profiles_dir):
 
31
        err_msg = _("Profiles directory does not exist")
 
32
        raise UFWError(err_msg)
34
33
 
35
34
    max_size = 10 * 1024 * 1024  # 10MB
36
35
    profiles = {}
37
36
 
38
 
    files = os.listdir(dir)
 
37
    files = os.listdir(profiles_dir)
39
38
    files.sort()
40
39
 
41
40
    total_size = 0
42
41
    pat = re.compile(r'^\.')
43
42
    for f in files:
44
 
        abs = dir + "/" + f
45
 
        if not os.path.isfile(abs):
 
43
        abs_path = profiles_dir + "/" + f
 
44
        if not os.path.isfile(abs_path):
46
45
            continue
47
46
 
48
47
        if pat.search(f):
59
58
        # benefit, just usability)
60
59
        size = 0
61
60
        try:
62
 
            size = os.stat(abs)[ST_SIZE]
 
61
            size = os.stat(abs_path)[stat.ST_SIZE]
63
62
        except Exception:
64
63
            warn_msg = _("Skipping '%s': couldn't stat") % (f)
65
64
            warn(warn_msg)
77
76
 
78
77
        total_size += size
79
78
 
80
 
        cdict = RawConfigParser()
 
79
        cdict = ConfigParser.RawConfigParser()
81
80
        try:
82
 
            cdict.read(abs)
 
81
            cdict.read(abs_path)
83
82
        except Exception:
84
83
            warn_msg = _("Skipping '%s': couldn't process") % (f)
85
84
            warn(warn_msg)
141
140
    if name == "all":
142
141
        return False
143
142
 
 
143
    # Don't allow integers (ports)
 
144
    try:
 
145
        int(name)
 
146
        return False
 
147
    except:
 
148
        pass
 
149
 
144
150
    # Require first character be alpha, so we can avoid collisions with port
145
151
    # numbers.
146
 
    if re.match(r'^[a-zA-Z][a-zA-Z0-9 _\-\.+]*$', name):
 
152
    if re.match(r'^[a-zA-Z0-9][a-zA-Z0-9 _\-\.+]*$', name):
147
153
        return True
148
154
    return False
149
155
 
184
190
 
185
191
def get_title(profile):
186
192
    '''Retrieve the title from the profile'''
187
 
    str = ""
 
193
    s = ""
188
194
    field = 'title'
189
195
    if profile.has_key(field) and profile[field]:
190
 
        str = profile[field]
191
 
    return str
 
196
        s = profile[field]
 
197
    return s
192
198
 
193
199
def get_description(profile):
194
200
    '''Retrieve the description from the profile'''
195
 
    str = ""
 
201
    s = ""
196
202
    field = 'description'
197
203
    if profile.has_key(field) and profile[field]:
198
 
        str = profile[field]
199
 
    return str
 
204
        s = profile[field]
 
205
    return s
200
206
 
201
207
def get_ports(profile):
202
208
    '''Retrieve a list of ports from a profile'''