blob: b480fdbecd297b964456f1f610a58c19fd49f3a2 [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 Lindstromdb65e8f2001-01-19 04:26:52 +000013RCSID("$OpenBSD: auth1.c,v 1.11 2001/01/18 16:59:59 markus Exp $");
Damien Millereba71ba2000-04-29 23:57:08 +100014
Damien Millerb8c656e2000-06-28 15:22:41 +100015#ifdef HAVE_OSF_SIA
16# include <sia.h>
17# include <siad.h>
18#endif
19
Damien Miller874d77b2000-10-14 16:23:11 +110020#include "xmalloc.h"
21#include "rsa.h"
22#include "ssh.h"
23#include "packet.h"
24#include "buffer.h"
25#include "mpaux.h"
26#include "servconf.h"
27#include "compat.h"
28#include "auth.h"
29#include "session.h"
30
Damien Millereba71ba2000-04-29 23:57:08 +100031/* import */
32extern ServerOptions options;
33extern char *forced_command;
Damien Miller874d77b2000-10-14 16:23:11 +110034
35#ifdef WITH_AIXAUTHENTICATE
36extern char *aixloginmsg;
37#endif /* WITH_AIXAUTHENTICATE */
Damien Millerfe668e42000-07-08 10:44:13 +100038#ifdef HAVE_OSF_SIA
39extern int saved_argc;
40extern char **saved_argv;
41#endif /* HAVE_OSF_SIA */
Damien Millereba71ba2000-04-29 23:57:08 +100042
43/*
44 * convert ssh auth msg type into description
45 */
46char *
47get_authname(int type)
48{
49 static char buf[1024];
50 switch (type) {
51 case SSH_CMSG_AUTH_PASSWORD:
52 return "password";
53 case SSH_CMSG_AUTH_RSA:
54 return "rsa";
55 case SSH_CMSG_AUTH_RHOSTS_RSA:
56 return "rhosts-rsa";
57 case SSH_CMSG_AUTH_RHOSTS:
58 return "rhosts";
Ben Lindstromdb65e8f2001-01-19 04:26:52 +000059 case SSH_CMSG_AUTH_TIS:
60 case SSH_CMSG_AUTH_TIS_RESPONSE:
61 return "challenge-response";
Damien Millereba71ba2000-04-29 23:57:08 +100062#ifdef KRB4
63 case SSH_CMSG_AUTH_KERBEROS:
64 return "kerberos";
65#endif
Damien Millereba71ba2000-04-29 23:57:08 +100066 }
67 snprintf(buf, sizeof buf, "bad-auth-msg-%d", type);
68 return buf;
69}
70
71/*
Ben Lindstromdb65e8f2001-01-19 04:26:52 +000072 * read packets, try to authenticate the user and
73 * return only if authentication is successful
Damien Millereba71ba2000-04-29 23:57:08 +100074 */
75void
Ben Lindstromdb65e8f2001-01-19 04:26:52 +000076do_authloop(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +100077{
Damien Miller874d77b2000-10-14 16:23:11 +110078 int authenticated = 0;
Ben Lindstrom46c16222000-12-22 01:43:59 +000079 u_int bits;
Damien Millereba71ba2000-04-29 23:57:08 +100080 RSA *client_host_key;
81 BIGNUM *n;
Damien Miller874d77b2000-10-14 16:23:11 +110082 char *client_user, *password;
Ben Lindstromdb65e8f2001-01-19 04:26:52 +000083 char info[1024];
Ben Lindstrom46c16222000-12-22 01:43:59 +000084 u_int dlen;
Damien Millereba71ba2000-04-29 23:57:08 +100085 int plen, nlen, elen;
Ben Lindstrom46c16222000-12-22 01:43:59 +000086 u_int ulen;
Damien Millereba71ba2000-04-29 23:57:08 +100087 int type = 0;
Ben Lindstromdb65e8f2001-01-19 04:26:52 +000088 struct passwd *pw = authctxt->pw;
89
90 debug("Attempting authentication for %s%.100s.",
91 authctxt->valid ? "" : "illegal user ", authctxt->user);
92
93 /* If the user has no password, accept authentication immediately. */
94 if (options.password_authentication &&
95#ifdef KRB4
96 (!options.kerberos_authentication || options.kerberos_or_local_passwd) &&
97#endif
Ben Lindstromb100ec92001-01-19 05:37:32 +000098#ifdef USE_PAM
Ben Lindstromdb65e8f2001-01-19 04:26:52 +000099 auth_pam_password(pw, password)) {
100#else
101 auth_password(pw, "")) {
102#endif
103 auth_log(authctxt, 1, "without authentication", "");
104 return;
105 }
Damien Millereba71ba2000-04-29 23:57:08 +1000106
107 /* Indicate that authentication is needed. */
108 packet_start(SSH_SMSG_FAILURE);
109 packet_send();
110 packet_write_wait();
111
Damien Miller874d77b2000-10-14 16:23:11 +1100112 client_user = NULL;
113
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000114 for (;;) {
Damien Miller874d77b2000-10-14 16:23:11 +1100115 /* default to fail */
116 authenticated = 0;
117
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000118 info[0] = '\0';
Damien Millereba71ba2000-04-29 23:57:08 +1000119
120 /* Get a packet from the client. */
121 type = packet_read(&plen);
122
123 /* Process the packet. */
124 switch (type) {
125#ifdef AFS
126 case SSH_CMSG_HAVE_KERBEROS_TGT:
127 if (!options.kerberos_tgt_passing) {
Damien Millereba71ba2000-04-29 23:57:08 +1000128 verbose("Kerberos tgt passing disabled.");
129 break;
130 } else {
131 /* Accept Kerberos tgt. */
132 char *tgt = packet_get_string(&dlen);
133 packet_integrity_check(plen, 4 + dlen, type);
134 if (!auth_kerberos_tgt(pw, tgt))
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000135 verbose("Kerberos tgt REFUSED for %.100s", authctxt->user);
Damien Millereba71ba2000-04-29 23:57:08 +1000136 xfree(tgt);
137 }
138 continue;
139
140 case SSH_CMSG_HAVE_AFS_TOKEN:
141 if (!options.afs_token_passing || !k_hasafs()) {
Damien Millereba71ba2000-04-29 23:57:08 +1000142 verbose("AFS token passing disabled.");
143 break;
144 } else {
145 /* Accept AFS token. */
146 char *token_string = packet_get_string(&dlen);
147 packet_integrity_check(plen, 4 + dlen, type);
148 if (!auth_afs_token(pw, token_string))
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000149 verbose("AFS token REFUSED for %.100s", authctxt->user);
Damien Millereba71ba2000-04-29 23:57:08 +1000150 xfree(token_string);
151 }
152 continue;
153#endif /* AFS */
154#ifdef KRB4
155 case SSH_CMSG_AUTH_KERBEROS:
156 if (!options.kerberos_authentication) {
Damien Millereba71ba2000-04-29 23:57:08 +1000157 verbose("Kerberos authentication disabled.");
158 break;
159 } else {
160 /* Try Kerberos v4 authentication. */
161 KTEXT_ST auth;
162 char *tkt_user = NULL;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000163 char *kdata = packet_get_string((u_int *) &auth.length);
Damien Millereba71ba2000-04-29 23:57:08 +1000164 packet_integrity_check(plen, 4 + auth.length, type);
165
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000166 if (authctxt->valid) {
167 if (auth.length < MAX_KTXT_LEN)
168 memcpy(auth.dat, kdata, auth.length);
Damien Miller874d77b2000-10-14 16:23:11 +1100169 authenticated = auth_krb4(pw->pw_name, &auth, &tkt_user);
170 if (authenticated) {
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000171 snprintf(info, sizeof info,
172 " tktuser %.100s", tkt_user);
Damien Miller874d77b2000-10-14 16:23:11 +1100173 xfree(tkt_user);
174 }
Damien Millereba71ba2000-04-29 23:57:08 +1000175 }
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000176 xfree(kdata);
Damien Millereba71ba2000-04-29 23:57:08 +1000177 }
178 break;
179#endif /* KRB4 */
180
181 case SSH_CMSG_AUTH_RHOSTS:
182 if (!options.rhosts_authentication) {
183 verbose("Rhosts authentication disabled.");
184 break;
185 }
186 /*
187 * Get client user name. Note that we just have to
188 * trust the client; this is one reason why rhosts
189 * authentication is insecure. (Another is
190 * IP-spoofing on a local network.)
191 */
192 client_user = packet_get_string(&ulen);
193 packet_integrity_check(plen, 4 + ulen, type);
194
Damien Miller874d77b2000-10-14 16:23:11 +1100195 /* Try to authenticate using /etc/hosts.equiv and .rhosts. */
Damien Millereba71ba2000-04-29 23:57:08 +1000196 authenticated = auth_rhosts(pw, client_user);
197
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000198 snprintf(info, sizeof info, " ruser %.100s", client_user);
Damien Millereba71ba2000-04-29 23:57:08 +1000199 break;
200
201 case SSH_CMSG_AUTH_RHOSTS_RSA:
202 if (!options.rhosts_rsa_authentication) {
203 verbose("Rhosts with RSA authentication disabled.");
204 break;
205 }
206 /*
207 * Get client user name. Note that we just have to
208 * trust the client; root on the client machine can
209 * claim to be any user.
210 */
211 client_user = packet_get_string(&ulen);
212
213 /* Get the client host key. */
214 client_host_key = RSA_new();
215 if (client_host_key == NULL)
216 fatal("RSA_new failed");
217 client_host_key->e = BN_new();
218 client_host_key->n = BN_new();
219 if (client_host_key->e == NULL || client_host_key->n == NULL)
220 fatal("BN_new failed");
221 bits = packet_get_int();
222 packet_get_bignum(client_host_key->e, &elen);
223 packet_get_bignum(client_host_key->n, &nlen);
224
225 if (bits != BN_num_bits(client_host_key->n))
Damien Miller874d77b2000-10-14 16:23:11 +1100226 verbose("Warning: keysize mismatch for client_host_key: "
Damien Millerbd483e72000-04-30 10:00:53 +1000227 "actual %d, announced %d", BN_num_bits(client_host_key->n), bits);
Damien Millereba71ba2000-04-29 23:57:08 +1000228 packet_integrity_check(plen, (4 + ulen) + 4 + elen + nlen, type);
229
230 authenticated = auth_rhosts_rsa(pw, client_user, client_host_key);
231 RSA_free(client_host_key);
232
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000233 snprintf(info, sizeof info, " ruser %.100s", client_user);
Damien Millereba71ba2000-04-29 23:57:08 +1000234 break;
235
236 case SSH_CMSG_AUTH_RSA:
237 if (!options.rsa_authentication) {
238 verbose("RSA authentication disabled.");
239 break;
240 }
241 /* RSA authentication requested. */
242 n = BN_new();
243 packet_get_bignum(n, &nlen);
244 packet_integrity_check(plen, nlen, type);
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);
260 packet_integrity_check(plen, 4 + dlen, type);
261
262#ifdef USE_PAM
263 /* Do PAM auth with password */
264 authenticated = auth_pam_password(pw, password);
Damien Millerb8c656e2000-06-28 15:22:41 +1000265#elif defined(HAVE_OSF_SIA)
266 /* Do SIA auth with password */
Damien Millerb8c656e2000-06-28 15:22:41 +1000267 if (sia_validate_user(NULL, saved_argc, saved_argv,
268 get_canonical_hostname(), pw->pw_name, NULL, 0,
269 NULL, password) == SIASUCCESS) {
270 authenticated = 1;
271 }
272#else /* !USE_PAM && !HAVE_OSF_SIA */
Damien Miller874d77b2000-10-14 16:23:11 +1100273 /* Try authentication with the password. */
Damien Millereba71ba2000-04-29 23:57:08 +1000274 authenticated = auth_password(pw, password);
275#endif /* USE_PAM */
276
277 memset(password, 0, strlen(password));
278 xfree(password);
279 break;
280
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000281#ifdef SKEY /* ISSUE: Is this right? we don't define
282 having skey_authentication in
283 servconf.h by default so I assume
284 we need to deal with this via #ifdef
285 in some reasonable way */
Damien Millereba71ba2000-04-29 23:57:08 +1000286 case SSH_CMSG_AUTH_TIS:
287 debug("rcvd SSH_CMSG_AUTH_TIS");
288 if (options.skey_authentication == 1) {
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000289 char *challenge = get_challenge(authctxt, authctxt->style);
290 if (challenge != NULL) {
291 debug("sending challenge '%s'", challenge);
Damien Millereba71ba2000-04-29 23:57:08 +1000292 packet_start(SSH_SMSG_AUTH_TIS_CHALLENGE);
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000293 packet_put_cstring(challenge);
Damien Millereba71ba2000-04-29 23:57:08 +1000294 packet_send();
295 packet_write_wait();
296 continue;
297 }
298 }
299 break;
300 case SSH_CMSG_AUTH_TIS_RESPONSE:
301 debug("rcvd SSH_CMSG_AUTH_TIS_RESPONSE");
302 if (options.skey_authentication == 1) {
303 char *response = packet_get_string(&dlen);
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000304 debug("got response '%s'", response);
Damien Millereba71ba2000-04-29 23:57:08 +1000305 packet_integrity_check(plen, 4 + dlen, type);
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000306 authenticated = verify_response(authctxt, response);
307 memset(response, 'r', dlen);
Damien Millereba71ba2000-04-29 23:57:08 +1000308 xfree(response);
309 }
310 break;
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000311#endif /* ISSUE: End of wrong SKEY defines */
Damien Millereba71ba2000-04-29 23:57:08 +1000312
313 default:
314 /*
315 * Any unknown messages will be ignored (and failure
316 * returned) during authentication.
317 */
318 log("Unknown message during authentication: type %d", type);
319 break;
320 }
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000321 if (!authctxt->valid && authenticated)
322 fatal("INTERNAL ERROR: authenticated invalid user %s",
323 authctxt->user);
Damien Millereba71ba2000-04-29 23:57:08 +1000324
Ben Lindstromcf0809d2001-01-19 15:44:10 +0000325#ifdef HAVE_CYGWIN
Damien Miller874d77b2000-10-14 16:23:11 +1100326 if (authenticated &&
Damien Millerb70b61f2000-09-16 16:25:12 +1100327 !check_nt_auth(type == SSH_CMSG_AUTH_PASSWORD,pw->pw_uid)) {
Damien Millerbac2d8a2000-09-05 16:13:06 +1100328 packet_disconnect("Authentication rejected for uid %d.",
Damien Miller874d77b2000-10-14 16:23:11 +1100329 (int)pw->pw_uid);
Damien Millerbac2d8a2000-09-05 16:13:06 +1100330 authenticated = 0;
331 }
Ben Lindstrom5dc81502001-01-19 06:10:29 +0000332#else
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000333 /* Special handling for root */
334 if (authenticated && authctxt->pw->pw_uid == 0 && !auth_root_allowed())
335 authenticated = 0;
Ben Lindstrom5dc81502001-01-19 06:10:29 +0000336#endif
337#ifdef USE_PAM
Damien Miller874d77b2000-10-14 16:23:11 +1100338 if (authenticated && !do_pam_account(pw->pw_name, client_user))
339 authenticated = 0;
340#endif
Damien Millereba71ba2000-04-29 23:57:08 +1000341
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000342 /* Log before sending the reply */
343 auth_log(authctxt, authenticated, get_authname(type), info);
344
Damien Millereba71ba2000-04-29 23:57:08 +1000345 if (client_user != NULL) {
346 xfree(client_user);
347 client_user = NULL;
348 }
349
Damien Miller874d77b2000-10-14 16:23:11 +1100350 if (authenticated)
351 return;
352
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000353 if (authctxt->failures++ > AUTH_FAIL_MAX) {
Damien Millerd2c208a2000-05-17 22:00:02 +1000354#ifdef WITH_AIXAUTHENTICATE
Damien Miller874d77b2000-10-14 16:23:11 +1100355 loginfailed(user,get_canonical_hostname(),"ssh");
Damien Millerd2c208a2000-05-17 22:00:02 +1000356#endif /* WITH_AIXAUTHENTICATE */
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000357 packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
Damien Millerd2c208a2000-05-17 22:00:02 +1000358 }
Damien Millereba71ba2000-04-29 23:57:08 +1000359
Damien Millereba71ba2000-04-29 23:57:08 +1000360 packet_start(SSH_SMSG_FAILURE);
361 packet_send();
362 packet_write_wait();
363 }
364}
365
366/*
367 * Performs authentication of an incoming connection. Session key has already
368 * been exchanged and encryption is enabled.
369 */
370void
371do_authentication()
372{
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000373 Authctxt *authctxt;
374 struct passwd *pw;
Damien Millereba71ba2000-04-29 23:57:08 +1000375 int plen;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000376 u_int ulen;
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000377 char *user, *style = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000378
379 /* Get the name of the user that we wish to log in as. */
380 packet_read_expect(&plen, SSH_CMSG_USER);
381
382 /* Get the user name. */
383 user = packet_get_string(&ulen);
384 packet_integrity_check(plen, (4 + ulen), SSH_CMSG_USER);
385
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000386 if ((style = strchr(user, ':')) != NULL)
387 *style++ = 0;
388
389 authctxt = authctxt_new();
390 authctxt->user = user;
391 authctxt->style = style;
392
Damien Millereba71ba2000-04-29 23:57:08 +1000393 setproctitle("%s", user);
394
395#ifdef AFS
396 /* If machine has AFS, set process authentication group. */
397 if (k_hasafs()) {
398 k_setpag();
399 k_unlog();
400 }
401#endif /* AFS */
402
403 /* Verify that the user is a valid user. */
404 pw = getpwnam(user);
Damien Miller874d77b2000-10-14 16:23:11 +1100405 if (pw && allowed_user(pw)) {
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000406 authctxt->valid = 1;
407 pw = pwcopy(pw);
Damien Miller874d77b2000-10-14 16:23:11 +1100408 } else {
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000409 debug("do_authentication: illegal user %s", user);
Damien Miller874d77b2000-10-14 16:23:11 +1100410 pw = NULL;
411 }
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000412 authctxt->pw = pw;
Damien Millereba71ba2000-04-29 23:57:08 +1000413
414#ifdef USE_PAM
Damien Miller874d77b2000-10-14 16:23:11 +1100415 if (pw)
Damien Miller22e22bf2001-01-19 15:46:38 +1100416 start_pam(user);
Damien Millereba71ba2000-04-29 23:57:08 +1000417#endif
418
419 /*
420 * If we are not running as root, the user must have the same uid as
Damien Miller874d77b2000-10-14 16:23:11 +1100421 * the server. (Unless you are running Windows)
Damien Millereba71ba2000-04-29 23:57:08 +1000422 */
Damien Miller874d77b2000-10-14 16:23:11 +1100423#ifndef HAVE_CYGWIN
424 if (getuid() != 0 && pw && pw->pw_uid != getuid())
Damien Millereba71ba2000-04-29 23:57:08 +1000425 packet_disconnect("Cannot change user when server not running as root.");
Damien Millerbac2d8a2000-09-05 16:13:06 +1100426#endif
Damien Millereba71ba2000-04-29 23:57:08 +1000427
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000428 /*
429 * Loop until the user has been authenticated or the connection is
430 * closed, do_authloop() returns only if authentication is successful
431 */
432 do_authloop(authctxt);
Damien Millereba71ba2000-04-29 23:57:08 +1000433
434 /* The user has been authenticated and accepted. */
Damien Miller874d77b2000-10-14 16:23:11 +1100435 packet_start(SSH_SMSG_SUCCESS);
436 packet_send();
437 packet_write_wait();
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000438
Damien Millereba71ba2000-04-29 23:57:08 +1000439#ifdef WITH_AIXAUTHENTICATE
Damien Millerd2c208a2000-05-17 22:00:02 +1000440 /* We don't have a pty yet, so just label the line as "ssh" */
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000441 if (loginsuccess(authctxt->user,get_canonical_hostname(),"ssh",&aixloginmsg) < 0)
Damien Millerd2c208a2000-05-17 22:00:02 +1000442 aixloginmsg = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000443#endif /* WITH_AIXAUTHENTICATE */
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000444
445 xfree(authctxt->user);
446 xfree(authctxt);
Damien Millereba71ba2000-04-29 23:57:08 +1000447
448 /* Perform session preparation. */
449 do_authenticated(pw);
450}