blob: 11c56a51983e2a569442776dae8f2059d2356937 [file] [log] [blame]
Damien Millereba71ba2000-04-29 23:57:08 +10001/*
2 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
3 * All rights reserved
Damien Millere4340be2000-09-16 13:29:08 +11004 *
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".
Damien Millereba71ba2000-04-29 23:57:08 +100010 */
11
12#include "includes.h"
Ben Lindstromc1ba31f2001-02-15 03:14:11 +000013RCSID("$OpenBSD: auth1.c,v 1.17 2001/02/13 22:49:40 markus Exp $");
Damien Millereba71ba2000-04-29 23:57:08 +100014
Damien Miller874d77b2000-10-14 16:23:11 +110015#include "xmalloc.h"
16#include "rsa.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000017#include "ssh1.h"
Damien Miller874d77b2000-10-14 16:23:11 +110018#include "packet.h"
19#include "buffer.h"
20#include "mpaux.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000021#include "log.h"
Damien Miller874d77b2000-10-14 16:23:11 +110022#include "servconf.h"
23#include "compat.h"
24#include "auth.h"
25#include "session.h"
26
Damien Millereba71ba2000-04-29 23:57:08 +100027/* import */
28extern ServerOptions options;
29extern char *forced_command;
Damien Miller874d77b2000-10-14 16:23:11 +110030
31#ifdef WITH_AIXAUTHENTICATE
32extern char *aixloginmsg;
33#endif /* WITH_AIXAUTHENTICATE */
Damien Millereba71ba2000-04-29 23:57:08 +100034
35/*
36 * convert ssh auth msg type into description
37 */
38char *
39get_authname(int type)
40{
41 static char buf[1024];
42 switch (type) {
43 case SSH_CMSG_AUTH_PASSWORD:
44 return "password";
45 case SSH_CMSG_AUTH_RSA:
46 return "rsa";
47 case SSH_CMSG_AUTH_RHOSTS_RSA:
48 return "rhosts-rsa";
49 case SSH_CMSG_AUTH_RHOSTS:
50 return "rhosts";
Ben Lindstromdb65e8f2001-01-19 04:26:52 +000051 case SSH_CMSG_AUTH_TIS:
52 case SSH_CMSG_AUTH_TIS_RESPONSE:
53 return "challenge-response";
Damien Millereba71ba2000-04-29 23:57:08 +100054#ifdef KRB4
55 case SSH_CMSG_AUTH_KERBEROS:
56 return "kerberos";
57#endif
Damien Millereba71ba2000-04-29 23:57:08 +100058 }
59 snprintf(buf, sizeof buf, "bad-auth-msg-%d", type);
60 return buf;
61}
62
63/*
Ben Lindstromdb65e8f2001-01-19 04:26:52 +000064 * read packets, try to authenticate the user and
65 * return only if authentication is successful
Damien Millereba71ba2000-04-29 23:57:08 +100066 */
67void
Ben Lindstromdb65e8f2001-01-19 04:26:52 +000068do_authloop(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +100069{
Damien Miller874d77b2000-10-14 16:23:11 +110070 int authenticated = 0;
Ben Lindstrom46c16222000-12-22 01:43:59 +000071 u_int bits;
Damien Millereba71ba2000-04-29 23:57:08 +100072 RSA *client_host_key;
73 BIGNUM *n;
Damien Miller874d77b2000-10-14 16:23:11 +110074 char *client_user, *password;
Ben Lindstromdb65e8f2001-01-19 04:26:52 +000075 char info[1024];
Ben Lindstrom46c16222000-12-22 01:43:59 +000076 u_int dlen;
Damien Millereba71ba2000-04-29 23:57:08 +100077 int plen, nlen, elen;
Ben Lindstrom46c16222000-12-22 01:43:59 +000078 u_int ulen;
Damien Millereba71ba2000-04-29 23:57:08 +100079 int type = 0;
Ben Lindstromdb65e8f2001-01-19 04:26:52 +000080 struct passwd *pw = authctxt->pw;
81
82 debug("Attempting authentication for %s%.100s.",
83 authctxt->valid ? "" : "illegal user ", authctxt->user);
84
85 /* If the user has no password, accept authentication immediately. */
86 if (options.password_authentication &&
87#ifdef KRB4
88 (!options.kerberos_authentication || options.kerberos_or_local_passwd) &&
89#endif
Ben Lindstromb100ec92001-01-19 05:37:32 +000090#ifdef USE_PAM
Kevin Stevesbca8c8f2001-02-13 11:26:21 +000091 auth_pam_password(pw, "")) {
Damien Miller92ddb7d2001-02-14 01:25:23 +110092#elif defined(HAVE_OSF_SIA)
93 0) {
Ben Lindstromdb65e8f2001-01-19 04:26:52 +000094#else
95 auth_password(pw, "")) {
96#endif
97 auth_log(authctxt, 1, "without authentication", "");
98 return;
99 }
Damien Millereba71ba2000-04-29 23:57:08 +1000100
101 /* Indicate that authentication is needed. */
102 packet_start(SSH_SMSG_FAILURE);
103 packet_send();
104 packet_write_wait();
105
Damien Miller874d77b2000-10-14 16:23:11 +1100106 client_user = NULL;
107
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000108 for (;;) {
Damien Miller874d77b2000-10-14 16:23:11 +1100109 /* default to fail */
110 authenticated = 0;
111
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000112 info[0] = '\0';
Damien Millereba71ba2000-04-29 23:57:08 +1000113
114 /* Get a packet from the client. */
115 type = packet_read(&plen);
116
117 /* Process the packet. */
118 switch (type) {
119#ifdef AFS
120 case SSH_CMSG_HAVE_KERBEROS_TGT:
121 if (!options.kerberos_tgt_passing) {
Damien Millereba71ba2000-04-29 23:57:08 +1000122 verbose("Kerberos tgt passing disabled.");
123 break;
124 } else {
125 /* Accept Kerberos tgt. */
126 char *tgt = packet_get_string(&dlen);
127 packet_integrity_check(plen, 4 + dlen, type);
128 if (!auth_kerberos_tgt(pw, tgt))
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000129 verbose("Kerberos tgt REFUSED for %.100s", authctxt->user);
Damien Millereba71ba2000-04-29 23:57:08 +1000130 xfree(tgt);
131 }
132 continue;
133
134 case SSH_CMSG_HAVE_AFS_TOKEN:
135 if (!options.afs_token_passing || !k_hasafs()) {
Damien Millereba71ba2000-04-29 23:57:08 +1000136 verbose("AFS token passing disabled.");
137 break;
138 } else {
139 /* Accept AFS token. */
140 char *token_string = packet_get_string(&dlen);
141 packet_integrity_check(plen, 4 + dlen, type);
142 if (!auth_afs_token(pw, token_string))
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000143 verbose("AFS token REFUSED for %.100s", authctxt->user);
Damien Millereba71ba2000-04-29 23:57:08 +1000144 xfree(token_string);
145 }
146 continue;
147#endif /* AFS */
148#ifdef KRB4
149 case SSH_CMSG_AUTH_KERBEROS:
150 if (!options.kerberos_authentication) {
Damien Millereba71ba2000-04-29 23:57:08 +1000151 verbose("Kerberos authentication disabled.");
152 break;
153 } else {
154 /* Try Kerberos v4 authentication. */
155 KTEXT_ST auth;
156 char *tkt_user = NULL;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000157 char *kdata = packet_get_string((u_int *) &auth.length);
Damien Millereba71ba2000-04-29 23:57:08 +1000158 packet_integrity_check(plen, 4 + auth.length, type);
159
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000160 if (authctxt->valid) {
161 if (auth.length < MAX_KTXT_LEN)
162 memcpy(auth.dat, kdata, auth.length);
Damien Miller874d77b2000-10-14 16:23:11 +1100163 authenticated = auth_krb4(pw->pw_name, &auth, &tkt_user);
164 if (authenticated) {
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000165 snprintf(info, sizeof info,
166 " tktuser %.100s", tkt_user);
Damien Miller874d77b2000-10-14 16:23:11 +1100167 xfree(tkt_user);
168 }
Damien Millereba71ba2000-04-29 23:57:08 +1000169 }
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000170 xfree(kdata);
Damien Millereba71ba2000-04-29 23:57:08 +1000171 }
172 break;
173#endif /* KRB4 */
174
175 case SSH_CMSG_AUTH_RHOSTS:
176 if (!options.rhosts_authentication) {
177 verbose("Rhosts authentication disabled.");
178 break;
179 }
180 /*
181 * Get client user name. Note that we just have to
182 * trust the client; this is one reason why rhosts
183 * authentication is insecure. (Another is
184 * IP-spoofing on a local network.)
185 */
186 client_user = packet_get_string(&ulen);
187 packet_integrity_check(plen, 4 + ulen, type);
188
Damien Miller874d77b2000-10-14 16:23:11 +1100189 /* Try to authenticate using /etc/hosts.equiv and .rhosts. */
Damien Millereba71ba2000-04-29 23:57:08 +1000190 authenticated = auth_rhosts(pw, client_user);
191
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000192 snprintf(info, sizeof info, " ruser %.100s", client_user);
Damien Millereba71ba2000-04-29 23:57:08 +1000193 break;
194
195 case SSH_CMSG_AUTH_RHOSTS_RSA:
196 if (!options.rhosts_rsa_authentication) {
197 verbose("Rhosts with RSA authentication disabled.");
198 break;
199 }
200 /*
201 * Get client user name. Note that we just have to
202 * trust the client; root on the client machine can
203 * claim to be any user.
204 */
205 client_user = packet_get_string(&ulen);
206
207 /* Get the client host key. */
208 client_host_key = RSA_new();
209 if (client_host_key == NULL)
210 fatal("RSA_new failed");
211 client_host_key->e = BN_new();
212 client_host_key->n = BN_new();
213 if (client_host_key->e == NULL || client_host_key->n == NULL)
214 fatal("BN_new failed");
215 bits = packet_get_int();
216 packet_get_bignum(client_host_key->e, &elen);
217 packet_get_bignum(client_host_key->n, &nlen);
218
219 if (bits != BN_num_bits(client_host_key->n))
Damien Miller874d77b2000-10-14 16:23:11 +1100220 verbose("Warning: keysize mismatch for client_host_key: "
Damien Millerbd483e72000-04-30 10:00:53 +1000221 "actual %d, announced %d", BN_num_bits(client_host_key->n), bits);
Damien Millereba71ba2000-04-29 23:57:08 +1000222 packet_integrity_check(plen, (4 + ulen) + 4 + elen + nlen, type);
223
224 authenticated = auth_rhosts_rsa(pw, client_user, client_host_key);
225 RSA_free(client_host_key);
226
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000227 snprintf(info, sizeof info, " ruser %.100s", client_user);
Damien Millereba71ba2000-04-29 23:57:08 +1000228 break;
229
230 case SSH_CMSG_AUTH_RSA:
231 if (!options.rsa_authentication) {
232 verbose("RSA authentication disabled.");
233 break;
234 }
235 /* RSA authentication requested. */
236 n = BN_new();
237 packet_get_bignum(n, &nlen);
238 packet_integrity_check(plen, nlen, type);
239 authenticated = auth_rsa(pw, n);
240 BN_clear_free(n);
241 break;
242
243 case SSH_CMSG_AUTH_PASSWORD:
244 if (!options.password_authentication) {
245 verbose("Password authentication disabled.");
246 break;
247 }
248 /*
249 * Read user password. It is in plain text, but was
250 * transmitted over the encrypted channel so it is
251 * not visible to an outside observer.
252 */
253 password = packet_get_string(&dlen);
254 packet_integrity_check(plen, 4 + dlen, type);
255
256#ifdef USE_PAM
257 /* Do PAM auth with password */
258 authenticated = auth_pam_password(pw, password);
Damien Millerb8c656e2000-06-28 15:22:41 +1000259#elif defined(HAVE_OSF_SIA)
260 /* Do SIA auth with password */
Damien Miller92ddb7d2001-02-14 01:25:23 +1100261 authenticated = auth_sia_password(authctxt->user,
262 password);
Damien Millerb8c656e2000-06-28 15:22:41 +1000263#else /* !USE_PAM && !HAVE_OSF_SIA */
Kevin Steves12aaa042001-01-24 21:23:39 +0000264 /* Try authentication with the password. */
Damien Millereba71ba2000-04-29 23:57:08 +1000265 authenticated = auth_password(pw, password);
266#endif /* USE_PAM */
267
268 memset(password, 0, strlen(password));
269 xfree(password);
270 break;
271
Damien Millereba71ba2000-04-29 23:57:08 +1000272 case SSH_CMSG_AUTH_TIS:
273 debug("rcvd SSH_CMSG_AUTH_TIS");
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000274 if (options.challenge_reponse_authentication == 1) {
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000275 char *challenge = get_challenge(authctxt, authctxt->style);
276 if (challenge != NULL) {
277 debug("sending challenge '%s'", challenge);
Damien Millereba71ba2000-04-29 23:57:08 +1000278 packet_start(SSH_SMSG_AUTH_TIS_CHALLENGE);
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000279 packet_put_cstring(challenge);
Damien Millereba71ba2000-04-29 23:57:08 +1000280 packet_send();
281 packet_write_wait();
282 continue;
283 }
284 }
285 break;
286 case SSH_CMSG_AUTH_TIS_RESPONSE:
287 debug("rcvd SSH_CMSG_AUTH_TIS_RESPONSE");
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000288 if (options.challenge_reponse_authentication == 1) {
Damien Millereba71ba2000-04-29 23:57:08 +1000289 char *response = packet_get_string(&dlen);
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000290 debug("got response '%s'", response);
Damien Millereba71ba2000-04-29 23:57:08 +1000291 packet_integrity_check(plen, 4 + dlen, type);
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000292 authenticated = verify_response(authctxt, response);
293 memset(response, 'r', dlen);
Damien Millereba71ba2000-04-29 23:57:08 +1000294 xfree(response);
295 }
296 break;
Damien Millereba71ba2000-04-29 23:57:08 +1000297
298 default:
299 /*
300 * Any unknown messages will be ignored (and failure
301 * returned) during authentication.
302 */
303 log("Unknown message during authentication: type %d", type);
304 break;
305 }
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000306 if (!authctxt->valid && authenticated)
307 fatal("INTERNAL ERROR: authenticated invalid user %s",
308 authctxt->user);
Damien Millereba71ba2000-04-29 23:57:08 +1000309
Kevin Stevesef4eea92001-02-05 12:42:17 +0000310#ifdef HAVE_CYGWIN
311 if (authenticated &&
Damien Millerb70b61f2000-09-16 16:25:12 +1100312 !check_nt_auth(type == SSH_CMSG_AUTH_PASSWORD,pw->pw_uid)) {
Damien Millerbac2d8a2000-09-05 16:13:06 +1100313 packet_disconnect("Authentication rejected for uid %d.",
Damien Miller874d77b2000-10-14 16:23:11 +1100314 (int)pw->pw_uid);
Damien Millerbac2d8a2000-09-05 16:13:06 +1100315 authenticated = 0;
316 }
Ben Lindstrom5dc81502001-01-19 06:10:29 +0000317#else
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000318 /* Special handling for root */
Ben Lindstromd8a90212001-02-15 03:08:27 +0000319 if (authenticated && authctxt->pw->pw_uid == 0 &&
320 !auth_root_allowed(get_authname(type)))
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000321 authenticated = 0;
Ben Lindstrom5dc81502001-01-19 06:10:29 +0000322#endif
Kevin Stevesef4eea92001-02-05 12:42:17 +0000323#ifdef USE_PAM
Damien Miller874d77b2000-10-14 16:23:11 +1100324 if (authenticated && !do_pam_account(pw->pw_name, client_user))
325 authenticated = 0;
326#endif
Damien Millereba71ba2000-04-29 23:57:08 +1000327
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000328 /* Log before sending the reply */
329 auth_log(authctxt, authenticated, get_authname(type), info);
330
Damien Millereba71ba2000-04-29 23:57:08 +1000331 if (client_user != NULL) {
332 xfree(client_user);
333 client_user = NULL;
334 }
335
Damien Miller874d77b2000-10-14 16:23:11 +1100336 if (authenticated)
337 return;
338
Kevin Steves12aaa042001-01-24 21:23:39 +0000339 if (authctxt->failures++ > AUTH_FAIL_MAX) {
Kevin Stevesef4eea92001-02-05 12:42:17 +0000340#ifdef WITH_AIXAUTHENTICATE
341 loginfailed(authctxt->user,
342 get_canonical_hostname(options.reverse_mapping_check),
Damien Miller33804262001-02-04 23:20:18 +1100343 "ssh");
Damien Millerd2c208a2000-05-17 22:00:02 +1000344#endif /* WITH_AIXAUTHENTICATE */
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000345 packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
Damien Millerd2c208a2000-05-17 22:00:02 +1000346 }
Damien Millereba71ba2000-04-29 23:57:08 +1000347
Damien Millereba71ba2000-04-29 23:57:08 +1000348 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 */
358void
359do_authentication()
360{
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000361 Authctxt *authctxt;
362 struct passwd *pw;
Damien Millereba71ba2000-04-29 23:57:08 +1000363 int plen;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000364 u_int ulen;
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000365 char *user, *style = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000366
367 /* Get the name of the user that we wish to log in as. */
368 packet_read_expect(&plen, SSH_CMSG_USER);
369
370 /* Get the user name. */
371 user = packet_get_string(&ulen);
372 packet_integrity_check(plen, (4 + ulen), SSH_CMSG_USER);
373
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000374 if ((style = strchr(user, ':')) != NULL)
375 *style++ = 0;
376
377 authctxt = authctxt_new();
378 authctxt->user = user;
379 authctxt->style = style;
380
Damien Millereba71ba2000-04-29 23:57:08 +1000381 /* Verify that the user is a valid user. */
382 pw = getpwnam(user);
Damien Miller874d77b2000-10-14 16:23:11 +1100383 if (pw && allowed_user(pw)) {
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000384 authctxt->valid = 1;
385 pw = pwcopy(pw);
Damien Miller874d77b2000-10-14 16:23:11 +1100386 } else {
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000387 debug("do_authentication: illegal user %s", user);
Damien Miller874d77b2000-10-14 16:23:11 +1100388 pw = NULL;
389 }
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000390 authctxt->pw = pw;
Damien Millereba71ba2000-04-29 23:57:08 +1000391
Ben Lindstromc1ba31f2001-02-15 03:14:11 +0000392 setproctitle("%s", pw ? user : "unknown");
393
Damien Millereba71ba2000-04-29 23:57:08 +1000394#ifdef USE_PAM
Damien Miller874d77b2000-10-14 16:23:11 +1100395 if (pw)
Damien Miller22e22bf2001-01-19 15:46:38 +1100396 start_pam(user);
Damien Millereba71ba2000-04-29 23:57:08 +1000397#endif
398
399 /*
400 * If we are not running as root, the user must have the same uid as
Damien Miller874d77b2000-10-14 16:23:11 +1100401 * the server. (Unless you are running Windows)
Damien Millereba71ba2000-04-29 23:57:08 +1000402 */
Damien Miller874d77b2000-10-14 16:23:11 +1100403#ifndef HAVE_CYGWIN
404 if (getuid() != 0 && pw && pw->pw_uid != getuid())
Damien Millereba71ba2000-04-29 23:57:08 +1000405 packet_disconnect("Cannot change user when server not running as root.");
Damien Millerbac2d8a2000-09-05 16:13:06 +1100406#endif
Damien Millereba71ba2000-04-29 23:57:08 +1000407
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000408 /*
Kevin Steves12aaa042001-01-24 21:23:39 +0000409 * Loop until the user has been authenticated or the connection is
410 * closed, do_authloop() returns only if authentication is successful
411 */
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000412 do_authloop(authctxt);
Damien Millereba71ba2000-04-29 23:57:08 +1000413
414 /* The user has been authenticated and accepted. */
Damien Miller874d77b2000-10-14 16:23:11 +1100415 packet_start(SSH_SMSG_SUCCESS);
416 packet_send();
417 packet_write_wait();
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000418
Damien Millereba71ba2000-04-29 23:57:08 +1000419#ifdef WITH_AIXAUTHENTICATE
Damien Millerd2c208a2000-05-17 22:00:02 +1000420 /* We don't have a pty yet, so just label the line as "ssh" */
Kevin Stevesef4eea92001-02-05 12:42:17 +0000421 if (loginsuccess(authctxt->user,
Damien Miller33804262001-02-04 23:20:18 +1100422 get_canonical_hostname(options.reverse_mapping_check),
423 "ssh", &aixloginmsg) < 0)
Damien Millerd2c208a2000-05-17 22:00:02 +1000424 aixloginmsg = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000425#endif /* WITH_AIXAUTHENTICATE */
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000426
427 xfree(authctxt->user);
428 xfree(authctxt);
Damien Millereba71ba2000-04-29 23:57:08 +1000429
430 /* Perform session preparation. */
431 do_authenticated(pw);
432}