xref: /openbsd-src/usr.bin/ssh/auth2.c (revision 850e275390052b330d93020bf619a739a3c277ac)
1 /* $OpenBSD: auth2.c,v 1.119 2008/07/04 23:30:16 djm Exp $ */
2 /*
3  * Copyright (c) 2000 Markus Friedl.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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.
24  */
25 
26 
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <sys/uio.h>
30 
31 #include <fcntl.h>
32 #include <pwd.h>
33 #include <string.h>
34 #include <stdarg.h>
35 #include <unistd.h>
36 
37 #include "atomicio.h"
38 #include "xmalloc.h"
39 #include "ssh2.h"
40 #include "packet.h"
41 #include "log.h"
42 #include "buffer.h"
43 #include "servconf.h"
44 #include "compat.h"
45 #include "key.h"
46 #include "hostfile.h"
47 #include "auth.h"
48 #include "dispatch.h"
49 #include "pathnames.h"
50 #ifdef GSSAPI
51 #include "ssh-gss.h"
52 #endif
53 #include "monitor_wrap.h"
54 
55 /* import */
56 extern ServerOptions options;
57 extern u_char *session_id2;
58 extern u_int session_id2_len;
59 
60 /* methods */
61 
62 extern Authmethod method_none;
63 extern Authmethod method_pubkey;
64 extern Authmethod method_passwd;
65 extern Authmethod method_kbdint;
66 extern Authmethod method_hostbased;
67 #ifdef GSSAPI
68 extern Authmethod method_gssapi;
69 #endif
70 
71 Authmethod *authmethods[] = {
72 	&method_none,
73 	&method_pubkey,
74 #ifdef GSSAPI
75 	&method_gssapi,
76 #endif
77 	&method_passwd,
78 	&method_kbdint,
79 	&method_hostbased,
80 	NULL
81 };
82 
83 /* protocol */
84 
85 static void input_service_request(int, u_int32_t, void *);
86 static void input_userauth_request(int, u_int32_t, void *);
87 
88 /* helper */
89 static Authmethod *authmethod_lookup(const char *);
90 static char *authmethods_get(void);
91 
92 char *
93 auth2_read_banner(void)
94 {
95 	struct stat st;
96 	char *banner = NULL;
97 	size_t len, n;
98 	int fd;
99 
100 	if ((fd = open(options.banner, O_RDONLY)) == -1)
101 		return (NULL);
102 	if (fstat(fd, &st) == -1) {
103 		close(fd);
104 		return (NULL);
105 	}
106 	if (st.st_size > 1*1024*1024) {
107 		close(fd);
108 		return (NULL);
109 	}
110 
111 	len = (size_t)st.st_size;		/* truncate */
112 	banner = xmalloc(len + 1);
113 	n = atomicio(read, fd, banner, len);
114 	close(fd);
115 
116 	if (n != len) {
117 		xfree(banner);
118 		return (NULL);
119 	}
120 	banner[n] = '\0';
121 
122 	return (banner);
123 }
124 
125 static void
126 userauth_banner(void)
127 {
128 	char *banner = NULL;
129 
130 	if (options.banner == NULL ||
131 	    strcasecmp(options.banner, "none") == 0 ||
132 	    (datafellows & SSH_BUG_BANNER) != 0)
133 		return;
134 
135 	if ((banner = PRIVSEP(auth2_read_banner())) == NULL)
136 		goto done;
137 
138 	packet_start(SSH2_MSG_USERAUTH_BANNER);
139 	packet_put_cstring(banner);
140 	packet_put_cstring("");		/* language, unused */
141 	packet_send();
142 	debug("userauth_banner: sent");
143 done:
144 	if (banner)
145 		xfree(banner);
146 }
147 
148 /*
149  * loop until authctxt->success == TRUE
150  */
151 void
152 do_authentication2(Authctxt *authctxt)
153 {
154 	dispatch_init(&dispatch_protocol_error);
155 	dispatch_set(SSH2_MSG_SERVICE_REQUEST, &input_service_request);
156 	dispatch_run(DISPATCH_BLOCK, &authctxt->success, authctxt);
157 }
158 
159 /*ARGSUSED*/
160 static void
161 input_service_request(int type, u_int32_t seq, void *ctxt)
162 {
163 	Authctxt *authctxt = ctxt;
164 	u_int len;
165 	int acceptit = 0;
166 	char *service = packet_get_string(&len);
167 	packet_check_eom();
168 
169 	if (authctxt == NULL)
170 		fatal("input_service_request: no authctxt");
171 
172 	if (strcmp(service, "ssh-userauth") == 0) {
173 		if (!authctxt->success) {
174 			acceptit = 1;
175 			/* now we can handle user-auth requests */
176 			dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &input_userauth_request);
177 		}
178 	}
179 	/* XXX all other service requests are denied */
180 
181 	if (acceptit) {
182 		packet_start(SSH2_MSG_SERVICE_ACCEPT);
183 		packet_put_cstring(service);
184 		packet_send();
185 		packet_write_wait();
186 	} else {
187 		debug("bad service request %s", service);
188 		packet_disconnect("bad service request %s", service);
189 	}
190 	xfree(service);
191 }
192 
193 /*ARGSUSED*/
194 static void
195 input_userauth_request(int type, u_int32_t seq, void *ctxt)
196 {
197 	Authctxt *authctxt = ctxt;
198 	Authmethod *m = NULL;
199 	char *user, *service, *method, *style = NULL;
200 	int authenticated = 0;
201 
202 	if (authctxt == NULL)
203 		fatal("input_userauth_request: no authctxt");
204 
205 	user = packet_get_string(NULL);
206 	service = packet_get_string(NULL);
207 	method = packet_get_string(NULL);
208 	debug("userauth-request for user %s service %s method %s", user, service, method);
209 	debug("attempt %d failures %d", authctxt->attempt, authctxt->failures);
210 
211 	if ((style = strchr(user, ':')) != NULL)
212 		*style++ = 0;
213 
214 	if (authctxt->attempt++ == 0) {
215 		/* setup auth context */
216 		authctxt->pw = PRIVSEP(getpwnamallow(user));
217 		if (authctxt->pw && strcmp(service, "ssh-connection")==0) {
218 			authctxt->valid = 1;
219 			debug2("input_userauth_request: setting up authctxt for %s", user);
220 		} else {
221 			logit("input_userauth_request: invalid user %s", user);
222 			authctxt->pw = fakepw();
223 		}
224 		setproctitle("%s%s", authctxt->valid ? user : "unknown",
225 		    use_privsep ? " [net]" : "");
226 		authctxt->user = xstrdup(user);
227 		authctxt->service = xstrdup(service);
228 		authctxt->style = style ? xstrdup(style) : NULL;
229 		if (use_privsep)
230 			mm_inform_authserv(service, style);
231 		userauth_banner();
232 	} else if (strcmp(user, authctxt->user) != 0 ||
233 	    strcmp(service, authctxt->service) != 0) {
234 		packet_disconnect("Change of username or service not allowed: "
235 		    "(%s,%s) -> (%s,%s)",
236 		    authctxt->user, authctxt->service, user, service);
237 	}
238 	/* reset state */
239 	auth2_challenge_stop(authctxt);
240 
241 #ifdef GSSAPI
242 	dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
243 	dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, NULL);
244 #endif
245 
246 	authctxt->postponed = 0;
247 
248 	/* try to authenticate user */
249 	m = authmethod_lookup(method);
250 	if (m != NULL && authctxt->failures < options.max_authtries) {
251 		debug2("input_userauth_request: try method %s", method);
252 		authenticated =	m->userauth(authctxt);
253 	}
254 	userauth_finish(authctxt, authenticated, method);
255 
256 	xfree(service);
257 	xfree(user);
258 	xfree(method);
259 }
260 
261 void
262 userauth_finish(Authctxt *authctxt, int authenticated, char *method)
263 {
264 	char *methods;
265 
266 	if (!authctxt->valid && authenticated)
267 		fatal("INTERNAL ERROR: authenticated invalid user %s",
268 		    authctxt->user);
269 
270 	/* Special handling for root */
271 	if (authenticated && authctxt->pw->pw_uid == 0 &&
272 	    !auth_root_allowed(method))
273 		authenticated = 0;
274 
275 	/* Log before sending the reply */
276 	auth_log(authctxt, authenticated, method, " ssh2");
277 
278 	if (authctxt->postponed)
279 		return;
280 
281 	/* XXX todo: check if multiple auth methods are needed */
282 	if (authenticated == 1) {
283 		/* turn off userauth */
284 		dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &dispatch_protocol_ignore);
285 		packet_start(SSH2_MSG_USERAUTH_SUCCESS);
286 		packet_send();
287 		packet_write_wait();
288 		/* now we can break out */
289 		authctxt->success = 1;
290 	} else {
291 		/* Allow initial try of "none" auth without failure penalty */
292 		if (authctxt->attempt > 1 || strcmp(method, "none") != 0)
293 			authctxt->failures++;
294 		if (authctxt->failures >= options.max_authtries)
295 			packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
296 		methods = authmethods_get();
297 		packet_start(SSH2_MSG_USERAUTH_FAILURE);
298 		packet_put_cstring(methods);
299 		packet_put_char(0);	/* XXX partial success, unused */
300 		packet_send();
301 		packet_write_wait();
302 		xfree(methods);
303 	}
304 }
305 
306 static char *
307 authmethods_get(void)
308 {
309 	Buffer b;
310 	char *list;
311 	int i;
312 
313 	buffer_init(&b);
314 	for (i = 0; authmethods[i] != NULL; i++) {
315 		if (strcmp(authmethods[i]->name, "none") == 0)
316 			continue;
317 		if (authmethods[i]->enabled != NULL &&
318 		    *(authmethods[i]->enabled) != 0) {
319 			if (buffer_len(&b) > 0)
320 				buffer_append(&b, ",", 1);
321 			buffer_append(&b, authmethods[i]->name,
322 			    strlen(authmethods[i]->name));
323 		}
324 	}
325 	buffer_append(&b, "\0", 1);
326 	list = xstrdup(buffer_ptr(&b));
327 	buffer_free(&b);
328 	return list;
329 }
330 
331 static Authmethod *
332 authmethod_lookup(const char *name)
333 {
334 	int i;
335 
336 	if (name != NULL)
337 		for (i = 0; authmethods[i] != NULL; i++)
338 			if (authmethods[i]->enabled != NULL &&
339 			    *(authmethods[i]->enabled) != 0 &&
340 			    strcmp(name, authmethods[i]->name) == 0)
341 				return authmethods[i];
342 	debug2("Unrecognized authentication method name: %s",
343 	    name ? name : "NULL");
344 	return NULL;
345 }
346 
347