1 /* $OpenBSD: auth2.c,v 1.122 2010/08/31 09:58:37 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>
50 #include "pathnames.h"
56 #include "monitor_wrap.h"
59 extern ServerOptions options
;
60 extern u_char
*session_id2
;
61 extern u_int session_id2_len
;
62 extern Buffer loginmsg
;
66 extern Authmethod method_none
;
67 extern Authmethod method_pubkey
;
68 extern Authmethod method_passwd
;
69 extern Authmethod method_kbdint
;
70 extern Authmethod method_hostbased
;
72 extern Authmethod method_gssapi
;
75 extern Authmethod method_jpake
;
78 Authmethod
*authmethods
[] = {
95 static void input_service_request(int, u_int32_t
, void *);
96 static void input_userauth_request(int, u_int32_t
, void *);
99 static Authmethod
*authmethod_lookup(const char *);
100 static char *authmethods_get(void);
103 auth2_read_banner(void)
110 if ((fd
= open(options
.banner
, O_RDONLY
)) == -1)
112 if (fstat(fd
, &st
) == -1) {
116 if (st
.st_size
> 1*1024*1024) {
121 len
= (size_t)st
.st_size
; /* truncate */
122 banner
= xmalloc(len
+ 1);
123 n
= atomicio(read
, fd
, banner
, len
);
136 userauth_send_banner(const char *msg
)
138 if (datafellows
& SSH_BUG_BANNER
)
141 packet_start(SSH2_MSG_USERAUTH_BANNER
);
142 packet_put_cstring(msg
);
143 packet_put_cstring(""); /* language, unused */
145 debug("%s: sent", __func__
);
149 userauth_banner(void)
153 if (options
.banner
== NULL
||
154 strcasecmp(options
.banner
, "none") == 0 ||
155 (datafellows
& SSH_BUG_BANNER
) != 0)
158 if ((banner
= PRIVSEP(auth2_read_banner())) == NULL
)
160 userauth_send_banner(banner
);
168 * loop until authctxt->success == TRUE
171 do_authentication2(Authctxt
*authctxt
)
173 dispatch_init(&dispatch_protocol_error
);
174 dispatch_set(SSH2_MSG_SERVICE_REQUEST
, &input_service_request
);
175 dispatch_run(DISPATCH_BLOCK
, &authctxt
->success
, authctxt
);
180 input_service_request(int type
, u_int32_t seq
, void *ctxt
)
182 Authctxt
*authctxt
= ctxt
;
185 char *service
= packet_get_cstring(&len
);
188 if (authctxt
== NULL
)
189 fatal("input_service_request: no authctxt");
191 if (strcmp(service
, "ssh-userauth") == 0) {
192 if (!authctxt
->success
) {
194 /* now we can handle user-auth requests */
195 dispatch_set(SSH2_MSG_USERAUTH_REQUEST
, &input_userauth_request
);
198 /* XXX all other service requests are denied */
201 packet_start(SSH2_MSG_SERVICE_ACCEPT
);
202 packet_put_cstring(service
);
206 debug("bad service request %s", service
);
207 packet_disconnect("bad service request %s", service
);
214 input_userauth_request(int type
, u_int32_t seq
, void *ctxt
)
216 Authctxt
*authctxt
= ctxt
;
217 Authmethod
*m
= NULL
;
218 char *user
, *service
, *method
, *style
= NULL
;
219 int authenticated
= 0;
221 if (authctxt
== NULL
)
222 fatal("input_userauth_request: no authctxt");
224 user
= packet_get_cstring(NULL
);
225 service
= packet_get_cstring(NULL
);
226 method
= packet_get_cstring(NULL
);
227 debug("userauth-request for user %s service %s method %s", user
, service
, method
);
228 debug("attempt %d failures %d", authctxt
->attempt
, authctxt
->failures
);
230 if ((style
= strchr(user
, ':')) != NULL
)
233 if (authctxt
->attempt
++ == 0) {
234 /* setup auth context */
235 authctxt
->pw
= PRIVSEP(getpwnamallow(user
));
236 authctxt
->user
= xstrdup(user
);
237 if (authctxt
->pw
&& strcmp(service
, "ssh-connection")==0) {
239 debug2("input_userauth_request: setting up authctxt for %s", user
);
241 logit("input_userauth_request: invalid user %s", user
);
242 authctxt
->pw
= fakepw();
243 #ifdef SSH_AUDIT_EVENTS
244 PRIVSEP(audit_event(SSH_INVALID_USER
));
249 PRIVSEP(start_pam(authctxt
));
251 setproctitle("%s%s", authctxt
->valid
? user
: "unknown",
252 use_privsep
? " [net]" : "");
253 authctxt
->service
= xstrdup(service
);
254 authctxt
->style
= style
? xstrdup(style
) : NULL
;
256 mm_inform_authserv(service
, style
);
258 } else if (strcmp(user
, authctxt
->user
) != 0 ||
259 strcmp(service
, authctxt
->service
) != 0) {
260 packet_disconnect("Change of username or service not allowed: "
261 "(%s,%s) -> (%s,%s)",
262 authctxt
->user
, authctxt
->service
, user
, service
);
265 auth2_challenge_stop(authctxt
);
267 auth2_jpake_stop(authctxt
);
271 /* XXX move to auth2_gssapi_stop() */
272 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN
, NULL
);
273 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE
, NULL
);
276 authctxt
->postponed
= 0;
278 /* try to authenticate user */
279 m
= authmethod_lookup(method
);
280 if (m
!= NULL
&& authctxt
->failures
< options
.max_authtries
) {
281 debug2("input_userauth_request: try method %s", method
);
282 authenticated
= m
->userauth(authctxt
);
284 userauth_finish(authctxt
, authenticated
, method
);
292 userauth_finish(Authctxt
*authctxt
, int authenticated
, char *method
)
296 if (!authctxt
->valid
&& authenticated
)
297 fatal("INTERNAL ERROR: authenticated invalid user %s",
300 /* Special handling for root */
301 if (authenticated
&& authctxt
->pw
->pw_uid
== 0 &&
302 !auth_root_allowed(method
)) {
304 #ifdef SSH_AUDIT_EVENTS
305 PRIVSEP(audit_event(SSH_LOGIN_ROOT_DENIED
));
310 if (options
.use_pam
&& authenticated
) {
311 if (!PRIVSEP(do_pam_account())) {
312 /* if PAM returned a message, send it to the user */
313 if (buffer_len(&loginmsg
) > 0) {
314 buffer_append(&loginmsg
, "\0", 1);
315 userauth_send_banner(buffer_ptr(&loginmsg
));
318 fatal("Access denied for user %s by PAM account "
319 "configuration", authctxt
->user
);
325 if (authenticated
&& cray_access_denied(authctxt
->user
)) {
327 fatal("Access denied for user %s.",authctxt
->user
);
331 /* Log before sending the reply */
332 auth_log(authctxt
, authenticated
, method
, " ssh2");
334 if (authctxt
->postponed
)
337 /* XXX todo: check if multiple auth methods are needed */
338 if (authenticated
== 1) {
339 /* turn off userauth */
340 dispatch_set(SSH2_MSG_USERAUTH_REQUEST
, &dispatch_protocol_ignore
);
341 packet_start(SSH2_MSG_USERAUTH_SUCCESS
);
344 /* now we can break out */
345 authctxt
->success
= 1;
348 /* Allow initial try of "none" auth without failure penalty */
349 if (authctxt
->attempt
> 1 || strcmp(method
, "none") != 0)
350 authctxt
->failures
++;
351 if (authctxt
->failures
>= options
.max_authtries
) {
352 #ifdef SSH_AUDIT_EVENTS
353 PRIVSEP(audit_event(SSH_LOGIN_EXCEED_MAXTRIES
));
355 packet_disconnect(AUTH_FAIL_MSG
, authctxt
->user
);
357 methods
= authmethods_get();
358 packet_start(SSH2_MSG_USERAUTH_FAILURE
);
359 packet_put_cstring(methods
);
360 packet_put_char(0); /* XXX partial success, unused */
368 authmethods_get(void)
375 for (i
= 0; authmethods
[i
] != NULL
; i
++) {
376 if (strcmp(authmethods
[i
]->name
, "none") == 0)
378 if (authmethods
[i
]->enabled
!= NULL
&&
379 *(authmethods
[i
]->enabled
) != 0) {
380 if (buffer_len(&b
) > 0)
381 buffer_append(&b
, ",", 1);
382 buffer_append(&b
, authmethods
[i
]->name
,
383 strlen(authmethods
[i
]->name
));
386 buffer_append(&b
, "\0", 1);
387 list
= xstrdup(buffer_ptr(&b
));
393 authmethod_lookup(const char *name
)
398 for (i
= 0; authmethods
[i
] != NULL
; i
++)
399 if (authmethods
[i
]->enabled
!= NULL
&&
400 *(authmethods
[i
]->enabled
) != 0 &&
401 strcmp(name
, authmethods
[i
]->name
) == 0)
402 return authmethods
[i
];
403 debug2("Unrecognized authentication method name: %s",
404 name
? name
: "NULL");