Skip to content

Commit 19ec1c3

Browse files
committed
reconstruct the full url when doing keychain_list
Signed-off-by: Emmanuel Briney <[email protected]>
1 parent b7c53e0 commit 19ec1c3

1 file changed

Lines changed: 52 additions & 22 deletions

File tree

osxkeychain/osxkeychain_darwin.c

Lines changed: 52 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "osxkeychain_darwin.h"
22
#include <CoreFoundation/CoreFoundation.h>
3+
#include <Foundation/NSValue.h>
34
#include <stdio.h>
45
#include <string.h>
56

@@ -123,36 +124,29 @@ char *keychain_list(char *** paths, char *** accts, unsigned int *list_l) {
123124
//Use this query dictionary
124125
CFTypeRef result= NULL;
125126
OSStatus status = SecItemCopyMatching(
126-
query,
127-
&result);
127+
query,
128+
&result);
128129
//Ran a search and store the results in result
129130
if (status) {
130131
return get_error(status);
131132
}
132-
int numKeys = CFArrayGetCount(result);
133+
CFIndex numKeys = CFArrayGetCount(result);
133134
*paths = (char **) malloc((int)sizeof(char *)*numKeys);
134135
*accts = (char **) malloc((int)sizeof(char *)*numKeys);
135136
//result is of type CFArray
136-
for(int i=0; i<numKeys; i++) {
137+
for(CFIndex i=0; i<numKeys; i++) {
137138
CFDictionaryRef currKey = CFArrayGetValueAtIndex(result,i);
138-
if (CFDictionaryContainsKey(currKey, CFSTR("path"))) {
139-
//Even if a key is stored without an account, Apple defaults it to null so these arrays will be of the same length
140-
CFStringRef pathTmp = CFDictionaryGetValue(currKey, CFSTR("path"));
141-
CFStringRef acctTmp = CFDictionaryGetValue(currKey, CFSTR("acct"));
142-
if (acctTmp == NULL) {
143-
acctTmp = CFSTR("account not defined");
139+
140+
CFStringRef protocolTmp = CFDictionaryGetValue(currKey, CFSTR("ptcl"));
141+
if (protocolTmp != NULL) {
142+
CFStringRef protocolStr = CFStringCreateWithFormat(NULL, NULL, CFSTR("%@"), protocolTmp);
143+
if (CFStringCompare(protocolStr, CFSTR("htps"), 0) == kCFCompareEqualTo) {
144+
protocolTmp = CFSTR("https://");
145+
}
146+
else {
147+
protocolTmp = CFSTR("http://");
144148
}
145-
char * path = (char *) malloc(CFStringGetLength(pathTmp)+1);
146-
path = CFStringToCharArr(pathTmp);
147-
path[strlen(path)] = '\0';
148-
char * acct = (char *) malloc(CFStringGetLength(acctTmp)+1);
149-
acct = CFStringToCharArr(acctTmp);
150-
acct[strlen(acct)] = '\0';
151-
//We now have all we need, username and servername. Now export this to .go
152-
(*paths)[i] = (char *) malloc(sizeof(char)*(strlen(path)+1));
153-
memcpy((*paths)[i], path, sizeof(char)*(strlen(path)+1));
154-
(*accts)[i] = (char *) malloc(sizeof(char)*(strlen(acct)+1));
155-
memcpy((*accts)[i], acct, sizeof(char)*(strlen(acct)+1));
149+
CFRelease(protocolStr);
156150
}
157151
else {
158152
char * path = "0";
@@ -161,9 +155,45 @@ char *keychain_list(char *** paths, char *** accts, unsigned int *list_l) {
161155
memcpy((*paths)[i], path, sizeof(char)*(strlen(path)));
162156
(*accts)[i] = (char *) malloc(sizeof(char)*(strlen(acct)));
163157
memcpy((*accts)[i], acct, sizeof(char)*(strlen(acct)));
158+
continue;
159+
}
160+
161+
CFMutableStringRef str = CFStringCreateMutableCopy(NULL, 0, protocolTmp);
162+
CFStringRef serverTmp = CFDictionaryGetValue(currKey, CFSTR("srvr"));
163+
if (serverTmp != NULL) {
164+
CFStringAppend(str, serverTmp);
165+
}
166+
167+
CFStringRef pathTmp = CFDictionaryGetValue(currKey, CFSTR("path"));
168+
if (pathTmp != NULL) {
169+
CFStringAppend(str, pathTmp);
170+
}
171+
172+
const NSNumber * portTmp = CFDictionaryGetValue(currKey, CFSTR("port"));
173+
if (portTmp != NULL && portTmp.integerValue != 0) {
174+
CFStringRef portStr = CFStringCreateWithFormat(NULL, NULL, CFSTR("%@"), portTmp);
175+
CFStringAppend(str, CFSTR(":"));
176+
CFStringAppend(str, portStr);
177+
CFRelease(portStr);
164178
}
179+
180+
CFStringRef acctTmp = CFDictionaryGetValue(currKey, CFSTR("acct"));
181+
if (acctTmp == NULL) {
182+
acctTmp = CFSTR("account not defined");
183+
}
184+
185+
char * path = CFStringToCharArr(str);
186+
char * acct = CFStringToCharArr(acctTmp);
187+
188+
//We now have all we need, username and servername. Now export this to .go
189+
(*paths)[i] = (char *) malloc(sizeof(char)*(strlen(path)+1));
190+
memcpy((*paths)[i], path, sizeof(char)*(strlen(path)+1));
191+
(*accts)[i] = (char *) malloc(sizeof(char)*(strlen(acct)+1));
192+
memcpy((*accts)[i], acct, sizeof(char)*(strlen(acct)+1));
193+
194+
CFRelease(str);
165195
}
166-
*list_l = numKeys;
196+
*list_l = (int)numKeys;
167197
return NULL;
168198
}
169199

0 commit comments

Comments
 (0)