pgsql/set-permissions.py
author Pascal Volk <user@localhost.localdomain.org>
Sat, 09 Feb 2013 17:24:12 +0000
branchv0.7.x
changeset 693 2e19ab98118a
parent 645 fcc3f100e56a
child 694 b1bfd4d1d9c0
permissions -rw-r--r--
pgsql/set-permissions: use argparse instead of optparse.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
645
fcc3f100e56a Updated shebang to use python3 instead of python.
Pascal Volk <user@localhost.localdomain.org>
parents: 644
diff changeset
     1
#!/usr/bin/env python3
556
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
     2
# coding: utf-8
693
2e19ab98118a pgsql/set-permissions: use argparse instead of optparse.
Pascal Volk <user@localhost.localdomain.org>
parents: 645
diff changeset
     3
# Copyright 2012 - 2013, Pascal Volk
556
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
     4
# See COPYING for distribution information.
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
     5
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
     6
"""
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
     7
    Use this script in order to set database permissions for your Dovecot
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
     8
    and Postfix database users.
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
     9
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    10
    Run `python set-permissions.py -h` for details.
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    11
"""
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    12
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    13
import getpass
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    14
import sys
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    15
693
2e19ab98118a pgsql/set-permissions: use argparse instead of optparse.
Pascal Volk <user@localhost.localdomain.org>
parents: 645
diff changeset
    16
from argparse import ArgumentDefaultsHelpFormatter, ArgumentParser
556
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    17
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    18
has_psycopg2 = False
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    19
try:
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    20
    import psycopg2
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    21
    has_psycopg2 = True
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    22
except ImportError:
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    23
    try:
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    24
        from pyPgSQL import PgSQL
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    25
    except ImportError:
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    26
        sys.stderr.write('error: no suitable database module found\n')
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    27
        raise SystemExit(1)
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    28
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    29
if has_psycopg2:
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    30
    DBErr = psycopg2.DatabaseError
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    31
else:
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    32
    DBErr = PgSQL.libpq.DatabaseError
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    33
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    34
693
2e19ab98118a pgsql/set-permissions: use argparse instead of optparse.
Pascal Volk <user@localhost.localdomain.org>
parents: 645
diff changeset
    35
def check_args(args, err_hdlr):
2e19ab98118a pgsql/set-permissions: use argparse instead of optparse.
Pascal Volk <user@localhost.localdomain.org>
parents: 645
diff changeset
    36
    if not args.postfix:
556
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    37
        err_hdlr('missing Postfix database user name')
693
2e19ab98118a pgsql/set-permissions: use argparse instead of optparse.
Pascal Volk <user@localhost.localdomain.org>
parents: 645
diff changeset
    38
    if not args.dovecot:
556
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    39
        err_hdlr('missing Dovecot database user name')
693
2e19ab98118a pgsql/set-permissions: use argparse instead of optparse.
Pascal Volk <user@localhost.localdomain.org>
parents: 645
diff changeset
    40
    if args.askp:
2e19ab98118a pgsql/set-permissions: use argparse instead of optparse.
Pascal Volk <user@localhost.localdomain.org>
parents: 645
diff changeset
    41
        args.dbpass = getpass.getpass()
556
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    42
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    43
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    44
def get_dbh(database, user, password, host, port):
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    45
    if has_psycopg2:
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    46
        return psycopg2.connect(database=database, user=user,
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    47
                                password=password, host=host, port=port)
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    48
    return PgSQL.connect(user=user, password=password, host=host,
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    49
                         database=database, port=port)
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    50
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    51
693
2e19ab98118a pgsql/set-permissions: use argparse instead of optparse.
Pascal Volk <user@localhost.localdomain.org>
parents: 645
diff changeset
    52
def get_argparser():
556
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    53
    descr = 'Set permissions for Dovecot and Postfix in the vmm database.'
693
2e19ab98118a pgsql/set-permissions: use argparse instead of optparse.
Pascal Volk <user@localhost.localdomain.org>
parents: 645
diff changeset
    54
    parser = ArgumentParser(description=descr, usage='%(prog)s OPTIONS',
2e19ab98118a pgsql/set-permissions: use argparse instead of optparse.
Pascal Volk <user@localhost.localdomain.org>
parents: 645
diff changeset
    55
                            formatter_class=ArgumentDefaultsHelpFormatter)
2e19ab98118a pgsql/set-permissions: use argparse instead of optparse.
Pascal Volk <user@localhost.localdomain.org>
parents: 645
diff changeset
    56
    parser.add_argument('-a', '--askpass', dest='askp',
556
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    57
            action='store_true', help='Prompt for the database password.')
693
2e19ab98118a pgsql/set-permissions: use argparse instead of optparse.
Pascal Volk <user@localhost.localdomain.org>
parents: 645
diff changeset
    58
    parser.add_argument('-H', '--host', dest='host', metavar='HOST',
556
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    59
            help='Hostname or IP address of the database server. Leave ' +
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    60
                 'blank in order to use the default Unix-domain socket.')
