- stevesk@cvs.openbsd.org 2006/07/22 20:48:23
[openssh-git.git] / auth1.c
blob034010fdaa49cd6fe2192e7ea02b2cee3c62dc66
1 /* $OpenBSD: auth1.c,v 1.68 2006/07/22 20:48:22 stevesk Exp $ */
2 /*
3 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4 * All rights reserved
6 * As far as I am concerned, the code I have written for this software
7 * can be used freely for any purpose. Any derived versions of this
8 * software must be clearly marked as such, and if the derived work is
9 * incompatible with the protocol description in the RFC file, it must be
10 * called by a name other than "ssh" or "Secure Shell".
13 #include "includes.h"
15 #include <sys/types.h>
17 #include <string.h>
18 #include <unistd.h>
20 #include "xmalloc.h"
21 #include "rsa.h"
22 #include "ssh1.h"
23 #include "packet.h"
24 #include "buffer.h"
25 #include "log.h"
26 #include "servconf.h"
27 #include "compat.h"
28 #include "auth.h"
29 #include "channels.h"
30 #include "session.h"
31 #include "uidswap.h"
32 #include "monitor_wrap.h"
33 #include "buffer.h"
35 /* import */
36 extern ServerOptions options;
37 extern Buffer loginmsg;
39 static int auth1_process_password(Authctxt *, char *, size_t);
40 static int auth1_process_rsa(Authctxt *, char *, size_t);
41 static int auth1_process_rhosts_rsa(Authctxt *, char *, size_t);
42 static int auth1_process_tis_challenge(Authctxt *, char *, size_t);
43 static int auth1_process_tis_response(Authctxt *, char *, size_t);
45 static char *client_user = NULL; /* Used to fill in remote user for PAM */
47 struct AuthMethod1 {
48 int type;
49 char *name;
50 int *enabled;
51 int (*method)(Authctxt *, char *, size_t);
54 const struct AuthMethod1 auth1_methods[] = {
56 SSH_CMSG_AUTH_PASSWORD, "password",
57 &options.password_authentication, auth1_process_password
60 SSH_CMSG_AUTH_RSA, "rsa",
61 &options.rsa_authentication, auth1_process_rsa
64 SSH_CMSG_AUTH_RHOSTS_RSA, "rhosts-rsa",
65 &options.rhosts_rsa_authentication, auth1_process_rhosts_rsa
68 SSH_CMSG_AUTH_TIS, "challenge-response",
69 &options.challenge_response_authentication,
70 auth1_process_tis_challenge
73 SSH_CMSG_AUTH_TIS_RESPONSE, "challenge-response",
74 &options.challenge_response_authentication,
75 auth1_process_tis_response
77 { -1, NULL, NULL, NULL}
80 static const struct AuthMethod1
81 *lookup_authmethod1(int type)
83 int i;
85 for (i = 0; auth1_methods[i].name != NULL; i++)
86 if (auth1_methods[i].type == type)
87 return (&(auth1_methods[i]));
89 return (NULL);
92 static char *
93 get_authname(int type)
95 const struct AuthMethod1 *a;
96 static char buf[64];
98 if ((a = lookup_authmethod1(type)) != NULL)
99 return (a->name);
100 snprintf(buf, sizeof(buf), "bad-auth-msg-%d", type);
101 return (buf);
104 /*ARGSUSED*/
105 static int
106 auth1_process_password(Authctxt *authctxt, char *info, size_t infolen)
108 int authenticated = 0;
109 char *password;
110 u_int dlen;
113 * Read user password. It is in plain text, but was
114 * transmitted over the encrypted channel so it is
115 * not visible to an outside observer.
117 password = packet_get_string(&dlen);
118 packet_check_eom();
120 /* Try authentication with the password. */
121 authenticated = PRIVSEP(auth_password(authctxt, password));
123 memset(password, 0, dlen);
124 xfree(password);
126 return (authenticated);
129 /*ARGSUSED*/
130 static int
131 auth1_process_rsa(Authctxt *authctxt, char *info, size_t infolen)
133 int authenticated = 0;
134 BIGNUM *n;
136 /* RSA authentication requested. */
137 if ((n = BN_new()) == NULL)
138 fatal("do_authloop: BN_new failed");
139 packet_get_bignum(n);
140 packet_check_eom();
141 authenticated = auth_rsa(authctxt, n);
142 BN_clear_free(n);
144 return (authenticated);
147 /*ARGSUSED*/
148 static int
149 auth1_process_rhosts_rsa(Authctxt *authctxt, char *info, size_t infolen)
151 int keybits, authenticated = 0;
152 u_int bits;
153 Key *client_host_key;
154 u_int ulen;
157 * Get client user name. Note that we just have to
158 * trust the client; root on the client machine can
159 * claim to be any user.
161 client_user = packet_get_string(&ulen);
163 /* Get the client host key. */
164 client_host_key = key_new(KEY_RSA1);
165 bits = packet_get_int();
166 packet_get_bignum(client_host_key->rsa->e);
167 packet_get_bignum(client_host_key->rsa->n);
169 keybits = BN_num_bits(client_host_key->rsa->n);
170 if (keybits < 0 || bits != (u_int)keybits) {
171 verbose("Warning: keysize mismatch for client_host_key: "
172 "actual %d, announced %d",
173 BN_num_bits(client_host_key->rsa->n), bits);
175 packet_check_eom();
177 authenticated = auth_rhosts_rsa(authctxt, client_user,
178 client_host_key);
179 key_free(client_host_key);
181 snprintf(info, infolen, " ruser %.100s", client_user);
183 return (authenticated);
186 /*ARGSUSED*/
187 static int
188 auth1_process_tis_challenge(Authctxt *authctxt, char *info, size_t infolen)
190 char *challenge;
192 if ((challenge = get_challenge(authctxt)) == NULL)
193 return (0);
195 debug("sending challenge '%s'", challenge);
196 packet_start(SSH_SMSG_AUTH_TIS_CHALLENGE);
197 packet_put_cstring(challenge);
198 xfree(challenge);
199 packet_send();
200 packet_write_wait();
202 return (-1);
205 /*ARGSUSED*/
206 static int
207 auth1_process_tis_response(Authctxt *authctxt, char *info, size_t infolen)
209 int authenticated = 0;
210 char *response;
211 u_int dlen;
213 response = packet_get_string(&dlen);
214 packet_check_eom();
215 authenticated = verify_response(authctxt, response);
216 memset(response, 'r', dlen);
217 xfree(response);
219 return (authenticated);
223 * read packets, try to authenticate the user and
224 * return only if authentication is successful
226 static void
227 do_authloop(Authctxt *authctxt)
229 int authenticated = 0;
230 char info[1024];
231 int prev = 0, type = 0;
232 const struct AuthMethod1 *meth;
234 debug("Attempting authentication for %s%.100s.",
235 authctxt->valid ? "" : "invalid user ", authctxt->user);
237 /* If the user has no password, accept authentication immediately. */
238 if (options.password_authentication &&
239 #ifdef KRB5
240 (!options.kerberos_authentication || options.kerberos_or_local_passwd) &&
241 #endif
242 PRIVSEP(auth_password(authctxt, ""))) {
243 #ifdef USE_PAM
244 if (options.use_pam && (PRIVSEP(do_pam_account())))
245 #endif
247 auth_log(authctxt, 1, "without authentication", "");
248 return;
252 /* Indicate that authentication is needed. */
253 packet_start(SSH_SMSG_FAILURE);
254 packet_send();
255 packet_write_wait();
257 for (;;) {
258 /* default to fail */
259 authenticated = 0;
261 info[0] = '\0';
263 /* Get a packet from the client. */
264 prev = type;
265 type = packet_read();
268 * If we started challenge-response authentication but the
269 * next packet is not a response to our challenge, release
270 * the resources allocated by get_challenge() (which would
271 * normally have been released by verify_response() had we
272 * received such a response)
274 if (prev == SSH_CMSG_AUTH_TIS &&
275 type != SSH_CMSG_AUTH_TIS_RESPONSE)
276 abandon_challenge_response(authctxt);
278 if ((meth = lookup_authmethod1(type)) == NULL) {
279 logit("Unknown message during authentication: "
280 "type %d", type);
281 goto skip;
284 if (!*(meth->enabled)) {
285 verbose("%s authentication disabled.", meth->name);
286 goto skip;
289 authenticated = meth->method(authctxt, info, sizeof(info));
290 if (authenticated == -1)
291 continue; /* "postponed" */
293 #ifdef BSD_AUTH
294 if (authctxt->as) {
295 auth_close(authctxt->as);
296 authctxt->as = NULL;
298 #endif
299 if (!authctxt->valid && authenticated)
300 fatal("INTERNAL ERROR: authenticated invalid user %s",
301 authctxt->user);
303 #ifdef _UNICOS
304 if (authenticated && cray_access_denied(authctxt->user)) {
305 authenticated = 0;
306 fatal("Access denied for user %s.",authctxt->user);
308 #endif /* _UNICOS */
310 #ifdef HAVE_CYGWIN
311 if (authenticated &&
312 !check_nt_auth(type == SSH_CMSG_AUTH_PASSWORD,
313 authctxt->pw)) {
314 packet_disconnect("Authentication rejected for uid %d.",
315 authctxt->pw == NULL ? -1 : authctxt->pw->pw_uid);
316 authenticated = 0;
318 #else
319 /* Special handling for root */
320 if (authenticated && authctxt->pw->pw_uid == 0 &&
321 !auth_root_allowed(meth->name)) {
322 authenticated = 0;
323 # ifdef SSH_AUDIT_EVENTS
324 PRIVSEP(audit_event(SSH_LOGIN_ROOT_DENIED));
325 # endif
327 #endif
329 #ifdef USE_PAM
330 if (options.use_pam && authenticated &&
331 !PRIVSEP(do_pam_account())) {
332 char *msg;
333 size_t len;
335 error("Access denied for user %s by PAM account "
336 "configuration", authctxt->user);
337 len = buffer_len(&loginmsg);
338 buffer_append(&loginmsg, "\0", 1);
339 msg = buffer_ptr(&loginmsg);
340 /* strip trailing newlines */
341 if (len > 0)
342 while (len > 0 && msg[--len] == '\n')
343 msg[len] = '\0';
344 else
345 msg = "Access denied.";
346 packet_disconnect(msg);
348 #endif
350 skip:
351 /* Log before sending the reply */
352 auth_log(authctxt, authenticated, get_authname(type), info);
354 if (client_user != NULL) {
355 xfree(client_user);
356 client_user = NULL;
359 if (authenticated)
360 return;
362 if (authctxt->failures++ > options.max_authtries) {
363 #ifdef SSH_AUDIT_EVENTS
364 PRIVSEP(audit_event(SSH_LOGIN_EXCEED_MAXTRIES));
365 #endif
366 packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
369 packet_start(SSH_SMSG_FAILURE);
370 packet_send();
371 packet_write_wait();
376 * Performs authentication of an incoming connection. Session key has already
377 * been exchanged and encryption is enabled.
379 void
380 do_authentication(Authctxt *authctxt)
382 u_int ulen;
383 char *user, *style = NULL;
385 /* Get the name of the user that we wish to log in as. */
386 packet_read_expect(SSH_CMSG_USER);
388 /* Get the user name. */
389 user = packet_get_string(&ulen);
390 packet_check_eom();
392 if ((style = strchr(user, ':')) != NULL)
393 *style++ = '\0';
395 authctxt->user = user;
396 authctxt->style = style;
398 /* Verify that the user is a valid user. */
399 if ((authctxt->pw = PRIVSEP(getpwnamallow(user))) != NULL)
400 authctxt->valid = 1;
401 else {
402 debug("do_authentication: invalid user %s", user);
403 authctxt->pw = fakepw();
406 setproctitle("%s%s", authctxt->valid ? user : "unknown",
407 use_privsep ? " [net]" : "");
409 #ifdef USE_PAM
410 if (options.use_pam)
411 PRIVSEP(start_pam(authctxt));
412 #endif
415 * If we are not running as root, the user must have the same uid as
416 * the server.
418 #ifndef HAVE_CYGWIN
419 if (!use_privsep && getuid() != 0 && authctxt->pw &&
420 authctxt->pw->pw_uid != getuid())
421 packet_disconnect("Cannot change user when server not running as root.");
422 #endif
425 * Loop until the user has been authenticated or the connection is
426 * closed, do_authloop() returns only if authentication is successful
428 do_authloop(authctxt);
430 /* The user has been authenticated and accepted. */
431 packet_start(SSH_SMSG_SUCCESS);
432 packet_send();
433 packet_write_wait();