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