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.102 2003/08/26 09:58:43 markus Exp $");
36 #include "pathnames.h"
37 #include "monitor_wrap.h"
44 extern ServerOptions options;
45 extern u_char *session_id2;
46 extern u_int session_id2_len;
48 Authctxt *x_authctxt = NULL;
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_external;
59 extern Authmethod method_gssapi;
62 Authmethod *authmethods[] = {
80 static void input_service_request(int, u_int32_t, void *);
81 static void input_userauth_request(int, u_int32_t, void *);
84 static Authmethod *authmethod_lookup(const char *);
85 static char *authmethods_get(void);
86 int user_key_allowed(struct passwd *, Key *);
87 int hostbased_key_allowed(struct passwd *, const char *, char *, Key *);
90 * loop until authctxt->success == TRUE
94 do_authentication2(void)
96 Authctxt *authctxt = authctxt_new();
98 x_authctxt = authctxt; /*XXX*/
100 /* challenge-response is implemented via keyboard interactive */
101 if (options.challenge_response_authentication)
102 options.kbd_interactive_authentication = 1;
104 dispatch_init(&dispatch_protocol_error);
105 dispatch_set(SSH2_MSG_SERVICE_REQUEST, &input_service_request);
106 dispatch_run(DISPATCH_BLOCK, &authctxt->success, authctxt);
112 input_service_request(int type, u_int32_t seq, void *ctxt)
114 Authctxt *authctxt = ctxt;
117 char *service = packet_get_string(&len);
120 if (authctxt == NULL)
121 fatal("input_service_request: no authctxt");
123 if (strcmp(service, "ssh-userauth") == 0) {
124 if (!authctxt->success) {
126 /* now we can handle user-auth requests */
127 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &input_userauth_request);
130 /* XXX all other service requests are denied */
133 packet_start(SSH2_MSG_SERVICE_ACCEPT);
134 packet_put_cstring(service);
138 debug("bad service request %s", service);
139 packet_disconnect("bad service request %s", service);
145 input_userauth_request(int type, u_int32_t seq, void *ctxt)
147 Authctxt *authctxt = ctxt;
148 Authmethod *m = NULL;
149 char *user, *service, *method, *style = NULL;
150 int authenticated = 0;
152 if (authctxt == NULL)
153 fatal("input_userauth_request: no authctxt");
155 user = packet_get_string(NULL);
156 service = packet_get_string(NULL);
157 method = packet_get_string(NULL);
160 if (strcmp(user, "") == 0) {
161 debug("received empty username for %s", method);
162 if (strcmp(method, "external-keyx") == 0) {
164 PRIVSEP(ssh_gssapi_localname(&lname));
165 if (lname && lname[0] != '\0') {
168 debug("set username to %s from gssapi context", user);
169 } else if (authctxt->valid) {
170 debug("failed to set username from gssapi context");
176 debug("userauth-request for user %s service %s method %s",
177 (user && user[0]) ? user : "<implicit>", service, method);
178 debug("attempt %d failures %d", authctxt->attempt, authctxt->failures);
180 if ((style = strchr(user, ':')) != NULL)
184 if (!authctxt->user ||
185 strcmp(user, authctxt->user) != 0) {
186 /* setup auth context */
187 if (authctxt->user) {
188 xfree(authctxt->user);
189 authctxt->user = NULL;
191 if (authctxt->service) {
192 xfree(authctxt->service);
193 authctxt->service = NULL;
195 if (authctxt->style) {
196 xfree(authctxt->style);
197 authctxt->style = NULL;
200 /* We'll verify the username after we set it from the
202 if ((strcmp(user, "") == 0) &&
203 ((strcmp(method, "gssapi") == 0) ||
204 (strcmp(method, "external-keyx") == 0))) {
209 authctxt->pw = PRIVSEP(getpwnamallow(user));
210 if (authctxt->pw && strcmp(service, "ssh-connection")==0) {
212 debug2("input_userauth_request: setting up authctxt for %s", user);
215 PRIVSEP(start_pam(authctxt->pw->pw_name));
218 logit("input_userauth_request: illegal user %s", user);
219 authctxt->pw = fakepw();
222 PRIVSEP(start_pam(user));
228 setproctitle("%s%s", authctxt->pw ? user : "unknown",
229 use_privsep ? " [net]" : "");
230 authctxt->user = xstrdup(user);
231 authctxt->service = xstrdup(service);
232 authctxt->style = style ? xstrdup(style) : NULL;
233 if (use_privsep && (authctxt->attempt == 1))
234 mm_inform_authserv(service, style);
235 } else if (strcmp(service, authctxt->service) != 0) {
236 packet_disconnect("Change of service not allowed: "
237 "(%s,%s) -> (%s,%s)",
238 authctxt->user, authctxt->service, user, service);
241 auth2_challenge_stop(authctxt);
244 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
245 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, NULL);
248 authctxt->postponed = 0;
250 /* try to authenticate user */
251 m = authmethod_lookup(method);
253 debug2("input_userauth_request: try method %s", method);
254 authenticated = m->userauth(authctxt);
256 userauth_finish(authctxt, authenticated, method);
264 userauth_finish(Authctxt *authctxt, int authenticated, char *method)
268 if (!authctxt->valid && authenticated)
269 fatal("INTERNAL ERROR: authenticated invalid user %s",
272 /* Special handling for root */
273 if (authenticated && authctxt->pw->pw_uid == 0 &&
274 !auth_root_allowed(method))
278 if (options.use_pam && authenticated && !PRIVSEP(do_pam_account()))
283 if (authenticated && cray_access_denied(authctxt->user)) {
285 fatal("Access denied for user %s.",authctxt->user);
289 /* Log before sending the reply */
290 auth_log(authctxt, authenticated, method, " ssh2");
292 if (authctxt->postponed)
295 /* XXX todo: check if multiple auth methods are needed */
296 if (authenticated == 1) {
297 /* turn off userauth */
298 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &dispatch_protocol_ignore);
299 packet_start(SSH2_MSG_USERAUTH_SUCCESS);
302 /* now we can break out */
303 authctxt->success = 1;
305 if (authctxt->failures++ > AUTH_FAIL_MAX)
306 packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
307 methods = authmethods_get();
308 packet_start(SSH2_MSG_USERAUTH_FAILURE);
309 packet_put_cstring(methods);
310 packet_put_char(0); /* XXX partial success, unused */
317 /* get current user */
322 return (x_authctxt != NULL && x_authctxt->valid) ? x_authctxt->pw : NULL;
328 authmethods_get(void)
335 for (i = 0; authmethods[i] != NULL; i++) {
336 if (strcmp(authmethods[i]->name, "none") == 0)
338 if (authmethods[i]->enabled != NULL &&
339 *(authmethods[i]->enabled) != 0) {
340 if (buffer_len(&b) > 0)
341 buffer_append(&b, ",", 1);
342 buffer_append(&b, authmethods[i]->name,
343 strlen(authmethods[i]->name));
346 buffer_append(&b, "\0", 1);
347 list = xstrdup(buffer_ptr(&b));
353 authmethod_lookup(const char *name)
358 for (i = 0; authmethods[i] != NULL; i++)
359 if (authmethods[i]->enabled != NULL &&
360 *(authmethods[i]->enabled) != 0 &&
361 strcmp(name, authmethods[i]->name) == 0)
362 return authmethods[i];
363 debug2("Unrecognized authentication method name: %s",
364 name ? name : "NULL");