693
2e19ab98118a pgsql/set-permissions: use argparse instead of optparse.
Pascal Volk <user@localhost.localdomain.org>
parents: 645
diff changeset
    61
    parser.add_argument('-n', '--name', dest='name', metavar='NAME',
556
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    62
            default='mailsys',
693
2e19ab98118a pgsql/set-permissions: use argparse instead of optparse.
Pascal Volk <user@localhost.localdomain.org>
parents: 645
diff changeset
    63
            help='Specifies the name of the database to connect to.')
2e19ab98118a pgsql/set-permissions: use argparse instead of optparse.
Pascal Volk <user@localhost.localdomain.org>
parents: 645
diff changeset
    64
    parser.add_argument('-p', '--pass', dest="dbpass", metavar='PASS',
2e19ab98118a pgsql/set-permissions: use argparse instead of optparse.
Pascal Volk <user@localhost.localdomain.org>
parents: 645
diff changeset
    65
            help='Password for the database connection.')
2e19ab98118a pgsql/set-permissions: use argparse instead of optparse.
Pascal Volk <user@localhost.localdomain.org>
parents: 645
diff changeset
    66
    parser.add_argument('-P', '--port', dest='port', metavar='PORT',
2e19ab98118a pgsql/set-permissions: use argparse instead of optparse.
Pascal Volk <user@localhost.localdomain.org>
parents: 645
diff changeset
    67
            type=int, default=5432,
556
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    68
            help='Specifies the TCP port or the local Unix-domain socket ' +
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    69
                 'file extension on which the server is listening for ' +
693
2e19ab98118a pgsql/set-permissions: use argparse instead of optparse.
Pascal Volk <user@localhost.localdomain.org>
parents: 645
diff changeset
    70
                 'connections.')
2e19ab98118a pgsql/set-permissions: use argparse instead of optparse.
Pascal Volk <user@localhost.localdomain.org>
parents: 645
diff changeset
    71
    parser.add_argument('-U', '--user', dest='user', metavar='USER',
556
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    72
            default=getpass.getuser(),
693
2e19ab98118a pgsql/set-permissions: use argparse instead of optparse.
Pascal Volk <user@localhost.localdomain.org>
parents: 645
diff changeset
    73
            help='Connect to the database as the user USER instead of the ')
2e19ab98118a pgsql/set-permissions: use argparse instead of optparse.
Pascal Volk <user@localhost.localdomain.org>
parents: 645
diff changeset
    74
    parser.add_argument('-D', '--dovecot', dest='dovecot', metavar='USER',
556
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    75
            default='dovecot',
693
2e19ab98118a pgsql/set-permissions: use argparse instead of optparse.
Pascal Volk <user@localhost.localdomain.org>
parents: 645
diff changeset
    76
            help='Database user name of the Dovecot database user.')
2e19ab98118a pgsql/set-permissions: use argparse instead of optparse.
Pascal Volk <user@localhost.localdomain.org>
parents: 645
diff changeset
    77
    parser.add_argument('-M', '--postfix', dest='postfix', metavar='USER',
556
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    78
            default='postfix',
693
2e19ab98118a pgsql/set-permissions: use argparse instead of optparse.
Pascal Volk <user@localhost.localdomain.org>
parents: 645
diff changeset
    79
            help='Database user name of the Postfix (MTA)  database user.')
556
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    80
    return parser
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    81
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    82
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    83
def set_permissions(dbh, dc_vers, dovecot, postfix):
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    84
    dc_rw = ('userquota_11', 'userquota')[dc_vers == 12]
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    85
    dbc = dbh.cursor()
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    86
    dbc.execute('GRANT SELECT ON domain_data, domain_name, mailboxformat, '
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    87
                'maillocation, quotalimit, service_set, users TO %s' % dovecot)
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    88
    dbc.execute('GRANT SELECT, INSERT, UPDATE, DELETE ON %s TO %s' %
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    89
                (dc_rw, dovecot))
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    90
    dbc.execute('GRANT SELECT ON alias, catchall, domain_data, domain_name, '
558
2fcf12fdecc2 pgsql/set-permissions.py: Extended permissions.
Pascal Volk <user@localhost.localdomain.org>
parents: 556
diff changeset
    91
                'maillocation, postfix_gid, relocated, transport, users TO %s'
2fcf12fdecc2 pgsql/set-permissions.py: Extended permissions.
Pascal Volk <user@localhost.localdomain.org>
parents: 556
diff changeset
    92
                % postfix)
556
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    93
    dbc.close()
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    94
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    95
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    96
def set_permissions84(dbh, dc_vers, dovecot, postfix):
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    97
    dc_rw_tbls = ('userquota_11', 'userquota')[dc_vers == 12]
558
2fcf12fdecc2 pgsql/set-permissions.py: Extended permissions.
Pascal Volk <user@localhost.localdomain.org>
parents: 556
diff changeset
    98
    dc_ro_tbls = 'mailboxformat, maillocation, service_set, quotalimit'
