]> andersk Git - gssapi-openssh.git/blob - openssh/auth2-gss.c
merged OpenSSH 3.7p1 to trunk
[gssapi-openssh.git] / openssh / auth2-gss.c
1 /*      $OpenBSD: auth2-gss.c,v 1.3 2003/09/01 20:44:54 markus 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 "ssh1.h"
34 #include "xmalloc.h"
35 #include "log.h"
36 #include "dispatch.h"
37 #include "servconf.h"
38 #include "compat.h"
39 #include "packet.h"
40 #include "monitor_wrap.h"
41
42 #include "ssh-gss.h"
43
44 extern ServerOptions options;
45 unsigned char ssh1_key_digest[16];
46
47 static int
48 userauth_external(Authctxt *authctxt)
49 {
50         packet_check_eom();
51
52         return(PRIVSEP(ssh_gssapi_userok(authctxt->user)));
53 }
54
55 static void ssh_gssapi_userauth_error(Gssctxt *ctxt);
56 static void input_gssapi_token(int type, u_int32_t plen, void *ctxt);
57 static void input_gssapi_exchange_complete(int type, u_int32_t plen, void *ctxt);
58 static void input_gssapi_errtok(int, u_int32_t, void *);
59
60 /*
61  * We only support those mechanisms that we know about (ie ones that we know
62  * how to check local user kuserok and the like
63  */
64 static int
65 userauth_gssapi(Authctxt *authctxt)
66 {
67         gss_OID_desc oid = {0, NULL};
68         Gssctxt *ctxt = NULL;
69         int mechs;
70         gss_OID_set supported;
71         int present;
72         OM_uint32 ms;
73         u_int len;
74         char *doid = NULL;
75
76         if (!authctxt->valid || authctxt->user == NULL)
77                 return (0);
78
79         mechs = packet_get_int();
80         if (mechs == 0) {
81                 debug("Mechanism negotiation is not supported");
82                 return (0);
83         }
84
85         ssh_gssapi_supported_oids(&supported);
86         do {
87                 mechs--;
88
89                 if (doid)
90                         xfree(doid);
91
92                 doid = packet_get_string(&len);
93
94                 if (doid[0] != SSH_GSS_OIDTYPE || doid[1] != len-2) {
95                         logit("Mechanism OID received using the old encoding form");
96                         oid.elements = doid;
97                         oid.length = len;
98                 } else {
99                         oid.elements = doid + 2;
100                         oid.length   = len - 2;
101                 }
102                 gss_test_oid_set_member(&ms, &oid, supported, &present);
103         } while (mechs > 0 && !present);
104
105         gss_release_oid_set(&ms, &supported);
106
107         if (!present) {
108                 xfree(doid);
109                 return (0);
110         }
111
112         if (GSS_ERROR(PRIVSEP(ssh_gssapi_server_ctx(&ctxt, &oid)))) {
113                 xfree(doid);
114                 return (0);
115         }
116
117         authctxt->methoddata=(void *)ctxt;
118
119         if (!compat20) {
120
121         packet_start(SSH_SMSG_AUTH_GSSAPI_RESPONSE);
122         packet_put_string(oid.elements,oid.length);
123
124         } else {
125
126         packet_start(SSH2_MSG_USERAUTH_GSSAPI_RESPONSE);
127
128         /* Return OID in same format as we received it*/
129         packet_put_string(doid, len);
130
131         } /* !compat20 */
132
133         packet_send();
134         xfree(doid);
135
136         if (!compat20)
137         dispatch_set(SSH_MSG_AUTH_GSSAPI_TOKEN, &input_gssapi_token);
138         else
139         dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, &input_gssapi_token);
140         dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, &input_gssapi_errtok);
141         authctxt->postponed = 1;
142
143         return (0);
144 }
145
146 static void
147 input_gssapi_token(int type, u_int32_t plen, void *ctxt)
148 {
149         Authctxt *authctxt = ctxt;
150         Gssctxt *gssctxt;
151         gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
152         gss_buffer_desc recv_tok;
153         OM_uint32 maj_status, min_status;
154         u_int len;
155
156         if (authctxt == NULL || (authctxt->methoddata == NULL && !use_privsep))
157                 fatal("No authentication or GSSAPI context");
158
159         gssctxt = authctxt->methoddata;
160         recv_tok.value = packet_get_string(&len);
161         recv_tok.length = len; /* u_int vs. size_t */
162
163         packet_check_eom();
164
165         maj_status = PRIVSEP(ssh_gssapi_accept_ctx(gssctxt, &recv_tok,
166             &send_tok, NULL));
167
168         xfree(recv_tok.value);
169
170         if (GSS_ERROR(maj_status)) {
171                 ssh_gssapi_userauth_error(gssctxt);
172                 if (send_tok.length != 0) {
173                         if (!compat20)
174                         packet_start(SSH_MSG_AUTH_GSSAPI_TOKEN);
175                         else
176                         packet_start(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK);
177                         packet_put_string(send_tok.value, send_tok.length);
178                         packet_send();
179                 }
180                 authctxt->postponed = 0;
181                 dispatch_set(SSH_MSG_AUTH_GSSAPI_TOKEN, NULL);
182                 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
183                 userauth_finish(authctxt, 0, "gssapi");
184         } else {
185                 if (send_tok.length != 0) {
186                         if (!compat20)
187                         packet_start(SSH_MSG_AUTH_GSSAPI_TOKEN);
188                         else
189                         packet_start(SSH2_MSG_USERAUTH_GSSAPI_TOKEN);
190                         packet_put_string(send_tok.value, send_tok.length);
191                         packet_send();
192                 }
193                 if (maj_status == GSS_S_COMPLETE) {
194                         dispatch_set(SSH_MSG_AUTH_GSSAPI_TOKEN, NULL);
195                         dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
196                         if (!compat20)
197                         input_gssapi_exchange_complete(0, 0, ctxt);
198                         else
199                         dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE,
200                                      &input_gssapi_exchange_complete);
201                 }
202         }
203
204         gss_release_buffer(&min_status, &send_tok);
205 }
206
207 static void
208 input_gssapi_errtok(int type, u_int32_t plen, void *ctxt)
209 {
210         Authctxt *authctxt = ctxt;
211         Gssctxt *gssctxt;
212         gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
213         gss_buffer_desc recv_tok;
214         OM_uint32 maj_status;
215         u_int len;
216
217         if (authctxt == NULL || (authctxt->methoddata == NULL && !use_privsep))
218                 fatal("No authentication or GSSAPI context");
219
220         gssctxt = authctxt->methoddata;
221         recv_tok.value = packet_get_string(&len);
222         recv_tok.length = len;
223
224         packet_check_eom();
225
226         /* Push the error token into GSSAPI to see what it says */
227         maj_status = PRIVSEP(ssh_gssapi_accept_ctx(gssctxt, &recv_tok,
228             &send_tok, NULL));
229
230         xfree(recv_tok.value);
231
232         /* We can't return anything to the client, even if we wanted to */
233         dispatch_set(SSH_MSG_AUTH_GSSAPI_TOKEN, NULL);
234         dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
235         dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, NULL);
236
237         /* The client will have already moved on to the next auth */
238
239         gss_release_buffer(&maj_status, &send_tok);
240 }
241
242 /*
243  * This is called when the client thinks we've completed authentication.
244  * It should only be enabled in the dispatch handler by the function above,
245  * which only enables it once the GSSAPI exchange is complete.
246  */
247
248 static void
249 input_gssapi_exchange_complete(int type, u_int32_t plen, void *ctxt)
250 {
251         Authctxt *authctxt = ctxt;
252         Gssctxt *gssctxt;
253         int authenticated;
254
255         if (authctxt == NULL || (authctxt->methoddata == NULL && !use_privsep))
256                 fatal("No authentication or GSSAPI context");
257
258         if ((strcmp(authctxt->user, "") == 0) && (authctxt->pw == NULL)) {
259             char *lname = NULL;
260             PRIVSEP(ssh_gssapi_localname(&lname));
261             if (lname && lname[0] != '\0') {
262                 xfree(authctxt->user);
263                 authctxt->user = lname;
264                 debug("set username to %s from gssapi context", lname);
265                 authctxt->pw = PRIVSEP(getpwnamallow(authctxt->user));
266             } else {
267                 debug("failed to set username from gssapi context");
268             }
269         }
270         if (authctxt->pw) {
271 #ifdef USE_PAM
272             PRIVSEP(start_pam(authctxt->pw->pw_name));
273 #endif
274         } else {
275             authctxt->valid = 0;
276             authenticated = 0;
277             goto finish;
278         }
279
280         gssctxt = authctxt->methoddata;
281
282         /* ssh1 needs to exchange the hash of the keys */
283         if (!compat20) {
284
285                 OM_uint32 min_status;
286                 gss_buffer_desc dummy, msg_tok;
287
288                 /* ssh1 wraps the keys, in the monitor */
289
290                 dummy.value=malloc(sizeof(ssh1_key_digest));
291                 memcpy(dummy.value,ssh1_key_digest,sizeof(ssh1_key_digest));
292                 dummy.length=sizeof(ssh1_key_digest);
293                 if (GSS_ERROR(PRIVSEP(ssh_gssapi_sign(gssctxt,&dummy,&msg_tok))))
294                     fatal("Couldn't wrap keys");
295  
296                 packet_start(SSH_SMSG_AUTH_GSSAPI_HASH);
297                 packet_put_string((char *)msg_tok.value,msg_tok.length);
298                 packet_send();
299                 packet_write_wait();
300                 gss_release_buffer(&min_status,&msg_tok);
301         }
302
303         /*
304          * We don't need to check the status, because the stored credentials
305          * which userok uses are only populated once the context init step
306          * has returned complete.
307          */
308
309         packet_check_eom();
310
311         authenticated = PRIVSEP(ssh_gssapi_userok(authctxt->user));
312
313 finish:
314         authctxt->postponed = 0;
315         dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
316         dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, NULL);
317         dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, NULL);
318         userauth_finish(authctxt, authenticated, "gssapi");
319 }
320
321 static void ssh_gssapi_userauth_error(Gssctxt *ctxt) {
322         char *errstr;
323         OM_uint32 maj,min;
324         
325         errstr=PRIVSEP(ssh_gssapi_last_error(ctxt,&maj,&min));
326         if (errstr) {
327             if (!compat20) {
328                 packet_send_debug(errstr);
329             } else {
330                 packet_start(SSH2_MSG_USERAUTH_GSSAPI_ERROR);
331                 packet_put_int(maj);
332                 packet_put_int(min);
333                 packet_put_cstring(errstr);
334                 packet_put_cstring("");
335                 packet_send();
336                 packet_write_wait();
337                 xfree(errstr);
338             }
339         }
340 }
341
342 Authmethod method_external = {
343         "external-keyx",
344         userauth_external,
345         &options.gss_authentication
346 };
347         
348 Authmethod method_gssapi = {
349         "gssapi",
350         userauth_gssapi,
351         &options.gss_authentication
352 };
353
354 #endif /* GSSAPI */
This page took 0.061168 seconds and 5 git commands to generate.