-
-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathenvchain_linux.c
207 lines (188 loc) · 6.37 KB
/
envchain_linux.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#include "envchain.h"
#include <libsecret/secret.h>
#include <stdio.h>
static const SecretSchema *envchain_get_schema(void) {
static const SecretSchema the_schema = {
.name = "envchain.EnvironmentVariable",
.flags = SECRET_SCHEMA_NONE,
.attributes =
{
{.name = "name", .type = SECRET_SCHEMA_ATTRIBUTE_STRING},
{.name = "key", .type = SECRET_SCHEMA_ATTRIBUTE_STRING},
{NULL, 0},
},
};
return &the_schema;
}
static GList *search_unlocked_collection(const char *name, GError **error) {
SecretService *service =
secret_service_get_sync(SECRET_SERVICE_LOAD_COLLECTIONS, NULL, error);
if (*error != NULL) {
return NULL;
}
SecretCollection *collection = secret_collection_for_alias_sync(
service, SECRET_COLLECTION_DEFAULT, SECRET_COLLECTION_LOAD_ITEMS, NULL,
error);
g_object_unref(service);
if (*error != NULL) {
return NULL;
}
if (collection == NULL) {
// Default collection does not exist
return NULL;
}
if (secret_collection_get_locked(collection)) {
GList *objects = g_list_append(NULL, collection);
GList *unlocked = NULL;
const gint n =
secret_service_unlock_sync(secret_collection_get_service(collection),
objects, NULL, &unlocked, error);
g_list_free(objects);
g_list_free(unlocked);
g_object_unref(collection);
if (*error != NULL) {
return NULL;
}
if (n == 0) {
fprintf(stderr, "%s: failed to unlock collection\n", envchain_name);
}
/* reload */
secret_service_disconnect();
service =
secret_service_get_sync(SECRET_SERVICE_LOAD_COLLECTIONS, NULL, error);
if (*error != NULL) {
return NULL;
}
collection = secret_collection_for_alias_sync(
service, SECRET_COLLECTION_DEFAULT, SECRET_COLLECTION_LOAD_ITEMS, NULL,
error);
g_object_unref(service);
if (*error != NULL) {
return NULL;
}
}
GHashTable *attributes =
g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
if (name != NULL) {
g_hash_table_insert(attributes, g_strdup("name"), g_strdup(name));
}
GList *items =
secret_collection_search_sync(collection, envchain_get_schema(),
attributes, SECRET_SEARCH_ALL, NULL, error);
g_hash_table_unref(attributes);
g_object_unref(collection);
return items;
}
int envchain_search_namespaces(envchain_namespace_search_callback callback,
void *data) {
GError *error = NULL;
GList *items = search_unlocked_collection(NULL, &error);
if (error != NULL) {
fprintf(stderr, "%s: search_unlocked_collection failed with %d: %s\n",
envchain_name, error->code, error->message);
g_error_free(error);
return 1;
}
GList *iter;
GHashTable *names =
g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL);
for (iter = items; iter != NULL; iter = iter->next) {
SecretItem *item = iter->data;
GHashTable *attrs = secret_item_get_attributes(item);
char *name = g_strdup(g_hash_table_lookup(attrs, "name"));
if (g_hash_table_add(names, name)) {
callback(name, data);
}
g_hash_table_unref(attrs);
}
g_hash_table_unref(names);
g_list_free(items);
return 0;
}
// Returns FALSE if the error is retryable
static gboolean try_search_items(const char *name,
envchain_search_callback callback, void *data,
int *result) {
GError *error = NULL;
GList *items = search_unlocked_collection(name, &error);
if (error != NULL) {
fprintf(stderr, "%s: search_unlocked_collection failed with %d: %s\n",
envchain_name, error->code, error->message);
g_error_free(error);
*result = 1;
return TRUE;
}
GList *iter;
for (iter = items; iter != NULL; iter = iter->next) {
SecretItem *item = iter->data;
GHashTable *attrs = secret_item_get_attributes(item);
char *key = g_hash_table_lookup(attrs, "key");
if (!secret_item_load_secret_sync(item, NULL, &error)) {
const int error_code = error->code;
g_error_free(error);
g_list_free(items);
if (error_code == SECRET_ERROR_PROTOCOL) {
return FALSE;
} else {
fprintf(stderr, "%s: secret_item_load_secret_sync failed with %d: %s\n",
envchain_name, error->code, error->message);
*result = 1;
return TRUE;
}
}
SecretValue *value = secret_item_get_secret(item);
callback(key, secret_value_get_text(value), data);
secret_value_unref(value);
g_hash_table_unref(attrs);
}
g_list_free(items);
*result = 0;
return TRUE;
}
int envchain_search_values(const char *name, envchain_search_callback callback,
void *data) {
/*
* Retry when org.freedesktop.Secret.Item.GetSecret (secret_item_load_secret_sync)
* fails. It occasionally fails with a message "** Message: received an
* invalid or unencryptable secret".
*/
for (int retry_count = 0; retry_count < 3; ++retry_count) {
int result = -1;
if (try_search_items(name, callback, data, &result)) {
return result;
}
secret_service_disconnect();
}
fprintf(stderr, "%s: too many secret_item_load_secret_sync failures\n",
envchain_name);
return 1;
}
void envchain_save_value(const char *name, const char *key, char *value,
int require_passphrase) {
if (require_passphrase == 1) {
fprintf(
stderr,
"%s: Sorry, `--require-passphrase' is unsupported on this platform\n",
envchain_name);
return;
}
GError *error = NULL;
secret_password_store_sync(envchain_get_schema(), SECRET_COLLECTION_DEFAULT,
key, value, NULL, &error, "name", name, "key", key,
NULL);
if (error != NULL) {
fprintf(stderr, "%s: secret_password_store_sync failed with %d: %s\n",
envchain_name, error->code, error->message);
g_error_free(error);
}
}
void envchain_delete_value(const char *name, const char *key) {
GError *error = NULL;
secret_password_clear_sync(envchain_get_schema(), NULL, &error,
"name", name, "key", key, NULL);
if (error != NULL) {
fprintf(stderr, "%s: secret_password_clear_sync failed with %d: %s\n",
envchain_name, error->code, error->message);
g_error_free(error);
}
}