]> git.decadent.org.uk Git - dak.git/blob - julia
lose second argument to open_file since we use default, change foo == [] to not foo.
[dak.git] / julia
1 #!/usr/bin/env python
2
3 # Sync PostgreSQL with (LDAP-generated) passwd file
4 # Copyright (C) 2001  James Troup <james@nocrew.org>
5 # $Id: julia,v 1.3 2001-11-04 22:28:44 troup Exp $
6
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
11
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU General Public License for more details.
16
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20
21 ################################################################################
22
23 # <aj> ARRRGGGHHH
24 # <aj> what's wrong with me!?!?!?
25 # <aj> i was just nice to some mormon doorknockers!!!
26 # <Omnic> AJ?!?!
27 # <aj> i know!!!!!
28 # <Omnic> I'm gonna have to kick your ass when you come over
29 # <Culus> aj: GET THE HELL OUT OF THE CABAL! :P
30
31 ################################################################################
32
33 import os, pg, string, sys;
34 import utils;
35 import apt_pkg;
36
37 ################################################################################
38
39 Cnf = None;
40 projectB = None;
41 ################################################################################
42
43 def usage (exit_code=0):
44     print """Usage: julia [OPTION]... PASSWD_FILE
45 Sync PostgreSQL's pg_user with PASSWD_FILE.
46
47   -q, --quiet                be quiet about what is being done
48   -v, --verbose              explain what is being done
49   -h, --help                 show this help and exit"""
50     sys.exit(exit_code)
51
52 ################################################################################
53
54 def main ():
55     global Cnf, projectB;
56
57     apt_pkg.init();
58
59     Cnf = apt_pkg.newConfiguration();
60     apt_pkg.ReadConfigFileISC(Cnf,utils.which_conf_file());
61
62     Arguments = [('q',"quiet","Julia::Options::Quiet"),
63                  ('v',"verbose","Julia::Options::Verbose"),
64                  ('h',"help","Julia::Options::Help")];
65     for i in ["quiet", "verbose", "help" ]:
66         Cnf["Julia::Options::%s" % (i)] = "";
67
68     arguments = apt_pkg.ParseCommandLine(Cnf,Arguments,sys.argv);
69     Options = Cnf.SubTree("Julia::Options")
70
71     if Options["Help"]:
72         usage();
73     if not arguments:
74         utils.warn("julia needs the name of the passwd file to sync with as an argument.");
75         usage(1);
76     elif len(arguments) > 1:
77         utils.warn("julia only takes one non-option argument, the passwd file to sync with.");
78         usage(1);
79
80     passwd = arguments[0];
81     projectB = pg.connect(Cnf["DB::Name"], Cnf["DB::Host"], int(Cnf["DB::Port"]));
82     valid_gid = Cnf.get("Julia::ValidGID","");
83
84     passwd_unames = {};
85     passwd_file = utils.open_file(passwd);
86     for line in passwd_file.readlines():
87         split = string.split(line, ':');
88         uname = split[0];
89         gid = split[3];
90         if valid_gid and gid != valid_gid:
91             if Options["Verbose"]:
92                 print "Skipping %s (GID %s != Valid GID %s)." % (uname, gid, valid_gid);
93             continue;
94         passwd_unames[uname] = "";
95
96     postgres_unames = {};
97     q = projectB.query("SELECT usename FROM pg_user");
98     ql = q.getresult();
99     for i in ql:
100         uname = i[0];
101         postgres_unames[uname] = "";
102
103     known_postgres_unames = {};
104     for i in string.split(Cnf.get("Julia::KnownPostgres",""),","):
105         uname = string.strip(i);
106         known_postgres_unames[uname] = "";
107
108     keys = postgres_unames.keys()
109     keys.sort();
110     for uname in keys:
111         if not passwd_unames.has_key(uname)and not known_postgres_unames.has_key(uname):
112             print "W: %s is in Postgres but not the passwd file or list of known Postgres users." % (uname);
113
114     keys = passwd_unames.keys()
115     keys.sort();
116     for uname in keys:
117         if not postgres_unames.has_key(uname):
118             if not Options["Quiet"]:
119                 print "Creating %s user in Postgres." % (uname);
120             q = projectB.query('CREATE USER "%s"' % (uname));
121
122 #######################################################################################
123
124 if __name__ == '__main__':
125     main()
126