3 * $Id: client.c,v 1.11 2003/07/13 11:19:49 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.11 2003/07/13 11:19:49 mdw
33 * Incopatible protocol fix! Include message type code under MAC tag to prevent
34 * cut-and-paste from key-exchange messages to general packet transport.
36 * Revision 1.10 2003/05/17 10:59:19 mdw
37 * Put a newline in the pidfile.
39 * Revision 1.9 2002/01/13 14:57:18 mdw
40 * Track @lbuf@ changes in mLib.
42 * Revision 1.8 2001/06/19 22:09:37 mdw
43 * Move the program name to the right place when constructing the arguments
44 * to pass to a new server.
46 * Revision 1.7 2001/02/22 09:07:54 mdw
47 * Write a pidfile on request, and delete it when finished.
49 * Revision 1.6 2001/02/22 09:06:08 mdw
50 * Fix logfile rotation to avoid program collapse.
52 * Revision 1.5 2001/02/16 21:23:39 mdw
53 * Use reliable signal handling for reopening logs.
55 * Revision 1.4 2001/02/06 09:34:53 mdw
56 * Change ERR response to FAIL for consistency with other programs.
58 * Revision 1.3 2001/02/04 17:10:15 mdw
59 * Reopen logfiles on receipt of @SIGHUP@ (not done very well). Don't
60 * change directory -- just mangle pathnames instead.
62 * Revision 1.2 2001/02/04 01:17:54 mdw
63 * Create a configuration header file to tidy up command lines.
65 * Revision 1.1 2001/02/03 20:26:37 mdw
70 /*----- Header files ------------------------------------------------------*/
82 #include <sys/types.h>
89 #include <sys/socket.h>
91 #include <arpa/inet.h>
94 #include <mLib/alloc.h>
95 #include <mLib/darray.h>
96 #include <mLib/dstr.h>
97 #include <mLib/mdwopt.h>
98 #include <mLib/quis.h>
99 #include <mLib/report.h>
100 #include <mLib/sel.h>
101 #include <mLib/selbuf.h>
102 #include <mLib/sig.h>
103 #include <mLib/str.h>
109 /*----- Data structures ---------------------------------------------------*/
113 DA_DECL(string_v
, char *);
116 /*----- Static variables --------------------------------------------------*/
118 static const char *pidfile
= 0;
119 static const char *logname
= 0;
120 static FILE *logfp
= 0;
121 static unsigned f
= 0;
127 #define f_spawnopts 8u
129 #define f_command 32u
130 #define f_noinput 64u
132 #define f_uclose 256u
134 /*----- Main code ---------------------------------------------------------*/
136 static void reap(int sig
)
139 while (waitpid(-1, 0, WNOHANG
) > 0)
144 static void writelog(const char *cat
, const char *msg
)
148 struct tm
*tm
= localtime(&t
);
149 strftime(buf
, sizeof(buf
), "%Y-%m-%d %H:%M:%S", tm
);
150 fprintf(logfp
, "%s %s: %s\n", buf
, cat
, msg
);
153 static void cline(char *p
, size_t len
, void *b
)
158 die(EXIT_FAILURE
, "server dropped the connection");
164 if (strcmp(q
, "WARN") == 0) {
166 syslog(LOG_WARNING
, "%s", p
);
168 writelog("warning", p
);
170 fprintf(stderr
, "Warning: %s\n", p
);
171 } else if (strcmp(q
, "TRACE") == 0) {
173 syslog(LOG_DEBUG
, "%s", p
);
175 writelog("debug", p
);
176 } else if (!(f
& f_command
)) {
178 syslog(LOG_ERR
, "unexpected output `%s %s'", q
, p
);
181 dstr_putf(&d
, "unexpected output `%s %s'", q
, p
);
182 writelog("error", d
.buf
);
185 } else if (strcmp(q
, "FAIL") == 0)
186 die(EXIT_FAILURE
, "%s", p
);
187 else if (strcmp(q
, "INFO") == 0)
189 else if (strcmp(q
, "OK") == 0)
192 die(EXIT_FAILURE
, "unexpected output `%s %s'", q
, p
);
195 static void sline(char *p
, size_t len
, void *b
)
199 moan("server closed the connection");
205 static void uline(char *p
, size_t len
, void *b
)
213 write(fd
, p
, len
+ 1);
217 static void logfile(const char *name
)
221 if ((fp
= fopen(name
, "a")) != 0) {
225 setvbuf(logfp
, 0, _IOLBF
, BUFSIZ
);
228 dstr_putf(&d
, "error opening logfile `%s': %s", name
, strerror(errno
));
230 writelog("error", d
.buf
);
232 die(EXIT_FAILURE
, d
.buf
);
234 syslog(LOG_ERR
, d
.buf
);
239 static void sighup(int sig
, void *v
)
244 static void cleanup(void)
250 static void sigdie(int sig
)
253 signal(sig
, SIG_DFL
);
257 static void version(FILE *fp
)
259 pquis(fp
, "$, TrIPE version " VERSION
"\n");
262 static void usage(FILE *fp
)
266 $ [-w] [-options] [command [args]...]\n\
267 $ [-Dl] [-f file] [-options]\n\
269 [-s] [-d directory] [-a socket] [-P pidfile]\n\
270 [-p program] [-S arg,arg,...]\n\
274 static void help(FILE *fp
)
283 -h, --help Show this help text.\n\
284 -v, --version Show version number.\n\
285 -u, --usage Show brief usage message.\n\
287 -D, --daemon Become a background task after connecting.\n\
288 -d, --directory=DIR Select current directory [default /var/lib/tripe]\n\
289 -a, --admin-socket=FILE Select socket to connect to.\n\
290 -P, --pidfile=FILE Write process-id to FILE.\n\
292 -s, --spawn Start server rather than connecting.\n\
293 -p, --spawn-path=PATH Specify path to executable.\n\
294 -S, --spawn-args=ARGS Specify comma-separated arguments.\n\
296 -l, --syslog Log messages to system log.\n\
297 -f, --logfile=FILE Log messages to FILE.\n\
298 -w, --warnings Show warnings when running commands.\n\
302 int main(int argc
, char *argv
[])
304 const char *dir
= "/var/lib/tripe";
305 const char *sock
= "tripesock";
306 const char *spawnpath
= "tripe";
307 string_v spawnopts
= DA_INIT
;
313 if ((p
= getenv("TRIPEDIR")) != 0)
316 /* --- Parse the arguments --- */
319 static const struct option opts
[] = {
320 { "help", 0, 0, 'h' },
321 { "version", 0, 0, 'v' },
322 { "usage", 0, 0, 'u' },
323 { "daemon", 0, 0, 'D' },
324 { "directory", OPTF_ARGREQ
, 0, 'd' },
325 { "admin-socket", OPTF_ARGREQ
, 0, 'a' },
326 { "spawn", 0, 0, 's' },
327 { "spawn-path", OPTF_ARGREQ
, 0, 'p' },
328 { "spawn-args", OPTF_ARGREQ
, 0, 'S' },
329 { "syslog", 0, 0, 'l' },
330 { "logfile", OPTF_ARGREQ
, 0, 'f' },
331 { "warnings", 0, 0, 'w' },
332 { "pidfile", OPTF_ARGREQ
, 0, 'P' },
336 int i
= mdwopt(argc
, argv
, "+hvuDd:a:sp:S:lwf:nP:", opts
, 0, 0, 0);
350 f
|= f_daemon
| f_noinput
;
366 f
|= f_spawn
| f_spawnopts
;
367 for (p
= strtok(optarg
, ","); p
; p
= strtok(0, ","))
368 DA_PUSH(&spawnopts
, p
);
371 f
|= f_syslog
| f_noinput
;
388 if ((f
& f_bogus
) || ((f
& f_noinput
) && optind
< argc
)) {
393 /* --- Set various things up --- */
396 die(EXIT_FAILURE
, "couldn't set `%s' as current directory: %s",
397 dir
, strerror(errno
));
401 if (!pidfile
&& (f
& f_daemon
) && ((f
& f_syslog
) || logname
))
402 pidfile
= "tripectl.pid";
403 if (pidfile
&& (pidfp
= fopen(pidfile
, "w")) == 0) {
404 die(EXIT_FAILURE
, "couldn't open `%s' for writing: %s",
405 pidfile
, strerror(errno
));
407 signal(SIGINT
, sigdie
);
408 signal(SIGQUIT
, sigdie
);
409 signal(SIGTERM
, sigdie
);
412 /* --- Connect to the server --- */
418 sigset_t newmask
, oldmask
;
420 sa
.sa_handler
= reap
;
421 sigemptyset(&sa
.sa_mask
);
422 sa
.sa_flags
= SA_NOCLDSTOP
;
424 sa
.sa_flags
|= SA_RESTART
;
426 sigaction(SIGCHLD
, &sa
, 0);
428 DA_UNSHIFT(&spawnopts
, (char *)sock
);
429 DA_UNSHIFT(&spawnopts
, "-a");
430 DA_UNSHIFT(&spawnopts
, "-d.");
431 DA_UNSHIFT(&spawnopts
, (char *)spawnpath
);
432 DA_PUSH(&spawnopts
, 0);
433 if (socketpair(PF_UNIX
, SOCK_STREAM
, 0, pfd
))
434 die(EXIT_FAILURE
, "error from socketpair: %s", strerror(errno
));
435 sigemptyset(&newmask
);
436 sigaddset(&newmask
, SIGCHLD
);
437 sigprocmask(SIG_BLOCK
, &newmask
, &oldmask
);
438 if ((kid
= fork()) < 0)
439 die(EXIT_FAILURE
, "fork failed: %s", strerror(errno
));
441 dup2(pfd
[1], STDIN_FILENO
);
442 dup2(pfd
[1], STDOUT_FILENO
);
452 execvp(DA(&spawnopts
)[0], DA(&spawnopts
));
453 die(127, "couldn't exec `%s': %s", spawnpath
, strerror(errno
));
455 sigprocmask(SIG_SETMASK
, &oldmask
, 0);
459 struct sockaddr_un sun
;
460 size_t sz
= strlen(sock
) + 1;
461 if (sz
> sizeof(sun
.sun_path
))
462 die(EXIT_FAILURE
, "socket name `%s' too long", sock
);
463 memset(&sun
, 0, sizeof(sun
));
464 sun
.sun_family
= AF_UNIX
;
465 memcpy(sun
.sun_path
, sock
, sz
);
466 sz
= sz
+ offsetof(struct sockaddr_un
, sun_path
);
467 if ((fd
= socket(PF_UNIX
, SOCK_STREAM
, 0)) < 0)
468 die(EXIT_FAILURE
, "error making socket: %s", strerror(errno
));
469 if (connect(fd
, (struct sockaddr
*)&sun
, sz
)) {
470 die(EXIT_FAILURE
, "error connecting to `%s': %s",
471 sun
.sun_path
, strerror(errno
));
477 die(EXIT_FAILURE
, "error becoming daemon: %s", strerror(errno
));
480 fprintf(pidfp
, "%li\n", (long)getpid());
484 /* --- If we're meant to be interactive, do that --- */
486 if (!(f
& f_noinput
) && optind
== argc
) {
491 selbuf_init(&bu
, &sel
, STDIN_FILENO
, uline
, &bu
);
492 selbuf_init(&bs
, &sel
, fd
, sline
, &bs
);
494 if (sel_select(&sel
) && errno
!= EINTR
&& errno
!= EAGAIN
)
495 die(EXIT_FAILURE
, "select failed: %s", strerror(errno
));
499 /* --- If there's a command, submit it --- */
503 dstr_puts(&d
, argv
[optind
++]);
504 while (optind
< argc
) {
506 dstr_puts(&d
, argv
[optind
++]);
509 write(fd
, d
.buf
, d
.len
);
515 /* --- Pull everything else out of the box --- */
523 selbuf_init(&b
, &sel
, fd
, cline
, 0);
526 openlog(QUIS
, 0, LOG_DAEMON
);
529 sig_add(&hup
, SIGHUP
, sighup
, 0);
532 if (sel_select(&sel
) && errno
!= EINTR
&& errno
!= EAGAIN
)
533 die(EXIT_FAILURE
, "select failed: %s", strerror(errno
));
540 /*----- That's all, folks -------------------------------------------------*/