1 /* $OpenBSD: auth2.c,v 1.118 2008/07/02 13:30:34 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>
49 #include "pathnames.h"
55 #include "monitor_wrap.h"
58 extern ServerOptions options;
59 extern u_char *session_id2;
60 extern u_int session_id2_len;
61 extern Buffer loginmsg;
65 extern Authmethod method_none;
66 extern Authmethod method_pubkey;
67 extern Authmethod method_passwd;
68 extern Authmethod method_kbdint;
69 extern Authmethod method_hostbased;
71 extern Authmethod method_gssapi;
74 Authmethod *authmethods[] = {
88 static void input_service_request(int, u_int32_t, void *);
89 static void input_userauth_request(int, u_int32_t, void *);
92 static Authmethod *authmethod_lookup(const char *);
93 static char *authmethods_get(void);
96 auth2_read_banner(void)
103 if ((fd = open(options.banner, O_RDONLY)) == -1)
105 if (fstat(fd, &st) == -1) {
109 if (st.st_size > 1*1024*1024) {
114 len = (size_t)st.st_size; /* truncate */
115 banner = xmalloc(len + 1);
116 n = atomicio(read, fd, banner, len);
129 userauth_send_banner(const char *msg)
131 if (datafellows & SSH_BUG_BANNER)
134 packet_start(SSH2_MSG_USERAUTH_BANNER);
135 packet_put_cstring(msg);
136 packet_put_cstring(""); /* language, unused */
138 debug("%s: sent", __func__);
142 userauth_banner(void)
146 if (options.banner == NULL ||
147 strcasecmp(options.banner, "none") == 0 ||
148 (datafellows & SSH_BUG_BANNER) != 0)
151 if ((banner = PRIVSEP(auth2_read_banner())) == NULL)
153 userauth_send_banner(banner);
161 * loop until authctxt->success == TRUE
164 do_authentication2(Authctxt *authctxt)
166 dispatch_init(&dispatch_protocol_error);
167 dispatch_set(SSH2_MSG_SERVICE_REQUEST, &input_service_request);
168 dispatch_run(DISPATCH_BLOCK, &authctxt->success, authctxt);
173 input_service_request(int type, u_int32_t seq, void *ctxt)
175 Authctxt *authctxt = ctxt;
178 char *service = packet_get_string(&len);
181 if (authctxt == NULL)
182 fatal("input_service_request: no authctxt");
184 if (strcmp(service, "ssh-userauth") == 0) {
185 if (!authctxt->success) {
187 /* now we can handle user-auth requests */
188 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &input_userauth_request);
191 /* XXX all other service requests are denied */
194 packet_start(SSH2_MSG_SERVICE_ACCEPT);
195 packet_put_cstring(service);
199 debug("bad service request %s", service);
200 packet_disconnect("bad service request %s", service);
207 input_userauth_request(int type, u_int32_t seq, void *ctxt)
209 Authctxt *authctxt = ctxt;
210 Authmethod *m = NULL;
211 char *user, *service, *method, *style = NULL;
212 int authenticated = 0;
214 if (authctxt == NULL)
215 fatal("input_userauth_request: no authctxt");
217 user = packet_get_string(NULL);
218 service = packet_get_string(NULL);
219 method = packet_get_string(NULL);
220 debug("userauth-request for user %s service %s method %s", user, service, method);
221 debug("attempt %d failures %d", authctxt->attempt, authctxt->failures);
223 if ((style = strchr(user, ':')) != NULL)
226 if (authctxt->attempt++ == 0) {
227 /* setup auth context */
228 authctxt->pw = PRIVSEP(getpwnamallow(user));
229 authctxt->user = xstrdup(user);
230 if (authctxt->pw && strcmp(service, "ssh-connection")==0) {
232 debug2("input_userauth_request: setting up authctxt for %s", user);
234 logit("input_userauth_request: invalid user %s", user);
235 authctxt->pw = fakepw();
236 #ifdef SSH_AUDIT_EVENTS
237 PRIVSEP(audit_event(SSH_INVALID_USER));
242 PRIVSEP(start_pam(authctxt));
244 setproctitle("%s%s", authctxt->valid ? user : "unknown",
245 use_privsep ? " [net]" : "");
246 authctxt->service = xstrdup(service);
247 authctxt->style = style ? xstrdup(style) : NULL;
249 mm_inform_authserv(service, style);
251 } else if (strcmp(user, authctxt->user) != 0 ||
252 strcmp(service, authctxt->service) != 0) {
253 packet_disconnect("Change of username or service not allowed: "
254 "(%s,%s) -> (%s,%s)",
255 authctxt->user, authctxt->service, user, service);
258 auth2_challenge_stop(authctxt);
261 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
262 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, NULL);
265 authctxt->postponed = 0;
267 /* try to authenticate user */
268 m = authmethod_lookup(method);
269 if (m != NULL && authctxt->failures < options.max_authtries) {
270 debug2("input_userauth_request: try method %s", method);
271 authenticated = m->userauth(authctxt);
273 userauth_finish(authctxt, authenticated, method);
281 userauth_finish(Authctxt *authctxt, int authenticated, char *method)
285 if (!authctxt->valid && authenticated)
286 fatal("INTERNAL ERROR: authenticated invalid user %s",
289 /* Special handling for root */
290 if (authenticated && authctxt->pw->pw_uid == 0 &&
291 !auth_root_allowed(method)) {
293 #ifdef SSH_AUDIT_EVENTS
294 PRIVSEP(audit_event(SSH_LOGIN_ROOT_DENIED));
299 if (options.use_pam && authenticated) {
300 if (!PRIVSEP(do_pam_account())) {
301 /* if PAM returned a message, send it to the user */
302 if (buffer_len(&loginmsg) > 0) {
303 buffer_append(&loginmsg, "\0", 1);
304 userauth_send_banner(buffer_ptr(&loginmsg));
307 fatal("Access denied for user %s by PAM account "
308 "configuration", authctxt->user);
314 if (authenticated && cray_access_denied(authctxt->user)) {
316 fatal("Access denied for user %s.",authctxt->user);
320 /* Log before sending the reply */
321 auth_log(authctxt, authenticated, method, " ssh2");
323 if (authctxt->postponed)
326 /* XXX todo: check if multiple auth methods are needed */
327 if (authenticated == 1) {
328 /* turn off userauth */
329 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &dispatch_protocol_ignore);
330 packet_start(SSH2_MSG_USERAUTH_SUCCESS);
333 /* now we can break out */
334 authctxt->success = 1;
336 if (++authctxt->failures >= options.max_authtries) {
337 #ifdef SSH_AUDIT_EVENTS
338 PRIVSEP(audit_event(SSH_LOGIN_EXCEED_MAXTRIES));
340 packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
342 methods = authmethods_get();
343 packet_start(SSH2_MSG_USERAUTH_FAILURE);
344 packet_put_cstring(methods);
345 packet_put_char(0); /* XXX partial success, unused */
353 authmethods_get(void)
360 for (i = 0; authmethods[i] != NULL; i++) {
361 if (strcmp(authmethods[i]->name, "none") == 0)
363 if (authmethods[i]->enabled != NULL &&
364 *(authmethods[i]->enabled) != 0) {
365 if (buffer_len(&b) > 0)
366 buffer_append(&b, ",", 1);
367 buffer_append(&b, authmethods[i]->name,
368 strlen(authmethods[i]->name));
371 buffer_append(&b, "\0", 1);
372 list = xstrdup(buffer_ptr(&b));
378 authmethod_lookup(const char *name)
383 for (i = 0; authmethods[i] != NULL; i++)
384 if (authmethods[i]->enabled != NULL &&
385 *(authmethods[i]->enabled) != 0 &&
386 strcmp(name, authmethods[i]->name) == 0)
387 return authmethods[i];
388 debug2("Unrecognized authentication method name: %s",
389 name ? name : "NULL");