e2411afb304cf6b8776c2fbd0c96ef9f865dab20
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 --------------------------------------------------*/
78 static const char *pidfile
= 0;
79 static const char *logname
= 0;
80 static FILE *logfp
= 0;
81 static unsigned f
= 0;
83 static const char *bgtag
= 0;
88 #define f_spawnopts 8u
95 /*----- Main code ---------------------------------------------------------*/
97 static void reap(int sig
)
100 while (waitpid(-1, 0, WNOHANG
) > 0)
105 static void writelog(const char *cat
, const char *msg
)
109 struct tm
*tm
= localtime(&t
);
110 strftime(buf
, sizeof(buf
), "%Y-%m-%d %H:%M:%S", tm
);
111 fprintf(logfp
, "%s %s: %s\n", buf
, cat
, msg
);
114 static void checkbg(char **p
)
116 char *q
= str_getword(p
);
118 die(EXIT_FAILURE
, "missing background tag");
119 if (!bgtag
|| strcmp(bgtag
, q
) != 0)
120 die(EXIT_FAILURE
, "unexpected background tag `%s'", q
);
123 static void checkfg(void)
124 { if (bgtag
) die(EXIT_FAILURE
, "unexpected foreground response"); }
126 static void cline(char *p
, size_t len
, void *b
)
131 die(EXIT_FAILURE
, "server dropped the connection");
137 if (strcmp(q
, "WARN") == 0) {
139 syslog(LOG_WARNING
, "%s", p
);
141 writelog("warning", p
);
143 fprintf(stderr
, "Warning: %s\n", p
);
144 } else if (strcmp(q
, "TRACE") == 0) {
146 syslog(LOG_DEBUG
, "%s", p
);
148 writelog("debug", p
);
149 } else if (!(f
& f_command
)) {
151 syslog(LOG_ERR
, "unexpected output `%s %s'", q
, p
);
154 dstr_putf(&d
, "unexpected output `%s %s'", q
, p
);
155 writelog("error", d
.buf
);
158 } else if (strcmp(q
, "FAIL") == 0) {
160 die(EXIT_FAILURE
, "%s", p
);
161 } else if (strcmp(q
, "INFO") == 0) {
165 } else if (strcmp(q
, "OK") == 0) {
168 } else if (strcmp(q
, "BGDETACH") == 0) {
170 die(EXIT_FAILURE
, "repeat detach");
172 } else if (strcmp(q
, "BGOK") == 0) {
175 } else if (strcmp(q
, "BGINFO") == 0) {
179 } else if (strcmp(q
, "BGFAIL") == 0) {
181 die(EXIT_FAILURE
, "%s", p
);
183 die(EXIT_FAILURE
, "unexpected output `%s %s'", q
, p
);
186 static void sline(char *p
, size_t len
, void *b
)
190 moan("server closed the connection");
197 static void uline(char *p
, size_t len
, void *b
)
206 if (write(fd
, p
, len
+ 1) != len
+ 1)
207 moan("write failed: %s", strerror(errno
));
211 static void setup(const char *cmd
)
220 errno
= EIO
; /* Relax: be vague */
221 if (write(fd
, d
.buf
, d
.len
) != d
.len
) {
222 die(EXIT_FAILURE
, "error sending setup command `%s': %s",
223 cmd
, strerror(errno
));
227 n
= read(fd
, &ch
, 1);
229 die(EXIT_FAILURE
, "unexpected EOF during setup");
231 die(EXIT_FAILURE
, "error receiving reply to `%s': %s",
232 cmd
, strerror(errno
));
241 else if (strcmp(q
, "OK") == 0)
243 else if (strcmp(q
, "FAIL") == 0)
244 die(EXIT_FAILURE
, "setup command `%s' failed: %s", cmd
, p
);
250 static void logfile(const char *name
)
254 if ((fp
= fopen(name
, "a")) != 0) {
258 setvbuf(logfp
, 0, _IOLBF
, BUFSIZ
);
261 dstr_putf(&d
, "error opening logfile `%s': %s", name
, strerror(errno
));
263 writelog("error", d
.buf
);
265 die(EXIT_FAILURE
, d
.buf
);
267 syslog(LOG_ERR
, d
.buf
);
272 static void sighup(int sig
, void *v
) { logfile(logname
); }
274 static void cleanup(void) { if (pidfile
) unlink(pidfile
); }
276 static void sigdie(int sig
)
277 { cleanup(); signal(sig
, SIG_DFL
); raise(sig
); }
279 static void putarg(string_v
*av
, const char *fmt
, ...)
285 dstr_vputf(&d
, fmt
, &ap
);
288 DA_UNSHIFT(av
, xstrdup(d
.buf
));
292 static void version(FILE *fp
)
293 { pquis(fp
, "$, TrIPE version " VERSION
"\n"); }
295 static void usage(FILE *fp
)
299 $ [-w] [-OPTIONS] [COMMAND [ARGS]...]\n\
300 $ [-Dl] [-f FILE] [-OPTIONS]\n\
302 [-s] [-d DIRECTORY] [-a SOCKET] [-P PIDFILE]\n\
303 [-p PROGRAM] [-S ARG,ARG,...]\n\
307 static void help(FILE *fp
)
316 -h, --help Show this help text.\n\
317 -v, --version Show version number.\n\
318 -u, --usage Show brief usage message.\n\
320 -D, --daemon Become a background task after connecting.\n\
321 -d, --directory=DIR Select current directory [default " CONFIGDIR
"].\n\
322 -U, --setuid=USER Set uid to USER after initialization.\n\
323 -G, --setgid=GROUP Set gid to GROUP after initialization.\n\
324 -a, --admin-socket=FILE Select socket to connect to\n\
325 [default " SOCKETDIR
"/tripesock].\n\
326 -P, --pidfile=FILE Write process-id to FILE.\n\
328 -s, --spawn Start server rather than connecting.\n\
329 -p, --spawn-path=PATH Specify path to executable.\n\
330 -S, --spawn-args=ARGS Specify comma-separated arguments.\n\
332 -l, --syslog Log messages to system log.\n\
333 -f, --logfile=FILE Log messages to FILE.\n\
334 -w, --warnings Show warnings when running commands.\n\
338 int main(int argc
, char *argv
[])
340 const char *dir
= CONFIGDIR
;
341 const char *sock
= SOCKETDIR
"/tripesock";
342 const char *spawnpath
= "tripe";
343 string_v spawnopts
= DA_INIT
;
353 sigset_t newmask
, oldmask
;
354 struct sockaddr_un sun
;
361 if ((p
= getenv("TRIPEDIR")) != 0)
363 if ((p
= getenv("TRIPESOCK")) != 0)
366 /* --- Parse the arguments --- */
369 static const struct option opts
[] = {
370 { "help", 0, 0, 'h' },
371 { "version", 0, 0, 'v' },
372 { "usage", 0, 0, 'u' },
373 { "daemon", 0, 0, 'D' },
374 { "uid", OPTF_ARGREQ
, 0, 'U' },
375 { "setuid", OPTF_ARGREQ
, 0, 'U' },
376 { "gid", OPTF_ARGREQ
, 0, 'G' },
377 { "setgid", OPTF_ARGREQ
, 0, 'G' },
378 { "directory", OPTF_ARGREQ
, 0, 'd' },
379 { "admin-socket", OPTF_ARGREQ
, 0, 'a' },
380 { "spawn", 0, 0, 's' },
381 { "spawn-path", OPTF_ARGREQ
, 0, 'p' },
382 { "spawn-args", OPTF_ARGREQ
, 0, 'S' },
383 { "syslog", 0, 0, 'l' },
384 { "logfile", OPTF_ARGREQ
, 0, 'f' },
385 { "warnings", 0, 0, 'w' },
386 { "pidfile", OPTF_ARGREQ
, 0, 'P' },
390 i
= mdwopt(argc
, argv
, "+hvuDU:G:d:a:sp:S:lwf:nP:", opts
, 0, 0, 0);
404 f
|= f_daemon
| f_noinput
;
407 u
= u_getuser(optarg
, &g
);
410 g
= u_getgroup(optarg
);
426 f
|= f_spawn
| f_spawnopts
;
427 for (p
= strtok(optarg
, ","); p
; p
= strtok(0, ","))
428 DA_PUSH(&spawnopts
, p
);
431 f
|= f_syslog
| f_noinput
;
448 if ((f
& f_bogus
) || ((f
& f_noinput
) && optind
< argc
)) {
453 /* --- Set various things up --- */
456 die(EXIT_FAILURE
, "couldn't set `%s' as current directory: %s",
457 dir
, strerror(errno
));
461 if (!pidfile
&& (f
& f_daemon
) && ((f
& f_syslog
) || logname
))
462 pidfile
= "tripectl.pid";
463 if (pidfile
&& (pidfp
= fopen(pidfile
, "w")) == 0) {
464 die(EXIT_FAILURE
, "couldn't open `%s' for writing: %s",
465 pidfile
, strerror(errno
));
469 signal(SIGINT
, sigdie
);
470 signal(SIGQUIT
, sigdie
);
471 signal(SIGTERM
, sigdie
);
474 /* --- Connect to the server --- */
477 sa
.sa_handler
= reap
;
478 sigemptyset(&sa
.sa_mask
);
479 sa
.sa_flags
= SA_NOCLDSTOP
;
481 sa
.sa_flags
|= SA_RESTART
;
483 sigaction(SIGCHLD
, &sa
, 0);
485 DA_PUSH(&spawnopts
, 0);
486 if (g
!= (gid_t
)-1) putarg(&spawnopts
, "-G%lu", (unsigned long)g
);
487 if (u
!= (uid_t
)-1) putarg(&spawnopts
, "-U%lu", (unsigned long)u
);
488 putarg(&spawnopts
, "-a%s", sock
);
489 putarg(&spawnopts
, "-d.");
490 putarg(&spawnopts
, "-F");
491 putarg(&spawnopts
, "%s", spawnpath
);
492 if (socketpair(PF_UNIX
, SOCK_STREAM
, 0, pfd
))
493 die(EXIT_FAILURE
, "error from socketpair: %s", strerror(errno
));
494 sigemptyset(&newmask
);
495 sigaddset(&newmask
, SIGCHLD
);
496 sigprocmask(SIG_BLOCK
, &newmask
, &oldmask
);
497 if ((kid
= fork()) < 0)
498 die(EXIT_FAILURE
, "fork failed: %s", strerror(errno
));
500 dup2(pfd
[1], STDIN_FILENO
);
501 dup2(pfd
[1], STDOUT_FILENO
);
504 if (logfp
) fclose(logfp
);
505 if (pidfp
) fclose(pidfp
);
507 if (f
& f_daemon
) detachtty();
508 execvp(DA(&spawnopts
)[0], DA(&spawnopts
));
509 die(127, "couldn't exec `%s': %s", spawnpath
, strerror(errno
));
511 sigprocmask(SIG_SETMASK
, &oldmask
, 0);
515 sz
= strlen(sock
) + 1;
516 if (sz
> sizeof(sun
.sun_path
))
517 die(EXIT_FAILURE
, "socket name `%s' too long", sock
);
518 memset(&sun
, 0, sizeof(sun
));
519 sun
.sun_family
= AF_UNIX
;
520 memcpy(sun
.sun_path
, sock
, sz
);
521 sz
= sz
+ offsetof(struct sockaddr_un
, sun_path
);
522 if ((fd
= socket(PF_UNIX
, SOCK_STREAM
, 0)) < 0)
523 die(EXIT_FAILURE
, "error making socket: %s", strerror(errno
));
524 if (connect(fd
, (struct sockaddr
*)&sun
, sz
)) {
525 die(EXIT_FAILURE
, "error connecting to `%s': %s",
526 sun
.sun_path
, strerror(errno
));
533 die(EXIT_FAILURE
, "error becoming daemon: %s", strerror(errno
));
536 fprintf(pidfp
, "%li\n", (long)getpid());
539 signal(SIGPIPE
, SIG_IGN
);
541 /* --- If we're meant to be interactive, do that --- */
544 setup("WATCH -A+tw");
545 if (!(f
& f_noinput
) && optind
== argc
) {
546 selbuf_init(&bu
, &sel
, STDIN_FILENO
, uline
, &bu
);
547 selbuf_init(&bs
, &sel
, fd
, sline
, &bs
);
549 if (sel_select(&sel
) && errno
!= EINTR
&& errno
!= EAGAIN
)
550 die(EXIT_FAILURE
, "select failed: %s", strerror(errno
));
554 /* --- If there's a command, submit it --- */
557 setup((f
& f_warn
) ?
"WATCH -A+w" : "WATCH -A");
558 while (optind
< argc
)
559 u_quotify(&d
, argv
[optind
++]);
562 if (write(fd
, d
.buf
, d
.len
) != d
.len
|| shutdown(fd
, 1))
563 die(EXIT_FAILURE
, "write failed: %s", strerror(errno
));
568 /* --- Pull everything else out of the box --- */
570 selbuf_init(&bs
, &sel
, fd
, cline
, 0);
573 openlog(QUIS
, 0, LOG_DAEMON
);
575 sig_add(&hup
, SIGHUP
, sighup
, 0);
577 if (sel_select(&sel
) && errno
!= EINTR
&& errno
!= EAGAIN
)
578 die(EXIT_FAILURE
, "select failed: %s", strerror(errno
));
584 /*----- That's all, folks -------------------------------------------------*/