Added support for -l to list all hosts in the directory
[mirror/userdir-ldap.git] / ud-host
1 #!/usr/bin/env python
2 # -*- mode: python -*-
3
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>
8 #
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.
13 #
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.
18 #
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.
22
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.
27 #
28 #  Usage: userinfo -a <user> -u <user> -c <user> -r
29 #    -a    Set the authentication user (the user whose password you are
30 #          going to enter)
31 #    -h    Set the host to display
32
33 import string, time, os, pwd, sys, getopt, ldap, crypt, whrandom, readline, copy;
34 from userdir_ldap import *;
35
36 RootMode = 0;
37 AttrInfo = {"description": ["Machine Descr.", 1],
38             "hostname": ["Host names", 2],
39             "status": ["Status", 3],
40             "l": ["Location", 4],
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],
48             "disk": ["Disk", 12],
49             "sshRSAHostKey": ["SSH Host Keys", 14],
50             "bandwidth": ["Bandwidth", 15]};
51
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"]};
66
67 # Create a map of IDs to desc,value,attr
68 OrderedIndex = {};
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);
73
74 # Print out the automatic time stamp information
75 def PrintModTime(Attrs):
76    Stamp = GetAttr(Attrs,"modifyTimestamp","");
77    if len(Stamp) >= 13:
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];
82
83    Stamp = GetAttr(Attrs,"createTimestamp","");
84    if len(Stamp) >= 13:
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);
88
89 # Display all of the attributes in a numbered list
90 def ShowAttrs(Attrs):
91    print;
92    PrintModTime(Attrs);
93
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]:
99                print "'%s'" % (x),
100             print;
101          else:
102             OrderedIndex[AttrInfo[at][1]][1] = Attrs[1][at];
103
104    Keys = OrderedIndex.keys();
105    Keys.sort();
106    for at in 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)),
111          print;
112
113 def Overview(Attrs):
114    """Display a one-line overview for a given host"""
115    if 'status' in Attrs[1].keys():
116       status = Attrs[1]['status'][0]
117    else:
118       status = ''
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], \
124       status)
125
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);
130
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? ");
134
135    # Empty string
136    if (NewValue == ""):
137       print "Leaving unchanged.";
138       return;
139
140    # Single space designates delete, trap the delete error
141    if (NewValue == " "):
142       print "Deleting.",;
143       try:
144          l.modify_s(HostDn,[(ldap.MOD_DELETE,Attr,None)]);
145       except ldap.NO_SUCH_ATTRIBUTE:
146          pass;
147
148       print;
149       Attrs[1][Attr] = [""];
150       return;
151
152    # Set a new value
153    print "Setting.",;
154    l.modify_s(HostDn,[(ldap.MOD_REPLACE,Attr,NewValue)]);
155    Attrs[1][Attr] = [NewValue];
156    print;
157
158 def MultiChangeAttr(Attrs,Attr):
159    # Make sure that we have an entry
160    if not Attrs[1].has_key(Attr):
161       Attrs[1][Attr] = [];
162
163    Attrs[1][Attr].sort();
164    print "Old values: ",Attrs[1][Attr];
165
166    Mode = string.upper(raw_input("[D]elete or [A]dd? "));
167    if (Mode != 'D' and Mode != 'A'):
168       return;
169
170    NewValue = raw_input("Value? ");
171    # Empty string
172    if (NewValue == ""):
173       print "Leaving unchanged.";
174       return;
175
176    # Delete
177    if (Mode == "D"):
178       print "Deleting.",;
179       try:
180          l.modify_s(HostDn,[(ldap.MOD_DELETE,Attr,NewValue)]);
181       except ldap.NO_SUCH_ATTRIBUTE:
182          print "Failed";
183
184       print;
185       Attrs[1][Attr].remove(NewValue);
186       return;
187
188    # Set a new value
189    print "Setting.",;
190    l.modify_s(HostDn,[(ldap.MOD_ADD,Attr,NewValue)]);
191    Attrs[1][Attr].append(NewValue);
192    print;
193
194 # Main program starts here
195 User = pwd.getpwuid(os.getuid())[0];
196 BindUser = User;
197 # Process options
198 (options, arguments) = getopt.getopt(sys.argv[1:], "nh:a:rl")
199 for (switch, val) in options:
200    if (switch == '-h'):
201       Host = val;
202    elif (switch == '-a'):
203       BindUser = val;
204    elif (switch == '-r'):
205       RootMode = 1;
206    elif (switch == '-n'):
207       BindUser = "";
208    elif (switch == '-l'):
209       ListMode = 1
210
211 if (BindUser != ""):
212    l = passwdAccessLDAP(LDAPServer, BaseDn, BindUser)
213 else:
214    l = ldap.open(LDAPServer);
215    l.simple_bind_s("","")
216
217 HBaseDn = "ou=hosts,dc=debian,dc=org";
218
219 if ListMode == 1:
220    Attrs = l.search_s(HBaseDn,ldap.SCOPE_ONELEVEL,"host=*")
221    hosts = []
222    for hAttrs in Attrs:
223       hosts.append(hAttrs[1]['host'][0])
224    hosts.sort()
225
226    print "%-12s  %-10s  %-38s  %-25s %s" % ("Host name","Arch","Distribution","Access","Status")
227    print "-"*115
228    for host in hosts:
229       for hAttrs in Attrs:
230          if host == hAttrs[1]['host'][0]:
231             Overview(hAttrs)
232    sys.exit(0)
233
234 HostDn = "host=" + Host + "," + HBaseDn;
235
236 # Query the server for all of the attributes
237 Attrs = l.search_s(HBaseDn,ldap.SCOPE_ONELEVEL,"host=" + Host);
238 if len(Attrs) == 0:
239    print "Host",Host,"was not found.";
240    sys.exit(0);
241
242 # repeatedly show the account configuration
243 while(1):
244    ShowAttrs(Attrs[0]);
245    if (BindUser == ""):
246       sys.exit(0);
247
248    if RootMode == 1:
249       print "   a) Arbitary Change";
250    print "   n) New Host";
251    print "   d) Delete Host";
252    print "   u) Switch Hosts";
253    print "   x) Exit";
254
255    # Prompt
256    Response = raw_input("Change? ");
257    if (Response == "x" or Response == "X" or Response == "q" or
258        Response == "quit" or Response == "exit"):
259       break;
260
261    # Change who we are looking at
262    if (Response == 'u' or Response == 'U'):
263       NewHost = raw_input("Host? ");
264       if NewHost == "":
265          continue;
266       NAttrs = l.search_s(HBaseDn,ldap.SCOPE_ONELEVEL,"host=" + NewHost);
267       if len(NAttrs) == 0:
268          print "Host",NewHost,"was not found.";
269          continue;
270       Attrs = NAttrs;
271       Host = NewHost;
272       HostDn = "host=" + Host + "," + HBaseDn;
273       OrderedIndex = copy.deepcopy(OrigOrderedIndex);
274       continue;
275
276    # Create a new entry and change to it Change who we are looking at
277    if (Response == 'n' or Response == 'N'):
278       NewHost = raw_input("Host? ");
279       if NewHost == "":
280          continue;
281       NAttrs = l.search_s(HBaseDn,ldap.SCOPE_ONELEVEL,"host=" + NewHost);
282       if len(NAttrs) != 0:
283          print "Host",NewHost,"already exists.";
284          continue;
285       NewHostName = raw_input("Hostname? ");
286       if NewHost == "":
287          continue;
288       Dn = "host=" + NewHost + "," + HBaseDn;
289       l.add_s(Dn,[("host", NewHost),
290                   ("hostname", NewHostName),
291                   ("objectClass", ("top", "debianServer"))]);
292
293       # Switch
294       NAttrs = l.search_s(HBaseDn,ldap.SCOPE_ONELEVEL,"host=" + NewHost);
295       if len(NAttrs) == 0:
296          print "Host",NewHost,"was not found.";
297          continue;
298       Attrs = NAttrs;
299       Host = NewHost;
300       HostDn = "host=" + Host + "," + HBaseDn;
301       OrderedIndex = copy.deepcopy(OrigOrderedIndex);
302       continue;
303
304    # Handle changing an arbitary value
305    if (Response == "a"):
306       Attr = raw_input("Attr? ");
307       ChangeAttr(Attrs[0],Attr);
308       continue;
309
310    if (Response == 'd'):
311       Really = raw_input("Really (type yes)? ");
312       if Really != 'yes':
313           continue;
314       print "Deleting",HostDn;
315       l.delete_s(HostDn);
316       continue;
317
318    # Convert the integer response
319    try:
320       ID = int(Response);
321       if (not OrderedIndex.has_key(ID) or (ID > 100 and RootMode == 0)):
322          raise ValueError;
323    except ValueError:
324       print "Invalid";
325       continue;
326
327    # Print the what to do prompt
328    print "Changing LDAP entry '%s' (%s)" % (OrderedIndex[ID][0],OrderedIndex[ID][2]);
329    print AttrPrompt[OrderedIndex[ID][2]][0];
330    ChangeAttr(Attrs[0],OrderedIndex[ID][2]);