]> andersk Git - gssapi-openssh.git/blame - openssh/auth2.c
merged OpenSSH 5.3p1 to trunk
[gssapi-openssh.git] / openssh / auth2.c
CommitLineData
b5afdff5 1/* $OpenBSD: auth2.c,v 1.121 2009/06/22 05:39:28 dtucker 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
30460aeb 28#include <sys/types.h>
5156b1a1 29#include <sys/stat.h>
30#include <sys/uio.h>
30460aeb 31
5156b1a1 32#include <fcntl.h>
30460aeb 33#include <pwd.h>
34#include <stdarg.h>
35#include <string.h>
5156b1a1 36#include <unistd.h>
30460aeb 37
5156b1a1 38#include "atomicio.h"
b5afdff5 39#include "xmalloc.h"
30460aeb 40#include "ssh2.h"
3c0ef626 41#include "packet.h"
3c0ef626 42#include "log.h"
30460aeb 43#include "buffer.h"
3c0ef626 44#include "servconf.h"
bddc9ab0 45#include "compat.h"
30460aeb 46#include "key.h"
47#include "hostfile.h"
3c0ef626 48#include "auth.h"
3c0ef626 49#include "dispatch.h"
3c0ef626 50#include "pathnames.h"
dfddba3d 51#include "buffer.h"
fa7499cc 52#include "canohost.h"
3c0ef626 53
5598e598 54#ifdef GSSAPI
55#include "ssh-gss.h"
56#endif
30460aeb 57#include "monitor_wrap.h"
5598e598 58
3c0ef626 59/* import */
60extern ServerOptions options;
61extern u_char *session_id2;
7cac2b65 62extern u_int session_id2_len;
dfddba3d 63extern Buffer loginmsg;
3c0ef626 64
44a053a3 65/* methods */
66
67extern Authmethod method_none;
44a053a3 68extern Authmethod method_pubkey;
69extern Authmethod method_passwd;
70extern Authmethod method_kbdint;
71extern Authmethod method_hostbased;
db870295 72#ifdef GSSAPI
73extern Authmethod method_external;
fe4ad273 74extern Authmethod method_gsskeyex;
db870295 75extern Authmethod method_gssapi;
540d72c3 76extern Authmethod method_gssapi_compat;
db870295 77#endif
5262cbfb 78#ifdef JPAKE
79extern Authmethod method_jpake;
80#endif
44a053a3 81
fa7499cc 82static int log_flag = 0;
83
84
44a053a3 85Authmethod *authmethods[] = {
86 &method_none,
44a053a3 87 &method_pubkey,
7cac2b65 88#ifdef GSSAPI
fe4ad273 89 &method_gsskeyex,
540d72c3 90 &method_external,
7cac2b65 91 &method_gssapi,
540d72c3 92 &method_gssapi_compat,
5262cbfb 93#endif
94#ifdef JPAKE
95 &method_jpake,
7cac2b65 96#endif
44a053a3 97 &method_passwd,
98 &method_kbdint,
99 &method_hostbased,
100 NULL
3c0ef626 101};
102
103/* protocol */
104
1e608e42 105static void input_service_request(int, u_int32_t, void *);
106static void input_userauth_request(int, u_int32_t, void *);
3c0ef626 107
108/* helper */
109static Authmethod *authmethod_lookup(const char *);
110static char *authmethods_get(void);
3c0ef626 111
5156b1a1 112char *
113auth2_read_banner(void)
114{
115 struct stat st;
116 char *banner = NULL;
117 size_t len, n;
118 int fd;
119
120 if ((fd = open(options.banner, O_RDONLY)) == -1)
121 return (NULL);
122 if (fstat(fd, &st) == -1) {
123 close(fd);
124 return (NULL);
125 }
126 if (st.st_size > 1*1024*1024) {
127 close(fd);
128 return (NULL);
129 }
130
131 len = (size_t)st.st_size; /* truncate */
132 banner = xmalloc(len + 1);
133 n = atomicio(read, fd, banner, len);
134 close(fd);
135
136 if (n != len) {
137 xfree(banner);
138 return (NULL);
139 }
140 banner[n] = '\0';
141
142 return (banner);
143}
144
145void
146userauth_send_banner(const char *msg)
147{
148 if (datafellows & SSH_BUG_BANNER)
149 return;
150
151 packet_start(SSH2_MSG_USERAUTH_BANNER);
152 packet_put_cstring(msg);
153 packet_put_cstring(""); /* language, unused */
154 packet_send();
155 debug("%s: sent", __func__);
156}
157
158static void
159userauth_banner(void)
160{
161 char *banner = NULL;
162
163 if (options.banner == NULL ||
164 strcasecmp(options.banner, "none") == 0 ||
165 (datafellows & SSH_BUG_BANNER) != 0)
166 return;
167
168 if ((banner = PRIVSEP(auth2_read_banner())) == NULL)
169 goto done;
170 userauth_send_banner(banner);
171
172done:
173 if (banner)
174 xfree(banner);
175}
176
3c0ef626 177/*
178 * loop until authctxt->success == TRUE
179 */
540d72c3 180void
181do_authentication2(Authctxt *authctxt)
3c0ef626 182{
1e608e42 183 dispatch_init(&dispatch_protocol_error);
3c0ef626 184 dispatch_set(SSH2_MSG_SERVICE_REQUEST, &input_service_request);
185 dispatch_run(DISPATCH_BLOCK, &authctxt->success, authctxt);
3c0ef626 186}
187
30460aeb 188/*ARGSUSED*/
3c0ef626 189static void
1e608e42 190input_service_request(int type, u_int32_t seq, void *ctxt)
3c0ef626 191{
192 Authctxt *authctxt = ctxt;
193 u_int len;
d03f4262 194 int acceptit = 0;
3c0ef626 195 char *service = packet_get_string(&len);
1e608e42 196 packet_check_eom();
3c0ef626 197
198 if (authctxt == NULL)
199 fatal("input_service_request: no authctxt");
200
201 if (strcmp(service, "ssh-userauth") == 0) {
202 if (!authctxt->success) {
d03f4262 203 acceptit = 1;
3c0ef626 204 /* now we can handle user-auth requests */
205 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &input_userauth_request);
206 }
207 }
208 /* XXX all other service requests are denied */
209
d03f4262 210 if (acceptit) {
3c0ef626 211 packet_start(SSH2_MSG_SERVICE_ACCEPT);
212 packet_put_cstring(service);
213 packet_send();
214 packet_write_wait();
215 } else {
216 debug("bad service request %s", service);
217 packet_disconnect("bad service request %s", service);
218 }
219 xfree(service);
220}
221
30460aeb 222/*ARGSUSED*/
3c0ef626 223static void
1e608e42 224input_userauth_request(int type, u_int32_t seq, void *ctxt)
3c0ef626 225{
226 Authctxt *authctxt = ctxt;
227 Authmethod *m = NULL;
228 char *user, *service, *method, *style = NULL;
229 int authenticated = 0;
230
231 if (authctxt == NULL)
232 fatal("input_userauth_request: no authctxt");
233
234 user = packet_get_string(NULL);
235 service = packet_get_string(NULL);
236 method = packet_get_string(NULL);
73a68d83 237
305d07c0 238#ifdef GSSAPI
8f3eb4db 239 if (user[0] == '\0') {
b8e8844a 240 debug("received empty username for %s", method);
fe4ad273 241 if (strcmp(method, "external-keyx") == 0 ||
242 strcmp(method, "gssapi-keyex") == 0) {
b8e8844a 243 char *lname = NULL;
244 PRIVSEP(ssh_gssapi_localname(&lname));
245 if (lname && lname[0] != '\0') {
246 xfree(user);
247 user = lname;
248 debug("set username to %s from gssapi context", user);
9ec9d5ad 249 } else {
b8e8844a 250 debug("failed to set username from gssapi context");
d9e17760 251 packet_send_debug("failed to set username from gssapi context");
b8e8844a 252 }
b59afbfe 253 }
254 }
255#endif
73a68d83 256
b8e8844a 257 debug("userauth-request for user %s service %s method %s",
8f3eb4db 258 user[0] ? user : "<implicit>", service, method);
fa7499cc 259 if (!log_flag) {
260 logit("SSH: Server;Ltype: Authname;Remote: %s-%d;Name: %s",
d3057ca4 261 get_remote_ipaddr(), get_remote_port(),
262 user[0] ? user : "<implicit>");
fa7499cc 263 log_flag = 1;
264 }
3c0ef626 265 debug("attempt %d failures %d", authctxt->attempt, authctxt->failures);
266
267 if ((style = strchr(user, ':')) != NULL)
268 *style++ = 0;
269
86a22a0a 270 /* If first time or username changed or empty username,
781f72b9 271 setup/reset authentication context. */
272 if ((authctxt->attempt++ == 0) ||
273 (strcmp(user, authctxt->user) != 0) ||
274 (strcmp(user, "") == 0)) {
b59afbfe 275 if (authctxt->user) {
276 xfree(authctxt->user);
277 authctxt->user = NULL;
278 }
b809a58e 279 authctxt->valid = 0;
86a22a0a 280 authctxt->user = xstrdup(user);
281 if (strcmp(service, "ssh-connection") != 0) {
282 packet_disconnect("Unsupported service %s", service);
283 }
b8e8844a 284#ifdef GSSAPI
9ec9d5ad 285 /* If we're going to set the username based on the
286 GSSAPI context later, then wait until then to
8f3eb4db 287 verify it. Just put in placeholders for now. */
288 if ((strcmp(user, "") == 0) &&
289 ((strcmp(method, "gssapi") == 0) ||
290 (strcmp(method, "gssapi-with-mic") == 0))) {
291 authctxt->pw = fakepw();
8f3eb4db 292 } else {
b8e8844a 293#endif
510132b6 294 authctxt->pw = PRIVSEP(getpwnamallow(user));
86a22a0a 295 if (authctxt->pw) {
3c0ef626 296 authctxt->valid = 1;
297 debug2("input_userauth_request: setting up authctxt for %s", user);
3c0ef626 298 } else {
7e82606e 299 logit("input_userauth_request: invalid user %s", user);
7cac2b65 300 authctxt->pw = fakepw();
dfddba3d 301#ifdef SSH_AUDIT_EVENTS
302 PRIVSEP(audit_event(SSH_INVALID_USER));
3c0ef626 303#endif
304 }
b8e8844a 305#ifdef GSSAPI
9ec9d5ad 306 } /* endif for setting username based on GSSAPI context */
08822d99 307#endif
308#ifdef USE_PAM
309 if (options.use_pam)
310 PRIVSEP(start_pam(authctxt));
b8e8844a 311#endif
7e82606e 312 setproctitle("%s%s", authctxt->valid ? user : "unknown",
510132b6 313 use_privsep ? " [net]" : "");
88ca8dfd 314 if (authctxt->attempt == 1) {
86a22a0a 315 authctxt->service = xstrdup(service);
316 authctxt->style = style ? xstrdup(style) : NULL;
317 if (use_privsep)
318 mm_inform_authserv(service, style);
5156b1a1 319 userauth_banner();
86a22a0a 320 }
885ffc2b 321 }
322 if (strcmp(service, authctxt->service) != 0) {
67b84e65 323 packet_disconnect("Change of service not allowed: "
324 "(%s,%s) -> (%s,%s)",
325 authctxt->user, authctxt->service, user, service);
3c0ef626 326 }
327 /* reset state */
1e608e42 328 auth2_challenge_stop(authctxt);
5262cbfb 329#ifdef JPAKE
330 auth2_jpake_stop(authctxt);
331#endif
5598e598 332
333#ifdef GSSAPI
5262cbfb 334 /* XXX move to auth2_gssapi_stop() */
1e608e42 335 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
336 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, NULL);
5598e598 337#endif
338
3c0ef626 339 authctxt->postponed = 0;
fe4ad273 340 authctxt->server_caused_failure = 0;
3c0ef626 341
342 /* try to authenticate user */
343 m = authmethod_lookup(method);
5156b1a1 344 if (m != NULL && authctxt->failures < options.max_authtries) {
3c0ef626 345 debug2("input_userauth_request: try method %s", method);
346 authenticated = m->userauth(authctxt);
347 }
348 userauth_finish(authctxt, authenticated, method);
349
350 xfree(service);
351 xfree(user);
352 xfree(method);
353}
354
355void
356userauth_finish(Authctxt *authctxt, int authenticated, char *method)
357{
358 char *methods;
359
360 if (!authctxt->valid && authenticated)
361 fatal("INTERNAL ERROR: authenticated invalid user %s",
362 authctxt->user);
363
364 /* Special handling for root */
bfe49944 365 if (authenticated && authctxt->pw->pw_uid == 0 &&
dfddba3d 366 !auth_root_allowed(method)) {
3c0ef626 367 authenticated = 0;
dfddba3d 368#ifdef SSH_AUDIT_EVENTS
369 PRIVSEP(audit_event(SSH_LOGIN_ROOT_DENIED));
370#endif
371 }
3c0ef626 372
373#ifdef USE_PAM
dfddba3d 374 if (options.use_pam && authenticated) {
375 if (!PRIVSEP(do_pam_account())) {
376 /* if PAM returned a message, send it to the user */
377 if (buffer_len(&loginmsg) > 0) {
378 buffer_append(&loginmsg, "\0", 1);
379 userauth_send_banner(buffer_ptr(&loginmsg));
380 packet_write_wait();
381 }
382 fatal("Access denied for user %s by PAM account "
2ce0bfe4 383 "configuration", authctxt->user);
dfddba3d 384 }
385 }
7cac2b65 386#endif
3c0ef626 387
d03f4262 388#ifdef _UNICOS
389 if (authenticated && cray_access_denied(authctxt->user)) {
390 authenticated = 0;
391 fatal("Access denied for user %s.",authctxt->user);
392 }
393#endif /* _UNICOS */
394
3c0ef626 395 /* Log before sending the reply */
396 auth_log(authctxt, authenticated, method, " ssh2");
397
398 if (authctxt->postponed)
399 return;
400
401 /* XXX todo: check if multiple auth methods are needed */
402 if (authenticated == 1) {
403 /* turn off userauth */
1e608e42 404 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &dispatch_protocol_ignore);
3c0ef626 405 packet_start(SSH2_MSG_USERAUTH_SUCCESS);
406 packet_send();
407 packet_write_wait();
408 /* now we can break out */
409 authctxt->success = 1;
410 } else {
fe4ad273 411 /* Dont count server configuration issues against the client */
5156b1a1 412 /* Allow initial try of "none" auth without failure penalty */
413 if (!authctxt->server_caused_failure &&
f97edba6 414 (authctxt->attempt > 1 || strcmp(method, "none") != 0))
5156b1a1 415 authctxt->failures++;
416 if (authctxt->failures >= options.max_authtries) {
dfddba3d 417#ifdef SSH_AUDIT_EVENTS
418 PRIVSEP(audit_event(SSH_LOGIN_EXCEED_MAXTRIES));
419#endif
3c0ef626 420 packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
dfddba3d 421 }
3c0ef626 422 methods = authmethods_get();
423 packet_start(SSH2_MSG_USERAUTH_FAILURE);
424 packet_put_cstring(methods);
425 packet_put_char(0); /* XXX partial success, unused */
426 packet_send();
427 packet_write_wait();
428 xfree(methods);
429 }
430}
431
3c0ef626 432static char *
433authmethods_get(void)
434{
1e608e42 435 Buffer b;
3c0ef626 436 char *list;
44a053a3 437 int i;
3c0ef626 438
1e608e42 439 buffer_init(&b);
44a053a3 440 for (i = 0; authmethods[i] != NULL; i++) {
441 if (strcmp(authmethods[i]->name, "none") == 0)
3c0ef626 442 continue;
44a053a3 443 if (authmethods[i]->enabled != NULL &&
444 *(authmethods[i]->enabled) != 0) {
1e608e42 445 if (buffer_len(&b) > 0)
446 buffer_append(&b, ",", 1);
44a053a3 447 buffer_append(&b, authmethods[i]->name,
448 strlen(authmethods[i]->name));
3c0ef626 449 }
450 }
1e608e42 451 buffer_append(&b, "\0", 1);
452 list = xstrdup(buffer_ptr(&b));
453 buffer_free(&b);
3c0ef626 454 return list;
455}
456
457static Authmethod *
458authmethod_lookup(const char *name)
459{
44a053a3 460 int i;
461
3c0ef626 462 if (name != NULL)
44a053a3 463 for (i = 0; authmethods[i] != NULL; i++)
464 if (authmethods[i]->enabled != NULL &&
465 *(authmethods[i]->enabled) != 0 &&
466 strcmp(name, authmethods[i]->name) == 0)
467 return authmethods[i];
468 debug2("Unrecognized authentication method name: %s",
469 name ? name : "NULL");
3c0ef626 470 return NULL;
471}
5156b1a1 472
This page took 0.240052 seconds and 5 git commands to generate.