3 * $Id: client.c,v 1.7 2001/02/22 09:07:54 mdw Exp $
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 /*----- Revision history --------------------------------------------------*
32 * Revision 1.7 2001/02/22 09:07:54 mdw
33 * Write a pidfile on request, and delete it when finished.
35 * Revision 1.6 2001/02/22 09:06:08 mdw
36 * Fix logfile rotation to avoid program collapse.
38 * Revision 1.5 2001/02/16 21:23:39 mdw
39 * Use reliable signal handling for reopening logs.
41 * Revision 1.4 2001/02/06 09:34:53 mdw
42 * Change ERR response to FAIL for consistency with other programs.
44 * Revision 1.3 2001/02/04 17:10:15 mdw
45 * Reopen logfiles on receipt of @SIGHUP@ (not done very well). Don't
46 * change directory -- just mangle pathnames instead.
48 * Revision 1.2 2001/02/04 01:17:54 mdw
49 * Create a configuration header file to tidy up command lines.
51 * Revision 1.1 2001/02/03 20:26:37 mdw
56 /*----- Header files ------------------------------------------------------*/
68 #include <sys/types.h>
75 #include <sys/socket.h>
77 #include <arpa/inet.h>
80 #include <mLib/alloc.h>
81 #include <mLib/darray.h>
82 #include <mLib/dstr.h>
83 #include <mLib/mdwopt.h>
84 #include <mLib/quis.h>
85 #include <mLib/report.h>
87 #include <mLib/selbuf.h>
95 /*----- Data structures ---------------------------------------------------*/
99 DA_DECL(string_v
, char *);
102 /*----- Static variables --------------------------------------------------*/
104 static const char *pidfile
= 0;
105 static const char *logname
= 0;
106 static FILE *logfp
= 0;
107 static unsigned f
= 0;
113 #define f_spawnopts 8u
115 #define f_command 32u
116 #define f_noinput 64u
118 #define f_uclose 256u
120 /*----- Main code ---------------------------------------------------------*/
122 static void reap(int sig
)
125 while (waitpid(-1, 0, WNOHANG
) > 0)
130 static void writelog(const char *cat
, const char *msg
)
134 struct tm
*tm
= localtime(&t
);
135 strftime(buf
, sizeof(buf
), "%Y-%m-%d %H:%M:%S", tm
);
136 fprintf(logfp
, "%s %s: %s\n", buf
, cat
, msg
);
139 static void cline(char *p
, void *b
)
144 die(EXIT_FAILURE
, "server dropped the connection");
150 if (strcmp(q
, "WARN") == 0) {
152 syslog(LOG_WARNING
, "%s", p
);
154 writelog("warning", p
);
156 fprintf(stderr
, "Warning: %s\n", p
);
157 } else if (strcmp(q
, "TRACE") == 0) {
159 syslog(LOG_DEBUG
, "%s", p
);
161 writelog("debug", p
);
162 } else if (!(f
& f_command
)) {
164 syslog(LOG_ERR
, "unexpected output `%s %s'", q
, p
);
167 dstr_putf(&d
, "unexpected output `%s %s'", q
, p
);
168 writelog("error", d
.buf
);
171 } else if (strcmp(q
, "FAIL") == 0)
172 die(EXIT_FAILURE
, "%s", p
);
173 else if (strcmp(q
, "INFO") == 0)
175 else if (strcmp(q
, "OK") == 0)
178 die(EXIT_FAILURE
, "unexpected output `%s %s'", q
, p
);
181 static void sline(char *p
, void *b
)
185 moan("server closed the connection");
191 static void uline(char *p
, void *b
)
201 write(fd
, p
, sz
+ 1);
205 static void logfile(const char *name
)
209 if ((fp
= fopen(name
, "a")) != 0) {
213 setvbuf(logfp
, 0, _IOLBF
, BUFSIZ
);
216 dstr_putf(&d
, "error opening logfile `%s': %s", name
, strerror(errno
));
218 writelog("error", d
.buf
);
220 die(EXIT_FAILURE
, d
.buf
);
222 syslog(LOG_ERR
, d
.buf
);
227 static void sighup(int sig
, void *v
)
232 static void cleanup(void)
238 static void sigdie(int sig
)
241 signal(sig
, SIG_DFL
);
245 static void version(FILE *fp
)
247 pquis(fp
, "$, TrIPE version " VERSION
"\n");
250 static void usage(FILE *fp
)
254 $ [-w] [-options] [command [args]...]\n\
255 $ [-Dl] [-f file] [-options]\n\
257 [-s] [-d directory] [-a socket] [-P pidfile]\n\
258 [-p program] [-S arg,arg,...]\n\
262 static void help(FILE *fp
)
271 -h, --help Show this help text.\n\
272 -v, --version Show version number.\n\
273 -u, --usage Show brief usage message.\n\
275 -D, --daemon Become a background task after connecting.\n\
276 -d, --directory=DIR Select current directory [default /var/lib/tripe]\n\
277 -a, --admin-socket=FILE Select socket to connect to.\n\
278 -P, --pidfile=FILE Write process-id to FILE.\n\
280 -s, --spawn Start server rather than connecting.\n\
281 -p, --spawn-path=PATH Specify path to executable.\n\
282 -S, --spawn-args=ARGS Specify comma-separated arguments.\n\
284 -l, --syslog Log messages to system log.\n\
285 -f, --logfile=FILE Log messages to FILE.\n\
286 -w, --warnings Show warnings when running commands.\n\
290 int main(int argc
, char *argv
[])
292 const char *dir
= "/var/lib/tripe";
293 const char *sock
= "tripesock";
294 const char *spawnpath
= "tripe";
295 string_v spawnopts
= DA_INIT
;
301 if ((p
= getenv("TRIPEDIR")) != 0)
304 /* --- Parse the arguments --- */
307 static const struct option opts
[] = {
308 { "help", 0, 0, 'h' },
309 { "version", 0, 0, 'v' },
310 { "usage", 0, 0, 'u' },
311 { "daemon", 0, 0, 'D' },
312 { "directory", OPTF_ARGREQ
, 0, 'd' },
313 { "admin-socket", OPTF_ARGREQ
, 0, 'a' },
314 { "spawn", 0, 0, 's' },
315 { "spawn-path", OPTF_ARGREQ
, 0, 'p' },
316 { "spawn-args", OPTF_ARGREQ
, 0, 'S' },
317 { "syslog", 0, 0, 'l' },
318 { "logfile", OPTF_ARGREQ
, 0, 'f' },
319 { "warnings", 0, 0, 'w' },
320 { "pidfile", OPTF_ARGREQ
, 0, 'P' },
324 int i
= mdwopt(argc
, argv
, "+hvuDd:a:sp:S:lwf:nP:", opts
, 0, 0, 0);
338 f
|= f_daemon
| f_noinput
;
354 f
|= f_spawn
| f_spawnopts
;
355 for (p
= strtok(optarg
, ","); p
; p
= strtok(0, ","))
356 DA_PUSH(&spawnopts
, p
);
359 f
|= f_syslog
| f_noinput
;
376 if ((f
& f_bogus
) || ((f
& f_noinput
) && optind
< argc
)) {
381 /* --- Set various things up --- */
384 die(EXIT_FAILURE
, "couldn't set `%s' as current directory: %s",
385 dir
, strerror(errno
));
389 if (!pidfile
&& (f
& f_daemon
) && ((f
& f_syslog
) || logname
))
390 pidfile
= "tripectl.pid";
391 if (pidfile
&& (pidfp
= fopen(pidfile
, "w")) == 0) {
392 die(EXIT_FAILURE
, "couldn't open `%s' for writing: %s",
393 pidfile
, strerror(errno
));
395 signal(SIGINT
, sigdie
);
396 signal(SIGQUIT
, sigdie
);
397 signal(SIGTERM
, sigdie
);
400 /* --- Connect to the server --- */
406 sigset_t newmask
, oldmask
;
408 sa
.sa_handler
= reap
;
409 sigemptyset(&sa
.sa_mask
);
410 sa
.sa_flags
= SA_NOCLDSTOP
;
412 sa
.sa_flags
|= SA_RESTART
;
414 sigaction(SIGCHLD
, &sa
, 0);
416 DA_UNSHIFT(&spawnopts
, (char *)spawnpath
);
417 DA_UNSHIFT(&spawnopts
, (char *)sock
);
418 DA_UNSHIFT(&spawnopts
, "-a");
419 DA_UNSHIFT(&spawnopts
, "-d.");
420 DA_PUSH(&spawnopts
, 0);
421 if (socketpair(PF_UNIX
, SOCK_STREAM
, 0, pfd
))
422 die(EXIT_FAILURE
, "error from socketpair: %s", strerror(errno
));
423 sigemptyset(&newmask
);
424 sigaddset(&newmask
, SIGCHLD
);
425 sigprocmask(SIG_BLOCK
, &newmask
, &oldmask
);
426 if ((kid
= fork()) < 0)
427 die(EXIT_FAILURE
, "fork failed: %s", strerror(errno
));
429 dup2(pfd
[1], STDIN_FILENO
);
430 dup2(pfd
[1], STDOUT_FILENO
);
438 execvp(DA(&spawnopts
)[0], DA(&spawnopts
));
439 die(127, "couldn't exec `%s': %s", spawnpath
, strerror(errno
));
441 sigprocmask(SIG_SETMASK
, &oldmask
, 0);
445 struct sockaddr_un sun
;
446 size_t sz
= strlen(sock
) + 1;
447 if (sz
> sizeof(sun
.sun_path
))
448 die(EXIT_FAILURE
, "socket name `%s' too long", sock
);
449 memset(&sun
, 0, sizeof(sun
));
450 sun
.sun_family
= AF_UNIX
;
451 memcpy(sun
.sun_path
, sock
, sz
);
452 sz
= sz
+ offsetof(struct sockaddr_un
, sun_path
);
453 if ((fd
= socket(PF_UNIX
, SOCK_STREAM
, 0)) < 0)
454 die(EXIT_FAILURE
, "error making socket: %s", strerror(errno
));
455 if (connect(fd
, (struct sockaddr
*)&sun
, sz
)) {
456 die(EXIT_FAILURE
, "error connecting to `%s': %s",
457 sun
.sun_path
, strerror(errno
));
463 die(EXIT_FAILURE
, "error becoming daemon: %s", strerror(errno
));
466 fprintf(pidfp
, "%li", (long)getpid());
470 /* --- If we're meant to be interactive, do that --- */
472 if (!(f
& f_noinput
) && optind
== argc
) {
477 selbuf_init(&bu
, &sel
, STDIN_FILENO
, uline
, &bu
);
478 selbuf_init(&bs
, &sel
, fd
, sline
, &bs
);
480 if (sel_select(&sel
) && errno
!= EINTR
&& errno
!= EAGAIN
)
481 die(EXIT_FAILURE
, "select failed: %s", strerror(errno
));
485 /* --- If there's a command, submit it --- */
489 dstr_puts(&d
, argv
[optind
++]);
490 while (optind
< argc
) {
492 dstr_puts(&d
, argv
[optind
++]);
495 write(fd
, d
.buf
, d
.len
);
501 /* --- Pull everything else out of the box --- */
509 selbuf_init(&b
, &sel
, fd
, cline
, 0);
512 openlog(QUIS
, 0, LOG_DAEMON
);
515 sig_add(&hup
, SIGHUP
, sighup
, 0);
518 if (sel_select(&sel
) && errno
!= EINTR
&& errno
!= EAGAIN
)
519 die(EXIT_FAILURE
, "select failed: %s", strerror(errno
));
526 /*----- That's all, folks -------------------------------------------------*/