Beispiel #1
0
def pre_django_sql_setup(needed):

    username = config_overrides["irdbd_sql_username"]
    password = config_overrides["irdbd_sql_password"]

    # If we have the MySQL root password, just blow away and recreate
    # the required databases.  Otherwise, check for missing databases,
    # then blow away all tables in the required databases.  In either
    # case, we assume that the Django syncdb code will populate
    # databases as necessary, all we need to do here is provide empty
    # databases for the Django code to fill in.

    if mysql_rootpass is not None:
        if mysql_rootpass:
            db = MySQLdb.connect(user=mysql_rootuser, passwd=mysql_rootpass)
        else:
            db = MySQLdb.connect(user=mysql_rootuser)
        cur = db.cursor()
        for database in needed:
            try:
                cur.execute("DROP DATABASE IF EXISTS %s" % database)
            except:
                pass
            cur.execute("CREATE DATABASE %s" % database)
            cur.execute(
                "GRANT ALL ON %s.* TO %s@localhost IDENTIFIED BY %%s" %
                (database, username), (password, ))

    else:
        db = MySQLdb.connect(user=username, passwd=password)
        cur = db.cursor()
        cur.execute("SHOW DATABASES")
        existing = set(r[0] for r in cur.fetchall())
        if needed - existing:
            sys.stderr.write("The following databases are missing:\n")
            for database in sorted(needed - existing):
                sys.stderr.write("  %s\n" % database)
            sys.stderr.write(
                "Please create them manually or put MySQL root password in my config file\n"
            )
            sys.exit(
                "Missing databases and MySQL root password not known, can't continue"
            )
        for database in needed:
            db.select_db(database)
            cur.execute("SHOW TABLES")
            tables = [r[0] for r in cur.fetchall()]
            cur.execute("SET foreign_key_checks = 0")
            for table in tables:
                cur.execute("DROP TABLE %s" % table)
            cur.execute("SET foreign_key_checks = 1")

    cur.close()
    db.commit()
    db.close()
Beispiel #2
0
def pre_django_sql_setup(needed):

    username = config_overrides["irdbd_sql_username"]
    password = config_overrides["irdbd_sql_password"]

    # If we have the MySQL root password, just blow away and recreate
    # the required databases.  Otherwise, check for missing databases,
    # then blow away all tables in the required databases.  In either
    # case, we assume that the Django syncdb code will populate
    # databases as necessary, all we need to do here is provide empty
    # databases for the Django code to fill in.

    if mysql_rootpass is not None:
        if mysql_rootpass:
            db = MySQLdb.connect(user = mysql_rootuser, passwd = mysql_rootpass)
        else:
            db = MySQLdb.connect(user = mysql_rootuser)
        cur = db.cursor()
        for database in needed:
            try:
                cur.execute("DROP DATABASE IF EXISTS %s" % database)
            except:
                pass
            cur.execute("CREATE DATABASE %s" % database)
            cur.execute("GRANT ALL ON %s.* TO %s@localhost IDENTIFIED BY %%s" % (
                database, username), (password,))

    else:
        db = MySQLdb.connect(user = username, passwd = password)
        cur = db.cursor()
        cur.execute("SHOW DATABASES")
        existing = set(r[0] for r in cur.fetchall())
        if needed - existing:
            sys.stderr.write("The following databases are missing:\n")
            for database in sorted(needed - existing):
                sys.stderr.write("  %s\n" % database)
            sys.stderr.write("Please create them manually or put MySQL root password in my config file\n")
            sys.exit("Missing databases and MySQL root password not known, can't continue")
        for database in needed:
            db.select_db(database)
            cur.execute("SHOW TABLES")
            tables = [r[0] for r in cur.fetchall()]
            cur.execute("SET foreign_key_checks = 0")
            for table in tables:
                cur.execute("DROP TABLE %s" % table)
            cur.execute("SET foreign_key_checks = 1")

    cur.close()
    db.commit()
    db.close()
