2 * Copyright (c) 2000 Markus Friedl. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 RCSID("$OpenBSD: auth2.c,v 1.107 2004/07/28 09:40:29 markus Exp $");
38 #include "pathnames.h"
39 #include "monitor_wrap.h"
46 extern ServerOptions options;
47 extern u_char *session_id2;
48 extern u_int session_id2_len;
52 extern Authmethod method_none;
53 extern Authmethod method_pubkey;
54 extern Authmethod method_passwd;
55 extern Authmethod method_kbdint;
56 extern Authmethod method_hostbased;
58 extern Authmethod method_gssapi;
61 Authmethod *authmethods[] = {
75 static void input_service_request(int, u_int32_t, void *);
76 static void input_userauth_request(int, u_int32_t, void *);
79 static Authmethod *authmethod_lookup(const char *);
80 static char *authmethods_get(void);
81 int user_key_allowed(struct passwd *, Key *);
84 * loop until authctxt->success == TRUE
88 do_authentication2(Authctxt *authctxt)
90 /* challenge-response is implemented via keyboard interactive */
91 if (options.challenge_response_authentication)
92 options.kbd_interactive_authentication = 1;
94 dispatch_init(&dispatch_protocol_error);
95 dispatch_set(SSH2_MSG_SERVICE_REQUEST, &input_service_request);
96 dispatch_run(DISPATCH_BLOCK, &authctxt->success, authctxt);
100 input_service_request(int type, u_int32_t seq, void *ctxt)
102 Authctxt *authctxt = ctxt;
105 char *service = packet_get_string(&len);
108 if (authctxt == NULL)
109 fatal("input_service_request: no authctxt");
111 if (strcmp(service, "ssh-userauth") == 0) {
112 if (!authctxt->success) {
114 /* now we can handle user-auth requests */
115 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &input_userauth_request);
118 /* XXX all other service requests are denied */
121 packet_start(SSH2_MSG_SERVICE_ACCEPT);
122 packet_put_cstring(service);
126 debug("bad service request %s", service);
127 packet_disconnect("bad service request %s", service);
133 input_userauth_request(int type, u_int32_t seq, void *ctxt)
135 Authctxt *authctxt = ctxt;
136 Authmethod *m = NULL;
137 char *user, *service, *method, *style = NULL;
138 int authenticated = 0;
139 #ifdef HAVE_LOGIN_CAP
141 const char *from_host, *from_ip;
143 from_host = get_canonical_hostname(options.use_dns);
144 from_ip = get_remote_ipaddr();
147 if (authctxt == NULL)
148 fatal("input_userauth_request: no authctxt");
150 user = packet_get_string(NULL);
151 service = packet_get_string(NULL);
152 method = packet_get_string(NULL);
153 debug("userauth-request for user %s service %s method %s", user, service, method);
154 debug("attempt %d failures %d", authctxt->attempt, authctxt->failures);
156 if ((style = strchr(user, ':')) != NULL)
159 if (authctxt->attempt++ == 0) {
160 /* setup auth context */
161 authctxt->pw = PRIVSEP(getpwnamallow(user));
162 authctxt->user = xstrdup(user);
163 if (authctxt->pw && strcmp(service, "ssh-connection")==0) {
165 debug2("input_userauth_request: setting up authctxt for %s", user);
168 PRIVSEP(start_pam(authctxt));
171 logit("input_userauth_request: invalid user %s", user);
172 authctxt->pw = fakepw();
175 PRIVSEP(start_pam(authctxt));
178 setproctitle("%s%s", authctxt->valid ? user : "unknown",
179 use_privsep ? " [net]" : "");
180 authctxt->service = xstrdup(service);
181 authctxt->style = style ? xstrdup(style) : NULL;
183 mm_inform_authserv(service, style);
184 } else if (strcmp(user, authctxt->user) != 0 ||
185 strcmp(service, authctxt->service) != 0) {
186 packet_disconnect("Change of username or service not allowed: "
187 "(%s,%s) -> (%s,%s)",
188 authctxt->user, authctxt->service, user, service);
191 #ifdef HAVE_LOGIN_CAP
192 if (authctxt->pw != NULL) {
193 lc = login_getpwclass(authctxt->pw);
195 lc = login_getclassbyname(NULL, authctxt->pw);
196 if (!auth_hostok(lc, from_host, from_ip)) {
197 logit("Denied connection for %.200s from %.200s [%.200s].",
198 authctxt->pw->pw_name, from_host, from_ip);
199 packet_disconnect("Sorry, you are not allowed to connect.");
201 if (!auth_timeok(lc, time(NULL))) {
202 logit("LOGIN %.200s REFUSED (TIME) FROM %.200s",
203 authctxt->pw->pw_name, from_host);
204 packet_disconnect("Logins not available right now.");
209 #endif /* HAVE_LOGIN_CAP */
212 auth2_challenge_stop(authctxt);
215 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
216 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, NULL);
219 authctxt->postponed = 0;
221 /* try to authenticate user */
222 m = authmethod_lookup(method);
224 debug2("input_userauth_request: try method %s", method);
225 authenticated = m->userauth(authctxt);
227 userauth_finish(authctxt, authenticated, method);
235 userauth_finish(Authctxt *authctxt, int authenticated, char *method)
239 if (!authctxt->valid && authenticated)
240 fatal("INTERNAL ERROR: authenticated invalid user %s",
243 /* Special handling for root */
244 if (authenticated && authctxt->pw->pw_uid == 0 &&
245 !auth_root_allowed(method))
249 if (options.use_pam && authenticated && !PRIVSEP(do_pam_account()))
254 if (authenticated && cray_access_denied(authctxt->user)) {
256 fatal("Access denied for user %s.",authctxt->user);
260 /* Log before sending the reply */
261 auth_log(authctxt, authenticated, method, " ssh2");
263 if (authctxt->postponed)
266 /* XXX todo: check if multiple auth methods are needed */
267 if (authenticated == 1) {
268 /* turn off userauth */
269 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &dispatch_protocol_ignore);
270 packet_start(SSH2_MSG_USERAUTH_SUCCESS);
273 /* now we can break out */
274 authctxt->success = 1;
276 if (authctxt->failures++ > options.max_authtries)
277 packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
278 methods = authmethods_get();
279 packet_start(SSH2_MSG_USERAUTH_FAILURE);
280 packet_put_cstring(methods);
281 packet_put_char(0); /* XXX partial success, unused */
291 authmethods_get(void)
298 for (i = 0; authmethods[i] != NULL; i++) {
299 if (strcmp(authmethods[i]->name, "none") == 0)
301 if (authmethods[i]->enabled != NULL &&
302 *(authmethods[i]->enabled) != 0) {
303 if (buffer_len(&b) > 0)
304 buffer_append(&b, ",", 1);
305 buffer_append(&b, authmethods[i]->name,
306 strlen(authmethods[i]->name));
309 buffer_append(&b, "\0", 1);
310 list = xstrdup(buffer_ptr(&b));
316 authmethod_lookup(const char *name)
321 for (i = 0; authmethods[i] != NULL; i++)
322 if (authmethods[i]->enabled != NULL &&
323 *(authmethods[i]->enabled) != 0 &&
324 strcmp(name, authmethods[i]->name) == 0)
325 return authmethods[i];
326 debug2("Unrecognized authentication method name: %s",
327 name ? name : "NULL");