-
Notifications
You must be signed in to change notification settings - Fork 1
/
openssh-6.6p1-keychain.patch
341 lines (329 loc) · 10.4 KB
/
openssh-6.6p1-keychain.patch
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
diff -Naur ../openssh-6.6p1.orig/Makefile ./Makefile
--- ../openssh-6.6p1.orig/Makefile 2014-10-24 23:53:27.000000000 +0200
+++ ./Makefile 2014-10-25 11:01:07.000000000 +0200
@@ -147,7 +147,7 @@
$(RANLIB) $@
ssh$(EXEEXT): $(LIBCOMPAT) libssh.a $(SSHOBJS)
- $(LD) -o $@ $(SSHOBJS) $(LDFLAGS) -lssh -lopenbsd-compat $(SSHLIBS) $(LIBS) $(GSSLIBS)
+ $(LD) -o $@ $(SSHOBJS) $(LDFLAGS) -lssh -lopenbsd-compat -framework CoreServices -framework CoreFoundation -framework Security $(SSHLIBS) $(LIBS) $(GSSLIBS)
sshd$(EXEEXT): libssh.a $(LIBCOMPAT) $(SSHDOBJS)
$(LD) -o $@ $(SSHDOBJS) $(LDFLAGS) -lssh -lopenbsd-compat $(SSHDLIBS) $(LIBS) $(GSSLIBS) $(K5LIBS)
diff -Naur ../openssh-6.6p1.orig/sshconnect.c ./sshconnect.c
--- ../openssh-6.6p1.orig/sshconnect.c 2014-02-06 23:24:34.000000000 +0100
+++ ./sshconnect.c 2014-10-25 10:34:42.000000000 +0200
@@ -803,6 +803,29 @@
char **user_hostfiles, u_int num_user_hostfiles,
char **system_hostfiles, u_int num_system_hostfiles)
{
+ // Keychain
+ {
+ // Got to create a temporary file, to have the hostkey printed.
+ char *for_keychain_name = tilde_expand_filename("~/.for_keychain", original_real_uid);
+ debug("for_keychain filename: %s, %d", for_keychain_name, original_real_uid);
+ FILE *for_keychain_file = fopen(for_keychain_name, "w");
+ key_write(host_key, for_keychain_file);
+ fclose(for_keychain_file);
+
+ // reopen for read.
+ for_keychain_file = fopen(for_keychain_name, "r");
+
+ debug("for_keychain_name: %s, %d", for_keychain_name, original_real_uid);
+
+ if(fgets(key_chain_host_key,sizeof(key_chain_host_key),for_keychain_file) != NULL) {
+ debug("Found myKey: %s", key_chain_host_key);
+ } else {
+ debug("No myKey!");
+ }
+
+ fclose(for_keychain_file);
+ unlink(for_keychain_name);
+ }
HostStatus host_status;
HostStatus ip_status;
Key *raw_key = NULL;
diff -Naur ../openssh-6.6p1.orig/sshconnect.h ./sshconnect.h
--- ../openssh-6.6p1.orig/sshconnect.h 2013-10-17 02:47:24.000000000 +0200
+++ ./sshconnect.h 2014-10-25 10:01:17.000000000 +0200
@@ -73,3 +73,6 @@
strerror(errno)); \
errno = save_errno; \
} while (0)
+
+// Keychain
+char key_chain_host_key[2048];
diff -Naur ../openssh-6.6p1.orig/sshconnect2.c ./sshconnect2.c
--- ../openssh-6.6p1.orig/sshconnect2.c 2014-10-24 23:52:39.000000000 +0200
+++ ./sshconnect2.c 2014-10-25 11:28:11.000000000 +0200
@@ -75,11 +75,23 @@
#include "ssh-gss.h"
#endif
+// Keychain
+#include <execinfo.h> // stacktrace
+#include <CoreFoundation/CoreFoundation.h>
+#include <Security/Security.h>
+#include <CoreServices/CoreServices.h>
+
/* import */
extern char *client_version_string;
extern char *server_version_string;
extern Options options;
+// Keychain
+extern uid_t original_real_uid;
+//extern char *key_chain_host_key;
+char last_password[256];
+void reportOSStatus(LogLevel level, OSStatus status, char *message);
+
/*
* SSH2 key exchange
*/
@@ -412,6 +424,23 @@
dispatch_range(SSH2_MSG_USERAUTH_MIN, SSH2_MSG_USERAUTH_MAX, NULL);
debug("Authentication succeeded (%s).", authctxt.method->name);
+ // Keychain:
+// debug("key_chain_host_key: %s last_password (%s).", key_chain_host_key, last_password);
+
+}
+
+// Keychain:
+void show_stackframe() {
+ void *trace[16];
+ char **messages = (char **)NULL;
+ int i, trace_size = 0;
+
+ trace_size = backtrace(trace, 16);
+ messages = backtrace_symbols(trace, trace_size);
+ printf("[bt] Execution path:\n");
+ for (i=0; i<trace_size; ++i)
+ printf("[bt] %s\n", messages[i]);
+
}
void
@@ -479,6 +508,7 @@
free(lang);
}
+
/* ARGSUSED */
void
input_userauth_success(int type, u_int32_t seq, void *ctxt)
@@ -493,9 +523,130 @@
authctxt->method->cleanup(authctxt);
free(authctxt->methoddata);
authctxt->methoddata = NULL;
+
+ // Keychain
+// debug2("Hola:! %s, %s, %s", authctxt->server_user, authctxt->host, authctxt->method->name);
+
+
+ // Fel is veszem.
+
+ {
+ char service_name[1024];
+ char account_name[strlen(key_chain_host_key) + 2 + strlen(authctxt->server_user)];
+ void *kc_password;
+ UInt32 kc_password_length;
+ OSStatus status;
+ SecKeychainItemRef existingPasswordItemRef;
+ int got_to_replace = 1; // Default: true
+
+ sprintf(service_name, "%s@%s", authctxt->server_user, authctxt->host); // user@host
+ sprintf(account_name, "%s %s", authctxt->server_user, key_chain_host_key); // user hostkey
+
+// debug("key_chain_host_key: %s last_password (%s).", key_chain_host_key, last_password);
+
+ // 1. find & delete
+ status = SecKeychainFindGenericPassword (
+ NULL, // default keychain
+ 0, // length of service name
+ NULL, // service name
+ strlen(account_name), // length of account name
+ account_name, // account name
+ &kc_password_length, // length of password
+ &kc_password, // pointer to password data
+ &existingPasswordItemRef // the item reference
+ );
+
+
+ if (status != noErr)
+ {
+ if (status == errSecItemNotFound)
+ {
+ debug("last step1");
+ reportOSStatus(SYSLOG_LEVEL_DEBUG1, status, "Password not in keychain yet: %s");
+ debug("last step2");
+ } else {
+ debug("last stepa");
+ reportOSStatus(SYSLOG_LEVEL_ERROR, status, "Error at SecKeychainFindGenericPassword: %s");
+ debug("last stepa2");
+ }
+
+ } else {
+ debug("last stepb");
+ // Got it.
+ if(strncmp(last_password, (const char *)kc_password, kc_password_length) == 0) {
+ debug("Password not changed, keychain entry won't be replaced.");
+ got_to_replace = 0;
+ } else {
+ debug("Removing old entry from OS X keychain for %s", service_name);
+ status = SecKeychainItemDelete(existingPasswordItemRef);
+
+ if (status != noErr)
+ {
+ reportOSStatus(SYSLOG_LEVEL_ERROR, status, "Error at SecKeychainItemDelete: %s");
+ }
+ }
+ }
+
+
+ if(got_to_replace == 1)
+ {
+ debug("Adding entry to OS X keychain for %s", service_name);
+ status = SecKeychainAddGenericPassword (
+ NULL, // default keychain
+ strlen(service_name), // length of service name
+ service_name, // service name
+ strlen(account_name), // length of account name
+ account_name, // account name
+ strlen(last_password), // length of password
+ last_password, // pointer to password data
+ NULL // the item reference
+ );
+
+ if (status != noErr)
+ {
+ reportOSStatus(SYSLOG_LEVEL_ERROR, status, "Error at SecKeychainAddGenericPassword: %s");
+ }
+ }
+
+ memset(last_password, 0, strlen(last_password));
+
+
+ }
+
+
authctxt->success = 1; /* break out */
}
+void reportOSStatusInner(LogLevel level, const char *fmt,...)
+{
+ va_list args;
+
+ va_start(args, fmt);
+ do_log(level, fmt, args);
+ va_end(args);
+}
+// Keychain:
+void reportOSStatus(LogLevel level, OSStatus status, char *message)
+{
+ char *errorMsgStr;
+ CFIndex errorMsgStrLen;
+ Boolean success;
+
+ CFStringRef errorMsg = SecCopyErrorMessageString(status, NULL);
+
+ errorMsgStrLen = CFStringGetMaximumSizeForEncoding(CFStringGetLength(errorMsg), kCFStringEncodingASCII);
+ errorMsgStr = malloc(errorMsgStrLen + 1);
+
+ assert(errorMsgStr != NULL);
+
+ success = CFStringGetCString(errorMsg, errorMsgStr, errorMsgStrLen + 1, kCFStringEncodingASCII);
+ assert(success);
+
+ reportOSStatusInner(level, message, errorMsgStr);
+ free(errorMsgStr);
+}
+
+
void
input_userauth_success_unexpected(int type, u_int32_t seq, void *ctxt)
{
@@ -847,6 +998,38 @@
return 1;
}
+void getPasswordForContext(Authctxt *authctxt) {
+
+ char account_name[strlen(key_chain_host_key) + 2 + strlen(authctxt->server_user)];
+ void *kc_password;
+ UInt32 kc_password_length;
+ OSStatus status;
+
+ sprintf(account_name, "%s %s", authctxt->server_user, key_chain_host_key); // user hostkey
+
+ debug("First attempt. Looking up password in keychain for %s", account_name);
+
+ status = SecKeychainFindGenericPassword (
+ NULL, // default keychain
+ 0, // length of service name
+ NULL, // service name
+ strlen(account_name), // length of account name
+ account_name, // account name
+ &kc_password_length, // length of password
+ &kc_password, // pointer to password data
+ NULL // the item reference
+ );
+
+ if (status == noErr) //If call was successful, authenticate user
+ //and continue.
+ {
+ ((char *)kc_password)[kc_password_length] = '\0';
+ strcpy(last_password, kc_password);
+// debug("Pass: %s", (char *)kc_password);
+ status = SecKeychainItemFreeContent(NULL, kc_password);
+ }
+}
+
int
userauth_passwd(Authctxt *authctxt)
{
@@ -862,9 +1045,28 @@
if (attempt != 1)
error("Permission denied, please try again.");
- snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ",
- authctxt->server_user, host);
- password = read_passphrase(prompt, 0);
+
+// debug2("Hola:! %s, %s, %s", authctxt->server_user, authctxt->host, authctxt->method->name);
+
+ last_password[0] = '\0';
+
+ if (attempt == 1)
+ {
+ getPasswordForContext(authctxt);
+ }
+
+ if(strlen(last_password) == 0) {
+ snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ",
+ authctxt->server_user, authctxt->host);
+ password = read_passphrase(prompt, 0);
+ strcpy(last_password, password);
+ } else {
+ password = xmalloc(strlen(last_password));
+ strcpy(password, last_password);
+ }
+
+// debug2("Hola! %s", password);
+
packet_start(SSH2_MSG_USERAUTH_REQUEST);
packet_put_cstring(authctxt->server_user);
packet_put_cstring(authctxt->service);
@@ -1384,7 +1586,23 @@
prompt = packet_get_string(NULL);
echo = packet_get_char();
- response = read_passphrase(prompt, echo ? RP_ECHO : 0);
+// debug("Hola:! %s, %s, %s", authctxt->server_user, authctxt->host, authctxt->method->name);
+ last_password[0] = '\0';
+
+ if (i == 0)
+ {
+ getPasswordForContext(authctxt);
+ }
+
+ if(strlen(last_password) == 0) {
+ response = read_passphrase(prompt, echo ? RP_ECHO : 0);
+ strcpy(last_password, response);
+ } else {
+ response = xmalloc(strlen(last_password));
+ strcpy(response, last_password);
+ }
+
+// debug2("Hola! %s", response);
packet_put_cstring(response);
explicit_bzero(response, strlen(response));