]> andersk Git - gssapi-openssh.git/blob - openssh/auth-krb5.c
Fixes for krb5 support in OpenSSH 3.0.2p1 by Simon Wilkinson from
[gssapi-openssh.git] / openssh / auth-krb5.c
1 /*
2  *    Kerberos v5 authentication and ticket-passing routines.
3  * 
4  * $FreeBSD: src/crypto/openssh/auth-krb5.c,v 1.6 2001/02/13 16:58:04 assar Exp $
5  * $OpenBSD: auth-krb5.c,v 1.2 2001/11/12 01:47:09 dugsong Exp $
6  */
7
8 #include "includes.h"
9 #include "ssh.h"
10 #include "ssh1.h"
11 #include "packet.h"
12 #include "xmalloc.h"
13 #include "log.h"
14 #include "servconf.h"
15 #include "uidswap.h"
16 #include "auth.h"
17
18 #ifdef KRB5
19 #include <krb5.h>
20 #ifndef HEIMDAL
21 #define krb5_get_err_text(context,code) error_message(code)
22 #endif /* !HEIMDAL */
23
24 extern ServerOptions     options;
25
26 static int
27 krb5_init(void *context)
28 {
29         Authctxt *authctxt = (Authctxt *)context;
30         krb5_error_code problem;
31         static int cleanup_registered = 0;
32         
33         if (authctxt->krb5_ctx == NULL) {
34                 problem = krb5_init_context(&authctxt->krb5_ctx);
35                 if (problem)
36                         return (problem);
37                 krb5_init_ets(authctxt->krb5_ctx);
38         }
39         if (!cleanup_registered) {
40                 fatal_add_cleanup(krb5_cleanup_proc, authctxt);
41                 cleanup_registered = 1;
42         }
43         return (0);
44 }
45
46 /*
47  * Try krb5 authentication. server_user is passed for logging purposes
48  * only, in auth is received ticket, in client is returned principal
49  * from the ticket
50  */
51 int 
52 auth_krb5(Authctxt *authctxt, krb5_data *auth, char **client)
53 {
54         krb5_error_code problem;
55         krb5_principal server;
56         krb5_data reply;
57         krb5_ticket *ticket;
58         int fd, ret;
59
60         ret = 0;
61         server = NULL;
62         ticket = NULL;
63         reply.length = 0;
64         
65         problem = krb5_init(authctxt);
66         if (problem) 
67                 goto err;
68         
69         problem = krb5_auth_con_init(authctxt->krb5_ctx,
70             &authctxt->krb5_auth_ctx);
71         if (problem)
72                 goto err;
73         
74         fd = packet_get_connection_in();
75 #ifdef HEIMDAL
76         problem = krb5_auth_con_setaddrs_from_fd(authctxt->krb5_ctx,
77             authctxt->krb5_auth_ctx, &fd);
78 #else
79         problem = krb5_auth_con_genaddrs(authctxt->krb5_ctx, 
80             authctxt->krb5_auth_ctx,fd,
81             KRB5_AUTH_CONTEXT_GENERATE_REMOTE_FULL_ADDR |
82             KRB5_AUTH_CONTEXT_GENERATE_LOCAL_FULL_ADDR);
83 #endif
84         if (problem)
85                 goto err;
86         
87         problem = krb5_sname_to_principal(authctxt->krb5_ctx,  NULL, NULL ,
88             KRB5_NT_SRV_HST, &server);
89         if (problem)
90                 goto err;
91         
92         problem = krb5_rd_req(authctxt->krb5_ctx, &authctxt->krb5_auth_ctx,
93             auth, server, NULL, NULL, &ticket);
94         if (problem)
95                 goto err;
96         
97 #ifdef HEIMDAL  
98         problem = krb5_copy_principal(authctxt->krb5_ctx, ticket->client,
99             &authctxt->krb5_user);
100 #else
101         problem = krb5_copy_principal(authctxt->krb5_ctx, ticket->enc_part2->client,
102             &authctxt->krb5_user);
103 #endif
104         if (problem)
105                 goto err;
106         
107         /* if client wants mutual auth */
108         problem = krb5_mk_rep(authctxt->krb5_ctx, authctxt->krb5_auth_ctx,
109             &reply);
110         if (problem)
111                 goto err;
112         
113         /* Check .k5login authorization now. */
114         if (!krb5_kuserok(authctxt->krb5_ctx, authctxt->krb5_user,
115             authctxt->pw->pw_name))
116                 goto err;
117         
118         if (client)
119                 krb5_unparse_name(authctxt->krb5_ctx, authctxt->krb5_user,
120                     client);
121         
122         packet_start(SSH_SMSG_AUTH_KERBEROS_RESPONSE);
123         packet_put_string((char *) reply.data, reply.length);
124         packet_send();
125         packet_write_wait();
126
127         ret = 1;
128  err:
129         if (server)
130                 krb5_free_principal(authctxt->krb5_ctx, server);
131         if (ticket)
132                 krb5_free_ticket(authctxt->krb5_ctx, ticket);
133         if (reply.length)
134                 xfree(reply.data);
135         
136         if (problem)
137                 debug("Kerberos v5 authentication failed: %s",
138                     krb5_get_err_text(authctxt->krb5_ctx, problem));
139
140         return (ret);
141 }
142
143 int
144 auth_krb5_tgt(Authctxt *authctxt, krb5_data *tgt)
145 {
146         krb5_error_code problem;
147         krb5_ccache ccache = NULL;
148         char *pname;
149         krb5_creds **creds;
150         
151         if (authctxt->pw == NULL || authctxt->krb5_user == NULL)
152                 return (0);
153         
154         temporarily_use_uid(authctxt->pw);
155         
156 #ifdef HEIMDAL  
157         problem = krb5_cc_gen_new(authctxt->krb5_ctx, &krb5_fcc_ops, &ccache);
158 #else
159 {
160         char ccname[35];
161         
162         snprintf(ccname, sizeof(ccname), "FILE:/tmp/krb5cc_%d", authctxt->pw->pw_uid);
163         problem = krb5_cc_resolve(authctxt->krb5_ctx, ccname, &ccache);
164 }
165 #endif
166         if (problem)
167                 goto fail;
168         
169         problem = krb5_cc_initialize(authctxt->krb5_ctx, ccache,
170             authctxt->krb5_user);
171         if (problem)
172                 goto fail;
173         
174 #ifdef HEIMDAL  
175         problem = krb5_rd_cred2(authctxt->krb5_ctx, authctxt->krb5_auth_ctx,
176             ccache, tgt);
177         if (problem)
178                 goto fail;
179 #else
180         problem = krb5_rd_cred(authctxt->krb5_ctx, authctxt->krb5_auth_ctx,
181             tgt, &creds, NULL);
182         if (problem) 
183                 goto fail;
184         problem = krb5_cc_store_cred(authctxt->krb5_ctx, ccache, *creds);
185         if (problem)
186                 goto fail;
187 #endif
188         
189         authctxt->krb5_fwd_ccache = ccache;
190         ccache = NULL;
191         
192         authctxt->krb5_ticket_file = (char *)krb5_cc_get_name(authctxt->krb5_ctx, authctxt->krb5_fwd_ccache);
193         
194         problem = krb5_unparse_name(authctxt->krb5_ctx, authctxt->krb5_user,
195             &pname);
196         if (problem)
197                 goto fail;
198         
199         debug("Kerberos v5 TGT accepted (%s)", pname);
200         
201         restore_uid();
202         
203         return (1);
204         
205  fail:
206         if (problem)
207                 debug("Kerberos v5 TGT passing failed: %s",
208                     krb5_get_err_text(authctxt->krb5_ctx, problem));
209         if (ccache)
210                 krb5_cc_destroy(authctxt->krb5_ctx, ccache);
211         
212         restore_uid();
213         
214         return (0);
215 }
216
217 int
218 auth_krb5_password(Authctxt *authctxt, const char *password)
219 {
220 #ifndef HEIMDAL
221         krb5_creds creds;
222         krb5_principal server;
223 #endif  
224         krb5_error_code problem;
225         
226         if (authctxt->pw == NULL)
227                 return (0);
228         
229         temporarily_use_uid(authctxt->pw);
230         
231         problem = krb5_init(authctxt);
232         if (problem)
233                 goto out;
234         
235         problem = krb5_parse_name(authctxt->krb5_ctx, authctxt->pw->pw_name,
236                     &authctxt->krb5_user);
237         if (problem)
238                 goto out;
239         
240 #ifdef HEIMDAL  
241         problem = krb5_cc_gen_new(authctxt->krb5_ctx, &krb5_mcc_ops,
242             &authctxt->krb5_fwd_ccache);
243 #else
244         problem = krb5_cc_resolve(authctxt->krb5_ctx, "MEMORY:", 
245             &authctxt->krb5_fwd_ccache);
246 #endif
247         if (problem)
248                 goto out;
249         
250         problem = krb5_cc_initialize(authctxt->krb5_ctx,
251             authctxt->krb5_fwd_ccache, authctxt->krb5_user);
252         if (problem)
253                 goto out;
254         
255 #ifdef HEIMDAL  
256         problem = krb5_verify_user(authctxt->krb5_ctx, authctxt->krb5_user,
257             authctxt->krb5_fwd_ccache, password, 1, NULL);
258         if (problem)
259                 goto out;
260         
261 #else
262         problem = krb5_get_init_creds_password(authctxt->krb5_ctx, &creds, 
263             authctxt->krb5_user, password, NULL, NULL, 0, NULL, NULL);
264         if (problem)
265                 goto out;
266
267         problem = krb5_sname_to_principal(authctxt->krb5_ctx, NULL, NULL, 
268             KRB5_NT_SRV_HST, &server);
269         if (problem)
270                 goto out;
271
272         restore_uid();
273         problem = krb5_verify_init_creds(authctxt->krb5_ctx, &creds, server, NULL, NULL, 
274             NULL);
275         temporarily_use_uid(authctxt->pw);
276         
277         krb5_free_principal(authctxt->krb5_ctx, server);
278         if (problem)
279                 goto out;
280
281         problem = krb5_cc_store_cred(authctxt->krb5_ctx, authctxt->krb5_fwd_ccache, &creds);
282         if (problem)
283                 goto out;
284
285 #endif /* HEIMDAL */
286
287         authctxt->krb5_ticket_file = (char *)krb5_cc_get_name(authctxt->krb5_ctx, authctxt->krb5_fwd_ccache);
288         
289  out:
290         restore_uid();
291         
292         if (problem) {
293                 debug("Kerberos password authentication failed: %s",
294                     krb5_get_err_text(authctxt->krb5_ctx, problem));
295                 
296                 krb5_cleanup_proc(authctxt);
297                 
298                 if (options.kerberos_or_local_passwd)
299                         return (-1);
300                 else
301                         return (0);
302         }
303         return (1);
304 }
305
306 void
307 krb5_cleanup_proc(void *context)
308 {
309         Authctxt *authctxt = (Authctxt *)context;
310         
311         debug("krb5_cleanup_proc called");
312         if (authctxt->krb5_fwd_ccache) {
313                 krb5_cc_destroy(authctxt->krb5_ctx, authctxt->krb5_fwd_ccache);
314                 authctxt->krb5_fwd_ccache = NULL;
315         }
316         if (authctxt->krb5_user) {
317                 krb5_free_principal(authctxt->krb5_ctx, authctxt->krb5_user);
318                 authctxt->krb5_user = NULL;
319         }
320         if (authctxt->krb5_auth_ctx) {
321                 krb5_auth_con_free(authctxt->krb5_ctx,
322                     authctxt->krb5_auth_ctx);
323                 authctxt->krb5_auth_ctx = NULL;
324         }
325         if (authctxt->krb5_ctx) {
326                 krb5_free_context(authctxt->krb5_ctx);
327                 authctxt->krb5_ctx = NULL;
328         }
329 }
330
331 #endif /* KRB5 */
This page took 0.084914 seconds and 5 git commands to generate.