]> andersk Git - openssh.git/blame - auth1.c
Hopefully things did not get mixed around too much. It compiles under
[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"
42f11eb2 13RCSID("$OpenBSD: auth1.c,v 1.13 2001/01/21 19:05:43 markus Exp $");
94ec8c6b 14
15#ifdef HAVE_OSF_SIA
16# include <sia.h>
17# include <siad.h>
18#endif
a306f2dd 19
20#include "xmalloc.h"
21#include "rsa.h"
42f11eb2 22#include "ssh1.h"
a306f2dd 23#include "packet.h"
24#include "buffer.h"
a306f2dd 25#include "mpaux.h"
42f11eb2 26#include "log.h"
a306f2dd 27#include "servconf.h"
28#include "compat.h"
29#include "auth.h"
30#include "session.h"
31
32/* import */
33extern ServerOptions options;
34extern char *forced_command;
94ec8c6b 35
36#ifdef WITH_AIXAUTHENTICATE
37extern char *aixloginmsg;
38#endif /* WITH_AIXAUTHENTICATE */
b3f162ba 39#ifdef HAVE_OSF_SIA
40extern int saved_argc;
41extern char **saved_argv;
42#endif /* HAVE_OSF_SIA */
a306f2dd 43
44/*
45 * convert ssh auth msg type into description
46 */
47char *
48get_authname(int type)
49{
50 static char buf[1024];
51 switch (type) {
52 case SSH_CMSG_AUTH_PASSWORD:
53 return "password";
54 case SSH_CMSG_AUTH_RSA:
55 return "rsa";
56 case SSH_CMSG_AUTH_RHOSTS_RSA:
57 return "rhosts-rsa";
58 case SSH_CMSG_AUTH_RHOSTS:
59 return "rhosts";
59c97189 60 case SSH_CMSG_AUTH_TIS:
61 case SSH_CMSG_AUTH_TIS_RESPONSE:
62 return "challenge-response";
a306f2dd 63#ifdef KRB4
64 case SSH_CMSG_AUTH_KERBEROS:
65 return "kerberos";
a306f2dd 66#endif
67 }
68 snprintf(buf, sizeof buf, "bad-auth-msg-%d", type);
69 return buf;
70}
71
72/*
59c97189 73 * read packets, try to authenticate the user and
74 * return only if authentication is successful
a306f2dd 75 */
76void
59c97189 77do_authloop(Authctxt *authctxt)
a306f2dd 78{
94ec8c6b 79 int authenticated = 0;
1e3b8b07 80 u_int bits;
a306f2dd 81 RSA *client_host_key;
82 BIGNUM *n;
94ec8c6b 83 char *client_user, *password;
59c97189 84 char info[1024];
1e3b8b07 85 u_int dlen;
a306f2dd 86 int plen, nlen, elen;
1e3b8b07 87 u_int ulen;
a306f2dd 88 int type = 0;
59c97189 89 struct passwd *pw = authctxt->pw;
90
91 debug("Attempting authentication for %s%.100s.",
92 authctxt->valid ? "" : "illegal user ", authctxt->user);
93
94 /* If the user has no password, accept authentication immediately. */
95 if (options.password_authentication &&
96#ifdef KRB4
97 (!options.kerberos_authentication || options.kerberos_or_local_passwd) &&
98#endif
f4ebf0e8 99#ifdef USE_PAM
59c97189 100 auth_pam_password(pw, password)) {
101#else
102 auth_password(pw, "")) {
103#endif
104 auth_log(authctxt, 1, "without authentication", "");
105 return;
106 }
a306f2dd 107
108 /* Indicate that authentication is needed. */
109 packet_start(SSH_SMSG_FAILURE);
110 packet_send();
111 packet_write_wait();
112
94ec8c6b 113 client_user = NULL;
114
59c97189 115 for (;;) {
94ec8c6b 116 /* default to fail */
117 authenticated = 0;
118
59c97189 119 info[0] = '\0';
a306f2dd 120
121 /* Get a packet from the client. */
122 type = packet_read(&plen);
123
124 /* Process the packet. */
125 switch (type) {
126#ifdef AFS
127 case SSH_CMSG_HAVE_KERBEROS_TGT:
128 if (!options.kerberos_tgt_passing) {
a306f2dd 129 verbose("Kerberos tgt passing disabled.");
130 break;
131 } else {
132 /* Accept Kerberos tgt. */
133 char *tgt = packet_get_string(&dlen);
134 packet_integrity_check(plen, 4 + dlen, type);
135 if (!auth_kerberos_tgt(pw, tgt))
59c97189 136 verbose("Kerberos tgt REFUSED for %.100s", authctxt->user);
a306f2dd 137 xfree(tgt);
138 }
139 continue;
140
141 case SSH_CMSG_HAVE_AFS_TOKEN:
142 if (!options.afs_token_passing || !k_hasafs()) {
a306f2dd 143 verbose("AFS token passing disabled.");
144 break;
145 } else {
146 /* Accept AFS token. */
147 char *token_string = packet_get_string(&dlen);
148 packet_integrity_check(plen, 4 + dlen, type);
149 if (!auth_afs_token(pw, token_string))
59c97189 150 verbose("AFS token REFUSED for %.100s", authctxt->user);
a306f2dd 151 xfree(token_string);
152 }
153 continue;
154#endif /* AFS */
155#ifdef KRB4
156 case SSH_CMSG_AUTH_KERBEROS:
157 if (!options.kerberos_authentication) {
a306f2dd 158 verbose("Kerberos authentication disabled.");
159 break;
160 } else {
161 /* Try Kerberos v4 authentication. */
162 KTEXT_ST auth;
163 char *tkt_user = NULL;
1e3b8b07 164 char *kdata = packet_get_string((u_int *) &auth.length);
a306f2dd 165 packet_integrity_check(plen, 4 + auth.length, type);
166
59c97189 167 if (authctxt->valid) {
168 if (auth.length < MAX_KTXT_LEN)
169 memcpy(auth.dat, kdata, auth.length);
94ec8c6b 170 authenticated = auth_krb4(pw->pw_name, &auth, &tkt_user);
171 if (authenticated) {
59c97189 172 snprintf(info, sizeof info,
173 " tktuser %.100s", tkt_user);
94ec8c6b 174 xfree(tkt_user);
175 }
a306f2dd 176 }
59c97189 177 xfree(kdata);
a306f2dd 178 }
179 break;
180#endif /* KRB4 */
181
182 case SSH_CMSG_AUTH_RHOSTS:
183 if (!options.rhosts_authentication) {
184 verbose("Rhosts authentication disabled.");
185 break;
186 }
187 /*
188 * Get client user name. Note that we just have to
189 * trust the client; this is one reason why rhosts
190 * authentication is insecure. (Another is
191 * IP-spoofing on a local network.)
192 */
193 client_user = packet_get_string(&ulen);
194 packet_integrity_check(plen, 4 + ulen, type);
195
94ec8c6b 196 /* Try to authenticate using /etc/hosts.equiv and .rhosts. */
a306f2dd 197 authenticated = auth_rhosts(pw, client_user);
198
59c97189 199 snprintf(info, sizeof info, " ruser %.100s", client_user);
a306f2dd 200 break;
201
202 case SSH_CMSG_AUTH_RHOSTS_RSA:
203 if (!options.rhosts_rsa_authentication) {
204 verbose("Rhosts with RSA authentication disabled.");
205 break;
206 }
207 /*
208 * Get client user name. Note that we just have to
209 * trust the client; root on the client machine can
210 * claim to be any user.
211 */
212 client_user = packet_get_string(&ulen);
213
214 /* Get the client host key. */
215 client_host_key = RSA_new();
216 if (client_host_key == NULL)
217 fatal("RSA_new failed");
218 client_host_key->e = BN_new();
219 client_host_key->n = BN_new();
220 if (client_host_key->e == NULL || client_host_key->n == NULL)
221 fatal("BN_new failed");
222 bits = packet_get_int();
223 packet_get_bignum(client_host_key->e, &elen);
224 packet_get_bignum(client_host_key->n, &nlen);
225
226 if (bits != BN_num_bits(client_host_key->n))
94ec8c6b 227 verbose("Warning: keysize mismatch for client_host_key: "
0b242b12 228 "actual %d, announced %d", BN_num_bits(client_host_key->n), bits);
a306f2dd 229 packet_integrity_check(plen, (4 + ulen) + 4 + elen + nlen, type);
230
231 authenticated = auth_rhosts_rsa(pw, client_user, client_host_key);
232 RSA_free(client_host_key);
233
59c97189 234 snprintf(info, sizeof info, " ruser %.100s", client_user);
a306f2dd 235 break;
236
237 case SSH_CMSG_AUTH_RSA:
238 if (!options.rsa_authentication) {
239 verbose("RSA authentication disabled.");
240 break;
241 }
242 /* RSA authentication requested. */
243 n = BN_new();
244 packet_get_bignum(n, &nlen);
245 packet_integrity_check(plen, nlen, type);
246 authenticated = auth_rsa(pw, n);
247 BN_clear_free(n);
248 break;
249
250 case SSH_CMSG_AUTH_PASSWORD:
251 if (!options.password_authentication) {
252 verbose("Password authentication disabled.");
253 break;
254 }
255 /*
256 * Read user password. It is in plain text, but was
257 * transmitted over the encrypted channel so it is
258 * not visible to an outside observer.
259 */
260 password = packet_get_string(&dlen);
261 packet_integrity_check(plen, 4 + dlen, type);
262
263#ifdef USE_PAM
264 /* Do PAM auth with password */
265 authenticated = auth_pam_password(pw, password);
4d33e531 266#elif defined(HAVE_OSF_SIA)
267 /* Do SIA auth with password */
4d33e531 268 if (sia_validate_user(NULL, saved_argc, saved_argv,
269 get_canonical_hostname(), pw->pw_name, NULL, 0,
270 NULL, password) == SIASUCCESS) {
271 authenticated = 1;
272 }
273#else /* !USE_PAM && !HAVE_OSF_SIA */
94ec8c6b 274 /* Try authentication with the password. */
a306f2dd 275 authenticated = auth_password(pw, password);
276#endif /* USE_PAM */
277
278 memset(password, 0, strlen(password));
279 xfree(password);
280 break;
281
a306f2dd 282 case SSH_CMSG_AUTH_TIS:
283 debug("rcvd SSH_CMSG_AUTH_TIS");
284 if (options.skey_authentication == 1) {
59c97189 285 char *challenge = get_challenge(authctxt, authctxt->style);
286 if (challenge != NULL) {
287 debug("sending challenge '%s'", challenge);
a306f2dd 288 packet_start(SSH_SMSG_AUTH_TIS_CHALLENGE);
59c97189 289 packet_put_cstring(challenge);
a306f2dd 290 packet_send();
291 packet_write_wait();
292 continue;
293 }
294 }
295 break;
42f11eb2 296
a306f2dd 297 case SSH_CMSG_AUTH_TIS_RESPONSE:
298 debug("rcvd SSH_CMSG_AUTH_TIS_RESPONSE");
299 if (options.skey_authentication == 1) {
300 char *response = packet_get_string(&dlen);
59c97189 301 debug("got response '%s'", response);
a306f2dd 302 packet_integrity_check(plen, 4 + dlen, type);
59c97189 303 authenticated = verify_response(authctxt, response);
304 memset(response, 'r', dlen);
a306f2dd 305 xfree(response);
306 }
307 break;
a306f2dd 308
309 default:
310 /*
311 * Any unknown messages will be ignored (and failure
312 * returned) during authentication.
313 */
314 log("Unknown message during authentication: type %d", type);
315 break;
316 }
59c97189 317 if (!authctxt->valid && authenticated)
318 fatal("INTERNAL ERROR: authenticated invalid user %s",
319 authctxt->user);
a306f2dd 320
6005a40c 321#ifdef HAVE_CYGWIN
94ec8c6b 322 if (authenticated &&
9cd45ea4 323 !check_nt_auth(type == SSH_CMSG_AUTH_PASSWORD,pw->pw_uid)) {
3c62e7eb 324 packet_disconnect("Authentication rejected for uid %d.",
94ec8c6b 325 (int)pw->pw_uid);
3c62e7eb 326 authenticated = 0;
327 }
3c418020 328#else
59c97189 329 /* Special handling for root */
330 if (authenticated && authctxt->pw->pw_uid == 0 && !auth_root_allowed())
331 authenticated = 0;
3c418020 332#endif
333#ifdef USE_PAM
94ec8c6b 334 if (authenticated && !do_pam_account(pw->pw_name, client_user))
335 authenticated = 0;
336#endif
a306f2dd 337
59c97189 338 /* Log before sending the reply */
339 auth_log(authctxt, authenticated, get_authname(type), info);
340
a306f2dd 341 if (client_user != NULL) {
342 xfree(client_user);
343 client_user = NULL;
344 }
345
94ec8c6b 346 if (authenticated)
347 return;
348
59c97189 349 if (authctxt->failures++ > AUTH_FAIL_MAX) {
c1ef8333 350#ifdef WITH_AIXAUTHENTICATE
94ec8c6b 351 loginfailed(user,get_canonical_hostname(),"ssh");
c1ef8333 352#endif /* WITH_AIXAUTHENTICATE */
59c97189 353 packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
c1ef8333 354 }
a306f2dd 355
a306f2dd 356 packet_start(SSH_SMSG_FAILURE);
357 packet_send();
358 packet_write_wait();
359 }
360}
361
362/*
363 * Performs authentication of an incoming connection. Session key has already
364 * been exchanged and encryption is enabled.
365 */
366void
367do_authentication()
368{
59c97189 369 Authctxt *authctxt;
370 struct passwd *pw;
a306f2dd 371 int plen;
1e3b8b07 372 u_int ulen;
59c97189 373 char *user, *style = NULL;
a306f2dd 374
375 /* Get the name of the user that we wish to log in as. */
376 packet_read_expect(&plen, SSH_CMSG_USER);
377
378 /* Get the user name. */
379 user = packet_get_string(&ulen);
380 packet_integrity_check(plen, (4 + ulen), SSH_CMSG_USER);
381
59c97189 382 if ((style = strchr(user, ':')) != NULL)
383 *style++ = 0;
384
385 authctxt = authctxt_new();
386 authctxt->user = user;
387 authctxt->style = style;
388
a306f2dd 389 setproctitle("%s", user);
390
391#ifdef AFS
392 /* If machine has AFS, set process authentication group. */
393 if (k_hasafs()) {
394 k_setpag();
395 k_unlog();
396 }
397#endif /* AFS */
398
399 /* Verify that the user is a valid user. */
400 pw = getpwnam(user);
94ec8c6b 401 if (pw && allowed_user(pw)) {
59c97189 402 authctxt->valid = 1;
403 pw = pwcopy(pw);
94ec8c6b 404 } else {
59c97189 405 debug("do_authentication: illegal user %s", user);
94ec8c6b 406 pw = NULL;
407 }
59c97189 408 authctxt->pw = pw;
a306f2dd 409
410#ifdef USE_PAM
94ec8c6b 411 if (pw)
04fc7a67 412 start_pam(user);
a306f2dd 413#endif
414
415 /*
416 * If we are not running as root, the user must have the same uid as
94ec8c6b 417 * the server. (Unless you are running Windows)
a306f2dd 418 */
94ec8c6b 419#ifndef HAVE_CYGWIN
420 if (getuid() != 0 && pw && pw->pw_uid != getuid())
a306f2dd 421 packet_disconnect("Cannot change user when server not running as root.");
3c62e7eb 422#endif
a306f2dd 423
59c97189 424 /*
425 * Loop until the user has been authenticated or the connection is
426 * closed, do_authloop() returns only if authentication is successful
427 */
428 do_authloop(authctxt);
a306f2dd 429
430 /* The user has been authenticated and accepted. */
94ec8c6b 431 packet_start(SSH_SMSG_SUCCESS);
432 packet_send();
433 packet_write_wait();
59c97189 434
a306f2dd 435#ifdef WITH_AIXAUTHENTICATE
c1ef8333 436 /* We don't have a pty yet, so just label the line as "ssh" */
59c97189 437 if (loginsuccess(authctxt->user,get_canonical_hostname(),"ssh",&aixloginmsg) < 0)
c1ef8333 438 aixloginmsg = NULL;
a306f2dd 439#endif /* WITH_AIXAUTHENTICATE */
59c97189 440
441 xfree(authctxt->user);
442 xfree(authctxt);
a306f2dd 443
444 /* Perform session preparation. */
445 do_authenticated(pw);
446}
This page took 0.129744 seconds and 5 git commands to generate.