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)
123 { if (bgtag
) die(EXIT_FAILURE
, "unexpected foreground response"); }
125 static void cline(char *p
, size_t len
, void *b
)
130 die(EXIT_FAILURE
, "server dropped the connection");
136 if (strcmp(q
, "WARN") == 0) {
138 syslog(LOG_WARNING
, "%s", p
);
140 writelog("warning", p
);
142 fprintf(stderr
, "Warning: %s\n", p
);
143 } else if (strcmp(q
, "TRACE") == 0) {
145 syslog(LOG_DEBUG
, "%s", p
);
147 writelog("debug", p
);
148 } else if (!(f
& f_command
)) {
150 syslog(LOG_ERR
, "unexpected output `%s %s'", q
, p
);
153 dstr_putf(&d
, "unexpected output `%s %s'", q
, p
);
154 writelog("error", d
.buf
);
157 } else if (strcmp(q
, "FAIL") == 0) {
159 die(EXIT_FAILURE
, "%s", p
);
160 } else if (strcmp(q
, "INFO") == 0) {
164 } else if (strcmp(q
, "OK") == 0) {
167 } else if (strcmp(q
, "BGDETACH") == 0) {
169 die(EXIT_FAILURE
, "repeat detach");
171 } else if (strcmp(q
, "BGOK") == 0) {
174 } else if (strcmp(q
, "BGINFO") == 0) {
178 } else if (strcmp(q
, "BGFAIL") == 0) {
180 die(EXIT_FAILURE
, "%s", p
);
182 die(EXIT_FAILURE
, "unexpected output `%s %s'", q
, p
);
185 static void sline(char *p
, size_t len
, void *b
)
189 moan("server closed the connection");
196 static void uline(char *p
, size_t len
, void *b
)
205 if (write(fd
, p
, len
+ 1) != len
+ 1)
206 moan("write failed: %s", strerror(errno
));
210 static void setup(const char *cmd
)
219 errno
= EIO
; /* Relax: be vague */
220 if (write(fd
, d
.buf
, d
.len
) != d
.len
) {
221 die(EXIT_FAILURE
, "error sending setup command `%s': %s",
222 cmd
, strerror(errno
));
226 n
= read(fd
, &ch
, 1);
228 die(EXIT_FAILURE
, "unexpected EOF during setup");
230 die(EXIT_FAILURE
, "error receiving reply to `%s': %s",
231 cmd
, strerror(errno
));
240 else if (strcmp(q
, "OK") == 0)
242 else if (strcmp(q
, "FAIL") == 0)
243 die(EXIT_FAILURE
, "setup command `%s' failed: %s", cmd
, p
);
249 static void logfile(const char *name
)
253 if ((fp
= fopen(name
, "a")) != 0) {
257 setvbuf(logfp
, 0, _IOLBF
, BUFSIZ
);
260 dstr_putf(&d
, "error opening logfile `%s': %s", name
, strerror(errno
));
262 writelog("error", d
.buf
);
264 die(EXIT_FAILURE
, d
.buf
);
266 syslog(LOG_ERR
, d
.buf
);
271 static void sighup(int sig
, void *v
) { logfile(logname
); }
273 static void cleanup(void) { if (pidfile
) unlink(pidfile
); }
275 static void sigdie(int sig
)
276 { cleanup(); signal(sig
, SIG_DFL
); raise(sig
); }
278 static void putarg(string_v
*av
, const char *fmt
, ...)
284 dstr_vputf(&d
, fmt
, &ap
);
287 DA_UNSHIFT(av
, xstrdup(d
.buf
));
291 static void version(FILE *fp
)
292 { pquis(fp
, "$, TrIPE version " VERSION
"\n"); }
294 static void usage(FILE *fp
)
298 $ [-w] [-OPTIONS] [COMMAND [ARGS]...]\n\
299 $ [-Dl] [-f FILE] [-OPTIONS]\n\
301 [-s] [-d DIRECTORY] [-a SOCKET] [-P PIDFILE]\n\
302 [-p PROGRAM] [-S ARG,ARG,...]\n\
306 static void help(FILE *fp
)
315 -h, --help Show this help text.\n\
316 -v, --version Show version number.\n\
317 -u, --usage Show brief usage message.\n\
319 -D, --daemon Become a background task after connecting.\n\
320 -d, --directory=DIR Select current directory [default " CONFIGDIR
"].\n\
321 -U, --setuid=USER Set uid to USER after initialization.\n\
322 -G, --setgid=GROUP Set gid to GROUP after initialization.\n\
323 -a, --admin-socket=FILE Select socket to connect to\n\
324 [default " SOCKETDIR
"/tripesock].\n\
325 -P, --pidfile=FILE Write process-id to FILE.\n\
327 -s, --spawn Start server rather than connecting.\n\
328 -p, --spawn-path=PATH Specify path to executable.\n\
329 -S, --spawn-args=ARGS Specify comma-separated arguments.\n\
331 -l, --syslog Log messages to system log.\n\
332 -f, --logfile=FILE Log messages to FILE.\n\
333 -w, --warnings Show warnings when running commands.\n\
337 int main(int argc
, char *argv
[])
339 const char *dir
= CONFIGDIR
;
340 const char *sock
= SOCKETDIR
"/tripesock";
341 const char *spawnpath
= "tripe";
342 string_v spawnopts
= DA_INIT
;
350 if ((p
= getenv("TRIPEDIR")) != 0)
352 if ((p
= getenv("TRIPESOCK")) != 0)
355 /* --- Parse the arguments --- */
358 static const struct option opts
[] = {
359 { "help", 0, 0, 'h' },
360 { "version", 0, 0, 'v' },
361 { "usage", 0, 0, 'u' },
362 { "daemon", 0, 0, 'D' },
363 { "uid", OPTF_ARGREQ
, 0, 'U' },
364 { "setuid", OPTF_ARGREQ
, 0, 'U' },
365 { "gid", OPTF_ARGREQ
, 0, 'G' },
366 { "setgid", OPTF_ARGREQ
, 0, 'G' },
367 { "directory", OPTF_ARGREQ
, 0, 'd' },
368 { "admin-socket", OPTF_ARGREQ
, 0, 'a' },
369 { "spawn", 0, 0, 's' },
370 { "spawn-path", OPTF_ARGREQ
, 0, 'p' },
371 { "spawn-args", OPTF_ARGREQ
, 0, 'S' },
372 { "syslog", 0, 0, 'l' },
373 { "logfile", OPTF_ARGREQ
, 0, 'f' },
374 { "warnings", 0, 0, 'w' },
375 { "pidfile", OPTF_ARGREQ
, 0, 'P' },
379 int i
= mdwopt(argc
, argv
, "+hvuDU:G:d:a:sp:S:lwf:nP:", opts
, 0, 0, 0);
393 f
|= f_daemon
| f_noinput
;
396 u
= u_getuser(optarg
, &g
);
399 g
= u_getgroup(optarg
);
415 f
|= f_spawn
| f_spawnopts
;
416 for (p
= strtok(optarg
, ","); p
; p
= strtok(0, ","))
417 DA_PUSH(&spawnopts
, p
);
420 f
|= f_syslog
| f_noinput
;
437 if ((f
& f_bogus
) || ((f
& f_noinput
) && optind
< argc
)) {
442 /* --- Set various things up --- */
445 die(EXIT_FAILURE
, "couldn't set `%s' as current directory: %s",
446 dir
, strerror(errno
));
450 if (!pidfile
&& (f
& f_daemon
) && ((f
& f_syslog
) || logname
))
451 pidfile
= "tripectl.pid";
452 if (pidfile
&& (pidfp
= fopen(pidfile
, "w")) == 0) {
453 die(EXIT_FAILURE
, "couldn't open `%s' for writing: %s",
454 pidfile
, strerror(errno
));
456 signal(SIGINT
, sigdie
);
457 signal(SIGQUIT
, sigdie
);
458 signal(SIGTERM
, sigdie
);
461 /* --- Connect to the server --- */
467 sigset_t newmask
, oldmask
;
469 sa
.sa_handler
= reap
;
470 sigemptyset(&sa
.sa_mask
);
471 sa
.sa_flags
= SA_NOCLDSTOP
;
473 sa
.sa_flags
|= SA_RESTART
;
475 sigaction(SIGCHLD
, &sa
, 0);
477 DA_PUSH(&spawnopts
, 0);
478 if (g
!= (gid_t
)-1) putarg(&spawnopts
, "-G%lu", (unsigned long)g
);
479 if (u
!= (uid_t
)-1) putarg(&spawnopts
, "-U%lu", (unsigned long)u
);
480 putarg(&spawnopts
, "-a%s", sock
);
481 putarg(&spawnopts
, "-d.");
482 putarg(&spawnopts
, "-F");
483 putarg(&spawnopts
, "%s", spawnpath
);
484 if (socketpair(PF_UNIX
, SOCK_STREAM
, 0, pfd
))
485 die(EXIT_FAILURE
, "error from socketpair: %s", strerror(errno
));
486 sigemptyset(&newmask
);
487 sigaddset(&newmask
, SIGCHLD
);
488 sigprocmask(SIG_BLOCK
, &newmask
, &oldmask
);
489 if ((kid
= fork()) < 0)
490 die(EXIT_FAILURE
, "fork failed: %s", strerror(errno
));
492 dup2(pfd
[1], STDIN_FILENO
);
493 dup2(pfd
[1], STDOUT_FILENO
);
496 if (logfp
) fclose(logfp
);
497 if (pidfp
) fclose(pidfp
);
499 if (f
& f_daemon
) detachtty();
500 execvp(DA(&spawnopts
)[0], DA(&spawnopts
));
501 die(127, "couldn't exec `%s': %s", spawnpath
, strerror(errno
));
503 sigprocmask(SIG_SETMASK
, &oldmask
, 0);
507 struct sockaddr_un sun
;
508 size_t sz
= strlen(sock
) + 1;
509 if (sz
> sizeof(sun
.sun_path
))
510 die(EXIT_FAILURE
, "socket name `%s' too long", sock
);
511 memset(&sun
, 0, sizeof(sun
));
512 sun
.sun_family
= AF_UNIX
;
513 memcpy(sun
.sun_path
, sock
, sz
);
514 sz
= sz
+ offsetof(struct sockaddr_un
, sun_path
);
515 if ((fd
= socket(PF_UNIX
, SOCK_STREAM
, 0)) < 0)
516 die(EXIT_FAILURE
, "error making socket: %s", strerror(errno
));
517 if (connect(fd
, (struct sockaddr
*)&sun
, sz
)) {
518 die(EXIT_FAILURE
, "error connecting to `%s': %s",
519 sun
.sun_path
, strerror(errno
));
526 die(EXIT_FAILURE
, "error becoming daemon: %s", strerror(errno
));
529 fprintf(pidfp
, "%li\n", (long)getpid());
532 signal(SIGPIPE
, SIG_IGN
);
534 /* --- If we're meant to be interactive, do that --- */
537 setup("WATCH -A+tw");
538 if (!(f
& f_noinput
) && optind
== argc
) {
543 selbuf_init(&bu
, &sel
, STDIN_FILENO
, uline
, &bu
);
544 selbuf_init(&bs
, &sel
, fd
, sline
, &bs
);
546 if (sel_select(&sel
) && errno
!= EINTR
&& errno
!= EAGAIN
)
547 die(EXIT_FAILURE
, "select failed: %s", strerror(errno
));
551 /* --- If there's a command, submit it --- */
555 setup((f
& f_warn
) ?
"WATCH -A+w" : "WATCH -A");
556 while (optind
< argc
)
557 u_quotify(&d
, argv
[optind
++]);
560 if (write(fd
, d
.buf
, d
.len
) != d
.len
|| shutdown(fd
, 1))
561 die(EXIT_FAILURE
, "write failed: %s", strerror(errno
));
566 /* --- Pull everything else out of the box --- */
574 selbuf_init(&b
, &sel
, fd
, cline
, 0);
577 openlog(QUIS
, 0, LOG_DAEMON
);
580 sig_add(&hup
, SIGHUP
, sighup
, 0);
583 if (sel_select(&sel
) && errno
!= EINTR
&& errno
!= EAGAIN
)
584 die(EXIT_FAILURE
, "select failed: %s", strerror(errno
));
591 /*----- That's all, folks -------------------------------------------------*/