1 /* $NetBSD: auth2.c,v 1.1.1.2 2009/12/27 01:06:47 christos Exp $ */
2 /* $OpenBSD: auth2.c,v 1.121 2009/06/22 05:39:28 dtucker Exp $ */
4 * Copyright (c) 2000 Markus Friedl. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 __RCSID("$NetBSD: auth2.c,v 1.2 2009/06/07 22:38:46 christos Exp $");
29 #include <sys/types.h>
51 #include "pathnames.h"
59 #include "monitor_wrap.h"
62 extern ServerOptions options
;
63 extern u_char
*session_id2
;
64 extern u_int session_id2_len
;
65 extern Buffer loginmsg
;
69 extern Authmethod method_none
;
70 extern Authmethod method_pubkey
;
71 extern Authmethod method_passwd
;
72 extern Authmethod method_kbdint
;
73 extern Authmethod method_hostbased
;
75 extern Authmethod method_kerberos
;
78 extern Authmethod method_gssapi
;
81 extern Authmethod method_jpake
;
84 static int log_flag
= 0;
86 Authmethod
*authmethods
[] = {
106 static void input_service_request(int, u_int32_t
, void *);
107 static void input_userauth_request(int, u_int32_t
, void *);
110 static Authmethod
*authmethod_lookup(const char *);
111 static char *authmethods_get(void);
114 auth2_read_banner(void)
121 if ((fd
= open(options
.banner
, O_RDONLY
)) == -1)
123 if (fstat(fd
, &st
) == -1) {
127 if (st
.st_size
> 1*1024*1024) {
132 len
= (size_t)st
.st_size
; /* truncate */
133 banner
= xmalloc(len
+ 1);
134 n
= atomicio(read
, fd
, banner
, len
);
147 userauth_send_banner(const char *msg
)
149 if (datafellows
& SSH_BUG_BANNER
)
152 packet_start(SSH2_MSG_USERAUTH_BANNER
);
153 packet_put_cstring(msg
);
154 packet_put_cstring(""); /* language, unused */
156 debug("%s: sent", __func__
);
160 userauth_banner(void)
164 if (options
.banner
== NULL
||
165 strcasecmp(options
.banner
, "none") == 0 ||
166 (datafellows
& SSH_BUG_BANNER
) != 0)
169 if ((banner
= PRIVSEP(auth2_read_banner())) == NULL
)
172 userauth_send_banner(banner
);
179 * loop until authctxt->success == TRUE
182 do_authentication2(Authctxt
*authctxt
)
184 dispatch_init(&dispatch_protocol_error
);
185 dispatch_set(SSH2_MSG_SERVICE_REQUEST
, &input_service_request
);
186 dispatch_run(DISPATCH_BLOCK
, &authctxt
->success
, authctxt
);
191 input_service_request(int type
, u_int32_t seq
, void *ctxt
)
193 Authctxt
*authctxt
= ctxt
;
196 char *service
= packet_get_string(&len
);
199 if (authctxt
== NULL
)
200 fatal("input_service_request: no authctxt");
202 if (strcmp(service
, "ssh-userauth") == 0) {
203 if (!authctxt
->success
) {
205 /* now we can handle user-auth requests */
206 dispatch_set(SSH2_MSG_USERAUTH_REQUEST
, &input_userauth_request
);
209 /* XXX all other service requests are denied */
212 packet_start(SSH2_MSG_SERVICE_ACCEPT
);
213 packet_put_cstring(service
);
217 debug("bad service request %s", service
);
218 packet_disconnect("bad service request %s", service
);
225 input_userauth_request(int type
, u_int32_t seq
, void *ctxt
)
227 Authctxt
*authctxt
= ctxt
;
228 Authmethod
*m
= NULL
;
229 char *user
, *service
, *method
, *style
= NULL
;
230 int authenticated
= 0;
232 if (authctxt
== NULL
)
233 fatal("input_userauth_request: no authctxt");
235 user
= packet_get_string(NULL
);
236 service
= packet_get_string(NULL
);
237 method
= packet_get_string(NULL
);
238 debug("userauth-request for user %s service %s method %s", user
, service
, method
);
240 logit("SSH: Server;Ltype: Authname;Remote: %s-%d;Name: %s",
241 get_remote_ipaddr(), get_remote_port(), user
);
244 debug("attempt %d failures %d", authctxt
->attempt
, authctxt
->failures
);
246 if ((style
= strchr(user
, ':')) != NULL
)
249 if (authctxt
->attempt
++ == 0) {
250 /* setup auth context */
251 authctxt
->pw
= PRIVSEP(getpwnamallow(user
));
252 authctxt
->user
= xstrdup(user
);
253 if (authctxt
->pw
&& strcmp(service
, "ssh-connection")==0) {
255 debug2("input_userauth_request: setting up authctxt for %s", user
);
257 logit("input_userauth_request: invalid user %s", user
);
258 authctxt
->pw
= fakepw();
262 PRIVSEP(start_pam(authctxt
));
264 setproctitle("%s%s", authctxt
->valid
? user
: "unknown",
265 use_privsep
? " [net]" : "");
266 authctxt
->service
= xstrdup(service
);
267 authctxt
->style
= style
? xstrdup(style
) : NULL
;
269 mm_inform_authserv(service
, style
);
271 } else if (strcmp(user
, authctxt
->user
) != 0 ||
272 strcmp(service
, authctxt
->service
) != 0) {
273 packet_disconnect("Change of username or service not allowed: "
274 "(%s,%s) -> (%s,%s)",
275 authctxt
->user
, authctxt
->service
, user
, service
);
278 auth2_challenge_stop(authctxt
);
280 auth2_jpake_stop(authctxt
);
284 /* XXX move to auth2_gssapi_stop() */
285 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN
, NULL
);
286 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE
, NULL
);
289 authctxt
->postponed
= 0;
291 /* try to authenticate user */
292 m
= authmethod_lookup(method
);
293 if (m
!= NULL
&& authctxt
->failures
< options
.max_authtries
) {
294 debug2("input_userauth_request: try method %s", method
);
295 authenticated
= m
->userauth(authctxt
);
297 userauth_finish(authctxt
, authenticated
, method
);
305 userauth_finish(Authctxt
*authctxt
, int authenticated
, char *method
)
309 if (!authctxt
->valid
&& authenticated
)
310 fatal("INTERNAL ERROR: authenticated invalid user %s",
313 /* Special handling for root */
314 if (authenticated
&& authctxt
->pw
->pw_uid
== 0 &&
315 !auth_root_allowed(method
)) {
317 #ifdef SSH_AUDIT_EVENTS
318 PRIVSEP(audit_event(SSH_LOGIN_ROOT_DENIED
));
323 if (options
.use_pam
&& authenticated
) {
324 if (!PRIVSEP(do_pam_account())) {
325 /* if PAM returned a message, send it to the user */
326 if (buffer_len(&loginmsg
) > 0) {
327 buffer_append(&loginmsg
, "\0", 1);
328 userauth_send_banner(buffer_ptr(&loginmsg
));
331 fatal("Access denied for user %s by PAM account "
332 "configuration", authctxt
->user
);
337 /* Log before sending the reply */
338 auth_log(authctxt
, authenticated
, method
, " ssh2");
340 if (authctxt
->postponed
)
343 /* XXX todo: check if multiple auth methods are needed */
344 if (authenticated
== 1) {
345 /* turn off userauth */
346 dispatch_set(SSH2_MSG_USERAUTH_REQUEST
, &dispatch_protocol_ignore
);
347 packet_start(SSH2_MSG_USERAUTH_SUCCESS
);
350 /* now we can break out */
351 authctxt
->success
= 1;
353 /* Allow initial try of "none" auth without failure penalty */
354 if (authctxt
->attempt
> 1 || strcmp(method
, "none") != 0)
355 authctxt
->failures
++;
356 if (authctxt
->failures
>= options
.max_authtries
) {
357 packet_disconnect(AUTH_FAIL_MSG
, authctxt
->user
);
358 #ifdef SSH_AUDIT_EVENTS
359 PRIVSEP(audit_event(SSH_LOGIN_EXCEED_MAXTRIES
));
362 methods
= authmethods_get();
363 packet_start(SSH2_MSG_USERAUTH_FAILURE
);
364 packet_put_cstring(methods
);
365 packet_put_char(0); /* XXX partial success, unused */
373 authmethods_get(void)
380 for (i
= 0; authmethods
[i
] != NULL
; i
++) {
381 if (strcmp(authmethods
[i
]->name
, "none") == 0)
383 if (authmethods
[i
]->enabled
!= NULL
&&
384 *(authmethods
[i
]->enabled
) != 0) {
385 if (buffer_len(&b
) > 0)
386 buffer_append(&b
, ",", 1);
387 buffer_append(&b
, authmethods
[i
]->name
,
388 strlen(authmethods
[i
]->name
));
391 buffer_append(&b
, "\0", 1);
392 list
= xstrdup(buffer_ptr(&b
));
398 authmethod_lookup(const char *name
)
403 for (i
= 0; authmethods
[i
] != NULL
; i
++)
404 if (authmethods
[i
]->enabled
!= NULL
&&
405 *(authmethods
[i
]->enabled
) != 0 &&
406 strcmp(name
, authmethods
[i
]->name
) == 0)
407 return authmethods
[i
];
408 debug2("Unrecognized authentication method name: %s",
409 name
? name
: "NULL");