Esempio n. 1
0
 def open_key(fname):
     if fname == privkey_filename:
         return StringIO("privatekey")
     elif fname == pubkey_filename:
         return StringIO("ssh-rsa publickey [email protected]")
     else:
         self.fail("Unexpected open call: %s" % fname)
Esempio n. 2
0
File: Yum.py Progetto: ab/bcfg2
    def get_config(self, raw=False):
        config = ConfigParser.SafeConfigParser()
        for source in self.sources:
            for url_map in source.url_map:
                if url_map['arch'] not in self.metadata.groups:
                    continue
                basereponame = source.get_repo_name(url_map)
                reponame = basereponame

                added = False
                while not added:
                    try:
                        config.add_section(reponame)
                        added = True
                    except ConfigParser.DuplicateSectionError:
                        match = re.search("-(\d+)", reponame)
                        if match:
                            rid = int(match.group(1)) + 1
                        else:
                            rid = 1
                        reponame = "%s-%d" % (basereponame, rid)

                config.set(reponame, "name", reponame)
                config.set(reponame, "baseurl", url_map['url'])
                config.set(reponame, "enabled", "1")
                if len(source.gpgkeys):
                    config.set(reponame, "gpgcheck", "1")
                    config.set(reponame, "gpgkey",
                               " ".join(source.gpgkeys))
                else:
                    config.set(reponame, "gpgcheck", "0")

                if len(source.blacklist):
                    config.set(reponame, "exclude",
                               " ".join(source.blacklist))
                if len(source.whitelist):
                    config.set(reponame, "includepkgs",
                               " ".join(source.whitelist))

                if raw:
                    opts = source.server_options
                else:
                    opts = source.client_options
                for opt, val in opts.items():
                    config.set(reponame, opt, val)

        if raw:
            return config
        else:
            # configparser only writes to file, so we have to use a
            # StringIO object to get the data out as a string
            buf = StringIO()
            config.write(buf)
            return "# This config was generated automatically by the Bcfg2 " \
                   "Packages plugin\n\n" + buf.getvalue()
Esempio n. 3
0
 def usage(self):
     """ Get the short usage message. """
     if self._usage is None:
         sio = StringIO()
         self.parser.print_usage(file=sio)
         usage = self._ws_re.sub(' ', sio.getvalue()).strip()[7:]
         doc = self._ws_re.sub(' ', getattr(self, "__doc__") or "").strip()
         if not doc:
             self._usage = usage
         else:
             self._usage = "%s - %s" % (usage, doc)
     return self._usage
Esempio n. 4
0
 def usage(self):
     """ Get the short usage message. """
     if self._usage is None:
         sio = StringIO()
         self.parser.print_usage(file=sio)
         usage = self._ws_re.sub(' ', sio.getvalue()).strip()[7:]
         doc = self._ws_re.sub(' ', getattr(self, "__doc__") or "").strip()
         if not doc:
             self._usage = usage
         else:
             self._usage = "%s - %s" % (usage, doc)
     return self._usage
 def _instantiate(self, cls, fileobj, filepath, filename, encoding=None):
     plaintext = StringIO(bruteforce_decrypt(fileobj.read()))
     return TemplateLoader._instantiate(self,
                                        cls,
                                        plaintext,
                                        filepath,
                                        filename,
                                        encoding=encoding)
Esempio n. 6
0
    def buildNetgroups(self):
        """Makes the *-machine files"""
        header = """###################################################################
#  This file lists hosts in the '%s' machine netgroup, it is
#  automatically generated. DO NOT EDIT THIS FILE!
#
#  Number of hosts in '%s' machine netgroup: %i
#\n\n"""

        cursor = connection.cursor()
        # fetches all the hosts that with valid netgroup entries
        cursor.execute("""
        SELECT h.hostname, n.name, h.netgroup, n.only FROM ((hostbase_host h
        INNER JOIN hostbase_interface i ON h.id = i.host_id)
        INNER JOIN hostbase_ip p ON i.id = p.interface_id)
        INNER JOIN hostbase_name n ON p.id = n.ip_id
        WHERE h.netgroup <> '' AND h.netgroup <> 'none' AND h.status = 'active'
        ORDER BY h.netgroup, h.hostname
        """)
        nameslist = cursor.fetchall()
        # gets the first host and initializes the hash
        hostdata = nameslist[0]
        netgroups = {hostdata[2]: [hostdata[0]]}
        for row in nameslist:
            # if new netgroup, create it
            if row[2] not in netgroups:
                netgroups.update({row[2]: []})
            # if it belongs in the netgroup and has multiple interfaces, put them in
            if hostdata[0] == row[0] and row[3]:
                netgroups[row[2]].append(row[1])
                hostdata = row
            # if its a new host, write the old one to the hash
            elif hostdata[0] != row[0]:
                netgroups[row[2]].append(row[0])
                hostdata = row

        for netgroup in netgroups:
            fileoutput = StringIO()
            fileoutput.write(header % (netgroup, netgroup, len(netgroups[netgroup])))
            for each in netgroups[netgroup]:
                fileoutput.write(each + "\n")
            self.filedata['%s-machines' % netgroup] = fileoutput.getvalue()
            fileoutput.close()
            self.Entries['ConfigFile']['/my/adm/hostbase/makenets/machines/%s-machines' % netgroup] = self.FetchFile

        cursor.execute("""
        UPDATE hostbase_host SET dirty=0
        """)
