#include <stdio.h>
#include <stdint.h>
#include <getarg.h>
#include <sl.h>
#include "kadmin-commands.h"

static int
stash_wrap(int argc, char **argv)
{
    struct stash_options opt;
    int ret;
    int optidx = 0;
    struct getargs args[] = {
        { "enctype", 'e', arg_string, NULL, "encryption type", NULL },
        { "key-file", 'k', arg_string, NULL, "master key file", "file" },
        { "convert-file", 0, arg_flag, NULL, "just convert keyfile to new format", NULL },
        { "random-password", 0, arg_flag, NULL, "use a random password (and print the password to stdout)", NULL },
        { "master-key-fd", 0, arg_integer, NULL, "filedescriptor to read passphrase from", "fd" },
        { "help", 'h', arg_flag, NULL, NULL, NULL }
    };
    int help_flag = 0;
    opt.enctype_string = (char *)(uintptr_t)"des3-cbc-sha1";
    opt.key_file_string = NULL;
    opt.convert_file_flag = 0;
    opt.random_password_flag = 0;
    opt.master_key_fd_integer = -1;
    args[0].value = &opt.enctype_string;
    args[1].value = &opt.key_file_string;
    args[2].value = &opt.convert_file_flag;
    args[3].value = &opt.random_password_flag;
    args[4].value = &opt.master_key_fd_integer;
    args[5].value = &help_flag;
    if(getarg(args, 6, argc, argv, &optidx))
        goto usage;
    if(help_flag)
        goto usage;
    ret = stash(&opt, argc - optidx, argv + optidx);
    return ret;
usage:
    arg_printusage (args, 6, "stash", "");
    return 1;
}

static int
dump_wrap(int argc, char **argv)
{
    struct dump_options opt;
    int ret;
    int optidx = 0;
    struct getargs args[] = {
        { "decrypt", 'd', arg_flag, NULL, "decrypt keys", NULL },
        { "format", 'f', arg_string, NULL, "dump format, mit or heimdal (default: heimdal)", NULL },
        { "help", 'h', arg_flag, NULL, NULL, NULL }
    };
    int help_flag = 0;
    opt.decrypt_flag = 0;
    opt.format_string = NULL;
    args[0].value = &opt.decrypt_flag;
    args[1].value = &opt.format_string;
    args[2].value = &help_flag;
    if(getarg(args, 3, argc, argv, &optidx))
        goto usage;
    if(argc - optidx > 1) {
        fprintf(stderr, "Arguments given (%u) are more than expected (1).\n\n", argc - optidx);
        goto usage;
    }
    if(argc - optidx < 0) {
        fprintf(stderr, "Arguments given (%u) are less than expected (0).\n\n", argc - optidx);
        goto usage;
    }
    if(help_flag)
        goto usage;
    ret = dump(&opt, argc - optidx, argv + optidx);
    return ret;
usage:
    arg_printusage (args, 3, "dump", "[dump-file]");
    return 1;
}

static int
init_wrap(int argc, char **argv)
{
    struct init_options opt;
    int ret;
    int optidx = 0;
    struct getargs args[] = {
        { "realm-max-ticket-life", 0, arg_string, NULL, "realm max ticket lifetime", NULL },
        { "realm-max-renewable-life", 0, arg_string, NULL, "realm max renewable lifetime", NULL },
        { "bare", 0, arg_flag, NULL, "only create krbtgt for realm", NULL },
        { "help", 'h', arg_flag, NULL, NULL, NULL }
    };
    int help_flag = 0;
    opt.realm_max_ticket_life_string = NULL;
    opt.realm_max_renewable_life_string = NULL;
    opt.bare_flag = 0;
    args[0].value = &opt.realm_max_ticket_life_string;
    args[1].value = &opt.realm_max_renewable_life_string;
    args[2].value = &opt.bare_flag;
    args[3].value = &help_flag;
    if(getarg(args, 4, argc, argv, &optidx))
        goto usage;
    if(argc - optidx < 1) {
        fprintf(stderr, "Arguments given (%u) are less than expected (1).\n\n", argc - optidx);
        goto usage;
    }
    if(help_flag)
        goto usage;
    ret = init(&opt, argc - optidx, argv + optidx);
    return ret;
usage:
    arg_printusage (args, 4, "init", "realm...");
    return 1;
}

static int
load_wrap(int argc, char **argv)
{
    int ret;
    int optidx = 0;
    struct getargs args[] = {
        { "help", 'h', arg_flag, NULL, NULL, NULL }
    };
    int help_flag = 0;
    args[0].value = &help_flag;
    if(getarg(args, 1, argc, argv, &optidx))
        goto usage;
    if(argc - optidx != 1) {
        fprintf(stderr, "Need exactly 1 parameters (%u given).\n\n", argc - optidx);
        goto usage;
    }
    if(help_flag)
        goto usage;
    ret = load(NULL, argc - optidx, argv + optidx);
    return ret;
usage:
    arg_printusage (args, 1, "load", "file");
    return 1;
}

