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 "bandwidth": ["Bandwidth", 13]};
31 AttrPrompt = {"description": ["Purpose of the machine"],
32 "hostname": ["The hostnames for the box (ipv4/ipv6)"],
33 "status": ["Blank if Up, explaination if not"],
34 "l": ["Physical location"],
35 "sponsor": ["Sponsors and their URLs"],
36 "distribution": ["The distribution version"],
37 "access": ["all, developer only, restricted"],
38 "admin": ["Admin email address"],
39 "architecture": ["Debian Arhitecture string"],
40 "machine": ["Hardware description"],
41 "memory": ["Installed RAM"],
42 "disk": ["Disk Space, RAID levels, etc"],
43 "bandwidth": ["Available outbound"]};
45 # Create a map of IDs to desc,value,attr
47 for at in AttrInfo.keys():
48 if (AttrInfo[at][1] != 0):
49 OrderedIndex[AttrInfo[at][1]] = [AttrInfo[at][0], "", at];
50 OrigOrderedIndex = copy.deepcopy(OrderedIndex);
52 # Print out the automatic time stamp information
53 def PrintModTime(Attrs):
54 Stamp = GetAttr(Attrs,"modifytimestamp","");
56 Time = (int(Stamp[0:4]),int(Stamp[4:6]),int(Stamp[6:8]),
57 int(Stamp[8:10]),int(Stamp[10:12]),int(Stamp[12:14]),0,0,-1);
58 print "%-24s:" % ("Record last modified on"), time.strftime("%a %d/%m/%Y %X UTC",Time),
59 print "by",ldap.explode_dn(GetAttr(Attrs,"modifiersname"),1)[0];
61 Stamp = GetAttr(Attrs,"createtimestamp","");
63 Time = (int(Stamp[0:4]),int(Stamp[4:6]),int(Stamp[6:8]),
64 int(Stamp[8:10]),int(Stamp[10:12]),int(Stamp[12:14]),0,0,-1);
65 print "%-24s:" % ("Record created on"), time.strftime("%a %d/%m/%Y %X UTC",Time);
67 # Display all of the attributes in a numbered list
72 for at in Attrs[1].keys():
73 if AttrInfo.has_key(at):
74 if AttrInfo[at][1] == 0:
75 print " %-18s:" % (AttrInfo[at][0]),
76 for x in Attrs[1][at]:
80 OrderedIndex[AttrInfo[at][1]][1] = Attrs[1][at];
82 Keys = OrderedIndex.keys();
85 if at < 100 or RootMode != 0:
86 print " %3u) %-18s: " % (at,OrderedIndex[at][0]),
87 for x in OrderedIndex[at][1]:
88 print "'%s'" % (re.sub('[\n\r]','?',x)),
91 # Change a single attribute
92 def ChangeAttr(Attrs,Attr):
93 if (Attr == "sponsor" or Attr == "hostname"):
94 return MultiChangeAttr(Attrs,Attr);
96 print "Old value: '%s'" % (GetAttr(Attrs,Attr,""));
97 print "Press enter to leave unchanged and a single space to set to empty";
98 NewValue = raw_input("New? ");
102 print "Leaving unchanged.";
105 # Single space designates delete, trap the delete error
106 if (NewValue == " "):
109 l.modify_s(HostDn,[(ldap.MOD_DELETE,Attr,None)]);
110 except ldap.NO_SUCH_ATTRIBUTE:
114 Attrs[1][Attr] = [""];
119 l.modify_s(HostDn,[(ldap.MOD_REPLACE,Attr,NewValue)]);
120 Attrs[1][Attr] = [NewValue];
123 def MultiChangeAttr(Attrs,Attr):
124 # Make sure that we have an entry
125 if not Attrs[1].has_key(Attr):
128 Attrs[1][Attr].sort();
129 print "Old values: ",Attrs[1][Attr];
131 Mode = string.upper(raw_input("[D]elete or [A]dd? "));
132 if (Mode != 'D' and Mode != 'A'):
135 NewValue = raw_input("Value? ");
138 print "Leaving unchanged.";
145 l.modify_s(HostDn,[(ldap.MOD_DELETE,Attr,NewValue)]);
146 except ldap.NO_SUCH_ATTRIBUTE:
150 Attrs[1][Attr].remove(NewValue);
155 l.modify_s(HostDn,[(ldap.MOD_ADD,Attr,NewValue)]);
156 Attrs[1][Attr].append(NewValue);
159 # Main program starts here
160 User = pwd.getpwuid(os.getuid())[0];
163 (options, arguments) = getopt.getopt(sys.argv[1:], "nh:a:r")
164 for (switch, val) in options:
167 elif (switch == '-a'):
169 elif (switch == '-r'):
171 elif (switch == '-n'):
175 print "Accessing LDAP entry",
176 if (BindUser != User):
178 print "as '" + BindUser + "'";
182 Password = getpass(BindUser + "'s password: ");
184 # Connect to the ldap server
185 l = ldap.open(LDAPServer);
186 UserDn = "uid=" + BindUser + "," + BaseDn;
188 l.simple_bind_s(UserDn,Password);
190 l.simple_bind_s("","");
192 HBaseDn = "ou=hosts,dc=debian,dc=org";
193 HostDn = "host=" + Host + "," + HBaseDn;
195 # Query the server for all of the attributes
196 Attrs = l.search_s(HBaseDn,ldap.SCOPE_ONELEVEL,"host=" + Host);
198 print "Host",Host,"was not found.";
201 # repeatedly show the account configuration
208 print " a) Arbitary Change";
209 print " n) New Host";
210 print " u) Switch Hosts";
214 Response = raw_input("Change? ");
215 if (Response == "x" or Response == "X" or Response == "q" or
216 Response == "quit" or Response == "exit"):
219 # Change who we are looking at
220 if (Response == 'u' or Response == 'U'):
221 NewHost = raw_input("Host? ");
224 NAttrs = l.search_s(HBaseDn,ldap.SCOPE_ONELEVEL,"host=" + NewHost);
226 print "Host",NewHost,"was not found.";
230 HostDn = "host=" + Host + "," + HBaseDn;
231 OrderedIndex = copy.deepcopy(OrigOrderedIndex);
234 # Create a new entry and change to it Change who we are looking at
235 if (Response == 'n' or Response == 'N'):
236 NewHost = raw_input("Host? ");
239 NAttrs = l.search_s(HBaseDn,ldap.SCOPE_ONELEVEL,"host=" + NewHost);
241 print "Host",NewHost,"already exists.";
243 Dn = "host=" + NewHost + "," + HBaseDn;
244 l.add_s(Dn,[("host",NewHost),
245 ("objectclass","top")]);
248 NAttrs = l.search_s(HBaseDn,ldap.SCOPE_ONELEVEL,"host=" + NewHost);
250 print "Host",NewHost,"was not found.";
254 HostDn = "host=" + Host + "," + HBaseDn;
255 OrderedIndex = copy.deepcopy(OrigOrderedIndex);
258 # Handle changing an arbitary value
259 if (Response == "a"):
260 Attr = raw_input("Attr? ");
261 ChangeAttr(Attrs[0],Attr);
264 # Convert the integer response
267 if (not OrderedIndex.has_key(ID) or (ID > 100 and RootMode == 0)):
273 # Print the what to do prompt
274 print "Changing LDAP entry '%s' (%s)" % (OrderedIndex[ID][0],OrderedIndex[ID][2]);
275 print AttrPrompt[OrderedIndex[ID][2]][0];
276 ChangeAttr(Attrs[0],OrderedIndex[ID][2]);