Esempio n. 7
0
 def _get_subcommand_output(self, args):
     self.parser.parse(args)
     old_stdout = sys.stdout
     sys.stdout = StringIO()
     rv = self.registry.runcommand()
     output = [
         l for l in sys.stdout.getvalue().splitlines()
         if not l.startswith("DEBUG: ")
     ]
     sys.stdout = old_stdout
     return (rv, output)
Esempio n. 8
0
    def buildNetgroups(self):
        """Makes the *-machine files"""
        header = """###################################################################
#  This file lists hosts in the '%s' machine netgroup, it is
#  automatically generated. DO NOT EDIT THIS FILE!
#
#  Number of hosts in '%s' machine netgroup: %i
#\n\n"""

        cursor = connection.cursor()
        # fetches all the hosts that with valid netgroup entries
        cursor.execute("""
        SELECT h.hostname, n.name, h.netgroup, n.only FROM ((hostbase_host h
        INNER JOIN hostbase_interface i ON h.id = i.host_id)
        INNER JOIN hostbase_ip p ON i.id = p.interface_id)
        INNER JOIN hostbase_name n ON p.id = n.ip_id
        WHERE h.netgroup <> '' AND h.netgroup <> 'none' AND h.status = 'active'
        ORDER BY h.netgroup, h.hostname
        """)
        nameslist = cursor.fetchall()
        # gets the first host and initializes the hash
        hostdata = nameslist[0]
        netgroups = {hostdata[2]: [hostdata[0]]}
        for row in nameslist:
            # if new netgroup, create it
            if row[2] not in netgroups:
                netgroups.update({row[2]: []})
            # if it belongs in the netgroup and has multiple interfaces, put them in
            if hostdata[0] == row[0] and row[3]:
                netgroups[row[2]].append(row[1])
                hostdata = row
            # if its a new host, write the old one to the hash
            elif hostdata[0] != row[0]:
                netgroups[row[2]].append(row[0])
                hostdata = row

        for netgroup in netgroups:
            fileoutput = StringIO()
            fileoutput.write(header % (netgroup, netgroup, len(netgroups[netgroup])))
            for each in netgroups[netgroup]:
                fileoutput.write(each + "\n")
            self.filedata['%s-machines' % netgroup] = fileoutput.getvalue()
            fileoutput.close()
            self.Entries['ConfigFile']['/my/adm/hostbase/makenets/machines/%s-machines' % netgroup] = self.FetchFile

        cursor.execute("""
        UPDATE hostbase_host SET dirty=0
        """)
Esempio n. 9
0
    def test_cfg_stdout(self):
        """ Decrypt a Cfg file to stdout """
        cli = CLI([
            "--decrypt", "--cfg", "-p", "basic", "--stdout",
            os.path.join(self.basedir, "basic.crypt")
        ])
        self.set_options()
        old_stdout = sys.stdout
        sys.stdout = StringIO()
        cli.run()
        output = sys.stdout.getvalue()
        sys.stdout = old_stdout

        self.assertNotExists("basic")
        self.assertEqual(self.cfg_plaintext.strip(), output.strip())
        self.assertNotEncrypted(output)
