3 * $Id: client.c,v 1.1 2001/02/03 20:26:37 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.1 2001/02/03 20:26:37 mdw
37 /*----- Header files ------------------------------------------------------*/
47 #include <sys/types.h>
54 #include <sys/socket.h>
56 #include <arpa/inet.h>
59 #include <mLib/alloc.h>
60 #include <mLib/darray.h>
61 #include <mLib/dstr.h>
62 #include <mLib/lbuf.h>
63 #include <mLib/mdwopt.h>
64 #include <mLib/quis.h>
65 #include <mLib/report.h>
67 #include <mLib/selbuf.h>
74 /*----- Data structures ---------------------------------------------------*/
78 DA_DECL(string_v
, char *);
81 /*----- Static variables --------------------------------------------------*/
83 static FILE *logfp
= 0;
84 static unsigned f
= 0;
90 #define f_spawnopts 8u
97 /*----- Main code ---------------------------------------------------------*/
99 static void reap(int sig
)
103 while (waitpid(-1, &s
, WNOHANG
) > 0)
108 static void writelog(const char *cat
, const char *msg
)
112 struct tm
*tm
= localtime(&t
);
113 strftime(buf
, sizeof(buf
), "%Y-%m-%d %H:%M:%S", tm
);
114 fprintf(logfp
, "%s %s: %s\n", buf
, cat
, msg
);
117 static void cline(char *p
, void *b
)
122 die(EXIT_FAILURE
, "server dropped the connection");
128 if (strcmp(q
, "WARN") == 0) {
130 syslog(LOG_WARNING
, "%s", p
);
132 writelog("warning", p
);
134 fprintf(stderr
, "Warning: %s\n", p
);
135 } else if (strcmp(q
, "TRACE") == 0) {
137 syslog(LOG_DEBUG
, "%s", p
);
139 writelog("debug", p
);
140 } else if (!(f
& f_command
)) {
142 syslog(LOG_ERR
, "unexpected output `%s %s'", q
, p
);
145 dstr_putf(&d
, "unexpected output `%s %s'", q
, p
);
146 writelog("error", d
.buf
);
149 } else if (strcmp(q
, "ERR") == 0)
150 die(EXIT_FAILURE
, "%s", p
);
151 else if (strcmp(q
, "INFO") == 0)
153 else if (strcmp(q
, "OK") == 0)
156 die(EXIT_FAILURE
, "unexpected output `%s %s'", q
, p
);
159 static void sline(char *p
, void *b
)
163 moan("server closed the connection");
169 static void uline(char *p
, void *b
)
179 write(fd
, p
, sz
+ 1);
183 static void version(FILE *fp
)
185 pquis(fp
, "$, TrIPE version " VERSION
"\n");
188 static void usage(FILE *fp
)
192 $ [-w] [-options] [command [args]...]\n\
193 $ [-Dl] [-f file] [-options]\n\
195 [-s] [-d directory] [-a socket] [-p program] [-S arg,arg,...]\n\
199 static void help(FILE *fp
)
208 -h, --help Show this help text.\n\
209 -v, --version Show version number.\n\
210 -u, --usage Show brief usage message.\n\
212 -D, --daemon Become a background task after connecting.\n\
213 -d, --directory=DIR Select current directory [default /var/lib/tripe]\n\
214 -a, --admin-socket=FILE Select socket to connect to.\n\
216 -s, --spawn Start server rather than connecting.\n\
217 -p, --spawn-path=PATH Specify path to executable.\n\
218 -S, --spawn-args=ARGS Specify comma-separated arguments.\n\
220 -l, --syslog Log messages to system log.\n\
221 -f, --logfile=FILE Log messages to FILE.\n\
222 -w, --warnings Show warnings when running commands.\n\
226 int main(int argc
, char *argv
[])
228 const char *dir
= "/var/lib/tripe";
229 const char *sock
= "tripesock";
230 const char *spawnpath
= "tripe";
231 string_v spawnopts
= DA_INIT
;
236 if ((p
= getenv("TRIPEDIR")) != 0)
239 /* --- Parse the arguments --- */
242 static const struct option opts
[] = {
243 { "help", 0, 0, 'h' },
244 { "version", 0, 0, 'v' },
245 { "usage", 0, 0, 'u' },
246 { "daemon", 0, 0, 'D' },
247 { "directory", OPTF_ARGREQ
, 0, 'd' },
248 { "admin-socket", OPTF_ARGREQ
, 0, 'a' },
249 { "spawn", 0, 0, 's' },
250 { "spawn-path", OPTF_ARGREQ
, 0, 'p' },
251 { "spawn-args", OPTF_ARGREQ
, 0, 'S' },
252 { "syslog", 0, 0, 'l' },
253 { "logfile", OPTF_ARGREQ
, 0, 'f' },
254 { "warnings", 0, 0, 'w' },
258 int i
= mdwopt(argc
, argv
, "hvuDd:a:sp:S:lwf:n", opts
, 0, 0, 0);
272 f
|= f_daemon
| f_noinput
;
288 f
|= f_spawn
| f_spawnopts
;
289 for (p
= strtok(optarg
, ","); p
; p
= strtok(0, ","))
290 DA_PUSH(&spawnopts
, p
);
293 f
|= f_syslog
| f_noinput
;
301 if ((logfp
= fopen(optarg
, "a")) == 0) {
302 die(EXIT_FAILURE
, "error opening logfile `%s': %s",
303 optarg
, strerror(errno
));
312 if ((f
& f_bogus
) || ((f
& f_noinput
) && optind
< argc
)) {
317 /* --- Set the world up --- */
320 die(EXIT_FAILURE
, "couldn't set current directory to `%s': %s",
321 dir
, strerror(errno
));
324 /* --- Connect to the server --- */
330 sigset_t newmask
, oldmask
;
332 sa
.sa_handler
= reap
;
333 sigemptyset(&sa
.sa_mask
);
334 sa
.sa_flags
= SA_NOCLDSTOP
;
336 sa
.sa_flags
|= SA_RESTART
;
338 sigaction(SIGCHLD
, &sa
, 0);
340 DA_UNSHIFT(&spawnopts
, (char *)spawnpath
);
341 if (!(f
& f_spawnopts
)) {
342 DA_PUSH(&spawnopts
, "-d.");
343 DA_PUSH(&spawnopts
, "-a");
344 DA_PUSH(&spawnopts
, (char *)sock
);
346 DA_PUSH(&spawnopts
, 0);
347 if (socketpair(PF_UNIX
, SOCK_STREAM
, 0, pfd
))
348 die(EXIT_FAILURE
, "error from socketpair: %s", strerror(errno
));
349 sigemptyset(&newmask
);
350 sigaddset(&newmask
, SIGCHLD
);
351 sigprocmask(SIG_BLOCK
, &newmask
, &oldmask
);
352 if ((kid
= fork()) < 0)
353 die(EXIT_FAILURE
, "fork failed: %s", strerror(errno
));
355 dup2(pfd
[1], STDIN_FILENO
);
356 dup2(pfd
[1], STDOUT_FILENO
);
359 execvp(DA(&spawnopts
)[0], DA(&spawnopts
));
360 die(127, "couldn't exec `%s': %s", spawnpath
, strerror(errno
));
362 sigprocmask(SIG_SETMASK
, &oldmask
, 0);
366 struct sockaddr_un sun
;
367 size_t sz
= strlen(sock
) + 1;
368 if (sz
> sizeof(sun
.sun_path
))
369 die(EXIT_FAILURE
, "socket name `%s' too long", sock
);
370 memset(&sun
, 0, sizeof(sun
));
371 sun
.sun_family
= AF_UNIX
;
372 memcpy(sun
.sun_path
, sock
, sz
);
373 sz
+= offsetof(struct sockaddr_un
, sun_path
);
374 if ((fd
= socket(PF_UNIX
, SOCK_STREAM
, 0)) < 0)
375 die(EXIT_FAILURE
, "error making socket: %s", strerror(errno
));
376 if (connect(fd
, (struct sockaddr
*)&sun
, sz
)) {
377 die(EXIT_FAILURE
, "error connecting to `%s': %s",
378 sock
, strerror(errno
));
384 die(EXIT_FAILURE
, "error becoming daemon: %s", strerror(errno
));
387 /* --- If we're meant to be interactive, do that --- */
389 if (!(f
& f_noinput
) && optind
== argc
) {
394 selbuf_init(&bu
, &sel
, STDIN_FILENO
, uline
, &bu
);
395 selbuf_init(&bs
, &sel
, fd
, sline
, &bs
);
397 if (sel_select(&sel
))
398 die(EXIT_FAILURE
, "select failed: %s", strerror(errno
));
402 /* --- If there's a command, submit it --- */
406 dstr_puts(&d
, argv
[optind
++]);
407 while (optind
< argc
) {
409 dstr_puts(&d
, argv
[optind
++]);
412 write(fd
, d
.buf
, d
.len
);
418 /* --- Pull everything else out of the box --- */
422 lbuf_init(&b
, cline
, 0);
424 openlog(QUIS
, 0, LOG_DAEMON
);
426 size_t sz
= lbuf_free(&b
, &p
);
427 ssize_t n
= read(fd
, p
, sz
);
429 die(EXIT_FAILURE
, "read failed: %s", strerror(errno
));
432 lbuf_flush(&b
, p
, n
);
440 /*----- That's all, folks -------------------------------------------------*/