]> andersk Git - gssapi-openssh.git/blob - openssh/auth2-gss.c
authctxt->user should never be null but add a test just to be sure
[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 void ssh_gssapi_userauth_error(Gssctxt *ctxt);
46 static void input_gssapi_token(int type, u_int32_t plen, void *ctxt);
47 static void input_gssapi_mic(int type, u_int32_t plen, void *ctxt);
48 static void input_gssapi_exchange_complete(int type, u_int32_t plen, void *ctxt);
49 static void input_gssapi_errtok(int, u_int32_t, void *);
50
51 static int gssapi_with_mic = 1; /* flag to toggle "gssapi-with-mic" vs.
52                                    "gssapi" */
53
54 static int
55 userauth_external(Authctxt *authctxt)
56 {
57         packet_check_eom();
58
59         if (authctxt->valid && authctxt->user &&
60             strcmp(authctxt->user, "") != 0) {
61                 return(PRIVSEP(ssh_gssapi_userok(authctxt->user)));
62         }
63         return 0;
64 }
65
66 /*
67  * We only support those mechanisms that we know about (ie ones that we know
68  * how to check local user kuserok and the like
69  */
70 static int
71 userauth_gssapi(Authctxt *authctxt)
72 {
73         gss_OID_desc oid = {0, NULL};
74         Gssctxt *ctxt = NULL;
75         int mechs;
76         gss_OID_set supported;
77         int present;
78         OM_uint32 ms;
79         u_int len;
80         char *doid = NULL;
81
82         if (authctxt->user == NULL)
83                 return (0);
84
85         mechs = packet_get_int();
86         if (mechs == 0) {
87                 debug("Mechanism negotiation is not supported");
88                 return (0);
89         }
90
91         ssh_gssapi_supported_oids(&supported);
92         do {
93                 mechs--;
94
95                 if (doid)
96                         xfree(doid);
97
98                 present = 0;
99                 doid = packet_get_string(&len);
100
101                 if (doid[0] != SSH_GSS_OIDTYPE || doid[1] != len-2) {
102                         logit("Mechanism OID received using the old encoding form");
103                         oid.elements = doid;
104                         oid.length = len;
105                 } else {
106                         oid.elements = doid + 2;
107                         oid.length   = len - 2;
108                 }
109                 gss_test_oid_set_member(&ms, &oid, supported, &present);
110         } while (mechs > 0 && !present);
111
112         gss_release_oid_set(&ms, &supported);
113
114         if (!present) {
115                 xfree(doid);
116                 return (0);
117         }
118
119         if (GSS_ERROR(PRIVSEP(ssh_gssapi_server_ctx(&ctxt, &oid)))) {
120                 xfree(doid);
121                 return (0);
122         }
123
124         authctxt->methoddata=(void *)ctxt;
125
126         packet_start(SSH2_MSG_USERAUTH_GSSAPI_RESPONSE);
127
128         /* Return the OID that we received */
129         packet_put_string(doid, len);
130
131         packet_send();
132         xfree(doid);
133
134         dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, &input_gssapi_token);
135         dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, &input_gssapi_errtok);
136         authctxt->postponed = 1;
137
138         return (0);
139 }
140
141 static void
142 input_gssapi_token(int type, u_int32_t plen, void *ctxt)
143 {
144         Authctxt *authctxt = ctxt;
145         Gssctxt *gssctxt;
146         gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
147         gss_buffer_desc recv_tok;
148         OM_uint32 maj_status, min_status, flags=0;
149         u_int len;
150
151         if (authctxt == NULL || (authctxt->methoddata == NULL && !use_privsep))
152                 fatal("No authentication or GSSAPI context");
153
154         gssctxt = authctxt->methoddata;
155         recv_tok.value = packet_get_string(&len);
156         recv_tok.length = len; /* u_int vs. size_t */
157
158         packet_check_eom();
159
160         maj_status = PRIVSEP(ssh_gssapi_accept_ctx(gssctxt, &recv_tok,
161             &send_tok, &flags));
162
163         xfree(recv_tok.value);
164
165         if (GSS_ERROR(maj_status)) {
166                 ssh_gssapi_userauth_error(gssctxt);
167                 if (send_tok.length != 0) {
168                         packet_start(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK);
169                         packet_put_string(send_tok.value, send_tok.length);
170                         packet_send();
171                 }
172                 authctxt->postponed = 0;
173                 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
174                 userauth_finish(authctxt, 0,
175                                 gssapi_with_mic ? "gssapi-with-mic" :
176                                                   "gssapi");
177         } else {
178                 if (send_tok.length != 0) {
179                         packet_start(SSH2_MSG_USERAUTH_GSSAPI_TOKEN);
180                         packet_put_string(send_tok.value, send_tok.length);
181                         packet_send();
182                 }
183                 if (maj_status == GSS_S_COMPLETE) {
184                         dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
185                         if (flags & GSS_C_INTEG_FLAG && gssapi_with_mic)
186                                 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_MIC,
187                                     &input_gssapi_mic);
188                         else
189                                 dispatch_set(
190                                     SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE,
191                                     &input_gssapi_exchange_complete);
192                 }
193         }
194
195         gss_release_buffer(&min_status, &send_tok);
196 }
197
198 static void
199 input_gssapi_errtok(int type, u_int32_t plen, void *ctxt)
200 {
201         Authctxt *authctxt = ctxt;
202         Gssctxt *gssctxt;
203         gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
204         gss_buffer_desc recv_tok;
205         OM_uint32 maj_status;
206         u_int len;
207
208         if (authctxt == NULL || (authctxt->methoddata == NULL && !use_privsep))
209                 fatal("No authentication or GSSAPI context");
210
211         gssctxt = authctxt->methoddata;
212         recv_tok.value = packet_get_string(&len);
213         recv_tok.length = len;
214
215         packet_check_eom();
216
217         /* Push the error token into GSSAPI to see what it says */
218         maj_status = PRIVSEP(ssh_gssapi_accept_ctx(gssctxt, &recv_tok,
219             &send_tok, NULL));
220
221         xfree(recv_tok.value);
222
223         /* We can't return anything to the client, even if we wanted to */
224         dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
225         dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, NULL);
226
227         /* The client will have already moved on to the next auth */
228
229         gss_release_buffer(&maj_status, &send_tok);
230 }
231
232 static void
233 gssapi_set_implicit_username(Authctxt *authctxt)
234 {
235     if ((authctxt->user == NULL) || (strcmp(authctxt->user, "") == 0)) {
236         char *lname = NULL;
237         PRIVSEP(ssh_gssapi_localname(&lname));
238         if (lname && lname[0] != '\0') {
239             xfree(authctxt->user);
240             authctxt->user = lname;
241             debug("set username to %s from gssapi context", lname);
242             authctxt->pw = PRIVSEP(getpwnamallow(authctxt->user));
243             if (authctxt->pw) {
244                 authctxt->valid = 1;
245             }
246         } else {
247             debug("failed to set username from gssapi context");
248         }
249     }
250     if (authctxt->pw) {
251 #ifdef USE_PAM
252         if (options.use_pam)
253                 PRIVSEP(start_pam(authctxt));
254 #endif
255     }
256 }
257
258 /*
259  * This is called when the client thinks we've completed authentication.
260  * It should only be enabled in the dispatch handler by the function above,
261  * which only enables it once the GSSAPI exchange is complete.
262  */
263
264 static void
265 input_gssapi_exchange_complete(int type, u_int32_t plen, void *ctxt)
266 {
267         Authctxt *authctxt = ctxt;
268         Gssctxt *gssctxt;
269         int authenticated;
270
271         if (authctxt == NULL || (authctxt->methoddata == NULL && !use_privsep))
272                 fatal("No authentication or GSSAPI context");
273
274         gssapi_set_implicit_username(authctxt);
275
276         gssctxt = authctxt->methoddata;
277
278         /*
279          * We don't need to check the status, because we're only enabled in
280          * the dispatcher once the exchange is complete
281          */
282
283         packet_check_eom();
284
285         if (authctxt->valid && strcmp(authctxt->user, "") != 0) {
286             authenticated = PRIVSEP(ssh_gssapi_userok(authctxt->user));
287         } else {
288             authenticated = 0;
289         }
290
291         authctxt->postponed = 0;
292         dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
293         dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, NULL);
294         dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_MIC, NULL);
295         dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, NULL);
296         userauth_finish(authctxt, authenticated,
297                         gssapi_with_mic ? "gssapi-with-mic" : "gssapi");
298 }
299
300 static int
301 userauth_gssapi_with_mic(Authctxt *authctxt)
302 {
303     gssapi_with_mic = 1;
304     return userauth_gssapi(authctxt);
305 }
306
307 static int
308 userauth_gssapi_without_mic(Authctxt *authctxt)
309 {
310     gssapi_with_mic = 0;
311     return userauth_gssapi(authctxt);
312 }
313
314 static void
315 input_gssapi_mic(int type, u_int32_t plen, void *ctxt)
316 {
317         Authctxt *authctxt = ctxt;
318         Gssctxt *gssctxt;
319         int authenticated = 0;
320         Buffer b;
321         gss_buffer_desc mic, gssbuf;
322         u_int len;
323
324         if (authctxt == NULL || (authctxt->methoddata == NULL && !use_privsep))
325                 fatal("No authentication or GSSAPI context");
326
327         gssapi_set_implicit_username(authctxt);
328
329         gssctxt = authctxt->methoddata;
330
331         mic.value = packet_get_string(&len);
332         mic.length = len;
333
334         ssh_gssapi_buildmic(&b, authctxt->user, authctxt->service,
335             "gssapi-with-mic");
336
337         gssbuf.value = buffer_ptr(&b);
338         gssbuf.length = buffer_len(&b);
339
340         if (!GSS_ERROR(PRIVSEP(ssh_gssapi_checkmic(gssctxt, &gssbuf, &mic))))
341             if (authctxt->valid && strcmp(authctxt->user, "") != 0) {
342                 authenticated = PRIVSEP(ssh_gssapi_userok(authctxt->user));
343             } else {
344                 authenticated = 0;
345             }
346         else
347                 logit("GSSAPI MIC check failed");
348
349         buffer_free(&b);
350         xfree(mic.value);
351
352         authctxt->postponed = 0;
353         dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
354         dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, NULL);
355         dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_MIC, NULL);
356         dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, NULL);
357         userauth_finish(authctxt, authenticated, "gssapi-with-mic");
358 }
359
360 static void ssh_gssapi_userauth_error(Gssctxt *ctxt) {
361         char *errstr;
362         OM_uint32 maj,min;
363         
364         errstr=PRIVSEP(ssh_gssapi_last_error(ctxt,&maj,&min));
365         if (errstr) {
366                 packet_start(SSH2_MSG_USERAUTH_GSSAPI_ERROR);
367                 packet_put_int(maj);
368                 packet_put_int(min);
369                 packet_put_cstring(errstr);
370                 packet_put_cstring("");
371                 packet_send();
372                 packet_write_wait();
373                 xfree(errstr);
374         }
375 }
376
377 Authmethod method_external = {
378         "external-keyx",
379         userauth_external,
380         &options.gss_authentication
381 };
382         
383 Authmethod method_gssapi = {
384         "gssapi-with-mic",
385         userauth_gssapi_with_mic,
386         &options.gss_authentication
387 };
388
389 Authmethod method_gssapi_compat = {
390         "gssapi",
391         userauth_gssapi_without_mic,
392         &options.gss_authentication
393 };
394
395 #endif /* GSSAPI */
This page took 0.52166 seconds and 5 git commands to generate.