Esempio n. 10
0
 def _update_pkgdata(self, pkgdata, source_url):
     for section in source_url.sections:
         for arch in self.architectures:
             if source_url.arch != arch and source_url.arch != "all":
                 continue
             if source_url.arch == "all" and arch in self.arch_specialurl:
                 continue
             url = "%s/dists/%s/%s/binary-%s/Packages.gz" % (
                 source_url.url, source_url.distribution, section, arch)
             debug("Processing url %s\n" % (url))
             try:
                 data = urlopen(url)
                 buf = StringIO(''.join(data.readlines()))
                 reader = gzip.GzipFile(fileobj=buf)
                 for line in reader.readlines():
                     if line[:8] == 'Package:':
                         pkgname = line.split(' ')[1].strip()
                     elif line[:8] == 'Version:':
                         version = line.split(' ')[1].strip()
                         if pkgname in pkgdata:
                             if arch in pkgdata[pkgname]:
                                 # The package is listed twice for the same architecture
                                 # We keep the most recent version
                                 old_version = pkgdata[pkgname][arch]
                                 if self._pkg_version_is_older(
                                         old_version, version):
                                     pkgdata[pkgname][arch] = version
                             else:
                                 # The package data exists for another architecture,
                                 # but not for this one. Add it.
                                 pkgdata[pkgname][arch] = version
                         else:
                             # First entry for this package
                             pkgdata[pkgname] = {arch: version}
                     else:
                         continue
             except:
                 raise Exception("Could not process URL %s\n%s\nPlease "
                                 "verify the URL." %
                                 (url, sys.exc_info()[1]))
     return dict((k, v) for (k, v) in list(pkgdata.items()) \
                 if re.search(self.pattern, k))
Esempio n. 11
0
def _cipher_filter(cipher, instr):
    inbuf = StringIO(instr)
    outbuf = StringIO()
    while 1:
        buf = inbuf.read()
        if not buf:
            break
        outbuf.write(cipher.update(buf))
    outbuf.write(cipher.final())
    rv = outbuf.getvalue()
    inbuf.close()
    outbuf.close()
    return rv
Esempio n. 12
0
    def get_config(self, raw=False):  # pylint: disable=W0221
        """ Get the yum configuration for this collection.

        :param raw: Return a :class:`ConfigParser.SafeConfigParser`
                    object representing the configuration instead of a
                    string.  This is useful if you need to modify the
                    config before writing it (as :func:`write_config`
                    does in order to produce a server-specific
                    configuration).
        :type raw: bool
        :returns: string or ConfigParser.SafeConfigParser """

        config = ConfigParser.SafeConfigParser()
        for source in self:
            for url_map in source.url_map:
                if url_map['arch'] not in self.metadata.groups:
                    continue
                basereponame = source.get_repo_name(url_map)
                reponame = basereponame

                added = False
                while not added:
                    try:
                        config.add_section(reponame)
                        added = True
                    except ConfigParser.DuplicateSectionError:
                        match = re.search("-(\d+)", reponame)
                        if match:
                            rid = int(match.group(1)) + 1
                        else:
                            rid = 1
                        reponame = "%s-%d" % (basereponame, rid)

                config.set(reponame, "name", reponame)
                config.set(reponame, "baseurl", url_map['url'])
                config.set(reponame, "enabled", "1")
                if len(source.gpgkeys):
                    config.set(reponame, "gpgcheck", "1")
                    config.set(reponame, "gpgkey", " ".join(source.gpgkeys))
                else:
                    config.set(reponame, "gpgcheck", "0")

                if len(source.blacklist):
                    config.set(reponame, "exclude", " ".join(source.blacklist))
                if len(source.whitelist):
                    config.set(reponame, "includepkgs",
                               " ".join(source.whitelist))

                if raw:
                    opts = source.server_options
                else:
                    opts = source.client_options
                for opt, val in opts.items():
                    config.set(reponame, opt, val)

        if raw:
            return config
        else:
            # configparser only writes to file, so we have to use a
            # StringIO object to get the data out as a string
            buf = StringIO()
            config.write(buf)
            return "# This config was generated automatically by the Bcfg2 " \
                   "Packages plugin\n\n" + buf.getvalue()
Esempio n. 13
0
File: Yum.py Progetto: zenazn/bcfg2
    def get_config(self, raw=False):  # pylint: disable=W0221
        """ Get the yum configuration for this collection.

        :param raw: Return a :class:`ConfigParser.SafeConfigParser`
                    object representing the configuration instead of a
                    string.  This is useful if you need to modify the
                    config before writing it (as :func:`write_config`
                    does in order to produce a server-specific
                    configuration).
        :type raw: bool
        :returns: string or ConfigParser.SafeConfigParser """

        config = ConfigParser.SafeConfigParser()
        for source in self:
            for url_map in source.url_map:
                if url_map['arch'] not in self.metadata.groups:
                    continue
                basereponame = source.get_repo_name(url_map)
                reponame = basereponame

                added = False
                while not added:
                    try:
                        config.add_section(reponame)
                        added = True
                    except ConfigParser.DuplicateSectionError:
                        match = re.search("-(\d+)", reponame)
                        if match:
                            rid = int(match.group(1)) + 1
                        else:
                            rid = 1
                        reponame = "%s-%d" % (basereponame, rid)

                config.set(reponame, "name", reponame)
                config.set(reponame, "baseurl", url_map['url'])
                config.set(reponame, "enabled", "1")
                if len(source.gpgkeys):
                    config.set(reponame, "gpgcheck", "1")
                    config.set(reponame, "gpgkey",
                               " ".join(source.gpgkeys))
                else:
                    config.set(reponame, "gpgcheck", "0")

                if len(source.blacklist):
                    config.set(reponame, "exclude",
                               " ".join(source.blacklist))
                if len(source.whitelist):
                    config.set(reponame, "includepkgs",
                               " ".join(source.whitelist))

                if raw:
                    opts = source.server_options
                else:
                    opts = source.client_options
                for opt, val in opts.items():
                    config.set(reponame, opt, val)

        if raw:
            return config
        else:
            # configparser only writes to file, so we have to use a
            # StringIO object to get the data out as a string
            buf = StringIO()
            config.write(buf)
            return "# This config was generated automatically by the Bcfg2 " \
                   "Packages plugin\n\n" + buf.getvalue()
