4 # Copyright (c) 2000-2001 Jason Gunthorpe <jgg@debian.org>
5 # Copyright (c) 2001 Ryan Murray <rmurray@debian.org>
6 # Copyright (c) 2003 James Troup <troup@debian.org>
7 # Copyright (c) 2004 Joey Schulze <joey@infodrom.org>
9 # This program is free software; you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 2 of the License, or
12 # (at your option) any later version.
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
19 # You should have received a copy of the GNU General Public License
20 # along with this program; if not, write to the Free Software
21 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 # This script is an interactive way to manipulate fields in the LDAP directory.
24 # When run it connects to the directory using the current users ID and fetches
25 # all the attributes for the first machine. It then formats them nicely and
26 # allows the user to change them.
28 # Usage: userinfo -a <user> -u <user> -c <user> -r
29 # -a Set the authentication user (the user whose password you are
31 # -h Set the host to display
33 import string, time, os, pwd, sys, getopt, ldap, crypt, whrandom, readline, copy;
34 from userdir_ldap import *;
37 AttrInfo = {"description": ["Machine Descr.", 1],
38 "hostname": ["Host names", 2],
39 "status": ["Status", 3],
41 "sponsor": ["Sponsors", 5],
42 "distribution": ["Distribution", 6],
43 "access": ["Access", 7],
44 "admin": ["Admin", 8],
45 "architecture": ["Architecture", 9],
46 "machine": ["Machine Hardware", 10],
47 "memory": ["Memory", 11],
49 "sshRSAHostKey": ["SSH Host Keys", 14],
50 "bandwidth": ["Bandwidth", 15]};
52 AttrPrompt = {"description": ["Purpose of the machine"],
53 "hostname": ["The hostnames for the box (ipv4/ipv6)"],
54 "status": ["Blank if Up, explaination if not"],
55 "l": ["Physical location"],
56 "sponsor": ["Sponsors and their URLs"],
57 "distribution": ["The distribution version"],
58 "access": ["all, developer only, restricted"],
59 "admin": ["Admin email address"],
60 "architecture": ["Debian Architecture string"],
61 "machine": ["Hardware description"],
62 "memory": ["Installed RAM"],
63 "disk": ["Disk Space, RAID levels, etc"],
64 "sshRSAHostKey": ["A copy of /etc/ssh/ssh_*host_key.pub"],
65 "bandwidth": ["Available outbound"]};
67 # Create a map of IDs to desc,value,attr
69 for at in AttrInfo.keys():
70 if (AttrInfo[at][1] != 0):
71 OrderedIndex[AttrInfo[at][1]] = [AttrInfo[at][0], "", at];
72 OrigOrderedIndex = copy.deepcopy(OrderedIndex);
74 # Print out the automatic time stamp information
75 def PrintModTime(Attrs):
76 Stamp = GetAttr(Attrs,"modifyTimestamp","");
78 Time = (int(Stamp[0:4]),int(Stamp[4:6]),int(Stamp[6:8]),
79 int(Stamp[8:10]),int(Stamp[10:12]),int(Stamp[12:14]),0,0,-1);
80 print "%-24s:" % ("Record last modified on"), time.strftime("%a %d/%m/%Y %X UTC",Time),
81 print "by",ldap.explode_dn(GetAttr(Attrs,"modifiersName"),1)[0];
83 Stamp = GetAttr(Attrs,"createTimestamp","");
85 Time = (int(Stamp[0:4]),int(Stamp[4:6]),int(Stamp[6:8]),
86 int(Stamp[8:10]),int(Stamp[10:12]),int(Stamp[12:14]),0,0,-1);
87 print "%-24s:" % ("Record created on"), time.strftime("%a %d/%m/%Y %X UTC",Time);
89 # Display all of the attributes in a numbered list
94 for at in Attrs[1].keys():
95 if AttrInfo.has_key(at):
96 if AttrInfo[at][1] == 0:
97 print " %-18s:" % (AttrInfo[at][0]),
98 for x in Attrs[1][at]:
102 OrderedIndex[AttrInfo[at][1]][1] = Attrs[1][at];
104 Keys = OrderedIndex.keys();
107 if at < 100 or RootMode != 0:
108 print " %3u) %-18s: " % (at,OrderedIndex[at][0]),
109 for x in OrderedIndex[at][1]:
110 print "'%s'" % (re.sub('[\n\r]','?',x)),
114 """Display a one-line overview for a given host"""
115 if 'status' in Attrs[1].keys():
116 status = Attrs[1]['status'][0]
119 print "%-12s %-10s %-38s %-25s %s" % (\
120 Attrs[1]['host'][0], \
121 Attrs[1]['architecture'][0], \
122 Attrs[1]['distribution'][0], \
123 Attrs[1]['access'][0], \
126 # Change a single attribute
127 def ChangeAttr(Attrs,Attr):
128 if (Attr == "sponsor" or Attr == "hostname" or Attr == "sshRSAHostKey"):
129 return MultiChangeAttr(Attrs,Attr);
131 print "Old value: '%s'" % (GetAttr(Attrs,Attr,""));
132 print "Press enter to leave unchanged and a single space to set to empty";
133 NewValue = raw_input("New? ");
137 print "Leaving unchanged.";
140 # Single space designates delete, trap the delete error
141 if (NewValue == " "):
144 l.modify_s(HostDn,[(ldap.MOD_DELETE,Attr,None)]);
145 except ldap.NO_SUCH_ATTRIBUTE:
149 Attrs[1][Attr] = [""];
154 l.modify_s(HostDn,[(ldap.MOD_REPLACE,Attr,NewValue)]);
155 Attrs[1][Attr] = [NewValue];
158 def MultiChangeAttr(Attrs,Attr):
159 # Make sure that we have an entry
160 if not Attrs[1].has_key(Attr):
163 Attrs[1][Attr].sort();
164 print "Old values: ",Attrs[1][Attr];
166 Mode = string.upper(raw_input("[D]elete or [A]dd? "));
167 if (Mode != 'D' and Mode != 'A'):
170 NewValue = raw_input("Value? ");
173 print "Leaving unchanged.";
180 l.modify_s(HostDn,[(ldap.MOD_DELETE,Attr,NewValue)]);
181 except ldap.NO_SUCH_ATTRIBUTE:
185 Attrs[1][Attr].remove(NewValue);
190 l.modify_s(HostDn,[(ldap.MOD_ADD,Attr,NewValue)]);
191 Attrs[1][Attr].append(NewValue);
194 # Main program starts here
195 User = pwd.getpwuid(os.getuid())[0];
198 (options, arguments) = getopt.getopt(sys.argv[1:], "nh:a:rl")
199 for (switch, val) in options:
202 elif (switch == '-a'):
204 elif (switch == '-r'):
206 elif (switch == '-n'):
208 elif (switch == '-l'):
213 l = passwdAccessLDAP(LDAPServer, BaseDn, BindUser)
215 l = ldap.open(LDAPServer);
216 l.simple_bind_s("","")
218 HBaseDn = "ou=hosts,dc=debian,dc=org";
221 Attrs = l.search_s(HBaseDn,ldap.SCOPE_ONELEVEL,"host=*")
224 hosts.append(hAttrs[1]['host'][0])
227 print "%-12s %-10s %-38s %-25s %s" % ("Host name","Arch","Distribution","Access","Status")
231 if host == hAttrs[1]['host'][0]:
235 HostDn = "host=" + Host + "," + HBaseDn;
237 # Query the server for all of the attributes
238 Attrs = l.search_s(HBaseDn,ldap.SCOPE_ONELEVEL,"host=" + Host);
240 print "Host",Host,"was not found.";
243 # repeatedly show the account configuration
250 print " a) Arbitary Change";
251 print " n) New Host";
252 print " d) Delete Host";
253 print " u) Switch Hosts";
257 Response = raw_input("Change? ");
258 if (Response == "x" or Response == "X" or Response == "q" or
259 Response == "quit" or Response == "exit"):
262 # Change who we are looking at
263 if (Response == 'u' or Response == 'U'):
264 NewHost = raw_input("Host? ");
267 NAttrs = l.search_s(HBaseDn,ldap.SCOPE_ONELEVEL,"host=" + NewHost);
269 print "Host",NewHost,"was not found.";
273 HostDn = "host=" + Host + "," + HBaseDn;
274 OrderedIndex = copy.deepcopy(OrigOrderedIndex);
277 # Create a new entry and change to it Change who we are looking at
278 if (Response == 'n' or Response == 'N'):
279 NewHost = raw_input("Host? ");
282 NAttrs = l.search_s(HBaseDn,ldap.SCOPE_ONELEVEL,"host=" + NewHost);
284 print "Host",NewHost,"already exists.";
286 NewHostName = raw_input("Hostname? ");
289 Dn = "host=" + NewHost + "," + HBaseDn;
290 l.add_s(Dn,[("host", NewHost),
291 ("hostname", NewHostName),
292 ("objectClass", ("top", "debianServer"))]);
295 NAttrs = l.search_s(HBaseDn,ldap.SCOPE_ONELEVEL,"host=" + NewHost);
297 print "Host",NewHost,"was not found.";
301 HostDn = "host=" + Host + "," + HBaseDn;
302 OrderedIndex = copy.deepcopy(OrigOrderedIndex);
305 # Handle changing an arbitary value
306 if (Response == "a"):
307 Attr = raw_input("Attr? ");
308 ChangeAttr(Attrs[0],Attr);
311 if (Response == 'd'):
312 Really = raw_input("Really (type yes)? ");
315 print "Deleting",HostDn;
319 # Convert the integer response
322 if (not OrderedIndex.has_key(ID) or (ID > 100 and RootMode == 0)):
328 # Print the what to do prompt
329 print "Changing LDAP entry '%s' (%s)" % (OrderedIndex[ID][0],OrderedIndex[ID][2]);
330 print AttrPrompt[OrderedIndex[ID][2]][0];
331 ChangeAttr(Attrs[0],OrderedIndex[ID][2]);