~ubuntu-branches/ubuntu/hardy/pyca/hardy

« back to all changes in this revision

Viewing changes to bin/ca2ldif.py

  • Committer: Bazaar Package Importer
  • Author(s): Lars Bahner
  • Date: 2003-12-02 19:39:35 UTC
  • Revision ID: james.westby@ubuntu.com-20031202193935-fzzt289mntvy6a8q
Tags: upstream-20031118
ImportĀ upstreamĀ versionĀ 20031118

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python
 
2
 
 
3
########################################################################
 
4
# ca2ldif.py
 
5
# (c) by Michael Stroeder, michael@stroeder.com
 
6
########################################################################
 
7
 
 
8
__version__ = '0.6.6'
 
9
 
 
10
########################################################################
 
11
# This simple script generates a LDIF file containing all CA certs
 
12
# and CRLs.
 
13
########################################################################
 
14
 
 
15
 
 
16
import sys, string, os, getopt, types, shutil
 
17
 
 
18
def findoption(options,paramname):
 
19
  for i in options:
 
20
    if i[0]==paramname:
 
21
      return i
 
22
  return ()
 
23
 
 
24
def PrintUsage(ErrorMsg='',ErrorCode=1):
 
25
  script_name = string.split(sys.argv[0],os.sep)[-1]
 
26
  sys.stderr.write("""*** %s *** (C) by Michael Stroeder, 1999
 
27
 
 
28
usage: %s [options]
 
29
 
 
30
Options:
 
31
 
 
32
  -h or --help
 
33
        Print out this message
 
34
 
 
35
  --config=[pathname]
 
36
        Pathname of OpenSSL configuration file.
 
37
        Default: /etc/openssl/openssl.cnf
 
38
 
 
39
  --pycalib=[directory]
 
40
        Specify directory containing the pyCA modules
 
41
        Default: /usr/local/pyca/pylib
 
42
 
 
43
  --out=[pathname]
 
44
        Pathname of LDIF file for output
 
45
        Default: stdout
 
46
 
 
47
  --dntemplate=[Python dict string]
 
48
        A Python string used as template for building LDAP
 
49
        Distinguished Names E.g. cn=%%(CN)s,ou=TestCA,o=My company,c=DE
 
50
        Default: cn=%%(CN)s
 
51
 
 
52
  --crl
 
53
        Add CRLs to entries.
 
54
 
 
55
""" % (script_name,script_name))
 
56
  if ErrorMsg:
 
57
    sys.stderr.write('Error: %s\n' % ErrorMsg)
 
58
  sys.exit(ErrorCode)
 
59
 
 
60
script_name=sys.argv[0]
 
61
 
 
62
try:
 
63
  options,args=getopt.getopt(sys.argv[1:],'h',['help','config=','pycalib=','out=','dntemplate=','crl'])
 
64
except getopt.error,e:
 
65
  PrintUsage(str(e))
 
66
 
 
67
if findoption(options,'-h')!=() or findoption(options,'--help')!=():
 
68
  PrintUsage(script_name)
 
69
 
 
70
if findoption(options,'--config')!=():
 
71
  opensslcnfname = findoption(options,'--config')[1]
 
72
else:
 
73
  opensslcnfname = os.environ.get('OPENSSL_CONF','/etc/openssl/openssl.cnf')
 
74
 
 
75
if not os.path.isfile(opensslcnfname):
 
76
  PrintUsage('Config file %s not found.' % (opensslcnfname))
 
77
  sys.exit(1)
 
78
 
 
79
if findoption(options,'--pycalib')!=():
 
80
  pycalib = findoption(options,'--pycalib')[1]
 
81
else:
 
82
  pycalib = os.environ.get('PYCALIB','/usr/local/pyca/pylib')
 
83
 
 
84
if not os.path.exists(pycalib) or not os.path.isdir(pycalib):
 
85
  PrintUsage('Module directory %s does not exist or is no directory.' % (pycalib))
 
86
  sys.exit(1)
 
87
 
 
88
sys.path.append(pycalib)
 
89
 
 
90
try:
 
91
  import openssl, charset, ldif, ldapbase
 
