5 # 1. /etc/mail/aliasesPL, /etc/mail/virtusertable
6 # <slicename>@slices.planet-lab.org: all users and PIs of a slice
7 # pi-<loginbase>@sites.planet-lab.org: all PIs at a site
8 # tech-<loginbase>@sites.planet-lab.org: all techs at a site
9 # 2. /etc/mail/local-host-names
10 # Additional local e-mail FQDNs
14 # 1. announce@lists.planet-lab.org membership from the database and from
15 # the membership of announce-additions@lists.planet-lab.org
16 # 2. {pis,techs}@lists.planet-lab.org membership from the database.
17 # 3. cvs@lists.planet-lab.org accept_these_nonmembers from
18 # /usr/share/doc/plc/accounts.txt
22 # 1. See plc/mail/sendmail-mail.mc for the ALIAS_FILE definition that
23 # includes /etc/mail/aliasesPL.
25 # Mark Huang <mlhuang@cs.princeton.edu>
26 # Copyright (C) 2005 The Trustees of Princeton University
36 # Procmail command for aliases. See plc/mail/procmailrc for how these
37 # two scripts interact with each other.
38 procmail = "|/usr/bin/procmail -m /etc/mail/procmailrc"
40 # Parse additional options
44 usage: %s [OPTION]... [slice|pi|tech] [slicename|sitename]
46 gen_aliases.py options:
47 -n Dry run, do not sync memberships or write files
53 (plcapi, moreopts, argv) = plcapilib.plcapi(globals(), sys.argv, shortopts, longopts, moreusage)
54 for opt, optval in moreopts.iteritems():
55 if opt == "-n" or opt == "--dryrun":
58 # Parse /usr/share/doc/plc/accounts.txt (or /etc/passwd)
59 def passwd(path = '/etc/passwd'):
62 required = ['account', 'password', 'uid', 'gid', 'gecos', 'directory', 'shell']
63 optional = ['email', 'servers']
65 for line in file(path):
67 if line.strip() == '' or line[0] in '#':
70 fields = line.strip().split(':')
71 if len(fields) < len(required):
73 # {'account': 'princeton_mlh', 'password': 'x', ...}
74 entries.append(dict(zip(required + optional, fields)))
78 def GetPIs(site_id_or_login_base):
80 for site in GetSites([site_id_or_login_base], ['person_ids']):
81 persons = GetPersons(site['person_ids'], ['email', 'roles', 'enabled'])
82 pis += filter(lambda person: 'pi' in person['roles'] and person['enabled'], persons)
84 return [pi['email'] for pi in pis]
87 def GetTechs(login_base):
89 for site in GetSites([login_base], ['person_ids']):
90 persons = GetPersons(site['person_ids'], ['email', 'roles', 'enabled'])
91 techs += filter(lambda person: 'tech' in person['roles'] and person['enabled'], persons)
93 return [tech['email'] for tech in techs]
96 def GetSliceUsers(name):
97 # Get the users of the slice
100 for slice in GetSlices([name], ['site_id', 'person_ids']):
101 persons = GetPersons(slice['person_ids'], ['email', 'enabled'])
102 enabledpersons += filter(lambda person: person['enabled'], persons)
103 users += [person['email'] for person in enabledpersons]
104 # Add all the PIs for the site
105 users += GetPIs(slice['site_id'])
106 # Remove duplicates and sort
107 users = list(Set(users))
111 # Called every 10 minutes without arguments to sync
112 # {announce,pis,techs,cvs}@lists.planet-lab.org memberships and to
113 # regenerate /etc/mail/{aliasesPL,virtusertable,local-host-names}.
118 local_host_names = virtusertable = aliases = cvs_config = sys.stdout
120 local_host_names = file("local-host-names", 'w')
121 virtusertable = file("virtusertable", 'w')
122 aliases = file("aliasesPL", 'w')
123 cvs_config = tempfile.NamedTemporaryFile()
125 # Parse /usr/share/doc/plc/accounts.txt. XXX Should probably make
126 # CVS access a DB property.
128 for pw in passwd('/usr/share/doc/plc/accounts.txt'):
129 # Only allow posts from those with implicit or explicit access to
130 # all servers or explicit access to the CVS server
131 if pw.has_key('servers') and pw['servers'] not in ['*', 'cvs']:
134 # System users are those with UIDs greater than 2000 and less than 3000
135 if int(pw['uid']) >= 2000 and int(pw['uid']) < 3000:
138 cvs_nonmembers.append("'" + pw['account'] + "@planet-lab.org" + "'")
140 # Update accept_these_nonmembers property of the CVS mailing
141 # list. This ensures that those with access to the CVS server are
142 # able to post loginfo messages when they check in files. The CVS
143 # server's sendmail setup is configured to masquerade as
145 cvs_config.write("accept_these_nonmembers = [" + ", ".join(cvs_nonmembers) + "]\n")
148 config_list = os.popen("/var/mailman/bin/config_list -i %s cvs" % cvs_config.name)
149 if config_list.close() is not None:
150 raise Exception, "/var/mailman/bin/config_list cvs failed"
156 for person in GetPersons({'enabled': True}, ['person_id', 'email', 'roles']):
157 announce.append(person['email'])
158 if 'pi' in person['roles']:
159 pis.append(person['email'])
160 if 'tech' in person['roles']:
161 techs.append(person['email'])
163 # Generate announce@lists.planet-lab.org membership
165 # Merge in membership of announce-additions
166 list_members = os.popen("/var/mailman/bin/list_members announce-additions", 'r')
167 announce += map(lambda line: line.strip(), list_members.readlines())
169 # Remove duplicates and sort
170 announce = list(Set(announce))
173 sync_members = os.popen("/var/mailman/bin/sync_members %s -w=yes -g=yes -f - announce" % flags, 'w')
174 sync_members.write("\n".join(announce))
175 if sync_members.close() is not None:
176 raise Exception, "/var/mailman/bin/sync_members announce failed"
178 # Generate {pis,techs}@lists.planet-lab.org membership
180 # Remove duplicates and sort
183 techs = list(Set(techs))
186 sync_members = os.popen("/var/mailman/bin/sync_members %s -w=no -g=no -f - pis" % flags, 'w')
187 sync_members.write("\n".join(pis))
188 if sync_members.close() is not None:
189 raise Exception, "/var/mailman/bin/sync_members pis failed"
190 sync_members = os.popen("/var/mailman/bin/sync_members %s -w=no -g=no -f - techs" % flags, 'w')
191 sync_members.write("\n".join(techs))
192 if sync_members.close() is not None:
193 raise Exception, "/var/mailman/bin/sync_members techs failed"
195 # Generate local-host-names file
196 local_host_names.write("planet-lab.org\n")
197 local_host_names.write("slices.planet-lab.org\n")
198 local_host_names.write("sites.planet-lab.org\n")
200 # Generate SLICENAME@slices.planet-lab.org mapping and
201 # slice-SLICENAME alias
202 for slice in GetSlices(None, ['name']):
204 virtusertable.write("%s@slices.planet-lab.org\tslice-%s\n" % \
206 aliases.write("slice-%s: \"%s slice %s\"\n" % \
207 (name, procmail, name))
209 # Generate {pi,tech}-LOGINBASE@sites.planet-lab.org and
210 # {pi,tech}-LOGINBASE alias
211 for site in GetSites(None, ['login_base']):
212 for prefix in ['pi', 'tech']:
213 # This is probably unnecessary since the mapping is 1-1
214 virtusertable.write("%s-%s@sites.planet-lab.org\t%s-%s\n" % \
215 (prefix, site['login_base'], prefix, site['login_base']))
216 aliases.write("%s-%s: \"%s %s %s\"\n" % \
217 (prefix, site['login_base'], procmail, prefix, site['login_base']))
219 # Generate special cases. all-pi and all-tech used to be aliases
220 # for all PIs and all techs. They are now mailing lists like
221 # announce. NO-pi and NO-tech notify support that a site is
222 # missing one or the other and are used by some scripts.
223 aliases.write("all-pi: pis\n")
224 aliases.write("all-tech: techs\n")
225 aliases.write("NO-pi: support\n")
226 aliases.write("NO-tech: support\n")
229 local_host_names.close()
230 virtusertable.close()
234 # Otherwise, print space-separated list of aliases
236 if argv[0] == "slice":
237 print " ".join(GetSliceUsers(argv[1]))
238 elif argv[0] == "pi":
239 print " ".join(GetPIs(argv[1]))
240 elif argv[0] == "tech":
241 print " ".join(GetTechs(argv[1]))
244 plcapi.usage(moreusage)