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>
12 # This program is free software; you can redistribute it and/or modify
13 # it under the terms of the GNU General Public License as published by
14 # the Free Software Foundation; either version 2 of the License, or
15 # (at your option) any later version.
17 # This program is distributed in the hope that it will be useful,
18 # but WITHOUT ANY WARRANTY; without even the implied warranty of
19 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 # GNU General Public License for more details.
22 # You should have received a copy of the GNU General Public License
23 # along with this program; if not, write to the Free Software
24 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 import string, re, time, ldap, getopt, sys, os, pwd, posix, socket, base64, sha, shutil
27 from userdir_ldap import *;
37 EmailCheck = re.compile("^([^ <>@]+@[^ ,<>@]+)?$");
38 BSMTPCheck = re.compile(".*mx 0 (gluck)\.debian\.org\..*",re.DOTALL);
39 DNSZone = ".debian.net"
40 Keyrings = [ "/org/keyring.debian.org/keyrings/debian-keyring.gpg",
41 "/org/keyring.debian.org/keyrings/debian-keyring.pgp" ]
44 return Str.translate(string.maketrans("\n\r\t","$$$"))
46 def DoLink(From,To,File):
47 try: posix.remove(To+File);
49 posix.link(From+File,To+File);
51 # See if this user is in the group list
52 def IsInGroup(DnRecord):
56 # See if the primary group is in the list
57 if Allowed.has_key(GetAttr(DnRecord,"gidNumber")) != 0:
60 # Check the host based ACL
61 if DnRecord[1].has_key("allowedHost") != 0:
62 for I in DnRecord[1]["allowedHost"]:
66 # See if there are supplementary groups
67 if DnRecord[1].has_key("supplementaryGid") == 0:
70 # Check the supplementary groups
71 for I in DnRecord[1]["supplementaryGid"]:
72 if Allowed.has_key(I):
81 try: os.remove(File + ".tmp");
83 try: os.remove(File + ".tdb.tmp");
89 os.rename(File + ".tmp",File);
92 os.rename(File + ".tdb.tmp",File+".tdb");
94 # Generate the password list
95 def GenPasswd(l,File,HomePrefix,PwdMarker):
98 F = open(File + ".tdb.tmp","w");
100 # Fetch all the users
102 if PasswdAttrs == None:
106 for x in PasswdAttrs:
107 if x[1].has_key("uidNumber") == 0 or IsInGroup(x) == 0:
110 # Do not let people try to buffer overflow some busted passwd parser.
111 if len(GetAttr(x,"gecos")) > 100 or len(GetAttr(x,"loginShell")) > 50:
114 Line = "%s:%s:%s:%s:%s:%s%s:%s" % (GetAttr(x,"uid"),\
116 GetAttr(x,"uidNumber"),GetAttr(x,"gidNumber"),\
117 GetAttr(x,"gecos"),HomePrefix,GetAttr(x,"uid"),\
118 GetAttr(x,"loginShell"));
120 Line = Sanitize(Line) + "\n";
121 F.write("0%u %s" % (I,Line));
122 F.write(".%s %s" % (GetAttr(x,"uid"),Line));
123 F.write("=%s %s" % (GetAttr(x,"uidNumber"),Line));
126 # Oops, something unspeakable happened.
132 # Generate the shadow list
133 def GenShadow(l,File):
136 OldMask = os.umask(0077);
137 F = open(File + ".tdb.tmp","w",0600);
140 # Fetch all the users
142 if PasswdAttrs == None:
146 for x in PasswdAttrs:
147 if x[1].has_key("uidNumber") == 0 or IsInGroup(x) == 0:
150 Pass = GetAttr(x,"userPassword");
151 if Pass[0:7] != "{crypt}" or len(Pass) > 50:
156 # If the account is locked, mark it as such in shadow
157 # See Debian Bug #308229 for why we set it to 1 instead of 0
158 if (GetAttr(x,"userPassword").find("*LK*") != -1) \
159 or GetAttr(x,"userPassword").startswith("!"):
162 ShadowExpire = GetAttr(x,"shadowexpire")
164 Line = "%s:%s:%s:%s:%s:%s:%s:%s:" % (GetAttr(x,"uid"),\
165 Pass,GetAttr(x,"shadowLastChange"),\
166 GetAttr(x,"shadowMin"),GetAttr(x,"shadowMax"),\
167 GetAttr(x,"shadowWarning"),GetAttr(x,"shadowinactive"),\
169 Line = Sanitize(Line) + "\n";
170 F.write("0%u %s" % (I,Line));
171 F.write(".%s %s" % (GetAttr(x,"uid"),Line));
174 # Oops, something unspeakable happened.
180 # Generate the shadow list
181 def GenSSHShadow(l,File):
184 OldMask = os.umask(0077);
185 F = open(File + ".tmp","w",0600);
188 # Fetch all the users
190 if PasswdAttrs == None:
193 for x in PasswdAttrs:
194 # If the account is locked, do not write it.
195 # This is a partial stop-gap. The ssh also needs to change this
196 # to ignore ~/.ssh/authorized* files.
197 if (GetAttr(x,"userPassword").find("*LK*") != -1) \
198 or GetAttr(x,"userPassword").startswith("!"):
201 if x[1].has_key("uidNumber") == 0 or \
202 x[1].has_key("sshRSAAuthKey") == 0:
204 for I in x[1]["sshRSAAuthKey"]:
205 User = GetAttr(x,"uid");
206 Line = "%s: %s" %(User,I);
207 Line = Sanitize(Line) + "\n";
209 # Oops, something unspeakable happened.
215 # Generate the group list
216 def GenGroup(l,File):
219 F = open(File + ".tdb.tmp","w");
221 # Generate the GroupMap
223 for x in GroupIDMap.keys():
226 # Fetch all the users
228 if PasswdAttrs == None:
231 # Sort them into a list of groups having a set of users
232 for x in PasswdAttrs:
233 if x[1].has_key("uidNumber") == 0 or IsInGroup(x) == 0:
235 if x[1].has_key("supplementaryGid") == 0:
238 for I in x[1]["supplementaryGid"]:
239 if GroupMap.has_key(I):
240 GroupMap[I].append(GetAttr(x,"uid"));
242 print "Group does not exist ",I,"but",GetAttr(x,"uid"),"is in it";
244 # Output the group file.
246 for x in GroupMap.keys():
247 if GroupIDMap.has_key(x) == 0:
249 Line = "%s:x:%u:" % (x,GroupIDMap[x]);
251 for I in GroupMap[x]:
252 Line = Line + ("%s%s" % (Comma,I));
254 Line = Sanitize(Line) + "\n";
255 F.write("0%u %s" % (J,Line));
256 F.write(".%s %s" % (x,Line));
257 F.write("=%u %s" % (GroupIDMap[x],Line));
260 # Oops, something unspeakable happened.
266 # Generate the email forwarding list
267 def GenForward(l,File):
270 OldMask = os.umask(0022);
271 F = open(File + ".tmp","w",0644);
274 # Fetch all the users
276 if PasswdAttrs == None:
279 # Write out the email address for each user
280 for x in PasswdAttrs:
281 if x[1].has_key("emailForward") == 0 or IsInGroup(x) == 0:
284 # Do not allow people to try to buffer overflow busted parsers
285 if len(GetAttr(x,"emailForward")) > 200:
288 # Check the forwarding address
289 if EmailCheck.match(GetAttr(x,"emailForward")) == None:
291 Line = "%s: %s" % (GetAttr(x,"uid"),GetAttr(x,"emailForward"));
292 Line = Sanitize(Line) + "\n";
295 # Oops, something unspeakable happened.
301 def GenAllForward(l,File):
304 OldMask = os.umask(0022);
305 Fdb = os.popen("cdbmake %s %s.tmp"%(File,File),"w");
308 # Fetch all the users
310 if PasswdAttrs == None:
313 # Write out the email address for each user
314 for x in PasswdAttrs:
315 if x[1].has_key("emailForward") == 0:
318 # Do not allow people to try to buffer overflow busted parsers
319 Forward = GetAttr(x,"emailForward");
320 if len(Forward) > 200:
323 # Check the forwarding address
324 if EmailCheck.match(Forward) == None:
327 User = GetAttr(x,"uid");
328 Fdb.write("+%d,%d:%s->%s\n"%(len(User),len(Forward),User,Forward));
330 # Oops, something unspeakable happened.
334 if Fdb.close() != None:
335 raise "cdbmake gave an error";
337 # Generate the anon XEarth marker file
338 def GenMarkers(l,File):
341 F = open(File + ".tmp","w");
343 # Fetch all the users
345 if PasswdAttrs == None:
348 # Write out the position for each user
349 for x in PasswdAttrs:
350 if x[1].has_key("latitude") == 0 or x[1].has_key("longitude") == 0:
353 Line = "%8s %8s \"\""%(DecDegree(GetAttr(x,"latitude"),1),DecDegree(GetAttr(x,"longitude"),1));
354 Line = Sanitize(Line) + "\n";
359 # Oops, something unspeakable happened.
365 # Generate the debian-private subscription list
366 def GenPrivate(l,File):
369 F = open(File + ".tmp","w");
371 # Fetch all the users
373 if PasswdAttrs == None:
376 # Write out the position for each user
377 for x in PasswdAttrs:
378 if x[1].has_key("privateSub") == 0:
381 # If the account is locked, do not write it
382 if (GetAttr(x,"userPassword").find("*LK*") != -1) \
383 or GetAttr(x,"userPassword").startswith("!"):
386 # If the account has no PGP key, do not write it
387 if x[1].has_key("keyFingerPrint") == 0:
390 # Must be in the Debian group (yuk, hard coded for now)
391 if GetAttr(x,"gidNumber") != "800":
395 Line = "%s"%(GetAttr(x,"privateSub"));
396 Line = Sanitize(Line) + "\n";
401 # Oops, something unspeakable happened.
407 # Generate a list of locked accounts
408 def GenDisabledAccounts(l,File):
411 F = open(File + ".tmp","w");
413 # Fetch all the users
415 if PasswdAttrs == None:
419 for x in PasswdAttrs:
420 if x[1].has_key("uidNumber") == 0:
423 Pass = GetAttr(x,"userPassword");
425 # *LK* is the reference value for a locked account
426 # password starting with ! is also a locked account
427 if Pass.find("*LK*") != -1 or Pass.startswith("!"):
428 # Format is <login>:<reason>
429 Line = "%s:%s" % (GetAttr(x,"uid"), "Account is locked")
432 F.write(Sanitize(Line) + "\n")
434 # Oops, something unspeakable happened.
440 # Generate the list of local addresses that refuse all mail
441 def GenMailDisable(l,File):
444 F = open(File + ".tmp","w");
446 # Fetch all the users
448 if PasswdAttrs == None:
451 for x in PasswdAttrs:
454 # If the account is locked, disable incoming mail
455 if (GetAttr(x,"userPassword").find("*LK*") != -1):
456 if GetAttr(x,"uid") == "luther":
459 Reason = "user account locked"
461 if x[1].has_key("mailDisableMessage"):
462 Reason = GetAttr(x,"mailDisableMessage")
466 # Must be in the Debian group (yuk, hard coded for now)
467 if GetAttr(x,"gidNumber") != "800":
471 Line = "%s: %s"%(GetAttr(x,"uid"),Reason);
472 Line = Sanitize(Line) + "\n";
477 # Oops, something unspeakable happened.
483 # Generate a list of uids that should have boolean affects applied
484 def GenMailBool(l,File,Key):
487 F = open(File + ".tmp","w");
489 # Fetch all the users
491 if PasswdAttrs == None:
494 for x in PasswdAttrs:
497 if x[1].has_key(Key) == 0:
500 # Must be in the Debian group (yuk, hard coded for now)
501 if GetAttr(x,"gidNumber") != "800":
504 if GetAttr(x,Key) != "TRUE":
508 Line = "%s"%(GetAttr(x,"uid"));
509 Line = Sanitize(Line) + "\n";
514 # Oops, something unspeakable happened.
520 # Generate a list of hosts for RBL or whitelist purposes.
521 def GenMailList(l,File,Key):
524 F = open(File + ".tmp","w");
526 # Fetch all the users
528 if PasswdAttrs == None:
531 for x in PasswdAttrs:
534 if x[1].has_key(Key) == 0:
537 # Must be in the Debian group (yuk, hard coded for now)
538 if GetAttr(x,"gidNumber") != "800":
545 if Key == "mailWhitelist":
546 if re.match('^[-\w.]+(/[\d]+)?$',z) == None:
549 if re.match('^[-\w.]+$',z) == None:
553 Line = GetAttr(x,"uid")
557 if Key == "mailRHSBL":
558 Line += "/$sender_address_domain"
561 Line = Sanitize(Line) + "\n";
566 # Oops, something unspeakable happened.
572 # Generate the DNS Zone file
573 def GenDNS(l,File,HomePrefix):
576 F = open(File + ".tmp","w");
578 # Fetch all the users
580 if PasswdAttrs == None:
583 # Write out the zone file entry for each user
584 for x in PasswdAttrs:
585 if x[1].has_key("dnsZoneEntry") == 0:
588 # If the account has no PGP key, do not write it
589 if x[1].has_key("keyFingerPrint") == 0:
592 F.write("; %s\n"%(EmailAddress(x)));
593 for z in x[1]["dnsZoneEntry"]:
594 Split = z.lower().split()
595 if Split[1].lower() == 'in':
596 for y in range(0,len(Split)):
599 Line = " ".join(Split) + "\n";
602 Host = Split[0] + DNSZone;
603 if BSMTPCheck.match(Line) != None:
604 F.write("; Has BSMTP\n");
606 # Write some identification information
607 if Split[2].lower() == "a":
608 Line = "%s IN TXT \"%s\"\n"%(Split[0],EmailAddress(x));
609 for y in x[1]["keyFingerPrint"]:
610 Line = Line + "%s IN TXT \"PGP %s\"\n"%(Split[0],FormatPGPKey(y));
613 Line = "; Err %s"%(str(Split));
618 F.write("; Errors\n");
621 # Oops, something unspeakable happened.
627 # Generate the DNS SSHFP records
628 def GenSSHFP(l,File,HomePrefix):
631 F = open(File + ".tmp","w")
633 # Fetch all the hosts
635 if HostAttrs == None:
639 if x[1].has_key("hostname") == 0 or \
640 x[1].has_key("sshRSAHostKey") == 0:
642 Host = GetAttr(x,"hostname");
644 for I in x[1]["sshRSAHostKey"]:
646 if Split[0] == 'ssh-rsa':
648 if Split[0] == 'ssh-dss':
650 if Algorithm == None:
652 Fingerprint = sha.new(base64.decodestring(Split[1])).hexdigest()
653 Line = "%s. IN SSHFP %u 1 %s" % (Host,Algorithm,Fingerprint)
654 Line = Sanitize(Line) + "\n"
656 # Oops, something unspeakable happened.
662 # Generate the BSMTP file
663 def GenBSMTP(l,File,HomePrefix):
666 F = open(File + ".tmp","w");
668 # Fetch all the users
670 if PasswdAttrs == None:
673 # Write out the zone file entry for each user
674 for x in PasswdAttrs:
675 if x[1].has_key("dnsZoneEntry") == 0:
678 # If the account has no PGP key, do not write it
679 if x[1].has_key("keyFingerPrint") == 0:
682 for z in x[1]["dnsZoneEntry"]:
683 Split = z.lower().split()
684 if Split[1].lower() == 'in':
685 for y in range(0,len(Split)):
688 Line = " ".join(Split) + "\n";
690 Host = Split[0] + DNSZone;
691 if BSMTPCheck.match(Line) != None:
692 F.write("%s: user=%s group=Debian file=%s%s/bsmtp/%s\n"%(Host,
693 GetAttr(x,"uid"),HomePrefix,GetAttr(x,"uid"),Host));
696 F.write("; Errors\n");
699 # Oops, something unspeakable happened.
709 if not Host in HostToIPCache:
712 IPAdressesT = list(set([ (a[0],a[4][0]) for a in socket.getaddrinfo(Host, None)]))
713 except socket.gaierror, (code):
714 if code[0] != -2: raise
716 for addr in IPAdressesT:
717 if addr[0] == socket.AF_INET: IPAdresses += [addr[1], "::ffff:"+addr[1]]
718 else: IPAdresses += [addr[1]]
719 HostToIPCache[Host] = IPAdresses
720 return HostToIPCache[Host]
723 # Generate the ssh known hosts file
724 def GenSSHKnown(l,File,mode=None):
727 OldMask = os.umask(0022);
728 F = open(File + ".tmp","w",0644);
732 if HostAttrs == None:
736 if x[1].has_key("hostname") == 0 or \
737 x[1].has_key("sshRSAHostKey") == 0:
739 Host = GetAttr(x,"hostname");
741 SHost = Host.find(".")
742 if SHost != None: HostNames += [Host[0:SHost]]
744 for I in x[1]["sshRSAHostKey"]:
745 if mode and mode == 'authorized_keys':
746 #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)
747 Line = 'command="rsync --server --sender -pr . /var/cache/userdir-ldap/hosts/%s",no-port-forwarding,no-X11-forwarding,no-agent-forwarding %s' % (Host,I)
749 Line = "%s %s" %(",".join(HostNames + HostToIP(Host)), I);
750 Line = Sanitize(Line) + "\n";
752 # Oops, something unspeakable happened.
758 # Generate the debianhosts file (list of all IP addresses)
759 def GenHosts(l,File):
762 OldMask = os.umask(0022);
763 F = open(File + ".tmp","w",0644);
766 # Fetch all the hosts
767 HostNames = l.search_s(HostBaseDn,ldap.SCOPE_ONELEVEL,"hostname=*",\
770 if HostNames == None:
774 if x[1].has_key("hostname") == 0:
776 Host = GetAttr(x,"hostname");
778 Addr = socket.gethostbyname(Host);
779 F.write(Addr + "\n");
782 # Oops, something unspeakable happened.
788 def GenKeyrings(l,OutDir):
790 shutil.copy(k, OutDir)
792 # Connect to the ldap server
793 l = ldap.open(LDAPServer);
794 F = open(PassDir+"/pass-"+pwd.getpwuid(os.getuid())[0],"r");
795 Pass = F.readline().strip().split(" ")
797 l.simple_bind_s("uid="+Pass[0]+","+BaseDn,Pass[1]);
799 # Fetch all the groups
801 Attrs = l.search_s(BaseDn,ldap.SCOPE_ONELEVEL,"gid=*",\
802 ["gid","gidNumber"]);
804 # Generate the GroupMap and GroupIDMap
806 if x[1].has_key("gidNumber") == 0:
808 GroupIDMap[x[1]["gid"][0]] = int(x[1]["gidNumber"][0]);
810 # Fetch all the users
811 PasswdAttrs = l.search_s(BaseDn,ldap.SCOPE_ONELEVEL,"uid=*",\
812 ["uid","uidNumber","gidNumber","supplementaryGid",\
813 "gecos","loginShell","userPassword","shadowLastChange",\
814 "shadowMin","shadowMax","shadowWarning","shadowinactive",
815 "shadowexpire","emailForward","latitude","longitude",\
816 "allowedHost","sshRSAAuthKey","dnsZoneEntry","cn","sn",\
817 "keyFingerPrint","privateSub","mailDisableMessage",\
818 "mailGreylisting","mailCallout","mailRBL","mailRHSBL",\
820 # Fetch all the hosts
821 HostAttrs = l.search_s(HostBaseDn,ldap.SCOPE_ONELEVEL,"sshRSAHostKey=*",\
822 ["hostname","sshRSAHostKey"]);
824 # Open the control file
825 if len(sys.argv) == 1:
826 F = open(GenerateConf,"r");
828 F = open(sys.argv[1],"r")
830 # Generate global things
831 GlobalDir = GenerateDir+"/";
832 GenSSHShadow(l,GlobalDir+"ssh-rsa-shadow");
833 GenAllForward(l,GlobalDir+"mail-forward.cdb");
834 GenMarkers(l,GlobalDir+"markers");
835 GenPrivate(l,GlobalDir+"debian-private");
836 GenDisabledAccounts(l,GlobalDir+"disabled-accounts");
837 GenSSHKnown(l,GlobalDir+"ssh_known_hosts");
838 #GenSSHKnown(l,GlobalDir+"authorized_keys", 'authorized_keys');
839 GenHosts(l,GlobalDir+"debianhosts");
840 GenMailDisable(l,GlobalDir+"mail-disable");
841 GenMailBool(l,GlobalDir+"mail-greylist","mailGreylisting");
842 GenMailBool(l,GlobalDir+"mail-callout","mailCallout");
843 GenMailList(l,GlobalDir+"mail-rbl","mailRBL");
844 GenMailList(l,GlobalDir+"mail-rhsbl","mailRHSBL");
845 GenMailList(l,GlobalDir+"mail-whitelist","mailWhitelist");
846 GenKeyrings(l,GlobalDir);
849 GenForward(l,GlobalDir+"forward-alias");
861 Split = Line.split(" ")
862 OutDir = GenerateDir + '/' + Split[0] + '/';
863 try: os.mkdir(OutDir);
866 # Get the group list and convert any named groups to numerics
874 if GroupIDMap.has_key(I):
875 GroupList[str(GroupIDMap[I])] = None;
880 CurrentHost = Split[0];
882 DoLink(GlobalDir,OutDir,"ssh-rsa-shadow");
883 DoLink(GlobalDir,OutDir,"debianhosts");
884 DoLink(GlobalDir,OutDir,"ssh_known_hosts");
885 DoLink(GlobalDir,OutDir,"disabled-accounts")
888 if ExtraList.has_key("[NOPASSWD]"):
889 GenPasswd(l,OutDir+"passwd",Split[1], "*");
891 GenPasswd(l,OutDir+"passwd",Split[1], "x");
893 GenGroup(l,OutDir+"group");
894 if ExtraList.has_key("[UNTRUSTED]"):
896 if not ExtraList.has_key("[NOPASSWD]"):
897 GenShadow(l,OutDir+"shadow");
899 # Link in global things
900 DoLink(GlobalDir,OutDir,"markers");
901 DoLink(GlobalDir,OutDir,"mail-forward.cdb");
902 DoLink(GlobalDir,OutDir,"mail-disable");
903 DoLink(GlobalDir,OutDir,"mail-greylist");
904 DoLink(GlobalDir,OutDir,"mail-callout");
905 DoLink(GlobalDir,OutDir,"mail-rbl");
906 DoLink(GlobalDir,OutDir,"mail-rhsbl");
907 DoLink(GlobalDir,OutDir,"mail-whitelist");
910 DoLink(GlobalDir,OutDir,"forward-alias");
912 if ExtraList.has_key("[DNS]"):
913 GenDNS(l,OutDir+"dns-zone",Split[1]);
914 GenSSHFP(l,OutDir+"dns-sshfp",Split[1])
916 if ExtraList.has_key("[BSMTP]"):
917 GenBSMTP(l,OutDir+"bsmtp",Split[1])
919 if ExtraList.has_key("[PRIVATE]"):
920 DoLink(GlobalDir,OutDir,"debian-private")
922 if ExtraList.has_key("[KEYRING]"):
924 DoLink(GlobalDir,OutDir,os.path.basename(k))
927 try: posix.remove(OutDir+os.path.basename(k));