]> andersk Git - openssh.git/blame - auth2.c
- djm@cvs.openbsd.org 2008/07/02 12:36:39
[openssh.git] / auth2.c
CommitLineData
221fc73c 1/* $OpenBSD: auth2.c,v 1.117 2008/07/02 12:36:39 djm 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
a306f2dd 38#include "xmalloc.h"
31652869 39#include "ssh2.h"
a306f2dd 40#include "packet.h"
42f11eb2 41#include "log.h"
31652869 42#include "buffer.h"
a306f2dd 43#include "servconf.h"
44#include "compat.h"
31652869 45#include "key.h"
46#include "hostfile.h"
a306f2dd 47#include "auth.h"
a306f2dd 48#include "dispatch.h"
42f11eb2 49#include "pathnames.h"
ba6dd90e 50#include "buffer.h"
a306f2dd 51
7364bd04 52#ifdef GSSAPI
53#include "ssh-gss.h"
54#endif
31652869 55#include "monitor_wrap.h"
7364bd04 56
a306f2dd 57/* import */
58extern ServerOptions options;
1e3b8b07 59extern u_char *session_id2;
a27002e5 60extern u_int session_id2_len;
ba6dd90e 61extern Buffer loginmsg;
a306f2dd 62
01c24737 63/* methods */
64
65extern Authmethod method_none;
66extern Authmethod method_pubkey;
67extern Authmethod method_passwd;
68extern Authmethod method_kbdint;
69extern Authmethod method_hostbased;
7364bd04 70#ifdef GSSAPI
71extern Authmethod method_gssapi;
72#endif
01c24737 73
74Authmethod *authmethods[] = {
75 &method_none,
76 &method_pubkey,
7364bd04 77#ifdef GSSAPI
78 &method_gssapi,
79#endif
01c24737 80 &method_passwd,
81 &method_kbdint,
82 &method_hostbased,
83 NULL
94ec8c6b 84};
85
a306f2dd 86/* protocol */
87
7819b5c3 88static void input_service_request(int, u_int32_t, void *);
89static void input_userauth_request(int, u_int32_t, void *);
a306f2dd 90
a306f2dd 91/* helper */
396c147e 92static Authmethod *authmethod_lookup(const char *);
ffb215be 93static char *authmethods_get(void);
a306f2dd 94
221fc73c 95char *
96auth2_read_banner(void)
97{
98 struct stat st;
99 char *banner = NULL;
100 size_t len, n;
101 int fd;
102
103 if ((fd = open(options.banner, O_RDONLY)) == -1)
104 return (NULL);
105 if (fstat(fd, &st) == -1) {
106 close(fd);
107 return (NULL);
108 }
109 if (st.st_size > 1*1024*1024) {
110 close(fd);
111 return (NULL);
112 }
113
114 len = (size_t)st.st_size; /* truncate */
115 banner = xmalloc(len + 1);
116 n = atomicio(read, fd, banner, len);
117 close(fd);
118
119 if (n != len) {
120 xfree(banner);
121 return (NULL);
122 }
123 banner[n] = '\0';
124
125 return (banner);
126}
127
128void
129userauth_send_banner(const char *msg)
130{
131 if (datafellows & SSH_BUG_BANNER)
132 return;
133
134 packet_start(SSH2_MSG_USERAUTH_BANNER);
135 packet_put_cstring(msg);
136 packet_put_cstring(""); /* language, unused */
137 packet_send();
138 debug("%s: sent", __func__);
139}
140
141static void
142userauth_banner(void)
143{
144 char *banner = NULL;
145
146 if (options.banner == NULL ||
147 strcasecmp(options.banner, "none") == 0 ||
148 (datafellows & SSH_BUG_BANNER) != 0)
149 return;
150
151 if ((banner = PRIVSEP(auth2_read_banner())) == NULL)
152 goto done;
153 userauth_send_banner(banner);
154
155done:
156 if (banner)
157 xfree(banner);
158}
159
a306f2dd 160/*
94ec8c6b 161 * loop until authctxt->success == TRUE
a306f2dd 162 */
2362db19 163void
164do_authentication2(Authctxt *authctxt)
a306f2dd 165{
6367063f 166 dispatch_init(&dispatch_protocol_error);
a306f2dd 167 dispatch_set(SSH2_MSG_SERVICE_REQUEST, &input_service_request);
94ec8c6b 168 dispatch_run(DISPATCH_BLOCK, &authctxt->success, authctxt);
a306f2dd 169}
170
fe8c3af1 171/*ARGSUSED*/
396c147e 172static void
7819b5c3 173input_service_request(int type, u_int32_t seq, void *ctxt)
a306f2dd 174{
94ec8c6b 175 Authctxt *authctxt = ctxt;
1e3b8b07 176 u_int len;
7fdc56c5 177 int acceptit = 0;
a306f2dd 178 char *service = packet_get_string(&len);
95500969 179 packet_check_eom();
a306f2dd 180
94ec8c6b 181 if (authctxt == NULL)
182 fatal("input_service_request: no authctxt");
183
a306f2dd 184 if (strcmp(service, "ssh-userauth") == 0) {
94ec8c6b 185 if (!authctxt->success) {
7fdc56c5 186 acceptit = 1;
a306f2dd 187 /* now we can handle user-auth requests */
188 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &input_userauth_request);
189 }
190 }
191 /* XXX all other service requests are denied */
192
7fdc56c5 193 if (acceptit) {
a306f2dd 194 packet_start(SSH2_MSG_SERVICE_ACCEPT);
195 packet_put_cstring(service);
196 packet_send();
197 packet_write_wait();
198 } else {
199 debug("bad service request %s", service);
200 packet_disconnect("bad service request %s", service);
201 }
202 xfree(service);
203}
204
fe8c3af1 205/*ARGSUSED*/
396c147e 206static void
7819b5c3 207input_userauth_request(int type, u_int32_t seq, void *ctxt)
a306f2dd 208{
94ec8c6b 209 Authctxt *authctxt = ctxt;
210 Authmethod *m = NULL;
59c97189 211 char *user, *service, *method, *style = NULL;
a306f2dd 212 int authenticated = 0;
a306f2dd 213
94ec8c6b 214 if (authctxt == NULL)
215 fatal("input_userauth_request: no authctxt");
a306f2dd 216
94ec8c6b 217 user = packet_get_string(NULL);
218 service = packet_get_string(NULL);
219 method = packet_get_string(NULL);
220 debug("userauth-request for user %s service %s method %s", user, service, method);
8abcdba4 221 debug("attempt %d failures %d", authctxt->attempt, authctxt->failures);
94ec8c6b 222
59c97189 223 if ((style = strchr(user, ':')) != NULL)
224 *style++ = 0;
225
2b87da3b 226 if (authctxt->attempt++ == 0) {
63284fbb 227 /* setup auth context */
f7ed12f1 228 authctxt->pw = PRIVSEP(getpwnamallow(user));
529d73ab 229 authctxt->user = xstrdup(user);
f7ed12f1 230 if (authctxt->pw && strcmp(service, "ssh-connection")==0) {
94ec8c6b 231 authctxt->valid = 1;
232 debug2("input_userauth_request: setting up authctxt for %s", user);
94ec8c6b 233 } else {
d77347cc 234 logit("input_userauth_request: invalid user %s", user);
96d0bf74 235 authctxt->pw = fakepw();
6039eeef 236#ifdef SSH_AUDIT_EVENTS
237 PRIVSEP(audit_event(SSH_INVALID_USER));
04fc7a67 238#endif
94ec8c6b 239 }
b6c37221 240#ifdef USE_PAM
241 if (options.use_pam)
242 PRIVSEP(start_pam(authctxt));
243#endif
bd5c0694 244 setproctitle("%s%s", authctxt->valid ? user : "unknown",
1853d1ef 245 use_privsep ? " [net]" : "");
94ec8c6b 246 authctxt->service = xstrdup(service);
2a6a054e 247 authctxt->style = style ? xstrdup(style) : NULL;
1853d1ef 248 if (use_privsep)
249 mm_inform_authserv(service, style);
221fc73c 250 userauth_banner();
2a6a054e 251 } else if (strcmp(user, authctxt->user) != 0 ||
252 strcmp(service, authctxt->service) != 0) {
253 packet_disconnect("Change of username or service not allowed: "
254 "(%s,%s) -> (%s,%s)",
255 authctxt->user, authctxt->service, user, service);
a306f2dd 256 }
59c97189 257 /* reset state */
2f293d43 258 auth2_challenge_stop(authctxt);
7364bd04 259
260#ifdef GSSAPI
261 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
262 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, NULL);
263#endif
264
59c97189 265 authctxt->postponed = 0;
9cd45ea4 266
59c97189 267 /* try to authenticate user */
94ec8c6b 268 m = authmethod_lookup(method);
221fc73c 269 if (m != NULL && authctxt->failures < options.max_authtries) {
94ec8c6b 270 debug2("input_userauth_request: try method %s", method);
271 authenticated = m->userauth(authctxt);
9cd45ea4 272 }
d9cd3575 273 userauth_finish(authctxt, authenticated, method);
274
275 xfree(service);
276 xfree(user);
277 xfree(method);
278}
279
280void
281userauth_finish(Authctxt *authctxt, int authenticated, char *method)
282{
4e81159c 283 char *methods;
284
59c97189 285 if (!authctxt->valid && authenticated)
286 fatal("INTERNAL ERROR: authenticated invalid user %s",
287 authctxt->user);
9cd45ea4 288
94ec8c6b 289 /* Special handling for root */
5d47f1d4 290 if (authenticated && authctxt->pw->pw_uid == 0 &&
c00e4d75 291 !auth_root_allowed(method)) {
a306f2dd 292 authenticated = 0;
6039eeef 293#ifdef SSH_AUDIT_EVENTS
294 PRIVSEP(audit_event(SSH_LOGIN_ROOT_DENIED));
c00e4d75 295#endif
296 }
a306f2dd 297
5b9e2464 298#ifdef USE_PAM
ba6dd90e 299 if (options.use_pam && authenticated) {
300 if (!PRIVSEP(do_pam_account())) {
ba6dd90e 301 /* if PAM returned a message, send it to the user */
302 if (buffer_len(&loginmsg) > 0) {
303 buffer_append(&loginmsg, "\0", 1);
304 userauth_send_banner(buffer_ptr(&loginmsg));
9aab0af7 305 packet_write_wait();
ba6dd90e 306 }
9aab0af7 307 fatal("Access denied for user %s by PAM account "
98c044d0 308 "configuration", authctxt->user);
ba6dd90e 309 }
310 }
5b9e2464 311#endif
312
ef51930f 313#ifdef _UNICOS
314 if (authenticated && cray_access_denied(authctxt->user)) {
315 authenticated = 0;
316 fatal("Access denied for user %s.",authctxt->user);
317 }
318#endif /* _UNICOS */
319
94ec8c6b 320 /* Log before sending the reply */
59c97189 321 auth_log(authctxt, authenticated, method, " ssh2");
322
4e81159c 323 if (authctxt->postponed)
324 return;
325
326 /* XXX todo: check if multiple auth methods are needed */
327 if (authenticated == 1) {
328 /* turn off userauth */
6367063f 329 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &dispatch_protocol_ignore);
4e81159c 330 packet_start(SSH2_MSG_USERAUTH_SUCCESS);
331 packet_send();
332 packet_write_wait();
333 /* now we can break out */
334 authctxt->success = 1;
335 } else {
221fc73c 336 if (++authctxt->failures > options.max_authtries) {
6039eeef 337#ifdef SSH_AUDIT_EVENTS
338 PRIVSEP(audit_event(SSH_LOGIN_EXCEED_MAXTRIES));
c00e4d75 339#endif
4e81159c 340 packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
c00e4d75 341 }
4e81159c 342 methods = authmethods_get();
343 packet_start(SSH2_MSG_USERAUTH_FAILURE);
344 packet_put_cstring(methods);
345 packet_put_char(0); /* XXX partial success, unused */
346 packet_send();
347 packet_write_wait();
348 xfree(methods);
349 }
94ec8c6b 350}
351
ffb215be 352static char *
94ec8c6b 353authmethods_get(void)
a306f2dd 354{
3469eac4 355 Buffer b;
94ec8c6b 356 char *list;
01c24737 357 int i;
94ec8c6b 358
3469eac4 359 buffer_init(&b);
01c24737 360 for (i = 0; authmethods[i] != NULL; i++) {
361 if (strcmp(authmethods[i]->name, "none") == 0)
94ec8c6b 362 continue;
01c24737 363 if (authmethods[i]->enabled != NULL &&
364 *(authmethods[i]->enabled) != 0) {
3469eac4 365 if (buffer_len(&b) > 0)
366 buffer_append(&b, ",", 1);
01c24737 367 buffer_append(&b, authmethods[i]->name,
368 strlen(authmethods[i]->name));
a306f2dd 369 }
370 }
3469eac4 371 buffer_append(&b, "\0", 1);
372 list = xstrdup(buffer_ptr(&b));
373 buffer_free(&b);
94ec8c6b 374 return list;
375}
376
396c147e 377static Authmethod *
94ec8c6b 378authmethod_lookup(const char *name)
379{
01c24737 380 int i;
381
94ec8c6b 382 if (name != NULL)
01c24737 383 for (i = 0; authmethods[i] != NULL; i++)
384 if (authmethods[i]->enabled != NULL &&
385 *(authmethods[i]->enabled) != 0 &&
386 strcmp(name, authmethods[i]->name) == 0)
387 return authmethods[i];
388 debug2("Unrecognized authentication method name: %s",
389 name ? name : "NULL");
94ec8c6b 390 return NULL;
a306f2dd 391}
221fc73c 392
This page took 4.100413 seconds and 5 git commands to generate.