]> andersk Git - openssh.git/blame - auth1.c
- provos@cvs.openbsd.org 2002/03/18 17:50:31
[openssh.git] / auth1.c
CommitLineData
a306f2dd 1/*
2 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
3 * All rights reserved
bcbf86ec 4 *
5 * As far as I am concerned, the code I have written for this software
6 * can be used freely for any purpose. Any derived versions of this
7 * software must be clearly marked as such, and if the derived work is
8 * incompatible with the protocol description in the RFC file, it must be
9 * called by a name other than "ssh" or "Secure Shell".
a306f2dd 10 */
11
12#include "includes.h"
1853d1ef 13RCSID("$OpenBSD: auth1.c,v 1.38 2002/03/18 17:50:31 provos Exp $");
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"
a306f2dd 20#include "mpaux.h"
42f11eb2 21#include "log.h"
a306f2dd 22#include "servconf.h"
23#include "compat.h"
24#include "auth.h"
7899c98f 25#include "channels.h"
a306f2dd 26#include "session.h"
3b1a83df 27#include "misc.h"
ced49be2 28#include "uidswap.h"
1853d1ef 29#include "monitor_wrap.h"
a306f2dd 30
31/* import */
32extern ServerOptions options;
94ec8c6b 33
a306f2dd 34/*
35 * convert ssh auth msg type into description
36 */
396c147e 37static char *
a306f2dd 38get_authname(int type)
39{
40 static char buf[1024];
41 switch (type) {
42 case SSH_CMSG_AUTH_PASSWORD:
43 return "password";
44 case SSH_CMSG_AUTH_RSA:
45 return "rsa";
46 case SSH_CMSG_AUTH_RHOSTS_RSA:
47 return "rhosts-rsa";
48 case SSH_CMSG_AUTH_RHOSTS:
49 return "rhosts";
59c97189 50 case SSH_CMSG_AUTH_TIS:
51 case SSH_CMSG_AUTH_TIS_RESPONSE:
52 return "challenge-response";
ced49be2 53#if defined(KRB4) || defined(KRB5)
a306f2dd 54 case SSH_CMSG_AUTH_KERBEROS:
55 return "kerberos";
a306f2dd 56#endif
57 }
58 snprintf(buf, sizeof buf, "bad-auth-msg-%d", type);
59 return buf;
60}
61
62/*
59c97189 63 * read packets, try to authenticate the user and
64 * return only if authentication is successful
a306f2dd 65 */
396c147e 66static void
59c97189 67do_authloop(Authctxt *authctxt)
a306f2dd 68{
94ec8c6b 69 int authenticated = 0;
1e3b8b07 70 u_int bits;
b775c6f2 71 Key *client_host_key;
a306f2dd 72 BIGNUM *n;
94ec8c6b 73 char *client_user, *password;
59c97189 74 char info[1024];
1e3b8b07 75 u_int dlen;
1e3b8b07 76 u_int ulen;
a306f2dd 77 int type = 0;
59c97189 78 struct passwd *pw = authctxt->pw;
79
80 debug("Attempting authentication for %s%.100s.",
184eed6a 81 authctxt->valid ? "" : "illegal user ", authctxt->user);
59c97189 82
83 /* If the user has no password, accept authentication immediately. */
84 if (options.password_authentication &&
ced49be2 85#if defined(KRB4) || defined(KRB5)
59c97189 86 (!options.kerberos_authentication || options.kerberos_or_local_passwd) &&
87#endif
f4ebf0e8 88#ifdef USE_PAM
8d60e965 89 auth_pam_password(pw, "")) {
b7ccb051 90#elif defined(HAVE_OSF_SIA)
91 0) {
59c97189 92#else
1853d1ef 93 PRIVSEP(auth_password(authctxt, ""))) {
59c97189 94#endif
95 auth_log(authctxt, 1, "without authentication", "");
96 return;
97 }
a306f2dd 98
99 /* Indicate that authentication is needed. */
100 packet_start(SSH_SMSG_FAILURE);
101 packet_send();
102 packet_write_wait();
103
94ec8c6b 104 client_user = NULL;
105
59c97189 106 for (;;) {
94ec8c6b 107 /* default to fail */
108 authenticated = 0;
109
59c97189 110 info[0] = '\0';
a306f2dd 111
112 /* Get a packet from the client. */
54a5250f 113 type = packet_read();
a306f2dd 114
115 /* Process the packet. */
116 switch (type) {
a306f2dd 117
ced49be2 118#if defined(KRB4) || defined(KRB5)
a306f2dd 119 case SSH_CMSG_AUTH_KERBEROS:
120 if (!options.kerberos_authentication) {
a306f2dd 121 verbose("Kerberos authentication disabled.");
a306f2dd 122 } else {
ced49be2 123 char *kdata = packet_get_string(&dlen);
95500969 124 packet_check_eom();
184eed6a 125
ced49be2 126 if (kdata[0] == 4) { /* KRB_PROT_VERSION */
127#ifdef KRB4
128 KTEXT_ST tkt;
184eed6a 129
ced49be2 130 tkt.length = dlen;
131 if (tkt.length < MAX_KTXT_LEN)
132 memcpy(tkt.dat, kdata, tkt.length);
184eed6a 133
ced49be2 134 if (auth_krb4(authctxt, &tkt, &client_user)) {
135 authenticated = 1;
136 snprintf(info, sizeof(info),
137 " tktuser %.100s",
138 client_user);
94ec8c6b 139 }
ced49be2 140#endif /* KRB4 */
141 } else {
142#ifdef KRB5
143 krb5_data tkt;
144 tkt.length = dlen;
145 tkt.data = kdata;
184eed6a 146
ced49be2 147 if (auth_krb5(authctxt, &tkt, &client_user)) {
148 authenticated = 1;
149 snprintf(info, sizeof(info),
150 " tktuser %.100s",
151 client_user);
ced49be2 152 }
153#endif /* KRB5 */
a306f2dd 154 }
59c97189 155 xfree(kdata);
a306f2dd 156 }
157 break;
ced49be2 158#endif /* KRB4 || KRB5 */
184eed6a 159
ced49be2 160#if defined(AFS) || defined(KRB5)
161 /* XXX - punt on backward compatibility here. */
162 case SSH_CMSG_HAVE_KERBEROS_TGT:
163 packet_send_debug("Kerberos TGT passing disabled before authentication.");
164 break;
165#ifdef AFS
166 case SSH_CMSG_HAVE_AFS_TOKEN:
167 packet_send_debug("AFS token passing disabled before authentication.");
168 break;
169#endif /* AFS */
170#endif /* AFS || KRB5 */
184eed6a 171
a306f2dd 172 case SSH_CMSG_AUTH_RHOSTS:
173 if (!options.rhosts_authentication) {
174 verbose("Rhosts authentication disabled.");
175 break;
176 }
177 /*
178 * Get client user name. Note that we just have to
179 * trust the client; this is one reason why rhosts
180 * authentication is insecure. (Another is
181 * IP-spoofing on a local network.)
182 */
183 client_user = packet_get_string(&ulen);
95500969 184 packet_check_eom();
a306f2dd 185
94ec8c6b 186 /* Try to authenticate using /etc/hosts.equiv and .rhosts. */
a306f2dd 187 authenticated = auth_rhosts(pw, client_user);
188
59c97189 189 snprintf(info, sizeof info, " ruser %.100s", client_user);
a306f2dd 190 break;
191
192 case SSH_CMSG_AUTH_RHOSTS_RSA:
193 if (!options.rhosts_rsa_authentication) {
194 verbose("Rhosts with RSA authentication disabled.");
195 break;
196 }
197 /*
198 * Get client user name. Note that we just have to
199 * trust the client; root on the client machine can
200 * claim to be any user.
201 */
202 client_user = packet_get_string(&ulen);
203
204 /* Get the client host key. */
b775c6f2 205 client_host_key = key_new(KEY_RSA1);
a306f2dd 206 bits = packet_get_int();
20b279e6 207 packet_get_bignum(client_host_key->rsa->e);
208 packet_get_bignum(client_host_key->rsa->n);
a306f2dd 209
b775c6f2 210 if (bits != BN_num_bits(client_host_key->rsa->n))
94ec8c6b 211 verbose("Warning: keysize mismatch for client_host_key: "
b775c6f2 212 "actual %d, announced %d",
213 BN_num_bits(client_host_key->rsa->n), bits);
95500969 214 packet_check_eom();
a306f2dd 215
b775c6f2 216 authenticated = auth_rhosts_rsa(pw, client_user,
dc421aa3 217 client_host_key);
b775c6f2 218 key_free(client_host_key);
a306f2dd 219
59c97189 220 snprintf(info, sizeof info, " ruser %.100s", client_user);
a306f2dd 221 break;
222
223 case SSH_CMSG_AUTH_RSA:
224 if (!options.rsa_authentication) {
225 verbose("RSA authentication disabled.");
226 break;
227 }
228 /* RSA authentication requested. */
b775c6f2 229 if ((n = BN_new()) == NULL)
230 fatal("do_authloop: BN_new failed");
20b279e6 231 packet_get_bignum(n);
95500969 232 packet_check_eom();
a306f2dd 233 authenticated = auth_rsa(pw, n);
234 BN_clear_free(n);
235 break;
236
237 case SSH_CMSG_AUTH_PASSWORD:
238 if (!options.password_authentication) {
239 verbose("Password authentication disabled.");
240 break;
241 }
242 /*
243 * Read user password. It is in plain text, but was
244 * transmitted over the encrypted channel so it is
245 * not visible to an outside observer.
246 */
247 password = packet_get_string(&dlen);
95500969 248 packet_check_eom();
a306f2dd 249
250#ifdef USE_PAM
251 /* Do PAM auth with password */
252 authenticated = auth_pam_password(pw, password);
4d33e531 253#elif defined(HAVE_OSF_SIA)
254 /* Do SIA auth with password */
b7ccb051 255 authenticated = auth_sia_password(authctxt->user,
256 password);
b4b5d644 257 /* Try authentication with the password. */
1853d1ef 258 authenticated = PRIVSEP(auth_password(authctxt, password));
a306f2dd 259#endif /* USE_PAM */
260
261 memset(password, 0, strlen(password));
262 xfree(password);
263 break;
264
a306f2dd 265 case SSH_CMSG_AUTH_TIS:
266 debug("rcvd SSH_CMSG_AUTH_TIS");
5ba55ada 267 if (options.challenge_response_authentication == 1) {
268 char *challenge = get_challenge(authctxt);
59c97189 269 if (challenge != NULL) {
270 debug("sending challenge '%s'", challenge);
a306f2dd 271 packet_start(SSH_SMSG_AUTH_TIS_CHALLENGE);
59c97189 272 packet_put_cstring(challenge);
5ba55ada 273 xfree(challenge);
a306f2dd 274 packet_send();
275 packet_write_wait();
276 continue;
277 }
278 }
279 break;
280 case SSH_CMSG_AUTH_TIS_RESPONSE:
281 debug("rcvd SSH_CMSG_AUTH_TIS_RESPONSE");
5ba55ada 282 if (options.challenge_response_authentication == 1) {
a306f2dd 283 char *response = packet_get_string(&dlen);
59c97189 284 debug("got response '%s'", response);
95500969 285 packet_check_eom();
59c97189 286 authenticated = verify_response(authctxt, response);
287 memset(response, 'r', dlen);
a306f2dd 288 xfree(response);
289 }
290 break;
a306f2dd 291
292 default:
293 /*
294 * Any unknown messages will be ignored (and failure
295 * returned) during authentication.
296 */
297 log("Unknown message during authentication: type %d", type);
298 break;
299 }
af774732 300#ifdef BSD_AUTH
301 if (authctxt->as) {
302 auth_close(authctxt->as);
303 authctxt->as = NULL;
304 }
305#endif
59c97189 306 if (!authctxt->valid && authenticated)
307 fatal("INTERNAL ERROR: authenticated invalid user %s",
308 authctxt->user);
a306f2dd 309
2b87da3b 310#ifdef HAVE_CYGWIN
311 if (authenticated &&
e9571a2c 312 !check_nt_auth(type == SSH_CMSG_AUTH_PASSWORD, pw)) {
3c62e7eb 313 packet_disconnect("Authentication rejected for uid %d.",
e9571a2c 314 pw == NULL ? -1 : pw->pw_uid);
3c62e7eb 315 authenticated = 0;
316 }
3c418020 317#else
59c97189 318 /* Special handling for root */
15853e93 319 if (authenticated && authctxt->pw->pw_uid == 0 &&
320 !auth_root_allowed(get_authname(type)))
59c97189 321 authenticated = 0;
3c418020 322#endif
2b87da3b 323#ifdef USE_PAM
94ec8c6b 324 if (authenticated && !do_pam_account(pw->pw_name, client_user))
325 authenticated = 0;
326#endif
a306f2dd 327
59c97189 328 /* Log before sending the reply */
329 auth_log(authctxt, authenticated, get_authname(type), info);
330
a306f2dd 331 if (client_user != NULL) {
332 xfree(client_user);
333 client_user = NULL;
334 }
335
94ec8c6b 336 if (authenticated)
337 return;
338
b4b5d644 339 if (authctxt->failures++ > AUTH_FAIL_MAX) {
2b87da3b 340#ifdef WITH_AIXAUTHENTICATE
341 loginfailed(authctxt->user,
983784a1 342 get_canonical_hostname(options.verify_reverse_mapping),
61e96248 343 "ssh");
c1ef8333 344#endif /* WITH_AIXAUTHENTICATE */
59c97189 345 packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
c1ef8333 346 }
a306f2dd 347
a306f2dd 348 packet_start(SSH_SMSG_FAILURE);
349 packet_send();
350 packet_write_wait();
351 }
352}
353
354/*
355 * Performs authentication of an incoming connection. Session key has already
356 * been exchanged and encryption is enabled.
357 */
1b34c1b3 358Authctxt *
d5bb9418 359do_authentication(void)
a306f2dd 360{
59c97189 361 Authctxt *authctxt;
1853d1ef 362 struct passwd *pw = NULL, *pwent;
1e3b8b07 363 u_int ulen;
ced49be2 364 char *p, *user, *style = NULL;
a306f2dd 365
366 /* Get the name of the user that we wish to log in as. */
54a5250f 367 packet_read_expect(SSH_CMSG_USER);
a306f2dd 368
369 /* Get the user name. */
370 user = packet_get_string(&ulen);
95500969 371 packet_check_eom();
a306f2dd 372
59c97189 373 if ((style = strchr(user, ':')) != NULL)
ced49be2 374 *style++ = '\0';
59c97189 375
ced49be2 376 /* XXX - SSH.com Kerberos v5 braindeath. */
377 if ((p = strchr(user, '@')) != NULL)
378 *p = '\0';
184eed6a 379
59c97189 380 authctxt = authctxt_new();
381 authctxt->user = user;
382 authctxt->style = style;
383
a306f2dd 384 /* Verify that the user is a valid user. */
1853d1ef 385 pwent = PRIVSEP(getpwnamallow(user));
386 if (pwent) {
59c97189 387 authctxt->valid = 1;
1853d1ef 388 pw = pwcopy(pwent);
94ec8c6b 389 } else {
59c97189 390 debug("do_authentication: illegal user %s", user);
94ec8c6b 391 pw = NULL;
392 }
1853d1ef 393 /* Free memory */
394 if (use_privsep && pwent != NULL)
395 pwfree(pwent);
396
59c97189 397 authctxt->pw = pw;
a306f2dd 398
1853d1ef 399 setproctitle("%s%s", pw ? user : "unknown",
400 use_privsep ? " [net]" : "");
6172e290 401
a306f2dd 402#ifdef USE_PAM
3b83c722 403 start_pam(pw == NULL ? "NOUSER" : user);
a306f2dd 404#endif
405
406 /*
407 * If we are not running as root, the user must have the same uid as
94ec8c6b 408 * the server. (Unless you are running Windows)
a306f2dd 409 */
94ec8c6b 410#ifndef HAVE_CYGWIN
1853d1ef 411 if (!use_privsep && getuid() != 0 && pw && pw->pw_uid != getuid())
a306f2dd 412 packet_disconnect("Cannot change user when server not running as root.");
3c62e7eb 413#endif
a306f2dd 414
59c97189 415 /*
b4b5d644 416 * Loop until the user has been authenticated or the connection is
417 * closed, do_authloop() returns only if authentication is successful
418 */
59c97189 419 do_authloop(authctxt);
a306f2dd 420
421 /* The user has been authenticated and accepted. */
94ec8c6b 422 packet_start(SSH_SMSG_SUCCESS);
423 packet_send();
424 packet_write_wait();
59c97189 425
1b34c1b3 426 return (authctxt);
a306f2dd 427}
This page took 1.054906 seconds and 5 git commands to generate.