summaryrefslogtreecommitdiff
path: root/openbsd-compat
diff options
context:
space:
mode:
authornicoo <nicoo@debian.org>2020-02-12 13:42:22 +0100
committerNicolas Braud-Santoni <nicolas@braud-santoni.eu>2020-02-12 13:42:22 +0100
commitc79050aa44b8836d836c5dd22a383a073c28b74b (patch)
tree7bcca9fabd7718bf87ca600a6594f57b76d8de7d /openbsd-compat
Import upstream release 1.3.0
Closes: #951184
Diffstat (limited to 'openbsd-compat')
-rw-r--r--openbsd-compat/bsd-getline.c115
-rw-r--r--openbsd-compat/bsd-getpagesize.c27
-rwxr-xr-xopenbsd-compat/diff.sh24
-rw-r--r--openbsd-compat/err.h85
-rw-r--r--openbsd-compat/explicit_bzero.c57
-rw-r--r--openbsd-compat/explicit_bzero_win32.c19
-rw-r--r--openbsd-compat/getopt.h74
-rw-r--r--openbsd-compat/getopt_long.c523
-rw-r--r--openbsd-compat/openbsd-compat.h87
-rw-r--r--openbsd-compat/posix_win.c61
-rw-r--r--openbsd-compat/posix_win.h47
-rw-r--r--openbsd-compat/readpassphrase.c214
-rw-r--r--openbsd-compat/readpassphrase.h42
-rw-r--r--openbsd-compat/readpassphrase_win32.c131
-rw-r--r--openbsd-compat/recallocarray.c91
-rw-r--r--openbsd-compat/strlcat.c63
-rw-r--r--openbsd-compat/strlcpy.c59
-rw-r--r--openbsd-compat/timingsafe_bcmp.c35
-rw-r--r--openbsd-compat/types.h71
19 files changed, 1825 insertions, 0 deletions
diff --git a/openbsd-compat/bsd-getline.c b/openbsd-compat/bsd-getline.c
new file mode 100644
index 0000000..52b44f7
--- /dev/null
+++ b/openbsd-compat/bsd-getline.c
@@ -0,0 +1,115 @@
1/* $NetBSD: getline.c,v 1.1.1.6 2015/01/02 20:34:27 christos Exp $ */
2
3/* NetBSD: getline.c,v 1.2 2014/09/16 17:23:50 christos Exp */
4
5/*-
6 * Copyright (c) 2011 The NetBSD Foundation, Inc.
7 * All rights reserved.
8 *
9 * This code is derived from software contributed to The NetBSD Foundation
10 * by Christos Zoulas.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
32 */
33
34/* NETBSD ORIGINAL: external/bsd/file/dist/src/getline.c */
35
36#include "openbsd-compat.h"
37
38#if 0
39#include "file.h"
40#endif
41
42#if !HAVE_GETLINE
43#include <stdlib.h>
44#include <stdio.h>
45#ifdef HAVE_UNISTD_H
46#include <unistd.h>
47#endif
48#include <errno.h>
49#include <string.h>
50
51static ssize_t
52getdelim(char **buf, size_t *bufsiz, int delimiter, FILE *fp)
53{
54 char *ptr, *eptr;
55
56
57 if (*buf == NULL || *bufsiz == 0) {
58 if ((*buf = malloc(BUFSIZ)) == NULL)
59 return -1;
60 *bufsiz = BUFSIZ;
61 }
62
63 for (ptr = *buf, eptr = *buf + *bufsiz;;) {
64 int c = fgetc(fp);
65 if (c == -1) {
66 if (feof(fp)) {
67 ssize_t diff = (ssize_t)(ptr - *buf);
68 if (diff != 0) {
69 *ptr = '\0';
70 return diff;
71 }
72 }
73 return -1;
74 }
75 *ptr++ = (char)c;
76 if (c == delimiter) {
77 *ptr = '\0';
78 return ptr - *buf;
79 }
80 if (ptr + 2 >= eptr) {
81 char *nbuf;
82 size_t nbufsiz = *bufsiz * 2;
83 ssize_t d = ptr - *buf;
84 if ((nbuf = realloc(*buf, nbufsiz)) == NULL)
85 return -1;
86 *buf = nbuf;
87 *bufsiz = nbufsiz;
88 eptr = nbuf + nbufsiz;
89 ptr = nbuf + d;
90 }
91 }
92}
93
94ssize_t
95getline(char **buf, size_t *bufsiz, FILE *fp)
96{
97 return getdelim(buf, bufsiz, '\n', fp);
98}
99
100#endif
101
102#ifdef TEST
103int
104main(int argc, char *argv[])
105{
106 char *p = NULL;
107 ssize_t len;
108 size_t n = 0;
109
110 while ((len = getline(&p, &n, stdin)) != -1)
111 (void)printf("%" SIZE_T_FORMAT "d %s", len, p);
112 free(p);
113 return 0;
114}
115#endif
diff --git a/openbsd-compat/bsd-getpagesize.c b/openbsd-compat/bsd-getpagesize.c
new file mode 100644
index 0000000..903bfc3
--- /dev/null
+++ b/openbsd-compat/bsd-getpagesize.c
@@ -0,0 +1,27 @@
1/* Placed in the public domain */
2
3#include "openbsd-compat.h"
4
5#if !defined(HAVE_GETPAGESIZE)
6
7#ifdef HAVE_UNISTD_H
8#include <unistd.h>
9#endif
10#include <limits.h>
11
12int
13getpagesize(void)
14{
15#if defined(HAVE_SYSCONF) && defined(_SC_PAGESIZE)
16 long r = sysconf(_SC_PAGESIZE);
17 if (r > 0 && r < INT_MAX)
18 return (int)r;
19#endif
20 /*
21 * This is at the lower end of common values and appropriate for
22 * our current use of getpagesize() in recallocarray().
23 */
24 return 4096;
25}
26
27#endif /* !defined(HAVE_GETPAGESIZE) */
diff --git a/openbsd-compat/diff.sh b/openbsd-compat/diff.sh
new file mode 100755
index 0000000..f21e7d8
--- /dev/null
+++ b/openbsd-compat/diff.sh
@@ -0,0 +1,24 @@
1#!/bin/bash -u
2
3# Copyright (c) 2019 Yubico AB. All rights reserved.
4# Use of this source code is governed by a BSD-style
5# license that can be found in the LICENSE file.
6
7OPENSSH=$(realpath ../../openssh)
8LIBRESSL=$(realpath ../../libressl-2.8.3)
9[[ ! -d "${OPENSSH}" || ! -d "${LIBRESSL}" ]] && exit 1
10
11diff -pu bsd-getpagesize.c ${OPENSSH}/openbsd-compat/bsd-getpagesize.c
12diff -pu err.h ${LIBRESSL}/include/compat/err.h
13diff -pu explicit_bzero.c ${OPENSSH}/openbsd-compat/explicit_bzero.c
14diff -pu explicit_bzero_win32.c ${LIBRESSL}/crypto/compat/explicit_bzero_win.c
15diff -pu getopt.h ${OPENSSH}/openbsd-compat/getopt.h
16diff -pu getopt_long.c ${OPENSSH}/openbsd-compat/getopt_long.c
17diff -pu posix_win.c ${LIBRESSL}/crypto/compat/posix_win.c
18diff -pu readpassphrase.c ${OPENSSH}/openbsd-compat/readpassphrase.c
19diff -pu readpassphrase.h ${OPENSSH}/openbsd-compat/readpassphrase.h
20diff -pu recallocarray.c ${OPENSSH}/openbsd-compat/recallocarray.c
21diff -pu strlcat.c ${OPENSSH}/openbsd-compat/strlcat.c
22diff -pu strlcpy.c ${OPENSSH}/openbsd-compat/strlcpy.c
23diff -pu timingsafe_bcmp.c ${OPENSSH}/openbsd-compat/timingsafe_bcmp.c
24diff -pu types.h ${LIBRESSL}/include/compat/sys/types.h
diff --git a/openbsd-compat/err.h b/openbsd-compat/err.h
new file mode 100644
index 0000000..394c7bb
--- /dev/null
+++ b/openbsd-compat/err.h
@@ -0,0 +1,85 @@
1/*
2 * Public domain
3 * err.h compatibility shim
4 */
5
6#ifndef _COMPAT_ERR_H
7#define _COMPAT_ERR_H
8
9#if !defined(HAVE_ERR_H)
10
11#include <errno.h>
12#include <stdarg.h>
13#include <stdlib.h>
14#include <stdio.h>
15#include <string.h>
16
17#if defined(_MSC_VER)
18__declspec(noreturn)
19#else
20__attribute__((noreturn))
21#endif
22static inline void
23err(int eval, const char *fmt, ...)
24{
25 int sverrno = errno;
26 va_list ap;
27
28 va_start(ap, fmt);
29 if (fmt != NULL) {
30 vfprintf(stderr, fmt, ap);
31 fprintf(stderr, ": ");
32 }
33 va_end(ap);
34 fprintf(stderr, "%s\n", strerror(sverrno));
35 exit(eval);
36}
37
38#if defined(_MSC_VER)
39__declspec(noreturn)
40#else
41__attribute__((noreturn))
42#endif
43static inline void
44errx(int eval, const char *fmt, ...)
45{
46 va_list ap;
47
48 va_start(ap, fmt);
49 if (fmt != NULL)
50 vfprintf(stderr, fmt, ap);
51 va_end(ap);
52 fprintf(stderr, "\n");
53 exit(eval);
54}
55
56static inline void
57warn(const char *fmt, ...)
58{
59 int sverrno = errno;
60 va_list ap;
61
62 va_start(ap, fmt);
63 if (fmt != NULL) {
64 vfprintf(stderr, fmt, ap);
65 fprintf(stderr, ": ");
66 }
67 va_end(ap);
68 fprintf(stderr, "%s\n", strerror(sverrno));
69}
70
71static inline void
72warnx(const char *fmt, ...)
73{
74 va_list ap;
75
76 va_start(ap, fmt);
77 if (fmt != NULL)
78 vfprintf(stderr, fmt, ap);
79 va_end(ap);
80 fprintf(stderr, "\n");
81}
82
83#endif /* !defined(HAVE_ERR_H) */
84
85#endif /* _COMPAT_ERR_H */
diff --git a/openbsd-compat/explicit_bzero.c b/openbsd-compat/explicit_bzero.c
new file mode 100644
index 0000000..ac64e69
--- /dev/null
+++ b/openbsd-compat/explicit_bzero.c
@@ -0,0 +1,57 @@
1/* OPENBSD ORIGINAL: lib/libc/string/explicit_bzero.c */
2/* $OpenBSD: explicit_bzero.c,v 1.1 2014/01/22 21:06:45 tedu Exp $ */
3/*
4 * Public domain.
5 * Written by Ted Unangst
6 */
7
8#include "openbsd-compat.h"
9
10#if !defined(HAVE_EXPLICIT_BZERO) && !defined(_WIN32)
11
12#include <string.h>
13
14/*
15 * explicit_bzero - don't let the compiler optimize away bzero
16 */
17
18#ifdef HAVE_MEMSET_S
19
20void
21explicit_bzero(void *p, size_t n)
22{
23 if (n == 0)
24 return;
25 (void)memset_s(p, n, 0, n);
26}
27
28#else /* HAVE_MEMSET_S */
29
30/*
31 * Indirect bzero through a volatile pointer to hopefully avoid
32 * dead-store optimisation eliminating the call.
33 */
34static void (* volatile ssh_bzero)(void *, size_t) = bzero;
35
36void
37explicit_bzero(void *p, size_t n)
38{
39 if (n == 0)
40 return;
41 /*
42 * clang -fsanitize=memory needs to intercept memset-like functions
43 * to correctly detect memory initialisation. Make sure one is called
44 * directly since our indirection trick above successfully confuses it.
45 */
46#if defined(__has_feature)
47# if __has_feature(memory_sanitizer)
48 memset(p, 0, n);
49# endif
50#endif
51
52 ssh_bzero(p, n);
53}
54
55#endif /* HAVE_MEMSET_S */
56
57#endif /* !defined(HAVE_EXPLICIT_BZERO) && !defined(_WIN32) */
diff --git a/openbsd-compat/explicit_bzero_win32.c b/openbsd-compat/explicit_bzero_win32.c
new file mode 100644
index 0000000..8017aff
--- /dev/null
+++ b/openbsd-compat/explicit_bzero_win32.c
@@ -0,0 +1,19 @@
1/*
2 * Public domain.
3 * Win32 explicit_bzero compatibility shim.
4 */
5
6#include "openbsd-compat.h"
7
8#if !defined(HAVE_EXPLICIT_BZERO) && defined(_WIN32)
9
10#include <windows.h>
11#include <string.h>
12
13void
14explicit_bzero(void *buf, size_t len)
15{
16 SecureZeroMemory(buf, len);
17}
18
19#endif /* !defined(HAVE_EXPLICIT_BZERO) && defined(_WIN32) */
diff --git a/openbsd-compat/getopt.h b/openbsd-compat/getopt.h
new file mode 100644
index 0000000..8eb1244
--- /dev/null
+++ b/openbsd-compat/getopt.h
@@ -0,0 +1,74 @@
1/* $OpenBSD: getopt.h,v 1.2 2008/06/26 05:42:04 ray Exp $ */
2/* $NetBSD: getopt.h,v 1.4 2000/07/07 10:43:54 ad Exp $ */
3
4/*-
5 * Copyright (c) 2000 The NetBSD Foundation, Inc.
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Dieter Baron and Thomas Klausner.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33#ifndef _GETOPT_H_
34#define _GETOPT_H_
35
36/*
37 * GNU-like getopt_long() and 4.4BSD getsubopt()/optreset extensions
38 */
39#define no_argument 0
40#define required_argument 1
41#define optional_argument 2
42
43struct option {
44 /* name of long option */
45 const char *name;
46 /*
47 * one of no_argument, required_argument, and optional_argument:
48 * whether option takes an argument
49 */
50 int has_arg;
51 /* if not NULL, set *flag to val when option found */
52 int *flag;
53 /* if flag not NULL, value to set *flag to; else return value */
54 int val;
55};
56
57int getopt_long(int, char * const *, const char *,
58 const struct option *, int *);
59int getopt_long_only(int, char * const *, const char *,
60 const struct option *, int *);
61#ifndef _GETOPT_DEFINED_
62#define _GETOPT_DEFINED_
63int getopt(int, char * const *, const char *);
64int getsubopt(char **, char * const *, char **);
65
66extern char *optarg; /* getopt(3) external variables */
67extern int opterr;
68extern int optind;
69extern int optopt;
70extern int optreset;
71extern char *suboptarg; /* getsubopt(3) external variable */
72#endif
73
74#endif /* !_GETOPT_H_ */
diff --git a/openbsd-compat/getopt_long.c b/openbsd-compat/getopt_long.c
new file mode 100644
index 0000000..dabbb46
--- /dev/null
+++ b/openbsd-compat/getopt_long.c
@@ -0,0 +1,523 @@
1/* $OpenBSD: getopt_long.c,v 1.25 2011/03/05 22:10:11 guenther Exp $ */
2/* $NetBSD: getopt_long.c,v 1.15 2002/01/31 22:43:40 tv Exp $ */
3
4/*
5 * Copyright (c) 2002 Todd C. Miller <Todd.Miller@courtesan.com>
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 * Sponsored in part by the Defense Advanced Research Projects
20 * Agency (DARPA) and Air Force Research Laboratory, Air Force
21 * Materiel Command, USAF, under agreement number F39502-99-1-0512.
22 */
23/*-
24 * Copyright (c) 2000 The NetBSD Foundation, Inc.
25 * All rights reserved.
26 *
27 * This code is derived from software contributed to The NetBSD Foundation
28 * by Dieter Baron and Thomas Klausner.
29 *
30 * Redistribution and use in source and binary forms, with or without
31 * modification, are permitted provided that the following conditions
32 * are met:
33 * 1. Redistributions of source code must retain the above copyright
34 * notice, this list of conditions and the following disclaimer.
35 * 2. Redistributions in binary form must reproduce the above copyright
36 * notice, this list of conditions and the following disclaimer in the
37 * documentation and/or other materials provided with the distribution.
38 *
39 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
40 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
41 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
43 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
44 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
45 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
46 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
47 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
48 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
49 * POSSIBILITY OF SUCH DAMAGE.
50 */
51
52/* OPENBSD ORIGINAL: lib/libc/stdlib/getopt_long.c */
53#include "openbsd-compat.h"
54
55#if !defined(HAVE_GETOPT)
56
57#if 0
58#include <err.h>
59#include <getopt.h>
60#endif
61#include <errno.h>
62#include <stdlib.h>
63#include <string.h>
64#include <stdarg.h>
65
66int opterr = 1; /* if error message should be printed */
67int optind = 1; /* index into parent argv vector */
68int optopt = '?'; /* character checked for validity */
69int optreset; /* reset getopt */
70char *optarg; /* argument associated with option */
71
72#define PRINT_ERROR ((opterr) && (*options != ':'))
73
74#define FLAG_PERMUTE 0x01 /* permute non-options to the end of argv */
75#define FLAG_ALLARGS 0x02 /* treat non-options as args to option "-1" */
76#define FLAG_LONGONLY 0x04 /* operate as getopt_long_only */
77
78/* return values */
79#define BADCH (int)'?'
80#define BADARG ((*options == ':') ? (int)':' : (int)'?')
81#define INORDER (int)1
82
83#define EMSG ""
84
85static int getopt_internal(int, char * const *, const char *,
86 const struct option *, int *, int);
87static int parse_long_options(char * const *, const char *,
88 const struct option *, int *, int);
89static int gcd(int, int);
90static void permute_args(int, int, int, char * const *);
91
92static char *place = EMSG; /* option letter processing */
93
94/* XXX: set optreset to 1 rather than these two */
95static int nonopt_start = -1; /* first non option argument (for permute) */
96static int nonopt_end = -1; /* first option after non options (for permute) */
97
98/* Error messages */
99static const char recargchar[] = "option requires an argument -- %c";
100static const char recargstring[] = "option requires an argument -- %s";
101static const char ambig[] = "ambiguous option -- %.*s";
102static const char noarg[] = "option doesn't take an argument -- %.*s";
103static const char illoptchar[] = "unknown option -- %c";
104static const char illoptstring[] = "unknown option -- %s";
105
106/*
107 * Compute the greatest common divisor of a and b.
108 */
109static int
110gcd(int a, int b)
111{
112 int c;
113
114 c = a % b;
115 while (c != 0) {
116 a = b;
117 b = c;
118 c = a % b;
119 }
120
121 return (b);
122}
123
124/*
125 * Exchange the block from nonopt_start to nonopt_end with the block
126 * from nonopt_end to opt_end (keeping the same order of arguments
127 * in each block).
128 */
129static void
130permute_args(int panonopt_start, int panonopt_end, int opt_end,
131 char * const *nargv)
132{
133 int cstart, cyclelen, i, j, ncycle, nnonopts, nopts, pos;
134 char *swap;
135
136 /*
137 * compute lengths of blocks and number and size of cycles
138 */
139 nnonopts = panonopt_end - panonopt_start;
140 nopts = opt_end - panonopt_end;
141 ncycle = gcd(nnonopts, nopts);
142 cyclelen = (opt_end - panonopt_start) / ncycle;
143
144 for (i = 0; i < ncycle; i++) {
145 cstart = panonopt_end+i;
146 pos = cstart;
147 for (j = 0; j < cyclelen; j++) {
148 if (pos >= panonopt_end)
149 pos -= nnonopts;
150 else
151 pos += nopts;
152 swap = nargv[pos];
153 /* LINTED const cast */
154 ((char **) nargv)[pos] = nargv[cstart];
155 /* LINTED const cast */
156 ((char **)nargv)[cstart] = swap;
157 }
158 }
159}
160
161/*
162 * parse_long_options --
163 * Parse long options in argc/argv argument vector.
164 * Returns -1 if short_too is set and the option does not match long_options.
165 */
166static int
167parse_long_options(char * const *nargv, const char *options,
168 const struct option *long_options, int *idx, int short_too)
169{
170 char *current_argv, *has_equal;
171 size_t current_argv_len;
172 int i, match;
173
174 current_argv = place;
175 match = -1;
176
177 optind++;
178
179 if ((has_equal = strchr(current_argv, '=')) != NULL) {
180 /* argument found (--option=arg) */
181 current_argv_len = has_equal - current_argv;
182 has_equal++;
183 } else
184 current_argv_len = strlen(current_argv);
185
186 for (i = 0; long_options[i].name; i++) {
187 /* find matching long option */
188 if (strncmp(current_argv, long_options[i].name,
189 current_argv_len))
190 continue;
191
192 if (strlen(long_options[i].name) == current_argv_len) {
193 /* exact match */
194 match = i;
195 break;
196 }
197 /*
198 * If this is a known short option, don't allow
199 * a partial match of a single character.
200 */
201 if (short_too && current_argv_len == 1)
202 continue;
203
204 if (match == -1) /* partial match */
205 match = i;
206 else {
207 /* ambiguous abbreviation */
208 if (PRINT_ERROR)
209 warnx(ambig, (int)current_argv_len,
210 current_argv);
211 optopt = 0;
212 return (BADCH);
213 }
214 }
215 if (match != -1) { /* option found */
216 if (long_options[match].has_arg == no_argument
217 && has_equal) {
218 if (PRINT_ERROR)
219 warnx(noarg, (int)current_argv_len,
220 current_argv);
221 /*
222 * XXX: GNU sets optopt to val regardless of flag
223 */
224 if (long_options[match].flag == NULL)
225 optopt = long_options[match].val;
226 else
227 optopt = 0;
228 return (BADARG);
229 }
230 if (long_options[match].has_arg == required_argument ||
231 long_options[match].has_arg == optional_argument) {
232 if (has_equal)
233 optarg = has_equal;
234 else if (long_options[match].has_arg ==
235 required_argument) {
236 /*
237 * optional argument doesn't use next nargv
238 */
239 optarg = nargv[optind++];
240 }
241 }
242 if ((long_options[match].has_arg == required_argument)
243 && (optarg == NULL)) {
244 /*
245 * Missing argument; leading ':' indicates no error
246 * should be generated.
247 */
248 if (PRINT_ERROR)
249 warnx(recargstring,
250 current_argv);
251 /*
252 * XXX: GNU sets optopt to val regardless of flag
253 */
254 if (long_options[match].flag == NULL)
255 optopt = long_options[match].val;
256 else
257 optopt = 0;
258 --optind;
259 return (BADARG);
260 }
261 } else { /* unknown option */
262 if (short_too) {
263 --optind;
264 return (-1);
265 }
266 if (PRINT_ERROR)
267 warnx(illoptstring, current_argv);
268 optopt = 0;
269 return (BADCH);
270 }
271 if (idx)
272 *idx = match;
273 if (long_options[match].flag) {
274 *long_options[match].flag = long_options[match].val;
275 return (0);
276 } else
277 return (long_options[match].val);
278}
279
280/*
281 * getopt_internal --
282 * Parse argc/argv argument vector. Called by user level routines.
283 */
284static int
285getopt_internal(int nargc, char * const *nargv, const char *options,
286 const struct option *long_options, int *idx, int flags)
287{
288 char *oli; /* option letter list index */
289 int optchar, short_too;
290 static int posixly_correct = -1;
291
292 if (options == NULL)
293 return (-1);
294
295 /*
296 * XXX Some GNU programs (like cvs) set optind to 0 instead of
297 * XXX using optreset. Work around this braindamage.
298 */
299 if (optind == 0)
300 optind = optreset = 1;
301
302 /*
303 * Disable GNU extensions if POSIXLY_CORRECT is set or options
304 * string begins with a '+'.
305 */
306 if (posixly_correct == -1 || optreset)
307 posixly_correct = (getenv("POSIXLY_CORRECT") != NULL);
308 if (*options == '-')
309 flags |= FLAG_ALLARGS;
310 else if (posixly_correct || *options == '+')
311 flags &= ~FLAG_PERMUTE;
312 if (*options == '+' || *options == '-')
313 options++;
314
315 optarg = NULL;
316 if (optreset)
317 nonopt_start = nonopt_end = -1;
318start:
319 if (optreset || !*place) { /* update scanning pointer */
320 optreset = 0;
321 if (optind >= nargc) { /* end of argument vector */
322 place = EMSG;
323 if (nonopt_end != -1) {
324 /* do permutation, if we have to */
325 permute_args(nonopt_start, nonopt_end,
326 optind, nargv);
327 optind -= nonopt_end - nonopt_start;
328 }
329 else if (nonopt_start != -1) {
330 /*
331 * If we skipped non-options, set optind
332 * to the first of them.
333 */
334 optind = nonopt_start;
335 }
336 nonopt_start = nonopt_end = -1;
337 return (-1);
338 }
339 if (*(place = nargv[optind]) != '-' ||
340 (place[1] == '\0' && strchr(options, '-') == NULL)) {
341 place = EMSG; /* found non-option */
342 if (flags & FLAG_ALLARGS) {
343 /*
344 * GNU extension:
345 * return non-option as argument to option 1
346 */
347 optarg = nargv[optind++];
348 return (INORDER);
349 }
350 if (!(flags & FLAG_PERMUTE)) {
351 /*
352 * If no permutation wanted, stop parsing
353 * at first non-option.
354 */
355 return (-1);
356 }
357 /* do permutation */
358 if (nonopt_start == -1)
359 nonopt_start = optind;
360 else if (nonopt_end != -1) {
361 permute_args(nonopt_start, nonopt_end,
362 optind, nargv);
363 nonopt_start = optind -
364 (nonopt_end - nonopt_start);
365 nonopt_end = -1;
366 }
367 optind++;
368 /* process next argument */
369 goto start;
370 }
371 if (nonopt_start != -1 && nonopt_end == -1)
372 nonopt_end = optind;
373
374 /*
375 * If we have "-" do nothing, if "--" we are done.
376 */
377 if (place[1] != '\0' && *++place == '-' && place[1] == '\0') {
378 optind++;
379 place = EMSG;
380 /*
381 * We found an option (--), so if we skipped
382 * non-options, we have to permute.
383 */
384 if (nonopt_end != -1) {
385 permute_args(nonopt_start, nonopt_end,
386 optind, nargv);
387 optind -= nonopt_end - nonopt_start;
388 }
389 nonopt_start = nonopt_end = -1;
390 return (-1);
391 }
392 }
393
394 /*
395 * Check long options if:
396 * 1) we were passed some
397 * 2) the arg is not just "-"
398 * 3) either the arg starts with -- we are getopt_long_only()
399 */
400 if (long_options != NULL && place != nargv[optind] &&
401 (*place == '-' || (flags & FLAG_LONGONLY))) {
402 short_too = 0;
403 if (*place == '-')
404 place++; /* --foo long option */
405 else if (*place != ':' && strchr(options, *place) != NULL)
406 short_too = 1; /* could be short option too */
407
408 optchar = parse_long_options(nargv, options, long_options,
409 idx, short_too);
410 if (optchar != -1) {
411 place = EMSG;
412 return (optchar);
413 }
414 }
415
416 if ((optchar = (int)*place++) == (int)':' ||
417 (optchar == (int)'-' && *place != '\0') ||
418 (oli = strchr(options, optchar)) == NULL) {
419 /*
420 * If the user specified "-" and '-' isn't listed in
421 * options, return -1 (non-option) as per POSIX.
422 * Otherwise, it is an unknown option character (or ':').
423 */
424 if (optchar == (int)'-' && *place == '\0')
425 return (-1);
426 if (!*place)
427 ++optind;
428 if (PRINT_ERROR)
429 warnx(illoptchar, optchar);
430 optopt = optchar;
431 return (BADCH);
432 }
433 if (long_options != NULL && optchar == 'W' && oli[1] == ';') {
434 /* -W long-option */
435 if (*place) /* no space */
436 /* NOTHING */;
437 else if (++optind >= nargc) { /* no arg */
438 place = EMSG;
439 if (PRINT_ERROR)
440 warnx(recargchar, optchar);
441 optopt = optchar;
442 return (BADARG);
443 } else /* white space */
444 place = nargv[optind];
445 optchar = parse_long_options(nargv, options, long_options,
446 idx, 0);
447 place = EMSG;
448 return (optchar);
449 }
450 if (*++oli != ':') { /* doesn't take argument */
451 if (!*place)
452 ++optind;
453 } else { /* takes (optional) argument */
454 optarg = NULL;
455 if (*place) /* no white space */
456 optarg = place;
457 else if (oli[1] != ':') { /* arg not optional */
458 if (++optind >= nargc) { /* no arg */
459 place = EMSG;
460 if (PRINT_ERROR)
461 warnx(recargchar, optchar);
462 optopt = optchar;
463 return (BADARG);
464 } else
465 optarg = nargv[optind];
466 }
467 place = EMSG;
468 ++optind;
469 }
470 /* dump back option letter */
471 return (optchar);
472}
473
474/*
475 * getopt --
476 * Parse argc/argv argument vector.
477 *
478 * [eventually this will replace the BSD getopt]
479 */
480int
481getopt(int nargc, char * const *nargv, const char *options)
482{
483
484 /*
485 * We don't pass FLAG_PERMUTE to getopt_internal() since
486 * the BSD getopt(3) (unlike GNU) has never done this.
487 *
488 * Furthermore, since many privileged programs call getopt()
489 * before dropping privileges it makes sense to keep things
490 * as simple (and bug-free) as possible.
491 */
492 return (getopt_internal(nargc, nargv, options, NULL, NULL, 0));
493}
494
495#if 0
496/*
497 * getopt_long --
498 * Parse argc/argv argument vector.
499 */
500int
501getopt_long(int nargc, char * const *nargv, const char *options,
502 const struct option *long_options, int *idx)
503{
504
505 return (getopt_internal(nargc, nargv, options, long_options, idx,
506 FLAG_PERMUTE));
507}
508
509/*
510 * getopt_long_only --
511 * Parse argc/argv argument vector.
512 */
513int
514getopt_long_only(int nargc, char * const *nargv, const char *options,
515 const struct option *long_options, int *idx)
516{
517
518 return (getopt_internal(nargc, nargv, options, long_options, idx,
519 FLAG_PERMUTE|FLAG_LONGONLY));
520}
521#endif
522
523#endif /* !defined(HAVE_GETOPT) */
diff --git a/openbsd-compat/openbsd-compat.h b/openbsd-compat/openbsd-compat.h
new file mode 100644
index 0000000..d1d8652
--- /dev/null
+++ b/openbsd-compat/openbsd-compat.h
@@ -0,0 +1,87 @@
1/*
2 * Copyright (c) 2018 Yubico AB. All rights reserved.
3 * Use of this source code is governed by a BSD-style
4 * license that can be found in the LICENSE file.
5 */
6
7#ifndef _OPENBSD_COMPAT_H
8#define _OPENBSD_COMPAT_H
9
10#if defined(_MSC_VER)
11#include "types.h"
12#endif
13
14#if defined(HAVE_ENDIAN_H)
15#include <endian.h>
16#endif
17
18#if defined(__APPLE__) && !defined(HAVE_ENDIAN_H)
19#include <libkern/OSByteOrder.h>
20#define be16toh(x) OSSwapBigToHostInt16((x))
21#define be32toh(x) OSSwapBigToHostInt32((x))
22#endif /* __APPLE__ && !HAVE_ENDIAN_H */
23
24#if defined(_WIN32) && !defined(HAVE_ENDIAN_H)
25#include <winsock2.h>
26#if !defined(_MSC_VER)
27#include <sys/param.h>
28#endif
29#define be16toh(x) ntohs((x))
30#define be32toh(x) ntohl((x))
31#endif /* _WIN32 && !HAVE_ENDIAN_H */
32
33#include <stdlib.h>
34
35#if !defined(HAVE_STRLCAT)
36size_t strlcat(char *, const char *, size_t);
37#endif
38
39#if !defined(HAVE_STRLCPY)
40size_t strlcpy(char *, const char *, size_t);
41#endif
42
43#if !defined(HAVE_RECALLOCARRAY)
44void *recallocarray(void *, size_t, size_t, size_t);
45#endif
46
47#if !defined(HAVE_EXPLICIT_BZERO)
48void explicit_bzero(void *, size_t);
49#endif
50
51#if !defined(HAVE_GETPAGESIZE)
52int getpagesize(void);
53#endif
54
55#if !defined(HAVE_TIMINGSAFE_BCMP)
56int timingsafe_bcmp(const void *, const void *, size_t);
57#endif
58
59#if !defined(HAVE_READPASSPHRASE)
60#include "readpassphrase.h"
61#else
62#include <readpassphrase.h>
63#endif
64
65#if OPENSSL_VERSION_NUMBER < 0x10100000L
66#define EVP_PKEY_get0_EC_KEY(x) ((x)->pkey.ec)
67#define EVP_PKEY_get0_RSA(x) ((x)->pkey.rsa)
68#endif
69
70#if !defined(HAVE_ERR_H)
71#include "err.h"
72#else
73#include <err.h>
74#endif
75
76#if !defined(HAVE_GETOPT)
77#include "getopt.h"
78#else
79#include <unistd.h>
80#endif
81
82#if !defined(HAVE_GETLINE)
83#include <stdio.h>
84ssize_t getline(char **, size_t *, FILE *);
85#endif
86
87#endif /* !_OPENBSD_COMPAT_H */
diff --git a/openbsd-compat/posix_win.c b/openbsd-compat/posix_win.c
new file mode 100644
index 0000000..eac67c2
--- /dev/null
+++ b/openbsd-compat/posix_win.c
@@ -0,0 +1,61 @@
1/*
2 * Public domain
3 *
4 * File IO compatibility shims
5 * Brent Cook <bcook@openbsd.org>
6 */
7
8#define NO_REDEF_POSIX_FUNCTIONS
9
10#include <windows.h>
11
12#include <errno.h>
13#include <io.h>
14
15#include "posix_win.h"
16
17int
18posix_open(const char *path, ...)
19{
20 va_list ap;
21 int mode = 0;
22 int flags;
23
24 va_start(ap, path);
25 flags = va_arg(ap, int);
26 if (flags & O_CREAT)
27 mode = va_arg(ap, int);
28 va_end(ap);
29
30 flags |= O_BINARY | O_NOINHERIT;
31
32 return (open(path, flags, mode));
33}
34
35int
36posix_close(int fd)
37{
38 return (close(fd));
39}
40
41ssize_t
42posix_read(int fd, void *buf, size_t count)
43{
44 if (count > INT_MAX) {
45 errno = EINVAL;
46 return (-1);
47 }
48
49 return (read(fd, buf, (unsigned int)count));
50}
51
52ssize_t
53posix_write(int fd, const void *buf, size_t count)
54{
55 if (count > INT_MAX) {
56 errno = EINVAL;
57 return (-1);
58 }
59
60 return (write(fd, buf, (unsigned int)count));
61}
diff --git a/openbsd-compat/posix_win.h b/openbsd-compat/posix_win.h
new file mode 100644
index 0000000..a1e0888
--- /dev/null
+++ b/openbsd-compat/posix_win.h
@@ -0,0 +1,47 @@
1/*
2 * Public domain
3 *
4 * BSD socket emulation code for Winsock2
5 * Brent Cook <bcook@openbsd.org>
6 */
7
8#ifndef _COMPAT_POSIX_WIN_H
9#define _COMPAT_POSIX_WIN_H
10
11#ifdef _WIN32
12
13#include <windows.h>
14
15#include <errno.h>
16#include <stdarg.h>
17#include <stdint.h>
18#include <stdio.h>
19#include <stdlib.h>
20#include <string.h>
21
22#if _MSC_VER >= 1900
23#include <../ucrt/fcntl.h>
24#else
25#include <../include/fcntl.h>
26#endif
27
28#include "types.h"
29
30int posix_open(const char *path, ...);
31
32int posix_close(int fd);
33
34ssize_t posix_read(int fd, void *buf, size_t count);
35
36ssize_t posix_write(int fd, const void *buf, size_t count);
37
38#ifndef NO_REDEF_POSIX_FUNCTIONS
39#define open(path, ...) posix_open(path, __VA_ARGS__)
40#define close(fd) posix_close(fd)
41#define read(fd, buf, count) posix_read(fd, buf, count)
42#define write(fd, buf, count) posix_write(fd, buf, count)
43#endif
44
45#endif /* _WIN32 */
46
47#endif /* !_COMPAT_POSIX_WIN_H */
diff --git a/openbsd-compat/readpassphrase.c b/openbsd-compat/readpassphrase.c
new file mode 100644
index 0000000..dfb3065
--- /dev/null
+++ b/openbsd-compat/readpassphrase.c
@@ -0,0 +1,214 @@
1/* $OpenBSD: readpassphrase.c,v 1.26 2016/10/18 12:47:18 millert Exp $ */
2
3/*
4 * Copyright (c) 2000-2002, 2007, 2010
5 * Todd C. Miller <Todd.Miller@courtesan.com>
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 * Sponsored in part by the Defense Advanced Research Projects
20 * Agency (DARPA) and Air Force Research Laboratory, Air Force
21 * Materiel Command, USAF, under agreement number F39502-99-1-0512.
22 */
23
24/* OPENBSD ORIGINAL: lib/libc/gen/readpassphrase.c */
25
26#include "openbsd-compat.h"
27
28#ifndef HAVE_READPASSPHRASE
29
30#include <termios.h>
31#include <signal.h>
32#include <ctype.h>
33#include <fcntl.h>
34#include <errno.h>
35#include <string.h>
36#ifdef HAVE_UNISTD_H
37#include <unistd.h>
38#endif
39#include <paths.h>
40
41#ifndef _PATH_TTY
42# define _PATH_TTY "/dev/tty"
43#endif
44
45#ifndef TCSASOFT
46/* If we don't have TCSASOFT define it so that ORing it it below is a no-op. */
47# define TCSASOFT 0
48#endif
49
50/* SunOS 4.x which lacks _POSIX_VDISABLE, but has VDISABLE */
51#if !defined(_POSIX_VDISABLE) && defined(VDISABLE)
52# define _POSIX_VDISABLE VDISABLE
53#endif
54
55static volatile sig_atomic_t signo[_NSIG];
56
57static void handler(int);
58
59char *
60readpassphrase(const char *prompt, char *buf, size_t bufsiz, int flags)
61{
62 ssize_t nr;
63 int input, output, save_errno, i, need_restart;
64 char ch, *p, *end;
65 struct termios term, oterm;
66 struct sigaction sa, savealrm, saveint, savehup, savequit, saveterm;
67 struct sigaction savetstp, savettin, savettou, savepipe;
68
69 /* I suppose we could alloc on demand in this case (XXX). */
70 if (bufsiz == 0) {
71 errno = EINVAL;
72 return(NULL);
73 }
74
75restart:
76 for (i = 0; i < _NSIG; i++)
77 signo[i] = 0;
78 need_restart = 0;
79 /*
80 * Read and write to /dev/tty if available. If not, read from
81 * stdin and write to stderr unless a tty is required.
82 */
83 if ((flags & RPP_STDIN) ||
84 (input = output = open(_PATH_TTY, O_RDWR)) == -1) {
85 if (flags & RPP_REQUIRE_TTY) {
86 errno = ENOTTY;
87 return(NULL);
88 }
89 input = STDIN_FILENO;
90 output = STDERR_FILENO;
91 }
92
93 /*
94 * Turn off echo if possible.
95 * If we are using a tty but are not the foreground pgrp this will
96 * generate SIGTTOU, so do it *before* installing the signal handlers.
97 */
98 if (input != STDIN_FILENO && tcgetattr(input, &oterm) == 0) {
99 memcpy(&term, &oterm, sizeof(term));
100 if (!(flags & RPP_ECHO_ON))
101 term.c_lflag &= ~(ECHO | ECHONL);
102#ifdef VSTATUS
103 if (term.c_cc[VSTATUS] != _POSIX_VDISABLE)
104 term.c_cc[VSTATUS] = _POSIX_VDISABLE;
105#endif
106 (void)tcsetattr(input, TCSAFLUSH|TCSASOFT, &term);
107 } else {
108 memset(&term, 0, sizeof(term));
109 term.c_lflag |= ECHO;
110 memset(&oterm, 0, sizeof(oterm));
111 oterm.c_lflag |= ECHO;
112 }
113
114 /*
115 * Catch signals that would otherwise cause the user to end
116 * up with echo turned off in the shell. Don't worry about
117 * things like SIGXCPU and SIGVTALRM for now.
118 */
119 sigemptyset(&sa.sa_mask);
120 sa.sa_flags = 0; /* don't restart system calls */
121 sa.sa_handler = handler;
122 (void)sigaction(SIGALRM, &sa, &savealrm);
123 (void)sigaction(SIGHUP, &sa, &savehup);
124 (void)sigaction(SIGINT, &sa, &saveint);
125 (void)sigaction(SIGPIPE, &sa, &savepipe);
126 (void)sigaction(SIGQUIT, &sa, &savequit);
127 (void)sigaction(SIGTERM, &sa, &saveterm);
128 (void)sigaction(SIGTSTP, &sa, &savetstp);
129 (void)sigaction(SIGTTIN, &sa, &savettin);
130 (void)sigaction(SIGTTOU, &sa, &savettou);
131
132 if (!(flags & RPP_STDIN))
133 (void)write(output, prompt, strlen(prompt));
134 end = buf + bufsiz - 1;
135 p = buf;
136 while ((nr = read(input, &ch, 1)) == 1 && ch != '\n' && ch != '\r') {
137 if (p < end) {
138 if ((flags & RPP_SEVENBIT))
139 ch &= 0x7f;
140 if (isalpha((unsigned char)ch)) {
141 if ((flags & RPP_FORCELOWER))
142 ch = (char)tolower((unsigned char)ch);
143 if ((flags & RPP_FORCEUPPER))
144 ch = (char)toupper((unsigned char)ch);
145 }
146 *p++ = ch;
147 }
148 }
149 *p = '\0';
150 save_errno = errno;
151 if (!(term.c_lflag & ECHO))
152 (void)write(output, "\n", 1);
153
154 /* Restore old terminal settings and signals. */
155 if (memcmp(&term, &oterm, sizeof(term)) != 0) {
156 const int sigttou = signo[SIGTTOU];
157
158 /* Ignore SIGTTOU generated when we are not the fg pgrp. */
159 while (tcsetattr(input, TCSAFLUSH|TCSASOFT, &oterm) == -1 &&
160 errno == EINTR && !signo[SIGTTOU])
161 continue;
162 signo[SIGTTOU] = sigttou;
163 }
164 (void)sigaction(SIGALRM, &savealrm, NULL);
165 (void)sigaction(SIGHUP, &savehup, NULL);
166 (void)sigaction(SIGINT, &saveint, NULL);
167 (void)sigaction(SIGQUIT, &savequit, NULL);
168 (void)sigaction(SIGPIPE, &savepipe, NULL);
169 (void)sigaction(SIGTERM, &saveterm, NULL);
170 (void)sigaction(SIGTSTP, &savetstp, NULL);
171 (void)sigaction(SIGTTIN, &savettin, NULL);
172 (void)sigaction(SIGTTOU, &savettou, NULL);
173 if (input != STDIN_FILENO)
174 (void)close(input);
175
176 /*
177 * If we were interrupted by a signal, resend it to ourselves
178 * now that we have restored the signal handlers.
179 */
180 for (i = 0; i < _NSIG; i++) {
181 if (signo[i]) {
182 kill(getpid(), i);
183 switch (i) {
184 case SIGTSTP:
185 case SIGTTIN:
186 case SIGTTOU:
187 need_restart = 1;
188 }
189 }
190 }
191 if (need_restart)
192 goto restart;
193
194 if (save_errno)
195 errno = save_errno;
196 return(nr == -1 ? NULL : buf);
197}
198
199#if 0
200char *
201getpass(const char *prompt)
202{
203 static char buf[_PASSWORD_LEN + 1];
204
205 return(readpassphrase(prompt, buf, sizeof(buf), RPP_ECHO_OFF));
206}
207#endif
208
209static void handler(int s)
210{
211
212 signo[s] = 1;
213}
214#endif /* HAVE_READPASSPHRASE */
diff --git a/openbsd-compat/readpassphrase.h b/openbsd-compat/readpassphrase.h
new file mode 100644
index 0000000..0c4a59e
--- /dev/null
+++ b/openbsd-compat/readpassphrase.h
@@ -0,0 +1,42 @@
1/* $OpenBSD: readpassphrase.h,v 1.5 2003/06/17 21:56:23 millert Exp $ */
2
3/*
4 * Copyright (c) 2000, 2002 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: include/readpassphrase.h */
24
25#ifndef _READPASSPHRASE_H_
26#define _READPASSPHRASE_H_
27
28#ifndef HAVE_READPASSPHRASE
29
30#define RPP_ECHO_OFF 0x00 /* Turn off echo (default). */
31#define RPP_ECHO_ON 0x01 /* Leave echo on. */
32#define RPP_REQUIRE_TTY 0x02 /* Fail if there is no tty. */
33#define RPP_FORCELOWER 0x04 /* Force input to lower case. */
34#define RPP_FORCEUPPER 0x08 /* Force input to upper case. */
35#define RPP_SEVENBIT 0x10 /* Strip the high bit from input. */
36#define RPP_STDIN 0x20 /* Read from stdin, not /dev/tty */
37
38char * readpassphrase(const char *, char *, size_t, int);
39
40#endif /* HAVE_READPASSPHRASE */
41
42#endif /* !_READPASSPHRASE_H_ */
diff --git a/openbsd-compat/readpassphrase_win32.c b/openbsd-compat/readpassphrase_win32.c
new file mode 100644
index 0000000..f3079e4
--- /dev/null
+++ b/openbsd-compat/readpassphrase_win32.c
@@ -0,0 +1,131 @@
1/*
2* Author: Manoj Ampalam <manoj.ampalam@microsoft.com>
3*
4* Author: Bryan Berns <berns@uwalumni.com>
5* Modified group detection use s4u token information
6*
7* Copyright(c) 2016 Microsoft Corp.
8* All rights reserved
9*
10* Misc Unix POSIX routine implementations for Windows
11*
12* Redistribution and use in source and binary forms, with or without
13* modification, are permitted provided that the following conditions
14* are met :
15*
16* 1. Redistributions of source code must retain the above copyright
17* notice, this list of conditions and the following disclaimer.
18* 2. Redistributions in binary form must reproduce the above copyright
19* notice, this list of conditions and the following disclaimer in the
20* documentation and / or other materials provided with the distribution.
21*
22* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES(INCLUDING, BUT
27* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32*/
33
34#define UMDF_USING_NTSTATUS
35#define SECURITY_WIN32
36#include <windows.h>
37#include <stdio.h>
38#include <time.h>
39#include <shlwapi.h>
40#include <conio.h>
41#include <lm.h>
42#include <sddl.h>
43#include <aclapi.h>
44#include <ntsecapi.h>
45#include <security.h>
46#include <ntstatus.h>
47#include <wchar.h>
48
49#include "openbsd-compat.h"
50
51#ifndef HAVE_READPASSPHRASE
52
53/*on error returns NULL and sets errno*/
54static wchar_t *
55utf8_to_utf16(const char *utf8)
56{
57 int needed = 0;
58 wchar_t* utf16 = NULL;
59 if ((needed = MultiByteToWideChar(CP_UTF8, 0, utf8, -1, NULL, 0)) == 0 ||
60 (utf16 = malloc(needed * sizeof(wchar_t))) == NULL ||
61 MultiByteToWideChar(CP_UTF8, 0, utf8, -1, utf16, needed) == 0) {
62 /* debug3("failed to convert utf8 payload:%s error:%d", utf8, GetLastError()); */
63 errno = ENOMEM;
64 return NULL;
65 }
66
67 return utf16;
68}
69
70char *
71readpassphrase(const char *prompt, char *outBuf, size_t outBufLen, int flags)
72{
73 size_t current_index = 0;
74 char ch;
75 wchar_t* wtmp = NULL;
76
77 if (outBufLen == 0) {
78 errno = EINVAL;
79 return NULL;
80 }
81
82 while (_kbhit()) _getch();
83
84 wtmp = utf8_to_utf16(prompt);
85 if (wtmp == NULL)
86 errx(1, "unable to alloc memory");
87
88 _cputws(wtmp);
89 free(wtmp);
90
91 while (current_index < outBufLen - 1) {
92 ch = (char)_getch();
93
94 if (ch == '\r') {
95 if (_kbhit()) _getch(); /* read linefeed if its there */
96 break;
97 } else if (ch == '\n') {
98 break;
99 } else if (ch == '\b') { /* backspace */
100 if (current_index > 0) {
101 if (flags & RPP_ECHO_ON)
102 printf_s("%c \b", ch);
103
104 current_index--; /* overwrite last character */
105 }
106 } else if (ch == '\003') { /* exit on Ctrl+C */
107 errx(1, "");
108 } else {
109 if (flags & RPP_SEVENBIT)
110 ch &= 0x7f;
111
112 if (isalpha((unsigned char)ch)) {
113 if(flags & RPP_FORCELOWER)
114 ch = (char)tolower((unsigned char)ch);
115 if(flags & RPP_FORCEUPPER)
116 ch = (char)toupper((unsigned char)ch);
117 }
118
119 outBuf[current_index++] = ch;
120 if(flags & RPP_ECHO_ON)
121 printf_s("%c", ch);
122 }
123 }
124
125 outBuf[current_index] = '\0';
126 _cputs("\n");
127
128 return outBuf;
129}
130
131#endif /* HAVE_READPASSPHRASE */
diff --git a/openbsd-compat/recallocarray.c b/openbsd-compat/recallocarray.c
new file mode 100644
index 0000000..5d2f8d9
--- /dev/null
+++ b/openbsd-compat/recallocarray.c
@@ -0,0 +1,91 @@
1/* $OpenBSD: recallocarray.c,v 1.1 2017/03/06 18:44:21 otto Exp $ */
2/*
3 * Copyright (c) 2008, 2017 Otto Moerbeek <otto@drijf.net>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17
18/* OPENBSD ORIGINAL: lib/libc/stdlib/recallocarray.c */
19
20#include "openbsd-compat.h"
21
22#if !defined(HAVE_RECALLOCARRAY)
23
24#include <errno.h>
25#include <stdlib.h>
26#include <stdint.h>
27#include <string.h>
28#ifdef HAVE_UNISTD_H
29#include <unistd.h>
30#endif
31
32/*
33 * This is sqrt(SIZE_MAX+1), as s1*s2 <= SIZE_MAX
34 * if both s1 < MUL_NO_OVERFLOW and s2 < MUL_NO_OVERFLOW
35 */
36#define MUL_NO_OVERFLOW ((size_t)1 << (sizeof(size_t) * 4))
37
38void *
39recallocarray(void *ptr, size_t oldnmemb, size_t newnmemb, size_t size)
40{
41 size_t oldsize, newsize;
42 void *newptr;
43
44 if (ptr == NULL)
45 return calloc(newnmemb, size);
46
47 if ((newnmemb >= MUL_NO_OVERFLOW || size >= MUL_NO_OVERFLOW) &&
48 newnmemb > 0 && SIZE_MAX / newnmemb < size) {
49 errno = ENOMEM;
50 return NULL;
51 }
52 newsize = newnmemb * size;
53
54 if ((oldnmemb >= MUL_NO_OVERFLOW || size >= MUL_NO_OVERFLOW) &&
55 oldnmemb > 0 && SIZE_MAX / oldnmemb < size) {
56 errno = EINVAL;
57 return NULL;
58 }
59 oldsize = oldnmemb * size;
60
61 /*
62 * Don't bother too much if we're shrinking just a bit,
63 * we do not shrink for series of small steps, oh well.
64 */
65 if (newsize <= oldsize) {
66 size_t d = oldsize - newsize;
67
68 if (d < oldsize / 2 && d < (size_t)getpagesize()) {
69 memset((char *)ptr + newsize, 0, d);
70 return ptr;
71 }
72 }
73
74 newptr = malloc(newsize);
75 if (newptr == NULL)
76 return NULL;
77
78 if (newsize > oldsize) {
79 memcpy(newptr, ptr, oldsize);
80 memset((char *)newptr + oldsize, 0, newsize - oldsize);
81 } else
82 memcpy(newptr, ptr, newsize);
83
84 explicit_bzero(ptr, oldsize);
85 free(ptr);
86
87 return newptr;
88}
89/* DEF_WEAK(recallocarray); */
90
91#endif /* !defined(HAVE_RECALLOCARRAY) */
diff --git a/openbsd-compat/strlcat.c b/openbsd-compat/strlcat.c
new file mode 100644
index 0000000..44470de
--- /dev/null
+++ b/openbsd-compat/strlcat.c
@@ -0,0 +1,63 @@
1/* $OpenBSD: strlcat.c,v 1.13 2005/08/08 08:05:37 espie Exp $ */
2
3/*
4 * Copyright (c) 1998 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
19/* OPENBSD ORIGINAL: lib/libc/string/strlcat.c */
20
21#include "openbsd-compat.h"
22
23#if !defined(HAVE_STRLCAT)
24
25#include <sys/types.h>
26#include <string.h>
27
28/*
29 * Appends src to string dst of size siz (unlike strncat, siz is the
30 * full size of dst, not space left). At most siz-1 characters
31 * will be copied. Always NUL terminates (unless siz <= strlen(dst)).
32 * Returns strlen(src) + MIN(siz, strlen(initial dst)).
33 * If retval >= siz, truncation occurred.
34 */
35size_t
36strlcat(char *dst, const char *src, size_t siz)
37{
38 char *d = dst;
39 const char *s = src;
40 size_t n = siz;
41 size_t dlen;
42
43 /* Find the end of dst and adjust bytes left but don't go past end */
44 while (n-- != 0 && *d != '\0')
45 d++;
46 dlen = d - dst;
47 n = siz - dlen;
48
49 if (n == 0)
50 return(dlen + strlen(s));
51 while (*s != '\0') {
52 if (n != 1) {
53 *d++ = *s;
54 n--;
55 }
56 s++;
57 }
58 *d = '\0';
59
60 return(dlen + (s - src)); /* count does not include NUL */
61}
62
63#endif /* !defined(HAVE_STRLCAT) */
diff --git a/openbsd-compat/strlcpy.c b/openbsd-compat/strlcpy.c
new file mode 100644
index 0000000..a8b18ea
--- /dev/null
+++ b/openbsd-compat/strlcpy.c
@@ -0,0 +1,59 @@
1/* $OpenBSD: strlcpy.c,v 1.11 2006/05/05 15:27:38 millert Exp $ */
2
3/*
4 * Copyright (c) 1998 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
19/* OPENBSD ORIGINAL: lib/libc/string/strlcpy.c */
20
21#include "openbsd-compat.h"
22
23#if !defined(HAVE_STRLCPY)
24
25#include <sys/types.h>
26#include <string.h>
27
28/*
29 * Copy src to string dst of size siz. At most siz-1 characters
30 * will be copied. Always NUL terminates (unless siz == 0).
31 * Returns strlen(src); if retval >= siz, truncation occurred.
32 */
33size_t
34strlcpy(char *dst, const char *src, size_t siz)
35{
36 char *d = dst;
37 const char *s = src;
38 size_t n = siz;
39
40 /* Copy as many bytes as will fit */
41 if (n != 0) {
42 while (--n != 0) {
43 if ((*d++ = *s++) == '\0')
44 break;
45 }
46 }
47
48 /* Not enough room in dst, add NUL and traverse rest of src */
49 if (n == 0) {
50 if (siz != 0)
51 *d = '\0'; /* NUL-terminate dst */
52 while (*s++)
53 ;
54 }
55
56 return(s - src - 1); /* count does not include NUL */
57}
58
59#endif /* !defined(HAVE_STRLCPY) */
diff --git a/openbsd-compat/timingsafe_bcmp.c b/openbsd-compat/timingsafe_bcmp.c
new file mode 100644
index 0000000..3f7b9e5
--- /dev/null
+++ b/openbsd-compat/timingsafe_bcmp.c
@@ -0,0 +1,35 @@
1/* $OpenBSD: timingsafe_bcmp.c,v 1.1 2010/09/24 13:33:00 matthew Exp $ */
2/*
3 * Copyright (c) 2010 Damien Miller. All rights reserved.
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17
18/* OPENBSD ORIGINAL: lib/libc/string/timingsafe_bcmp.c */
19
20#include "openbsd-compat.h"
21
22#if !defined(HAVE_TIMINGSAFE_BCMP)
23
24int
25timingsafe_bcmp(const void *b1, const void *b2, size_t n)
26{
27 const unsigned char *p1 = b1, *p2 = b2;
28 int ret = 0;
29
30 for (; n > 0; n--)
31 ret |= *p1++ ^ *p2++;
32 return (ret != 0);
33}
34
35#endif /* !defined(HAVE_TIMINGSAFE_BCMP) */
diff --git a/openbsd-compat/types.h b/openbsd-compat/types.h
new file mode 100644
index 0000000..cc1da66
--- /dev/null
+++ b/openbsd-compat/types.h
@@ -0,0 +1,71 @@
1/*
2 * Public domain
3 * sys/types.h compatibility shim
4 */
5
6#ifdef _MSC_VER
7#if _MSC_VER >= 1900
8#include <../ucrt/sys/types.h>
9#else
10#include <../include/sys/types.h>
11#endif
12#endif
13
14#ifndef _COMPAT_TYPES_H
15#define _COMPAT_TYPES_H
16
17#include <stdint.h>
18
19#ifdef __MINGW32__
20#include <_bsd_types.h>
21typedef uint32_t in_addr_t;
22typedef uint32_t uid_t;
23#endif
24
25#ifdef _MSC_VER
26typedef unsigned char u_char;
27typedef unsigned short u_short;
28typedef unsigned int u_int;
29typedef uint32_t in_addr_t;
30typedef uint32_t mode_t;
31typedef uint32_t uid_t;
32
33#include <basetsd.h>
34typedef SSIZE_T ssize_t;
35
36#ifndef SSIZE_MAX
37#ifdef _WIN64
38#define SSIZE_MAX _I64_MAX
39#else
40#define SSIZE_MAX INT_MAX
41#endif
42#endif
43
44#endif
45
46#if !defined(HAVE_ATTRIBUTE__BOUNDED__) && !defined(__bounded__)
47# define __bounded__(x, y, z)
48#endif
49
50#ifdef _WIN32
51#define __warn_references(sym,msg)
52#else
53
54#ifndef __warn_references
55
56#ifndef __STRING
57#define __STRING(x) #x
58#endif
59
60#if defined(__GNUC__) && defined (HAS_GNU_WARNING_LONG)
61#define __warn_references(sym,msg) \
62 __asm__(".section .gnu.warning." __STRING(sym) \
63 "\n\t.ascii \"" msg "\"\n\t.text");
64#else
65#define __warn_references(sym,msg)
66#endif
67
68#endif /* __warn_references */
69#endif /* _WIN32 */
70
71#endif /* !_COMPAT_TYPES_H */