pgsql/set-permissions.py
author Pascal Volk <user@localhost.localdomain.org>
Sat, 15 Feb 2014 21:48:40 +0000
branchv0.7.x
changeset 729 26a6231b9c6a
parent 711 2a75058fc064
permissions -rw-r--r--
VMM/cli/handler: Improved CliHandler.user_password. When possible check the given password scheme before prompting for the password.
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
703
58815c004a61 Updated copyright notices to include the year 2014.
Pascal Volk <user@localhost.localdomain.org>
parents: 558
diff changeset
     3
# Copyright 2012 - 2014, 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
694
b1bfd4d1d9c0 Dropped pyPgSQL support.
Pascal Volk <user@localhost.localdomain.org>
parents: 693
diff changeset
    18
import psycopg2
556
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    19
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    20
693
2e19ab98118a pgsql/set-permissions: use argparse instead of optparse.
Pascal Volk <user@localhost.localdomain.org>
parents: 645
diff changeset
    21
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
    22
    if not args.postfix:
556
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    23
        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
    24
    if not args.dovecot:
556
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    25
        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
    26
    if args.askp:
2e19ab98118a pgsql/set-permissions: use argparse instead of optparse.
Pascal Volk <user@localhost.localdomain.org>
parents: 645
diff changeset
    27
        args.dbpass = getpass.getpass()
556
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
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    30
def get_dbh(database, user, password, host, port):
694
b1bfd4d1d9c0 Dropped pyPgSQL support.
Pascal Volk <user@localhost.localdomain.org>
parents: 693
diff changeset
    31
    return psycopg2.connect(database=database, user=user,
b1bfd4d1d9c0 Dropped pyPgSQL support.
Pascal Volk <user@localhost.localdomain.org>
parents: 693
diff changeset
    32
                            password=password, host=host, port=port)
556
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 get_argparser():
556
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    36
    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
    37
    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
    38
                            formatter_class=ArgumentDefaultsHelpFormatter)
2e19ab98118a pgsql/set-permissions: use argparse instead of optparse.
Pascal Volk <user@localhost.localdomain.org>
parents: 645
diff changeset
    39
    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
    40
            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
    41
    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
    42
            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
    43
                 '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
    44
    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
    45
            default='mailsys',
693
2e19ab98118a pgsql/set-permissions: use argparse instead of optparse.
Pascal Volk <user@localhost.localdomain.org>
parents: 645
diff changeset
    46
            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
    47
    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
    48
            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
    49
    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
    50
            type=int, default=5432,
556
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    51
            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
    52
                 '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
    53
                 'connections.')
2e19ab98118a pgsql/set-permissions: use argparse instead of optparse.
Pascal Volk <user@localhost.localdomain.org>
parents: 645
diff changeset
    54
    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
    55
            default=getpass.getuser(),
693
2e19ab98118a pgsql/set-permissions: use argparse instead of optparse.
Pascal Volk <user@localhost.localdomain.org>
parents: 645
diff changeset
    56
            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
    57
    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
    58
            default='dovecot',
693
2e19ab98118a pgsql/set-permissions: use argparse instead of optparse.
Pascal Volk <user@localhost.localdomain.org>
parents: 645
diff changeset
    59
            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
    60
    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
    61
            default='postfix',
693
2e19ab98118a pgsql/set-permissions: use argparse instead of optparse.
Pascal Volk <user@localhost.localdomain.org>
parents: 645
diff changeset
    62
            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
    63
    return parser
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    64
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    65
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    66
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
    67
    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
    68
    dbc = dbh.cursor()
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    69
    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
    70
                '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
    71
    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
    72
                (dc_rw, dovecot))
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    73
    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
    74
                '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
    75
                % postfix)
556
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    76
    dbc.close()
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    77
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    78
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    79
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
    80
    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
    81
    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
    82
    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
    83
    db = dict(dovecot=dovecot, postfix=postfix)
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    84
    db['dovecot_tbls'] = {
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    85
        'domain_data': 'domaindir, gid, qid, ssid',
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    86
        'domain_name': 'domainname, gid',
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    87
        '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
    88
    }
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    89
    db['postfix_tbls'] = {
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    90
        'domain_data': 'domaindir, gid, tid',
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    91
        'domain_name': 'domainname, gid',
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    92
        'maillocation': 'directory, mid',
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    93
        'users': 'gid, local_part, mid, tid, uid',
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
    dbc = dbh.cursor()
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    96
    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
    97
                (dc_rw_tbls, db['dovecot']))
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    98
    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
    99
    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
   100
    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
   101
        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
   102
                                                       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
   103
    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
   104
        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
   105
                                                       db['postfix']))
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   106
    dbc.close()
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   107
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   108
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   109
def set_versions(dbh, versions):
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   110
    dbc = dbh.cursor()
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   111
    if hasattr(dbh, 'server_version'):
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   112
        versions['pgsql'] = dbh.server_version
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   113
    else:
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   114
        try:
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   115
            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
   116
            versions['pgsql'] = int(dbc.fetchone()[0])
694
b1bfd4d1d9c0 Dropped pyPgSQL support.
Pascal Volk <user@localhost.localdomain.org>
parents: 693
diff changeset
   117
        except psycopg2.DatabaseError:
556
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   118
            versions['pgsql'] = 80199
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   119
    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
   120
                "'userquota%'")
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   121
    res = dbc.fetchall()
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   122
    dbc.close()
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   123
    tbls = [tbl[0] for tbl in res]
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   124
    if 'userquota' in tbls:
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   125
        versions['dovecot'] = 12
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   126
    elif 'userquota_11' in tbls:
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   127
        versions['dovecot'] = 11
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   128
    else:
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   129
        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
   130
                         '" 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
   131
        dbh.close()
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   132
        raise SystemExit(1)
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   133
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   134
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   135
if __name__ == '__main__':
693
2e19ab98118a pgsql/set-permissions: use argparse instead of optparse.
Pascal Volk <user@localhost.localdomain.org>
parents: 645
diff changeset
   136
    argparser = get_argparser()
2e19ab98118a pgsql/set-permissions: use argparse instead of optparse.
Pascal Volk <user@localhost.localdomain.org>
parents: 645
diff changeset
   137
    args = argparser.parse_args()
2e19ab98118a pgsql/set-permissions: use argparse instead of optparse.
Pascal Volk <user@localhost.localdomain.org>
parents: 645
diff changeset
   138
    check_args(args, argparser.error)
2e19ab98118a pgsql/set-permissions: use argparse instead of optparse.
Pascal Volk <user@localhost.localdomain.org>
parents: 645
diff changeset
   139
    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
   140
    versions = {}
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   141
    set_versions(dbh, versions)
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   142
    if versions['pgsql'] < 80400:
693
2e19ab98118a pgsql/set-permissions: use argparse instead of optparse.
Pascal Volk <user@localhost.localdomain.org>
parents: 645
diff changeset
   143
        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
   144
    else:
693
2e19ab98118a pgsql/set-permissions: use argparse instead of optparse.
Pascal Volk <user@localhost.localdomain.org>
parents: 645
diff changeset
   145
        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
   146
    dbh.commit()
fbbb16476c5b pgsql: Added helper script: set-permissions.py.
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   147
    dbh.close()