]>
Commit | Line | Data |
---|---|---|
a306f2dd | 1 | /* |
2 | * Copyright (c) 2000 Markus Friedl. All rights reserved. | |
3 | * | |
4 | * Redistribution and use in source and binary forms, with or without | |
5 | * modification, are permitted provided that the following conditions | |
6 | * are met: | |
7 | * 1. Redistributions of source code must retain the above copyright | |
8 | * notice, this list of conditions and the following disclaimer. | |
9 | * 2. Redistributions in binary form must reproduce the above copyright | |
10 | * notice, this list of conditions and the following disclaimer in the | |
11 | * documentation and/or other materials provided with the distribution. | |
a306f2dd | 12 | * |
13 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR | |
14 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | |
15 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. | |
16 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, | |
17 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | |
18 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
19 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
20 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
21 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | |
22 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
23 | */ | |
bcbf86ec | 24 | |
a306f2dd | 25 | #include "includes.h" |
eea39c02 | 26 | RCSID("$OpenBSD: auth2.c,v 1.25 2001/01/08 22:29:05 markus Exp $"); |
94ec8c6b | 27 | |
28 | #ifdef HAVE_OSF_SIA | |
29 | # include <sia.h> | |
30 | # include <siad.h> | |
31 | #endif | |
a306f2dd | 32 | |
33 | #include <openssl/dsa.h> | |
34 | #include <openssl/rsa.h> | |
35 | #include <openssl/evp.h> | |
36 | ||
37 | #include "xmalloc.h" | |
38 | #include "rsa.h" | |
39 | #include "ssh.h" | |
40 | #include "pty.h" | |
41 | #include "packet.h" | |
42 | #include "buffer.h" | |
a306f2dd | 43 | #include "servconf.h" |
44 | #include "compat.h" | |
45 | #include "channels.h" | |
46 | #include "bufaux.h" | |
47 | #include "ssh2.h" | |
48 | #include "auth.h" | |
49 | #include "session.h" | |
50 | #include "dispatch.h" | |
51 | #include "auth.h" | |
52 | #include "key.h" | |
53 | #include "kex.h" | |
54 | ||
a306f2dd | 55 | #include "uidswap.h" |
38c295d6 | 56 | #include "auth-options.h" |
a306f2dd | 57 | |
58 | /* import */ | |
59 | extern ServerOptions options; | |
1e3b8b07 | 60 | extern u_char *session_id2; |
a306f2dd | 61 | extern int session_id2_len; |
62 | ||
94ec8c6b | 63 | #ifdef WITH_AIXAUTHENTICATE |
64 | extern char *aixloginmsg; | |
65 | #endif | |
66 | #ifdef HAVE_OSF_SIA | |
67 | extern int saved_argc; | |
68 | extern char **saved_argv; | |
69 | #endif | |
70 | ||
71 | static Authctxt *x_authctxt = NULL; | |
72 | static int one = 1; | |
73 | ||
74 | typedef struct Authmethod Authmethod; | |
75 | struct Authmethod { | |
76 | char *name; | |
77 | int (*userauth)(Authctxt *authctxt); | |
78 | int *enabled; | |
79 | }; | |
80 | ||
a306f2dd | 81 | /* protocol */ |
82 | ||
188adeb2 | 83 | void input_service_request(int type, int plen, void *ctxt); |
84 | void input_userauth_request(int type, int plen, void *ctxt); | |
85 | void protocol_error(int type, int plen, void *ctxt); | |
a306f2dd | 86 | |
a306f2dd | 87 | |
88 | /* helper */ | |
94ec8c6b | 89 | Authmethod *authmethod_lookup(const char *name); |
90 | struct passwd *pwcopy(struct passwd *pw); | |
fa08c86b | 91 | int user_key_allowed(struct passwd *pw, Key *key); |
94ec8c6b | 92 | char *authmethods_get(void); |
a306f2dd | 93 | |
94ec8c6b | 94 | /* auth */ |
eea39c02 | 95 | void userauth_banner(void); |
94ec8c6b | 96 | int userauth_none(Authctxt *authctxt); |
97 | int userauth_passwd(Authctxt *authctxt); | |
98 | int userauth_pubkey(Authctxt *authctxt); | |
99 | int userauth_kbdint(Authctxt *authctxt); | |
100 | ||
101 | Authmethod authmethods[] = { | |
102 | {"none", | |
103 | userauth_none, | |
104 | &one}, | |
105 | {"publickey", | |
106 | userauth_pubkey, | |
fa08c86b | 107 | &options.pubkey_authentication}, |
94ec8c6b | 108 | {"keyboard-interactive", |
109 | userauth_kbdint, | |
110 | &options.kbd_interactive_authentication}, | |
111 | {"password", | |
112 | userauth_passwd, | |
113 | &options.password_authentication}, | |
114 | {NULL, NULL, NULL} | |
a306f2dd | 115 | }; |
a306f2dd | 116 | |
117 | /* | |
94ec8c6b | 118 | * loop until authctxt->success == TRUE |
a306f2dd | 119 | */ |
120 | ||
121 | void | |
122 | do_authentication2() | |
123 | { | |
94ec8c6b | 124 | Authctxt *authctxt = xmalloc(sizeof(*authctxt)); |
125 | memset(authctxt, 'a', sizeof(*authctxt)); | |
126 | authctxt->valid = 0; | |
127 | authctxt->attempt = 0; | |
8abcdba4 | 128 | authctxt->failures = 0; |
94ec8c6b | 129 | authctxt->success = 0; |
130 | x_authctxt = authctxt; /*XXX*/ | |
131 | ||
8cb940db | 132 | #ifdef KRB4 |
94ec8c6b | 133 | /* turn off kerberos, not supported by SSH2 */ |
3189621b | 134 | options.kerberos_authentication = 0; |
8cb940db | 135 | #endif |
a306f2dd | 136 | dispatch_init(&protocol_error); |
137 | dispatch_set(SSH2_MSG_SERVICE_REQUEST, &input_service_request); | |
94ec8c6b | 138 | dispatch_run(DISPATCH_BLOCK, &authctxt->success, authctxt); |
a306f2dd | 139 | do_authenticated2(); |
140 | } | |
141 | ||
142 | void | |
188adeb2 | 143 | protocol_error(int type, int plen, void *ctxt) |
a306f2dd | 144 | { |
145 | log("auth: protocol error: type %d plen %d", type, plen); | |
146 | packet_start(SSH2_MSG_UNIMPLEMENTED); | |
147 | packet_put_int(0); | |
148 | packet_send(); | |
149 | packet_write_wait(); | |
150 | } | |
151 | ||
152 | void | |
188adeb2 | 153 | input_service_request(int type, int plen, void *ctxt) |
a306f2dd | 154 | { |
94ec8c6b | 155 | Authctxt *authctxt = ctxt; |
1e3b8b07 | 156 | u_int len; |
a306f2dd | 157 | int accept = 0; |
158 | char *service = packet_get_string(&len); | |
159 | packet_done(); | |
160 | ||
94ec8c6b | 161 | if (authctxt == NULL) |
162 | fatal("input_service_request: no authctxt"); | |
163 | ||
a306f2dd | 164 | if (strcmp(service, "ssh-userauth") == 0) { |
94ec8c6b | 165 | if (!authctxt->success) { |
a306f2dd | 166 | accept = 1; |
167 | /* now we can handle user-auth requests */ | |
168 | dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &input_userauth_request); | |
169 | } | |
170 | } | |
171 | /* XXX all other service requests are denied */ | |
172 | ||
173 | if (accept) { | |
174 | packet_start(SSH2_MSG_SERVICE_ACCEPT); | |
175 | packet_put_cstring(service); | |
176 | packet_send(); | |
177 | packet_write_wait(); | |
178 | } else { | |
179 | debug("bad service request %s", service); | |
180 | packet_disconnect("bad service request %s", service); | |
181 | } | |
182 | xfree(service); | |
183 | } | |
184 | ||
185 | void | |
188adeb2 | 186 | input_userauth_request(int type, int plen, void *ctxt) |
a306f2dd | 187 | { |
94ec8c6b | 188 | Authctxt *authctxt = ctxt; |
189 | Authmethod *m = NULL; | |
190 | char *user, *service, *method; | |
a306f2dd | 191 | int authenticated = 0; |
a306f2dd | 192 | |
94ec8c6b | 193 | if (authctxt == NULL) |
194 | fatal("input_userauth_request: no authctxt"); | |
a306f2dd | 195 | |
94ec8c6b | 196 | user = packet_get_string(NULL); |
197 | service = packet_get_string(NULL); | |
198 | method = packet_get_string(NULL); | |
199 | debug("userauth-request for user %s service %s method %s", user, service, method); | |
8abcdba4 | 200 | debug("attempt %d failures %d", authctxt->attempt, authctxt->failures); |
94ec8c6b | 201 | |
8abcdba4 | 202 | if (authctxt->attempt++ == 0) { |
94ec8c6b | 203 | /* setup auth context */ |
204 | struct passwd *pw = NULL; | |
205 | setproctitle("%s", user); | |
206 | pw = getpwnam(user); | |
207 | if (pw && allowed_user(pw) && strcmp(service, "ssh-connection")==0) { | |
208 | authctxt->pw = pwcopy(pw); | |
209 | authctxt->valid = 1; | |
210 | debug2("input_userauth_request: setting up authctxt for %s", user); | |
211 | #ifdef USE_PAM | |
212 | start_pam(pw); | |
213 | #endif | |
214 | } else { | |
215 | log("input_userauth_request: illegal user %s", user); | |
216 | } | |
217 | authctxt->user = xstrdup(user); | |
218 | authctxt->service = xstrdup(service); | |
219 | } else if (authctxt->valid) { | |
220 | if (strcmp(user, authctxt->user) != 0 || | |
221 | strcmp(service, authctxt->service) != 0) { | |
222 | log("input_userauth_request: missmatch: (%s,%s)!=(%s,%s)", | |
223 | user, service, authctxt->user, authctxt->service); | |
224 | authctxt->valid = 0; | |
a306f2dd | 225 | } |
226 | } | |
9cd45ea4 | 227 | |
94ec8c6b | 228 | m = authmethod_lookup(method); |
229 | if (m != NULL) { | |
230 | debug2("input_userauth_request: try method %s", method); | |
231 | authenticated = m->userauth(authctxt); | |
232 | } else { | |
233 | debug2("input_userauth_request: unsupported method %s", method); | |
234 | } | |
235 | if (!authctxt->valid && authenticated == 1) { | |
236 | log("input_userauth_request: INTERNAL ERROR: authenticated invalid user %s service %s", user, method); | |
9cd45ea4 | 237 | authenticated = 0; |
238 | } | |
9cd45ea4 | 239 | |
94ec8c6b | 240 | /* Special handling for root */ |
241 | if (authenticated == 1 && | |
242 | authctxt->valid && authctxt->pw->pw_uid == 0 && !options.permit_root_login) { | |
a306f2dd | 243 | authenticated = 0; |
94ec8c6b | 244 | log("ROOT LOGIN REFUSED FROM %.200s", get_canonical_hostname()); |
a306f2dd | 245 | } |
246 | ||
247 | #ifdef USE_PAM | |
606ea390 | 248 | if (authenticated && authctxt->user && !do_pam_account(authctxt->user, NULL)) |
9cd45ea4 | 249 | authenticated = 0; |
a306f2dd | 250 | #endif /* USE_PAM */ |
251 | ||
94ec8c6b | 252 | /* Log before sending the reply */ |
253 | userauth_log(authctxt, authenticated, method); | |
254 | userauth_reply(authctxt, authenticated); | |
255 | ||
256 | xfree(service); | |
257 | xfree(user); | |
258 | xfree(method); | |
259 | } | |
260 | ||
eea39c02 | 261 | void |
262 | userauth_banner(void) | |
263 | { | |
264 | struct stat st; | |
265 | char *banner = NULL; | |
266 | off_t len, n; | |
267 | int fd; | |
268 | ||
269 | if (options.banner == NULL || (datafellows & SSH_BUG_BANNER)) | |
270 | return; | |
271 | if ((fd = open(options.banner, O_RDONLY)) < 0) { | |
272 | error("userauth_banner: open %s failed: %s", | |
273 | options.banner, strerror(errno)); | |
274 | return; | |
275 | } | |
276 | if (fstat(fd, &st) < 0) | |
277 | goto done; | |
278 | len = st.st_size; | |
279 | banner = xmalloc(len + 1); | |
280 | if ((n = read(fd, banner, len)) < 0) | |
281 | goto done; | |
282 | banner[n] = '\0'; | |
283 | packet_start(SSH2_MSG_USERAUTH_BANNER); | |
284 | packet_put_cstring(banner); | |
285 | packet_put_cstring(""); /* language, unused */ | |
286 | packet_send(); | |
287 | debug("userauth_banner: sent"); | |
288 | done: | |
289 | if (banner) | |
290 | xfree(banner); | |
291 | close(fd); | |
292 | return; | |
293 | } | |
94ec8c6b | 294 | |
295 | void | |
296 | userauth_log(Authctxt *authctxt, int authenticated, char *method) | |
297 | { | |
298 | void (*authlog) (const char *fmt,...) = verbose; | |
299 | char *user = NULL, *authmsg = NULL; | |
300 | ||
1d1ffb87 | 301 | /* Raise logging level */ |
302 | if (authenticated == 1 || | |
94ec8c6b | 303 | !authctxt->valid || |
8abcdba4 | 304 | authctxt->failures >= AUTH_FAIL_LOG || |
1d1ffb87 | 305 | strcmp(method, "password") == 0) |
306 | authlog = log; | |
307 | ||
a306f2dd | 308 | if (authenticated == 1) { |
309 | authmsg = "Accepted"; | |
1d1ffb87 | 310 | } else if (authenticated == 0) { |
311 | authmsg = "Failed"; | |
312 | } else { | |
313 | authmsg = "Postponed"; | |
314 | } | |
94ec8c6b | 315 | |
316 | if (authctxt->valid) { | |
317 | user = authctxt->pw->pw_uid == 0 ? "ROOT" : authctxt->user; | |
318 | } else { | |
319 | user = "NOUSER"; | |
320 | } | |
321 | ||
1d1ffb87 | 322 | authlog("%s %s for %.200s from %.200s port %d ssh2", |
94ec8c6b | 323 | authmsg, |
324 | method, | |
325 | user, | |
326 | get_remote_ipaddr(), | |
327 | get_remote_port()); | |
328 | } | |
1d1ffb87 | 329 | |
94ec8c6b | 330 | void |
331 | userauth_reply(Authctxt *authctxt, int authenticated) | |
332 | { | |
8abcdba4 | 333 | char *methods; |
1d1ffb87 | 334 | /* XXX todo: check if multiple auth methods are needed */ |
335 | if (authenticated == 1) { | |
c1ef8333 | 336 | #ifdef WITH_AIXAUTHENTICATE |
337 | /* We don't have a pty yet, so just label the line as "ssh" */ | |
606ea390 | 338 | if (loginsuccess(authctxt->user?authctxt->user:"NOUSER", |
339 | get_canonical_hostname(), "ssh", &aixloginmsg) < 0) | |
c1ef8333 | 340 | aixloginmsg = NULL; |
341 | #endif /* WITH_AIXAUTHENTICATE */ | |
a306f2dd | 342 | /* turn off userauth */ |
343 | dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &protocol_error); | |
344 | packet_start(SSH2_MSG_USERAUTH_SUCCESS); | |
345 | packet_send(); | |
346 | packet_write_wait(); | |
347 | /* now we can break out */ | |
94ec8c6b | 348 | authctxt->success = 1; |
a306f2dd | 349 | } else if (authenticated == 0) { |
8abcdba4 | 350 | if (authctxt->failures++ >= AUTH_FAIL_MAX) |
351 | packet_disconnect("too many failed userauth_requests"); | |
352 | methods = authmethods_get(); | |
a306f2dd | 353 | packet_start(SSH2_MSG_USERAUTH_FAILURE); |
94ec8c6b | 354 | packet_put_cstring(methods); |
355 | packet_put_char(0); /* XXX partial success, unused */ | |
a306f2dd | 356 | packet_send(); |
357 | packet_write_wait(); | |
94ec8c6b | 358 | xfree(methods); |
359 | } else { | |
360 | /* do nothing, we did already send a reply */ | |
a306f2dd | 361 | } |
a306f2dd | 362 | } |
363 | ||
364 | int | |
94ec8c6b | 365 | userauth_none(Authctxt *authctxt) |
a306f2dd | 366 | { |
94ec8c6b | 367 | /* disable method "none", only allowed one time */ |
368 | Authmethod *m = authmethod_lookup("none"); | |
369 | if (m != NULL) | |
370 | m->enabled = NULL; | |
a306f2dd | 371 | packet_done(); |
eea39c02 | 372 | userauth_banner(); |
4d33e531 | 373 | |
94ec8c6b | 374 | if (authctxt->valid == 0) |
375 | return(0); | |
376 | ||
377 | #ifdef HAVE_CYGWIN | |
378 | if (check_nt_auth(1, authctxt->pw->pw_uid) == 0) | |
379 | return(0); | |
380 | #endif | |
a306f2dd | 381 | #ifdef USE_PAM |
94ec8c6b | 382 | return auth_pam_password(authctxt->pw, ""); |
4d33e531 | 383 | #elif defined(HAVE_OSF_SIA) |
94ec8c6b | 384 | return (sia_validate_user(NULL, saved_argc, saved_argv, |
606ea390 | 385 | get_canonical_hostname(), authctxt->user?authctxt->user:"NOUSER", |
386 | NULL, 0, NULL, "") == SIASUCCESS); | |
4d33e531 | 387 | #else /* !HAVE_OSF_SIA && !USE_PAM */ |
94ec8c6b | 388 | return auth_password(authctxt->pw, ""); |
a306f2dd | 389 | #endif /* USE_PAM */ |
390 | } | |
94ec8c6b | 391 | |
a306f2dd | 392 | int |
94ec8c6b | 393 | userauth_passwd(Authctxt *authctxt) |
a306f2dd | 394 | { |
395 | char *password; | |
396 | int authenticated = 0; | |
397 | int change; | |
1e3b8b07 | 398 | u_int len; |
a306f2dd | 399 | change = packet_get_char(); |
400 | if (change) | |
401 | log("password change not supported"); | |
402 | password = packet_get_string(&len); | |
403 | packet_done(); | |
94ec8c6b | 404 | if (authctxt->valid && |
405 | #ifdef HAVE_CYGWIN | |
406 | check_nt_auth(1, authctxt->pw->pw_uid) && | |
407 | #endif | |
a306f2dd | 408 | #ifdef USE_PAM |
94ec8c6b | 409 | auth_pam_password(authctxt->pw, password) == 1) |
4d33e531 | 410 | #elif defined(HAVE_OSF_SIA) |
411 | sia_validate_user(NULL, saved_argc, saved_argv, | |
606ea390 | 412 | get_canonical_hostname(), authctxt->user?authctxt->user:"NOUSER", |
413 | NULL, 0, NULL, password) == SIASUCCESS) | |
4d33e531 | 414 | #else /* !USE_PAM && !HAVE_OSF_SIA */ |
94ec8c6b | 415 | auth_password(authctxt->pw, password) == 1) |
a306f2dd | 416 | #endif /* USE_PAM */ |
417 | authenticated = 1; | |
418 | memset(password, 0, len); | |
419 | xfree(password); | |
420 | return authenticated; | |
421 | } | |
94ec8c6b | 422 | |
423 | int | |
424 | userauth_kbdint(Authctxt *authctxt) | |
425 | { | |
426 | int authenticated = 0; | |
427 | char *lang = NULL; | |
428 | char *devs = NULL; | |
429 | ||
430 | lang = packet_get_string(NULL); | |
431 | devs = packet_get_string(NULL); | |
432 | packet_done(); | |
433 | ||
434 | debug("keyboard-interactive language %s devs %s", lang, devs); | |
8c9fe09e | 435 | #ifdef USE_PAM |
436 | if (authenticated == 0) | |
437 | authenticated = auth2_pam(authctxt); | |
438 | #endif | |
94ec8c6b | 439 | #ifdef SKEY |
440 | /* XXX hardcoded, we should look at devs */ | |
8c9fe09e | 441 | if (authenticated == 0) |
442 | if (options.skey_authentication != 0) | |
443 | authenticated = auth2_skey(authctxt); | |
94ec8c6b | 444 | #endif |
445 | xfree(lang); | |
446 | xfree(devs); | |
447 | #ifdef HAVE_CYGWIN | |
448 | if (check_nt_auth(0, authctxt->pw->pw_uid) == 0) | |
449 | return(0); | |
450 | #endif | |
451 | return authenticated; | |
452 | } | |
453 | ||
a306f2dd | 454 | int |
94ec8c6b | 455 | userauth_pubkey(Authctxt *authctxt) |
a306f2dd | 456 | { |
457 | Buffer b; | |
458 | Key *key; | |
459 | char *pkalg, *pkblob, *sig; | |
1e3b8b07 | 460 | u_int alen, blen, slen; |
fa08c86b | 461 | int have_sig, pktype; |
a306f2dd | 462 | int authenticated = 0; |
463 | ||
94ec8c6b | 464 | if (!authctxt->valid) { |
465 | debug2("userauth_pubkey: disabled because of invalid user"); | |
a306f2dd | 466 | return 0; |
467 | } | |
468 | have_sig = packet_get_char(); | |
cbc5abf9 | 469 | if (datafellows & SSH_BUG_PKAUTH) { |
470 | debug2("userauth_pubkey: SSH_BUG_PKAUTH"); | |
471 | /* no explicit pkalg given */ | |
472 | pkblob = packet_get_string(&blen); | |
473 | buffer_init(&b); | |
474 | buffer_append(&b, pkblob, blen); | |
475 | /* so we have to extract the pkalg from the pkblob */ | |
476 | pkalg = buffer_get_string(&b, &alen); | |
477 | buffer_free(&b); | |
478 | } else { | |
479 | pkalg = packet_get_string(&alen); | |
480 | pkblob = packet_get_string(&blen); | |
481 | } | |
fa08c86b | 482 | pktype = key_type_from_name(pkalg); |
483 | if (pktype == KEY_UNSPEC) { | |
cbc5abf9 | 484 | /* this is perfectly legal */ |
485 | log("userauth_pubkey: unsupported public key algorithm: %s", pkalg); | |
94ec8c6b | 486 | xfree(pkalg); |
cbc5abf9 | 487 | xfree(pkblob); |
a306f2dd | 488 | return 0; |
489 | } | |
fa08c86b | 490 | key = key_from_blob(pkblob, blen); |
a306f2dd | 491 | if (key != NULL) { |
492 | if (have_sig) { | |
493 | sig = packet_get_string(&slen); | |
494 | packet_done(); | |
495 | buffer_init(&b); | |
33de75a3 | 496 | if (datafellows & SSH_OLD_SESSIONID) { |
74fc9186 | 497 | buffer_append(&b, session_id2, session_id2_len); |
33de75a3 | 498 | } else { |
499 | buffer_put_string(&b, session_id2, session_id2_len); | |
74fc9186 | 500 | } |
38c295d6 | 501 | /* reconstruct packet */ |
a306f2dd | 502 | buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST); |
94ec8c6b | 503 | buffer_put_cstring(&b, authctxt->user); |
38c295d6 | 504 | buffer_put_cstring(&b, |
cbc5abf9 | 505 | datafellows & SSH_BUG_PKSERVICE ? |
38c295d6 | 506 | "ssh-userauth" : |
94ec8c6b | 507 | authctxt->service); |
cbc5abf9 | 508 | if (datafellows & SSH_BUG_PKAUTH) { |
509 | buffer_put_char(&b, have_sig); | |
510 | } else { | |
511 | buffer_put_cstring(&b, "publickey"); | |
512 | buffer_put_char(&b, have_sig); | |
513 | buffer_put_cstring(&b, key_ssh_name(key)); | |
514 | } | |
38c295d6 | 515 | buffer_put_string(&b, pkblob, blen); |
fa08c86b | 516 | #ifdef DEBUG_PK |
a306f2dd | 517 | buffer_dump(&b); |
518 | #endif | |
519 | /* test for correct signature */ | |
fa08c86b | 520 | if (user_key_allowed(authctxt->pw, key) && |
521 | key_verify(key, sig, slen, buffer_ptr(&b), buffer_len(&b)) == 1) | |
a306f2dd | 522 | authenticated = 1; |
523 | buffer_clear(&b); | |
524 | xfree(sig); | |
525 | } else { | |
94ec8c6b | 526 | debug("test whether pkalg/pkblob are acceptable"); |
a306f2dd | 527 | packet_done(); |
94ec8c6b | 528 | |
a306f2dd | 529 | /* XXX fake reply and always send PK_OK ? */ |
1d1ffb87 | 530 | /* |
531 | * XXX this allows testing whether a user is allowed | |
532 | * to login: if you happen to have a valid pubkey this | |
533 | * message is sent. the message is NEVER sent at all | |
534 | * if a user is not allowed to login. is this an | |
535 | * issue? -markus | |
536 | */ | |
fa08c86b | 537 | if (user_key_allowed(authctxt->pw, key)) { |
a306f2dd | 538 | packet_start(SSH2_MSG_USERAUTH_PK_OK); |
539 | packet_put_string(pkalg, alen); | |
540 | packet_put_string(pkblob, blen); | |
541 | packet_send(); | |
542 | packet_write_wait(); | |
543 | authenticated = -1; | |
544 | } | |
545 | } | |
94ec8c6b | 546 | if (authenticated != 1) |
547 | auth_clear_options(); | |
a306f2dd | 548 | key_free(key); |
549 | } | |
fa08c86b | 550 | debug2("userauth_pubkey: authenticated %d pkalg %s", authenticated, pkalg); |
a306f2dd | 551 | xfree(pkalg); |
552 | xfree(pkblob); | |
94ec8c6b | 553 | #ifdef HAVE_CYGWIN |
554 | if (check_nt_auth(0, authctxt->pw->pw_uid) == 0) | |
555 | return(0); | |
556 | #endif | |
a306f2dd | 557 | return authenticated; |
558 | } | |
559 | ||
94ec8c6b | 560 | /* get current user */ |
a306f2dd | 561 | |
562 | struct passwd* | |
563 | auth_get_user(void) | |
564 | { | |
94ec8c6b | 565 | return (x_authctxt != NULL && x_authctxt->valid) ? x_authctxt->pw : NULL; |
a306f2dd | 566 | } |
567 | ||
94ec8c6b | 568 | #define DELIM "," |
569 | ||
570 | char * | |
571 | authmethods_get(void) | |
a306f2dd | 572 | { |
94ec8c6b | 573 | Authmethod *method = NULL; |
1e3b8b07 | 574 | u_int size = 0; |
94ec8c6b | 575 | char *list; |
576 | ||
577 | for (method = authmethods; method->name != NULL; method++) { | |
578 | if (strcmp(method->name, "none") == 0) | |
579 | continue; | |
580 | if (method->enabled != NULL && *(method->enabled) != 0) { | |
581 | if (size != 0) | |
582 | size += strlen(DELIM); | |
583 | size += strlen(method->name); | |
a306f2dd | 584 | } |
94ec8c6b | 585 | } |
586 | size++; /* trailing '\0' */ | |
587 | list = xmalloc(size); | |
588 | list[0] = '\0'; | |
589 | ||
590 | for (method = authmethods; method->name != NULL; method++) { | |
591 | if (strcmp(method->name, "none") == 0) | |
592 | continue; | |
593 | if (method->enabled != NULL && *(method->enabled) != 0) { | |
594 | if (list[0] != '\0') | |
595 | strlcat(list, DELIM, size); | |
596 | strlcat(list, method->name, size); | |
a306f2dd | 597 | } |
598 | } | |
94ec8c6b | 599 | return list; |
600 | } | |
601 | ||
602 | Authmethod * | |
603 | authmethod_lookup(const char *name) | |
604 | { | |
605 | Authmethod *method = NULL; | |
606 | if (name != NULL) | |
607 | for (method = authmethods; method->name != NULL; method++) | |
608 | if (method->enabled != NULL && | |
609 | *(method->enabled) != 0 && | |
610 | strcmp(name, method->name) == 0) | |
611 | return method; | |
612 | debug2("Unrecognized authentication method name: %s", name ? name : "NULL"); | |
613 | return NULL; | |
a306f2dd | 614 | } |
615 | ||
616 | /* return 1 if user allows given key */ | |
617 | int | |
fa08c86b | 618 | user_key_allowed(struct passwd *pw, Key *key) |
a306f2dd | 619 | { |
620 | char line[8192], file[1024]; | |
621 | int found_key = 0; | |
a306f2dd | 622 | FILE *f; |
1e3b8b07 | 623 | u_long linenum = 0; |
a306f2dd | 624 | struct stat st; |
625 | Key *found; | |
626 | ||
94ec8c6b | 627 | if (pw == NULL) |
628 | return 0; | |
629 | ||
a306f2dd | 630 | /* Temporarily use the user's uid. */ |
631 | temporarily_use_uid(pw->pw_uid); | |
632 | ||
633 | /* The authorized keys. */ | |
634 | snprintf(file, sizeof file, "%.500s/%.100s", pw->pw_dir, | |
635 | SSH_USER_PERMITTED_KEYS2); | |
636 | ||
637 | /* Fail quietly if file does not exist */ | |
638 | if (stat(file, &st) < 0) { | |
639 | /* Restore the privileged uid. */ | |
640 | restore_uid(); | |
641 | return 0; | |
642 | } | |
643 | /* Open the file containing the authorized keys. */ | |
644 | f = fopen(file, "r"); | |
645 | if (!f) { | |
646 | /* Restore the privileged uid. */ | |
647 | restore_uid(); | |
648 | return 0; | |
649 | } | |
650 | if (options.strict_modes) { | |
651 | int fail = 0; | |
652 | char buf[1024]; | |
653 | /* Check open file in order to avoid open/stat races */ | |
654 | if (fstat(fileno(f), &st) < 0 || | |
655 | (st.st_uid != 0 && st.st_uid != pw->pw_uid) || | |
656 | (st.st_mode & 022) != 0) { | |
de273eef | 657 | snprintf(buf, sizeof buf, |
658 | "%s authentication refused for %.100s: " | |
659 | "bad ownership or modes for '%s'.", | |
660 | key_type(key), pw->pw_name, file); | |
a306f2dd | 661 | fail = 1; |
662 | } else { | |
663 | /* Check path to SSH_USER_PERMITTED_KEYS */ | |
664 | int i; | |
665 | static const char *check[] = { | |
666 | "", SSH_USER_DIR, NULL | |
667 | }; | |
668 | for (i = 0; check[i]; i++) { | |
669 | snprintf(line, sizeof line, "%.500s/%.100s", | |
670 | pw->pw_dir, check[i]); | |
671 | if (stat(line, &st) < 0 || | |
672 | (st.st_uid != 0 && st.st_uid != pw->pw_uid) || | |
673 | (st.st_mode & 022) != 0) { | |
674 | snprintf(buf, sizeof buf, | |
de273eef | 675 | "%s authentication refused for %.100s: " |
a306f2dd | 676 | "bad ownership or modes for '%s'.", |
de273eef | 677 | key_type(key), pw->pw_name, line); |
a306f2dd | 678 | fail = 1; |
679 | break; | |
680 | } | |
681 | } | |
682 | } | |
683 | if (fail) { | |
a306f2dd | 684 | fclose(f); |
089fbbd2 | 685 | log("%s",buf); |
a306f2dd | 686 | restore_uid(); |
687 | return 0; | |
688 | } | |
689 | } | |
690 | found_key = 0; | |
de273eef | 691 | found = key_new(key->type); |
a306f2dd | 692 | |
693 | while (fgets(line, sizeof(line), f)) { | |
38c295d6 | 694 | char *cp, *options = NULL; |
a306f2dd | 695 | linenum++; |
696 | /* Skip leading whitespace, empty and comment lines. */ | |
697 | for (cp = line; *cp == ' ' || *cp == '\t'; cp++) | |
698 | ; | |
699 | if (!*cp || *cp == '\n' || *cp == '#') | |
700 | continue; | |
38c295d6 | 701 | |
fa08c86b | 702 | if (key_read(found, &cp) == -1) { |
38c295d6 | 703 | /* no key? check if there are options for this key */ |
704 | int quoted = 0; | |
fa08c86b | 705 | debug2("user_key_allowed: check options: '%s'", cp); |
38c295d6 | 706 | options = cp; |
707 | for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) { | |
708 | if (*cp == '\\' && cp[1] == '"') | |
709 | cp++; /* Skip both */ | |
710 | else if (*cp == '"') | |
711 | quoted = !quoted; | |
712 | } | |
713 | /* Skip remaining whitespace. */ | |
714 | for (; *cp == ' ' || *cp == '\t'; cp++) | |
715 | ; | |
fa08c86b | 716 | if (key_read(found, &cp) == -1) { |
717 | debug2("user_key_allowed: advance: '%s'", cp); | |
38c295d6 | 718 | /* still no key? advance to next line*/ |
719 | continue; | |
720 | } | |
721 | } | |
722 | if (key_equal(found, key) && | |
723 | auth_parse_options(pw, options, linenum) == 1) { | |
a306f2dd | 724 | found_key = 1; |
725 | debug("matching key found: file %s, line %ld", | |
726 | file, linenum); | |
727 | break; | |
728 | } | |
729 | } | |
730 | restore_uid(); | |
731 | fclose(f); | |
732 | key_free(found); | |
733 | return found_key; | |
734 | } | |
94ec8c6b | 735 | |
736 | struct passwd * | |
737 | pwcopy(struct passwd *pw) | |
738 | { | |
739 | struct passwd *copy = xmalloc(sizeof(*copy)); | |
740 | memset(copy, 0, sizeof(*copy)); | |
741 | copy->pw_name = xstrdup(pw->pw_name); | |
742 | copy->pw_passwd = xstrdup(pw->pw_passwd); | |
743 | copy->pw_uid = pw->pw_uid; | |
744 | copy->pw_gid = pw->pw_gid; | |
745 | #ifdef HAVE_PW_CLASS_IN_PASSWD | |
746 | copy->pw_class = xstrdup(pw->pw_class); | |
747 | #endif | |
748 | copy->pw_dir = xstrdup(pw->pw_dir); | |
749 | copy->pw_shell = xstrdup(pw->pw_shell); | |
750 | return copy; | |
751 | } |