]> andersk Git - gssapi-openssh.git/blob - openssh/auth2.c
76724f4406babbfb66593f48c241c0194726461d
[gssapi-openssh.git] / openssh / auth2.c
1 /* $OpenBSD: auth2.c,v 1.115 2007/04/14 22:01:58 stevesk 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_external;
68 extern Authmethod method_gsskeyex;
69 extern Authmethod method_gssapi;
70 extern Authmethod method_gssapi_compat;
71 #endif
72
73 Authmethod *authmethods[] = {
74         &method_none,
75         &method_pubkey,
76 #ifdef GSSAPI
77         &method_gsskeyex,
78         &method_external,
79         &method_gssapi,
80         &method_gssapi_compat,
81 #endif
82         &method_passwd,
83         &method_kbdint,
84         &method_hostbased,
85         NULL
86 };
87
88 /* protocol */
89
90 static void input_service_request(int, u_int32_t, void *);
91 static void input_userauth_request(int, u_int32_t, void *);
92
93 /* helper */
94 static Authmethod *authmethod_lookup(const char *);
95 static char *authmethods_get(void);
96 int user_key_allowed(struct passwd *, Key *);
97
98 /*
99  * loop until authctxt->success == TRUE
100  */
101
102 void
103 do_authentication2(Authctxt *authctxt)
104 {
105         dispatch_init(&dispatch_protocol_error);
106         dispatch_set(SSH2_MSG_SERVICE_REQUEST, &input_service_request);
107         dispatch_run(DISPATCH_BLOCK, &authctxt->success, authctxt);
108 }
109
110 /*ARGSUSED*/
111 static void
112 input_service_request(int type, u_int32_t seq, void *ctxt)
113 {
114         Authctxt *authctxt = ctxt;
115         u_int len;
116         int acceptit = 0;
117         char *service = packet_get_string(&len);
118         packet_check_eom();
119
120         if (authctxt == NULL)
121                 fatal("input_service_request: no authctxt");
122
123         if (strcmp(service, "ssh-userauth") == 0) {
124                 if (!authctxt->success) {
125                         acceptit = 1;
126                         /* now we can handle user-auth requests */
127                         dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &input_userauth_request);
128                 }
129         }
130         /* XXX all other service requests are denied */
131
132         if (acceptit) {
133                 packet_start(SSH2_MSG_SERVICE_ACCEPT);
134                 packet_put_cstring(service);
135                 packet_send();
136                 packet_write_wait();
137         } else {
138                 debug("bad service request %s", service);
139                 packet_disconnect("bad service request %s", service);
140         }
141         xfree(service);
142 }
143
144 /*ARGSUSED*/
145 static void
146 input_userauth_request(int type, u_int32_t seq, void *ctxt)
147 {
148         Authctxt *authctxt = ctxt;
149         Authmethod *m = NULL;
150         char *user, *service, *method, *style = NULL;
151         int authenticated = 0;
152
153         if (authctxt == NULL)
154                 fatal("input_userauth_request: no authctxt");
155
156         user = packet_get_string(NULL);
157         service = packet_get_string(NULL);
158         method = packet_get_string(NULL);
159
160 #ifdef GSSAPI
161         if (user[0] == '\0') {
162             debug("received empty username for %s", method);
163             if (strcmp(method, "external-keyx") == 0 ||
164                 strcmp(method, "gssapi-keyex") == 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 {
172                     debug("failed to set username from gssapi context");
173                     packet_send_debug("failed to set username from gssapi context");
174                 }
175             }
176         }
177 #endif
178
179         debug("userauth-request for user %s service %s method %s",
180               user[0] ? user : "<implicit>", service, method);
181         debug("attempt %d failures %d", authctxt->attempt, authctxt->failures);
182
183         if ((style = strchr(user, ':')) != NULL)
184                 *style++ = 0;
185
186         /* If first time or username changed or empty username,
187            setup/reset authentication context. */
188         if ((authctxt->attempt++ == 0) ||
189             (strcmp(user, authctxt->user) != 0) ||
190             (strcmp(user, "") == 0)) {
191                 if (authctxt->user) {
192                     xfree(authctxt->user);
193                     authctxt->user = NULL;
194                 }
195                 authctxt->valid = 0;
196         authctxt->user = xstrdup(user);
197         if (strcmp(service, "ssh-connection") != 0) {
198             packet_disconnect("Unsupported service %s", service);
199         }
200 #ifdef GSSAPI
201                 /* If we're going to set the username based on the
202                    GSSAPI context later, then wait until then to
203                    verify it. Just put in placeholders for now. */
204                 if ((strcmp(user, "") == 0) &&
205                     ((strcmp(method, "gssapi") == 0) ||
206                      (strcmp(method, "gssapi-with-mic") == 0))) {
207                         authctxt->pw = fakepw();
208                 } else {
209 #endif
210                 authctxt->pw = PRIVSEP(getpwnamallow(user));
211                 if (authctxt->pw) {
212                         authctxt->valid = 1;
213                         debug2("input_userauth_request: setting up authctxt for %s", user);
214                 } else {
215                         logit("input_userauth_request: invalid user %s", user);
216                         authctxt->pw = fakepw();
217 #ifdef SSH_AUDIT_EVENTS
218                         PRIVSEP(audit_event(SSH_INVALID_USER));
219 #endif
220                 }
221 #ifdef GSSAPI
222                 } /* endif for setting username based on GSSAPI context */
223 #endif
224 #ifdef USE_PAM
225                 if (options.use_pam)
226                         PRIVSEP(start_pam(authctxt));
227 #endif
228                 setproctitle("%s%s", authctxt->valid ? user : "unknown",
229                     use_privsep ? " [net]" : "");
230                 if (authctxt->attempt == 1) {
231             authctxt->service = xstrdup(service);
232             authctxt->style = style ? xstrdup(style) : NULL;
233             if (use_privsep)
234                 mm_inform_authserv(service, style);
235                 }
236         }
237         if (strcmp(service, authctxt->service) != 0) {
238                 packet_disconnect("Change of service not allowed: "
239                     "(%s,%s) -> (%s,%s)",
240                     authctxt->user, authctxt->service, user, service);
241         }
242         /* reset state */
243         auth2_challenge_stop(authctxt);
244
245 #ifdef GSSAPI
246         dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
247         dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, NULL);
248 #endif
249
250         authctxt->postponed = 0;
251         authctxt->server_caused_failure = 0;
252
253         /* try to authenticate user */
254         m = authmethod_lookup(method);
255         if (m != NULL) {
256                 debug2("input_userauth_request: try method %s", method);
257                 authenticated = m->userauth(authctxt);
258         }
259         userauth_finish(authctxt, authenticated, method);
260
261         xfree(service);
262         xfree(user);
263         xfree(method);
264 }
265
266 void
267 userauth_finish(Authctxt *authctxt, int authenticated, char *method)
268 {
269         char *methods;
270
271         if (!authctxt->valid && authenticated)
272                 fatal("INTERNAL ERROR: authenticated invalid user %s",
273                     authctxt->user);
274
275         /* Special handling for root */
276         if (authenticated && authctxt->pw->pw_uid == 0 &&
277             !auth_root_allowed(method)) {
278                 authenticated = 0;
279 #ifdef SSH_AUDIT_EVENTS
280                 PRIVSEP(audit_event(SSH_LOGIN_ROOT_DENIED));
281 #endif
282         }
283
284 #ifdef USE_PAM
285         if (options.use_pam && authenticated) {
286                 if (!PRIVSEP(do_pam_account())) {
287                         /* if PAM returned a message, send it to the user */
288                         if (buffer_len(&loginmsg) > 0) {
289                                 buffer_append(&loginmsg, "\0", 1);
290                                 userauth_send_banner(buffer_ptr(&loginmsg));
291                                 packet_write_wait();
292                         }
293                         fatal("Access denied for user %s by PAM account "
294                             "configuration", authctxt->user);
295                 }
296         }
297 #endif
298
299 #ifdef _UNICOS
300         if (authenticated && cray_access_denied(authctxt->user)) {
301                 authenticated = 0;
302                 fatal("Access denied for user %s.",authctxt->user);
303         }
304 #endif /* _UNICOS */
305
306         /* Log before sending the reply */
307         auth_log(authctxt, authenticated, method, " ssh2");
308
309         if (authctxt->postponed)
310                 return;
311
312         /* XXX todo: check if multiple auth methods are needed */
313         if (authenticated == 1) {
314                 /* turn off userauth */
315                 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &dispatch_protocol_ignore);
316                 packet_start(SSH2_MSG_USERAUTH_SUCCESS);
317                 packet_send();
318                 packet_write_wait();
319                 /* now we can break out */
320                 authctxt->success = 1;
321         } else {
322                 /* Dont count server configuration issues against the client */
323                 if (!authctxt->server_caused_failure && 
324                     authctxt->failures++ > options.max_authtries) {
325 #ifdef SSH_AUDIT_EVENTS
326                         PRIVSEP(audit_event(SSH_LOGIN_EXCEED_MAXTRIES));
327 #endif
328                         packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
329                 }
330                 methods = authmethods_get();
331                 packet_start(SSH2_MSG_USERAUTH_FAILURE);
332                 packet_put_cstring(methods);
333                 packet_put_char(0);     /* XXX partial success, unused */
334                 packet_send();
335                 packet_write_wait();
336                 xfree(methods);
337         }
338 }
339
340 static char *
341 authmethods_get(void)
342 {
343         Buffer b;
344         char *list;
345         int i;
346
347         buffer_init(&b);
348         for (i = 0; authmethods[i] != NULL; i++) {
349                 if (strcmp(authmethods[i]->name, "none") == 0)
350                         continue;
351                 if (authmethods[i]->enabled != NULL &&
352                     *(authmethods[i]->enabled) != 0) {
353                         if (buffer_len(&b) > 0)
354                                 buffer_append(&b, ",", 1);
355                         buffer_append(&b, authmethods[i]->name,
356                             strlen(authmethods[i]->name));
357                 }
358         }
359         buffer_append(&b, "\0", 1);
360         list = xstrdup(buffer_ptr(&b));
361         buffer_free(&b);
362         return list;
363 }
364
365 static Authmethod *
366 authmethod_lookup(const char *name)
367 {
368         int i;
369
370         if (name != NULL)
371                 for (i = 0; authmethods[i] != NULL; i++)
372                         if (authmethods[i]->enabled != NULL &&
373                             *(authmethods[i]->enabled) != 0 &&
374                             strcmp(name, authmethods[i]->name) == 0)
375                                 return authmethods[i];
376         debug2("Unrecognized authentication method name: %s",
377             name ? name : "NULL");
378         return NULL;
379 }
This page took 0.214573 seconds and 3 git commands to generate.