summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authornicm <nicm@openbsd.org>2014-03-26 13:00:50 +0000
committernicm <nicm@openbsd.org>2014-03-26 13:00:50 +0000
commit439243a3f3b3789adf5847d8569a85b6ce757624 (patch)
tree1da323f9e6271d2d24930506197a7038a3551554
parentupdate manpage (diff)
downloadwireguard-openbsd-439243a3f3b3789adf5847d8569a85b6ce757624.tar.xz
wireguard-openbsd-439243a3f3b3789adf5847d8569a85b6ce757624.zip
Add support for retrieving the line and speed from the /etc/remote dv
and br capabilities like tip(1). No other capabilities are supported. Also handle REMOTE in the environment as either a separate remote(5) file or a host. Discussed with and approval from several, man page help from schwarze@.
-rw-r--r--usr.bin/cu/cu.131
-rw-r--r--usr.bin/cu/cu.c91
2 files changed, 100 insertions, 22 deletions
diff --git a/usr.bin/cu/cu.1 b/usr.bin/cu/cu.1
index b1bd4b3edd9..be0b8c7388a 100644
--- a/usr.bin/cu/cu.1
+++ b/usr.bin/cu/cu.1
@@ -1,4 +1,4 @@
-.\" $OpenBSD: cu.1,v 1.9 2013/03/11 12:47:49 nicm Exp $
+.\" $OpenBSD: cu.1,v 1.10 2014/03/26 13:00:50 nicm Exp $
.\"
.\" Copyright (c) 1980, 1990, 1993
.\" The Regents of the University of California. All rights reserved.
@@ -29,7 +29,7 @@
.\"
.\" @(#)tip.1 8.4 (Berkeley) 4/18/94
.\"
-.Dd $Mdocdate: March 11 2013 $
+.Dd $Mdocdate: March 26 2014 $
.Dt CU 1
.Os
.Sh NAME
@@ -39,6 +39,7 @@
.Nm
.Op Fl l Ar line
.Op Fl s Ar speed \*(Ba Fl Ar speed
+.Op Ar host
.Sh DESCRIPTION
.Nm
is used to connect to another system over a serial link.
@@ -72,6 +73,21 @@ Set the speed of the connection.
The default is 9600.
.El
.Pp
+If
+.Ar host
+is given,
+.Nm
+uses the
+.Xr remote 5
+host description file to retrieve the
+.Sy dv Pq device
+and
+.Sy br Pq baud rate
+capabilities for that host.
+The
+.Nm
+utility ignores other capabilities found in that database.
+.Pp
Typed characters are normally transmitted directly to the remote
machine (which does the echoing as well).
A tilde
@@ -135,6 +151,17 @@ dialogue and return the user to the remote machine.
.Nm
guards against multiple users connecting to a remote system by opening
modems and terminal lines with exclusive access.
+.Sh ENVIRONMENT
+If
+.Ev REMOTE
+is set and begins with a slash, the named file is searched before the
+.Xr remote 5
+host description file; otherwise it is searched for as a host.
+.Sh FILES
+.Bl -tag -width /etc/remote
+.It Pa /etc/remote
+host description file
+.El
.Sh HISTORY
The
.Nm
diff --git a/usr.bin/cu/cu.c b/usr.bin/cu/cu.c
index e0feb21ca40..219e90f0e7a 100644
--- a/usr.bin/cu/cu.c
+++ b/usr.bin/cu/cu.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: cu.c,v 1.15 2013/11/20 20:55:09 deraadt Exp $ */
+/* $OpenBSD: cu.c,v 1.16 2014/03/26 13:00:50 nicm Exp $ */
/*
* Copyright (c) 2012 Nicholas Marriott <nicm@openbsd.org>
@@ -41,6 +41,8 @@ FILE *record_file;
struct termios saved_tio;
struct bufferevent *input_ev;
struct bufferevent *output_ev;
+const char *line_path = NULL;
+int line_speed = -1;
int line_fd;
struct termios line_tio;
struct bufferevent *line_ev;
@@ -58,11 +60,12 @@ void stream_read(struct bufferevent *, void *);
void stream_error(struct bufferevent *, short, void *);
void line_read(struct bufferevent *, void *);
void line_error(struct bufferevent *, short, void *);
+void try_remote(const char *, const char *);
__dead void
usage(void)
{
- fprintf(stderr, "usage: %s [-l line] [-s speed | -speed]\n",
+ fprintf(stderr, "usage: %s [-l line] [-s speed | -speed] [host]\n",
__progname);
exit(1);
}
@@ -70,12 +73,12 @@ usage(void)
int
main(int argc, char **argv)
{
- const char *line, *errstr;
- char *tmp;
- int opt, speed, i;
+ const char *errstr;
+ char *tmp, *s;
+ int opt, i;
- line = "/dev/cua00";
- speed = 9600;
+ if (isatty(STDIN_FILENO) && tcgetattr(STDIN_FILENO, &saved_tio) != 0)
+ err(1, "tcgetattr");
/*
* Convert obsolescent -### speed to modern -s### syntax which getopt()
@@ -94,10 +97,10 @@ main(int argc, char **argv)
while ((opt = getopt(argc, argv, "l:s:")) != -1) {
switch (opt) {
case 'l':
- line = optarg;
+ line_path = optarg;
break;
case 's':
- speed = strtonum(optarg, 0, UINT_MAX, &errstr);
+ line_speed = strtonum(optarg, 0, INT_MAX, &errstr);
if (errstr != NULL)
errx(1, "speed is %s: %s", errstr, optarg);
break;
@@ -107,28 +110,39 @@ main(int argc, char **argv)
}
argc -= optind;
argv += optind;
- if (argc != 0)
+ if (argc != 0 && argc != 1)
usage();
- if (strchr(line, '/') == NULL) {
- if (asprintf(&tmp, "%s%s", _PATH_DEV, line) == -1)
+ s = getenv("REMOTE");
+ if (argc == 1) {
+ if (s != NULL && *s == '/')
+ try_remote(argv[0], s);
+ else
+ try_remote(argv[0], NULL);
+ } else if (s != NULL && *s != '/')
+ try_remote(s, NULL);
+
+ if (line_path == NULL)
+ line_path = "/dev/cua00";
+ if (line_speed == -1)
+ line_speed = 9600;
+
+ if (strchr(line_path, '/') == NULL) {
+ if (asprintf(&tmp, "%s%s", _PATH_DEV, line_path) == -1)
err(1, "asprintf");
- line = tmp;
+ line_path = tmp;
}
- line_fd = open(line, O_RDWR);
+ line_fd = open(line_path, O_RDWR);
if (line_fd < 0)
- err(1, "open(\"%s\")", line);
+ err(1, "open(\"%s\")", line_path);
if (ioctl(line_fd, TIOCEXCL) != 0)
err(1, "ioctl(TIOCEXCL)");
if (tcgetattr(line_fd, &line_tio) != 0)
err(1, "tcgetattr");
- if (set_line(speed) != 0)
+ if (set_line(line_speed) != 0)
err(1, "tcsetattr");
- if (isatty(STDIN_FILENO) && tcgetattr(STDIN_FILENO, &saved_tio) != 0)
- err(1, "tcgetattr");
-
event_init();
signal_set(&sigterm_ev, SIGTERM, signal_event, NULL);
@@ -154,7 +168,7 @@ main(int argc, char **argv)
NULL);
bufferevent_enable(line_ev, EV_READ|EV_WRITE);
- printf("Connected (speed %d)\r\n", speed);
+ printf("Connected to %s (speed %d)\r\n", line_path, line_speed);
event_dispatch();
restore_termios();
@@ -291,6 +305,43 @@ line_error(struct bufferevent *bufev, short what, void *data)
event_loopexit(NULL);
}
+void
+try_remote(const char *host, const char *path)
+{
+ const char *paths[] = { "/etc/remote", NULL, NULL };
+ char *cp, *s;
+ long l;
+ int error;
+
+ if (path != NULL) {
+ paths[0] = path;
+ paths[1] = "/etc/remote";
+ }
+
+ error = cgetent(&cp, (char**)paths, (char*)host);
+ if (error < 0) {
+ switch (error) {
+ case -1:
+ cu_errx(1, "unknown host %s", host);
+ case -2:
+ cu_errx(1, "can't open remote file");
+ case -3:
+ cu_errx(1, "loop in remote file");
+ default:
+ cu_errx(1, "unknown error in remote file");
+ }
+ }
+
+ if (line_path == NULL && cgetstr(cp, "dv", &s) >= 0)
+ line_path = s;
+
+ if (line_speed == -1 && cgetnum(cp, "br", &l) >= 0) {
+ if (l < 0 || l > INT_MAX)
+ cu_errx(1, "speed out of range");
+ line_speed = l;
+ }
+}
+
/* Expands tildes in the file name. Based on code from ssh/misc.c. */
char *
tilde_expand(const char *filename1)