3 * $Id: tripe.c,v 1.5 2001/02/16 21:43:12 mdw Exp $
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 /*----- Revision history --------------------------------------------------*
32 * Revision 1.5 2001/02/16 21:43:12 mdw
33 * Provide a more helpful usage message.
35 * Revision 1.4 2001/02/16 21:41:31 mdw
38 * Revision 1.3 2001/02/04 17:10:40 mdw
39 * Remove a debugging @abort@ call.
41 * Revision 1.2 2001/02/03 22:33:00 mdw
42 * Stuff more randomness into the pool in the interval timer.
44 * Revision 1.1 2001/02/03 20:26:37 mdw
49 /*----- Header files ------------------------------------------------------*/
53 /*----- Global variables --------------------------------------------------*/
56 octet buf_i
[PKBUFSZ
], buf_o
[PKBUFSZ
], buf_t
[PKBUFSZ
];
58 /*----- Static variables --------------------------------------------------*/
61 #define T_INTERVAL MIN(1)
63 /*----- Main code ---------------------------------------------------------*/
65 /* --- @interval@ --- *
67 * Arguments: @struct timeval *tv@ = time when called
68 * @void *v@ = boring pointer
72 * Use: Called periodically to do housekeeping tasks.
75 void interval(struct timeval
*tv
, void *v
)
78 T( trace(T_PEER
, "peer: interval timer"); )
79 rand_seed(RAND_GLOBAL
, HASHSZ
);
82 tvv
.tv_sec
+= T_INTERVAL
;
83 sel_addtimer(&sel
, &it
, &tvv
, interval
, v
);
88 * Arguments: @int argc@ = number of command line arguments
89 * @char *argv[]@ = vector of arguments
91 * Returns: Zero if OK, nonzero on error.
93 * Use: Main program. Provides a simple VPN.
96 static void usage(FILE *fp
)
98 pquis(fp
, "Usage: $ [-D] [-p port] [-T trace-opts] [-d dir] [-a socket]\n\
99 [-k priv-keyring] [-K pub-keyring] [-t key-tag]\n");
102 static void version(FILE *fp
)
104 pquis(fp
, "$, version " VERSION
"\n");
107 static void help(FILE *fp
)
115 -h, --help Display this help text.\n\
116 -v, --version Display version number.\n\
117 -u, --usage Display pointless usage message.\n\
119 -D, --daemon Run in the background.\n\
120 -d, --directory=DIR Switch to directory DIR (default $TRIPEDIR).\n\
121 -p, --port=PORT Select UDP port to listen to.\n\
122 -k, --priv-keyring=FILE Get private key from FILE.\n\
123 -K, --pub-keyring=FILE Get public keys from FILE.\n\
124 -t, --tag=KEYTAG Use private key labelled TAG.\n\
125 -a, --admin-socket=FILE Use FILE as the adminstration socket.\n\
126 -T, --trace=OPTIONS Turn on tracing options.\n\
130 int main(int argc
, char *argv
[])
132 const char *kr_priv
= "keyring", *kr_pub
= "keyring.pub";
133 const char *tag_priv
= "tripe-dh";
134 const char *csock
= "tripesock";
135 const char *dir
= "/var/lib/tripe";
148 if ((p
= getenv("TRIPEDIR")) != 0)
152 static const struct option opts
[] = {
153 { "help", 0, 0, 'h' },
154 { "version", 0, 0, 'v' },
155 { "usage", 0, 0, 'u' },
157 { "daemon", 0, 0, 'D' },
158 { "uid", OPTF_ARGREQ
, 0, 'U' },
159 { "setuid", OPTF_ARGREQ
, 0, 'U' },
160 { "gid", OPTF_ARGREQ
, 0, 'G' },
161 { "setgid", OPTF_ARGREQ
, 0, 'G' },
162 { "port", OPTF_ARGREQ
, 0, 'p' },
163 { "directory", OPTF_ARGREQ
, 0, 'd' },
164 { "priv-keyring", OPTF_ARGREQ
, 0, 'k' },
165 { "pub-keyring", OPTF_ARGREQ
, 0, 'K' },
166 { "tag", OPTF_ARGREQ
, 0, 't' },
167 { "admin-socket", OPTF_ARGREQ
, 0, 'a' },
169 { "trace", OPTF_ARGREQ
, 0, 'T' },
175 int i
= mdwopt(argc
, argv
, "hvu DU:G: p:d:k:K:t:a:" T("T:"),
195 unsigned long i
= strtoul(optarg
, &p
, 0);
200 if ((pw
= getpwnam(optarg
)) == 0)
201 die(EXIT_FAILURE
, "user name `%s' not found", optarg
);
207 unsigned long i
= strtoul(optarg
, &p
, 0);
212 if ((gr
= getgrnam(optarg
)) == 0)
213 die(EXIT_FAILURE
, "group name `%s' not found", optarg
);
220 unsigned long i
= strtoul(optarg
, &p
, 0);
222 struct servent
*s
= getservbyname(optarg
, "udp");
224 die(EXIT_FAILURE
, "unknown service name `%s'", optarg
);
225 i
= ntohs(s
->s_port
);
227 if (i
== 0 || i
>= 65536)
228 die(EXIT_FAILURE
, "bad port number %lu", i
);
248 tr_flags
= traceopt(tr_opts
, optarg
, tr_flags
, 0);
249 trace_level(tr_flags
);
258 if (optind
< argc
|| (f
& f_bogus
)) {
264 die(EXIT_FAILURE
, "can't set current directory to `%s': %s",
265 dir
, strerror(errno
));
270 rand_noisesrc(RAND_GLOBAL
, &noise_source
);
271 rand_seed(RAND_GLOBAL
, RMD160_HASHSZ
);
272 signal(SIGPIPE
, SIG_IGN
);
276 a_create(STDIN_FILENO
, STDOUT_FILENO
);
279 die(EXIT_FAILURE
, "couldn't setgid to %u: %s",
280 (unsigned)g
, strerror(errno
));
285 die(EXIT_FAILURE
, "couldn't setuid to %u: %s",
286 (unsigned)u
, strerror(errno
));
289 km_init(kr_priv
, kr_pub
, tag_priv
);
293 die(EXIT_FAILURE
, "couldn't become a daemon: %s", strerror(errno
));
299 tv
.tv_sec
= time(0) + T_INTERVAL
;
301 sel_addtimer(&sel
, &it
, &tv
, interval
, 0);
307 if (!sel_select(&sel
))
309 else if (errno
!= EINTR
&& errno
!= EAGAIN
) {
310 a_warn("select failed: %s", strerror(errno
));
313 a_warn("too many select errors: bailing out");
323 /*----- That's all, folks -------------------------------------------------*/