* src/include/krb5/krb5.hin: Add prototype for krb5_copy_context
authorTom Yu <tlyu@mit.edu>
Mon, 26 Jun 2006 21:03:04 +0000 (21:03 +0000)
committerTom Yu <tlyu@mit.edu>
Mon, 26 Jun 2006 21:03:04 +0000 (21:03 +0000)
* src/lib/krb5/krb/init_ctx.c (krb5_copy_context): New function to
copy/clone an existing krb5 context.
(copy_ktypes): Local helper function.

ticket: 2856
tags: pullup

git-svn-id: svn://anonsvn.mit.edu/krb5/trunk@18228 dc483132-0cff-0310-8789-dd5450dbe970

src/include/krb5/krb5.hin
src/lib/krb5/krb/init_ctx.c

index d786e6770c3a7e1b23e445b4d7bd001b20d4b6dd..9c8399847d00244ce94e8466d8c9e410f06c13ff 100644 (file)
@@ -1343,6 +1343,8 @@ krb5_error_code KRB5_CALLCONV krb5_init_secure_context
        (krb5_context *);
 void KRB5_CALLCONV krb5_free_context
        (krb5_context);
+krb5_error_code KRB5_CALLCONV krb5_copy_context
+       (krb5_context, krb5_context *);
 
 #if KRB5_PRIVATE
 krb5_error_code krb5_set_default_in_tkt_ktypes
index e1e1e755e7d1e404cdd11b4ee675629d17667df3..85a3d144a00428b8486fb32345fbda8bb28d1e8b 100644 (file)
@@ -485,3 +485,84 @@ krb5_is_permitted_enctype(krb5_context context, krb5_enctype etype)
 
     return(ret);
 }
+
+static krb5_error_code
+copy_ktypes(krb5_context ctx,
+           unsigned int nktypes,
+           krb5_enctype *oldktypes,
+           krb5_enctype **newktypes)
+{
+    unsigned int i;
+
+    *newktypes = NULL;
+    if (!nktypes)
+       return 0;
+
+    *newktypes = malloc(nktypes * sizeof(krb5_enctype));
+    if (*newktypes == NULL)
+       return ENOMEM;
+    for (i = 0; i < nktypes; i++)
+       (*newktypes)[i] = oldktypes[i];
+    return 0;
+}
+
+krb5_error_code KRB5_CALLCONV
+krb5_copy_context(krb5_context ctx, krb5_context *nctx_out)
+{
+    krb5_error_code ret;
+    krb5_context nctx;
+
+    *nctx_out = NULL;
+    if (ctx == NULL)
+       return EINVAL;          /* XXX */
+
+    nctx = malloc(sizeof(krb5_context*));
+    *nctx = *ctx;
+
+    nctx->in_tkt_ktypes = NULL;
+    nctx->in_tkt_ktype_count = 0;
+    nctx->tgs_ktypes = NULL;
+    nctx->tgs_ktype_count = 0;
+    nctx->default_realm = NULL;
+    nctx->profile = NULL;
+    nctx->db_context = NULL;
+    nctx->ser_ctx_count = 0;
+    nctx->ser_ctx = NULL;
+    nctx->prompt_types = NULL;
+    nctx->os_context->default_ccname = NULL;
+
+    memset(&nctx->libkrb5_plugins, 0, sizeof(nctx->libkrb5_plugins));
+    nctx->vtbl = NULL;
+    nctx->locate_fptrs = NULL;
+
+    memset(&nctx->err, 0, sizeof(nctx->err));
+
+    ret = copy_ktypes(nctx, ctx->in_tkt_ktype_count,
+                     ctx->in_tkt_ktypes, &nctx->in_tkt_ktypes);
+    if (ret)
+       goto errout;
+    nctx->in_tkt_ktype_count = ctx->in_tkt_ktype_count;
+
+    ret = copy_ktypes(nctx, ctx->tgs_ktype_count,
+                     ctx->tgs_ktypes, &nctx->in_tkt_ktypes);
+    if (ret)
+       goto errout;
+    nctx->tgs_ktype_count = ctx->tgs_ktype_count;
+
+    nctx->os_context->default_ccname = strdup(ctx->os_context->default_ccname);
+    if (nctx->os_context->default_ccname == NULL) {
+       ret = ENOMEM;
+       goto errout;
+    }
+    ret = krb5_get_profile(ctx, &nctx->profile);
+    if (ret)
+       goto errout;
+
+errout:
+    if (ret) {
+       krb5_free_context(nctx);
+    } else {
+       *nctx_out = nctx;
+    }
+    return ret;
+}