Esempio n. 14
0
    def buildZones(self):
        """Pre-build and stash zone files."""
        cursor = connection.cursor()

        cursor.execute("SELECT id, serial FROM hostbase_zone")
        zones = cursor.fetchall()

        for zone in zones:
        # update the serial number for all zone files
            todaydate = (strftime('%Y%m%d'))
            try:
                if todaydate == str(zone[1])[:8]:
                    serial = zone[1] + 1
                else:
                    serial = int(todaydate) * 100
            except (KeyError):
                serial = int(todaydate) * 100
            cursor.execute("""UPDATE hostbase_zone SET serial = \'%s\' WHERE id = \'%s\'""" % (str(serial), zone[0]))

        cursor.execute("SELECT * FROM hostbase_zone WHERE zone NOT LIKE \'%%.rev\'")
        zones = cursor.fetchall()

        iplist = []
        hosts = {}

        for zone in zones:
            zonefile = StringIO()
            externalzonefile = StringIO()
            cursor.execute("""SELECT n.name FROM hostbase_zone_nameservers z
            INNER JOIN hostbase_nameserver n ON z.nameserver_id = n.id
            WHERE z.zone_id = \'%s\'""" % zone[0])
            nameservers = cursor.fetchall()
            cursor.execute("""SELECT i.ip_addr FROM hostbase_zone_addresses z
            INNER JOIN hostbase_zoneaddress i ON z.zoneaddress_id = i.id
            WHERE z.zone_id = \'%s\'""" % zone[0])
            addresses = cursor.fetchall()
            cursor.execute("""SELECT m.priority, m.mx FROM hostbase_zone_mxs z
            INNER JOIN hostbase_mx m ON z.mx_id = m.id
            WHERE z.zone_id = \'%s\'""" % zone[0])
            mxs = cursor.fetchall()
            context = Context({
                'zone': zone,
                'nameservers': nameservers,
                'addresses': addresses,
                'mxs': mxs
                })
            zonefile.write(self.templates['zone'].render(context))
            externalzonefile.write(self.templates['zone'].render(context))

            querystring = """SELECT h.hostname, p.ip_addr,
            n.name, c.cname, m.priority, m.mx, n.dns_view
            FROM (((((hostbase_host h INNER JOIN hostbase_interface i ON h.id = i.host_id)
            INNER JOIN hostbase_ip p ON i.id = p.interface_id)
            INNER JOIN hostbase_name n ON p.id = n.ip_id)
            INNER JOIN hostbase_name_mxs x ON n.id = x.name_id)
            INNER JOIN hostbase_mx m ON m.id = x.mx_id)
            LEFT JOIN hostbase_cname c ON n.id = c.name_id
            WHERE n.name LIKE '%%%%%s'
            AND h.status = 'active'
            ORDER BY h.hostname, n.name, p.ip_addr
            """ % zone[1]
            cursor.execute(querystring)
            zonehosts = cursor.fetchall()
            prevhost = (None, None, None, None)
            cnames = StringIO()
            cnamesexternal = StringIO()
            for host in zonehosts:
                if not host[2].split(".", 1)[1] == zone[1]:
                    zonefile.write(cnames.getvalue())
                    externalzonefile.write(cnamesexternal.getvalue())
                    cnames = StringIO()
                    cnamesexternal = StringIO()
                    continue
                if not prevhost[1] == host[1] or not prevhost[2] == host[2]:
                    zonefile.write(cnames.getvalue())
                    externalzonefile.write(cnamesexternal.getvalue())
                    cnames = StringIO()
                    cnamesexternal = StringIO()
                    zonefile.write("%-32s%-10s%-32s\n" %
                                   (host[2].split(".", 1)[0], 'A', host[1]))
                    zonefile.write("%-32s%-10s%-3s%s.\n" %
                                   ('', 'MX', host[4], host[5]))
                    if host[6] == 'global':
                        externalzonefile.write("%-32s%-10s%-32s\n" %
                                               (host[2].split(".", 1)[0], 'A', host[1]))
                        externalzonefile.write("%-32s%-10s%-3s%s.\n" %
                                               ('', 'MX', host[4], host[5]))
                elif not prevhost[5] == host[5]:
                    zonefile.write("%-32s%-10s%-3s%s.\n" %
                                   ('', 'MX', host[4], host[5]))
                    if host[6] == 'global':
                        externalzonefile.write("%-32s%-10s%-3s%s.\n" %
                                         ('', 'MX', host[4], host[5]))

                if host[3]:
                    try:
                        if host[3].split(".", 1)[1] == zone[1]:
                            cnames.write("%-32s%-10s%-32s\n" %
                                         (host[3].split(".", 1)[0],
                                          'CNAME', host[2].split(".", 1)[0]))
                            if host[6] == 'global':
                                cnamesexternal.write("%-32s%-10s%-32s\n" %
                                                     (host[3].split(".", 1)[0],
                                                      'CNAME', host[2].split(".", 1)[0]))
                        else:
                            cnames.write("%-32s%-10s%-32s\n" %
                                         (host[3] + ".",
                                          'CNAME',
                                          host[2].split(".", 1)[0]))
                            if host[6] == 'global':
                                cnamesexternal.write("%-32s%-10s%-32s\n" %
                                                     (host[3] + ".",
                                                      'CNAME',
                                                      host[2].split(".", 1)[0]))

                    except:
                        pass
                prevhost = host
            zonefile.write(cnames.getvalue())
            externalzonefile.write(cnamesexternal.getvalue())
            zonefile.write("\n\n%s" % zone[9])
            externalzonefile.write("\n\n%s" % zone[9])
            self.filedata[zone[1]] = zonefile.getvalue()
            self.filedata[zone[1] + ".external"] = externalzonefile.getvalue()
            zonefile.close()
            externalzonefile.close()
            self.Entries['ConfigFile']["%s/%s" % (self.filepath, zone[1])] = self.FetchFile
            self.Entries['ConfigFile']["%s/%s.external" % (self.filepath, zone[1])] = self.FetchFile

        cursor.execute("SELECT * FROM hostbase_zone WHERE zone LIKE \'%%.rev\' AND zone <> \'.rev\'")
        reversezones = cursor.fetchall()

        reversenames = []
        for reversezone in reversezones:
            cursor.execute("""SELECT n.name FROM hostbase_zone_nameservers z
            INNER JOIN hostbase_nameserver n ON z.nameserver_id = n.id
            WHERE z.zone_id = \'%s\'""" % reversezone[0])
            reverse_nameservers = cursor.fetchall()

            context = Context({
                'inaddr': reversezone[1].rstrip('.rev'),
                'zone': reversezone,
                'nameservers': reverse_nameservers,
                })

            self.filedata[reversezone[1]] = self.templates['reversesoa'].render(context)
            self.filedata[reversezone[1] + '.external'] = self.templates['reversesoa'].render(context)
            self.filedata[reversezone[1]] += reversezone[9]
            self.filedata[reversezone[1] + '.external'] += reversezone[9]

            subnet = reversezone[1].split(".")
            subnet.reverse()
            reversenames.append((reversezone[1].rstrip('.rev'), ".".join(subnet[1:])))

        for filename in reversenames:
            cursor.execute("""
            SELECT DISTINCT h.hostname, p.ip_addr, n.dns_view FROM ((hostbase_host h
            INNER JOIN hostbase_interface i ON h.id = i.host_id)
            INNER JOIN hostbase_ip p ON i.id = p.interface_id)
            INNER JOIN hostbase_name n ON n.ip_id = p.id
            WHERE p.ip_addr LIKE '%s%%%%' AND h.status = 'active' ORDER BY p.ip_addr
            """ % filename[1])
            reversehosts = cursor.fetchall()
            zonefile = StringIO()
            externalzonefile = StringIO()
            if len(filename[0].split(".")) == 2:
                originlist = []
                [originlist.append((".".join([ip[1].split(".")[2], filename[0]]),
                                    ".".join([filename[1], ip[1].split(".")[2]])))
                 for ip in reversehosts
                 if (".".join([ip[1].split(".")[2], filename[0]]),
                     ".".join([filename[1], ip[1].split(".")[2]])) not in originlist]
                for origin in originlist:
                    hosts = [(host[1].split("."), host[0])
                             for host in reversehosts
                             if host[1].rstrip('0123456789').rstrip('.') == origin[1]]
                    hosts_external = [(host[1].split("."), host[0])
                                     for host in reversehosts
                                     if (host[1].rstrip('0123456789').rstrip('.') == origin[1]
                                         and host[2] == 'global')]
                    context = Context({
                        'hosts': hosts,
                        'inaddr': origin[0],
                        'fileorigin': filename[0],
                        })
                    zonefile.write(self.templates['reverseapp'].render(context))
                    context = Context({
                        'hosts': hosts_external,
                        'inaddr': origin[0],
                        'fileorigin': filename[0],
                        })
                    externalzonefile.write(self.templates['reverseapp'].render(context))
            else:
                originlist = [filename[0]]
                hosts = [(host[1].split("."), host[0])
                         for host in reversehosts
                         if (host[1].split("."), host[0]) not in hosts]
                hosts_external = [(host[1].split("."), host[0])
                                  for host in reversehosts
                                  if ((host[1].split("."), host[0]) not in hosts_external
                                  and host[2] == 'global')]
                context = Context({
                    'hosts': hosts,
                    'inaddr': filename[0],
                    'fileorigin': None,
                    })
                zonefile.write(self.templates['reverseapp'].render(context))
                context = Context({
                    'hosts': hosts_external,
                    'inaddr': filename[0],
                    'fileorigin': None,
                    })
                externalzonefile.write(self.templates['reverseapp'].render(context))
            self.filedata['%s.rev' % filename[0]] += zonefile.getvalue()
            self.filedata['%s.rev.external' % filename[0]] += externalzonefile.getvalue()
            zonefile.close()
            externalzonefile.close()
            self.Entries['ConfigFile']['%s/%s.rev' % (self.filepath, filename[0])] = self.FetchFile
            self.Entries['ConfigFile']['%s/%s.rev.external' % (self.filepath, filename[0])] = self.FetchFile

        ## here's where the named.conf file gets written
        context = Context({
            'zones': zones,
            'reverses': reversenames,
            })
        self.filedata['named.conf'] = self.templates['named'].render(context)
        self.Entries['ConfigFile']['/my/adm/hostbase/files/named.conf'] = self.FetchFile
        self.filedata['named.conf.views'] = self.templates['namedviews'].render(context)
        self.Entries['ConfigFile']['/my/adm/hostbase/files/named.conf.views'] = self.FetchFile
