summaryrefslogtreecommitdiffstats
path: root/src/kadmin.old/server/adm_network.c
blob: 3ce8ac13eb52295f13e627e14a1459ea875a688f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*
 * kadmin/server/adm_network.c
 *
 * Copyright 1988 by the Massachusetts Institute of Technology.
 *
 * For copying and distribution information, please see the file
 * <mit-copyright.h>.
 *
 * Network Initialization/Shutdown Component of the 
 * Version 5 Administration network
 */

/* 
 * Sandia National Laboratories also makes no representations about the 
 * suitability of the modifications, or additions to this software for 
 * any purpose.  It is provided "as is" without express or implied warranty.
 */


/*
 *   adm_network.c
 */

#include <stdio.h>
#include "com_err.h"
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/param.h>
#include <signal.h>

#ifndef sigmask
#define sigmask(m)    (1 <<((m)-1))
#endif

#include <sys/socket.h>
#include <netinet/in.h>
#ifndef hpux
#include <arpa/inet.h>
#endif
#include <netdb.h>

#include "k5-int.h"
#include "adm_extern.h"

extern int errno;

#ifdef POSIX_SIGTYPE
#define SIGNAL_RETURN return
#else
#define SIGNAL_RETURN return(0)
#endif

krb5_error_code
closedown_network(prog)
const char *prog;
{
    if (client_server_info.server_socket == -1) return(1);

    (void) close(client_server_info.server_socket);
    client_server_info.server_socket = -1;
    return(0);
}

krb5_sigtype
doexit()
{
    exit_now = 1;
    SIGNAL_RETURN;
}

/*
 *  SIGCHLD brings us here
 */
krb5_sigtype
do_child()
{
    /*
     *  <sys/param.h> has been included, so BSD will be defined on
     * BSD systems
     */
#if BSD > 0 && BSD <= 43
#ifndef WEXITSTATUS
#define	WEXITSTATUS(w)	(w).w_retcode
#define WTERMSIG(w)	(w).w_termsig
#endif
    union wait	status;
#else
    int	status;
#endif
    int pid, i, j;

#ifdef OLD_SIGNALS
    signal(SIGCHLD, do_child);
#endif
    
    pid = wait(&status);
    if (pid < 0)
	SIGNAL_RETURN;
 
    for (i = 0; i < pidarraysize; i++)
	if (pidarray[i] == pid) {
			/* found it */
		for (j = i; j < pidarraysize-1; j++)
			/* copy others down */
			pidarray[j] = pidarray[j+1];
		pidarraysize--;
		if ( !WIFEXITED(status) ) {
			com_err("adm_network", 0, "child %d: termsig %d", 
				pid, WTERMSIG(status) );
			com_err("adm_network", 0, "retcode %d", 
				WEXITSTATUS(status));
		}

		SIGNAL_RETURN;
	}

    com_err("adm_network", 0, 
	"child %d not in list: termsig %d, retcode %d", pid,
	WTERMSIG(status), WEXITSTATUS(status));

    SIGNAL_RETURN;
}

