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