Path: blob/main/crypto/heimdal/appl/test/tcp_server.c
34879 views
/*1* Copyright (c) 1997 - 1999 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 *service)40{41krb5_auth_context auth_context;42krb5_error_code status;43krb5_principal server;44krb5_ticket *ticket;45char *name;46char hostname[MAXHOSTNAMELEN];47krb5_data packet;48krb5_data data;49uint32_t len, net_len;50ssize_t n;5152status = krb5_auth_con_init (context, &auth_context);53if (status)54krb5_err (context, 1, status, "krb5_auth_con_init");5556status = krb5_auth_con_setaddrs_from_fd (context,57auth_context,58&sock);5960if (status)61krb5_err (context, 1, status, "krb5_auth_con_setaddrs_from_fd");6263if(gethostname (hostname, sizeof(hostname)) < 0)64krb5_err (context, 1, errno, "gethostname");6566status = krb5_sname_to_principal (context,67hostname,68service,69KRB5_NT_SRV_HST,70&server);71if (status)72krb5_err (context, 1, status, "krb5_sname_to_principal");7374status = krb5_recvauth (context,75&auth_context,76&sock,77VERSION,78server,790,80keytab,81&ticket);82if (status)83krb5_err (context, 1, status, "krb5_recvauth");8485status = krb5_unparse_name (context,86ticket->client,87&name);88if (status)89krb5_err (context, 1, status, "krb5_unparse_name");9091fprintf (stderr, "User is `%s'\n", name);92free (name);9394krb5_data_zero (&data);95krb5_data_zero (&packet);9697n = krb5_net_read (context, &sock, &net_len, 4);98if (n == 0)99krb5_errx (context, 1, "EOF in krb5_net_read");100if (n < 0)101krb5_err (context, 1, errno, "krb5_net_read");102103len = ntohl(net_len);104105krb5_data_alloc (&packet, len);106107n = krb5_net_read (context, &sock, packet.data, len);108if (n == 0)109krb5_errx (context, 1, "EOF in krb5_net_read");110if (n < 0)111krb5_err (context, 1, errno, "krb5_net_read");112113status = krb5_rd_safe (context,114auth_context,115&packet,116&data,117NULL);118if (status)119krb5_err (context, 1, status, "krb5_rd_safe");120121fprintf (stderr, "safe packet: %.*s\n", (int)data.length,122(char *)data.data);123124n = krb5_net_read (context, &sock, &net_len, 4);125if (n == 0)126krb5_errx (context, 1, "EOF in krb5_net_read");127if (n < 0)128krb5_err (context, 1, errno, "krb5_net_read");129130len = ntohl(net_len);131132krb5_data_alloc (&packet, len);133134n = krb5_net_read (context, &sock, packet.data, len);135if (n == 0)136krb5_errx (context, 1, "EOF in krb5_net_read");137if (n < 0)138krb5_err (context, 1, errno, "krb5_net_read");139140status = krb5_rd_priv (context,141auth_context,142&packet,143&data,144NULL);145if (status)146krb5_err (context, 1, status, "krb5_rd_priv");147148fprintf (stderr, "priv packet: %.*s\n", (int)data.length,149(char *)data.data);150151return 0;152}153154static int155doit (int port, const char *service)156{157mini_inetd (port, NULL);158159return proto (STDIN_FILENO, service);160}161162int163main(int argc, char **argv)164{165int port = server_setup(&context, argc, argv);166return doit (port, service);167}168169170