Path: blob/main/crypto/heimdal/appl/test/uu_client.c
34889 views
/*1* Copyright (c) 1997 - 2000 Kungliga Tekniska Högskolan2* (Royal Institute of Technology, Stockholm, Sweden).3* All rights reserved.4*5* Redistribution and use in source and binary forms, with or without6* modification, are permitted provided that the following conditions7* are met:8*9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11*12* 2. Redistributions in binary form must reproduce the above copyright13* notice, this list of conditions and the following disclaimer in the14* documentation and/or other materials provided with the distribution.15*16* 3. Neither the name of the Institute nor the names of its contributors17* may be used to endorse or promote products derived from this software18* without specific prior written permission.19*20* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND21* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE22* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE23* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE24* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL25* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS26* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)27* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT28* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY29* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF30* SUCH DAMAGE.31*/3233#include "test_locl.h"34RCSID("$Id$");3536krb5_context context;3738static int39proto (int sock, const char *hostname, const char *service)40{41struct sockaddr_in remote, local;42socklen_t addrlen;43krb5_address remote_addr, local_addr;44krb5_context context;45krb5_ccache ccache;46krb5_auth_context auth_context;47krb5_error_code status;48krb5_principal client;49krb5_data data;50krb5_data packet;51krb5_creds mcred, cred;52krb5_ticket *ticket;5354addrlen = sizeof(local);55if (getsockname (sock, (struct sockaddr *)&local, &addrlen) < 056|| addrlen != sizeof(local))57err (1, "getsockname(%s)", hostname);5859addrlen = sizeof(remote);60if (getpeername (sock, (struct sockaddr *)&remote, &addrlen) < 061|| addrlen != sizeof(remote))62err (1, "getpeername(%s)", hostname);6364status = krb5_init_context(&context);65if (status)66errx(1, "krb5_init_context failed: %d", status);6768status = krb5_cc_default (context, &ccache);69if (status)70krb5_err(context, 1, status, "krb5_cc_default");7172status = krb5_auth_con_init (context, &auth_context);73if (status)74krb5_err(context, 1, status, "krb5_auth_con_init");7576local_addr.addr_type = AF_INET;77local_addr.address.length = sizeof(local.sin_addr);78local_addr.address.data = &local.sin_addr;7980remote_addr.addr_type = AF_INET;81remote_addr.address.length = sizeof(remote.sin_addr);82remote_addr.address.data = &remote.sin_addr;8384status = krb5_auth_con_setaddrs (context,85auth_context,86&local_addr,87&remote_addr);88if (status)89krb5_err(context, 1, status, "krb5_auth_con_setaddr");9091krb5_cc_clear_mcred(&mcred);9293status = krb5_cc_get_principal(context, ccache, &client);94if(status)95krb5_err(context, 1, status, "krb5_cc_get_principal");96status = krb5_make_principal(context, &mcred.server,97krb5_principal_get_realm(context, client),98"krbtgt",99krb5_principal_get_realm(context, client),100NULL);101if(status)102krb5_err(context, 1, status, "krb5_make_principal");103mcred.client = client;104105status = krb5_cc_retrieve_cred(context, ccache, 0, &mcred, &cred);106if(status)107krb5_err(context, 1, status, "krb5_cc_retrieve_cred");108109{110char *client_name;111krb5_data data;112status = krb5_unparse_name(context, cred.client, &client_name);113if(status)114krb5_err(context, 1, status, "krb5_unparse_name");115data.data = client_name;116data.length = strlen(client_name) + 1;117status = krb5_write_message(context, &sock, &data);118if(status)119krb5_err(context, 1, status, "krb5_write_message");120free(client_name);121}122123status = krb5_write_message(context, &sock, &cred.ticket);124if(status)125krb5_err(context, 1, status, "krb5_write_message");126127status = krb5_auth_con_setuserkey(context, auth_context, &cred.session);128if(status)129krb5_err(context, 1, status, "krb5_auth_con_setuserkey");130131status = krb5_recvauth(context, &auth_context, &sock,132VERSION, client, 0, NULL, &ticket);133134if (status)135krb5_err(context, 1, status, "krb5_recvauth");136137if (ticket->ticket.authorization_data) {138AuthorizationData *authz;139int i;140141printf("Authorization data:\n");142143authz = ticket->ticket.authorization_data;144for (i = 0; i < authz->len; i++) {145printf("\ttype %d, length %lu\n",146authz->val[i].ad_type,147(unsigned long)authz->val[i].ad_data.length);148}149}150151data.data = "hej";152data.length = 3;153154krb5_data_zero (&packet);155156status = krb5_mk_safe (context,157auth_context,158&data,159&packet,160NULL);161if (status)162krb5_err(context, 1, status, "krb5_mk_safe");163164status = krb5_write_message(context, &sock, &packet);165if(status)166krb5_err(context, 1, status, "krb5_write_message");167168data.data = "hemligt";169data.length = 7;170171krb5_data_free (&packet);172173status = krb5_mk_priv (context,174auth_context,175&data,176&packet,177NULL);178if (status)179krb5_err(context, 1, status, "krb5_mk_priv");180181status = krb5_write_message(context, &sock, &packet);182if(status)183krb5_err(context, 1, status, "krb5_write_message");184return 0;185}186187int188main(int argc, char **argv)189{190int port = client_setup(&context, &argc, argv);191return client_doit (argv[argc], port, service, proto);192}193194195