Initial import
[mirror/userdir-ldap.git] / ud-arbimport
1 #!/usr/bin/env python
2 # -*- mode: python -*-
3 # This script imports arbitary lists of data. The input is a file with 
4 # the form of:
5 #  uid: <data>
6
7 import string, re, time, ldap, getopt, sys;
8 from userdir_ldap import *;
9
10 # Process options
11 (options, arguments) = getopt.getopt(sys.argv[1:], "u:m:n")
12 for (switch, val) in options:
13    if (switch == '-u'):
14       AdminUser = val
15    elif (switch == '-m'):
16        LoadOverride(val);
17    elif (switch == '-n'):
18        NoAct = 1;
19 if len(arguments) == 0:
20    print "Give the key to assignt to then the file to import";
21    os.exit(0);
22
23 # Main program starts here
24 print "Accessing LDAP directory as '" + AdminUser + "'";
25 Password = getpass(AdminUser + "'s password: ");
26
27 # Connect to the ldap server
28 l = ldap.open(LDAPServer);
29 UserDn = "uid=" + AdminUser + "," + BaseDn;
30 l.simple_bind_s(UserDn,Password);
31
32 # Read the override file into the unknown map. The override file is a list
33 # of colon delimited entires mapping PGP email addresess to local users
34 List = open(arguments[1],"r");
35 while(1):
36    Line = List.readline();
37    if Line == "":
38       break;
39    Split = re.split("[:\n]",Line);
40    
41    Rec = [(ldap.MOD_REPLACE,arguments[0],string.strip(Split[1]))];
42    Dn = "uid=" + Split[0] + "," + BaseDn;
43    try:
44       l.modify_s(Dn,Rec);
45    except:
46       print "Failed",Dn;