Beispiel #3
0
def sql_setup(name):
  """
  Create a new SQL database and construct all its tables.
  """
  database = cfg.get("sql-database", section = name)
  username = cfg.get("sql-username", section = name)
  password = cfg.get("sql-password", section = name)
  schema = read_schema(name)

  print "Creating database", database
  cur = rootdb.cursor()
  try:
    cur.execute("DROP DATABASE IF EXISTS %s" %  database)
  except Exception:
    pass
  cur.execute("CREATE DATABASE %s" % database)
  cur.execute("GRANT ALL ON %s.* TO %s@localhost IDENTIFIED BY %%s" % (database, username), (password,))
  rootdb.commit()

  db = MySQLdb.connect(db = database, user = username, passwd = password)
  cur = db.cursor()
  for statement in schema:
    if statement.upper().startswith("DROP TABLE"):
      continue
    if verbose:
      print "+", statement
    cur.execute(statement)
  db.commit()
  db.close()
Beispiel #4
0
 def connect(self):
   self.db = MySQLdb.connect(user   = self.username,
                             db     = self.database,
                             passwd = self.password,
                             conv   = self.conv)
   self.cur = self.db.cursor()
   self.db.autocommit(True)
   self.timestamp = rpki.sundial.now()
Beispiel #5
0
 def connect(self):
     self.db = MySQLdb.connect(user=self.username,
                               db=self.database,
                               passwd=self.password,
                               conv=self.conv)
     self.cur = self.db.cursor()
     self.db.autocommit(True)
     self.timestamp = rpki.sundial.now()
def fix(name, *statements):
    db = MySQLdb.connect(db     = cfg.get("sql-database", section = name),
                         user   = cfg.get("sql-username", section = name),
                         passwd = cfg.get("sql-password", section = name))
    cur = db.cursor()
    for statement in statements:
        cur.execute(statement)
    db.commit()
    db.close()
Beispiel #7
0
def fix(name, *statements):
    db = MySQLdb.connect(db=cfg.get("sql-database", section=name),
                         user=cfg.get("sql-username", section=name),
                         passwd=cfg.get("sql-password", section=name))
    cur = db.cursor()
    for statement in statements:
        cur.execute(statement)
    db.commit()
    db.close()
Beispiel #8
0
    def __init__(self):

        os.environ["TZ"] = "UTC"
        time.tzset()

        parser = argparse.ArgumentParser(description=__doc__)
        parser.add_argument(
            "-c",
            "--config",
            help="override default location of configuration file")
        parser.add_argument(
            "-f",
            "--foreground",
            action="store_true",
            help="do not daemonize (ignored, old_irdbd never daemonizes)")
        rpki.log.argparse_setup(parser)
        args = parser.parse_args()

        rpki.log.init("irdbd", args)

        self.cfg = rpki.config.parser(args.config, "irdbd")

        startup_msg = self.cfg.get("startup-message", "")
        if startup_msg:
            logger.info(startup_msg)

        self.cfg.set_global_flags()

        self.db = MySQLdb.connect(user=self.cfg.get("sql-username"),
                                  db=self.cfg.get("sql-database"),
                                  passwd=self.cfg.get("sql-password"))

        self.cur = self.db.cursor()
        self.db.autocommit(True)

        self.bpki_ta = rpki.x509.X509(Auto_update=self.cfg.get("bpki-ta"))
        self.rpkid_cert = rpki.x509.X509(
            Auto_update=self.cfg.get("rpkid-cert"))
        self.irdbd_cert = rpki.x509.X509(
            Auto_update=self.cfg.get("irdbd-cert"))
        self.irdbd_key = rpki.x509.RSA(Auto_update=self.cfg.get("irdbd-key"))

        u = urlparse.urlparse(self.cfg.get("http-url"))

        assert u.scheme in ("", "http") and \
               u.username is None and \
               u.password is None and \
               u.params   == "" and \
               u.query    == "" and \
               u.fragment == ""

        rpki.http.server(host=u.hostname or "localhost",
                         port=u.port or 443,
                         handlers=((u.path, self.handler), ))
