xref: /openbsd-src/usr.bin/ssh/auth2.c (revision db3296cf5c1dd9058ceecc3a29fe4aaa0bd26000)
1 /*
2  * Copyright (c) 2000 Markus Friedl.  All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
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.
12  *
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.
23  */
24 
25 #include "includes.h"
26 RCSID("$OpenBSD: auth2.c,v 1.99 2003/06/24 08:23:46 markus Exp $");
27 
28 #include "ssh2.h"
29 #include "xmalloc.h"
30 #include "packet.h"
31 #include "log.h"
32 #include "servconf.h"
33 #include "compat.h"
34 #include "auth.h"
35 #include "dispatch.h"
36 #include "pathnames.h"
37 #include "monitor_wrap.h"
38 
39 /* import */
40 extern ServerOptions options;
41 extern u_char *session_id2;
42 extern u_int session_id2_len;
43 
44 Authctxt *x_authctxt = NULL;
45 
46 /* methods */
47 
48 extern Authmethod method_none;
49 extern Authmethod method_pubkey;
50 extern Authmethod method_passwd;
51 extern Authmethod method_kbdint;
52 extern Authmethod method_hostbased;
53 #ifdef KRB5
54 extern Authmethod method_kerberos;
55 #endif
56 
57 Authmethod *authmethods[] = {
58 	&method_none,
59 	&method_pubkey,
60 	&method_passwd,
61 	&method_kbdint,
62 	&method_hostbased,
63 #ifdef KRB5
64 	&method_kerberos,
65 #endif
66 	NULL
67 };
68 
69 /* protocol */
70 
71 static void input_service_request(int, u_int32_t, void *);
72 static void input_userauth_request(int, u_int32_t, void *);
73 
74 /* helper */
75 static Authmethod *authmethod_lookup(const char *);
76 static char *authmethods_get(void);
77 int user_key_allowed(struct passwd *, Key *);
78 int hostbased_key_allowed(struct passwd *, const char *, char *, Key *);
79 
80 /*
81  * loop until authctxt->success == TRUE
82  */
83 
84 Authctxt *
85 do_authentication2(void)
86 {
87 	Authctxt *authctxt = authctxt_new();
88 
89 	x_authctxt = authctxt;		/*XXX*/
90 
91 	/* challenge-response is implemented via keyboard interactive */
92 	if (options.challenge_response_authentication)
93 		options.kbd_interactive_authentication = 1;
94 
95 	dispatch_init(&dispatch_protocol_error);
96 	dispatch_set(SSH2_MSG_SERVICE_REQUEST, &input_service_request);
97 	dispatch_run(DISPATCH_BLOCK, &authctxt->success, authctxt);
98 
99 	return (authctxt);
100 }
101 
102 static void
103 input_service_request(int type, u_int32_t seq, void *ctxt)
104 {
105 	Authctxt *authctxt = ctxt;
106 	u_int len;
107 	int acceptit = 0;
108 	char *service = packet_get_string(&len);
109 	packet_check_eom();
110 
111 	if (authctxt == NULL)
112 		fatal("input_service_request: no authctxt");
113 
114 	if (strcmp(service, "ssh-userauth") == 0) {
115 		if (!authctxt->success) {
116 			acceptit = 1;
117 			/* now we can handle user-auth requests */
118 			dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &input_userauth_request);
119 		}
120 	}
121 	/* XXX all other service requests are denied */
122 
123 	if (acceptit) {
124 		packet_start(SSH2_MSG_SERVICE_ACCEPT);
125 		packet_put_cstring(service);
126 		packet_send();
127 		packet_write_wait();
128 	} else {
129 		debug("bad service request %s", service);
130 		packet_disconnect("bad service request %s", service);
131 	}
132 	xfree(service);
133 }
134 
135 static void
136 input_userauth_request(int type, u_int32_t seq, void *ctxt)
137 {
138 	Authctxt *authctxt = ctxt;
139 	Authmethod *m = NULL;
140 	char *user, *service, *method, *style = NULL;
141 	int authenticated = 0;
142 
143 	if (authctxt == NULL)
144 		fatal("input_userauth_request: no authctxt");
145 
146 	user = packet_get_string(NULL);
147 	service = packet_get_string(NULL);
148 	method = packet_get_string(NULL);
149 	debug("userauth-request for user %s service %s method %s", user, service, method);
150 	debug("attempt %d failures %d", authctxt->attempt, authctxt->failures);
151 
152 	if ((style = strchr(user, ':')) != NULL)
153 		*style++ = 0;
154 
155 	if (authctxt->attempt++ == 0) {
156 		/* setup auth context */
157 		authctxt->pw = PRIVSEP(getpwnamallow(user));
158 		if (authctxt->pw && strcmp(service, "ssh-connection")==0) {
159 			authctxt->valid = 1;
160 			debug2("input_userauth_request: setting up authctxt for %s", user);
161 		} else {
162 			logit("input_userauth_request: illegal user %s", user);
163 		}
164 		setproctitle("%s%s", authctxt->pw ? user : "unknown",
165 		    use_privsep ? " [net]" : "");
166 		authctxt->user = xstrdup(user);
167 		authctxt->service = xstrdup(service);
168 		authctxt->style = style ? xstrdup(style) : NULL;
169 		if (use_privsep)
170 			mm_inform_authserv(service, style);
171 	} else if (strcmp(user, authctxt->user) != 0 ||
172 	    strcmp(service, authctxt->service) != 0) {
173 		packet_disconnect("Change of username or service not allowed: "
174 		    "(%s,%s) -> (%s,%s)",
175 		    authctxt->user, authctxt->service, user, service);
176 	}
177 	/* reset state */
178 	auth2_challenge_stop(authctxt);
179 	authctxt->postponed = 0;
180 
181 	/* try to authenticate user */
182 	m = authmethod_lookup(method);
183 	if (m != NULL) {
184 		debug2("input_userauth_request: try method %s", method);
185 		authenticated =	m->userauth(authctxt);
186 	}
187 	userauth_finish(authctxt, authenticated, method);
188 
189 	xfree(service);
190 	xfree(user);
191 	xfree(method);
192 }
193 
194 void
195 userauth_finish(Authctxt *authctxt, int authenticated, char *method)
196 {
197 	char *methods;
198 
199 	if (!authctxt->valid && authenticated)
200 		fatal("INTERNAL ERROR: authenticated invalid user %s",
201 		    authctxt->user);
202 
203 	/* Special handling for root */
204 	if (authenticated && authctxt->pw->pw_uid == 0 &&
205 	    !auth_root_allowed(method))
206 		authenticated = 0;
207 
208 	/* Log before sending the reply */
209 	auth_log(authctxt, authenticated, method, " ssh2");
210 
211 	if (authctxt->postponed)
212 		return;
213 
214 	/* XXX todo: check if multiple auth methods are needed */
215 	if (authenticated == 1) {
216 		/* turn off userauth */
217 		dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &dispatch_protocol_ignore);
218 		packet_start(SSH2_MSG_USERAUTH_SUCCESS);
219 		packet_send();
220 		packet_write_wait();
221 		/* now we can break out */
222 		authctxt->success = 1;
223 	} else {
224 		if (authctxt->failures++ > AUTH_FAIL_MAX)
225 			packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
226 		methods = authmethods_get();
227 		packet_start(SSH2_MSG_USERAUTH_FAILURE);
228 		packet_put_cstring(methods);
229 		packet_put_char(0);	/* XXX partial success, unused */
230 		packet_send();
231 		packet_write_wait();
232 		xfree(methods);
233 	}
234 }
235 
236 /* get current user */
237 
238 struct passwd*
239 auth_get_user(void)
240 {
241 	return (x_authctxt != NULL && x_authctxt->valid) ? x_authctxt->pw : NULL;
242 }
243 
244 #define	DELIM	","
245 
246 static char *
247 authmethods_get(void)
248 {
249 	Buffer b;
250 	char *list;
251 	int i;
252 
253 	buffer_init(&b);
254 	for (i = 0; authmethods[i] != NULL; i++) {
255 		if (strcmp(authmethods[i]->name, "none") == 0)
256 			continue;
257 		if (authmethods[i]->enabled != NULL &&
258 		    *(authmethods[i]->enabled) != 0) {
259 			if (buffer_len(&b) > 0)
260 				buffer_append(&b, ",", 1);
261 			buffer_append(&b, authmethods[i]->name,
262 			    strlen(authmethods[i]->name));
263 		}
264 	}
265 	buffer_append(&b, "\0", 1);
266 	list = xstrdup(buffer_ptr(&b));
267 	buffer_free(&b);
268 	return list;
269 }
270 
271 static Authmethod *
272 authmethod_lookup(const char *name)
273 {
274 	int i;
275 
276 	if (name != NULL)
277 		for (i = 0; authmethods[i] != NULL; i++)
278 			if (authmethods[i]->enabled != NULL &&
279 			    *(authmethods[i]->enabled) != 0 &&
280 			    strcmp(name, authmethods[i]->name) == 0)
281 				return authmethods[i];
282 	debug2("Unrecognized authentication method name: %s",
283 	    name ? name : "NULL");
284 	return NULL;
285 }
286