af5c1838ee7a49909cd26d82cb9cf7cf33dbfac6
5 * (c) 2001 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Trivial IP Encryption (TrIPE).
12 * TrIPE is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * TrIPE is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with TrIPE; if not, write to the Free Software Foundation,
24 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 /*----- Header files ------------------------------------------------------*/
39 #include <sys/types.h>
46 #include <sys/socket.h>
48 #include <arpa/inet.h>
51 #include <mLib/alloc.h>
52 #include <mLib/daemonize.h>
53 #include <mLib/darray.h>
54 #include <mLib/dstr.h>
55 #include <mLib/mdwopt.h>
56 #include <mLib/quis.h>
57 #include <mLib/report.h>
59 #include <mLib/selbuf.h>
62 #include <mLib/versioncmp.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;
82 static const char *bgtag
= 0;
87 #define f_spawnopts 8u
94 /*----- Main code ---------------------------------------------------------*/
96 static void reap(int sig
)
99 while (waitpid(-1, 0, WNOHANG
) > 0)
104 static void writelog(const char *cat
, const char *msg
)
108 struct tm
*tm
= localtime(&t
);
109 strftime(buf
, sizeof(buf
), "%Y-%m-%d %H:%M:%S", tm
);
110 fprintf(logfp
, "%s %s: %s\n", buf
, cat
, msg
);
113 static void checkbg(char **p
)
115 char *q
= str_getword(p
);
117 die(EXIT_FAILURE
, "missing background tag");
118 if (!bgtag
|| strcmp(bgtag
, q
) != 0)
119 die(EXIT_FAILURE
, "unexpected background tag `%s'", q
);
122 static void checkfg(void)
125 die(EXIT_FAILURE
, "unexpected foreground response");
128 static void cline(char *p
, size_t len
, void *b
)
133 die(EXIT_FAILURE
, "server dropped the connection");
139 if (strcmp(q
, "WARN") == 0) {
141 syslog(LOG_WARNING
, "%s", p
);
143 writelog("warning", p
);
145 fprintf(stderr
, "Warning: %s\n", p
);
146 } else if (strcmp(q
, "TRACE") == 0) {
148 syslog(LOG_DEBUG
, "%s", p
);
150 writelog("debug", p
);
151 } else if (!(f
& f_command
)) {
153 syslog(LOG_ERR
, "unexpected output `%s %s'", q
, p
);
156 dstr_putf(&d
, "unexpected output `%s %s'", q
, p
);
157 writelog("error", d
.buf
);
160 } else if (strcmp(q
, "FAIL") == 0) {
162 die(EXIT_FAILURE
, "%s", p
);
163 } else if (strcmp(q
, "INFO") == 0) {
167 } else if (strcmp(q
, "OK") == 0) {
170 } else if (strcmp(q
, "BGDETACH") == 0) {
172 die(EXIT_FAILURE
, "repeat detach");
174 } else if (strcmp(q
, "BGOK") == 0) {
177 } else if (strcmp(q
, "BGINFO") == 0) {
181 } else if (strcmp(q
, "BGFAIL") == 0) {
183 die(EXIT_FAILURE
, "%s", p
);
185 die(EXIT_FAILURE
, "unexpected output `%s %s'", q
, p
);
188 static void sline(char *p
, size_t len
, void *b
)
192 moan("server closed the connection");
199 static void uline(char *p
, size_t len
, void *b
)
208 if (write(fd
, p
, len
+ 1) != len
+ 1)
209 moan("write failed: %s", strerror(errno
));
213 static void setup(const char *cmd
)
222 errno
= EIO
; /* Relax: be vague */
223 if (write(fd
, d
.buf
, d
.len
) != d
.len
) {
224 die(EXIT_FAILURE
, "error sending setup command `%s': %s",
225 cmd
, strerror(errno
));
229 n
= read(fd
, &ch
, 1);
231 die(EXIT_FAILURE
, "unexpected EOF during setup");
233 die(EXIT_FAILURE
, "error receiving reply to `%s': %s",
234 cmd
, strerror(errno
));
243 else if (strcmp(q
, "OK") == 0)
245 else if (strcmp(q
, "FAIL") == 0)
246 die(EXIT_FAILURE
, "setup command `%s' failed: %s", cmd
, p
);
252 static void logfile(const char *name
)
256 if ((fp
= fopen(name
, "a")) != 0) {
260 setvbuf(logfp
, 0, _IOLBF
, BUFSIZ
);
263 dstr_putf(&d
, "error opening logfile `%s': %s", name
, strerror(errno
));
265 writelog("error", d
.buf
);
267 die(EXIT_FAILURE
, d
.buf
);
269 syslog(LOG_ERR
, d
.buf
);
274 static void sighup(int sig
, void *v
) { logfile(logname
); }
276 static void cleanup(void)
282 static void sigdie(int sig
)
285 signal(sig
, SIG_DFL
);
289 static void version(FILE *fp
)
290 { pquis(fp
, "$, TrIPE version " VERSION
"\n"); }
292 static void usage(FILE *fp
)
296 $ [-w] [-OPTIONS] [COMMAND [ARGS]...]\n\
297 $ [-Dl] [-f FILE] [-OPTIONS]\n\
299 [-s] [-d DIRECTORY] [-a SOCKET] [-P PIDFILE]\n\
300 [-p PROGRAM] [-S ARG,ARG,...]\n\
304 static void help(FILE *fp
)
313 -h, --help Show this help text.\n\
314 -v, --version Show version number.\n\
315 -u, --usage Show brief usage message.\n\
317 -D, --daemon Become a background task after connecting.\n\
318 -d, --directory=DIR Select current directory [default " CONFIGDIR
"].\n\
319 -a, --admin-socket=FILE Select socket to connect to\n\
320 [default " SOCKETDIR
"/tripesock].\n\
321 -P, --pidfile=FILE Write process-id to FILE.\n\
323 -s, --spawn Start server rather than connecting.\n\
324 -p, --spawn-path=PATH Specify path to executable.\n\
325 -S, --spawn-args=ARGS Specify comma-separated arguments.\n\
327 -l, --syslog Log messages to system log.\n\
328 -f, --logfile=FILE Log messages to FILE.\n\
329 -w, --warnings Show warnings when running commands.\n\
333 int main(int argc
, char *argv
[])
335 const char *dir
= CONFIGDIR
;
336 const char *sock
= SOCKETDIR
"/tripesock";
337 const char *spawnpath
= "tripe";
338 string_v spawnopts
= DA_INIT
;
344 if ((p
= getenv("TRIPEDIR")) != 0)
346 if ((p
= getenv("TRIPESOCK")) != 0)
349 /* --- Parse the arguments --- */
352 static const struct option opts
[] = {
353 { "help", 0, 0, 'h' },
354 { "version", 0, 0, 'v' },
355 { "usage", 0, 0, 'u' },
356 { "daemon", 0, 0, 'D' },
357 { "directory", OPTF_ARGREQ
, 0, 'd' },
358 { "admin-socket", OPTF_ARGREQ
, 0, 'a' },
359 { "spawn", 0, 0, 's' },
360 { "spawn-path", OPTF_ARGREQ
, 0, 'p' },
361 { "spawn-args", OPTF_ARGREQ
, 0, 'S' },
362 { "syslog", 0, 0, 'l' },
363 { "logfile", OPTF_ARGREQ
, 0, 'f' },
364 { "warnings", 0, 0, 'w' },
365 { "pidfile", OPTF_ARGREQ
, 0, 'P' },
369 int i
= mdwopt(argc
, argv
, "+hvuDd:a:sp:S:lwf:nP:", opts
, 0, 0, 0);
383 f
|= f_daemon
| f_noinput
;
399 f
|= f_spawn
| f_spawnopts
;
400 for (p
= strtok(optarg
, ","); p
; p
= strtok(0, ","))
401 DA_PUSH(&spawnopts
, p
);
404 f
|= f_syslog
| f_noinput
;
421 if ((f
& f_bogus
) || ((f
& f_noinput
) && optind
< argc
)) {
426 /* --- Set various things up --- */
429 die(EXIT_FAILURE
, "couldn't set `%s' as current directory: %s",
430 dir
, strerror(errno
));
434 if (!pidfile
&& (f
& f_daemon
) && ((f
& f_syslog
) || logname
))
435 pidfile
= "tripectl.pid";
436 if (pidfile
&& (pidfp
= fopen(pidfile
, "w")) == 0) {
437 die(EXIT_FAILURE
, "couldn't open `%s' for writing: %s",
438 pidfile
, strerror(errno
));
440 signal(SIGINT
, sigdie
);
441 signal(SIGQUIT
, sigdie
);
442 signal(SIGTERM
, sigdie
);
445 /* --- Connect to the server --- */
451 sigset_t newmask
, oldmask
;
453 sa
.sa_handler
= reap
;
454 sigemptyset(&sa
.sa_mask
);
455 sa
.sa_flags
= SA_NOCLDSTOP
;
457 sa
.sa_flags
|= SA_RESTART
;
459 sigaction(SIGCHLD
, &sa
, 0);
461 DA_UNSHIFT(&spawnopts
, (char *)sock
);
462 DA_UNSHIFT(&spawnopts
, "-a");
463 DA_UNSHIFT(&spawnopts
, "-d.");
464 DA_UNSHIFT(&spawnopts
, (char *)spawnpath
);
465 DA_PUSH(&spawnopts
, 0);
466 if (socketpair(PF_UNIX
, SOCK_STREAM
, 0, pfd
))
467 die(EXIT_FAILURE
, "error from socketpair: %s", strerror(errno
));
468 sigemptyset(&newmask
);
469 sigaddset(&newmask
, SIGCHLD
);
470 sigprocmask(SIG_BLOCK
, &newmask
, &oldmask
);
471 if ((kid
= fork()) < 0)
472 die(EXIT_FAILURE
, "fork failed: %s", strerror(errno
));
474 dup2(pfd
[1], STDIN_FILENO
);
475 dup2(pfd
[1], STDOUT_FILENO
);
485 execvp(DA(&spawnopts
)[0], DA(&spawnopts
));
486 die(127, "couldn't exec `%s': %s", spawnpath
, strerror(errno
));
488 sigprocmask(SIG_SETMASK
, &oldmask
, 0);
492 struct sockaddr_un sun
;
493 size_t sz
= strlen(sock
) + 1;
494 if (sz
> sizeof(sun
.sun_path
))
495 die(EXIT_FAILURE
, "socket name `%s' too long", sock
);
496 memset(&sun
, 0, sizeof(sun
));
497 sun
.sun_family
= AF_UNIX
;
498 memcpy(sun
.sun_path
, sock
, sz
);
499 sz
= sz
+ offsetof(struct sockaddr_un
, sun_path
);
500 if ((fd
= socket(PF_UNIX
, SOCK_STREAM
, 0)) < 0)
501 die(EXIT_FAILURE
, "error making socket: %s", strerror(errno
));
502 if (connect(fd
, (struct sockaddr
*)&sun
, sz
)) {
503 die(EXIT_FAILURE
, "error connecting to `%s': %s",
504 sun
.sun_path
, strerror(errno
));
510 die(EXIT_FAILURE
, "error becoming daemon: %s", strerror(errno
));
513 fprintf(pidfp
, "%li\n", (long)getpid());
516 signal(SIGPIPE
, SIG_IGN
);
518 /* --- If we're meant to be interactive, do that --- */
521 setup("WATCH -A+tw");
522 if (!(f
& f_noinput
) && optind
== argc
) {
527 selbuf_init(&bu
, &sel
, STDIN_FILENO
, uline
, &bu
);
528 selbuf_init(&bs
, &sel
, fd
, sline
, &bs
);
530 if (sel_select(&sel
) && errno
!= EINTR
&& errno
!= EAGAIN
)
531 die(EXIT_FAILURE
, "select failed: %s", strerror(errno
));
535 /* --- If there's a command, submit it --- */
539 setup((f
& f_warn
) ?
"WATCH -A+w" : "WATCH -A");
540 while (optind
< argc
)
541 u_quotify(&d
, argv
[optind
++]);
544 if (write(fd
, d
.buf
, d
.len
) != d
.len
|| shutdown(fd
, 1))
545 die(EXIT_FAILURE
, "write failed: %s", strerror(errno
));
550 /* --- Pull everything else out of the box --- */
558 selbuf_init(&b
, &sel
, fd
, cline
, 0);
561 openlog(QUIS
, 0, LOG_DAEMON
);
564 sig_add(&hup
, SIGHUP
, sighup
, 0);
567 if (sel_select(&sel
) && errno
!= EINTR
&& errno
!= EAGAIN
)
568 die(EXIT_FAILURE
, "select failed: %s", strerror(errno
));
575 /*----- That's all, folks -------------------------------------------------*/