File: pptpmanager.c

package info (click to toggle)
pptpd 1.1.2-1.4
  • links: PTS
  • area: main
  • in suites: woody
  • size: 636 kB
  • ctags: 510
  • sloc: ansic: 3,687; sh: 443; makefile: 81; perl: 70
file content (369 lines) | stat: -rw-r--r-- 9,509 bytes parent folder | download
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/*
 * pptpmanager.c
 *
 * Manages the PoPToP sessions.
 *
 * $Id: pptpmanager.c,v 1.1.1.1 1999/12/17 16:30:14 patl Exp $
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#ifdef __linux__
#define _GNU_SOURCE 1		/* broken arpa/inet.h */
#endif

#if HAVE_SYSLOG_H
#include <syslog.h>
#else
#include "our_syslog.h"
#endif

#include <errno.h>
#include <netdb.h>
#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/un.h>
#include <sys/wait.h>
#include <unistd.h>
#include <sys/time.h>
#include <fcntl.h>

#if HAVE_LIBWRAP
/* re-include, just in case HAVE_SYSLOG_H wasn't defined */
#include <syslog.h>
#include <tcpd.h>

int allow_severity = LOG_WARNING;
int deny_severity = LOG_WARNING;
#endif

#include "configfile.h"
#include "defaults.h"
#include "pptpctrl.h"
#include "pptpdefs.h"
#include "pptpmanager.h"
#include "compat.h"

/* command line arg variables */
extern char *pppdoptstr;
extern char *speedstr;
extern char *bindaddr;
extern int pptp_debug;

#if !defined(PPPD_IP_ALLOC)
extern char localIP[MAX_CONNECTIONS][16];
extern char remoteIP[MAX_CONNECTIONS][16];
extern u_int32_t ipx_min;

/* for now, this contains all relavant info about a call
 * we are assuming that the remote and local IP's never change
 * and are set at startup.
 */
struct callArray {
	pid_t pid;
	char pppRemote[16];
	char pppLocal[16];
};

/* global for signal handler */
static struct callArray clientArray[MAX_CONNECTIONS];

/* from IP parser */
extern int maxConnections;
#endif

/* local function prototypes */
static void connectCall(int clientSocket, int clientNumber);
static int createHostSocket(int *hostSocket);

static void pptp_reap_child(int sig)
{
	int chld, status;

	while ((chld = waitpid(-1, &status, WNOHANG)) > 0) {
#if !defined(PPPD_IP_ALLOC)
		int i;
		for (i = 0; i < maxConnections; i++)
			if (clientArray[i].pid == chld)
				break;
		if (i < maxConnections) {
			clientArray[i].pid = 0;
			if (pptp_debug)
				syslog(LOG_DEBUG, "MGR: Reaped child %d", chld);
		} else
			syslog(LOG_INFO, "MGR: Reaped unknown child %d", chld);
#else
		if (pptp_debug)
			syslog(LOG_DEBUG, "MGR: Reaped child %d", chld);
#endif
	}
	signal(SIGCHLD, pptp_reap_child);
}