92
except ImportError:
 
93
  PrintUsage('Required pyCA modules not found in directory %s!' % (pycalib))
 
94
 
 
95
# Read the configuration file
 
96
if os.path.isfile('%s.pickle' % (opensslcnfname)):
 
97
  # Try to read OpenSSL's config file from a pickled copy
 
98
  f=open('%s.pickle' % (opensslcnfname),'rb')
 
99
  try:
 
100
    # first try to use the faster cPickle module
 
101
    from cPickle import load
 
102
  except ImportError:
 
103
    from pickle import load
 
104
  opensslcnf=load(f)
 
105
  f.close()
 
106
else:
 
107
  # Parse OpenSSL's config file from source
 
108
  opensslcnf=openssl.cnf.OpenSSLConfigClass(opensslcnfname)
 
109
 
 
110
create_crls = findoption(options,'--crl')!=()
 
111
 
 
112
pyca_section = opensslcnf.data.get('pyca',{})
 
113
openssl.bin_filename = pyca_section.get('OpenSSLExec','/usr/local/ssl/bin/openssl')
 
114
if not os.path.isfile(openssl.bin_filename):
 
115
  sys.stderr.write('Did not find OpenSSL executable %s.\n' % (openssl.bin_filename))
 
116
  sys.exit(1)
 
117
 
 
118
if findoption(options,'--out')!=():
 
119
  ldiffile = open(findoption(options,'--out')[1],'w')
 
120
else:
 
121
  ldiffile = sys.stdout
 
122
 
 
123
if findoption(options,'--dntemplate')!=():
 
124
  dntemplate = findoption(options,'--dntemplate')[1]
 
125
else:
 
126
  dntemplate = r'cn=%(CN)s'
 
127
 
 
128
ca_names = opensslcnf.sectionkeys.get('ca',[])
 
129
ca_dn_dict = {}
 
130
 
 
131
for ca_name in ca_names:
 
132
 
 
133
  ca = opensslcnf.getcadata(ca_name)
 
134
 
 
135
  if os.path.isfile(ca.certificate):
 
136
 
 
137
    cacert = openssl.cert.X509CertificateClass(ca.certificate)
 
138
 
 
139
    ca_dn = charset.iso2utf(charset.t612iso(dntemplate % (cacert.subject)))
 
140
    if ca_dn_dict.has_key(ca_dn):
 
141
      sys.stderr.write('Warning: DN of %s conflicts with %s.\n' % (ca_name,ca_dn_dict[ca_dn]))
 
142
    else:
 
143
      ca_dn_dict[ca_dn]=ca_name
 
144
 
 
145
    if ldapbase.dn_regex.match(ca_dn):
 
146
      ca_entry = {'objectclass':['top','certificationAuthority']}
 
147
      ca_entry['cACertificate;binary'] = [cacert.readcertfile('der')]
 
148
 
 
149
      if create_crls:
 
150
        if os.path.isfile(ca.crl):
 
151
 
 
152
          cacrl = openssl.cert.CRLClass(ca.crl)
 
153
          ca_entry['certificateRevocationList;binary'] = [cacrl.readcertfile('der')]
 
154
          ca_entry['authorityRevocationList;binary'] = [cacrl.readcertfile('der')]
 
155
 
 
156
        else:
 
157
          sys.stderr.write('Warning: CRL file %s not found.\n' % (ca.crl))
 
158
          certificateRevocationList_binary=''
 
159
 
 
160
      ldiffile.write(ldif.CreateLDIF(ca_dn,ca_entry,['cACertificate;binary','certificateRevocationList;binary']))
 
161
      ldiffile.write('\n')
 
162
 
 
163
    else:
 
164
      sys.stderr.write('Warning: DN "%s" is not a valid DN.\nCheck parameter --dntemplate="%s".\n' % (ca_dn,dntemplate))
 
165
      cACertificate_binary=''
 
166
 
 
167
  else:
 
168
    sys.stderr.write('Warning: CA certificate file %s not found.\n' % (ca.certificate))
 
169
    cACertificate_binary=''
 
170
 
 
171
ldiffile.close()
 
172