3 """ Sync PostgreSQL users with system users """
4 # Copyright (C) 2001, 2002, 2006 James Troup <james@nocrew.org>
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 ################################################################################
23 # <aj> what's wrong with me!?!?!?
24 # <aj> i was just nice to some mormon doorknockers!!!
27 # <Omnic> I'm gonna have to kick your ass when you come over
28 # <Culus> aj: GET THE HELL OUT OF THE CABAL! :P
30 ################################################################################
34 from daklib import utils
36 ################################################################################
40 ################################################################################
42 def usage (exit_code=0):
43 print """Usage: dak import-users-from-passwd [OPTION]...
44 Sync PostgreSQL's users with system users.
46 -h, --help show this help and exit
47 -n, --no-action don't do anything
48 -q, --quiet be quiet about what is being done
49 -v, --verbose explain what is being done"""
52 ################################################################################
57 Cnf = utils.get_conf()
59 Arguments = [('n', "no-action", "Import-Users-From-Passwd::Options::No-Action"),
60 ('q', "quiet", "Import-Users-From-Passwd::Options::Quiet"),
61 ('v', "verbose", "Import-Users-From-Passwd::Options::Verbose"),
62 ('h', "help", "Import-Users-From-Passwd::Options::Help")]
63 for i in [ "no-action", "quiet", "verbose", "help" ]:
64 if not Cnf.has_key("Import-Users-From-Passwd::Options::%s" % (i)):
65 Cnf["Import-Users-From-Passwd::Options::%s" % (i)] = ""
67 arguments = apt_pkg.ParseCommandLine(Cnf,Arguments,sys.argv)
68 Options = Cnf.SubTree("Import-Users-From-Passwd::Options")
73 utils.warn("dak import-users-from-passwd takes no non-option arguments.")
76 projectB = pg.connect(Cnf["DB::Name"], Cnf["DB::Host"], int(Cnf["DB::Port"]))
77 valid_gid = int(Cnf.get("Import-Users-From-Passwd::ValidGID",""))
80 for entry in pwd.getpwall():
83 if valid_gid and gid != valid_gid:
84 if Options["Verbose"]:
85 print "Skipping %s (GID %s != Valid GID %s)." % (uname, gid, valid_gid)
87 passwd_unames[uname] = ""
90 q = projectB.query("SELECT usename FROM pg_user")
94 postgres_unames[uname] = ""
96 known_postgres_unames = {}
97 for i in Cnf.get("Import-Users-From-Passwd::KnownPostgres","").split(","):
99 known_postgres_unames[uname] = ""
101 keys = postgres_unames.keys()
104 if not passwd_unames.has_key(uname)and not known_postgres_unames.has_key(uname):
105 print "W: %s is in Postgres but not the passwd file or list of known Postgres users." % (uname)
107 keys = passwd_unames.keys()
110 if not postgres_unames.has_key(uname):
111 if not Options["Quiet"]:
112 print "Creating %s user in Postgres." % (uname)
113 if not Options["No-Action"]:
114 q = projectB.query('CREATE USER "%s"' % (uname))
116 #######################################################################################
118 if __name__ == '__main__':