Line | Source | Count |
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 | #include "includes.h" | - |
27 | | - |
28 | #include <sys/types.h> | - |
29 | | - |
30 | #include <pwd.h> | - |
31 | #include <string.h> | - |
32 | #include <stdarg.h> | - |
33 | | - |
34 | #include "xmalloc.h" | - |
35 | #include "ssh2.h" | - |
36 | #include "packet.h" | - |
37 | #include "sshbuf.h" | - |
38 | #include "log.h" | - |
39 | #include "misc.h" | - |
40 | #include "servconf.h" | - |
41 | #include "compat.h" | - |
42 | #include "sshkey.h" | - |
43 | #include "hostfile.h" | - |
44 | #include "auth.h" | - |
45 | #include "canohost.h" | - |
46 | #ifdef GSSAPI | - |
47 | #include "ssh-gss.h" | - |
48 | #endif | - |
49 | #include "monitor_wrap.h" | - |
50 | #include "pathnames.h" | - |
51 | #include "ssherr.h" | - |
52 | #include "match.h" | - |
53 | | - |
54 | | - |
55 | extern ServerOptions options; | - |
56 | extern u_char *session_id2; | - |
57 | extern u_int session_id2_len; | - |
58 | | - |
59 | static int | - |
60 | userauth_hostbased(struct ssh *ssh) | - |
61 | { | - |
62 | Authctxt *authctxt = ssh->authctxt; | - |
63 | struct sshbuf *b; | - |
64 | struct sshkey *key = NULL; | - |
65 | char *pkalg, *cuser, *chost; | - |
66 | u_char *pkblob, *sig; | - |
67 | size_t alen, blen, slen; | - |
68 | int r, pktype, authenticated = 0; | - |
69 | | - |
70 | | - |
71 | if ((r = sshpkt_get_cstring(ssh, &pkalg, &alen)) != 0 ||TRUE | never evaluated | FALSE | never evaluated |
| 0 |
72 | (r = sshpkt_get_string(ssh, &pkblob, &blen)) != 0 ||TRUE | never evaluated | FALSE | never evaluated |
| 0 |
73 | (r = sshpkt_get_cstring(ssh, &chost, NULL)) != 0 ||TRUE | never evaluated | FALSE | never evaluated |
| 0 |
74 | (r = sshpkt_get_cstring(ssh, &cuser, NULL)) != 0 ||TRUE | never evaluated | FALSE | never evaluated |
| 0 |
75 | (r = sshpkt_get_string(ssh, &sig, &slen)) != 0)TRUE | never evaluated | FALSE | never evaluated |
| 0 |
76 | fatal("%s: packet parsing: %s", __func__, ssh_err(r)); never executed: fatal("%s: packet parsing: %s", __func__, ssh_err(r)); | 0 |
77 | | - |
78 | debug("%s: cuser %s chost %s pkalg %s slen %zu", __func__, | - |
79 | cuser, chost, pkalg, slen); | - |
80 | #ifdef DEBUG_PK | - |
81 | debug("signature:"); | - |
82 | sshbuf_dump_data(sig, slen, stderr); | - |
83 | #endif | - |
84 | pktype = sshkey_type_from_name(pkalg); | - |
85 | if (pktype == KEY_UNSPEC) {TRUE | never evaluated | FALSE | never evaluated |
| 0 |
86 | | - |
87 | logit("%s: unsupported public key algorithm: %s", | - |
88 | __func__, pkalg); | - |
89 | goto done; never executed: goto done; | 0 |
90 | } | - |
91 | if ((r = sshkey_from_blob(pkblob, blen, &key)) != 0) {TRUE | never evaluated | FALSE | never evaluated |
| 0 |
92 | error("%s: key_from_blob: %s", __func__, ssh_err(r)); | - |
93 | goto done; never executed: goto done; | 0 |
94 | } | - |
95 | if (key == NULL) {TRUE | never evaluated | FALSE | never evaluated |
| 0 |
96 | error("%s: cannot decode key: %s", __func__, pkalg); | - |
97 | goto done; never executed: goto done; | 0 |
98 | } | - |
99 | if (key->type != pktype) {TRUE | never evaluated | FALSE | never evaluated |
| 0 |
100 | error("%s: type mismatch for decoded key " | - |
101 | "(received %d, expected %d)", __func__, key->type, pktype); | - |
102 | goto done; never executed: goto done; | 0 |
103 | } | - |
104 | if (sshkey_type_plain(key->type) == KEY_RSA &&TRUE | never evaluated | FALSE | never evaluated |
| 0 |
105 | (ssh->compat & SSH_BUG_RSASIGMD5) != 0) {TRUE | never evaluated | FALSE | never evaluated |
| 0 |
106 | error("Refusing RSA key because peer uses unsafe " | - |
107 | "signature format"); | - |
108 | goto done; never executed: goto done; | 0 |
109 | } | - |
110 | if (match_pattern_list(pkalg, options.hostbased_key_types, 0) != 1) {TRUE | never evaluated | FALSE | never evaluated |
| 0 |
111 | logit("%s: key type %s not in HostbasedAcceptedKeyTypes", | - |
112 | __func__, sshkey_type(key)); | - |
113 | goto done; never executed: goto done; | 0 |
114 | } | - |
115 | if ((r = sshkey_check_cert_sigtype(key,TRUE | never evaluated | FALSE | never evaluated |
| 0 |
116 | options.ca_sign_algorithms)) != 0) {TRUE | never evaluated | FALSE | never evaluated |
| 0 |
117 | logit("%s: certificate signature algorithm %s: %s", __func__, | - |
118 | (key->cert == NULL || key->cert->signature_type == NULL) ? | - |
119 | "(null)" : key->cert->signature_type, ssh_err(r)); | - |
120 | goto done; never executed: goto done; | 0 |
121 | } | - |
122 | | - |
123 | if (!authctxt->valid || authctxt->user == NULL) {TRUE | never evaluated | FALSE | never evaluated |
TRUE | never evaluated | FALSE | never evaluated |
| 0 |
124 | debug2("%s: disabled because of invalid user", __func__); | - |
125 | goto done; never executed: goto done; | 0 |
126 | } | - |
127 | | - |
128 | if ((b = sshbuf_new()) == NULL)TRUE | never evaluated | FALSE | never evaluated |
| 0 |
129 | fatal("%s: sshbuf_new failed", __func__); never executed: fatal("%s: sshbuf_new failed", __func__); | 0 |
130 | | - |
131 | if ((r = sshbuf_put_string(b, session_id2, session_id2_len)) != 0 ||TRUE | never evaluated | FALSE | never evaluated |
| 0 |
132 | (r = sshbuf_put_u8(b, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||TRUE | never evaluated | FALSE | never evaluated |
| 0 |
133 | (r = sshbuf_put_cstring(b, authctxt->user)) != 0 ||TRUE | never evaluated | FALSE | never evaluated |
| 0 |
134 | (r = sshbuf_put_cstring(b, authctxt->service)) != 0 ||TRUE | never evaluated | FALSE | never evaluated |
| 0 |
135 | (r = sshbuf_put_cstring(b, "hostbased")) != 0 ||TRUE | never evaluated | FALSE | never evaluated |
| 0 |
136 | (r = sshbuf_put_string(b, pkalg, alen)) != 0 ||TRUE | never evaluated | FALSE | never evaluated |
| 0 |
137 | (r = sshbuf_put_string(b, pkblob, blen)) != 0 ||TRUE | never evaluated | FALSE | never evaluated |
| 0 |
138 | (r = sshbuf_put_cstring(b, chost)) != 0 ||TRUE | never evaluated | FALSE | never evaluated |
| 0 |
139 | (r = sshbuf_put_cstring(b, cuser)) != 0)TRUE | never evaluated | FALSE | never evaluated |
| 0 |
140 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); never executed: fatal("%s: buffer error: %s", __func__, ssh_err(r)); | 0 |
141 | #ifdef DEBUG_PK | - |
142 | sshbuf_dump(b, stderr); | - |
143 | #endif | - |
144 | | - |
145 | auth2_record_info(authctxt, | - |
146 | "client user \"%.100s\", client host \"%.100s\"", cuser, chost); | - |
147 | | - |
148 | | - |
149 | authenticated = 0; | - |
150 | if (PRIVSEP(hostbased_key_allowed(authctxt->pw, cuser, chost, key)) &&TRUE | never evaluated | FALSE | never evaluated |
TRUE | never evaluated | FALSE | never evaluated |
| 0 |
151 | PRIVSEP(sshkey_verify(key, sig, slen,TRUE | never evaluated | FALSE | never evaluated |
TRUE | never evaluated | FALSE | never evaluated |
| 0 |
152 | sshbuf_ptr(b), sshbuf_len(b), pkalg, ssh->compat)) == 0)TRUE | never evaluated | FALSE | never evaluated |
| 0 |
153 | authenticated = 1; never executed: authenticated = 1; | 0 |
154 | | - |
155 | auth2_record_key(authctxt, authenticated, key); | - |
156 | sshbuf_free(b); | - |
157 | done: code before this statement never executed: done: | 0 |
158 | debug2("%s: authenticated %d", __func__, authenticated); | - |
159 | sshkey_free(key); | - |
160 | free(pkalg); | - |
161 | free(pkblob); | - |
162 | free(cuser); | - |
163 | free(chost); | - |
164 | free(sig); | - |
165 | return authenticated; never executed: return authenticated; | 0 |
166 | } | - |
167 | | - |
168 | | - |
169 | int | - |
170 | hostbased_key_allowed(struct passwd *pw, const char *cuser, char *chost, | - |
171 | struct sshkey *key) | - |
172 | { | - |
173 | struct ssh *ssh = active_state; | - |
174 | const char *resolvedname, *ipaddr, *lookup, *reason; | - |
175 | HostStatus host_status; | - |
176 | int len; | - |
177 | char *fp; | - |
178 | | - |
179 | if (auth_key_is_revoked(key))TRUE | never evaluated | FALSE | never evaluated |
| 0 |
180 | return 0; never executed: return 0; | 0 |
181 | | - |
182 | resolvedname = auth_get_canonical_hostname(ssh, options.use_dns); | - |
183 | ipaddr = ssh_remote_ipaddr(ssh); | - |
184 | | - |
185 | debug2("%s: chost %s resolvedname %s ipaddr %s", __func__, | - |
186 | chost, resolvedname, ipaddr); | - |
187 | | - |
188 | if (((len = strlen(chost)) > 0) && chost[len - 1] == '.') {TRUE | never evaluated | FALSE | never evaluated |
TRUE | never evaluated | FALSE | never evaluated |
| 0 |
189 | debug2("stripping trailing dot from chost %s", chost); | - |
190 | chost[len - 1] = '\0'; | - |
191 | } never executed: end of block | 0 |
192 | | - |
193 | if (options.hostbased_uses_name_from_packet_only) {TRUE | never evaluated | FALSE | never evaluated |
| 0 |
194 | if (auth_rhosts2(pw, cuser, chost, chost) == 0) {TRUE | never evaluated | FALSE | never evaluated |
| 0 |
195 | debug2("%s: auth_rhosts2 refused " | - |
196 | "user \"%.100s\" host \"%.100s\" (from packet)", | - |
197 | __func__, cuser, chost); | - |
198 | return 0; never executed: return 0; | 0 |
199 | } | - |
200 | lookup = chost; | - |
201 | } else { never executed: end of block | 0 |
202 | if (strcasecmp(resolvedname, chost) != 0)TRUE | never evaluated | FALSE | never evaluated |
| 0 |
203 | logit("userauth_hostbased mismatch: " never executed: logit("userauth_hostbased mismatch: " "client sends %s, but we resolve %s to %s", chost, ipaddr, resolvedname); | 0 |
204 | "client sends %s, but we resolve %s to %s", never executed: logit("userauth_hostbased mismatch: " "client sends %s, but we resolve %s to %s", chost, ipaddr, resolvedname); | 0 |
205 | chost, ipaddr, resolvedname); never executed: logit("userauth_hostbased mismatch: " "client sends %s, but we resolve %s to %s", chost, ipaddr, resolvedname); | 0 |
206 | if (auth_rhosts2(pw, cuser, resolvedname, ipaddr) == 0) {TRUE | never evaluated | FALSE | never evaluated |
| 0 |
207 | debug2("%s: auth_rhosts2 refused " | - |
208 | "user \"%.100s\" host \"%.100s\" addr \"%.100s\"", | - |
209 | __func__, cuser, resolvedname, ipaddr); | - |
210 | return 0; never executed: return 0; | 0 |
211 | } | - |
212 | lookup = resolvedname; | - |
213 | } never executed: end of block | 0 |
214 | debug2("%s: access allowed by auth_rhosts2", __func__); | - |
215 | | - |
216 | if (sshkey_is_cert(key) &&TRUE | never evaluated | FALSE | never evaluated |
| 0 |
217 | sshkey_cert_check_authority(key, 1, 0, lookup, &reason)) {TRUE | never evaluated | FALSE | never evaluated |
| 0 |
218 | error("%s", reason); | - |
219 | auth_debug_add("%s", reason); | - |
220 | return 0; never executed: return 0; | 0 |
221 | } | - |
222 | | - |
223 | host_status = check_key_in_hostfiles(pw, key, lookup, | - |
224 | _PATH_SSH_SYSTEM_HOSTFILE, | - |
225 | options.ignore_user_known_hosts ? NULL : _PATH_SSH_USER_HOSTFILE); | - |
226 | | - |
227 | | - |
228 | if (host_status == HOST_NEW) {TRUE | never evaluated | FALSE | never evaluated |
| 0 |
229 | host_status = check_key_in_hostfiles(pw, key, lookup, | - |
230 | _PATH_SSH_SYSTEM_HOSTFILE2, | - |
231 | options.ignore_user_known_hosts ? NULL : | - |
232 | _PATH_SSH_USER_HOSTFILE2); | - |
233 | } never executed: end of block | 0 |
234 | | - |
235 | if (host_status == HOST_OK) {TRUE | never evaluated | FALSE | never evaluated |
| 0 |
236 | if (sshkey_is_cert(key)) {TRUE | never evaluated | FALSE | never evaluated |
| 0 |
237 | if ((fp = sshkey_fingerprint(key->cert->signature_key,TRUE | never evaluated | FALSE | never evaluated |
| 0 |
238 | options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL)TRUE | never evaluated | FALSE | never evaluated |
| 0 |
239 | fatal("%s: sshkey_fingerprint fail", __func__); never executed: fatal("%s: sshkey_fingerprint fail", __func__); | 0 |
240 | verbose("Accepted certificate ID \"%s\" signed by " | - |
241 | "%s CA %s from %s@%s", key->cert->key_id, | - |
242 | sshkey_type(key->cert->signature_key), fp, | - |
243 | cuser, lookup); | - |
244 | } else { never executed: end of block | 0 |
245 | if ((fp = sshkey_fingerprint(key,TRUE | never evaluated | FALSE | never evaluated |
| 0 |
246 | options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL)TRUE | never evaluated | FALSE | never evaluated |
| 0 |
247 | fatal("%s: sshkey_fingerprint fail", __func__); never executed: fatal("%s: sshkey_fingerprint fail", __func__); | 0 |
248 | verbose("Accepted %s public key %s from %s@%s", | - |
249 | sshkey_type(key), fp, cuser, lookup); | - |
250 | } never executed: end of block | 0 |
251 | free(fp); | - |
252 | } never executed: end of block | 0 |
253 | | - |
254 | return (host_status == HOST_OK); never executed: return (host_status == HOST_OK); | 0 |
255 | } | - |
256 | | - |
257 | Authmethod method_hostbased = { | - |
258 | "hostbased", | - |
259 | userauth_hostbased, | - |
260 | &options.hostbased_authentication | - |
261 | }; | - |
| | |