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
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * TrIPE is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with TrIPE; if not, write to the Free Software Foundation,
24 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 /*----- Header files ------------------------------------------------------*/
31 /*----- Global variables --------------------------------------------------*/
35 /*----- Static variables --------------------------------------------------*/
38 #define T_INTERVAL MIN(1)
40 /*----- Main code ---------------------------------------------------------*/
42 /* --- @interval@ --- *
44 * Arguments: @struct timeval *tv@ = time when called
45 * @void *v@ = boring pointer
49 * Use: Called periodically to do housekeeping tasks.
52 static void interval(struct timeval
*tv
, void *v
)
55 T( trace(T_PEER
, "peer: interval timer"); )
56 rand_seed(RAND_GLOBAL
, MAXHASHSZ
);
59 tvv
.tv_sec
+= T_INTERVAL
;
60 sel_addtimer(&sel
, &it
, &tvv
, interval
, v
);
65 * Arguments: @int argc@ = number of command line arguments
66 * @char *argv[]@ = vector of arguments
68 * Returns: Zero if OK, nonzero on error.
70 * Use: Main program. Provides a simple VPN.
73 static void usage(FILE *fp
)
75 pquis(fp
, "Usage: $ [-DF] [-d DIR] [-b ADDR] [-p PORT] [-n TUNNEL]\n\
76 [-U USER] [-G GROUP] [-a SOCKET] [-m MODE] [-T TRACE-OPTS]\n\
77 [-k PRIV-KEYRING] [-K PUB-KEYRING] [-t KEY-TAG]\n");
80 static void version(FILE *fp
) { pquis(fp
, "$, version " VERSION
"\n"); }
82 static void help(FILE *fp
)
90 -h, --help Display this help text.\n\
91 -v, --version Display version number.\n\
92 -u, --usage Display pointless usage message.\n\
93 --tunnels Display IP tunnel drivers and exit.\n\
95 -D, --daemon Run in the background.\n\
96 -F, --foreground Quit when stdin reports end-of-file.\n\
97 -d, --directory=DIR Switch to directory DIR [default " CONFIGDIR
"].\n\
98 -b, --bind-address=ADDR Bind UDP socket to this IP ADDR.\n\
99 -p, --port=PORT Select UDP port to listen to "
100 "[default " STR(TRIPE_PORT
) "].\n\
101 -n, --tunnel=TUNNEL Seelect default tunnel driver.\n\
102 -U, --setuid=USER Set uid to USER after initialization.\n\
103 -G, --setgid=GROUP Set gid to GROUP after initialization.\n\
104 -k, --priv-keyring=FILE Get private key from FILE.\n\
105 -K, --pub-keyring=FILE Get public keys from FILE.\n\
106 -t, --tag=KEYTAG Use private key labelled TAG.\n\
107 -a, --admin-socket=FILE Use FILE as the adminstration socket.\n\
108 -m, --admin-perms=MODE Permissions to set on admin socket [default 600].\n\
110 -T, --trace=OPTIONS Turn on tracing options.\n\
115 int main(int argc
, char *argv
[])
117 const char *kr_priv
= "keyring", *kr_pub
= "keyring.pub";
118 const char *tag_priv
= 0;
119 const char *csock
= SOCKETDIR
"/tripesock";
120 int csockmode
= 0600;
121 const char *dir
= CONFIGDIR
;
123 unsigned port
= TRIPE_PORT
;
124 struct in_addr baddr
= { INADDR_ANY
};
135 #define f_foreground 4u
138 T( trace_on(stderr
, 0); )
140 if ((p
= getenv("TRIPEDIR")) != 0)
142 if ((p
= getenv("TRIPESOCK")) != 0)
144 tun_default
= tunnels
[0];
147 static const struct option opts
[] = {
148 { "help", 0, 0, 'h' },
149 { "version", 0, 0, 'v' },
150 { "usage", 0, 0, 'u' },
151 { "tunnels", 0, 0, '0' },
153 { "daemon", 0, 0, 'D' },
154 { "foreground", 0, 0, 'F' },
155 { "uid", OPTF_ARGREQ
, 0, 'U' },
156 { "setuid", OPTF_ARGREQ
, 0, 'U' },
157 { "gid", OPTF_ARGREQ
, 0, 'G' },
158 { "setgid", OPTF_ARGREQ
, 0, 'G' },
159 { "bind-address", OPTF_ARGREQ
, 0, 'b' },
160 { "tunnel", OPTF_ARGREQ
, 0, 'n' },
161 { "port", OPTF_ARGREQ
, 0, 'p' },
162 { "directory", OPTF_ARGREQ
, 0, 'd' },
163 { "priv-keyring", OPTF_ARGREQ
, 0, 'k' },
164 { "pub-keyring", OPTF_ARGREQ
, 0, 'K' },
165 { "tag", OPTF_ARGREQ
, 0, 't' },
166 { "admin-socket", OPTF_ARGREQ
, 0, 'a' },
167 { "admin-perms", OPTF_ARGREQ
, 0, 'm' },
169 { "trace", OPTF_ARGREQ
, 0, 'T' },
175 i
= mdwopt(argc
, argv
, "hvuDFU:G:b:n:p:d:k:K:t:a:m:" T("T:"),
194 u
= u_getuser(optarg
, &g
);
197 g
= u_getgroup(optarg
);
204 struct hostent
*h
= gethostbyname(optarg
);
206 die(EXIT_FAILURE
, "unknown host name `%s'", optarg
);
207 memcpy(&baddr
, h
->h_addr
, sizeof(struct in_addr
));
211 unsigned long i
= strtoul(optarg
, &p
, 0);
213 struct servent
*s
= getservbyname(optarg
, "udp");
215 die(EXIT_FAILURE
, "unknown service name `%s'", optarg
);
216 i
= ntohs(s
->s_port
);
219 die(EXIT_FAILURE
, "bad port number %lu", i
);
226 die(EXIT_FAILURE
, "unknown tunnel `%s'", optarg
);
227 if (mystrieq(optarg
, tunnels
[i
]->name
))
230 tun_default
= tunnels
[i
];
246 csockmode
= strtol(optarg
, &p
, 8);
247 if (*p
) die(EXIT_FAILURE
, "bad permissions: `%s'", optarg
);
254 tr_flags
= traceopt(tr_opts
, optarg
, tr_flags
, 0);
255 trace_level(tr_flags
);
260 for (i
= 0; tunnels
[i
]; i
++)
261 puts(tunnels
[i
]->name
);
270 if (optind
< argc
|| (f
& f_bogus
)) {
274 if (!(~f
& (f_daemon
| f_foreground
)))
275 die(EXIT_FAILURE
, "foreground operation for a daemon is silly");
278 die(EXIT_FAILURE
, "can't set current directory to `%s': %s",
279 dir
, strerror(errno
));
284 rand_noisesrc(RAND_GLOBAL
, &noise_source
);
285 rand_seed(RAND_GLOBAL
, MAXHASHSZ
);
286 signal(SIGPIPE
, SIG_IGN
);
287 for (i
= 0; tunnels
[i
]; i
++)
290 if (!(f
& f_daemon
)) {
295 if (f
& f_foreground
)
297 a_create(STDIN_FILENO
, STDOUT_FILENO
, af
);
299 ps_split(f
& f_daemon
);
300 a_init(csock
, u
, g
, csockmode
);
302 km_init(kr_priv
, kr_pub
, tag_priv
);
305 die(EXIT_FAILURE
, "couldn't become a daemon: %s", strerror(errno
));
309 tv
.tv_sec
= time(0) + T_INTERVAL
;
311 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", "?ERRNO", A_END
);
321 a_warn("ABORT", "repeated-select-errors", A_END
);
330 /*----- That's all, folks -------------------------------------------------*/