|
Packit |
fd8b60 |
/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
|
|
Packit |
fd8b60 |
/* tests/icred.c - test harness for getting initial creds */
|
|
Packit |
fd8b60 |
/*
|
|
Packit |
fd8b60 |
* Copyright (C) 2013 by the Massachusetts Institute of Technology.
|
|
Packit |
fd8b60 |
* All rights reserved.
|
|
Packit |
fd8b60 |
*
|
|
Packit |
fd8b60 |
* Redistribution and use in source and binary forms, with or without
|
|
Packit |
fd8b60 |
* modification, are permitted provided that the following conditions
|
|
Packit |
fd8b60 |
* are met:
|
|
Packit |
fd8b60 |
*
|
|
Packit |
fd8b60 |
* * Redistributions of source code must retain the above copyright
|
|
Packit |
fd8b60 |
* notice, this list of conditions and the following disclaimer.
|
|
Packit |
fd8b60 |
*
|
|
Packit |
fd8b60 |
* * Redistributions in binary form must reproduce the above copyright
|
|
Packit |
fd8b60 |
* notice, this list of conditions and the following disclaimer in
|
|
Packit |
fd8b60 |
* the documentation and/or other materials provided with the
|
|
Packit |
fd8b60 |
* distribution.
|
|
Packit |
fd8b60 |
*
|
|
Packit |
fd8b60 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
Packit |
fd8b60 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
Packit |
fd8b60 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
Packit |
fd8b60 |
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
Packit |
fd8b60 |
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
|
|
Packit |
fd8b60 |
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
Packit |
fd8b60 |
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
Packit |
fd8b60 |
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
Packit |
fd8b60 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
|
Packit |
fd8b60 |
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
Packit |
fd8b60 |
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
|
Packit |
fd8b60 |
* OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
Packit |
fd8b60 |
*/
|
|
Packit |
fd8b60 |
|
|
Packit |
fd8b60 |
/*
|
|
Packit |
fd8b60 |
* This program exercises the init_creds APIs in ways kinit doesn't. Right now
|
|
Packit |
fd8b60 |
* it is very simplistic, but it can be extended as needed.
|
|
Packit |
fd8b60 |
*/
|
|
Packit |
fd8b60 |
|
|
Packit |
fd8b60 |
#include "k5-platform.h"
|
|
Packit |
fd8b60 |
#include <krb5.h>
|
|
Packit |
fd8b60 |
|
|
Packit |
fd8b60 |
static krb5_context ctx;
|
|
Packit |
fd8b60 |
|
|
Packit |
fd8b60 |
static void
|
|
Packit |
fd8b60 |
check(krb5_error_code code)
|
|
Packit |
fd8b60 |
{
|
|
Packit |
fd8b60 |
const char *errmsg;
|
|
Packit |
fd8b60 |
|
|
Packit |
fd8b60 |
if (code) {
|
|
Packit |
fd8b60 |
errmsg = krb5_get_error_message(ctx, code);
|
|
Packit |
fd8b60 |
fprintf(stderr, "%s\n", errmsg);
|
|
Packit |
fd8b60 |
krb5_free_error_message(ctx, errmsg);
|
|
Packit |
fd8b60 |
exit(1);
|
|
Packit |
fd8b60 |
}
|
|
Packit |
fd8b60 |
}
|
|
Packit |
fd8b60 |
|
|
Packit |
fd8b60 |
int
|
|
Packit |
fd8b60 |
main(int argc, char **argv)
|
|
Packit |
fd8b60 |
{
|
|
Packit |
fd8b60 |
const char *princstr, *password;
|
|
Packit |
fd8b60 |
krb5_principal client;
|
|
Packit |
fd8b60 |
krb5_init_creds_context icc;
|
|
Packit |
fd8b60 |
krb5_get_init_creds_opt *opt;
|
|
Packit |
fd8b60 |
krb5_creds creds;
|
|
Packit |
fd8b60 |
krb5_boolean stepwise = FALSE;
|
|
Packit |
fd8b60 |
krb5_preauthtype ptypes[64];
|
|
Packit |
fd8b60 |
int c, nptypes = 0;
|
|
Packit |
fd8b60 |
char *val;
|
|
Packit |
fd8b60 |
|
|
Packit |
fd8b60 |
check(krb5_init_context(&ctx));
|
|
Packit |
fd8b60 |
check(krb5_get_init_creds_opt_alloc(ctx, &opt));
|
|
Packit |
fd8b60 |
|
|
Packit |
fd8b60 |
while ((c = getopt(argc, argv, "so:X:")) != -1) {
|
|
Packit |
fd8b60 |
switch (c) {
|
|
Packit |
fd8b60 |
case 's':
|
|
Packit |
fd8b60 |
stepwise = TRUE;
|
|
Packit |
fd8b60 |
break;
|
|
Packit |
fd8b60 |
case 'o':
|
|
Packit |
fd8b60 |
assert(nptypes < 64);
|
|
Packit |
fd8b60 |
ptypes[nptypes++] = atoi(optarg);
|
|
Packit |
fd8b60 |
break;
|
|
Packit |
fd8b60 |
case 'X':
|
|
Packit |
fd8b60 |
val = strchr(optarg, '=');
|
|
Packit |
fd8b60 |
if (val != NULL)
|
|
Packit |
fd8b60 |
*val++ = '\0';
|
|
Packit |
fd8b60 |
else
|
|
Packit |
fd8b60 |
val = "yes";
|
|
Packit |
fd8b60 |
check(krb5_get_init_creds_opt_set_pa(ctx, opt, optarg, val));
|
|
Packit |
fd8b60 |
break;
|
|
Packit |
fd8b60 |
default:
|
|
Packit |
fd8b60 |
abort();
|
|
Packit |
fd8b60 |
}
|
|
Packit |
fd8b60 |
}
|
|
Packit |
fd8b60 |
|
|
Packit |
fd8b60 |
argc -= optind;
|
|
Packit |
fd8b60 |
argv += optind;
|
|
Packit |
fd8b60 |
if (argc != 2)
|
|
Packit |
fd8b60 |
abort();
|
|
Packit |
fd8b60 |
princstr = argv[0];
|
|
Packit |
fd8b60 |
password = argv[1];
|
|
Packit |
fd8b60 |
|
|
Packit |
fd8b60 |
check(krb5_parse_name(ctx, princstr, &client));
|
|
Packit |
fd8b60 |
|
|
Packit |
fd8b60 |
if (nptypes > 0)
|
|
Packit |
fd8b60 |
krb5_get_init_creds_opt_set_preauth_list(opt, ptypes, nptypes);
|
|
Packit |
fd8b60 |
|
|
Packit |
fd8b60 |
if (stepwise) {
|
|
Packit |
fd8b60 |
/* Use the stepwise interface. */
|
|
Packit |
fd8b60 |
check(krb5_init_creds_init(ctx, client, NULL, NULL, 0, NULL, &icc));
|
|
Packit |
fd8b60 |
check(krb5_init_creds_set_password(ctx, icc, password));
|
|
Packit |
fd8b60 |
check(krb5_init_creds_get(ctx, icc));
|
|
Packit |
fd8b60 |
krb5_init_creds_free(ctx, icc);
|
|
Packit |
fd8b60 |
} else {
|
|
Packit |
fd8b60 |
/* Use the traditional one-shot interface. */
|
|
Packit |
fd8b60 |
check(krb5_get_init_creds_password(ctx, &creds, client, password, NULL,
|
|
Packit |
fd8b60 |
NULL, 0, NULL, opt));
|
|
Packit |
fd8b60 |
krb5_free_cred_contents(ctx, &creds);
|
|
Packit |
fd8b60 |
}
|
|
Packit |
fd8b60 |
|
|
Packit |
fd8b60 |
krb5_get_init_creds_opt_free(ctx, opt);
|
|
Packit |
fd8b60 |
krb5_free_principal(ctx, client);
|
|
Packit |
fd8b60 |
krb5_free_context(ctx);
|
|
Packit |
fd8b60 |
return 0;
|
|
Packit |
fd8b60 |
}
|