在 Ubuntu 14.04 上使用 Python 2.7 GTK3 获取 GnomeKeyring 密钥的属性

Get attributes of GnomeKeyring key with Python 2.7 GTK3 on Ubuntu 14.04

我正在使用 Linux Mint 17,基于 Ubuntu 14.04

我已经安装了:

python-gnomekeyring 2.3.2 python-gi 3.12 python-gtk2 2.24

我想使用 GnomeKeyring。我保存并读取了密钥名称和密钥密码,但我无法访问属性。当我尝试访问它们时,我总是得到:

*** Error in `python': double free or corruption (out)

这是我的代码,我今天创建它是为了使用 GnomeKeyring:

#!/usr/bin/python
# -*- coding: utf8 -*-


import gi
gi.require_version('Gtk', '3.0')
gi.require_version('GnomeKeyring', '1.0')
from gi.repository import Gtk
from gi.repository import GnomeKeyring

import logging
from pprint import pprint


class KeyringManager():

    def __init__(self):
        pass

    def create_key(self, keyring_name, key_name, attributes=None):
        logging.warning("create_key: %s / %s", keyring_name, key_name)
        self.unlock_keyring(keyring_name)
        key_id = self.get_key_id(keyring_name, key_name)
        if key_id:
            logging.warning("key already exists, overwriting now, press return key to continue")
            raw_input()
        attrs = GnomeKeyring.Attribute.list_new()
        for key, value in attributes.items():
            GnomeKeyring.Attribute.list_append_string(attrs, key, value)
        GnomeKeyring.item_create_sync(keyring_name, GnomeKeyring.ItemType.GENERIC_SECRET, key_name, attrs, raw_input("please enter password for key '{}': ".format(key_name)), True)

    def unlock_keyring(self, keyring_name):
        logging.warning("unlock_keyring: %s", keyring_name)
        # check if keyring exists
        (result, info) = GnomeKeyring.get_info_sync(keyring_name)
        if result == GnomeKeyring.Result.NO_SUCH_KEYRING:
            logging.warning("keyring does not exists: %s", keyring_name)
            self.create_keyring(keyring_name)
        result = GnomeKeyring.unlock_sync(keyring_name, None)
        pprint(result.value_name)

    def create_keyring(self, keyring_name):
        logging.warning("create_keyring: %s", keyring_name)
        result = GnomeKeyring.create_sync(keyring_name, raw_input("please enter passwort for new keyring '{}': ".format(keyring_name)))
        pprint(result)

    def get_key_id(self, keyring_name, key_name):
        logging.warning("get_key_id: %s / %s", keyring_name, key_name)
        (result, key_ids) = GnomeKeyring.list_item_ids_sync(keyring_name)
        for key_id in key_ids:
            (result, item) = GnomeKeyring.item_get_info_sync(keyring_name, key_id)
            if item.get_display_name() == key_name:
                return key_id

    def get_key_info(self, keyring_name, key_name):
        logging.warning("get_key_info: %s / %s", keyring_name, key_name)
        self.unlock_keyring(keyring_name)
        key_id = self.get_key_id(keyring_name, key_name)
        GnomeKeyring.item_get_info_sync (keyring_name, key_id)
        (result, item) = GnomeKeyring.item_get_info_sync(keyring_name, key_id)
        self.key_name = item.get_display_name()
        self.secret = item.get_secret()
        self.attrs = GnomeKeyring.Attribute.list_new()
        result = GnomeKeyring.item_get_attributes_sync(keyring_name, key_id, self.attrs)#
        try:
            for attr in GnomeKeyring.Attribute.list_to_glist(self.attrs):
                ## *** Error in `python': double free or corruption (out)
                print '%s = %s ' % (attr.name, attr.get_string())
        except Exception, e:
            logging.error(e.message, exc_info=True)
            exit(e.message)

    def delete_key(self, keyring_name="login", key_name="==THENAME"):
        logging.warning("delete_key: %s / %s", keyring_name, key_name)
        raw_input("press return key to continue")
        logging.error("not implemented yet")
        #GnomeKeyring.item_delete_sync(keyring, id)


#KeyringManager().create_key("my_keyring", "my_key", {"user":"carl", "url":"apple.de", "port":"1212"})
KeyringManager().get_key_info("my_keyring", "my_key")

一些链接:

https://github.com/lwindolf/liferea/blob/master/plugins/gnome-keyring.py

这是有效的:

def get_attributes(self, keyring_name, key_id):
    attributes = {}
    attrs_list = GnomeKeyring.Attribute.list_new()
    (result, items )= GnomeKeyring.find_items_sync(GnomeKeyring.ItemType.GENERIC_SECRET, attrs_list)
    for item in items:
        if item.keyring == keyring_name:
            if item.item_id == key_id:
                for attr in GnomeKeyring.Attribute.list_to_glist(item.attributes):
                    if attr.name != "xdg:schema":
                        attributes[attr.name] = attr.get_string()
    return attributes

我看到你已经澄清了主要问题。

错误是因为您使用 GnomeKeyring.Attribute.list_new() 作为 GnomeKeyring.Attribute.list_to_glist(…) 的参数,而不是 GnomeKeyring.Found 的 item.attributes。