]> andersk Git - openssh.git/blame - auth2.c
- djm@cvs.openbsd.org 2010/01/30 02:54:53
[openssh.git] / auth2.c
CommitLineData
127c96db 1/* $OpenBSD: auth2.c,v 1.121 2009/06/22 05:39:28 dtucker Exp $ */
a306f2dd 2/*
3 * Copyright (c) 2000 Markus Friedl. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
a306f2dd 13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
bcbf86ec 25
a306f2dd 26#include "includes.h"
a306f2dd 27
b1842393 28#include <sys/types.h>
221fc73c 29#include <sys/stat.h>
30#include <sys/uio.h>
b1842393 31
221fc73c 32#include <fcntl.h>
b1842393 33#include <pwd.h>
31652869 34#include <stdarg.h>
00146caa 35#include <string.h>
221fc73c 36#include <unistd.h>
b1842393 37
71709bcd 38#include "atomicio.h"
9b9302ea 39#include "xmalloc.h"
31652869 40#include "ssh2.h"
a306f2dd 41#include "packet.h"
42f11eb2 42#include "log.h"
31652869 43#include "buffer.h"
a306f2dd 44#include "servconf.h"
45#include "compat.h"
31652869 46#include "key.h"
47#include "hostfile.h"
a306f2dd 48#include "auth.h"
a306f2dd 49#include "dispatch.h"
42f11eb2 50#include "pathnames.h"
ba6dd90e 51#include "buffer.h"
a306f2dd 52
7364bd04 53#ifdef GSSAPI
54#include "ssh-gss.h"
55#endif
31652869 56#include "monitor_wrap.h"
7364bd04 57
a306f2dd 58/* import */
59extern ServerOptions options;
1e3b8b07 60extern u_char *session_id2;
a27002e5 61extern u_int session_id2_len;
ba6dd90e 62extern Buffer loginmsg;
a306f2dd 63
01c24737 64/* methods */
65
66extern Authmethod method_none;
67extern Authmethod method_pubkey;
68extern Authmethod method_passwd;
69extern Authmethod method_kbdint;
70extern Authmethod method_hostbased;
7364bd04 71#ifdef GSSAPI
72extern Authmethod method_gssapi;
73#endif
5adf6b9a 74#ifdef JPAKE
75extern Authmethod method_jpake;
76#endif
01c24737 77
78Authmethod *authmethods[] = {
79 &method_none,
80 &method_pubkey,
7364bd04 81#ifdef GSSAPI
82 &method_gssapi,
5adf6b9a 83#endif
84#ifdef JPAKE
85 &method_jpake,
7364bd04 86#endif
01c24737 87 &method_passwd,
88 &method_kbdint,
89 &method_hostbased,
90 NULL
94ec8c6b 91};
92
a306f2dd 93/* protocol */
94
7819b5c3 95static void input_service_request(int, u_int32_t, void *);
96static void input_userauth_request(int, u_int32_t, void *);
a306f2dd 97
a306f2dd 98/* helper */
396c147e 99static Authmethod *authmethod_lookup(const char *);
ffb215be 100static char *authmethods_get(void);
a306f2dd 101
221fc73c 102char *
103auth2_read_banner(void)
104{
105 struct stat st;
106 char *banner = NULL;
107 size_t len, n;
108 int fd;
109
110 if ((fd = open(options.banner, O_RDONLY)) == -1)
111 return (NULL);
112 if (fstat(fd, &st) == -1) {
113 close(fd);
114 return (NULL);
115 }
116 if (st.st_size > 1*1024*1024) {
117 close(fd);
118 return (NULL);
119 }
120
121 len = (size_t)st.st_size; /* truncate */
122 banner = xmalloc(len + 1);
123 n = atomicio(read, fd, banner, len);
124 close(fd);
125
126 if (n != len) {
127 xfree(banner);
128 return (NULL);
129 }
130 banner[n] = '\0';
131
132 return (banner);
133}
134
135void
136userauth_send_banner(const char *msg)
137{
138 if (datafellows & SSH_BUG_BANNER)
139 return;
140
141 packet_start(SSH2_MSG_USERAUTH_BANNER);
142 packet_put_cstring(msg);
143 packet_put_cstring(""); /* language, unused */
144 packet_send();
145 debug("%s: sent", __func__);
146}
147
148static void
149userauth_banner(void)
150{
151 char *banner = NULL;
152
153 if (options.banner == NULL ||
154 strcasecmp(options.banner, "none") == 0 ||
155 (datafellows & SSH_BUG_BANNER) != 0)
156 return;
157
158 if ((banner = PRIVSEP(auth2_read_banner())) == NULL)
159 goto done;
160 userauth_send_banner(banner);
161
162done:
163 if (banner)
164 xfree(banner);
165}
166
a306f2dd 167/*
94ec8c6b 168 * loop until authctxt->success == TRUE
a306f2dd 169 */
2362db19 170void
171do_authentication2(Authctxt *authctxt)
a306f2dd 172{
6367063f 173 dispatch_init(&dispatch_protocol_error);
a306f2dd 174 dispatch_set(SSH2_MSG_SERVICE_REQUEST, &input_service_request);
94ec8c6b 175 dispatch_run(DISPATCH_BLOCK, &authctxt->success, authctxt);
a306f2dd 176}
177
fe8c3af1 178/*ARGSUSED*/
396c147e 179static void
7819b5c3 180input_service_request(int type, u_int32_t seq, void *ctxt)
a306f2dd 181{
94ec8c6b 182 Authctxt *authctxt = ctxt;
1e3b8b07 183 u_int len;
7fdc56c5 184 int acceptit = 0;
a306f2dd 185 char *service = packet_get_string(&len);
95500969 186 packet_check_eom();
a306f2dd 187
94ec8c6b 188 if (authctxt == NULL)
189 fatal("input_service_request: no authctxt");
190
a306f2dd 191 if (strcmp(service, "ssh-userauth") == 0) {
94ec8c6b 192 if (!authctxt->success) {
7fdc56c5 193 acceptit = 1;
a306f2dd 194 /* now we can handle user-auth requests */
195 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &input_userauth_request);
196 }
197 }
198 /* XXX all other service requests are denied */
199
7fdc56c5 200 if (acceptit) {
a306f2dd 201 packet_start(SSH2_MSG_SERVICE_ACCEPT);
202 packet_put_cstring(service);
203 packet_send();
204 packet_write_wait();
205 } else {
206 debug("bad service request %s", service);
207 packet_disconnect("bad service request %s", service);
208 }
209 xfree(service);
210}
211
fe8c3af1 212/*ARGSUSED*/
396c147e 213static void
7819b5c3 214input_userauth_request(int type, u_int32_t seq, void *ctxt)
a306f2dd 215{
94ec8c6b 216 Authctxt *authctxt = ctxt;
217 Authmethod *m = NULL;
59c97189 218 char *user, *service, *method, *style = NULL;
a306f2dd 219 int authenticated = 0;
a306f2dd 220
94ec8c6b 221 if (authctxt == NULL)
222 fatal("input_userauth_request: no authctxt");
a306f2dd 223
94ec8c6b 224 user = packet_get_string(NULL);
225 service = packet_get_string(NULL);
226 method = packet_get_string(NULL);
227 debug("userauth-request for user %s service %s method %s", user, service, method);
8abcdba4 228 debug("attempt %d failures %d", authctxt->attempt, authctxt->failures);
94ec8c6b 229
59c97189 230 if ((style = strchr(user, ':')) != NULL)
231 *style++ = 0;
232
2b87da3b 233 if (authctxt->attempt++ == 0) {
63284fbb 234 /* setup auth context */
f7ed12f1 235 authctxt->pw = PRIVSEP(getpwnamallow(user));
529d73ab 236 authctxt->user = xstrdup(user);
f7ed12f1 237 if (authctxt->pw && strcmp(service, "ssh-connection")==0) {
94ec8c6b 238 authctxt->valid = 1;
239 debug2("input_userauth_request: setting up authctxt for %s", user);
94ec8c6b 240 } else {
d77347cc 241 logit("input_userauth_request: invalid user %s", user);
96d0bf74 242 authctxt->pw = fakepw();
6039eeef 243#ifdef SSH_AUDIT_EVENTS
244 PRIVSEP(audit_event(SSH_INVALID_USER));
04fc7a67 245#endif
94ec8c6b 246 }
b6c37221 247#ifdef USE_PAM
248 if (options.use_pam)
249 PRIVSEP(start_pam(authctxt));
250#endif
bd5c0694 251 setproctitle("%s%s", authctxt->valid ? user : "unknown",
1853d1ef 252 use_privsep ? " [net]" : "");
94ec8c6b 253 authctxt->service = xstrdup(service);
2a6a054e 254 authctxt->style = style ? xstrdup(style) : NULL;
1853d1ef 255 if (use_privsep)
256 mm_inform_authserv(service, style);
221fc73c 257 userauth_banner();
2a6a054e 258 } else if (strcmp(user, authctxt->user) != 0 ||
259 strcmp(service, authctxt->service) != 0) {
260 packet_disconnect("Change of username or service not allowed: "
261 "(%s,%s) -> (%s,%s)",
262 authctxt->user, authctxt->service, user, service);
a306f2dd 263 }
59c97189 264 /* reset state */
2f293d43 265 auth2_challenge_stop(authctxt);
5adf6b9a 266#ifdef JPAKE
267 auth2_jpake_stop(authctxt);
268#endif
7364bd04 269
270#ifdef GSSAPI
5adf6b9a 271 /* XXX move to auth2_gssapi_stop() */
7364bd04 272 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
273 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, NULL);
274#endif
275
59c97189 276 authctxt->postponed = 0;
9cd45ea4 277
59c97189 278 /* try to authenticate user */
94ec8c6b 279 m = authmethod_lookup(method);
221fc73c 280 if (m != NULL && authctxt->failures < options.max_authtries) {
94ec8c6b 281 debug2("input_userauth_request: try method %s", method);
282 authenticated = m->userauth(authctxt);
9cd45ea4 283 }
d9cd3575 284 userauth_finish(authctxt, authenticated, method);
285
286 xfree(service);
287 xfree(user);
288 xfree(method);
289}
290
291void
292userauth_finish(Authctxt *authctxt, int authenticated, char *method)
293{
4e81159c 294 char *methods;
295
59c97189 296 if (!authctxt->valid && authenticated)
297 fatal("INTERNAL ERROR: authenticated invalid user %s",
298 authctxt->user);
9cd45ea4 299
94ec8c6b 300 /* Special handling for root */
5d47f1d4 301 if (authenticated && authctxt->pw->pw_uid == 0 &&
c00e4d75 302 !auth_root_allowed(method)) {
a306f2dd 303 authenticated = 0;
6039eeef 304#ifdef SSH_AUDIT_EVENTS
305 PRIVSEP(audit_event(SSH_LOGIN_ROOT_DENIED));
c00e4d75 306#endif
307 }
a306f2dd 308
5b9e2464 309#ifdef USE_PAM
ba6dd90e 310 if (options.use_pam && authenticated) {
311 if (!PRIVSEP(do_pam_account())) {
ba6dd90e 312 /* if PAM returned a message, send it to the user */
313 if (buffer_len(&loginmsg) > 0) {
314 buffer_append(&loginmsg, "\0", 1);
315 userauth_send_banner(buffer_ptr(&loginmsg));
9aab0af7 316 packet_write_wait();
ba6dd90e 317 }
9aab0af7 318 fatal("Access denied for user %s by PAM account "
98c044d0 319 "configuration", authctxt->user);
ba6dd90e 320 }
321 }
5b9e2464 322#endif
323
ef51930f 324#ifdef _UNICOS
325 if (authenticated && cray_access_denied(authctxt->user)) {
326 authenticated = 0;
327 fatal("Access denied for user %s.",authctxt->user);
328 }
329#endif /* _UNICOS */
330
94ec8c6b 331 /* Log before sending the reply */
59c97189 332 auth_log(authctxt, authenticated, method, " ssh2");
333
4e81159c 334 if (authctxt->postponed)
335 return;
336
337 /* XXX todo: check if multiple auth methods are needed */
338 if (authenticated == 1) {
339 /* turn off userauth */
6367063f 340 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &dispatch_protocol_ignore);
4e81159c 341 packet_start(SSH2_MSG_USERAUTH_SUCCESS);
342 packet_send();
343 packet_write_wait();
344 /* now we can break out */
345 authctxt->success = 1;
346 } else {
71709bcd 347
348 /* Allow initial try of "none" auth without failure penalty */
349 if (authctxt->attempt > 1 || strcmp(method, "none") != 0)
350 authctxt->failures++;
351 if (authctxt->failures >= options.max_authtries) {
6039eeef 352#ifdef SSH_AUDIT_EVENTS
353 PRIVSEP(audit_event(SSH_LOGIN_EXCEED_MAXTRIES));
c00e4d75 354#endif
4e81159c 355 packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
c00e4d75 356 }
4e81159c 357 methods = authmethods_get();
358 packet_start(SSH2_MSG_USERAUTH_FAILURE);
359 packet_put_cstring(methods);
360 packet_put_char(0); /* XXX partial success, unused */
361 packet_send();
362 packet_write_wait();
363 xfree(methods);
364 }
94ec8c6b 365}
366
ffb215be 367static char *
94ec8c6b 368authmethods_get(void)
a306f2dd 369{
3469eac4 370 Buffer b;
94ec8c6b 371 char *list;
01c24737 372 int i;
94ec8c6b 373
3469eac4 374 buffer_init(&b);
01c24737 375 for (i = 0; authmethods[i] != NULL; i++) {
376 if (strcmp(authmethods[i]->name, "none") == 0)
94ec8c6b 377 continue;
01c24737 378 if (authmethods[i]->enabled != NULL &&
379 *(authmethods[i]->enabled) != 0) {
3469eac4 380 if (buffer_len(&b) > 0)
381 buffer_append(&b, ",", 1);
01c24737 382 buffer_append(&b, authmethods[i]->name,
383 strlen(authmethods[i]->name));
a306f2dd 384 }
385 }
3469eac4 386 buffer_append(&b, "\0", 1);
387 list = xstrdup(buffer_ptr(&b));
388 buffer_free(&b);
94ec8c6b 389 return list;
390}
391
396c147e 392static Authmethod *
94ec8c6b 393authmethod_lookup(const char *name)
394{
01c24737 395 int i;
396
94ec8c6b 397 if (name != NULL)
01c24737 398 for (i = 0; authmethods[i] != NULL; i++)
399 if (authmethods[i]->enabled != NULL &&
400 *(authmethods[i]->enabled) != 0 &&
401 strcmp(name, authmethods[i]->name) == 0)
402 return authmethods[i];
403 debug2("Unrecognized authentication method name: %s",
404 name ? name : "NULL");
94ec8c6b 405 return NULL;
a306f2dd 406}
221fc73c 407
This page took 0.273199 seconds and 5 git commands to generate.