static int
merge_wrap(int argc, char **argv)
{
    int ret;
    int optidx = 0;
    struct getargs args[] = {
        { "help", 'h', arg_flag, NULL, NULL, NULL }
    };
    int help_flag = 0;
    args[0].value = &help_flag;
    if(getarg(args, 1, argc, argv, &optidx))
        goto usage;
    if(argc - optidx != 1) {
        fprintf(stderr, "Need exactly 1 parameters (%u given).\n\n", argc - optidx);
        goto usage;
    }
    if(help_flag)
        goto usage;
    ret = merge(NULL, argc - optidx, argv + optidx);
    return ret;
usage:
    arg_printusage (args, 1, "merge", "file");
    return 1;
}

static int
add_wrap(int argc, char **argv)
{
    struct add_options opt;
    int ret;
    int optidx = 0;
    struct getargs args[] = {
        { "random-key", 'r', arg_flag, NULL, "set random key", NULL },
        { "random-password", 0, arg_flag, NULL, "set random password", NULL },
        { "enctypes", 'e', arg_string, NULL, "encryption type(s)", NULL },
        { "password", 'p', arg_string, NULL, "principal's password", NULL },
        { "key", 0, arg_string, NULL, "DES-key in hex", NULL },
        { "max-ticket-life", 0, arg_string, NULL, "max ticket lifetime", "lifetime" },
        { "max-renewable-life", 0, arg_string, NULL, "max renewable life", "lifetime" },
        { "attributes", 0, arg_string, NULL, "principal attributes", "attributes" },
        { "expiration-time", 0, arg_string, NULL, "principal expiration time", "time" },
        { "pw-expiration-time", 0, arg_string, NULL, "password expiration time", "time" },
        { "hist-kvno-diff-clnt", 0, arg_integer, NULL, "historic keys allowed for client", "kvno diff" },
        { "hist-kvno-diff-svc", 0, arg_integer, NULL, "historic keys allowed for service", "kvno diff" },
        { "use-defaults", 0, arg_flag, NULL, "use default values", NULL },
        { "policy", 0, arg_string, NULL, "policy name", "policy" },
        { "help", 'h', arg_flag, NULL, NULL, NULL }
    };
    int help_flag = 0;
    opt.random_key_flag = 0;
    opt.random_password_flag = 0;
    opt.enctypes_string = NULL;
    opt.password_string = NULL;
    opt.key_string = NULL;
    opt.max_ticket_life_string = NULL;
    opt.max_renewable_life_string = NULL;
    opt.attributes_string = NULL;
    opt.expiration_time_string = NULL;
    opt.pw_expiration_time_string = NULL;
    opt.hist_kvno_diff_clnt_integer = -1;
    opt.hist_kvno_diff_svc_integer = -1;
    opt.use_defaults_flag = 0;
    opt.policy_string = NULL;
    args[0].value = &opt.random_key_flag;
    args[1].value = &opt.random_password_flag;
    args[2].value = &opt.enctypes_string;
    args[3].value = &opt.password_string;
    args[4].value = &opt.key_string;
    args[5].value = &opt.max_ticket_life_string;
    args[6].value = &opt.max_renewable_life_string;
    args[7].value = &opt.attributes_string;
    args[8].value = &opt.expiration_time_string;
    args[9].value = &opt.pw_expiration_time_string;
    args[10].value = &opt.hist_kvno_diff_clnt_integer;
    args[11].value = &opt.hist_kvno_diff_svc_integer;
    args[12].value = &opt.use_defaults_flag;
    args[13].value = &opt.policy_string;
    args[14].value = &help_flag;
    if(getarg(args, 15, argc, argv, &optidx))
        goto usage;
    if(argc - optidx < 1) {
        fprintf(stderr, "Arguments given (%u) are less than expected (1).\n\n", argc - optidx);
        goto usage;
    }
    if(help_flag)
        goto usage;
    ret = add_new_key(&opt, argc - optidx, argv + optidx);
    return ret;
usage:
    arg_printusage (args, 15, "add", "principal...");
    return 1;
}

static int
add_namespace_wrap(int argc, char **argv)
{
    struct add_namespace_options opt;
    int ret;
    int optidx = 0;
    struct getargs args[] = {
        { "enctypes", 'e', arg_string, NULL, "encryption type(s)", NULL },
        { "max-ticket-life", 0, arg_string, NULL, "max ticket lifetime", "lifetime" },
        { "max-renewable-life", 0, arg_string, NULL, "max renewable life", "lifetime" },
        { "key-rotation-epoch", 0, arg_string, NULL, "absolute start time (or +timespec for relative to now with default unit of month)", "time" },
        { "key-rotation-period", 0, arg_string, NULL, "automatic key rotation period", "time" },
        { "attributes", 0, arg_string, NULL, "principal attributes", "attributes" },
        { "help", 'h', arg_flag, NULL, NULL, NULL }
    };
    int help_flag = 0;
    opt.enctypes_string = NULL;
    opt.max_ticket_life_string = NULL;
    opt.max_renewable_life_string = NULL;
    opt.key_rotation_epoch_string = NULL;
    opt.key_rotation_period_string = NULL;
    opt.attributes_string = NULL;
    args[0].value = &opt.enctypes_string;
    args[1].value = &opt.max_ticket_life_string;
    args[2].value = &opt.max_renewable_life_string;
    args[3].value = &opt.key_rotation_epoch_string;
    args[4].value = &opt.key_rotation_period_string;
    args[5].value = &opt.attributes_string;
    args[6].value = &help_flag;
    if(getarg(args, 7, argc, argv, &optidx))
        goto usage;
    if(argc - optidx < 1) {
        fprintf(stderr, "Arguments given (%u) are less than expected (1).\n\n", argc - optidx);
        goto usage;
    }
    if(help_flag)
        goto usage;
    ret = add_new_namespace(&opt, argc - optidx, argv + optidx);
    return ret;
usage:
    arg_printusage (args, 7, "add_namespace", "principal...");
    return 1;
}

