@@ -7,6 +7,7 @@ const SecretSchema *docker_get_schema(void)
77 static const SecretSchema docker_schema = {
88 "io.docker.Credentials" , SECRET_SCHEMA_NONE ,
99 {
10+ { "label" , SECRET_SCHEMA_ATTRIBUTE_STRING },
1011 { "server" , SECRET_SCHEMA_ATTRIBUTE_STRING },
1112 { "username" , SECRET_SCHEMA_ATTRIBUTE_STRING },
1213 { "docker_cli" , SECRET_SCHEMA_ATTRIBUTE_STRING },
@@ -16,11 +17,12 @@ const SecretSchema *docker_get_schema(void)
1617 return & docker_schema ;
1718}
1819
19- GError * add (char * server , char * username , char * secret ) {
20+ GError * add (char * label , char * server , char * username , char * secret ) {
2021 GError * err = NULL ;
2122
2223 secret_password_store_sync (DOCKER_SCHEMA , SECRET_COLLECTION_DEFAULT ,
2324 server , secret , NULL , & err ,
25+ "label" , label ,
2426 "server" , server ,
2527 "username" , username ,
2628 "docker_cli" , "1" ,
@@ -40,15 +42,15 @@ GError *delete(char *server) {
4042 return NULL ;
4143}
4244
43- char * get_username ( SecretItem * item ) {
45+ char * get_attribute ( const char * attribute , SecretItem * item ) {
4446 GHashTable * attributes ;
4547 GHashTableIter iter ;
4648 gchar * value , * key ;
4749
4850 attributes = secret_item_get_attributes (item );
4951 g_hash_table_iter_init (& iter , attributes );
5052 while (g_hash_table_iter_next (& iter , (void * * )& key , (void * * )& value )) {
51- if (strncmp (key , "username" , strlen (key )) == 0 )
53+ if (strncmp (key , attribute , strlen (key )) == 0 )
5254 return (char * )value ;
5355 }
5456 g_hash_table_unref (attributes );
@@ -71,7 +73,7 @@ GError *get(char *server, char **username, char **secret) {
7173
7274 service = secret_service_get_sync (SECRET_SERVICE_NONE , NULL , & err );
7375 if (err == NULL ) {
74- items = secret_service_search_sync (service , NULL , attributes , flags , NULL , & err );
76+ items = secret_service_search_sync (service , DOCKER_SCHEMA , attributes , flags , NULL , & err );
7577 if (err == NULL ) {
7678 for (l = items ; l != NULL ; l = g_list_next (l )) {
7779 value = secret_item_get_schema_name (l -> data );
@@ -85,7 +87,7 @@ GError *get(char *server, char **username, char **secret) {
8587 * secret = strdup (secret_value_get (secretValue , & length ));
8688 secret_value_unref (secretValue );
8789 }
88- * username = get_username ( l -> data );
90+ * username = get_attribute ( "username" , l -> data );
8991 }
9092 g_list_free_full (items , g_object_unref );
9193 }
@@ -98,44 +100,56 @@ GError *get(char *server, char **username, char **secret) {
98100 return NULL ;
99101}
100102
101- GError * list (char * * * paths , char * * * accts , unsigned int * list_l ) {
103+ GError * list (char * ref_label , char * * * paths , char * * * accts , unsigned int * list_l ) {
102104 GList * items ;
103105 GError * err = NULL ;
104106 SecretService * service ;
105107 SecretSearchFlags flags = SECRET_SEARCH_LOAD_SECRETS | SECRET_SEARCH_ALL | SECRET_SEARCH_UNLOCK ;
106- GHashTable * attributes ;
107- g_hash_table_new_full (g_str_hash , g_str_equal , g_free , g_free );
108- attributes = g_hash_table_new_full (g_str_hash , g_str_equal , g_free , g_free );
108+ GHashTable * attributes = g_hash_table_new_full (g_str_hash , g_str_equal , g_free , g_free );
109+
110+ // List credentials with the right label only
111+ g_hash_table_insert (attributes , g_strdup ("label" ), g_strdup (ref_label ));
112+
109113 service = secret_service_get_sync (SECRET_SERVICE_NONE , NULL , & err );
114+ if (err != NULL ) {
115+ return err ;
116+ }
117+
110118 items = secret_service_search_sync (service , NULL , attributes , flags , NULL , & err );
111119 int numKeys = g_list_length (items );
112120 if (err != NULL ) {
113121 return err ;
114122 }
115- * paths = (char * * ) malloc ((int )sizeof (char * )* numKeys );
116- * accts = (char * * ) malloc ((int )sizeof (char * )* numKeys );
123+
124+ char * * tmp_paths = (char * * ) calloc (1 ,(int )sizeof (char * )* numKeys );
125+ char * * tmp_accts = (char * * ) calloc (1 ,(int )sizeof (char * )* numKeys );
126+
117127 // items now contains our keys from the gnome keyring
118128 // we will now put it in our two lists to return it to go
119129 GList * current ;
120130 int listNumber = 0 ;
121131 for (current = items ; current != NULL ; current = current -> next ) {
122132 char * pathTmp = secret_item_get_label (current -> data );
123133 // you cannot have a key without a label in the gnome keyring
124- char * acctTmp = get_username ( current -> data );
134+ char * acctTmp = get_attribute ( "username" , current -> data );
125135 if (acctTmp == NULL ) {
126136 acctTmp = "account not defined" ;
127137 }
128- char * path = (char * ) malloc (strlen (pathTmp ));
129- char * acct = (char * ) malloc (strlen (acctTmp ));
130- path = pathTmp ;
131- acct = acctTmp ;
132- (* paths )[listNumber ] = (char * ) malloc (sizeof (char )* (strlen (path )));
133- memcpy ((* paths )[listNumber ], path , sizeof (char )* (strlen (path )));
134- (* accts )[listNumber ] = (char * ) malloc (sizeof (char )* (strlen (acct )));
135- memcpy ((* accts )[listNumber ], acct , sizeof (char )* (strlen (acct )));
138+
139+ tmp_paths [listNumber ] = (char * ) calloc (1 , sizeof (char )* (strlen (pathTmp )+ 1 ));
140+ tmp_accts [listNumber ] = (char * ) calloc (1 , sizeof (char )* (strlen (acctTmp )+ 1 ));
141+
142+ memcpy (tmp_paths [listNumber ], pathTmp , sizeof (char )* (strlen (pathTmp )+ 1 ));
143+ memcpy (tmp_accts [listNumber ], acctTmp , sizeof (char )* (strlen (acctTmp )+ 1 ));
144+
136145 listNumber = listNumber + 1 ;
137146 }
138- * list_l = numKeys ;
147+
148+ * paths = (char * * ) realloc (tmp_paths , (int )sizeof (char * )* listNumber );
149+ * accts = (char * * ) realloc (tmp_accts , (int )sizeof (char * )* listNumber );
150+
151+ * list_l = listNumber ;
152+
139153 return NULL ;
140154}
141155
0 commit comments