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-2005 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
32 # -l list all hosts and their status
33 # -f list all SSH fingerprints
35 import string, time, os, pwd, sys, getopt, ldap, crypt, whrandom, readline, copy;
36 from tempfile import mktemp
37 from os import O_CREAT, O_EXCL, O_WRONLY
38 from userdir_ldap import *;
41 AttrInfo = {"description": ["Machine Descr.", 1],
42 "hostname": ["Host names", 2],
43 "status": ["Status", 3],
45 "sponsor": ["Sponsors", 5],
46 "distribution": ["Distribution", 6],
47 "access": ["Access", 7],
48 "admin": ["Admin", 8],
49 "architecture": ["Architecture", 9],
50 "machine": ["Machine Hardware", 10],
51 "memory": ["Memory", 11],
53 "sshRSAHostKey": ["SSH Host Keys", 14],
54 "bandwidth": ["Bandwidth", 15]};
56 AttrPrompt = {"description": ["Purpose of the machine"],
57 "hostname": ["The hostnames for the box (ipv4/ipv6)"],
58 "status": ["Blank if Up, explaination if not"],
59 "l": ["Physical location"],
60 "sponsor": ["Sponsors and their URLs"],
61 "distribution": ["The distribution version"],
62 "access": ["all, developer only, restricted"],
63 "admin": ["Admin email address"],
64 "architecture": ["Debian Architecture string"],
65 "machine": ["Hardware description"],
66 "memory": ["Installed RAM"],
67 "disk": ["Disk Space, RAID levels, etc"],
68 "sshRSAHostKey": ["A copy of /etc/ssh/ssh_*host_key.pub"],
69 "bandwidth": ["Available outbound"]};
71 # Create a map of IDs to desc,value,attr
73 for at in AttrInfo.keys():
74 if (AttrInfo[at][1] != 0):
75 OrderedIndex[AttrInfo[at][1]] = [AttrInfo[at][0], "", at];
76 OrigOrderedIndex = copy.deepcopy(OrderedIndex);
78 # Print out the automatic time stamp information
79 def PrintModTime(Attrs):
80 Stamp = GetAttr(Attrs,"modifyTimestamp","");
82 Time = (int(Stamp[0:4]),int(Stamp[4:6]),int(Stamp[6:8]),
83 int(Stamp[8:10]),int(Stamp[10:12]),int(Stamp[12:14]),0,0,-1);
84 print "%-24s:" % ("Record last modified on"), time.strftime("%a %d/%m/%Y %X UTC",Time),
85 print "by",ldap.explode_dn(GetAttr(Attrs,"modifiersName"),1)[0];
87 Stamp = GetAttr(Attrs,"createTimestamp","");
89 Time = (int(Stamp[0:4]),int(Stamp[4:6]),int(Stamp[6:8]),
90 int(Stamp[8:10]),int(Stamp[10:12]),int(Stamp[12:14]),0,0,-1);
91 print "%-24s:" % ("Record created on"), time.strftime("%a %d/%m/%Y %X UTC",Time);
93 # Display all of the attributes in a numbered list
98 for at in Attrs[1].keys():
99 if AttrInfo.has_key(at):
100 if AttrInfo[at][1] == 0:
101 print " %-18s:" % (AttrInfo[at][0]),
102 for x in Attrs[1][at]:
106 OrderedIndex[AttrInfo[at][1]][1] = Attrs[1][at];
108 Keys = OrderedIndex.keys();
111 if at < 100 or RootMode != 0:
112 print " %3u) %-18s: " % (at,OrderedIndex[at][0]),
113 for x in OrderedIndex[at][1]:
114 print "'%s'" % (re.sub('[\n\r]','?',x)),
118 """Display a one-line overview for a given host"""
119 if 'status' in Attrs[1].keys():
120 status = Attrs[1]['status'][0]
123 print "%-12s %-10s %-38s %-25s %s" % (\
124 Attrs[1]['host'][0], \
125 Attrs[1]['architecture'][0], \
126 Attrs[1]['distribution'][0], \
127 Attrs[1]['access'][0], \
130 # Change a single attribute
131 def ChangeAttr(Attrs,Attr):
132 if (Attr == "sponsor" or Attr == "hostname" or Attr == "sshRSAHostKey"):
133 return MultiChangeAttr(Attrs,Attr);
135 print "Old value: '%s'" % (GetAttr(Attrs,Attr,""));
136 print "Press enter to leave unchanged and a single space to set to empty";
137 NewValue = raw_input("New? ");
141 print "Leaving unchanged.";
144 # Single space designates delete, trap the delete error
145 if (NewValue == " "):
148 l.modify_s(HostDn,[(ldap.MOD_DELETE,Attr,None)]);
149 except ldap.NO_SUCH_ATTRIBUTE:
153 Attrs[1][Attr] = [""];
158 l.modify_s(HostDn,[(ldap.MOD_REPLACE,Attr,NewValue)]);
159 Attrs[1][Attr] = [NewValue];
162 def MultiChangeAttr(Attrs,Attr):
163 # Make sure that we have an entry
164 if not Attrs[1].has_key(Attr):
167 Attrs[1][Attr].sort();
168 print "Old values: ",Attrs[1][Attr];
170 Mode = string.upper(raw_input("[D]elete or [A]dd? "));
171 if (Mode != 'D' and Mode != 'A'):
174 NewValue = raw_input("Value? ");
177 print "Leaving unchanged.";
184 l.modify_s(HostDn,[(ldap.MOD_DELETE,Attr,NewValue)]);
185 except ldap.NO_SUCH_ATTRIBUTE:
189 Attrs[1][Attr].remove(NewValue);
194 l.modify_s(HostDn,[(ldap.MOD_ADD,Attr,NewValue)]);
195 Attrs[1][Attr].append(NewValue);
203 fd = os.open(name, O_CREAT | O_EXCL | O_WRONLY, 0600)
211 # Main program starts here
212 User = pwd.getpwuid(os.getuid())[0];
217 (options, arguments) = getopt.getopt(sys.argv[1:], "nh:a:rlf")
218 for (switch, val) in options:
221 elif (switch == '-a'):
223 elif (switch == '-r'):
225 elif (switch == '-n'):
227 elif (switch == '-l'):
230 elif (switch == '-f'):
235 l = passwdAccessLDAP(LDAPServer, BaseDn, BindUser)
237 l = ldap.open(LDAPServer);
238 l.simple_bind_s("","")
240 HBaseDn = "ou=hosts,dc=debian,dc=org";
243 Attrs = l.search_s(HBaseDn,ldap.SCOPE_ONELEVEL,"host=*")
246 hosts.append(hAttrs[1]['host'][0])
249 print "%-12s %-10s %-38s %-25s %s" % ("Host name","Arch","Distribution","Access","Status")
253 if host == hAttrs[1]['host'][0]:
256 elif FingerPrints == 1:
258 Attrs = l.search_s(HBaseDn,ldap.SCOPE_ONELEVEL,"host=" + Host)
260 Attrs = l.search_s(HBaseDn,ldap.SCOPE_ONELEVEL,"host=*")
263 hosts.append(hAttrs[1]['host'][0])
266 tmpfile = CalcTempFile()
269 if host == hAttrs[1]['host'][0]:
270 if 'sshRSAHostKey' in hAttrs[1].keys():
271 for key in hAttrs[1]['sshRSAHostKey']:
272 tmp = open(tmpfile, 'w')
273 tmp.write(key + '\n')
275 fp = os.popen('/usr/bin/ssh-keygen -l -f ' + tmpfile, "r")
276 input = fp.readline()
278 fingerprint = input.split(' ')
279 print "%s %s root@%s" % (fingerprint[0], fingerprint[1], host)
283 HostDn = "host=" + Host + "," + HBaseDn;
285 # Query the server for all of the attributes
286 Attrs = l.search_s(HBaseDn,ldap.SCOPE_ONELEVEL,"host=" + Host);
288 print "Host",Host,"was not found.";
291 # repeatedly show the account configuration
298 print " a) Arbitary Change";
299 print " n) New Host";
300 print " d) Delete Host";
301 print " u) Switch Hosts";
305 Response = raw_input("Change? ");
306 if (Response == "x" or Response == "X" or Response == "q" or
307 Response == "quit" or Response == "exit"):
310 # Change who we are looking at
311 if (Response == 'u' or Response == 'U'):
312 NewHost = raw_input("Host? ");
315 NAttrs = l.search_s(HBaseDn,ldap.SCOPE_ONELEVEL,"host=" + NewHost);
317 print "Host",NewHost,"was not found.";
321 HostDn = "host=" + Host + "," + HBaseDn;
322 OrderedIndex = copy.deepcopy(OrigOrderedIndex);
325 # Create a new entry and change to it Change who we are looking at
326 if (Response == 'n' or Response == 'N'):
327 NewHost = raw_input("Host? ");
330 NAttrs = l.search_s(HBaseDn,ldap.SCOPE_ONELEVEL,"host=" + NewHost);
332 print "Host",NewHost,"already exists.";
334 NewHostName = raw_input("Hostname? ");
337 Dn = "host=" + NewHost + "," + HBaseDn;
338 l.add_s(Dn,[("host", NewHost),
339 ("hostname", NewHostName),
340 ("objectClass", ("top", "debianServer"))]);
343 NAttrs = l.search_s(HBaseDn,ldap.SCOPE_ONELEVEL,"host=" + NewHost);
345 print "Host",NewHost,"was not found.";
349 HostDn = "host=" + Host + "," + HBaseDn;
350 OrderedIndex = copy.deepcopy(OrigOrderedIndex);
353 # Handle changing an arbitary value
354 if (Response == "a"):
355 Attr = raw_input("Attr? ");
356 ChangeAttr(Attrs[0],Attr);
359 if (Response == 'd'):
360 Really = raw_input("Really (type yes)? ");
363 print "Deleting",HostDn;
367 # Convert the integer response
370 if (not OrderedIndex.has_key(ID) or (ID > 100 and RootMode == 0)):
376 # Print the what to do prompt
377 print "Changing LDAP entry '%s' (%s)" % (OrderedIndex[ID][0],OrderedIndex[ID][2]);
378 print AttrPrompt[OrderedIndex[ID][2]][0];
379 ChangeAttr(Attrs[0],OrderedIndex[ID][2]);