aboutsummaryrefslogtreecommitdiff
path: root/scripts/resource-cert-samples/RIR/22.pem
blob: f46564ddbfa83b67936df5f815feef1c33265b73 (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
67
68
69
70
71
72
73
74
75
76
Certificate:
    Data:
        Version: 3 (0x2)
        Serial Number: 34 (0x22)
        Signature Algorithm: sha256WithRSAEncryption
        Issuer: CN=TEST ENTITY RIR
        Validity
            Not Before: Aug 10 01:13:39 2007 GMT
            Not After : Aug  9 01:13:39 2008 GMT
        Subject: CN=TEST ENTITY LIR1
        Subject Public Key Info:
            Public Key Algorithm: rsaEncryption
            RSA Public Key: (2048 bit)
                Modulus (2048 bit):
                    00:af:5d:1c:f9:d9:bb:d5:01:e1:5b:36:cc:51:f6:
                    fd:86:57:60:aa:9e:c7:ec:4e:05:af:fb:51:5c:7a:
                    c2:58:c4:a8:57:ae:14:62:e9:bc:b6:72:7d:cf:49:
                    c8:4a:40:82:4a:f4:3e:30:b5:94:25:9e:6c:78:81:
                    57:43:d6:85:02:8d:d1:9c:b5:d7:34:2f:e2:a9:7d:
                    18:27:b5:47:9a:42:16:c8:90:7f:96:2c:dd:b8:98:
                    17:1f:77:62:4a:08:00:2d:e0:73:0c:39:37:ba:0f:
                    a7:59:59:4c:7c:cd:e2:5c:d7:98:36:10:6c:88:3e:
                    45:99:a6:88:2f:f6:7f:31:49:ba:42:2b:13:79:c2:
                    b2:f1:09:d9:ad:37:a4:41:b6:6d:46:a1:18:05:a0:
                    53:07:8e:e0:98:b2:d1:fd:67:68:77:64:d5:f3:fe:
                    1d:22:36:9e:26:5a:1a:aa:18:94:c3:2c:7e:9a:af:
                    be:2c:9d:5e:75:2c:49:d6:37:2b:06:1f:cc:63:97:
                    7e:ee:2c:5f:67:af:4d:62:3e:7a:1f:0c:e1:1e:02:
                    f2:d2:06:75:ae:3f:11:bc:8e:0f:13:64:38:14:36:
                    1d:5d:02:ec:af:65:d5:b9:68:f4:22:66:2b:ef:47:
                    5b:ad:3b:f2:af:b6:71:0c:94:56:8a:7c:01:36:f0:
                    3a:3f
                Exponent: 65537 (0x10001)
        X509v3 extensions:
            X509v3 Basic Constraints: critical
                CA:TRUE
            X509v3 Key Usage: critical
                Certificate Sign, CRL Sign
            Subject Information Access: 
                1.3.6.1.5.5.7.48.5 - URI:rsync://wombats-r-us.hactrn.net/LIR1/

    Signature Algorithm: sha256WithRSAEncryption
        64:a2:b2:a2:9e:50:69:0e:9f:15:f1:85:10:e8:5e:bf:22:c0:
        2e:4c:58:1c:43:c8:ba:7d:3d:67:8c:a6:94:99:b0:fc:ec:2c:
        e2:0c:69:a7:d6:9e:35:b7:06:bd:d0:30:9a:ba:c9:1c:49:96:
        ee:06:68:45:e3:ed:48:4d:7a:c0:68:4f:57:52:e6:e7:f1:1c:
        14:58:d5:a2:da:d0:19:c5:e9:c4:63:4f:bc:3b:10:8b:2e:fe:
        b7:95:8a:f4:7e:00:ac:f8:5b:dc:4e:70:81:d7:9e:d8:4b:e8:
        89:03:05:3e:11:dc:8f:7a:45:a3:14:78:5f:9d:dc:fe:7f:fd:
        4a:b0:bb:33:e0:7c:46:f4:e3:df:f7:2b:9e:64:44:ba:39:b0:
        d4:72:a3:cf:35:55:ae:04:29:ed:d8:23:22:b0:a3:16:d2:5d:
        69:b9:c6:5a:e5:53:42:71:2b:5e:37:e1:1e:26:42:ce:29:23:
        64:2e:51:fd:a9:e4:9b:20:65:b1:f1:c2:ce:14:56:10:68:2f:
        fc:f3:eb:55:6d:d7:a0:0e:1a:0c:52:4a:81:47:e6:34:1f:9e:
        3a:c5:38:2e:e6:f2:43:bf:f8:e3:cb:cd:44:83:4f:7c:fb:69:
        a9:41:96:d6:50:22:b7:3c:06:e0:09:ff:34:cb:41:f6:17:97:
        86:7d:f0:c5
-----BEGIN CERTIFICATE-----
MIIDFjCCAf6gAwIBAgIBIjANBgkqhkiG9w0BAQsFADAaMRgwFgYDVQQDEw9URVNU
IEVOVElUWSBSSVIwHhcNMDcwODEwMDExMzM5WhcNMDgwODA5MDExMzM5WjAbMRkw
FwYDVQQDExBURVNUIEVOVElUWSBMSVIxMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8A
MIIBCgKCAQEAr10c+dm71QHhWzbMUfb9hldgqp7H7E4Fr/tRXHrCWMSoV64UYum8
tnJ9z0nISkCCSvQ+MLWUJZ5seIFXQ9aFAo3RnLXXNC/iqX0YJ7VHmkIWyJB/lizd
uJgXH3diSggALeBzDDk3ug+nWVlMfM3iXNeYNhBsiD5FmaaIL/Z/MUm6QisTecKy
8QnZrTekQbZtRqEYBaBTB47gmLLR/Wdod2TV8/4dIjaeJloaqhiUwyx+mq++LJ1e
dSxJ1jcrBh/MY5d+7ixfZ69NYj56HwzhHgLy0gZ1rj8RvI4PE2Q4FDYdXQLsr2XV
uWj0ImYr70dbrTvyr7ZxDJRWinwBNvA6PwIDAQABo2YwZDAPBgNVHRMBAf8EBTAD
AQH/MA4GA1UdDwEB/wQEAwIBBjBBBggrBgEFBQcBCwQ1MDMwMQYIKwYBBQUHMAWG
JXJzeW5jOi8vd29tYmF0cy1yLXVzLmhhY3Rybi5uZXQvTElSMS8wDQYJKoZIhvcN
AQELBQADggEBAGSisqKeUGkOnxXxhRDoXr8iwC5MWBxDyLp9PWeMppSZsPzsLOIM
aafWnjW3Br3QMJq6yRxJlu4GaEXj7UhNesBoT1dS5ufxHBRY1aLa0BnF6cRjT7w7
EIsu/reVivR+AKz4W9xOcIHXnthL6IkDBT4R3I96RaMUeF+d3P5//UqwuzPgfEb0
49/3K55kRLo5sNRyo881Va4EKe3YIyKwoxbSXWm5xlrlU0JxK1434R4mQs4pI2Qu
Uf2p5JsgZbHxws4UVhBoL/zz61Vt16AOGgxSSoFH5jQfnjrFOC7m8kO/+OPLzUSD
T3z7aalBltZQIrc8BuAJ/zTLQfYXl4Z98MU=
-----END CERTIFICATE-----
'#n527'>527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681
"""
This program is now the merger of three different tools: the old
myrpki.py script, the old myirbe.py script, and the newer setup.py CLI
tool.  As such, it is still in need of some cleanup, but the need to
provide a saner user interface is more urgent than internal code
prettiness at the moment.  In the long run, 90% of the code in this
file probably ought to move to well-designed library modules.

Overall goal here is to build up the configuration necessary to run
rpkid and friends, by reading a config file, a collection of .CSV
files, and the results of a few out-of-band XML setup messages
exchanged with one's parents, children, and so forth.

The config file is in an OpenSSL-compatible format, the CSV files are
simple tab-delimited text.  The XML files are all generated by this
program, either the local instance or an instance being run by another
player in the system; the mechanism used to exchange these setup
messages is outside the scope of this program, feel free to use
PGP-signed mail, a web interface (not provided), USB stick, carrier
pigeons, whatever works.

With one exception, the commands in this program avoid using any
third-party Python code other than the rpki libraries themselves; with
the same one exception, all OpenSSL work is done with the OpenSSL
command line tool (the one built as a side effect of building rcynic
will do, if your platform has no system copy or the system copy is too
old).  This is all done in an attempt to make the code more portable,
so one can run most of the RPKI back end software on a laptop or
whatever.  The one exception is the configure_daemons command, which
must, of necessity, use the same communication libraries as the
daemons with which it is conversing.  So that one command will not
work if the correct Python modules are not available.


$Id$

Copyright (C) 2009-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 subprocess, csv, re, os, getopt, sys, base64, time, glob, copy, warnings
import rpki.config, rpki.cli, rpki.sundial, rpki.log, rpki.oids

try:
  from lxml.etree            import (Element, SubElement, ElementTree,
                                     fromstring as ElementFromString,
                                     tostring   as ElementToString)
except ImportError:
  from xml.etree.ElementTree import (Element, SubElement, ElementTree,
                                     fromstring as ElementFromString,
                                     tostring   as ElementToString)



# Our XML namespace and protocol version.

namespace      = "http://www.hactrn.net/uris/rpki/myrpki/"
version        = "2"
namespaceQName = "{" + namespace + "}"

# Dialect for our use of CSV files, here to make it easy to change if
# your site needs to do something different.  See doc for the csv
# module in the Python standard libraries for details if you need to
# customize this.

csv_dialect = csv.get_dialect("excel-tab")

# Whether to include incomplete entries when rendering to XML.

allow_incomplete = False

# Whether to whine about incomplete entries while rendering to XML.

whine = False

class comma_set(set):
  """
  Minor customization of set(), to provide a print syntax.
  """

  def __str__(self):
    return ",".join(self)

class EntityDB(object):
  """
  Wrapper for entitydb path lookups.  Hmm, maybe some or all of the
  entitydb glob stuff should end up here too?  Later.
  """

  def __init__(self, cfg):
    self.dir = cfg.get("entitydb_dir", "entitydb")

  def __call__(self, *args):
    return os.path.join(self.dir, *args)

  def iterate(self, *args):
    return glob.iglob(os.path.join(self.dir, *args))

class roa_request(object):
  """
  Representation of a ROA request.
  """

  v4re = re.compile("^([0-9]{1,3}\.){3}[0-9]{1,3}/[0-9]+(-[0-9]+)?$", re.I)
  v6re = re.compile("^([0-9a-f]{0,4}:){0,15}[0-9a-f]{0,4}/[0-9]+(-[0-9]+)?$", re.I)

  def __init__(self, asn, group):
    self.asn = asn
    self.group = group
    self.v4 = comma_set()
    self.v6 = comma_set()

  def __repr__(self):
    s = "<%s asn %s group %s" % (self.__class__.__name__, self.asn, self.group)
    if self.v4:
      s += " v4 %s" % self.v4
    if self.v6:
      s += " v6 %s" % self.v6
    return s + ">"

  def add(self, prefix):
    """
    Add one prefix to this ROA request.
    """
    if self.v4re.match(prefix):
      self.v4.add(prefix)
    elif self.v6re.match(prefix):
      self.v6.add(prefix)
    else:
      raise RuntimeError, "Bad prefix syntax: %r" % (prefix,)

  def xml(self, e):
    """
    Generate XML element represeting representing this ROA request.
    """
    e = SubElement(e, "roa_request",
                   asn = self.asn,
                   v4 = str(self.v4),
                   v6 = str(self.v6))
    e.tail = "\n"

class roa_requests(dict):
  """
  Database of ROA requests.
  """

  def add(self, asn, group, prefix):
    """
    Add one <ASN, group, prefix> set to ROA request database.
    """
    key = (asn, group)
    if key not in self:
      self[key] = roa_request(asn, group)
    self[key].add(prefix)

  def xml(self, e):
    """
    Render ROA requests as XML elements.
    """
    for r in self.itervalues():
      r.xml(e)

  @classmethod
  def from_csv(cls, roa_csv_file):
    """
    Parse ROA requests from CSV file.
    """
    self = cls()
    # format:  p/n-m asn group
    for pnm, asn, group in csv_open(roa_csv_file):
      self.add(asn = asn, group = group, prefix = pnm)
    return self

class child(object):
  """
  Representation of one child entity.
  """

  v4re = re.compile("^(([0-9]{1,3}\.){3}[0-9]{1,3}/[0-9]+)|(([0-9]{1,3}\.){3}[0-9]{1,3}-([0-9]{1,3}\.){3}[0-9]{1,3})$", re.I)
  v6re = re.compile("^(([0-9a-f]{0,4}:){0,15}[0-9a-f]{0,4}/[0-9]+)|(([0-9a-f]{0,4}:){0,15}[0-9a-f]{0,4}-([0-9a-f]{0,4}:){0,15}[0-9a-f]{0,4})$", re.I)

  def __init__(self, handle):
    self.handle = handle
    self.asns = comma_set()
    self.v4 = comma_set()
    self.v6 = comma_set()
    self.validity = None
    self.bpki_certificate = None

  def __repr__(self):
    s = "<%s %s" % (self.__class__.__name__, self.handle)
    if self.asns:
      s += " asn %s" % self.asns
    if self.v4:
      s += " v4 %s" % self.v4
    if self.v6:
      s += " v6 %s" % self.v6
    if self.validity:
      s += " valid %s" % self.validity
    if self.bpki_certificate:
      s += " cert %s" % self.bpki_certificate
    return s + ">"

  def add(self, prefix = None, asn = None, validity = None, bpki_certificate = None):
    """
    Add prefix, autonomous system number, validity date, or BPKI
    certificate for this child.
    """
    if prefix is not None:
      if self.v4re.match(prefix):
        self.v4.add(prefix)
      elif self.v6re.match(prefix):
        self.v6.add(prefix)
      else:
        raise RuntimeError, "Bad prefix syntax: %r" % (prefix,)
    if asn is not None:
      self.asns.add(asn)
    if validity is not None:
      self.validity = validity
    if bpki_certificate is not None:
      self.bpki_certificate = bpki_certificate

  def xml(self, e):
    """
    Render this child as an XML element.
    """
    complete = self.bpki_certificate and self.validity
    if whine and not complete:
      print "Incomplete child entry %s" % self
    if complete or allow_incomplete:
      e = SubElement(e, "child",
                     handle = self.handle,
                     valid_until = self.validity,
                     asns = str(self.asns),
                     v4 = str(self.v4),
                     v6 = str(self.v6))
      e.tail = "\n"
      if self.bpki_certificate:
        PEMElement(e, "bpki_certificate", self.bpki_certificate)

class children(dict):
  """
  Database of children.
  """

  def add(self, handle, prefix = None, asn = None, validity = None, bpki_certificate = None):
    """
    Add resources to a child, creating the child object if necessary.
    """
    if handle not in self:
      self[handle] = child(handle)
    self[handle].add(prefix = prefix, asn = asn, validity = validity, bpki_certificate = bpki_certificate)

  def xml(self, e):
    """
    Render children database to XML.
    """
    for c in self.itervalues():
      c.xml(e)

  @classmethod
  def from_csv(cls, prefix_csv_file, asn_csv_file, fxcert, entitydb):
    """
    Parse child resources, certificates, and validity dates from CSV files.
    """
    self = cls()
    for f in entitydb.iterate("children", "*.xml"):
      c = etree_read(f)
      self.add(handle = os.path.splitext(os.path.split(f)[-1])[0],
               validity = c.get("valid_until"),
               bpki_certificate = fxcert(c.findtext("bpki_child_ta")))
    # childname p/n
    for handle, pn in csv_open(prefix_csv_file):
      self.add(handle = handle, prefix = pn)
    # childname asn
    for handle, asn in csv_open(asn_csv_file):
      self.add(handle = handle, asn = asn)
    return self

class parent(object):
  """
  Representation of one parent entity.
  """

  def __init__(self, handle):
    self.handle = handle
    self.service_uri = None
    self.bpki_cms_certificate = None
    self.bpki_https_certificate = None
    self.myhandle = None
    self.sia_base = None

  def __repr__(self):
    s = "<%s %s" % (self.__class__.__name__, self.handle)
    if self.myhandle:
      s += " myhandle %s" % self.myhandle
    if self.service_uri:
      s += " uri %s" % self.service_uri
    if self.sia_base:
      s += " sia %s" % self.sia_base
    if self.bpki_cms_certificate:
      s += " cms %s" % self.bpki_cms_certificate
    if self.bpki_https_certificate:
      s += " https %s" % self.bpki_https_certificate
    return s + ">"

  def add(self, service_uri = None,
          bpki_cms_certificate = None,
          bpki_https_certificate = None,
          myhandle = None,
          sia_base = None):
    """
    Add service URI or BPKI certificates to this parent object.
    """
    if service_uri is not None:
      self.service_uri = service_uri
    if bpki_cms_certificate is not None:
      self.bpki_cms_certificate = bpki_cms_certificate
    if bpki_https_certificate is not None:
      self.bpki_https_certificate = bpki_https_certificate
    if myhandle is not None:
      self.myhandle = myhandle
    if sia_base is not None:
      self.sia_base = sia_base

  def xml(self, e):
    """
    Render this parent object to XML.
    """
    complete = self.bpki_cms_certificate and self.bpki_https_certificate and self.myhandle and self.service_uri and self.sia_base
    if whine and not complete:
      print "Incomplete parent entry %s" % self
    if complete or allow_incomplete:
      e = SubElement(e, "parent",
                     handle = self.handle,
                     myhandle = self.myhandle,
                     service_uri = self.service_uri,
                     sia_base = self.sia_base)
      e.tail = "\n"
      if self.bpki_cms_certificate:
        PEMElement(e, "bpki_cms_certificate", self.bpki_cms_certificate)
      if self.bpki_https_certificate:
        PEMElement(e, "bpki_https_certificate", self.bpki_https_certificate)

class parents(dict):
  """
  Database of parent objects.
  """

  def add(self, handle,
          service_uri = None,
          bpki_cms_certificate = None,
          bpki_https_certificate = None,
          myhandle = None,
          sia_base = None):
    """
    Add service URI or certificates to parent object, creating it if necessary.
    """
    if handle not in self:
      self[handle] = parent(handle)
    self[handle].add(service_uri = service_uri,
                     bpki_cms_certificate = bpki_cms_certificate,
                     bpki_https_certificate = bpki_https_certificate,
                     myhandle = myhandle,
                     sia_base = sia_base)

  def xml(self, e):
    for c in self.itervalues():
      c.xml(e)

  @classmethod
  def from_csv(cls, fxcert, entitydb):
    """
    Parse parent data from entitydb.
    """
    self = cls()
    for f in entitydb.iterate("parents", "*.xml"):
      h = os.path.splitext(os.path.split(f)[-1])[0]
      p = etree_read(f)
      r = etree_read(f.replace(os.path.sep + "parents"      + os.path.sep,
                               os.path.sep + "repositories" + os.path.sep))
      assert r.get("type") == "confirmed"
      self.add(handle = h,
               service_uri = p.get("service_uri"),
               bpki_cms_certificate = fxcert(p.findtext("bpki_resource_ta")),
               bpki_https_certificate = fxcert(p.findtext("bpki_server_ta")),
               myhandle = p.get("child_handle"),
               sia_base = r.get("sia_base"))
    return self

class repository(object):
  """
  Representation of one repository entity.
  """

  def __init__(self, handle):
    self.handle = handle
    self.service_uri = None
    self.bpki_certificate = None

  def __repr__(self):
    s = "<%s %s" % (self.__class__.__name__, self.handle)
    if self.service_uri:
      s += " uri %s" % self.service_uri
    if self.bpki_certificate:
      s += " cert %s" % self.bpki_certificate
    return s + ">"

  def add(self, service_uri = None, bpki_certificate = None):
    """
    Add service URI or BPKI certificates to this repository object.
    """
    if service_uri is not None:
      self.service_uri = service_uri
    if bpki_certificate is not None:
      self.bpki_certificate = bpki_certificate

  def xml(self, e):
    """
    Render this repository object to XML.
    """
    complete = self.bpki_certificate and self.service_uri
    if whine and not complete:
      print "Incomplete repository entry %s" % self
    if complete or allow_incomplete:
      e = SubElement(e, "repository",
                     handle = self.handle,
                     service_uri = self.service_uri)
      e.tail = "\n"
      if self.bpki_certificate:
        PEMElement(e, "bpki_certificate", self.bpki_certificate)

class repositories(dict):
  """
  Database of repository objects.
  """

  def add(self, handle,
          service_uri = None,
          bpki_certificate = None):
    """
    Add service URI or certificate to repository object, creating it if necessary.
    """
    if handle not in self:
      self[handle] = repository(handle)
    self[handle].add(service_uri = service_uri,
                     bpki_certificate = bpki_certificate)

  def xml(self, e):
    for c in self.itervalues():
      c.xml(e)

  @classmethod
  def from_csv(cls, fxcert, entitydb):
    """
    Parse repository data from entitydb.
    """
    self = cls()
    for f in entitydb.iterate("repositories", "*.xml"):
      h = os.path.splitext(os.path.split(f)[-1])[0]
      r = etree_read(f)
      assert r.get("type") == "confirmed"
      self.add(handle = h,
               service_uri = r.get("service_uri"),
               bpki_certificate = fxcert(r.findtext("bpki_server_ta")))
    return self

def csv_open(filename):
  """
  Open a CSV file, with settings that make it a tab-delimited file.
  You may need to tweak this function for your environment, see the
  csv module in the Python standard libraries for details.
  """
  return csv.reader(open(filename, "rb"), dialect = csv_dialect)

def PEMElement(e, tag, filename, **kwargs):
  """
  Create an XML element containing Base64 encoded data taken from a
  PEM file.
  """
  lines = open(filename).readlines()
  while lines:
    if lines.pop(0).startswith("-----BEGIN "):
      break
  while lines:
    if lines.pop(-1).startswith("-----END "):
      break
  if e.text is None:
    e.text = "\n"
  se = SubElement(e, tag, **kwargs)
  se.text = "\n" + "".join(lines)
  se.tail = "\n"
  return se

class CA(object):
  """
  Representation of one certification authority.
  """

  # Mapping of path restriction values we use to OpenSSL config file
  # section names.

  path_restriction = { 0 : "ca_x509_ext_xcert0",
                       1 : "ca_x509_ext_xcert1" }

  def __init__(self, cfg_file, dir):
    self.cfg    = cfg_file
    self.dir    = dir
    self.cer    = dir + "/ca.cer"
    self.key    = dir + "/ca.key"
    self.req    = dir + "/ca.req"
    self.crl    = dir + "/ca.crl"
    self.index  = dir + "/index"
    self.serial = dir + "/serial"
    self.crlnum = dir + "/crl_number"

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

    self.env = { "PATH" : os.environ["PATH"],
                 "BPKI_DIRECTORY" : dir,
                 "RANDFILE" : ".OpenSSL.whines.unless.I.set.this",
                 "OPENSSL_CONF" : cfg_file }

  def run_openssl(self, *cmd, **kwargs):
    """
    Run an OpenSSL command, suppresses stderr unless OpenSSL returns
    failure, and returns stdout.
    """
    stdin = kwargs.pop("stdin", None)
    env = self.env.copy()
    env.update(kwargs)
    cmd = (self.openssl,) + cmd
    p = subprocess.Popen(cmd, env = env, stdin = subprocess.PIPE, stdout = subprocess.PIPE, stderr = subprocess.PIPE)
    stdout, stderr = p.communicate(stdin)
    if p.wait() != 0:
      sys.stderr.write("OpenSSL command failed: " + stderr + "\n")
      raise subprocess.CalledProcessError(returncode = p.returncode, cmd = cmd)
    return stdout

  def run_ca(self, *args):
    """
    Run OpenSSL "ca" command with common initial arguments.
    """
    self.run_openssl("ca", "-batch", "-config",  self.cfg, *args)

  def run_req(self, key_file, req_file):
    """
    Run OpenSSL "genrsa" and  "req" commands.
    """
    if not os.path.exists(key_file):
      self.run_openssl("genrsa", "-out", key_file, "2048")
    if not os.path.exists(req_file):
      self.run_openssl("req", "-new", "-sha256", "-config", self.cfg, "-key", key_file, "-out", req_file)

  def run_dgst(self, input, algorithm = "md5"):
    """
    Run OpenSSL "dgst" command, return cleaned-up result.
    """
    hash = self.run_openssl("dgst", "-" + algorithm, stdin = input)
    #
    # Twits just couldn't leave well enough alone, grr.
    hash = "".join(hash.split())
    if hash.startswith("(stdin)="):
      hash =  hash[len("(stdin)="):]
    return hash

  @staticmethod
  def touch_file(filename, content = None):
    """
    Create dumb little text files expected by OpenSSL "ca" utility.
    """
    if not os.path.exists(filename):
      f = open(filename, "w")
      if content is not None:
        f.write(content)
      f.close()

  def setup(self, ca_name):
    """
    Set up this CA.  ca_name is an X.509 distinguished name in
    /tag=val/tag=val format.
    """

    modified = False

    if not os.path.exists(self.dir):
      os.makedirs(self.dir)
      self.touch_file(self.index)
      self.touch_file(self.serial, "01\n")
      self.touch_file(self.crlnum, "01\n")

    self.run_req(key_file = self.key, req_file = self.req)

    if not os.path.exists(self.cer):
      modified = True
      self.run_ca("-selfsign", "-extensions", "ca_x509_ext_ca", "-subj", ca_name, "-in", self.req, "-out", self.cer)

    if not os.path.exists(self.crl):
      modified = True
      self.run_ca("-gencrl", "-out", self.crl)

    return modified

  def ee(self, ee_name, base_name):
    """
    Issue an end-enity certificate.
    """
    key_file = "%s/%s.key" % (self.dir, base_name)
    req_file = "%s/%s.req" % (self.dir, base_name)
    cer_file = "%s/%s.cer" % (self.dir, base_name)
    self.run_req(key_file = key_file, req_file = req_file)
    if not os.path.exists(cer_file):
      self.run_ca("-extensions", "ca_x509_ext_ee", "-subj", ee_name, "-in", req_file, "-out", cer_file)
      return True
    else:
      return False

  def cms_xml_sign(self, ee_name, base_name, elt):
    """
    Sign an XML object with CMS, return Base64 text.
    """
    self.ee(ee_name, base_name)
    return base64.b64encode(self.run_openssl(
      "cms", "-sign", "-binary", "-outform", "DER",
      "-keyid", "-md", "sha256", "-nodetach", "-nosmimecap",
      "-econtent_type", ".".join(str(i) for i in rpki.oids.name2oid["id-ct-xml"]),
      "-inkey",  "%s/%s.key" % (self.dir, base_name),
      "-signer", "%s/%s.cer" % (self.dir, base_name),
      stdin = ElementToString(etree_pre_write(elt))))

  def cms_xml_verify(self, b64, ca):
    """
    Attempt to verify and extract XML from a Base64-encoded signed CMS
    object.  CA is the filename of a certificate that we expect to be
    the issuer of the EE certificate bundled with the CMS, and must
    previously have been cross-certified under our trust anchor.
    """
    # In theory, we should be able to use the -certfile parameter to
    # pass in the CA certificate, but in practice, I have never gotten
    # this to work, either with the command line tool or in the
    # OpenSSL C API.  Dunno why.  Passing both TA and CA via -CAfile
    # does work, so we do that, using a temporary file, sigh.
    CAfile = os.path.join(self.dir, "temp.%s.pem" % os.getpid())
    try:
      f = open(CAfile, "w")
      f.write(open(self.cer).read())
      f.write(open(ca).read())
      f.close()
      return etree_post_read(ElementFromString(self.run_openssl(
        "cms", "-verify", "-inform", "DER", "-CAfile", CAfile,
        stdin = base64.b64decode(b64))))
    finally:
      if os.path.exists(CAfile):
        os.unlink(CAfile)

  def bsc(self, pkcs10):
    """
    Issue BSC certificiate, if we have a PKCS #10 request for it.
    """

    if pkcs10 is None:
      return None, None

    pkcs10 = base64.b64decode(pkcs10)

    hash = self.run_dgst(pkcs10)

    req_file = "%s/bsc.%s.req" % (self.dir, hash)
    cer_file = "%s/bsc.%s.cer" % (self.dir, hash)

    if not os.path.exists(cer_file):
      self.run_openssl("req", "-inform", "DER", "-out", req_file, stdin = pkcs10)
      self.run_ca("-extensions", "ca_x509_ext_ee", "-in", req_file, "-out", cer_file)

    return req_file, cer_file

  def fxcert(self, b64, filename = None, path_restriction = 0):
    """
    Write PEM certificate to file, then cross-certify.
    """
    fn = os.path.join(self.dir, filename or "temp.%s.cer" % os.getpid())
    try:
      self.run_openssl("x509", "-inform", "DER", "-out", fn,
                       stdin = base64.b64decode(b64))
      return self.xcert(fn, path_restriction)
    finally:
      if not filename and os.path.exists(fn):
        os.unlink(fn)
        pass

  def xcert(self, cert, path_restriction = 0):
    """
    Cross-certify a certificate represented as a PEM file.
    """

    if not cert or not os.path.exists(cert):
      return None

    # Extract public key and subject name from PEM file and hash it so
    # we can use the result as a tag for cross-certifying this cert.

    hash = self.run_dgst(self.run_openssl(
      "x509", "-noout", "-pubkey", "-subject", "-in", cert))

    # Cross-certify the cert we were given, if we haven't already.
    # This only works for self-signed certs, due to limitations of the
    # OpenSSL command line tool, but that suffices for our purposes.

    xcert = "%s/xcert.%s.cer" % (self.dir, hash.strip())
    if not os.path.exists(xcert):
      self.run_ca("-ss_cert", cert, "-out", xcert, "-extensions", self.path_restriction[path_restriction])
    return xcert

def etree_validate(e):
  # This is a kludge, schema should be loaded as module or configured
  # in .conf, but it will do as a temporary debugging hack.
  schema = os.getenv("MYRPKI_RNG")
  if schema:
    try:
      import lxml.etree
    except ImportError:
      return
    try:
      lxml.etree.RelaxNG(file = schema).assertValid(e)
    except lxml.etree.RelaxNGParseError:
      return
    except lxml.etree.DocumentInvalid:
      print lxml.etree.tostring(e, pretty_print = True)
      raise

def etree_write(e, filename, verbose = True, validate = True):
  """
  Write out an etree to a file, safely.

  I still miss SYSCAL(RENMWO).
  """
  if verbose:
    print "Writing", filename
  e = etree_pre_write(e, validate)
  ElementTree(e).write(filename + ".tmp")
  os.rename(filename + ".tmp", filename)

def etree_pre_write(e, validate = True):
  """
  Do the namespace frobbing needed on write; broken out of
  etree_write() because also needed with ElementToString().
  """
  e = copy.deepcopy(e)
  e.set("version", version)
  for i in e.getiterator():
    if i.tag[0] != "{":
      i.tag = namespaceQName + i.tag
    assert i.tag.startswith(namespaceQName)
  if validate:
    etree_validate(e)
  return e

def etree_read(filename, verbose = True, validate = True):
  """
  Read an etree from a file, verifying then stripping XML namespace
  cruft.
  """
  if verbose:
    print "Reading", filename
  e = ElementTree(file = filename).getroot()
  return etree_post_read(e, validate)

def etree_post_read(e, validate = True):
  """
  Do the namespace frobbing needed on read; broken out of etree_read()
  beause also needed by ElementFromString().
  """
  if validate:
    etree_validate(e)
  for i in e.getiterator():
    if i.tag.startswith(namespaceQName):
      i.tag = i.tag[len(namespaceQName):]
    else:
      raise RuntimeError, "XML tag %r is not in namespace %r" % (i.tag, namespace)
  return e

def b64_equal(thing1, thing2):
  """
  Compare two Base64-encoded values for equality.
  """
  return "".join(thing1.split()) == "".join(thing2.split())



class main(rpki.cli.Cmd):

  prompt = "myrpki> "

  completedefault = rpki.cli.Cmd.filename_complete

  show_xml = False

  def __init__(self):
    os.environ["TZ"] = "UTC"
    time.tzset()

    self.cfg_file = os.getenv("MYRPKI_CONF", "myrpki.conf")

    opts, argv = getopt.getopt(sys.argv[1:], "c:h?", ["config=", "help"])
    for o, a in opts:
      if o in ("-c", "--config"):
        self.cfg_file = a
      elif o in ("-h", "--help", "-?"):
        argv = ["help"]

    if not argv or argv[0] != "help":
      rpki.log.init("myrpki")
      self.read_config()

    rpki.cli.Cmd.__init__(self, argv)


  def help_overview(self):
    """
    Show program __doc__ string.  Perhaps there's some clever way to
    do this using the textwrap module, but for now something simple
    and crude will suffice.
    """
    for line in __doc__.splitlines(True):
      self.stdout.write(" " * 4 + line)
    self.stdout.write("\n")

  def read_config(self):

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

    self.histfile  = self.cfg.get("history_file", ".setup_history")
    self.handle    = self.cfg.get("handle")
    self.run_rpkid = self.cfg.getboolean("run_rpkid")
    self.run_pubd  = self.cfg.getboolean("run_pubd")
    self.run_rootd = self.cfg.getboolean("run_rootd")
    self.entitydb  = EntityDB(self.cfg)

    if self.run_rootd and (not self.run_pubd or not self.run_rpkid):
      raise RuntimeError, "Can't run rootd unless also running rpkid and pubd"

    self.bpki_resources = CA(self.cfg_file, self.cfg.get("bpki_resources_directory"))
    if self.run_rpkid or self.run_pubd or self.run_rootd:
      self.bpki_servers = CA(self.cfg_file, self.cfg.get("bpki_servers_directory"))

    self.pubd_contact_info = self.cfg.get("pubd_contact_info", "")

    self.rsync_module = self.cfg.get("publication_rsync_module")
    self.rsync_server = self.cfg.get("publication_rsync_server")


  def do_initialize(self, arg):
    """
    Initialize an RPKI installation.  This command reads the
    configuration file, creates the BPKI and EntityDB directories,
    generates the initial BPKI certificates, and creates an XML file
    describing the resource-holding aspect of this RPKI installation.
    """

    if arg:
      raise RuntimeError, "This command takes no arguments"

    print "Generating RSA keys, this may take a little while..."

    self.bpki_resources.setup(self.cfg.get("bpki_resources_ta_dn",
                                           "/CN=%s BPKI Resource Trust Anchor" % self.handle))
    if self.run_rpkid or self.run_pubd or self.run_rootd:
      self.bpki_servers.setup(self.cfg.get("bpki_servers_ta_dn",
                                           "/CN=%s BPKI Server Trust Anchor" % self.handle))

    # Create entitydb directories.

    for i in ("parents", "children", "repositories", "pubclients"):
      d = self.entitydb(i)
      if not os.path.exists(d):
        os.makedirs(d)

    if self.run_rpkid or self.run_pubd or self.run_rootd:

      if self.run_rpkid:
        self.bpki_servers.ee(self.cfg.get("bpki_rpkid_ee_dn",
                                          "/CN=%s rpkid server certificate" % self.handle), "rpkid")
        self.bpki_servers.ee(self.cfg.get("bpki_irdbd_ee_dn",
                                          "/CN=%s irdbd server certificate" % self.handle), "irdbd")
      if self.run_pubd:
        self.bpki_servers.ee(self.cfg.get("bpki_pubd_ee_dn",
                                          "/CN=%s pubd server certificate" % self.handle), "pubd")
      if self.run_rpkid or self.run_pubd:
        self.bpki_servers.ee(self.cfg.get("bpki_irbe_ee_dn",
                                          "/CN=%s irbe client certificate" % self.handle), "irbe")
      if self.run_rootd:
        self.bpki_servers.ee(self.cfg.get("bpki_rootd_ee_dn",
                                          "/CN=%s rootd server certificate" % self.handle), "rootd")

    # Build the identity.xml file.  Need to check for existing file so we don't
    # overwrite?  Worry about that later.

    e = Element("identity", handle = self.handle)
    PEMElement(e, "bpki_ta", self.bpki_resources.cer)
    etree_write(e, self.entitydb("identity.xml"))

    # If we're running rootd, construct a fake parent to go with it,
    # and cross-certify in both directions so we can talk to rootd.

    if self.run_rootd:

      e = Element("parent", parent_handle = self.handle, child_handle = self.handle,
                  service_uri = "https://localhost:%s/" % self.cfg.get("rootd_server_port"),
                  valid_until = str(rpki.sundial.now() + rpki.sundial.timedelta(days = 365)))
      PEMElement(e, "bpki_resource_ta", self.bpki_servers.cer)
      PEMElement(e, "bpki_server_ta", self.bpki_servers.cer)
      PEMElement(e, "bpki_child_ta", self.bpki_resources.cer)
      SubElement(e, "repository", type = "offer")
      etree_write(e, self.entitydb("parents", "%s.xml" % self.handle))

      self.bpki_resources.xcert(self.bpki_servers.cer)

      rootd_child_fn = self.cfg.get("child-bpki-cert", None, "rootd")
      if not os.path.exists(rootd_child_fn):
        os.link(self.bpki_servers.xcert(self.bpki_resources.cer), rootd_child_fn)

      e = Element("repository", type = "offer", handle = self.handle, parent_handle = self.handle)
      PEMElement(e, "bpki_client_ta", self.bpki_resources.cer)
      etree_write(e, self.entitydb("repositories", "%s.xml" % self.handle))


  def do_configure_child(self, arg):
    """
    Configure a new child of this RPKI entity, given the child's XML
    identity file as an input.  This command extracts the child's data
    from the XML, cross-certifies the child's resource-holding BPKI
    certificate, and generates an XML file describing the relationship
    between the child and this parent, including this parent's BPKI
    data and up-down protocol service URI.
    """

    child_handle = None

    opts, argv = getopt.getopt(arg.split(), "", ["child_handle="])
    for o, a in opts:
      if o == "--child_handle":
        child_handle = a
    
    if len(argv) != 1:
      raise RuntimeError, "Need to specify filename for child.xml"

    c = etree_read(argv[0])

    if child_handle is None:
      child_handle = c.get("handle")

    try:
      e = etree_read(self.cfg.get("xml_filename"))
      service_uri_base = e.get("service_uri")
    except IOError:
      service_uri_base = None      

    if not service_uri_base and self.run_rpkid:
      service_uri_base = "https://%s:%s/up-down/%s" % (self.cfg.get("rpkid_server_host"),
                                                       self.cfg.get("rpkid_server_port"),
                                                       self.handle)
    if not service_uri_base:
      print "Sorry, you can't set up children of a hosted config that itself has not yet been set up"
      return

    print "Child calls itself %r, we call it %r" % (c.get("handle"), child_handle)

    self.bpki_servers.fxcert(c.findtext("bpki_ta"))

    e = Element("parent", parent_handle = self.handle, child_handle = child_handle,
                service_uri = "%s/%s" % (service_uri_base, child_handle),
                valid_until = str(rpki.sundial.now() + rpki.sundial.timedelta(days = 365)))

    PEMElement(e, "bpki_resource_ta", self.bpki_resources.cer)
    PEMElement(e, "bpki_server_ta",   self.bpki_servers.cer)
    SubElement(e, "bpki_child_ta").text = c.findtext("bpki_ta")

    try:
      repo = None
      for f in self.entitydb.iterate("repositories", "*.xml"):
        r = etree_read(f)
        if r.get("type") == "confirmed":
          if repo is not None:
            raise RuntimeError, "Too many repositories, I don't know what to do, not giving referral"
          repo_handle = os.path.splitext(os.path.split(f)[-1])[0]
          repo = r
      if repo is None:
        raise RuntimeError, "Couldn't find any usable repositories, not giving referral"

      if repo_handle == self.handle:
        SubElement(e, "repository", type = "offer")
      else:
        proposed_sia_base = repo.get("sia_base") + child_handle + "/"
        r = Element("referral", authorized_sia_base = proposed_sia_base)
        r.text = c.findtext("bpki_ta")
        auth = self.bpki_resources.cms_xml_sign(
          "/CN=%s Publication Referral" % self.handle, "referral", r)
        r = SubElement(e, "repository", type = "referral")
        SubElement(r, "authorization", referrer = repo.get("client_handle")).text = auth
        SubElement(r, "contact_info").text = repo.findtext("contact_info")

    except RuntimeError, err:
      print err

    etree_write(e, self.entitydb("children", "%s.xml" % child_handle))


  def do_configure_parent(self, arg):
    """
    Configure a new parent of this RPKI entity, given the output of
    the parent's configure_child command as input.  This command reads
    the parent's response XML, extracts the parent's BPKI and service
    URI information, cross-certifies the parent's BPKI data into this
    entity's BPKI, and checks for offers or referrals of publication
    service.  If a publication offer or referral is present, we
    generate a request-for-service message to that repository, in case
    the user wants to avail herself of the referral or offer.
    """

    parent_handle = None

    opts, argv = getopt.getopt(arg.split(), "", ["parent_handle="])
    for o, a in opts:
      if o == "--parent_handle":
        parent_handle = a

    if len(argv) != 1:
      raise RuntimeError, "Need to specify filename for parent.xml on command line"

    p = etree_read(argv[0])

    if parent_handle is None:
      parent_handle = p.get("parent_handle")

    print "Parent calls itself %r, we call it %r" % (p.get("parent_handle"), parent_handle)
    print "Parent calls us %r" % p.get("child_handle")

    self.bpki_resources.fxcert(p.findtext("bpki_resource_ta"))
    self.bpki_resources.fxcert(p.findtext("bpki_server_ta"))

    etree_write(p, self.entitydb("parents", "%s.xml" % parent_handle))

    r = p.find("repository")

    if r is not None and r.get("type") in ("offer", "referral"):
      r.set("handle", self.handle)
      r.set("parent_handle", parent_handle)
      PEMElement(r, "bpki_client_ta", self.bpki_resources.cer)
      etree_write(r, self.entitydb("repositories", "%s.xml" % parent_handle))

    else:
      print "Couldn't find repository offer or referral"


  def do_configure_publication_client(self, arg):
    """
    Configure publication server to know about a new client, given the
    client's request-for-service message as input.  This command reads
    the client's request for service, cross-certifies the client's
    BPKI data, and generates a response message containing the
    repository's BPKI data and service URI.
    """

    sia_base = None

    opts, argv = getopt.getopt(arg.split(), "", ["sia_base="])
    for o, a in opts:
      if o == "--sia_base":
        sia_base = a
    
    if len(argv) != 1:
      raise RuntimeError, "Need to specify filename for client.xml"

    client = etree_read(argv[0])

    if sia_base is None:

      auth = client.find("authorization")
      if auth is not None:
        print "Found <authorization/> element, this looks like a referral"
        referrer = etree_read(self.entitydb("pubclients", "%s.xml" % auth.get("referrer").replace("/",".")))
        referrer = self.bpki_servers.fxcert(referrer.findtext("bpki_client_ta"))
        referral = self.bpki_servers.cms_xml_verify(auth.text, referrer)
        if not b64_equal(referral.text, client.findtext("bpki_client_ta")):
          raise RuntimeError, "Referral trust anchor does not match"
        sia_base = referral.get("authorized_sia_base")

      elif client.get("parent_handle") == self.handle:
        print "Client claims to be our child, checking"
        client_ta = client.findtext("bpki_client_ta")
        assert client_ta
        for child in self.entitydb.iterate("children", "*.xml"):
          c = etree_read(child)
          if b64_equal(c.findtext("bpki_child_ta"), client_ta):
            sia_base = "rsync://%s/%s/%s/%s/" % (self.rsync_server, self.rsync_module,
                                                 self.handle, client.get("handle"))
            break

    # If we still haven't figured out what to do with this client, it
    # gets a top-level tree of its own, no attempt at nesting.

    if sia_base is None:
      print "Don't know where to nest this client, defaulting to top-level"
      sia_base = "rsync://%s/%s/%s/" % (self.rsync_server, self.rsync_module, client.get("handle"))
      
    assert sia_base.startswith("rsync://")

    client_handle = "/".join(sia_base.rstrip("/").split("/")[4:])

    parent_handle = client.get("parent_handle")

    print "Client calls itself %r, we call it %r" % (client.get("handle"), client_handle)
    print "Client says its parent handle is %r" % parent_handle

    self.bpki_servers.fxcert(client.findtext("bpki_client_ta"))

    e = Element("repository", type = "confirmed",
                client_handle = client_handle,
                parent_handle = parent_handle,
                sia_base = sia_base,
                service_uri = "https://%s:%s/client/%s" % (self.cfg.get("pubd_server_host"),
                                                           self.cfg.get("pubd_server_port"),
                                                           client_handle))

    PEMElement(e, "bpki_server_ta", self.bpki_servers.cer)
    SubElement(e, "bpki_client_ta").text = client.findtext("bpki_client_ta")
    SubElement(e, "contact_info").text = self.pubd_contact_info
    etree_write(e, self.entitydb("pubclients", "%s.xml" % client_handle.replace("/", ".")))


  def do_configure_repository(self, arg):
    """
    Configure a publication repository for this RPKI entity, given the
    repository's response to our request-for-service message as input.
    This command reads the repository's response, extracts and
    cross-certifies the BPKI data and service URI, and links the
    repository data with the corresponding parent data in our local
    database.
    """

    argv = arg.split()

    if len(argv) != 1:
      raise RuntimeError, "Need to specify filename for repository.xml on command line"

    r = etree_read(argv[0])

    parent_handle = r.get("parent_handle")

    print "Repository calls us %r" % (r.get("client_handle"))
    print "Repository response associated with parent_handle %r" % parent_handle

    etree_write(r, self.entitydb("repositories", "%s.xml" % parent_handle))




  def myrpki_main(self):
    """
    Main program of old myrpki.py script.  This remains separate
    because it's called from more than one place.
    """

    roa_csv_file                  = self.cfg.get("roa_csv")
    prefix_csv_file               = self.cfg.get("prefix_csv")
    asn_csv_file                  = self.cfg.get("asn_csv")

    # This probably should become an argument instead of (or in
    # addition to a default from?) a config file option.
    xml_filename                  = self.cfg.get("xml_filename")

    try:
      e = etree_read(xml_filename)
      bsc_req, bsc_cer = self.bpki_resources.bsc(e.findtext("bpki_bsc_pkcs10"))
      service_uri = e.get("service_uri")
      server_ta = e.findtext("bpki_server_ta")
    except IOError:
      bsc_req, bsc_cer = None, None
      service_uri = None
      server_ta = None

    e = Element("myrpki", handle = self.handle)

    if service_uri:
      e.set("service_uri", service_uri)

    roa_requests.from_csv(roa_csv_file).xml(e)

    children.from_csv(
      prefix_csv_file = prefix_csv_file,
      asn_csv_file = asn_csv_file,
      fxcert = self.bpki_resources.fxcert,
      entitydb = self.entitydb).xml(e)

    parents.from_csv(     fxcert = self.bpki_resources.fxcert, entitydb = self.entitydb).xml(e)
    repositories.from_csv(fxcert = self.bpki_resources.fxcert, entitydb = self.entitydb).xml(e)

    PEMElement(e, "bpki_ca_certificate", self.bpki_resources.cer)
    PEMElement(e, "bpki_crl",            self.bpki_resources.crl)

    if bsc_cer:
      PEMElement(e, "bpki_bsc_certificate", bsc_cer)

    if bsc_req:
      PEMElement(e, "bpki_bsc_pkcs10", bsc_req)

    if server_ta:
      SubElement(e, "bpki_server_ta").text = server_ta

    etree_write(e, xml_filename)


  def do_configure_resources(self, arg):
    """
    Read CSV files and all the descriptions of parents and children
    that we've built up, package the result up as a single XML file to
    be shipped to a hosting rpkid.
    """

    if arg:
      raise RuntimeError, "Unexpected argument %r" % arg
    self.myrpki_main()



  def do_configure_daemons(self, arg):
    """
    Configure RPKI daemons with the data built up by the other
    commands in this program.

    The basic model here is that each entity with resources to certify
    runs the myrpki tool, but not all of them necessarily run their
    own RPKI engines.  The entities that do run RPKI engines get data
    from the entities they host via the XML files output by the
    configure_resources command.  Those XML files are the input to
    this command, which uses them to do all the work of configuring
    daemons, populating SQL databases, and so forth.  A few operations
    (eg, BSC construction) generate data which has to be shipped back
    to the resource holder, which we do by updating the same XML file.

    In essence, the XML files are a sneakernet (or email, or carrier
    pigeon) communication channel between the resource holders and the
    RPKI engine operators.

    As a convenience, for the normal case where the RPKI engine
    operator is itself a resource holder, this command in effect runs
    the configure_resources command automatically to process the RPKI
    engine operator's own resources.

    Note that, due to the back and forth nature of some of these
    operations, it may take several cycles for data structures to stablize
    and everything to reach a steady state.  This is normal.
    """

    argv = arg.split()

    try:
      import rpki.https, rpki.resource_set, rpki.relaxng, rpki.exceptions
      import rpki.left_right, rpki.x509, rpki.async, lxml.etree
      if hasattr(warnings, "catch_warnings"):
        with warnings.catch_warnings():
          warnings.simplefilter("ignore", DeprecationWarning)
          import MySQLdb
      else:
        import MySQLdb

    except ImportError, e:
      print "Sorry, you appear to be missing some of the Python modules needed to run this command"
      print "[Error: %r]" % e

    def findbase64(tree, name, b64type = rpki.x509.X509):
      x = tree.findtext(name)
      return b64type(Base64 = x) if x else None

    # We can use a single BSC for everything -- except BSC key
    # rollovers.  Drive off that bridge when we get to it.

    bsc_handle = "bsc"

    self.cfg.set_global_flags()

    # Default values for CRL parameters are low, for testing.  Not
    # quite as low as they once were, too much expired CRL whining.

    self_crl_interval = self.cfg.getint("self_crl_interval", 2 * 60 * 60)
    self_regen_margin = self.cfg.getint("self_regen_margin", 30 * 60)
    pubd_base         = "https://%s:%s/" % (self.cfg.get("pubd_server_host"), self.cfg.get("pubd_server_port"))
    rpkid_base        = "https://%s:%s/" % (self.cfg.get("rpkid_server_host"), self.cfg.get("rpkid_server_port"))

    # Wrappers to simplify calling rpkid and pubd.

    call_rpkid = rpki.async.sync_wrapper(rpki.https.caller(
      proto       = rpki.left_right,
      client_key  = rpki.x509.RSA( PEM_file = self.bpki_servers.dir + "/irbe.key"),
      client_cert = rpki.x509.X509(PEM_file = self.bpki_servers.dir + "/irbe.cer"),
      server_ta   = rpki.x509.X509(PEM_file = self.bpki_servers.cer),
      server_cert = rpki.x509.X509(PEM_file = self.bpki_servers.dir + "/rpkid.cer"),
      url         = rpkid_base + "left-right",
      debug       = self.show_xml))

    if self.run_pubd:

      call_pubd = rpki.async.sync_wrapper(rpki.https.caller(
        proto       = rpki.publication,
        client_key  = rpki.x509.RSA( PEM_file = self.bpki_servers.dir + "/irbe.key"),
        client_cert = rpki.x509.X509(PEM_file = self.bpki_servers.dir + "/irbe.cer"),
        server_ta   = rpki.x509.X509(PEM_file = self.bpki_servers.cer),
        server_cert = rpki.x509.X509(PEM_file = self.bpki_servers.dir + "/pubd.cer"),
        url         = pubd_base + "control",
        debug       = self.show_xml))

      # Make sure that pubd's BPKI CRL is up to date.

      call_pubd(rpki.publication.config_elt.make_pdu(
        action = "set",
        bpki_crl = rpki.x509.CRL(PEM_file = self.bpki_servers.crl)))

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

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

    cur = db.cursor()

    xmlfiles = []

    # If [myrpki] section includes an "xml_filename" setting, run
    # myrpki.py internally, as a convenience, and include its output at
    # the head of our list of XML files to process.

    my_xmlfile = self.cfg.get("xml_filename", "")
    if my_xmlfile:
      self.myrpki_main()
      xmlfiles.append(my_xmlfile)
    else:
      my_xmlfile = None

    # Add any other XML files specified on the command line

    xmlfiles.extend(argv)

    my_handle = None

    for xmlfile in xmlfiles:

      # Parse XML file and validate it against our scheme

      tree = etree_read(xmlfile, validate = True)

      handle = tree.get("handle")

      if xmlfile == my_xmlfile:
        my_handle = handle

      # Update IRDB with parsed resource and roa-request data.

      cur.execute(
        """
        DELETE
        FROM  roa_request_prefix
        USING roa_request, roa_request_prefix
        WHERE roa_request.roa_request_id = roa_request_prefix.roa_request_id AND roa_request.roa_request_handle = %s
        """, (handle,))

      cur.execute("DELETE FROM roa_request WHERE roa_request.roa_request_handle = %s", (handle,))

      for x in tree.getiterator("roa_request"):
        cur.execute("INSERT roa_request (roa_request_handle, asn) VALUES (%s, %s)", (handle, x.get("asn")))
        roa_request_id = cur.lastrowid
        for version, prefix_set in ((4, rpki.resource_set.roa_prefix_set_ipv4(x.get("v4"))), (6, rpki.resource_set.roa_prefix_set_ipv6(x.get("v6")))):
          if prefix_set:
            cur.executemany("INSERT roa_request_prefix (roa_request_id, prefix, prefixlen, max_prefixlen, version) VALUES (%s, %s, %s, %s, %s)",
                            ((roa_request_id, p.prefix, p.prefixlen, p.max_prefixlen, version) for p in prefix_set))

      cur.execute(
        """
        DELETE
        FROM   registrant_asn
        USING registrant, registrant_asn
        WHERE registrant.registrant_id = registrant_asn.registrant_id AND registrant.registry_handle = %s
        """ , (handle,))

      cur.execute(
        """
        DELETE FROM registrant_net USING registrant, registrant_net
        WHERE registrant.registrant_id = registrant_net.registrant_id AND registrant.registry_handle = %s
        """ , (handle,))

      cur.execute("DELETE FROM registrant WHERE registrant.registry_handle = %s" , (handle,))

      for x in tree.getiterator("child"):
        child_handle = x.get("handle")
        asns = rpki.resource_set.resource_set_as(x.get("asns"))
        ipv4 = rpki.resource_set.resource_set_ipv4(x.get("v4"))
        ipv6 = rpki.resource_set.resource_set_ipv6(x.get("v6"))

        cur.execute("INSERT registrant (registrant_handle, registry_handle, registrant_name, valid_until) VALUES (%s, %s, %s, %s)",
                    (child_handle, handle, child_handle, rpki.sundial.datetime.fromXMLtime(x.get("valid_until")).to_sql()))
        child_id = cur.lastrowid
        if asns:
          cur.executemany("INSERT registrant_asn (start_as, end_as, registrant_id) VALUES (%s, %s, %s)",
                          ((a.min, a.max, child_id) for a in asns))
        if ipv4:
          cur.executemany("INSERT registrant_net (start_ip, end_ip, version, registrant_id) VALUES (%s, %s, 4, %s)",
                          ((a.min, a.max, child_id) for a in ipv4))
        if ipv6:
          cur.executemany("INSERT registrant_net (start_ip, end_ip, version, registrant_id) VALUES (%s, %s, 6, %s)",
                          ((a.min, a.max, child_id) for a in ipv6))

      db.commit()

      # Check for certificates before attempting anything else

      hosted_cacert = findbase64(tree, "bpki_ca_certificate")
      if not hosted_cacert:
        print "Nothing else I can do without a trust anchor for the entity I'm hosting."
        continue

      rpkid_xcert = rpki.x509.X509(PEM_file = self.bpki_servers.fxcert(b64 = hosted_cacert.get_Base64(),
                                                                       filename = handle + ".cacert.cer",
                                                                       path_restriction = 1))

      # See what rpkid and pubd already have on file for this entity.

      if self.run_pubd:
        client_pdus = dict((x.client_handle, x)
                           for x in call_pubd(rpki.publication.client_elt.make_pdu(action = "list"))
                           if isinstance(x, rpki.publication.client_elt))

      rpkid_reply = call_rpkid(
        rpki.left_right.self_elt.make_pdu(      action = "get",  tag = "self",       self_handle = handle),
        rpki.left_right.bsc_elt.make_pdu(       action = "list", tag = "bsc",        self_handle = handle),
        rpki.left_right.repository_elt.make_pdu(action = "list", tag = "repository", self_handle = handle),
        rpki.left_right.parent_elt.make_pdu(    action = "list", tag = "parent",     self_handle = handle),
        rpki.left_right.child_elt.make_pdu(     action = "list", tag = "child",      self_handle = handle))

      self_pdu        = rpkid_reply[0]
      bsc_pdus        = dict((x.bsc_handle, x) for x in rpkid_reply if isinstance(x, rpki.left_right.bsc_elt))
      repository_pdus = dict((x.repository_handle, x) for x in rpkid_reply if isinstance(x, rpki.left_right.repository_elt))
      parent_pdus     = dict((x.parent_handle, x) for x in rpkid_reply if isinstance(x, rpki.left_right.parent_elt))
      child_pdus      = dict((x.child_handle, x) for x in rpkid_reply if isinstance(x, rpki.left_right.child_elt))

      pubd_query = []
      rpkid_query = []

      # There should be exactly one <self/> object per hosted entity, by definition

      if (isinstance(self_pdu, rpki.left_right.report_error_elt) or
          self_pdu.crl_interval != self_crl_interval or
          self_pdu.regen_margin != self_regen_margin or
          self_pdu.bpki_cert != rpkid_xcert):
        rpkid_query.append(rpki.left_right.self_elt.make_pdu(
          action = "create" if isinstance(self_pdu, rpki.left_right.report_error_elt) else "set",
          tag = "self",
          self_handle = handle,
          bpki_cert = rpkid_xcert,
          crl_interval = self_crl_interval,
          regen_margin = self_regen_margin))

      # In general we only need one <bsc/> per <self/>.  BSC objects are a
      # little unusual in that the PKCS #10 subelement is generated by rpkid
      # in response to generate_keypair, so there's more of a separation
      # between create and set than with other objects.

      bsc_cert = findbase64(tree, "bpki_bsc_certificate")
      bsc_crl  = findbase64(tree, "bpki_crl", rpki.x509.CRL)

      bsc_pdu = bsc_pdus.pop(bsc_handle, None)

      if bsc_pdu is None:
        rpkid_query.append(rpki.left_right.bsc_elt.make_pdu(
          action = "create",
          tag = "bsc",
          self_handle = handle,
          bsc_handle = bsc_handle,
          generate_keypair = "yes"))
      elif bsc_pdu.signing_cert != bsc_cert or bsc_pdu.signing_cert_crl != bsc_crl:
        rpkid_query.append(rpki.left_right.bsc_elt.make_pdu(
          action = "set",
          tag = "bsc",
          self_handle = handle,
          bsc_handle = bsc_handle,
          signing_cert = bsc_cert,
          signing_cert_crl = bsc_crl))

      rpkid_query.extend(rpki.left_right.bsc_elt.make_pdu(
        action = "destroy", self_handle = handle, bsc_handle = b) for b in bsc_pdus)

      bsc_req = None

      if bsc_pdu and bsc_pdu.pkcs10_request:
        bsc_req = bsc_pdu.pkcs10_request

      # At present we need one <repository/> per <parent/>, not because
      # rpkid requires that, but because pubd does.  pubd probably should
      # be fixed to support a single client allowed to update multiple
      # trees, but for the moment the easiest way forward is just to
      # enforce a 1:1 mapping between <parent/> and <repository/> objects

      for repository in tree.getiterator("repository"):

        repository_handle = repository.get("handle")
        repository_pdu = repository_pdus.pop(repository_handle, None)
        repository_uri = repository.get("service_uri")
        repository_cert = findbase64(repository, "bpki_certificate")

        if (repository_pdu is None or
            repository_pdu.bsc_handle != bsc_handle or
            repository_pdu.peer_contact_uri != repository_uri or
            repository_pdu.bpki_cert != repository_cert):
          rpkid_query.append(rpki.left_right.repository_elt.make_pdu(
            action = "create" if repository_pdu is None else "set",
            tag = repository_handle,
            self_handle = handle,
            repository_handle = repository_handle,
            bsc_handle = bsc_handle,
            peer_contact_uri = repository_uri,
            bpki_cert = repository_cert))

      rpkid_query.extend(rpki.left_right.repository_elt.make_pdu(
        action = "destroy", self_handle = handle, repository_handle = r) for r in repository_pdus)

      # <parent/> setup code currently assumes 1:1 mapping between
      # <repository/> and <parent/>, and further assumes that the handles
      # for an associated pair are the identical (that is:
      # parent.repository_handle == parent.parent_handle).

      for parent in tree.getiterator("parent"):

        parent_handle = parent.get("handle")
        parent_pdu = parent_pdus.pop(parent_handle, None)
        parent_uri = parent.get("service_uri")
        parent_myhandle = parent.get("myhandle")
        parent_sia_base = parent.get("sia_base")
        parent_cms_cert = findbase64(parent, "bpki_cms_certificate")
        parent_https_cert = findbase64(parent, "bpki_https_certificate")

        if (parent_pdu is None or
            parent_pdu.bsc_handle != bsc_handle or
            parent_pdu.repository_handle != parent_handle or
            parent_pdu.peer_contact_uri != parent_uri or
            parent_pdu.sia_base != parent_sia_base or
            parent_pdu.sender_name != parent_myhandle or
            parent_pdu.recipient_name != parent_handle or
            parent_pdu.bpki_cms_cert != parent_cms_cert or
            parent_pdu.bpki_https_cert != parent_https_cert):
          rpkid_query.append(rpki.left_right.parent_elt.make_pdu(
            action = "create" if parent_pdu is None else "set",
            tag = parent_handle,
            self_handle = handle,
            parent_handle = parent_handle,
            bsc_handle = bsc_handle,
            repository_handle = parent_handle,
            peer_contact_uri = parent_uri,
            sia_base = parent_sia_base,
            sender_name = parent_myhandle,
            recipient_name = parent_handle,
            bpki_cms_cert = parent_cms_cert,
            bpki_https_cert = parent_https_cert))

      rpkid_query.extend(rpki.left_right.parent_elt.make_pdu(
        action = "destroy", self_handle = handle, parent_handle = p) for p in parent_pdus)

      # Children are simpler than parents, because they call us, so no URL
      # to construct and figuring out what certificate to use is their
      # problem, not ours.

      for child in tree.getiterator("child"):

        child_handle = child.get("handle")
        child_pdu = child_pdus.pop(child_handle, None)
        child_cert = findbase64(child, "bpki_certificate")

        if (child_pdu is None or
            child_pdu.bsc_handle != bsc_handle or
            child_pdu.bpki_cert != child_cert):
          rpkid_query.append(rpki.left_right.child_elt.make_pdu(
            action = "create" if child_pdu is None else "set",
            tag = child_handle,
            self_handle = handle,
            child_handle = child_handle,
            bsc_handle = bsc_handle,
            bpki_cert = child_cert))

      rpkid_query.extend(rpki.left_right.child_elt.make_pdu(
        action = "destroy", self_handle = handle, child_handle = c) for c in child_pdus)

      # Publication setup.

      if self.run_pubd:

        for f in self.entitydb.iterate("pubclients", "*.xml"):
          c = etree_read(f)

          client_handle = c.get("client_handle")
          client_base_uri = c.get("sia_base")
          client_bpki_cert = rpki.x509.X509(PEM_file = self.bpki_servers.fxcert(c.findtext("bpki_client_ta")))
          client_pdu = client_pdus.pop(client_handle, None)

          if (client_pdu is None or
              client_pdu.base_uri != client_base_uri or
              client_pdu.bpki_cert != client_bpki_cert):
            pubd_query.append(rpki.publication.client_elt.make_pdu(
              action = "create" if client_pdu is None else "set",
              client_handle = client_handle,
              bpki_cert = client_bpki_cert,
              base_uri = client_base_uri))

        pubd_query.extend(rpki.publication.client_elt.make_pdu(
            action = "destroy", client_handle = p) for p in client_pdus)

      # If we changed anything, ship updates off to daemons

      failed = False

      if rpkid_query:
        rpkid_reply = call_rpkid(*rpkid_query)
        bsc_pdus = dict((x.bsc_handle, x) for x in rpkid_reply if isinstance(x, rpki.left_right.bsc_elt))
        if bsc_handle in bsc_pdus and bsc_pdus[bsc_handle].pkcs10_request:
          bsc_req = bsc_pdus[bsc_handle].pkcs10_request
        for r in rpkid_reply:
          if isinstance(r, rpki.left_right.report_error_elt):
            failed = True
            print "rpkid reported failure:", r.error_code
            if r.error_text:
              print r.error_text

      if failed:
        raise RuntimeError

      if pubd_query:
        assert self.run_pubd
        pubd_reply = call_pubd(*pubd_query)
        for r in pubd_reply:
          if isinstance(r, rpki.publication.report_error_elt):
            failed = True
            print "pubd reported failure:", r.error_code
            if r.error_text:
              print r.error_text
            
      if failed:
        raise RuntimeError

      # Rewrite XML.

      e = tree.find("bpki_bsc_pkcs10")
      if e is not None:
        tree.remove(e)
      if bsc_req is not None:
        SubElement(tree, "bpki_bsc_pkcs10").text = bsc_req.get_Base64()

      tree.set("service_uri", rpkid_base + "up-down/" + handle)

      e = tree.find("bpki_server_ta")
      if e is not None:
        tree.remove(e)
      PEMElement(tree, "bpki_server_ta", self.bpki_resources.cer)

      etree_write(tree, xmlfile, validate = True)

    db.close()



if __name__ == "__main__":
  main()