]> andersk Git - openssh.git/blame - auth1.c
- djm@cvs.openbsd.org 2006/03/25 13:17:03
[openssh.git] / auth1.c
CommitLineData
c1cb7bae 1/* $OpenBSD: auth1.c,v 1.66 2006/03/25 13:17:01 djm Exp $ */
a306f2dd 2/*
3 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4 * All rights reserved
bcbf86ec 5 *
6 * As far as I am concerned, the code I have written for this software
7 * can be used freely for any purpose. Any derived versions of this
8 * software must be clearly marked as such, and if the derived work is
9 * incompatible with the protocol description in the RFC file, it must be
10 * called by a name other than "ssh" or "Secure Shell".
a306f2dd 11 */
12
13#include "includes.h"
94ec8c6b 14
a306f2dd 15#include "xmalloc.h"
16#include "rsa.h"
42f11eb2 17#include "ssh1.h"
a306f2dd 18#include "packet.h"
19#include "buffer.h"
42f11eb2 20#include "log.h"
a306f2dd 21#include "servconf.h"
22#include "compat.h"
23#include "auth.h"
7899c98f 24#include "channels.h"
a306f2dd 25#include "session.h"
ced49be2 26#include "uidswap.h"
1853d1ef 27#include "monitor_wrap.h"
9aab0af7 28#include "buffer.h"
a306f2dd 29
30/* import */
31extern ServerOptions options;
9aab0af7 32extern Buffer loginmsg;
94ec8c6b 33
8485ce56 34static int auth1_process_password(Authctxt *, char *, size_t);
35static int auth1_process_rsa(Authctxt *, char *, size_t);
36static int auth1_process_rhosts_rsa(Authctxt *, char *, size_t);
37static int auth1_process_tis_challenge(Authctxt *, char *, size_t);
38static int auth1_process_tis_response(Authctxt *, char *, size_t);
39
40static char *client_user = NULL; /* Used to fill in remote user for PAM */
41
42struct AuthMethod1 {
43 int type;
44 char *name;
45 int *enabled;
46 int (*method)(Authctxt *, char *, size_t);
47};
48
49const struct AuthMethod1 auth1_methods[] = {
50 {
51 SSH_CMSG_AUTH_PASSWORD, "password",
52 &options.password_authentication, auth1_process_password
53 },
54 {
55 SSH_CMSG_AUTH_RSA, "rsa",
56 &options.rsa_authentication, auth1_process_rsa
57 },
58 {
59 SSH_CMSG_AUTH_RHOSTS_RSA, "rhosts-rsa",
60 &options.rhosts_rsa_authentication, auth1_process_rhosts_rsa
61 },
62 {
63 SSH_CMSG_AUTH_TIS, "challenge-response",
64 &options.challenge_response_authentication,
65 auth1_process_tis_challenge
66 },
67 {
68 SSH_CMSG_AUTH_TIS_RESPONSE, "challenge-response",
69 &options.challenge_response_authentication,
70 auth1_process_tis_response
71 },
72 { -1, NULL, NULL, NULL}
73};
74
75static const struct AuthMethod1
76*lookup_authmethod1(int type)
77{
78 int i;
79
c8e9c167 80 for (i = 0; auth1_methods[i].name != NULL; i++)
8485ce56 81 if (auth1_methods[i].type == type)
82 return (&(auth1_methods[i]));
83
84 return (NULL);
85}
86
396c147e 87static char *
a306f2dd 88get_authname(int type)
89{
8485ce56 90 const struct AuthMethod1 *a;
91 static char buf[64];
92
93 if ((a = lookup_authmethod1(type)) != NULL)
94 return (a->name);
95 snprintf(buf, sizeof(buf), "bad-auth-msg-%d", type);
96 return (buf);
97}
98
fe8c3af1 99/*ARGSUSED*/
8485ce56 100static int
101auth1_process_password(Authctxt *authctxt, char *info, size_t infolen)
102{
103 int authenticated = 0;
104 char *password;
105 u_int dlen;
106
107 /*
108 * Read user password. It is in plain text, but was
109 * transmitted over the encrypted channel so it is
110 * not visible to an outside observer.
111 */
112 password = packet_get_string(&dlen);
113 packet_check_eom();
114
115 /* Try authentication with the password. */
116 authenticated = PRIVSEP(auth_password(authctxt, password));
117
118 memset(password, 0, dlen);
119 xfree(password);
120
121 return (authenticated);
122}
123
fe8c3af1 124/*ARGSUSED*/
8485ce56 125static int
126auth1_process_rsa(Authctxt *authctxt, char *info, size_t infolen)
127{
128 int authenticated = 0;
129 BIGNUM *n;
130
131 /* RSA authentication requested. */
132 if ((n = BN_new()) == NULL)
133 fatal("do_authloop: BN_new failed");
134 packet_get_bignum(n);
135 packet_check_eom();
136 authenticated = auth_rsa(authctxt, n);
137 BN_clear_free(n);
138
139 return (authenticated);
140}
141
fe8c3af1 142/*ARGSUSED*/
8485ce56 143static int
144auth1_process_rhosts_rsa(Authctxt *authctxt, char *info, size_t infolen)
145{
a375df46 146 int keybits, authenticated = 0;
8485ce56 147 u_int bits;
148 Key *client_host_key;
149 u_int ulen;
150
151 /*
152 * Get client user name. Note that we just have to
153 * trust the client; root on the client machine can
154 * claim to be any user.
155 */
156 client_user = packet_get_string(&ulen);
157
158 /* Get the client host key. */
159 client_host_key = key_new(KEY_RSA1);
160 bits = packet_get_int();
161 packet_get_bignum(client_host_key->rsa->e);
162 packet_get_bignum(client_host_key->rsa->n);
163
a375df46 164 keybits = BN_num_bits(client_host_key->rsa->n);
165 if (keybits < 0 || bits != (u_int)keybits) {
8485ce56 166 verbose("Warning: keysize mismatch for client_host_key: "
167 "actual %d, announced %d",
168 BN_num_bits(client_host_key->rsa->n), bits);
a306f2dd 169 }
8485ce56 170 packet_check_eom();
171
172 authenticated = auth_rhosts_rsa(authctxt, client_user,
173 client_host_key);
174 key_free(client_host_key);
175
176 snprintf(info, infolen, " ruser %.100s", client_user);
d1cf9a87 177
8485ce56 178 return (authenticated);
179}
180
fe8c3af1 181/*ARGSUSED*/
8485ce56 182static int
183auth1_process_tis_challenge(Authctxt *authctxt, char *info, size_t infolen)
184{
185 char *challenge;
56964485 186
8485ce56 187 if ((challenge = get_challenge(authctxt)) == NULL)
188 return (0);
189
190 debug("sending challenge '%s'", challenge);
191 packet_start(SSH_SMSG_AUTH_TIS_CHALLENGE);
192 packet_put_cstring(challenge);
193 xfree(challenge);
194 packet_send();
195 packet_write_wait();
196
197 return (-1);
198}
199
fe8c3af1 200/*ARGSUSED*/
8485ce56 201static int
202auth1_process_tis_response(Authctxt *authctxt, char *info, size_t infolen)
203{
204 int authenticated = 0;
205 char *response;
206 u_int dlen;
207
208 response = packet_get_string(&dlen);
209 packet_check_eom();
210 authenticated = verify_response(authctxt, response);
211 memset(response, 'r', dlen);
212 xfree(response);
213
214 return (authenticated);
a306f2dd 215}
216
217/*
59c97189 218 * read packets, try to authenticate the user and
219 * return only if authentication is successful
a306f2dd 220 */
396c147e 221static void
59c97189 222do_authloop(Authctxt *authctxt)
a306f2dd 223{
94ec8c6b 224 int authenticated = 0;
59c97189 225 char info[1024];
8485ce56 226 int prev = 0, type = 0;
227 const struct AuthMethod1 *meth;
59c97189 228
229 debug("Attempting authentication for %s%.100s.",
d77347cc 230 authctxt->valid ? "" : "invalid user ", authctxt->user);
59c97189 231
232 /* If the user has no password, accept authentication immediately. */
233 if (options.password_authentication &&
1c590258 234#ifdef KRB5
59c97189 235 (!options.kerberos_authentication || options.kerberos_or_local_passwd) &&
236#endif
1853d1ef 237 PRIVSEP(auth_password(authctxt, ""))) {
ece30983 238#ifdef USE_PAM
239 if (options.use_pam && (PRIVSEP(do_pam_account())))
240#endif
241 {
242 auth_log(authctxt, 1, "without authentication", "");
243 return;
244 }
59c97189 245 }
a306f2dd 246
247 /* Indicate that authentication is needed. */
248 packet_start(SSH_SMSG_FAILURE);
249 packet_send();
250 packet_write_wait();
251
59c97189 252 for (;;) {
94ec8c6b 253 /* default to fail */
254 authenticated = 0;
255
59c97189 256 info[0] = '\0';
a306f2dd 257
258 /* Get a packet from the client. */
05114c74 259 prev = type;
54a5250f 260 type = packet_read();
a306f2dd 261
05114c74 262 /*
263 * If we started challenge-response authentication but the
264 * next packet is not a response to our challenge, release
265 * the resources allocated by get_challenge() (which would
266 * normally have been released by verify_response() had we
267 * received such a response)
268 */
269 if (prev == SSH_CMSG_AUTH_TIS &&
270 type != SSH_CMSG_AUTH_TIS_RESPONSE)
271 abandon_challenge_response(authctxt);
272
8485ce56 273 if ((meth = lookup_authmethod1(type)) == NULL) {
274 logit("Unknown message during authentication: "
275 "type %d", type);
276 goto skip;
277 }
278
279 if (!*(meth->enabled)) {
280 verbose("%s authentication disabled.", meth->name);
281 goto skip;
a306f2dd 282 }
8485ce56 283
284 authenticated = meth->method(authctxt, info, sizeof(info));
285 if (authenticated == -1)
286 continue; /* "postponed" */
287
af774732 288#ifdef BSD_AUTH
289 if (authctxt->as) {
290 auth_close(authctxt->as);
291 authctxt->as = NULL;
292 }
293#endif
59c97189 294 if (!authctxt->valid && authenticated)
295 fatal("INTERNAL ERROR: authenticated invalid user %s",
296 authctxt->user);
a306f2dd 297
ef51930f 298#ifdef _UNICOS
ef51930f 299 if (authenticated && cray_access_denied(authctxt->user)) {
300 authenticated = 0;
301 fatal("Access denied for user %s.",authctxt->user);
302 }
303#endif /* _UNICOS */
304
2b87da3b 305#ifdef HAVE_CYGWIN
306 if (authenticated &&
d1cf9a87 307 !check_nt_auth(type == SSH_CMSG_AUTH_PASSWORD,
02a57950 308 authctxt->pw)) {
3c62e7eb 309 packet_disconnect("Authentication rejected for uid %d.",
02a57950 310 authctxt->pw == NULL ? -1 : authctxt->pw->pw_uid);
3c62e7eb 311 authenticated = 0;
312 }
3c418020 313#else
59c97189 314 /* Special handling for root */
5d47f1d4 315 if (authenticated && authctxt->pw->pw_uid == 0 &&
8485ce56 316 !auth_root_allowed(meth->name)) {
317 authenticated = 0;
6039eeef 318# ifdef SSH_AUDIT_EVENTS
319 PRIVSEP(audit_event(SSH_LOGIN_ROOT_DENIED));
c00e4d75 320# endif
321 }
3c418020 322#endif
a306f2dd 323
5b9e2464 324#ifdef USE_PAM
aff51935 325 if (options.use_pam && authenticated &&
9aab0af7 326 !PRIVSEP(do_pam_account())) {
327 char *msg;
328 size_t len;
329
330 error("Access denied for user %s by PAM account "
98c044d0 331 "configuration", authctxt->user);
9aab0af7 332 len = buffer_len(&loginmsg);
333 buffer_append(&loginmsg, "\0", 1);
334 msg = buffer_ptr(&loginmsg);
335 /* strip trailing newlines */
336 if (len > 0)
337 while (len > 0 && msg[--len] == '\n')
338 msg[len] = '\0';
339 else
340 msg = "Access denied.";
341 packet_disconnect(msg);
342 }
5b9e2464 343#endif
344
8485ce56 345 skip:
59c97189 346 /* Log before sending the reply */
347 auth_log(authctxt, authenticated, get_authname(type), info);
348
a306f2dd 349 if (client_user != NULL) {
350 xfree(client_user);
351 client_user = NULL;
352 }
353
94ec8c6b 354 if (authenticated)
355 return;
356
c00e4d75 357 if (authctxt->failures++ > options.max_authtries) {
6039eeef 358#ifdef SSH_AUDIT_EVENTS
359 PRIVSEP(audit_event(SSH_LOGIN_EXCEED_MAXTRIES));
c00e4d75 360#endif
59c97189 361 packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
c00e4d75 362 }
a306f2dd 363
a306f2dd 364 packet_start(SSH_SMSG_FAILURE);
365 packet_send();
366 packet_write_wait();
367 }
368}
369
370/*
371 * Performs authentication of an incoming connection. Session key has already
372 * been exchanged and encryption is enabled.
373 */
2362db19 374void
375do_authentication(Authctxt *authctxt)
a306f2dd 376{
1e3b8b07 377 u_int ulen;
f8d99a32 378 char *user, *style = NULL;
a306f2dd 379
380 /* Get the name of the user that we wish to log in as. */
54a5250f 381 packet_read_expect(SSH_CMSG_USER);
a306f2dd 382
383 /* Get the user name. */
384 user = packet_get_string(&ulen);
95500969 385 packet_check_eom();
a306f2dd 386
59c97189 387 if ((style = strchr(user, ':')) != NULL)
ced49be2 388 *style++ = '\0';
59c97189 389
59c97189 390 authctxt->user = user;
391 authctxt->style = style;
392
a306f2dd 393 /* Verify that the user is a valid user. */
5f1f36b5 394 if ((authctxt->pw = PRIVSEP(getpwnamallow(user))) != NULL)
59c97189 395 authctxt->valid = 1;
96d0bf74 396 else {
d77347cc 397 debug("do_authentication: invalid user %s", user);
96d0bf74 398 authctxt->pw = fakepw();
399 }
a306f2dd 400
bd5c0694 401 setproctitle("%s%s", authctxt->valid ? user : "unknown",
1853d1ef 402 use_privsep ? " [net]" : "");
6172e290 403
a306f2dd 404#ifdef USE_PAM
7fceb20d 405 if (options.use_pam)
529d73ab 406 PRIVSEP(start_pam(authctxt));
a306f2dd 407#endif
408
409 /*
410 * If we are not running as root, the user must have the same uid as
8485ce56 411 * the server.
a306f2dd 412 */
94ec8c6b 413#ifndef HAVE_CYGWIN
5f1f36b5 414 if (!use_privsep && getuid() != 0 && authctxt->pw &&
415 authctxt->pw->pw_uid != getuid())
a306f2dd 416 packet_disconnect("Cannot change user when server not running as root.");
3c62e7eb 417#endif
a306f2dd 418
59c97189 419 /*
b4b5d644 420 * Loop until the user has been authenticated or the connection is
421 * closed, do_authloop() returns only if authentication is successful
422 */
59c97189 423 do_authloop(authctxt);
a306f2dd 424
425 /* The user has been authenticated and accepted. */
94ec8c6b 426 packet_start(SSH_SMSG_SUCCESS);
427 packet_send();
428 packet_write_wait();
a306f2dd 429}
This page took 0.246461 seconds and 5 git commands to generate.