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