source: trunk/third/ksrvutil/kadm_net.c @ 11760

Revision 11760, 3.6 KB checked in by ghudson, 26 years ago (diff)
Some CNS libkadm sources to build the CNS kpasswd against. In some files, KRB_INT32 needed to be redefined to KRB4_32 for krb5 build system.
Line 
1/*
2 * kadm_net.c
3 *
4 * Copyright 1988 by the Massachusetts Institute of Technology.
5 *
6 * For copying and distribution information, please see the file
7 * <mit-copyright.h>.
8 *
9 * Kerberos administration server client-side network access routines
10 * These routines do actual network traffic, in a machine dependent manner.
11 */
12
13#include <mit-copyright.h>
14
15#define DEFINE_SOCKADDR         /* Ask krb.h for struct sockaddr, etc */
16#include "krb.h"
17
18#include <errno.h>
19#include <signal.h>
20#include <kadm.h>
21#include <kadm_err.h>
22#include <krbports.h>
23
24#ifndef NULL
25#define NULL 0
26#endif
27
28#ifdef _WINDOWS
29#define SIGNAL(s, f) 0
30#endif
31
32#ifndef _WINDOWS
33#define SIGNAL(s, f) signal(s, f)
34extern int errno;
35#endif
36
37
38extern Kadm_Client client_parm;
39extern int default_client_port;
40
41static sigtype (*opipe)();
42
43int kadm_cli_conn()
44{                                       /* this connects and sets my_addr */
45    int on = 1;
46
47    if ((client_parm.admin_fd =
48         socket(client_parm.admin_addr.sin_family, SOCK_STREAM,0)) < 0)
49        return KADM_NO_SOCK;            /* couldnt create the socket */
50    if (connect(client_parm.admin_fd,
51                (struct sockaddr *) & client_parm.admin_addr,
52                sizeof(client_parm.admin_addr))) {
53        (void) closesocket(client_parm.admin_fd);
54        client_parm.admin_fd = -1;
55
56        /* The V4 kadmind port number is 751.  The RFC assigned
57           number, for V5, is 749.  Sometimes the entry in
58           /etc/services on a client machine will say 749, but the
59           server may be listening on port 751.  We try to partially
60           cope by automatically falling back to try port 751 if we
61           don't get a reply on port we are using.  */
62        if (client_parm.admin_addr.sin_port != htons(KADM_PORT)
63             && default_client_port) {
64            client_parm.admin_addr.sin_port = htons(KADM_PORT);
65            return kadm_cli_conn();
66        }
67
68        return KADM_NO_CONN;            /* couldnt get the connect */
69    }
70    opipe = SIGNAL(SIGPIPE, SIG_IGN);
71    client_parm.my_addr_len = sizeof(client_parm.my_addr);
72    if (getsockname(client_parm.admin_fd,
73                    (struct sockaddr *) & client_parm.my_addr,
74                    &client_parm.my_addr_len) < 0) {
75        (void) closesocket(client_parm.admin_fd);
76        client_parm.admin_fd = -1;
77        (void) SIGNAL(SIGPIPE, opipe);
78        return KADM_NO_HERE;            /* couldnt find out who we are */
79    }
80    if (setsockopt(client_parm.admin_fd, SOL_SOCKET, SO_KEEPALIVE, (char *)&on,
81                   sizeof(on)) < 0) {
82        (void) closesocket(client_parm.admin_fd);
83        client_parm.admin_fd = -1;
84        (void) SIGNAL(SIGPIPE, opipe);
85        return KADM_NO_CONN;            /* XXX */
86    }
87    return KADM_SUCCESS;
88}
89
90void kadm_cli_disconn()
91{
92    (void) closesocket(client_parm.admin_fd);
93    (void) SIGNAL(SIGPIPE, opipe);
94    return;
95}
96
97int kadm_cli_out(dat, dat_len, ret_dat, ret_siz)
98u_char *dat;
99int dat_len;
100u_char **ret_dat;
101int *ret_siz;
102{
103        u_short dlen;
104        int retval;
105        extern char *malloc();
106
107        dlen = (u_short) dat_len;
108
109        if (dat_len != (int)dlen)
110                return (KADM_NO_ROOM);
111
112        dlen = htons(dlen);
113        if (krb_net_write(client_parm.admin_fd, (char *) &dlen,
114                          sizeof(u_short)) < 0)
115                return (SOCKET_ERRNO);  /* XXX */
116
117        if (krb_net_write(client_parm.admin_fd, (char *) dat, dat_len) < 0)
118                return (SOCKET_ERRNO);  /* XXX */
119
120        if (retval = krb_net_read(client_parm.admin_fd, (char *) &dlen,
121                                  sizeof(u_short)) != sizeof(u_short)) {
122            if (retval < 0)
123                return(SOCKET_ERRNO);   /* XXX */
124            else
125                return(EPIPE);          /* short read ! */
126        }
127
128        dlen = ntohs(dlen);
129        *ret_dat = (u_char *)malloc((unsigned)dlen);
130        if (!*ret_dat)
131            return(KADM_NOMEM);
132
133        if (retval = krb_net_read(client_parm.admin_fd, (char *) *ret_dat,
134                                  (int) dlen) != dlen) {
135            if (retval < 0)
136                return(SOCKET_ERRNO);   /* XXX */
137            else
138                return(EPIPE);          /* short read ! */
139        }
140        *ret_siz = (int) dlen;
141        return KADM_SUCCESS;
142}
Note: See TracBrowser for help on using the repository browser.