2 * Kerberos v5 authentication and ticket-passing routines.
4 * $FreeBSD: src/crypto/openssh/auth-krb5.c,v 1.6 2001/02/13 16:58:04 assar Exp $
8 RCSID("$OpenBSD: auth-krb5.c,v 1.6 2002/03/04 17:27:39 stevesk Exp $");
22 extern ServerOptions options;
25 krb5_init(void *context)
27 Authctxt *authctxt = (Authctxt *)context;
28 krb5_error_code problem;
29 static int cleanup_registered = 0;
31 if (authctxt->krb5_ctx == NULL) {
32 problem = krb5_init_context(&authctxt->krb5_ctx);
35 krb5_init_ets(authctxt->krb5_ctx);
37 if (!cleanup_registered) {
38 fatal_add_cleanup(krb5_cleanup_proc, authctxt);
39 cleanup_registered = 1;
45 * Try krb5 authentication. server_user is passed for logging purposes
46 * only, in auth is received ticket, in client is returned principal
50 auth_krb5(Authctxt *authctxt, krb5_data *auth, char **client)
52 krb5_error_code problem;
53 krb5_principal server;
63 problem = krb5_init(authctxt);
67 problem = krb5_auth_con_init(authctxt->krb5_ctx,
68 &authctxt->krb5_auth_ctx);
72 fd = packet_get_connection_in();
73 problem = krb5_auth_con_setaddrs_from_fd(authctxt->krb5_ctx,
74 authctxt->krb5_auth_ctx, &fd);
78 problem = krb5_sname_to_principal(authctxt->krb5_ctx, NULL, NULL ,
79 KRB5_NT_SRV_HST, &server);
83 problem = krb5_rd_req(authctxt->krb5_ctx, &authctxt->krb5_auth_ctx,
84 auth, server, NULL, NULL, &ticket);
88 problem = krb5_copy_principal(authctxt->krb5_ctx, ticket->client,
89 &authctxt->krb5_user);
93 /* if client wants mutual auth */
94 problem = krb5_mk_rep(authctxt->krb5_ctx, authctxt->krb5_auth_ctx,
99 /* Check .k5login authorization now. */
100 if (!krb5_kuserok(authctxt->krb5_ctx, authctxt->krb5_user,
101 authctxt->pw->pw_name))
105 krb5_unparse_name(authctxt->krb5_ctx, authctxt->krb5_user,
108 packet_start(SSH_SMSG_AUTH_KERBEROS_RESPONSE);
109 packet_put_string((char *) reply.data, reply.length);
116 krb5_free_principal(authctxt->krb5_ctx, server);
118 krb5_free_ticket(authctxt->krb5_ctx, ticket);
123 if (authctxt->krb5_ctx != NULL)
124 debug("Kerberos v5 authentication failed: %s",
125 krb5_get_err_text(authctxt->krb5_ctx, problem));
127 debug("Kerberos v5 authentication failed: %d",
135 auth_krb5_tgt(Authctxt *authctxt, krb5_data *tgt)
137 krb5_error_code problem;
138 krb5_ccache ccache = NULL;
141 if (authctxt->pw == NULL || authctxt->krb5_user == NULL)
144 temporarily_use_uid(authctxt->pw);
146 problem = krb5_cc_gen_new(authctxt->krb5_ctx, &krb5_fcc_ops, &ccache);
150 problem = krb5_cc_initialize(authctxt->krb5_ctx, ccache,
151 authctxt->krb5_user);
155 problem = krb5_rd_cred2(authctxt->krb5_ctx, authctxt->krb5_auth_ctx,
160 authctxt->krb5_fwd_ccache = ccache;
163 authctxt->krb5_ticket_file = (char *)krb5_cc_get_name(authctxt->krb5_ctx, authctxt->krb5_fwd_ccache);
165 problem = krb5_unparse_name(authctxt->krb5_ctx, authctxt->krb5_user,
170 debug("Kerberos v5 TGT accepted (%s)", pname);
178 debug("Kerberos v5 TGT passing failed: %s",
179 krb5_get_err_text(authctxt->krb5_ctx, problem));
181 krb5_cc_destroy(authctxt->krb5_ctx, ccache);
189 auth_krb5_password(Authctxt *authctxt, const char *password)
191 krb5_error_code problem;
193 if (authctxt->pw == NULL)
196 temporarily_use_uid(authctxt->pw);
198 problem = krb5_init(authctxt);
202 problem = krb5_parse_name(authctxt->krb5_ctx, authctxt->pw->pw_name,
203 &authctxt->krb5_user);
207 problem = krb5_cc_gen_new(authctxt->krb5_ctx, &krb5_mcc_ops,
208 &authctxt->krb5_fwd_ccache);
212 problem = krb5_cc_initialize(authctxt->krb5_ctx,
213 authctxt->krb5_fwd_ccache, authctxt->krb5_user);
218 problem = krb5_verify_user(authctxt->krb5_ctx, authctxt->krb5_user,
219 authctxt->krb5_fwd_ccache, password, 1, NULL);
220 temporarily_use_uid(authctxt->pw);
225 authctxt->krb5_ticket_file = (char *)krb5_cc_get_name(authctxt->krb5_ctx, authctxt->krb5_fwd_ccache);
231 if (authctxt->krb5_ctx != NULL)
232 debug("Kerberos password authentication failed: %s",
233 krb5_get_err_text(authctxt->krb5_ctx, problem));
235 debug("Kerberos password authentication failed: %d",
238 krb5_cleanup_proc(authctxt);
240 if (options.kerberos_or_local_passwd)
249 krb5_cleanup_proc(void *context)
251 Authctxt *authctxt = (Authctxt *)context;
253 debug("krb5_cleanup_proc called");
254 if (authctxt->krb5_fwd_ccache) {
255 krb5_cc_destroy(authctxt->krb5_ctx, authctxt->krb5_fwd_ccache);
256 authctxt->krb5_fwd_ccache = NULL;
258 if (authctxt->krb5_user) {
259 krb5_free_principal(authctxt->krb5_ctx, authctxt->krb5_user);
260 authctxt->krb5_user = NULL;
262 if (authctxt->krb5_auth_ctx) {
263 krb5_auth_con_free(authctxt->krb5_ctx,
264 authctxt->krb5_auth_ctx);
265 authctxt->krb5_auth_ctx = NULL;
267 if (authctxt->krb5_ctx) {
268 krb5_free_context(authctxt->krb5_ctx);
269 authctxt->krb5_ctx = NULL;