"Fossies" - the Fresh Open Source Software Archive

Member "LinOTP-release-2.10.5.2/linotpd/src/tools/linotp-create-ad-users" (13 May 2019, 2889 Bytes) of package /linux/misc/LinOTP-release-2.10.5.2.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file.

    1 #!/usr/bin/env python2
    2 # -*- coding: utf-8 -*-
    3 #
    4 #    LinOTP - the open source solution for two factor authentication
    5 #    Copyright (C) 2010 - 2019 KeyIdentity GmbH
    6 #
    7 #    This file is part of LinOTP server.
    8 #
    9 #    This program is free software: you can redistribute it and/or
   10 #    modify it under the terms of the GNU Affero General Public
   11 #    License, version 3, as published by the Free Software Foundation.
   12 #
   13 #    This program is distributed in the hope that it will be useful,
   14 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
   15 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   16 #    GNU Affero General Public License for more details.
   17 #
   18 #    You should have received a copy of the
   19 #               GNU Affero General Public License
   20 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
   21 #
   22 #
   23 #    E-mail: linotp@keyidentity.com
   24 #    Contact: www.linotp.org
   25 #    Support: www.keyidentity.com
   26 #
   27 
   28 VERSION = '0.1'
   29 import os, sys
   30 from getopt import getopt, GetoptError
   31 from subprocess import call
   32 import ldap
   33 import ldap.modlist as modlist
   34 import getpass
   35 
   36 def usage():
   37     print  '''
   38     Parameter:
   39     -f <csv file>
   40     -h : help
   41     -u <ldap uri>
   42     -b <base, where to create the user>
   43     -d <Bind DN>
   44     '''
   45 
   46 
   47 def create_user(l, base, keys, values):
   48 
   49     dn = "%s=%s,%s" % (keys[0], values[0], base)
   50     attrs = {}
   51     attrs['objectclass'] = ['top', 'person', 'organizationalPerson', 'user']
   52     for x in range(1, len(keys)):
   53         attrs[keys[x - 1]] = values[x - 1]
   54 
   55     ldif = modlist.addModlist(attrs)
   56 
   57     print "creating user %s with attrs %s" % (values, attrs)
   58     l.add_s(dn, ldif)
   59     #l.modify(dn, mods)
   60 
   61 
   62 
   63 
   64 def main():
   65 
   66     file = None
   67     base = None
   68     uri = None
   69     bind = None
   70     password = None
   71 
   72     try:
   73         opts, args = getopt(sys.argv[1:], "f:u:b:d:", ["file="])
   74 
   75     except GetoptError:
   76         print "There is an error in your parameter syntax:"
   77         usage()
   78         sys.exit(1)
   79 
   80     for opt, arg in opts:
   81         if opt in ('-f', '--file'):
   82             file = arg
   83         elif opt in ('-b'):
   84             base = arg
   85         elif opt in ('-d'):
   86             bind = arg
   87         elif opt in ('-u'):
   88             uri = arg
   89         elif opt in ('-h', '--help'):
   90             usage()
   91             sys.exit(1)
   92 
   93     if not file or not base or not uri or not bind:
   94         usage()
   95         sys.exit(2)
   96     else:
   97         password = getpass.getpass("Please enter password for %s: " % bind)
   98 
   99         # read the first line
  100         f = open(file, "r")
  101         first_line = f.readline()
  102         keys = [ x.strip() for x in first_line.split(',') ]
  103 
  104         l = ldap.initialize(uri)
  105         l.simple_bind_s(bind, password)
  106 
  107         for line in f:
  108             values = [ v.strip() for v in line.split(',') ]
  109 
  110             create_user(l, base, keys, values)
  111 
  112         l.unbind_s()
  113 
  114 
  115 
  116 
  117 if __name__ == '__main__':
  118     main()