static int
modify_namespace_wrap(int argc, char **argv)
{
    struct modify_namespace_options opt;
    int ret;
    int optidx = 0;
    struct getargs args[] = {
        { "enctypes", 'e', arg_strings, NULL, "encryption type(s)", NULL },
        { "max-ticket-life", 0, arg_string, NULL, "max ticket lifetime", "lifetime" },
        { "max-renewable-life", 0, arg_string, NULL, "max renewable life", "lifetime" },
        { "attributes", 0, arg_string, NULL, "principal attributes", "attributes" },
        { "krb5-config-file", 'C', arg_string, NULL, "filename to save the principal's krb5.confg in", NULL },
        { "help", 'h', arg_flag, NULL, NULL, NULL }
    };
    int help_flag = 0;
    opt.enctypes_strings.num_strings = 0;
    opt.enctypes_strings.strings = NULL;
    opt.max_ticket_life_string = NULL;
    opt.max_renewable_life_string = NULL;
    opt.attributes_string = NULL;
    opt.krb5_config_file_string = NULL;
    args[0].value = &opt.enctypes_strings;
    args[1].value = &opt.max_ticket_life_string;
    args[2].value = &opt.max_renewable_life_string;
    args[3].value = &opt.attributes_string;
    args[4].value = &opt.krb5_config_file_string;
    args[5].value = &help_flag;
    if(getarg(args, 6, argc, argv, &optidx))
        goto usage;
    if(argc - optidx < 1) {
        fprintf(stderr, "Arguments given (%u) are less than expected (1).\n\n", argc - optidx);
        goto usage;
    }
    if(help_flag)
        goto usage;
    ret = modify_namespace(&opt, argc - optidx, argv + optidx);
    free_getarg_strings (&opt.enctypes_strings);
    return ret;
usage:
    arg_printusage (args, 6, "modify_namespace", "principal...");
    free_getarg_strings (&opt.enctypes_strings);
    return 1;
}

static int
modify_namespace_key_rotation_wrap(int argc, char **argv)
{
    struct modify_namespace_key_rotation_options opt;
    int ret;
    int optidx = 0;
    struct getargs args[] = {
        { "force", 'f', arg_flag, NULL, "change schedule even if it would revoke some extant tickets", NULL },
        { "keep-base-key", 'k', arg_flag, NULL, "keep current base key for new key rotation schedule", NULL },
        { "revoke-old", 'r', arg_string, NULL, "delete base keys older than this to revoke old tickets", "time" },
        { "new-key-rotation-epoch", 0, arg_string, NULL, "new start time relative to now", "time" },
        { "new-key-rotation-period", 0, arg_string, NULL, "new automatic key rotation period", "time" },
        { "help", 'h', arg_flag, NULL, NULL, NULL }
    };
    int help_flag = 0;
    opt.force_flag = 0;
    opt.keep_base_key_flag = 0;
    opt.revoke_old_string = NULL;
    opt.new_key_rotation_epoch_string = NULL;
    opt.new_key_rotation_period_string = NULL;
    args[0].value = &opt.force_flag;
    args[1].value = &opt.keep_base_key_flag;
    args[2].value = &opt.revoke_old_string;
    args[3].value = &opt.new_key_rotation_epoch_string;
    args[4].value = &opt.new_key_rotation_period_string;
    args[5].value = &help_flag;
    if(getarg(args, 6, argc, argv, &optidx))
        goto usage;
    if(argc - optidx != 1) {
        fprintf(stderr, "Need exactly 1 parameters (%u given).\n\n", argc - optidx);
        goto usage;
    }
    if(help_flag)
        goto usage;
    ret = modify_ns_kr(&opt, argc - optidx, argv + optidx);
    return ret;
usage:
    arg_printusage (args, 6, "modify_namespace_key_rotation", "principal...");
    return 1;
}

