]> andersk Git - gssapi-openssh.git/blob - openssh/auth2.c
apply updates from OpenSSH-4.3p1-hpn11-none.patch
[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.107 2004/07/28 09:40:29 markus Exp $");
27
28 #include "ssh2.h"
29 #include "xmalloc.h"
30 #include "packet.h"
31 #include "log.h"
32 #include "servconf.h"
33 #include "compat.h"
34 #include "auth.h"
35 #include "dispatch.h"
36 #include "pathnames.h"
37 #include "monitor_wrap.h"
38 #include "buffer.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 u_int session_id2_len;
48 extern Buffer loginmsg;
49
50 /* methods */
51
52 extern Authmethod method_none;
53 extern Authmethod method_pubkey;
54 extern Authmethod method_passwd;
55 extern Authmethod method_kbdint;
56 extern Authmethod method_hostbased;
57 #ifdef GSSAPI
58 extern Authmethod method_external;
59 extern Authmethod method_gsskeyex;
60 extern Authmethod method_gssapi;
61 extern Authmethod method_gssapi_compat;
62 #endif
63
64 Authmethod *authmethods[] = {
65         &method_none,
66         &method_pubkey,
67 #ifdef GSSAPI
68         &method_gsskeyex,
69         &method_external,
70         &method_gssapi,
71         &method_gssapi_compat,
72 #endif
73         &method_passwd,
74         &method_kbdint,
75         &method_hostbased,
76         NULL
77 };
78
79 /* protocol */
80
81 static void input_service_request(int, u_int32_t, void *);
82 static void input_userauth_request(int, u_int32_t, void *);
83
84 /* helper */
85 static Authmethod *authmethod_lookup(const char *);
86 static char *authmethods_get(void);
87 int user_key_allowed(struct passwd *, Key *);
88
89 /*
90  * loop until authctxt->success == TRUE
91  */
92
93 void
94 do_authentication2(Authctxt *authctxt)
95 {
96         /* challenge-response is implemented via keyboard interactive */
97         if (options.challenge_response_authentication)
98                 options.kbd_interactive_authentication = 1;
99
100         dispatch_init(&dispatch_protocol_error);
101         dispatch_set(SSH2_MSG_SERVICE_REQUEST, &input_service_request);
102         dispatch_run(DISPATCH_BLOCK, &authctxt->success, authctxt);
103 }
104
105 static void
106 input_service_request(int type, u_int32_t seq, void *ctxt)
107 {
108         Authctxt *authctxt = ctxt;
109         u_int len;
110         int acceptit = 0;
111         char *service = packet_get_string(&len);
112         packet_check_eom();
113
114         if (authctxt == NULL)
115                 fatal("input_service_request: no authctxt");
116
117         if (strcmp(service, "ssh-userauth") == 0) {
118                 if (!authctxt->success) {
119                         acceptit = 1;
120                         /* now we can handle user-auth requests */
121                         dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &input_userauth_request);
122                 }
123         }
124         /* XXX all other service requests are denied */
125
126         if (acceptit) {
127                 packet_start(SSH2_MSG_SERVICE_ACCEPT);
128                 packet_put_cstring(service);
129                 packet_send();
130                 packet_write_wait();
131         } else {
132                 debug("bad service request %s", service);
133                 packet_disconnect("bad service request %s", service);
134         }
135         xfree(service);
136 }
137
138 static void
139 input_userauth_request(int type, u_int32_t seq, void *ctxt)
140 {
141         Authctxt *authctxt = ctxt;
142         Authmethod *m = NULL;
143         char *user, *service, *method, *style = NULL;
144         int authenticated = 0;
145
146         if (authctxt == NULL)
147                 fatal("input_userauth_request: no authctxt");
148
149         user = packet_get_string(NULL);
150         service = packet_get_string(NULL);
151         method = packet_get_string(NULL);
152
153 #ifdef GSSAPI
154         if (user[0] == '\0') {
155             debug("received empty username for %s", method);
156             if (strcmp(method, "external-keyx") == 0 ||
157                 strcmp(method, "gssapi-keyex") == 0) {
158                 char *lname = NULL;
159                 PRIVSEP(ssh_gssapi_localname(&lname));
160                 if (lname && lname[0] != '\0') {
161                     xfree(user);
162                     user = lname;
163                     debug("set username to %s from gssapi context", user);
164                 } else {
165                     debug("failed to set username from gssapi context");
166                 }
167             }
168         }
169 #endif
170
171         debug("userauth-request for user %s service %s method %s",
172               user[0] ? user : "<implicit>", service, method);
173         debug("attempt %d failures %d", authctxt->attempt, authctxt->failures);
174
175         if ((style = strchr(user, ':')) != NULL)
176                 *style++ = 0;
177
178         /* If first time or username changed or implicit username,
179            setup/reset authentication context. */
180         if ((authctxt->attempt++ == 0) ||
181             (strcmp(user, authctxt->user) != 0) ||
182             (strcmp(user, "") == 0)) {
183                 if (authctxt->user) {
184                     xfree(authctxt->user);
185                     authctxt->user = NULL;
186                 }
187                 authctxt->valid = 0;
188 #ifdef GSSAPI
189                 /* If we're going to set the username based on the
190                    GSSAPI context later, then wait until then to
191                    verify it. Just put in placeholders for now. */
192                 if ((strcmp(user, "") == 0) &&
193                     ((strcmp(method, "gssapi") == 0) ||
194                      (strcmp(method, "gssapi-with-mic") == 0))) {
195                         authctxt->pw = fakepw();
196                         authctxt->user = xstrdup(user);
197                 } else {
198 #endif
199                 authctxt->pw = PRIVSEP(getpwnamallow(user));
200                 authctxt->user = xstrdup(user);
201                 if (authctxt->pw && strcmp(service, "ssh-connection")==0) {
202                         authctxt->valid = 1;
203                         debug2("input_userauth_request: setting up authctxt for %s", user);
204                 } else {
205                         logit("input_userauth_request: invalid user %s", user);
206                         authctxt->pw = fakepw();
207 #ifdef SSH_AUDIT_EVENTS
208                         PRIVSEP(audit_event(SSH_INVALID_USER));
209 #endif
210                 }
211 #ifdef GSSAPI
212                 } /* endif for setting username based on GSSAPI context */
213 #endif
214 #ifdef USE_PAM
215                 if (options.use_pam)
216                         PRIVSEP(start_pam(authctxt));
217 #endif
218                 setproctitle("%s%s", authctxt->valid ? user : "unknown",
219                     use_privsep ? " [net]" : "");
220 #ifdef GSSAPI
221                 if (authctxt->attempt == 1) {
222 #endif
223                 authctxt->service = xstrdup(service);
224                 authctxt->style = style ? xstrdup(style) : NULL;
225                 if (use_privsep)
226                         mm_inform_authserv(service, style);
227 #ifdef GSSAPI
228                 } /* if (authctxt->attempt == 1) */
229 #endif
230         }
231         if (strcmp(service, authctxt->service) != 0) {
232                 packet_disconnect("Change of service not allowed: "
233                     "(%s,%s) -> (%s,%s)",
234                     authctxt->user, authctxt->service, user, service);
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         authctxt->server_caused_failure = 0;
246
247         /* try to authenticate user */
248         m = authmethod_lookup(method);
249         if (m != NULL) {
250                 debug2("input_userauth_request: try method %s", method);
251                 authenticated = m->userauth(authctxt);
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 (authenticated && authctxt->pw->pw_uid == 0 &&
271             !auth_root_allowed(method)) {
272                 authenticated = 0;
273 #ifdef SSH_AUDIT_EVENTS
274                 PRIVSEP(audit_event(SSH_LOGIN_ROOT_DENIED));
275 #endif
276         }
277
278 #ifdef USE_PAM
279         if (options.use_pam && authenticated) {
280                 if (!PRIVSEP(do_pam_account())) {
281                         /* if PAM returned a message, send it to the user */
282                         if (buffer_len(&loginmsg) > 0) {
283                                 buffer_append(&loginmsg, "\0", 1);
284                                 userauth_send_banner(buffer_ptr(&loginmsg));
285                                 packet_write_wait();
286                         }
287                         fatal("Access denied for user %s by PAM account "
288                             "configuration", authctxt->user);
289                 }
290         }
291 #endif
292
293 #ifdef _UNICOS
294         if (authenticated && cray_access_denied(authctxt->user)) {
295                 authenticated = 0;
296                 fatal("Access denied for user %s.",authctxt->user);
297         }
298 #endif /* _UNICOS */
299
300         /* Log before sending the reply */
301         auth_log(authctxt, authenticated, method, " ssh2");
302
303         if (authctxt->postponed)
304                 return;
305
306         /* XXX todo: check if multiple auth methods are needed */
307         if (authenticated == 1) {
308                 /* turn off userauth */
309                 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &dispatch_protocol_ignore);
310                 packet_start(SSH2_MSG_USERAUTH_SUCCESS);
311                 packet_send();
312                 packet_write_wait();
313                 /* now we can break out */
314                 authctxt->success = 1;
315         } else {
316                 /* Dont count server configuration issues against the client */
317                 if (!authctxt->server_caused_failure && 
318                     authctxt->failures++ > options.max_authtries) {
319 #ifdef SSH_AUDIT_EVENTS
320                         PRIVSEP(audit_event(SSH_LOGIN_EXCEED_MAXTRIES));
321 #endif
322                         packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
323                 }
324                 methods = authmethods_get();
325                 packet_start(SSH2_MSG_USERAUTH_FAILURE);
326                 packet_put_cstring(methods);
327                 packet_put_char(0);     /* XXX partial success, unused */
328                 packet_send();
329                 packet_write_wait();
330                 xfree(methods);
331         }
332 }
333
334 #define DELIM   ","
335
336 static char *
337 authmethods_get(void)
338 {
339         Buffer b;
340         char *list;
341         int i;
342
343         buffer_init(&b);
344         for (i = 0; authmethods[i] != NULL; i++) {
345                 if (strcmp(authmethods[i]->name, "none") == 0)
346                         continue;
347                 if (authmethods[i]->enabled != NULL &&
348                     *(authmethods[i]->enabled) != 0) {
349                         if (buffer_len(&b) > 0)
350                                 buffer_append(&b, ",", 1);
351                         buffer_append(&b, authmethods[i]->name,
352                             strlen(authmethods[i]->name));
353                 }
354         }
355         buffer_append(&b, "\0", 1);
356         list = xstrdup(buffer_ptr(&b));
357         buffer_free(&b);
358         return list;
359 }
360
361 static Authmethod *
362 authmethod_lookup(const char *name)
363 {
364         int i;
365
366         if (name != NULL)
367                 for (i = 0; authmethods[i] != NULL; i++)
368                         if (authmethods[i]->enabled != NULL &&
369                             *(authmethods[i]->enabled) != 0 &&
370                             strcmp(name, authmethods[i]->name) == 0)
371                                 return authmethods[i];
372         debug2("Unrecognized authentication method name: %s",
373             name ? name : "NULL");
374         return NULL;
375 }
This page took 0.267595 seconds and 5 git commands to generate.