30 DBErr = psycopg2.DatabaseError |
30 DBErr = psycopg2.DatabaseError |
31 else: |
31 else: |
32 DBErr = PgSQL.libpq.DatabaseError |
32 DBErr = PgSQL.libpq.DatabaseError |
33 |
33 |
34 |
34 |
35 def check_opts(opts, err_hdlr): |
35 def check_args(args, err_hdlr): |
36 if not opts.postfix: |
36 if not args.postfix: |
37 err_hdlr('missing Postfix database user name') |
37 err_hdlr('missing Postfix database user name') |
38 if not opts.dovecot: |
38 if not args.dovecot: |
39 err_hdlr('missing Dovecot database user name') |
39 err_hdlr('missing Dovecot database user name') |
40 if opts.askp: |
40 if args.askp: |
41 opts.dbpass = getpass.getpass() |
41 args.dbpass = getpass.getpass() |
42 |
42 |
43 |
43 |
44 def get_dbh(database, user, password, host, port): |
44 def get_dbh(database, user, password, host, port): |
45 if has_psycopg2: |
45 if has_psycopg2: |
46 return psycopg2.connect(database=database, user=user, |
46 return psycopg2.connect(database=database, user=user, |
47 password=password, host=host, port=port) |
47 password=password, host=host, port=port) |
48 return PgSQL.connect(user=user, password=password, host=host, |
48 return PgSQL.connect(user=user, password=password, host=host, |
49 database=database, port=port) |
49 database=database, port=port) |
50 |
50 |
51 |
51 |
52 def get_optparser(): |
52 def get_argparser(): |
53 descr = 'Set permissions for Dovecot and Postfix in the vmm database.' |
53 descr = 'Set permissions for Dovecot and Postfix in the vmm database.' |
54 usage = 'usage: %prog OPTIONS' |
54 parser = ArgumentParser(description=descr, usage='%(prog)s OPTIONS', |
55 parser = OptionParser(description=descr, usage=usage) |
55 formatter_class=ArgumentDefaultsHelpFormatter) |
56 parser.add_option('-a', '--askpass', dest='askp', default=False, |
56 parser.add_argument('-a', '--askpass', dest='askp', |
57 action='store_true', help='Prompt for the database password.') |
57 action='store_true', help='Prompt for the database password.') |
58 parser.add_option('-H', '--host', dest='host', metavar='HOST', |
58 parser.add_argument('-H', '--host', dest='host', metavar='HOST', |
59 default=None, |
|
60 help='Hostname or IP address of the database server. Leave ' + |
59 help='Hostname or IP address of the database server. Leave ' + |
61 'blank in order to use the default Unix-domain socket.') |
60 'blank in order to use the default Unix-domain socket.') |
62 parser.add_option('-n', '--name', dest='name', metavar='NAME', |
61 parser.add_argument('-n', '--name', dest='name', metavar='NAME', |
63 default='mailsys', |
62 default='mailsys', |
64 help='Specifies the name of the database to connect to. ' + |
63 help='Specifies the name of the database to connect to.') |
65 'Default: %default') |
64 parser.add_argument('-p', '--pass', dest="dbpass", metavar='PASS', |
66 parser.add_option('-p', '--pass', dest="dbpass", metavar='PASS', |
65 help='Password for the database connection.') |
67 default=None, help='Password for the database connection.') |
66 parser.add_argument('-P', '--port', dest='port', metavar='PORT', |
68 parser.add_option('-P', '--port', dest='port', metavar='PORT', type='int', |
67 type=int, default=5432, |
69 default=5432, |
|
70 help='Specifies the TCP port or the local Unix-domain socket ' + |
68 help='Specifies the TCP port or the local Unix-domain socket ' + |
71 'file extension on which the server is listening for ' + |
69 'file extension on which the server is listening for ' + |
72 'connections. Default: %default') |
70 'connections.') |
73 parser.add_option('-U', '--user', dest='user', metavar='USER', |
71 parser.add_argument('-U', '--user', dest='user', metavar='USER', |
74 default=getpass.getuser(), |
72 default=getpass.getuser(), |
75 help='Connect to the database as the user USER instead of the ' + |
73 help='Connect to the database as the user USER instead of the ') |
76 'default: %default') |
74 parser.add_argument('-D', '--dovecot', dest='dovecot', metavar='USER', |
77 parser.add_option('-D', '--dovecot', dest='dovecot', metavar='USER', |
|
78 default='dovecot', |
75 default='dovecot', |
79 help='Database user name of the Dovecot database user. Default: ' + |
76 help='Database user name of the Dovecot database user.') |
80 '%default') |
77 parser.add_argument('-M', '--postfix', dest='postfix', metavar='USER', |
81 parser.add_option('-M', '--postfix', dest='postfix', metavar='USER', |
|
82 default='postfix', |
78 default='postfix', |
83 help='Database user name of the Postfix (MTA) database user. ' + |
79 help='Database user name of the Postfix (MTA) database user.') |
84 'Default: %default') |
|
85 return parser |
80 return parser |
86 |
81 |
87 |
82 |
88 def set_permissions(dbh, dc_vers, dovecot, postfix): |
83 def set_permissions(dbh, dc_vers, dovecot, postfix): |
89 dc_rw = ('userquota_11', 'userquota')[dc_vers == 12] |
84 dc_rw = ('userquota_11', 'userquota')[dc_vers == 12] |
153 dbh.close() |
148 dbh.close() |
154 raise SystemExit(1) |
149 raise SystemExit(1) |
155 |
150 |
156 |
151 |
157 if __name__ == '__main__': |
152 if __name__ == '__main__': |
158 optparser = get_optparser() |
153 argparser = get_argparser() |
159 opts, args = optparser.parse_args() |
154 args = argparser.parse_args() |
160 check_opts(opts, optparser.error) |
155 check_args(args, argparser.error) |
161 dbh = get_dbh(opts.name, opts.user, opts.dbpass, opts.host, opts.port) |
156 dbh = get_dbh(args.name, args.user, args.dbpass, args.host, args.port) |
162 versions = {} |
157 versions = {} |
163 set_versions(dbh, versions) |
158 set_versions(dbh, versions) |
164 if versions['pgsql'] < 80400: |
159 if versions['pgsql'] < 80400: |
165 set_permissions(dbh, versions['dovecot'], opts.dovecot, opts.postfix) |
160 set_permissions(dbh, versions['dovecot'], args.dovecot, args.postfix) |
166 else: |
161 else: |
167 set_permissions84(dbh, versions['dovecot'], opts.dovecot, opts.postfix) |
162 set_permissions84(dbh, versions['dovecot'], args.dovecot, args.postfix) |
168 dbh.commit() |
163 dbh.commit() |
169 dbh.close() |
164 dbh.close() |