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