File: vde_plug.c

package info (click to toggle)
vdeplug4 4.0.1-5.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 464 kB
  • sloc: ansic: 4,280; makefile: 19
file content (423 lines) | stat: -rw-r--r-- 11,050 bytes parent folder | download | duplicates (3)
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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
/* 
 * Copyright (C) 2002-2016  Renzo Davoli, University of Bologna
 * Modified by Ludovico Gardenghi 2005
 * 
 * vde_plug: connect vde network together
 *
 * VDE is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; If not, see <http://www.gnu.org/licenses/>. 
 *
 */

#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <errno.h>
#include <unistd.h>
#include <stdint.h>
#include <getopt.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/uio.h>
#include <sys/utsname.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <pwd.h>
#include <grp.h>
#include <stdarg.h>
#include <poll.h>

#include <libvdeplug.h>
#include <vde_plug_log.h>
#include <vde_plug_iplog.h>
#include <openclosepidfile.h>
#include <selfsighandler.h>

#define VDE_IP_LOG_GROUP "vdeplug_iplog"
#define DEFAULT_DESCRIPTION "vdeplug:"

int vde_ip_log;

VDECONN *conn;
VDECONN *conn2;
VDESTREAM *vdestream;

#define ETH_ALEN 6
#define ETH_HDRLEN (ETH_ALEN+ETH_ALEN+2)

struct utsname me;
#define myname me.nodename

void vdeplug_err(void *opaque, int type, char *format,...)
{
	va_list args;

	if (isatty(STDERR_FILENO)) {
		fprintf(stderr, "%s: Packet length error ",myname);
		va_start(args, format);
		vfprintf(stderr, format, args);
		va_end(args);
		fprintf(stderr,"\n");
	}
}

ssize_t vdeplug_recv(void *opaque, void *buf, size_t count)
{
	VDECONN *conn=opaque;
	if (__builtin_expect(vde_ip_log != 0, 0))
		vde_ip_check(buf,count);
	return vde_send(conn,(char *)buf,count,0);
}


int checkuidgroup(uid_t uid, const char *group) {
	struct passwd *pw=getpwuid(uid);
	int ngroups=0;
	if (pw == NULL) return -1;
	if (getgrouplist(pw->pw_name, pw->pw_gid, NULL, &ngroups) < 0) {
		gid_t gids[ngroups];
		if (getgrouplist(pw->pw_name, pw->pw_gid, gids, &ngroups) == ngroups) {
			struct group *grp;
			int i;
			while ((grp=getgrent()) != NULL) {
				for (i=0; i<ngroups; i++) {
					if (grp->gr_gid == gids[i] && strcmp(grp->gr_name,group) == 0) {
						endgrent();
						return 1;
					}
				}
			}
			endgrent();
			return 0;
		}
	}
	return -1;
}

static void cleanup(void)
{
  vde_close(conn);
	if (conn2 != NULL) vde_close(conn2);
	if (vdestream != NULL) vdestream_close(vdestream);
	openclosepidfile(NULL);
}

unsigned char bufin[VDE_ETHBUFSIZE];

int plug2stream(void) {
	register ssize_t nx;
	struct pollfd pollv[] = {
		{STDIN_FILENO, POLLIN|POLLHUP},
		{vde_datafd(conn), POLLIN|POLLHUP},
		{vde_ctlfd(conn), POLLIN|POLLHUP},
	};

	vdestream = vdestream_open(conn,STDOUT_FILENO,vdeplug_recv,vdeplug_err);

	for(;;) {
		poll(pollv,3,-1);
		if (__builtin_expect(((pollv[0].revents | pollv[1].revents | pollv[2].revents) & POLLHUP ||
						pollv[2].revents & POLLIN),0))
			break;
		if (pollv[0].revents & POLLIN) {
			nx = read(STDIN_FILENO,bufin,sizeof(bufin));
			/* if POLLIN but not data it means that the stream has been
			 * closed at the other end */
			/*fprintf(stderr,"%s: RECV %d %x %x \n",myname,nx,bufin[0],bufin[1]);*/
			if (nx == 0)
				break;
			vdestream_recv(vdestream, bufin, nx);
		}
		if (pollv[1].revents & POLLIN) {
			nx = vde_recv(conn,bufin,VDE_ETHBUFSIZE,0);
			if (__builtin_expect((nx >= ETH_HDRLEN),1))
			{
				vdestream_send(vdestream, bufin, nx);
				/*fprintf(stderr,"%s: SENT %d %x %x \n",myname,nx,bufin[0],bufin[1]);*/
			} else if (nx<0)
				perror("vde_plug: recvfrom ");
		}
	}
	return(0);
}

