'''Get profiles found in profiles database. Returns dictionary with
profile name as key and tuples for fields
'''
if not os.path.isdir(dir):
err_msg = _('Profiles directory does not exist') % dir
raise UFWError('Error: profiles directory does not exist')
os.path.isdir(dir)
max_size = 10485760
profiles = { }
files = os.listdir(dir)
files.sort()
total_size = 0
pat = re.compile('^\\.')
for f in files:
abs = dir + '/' + f
if not os.path.isfile(abs):
continue
if pat.search(f):
debug("Skipping '%s': hidden file" % f)
continue
if f.endswith('.dpkg-new') and f.endswith('.dpkg-old') and f.endswith('.dpkg-dist') and f.endswith('.rpmnew') and f.endswith('.rpmsave') or f.endswith('~'):
debug("Skipping '%s'" % f)
continue
size = 0
try:
size = os.stat(abs)[ST_SIZE]
except Exception:
warn_msg = _("Skipping '%s': couldn't stat") % f
warn(warn_msg)
continue
if size > max_size:
warn_msg = _("Skipping '%s': too big") % f
warn(warn_msg)
continue
if total_size + size > max_size:
warn_msg = _("Skipping '%s': too many files read already") % f
warn(warn_msg)
continue
total_size += size
cdict = RawConfigParser()
try:
cdict.read(abs)
except Exception:
warn_msg = _("Skipping '%s': couldn't process") % f
warn(warn_msg)
continue
for p in cdict.sections():
if len(p) > 64:
warn_msg = _("Skipping '%s': name too long") % p
warn(warn_msg)
continue
if not valid_profile_name(p):
warn_msg = _("Skipping '%s': invalid name") % p
warn(warn_msg)
continue
try:
ufw.util.get_services_proto(p)
warn_msg = _("Skipping '%s': also in /etc/services") % p
warn(warn_msg)
except Exception:
pass
skip = False
for key, value in cdict.items(p):
if len(key) > 64:
warn_msg = _("Skipping '%s': field too long") % p
warn(warn_msg)
skip = True
break
if len(value) > 1024:
warn_msg = _("Skipping '%s': value too long for '%s'") % (p, key)
warn(warn_msg)
skip = True
break
continue
if skip:
continue
if profiles.has_key(p):
warn_msg = _("Duplicate profile '%s', using last found") % p
warn(warn_msg)
pdict = { }
for key, value in cdict.items(p):
pdict[key] = value
profiles[p] = pdict
return profiles
def valid_profile_name(name):
'''Only accept a limited set of characters for name'''
if name == 'all':
return False
if re.match('^[a-zA-Z][a-zA-Z0-9 _\\-\\.+]*$', name):