3 * $Id: client.c,v 1.8 2001/06/19 22:09: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.8 2001/06/19 22:09:37 mdw
33 * Move the program name to the right place when constructing the arguments
34 * to pass to a new server.
36 * Revision 1.7 2001/02/22 09:07:54 mdw
37 * Write a pidfile on request, and delete it when finished.
39 * Revision 1.6 2001/02/22 09:06:08 mdw
40 * Fix logfile rotation to avoid program collapse.
42 * Revision 1.5 2001/02/16 21:23:39 mdw
43 * Use reliable signal handling for reopening logs.
45 * Revision 1.4 2001/02/06 09:34:53 mdw
46 * Change ERR response to FAIL for consistency with other programs.
48 * Revision 1.3 2001/02/04 17:10:15 mdw
49 * Reopen logfiles on receipt of @SIGHUP@ (not done very well). Don't
50 * change directory -- just mangle pathnames instead.
52 * Revision 1.2 2001/02/04 01:17:54 mdw
53 * Create a configuration header file to tidy up command lines.
55 * Revision 1.1 2001/02/03 20:26:37 mdw
60 /*----- Header files ------------------------------------------------------*/
72 #include <sys/types.h>
79 #include <sys/socket.h>
81 #include <arpa/inet.h>
84 #include <mLib/alloc.h>
85 #include <mLib/darray.h>
86 #include <mLib/dstr.h>
87 #include <mLib/mdwopt.h>
88 #include <mLib/quis.h>
89 #include <mLib/report.h>
91 #include <mLib/selbuf.h>
99 /*----- Data structures ---------------------------------------------------*/
103 DA_DECL(string_v
, char *);
106 /*----- Static variables --------------------------------------------------*/
108 static const char *pidfile
= 0;
109 static const char *logname
= 0;
110 static FILE *logfp
= 0;
111 static unsigned f
= 0;
117 #define f_spawnopts 8u
119 #define f_command 32u
120 #define f_noinput 64u
122 #define f_uclose 256u
124 /*----- Main code ---------------------------------------------------------*/
126 static void reap(int sig
)
129 while (waitpid(-1, 0, WNOHANG
) > 0)
134 static void writelog(const char *cat
, const char *msg
)
138 struct tm
*tm
= localtime(&t
);
139 strftime(buf
, sizeof(buf
), "%Y-%m-%d %H:%M:%S", tm
);
140 fprintf(logfp
, "%s %s: %s\n", buf
, cat
, msg
);
143 static void cline(char *p
, void *b
)
148 die(EXIT_FAILURE
, "server dropped the connection");
154 if (strcmp(q
, "WARN") == 0) {
156 syslog(LOG_WARNING
, "%s", p
);
158 writelog("warning", p
);
160 fprintf(stderr
, "Warning: %s\n", p
);
161 } else if (strcmp(q
, "TRACE") == 0) {
163 syslog(LOG_DEBUG
, "%s", p
);
165 writelog("debug", p
);
166 } else if (!(f
& f_command
)) {
168 syslog(LOG_ERR
, "unexpected output `%s %s'", q
, p
);
171 dstr_putf(&d
, "unexpected output `%s %s'", q
, p
);
172 writelog("error", d
.buf
);
175 } else if (strcmp(q
, "FAIL") == 0)
176 die(EXIT_FAILURE
, "%s", p
);
177 else if (strcmp(q
, "INFO") == 0)
179 else if (strcmp(q
, "OK") == 0)
182 die(EXIT_FAILURE
, "unexpected output `%s %s'", q
, p
);
185 static void sline(char *p
, void *b
)
189 moan("server closed the connection");
195 static void uline(char *p
, void *b
)
205 write(fd
, p
, sz
+ 1);
209 static void logfile(const char *name
)
213 if ((fp
= fopen(name
, "a")) != 0) {
217 setvbuf(logfp
, 0, _IOLBF
, BUFSIZ
);
220 dstr_putf(&d
, "error opening logfile `%s': %s", name
, strerror(errno
));
222 writelog("error", d
.buf
);
224 die(EXIT_FAILURE
, d
.buf
);
226 syslog(LOG_ERR
, d
.buf
);
231 static void sighup(int sig
, void *v
)
236 static void cleanup(void)
242 static void sigdie(int sig
)
245 signal(sig
, SIG_DFL
);
249 static void version(FILE *fp
)
251 pquis(fp
, "$, TrIPE version " VERSION
"\n");
254 static void usage(FILE *fp
)
258 $ [-w] [-options] [command [args]...]\n\
259 $ [-Dl] [-f file] [-options]\n\
261 [-s] [-d directory] [-a socket] [-P pidfile]\n\
262 [-p program] [-S arg,arg,...]\n\
266 static void help(FILE *fp
)
275 -h, --help Show this help text.\n\
276 -v, --version Show version number.\n\
277 -u, --usage Show brief usage message.\n\
279 -D, --daemon Become a background task after connecting.\n\
280 -d, --directory=DIR Select current directory [default /var/lib/tripe]\n\
281 -a, --admin-socket=FILE Select socket to connect to.\n\
282 -P, --pidfile=FILE Write process-id to FILE.\n\
284 -s, --spawn Start server rather than connecting.\n\
285 -p, --spawn-path=PATH Specify path to executable.\n\
286 -S, --spawn-args=ARGS Specify comma-separated arguments.\n\
288 -l, --syslog Log messages to system log.\n\
289 -f, --logfile=FILE Log messages to FILE.\n\
290 -w, --warnings Show warnings when running commands.\n\
294 int main(int argc
, char *argv
[])
296 const char *dir
= "/var/lib/tripe";
297 const char *sock
= "tripesock";
298 const char *spawnpath
= "tripe";
299 string_v spawnopts
= DA_INIT
;
305 if ((p
= getenv("TRIPEDIR")) != 0)
308 /* --- Parse the arguments --- */
311 static const struct option opts
[] = {
312 { "help", 0, 0, 'h' },
313 { "version", 0, 0, 'v' },
314 { "usage", 0, 0, 'u' },
315 { "daemon", 0, 0, 'D' },
316 { "directory", OPTF_ARGREQ
, 0, 'd' },
317 { "admin-socket", OPTF_ARGREQ
, 0, 'a' },
318 { "spawn", 0, 0, 's' },
319 { "spawn-path", OPTF_ARGREQ
, 0, 'p' },
320 { "spawn-args", OPTF_ARGREQ
, 0, 'S' },
321 { "syslog", 0, 0, 'l' },
322 { "logfile", OPTF_ARGREQ
, 0, 'f' },
323 { "warnings", 0, 0, 'w' },
324 { "pidfile", OPTF_ARGREQ
, 0, 'P' },
328 int i
= mdwopt(argc
, argv
, "+hvuDd:a:sp:S:lwf:nP:", opts
, 0, 0, 0);
342 f
|= f_daemon
| f_noinput
;
358 f
|= f_spawn
| f_spawnopts
;
359 for (p
= strtok(optarg
, ","); p
; p
= strtok(0, ","))
360 DA_PUSH(&spawnopts
, p
);
363 f
|= f_syslog
| f_noinput
;
380 if ((f
& f_bogus
) || ((f
& f_noinput
) && optind
< argc
)) {
385 /* --- Set various things up --- */
388 die(EXIT_FAILURE
, "couldn't set `%s' as current directory: %s",
389 dir
, strerror(errno
));
393 if (!pidfile
&& (f
& f_daemon
) && ((f
& f_syslog
) || logname
))
394 pidfile
= "tripectl.pid";
395 if (pidfile
&& (pidfp
= fopen(pidfile
, "w")) == 0) {
396 die(EXIT_FAILURE
, "couldn't open `%s' for writing: %s",
397 pidfile
, strerror(errno
));
399 signal(SIGINT
, sigdie
);
400 signal(SIGQUIT
, sigdie
);
401 signal(SIGTERM
, sigdie
);
404 /* --- Connect to the server --- */
410 sigset_t newmask
, oldmask
;
412 sa
.sa_handler
= reap
;
413 sigemptyset(&sa
.sa_mask
);
414 sa
.sa_flags
= SA_NOCLDSTOP
;
416 sa
.sa_flags
|= SA_RESTART
;
418 sigaction(SIGCHLD
, &sa
, 0);
420 DA_UNSHIFT(&spawnopts
, (char *)sock
);
421 DA_UNSHIFT(&spawnopts
, "-a");
422 DA_UNSHIFT(&spawnopts
, "-d.");
423 DA_UNSHIFT(&spawnopts
, (char *)spawnpath
);
424 DA_PUSH(&spawnopts
, 0);
425 if (socketpair(PF_UNIX
, SOCK_STREAM
, 0, pfd
))
426 die(EXIT_FAILURE
, "error from socketpair: %s", strerror(errno
));
427 sigemptyset(&newmask
);
428 sigaddset(&newmask
, SIGCHLD
);
429 sigprocmask(SIG_BLOCK
, &newmask
, &oldmask
);
430 if ((kid
= fork()) < 0)
431 die(EXIT_FAILURE
, "fork failed: %s", strerror(errno
));
433 dup2(pfd
[1], STDIN_FILENO
);
434 dup2(pfd
[1], STDOUT_FILENO
);
442 execvp(DA(&spawnopts
)[0], DA(&spawnopts
));
443 die(127, "couldn't exec `%s': %s", spawnpath
, strerror(errno
));
445 sigprocmask(SIG_SETMASK
, &oldmask
, 0);
449 struct sockaddr_un sun
;
450 size_t sz
= strlen(sock
) + 1;
451 if (sz
> sizeof(sun
.sun_path
))
452 die(EXIT_FAILURE
, "socket name `%s' too long", sock
);
453 memset(&sun
, 0, sizeof(sun
));
454 sun
.sun_family
= AF_UNIX
;
455 memcpy(sun
.sun_path
, sock
, sz
);
456 sz
= sz
+ offsetof(struct sockaddr_un
, sun_path
);
457 if ((fd
= socket(PF_UNIX
, SOCK_STREAM
, 0)) < 0)
458 die(EXIT_FAILURE
, "error making socket: %s", strerror(errno
));
459 if (connect(fd
, (struct sockaddr
*)&sun
, sz
)) {
460 die(EXIT_FAILURE
, "error connecting to `%s': %s",
461 sun
.sun_path
, strerror(errno
));
467 die(EXIT_FAILURE
, "error becoming daemon: %s", strerror(errno
));
470 fprintf(pidfp
, "%li", (long)getpid());
474 /* --- If we're meant to be interactive, do that --- */
476 if (!(f
& f_noinput
) && optind
== argc
) {
481 selbuf_init(&bu
, &sel
, STDIN_FILENO
, uline
, &bu
);
482 selbuf_init(&bs
, &sel
, fd
, sline
, &bs
);
484 if (sel_select(&sel
) && errno
!= EINTR
&& errno
!= EAGAIN
)
485 die(EXIT_FAILURE
, "select failed: %s", strerror(errno
));
489 /* --- If there's a command, submit it --- */
493 dstr_puts(&d
, argv
[optind
++]);
494 while (optind
< argc
) {
496 dstr_puts(&d
, argv
[optind
++]);
499 write(fd
, d
.buf
, d
.len
);
505 /* --- Pull everything else out of the box --- */
513 selbuf_init(&b
, &sel
, fd
, cline
, 0);
516 openlog(QUIS
, 0, LOG_DAEMON
);
519 sig_add(&hup
, SIGHUP
, sighup
, 0);
522 if (sel_select(&sel
) && errno
!= EINTR
&& errno
!= EAGAIN
)
523 die(EXIT_FAILURE
, "select failed: %s", strerror(errno
));
530 /*----- That's all, folks -------------------------------------------------*/