]> andersk Git - gssapi-openssh.git/blob - openssh/auth2-gss.c
remove unused label
[gssapi-openssh.git] / openssh / auth2-gss.c
1 /*      $OpenBSD: auth2-gss.c,v 1.7 2003/11/21 11:57:03 djm Exp $       */
2
3 /*
4  * Copyright (c) 2001-2003 Simon Wilkinson. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26
27 #include "includes.h"
28
29 #ifdef GSSAPI
30
31 #include "auth.h"
32 #include "ssh2.h"
33 #include "xmalloc.h"
34 #include "log.h"
35 #include "dispatch.h"
36 #include "servconf.h"
37 #include "compat.h"
38 #include "packet.h"
39 #include "monitor_wrap.h"
40
41 #include "ssh-gss.h"
42
43 extern ServerOptions options;
44
45 static int
46 userauth_external(Authctxt *authctxt)
47 {
48         packet_check_eom();
49
50         return(PRIVSEP(ssh_gssapi_userok(authctxt->user)));
51 }
52
53 static void ssh_gssapi_userauth_error(Gssctxt *ctxt);
54 static void input_gssapi_token(int type, u_int32_t plen, void *ctxt);
55 static void input_gssapi_mic(int type, u_int32_t plen, void *ctxt);
56 static void input_gssapi_exchange_complete(int type, u_int32_t plen, void *ctxt);
57 static void input_gssapi_errtok(int, u_int32_t, void *);
58
59 static int gssapi_with_mic = 1; /* flag to toggle "gssapi-with-mic" vs.
60                                    "gssapi" */
61
62 /*
63  * We only support those mechanisms that we know about (ie ones that we know
64  * how to check local user kuserok and the like
65  */
66 static int
67 userauth_gssapi(Authctxt *authctxt)
68 {
69         gss_OID_desc oid = {0, NULL};
70         Gssctxt *ctxt = NULL;
71         int mechs;
72         gss_OID_set supported;
73         int present;
74         OM_uint32 ms;
75         u_int len;
76         char *doid = NULL;
77
78         if (!authctxt->valid || authctxt->user == NULL)
79                 return (0);
80
81         mechs = packet_get_int();
82         if (mechs == 0) {
83                 debug("Mechanism negotiation is not supported");
84                 return (0);
85         }
86
87         ssh_gssapi_supported_oids(&supported);
88         do {
89                 mechs--;
90
91                 if (doid)
92                         xfree(doid);
93
94                 present = 0;
95                 doid = packet_get_string(&len);
96
97                 if (doid[0] != SSH_GSS_OIDTYPE || doid[1] != len-2) {
98                         logit("Mechanism OID received using the old encoding form");
99                         oid.elements = doid;
100                         oid.length = len;
101                 } else {
102                         oid.elements = doid + 2;
103                         oid.length   = len - 2;
104                 }
105                 gss_test_oid_set_member(&ms, &oid, supported, &present);
106         } while (mechs > 0 && !present);
107
108         gss_release_oid_set(&ms, &supported);
109
110         if (!present) {
111                 xfree(doid);
112                 return (0);
113         }
114
115         if (GSS_ERROR(PRIVSEP(ssh_gssapi_server_ctx(&ctxt, &oid)))) {
116                 xfree(doid);
117                 return (0);
118         }
119
120         authctxt->methoddata=(void *)ctxt;
121
122         packet_start(SSH2_MSG_USERAUTH_GSSAPI_RESPONSE);
123
124         /* Return the OID that we received */
125         packet_put_string(doid, len);
126
127         packet_send();
128         xfree(doid);
129
130         dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, &input_gssapi_token);
131         dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, &input_gssapi_errtok);
132         authctxt->postponed = 1;
133
134         return (0);
135 }
136
137 static void
138 input_gssapi_token(int type, u_int32_t plen, void *ctxt)
139 {
140         Authctxt *authctxt = ctxt;
141         Gssctxt *gssctxt;
142         gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
143         gss_buffer_desc recv_tok;
144         OM_uint32 maj_status, min_status, flags;
145         u_int len;
146
147         if (authctxt == NULL || (authctxt->methoddata == NULL && !use_privsep))
148                 fatal("No authentication or GSSAPI context");
149
150         gssctxt = authctxt->methoddata;
151         recv_tok.value = packet_get_string(&len);
152         recv_tok.length = len; /* u_int vs. size_t */
153
154         packet_check_eom();
155
156         maj_status = PRIVSEP(ssh_gssapi_accept_ctx(gssctxt, &recv_tok,
157             &send_tok, &flags));
158
159         xfree(recv_tok.value);
160
161         if (GSS_ERROR(maj_status)) {
162                 ssh_gssapi_userauth_error(gssctxt);
163                 if (send_tok.length != 0) {
164                         packet_start(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK);
165                         packet_put_string(send_tok.value, send_tok.length);
166                         packet_send();
167                 }
168                 authctxt->postponed = 0;
169                 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
170                 userauth_finish(authctxt, 0,
171                                 gssapi_with_mic ? "gssapi-with-mic" :
172                                                   "gssapi");
173         } else {
174                 if (send_tok.length != 0) {
175                         packet_start(SSH2_MSG_USERAUTH_GSSAPI_TOKEN);
176                         packet_put_string(send_tok.value, send_tok.length);
177                         packet_send();
178                 }
179                 if (maj_status == GSS_S_COMPLETE) {
180                         dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
181                         if (flags & GSS_C_INTEG_FLAG && gssapi_with_mic)
182                                 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_MIC,
183                                     &input_gssapi_mic);
184                         else
185                                 dispatch_set(
186                                     SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE,
187                                     &input_gssapi_exchange_complete);
188                 }
189         }
190
191         gss_release_buffer(&min_status, &send_tok);
192 }
193
194 static void
195 input_gssapi_errtok(int type, u_int32_t plen, void *ctxt)
196 {
197         Authctxt *authctxt = ctxt;
198         Gssctxt *gssctxt;
199         gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
200         gss_buffer_desc recv_tok;
201         OM_uint32 maj_status;
202         u_int len;
203
204         if (authctxt == NULL || (authctxt->methoddata == NULL && !use_privsep))
205                 fatal("No authentication or GSSAPI context");
206
207         gssctxt = authctxt->methoddata;
208         recv_tok.value = packet_get_string(&len);
209         recv_tok.length = len;
210
211         packet_check_eom();
212
213         /* Push the error token into GSSAPI to see what it says */
214         maj_status = PRIVSEP(ssh_gssapi_accept_ctx(gssctxt, &recv_tok,
215             &send_tok, NULL));
216
217         xfree(recv_tok.value);
218
219         /* We can't return anything to the client, even if we wanted to */
220         dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
221         dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, NULL);
222
223         /* The client will have already moved on to the next auth */
224
225         gss_release_buffer(&maj_status, &send_tok);
226 }
227
228 static void
229 gssapi_set_implicit_username(Authctxt *authctxt)
230 {
231     if ((strcmp(authctxt->user, "") == 0) && (authctxt->pw == NULL)) {
232         char *lname = NULL;
233         PRIVSEP(ssh_gssapi_localname(&lname));
234         if (lname && lname[0] != '\0') {
235             xfree(authctxt->user);
236             authctxt->user = lname;
237             debug("set username to %s from gssapi context", lname);
238             authctxt->pw = PRIVSEP(getpwnamallow(authctxt->user));
239         } else {
240             debug("failed to set username from gssapi context");
241         }
242     }
243     if (authctxt->pw) {
244 #ifdef USE_PAM
245         PRIVSEP(start_pam(authctxt->pw->pw_name));
246 #endif
247     }
248 }
249
250 /*
251  * This is called when the client thinks we've completed authentication.
252  * It should only be enabled in the dispatch handler by the function above,
253  * which only enables it once the GSSAPI exchange is complete.
254  */
255
256 static void
257 input_gssapi_exchange_complete(int type, u_int32_t plen, void *ctxt)
258 {
259         Authctxt *authctxt = ctxt;
260         Gssctxt *gssctxt;
261         int authenticated;
262
263         if (authctxt == NULL || (authctxt->methoddata == NULL && !use_privsep))
264                 fatal("No authentication or GSSAPI context");
265
266         gssapi_set_implicit_username(authctxt);
267
268         gssctxt = authctxt->methoddata;
269
270         /*
271          * We don't need to check the status, because we're only enabled in
272          * the dispatcher once the exchange is complete
273          */
274
275         packet_check_eom();
276
277         if (strcmp(authctxt->user, "") != 0) {
278             authenticated = PRIVSEP(ssh_gssapi_userok(authctxt->user));
279         } else {
280             authenticated = 0;
281         }
282
283         authctxt->postponed = 0;
284         dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
285         dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, NULL);
286         dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_MIC, NULL);
287         dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, NULL);
288         userauth_finish(authctxt, authenticated,
289                         gssapi_with_mic ? "gssapi-with-mic" : "gssapi");
290 }
291
292 static int
293 userauth_gssapi_with_mic(Authctxt *authctxt)
294 {
295     gssapi_with_mic = 1;
296     return userauth_gssapi(authctxt);
297 }
298
299 static int
300 userauth_gssapi_without_mic(Authctxt *authctxt)
301 {
302     gssapi_with_mic = 0;
303     return userauth_gssapi(authctxt);
304 }
305
306 static void
307 input_gssapi_mic(int type, u_int32_t plen, void *ctxt)
308 {
309         Authctxt *authctxt = ctxt;
310         Gssctxt *gssctxt;
311         int authenticated = 0;
312         Buffer b;
313         gss_buffer_desc mic, gssbuf;
314         u_int len;
315
316         if (authctxt == NULL || (authctxt->methoddata == NULL && !use_privsep))
317                 fatal("No authentication or GSSAPI context");
318
319         gssapi_set_implicit_username(authctxt);
320
321         gssctxt = authctxt->methoddata;
322
323         mic.value = packet_get_string(&len);
324         mic.length = len;
325
326         ssh_gssapi_buildmic(&b, authctxt->user, authctxt->service,
327             "gssapi-with-mic");
328
329         gssbuf.value = buffer_ptr(&b);
330         gssbuf.length = buffer_len(&b);
331
332         if (!GSS_ERROR(PRIVSEP(ssh_gssapi_checkmic(gssctxt, &gssbuf, &mic))))
333             if (strcmp(authctxt->user, "") != 0) {
334                 authenticated = PRIVSEP(ssh_gssapi_userok(authctxt->user));
335             } else {
336                 authenticated = 0;
337             }
338         else
339                 logit("GSSAPI MIC check failed");
340
341         buffer_free(&b);
342         xfree(mic.value);
343
344         authctxt->postponed = 0;
345         dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
346         dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, NULL);
347         dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_MIC, NULL);
348         dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, NULL);
349         userauth_finish(authctxt, authenticated, "gssapi-with-mic");
350 }
351
352 static void ssh_gssapi_userauth_error(Gssctxt *ctxt) {
353         char *errstr;
354         OM_uint32 maj,min;
355         
356         errstr=PRIVSEP(ssh_gssapi_last_error(ctxt,&maj,&min));
357         if (errstr) {
358                 packet_start(SSH2_MSG_USERAUTH_GSSAPI_ERROR);
359                 packet_put_int(maj);
360                 packet_put_int(min);
361                 packet_put_cstring(errstr);
362                 packet_put_cstring("");
363                 packet_send();
364                 packet_write_wait();
365                 xfree(errstr);
366         }
367 }
368
369 Authmethod method_external = {
370         "external-keyx",
371         userauth_external,
372         &options.gss_authentication
373 };
374         
375 Authmethod method_gssapi = {
376         "gssapi-with-mic",
377         userauth_gssapi_with_mic,
378         &options.gss_authentication
379 };
380
381 Authmethod method_gssapi_compat = {
382         "gssapi",
383         userauth_gssapi_without_mic,
384         &options.gss_authentication
385 };
386
387 #endif /* GSSAPI */
This page took 0.074705 seconds and 5 git commands to generate.