2fcf12fdecc2 pgsql/set-permissions.py: Extended permissions.
Pascal Volk <user@localhost.localdomain.org>
parents: 556
diff changeset
    99
    pf_ro_tbls = 'alias, catchall, postfix_gid, relocated, transport'
556
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   100
    db = dict(dovecot=dovecot, postfix=postfix)
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   101
    db['dovecot_tbls'] = {
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   102
        'domain_data': 'domaindir, gid, qid, ssid',
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   103
        'domain_name': 'domainname, gid',
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   104
        'users': 'gid, local_part, mid, passwd, qid, ssid, uid',
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   105
    }
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   106
    db['postfix_tbls'] = {
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   107
        'domain_data': 'domaindir, gid, tid',
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   108
        'domain_name': 'domainname, gid',
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   109
        'maillocation': 'directory, mid',
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   110
        'users': 'gid, local_part, mid, tid, uid',
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   111
    }
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   112
    dbc = dbh.cursor()
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   113
    dbc.execute('GRANT SELECT, INSERT, UPDATE, DELETE ON %s TO %s' %
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   114
                (dc_rw_tbls, db['dovecot']))
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   115
    dbc.execute('GRANT SELECT ON %s TO %s' % (dc_ro_tbls, db['dovecot']))
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   116
    dbc.execute('GRANT SELECT ON %s TO %s' % (pf_ro_tbls, db['postfix']))
644
d879f915c1c5 pgsql/set-permissions.py: Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents: 558
diff changeset
   117
    for table, columns in db['dovecot_tbls'].items():
556
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   118
        dbc.execute('GRANT SELECT (%s) ON %s TO %s' % (columns, table,
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   119
                                                       db['dovecot']))
644
d879f915c1c5 pgsql/set-permissions.py: Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents: 558
diff changeset
   120
    for table, columns in db['postfix_tbls'].items():
556
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   121
        dbc.execute('GRANT SELECT (%s) ON %s TO %s' % (columns, table,
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   122
                                                       db['postfix']))
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   123
    dbc.close()
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   124
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   125
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   126
def set_versions(dbh, versions):
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   127
    dbc = dbh.cursor()
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   128
    if hasattr(dbh, 'server_version'):
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   129
        versions['pgsql'] = dbh.server_version
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   130
    else:
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   131
        try:
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   132
            dbc.execute("SELECT current_setting('server_version_num')")
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   133
            versions['pgsql'] = int(dbc.fetchone()[0])
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   134
        except DBErr:
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   135
            versions['pgsql'] = 80199
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   136
    dbc.execute("SELECT relname FROM pg_stat_user_tables WHERE relname LIKE "
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   137
                "'userquota%'")
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   138
    res = dbc.fetchall()
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   139
    dbc.close()
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   140
    tbls = [tbl[0] for tbl in res]
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   141
    if 'userquota' in tbls:
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   142
        versions['dovecot'] = 12
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   143
    elif 'userquota_11' in tbls:
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   144
        versions['dovecot'] = 11
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   145
    else:
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   146
        sys.stderr.write('error: no userquota table found\nis "' + dbh.dsn +
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   147
                         '" correct? is the database up to date?\n')
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   148
        dbh.close()
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   149
        raise SystemExit(1)
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   150
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   151
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   152
if __name__ == '__main__':
693
2e19ab98118a pgsql/set-permissions: use argparse instead of optparse.
Pascal Volk <user@localhost.localdomain.org>
parents: 645
diff changeset
   153
    argparser = get_argparser()
2e19ab98118a pgsql/set-permissions: use argparse instead of optparse.
Pascal Volk <user@localhost.localdomain.org>
parents: 645
diff changeset
   154
    args = argparser.parse_args()
2e19ab98118a pgsql/set-permissions: use argparse instead of optparse.
Pascal Volk <user@localhost.localdomain.org>
parents: 645
diff changeset
   155
    check_args(args, argparser.error)
2e19ab98118a pgsql/set-permissions: use argparse instead of optparse.
Pascal Volk <user@localhost.localdomain.org>
parents: 645
diff changeset
   156
    dbh = get_dbh(args.name, args.user, args.dbpass, args.host, args.port)
556
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   157
    versions = {}
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   158
    set_versions(dbh, versions)
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   159
    if versions['pgsql'] < 80400:
693
2e19ab98118a pgsql/set-permissions: use argparse instead of optparse.
Pascal Volk <user@localhost.localdomain.org>
parents: 645
diff changeset
   160
        set_permissions(dbh, versions['dovecot'], args.dovecot, args.postfix)
556
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   161
    else:
693
2e19ab98118a pgsql/set-permissions: use argparse instead of optparse.
Pascal Volk <user@localhost.localdomain.org>
parents: 645
diff changeset
   162
        set_permissions84(dbh, versions['dovecot'], args.dovecot, args.postfix)
556
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   163
    dbh.commit()
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   164
    dbh.close()