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):
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:
219 if x[1].has_key("uidNumber") == 0 or IsInGroup(x) == 0:
221 if not x[1].has_key('sudoPassword'):
225 for entry in x[1]['sudoPassword']:
226 Match = re.compile('^('+UUID_FORMAT+') (confirmed|unconfirmed) ([a-z0-9.,*]+) ([^ ]+)$').match(entry.lower())
229 status = Match.group(2)
230 hosts = Match.group(3)
231 cryptedpass = Match.group(4)
233 if status != 'confirmed':
235 for_all = hosts == "*"
236 for_this_host = CurrentHost in hosts.split(',')
237 if not (for_all or for_this_host):
240 if for_this_host: # this makes sure we take a per-host entry over the for-all entry
247 Line = "%s:%s" % (GetAttr(x,"uid"), Pass)
248 Line = Sanitize(Line) + "\n";
249 F.write("%s" % (Line));
251 # Oops, something unspeakable happened.
257 # Generate the shadow list
258 def GenSSHShadow(l,masterFileName):
259 # Fetch all the users
262 # Depending on config, we write out either a single file,
263 # multiple files, or both
266 OldMask = os.umask(0077);
267 masterFile = open(masterFileName + ".tmp","w",0600);
270 Die(masterFileName,masterFile,None)
274 if PasswdAttrs == None:
277 # If we're going to be dealing with multiple keys, empty the
278 # directory before we start to avoid old keys hanging around
280 safe_rmtree(os.path.join(GlobalDir, 'userkeys'))
281 safe_makedirs(os.path.join(GlobalDir, 'userkeys'))
283 for x in PasswdAttrs:
284 # If the account is locked, do not write it.
285 # This is a partial stop-gap. The ssh also needs to change this
286 # to ignore ~/.ssh/authorized* files.
287 if (GetAttr(x,"userPassword").find("*LK*") != -1) \
288 or GetAttr(x,"userPassword").startswith("!"):
291 if x[1].has_key("uidNumber") == 0 or \
292 x[1].has_key("sshRSAAuthKey") == 0:
294 User = GetAttr(x,"uid");
299 OldMask = os.umask(0077);
300 File = os.path.join(GlobalDir, 'userkeys', User)
301 F = open(File + ".tmp","w",0600);
304 for I in x[1]["sshRSAAuthKey"]:
306 MultipleLine = "%s" % I
307 MultipleLine = Sanitize(MultipleLine) + "\n"
308 F.write(MultipleLine)
310 SingleLine = "%s: %s" % (User, I)
311 SingleLine = Sanitize(SingleLine) + "\n"
312 masterFile.write(SingleLine)
316 userfiles.append(os.path.basename(File))
318 # Oops, something unspeakable happened.
321 Die(masterFileName,masterFile,None)
325 Done(masterFileName,masterFile,None)
326 singlefile = os.path.basename(masterFileName)
328 return singlefile, userfiles
330 # Generate the group list
331 def GenGroup(l,File):
335 F = open(File + ".tdb.tmp","w");
337 # Generate the GroupMap
339 for x in GroupIDMap.keys():
342 # Fetch all the users
344 if PasswdAttrs == None:
347 # Sort them into a list of groups having a set of users
348 for x in PasswdAttrs:
349 if x[1].has_key("uidNumber") == 0 or IsInGroup(x) == 0:
351 if x[1].has_key("supplementaryGid") == 0:
354 for I in x[1]["supplementaryGid"]:
355 if GroupMap.has_key(I):
356 GroupMap[I].append(GetAttr(x,"uid"));
358 print "Group does not exist ",I,"but",GetAttr(x,"uid"),"is in it";
360 # Output the group file.
362 for x in GroupMap.keys():
363 grouprevmap[GroupIDMap[x]] = x
364 if GroupIDMap.has_key(x) == 0:
366 Line = "%s:x:%u:" % (x,GroupIDMap[x]);
368 for I in GroupMap[x]:
369 Line = Line + ("%s%s" % (Comma,I));
371 Line = Sanitize(Line) + "\n";
372 F.write("0%u %s" % (J,Line));
373 F.write(".%s %s" % (x,Line));
374 F.write("=%u %s" % (GroupIDMap[x],Line));
377 # Oops, something unspeakable happened.
385 # Generate the email forwarding list
386 def GenForward(l,File):
389 OldMask = os.umask(0022);
390 F = open(File + ".tmp","w",0644);
393 # Fetch all the users
395 if PasswdAttrs == None:
398 # Write out the email address for each user
399 for x in PasswdAttrs:
400 if x[1].has_key("emailForward") == 0 or IsInGroup(x) == 0:
403 # Do not allow people to try to buffer overflow busted parsers
404 if len(GetAttr(x,"emailForward")) > 200:
407 # Check the forwarding address
408 if EmailCheck.match(GetAttr(x,"emailForward")) == None:
410 Line = "%s: %s" % (GetAttr(x,"uid"),GetAttr(x,"emailForward"));
411 Line = Sanitize(Line) + "\n";
414 # Oops, something unspeakable happened.
420 def GenAllForward(l,File):
423 OldMask = os.umask(0022);
424 Fdb = os.popen("cdbmake %s %s.tmp"%(File,File),"w");
427 # Fetch all the users
429 if PasswdAttrs == None:
432 # Write out the email address for each user
433 for x in PasswdAttrs:
434 if x[1].has_key("emailForward") == 0:
437 # Do not allow people to try to buffer overflow busted parsers
438 Forward = GetAttr(x,"emailForward");
439 if len(Forward) > 200:
442 # Check the forwarding address
443 if EmailCheck.match(Forward) == None:
446 User = GetAttr(x,"uid");
447 Fdb.write("+%d,%d:%s->%s\n"%(len(User),len(Forward),User,Forward));
449 # Oops, something unspeakable happened.
453 if Fdb.close() != None:
454 raise "cdbmake gave an error";
456 # Generate the anon XEarth marker file
457 def GenMarkers(l,File):
460 F = open(File + ".tmp","w");
462 # Fetch all the users
464 if PasswdAttrs == None:
467 # Write out the position for each user
468 for x in PasswdAttrs:
469 if x[1].has_key("latitude") == 0 or x[1].has_key("longitude") == 0:
472 Line = "%8s %8s \"\""%(DecDegree(GetAttr(x,"latitude"),1),DecDegree(GetAttr(x,"longitude"),1));
473 Line = Sanitize(Line) + "\n";
478 # Oops, something unspeakable happened.
484 # Generate the debian-private subscription list
485 def GenPrivate(l,File):
488 F = open(File + ".tmp","w");
490 # Fetch all the users
492 if PasswdAttrs == None:
495 # Write out the position for each user
496 for x in PasswdAttrs:
497 if x[1].has_key("privateSub") == 0:
500 # If the account is locked, do not write it
501 if (GetAttr(x,"userPassword").find("*LK*") != -1) \
502 or GetAttr(x,"userPassword").startswith("!"):
505 # If the account has no PGP key, do not write it
506 if x[1].has_key("keyFingerPrint") == 0:
509 # Must be in the Debian group (yuk, hard coded for now)
510 if GetAttr(x,"gidNumber") != "800":
514 Line = "%s"%(GetAttr(x,"privateSub"));
515 Line = Sanitize(Line) + "\n";
520 # Oops, something unspeakable happened.
526 # Generate a list of locked accounts
527 def GenDisabledAccounts(l,File):
530 F = open(File + ".tmp","w");
532 # Fetch all the users
534 if PasswdAttrs == None:
538 for x in PasswdAttrs:
539 if x[1].has_key("uidNumber") == 0:
542 Pass = GetAttr(x,"userPassword");
544 # *LK* is the reference value for a locked account
545 # password starting with ! is also a locked account
546 if Pass.find("*LK*") != -1 or Pass.startswith("!"):
547 # Format is <login>:<reason>
548 Line = "%s:%s" % (GetAttr(x,"uid"), "Account is locked")
551 F.write(Sanitize(Line) + "\n")
553 # Oops, something unspeakable happened.
559 # Generate the list of local addresses that refuse all mail
560 def GenMailDisable(l,File):
563 F = open(File + ".tmp","w");
565 # Fetch all the users
567 if PasswdAttrs == None:
570 for x in PasswdAttrs:
573 if x[1].has_key("mailDisableMessage"):
574 Reason = GetAttr(x,"mailDisableMessage")
578 # Must be in the Debian group (yuk, hard coded for now)
579 if GetAttr(x,"gidNumber") != "800":
583 Line = "%s: %s"%(GetAttr(x,"uid"),Reason);
584 Line = Sanitize(Line) + "\n";
589 # Oops, something unspeakable happened.
595 # Generate a list of uids that should have boolean affects applied
596 def GenMailBool(l,File,Key):
599 F = open(File + ".tmp","w");
601 # Fetch all the users
603 if PasswdAttrs == None:
606 for x in PasswdAttrs:
609 if x[1].has_key(Key) == 0:
612 # Must be in the Debian group (yuk, hard coded for now)
613 if GetAttr(x,"gidNumber") != "800":
616 if GetAttr(x,Key) != "TRUE":
620 Line = "%s"%(GetAttr(x,"uid"));
621 Line = Sanitize(Line) + "\n";
626 # Oops, something unspeakable happened.
632 # Generate a list of hosts for RBL or whitelist purposes.
633 def GenMailList(l,File,Key):
636 F = open(File + ".tmp","w");
638 # Fetch all the users
640 if PasswdAttrs == None:
643 for x in PasswdAttrs:
646 if x[1].has_key(Key) == 0:
649 # Must be in the Debian group (yuk, hard coded for now)
650 if GetAttr(x,"gidNumber") != "800":
657 if Key == "mailWhitelist":
658 if re.match('^[-\w.]+(/[\d]+)?$',z) == None:
661 if re.match('^[-\w.]+$',z) == None:
665 Line = GetAttr(x,"uid")
669 if Key == "mailRHSBL":
670 Line += "/$sender_address_domain"
673 Line = Sanitize(Line) + "\n";
678 # Oops, something unspeakable happened.
684 # Generate the DNS Zone file
685 def GenDNS(l,File,HomePrefix):
688 F = open(File + ".tmp","w");
690 # Fetch all the users
692 if PasswdAttrs == None:
695 # Write out the zone file entry for each user
696 for x in PasswdAttrs:
697 if x[1].has_key("dnsZoneEntry") == 0:
700 # If the account has no PGP key, do not write it
701 if x[1].has_key("keyFingerPrint") == 0:
704 F.write("; %s\n"%(EmailAddress(x)));
705 for z in x[1]["dnsZoneEntry"]:
706 Split = z.lower().split()
707 if Split[1].lower() == 'in':
708 for y in range(0,len(Split)):
711 Line = " ".join(Split) + "\n";
714 Host = Split[0] + DNSZone;
715 if BSMTPCheck.match(Line) != None:
716 F.write("; Has BSMTP\n");
718 # Write some identification information
719 if Split[2].lower() == "a":
720 Line = "%s IN TXT \"%s\"\n"%(Split[0],EmailAddress(x));
721 for y in x[1]["keyFingerPrint"]:
722 Line = Line + "%s IN TXT \"PGP %s\"\n"%(Split[0],FormatPGPKey(y));
725 Line = "; Err %s"%(str(Split));
730 F.write("; Errors\n");
733 # Oops, something unspeakable happened.
739 # Generate the DNS SSHFP records
740 def GenSSHFP(l,File,HomePrefix):
743 F = open(File + ".tmp","w")
745 # Fetch all the hosts
747 if HostAttrs == None:
751 if x[1].has_key("hostname") == 0 or \
752 x[1].has_key("sshRSAHostKey") == 0:
754 Host = GetAttr(x,"hostname");
756 for I in x[1]["sshRSAHostKey"]:
758 if Split[0] == 'ssh-rsa':
760 if Split[0] == 'ssh-dss':
762 if Algorithm == None:
764 Fingerprint = sha.new(base64.decodestring(Split[1])).hexdigest()
765 Line = "%s. IN SSHFP %u 1 %s" % (Host,Algorithm,Fingerprint)
766 Line = Sanitize(Line) + "\n"
768 # Oops, something unspeakable happened.
774 # Generate the BSMTP file
775 def GenBSMTP(l,File,HomePrefix):
778 F = open(File + ".tmp","w");
780 # Fetch all the users
782 if PasswdAttrs == None:
785 # Write out the zone file entry for each user
786 for x in PasswdAttrs:
787 if x[1].has_key("dnsZoneEntry") == 0:
790 # If the account has no PGP key, do not write it
791 if x[1].has_key("keyFingerPrint") == 0:
794 for z in x[1]["dnsZoneEntry"]:
795 Split = z.lower().split()
796 if Split[1].lower() == 'in':
797 for y in range(0,len(Split)):
800 Line = " ".join(Split) + "\n";
802 Host = Split[0] + DNSZone;
803 if BSMTPCheck.match(Line) != None:
804 F.write("%s: user=%s group=Debian file=%s%s/bsmtp/%s\n"%(Host,
805 GetAttr(x,"uid"),HomePrefix,GetAttr(x,"uid"),Host));
808 F.write("; Errors\n");
811 # Oops, something unspeakable happened.
821 if not Host in HostToIPCache:
824 IPAdressesT = list(set([ (a[0],a[4][0]) for a in socket.getaddrinfo(Host, None)]))
825 except socket.gaierror, (code):
826 if code[0] != -2: raise
828 for addr in IPAdressesT:
829 if addr[0] == socket.AF_INET: IPAdresses += [addr[1], "::ffff:"+addr[1]]
830 else: IPAdresses += [addr[1]]
831 HostToIPCache[Host] = IPAdresses
832 return HostToIPCache[Host]
835 # Generate the ssh known hosts file
836 def GenSSHKnown(l,File,mode=None):
839 OldMask = os.umask(0022);
840 F = open(File + ".tmp","w",0644);
844 if HostAttrs == None:
848 if x[1].has_key("hostname") == 0 or \
849 x[1].has_key("sshRSAHostKey") == 0:
851 Host = GetAttr(x,"hostname");
853 SHost = Host.find(".")
854 if SHost != None: HostNames += [Host[0:SHost]]
856 for I in x[1]["sshRSAHostKey"]:
857 if mode and mode == 'authorized_keys':
858 #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)
859 Line = 'command="rsync --server --sender -pr . /var/cache/userdir-ldap/hosts/%s",no-port-forwarding,no-X11-forwarding,no-agent-forwarding %s' % (Host,I)
861 Line = "%s %s" %(",".join(HostNames + HostToIP(Host)), I);
862 Line = Sanitize(Line) + "\n";
864 # Oops, something unspeakable happened.
870 # Generate the debianhosts file (list of all IP addresses)
871 def GenHosts(l,File):
874 OldMask = os.umask(0022);
875 F = open(File + ".tmp","w",0644);
878 # Fetch all the hosts
879 HostNames = l.search_s(HostBaseDn,ldap.SCOPE_ONELEVEL,"hostname=*",\
882 if HostNames == None:
886 if x[1].has_key("hostname") == 0:
888 Host = GetAttr(x,"hostname");
890 Addr = socket.gethostbyname(Host);
891 F.write(Addr + "\n");
894 # Oops, something unspeakable happened.
900 def GenKeyrings(l,OutDir):
902 shutil.copy(k, OutDir)
904 # Connect to the ldap server
906 F = open(PassDir+"/pass-"+pwd.getpwuid(os.getuid())[0],"r");
907 Pass = F.readline().strip().split(" ")
909 l.simple_bind_s("uid="+Pass[0]+","+BaseDn,Pass[1]);
911 # Fetch all the groups
913 Attrs = l.search_s(BaseDn,ldap.SCOPE_ONELEVEL,"gid=*",\
914 ["gid","gidNumber"]);
916 # Generate the GroupMap and GroupIDMap
918 if x[1].has_key("gidNumber") == 0:
920 GroupIDMap[x[1]["gid"][0]] = int(x[1]["gidNumber"][0]);
922 # Fetch all the users
923 PasswdAttrs = l.search_s(BaseDn,ldap.SCOPE_ONELEVEL,"uid=*",\
924 ["uid","uidNumber","gidNumber","supplementaryGid",\
925 "gecos","loginShell","userPassword","shadowLastChange",\
926 "shadowMin","shadowMax","shadowWarning","shadowInactive",
927 "shadowExpire","emailForward","latitude","longitude",\
928 "allowedHost","sshRSAAuthKey","dnsZoneEntry","cn","sn",\
929 "keyFingerPrint","privateSub","mailDisableMessage",\
930 "mailGreylisting","mailCallout","mailRBL","mailRHSBL",\
931 "mailWhitelist", "sudoPassword"]);
932 # Fetch all the hosts
933 HostAttrs = l.search_s(HostBaseDn,ldap.SCOPE_ONELEVEL,"sshRSAHostKey=*",\
934 ["hostname","sshRSAHostKey"]);
936 # Open the control file
937 if len(sys.argv) == 1:
938 F = open(GenerateConf,"r");
940 F = open(sys.argv[1],"r")
942 # Generate global things
943 GlobalDir = GenerateDir+"/";
944 SSHGlobal, SSHFiles = GenSSHShadow(l,GlobalDir+"ssh-rsa-shadow");
945 GenAllForward(l,GlobalDir+"mail-forward.cdb");
946 GenMarkers(l,GlobalDir+"markers");
947 GenPrivate(l,GlobalDir+"debian-private");
948 GenDisabledAccounts(l,GlobalDir+"disabled-accounts");
949 GenSSHKnown(l,GlobalDir+"ssh_known_hosts");
950 #GenSSHKnown(l,GlobalDir+"authorized_keys", 'authorized_keys');
951 GenHosts(l,GlobalDir+"debianhosts");
952 GenMailDisable(l,GlobalDir+"mail-disable");
953 GenMailBool(l,GlobalDir+"mail-greylist","mailGreylisting");
954 GenMailBool(l,GlobalDir+"mail-callout","mailCallout");
955 GenMailList(l,GlobalDir+"mail-rbl","mailRBL");
956 GenMailList(l,GlobalDir+"mail-rhsbl","mailRHSBL");
957 GenMailList(l,GlobalDir+"mail-whitelist","mailWhitelist");
958 GenKeyrings(l,GlobalDir);
961 GenForward(l,GlobalDir+"forward-alias");
973 Split = Line.split(" ")
974 OutDir = GenerateDir + '/' + Split[0] + '/';
975 try: os.mkdir(OutDir);
978 # Get the group list and convert any named groups to numerics
986 if GroupIDMap.has_key(I):
987 GroupList[str(GroupIDMap[I])] = None;
992 CurrentHost = Split[0];
994 # If we're using a single SSH file, deal with it
995 if SSHGlobal is not None:
996 DoLink(GlobalDir, OutDir, SSHGlobal)
998 DoLink(GlobalDir,OutDir,"debianhosts");
999 DoLink(GlobalDir,OutDir,"ssh_known_hosts");
1000 DoLink(GlobalDir,OutDir,"disabled-accounts")
1003 if ExtraList.has_key("[NOPASSWD]"):
1004 userlist = GenPasswd(l,OutDir+"passwd",Split[1], "*");
1006 userlist = GenPasswd(l,OutDir+"passwd",Split[1], "x");
1008 grouprevmap = GenGroup(l,OutDir+"group");
1009 GenShadowSudo(l, OutDir+"sudo-passwd")
1011 # Now we know who we're allowing on the machine, export
1012 # the relevant ssh keys
1013 if MultipleSSHFiles:
1014 OldMask = os.umask(0077);
1015 tf = tarfile.open(name=os.path.join(GlobalDir, 'ssh-keys-%s.tar.gz' % CurrentHost), mode='w:gz')
1017 for f in userlist.keys():
1018 if f not in SSHFiles:
1020 # If we're not exporting their primary group, don't export
1023 if userlist[f] in grouprevmap.keys():
1024 grname = grouprevmap[userlist[f]]
1027 if int(userlist[f]) <= 100:
1028 # In these cases, look it up in the normal way so we
1029 # deal with cases where, for instance, users are in group
1030 # users as their primary group.
1031 grname = grp.getgrgid(userlist[f])[0]
1032 except Exception, e:
1036 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])
1039 to = tf.gettarinfo(os.path.join(GlobalDir, 'userkeys', f), f)
1040 # These will only be used where the username doesn't
1041 # exist on the target system for some reason; hence,
1042 # in those cases, the safest thing is for the file to
1043 # be owned by root but group nobody. This deals with
1044 # the bloody obscure case where the group fails to exist
1045 # whilst the user does (in which case we want to avoid
1046 # ending up with a file which is owned user:root to avoid
1047 # a fairly obvious attack vector)
1050 # Using the username / groupname fields avoids any need
1051 # to give a shit^W^W^Wcare about the UIDoffset stuff.
1055 tf.addfile(to, file(os.path.join(GlobalDir, 'userkeys', f)))
1058 os.rename(os.path.join(GlobalDir, 'ssh-keys-%s.tar.gz' % CurrentHost),
1059 os.path.join(OutDir, 'ssh-keys.tar.gz'))
1061 if ExtraList.has_key("[UNTRUSTED]"):
1063 if not ExtraList.has_key("[NOPASSWD]"):
1064 GenShadow(l,OutDir+"shadow");
1066 # Link in global things
1067 DoLink(GlobalDir,OutDir,"markers");
1068 DoLink(GlobalDir,OutDir,"mail-forward.cdb");
1069 DoLink(GlobalDir,OutDir,"mail-disable");
1070 DoLink(GlobalDir,OutDir,"mail-greylist");
1071 DoLink(GlobalDir,OutDir,"mail-callout");
1072 DoLink(GlobalDir,OutDir,"mail-rbl");
1073 DoLink(GlobalDir,OutDir,"mail-rhsbl");
1074 DoLink(GlobalDir,OutDir,"mail-whitelist");
1077 DoLink(GlobalDir,OutDir,"forward-alias");
1079 if ExtraList.has_key("[DNS]"):
1080 GenDNS(l,OutDir+"dns-zone",Split[1]);
1081 GenSSHFP(l,OutDir+"dns-sshfp",Split[1])
1083 if ExtraList.has_key("[BSMTP]"):
1084 GenBSMTP(l,OutDir+"bsmtp",Split[1])
1086 if ExtraList.has_key("[PRIVATE]"):
1087 DoLink(GlobalDir,OutDir,"debian-private")
1089 if ExtraList.has_key("[KEYRING]"):
1091 DoLink(GlobalDir,OutDir,os.path.basename(k))
1094 try: posix.remove(OutDir+os.path.basename(k));