update version
[openssh.git] / auth2.c
1 /* $OpenBSD: auth2.c,v 1.123 2011/03/10 02:52:57 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 #include "includes.h"
27
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <sys/uio.h>
31
32 #include <fcntl.h>
33 #include <pwd.h>
34 #include <stdarg.h>
35 #include <string.h>
36 #include <unistd.h>
37
38 #include "atomicio.h"
39 #include "xmalloc.h"
40 #include "ssh2.h"
41 #include "packet.h"
42 #include "log.h"
43 #include "buffer.h"
44 #include "servconf.h"
45 #include "compat.h"
46 #include "key.h"
47 #include "hostfile.h"
48 #include "auth.h"
49 #include "dispatch.h"
50 #include "pathnames.h"
51 #include "buffer.h"
52
53 #ifdef GSSAPI
54 #include "ssh-gss.h"
55 #endif
56 #include "monitor_wrap.h"
57
58 /* import */
59 extern ServerOptions options;
60 extern u_char *session_id2;
61 extern u_int session_id2_len;
62 extern Buffer loginmsg;
63
64 /* methods */
65
66 extern Authmethod method_none;
67 extern Authmethod method_pubkey;
68 extern Authmethod method_passwd;
69 extern Authmethod method_kbdint;
70 extern Authmethod method_hostbased;
71 #ifdef GSSAPI
72 extern Authmethod method_gsskeyex;
73 extern Authmethod method_gssapi;
74 #endif
75 #ifdef JPAKE
76 extern Authmethod method_jpake;
77 #endif
78
79 Authmethod *authmethods[] = {
80         &method_none,
81         &method_pubkey,
82 #ifdef GSSAPI
83         &method_gsskeyex,
84         &method_gssapi,
85 #endif
86 #ifdef JPAKE
87         &method_jpake,
88 #endif
89         &method_passwd,
90         &method_kbdint,
91         &method_hostbased,
92         NULL
93 };
94
95 /* protocol */
96
97 static void input_service_request(int, u_int32_t, void *);
98 static void input_userauth_request(int, u_int32_t, void *);
99
100 /* helper */
101 static Authmethod *authmethod_lookup(const char *);
102 static char *authmethods_get(void);
103
104 char *
105 auth2_read_banner(void)
106 {
107         struct stat st;
108         char *banner = NULL;
109         size_t len, n;
110         int fd;
111
112         if ((fd = open(options.banner, O_RDONLY)) == -1)
113                 return (NULL);
114         if (fstat(fd, &st) == -1) {
115                 close(fd);
116                 return (NULL);
117         }
118         if (st.st_size > 1*1024*1024) {
119                 close(fd);
120                 return (NULL);
121         }
122
123         len = (size_t)st.st_size;               /* truncate */
124         banner = xmalloc(len + 1);
125         n = atomicio(read, fd, banner, len);
126         close(fd);
127
128         if (n != len) {
129                 xfree(banner);
130                 return (NULL);
131         }
132         banner[n] = '\0';
133
134         return (banner);
135 }
136
137 void
138 userauth_send_banner(const char *msg)
139 {
140         if (datafellows & SSH_BUG_BANNER)
141                 return;
142
143         packet_start(SSH2_MSG_USERAUTH_BANNER);
144         packet_put_cstring(msg);
145         packet_put_cstring("");         /* language, unused */
146         packet_send();
147         debug("%s: sent", __func__);
148 }
149
150 static void
151 userauth_banner(void)
152 {
153         char *banner = NULL;
154
155         if (options.banner == NULL ||
156             strcasecmp(options.banner, "none") == 0 ||
157             (datafellows & SSH_BUG_BANNER) != 0)
158                 return;
159
160         if ((banner = PRIVSEP(auth2_read_banner())) == NULL)
161                 goto done;
162         userauth_send_banner(banner);
163
164 done:
165         if (banner)
166                 xfree(banner);
167 }
168
169 /*
170  * loop until authctxt->success == TRUE
171  */
172 void
173 do_authentication2(Authctxt *authctxt)
174 {
175         dispatch_init(&dispatch_protocol_error);
176         dispatch_set(SSH2_MSG_SERVICE_REQUEST, &input_service_request);
177         dispatch_run(DISPATCH_BLOCK, &authctxt->success, authctxt);
178 }
179
180 /*ARGSUSED*/
181 static void
182 input_service_request(int type, u_int32_t seq, void *ctxt)
183 {
184         Authctxt *authctxt = ctxt;
185         u_int len;
186         int acceptit = 0;
187         char *service = packet_get_cstring(&len);
188         packet_check_eom();
189
190         if (authctxt == NULL)
191                 fatal("input_service_request: no authctxt");
192
193         if (strcmp(service, "ssh-userauth") == 0) {
194                 if (!authctxt->success) {
195                         acceptit = 1;
196                         /* now we can handle user-auth requests */
197                         dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &input_userauth_request);
198                 }
199         }
200         /* XXX all other service requests are denied */
201
202         if (acceptit) {
203                 packet_start(SSH2_MSG_SERVICE_ACCEPT);
204                 packet_put_cstring(service);
205                 packet_send();
206                 packet_write_wait();
207         } else {
208                 debug("bad service request %s", service);
209                 packet_disconnect("bad service request %s", service);
210         }
211         xfree(service);
212 }
213
214 /*ARGSUSED*/
215 static void
216 input_userauth_request(int type, u_int32_t seq, void *ctxt)
217 {
218         Authctxt *authctxt = ctxt;
219         Authmethod *m = NULL;
220         char *user, *service, *method, *style = NULL, *role = NULL;
221         int authenticated = 0;
222
223         if (authctxt == NULL)
224                 fatal("input_userauth_request: no authctxt");
225
226         user = packet_get_string(NULL);
227         service = packet_get_string(NULL);
228         method = packet_get_string(NULL);
229
230 #ifdef GSSAPI
231         if (user[0] == '\0') {
232             debug("received empty username for %s", method);
233             if (strcmp(method, "gssapi-keyex") == 0) {
234                 char *lname = NULL;
235                 PRIVSEP(ssh_gssapi_localname(&lname));
236                 if (lname && lname[0] != '\0') {
237                     xfree(user);
238                     user = lname;
239                     debug("set username to %s from gssapi context", user);
240                 } else {
241                     debug("failed to set username from gssapi context");
242                     packet_send_debug("failed to set username from gssapi context");
243                 }
244             }
245         }
246 #endif
247
248         debug("userauth-request for user %s service %s method %s",
249               user[0] ? user : "<implicit>", service, method);
250         debug("attempt %d failures %d", authctxt->attempt, authctxt->failures);
251
252         if ((role = strchr(user, '/')) != NULL)
253                 *role++ = 0;
254
255         if ((style = strchr(user, ':')) != NULL)
256                 *style++ = 0;
257         else if (role && (style = strchr(role, ':')) != NULL)
258                 *style++ = '\0';
259
260         /* If first time or username changed or empty username,
261            setup/reset authentication context. */
262         if ((authctxt->attempt++ == 0) ||
263             (strcmp(user, authctxt->user) != 0) ||
264             (strcmp(user, "") == 0)) {
265                 if (authctxt->user) {
266                     xfree(authctxt->user);
267                     authctxt->user = NULL;
268                 }
269                 authctxt->valid = 0;
270         authctxt->user = xstrdup(user);
271         if (strcmp(service, "ssh-connection") != 0) {
272             packet_disconnect("Unsupported service %s", service);
273         }
274 #ifdef GSSAPI
275                 /* If we're going to set the username based on the
276                    GSSAPI context later, then wait until then to
277                    verify it. Just put in placeholders for now. */
278                 if ((strcmp(user, "") == 0) &&
279                     ((strcmp(method, "gssapi") == 0) ||
280                      (strcmp(method, "gssapi-with-mic") == 0))) {
281                         authctxt->pw = fakepw();
282                 } else {
283 #endif
284                 authctxt->pw = PRIVSEP(getpwnamallow(user));
285                 if (authctxt->pw) {
286                         authctxt->valid = 1;
287                         debug2("input_userauth_request: setting up authctxt for %s", user);
288                 } else {
289                         logit("input_userauth_request: invalid user %s", user);
290                         authctxt->pw = fakepw();
291 #ifdef SSH_AUDIT_EVENTS
292                         PRIVSEP(audit_event(SSH_INVALID_USER));
293 #endif
294                 }
295 #ifdef GSSAPI
296                 } /* endif for setting username based on GSSAPI context */
297 #endif
298 #ifdef USE_PAM
299                 if (options.use_pam)
300                         PRIVSEP(start_pam(authctxt));
301 #endif
302                 setproctitle("%s%s", authctxt->valid ? user : "unknown",
303                     use_privsep ? " [net]" : "");
304                 if (authctxt->attempt == 1) {
305             authctxt->service = xstrdup(service);
306             authctxt->style = style ? xstrdup(style) : NULL;
307             authctxt->role = role ? xstrdup(role) : NULL;
308             if (use_privsep)
309               mm_inform_authserv(service, style, role);
310             userauth_banner();
311                 }
312         }
313         if (strcmp(service, authctxt->service) != 0) {
314                 packet_disconnect("Change of service not allowed: "
315                     "(%s,%s) -> (%s,%s)",
316                     authctxt->user, authctxt->service, user, service);
317         }
318         /* reset state */
319         auth2_challenge_stop(authctxt);
320 #ifdef JPAKE
321         auth2_jpake_stop(authctxt);
322 #endif
323
324 #ifdef GSSAPI
325         /* XXX move to auth2_gssapi_stop() */
326         dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
327         dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, NULL);
328 #endif
329
330         authctxt->postponed = 0;
331         authctxt->server_caused_failure = 0;
332
333         /* try to authenticate user */
334         m = authmethod_lookup(method);
335         if (m != NULL && authctxt->failures < options.max_authtries) {
336                 debug2("input_userauth_request: try method %s", method);
337                 authenticated = m->userauth(authctxt);
338         }
339         userauth_finish(authctxt, authenticated, method);
340
341         xfree(service);
342         xfree(user);
343         xfree(method);
344 }
345
346 void
347 userauth_finish(Authctxt *authctxt, int authenticated, char *method)
348 {
349         char *methods;
350
351         if (!authctxt->valid && authenticated)
352                 fatal("INTERNAL ERROR: authenticated invalid user %s",
353                     authctxt->user);
354
355         /* Special handling for root */
356         if (authenticated && authctxt->pw->pw_uid == 0 &&
357             !auth_root_allowed(method)) {
358                 authenticated = 0;
359 #ifdef SSH_AUDIT_EVENTS
360                 PRIVSEP(audit_event(SSH_LOGIN_ROOT_DENIED));
361 #endif
362         }
363
364 #ifdef USE_PAM
365         if (options.use_pam && authenticated) {
366                 if (!PRIVSEP(do_pam_account())) {
367                         /* if PAM returned a message, send it to the user */
368                         if (buffer_len(&loginmsg) > 0) {
369                                 buffer_append(&loginmsg, "\0", 1);
370                                 userauth_send_banner(buffer_ptr(&loginmsg));
371                                 packet_write_wait();
372                         }
373                         fatal("Access denied for user %s by PAM account "
374                             "configuration", authctxt->user);
375                 }
376         }
377 #endif
378
379 #ifdef _UNICOS
380         if (authenticated && cray_access_denied(authctxt->user)) {
381                 authenticated = 0;
382                 fatal("Access denied for user %s.",authctxt->user);
383         }
384 #endif /* _UNICOS */
385
386         /* Log before sending the reply */
387         auth_log(authctxt, authenticated, method, " ssh2");
388
389         if (authctxt->postponed)
390                 return;
391
392         /* XXX todo: check if multiple auth methods are needed */
393         if (authenticated == 1) {
394                 /* turn off userauth */
395                 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &dispatch_protocol_ignore);
396                 packet_start(SSH2_MSG_USERAUTH_SUCCESS);
397                 packet_send();
398                 packet_write_wait();
399                 /* now we can break out */
400                 authctxt->success = 1;
401         } else {
402
403                 /* Allow initial try of "none" auth without failure penalty */
404                 if (!authctxt->server_caused_failure &&
405                     (authctxt->attempt > 1 || strcmp(method, "none") != 0))
406                         authctxt->failures++;
407                 if (authctxt->failures >= options.max_authtries) {
408 #ifdef SSH_AUDIT_EVENTS
409                         PRIVSEP(audit_event(SSH_LOGIN_EXCEED_MAXTRIES));
410 #endif
411                         packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
412                 }
413                 methods = authmethods_get();
414                 packet_start(SSH2_MSG_USERAUTH_FAILURE);
415                 packet_put_cstring(methods);
416                 packet_put_char(0);     /* XXX partial success, unused */
417                 packet_send();
418                 packet_write_wait();
419                 xfree(methods);
420         }
421 }
422
423 static char *
424 authmethods_get(void)
425 {
426         Buffer b;
427         char *list;
428         int i;
429
430         buffer_init(&b);
431         for (i = 0; authmethods[i] != NULL; i++) {
432                 if (strcmp(authmethods[i]->name, "none") == 0)
433                         continue;
434                 if (authmethods[i]->enabled != NULL &&
435                     *(authmethods[i]->enabled) != 0) {
436                         if (buffer_len(&b) > 0)
437                                 buffer_append(&b, ",", 1);
438                         buffer_append(&b, authmethods[i]->name,
439                             strlen(authmethods[i]->name));
440                 }
441         }
442         buffer_append(&b, "\0", 1);
443         list = xstrdup(buffer_ptr(&b));
444         buffer_free(&b);
445         return list;
446 }
447
448 static Authmethod *
449 authmethod_lookup(const char *name)
450 {
451         int i;
452
453         if (name != NULL)
454                 for (i = 0; authmethods[i] != NULL; i++)
455                         if (authmethods[i]->enabled != NULL &&
456                             *(authmethods[i]->enabled) != 0 &&
457                             strcmp(name, authmethods[i]->name) == 0)
458                                 return authmethods[i];
459         debug2("Unrecognized authentication method name: %s",
460             name ? name : "NULL");
461         return NULL;
462 }
463