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 static 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
);
65 /* --- @mystrieq@ --- *
67 * Arguments: @const char *x, *y@ = two strings
69 * Returns: True if @x@ and @y are equal, up to case.
72 int mystrieq(const char *x
, const char *y
)
75 if (!*x
&& !*y
) return (1);
76 if (tolower((unsigned char)*x
) != tolower((unsigned char)*y
))
84 * Arguments: @int argc@ = number of command line arguments
85 * @char *argv[]@ = vector of arguments
87 * Returns: Zero if OK, nonzero on error.
89 * Use: Main program. Provides a simple VPN.
92 static void usage(FILE *fp
)
94 pquis(fp
, "Usage: $ [-D] [-d DIR] [-b ADDR] [-p PORT] [-n TUNNEL]\n\
95 [-U USER] [-G GROUP] [-a SOCKET] [-T TRACE-OPTS]\n\
96 [-k PRIV-KEYRING] [-K PUB-KEYRING] [-t KEY-TAG]\n");
99 static void version(FILE *fp
) { pquis(fp
, "$, version " VERSION
"\n"); }
101 static void help(FILE *fp
)
109 -h, --help Display this help text.\n\
110 -v, --version Display version number.\n\
111 -u, --usage Display pointless usage message.\n\
112 --tunnels Display IP tunnel drivers and exit.\n\
114 -D, --daemon Run in the background.\n\
115 -d, --directory=DIR Switch to directory DIR [default " CONFIGDIR
"].\n\
116 -b, --bind-address=ADDR Bind UDP socket to this IP ADDR.\n\
117 -p, --port=PORT Select UDP port to listen to.\n\
118 -n, --tunnel=TUNNEL Seelect default tunnel driver.\n\
119 -U, --setuid=USER Set uid to USER after initialization.\n\
120 -G, --setgid=GROUP Set gid to GROUP after initialization.\n\
121 -k, --priv-keyring=FILE Get private key from FILE.\n\
122 -K, --pub-keyring=FILE Get public keys from FILE.\n\
123 -t, --tag=KEYTAG Use private key labelled TAG.\n\
124 -a, --admin-socket=FILE Use FILE as the adminstration socket.\n\
126 -T, --trace=OPTIONS Turn on tracing options.\n\
131 int main(int argc
, char *argv
[])
133 const char *kr_priv
= "keyring", *kr_pub
= "keyring.pub";
134 const char *tag_priv
= "tripe-dh";
135 const char *csock
= SOCKETDIR
"/tripesock";
136 const char *dir
= CONFIGDIR
;
139 struct in_addr baddr
= { INADDR_ANY
};
151 T( trace_on(stderr
, 0); )
153 if ((p
= getenv("TRIPEDIR")) != 0)
155 if ((p
= getenv("TRIPESOCK")) != 0)
157 tun_default
= tunnels
[0];
160 static const struct option opts
[] = {
161 { "help", 0, 0, 'h' },
162 { "version", 0, 0, 'v' },
163 { "usage", 0, 0, 'u' },
164 { "tunnels", 0, 0, '0' },
166 { "daemon", 0, 0, 'D' },
167 { "uid", OPTF_ARGREQ
, 0, 'U' },
168 { "setuid", OPTF_ARGREQ
, 0, 'U' },
169 { "gid", OPTF_ARGREQ
, 0, 'G' },
170 { "setgid", OPTF_ARGREQ
, 0, 'G' },
171 { "bind-address", OPTF_ARGREQ
, 0, 'b' },
172 { "tunnel", OPTF_ARGREQ
, 0, 'n' },
173 { "port", OPTF_ARGREQ
, 0, 'p' },
174 { "directory", OPTF_ARGREQ
, 0, 'd' },
175 { "priv-keyring", OPTF_ARGREQ
, 0, 'k' },
176 { "pub-keyring", OPTF_ARGREQ
, 0, 'K' },
177 { "tag", OPTF_ARGREQ
, 0, 't' },
178 { "admin-socket", OPTF_ARGREQ
, 0, 'a' },
180 { "trace", OPTF_ARGREQ
, 0, 'T' },
186 i
= mdwopt(argc
, argv
, "hvuDU:G:b:p:d:k:K:t:a:" T("T:"),
207 unsigned long i
= strtoul(optarg
, &p
, 0);
211 pw
= getpwnam(optarg
);
213 die(EXIT_FAILURE
, "user `%s' not found", optarg
);
221 unsigned long i
= strtoul(optarg
, &p
, 0);
225 gr
= getgrnam(optarg
);
227 die(EXIT_FAILURE
, "group `%s' not found", optarg
);
232 struct hostent
*h
= gethostbyname(optarg
);
234 die(EXIT_FAILURE
, "unknown host name `%s'", optarg
);
235 memcpy(&baddr
, h
->h_addr
, sizeof(struct in_addr
));
239 unsigned long i
= strtoul(optarg
, &p
, 0);
241 struct servent
*s
= getservbyname(optarg
, "udp");
243 die(EXIT_FAILURE
, "unknown service name `%s'", optarg
);
244 i
= ntohs(s
->s_port
);
246 if (i
== 0 || i
>= 65536)
247 die(EXIT_FAILURE
, "bad port number %lu", i
);
254 die(EXIT_FAILURE
, "unknown tunnel `%s'", optarg
);
255 if (mystrieq(optarg
, tunnels
[i
]->name
))
258 tun_default
= tunnels
[i
];
277 tr_flags
= traceopt(tr_opts
, optarg
, tr_flags
, 0);
278 trace_level(tr_flags
);
283 for (i
= 0; tunnels
[i
]; i
++)
284 puts(tunnels
[i
]->name
);
293 if (optind
< argc
|| (f
& f_bogus
)) {
299 die(EXIT_FAILURE
, "can't set current directory to `%s': %s",
300 dir
, strerror(errno
));
305 rand_noisesrc(RAND_GLOBAL
, &noise_source
);
306 rand_seed(RAND_GLOBAL
, MAXHASHSZ
);
307 signal(SIGPIPE
, SIG_IGN
);
308 for (i
= 0; tunnels
[i
]; i
++)
311 if (!(f
& f_daemon
)) {
313 a_create(STDIN_FILENO
, STDOUT_FILENO
, AF_TRACE
| AF_WARN
);
315 a_create(STDIN_FILENO
, STDOUT_FILENO
, AF_WARN
);
318 if (g
!= (gid_t
)-1) {
319 if (setgid(g
) || (getuid() == 0 && setgroups(1, &g
))) {
320 die(EXIT_FAILURE
, "couldn't setgid to %u: %s",
321 (unsigned)g
, strerror(errno
));
324 if (u
!= (uid_t
)-1) {
326 die(EXIT_FAILURE
, "couldn't setuid to %u: %s",
327 (unsigned)u
, strerror(errno
));
330 km_init(kr_priv
, kr_pub
, tag_priv
);
334 die(EXIT_FAILURE
, "couldn't become a daemon: %s", strerror(errno
));
338 tv
.tv_sec
= time(0) + T_INTERVAL
;
340 sel_addtimer(&sel
, &it
, &tv
, interval
, 0);
344 if (!sel_select(&sel
))
346 else if (errno
!= EINTR
&& errno
!= EAGAIN
) {
347 a_warn("SERVER", "select-error", "?ERRNO", A_END
);
350 a_warn("ABORT", "repeated-select-errors", A_END
);
359 /*----- That's all, folks -------------------------------------------------*/