int plug2cmd(char **argv) {
	int p2c[2],c2p[2];
	pid_t pid;
	if (pipe(p2c) < 0 || pipe(c2p) < 0) {
		perror("pipe open");
		return(1);
	}

	pid=fork();
	if (pid < 0) {
		perror("fork");
		exit(1);
	} else if (pid == 0) {
		dup2(p2c[0], STDIN_FILENO);
		dup2(c2p[1], STDOUT_FILENO);
		close(p2c[0]); close(p2c[1]);
		close(c2p[0]); close(c2p[1]);
		execvp(*argv, argv);
		exit(1);
	}
	dup2(c2p[0], STDIN_FILENO);
	dup2(p2c[1], STDOUT_FILENO);
	close(p2c[0]); close(p2c[1]);
	close(c2p[0]); close(c2p[1]);

	return plug2stream();
}

int plug2plug(void)
{
	register ssize_t nx;
	struct pollfd pollv[] = {
		{vde_datafd(conn), POLLIN|POLLHUP},
		{vde_datafd(conn2), POLLIN|POLLHUP},
		{vde_ctlfd(conn), POLLIN|POLLHUP},
		{vde_ctlfd(conn2), POLLIN|POLLHUP}
	};

	for(;;) {
		poll(pollv,4,-1);
		if ((pollv[0].revents | pollv[1].revents | pollv[2].revents | pollv[2].revents) & POLLHUP ||
				(pollv[2].revents | pollv[3].revents) & POLLIN)
			break;
		if (pollv[0].revents & POLLIN) {
			nx = vde_recv(conn, bufin, VDE_ETHBUFSIZE,0);
			if (__builtin_expect((nx >= ETH_HDRLEN),1)) {
				vde_send(conn2, bufin, nx, 0);
				/*fprintf(stderr,"0->1 %d ",nx);*/
			} else if (nx<0)
				break;
		}
		if (pollv[1].revents & POLLIN) {
			nx = vde_recv(conn2,bufin,VDE_ETHBUFSIZE,0);
			if (__builtin_expect((nx >= ETH_HDRLEN),1)) {
				vde_send(conn, bufin, nx, 0);
				/*fprintf(stderr,"1->0 %d ",nx);*/
			} else if (nx<0)
				break;
		}
	}
	return(0);
}


static void netusage_and_exit() {
	vdeplug_openlog("FAILED");
	fprintf (stderr,"This is a Virtual Distributed Ethernet (vde) tunnel broker. \n"
			"This is not a login shell, only vde_plug can be executed\n");
	exit(-1);
}

static void usage_and_exit(char *progname) {
	fprintf (stderr,"Usage:\n"
			"  %s [ OPTIONS ] \n"
			"  %s [ OPTIONS ] vde_plug_url\n"
			"  %s [ OPTIONS ] vde_plug_url vde_plug_url\n"
			"  %s [ OPTIONS ] = command [args ...]\n"
			"  %s [ OPTIONS ] vde_plug_url = command [args ...]\n"
			"  an omitted or empty '' vde_plug_url refer to the default vde service\n"
			"  i.e. what defined in \"~/.vde2/default.switch\" or\n"
			"       a standard switch defined by libvdeplug_vde.so (e.g. /var/run/vde.ctl)\n"
			"Options:\n"
			"  -d | --daemon:            daemonize the program\n"
			"  -p PIDFILE | --pidfile PIDFILE: \n"
			"                            write pid of daemon to PIDFILE\n"
			"  -l | --log:               log START/STOP of vde_plug on syslog\n"
			"  -L | --iplog:             -l plus log the IP addresses used\n"
			"  -h | --help:              show this short summary\n"
			"  -g GROUP | -group GROUP:  set the group ownership (for vde://...)\n"
			"  -G GROUP | -group2 GROUP: like -g, for the second plug\n"
			"  -m MODE | -mod MODE:      comm socket protection mode (see chmod) (for vde://...)\n"
			"  -M MODE | -mod2 MODE:     like -m, for the second plug\n"
			"  --port PORT1, --port2 PORT2:\n"
			"                            obsolete options, set the vde switch port,\n"
			"                            use [port] suffix in the vde_plug_url instead\n"
			"  -D DESCR | --descr DESCR  set the description of this connection to DESCR\n"
			"                            (the default value is \"" DEFAULT_DESCRIPTION "\"\n"
			"\n",progname,progname,progname,progname,progname);
	exit(-1);
}

