3 * Initialization for the standalone server
5 * (c) 2018 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 /*----- Tunnel table ------------------------------------------------------*/
32 static const tunnel_ops
*tunnels
[] = {
45 /*----- Main code ---------------------------------------------------------*/
49 * Arguments: @int argc@ = number of command line arguments
50 * @char *argv[]@ = vector of arguments
52 * Returns: Zero if OK, nonzero on error.
54 * Use: Main program. Provides a simple VPN.
57 static void usage(FILE *fp
)
59 pquis(fp
, "Usage: $ [-DF] [-d DIR] [-b ADDR] [-p PORT] [-n TUNNEL]\n\
60 [-U USER] [-G GROUP] [-a SOCKET] [-m MODE] [-T TRACE-OPTS]\n\
61 [-k PRIV-KEYRING] [-K PUB-KEYRING] [-t KEY-TAG]\n");
64 static void version(FILE *fp
) { pquis(fp
, "$, version " VERSION
"\n"); }
66 static void help(FILE *fp
)
74 -h, --help Display this help text.\n\
75 -v, --version Display version number.\n\
76 -u, --usage Display pointless usage message.\n\
77 --tunnels Display IP tunnel drivers and exit.\n\
79 -4, --ipv4 Transport over IPv4 only.\n\
80 -6, --ipv6 Transport over IPv6 only.\n\
81 -D, --daemon Run in the background.\n\
82 -F, --foreground Quit when stdin reports end-of-file.\n\
83 -d, --directory=DIR Switch to directory DIR [default " CONFIGDIR
"].\n\
84 -b, --bind-address=ADDR Bind UDP socket to this IP ADDR.\n\
85 -p, --port=PORT Select UDP port to listen to "
86 "[default " STR(TRIPE_PORT
) "].\n\
87 -n, --tunnel=TUNNEL Seelect default tunnel driver.\n\
88 -U, --setuid=USER Set uid to USER after initialization.\n\
89 -G, --setgid=GROUP Set gid to GROUP after initialization.\n\
90 -k, --priv-keyring=FILE Get private key from FILE.\n\
91 -K, --pub-keyring=FILE Get public keys from FILE.\n\
92 -t, --tag=KEYTAG Use private key labelled TAG.\n\
93 -a, --admin-socket=FILE Use FILE as the adminstration socket.\n\
94 -m, --admin-perms=MODE Permissions to set on admin socket [default 600].\n\
96 -T, --trace=OPTIONS Turn on tracing options.\n\
101 int main(int argc
, char *argv
[])
103 const char *kr_priv
= "keyring", *kr_pub
= "keyring.pub";
104 const char *tag_priv
= 0;
105 const char *csock
= SOCKETDIR
"/tripesock";
106 int csockmode
= 0600;
107 const char *dir
= CONFIGDIR
;
109 const char *bindhost
= 0, *bindsvc
= STR(TRIPE_PORT
);
110 struct addrinfo aihint
= { 0 }, *ailist
;
111 const tunnel_ops
*dflt
= 0;
121 #define f_foreground 4u
124 T( trace_on(stderr
, 0); )
126 if ((p
= getenv("TRIPEDIR")) != 0)
128 if ((p
= getenv("TRIPESOCK")) != 0)
130 aihint
.ai_family
= AF_UNSPEC
;
133 static const struct option opts
[] = {
134 { "help", 0, 0, 'h' },
135 { "version", 0, 0, 'v' },
136 { "usage", 0, 0, 'u' },
137 { "tunnels", 0, 0, '0' },
139 { "ipv4", 0, 0, '4' },
140 { "ipv6", 0, 0, '6' },
141 { "daemon", 0, 0, 'D' },
142 { "foreground", 0, 0, 'F' },
143 { "uid", OPTF_ARGREQ
, 0, 'U' },
144 { "setuid", OPTF_ARGREQ
, 0, 'U' },
145 { "gid", OPTF_ARGREQ
, 0, 'G' },
146 { "setgid", OPTF_ARGREQ
, 0, 'G' },
147 { "bind-address", OPTF_ARGREQ
, 0, 'b' },
148 { "tunnel", OPTF_ARGREQ
, 0, 'n' },
149 { "port", OPTF_ARGREQ
, 0, 'p' },
150 { "directory", OPTF_ARGREQ
, 0, 'd' },
151 { "priv-keyring", OPTF_ARGREQ
, 0, 'k' },
152 { "pub-keyring", OPTF_ARGREQ
, 0, 'K' },
153 { "tag", OPTF_ARGREQ
, 0, 't' },
154 { "admin-socket", OPTF_ARGREQ
, 0, 'a' },
155 { "admin-perms", OPTF_ARGREQ
, 0, 'm' },
157 { "trace", OPTF_ARGREQ
, 0, 'T' },
163 i
= mdwopt(argc
, argv
, "hvu46DFU:G:b:n:p:d:k:K:t:a:m:" T("T:"),
179 aihint
.ai_family
= AF_INET
;
182 aihint
.ai_family
= AF_INET6
;
188 u
= u_getuser(optarg
, &g
);
191 g
= u_getgroup(optarg
);
205 for (i
= 0; i
< N(tunnels
); i
++)
206 if (mystrieq(optarg
, tunnels
[i
]->name
))
207 { dflt
= tunnels
[i
]; goto found_tun
; }
208 die(EXIT_FAILURE
, "unknown tunnel `%s'", optarg
);
225 csockmode
= strtol(optarg
, &p
, 8);
226 if (*p
) die(EXIT_FAILURE
, "bad permissions: `%s'", optarg
);
233 tr_flags
= traceopt(tr_opts
, optarg
, tr_flags
, 0);
234 trace_level(tr_flags
);
239 for (i
= 0; i
< N(tunnels
); i
++)
240 puts(tunnels
[i
]->name
);
249 if (optind
< argc
|| (f
& f_bogus
)) {
253 if (!(~f
& (f_daemon
| f_foreground
)))
254 die(EXIT_FAILURE
, "foreground operation for a daemon is silly");
256 aihint
.ai_protocol
= IPPROTO_UDP
;
257 aihint
.ai_socktype
= SOCK_DGRAM
;
258 aihint
.ai_flags
= AI_PASSIVE
| AI_ADDRCONFIG
;
259 if ((err
= getaddrinfo(bindhost
, bindsvc
, &aihint
, &ailist
)) != 0) {
260 die(EXIT_FAILURE
, "couldn't resolve hostname %c%s%c, port `%s': %s",
261 bindhost ?
'`' : '<',
262 bindhost ? bindhost
: "nil",
263 bindhost ?
'\'' : '>',
264 bindsvc
, gai_strerror(err
));
268 die(EXIT_FAILURE
, "can't set current directory to `%s': %s",
269 dir
, strerror(errno
));
272 rand_noisesrc(RAND_GLOBAL
, &noise_source
);
273 rand_seed(RAND_GLOBAL
, MAXHASHSZ
);
277 if (!(f
& f_daemon
)) {
282 if (f
& f_foreground
)
284 a_create(STDIN_FILENO
, STDOUT_FILENO
, af
);
289 for (i
= 0; i
< N(tunnels
); i
++)
290 if (p_addtun(tunnels
[i
])) exit(EXIT_FAILURE
);
291 if (dflt
) p_setdflttun(dflt
);
292 if (p_bind(ailist
)) exit(EXIT_FAILURE
);
293 freeaddrinfo(ailist
);
295 for (i
= 0; tunnels
[i
]; i
++) {
296 if (tunnels
[i
]->flags
&TUNF_PRIVOPEN
) {
297 if (ps_split(f
& f_daemon
)) exit(EXIT_FAILURE
);
302 if (a_init()) exit(EXIT_FAILURE
);
304 if (a_listen(csock
, u
, g
, csockmode
)) exit(EXIT_FAILURE
);
306 if (km_init(kr_priv
, kr_pub
, tag_priv
)) exit(EXIT_FAILURE
);
310 a_warn("SERVER", "daemon-error", "?ERRNO", A_END
);
327 /*----- That's all, folks -------------------------------------------------*/