If the current thread is not a network thread then rejecting the packet back
[dragonfly.git] / crypto / openssh-5 / auth2.c
1 /* $OpenBSD: auth2.c,v 1.116 2007/09/29 00:25:51 dtucker 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
30 #include <pwd.h>
31 #include <stdarg.h>
32 #include <string.h>
33
34 #include "xmalloc.h"
35 #include "canohost.h"
36 #include "ssh2.h"
37 #include "packet.h"
38 #include "log.h"
39 #include "buffer.h"
40 #include "servconf.h"
41 #include "compat.h"
42 #include "key.h"
43 #include "hostfile.h"
44 #include "auth.h"
45 #include "dispatch.h"
46 #include "pathnames.h"
47 #include "buffer.h"
48
49 #ifdef GSSAPI
50 #include "ssh-gss.h"
51 #endif
52 #include "monitor_wrap.h"
53
54 /* import */
55 extern ServerOptions options;
56 extern u_char *session_id2;
57 extern u_int session_id2_len;
58 extern Buffer loginmsg;
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 /*
93  * loop until authctxt->success == TRUE
94  */
95
96 void
97 do_authentication2(Authctxt *authctxt)
98 {
99         dispatch_init(&dispatch_protocol_error);
100         dispatch_set(SSH2_MSG_SERVICE_REQUEST, &input_service_request);
101         dispatch_run(DISPATCH_BLOCK, &authctxt->success, authctxt);
102 }
103
104 /*ARGSUSED*/
105 static void
106 input_service_request(int type, u_int32_t seq, void *ctxt)
107 {
108         Authctxt *authctxt = ctxt;
109         u_int len;
110         int acceptit = 0;
111         char *service = packet_get_string(&len);
112         packet_check_eom();
113
114         if (authctxt == NULL)
115                 fatal("input_service_request: no authctxt");
116
117         if (strcmp(service, "ssh-userauth") == 0) {
118                 if (!authctxt->success) {
119                         acceptit = 1;
120                         /* now we can handle user-auth requests */
121                         dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &input_userauth_request);
122                 }
123         }
124         /* XXX all other service requests are denied */
125
126         if (acceptit) {
127                 packet_start(SSH2_MSG_SERVICE_ACCEPT);
128                 packet_put_cstring(service);
129                 packet_send();
130                 packet_write_wait();
131         } else {
132                 debug("bad service request %s", service);
133                 packet_disconnect("bad service request %s", service);
134         }
135         xfree(service);
136 }
137
138 /*ARGSUSED*/
139 static void
140 input_userauth_request(int type, u_int32_t seq, void *ctxt)
141 {
142         Authctxt *authctxt = ctxt;
143         Authmethod *m = NULL;
144         char *user, *service, *method, *style = NULL;
145         int authenticated = 0;
146 #ifdef HAVE_LOGIN_CAP
147         login_cap_t *lc;
148         const char *from_host, *from_ip;
149
150         from_host = get_canonical_hostname(options.use_dns);
151         from_ip = get_remote_ipaddr();
152 #endif
153
154         if (authctxt == NULL)
155                 fatal("input_userauth_request: no authctxt");
156
157         user = packet_get_string(NULL);
158         service = packet_get_string(NULL);
159         method = packet_get_string(NULL);
160         debug("userauth-request for user %s service %s method %s", user, service, method);
161         debug("attempt %d failures %d", authctxt->attempt, authctxt->failures);
162
163         if ((style = strchr(user, ':')) != NULL)
164                 *style++ = 0;
165
166         if (authctxt->attempt++ == 0) {
167                 /* setup auth context */
168                 authctxt->pw = PRIVSEP(getpwnamallow(user));
169                 authctxt->user = xstrdup(user);
170                 if (authctxt->pw && strcmp(service, "ssh-connection")==0) {
171                         authctxt->valid = 1;
172                         debug2("input_userauth_request: setting up authctxt for %s", user);
173                 } else {
174                         logit("input_userauth_request: invalid user %s", user);
175                         authctxt->pw = fakepw();
176 #ifdef SSH_AUDIT_EVENTS
177                         PRIVSEP(audit_event(SSH_INVALID_USER));
178 #endif
179                 }
180 #ifdef USE_PAM
181                 if (options.use_pam)
182                         PRIVSEP(start_pam(authctxt));
183 #endif
184                 setproctitle("%s%s", authctxt->valid ? user : "unknown",
185                     use_privsep ? " [net]" : "");
186                 authctxt->service = xstrdup(service);
187                 authctxt->style = style ? xstrdup(style) : NULL;
188                 if (use_privsep)
189                         mm_inform_authserv(service, style);
190         } else if (strcmp(user, authctxt->user) != 0 ||
191             strcmp(service, authctxt->service) != 0) {
192                 packet_disconnect("Change of username or service not allowed: "
193                     "(%s,%s) -> (%s,%s)",
194                     authctxt->user, authctxt->service, user, service);
195         }
196
197 #ifdef HAVE_LOGIN_CAP
198         if (authctxt->pw != NULL) {
199                 lc = login_getpwclass(authctxt->pw);
200                 if (lc == NULL)
201                         lc = login_getclassbyname(NULL, authctxt->pw);
202                 if (!auth_hostok(lc, from_host, from_ip)) {
203                         logit("Denied connection for %.200s from %.200s [%.200s].",
204                             authctxt->pw->pw_name, from_host, from_ip);
205                         packet_disconnect("Sorry, you are not allowed to connect.");
206                 }
207                 if (!auth_timeok(lc, time(NULL))) {
208                         logit("LOGIN %.200s REFUSED (TIME) FROM %.200s",
209                             authctxt->pw->pw_name, from_host);
210                         packet_disconnect("Logins not available right now.");
211                 }
212                 login_close(lc);
213                 lc = NULL;
214         }
215 #endif  /* HAVE_LOGIN_CAP */
216
217         /* reset state */
218         auth2_challenge_stop(authctxt);
219
220 #ifdef GSSAPI
221         dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
222         dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, NULL);
223 #endif
224
225         authctxt->postponed = 0;
226
227         /* try to authenticate user */
228         m = authmethod_lookup(method);
229         if (m != NULL) {
230                 debug2("input_userauth_request: try method %s", method);
231                 authenticated = m->userauth(authctxt);
232         }
233         userauth_finish(authctxt, authenticated, method);
234
235         xfree(service);
236         xfree(user);
237         xfree(method);
238 }
239
240 void
241 userauth_finish(Authctxt *authctxt, int authenticated, char *method)
242 {
243         char *methods;
244
245         if (!authctxt->valid && authenticated)
246                 fatal("INTERNAL ERROR: authenticated invalid user %s",
247                     authctxt->user);
248
249         /* Special handling for root */
250         if (authenticated && authctxt->pw->pw_uid == 0 &&
251             !auth_root_allowed(method)) {
252                 authenticated = 0;
253 #ifdef SSH_AUDIT_EVENTS
254                 PRIVSEP(audit_event(SSH_LOGIN_ROOT_DENIED));
255 #endif
256         }
257
258 #ifdef USE_PAM
259         if (options.use_pam && authenticated) {
260                 if (!PRIVSEP(do_pam_account())) {
261                         /* if PAM returned a message, send it to the user */
262                         if (buffer_len(&loginmsg) > 0) {
263                                 buffer_append(&loginmsg, "\0", 1);
264                                 userauth_send_banner(buffer_ptr(&loginmsg));
265                                 packet_write_wait();
266                         }
267                         fatal("Access denied for user %s by PAM account "
268                             "configuration", authctxt->user);
269                 }
270         }
271 #endif
272
273 #ifdef _UNICOS
274         if (authenticated && cray_access_denied(authctxt->user)) {
275                 authenticated = 0;
276                 fatal("Access denied for user %s.",authctxt->user);
277         }
278 #endif /* _UNICOS */
279
280         /* Log before sending the reply */
281         auth_log(authctxt, authenticated, method, " ssh2");
282
283         if (authctxt->postponed)
284                 return;
285
286         /* XXX todo: check if multiple auth methods are needed */
287         if (authenticated == 1) {
288                 /* turn off userauth */
289                 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &dispatch_protocol_ignore);
290                 packet_start(SSH2_MSG_USERAUTH_SUCCESS);
291                 packet_send();
292                 packet_write_wait();
293                 /* now we can break out */
294                 authctxt->success = 1;
295         } else {
296                 if (authctxt->failures++ > options.max_authtries) {
297 #ifdef SSH_AUDIT_EVENTS
298                         PRIVSEP(audit_event(SSH_LOGIN_EXCEED_MAXTRIES));
299 #endif
300                         packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
301                 }
302                 methods = authmethods_get();
303                 packet_start(SSH2_MSG_USERAUTH_FAILURE);
304                 packet_put_cstring(methods);
305                 packet_put_char(0);     /* XXX partial success, unused */
306                 packet_send();
307                 packet_write_wait();
308                 xfree(methods);
309         }
310 }
311
312 static char *
313 authmethods_get(void)
314 {
315         Buffer b;
316         char *list;
317         int i;
318
319         buffer_init(&b);
320         for (i = 0; authmethods[i] != NULL; i++) {
321                 if (strcmp(authmethods[i]->name, "none") == 0)
322                         continue;
323                 if (authmethods[i]->enabled != NULL &&
324                     *(authmethods[i]->enabled) != 0) {
325                         if (buffer_len(&b) > 0)
326                                 buffer_append(&b, ",", 1);
327                         buffer_append(&b, authmethods[i]->name,
328                             strlen(authmethods[i]->name));
329                 }
330         }
331         buffer_append(&b, "\0", 1);
332         list = xstrdup(buffer_ptr(&b));
333         buffer_free(&b);
334         return list;
335 }
336
337 static Authmethod *
338 authmethod_lookup(const char *name)
339 {
340         int i;
341
342         if (name != NULL)
343                 for (i = 0; authmethods[i] != NULL; i++)
344                         if (authmethods[i]->enabled != NULL &&
345                             *(authmethods[i]->enabled) != 0 &&
346                             strcmp(name, authmethods[i]->name) == 0)
347                                 return authmethods[i];
348         debug2("Unrecognized authentication method name: %s",
349             name ? name : "NULL");
350         return NULL;
351 }