Mercurial > dropbear
annotate cli-kex.c @ 63:dcc43965928f
- A nice cleaner structure for tcp (acceptor) forwarding.
- still a checkpoint-ish commit
- sorted out listening on localhost only
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Wed, 11 Aug 2004 17:26:47 +0000 |
parents | bdc97a5719f4 |
children | e3adf4cf5465 |
rev | line source |
---|---|
26 | 1 /* |
2 * Dropbear - a SSH2 server | |
3 * | |
4 * Copyright (c) 2002,2003 Matt Johnston | |
5 * All rights reserved. | |
6 * | |
7 * Permission is hereby granted, free of charge, to any person obtaining a copy | |
8 * of this software and associated documentation files (the "Software"), to deal | |
9 * in the Software without restriction, including without limitation the rights | |
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
11 * copies of the Software, and to permit persons to whom the Software is | |
12 * furnished to do so, subject to the following conditions: | |
13 * | |
14 * The above copyright notice and this permission notice shall be included in | |
15 * all copies or substantial portions of the Software. | |
16 * | |
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
23 * SOFTWARE. */ | |
24 | |
25 #include "includes.h" | |
26 #include "session.h" | |
27 #include "dbutil.h" | |
28 #include "algo.h" | |
29 #include "buffer.h" | |
30 #include "session.h" | |
31 #include "kex.h" | |
32 #include "ssh.h" | |
33 #include "packet.h" | |
34 #include "bignum.h" | |
35 #include "random.h" | |
36 #include "runopts.h" | |
33 | 37 #include "signkey.h" |
26 | 38 |
39 | |
51
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
34
diff
changeset
|
40 static void checkhostkey(unsigned char* keyblob, unsigned int keybloblen); |
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
34
diff
changeset
|
41 #define MAX_KNOWNHOSTS_LINE 4500 |
26 | 42 |
43 void send_msg_kexdh_init() { | |
44 | |
45 cli_ses.dh_e = (mp_int*)m_malloc(sizeof(mp_int)); | |
46 cli_ses.dh_x = (mp_int*)m_malloc(sizeof(mp_int)); | |
47 | |
34
e2a1eaa19f22
Client mostly works up to password auth
Matt Johnston <matt@ucc.asn.au>
parents:
33
diff
changeset
|
48 m_mp_init_multi(cli_ses.dh_e, cli_ses.dh_x, NULL); |
26 | 49 gen_kexdh_vals(cli_ses.dh_e, cli_ses.dh_x); |
50 | |
51 CHECKCLEARTOWRITE(); | |
52 buf_putbyte(ses.writepayload, SSH_MSG_KEXDH_INIT); | |
53 buf_putmpint(ses.writepayload, cli_ses.dh_e); | |
54 encrypt_packet(); | |
55 ses.requirenext = SSH_MSG_KEXDH_REPLY; | |
56 } | |
57 | |
58 /* Handle a diffie-hellman key exchange reply. */ | |
59 void recv_msg_kexdh_reply() { | |
60 | |
61 mp_int dh_f; | |
62 sign_key *hostkey = NULL; | |
51
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
34
diff
changeset
|
63 unsigned int type, keybloblen; |
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
34
diff
changeset
|
64 unsigned char* keyblob = NULL; |
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
34
diff
changeset
|
65 |
26 | 66 |
34
e2a1eaa19f22
Client mostly works up to password auth
Matt Johnston <matt@ucc.asn.au>
parents:
33
diff
changeset
|
67 TRACE(("enter recv_msg_kexdh_reply")); |
26 | 68 type = ses.newkeys->algo_hostkey; |
34
e2a1eaa19f22
Client mostly works up to password auth
Matt Johnston <matt@ucc.asn.au>
parents:
33
diff
changeset
|
69 TRACE(("type is %d", type)); |
26 | 70 |
71 hostkey = new_sign_key(); | |
51
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
34
diff
changeset
|
72 keybloblen = buf_getint(ses.payload); |
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
34
diff
changeset
|
73 |
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
34
diff
changeset
|
74 keyblob = buf_getptr(ses.payload, keybloblen); |
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
34
diff
changeset
|
75 if (!ses.kexstate.donefirstkex) { |
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
34
diff
changeset
|
76 /* Only makes sense the first time */ |
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
34
diff
changeset
|
77 checkhostkey(keyblob, keybloblen); |
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
34
diff
changeset
|
78 } |
34
e2a1eaa19f22
Client mostly works up to password auth
Matt Johnston <matt@ucc.asn.au>
parents:
33
diff
changeset
|
79 |
26 | 80 if (buf_get_pub_key(ses.payload, hostkey, &type) != DROPBEAR_SUCCESS) { |
34
e2a1eaa19f22
Client mostly works up to password auth
Matt Johnston <matt@ucc.asn.au>
parents:
33
diff
changeset
|
81 TRACE(("failed getting pubkey")); |
26 | 82 dropbear_exit("Bad KEX packet"); |
83 } | |
84 | |
85 m_mp_init(&dh_f); | |
86 if (buf_getmpint(ses.payload, &dh_f) != DROPBEAR_SUCCESS) { | |
34
e2a1eaa19f22
Client mostly works up to password auth
Matt Johnston <matt@ucc.asn.au>
parents:
33
diff
changeset
|
87 TRACE(("failed getting mpint")); |
26 | 88 dropbear_exit("Bad KEX packet"); |
89 } | |
90 | |
91 kexdh_comb_key(cli_ses.dh_e, cli_ses.dh_x, &dh_f, hostkey); | |
92 mp_clear(&dh_f); | |
93 | |
94 if (buf_verify(ses.payload, hostkey, ses.hash, SHA1_HASH_SIZE) | |
95 != DROPBEAR_SUCCESS) { | |
96 dropbear_exit("Bad hostkey signature"); | |
97 } | |
98 | |
99 sign_key_free(hostkey); | |
100 hostkey = NULL; | |
101 | |
102 send_msg_newkeys(); | |
103 ses.requirenext = SSH_MSG_NEWKEYS; | |
104 TRACE(("leave recv_msg_kexdh_init")); | |
105 } | |
51
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
34
diff
changeset
|
106 |
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
34
diff
changeset
|
107 static void ask_to_confirm(unsigned char* keyblob, unsigned int keybloblen) { |
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
34
diff
changeset
|
108 |
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
34
diff
changeset
|
109 char* fp = NULL; |
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
34
diff
changeset
|
110 |
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
34
diff
changeset
|
111 fp = sign_key_fingerprint(keyblob, keybloblen); |
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
34
diff
changeset
|
112 fprintf(stderr, "\nHost '%s' is not in the trusted hosts file.\n(fingerprint %s)\nDo you want to continue connecting? (y/n)\n", |
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
34
diff
changeset
|
113 cli_opts.remotehost, |
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
34
diff
changeset
|
114 fp); |
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
34
diff
changeset
|
115 |
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
34
diff
changeset
|
116 if (getc(stdin) == 'y') { |
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
34
diff
changeset
|
117 m_free(fp); |
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
34
diff
changeset
|
118 return; |
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
34
diff
changeset
|
119 } |
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
34
diff
changeset
|
120 |
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
34
diff
changeset
|
121 dropbear_exit("Didn't validate host key"); |
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
34
diff
changeset
|
122 } |
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
34
diff
changeset
|
123 |
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
34
diff
changeset
|
124 static void checkhostkey(unsigned char* keyblob, unsigned int keybloblen) { |
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
34
diff
changeset
|
125 |
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
34
diff
changeset
|
126 char * filename = NULL; |
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
34
diff
changeset
|
127 FILE *hostsfile = NULL; |
59
bdc97a5719f4
add new entries to known_hosts
Matt Johnston <matt@ucc.asn.au>
parents:
51
diff
changeset
|
128 int readonly = 0; |
51
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
34
diff
changeset
|
129 struct passwd *pw = NULL; |
59
bdc97a5719f4
add new entries to known_hosts
Matt Johnston <matt@ucc.asn.au>
parents:
51
diff
changeset
|
130 unsigned int hostlen, algolen; |
bdc97a5719f4
add new entries to known_hosts
Matt Johnston <matt@ucc.asn.au>
parents:
51
diff
changeset
|
131 unsigned long len; |
51
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
34
diff
changeset
|
132 const char *algoname = NULL; |
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
34
diff
changeset
|
133 buffer * line = NULL; |
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
34
diff
changeset
|
134 int ret; |
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
34
diff
changeset
|
135 |
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
34
diff
changeset
|
136 pw = getpwuid(getuid()); |
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
34
diff
changeset
|
137 |
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
34
diff
changeset
|
138 if (pw == NULL) { |
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
34
diff
changeset
|
139 dropbear_exit("Failed to get homedir"); |
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
34
diff
changeset
|
140 } |
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
34
diff
changeset
|
141 |
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
34
diff
changeset
|
142 len = strlen(pw->pw_dir); |
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
34
diff
changeset
|
143 filename = m_malloc(len + 18); /* "/.ssh/known_hosts" and null-terminator*/ |
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
34
diff
changeset
|
144 |
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
34
diff
changeset
|
145 snprintf(filename, len+18, "%s/.ssh", pw->pw_dir); |
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
34
diff
changeset
|
146 /* Check that ~/.ssh exists - easiest way is just to mkdir */ |
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
34
diff
changeset
|
147 if (mkdir(filename, S_IRWXU) != 0) { |
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
34
diff
changeset
|
148 if (errno != EEXIST) { |
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
34
diff
changeset
|
149 ask_to_confirm(keyblob, keybloblen); |
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
34
diff
changeset
|
150 goto out; /* only get here on success */ |
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
34
diff
changeset
|
151 } |
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
34
diff
changeset
|
152 } |
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
34
diff
changeset
|
153 |
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
34
diff
changeset
|
154 snprintf(filename, len+18, "%s/.ssh/known_hosts", pw->pw_dir); |
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
34
diff
changeset
|
155 hostsfile = fopen(filename, "r+"); |
59
bdc97a5719f4
add new entries to known_hosts
Matt Johnston <matt@ucc.asn.au>
parents:
51
diff
changeset
|
156 |
bdc97a5719f4
add new entries to known_hosts
Matt Johnston <matt@ucc.asn.au>
parents:
51
diff
changeset
|
157 /* We mightn't have been able to open it if it was read-only */ |
bdc97a5719f4
add new entries to known_hosts
Matt Johnston <matt@ucc.asn.au>
parents:
51
diff
changeset
|
158 if (hostsfile == NULL && (errno == EACCES || errno == EROFS)) { |
bdc97a5719f4
add new entries to known_hosts
Matt Johnston <matt@ucc.asn.au>
parents:
51
diff
changeset
|
159 readonly = 1; |
bdc97a5719f4
add new entries to known_hosts
Matt Johnston <matt@ucc.asn.au>
parents:
51
diff
changeset
|
160 hostsfile = fopen(filename, "r"); |
bdc97a5719f4
add new entries to known_hosts
Matt Johnston <matt@ucc.asn.au>
parents:
51
diff
changeset
|
161 } |
bdc97a5719f4
add new entries to known_hosts
Matt Johnston <matt@ucc.asn.au>
parents:
51
diff
changeset
|
162 |
51
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
34
diff
changeset
|
163 if (hostsfile == NULL) { |
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
34
diff
changeset
|
164 ask_to_confirm(keyblob, keybloblen); |
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
34
diff
changeset
|
165 goto out; /* We only get here on success */ |
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
34
diff
changeset
|
166 } |
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
34
diff
changeset
|
167 |
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
34
diff
changeset
|
168 line = buf_new(MAX_KNOWNHOSTS_LINE); |
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
34
diff
changeset
|
169 hostlen = strlen(cli_opts.remotehost); |
59
bdc97a5719f4
add new entries to known_hosts
Matt Johnston <matt@ucc.asn.au>
parents:
51
diff
changeset
|
170 algoname = signkey_name_from_type(ses.newkeys->algo_hostkey, &algolen); |
51
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
34
diff
changeset
|
171 |
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
34
diff
changeset
|
172 do { |
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
34
diff
changeset
|
173 if (buf_getline(line, hostsfile) == DROPBEAR_FAILURE) { |
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
34
diff
changeset
|
174 TRACE(("failed reading line: prob EOF")); |
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
34
diff
changeset
|
175 break; |
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
34
diff
changeset
|
176 } |
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
34
diff
changeset
|
177 |
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
34
diff
changeset
|
178 /* The line is too short to be sensible */ |
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
34
diff
changeset
|
179 /* "30" is 'enough to hold ssh-dss plus the spaces, ie so we don't |
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
34
diff
changeset
|
180 * buf_getfoo() past the end and die horribly - the base64 parsing |
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
34
diff
changeset
|
181 * code is what tiptoes up to the end nicely */ |
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
34
diff
changeset
|
182 if (line->len < (hostlen+30) ) { |
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
34
diff
changeset
|
183 TRACE(("line is too short to be sensible")); |
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
34
diff
changeset
|
184 continue; |
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
34
diff
changeset
|
185 } |
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
34
diff
changeset
|
186 |
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
34
diff
changeset
|
187 /* Compare hostnames */ |
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
34
diff
changeset
|
188 if (strncmp(cli_opts.remotehost, buf_getptr(line, hostlen), |
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
34
diff
changeset
|
189 hostlen) != 0) { |
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
34
diff
changeset
|
190 TRACE(("hosts don't match")); |
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
34
diff
changeset
|
191 continue; |
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
34
diff
changeset
|
192 } |
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
34
diff
changeset
|
193 |
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
34
diff
changeset
|
194 buf_incrpos(line, hostlen); |
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
34
diff
changeset
|
195 if (buf_getbyte(line) != ' ') { |
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
34
diff
changeset
|
196 /* there wasn't a space after the hostname, something dodgy */ |
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
34
diff
changeset
|
197 TRACE(("missing space afte matching hostname")); |
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
34
diff
changeset
|
198 continue; |
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
34
diff
changeset
|
199 } |
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
34
diff
changeset
|
200 |
59
bdc97a5719f4
add new entries to known_hosts
Matt Johnston <matt@ucc.asn.au>
parents:
51
diff
changeset
|
201 if ( strncmp(buf_getptr(line, algolen), algoname, algolen) != 0) { |
51
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
34
diff
changeset
|
202 TRACE(("algo doesn't match")); |
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
34
diff
changeset
|
203 continue; |
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
34
diff
changeset
|
204 } |
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
34
diff
changeset
|
205 |
59
bdc97a5719f4
add new entries to known_hosts
Matt Johnston <matt@ucc.asn.au>
parents:
51
diff
changeset
|
206 buf_incrpos(line, algolen); |
51
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
34
diff
changeset
|
207 if (buf_getbyte(line) != ' ') { |
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
34
diff
changeset
|
208 TRACE(("missing space after algo")); |
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
34
diff
changeset
|
209 continue; |
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
34
diff
changeset
|
210 } |
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
34
diff
changeset
|
211 |
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
34
diff
changeset
|
212 /* Now we're at the interesting hostkey */ |
59
bdc97a5719f4
add new entries to known_hosts
Matt Johnston <matt@ucc.asn.au>
parents:
51
diff
changeset
|
213 ret = cmp_base64_key(keyblob, keybloblen, algoname, algolen, line); |
51
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
34
diff
changeset
|
214 |
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
34
diff
changeset
|
215 if (ret == DROPBEAR_SUCCESS) { |
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
34
diff
changeset
|
216 /* Good matching key */ |
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
34
diff
changeset
|
217 TRACE(("good matching key")); |
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
34
diff
changeset
|
218 goto out; |
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
34
diff
changeset
|
219 } |
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
34
diff
changeset
|
220 |
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
34
diff
changeset
|
221 /* The keys didn't match. eep. */ |
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
34
diff
changeset
|
222 } while (1); /* keep going 'til something happens */ |
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
34
diff
changeset
|
223 |
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
34
diff
changeset
|
224 /* Key doesn't exist yet */ |
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
34
diff
changeset
|
225 ask_to_confirm(keyblob, keybloblen); |
59
bdc97a5719f4
add new entries to known_hosts
Matt Johnston <matt@ucc.asn.au>
parents:
51
diff
changeset
|
226 |
51
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
34
diff
changeset
|
227 /* If we get here, they said yes */ |
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
34
diff
changeset
|
228 |
59
bdc97a5719f4
add new entries to known_hosts
Matt Johnston <matt@ucc.asn.au>
parents:
51
diff
changeset
|
229 if (readonly) { |
bdc97a5719f4
add new entries to known_hosts
Matt Johnston <matt@ucc.asn.au>
parents:
51
diff
changeset
|
230 goto out; |
bdc97a5719f4
add new entries to known_hosts
Matt Johnston <matt@ucc.asn.au>
parents:
51
diff
changeset
|
231 } |
bdc97a5719f4
add new entries to known_hosts
Matt Johnston <matt@ucc.asn.au>
parents:
51
diff
changeset
|
232 |
bdc97a5719f4
add new entries to known_hosts
Matt Johnston <matt@ucc.asn.au>
parents:
51
diff
changeset
|
233 /* put the new entry in the file */ |
bdc97a5719f4
add new entries to known_hosts
Matt Johnston <matt@ucc.asn.au>
parents:
51
diff
changeset
|
234 fseek(hostsfile, 0, SEEK_END); |
bdc97a5719f4
add new entries to known_hosts
Matt Johnston <matt@ucc.asn.au>
parents:
51
diff
changeset
|
235 buf_setpos(line, 0); |
bdc97a5719f4
add new entries to known_hosts
Matt Johnston <matt@ucc.asn.au>
parents:
51
diff
changeset
|
236 buf_setlen(line, 0); |
bdc97a5719f4
add new entries to known_hosts
Matt Johnston <matt@ucc.asn.au>
parents:
51
diff
changeset
|
237 buf_putbytes(line, ses.remotehost, hostlen); |
bdc97a5719f4
add new entries to known_hosts
Matt Johnston <matt@ucc.asn.au>
parents:
51
diff
changeset
|
238 buf_putbyte(line, ' '); |
bdc97a5719f4
add new entries to known_hosts
Matt Johnston <matt@ucc.asn.au>
parents:
51
diff
changeset
|
239 buf_putbytes(line, algoname, algolen); |
bdc97a5719f4
add new entries to known_hosts
Matt Johnston <matt@ucc.asn.au>
parents:
51
diff
changeset
|
240 buf_putbyte(line, ' '); |
bdc97a5719f4
add new entries to known_hosts
Matt Johnston <matt@ucc.asn.au>
parents:
51
diff
changeset
|
241 len = line->size - line->pos; |
bdc97a5719f4
add new entries to known_hosts
Matt Johnston <matt@ucc.asn.au>
parents:
51
diff
changeset
|
242 TRACE(("keybloblen %d, len %d", keybloblen, len)); |
bdc97a5719f4
add new entries to known_hosts
Matt Johnston <matt@ucc.asn.au>
parents:
51
diff
changeset
|
243 /* The only failure with base64 is buffer_overflow, but buf_getwriteptr |
bdc97a5719f4
add new entries to known_hosts
Matt Johnston <matt@ucc.asn.au>
parents:
51
diff
changeset
|
244 * will die horribly in the case anyway */ |
bdc97a5719f4
add new entries to known_hosts
Matt Johnston <matt@ucc.asn.au>
parents:
51
diff
changeset
|
245 base64_encode(keyblob, keybloblen, buf_getwriteptr(line, len), &len); |
bdc97a5719f4
add new entries to known_hosts
Matt Johnston <matt@ucc.asn.au>
parents:
51
diff
changeset
|
246 buf_incrwritepos(line, len); |
bdc97a5719f4
add new entries to known_hosts
Matt Johnston <matt@ucc.asn.au>
parents:
51
diff
changeset
|
247 buf_putbyte(line, '\n'); |
bdc97a5719f4
add new entries to known_hosts
Matt Johnston <matt@ucc.asn.au>
parents:
51
diff
changeset
|
248 buf_setpos(line, 0); |
bdc97a5719f4
add new entries to known_hosts
Matt Johnston <matt@ucc.asn.au>
parents:
51
diff
changeset
|
249 fwrite(buf_getptr(line, line->len), line->len, 1, hostsfile); |
bdc97a5719f4
add new entries to known_hosts
Matt Johnston <matt@ucc.asn.au>
parents:
51
diff
changeset
|
250 /* We ignore errors, since there's not much we can do about them */ |
bdc97a5719f4
add new entries to known_hosts
Matt Johnston <matt@ucc.asn.au>
parents:
51
diff
changeset
|
251 |
51
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
34
diff
changeset
|
252 out: |
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
34
diff
changeset
|
253 if (hostsfile != NULL) { |
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
34
diff
changeset
|
254 fclose(hostsfile); |
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
34
diff
changeset
|
255 } |
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
34
diff
changeset
|
256 m_free(filename); |
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
34
diff
changeset
|
257 buf_free(line); |
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
34
diff
changeset
|
258 } |