]> andersk Git - gssapi-openssh.git/blame - openssh/auth1.c
Import of OpenSSH 3.2.2p1
[gssapi-openssh.git] / openssh / auth1.c
CommitLineData
3c0ef626 1/*
2 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
3 * All rights reserved
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".
10 */
11
12#include "includes.h"
700318f3 13RCSID("$OpenBSD: auth1.c,v 1.40 2002/04/10 08:21:47 markus Exp $");
3c0ef626 14
15#include "xmalloc.h"
16#include "rsa.h"
17#include "ssh1.h"
18#include "packet.h"
19#include "buffer.h"
20#include "mpaux.h"
21#include "log.h"
22#include "servconf.h"
23#include "compat.h"
24#include "auth.h"
e9a17296 25#include "channels.h"
3c0ef626 26#include "session.h"
3c0ef626 27#include "uidswap.h"
700318f3 28#include "monitor_wrap.h"
3c0ef626 29
30/* import */
31extern ServerOptions options;
32
33/*
34 * convert ssh auth msg type into description
35 */
36static char *
37get_authname(int type)
38{
39 static char buf[1024];
40 switch (type) {
41 case SSH_CMSG_AUTH_PASSWORD:
42 return "password";
43 case SSH_CMSG_AUTH_RSA:
44 return "rsa";
45 case SSH_CMSG_AUTH_RHOSTS_RSA:
46 return "rhosts-rsa";
47 case SSH_CMSG_AUTH_RHOSTS:
48 return "rhosts";
49 case SSH_CMSG_AUTH_TIS:
50 case SSH_CMSG_AUTH_TIS_RESPONSE:
51 return "challenge-response";
52#if defined(KRB4) || defined(KRB5)
53 case SSH_CMSG_AUTH_KERBEROS:
54 return "kerberos";
55#endif
56 }
57 snprintf(buf, sizeof buf, "bad-auth-msg-%d", type);
58 return buf;
59}
60
61/*
62 * read packets, try to authenticate the user and
63 * return only if authentication is successful
64 */
65static void
66do_authloop(Authctxt *authctxt)
67{
68 int authenticated = 0;
69 u_int bits;
e9a17296 70 Key *client_host_key;
3c0ef626 71 BIGNUM *n;
72 char *client_user, *password;
73 char info[1024];
74 u_int dlen;
3c0ef626 75 u_int ulen;
76 int type = 0;
77 struct passwd *pw = authctxt->pw;
78
79 debug("Attempting authentication for %s%.100s.",
e9a17296 80 authctxt->valid ? "" : "illegal user ", authctxt->user);
3c0ef626 81
82 /* If the user has no password, accept authentication immediately. */
83 if (options.password_authentication &&
84#if defined(KRB4) || defined(KRB5)
85 (!options.kerberos_authentication || options.kerberos_or_local_passwd) &&
86#endif
700318f3 87 PRIVSEP(auth_password(authctxt, ""))) {
3c0ef626 88 auth_log(authctxt, 1, "without authentication", "");
89 return;
90 }
91
92 /* Indicate that authentication is needed. */
93 packet_start(SSH_SMSG_FAILURE);
94 packet_send();
95 packet_write_wait();
96
97 client_user = NULL;
98
99 for (;;) {
100 /* default to fail */
101 authenticated = 0;
102
103 info[0] = '\0';
104
105 /* Get a packet from the client. */
e9a17296 106 type = packet_read();
3c0ef626 107
108 /* Process the packet. */
109 switch (type) {
110
111#if defined(KRB4) || defined(KRB5)
112 case SSH_CMSG_AUTH_KERBEROS:
113 if (!options.kerberos_authentication) {
114 verbose("Kerberos authentication disabled.");
115 } else {
116 char *kdata = packet_get_string(&dlen);
e9a17296 117 packet_check_eom();
118
3c0ef626 119 if (kdata[0] == 4) { /* KRB_PROT_VERSION */
120#ifdef KRB4
121 KTEXT_ST tkt;
e9a17296 122
3c0ef626 123 tkt.length = dlen;
124 if (tkt.length < MAX_KTXT_LEN)
125 memcpy(tkt.dat, kdata, tkt.length);
e9a17296 126
3c0ef626 127 if (auth_krb4(authctxt, &tkt, &client_user)) {
128 authenticated = 1;
129 snprintf(info, sizeof(info),
130 " tktuser %.100s",
131 client_user);
132 }
133#endif /* KRB4 */
134 } else {
135#ifdef KRB5
136 krb5_data tkt;
137 tkt.length = dlen;
138 tkt.data = kdata;
e9a17296 139
3c0ef626 140 if (auth_krb5(authctxt, &tkt, &client_user)) {
141 authenticated = 1;
142 snprintf(info, sizeof(info),
143 " tktuser %.100s",
144 client_user);
145 }
146#endif /* KRB5 */
147 }
148 xfree(kdata);
149 }
150 break;
151#endif /* KRB4 || KRB5 */
e9a17296 152
3c0ef626 153#if defined(AFS) || defined(KRB5)
154 /* XXX - punt on backward compatibility here. */
155 case SSH_CMSG_HAVE_KERBEROS_TGT:
156 packet_send_debug("Kerberos TGT passing disabled before authentication.");
157 break;
158#ifdef AFS
159 case SSH_CMSG_HAVE_AFS_TOKEN:
160 packet_send_debug("AFS token passing disabled before authentication.");
161 break;
162#endif /* AFS */
163#endif /* AFS || KRB5 */
e9a17296 164
3c0ef626 165 case SSH_CMSG_AUTH_RHOSTS:
166 if (!options.rhosts_authentication) {
167 verbose("Rhosts authentication disabled.");
168 break;
169 }
170 /*
171 * Get client user name. Note that we just have to
172 * trust the client; this is one reason why rhosts
173 * authentication is insecure. (Another is
174 * IP-spoofing on a local network.)
175 */
176 client_user = packet_get_string(&ulen);
e9a17296 177 packet_check_eom();
3c0ef626 178
179 /* Try to authenticate using /etc/hosts.equiv and .rhosts. */
180 authenticated = auth_rhosts(pw, client_user);
181
182 snprintf(info, sizeof info, " ruser %.100s", client_user);
183 break;
184
185 case SSH_CMSG_AUTH_RHOSTS_RSA:
186 if (!options.rhosts_rsa_authentication) {
187 verbose("Rhosts with RSA authentication disabled.");
188 break;
189 }
190 /*
191 * Get client user name. Note that we just have to
192 * trust the client; root on the client machine can
193 * claim to be any user.
194 */
195 client_user = packet_get_string(&ulen);
196
197 /* Get the client host key. */
e9a17296 198 client_host_key = key_new(KEY_RSA1);
3c0ef626 199 bits = packet_get_int();
e9a17296 200 packet_get_bignum(client_host_key->rsa->e);
201 packet_get_bignum(client_host_key->rsa->n);
3c0ef626 202
e9a17296 203 if (bits != BN_num_bits(client_host_key->rsa->n))
3c0ef626 204 verbose("Warning: keysize mismatch for client_host_key: "
e9a17296 205 "actual %d, announced %d",
206 BN_num_bits(client_host_key->rsa->n), bits);
207 packet_check_eom();
3c0ef626 208
e9a17296 209 authenticated = auth_rhosts_rsa(pw, client_user,
210 client_host_key);
211 key_free(client_host_key);
3c0ef626 212
213 snprintf(info, sizeof info, " ruser %.100s", client_user);
214 break;
215
216 case SSH_CMSG_AUTH_RSA:
217 if (!options.rsa_authentication) {
218 verbose("RSA authentication disabled.");
219 break;
220 }
221 /* RSA authentication requested. */
e9a17296 222 if ((n = BN_new()) == NULL)
223 fatal("do_authloop: BN_new failed");
224 packet_get_bignum(n);
225 packet_check_eom();
3c0ef626 226 authenticated = auth_rsa(pw, n);
227 BN_clear_free(n);
228 break;
229
230 case SSH_CMSG_AUTH_PASSWORD:
231 if (!options.password_authentication) {
232 verbose("Password authentication disabled.");
233 break;
234 }
235 /*
236 * Read user password. It is in plain text, but was
237 * transmitted over the encrypted channel so it is
238 * not visible to an outside observer.
239 */
240 password = packet_get_string(&dlen);
e9a17296 241 packet_check_eom();
3c0ef626 242
3c0ef626 243 /* Try authentication with the password. */
700318f3 244 authenticated = PRIVSEP(auth_password(authctxt, password));
3c0ef626 245
246 memset(password, 0, strlen(password));
247 xfree(password);
248 break;
249
250 case SSH_CMSG_AUTH_TIS:
251 debug("rcvd SSH_CMSG_AUTH_TIS");
252 if (options.challenge_response_authentication == 1) {
253 char *challenge = get_challenge(authctxt);
254 if (challenge != NULL) {
255 debug("sending challenge '%s'", challenge);
256 packet_start(SSH_SMSG_AUTH_TIS_CHALLENGE);
257 packet_put_cstring(challenge);
258 xfree(challenge);
259 packet_send();
260 packet_write_wait();
261 continue;
262 }
263 }
264 break;
265 case SSH_CMSG_AUTH_TIS_RESPONSE:
266 debug("rcvd SSH_CMSG_AUTH_TIS_RESPONSE");
267 if (options.challenge_response_authentication == 1) {
268 char *response = packet_get_string(&dlen);
269 debug("got response '%s'", response);
e9a17296 270 packet_check_eom();
3c0ef626 271 authenticated = verify_response(authctxt, response);
272 memset(response, 'r', dlen);
273 xfree(response);
274 }
275 break;
276
277 default:
278 /*
279 * Any unknown messages will be ignored (and failure
280 * returned) during authentication.
281 */
282 log("Unknown message during authentication: type %d", type);
283 break;
284 }
285#ifdef BSD_AUTH
286 if (authctxt->as) {
287 auth_close(authctxt->as);
288 authctxt->as = NULL;
289 }
290#endif
291 if (!authctxt->valid && authenticated)
292 fatal("INTERNAL ERROR: authenticated invalid user %s",
293 authctxt->user);
294
295#ifdef HAVE_CYGWIN
296 if (authenticated &&
e9a17296 297 !check_nt_auth(type == SSH_CMSG_AUTH_PASSWORD, pw)) {
3c0ef626 298 packet_disconnect("Authentication rejected for uid %d.",
e9a17296 299 pw == NULL ? -1 : pw->pw_uid);
3c0ef626 300 authenticated = 0;
301 }
302#else
303 /* Special handling for root */
304 if (authenticated && authctxt->pw->pw_uid == 0 &&
305 !auth_root_allowed(get_authname(type)))
306 authenticated = 0;
307#endif
308#ifdef USE_PAM
700318f3 309 if (!use_privsep && authenticated &&
310 !do_pam_account(pw->pw_name, client_user))
3c0ef626 311 authenticated = 0;
312#endif
313
314 /* Log before sending the reply */
315 auth_log(authctxt, authenticated, get_authname(type), info);
316
317 if (client_user != NULL) {
318 xfree(client_user);
319 client_user = NULL;
320 }
321
322 if (authenticated)
323 return;
324
325 if (authctxt->failures++ > AUTH_FAIL_MAX) {
326#ifdef WITH_AIXAUTHENTICATE
327 loginfailed(authctxt->user,
e9a17296 328 get_canonical_hostname(options.verify_reverse_mapping),
3c0ef626 329 "ssh");
330#endif /* WITH_AIXAUTHENTICATE */
331 packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
332 }
333
334 packet_start(SSH_SMSG_FAILURE);
335 packet_send();
336 packet_write_wait();
337 }
338}
339
340/*
341 * Performs authentication of an incoming connection. Session key has already
342 * been exchanged and encryption is enabled.
343 */
700318f3 344Authctxt *
e9a17296 345do_authentication(void)
3c0ef626 346{
347 Authctxt *authctxt;
3c0ef626 348 u_int ulen;
700318f3 349 char *user, *style = NULL;
3c0ef626 350
351 /* Get the name of the user that we wish to log in as. */
e9a17296 352 packet_read_expect(SSH_CMSG_USER);
3c0ef626 353
354 /* Get the user name. */
355 user = packet_get_string(&ulen);
e9a17296 356 packet_check_eom();
3c0ef626 357
358 if ((style = strchr(user, ':')) != NULL)
359 *style++ = '\0';
360
700318f3 361#ifdef KRB5
3c0ef626 362 /* XXX - SSH.com Kerberos v5 braindeath. */
700318f3 363 if ((datafellows & SSH_BUG_K5USER) &&
364 options.kerberos_authentication) {
365 char *p;
366 if ((p = strchr(user, '@')) != NULL)
367 *p = '\0';
368 }
369#endif
e9a17296 370
3c0ef626 371 authctxt = authctxt_new();
372 authctxt->user = user;
373 authctxt->style = style;
374
375 /* Verify that the user is a valid user. */
700318f3 376 if ((authctxt->pw = PRIVSEP(getpwnamallow(user))) != NULL)
3c0ef626 377 authctxt->valid = 1;
700318f3 378 else
3c0ef626 379 debug("do_authentication: illegal user %s", user);
3c0ef626 380
700318f3 381 setproctitle("%s%s", authctxt->pw ? user : "unknown",
382 use_privsep ? " [net]" : "");
3c0ef626 383
384#ifdef USE_PAM
700318f3 385 PRIVSEP(start_pam(authctxt->pw == NULL ? "NOUSER" : user));
3c0ef626 386#endif
387
388 /*
389 * If we are not running as root, the user must have the same uid as
390 * the server. (Unless you are running Windows)
391 */
392#ifndef HAVE_CYGWIN
700318f3 393 if (!use_privsep && getuid() != 0 && authctxt->pw &&
394 authctxt->pw->pw_uid != getuid())
3c0ef626 395 packet_disconnect("Cannot change user when server not running as root.");
396#endif
397
398 /*
399 * Loop until the user has been authenticated or the connection is
400 * closed, do_authloop() returns only if authentication is successful
401 */
402 do_authloop(authctxt);
403
404 /* The user has been authenticated and accepted. */
405 packet_start(SSH_SMSG_SUCCESS);
406 packet_send();
407 packet_write_wait();
408
700318f3 409 return (authctxt);
3c0ef626 410}
This page took 0.108179 seconds and 5 git commands to generate.