]> andersk Git - gssapi-openssh.git/blob - openssh/auth2-gss.c
openssh-3.6.1p2-gssapi-20030430.diff from Simon
[gssapi-openssh.git] / openssh / auth2-gss.c
1 /*
2  * Copyright (c) 2001-2003 Simon Wilkinson. 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
27 #ifdef GSSAPI
28 #include "auth.h"
29 #include "ssh2.h"
30 #include "xmalloc.h"
31 #include "log.h"
32 #include "dispatch.h"
33 #include "servconf.h"
34 #include "compat.h"
35 #include "packet.h"
36 #include "monitor_wrap.h"
37
38 #include "ssh-gss.h"
39
40 extern ServerOptions options;
41
42 static int
43 userauth_external(Authctxt *authctxt)
44 {
45         packet_check_eom();
46
47         return(PRIVSEP(ssh_gssapi_userok(authctxt->user)));
48 }
49
50 static void ssh_gssapi_userauth_error(Gssctxt *ctxt);
51 static void input_gssapi_token(int type, u_int32_t plen, void *ctxt);
52 static void input_gssapi_exchange_complete(int type, u_int32_t plen, void *ctxt);
53 static void input_gssapi_errtok(int, u_int32_t, void *);
54
55 /* We only support those mechanisms that we know about (ie ones that we know
56  * how to check local user kuserok and the like
57  */
58 static int
59 userauth_gssapi(Authctxt *authctxt)
60 {
61         gss_OID_desc    oid= {0,NULL};
62         Gssctxt         *ctxt = NULL;
63         int             mechs;
64         gss_OID_set     supported;
65         int             present;
66         OM_uint32       ms;
67         u_int           len;
68         char *          doid = NULL;
69         
70         if (!authctxt->valid || authctxt->user == NULL)
71                 return 0;
72                 
73         if (datafellows & SSH_OLD_GSSAPI) {
74                 debug("Early drafts of GSSAPI userauth not supported");
75                 return 0;
76         }
77         
78         mechs=packet_get_int();
79         if (mechs==0) {
80                 debug("Mechanism negotiation is not supported");
81                 return 0;
82         }
83
84         ssh_gssapi_supported_oids(&supported);
85         do {
86                 mechs--;
87                 
88                 if (doid)
89                         xfree(doid);
90                 
91                 debug("Trying to get OID string");
92                 doid = packet_get_string(&len);
93                 debug("Got string");
94                 
95                 if (doid[0]!=0x06 || doid[1]!=len-2) {
96                         log("Mechanism OID received using the old encoding form");
97                         oid.elements = doid;
98                         oid.length = len;
99                 } else {
100                         oid.elements = doid + 2;
101                         oid.length   = len - 2;
102                 }
103                 gss_test_oid_set_member(&ms, &oid, supported, &present);
104         } while (mechs>0 && !present);
105         
106         if (!present) {
107                 xfree(doid);
108                 return(0);
109         }
110                 
111         if (GSS_ERROR(PRIVSEP(ssh_gssapi_server_ctx(&ctxt,&oid)))) {
112                 ssh_gssapi_userauth_error(ctxt);
113                 return(0);
114         }
115         
116         authctxt->methoddata=(void *)ctxt;
117
118         /* Send SSH_MSG_USERAUTH_GSSAPI_RESPONSE */
119
120         packet_start(SSH2_MSG_USERAUTH_GSSAPI_RESPONSE);
121
122         /* Just return whatever they sent */
123         packet_put_string(doid,len);
124         
125         packet_send();
126         packet_write_wait();
127         xfree(doid);
128
129         dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, 
130                      &input_gssapi_token);
131         dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK,
132                      &input_gssapi_errtok);
133         authctxt->postponed = 1;
134         
135         return 0;
136 }
137
138 static void
139 input_gssapi_token(int type, u_int32_t plen, void *ctxt)
140 {
141         Authctxt *authctxt = ctxt;
142         Gssctxt *gssctxt;
143         gss_buffer_desc send_tok,recv_tok;
144         OM_uint32 maj_status, min_status;
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         maj_status=PRIVSEP(ssh_gssapi_accept_ctx(gssctxt, &recv_tok, 
155                                                  &send_tok, NULL));
156         packet_check_eom();
157         
158         if (GSS_ERROR(maj_status)) {
159                 ssh_gssapi_userauth_error(gssctxt);
160                 if (send_tok.length != 0) {
161                         packet_start(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK);
162                         packet_put_string(send_tok.value,send_tok.length);
163                         packet_send();
164                         packet_write_wait();
165                 }
166                 authctxt->postponed = 0;
167                 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
168                 userauth_finish(authctxt, 0, "gssapi");
169         } else {
170                 if (send_tok.length != 0) {
171                         packet_start(SSH2_MSG_USERAUTH_GSSAPI_TOKEN);
172                         packet_put_string(send_tok.value,send_tok.length);
173                         packet_send();
174                         packet_write_wait();
175                 }
176                 if (maj_status == GSS_S_COMPLETE) {
177                         dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN,NULL);
178                         dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE,
179                                      &input_gssapi_exchange_complete);
180                 }
181         }
182         
183         gss_release_buffer(&min_status, &send_tok);        
184 }
185
186 static void
187 input_gssapi_errtok(int type, u_int32_t plen, void *ctxt)
188 {
189         Authctxt *authctxt = ctxt;
190         Gssctxt *gssctxt;
191         gss_buffer_desc send_tok,recv_tok;
192         OM_uint32 maj_status;
193         
194         if (authctxt == NULL || (authctxt->methoddata == NULL && !use_privsep))
195                 fatal("No authentication or GSSAPI context");
196                 
197         gssctxt=authctxt->methoddata;
198         recv_tok.value=packet_get_string(&recv_tok.length);
199         
200         /* Push the error token into GSSAPI to see what it says */
201         maj_status=PRIVSEP(ssh_gssapi_accept_ctx(gssctxt, &recv_tok, 
202                                                  &send_tok, NULL));
203         packet_check_eom();
204
205         /* We can't return anything to the client, even if we wanted to */
206         dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
207         dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK,NULL);
208
209         /* The client will have already moved on to the next auth */
210         
211 }
212
213 /* This is called when the client thinks we've completed authentication.
214  * It should only be enabled in the dispatch handler by the function above,
215  * which only enables it once the GSSAPI exchange is complete.
216  */
217  
218 static void
219 input_gssapi_exchange_complete(int type, u_int32_t plen, void *ctxt)
220 {
221         Authctxt *authctxt = ctxt;
222         Gssctxt *gssctxt;
223         int authenticated;
224         
225         if (authctxt == NULL || (authctxt->methoddata == NULL && !use_privsep))
226                 fatal("No authentication or GSSAPI context");
227                 
228         gssctxt=authctxt->methoddata;
229         
230         /* We don't need to check the status, because the stored credentials
231          * which userok uses are only populated once the context init step
232          * has returned complete.
233          */
234
235         authenticated = PRIVSEP(ssh_gssapi_userok(authctxt->user));
236
237         authctxt->postponed = 0;
238         dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
239         dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, NULL);
240         dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, NULL);
241         userauth_finish(authctxt, authenticated, "gssapi");
242 }
243
244 static void ssh_gssapi_userauth_error(Gssctxt *ctxt) {
245         char *errstr;
246         OM_uint32 maj,min;
247         
248         errstr=PRIVSEP(ssh_gssapi_last_error(ctxt,&maj,&min));
249         if (errstr) {
250                 packet_start(SSH2_MSG_USERAUTH_GSSAPI_ERROR);
251                 packet_put_int(maj);
252                 packet_put_int(min);
253                 packet_put_cstring(errstr);
254                 packet_put_cstring("");
255                 packet_send();
256                 packet_write_wait();
257                 xfree(errstr);
258         }
259 }
260
261 Authmethod method_external = {
262         "external-keyx",
263         userauth_external,
264         &options.gss_authentication
265 };
266         
267 Authmethod method_gssapi = {
268         "gssapi",
269         userauth_gssapi,
270         &options.gss_authentication
271 };
272
273 #endif /* GSSAPI */
This page took 0.058799 seconds and 5 git commands to generate.