krb5_error_code
setup_network(context, prog)
    krb5_context context;
    const char *prog;
{
    krb5_error_code retval;
    char server_host_name[MAXHOSTNAMELEN];
    krb5_sigtype     doexit(), do_child();
    struct servent *service_servent;
    struct hostent *service_hostent;
    int on = 1;
#ifndef OLD_SIGNALS
    struct sigaction new_act;

    new_act.sa_handler = doexit;
    sigemptyset(&new_act.sa_mask);
    sigaction(SIGINT, &new_act, 0);
    sigaction(SIGTERM, &new_act, 0);
    sigaction(SIGHUP, &new_act, 0);
    sigaction(SIGQUIT, &new_act, 0);
    sigaction(SIGALRM, &new_act, 0);
    new_act.sa_handler = SIG_IGN;
    sigaction(SIGPIPE, &new_act, 0);
    new_act.sa_handler = do_child;
    sigaction(SIGCHLD, &new_act, 0);
#else
    signal(SIGINT, doexit);
    signal(SIGTERM, doexit);
    signal(SIGHUP, doexit);
    signal(SIGQUIT, doexit);
    signal(SIGPIPE, SIG_IGN); /* get errors on write() */
    signal(SIGALRM, doexit);
    signal(SIGCHLD, do_child);
#endif
 
    client_server_info.name_of_service = malloc(768);
    if (!client_server_info.name_of_service) {
        com_err("setup_network", 0, 
		"adm_network: No Memory for name_of_service");
        return ENOMEM;
    }

    (void) sprintf(client_server_info.name_of_service, "%s%s%s%s%s",
                        CPWNAME, "/", realm, "@", realm);

#ifdef DEBUG
    fprintf(stderr, "client_server_info.name_of_service = %s\n",
		client_server_info.name_of_service);
#endif	/* DEBUG */

    if ((retval = krb5_parse_name(context, client_server_info.name_of_service,
                        &client_server_info.server))) {
        free(client_server_info.name_of_service);
	com_err( "setup_network", retval, 
		"adm_network: Unable to Parse Server Name");
	return retval;
    }

    if (gethostname(server_host_name, sizeof(server_host_name))) {
	retval = errno;
        krb5_free_principal(context, client_server_info.server);
        free(client_server_info.name_of_service);
	com_err( "setup_network", retval,
		"adm_network: Unable to Identify Who I am");
	return retval;
    }

    service_hostent = gethostbyname(server_host_name);
    if (!service_hostent) {
	retval = errno;
        free(client_server_info.name_of_service);
	com_err("setup_network", retval, "adm_network: Failed gethostname");
	return retval;
    }

#ifdef DEBUG
    fprintf(stderr, "Official host name = %s\n", service_hostent->h_name);
#endif	/* DEBUG */

    client_server_info.server_name.sin_family = AF_INET;

#ifdef unicos61
    memcpy((char *) &client_server_info.server_name.sin_addr,
		(char *) service_hostent->h_addr, service_hostent->h_length);
#else
    memcpy((char *) &client_server_info.server_name.sin_addr.s_addr,
		(char *) service_hostent->h_addr, service_hostent->h_length);
#endif /* unicos61 */

    client_server_info.server_socket = -1;

#ifdef DEBUG
    fprintf(stderr, "adm5_tcp_portname = %s\n", adm5_tcp_portname);
#endif	/* DEBUG */

    service_servent = getservbyname(adm5_tcp_portname, "tcp");
    if (admin_port) {
      client_server_info.server_name.sin_port = admin_port;
    } else if (service_servent) {
      client_server_info.server_name.sin_port = service_servent->s_port;
#ifdef DEBUG
      fprintf(stderr, "Official service name = %s\n", service_servent->s_name);
#endif	/* DEBUG */
    } else {
#ifdef ADM5_DEFAULT_PORT
      client_server_info.server_name.sin_port = htons(ADM5_DEFAULT_PORT);
      com_err("setup_network", 0, "adm_network: using default port %d",
	      ADM5_DEFAULT_PORT);
#else
      krb5_free_principal(client_server_info.server);
      free(client_server_info.name_of_service);
      com_err("setup_network", 0, "adm_network: %s/tcp service unknown", 
	      adm5_tcp_portname);
      return(1);
#endif
    }


    if ((client_server_info.server_socket = 
		socket(AF_INET, SOCK_STREAM, 0)) < 0) {
	retval = errno;
        krb5_free_principal(context, client_server_info.server);
        free(client_server_info.name_of_service);
	com_err("setup_network", retval, 
		"adm_network: Cannot create server socket.");
	return(1);
    }

#ifdef DEBUG
    fprintf(stderr, "Socket File Descriptor = %d\n", 
		client_server_info.server_socket);
    fprintf(stderr, "sin_family = %d\n", 
		client_server_info.server_name.sin_family);
    fprintf(stderr, "sin_port = %d\n", 
		client_server_info.server_name.sin_port);
    fprintf(stderr, "in_addr.s_addr = %s\n", 
		inet_ntoa( client_server_info.server_name.sin_addr ));
#endif	/* DEBUG */

    if (admin_port && admin_port != htons(ADM5_DEFAULT_PORT)) {
	 (void) setsockopt(client_server_info.server_socket, SOL_SOCKET, 
			   SO_REUSEADDR, (char *)&on, sizeof(on));
     }

    if (bind(client_server_info.server_socket,
		(struct sockaddr *) &client_server_info.server_name, 
		sizeof(client_server_info.server_name)) < 0) {
	retval = errno;
        krb5_free_principal(context, client_server_info.server);
        free(client_server_info.name_of_service);
	com_err("setup_network", retval, 
		"adm_network: Cannot bind server socket.");
	return(1);
    }

    return(0);
}