From 4fd0e3907b52492565315ffaa5f9ca8f0b029b8e Mon Sep 17 00:00:00 2001 From: Marko Kreen Date: Fri, 11 Jun 2010 16:11:47 +0300 Subject: [PATCH] Sample script for generating users file from database --- Makefile | 2 +- etc/mkauth.py | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) create mode 100755 etc/mkauth.py diff --git a/Makefile b/Makefile index 5eeddc9..b505bb7 100644 --- a/Makefile +++ b/Makefile @@ -11,7 +11,7 @@ HDRS = client.h loader.h objects.h pooler.h proto.h sbuf.h server.h util.h \ DOCS = doc/overview.txt doc/usage.txt doc/config.txt doc/todo.txt MANPAGES = doc/pgbouncer.1 doc/pgbouncer.5 DATA = README NEWS AUTHORS COPYRIGHT etc/pgbouncer.ini etc/userlist.txt Makefile \ - config.mak.in include/config.h.in \ + config.mak.in include/config.h.in etc/mkauth.py \ configure configure.ac debian/packages debian/changelog doc/Makefile \ test/Makefile test/asynctest.c test/conntest.sh test/ctest6000.ini \ test/ctest7000.ini test/run-conntest.sh test/stress.py test/test.ini \ diff --git a/etc/mkauth.py b/etc/mkauth.py new file mode 100755 index 0000000..7cc9d8b --- /dev/null +++ b/etc/mkauth.py @@ -0,0 +1,36 @@ +#! /usr/bin/env python + +import sys, os, tempfile, psycopg2 + +if len(sys.argv) != 3: + print 'usage: mkauth DSTFN CONNSTR' + sys.exit(1) + +# read old file +fn = sys.argv[1] +try: + old = open(fn, 'r').read() +except IOError: + old = '' + +# create new file data +db = psycopg2.connect(sys.argv[2]) +curs = db.cursor() +curs.execute("select usename, passwd from pg_shadow order by 1") +lines = [] +for user, psw in curs.fetchall(): + user = user.replace('"', '""') + if not psw: psw = '' + psw = psw.replace('"', '""') + lines.append('"%s" "%s" ""\n' % (user, psw)) +db.commit() +cur = "".join(lines) + +# if changed, replace data securely +if old != cur: + fd, tmpfn = tempfile.mkstemp(dir = os.path.split(fn)[0]) + f = os.fdopen(fd, 'w') + f.write(cur) + f.close() + os.rename(tmpfn, fn) + -- 2.40.0