]> andersk Git - gssapi-openssh.git/blob - openssh/auth2-gss.c
- initialize ret_flags before calling gss_accept_sec_context()
[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=0;
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         if (options.use_pam)
246                 PRIVSEP(start_pam(authctxt->pw->pw_name));
247 #endif
248     }
249 }
250
251 /*
252  * This is called when the client thinks we've completed authentication.
253  * It should only be enabled in the dispatch handler by the function above,
254  * which only enables it once the GSSAPI exchange is complete.
255  */
256
257 static void
258 input_gssapi_exchange_complete(int type, u_int32_t plen, void *ctxt)
259 {
260         Authctxt *authctxt = ctxt;
261         Gssctxt *gssctxt;
262         int authenticated;
263
264         if (authctxt == NULL || (authctxt->methoddata == NULL && !use_privsep))
265                 fatal("No authentication or GSSAPI context");
266
267         gssapi_set_implicit_username(authctxt);
268
269         gssctxt = authctxt->methoddata;
270
271         /*
272          * We don't need to check the status, because we're only enabled in
273          * the dispatcher once the exchange is complete
274          */
275
276         packet_check_eom();
277
278         if (strcmp(authctxt->user, "") != 0) {
279             authenticated = PRIVSEP(ssh_gssapi_userok(authctxt->user));
280         } else {
281             authenticated = 0;
282         }
283
284         authctxt->postponed = 0;
285         dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
286         dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, NULL);
287         dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_MIC, NULL);
288         dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, NULL);
289         userauth_finish(authctxt, authenticated,
290                         gssapi_with_mic ? "gssapi-with-mic" : "gssapi");
291 }
292
293 static int
294 userauth_gssapi_with_mic(Authctxt *authctxt)
295 {
296     gssapi_with_mic = 1;
297     return userauth_gssapi(authctxt);
298 }
299
300 static int
301 userauth_gssapi_without_mic(Authctxt *authctxt)
302 {
303     gssapi_with_mic = 0;
304     return userauth_gssapi(authctxt);
305 }
306
307 static void
308 input_gssapi_mic(int type, u_int32_t plen, void *ctxt)
309 {
310         Authctxt *authctxt = ctxt;
311         Gssctxt *gssctxt;
312         int authenticated = 0;
313         Buffer b;
314         gss_buffer_desc mic, gssbuf;
315         u_int len;
316
317         if (authctxt == NULL || (authctxt->methoddata == NULL && !use_privsep))
318                 fatal("No authentication or GSSAPI context");
319
320         gssapi_set_implicit_username(authctxt);
321
322         gssctxt = authctxt->methoddata;
323
324         mic.value = packet_get_string(&len);
325         mic.length = len;
326
327         ssh_gssapi_buildmic(&b, authctxt->user, authctxt->service,
328             "gssapi-with-mic");
329
330         gssbuf.value = buffer_ptr(&b);
331         gssbuf.length = buffer_len(&b);
332
333         if (!GSS_ERROR(PRIVSEP(ssh_gssapi_checkmic(gssctxt, &gssbuf, &mic))))
334             if (strcmp(authctxt->user, "") != 0) {
335                 authenticated = PRIVSEP(ssh_gssapi_userok(authctxt->user));
336             } else {
337                 authenticated = 0;
338             }
339         else
340                 logit("GSSAPI MIC check failed");
341
342         buffer_free(&b);
343         xfree(mic.value);
344
345         authctxt->postponed = 0;
346         dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
347         dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, NULL);
348         dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_MIC, NULL);
349         dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, NULL);
350         userauth_finish(authctxt, authenticated, "gssapi-with-mic");
351 }
352
353 static void ssh_gssapi_userauth_error(Gssctxt *ctxt) {
354         char *errstr;
355         OM_uint32 maj,min;
356         
357         errstr=PRIVSEP(ssh_gssapi_last_error(ctxt,&maj,&min));
358         if (errstr) {
359                 packet_start(SSH2_MSG_USERAUTH_GSSAPI_ERROR);
360                 packet_put_int(maj);
361                 packet_put_int(min);
362                 packet_put_cstring(errstr);
363                 packet_put_cstring("");
364                 packet_send();
365                 packet_write_wait();
366                 xfree(errstr);
367         }
368 }
369
370 Authmethod method_external = {
371         "external-keyx",
372         userauth_external,
373         &options.gss_authentication
374 };
375         
376 Authmethod method_gssapi = {
377         "gssapi-with-mic",
378         userauth_gssapi_with_mic,
379         &options.gss_authentication
380 };
381
382 Authmethod method_gssapi_compat = {
383         "gssapi",
384         userauth_gssapi_without_mic,
385         &options.gss_authentication
386 };
387
388 #endif /* GSSAPI */
This page took 0.063836 seconds and 5 git commands to generate.