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