blob: 0d440e5214407894133c0fce02b65e9b09f7fcd3 [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
4 */
5
6#include "includes.h"
Damien Millerbd483e72000-04-30 10:00:53 +10007RCSID("$OpenBSD: auth1.c,v 1.2 2000/04/29 18:11:52 markus Exp $");
Damien Millereba71ba2000-04-29 23:57:08 +10008
9#include "xmalloc.h"
10#include "rsa.h"
11#include "ssh.h"
12#include "packet.h"
13#include "buffer.h"
14#include "cipher.h"
15#include "mpaux.h"
16#include "servconf.h"
17#include "compat.h"
18#include "auth.h"
19#include "session.h"
20
Damien Millerb8c656e2000-06-28 15:22:41 +100021#ifdef HAVE_OSF_SIA
22# include <sia.h>
23# include <siad.h>
24#endif
25
Damien Millereba71ba2000-04-29 23:57:08 +100026/* import */
27extern ServerOptions options;
28extern char *forced_command;
29
30/*
31 * convert ssh auth msg type into description
32 */
33char *
34get_authname(int type)
35{
36 static char buf[1024];
37 switch (type) {
38 case SSH_CMSG_AUTH_PASSWORD:
39 return "password";
40 case SSH_CMSG_AUTH_RSA:
41 return "rsa";
42 case SSH_CMSG_AUTH_RHOSTS_RSA:
43 return "rhosts-rsa";
44 case SSH_CMSG_AUTH_RHOSTS:
45 return "rhosts";
46#ifdef KRB4
47 case SSH_CMSG_AUTH_KERBEROS:
48 return "kerberos";
49#endif
50#ifdef SKEY
51 case SSH_CMSG_AUTH_TIS_RESPONSE:
52 return "s/key";
53#endif
54 }
55 snprintf(buf, sizeof buf, "bad-auth-msg-%d", type);
56 return buf;
57}
58
59/*
60 * The user does not exist or access is denied,
61 * but fake indication that authentication is needed.
62 */
63void
64do_fake_authloop1(char *user)
65{
66 int attempt = 0;
67
68 log("Faking authloop for illegal user %.200s from %.200s port %d",
69 user,
70 get_remote_ipaddr(),
71 get_remote_port());
72
73#ifdef WITH_AIXAUTHENTICATE
Damien Millerd2c208a2000-05-17 22:00:02 +100074 loginfailed(user,get_canonical_hostname(),"ssh");
Damien Millereba71ba2000-04-29 23:57:08 +100075#endif /* WITH_AIXAUTHENTICATE */
76
77 /* Indicate that authentication is needed. */
78 packet_start(SSH_SMSG_FAILURE);
79 packet_send();
80 packet_write_wait();
81
82 /*
83 * Keep reading packets, and always respond with a failure. This is
84 * to avoid disclosing whether such a user really exists.
85 */
86 for (attempt = 1;; attempt++) {
87 /* Read a packet. This will not return if the client disconnects. */
88 int plen;
89#ifndef SKEY
90 (void)packet_read(&plen);
91#else /* SKEY */
92 int type = packet_read(&plen);
93 unsigned int dlen;
94 char *password, *skeyinfo;
95 password = NULL;
96 /* Try to send a fake s/key challenge. */
97 if (options.skey_authentication == 1 &&
98 (skeyinfo = skey_fake_keyinfo(user)) != NULL) {
99 if (type == SSH_CMSG_AUTH_TIS) {
100 packet_start(SSH_SMSG_AUTH_TIS_CHALLENGE);
101 packet_put_string(skeyinfo, strlen(skeyinfo));
102 packet_send();
103 packet_write_wait();
104 continue;
105 } else if (type == SSH_CMSG_AUTH_PASSWORD &&
106 options.password_authentication &&
107 (password = packet_get_string(&dlen)) != NULL &&
108 dlen == 5 &&
109 strncasecmp(password, "s/key", 5) == 0 ) {
110 packet_send_debug(skeyinfo);
111 }
112 }
113 if (password != NULL)
114 xfree(password);
115#endif
116 if (attempt > AUTH_FAIL_MAX)
117 packet_disconnect(AUTH_FAIL_MSG, user);
118
119 /*
120 * Send failure. This should be indistinguishable from a
121 * failed authentication.
122 */
123 packet_start(SSH_SMSG_FAILURE);
124 packet_send();
125 packet_write_wait();
126 }
127 /* NOTREACHED */
128 abort();
129}
130
131/*
132 * read packets and try to authenticate local user *pw.
133 * return if authentication is successfull
134 */
135void
136do_authloop(struct passwd * pw)
137{
138 int attempt = 0;
139 unsigned int bits;
140 RSA *client_host_key;
141 BIGNUM *n;
142 char *client_user = NULL, *password = NULL;
143 char user[1024];
144 unsigned int dlen;
145 int plen, nlen, elen;
146 unsigned int ulen;
147 int type = 0;
148 void (*authlog) (const char *fmt,...) = verbose;
Damien Millerb8c656e2000-06-28 15:22:41 +1000149#ifdef HAVE_OSF_SIA
150 extern int saved_argc;
151 extern char **saved_argv;
152#endif /* HAVE_OSF_SIA */
Damien Millereba71ba2000-04-29 23:57:08 +1000153
154 /* Indicate that authentication is needed. */
155 packet_start(SSH_SMSG_FAILURE);
156 packet_send();
157 packet_write_wait();
158
159 for (attempt = 1;; attempt++) {
160 int authenticated = 0;
161 strlcpy(user, "", sizeof user);
162
163 /* Get a packet from the client. */
164 type = packet_read(&plen);
165
166 /* Process the packet. */
167 switch (type) {
168#ifdef AFS
169 case SSH_CMSG_HAVE_KERBEROS_TGT:
170 if (!options.kerberos_tgt_passing) {
171 /* packet_get_all(); */
172 verbose("Kerberos tgt passing disabled.");
173 break;
174 } else {
175 /* Accept Kerberos tgt. */
176 char *tgt = packet_get_string(&dlen);
177 packet_integrity_check(plen, 4 + dlen, type);
178 if (!auth_kerberos_tgt(pw, tgt))
179 verbose("Kerberos tgt REFUSED for %s", pw->pw_name);
180 xfree(tgt);
181 }
182 continue;
183
184 case SSH_CMSG_HAVE_AFS_TOKEN:
185 if (!options.afs_token_passing || !k_hasafs()) {
186 /* packet_get_all(); */
187 verbose("AFS token passing disabled.");
188 break;
189 } else {
190 /* Accept AFS token. */
191 char *token_string = packet_get_string(&dlen);
192 packet_integrity_check(plen, 4 + dlen, type);
193 if (!auth_afs_token(pw, token_string))
194 verbose("AFS token REFUSED for %s", pw->pw_name);
195 xfree(token_string);
196 }
197 continue;
198#endif /* AFS */
199#ifdef KRB4
200 case SSH_CMSG_AUTH_KERBEROS:
201 if (!options.kerberos_authentication) {
202 /* packet_get_all(); */
203 verbose("Kerberos authentication disabled.");
204 break;
205 } else {
206 /* Try Kerberos v4 authentication. */
207 KTEXT_ST auth;
208 char *tkt_user = NULL;
209 char *kdata = packet_get_string((unsigned int *) &auth.length);
210 packet_integrity_check(plen, 4 + auth.length, type);
211
212 if (auth.length < MAX_KTXT_LEN)
213 memcpy(auth.dat, kdata, auth.length);
214 xfree(kdata);
215
216 authenticated = auth_krb4(pw->pw_name, &auth, &tkt_user);
217
218 if (authenticated) {
219 snprintf(user, sizeof user, " tktuser %s", tkt_user);
220 xfree(tkt_user);
221 }
222 }
223 break;
224#endif /* KRB4 */
225
226 case SSH_CMSG_AUTH_RHOSTS:
227 if (!options.rhosts_authentication) {
228 verbose("Rhosts authentication disabled.");
229 break;
230 }
231 /*
232 * Get client user name. Note that we just have to
233 * trust the client; this is one reason why rhosts
234 * authentication is insecure. (Another is
235 * IP-spoofing on a local network.)
236 */
237 client_user = packet_get_string(&ulen);
238 packet_integrity_check(plen, 4 + ulen, type);
239
240 /* Try to authenticate using /etc/hosts.equiv and
241 .rhosts. */
242 authenticated = auth_rhosts(pw, client_user);
243
244 snprintf(user, sizeof user, " ruser %s", client_user);
245 break;
246
247 case SSH_CMSG_AUTH_RHOSTS_RSA:
248 if (!options.rhosts_rsa_authentication) {
249 verbose("Rhosts with RSA authentication disabled.");
250 break;
251 }
252 /*
253 * Get client user name. Note that we just have to
254 * trust the client; root on the client machine can
255 * claim to be any user.
256 */
257 client_user = packet_get_string(&ulen);
258
259 /* Get the client host key. */
260 client_host_key = RSA_new();
261 if (client_host_key == NULL)
262 fatal("RSA_new failed");
263 client_host_key->e = BN_new();
264 client_host_key->n = BN_new();
265 if (client_host_key->e == NULL || client_host_key->n == NULL)
266 fatal("BN_new failed");
267 bits = packet_get_int();
268 packet_get_bignum(client_host_key->e, &elen);
269 packet_get_bignum(client_host_key->n, &nlen);
270
271 if (bits != BN_num_bits(client_host_key->n))
Damien Millerbd483e72000-04-30 10:00:53 +1000272 log("Warning: keysize mismatch for client_host_key: "
273 "actual %d, announced %d", BN_num_bits(client_host_key->n), bits);
Damien Millereba71ba2000-04-29 23:57:08 +1000274 packet_integrity_check(plen, (4 + ulen) + 4 + elen + nlen, type);
275
276 authenticated = auth_rhosts_rsa(pw, client_user, client_host_key);
277 RSA_free(client_host_key);
278
279 snprintf(user, sizeof user, " ruser %s", client_user);
280 break;
281
282 case SSH_CMSG_AUTH_RSA:
283 if (!options.rsa_authentication) {
284 verbose("RSA authentication disabled.");
285 break;
286 }
287 /* RSA authentication requested. */
288 n = BN_new();
289 packet_get_bignum(n, &nlen);
290 packet_integrity_check(plen, nlen, type);
291 authenticated = auth_rsa(pw, n);
292 BN_clear_free(n);
293 break;
294
295 case SSH_CMSG_AUTH_PASSWORD:
296 if (!options.password_authentication) {
297 verbose("Password authentication disabled.");
298 break;
299 }
300 /*
301 * Read user password. It is in plain text, but was
302 * transmitted over the encrypted channel so it is
303 * not visible to an outside observer.
304 */
305 password = packet_get_string(&dlen);
306 packet_integrity_check(plen, 4 + dlen, type);
307
308#ifdef USE_PAM
309 /* Do PAM auth with password */
310 authenticated = auth_pam_password(pw, password);
Damien Millerb8c656e2000-06-28 15:22:41 +1000311#elif defined(HAVE_OSF_SIA)
312 /* Do SIA auth with password */
313 host = get_canonical_hostname();
314 if (sia_validate_user(NULL, saved_argc, saved_argv,
315 get_canonical_hostname(), pw->pw_name, NULL, 0,
316 NULL, password) == SIASUCCESS) {
317 authenticated = 1;
318 }
319#else /* !USE_PAM && !HAVE_OSF_SIA */
Damien Millereba71ba2000-04-29 23:57:08 +1000320 /* Try authentication with the password. */
321 authenticated = auth_password(pw, password);
322#endif /* USE_PAM */
323
324 memset(password, 0, strlen(password));
325 xfree(password);
326 break;
327
328#ifdef SKEY
329 case SSH_CMSG_AUTH_TIS:
330 debug("rcvd SSH_CMSG_AUTH_TIS");
331 if (options.skey_authentication == 1) {
332 char *skeyinfo = skey_keyinfo(pw->pw_name);
333 if (skeyinfo == NULL) {
334 debug("generating fake skeyinfo for %.100s.", pw->pw_name);
335 skeyinfo = skey_fake_keyinfo(pw->pw_name);
336 }
337 if (skeyinfo != NULL) {
338 /* we send our s/key- in tis-challenge messages */
339 debug("sending challenge '%s'", skeyinfo);
340 packet_start(SSH_SMSG_AUTH_TIS_CHALLENGE);
341 packet_put_string(skeyinfo, strlen(skeyinfo));
342 packet_send();
343 packet_write_wait();
344 continue;
345 }
346 }
347 break;
348 case SSH_CMSG_AUTH_TIS_RESPONSE:
349 debug("rcvd SSH_CMSG_AUTH_TIS_RESPONSE");
350 if (options.skey_authentication == 1) {
351 char *response = packet_get_string(&dlen);
352 debug("skey response == '%s'", response);
353 packet_integrity_check(plen, 4 + dlen, type);
354 authenticated = (skey_haskey(pw->pw_name) == 0 &&
355 skey_passcheck(pw->pw_name, response) != -1);
356 xfree(response);
357 }
358 break;
359#else
360 case SSH_CMSG_AUTH_TIS:
361 /* TIS Authentication is unsupported */
362 log("TIS authentication unsupported.");
363 break;
364#endif
365
366 default:
367 /*
368 * Any unknown messages will be ignored (and failure
369 * returned) during authentication.
370 */
371 log("Unknown message during authentication: type %d", type);
372 break;
373 }
374
375 /*
376 * Check if the user is logging in as root and root logins
377 * are disallowed.
378 * Note that root login is allowed for forced commands.
379 */
380 if (authenticated && pw->pw_uid == 0 && !options.permit_root_login) {
381 if (forced_command) {
382 log("Root login accepted for forced command.");
383 } else {
384 authenticated = 0;
385 log("ROOT LOGIN REFUSED FROM %.200s",
386 get_canonical_hostname());
387 }
388 }
389
390 /* Raise logging level */
391 if (authenticated ||
392 attempt == AUTH_FAIL_LOG ||
393 type == SSH_CMSG_AUTH_PASSWORD)
394 authlog = log;
395
396 authlog("%s %s for %.200s from %.200s port %d%s",
397 authenticated ? "Accepted" : "Failed",
398 get_authname(type),
399 pw->pw_uid == 0 ? "ROOT" : pw->pw_name,
400 get_remote_ipaddr(),
401 get_remote_port(),
402 user);
403
404#ifdef USE_PAM
405 if (authenticated) {
406 if (!do_pam_account(pw->pw_name, client_user)) {
407 if (client_user != NULL) {
408 xfree(client_user);
409 client_user = NULL;
410 }
411 do_fake_authloop1(pw->pw_name);
412 }
413 return;
414 }
415#else /* USE_PAM */
416 if (authenticated) {
417 return;
418 }
419#endif /* USE_PAM */
420
421 if (client_user != NULL) {
422 xfree(client_user);
423 client_user = NULL;
424 }
425
Damien Millerd2c208a2000-05-17 22:00:02 +1000426 if (attempt > AUTH_FAIL_MAX) {
427#ifdef WITH_AIXAUTHENTICATE
428 loginfailed(pw->pw_name,get_canonical_hostname(),"ssh");
429#endif /* WITH_AIXAUTHENTICATE */
Damien Millereba71ba2000-04-29 23:57:08 +1000430 packet_disconnect(AUTH_FAIL_MSG, pw->pw_name);
Damien Millerd2c208a2000-05-17 22:00:02 +1000431 }
Damien Millereba71ba2000-04-29 23:57:08 +1000432
433 /* Send a message indicating that the authentication attempt failed. */
434 packet_start(SSH_SMSG_FAILURE);
435 packet_send();
436 packet_write_wait();
437 }
438}
439
440/*
441 * Performs authentication of an incoming connection. Session key has already
442 * been exchanged and encryption is enabled.
443 */
444void
445do_authentication()
446{
447 struct passwd *pw, pwcopy;
448 int plen;
449 unsigned int ulen;
450 char *user;
451#ifdef WITH_AIXAUTHENTICATE
Damien Millerd2c208a2000-05-17 22:00:02 +1000452 extern char *aixloginmsg;
Damien Millereba71ba2000-04-29 23:57:08 +1000453#endif /* WITH_AIXAUTHENTICATE */
454
455 /* Get the name of the user that we wish to log in as. */
456 packet_read_expect(&plen, SSH_CMSG_USER);
457
458 /* Get the user name. */
459 user = packet_get_string(&ulen);
460 packet_integrity_check(plen, (4 + ulen), SSH_CMSG_USER);
461
462 setproctitle("%s", user);
463
464#ifdef AFS
465 /* If machine has AFS, set process authentication group. */
466 if (k_hasafs()) {
467 k_setpag();
468 k_unlog();
469 }
470#endif /* AFS */
471
472 /* Verify that the user is a valid user. */
473 pw = getpwnam(user);
474 if (!pw || !allowed_user(pw))
475 do_fake_authloop1(user);
476 xfree(user);
477
478 /* Take a copy of the returned structure. */
479 memset(&pwcopy, 0, sizeof(pwcopy));
480 pwcopy.pw_name = xstrdup(pw->pw_name);
481 pwcopy.pw_passwd = xstrdup(pw->pw_passwd);
482 pwcopy.pw_uid = pw->pw_uid;
483 pwcopy.pw_gid = pw->pw_gid;
484 pwcopy.pw_dir = xstrdup(pw->pw_dir);
485 pwcopy.pw_shell = xstrdup(pw->pw_shell);
486 pw = &pwcopy;
487
488#ifdef USE_PAM
489 start_pam(pw);
490#endif
491
492 /*
493 * If we are not running as root, the user must have the same uid as
494 * the server.
495 */
496 if (getuid() != 0 && pw->pw_uid != getuid())
497 packet_disconnect("Cannot change user when server not running as root.");
498
499 debug("Attempting authentication for %.100s.", pw->pw_name);
500
501 /* If the user has no password, accept authentication immediately. */
502 if (options.password_authentication &&
503#ifdef KRB4
504 (!options.kerberos_authentication || options.kerberos_or_local_passwd) &&
505#endif /* KRB4 */
506#ifdef USE_PAM
507 auth_pam_password(pw, "")) {
508#else /* USE_PAM */
509 auth_password(pw, "")) {
510#endif /* USE_PAM */
511 /* Authentication with empty password succeeded. */
512 log("Login for user %s from %.100s, accepted without authentication.",
513 pw->pw_name, get_remote_ipaddr());
514 } else {
515 /* Loop until the user has been authenticated or the
516 connection is closed, do_authloop() returns only if
517 authentication is successfull */
518 do_authloop(pw);
519 }
520
521 /* The user has been authenticated and accepted. */
522#ifdef WITH_AIXAUTHENTICATE
Damien Millerd2c208a2000-05-17 22:00:02 +1000523 /* We don't have a pty yet, so just label the line as "ssh" */
524 if (loginsuccess(user,get_canonical_hostname(),"ssh",&aixloginmsg) < 0)
525 aixloginmsg = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000526#endif /* WITH_AIXAUTHENTICATE */
527 packet_start(SSH_SMSG_SUCCESS);
528 packet_send();
529 packet_write_wait();
530
531 /* Perform session preparation. */
532 do_authenticated(pw);
533}