10 USAGE = '''cam [<OPTIONS>] <COMMAND> [<ARG>...]
11 CAM v%(version)s - (c)2012-2014 by <ale@incal.net>
12 Minimal X509 Certification Authority management tool.
17 Initialize the environment and create a new CA certificate
18 (you can also import an existing certificate)
21 Create (or re-create) the certificates corresponding
28 List all known certificates
31 Verify the certificates found in FILES against the CA
34 Print SHA1/MD5 fingerprints of certificates
37 Dump all the certificate-related files of this TAG
40 Should be run weekly from a cron job to warn you if some
41 certificates are about to expire (controlled by the 'warning_days'
42 parameter in the 'global' section of the configuration)
44 The configuration file consists of a ini-style file, with a 'ca'
45 section that specifies global CA parameters, and more sections for
46 each tag with certificate-specific information. See the documentation
47 for more details on how to write your own configuration.
49 Run `cam --help' to get a list of available command-line options.
51 ''' % {'version': '2.1'}
54 def find_cert(certs, name):
58 raise Exception('Certificate "%s" not found' % name)
61 def cmd_init(global_config, ca, certs, args):
65 def cmd_gen(global_config, ca, certs, args):
67 print 'Nothing to do.'
69 ca.generate(find_cert(certs, tag))
72 def cmd_gencrl(global_config, ca, certs, args):
76 def cmd_files(global_config, ca, certs, args):
78 print 'Nothing to do.'
80 c = find_cert(certs, tag)
81 print c.public_key_file
82 print c.private_key_file
85 def cmd_list(global_config, ca, certs, args):
87 for cert in sorted(certs, key=lambda x: x.name):
88 expiry = cert.get_expiration_date()
96 expiry_str = time.strftime('%Y/%m/%d', time.gmtime(expiry))
97 print cert.name, cert.cn, state, expiry_str
100 def cmd_verify(global_config, ca, certs, args):
102 print 'Nothing to do.'
105 if not ca.verify(path):
106 print '%s: FAIL' % path
109 print '%s: OK' % path
113 def cmd_fingerprint(global_config, ca, certs, args):
115 certs = [find_cert(certs, x) for x in args]
117 print cert.name, cert.cn
118 print ' SHA1:', cert.get_fingerprint('sha1')
119 print ' MD5:', cert.get_fingerprint('md5')
122 def cmd_check(global_config, ca, certs, args):
124 warning_time = 86400 * int(global_config.get('warning_days', 15))
127 exp = cert.get_expiration_date()
128 if exp and (exp - now) < warning_time:
129 print '%s (%s) is about to expire.' % (cert.name, cert.cn)
137 'gencrl': cmd_gencrl,
140 'verify': cmd_verify,
141 'fp': cmd_fingerprint,
142 'fingerprint': cmd_fingerprint,
148 parser = optparse.OptionParser(usage=USAGE)
149 parser.add_option('-d', '--debug', dest='debug', help='Be verbose',
151 parser.add_option('-c', '--config', dest='config', help='Config file')
152 opts, args = parser.parse_args()
154 if len(args) > 0 and args[0] == 'help':
158 parser.error('Must specify --config')
160 parser.error('Must specify a command')
163 format='cam: %(levelname)s: %(message)s',
164 level=logging.DEBUG if opts.debug else logging.INFO)
167 global_config, ca, certs = config.read_config(opts.config)
169 cmd, args = args[0], args[1:]
170 if cmd not in cmd_table:
171 parser.error('unknown command "%s"' % cmd)
172 cmdfn = cmd_table[cmd]
173 return cmdfn(global_config, ca, certs, args)
176 except Exception as e:
188 logging.exception('uncaught exception')
192 if __name__ == '__main__':
193 sys.exit(main_wrapper())