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