Beispiel #9
0
  def __init__(self):

    os.environ["TZ"] = "UTC"
    time.tzset()

    cfg_file = None

    opts, argv = getopt.getopt(sys.argv[1:], "c:dh?", ["config=", "debug", "help"])
    for o, a in opts:
      if o in ("-h", "--help", "-?"):
        print __doc__
        sys.exit(0)
      if o in ("-c", "--config"):
        cfg_file = a
      elif o in ("-d", "--debug"):
        rpki.log.use_syslog = False
    if argv:
      raise rpki.exceptions.CommandParseFailure, "Unexpected arguments %s" % argv

    rpki.log.init("irdbd")

    self.cfg = rpki.config.parser(cfg_file, "irdbd")

    startup_msg = self.cfg.get("startup-message", "")
    if startup_msg:
      rpki.log.info(startup_msg)

    self.cfg.set_global_flags()

    self.db = MySQLdb.connect(user   = self.cfg.get("sql-username"),
                              db     = self.cfg.get("sql-database"),
                              passwd = self.cfg.get("sql-password"))

    self.cur = self.db.cursor()
    self.db.autocommit(True)

    self.bpki_ta         = rpki.x509.X509(Auto_update = self.cfg.get("bpki-ta"))
    self.rpkid_cert      = rpki.x509.X509(Auto_update = self.cfg.get("rpkid-cert"))
    self.irdbd_cert      = rpki.x509.X509(Auto_update = self.cfg.get("irdbd-cert"))
    self.irdbd_key       = rpki.x509.RSA( Auto_update = self.cfg.get("irdbd-key"))

    u = urlparse.urlparse(self.cfg.get("http-url"))

    assert u.scheme in ("", "http") and \
           u.username is None and \
           u.password is None and \
           u.params   == "" and \
           u.query    == "" and \
           u.fragment == ""

    rpki.http.server(host         = u.hostname or "localhost",
                     port         = u.port or 443,
                     handlers     = ((u.path, self.handler),))
Beispiel #10
0
def sql_create_dbs(cfg_file, rootpw):
  cfg = rpki.config.parser(cfg_file, "myrpki")
  rootdb = MySQLdb.connect(db = "mysql", user = "******", passwd = rootpw)
  print "*********", cfg_file

  if cfg.getboolean("start_irdbd", False):
    sql_create_db(cfg, rootdb, "irdbd")

  if cfg.getboolean("start_rpkid", False):
    sql_create_db(cfg,  rootdb,"rpkid")

  if cfg.getboolean("start_pubd",  False):
    sql_create_db(cfg, rootdb, "pubd")
  rootdb.close()
  def __init__(self):

    os.environ["TZ"] = "UTC"
    time.tzset()

    parser = argparse.ArgumentParser(description = __doc__)
    parser.add_argument("-c", "--config",
                        help = "override default location of configuration file")
    parser.add_argument("-f", "--foreground", action = "store_true",
                        help = "do not daemonize (ignored, old_irdbd never daemonizes)")
    rpki.log.argparse_setup(parser)
    args = parser.parse_args()

    rpki.log.init("irdbd", args)

    self.cfg = rpki.config.parser(args.config, "irdbd")

    startup_msg = self.cfg.get("startup-message", "")
    if startup_msg:
      logger.info(startup_msg)

    self.cfg.set_global_flags()

    self.db = MySQLdb.connect(user   = self.cfg.get("sql-username"),
                              db     = self.cfg.get("sql-database"),
                              passwd = self.cfg.get("sql-password"))

    self.cur = self.db.cursor()
    self.db.autocommit(True)

    self.bpki_ta         = rpki.x509.X509(Auto_update = self.cfg.get("bpki-ta"))
    self.rpkid_cert      = rpki.x509.X509(Auto_update = self.cfg.get("rpkid-cert"))
    self.irdbd_cert      = rpki.x509.X509(Auto_update = self.cfg.get("irdbd-cert"))
    self.irdbd_key       = rpki.x509.RSA( Auto_update = self.cfg.get("irdbd-key"))

    u = urlparse.urlparse(self.cfg.get("http-url"))

    assert u.scheme in ("", "http") and \
           u.username is None and \
           u.password is None and \
           u.params   == "" and \
           u.query    == "" and \
           u.fragment == ""

    rpki.http.server(host         = u.hostname or "localhost",
                     port         = u.port or 443,
                     handlers     = ((u.path, self.handler),))
