1 |
|
|
/* $OpenBSD: auth2.c,v 1.143 2017/06/24 06:34:38 djm Exp $ */ |
2 |
|
|
/* |
3 |
|
|
* Copyright (c) 2000 Markus Friedl. All rights reserved. |
4 |
|
|
* |
5 |
|
|
* Redistribution and use in source and binary forms, with or without |
6 |
|
|
* modification, are permitted provided that the following conditions |
7 |
|
|
* are met: |
8 |
|
|
* 1. Redistributions of source code must retain the above copyright |
9 |
|
|
* notice, this list of conditions and the following disclaimer. |
10 |
|
|
* 2. Redistributions in binary form must reproduce the above copyright |
11 |
|
|
* notice, this list of conditions and the following disclaimer in the |
12 |
|
|
* documentation and/or other materials provided with the distribution. |
13 |
|
|
* |
14 |
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
15 |
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
16 |
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
17 |
|
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
18 |
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
19 |
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
20 |
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
21 |
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
22 |
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
23 |
|
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
24 |
|
|
*/ |
25 |
|
|
|
26 |
|
|
|
27 |
|
|
#include <sys/types.h> |
28 |
|
|
#include <sys/stat.h> |
29 |
|
|
#include <sys/uio.h> |
30 |
|
|
|
31 |
|
|
#include <fcntl.h> |
32 |
|
|
#include <limits.h> |
33 |
|
|
#include <pwd.h> |
34 |
|
|
#include <stdarg.h> |
35 |
|
|
#include <string.h> |
36 |
|
|
#include <unistd.h> |
37 |
|
|
|
38 |
|
|
#include "atomicio.h" |
39 |
|
|
#include "xmalloc.h" |
40 |
|
|
#include "ssh2.h" |
41 |
|
|
#include "packet.h" |
42 |
|
|
#include "log.h" |
43 |
|
|
#include "buffer.h" |
44 |
|
|
#include "misc.h" |
45 |
|
|
#include "servconf.h" |
46 |
|
|
#include "compat.h" |
47 |
|
|
#include "key.h" |
48 |
|
|
#include "hostfile.h" |
49 |
|
|
#include "auth.h" |
50 |
|
|
#include "dispatch.h" |
51 |
|
|
#include "pathnames.h" |
52 |
|
|
#ifdef GSSAPI |
53 |
|
|
#include "ssh-gss.h" |
54 |
|
|
#endif |
55 |
|
|
#include "monitor_wrap.h" |
56 |
|
|
#include "ssherr.h" |
57 |
|
|
|
58 |
|
|
/* import */ |
59 |
|
|
extern ServerOptions options; |
60 |
|
|
extern u_char *session_id2; |
61 |
|
|
extern u_int session_id2_len; |
62 |
|
|
|
63 |
|
|
/* methods */ |
64 |
|
|
|
65 |
|
|
extern Authmethod method_none; |
66 |
|
|
extern Authmethod method_pubkey; |
67 |
|
|
extern Authmethod method_passwd; |
68 |
|
|
extern Authmethod method_kbdint; |
69 |
|
|
extern Authmethod method_hostbased; |
70 |
|
|
#ifdef GSSAPI |
71 |
|
|
extern Authmethod method_gssapi; |
72 |
|
|
#endif |
73 |
|
|
|
74 |
|
|
Authmethod *authmethods[] = { |
75 |
|
|
&method_none, |
76 |
|
|
&method_pubkey, |
77 |
|
|
#ifdef GSSAPI |
78 |
|
|
&method_gssapi, |
79 |
|
|
#endif |
80 |
|
|
&method_passwd, |
81 |
|
|
&method_kbdint, |
82 |
|
|
&method_hostbased, |
83 |
|
|
NULL |
84 |
|
|
}; |
85 |
|
|
|
86 |
|
|
/* protocol */ |
87 |
|
|
|
88 |
|
|
static int input_service_request(int, u_int32_t, struct ssh *); |
89 |
|
|
static int input_userauth_request(int, u_int32_t, struct ssh *); |
90 |
|
|
|
91 |
|
|
/* helper */ |
92 |
|
|
static Authmethod *authmethod_lookup(Authctxt *, const char *); |
93 |
|
|
static char *authmethods_get(Authctxt *authctxt); |
94 |
|
|
|
95 |
|
|
#define MATCH_NONE 0 /* method or submethod mismatch */ |
96 |
|
|
#define MATCH_METHOD 1 /* method matches (no submethod specified) */ |
97 |
|
|
#define MATCH_BOTH 2 /* method and submethod match */ |
98 |
|
|
#define MATCH_PARTIAL 3 /* method matches, submethod can't be checked */ |
99 |
|
|
static int list_starts_with(const char *, const char *, const char *); |
100 |
|
|
|
101 |
|
|
char * |
102 |
|
|
auth2_read_banner(void) |
103 |
|
|
{ |
104 |
|
|
struct stat st; |
105 |
|
|
char *banner = NULL; |
106 |
|
|
size_t len, n; |
107 |
|
|
int fd; |
108 |
|
|
|
109 |
|
|
if ((fd = open(options.banner, O_RDONLY)) == -1) |
110 |
|
|
return (NULL); |
111 |
|
|
if (fstat(fd, &st) == -1) { |
112 |
|
|
close(fd); |
113 |
|
|
return (NULL); |
114 |
|
|
} |
115 |
|
|
if (st.st_size <= 0 || st.st_size > 1*1024*1024) { |
116 |
|
|
close(fd); |
117 |
|
|
return (NULL); |
118 |
|
|
} |
119 |
|
|
|
120 |
|
|
len = (size_t)st.st_size; /* truncate */ |
121 |
|
|
banner = xmalloc(len + 1); |
122 |
|
|
n = atomicio(read, fd, banner, len); |
123 |
|
|
close(fd); |
124 |
|
|
|
125 |
|
|
if (n != len) { |
126 |
|
|
free(banner); |
127 |
|
|
return (NULL); |
128 |
|
|
} |
129 |
|
|
banner[n] = '\0'; |
130 |
|
|
|
131 |
|
|
return (banner); |
132 |
|
|
} |
133 |
|
|
|
134 |
|
|
static void |
135 |
|
|
userauth_banner(void) |
136 |
|
|
{ |
137 |
|
|
char *banner = NULL; |
138 |
|
|
|
139 |
|
|
if (options.banner == NULL || (datafellows & SSH_BUG_BANNER) != 0) |
140 |
|
|
return; |
141 |
|
|
|
142 |
|
|
if ((banner = PRIVSEP(auth2_read_banner())) == NULL) |
143 |
|
|
goto done; |
144 |
|
|
|
145 |
|
|
packet_start(SSH2_MSG_USERAUTH_BANNER); |
146 |
|
|
packet_put_cstring(banner); |
147 |
|
|
packet_put_cstring(""); /* language, unused */ |
148 |
|
|
packet_send(); |
149 |
|
|
debug("userauth_banner: sent"); |
150 |
|
|
done: |
151 |
|
|
free(banner); |
152 |
|
|
} |
153 |
|
|
|
154 |
|
|
/* |
155 |
|
|
* loop until authctxt->success == TRUE |
156 |
|
|
*/ |
157 |
|
|
void |
158 |
|
|
do_authentication2(Authctxt *authctxt) |
159 |
|
|
{ |
160 |
|
|
struct ssh *ssh = active_state; /* XXX */ |
161 |
|
|
ssh->authctxt = authctxt; /* XXX move to caller */ |
162 |
|
|
ssh_dispatch_init(ssh, &dispatch_protocol_error); |
163 |
|
|
ssh_dispatch_set(ssh, SSH2_MSG_SERVICE_REQUEST, &input_service_request); |
164 |
|
|
ssh_dispatch_run_fatal(ssh, DISPATCH_BLOCK, &authctxt->success); |
165 |
|
|
ssh->authctxt = NULL; |
166 |
|
|
} |
167 |
|
|
|
168 |
|
|
/*ARGSUSED*/ |
169 |
|
|
static int |
170 |
|
|
input_service_request(int type, u_int32_t seq, struct ssh *ssh) |
171 |
|
|
{ |
172 |
|
|
Authctxt *authctxt = ssh->authctxt; |
173 |
|
|
u_int len; |
174 |
|
|
int acceptit = 0; |
175 |
|
|
char *service = packet_get_cstring(&len); |
176 |
|
|
packet_check_eom(); |
177 |
|
|
|
178 |
|
|
if (authctxt == NULL) |
179 |
|
|
fatal("input_service_request: no authctxt"); |
180 |
|
|
|
181 |
|
|
if (strcmp(service, "ssh-userauth") == 0) { |
182 |
|
|
if (!authctxt->success) { |
183 |
|
|
acceptit = 1; |
184 |
|
|
/* now we can handle user-auth requests */ |
185 |
|
|
ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_REQUEST, &input_userauth_request); |
186 |
|
|
} |
187 |
|
|
} |
188 |
|
|
/* XXX all other service requests are denied */ |
189 |
|
|
|
190 |
|
|
if (acceptit) { |
191 |
|
|
packet_start(SSH2_MSG_SERVICE_ACCEPT); |
192 |
|
|
packet_put_cstring(service); |
193 |
|
|
packet_send(); |
194 |
|
|
packet_write_wait(); |
195 |
|
|
} else { |
196 |
|
|
debug("bad service request %s", service); |
197 |
|
|
packet_disconnect("bad service request %s", service); |
198 |
|
|
} |
199 |
|
|
free(service); |
200 |
|
|
return 0; |
201 |
|
|
} |
202 |
|
|
|
203 |
|
|
/*ARGSUSED*/ |
204 |
|
|
static int |
205 |
|
|
input_userauth_request(int type, u_int32_t seq, struct ssh *ssh) |
206 |
|
|
{ |
207 |
|
|
Authctxt *authctxt = ssh->authctxt; |
208 |
|
|
Authmethod *m = NULL; |
209 |
|
|
char *user, *service, *method, *style = NULL; |
210 |
|
|
int authenticated = 0; |
211 |
|
|
|
212 |
|
|
if (authctxt == NULL) |
213 |
|
|
fatal("input_userauth_request: no authctxt"); |
214 |
|
|
|
215 |
|
|
user = packet_get_cstring(NULL); |
216 |
|
|
service = packet_get_cstring(NULL); |
217 |
|
|
method = packet_get_cstring(NULL); |
218 |
|
|
debug("userauth-request for user %s service %s method %s", user, service, method); |
219 |
|
|
debug("attempt %d failures %d", authctxt->attempt, authctxt->failures); |
220 |
|
|
|
221 |
|
|
if ((style = strchr(user, ':')) != NULL) |
222 |
|
|
*style++ = 0; |
223 |
|
|
|
224 |
|
|
if (authctxt->attempt++ == 0) { |
225 |
|
|
/* setup auth context */ |
226 |
|
|
authctxt->pw = PRIVSEP(getpwnamallow(user)); |
227 |
|
|
if (authctxt->pw && strcmp(service, "ssh-connection")==0) { |
228 |
|
|
authctxt->valid = 1; |
229 |
|
|
debug2("%s: setting up authctxt for %s", |
230 |
|
|
__func__, user); |
231 |
|
|
} else { |
232 |
|
|
/* Invalid user, fake password information */ |
233 |
|
|
authctxt->pw = fakepw(); |
234 |
|
|
} |
235 |
|
|
ssh_packet_set_log_preamble(ssh, "%suser %s", |
236 |
|
|
authctxt->valid ? "authenticating " : "invalid ", user); |
237 |
|
|
setproctitle("%s%s", authctxt->valid ? user : "unknown", |
238 |
|
|
use_privsep ? " [net]" : ""); |
239 |
|
|
authctxt->user = xstrdup(user); |
240 |
|
|
authctxt->service = xstrdup(service); |
241 |
|
|
authctxt->style = style ? xstrdup(style) : NULL; |
242 |
|
|
if (use_privsep) |
243 |
|
|
mm_inform_authserv(service, style); |
244 |
|
|
userauth_banner(); |
245 |
|
|
if (auth2_setup_methods_lists(authctxt) != 0) |
246 |
|
|
packet_disconnect("no authentication methods enabled"); |
247 |
|
|
} else if (strcmp(user, authctxt->user) != 0 || |
248 |
|
|
strcmp(service, authctxt->service) != 0) { |
249 |
|
|
packet_disconnect("Change of username or service not allowed: " |
250 |
|
|
"(%s,%s) -> (%s,%s)", |
251 |
|
|
authctxt->user, authctxt->service, user, service); |
252 |
|
|
} |
253 |
|
|
/* reset state */ |
254 |
|
|
auth2_challenge_stop(ssh); |
255 |
|
|
|
256 |
|
|
#ifdef GSSAPI |
257 |
|
|
/* XXX move to auth2_gssapi_stop() */ |
258 |
|
|
ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL); |
259 |
|
|
ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, NULL); |
260 |
|
|
#endif |
261 |
|
|
|
262 |
|
|
auth2_authctxt_reset_info(authctxt); |
263 |
|
|
authctxt->postponed = 0; |
264 |
|
|
authctxt->server_caused_failure = 0; |
265 |
|
|
|
266 |
|
|
/* try to authenticate user */ |
267 |
|
|
m = authmethod_lookup(authctxt, method); |
268 |
|
|
if (m != NULL && authctxt->failures < options.max_authtries) { |
269 |
|
|
debug2("input_userauth_request: try method %s", method); |
270 |
|
|
authenticated = m->userauth(ssh); |
271 |
|
|
} |
272 |
|
|
userauth_finish(ssh, authenticated, method, NULL); |
273 |
|
|
|
274 |
|
|
free(service); |
275 |
|
|
free(user); |
276 |
|
|
free(method); |
277 |
|
|
return 0; |
278 |
|
|
} |
279 |
|
|
|
280 |
|
|
void |
281 |
|
|
userauth_finish(struct ssh *ssh, int authenticated, const char *method, |
282 |
|
|
const char *submethod) |
283 |
|
|
{ |
284 |
|
|
Authctxt *authctxt = ssh->authctxt; |
285 |
|
|
char *methods; |
286 |
|
|
int partial = 0; |
287 |
|
|
|
288 |
|
|
if (!authctxt->valid && authenticated) |
289 |
|
|
fatal("INTERNAL ERROR: authenticated invalid user %s", |
290 |
|
|
authctxt->user); |
291 |
|
|
if (authenticated && authctxt->postponed) |
292 |
|
|
fatal("INTERNAL ERROR: authenticated and postponed"); |
293 |
|
|
|
294 |
|
|
/* Special handling for root */ |
295 |
|
|
if (authenticated && authctxt->pw->pw_uid == 0 && |
296 |
|
|
!auth_root_allowed(method)) |
297 |
|
|
authenticated = 0; |
298 |
|
|
|
299 |
|
|
if (authenticated && options.num_auth_methods != 0) { |
300 |
|
|
if (!auth2_update_methods_lists(authctxt, method, submethod)) { |
301 |
|
|
authenticated = 0; |
302 |
|
|
partial = 1; |
303 |
|
|
} |
304 |
|
|
} |
305 |
|
|
|
306 |
|
|
/* Log before sending the reply */ |
307 |
|
|
auth_log(authctxt, authenticated, partial, method, submethod); |
308 |
|
|
|
309 |
|
|
/* Update information exposed to session */ |
310 |
|
|
if (authenticated || partial) |
311 |
|
|
auth2_update_session_info(authctxt, method, submethod); |
312 |
|
|
|
313 |
|
|
if (authctxt->postponed) |
314 |
|
|
return; |
315 |
|
|
|
316 |
|
|
if (authenticated == 1) { |
317 |
|
|
/* turn off userauth */ |
318 |
|
|
ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_REQUEST, &dispatch_protocol_ignore); |
319 |
|
|
packet_start(SSH2_MSG_USERAUTH_SUCCESS); |
320 |
|
|
packet_send(); |
321 |
|
|
packet_write_wait(); |
322 |
|
|
/* now we can break out */ |
323 |
|
|
authctxt->success = 1; |
324 |
|
|
ssh_packet_set_log_preamble(ssh, "user %s", authctxt->user); |
325 |
|
|
} else { |
326 |
|
|
/* Allow initial try of "none" auth without failure penalty */ |
327 |
|
|
if (!partial && !authctxt->server_caused_failure && |
328 |
|
|
(authctxt->attempt > 1 || strcmp(method, "none") != 0)) |
329 |
|
|
authctxt->failures++; |
330 |
|
|
if (authctxt->failures >= options.max_authtries) |
331 |
|
|
auth_maxtries_exceeded(authctxt); |
332 |
|
|
methods = authmethods_get(authctxt); |
333 |
|
|
debug3("%s: failure partial=%d next methods=\"%s\"", __func__, |
334 |
|
|
partial, methods); |
335 |
|
|
packet_start(SSH2_MSG_USERAUTH_FAILURE); |
336 |
|
|
packet_put_cstring(methods); |
337 |
|
|
packet_put_char(partial); |
338 |
|
|
packet_send(); |
339 |
|
|
packet_write_wait(); |
340 |
|
|
free(methods); |
341 |
|
|
} |
342 |
|
|
} |
343 |
|
|
|
344 |
|
|
/* |
345 |
|
|
* Checks whether method is allowed by at least one AuthenticationMethods |
346 |
|
|
* methods list. Returns 1 if allowed, or no methods lists configured. |
347 |
|
|
* 0 otherwise. |
348 |
|
|
*/ |
349 |
|
|
int |
350 |
|
|
auth2_method_allowed(Authctxt *authctxt, const char *method, |
351 |
|
|
const char *submethod) |
352 |
|
|
{ |
353 |
|
|
u_int i; |
354 |
|
|
|
355 |
|
|
/* |
356 |
|
|
* NB. authctxt->num_auth_methods might be zero as a result of |
357 |
|
|
* auth2_setup_methods_lists(), so check the configuration. |
358 |
|
|
*/ |
359 |
|
|
if (options.num_auth_methods == 0) |
360 |
|
|
return 1; |
361 |
|
|
for (i = 0; i < authctxt->num_auth_methods; i++) { |
362 |
|
|
if (list_starts_with(authctxt->auth_methods[i], method, |
363 |
|
|
submethod) != MATCH_NONE) |
364 |
|
|
return 1; |
365 |
|
|
} |
366 |
|
|
return 0; |
367 |
|
|
} |
368 |
|
|
|
369 |
|
|
static char * |
370 |
|
|
authmethods_get(Authctxt *authctxt) |
371 |
|
|
{ |
372 |
|
|
Buffer b; |
373 |
|
|
char *list; |
374 |
|
|
u_int i; |
375 |
|
|
|
376 |
|
|
buffer_init(&b); |
377 |
|
|
for (i = 0; authmethods[i] != NULL; i++) { |
378 |
|
|
if (strcmp(authmethods[i]->name, "none") == 0) |
379 |
|
|
continue; |
380 |
|
|
if (authmethods[i]->enabled == NULL || |
381 |
|
|
*(authmethods[i]->enabled) == 0) |
382 |
|
|
continue; |
383 |
|
|
if (!auth2_method_allowed(authctxt, authmethods[i]->name, |
384 |
|
|
NULL)) |
385 |
|
|
continue; |
386 |
|
|
if (buffer_len(&b) > 0) |
387 |
|
|
buffer_append(&b, ",", 1); |
388 |
|
|
buffer_append(&b, authmethods[i]->name, |
389 |
|
|
strlen(authmethods[i]->name)); |
390 |
|
|
} |
391 |
|
|
if ((list = sshbuf_dup_string(&b)) == NULL) |
392 |
|
|
fatal("%s: sshbuf_dup_string failed", __func__); |
393 |
|
|
buffer_free(&b); |
394 |
|
|
return list; |
395 |
|
|
} |
396 |
|
|
|
397 |
|
|
static Authmethod * |
398 |
|
|
authmethod_lookup(Authctxt *authctxt, const char *name) |
399 |
|
|
{ |
400 |
|
|
int i; |
401 |
|
|
|
402 |
|
|
if (name != NULL) |
403 |
|
|
for (i = 0; authmethods[i] != NULL; i++) |
404 |
|
|
if (authmethods[i]->enabled != NULL && |
405 |
|
|
*(authmethods[i]->enabled) != 0 && |
406 |
|
|
strcmp(name, authmethods[i]->name) == 0 && |
407 |
|
|
auth2_method_allowed(authctxt, |
408 |
|
|
authmethods[i]->name, NULL)) |
409 |
|
|
return authmethods[i]; |
410 |
|
|
debug2("Unrecognized authentication method name: %s", |
411 |
|
|
name ? name : "NULL"); |
412 |
|
|
return NULL; |
413 |
|
|
} |
414 |
|
|
|
415 |
|
|
/* |
416 |
|
|
* Check a comma-separated list of methods for validity. Is need_enable is |
417 |
|
|
* non-zero, then also require that the methods are enabled. |
418 |
|
|
* Returns 0 on success or -1 if the methods list is invalid. |
419 |
|
|
*/ |
420 |
|
|
int |
421 |
|
|
auth2_methods_valid(const char *_methods, int need_enable) |
422 |
|
|
{ |
423 |
|
|
char *methods, *omethods, *method, *p; |
424 |
|
|
u_int i, found; |
425 |
|
|
int ret = -1; |
426 |
|
|
|
427 |
|
|
if (*_methods == '\0') { |
428 |
|
|
error("empty authentication method list"); |
429 |
|
|
return -1; |
430 |
|
|
} |
431 |
|
|
omethods = methods = xstrdup(_methods); |
432 |
|
|
while ((method = strsep(&methods, ",")) != NULL) { |
433 |
|
|
for (found = i = 0; !found && authmethods[i] != NULL; i++) { |
434 |
|
|
if ((p = strchr(method, ':')) != NULL) |
435 |
|
|
*p = '\0'; |
436 |
|
|
if (strcmp(method, authmethods[i]->name) != 0) |
437 |
|
|
continue; |
438 |
|
|
if (need_enable) { |
439 |
|
|
if (authmethods[i]->enabled == NULL || |
440 |
|
|
*(authmethods[i]->enabled) == 0) { |
441 |
|
|
error("Disabled method \"%s\" in " |
442 |
|
|
"AuthenticationMethods list \"%s\"", |
443 |
|
|
method, _methods); |
444 |
|
|
goto out; |
445 |
|
|
} |
446 |
|
|
} |
447 |
|
|
found = 1; |
448 |
|
|
break; |
449 |
|
|
} |
450 |
|
|
if (!found) { |
451 |
|
|
error("Unknown authentication method \"%s\" in list", |
452 |
|
|
method); |
453 |
|
|
goto out; |
454 |
|
|
} |
455 |
|
|
} |
456 |
|
|
ret = 0; |
457 |
|
|
out: |
458 |
|
|
free(omethods); |
459 |
|
|
return ret; |
460 |
|
|
} |
461 |
|
|
|
462 |
|
|
/* |
463 |
|
|
* Prune the AuthenticationMethods supplied in the configuration, removing |
464 |
|
|
* any methods lists that include disabled methods. Note that this might |
465 |
|
|
* leave authctxt->num_auth_methods == 0, even when multiple required auth |
466 |
|
|
* has been requested. For this reason, all tests for whether multiple is |
467 |
|
|
* enabled should consult options.num_auth_methods directly. |
468 |
|
|
*/ |
469 |
|
|
int |
470 |
|
|
auth2_setup_methods_lists(Authctxt *authctxt) |
471 |
|
|
{ |
472 |
|
|
u_int i; |
473 |
|
|
|
474 |
|
|
if (options.num_auth_methods == 0) |
475 |
|
|
return 0; |
476 |
|
|
debug3("%s: checking methods", __func__); |
477 |
|
|
authctxt->auth_methods = xcalloc(options.num_auth_methods, |
478 |
|
|
sizeof(*authctxt->auth_methods)); |
479 |
|
|
authctxt->num_auth_methods = 0; |
480 |
|
|
for (i = 0; i < options.num_auth_methods; i++) { |
481 |
|
|
if (auth2_methods_valid(options.auth_methods[i], 1) != 0) { |
482 |
|
|
logit("Authentication methods list \"%s\" contains " |
483 |
|
|
"disabled method, skipping", |
484 |
|
|
options.auth_methods[i]); |
485 |
|
|
continue; |
486 |
|
|
} |
487 |
|
|
debug("authentication methods list %d: %s", |
488 |
|
|
authctxt->num_auth_methods, options.auth_methods[i]); |
489 |
|
|
authctxt->auth_methods[authctxt->num_auth_methods++] = |
490 |
|
|
xstrdup(options.auth_methods[i]); |
491 |
|
|
} |
492 |
|
|
if (authctxt->num_auth_methods == 0) { |
493 |
|
|
error("No AuthenticationMethods left after eliminating " |
494 |
|
|
"disabled methods"); |
495 |
|
|
return -1; |
496 |
|
|
} |
497 |
|
|
return 0; |
498 |
|
|
} |
499 |
|
|
|
500 |
|
|
static int |
501 |
|
|
list_starts_with(const char *methods, const char *method, |
502 |
|
|
const char *submethod) |
503 |
|
|
{ |
504 |
|
|
size_t l = strlen(method); |
505 |
|
|
int match; |
506 |
|
|
const char *p; |
507 |
|
|
|
508 |
|
|
if (strncmp(methods, method, l) != 0) |
509 |
|
|
return MATCH_NONE; |
510 |
|
|
p = methods + l; |
511 |
|
|
match = MATCH_METHOD; |
512 |
|
|
if (*p == ':') { |
513 |
|
|
if (!submethod) |
514 |
|
|
return MATCH_PARTIAL; |
515 |
|
|
l = strlen(submethod); |
516 |
|
|
p += 1; |
517 |
|
|
if (strncmp(submethod, p, l)) |
518 |
|
|
return MATCH_NONE; |
519 |
|
|
p += l; |
520 |
|
|
match = MATCH_BOTH; |
521 |
|
|
} |
522 |
|
|
if (*p != ',' && *p != '\0') |
523 |
|
|
return MATCH_NONE; |
524 |
|
|
return match; |
525 |
|
|
} |
526 |
|
|
|
527 |
|
|
/* |
528 |
|
|
* Remove method from the start of a comma-separated list of methods. |
529 |
|
|
* Returns 0 if the list of methods did not start with that method or 1 |
530 |
|
|
* if it did. |
531 |
|
|
*/ |
532 |
|
|
static int |
533 |
|
|
remove_method(char **methods, const char *method, const char *submethod) |
534 |
|
|
{ |
535 |
|
|
char *omethods = *methods, *p; |
536 |
|
|
size_t l = strlen(method); |
537 |
|
|
int match; |
538 |
|
|
|
539 |
|
|
match = list_starts_with(omethods, method, submethod); |
540 |
|
|
if (match != MATCH_METHOD && match != MATCH_BOTH) |
541 |
|
|
return 0; |
542 |
|
|
p = omethods + l; |
543 |
|
|
if (submethod && match == MATCH_BOTH) |
544 |
|
|
p += 1 + strlen(submethod); /* include colon */ |
545 |
|
|
if (*p == ',') |
546 |
|
|
p++; |
547 |
|
|
*methods = xstrdup(p); |
548 |
|
|
free(omethods); |
549 |
|
|
return 1; |
550 |
|
|
} |
551 |
|
|
|
552 |
|
|
/* |
553 |
|
|
* Called after successful authentication. Will remove the successful method |
554 |
|
|
* from the start of each list in which it occurs. If it was the last method |
555 |
|
|
* in any list, then authentication is deemed successful. |
556 |
|
|
* Returns 1 if the method completed any authentication list or 0 otherwise. |
557 |
|
|
*/ |
558 |
|
|
int |
559 |
|
|
auth2_update_methods_lists(Authctxt *authctxt, const char *method, |
560 |
|
|
const char *submethod) |
561 |
|
|
{ |
562 |
|
|
u_int i, found = 0; |
563 |
|
|
|
564 |
|
|
debug3("%s: updating methods list after \"%s\"", __func__, method); |
565 |
|
|
for (i = 0; i < authctxt->num_auth_methods; i++) { |
566 |
|
|
if (!remove_method(&(authctxt->auth_methods[i]), method, |
567 |
|
|
submethod)) |
568 |
|
|
continue; |
569 |
|
|
found = 1; |
570 |
|
|
if (*authctxt->auth_methods[i] == '\0') { |
571 |
|
|
debug2("authentication methods list %d complete", i); |
572 |
|
|
return 1; |
573 |
|
|
} |
574 |
|
|
debug3("authentication methods list %d remaining: \"%s\"", |
575 |
|
|
i, authctxt->auth_methods[i]); |
576 |
|
|
} |
577 |
|
|
/* This should not happen, but would be bad if it did */ |
578 |
|
|
if (!found) |
579 |
|
|
fatal("%s: method not in AuthenticationMethods", __func__); |
580 |
|
|
return 0; |
581 |
|
|
} |
582 |
|
|
|
583 |
|
|
/* Reset method-specific information */ |
584 |
|
|
void auth2_authctxt_reset_info(Authctxt *authctxt) |
585 |
|
|
{ |
586 |
|
|
sshkey_free(authctxt->auth_method_key); |
587 |
|
|
free(authctxt->auth_method_info); |
588 |
|
|
authctxt->auth_method_key = NULL; |
589 |
|
|
authctxt->auth_method_info = NULL; |
590 |
|
|
} |
591 |
|
|
|
592 |
|
|
/* Record auth method-specific information for logs */ |
593 |
|
|
void |
594 |
|
|
auth2_record_info(Authctxt *authctxt, const char *fmt, ...) |
595 |
|
|
{ |
596 |
|
|
va_list ap; |
597 |
|
|
int i; |
598 |
|
|
|
599 |
|
|
free(authctxt->auth_method_info); |
600 |
|
|
authctxt->auth_method_info = NULL; |
601 |
|
|
|
602 |
|
|
va_start(ap, fmt); |
603 |
|
|
i = vasprintf(&authctxt->auth_method_info, fmt, ap); |
604 |
|
|
va_end(ap); |
605 |
|
|
|
606 |
|
|
if (i < 0 || authctxt->auth_method_info == NULL) |
607 |
|
|
fatal("%s: vasprintf failed", __func__); |
608 |
|
|
} |
609 |
|
|
|
610 |
|
|
/* |
611 |
|
|
* Records a public key used in authentication. This is used for logging |
612 |
|
|
* and to ensure that the same key is not subsequently accepted again for |
613 |
|
|
* multiple authentication. |
614 |
|
|
*/ |
615 |
|
|
void |
616 |
|
|
auth2_record_key(Authctxt *authctxt, int authenticated, |
617 |
|
|
const struct sshkey *key) |
618 |
|
|
{ |
619 |
|
|
struct sshkey **tmp, *dup; |
620 |
|
|
int r; |
621 |
|
|
|
622 |
|
|
if ((r = sshkey_demote(key, &dup)) != 0) |
623 |
|
|
fatal("%s: copy key: %s", __func__, ssh_err(r)); |
624 |
|
|
sshkey_free(authctxt->auth_method_key); |
625 |
|
|
authctxt->auth_method_key = dup; |
626 |
|
|
|
627 |
|
|
if (!authenticated) |
628 |
|
|
return; |
629 |
|
|
|
630 |
|
|
/* If authenticated, make sure we don't accept this key again */ |
631 |
|
|
if ((r = sshkey_demote(key, &dup)) != 0) |
632 |
|
|
fatal("%s: copy key: %s", __func__, ssh_err(r)); |
633 |
|
|
if (authctxt->nprev_keys >= INT_MAX || |
634 |
|
|
(tmp = recallocarray(authctxt->prev_keys, authctxt->nprev_keys, |
635 |
|
|
authctxt->nprev_keys + 1, sizeof(*authctxt->prev_keys))) == NULL) |
636 |
|
|
fatal("%s: reallocarray failed", __func__); |
637 |
|
|
authctxt->prev_keys = tmp; |
638 |
|
|
authctxt->prev_keys[authctxt->nprev_keys] = dup; |
639 |
|
|
authctxt->nprev_keys++; |
640 |
|
|
|
641 |
|
|
} |
642 |
|
|
|
643 |
|
|
/* Checks whether a key has already been previously used for authentication */ |
644 |
|
|
int |
645 |
|
|
auth2_key_already_used(Authctxt *authctxt, const struct sshkey *key) |
646 |
|
|
{ |
647 |
|
|
u_int i; |
648 |
|
|
char *fp; |
649 |
|
|
|
650 |
|
|
for (i = 0; i < authctxt->nprev_keys; i++) { |
651 |
|
|
if (sshkey_equal_public(key, authctxt->prev_keys[i])) { |
652 |
|
|
fp = sshkey_fingerprint(authctxt->prev_keys[i], |
653 |
|
|
options.fingerprint_hash, SSH_FP_DEFAULT); |
654 |
|
|
debug3("%s: key already used: %s %s", __func__, |
655 |
|
|
sshkey_type(authctxt->prev_keys[i]), |
656 |
|
|
fp == NULL ? "UNKNOWN" : fp); |
657 |
|
|
free(fp); |
658 |
|
|
return 1; |
659 |
|
|
} |
660 |
|
|
} |
661 |
|
|
return 0; |
662 |
|
|
} |
663 |
|
|
|
664 |
|
|
/* |
665 |
|
|
* Updates authctxt->session_info with details of authentication. Should be |
666 |
|
|
* whenever an authentication method succeeds. |
667 |
|
|
*/ |
668 |
|
|
void |
669 |
|
|
auth2_update_session_info(Authctxt *authctxt, const char *method, |
670 |
|
|
const char *submethod) |
671 |
|
|
{ |
672 |
|
|
int r; |
673 |
|
|
|
674 |
|
|
if (authctxt->session_info == NULL) { |
675 |
|
|
if ((authctxt->session_info = sshbuf_new()) == NULL) |
676 |
|
|
fatal("%s: sshbuf_new", __func__); |
677 |
|
|
} |
678 |
|
|
|
679 |
|
|
/* Append method[/submethod] */ |
680 |
|
|
if ((r = sshbuf_putf(authctxt->session_info, "%s%s%s", |
681 |
|
|
method, submethod == NULL ? "" : "/", |
682 |
|
|
submethod == NULL ? "" : submethod)) != 0) |
683 |
|
|
fatal("%s: append method: %s", __func__, ssh_err(r)); |
684 |
|
|
|
685 |
|
|
/* Append key if present */ |
686 |
|
|
if (authctxt->auth_method_key != NULL) { |
687 |
|
|
if ((r = sshbuf_put_u8(authctxt->session_info, ' ')) != 0 || |
688 |
|
|
(r = sshkey_format_text(authctxt->auth_method_key, |
689 |
|
|
authctxt->session_info)) != 0) |
690 |
|
|
fatal("%s: append key: %s", __func__, ssh_err(r)); |
691 |
|
|
} |
692 |
|
|
|
693 |
|
|
if (authctxt->auth_method_info != NULL) { |
694 |
|
|
/* Ensure no ambiguity here */ |
695 |
|
|
if (strchr(authctxt->auth_method_info, '\n') != NULL) |
696 |
|
|
fatal("%s: auth_method_info contains \\n", __func__); |
697 |
|
|
if ((r = sshbuf_put_u8(authctxt->session_info, ' ')) != 0 || |
698 |
|
|
(r = sshbuf_putf(authctxt->session_info, "%s", |
699 |
|
|
authctxt->auth_method_info)) != 0) { |
700 |
|
|
fatal("%s: append method info: %s", |
701 |
|
|
__func__, ssh_err(r)); |
702 |
|
|
} |
703 |
|
|
} |
704 |
|
|
if ((r = sshbuf_put_u8(authctxt->session_info, '\n')) != 0) |
705 |
|
|
fatal("%s: append: %s", __func__, ssh_err(r)); |
706 |
|
|
} |
707 |
|
|
|