| 1 | /* -*-c-*- |
| 2 | * |
| 3 | * Client for TrIPE |
| 4 | * |
| 5 | * (c) 2001 Straylight/Edgeware |
| 6 | */ |
| 7 | |
| 8 | /*----- Licensing notice --------------------------------------------------* |
| 9 | * |
| 10 | * This file is part of Trivial IP Encryption (TrIPE). |
| 11 | * |
| 12 | * TrIPE is free software: you can redistribute it and/or modify it under |
| 13 | * the terms of the GNU General Public License as published by the Free |
| 14 | * Software Foundation; either version 3 of the License, or (at your |
| 15 | * option) any later version. |
| 16 | * |
| 17 | * TrIPE is distributed in the hope that it will be useful, but WITHOUT |
| 18 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 19 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 20 | * for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License |
| 23 | * along with TrIPE. If not, see <https://www.gnu.org/licenses/>. |
| 24 | */ |
| 25 | |
| 26 | /*----- Header files ------------------------------------------------------*/ |
| 27 | |
| 28 | #include "config.h" |
| 29 | |
| 30 | #include <ctype.h> |
| 31 | #include <errno.h> |
| 32 | #include <signal.h> |
| 33 | #include <stdio.h> |
| 34 | #include <stdlib.h> |
| 35 | #include <string.h> |
| 36 | #include <time.h> |
| 37 | |
| 38 | #include <sys/types.h> |
| 39 | #include <sys/time.h> |
| 40 | #include <unistd.h> |
| 41 | #include <fcntl.h> |
| 42 | #include <sys/wait.h> |
| 43 | #include <syslog.h> |
| 44 | |
| 45 | #include <sys/socket.h> |
| 46 | #include <sys/un.h> |
| 47 | #include <arpa/inet.h> |
| 48 | #include <netdb.h> |
| 49 | |
| 50 | #include <mLib/alloc.h> |
| 51 | #include <mLib/daemonize.h> |
| 52 | #include <mLib/darray.h> |
| 53 | #include <mLib/dstr.h> |
| 54 | #include <mLib/macros.h> |
| 55 | #include <mLib/mdup.h> |
| 56 | #include <mLib/mdwopt.h> |
| 57 | #include <mLib/quis.h> |
| 58 | #include <mLib/report.h> |
| 59 | #include <mLib/sel.h> |
| 60 | #include <mLib/selbuf.h> |
| 61 | #include <mLib/sig.h> |
| 62 | #include <mLib/str.h> |
| 63 | #include <mLib/versioncmp.h> |
| 64 | |
| 65 | #include "util.h" |
| 66 | |
| 67 | #undef sun |
| 68 | |
| 69 | /*----- Data structures ---------------------------------------------------*/ |
| 70 | |
| 71 | #ifndef STRING_V |
| 72 | # define STRING_V |
| 73 | DA_DECL(string_v, char *); |
| 74 | #endif |
| 75 | |
| 76 | /*----- Static variables --------------------------------------------------*/ |
| 77 | |
| 78 | static sel_state sel; |
| 79 | static const char *pidfile = 0; |
| 80 | static const char *logname = 0; |
| 81 | static FILE *logfp = 0; |
| 82 | static unsigned f = 0; |
| 83 | static const char *watch = 0; |
| 84 | static int fd; |
| 85 | static const char *bgtag = 0; |
| 86 | |
| 87 | #define f_bogus 1u |
| 88 | #define f_spawn 2u |
| 89 | #define f_daemon 4u |
| 90 | #define f_spawnopts 8u |
| 91 | #define f_syslog 16u |
| 92 | #define f_command 32u |
| 93 | #define f_noinput 64u |
| 94 | #define f_warn 128u |
| 95 | #define f_uclose 256u |
| 96 | #define f_losing 512u |
| 97 | #define f_nostamp 1024u |
| 98 | |
| 99 | /*----- Main code ---------------------------------------------------------*/ |
| 100 | |
| 101 | static void reap(int sig) |
| 102 | { |
| 103 | int e = errno; |
| 104 | while (waitpid(-1, 0, WNOHANG) > 0) |
| 105 | ; |
| 106 | errno = e; |
| 107 | } |
| 108 | |
| 109 | static void writelog(const char *cat, const char *msg) |
| 110 | { |
| 111 | char buf[256]; |
| 112 | time_t t = time(0); |
| 113 | struct tm *tm = localtime(&t); |
| 114 | if (f & f_nostamp) buf[0] = 0; |
| 115 | else strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S ", tm); |
| 116 | fprintf(logfp, "%s%s: %s\n", buf, cat, msg); |
| 117 | } |
| 118 | |
| 119 | static void checkbg(char **p) |
| 120 | { |
| 121 | char *q = str_getword(p); |
| 122 | if (!q) |
| 123 | die(EXIT_FAILURE, "missing background tag"); |
| 124 | if (!bgtag || strcmp(bgtag, q) != 0) |
| 125 | die(EXIT_FAILURE, "unexpected background tag `%s'", q); |
| 126 | } |
| 127 | |
| 128 | static void PRINTF_LIKE(2, 3) dolog(int prio, const char *msg, ...) |
| 129 | { |
| 130 | va_list ap; |
| 131 | dstr d = DSTR_INIT; |
| 132 | const char *cat; |
| 133 | |
| 134 | va_start(ap, msg); |
| 135 | dstr_vputf(&d, msg, &ap); |
| 136 | va_end(ap); |
| 137 | if (f & f_syslog) syslog(prio, "%s", d.buf); |
| 138 | if (logfp) { |
| 139 | switch (prio) { |
| 140 | case LOG_WARNING: cat = "warning"; break; |
| 141 | case LOG_DEBUG: cat = "debug"; break; |
| 142 | case LOG_ERR: cat = "error"; break; |
| 143 | default: cat = "message"; break; |
| 144 | } |
| 145 | writelog(cat, d.buf); |
| 146 | } |
| 147 | if (prio == LOG_WARNING && (f & f_warn)) |
| 148 | fprintf(stderr, "Warning: %s\n", d.buf); |
| 149 | dstr_destroy(&d); |
| 150 | } |
| 151 | |
| 152 | static void checkfg(void) |
| 153 | { if (bgtag) die(EXIT_FAILURE, "unexpected foreground response"); } |
| 154 | |
| 155 | static void cline(char *p, size_t len, void *b) |
| 156 | { |
| 157 | char *q; |
| 158 | if (!p) { |
| 159 | if (f & f_command) |
| 160 | die(EXIT_FAILURE, "server dropped the connection"); |
| 161 | f &= ~f_losing; |
| 162 | exit(0); |
| 163 | } |
| 164 | q = str_getword(&p); |
| 165 | if (!q) |
| 166 | return; |
| 167 | if (strcmp(q, "WARN") == 0) |
| 168 | dolog(LOG_WARNING, "%s", p); |
| 169 | else if (strcmp(q, "TRACE") == 0) |
| 170 | dolog(LOG_DEBUG, "%s", p); |
| 171 | else if (!(f & f_command)) |
| 172 | dolog(LOG_ERR, "unexpected output `%s %s'", q, p); |
| 173 | else if (strcmp(q, "FAIL") == 0) { |
| 174 | checkfg(); |
| 175 | die(EXIT_FAILURE, "%s", p); |
| 176 | } else if (strcmp(q, "INFO") == 0) { |
| 177 | checkfg(); |
| 178 | puts(p); |
| 179 | fflush(stdout); |
| 180 | } else if (strcmp(q, "OK") == 0) { |
| 181 | checkfg(); |
| 182 | exit(0); |
| 183 | } else if (strcmp(q, "BGDETACH") == 0) { |
| 184 | if (bgtag) |
| 185 | die(EXIT_FAILURE, "repeat detach"); |
| 186 | bgtag = xstrdup(p); |
| 187 | } else if (strcmp(q, "BGOK") == 0) { |
| 188 | checkbg(&p); |
| 189 | exit(0); |
| 190 | } else if (strcmp(q, "BGINFO") == 0) { |
| 191 | checkbg(&p); |
| 192 | puts(p); |
| 193 | fflush(stdout); |
| 194 | } else if (strcmp(q, "BGFAIL") == 0) { |
| 195 | checkbg(&p); |
| 196 | die(EXIT_FAILURE, "%s", p); |
| 197 | } else |
| 198 | die(EXIT_FAILURE, "unexpected output `%s %s'", q, p); |
| 199 | } |
| 200 | |
| 201 | static void sline(char *p, size_t len, void *b) |
| 202 | { |
| 203 | if (!p) { |
| 204 | if (!(f & f_uclose)) |
| 205 | moan("server closed the connection"); |
| 206 | exit(0); |
| 207 | } |
| 208 | puts(p); |
| 209 | fflush(stdout); |
| 210 | } |
| 211 | |
| 212 | static void uline(char *p, size_t len, void *b) |
| 213 | { |
| 214 | if (!p) { |
| 215 | selbuf_destroy(b); |
| 216 | shutdown(fd, 1); |
| 217 | f |= f_uclose; |
| 218 | } else { |
| 219 | p[len] = '\n'; |
| 220 | errno = EIO; |
| 221 | if (write(fd, p, len + 1) != len + 1) |
| 222 | moan("write failed: %s", strerror(errno)); |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | static void eline(char *p, size_t len, void *b) |
| 227 | { |
| 228 | if (p) |
| 229 | dolog(LOG_WARNING, "(stderr): %s", p); |
| 230 | else { |
| 231 | selbuf_destroy(b); |
| 232 | close(fd); |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | static void setup(const char *cmd) |
| 237 | { |
| 238 | dstr d = DSTR_INIT; |
| 239 | char ch; |
| 240 | char *p, *q; |
| 241 | int n; |
| 242 | |
| 243 | dstr_puts(&d, cmd); |
| 244 | dstr_putc(&d, '\n'); |
| 245 | errno = EIO; /* Relax: be vague */ |
| 246 | if (write(fd, d.buf, d.len) != d.len) { |
| 247 | die(EXIT_FAILURE, "error sending setup command `%s': %s", |
| 248 | cmd, strerror(errno)); |
| 249 | } |
| 250 | dstr_reset(&d); |
| 251 | for (;;) { |
| 252 | n = read(fd, &ch, 1); |
| 253 | if (!n) |
| 254 | die(EXIT_FAILURE, "unexpected EOF during setup"); |
| 255 | if (n < 0) { |
| 256 | die(EXIT_FAILURE, "error receiving reply to `%s': %s", |
| 257 | cmd, strerror(errno)); |
| 258 | } |
| 259 | if (d.len < 256) |
| 260 | dstr_putc(&d, ch); |
| 261 | if (ch == '\n') { |
| 262 | p = d.buf; |
| 263 | q = str_getword(&p); |
| 264 | if (!q) |
| 265 | ; |
| 266 | else if (strcmp(q, "OK") == 0) |
| 267 | return; |
| 268 | else if (strcmp(q, "FAIL") == 0) |
| 269 | die(EXIT_FAILURE, "setup command `%s' failed: %s", cmd, p); |
| 270 | dstr_reset(&d); |
| 271 | } |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | static void logfile(const char *name) |
| 276 | { |
| 277 | FILE *fp; |
| 278 | |
| 279 | if (strcmp(name, "-") == 0) |
| 280 | logfp = stdout; |
| 281 | else if (strcmp(name, "!") == 0) |
| 282 | logfp = stderr; |
| 283 | else if ((fp = fopen(name, "a")) != 0) { |
| 284 | if (logfp) |
| 285 | fclose(logfp); |
| 286 | logfp = fp; |
| 287 | setvbuf(logfp, 0, _IOLBF, BUFSIZ); |
| 288 | } else { |
| 289 | dstr d = DSTR_INIT; |
| 290 | dstr_putf(&d, "error opening logfile `%s': %s", name, strerror(errno)); |
| 291 | if (logfp) |
| 292 | writelog("error", d.buf); |
| 293 | else if (logname) |
| 294 | die(EXIT_FAILURE, "%s", d.buf); |
| 295 | if (f & f_syslog) |
| 296 | syslog(LOG_ERR, "%s", d.buf); |
| 297 | dstr_destroy(&d); |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | static void sighup(int sig, void *v) { logfile(logname); } |
| 302 | |
| 303 | static void cleanup(void) { if (pidfile) unlink(pidfile); } |
| 304 | |
| 305 | static void sigdie(int sig) |
| 306 | { cleanup(); signal(sig, SIG_DFL); raise(sig); } |
| 307 | |
| 308 | static void PRINTF_LIKE(2, 3) putarg(string_v *av, const char *fmt, ...) |
| 309 | { |
| 310 | va_list ap; |
| 311 | dstr d = DSTR_INIT; |
| 312 | |
| 313 | va_start(ap, fmt); |
| 314 | dstr_vputf(&d, fmt, &ap); |
| 315 | dstr_putz(&d); |
| 316 | va_end(ap); |
| 317 | DA_UNSHIFT(av, xstrdup(d.buf)); |
| 318 | dstr_destroy(&d); |
| 319 | } |
| 320 | |
| 321 | static void version(FILE *fp) |
| 322 | { pquis(fp, "$, TrIPE version " VERSION "\n"); } |
| 323 | |
| 324 | static void usage(FILE *fp) |
| 325 | { |
| 326 | pquis(fp, "\ |
| 327 | Usage:\n\ |
| 328 | $ [-w] [-OPTIONS] [COMMAND [ARGS]...]\n\ |
| 329 | $ [-Dl] [-f FILE] [-OPTIONS]\n\ |
| 330 | Options:\n\ |
| 331 | [-s] [-d DIRECTORY] [-a SOCKET] [-P PIDFILE]\n\ |
| 332 | [-p PROGRAM] [-S ARG,ARG,...]\n\ |
| 333 | "); |
| 334 | } |
| 335 | |
| 336 | static void help(FILE *fp) |
| 337 | { |
| 338 | version(fp); |
| 339 | fputc('\n', fp); |
| 340 | usage(fp); |
| 341 | fputs("\ |
| 342 | \n\ |
| 343 | Options in full:\n\ |
| 344 | \n\ |
| 345 | -h, --help Show this help text.\n\ |
| 346 | -v, --version Show version number.\n\ |
| 347 | -u, --usage Show brief usage message.\n\ |
| 348 | \n\ |
| 349 | -D, --daemon Become a background task after connecting.\n\ |
| 350 | -d, --directory=DIR Select current directory [default " CONFIGDIR "].\n\ |
| 351 | -U, --setuid=USER Set uid to USER after initialization.\n\ |
| 352 | -G, --setgid=GROUP Set gid to GROUP after initialization.\n\ |
| 353 | -a, --admin-socket=FILE Select socket to connect to\n\ |
| 354 | [default " SOCKETDIR "/tripesock].\n\ |
| 355 | -P, --pidfile=FILE Write process-id to FILE.\n\ |
| 356 | \n\ |
| 357 | -s, --spawn Start server rather than connecting.\n\ |
| 358 | -p, --spawn-path=PATH Specify path to executable.\n\ |
| 359 | -S, --spawn-args=ARGS Specify comma-separated arguments.\n\ |
| 360 | \n\ |
| 361 | -l, --syslog Log messages to system log.\n\ |
| 362 | -f, --logfile=FILE Log messages to FILE.\n\ |
| 363 | -t, --no-timestamp When logging to a file, don't emit timestamps.\n\ |
| 364 | -w, --warnings Show warnings when running commands.\n\ |
| 365 | -W, --watch=THINGS Watch for THINGS after connecting.\n\ |
| 366 | ", fp); |
| 367 | } |
| 368 | |
| 369 | int main(int argc, char *argv[]) |
| 370 | { |
| 371 | const char *dir = CONFIGDIR; |
| 372 | const char *sock = SOCKETDIR "/tripesock"; |
| 373 | const char *spawnpath = "tripe"; |
| 374 | string_v spawnopts = DA_INIT; |
| 375 | char *p; |
| 376 | FILE *pidfp = 0; |
| 377 | int i; |
| 378 | size_t sz; |
| 379 | uid_t u = -1; |
| 380 | gid_t g = -1; |
| 381 | int pfd[2], efd[2]; |
| 382 | mdup_fd md[3]; |
| 383 | pid_t kid; |
| 384 | struct sigaction sa; |
| 385 | sigset_t newmask, oldmask; |
| 386 | struct sockaddr_un sun; |
| 387 | selbuf bu, bs, be; |
| 388 | dstr d = DSTR_INIT; |
| 389 | sig hup; |
| 390 | |
| 391 | ego(argv[0]); |
| 392 | |
| 393 | if ((p = getenv("TRIPEDIR")) != 0) |
| 394 | dir = p; |
| 395 | if ((p = getenv("TRIPESOCK")) != 0) |
| 396 | sock = p; |
| 397 | |
| 398 | /* --- Parse the arguments --- */ |
| 399 | |
| 400 | for (;;) { |
| 401 | static const struct option opts[] = { |
| 402 | { "help", 0, 0, 'h' }, |
| 403 | { "version", 0, 0, 'v' }, |
| 404 | { "usage", 0, 0, 'u' }, |
| 405 | { "daemon", 0, 0, 'D' }, |
| 406 | { "uid", OPTF_ARGREQ, 0, 'U' }, |
| 407 | { "setuid", OPTF_ARGREQ, 0, 'U' }, |
| 408 | { "gid", OPTF_ARGREQ, 0, 'G' }, |
| 409 | { "setgid", OPTF_ARGREQ, 0, 'G' }, |
| 410 | { "directory", OPTF_ARGREQ, 0, 'd' }, |
| 411 | { "admin-socket", OPTF_ARGREQ, 0, 'a' }, |
| 412 | { "spawn", 0, 0, 's' }, |
| 413 | { "spawn-path", OPTF_ARGREQ, 0, 'p' }, |
| 414 | { "spawn-args", OPTF_ARGREQ, 0, 'S' }, |
| 415 | { "syslog", 0, 0, 'l' }, |
| 416 | { "logfile", OPTF_ARGREQ, 0, 'f' }, |
| 417 | { "no-timestamp", 0, 0, 't' }, |
| 418 | { "warnings", 0, 0, 'w' }, |
| 419 | { "watch", OPTF_ARGREQ, 0, 'W' }, |
| 420 | { "pidfile", OPTF_ARGREQ, 0, 'P' }, |
| 421 | { 0, 0, 0, 0 } |
| 422 | }; |
| 423 | |
| 424 | i = mdwopt(argc, argv, "+hvuDU:G:d:a:sp:S:lwW:f:nP:t", opts, 0, 0, 0); |
| 425 | if (i < 0) |
| 426 | break; |
| 427 | switch (i) { |
| 428 | case 'h': |
| 429 | help(stdout); |
| 430 | exit(0); |
| 431 | case 'v': |
| 432 | version(stdout); |
| 433 | exit(0); |
| 434 | case 'u': |
| 435 | usage(stdout); |
| 436 | exit(0); |
| 437 | case 'D': |
| 438 | f |= f_daemon | f_noinput; |
| 439 | break; |
| 440 | case 'U': |
| 441 | u = u_getuser(optarg, &g); |
| 442 | break; |
| 443 | case 'G': |
| 444 | g = u_getgroup(optarg); |
| 445 | break; |
| 446 | case 'd': |
| 447 | dir = optarg; |
| 448 | break; |
| 449 | case 'a': |
| 450 | sock = optarg; |
| 451 | break; |
| 452 | case 's': |
| 453 | f |= f_spawn; |
| 454 | break; |
| 455 | case 'p': |
| 456 | f |= f_spawn; |
| 457 | spawnpath = optarg; |
| 458 | break; |
| 459 | case 'S': |
| 460 | f |= f_spawn | f_spawnopts; |
| 461 | for (p = strtok(optarg, ","); p; p = strtok(0, ",")) |
| 462 | DA_PUSH(&spawnopts, p); |
| 463 | break; |
| 464 | case 'l': |
| 465 | f |= f_syslog | f_noinput; |
| 466 | break; |
| 467 | case 'w': |
| 468 | f |= f_warn; |
| 469 | break; |
| 470 | case 'W': |
| 471 | watch = optarg; |
| 472 | break; |
| 473 | case 'f': |
| 474 | logname = optarg; |
| 475 | f |= f_noinput; |
| 476 | break; |
| 477 | case 't': |
| 478 | f |= f_nostamp; |
| 479 | break; |
| 480 | case 'P': |
| 481 | pidfile = optarg; |
| 482 | break; |
| 483 | default: |
| 484 | f |= f_bogus; |
| 485 | break; |
| 486 | } |
| 487 | } |
| 488 | if ((f & f_bogus) || ((f & f_noinput) && optind < argc)) { |
| 489 | usage(stderr); |
| 490 | exit(EXIT_FAILURE); |
| 491 | } |
| 492 | |
| 493 | /* --- Set various things up --- */ |
| 494 | |
| 495 | if (chdir(dir)) { |
| 496 | die(EXIT_FAILURE, "couldn't set `%s' as current directory: %s", |
| 497 | dir, strerror(errno)); |
| 498 | } |
| 499 | if (!pidfile && (f & f_daemon) && ((f & f_syslog) || logname)) |
| 500 | pidfile = "tripectl.pid"; |
| 501 | if (pidfile && (pidfp = fopen(pidfile, "w")) == 0) { |
| 502 | die(EXIT_FAILURE, "couldn't open `%s' for writing: %s", |
| 503 | pidfile, strerror(errno)); |
| 504 | } |
| 505 | sel_init(&sel); |
| 506 | sig_init(&sel); |
| 507 | signal(SIGINT, sigdie); |
| 508 | signal(SIGQUIT, sigdie); |
| 509 | signal(SIGTERM, sigdie); |
| 510 | atexit(cleanup); |
| 511 | |
| 512 | /* --- Connect to the server --- */ |
| 513 | |
| 514 | if (f & f_spawn) { |
| 515 | sa.sa_handler = reap; |
| 516 | sigemptyset(&sa.sa_mask); |
| 517 | sa.sa_flags = SA_NOCLDSTOP; |
| 518 | #ifdef SA_RESTART |
| 519 | sa.sa_flags |= SA_RESTART; |
| 520 | #endif |
| 521 | sigaction(SIGCHLD, &sa, 0); |
| 522 | |
| 523 | DA_PUSH(&spawnopts, 0); |
| 524 | if (g != (gid_t)-1) putarg(&spawnopts, "-G%lu", (unsigned long)g); |
| 525 | if (u != (uid_t)-1) putarg(&spawnopts, "-U%lu", (unsigned long)u); |
| 526 | putarg(&spawnopts, "-a%s", sock); |
| 527 | putarg(&spawnopts, "-d."); |
| 528 | putarg(&spawnopts, "-F"); |
| 529 | putarg(&spawnopts, "%s", spawnpath); |
| 530 | if (socketpair(PF_UNIX, SOCK_STREAM, 0, pfd) || pipe(efd)) |
| 531 | die(EXIT_FAILURE, "error from socketpair: %s", strerror(errno)); |
| 532 | sigemptyset(&newmask); |
| 533 | sigaddset(&newmask, SIGCHLD); |
| 534 | sigprocmask(SIG_BLOCK, &newmask, &oldmask); |
| 535 | if ((kid = fork()) < 0) |
| 536 | die(EXIT_FAILURE, "fork failed: %s", strerror(errno)); |
| 537 | if (!kid) { |
| 538 | close(pfd[0]); close(efd[0]); |
| 539 | sigprocmask(SIG_SETMASK, &oldmask, 0); |
| 540 | md[0].cur = pfd[1]; md[0].want = STDIN_FILENO; |
| 541 | md[1].cur = pfd[1]; md[1].want = STDOUT_FILENO; |
| 542 | md[2].cur = efd[1]; md[2].want = STDERR_FILENO; |
| 543 | mdup(md, 3); |
| 544 | if (pidfp) fclose(pidfp); |
| 545 | closelog(); |
| 546 | if (f & f_daemon) detachtty(); |
| 547 | execvp(DA(&spawnopts)[0], DA(&spawnopts)); |
| 548 | die(127, "couldn't exec `%s': %s", spawnpath, strerror(errno)); |
| 549 | } |
| 550 | sigprocmask(SIG_SETMASK, &oldmask, 0); |
| 551 | fd = pfd[0]; |
| 552 | close(pfd[1]); close(efd[1]); |
| 553 | selbuf_init(&be, &sel, efd[0], eline, &be); |
| 554 | } else { |
| 555 | sz = strlen(sock) + 1; |
| 556 | if (sz > sizeof(sun.sun_path)) |
| 557 | die(EXIT_FAILURE, "socket name `%s' too long", sock); |
| 558 | memset(&sun, 0, sizeof(sun)); |
| 559 | sun.sun_family = AF_UNIX; |
| 560 | memcpy(sun.sun_path, sock, sz); |
| 561 | sz = sz + offsetof(struct sockaddr_un, sun_path); |
| 562 | if ((fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) |
| 563 | die(EXIT_FAILURE, "error making socket: %s", strerror(errno)); |
| 564 | if (connect(fd, (struct sockaddr *)&sun, sz)) { |
| 565 | die(EXIT_FAILURE, "error connecting to `%s': %s", |
| 566 | sun.sun_path, strerror(errno)); |
| 567 | } |
| 568 | } |
| 569 | |
| 570 | f |= f_losing; /* pessimism */ |
| 571 | u_setugid(u, g); |
| 572 | if (logname) |
| 573 | logfile(logname); |
| 574 | if (f & f_daemon) { |
| 575 | if (daemonize()) |
| 576 | die(EXIT_FAILURE, "error becoming daemon: %s", strerror(errno)); |
| 577 | } |
| 578 | if (pidfp) { |
| 579 | fprintf(pidfp, "%li\n", (long)getpid()); |
| 580 | fclose(pidfp); |
| 581 | } |
| 582 | signal(SIGPIPE, SIG_IGN); |
| 583 | |
| 584 | /* --- If we're meant to be interactive, do that --- */ |
| 585 | |
| 586 | if (optind == argc) { |
| 587 | DRESET(&d); |
| 588 | dstr_puts(&d, "watch"); |
| 589 | u_quotify(&d, watch ? watch : "-A+tw"); |
| 590 | setup(d.buf); |
| 591 | } |
| 592 | if (!(f & f_noinput) && optind == argc) { |
| 593 | selbuf_init(&bu, &sel, STDIN_FILENO, uline, &bu); |
| 594 | selbuf_init(&bs, &sel, fd, sline, &bs); |
| 595 | for (;;) { |
| 596 | if (sel_select(&sel) && errno != EINTR && errno != EAGAIN) |
| 597 | die(EXIT_FAILURE, "select failed: %s", strerror(errno)); |
| 598 | } |
| 599 | } |
| 600 | |
| 601 | /* --- If there's a command, submit it --- */ |
| 602 | |
| 603 | if (optind < argc) { |
| 604 | DRESET(&d); |
| 605 | dstr_puts(&d, "watch"); |
| 606 | u_quotify(&d, watch ? watch : (f & f_warn) ? "-A+w" : "-A"); |
| 607 | setup(d.buf); |
| 608 | DRESET(&d); |
| 609 | while (optind < argc) |
| 610 | u_quotify(&d, argv[optind++]); |
| 611 | dstr_putc(&d, '\n'); |
| 612 | errno = EIO; |
| 613 | if (write(fd, d.buf, d.len) != d.len || shutdown(fd, 1)) |
| 614 | die(EXIT_FAILURE, "write failed: %s", strerror(errno)); |
| 615 | dstr_destroy(&d); |
| 616 | f |= f_command; |
| 617 | } |
| 618 | |
| 619 | /* --- Pull everything else out of the box --- */ |
| 620 | |
| 621 | selbuf_init(&bs, &sel, fd, cline, 0); |
| 622 | |
| 623 | if (f & f_syslog) |
| 624 | openlog(QUIS, 0, LOG_DAEMON); |
| 625 | if (logfp) |
| 626 | sig_add(&hup, SIGHUP, sighup, 0); |
| 627 | for (;;) { |
| 628 | if (sel_select(&sel) && errno != EINTR && errno != EAGAIN) |
| 629 | die(EXIT_FAILURE, "select failed: %s", strerror(errno)); |
| 630 | } |
| 631 | |
| 632 | return (0); |
| 633 | } |
| 634 | |
| 635 | /*----- That's all, folks -------------------------------------------------*/ |