- (djm) Release 5.7p1
[openssh-git.git] / auth1.c
blobcc85aec740328a21c2dab170df6d091a35445157
1 /* $OpenBSD: auth1.c,v 1.75 2010/08/31 09:58:37 djm 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 <stdarg.h>
18 #include <stdio.h>
19 #include <string.h>
20 #include <unistd.h>
21 #include <pwd.h>
23 #include "openbsd-compat/sys-queue.h"
24 #include "xmalloc.h"
25 #include "rsa.h"
26 #include "ssh1.h"
27 #include "packet.h"
28 #include "buffer.h"
29 #include "log.h"
30 #include "servconf.h"
31 #include "compat.h"
32 #include "key.h"
33 #include "hostfile.h"
34 #include "auth.h"
35 #include "channels.h"
36 #include "session.h"
37 #include "uidswap.h"
38 #ifdef GSSAPI
39 #include "ssh-gss.h"
40 #endif
41 #include "monitor_wrap.h"
42 #include "buffer.h"
44 /* import */
45 extern ServerOptions options;
46 extern Buffer loginmsg;
48 static int auth1_process_password(Authctxt *, char *, size_t);
49 static int auth1_process_rsa(Authctxt *, char *, size_t);
50 static int auth1_process_rhosts_rsa(Authctxt *, char *, size_t);
51 static int auth1_process_tis_challenge(Authctxt *, char *, size_t);
52 static int auth1_process_tis_response(Authctxt *, char *, size_t);
54 static char *client_user = NULL; /* Used to fill in remote user for PAM */
56 struct AuthMethod1 {
57 int type;
58 char *name;
59 int *enabled;
60 int (*method)(Authctxt *, char *, size_t);
63 const struct AuthMethod1 auth1_methods[] = {
65 SSH_CMSG_AUTH_PASSWORD, "password",
66 &options.password_authentication, auth1_process_password
69 SSH_CMSG_AUTH_RSA, "rsa",
70 &options.rsa_authentication, auth1_process_rsa
73 SSH_CMSG_AUTH_RHOSTS_RSA, "rhosts-rsa",
74 &options.rhosts_rsa_authentication, auth1_process_rhosts_rsa
77 SSH_CMSG_AUTH_TIS, "challenge-response",
78 &options.challenge_response_authentication,
79 auth1_process_tis_challenge
82 SSH_CMSG_AUTH_TIS_RESPONSE, "challenge-response",
83 &options.challenge_response_authentication,
84 auth1_process_tis_response
86 { -1, NULL, NULL, NULL}
89 static const struct AuthMethod1
90 *lookup_authmethod1(int type)
92 int i;
94 for (i = 0; auth1_methods[i].name != NULL; i++)
95 if (auth1_methods[i].type == type)
96 return (&(auth1_methods[i]));
98 return (NULL);
101 static char *
102 get_authname(int type)
104 const struct AuthMethod1 *a;
105 static char buf[64];
107 if ((a = lookup_authmethod1(type)) != NULL)
108 return (a->name);
109 snprintf(buf, sizeof(buf), "bad-auth-msg-%d", type);
110 return (buf);
113 /*ARGSUSED*/
114 static int
115 auth1_process_password(Authctxt *authctxt, char *info, size_t infolen)
117 int authenticated = 0;
118 char *password;
119 u_int dlen;
122 * Read user password. It is in plain text, but was
123 * transmitted over the encrypted channel so it is
124 * not visible to an outside observer.
126 password = packet_get_string(&dlen);
127 packet_check_eom();
129 /* Try authentication with the password. */
130 authenticated = PRIVSEP(auth_password(authctxt, password));
132 memset(password, 0, dlen);
133 xfree(password);
135 return (authenticated);
138 /*ARGSUSED*/
139 static int
140 auth1_process_rsa(Authctxt *authctxt, char *info, size_t infolen)
142 int authenticated = 0;
143 BIGNUM *n;
145 /* RSA authentication requested. */
146 if ((n = BN_new()) == NULL)
147 fatal("do_authloop: BN_new failed");
148 packet_get_bignum(n);
149 packet_check_eom();
150 authenticated = auth_rsa(authctxt, n);
151 BN_clear_free(n);
153 return (authenticated);
156 /*ARGSUSED*/
157 static int
158 auth1_process_rhosts_rsa(Authctxt *authctxt, char *info, size_t infolen)
160 int keybits, authenticated = 0;
161 u_int bits;
162 Key *client_host_key;
163 u_int ulen;
166 * Get client user name. Note that we just have to
167 * trust the client; root on the client machine can
168 * claim to be any user.
170 client_user = packet_get_cstring(&ulen);
172 /* Get the client host key. */
173 client_host_key = key_new(KEY_RSA1);
174 bits = packet_get_int();
175 packet_get_bignum(client_host_key->rsa->e);
176 packet_get_bignum(client_host_key->rsa->n);
178 keybits = BN_num_bits(client_host_key->rsa->n);
179 if (keybits < 0 || bits != (u_int)keybits) {
180 verbose("Warning: keysize mismatch for client_host_key: "
181 "actual %d, announced %d",
182 BN_num_bits(client_host_key->rsa->n), bits);
184 packet_check_eom();
186 authenticated = auth_rhosts_rsa(authctxt, client_user,
187 client_host_key);
188 key_free(client_host_key);
190 snprintf(info, infolen, " ruser %.100s", client_user);
192 return (authenticated);
195 /*ARGSUSED*/
196 static int
197 auth1_process_tis_challenge(Authctxt *authctxt, char *info, size_t infolen)
199 char *challenge;
201 if ((challenge = get_challenge(authctxt)) == NULL)
202 return (0);
204 debug("sending challenge '%s'", challenge);
205 packet_start(SSH_SMSG_AUTH_TIS_CHALLENGE);
206 packet_put_cstring(challenge);
207 xfree(challenge);
208 packet_send();
209 packet_write_wait();
211 return (-1);
214 /*ARGSUSED*/
215 static int
216 auth1_process_tis_response(Authctxt *authctxt, char *info, size_t infolen)
218 int authenticated = 0;
219 char *response;
220 u_int dlen;
222 response = packet_get_string(&dlen);
223 packet_check_eom();
224 authenticated = verify_response(authctxt, response);
225 memset(response, 'r', dlen);
226 xfree(response);
228 return (authenticated);
232 * read packets, try to authenticate the user and
233 * return only if authentication is successful
235 static void
236 do_authloop(Authctxt *authctxt)
238 int authenticated = 0;
239 char info[1024];
240 int prev = 0, type = 0;
241 const struct AuthMethod1 *meth;
243 debug("Attempting authentication for %s%.100s.",
244 authctxt->valid ? "" : "invalid user ", authctxt->user);
246 /* If the user has no password, accept authentication immediately. */
247 if (options.permit_empty_passwd && options.password_authentication &&
248 #ifdef KRB5
249 (!options.kerberos_authentication || options.kerberos_or_local_passwd) &&
250 #endif
251 PRIVSEP(auth_password(authctxt, ""))) {
252 #ifdef USE_PAM
253 if (options.use_pam && (PRIVSEP(do_pam_account())))
254 #endif
256 auth_log(authctxt, 1, "without authentication", "");
257 return;
261 /* Indicate that authentication is needed. */
262 packet_start(SSH_SMSG_FAILURE);
263 packet_send();
264 packet_write_wait();
266 for (;;) {
267 /* default to fail */
268 authenticated = 0;
270 info[0] = '\0';
272 /* Get a packet from the client. */
273 prev = type;
274 type = packet_read();
277 * If we started challenge-response authentication but the
278 * next packet is not a response to our challenge, release
279 * the resources allocated by get_challenge() (which would
280 * normally have been released by verify_response() had we
281 * received such a response)
283 if (prev == SSH_CMSG_AUTH_TIS &&
284 type != SSH_CMSG_AUTH_TIS_RESPONSE)
285 abandon_challenge_response(authctxt);
287 if (authctxt->failures >= options.max_authtries)
288 goto skip;
289 if ((meth = lookup_authmethod1(type)) == NULL) {
290 logit("Unknown message during authentication: "
291 "type %d", type);
292 goto skip;
295 if (!*(meth->enabled)) {
296 verbose("%s authentication disabled.", meth->name);
297 goto skip;
300 authenticated = meth->method(authctxt, info, sizeof(info));
301 if (authenticated == -1)
302 continue; /* "postponed" */
304 #ifdef BSD_AUTH
305 if (authctxt->as) {
306 auth_close(authctxt->as);
307 authctxt->as = NULL;
309 #endif
310 if (!authctxt->valid && authenticated)
311 fatal("INTERNAL ERROR: authenticated invalid user %s",
312 authctxt->user);
314 #ifdef _UNICOS
315 if (authenticated && cray_access_denied(authctxt->user)) {
316 authenticated = 0;
317 fatal("Access denied for user %s.",authctxt->user);
319 #endif /* _UNICOS */
321 #ifndef HAVE_CYGWIN
322 /* Special handling for root */
323 if (authenticated && authctxt->pw->pw_uid == 0 &&
324 !auth_root_allowed(meth->name)) {
325 authenticated = 0;
326 # ifdef SSH_AUDIT_EVENTS
327 PRIVSEP(audit_event(SSH_LOGIN_ROOT_DENIED));
328 # endif
330 #endif
332 #ifdef USE_PAM
333 if (options.use_pam && authenticated &&
334 !PRIVSEP(do_pam_account())) {
335 char *msg;
336 size_t len;
338 error("Access denied for user %s by PAM account "
339 "configuration", authctxt->user);
340 len = buffer_len(&loginmsg);
341 buffer_append(&loginmsg, "\0", 1);
342 msg = buffer_ptr(&loginmsg);
343 /* strip trailing newlines */
344 if (len > 0)
345 while (len > 0 && msg[--len] == '\n')
346 msg[len] = '\0';
347 else
348 msg = "Access denied.";
349 packet_disconnect("%s", msg);
351 #endif
353 skip:
354 /* Log before sending the reply */
355 auth_log(authctxt, authenticated, get_authname(type), info);
357 if (client_user != NULL) {
358 xfree(client_user);
359 client_user = NULL;
362 if (authenticated)
363 return;
365 if (++authctxt->failures >= options.max_authtries) {
366 #ifdef SSH_AUDIT_EVENTS
367 PRIVSEP(audit_event(SSH_LOGIN_EXCEED_MAXTRIES));
368 #endif
369 packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
372 packet_start(SSH_SMSG_FAILURE);
373 packet_send();
374 packet_write_wait();
379 * Performs authentication of an incoming connection. Session key has already
380 * been exchanged and encryption is enabled.
382 void
383 do_authentication(Authctxt *authctxt)
385 u_int ulen;
386 char *user, *style = NULL;
388 /* Get the name of the user that we wish to log in as. */
389 packet_read_expect(SSH_CMSG_USER);
391 /* Get the user name. */
392 user = packet_get_cstring(&ulen);
393 packet_check_eom();
395 if ((style = strchr(user, ':')) != NULL)
396 *style++ = '\0';
398 authctxt->user = user;
399 authctxt->style = style;
401 /* Verify that the user is a valid user. */
402 if ((authctxt->pw = PRIVSEP(getpwnamallow(user))) != NULL)
403 authctxt->valid = 1;
404 else {
405 debug("do_authentication: invalid user %s", user);
406 authctxt->pw = fakepw();
409 setproctitle("%s%s", authctxt->valid ? user : "unknown",
410 use_privsep ? " [net]" : "");
412 #ifdef USE_PAM
413 if (options.use_pam)
414 PRIVSEP(start_pam(authctxt));
415 #endif
418 * If we are not running as root, the user must have the same uid as
419 * the server.
421 #ifndef HAVE_CYGWIN
422 if (!use_privsep && getuid() != 0 && authctxt->pw &&
423 authctxt->pw->pw_uid != getuid())
424 packet_disconnect("Cannot change user when server not running as root.");
425 #endif
428 * Loop until the user has been authenticated or the connection is
429 * closed, do_authloop() returns only if authentication is successful
431 do_authloop(authctxt);
433 /* The user has been authenticated and accepted. */
434 packet_start(SSH_SMSG_SUCCESS);
435 packet_send();
436 packet_write_wait();