Beispiel #12
0
  def connect(self):
    self.db = MySQLdb.connect(user   = self.username,
                              db     = self.database,
                              passwd = self.password,
                              conv   = self.conv)
    self.cur = self.db.cursor()
    self.db.autocommit(True)
    self.timestamp = rpki.sundial.now()

    # Try this as a workaround for MySQL 5.6 UTF8 characterset
    # braindamage, in which MySQL starts rejecting ASN.1 DER because
    # it's not valid UTF-8.  Twits.
    #
    # Except that it breaks MySQL 5.5, so wrap it and ignore errors.  Twits ** 2.
    try:
      self.execute("charset = latin1")
    except:
      logger.info("Whacking charset to Latin1 to save MySQL 5.6 from its own confusion failed, blundering onwards")
Beispiel #13
0
    def __init__(self):

        os.environ["TZ"] = "UTC"
        time.tzset()

        self.cfg = rpki.config.argparser(section = "irdbd", doc = __doc__)
        self.cfg.add_boolean_argument("--foreground", default = False,
                                      help = "do not daemonize (ignored, old_irdbd never daemonizes)")
        self.cfg.add_logging_arguments()
        args = parser.parse_args()

        cfg.configure_logging(args = args, ident = "irdbd")

        startup_msg = self.cfg.get("startup-message", "")
        if startup_msg:
            logger.info(startup_msg)

        self.cfg.set_global_flags()

        self.db = MySQLdb.connect(user   = self.cfg.get("sql-username"),
                                  db     = self.cfg.get("sql-database"),
                                  passwd = self.cfg.get("sql-password"))

        self.cur = self.db.cursor()
        self.db.autocommit(True)

        self.bpki_ta         = rpki.x509.X509(Auto_update = self.cfg.get("bpki-ta"))
        self.rpkid_cert      = rpki.x509.X509(Auto_update = self.cfg.get("rpkid-cert"))
        self.irdbd_cert      = rpki.x509.X509(Auto_update = self.cfg.get("irdbd-cert"))
        self.irdbd_key       = rpki.x509.RSA( Auto_update = self.cfg.get("irdbd-key"))

        u = urlparse.urlparse(self.cfg.get("http-url"))

        assert u.scheme in ("", "http") and \
               u.username is None and \
               u.password is None and \
               u.params   == "" and \
               u.query    == "" and \
               u.fragment == ""

        rpki.http_simple.server(host         = u.hostname or "localhost",
                                port         = u.port or 443,
                                handlers     = ((u.path, self.handler),))
Beispiel #14
0
    def connect(self):
        self.db = MySQLdb.connect(user=self.username,
                                  db=self.database,
                                  passwd=self.password,
                                  conv=self.conv)
        self.cur = self.db.cursor()
        self.db.autocommit(True)
        self.timestamp = rpki.sundial.now()

        # Try this as a workaround for MySQL 5.6 UTF8 characterset
        # braindamage, in which MySQL starts rejecting ASN.1 DER because
        # it's not valid UTF-8.  Twits.
        #
        # Except that it breaks MySQL 5.5, so wrap it and ignore errors.  Twits ** 2.
        try:
            self.execute("charset = latin1")
        except:
            logger.info(
                "Whacking charset to Latin1 to save MySQL 5.6 from its own confusion failed, blundering onwards"
            )
Beispiel #15
0
 def connect(self):
   self.db = MySQLdb.connect(user = self.username, db = self.database, passwd = self.password)
   self.cur = self.db.cursor()
   self.db.autocommit(True)
Beispiel #16
0
  db.close()

cfg_file = None

verbose = False

opts, argv = getopt.getopt(sys.argv[1:], "c:hv?", ["config=", "help", "verbose"])
for o, a in opts:
  if o in ("-h", "--help", "-?"):
    print __doc__
    sys.exit(0)
  if o in ("-v", "--verbose"):
    verbose = True
  if o in ("-c", "--config"):
    cfg_file = a

cfg = rpki.config.parser(cfg_file, "myrpki")