static int
passwd_wrap(int argc, char **argv)
{
    struct passwd_options opt;
    int ret;
    int optidx = 0;
    struct getargs args[] = {
        { "random-key", 'r', arg_flag, NULL, "set random key", NULL },
        { "random-password", 0, arg_flag, NULL, "set random password", NULL },
        { "enctypes", 'e', arg_string, NULL, "encryption type(s)", NULL },
        { "password", 'p', arg_string, NULL, "principal's password", NULL },
        { "key", 0, arg_string, NULL, "DES key in hex", NULL },
        { "keepold", 0, arg_flag, NULL, "keep old keys/password needed to decrypt extant tickets (default)", NULL },
        { "keepallold", 0, arg_flag, NULL, "keep all old keys/password", NULL },
        { "pruneall", 0, arg_flag, NULL, "delete all old keys", NULL },
        { "help", 'h', arg_flag, NULL, NULL, NULL }
    };
    int help_flag = 0;
    opt.random_key_flag = 0;
    opt.random_password_flag = 0;
    opt.enctypes_string = NULL;
    opt.password_string = NULL;
    opt.key_string = NULL;
    opt.keepold_flag = 0;
    opt.keepallold_flag = 0;
    opt.pruneall_flag = 0;
    args[0].value = &opt.random_key_flag;
    args[1].value = &opt.random_password_flag;
    args[2].value = &opt.enctypes_string;
    args[3].value = &opt.password_string;
    args[4].value = &opt.key_string;
    args[5].value = &opt.keepold_flag;
    args[6].value = &opt.keepallold_flag;
    args[7].value = &opt.pruneall_flag;
    args[8].value = &help_flag;
    if(getarg(args, 9, argc, argv, &optidx))
        goto usage;
    if(argc - optidx < 1) {
        fprintf(stderr, "Arguments given (%u) are less than expected (1).\n\n", argc - optidx);
        goto usage;
    }
    if(help_flag)
        goto usage;
    ret = cpw_entry(&opt, argc - optidx, argv + optidx);
    return ret;
usage:
    arg_printusage (args, 9, "passwd", "principal...");
    return 1;
}

static int
delete_wrap(int argc, char **argv)
{
    int ret;
    int optidx = 0;
    struct getargs args[] = {
        { "help", 'h', arg_flag, NULL, NULL, NULL }
    };
    int help_flag = 0;
    args[0].value = &help_flag;
    if(getarg(args, 1, argc, argv, &optidx))
        goto usage;
    if(argc - optidx < 1) {
        fprintf(stderr, "Arguments given (%u) are less than expected (1).\n\n", argc - optidx);
        goto usage;
    }
    if(help_flag)
        goto usage;
    ret = del_entry(NULL, argc - optidx, argv + optidx);
    return ret;
usage:
    arg_printusage (args, 1, "delete", "principal...");
    return 1;
}

static int
delete_namespace_wrap(int argc, char **argv)
{
    int ret;
    int optidx = 0;
    struct getargs args[] = {
        { "help", 'h', arg_flag, NULL, NULL, NULL }
    };
    int help_flag = 0;
    args[0].value = &help_flag;
    if(getarg(args, 1, argc, argv, &optidx))
        goto usage;
    if(argc - optidx < 1) {
        fprintf(stderr, "Arguments given (%u) are less than expected (1).\n\n", argc - optidx);
        goto usage;
    }
    if(help_flag)
        goto usage;
    ret = del_namespace(NULL, argc - optidx, argv + optidx);
    return ret;
usage:
    arg_printusage (args, 1, "delete_namespace", "principal...");
    return 1;
}

static int
del_enctype_wrap(int argc, char **argv)
{
    int ret;
    int optidx = 0;
    struct getargs args[] = {
        { "help", 'h', arg_flag, NULL, NULL, NULL }
    };
    int help_flag = 0;
    args[0].value = &help_flag;
    if(getarg(args, 1, argc, argv, &optidx))
        goto usage;
    if(argc - optidx < 2) {
        fprintf(stderr, "Arguments given (%u) are less than expected (2).\n\n", argc - optidx);
        goto usage;
    }
    if(help_flag)
        goto usage;
    ret = del_enctype(NULL, argc - optidx, argv + optidx);
    return ret;
usage:
    arg_printusage (args, 1, "del_enctype", "principal enctype...");
    return 1;
}

static int
add_enctype_wrap(int argc, char **argv)
{
    struct add_enctype_options opt;
    int ret;
    int optidx = 0;
    struct getargs args[] = {
        { "random-key", 'r', arg_flag, NULL, "set random key", NULL },
        { "help", 'h', arg_flag, NULL, NULL, NULL }
    };
    int help_flag = 0;
    opt.random_key_flag = 0;
    args[0].value = &opt.random_key_flag;
    args[1].value = &help_flag;
    if(getarg(args, 2, argc, argv, &optidx))
        goto usage;
    if(argc - optidx < 2) {
        fprintf(stderr, "Arguments given (%u) are less than expected (2).\n\n", argc - optidx);
        goto usage;
    }
    if(help_flag)
        goto usage;
    ret = add_enctype(&opt, argc - optidx, argv + optidx);
    return ret;
usage:
    arg_printusage (args, 2, "add_enctype", "principal enctype...");
    return 1;
}

static int
ext_keytab_wrap(int argc, char **argv)
{
    struct ext_keytab_options opt;
    int ret;
    int optidx = 0;
    struct getargs args[] = {
        { "keytab", 'k', arg_string, NULL, "keytab to use", "keytab" },
        { "random-key", 'r', arg_flag, NULL, "set random key", NULL },
        { "enctypes", 'e', arg_string, NULL, "encryption type(s)", NULL },
        { "keepold", 0, arg_flag, NULL, "keep old keys/password needed to decrypt extant tickets (default)", NULL },
        { "keepallold", 0, arg_flag, NULL, "keep all old keys/password", NULL },
        { "pruneall", 0, arg_flag, NULL, "delete all old keys", NULL },
        { "help", 'h', arg_flag, NULL, NULL, NULL }
    };
    int help_flag = 0;
    opt.keytab_string = NULL;
    opt.random_key_flag = 0;
    opt.enctypes_string = NULL;
    opt.keepold_flag = 0;
    opt.keepallold_flag = 0;
    opt.pruneall_flag = 0;
    args[0].value = &opt.keytab_string;
    args[1].value = &opt.random_key_flag;
    args[2].value = &opt.enctypes_string;
    args[3].value = &opt.keepold_flag;
    args[4].value = &opt.keepallold_flag;
    args[5].value = &opt.pruneall_flag;
    args[6].value = &help_flag;
    if(getarg(args, 7, argc, argv, &optidx))
        goto usage;
    if(argc - optidx < 1) {
        fprintf(stderr, "Arguments given (%u) are less than expected (1).\n\n", argc - optidx);
        goto usage;
    }
    if(help_flag)
        goto usage;
    ret = ext_keytab(&opt, argc - optidx, argv + optidx);
    return ret;
usage:
    arg_printusage (args, 7, "ext_keytab", "principal...");
    return 1;
}

