uu_server.c revision 55682
155682Smarkm/*
255682Smarkm * Copyright (c) 1997 - 1999 Kungliga Tekniska H�gskolan
355682Smarkm * (Royal Institute of Technology, Stockholm, Sweden).
455682Smarkm * All rights reserved.
555682Smarkm *
655682Smarkm * Redistribution and use in source and binary forms, with or without
755682Smarkm * modification, are permitted provided that the following conditions
855682Smarkm * are met:
955682Smarkm *
1055682Smarkm * 1. Redistributions of source code must retain the above copyright
1155682Smarkm *    notice, this list of conditions and the following disclaimer.
1255682Smarkm *
1355682Smarkm * 2. Redistributions in binary form must reproduce the above copyright
1455682Smarkm *    notice, this list of conditions and the following disclaimer in the
1555682Smarkm *    documentation and/or other materials provided with the distribution.
1655682Smarkm *
1755682Smarkm * 3. Neither the name of the Institute nor the names of its contributors
1855682Smarkm *    may be used to endorse or promote products derived from this software
1955682Smarkm *    without specific prior written permission.
2055682Smarkm *
2155682Smarkm * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
2255682Smarkm * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2355682Smarkm * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2455682Smarkm * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
2555682Smarkm * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2655682Smarkm * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2755682Smarkm * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2855682Smarkm * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2955682Smarkm * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3055682Smarkm * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3155682Smarkm * SUCH DAMAGE.
3255682Smarkm */
3355682Smarkm
3455682Smarkm#include "test_locl.h"
3555682SmarkmRCSID("$Id: uu_server.c,v 1.6 1999/12/16 10:32:44 assar Exp $");
3655682Smarkm
3755682Smarkmkrb5_context context;
3855682Smarkm
3955682Smarkmstatic int
4055682Smarkmproto (int sock, const char *service)
4155682Smarkm{
4255682Smarkm    struct sockaddr_in remote, local;
4355682Smarkm    int addrlen;
4455682Smarkm    krb5_address remote_addr, local_addr;
4555682Smarkm    krb5_ccache ccache;
4655682Smarkm    krb5_auth_context auth_context;
4755682Smarkm    krb5_error_code status;
4855682Smarkm    krb5_data packet;
4955682Smarkm    krb5_data data;
5055682Smarkm    krb5_data client_name;
5155682Smarkm    krb5_creds in_creds, *out_creds;
5255682Smarkm
5355682Smarkm    addrlen = sizeof(local);
5455682Smarkm    if (getsockname (sock, (struct sockaddr *)&local, &addrlen) < 0
5555682Smarkm	|| addrlen != sizeof(local))
5655682Smarkm	err (1, "getsockname)");
5755682Smarkm
5855682Smarkm    addrlen = sizeof(remote);
5955682Smarkm    if (getpeername (sock, (struct sockaddr *)&remote, &addrlen) < 0
6055682Smarkm	|| addrlen != sizeof(remote))
6155682Smarkm	err (1, "getpeername");
6255682Smarkm
6355682Smarkm    status = krb5_auth_con_init (context, &auth_context);
6455682Smarkm    if (status)
6555682Smarkm	errx (1, "krb5_auth_con_init: %s",
6655682Smarkm	      krb5_get_err_text(context, status));
6755682Smarkm
6855682Smarkm    local_addr.addr_type = AF_INET;
6955682Smarkm    local_addr.address.length = sizeof(local.sin_addr);
7055682Smarkm    local_addr.address.data   = &local.sin_addr;
7155682Smarkm
7255682Smarkm    remote_addr.addr_type = AF_INET;
7355682Smarkm    remote_addr.address.length = sizeof(remote.sin_addr);
7455682Smarkm    remote_addr.address.data   = &remote.sin_addr;
7555682Smarkm
7655682Smarkm    status = krb5_auth_con_setaddrs (context,
7755682Smarkm				     auth_context,
7855682Smarkm				     &local_addr,
7955682Smarkm				     &remote_addr);
8055682Smarkm    if (status)
8155682Smarkm	errx (1, "krb5_auth_con_setaddr: %s",
8255682Smarkm	      krb5_get_err_text(context, status));
8355682Smarkm
8455682Smarkm    status = krb5_read_message(context, &sock, &client_name);
8555682Smarkm    if(status)
8655682Smarkm	krb5_err(context, 1, status, "krb5_read_message");
8755682Smarkm
8855682Smarkm    memset(&in_creds, 0, sizeof(in_creds));
8955682Smarkm    status = krb5_cc_default(context, &ccache);
9055682Smarkm    status = krb5_cc_get_principal(context, ccache, &in_creds.client);
9155682Smarkm
9255682Smarkm    status = krb5_read_message(context, &sock, &in_creds.second_ticket);
9355682Smarkm    if(status)
9455682Smarkm	krb5_err(context, 1, status, "krb5_read_message");
9555682Smarkm
9655682Smarkm    status = krb5_parse_name(context, client_name.data, &in_creds.server);
9755682Smarkm    if(status)
9855682Smarkm	krb5_err(context, 1, status, "krb5_parse_name");
9955682Smarkm
10055682Smarkm    status = krb5_get_credentials(context, KRB5_GC_USER_USER, ccache,
10155682Smarkm				  &in_creds, &out_creds);
10255682Smarkm    if(status)
10355682Smarkm	krb5_err(context, 1, status, "krb5_get_credentials");
10455682Smarkm
10555682Smarkm    status = krb5_cc_default(context, &ccache);
10655682Smarkm
10755682Smarkm    status = krb5_sendauth(context,
10855682Smarkm			   &auth_context,
10955682Smarkm			   &sock,
11055682Smarkm			   VERSION,
11155682Smarkm			   in_creds.client,
11255682Smarkm			   in_creds.server,
11355682Smarkm			   AP_OPTS_USE_SESSION_KEY,
11455682Smarkm			   NULL,
11555682Smarkm			   out_creds,
11655682Smarkm			   ccache,
11755682Smarkm			   NULL,
11855682Smarkm			   NULL,
11955682Smarkm			   NULL);
12055682Smarkm
12155682Smarkm    if (status)
12255682Smarkm	krb5_err(context, 1, status, "krb5_sendauth");
12355682Smarkm
12455682Smarkm    fprintf (stderr, "User is `%.*s'\n", (int)client_name.length,
12555682Smarkm	    (char *)client_name.data);
12655682Smarkm
12755682Smarkm    krb5_data_zero (&data);
12855682Smarkm    krb5_data_zero (&packet);
12955682Smarkm
13055682Smarkm    status = krb5_read_message(context, &sock, &packet);
13155682Smarkm    if(status)
13255682Smarkm	krb5_err(context, 1, status, "krb5_read_message");
13355682Smarkm
13455682Smarkm    status = krb5_rd_safe (context,
13555682Smarkm			   auth_context,
13655682Smarkm			   &packet,
13755682Smarkm			   &data,
13855682Smarkm			   NULL);
13955682Smarkm    if (status)
14055682Smarkm	errx (1, "krb5_rd_safe: %s",
14155682Smarkm	      krb5_get_err_text(context, status));
14255682Smarkm
14355682Smarkm    fprintf (stderr, "safe packet: %.*s\n", (int)data.length,
14455682Smarkm	    (char *)data.data);
14555682Smarkm
14655682Smarkm    status = krb5_read_message(context, &sock, &packet);
14755682Smarkm    if(status)
14855682Smarkm	krb5_err(context, 1, status, "krb5_read_message");
14955682Smarkm
15055682Smarkm    status = krb5_rd_priv (context,
15155682Smarkm			   auth_context,
15255682Smarkm			   &packet,
15355682Smarkm			   &data,
15455682Smarkm			   NULL);
15555682Smarkm    if (status)
15655682Smarkm	errx (1, "krb5_rd_priv: %s",
15755682Smarkm	      krb5_get_err_text(context, status));
15855682Smarkm
15955682Smarkm    fprintf (stderr, "priv packet: %.*s\n", (int)data.length,
16055682Smarkm	    (char *)data.data);
16155682Smarkm
16255682Smarkm    return 0;
16355682Smarkm}
16455682Smarkm
16555682Smarkmstatic int
16655682Smarkmdoit (int port, const char *service)
16755682Smarkm{
16855682Smarkm    int sock, sock2;
16955682Smarkm    struct sockaddr_in my_addr;
17055682Smarkm    int one = 1;
17155682Smarkm
17255682Smarkm    sock = socket (AF_INET, SOCK_STREAM, 0);
17355682Smarkm    if (sock < 0)
17455682Smarkm	err (1, "socket");
17555682Smarkm
17655682Smarkm    memset (&my_addr, 0, sizeof(my_addr));
17755682Smarkm    my_addr.sin_family      = AF_INET;
17855682Smarkm    my_addr.sin_port        = port;
17955682Smarkm    my_addr.sin_addr.s_addr = INADDR_ANY;
18055682Smarkm
18155682Smarkm    if (setsockopt (sock, SOL_SOCKET, SO_REUSEADDR,
18255682Smarkm		    (void *)&one, sizeof(one)) < 0)
18355682Smarkm	warn ("setsockopt SO_REUSEADDR");
18455682Smarkm
18555682Smarkm    if (bind (sock, (struct sockaddr *)&my_addr, sizeof(my_addr)) < 0)
18655682Smarkm	err (1, "bind");
18755682Smarkm
18855682Smarkm    if (listen (sock, 1) < 0)
18955682Smarkm	err (1, "listen");
19055682Smarkm
19155682Smarkm    sock2 = accept (sock, NULL, NULL);
19255682Smarkm    if (sock2 < 0)
19355682Smarkm	err (1, "accept");
19455682Smarkm
19555682Smarkm    return proto (sock2, service);
19655682Smarkm}
19755682Smarkm
19855682Smarkmint
19955682Smarkmmain(int argc, char **argv)
20055682Smarkm{
20155682Smarkm    int port = server_setup(&context, argc, argv);
20255682Smarkm    return doit (port, service);
20355682Smarkm}
204