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