|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +from __future__ import print_function |
| 4 | + |
| 5 | +from datetime import datetime, timedelta |
| 6 | +import ldap |
| 7 | +from OpenSSL import crypto |
| 8 | +from scripts import cert, log, auth |
| 9 | +from scriptspony import vhosts |
| 10 | +import os |
| 11 | +import sys |
| 12 | +import logging |
| 13 | + |
| 14 | + |
| 15 | +def pkey_to_pem(pk): |
| 16 | + return crypto.dump_publickey(crypto.FILETYPE_PEM, pk) |
| 17 | + |
| 18 | + |
| 19 | +@log.exceptions |
| 20 | +def main(): |
| 21 | + pem = sys.stdin.read() |
| 22 | + replacement_certs = cert.pem_to_certs(pem) |
| 23 | + replacements = {pkey_to_pem(c.get_pubkey()): c for c in replacement_certs} |
| 24 | + |
| 25 | + logging.info("Replacement certificates: %s", replacements) |
| 26 | + |
| 27 | + vhosts.connect() |
| 28 | + res = vhosts.conn.search_s( |
| 29 | + "ou=VirtualHosts,dc=scripts,dc=mit,dc=edu", |
| 30 | + ldap.SCOPE_ONELEVEL, |
| 31 | + "(&(objectClass=scriptsVhost)(scriptsVhostCertificate=*))", |
| 32 | + ["scriptsVhostName", "scriptsVhostCertificate"], |
| 33 | + ) |
| 34 | + |
| 35 | + for dn, attrs in res: |
| 36 | + replace = 0 |
| 37 | + vhost, = attrs["scriptsVhostName"] |
| 38 | + logging.info("Examining %s", vhost) |
| 39 | + scripts, = attrs["scriptsVhostCertificate"] |
| 40 | + chain = cert.scripts_to_chain(scripts) |
| 41 | + for i, c in enumerate(chain): |
| 42 | + new = replacements.get(pkey_to_pem(c.get_pubkey())) |
| 43 | + if new: |
| 44 | + chain[i] = new |
| 45 | + replace += 1 |
| 46 | + if replace: |
| 47 | + logging.info( |
| 48 | + "Replacing %d certificates for %s" |
| 49 | + % (replace, vhost) |
| 50 | + ) |
| 51 | + try: |
| 52 | + vhosts.conn.modify_s( |
| 53 | + dn, |
| 54 | + [ |
| 55 | + ( |
| 56 | + ldap.MOD_REPLACE, |
| 57 | + "scriptsVhostCertificate", |
| 58 | + cert.chain_to_scripts(chain), |
| 59 | + ), |
| 60 | + ], |
| 61 | + ) |
| 62 | + except ldap.INSUFFICIENT_ACCESS as e: |
| 63 | + logging.exception(e) |
| 64 | + |
| 65 | + |
| 66 | +if __name__ == "__main__": |
| 67 | + auth.set_user_from_parent_process() |
| 68 | + from paste.deploy import loadapp |
| 69 | + |
| 70 | + loadapp("config:development.ini", relative_to=os.path.dirname(__file__)) |
| 71 | + logging.basicConfig(level=logging.DEBUG) |
| 72 | + main() |
0 commit comments