]> andersk Git - gssapi-openssh.git/blob - openssh/auth2.c
o Remove compat package from bundle.
[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.102 2003/08/26 09:58:43 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
39 #ifdef GSSAPI
40 #include "ssh-gss.h"
41 #endif
42
43 /* import */
44 extern ServerOptions options;
45 extern u_char *session_id2;
46 extern u_int session_id2_len;
47
48 Authctxt *x_authctxt = NULL;
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_gssapi;
60 #endif
61
62 Authmethod *authmethods[] = {
63         &method_none,
64 #ifdef GSSAPI
65         &method_external,
66         &method_gssapi,
67 #endif
68         &method_pubkey,
69 #ifdef GSSAPI
70         &method_gssapi,
71 #endif
72         &method_passwd,
73         &method_kbdint,
74         &method_hostbased,
75         NULL
76 };
77
78 /* protocol */
79
80 static void input_service_request(int, u_int32_t, void *);
81 static void input_userauth_request(int, u_int32_t, void *);
82
83 /* helper */
84 static Authmethod *authmethod_lookup(const char *);
85 static char *authmethods_get(void);
86 int user_key_allowed(struct passwd *, Key *);
87 int hostbased_key_allowed(struct passwd *, const char *, char *, Key *);
88
89 /*
90  * loop until authctxt->success == TRUE
91  */
92
93 Authctxt *
94 do_authentication2(void)
95 {
96         Authctxt *authctxt = authctxt_new();
97
98         x_authctxt = authctxt;          /*XXX*/
99
100         /* challenge-response is implemented via keyboard interactive */
101         if (options.challenge_response_authentication)
102                 options.kbd_interactive_authentication = 1;
103
104         dispatch_init(&dispatch_protocol_error);
105         dispatch_set(SSH2_MSG_SERVICE_REQUEST, &input_service_request);
106         dispatch_run(DISPATCH_BLOCK, &authctxt->success, authctxt);
107
108         return (authctxt);
109 }
110
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 static void
145 input_userauth_request(int type, u_int32_t seq, void *ctxt)
146 {
147         Authctxt *authctxt = ctxt;
148         Authmethod *m = NULL;
149         char *user, *service, *method, *style = NULL;
150         int authenticated = 0;
151
152         if (authctxt == NULL)
153                 fatal("input_userauth_request: no authctxt");
154
155         user = packet_get_string(NULL);
156         service = packet_get_string(NULL);
157         method = packet_get_string(NULL);
158
159 #ifdef GSSAPI
160         if (strcmp(user, "") == 0) {
161             debug("received empty username for %s", method);
162             if (strcmp(method, "external-keyx") == 0) {
163                 char *lname = NULL;
164                 PRIVSEP(ssh_gssapi_localname(&lname));
165                 if (lname && lname[0] != '\0') {
166                     xfree(user);
167                     user = lname;
168                     debug("set username to %s from gssapi context", user);
169                 } else if (authctxt->valid) {
170                     debug("failed to set username from gssapi context");
171                 }
172             }
173         }
174 #endif
175
176         debug("userauth-request for user %s service %s method %s",
177               (user && user[0]) ? user : "<implicit>", service, method);
178         debug("attempt %d failures %d", authctxt->attempt, authctxt->failures);
179
180         if ((style = strchr(user, ':')) != NULL)
181                 *style++ = 0;
182
183         authctxt->attempt++;
184         if (!authctxt->user ||
185             strcmp(user, authctxt->user) != 0) {
186                 /* setup auth context */
187                 if (authctxt->user) {
188                     xfree(authctxt->user);
189                     authctxt->user = NULL;
190                 }
191                 if (authctxt->service) {
192                     xfree(authctxt->service);
193                     authctxt->service = NULL;
194                 }
195                 if (authctxt->style) {
196                     xfree(authctxt->style);
197                     authctxt->style = NULL;
198                 }
199 #ifdef GSSAPI
200                 /* We'll verify the username after we set it from the
201                    GSSAPI context. */
202                 if ((strcmp(user, "") == 0) &&
203                     ((strcmp(method, "gssapi") == 0) ||
204                      (strcmp(method, "external-keyx") == 0))) {
205                     authctxt->pw = NULL;
206                     authctxt->valid = 1;
207                 } else {
208 #endif
209                 authctxt->pw = PRIVSEP(getpwnamallow(user));
210                 if (authctxt->pw && strcmp(service, "ssh-connection")==0) {
211                         authctxt->valid = 1;
212                         debug2("input_userauth_request: setting up authctxt for %s", user);
213 #ifdef USE_PAM
214                         if (options.use_pam)
215                                 PRIVSEP(start_pam(authctxt->pw->pw_name));
216 #endif
217                 } else {
218                         logit("input_userauth_request: illegal user %s", user);
219                         authctxt->pw = fakepw();
220 #ifdef USE_PAM
221                         if (options.use_pam)
222                                 PRIVSEP(start_pam(user));
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         } else if (strcmp(service, authctxt->service) != 0) {
236                 packet_disconnect("Change of service not allowed: "
237                     "(%s,%s) -> (%s,%s)",
238                     authctxt->user, authctxt->service, user, service);
239         }
240         /* reset state */
241         auth2_challenge_stop(authctxt);
242
243 #ifdef GSSAPI
244         dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
245         dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, NULL);
246 #endif
247
248         authctxt->postponed = 0;
249
250         /* try to authenticate user */
251         m = authmethod_lookup(method);
252         if (m != NULL) {
253                 debug2("input_userauth_request: try method %s", method);
254                 authenticated = m->userauth(authctxt);
255         }
256         userauth_finish(authctxt, authenticated, method);
257
258         xfree(service);
259         xfree(user);
260         xfree(method);
261 }
262
263 void
264 userauth_finish(Authctxt *authctxt, int authenticated, char *method)
265 {
266         char *methods;
267
268         if (!authctxt->valid && authenticated)
269                 fatal("INTERNAL ERROR: authenticated invalid user %s",
270                     authctxt->user);
271
272         /* Special handling for root */
273         if (authenticated && authctxt->pw->pw_uid == 0 &&
274             !auth_root_allowed(method))
275                 authenticated = 0;
276
277 #ifdef USE_PAM
278         if (options.use_pam && authenticated && !PRIVSEP(do_pam_account()))
279                 authenticated = 0;
280 #endif
281
282 #ifdef _UNICOS
283         if (authenticated && cray_access_denied(authctxt->user)) {
284                 authenticated = 0;
285                 fatal("Access denied for user %s.",authctxt->user);
286         }
287 #endif /* _UNICOS */
288
289         /* Log before sending the reply */
290         auth_log(authctxt, authenticated, method, " ssh2");
291
292         if (authctxt->postponed)
293                 return;
294
295         /* XXX todo: check if multiple auth methods are needed */
296         if (authenticated == 1) {
297                 /* turn off userauth */
298                 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &dispatch_protocol_ignore);
299                 packet_start(SSH2_MSG_USERAUTH_SUCCESS);
300                 packet_send();
301                 packet_write_wait();
302                 /* now we can break out */
303                 authctxt->success = 1;
304         } else {
305                 if (authctxt->failures++ > AUTH_FAIL_MAX)
306                         packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
307                 methods = authmethods_get();
308                 packet_start(SSH2_MSG_USERAUTH_FAILURE);
309                 packet_put_cstring(methods);
310                 packet_put_char(0);     /* XXX partial success, unused */
311                 packet_send();
312                 packet_write_wait();
313                 xfree(methods);
314         }
315 }
316
317 /* get current user */
318
319 struct passwd*
320 auth_get_user(void)
321 {
322         return (x_authctxt != NULL && x_authctxt->valid) ? x_authctxt->pw : NULL;
323 }
324
325 #define DELIM   ","
326
327 static char *
328 authmethods_get(void)
329 {
330         Buffer b;
331         char *list;
332         int i;
333
334         buffer_init(&b);
335         for (i = 0; authmethods[i] != NULL; i++) {
336                 if (strcmp(authmethods[i]->name, "none") == 0)
337                         continue;
338                 if (authmethods[i]->enabled != NULL &&
339                     *(authmethods[i]->enabled) != 0) {
340                         if (buffer_len(&b) > 0)
341                                 buffer_append(&b, ",", 1);
342                         buffer_append(&b, authmethods[i]->name,
343                             strlen(authmethods[i]->name));
344                 }
345         }
346         buffer_append(&b, "\0", 1);
347         list = xstrdup(buffer_ptr(&b));
348         buffer_free(&b);
349         return list;
350 }
351
352 static Authmethod *
353 authmethod_lookup(const char *name)
354 {
355         int i;
356
357         if (name != NULL)
358                 for (i = 0; authmethods[i] != NULL; i++)
359                         if (authmethods[i]->enabled != NULL &&
360                             *(authmethods[i]->enabled) != 0 &&
361                             strcmp(name, authmethods[i]->name) == 0)
362                                 return authmethods[i];
363         debug2("Unrecognized authentication method name: %s",
364             name ? name : "NULL");
365         return NULL;
366 }
This page took 0.066552 seconds and 5 git commands to generate.