]> andersk Git - openssh.git/blame - auth1.c
- djm@cvs.openbsd.org 2006/03/25 01:13:23
[openssh.git] / auth1.c
CommitLineData
a306f2dd 1/*
2 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
3 * All rights reserved
bcbf86ec 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".
a306f2dd 10 */
11
12#include "includes.h"
94ec8c6b 13
a306f2dd 14#include "xmalloc.h"
15#include "rsa.h"
42f11eb2 16#include "ssh1.h"
a306f2dd 17#include "packet.h"
18#include "buffer.h"
42f11eb2 19#include "log.h"
a306f2dd 20#include "servconf.h"
21#include "compat.h"
22#include "auth.h"
7899c98f 23#include "channels.h"
a306f2dd 24#include "session.h"
ced49be2 25#include "uidswap.h"
1853d1ef 26#include "monitor_wrap.h"
9aab0af7 27#include "buffer.h"
a306f2dd 28
29/* import */
30extern ServerOptions options;
9aab0af7 31extern Buffer loginmsg;
94ec8c6b 32
8485ce56 33static int auth1_process_password(Authctxt *, char *, size_t);
34static int auth1_process_rsa(Authctxt *, char *, size_t);
35static int auth1_process_rhosts_rsa(Authctxt *, char *, size_t);
36static int auth1_process_tis_challenge(Authctxt *, char *, size_t);
37static int auth1_process_tis_response(Authctxt *, char *, size_t);
38
39static char *client_user = NULL; /* Used to fill in remote user for PAM */
40
41struct AuthMethod1 {
42 int type;
43 char *name;
44 int *enabled;
45 int (*method)(Authctxt *, char *, size_t);
46};
47
48const struct AuthMethod1 auth1_methods[] = {
49 {
50 SSH_CMSG_AUTH_PASSWORD, "password",
51 &options.password_authentication, auth1_process_password
52 },
53 {
54 SSH_CMSG_AUTH_RSA, "rsa",
55 &options.rsa_authentication, auth1_process_rsa
56 },
57 {
58 SSH_CMSG_AUTH_RHOSTS_RSA, "rhosts-rsa",
59 &options.rhosts_rsa_authentication, auth1_process_rhosts_rsa
60 },
61 {
62 SSH_CMSG_AUTH_TIS, "challenge-response",
63 &options.challenge_response_authentication,
64 auth1_process_tis_challenge
65 },
66 {
67 SSH_CMSG_AUTH_TIS_RESPONSE, "challenge-response",
68 &options.challenge_response_authentication,
69 auth1_process_tis_response
70 },
71 { -1, NULL, NULL, NULL}
72};
73
74static const struct AuthMethod1
75*lookup_authmethod1(int type)
76{
77 int i;
78
c8e9c167 79 for (i = 0; auth1_methods[i].name != NULL; i++)
8485ce56 80 if (auth1_methods[i].type == type)
81 return (&(auth1_methods[i]));
82
83 return (NULL);
84}
85
396c147e 86static char *
a306f2dd 87get_authname(int type)
88{
8485ce56 89 const struct AuthMethod1 *a;
90 static char buf[64];
91
92 if ((a = lookup_authmethod1(type)) != NULL)
93 return (a->name);
94 snprintf(buf, sizeof(buf), "bad-auth-msg-%d", type);
95 return (buf);
96}
97
fe8c3af1 98/*ARGSUSED*/
8485ce56 99static int
100auth1_process_password(Authctxt *authctxt, char *info, size_t infolen)
101{
102 int authenticated = 0;
103 char *password;
104 u_int dlen;
105
106 /*
107 * Read user password. It is in plain text, but was
108 * transmitted over the encrypted channel so it is
109 * not visible to an outside observer.
110 */
111 password = packet_get_string(&dlen);
112 packet_check_eom();
113
114 /* Try authentication with the password. */
115 authenticated = PRIVSEP(auth_password(authctxt, password));
116
117 memset(password, 0, dlen);
118 xfree(password);
119
120 return (authenticated);
121}
122
fe8c3af1 123/*ARGSUSED*/
8485ce56 124static int
125auth1_process_rsa(Authctxt *authctxt, char *info, size_t infolen)
126{
127 int authenticated = 0;
128 BIGNUM *n;
129
130 /* RSA authentication requested. */
131 if ((n = BN_new()) == NULL)
132 fatal("do_authloop: BN_new failed");
133 packet_get_bignum(n);
134 packet_check_eom();
135 authenticated = auth_rsa(authctxt, n);
136 BN_clear_free(n);
137
138 return (authenticated);
139}
140
fe8c3af1 141/*ARGSUSED*/
8485ce56 142static int
143auth1_process_rhosts_rsa(Authctxt *authctxt, char *info, size_t infolen)
144{
a375df46 145 int keybits, authenticated = 0;
8485ce56 146 u_int bits;
147 Key *client_host_key;
148 u_int ulen;
149
150 /*
151 * Get client user name. Note that we just have to
152 * trust the client; root on the client machine can
153 * claim to be any user.
154 */
155 client_user = packet_get_string(&ulen);
156
157 /* Get the client host key. */
158 client_host_key = key_new(KEY_RSA1);
159 bits = packet_get_int();
160 packet_get_bignum(client_host_key->rsa->e);
161 packet_get_bignum(client_host_key->rsa->n);
162
a375df46 163 keybits = BN_num_bits(client_host_key->rsa->n);
164 if (keybits < 0 || bits != (u_int)keybits) {
8485ce56 165 verbose("Warning: keysize mismatch for client_host_key: "
166 "actual %d, announced %d",
167 BN_num_bits(client_host_key->rsa->n), bits);
a306f2dd 168 }
8485ce56 169 packet_check_eom();
170
171 authenticated = auth_rhosts_rsa(authctxt, client_user,
172 client_host_key);
173 key_free(client_host_key);
174
175 snprintf(info, infolen, " ruser %.100s", client_user);
d1cf9a87 176
8485ce56 177 return (authenticated);
178}
179
fe8c3af1 180/*ARGSUSED*/
8485ce56 181static int
182auth1_process_tis_challenge(Authctxt *authctxt, char *info, size_t infolen)
183{
184 char *challenge;
56964485 185
8485ce56 186 if ((challenge = get_challenge(authctxt)) == NULL)
187 return (0);
188
189 debug("sending challenge '%s'", challenge);
190 packet_start(SSH_SMSG_AUTH_TIS_CHALLENGE);
191 packet_put_cstring(challenge);
192 xfree(challenge);
193 packet_send();
194 packet_write_wait();
195
196 return (-1);
197}
198
fe8c3af1 199/*ARGSUSED*/
8485ce56 200static int
201auth1_process_tis_response(Authctxt *authctxt, char *info, size_t infolen)
202{
203 int authenticated = 0;
204 char *response;
205 u_int dlen;
206
207 response = packet_get_string(&dlen);
208 packet_check_eom();
209 authenticated = verify_response(authctxt, response);
210 memset(response, 'r', dlen);
211 xfree(response);
212
213 return (authenticated);
a306f2dd 214}
215
216/*
59c97189 217 * read packets, try to authenticate the user and
218 * return only if authentication is successful
a306f2dd 219 */
396c147e 220static void
59c97189 221do_authloop(Authctxt *authctxt)
a306f2dd 222{
94ec8c6b 223 int authenticated = 0;
59c97189 224 char info[1024];
8485ce56 225 int prev = 0, type = 0;
226 const struct AuthMethod1 *meth;
59c97189 227
228 debug("Attempting authentication for %s%.100s.",
d77347cc 229 authctxt->valid ? "" : "invalid user ", authctxt->user);
59c97189 230
231 /* If the user has no password, accept authentication immediately. */
232 if (options.password_authentication &&
1c590258 233#ifdef KRB5
59c97189 234 (!options.kerberos_authentication || options.kerberos_or_local_passwd) &&
235#endif
1853d1ef 236 PRIVSEP(auth_password(authctxt, ""))) {
ece30983 237#ifdef USE_PAM
238 if (options.use_pam && (PRIVSEP(do_pam_account())))
239#endif
240 {
241 auth_log(authctxt, 1, "without authentication", "");
242 return;
243 }
59c97189 244 }
a306f2dd 245
246 /* Indicate that authentication is needed. */
247 packet_start(SSH_SMSG_FAILURE);
248 packet_send();
249 packet_write_wait();
250
59c97189 251 for (;;) {
94ec8c6b 252 /* default to fail */
253 authenticated = 0;
254
59c97189 255 info[0] = '\0';
a306f2dd 256
257 /* Get a packet from the client. */
05114c74 258 prev = type;
54a5250f 259 type = packet_read();
a306f2dd 260
05114c74 261 /*
262 * If we started challenge-response authentication but the
263 * next packet is not a response to our challenge, release
264 * the resources allocated by get_challenge() (which would
265 * normally have been released by verify_response() had we
266 * received such a response)
267 */
268 if (prev == SSH_CMSG_AUTH_TIS &&
269 type != SSH_CMSG_AUTH_TIS_RESPONSE)
270 abandon_challenge_response(authctxt);
271
8485ce56 272 if ((meth = lookup_authmethod1(type)) == NULL) {
273 logit("Unknown message during authentication: "
274 "type %d", type);
275 goto skip;
276 }
277
278 if (!*(meth->enabled)) {
279 verbose("%s authentication disabled.", meth->name);
280 goto skip;
a306f2dd 281 }
8485ce56 282
283 authenticated = meth->method(authctxt, info, sizeof(info));
284 if (authenticated == -1)
285 continue; /* "postponed" */
286
af774732 287#ifdef BSD_AUTH
288 if (authctxt->as) {
289 auth_close(authctxt->as);
290 authctxt->as = NULL;
291 }
292#endif
59c97189 293 if (!authctxt->valid && authenticated)
294 fatal("INTERNAL ERROR: authenticated invalid user %s",
295 authctxt->user);
a306f2dd 296
ef51930f 297#ifdef _UNICOS
ef51930f 298 if (authenticated && cray_access_denied(authctxt->user)) {
299 authenticated = 0;
300 fatal("Access denied for user %s.",authctxt->user);
301 }
302#endif /* _UNICOS */
303
2b87da3b 304#ifdef HAVE_CYGWIN
305 if (authenticated &&
d1cf9a87 306 !check_nt_auth(type == SSH_CMSG_AUTH_PASSWORD,
02a57950 307 authctxt->pw)) {
3c62e7eb 308 packet_disconnect("Authentication rejected for uid %d.",
02a57950 309 authctxt->pw == NULL ? -1 : authctxt->pw->pw_uid);
3c62e7eb 310 authenticated = 0;
311 }
3c418020 312#else
59c97189 313 /* Special handling for root */
5d47f1d4 314 if (authenticated && authctxt->pw->pw_uid == 0 &&
8485ce56 315 !auth_root_allowed(meth->name)) {
316 authenticated = 0;
6039eeef 317# ifdef SSH_AUDIT_EVENTS
318 PRIVSEP(audit_event(SSH_LOGIN_ROOT_DENIED));
c00e4d75 319# endif
320 }
3c418020 321#endif
a306f2dd 322
5b9e2464 323#ifdef USE_PAM
aff51935 324 if (options.use_pam && authenticated &&
9aab0af7 325 !PRIVSEP(do_pam_account())) {
326 char *msg;
327 size_t len;
328
329 error("Access denied for user %s by PAM account "
98c044d0 330 "configuration", authctxt->user);
9aab0af7 331 len = buffer_len(&loginmsg);
332 buffer_append(&loginmsg, "\0", 1);
333 msg = buffer_ptr(&loginmsg);
334 /* strip trailing newlines */
335 if (len > 0)
336 while (len > 0 && msg[--len] == '\n')
337 msg[len] = '\0';
338 else
339 msg = "Access denied.";
340 packet_disconnect(msg);
341 }
5b9e2464 342#endif
343
8485ce56 344 skip:
59c97189 345 /* Log before sending the reply */
346 auth_log(authctxt, authenticated, get_authname(type), info);
347
a306f2dd 348 if (client_user != NULL) {
349 xfree(client_user);
350 client_user = NULL;
351 }
352
94ec8c6b 353 if (authenticated)
354 return;
355
c00e4d75 356 if (authctxt->failures++ > options.max_authtries) {
6039eeef 357#ifdef SSH_AUDIT_EVENTS
358 PRIVSEP(audit_event(SSH_LOGIN_EXCEED_MAXTRIES));
c00e4d75 359#endif
59c97189 360 packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
c00e4d75 361 }
a306f2dd 362
a306f2dd 363 packet_start(SSH_SMSG_FAILURE);
364 packet_send();
365 packet_write_wait();
366 }
367}
368
369/*
370 * Performs authentication of an incoming connection. Session key has already
371 * been exchanged and encryption is enabled.
372 */
2362db19 373void
374do_authentication(Authctxt *authctxt)
a306f2dd 375{
1e3b8b07 376 u_int ulen;
f8d99a32 377 char *user, *style = NULL;
a306f2dd 378
379 /* Get the name of the user that we wish to log in as. */
54a5250f 380 packet_read_expect(SSH_CMSG_USER);
a306f2dd 381
382 /* Get the user name. */
383 user = packet_get_string(&ulen);
95500969 384 packet_check_eom();
a306f2dd 385
59c97189 386 if ((style = strchr(user, ':')) != NULL)
ced49be2 387 *style++ = '\0';
59c97189 388
59c97189 389 authctxt->user = user;
390 authctxt->style = style;
391
a306f2dd 392 /* Verify that the user is a valid user. */
5f1f36b5 393 if ((authctxt->pw = PRIVSEP(getpwnamallow(user))) != NULL)
59c97189 394 authctxt->valid = 1;
96d0bf74 395 else {
d77347cc 396 debug("do_authentication: invalid user %s", user);
96d0bf74 397 authctxt->pw = fakepw();
398 }
a306f2dd 399
bd5c0694 400 setproctitle("%s%s", authctxt->valid ? user : "unknown",
1853d1ef 401 use_privsep ? " [net]" : "");
6172e290 402
a306f2dd 403#ifdef USE_PAM
7fceb20d 404 if (options.use_pam)
529d73ab 405 PRIVSEP(start_pam(authctxt));
a306f2dd 406#endif
407
408 /*
409 * If we are not running as root, the user must have the same uid as
8485ce56 410 * the server.
a306f2dd 411 */
94ec8c6b 412#ifndef HAVE_CYGWIN
5f1f36b5 413 if (!use_privsep && getuid() != 0 && authctxt->pw &&
414 authctxt->pw->pw_uid != getuid())
a306f2dd 415 packet_disconnect("Cannot change user when server not running as root.");
3c62e7eb 416#endif
a306f2dd 417
59c97189 418 /*
b4b5d644 419 * Loop until the user has been authenticated or the connection is
420 * closed, do_authloop() returns only if authentication is successful
421 */
59c97189 422 do_authloop(authctxt);
a306f2dd 423
424 /* The user has been authenticated and accepted. */
94ec8c6b 425 packet_start(SSH_SMSG_SUCCESS);
426 packet_send();
427 packet_write_wait();
a306f2dd 428}
This page took 0.229681 seconds and 5 git commands to generate.