]> andersk Git - openssh.git/blame - auth1.c
- (djm) Split out and improve OSF SIA auth code. Patch from Chris Adams
[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"
abf1f107 13RCSID("$OpenBSD: auth1.c,v 1.15 2001/02/07 22:35:45 markus 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"
25#include "session.h"
26
27/* import */
28extern ServerOptions options;
29extern char *forced_command;
94ec8c6b 30
31#ifdef WITH_AIXAUTHENTICATE
32extern char *aixloginmsg;
33#endif /* WITH_AIXAUTHENTICATE */
a306f2dd 34
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";
59c97189 51 case SSH_CMSG_AUTH_TIS:
52 case SSH_CMSG_AUTH_TIS_RESPONSE:
53 return "challenge-response";
a306f2dd 54#ifdef KRB4
55 case SSH_CMSG_AUTH_KERBEROS:
56 return "kerberos";
a306f2dd 57#endif
58 }
59 snprintf(buf, sizeof buf, "bad-auth-msg-%d", type);
60 return buf;
61}
62
63/*
59c97189 64 * read packets, try to authenticate the user and
65 * return only if authentication is successful
a306f2dd 66 */
67void
59c97189 68do_authloop(Authctxt *authctxt)
a306f2dd 69{
94ec8c6b 70 int authenticated = 0;
1e3b8b07 71 u_int bits;
a306f2dd 72 RSA *client_host_key;
73 BIGNUM *n;
94ec8c6b 74 char *client_user, *password;
59c97189 75 char info[1024];
1e3b8b07 76 u_int dlen;
a306f2dd 77 int plen, nlen, elen;
1e3b8b07 78 u_int ulen;
a306f2dd 79 int type = 0;
59c97189 80 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
f4ebf0e8 90#ifdef USE_PAM
8d60e965 91 auth_pam_password(pw, "")) {
b7ccb051 92#elif defined(HAVE_OSF_SIA)
93 0) {
59c97189 94#else
95 auth_password(pw, "")) {
96#endif
97 auth_log(authctxt, 1, "without authentication", "");
98 return;
99 }
a306f2dd 100
101 /* Indicate that authentication is needed. */
102 packet_start(SSH_SMSG_FAILURE);
103 packet_send();
104 packet_write_wait();
105
94ec8c6b 106 client_user = NULL;
107
59c97189 108 for (;;) {
94ec8c6b 109 /* default to fail */
110 authenticated = 0;
111
59c97189 112 info[0] = '\0';
a306f2dd 113
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) {
a306f2dd 122 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))
59c97189 129 verbose("Kerberos tgt REFUSED for %.100s", authctxt->user);
a306f2dd 130 xfree(tgt);
131 }
132 continue;
133
134 case SSH_CMSG_HAVE_AFS_TOKEN:
135 if (!options.afs_token_passing || !k_hasafs()) {
a306f2dd 136 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))
59c97189 143 verbose("AFS token REFUSED for %.100s", authctxt->user);
a306f2dd 144 xfree(token_string);
145 }
146 continue;
147#endif /* AFS */
148#ifdef KRB4
149 case SSH_CMSG_AUTH_KERBEROS:
150 if (!options.kerberos_authentication) {
a306f2dd 151 verbose("Kerberos authentication disabled.");
152 break;
153 } else {
154 /* Try Kerberos v4 authentication. */
155 KTEXT_ST auth;
156 char *tkt_user = NULL;
1e3b8b07 157 char *kdata = packet_get_string((u_int *) &auth.length);
a306f2dd 158 packet_integrity_check(plen, 4 + auth.length, type);
159
59c97189 160 if (authctxt->valid) {
161 if (auth.length < MAX_KTXT_LEN)
162 memcpy(auth.dat, kdata, auth.length);
94ec8c6b 163 authenticated = auth_krb4(pw->pw_name, &auth, &tkt_user);
164 if (authenticated) {
59c97189 165 snprintf(info, sizeof info,
166 " tktuser %.100s", tkt_user);
94ec8c6b 167 xfree(tkt_user);
168 }
a306f2dd 169 }
59c97189 170 xfree(kdata);
a306f2dd 171 }
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
94ec8c6b 189 /* Try to authenticate using /etc/hosts.equiv and .rhosts. */
a306f2dd 190 authenticated = auth_rhosts(pw, client_user);
191
59c97189 192 snprintf(info, sizeof info, " ruser %.100s", client_user);
a306f2dd 193 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))
94ec8c6b 220 verbose("Warning: keysize mismatch for client_host_key: "
0b242b12 221 "actual %d, announced %d", BN_num_bits(client_host_key->n), bits);
a306f2dd 222 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
59c97189 227 snprintf(info, sizeof info, " ruser %.100s", client_user);
a306f2dd 228 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);
4d33e531 259#elif defined(HAVE_OSF_SIA)
260 /* Do SIA auth with password */
b7ccb051 261 authenticated = auth_sia_password(authctxt->user,
262 password);
4d33e531 263#else /* !USE_PAM && !HAVE_OSF_SIA */
b4b5d644 264 /* Try authentication with the password. */
a306f2dd 265 authenticated = auth_password(pw, password);
266#endif /* USE_PAM */
267
268 memset(password, 0, strlen(password));
269 xfree(password);
270 break;
271
a306f2dd 272 case SSH_CMSG_AUTH_TIS:
273 debug("rcvd SSH_CMSG_AUTH_TIS");
d464095c 274 if (options.challenge_reponse_authentication == 1) {
59c97189 275 char *challenge = get_challenge(authctxt, authctxt->style);
276 if (challenge != NULL) {
277 debug("sending challenge '%s'", challenge);
a306f2dd 278 packet_start(SSH_SMSG_AUTH_TIS_CHALLENGE);
59c97189 279 packet_put_cstring(challenge);
a306f2dd 280 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");
d464095c 288 if (options.challenge_reponse_authentication == 1) {
a306f2dd 289 char *response = packet_get_string(&dlen);
59c97189 290 debug("got response '%s'", response);
a306f2dd 291 packet_integrity_check(plen, 4 + dlen, type);
59c97189 292 authenticated = verify_response(authctxt, response);
293 memset(response, 'r', dlen);
a306f2dd 294 xfree(response);
295 }
296 break;
a306f2dd 297
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 }
59c97189 306 if (!authctxt->valid && authenticated)
307 fatal("INTERNAL ERROR: authenticated invalid user %s",
308 authctxt->user);
a306f2dd 309
2b87da3b 310#ifdef HAVE_CYGWIN
311 if (authenticated &&
9cd45ea4 312 !check_nt_auth(type == SSH_CMSG_AUTH_PASSWORD,pw->pw_uid)) {
3c62e7eb 313 packet_disconnect("Authentication rejected for uid %d.",
94ec8c6b 314 (int)pw->pw_uid);
3c62e7eb 315 authenticated = 0;
316 }
3c418020 317#else
59c97189 318 /* Special handling for root */
319 if (authenticated && authctxt->pw->pw_uid == 0 && !auth_root_allowed())
320 authenticated = 0;
3c418020 321#endif
2b87da3b 322#ifdef USE_PAM
94ec8c6b 323 if (authenticated && !do_pam_account(pw->pw_name, client_user))
324 authenticated = 0;
325#endif
a306f2dd 326
59c97189 327 /* Log before sending the reply */
328 auth_log(authctxt, authenticated, get_authname(type), info);
329
a306f2dd 330 if (client_user != NULL) {
331 xfree(client_user);
332 client_user = NULL;
333 }
334
94ec8c6b 335 if (authenticated)
336 return;
337
b4b5d644 338 if (authctxt->failures++ > AUTH_FAIL_MAX) {
2b87da3b 339#ifdef WITH_AIXAUTHENTICATE
340 loginfailed(authctxt->user,
341 get_canonical_hostname(options.reverse_mapping_check),
61e96248 342 "ssh");
c1ef8333 343#endif /* WITH_AIXAUTHENTICATE */
59c97189 344 packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
c1ef8333 345 }
a306f2dd 346
a306f2dd 347 packet_start(SSH_SMSG_FAILURE);
348 packet_send();
349 packet_write_wait();
350 }
351}
352
353/*
354 * Performs authentication of an incoming connection. Session key has already
355 * been exchanged and encryption is enabled.
356 */
357void
358do_authentication()
359{
59c97189 360 Authctxt *authctxt;
361 struct passwd *pw;
a306f2dd 362 int plen;
1e3b8b07 363 u_int ulen;
59c97189 364 char *user, *style = NULL;
a306f2dd 365
366 /* Get the name of the user that we wish to log in as. */
367 packet_read_expect(&plen, SSH_CMSG_USER);
368
369 /* Get the user name. */
370 user = packet_get_string(&ulen);
371 packet_integrity_check(plen, (4 + ulen), SSH_CMSG_USER);
372
59c97189 373 if ((style = strchr(user, ':')) != NULL)
374 *style++ = 0;
375
376 authctxt = authctxt_new();
377 authctxt->user = user;
378 authctxt->style = style;
379
a306f2dd 380 setproctitle("%s", user);
381
a306f2dd 382 /* Verify that the user is a valid user. */
383 pw = getpwnam(user);
94ec8c6b 384 if (pw && allowed_user(pw)) {
59c97189 385 authctxt->valid = 1;
386 pw = pwcopy(pw);
94ec8c6b 387 } else {
59c97189 388 debug("do_authentication: illegal user %s", user);
94ec8c6b 389 pw = NULL;
390 }
59c97189 391 authctxt->pw = pw;
a306f2dd 392
393#ifdef USE_PAM
94ec8c6b 394 if (pw)
04fc7a67 395 start_pam(user);
a306f2dd 396#endif
397
398 /*
399 * If we are not running as root, the user must have the same uid as
94ec8c6b 400 * the server. (Unless you are running Windows)
a306f2dd 401 */
94ec8c6b 402#ifndef HAVE_CYGWIN
403 if (getuid() != 0 && pw && pw->pw_uid != getuid())
a306f2dd 404 packet_disconnect("Cannot change user when server not running as root.");
3c62e7eb 405#endif
a306f2dd 406
59c97189 407 /*
b4b5d644 408 * Loop until the user has been authenticated or the connection is
409 * closed, do_authloop() returns only if authentication is successful
410 */
59c97189 411 do_authloop(authctxt);
a306f2dd 412
413 /* The user has been authenticated and accepted. */
94ec8c6b 414 packet_start(SSH_SMSG_SUCCESS);
415 packet_send();
416 packet_write_wait();
59c97189 417
a306f2dd 418#ifdef WITH_AIXAUTHENTICATE
c1ef8333 419 /* We don't have a pty yet, so just label the line as "ssh" */
2b87da3b 420 if (loginsuccess(authctxt->user,
61e96248 421 get_canonical_hostname(options.reverse_mapping_check),
422 "ssh", &aixloginmsg) < 0)
c1ef8333 423 aixloginmsg = NULL;
a306f2dd 424#endif /* WITH_AIXAUTHENTICATE */
59c97189 425
426 xfree(authctxt->user);
427 xfree(authctxt);
a306f2dd 428
429 /* Perform session preparation. */
430 do_authenticated(pw);
431}
This page took 0.138857 seconds and 5 git commands to generate.