]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - crypto/openssh/auth-krb5.c
Resolve conflicts and remove obsolete files.
[FreeBSD/FreeBSD.git] / crypto / openssh / auth-krb5.c
1 /*
2  *    Kerberos v5 authentication and ticket-passing routines.
3  *
4  * $xFreeBSD: src/crypto/openssh/auth-krb5.c,v 1.6 2001/02/13 16:58:04 assar Exp$
5  */
6 /*
7  * Copyright (c) 2002 Daniel Kouril.  All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29
30 #include "includes.h"
31 RCSID("$OpenBSD: auth-krb5.c,v 1.12 2003/08/28 12:54:34 markus Exp $");
32 RCSID("$FreeBSD$");
33
34 #include "ssh.h"
35 #include "ssh1.h"
36 #include "packet.h"
37 #include "xmalloc.h"
38 #include "log.h"
39 #include "servconf.h"
40 #include "uidswap.h"
41 #include "auth.h"
42
43 #ifdef KRB5
44
45 #include <krb5.h>
46
47 extern ServerOptions     options;
48
49 static int
50 krb5_init(void *context)
51 {
52         Authctxt *authctxt = (Authctxt *)context;
53         krb5_error_code problem;
54         static int cleanup_registered = 0;
55
56         if (authctxt->krb5_ctx == NULL) {
57                 problem = krb5_init_context(&authctxt->krb5_ctx);
58                 if (problem)
59                         return (problem);
60                 krb5_init_ets(authctxt->krb5_ctx);
61         }
62         if (!cleanup_registered) {
63                 fatal_add_cleanup(krb5_cleanup_proc, authctxt);
64                 cleanup_registered = 1;
65         }
66         return (0);
67 }
68
69 int
70 auth_krb5_password(Authctxt *authctxt, const char *password)
71 {
72 #ifndef HEIMDAL
73         krb5_creds creds;
74         krb5_principal server;
75         char ccname[40];
76         int tmpfd;
77 #endif  
78         krb5_error_code problem;
79         krb5_ccache ccache = NULL;
80
81         if (authctxt->pw == NULL)
82                 return (0);
83
84         temporarily_use_uid(authctxt->pw);
85
86         problem = krb5_init(authctxt);
87         if (problem)
88                 goto out;
89
90         problem = krb5_parse_name(authctxt->krb5_ctx, authctxt->pw->pw_name,
91                     &authctxt->krb5_user);
92         if (problem)
93                 goto out;
94
95 #ifdef HEIMDAL
96         problem = krb5_cc_gen_new(authctxt->krb5_ctx, &krb5_mcc_ops, &ccache);
97         if (problem)
98                 goto out;
99
100         problem = krb5_cc_initialize(authctxt->krb5_ctx, ccache,
101                 authctxt->krb5_user);
102         if (problem)
103                 goto out;
104
105         restore_uid();
106         
107         problem = krb5_verify_user(authctxt->krb5_ctx, authctxt->krb5_user,
108             ccache, password, 1, NULL);
109         
110         temporarily_use_uid(authctxt->pw);
111
112         if (problem)
113                 goto out;
114         problem = krb5_cc_gen_new(authctxt->krb5_ctx, &krb5_fcc_ops,
115             &authctxt->krb5_fwd_ccache);
116         if (problem)
117                 goto out;
118
119         problem = krb5_cc_copy_cache(authctxt->krb5_ctx, ccache,
120             authctxt->krb5_fwd_ccache);
121         krb5_cc_destroy(authctxt->krb5_ctx, ccache);
122         ccache = NULL;
123         if (problem)
124                 goto out;
125
126 #else
127         problem = krb5_get_init_creds_password(authctxt->krb5_ctx, &creds,
128             authctxt->krb5_user, (char *)password, NULL, NULL, 0, NULL, NULL);
129         if (problem)
130                 goto out;
131
132         problem = krb5_sname_to_principal(authctxt->krb5_ctx, NULL, NULL,
133             KRB5_NT_SRV_HST, &server);
134         if (problem)
135                 goto out;
136
137         restore_uid();
138         problem = krb5_verify_init_creds(authctxt->krb5_ctx, &creds, server,
139             NULL, NULL, NULL);
140         krb5_free_principal(authctxt->krb5_ctx, server);
141         temporarily_use_uid(authctxt->pw);
142         if (problem)
143                 goto out;
144         
145         if (!krb5_kuserok(authctxt->krb5_ctx, authctxt->krb5_user, 
146                           authctxt->pw->pw_name)) {
147                 problem = -1;
148                 goto out;
149         } 
150
151         snprintf(ccname,sizeof(ccname),"FILE:/tmp/krb5cc_%d_XXXXXX",geteuid());
152         
153         if ((tmpfd = mkstemp(ccname+strlen("FILE:")))==-1) {
154                 logit("mkstemp(): %.100s", strerror(errno));
155                 problem = errno;
156                 goto out;
157         }
158         
159         if (fchmod(tmpfd,S_IRUSR | S_IWUSR) == -1) {
160                 logit("fchmod(): %.100s", strerror(errno));
161                 close(tmpfd);
162                 problem = errno;
163                 goto out;
164         }
165         close(tmpfd);
166
167         problem = krb5_cc_resolve(authctxt->krb5_ctx, ccname, &authctxt->krb5_fwd_ccache);
168         if (problem)
169                 goto out;
170
171         problem = krb5_cc_initialize(authctxt->krb5_ctx, authctxt->krb5_fwd_ccache,
172                                      authctxt->krb5_user);
173         if (problem)
174                 goto out;
175                                 
176         problem= krb5_cc_store_cred(authctxt->krb5_ctx, authctxt->krb5_fwd_ccache,
177                                  &creds);
178         if (problem)
179                 goto out;
180 #endif          
181
182         authctxt->krb5_ticket_file = (char *)krb5_cc_get_name(authctxt->krb5_ctx, authctxt->krb5_fwd_ccache);
183
184  out:
185         restore_uid();
186
187         if (problem) {
188                 if (ccache)
189                         krb5_cc_destroy(authctxt->krb5_ctx, ccache);
190
191                 if (authctxt->krb5_ctx != NULL && problem!=-1)
192                         debug("Kerberos password authentication failed: %s",
193                             krb5_get_err_text(authctxt->krb5_ctx, problem));
194                 else
195                         debug("Kerberos password authentication failed: %d",
196                             problem);
197
198                 krb5_cleanup_proc(authctxt);
199
200                 if (options.kerberos_or_local_passwd)
201                         return (-1);
202                 else
203                         return (0);
204         }
205         return (1);
206 }
207
208 void
209 krb5_cleanup_proc(void *context)
210 {
211         Authctxt *authctxt = (Authctxt *)context;
212
213         debug("krb5_cleanup_proc called");
214         if (authctxt->krb5_fwd_ccache) {
215                 krb5_cc_destroy(authctxt->krb5_ctx, authctxt->krb5_fwd_ccache);
216                 authctxt->krb5_fwd_ccache = NULL;
217         }
218         if (authctxt->krb5_user) {
219                 krb5_free_principal(authctxt->krb5_ctx, authctxt->krb5_user);
220                 authctxt->krb5_user = NULL;
221         }
222         if (authctxt->krb5_ctx) {
223                 krb5_free_context(authctxt->krb5_ctx);
224                 authctxt->krb5_ctx = NULL;
225         }
226 }
227
228 #endif /* KRB5 */