This file is indexed.

/usr/bin/certs2ldap.py is in pyca 20031119-0.1ubuntu1.

This file is owned by root:root, with mode 0o755.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
#!/usr/bin/python

"""
certs2ldap.py - Upload all EE certificates on LDAP server
(c) by Michael Stroeder, michael@stroeder.com
"""

__version__ = '0.6.6'

import sys, string, os, getopt

ldap_attrtype = {
  'ST':'st',
  'Email':'mail',
  'emailAddress':'mail',
  'E':'mail',
  'L':'l',
  'O':'o',
  'OU':'ou',
  'CN':'cn',
}

def findoption(options,paramname):
  for i in options:
    if i[0]==paramname:
      return i
  return ()


def PrintUsage(ErrorMsg='',ErrorCode=1):
  script_name = string.split(sys.argv[0],os.sep)[-1]
  sys.stderr.write("""*** %s *** (C) by Michael Stroeder, 1999

usage: %s [options]

Options:

  -h or --help
        Print out this message

  --config=[pathname]
	Pathname of OpenSSL configuration file.
	You may also use env variable OPENSSL_CONF.
	Default: /etc/openssl/openssl.cnf

  --pycalib=[directory]
        Specify directory containing the pyCA modules
        You may also use env variable PYCALIB.
	Default: /usr/local/pyca/pylib

  --host=[LDAP host]
	Specify an alternate host:port on which the ldap server
        is running.
	Default: localhost:389

  --basedn=[searchbase]
        Use searchbase as the starting point for the search
        instead of the default.
	Default: emtpy string

  --binddn=[binddn]
	Use binddn to bind to the LDAP directory.
	Default: cn=root[,searchbase]

  --bindpasswd=[password]
	Use password to bind to the LDAP directory. For security
	reasons it is better to set this with the env variable
	LDAP_PASSWD if you really have to provide the password
	in a non-interactive script.
	Default: emtpy string

  --filtertemplate=[Python dict string]
	A Python string used as template for searching the
	LDAP entries of certificate owners.
	E.g. (&(cn=%%(CN)s)(mail=%%(Email)s))
	Default: (mail=%%(Email)s)

  --certdnfilter=[regex]
        Specify a filter as comma separated list of regular expressions
	for DNs of the certificates which should be sent to the LDAP host.
	E.g. C=DE,CN=.*,Email=.*@domain.my
	Default: Email=.*

  --objectclasses=[objectClass]
        Add objectclass: [objectClass] to the entry. Might be
        a comma-separated list for specifying multiple object classes.

  --replace
        Replace existing userCertificate;binary attributes

  --create
        Create LDAP entries if no entry for a user certificate
        was found.

  --dntemplate=[Python dict string]
	A Python string used as template for the distinguished
	name of LDAP entries to be created.
	E.g. cn=%%(CN)s+mail=%%(Email)s,ou=Testing,dc=stroeder,dc=com

""" % (script_name,script_name))
  if ErrorMsg:
    sys.stderr.write('Error: %s\n' % ErrorMsg)
  sys.exit(ErrorCode)

script_name=sys.argv[0]

try:
  options,args=getopt.getopt(
    sys.argv[1:],'h',
    [
      'help',
      'config=',
      'pycalib=',
      'host=',
      'basedn=',
      'binddn=',
      'bindpasswd=',
      'filtertemplate=',
      'certdnfilter=',
      'objectclasses=',
      'replace',
      'create',
      'dntemplate='
    ]
  )
except getopt.error,e:
  PrintUsage(str(e))

if findoption(options,'-h')!=() or findoption(options,'--help')!=():
  PrintUsage()

if findoption(options,'--config')!=():
  opensslcnfname = findoption(options,'--config')[1]
else:
  opensslcnfname = os.environ.get('OPENSSL_CONF','/etc/openssl/openssl.cnf')

if not os.path.isfile(opensslcnfname):
  PrintUsage('Config file %s not found.' % (opensslcnfname))
  sys.exit(1)

if findoption(options,'--pycalib')!=():
  pycalib = findoption(options,'--pycalib')[1]
