Forward port of Richard Sharpe's <realrichardsharpe@gmail.com> fix for bug #8970...
[Samba.git] / source4 / libnet / libnet_export_keytab.c
blobe8a0a1321d83085a8726cbdf086a4a5a228e8117
1 #include "includes.h"
2 #include "system/kerberos.h"
3 #include "auth/kerberos/kerberos.h"
4 #include <hdb.h>
5 #include "kdc/samba_kdc.h"
6 #include "libnet/libnet.h"
8 extern struct hdb_method hdb_samba4;
10 NTSTATUS libnet_export_keytab(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, struct libnet_export_keytab *r)
12 krb5_error_code ret;
13 struct smb_krb5_context *smb_krb5_context;
14 const char *from_keytab;
16 /* Register hdb-samba4 hooks for use as a keytab */
18 struct samba_kdc_base_context *base_ctx = talloc_zero(mem_ctx, struct samba_kdc_base_context);
19 if (!base_ctx) {
20 return NT_STATUS_NO_MEMORY;
23 base_ctx->ev_ctx = ctx->event_ctx;
24 base_ctx->lp_ctx = ctx->lp_ctx;
26 from_keytab = talloc_asprintf(base_ctx, "HDB:samba4&%p", base_ctx);
27 if (!from_keytab) {
28 return NT_STATUS_NO_MEMORY;
31 ret = smb_krb5_init_context(ctx, ctx->event_ctx, ctx->lp_ctx, &smb_krb5_context);
32 if (ret) {
33 return NT_STATUS_NO_MEMORY;
36 ret = krb5_plugin_register(smb_krb5_context->krb5_context,
37 PLUGIN_TYPE_DATA, "hdb",
38 &hdb_samba4);
39 if(ret) {
40 return NT_STATUS_NO_MEMORY;
43 ret = krb5_kt_register(smb_krb5_context->krb5_context, &hdb_kt_ops);
44 if(ret) {
45 return NT_STATUS_NO_MEMORY;
48 unlink(r->in.keytab_name);
50 ret = kt_copy(smb_krb5_context->krb5_context, from_keytab, r->in.keytab_name);
51 if(ret) {
52 r->out.error_string = smb_get_krb5_error_message(smb_krb5_context->krb5_context,
53 ret, mem_ctx);
54 return NT_STATUS_UNSUCCESSFUL;
56 return NT_STATUS_OK;