Esempio n. 15
0
def _cipher_filter(cipher, instr):
    """ M2Crypto reads and writes file-like objects, so this uses
    StringIO to pass data through it """
    inbuf = StringIO(instr)
    outbuf = StringIO()
    while 1:
        buf = inbuf.read()
        if not buf:
            break
        outbuf.write(cipher.update(buf))
    outbuf.write(cipher.final())
    rv = outbuf.getvalue()
    inbuf.close()
    outbuf.close()
    return rv
Esempio n. 16
0
def _cipher_filter(cipher, instr):
    """ M2Crypto reads and writes file-like objects, so this uses
    StringIO to pass data through it """
    inbuf = StringIO(instr)
    outbuf = StringIO()
    while 1:
        buf = inbuf.read()
        if not buf:
            break
        outbuf.write(cipher.update(buf))
    outbuf.write(cipher.final())
    rv = outbuf.getvalue()
    inbuf.close()
    outbuf.close()
    return rv
Esempio n. 17
0
    def buildZones(self):
        """Pre-build and stash zone files."""
        cursor = connection.cursor()

        cursor.execute("SELECT id, serial FROM hostbase_zone")
        zones = cursor.fetchall()

        for zone in zones:
        # update the serial number for all zone files
            todaydate = (strftime('%Y%m%d'))
            try:
                if todaydate == str(zone[1])[:8]:
                    serial = zone[1] + 1
                else:
                    serial = int(todaydate) * 100
            except (KeyError):
                serial = int(todaydate) * 100
            cursor.execute("""UPDATE hostbase_zone SET serial = \'%s\' WHERE id = \'%s\'""" % (str(serial), zone[0]))

        cursor.execute("SELECT * FROM hostbase_zone WHERE zone NOT LIKE \'%%.rev\'")
        zones = cursor.fetchall()

        iplist = []
        hosts = {}

        for zone in zones:
            zonefile = StringIO()
            externalzonefile = StringIO()
            cursor.execute("""SELECT n.name FROM hostbase_zone_nameservers z
            INNER JOIN hostbase_nameserver n ON z.nameserver_id = n.id
            WHERE z.zone_id = \'%s\'""" % zone[0])
            nameservers = cursor.fetchall()
            cursor.execute("""SELECT i.ip_addr FROM hostbase_zone_addresses z
            INNER JOIN hostbase_zoneaddress i ON z.zoneaddress_id = i.id
            WHERE z.zone_id = \'%s\'""" % zone[0])
            addresses = cursor.fetchall()
            cursor.execute("""SELECT m.priority, m.mx FROM hostbase_zone_mxs z
            INNER JOIN hostbase_mx m ON z.mx_id = m.id
            WHERE z.zone_id = \'%s\'""" % zone[0])
            mxs = cursor.fetchall()
            context = Context({
                'zone': zone,
                'nameservers': nameservers,
                'addresses': addresses,
                'mxs': mxs
                })
            zonefile.write(self.templates['zone'].render(context))
            externalzonefile.write(self.templates['zone'].render(context))

            querystring = """SELECT h.hostname, p.ip_addr,
            n.name, c.cname, m.priority, m.mx, n.dns_view
            FROM (((((hostbase_host h INNER JOIN hostbase_interface i ON h.id = i.host_id)
            INNER JOIN hostbase_ip p ON i.id = p.interface_id)
            INNER JOIN hostbase_name n ON p.id = n.ip_id)
            INNER JOIN hostbase_name_mxs x ON n.id = x.name_id)
            INNER JOIN hostbase_mx m ON m.id = x.mx_id)
            LEFT JOIN hostbase_cname c ON n.id = c.name_id
            WHERE n.name LIKE '%%%%%s'
            AND h.status = 'active'
            ORDER BY h.hostname, n.name, p.ip_addr
            """ % zone[1]
            cursor.execute(querystring)
            zonehosts = cursor.fetchall()
            prevhost = (None, None, None, None)
            cnames = StringIO()
            cnamesexternal = StringIO()
            for host in zonehosts:
                if not host[2].split(".", 1)[1] == zone[1]:
                    zonefile.write(cnames.getvalue())
                    externalzonefile.write(cnamesexternal.getvalue())
                    cnames = StringIO()
                    cnamesexternal = StringIO()
                    continue
                if not prevhost[1] == host[1] or not prevhost[2] == host[2]:
                    zonefile.write(cnames.getvalue())
                    externalzonefile.write(cnamesexternal.getvalue())
                    cnames = StringIO()
                    cnamesexternal = StringIO()
                    zonefile.write("%-32s%-10s%-32s\n" %
                                   (host[2].split(".", 1)[0], 'A', host[1]))
                    zonefile.write("%-32s%-10s%-3s%s.\n" %
                                   ('', 'MX', host[4], host[5]))
                    if host[6] == 'global':
                        externalzonefile.write("%-32s%-10s%-32s\n" %
                                               (host[2].split(".", 1)[0], 'A', host[1]))
                        externalzonefile.write("%-32s%-10s%-3s%s.\n" %
                                               ('', 'MX', host[4], host[5]))
                elif not prevhost[5] == host[5]:
                    zonefile.write("%-32s%-10s%-3s%s.\n" %
                                   ('', 'MX', host[4], host[5]))
                    if host[6] == 'global':
                        externalzonefile.write("%-32s%-10s%-3s%s.\n" %
                                         ('', 'MX', host[4], host[5]))

                if host[3]:
                    try:
                        if host[3].split(".", 1)[1] == zone[1]:
                            cnames.write("%-32s%-10s%-32s\n" %
                                         (host[3].split(".", 1)[0],
                                          'CNAME', host[2].split(".", 1)[0]))
                            if host[6] == 'global':
                                cnamesexternal.write("%-32s%-10s%-32s\n" %
                                                     (host[3].split(".", 1)[0],
                                                      'CNAME', host[2].split(".", 1)[0]))
                        else:
                            cnames.write("%-32s%-10s%-32s\n" %
                                         (host[3] + ".",
                                          'CNAME',
                                          host[2].split(".", 1)[0]))
                            if host[6] == 'global':
                                cnamesexternal.write("%-32s%-10s%-32s\n" %
                                                     (host[3] + ".",
                                                      'CNAME',
                                                      host[2].split(".", 1)[0]))

                    except:
                        pass
                prevhost = host
            zonefile.write(cnames.getvalue())
            externalzonefile.write(cnamesexternal.getvalue())
            zonefile.write("\n\n%s" % zone[9])
            externalzonefile.write("\n\n%s" % zone[9])
            self.filedata[zone[1]] = zonefile.getvalue()
            self.filedata[zone[1] + ".external"] = externalzonefile.getvalue()
            zonefile.close()
            externalzonefile.close()
            self.Entries['ConfigFile']["%s/%s" % (self.filepath, zone[1])] = self.FetchFile
            self.Entries['ConfigFile']["%s/%s.external" % (self.filepath, zone[1])] = self.FetchFile

        cursor.execute("SELECT * FROM hostbase_zone WHERE zone LIKE \'%%.rev\' AND zone <> \'.rev\'")
        reversezones = cursor.fetchall()

        reversenames = []
        for reversezone in reversezones:
            cursor.execute("""SELECT n.name FROM hostbase_zone_nameservers z
            INNER JOIN hostbase_nameserver n ON z.nameserver_id = n.id
            WHERE z.zone_id = \'%s\'""" % reversezone[0])
            reverse_nameservers = cursor.fetchall()

            context = Context({
                'inaddr': reversezone[1].rstrip('.rev'),
                'zone': reversezone,
                'nameservers': reverse_nameservers,
                })

            self.filedata[reversezone[1]] = self.templates['reversesoa'].render(context)
            self.filedata[reversezone[1] + '.external'] = self.templates['reversesoa'].render(context)
            self.filedata[reversezone[1]] += reversezone[9]
            self.filedata[reversezone[1] + '.external'] += reversezone[9]

            subnet = reversezone[1].split(".")
            subnet.reverse()
            reversenames.append((reversezone[1].rstrip('.rev'), ".".join(subnet[1:])))

        for filename in reversenames:
            cursor.execute("""
            SELECT DISTINCT h.hostname, p.ip_addr, n.dns_view FROM ((hostbase_host h
            INNER JOIN hostbase_interface i ON h.id = i.host_id)
            INNER JOIN hostbase_ip p ON i.id = p.interface_id)
            INNER JOIN hostbase_name n ON n.ip_id = p.id
            WHERE p.ip_addr LIKE '%s%%%%' AND h.status = 'active' ORDER BY p.ip_addr
            """ % filename[1])
            reversehosts = cursor.fetchall()
            zonefile = StringIO()
            externalzonefile = StringIO()
            if len(filename[0].split(".")) == 2:
                originlist = []
                [originlist.append((".".join([ip[1].split(".")[2], filename[0]]),
                                    ".".join([filename[1], ip[1].split(".")[2]])))
                 for ip in reversehosts
                 if (".".join([ip[1].split(".")[2], filename[0]]),
                     ".".join([filename[1], ip[1].split(".")[2]])) not in originlist]
                for origin in originlist:
                    hosts = [(host[1].split("."), host[0])
                             for host in reversehosts
                             if host[1].rstrip('0123456789').rstrip('.') == origin[1]]
                    hosts_external = [(host[1].split("."), host[0])
                                     for host in reversehosts
                                     if (host[1].rstrip('0123456789').rstrip('.') == origin[1]
                                         and host[2] == 'global')]
                    context = Context({
                        'hosts': hosts,
                        'inaddr': origin[0],
                        'fileorigin': filename[0],
                        })
                    zonefile.write(self.templates['reverseapp'].render(context))
                    context = Context({
                        'hosts': hosts_external,
                        'inaddr': origin[0],
                        'fileorigin': filename[0],
                        })
                    externalzonefile.write(self.templates['reverseapp'].render(context))
            else:
                originlist = [filename[0]]
                hosts = [(host[1].split("."), host[0])
                         for host in reversehosts
                         if (host[1].split("."), host[0]) not in hosts]
                hosts_external = [(host[1].split("."), host[0])
                                  for host in reversehosts
                                  if ((host[1].split("."), host[0]) not in hosts_external
                                  and host[2] == 'global')]
                context = Context({
                    'hosts': hosts,
                    'inaddr': filename[0],
                    'fileorigin': None,
                    })
                zonefile.write(self.templates['reverseapp'].render(context))
                context = Context({
                    'hosts': hosts_external,
                    'inaddr': filename[0],
                    'fileorigin': None,
                    })
                externalzonefile.write(self.templates['reverseapp'].render(context))
            self.filedata['%s.rev' % filename[0]] += zonefile.getvalue()
            self.filedata['%s.rev.external' % filename[0]] += externalzonefile.getvalue()
            zonefile.close()
            externalzonefile.close()
            self.Entries['ConfigFile']['%s/%s.rev' % (self.filepath, filename[0])] = self.FetchFile
            self.Entries['ConfigFile']['%s/%s.rev.external' % (self.filepath, filename[0])] = self.FetchFile

        ## here's where the named.conf file gets written
        context = Context({
            'zones': zones,
            'reverses': reversenames,
            })
        self.filedata['named.conf'] = self.templates['named'].render(context)
        self.Entries['ConfigFile']['/my/adm/hostbase/files/named.conf'] = self.FetchFile
        self.filedata['named.conf.views'] = self.templates['namedviews'].render(context)
        self.Entries['ConfigFile']['/my/adm/hostbase/files/named.conf.views'] = self.FetchFile