]> granicus.if.org Git - pgbouncer/commitdiff
Sample script for generating users file from database
authorMarko Kreen <markokr@gmail.com>
Fri, 11 Jun 2010 13:11:47 +0000 (16:11 +0300)
committerMarko Kreen <markokr@gmail.com>
Fri, 11 Jun 2010 13:11:47 +0000 (16:11 +0300)
Makefile
etc/mkauth.py [new file with mode: 0755]

index 5eeddc9923fc09611cffa486d88b087ad47bbd1b..b505bb7ec87c9d66d526c8878af7d738b41b53e2 100644 (file)
--- 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 (executable)
index 0000000..7cc9d8b
--- /dev/null
@@ -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)
+