static int
get_wrap(int argc, char **argv)
{
    struct get_options opt;
    int ret;
    int optidx = 0;
    struct getargs args[] = {
        { "long", 'l', arg_flag, NULL, "long format", NULL },
        { "short", 's', arg_flag, NULL, "short format", NULL },
        { "terse", 't', arg_flag, NULL, "terse format", NULL },
        { "column-info", 'o', arg_string, NULL, "columns to print for short output", NULL },
        { "krb5-config-file", 'C', arg_string, NULL, "filename to save the principal's krb5.confg in", NULL },
        { "upto", 0, arg_integer, NULL, "maximum number of principals to get/list", NULL },
        { "help", 'h', arg_flag, NULL, NULL, NULL }
    };
    int help_flag = 0;
    opt.long_flag = -1;
    opt.short_flag = 0;
    opt.terse_flag = 0;
    opt.column_info_string = NULL;
    opt.krb5_config_file_string = NULL;
    opt.upto_integer = -1;
    args[0].value = &opt.long_flag;
    args[1].value = &opt.short_flag;
    args[2].value = &opt.terse_flag;
    args[3].value = &opt.column_info_string;
    args[4].value = &opt.krb5_config_file_string;
    args[5].value = &opt.upto_integer;
    args[6].value = &help_flag;
    if(getarg(args, 7, argc, argv, &optidx))
        goto usage;
    if(argc - optidx < 1) {
        fprintf(stderr, "Arguments given (%u) are less than expected (1).\n\n", argc - optidx);
        goto usage;
    }
    if(help_flag)
        goto usage;
    ret = get_entry(&opt, argc - optidx, argv + optidx);
    return ret;
usage:
    arg_printusage (args, 7, "get", "principal...");
    return 1;
}

static int
rename_wrap(int argc, char **argv)
{
    int ret;
    int optidx = 0;
    struct getargs args[] = {
        { "help", 'h', arg_flag, NULL, NULL, NULL }
    };
    int help_flag = 0;
    args[0].value = &help_flag;
    if(getarg(args, 1, argc, argv, &optidx))
        goto usage;
    if(argc - optidx != 2) {
        fprintf(stderr, "Need exactly 2 parameters (%u given).\n\n", argc - optidx);
        goto usage;
    }
    if(help_flag)
        goto usage;
    ret = rename_entry(NULL, argc - optidx, argv + optidx);
    return ret;
usage:
    arg_printusage (args, 1, "rename", "from to");
    return 1;
}

