11 USAGE = '''cam [<OPTIONS>] <COMMAND> [<ARG>...]
12 CAM v2.0 - (c)2012 by <ale@incal.net>
13 A Certification Authority manager for complex situations.
17 init [<RSA_CRT> [<DSA_CRT>]]
18 Initialize the environment and create a new CA certificate
19 (you can also import your own existing certificates)
22 Create (or re-create) the certificates corresponding
29 List all known certificates
32 Print SHA1/MD5 fingerprints of certificates
35 Dump all the certificate-related files of this TAG
38 Should be run weekly from a cron job to warn you if some
39 certificates are about to expire (controlled by the 'warning_days'
40 parameter in the 'global' section of the configuration)
42 The configuration file consists of a ini-style file, with one 'ca'
43 section that specifies global CA parameters, and more sections for
44 each tag with certificate-specific information. See the examples for
45 more details on how to write your own configuration.
49 def find_cert(certs, name):
53 raise Exception('Certificate "%s" not found' % name)
57 parser = optparse.OptionParser(usage=USAGE)
58 parser.add_option('-d', '--debug', dest='debug', help='Be verbose',
60 parser.add_option('-c', '--config', dest='config', help='Config file')
61 opts, args = parser.parse_args()
63 parser.error('Must specify --config')
65 parser.error('Must specify a command')
68 logging.getLogger().setLevel(opts.debug and logging.DEBUG or logging.INFO)
70 global_config, ca, certs = config.read_config(opts.config)
72 cmd, args = args[0], args[1:]
79 parser.error('Wrong number of arguments')
80 ca.generate(find_cert(certs, args[0]))
85 parser.error('Wrong number of arguments')
86 c = find_cert(certs, args[0])
87 print c.public_key_file
88 print c.private_key_file
91 for cert in sorted(certs, key=lambda x: x.name):
92 expiry = cert.get_expiration_date()
100 expiry_str = time.strftime('%Y/%m/%d', time.gmtime(expiry))
101 print cert.name, cert.cn, state, expiry_str
102 elif cmd == 'fp' or cmd == 'fingerprint':
104 certs = [find_cert(certs, x) for x in args]
106 print cert.name, cert.cn
107 print ' SHA1:', cert.get_fingerprint('sha1')
108 print ' MD5:', cert.get_fingerprint('md5')
111 warning_time = 86400 * int(global_config.get('warning_days', 15))
113 exp = cert.get_expiration_date()
114 if exp and (exp - now) < warning_time:
115 print '%s (%s) is about to expire.' % (cert.name, cert.cn)
117 parser.error('unknown command "%s"' % cmd)
127 logging.exception('uncaught exception')
131 if __name__ == '__main__':
132 sys.exit(main_wrapper())