home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / lib / python2.6 / dist-packages / ufw / applications.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-04-20  |  5.0 KB  |  195 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. from ConfigParser import *
  5. import os
  6. import re
  7. from stat import *
  8. import ufw.util as ufw
  9. from ufw.util import debug, warn
  10. from ufw.common import UFWError
  11.  
  12. def get_profiles(dir):
  13.     '''Get profiles found in profiles database.  Returns dictionary with
  14.        profile name as key and tuples for fields
  15.     '''
  16.     if not os.path.isdir(dir):
  17.         err_msg = _('Profiles directory does not exist') % dir
  18.         raise UFWError('Error: profiles directory does not exist')
  19.     os.path.isdir(dir)
  20.     max_size = 10485760
  21.     profiles = { }
  22.     files = os.listdir(dir)
  23.     files.sort()
  24.     total_size = 0
  25.     pat = re.compile('^\\.')
  26.     for f in files:
  27.         abs = dir + '/' + f
  28.         if not os.path.isfile(abs):
  29.             continue
  30.         
  31.         if pat.search(f):
  32.             debug("Skipping '%s': hidden file" % f)
  33.             continue
  34.         
  35.         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('~'):
  36.             debug("Skipping '%s'" % f)
  37.             continue
  38.         
  39.         size = 0
  40.         
  41.         try:
  42.             size = os.stat(abs)[ST_SIZE]
  43.         except Exception:
  44.             warn_msg = _("Skipping '%s': couldn't stat") % f
  45.             warn(warn_msg)
  46.             continue
  47.  
  48.         if size > max_size:
  49.             warn_msg = _("Skipping '%s': too big") % f
  50.             warn(warn_msg)
  51.             continue
  52.         
  53.         if total_size + size > max_size:
  54.             warn_msg = _("Skipping '%s': too many files read already") % f
  55.             warn(warn_msg)
  56.             continue
  57.         
  58.         total_size += size
  59.         cdict = RawConfigParser()
  60.         
  61.         try:
  62.             cdict.read(abs)
  63.         except Exception:
  64.             warn_msg = _("Skipping '%s': couldn't process") % f
  65.             warn(warn_msg)
  66.             continue
  67.  
  68.         for p in cdict.sections():
  69.             if len(p) > 64:
  70.                 warn_msg = _("Skipping '%s': name too long") % p
  71.                 warn(warn_msg)
  72.                 continue
  73.             
  74.             if not valid_profile_name(p):
  75.                 warn_msg = _("Skipping '%s': invalid name") % p
  76.                 warn(warn_msg)
  77.                 continue
  78.             
  79.             
  80.             try:
  81.                 ufw.util.get_services_proto(p)
  82.                 warn_msg = _("Skipping '%s': also in /etc/services") % p
  83.                 warn(warn_msg)
  84.             except Exception:
  85.                 pass
  86.  
  87.             skip = False
  88.             for key, value in cdict.items(p):
  89.                 if len(key) > 64:
  90.                     warn_msg = _("Skipping '%s': field too long") % p
  91.                     warn(warn_msg)
  92.                     skip = True
  93.                     break
  94.                 
  95.                 if len(value) > 1024:
  96.                     warn_msg = _("Skipping '%s': value too long for '%s'") % (p, key)
  97.                     warn(warn_msg)
  98.                     skip = True
  99.                     break
  100.                     continue
  101.             
  102.             if skip:
  103.                 continue
  104.             
  105.             if profiles.has_key(p):
  106.                 warn_msg = _("Duplicate profile '%s', using last found") % p
  107.                 warn(warn_msg)
  108.             
  109.             pdict = { }
  110.             for key, value in cdict.items(p):
  111.                 pdict[key] = value
  112.             
  113.             profiles[p] = pdict
  114.         
  115.     
  116.     return profiles
  117.  
  118.  
  119. def valid_profile_name(name):
  120.     '''Only accept a limited set of characters for name'''
  121.     if name == 'all':
  122.         return False
  123.     if re.match('^[a-zA-Z][a-zA-Z0-9 _\\-\\.+]*$', name):
  124.         return True
  125.     return False
  126.  
  127.  
  128. def verify_profile(name, profile):
  129.     '''Make sure profile has everything needed'''
  130.     app_fields = [
  131.         'title',
  132.         'description',
  133.         'ports']
  134.     for f in app_fields:
  135.         if not profile.has_key(f):
  136.             err_msg = _("Profile '%s' missing required field '%s'") % (name, f)
  137.             raise UFWError(err_msg)
  138.         profile.has_key(f)
  139.         if not profile[f]:
  140.             err_msg = _("Profile '%s' has empty required field '%s'") % (name, f)
  141.             raise UFWError(err_msg)
  142.         profile[f]
  143.     
  144.     ports = profile['ports'].split('|')
  145.     if len(ports) < 1:
  146.         err_msg = _("No ports found in profile '%s'") % name
  147.         return False
  148.     
  149.     try:
  150.         for p in ports:
  151.             (port, proto) = ufw.util.parse_port_proto(p)
  152.             rule = ufw.common.UFWRule('ACCEPT', proto, port)
  153.             debug(rule)
  154.     except Exception:
  155.         len(ports) < 1
  156.         e = len(ports) < 1
  157.         debug(e)
  158.         err_msg = _("Invalid ports in profile '%s'") % name
  159.         raise UFWError(err_msg)
  160.     except:
  161.         len(ports) < 1
  162.  
  163.     return True
  164.  
  165.  
  166. def get_title(profile):
  167.     '''Retrieve the title from the profile'''
  168.     str = ''
  169.     field = 'title'
  170.     if profile.has_key(field) and profile[field]:
  171.         str = profile[field]
  172.     
  173.     return str
  174.  
  175.  
  176. def get_description(profile):
  177.     '''Retrieve the description from the profile'''
  178.     str = ''
  179.     field = 'description'
  180.     if profile.has_key(field) and profile[field]:
  181.         str = profile[field]
  182.     
  183.     return str
  184.  
  185.  
  186. def get_ports(profile):
  187.     '''Retrieve a list of ports from a profile'''
  188.     ports = []
  189.     field = 'ports'
  190.     if profile.has_key(field) and profile[field]:
  191.         ports = profile[field].split('|')
  192.     
  193.     return ports
  194.  
  195.