else:
  pycalib = os.environ.get('PYCALIB','/usr/local/pyca/pylib')

if not os.path.exists(pycalib) or not os.path.isdir(pycalib):
  PrintUsage('Directory %s with pyCA modules not found!' % (pycalib))

sys.path.append(pycalib)

try:
  import ldap
except ImportError:
  PrintUsage('python-ldap module not found.' % (pycalib))
  sys.exit(1)

try:
  import openssl,charset
except ImportError:
  PrintUsage('pyCA modules not found in directory %s.' % (pycalib))
  sys.exit(1)

# Read the configuration file
if os.path.isfile('%s.pickle' % (opensslcnfname)):
  # Try to read OpenSSL's config file from a pickled copy
  f=open('%s.pickle' % (opensslcnfname),'rb')
  try:
    # first try to use the faster cPickle module
    from cPickle import load
  except ImportError:
    from pickle import load
  opensslcnf=load(f)
  f.close()
else:
  # Parse OpenSSL's config file from source
  opensslcnf=openssl.cnf.OpenSSLConfigClass(opensslcnfname)

delete_reason = {openssl.db.DB_TYPE_EXP:'expired',openssl.db.DB_TYPE_REV:'revoked'}

pyca_section = opensslcnf.data.get('pyca',{})
openssl.bin_filename = pyca_section.get('OpenSSLExec','/usr/bin/openssl')
if not os.path.isfile(openssl.bin_filename):
  sys.stderr.write('Did not find OpenSSL executable %s.\n' % (openssl.bin_filename))
  sys.exit(1)

if findoption(options,'--host')!=():
  ldap_host = findoption(options,'--host')[1]
else:
  ldap_host = 'localhost:389'

if findoption(options,'--basedn')!=():
  basedn = findoption(options,'--basedn')[1]
else:
  basedn = ''

if findoption(options,'--binddn')!=():
  binddn = findoption(options,'--binddn')[1]
else:
  if basedn:
    binddn = 'cn=root,%s' % basedn
  else:
    binddn = 'cn=root'

if findoption(options,'--bindpasswd')!=():
  bindpasswd = findoption(options,'--bindpasswd')[1]
else:
  if os.environ.has_key('LDAP_PASSWD'):
    bindpasswd = os.environ.get['LDAP_PASSWD']
  else:
    from getpass import getpass
    sys.stdout.write('Enter password for bind DN "%s".\n' % (binddn))
    bindpasswd = getpass()

if findoption(options,'--filtertemplate')!=():
  filtertemplate = findoption(options,'--filtertemplate')[1]
else:
  filtertemplate = r'(mail=%(Email)s)'

if findoption(options,'--objectclasses')!=():
  objectclasses = map(
    string.strip,
    map(
      None,string.split(findoption(options,'--objectclasses')[1],',')
    )
  )
else:
  objectclasses = None

replace = findoption(options,'--replace')!=()

if findoption(options,'--certdnfilter')!=():
  certdnfilterlist = string.split(findoption(options,'--certdnfilter')[1],',')
  certdnfilter = {}
  for i in certdnfilterlist:
    attr,filter = string.split(i,'=',1)
    if filter:
      certdnfilter[attr]=filter
else:
  certdnfilter = {'Email':'.*'}

create = findoption(options,'--create')!=()

if findoption(options,'--dntemplate')!=():
  dntemplate = findoption(options,'--dntemplate')[1]
else:
  dntemplate = r'mail=%(Email)s,'+basedn

print repr(dntemplate)

# FIX ME!!!
# This should be surrounded by a nice try: except: clause
# which catches specific exceptions and outputs
# nicer error messages.
l = ldap.open(ldap_host)
l.bind_s(binddn,bindpasswd,ldap.AUTH_SIMPLE)

ca_names = opensslcnf.sectionkeys.get('ca',[])

old_db_filenames = []

