Package: gnome-keyring
Severity: wishlist
Tags: upstream patch

I would like a command-line tool to get and set passwords from the
keyring included in the gnome-keyring package.

The attached program does this. It is public domain; it was declared as
such in the Gentoo wiki article where I got it.

I wrote the attached manual page as I wanted to package this up for
Debian. (See my ITP, bug #505633.) It's currently GPL v2-or-later. If
the packaging for gnome-keyring is different, I'm open to relicensing;
just ask.

I've filed a request upstream to include this:
http://bugzilla.gnome.org/show_bug.cgi?id=561582

Thanks,
Richard
/*
 * gnome-keyring-query
 *
 * The source code is provided in the PUBLIC DOMAIN, AS-IS, without warranty.
 *
 * This code was downloaded from:
 * http://gentoo-wiki.info/HOWTO_Use_gnome-keyring_to_store_SSH_passphrases
 *
 */

#include <stdlib.h>
#include <stdio.h>

#include <glib.h>
#include "gnome-keyring.h"


#define APPLICATION_NAME "gnome-keyring-query"
#define MAX_PASSWORD_LENGTH 100


char * get_password(const char * name);
int    set_password(const char * name, const char * password);


void usage()
{
    puts("Usage:\n"
         "    " APPLICATION_NAME " <mode> <name>\n"
         "Parameters:\n"
         "    mode     - either 'get' or 'set' (without quotes)\n"
         "    name     - a name to identify the key\n"
         "Notes:\n"
         "    If mode is 'get', then the password is dumped to stdout.\n"
         "    If mode is 'set', then the password is read from stdin.\n");
    exit(EXIT_FAILURE);
}


int main(int argc, char * argv[])
{
    enum
    {
        MODE_GET, MODE_SET
    } mode;
    char * name;
    char * password;
    
    g_set_application_name(APPLICATION_NAME);
    
    if (argc != 3)
        usage();
        
    if (g_ascii_strcasecmp(argv[1], "get") == 0)
        mode = MODE_GET;
    else if (g_ascii_strcasecmp(argv[1], "set") == 0)
        mode = MODE_SET;
    else
    {
        fprintf(stderr, "Invalid mode: %s\n", argv[1]);
        exit(EXIT_FAILURE);
    }
    
    name = argv[2];
    
    switch (mode)
    {
        case MODE_GET:
            password = get_password(name);
            if (!password)
            {
                fprintf(stderr, "Failed to get password: %s\n", name);
                exit(EXIT_FAILURE);
            }
            
            puts(password);
            g_free(password);
            break;
            
        case MODE_SET:
            password = g_malloc(MAX_PASSWORD_LENGTH);
            *password = '\0';
            fgets(password, MAX_PASSWORD_LENGTH, stdin);
            
            if (!set_password(name, password))
            {
                fprintf(stderr, "Failed to set password: %s\n", name);
                exit(EXIT_FAILURE);
            }
            
            g_free(password);
            break;
    }
    
    return 0;
}


char * get_password(const char * name)
{
    GnomeKeyringAttributeList * attributes;
    GnomeKeyringResult result;
    GList * found_list;
    GList * i;
    GnomeKeyringFound * found;
    char * password;
    
    attributes = g_array_new(FALSE, FALSE, sizeof (GnomeKeyringAttribute));
    gnome_keyring_attribute_list_append_string(attributes,
            "name",
            name);
    gnome_keyring_attribute_list_append_string(attributes,
            "magic",
            APPLICATION_NAME);
    
    result = gnome_keyring_find_items_sync(GNOME_KEYRING_ITEM_GENERIC_SECRET,
            attributes,
            &found_list);
    gnome_keyring_attribute_list_free(attributes);
    
    if (result != GNOME_KEYRING_RESULT_OK)
        return NULL;
    
    for (i = found_list; i != NULL; i = i->next)
    {
        found = i->data;
        password = g_strdup(found->secret);
        break;
    }
    gnome_keyring_found_list_free(found_list);
    
    return password;
}


int set_password(const char * name, const char * password)
{
    GnomeKeyringAttributeList * attributes;
    GnomeKeyringResult result;
    guint item_id;
    
    attributes = g_array_new(FALSE, FALSE, sizeof (GnomeKeyringAttribute));
    gnome_keyring_attribute_list_append_string(attributes,
            "name",
            name);
    gnome_keyring_attribute_list_append_string(attributes,
            "magic",
            APPLICATION_NAME);
    
    result = gnome_keyring_item_create_sync(NULL,
            GNOME_KEYRING_ITEM_GENERIC_SECRET,
            name,
            attributes,
            password,
            TRUE,
            &item_id);
    gnome_keyring_attribute_list_free(attributes);
    
    return (result == GNOME_KEYRING_RESULT_OK);
}
\Ri.\" Copyright (c) 2008, Richard Laager <[EMAIL PROTECTED]>
.\"
.\" This is free documentation; you can redistribute it and/or
.\" modify it under the terms of the GNU General Public License as
.\" published by the Free Software Foundation; either version 2 of
.\" the License, or (at your option) any later version.
.\"
.\" The GNU General Public License's references to "object code"
.\" and "executables" are to be interpreted as the output of any
.\" document formatting or typesetting system, including
.\" intermediate and printed output.
.\"
.\" This manual is distributed in the hope that it will be useful,
.\" but WITHOUT ANY WARRANTY; without even the implied warranty of
.\" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
.\" GNU General Public License for more details.
.\"
.\" You should have received a copy of the GNU General Public
.\" License along with this manual; if not, write to the Free
.\" Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
.\" Boston, MA  02111-1301  USA.
.TH gnome-keyring-query 1 "November 13, 2008"
.SH NAME
\fBgnome-keyring-query\fP \- Command\-line utility for querying GNOME Keyring
.SH SYNOPSIS
.TP 5
\fBgnome-keyring-query\fR \fImode\fR \fIname\fR

.SH DESCRIPTION
.PP
\fBgnome-keyring-query\fR is a small, simple command-line utility for setting
and getting passwords from GNOME Keyring.  It is useful in shell scripts.

\fBgnome-keyring-query\fR may be used as follows:
.RS
.br
.BI "gnome-keyring-query " "mode name"
.RE
.PP
If \fImode\fR is get, the password identified by the \fIname\fR key is returned
on stdout.
.PP
If \fImode\fR is set, the password read from stdin is saved to the keyring
under the \fIname\fR key.

.SH AUTHOR
\fBgnome-keyring-query\fR was written by "Koster".
.PP
This manual page was written by Richard Laager <[EMAIL PROTECTED]>,
for the Debian project (but may be used by others).

Attachment: signature.asc
Description: This is a digitally signed message part

Reply via email to