xref: /openbsd/usr.sbin/mopd/mopd/mopd.c (revision 28056f30)
1 /*	$OpenBSD: mopd.c,v 1.19 2015/02/09 23:00:14 deraadt Exp $ */
2 
3 /*
4  * Copyright (c) 1993-96 Mats O Jansson.  All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 
27 /*
28  * mopd - MOP Dump/Load Daemon
29  *
30  * Usage:	mopd [-3 | -4] [-adfv] interface
31  */
32 
33 #include "os.h"
34 #include "common/common.h"
35 #include "common/mopdef.h"
36 #include "common/device.h"
37 #include "common/print.h"
38 #include "common/pf.h"
39 #include "common/cmp.h"
40 #include "common/get.h"
41 #include "common/dl.h"
42 #include "common/rc.h"
43 #include "process.h"
44 
45 #include "pwd.h"
46 
47 /*
48  * The list of all interfaces that are being listened to.
49  * "selects" on the descriptors in this list.
50  */
51 struct if_info	*iflist;
52 
53 void		Usage(void);
54 void		mopProcess(struct if_info *, u_char *);
55 
56 int	 AllFlag = 0;		/* listen on "all" interfaces */
57 int	 DebugFlag = 0;		/* print debugging messages   */
58 int	 ForegroundFlag = 0;	/* run in foreground          */
59 int	 VersionFlag = 0;	/* print version              */
60 int	 Not3Flag = 0;		/* Not MOP V3 messages.       */
61 int	 Not4Flag = 0;		/* Not MOP V4 messages.       */
62 int	 promisc = 1;		/* Need promisc mode    */
63 
64 extern char *__progname;
65 
66 int
main(int argc,char * argv[])67 main(int argc, char *argv[])
68 {
69 	int		 c;
70 	char		*interface;
71 	struct passwd	*pw;
72 
73 	extern char version[];
74 
75 	while ((c = getopt(argc, argv, "34adfv")) != -1)
76 		switch (c) {
77 		case '3':
78 			Not3Flag = 1;
79 			break;
80 		case '4':
81 			Not4Flag = 1;
82 			break;
83 		case 'a':
84 			AllFlag = 1;
85 			break;
86 		case 'd':
87 			DebugFlag++;
88 			break;
89 		case 'f':
90 			ForegroundFlag = 1;
91 			break;
92 		case 'v':
93 			VersionFlag = 1;
94 			break;
95 		default:
96 			Usage();
97 			/* NOTREACHED */
98 		}
99 
100 	if (VersionFlag) {
101 		fprintf(stdout,"%s: version %s\n", __progname, version);
102 		exit(0);
103 	}
104 
105 	interface = argv[optind++];
106 
107 	if ((AllFlag && interface) || (!AllFlag && interface == 0) ||
108 	    (argc > optind) || (Not3Flag && Not4Flag))
109 		Usage();
110 
111 	/* All error reporting is done through syslogs. */
112 	openlog(__progname, LOG_PID | LOG_CONS, LOG_DAEMON);
113 	tzset();
114 
115 	if ((pw = getpwnam("_mopd")) == NULL)
116 		err(1, "getpwnam");
117 
118 	if ((!ForegroundFlag) && DebugFlag)
119 		fprintf(stdout, "%s: not running as daemon, -d given.\n",
120 		    __progname);
121 
122 	if ((!ForegroundFlag) && (!DebugFlag))
123 		if (daemon(0, 0) == -1)
124 			err(1, NULL);
125 
126 	syslog(LOG_INFO, "%s %s started.", __progname, version);
127 
128 	if (AllFlag)
129 		deviceInitAll();
130 	else
131 		deviceInitOne(interface);
132 
133 	if (chroot(MOP_FILE_PATH) == -1) {
134 		syslog(LOG_CRIT, "chroot %s: %m", MOP_FILE_PATH);
135 		exit(1);
136 	}
137 	if (chdir("/") == -1) {
138 		syslog(LOG_CRIT, "chdir(\"/\"): %m");
139 		exit(1);
140 	}
141 	if (setgroups(1, &pw->pw_gid) ||
142 	    setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) ||
143 	    setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid)) {
144 		syslog(LOG_CRIT, "can't drop privileges: %m");
145 		exit(1);
146 	}
147 	endpwent();
148 
149 	Loop();
150 	/* NOTREACHED */
151 }
152 
153 void
Usage()154 Usage()
155 {
156 	fprintf(stderr, "usage: %s [-3 | -4] [-adfv] interface\n",
157 	    __progname);
158 	exit(1);
159 }
160 
161 /*
162  * Process incoming packages.
163  */
164 void
mopProcess(struct if_info * ii,u_char * pkt)165 mopProcess(struct if_info *ii, u_char *pkt)
166 {
167 	u_char	*dst, *src;
168 	u_short  ptype;
169 	int	 idx, trans, len;
170 
171 	/* We don't known with transport, Guess! */
172 	trans = mopGetTrans(pkt, 0);
173 
174 	/* Ok, return if we don't wan't this message */
175 	if ((trans == TRANS_ETHER) && Not3Flag) return;
176 	if ((trans == TRANS_8023) && Not4Flag)	return;
177 
178 	idx = 0;
179 	mopGetHeader(pkt, &idx, &dst, &src, &ptype, &len, trans);
180 
181 	/*
182 	 * Ignore our own transmissions
183 	 *
184 	 */
185 	if (mopCmpEAddr(ii->eaddr,src) == 0)
186 		return;
187 
188 	switch (ptype) {
189 	case MOP_K_PROTO_DL:
190 		mopProcessDL(stdout, ii, pkt, &idx, dst, src, trans, len);
191 		break;
192 	case MOP_K_PROTO_RC:
193 		mopProcessRC(stdout, ii, pkt, &idx, dst, src, trans, len);
194 		break;
195 	default:
196 		break;
197 	}
198 }
199