3 * $Id: client.c,v 1.2 2001/02/04 01:17: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.2 2001/02/04 01:17:54 mdw
33 * Create a configuration header file to tidy up command lines.
35 * Revision 1.1 2001/02/03 20:26:37 mdw
40 /*----- Header files ------------------------------------------------------*/
52 #include <sys/types.h>
59 #include <sys/socket.h>
61 #include <arpa/inet.h>
64 #include <mLib/alloc.h>
65 #include <mLib/darray.h>
66 #include <mLib/dstr.h>
67 #include <mLib/lbuf.h>
68 #include <mLib/mdwopt.h>
69 #include <mLib/quis.h>
70 #include <mLib/report.h>
72 #include <mLib/selbuf.h>
79 /*----- Data structures ---------------------------------------------------*/
83 DA_DECL(string_v
, char *);
86 /*----- Static variables --------------------------------------------------*/
88 static FILE *logfp
= 0;
89 static unsigned f
= 0;
95 #define f_spawnopts 8u
100 #define f_uclose 256u
102 /*----- Main code ---------------------------------------------------------*/
104 static void reap(int sig
)
108 while (waitpid(-1, &s
, WNOHANG
) > 0)
113 static void writelog(const char *cat
, const char *msg
)
117 struct tm
*tm
= localtime(&t
);
118 strftime(buf
, sizeof(buf
), "%Y-%m-%d %H:%M:%S", tm
);
119 fprintf(logfp
, "%s %s: %s\n", buf
, cat
, msg
);
122 static void cline(char *p
, void *b
)
127 die(EXIT_FAILURE
, "server dropped the connection");
133 if (strcmp(q
, "WARN") == 0) {
135 syslog(LOG_WARNING
, "%s", p
);
137 writelog("warning", p
);
139 fprintf(stderr
, "Warning: %s\n", p
);
140 } else if (strcmp(q
, "TRACE") == 0) {
142 syslog(LOG_DEBUG
, "%s", p
);
144 writelog("debug", p
);
145 } else if (!(f
& f_command
)) {
147 syslog(LOG_ERR
, "unexpected output `%s %s'", q
, p
);
150 dstr_putf(&d
, "unexpected output `%s %s'", q
, p
);
151 writelog("error", d
.buf
);
154 } else if (strcmp(q
, "ERR") == 0)
155 die(EXIT_FAILURE
, "%s", p
);
156 else if (strcmp(q
, "INFO") == 0)
158 else if (strcmp(q
, "OK") == 0)
161 die(EXIT_FAILURE
, "unexpected output `%s %s'", q
, p
);
164 static void sline(char *p
, void *b
)
168 moan("server closed the connection");
174 static void uline(char *p
, void *b
)
184 write(fd
, p
, sz
+ 1);
188 static void version(FILE *fp
)
190 pquis(fp
, "$, TrIPE version " VERSION
"\n");
193 static void usage(FILE *fp
)
197 $ [-w] [-options] [command [args]...]\n\
198 $ [-Dl] [-f file] [-options]\n\
200 [-s] [-d directory] [-a socket] [-p program] [-S arg,arg,...]\n\
204 static void help(FILE *fp
)
213 -h, --help Show this help text.\n\
214 -v, --version Show version number.\n\
215 -u, --usage Show brief usage message.\n\
217 -D, --daemon Become a background task after connecting.\n\
218 -d, --directory=DIR Select current directory [default /var/lib/tripe]\n\
219 -a, --admin-socket=FILE Select socket to connect to.\n\
221 -s, --spawn Start server rather than connecting.\n\
222 -p, --spawn-path=PATH Specify path to executable.\n\
223 -S, --spawn-args=ARGS Specify comma-separated arguments.\n\
225 -l, --syslog Log messages to system log.\n\
226 -f, --logfile=FILE Log messages to FILE.\n\
227 -w, --warnings Show warnings when running commands.\n\
231 int main(int argc
, char *argv
[])
233 const char *dir
= "/var/lib/tripe";
234 const char *sock
= "tripesock";
235 const char *spawnpath
= "tripe";
236 string_v spawnopts
= DA_INIT
;
241 if ((p
= getenv("TRIPEDIR")) != 0)
244 /* --- Parse the arguments --- */
247 static const struct option opts
[] = {
248 { "help", 0, 0, 'h' },
249 { "version", 0, 0, 'v' },
250 { "usage", 0, 0, 'u' },
251 { "daemon", 0, 0, 'D' },
252 { "directory", OPTF_ARGREQ
, 0, 'd' },
253 { "admin-socket", OPTF_ARGREQ
, 0, 'a' },
254 { "spawn", 0, 0, 's' },
255 { "spawn-path", OPTF_ARGREQ
, 0, 'p' },
256 { "spawn-args", OPTF_ARGREQ
, 0, 'S' },
257 { "syslog", 0, 0, 'l' },
258 { "logfile", OPTF_ARGREQ
, 0, 'f' },
259 { "warnings", 0, 0, 'w' },
263 int i
= mdwopt(argc
, argv
, "hvuDd:a:sp:S:lwf:n", opts
, 0, 0, 0);
277 f
|= f_daemon
| f_noinput
;
293 f
|= f_spawn
| f_spawnopts
;
294 for (p
= strtok(optarg
, ","); p
; p
= strtok(0, ","))
295 DA_PUSH(&spawnopts
, p
);
298 f
|= f_syslog
| f_noinput
;
306 if ((logfp
= fopen(optarg
, "a")) == 0) {
307 die(EXIT_FAILURE
, "error opening logfile `%s': %s",
308 optarg
, strerror(errno
));
317 if ((f
& f_bogus
) || ((f
& f_noinput
) && optind
< argc
)) {
322 /* --- Set the world up --- */
325 die(EXIT_FAILURE
, "couldn't set current directory to `%s': %s",
326 dir
, strerror(errno
));
329 /* --- Connect to the server --- */
335 sigset_t newmask
, oldmask
;
337 sa
.sa_handler
= reap
;
338 sigemptyset(&sa
.sa_mask
);
339 sa
.sa_flags
= SA_NOCLDSTOP
;
341 sa
.sa_flags
|= SA_RESTART
;
343 sigaction(SIGCHLD
, &sa
, 0);
345 DA_UNSHIFT(&spawnopts
, (char *)spawnpath
);
346 if (!(f
& f_spawnopts
)) {
347 DA_PUSH(&spawnopts
, "-d.");
348 DA_PUSH(&spawnopts
, "-a");
349 DA_PUSH(&spawnopts
, (char *)sock
);
351 DA_PUSH(&spawnopts
, 0);
352 if (socketpair(PF_UNIX
, SOCK_STREAM
, 0, pfd
))
353 die(EXIT_FAILURE
, "error from socketpair: %s", strerror(errno
));
354 sigemptyset(&newmask
);
355 sigaddset(&newmask
, SIGCHLD
);
356 sigprocmask(SIG_BLOCK
, &newmask
, &oldmask
);
357 if ((kid
= fork()) < 0)
358 die(EXIT_FAILURE
, "fork failed: %s", strerror(errno
));
360 dup2(pfd
[1], STDIN_FILENO
);
361 dup2(pfd
[1], STDOUT_FILENO
);
364 execvp(DA(&spawnopts
)[0], DA(&spawnopts
));
365 die(127, "couldn't exec `%s': %s", spawnpath
, strerror(errno
));
367 sigprocmask(SIG_SETMASK
, &oldmask
, 0);
371 struct sockaddr_un sun
;
372 size_t sz
= strlen(sock
) + 1;
373 if (sz
> sizeof(sun
.sun_path
))
374 die(EXIT_FAILURE
, "socket name `%s' too long", sock
);
375 memset(&sun
, 0, sizeof(sun
));
376 sun
.sun_family
= AF_UNIX
;
377 memcpy(sun
.sun_path
, sock
, sz
);
378 sz
+= offsetof(struct sockaddr_un
, sun_path
);
379 if ((fd
= socket(PF_UNIX
, SOCK_STREAM
, 0)) < 0)
380 die(EXIT_FAILURE
, "error making socket: %s", strerror(errno
));
381 if (connect(fd
, (struct sockaddr
*)&sun
, sz
)) {
382 die(EXIT_FAILURE
, "error connecting to `%s': %s",
383 sock
, strerror(errno
));
389 die(EXIT_FAILURE
, "error becoming daemon: %s", strerror(errno
));
392 /* --- If we're meant to be interactive, do that --- */
394 if (!(f
& f_noinput
) && optind
== argc
) {
399 selbuf_init(&bu
, &sel
, STDIN_FILENO
, uline
, &bu
);
400 selbuf_init(&bs
, &sel
, fd
, sline
, &bs
);
402 if (sel_select(&sel
))
403 die(EXIT_FAILURE
, "select failed: %s", strerror(errno
));
407 /* --- If there's a command, submit it --- */
411 dstr_puts(&d
, argv
[optind
++]);
412 while (optind
< argc
) {
414 dstr_puts(&d
, argv
[optind
++]);
417 write(fd
, d
.buf
, d
.len
);
423 /* --- Pull everything else out of the box --- */
427 lbuf_init(&b
, cline
, 0);
429 openlog(QUIS
, 0, LOG_DAEMON
);
431 size_t sz
= lbuf_free(&b
, &p
);
432 ssize_t n
= read(fd
, p
, sz
);
434 die(EXIT_FAILURE
, "read failed: %s", strerror(errno
));
437 lbuf_flush(&b
, p
, n
);
445 /*----- That's all, folks -------------------------------------------------*/