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 /*----- Header files ------------------------------------------------------*/
41 #include <sys/types.h>
48 #include <sys/socket.h>
50 #include <arpa/inet.h>
53 #include <mLib/alloc.h>
54 #include <mLib/darray.h>
55 #include <mLib/dstr.h>
56 #include <mLib/mdwopt.h>
57 #include <mLib/quis.h>
58 #include <mLib/report.h>
60 #include <mLib/selbuf.h>
68 /*----- Data structures ---------------------------------------------------*/
72 DA_DECL(string_v
, char *);
75 /*----- Static variables --------------------------------------------------*/
77 static const char *pidfile
= 0;
78 static const char *logname
= 0;
79 static FILE *logfp
= 0;
80 static unsigned f
= 0;
86 #define f_spawnopts 8u
93 /*----- Main code ---------------------------------------------------------*/
95 static void reap(int sig
)
98 while (waitpid(-1, 0, WNOHANG
) > 0)
103 static void writelog(const char *cat
, const char *msg
)
107 struct tm
*tm
= localtime(&t
);
108 strftime(buf
, sizeof(buf
), "%Y-%m-%d %H:%M:%S", tm
);
109 fprintf(logfp
, "%s %s: %s\n", buf
, cat
, msg
);
112 static void cline(char *p
, size_t len
, void *b
)
117 die(EXIT_FAILURE
, "server dropped the connection");
123 if (strcmp(q
, "WARN") == 0) {
125 syslog(LOG_WARNING
, "%s", p
);
127 writelog("warning", p
);
129 fprintf(stderr
, "Warning: %s\n", p
);
130 } else if (strcmp(q
, "TRACE") == 0) {
132 syslog(LOG_DEBUG
, "%s", p
);
134 writelog("debug", p
);
135 } else if (!(f
& f_command
)) {
137 syslog(LOG_ERR
, "unexpected output `%s %s'", q
, p
);
140 dstr_putf(&d
, "unexpected output `%s %s'", q
, p
);
141 writelog("error", d
.buf
);
144 } else if (strcmp(q
, "FAIL") == 0)
145 die(EXIT_FAILURE
, "%s", p
);
146 else if (strcmp(q
, "INFO") == 0)
148 else if (strcmp(q
, "OK") == 0)
151 die(EXIT_FAILURE
, "unexpected output `%s %s'", q
, p
);
154 static void sline(char *p
, size_t len
, void *b
)
158 moan("server closed the connection");
164 static void uline(char *p
, size_t len
, void *b
)
173 if (write(fd
, p
, len
+ 1) != len
+ 1)
174 moan("write failed: %s", strerror(errno
));
178 static void setup(const char *cmd
)
187 errno
= EIO
; /* Relax: be vague */
188 if (write(fd
, d
.buf
, d
.len
) != d
.len
) {
189 die(EXIT_FAILURE
, "error sending setup command `%s': %s",
190 cmd
, strerror(errno
));
194 n
= read(fd
, &ch
, 1);
196 die(EXIT_FAILURE
, "unexpected EOF during setup");
198 die(EXIT_FAILURE
, "error receiving reply to `%s': %s",
199 cmd
, strerror(errno
));
208 else if (strcmp(q
, "OK") == 0)
210 else if (strcmp(q
, "FAIL") == 0)
211 die(EXIT_FAILURE
, "setup command `%s' failed: %s", cmd
, p
);
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 " CONFIGDIR
"].\n\
289 -a, --admin-socket=FILE Select socket to connect to\n\
290 [default " SOCKETDIR
"/tripesock].\n\
291 -P, --pidfile=FILE Write process-id to FILE.\n\
293 -s, --spawn Start server rather than connecting.\n\
294 -p, --spawn-path=PATH Specify path to executable.\n\
295 -S, --spawn-args=ARGS Specify comma-separated arguments.\n\
297 -l, --syslog Log messages to system log.\n\
298 -f, --logfile=FILE Log messages to FILE.\n\
299 -w, --warnings Show warnings when running commands.\n\
303 int main(int argc
, char *argv
[])
305 const char *dir
= CONFIGDIR
;
306 const char *sock
= SOCKETDIR
"/tripesock";
307 const char *spawnpath
= "tripe";
308 string_v spawnopts
= DA_INIT
;
314 if ((p
= getenv("TRIPEDIR")) != 0)
317 /* --- Parse the arguments --- */
320 static const struct option opts
[] = {
321 { "help", 0, 0, 'h' },
322 { "version", 0, 0, 'v' },
323 { "usage", 0, 0, 'u' },
324 { "daemon", 0, 0, 'D' },
325 { "directory", OPTF_ARGREQ
, 0, 'd' },
326 { "admin-socket", OPTF_ARGREQ
, 0, 'a' },
327 { "spawn", 0, 0, 's' },
328 { "spawn-path", OPTF_ARGREQ
, 0, 'p' },
329 { "spawn-args", OPTF_ARGREQ
, 0, 'S' },
330 { "syslog", 0, 0, 'l' },
331 { "logfile", OPTF_ARGREQ
, 0, 'f' },
332 { "warnings", 0, 0, 'w' },
333 { "pidfile", OPTF_ARGREQ
, 0, 'P' },
337 int i
= mdwopt(argc
, argv
, "+hvuDd:a:sp:S:lwf:nP:", opts
, 0, 0, 0);
351 f
|= f_daemon
| f_noinput
;
367 f
|= f_spawn
| f_spawnopts
;
368 for (p
= strtok(optarg
, ","); p
; p
= strtok(0, ","))
369 DA_PUSH(&spawnopts
, p
);
372 f
|= f_syslog
| f_noinput
;
389 if ((f
& f_bogus
) || ((f
& f_noinput
) && optind
< argc
)) {
394 /* --- Set various things up --- */
397 die(EXIT_FAILURE
, "couldn't set `%s' as current directory: %s",
398 dir
, strerror(errno
));
402 if (!pidfile
&& (f
& f_daemon
) && ((f
& f_syslog
) || logname
))
403 pidfile
= "tripectl.pid";
404 if (pidfile
&& (pidfp
= fopen(pidfile
, "w")) == 0) {
405 die(EXIT_FAILURE
, "couldn't open `%s' for writing: %s",
406 pidfile
, strerror(errno
));
408 signal(SIGINT
, sigdie
);
409 signal(SIGQUIT
, sigdie
);
410 signal(SIGTERM
, sigdie
);
413 /* --- Connect to the server --- */
419 sigset_t newmask
, oldmask
;
421 sa
.sa_handler
= reap
;
422 sigemptyset(&sa
.sa_mask
);
423 sa
.sa_flags
= SA_NOCLDSTOP
;
425 sa
.sa_flags
|= SA_RESTART
;
427 sigaction(SIGCHLD
, &sa
, 0);
429 DA_UNSHIFT(&spawnopts
, (char *)sock
);
430 DA_UNSHIFT(&spawnopts
, "-a");
431 DA_UNSHIFT(&spawnopts
, "-d.");
432 DA_UNSHIFT(&spawnopts
, (char *)spawnpath
);
433 DA_PUSH(&spawnopts
, 0);
434 if (socketpair(PF_UNIX
, SOCK_STREAM
, 0, pfd
))
435 die(EXIT_FAILURE
, "error from socketpair: %s", strerror(errno
));
436 sigemptyset(&newmask
);
437 sigaddset(&newmask
, SIGCHLD
);
438 sigprocmask(SIG_BLOCK
, &newmask
, &oldmask
);
439 if ((kid
= fork()) < 0)
440 die(EXIT_FAILURE
, "fork failed: %s", strerror(errno
));
442 dup2(pfd
[1], STDIN_FILENO
);
443 dup2(pfd
[1], STDOUT_FILENO
);
453 execvp(DA(&spawnopts
)[0], DA(&spawnopts
));
454 die(127, "couldn't exec `%s': %s", spawnpath
, strerror(errno
));
456 sigprocmask(SIG_SETMASK
, &oldmask
, 0);
460 struct sockaddr_un sun
;
461 size_t sz
= strlen(sock
) + 1;
462 if (sz
> sizeof(sun
.sun_path
))
463 die(EXIT_FAILURE
, "socket name `%s' too long", sock
);
464 memset(&sun
, 0, sizeof(sun
));
465 sun
.sun_family
= AF_UNIX
;
466 memcpy(sun
.sun_path
, sock
, sz
);
467 sz
= sz
+ offsetof(struct sockaddr_un
, sun_path
);
468 if ((fd
= socket(PF_UNIX
, SOCK_STREAM
, 0)) < 0)
469 die(EXIT_FAILURE
, "error making socket: %s", strerror(errno
));
470 if (connect(fd
, (struct sockaddr
*)&sun
, sz
)) {
471 die(EXIT_FAILURE
, "error connecting to `%s': %s",
472 sun
.sun_path
, strerror(errno
));
478 die(EXIT_FAILURE
, "error becoming daemon: %s", strerror(errno
));
481 fprintf(pidfp
, "%li\n", (long)getpid());
484 signal(SIGPIPE
, SIG_IGN
);
486 /* --- If we're meant to be interactive, do that --- */
489 setup("WATCH -A+tw");
490 if (!(f
& f_noinput
) && optind
== argc
) {
495 selbuf_init(&bu
, &sel
, STDIN_FILENO
, uline
, &bu
);
496 selbuf_init(&bs
, &sel
, fd
, sline
, &bs
);
498 if (sel_select(&sel
) && errno
!= EINTR
&& errno
!= EAGAIN
)
499 die(EXIT_FAILURE
, "select failed: %s", strerror(errno
));
503 /* --- If there's a command, submit it --- */
507 setup((f
& f_warn
) ?
"WATCH -A+w" : "WATCH -A");
508 dstr_puts(&d
, argv
[optind
++]);
509 while (optind
< argc
) {
511 dstr_puts(&d
, argv
[optind
++]);
515 if (write(fd
, d
.buf
, d
.len
) != d
.len
|| shutdown(fd
, 1))
516 die(EXIT_FAILURE
, "write failed: %s", strerror(errno
));
521 /* --- Pull everything else out of the box --- */
529 selbuf_init(&b
, &sel
, fd
, cline
, 0);
532 openlog(QUIS
, 0, LOG_DAEMON
);
535 sig_add(&hup
, SIGHUP
, sighup
, 0);
538 if (sel_select(&sel
) && errno
!= EINTR
&& errno
!= EAGAIN
)
539 die(EXIT_FAILURE
, "select failed: %s", strerror(errno
));
546 /*----- That's all, folks -------------------------------------------------*/