3 # Generates passwd, shadow and group files from the ldap directory.
5 # Copyright (c) 2000-2001 Jason Gunthorpe <jgg@debian.org>
6 # Copyright (c) 2003-2004 James Troup <troup@debian.org>
7 # Copyright (c) 2004-2005,7 Joey Schulze <joey@infodrom.org>
8 # Copyright (c) 2001-2007 Ryan Murray <rmurray@debian.org>
9 # Copyright (c) 2008 Peter Palfrader <peter@palfrader.org>
10 # Copyright (c) 2008 Andreas Barth <aba@not.so.argh.org>
11 # Copyright (c) 2008 Mark Hymers <mhy@debian.org>
13 # This program is free software; you can redistribute it and/or modify
14 # it under the terms of the GNU General Public License as published by
15 # the Free Software Foundation; either version 2 of the License, or
16 # (at your option) any later version.
18 # This program is distributed in the hope that it will be useful,
19 # but WITHOUT ANY WARRANTY; without even the implied warranty of
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 # GNU General Public License for more details.
23 # You should have received a copy of the GNU General Public License
24 # along with this program; if not, write to the Free Software
25 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 import string, re, time, ldap, getopt, sys, os, pwd, posix, socket, base64, sha, shutil, errno, tarfile, grp
28 from userdir_ldap import *;
38 UUID_FORMAT = '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}'
40 EmailCheck = re.compile("^([^ <>@]+@[^ ,<>@]+)?$");
41 BSMTPCheck = re.compile(".*mx 0 (gluck)\.debian\.org\..*",re.DOTALL);
42 DNSZone = ".debian.net"
43 Keyrings = ConfModule.sync_keyrings.split(":")
45 def safe_makedirs(dir):
49 if e.errno == errno.EEXIST:
58 if e.errno == errno.ENOENT:
64 return Str.translate(string.maketrans("\n\r\t","$$$"))
66 def DoLink(From,To,File):
67 try: posix.remove(To+File);
69 posix.link(From+File,To+File);
71 # See if this user is in the group list
72 def IsInGroup(DnRecord):
76 # See if the primary group is in the list
77 if Allowed.has_key(GetAttr(DnRecord,"gidNumber")) != 0:
80 # Check the host based ACL
81 if DnRecord[1].has_key("allowedHost") != 0:
82 for I in DnRecord[1]["allowedHost"]:
86 # See if there are supplementary groups
87 if DnRecord[1].has_key("supplementaryGid") == 0:
90 # Check the supplementary groups
91 for I in DnRecord[1]["supplementaryGid"]:
92 if Allowed.has_key(I):
101 try: os.remove(File + ".tmp");
103 try: os.remove(File + ".tdb.tmp");
106 def Done(File,F,Fdb):
109 os.rename(File + ".tmp",File);
112 os.rename(File + ".tdb.tmp",File+".tdb");
114 # Generate the password list
115 def GenPasswd(l,File,HomePrefix,PwdMarker):
118 F = open(File + ".tdb.tmp","w");
121 # Fetch all the users
123 if PasswdAttrs == None:
127 for x in PasswdAttrs:
128 if x[1].has_key("uidNumber") == 0 or IsInGroup(x) == 0:
131 # Do not let people try to buffer overflow some busted passwd parser.
132 if len(GetAttr(x,"gecos")) > 100 or len(GetAttr(x,"loginShell")) > 50:
135 userlist[GetAttr(x, "uid")] = int(GetAttr(x, "gidNumber"))
136 Line = "%s:%s:%s:%s:%s:%s%s:%s" % (GetAttr(x,"uid"),\
138 GetAttr(x,"uidNumber"),GetAttr(x,"gidNumber"),\
139 GetAttr(x,"gecos"),HomePrefix,GetAttr(x,"uid"),\
140 GetAttr(x,"loginShell"));
142 Line = Sanitize(Line) + "\n";
143 F.write("0%u %s" % (I,Line));
144 F.write(".%s %s" % (GetAttr(x,"uid"),Line));
145 F.write("=%s %s" % (GetAttr(x,"uidNumber"),Line));
148 # Oops, something unspeakable happened.
154 # Return the list of users so we know which keys to export
157 # Generate the shadow list
158 def GenShadow(l,File):
161 OldMask = os.umask(0077);
162 F = open(File + ".tdb.tmp","w",0600);
165 # Fetch all the users
167 if PasswdAttrs == None:
171 for x in PasswdAttrs:
172 if x[1].has_key("uidNumber") == 0 or IsInGroup(x) == 0:
175 Pass = GetAttr(x,"userPassword");
176 if Pass[0:7] != "{crypt}" or len(Pass) > 50:
181 # If the account is locked, mark it as such in shadow
182 # See Debian Bug #308229 for why we set it to 1 instead of 0
183 if (GetAttr(x,"userPassword").find("*LK*") != -1) \
184 or GetAttr(x,"userPassword").startswith("!"):
187 ShadowExpire = GetAttr(x,"shadowExpire")
189 Line = "%s:%s:%s:%s:%s:%s:%s:%s:" % (GetAttr(x,"uid"),\
190 Pass,GetAttr(x,"shadowLastChange"),\
191 GetAttr(x,"shadowMin"),GetAttr(x,"shadowMax"),\
192 GetAttr(x,"shadowWarning"),GetAttr(x,"shadowInactive"),\
194 Line = Sanitize(Line) + "\n";
195 F.write("0%u %s" % (I,Line));
196 F.write(".%s %s" % (GetAttr(x,"uid"),Line));
199 # Oops, something unspeakable happened.
205 # Generate the sudo passwd file
206 def GenShadowSudo(l,File, untrusted):
209 OldMask = os.umask(0077);
210 F = open(File + ".tmp","w",0600);
213 # Fetch all the users
215 if PasswdAttrs == None:
218 for x in PasswdAttrs:
220 if x[1].has_key("uidNumber") == 0 or IsInGroup(x) == 0:
223 if x[1].has_key('sudoPassword'):
224 for entry in x[1]['sudoPassword']:
225 Match = re.compile('^('+UUID_FORMAT+') (confirmed:[0-9a-f]{40}|unconfirmed) ([a-z0-9.,*]+) ([^ ]+)$').match(entry)
228 uuid = Match.group(1)
229 status = Match.group(2)
230 hosts = Match.group(3)
231 cryptedpass = Match.group(4)
233 if status != 'confirmed:'+make_passwd_hmac('password-is-confirmed', 'sudo', x[1]['uid'][0], uuid, hosts, cryptedpass):
235 for_all = hosts == "*"
236 for_this_host = CurrentHost in hosts.split(',')
237 if not (for_all or for_this_host):
239 # ignore * passwords for untrusted hosts, but copy host specific passwords
240 if for_all and untrusted:
243 if for_this_host: # this makes sure we take a per-host entry over the for-all entry
248 Line = "%s:%s" % (GetAttr(x,"uid"), Pass)
249 Line = Sanitize(Line) + "\n";
250 F.write("%s" % (Line));
252 # Oops, something unspeakable happened.
258 # Generate the shadow list
259 def GenSSHShadow(l,masterFileName):
260 # Fetch all the users
263 # Depending on config, we write out either a single file,
264 # multiple files, or both
267 OldMask = os.umask(0077);
268 masterFile = open(masterFileName + ".tmp","w",0600);
271 Die(masterFileName,masterFile,None)
275 if PasswdAttrs == None:
278 # If we're going to be dealing with multiple keys, empty the
279 # directory before we start to avoid old keys hanging around
281 safe_rmtree(os.path.join(GlobalDir, 'userkeys'))
282 safe_makedirs(os.path.join(GlobalDir, 'userkeys'))
284 for x in PasswdAttrs:
285 # If the account is locked, do not write it.
286 # This is a partial stop-gap. The ssh also needs to change this
287 # to ignore ~/.ssh/authorized* files.
288 if (GetAttr(x,"userPassword").find("*LK*") != -1) \
289 or GetAttr(x,"userPassword").startswith("!"):
292 if x[1].has_key("uidNumber") == 0 or \
293 x[1].has_key("sshRSAAuthKey") == 0:
295 User = GetAttr(x,"uid");
300 OldMask = os.umask(0077);
301 File = os.path.join(GlobalDir, 'userkeys', User)
302 F = open(File + ".tmp","w",0600);
305 for I in x[1]["sshRSAAuthKey"]:
307 MultipleLine = "%s" % I
308 MultipleLine = Sanitize(MultipleLine) + "\n"
309 F.write(MultipleLine)
311 SingleLine = "%s: %s" % (User, I)
312 SingleLine = Sanitize(SingleLine) + "\n"
313 masterFile.write(SingleLine)
317 userfiles.append(os.path.basename(File))
319 # Oops, something unspeakable happened.
322 Die(masterFileName,masterFile,None)
326 Done(masterFileName,masterFile,None)
327 singlefile = os.path.basename(masterFileName)
329 return singlefile, userfiles
331 # Generate the group list
332 def GenGroup(l,File):
336 F = open(File + ".tdb.tmp","w");
338 # Generate the GroupMap
340 for x in GroupIDMap.keys():
343 # Fetch all the users
345 if PasswdAttrs == None:
348 # Sort them into a list of groups having a set of users
349 for x in PasswdAttrs:
350 if x[1].has_key("uidNumber") == 0 or IsInGroup(x) == 0:
352 if x[1].has_key("supplementaryGid") == 0:
355 for I in x[1]["supplementaryGid"]:
356 if GroupMap.has_key(I):
357 GroupMap[I].append(GetAttr(x,"uid"));
359 print "Group does not exist ",I,"but",GetAttr(x,"uid"),"is in it";
361 # Output the group file.
363 for x in GroupMap.keys():
364 grouprevmap[GroupIDMap[x]] = x
365 if GroupIDMap.has_key(x) == 0:
367 Line = "%s:x:%u:" % (x,GroupIDMap[x]);
369 for I in GroupMap[x]:
370 Line = Line + ("%s%s" % (Comma,I));
372 Line = Sanitize(Line) + "\n";
373 F.write("0%u %s" % (J,Line));
374 F.write(".%s %s" % (x,Line));
375 F.write("=%u %s" % (GroupIDMap[x],Line));
378 # Oops, something unspeakable happened.
386 # Generate the email forwarding list
387 def GenForward(l,File):
390 OldMask = os.umask(0022);
391 F = open(File + ".tmp","w",0644);
394 # Fetch all the users
396 if PasswdAttrs == None:
399 # Write out the email address for each user
400 for x in PasswdAttrs:
401 if x[1].has_key("emailForward") == 0 or IsInGroup(x) == 0:
404 # Do not allow people to try to buffer overflow busted parsers
405 if len(GetAttr(x,"emailForward")) > 200:
408 # Check the forwarding address
409 if EmailCheck.match(GetAttr(x,"emailForward")) == None:
411 Line = "%s: %s" % (GetAttr(x,"uid"),GetAttr(x,"emailForward"));
412 Line = Sanitize(Line) + "\n";
415 # Oops, something unspeakable happened.
421 def GenAllForward(l,File):
424 OldMask = os.umask(0022);
425 Fdb = os.popen("cdbmake %s %s.tmp"%(File,File),"w");
428 # Fetch all the users
430 if PasswdAttrs == None:
433 # Write out the email address for each user
434 for x in PasswdAttrs:
435 if x[1].has_key("emailForward") == 0:
438 # Do not allow people to try to buffer overflow busted parsers
439 Forward = GetAttr(x,"emailForward");
440 if len(Forward) > 200:
443 # Check the forwarding address
444 if EmailCheck.match(Forward) == None:
447 User = GetAttr(x,"uid");
448 Fdb.write("+%d,%d:%s->%s\n"%(len(User),len(Forward),User,Forward));
450 # Oops, something unspeakable happened.
454 if Fdb.close() != None:
455 raise "cdbmake gave an error";
457 # Generate the anon XEarth marker file
458 def GenMarkers(l,File):
461 F = open(File + ".tmp","w");
463 # Fetch all the users
465 if PasswdAttrs == None:
468 # Write out the position for each user
469 for x in PasswdAttrs:
470 if x[1].has_key("latitude") == 0 or x[1].has_key("longitude") == 0:
473 Line = "%8s %8s \"\""%(DecDegree(GetAttr(x,"latitude"),1),DecDegree(GetAttr(x,"longitude"),1));
474 Line = Sanitize(Line) + "\n";
479 # Oops, something unspeakable happened.
485 # Generate the debian-private subscription list
486 def GenPrivate(l,File):
489 F = open(File + ".tmp","w");
491 # Fetch all the users
493 if PasswdAttrs == None:
496 # Write out the position for each user
497 for x in PasswdAttrs:
498 if x[1].has_key("privateSub") == 0:
501 # If the account is locked, do not write it
502 if (GetAttr(x,"userPassword").find("*LK*") != -1) \
503 or GetAttr(x,"userPassword").startswith("!"):
506 # If the account has no PGP key, do not write it
507 if x[1].has_key("keyFingerPrint") == 0:
510 # Must be in the Debian group (yuk, hard coded for now)
511 if GetAttr(x,"gidNumber") != "800":
515 Line = "%s"%(GetAttr(x,"privateSub"));
516 Line = Sanitize(Line) + "\n";
521 # Oops, something unspeakable happened.
527 # Generate a list of locked accounts
528 def GenDisabledAccounts(l,File):
531 F = open(File + ".tmp","w");
533 # Fetch all the users
535 if PasswdAttrs == None:
539 for x in PasswdAttrs:
540 if x[1].has_key("uidNumber") == 0:
543 Pass = GetAttr(x,"userPassword");
545 # *LK* is the reference value for a locked account
546 # password starting with ! is also a locked account
547 if Pass.find("*LK*") != -1 or Pass.startswith("!"):
548 # Format is <login>:<reason>
549 Line = "%s:%s" % (GetAttr(x,"uid"), "Account is locked")
552 F.write(Sanitize(Line) + "\n")
554 # Oops, something unspeakable happened.
560 # Generate the list of local addresses that refuse all mail
561 def GenMailDisable(l,File):
564 F = open(File + ".tmp","w");
566 # Fetch all the users
568 if PasswdAttrs == None:
571 for x in PasswdAttrs:
574 if x[1].has_key("mailDisableMessage"):
575 Reason = GetAttr(x,"mailDisableMessage")
579 # Must be in the Debian group (yuk, hard coded for now)
580 if GetAttr(x,"gidNumber") != "800":
584 Line = "%s: %s"%(GetAttr(x,"uid"),Reason);
585 Line = Sanitize(Line) + "\n";
590 # Oops, something unspeakable happened.
596 # Generate a list of uids that should have boolean affects applied
597 def GenMailBool(l,File,Key):
600 F = open(File + ".tmp","w");
602 # Fetch all the users
604 if PasswdAttrs == None:
607 for x in PasswdAttrs:
610 if x[1].has_key(Key) == 0:
613 # Must be in the Debian group (yuk, hard coded for now)
614 if GetAttr(x,"gidNumber") != "800":
617 if GetAttr(x,Key) != "TRUE":
621 Line = "%s"%(GetAttr(x,"uid"));
622 Line = Sanitize(Line) + "\n";
627 # Oops, something unspeakable happened.
633 # Generate a list of hosts for RBL or whitelist purposes.
634 def GenMailList(l,File,Key):
637 F = open(File + ".tmp","w");
639 # Fetch all the users
641 if PasswdAttrs == None:
644 for x in PasswdAttrs:
647 if x[1].has_key(Key) == 0:
650 # Must be in the Debian group (yuk, hard coded for now)
651 if GetAttr(x,"gidNumber") != "800":
658 if Key == "mailWhitelist":
659 if re.match('^[-\w.]+(/[\d]+)?$',z) == None:
662 if re.match('^[-\w.]+$',z) == None:
666 Line = GetAttr(x,"uid")
670 if Key == "mailRHSBL":
671 Line += "/$sender_address_domain"
674 Line = Sanitize(Line) + "\n";
679 # Oops, something unspeakable happened.
685 # Generate the DNS Zone file
686 def GenDNS(l,File,HomePrefix):
689 F = open(File + ".tmp","w");
691 # Fetch all the users
693 if PasswdAttrs == None:
696 # Write out the zone file entry for each user
697 for x in PasswdAttrs:
698 if x[1].has_key("dnsZoneEntry") == 0:
701 # If the account has no PGP key, do not write it
702 if x[1].has_key("keyFingerPrint") == 0:
705 F.write("; %s\n"%(EmailAddress(x)));
706 for z in x[1]["dnsZoneEntry"]:
707 Split = z.lower().split()
708 if Split[1].lower() == 'in':
709 for y in range(0,len(Split)):
712 Line = " ".join(Split) + "\n";
715 Host = Split[0] + DNSZone;
716 if BSMTPCheck.match(Line) != None:
717 F.write("; Has BSMTP\n");
719 # Write some identification information
720 if Split[2].lower() == "a":
721 Line = "%s IN TXT \"%s\"\n"%(Split[0],EmailAddress(x));
722 for y in x[1]["keyFingerPrint"]:
723 Line = Line + "%s IN TXT \"PGP %s\"\n"%(Split[0],FormatPGPKey(y));
726 Line = "; Err %s"%(str(Split));
731 F.write("; Errors\n");
734 # Oops, something unspeakable happened.
740 # Generate the DNS SSHFP records
741 def GenSSHFP(l,File,HomePrefix):
744 F = open(File + ".tmp","w")
746 # Fetch all the hosts
748 if HostAttrs == None:
752 if x[1].has_key("hostname") == 0 or \
753 x[1].has_key("sshRSAHostKey") == 0:
755 Host = GetAttr(x,"hostname");
757 for I in x[1]["sshRSAHostKey"]:
759 if Split[0] == 'ssh-rsa':
761 if Split[0] == 'ssh-dss':
763 if Algorithm == None:
765 Fingerprint = sha.new(base64.decodestring(Split[1])).hexdigest()
766 Line = "%s. IN SSHFP %u 1 %s" % (Host,Algorithm,Fingerprint)
767 Line = Sanitize(Line) + "\n"
769 # Oops, something unspeakable happened.
775 # Generate the BSMTP file
776 def GenBSMTP(l,File,HomePrefix):
779 F = open(File + ".tmp","w");
781 # Fetch all the users
783 if PasswdAttrs == None:
786 # Write out the zone file entry for each user
787 for x in PasswdAttrs:
788 if x[1].has_key("dnsZoneEntry") == 0:
791 # If the account has no PGP key, do not write it
792 if x[1].has_key("keyFingerPrint") == 0:
795 for z in x[1]["dnsZoneEntry"]:
796 Split = z.lower().split()
797 if Split[1].lower() == 'in':
798 for y in range(0,len(Split)):
801 Line = " ".join(Split) + "\n";
803 Host = Split[0] + DNSZone;
804 if BSMTPCheck.match(Line) != None:
805 F.write("%s: user=%s group=Debian file=%s%s/bsmtp/%s\n"%(Host,
806 GetAttr(x,"uid"),HomePrefix,GetAttr(x,"uid"),Host));
809 F.write("; Errors\n");
812 # Oops, something unspeakable happened.
822 if not Host in HostToIPCache:
825 IPAdressesT = list(set([ (a[0],a[4][0]) for a in socket.getaddrinfo(Host, None)]))
826 except socket.gaierror, (code):
827 if code[0] != -2: raise
829 for addr in IPAdressesT:
830 if addr[0] == socket.AF_INET: IPAdresses += [addr[1], "::ffff:"+addr[1]]
831 else: IPAdresses += [addr[1]]
832 HostToIPCache[Host] = IPAdresses
833 return HostToIPCache[Host]
836 # Generate the ssh known hosts file
837 def GenSSHKnown(l,File,mode=None):
840 OldMask = os.umask(0022);
841 F = open(File + ".tmp","w",0644);
845 if HostAttrs == None:
849 if x[1].has_key("hostname") == 0 or \
850 x[1].has_key("sshRSAHostKey") == 0:
852 Host = GetAttr(x,"hostname");
854 SHost = Host.find(".")
855 if SHost != None: HostNames += [Host[0:SHost]]
857 for I in x[1]["sshRSAHostKey"]:
858 if mode and mode == 'authorized_keys':
859 #Line = 'command="rsync --server --sender -pr . /var/cache/userdir-ldap/hosts/%s",no-port-forwarding,no-X11-forwarding,no-agent-forwarding,from="%s" %s' % (Host, ",".join(HNames + HostToIP(Host)), I)
860 Line = 'command="rsync --server --sender -pr . /var/cache/userdir-ldap/hosts/%s",no-port-forwarding,no-X11-forwarding,no-agent-forwarding %s' % (Host,I)
862 Line = "%s %s" %(",".join(HostNames + HostToIP(Host)), I);
863 Line = Sanitize(Line) + "\n";
865 # Oops, something unspeakable happened.
871 # Generate the debianhosts file (list of all IP addresses)
872 def GenHosts(l,File):
875 OldMask = os.umask(0022);
876 F = open(File + ".tmp","w",0644);
879 # Fetch all the hosts
880 HostNames = l.search_s(HostBaseDn,ldap.SCOPE_ONELEVEL,"hostname=*",\
883 if HostNames == None:
887 if x[1].has_key("hostname") == 0:
889 Host = GetAttr(x,"hostname");
891 Addr = socket.gethostbyname(Host);
892 F.write(Addr + "\n");
895 # Oops, something unspeakable happened.
901 def GenKeyrings(l,OutDir):
903 shutil.copy(k, OutDir)
905 # Connect to the ldap server
907 F = open(PassDir+"/pass-"+pwd.getpwuid(os.getuid())[0],"r");
908 Pass = F.readline().strip().split(" ")
910 l.simple_bind_s("uid="+Pass[0]+","+BaseDn,Pass[1]);
912 # Fetch all the groups
914 Attrs = l.search_s(BaseDn,ldap.SCOPE_ONELEVEL,"gid=*",\
915 ["gid","gidNumber"]);
917 # Generate the GroupMap and GroupIDMap
919 if x[1].has_key("gidNumber") == 0:
921 GroupIDMap[x[1]["gid"][0]] = int(x[1]["gidNumber"][0]);
923 # Fetch all the users
924 PasswdAttrs = l.search_s(BaseDn,ldap.SCOPE_ONELEVEL,"uid=*",\
925 ["uid","uidNumber","gidNumber","supplementaryGid",\
926 "gecos","loginShell","userPassword","shadowLastChange",\
927 "shadowMin","shadowMax","shadowWarning","shadowInactive",
928 "shadowExpire","emailForward","latitude","longitude",\
929 "allowedHost","sshRSAAuthKey","dnsZoneEntry","cn","sn",\
930 "keyFingerPrint","privateSub","mailDisableMessage",\
931 "mailGreylisting","mailCallout","mailRBL","mailRHSBL",\
932 "mailWhitelist", "sudoPassword"]);
933 # Fetch all the hosts
934 HostAttrs = l.search_s(HostBaseDn,ldap.SCOPE_ONELEVEL,"sshRSAHostKey=*",\
935 ["hostname","sshRSAHostKey"]);
937 # Open the control file
938 if len(sys.argv) == 1:
939 F = open(GenerateConf,"r");
941 F = open(sys.argv[1],"r")
943 # Generate global things
944 GlobalDir = GenerateDir+"/";
945 SSHGlobal, SSHFiles = GenSSHShadow(l,GlobalDir+"ssh-rsa-shadow");
946 GenAllForward(l,GlobalDir+"mail-forward.cdb");
947 GenMarkers(l,GlobalDir+"markers");
948 GenPrivate(l,GlobalDir+"debian-private");
949 GenDisabledAccounts(l,GlobalDir+"disabled-accounts");
950 GenSSHKnown(l,GlobalDir+"ssh_known_hosts");
951 #GenSSHKnown(l,GlobalDir+"authorized_keys", 'authorized_keys');
952 GenHosts(l,GlobalDir+"debianhosts");
953 GenMailDisable(l,GlobalDir+"mail-disable");
954 GenMailBool(l,GlobalDir+"mail-greylist","mailGreylisting");
955 GenMailBool(l,GlobalDir+"mail-callout","mailCallout");
956 GenMailList(l,GlobalDir+"mail-rbl","mailRBL");
957 GenMailList(l,GlobalDir+"mail-rhsbl","mailRHSBL");
958 GenMailList(l,GlobalDir+"mail-whitelist","mailWhitelist");
959 GenKeyrings(l,GlobalDir);
962 GenForward(l,GlobalDir+"forward-alias");
974 Split = Line.split(" ")
975 OutDir = GenerateDir + '/' + Split[0] + '/';
976 try: os.mkdir(OutDir);
979 # Get the group list and convert any named groups to numerics
987 if GroupIDMap.has_key(I):
988 GroupList[str(GroupIDMap[I])] = None;
993 CurrentHost = Split[0];
995 # If we're using a single SSH file, deal with it
996 if SSHGlobal is not None:
997 DoLink(GlobalDir, OutDir, SSHGlobal)
999 DoLink(GlobalDir,OutDir,"debianhosts");
1000 DoLink(GlobalDir,OutDir,"ssh_known_hosts");
1001 DoLink(GlobalDir,OutDir,"disabled-accounts")
1004 if ExtraList.has_key("[NOPASSWD]"):
1005 userlist = GenPasswd(l,OutDir+"passwd",Split[1], "*");
1007 userlist = GenPasswd(l,OutDir+"passwd",Split[1], "x");
1009 grouprevmap = GenGroup(l,OutDir+"group");
1010 GenShadowSudo(l, OutDir+"sudo-passwd", ExtraList.has_key("[UNTRUSTED]") or ExtraList.has_key("[NOPASSWD]"))
1012 # Now we know who we're allowing on the machine, export
1013 # the relevant ssh keys
1014 if MultipleSSHFiles:
1015 OldMask = os.umask(0077);
1016 tf = tarfile.open(name=os.path.join(GlobalDir, 'ssh-keys-%s.tar.gz' % CurrentHost), mode='w:gz')
1018 for f in userlist.keys():
1019 if f not in SSHFiles:
1021 # If we're not exporting their primary group, don't export
1024 if userlist[f] in grouprevmap.keys():
1025 grname = grouprevmap[userlist[f]]
1028 if int(userlist[f]) <= 100:
1029 # In these cases, look it up in the normal way so we
1030 # deal with cases where, for instance, users are in group
1031 # users as their primary group.
1032 grname = grp.getgrgid(userlist[f])[0]
1033 except Exception, e:
1037 print "User %s is supposed to have their key exported to host %s but their primary group (gid: %d) isn't in LDAP" % (f, CurrentHost, userlist[f])
1040 to = tf.gettarinfo(os.path.join(GlobalDir, 'userkeys', f), f)
1041 # These will only be used where the username doesn't
1042 # exist on the target system for some reason; hence,
1043 # in those cases, the safest thing is for the file to
1044 # be owned by root but group nobody. This deals with
1045 # the bloody obscure case where the group fails to exist
1046 # whilst the user does (in which case we want to avoid
1047 # ending up with a file which is owned user:root to avoid
1048 # a fairly obvious attack vector)
1051 # Using the username / groupname fields avoids any need
1052 # to give a shit^W^W^Wcare about the UIDoffset stuff.
1056 tf.addfile(to, file(os.path.join(GlobalDir, 'userkeys', f)))
1059 os.rename(os.path.join(GlobalDir, 'ssh-keys-%s.tar.gz' % CurrentHost),
1060 os.path.join(OutDir, 'ssh-keys.tar.gz'))
1062 if ExtraList.has_key("[UNTRUSTED]"):
1063 print "[UNTRUSTED] tag is obsolete and may be removed in the future."
1065 if not ExtraList.has_key("[NOPASSWD]"):
1066 GenShadow(l,OutDir+"shadow");
1068 # Link in global things
1069 if not ExtraList.has_key("[NOMARKERS]"):
1070 DoLink(GlobalDir,OutDir,"markers");
1071 DoLink(GlobalDir,OutDir,"mail-forward.cdb");
1072 DoLink(GlobalDir,OutDir,"mail-disable");
1073 DoLink(GlobalDir,OutDir,"mail-greylist");
1074 DoLink(GlobalDir,OutDir,"mail-callout");
1075 DoLink(GlobalDir,OutDir,"mail-rbl");
1076 DoLink(GlobalDir,OutDir,"mail-rhsbl");
1077 DoLink(GlobalDir,OutDir,"mail-whitelist");
1080 DoLink(GlobalDir,OutDir,"forward-alias");
1082 if ExtraList.has_key("[DNS]"):
1083 GenDNS(l,OutDir+"dns-zone",Split[1]);
1084 GenSSHFP(l,OutDir+"dns-sshfp",Split[1])
1086 if ExtraList.has_key("[BSMTP]"):
1087 GenBSMTP(l,OutDir+"bsmtp",Split[1])
1089 if ExtraList.has_key("[PRIVATE]"):
1090 DoLink(GlobalDir,OutDir,"debian-private")
1092 if ExtraList.has_key("[KEYRING]"):
1094 DoLink(GlobalDir,OutDir,os.path.basename(k))
1097 try: posix.remove(OutDir+os.path.basename(k));
1102 # vim:set shiftwidth=3: