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 $
5 * $OpenBSD: auth-krb5.c,v 1.2 2001/11/12 01:47:09 dugsong Exp $
21 #define krb5_get_err_text(context,code) error_message(code)
24 extern ServerOptions options;
27 krb5_init(void *context)
29 Authctxt *authctxt = (Authctxt *)context;
30 krb5_error_code problem;
31 static int cleanup_registered = 0;
33 if (authctxt->krb5_ctx == NULL) {
34 problem = krb5_init_context(&authctxt->krb5_ctx);
37 krb5_init_ets(authctxt->krb5_ctx);
39 if (!cleanup_registered) {
40 fatal_add_cleanup(krb5_cleanup_proc, authctxt);
41 cleanup_registered = 1;
47 * Try krb5 authentication. server_user is passed for logging purposes
48 * only, in auth is received ticket, in client is returned principal
52 auth_krb5(Authctxt *authctxt, krb5_data *auth, char **client)
54 krb5_error_code problem;
55 krb5_principal server;
65 problem = krb5_init(authctxt);
69 problem = krb5_auth_con_init(authctxt->krb5_ctx,
70 &authctxt->krb5_auth_ctx);
74 fd = packet_get_connection_in();
76 problem = krb5_auth_con_setaddrs_from_fd(authctxt->krb5_ctx,
77 authctxt->krb5_auth_ctx, &fd);
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);
87 problem = krb5_sname_to_principal(authctxt->krb5_ctx, NULL, NULL ,
88 KRB5_NT_SRV_HST, &server);
92 problem = krb5_rd_req(authctxt->krb5_ctx, &authctxt->krb5_auth_ctx,
93 auth, server, NULL, NULL, &ticket);
98 problem = krb5_copy_principal(authctxt->krb5_ctx, ticket->client,
99 &authctxt->krb5_user);
101 problem = krb5_copy_principal(authctxt->krb5_ctx, ticket->enc_part2->client,
102 &authctxt->krb5_user);
107 /* if client wants mutual auth */
108 problem = krb5_mk_rep(authctxt->krb5_ctx, authctxt->krb5_auth_ctx,
113 /* Check .k5login authorization now. */
114 if (!krb5_kuserok(authctxt->krb5_ctx, authctxt->krb5_user,
115 authctxt->pw->pw_name))
119 krb5_unparse_name(authctxt->krb5_ctx, authctxt->krb5_user,
122 packet_start(SSH_SMSG_AUTH_KERBEROS_RESPONSE);
123 packet_put_string((char *) reply.data, reply.length);
130 krb5_free_principal(authctxt->krb5_ctx, server);
132 krb5_free_ticket(authctxt->krb5_ctx, ticket);
137 debug("Kerberos v5 authentication failed: %s",
138 krb5_get_err_text(authctxt->krb5_ctx, problem));
144 auth_krb5_tgt(Authctxt *authctxt, krb5_data *tgt)
146 krb5_error_code problem;
147 krb5_ccache ccache = NULL;
151 if (authctxt->pw == NULL || authctxt->krb5_user == NULL)
154 temporarily_use_uid(authctxt->pw);
157 problem = krb5_cc_gen_new(authctxt->krb5_ctx, &krb5_fcc_ops, &ccache);
162 snprintf(ccname, sizeof(ccname), "FILE:/tmp/krb5cc_%d", authctxt->pw->pw_uid);
163 problem = krb5_cc_resolve(authctxt->krb5_ctx, ccname, &ccache);
169 problem = krb5_cc_initialize(authctxt->krb5_ctx, ccache,
170 authctxt->krb5_user);
175 problem = krb5_rd_cred2(authctxt->krb5_ctx, authctxt->krb5_auth_ctx,
180 problem = krb5_rd_cred(authctxt->krb5_ctx, authctxt->krb5_auth_ctx,
184 problem = krb5_cc_store_cred(authctxt->krb5_ctx, ccache, *creds);
189 authctxt->krb5_fwd_ccache = ccache;
192 authctxt->krb5_ticket_file = (char *)krb5_cc_get_name(authctxt->krb5_ctx, authctxt->krb5_fwd_ccache);
194 problem = krb5_unparse_name(authctxt->krb5_ctx, authctxt->krb5_user,
199 debug("Kerberos v5 TGT accepted (%s)", pname);
207 debug("Kerberos v5 TGT passing failed: %s",
208 krb5_get_err_text(authctxt->krb5_ctx, problem));
210 krb5_cc_destroy(authctxt->krb5_ctx, ccache);
218 auth_krb5_password(Authctxt *authctxt, const char *password)
222 krb5_principal server;
224 krb5_error_code problem;
226 if (authctxt->pw == NULL)
229 temporarily_use_uid(authctxt->pw);
231 problem = krb5_init(authctxt);
235 problem = krb5_parse_name(authctxt->krb5_ctx, authctxt->pw->pw_name,
236 &authctxt->krb5_user);
241 problem = krb5_cc_gen_new(authctxt->krb5_ctx, &krb5_mcc_ops,
242 &authctxt->krb5_fwd_ccache);
244 problem = krb5_cc_resolve(authctxt->krb5_ctx, "MEMORY:",
245 &authctxt->krb5_fwd_ccache);
250 problem = krb5_cc_initialize(authctxt->krb5_ctx,
251 authctxt->krb5_fwd_ccache, authctxt->krb5_user);
256 problem = krb5_verify_user(authctxt->krb5_ctx, authctxt->krb5_user,
257 authctxt->krb5_fwd_ccache, password, 1, NULL);
262 problem = krb5_get_init_creds_password(authctxt->krb5_ctx, &creds,
263 authctxt->krb5_user, password, NULL, NULL, 0, NULL, NULL);
267 problem = krb5_sname_to_principal(authctxt->krb5_ctx, NULL, NULL,
268 KRB5_NT_SRV_HST, &server);
273 problem = krb5_verify_init_creds(authctxt->krb5_ctx, &creds, server, NULL, NULL,
275 temporarily_use_uid(authctxt->pw);
277 krb5_free_principal(authctxt->krb5_ctx, server);
281 problem = krb5_cc_store_cred(authctxt->krb5_ctx, authctxt->krb5_fwd_ccache, &creds);
287 authctxt->krb5_ticket_file = (char *)krb5_cc_get_name(authctxt->krb5_ctx, authctxt->krb5_fwd_ccache);
293 debug("Kerberos password authentication failed: %s",
294 krb5_get_err_text(authctxt->krb5_ctx, problem));
296 krb5_cleanup_proc(authctxt);
298 if (options.kerberos_or_local_passwd)
307 krb5_cleanup_proc(void *context)
309 Authctxt *authctxt = (Authctxt *)context;
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;
316 if (authctxt->krb5_user) {
317 krb5_free_principal(authctxt->krb5_ctx, authctxt->krb5_user);
318 authctxt->krb5_user = NULL;
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;
325 if (authctxt->krb5_ctx) {
326 krb5_free_context(authctxt->krb5_ctx);
327 authctxt->krb5_ctx = NULL;