aboutsummaryrefslogtreecommitdiff
path: root/scripts/convert-https-to-http.py
blob: 821255c6a6afe1931d4213394186afd641b57fda (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
pre { line-height: 125%; }
td.linenos .normal { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
span.linenos { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
td.linenos .special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
span.linenos.special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
.highlight .hll { background-color: #ffffcc }
.highlight .c { color: #888888 } /* Comment */
.highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.highlight .k { color: #008800; font-weight: bold } /* Keyword */
.highlight .ch { color: #888888 } /* Comment.Hashbang */
.highlight .cm { color: #888888 } /* Comment.Multiline */
.highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */
.highlight .cpf { color: #888888 } /* Comment.PreprocFile */
.highlight .c1 { color: #888888 } /* Comment.Single */
.highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */
.highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .ges { font-weight: bold; font-style: italic } /* Generic.EmphStrong */
.highlight .gr { color: #aa0000 } /* Generic.Error */
.highlight .gh { color: #333333 } /* Generic.Heading */
.highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */
.highlight .go { color: #888888 } /* Generic.Output */
.highlight .gp { color: #555555 } /* Generic.Prompt */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #666666 } /* Generic.Subheading */
.highlight .gt { color: #aa0000 } /* Generic.Traceback */
.highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */
.highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */
.highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */
.highlight .kp { color: #008800 } /* Keyword.Pseudo */
.highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */
.highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */
.highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */
.highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */
.highlight .na { color: #336699 } /* Name.Attribute */
.highlight .nb { color: #003388 } /* Name.Builtin */
.highlight 
"""
Conversion tool for flag day on which we rip TLS (HTTPS) support out
of rpkid and friends.

Usage: python convert-https-to-http.py [ { -c | --config } configfile ]
                                       [ { -e | --entitydb } entitydbdir ]
                                       [ { -h | --help } ]

Default configuration file is myrpki.conf, override with --config option.

$Id$

Copyright (C) 2010  Internet Systems Consortium ("ISC")

Permission to use, copy, modify, and distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
"""

from __future__ import with_statement

import getopt, sys, os, warnings, lxml.etree, rpki.config

cfg_file = "myrpki.conf"
entitydb_dir = "entitydb"
convert_sql = True

opts, argv = getopt.getopt(sys.argv[1:], "c:e:h?", ["config=", "entitydb=", "help"])
for o, a in opts:
  if o in ("-h", "--help", "-?"):
    print __doc__
    sys.exit(0)
  elif o in ("-c", "--config"):
    cfg_file = a
  elif o in ("-e", "--entitydb"):
    entitydb_dir = a
if argv:
  sys.exit("Unexpected arguments %s" % argv)

print "Checking", cfg_file
f = open(cfg_file + ".new", "w")
for line in open(cfg_file, "r"):
  cmd, sep, comment = line.partition("#")
  if "https" in cmd:
    line = cmd.replace("https", "http") + sep + comment
    print "Rewrote line:", " ".join(line.split())
  f.write(line)
f.close()
os.rename(cfg_file + ".new", cfg_file)

def localname(s):
  return s.partition("}")[-1]

for root, dirs, files in os.walk(entitydb_dir):
  for filename in files:
    if filename.endswith(".xml"):
      filename = os.path.join(root, filename)
      print "Checking", filename
      tree = lxml.etree.ElementTree(file = filename)
      changed = False
      for e in tree.getiterator():
        p = e.getparent()
        if (e.tag in ("{http://www.hactrn.net/uris/rpki/myrpki/}bpki_https_cert",
                      "{http://www.hactrn.net/uris/rpki/myrpki/}bpki_https_glue",
                      "{http://www.hactrn.net/uris/rpki/myrpki/}bpki_https_certificate") or
            (e.tag == "{http://www.hactrn.net/uris/rpki/myrpki/}bpki_server_ta" and
             p.tag == "{http://www.hactrn.net/uris/rpki/myrpki/}parent")):
          print "Deleting element %s/%s" % (localname(p.tag), localname(e.tag))
          p.remove(e)
          changed = True
          continue
        for k, v in e.items():
          if v.startswith("https://"):
            e.set(k, v.replace("https://", "http://"))
            print "Rewrote attribute %s/@%s to %s" % (localname(e.tag), k, e.get(k))
            changed = True
      if changed:
        tree.write(filename + ".new")
        os.rename(filename + ".new", filename)


# Automatic conversion of SQL is particularly dangerous, so we only do it on request

if convert_sql:

  if hasattr(warnings, "catch_warnings"):
    with warnings.catch_warnings():
      warnings.simplefilter("ignore", DeprecationWarning)
      import MySQLdb
  else:
    import MySQLdb

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

  print "Converting SQL tables"

  def do_sql(section, *cmds):
    if cfg.getboolean("run_" + section):
      db = MySQLdb.connect(user   = cfg.get("sql-username", section = section),
                           db     = cfg.get("sql-database", section = section),
                           passwd = cfg.get("sql-password", section = section))
      cur = db.cursor()
      ok = True
      for cmd in cmds:
        try:
          print "SQL[%s]: %s" % (section, cmd)
          cur.execute(cmd)
        except MySQLdb.Error, e:
          print str(e)
          ok = False
      if ok:
        print "SQL[%s]: Comitting" % section
        db.commit()
      else:
        print "SQL[%s]: NOT comitting due to previous errors" % section
      db.close()


  do_sql("rpkid",
         "ALTER TABLE repository ADD COLUMN last_cms_timestamp DATETIME",
         "ALTER TABLE parent ADD COLUMN last_cms_timestamp DATETIME",
         "ALTER TABLE parent DROP COLUMN bpki_https_cert",
         "ALTER TABLE parent DROP COLUMN bpki_https_glue",
         "ALTER TABLE child ADD COLUMN last_cms_timestamp DATETIME",
         "ALTER TABLE ca CHANGE COLUMN parent_id parent_id BIGINT UNSIGNED NOT NULL")

  do_sql("pubd",
         "ALTER TABLE client ADD COLUMN last_cms_timestamp DATETIME")


print """
Done.  Don't forget to run "myrpki configure_daemons" (with whatever arguments you usually
give it) to push changed URLs and so forth into rpkid et al.
"""