summaryrefslogtreecommitdiff
path: root/openbsd-compat
diff options
context:
space:
mode:
Diffstat (limited to 'openbsd-compat')
-rw-r--r--openbsd-compat/Makefile.in7
-rw-r--r--openbsd-compat/bsd-misc.c10
-rw-r--r--openbsd-compat/bsd-misc.h31
-rw-r--r--openbsd-compat/bsd-openpty.c8
-rw-r--r--openbsd-compat/bsd-signal.c28
-rw-r--r--openbsd-compat/bsd-signal.h7
-rw-r--r--openbsd-compat/bsd-statvfs.c10
-rw-r--r--openbsd-compat/fnmatch.c495
-rw-r--r--openbsd-compat/fnmatch.h66
-rw-r--r--openbsd-compat/glob.c149
-rw-r--r--openbsd-compat/glob.h9
-rw-r--r--openbsd-compat/openbsd-compat.h8
-rw-r--r--openbsd-compat/openssl-compat.h6
-rw-r--r--openbsd-compat/port-aix.c4
-rw-r--r--openbsd-compat/port-net.c8
-rw-r--r--openbsd-compat/rmd160.c378
-rw-r--r--openbsd-compat/rmd160.h61
-rw-r--r--openbsd-compat/sha2.c2
18 files changed, 709 insertions, 578 deletions
diff --git a/openbsd-compat/Makefile.in b/openbsd-compat/Makefile.in
index 1162dc550..3eb188f0b 100644
--- a/openbsd-compat/Makefile.in
+++ b/openbsd-compat/Makefile.in
@@ -7,12 +7,15 @@ VPATH=@srcdir@
7CC=@CC@ 7CC=@CC@
8LD=@LD@ 8LD=@LD@
9CFLAGS=@CFLAGS@ 9CFLAGS=@CFLAGS@
10CFLAGS_NOPIE=@CFLAGS_NOPIE@
10CPPFLAGS=-I. -I.. -I$(srcdir) -I$(srcdir)/.. @CPPFLAGS@ @DEFS@ 11CPPFLAGS=-I. -I.. -I$(srcdir) -I$(srcdir)/.. @CPPFLAGS@ @DEFS@
12PICFLAG=@PICFLAG@
11LIBS=@LIBS@ 13LIBS=@LIBS@
12AR=@AR@ 14AR=@AR@
13RANLIB=@RANLIB@ 15RANLIB=@RANLIB@
14INSTALL=@INSTALL@ 16INSTALL=@INSTALL@
15LDFLAGS=-L. @LDFLAGS@ 17LDFLAGS=-L. @LDFLAGS@
18LDFLAGS_NOPIE=-L. -Lopenbsd-compat/ @LDFLAGS_NOPIE@
16 19
17OPENBSD=base64.o \ 20OPENBSD=base64.o \
18 basename.o \ 21 basename.o \
@@ -25,6 +28,7 @@ OPENBSD=base64.o \
25 explicit_bzero.o \ 28 explicit_bzero.o \
26 fmt_scaled.o \ 29 fmt_scaled.o \
27 freezero.o \ 30 freezero.o \
31 fnmatch.o \
28 getcwd.o \ 32 getcwd.o \
29 getgrouplist.o \ 33 getgrouplist.o \
30 getopt_long.o \ 34 getopt_long.o \
@@ -40,7 +44,6 @@ OPENBSD=base64.o \
40 readpassphrase.o \ 44 readpassphrase.o \
41 reallocarray.o \ 45 reallocarray.o \
42 recallocarray.o \ 46 recallocarray.o \
43 rmd160.o \
44 rresvport.o \ 47 rresvport.o \
45 setenv.o \ 48 setenv.o \
46 setproctitle.o \ 49 setproctitle.o \
@@ -96,7 +99,7 @@ PORTS= port-aix.o \
96 port-uw.o 99 port-uw.o
97 100
98.c.o: 101.c.o:
99 $(CC) $(CFLAGS) $(CPPFLAGS) -c $< 102 $(CC) $(CFLAGS_NOPIE) $(PICFLAG) $(CPPFLAGS) -c $<
100 103
101all: libopenbsd-compat.a 104all: libopenbsd-compat.a
102 105
diff --git a/openbsd-compat/bsd-misc.c b/openbsd-compat/bsd-misc.c
index 7a26ee40c..829e0c075 100644
--- a/openbsd-compat/bsd-misc.c
+++ b/openbsd-compat/bsd-misc.c
@@ -426,3 +426,13 @@ int _ssh_compat_fflush(FILE *f)
426 return fflush(f); 426 return fflush(f);
427} 427}
428#endif 428#endif
429
430#ifndef HAVE_LOCALTIME_R
431struct tm *
432localtime_r(const time_t *timep, struct tm *result)
433{
434 struct tm *tm = localtime(timep);
435 *result = *tm;
436 return result;
437}
438#endif
diff --git a/openbsd-compat/bsd-misc.h b/openbsd-compat/bsd-misc.h
index cb158cd5c..3a7dd6f4c 100644
--- a/openbsd-compat/bsd-misc.h
+++ b/openbsd-compat/bsd-misc.h
@@ -64,14 +64,6 @@ struct timeval {
64int utimes(char *, struct timeval *); 64int utimes(char *, struct timeval *);
65#endif /* HAVE_UTIMES */ 65#endif /* HAVE_UTIMES */
66 66
67#ifndef HAVE_UTIMENSAT
68/* start with the high bits and work down to minimise risk of overlap */
69# ifndef AT_SYMLINK_NOFOLLOW
70# define AT_SYMLINK_NOFOLLOW 0x80000000
71# endif
72int utimensat(int, const char *, const struct timespec[2], int);
73#endif
74
75#ifndef AT_FDCWD 67#ifndef AT_FDCWD
76# define AT_FDCWD (-2) 68# define AT_FDCWD (-2)
77#endif 69#endif
@@ -88,16 +80,27 @@ int fchownat(int, const char *, uid_t, gid_t, int);
88int truncate (const char *, off_t); 80int truncate (const char *, off_t);
89#endif /* HAVE_TRUNCATE */ 81#endif /* HAVE_TRUNCATE */
90 82
91#if !defined(HAVE_NANOSLEEP) && !defined(HAVE_NSLEEP)
92#ifndef HAVE_STRUCT_TIMESPEC 83#ifndef HAVE_STRUCT_TIMESPEC
93struct timespec { 84struct timespec {
94 time_t tv_sec; 85 time_t tv_sec;
95 long tv_nsec; 86 long tv_nsec;
96}; 87};
97#endif 88#endif /* !HAVE_STRUCT_TIMESPEC */
89
90#if !defined(HAVE_NANOSLEEP) && !defined(HAVE_NSLEEP)
91# include <time.h>
98int nanosleep(const struct timespec *, struct timespec *); 92int nanosleep(const struct timespec *, struct timespec *);
99#endif 93#endif
100 94
95#ifndef HAVE_UTIMENSAT
96# include <time.h>
97/* start with the high bits and work down to minimise risk of overlap */
98# ifndef AT_SYMLINK_NOFOLLOW
99# define AT_SYMLINK_NOFOLLOW 0x80000000
100# endif
101int utimensat(int, const char *, const struct timespec[2], int);
102#endif /* !HAVE_UTIMENSAT */
103
101#ifndef HAVE_USLEEP 104#ifndef HAVE_USLEEP
102int usleep(unsigned int useconds); 105int usleep(unsigned int useconds);
103#endif 106#endif
@@ -177,4 +180,12 @@ int flock(int, int);
177# define fflush(x) (_ssh_compat_fflush(x)) 180# define fflush(x) (_ssh_compat_fflush(x))
178#endif 181#endif
179 182
183#ifndef HAVE_LOCALTIME_R
184struct tm *localtime_r(const time_t *, struct tm *);
185#endif
186
187#ifndef HAVE_REALPATH
188#define realpath(x, y) (sftp_realpath((x), (y)))
189#endif
190
180#endif /* _BSD_MISC_H */ 191#endif /* _BSD_MISC_H */
diff --git a/openbsd-compat/bsd-openpty.c b/openbsd-compat/bsd-openpty.c
index 123a9be56..1ab41f42b 100644
--- a/openbsd-compat/bsd-openpty.c
+++ b/openbsd-compat/bsd-openpty.c
@@ -65,6 +65,8 @@
65#include <string.h> 65#include <string.h>
66#include <unistd.h> 66#include <unistd.h>
67 67
68#include "misc.h"
69
68#ifndef O_NOCTTY 70#ifndef O_NOCTTY
69#define O_NOCTTY 0 71#define O_NOCTTY 0
70#endif 72#endif
@@ -97,16 +99,16 @@ openpty(int *amaster, int *aslave, char *name, struct termios *termp,
97 */ 99 */
98 int ptm; 100 int ptm;
99 char *pts; 101 char *pts;
100 mysig_t old_signal; 102 sshsig_t old_signal;
101 103
102 if ((ptm = open("/dev/ptmx", O_RDWR | O_NOCTTY)) == -1) 104 if ((ptm = open("/dev/ptmx", O_RDWR | O_NOCTTY)) == -1)
103 return (-1); 105 return (-1);
104 106
105 /* XXX: need to close ptm on error? */ 107 /* XXX: need to close ptm on error? */
106 old_signal = signal(SIGCHLD, SIG_DFL); 108 old_signal = ssh_signal(SIGCHLD, SIG_DFL);
107 if (grantpt(ptm) < 0) 109 if (grantpt(ptm) < 0)
108 return (-1); 110 return (-1);
109 signal(SIGCHLD, old_signal); 111 ssh_signal(SIGCHLD, old_signal);
110 112
111 if (unlockpt(ptm) < 0) 113 if (unlockpt(ptm) < 0)
112 return (-1); 114 return (-1);
diff --git a/openbsd-compat/bsd-signal.c b/openbsd-compat/bsd-signal.c
index 0b816a3a6..38d5e972e 100644
--- a/openbsd-compat/bsd-signal.c
+++ b/openbsd-compat/bsd-signal.c
@@ -23,34 +23,6 @@
23 23
24#include "openbsd-compat/bsd-signal.h" 24#include "openbsd-compat/bsd-signal.h"
25 25
26#undef signal
27
28mysig_t
29mysignal(int sig, mysig_t act)
30{
31#ifdef HAVE_SIGACTION
32 struct sigaction sa, osa;
33
34 if (sigaction(sig, NULL, &osa) == -1)
35 return (mysig_t) -1;
36 if (osa.sa_handler != act) {
37 memset(&sa, 0, sizeof(sa));
38 sigemptyset(&sa.sa_mask);
39 sa.sa_flags = 0;
40#ifdef SA_INTERRUPT
41 if (sig == SIGALRM)
42 sa.sa_flags |= SA_INTERRUPT;
43#endif
44 sa.sa_handler = act;
45 if (sigaction(sig, &sa, NULL) == -1)
46 return (mysig_t) -1;
47 }
48 return (osa.sa_handler);
49#else
50 return (signal(sig, act));
51#endif
52}
53
54#if !defined(HAVE_STRSIGNAL) 26#if !defined(HAVE_STRSIGNAL)
55char *strsignal(int sig) 27char *strsignal(int sig)
56{ 28{
diff --git a/openbsd-compat/bsd-signal.h b/openbsd-compat/bsd-signal.h
index 4cb8cb7a0..8d8c44419 100644
--- a/openbsd-compat/bsd-signal.h
+++ b/openbsd-compat/bsd-signal.h
@@ -19,6 +19,8 @@
19 19
20#include "includes.h" 20#include "includes.h"
21 21
22#include <signal.h>
23
22#ifndef _NSIG 24#ifndef _NSIG
23# ifdef NSIG 25# ifdef NSIG
24# define _NSIG NSIG 26# define _NSIG NSIG
@@ -27,11 +29,6 @@
27# endif 29# endif
28#endif 30#endif
29 31
30/* wrapper for signal interface */
31typedef void (*mysig_t)(int);
32mysig_t mysignal(int sig, mysig_t act);
33#define signal(a,b) mysignal(a,b)
34
35#if !defined(HAVE_STRSIGNAL) 32#if !defined(HAVE_STRSIGNAL)
36char *strsignal(int); 33char *strsignal(int);
37#endif 34#endif
diff --git a/openbsd-compat/bsd-statvfs.c b/openbsd-compat/bsd-statvfs.c
index e3bd87d98..10d876439 100644
--- a/openbsd-compat/bsd-statvfs.c
+++ b/openbsd-compat/bsd-statvfs.c
@@ -29,6 +29,11 @@
29# define MNAMELEN 32 29# define MNAMELEN 32
30#endif 30#endif
31 31
32#ifdef HAVE_STRUCT_STATFS_F_FILES
33# define HAVE_STRUCT_STATFS
34#endif
35
36#ifdef HAVE_STRUCT_STATFS
32static void 37static void
33copy_statfs_to_statvfs(struct statvfs *to, struct statfs *from) 38copy_statfs_to_statvfs(struct statvfs *to, struct statfs *from)
34{ 39{
@@ -48,11 +53,12 @@ copy_statfs_to_statvfs(struct statvfs *to, struct statfs *from)
48#endif 53#endif
49 to->f_namemax = MNAMELEN; 54 to->f_namemax = MNAMELEN;
50} 55}
56#endif
51 57
52# ifndef HAVE_STATVFS 58# ifndef HAVE_STATVFS
53int statvfs(const char *path, struct statvfs *buf) 59int statvfs(const char *path, struct statvfs *buf)
54{ 60{
55# ifdef HAVE_STATFS 61# if defined(HAVE_STATFS) && defined(HAVE_STRUCT_STATFS)
56 struct statfs fs; 62 struct statfs fs;
57 63
58 memset(&fs, 0, sizeof(fs)); 64 memset(&fs, 0, sizeof(fs));
@@ -70,7 +76,7 @@ int statvfs(const char *path, struct statvfs *buf)
70# ifndef HAVE_FSTATVFS 76# ifndef HAVE_FSTATVFS
71int fstatvfs(int fd, struct statvfs *buf) 77int fstatvfs(int fd, struct statvfs *buf)
72{ 78{
73# ifdef HAVE_FSTATFS 79# if defined(HAVE_FSTATFS) && defined(HAVE_STRUCT_STATFS)
74 struct statfs fs; 80 struct statfs fs;
75 81
76 memset(&fs, 0, sizeof(fs)); 82 memset(&fs, 0, sizeof(fs));
diff --git a/openbsd-compat/fnmatch.c b/openbsd-compat/fnmatch.c
new file mode 100644
index 000000000..da841d203
--- /dev/null
+++ b/openbsd-compat/fnmatch.c
@@ -0,0 +1,495 @@
1/* $OpenBSD: fnmatch.c,v 1.21 2016/03/01 20:29:03 millert Exp $ */
2
3/* Copyright (c) 2011, VMware, Inc.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * * Neither the name of the VMware, Inc. nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL VMWARE, INC. OR CONTRIBUTORS BE LIABLE FOR
21 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/*
30 * Copyright (c) 2008, 2016 Todd C. Miller <millert@openbsd.org>
31 *
32 * Permission to use, copy, modify, and distribute this software for any
33 * purpose with or without fee is hereby granted, provided that the above
34 * copyright notice and this permission notice appear in all copies.
35 *
36 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
37 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
38 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
39 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
40 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
41 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
42 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
43 */
44
45/* Authored by William A. Rowe Jr. <wrowe; apache.org, vmware.com>, April 2011
46 *
47 * Derived from The Open Group Base Specifications Issue 7, IEEE Std 1003.1-2008
48 * as described in;
49 * http://pubs.opengroup.org/onlinepubs/9699919799/functions/fnmatch.html
50 *
51 * Filename pattern matches defined in section 2.13, "Pattern Matching Notation"
52 * from chapter 2. "Shell Command Language"
53 * http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_13
54 * where; 1. A bracket expression starting with an unquoted <circumflex> '^'
55 * character CONTINUES to specify a non-matching list; 2. an explicit <period> '.'
56 * in a bracket expression matching list, e.g. "[.abc]" does NOT match a leading
57 * <period> in a filename; 3. a <left-square-bracket> '[' which does not introduce
58 * a valid bracket expression is treated as an ordinary character; 4. a differing
59 * number of consecutive slashes within pattern and string will NOT match;
60 * 5. a trailing '\' in FNM_ESCAPE mode is treated as an ordinary '\' character.
61 *
62 * Bracket expansion defined in section 9.3.5, "RE Bracket Expression",
63 * from chapter 9, "Regular Expressions"
64 * http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap09.html#tag_09_03_05
65 * with no support for collating symbols, equivalence class expressions or
66 * character class expressions. A partial range expression with a leading
67 * hyphen following a valid range expression will match only the ordinary
68 * <hyphen> and the ending character (e.g. "[a-m-z]" will match characters
69 * 'a' through 'm', a <hyphen> '-', or a 'z').
70 *
71 * Supports BSD extensions FNM_LEADING_DIR to match pattern to the end of one
72 * path segment of string, and FNM_CASEFOLD to ignore alpha case.
73 *
74 * NOTE: Only POSIX/C single byte locales are correctly supported at this time.
75 * Notably, non-POSIX locales with FNM_CASEFOLD produce undefined results,
76 * particularly in ranges of mixed case (e.g. "[A-z]") or spanning alpha and
77 * nonalpha characters within a range.
78 *
79 * XXX comments below indicate porting required for multi-byte character sets
80 * and non-POSIX locale collation orders; requires mbr* APIs to track shift
81 * state of pattern and string (rewinding pattern and string repeatedly).
82 *
83 * Certain parts of the code assume 0x00-0x3F are unique with any MBCS (e.g.
84 * UTF-8, SHIFT-JIS, etc). Any implementation allowing '\' as an alternate
85 * path delimiter must be aware that 0x5C is NOT unique within SHIFT-JIS.
86 */
87
88/* OPENBSD ORIGINAL: lib/libc/gen/fnmatch.c */
89
90#include "includes.h"
91#ifndef HAVE_FNMATCH
92
93#include <fnmatch.h>
94#include <string.h>
95#include <ctype.h>
96
97#include "charclass.h"
98
99#define RANGE_MATCH 1
100#define RANGE_NOMATCH 0
101#define RANGE_ERROR (-1)
102
103static int
104classmatch(const char *pattern, char test, int foldcase, const char **ep)
105{
106 const char * const mismatch = pattern;
107 const char *colon;
108 struct cclass *cc;
109 int rval = RANGE_NOMATCH;
110 size_t len;
111
112 if (pattern[0] != '[' || pattern[1] != ':') {
113 *ep = mismatch;
114 return RANGE_ERROR;
115 }
116 pattern += 2;
117
118 if ((colon = strchr(pattern, ':')) == NULL || colon[1] != ']') {
119 *ep = mismatch;
120 return RANGE_ERROR;
121 }
122 *ep = colon + 2;
123 len = (size_t)(colon - pattern);
124
125 if (foldcase && strncmp(pattern, "upper:]", 7) == 0)
126 pattern = "lower:]";
127 for (cc = cclasses; cc->name != NULL; cc++) {
128 if (!strncmp(pattern, cc->name, len) && cc->name[len] == '\0') {
129 if (cc->isctype((unsigned char)test))
130 rval = RANGE_MATCH;
131 break;
132 }
133 }
134 if (cc->name == NULL) {
135 /* invalid character class, treat as normal text */
136 *ep = mismatch;
137 rval = RANGE_ERROR;
138 }
139 return rval;
140}
141
142/* Most MBCS/collation/case issues handled here. Wildcard '*' is not handled.
143 * EOS '\0' and the FNM_PATHNAME '/' delimiters are not advanced over,
144 * however the "\/" sequence is advanced to '/'.
145 *
146 * Both pattern and string are **char to support pointer increment of arbitrary
147 * multibyte characters for the given locale, in a later iteration of this code
148 */
149static int fnmatch_ch(const char **pattern, const char **string, int flags)
150{
151 const char * const mismatch = *pattern;
152 const int nocase = !!(flags & FNM_CASEFOLD);
153 const int escape = !(flags & FNM_NOESCAPE);
154 const int slash = !!(flags & FNM_PATHNAME);
155 int result = FNM_NOMATCH;
156 const char *startch;
157 int negate;
158
159 if (**pattern == '[') {
160 ++*pattern;
161
162 /* Handle negation, either leading ! or ^ operators */
163 negate = (**pattern == '!') || (**pattern == '^');
164 if (negate)
165 ++*pattern;
166
167 /* ']' is an ordinary char at the start of the range pattern */
168 if (**pattern == ']')
169 goto leadingclosebrace;
170
171 while (**pattern) {
172 if (**pattern == ']') {
173 ++*pattern;
174 /* XXX: Fix for MBCS character width */
175 ++*string;
176 return (result ^ negate);
177 }
178
179 if (escape && (**pattern == '\\')) {
180 ++*pattern;
181
182 /* Patterns must terminate with ']', not EOS */
183 if (!**pattern)
184 break;
185 }
186
187 /* Patterns must terminate with ']' not '/' */
188 if (slash && (**pattern == '/'))
189 break;
190
191 /* Match character classes. */
192 switch (classmatch(*pattern, **string, nocase, pattern)) {
193 case RANGE_MATCH:
194 result = 0;
195 continue;
196 case RANGE_NOMATCH:
197 /* Valid character class but no match. */
198 continue;
199 default:
200 /* Not a valid character class. */
201 break;
202 }
203 if (!**pattern)
204 break;
205
206leadingclosebrace:
207 /* Look at only well-formed range patterns;
208 * "x-]" is not allowed unless escaped ("x-\]")
209 * XXX: Fix for locale/MBCS character width
210 */
211 if (((*pattern)[1] == '-') && ((*pattern)[2] != ']')) {
212 startch = *pattern;
213 *pattern += (escape && ((*pattern)[2] == '\\')) ? 3 : 2;
214
215 /*
216 * NOT a properly balanced [expr] pattern, EOS
217 * terminated or ranges containing a slash in
218 * FNM_PATHNAME mode pattern fall out to to the
219 * rewind and test '[' literal code path.
220 */
221 if (!**pattern || (slash && (**pattern == '/')))
222 break;
223
224 /* XXX: handle locale/MBCS comparison, advance by MBCS char width */
225 if ((**string >= *startch) && (**string <= **pattern))
226 result = 0;
227 else if (nocase &&
228 (isupper((unsigned char)**string) ||
229 isupper((unsigned char)*startch) ||
230 isupper((unsigned char)**pattern)) &&
231 (tolower((unsigned char)**string) >=
232 tolower((unsigned char)*startch)) &&
233 (tolower((unsigned char)**string) <=
234 tolower((unsigned char)**pattern)))
235 result = 0;
236
237 ++*pattern;
238 continue;
239 }
240
241 /* XXX: handle locale/MBCS comparison, advance by MBCS char width */
242 if ((**string == **pattern))
243 result = 0;
244 else if (nocase && (isupper((unsigned char)**string) ||
245 isupper((unsigned char)**pattern)) &&
246 (tolower((unsigned char)**string) ==
247 tolower((unsigned char)**pattern)))
248 result = 0;
249
250 ++*pattern;
251 }
252 /*
253 * NOT a properly balanced [expr] pattern;
254 * Rewind and reset result to test '[' literal
255 */
256 *pattern = mismatch;
257 result = FNM_NOMATCH;
258 } else if (**pattern == '?') {
259 /* Optimize '?' match before unescaping **pattern */
260 if (!**string || (slash && (**string == '/')))
261 return FNM_NOMATCH;
262 result = 0;
263 goto fnmatch_ch_success;
264 } else if (escape && (**pattern == '\\') && (*pattern)[1]) {
265 ++*pattern;
266 }
267
268 /* XXX: handle locale/MBCS comparison, advance by the MBCS char width */
269 if (**string == **pattern)
270 result = 0;
271 else if (nocase && (isupper((unsigned char)**string) ||
272 isupper((unsigned char)**pattern)) &&
273 (tolower((unsigned char)**string) ==
274 tolower((unsigned char)**pattern)))
275 result = 0;
276
277 /* Refuse to advance over trailing slash or NULs */
278 if (**string == '\0' || **pattern == '\0' ||
279 (slash && ((**string == '/') || (**pattern == '/'))))
280 return result;
281
282fnmatch_ch_success:
283 ++*pattern;
284 ++*string;
285 return result;
286}
287
288
289int fnmatch(const char *pattern, const char *string, int flags)
290{
291 static const char dummystring[2] = {' ', 0};
292 const int escape = !(flags & FNM_NOESCAPE);
293 const int slash = !!(flags & FNM_PATHNAME);
294 const int leading_dir = !!(flags & FNM_LEADING_DIR);
295 const char *dummyptr, *matchptr, *strendseg;
296 int wild;
297 /* For '*' wild processing only; surpress 'used before initialization'
298 * warnings with dummy initialization values;
299 */
300 const char *strstartseg = NULL;
301 const char *mismatch = NULL;
302 int matchlen = 0;
303
304 if (*pattern == '*')
305 goto firstsegment;
306
307 while (*pattern && *string) {
308 /*
309 * Pre-decode "\/" which has no special significance, and
310 * match balanced slashes, starting a new segment pattern.
311 */
312 if (slash && escape && (*pattern == '\\') && (pattern[1] == '/'))
313 ++pattern;
314 if (slash && (*pattern == '/') && (*string == '/')) {
315 ++pattern;
316 ++string;
317 }
318
319firstsegment:
320 /*
321 * At the beginning of each segment, validate leading period
322 * behavior.
323 */
324 if ((flags & FNM_PERIOD) && (*string == '.')) {
325 if (*pattern == '.')
326 ++pattern;
327 else if (escape && (*pattern == '\\') && (pattern[1] == '.'))
328 pattern += 2;
329 else
330 return FNM_NOMATCH;
331 ++string;
332 }
333
334 /*
335 * Determine the end of string segment. Presumes '/'
336 * character is unique, not composite in any MBCS encoding
337 */
338 if (slash) {
339 strendseg = strchr(string, '/');
340 if (!strendseg)
341 strendseg = strchr(string, '\0');
342 } else {
343 strendseg = strchr(string, '\0');
344 }
345
346 /*
347 * Allow pattern '*' to be consumed even with no remaining
348 * string to match.
349 */
350 while (*pattern) {
351 if ((string > strendseg) ||
352 ((string == strendseg) && (*pattern != '*')))
353 break;
354
355 if (slash && ((*pattern == '/') ||
356 (escape && (*pattern == '\\') && (pattern[1] == '/'))))
357 break;
358
359 /*
360 * Reduce groups of '*' and '?' to n '?' matches
361 * followed by one '*' test for simplicity.
362 */
363 for (wild = 0; (*pattern == '*') || (*pattern == '?'); ++pattern) {
364 if (*pattern == '*') {
365 wild = 1;
366 } else if (string < strendseg) { /* && (*pattern == '?') */
367 /* XXX: Advance 1 char for MBCS locale */
368 ++string;
369 }
370 else { /* (string >= strendseg) && (*pattern == '?') */
371 return FNM_NOMATCH;
372 }
373 }
374
375 if (wild) {
376 strstartseg = string;
377 mismatch = pattern;
378
379 /*
380 * Count fixed (non '*') char matches remaining
381 * in pattern * excluding '/' (or "\/") and '*'.
382 */
383 for (matchptr = pattern, matchlen = 0; 1; ++matchlen) {
384 if ((*matchptr == '\0') ||
385 (slash && ((*matchptr == '/') ||
386 (escape && (*matchptr == '\\') &&
387 (matchptr[1] == '/'))))) {
388 /* Compare precisely this many
389 * trailing string chars, the
390 * resulting match needs no
391 * wildcard loop.
392 */
393 /* XXX: Adjust for MBCS */
394 if (string + matchlen > strendseg)
395 return FNM_NOMATCH;
396
397 string = strendseg - matchlen;
398 wild = 0;
399 break;
400 }
401
402 if (*matchptr == '*') {
403 /*
404 * Ensure at least this many
405 * trailing string chars remain
406 * for the first comparison.
407 */
408 /* XXX: Adjust for MBCS */
409 if (string + matchlen > strendseg)
410 return FNM_NOMATCH;
411
412 /*
413 * Begin first wild comparison
414 * at the current position.
415 */
416 break;
417 }
418
419 /*
420 * Skip forward in pattern by a single
421 * character match Use a dummy
422 * fnmatch_ch() test to count one
423 * "[range]" escape.
424 */
425 /* XXX: Adjust for MBCS */
426 if (escape && (*matchptr == '\\') &&
427 matchptr[1]) {
428 matchptr += 2;
429 } else if (*matchptr == '[') {
430 dummyptr = dummystring;
431 fnmatch_ch(&matchptr, &dummyptr,
432 flags);
433 } else {
434 ++matchptr;
435 }
436 }
437 }
438
439 /* Incrementally match string against the pattern. */
440 while (*pattern && (string < strendseg)) {
441 /* Success; begin a new wild pattern search. */
442 if (*pattern == '*')
443 break;
444
445 if (slash && ((*string == '/') ||
446 (*pattern == '/') || (escape &&
447 (*pattern == '\\') && (pattern[1] == '/'))))
448 break;
449
450 /*
451 * Compare ch's (the pattern is advanced over
452 * "\/" to the '/', but slashes will mismatch,
453 * and are not consumed).
454 */
455 if (!fnmatch_ch(&pattern, &string, flags))
456 continue;
457
458 /*
459 * Failed to match, loop against next char
460 * offset of string segment until not enough
461 * string chars remain to match the fixed
462 * pattern.
463 */
464 if (wild) {
465 /* XXX: Advance 1 char for MBCS locale */
466 string = ++strstartseg;
467 if (string + matchlen > strendseg)
468 return FNM_NOMATCH;
469
470 pattern = mismatch;
471 continue;
472 } else
473 return FNM_NOMATCH;
474 }
475 }
476
477 if (*string && !((slash || leading_dir) && (*string == '/')))
478 return FNM_NOMATCH;
479
480 if (*pattern && !(slash && ((*pattern == '/') ||
481 (escape && (*pattern == '\\') && (pattern[1] == '/')))))
482 return FNM_NOMATCH;
483
484 if (leading_dir && !*pattern && *string == '/')
485 return 0;
486 }
487
488 /* Where both pattern and string are at EOS, declare success. */
489 if (!*string && !*pattern)
490 return 0;
491
492 /* Pattern didn't match to the end of string. */
493 return FNM_NOMATCH;
494}
495#endif /* HAVE_FNMATCH */
diff --git a/openbsd-compat/fnmatch.h b/openbsd-compat/fnmatch.h
new file mode 100644
index 000000000..d3bc4a863
--- /dev/null
+++ b/openbsd-compat/fnmatch.h
@@ -0,0 +1,66 @@
1/* $OpenBSD: fnmatch.h,v 1.8 2005/12/13 00:35:22 millert Exp $ */
2/* $NetBSD: fnmatch.h,v 1.5 1994/10/26 00:55:53 cgd Exp $ */
3
4/*-
5 * Copyright (c) 1992, 1993
6 * The Regents of the University of California. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 *
32 * @(#)fnmatch.h 8.1 (Berkeley) 6/2/93
33 */
34
35/* OPENBSD ORIGINAL: include/fnmatch.h */
36
37#ifndef HAVE_FNMATCH_H
38/* Ensure we define FNM_CASEFOLD */
39#define __BSD_VISIBLE 1
40
41#ifndef _FNMATCH_H_
42#define _FNMATCH_H_
43
44#ifdef HAVE_SYS_CDEFS_H
45#include <sys/cdefs.h>
46#endif
47
48#define FNM_NOMATCH 1 /* Match failed. */
49#define FNM_NOSYS 2 /* Function not supported (unused). */
50
51#define FNM_NOESCAPE 0x01 /* Disable backslash escaping. */
52#define FNM_PATHNAME 0x02 /* Slash must be matched by slash. */
53#define FNM_PERIOD 0x04 /* Period must be matched by period. */
54#if __BSD_VISIBLE
55#define FNM_LEADING_DIR 0x08 /* Ignore /<tail> after Imatch. */
56#define FNM_CASEFOLD 0x10 /* Case insensitive search. */
57#define FNM_IGNORECASE FNM_CASEFOLD
58#define FNM_FILE_NAME FNM_PATHNAME
59#endif
60
61/* __BEGIN_DECLS */
62int fnmatch(const char *, const char *, int);
63/* __END_DECLS */
64
65#endif /* !_FNMATCH_H_ */
66#endif /* ! HAVE_FNMATCH_H */
diff --git a/openbsd-compat/glob.c b/openbsd-compat/glob.c
index 7c97e67f5..1e346a8f6 100644
--- a/openbsd-compat/glob.c
+++ b/openbsd-compat/glob.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: glob.c,v 1.38 2011/09/22 06:27:29 djm Exp $ */ 1/* $OpenBSD: glob.c,v 1.48 2019/02/04 16:45:40 millert Exp $ */
2/* 2/*
3 * Copyright (c) 1989, 1993 3 * Copyright (c) 1989, 1993
4 * The Regents of the University of California. All rights reserved. 4 * The Regents of the University of California. All rights reserved.
@@ -70,6 +70,9 @@
70#include <limits.h> 70#include <limits.h>
71#include <pwd.h> 71#include <pwd.h>
72#include <stdlib.h> 72#include <stdlib.h>
73#ifdef HAVE_STDINT_H
74#include <stdint.h>
75#endif
73#include <string.h> 76#include <string.h>
74#include <unistd.h> 77#include <unistd.h>
75 78
@@ -131,12 +134,9 @@ typedef char Char;
131#define ismeta(c) (((c)&M_QUOTE) != 0) 134#define ismeta(c) (((c)&M_QUOTE) != 0)
132 135
133#define GLOB_LIMIT_MALLOC 65536 136#define GLOB_LIMIT_MALLOC 65536
134#define GLOB_LIMIT_STAT 128 137#define GLOB_LIMIT_STAT 2048
135#define GLOB_LIMIT_READDIR 16384 138#define GLOB_LIMIT_READDIR 16384
136 139
137/* Limit of recursion during matching attempts. */
138#define GLOB_LIMIT_RECUR 64
139
140struct glob_lim { 140struct glob_lim {
141 size_t glim_malloc; 141 size_t glim_malloc;
142 size_t glim_stat; 142 size_t glim_stat;
@@ -150,7 +150,7 @@ struct glob_path_stat {
150 150
151static int compare(const void *, const void *); 151static int compare(const void *, const void *);
152static int compare_gps(const void *, const void *); 152static int compare_gps(const void *, const void *);
153static int g_Ctoc(const Char *, char *, u_int); 153static int g_Ctoc(const Char *, char *, size_t);
154static int g_lstat(Char *, struct stat *, glob_t *); 154static int g_lstat(Char *, struct stat *, glob_t *);
155static DIR *g_opendir(Char *, glob_t *); 155static DIR *g_opendir(Char *, glob_t *);
156static Char *g_strchr(const Char *, int); 156static Char *g_strchr(const Char *, int);
@@ -169,7 +169,7 @@ static const Char *
169static int globexp1(const Char *, glob_t *, struct glob_lim *); 169static int globexp1(const Char *, glob_t *, struct glob_lim *);
170static int globexp2(const Char *, const Char *, glob_t *, 170static int globexp2(const Char *, const Char *, glob_t *,
171 struct glob_lim *); 171 struct glob_lim *);
172static int match(Char *, Char *, Char *, int); 172static int match(Char *, Char *, Char *);
173#ifdef DEBUG 173#ifdef DEBUG
174static void qprintf(const char *, Char *); 174static void qprintf(const char *, Char *);
175#endif 175#endif
@@ -180,12 +180,9 @@ glob(const char *pattern, int flags, int (*errfunc)(const char *, int),
180{ 180{
181 const u_char *patnext; 181 const u_char *patnext;
182 int c; 182 int c;
183 Char *bufnext, *bufend, patbuf[MAXPATHLEN]; 183 Char *bufnext, *bufend, patbuf[PATH_MAX];
184 struct glob_lim limit = { 0, 0, 0 }; 184 struct glob_lim limit = { 0, 0, 0 };
185 185
186 if (strnlen(pattern, PATH_MAX) == PATH_MAX)
187 return(GLOB_NOMATCH);
188
189 patnext = (u_char *) pattern; 186 patnext = (u_char *) pattern;
190 if (!(flags & GLOB_APPEND)) { 187 if (!(flags & GLOB_APPEND)) {
191 pglob->gl_pathc = 0; 188 pglob->gl_pathc = 0;
@@ -198,13 +195,15 @@ glob(const char *pattern, int flags, int (*errfunc)(const char *, int),
198 pglob->gl_errfunc = errfunc; 195 pglob->gl_errfunc = errfunc;
199 pglob->gl_matchc = 0; 196 pglob->gl_matchc = 0;
200 197
201 if (pglob->gl_offs < 0 || pglob->gl_pathc < 0 || 198 if (strnlen(pattern, PATH_MAX) == PATH_MAX)
202 pglob->gl_offs >= INT_MAX || pglob->gl_pathc >= INT_MAX || 199 return(GLOB_NOMATCH);
203 pglob->gl_pathc >= INT_MAX - pglob->gl_offs - 1) 200
201 if (pglob->gl_offs >= SSIZE_MAX || pglob->gl_pathc >= SSIZE_MAX ||
202 pglob->gl_pathc >= SSIZE_MAX - pglob->gl_offs - 1)
204 return GLOB_NOSPACE; 203 return GLOB_NOSPACE;
205 204
206 bufnext = patbuf; 205 bufnext = patbuf;
207 bufend = bufnext + MAXPATHLEN - 1; 206 bufend = bufnext + PATH_MAX - 1;
208 if (flags & GLOB_NOESCAPE) 207 if (flags & GLOB_NOESCAPE)
209 while (bufnext < bufend && (c = *patnext++) != EOS) 208 while (bufnext < bufend && (c = *patnext++) != EOS)
210 *bufnext++ = c; 209 *bufnext++ = c;
@@ -261,7 +260,7 @@ globexp2(const Char *ptr, const Char *pattern, glob_t *pglob,
261 int i, rv; 260 int i, rv;
262 Char *lm, *ls; 261 Char *lm, *ls;
263 const Char *pe, *pm, *pl; 262 const Char *pe, *pm, *pl;
264 Char patbuf[MAXPATHLEN]; 263 Char patbuf[PATH_MAX];
265 264
266 /* copy part up to the brace */ 265 /* copy part up to the brace */
267 for (lm = patbuf, pm = pattern; pm != ptr; *lm++ = *pm++) 266 for (lm = patbuf, pm = pattern; pm != ptr; *lm++ = *pm++)
@@ -472,10 +471,11 @@ static int
472glob0(const Char *pattern, glob_t *pglob, struct glob_lim *limitp) 471glob0(const Char *pattern, glob_t *pglob, struct glob_lim *limitp)
473{ 472{
474 const Char *qpatnext; 473 const Char *qpatnext;
475 int c, err, oldpathc; 474 int c, err;
476 Char *bufnext, patbuf[MAXPATHLEN]; 475 size_t oldpathc;
476 Char *bufnext, patbuf[PATH_MAX];
477 477
478 qpatnext = globtilde(pattern, patbuf, MAXPATHLEN, pglob); 478 qpatnext = globtilde(pattern, patbuf, PATH_MAX, pglob);
479 oldpathc = pglob->gl_pathc; 479 oldpathc = pglob->gl_pathc;
480 bufnext = patbuf; 480 bufnext = patbuf;
481 481
@@ -545,7 +545,7 @@ glob0(const Char *pattern, glob_t *pglob, struct glob_lim *limitp)
545 qprintf("glob0:", patbuf); 545 qprintf("glob0:", patbuf);
546#endif 546#endif
547 547
548 if ((err = glob1(patbuf, patbuf+MAXPATHLEN-1, pglob, limitp)) != 0) 548 if ((err = glob1(patbuf, patbuf+PATH_MAX-1, pglob, limitp)) != 0)
549 return(err); 549 return(err);
550 550
551 /* 551 /*
@@ -566,9 +566,9 @@ glob0(const Char *pattern, glob_t *pglob, struct glob_lim *limitp)
566 if ((pglob->gl_flags & GLOB_KEEPSTAT)) { 566 if ((pglob->gl_flags & GLOB_KEEPSTAT)) {
567 /* Keep the paths and stat info synced during sort */ 567 /* Keep the paths and stat info synced during sort */
568 struct glob_path_stat *path_stat; 568 struct glob_path_stat *path_stat;
569 int i; 569 size_t i;
570 int n = pglob->gl_pathc - oldpathc; 570 size_t n = pglob->gl_pathc - oldpathc;
571 int o = pglob->gl_offs + oldpathc; 571 size_t o = pglob->gl_offs + oldpathc;
572 572
573 if ((path_stat = calloc(n, sizeof(*path_stat))) == NULL) 573 if ((path_stat = calloc(n, sizeof(*path_stat))) == NULL)
574 return GLOB_NOSPACE; 574 return GLOB_NOSPACE;
@@ -609,13 +609,13 @@ compare_gps(const void *_p, const void *_q)
609static int 609static int
610glob1(Char *pattern, Char *pattern_last, glob_t *pglob, struct glob_lim *limitp) 610glob1(Char *pattern, Char *pattern_last, glob_t *pglob, struct glob_lim *limitp)
611{ 611{
612 Char pathbuf[MAXPATHLEN]; 612 Char pathbuf[PATH_MAX];
613 613
614 /* A null pathname is invalid -- POSIX 1003.1 sect. 2.4. */ 614 /* A null pathname is invalid -- POSIX 1003.1 sect. 2.4. */
615 if (*pattern == EOS) 615 if (*pattern == EOS)
616 return(0); 616 return(0);
617 return(glob2(pathbuf, pathbuf+MAXPATHLEN-1, 617 return(glob2(pathbuf, pathbuf+PATH_MAX-1,
618 pathbuf, pathbuf+MAXPATHLEN-1, 618 pathbuf, pathbuf+PATH_MAX-1,
619 pattern, pattern_last, pglob, limitp)); 619 pattern, pattern_last, pglob, limitp));
620} 620}
621 621
@@ -639,8 +639,6 @@ glob2(Char *pathbuf, Char *pathbuf_last, Char *pathend, Char *pathend_last,
639 for (anymeta = 0;;) { 639 for (anymeta = 0;;) {
640 if (*pattern == EOS) { /* End of pattern? */ 640 if (*pattern == EOS) { /* End of pattern? */
641 *pathend = EOS; 641 *pathend = EOS;
642 if (g_lstat(pathbuf, &sb, pglob))
643 return(0);
644 642
645 if ((pglob->gl_flags & GLOB_LIMIT) && 643 if ((pglob->gl_flags & GLOB_LIMIT) &&
646 limitp->glim_stat++ >= GLOB_LIMIT_STAT) { 644 limitp->glim_stat++ >= GLOB_LIMIT_STAT) {
@@ -649,6 +647,8 @@ glob2(Char *pathbuf, Char *pathbuf_last, Char *pathend, Char *pathend_last,
649 *pathend = EOS; 647 *pathend = EOS;
650 return(GLOB_NOSPACE); 648 return(GLOB_NOSPACE);
651 } 649 }
650 if (g_lstat(pathbuf, &sb, pglob))
651 return(0);
652 652
653 if (((pglob->gl_flags & GLOB_MARK) && 653 if (((pglob->gl_flags & GLOB_MARK) &&
654 pathend[-1] != SEP) && (S_ISDIR(sb.st_mode) || 654 pathend[-1] != SEP) && (S_ISDIR(sb.st_mode) ||
@@ -700,7 +700,7 @@ glob3(Char *pathbuf, Char *pathbuf_last, Char *pathend, Char *pathend_last,
700 struct dirent *dp; 700 struct dirent *dp;
701 DIR *dirp; 701 DIR *dirp;
702 int err; 702 int err;
703 char buf[MAXPATHLEN]; 703 char buf[PATH_MAX];
704 704
705 /* 705 /*
706 * The readdirfunc declaration can't be prototyped, because it is 706 * The readdirfunc declaration can't be prototyped, because it is
@@ -760,7 +760,7 @@ glob3(Char *pathbuf, Char *pathbuf_last, Char *pathend, Char *pathend_last,
760 break; 760 break;
761 } 761 }
762 762
763 if (!match(pathend, pattern, restpattern, GLOB_LIMIT_RECUR)) { 763 if (!match(pathend, pattern, restpattern)) {
764 *pathend = EOS; 764 *pathend = EOS;
765 continue; 765 continue;
766 } 766 }
@@ -797,38 +797,33 @@ globextend(const Char *path, glob_t *pglob, struct glob_lim *limitp,
797 struct stat *sb) 797 struct stat *sb)
798{ 798{
799 char **pathv; 799 char **pathv;
800 ssize_t i; 800 size_t i, newn, len;
801 size_t newn, len;
802 char *copy = NULL; 801 char *copy = NULL;
803 const Char *p; 802 const Char *p;
804 struct stat **statv; 803 struct stat **statv;
805 804
806 newn = 2 + pglob->gl_pathc + pglob->gl_offs; 805 newn = 2 + pglob->gl_pathc + pglob->gl_offs;
807 if (pglob->gl_offs >= INT_MAX || 806 if (pglob->gl_offs >= SSIZE_MAX ||
808 pglob->gl_pathc >= INT_MAX || 807 pglob->gl_pathc >= SSIZE_MAX ||
809 newn >= INT_MAX || 808 newn >= SSIZE_MAX ||
810 SIZE_MAX / sizeof(*pathv) <= newn || 809 SIZE_MAX / sizeof(*pathv) <= newn ||
811 SIZE_MAX / sizeof(*statv) <= newn) { 810 SIZE_MAX / sizeof(*statv) <= newn) {
812 nospace: 811 nospace:
813 for (i = pglob->gl_offs; i < (ssize_t)(newn - 2); i++) { 812 for (i = pglob->gl_offs; i < newn - 2; i++) {
814 if (pglob->gl_pathv && pglob->gl_pathv[i]) 813 if (pglob->gl_pathv && pglob->gl_pathv[i])
815 free(pglob->gl_pathv[i]); 814 free(pglob->gl_pathv[i]);
816 if ((pglob->gl_flags & GLOB_KEEPSTAT) != 0 && 815 if ((pglob->gl_flags & GLOB_KEEPSTAT) != 0 &&
817 pglob->gl_pathv && pglob->gl_pathv[i]) 816 pglob->gl_pathv && pglob->gl_pathv[i])
818 free(pglob->gl_statv[i]); 817 free(pglob->gl_statv[i]);
819 } 818 }
820 if (pglob->gl_pathv) { 819 free(pglob->gl_pathv);
821 free(pglob->gl_pathv); 820 pglob->gl_pathv = NULL;
822 pglob->gl_pathv = NULL; 821 free(pglob->gl_statv);
823 } 822 pglob->gl_statv = NULL;
824 if (pglob->gl_statv) {
825 free(pglob->gl_statv);
826 pglob->gl_statv = NULL;
827 }
828 return(GLOB_NOSPACE); 823 return(GLOB_NOSPACE);
829 } 824 }
830 825
831 pathv = realloc(pglob->gl_pathv, newn * sizeof(*pathv)); 826 pathv = reallocarray(pglob->gl_pathv, newn, sizeof(*pathv));
832 if (pathv == NULL) 827 if (pathv == NULL)
833 goto nospace; 828 goto nospace;
834 if (pglob->gl_pathv == NULL && pglob->gl_offs > 0) { 829 if (pglob->gl_pathv == NULL && pglob->gl_offs > 0) {
@@ -840,7 +835,7 @@ globextend(const Char *path, glob_t *pglob, struct glob_lim *limitp,
840 pglob->gl_pathv = pathv; 835 pglob->gl_pathv = pathv;
841 836
842 if ((pglob->gl_flags & GLOB_KEEPSTAT) != 0) { 837 if ((pglob->gl_flags & GLOB_KEEPSTAT) != 0) {
843 statv = realloc(pglob->gl_statv, newn * sizeof(*statv)); 838 statv = reallocarray(pglob->gl_statv, newn, sizeof(*statv));
844 if (statv == NULL) 839 if (statv == NULL)
845 goto nospace; 840 goto nospace;
846 if (pglob->gl_statv == NULL && pglob->gl_offs > 0) { 841 if (pglob->gl_statv == NULL && pglob->gl_offs > 0) {
@@ -894,17 +889,24 @@ globextend(const Char *path, glob_t *pglob, struct glob_lim *limitp,
894 889
895/* 890/*
896 * pattern matching function for filenames. Each occurrence of the * 891 * pattern matching function for filenames. Each occurrence of the *
897 * pattern causes a recursion level. 892 * pattern causes an iteration.
893 *
894 * Note, this function differs from the original as per the discussion
895 * here: https://research.swtch.com/glob
896 *
897 * Basically we removed the recursion and made it use the algorithm
898 * from Russ Cox to not go quadratic on cases like a file called
899 * ("a" x 100) . "x" matched against a pattern like "a*a*a*a*a*a*a*y".
898 */ 900 */
899static int 901static int
900match(Char *name, Char *pat, Char *patend, int recur) 902match(Char *name, Char *pat, Char *patend)
901{ 903{
902 int ok, negate_range; 904 int ok, negate_range;
903 Char c, k; 905 Char c, k;
906 Char *nextp = NULL;
907 Char *nextn = NULL;
904 908
905 if (recur-- == 0) 909loop:
906 return(GLOB_NOSPACE);
907
908 while (pat < patend) { 910 while (pat < patend) {
909 c = *pat++; 911 c = *pat++;
910 switch (c & M_MASK) { 912 switch (c & M_MASK) {
@@ -913,19 +915,19 @@ match(Char *name, Char *pat, Char *patend, int recur)
913 pat++; /* eat consecutive '*' */ 915 pat++; /* eat consecutive '*' */
914 if (pat == patend) 916 if (pat == patend)
915 return(1); 917 return(1);
916 do { 918 if (*name == EOS)
917 if (match(name, pat, patend, recur)) 919 return(0);
918 return(1); 920 nextn = name + 1;
919 } while (*name++ != EOS); 921 nextp = pat - 1;
920 return(0); 922 break;
921 case M_ONE: 923 case M_ONE:
922 if (*name++ == EOS) 924 if (*name++ == EOS)
923 return(0); 925 goto fail;
924 break; 926 break;
925 case M_SET: 927 case M_SET:
926 ok = 0; 928 ok = 0;
927 if ((k = *name++) == EOS) 929 if ((k = *name++) == EOS)
928 return(0); 930 goto fail;
929 if ((negate_range = ((*pat & M_MASK) == M_NOT)) != EOS) 931 if ((negate_range = ((*pat & M_MASK) == M_NOT)) != EOS)
930 ++pat; 932 ++pat;
931 while (((c = *pat++) & M_MASK) != M_END) { 933 while (((c = *pat++) & M_MASK) != M_END) {
@@ -944,36 +946,43 @@ match(Char *name, Char *pat, Char *patend, int recur)
944 ok = 1; 946 ok = 1;
945 } 947 }
946 if (ok == negate_range) 948 if (ok == negate_range)
947 return(0); 949 goto fail;
948 break; 950 break;
949 default: 951 default:
950 if (*name++ != c) 952 if (*name++ != c)
951 return(0); 953 goto fail;
952 break; 954 break;
953 } 955 }
954 } 956 }
955 return(*name == EOS); 957 if (*name == EOS)
958 return(1);
959
960fail:
961 if (nextn) {
962 pat = nextp;
963 name = nextn;
964 goto loop;
965 }
966 return(0);
956} 967}
957 968
958/* Free allocated data belonging to a glob_t structure. */ 969/* Free allocated data belonging to a glob_t structure. */
959void 970void
960globfree(glob_t *pglob) 971globfree(glob_t *pglob)
961{ 972{
962 int i; 973 size_t i;
963 char **pp; 974 char **pp;
964 975
965 if (pglob->gl_pathv != NULL) { 976 if (pglob->gl_pathv != NULL) {
966 pp = pglob->gl_pathv + pglob->gl_offs; 977 pp = pglob->gl_pathv + pglob->gl_offs;
967 for (i = pglob->gl_pathc; i--; ++pp) 978 for (i = pglob->gl_pathc; i--; ++pp)
968 if (*pp) 979 free(*pp);
969 free(*pp);
970 free(pglob->gl_pathv); 980 free(pglob->gl_pathv);
971 pglob->gl_pathv = NULL; 981 pglob->gl_pathv = NULL;
972 } 982 }
973 if (pglob->gl_statv != NULL) { 983 if (pglob->gl_statv != NULL) {
974 for (i = 0; i < pglob->gl_pathc; i++) { 984 for (i = 0; i < pglob->gl_pathc; i++) {
975 if (pglob->gl_statv[i] != NULL) 985 free(pglob->gl_statv[i]);
976 free(pglob->gl_statv[i]);
977 } 986 }
978 free(pglob->gl_statv); 987 free(pglob->gl_statv);
979 pglob->gl_statv = NULL; 988 pglob->gl_statv = NULL;
@@ -983,7 +992,7 @@ globfree(glob_t *pglob)
983static DIR * 992static DIR *
984g_opendir(Char *str, glob_t *pglob) 993g_opendir(Char *str, glob_t *pglob)
985{ 994{
986 char buf[MAXPATHLEN]; 995 char buf[PATH_MAX];
987 996
988 if (!*str) 997 if (!*str)
989 strlcpy(buf, ".", sizeof buf); 998 strlcpy(buf, ".", sizeof buf);
@@ -1001,7 +1010,7 @@ g_opendir(Char *str, glob_t *pglob)
1001static int 1010static int
1002g_lstat(Char *fn, struct stat *sb, glob_t *pglob) 1011g_lstat(Char *fn, struct stat *sb, glob_t *pglob)
1003{ 1012{
1004 char buf[MAXPATHLEN]; 1013 char buf[PATH_MAX];
1005 1014
1006 if (g_Ctoc(fn, buf, sizeof(buf))) 1015 if (g_Ctoc(fn, buf, sizeof(buf)))
1007 return(-1); 1016 return(-1);
@@ -1013,7 +1022,7 @@ g_lstat(Char *fn, struct stat *sb, glob_t *pglob)
1013static int 1022static int
1014g_stat(Char *fn, struct stat *sb, glob_t *pglob) 1023g_stat(Char *fn, struct stat *sb, glob_t *pglob)
1015{ 1024{
1016 char buf[MAXPATHLEN]; 1025 char buf[PATH_MAX];
1017 1026
1018 if (g_Ctoc(fn, buf, sizeof(buf))) 1027 if (g_Ctoc(fn, buf, sizeof(buf)))
1019 return(-1); 1028 return(-1);
@@ -1033,7 +1042,7 @@ g_strchr(const Char *str, int ch)
1033} 1042}
1034 1043
1035static int 1044static int
1036g_Ctoc(const Char *str, char *buf, u_int len) 1045g_Ctoc(const Char *str, char *buf, size_t len)
1037{ 1046{
1038 1047
1039 while (len--) { 1048 while (len--) {
diff --git a/openbsd-compat/glob.h b/openbsd-compat/glob.h
index f069a05dc..1692d36cc 100644
--- a/openbsd-compat/glob.h
+++ b/openbsd-compat/glob.h
@@ -1,4 +1,4 @@
1/* $OpenBSD: glob.h,v 1.11 2010/09/24 13:32:55 djm Exp $ */ 1/* $OpenBSD: glob.h,v 1.14 2019/02/04 16:45:40 millert Exp $ */
2/* $NetBSD: glob.h,v 1.5 1994/10/26 00:55:56 cgd Exp $ */ 2/* $NetBSD: glob.h,v 1.5 1994/10/26 00:55:56 cgd Exp $ */
3 3
4/* 4/*
@@ -46,6 +46,7 @@
46#define _COMPAT_GLOB_H_ 46#define _COMPAT_GLOB_H_
47 47
48#include <sys/stat.h> 48#include <sys/stat.h>
49#include <sys/types.h>
49 50
50# define glob_t _ssh_compat_glob_t 51# define glob_t _ssh_compat_glob_t
51# define glob(a, b, c, d) _ssh__compat_glob(a, b, c, d) 52# define glob(a, b, c, d) _ssh__compat_glob(a, b, c, d)
@@ -53,9 +54,9 @@
53 54
54struct stat; 55struct stat;
55typedef struct { 56typedef struct {
56 int gl_pathc; /* Count of total paths so far. */ 57 size_t gl_pathc; /* Count of total paths so far. */
57 int gl_matchc; /* Count of paths matching pattern. */ 58 size_t gl_matchc; /* Count of paths matching pattern. */
58 int gl_offs; /* Reserved at beginning of gl_pathv. */ 59 size_t gl_offs; /* Reserved at beginning of gl_pathv. */
59 int gl_flags; /* Copy of flags parameter to glob. */ 60 int gl_flags; /* Copy of flags parameter to glob. */
60 char **gl_pathv; /* List of paths matching pattern. */ 61 char **gl_pathv; /* List of paths matching pattern. */
61 struct stat **gl_statv; /* Stat entries corresponding to gl_pathv */ 62 struct stat **gl_statv; /* Stat entries corresponding to gl_pathv */
diff --git a/openbsd-compat/openbsd-compat.h b/openbsd-compat/openbsd-compat.h
index fda6706f8..4a16702ef 100644
--- a/openbsd-compat/openbsd-compat.h
+++ b/openbsd-compat/openbsd-compat.h
@@ -44,9 +44,9 @@
44#include "getrrsetbyname.h" 44#include "getrrsetbyname.h"
45#include "sha1.h" 45#include "sha1.h"
46#include "sha2.h" 46#include "sha2.h"
47#include "rmd160.h"
48#include "md5.h" 47#include "md5.h"
49#include "blf.h" 48#include "blf.h"
49#include "fnmatch.h"
50 50
51#ifndef HAVE_BASENAME 51#ifndef HAVE_BASENAME
52char *basename(const char *path); 52char *basename(const char *path);
@@ -73,7 +73,7 @@ int getpagesize(void);
73char *getcwd(char *pt, size_t size); 73char *getcwd(char *pt, size_t size);
74#endif 74#endif
75 75
76#ifdef HAVE_MEMMEM 76#if defined(HAVE_DECL_MEMMEM) && HAVE_DECL_MEMMEM == 0
77void *memmem(const void *, size_t, const void *, size_t); 77void *memmem(const void *, size_t, const void *, size_t);
78#endif 78#endif
79 79
@@ -322,6 +322,10 @@ void explicit_bzero(void *p, size_t n);
322void freezero(void *, size_t); 322void freezero(void *, size_t);
323#endif 323#endif
324 324
325#ifndef HAVE_LOCALTIME_R
326struct tm *localtime_r(const time_t *, struct tm *);
327#endif
328
325char *xcrypt(const char *password, const char *salt); 329char *xcrypt(const char *password, const char *salt);
326char *shadow_pw(struct passwd *pw); 330char *shadow_pw(struct passwd *pw);
327 331
diff --git a/openbsd-compat/openssl-compat.h b/openbsd-compat/openssl-compat.h
index 917bc6f7c..abdcb8763 100644
--- a/openbsd-compat/openssl-compat.h
+++ b/openbsd-compat/openssl-compat.h
@@ -87,12 +87,6 @@ void ssh_aes_ctr_iv(EVP_CIPHER_CTX *, int, u_char *, size_t);
87# endif 87# endif
88#endif 88#endif
89 89
90#if defined(HAVE_EVP_RIPEMD160)
91# if defined(OPENSSL_NO_RIPEMD) || defined(OPENSSL_NO_RMD160)
92# undef HAVE_EVP_RIPEMD160
93# endif
94#endif
95
96/* LibreSSL/OpenSSL 1.1x API compat */ 90/* LibreSSL/OpenSSL 1.1x API compat */
97#ifndef HAVE_DSA_GET0_PQG 91#ifndef HAVE_DSA_GET0_PQG
98void DSA_get0_pqg(const DSA *d, const BIGNUM **p, const BIGNUM **q, 92void DSA_get0_pqg(const DSA *d, const BIGNUM **p, const BIGNUM **q,
diff --git a/openbsd-compat/port-aix.c b/openbsd-compat/port-aix.c
index fc80dc39f..e1ad3f117 100644
--- a/openbsd-compat/port-aix.c
+++ b/openbsd-compat/port-aix.c
@@ -26,6 +26,8 @@
26 */ 26 */
27#include "includes.h" 27#include "includes.h"
28 28
29#ifdef _AIX
30
29#include "xmalloc.h" 31#include "xmalloc.h"
30#include "sshbuf.h" 32#include "sshbuf.h"
31#include "ssherr.h" 33#include "ssherr.h"
@@ -36,8 +38,6 @@
36#include "ssh_api.h" 38#include "ssh_api.h"
37#include "log.h" 39#include "log.h"
38 40
39#ifdef _AIX
40
41#include <errno.h> 41#include <errno.h>
42#if defined(HAVE_NETDB_H) 42#if defined(HAVE_NETDB_H)
43# include <netdb.h> 43# include <netdb.h>
diff --git a/openbsd-compat/port-net.c b/openbsd-compat/port-net.c
index bb535626f..617bffceb 100644
--- a/openbsd-compat/port-net.c
+++ b/openbsd-compat/port-net.c
@@ -137,6 +137,7 @@ sys_set_process_rdomain(const char *name)
137 137
138#if defined(SSH_TUN_LINUX) 138#if defined(SSH_TUN_LINUX)
139#include <linux/if_tun.h> 139#include <linux/if_tun.h>
140#define TUN_CTRL_DEV "/dev/net/tun"
140 141
141int 142int
142sys_tun_open(int tun, int mode, char **ifname) 143sys_tun_open(int tun, int mode, char **ifname)
@@ -147,10 +148,9 @@ sys_tun_open(int tun, int mode, char **ifname)
147 148
148 if (ifname != NULL) 149 if (ifname != NULL)
149 *ifname = NULL; 150 *ifname = NULL;
150 151 if ((fd = open(TUN_CTRL_DEV, O_RDWR)) == -1) {
151 if ((fd = open("/dev/net/tun", O_RDWR)) == -1) { 152 debug("%s: failed to open tunnel control device \"%s\": %s",
152 debug("%s: failed to open tunnel control interface: %s", 153 __func__, TUN_CTRL_DEV, strerror(errno));
153 __func__, strerror(errno));
154 return (-1); 154 return (-1);
155 } 155 }
156 156
diff --git a/openbsd-compat/rmd160.c b/openbsd-compat/rmd160.c
deleted file mode 100644
index e915141a5..000000000
--- a/openbsd-compat/rmd160.c
+++ /dev/null
@@ -1,378 +0,0 @@
1/*
2 * Copyright (c) 2001 Markus Friedl. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 */
24/*
25 * Preneel, Bosselaers, Dobbertin, "The Cryptographic Hash Function RIPEMD-160",
26 * RSA Laboratories, CryptoBytes, Volume 3, Number 2, Autumn 1997,
27 * ftp://ftp.rsasecurity.com/pub/cryptobytes/crypto3n2.pdf
28 */
29
30#include "includes.h"
31
32#ifndef WITH_OPENSSL
33
34#include <sys/types.h>
35#ifdef HAVE_ENDIAN_H
36#include <endian.h>
37#endif
38#include <string.h>
39#include <rmd160.h>
40
41#define PUT_64BIT_LE(cp, value) do { \
42 (cp)[7] = (value) >> 56; \
43 (cp)[6] = (value) >> 48; \
44 (cp)[5] = (value) >> 40; \
45 (cp)[4] = (value) >> 32; \
46 (cp)[3] = (value) >> 24; \
47 (cp)[2] = (value) >> 16; \
48 (cp)[1] = (value) >> 8; \
49 (cp)[0] = (value); } while (0)
50
51#define PUT_32BIT_LE(cp, value) do { \
52 (cp)[3] = (value) >> 24; \
53 (cp)[2] = (value) >> 16; \
54 (cp)[1] = (value) >> 8; \
55 (cp)[0] = (value); } while (0)
56
57#define H0 0x67452301U
58#define H1 0xEFCDAB89U
59#define H2 0x98BADCFEU
60#define H3 0x10325476U
61#define H4 0xC3D2E1F0U
62
63#define K0 0x00000000U
64#define K1 0x5A827999U
65#define K2 0x6ED9EBA1U
66#define K3 0x8F1BBCDCU
67#define K4 0xA953FD4EU
68
69#define KK0 0x50A28BE6U
70#define KK1 0x5C4DD124U
71#define KK2 0x6D703EF3U
72#define KK3 0x7A6D76E9U
73#define KK4 0x00000000U
74
75/* rotate x left n bits. */
76#define ROL(n, x) (((x) << (n)) | ((x) >> (32-(n))))
77
78#define F0(x, y, z) ((x) ^ (y) ^ (z))
79#define F1(x, y, z) (((x) & (y)) | ((~x) & (z)))
80#define F2(x, y, z) (((x) | (~y)) ^ (z))
81#define F3(x, y, z) (((x) & (z)) | ((y) & (~z)))
82#define F4(x, y, z) ((x) ^ ((y) | (~z)))
83
84#define R(a, b, c, d, e, Fj, Kj, sj, rj) \
85 do { \
86 a = ROL(sj, a + Fj(b,c,d) + X(rj) + Kj) + e; \
87 c = ROL(10, c); \
88 } while(0)
89
90#define X(i) x[i]
91
92static u_int8_t PADDING[RMD160_BLOCK_LENGTH] = {
93 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
94 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
95 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
96};
97
98void
99RMD160Init(RMD160_CTX *ctx)
100{
101 ctx->count = 0;
102 ctx->state[0] = H0;
103 ctx->state[1] = H1;
104 ctx->state[2] = H2;
105 ctx->state[3] = H3;
106 ctx->state[4] = H4;
107}
108
109void
110RMD160Update(RMD160_CTX *ctx, const u_int8_t *input, size_t len)
111{
112 size_t have, off, need;
113
114 have = (ctx->count / 8) % RMD160_BLOCK_LENGTH;
115 need = RMD160_BLOCK_LENGTH - have;
116 ctx->count += 8 * len;
117 off = 0;
118
119 if (len >= need) {
120 if (have) {
121 memcpy(ctx->buffer + have, input, need);
122 RMD160Transform(ctx->state, ctx->buffer);
123 off = need;
124 have = 0;
125 }
126 /* now the buffer is empty */
127 while (off + RMD160_BLOCK_LENGTH <= len) {
128 RMD160Transform(ctx->state, input+off);
129 off += RMD160_BLOCK_LENGTH;
130 }
131 }
132 if (off < len)
133 memcpy(ctx->buffer + have, input+off, len-off);
134}
135
136void
137RMD160Pad(RMD160_CTX *ctx)
138{
139 u_int8_t size[8];
140 size_t padlen;
141
142 PUT_64BIT_LE(size, ctx->count);
143
144 /*
145 * pad to RMD160_BLOCK_LENGTH byte blocks, at least one byte from
146 * PADDING plus 8 bytes for the size
147 */
148 padlen = RMD160_BLOCK_LENGTH - ((ctx->count / 8) % RMD160_BLOCK_LENGTH);
149 if (padlen < 1 + 8)
150 padlen += RMD160_BLOCK_LENGTH;
151 RMD160Update(ctx, PADDING, padlen - 8); /* padlen - 8 <= 64 */
152 RMD160Update(ctx, size, 8);
153}
154
155void
156RMD160Final(u_int8_t digest[RMD160_DIGEST_LENGTH], RMD160_CTX *ctx)
157{
158 int i;
159
160 RMD160Pad(ctx);
161 for (i = 0; i < 5; i++)
162 PUT_32BIT_LE(digest + i*4, ctx->state[i]);
163 memset(ctx, 0, sizeof (*ctx));
164}
165
166void
167RMD160Transform(u_int32_t state[5], const u_int8_t block[RMD160_BLOCK_LENGTH])
168{
169 u_int32_t a, b, c, d, e, aa, bb, cc, dd, ee, t, x[16];
170
171#if BYTE_ORDER == LITTLE_ENDIAN
172 memcpy(x, block, RMD160_BLOCK_LENGTH);
173#else
174 int i;
175
176 for (i = 0; i < 16; i++)
177 x[i] = (u_int32_t)(
178 (u_int32_t)(block[i*4 + 0]) |
179 (u_int32_t)(block[i*4 + 1]) << 8 |
180 (u_int32_t)(block[i*4 + 2]) << 16 |
181 (u_int32_t)(block[i*4 + 3]) << 24);
182#endif
183
184 a = state[0];
185 b = state[1];
186 c = state[2];
187 d = state[3];
188 e = state[4];
189
190 /* Round 1 */
191 R(a, b, c, d, e, F0, K0, 11, 0);
192 R(e, a, b, c, d, F0, K0, 14, 1);
193 R(d, e, a, b, c, F0, K0, 15, 2);
194 R(c, d, e, a, b, F0, K0, 12, 3);
195 R(b, c, d, e, a, F0, K0, 5, 4);
196 R(a, b, c, d, e, F0, K0, 8, 5);
197 R(e, a, b, c, d, F0, K0, 7, 6);
198 R(d, e, a, b, c, F0, K0, 9, 7);
199 R(c, d, e, a, b, F0, K0, 11, 8);
200 R(b, c, d, e, a, F0, K0, 13, 9);
201 R(a, b, c, d, e, F0, K0, 14, 10);
202 R(e, a, b, c, d, F0, K0, 15, 11);
203 R(d, e, a, b, c, F0, K0, 6, 12);
204 R(c, d, e, a, b, F0, K0, 7, 13);
205 R(b, c, d, e, a, F0, K0, 9, 14);
206 R(a, b, c, d, e, F0, K0, 8, 15); /* #15 */
207 /* Round 2 */
208 R(e, a, b, c, d, F1, K1, 7, 7);
209 R(d, e, a, b, c, F1, K1, 6, 4);
210 R(c, d, e, a, b, F1, K1, 8, 13);
211 R(b, c, d, e, a, F1, K1, 13, 1);
212 R(a, b, c, d, e, F1, K1, 11, 10);
213 R(e, a, b, c, d, F1, K1, 9, 6);
214 R(d, e, a, b, c, F1, K1, 7, 15);
215 R(c, d, e, a, b, F1, K1, 15, 3);
216 R(b, c, d, e, a, F1, K1, 7, 12);
217 R(a, b, c, d, e, F1, K1, 12, 0);
218 R(e, a, b, c, d, F1, K1, 15, 9);
219 R(d, e, a, b, c, F1, K1, 9, 5);
220 R(c, d, e, a, b, F1, K1, 11, 2);
221 R(b, c, d, e, a, F1, K1, 7, 14);
222 R(a, b, c, d, e, F1, K1, 13, 11);
223 R(e, a, b, c, d, F1, K1, 12, 8); /* #31 */
224 /* Round 3 */
225 R(d, e, a, b, c, F2, K2, 11, 3);
226 R(c, d, e, a, b, F2, K2, 13, 10);
227 R(b, c, d, e, a, F2, K2, 6, 14);
228 R(a, b, c, d, e, F2, K2, 7, 4);
229 R(e, a, b, c, d, F2, K2, 14, 9);
230 R(d, e, a, b, c, F2, K2, 9, 15);
231 R(c, d, e, a, b, F2, K2, 13, 8);
232 R(b, c, d, e, a, F2, K2, 15, 1);
233 R(a, b, c, d, e, F2, K2, 14, 2);
234 R(e, a, b, c, d, F2, K2, 8, 7);
235 R(d, e, a, b, c, F2, K2, 13, 0);
236 R(c, d, e, a, b, F2, K2, 6, 6);
237 R(b, c, d, e, a, F2, K2, 5, 13);
238 R(a, b, c, d, e, F2, K2, 12, 11);
239 R(e, a, b, c, d, F2, K2, 7, 5);
240 R(d, e, a, b, c, F2, K2, 5, 12); /* #47 */
241 /* Round 4 */
242 R(c, d, e, a, b, F3, K3, 11, 1);
243 R(b, c, d, e, a, F3, K3, 12, 9);
244 R(a, b, c, d, e, F3, K3, 14, 11);
245 R(e, a, b, c, d, F3, K3, 15, 10);
246 R(d, e, a, b, c, F3, K3, 14, 0);
247 R(c, d, e, a, b, F3, K3, 15, 8);
248 R(b, c, d, e, a, F3, K3, 9, 12);
249 R(a, b, c, d, e, F3, K3, 8, 4);
250 R(e, a, b, c, d, F3, K3, 9, 13);
251 R(d, e, a, b, c, F3, K3, 14, 3);
252 R(c, d, e, a, b, F3, K3, 5, 7);
253 R(b, c, d, e, a, F3, K3, 6, 15);
254 R(a, b, c, d, e, F3, K3, 8, 14);
255 R(e, a, b, c, d, F3, K3, 6, 5);
256 R(d, e, a, b, c, F3, K3, 5, 6);
257 R(c, d, e, a, b, F3, K3, 12, 2); /* #63 */
258 /* Round 5 */
259 R(b, c, d, e, a, F4, K4, 9, 4);
260 R(a, b, c, d, e, F4, K4, 15, 0);
261 R(e, a, b, c, d, F4, K4, 5, 5);
262 R(d, e, a, b, c, F4, K4, 11, 9);
263 R(c, d, e, a, b, F4, K4, 6, 7);
264 R(b, c, d, e, a, F4, K4, 8, 12);
265 R(a, b, c, d, e, F4, K4, 13, 2);
266 R(e, a, b, c, d, F4, K4, 12, 10);
267 R(d, e, a, b, c, F4, K4, 5, 14);
268 R(c, d, e, a, b, F4, K4, 12, 1);
269 R(b, c, d, e, a, F4, K4, 13, 3);
270 R(a, b, c, d, e, F4, K4, 14, 8);
271 R(e, a, b, c, d, F4, K4, 11, 11);
272 R(d, e, a, b, c, F4, K4, 8, 6);
273 R(c, d, e, a, b, F4, K4, 5, 15);
274 R(b, c, d, e, a, F4, K4, 6, 13); /* #79 */
275
276 aa = a ; bb = b; cc = c; dd = d; ee = e;
277
278 a = state[0];
279 b = state[1];
280 c = state[2];
281 d = state[3];
282 e = state[4];
283
284 /* Parallel round 1 */
285 R(a, b, c, d, e, F4, KK0, 8, 5);
286 R(e, a, b, c, d, F4, KK0, 9, 14);
287 R(d, e, a, b, c, F4, KK0, 9, 7);
288 R(c, d, e, a, b, F4, KK0, 11, 0);
289 R(b, c, d, e, a, F4, KK0, 13, 9);
290 R(a, b, c, d, e, F4, KK0, 15, 2);
291 R(e, a, b, c, d, F4, KK0, 15, 11);
292 R(d, e, a, b, c, F4, KK0, 5, 4);
293 R(c, d, e, a, b, F4, KK0, 7, 13);
294 R(b, c, d, e, a, F4, KK0, 7, 6);
295 R(a, b, c, d, e, F4, KK0, 8, 15);
296 R(e, a, b, c, d, F4, KK0, 11, 8);
297 R(d, e, a, b, c, F4, KK0, 14, 1);
298 R(c, d, e, a, b, F4, KK0, 14, 10);
299 R(b, c, d, e, a, F4, KK0, 12, 3);
300 R(a, b, c, d, e, F4, KK0, 6, 12); /* #15 */
301 /* Parallel round 2 */
302 R(e, a, b, c, d, F3, KK1, 9, 6);
303 R(d, e, a, b, c, F3, KK1, 13, 11);
304 R(c, d, e, a, b, F3, KK1, 15, 3);
305 R(b, c, d, e, a, F3, KK1, 7, 7);
306 R(a, b, c, d, e, F3, KK1, 12, 0);
307 R(e, a, b, c, d, F3, KK1, 8, 13);
308 R(d, e, a, b, c, F3, KK1, 9, 5);
309 R(c, d, e, a, b, F3, KK1, 11, 10);
310 R(b, c, d, e, a, F3, KK1, 7, 14);
311 R(a, b, c, d, e, F3, KK1, 7, 15);
312 R(e, a, b, c, d, F3, KK1, 12, 8);
313 R(d, e, a, b, c, F3, KK1, 7, 12);
314 R(c, d, e, a, b, F3, KK1, 6, 4);
315 R(b, c, d, e, a, F3, KK1, 15, 9);
316 R(a, b, c, d, e, F3, KK1, 13, 1);
317 R(e, a, b, c, d, F3, KK1, 11, 2); /* #31 */
318 /* Parallel round 3 */
319 R(d, e, a, b, c, F2, KK2, 9, 15);
320 R(c, d, e, a, b, F2, KK2, 7, 5);
321 R(b, c, d, e, a, F2, KK2, 15, 1);
322 R(a, b, c, d, e, F2, KK2, 11, 3);
323 R(e, a, b, c, d, F2, KK2, 8, 7);
324 R(d, e, a, b, c, F2, KK2, 6, 14);
325 R(c, d, e, a, b, F2, KK2, 6, 6);
326 R(b, c, d, e, a, F2, KK2, 14, 9);
327 R(a, b, c, d, e, F2, KK2, 12, 11);
328 R(e, a, b, c, d, F2, KK2, 13, 8);
329 R(d, e, a, b, c, F2, KK2, 5, 12);
330 R(c, d, e, a, b, F2, KK2, 14, 2);
331 R(b, c, d, e, a, F2, KK2, 13, 10);
332 R(a, b, c, d, e, F2, KK2, 13, 0);
333 R(e, a, b, c, d, F2, KK2, 7, 4);
334 R(d, e, a, b, c, F2, KK2, 5, 13); /* #47 */
335 /* Parallel round 4 */
336 R(c, d, e, a, b, F1, KK3, 15, 8);
337 R(b, c, d, e, a, F1, KK3, 5, 6);
338 R(a, b, c, d, e, F1, KK3, 8, 4);
339 R(e, a, b, c, d, F1, KK3, 11, 1);
340 R(d, e, a, b, c, F1, KK3, 14, 3);
341 R(c, d, e, a, b, F1, KK3, 14, 11);
342 R(b, c, d, e, a, F1, KK3, 6, 15);
343 R(a, b, c, d, e, F1, KK3, 14, 0);
344 R(e, a, b, c, d, F1, KK3, 6, 5);
345 R(d, e, a, b, c, F1, KK3, 9, 12);
346 R(c, d, e, a, b, F1, KK3, 12, 2);
347 R(b, c, d, e, a, F1, KK3, 9, 13);
348 R(a, b, c, d, e, F1, KK3, 12, 9);
349 R(e, a, b, c, d, F1, KK3, 5, 7);
350 R(d, e, a, b, c, F1, KK3, 15, 10);
351 R(c, d, e, a, b, F1, KK3, 8, 14); /* #63 */
352 /* Parallel round 5 */
353 R(b, c, d, e, a, F0, KK4, 8, 12);
354 R(a, b, c, d, e, F0, KK4, 5, 15);
355 R(e, a, b, c, d, F0, KK4, 12, 10);
356 R(d, e, a, b, c, F0, KK4, 9, 4);
357 R(c, d, e, a, b, F0, KK4, 12, 1);
358 R(b, c, d, e, a, F0, KK4, 5, 5);
359 R(a, b, c, d, e, F0, KK4, 14, 8);
360 R(e, a, b, c, d, F0, KK4, 6, 7);
361 R(d, e, a, b, c, F0, KK4, 8, 6);
362 R(c, d, e, a, b, F0, KK4, 13, 2);
363 R(b, c, d, e, a, F0, KK4, 6, 13);
364 R(a, b, c, d, e, F0, KK4, 5, 14);
365 R(e, a, b, c, d, F0, KK4, 15, 0);
366 R(d, e, a, b, c, F0, KK4, 13, 3);
367 R(c, d, e, a, b, F0, KK4, 11, 9);
368 R(b, c, d, e, a, F0, KK4, 11, 11); /* #79 */
369
370 t = state[1] + cc + d;
371 state[1] = state[2] + dd + e;
372 state[2] = state[3] + ee + a;
373 state[3] = state[4] + aa + b;
374 state[4] = state[0] + bb + c;
375 state[0] = t;
376}
377
378#endif /* !WITH_OPENSSL */
diff --git a/openbsd-compat/rmd160.h b/openbsd-compat/rmd160.h
deleted file mode 100644
index 99c1dcdc0..000000000
--- a/openbsd-compat/rmd160.h
+++ /dev/null
@@ -1,61 +0,0 @@
1/* $OpenBSD: rmd160.h,v 1.17 2012/12/05 23:19:57 deraadt Exp $ */
2/*
3 * Copyright (c) 2001 Markus Friedl. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25#ifndef _RMD160_H
26#define _RMD160_H
27
28#ifndef WITH_OPENSSL
29
30#define RMD160_BLOCK_LENGTH 64
31#define RMD160_DIGEST_LENGTH 20
32#define RMD160_DIGEST_STRING_LENGTH (RMD160_DIGEST_LENGTH * 2 + 1)
33
34/* RMD160 context. */
35typedef struct RMD160Context {
36 u_int32_t state[5]; /* state */
37 u_int64_t count; /* number of bits, mod 2^64 */
38 u_int8_t buffer[RMD160_BLOCK_LENGTH]; /* input buffer */
39} RMD160_CTX;
40
41void RMD160Init(RMD160_CTX *);
42void RMD160Transform(u_int32_t [5], const u_int8_t [RMD160_BLOCK_LENGTH])
43 __attribute__((__bounded__(__minbytes__,1,5)))
44 __attribute__((__bounded__(__minbytes__,2,RMD160_BLOCK_LENGTH)));
45void RMD160Update(RMD160_CTX *, const u_int8_t *, size_t)
46 __attribute__((__bounded__(__string__,2,3)));
47void RMD160Pad(RMD160_CTX *);
48void RMD160Final(u_int8_t [RMD160_DIGEST_LENGTH], RMD160_CTX *)
49 __attribute__((__bounded__(__minbytes__,1,RMD160_DIGEST_LENGTH)));
50char *RMD160End(RMD160_CTX *, char *)
51 __attribute__((__bounded__(__minbytes__,2,RMD160_DIGEST_STRING_LENGTH)));
52char *RMD160File(const char *, char *)
53 __attribute__((__bounded__(__minbytes__,2,RMD160_DIGEST_STRING_LENGTH)));
54char *RMD160FileChunk(const char *, char *, off_t, off_t)
55 __attribute__((__bounded__(__minbytes__,2,RMD160_DIGEST_STRING_LENGTH)));
56char *RMD160Data(const u_int8_t *, size_t, char *)
57 __attribute__((__bounded__(__string__,1,2)))
58 __attribute__((__bounded__(__minbytes__,3,RMD160_DIGEST_STRING_LENGTH)));
59
60#endif /* !WITH_OPENSSL */
61#endif /* _RMD160_H */
diff --git a/openbsd-compat/sha2.c b/openbsd-compat/sha2.c
index e63324c99..e36cc24ef 100644
--- a/openbsd-compat/sha2.c
+++ b/openbsd-compat/sha2.c
@@ -42,7 +42,7 @@
42 !defined(HAVE_SHA512UPDATE) 42 !defined(HAVE_SHA512UPDATE)
43 43
44/* no-op out, similar to DEF_WEAK but only needed here */ 44/* no-op out, similar to DEF_WEAK but only needed here */
45#define MAKE_CLONE(x, y) void __ssh_compat_make_clone_##x_##y(void); 45#define MAKE_CLONE(x, y) void __ssh_compat_make_clone_##x_##y(void)
46 46
47#include <string.h> 47#include <string.h>
48#include <sha2.h> 48#include <sha2.h>