3 # This script is an interactive way to manipulate fields in the LDAP directory.
4 # When run it connects to the directory using the current users ID and fetches
5 # all the attributes for the first machine. It then formats them nicely and
6 # allows the user to change them.
8 # Usage: userinfo -a <user> -u <user> -c <user> -r
9 # -a Set the authentication user (the user whose password you are
11 # -h Set the host to display
13 import string, time, os, pwd, sys, getopt, ldap, crypt, whrandom, readline, copy;
14 from userdir_ldap import *;
17 AttrInfo = {"description": ["Machine Descr.", 1],
18 "hostname": ["Host names", 2],
19 "status": ["Status", 3],
21 "sponsor": ["Sponsors", 5],
22 "distribution": ["Distribution", 6],
23 "access": ["Access", 7],
24 "admin": ["Admin", 8],
25 "architecture": ["Architecture", 9],
26 "machine": ["Machine Hardware", 10],
27 "memory": ["Memory", 11],
29 "sshrsahostkey": ["SSH RSA", 14],
30 "sshdsahostkey": ["SSH DSA", 15],
31 "bandwidth": ["Bandwidth", 16]};
33 AttrPrompt = {"description": ["Purpose of the machine"],
34 "hostname": ["The hostnames for the box (ipv4/ipv6)"],
35 "status": ["Blank if Up, explaination if not"],
36 "l": ["Physical location"],
37 "sponsor": ["Sponsors and their URLs"],
38 "distribution": ["The distribution version"],
39 "access": ["all, developer only, restricted"],
40 "admin": ["Admin email address"],
41 "architecture": ["Debian Arhitecture string"],
42 "machine": ["Hardware description"],
43 "memory": ["Installed RAM"],
44 "disk": ["Disk Space, RAID levels, etc"],
45 "sshrsahostkey": ["A copy of /etc/ssh/ssh_host_key.pub"],
46 "sshdsahostkey": ["A copy of /etc/ssh/ssh_host_key.pub.dsa"],
47 "bandwidth": ["Available outbound"]};
49 # Create a map of IDs to desc,value,attr
51 for at in AttrInfo.keys():
52 if (AttrInfo[at][1] != 0):
53 OrderedIndex[AttrInfo[at][1]] = [AttrInfo[at][0], "", at];
54 OrigOrderedIndex = copy.deepcopy(OrderedIndex);
56 # Print out the automatic time stamp information
57 def PrintModTime(Attrs):
58 Stamp = GetAttr(Attrs,"modifytimestamp","");
60 Time = (int(Stamp[0:4]),int(Stamp[4:6]),int(Stamp[6:8]),
61 int(Stamp[8:10]),int(Stamp[10:12]),int(Stamp[12:14]),0,0,-1);
62 print "%-24s:" % ("Record last modified on"), time.strftime("%a %d/%m/%Y %X UTC",Time),
63 print "by",ldap.explode_dn(GetAttr(Attrs,"modifiersname"),1)[0];
65 Stamp = GetAttr(Attrs,"createtimestamp","");
67 Time = (int(Stamp[0:4]),int(Stamp[4:6]),int(Stamp[6:8]),
68 int(Stamp[8:10]),int(Stamp[10:12]),int(Stamp[12:14]),0,0,-1);
69 print "%-24s:" % ("Record created on"), time.strftime("%a %d/%m/%Y %X UTC",Time);
71 # Display all of the attributes in a numbered list
76 for at in Attrs[1].keys():
77 if AttrInfo.has_key(at):
78 if AttrInfo[at][1] == 0:
79 print " %-18s:" % (AttrInfo[at][0]),
80 for x in Attrs[1][at]:
84 OrderedIndex[AttrInfo[at][1]][1] = Attrs[1][at];
86 Keys = OrderedIndex.keys();
89 if at < 100 or RootMode != 0:
90 print " %3u) %-18s: " % (at,OrderedIndex[at][0]),
91 for x in OrderedIndex[at][1]:
92 print "'%s'" % (re.sub('[\n\r]','?',x)),
95 # Change a single attribute
96 def ChangeAttr(Attrs,Attr):
97 if (Attr == "sponsor" or Attr == "hostname"):
98 return MultiChangeAttr(Attrs,Attr);
100 print "Old value: '%s'" % (GetAttr(Attrs,Attr,""));
101 print "Press enter to leave unchanged and a single space to set to empty";
102 NewValue = raw_input("New? ");
106 print "Leaving unchanged.";
109 # Single space designates delete, trap the delete error
110 if (NewValue == " "):
113 l.modify_s(HostDn,[(ldap.MOD_DELETE,Attr,None)]);
114 except ldap.NO_SUCH_ATTRIBUTE:
118 Attrs[1][Attr] = [""];
123 l.modify_s(HostDn,[(ldap.MOD_REPLACE,Attr,NewValue)]);
124 Attrs[1][Attr] = [NewValue];
127 def MultiChangeAttr(Attrs,Attr):
128 # Make sure that we have an entry
129 if not Attrs[1].has_key(Attr):
132 Attrs[1][Attr].sort();
133 print "Old values: ",Attrs[1][Attr];
135 Mode = string.upper(raw_input("[D]elete or [A]dd? "));
136 if (Mode != 'D' and Mode != 'A'):
139 NewValue = raw_input("Value? ");
142 print "Leaving unchanged.";
149 l.modify_s(HostDn,[(ldap.MOD_DELETE,Attr,NewValue)]);
150 except ldap.NO_SUCH_ATTRIBUTE:
154 Attrs[1][Attr].remove(NewValue);
159 l.modify_s(HostDn,[(ldap.MOD_ADD,Attr,NewValue)]);
160 Attrs[1][Attr].append(NewValue);
163 # Main program starts here
164 User = pwd.getpwuid(os.getuid())[0];
167 (options, arguments) = getopt.getopt(sys.argv[1:], "nh:a:r")
168 for (switch, val) in options:
171 elif (switch == '-a'):
173 elif (switch == '-r'):
175 elif (switch == '-n'):
179 print "Accessing LDAP entry",
180 if (BindUser != User):
182 print "as '" + BindUser + "'";
186 Password = getpass(BindUser + "'s password: ");
188 # Connect to the ldap server
189 l = ldap.open(LDAPServer);
190 UserDn = "uid=" + BindUser + "," + BaseDn;
192 l.simple_bind_s(UserDn,Password);
194 l.simple_bind_s("","");
196 HBaseDn = "ou=hosts,dc=debian,dc=org";
197 HostDn = "host=" + Host + "," + HBaseDn;
199 # Query the server for all of the attributes
200 Attrs = l.search_s(HBaseDn,ldap.SCOPE_ONELEVEL,"host=" + Host);
202 print "Host",Host,"was not found.";
205 # repeatedly show the account configuration
212 print " a) Arbitary Change";
213 print " n) New Host";
214 print " u) Switch Hosts";
218 Response = raw_input("Change? ");
219 if (Response == "x" or Response == "X" or Response == "q" or
220 Response == "quit" or Response == "exit"):
223 # Change who we are looking at
224 if (Response == 'u' or Response == 'U'):
225 NewHost = raw_input("Host? ");
228 NAttrs = l.search_s(HBaseDn,ldap.SCOPE_ONELEVEL,"host=" + NewHost);
230 print "Host",NewHost,"was not found.";
234 HostDn = "host=" + Host + "," + HBaseDn;
235 OrderedIndex = copy.deepcopy(OrigOrderedIndex);
238 # Create a new entry and change to it Change who we are looking at
239 if (Response == 'n' or Response == 'N'):
240 NewHost = raw_input("Host? ");
243 NAttrs = l.search_s(HBaseDn,ldap.SCOPE_ONELEVEL,"host=" + NewHost);
245 print "Host",NewHost,"already exists.";
247 Dn = "host=" + NewHost + "," + HBaseDn;
248 l.add_s(Dn,[("host",NewHost),
249 ("objectclass","top")]);
252 NAttrs = l.search_s(HBaseDn,ldap.SCOPE_ONELEVEL,"host=" + NewHost);
254 print "Host",NewHost,"was not found.";
258 HostDn = "host=" + Host + "," + HBaseDn;
259 OrderedIndex = copy.deepcopy(OrigOrderedIndex);
262 # Handle changing an arbitary value
263 if (Response == "a"):
264 Attr = raw_input("Attr? ");
265 ChangeAttr(Attrs[0],Attr);
268 # Convert the integer response
271 if (not OrderedIndex.has_key(ID) or (ID > 100 and RootMode == 0)):
277 # Print the what to do prompt
278 print "Changing LDAP entry '%s' (%s)" % (OrderedIndex[ID][0],OrderedIndex[ID][2]);
279 print AttrPrompt[OrderedIndex[ID][2]][0];
280 ChangeAttr(Attrs[0],OrderedIndex[ID][2]);