Skip to content

Commit 72661b3

Browse files
committed
Implemented list functionality for secretservice- linux
Signed-off-by: Avi Vaid <[email protected]>
1 parent 5a8fb21 commit 72661b3

File tree

5 files changed

+70
-2
lines changed

5 files changed

+70
-2
lines changed

osxkeychain/osxkeychain_darwin.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ char *keychain_list(char *** paths, char *** accts, unsigned int *list_l) {
140140
CFStringRef pathTmp = CFDictionaryGetValue(currKey, CFSTR("path"));
141141
CFStringRef acctTmp = CFDictionaryGetValue(currKey, CFSTR("acct"));
142142
if (acctTmp == NULL) {
143-
acctTmp = CFSTR("<unknown>");
143+
acctTmp = CFSTR("account not defined");
144144
}
145145
char * path = (char *) malloc(CFStringGetLength(pathTmp)+1);
146146
path = CFStringToCharArr(pathTmp);
@@ -172,4 +172,4 @@ void freeListData(char *** data, unsigned int length) {
172172
free((*data)[i]);
173173
}
174174
free(*data);
175-
}
175+
}

secretservice/secretservice_linux.c

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,3 +96,44 @@ GError *get(char *server, char **username, char **secret) {
9696
}
9797
return NULL;
9898
}
99+
100+
GError *list(char *** paths, char *** accts, unsigned int *list_l) {
101+
GList *items;
102+
GError *err = NULL;
103+
SecretService *service;
104+
SecretSearchFlags flags = SECRET_SEARCH_LOAD_SECRETS | SECRET_SEARCH_ALL | SECRET_SEARCH_UNLOCK;
105+
GHashTable *attributes;
106+
g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
107+
attributes = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
108+
service = secret_service_get_sync(SECRET_SERVICE_NONE, NULL, &err);
109+
items = secret_service_search_sync(service, NULL, attributes, flags, NULL, &err);
110+
int numKeys = g_list_length(items);
111+
if (err != NULL) {
112+
return err;
113+
}
114+
*paths = (char **) malloc((int)sizeof(char *)*numKeys);
115+
*accts = (char **) malloc((int)sizeof(char *)*numKeys);
116+
// items now contains our keys from the gnome keyring
117+
// we will now put it in our two lists to return it to go
118+
GList *current;
119+
int listNumber = 0;
120+
for(current = items; current!=NULL; current = current->next) {
121+
char *pathTmp = secret_item_get_label(current->data);
122+
// you cannot have a key without a label in the gnome keyring
123+
char *acctTmp = get_username(current->data);
124+
if (acctTmp==NULL) {
125+
acctTmp = "account not defined";
126+
}
127+
char *path = (char *) malloc(strlen(pathTmp));
128+
char *acct = (char *) malloc(strlen(acctTmp));
129+
path = pathTmp;
130+
acct = acctTmp;
131+
(*paths)[listNumber] = (char *) malloc(sizeof(char)*(strlen(path)));
132+
memcpy((*paths)[listNumber], path, sizeof(char)*(strlen(path)));
133+
(*accts)[listNumber] = (char *) malloc(sizeof(char)*(strlen(acct)));
134+
memcpy((*accts)[listNumber], acct, sizeof(char)*(strlen(acct)));
135+
listNumber = listNumber + 1;
136+
}
137+
*list_l = numKeys;
138+
return NULL;
139+
}

secretservice/secretservice_linux.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,26 @@ func (h Secretservice) Get(serverURL string) (string, string, error) {
7878
}
7979
return user, pass, nil
8080
}
81+
82+
func (h Secretservice) List() ([]string, []string, error) {
83+
var pathsC **C.char
84+
defer C.free(unsafe.Pointer(pathsC))
85+
var acctsC **C.char
86+
defer C.free(unsafe.Pointer(acctsC))
87+
var listLenC C.uint
88+
err := C.list(&pathsC, &acctsC, &listLenC)
89+
if err != nil {
90+
defer C.free(unsafe.Pointer(err))
91+
return nil, nil, errors.New("Error from list function in secretservice_linux.c likely due to error in secretservice library")
92+
}
93+
listLen := int(listLenC)
94+
pathTmp := (*[1 << 30]*C.char)(unsafe.Pointer(pathsC))[:listLen:listLen]
95+
acctTmp := (*[1 << 30]*C.char)(unsafe.Pointer(acctsC))[:listLen:listLen]
96+
paths := make([]string, listLen)
97+
accts := make([]string, listLen)
98+
for i := 0; i < listLen; i++ {
99+
paths[i] = C.GoString(pathTmp[i])
100+
accts[i] = C.GoString(acctTmp[i])
101+
}
102+
return paths, accts, nil
103+
}

secretservice/secretservice_linux.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ const SecretSchema *docker_get_schema(void) G_GNUC_CONST;
99
GError *add(char *server, char *username, char *secret);
1010
GError *delete(char *server);
1111
GError *get(char *server, char **username, char **secret);
12+
GError *list(char *** paths, char *** accts, unsigned int *list_l);

secretservice/secretservice_linux_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ func TestSecretServiceHelper(t *testing.T) {
3636
if err := helper.Delete(creds.ServerURL); err != nil {
3737
t.Fatal(err)
3838
}
39+
if _, _, err := helper.List(); err != nil {
40+
t.Fatal(err)
41+
}
3942
}
4043

4144
func TestMissingCredentials(t *testing.T) {

0 commit comments

Comments
 (0)