This repository has been archived by the owner on Mar 11, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gnome-keyring-query.c
150 lines (119 loc) · 3.67 KB
/
gnome-keyring-query.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#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);
}