static int
modify_wrap(int argc, char **argv)
{
    struct modify_options opt;
    int ret;
    int optidx = 0;
    struct getargs args[] = {
        { "max-ticket-life", 0, arg_string, NULL, "max ticket lifetime", "lifetime" },
        { "max-renewable-life", 0, arg_string, NULL, "max renewable life", "lifetime" },
        { "attributes", 'a', arg_string, NULL, "principal attributes", "attributes" },
        { "expiration-time", 0, arg_string, NULL, "principal expiration time", "time" },
        { "pw-expiration-time", 0, arg_string, NULL, "password expiration time", "time" },
        { "kvno", 0, arg_integer, NULL, "key version number", NULL },
        { "constrained-delegation", 0, arg_strings, NULL, "allowed target principals", "principal" },
        { "alias", 0, arg_strings, NULL, "aliases", "principal" },
        { "pkinit-acl", 0, arg_strings, NULL, "aliases", "subject dn" },
        { "policy", 0, arg_string, NULL, "policy name", "policy" },
        { "service-enctypes", 'e', arg_strings, NULL, "set enctypes supported by service", "enctype" },
        { "hist-kvno-diff-clnt", 0, arg_integer, NULL, "historic keys allowed for client", "kvno diff" },
        { "hist-kvno-diff-svc", 0, arg_integer, NULL, "historic keys allowed for service", "kvno diff" },
        { "krb5-config-file", 'C', arg_string, NULL, "krb5.conf to save in principal record", NULL },
        { "help", 'h', arg_flag, NULL, NULL, NULL }
    };
    int help_flag = 0;
    opt.max_ticket_life_string = NULL;
    opt.max_renewable_life_string = NULL;
    opt.attributes_string = NULL;
    opt.expiration_time_string = NULL;
    opt.pw_expiration_time_string = NULL;
    opt.kvno_integer = -1;
    opt.constrained_delegation_strings.num_strings = 0;
    opt.constrained_delegation_strings.strings = NULL;
    opt.alias_strings.num_strings = 0;
    opt.alias_strings.strings = NULL;
    opt.pkinit_acl_strings.num_strings = 0;
    opt.pkinit_acl_strings.strings = NULL;
    opt.policy_string = NULL;
    opt.service_enctypes_strings.num_strings = 0;
    opt.service_enctypes_strings.strings = NULL;
    opt.hist_kvno_diff_clnt_integer = -1;
    opt.hist_kvno_diff_svc_integer = -1;
    opt.krb5_config_file_string = NULL;
    args[0].value = &opt.max_ticket_life_string;
    args[1].value = &opt.max_renewable_life_string;
    args[2].value = &opt.attributes_string;
    args[3].value = &opt.expiration_time_string;
    args[4].value = &opt.pw_expiration_time_string;
    args[5].value = &opt.kvno_integer;
    args[6].value = &opt.constrained_delegation_strings;
    args[7].value = &opt.alias_strings;
    args[8].value = &opt.pkinit_acl_strings;
    args[9].value = &opt.policy_string;
    args[10].value = &opt.service_enctypes_strings;
    args[11].value = &opt.hist_kvno_diff_clnt_integer;
    args[12].value = &opt.hist_kvno_diff_svc_integer;
    args[13].value = &opt.krb5_config_file_string;
    args[14].value = &help_flag;
    if(getarg(args, 15, argc, argv, &optidx))
        goto usage;
    if(argc - optidx != 1) {
        fprintf(stderr, "Need exactly 1 parameters (%u given).\n\n", argc - optidx);
        goto usage;
    }
    if(help_flag)
        goto usage;
    ret = mod_entry(&opt, argc - optidx, argv + optidx);
    free_getarg_strings (&opt.constrained_delegation_strings);
    free_getarg_strings (&opt.alias_strings);
    free_getarg_strings (&opt.pkinit_acl_strings);
    free_getarg_strings (&opt.service_enctypes_strings);
    return ret;
usage:
    arg_printusage (args, 15, "modify", "principal");
    free_getarg_strings (&opt.constrained_delegation_strings);
    free_getarg_strings (&opt.alias_strings);
    free_getarg_strings (&opt.pkinit_acl_strings);
    free_getarg_strings (&opt.service_enctypes_strings);
    return 1;
}

static int
add_alias_wrap(int argc, char **argv)
{
    int ret;
    int optidx = 0;
    struct getargs args[] = {
        { "help", 'h', arg_flag, NULL, NULL, NULL }
    };
    int help_flag = 0;
    args[0].value = &help_flag;
    if(getarg(args, 1, argc, argv, &optidx))
        goto usage;
    if(argc - optidx < 2) {
        fprintf(stderr, "Arguments given (%u) are less than expected (2).\n\n", argc - optidx);
        goto usage;
    }
    if(help_flag)
        goto usage;
    ret = add_alias(NULL, argc - optidx, argv + optidx);
    return ret;
usage:
    arg_printusage (args, 1, "add_alias", "principal");
    return 1;
}

static int
del_alias_wrap(int argc, char **argv)
{
    int ret;
    int optidx = 0;
    struct getargs args[] = {
        { "help", 'h', arg_flag, NULL, NULL, NULL }
    };
    int help_flag = 0;
    args[0].value = &help_flag;
    if(getarg(args, 1, argc, argv, &optidx))
        goto usage;
    if(argc - optidx < 1) {
        fprintf(stderr, "Arguments given (%u) are less than expected (1).\n\n", argc - optidx);
        goto usage;
    }
    if(help_flag)
        goto usage;
    ret = del_alias(NULL, argc - optidx, argv + optidx);
    return ret;
usage:
    arg_printusage (args, 1, "del_alias", "principal");
    return 1;
}

static int
prune_wrap(int argc, char **argv)
{
    struct prune_options opt;
    int ret;
    int optidx = 0;
    struct getargs args[] = {
        { "kvno", 0, arg_integer, NULL, "key version number", NULL },
        { "help", 'h', arg_flag, NULL, NULL, NULL }
    };
    int help_flag = 0;
    opt.kvno_integer = 0;
    args[0].value = &opt.kvno_integer;
    args[1].value = &help_flag;
    if(getarg(args, 2, argc, argv, &optidx))
        goto usage;
    if(argc - optidx != 1) {
        fprintf(stderr, "Need exactly 1 parameters (%u given).\n\n", argc - optidx);
        goto usage;
    }
    if(help_flag)
        goto usage;
    ret = prune(&opt, argc - optidx, argv + optidx);
    return ret;
usage:
    arg_printusage (args, 2, "prune", "principal");
    return 1;
}

static int
privileges_wrap(int argc, char **argv)
{
    int ret;
    int optidx = 0;
    struct getargs args[] = {
        { "help", 'h', arg_flag, NULL, NULL, NULL }
    };
    int help_flag = 0;
    args[0].value = &help_flag;
    if(getarg(args, 1, argc, argv, &optidx))
        goto usage;
    if(argc - optidx > 0) {
        fprintf(stderr, "Arguments given (%u) are more than expected (0).\n\n", argc - optidx);
        goto usage;
    }
    if(help_flag)
        goto usage;
    ret = get_privs(NULL, argc - optidx, argv + optidx);
    return ret;
usage:
    arg_printusage (args, 1, "privileges", "");
    return 1;
}

