ipfw3: dummynet dispatch back to the same cpu
[dragonfly.git] / crypto / openssh / auth1.c
blob50388285ce4cf1763940b4feb426597aee7eba90
1 /* $OpenBSD: auth1.c,v 1.82 2014/07/15 15:54:14 millert 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 "misc.h"
31 #include "servconf.h"
32 #include "compat.h"
33 #include "key.h"
34 #include "hostfile.h"
35 #include "auth.h"
36 #include "channels.h"
37 #include "session.h"
38 #include "uidswap.h"
39 #ifdef GSSAPI
40 #include "ssh-gss.h"
41 #endif
42 #include "monitor_wrap.h"
43 #include "buffer.h"
45 /* import */
46 extern ServerOptions options;
47 extern Buffer loginmsg;
49 static int auth1_process_password(Authctxt *);
50 static int auth1_process_rsa(Authctxt *);
51 static int auth1_process_rhosts_rsa(Authctxt *);
52 static int auth1_process_tis_challenge(Authctxt *);
53 static int auth1_process_tis_response(Authctxt *);
55 static char *client_user = NULL; /* Used to fill in remote user for PAM */
57 struct AuthMethod1 {
58 int type;
59 char *name;
60 int *enabled;
61 int (*method)(Authctxt *);
64 const struct AuthMethod1 auth1_methods[] = {
66 SSH_CMSG_AUTH_PASSWORD, "password",
67 &options.password_authentication, auth1_process_password
70 SSH_CMSG_AUTH_RSA, "rsa",
71 &options.rsa_authentication, auth1_process_rsa
74 SSH_CMSG_AUTH_RHOSTS_RSA, "rhosts-rsa",
75 &options.rhosts_rsa_authentication, auth1_process_rhosts_rsa
78 SSH_CMSG_AUTH_TIS, "challenge-response",
79 &options.challenge_response_authentication,
80 auth1_process_tis_challenge
83 SSH_CMSG_AUTH_TIS_RESPONSE, "challenge-response",
84 &options.challenge_response_authentication,
85 auth1_process_tis_response
87 { -1, NULL, NULL, NULL}
90 static const struct AuthMethod1
91 *lookup_authmethod1(int type)
93 int i;
95 for (i = 0; auth1_methods[i].name != NULL; i++)
96 if (auth1_methods[i].type == type)
97 return (&(auth1_methods[i]));
99 return (NULL);
102 static char *
103 get_authname(int type)
105 const struct AuthMethod1 *a;
106 static char buf[64];
108 if ((a = lookup_authmethod1(type)) != NULL)
109 return (a->name);
110 snprintf(buf, sizeof(buf), "bad-auth-msg-%d", type);
111 return (buf);
114 /*ARGSUSED*/
115 static int
116 auth1_process_password(Authctxt *authctxt)
118 int authenticated = 0;
119 char *password;
120 u_int dlen;
123 * Read user password. It is in plain text, but was
124 * transmitted over the encrypted channel so it is
125 * not visible to an outside observer.
127 password = packet_get_string(&dlen);
128 packet_check_eom();
130 /* Try authentication with the password. */
131 authenticated = PRIVSEP(auth_password(authctxt, password));
133 explicit_bzero(password, dlen);
134 free(password);
136 return (authenticated);
139 /*ARGSUSED*/
140 static int
141 auth1_process_rsa(Authctxt *authctxt)
143 int authenticated = 0;
144 BIGNUM *n;
146 /* RSA authentication requested. */
147 if ((n = BN_new()) == NULL)
148 fatal("do_authloop: BN_new failed");
149 packet_get_bignum(n);
150 packet_check_eom();
151 authenticated = auth_rsa(authctxt, n);
152 BN_clear_free(n);
154 return (authenticated);
157 /*ARGSUSED*/
158 static int
159 auth1_process_rhosts_rsa(Authctxt *authctxt)
161 int keybits, authenticated = 0;
162 u_int bits;
163 Key *client_host_key;
164 u_int ulen;
167 * Get client user name. Note that we just have to
168 * trust the client; root on the client machine can
169 * claim to be any user.
171 client_user = packet_get_cstring(&ulen);
173 /* Get the client host key. */
174 client_host_key = key_new(KEY_RSA1);
175 bits = packet_get_int();
176 packet_get_bignum(client_host_key->rsa->e);
177 packet_get_bignum(client_host_key->rsa->n);
179 keybits = BN_num_bits(client_host_key->rsa->n);
180 if (keybits < 0 || bits != (u_int)keybits) {
181 verbose("Warning: keysize mismatch for client_host_key: "
182 "actual %d, announced %d",
183 BN_num_bits(client_host_key->rsa->n), bits);
185 packet_check_eom();
187 authenticated = auth_rhosts_rsa(authctxt, client_user,
188 client_host_key);
189 key_free(client_host_key);
191 auth_info(authctxt, "ruser %.100s", client_user);
193 return (authenticated);
196 /*ARGSUSED*/
197 static int
198 auth1_process_tis_challenge(Authctxt *authctxt)
200 char *challenge;
202 if ((challenge = get_challenge(authctxt)) == NULL)
203 return (0);
205 debug("sending challenge '%s'", challenge);
206 packet_start(SSH_SMSG_AUTH_TIS_CHALLENGE);
207 packet_put_cstring(challenge);
208 free(challenge);
209 packet_send();
210 packet_write_wait();
212 return (-1);
215 /*ARGSUSED*/
216 static int
217 auth1_process_tis_response(Authctxt *authctxt)
219 int authenticated = 0;
220 char *response;
221 u_int dlen;
223 response = packet_get_string(&dlen);
224 packet_check_eom();
225 authenticated = verify_response(authctxt, response);
226 explicit_bzero(response, dlen);
227 free(response);
229 return (authenticated);
233 * read packets, try to authenticate the user and
234 * return only if authentication is successful
236 static void
237 do_authloop(Authctxt *authctxt)
239 int authenticated = 0;
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, 0, "without authentication",
257 NULL);
258 return;
262 /* Indicate that authentication is needed. */
263 packet_start(SSH_SMSG_FAILURE);
264 packet_send();
265 packet_write_wait();
267 for (;;) {
268 /* default to fail */
269 authenticated = 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);
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, 0, get_authname(type), NULL);
357 free(client_user);
358 client_user = NULL;
360 if (authenticated)
361 return;
363 if (++authctxt->failures >= options.max_authtries) {
364 #ifdef SSH_AUDIT_EVENTS
365 PRIVSEP(audit_event(SSH_LOGIN_EXCEED_MAXTRIES));
366 #endif
367 auth_maxtries_exceeded(authctxt);
370 packet_start(SSH_SMSG_FAILURE);
371 packet_send();
372 packet_write_wait();
377 * Performs authentication of an incoming connection. Session key has already
378 * been exchanged and encryption is enabled.
380 void
381 do_authentication(Authctxt *authctxt)
383 u_int ulen;
384 char *user, *style = NULL;
386 /* Get the name of the user that we wish to log in as. */
387 packet_read_expect(SSH_CMSG_USER);
389 /* Get the user name. */
390 user = packet_get_cstring(&ulen);
391 packet_check_eom();
393 if ((style = strchr(user, ':')) != NULL)
394 *style++ = '\0';
396 authctxt->user = user;
397 authctxt->style = style;
399 /* Verify that the user is a valid user. */
400 if ((authctxt->pw = PRIVSEP(getpwnamallow(user))) != NULL)
401 authctxt->valid = 1;
402 else {
403 debug("do_authentication: invalid user %s", user);
404 authctxt->pw = fakepw();
407 /* Configuration may have changed as a result of Match */
408 if (options.num_auth_methods != 0)
409 fatal("AuthenticationMethods is not supported with SSH "
410 "protocol 1");
412 setproctitle("%s%s", authctxt->valid ? user : "unknown",
413 use_privsep ? " [net]" : "");
415 #ifdef USE_PAM
416 if (options.use_pam)
417 PRIVSEP(start_pam(authctxt));
418 #endif
421 * If we are not running as root, the user must have the same uid as
422 * the server.
424 #ifndef HAVE_CYGWIN
425 if (!use_privsep && getuid() != 0 && authctxt->pw &&
426 authctxt->pw->pw_uid != getuid())
427 packet_disconnect("Cannot change user when server not running as root.");
428 #endif
431 * Loop until the user has been authenticated or the connection is
432 * closed, do_authloop() returns only if authentication is successful
434 do_authloop(authctxt);
436 /* The user has been authenticated and accepted. */
437 packet_start(SSH_SMSG_SUCCESS);
438 packet_send();
439 packet_write_wait();