]>
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" |
6172e290 | 26 | RCSID("$OpenBSD: auth2.c,v 1.42 2001/02/13 22:49:40 markus Exp $"); |
94ec8c6b | 27 | |
a306f2dd | 28 | #include <openssl/evp.h> |
29 | ||
42f11eb2 | 30 | #include "ssh2.h" |
a306f2dd | 31 | #include "xmalloc.h" |
32 | #include "rsa.h" | |
a306f2dd | 33 | #include "pty.h" |
34 | #include "packet.h" | |
35 | #include "buffer.h" | |
42f11eb2 | 36 | #include "log.h" |
a306f2dd | 37 | #include "servconf.h" |
38 | #include "compat.h" | |
39 | #include "channels.h" | |
40 | #include "bufaux.h" | |
a306f2dd | 41 | #include "auth.h" |
42 | #include "session.h" | |
43 | #include "dispatch.h" | |
a306f2dd | 44 | #include "key.h" |
b0e305c9 | 45 | #include "cipher.h" |
a306f2dd | 46 | #include "kex.h" |
42f11eb2 | 47 | #include "pathnames.h" |
a306f2dd | 48 | #include "uidswap.h" |
38c295d6 | 49 | #include "auth-options.h" |
a306f2dd | 50 | |
51 | /* import */ | |
52 | extern ServerOptions options; | |
1e3b8b07 | 53 | extern u_char *session_id2; |
a306f2dd | 54 | extern int session_id2_len; |
55 | ||
94ec8c6b | 56 | #ifdef WITH_AIXAUTHENTICATE |
57 | extern char *aixloginmsg; | |
58 | #endif | |
94ec8c6b | 59 | |
60 | static Authctxt *x_authctxt = NULL; | |
61 | static int one = 1; | |
62 | ||
63 | typedef struct Authmethod Authmethod; | |
64 | struct Authmethod { | |
65 | char *name; | |
66 | int (*userauth)(Authctxt *authctxt); | |
67 | int *enabled; | |
68 | }; | |
69 | ||
a306f2dd | 70 | /* protocol */ |
71 | ||
188adeb2 | 72 | void input_service_request(int type, int plen, void *ctxt); |
73 | void input_userauth_request(int type, int plen, void *ctxt); | |
74 | void protocol_error(int type, int plen, void *ctxt); | |
a306f2dd | 75 | |
a306f2dd | 76 | /* helper */ |
94ec8c6b | 77 | Authmethod *authmethod_lookup(const char *name); |
78 | struct passwd *pwcopy(struct passwd *pw); | |
fa08c86b | 79 | int user_key_allowed(struct passwd *pw, Key *key); |
94ec8c6b | 80 | char *authmethods_get(void); |
a306f2dd | 81 | |
94ec8c6b | 82 | /* auth */ |
eea39c02 | 83 | void userauth_banner(void); |
94ec8c6b | 84 | int userauth_none(Authctxt *authctxt); |
85 | int userauth_passwd(Authctxt *authctxt); | |
86 | int userauth_pubkey(Authctxt *authctxt); | |
87 | int userauth_kbdint(Authctxt *authctxt); | |
88 | ||
89 | Authmethod authmethods[] = { | |
90 | {"none", | |
91 | userauth_none, | |
92 | &one}, | |
93 | {"publickey", | |
94 | userauth_pubkey, | |
fa08c86b | 95 | &options.pubkey_authentication}, |
94ec8c6b | 96 | {"password", |
97 | userauth_passwd, | |
98 | &options.password_authentication}, | |
c845316f | 99 | {"keyboard-interactive", |
100 | userauth_kbdint, | |
101 | &options.kbd_interactive_authentication}, | |
94ec8c6b | 102 | {NULL, NULL, NULL} |
a306f2dd | 103 | }; |
a306f2dd | 104 | |
105 | /* | |
94ec8c6b | 106 | * loop until authctxt->success == TRUE |
a306f2dd | 107 | */ |
108 | ||
109 | void | |
110 | do_authentication2() | |
111 | { | |
59c97189 | 112 | Authctxt *authctxt = authctxt_new(); |
113 | ||
94ec8c6b | 114 | x_authctxt = authctxt; /*XXX*/ |
115 | ||
d464095c | 116 | /* challenge-reponse is implemented via keyboard interactive */ |
117 | if (options.challenge_reponse_authentication) | |
118 | options.kbd_interactive_authentication = 1; | |
119 | ||
a306f2dd | 120 | dispatch_init(&protocol_error); |
121 | dispatch_set(SSH2_MSG_SERVICE_REQUEST, &input_service_request); | |
94ec8c6b | 122 | dispatch_run(DISPATCH_BLOCK, &authctxt->success, authctxt); |
59c97189 | 123 | do_authenticated2(authctxt); |
a306f2dd | 124 | } |
125 | ||
126 | void | |
188adeb2 | 127 | protocol_error(int type, int plen, void *ctxt) |
a306f2dd | 128 | { |
129 | log("auth: protocol error: type %d plen %d", type, plen); | |
130 | packet_start(SSH2_MSG_UNIMPLEMENTED); | |
131 | packet_put_int(0); | |
132 | packet_send(); | |
133 | packet_write_wait(); | |
134 | } | |
135 | ||
136 | void | |
188adeb2 | 137 | input_service_request(int type, int plen, void *ctxt) |
a306f2dd | 138 | { |
94ec8c6b | 139 | Authctxt *authctxt = ctxt; |
1e3b8b07 | 140 | u_int len; |
a306f2dd | 141 | int accept = 0; |
142 | char *service = packet_get_string(&len); | |
143 | packet_done(); | |
144 | ||
94ec8c6b | 145 | if (authctxt == NULL) |
146 | fatal("input_service_request: no authctxt"); | |
147 | ||
a306f2dd | 148 | if (strcmp(service, "ssh-userauth") == 0) { |
94ec8c6b | 149 | if (!authctxt->success) { |
a306f2dd | 150 | accept = 1; |
151 | /* now we can handle user-auth requests */ | |
152 | dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &input_userauth_request); | |
153 | } | |
154 | } | |
155 | /* XXX all other service requests are denied */ | |
156 | ||
157 | if (accept) { | |
158 | packet_start(SSH2_MSG_SERVICE_ACCEPT); | |
159 | packet_put_cstring(service); | |
160 | packet_send(); | |
161 | packet_write_wait(); | |
162 | } else { | |
163 | debug("bad service request %s", service); | |
164 | packet_disconnect("bad service request %s", service); | |
165 | } | |
166 | xfree(service); | |
167 | } | |
168 | ||
169 | void | |
188adeb2 | 170 | input_userauth_request(int type, int plen, void *ctxt) |
a306f2dd | 171 | { |
94ec8c6b | 172 | Authctxt *authctxt = ctxt; |
173 | Authmethod *m = NULL; | |
59c97189 | 174 | char *user, *service, *method, *style = NULL; |
a306f2dd | 175 | int authenticated = 0; |
a306f2dd | 176 | |
94ec8c6b | 177 | if (authctxt == NULL) |
178 | fatal("input_userauth_request: no authctxt"); | |
a306f2dd | 179 | |
94ec8c6b | 180 | user = packet_get_string(NULL); |
181 | service = packet_get_string(NULL); | |
182 | method = packet_get_string(NULL); | |
183 | debug("userauth-request for user %s service %s method %s", user, service, method); | |
8abcdba4 | 184 | debug("attempt %d failures %d", authctxt->attempt, authctxt->failures); |
94ec8c6b | 185 | |
59c97189 | 186 | if ((style = strchr(user, ':')) != NULL) |
187 | *style++ = 0; | |
188 | ||
2b87da3b | 189 | if (authctxt->attempt++ == 0) { |
94ec8c6b | 190 | /* setup auth context */ |
191 | struct passwd *pw = NULL; | |
94ec8c6b | 192 | pw = getpwnam(user); |
193 | if (pw && allowed_user(pw) && strcmp(service, "ssh-connection")==0) { | |
194 | authctxt->pw = pwcopy(pw); | |
195 | authctxt->valid = 1; | |
196 | debug2("input_userauth_request: setting up authctxt for %s", user); | |
197 | #ifdef USE_PAM | |
04fc7a67 | 198 | start_pam(pw->pw_name); |
94ec8c6b | 199 | #endif |
200 | } else { | |
201 | log("input_userauth_request: illegal user %s", user); | |
04fc7a67 | 202 | #ifdef USE_PAM |
203 | start_pam("NOUSER"); | |
204 | #endif | |
94ec8c6b | 205 | } |
6172e290 | 206 | setproctitle("%s", pw ? user : "unknown"); |
94ec8c6b | 207 | authctxt->user = xstrdup(user); |
208 | authctxt->service = xstrdup(service); | |
59c97189 | 209 | authctxt->style = style ? xstrdup(style) : NULL; /* currently unused */ |
94ec8c6b | 210 | } else if (authctxt->valid) { |
211 | if (strcmp(user, authctxt->user) != 0 || | |
212 | strcmp(service, authctxt->service) != 0) { | |
213 | log("input_userauth_request: missmatch: (%s,%s)!=(%s,%s)", | |
214 | user, service, authctxt->user, authctxt->service); | |
215 | authctxt->valid = 0; | |
a306f2dd | 216 | } |
217 | } | |
59c97189 | 218 | /* reset state */ |
219 | dispatch_set(SSH2_MSG_USERAUTH_INFO_RESPONSE, &protocol_error); | |
220 | authctxt->postponed = 0; | |
9cd45ea4 | 221 | |
59c97189 | 222 | /* try to authenticate user */ |
94ec8c6b | 223 | m = authmethod_lookup(method); |
224 | if (m != NULL) { | |
225 | debug2("input_userauth_request: try method %s", method); | |
226 | authenticated = m->userauth(authctxt); | |
9cd45ea4 | 227 | } |
59c97189 | 228 | if (!authctxt->valid && authenticated) |
229 | fatal("INTERNAL ERROR: authenticated invalid user %s", | |
230 | authctxt->user); | |
9cd45ea4 | 231 | |
94ec8c6b | 232 | /* Special handling for root */ |
15853e93 | 233 | if (authenticated && authctxt->pw->pw_uid == 0 && |
234 | !auth_root_allowed(method)) | |
a306f2dd | 235 | authenticated = 0; |
a306f2dd | 236 | |
237 | #ifdef USE_PAM | |
edef62bf | 238 | if (authenticated && authctxt->user && !do_pam_account(authctxt->user, |
239 | NULL)) | |
9cd45ea4 | 240 | authenticated = 0; |
a306f2dd | 241 | #endif /* USE_PAM */ |
242 | ||
94ec8c6b | 243 | /* Log before sending the reply */ |
59c97189 | 244 | auth_log(authctxt, authenticated, method, " ssh2"); |
245 | ||
246 | if (!authctxt->postponed) | |
247 | userauth_reply(authctxt, authenticated); | |
94ec8c6b | 248 | |
249 | xfree(service); | |
250 | xfree(user); | |
251 | xfree(method); | |
252 | } | |
253 | ||
eea39c02 | 254 | void |
255 | userauth_banner(void) | |
256 | { | |
257 | struct stat st; | |
258 | char *banner = NULL; | |
259 | off_t len, n; | |
260 | int fd; | |
261 | ||
262 | if (options.banner == NULL || (datafellows & SSH_BUG_BANNER)) | |
263 | return; | |
264 | if ((fd = open(options.banner, O_RDONLY)) < 0) { | |
265 | error("userauth_banner: open %s failed: %s", | |
266 | options.banner, strerror(errno)); | |
267 | return; | |
268 | } | |
269 | if (fstat(fd, &st) < 0) | |
270 | goto done; | |
271 | len = st.st_size; | |
272 | banner = xmalloc(len + 1); | |
273 | if ((n = read(fd, banner, len)) < 0) | |
274 | goto done; | |
275 | banner[n] = '\0'; | |
276 | packet_start(SSH2_MSG_USERAUTH_BANNER); | |
277 | packet_put_cstring(banner); | |
278 | packet_put_cstring(""); /* language, unused */ | |
279 | packet_send(); | |
280 | debug("userauth_banner: sent"); | |
281 | done: | |
282 | if (banner) | |
283 | xfree(banner); | |
284 | close(fd); | |
285 | return; | |
286 | } | |
94ec8c6b | 287 | |
2b87da3b | 288 | void |
94ec8c6b | 289 | userauth_reply(Authctxt *authctxt, int authenticated) |
290 | { | |
8abcdba4 | 291 | char *methods; |
59c97189 | 292 | |
1d1ffb87 | 293 | /* XXX todo: check if multiple auth methods are needed */ |
25f4c264 | 294 | if (authenticated == 1) { |
c1ef8333 | 295 | #ifdef WITH_AIXAUTHENTICATE |
296 | /* We don't have a pty yet, so just label the line as "ssh" */ | |
2b87da3b | 297 | if (loginsuccess(authctxt->user?authctxt->user:"NOUSER", |
298 | get_canonical_hostname(options.reverse_mapping_check), | |
61e96248 | 299 | "ssh", &aixloginmsg) < 0) |
c1ef8333 | 300 | aixloginmsg = NULL; |
301 | #endif /* WITH_AIXAUTHENTICATE */ | |
a306f2dd | 302 | /* turn off userauth */ |
303 | dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &protocol_error); | |
304 | packet_start(SSH2_MSG_USERAUTH_SUCCESS); | |
305 | packet_send(); | |
306 | packet_write_wait(); | |
307 | /* now we can break out */ | |
94ec8c6b | 308 | authctxt->success = 1; |
59c97189 | 309 | } else { |
310 | if (authctxt->failures++ > AUTH_FAIL_MAX) | |
2b87da3b | 311 | packet_disconnect(AUTH_FAIL_MSG, authctxt->user); |
8abcdba4 | 312 | methods = authmethods_get(); |
a306f2dd | 313 | packet_start(SSH2_MSG_USERAUTH_FAILURE); |
94ec8c6b | 314 | packet_put_cstring(methods); |
315 | packet_put_char(0); /* XXX partial success, unused */ | |
a306f2dd | 316 | packet_send(); |
317 | packet_write_wait(); | |
94ec8c6b | 318 | xfree(methods); |
a306f2dd | 319 | } |
a306f2dd | 320 | } |
321 | ||
322 | int | |
94ec8c6b | 323 | userauth_none(Authctxt *authctxt) |
a306f2dd | 324 | { |
94ec8c6b | 325 | /* disable method "none", only allowed one time */ |
326 | Authmethod *m = authmethod_lookup("none"); | |
327 | if (m != NULL) | |
328 | m->enabled = NULL; | |
a306f2dd | 329 | packet_done(); |
2b87da3b | 330 | userauth_banner(); |
4d33e531 | 331 | |
94ec8c6b | 332 | if (authctxt->valid == 0) |
333 | return(0); | |
2b87da3b | 334 | |
94ec8c6b | 335 | #ifdef HAVE_CYGWIN |
336 | if (check_nt_auth(1, authctxt->pw->pw_uid) == 0) | |
337 | return(0); | |
338 | #endif | |
a306f2dd | 339 | #ifdef USE_PAM |
94ec8c6b | 340 | return auth_pam_password(authctxt->pw, ""); |
4d33e531 | 341 | #elif defined(HAVE_OSF_SIA) |
b7ccb051 | 342 | return 0; |
4d33e531 | 343 | #else /* !HAVE_OSF_SIA && !USE_PAM */ |
94ec8c6b | 344 | return auth_password(authctxt->pw, ""); |
a306f2dd | 345 | #endif /* USE_PAM */ |
346 | } | |
94ec8c6b | 347 | |
a306f2dd | 348 | int |
94ec8c6b | 349 | userauth_passwd(Authctxt *authctxt) |
a306f2dd | 350 | { |
351 | char *password; | |
352 | int authenticated = 0; | |
353 | int change; | |
1e3b8b07 | 354 | u_int len; |
a306f2dd | 355 | change = packet_get_char(); |
356 | if (change) | |
357 | log("password change not supported"); | |
358 | password = packet_get_string(&len); | |
359 | packet_done(); | |
94ec8c6b | 360 | if (authctxt->valid && |
361 | #ifdef HAVE_CYGWIN | |
362 | check_nt_auth(1, authctxt->pw->pw_uid) && | |
363 | #endif | |
a306f2dd | 364 | #ifdef USE_PAM |
94ec8c6b | 365 | auth_pam_password(authctxt->pw, password) == 1) |
4d33e531 | 366 | #elif defined(HAVE_OSF_SIA) |
b7ccb051 | 367 | auth_sia_password(authctxt->user, password) == 1) |
4d33e531 | 368 | #else /* !USE_PAM && !HAVE_OSF_SIA */ |
94ec8c6b | 369 | auth_password(authctxt->pw, password) == 1) |
a306f2dd | 370 | #endif /* USE_PAM */ |
371 | authenticated = 1; | |
372 | memset(password, 0, len); | |
373 | xfree(password); | |
374 | return authenticated; | |
375 | } | |
94ec8c6b | 376 | |
377 | int | |
378 | userauth_kbdint(Authctxt *authctxt) | |
379 | { | |
380 | int authenticated = 0; | |
381 | char *lang = NULL; | |
382 | char *devs = NULL; | |
383 | ||
384 | lang = packet_get_string(NULL); | |
385 | devs = packet_get_string(NULL); | |
386 | packet_done(); | |
387 | ||
388 | debug("keyboard-interactive language %s devs %s", lang, devs); | |
59c97189 | 389 | |
d464095c | 390 | if (options.challenge_reponse_authentication) |
391 | authenticated = auth2_challenge(authctxt, devs); | |
59c97189 | 392 | |
8c9fe09e | 393 | #ifdef USE_PAM |
394 | if (authenticated == 0) | |
395 | authenticated = auth2_pam(authctxt); | |
94ec8c6b | 396 | #endif |
397 | xfree(lang); | |
398 | xfree(devs); | |
399 | #ifdef HAVE_CYGWIN | |
400 | if (check_nt_auth(0, authctxt->pw->pw_uid) == 0) | |
401 | return(0); | |
402 | #endif | |
403 | return authenticated; | |
404 | } | |
405 | ||
a306f2dd | 406 | int |
94ec8c6b | 407 | userauth_pubkey(Authctxt *authctxt) |
a306f2dd | 408 | { |
409 | Buffer b; | |
410 | Key *key; | |
411 | char *pkalg, *pkblob, *sig; | |
1e3b8b07 | 412 | u_int alen, blen, slen; |
fa08c86b | 413 | int have_sig, pktype; |
a306f2dd | 414 | int authenticated = 0; |
415 | ||
94ec8c6b | 416 | if (!authctxt->valid) { |
417 | debug2("userauth_pubkey: disabled because of invalid user"); | |
a306f2dd | 418 | return 0; |
419 | } | |
420 | have_sig = packet_get_char(); | |
cbc5abf9 | 421 | if (datafellows & SSH_BUG_PKAUTH) { |
422 | debug2("userauth_pubkey: SSH_BUG_PKAUTH"); | |
423 | /* no explicit pkalg given */ | |
424 | pkblob = packet_get_string(&blen); | |
425 | buffer_init(&b); | |
426 | buffer_append(&b, pkblob, blen); | |
427 | /* so we have to extract the pkalg from the pkblob */ | |
428 | pkalg = buffer_get_string(&b, &alen); | |
429 | buffer_free(&b); | |
430 | } else { | |
431 | pkalg = packet_get_string(&alen); | |
432 | pkblob = packet_get_string(&blen); | |
433 | } | |
fa08c86b | 434 | pktype = key_type_from_name(pkalg); |
435 | if (pktype == KEY_UNSPEC) { | |
cbc5abf9 | 436 | /* this is perfectly legal */ |
437 | log("userauth_pubkey: unsupported public key algorithm: %s", pkalg); | |
94ec8c6b | 438 | xfree(pkalg); |
cbc5abf9 | 439 | xfree(pkblob); |
a306f2dd | 440 | return 0; |
441 | } | |
fa08c86b | 442 | key = key_from_blob(pkblob, blen); |
a306f2dd | 443 | if (key != NULL) { |
444 | if (have_sig) { | |
445 | sig = packet_get_string(&slen); | |
446 | packet_done(); | |
447 | buffer_init(&b); | |
33de75a3 | 448 | if (datafellows & SSH_OLD_SESSIONID) { |
74fc9186 | 449 | buffer_append(&b, session_id2, session_id2_len); |
33de75a3 | 450 | } else { |
451 | buffer_put_string(&b, session_id2, session_id2_len); | |
74fc9186 | 452 | } |
38c295d6 | 453 | /* reconstruct packet */ |
a306f2dd | 454 | buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST); |
94ec8c6b | 455 | buffer_put_cstring(&b, authctxt->user); |
38c295d6 | 456 | buffer_put_cstring(&b, |
cbc5abf9 | 457 | datafellows & SSH_BUG_PKSERVICE ? |
38c295d6 | 458 | "ssh-userauth" : |
94ec8c6b | 459 | authctxt->service); |
cbc5abf9 | 460 | if (datafellows & SSH_BUG_PKAUTH) { |
461 | buffer_put_char(&b, have_sig); | |
462 | } else { | |
463 | buffer_put_cstring(&b, "publickey"); | |
464 | buffer_put_char(&b, have_sig); | |
465 | buffer_put_cstring(&b, key_ssh_name(key)); | |
466 | } | |
38c295d6 | 467 | buffer_put_string(&b, pkblob, blen); |
fa08c86b | 468 | #ifdef DEBUG_PK |
a306f2dd | 469 | buffer_dump(&b); |
470 | #endif | |
471 | /* test for correct signature */ | |
fa08c86b | 472 | if (user_key_allowed(authctxt->pw, key) && |
473 | key_verify(key, sig, slen, buffer_ptr(&b), buffer_len(&b)) == 1) | |
a306f2dd | 474 | authenticated = 1; |
475 | buffer_clear(&b); | |
476 | xfree(sig); | |
477 | } else { | |
94ec8c6b | 478 | debug("test whether pkalg/pkblob are acceptable"); |
a306f2dd | 479 | packet_done(); |
94ec8c6b | 480 | |
a306f2dd | 481 | /* XXX fake reply and always send PK_OK ? */ |
1d1ffb87 | 482 | /* |
483 | * XXX this allows testing whether a user is allowed | |
484 | * to login: if you happen to have a valid pubkey this | |
485 | * message is sent. the message is NEVER sent at all | |
486 | * if a user is not allowed to login. is this an | |
487 | * issue? -markus | |
488 | */ | |
fa08c86b | 489 | if (user_key_allowed(authctxt->pw, key)) { |
a306f2dd | 490 | packet_start(SSH2_MSG_USERAUTH_PK_OK); |
491 | packet_put_string(pkalg, alen); | |
492 | packet_put_string(pkblob, blen); | |
493 | packet_send(); | |
494 | packet_write_wait(); | |
25f4c264 | 495 | authctxt->postponed = 1; |
a306f2dd | 496 | } |
497 | } | |
94ec8c6b | 498 | if (authenticated != 1) |
499 | auth_clear_options(); | |
a306f2dd | 500 | key_free(key); |
501 | } | |
fa08c86b | 502 | debug2("userauth_pubkey: authenticated %d pkalg %s", authenticated, pkalg); |
a306f2dd | 503 | xfree(pkalg); |
504 | xfree(pkblob); | |
94ec8c6b | 505 | #ifdef HAVE_CYGWIN |
506 | if (check_nt_auth(0, authctxt->pw->pw_uid) == 0) | |
507 | return(0); | |
508 | #endif | |
a306f2dd | 509 | return authenticated; |
510 | } | |
511 | ||
94ec8c6b | 512 | /* get current user */ |
a306f2dd | 513 | |
514 | struct passwd* | |
515 | auth_get_user(void) | |
516 | { | |
94ec8c6b | 517 | return (x_authctxt != NULL && x_authctxt->valid) ? x_authctxt->pw : NULL; |
a306f2dd | 518 | } |
519 | ||
94ec8c6b | 520 | #define DELIM "," |
521 | ||
522 | char * | |
523 | authmethods_get(void) | |
a306f2dd | 524 | { |
94ec8c6b | 525 | Authmethod *method = NULL; |
1e3b8b07 | 526 | u_int size = 0; |
94ec8c6b | 527 | char *list; |
528 | ||
529 | for (method = authmethods; method->name != NULL; method++) { | |
530 | if (strcmp(method->name, "none") == 0) | |
531 | continue; | |
532 | if (method->enabled != NULL && *(method->enabled) != 0) { | |
533 | if (size != 0) | |
534 | size += strlen(DELIM); | |
535 | size += strlen(method->name); | |
a306f2dd | 536 | } |
94ec8c6b | 537 | } |
538 | size++; /* trailing '\0' */ | |
539 | list = xmalloc(size); | |
540 | list[0] = '\0'; | |
541 | ||
542 | for (method = authmethods; method->name != NULL; method++) { | |
543 | if (strcmp(method->name, "none") == 0) | |
544 | continue; | |
545 | if (method->enabled != NULL && *(method->enabled) != 0) { | |
546 | if (list[0] != '\0') | |
547 | strlcat(list, DELIM, size); | |
548 | strlcat(list, method->name, size); | |
a306f2dd | 549 | } |
550 | } | |
94ec8c6b | 551 | return list; |
552 | } | |
553 | ||
554 | Authmethod * | |
555 | authmethod_lookup(const char *name) | |
556 | { | |
557 | Authmethod *method = NULL; | |
558 | if (name != NULL) | |
559 | for (method = authmethods; method->name != NULL; method++) | |
560 | if (method->enabled != NULL && | |
561 | *(method->enabled) != 0 && | |
562 | strcmp(name, method->name) == 0) | |
563 | return method; | |
564 | debug2("Unrecognized authentication method name: %s", name ? name : "NULL"); | |
565 | return NULL; | |
a306f2dd | 566 | } |
567 | ||
568 | /* return 1 if user allows given key */ | |
569 | int | |
fa08c86b | 570 | user_key_allowed(struct passwd *pw, Key *key) |
a306f2dd | 571 | { |
42f11eb2 | 572 | char line[8192], file[MAXPATHLEN]; |
a306f2dd | 573 | int found_key = 0; |
a306f2dd | 574 | FILE *f; |
1e3b8b07 | 575 | u_long linenum = 0; |
a306f2dd | 576 | struct stat st; |
577 | Key *found; | |
578 | ||
94ec8c6b | 579 | if (pw == NULL) |
580 | return 0; | |
581 | ||
a306f2dd | 582 | /* Temporarily use the user's uid. */ |
583 | temporarily_use_uid(pw->pw_uid); | |
584 | ||
585 | /* The authorized keys. */ | |
586 | snprintf(file, sizeof file, "%.500s/%.100s", pw->pw_dir, | |
42f11eb2 | 587 | _PATH_SSH_USER_PERMITTED_KEYS2); |
a306f2dd | 588 | |
589 | /* Fail quietly if file does not exist */ | |
590 | if (stat(file, &st) < 0) { | |
591 | /* Restore the privileged uid. */ | |
592 | restore_uid(); | |
593 | return 0; | |
594 | } | |
595 | /* Open the file containing the authorized keys. */ | |
596 | f = fopen(file, "r"); | |
597 | if (!f) { | |
598 | /* Restore the privileged uid. */ | |
599 | restore_uid(); | |
600 | return 0; | |
601 | } | |
602 | if (options.strict_modes) { | |
603 | int fail = 0; | |
604 | char buf[1024]; | |
605 | /* Check open file in order to avoid open/stat races */ | |
606 | if (fstat(fileno(f), &st) < 0 || | |
607 | (st.st_uid != 0 && st.st_uid != pw->pw_uid) || | |
608 | (st.st_mode & 022) != 0) { | |
de273eef | 609 | snprintf(buf, sizeof buf, |
610 | "%s authentication refused for %.100s: " | |
611 | "bad ownership or modes for '%s'.", | |
612 | key_type(key), pw->pw_name, file); | |
a306f2dd | 613 | fail = 1; |
614 | } else { | |
42f11eb2 | 615 | /* Check path to _PATH_SSH_USER_PERMITTED_KEYS */ |
a306f2dd | 616 | int i; |
617 | static const char *check[] = { | |
42f11eb2 | 618 | "", _PATH_SSH_USER_DIR, NULL |
a306f2dd | 619 | }; |
620 | for (i = 0; check[i]; i++) { | |
621 | snprintf(line, sizeof line, "%.500s/%.100s", | |
622 | pw->pw_dir, check[i]); | |
623 | if (stat(line, &st) < 0 || | |
624 | (st.st_uid != 0 && st.st_uid != pw->pw_uid) || | |
625 | (st.st_mode & 022) != 0) { | |
626 | snprintf(buf, sizeof buf, | |
de273eef | 627 | "%s authentication refused for %.100s: " |
a306f2dd | 628 | "bad ownership or modes for '%s'.", |
de273eef | 629 | key_type(key), pw->pw_name, line); |
a306f2dd | 630 | fail = 1; |
631 | break; | |
632 | } | |
633 | } | |
634 | } | |
635 | if (fail) { | |
a306f2dd | 636 | fclose(f); |
089fbbd2 | 637 | log("%s",buf); |
a306f2dd | 638 | restore_uid(); |
639 | return 0; | |
640 | } | |
641 | } | |
642 | found_key = 0; | |
de273eef | 643 | found = key_new(key->type); |
a306f2dd | 644 | |
645 | while (fgets(line, sizeof(line), f)) { | |
38c295d6 | 646 | char *cp, *options = NULL; |
a306f2dd | 647 | linenum++; |
648 | /* Skip leading whitespace, empty and comment lines. */ | |
649 | for (cp = line; *cp == ' ' || *cp == '\t'; cp++) | |
650 | ; | |
651 | if (!*cp || *cp == '\n' || *cp == '#') | |
652 | continue; | |
38c295d6 | 653 | |
fa08c86b | 654 | if (key_read(found, &cp) == -1) { |
38c295d6 | 655 | /* no key? check if there are options for this key */ |
656 | int quoted = 0; | |
fa08c86b | 657 | debug2("user_key_allowed: check options: '%s'", cp); |
38c295d6 | 658 | options = cp; |
659 | for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) { | |
660 | if (*cp == '\\' && cp[1] == '"') | |
661 | cp++; /* Skip both */ | |
662 | else if (*cp == '"') | |
663 | quoted = !quoted; | |
664 | } | |
665 | /* Skip remaining whitespace. */ | |
666 | for (; *cp == ' ' || *cp == '\t'; cp++) | |
667 | ; | |
fa08c86b | 668 | if (key_read(found, &cp) == -1) { |
669 | debug2("user_key_allowed: advance: '%s'", cp); | |
38c295d6 | 670 | /* still no key? advance to next line*/ |
671 | continue; | |
672 | } | |
673 | } | |
674 | if (key_equal(found, key) && | |
42f11eb2 | 675 | auth_parse_options(pw, options, file, linenum) == 1) { |
a306f2dd | 676 | found_key = 1; |
677 | debug("matching key found: file %s, line %ld", | |
678 | file, linenum); | |
679 | break; | |
680 | } | |
681 | } | |
682 | restore_uid(); | |
683 | fclose(f); | |
684 | key_free(found); | |
685 | return found_key; | |
686 | } |