static int
list_wrap(int argc, char **argv)
{
    struct list_options opt;
    int ret;
    int optidx = 0;
    struct getargs args[] = {
        { "long", 'l', arg_flag, NULL, "long format", NULL },
        { "short", 's', arg_flag, NULL, "short format", NULL },
        { "terse", 't', arg_flag, NULL, "terse format", NULL },
        { "column-info", 'o', arg_string, NULL, "columns to print for short output", NULL },
        { "krb5-config-file", 0, arg_string, NULL, "only use this option with the get command", NULL },
        { "upto", 0, arg_integer, NULL, "maximum number of principals to get/list", NULL },
        { "help", 'h', arg_flag, NULL, NULL, NULL }
    };
    int help_flag = 0;
    opt.long_flag = 0;
    opt.short_flag = 0;
    opt.terse_flag = -1;
    opt.column_info_string = NULL;
    opt.krb5_config_file_string = NULL;
    opt.upto_integer = -1;
    args[0].value = &opt.long_flag;
    args[1].value = &opt.short_flag;
    args[2].value = &opt.terse_flag;
    args[3].value = &opt.column_info_string;
    args[4].value = &opt.krb5_config_file_string;
    args[5].value = &opt.upto_integer;
    args[6].value = &help_flag;
    if(getarg(args, 7, argc, argv, &optidx))
        goto usage;
    if(argc - optidx < 1) {
        fprintf(stderr, "Arguments given (%u) are less than expected (1).\n\n", argc - optidx);
        goto usage;
    }
    if(help_flag)
        goto usage;
    ret = list_princs(&opt, argc - optidx, argv + optidx);
    return ret;
usage:
    arg_printusage (args, 7, "list", "principal...");
    return 1;
}

static int
verify_password_quality_wrap(int argc, char **argv)
{
    int ret;
    int optidx = 0;
    struct getargs args[] = {
        { "help", 'h', arg_flag, NULL, NULL, NULL }
    };
    int help_flag = 0;
    args[0].value = &help_flag;
    if(getarg(args, 1, argc, argv, &optidx))
        goto usage;
    if(argc - optidx != 2) {
        fprintf(stderr, "Need exactly 2 parameters (%u given).\n\n", argc - optidx);
        goto usage;
    }
    if(help_flag)
        goto usage;
    ret = password_quality(NULL, argc - optidx, argv + optidx);
    return ret;
usage:
    arg_printusage (args, 1, "verify-password-quality", "principal password");
    return 1;
}

static int
check_wrap(int argc, char **argv)
{
    int ret;
    int optidx = 0;
    struct getargs args[] = {
        { "help", 'h', arg_flag, NULL, NULL, NULL }
    };
    int help_flag = 0;
    args[0].value = &help_flag;
    if(getarg(args, 1, argc, argv, &optidx))
        goto usage;
    if(argc - optidx > 1) {
        fprintf(stderr, "Arguments given (%u) are more than expected (1).\n\n", argc - optidx);
        goto usage;
    }
    if(argc - optidx < 0) {
        fprintf(stderr, "Arguments given (%u) are less than expected (0).\n\n", argc - optidx);
        goto usage;
    }
    if(help_flag)
        goto usage;
    ret = check(NULL, argc - optidx, argv + optidx);
    return ret;
usage:
    arg_printusage (args, 1, "check", "[realm]");
    return 1;
}

static int
lock_wrap(int argc, char **argv)
{
    int ret;
    int optidx = 0;
    struct getargs args[] = {
        { "help", 'h', arg_flag, NULL, NULL, NULL }
    };
    int help_flag = 0;
    args[0].value = &help_flag;
    if(getarg(args, 1, argc, argv, &optidx))
        goto usage;
    if(argc - optidx != 0) {
        fprintf(stderr, "Need exactly 0 parameters (%u given).\n\n", argc - optidx);
        goto usage;
    }
    if(help_flag)
        goto usage;
    ret = lock(NULL, argc - optidx, argv + optidx);
    return ret;
usage:
    arg_printusage (args, 1, "lock", "");
    return 1;
}

static int
unlock_wrap(int argc, char **argv)
{
    int ret;
    int optidx = 0;
    struct getargs args[] = {
        { "help", 'h', arg_flag, NULL, NULL, NULL }
    };
    int help_flag = 0;
    args[0].value = &help_flag;
    if(getarg(args, 1, argc, argv, &optidx))
        goto usage;
    if(argc - optidx != 0) {
        fprintf(stderr, "Need exactly 0 parameters (%u given).\n\n", argc - optidx);
        goto usage;
    }
    if(help_flag)
        goto usage;
    ret = unlock(NULL, argc - optidx, argv + optidx);
    return ret;
usage:
    arg_printusage (args, 1, "unlock", "");
    return 1;
}

static int
help_wrap(int argc, char **argv)
{
    int ret;
    int optidx = 0;
    struct getargs args[] = {
        { "help", 'h', arg_flag, NULL, NULL, NULL }
    };
    int help_flag = 0;
    args[0].value = &help_flag;
    if(getarg(args, 1, argc, argv, &optidx))
        goto usage;
    if(argc - optidx > 1) {
        fprintf(stderr, "Arguments given (%u) are more than expected (1).\n\n", argc - optidx);
        goto usage;
    }
    if(argc - optidx < 0) {
        fprintf(stderr, "Arguments given (%u) are less than expected (0).\n\n", argc - optidx);
        goto usage;
    }
    if(help_flag)
        goto usage;
    ret = help(NULL, argc - optidx, argv + optidx);
    return ret;
usage:
    arg_printusage (args, 1, "help", "[command]");
    return 1;
}

