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.
29 #include <sys/types.h>
51 #include "pathnames.h"
58 #include "monitor_wrap.h"
61 extern ServerOptions options;
62 extern u_char *session_id2;
63 extern u_int session_id2_len;
64 extern Buffer loginmsg;
68 extern Authmethod method_none;
69 extern Authmethod method_pubkey;
70 extern Authmethod method_passwd;
71 extern Authmethod method_kbdint;
72 extern Authmethod method_hostbased;
74 extern Authmethod method_gssapi;
77 extern Authmethod method_jpake;
80 Authmethod *authmethods[] = {
97 static void input_service_request(int, u_int32_t, void *);
98 static void input_userauth_request(int, u_int32_t, void *);
101 static Authmethod *authmethod_lookup(const char *);
102 static char *authmethods_get(void);
105 auth2_read_banner(void)
112 if ((fd = open(options.banner, O_RDONLY)) == -1)
114 if (fstat(fd, &st) == -1) {
118 if (st.st_size > 1*1024*1024) {
123 len = (size_t)st.st_size; /* truncate */
124 banner = xmalloc(len + 1);
125 n = atomicio(read, fd, banner, len);
138 userauth_send_banner(const char *msg)
140 if (datafellows & SSH_BUG_BANNER)
143 packet_start(SSH2_MSG_USERAUTH_BANNER);
144 packet_put_cstring(msg);
145 packet_put_cstring(""); /* language, unused */
147 debug("%s: sent", __func__);
151 userauth_banner(void)
155 if (options.banner == NULL ||
156 strcasecmp(options.banner, "none") == 0 ||
157 (datafellows & SSH_BUG_BANNER) != 0)
160 if ((banner = PRIVSEP(auth2_read_banner())) == NULL)
162 userauth_send_banner(banner);
170 * loop until authctxt->success == TRUE
173 do_authentication2(Authctxt *authctxt)
175 dispatch_init(&dispatch_protocol_error);
176 dispatch_set(SSH2_MSG_SERVICE_REQUEST, &input_service_request);
177 dispatch_run(DISPATCH_BLOCK, &authctxt->success, authctxt);
182 input_service_request(int type, u_int32_t seq, void *ctxt)
184 Authctxt *authctxt = ctxt;
187 char *service = packet_get_cstring(&len);
190 if (authctxt == NULL)
191 fatal("input_service_request: no authctxt");
193 if (strcmp(service, "ssh-userauth") == 0) {
194 if (!authctxt->success) {
196 /* now we can handle user-auth requests */
197 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &input_userauth_request);
200 /* XXX all other service requests are denied */
203 packet_start(SSH2_MSG_SERVICE_ACCEPT);
204 packet_put_cstring(service);
208 debug("bad service request %s", service);
209 packet_disconnect("bad service request %s", service);
216 input_userauth_request(int type, u_int32_t seq, void *ctxt)
218 Authctxt *authctxt = ctxt;
219 Authmethod *m = NULL;
220 char *user, *service, *method, *style = NULL;
221 int authenticated = 0;
222 #ifdef HAVE_LOGIN_CAP
224 const char *from_host, *from_ip;
226 from_host = get_canonical_hostname(options.use_dns);
227 from_ip = get_remote_ipaddr();
230 if (authctxt == NULL)
231 fatal("input_userauth_request: no authctxt");
233 user = packet_get_cstring(NULL);
234 service = packet_get_cstring(NULL);
235 method = packet_get_cstring(NULL);
236 debug("userauth-request for user %s service %s method %s", user, service, method);
237 debug("attempt %d failures %d", authctxt->attempt, authctxt->failures);
239 if ((style = strchr(user, ':')) != NULL)
242 if (authctxt->attempt++ == 0) {
243 /* setup auth context */
244 authctxt->pw = PRIVSEP(getpwnamallow(user));
245 authctxt->user = xstrdup(user);
246 if (authctxt->pw && strcmp(service, "ssh-connection")==0) {
248 debug2("input_userauth_request: setting up authctxt for %s", user);
250 logit("input_userauth_request: invalid user %s", user);
251 authctxt->pw = fakepw();
252 #ifdef SSH_AUDIT_EVENTS
253 PRIVSEP(audit_event(SSH_INVALID_USER));
258 PRIVSEP(start_pam(authctxt));
260 setproctitle("%s%s", authctxt->valid ? user : "unknown",
261 use_privsep ? " [net]" : "");
262 authctxt->service = xstrdup(service);
263 authctxt->style = style ? xstrdup(style) : NULL;
265 mm_inform_authserv(service, style);
267 } else if (strcmp(user, authctxt->user) != 0 ||
268 strcmp(service, authctxt->service) != 0) {
269 packet_disconnect("Change of username or service not allowed: "
270 "(%s,%s) -> (%s,%s)",
271 authctxt->user, authctxt->service, user, service);
274 #ifdef HAVE_LOGIN_CAP
275 if (authctxt->pw != NULL) {
276 lc = login_getpwclass(authctxt->pw);
278 lc = login_getclassbyname(NULL, authctxt->pw);
279 if (!auth_hostok(lc, from_host, from_ip)) {
280 logit("Denied connection for %.200s from %.200s [%.200s].",
281 authctxt->pw->pw_name, from_host, from_ip);
282 packet_disconnect("Sorry, you are not allowed to connect.");
284 if (!auth_timeok(lc, time(NULL))) {
285 logit("LOGIN %.200s REFUSED (TIME) FROM %.200s",
286 authctxt->pw->pw_name, from_host);
287 packet_disconnect("Logins not available right now.");
292 #endif /* HAVE_LOGIN_CAP */
295 auth2_challenge_stop(authctxt);
297 auth2_jpake_stop(authctxt);
301 /* XXX move to auth2_gssapi_stop() */
302 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
303 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, NULL);
306 authctxt->postponed = 0;
308 /* try to authenticate user */
309 m = authmethod_lookup(method);
310 if (m != NULL && authctxt->failures < options.max_authtries) {
311 debug2("input_userauth_request: try method %s", method);
312 authenticated = m->userauth(authctxt);
314 userauth_finish(authctxt, authenticated, method);
322 userauth_finish(Authctxt *authctxt, int authenticated, char *method)
326 if (!authctxt->valid && authenticated)
327 fatal("INTERNAL ERROR: authenticated invalid user %s",
330 /* Special handling for root */
331 if (authenticated && authctxt->pw->pw_uid == 0 &&
332 !auth_root_allowed(method)) {
334 #ifdef SSH_AUDIT_EVENTS
335 PRIVSEP(audit_event(SSH_LOGIN_ROOT_DENIED));
340 if (options.use_pam && authenticated) {
341 if (!PRIVSEP(do_pam_account())) {
342 /* if PAM returned a message, send it to the user */
343 if (buffer_len(&loginmsg) > 0) {
344 buffer_append(&loginmsg, "\0", 1);
345 userauth_send_banner(buffer_ptr(&loginmsg));
348 fatal("Access denied for user %s by PAM account "
349 "configuration", authctxt->user);
355 if (authenticated && cray_access_denied(authctxt->user)) {
357 fatal("Access denied for user %s.",authctxt->user);
361 /* Log before sending the reply */
362 auth_log(authctxt, authenticated, method, " ssh2");
364 if (authctxt->postponed)
367 /* XXX todo: check if multiple auth methods are needed */
368 if (authenticated == 1) {
369 /* turn off userauth */
370 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &dispatch_protocol_ignore);
371 packet_start(SSH2_MSG_USERAUTH_SUCCESS);
374 /* now we can break out */
375 authctxt->success = 1;
378 /* Allow initial try of "none" auth without failure penalty */
379 if (authctxt->attempt > 1 || strcmp(method, "none") != 0)
380 authctxt->failures++;
381 if (authctxt->failures >= options.max_authtries) {
382 #ifdef SSH_AUDIT_EVENTS
383 PRIVSEP(audit_event(SSH_LOGIN_EXCEED_MAXTRIES));
385 packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
387 methods = authmethods_get();
388 packet_start(SSH2_MSG_USERAUTH_FAILURE);
389 packet_put_cstring(methods);
390 packet_put_char(0); /* XXX partial success, unused */
398 authmethods_get(void)
405 for (i = 0; authmethods[i] != NULL; i++) {
406 if (strcmp(authmethods[i]->name, "none") == 0)
408 if (authmethods[i]->enabled != NULL &&
409 *(authmethods[i]->enabled) != 0) {
410 if (buffer_len(&b) > 0)
411 buffer_append(&b, ",", 1);
412 buffer_append(&b, authmethods[i]->name,
413 strlen(authmethods[i]->name));
416 buffer_append(&b, "\0", 1);
417 list = xstrdup(buffer_ptr(&b));
423 authmethod_lookup(const char *name)
428 for (i = 0; authmethods[i] != NULL; i++)
429 if (authmethods[i]->enabled != NULL &&
430 *(authmethods[i]->enabled) != 0 &&
431 strcmp(name, authmethods[i]->name) == 0)
432 return authmethods[i];
433 debug2("Unrecognized authentication method name: %s",
434 name ? name : "NULL");