11 USAGE = '''cam [<OPTIONS>] <COMMAND> [<ARG>...]
12 CAM v%(version)s - (c)2012-2014 by <ale@incal.net>
13 Minimal X509 Certification Authority management tool.
18 Initialize the environment and create a new CA certificate
19 (you can also import an existing certificate)
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 a 'ca'
43 section that specifies global CA parameters, and more sections for
44 each tag with certificate-specific information. See the documentation
45 for more details on how to write your own configuration.
47 Run `cam --help' to get a list of available command-line options.
49 ''' % {'version': '2.1'}
52 def find_cert(certs, name):
56 raise Exception('Certificate "%s" not found' % name)
59 def cmd_init(global_config, ca, certs, args):
63 def cmd_gen(global_config, ca, certs, args):
65 print 'Nothing to do.'
67 ca.generate(find_cert(certs, tag))
70 def cmd_gencrl(global_config, ca, certs, args):
74 def cmd_files(global_config, ca, certs, args):
76 print 'Nothing to do.'
78 c = find_cert(certs, tag)
79 print c.public_key_file
80 print c.private_key_file
83 def cmd_list(global_config, ca, certs, args):
85 for cert in sorted(certs, key=lambda x: x.name):
86 expiry = cert.get_expiration_date()
94 expiry_str = time.strftime('%Y/%m/%d', time.gmtime(expiry))
95 print cert.name, cert.cn, state, expiry_str
98 def cmd_fingerprint(global_config, ca, certs, args):
100 certs = [find_cert(certs, x) for x in args]
102 print cert.name, cert.cn
103 print ' SHA1:', cert.get_fingerprint('sha1')
104 print ' MD5:', cert.get_fingerprint('md5')
107 def cmd_check(global_config, ca, certs, args):
109 warning_time = 86400 * int(global_config.get('warning_days', 15))
112 exp = cert.get_expiration_date()
113 if exp and (exp - now) < warning_time:
114 print '%s (%s) is about to expire.' % (cert.name, cert.cn)
122 'gencrl': cmd_gencrl,
125 'fp': cmd_fingerprint,
126 'fingerprint': cmd_fingerprint,
132 parser = optparse.OptionParser(usage=USAGE)
133 parser.add_option('-d', '--debug', dest='debug', help='Be verbose',
135 parser.add_option('-c', '--config', dest='config', help='Config file')
136 opts, args = parser.parse_args()
138 if len(args) > 0 and args[0] == 'help':
142 parser.error('Must specify --config')
144 parser.error('Must specify a command')
147 format='cam: %(levelname)s: %(message)s',
148 level=logging.DEBUG if opts.debug else logging.INFO)
151 global_config, ca, certs = config.read_config(opts.config)
153 cmd, args = args[0], args[1:]
154 if cmd not in cmd_table:
155 parser.error('unknown command "%s"' % cmd)
156 cmdfn = cmd_table[cmd]
157 return cmdfn(global_config, ca, certs, args)
160 except Exception as e:
172 logging.exception('uncaught exception')
176 if __name__ == '__main__':
177 sys.exit(main_wrapper())