static int
exit_wrap(int argc, char **argv)
{
    int ret;
    int optidx = 0;
    struct getargs args[] = {
        { "help", 'h', arg_flag, NULL, NULL, NULL }
    };
    int help_flag = 0;
    args[0].value = &help_flag;
    if(getarg(args, 1, argc, argv, &optidx))
        goto usage;
    if(argc - optidx > 0) {
        fprintf(stderr, "Arguments given (%u) are more than expected (0).\n\n", argc - optidx);
        goto usage;
    }
    if(help_flag)
        goto usage;
    ret = exit_kadmin(NULL, argc - optidx, argv + optidx);
    return ret;
usage:
    arg_printusage (args, 1, "exit", "");
    return 1;
}

SL_cmd commands[] = {
        { "stash", stash_wrap, "stash", "Writes the Kerberos master key to a file used by the KDC. \nLocal (-l) mode only." },
        { "kstash", NULL, NULL, NULL },

        { "dump", dump_wrap, "dump [dump-file]", "Dumps the database in a human readable format to the specified file, \nor the standard out. Local (-l) mode only." },

        { "init", init_wrap, "init realm...", "Initializes the default principals for a realm. Creates the database\nif necessary. Local (-l) mode only." },

        { "load", load_wrap, "load file", "Loads a previously dumped file. Local (-l) mode only." },

        { "merge", merge_wrap, "merge file", "Merges the contents of a dump file into the database. Local (-l) mode only." },

        { "add", add_wrap, "add principal...", "Adds a principal to the database." },
        { "ank", NULL, NULL, NULL },
        { "add_new_key", NULL, NULL, NULL },

        { "add_namespace", add_namespace_wrap, "add_namespace principal...", "Adds a namespace of virtual principals with derived keys to the database." },
        { "add_ns", NULL, NULL, NULL },

        { "modify_namespace", modify_namespace_wrap, "modify_namespace principal...", "Modifies a namespace of virtual principals with derived keys to the database." },
        { "mod_ns", NULL, NULL, NULL },

        { "modify_namespace_key_rotation", modify_namespace_key_rotation_wrap, "modify_namespace_key_rotation principal...", "Adds or changes new key rotation schedule for the given namespace." },
        { "mod_ns_kr", NULL, NULL, NULL },

        { "passwd", passwd_wrap, "passwd principal...", "Changes the password of one or more principals matching the expressions." },
        { "cpw", NULL, NULL, NULL },
        { "change_password", NULL, NULL, NULL },

        { "delete", delete_wrap, "delete principal...", "Deletes all principals matching the expressions." },
        { "del", NULL, NULL, NULL },
        { "del_entry", NULL, NULL, NULL },

        { "delete_namespace", delete_namespace_wrap, "delete_namespace principal...", "Deletes the given virtual principal namespaces" },
        { "del_ns", NULL, NULL, NULL },

        { "del_enctype", del_enctype_wrap, "del_enctype principal enctype...", "Delete all the mentioned enctypes for principal." },

        { "add_enctype", add_enctype_wrap, "add_enctype principal enctype...", "Add new enctypes for principal." },

        { "ext_keytab", ext_keytab_wrap, "ext_keytab principal...", "Extracts the keys of all principals matching the expressions, and stores them in a keytab." },

        { "get", get_wrap, "get principal...", "Shows information about principals matching the expressions." },
        { "get_entry", NULL, NULL, NULL },

        { "rename", rename_wrap, "rename from to", "Renames a principal." },

        { "modify", modify_wrap, "modify principal", "Modifies some attributes of the specified principal." },
        { "mod", NULL, NULL, NULL },

        { "add_alias", add_alias_wrap, "add_alias principal", "Add one or more aliases to the given principal." },

        { "del_alias", del_alias_wrap, "del_alias principal", "Delete one or more aliases without deleting their canonical principals." },

        { "prune", prune_wrap, "prune principal", "Delete keys from history by max-ticket-life or kvno." },

        { "privileges", privileges_wrap, "privileges", "Shows which operations you are allowed to perform." },
        { "privs", NULL, NULL, NULL },

        { "list", list_wrap, "list principal...", "Lists principals in a terse format. Equivalent to \"get -t\"." },

        { "verify-password-quality", verify_password_quality_wrap, "verify-password-quality principal password", "Try run the password quality function locally (not doing RPC out to server)." },
        { "pwq", NULL, NULL, NULL },

        { "check", check_wrap, "check [realm]", "Check the realm (if not given, the default realm) for configuration errors." },

        { "lock", lock_wrap, "lock ", "Lock the database for writing (use with care)." },

        { "unlock", unlock_wrap, "unlock ", "Unlock the database." },

        { "help", help_wrap, "help [command]", "Help! I need somebody." },
        { "?", NULL, NULL, NULL },

        { "exit", exit_wrap, "exit", "Quits." },
        { "quit", NULL, NULL, NULL },

    { NULL, NULL, NULL, NULL }
};
