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