]> andersk Git - gssapi-openssh.git/blob - openssh/auth2.c
merged OpenSSH 3.5p1 to trunk
[gssapi-openssh.git] / 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.95 2002/08/22 21:33:58 markus Exp $");
27
28 #include "ssh2.h"
29 #include "ssh1.h"
30 #include "xmalloc.h"
31 #include "packet.h"
32 #include "log.h"
33 #include "servconf.h"
34 #include "compat.h"
35 #include "auth.h"
36 #include "dispatch.h"
37 #include "pathnames.h"
38 #include "monitor_wrap.h"
39
40 #ifdef GSSAPI
41 #include "ssh-gss.h"
42 #endif
43
44 /* import */
45 extern ServerOptions options;
46 extern u_char *session_id2;
47 extern int session_id2_len;
48
49 Authctxt *x_authctxt = NULL;
50
51 /* methods */
52
53 extern Authmethod method_none;
54 #ifdef GSSAPI
55 extern Authmethod method_external;
56 extern Authmethod method_gssapi;
57 #endif
58 extern Authmethod method_pubkey;
59 extern Authmethod method_passwd;
60 extern Authmethod method_kbdint;
61 extern Authmethod method_hostbased;
62
63 Authmethod *authmethods[] = {
64         &method_none,
65 #ifdef GSSAPI
66         &method_external,
67         &method_gssapi,
68 #endif
69         &method_pubkey,
70         &method_passwd,
71         &method_kbdint,
72         &method_hostbased,
73         NULL
74 };
75
76 /* protocol */
77
78 static void input_service_request(int, u_int32_t, void *);
79 static void input_userauth_request(int, u_int32_t, void *);
80
81 /* helper */
82 static Authmethod *authmethod_lookup(const char *);
83 static char *authmethods_get(void);
84 int user_key_allowed(struct passwd *, Key *);
85 int hostbased_key_allowed(struct passwd *, const char *, char *, Key *);
86
87 /*
88  * loop until authctxt->success == TRUE
89  */
90
91 Authctxt *
92 do_authentication2(void)
93 {
94         Authctxt *authctxt = authctxt_new();
95
96         x_authctxt = authctxt;          /*XXX*/
97
98         /* challenge-response is implemented via keyboard interactive */
99         if (options.challenge_response_authentication)
100                 options.kbd_interactive_authentication = 1;
101         if (options.pam_authentication_via_kbd_int)
102                 options.kbd_interactive_authentication = 1;
103         if (use_privsep)
104                 options.pam_authentication_via_kbd_int = 0;
105
106         dispatch_init(&dispatch_protocol_error);
107         dispatch_set(SSH2_MSG_SERVICE_REQUEST, &input_service_request);
108         dispatch_run(DISPATCH_BLOCK, &authctxt->success, authctxt);
109
110         return (authctxt);
111 }
112
113 static void
114 input_service_request(int type, u_int32_t seq, void *ctxt)
115 {
116         Authctxt *authctxt = ctxt;
117         u_int len;
118         int acceptit = 0;
119         char *service = packet_get_string(&len);
120         packet_check_eom();
121
122         if (authctxt == NULL)
123                 fatal("input_service_request: no authctxt");
124
125         if (strcmp(service, "ssh-userauth") == 0) {
126                 if (!authctxt->success) {
127                         acceptit = 1;
128                         /* now we can handle user-auth requests */
129                         dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &input_userauth_request);
130                 }
131         }
132         /* XXX all other service requests are denied */
133
134         if (acceptit) {
135                 packet_start(SSH2_MSG_SERVICE_ACCEPT);
136                 packet_put_cstring(service);
137                 packet_send();
138                 packet_write_wait();
139         } else {
140                 debug("bad service request %s", service);
141                 packet_disconnect("bad service request %s", service);
142         }
143         xfree(service);
144 }
145
146 static void
147 input_userauth_request(int type, u_int32_t seq, void *ctxt)
148 {
149         Authctxt *authctxt = ctxt;
150         Authmethod *m = NULL;
151         char *user, *service, *method, *style = NULL;
152         int authenticated = 0;
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
161 #ifdef GSSAPI
162         if (strcmp(user, "") == 0) {
163             debug("received empty username for %s", method);
164             if (strcmp(method, "external-keyx") == 0) {
165                 char *lname = NULL;
166                 PRIVSEP(ssh_gssapi_localname(&lname));
167                 if (lname && lname[0] != '\0') {
168                     xfree(user);
169                     user = lname;
170                     debug("set username to %s from gssapi context", user);
171                 } else if (authctxt->valid) {
172                     debug("failed to set username from gssapi context");
173                 }
174             }
175         }
176 #endif
177
178         debug("userauth-request for user %s service %s method %s",
179               (user && user[0]) ? user : "<implicit>", service, method);
180         debug("attempt %d failures %d", authctxt->attempt, authctxt->failures);
181
182         if ((style = strchr(user, ':')) != NULL)
183                 *style++ = 0;
184
185         authctxt->attempt++;
186         if (!authctxt->user ||
187             strcmp(user, authctxt->user) != 0) {
188                 /* setup auth context */
189                 if (authctxt->user) {
190                     xfree(authctxt->user);
191                     authctxt->user = NULL;
192                 }
193                 if (authctxt->service) {
194                     xfree(authctxt->service);
195                     authctxt->service = NULL;
196                 }
197                 if (authctxt->style) {
198                     xfree(authctxt->style);
199                     authctxt->style = NULL;
200                 }
201 #ifdef GSSAPI
202                 /* We'll verify the username after we set it from the
203                    GSSAPI context. */
204                 if ((strcmp(user, "") == 0) &&
205                     ((strcmp(method, "gssapi") == 0) ||
206                      (strcmp(method, "external-keyx") == 0))) {
207                     authctxt->pw = NULL;
208                     authctxt->valid = 1;
209                 } else {
210 #endif
211                 authctxt->pw = PRIVSEP(getpwnamallow(user));
212                 if (authctxt->pw && strcmp(service, "ssh-connection")==0) {
213                         authctxt->valid = 1;
214                         debug2("input_userauth_request: setting up authctxt for %s", user);
215 #ifdef USE_PAM
216                         PRIVSEP(start_pam(authctxt->pw->pw_name));
217 #endif
218                 } else {
219                         authctxt->valid = 0;
220                         log("input_userauth_request: illegal user %s", user);
221 #ifdef USE_PAM
222                         PRIVSEP(start_pam("NOUSER"));
223 #endif
224                 }
225 #ifdef GSSAPI
226                 }
227 #endif
228                 setproctitle("%s%s", authctxt->pw ? user : "unknown",
229                     use_privsep ? " [net]" : "");
230                 authctxt->user = xstrdup(user);
231                 authctxt->service = xstrdup(service);
232                 authctxt->style = style ? xstrdup(style) : NULL;
233                 if (use_privsep && (authctxt->attempt == 1))
234                         mm_inform_authserv(service, style);
235         }
236         /* reset state */
237         auth2_challenge_stop(authctxt);
238
239 #ifdef GSSAPI
240         dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
241         dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, NULL);
242 #endif
243
244         authctxt->postponed = 0;
245
246         /* try to authenticate user */
247         m = authmethod_lookup(method);
248         if (m != NULL) {
249                 debug2("input_userauth_request: try method %s", method);
250                 authenticated = m->userauth(authctxt);
251         }
252
253         userauth_finish(authctxt, authenticated, method);
254
255         xfree(service);
256         xfree(user);
257         xfree(method);
258 }
259
260 void
261 userauth_finish(Authctxt *authctxt, int authenticated, char *method)
262 {
263         char *methods;
264
265         if (!authctxt->valid && authenticated)
266                 fatal("INTERNAL ERROR: authenticated invalid user %s",
267                     authctxt->user);
268
269         /* Special handling for root */
270         if (!use_privsep &&
271             authenticated && authctxt->pw->pw_uid == 0 &&
272             !auth_root_allowed(method))
273                 authenticated = 0;
274
275 #ifdef USE_PAM
276         if (!use_privsep && authenticated && authctxt->user && 
277             !do_pam_account(authctxt->user, NULL))
278                 authenticated = 0;
279 #endif /* USE_PAM */
280
281 #ifdef _UNICOS
282         if (authenticated && cray_access_denied(authctxt->user)) {
283                 authenticated = 0;
284                 fatal("Access denied for user %s.",authctxt->user);
285         }
286 #endif /* _UNICOS */
287
288         /* Log before sending the reply */
289         if (!compat20)
290         auth_log(authctxt, authenticated, method, " ssh1");
291         else
292         auth_log(authctxt, authenticated, method, " ssh2");
293
294         if (authctxt->postponed)
295                 return;
296
297         /* XXX todo: check if multiple auth methods are needed */
298         if (authenticated == 1) {
299                 /* turn off userauth */
300                 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &dispatch_protocol_ignore);
301                 if (compat20) {
302                 packet_start(SSH2_MSG_USERAUTH_SUCCESS);
303                 packet_send();
304                 packet_write_wait();
305                 }
306                 /* now we can break out */
307                 authctxt->success = 1;
308         } else {
309                 if (authctxt->failures++ > AUTH_FAIL_MAX) {
310                         packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
311                 }
312                 if (!compat20) {
313                 /*
314                  * Break out of the dispatch loop now and go back to
315                  * SSH1 code.  We need to set the 'success' flag to
316                  * break out of the loop.  Set the 'postponed' flag to
317                  * tell the SSH1 code that authentication failed.  The
318                  * SSH1 code will handle sending SSH_SMSG_FAILURE.
319                 */
320                 authctxt->success = authctxt->postponed = 1;
321                 } else {
322 #ifdef _UNICOS
323                 if (strcmp(method, "password") == 0)
324                         cray_login_failure(authctxt->user, IA_UDBERR);
325 #endif /* _UNICOS */
326                 methods = authmethods_get();
327                 packet_start(SSH2_MSG_USERAUTH_FAILURE);
328                 packet_put_cstring(methods);
329                 packet_put_char(0);     /* XXX partial success, unused */
330                 packet_send();
331                 packet_write_wait();
332                 xfree(methods);
333                 }
334         }
335 }
336
337 /* get current user */
338
339 struct passwd*
340 auth_get_user(void)
341 {
342         return (x_authctxt != NULL && x_authctxt->valid) ? x_authctxt->pw : NULL;
343 }
344
345 #define DELIM   ","
346
347 static char *
348 authmethods_get(void)
349 {
350         Buffer b;
351         char *list;
352         int i;
353
354         buffer_init(&b);
355         for (i = 0; authmethods[i] != NULL; i++) {
356                 if (strcmp(authmethods[i]->name, "none") == 0)
357                         continue;
358                 if (authmethods[i]->enabled != NULL &&
359                     *(authmethods[i]->enabled) != 0) {
360                         if (buffer_len(&b) > 0)
361                                 buffer_append(&b, ",", 1);
362                         buffer_append(&b, authmethods[i]->name,
363                             strlen(authmethods[i]->name));
364                 }
365         }
366         buffer_append(&b, "\0", 1);
367         list = xstrdup(buffer_ptr(&b));
368         buffer_free(&b);
369         return list;
370 }
371
372 static Authmethod *
373 authmethod_lookup(const char *name)
374 {
375         int i;
376
377         if (name != NULL)
378                 for (i = 0; authmethods[i] != NULL; i++)
379                         if (authmethods[i]->enabled != NULL &&
380                             *(authmethods[i]->enabled) != 0 &&
381                             strcmp(name, authmethods[i]->name) == 0)
382                                 return authmethods[i];
383         debug2("Unrecognized authentication method name: %s",
384             name ? name : "NULL");
385         return NULL;
386 }
This page took 0.1542 seconds and 5 git commands to generate.