16059Samurai/*	$NetBSD: mk_req.c,v 1.2 2017/01/28 21:31:49 christos Exp $	*/
26059Samurai
36059Samurai/*
46059Samurai * Copyright (c) 1997 - 2004 Kungliga Tekniska H��gskolan
56059Samurai * (Royal Institute of Technology, Stockholm, Sweden).
66059Samurai * All rights reserved.
76059Samurai *
86059Samurai * Redistribution and use in source and binary forms, with or without
96059Samurai * modification, are permitted provided that the following conditions
106059Samurai * are met:
116059Samurai *
126059Samurai * 1. Redistributions of source code must retain the above copyright
136059Samurai *    notice, this list of conditions and the following disclaimer.
146059Samurai *
156059Samurai * 2. Redistributions in binary form must reproduce the above copyright
166059Samurai *    notice, this list of conditions and the following disclaimer in the
176059Samurai *    documentation and/or other materials provided with the distribution.
186059Samurai *
198857Srgrimes * 3. Neither the name of the Institute nor the names of its contributors
2026326Sbrian *    may be used to endorse or promote products derived from this software
218857Srgrimes *    without specific prior written permission.
226059Samurai *
2313379Sphk * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
246059Samurai * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
256735Samurai * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2613385Speter * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
2713379Sphk * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2826031Sbrian * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2926031Sbrian * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3026031Sbrian * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3126031Sbrian * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3226031Sbrian * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3326031Sbrian * SUCH DAMAGE.
3426031Sbrian */
356059Samurai
366059Samurai#include "krb5_locl.h"
376059Samurai
386059SamuraiKRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
396059Samuraikrb5_mk_req_exact(krb5_context context,
4013389Sphk		  krb5_auth_context *auth_context,
416059Samurai		  const krb5_flags ap_req_options,
4226031Sbrian		  const krb5_principal server,
436059Samurai		  krb5_data *in_data,
4426142Sbrian		  krb5_ccache ccache,
456059Samurai		  krb5_data *outbuf)
4625630Sbrian{
4725630Sbrian    krb5_error_code ret;
486059Samurai    krb5_creds this_cred, *cred;
496059Samurai
506059Samurai    memset(&this_cred, 0, sizeof(this_cred));
516059Samurai
526059Samurai    ret = krb5_cc_get_principal(context, ccache, &this_cred.client);
5326031Sbrian
546735Samurai    if(ret)
556059Samurai	return ret;
566059Samurai
576059Samurai    ret = krb5_copy_principal (context, server, &this_cred.server);
586059Samurai    if (ret) {
5910528Samurai	krb5_free_cred_contents (context, &this_cred);
606735Samurai	return ret;
6114418Sache    }
626059Samurai
6322973Sphk    this_cred.times.endtime = 0;
6422973Sphk    if (auth_context && *auth_context && (*auth_context)->keytype)
656059Samurai	this_cred.session.keytype = (*auth_context)->keytype;
6623603Sache
676059Samurai    ret = krb5_get_credentials (context, 0, ccache, &this_cred, &cred);
686059Samurai    krb5_free_cred_contents(context, &this_cred);
696059Samurai    if (ret)
706059Samurai	return ret;
7110528Samurai
726059Samurai    ret = krb5_mk_req_extended (context,
736059Samurai				auth_context,
746059Samurai				ap_req_options,
756059Samurai				in_data,
766059Samurai				cred,
776059Samurai				outbuf);
786059Samurai    krb5_free_creds(context, cred);
796059Samurai    return ret;
806059Samurai}
816059Samurai
826059SamuraiKRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
836059Samuraikrb5_mk_req(krb5_context context,
846059Samurai	    krb5_auth_context *auth_context,
8525566Sbrian	    const krb5_flags ap_req_options,
8614418Sache	    const char *service,
8714418Sache	    const char *hostname,
886059Samurai	    krb5_data *in_data,
896059Samurai	    krb5_ccache ccache,
906059Samurai	    krb5_data *outbuf)
916059Samurai{
926059Samurai    krb5_error_code ret;
936059Samurai    char **realms;
946059Samurai    char *real_hostname;
956059Samurai    krb5_principal server;
966764Samurai
9726142Sbrian    ret = krb5_expand_hostname_realms (context, hostname,
986059Samurai				       &real_hostname, &realms);
996059Samurai    if (ret)
1006059Samurai	return ret;
1016059Samurai
1026059Samurai    ret = krb5_build_principal (context, &server,
1036059Samurai				strlen(*realms),
1046059Samurai				*realms,
1056059Samurai				service,
1066059Samurai				real_hostname,
1076059Samurai				NULL);
1086059Samurai    free (real_hostname);
1096059Samurai    krb5_free_host_realm (context, realms);
1106059Samurai    if (ret)
11120120Snate	return ret;
11220120Snate    ret = krb5_mk_req_exact (context, auth_context, ap_req_options,
11325908Sbrian			     server, in_data, ccache, outbuf);
11425908Sbrian    krb5_free_principal (context, server);
11520120Snate    return ret;
11610528Samurai}
1176059Samurai