bf217b377079835900b8fd2895cf8385330d4e64
5 * (c) 2001 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Trivial IP Encryption (TrIPE).
12 * TrIPE is free software: you can redistribute it and/or modify it under
13 * the terms of the GNU General Public License as published by the Free
14 * Software Foundation; either version 3 of the License, or (at your
15 * option) any later version.
17 * TrIPE is distributed in the hope that it will be useful, but WITHOUT
18 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 * You should have received a copy of the GNU General Public License
23 * along with TrIPE. If not, see <https://www.gnu.org/licenses/>.
26 /*----- Header files ------------------------------------------------------*/
30 /*----- Global variables --------------------------------------------------*/
34 /*----- Static variables --------------------------------------------------*/
37 #define T_INTERVAL MIN(1)
39 static unsigned iv_nreasons
= 0;
40 static struct timeval iv_next
= { 0, 0 };
42 /*----- The interval timer ------------------------------------------------*/
44 /* --- @interval@ --- *
46 * Arguments: @struct timeval *tv@ = time when called
47 * @void *v@ = boring pointer
51 * Use: Called periodically to do housekeeping tasks.
54 static void interval(struct timeval
*tv
, void *v
)
56 T( trace(T_PEER
, "peer: interval timer"); )
58 rand_seed(RAND_GLOBAL
, MAXHASHSZ
);
60 iv_next
.tv_sec
+= T_INTERVAL
;
61 sel_addtimer(&sel
, &it
, &iv_next
, interval
, v
);
64 /* --- @iv_addreason@ --- *
70 * Use: Adds an `interval timer reason'; if there are no others, the
71 * interval timer is engaged.
74 void iv_addreason(void)
80 if (TV_CMP(&tv
, >=, &iv_next
)) interval(&tv
, 0);
81 else sel_addtimer(&sel
, &it
, &iv_next
, interval
, 0);
86 /* --- @iv_rmreason@ --- *
92 * Use: Removes an interval timer reason; if there are none left, the
93 * interval timer is disengaged.
96 void iv_rmreason(void)
98 assert(iv_nreasons
); iv_nreasons
--;
99 if (!iv_nreasons
) sel_rmtimer(&it
);
102 /*----- The main loop -----------------------------------------------------*/
104 /* --- @lp_init@ --- *
110 * Use: Initializes the main loop. Most importantly, this sets up
111 * the select multiplexor that everything else hooks onto.
116 rand_noisesrc(RAND_GLOBAL
, &noise_source
);
117 rand_seed(RAND_GLOBAL
, MAXHASHSZ
);
118 gettimeofday(&iv_next
, 0); iv_next
.tv_sec
+= T_INTERVAL
;
119 signal(SIGPIPE
, SIG_IGN
);
124 /* --- @lp_run@ --- *
128 * Returns: Zero on successful termination; @-1@ if things went wrong.
130 * Use: Cranks the main loop until it should be cranked no more.
139 if (!sel_select(&sel
)) nerr
= 0;
140 else if (errno
!= EINTR
&& errno
!= EAGAIN
) {
141 a_warn("SERVER", "select-error", "?ERRNO", A_END
);
144 a_warn("ABORT", "repeated-select-errors", A_END
);
152 /*----- Main code ---------------------------------------------------------*/
156 * Arguments: @int argc@ = number of command line arguments
157 * @char *argv[]@ = vector of arguments
159 * Returns: Zero if OK, nonzero on error.
161 * Use: Main program. Provides a simple VPN.
164 static void usage(FILE *fp
)
166 pquis(fp
, "Usage: $ [-DF] [-d DIR] [-b ADDR] [-p PORT] [-n TUNNEL]\n\
167 [-U USER] [-G GROUP] [-a SOCKET] [-m MODE] [-T TRACE-OPTS]\n\
168 [-k PRIV-KEYRING] [-K PUB-KEYRING] [-t KEY-TAG]\n");
171 static void version(FILE *fp
) { pquis(fp
, "$, version " VERSION
"\n"); }
173 static void help(FILE *fp
)
181 -h, --help Display this help text.\n\
182 -v, --version Display version number.\n\
183 -u, --usage Display pointless usage message.\n\
184 --tunnels Display IP tunnel drivers and exit.\n\
186 -4, --ipv4 Transport over IPv4 only.\n\
187 -6, --ipv6 Transport over IPv6 only.\n\
188 -D, --daemon Run in the background.\n\
189 -F, --foreground Quit when stdin reports end-of-file.\n\
190 -d, --directory=DIR Switch to directory DIR [default " CONFIGDIR
"].\n\
191 -b, --bind-address=ADDR Bind UDP socket to this IP ADDR.\n\
192 -p, --port=PORT Select UDP port to listen to "
193 "[default " STR(TRIPE_PORT
) "].\n\
194 -n, --tunnel=TUNNEL Seelect default tunnel driver.\n\
195 -U, --setuid=USER Set uid to USER after initialization.\n\
196 -G, --setgid=GROUP Set gid to GROUP after initialization.\n\
197 -k, --priv-keyring=FILE Get private key from FILE.\n\
198 -K, --pub-keyring=FILE Get public keys from FILE.\n\
199 -t, --tag=KEYTAG Use private key labelled TAG.\n\
200 -a, --admin-socket=FILE Use FILE as the adminstration socket.\n\
201 -m, --admin-perms=MODE Permissions to set on admin socket [default 600].\n\
203 -T, --trace=OPTIONS Turn on tracing options.\n\
208 int main(int argc
, char *argv
[])
210 const char *kr_priv
= "keyring", *kr_pub
= "keyring.pub";
211 const char *tag_priv
= 0;
212 const char *csock
= SOCKETDIR
"/tripesock";
213 int csockmode
= 0600;
214 const char *dir
= CONFIGDIR
;
216 const char *bindhost
= 0, *bindsvc
= STR(TRIPE_PORT
);
217 struct addrinfo aihint
= { 0 }, *ailist
;
227 #define f_foreground 4u
230 T( trace_on(stderr
, 0); )
232 if ((p
= getenv("TRIPEDIR")) != 0)
234 if ((p
= getenv("TRIPESOCK")) != 0)
236 tun_default
= tunnels
[0];
237 aihint
.ai_family
= AF_UNSPEC
;
240 static const struct option opts
[] = {
241 { "help", 0, 0, 'h' },
242 { "version", 0, 0, 'v' },
243 { "usage", 0, 0, 'u' },
244 { "tunnels", 0, 0, '0' },
246 { "ipv4", 0, 0, '4' },
247 { "ipv6", 0, 0, '6' },
248 { "daemon", 0, 0, 'D' },
249 { "foreground", 0, 0, 'F' },
250 { "uid", OPTF_ARGREQ
, 0, 'U' },
251 { "setuid", OPTF_ARGREQ
, 0, 'U' },
252 { "gid", OPTF_ARGREQ
, 0, 'G' },
253 { "setgid", OPTF_ARGREQ
, 0, 'G' },
254 { "bind-address", OPTF_ARGREQ
, 0, 'b' },
255 { "tunnel", OPTF_ARGREQ
, 0, 'n' },
256 { "port", OPTF_ARGREQ
, 0, 'p' },
257 { "directory", OPTF_ARGREQ
, 0, 'd' },
258 { "priv-keyring", OPTF_ARGREQ
, 0, 'k' },
259 { "pub-keyring", OPTF_ARGREQ
, 0, 'K' },
260 { "tag", OPTF_ARGREQ
, 0, 't' },
261 { "admin-socket", OPTF_ARGREQ
, 0, 'a' },
262 { "admin-perms", OPTF_ARGREQ
, 0, 'm' },
264 { "trace", OPTF_ARGREQ
, 0, 'T' },
270 i
= mdwopt(argc
, argv
, "hvu46DFU:G:b:n:p:d:k:K:t:a:m:" T("T:"),
286 aihint
.ai_family
= AF_INET
;
289 aihint
.ai_family
= AF_INET6
;
295 u
= u_getuser(optarg
, &g
);
298 g
= u_getgroup(optarg
);
314 die(EXIT_FAILURE
, "unknown tunnel `%s'", optarg
);
315 if (mystrieq(optarg
, tunnels
[i
]->name
))
318 tun_default
= tunnels
[i
];
334 csockmode
= strtol(optarg
, &p
, 8);
335 if (*p
) die(EXIT_FAILURE
, "bad permissions: `%s'", optarg
);
342 tr_flags
= traceopt(tr_opts
, optarg
, tr_flags
, 0);
343 trace_level(tr_flags
);
348 for (i
= 0; tunnels
[i
]; i
++)
349 puts(tunnels
[i
]->name
);
358 if (optind
< argc
|| (f
& f_bogus
)) {
362 if (!(~f
& (f_daemon
| f_foreground
)))
363 die(EXIT_FAILURE
, "foreground operation for a daemon is silly");
365 aihint
.ai_protocol
= IPPROTO_UDP
;
366 aihint
.ai_socktype
= SOCK_DGRAM
;
367 aihint
.ai_flags
= AI_PASSIVE
| AI_ADDRCONFIG
;
368 if ((err
= getaddrinfo(bindhost
, bindsvc
, &aihint
, &ailist
)) != 0) {
369 die(EXIT_FAILURE
, "couldn't resolve hostname %c%s%c, port `%s': %s",
370 bindhost ?
'`' : '<',
371 bindhost ? bindhost
: "nil",
372 bindhost ?
'\'' : '>',
373 bindsvc
, gai_strerror(err
));
377 die(EXIT_FAILURE
, "can't set current directory to `%s': %s",
378 dir
, strerror(errno
));
383 if (!(f
& f_daemon
)) {
388 if (f
& f_foreground
)
390 a_create(STDIN_FILENO
, STDOUT_FILENO
, af
);
395 for (i
= 0; tunnels
[i
]; i
++)
397 p_bind(ailist
); freeaddrinfo(ailist
);
399 for (i
= 0; tunnels
[i
]; i
++) {
400 if (tunnels
[i
]->flags
&TUNF_PRIVOPEN
) {
401 ps_split(f
& f_daemon
);
408 a_listen(csock
, u
, g
, csockmode
);
410 km_init(kr_priv
, kr_pub
, tag_priv
);
414 a_warn("SERVER", "daemon-error", "?ERRNO", A_END
);
425 /*----- That's all, folks -------------------------------------------------*/