- (djm) [auth-pam.c auth-shadow.c auth2-none.c cleanup.c sshd.c]
[openssh-git.git] / auth1.c
blob34dcf62669e2290ac82c6df7027c4802cdbcf30c
1 /* $OpenBSD: auth1.c,v 1.70 2006/08/03 03:34:41 deraadt 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 <stdio.h>
18 #include <string.h>
19 #include <unistd.h>
20 #include <pwd.h>
22 #include "xmalloc.h"
23 #include "rsa.h"
24 #include "ssh1.h"
25 #include "packet.h"
26 #include "buffer.h"
27 #include "log.h"
28 #include "servconf.h"
29 #include "compat.h"
30 #include "key.h"
31 #include "hostfile.h"
32 #include "auth.h"
33 #include "channels.h"
34 #include "session.h"
35 #include "uidswap.h"
36 #ifdef GSSAPI
37 #include "ssh-gss.h"
38 #endif
39 #include "monitor_wrap.h"
40 #include "buffer.h"
42 /* import */
43 extern ServerOptions options;
44 extern Buffer loginmsg;
46 static int auth1_process_password(Authctxt *, char *, size_t);
47 static int auth1_process_rsa(Authctxt *, char *, size_t);
48 static int auth1_process_rhosts_rsa(Authctxt *, char *, size_t);
49 static int auth1_process_tis_challenge(Authctxt *, char *, size_t);
50 static int auth1_process_tis_response(Authctxt *, char *, size_t);
52 static char *client_user = NULL; /* Used to fill in remote user for PAM */
54 struct AuthMethod1 {
55 int type;
56 char *name;
57 int *enabled;
58 int (*method)(Authctxt *, char *, size_t);
61 const struct AuthMethod1 auth1_methods[] = {
63 SSH_CMSG_AUTH_PASSWORD, "password",
64 &options.password_authentication, auth1_process_password
67 SSH_CMSG_AUTH_RSA, "rsa",
68 &options.rsa_authentication, auth1_process_rsa
71 SSH_CMSG_AUTH_RHOSTS_RSA, "rhosts-rsa",
72 &options.rhosts_rsa_authentication, auth1_process_rhosts_rsa
75 SSH_CMSG_AUTH_TIS, "challenge-response",
76 &options.challenge_response_authentication,
77 auth1_process_tis_challenge
80 SSH_CMSG_AUTH_TIS_RESPONSE, "challenge-response",
81 &options.challenge_response_authentication,
82 auth1_process_tis_response
84 { -1, NULL, NULL, NULL}
87 static const struct AuthMethod1
88 *lookup_authmethod1(int type)
90 int i;
92 for (i = 0; auth1_methods[i].name != NULL; i++)
93 if (auth1_methods[i].type == type)
94 return (&(auth1_methods[i]));
96 return (NULL);
99 static char *
100 get_authname(int type)
102 const struct AuthMethod1 *a;
103 static char buf[64];
105 if ((a = lookup_authmethod1(type)) != NULL)
106 return (a->name);
107 snprintf(buf, sizeof(buf), "bad-auth-msg-%d", type);
108 return (buf);
111 /*ARGSUSED*/
112 static int
113 auth1_process_password(Authctxt *authctxt, char *info, size_t infolen)
115 int authenticated = 0;
116 char *password;
117 u_int dlen;
120 * Read user password. It is in plain text, but was
121 * transmitted over the encrypted channel so it is
122 * not visible to an outside observer.
124 password = packet_get_string(&dlen);
125 packet_check_eom();
127 /* Try authentication with the password. */
128 authenticated = PRIVSEP(auth_password(authctxt, password));
130 memset(password, 0, dlen);
131 xfree(password);
133 return (authenticated);
136 /*ARGSUSED*/
137 static int
138 auth1_process_rsa(Authctxt *authctxt, char *info, size_t infolen)
140 int authenticated = 0;
141 BIGNUM *n;
143 /* RSA authentication requested. */
144 if ((n = BN_new()) == NULL)
145 fatal("do_authloop: BN_new failed");
146 packet_get_bignum(n);
147 packet_check_eom();
148 authenticated = auth_rsa(authctxt, n);
149 BN_clear_free(n);
151 return (authenticated);
154 /*ARGSUSED*/
155 static int
156 auth1_process_rhosts_rsa(Authctxt *authctxt, char *info, size_t infolen)
158 int keybits, authenticated = 0;
159 u_int bits;
160 Key *client_host_key;
161 u_int ulen;
164 * Get client user name. Note that we just have to
165 * trust the client; root on the client machine can
166 * claim to be any user.
168 client_user = packet_get_string(&ulen);
170 /* Get the client host key. */
171 client_host_key = key_new(KEY_RSA1);
172 bits = packet_get_int();
173 packet_get_bignum(client_host_key->rsa->e);
174 packet_get_bignum(client_host_key->rsa->n);
176 keybits = BN_num_bits(client_host_key->rsa->n);
177 if (keybits < 0 || bits != (u_int)keybits) {
178 verbose("Warning: keysize mismatch for client_host_key: "
179 "actual %d, announced %d",
180 BN_num_bits(client_host_key->rsa->n), bits);
182 packet_check_eom();
184 authenticated = auth_rhosts_rsa(authctxt, client_user,
185 client_host_key);
186 key_free(client_host_key);
188 snprintf(info, infolen, " ruser %.100s", client_user);
190 return (authenticated);
193 /*ARGSUSED*/
194 static int
195 auth1_process_tis_challenge(Authctxt *authctxt, char *info, size_t infolen)
197 char *challenge;
199 if ((challenge = get_challenge(authctxt)) == NULL)
200 return (0);
202 debug("sending challenge '%s'", challenge);
203 packet_start(SSH_SMSG_AUTH_TIS_CHALLENGE);
204 packet_put_cstring(challenge);
205 xfree(challenge);
206 packet_send();
207 packet_write_wait();
209 return (-1);
212 /*ARGSUSED*/
213 static int
214 auth1_process_tis_response(Authctxt *authctxt, char *info, size_t infolen)
216 int authenticated = 0;
217 char *response;
218 u_int dlen;
220 response = packet_get_string(&dlen);
221 packet_check_eom();
222 authenticated = verify_response(authctxt, response);
223 memset(response, 'r', dlen);
224 xfree(response);
226 return (authenticated);
230 * read packets, try to authenticate the user and
231 * return only if authentication is successful
233 static void
234 do_authloop(Authctxt *authctxt)
236 int authenticated = 0;
237 char info[1024];
238 int prev = 0, type = 0;
239 const struct AuthMethod1 *meth;
241 debug("Attempting authentication for %s%.100s.",
242 authctxt->valid ? "" : "invalid user ", authctxt->user);
244 /* If the user has no password, accept authentication immediately. */
245 if (options.password_authentication &&
246 #ifdef KRB5
247 (!options.kerberos_authentication || options.kerberos_or_local_passwd) &&
248 #endif
249 PRIVSEP(auth_password(authctxt, ""))) {
250 #ifdef USE_PAM
251 if (options.use_pam && (PRIVSEP(do_pam_account())))
252 #endif
254 auth_log(authctxt, 1, "without authentication", "");
255 return;
259 /* Indicate that authentication is needed. */
260 packet_start(SSH_SMSG_FAILURE);
261 packet_send();
262 packet_write_wait();
264 for (;;) {
265 /* default to fail */
266 authenticated = 0;
268 info[0] = '\0';
270 /* Get a packet from the client. */
271 prev = type;
272 type = packet_read();
275 * If we started challenge-response authentication but the
276 * next packet is not a response to our challenge, release
277 * the resources allocated by get_challenge() (which would
278 * normally have been released by verify_response() had we
279 * received such a response)
281 if (prev == SSH_CMSG_AUTH_TIS &&
282 type != SSH_CMSG_AUTH_TIS_RESPONSE)
283 abandon_challenge_response(authctxt);
285 if ((meth = lookup_authmethod1(type)) == NULL) {
286 logit("Unknown message during authentication: "
287 "type %d", type);
288 goto skip;
291 if (!*(meth->enabled)) {
292 verbose("%s authentication disabled.", meth->name);
293 goto skip;
296 authenticated = meth->method(authctxt, info, sizeof(info));
297 if (authenticated == -1)
298 continue; /* "postponed" */
300 #ifdef BSD_AUTH
301 if (authctxt->as) {
302 auth_close(authctxt->as);
303 authctxt->as = NULL;
305 #endif
306 if (!authctxt->valid && authenticated)
307 fatal("INTERNAL ERROR: authenticated invalid user %s",
308 authctxt->user);
310 #ifdef _UNICOS
311 if (authenticated && cray_access_denied(authctxt->user)) {
312 authenticated = 0;
313 fatal("Access denied for user %s.",authctxt->user);
315 #endif /* _UNICOS */
317 #ifdef HAVE_CYGWIN
318 if (authenticated &&
319 !check_nt_auth(type == SSH_CMSG_AUTH_PASSWORD,
320 authctxt->pw)) {
321 packet_disconnect("Authentication rejected for uid %d.",
322 authctxt->pw == NULL ? -1 : authctxt->pw->pw_uid);
323 authenticated = 0;
325 #else
326 /* Special handling for root */
327 if (authenticated && authctxt->pw->pw_uid == 0 &&
328 !auth_root_allowed(meth->name)) {
329 authenticated = 0;
330 # ifdef SSH_AUDIT_EVENTS
331 PRIVSEP(audit_event(SSH_LOGIN_ROOT_DENIED));
332 # endif
334 #endif
336 #ifdef USE_PAM
337 if (options.use_pam && authenticated &&
338 !PRIVSEP(do_pam_account())) {
339 char *msg;
340 size_t len;
342 error("Access denied for user %s by PAM account "
343 "configuration", authctxt->user);
344 len = buffer_len(&loginmsg);
345 buffer_append(&loginmsg, "\0", 1);
346 msg = buffer_ptr(&loginmsg);
347 /* strip trailing newlines */
348 if (len > 0)
349 while (len > 0 && msg[--len] == '\n')
350 msg[len] = '\0';
351 else
352 msg = "Access denied.";
353 packet_disconnect(msg);
355 #endif
357 skip:
358 /* Log before sending the reply */
359 auth_log(authctxt, authenticated, get_authname(type), info);
361 if (client_user != NULL) {
362 xfree(client_user);
363 client_user = NULL;
366 if (authenticated)
367 return;
369 if (authctxt->failures++ > options.max_authtries) {
370 #ifdef SSH_AUDIT_EVENTS
371 PRIVSEP(audit_event(SSH_LOGIN_EXCEED_MAXTRIES));
372 #endif
373 packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
376 packet_start(SSH_SMSG_FAILURE);
377 packet_send();
378 packet_write_wait();
383 * Performs authentication of an incoming connection. Session key has already
384 * been exchanged and encryption is enabled.
386 void
387 do_authentication(Authctxt *authctxt)
389 u_int ulen;
390 char *user, *style = NULL;
392 /* Get the name of the user that we wish to log in as. */
393 packet_read_expect(SSH_CMSG_USER);
395 /* Get the user name. */
396 user = packet_get_string(&ulen);
397 packet_check_eom();
399 if ((style = strchr(user, ':')) != NULL)
400 *style++ = '\0';
402 authctxt->user = user;
403 authctxt->style = style;
405 /* Verify that the user is a valid user. */
406 if ((authctxt->pw = PRIVSEP(getpwnamallow(user))) != NULL)
407 authctxt->valid = 1;
408 else {
409 debug("do_authentication: invalid user %s", user);
410 authctxt->pw = fakepw();
413 setproctitle("%s%s", authctxt->valid ? user : "unknown",
414 use_privsep ? " [net]" : "");
416 #ifdef USE_PAM
417 if (options.use_pam)
418 PRIVSEP(start_pam(authctxt));
419 #endif
422 * If we are not running as root, the user must have the same uid as
423 * the server.
425 #ifndef HAVE_CYGWIN
426 if (!use_privsep && getuid() != 0 && authctxt->pw &&
427 authctxt->pw->pw_uid != getuid())
428 packet_disconnect("Cannot change user when server not running as root.");
429 #endif
432 * Loop until the user has been authenticated or the connection is
433 * closed, do_authloop() returns only if authentication is successful
435 do_authloop(authctxt);
437 /* The user has been authenticated and accepted. */
438 packet_start(SSH_SMSG_SUCCESS);
439 packet_send();
440 packet_write_wait();