1 /* $OpenBSD: auth2.c,v 1.119 2008/07/04 23:30:16 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 Authmethod *authmethods[] = {
91 static void input_service_request(int, u_int32_t, void *);
92 static void input_userauth_request(int, u_int32_t, void *);
95 static Authmethod *authmethod_lookup(const char *);
96 static char *authmethods_get(void);
99 auth2_read_banner(void)
106 if ((fd = open(options.banner, O_RDONLY)) == -1)
108 if (fstat(fd, &st) == -1) {
112 if (st.st_size > 1*1024*1024) {
117 len = (size_t)st.st_size; /* truncate */
118 banner = xmalloc(len + 1);
119 n = atomicio(read, fd, banner, len);
132 userauth_send_banner(const char *msg)
134 if (datafellows & SSH_BUG_BANNER)
137 packet_start(SSH2_MSG_USERAUTH_BANNER);
138 packet_put_cstring(msg);
139 packet_put_cstring(""); /* language, unused */
141 debug("%s: sent", __func__);
145 userauth_banner(void)
149 if (options.banner == NULL ||
150 strcasecmp(options.banner, "none") == 0 ||
151 (datafellows & SSH_BUG_BANNER) != 0)
154 if ((banner = PRIVSEP(auth2_read_banner())) == NULL)
156 userauth_send_banner(banner);
164 * loop until authctxt->success == TRUE
167 do_authentication2(Authctxt *authctxt)
169 dispatch_init(&dispatch_protocol_error);
170 dispatch_set(SSH2_MSG_SERVICE_REQUEST, &input_service_request);
171 dispatch_run(DISPATCH_BLOCK, &authctxt->success, authctxt);
176 input_service_request(int type, u_int32_t seq, void *ctxt)
178 Authctxt *authctxt = ctxt;
181 char *service = packet_get_string(&len);
184 if (authctxt == NULL)
185 fatal("input_service_request: no authctxt");
187 if (strcmp(service, "ssh-userauth") == 0) {
188 if (!authctxt->success) {
190 /* now we can handle user-auth requests */
191 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &input_userauth_request);
194 /* XXX all other service requests are denied */
197 packet_start(SSH2_MSG_SERVICE_ACCEPT);
198 packet_put_cstring(service);
202 debug("bad service request %s", service);
203 packet_disconnect("bad service request %s", service);
210 input_userauth_request(int type, u_int32_t seq, void *ctxt)
212 Authctxt *authctxt = ctxt;
213 Authmethod *m = NULL;
214 char *user, *service, *method, *style = NULL;
215 int authenticated = 0;
216 #ifdef HAVE_LOGIN_CAP
218 const char *from_host, *from_ip;
220 from_host = get_canonical_hostname(options.use_dns);
221 from_ip = get_remote_ipaddr();
224 if (authctxt == NULL)
225 fatal("input_userauth_request: no authctxt");
227 user = packet_get_string(NULL);
228 service = packet_get_string(NULL);
229 method = packet_get_string(NULL);
230 debug("userauth-request for user %s service %s method %s", user, service, method);
231 debug("attempt %d failures %d", authctxt->attempt, authctxt->failures);
233 if ((style = strchr(user, ':')) != NULL)
236 if (authctxt->attempt++ == 0) {
237 /* setup auth context */
238 authctxt->pw = PRIVSEP(getpwnamallow(user));
239 authctxt->user = xstrdup(user);
240 if (authctxt->pw && strcmp(service, "ssh-connection")==0) {
242 debug2("input_userauth_request: setting up authctxt for %s", user);
244 logit("input_userauth_request: invalid user %s", user);
245 authctxt->pw = fakepw();
246 #ifdef SSH_AUDIT_EVENTS
247 PRIVSEP(audit_event(SSH_INVALID_USER));
252 PRIVSEP(start_pam(authctxt));
254 setproctitle("%s%s", authctxt->valid ? user : "unknown",
255 use_privsep ? " [net]" : "");
256 authctxt->service = xstrdup(service);
257 authctxt->style = style ? xstrdup(style) : NULL;
259 mm_inform_authserv(service, style);
261 } else if (strcmp(user, authctxt->user) != 0 ||
262 strcmp(service, authctxt->service) != 0) {
263 packet_disconnect("Change of username or service not allowed: "
264 "(%s,%s) -> (%s,%s)",
265 authctxt->user, authctxt->service, user, service);
268 #ifdef HAVE_LOGIN_CAP
269 if (authctxt->pw != NULL) {
270 lc = login_getpwclass(authctxt->pw);
272 lc = login_getclassbyname(NULL, authctxt->pw);
273 if (!auth_hostok(lc, from_host, from_ip)) {
274 logit("Denied connection for %.200s from %.200s [%.200s].",
275 authctxt->pw->pw_name, from_host, from_ip);
276 packet_disconnect("Sorry, you are not allowed to connect.");
278 if (!auth_timeok(lc, time(NULL))) {
279 logit("LOGIN %.200s REFUSED (TIME) FROM %.200s",
280 authctxt->pw->pw_name, from_host);
281 packet_disconnect("Logins not available right now.");
286 #endif /* HAVE_LOGIN_CAP */
289 auth2_challenge_stop(authctxt);
292 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
293 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, NULL);
296 authctxt->postponed = 0;
298 /* try to authenticate user */
299 m = authmethod_lookup(method);
300 if (m != NULL && authctxt->failures < options.max_authtries) {
301 debug2("input_userauth_request: try method %s", method);
302 authenticated = m->userauth(authctxt);
304 userauth_finish(authctxt, authenticated, method);
312 userauth_finish(Authctxt *authctxt, int authenticated, char *method)
316 if (!authctxt->valid && authenticated)
317 fatal("INTERNAL ERROR: authenticated invalid user %s",
320 /* Special handling for root */
321 if (authenticated && authctxt->pw->pw_uid == 0 &&
322 !auth_root_allowed(method)) {
324 #ifdef SSH_AUDIT_EVENTS
325 PRIVSEP(audit_event(SSH_LOGIN_ROOT_DENIED));
330 if (options.use_pam && authenticated) {
331 if (!PRIVSEP(do_pam_account())) {
332 /* if PAM returned a message, send it to the user */
333 if (buffer_len(&loginmsg) > 0) {
334 buffer_append(&loginmsg, "\0", 1);
335 userauth_send_banner(buffer_ptr(&loginmsg));
338 fatal("Access denied for user %s by PAM account "
339 "configuration", authctxt->user);
345 if (authenticated && cray_access_denied(authctxt->user)) {
347 fatal("Access denied for user %s.",authctxt->user);
351 /* Log before sending the reply */
352 auth_log(authctxt, authenticated, method, " ssh2");
354 if (authctxt->postponed)
357 /* XXX todo: check if multiple auth methods are needed */
358 if (authenticated == 1) {
359 /* turn off userauth */
360 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &dispatch_protocol_ignore);
361 packet_start(SSH2_MSG_USERAUTH_SUCCESS);
364 /* now we can break out */
365 authctxt->success = 1;
368 /* Allow initial try of "none" auth without failure penalty */
369 if (authctxt->attempt > 1 || strcmp(method, "none") != 0)
370 authctxt->failures++;
371 if (authctxt->failures >= options.max_authtries) {
372 #ifdef SSH_AUDIT_EVENTS
373 PRIVSEP(audit_event(SSH_LOGIN_EXCEED_MAXTRIES));
375 packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
377 methods = authmethods_get();
378 packet_start(SSH2_MSG_USERAUTH_FAILURE);
379 packet_put_cstring(methods);
380 packet_put_char(0); /* XXX partial success, unused */
388 authmethods_get(void)
395 for (i = 0; authmethods[i] != NULL; i++) {
396 if (strcmp(authmethods[i]->name, "none") == 0)
398 if (authmethods[i]->enabled != NULL &&
399 *(authmethods[i]->enabled) != 0) {
400 if (buffer_len(&b) > 0)
401 buffer_append(&b, ",", 1);
402 buffer_append(&b, authmethods[i]->name,
403 strlen(authmethods[i]->name));
406 buffer_append(&b, "\0", 1);
407 list = xstrdup(buffer_ptr(&b));
413 authmethod_lookup(const char *name)
418 for (i = 0; authmethods[i] != NULL; i++)
419 if (authmethods[i]->enabled != NULL &&
420 *(authmethods[i]->enabled) != 0 &&
421 strcmp(name, authmethods[i]->name) == 0)
422 return authmethods[i];
423 debug2("Unrecognized authentication method name: %s",
424 name ? name : "NULL");