3 * $Id: tripe.c,v 1.2 2001/02/03 22:33:00 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.2 2001/02/03 22:33:00 mdw
33 * Stuff more randomness into the pool in the interval timer.
35 * Revision 1.1 2001/02/03 20:26:37 mdw
40 /*----- Header files ------------------------------------------------------*/
44 /*----- Global variables --------------------------------------------------*/
47 octet buf_i
[PKBUFSZ
], buf_o
[PKBUFSZ
];
49 /*----- Static variables --------------------------------------------------*/
52 #define T_INTERVAL MIN(1)
54 /*----- Main code ---------------------------------------------------------*/
56 /* --- @interval@ --- *
58 * Arguments: @struct timeval *tv@ = time when called
59 * @void *v@ = boring pointer
63 * Use: Called periodically to do housekeeping tasks.
66 void interval(struct timeval
*tv
, void *v
)
69 T( trace(T_PEER
, "peer: interval timer"); )
70 rand_seed(RAND_GLOBAL
, RMD160_HASHSZ
);
73 tvv
.tv_sec
+= T_INTERVAL
;
74 sel_addtimer(&sel
, &it
, &tvv
, interval
, v
);
79 * Arguments: @int argc@ = number of command line arguments
80 * @char *argv[]@ = vector of arguments
82 * Returns: Zero if OK, nonzero on error.
84 * Use: Main program. Provides a simple VPN.
87 static void usage(FILE *fp
)
89 pquis(fp
, "Usage: $ [-options]\n");
92 static void version(FILE *fp
)
94 pquis(fp
, "$, version " VERSION
"\n");
97 static void help(FILE *fp
)
105 -h, --help Display this help text.\n\
106 -v, --version Display version number.\n\
107 -u, --usage Display pointless usage message.\n\
109 -D, --daemon Run in the background.\n\
110 -d, --directory=DIR Switch to directory DIR (default $TRIPEDIR).\n\
111 -p, --port=PORT Select UDP port to listen to.\n\
112 -k, --priv-keyring=FILE Get private key from FILE.\n\
113 -K, --pub-keyring=FILE Get public keys from FILE.\n\
114 -t, --tag=KEYTAG Use private key labelled TAG.\n\
115 -a, --admin-socket=FILE Use FILE as the adminstration socket.\n\
116 -T, --trace=OPTIONS Turn on tracing options.\n\
120 int main(int argc
, char *argv
[])
122 const char *kr_priv
= "keyring", *kr_pub
= "keyring.pub";
123 const char *tag_priv
= "tripe-dh";
124 const char *csock
= "tripesock";
125 const char *dir
= "/var/lib/tripe";
138 if ((p
= getenv("TRIPEDIR")) != 0)
142 static const struct option opts
[] = {
143 { "help", 0, 0, 'h' },
144 { "version", 0, 0, 'v' },
145 { "usage", 0, 0, 'u' },
147 { "daemon", 0, 0, 'D' },
148 { "uid", OPTF_ARGREQ
, 0, 'U' },
149 { "setuid", OPTF_ARGREQ
, 0, 'U' },
150 { "gid", OPTF_ARGREQ
, 0, 'G' },
151 { "setgid", OPTF_ARGREQ
, 0, 'G' },
152 { "port", OPTF_ARGREQ
, 0, 'p' },
153 { "directory", OPTF_ARGREQ
, 0, 'd' },
154 { "priv-keyring", OPTF_ARGREQ
, 0, 'k' },
155 { "pub-keyring", OPTF_ARGREQ
, 0, 'K' },
156 { "tag", OPTF_ARGREQ
, 0, 't' },
157 { "admin-socket", OPTF_ARGREQ
, 0, 'a' },
159 { "trace", OPTF_ARGREQ
, 0, 'T' },
165 int i
= mdwopt(argc
, argv
, "hvu DU:G: p:d:k:K:t:a:" T("T:"),
185 unsigned long i
= strtoul(optarg
, &p
, 0);
190 if ((pw
= getpwnam(optarg
)) == 0)
191 die(EXIT_FAILURE
, "user name `%s' not found", optarg
);
197 unsigned long i
= strtoul(optarg
, &p
, 0);
202 if ((gr
= getgrnam(optarg
)) == 0)
203 die(EXIT_FAILURE
, "group name `%s' not found", optarg
);
210 unsigned long i
= strtoul(optarg
, &p
, 0);
212 struct servent
*s
= getservbyname(optarg
, "udp");
214 die(EXIT_FAILURE
, "unknown service name `%s'", optarg
);
215 i
= ntohs(s
->s_port
);
217 if (i
== 0 || i
>= 65536)
218 die(EXIT_FAILURE
, "bad port number %lu", i
);
238 tr_flags
= traceopt(tr_opts
, optarg
, tr_flags
, 0);
239 trace_level(tr_flags
);
248 if (optind
< argc
|| (f
& f_bogus
)) {
254 die(EXIT_FAILURE
, "can't set current directory to `%s': %s",
255 dir
, strerror(errno
));
260 rand_noisesrc(RAND_GLOBAL
, &noise_source
);
261 rand_seed(RAND_GLOBAL
, RMD160_HASHSZ
);
262 signal(SIGPIPE
, SIG_IGN
);
266 a_create(STDIN_FILENO
, STDOUT_FILENO
);
269 die(EXIT_FAILURE
, "couldn't setgid to %u: %s",
270 (unsigned)g
, strerror(errno
));
275 die(EXIT_FAILURE
, "couldn't setuid to %u: %s",
276 (unsigned)u
, strerror(errno
));
279 km_init(kr_priv
, kr_pub
, tag_priv
);
283 die(EXIT_FAILURE
, "couldn't become a daemon: %s", strerror(errno
));
289 tv
.tv_sec
= time(0) + T_INTERVAL
;
291 sel_addtimer(&sel
, &it
, &tv
, interval
, 0);
297 if (!sel_select(&sel
))
299 else if (errno
!= EINTR
&& errno
!= EAGAIN
) {
300 a_warn("select failed: %s", strerror(errno
));
304 a_warn("too many select errors: bailing out");
314 /*----- That's all, folks -------------------------------------------------*/