410c8acf |
1 | /* -*-c-*- |
2 | * |
2d752320 |
3 | * $Id$ |
410c8acf |
4 | * |
5 | * Main program |
6 | * |
7 | * (c) 2001 Straylight/Edgeware |
8 | */ |
9 | |
10 | /*----- Licensing notice --------------------------------------------------* |
11 | * |
12 | * This file is part of Trivial IP Encryption (TrIPE). |
13 | * |
14 | * TrIPE is free software; you can redistribute it and/or modify |
15 | * it under the terms of the GNU General Public License as published by |
16 | * the Free Software Foundation; either version 2 of the License, or |
17 | * (at your option) any later version. |
18 | * |
19 | * TrIPE is distributed in the hope that it will be useful, |
20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
22 | * GNU General Public License for more details. |
23 | * |
24 | * You should have received a copy of the GNU General Public License |
25 | * along with TrIPE; if not, write to the Free Software Foundation, |
26 | * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
27 | */ |
28 | |
410c8acf |
29 | /*----- Header files ------------------------------------------------------*/ |
30 | |
31 | #include "tripe.h" |
32 | |
33 | /*----- Global variables --------------------------------------------------*/ |
34 | |
35 | sel_state sel; |
410c8acf |
36 | |
37 | /*----- Static variables --------------------------------------------------*/ |
38 | |
39 | static sel_timer it; |
40 | #define T_INTERVAL MIN(1) |
41 | |
42 | /*----- Main code ---------------------------------------------------------*/ |
43 | |
44 | /* --- @interval@ --- * |
45 | * |
46 | * Arguments: @struct timeval *tv@ = time when called |
47 | * @void *v@ = boring pointer |
48 | * |
49 | * Returns: --- |
50 | * |
51 | * Use: Called periodically to do housekeeping tasks. |
52 | */ |
53 | |
54 | void interval(struct timeval *tv, void *v) |
55 | { |
56 | struct timeval tvv; |
57 | T( trace(T_PEER, "peer: interval timer"); ) |
b5c45da1 |
58 | rand_seed(RAND_GLOBAL, MAXHASHSZ); |
410c8acf |
59 | p_interval(); |
60 | tvv = *tv; |
61 | tvv.tv_sec += T_INTERVAL; |
62 | sel_addtimer(&sel, &it, &tvv, interval, v); |
63 | } |
64 | |
65 | /* --- @main@ --- * |
66 | * |
67 | * Arguments: @int argc@ = number of command line arguments |
68 | * @char *argv[]@ = vector of arguments |
69 | * |
70 | * Returns: Zero if OK, nonzero on error. |
71 | * |
72 | * Use: Main program. Provides a simple VPN. |
73 | */ |
74 | |
75 | static void usage(FILE *fp) |
76 | { |
2d752320 |
77 | pquis(fp, "Usage: $ [-D] [-d DIR] [-b ADDR] [-p PORT]\n\ |
78 | [-U USER] [-G GROUP] [-a SOCKET] [-T TRACE-OPTS]\n\ |
79 | [-k PRIV-KEYRING] [-K PUB-KEYRING] [-t KEY-TAG]\n"); |
410c8acf |
80 | } |
81 | |
82 | static void version(FILE *fp) |
83 | { |
84 | pquis(fp, "$, version " VERSION "\n"); |
85 | } |
86 | |
87 | static void help(FILE *fp) |
88 | { |
89 | version(fp); |
90 | fputc('\n', fp); |
91 | usage(fp); |
92 | fputs("\n\ |
93 | Options:\n\ |
94 | \n\ |
95 | -h, --help Display this help text.\n\ |
96 | -v, --version Display version number.\n\ |
97 | -u, --usage Display pointless usage message.\n\ |
3cdc3f3a |
98 | --tunnel Display IP tunnelling technique and exit.\n\ |
410c8acf |
99 | \n\ |
100 | -D, --daemon Run in the background.\n\ |
ef4a1ab7 |
101 | -d, --directory=DIR Switch to directory DIR [default " CONFIGDIR "].\n\ |
d13e5724 |
102 | -b, --bind-address=ADDR Bind UDP socket to this IP ADDR.\n\ |
410c8acf |
103 | -p, --port=PORT Select UDP port to listen to.\n\ |
d13e5724 |
104 | -U, --setuid=USER Set uid to USER after initialization.\n\ |
105 | -G, --setgid=GROUP Set gid to GROUP after initialization.\n\ |
410c8acf |
106 | -k, --priv-keyring=FILE Get private key from FILE.\n\ |
107 | -K, --pub-keyring=FILE Get public keys from FILE.\n\ |
108 | -t, --tag=KEYTAG Use private key labelled TAG.\n\ |
109 | -a, --admin-socket=FILE Use FILE as the adminstration socket.\n\ |
c5a2ea9b |
110 | " T( "\ |
410c8acf |
111 | -T, --trace=OPTIONS Turn on tracing options.\n\ |
c5a2ea9b |
112 | " ) "\ |
410c8acf |
113 | ", fp); |
114 | } |
115 | |
116 | int main(int argc, char *argv[]) |
117 | { |
118 | const char *kr_priv = "keyring", *kr_pub = "keyring.pub"; |
119 | const char *tag_priv = "tripe-dh"; |
ef4a1ab7 |
120 | const char *csock = SOCKETDIR "/tripesock"; |
121 | const char *dir = CONFIGDIR; |
410c8acf |
122 | const char *p; |
123 | unsigned port = 0; |
767b36e2 |
124 | struct in_addr baddr = { INADDR_ANY }; |
410c8acf |
125 | unsigned f = 0; |
126 | uid_t u = -1; |
127 | gid_t g = -1; |
128 | |
129 | #define f_bogus 1u |
130 | #define f_daemon 2u |
131 | |
132 | ego(argv[0]); |
d360f042 |
133 | T( trace_on(stderr, 0); ) |
410c8acf |
134 | |
135 | if ((p = getenv("TRIPEDIR")) != 0) |
136 | dir = p; |
137 | |
138 | for (;;) { |
139 | static const struct option opts[] = { |
140 | { "help", 0, 0, 'h' }, |
141 | { "version", 0, 0, 'v' }, |
142 | { "usage", 0, 0, 'u' }, |
143 | |
144 | { "daemon", 0, 0, 'D' }, |
145 | { "uid", OPTF_ARGREQ, 0, 'U' }, |
146 | { "setuid", OPTF_ARGREQ, 0, 'U' }, |
147 | { "gid", OPTF_ARGREQ, 0, 'G' }, |
148 | { "setgid", OPTF_ARGREQ, 0, 'G' }, |
767b36e2 |
149 | { "bind-address", OPTF_ARGREQ, 0, 'b' }, |
410c8acf |
150 | { "port", OPTF_ARGREQ, 0, 'p' }, |
151 | { "directory", OPTF_ARGREQ, 0, 'd' }, |
152 | { "priv-keyring", OPTF_ARGREQ, 0, 'k' }, |
153 | { "pub-keyring", OPTF_ARGREQ, 0, 'K' }, |
154 | { "tag", OPTF_ARGREQ, 0, 't' }, |
155 | { "admin-socket", OPTF_ARGREQ, 0, 'a' }, |
156 | #ifndef NTRACE |
157 | { "trace", OPTF_ARGREQ, 0, 'T' }, |
158 | #endif |
159 | |
ef4a1ab7 |
160 | { "tunnel", 0, 0, '0' }, |
410c8acf |
161 | { 0, 0, 0, 0 } |
162 | }; |
163 | |
74ee77cb |
164 | int i = mdwopt(argc, argv, "hvuDU:G:b:p:d:k:K:t:a:" T("T:"), |
410c8acf |
165 | opts, 0, 0, 0); |
166 | if (i < 0) |
167 | break; |
168 | switch (i) { |
169 | case 'h': |
170 | help(stdout); |
171 | exit(0); |
172 | case 'v': |
173 | version(stdout); |
174 | exit(0); |
175 | case 'u': |
176 | usage(stdout); |
177 | exit(0); |
178 | |
179 | case 'D': |
180 | f |= f_daemon; |
181 | break; |
182 | case 'U': { |
b303b584 |
183 | struct passwd *pw; |
410c8acf |
184 | char *p; |
185 | unsigned long i = strtoul(optarg, &p, 0); |
186 | if (!*p) |
b303b584 |
187 | pw = getpwuid(i); |
188 | else |
189 | pw = getpwnam(optarg); |
190 | if (!pw) |
191 | die(EXIT_FAILURE, "user `%s' not found", optarg); |
192 | u = pw->pw_uid; |
193 | if (g == -1) |
194 | g = pw->pw_gid; |
410c8acf |
195 | } break; |
196 | case 'G': { |
b303b584 |
197 | struct group *gr; |
410c8acf |
198 | char *p; |
199 | unsigned long i = strtoul(optarg, &p, 0); |
200 | if (!*p) |
b303b584 |
201 | gr = getgrgid(i); |
202 | else |
203 | gr = getgrnam(optarg); |
204 | if (!gr) |
205 | die(EXIT_FAILURE, "group `%s' not found", optarg); |
206 | g = gr->gr_gid; |
410c8acf |
207 | } break; |
767b36e2 |
208 | |
209 | case 'b': { |
210 | struct hostent *h = gethostbyname(optarg); |
211 | if (!h) |
212 | die(EXIT_FAILURE, "unknown host name `%s'", optarg); |
213 | memcpy(&baddr, h->h_addr, sizeof(struct in_addr)); |
214 | } break; |
410c8acf |
215 | case 'p': { |
216 | char *p; |
217 | unsigned long i = strtoul(optarg, &p, 0); |
218 | if (*p) { |
219 | struct servent *s = getservbyname(optarg, "udp"); |
220 | if (!s) |
221 | die(EXIT_FAILURE, "unknown service name `%s'", optarg); |
222 | i = ntohs(s->s_port); |
223 | } |
224 | if (i == 0 || i >= 65536) |
225 | die(EXIT_FAILURE, "bad port number %lu", i); |
226 | port = i; |
227 | } break; |
228 | case 'd': |
229 | dir = optarg; |
230 | break; |
231 | case 'k': |
232 | kr_priv = optarg; |
233 | break; |
234 | case 'K': |
235 | kr_pub = optarg; |
236 | break; |
237 | case 'a': |
238 | csock = optarg; |
239 | break; |
240 | case 't': |
241 | tag_priv = optarg; |
242 | break; |
243 | #ifndef NTRACE |
244 | case 'T': |
245 | tr_flags = traceopt(tr_opts, optarg, tr_flags, 0); |
246 | trace_level(tr_flags); |
247 | break; |
248 | #endif |
ef4a1ab7 |
249 | case '0': { |
b9066fbb |
250 | static const char *tun[] = |
251 | { "notdef", "unet", "bsd", "linux", "slip" }; |
ef4a1ab7 |
252 | puts(tun[TUN_TYPE]); |
253 | exit(0); |
254 | } break; |
410c8acf |
255 | default: |
256 | f |= f_bogus; |
257 | break; |
258 | } |
259 | } |
260 | |
261 | if (optind < argc || (f & f_bogus)) { |
262 | usage(stderr); |
263 | exit(EXIT_FAILURE); |
264 | } |
265 | |
266 | if (chdir(dir)) { |
267 | die(EXIT_FAILURE, "can't set current directory to `%s': %s", |
268 | dir, strerror(errno)); |
269 | } |
270 | |
271 | sel_init(&sel); |
272 | sig_init(&sel); |
273 | rand_noisesrc(RAND_GLOBAL, &noise_source); |
b5c45da1 |
274 | rand_seed(RAND_GLOBAL, MAXHASHSZ); |
410c8acf |
275 | signal(SIGPIPE, SIG_IGN); |
276 | tun_init(); |
767b36e2 |
277 | p_init(baddr, port); |
3cdc3f3a |
278 | if (!(f & f_daemon)) { |
279 | #ifndef NTRACE |
280 | a_create(STDIN_FILENO, STDOUT_FILENO, AF_TRACE | AF_WARN); |
281 | #else |
282 | a_create(STDIN_FILENO, STDOUT_FILENO, AF_WARN); |
283 | #endif |
284 | } |
b303b584 |
285 | if (g != (gid_t)-1) { |
286 | if (setgid(g) || (getuid() == 0 && setgroups(1, &g))) { |
410c8acf |
287 | die(EXIT_FAILURE, "couldn't setgid to %u: %s", |
288 | (unsigned)g, strerror(errno)); |
289 | } |
290 | } |
b303b584 |
291 | if (u != (uid_t)-1) { |
410c8acf |
292 | if (setuid(u)) { |
293 | die(EXIT_FAILURE, "couldn't setuid to %u: %s", |
294 | (unsigned)u, strerror(errno)); |
295 | } |
296 | } |
297 | km_init(kr_priv, kr_pub, tag_priv); |
298 | a_init(csock); |
299 | if (f & f_daemon) { |
37852359 |
300 | if (u_daemon()) |
410c8acf |
301 | die(EXIT_FAILURE, "couldn't become a daemon: %s", strerror(errno)); |
302 | a_daemon(); |
303 | } |
304 | |
305 | { |
306 | struct timeval tv; |
307 | tv.tv_sec = time(0) + T_INTERVAL; |
308 | tv.tv_usec = 0; |
309 | sel_addtimer(&sel, &it, &tv, interval, 0); |
310 | } |
311 | |
312 | { |
313 | int selerr = 0; |
314 | for (;;) { |
315 | if (!sel_select(&sel)) |
316 | selerr = 0; |
317 | else if (errno != EINTR && errno != EAGAIN) { |
3cdc3f3a |
318 | a_warn("SERVER select-error -- %s", strerror(errno)); |
410c8acf |
319 | selerr++; |
320 | if (selerr > 8) { |
3cdc3f3a |
321 | a_warn("ABORT repeated-select-errors"); |
322 | abort(); |
410c8acf |
323 | } |
324 | } |
325 | } |
326 | } |
327 | |
328 | return (0); |
329 | } |
330 | |
331 | /*----- That's all, folks -------------------------------------------------*/ |