int pptp_manager(int argc, char **argv)
{
	/* misc ints */
	int loop, firstOpen, ctrl_pid;
	socklen_t addrsize;

	/* for host stuff */
	int hostSocket;
	fd_set connSet;

	signal(SIGCHLD, pptp_reap_child);

	/* openlog() not required, done in pptpd.c */

	syslog(LOG_INFO, "MGR: Manager process started");

#if !defined(PPPD_IP_ALLOC)
	for (loop = 0; loop < maxConnections; loop++) {
		clientArray[loop].pid = 0;
	}
#endif

	/* Connect the host socket and activate it for listening */
	if (createHostSocket(&hostSocket) < 0) {
		syslog(LOG_ERR, "MGR: Couldn't create host socket");
		perror("createHostSocket");
		exit(-1);
	}

	FD_ZERO(&connSet);

	while (1) {
		firstOpen = -1;
		loop = 0;

#if !defined(PPPD_IP_ALLOC)
		while ((loop < maxConnections) && (firstOpen == -1)) {
			if ((clientArray[loop].pid == 0) && (firstOpen == -1))
				firstOpen = loop;
			loop++;
		}

		if (firstOpen == -1) {
			syslog(LOG_ERR, "MGR: No free connection slots or IPs - no more clients can connect!");
			fprintf(stderr, "No free connection slots or IPs available - no more clients can connect!\n");
			FD_CLR(hostSocket, &connSet);
		} else {
			FD_SET(hostSocket, &connSet);
		}
#else
		FD_SET(hostSocket, &connSet);
#endif

		/* wait for connection (or for SIGCHLD [EINTR]).  don't just loop on EINTR
		 * since hostSocket might not be in connSet (see above)
		 */
		if (select(hostSocket + 1, &connSet, NULL, NULL, NULL) < 0 && errno != EINTR) {
			syslog(LOG_ERR, "MGR: Error with manager select()!");
			perror("select");
			exit(-1);
		}

		if (FD_ISSET(hostSocket, &connSet)) {	/* A call came! */
			int clientSocket;
			struct sockaddr_in client_addr;

			/* Accept call and launch PPTPCTRL */
			addrsize = sizeof(client_addr);
			clientSocket = accept(hostSocket, (struct sockaddr *) &client_addr, &addrsize);

#if HAVE_LIBWRAP
			if (clientSocket != -1) {
				struct request_info r;
				request_init(&r, RQ_DAEMON, "pptpd", RQ_FILE, clientSocket, NULL);
				fromhost(&r);
				if (!hosts_access(&r)) {
					/* send a permission denied message? this is a tcp wrapper
					 * type deny so probably best to just drop it immediately like
					 * this, as tcp wrappers usually do.
					 */
					close(clientSocket);
					/* this would never be file descriptor 0, so use it as a error
					 * value
					 */
					clientSocket = 0;
				}
			}
#endif
			if (clientSocket == -1) {
				/* accept failed, but life goes on... */
				syslog(LOG_ERR, "MGR: accept() failed");
				perror("accept");
			} else if (clientSocket != 0) {

#ifndef HAVE_FORK
				switch (ctrl_pid = vfork()) {
#else
				switch (ctrl_pid = fork()) {
#endif
				case -1:	/* error */
					syslog(LOG_ERR, "MGR: fork() failed launching " PPTP_CTRL_BIN);
					close(clientSocket);
					break;

				case 0:	/* child */
					close(hostSocket);
					if (pptp_debug)
						syslog(LOG_DEBUG, "MGR: Launching " PPTP_CTRL_BIN " to handle client");
#if !defined(PPPD_IP_ALLOC)
					connectCall(clientSocket, firstOpen);
#else
					connectCall(clientSocket, 0);
#endif
					exit(1);
					/* NORETURN */
				default:	/* parent */
					close(clientSocket);
#if !defined(PPPD_IP_ALLOC)
					clientArray[firstOpen].pid = ctrl_pid;
#endif
					break;
				}
			}
		}		/* FD_ISSET(hostSocket, &connSet) */
	}			/* while (1) */
}				/* pptp_manager() */

/*
 * Author: Kevin Thayer
 * 
 * This creates a socket to listen on, sets the max # of pending connections and 
 * various other options.
 * 
 * Returns the fd of the host socket.
 * 
 * The function return values are:
 * 0 for sucessful
 * -1 for bad socket creation
 * -2 for bad socket options
 * -3 for bad bind
 * -4 for bad listen
 */
static int createHostSocket(int *hostSocket)
{
	int opt = 1;
	struct sockaddr_in address;
#ifdef HAVE_GETSERVBYNAME
	struct servent *serv;
#endif

	/* create the master socket and check it worked */
	if ((*hostSocket = socket(AF_INET, SOCK_STREAM, 0)) == 0)
		return -1;

	/* set master socket to allow daemon to be restarted with connections active  */
	if (setsockopt(*hostSocket, SOL_SOCKET, SO_REUSEADDR,
		       (char *) &opt, sizeof(opt)) < 0)
		return -2;

	/* set up socket */
	memset(&address, 0, sizeof(address));
	address.sin_family = AF_INET;
	if(bindaddr)
		address.sin_addr.s_addr = inet_addr(bindaddr);
	else
		address.sin_addr.s_addr = INADDR_ANY;
#ifdef HAVE_GETSERVBYNAME
	if ((serv = getservbyname("pptp", "tcp")) != NULL) {
		address.sin_port = serv->s_port;
	} else
#endif
		address.sin_port = htons(PPTP_PORT);

	/* bind the socket to the pptp port */
	if (bind(*hostSocket, (struct sockaddr *) &address, sizeof(address)) < 0)
		return -3;

	/* listen to as many connections as we can, but only accept() when a slot is free */
	if (listen(*hostSocket, SOMAXCONN) < 0)
		return -4;

	return 0;
}

/*
 * Author: Kevin Thayer
 * 
 * this routine sets up the arguments for the call handler and calls it.
 */

static void connectCall(int clientSocket, int clientNumber)
{

	char *ctrl_argv[16];	/* arguments for launching 'pptpctrl' binary */

	int pptpctrl_argc = 0;	/* count the number of arguments sent to pptpctrl */

	/* lame strings to hold passed args. */
	char ctrl_debug[16];
	char pppdoptfile_argv[16];
	char speedgiven_argv[16];

	/*
	 * Launch the CTRL manager binary; we send it some information such as
	 * speed and option file on the command line.
	 */

	ctrl_argv[pptpctrl_argc++] = PPTP_CTRL_BIN "                                ";

	/* Pass socket as stdin */
	dup2(clientSocket, 0);
	close(clientSocket);

	/* get argv set up */
	sprintf(ctrl_debug, "%d", pptp_debug);
	ctrl_argv[pptpctrl_argc++] = ctrl_debug;

	/* optionfile = TRUE or FALSE; so the CTRL manager knows whether to load a non-standard options file */
	sprintf(pppdoptfile_argv, "%d", pppdoptstr ? 1 : 0);
	ctrl_argv[pptpctrl_argc++] = pppdoptfile_argv;
	if (pppdoptstr) {
		/* send the option filename so the CTRL manager can launch pppd with this alternate file */
		ctrl_argv[pptpctrl_argc++] = pppdoptstr;
	}
	/* tell the ctrl manager whether we were given a speed */
	sprintf(speedgiven_argv, "%d", speedstr ? 1 : 0);
	ctrl_argv[pptpctrl_argc++] = speedgiven_argv;
	if (speedstr) {
		/* send the CTRL manager the speed of the connection so it can fire pppd at that speed */
		ctrl_argv[pptpctrl_argc++] = speedstr;
	}
#if PPPD_IP_ALLOC
	/* no local or remote address to specify */
	ctrl_argv[pptpctrl_argc++] = strdup("0");
	ctrl_argv[pptpctrl_argc++] = strdup("0");
	ctrl_argv[pptpctrl_argc++] = strdup("0");
#else
	/* specify local & remote addresses for this call */
	ctrl_argv[pptpctrl_argc++] = strdup("1");
	ctrl_argv[pptpctrl_argc++] = localIP[clientNumber];
	ctrl_argv[pptpctrl_argc++] = strdup("1");
	ctrl_argv[pptpctrl_argc++] = remoteIP[clientNumber];
	if(ipx_min != 0xffffffff) {
		ctrl_argv[pptpctrl_argc++] = strdup("1");
		sprintf((ctrl_argv[pptpctrl_argc++] = malloc(9)),
			"%08x", ipx_min + clientNumber);
	} else
		ctrl_argv[pptpctrl_argc++] = strdup("0");
#endif
	/* terminate argv array with a NULL */
	ctrl_argv[pptpctrl_argc] = NULL;
	pptpctrl_argc++;

	/* ok, args are setup: invoke the call handler */
	execvp(PPTP_CTRL_BIN, ctrl_argv);
	syslog(LOG_ERR, "MGR: Failed to exec " PPTP_CTRL_BIN "!");
	perror("execvp " PPTP_CTRL_BIN);
	exit(1);
}