]> git.decadent.org.uk Git - dak.git/blob - dak/acl.py
Merge branch 'dak-unpriv' into merge
[dak.git] / dak / acl.py
1 #! /usr/bin/env python
2 #
3 # Copyright (C) 2012, Ansgar Burchardt <ansgar@debian.org>
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License along
16 # with this program; if not, write to the Free Software Foundation, Inc.,
17 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18
19 import apt_pkg
20 import sys
21
22 from daklib.config import Config
23 from daklib.dbconn import DBConn, Fingerprint, Uid, ACL
24
25 def usage():
26     print """Usage: dak acl set-fingerprints <acl-name>
27
28 Reads list of fingerprints from stdin and sets the ACL <acl-name> to these.
29 """
30
31 def get_fingerprint(entry, session):
32     """get fingerprint for given ACL entry
33
34     The entry is a string in one of these formats::
35
36         uid:<uid>
37         name:<name>
38         fpr:<fingerprint>
39
40     @type  entry: string
41     @param entry: ACL entry
42
43     @param session: database session
44
45     @rtype:  L{daklib.dbconn.Fingerprint} or C{None}
46     @return: fingerprint for the entry
47     """
48     field, value = entry.split(":", 1)
49     q = session.query(Fingerprint)
50
51     if field == 'uid':
52         q = q.join(Fingerprint.uid).filter(Uid.uid == value)
53     elif field == 'name':
54         q = q.join(Fingerprint.uid).filter(Uid.name == value)
55     elif field == 'fpr':
56         q = q.filter(Fingerprint.fingerprint == value)
57
58     return q.all()
59
60 def acl_set_fingerprints(acl_name, entries):
61     session = DBConn().session()
62     acl = session.query(ACL).filter_by(name=acl_name).one()
63
64     acl.fingerprints.clear()
65     for entry in entries:
66         entry = entry.strip()
67         fps = get_fingerprint(entry, session)
68         if len(fps) == 0:
69             print "Unknown key for '{0}'".format(entry)
70         else:
71             acl.fingerprints.update(fps)
72
73     session.commit()
74
75 def main(argv=None):
76     if argv is None:
77         argv = sys.argv
78
79     if len(argv) != 3 or argv[1] != 'set-fingerprints':
80         usage()
81         sys.exit(1)
82
83     acl_set_fingerprints(argv[2], sys.stdin)