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
90 for cert in sorted(certs, key=lambda x: x.name):
91 print cert.name, cert.cn, cert.get_expiration_date()
92 elif cmd == 'fp' or cmd == 'fingerprint':
94 certs = [find_cert(certs, x) for x in args]
96 print cert.name, cert.cn
97 print ' SHA1:', cert.get_fingerprint('sha1')
98 print ' MD5:', cert.get_fingerprint('md5')
101 warning_time = 86400 * int(global_config.get('warning_days', 15))
103 exp = cert.get_expiration_date()
104 if exp and (exp - now) < warning_time:
105 print '%s (%s) is about to expire.' % (cert.name, cert.cn)
107 parser.error('unknown command "%s"' % cmd)
117 logging.exception('uncaught exception')
121 if __name__ == '__main__':
122 sys.exit(main_wrapper())