/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */1/* tests/rdreq.c - Test harness for krb5_rd_req */2/*3* Copyright (C) 2014 by the Massachusetts Institute of Technology.4* All rights reserved.5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted provided that the following conditions8* are met:9*10* * Redistributions of source code must retain the above copyright11* notice, this list of conditions and the following disclaimer.12*13* * Redistributions in binary form must reproduce the above copyright14* notice, this list of conditions and the following disclaimer in15* the documentation and/or other materials provided with the16* distribution.17*18* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS19* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT20* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS21* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE22* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,23* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES24* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR25* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)26* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,27* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)28* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED29* OF THE POSSIBILITY OF SUCH DAMAGE.30*/3132#include <stdio.h>33#include <stdlib.h>34#include <string.h>35#include <assert.h>36#include <krb5.h>3738int39main(int argc, char **argv)40{41krb5_context context;42krb5_principal client_princ, tkt_princ, server_princ;43krb5_ccache ccache;44krb5_creds *cred, mcred;45krb5_auth_context auth_con;46krb5_data apreq;47krb5_error_code ret, code;48const char *tkt_name, *server_name, *emsg;4950/* Parse arguments. */51if (argc < 2 || argc > 3) {52fprintf(stderr, "Usage: rdreq tktname [servername]\n");53exit(1);54}55tkt_name = argv[1];56server_name = argv[2];5758if (krb5_init_context(&context) != 0)59abort();6061/* Parse the requested principal names. */62if (krb5_parse_name(context, tkt_name, &tkt_princ) != 0)63abort();64if (server_name != NULL) {65if (krb5_parse_name(context, server_name, &server_princ) != 0)66abort();67server_princ->type = KRB5_NT_SRV_HST;68} else {69server_princ = NULL;70}7172/* Produce an AP-REQ message. */73if (krb5_cc_default(context, &ccache) != 0)74abort();75if (krb5_cc_get_principal(context, ccache, &client_princ) != 0)76abort();77memset(&mcred, 0, sizeof(mcred));78mcred.client = client_princ;79mcred.server = tkt_princ;80if (krb5_get_credentials(context, 0, ccache, &mcred, &cred) != 0)81abort();82auth_con = NULL;83if (krb5_mk_req_extended(context, &auth_con, 0, NULL, cred, &apreq) != 0)84abort();8586/* Consume the AP-REQ message without using a replay cache. */87krb5_auth_con_free(context, auth_con);88if (krb5_auth_con_init(context, &auth_con) != 0)89abort();90if (krb5_auth_con_setflags(context, auth_con, 0) != 0)91abort();92ret = krb5_rd_req(context, &auth_con, &apreq, server_princ, NULL, NULL,93NULL);9495/* Display the result. */96if (ret) {97code = ret - ERROR_TABLE_BASE_krb5;98if (code < 0 || code > 127)99code = 60; /* KRB_ERR_GENERIC */100emsg = krb5_get_error_message(context, ret);101printf("%d %s\n", code, emsg);102krb5_free_error_message(context, emsg);103} else {104printf("0 success\n");105}106107krb5_free_data_contents(context, &apreq);108assert(apreq.length == 0);109krb5_auth_con_free(context, auth_con);110krb5_free_creds(context, cred);111krb5_cc_close(context, ccache);112krb5_free_principal(context, client_princ);113krb5_free_principal(context, tkt_princ);114krb5_free_principal(context, server_princ);115krb5_free_context(context);116return 0;117}118119120