]> andersk Git - openssh.git/blame_incremental - auth1.c
- deraadt@cvs.openbsd.org 2002/07/06 01:00:49
[openssh.git] / auth1.c
... / ...
CommitLineData
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"
13RCSID("$OpenBSD: auth1.c,v 1.41 2002/06/19 00:27:55 deraadt Exp $");
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"
25#include "channels.h"
26#include "session.h"
27#include "uidswap.h"
28#include "monitor_wrap.h"
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;
70 Key *client_host_key;
71 BIGNUM *n;
72 char *client_user, *password;
73 char info[1024];
74 u_int dlen;
75 u_int ulen;
76 int type = 0;
77 struct passwd *pw = authctxt->pw;
78
79 debug("Attempting authentication for %s%.100s.",
80 authctxt->valid ? "" : "illegal user ", authctxt->user);
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
87 PRIVSEP(auth_password(authctxt, ""))) {
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. */
106 type = packet_read();
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);
117 packet_check_eom();
118
119 if (kdata[0] == 4) { /* KRB_PROT_VERSION */
120#ifdef KRB4
121 KTEXT_ST tkt;
122
123 tkt.length = dlen;
124 if (tkt.length < MAX_KTXT_LEN)
125 memcpy(tkt.dat, kdata, tkt.length);
126
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;
139
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 */
152
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 */
164
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);
177 packet_check_eom();
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. */
198 client_host_key = key_new(KEY_RSA1);
199 bits = packet_get_int();
200 packet_get_bignum(client_host_key->rsa->e);
201 packet_get_bignum(client_host_key->rsa->n);
202
203 if (bits != BN_num_bits(client_host_key->rsa->n))
204 verbose("Warning: keysize mismatch for client_host_key: "
205 "actual %d, announced %d",
206 BN_num_bits(client_host_key->rsa->n), bits);
207 packet_check_eom();
208
209 authenticated = auth_rhosts_rsa(pw, client_user,
210 client_host_key);
211 key_free(client_host_key);
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. */
222 if ((n = BN_new()) == NULL)
223 fatal("do_authloop: BN_new failed");
224 packet_get_bignum(n);
225 packet_check_eom();
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);
241 packet_check_eom();
242
243 /* Try authentication with the password. */
244 authenticated = PRIVSEP(auth_password(authctxt, password));
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);
270 packet_check_eom();
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 &&
297 !check_nt_auth(type == SSH_CMSG_AUTH_PASSWORD, pw)) {
298 packet_disconnect("Authentication rejected for uid %d.",
299 pw == NULL ? -1 : pw->pw_uid);
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
309 if (!use_privsep && authenticated &&
310 !do_pam_account(pw->pw_name, client_user))
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 packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
327 }
328
329 packet_start(SSH_SMSG_FAILURE);
330 packet_send();
331 packet_write_wait();
332 }
333}
334
335/*
336 * Performs authentication of an incoming connection. Session key has already
337 * been exchanged and encryption is enabled.
338 */
339Authctxt *
340do_authentication(void)
341{
342 Authctxt *authctxt;
343 u_int ulen;
344 char *user, *style = NULL;
345
346 /* Get the name of the user that we wish to log in as. */
347 packet_read_expect(SSH_CMSG_USER);
348
349 /* Get the user name. */
350 user = packet_get_string(&ulen);
351 packet_check_eom();
352
353 if ((style = strchr(user, ':')) != NULL)
354 *style++ = '\0';
355
356#ifdef KRB5
357 /* XXX - SSH.com Kerberos v5 braindeath. */
358 if ((datafellows & SSH_BUG_K5USER) &&
359 options.kerberos_authentication) {
360 char *p;
361 if ((p = strchr(user, '@')) != NULL)
362 *p = '\0';
363 }
364#endif
365
366 authctxt = authctxt_new();
367 authctxt->user = user;
368 authctxt->style = style;
369
370 /* Verify that the user is a valid user. */
371 if ((authctxt->pw = PRIVSEP(getpwnamallow(user))) != NULL)
372 authctxt->valid = 1;
373 else
374 debug("do_authentication: illegal user %s", user);
375
376 setproctitle("%s%s", authctxt->pw ? user : "unknown",
377 use_privsep ? " [net]" : "");
378
379#ifdef USE_PAM
380 PRIVSEP(start_pam(authctxt->pw == NULL ? "NOUSER" : user));
381#endif
382
383 /*
384 * If we are not running as root, the user must have the same uid as
385 * the server. (Unless you are running Windows)
386 */
387#ifndef HAVE_CYGWIN
388 if (!use_privsep && getuid() != 0 && authctxt->pw &&
389 authctxt->pw->pw_uid != getuid())
390 packet_disconnect("Cannot change user when server not running as root.");
391#endif
392
393 /*
394 * Loop until the user has been authenticated or the connection is
395 * closed, do_authloop() returns only if authentication is successful
396 */
397 do_authloop(authctxt);
398
399 /* The user has been authenticated and accepted. */
400 packet_start(SSH_SMSG_SUCCESS);
401 packet_send();
402 packet_write_wait();
403
404 return (authctxt);
405}
This page took 0.037883 seconds and 5 git commands to generate.