]> andersk Git - gssapi-openssh.git/blob - openssh/auth2-gss.c
o Remove compat package from bundle
[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 "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_exchange_complete(int type, u_int32_t plen, void *ctxt);
56 static void input_gssapi_errtok(int, u_int32_t, void *);
57
58 /*
59  * We only support those mechanisms that we know about (ie ones that we know
60  * how to check local user kuserok and the like
61  */
62 static int
63 userauth_gssapi(Authctxt *authctxt)
64 {
65         gss_OID_desc oid = {0, NULL};
66         Gssctxt *ctxt = NULL;
67         int mechs;
68         gss_OID_set supported;
69         int present;
70         OM_uint32 ms;
71         u_int len;
72         char *doid = NULL;
73
74         if (!authctxt->valid || authctxt->user == NULL)
75                 return (0);
76
77         mechs = packet_get_int();
78         if (mechs == 0) {
79                 debug("Mechanism negotiation is not supported");
80                 return (0);
81         }
82
83         ssh_gssapi_supported_oids(&supported);
84         do {
85                 mechs--;
86
87                 if (doid)
88                         xfree(doid);
89
90                 doid = packet_get_string(&len);
91
92                 if (doid[0] != SSH_GSS_OIDTYPE || doid[1] != len-2) {
93                         logit("Mechanism OID received using the old encoding form");
94                         oid.elements = doid;
95                         oid.length = len;
96                 } else {
97                         oid.elements = doid + 2;
98                         oid.length   = len - 2;
99                 }
100                 gss_test_oid_set_member(&ms, &oid, supported, &present);
101         } while (mechs > 0 && !present);
102
103         gss_release_oid_set(&ms, &supported);
104
105         if (!present) {
106                 xfree(doid);
107                 return (0);
108         }
109
110         if (GSS_ERROR(PRIVSEP(ssh_gssapi_server_ctx(&ctxt, &oid)))) {
111                 xfree(doid);
112                 return (0);
113         }
114
115         authctxt->methoddata=(void *)ctxt;
116
117         packet_start(SSH2_MSG_USERAUTH_GSSAPI_RESPONSE);
118
119         /* Return OID in same format as we received it*/
120         packet_put_string(doid, len);
121
122         packet_send();
123         xfree(doid);
124
125         dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, &input_gssapi_token);
126         dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, &input_gssapi_errtok);
127         authctxt->postponed = 1;
128
129         return (0);
130 }
131
132 static void
133 input_gssapi_token(int type, u_int32_t plen, void *ctxt)
134 {
135         Authctxt *authctxt = ctxt;
136         Gssctxt *gssctxt;
137         gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
138         gss_buffer_desc recv_tok;
139         OM_uint32 maj_status, min_status;
140         u_int len;
141
142         if (authctxt == NULL || (authctxt->methoddata == NULL && !use_privsep))
143                 fatal("No authentication or GSSAPI context");
144
145         gssctxt = authctxt->methoddata;
146         recv_tok.value = packet_get_string(&len);
147         recv_tok.length = len; /* u_int vs. size_t */
148
149         packet_check_eom();
150
151         maj_status = PRIVSEP(ssh_gssapi_accept_ctx(gssctxt, &recv_tok,
152             &send_tok, NULL));
153
154         xfree(recv_tok.value);
155
156         if (GSS_ERROR(maj_status)) {
157                 ssh_gssapi_userauth_error(gssctxt);
158                 if (send_tok.length != 0) {
159                         packet_start(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK);
160                         packet_put_string(send_tok.value, send_tok.length);
161                         packet_send();
162                 }
163                 authctxt->postponed = 0;
164                 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
165                 userauth_finish(authctxt, 0, "gssapi");
166         } else {
167                 if (send_tok.length != 0) {
168                         packet_start(SSH2_MSG_USERAUTH_GSSAPI_TOKEN);
169                         packet_put_string(send_tok.value, send_tok.length);
170                         packet_send();
171                 }
172                 if (maj_status == GSS_S_COMPLETE) {
173                         dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
174                         dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE,
175                                      &input_gssapi_exchange_complete);
176                 }
177         }
178
179         gss_release_buffer(&min_status, &send_tok);
180 }
181
182 static void
183 input_gssapi_errtok(int type, u_int32_t plen, void *ctxt)
184 {
185         Authctxt *authctxt = ctxt;
186         Gssctxt *gssctxt;
187         gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
188         gss_buffer_desc recv_tok;
189         OM_uint32 maj_status;
190         u_int len;
191
192         if (authctxt == NULL || (authctxt->methoddata == NULL && !use_privsep))
193                 fatal("No authentication or GSSAPI context");
194
195         gssctxt = authctxt->methoddata;
196         recv_tok.value = packet_get_string(&len);
197         recv_tok.length = len;
198
199         packet_check_eom();
200
201         /* Push the error token into GSSAPI to see what it says */
202         maj_status = PRIVSEP(ssh_gssapi_accept_ctx(gssctxt, &recv_tok,
203             &send_tok, NULL));
204
205         xfree(recv_tok.value);
206
207         /* We can't return anything to the client, even if we wanted to */
208         dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
209         dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, NULL);
210
211         /* The client will have already moved on to the next auth */
212
213         gss_release_buffer(&maj_status, &send_tok);
214 }
215
216 /*
217  * This is called when the client thinks we've completed authentication.
218  * It should only be enabled in the dispatch handler by the function above,
219  * which only enables it once the GSSAPI exchange is complete.
220  */
221
222 static void
223 input_gssapi_exchange_complete(int type, u_int32_t plen, void *ctxt)
224 {
225         Authctxt *authctxt = ctxt;
226         Gssctxt *gssctxt;
227         int authenticated;
228
229         if (authctxt == NULL || (authctxt->methoddata == NULL && !use_privsep))
230                 fatal("No authentication or GSSAPI context");
231
232         if ((strcmp(authctxt->user, "") == 0) && (authctxt->pw == NULL)) {
233             char *lname = NULL;
234             PRIVSEP(ssh_gssapi_localname(&lname));
235             if (lname && lname[0] != '\0') {
236                 xfree(authctxt->user);
237                 authctxt->user = lname;
238                 debug("set username to %s from gssapi context", lname);
239                 authctxt->pw = PRIVSEP(getpwnamallow(authctxt->user));
240             } else {
241                 debug("failed to set username from gssapi context");
242             }
243         }
244         if (authctxt->pw) {
245 #ifdef USE_PAM
246             PRIVSEP(start_pam(authctxt->pw->pw_name));
247 #endif
248         } else {
249             authctxt->valid = 0;
250             authenticated = 0;
251             goto finish;
252         }
253
254         gssctxt = authctxt->methoddata;
255
256         /*
257          * We don't need to check the status, because the stored credentials
258          * which userok uses are only populated once the context init step
259          * has returned complete.
260          */
261
262         packet_check_eom();
263
264         authenticated = PRIVSEP(ssh_gssapi_userok(authctxt->user));
265
266 finish:
267         authctxt->postponed = 0;
268         dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
269         dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, NULL);
270         dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, NULL);
271         userauth_finish(authctxt, authenticated, "gssapi");
272 }
273
274 static void ssh_gssapi_userauth_error(Gssctxt *ctxt) {
275         char *errstr;
276         OM_uint32 maj,min;
277         
278         errstr=PRIVSEP(ssh_gssapi_last_error(ctxt,&maj,&min));
279         if (errstr) {
280                 packet_start(SSH2_MSG_USERAUTH_GSSAPI_ERROR);
281                 packet_put_int(maj);
282                 packet_put_int(min);
283                 packet_put_cstring(errstr);
284                 packet_put_cstring("");
285                 packet_send();
286                 packet_write_wait();
287                 xfree(errstr);
288         }
289 }
290
291 Authmethod method_external = {
292         "external-keyx",
293         userauth_external,
294         &options.gss_authentication
295 };
296         
297 Authmethod method_gssapi = {
298         "gssapi",
299         userauth_gssapi,
300         &options.gss_authentication
301 };
302
303 #endif /* GSSAPI */
This page took 0.134807 seconds and 5 git commands to generate.