Path: blob/main/crypto/krb5/src/appl/user_user/server.c
34890 views
1/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */2/* appl/user_user/server.c - One end of user-user client-server pair */3/*4* Copyright 1991 by the Massachusetts Institute of Technology.5* All Rights Reserved.6*7* Export of this software from the United States of America may8* require a specific license from the United States Government.9* It is the responsibility of any person or organization contemplating10* export to obtain such a license before exporting.11*12* WITHIN THAT CONSTRAINT, permission to use, copy, modify, and13* distribute this software and its documentation for any purpose and14* without fee is hereby granted, provided that the above copyright15* notice appear in all copies and that both that copyright notice and16* this permission notice appear in supporting documentation, and that17* the name of M.I.T. not be used in advertising or publicity pertaining18* to distribution of the software without specific, written prior19* permission. Furthermore if you modify this software you must label20* your software as modified software and not distribute it in such a21* fashion that it might be confused with the original M.I.T. software.22* M.I.T. makes no representations about the suitability of23* this software for any purpose. It is provided "as is" without express24* or implied warranty.25*/2627#include "k5-int.h"28#include "port-sockets.h"29#include "com_err.h"3031#include <sys/types.h>32#include <sys/socket.h>33#include <netinet/in.h>34#include <arpa/inet.h>35#include <netdb.h>36#include <unistd.h>37#include <fcntl.h>3839/* fd 0 is a tcp socket used to talk to the client */4041int42main(int argc, char *argv[])43{44krb5_data pname_data, tkt_data;45int sock = 0;46socklen_t l;47int retval;48struct sockaddr_in l_inaddr, f_inaddr; /* local, foreign address */49krb5_creds creds, *new_creds;50krb5_ccache cc;51krb5_data msgtext, msg;52krb5_context context;53krb5_auth_context auth_context = NULL;5455#ifndef DEBUG56freopen("/tmp/uu-server.log", "w", stderr);57#endif5859retval = krb5_init_context(&context);60if (retval) {61com_err(argv[0], retval, "while initializing krb5");62exit(1);63}6465#ifdef DEBUG66{67int one = 1;68int acc;69struct servent *sp;70socklen_t namelen = sizeof(f_inaddr);7172if ((sock = socket(PF_INET, SOCK_STREAM, 0)) < 0) {73com_err("uu-server", errno, "creating socket");74exit(3);75}7677l_inaddr.sin_family = AF_INET;78l_inaddr.sin_addr.s_addr = 0;79if (argc == 2) {80l_inaddr.sin_port = htons(atoi(argv[1]));81} else {82if (!(sp = getservbyname("uu-sample", "tcp"))) {83com_err("uu-server", 0, "can't find uu-sample/tcp service");84exit(3);85}86l_inaddr.sin_port = sp->s_port;87}8889(void) setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char *)&one, sizeof (one));90if (bind(sock, (struct sockaddr *)&l_inaddr, sizeof(l_inaddr))) {91com_err("uu-server", errno, "binding socket");92exit(3);93}94if (listen(sock, 1) == -1) {95com_err("uu-server", errno, "listening");96exit(3);97}9899printf("Server started\n");100fflush(stdout);101102if ((acc = accept(sock, (struct sockaddr *)&f_inaddr, &namelen)) == -1) {103com_err("uu-server", errno, "accepting");104exit(3);105}106dup2(acc, 0);107close(sock);108sock = 0;109}110#endif111112/* principal name must be sent null-terminated. */113retval = krb5_read_message(context, (krb5_pointer) &sock, &pname_data);114if (retval || pname_data.length == 0 ||115pname_data.data[pname_data.length - 1] != '\0') {116com_err ("uu-server", retval, "reading pname");117return 2;118}119120retval = krb5_read_message(context, (krb5_pointer) &sock, &tkt_data);121if (retval) {122com_err ("uu-server", retval, "reading ticket data");123return 2;124}125126retval = krb5_cc_default(context, &cc);127if (retval) {128com_err("uu-server", retval, "getting credentials cache");129return 4;130}131132memset (&creds, 0, sizeof(creds));133retval = krb5_cc_get_principal(context, cc, &creds.client);134if (retval) {135com_err("uu-client", retval, "getting principal name");136return 6;137}138139/* client sends it already null-terminated. */140printf ("uu-server: client principal is \"%s\".\n", pname_data.data);141142retval = krb5_parse_name(context, pname_data.data, &creds.server);143if (retval) {144com_err("uu-server", retval, "parsing client name");145return 3;146}147148creds.second_ticket = tkt_data;149printf ("uu-server: client ticket is %d bytes.\n",150creds.second_ticket.length);151152retval = krb5_get_credentials(context, KRB5_GC_USER_USER, cc,153&creds, &new_creds);154if (retval) {155com_err("uu-server", retval, "getting user-user ticket");156return 5;157}158159#ifndef DEBUG160l = sizeof(f_inaddr);161if (getpeername(0, (struct sockaddr *)&f_inaddr, &l) == -1)162{163com_err("uu-server", errno, "getting client address");164return 6;165}166#endif167l = sizeof(l_inaddr);168if (getsockname(0, (struct sockaddr *)&l_inaddr, &l) == -1)169{170com_err("uu-server", errno, "getting local address");171return 6;172}173174/* send a ticket/authenticator to the other side, so it can get the key175we're using for the krb_safe below. */176177retval = krb5_auth_con_init(context, &auth_context);178if (retval) {179com_err("uu-server", retval, "making auth_context");180return 8;181}182183retval = krb5_auth_con_setflags(context, auth_context,184KRB5_AUTH_CONTEXT_DO_SEQUENCE);185if (retval) {186com_err("uu-server", retval, "initializing the auth_context flags");187return 8;188}189190retval =191krb5_auth_con_genaddrs(context, auth_context, sock,192KRB5_AUTH_CONTEXT_GENERATE_LOCAL_FULL_ADDR |193KRB5_AUTH_CONTEXT_GENERATE_REMOTE_FULL_ADDR);194if (retval) {195com_err("uu-server", retval, "generating addrs for auth_context");196return 9;197}198199#if 1200retval = krb5_mk_req_extended(context, &auth_context,201AP_OPTS_USE_SESSION_KEY,202NULL, new_creds, &msg);203if (retval) {204com_err("uu-server", retval, "making AP_REQ");205return 8;206}207retval = krb5_write_message(context, (krb5_pointer) &sock, &msg);208#else209retval = krb5_sendauth(context, &auth_context, (krb5_pointer)&sock, "???",2100, 0,211AP_OPTS_MUTUAL_REQUIRED | AP_OPTS_USE_SESSION_KEY,212NULL, &creds, cc, NULL, NULL, NULL);213#endif214if (retval)215goto cl_short_wrt;216217free(msg.data);218219msgtext.length = 32;220msgtext.data = "Hello, other end of connection.";221222retval = krb5_mk_safe(context, auth_context, &msgtext, &msg, NULL);223if (retval) {224com_err("uu-server", retval, "encoding message to client");225return 6;226}227228retval = krb5_write_message(context, (krb5_pointer) &sock, &msg);229if (retval) {230cl_short_wrt:231com_err("uu-server", retval, "writing message to client");232return 7;233}234235236krb5_free_data_contents(context, &msg);237krb5_free_data_contents(context, &pname_data);238/* tkt_data freed with creds */239krb5_free_cred_contents(context, &creds);240krb5_free_creds(context, new_creds);241krb5_cc_close(context, cc);242krb5_auth_con_free(context, auth_context);243krb5_free_context(context);244return 0;245}246247248