3 * $Id: client.c,v 1.5 2001/02/16 21:23:39 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.5 2001/02/16 21:23:39 mdw
33 * Use reliable signal handling for reopening logs.
35 * Revision 1.4 2001/02/06 09:34:53 mdw
36 * Change ERR response to FAIL for consistency with other programs.
38 * Revision 1.3 2001/02/04 17:10:15 mdw
39 * Reopen logfiles on receipt of @SIGHUP@ (not done very well). Don't
40 * change directory -- just mangle pathnames instead.
42 * Revision 1.2 2001/02/04 01:17:54 mdw
43 * Create a configuration header file to tidy up command lines.
45 * Revision 1.1 2001/02/03 20:26:37 mdw
50 /*----- Header files ------------------------------------------------------*/
62 #include <sys/types.h>
69 #include <sys/socket.h>
71 #include <arpa/inet.h>
74 #include <mLib/alloc.h>
75 #include <mLib/darray.h>
76 #include <mLib/dstr.h>
77 #include <mLib/mdwopt.h>
78 #include <mLib/quis.h>
79 #include <mLib/report.h>
81 #include <mLib/selbuf.h>
89 /*----- Data structures ---------------------------------------------------*/
93 DA_DECL(string_v
, char *);
96 /*----- Static variables --------------------------------------------------*/
98 static FILE *logfp
= 0;
99 static unsigned f
= 0;
100 static const char *logname
= 0;
106 #define f_spawnopts 8u
108 #define f_command 32u
109 #define f_noinput 64u
111 #define f_uclose 256u
113 /*----- Main code ---------------------------------------------------------*/
115 static void reap(int sig
)
119 while (waitpid(-1, &s
, WNOHANG
) > 0)
124 static void writelog(const char *cat
, const char *msg
)
128 struct tm
*tm
= localtime(&t
);
129 strftime(buf
, sizeof(buf
), "%Y-%m-%d %H:%M:%S", tm
);
130 fprintf(logfp
, "%s %s: %s\n", buf
, cat
, msg
);
133 static void cline(char *p
, void *b
)
138 die(EXIT_FAILURE
, "server dropped the connection");
144 if (strcmp(q
, "WARN") == 0) {
146 syslog(LOG_WARNING
, "%s", p
);
148 writelog("warning", p
);
150 fprintf(stderr
, "Warning: %s\n", p
);
151 } else if (strcmp(q
, "TRACE") == 0) {
153 syslog(LOG_DEBUG
, "%s", p
);
155 writelog("debug", p
);
156 } else if (!(f
& f_command
)) {
158 syslog(LOG_ERR
, "unexpected output `%s %s'", q
, p
);
161 dstr_putf(&d
, "unexpected output `%s %s'", q
, p
);
162 writelog("error", d
.buf
);
165 } else if (strcmp(q
, "FAIL") == 0)
166 die(EXIT_FAILURE
, "%s", p
);
167 else if (strcmp(q
, "INFO") == 0)
169 else if (strcmp(q
, "OK") == 0)
172 die(EXIT_FAILURE
, "unexpected output `%s %s'", q
, p
);
175 static void sline(char *p
, void *b
)
179 moan("server closed the connection");
185 static void uline(char *p
, void *b
)
195 write(fd
, p
, sz
+ 1);
199 static void logfile(const char *name
)
203 if ((logfp
= fopen(name
, "a")) == 0) {
204 die(EXIT_FAILURE
, "error opening logfile `%s': %s",
205 name
, strerror(errno
));
207 setvbuf(logfp
, 0, _IOLBF
, BUFSIZ
);
210 static void sighup(int sig
, void *v
)
215 static void version(FILE *fp
)
217 pquis(fp
, "$, TrIPE version " VERSION
"\n");
220 static void usage(FILE *fp
)
224 $ [-w] [-options] [command [args]...]\n\
225 $ [-Dl] [-f file] [-options]\n\
227 [-s] [-d directory] [-a socket] [-p program] [-S arg,arg,...]\n\
231 static void help(FILE *fp
)
240 -h, --help Show this help text.\n\
241 -v, --version Show version number.\n\
242 -u, --usage Show brief usage message.\n\
244 -D, --daemon Become a background task after connecting.\n\
245 -d, --directory=DIR Select current directory [default /var/lib/tripe]\n\
246 -a, --admin-socket=FILE Select socket to connect to.\n\
248 -s, --spawn Start server rather than connecting.\n\
249 -p, --spawn-path=PATH Specify path to executable.\n\
250 -S, --spawn-args=ARGS Specify comma-separated arguments.\n\
252 -l, --syslog Log messages to system log.\n\
253 -f, --logfile=FILE Log messages to FILE.\n\
254 -w, --warnings Show warnings when running commands.\n\
258 int main(int argc
, char *argv
[])
260 const char *dir
= "/var/lib/tripe";
261 const char *sock
= "tripesock";
262 const char *spawnpath
= "tripe";
263 string_v spawnopts
= DA_INIT
;
268 if ((p
= getenv("TRIPEDIR")) != 0)
271 /* --- Parse the arguments --- */
274 static const struct option opts
[] = {
275 { "help", 0, 0, 'h' },
276 { "version", 0, 0, 'v' },
277 { "usage", 0, 0, 'u' },
278 { "daemon", 0, 0, 'D' },
279 { "directory", OPTF_ARGREQ
, 0, 'd' },
280 { "admin-socket", OPTF_ARGREQ
, 0, 'a' },
281 { "spawn", 0, 0, 's' },
282 { "spawn-path", OPTF_ARGREQ
, 0, 'p' },
283 { "spawn-args", OPTF_ARGREQ
, 0, 'S' },
284 { "syslog", 0, 0, 'l' },
285 { "logfile", OPTF_ARGREQ
, 0, 'f' },
286 { "warnings", 0, 0, 'w' },
290 int i
= mdwopt(argc
, argv
, "hvuDd:a:sp:S:lwf:n", opts
, 0, 0, 0);
304 f
|= f_daemon
| f_noinput
;
320 f
|= f_spawn
| f_spawnopts
;
321 for (p
= strtok(optarg
, ","); p
; p
= strtok(0, ","))
322 DA_PUSH(&spawnopts
, p
);
325 f
|= f_syslog
| f_noinput
;
340 if ((f
& f_bogus
) || ((f
& f_noinput
) && optind
< argc
)) {
345 /* --- Connect to the server --- */
351 sigset_t newmask
, oldmask
;
353 sa
.sa_handler
= reap
;
354 sigemptyset(&sa
.sa_mask
);
355 sa
.sa_flags
= SA_NOCLDSTOP
;
357 sa
.sa_flags
|= SA_RESTART
;
359 sigaction(SIGCHLD
, &sa
, 0);
361 DA_UNSHIFT(&spawnopts
, (char *)spawnpath
);
362 if (!(f
& f_spawnopts
)) {
363 DA_PUSH(&spawnopts
, "-d");
364 DA_PUSH(&spawnopts
, (char *)dir
);
365 DA_PUSH(&spawnopts
, "-a");
366 DA_PUSH(&spawnopts
, (char *)sock
);
368 DA_PUSH(&spawnopts
, 0);
369 if (socketpair(PF_UNIX
, SOCK_STREAM
, 0, pfd
))
370 die(EXIT_FAILURE
, "error from socketpair: %s", strerror(errno
));
371 sigemptyset(&newmask
);
372 sigaddset(&newmask
, SIGCHLD
);
373 sigprocmask(SIG_BLOCK
, &newmask
, &oldmask
);
374 if ((kid
= fork()) < 0)
375 die(EXIT_FAILURE
, "fork failed: %s", strerror(errno
));
377 dup2(pfd
[1], STDIN_FILENO
);
378 dup2(pfd
[1], STDOUT_FILENO
);
384 execvp(DA(&spawnopts
)[0], DA(&spawnopts
));
385 die(127, "couldn't exec `%s': %s", spawnpath
, strerror(errno
));
387 sigprocmask(SIG_SETMASK
, &oldmask
, 0);
391 struct sockaddr_un sun
;
392 size_t sz
= strlen(sock
) + 1;
394 dstr_putf(&d
, "%s/%s", dir
, sock
);
395 if (d
.sz
+ 1 > sizeof(sun
.sun_path
))
396 die(EXIT_FAILURE
, "socket name `%s' too long", sock
);
397 memset(&sun
, 0, sizeof(sun
));
398 sun
.sun_family
= AF_UNIX
;
399 memcpy(sun
.sun_path
, d
.buf
, d
.sz
+ 1);
400 sz
= d
.sz
+ offsetof(struct sockaddr_un
, sun_path
) + 1;
402 if ((fd
= socket(PF_UNIX
, SOCK_STREAM
, 0)) < 0)
403 die(EXIT_FAILURE
, "error making socket: %s", strerror(errno
));
404 if (connect(fd
, (struct sockaddr
*)&sun
, sz
)) {
405 die(EXIT_FAILURE
, "error connecting to `%s': %s",
406 sun
.sun_path
, strerror(errno
));
412 die(EXIT_FAILURE
, "error becoming daemon: %s", strerror(errno
));
415 /* --- If we're meant to be interactive, do that --- */
417 if (!(f
& f_noinput
) && optind
== argc
) {
422 selbuf_init(&bu
, &sel
, STDIN_FILENO
, uline
, &bu
);
423 selbuf_init(&bs
, &sel
, fd
, sline
, &bs
);
425 if (sel_select(&sel
) && errno
!= EINTR
&& errno
!= EAGAIN
)
426 die(EXIT_FAILURE
, "select failed: %s", strerror(errno
));
430 /* --- If there's a command, submit it --- */
434 dstr_puts(&d
, argv
[optind
++]);
435 while (optind
< argc
) {
437 dstr_puts(&d
, argv
[optind
++]);
440 write(fd
, d
.buf
, d
.len
);
446 /* --- Pull everything else out of the box --- */
454 selbuf_init(&b
, &sel
, fd
, cline
, 0);
457 openlog(QUIS
, 0, LOG_DAEMON
);
460 sig_add(&hup
, SIGHUP
, sighup
, 0);
463 if (sel_select(&sel
) && errno
!= EINTR
&& errno
!= EAGAIN
)
464 die(EXIT_FAILURE
, "select failed: %s", strerror(errno
));
471 /*----- That's all, folks -------------------------------------------------*/