]> andersk Git - gssapi-openssh.git/blob - openssh/auth2.c
http://www.sxw.org.uk/computing/patches/openssh-4.6p1-gsskex-20070312.patch committed...
[gssapi-openssh.git] / openssh / auth2.c
1 /* $OpenBSD: auth2.c,v 1.114 2007/03/01 10:28:02 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 "ssh2.h"
36 #include "packet.h"
37 #include "log.h"
38 #include "buffer.h"
39 #include "servconf.h"
40 #include "compat.h"
41 #include "key.h"
42 #include "hostfile.h"
43 #include "auth.h"
44 #include "dispatch.h"
45 #include "pathnames.h"
46 #include "buffer.h"
47
48 #ifdef GSSAPI
49 #include "ssh-gss.h"
50 #endif
51 #include "monitor_wrap.h"
52
53 /* import */
54 extern ServerOptions options;
55 extern u_char *session_id2;
56 extern u_int session_id2_len;
57 extern Buffer loginmsg;
58
59 /* methods */
60
61 extern Authmethod method_none;
62 extern Authmethod method_pubkey;
63 extern Authmethod method_passwd;
64 extern Authmethod method_kbdint;
65 extern Authmethod method_hostbased;
66 #ifdef GSSAPI
67 extern Authmethod method_gsskeyex;
68 extern Authmethod method_gssapi;
69 #endif
70
71 Authmethod *authmethods[] = {
72         &method_none,
73         &method_pubkey,
74 #ifdef GSSAPI
75         &method_gsskeyex,
76         &method_gssapi,
77 #endif
78         &method_passwd,
79         &method_kbdint,
80         &method_hostbased,
81         NULL
82 };
83
84 /* protocol */
85
86 static void input_service_request(int, u_int32_t, void *);
87 static void input_userauth_request(int, u_int32_t, void *);
88
89 /* helper */
90 static Authmethod *authmethod_lookup(const char *);
91 static char *authmethods_get(void);
92 int user_key_allowed(struct passwd *, Key *);
93
94 /*
95  * loop until authctxt->success == TRUE
96  */
97
98 void
99 do_authentication2(Authctxt *authctxt)
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
106 /*ARGSUSED*/
107 static void
108 input_service_request(int type, u_int32_t seq, void *ctxt)
109 {
110         Authctxt *authctxt = ctxt;
111         u_int len;
112         int acceptit = 0;
113         char *service = packet_get_string(&len);
114         packet_check_eom();
115
116         if (authctxt == NULL)
117                 fatal("input_service_request: no authctxt");
118
119         if (strcmp(service, "ssh-userauth") == 0) {
120                 if (!authctxt->success) {
121                         acceptit = 1;
122                         /* now we can handle user-auth requests */
123                         dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &input_userauth_request);
124                 }
125         }
126         /* XXX all other service requests are denied */
127
128         if (acceptit) {
129                 packet_start(SSH2_MSG_SERVICE_ACCEPT);
130                 packet_put_cstring(service);
131                 packet_send();
132                 packet_write_wait();
133         } else {
134                 debug("bad service request %s", service);
135                 packet_disconnect("bad service request %s", service);
136         }
137         xfree(service);
138 }
139
140 /*ARGSUSED*/
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
149         if (authctxt == NULL)
150                 fatal("input_userauth_request: no authctxt");
151
152         user = packet_get_string(NULL);
153         service = packet_get_string(NULL);
154         method = packet_get_string(NULL);
155         debug("userauth-request for user %s service %s method %s", user, service, method);
156         debug("attempt %d failures %d", authctxt->attempt, authctxt->failures);
157
158         if ((style = strchr(user, ':')) != NULL)
159                 *style++ = 0;
160
161         if (authctxt->attempt++ == 0) {
162                 /* setup auth context */
163                 authctxt->pw = PRIVSEP(getpwnamallow(user));
164                 authctxt->user = xstrdup(user);
165                 if (authctxt->pw && strcmp(service, "ssh-connection")==0) {
166                         authctxt->valid = 1;
167                         debug2("input_userauth_request: setting up authctxt for %s", user);
168                 } else {
169                         logit("input_userauth_request: invalid user %s", user);
170                         authctxt->pw = fakepw();
171 #ifdef SSH_AUDIT_EVENTS
172                         PRIVSEP(audit_event(SSH_INVALID_USER));
173 #endif
174                 }
175 #ifdef USE_PAM
176                 if (options.use_pam)
177                         PRIVSEP(start_pam(authctxt));
178 #endif
179                 setproctitle("%s%s", authctxt->valid ? user : "unknown",
180                     use_privsep ? " [net]" : "");
181                 authctxt->service = xstrdup(service);
182                 authctxt->style = style ? xstrdup(style) : NULL;
183                 if (use_privsep)
184                         mm_inform_authserv(service, style);
185         } else if (strcmp(user, authctxt->user) != 0 ||
186             strcmp(service, authctxt->service) != 0) {
187                 packet_disconnect("Change of username or service not allowed: "
188                     "(%s,%s) -> (%s,%s)",
189                     authctxt->user, authctxt->service, user, service);
190         }
191         /* reset state */
192         auth2_challenge_stop(authctxt);
193
194 #ifdef GSSAPI
195         dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
196         dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, NULL);
197 #endif
198
199         authctxt->postponed = 0;
200         authctxt->server_caused_failure = 0;
201
202         /* try to authenticate user */
203         m = authmethod_lookup(method);
204         if (m != NULL) {
205                 debug2("input_userauth_request: try method %s", method);
206                 authenticated = m->userauth(authctxt);
207         }
208         userauth_finish(authctxt, authenticated, method);
209
210         xfree(service);
211         xfree(user);
212         xfree(method);
213 }
214
215 void
216 userauth_finish(Authctxt *authctxt, int authenticated, char *method)
217 {
218         char *methods;
219
220         if (!authctxt->valid && authenticated)
221                 fatal("INTERNAL ERROR: authenticated invalid user %s",
222                     authctxt->user);
223
224         /* Special handling for root */
225         if (authenticated && authctxt->pw->pw_uid == 0 &&
226             !auth_root_allowed(method)) {
227                 authenticated = 0;
228 #ifdef SSH_AUDIT_EVENTS
229                 PRIVSEP(audit_event(SSH_LOGIN_ROOT_DENIED));
230 #endif
231         }
232
233 #ifdef USE_PAM
234         if (options.use_pam && authenticated) {
235                 if (!PRIVSEP(do_pam_account())) {
236                         /* if PAM returned a message, send it to the user */
237                         if (buffer_len(&loginmsg) > 0) {
238                                 buffer_append(&loginmsg, "\0", 1);
239                                 userauth_send_banner(buffer_ptr(&loginmsg));
240                                 packet_write_wait();
241                         }
242                         fatal("Access denied for user %s by PAM account "
243                             "configuration", authctxt->user);
244                 }
245         }
246 #endif
247
248 #ifdef _UNICOS
249         if (authenticated && cray_access_denied(authctxt->user)) {
250                 authenticated = 0;
251                 fatal("Access denied for user %s.",authctxt->user);
252         }
253 #endif /* _UNICOS */
254
255         /* Log before sending the reply */
256         auth_log(authctxt, authenticated, method, " ssh2");
257
258         if (authctxt->postponed)
259                 return;
260
261         /* XXX todo: check if multiple auth methods are needed */
262         if (authenticated == 1) {
263                 /* turn off userauth */
264                 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &dispatch_protocol_ignore);
265                 packet_start(SSH2_MSG_USERAUTH_SUCCESS);
266                 packet_send();
267                 packet_write_wait();
268                 /* now we can break out */
269                 authctxt->success = 1;
270         } else {
271                 /* Dont count server configuration issues against the client */
272                 if (!authctxt->server_caused_failure && 
273                     authctxt->failures++ > options.max_authtries) {
274 #ifdef SSH_AUDIT_EVENTS
275                         PRIVSEP(audit_event(SSH_LOGIN_EXCEED_MAXTRIES));
276 #endif
277                         packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
278                 }
279                 methods = authmethods_get();
280                 packet_start(SSH2_MSG_USERAUTH_FAILURE);
281                 packet_put_cstring(methods);
282                 packet_put_char(0);     /* XXX partial success, unused */
283                 packet_send();
284                 packet_write_wait();
285                 xfree(methods);
286         }
287 }
288
289 #define DELIM   ","
290
291 static char *
292 authmethods_get(void)
293 {
294         Buffer b;
295         char *list;
296         int i;
297
298         buffer_init(&b);
299         for (i = 0; authmethods[i] != NULL; i++) {
300                 if (strcmp(authmethods[i]->name, "none") == 0)
301                         continue;
302                 if (authmethods[i]->enabled != NULL &&
303                     *(authmethods[i]->enabled) != 0) {
304                         if (buffer_len(&b) > 0)
305                                 buffer_append(&b, ",", 1);
306                         buffer_append(&b, authmethods[i]->name,
307                             strlen(authmethods[i]->name));
308                 }
309         }
310         buffer_append(&b, "\0", 1);
311         list = xstrdup(buffer_ptr(&b));
312         buffer_free(&b);
313         return list;
314 }
315
316 static Authmethod *
317 authmethod_lookup(const char *name)
318 {
319         int i;
320
321         if (name != NULL)
322                 for (i = 0; authmethods[i] != NULL; i++)
323                         if (authmethods[i]->enabled != NULL &&
324                             *(authmethods[i]->enabled) != 0 &&
325                             strcmp(name, authmethods[i]->name) == 0)
326                                 return authmethods[i];
327         debug2("Unrecognized authentication method name: %s",
328             name ? name : "NULL");
329         return NULL;
330 }
This page took 0.243739 seconds and 5 git commands to generate.