1 /* $OpenBSD: auth2.c,v 1.120 2008/11/04 08:22:12 djm Exp $ */
3 * Copyright (c) 2000 Markus Friedl. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
28 #include <sys/types.h>
51 #include "pathnames.h"
57 #include "monitor_wrap.h"
60 extern ServerOptions options
;
61 extern u_char
*session_id2
;
62 extern u_int session_id2_len
;
63 extern Buffer loginmsg
;
67 extern Authmethod method_none
;
68 extern Authmethod method_pubkey
;
69 extern Authmethod method_passwd
;
70 extern Authmethod method_kbdint
;
71 extern Authmethod method_hostbased
;
73 extern Authmethod method_gssapi
;
76 extern Authmethod method_jpake
;
79 Authmethod
*authmethods
[] = {
96 static void input_service_request(int, u_int32_t
, void *);
97 static void input_userauth_request(int, u_int32_t
, void *);
100 static Authmethod
*authmethod_lookup(const char *);
101 static char *authmethods_get(void);
104 auth2_read_banner(void)
111 if ((fd
= open(options
.banner
, O_RDONLY
)) == -1)
113 if (fstat(fd
, &st
) == -1) {
117 if (st
.st_size
> 1*1024*1024) {
122 len
= (size_t)st
.st_size
; /* truncate */
123 banner
= xmalloc(len
+ 1);
124 n
= atomicio(read
, fd
, banner
, len
);
137 userauth_send_banner(const char *msg
)
139 if (datafellows
& SSH_BUG_BANNER
)
142 packet_start(SSH2_MSG_USERAUTH_BANNER
);
143 packet_put_cstring(msg
);
144 packet_put_cstring(""); /* language, unused */
146 debug("%s: sent", __func__
);
150 userauth_banner(void)
154 if (options
.banner
== NULL
||
155 strcasecmp(options
.banner
, "none") == 0 ||
156 (datafellows
& SSH_BUG_BANNER
) != 0)
159 if ((banner
= PRIVSEP(auth2_read_banner())) == NULL
)
161 userauth_send_banner(banner
);
169 * loop until authctxt->success == TRUE
172 do_authentication2(Authctxt
*authctxt
)
174 dispatch_init(&dispatch_protocol_error
);
175 dispatch_set(SSH2_MSG_SERVICE_REQUEST
, &input_service_request
);
176 dispatch_run(DISPATCH_BLOCK
, &authctxt
->success
, authctxt
);
181 input_service_request(int type
, u_int32_t seq
, void *ctxt
)
183 Authctxt
*authctxt
= ctxt
;
186 char *service
= packet_get_string(&len
);
189 if (authctxt
== NULL
)
190 fatal("input_service_request: no authctxt");
192 if (strcmp(service
, "ssh-userauth") == 0) {
193 if (!authctxt
->success
) {
195 /* now we can handle user-auth requests */
196 dispatch_set(SSH2_MSG_USERAUTH_REQUEST
, &input_userauth_request
);
199 /* XXX all other service requests are denied */
202 packet_start(SSH2_MSG_SERVICE_ACCEPT
);
203 packet_put_cstring(service
);
207 debug("bad service request %s", service
);
208 packet_disconnect("bad service request %s", service
);
215 input_userauth_request(int type
, u_int32_t seq
, void *ctxt
)
217 Authctxt
*authctxt
= ctxt
;
218 Authmethod
*m
= NULL
;
219 char *user
, *service
, *method
, *style
= NULL
;
220 int authenticated
= 0;
221 #ifdef HAVE_LOGIN_CAP
223 const char *from_host
, *from_ip
;
225 from_host
= get_canonical_hostname(options
.use_dns
);
226 from_ip
= get_remote_ipaddr();
229 if (authctxt
== NULL
)
230 fatal("input_userauth_request: no authctxt");
232 user
= packet_get_string(NULL
);
233 service
= packet_get_string(NULL
);
234 method
= packet_get_string(NULL
);
235 debug("userauth-request for user %s service %s method %s", user
, service
, method
);
236 debug("attempt %d failures %d", authctxt
->attempt
, authctxt
->failures
);
238 if ((style
= strchr(user
, ':')) != NULL
)
241 if (authctxt
->attempt
++ == 0) {
242 /* setup auth context */
243 authctxt
->pw
= PRIVSEP(getpwnamallow(user
));
244 authctxt
->user
= xstrdup(user
);
245 if (authctxt
->pw
&& strcmp(service
, "ssh-connection")==0) {
247 debug2("input_userauth_request: setting up authctxt for %s", user
);
249 logit("input_userauth_request: invalid user %s", user
);
250 authctxt
->pw
= fakepw();
251 #ifdef SSH_AUDIT_EVENTS
252 PRIVSEP(audit_event(SSH_INVALID_USER
));
257 PRIVSEP(start_pam(authctxt
));
259 setproctitle("%s%s", authctxt
->valid
? user
: "unknown",
260 use_privsep
? " [net]" : "");
261 authctxt
->service
= xstrdup(service
);
262 authctxt
->style
= style
? xstrdup(style
) : NULL
;
264 mm_inform_authserv(service
, style
);
266 } else if (strcmp(user
, authctxt
->user
) != 0 ||
267 strcmp(service
, authctxt
->service
) != 0) {
268 packet_disconnect("Change of username or service not allowed: "
269 "(%s,%s) -> (%s,%s)",
270 authctxt
->user
, authctxt
->service
, user
, service
);
273 #ifdef HAVE_LOGIN_CAP
274 if (authctxt
->pw
!= NULL
) {
275 lc
= login_getpwclass(authctxt
->pw
);
277 lc
= login_getclassbyname(NULL
, authctxt
->pw
);
278 if (!auth_hostok(lc
, from_host
, from_ip
)) {
279 logit("Denied connection for %.200s from %.200s [%.200s].",
280 authctxt
->pw
->pw_name
, from_host
, from_ip
);
281 packet_disconnect("Sorry, you are not allowed to connect.");
283 if (!auth_timeok(lc
, time(NULL
))) {
284 logit("LOGIN %.200s REFUSED (TIME) FROM %.200s",
285 authctxt
->pw
->pw_name
, from_host
);
286 packet_disconnect("Logins not available right now.");
291 #endif /* HAVE_LOGIN_CAP */
294 auth2_challenge_stop(authctxt
);
296 auth2_jpake_stop(authctxt
);
300 /* XXX move to auth2_gssapi_stop() */
301 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN
, NULL
);
302 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE
, NULL
);
305 authctxt
->postponed
= 0;
307 /* try to authenticate user */
308 m
= authmethod_lookup(method
);
309 if (m
!= NULL
&& authctxt
->failures
< options
.max_authtries
) {
310 debug2("input_userauth_request: try method %s", method
);
311 authenticated
= m
->userauth(authctxt
);
313 userauth_finish(authctxt
, authenticated
, method
);
321 userauth_finish(Authctxt
*authctxt
, int authenticated
, char *method
)
325 if (!authctxt
->valid
&& authenticated
)
326 fatal("INTERNAL ERROR: authenticated invalid user %s",
329 /* Special handling for root */
330 if (authenticated
&& authctxt
->pw
->pw_uid
== 0 &&
331 !auth_root_allowed(method
)) {
333 #ifdef SSH_AUDIT_EVENTS
334 PRIVSEP(audit_event(SSH_LOGIN_ROOT_DENIED
));
339 if (options
.use_pam
&& authenticated
) {
340 if (!PRIVSEP(do_pam_account())) {
341 /* if PAM returned a message, send it to the user */
342 if (buffer_len(&loginmsg
) > 0) {
343 buffer_append(&loginmsg
, "\0", 1);
344 userauth_send_banner(buffer_ptr(&loginmsg
));
347 fatal("Access denied for user %s by PAM account "
348 "configuration", authctxt
->user
);
354 if (authenticated
&& cray_access_denied(authctxt
->user
)) {
356 fatal("Access denied for user %s.",authctxt
->user
);
360 /* Log before sending the reply */
361 auth_log(authctxt
, authenticated
, method
, " ssh2");
363 if (authctxt
->postponed
)
366 /* XXX todo: check if multiple auth methods are needed */
367 if (authenticated
== 1) {
368 /* turn off userauth */
369 dispatch_set(SSH2_MSG_USERAUTH_REQUEST
, &dispatch_protocol_ignore
);
370 packet_start(SSH2_MSG_USERAUTH_SUCCESS
);
373 /* now we can break out */
374 authctxt
->success
= 1;
377 /* Allow initial try of "none" auth without failure penalty */
378 if (authctxt
->attempt
> 1 || strcmp(method
, "none") != 0)
379 authctxt
->failures
++;
380 if (authctxt
->failures
>= options
.max_authtries
) {
381 #ifdef SSH_AUDIT_EVENTS
382 PRIVSEP(audit_event(SSH_LOGIN_EXCEED_MAXTRIES
));
384 packet_disconnect(AUTH_FAIL_MSG
, authctxt
->user
);
386 methods
= authmethods_get();
387 packet_start(SSH2_MSG_USERAUTH_FAILURE
);
388 packet_put_cstring(methods
);
389 packet_put_char(0); /* XXX partial success, unused */
397 authmethods_get(void)
404 for (i
= 0; authmethods
[i
] != NULL
; i
++) {
405 if (strcmp(authmethods
[i
]->name
, "none") == 0)
407 if (authmethods
[i
]->enabled
!= NULL
&&
408 *(authmethods
[i
]->enabled
) != 0) {
409 if (buffer_len(&b
) > 0)
410 buffer_append(&b
, ",", 1);
411 buffer_append(&b
, authmethods
[i
]->name
,
412 strlen(authmethods
[i
]->name
));
415 buffer_append(&b
, "\0", 1);
416 list
= xstrdup(buffer_ptr(&b
));
422 authmethod_lookup(const char *name
)
427 for (i
= 0; authmethods
[i
] != NULL
; i
++)
428 if (authmethods
[i
]->enabled
!= NULL
&&
429 *(authmethods
[i
]->enabled
) != 0 &&
430 strcmp(name
, authmethods
[i
]->name
) == 0)
431 return authmethods
[i
];
432 debug2("Unrecognized authentication method name: %s",
433 name
? name
: "NULL");