char short_options[] = "c:hp:dm:M:g:G:lLD:";
struct option long_options[] = {
	{"pidfile",  required_argument, 0, 'p' },
	{"daemon",   no_argument,       0, 'd' },
	{"port",     required_argument, 0, 0x100 + 'p'},
	{"port2",    required_argument, 0, 0x100 + 'P'},
	{"mod",      required_argument, 0, 'm'},
	{"mod2",     required_argument, 0, 'M'},
	{"group",    required_argument, 0, 'g'},
	{"group2",   required_argument, 0, 'G'},
	{"descr",    required_argument, 0, 'D' },
	{"log",      no_argument,       0, 'l' },
	{"iplog",    no_argument,       0, 'L' },
	{"help",     no_argument,       0, 'h' },
	{0, 0, 0, 0}
};

#include <syslog.h>
int main(int argc, char *argv[])
{
	char *progname = basename(argv[0]);
	static char *vde_url = NULL;
	static char *vde_url2 = NULL;
	static char **cmdargv = NULL;
	struct vde_open_args open_args = {.port = 0,.group = NULL,.mode = 0700};
	struct vde_open_args open_args2 = {.port = 0,.group = NULL,.mode = 0700};
	int daemonize = 0;
	char *pidfile = NULL;
	char *description = DEFAULT_DESCRIPTION;

	uname(&me);

	if (argv[0][0] == '-')
		netusage_and_exit(); //implies exit
	/* option parsing */
	while (1) {
		int option_index = 0;
		int c;

		c = getopt_long(argc, argv, short_options, long_options, &option_index);
		if (c == -1) break;

		switch (c) {
			case 'c':
				if (strcmp(optarg,"vde_plug") == 0) {
					vdeplug_openlog(NULL);
					atexit(vdeplug_closelog);
					if (checkuidgroup(getuid(), VDE_IP_LOG_GROUP) == 1)
						vde_ip_log = 1;
				}
				else
					netusage_and_exit(); //implies exit
				break;

			case 0x100 + 'p':
				open_args.port = atoi(optarg);
				if (open_args.port <= 0)
					usage_and_exit(progname); //implies exit
				break;

			case 0x100 + 'P':
				open_args2.port = atoi(optarg);
				if (open_args2.port <= 0)
					usage_and_exit(progname); //implies exit
				break;

			case 'h':
				usage_and_exit(progname); //implies exit
				break;

			case 'm': 
				sscanf(optarg,"%o",(unsigned int *)&(open_args.mode));
				break;

			case 'M': 
				sscanf(optarg,"%o",(unsigned int *)&(open_args2.mode));
				break;

			case 'g':
				open_args.group = optarg;
				break;

			case 'G':
				open_args2.group = optarg;
				break;

			case 'L':
				vde_ip_log = 1;

			case 'l':
				vdeplug_openlog(NULL);
				atexit(vdeplug_closelog);
				break;

			case 'p' : 
				pidfile = optarg;
				break;

			case 'd' : 
				daemonize = 1;
				break;

			case 'D' : 
				if (*optarg != 0)
					description = optarg;
				break;

			default:
				usage_and_exit(progname); //implies exit
		}
	}

	argv += optind;
	argc -= optind;

	switch (argc) {
		case 0: break;
		case 1: vde_url = *argv;
						break;
		case 2: if (strcmp(argv[0],"=") == 0)
							cmdargv = argv+1;
						else {
							vde_url = argv[0];
							vde_url2 = argv[1];
						}
						break;
		default: if (strcmp(argv[0],"=") == 0)
							 cmdargv = argv+1;
						 else if (strcmp(argv[1],"=") == 0) {
							 vde_url = argv[0];
							 cmdargv = argv+2;
						 } else
							 usage_and_exit(progname); //implies exit
						 break;
	}

	if (daemonize != 0) {
		if (daemon(0,0) < 0) {
			fprintf(stderr,"%s daemonize: %s\n", progname, strerror(errno));
			exit(1);
		}
	}

	if (pidfile != NULL)
		openclosepidfile(pidfile);

	atexit(cleanup);
	setsighandlers(cleanup);

	conn = vde_open(vde_url, description, &open_args);
	if (conn == NULL) {
		fprintf(stderr,"vde_open %s: %s\n",vde_url && *vde_url ? vde_url : "default switch", strerror(errno));
		exit(1);
	}
	if (vde_url2 != NULL) {
		conn2 = vde_open(vde_url2, description, &open_args2);
		if (conn2 == NULL) {
			fprintf(stderr,"vde_open %s: %s\n",vde_url2 && *vde_url2 ? vde_url2 : "default switch", strerror(errno));
			exit(1);
		}
		return plug2plug();
	} else if (cmdargv != NULL)
		return plug2cmd(cmdargv);
	else
		return plug2stream();
}