aboutsummaryrefslogtreecommitdiff
path: root/apps
diff options
context:
space:
mode:
authorBrent Cook <bcook@openbsd.org>2015-09-13 11:56:41 -0500
committerBrent Cook <bcook@openbsd.org>2015-09-13 18:42:15 -0500
commit8c90be2a29053ac613dfe0c11a423da16c7c4520 (patch)
treecf31a8e35cd9793f5f6f622b0ffcce61bbad8652 /apps
parent627b0261a81bb18ef95156baa37101ddcb14e356 (diff)
downloadportable-8c90be2a29053ac613dfe0c11a423da16c7c4520.tar.gz
portable-8c90be2a29053ac613dfe0c11a423da16c7c4520.tar.bz2
portable-8c90be2a29053ac613dfe0c11a423da16c7c4520.zip
allow nc to build on linux and os x
Diffstat (limited to 'apps')
-rw-r--r--apps/nc/Makefile.am19
-rw-r--r--apps/nc/compat/accept4.c17
-rw-r--r--apps/nc/compat/readpassphrase.c205
-rw-r--r--apps/nc/compat/socket.c29
-rw-r--r--apps/nc/compat/strtonum.c65
-rw-r--r--apps/nc/compat/sys/socket.h31
6 files changed, 361 insertions, 5 deletions
diff --git a/apps/nc/Makefile.am b/apps/nc/Makefile.am
index 916681b..89fe266 100644
--- a/apps/nc/Makefile.am
+++ b/apps/nc/Makefile.am
@@ -1,8 +1,6 @@
1include $(top_srcdir)/Makefile.am.common 1include $(top_srcdir)/Makefile.am.common
2 2
3if HOST_OPENBSD 3if BUILD_NC
4
5if HAVE_POLL
6 4
7noinst_PROGRAMS = nc 5noinst_PROGRAMS = nc
8 6
@@ -11,15 +9,26 @@ EXTRA_DIST = nc.1
11nc_LDADD = $(PLATFORM_LDADD) $(PROG_LDADD) 9nc_LDADD = $(PLATFORM_LDADD) $(PROG_LDADD)
12nc_LDADD += $(top_builddir)/tls/libtls.la 10nc_LDADD += $(top_builddir)/tls/libtls.la
13 11
12CPPFLAGS += -I$(top_srcdir)/apps/nc/compat
13
14nc_SOURCES = atomicio.c 14nc_SOURCES = atomicio.c
15nc_SOURCES += netcat.c 15nc_SOURCES += netcat.c
16nc_SOURCES += socks.c 16nc_SOURCES += socks.c
17noinst_HEADERS = atomicio.h 17noinst_HEADERS = atomicio.h
18noinst_HEADERS += compat/sys/socket.h
18 19
19if !HAVE_STRTONUM 20nc_SOURCES += compat/socket.c
20nc_SOURCES += strtonum.c 21
22if !HAVE_ACCEPT4
23nc_SOURCES += compat/accept4.c
21endif 24endif
22 25
26if !HAVE_READPASSPHRASE
27nc_SOURCES += compat/readpassphrase.c
28endif
29
30if !HAVE_STRTONUM
31nc_SOURCES += compat/strtonum.c
23endif 32endif
24 33
25endif 34endif
diff --git a/apps/nc/compat/accept4.c b/apps/nc/compat/accept4.c
new file mode 100644
index 0000000..278198b
--- /dev/null
+++ b/apps/nc/compat/accept4.c
@@ -0,0 +1,17 @@
1#include <sys/socket.h>
2#include <fcntl.h>
3
4int
5accept4(int s, struct sockaddr *addr, socklen_t *addrlen, int flags)
6{
7 int rets = accept(s, addr, addrlen);
8 if (rets == -1)
9 return s;
10
11 if (flags & SOCK_CLOEXEC) {
12 flags = fcntl(s, F_GETFD);
13 fcntl(rets, F_SETFD, flags | FD_CLOEXEC);
14 }
15
16 return rets;
17}
diff --git a/apps/nc/compat/readpassphrase.c b/apps/nc/compat/readpassphrase.c
new file mode 100644
index 0000000..0d04134
--- /dev/null
+++ b/apps/nc/compat/readpassphrase.c
@@ -0,0 +1,205 @@
1/* $OpenBSD: readpassphrase.c,v 1.22 2010/01/13 10:20:54 dtucker Exp $ */
2
3/*
4 * Copyright (c) 2000-2002, 2007 Todd C. Miller <Todd.Miller@courtesan.com>
5 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 *
18 * Sponsored in part by the Defense Advanced Research Projects
19 * Agency (DARPA) and Air Force Research Laboratory, Air Force
20 * Materiel Command, USAF, under agreement number F39502-99-1-0512.
21 */
22
23/* OPENBSD ORIGINAL: lib/libc/gen/readpassphrase.c */
24
25#include <termios.h>
26#include <signal.h>
27#include <ctype.h>
28#include <fcntl.h>
29#include <errno.h>
30#include <string.h>
31#include <unistd.h>
32
33#include <readpassphrase.h>
34
35#ifndef _PATH_TTY
36# define _PATH_TTY "/dev/tty"
37#endif
38
39#ifdef TCSASOFT
40# define _T_FLUSH (TCSAFLUSH|TCSASOFT)
41#else
42# define _T_FLUSH (TCSAFLUSH)
43#endif
44
45/* SunOS 4.x which lacks _POSIX_VDISABLE, but has VDISABLE */
46#if !defined(_POSIX_VDISABLE) && defined(VDISABLE)
47# define _POSIX_VDISABLE VDISABLE
48#endif
49
50#ifndef _NSIG
51# ifdef NSIG
52# define _NSIG NSIG
53# else
54# define _NSIG 128
55# endif
56#endif
57
58static volatile sig_atomic_t signo[_NSIG];
59
60static void handler(int);
61
62char *
63readpassphrase(const char *prompt, char *buf, size_t bufsiz, int flags)
64{
65 ssize_t bytes_written = 0;
66 ssize_t nr;
67 int input, output, save_errno, i, need_restart;
68 char ch, *p, *end;
69 struct termios term, oterm;
70 struct sigaction sa, savealrm, saveint, savehup, savequit, saveterm;
71 struct sigaction savetstp, savettin, savettou, savepipe;
72
73 /* I suppose we could alloc on demand in this case (XXX). */
74 if (bufsiz == 0) {
75 errno = EINVAL;
76 return(NULL);
77 }
78
79restart:
80 for (i = 0; i < _NSIG; i++)
81 signo[i] = 0;
82 nr = -1;
83 save_errno = 0;
84 need_restart = 0;
85 /*
86 * Read and write to /dev/tty if available. If not, read from
87 * stdin and write to stderr unless a tty is required.
88 */
89 if ((flags & RPP_STDIN) ||
90 (input = output = open(_PATH_TTY, O_RDWR)) == -1) {
91 if (flags & RPP_REQUIRE_TTY) {
92 errno = ENOTTY;
93 return(NULL);
94 }
95 input = STDIN_FILENO;
96 output = STDERR_FILENO;
97 }
98
99 /*
100 * Catch signals that would otherwise cause the user to end
101 * up with echo turned off in the shell. Don't worry about
102 * things like SIGXCPU and SIGVTALRM for now.
103 */
104 sigemptyset(&sa.sa_mask);
105 sa.sa_flags = 0; /* don't restart system calls */
106 sa.sa_handler = handler;
107 (void)sigaction(SIGALRM, &sa, &savealrm);
108 (void)sigaction(SIGHUP, &sa, &savehup);
109 (void)sigaction(SIGINT, &sa, &saveint);
110 (void)sigaction(SIGPIPE, &sa, &savepipe);
111 (void)sigaction(SIGQUIT, &sa, &savequit);
112 (void)sigaction(SIGTERM, &sa, &saveterm);
113 (void)sigaction(SIGTSTP, &sa, &savetstp);
114 (void)sigaction(SIGTTIN, &sa, &savettin);
115 (void)sigaction(SIGTTOU, &sa, &savettou);
116
117 /* Turn off echo if possible. */
118 if (input != STDIN_FILENO && tcgetattr(input, &oterm) == 0) {
119 memcpy(&term, &oterm, sizeof(term));
120 if (!(flags & RPP_ECHO_ON))
121 term.c_lflag &= ~(ECHO | ECHONL);
122#ifdef VSTATUS
123 if (term.c_cc[VSTATUS] != _POSIX_VDISABLE)
124 term.c_cc[VSTATUS] = _POSIX_VDISABLE;
125#endif
126 (void)tcsetattr(input, _T_FLUSH, &term);
127 } else {
128 memset(&term, 0, sizeof(term));
129 term.c_lflag |= ECHO;
130 memset(&oterm, 0, sizeof(oterm));
131 oterm.c_lflag |= ECHO;
132 }
133
134 /* No I/O if we are already backgrounded. */
135 if (signo[SIGTTOU] != 1 && signo[SIGTTIN] != 1) {
136 if (!(flags & RPP_STDIN))
137 bytes_written = write(output, prompt, strlen(prompt));
138 end = buf + bufsiz - 1;
139 p = buf;
140 while ((nr = read(input, &ch, 1)) == 1 && ch != '\n' && ch != '\r') {
141 if (p < end) {
142 if ((flags & RPP_SEVENBIT))
143 ch &= 0x7f;
144 if (isalpha(ch)) {
145 if ((flags & RPP_FORCELOWER))
146 ch = (char)tolower(ch);
147 if ((flags & RPP_FORCEUPPER))
148 ch = (char)toupper(ch);
149 }
150 *p++ = ch;
151 }
152 }
153 *p = '\0';
154 save_errno = errno;
155 if (!(term.c_lflag & ECHO))
156 bytes_written = write(output, "\n", 1);
157 }
158
159 (void) bytes_written;
160
161 /* Restore old terminal settings and signals. */
162 if (memcmp(&term, &oterm, sizeof(term)) != 0) {
163 while (tcsetattr(input, _T_FLUSH, &oterm) == -1 &&
164 errno == EINTR)
165 continue;
166 }
167 (void)sigaction(SIGALRM, &savealrm, NULL);
168 (void)sigaction(SIGHUP, &savehup, NULL);
169 (void)sigaction(SIGINT, &saveint, NULL);
170 (void)sigaction(SIGQUIT, &savequit, NULL);
171 (void)sigaction(SIGPIPE, &savepipe, NULL);
172 (void)sigaction(SIGTERM, &saveterm, NULL);
173 (void)sigaction(SIGTSTP, &savetstp, NULL);
174 (void)sigaction(SIGTTIN, &savettin, NULL);
175 (void)sigaction(SIGTTOU, &savettou, NULL);
176 if (input != STDIN_FILENO)
177 (void)close(input);
178
179 /*
180 * If we were interrupted by a signal, resend it to ourselves
181 * now that we have restored the signal handlers.
182 */
183 for (i = 0; i < _NSIG; i++) {
184 if (signo[i]) {
185 kill(getpid(), i);
186 switch (i) {
187 case SIGTSTP:
188 case SIGTTIN:
189 case SIGTTOU:
190 need_restart = 1;
191 }
192 }
193 }
194 if (need_restart)
195 goto restart;
196
197 if (save_errno)
198 errno = save_errno;
199 return(nr == -1 ? NULL : buf);
200}
201
202static void handler(int s)
203{
204 signo[s] = 1;
205}
diff --git a/apps/nc/compat/socket.c b/apps/nc/compat/socket.c
new file mode 100644
index 0000000..fd699f9
--- /dev/null
+++ b/apps/nc/compat/socket.c
@@ -0,0 +1,29 @@
1#define SOCKET_FLAGS_PRIV
2
3#include <sys/socket.h>
4
5#ifdef NEED_SOCKET_FLAGS
6
7#include <fcntl.h>
8
9int
10_socket(int domain, int type, int protocol)
11{
12 int s = socket(domain, type & ~(SOCK_CLOEXEC | SOCK_NONBLOCK), protocol);
13 int flags;
14 if (s == -1)
15 return s;
16
17 if (type & SOCK_CLOEXEC) {
18 flags = fcntl(s, F_GETFD);
19 fcntl(s, F_SETFD, flags | FD_CLOEXEC);
20 }
21
22 if (type & SOCK_NONBLOCK) {
23 flags = fcntl(s, F_GETFL);
24 fcntl(s, F_SETFL, flags | O_NONBLOCK);
25 }
26 return s;
27}
28
29#endif
diff --git a/apps/nc/compat/strtonum.c b/apps/nc/compat/strtonum.c
new file mode 100644
index 0000000..1aeee34
--- /dev/null
+++ b/apps/nc/compat/strtonum.c
@@ -0,0 +1,65 @@
1/* $OpenBSD: strtonum.c,v 1.7 2013/04/17 18:40:58 tedu Exp $ */
2
3/*
4 * Copyright (c) 2004 Ted Unangst and Todd Miller
5 * All rights reserved.
6 *
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 */
19
20#include <errno.h>
21#include <limits.h>
22#include <stdlib.h>
23
24#define INVALID 1
25#define TOOSMALL 2
26#define TOOLARGE 3
27
28long long
29strtonum(const char *numstr, long long minval, long long maxval,
30 const char **errstrp)
31{
32 long long ll = 0;
33 int error = 0;
34 char *ep;
35 struct errval {
36 const char *errstr;
37 int err;
38 } ev[4] = {
39 { NULL, 0 },
40 { "invalid", EINVAL },
41 { "too small", ERANGE },
42 { "too large", ERANGE },
43 };
44
45 ev[0].err = errno;
46 errno = 0;
47 if (minval > maxval) {
48 error = INVALID;
49 } else {
50 ll = strtoll(numstr, &ep, 10);
51 if (numstr == ep || *ep != '\0')
52 error = INVALID;
53 else if ((ll == LLONG_MIN && errno == ERANGE) || ll < minval)
54 error = TOOSMALL;
55 else if ((ll == LLONG_MAX && errno == ERANGE) || ll > maxval)
56 error = TOOLARGE;
57 }
58 if (errstrp != NULL)
59 *errstrp = ev[error].errstr;
60 errno = ev[error].err;
61 if (error)
62 ll = 0;
63
64 return (ll);
65}
diff --git a/apps/nc/compat/sys/socket.h b/apps/nc/compat/sys/socket.h
new file mode 100644
index 0000000..13eb380
--- /dev/null
+++ b/apps/nc/compat/sys/socket.h
@@ -0,0 +1,31 @@
1/*
2 * Public domain
3 * sys/socket.h compatibility shim
4 */
5
6#ifndef _WIN32
7#include_next <sys/socket.h>
8
9#if !defined(SOCK_NONBLOCK) || !defined(SOCK_CLOEXEC)
10#define NEED_SOCKET_FLAGS
11int _socket(int domain, int type, int protocol);
12#ifndef SOCKET_FLAGS_PRIV
13#define socket(d, t, p) _socket(d, t, p)
14#endif
15#endif
16
17#ifndef SOCK_NONBLOCK
18#define SOCK_NONBLOCK 0x4000 /* set O_NONBLOCK */
19#endif
20
21#ifndef SOCK_CLOEXEC
22#define SOCK_CLOEXEC 0x8000 /* set FD_CLOEXEC */
23#endif
24
25#ifndef HAVE_ACCEPT4
26int accept4(int s, struct sockaddr *addr, socklen_t *addrlen, int flags);
27#endif
28
29#else
30#include <win32netcompat.h>
31#endif