7 * (c) 2001 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Trivial IP Encryption (TrIPE).
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.
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.
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.
29 /*----- Header files ------------------------------------------------------*/
33 /*----- Global variables --------------------------------------------------*/
37 /*----- Static variables --------------------------------------------------*/
40 #define T_INTERVAL MIN(1)
42 /*----- Main code ---------------------------------------------------------*/
44 /* --- @interval@ --- *
46 * Arguments: @struct timeval *tv@ = time when called
47 * @void *v@ = boring pointer
51 * Use: Called periodically to do housekeeping tasks.
54 void interval(struct timeval
*tv
, void *v
)
57 T( trace(T_PEER
, "peer: interval timer"); )
58 rand_seed(RAND_GLOBAL
, MAXHASHSZ
);
61 tvv
.tv_sec
+= T_INTERVAL
;
62 sel_addtimer(&sel
, &it
, &tvv
, interval
, v
);
67 * Arguments: @int argc@ = number of command line arguments
68 * @char *argv[]@ = vector of arguments
70 * Returns: Zero if OK, nonzero on error.
72 * Use: Main program. Provides a simple VPN.
75 static void usage(FILE *fp
)
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");
82 static void version(FILE *fp
)
84 pquis(fp
, "$, version " VERSION
"\n");
87 static void help(FILE *fp
)
95 -h, --help Display this help text.\n\
96 -v, --version Display version number.\n\
97 -u, --usage Display pointless usage message.\n\
98 --tunnel Display IP tunnelling technique and exit.\n\
100 -D, --daemon Run in the background.\n\
101 -d, --directory=DIR Switch to directory DIR [default " CONFIGDIR
"].\n\
102 -b, --bind-address=ADDR Bind UDP socket to this IP ADDR.\n\
103 -p, --port=PORT Select UDP port to listen to.\n\
104 -U, --setuid=USER Set uid to USER after initialization.\n\
105 -G, --setgid=GROUP Set gid to GROUP after initialization.\n\
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\
111 -T, --trace=OPTIONS Turn on tracing options.\n\
116 int main(int argc
, char *argv
[])
118 const char *kr_priv
= "keyring", *kr_pub
= "keyring.pub";
119 const char *tag_priv
= "tripe-dh";
120 const char *csock
= SOCKETDIR
"/tripesock";
121 const char *dir
= CONFIGDIR
;
124 struct in_addr baddr
= { INADDR_ANY
};
133 T( trace_on(stderr
, 0); )
135 if ((p
= getenv("TRIPEDIR")) != 0)
139 static const struct option opts
[] = {
140 { "help", 0, 0, 'h' },
141 { "version", 0, 0, 'v' },
142 { "usage", 0, 0, 'u' },
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' },
149 { "bind-address", OPTF_ARGREQ
, 0, 'b' },
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' },
157 { "trace", OPTF_ARGREQ
, 0, 'T' },
160 { "tunnel", 0, 0, '0' },
164 int i
= mdwopt(argc
, argv
, "hvuDU:G:b:p:d:k:K:t:a:" T("T:"),
185 unsigned long i
= strtoul(optarg
, &p
, 0);
189 pw
= getpwnam(optarg
);
191 die(EXIT_FAILURE
, "user `%s' not found", optarg
);
199 unsigned long i
= strtoul(optarg
, &p
, 0);
203 gr
= getgrnam(optarg
);
205 die(EXIT_FAILURE
, "group `%s' not found", optarg
);
210 struct hostent
*h
= gethostbyname(optarg
);
212 die(EXIT_FAILURE
, "unknown host name `%s'", optarg
);
213 memcpy(&baddr
, h
->h_addr
, sizeof(struct in_addr
));
217 unsigned long i
= strtoul(optarg
, &p
, 0);
219 struct servent
*s
= getservbyname(optarg
, "udp");
221 die(EXIT_FAILURE
, "unknown service name `%s'", optarg
);
222 i
= ntohs(s
->s_port
);
224 if (i
== 0 || i
>= 65536)
225 die(EXIT_FAILURE
, "bad port number %lu", i
);
245 tr_flags
= traceopt(tr_opts
, optarg
, tr_flags
, 0);
246 trace_level(tr_flags
);
250 static const char *tun
[] =
251 { "notdef", "unet", "bsd", "linux", "slip" };
261 if (optind
< argc
|| (f
& f_bogus
)) {
267 die(EXIT_FAILURE
, "can't set current directory to `%s': %s",
268 dir
, strerror(errno
));
273 rand_noisesrc(RAND_GLOBAL
, &noise_source
);
274 rand_seed(RAND_GLOBAL
, MAXHASHSZ
);
275 signal(SIGPIPE
, SIG_IGN
);
278 if (!(f
& f_daemon
)) {
280 a_create(STDIN_FILENO
, STDOUT_FILENO
, AF_TRACE
| AF_WARN
);
282 a_create(STDIN_FILENO
, STDOUT_FILENO
, AF_WARN
);
285 if (g
!= (gid_t
)-1) {
286 if (setgid(g
) || (getuid() == 0 && setgroups(1, &g
))) {
287 die(EXIT_FAILURE
, "couldn't setgid to %u: %s",
288 (unsigned)g
, strerror(errno
));
291 if (u
!= (uid_t
)-1) {
293 die(EXIT_FAILURE
, "couldn't setuid to %u: %s",
294 (unsigned)u
, strerror(errno
));
297 km_init(kr_priv
, kr_pub
, tag_priv
);
301 die(EXIT_FAILURE
, "couldn't become a daemon: %s", strerror(errno
));
307 tv
.tv_sec
= time(0) + T_INTERVAL
;
309 sel_addtimer(&sel
, &it
, &tv
, interval
, 0);
315 if (!sel_select(&sel
))
317 else if (errno
!= EINTR
&& errno
!= EAGAIN
) {
318 a_warn("SERVER select-error -- %s", strerror(errno
));
321 a_warn("ABORT repeated-select-errors");
331 /*----- That's all, folks -------------------------------------------------*/