for ca_name in ca_names:

  sys.stdout.write('*** Processing %s ***\n\n' % (ca_name))

  ca = opensslcnf.getcadata(ca_name)

  # Ist der Zertifikattyp 'S/MIME for client use' ?
  if ca.isclientcert() and \
     not ca.database in old_db_filenames and \
     os.path.isfile(ca.database):

    old_db_filenames.append(ca.database)

    # Anfrage starten
    certs_found = openssl.db.GetEntriesbyDN(ca.database,certdnfilter,casesensitive=1,onlyvalid=0)

    for cert_entry in certs_found:

      certdn = charset.asn12iso(cert_entry[openssl.db.DB_name])
      certdndict = openssl.db.SplitDN(charset.iso2utf(certdn))
      ldap_filter = filtertemplate % certdndict
      try:
        ldap_result = l.search_s(
	  basedn,
	  ldap.SCOPE_SUBTREE,
	  ldap_filter,
	  ['objectclass','userCertificate;binary','userSMIMECertificate;binary'],
	  0
	)
      except ldap.NO_SUCH_OBJECT:
  	sys.stdout.write('Certificate subject "%s" not found with filter "%s".\n' % (certdn,ldap_filter))
	ldap_result=[]
      except:
	exc_obj,exc_value,exc_traceback = sys.exc_info()
	sys.stderr.write('Unexpected error during searching with filter "%s":\n%s\n' % (ldap_filter,exc_value))
	sys.exit(1)

      if ldap_result:

        # Read certificate data
        certfilename = os.path.join(ca.certs,'%s.pem' % (cert_entry[openssl.db.DB_serial]))
	cert = openssl.cert.X509CertificateClass(certfilename)
	local_cert = cert.readcertfile('der')

	for entry in ldap_result:

          ldap_dn = entry[0]

          old_objectclasses = {}
          for oc in entry[1].get('objectClass',entry[1].get('objectclass',[])):
            old_objectclasses[string.lower(oc)] = None

          existing_usercert_attrtype = None
          for a in [
            'userCertificate;binary','userCertificate',
            'usercertificate;binary','usercertificate',
          ]:
            if entry[1].has_key(a):
              existing_usercert_attrtype = a
              break

          old_usercertificate_attr = {}
          if existing_usercert_attrtype!=None:
            for ldap_cert in entry[1][existing_usercert_attrtype]:
              old_usercertificate_attr[ldap_cert] = None

          ldap_modlist = []

          if cert_entry[openssl.db.DB_type]==openssl.db.DB_TYPE_VAL:

	    if existing_usercert_attrtype is None:
              # Add new certificate attribute
              ldap_modlist.append((ldap.MOD_ADD,'userCertificate;binary',[local_cert]))
   	      sys.stdout.write('Adding new certificate attribute usercertificate;binary with certificate serial %s of LDAP entry "%s".\n' % (cert_entry[openssl.db.DB_serial],charset.utf2iso(ldap_dn)))
            elif replace:
              # Replace existing certificate attribute
              ldap_modlist.append((ldap.MOD_DELETE,existing_usercert_attrtype,None))
              ldap_modlist.append((ldap.MOD_ADD,existing_usercert_attrtype,[local_cert]))
   	      sys.stdout.write('Replacing attribute %s of entry %s with certificate serial %s.\n' % (
                  existing_usercert_attrtype,
		  charset.utf2iso(ldap_dn),
		  cert_entry[openssl.db.DB_serial]
	        )
	      )
	    elif not old_usercertificate_attr.has_key(local_cert):
              # Add new certificate attribute value
              ldap_modlist.append((ldap.MOD_DELETE,existing_usercert_attrtype,None))
              ldap_modlist.append((ldap.MOD_ADD,existing_usercert_attrtype,old_usercertificate_attr.keys()+[local_cert]))
   	      sys.stdout.write(
                'Adding certificate with certificate serial %s to existing attribute %s of LDAP entry "%s".\n' % (
                  cert_entry[openssl.db.DB_serial],
                  existing_usercert_attrtype,
                  charset.utf2iso(ldap_dn)
                )
              )
            else:
   	      sys.stdout.write('Leaving attribute %s of entry %s untouched.\n' % (
                  existing_usercert_attrtype,
		  charset.utf2iso(ldap_dn)
	        )
	      )

            if ldap_modlist and objectclasses:
              # New object classes were specified at command-line
              # => add to modify list if necessary
              new_objectclasses = []
              for oc in objectclasses:
                if not old_objectclasses.has_key(string.lower(oc)):
                  new_objectclasses.append(oc)
              if new_objectclasses:
                ldap_modlist.append((ldap.MOD_ADD,'objectClass',new_objectclasses))


          elif (cert_entry[openssl.db.DB_type]==openssl.db.DB_TYPE_EXP) or \
	       (cert_entry[openssl.db.DB_type]==openssl.db.DB_TYPE_REV):

            sys.stdout.write('Certificate (serial %s) %s.\n' % (cert_entry[openssl.db.DB_serial],delete_reason[cert_entry[openssl.db.DB_type]]))

            if old_usercertificate_attr.has_key(local_cert):
              del old_usercertificate_attr[local_cert]
              sys.stdout.write('Deleting certificate with certificate serial %s from attribute usercertificate;binary of LDAP entry "%s".\n' % (cert_entry[openssl.db.DB_serial],charset.utf2iso(ldap_dn)))
              ldap_modlist.append((ldap.MOD_REPLACE,existing_usercert_attrtype,old_usercertificate_attr.keys()))

            if ldap_modlist and objectclasses:
              new_objectclasses = []
              for oc in objectclasses:
                if old_objectclasses.has_key(string.lower(oc)):
                  new_objectclasses.append(oc)
              if new_objectclasses:
                ldap_modlist.append((ldap.MOD_DELETE,'objectClass',new_objectclasses))


          # Do modifications on directory if modlist is not empty
          if ldap_modlist:
	    try:
	      l.modify_s(ldap_dn,ldap_modlist)
	    except ldap.NO_SUCH_OBJECT:
	      sys.stderr.write('No such object "%s": Probably a parent entry is missing.\n' % (
	          charset.utf2iso(ldap_dn)
	        )
	      )
	    except ldap.INSUFFICIENT_ACCESS,e:
	      sys.stderr.write('You are not allowed to modify entry "%s": %s.\n' % (
	          charset.utf2iso(ldap_dn),str(e)
	        )
	      )
	    except ldap.LDAPError,e:
	      sys.stderr.write('LDAPError: %s.\n' % str(e))

      else:

        if cert_entry[openssl.db.DB_type]==openssl.db.DB_TYPE_VAL:

          if create:

            # Read certificate data
            certfilename = os.path.join(ca.certs,'%s.pem' % (cert_entry[openssl.db.DB_serial]))
	    cert = openssl.cert.X509CertificateClass(certfilename)
	    local_cert = cert.readcertfile('der')

            ldap_dn = dntemplate % certdndict

            ldap_modlist = [
              ('objectClass',['person','organizationalPerson','inetOrgPerson']),
              ('userCertificate;binary',[local_cert]),
              ('sn',['-'])
            ]
            for k in certdndict.keys():
              try:
                ldap_modlist.append((ldap_attrtype[k],certdndict[k]))
              except KeyError:
                pass

	    try:
#              print ldap_modlist
	      l.add_s(ldap_dn,ldap_modlist)
	    except ldap.NO_SUCH_OBJECT:
	      sys.stderr.write('No such object "%s": Probably a parent entry is missing.\n' % (
	          charset.utf2iso(ldap_dn)
	        )
	      )
	    except ldap.INSUFFICIENT_ACCESS,e:
	      sys.stderr.write('You are not allowed to add entry "%s": %s.\n' % (
	          charset.utf2iso(ldap_dn),str(e)
	        )
	      )
	    except ldap.LDAPError,e:
	      sys.stderr.write('LDAPError: %s.\n' % str(e))
            else:
   	      sys.stdout.write('Added new entry "%s" for certificate serial %s.\n' % (charset.utf2iso(ldap_dn),cert_entry[openssl.db.DB_serial]))
            
          else:
            sys.stderr.write('No entry found with filter "%s" for %s.\n' % (ldap_filter,certdn))     

l.unbind_s()