~nafai/gwibber/gnomekeyring-fix

708 by Travis B. Hartwell
First pass at fixing LP: #554005
1
from desktopcouch.records.server import CouchDatabase
707.1.4 by Ken VanDine
MapAsync doesn't need to inherit from threading.Thread
2
from const import *
708 by Travis B. Hartwell
First pass at fixing LP: #554005
3
4
import atexit
714 by Travis B. Hartwell
Access mlock/munlock via ctypes.
5
import ctypes
708 by Travis B. Hartwell
First pass at fixing LP: #554005
6
import gnomekeyring
7
8
passwords = {}
9
10
def get_account_passwords():
11
    global passwords
12
13
    accounts = CouchDatabase(COUCH_DB_ACCOUNTS, create=True)
14
    ids = [a['id'] for a in accounts.get_records().rows]
15
16
    for id in ids:
17
        account = dict(accounts.get_record(id).items())
18
        for key, val in account.items():
19
            if isinstance(val, str) and val.startswith(":KEYRING:"):
20
                try:
21
                    value = gnomekeyring.find_items_sync(
22
                        gnomekeyring.ITEM_GENERIC_SECRET,
23
                        {"id": str("%s/%s" % (account["_id"], key))})[0].secret
716 by Travis B. Hartwell
Clean-ups per pitti's suggestions.
24
                    mlock(value)
708 by Travis B. Hartwell
First pass at fixing LP: #554005
25
                except gnomekeyring.NoMatchError:
26
                    raise exceptions.GwibberProtocolError("keyring")
27
716 by Travis B. Hartwell
Clean-ups per pitti's suggestions.
28
                passwords[id] = value
708 by Travis B. Hartwell
First pass at fixing LP: #554005
29
30
def get_account_password(accountid):
31
    global passwords
32
33
    return passwords.get(accountid, None)
34
714 by Travis B. Hartwell
Access mlock/munlock via ctypes.
35
libc = ctypes.CDLL("libc.so.6")
36
708 by Travis B. Hartwell
First pass at fixing LP: #554005
37
def mlock(var):
714 by Travis B. Hartwell
Access mlock/munlock via ctypes.
38
    libc.mlock(var, len(var))
708 by Travis B. Hartwell
First pass at fixing LP: #554005
39
40
def munlock(var):
714 by Travis B. Hartwell
Access mlock/munlock via ctypes.
41
    libc.munlock(var, len(var))
708 by Travis B. Hartwell
First pass at fixing LP: #554005
42
43
def munlock_passwords():
44
    global passwords
714 by Travis B. Hartwell
Access mlock/munlock via ctypes.
45
46
    for acctid in passwords.keys():
715 by Travis B. Hartwell
Call munlock properly
47
        munlock(passwords[acctid])
708 by Travis B. Hartwell
First pass at fixing LP: #554005
48
49
atexit.register(munlock_passwords)