rootdb = MySQLdb.connect(db = "mysql", user = "******", passwd = getpass.getpass("Please enter your MySQL root password: "******"start_irdbd", False):
  sql_setup("irdbd")

if cfg.getboolean("start_rpkid", False):
  sql_setup("rpkid")

if cfg.getboolean("start_pubd",  False):
  sql_setup("pubd")

rootdb.close()
Beispiel #17
0
# WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
"""
(Re)Initialize SQL tables used by these programs.
"""

import rpki.config
from rpki.mysql_import import MySQLdb

cfg = rpki.config.parser(section="yamltest", allow_missing=True)

for name in ("rpkid", "irdbd", "pubd"):

    username = cfg.get("%s_sql_username" % name, name[:4])
    password = cfg.get("%s_sql_password" % name, "fnord")

    db = MySQLdb.connect(user=username, passwd=password)
    cur = db.cursor()

    cur.execute("SHOW DATABASES")

    databases = [
        r[0] for r in cur.fetchall()
        if r[0][:4] == name[:4] and r[0][4:].isdigit()
    ]

    for database in databases:

        cur.execute("USE " + database)

        cur.execute("SHOW TABLES")
        tables = [r[0] for r in cur.fetchall()]
# OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
# PERFORMANCE OF THIS SOFTWARE.

"""
Dump backup copies of SQL tables used by these programs.
"""

import subprocess
import rpki.config
from rpki.mysql_import import MySQLdb

cfg = rpki.config.parser(None, "yamltest", allow_missing = True)

for name in ("rpkid", "irdbd", "pubd"):

  username = cfg.get("%s_sql_username" % name, name[:4])
  password = cfg.get("%s_sql_password" % name, "fnord")

  cmd = ["mysqldump", "-u", username, "-p" + password, "--databases"]

  db = MySQLdb.connect(user = username, passwd = password)
  cur = db.cursor()

  cur.execute("SHOW DATABASES")
  cmd.extend(r[0] for r in cur.fetchall() if r[0][:4] == name[:4] and r[0][4:].isdigit())

  cur.close()
  db.close()

  subprocess.check_call(cmd, stdout = open("backup.%s.sql" % name, "w"))
Beispiel #19
0
parser.add_argument("-c", "--config",
                    help = "specify alternate location for rpki.conf")
parser.add_argument("-p", "--protocol",
                    choices = (0, 1, 2), type = int, default = 2,
                    help = "pickling protocol to use")
parser.add_argument("output",
                    help = "output file")
args = parser.parse_args()

cfg = rpki.config.parser(args.config)

databases = {}

for section in ("rpkid", "irdbd", "pubd"):
    db = MySQLdb.connect(db     = cfg.get(section = section, option = "sql-database"),
                         user   = cfg.get(section = section, option = "sql-username"),
                         passwd = cfg.get(section = section, option = "sql-password"))
    tables = {}

    cur = db.cursor()
    cur.execute("SHOW TABLES")
    table_names = tuple(row[0] for row in cur.fetchall())
    cur.close()

    cur = db.cursor(MySQLdb.cursors.DictCursor)
    for name in table_names:
        cur.execute("SELECT * FROM " + name)
        tables[name] = cur.fetchall()
    cur.close()

    db.close()
Beispiel #20
0
for name in ("rpkid", "irdbd", "pubd"):

  username = cfg.get("%s_sql_username" % name, name[:4])
  password = cfg.get("%s_sql_password" % name, "fnord")
  
  schema = []
  for line in getattr(rpki.sql_schemas, name, "").splitlines():
    schema.extend(line.partition("--")[0].split())
  schema = " ".join(schema).strip(";").split(";")
  schema = [statement.strip() for statement in schema if statement and "DROP TABLE" not in statement]

  for i in xrange(12):

    database = "%s%d" % (name[:4], i)

    db = MySQLdb.connect(user = username, db = database, passwd = password)
    cur = db.cursor()

    cur.execute("SHOW TABLES")
    tables = [r[0] for r in cur.fetchall()]

    cur.execute("SET foreign_key_checks = 0")
    for table in tables:
      cur.execute("DROP TABLE %s" % table)
    cur.execute("SET foreign_key_checks = 1")  

    for statement in schema:
      cur.execute(statement)

    cur.close()
    db.close()