auth2.c revision 1.5 1 1.5 christos /* $NetBSD: auth2.c,v 1.5 2011/09/07 17:49:19 christos Exp $ */
2 1.5 christos /* $OpenBSD: auth2.c,v 1.123 2011/03/10 02:52:57 djm Exp $ */
3 1.1 christos /*
4 1.1 christos * Copyright (c) 2000 Markus Friedl. All rights reserved.
5 1.1 christos *
6 1.1 christos * Redistribution and use in source and binary forms, with or without
7 1.1 christos * modification, are permitted provided that the following conditions
8 1.1 christos * are met:
9 1.1 christos * 1. Redistributions of source code must retain the above copyright
10 1.1 christos * notice, this list of conditions and the following disclaimer.
11 1.1 christos * 2. Redistributions in binary form must reproduce the above copyright
12 1.1 christos * notice, this list of conditions and the following disclaimer in the
13 1.1 christos * documentation and/or other materials provided with the distribution.
14 1.1 christos *
15 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 1.1 christos * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 1.1 christos * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 1.1 christos * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 1.1 christos * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 1.1 christos * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 1.1 christos * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 1.1 christos * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 1.1 christos * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 1.1 christos * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 1.1 christos */
26 1.1 christos
27 1.2 christos #include "includes.h"
28 1.5 christos __RCSID("$NetBSD: auth2.c,v 1.5 2011/09/07 17:49:19 christos Exp $");
29 1.1 christos #include <sys/types.h>
30 1.1 christos #include <sys/stat.h>
31 1.1 christos #include <sys/uio.h>
32 1.1 christos
33 1.1 christos #include <fcntl.h>
34 1.1 christos #include <pwd.h>
35 1.3 christos #include <stdarg.h>
36 1.1 christos #include <string.h>
37 1.1 christos #include <unistd.h>
38 1.1 christos
39 1.1 christos #include "atomicio.h"
40 1.1 christos #include "xmalloc.h"
41 1.1 christos #include "ssh2.h"
42 1.1 christos #include "packet.h"
43 1.1 christos #include "log.h"
44 1.1 christos #include "buffer.h"
45 1.1 christos #include "servconf.h"
46 1.1 christos #include "compat.h"
47 1.1 christos #include "key.h"
48 1.1 christos #include "hostfile.h"
49 1.1 christos #include "auth.h"
50 1.1 christos #include "dispatch.h"
51 1.1 christos #include "pathnames.h"
52 1.2 christos #include "buffer.h"
53 1.2 christos #include "canohost.h"
54 1.2 christos
55 1.1 christos #ifdef GSSAPI
56 1.1 christos #include "ssh-gss.h"
57 1.1 christos #endif
58 1.2 christos
59 1.1 christos #include "monitor_wrap.h"
60 1.1 christos
61 1.1 christos /* import */
62 1.1 christos extern ServerOptions options;
63 1.1 christos extern u_char *session_id2;
64 1.1 christos extern u_int session_id2_len;
65 1.2 christos extern Buffer loginmsg;
66 1.1 christos
67 1.1 christos /* methods */
68 1.1 christos
69 1.1 christos extern Authmethod method_none;
70 1.1 christos extern Authmethod method_pubkey;
71 1.1 christos extern Authmethod method_passwd;
72 1.1 christos extern Authmethod method_kbdint;
73 1.1 christos extern Authmethod method_hostbased;
74 1.2 christos #ifdef KRB5
75 1.2 christos extern Authmethod method_kerberos;
76 1.2 christos #endif
77 1.1 christos #ifdef GSSAPI
78 1.1 christos extern Authmethod method_gssapi;
79 1.1 christos #endif
80 1.1 christos #ifdef JPAKE
81 1.1 christos extern Authmethod method_jpake;
82 1.1 christos #endif
83 1.1 christos
84 1.2 christos static int log_flag = 0;
85 1.2 christos
86 1.1 christos Authmethod *authmethods[] = {
87 1.1 christos &method_none,
88 1.1 christos &method_pubkey,
89 1.1 christos #ifdef GSSAPI
90 1.1 christos &method_gssapi,
91 1.1 christos #endif
92 1.1 christos #ifdef JPAKE
93 1.1 christos &method_jpake,
94 1.1 christos #endif
95 1.1 christos &method_passwd,
96 1.1 christos &method_kbdint,
97 1.1 christos &method_hostbased,
98 1.2 christos #ifdef KRB5
99 1.2 christos &method_kerberos,
100 1.2 christos #endif
101 1.1 christos NULL
102 1.1 christos };
103 1.1 christos
104 1.1 christos /* protocol */
105 1.1 christos
106 1.1 christos static void input_service_request(int, u_int32_t, void *);
107 1.1 christos static void input_userauth_request(int, u_int32_t, void *);
108 1.1 christos
109 1.1 christos /* helper */
110 1.1 christos static Authmethod *authmethod_lookup(const char *);
111 1.1 christos static char *authmethods_get(void);
112 1.1 christos
113 1.1 christos char *
114 1.1 christos auth2_read_banner(void)
115 1.1 christos {
116 1.1 christos struct stat st;
117 1.1 christos char *banner = NULL;
118 1.1 christos size_t len, n;
119 1.1 christos int fd;
120 1.1 christos
121 1.1 christos if ((fd = open(options.banner, O_RDONLY)) == -1)
122 1.1 christos return (NULL);
123 1.1 christos if (fstat(fd, &st) == -1) {
124 1.1 christos close(fd);
125 1.1 christos return (NULL);
126 1.1 christos }
127 1.1 christos if (st.st_size > 1*1024*1024) {
128 1.1 christos close(fd);
129 1.1 christos return (NULL);
130 1.1 christos }
131 1.1 christos
132 1.1 christos len = (size_t)st.st_size; /* truncate */
133 1.1 christos banner = xmalloc(len + 1);
134 1.1 christos n = atomicio(read, fd, banner, len);
135 1.1 christos close(fd);
136 1.1 christos
137 1.1 christos if (n != len) {
138 1.1 christos xfree(banner);
139 1.1 christos return (NULL);
140 1.1 christos }
141 1.1 christos banner[n] = '\0';
142 1.1 christos
143 1.1 christos return (banner);
144 1.1 christos }
145 1.1 christos
146 1.2 christos void
147 1.2 christos userauth_send_banner(const char *msg)
148 1.2 christos {
149 1.2 christos if (datafellows & SSH_BUG_BANNER)
150 1.2 christos return;
151 1.2 christos
152 1.2 christos packet_start(SSH2_MSG_USERAUTH_BANNER);
153 1.2 christos packet_put_cstring(msg);
154 1.2 christos packet_put_cstring(""); /* language, unused */
155 1.2 christos packet_send();
156 1.2 christos debug("%s: sent", __func__);
157 1.2 christos }
158 1.2 christos
159 1.1 christos static void
160 1.1 christos userauth_banner(void)
161 1.1 christos {
162 1.1 christos char *banner = NULL;
163 1.1 christos
164 1.1 christos if (options.banner == NULL ||
165 1.1 christos strcasecmp(options.banner, "none") == 0 ||
166 1.1 christos (datafellows & SSH_BUG_BANNER) != 0)
167 1.1 christos return;
168 1.1 christos
169 1.1 christos if ((banner = PRIVSEP(auth2_read_banner())) == NULL)
170 1.1 christos goto done;
171 1.1 christos
172 1.2 christos userauth_send_banner(banner);
173 1.1 christos done:
174 1.1 christos if (banner)
175 1.1 christos xfree(banner);
176 1.1 christos }
177 1.1 christos
178 1.1 christos /*
179 1.1 christos * loop until authctxt->success == TRUE
180 1.1 christos */
181 1.1 christos void
182 1.1 christos do_authentication2(Authctxt *authctxt)
183 1.1 christos {
184 1.1 christos dispatch_init(&dispatch_protocol_error);
185 1.1 christos dispatch_set(SSH2_MSG_SERVICE_REQUEST, &input_service_request);
186 1.1 christos dispatch_run(DISPATCH_BLOCK, &authctxt->success, authctxt);
187 1.1 christos }
188 1.1 christos
189 1.1 christos /*ARGSUSED*/
190 1.1 christos static void
191 1.1 christos input_service_request(int type, u_int32_t seq, void *ctxt)
192 1.1 christos {
193 1.1 christos Authctxt *authctxt = ctxt;
194 1.1 christos u_int len;
195 1.1 christos int acceptit = 0;
196 1.4 christos char *service = packet_get_cstring(&len);
197 1.1 christos packet_check_eom();
198 1.1 christos
199 1.1 christos if (authctxt == NULL)
200 1.1 christos fatal("input_service_request: no authctxt");
201 1.1 christos
202 1.1 christos if (strcmp(service, "ssh-userauth") == 0) {
203 1.1 christos if (!authctxt->success) {
204 1.1 christos acceptit = 1;
205 1.1 christos /* now we can handle user-auth requests */
206 1.1 christos dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &input_userauth_request);
207 1.1 christos }
208 1.1 christos }
209 1.1 christos /* XXX all other service requests are denied */
210 1.1 christos
211 1.1 christos if (acceptit) {
212 1.1 christos packet_start(SSH2_MSG_SERVICE_ACCEPT);
213 1.1 christos packet_put_cstring(service);
214 1.1 christos packet_send();
215 1.1 christos packet_write_wait();
216 1.1 christos } else {
217 1.1 christos debug("bad service request %s", service);
218 1.1 christos packet_disconnect("bad service request %s", service);
219 1.1 christos }
220 1.1 christos xfree(service);
221 1.1 christos }
222 1.1 christos
223 1.1 christos /*ARGSUSED*/
224 1.1 christos static void
225 1.1 christos input_userauth_request(int type, u_int32_t seq, void *ctxt)
226 1.1 christos {
227 1.1 christos Authctxt *authctxt = ctxt;
228 1.1 christos Authmethod *m = NULL;
229 1.1 christos char *user, *service, *method, *style = NULL;
230 1.1 christos int authenticated = 0;
231 1.1 christos
232 1.1 christos if (authctxt == NULL)
233 1.1 christos fatal("input_userauth_request: no authctxt");
234 1.1 christos
235 1.4 christos user = packet_get_cstring(NULL);
236 1.4 christos service = packet_get_cstring(NULL);
237 1.4 christos method = packet_get_cstring(NULL);
238 1.1 christos debug("userauth-request for user %s service %s method %s", user, service, method);
239 1.2 christos if (!log_flag) {
240 1.2 christos logit("SSH: Server;Ltype: Authname;Remote: %s-%d;Name: %s",
241 1.2 christos get_remote_ipaddr(), get_remote_port(), user);
242 1.2 christos log_flag = 1;
243 1.2 christos }
244 1.1 christos debug("attempt %d failures %d", authctxt->attempt, authctxt->failures);
245 1.1 christos
246 1.1 christos if ((style = strchr(user, ':')) != NULL)
247 1.1 christos *style++ = 0;
248 1.1 christos
249 1.1 christos if (authctxt->attempt++ == 0) {
250 1.1 christos /* setup auth context */
251 1.1 christos authctxt->pw = PRIVSEP(getpwnamallow(user));
252 1.2 christos authctxt->user = xstrdup(user);
253 1.1 christos if (authctxt->pw && strcmp(service, "ssh-connection")==0) {
254 1.1 christos authctxt->valid = 1;
255 1.1 christos debug2("input_userauth_request: setting up authctxt for %s", user);
256 1.1 christos } else {
257 1.1 christos logit("input_userauth_request: invalid user %s", user);
258 1.1 christos authctxt->pw = fakepw();
259 1.1 christos }
260 1.2 christos #ifdef USE_PAM
261 1.2 christos if (options.use_pam)
262 1.2 christos PRIVSEP(start_pam(authctxt));
263 1.2 christos #endif
264 1.1 christos setproctitle("%s%s", authctxt->valid ? user : "unknown",
265 1.1 christos use_privsep ? " [net]" : "");
266 1.1 christos authctxt->service = xstrdup(service);
267 1.1 christos authctxt->style = style ? xstrdup(style) : NULL;
268 1.1 christos if (use_privsep)
269 1.1 christos mm_inform_authserv(service, style);
270 1.1 christos userauth_banner();
271 1.1 christos } else if (strcmp(user, authctxt->user) != 0 ||
272 1.1 christos strcmp(service, authctxt->service) != 0) {
273 1.1 christos packet_disconnect("Change of username or service not allowed: "
274 1.1 christos "(%s,%s) -> (%s,%s)",
275 1.1 christos authctxt->user, authctxt->service, user, service);
276 1.1 christos }
277 1.1 christos /* reset state */
278 1.1 christos auth2_challenge_stop(authctxt);
279 1.1 christos #ifdef JPAKE
280 1.1 christos auth2_jpake_stop(authctxt);
281 1.1 christos #endif
282 1.1 christos
283 1.1 christos #ifdef GSSAPI
284 1.1 christos /* XXX move to auth2_gssapi_stop() */
285 1.1 christos dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
286 1.1 christos dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, NULL);
287 1.1 christos #endif
288 1.1 christos
289 1.1 christos authctxt->postponed = 0;
290 1.5 christos authctxt->server_caused_failure = 0;
291 1.1 christos
292 1.1 christos /* try to authenticate user */
293 1.1 christos m = authmethod_lookup(method);
294 1.1 christos if (m != NULL && authctxt->failures < options.max_authtries) {
295 1.1 christos debug2("input_userauth_request: try method %s", method);
296 1.1 christos authenticated = m->userauth(authctxt);
297 1.1 christos }
298 1.1 christos userauth_finish(authctxt, authenticated, method);
299 1.1 christos
300 1.1 christos xfree(service);
301 1.1 christos xfree(user);
302 1.1 christos xfree(method);
303 1.1 christos }
304 1.1 christos
305 1.1 christos void
306 1.4 christos userauth_finish(Authctxt *authctxt, int authenticated, const char *method)
307 1.1 christos {
308 1.1 christos char *methods;
309 1.1 christos
310 1.1 christos if (!authctxt->valid && authenticated)
311 1.1 christos fatal("INTERNAL ERROR: authenticated invalid user %s",
312 1.1 christos authctxt->user);
313 1.1 christos
314 1.1 christos /* Special handling for root */
315 1.1 christos if (authenticated && authctxt->pw->pw_uid == 0 &&
316 1.2 christos !auth_root_allowed(method)) {
317 1.1 christos authenticated = 0;
318 1.2 christos #ifdef SSH_AUDIT_EVENTS
319 1.2 christos PRIVSEP(audit_event(SSH_LOGIN_ROOT_DENIED));
320 1.2 christos #endif
321 1.2 christos }
322 1.2 christos
323 1.2 christos #ifdef USE_PAM
324 1.2 christos if (options.use_pam && authenticated) {
325 1.2 christos if (!PRIVSEP(do_pam_account())) {
326 1.2 christos /* if PAM returned a message, send it to the user */
327 1.2 christos if (buffer_len(&loginmsg) > 0) {
328 1.2 christos buffer_append(&loginmsg, "\0", 1);
329 1.2 christos userauth_send_banner(buffer_ptr(&loginmsg));
330 1.2 christos packet_write_wait();
331 1.2 christos }
332 1.2 christos fatal("Access denied for user %s by PAM account "
333 1.2 christos "configuration", authctxt->user);
334 1.2 christos }
335 1.2 christos }
336 1.2 christos #endif
337 1.1 christos
338 1.1 christos /* Log before sending the reply */
339 1.1 christos auth_log(authctxt, authenticated, method, " ssh2");
340 1.1 christos
341 1.1 christos if (authctxt->postponed)
342 1.1 christos return;
343 1.1 christos
344 1.1 christos /* XXX todo: check if multiple auth methods are needed */
345 1.1 christos if (authenticated == 1) {
346 1.1 christos /* turn off userauth */
347 1.1 christos dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &dispatch_protocol_ignore);
348 1.1 christos packet_start(SSH2_MSG_USERAUTH_SUCCESS);
349 1.1 christos packet_send();
350 1.1 christos packet_write_wait();
351 1.1 christos /* now we can break out */
352 1.1 christos authctxt->success = 1;
353 1.1 christos } else {
354 1.1 christos /* Allow initial try of "none" auth without failure penalty */
355 1.5 christos if (!authctxt->server_caused_failure &&
356 1.5 christos (authctxt->attempt > 1 || strcmp(method, "none") != 0))
357 1.1 christos authctxt->failures++;
358 1.2 christos if (authctxt->failures >= options.max_authtries) {
359 1.1 christos packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
360 1.2 christos #ifdef SSH_AUDIT_EVENTS
361 1.2 christos PRIVSEP(audit_event(SSH_LOGIN_EXCEED_MAXTRIES));
362 1.2 christos #endif
363 1.2 christos }
364 1.1 christos methods = authmethods_get();
365 1.1 christos packet_start(SSH2_MSG_USERAUTH_FAILURE);
366 1.1 christos packet_put_cstring(methods);
367 1.1 christos packet_put_char(0); /* XXX partial success, unused */
368 1.1 christos packet_send();
369 1.1 christos packet_write_wait();
370 1.1 christos xfree(methods);
371 1.1 christos }
372 1.1 christos }
373 1.1 christos
374 1.1 christos static char *
375 1.1 christos authmethods_get(void)
376 1.1 christos {
377 1.1 christos Buffer b;
378 1.1 christos char *list;
379 1.1 christos int i;
380 1.1 christos
381 1.1 christos buffer_init(&b);
382 1.1 christos for (i = 0; authmethods[i] != NULL; i++) {
383 1.1 christos if (strcmp(authmethods[i]->name, "none") == 0)
384 1.1 christos continue;
385 1.1 christos if (authmethods[i]->enabled != NULL &&
386 1.1 christos *(authmethods[i]->enabled) != 0) {
387 1.1 christos if (buffer_len(&b) > 0)
388 1.1 christos buffer_append(&b, ",", 1);
389 1.1 christos buffer_append(&b, authmethods[i]->name,
390 1.1 christos strlen(authmethods[i]->name));
391 1.1 christos }
392 1.1 christos }
393 1.1 christos buffer_append(&b, "\0", 1);
394 1.1 christos list = xstrdup(buffer_ptr(&b));
395 1.1 christos buffer_free(&b);
396 1.1 christos return list;
397 1.1 christos }
398 1.1 christos
399 1.1 christos static Authmethod *
400 1.1 christos authmethod_lookup(const char *name)
401 1.1 christos {
402 1.1 christos int i;
403 1.1 christos
404 1.1 christos if (name != NULL)
405 1.1 christos for (i = 0; authmethods[i] != NULL; i++)
406 1.1 christos if (authmethods[i]->enabled != NULL &&
407 1.1 christos *(authmethods[i]->enabled) != 0 &&
408 1.1 christos strcmp(name, authmethods[i]->name) == 0)
409 1.1 christos return authmethods[i];
410 1.1 christos debug2("Unrecognized authentication method name: %s",
411 1.1 christos name ? name : "NULL");
412 1.1 christos return NULL;
413 1.1 christos }
414 1.1 christos
415