3 * $Id: tripe.c,v 1.1 2001/02/03 20:26:37 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.1 2001/02/03 20:26:37 mdw
37 /*----- Header files ------------------------------------------------------*/
41 /*----- Global variables --------------------------------------------------*/
44 octet buf_i
[PKBUFSZ
], buf_o
[PKBUFSZ
];
46 /*----- Static variables --------------------------------------------------*/
49 #define T_INTERVAL MIN(1)
51 /*----- Main code ---------------------------------------------------------*/
53 /* --- @interval@ --- *
55 * Arguments: @struct timeval *tv@ = time when called
56 * @void *v@ = boring pointer
60 * Use: Called periodically to do housekeeping tasks.
63 void interval(struct timeval
*tv
, void *v
)
66 T( trace(T_PEER
, "peer: interval timer"); )
69 tvv
.tv_sec
+= T_INTERVAL
;
70 sel_addtimer(&sel
, &it
, &tvv
, interval
, v
);
75 * Arguments: @int argc@ = number of command line arguments
76 * @char *argv[]@ = vector of arguments
78 * Returns: Zero if OK, nonzero on error.
80 * Use: Main program. Provides a simple VPN.
83 static void usage(FILE *fp
)
85 pquis(fp
, "Usage: $ [-options]\n");
88 static void version(FILE *fp
)
90 pquis(fp
, "$, version " VERSION
"\n");
93 static void help(FILE *fp
)
101 -h, --help Display this help text.\n\
102 -v, --version Display version number.\n\
103 -u, --usage Display pointless usage message.\n\
105 -D, --daemon Run in the background.\n\
106 -d, --directory=DIR Switch to directory DIR (default $TRIPEDIR).\n\
107 -p, --port=PORT Select UDP port to listen to.\n\
108 -k, --priv-keyring=FILE Get private key from FILE.\n\
109 -K, --pub-keyring=FILE Get public keys from FILE.\n\
110 -t, --tag=KEYTAG Use private key labelled TAG.\n\
111 -a, --admin-socket=FILE Use FILE as the adminstration socket.\n\
112 -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
= "tripesock";
121 const char *dir
= "/var/lib/tripe";
134 if ((p
= getenv("TRIPEDIR")) != 0)
138 static const struct option opts
[] = {
139 { "help", 0, 0, 'h' },
140 { "version", 0, 0, 'v' },
141 { "usage", 0, 0, 'u' },
143 { "daemon", 0, 0, 'D' },
144 { "uid", OPTF_ARGREQ
, 0, 'U' },
145 { "setuid", OPTF_ARGREQ
, 0, 'U' },
146 { "gid", OPTF_ARGREQ
, 0, 'G' },
147 { "setgid", OPTF_ARGREQ
, 0, 'G' },
148 { "port", OPTF_ARGREQ
, 0, 'p' },
149 { "directory", OPTF_ARGREQ
, 0, 'd' },
150 { "priv-keyring", OPTF_ARGREQ
, 0, 'k' },
151 { "pub-keyring", OPTF_ARGREQ
, 0, 'K' },
152 { "tag", OPTF_ARGREQ
, 0, 't' },
153 { "admin-socket", OPTF_ARGREQ
, 0, 'a' },
155 { "trace", OPTF_ARGREQ
, 0, 'T' },
161 int i
= mdwopt(argc
, argv
, "hvu DU:G: p:d:k:K:t:a:" T("T:"),
181 unsigned long i
= strtoul(optarg
, &p
, 0);
186 if ((pw
= getpwnam(optarg
)) == 0)
187 die(EXIT_FAILURE
, "user name `%s' not found", optarg
);
193 unsigned long i
= strtoul(optarg
, &p
, 0);
198 if ((gr
= getgrnam(optarg
)) == 0)
199 die(EXIT_FAILURE
, "group name `%s' not found", optarg
);
206 unsigned long i
= strtoul(optarg
, &p
, 0);
208 struct servent
*s
= getservbyname(optarg
, "udp");
210 die(EXIT_FAILURE
, "unknown service name `%s'", optarg
);
211 i
= ntohs(s
->s_port
);
213 if (i
== 0 || i
>= 65536)
214 die(EXIT_FAILURE
, "bad port number %lu", i
);
234 tr_flags
= traceopt(tr_opts
, optarg
, tr_flags
, 0);
235 trace_level(tr_flags
);
244 if (optind
< argc
|| (f
& f_bogus
)) {
250 die(EXIT_FAILURE
, "can't set current directory to `%s': %s",
251 dir
, strerror(errno
));
256 rand_noisesrc(RAND_GLOBAL
, &noise_source
);
257 rand_seed(RAND_GLOBAL
, RMD160_HASHSZ
);
258 signal(SIGPIPE
, SIG_IGN
);
262 a_create(STDIN_FILENO
, STDOUT_FILENO
);
265 die(EXIT_FAILURE
, "couldn't setgid to %u: %s",
266 (unsigned)g
, strerror(errno
));
271 die(EXIT_FAILURE
, "couldn't setuid to %u: %s",
272 (unsigned)u
, strerror(errno
));
275 km_init(kr_priv
, kr_pub
, tag_priv
);
279 die(EXIT_FAILURE
, "couldn't become a daemon: %s", strerror(errno
));
285 tv
.tv_sec
= time(0) + T_INTERVAL
;
287 sel_addtimer(&sel
, &it
, &tv
, interval
, 0);
293 if (!sel_select(&sel
))
295 else if (errno
!= EINTR
&& errno
!= EAGAIN
) {
296 a_warn("select failed: %s", strerror(errno
));
300 a_warn("too many select errors: bailing out");
310 /*----- That's all, folks -------------------------------------------------*/