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