3 # Sync PostgreSQL with (LDAP-generated) passwd file
4 # Copyright (C) 2001 James Troup <james@nocrew.org>
5 # $Id: julia,v 1.1 2001-09-13 23:55:51 troup Exp $
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.
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.
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
22 # <aj> what's wrong with me!?!?!?
23 # <aj> i was just nice to some mormon doorknockers!!!
26 # <Omnic> I'm gonna have to kick your ass when you come over
27 # <Culus> aj: GET THE HELL OUT OF THE CABAL! :P
29 ################################################################################
31 import os, pg, string, sys;
35 ################################################################################
39 ################################################################################
41 def usage (exit_code):
42 print """Usage: julia [OPTION]... PASSWD_FILE
43 Sync postgres' pg_shadow with PASSWD_FILE.
45 -v, --verbose explain what is being done
46 -h, --help display this help and exit"""
49 ################################################################################
56 Cnf = apt_pkg.newConfiguration();
57 apt_pkg.ReadConfigFileISC(Cnf,utils.which_conf_file());
59 Arguments = [('q',"quiet","Julia::Options::Quiet"),
60 ('v',"verbose","Julia::Options::Verbose"),
61 ('D',"debug","Julia::Options::Debug", "IntVal"),
62 ('h',"help","Julia::Options::Help"),
63 ('V',"version","Julia::Options::Version")];
65 arguments = apt_pkg.ParseCommandLine(Cnf,Arguments,sys.argv);
66 Options = Cnf.SubTree("Julia::Options")
71 utils.warn("julia needs the name of the passwd file to sync with as an argument.");
73 elif len(arguments) > 1:
74 utils.warn("julia only takes one non-option argument, the passwd file to sync with.");
77 passwd = arguments[0];
78 projectB = pg.connect(Cnf["DB::Name"], Cnf["DB::Host"], int(Cnf["DB::Port"]));
79 valid_gid = Cnf.get("Julia::ValidGID","");
82 passwd_file = utils.open_file(passwd);
83 for line in passwd_file.readlines():
84 split = string.split(line, ':');
87 if valid_gid and gid != valid_gid:
88 if Options["Verbose"]:
89 print "Skipping %s (GID %s != Valid GID %s)." % (uname, gid, valid_gid);
91 passwd_unames[uname] = "";
94 q = projectB.query("SELECT usename FROM pg_user");
98 postgres_unames[uname] = "";
100 known_postgres_unames = {};
101 for i in string.split(Cnf.get("Julia::KnownPostgres",""),","):
102 uname = string.strip(i);
103 known_postgres_unames[uname] = "";
105 keys = postgres_unames.keys()
108 if not passwd_unames.has_key(uname)and not known_postgres_unames.has_key(uname):
109 print "W: %s is in Postgres but not the passwd file or list of known Postgres users." % (uname);
111 keys = passwd_unames.keys()
114 if not postgres_unames.has_key(uname):
115 if not Options["Quiet"]:
116 print "Creating %s user in Postgres." % (uname);
117 q = projectB.query('CREATE USER "%s"' % (uname));
119 #######################################################################################
121 if __name__ == '__main__':