summaryrefslogtreecommitdiff
path: root/openbsd-compat
diff options
context:
space:
mode:
Diffstat (limited to 'openbsd-compat')
-rw-r--r--openbsd-compat/Makefile.in6
-rw-r--r--openbsd-compat/bsd-cygwin_util.c1
-rw-r--r--openbsd-compat/bsd-cygwin_util.h8
-rw-r--r--openbsd-compat/bsd-misc.h4
-rw-r--r--openbsd-compat/getcwd.c2
-rw-r--r--openbsd-compat/getgrouplist.c2
-rw-r--r--openbsd-compat/getrrsetbyname-ldns.c284
-rw-r--r--openbsd-compat/getrrsetbyname.c4
-rw-r--r--openbsd-compat/glob.c69
-rw-r--r--openbsd-compat/inet_ntop.c8
-rw-r--r--openbsd-compat/mktemp.c239
-rw-r--r--openbsd-compat/openbsd-compat.h4
-rw-r--r--openbsd-compat/openssl-compat.h7
-rw-r--r--openbsd-compat/port-linux.c7
-rw-r--r--openbsd-compat/setenv.c123
-rw-r--r--[-rwxr-xr-x]openbsd-compat/sha2.c2
-rw-r--r--[-rwxr-xr-x]openbsd-compat/sha2.h2
-rw-r--r--openbsd-compat/strlcpy.c10
-rw-r--r--openbsd-compat/strnlen.c37
19 files changed, 624 insertions, 195 deletions
diff --git a/openbsd-compat/Makefile.in b/openbsd-compat/Makefile.in
index 41b22d837..196a81d13 100644
--- a/openbsd-compat/Makefile.in
+++ b/openbsd-compat/Makefile.in
@@ -1,4 +1,4 @@
1# $Id: Makefile.in,v 1.46 2010/10/07 11:19:24 djm Exp $ 1# $Id: Makefile.in,v 1.48 2011/11/04 00:25:25 dtucker Exp $
2 2
3sysconfdir=@sysconfdir@ 3sysconfdir=@sysconfdir@
4piddir=@piddir@ 4piddir=@piddir@
@@ -16,9 +16,9 @@ RANLIB=@RANLIB@
16INSTALL=@INSTALL@ 16INSTALL=@INSTALL@
17LDFLAGS=-L. @LDFLAGS@ 17LDFLAGS=-L. @LDFLAGS@
18 18
19OPENBSD=base64.o basename.o bindresvport.o daemon.o dirname.o fmt_scaled.o getcwd.o getgrouplist.o getopt.o getrrsetbyname.o glob.o inet_aton.o inet_ntoa.o inet_ntop.o mktemp.o pwcache.o readpassphrase.o realpath.o rresvport.o setenv.o setproctitle.o sha2.o sigact.o strlcat.o strlcpy.o strmode.o strptime.o strsep.o strtonum.o strtoll.o strtoul.o timingsafe_bcmp.o vis.o 19OPENBSD=base64.o basename.o bindresvport.o daemon.o dirname.o fmt_scaled.o getcwd.o getgrouplist.o getopt.o getrrsetbyname.o glob.o inet_aton.o inet_ntoa.o inet_ntop.o mktemp.o pwcache.o readpassphrase.o realpath.o rresvport.o setenv.o setproctitle.o sha2.o sigact.o strlcat.o strlcpy.o strmode.o strnlen.o strptime.o strsep.o strtonum.o strtoll.o strtoul.o timingsafe_bcmp.o vis.o
20 20
21COMPAT=bsd-arc4random.o bsd-asprintf.o bsd-closefrom.o bsd-cray.o bsd-cygwin_util.o bsd-getpeereid.o bsd-misc.o bsd-nextstep.o bsd-openpty.o bsd-poll.o bsd-snprintf.o bsd-statvfs.o bsd-waitpid.o fake-rfc2553.o openssl-compat.o xmmap.o xcrypt.o 21COMPAT=bsd-arc4random.o bsd-asprintf.o bsd-closefrom.o bsd-cray.o bsd-cygwin_util.o bsd-getpeereid.o getrrsetbyname-ldns.o bsd-misc.o bsd-nextstep.o bsd-openpty.o bsd-poll.o bsd-snprintf.o bsd-statvfs.o bsd-waitpid.o fake-rfc2553.o openssl-compat.o xmmap.o xcrypt.o
22 22
23PORTS=port-aix.o port-irix.o port-linux.o port-solaris.o port-tun.o port-uw.o 23PORTS=port-aix.o port-irix.o port-linux.o port-solaris.o port-tun.o port-uw.o
24 24
diff --git a/openbsd-compat/bsd-cygwin_util.c b/openbsd-compat/bsd-cygwin_util.c
index 9eedc88d2..6befc016f 100644
--- a/openbsd-compat/bsd-cygwin_util.c
+++ b/openbsd-compat/bsd-cygwin_util.c
@@ -76,6 +76,7 @@ static struct wenv {
76 { NL("OS=") }, 76 { NL("OS=") },
77 { NL("PATH=") }, 77 { NL("PATH=") },
78 { NL("PATHEXT=") }, 78 { NL("PATHEXT=") },
79 { NL("PROGRAMFILES=") },
79 { NL("SYSTEMDRIVE=") }, 80 { NL("SYSTEMDRIVE=") },
80 { NL("SYSTEMROOT=") }, 81 { NL("SYSTEMROOT=") },
81 { NL("WINDIR=") } 82 { NL("WINDIR=") }
diff --git a/openbsd-compat/bsd-cygwin_util.h b/openbsd-compat/bsd-cygwin_util.h
index 48f64b740..d223792d7 100644
--- a/openbsd-compat/bsd-cygwin_util.h
+++ b/openbsd-compat/bsd-cygwin_util.h
@@ -1,4 +1,4 @@
1/* $Id: bsd-cygwin_util.h,v 1.13 2011/08/17 01:31:09 djm Exp $ */ 1/* $Id: bsd-cygwin_util.h,v 1.14 2012/03/30 03:07:07 djm Exp $ */
2 2
3/* 3/*
4 * Copyright (c) 2000, 2001, 2011 Corinna Vinschen <vinschen@redhat.com> 4 * Copyright (c) 2000, 2001, 2011 Corinna Vinschen <vinschen@redhat.com>
@@ -40,6 +40,12 @@
40#include <sys/cygwin.h> 40#include <sys/cygwin.h>
41#include <io.h> 41#include <io.h>
42 42
43/* Make sure _WIN32 isn't defined later in the code, otherwise headers from
44 other packages might get the wrong idea about the target system. */
45#ifdef _WIN32
46#undef _WIN32
47#endif
48
43int binary_open(const char *, int , ...); 49int binary_open(const char *, int , ...);
44int check_ntsec(const char *); 50int check_ntsec(const char *);
45char **fetch_windows_environment(void); 51char **fetch_windows_environment(void);
diff --git a/openbsd-compat/bsd-misc.h b/openbsd-compat/bsd-misc.h
index e70c3f9e9..e37175625 100644
--- a/openbsd-compat/bsd-misc.h
+++ b/openbsd-compat/bsd-misc.h
@@ -1,4 +1,4 @@
1/* $Id: bsd-misc.h,v 1.19 2010/11/08 22:26:23 tim Exp $ */ 1/* $Id: bsd-misc.h,v 1.20 2012/02/14 18:03:31 tim Exp $ */
2 2
3/* 3/*
4 * Copyright (c) 1999-2004 Damien Miller <djm@mindrot.org> 4 * Copyright (c) 1999-2004 Damien Miller <djm@mindrot.org>
@@ -86,7 +86,7 @@ int tcsendbreak(int, int);
86#endif 86#endif
87 87
88#ifndef HAVE_UNSETENV 88#ifndef HAVE_UNSETENV
89void unsetenv(const char *); 89int unsetenv(const char *);
90#endif 90#endif
91 91
92/* wrapper for signal interface */ 92/* wrapper for signal interface */
diff --git a/openbsd-compat/getcwd.c b/openbsd-compat/getcwd.c
index 711cb9cd5..3edbb9cba 100644
--- a/openbsd-compat/getcwd.c
+++ b/openbsd-compat/getcwd.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: getcwd.c,v 1.14 2005/08/08 08:05:34 espie Exp $ */ 1/* from OpenBSD: getcwd.c,v 1.14 2005/08/08 08:05:34 espie Exp */
2/* 2/*
3 * Copyright (c) 1989, 1991, 1993 3 * Copyright (c) 1989, 1991, 1993
4 * The Regents of the University of California. All rights reserved. 4 * The Regents of the University of California. All rights reserved.
diff --git a/openbsd-compat/getgrouplist.c b/openbsd-compat/getgrouplist.c
index a57d7d388..3afcb9281 100644
--- a/openbsd-compat/getgrouplist.c
+++ b/openbsd-compat/getgrouplist.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: getgrouplist.c,v 1.12 2005/08/08 08:05:34 espie Exp $ */ 1/* from OpenBSD: getgrouplist.c,v 1.12 2005/08/08 08:05:34 espie Exp */
2/* 2/*
3 * Copyright (c) 1991, 1993 3 * Copyright (c) 1991, 1993
4 * The Regents of the University of California. All rights reserved. 4 * The Regents of the University of California. All rights reserved.
diff --git a/openbsd-compat/getrrsetbyname-ldns.c b/openbsd-compat/getrrsetbyname-ldns.c
new file mode 100644
index 000000000..8ce5678c9
--- /dev/null
+++ b/openbsd-compat/getrrsetbyname-ldns.c
@@ -0,0 +1,284 @@
1/* $OpenBSD: getrrsetbyname.c,v 1.10 2005/03/30 02:58:28 tedu Exp $ */
2
3/*
4 * Copyright (c) 2007 Simon Vallet / Genoscope <svallet@genoscope.cns.fr>
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 *
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 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * 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 * Portions Copyright (c) 1999-2001 Internet Software Consortium.
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 INTERNET SOFTWARE CONSORTIUM
37 * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
38 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
39 * INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
40 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
41 * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
42 * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
43 * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
44 */
45
46#include "includes.h"
47
48#if !defined (HAVE_GETRRSETBYNAME) && defined (HAVE_LDNS)
49
50#include <stdlib.h>
51#include <string.h>
52
53#include <ldns/ldns.h>
54
55#include "getrrsetbyname.h"
56#include "log.h"
57#include "xmalloc.h"
58
59#define malloc(x) (xmalloc(x))
60#define calloc(x, y) (xcalloc((x),(y)))
61#define free(x) (xfree(x))
62
63int
64getrrsetbyname(const char *hostname, unsigned int rdclass,
65 unsigned int rdtype, unsigned int flags,
66 struct rrsetinfo **res)
67{
68 int result;
69 unsigned int i, j, index_ans, index_sig;
70 struct rrsetinfo *rrset = NULL;
71 struct rdatainfo *rdata;
72 size_t len;
73 ldns_resolver *ldns_res;
74 ldns_rdf *domain = NULL;
75 ldns_pkt *pkt = NULL;
76 ldns_rr_list *rrsigs = NULL, *rrdata = NULL;
77 ldns_status err;
78 ldns_rr *rr;
79
80 /* check for invalid class and type */
81 if (rdclass > 0xffff || rdtype > 0xffff) {
82 result = ERRSET_INVAL;
83 goto fail;
84 }
85
86 /* don't allow queries of class or type ANY */
87 if (rdclass == 0xff || rdtype == 0xff) {
88 result = ERRSET_INVAL;
89 goto fail;
90 }
91
92 /* don't allow flags yet, unimplemented */
93 if (flags) {
94 result = ERRSET_INVAL;
95 goto fail;
96 }
97
98 /* Initialize resolver from resolv.conf */
99 domain = ldns_dname_new_frm_str(hostname);
100 if ((err = ldns_resolver_new_frm_file(&ldns_res, NULL)) != \
101 LDNS_STATUS_OK) {
102 result = ERRSET_FAIL;
103 goto fail;
104 }
105
106#ifdef LDNS_DEBUG
107 ldns_resolver_set_debug(ldns_res, true);
108#endif /* LDNS_DEBUG */
109
110 ldns_resolver_set_dnssec(ldns_res, true); /* Use DNSSEC */
111
112 /* make query */
113 pkt = ldns_resolver_query(ldns_res, domain, rdtype, rdclass, LDNS_RD);
114
115 /*** TODO: finer errcodes -- see original **/
116 if (!pkt || ldns_pkt_ancount(pkt) < 1) {
117 result = ERRSET_FAIL;
118 goto fail;
119 }
120
121 /* initialize rrset */
122 rrset = calloc(1, sizeof(struct rrsetinfo));
123 if (rrset == NULL) {
124 result = ERRSET_NOMEMORY;
125 goto fail;
126 }
127
128 rrdata = ldns_pkt_rr_list_by_type(pkt, rdtype, LDNS_SECTION_ANSWER);
129 rrset->rri_nrdatas = ldns_rr_list_rr_count(rrdata);
130 if (!rrset->rri_nrdatas) {
131 result = ERRSET_NODATA;
132 goto fail;
133 }
134
135 /* copy name from answer section */
136 len = ldns_rdf_size(ldns_rr_owner(ldns_rr_list_rr(rrdata, 0)));
137 if ((rrset->rri_name = malloc(len)) == NULL) {
138 result = ERRSET_NOMEMORY;
139 goto fail;
140 }
141 memcpy(rrset->rri_name,
142 ldns_rdf_data(ldns_rr_owner(ldns_rr_list_rr(rrdata, 0))), len);
143
144 rrset->rri_rdclass = ldns_rr_get_class(ldns_rr_list_rr(rrdata, 0));
145 rrset->rri_rdtype = ldns_rr_get_type(ldns_rr_list_rr(rrdata, 0));
146 rrset->rri_ttl = ldns_rr_ttl(ldns_rr_list_rr(rrdata, 0));
147
148 debug2("ldns: got %u answers from DNS", rrset->rri_nrdatas);
149
150 /* Check for authenticated data */
151 if (ldns_pkt_ad(pkt)) {
152 rrset->rri_flags |= RRSET_VALIDATED;
153 } else { /* AD is not set, try autonomous validation */
154 ldns_rr_list * trusted_keys = ldns_rr_list_new();
155
156 debug2("ldns: trying to validate RRset");
157 /* Get eventual sigs */
158 rrsigs = ldns_pkt_rr_list_by_type(pkt, LDNS_RR_TYPE_RRSIG,
159 LDNS_SECTION_ANSWER);
160
161 rrset->rri_nsigs = ldns_rr_list_rr_count(rrsigs);
162 debug2("ldns: got %u signature(s) (RRTYPE %u) from DNS",
163 rrset->rri_nsigs, LDNS_RR_TYPE_RRSIG);
164
165 if ((err = ldns_verify_trusted(ldns_res, rrdata, rrsigs,
166 trusted_keys)) == LDNS_STATUS_OK) {
167 rrset->rri_flags |= RRSET_VALIDATED;
168 debug2("ldns: RRset is signed with a valid key");
169 } else {
170 debug2("ldns: RRset validation failed: %s",
171 ldns_get_errorstr_by_id(err));
172 }
173
174 ldns_rr_list_deep_free(trusted_keys);
175 }
176
177 /* allocate memory for answers */
178 rrset->rri_rdatas = calloc(rrset->rri_nrdatas,
179 sizeof(struct rdatainfo));
180
181 if (rrset->rri_rdatas == NULL) {
182 result = ERRSET_NOMEMORY;
183 goto fail;
184 }
185
186 /* allocate memory for signatures */
187 if (rrset->rri_nsigs > 0) {
188 rrset->rri_sigs = calloc(rrset->rri_nsigs,
189 sizeof(struct rdatainfo));
190
191 if (rrset->rri_sigs == NULL) {
192 result = ERRSET_NOMEMORY;
193 goto fail;
194 }
195 }
196
197 /* copy answers & signatures */
198 for (i=0, index_ans=0, index_sig=0; i< pkt->_header->_ancount; i++) {
199 rdata = NULL;
200 rr = ldns_rr_list_rr(ldns_pkt_answer(pkt), i);
201
202 if (ldns_rr_get_class(rr) == rrset->rri_rdclass &&
203 ldns_rr_get_type(rr) == rrset->rri_rdtype) {
204 rdata = &rrset->rri_rdatas[index_ans++];
205 }
206
207 if (rr->_rr_class == rrset->rri_rdclass &&
208 rr->_rr_type == LDNS_RR_TYPE_RRSIG) {
209 rdata = &rrset->rri_sigs[index_sig++];
210 }
211
212 if (rdata) {
213 size_t rdata_offset = 0;
214
215 rdata->rdi_length = 0;
216 for (j=0; j< rr->_rd_count; j++) {
217 rdata->rdi_length +=
218 ldns_rdf_size(ldns_rr_rdf(rr, j));
219 }
220
221 rdata->rdi_data = malloc(rdata->rdi_length);
222 if (rdata->rdi_data == NULL) {
223 result = ERRSET_NOMEMORY;
224 goto fail;
225 }
226
227 /* Re-create the raw DNS RDATA */
228 for (j=0; j< rr->_rd_count; j++) {
229 len = ldns_rdf_size(ldns_rr_rdf(rr, j));
230 memcpy(rdata->rdi_data + rdata_offset,
231 ldns_rdf_data(ldns_rr_rdf(rr, j)), len);
232 rdata_offset += len;
233 }
234 }
235 }
236
237 *res = rrset;
238 result = ERRSET_SUCCESS;
239
240fail:
241 /* freerrset(rrset); */
242 ldns_rdf_deep_free(domain);
243 ldns_pkt_free(pkt);
244 ldns_rr_list_deep_free(rrsigs);
245 ldns_rr_list_deep_free(rrdata);
246 ldns_resolver_deep_free(ldns_res);
247
248 return result;
249}
250
251
252void
253freerrset(struct rrsetinfo *rrset)
254{
255 u_int16_t i;
256
257 if (rrset == NULL)
258 return;
259
260 if (rrset->rri_rdatas) {
261 for (i = 0; i < rrset->rri_nrdatas; i++) {
262 if (rrset->rri_rdatas[i].rdi_data == NULL)
263 break;
264 free(rrset->rri_rdatas[i].rdi_data);
265 }
266 free(rrset->rri_rdatas);
267 }
268
269 if (rrset->rri_sigs) {
270 for (i = 0; i < rrset->rri_nsigs; i++) {
271 if (rrset->rri_sigs[i].rdi_data == NULL)
272 break;
273 free(rrset->rri_sigs[i].rdi_data);
274 }
275 free(rrset->rri_sigs);
276 }
277
278 if (rrset->rri_name)
279 free(rrset->rri_name);
280 free(rrset);
281}
282
283
284#endif /* !defined (HAVE_GETRRSETBYNAME) && defined (HAVE_LDNS) */
diff --git a/openbsd-compat/getrrsetbyname.c b/openbsd-compat/getrrsetbyname.c
index 98876673d..dc6fe0533 100644
--- a/openbsd-compat/getrrsetbyname.c
+++ b/openbsd-compat/getrrsetbyname.c
@@ -47,7 +47,7 @@
47 47
48#include "includes.h" 48#include "includes.h"
49 49
50#ifndef HAVE_GETRRSETBYNAME 50#if !defined (HAVE_GETRRSETBYNAME) && !defined (HAVE_LDNS)
51 51
52#include <stdlib.h> 52#include <stdlib.h>
53#include <string.h> 53#include <string.h>
@@ -607,4 +607,4 @@ count_dns_rr(struct dns_rr *p, u_int16_t class, u_int16_t type)
607 return (n); 607 return (n);
608} 608}
609 609
610#endif /* !defined(HAVE_GETRRSETBYNAME) */ 610#endif /* !defined (HAVE_GETRRSETBYNAME) && !defined (HAVE_LDNS) */
diff --git a/openbsd-compat/glob.c b/openbsd-compat/glob.c
index 0341225cd..742b4b954 100644
--- a/openbsd-compat/glob.c
+++ b/openbsd-compat/glob.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: glob.c,v 1.35 2011/01/12 01:53:14 djm Exp $ */ 1/* $OpenBSD: glob.c,v 1.38 2011/09/22 06:27:29 djm 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.
@@ -66,6 +66,7 @@
66#include <dirent.h> 66#include <dirent.h>
67#include <ctype.h> 67#include <ctype.h>
68#include <errno.h> 68#include <errno.h>
69#include <limits.h>
69#include <pwd.h> 70#include <pwd.h>
70#include <stdlib.h> 71#include <stdlib.h>
71#include <string.h> 72#include <string.h>
@@ -132,13 +133,22 @@ typedef char Char;
132#define GLOB_LIMIT_STAT 128 133#define GLOB_LIMIT_STAT 128
133#define GLOB_LIMIT_READDIR 16384 134#define GLOB_LIMIT_READDIR 16384
134 135
136/* Limit of recursion during matching attempts. */
137#define GLOB_LIMIT_RECUR 64
138
135struct glob_lim { 139struct glob_lim {
136 size_t glim_malloc; 140 size_t glim_malloc;
137 size_t glim_stat; 141 size_t glim_stat;
138 size_t glim_readdir; 142 size_t glim_readdir;
139}; 143};
140 144
145struct glob_path_stat {
146 char *gps_path;
147 struct stat *gps_stat;
148};
149
141static int compare(const void *, const void *); 150static int compare(const void *, const void *);
151static int compare_gps(const void *, const void *);
142static int g_Ctoc(const Char *, char *, u_int); 152static int g_Ctoc(const Char *, char *, u_int);
143static int g_lstat(Char *, struct stat *, glob_t *); 153static int g_lstat(Char *, struct stat *, glob_t *);
144static DIR *g_opendir(Char *, glob_t *); 154static DIR *g_opendir(Char *, glob_t *);
@@ -158,7 +168,7 @@ static const Char *
158static int globexp1(const Char *, glob_t *, struct glob_lim *); 168static int globexp1(const Char *, glob_t *, struct glob_lim *);
159static int globexp2(const Char *, const Char *, glob_t *, 169static int globexp2(const Char *, const Char *, glob_t *,
160 struct glob_lim *); 170 struct glob_lim *);
161static int match(Char *, Char *, Char *); 171static int match(Char *, Char *, Char *, int);
162#ifdef DEBUG 172#ifdef DEBUG
163static void qprintf(const char *, Char *); 173static void qprintf(const char *, Char *);
164#endif 174#endif
@@ -172,6 +182,9 @@ glob(const char *pattern, int flags, int (*errfunc)(const char *, int),
172 Char *bufnext, *bufend, patbuf[MAXPATHLEN]; 182 Char *bufnext, *bufend, patbuf[MAXPATHLEN];
173 struct glob_lim limit = { 0, 0, 0 }; 183 struct glob_lim limit = { 0, 0, 0 };
174 184
185 if (strnlen(pattern, PATH_MAX) == PATH_MAX)
186 return(GLOB_NOMATCH);
187
175 patnext = (u_char *) pattern; 188 patnext = (u_char *) pattern;
176 if (!(flags & GLOB_APPEND)) { 189 if (!(flags & GLOB_APPEND)) {
177 pglob->gl_pathc = 0; 190 pglob->gl_pathc = 0;
@@ -548,9 +561,32 @@ glob0(const Char *pattern, glob_t *pglob, struct glob_lim *limitp)
548 else 561 else
549 return(GLOB_NOMATCH); 562 return(GLOB_NOMATCH);
550 } 563 }
551 if (!(pglob->gl_flags & GLOB_NOSORT)) 564 if (!(pglob->gl_flags & GLOB_NOSORT)) {
552 qsort(pglob->gl_pathv + pglob->gl_offs + oldpathc, 565 if ((pglob->gl_flags & GLOB_KEEPSTAT)) {
553 pglob->gl_pathc - oldpathc, sizeof(char *), compare); 566 /* Keep the paths and stat info synced during sort */
567 struct glob_path_stat *path_stat;
568 int i;
569 int n = pglob->gl_pathc - oldpathc;
570 int o = pglob->gl_offs + oldpathc;
571
572 if ((path_stat = calloc(n, sizeof(*path_stat))) == NULL)
573 return GLOB_NOSPACE;
574 for (i = 0; i < n; i++) {
575 path_stat[i].gps_path = pglob->gl_pathv[o + i];
576 path_stat[i].gps_stat = pglob->gl_statv[o + i];
577 }
578 qsort(path_stat, n, sizeof(*path_stat), compare_gps);
579 for (i = 0; i < n; i++) {
580 pglob->gl_pathv[o + i] = path_stat[i].gps_path;
581 pglob->gl_statv[o + i] = path_stat[i].gps_stat;
582 }
583 free(path_stat);
584 } else {
585 qsort(pglob->gl_pathv + pglob->gl_offs + oldpathc,
586 pglob->gl_pathc - oldpathc, sizeof(char *),
587 compare);
588 }
589 }
554 return(0); 590 return(0);
555} 591}
556 592
@@ -561,6 +597,15 @@ compare(const void *p, const void *q)
561} 597}
562 598
563static int 599static int
600compare_gps(const void *_p, const void *_q)
601{
602 const struct glob_path_stat *p = (const struct glob_path_stat *)_p;
603 const struct glob_path_stat *q = (const struct glob_path_stat *)_q;
604
605 return(strcmp(p->gps_path, q->gps_path));
606}
607
608static int
564glob1(Char *pattern, Char *pattern_last, glob_t *pglob, struct glob_lim *limitp) 609glob1(Char *pattern, Char *pattern_last, glob_t *pglob, struct glob_lim *limitp)
565{ 610{
566 Char pathbuf[MAXPATHLEN]; 611 Char pathbuf[MAXPATHLEN];
@@ -697,7 +742,8 @@ glob3(Char *pathbuf, Char *pathbuf_last, Char *pathend, Char *pathend_last,
697 errno = 0; 742 errno = 0;
698 *pathend++ = SEP; 743 *pathend++ = SEP;
699 *pathend = EOS; 744 *pathend = EOS;
700 return(GLOB_NOSPACE); 745 err = GLOB_NOSPACE;
746 break;
701 } 747 }
702 748
703 /* Initial DOT must be matched literally. */ 749 /* Initial DOT must be matched literally. */
@@ -713,7 +759,7 @@ glob3(Char *pathbuf, Char *pathbuf_last, Char *pathend, Char *pathend_last,
713 break; 759 break;
714 } 760 }
715 761
716 if (!match(pathend, pattern, restpattern)) { 762 if (!match(pathend, pattern, restpattern, GLOB_LIMIT_RECUR)) {
717 *pathend = EOS; 763 *pathend = EOS;
718 continue; 764 continue;
719 } 765 }
@@ -850,19 +896,24 @@ globextend(const Char *path, glob_t *pglob, struct glob_lim *limitp,
850 * pattern causes a recursion level. 896 * pattern causes a recursion level.
851 */ 897 */
852static int 898static int
853match(Char *name, Char *pat, Char *patend) 899match(Char *name, Char *pat, Char *patend, int recur)
854{ 900{
855 int ok, negate_range; 901 int ok, negate_range;
856 Char c, k; 902 Char c, k;
857 903
904 if (recur-- == 0)
905 return(GLOB_NOSPACE);
906
858 while (pat < patend) { 907 while (pat < patend) {
859 c = *pat++; 908 c = *pat++;
860 switch (c & M_MASK) { 909 switch (c & M_MASK) {
861 case M_ALL: 910 case M_ALL:
911 while (pat < patend && (*pat & M_MASK) == M_ALL)
912 pat++; /* eat consecutive '*' */
862 if (pat == patend) 913 if (pat == patend)
863 return(1); 914 return(1);
864 do { 915 do {
865 if (match(name, pat, patend)) 916 if (match(name, pat, patend, recur))
866 return(1); 917 return(1);
867 } while (*name++ != EOS); 918 } while (*name++ != EOS);
868 return(0); 919 return(0);
diff --git a/openbsd-compat/inet_ntop.c b/openbsd-compat/inet_ntop.c
index e7ca4b7f8..3259037ba 100644
--- a/openbsd-compat/inet_ntop.c
+++ b/openbsd-compat/inet_ntop.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: inet_ntop.c,v 1.7 2005/08/06 20:30:03 espie Exp $ */ 1/* $OpenBSD: inet_ntop.c,v 1.8 2008/12/09 19:38:38 otto Exp $ */
2 2
3/* Copyright (c) 1996 by Internet Software Consortium. 3/* Copyright (c) 1996 by Internet Software Consortium.
4 * 4 *
@@ -57,13 +57,13 @@ static const char *inet_ntop6(const u_char *src, char *dst, size_t size);
57 * Paul Vixie, 1996. 57 * Paul Vixie, 1996.
58 */ 58 */
59const char * 59const char *
60inet_ntop(int af, const void *src, char *dst, size_t size) 60inet_ntop(int af, const void *src, char *dst, socklen_t size)
61{ 61{
62 switch (af) { 62 switch (af) {
63 case AF_INET: 63 case AF_INET:
64 return (inet_ntop4(src, dst, size)); 64 return (inet_ntop4(src, dst, (size_t)size));
65 case AF_INET6: 65 case AF_INET6:
66 return (inet_ntop6(src, dst, size)); 66 return (inet_ntop6(src, dst, (size_t)size));
67 default: 67 default:
68 errno = EAFNOSUPPORT; 68 errno = EAFNOSUPPORT;
69 return (NULL); 69 return (NULL);
diff --git a/openbsd-compat/mktemp.c b/openbsd-compat/mktemp.c
index 2285c84df..4eb52f421 100644
--- a/openbsd-compat/mktemp.c
+++ b/openbsd-compat/mktemp.c
@@ -1,34 +1,22 @@
1/* THIS FILE HAS BEEN MODIFIED FROM THE ORIGINAL OPENBSD SOURCE */ 1/* THIS FILE HAS BEEN MODIFIED FROM THE ORIGINAL OPENBSD SOURCE */
2/* Changes: Removed mktemp */ 2/* Changes: Removed mktemp */
3 3
4/* $OpenBSD: mktemp.c,v 1.19 2005/08/08 08:05:36 espie Exp $ */ 4/* $OpenBSD: mktemp.c,v 1.30 2010/03/21 23:09:30 schwarze Exp $ */
5/* 5/*
6 * Copyright (c) 1987, 1993 6 * Copyright (c) 1996-1998, 2008 Theo de Raadt
7 * The Regents of the University of California. All rights reserved. 7 * Copyright (c) 1997, 2008-2009 Todd C. Miller
8 * 8 *
9 * Redistribution and use in source and binary forms, with or without 9 * Permission to use, copy, modify, and distribute this software for any
10 * modification, are permitted provided that the following conditions 10 * purpose with or without fee is hereby granted, provided that the above
11 * are met: 11 * copyright notice and this permission notice appear in all copies.
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 * 12 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 13 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 14 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 15 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 16 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 17 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 18 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 19 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */ 20 */
33 21
34/* OPENBSD ORIGINAL: lib/libc/stdio/mktemp.c */ 22/* OPENBSD ORIGINAL: lib/libc/stdio/mktemp.c */
@@ -37,142 +25,117 @@
37 25
38#include <sys/types.h> 26#include <sys/types.h>
39#include <sys/stat.h> 27#include <sys/stat.h>
40 28#include <errno.h>
41#include <fcntl.h> 29#include <fcntl.h>
30#include <limits.h>
31#include <stdio.h>
32#include <stdlib.h>
33#include <string.h>
42#include <ctype.h> 34#include <ctype.h>
43#include <errno.h>
44#include <unistd.h> 35#include <unistd.h>
45 36
46#if !defined(HAVE_MKDTEMP) || defined(HAVE_STRICT_MKSTEMP) 37#if !defined(HAVE_MKDTEMP) || defined(HAVE_STRICT_MKSTEMP)
47 38
48static int _gettemp(char *, int *, int, int); 39#define MKTEMP_NAME 0
40#define MKTEMP_FILE 1
41#define MKTEMP_DIR 2
49 42
50int 43#define TEMPCHARS "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
51mkstemps(char *path, int slen) 44#define NUM_CHARS (sizeof(TEMPCHARS) - 1)
45
46static int
47mktemp_internal(char *path, int slen, int mode)
52{ 48{
49 char *start, *cp, *ep;
50 const char *tempchars = TEMPCHARS;
51 unsigned int r, tries;
52 struct stat sb;
53 size_t len;
53 int fd; 54 int fd;
54 55
55 return (_gettemp(path, &fd, 0, slen) ? fd : -1); 56 len = strlen(path);
57 if (len == 0 || slen < 0 || (size_t)slen >= len) {
58 errno = EINVAL;
59 return(-1);
60 }
61 ep = path + len - slen;
62
63 tries = 1;
64 for (start = ep; start > path && start[-1] == 'X'; start--) {
65 if (tries < INT_MAX / NUM_CHARS)
66 tries *= NUM_CHARS;
67 }
68 tries *= 2;
69
70 do {
71 for (cp = start; cp != ep; cp++) {
72 r = arc4random_uniform(NUM_CHARS);
73 *cp = tempchars[r];
74 }
75
76 switch (mode) {
77 case MKTEMP_NAME:
78 if (lstat(path, &sb) != 0)
79 return(errno == ENOENT ? 0 : -1);
80 break;
81 case MKTEMP_FILE:
82 fd = open(path, O_CREAT|O_EXCL|O_RDWR, S_IRUSR|S_IWUSR);
83 if (fd != -1 || errno != EEXIST)
84 return(fd);
85 break;
86 case MKTEMP_DIR:
87 if (mkdir(path, S_IRUSR|S_IWUSR|S_IXUSR) == 0)
88 return(0);
89 if (errno != EEXIST)
90 return(-1);
91 break;
92 }
93 } while (--tries);
94
95 errno = EEXIST;
96 return(-1);
56} 97}
57 98
58int 99#if 0
59mkstemp(char *path) 100char *_mktemp(char *);
60{
61 int fd;
62 101
63 return (_gettemp(path, &fd, 0, 0) ? fd : -1); 102char *
103_mktemp(char *path)
104{
105 if (mktemp_internal(path, 0, MKTEMP_NAME) == -1)
106 return(NULL);
107 return(path);
64} 108}
65 109
110__warn_references(mktemp,
111 "warning: mktemp() possibly used unsafely; consider using mkstemp()");
112
66char * 113char *
67mkdtemp(char *path) 114mktemp(char *path)
68{ 115{
69 return(_gettemp(path, (int *)NULL, 1, 0) ? path : (char *)NULL); 116 return(_mktemp(path));
70} 117}
118#endif
71 119
72static int 120int
73_gettemp(path, doopen, domkdir, slen) 121mkstemp(char *path)
74 char *path;
75 register int *doopen;
76 int domkdir;
77 int slen;
78{ 122{
79 register char *start, *trv, *suffp; 123 return(mktemp_internal(path, 0, MKTEMP_FILE));
80 struct stat sbuf; 124}
81 int rval;
82 pid_t pid;
83 125
84 if (doopen && domkdir) { 126int
85 errno = EINVAL; 127mkstemps(char *path, int slen)
86 return(0); 128{
87 } 129 return(mktemp_internal(path, slen, MKTEMP_FILE));
130}
88 131
89 for (trv = path; *trv; ++trv) 132char *
90 ; 133mkdtemp(char *path)
91 trv -= slen; 134{
92 suffp = trv; 135 int error;
93 --trv;
94 if (trv < path) {
95 errno = EINVAL;
96 return (0);
97 }
98 pid = getpid();
99 while (trv >= path && *trv == 'X' && pid != 0) {
100 *trv-- = (pid % 10) + '0';
101 pid /= 10;
102 }
103 while (trv >= path && *trv == 'X') {
104 char c;
105
106 pid = (arc4random() & 0xffff) % (26+26);
107 if (pid < 26)
108 c = pid + 'A';
109 else
110 c = (pid - 26) + 'a';
111 *trv-- = c;
112 }
113 start = trv + 1;
114
115 /*
116 * check the target directory; if you have six X's and it
117 * doesn't exist this runs for a *very* long time.
118 */
119 if (doopen || domkdir) {
120 for (;; --trv) {
121 if (trv <= path)
122 break;
123 if (*trv == '/') {
124 *trv = '\0';
125 rval = stat(path, &sbuf);
126 *trv = '/';
127 if (rval != 0)
128 return(0);
129 if (!S_ISDIR(sbuf.st_mode)) {
130 errno = ENOTDIR;
131 return(0);
132 }
133 break;
134 }
135 }
136 }
137 136
138 for (;;) { 137 error = mktemp_internal(path, 0, MKTEMP_DIR);
139 if (doopen) { 138 return(error ? NULL : path);
140 if ((*doopen =
141 open(path, O_CREAT|O_EXCL|O_RDWR, 0600)) >= 0)
142 return(1);
143 if (errno != EEXIST)
144 return(0);
145 } else if (domkdir) {
146 if (mkdir(path, 0700) == 0)
147 return(1);
148 if (errno != EEXIST)
149 return(0);
150 } else if (lstat(path, &sbuf))
151 return(errno == ENOENT ? 1 : 0);
152
153 /* tricky little algorithm for backward compatibility */
154 for (trv = start;;) {
155 if (!*trv)
156 return (0);
157 if (*trv == 'Z') {
158 if (trv == suffp)
159 return (0);
160 *trv++ = 'a';
161 } else {
162 if (isdigit(*trv))
163 *trv = 'a';
164 else if (*trv == 'z') /* inc from z to A */
165 *trv = 'A';
166 else {
167 if (trv == suffp)
168 return (0);
169 ++*trv;
170 }
171 break;
172 }
173 }
174 }
175 /*NOTREACHED*/
176} 139}
177 140
178#endif /* !defined(HAVE_MKDTEMP) || defined(HAVE_STRICT_MKSTEMP) */ 141#endif /* !defined(HAVE_MKDTEMP) || defined(HAVE_STRICT_MKSTEMP) */
diff --git a/openbsd-compat/openbsd-compat.h b/openbsd-compat/openbsd-compat.h
index 77c5ed2b1..807acf626 100644
--- a/openbsd-compat/openbsd-compat.h
+++ b/openbsd-compat/openbsd-compat.h
@@ -1,4 +1,4 @@
1/* $Id: openbsd-compat.h,v 1.51 2010/10/07 10:25:29 djm Exp $ */ 1/* $Id: openbsd-compat.h,v 1.52 2011/09/23 01:16:11 djm Exp $ */
2 2
3/* 3/*
4 * Copyright (c) 1999-2003 Damien Miller. All rights reserved. 4 * Copyright (c) 1999-2003 Damien Miller. All rights reserved.
@@ -116,7 +116,7 @@ char *inet_ntoa(struct in_addr in);
116#endif 116#endif
117 117
118#ifndef HAVE_INET_NTOP 118#ifndef HAVE_INET_NTOP
119const char *inet_ntop(int af, const void *src, char *dst, size_t size); 119const char *inet_ntop(int af, const void *src, char *dst, socklen_t size);
120#endif 120#endif
121 121
122#ifndef HAVE_INET_ATON 122#ifndef HAVE_INET_ATON
diff --git a/openbsd-compat/openssl-compat.h b/openbsd-compat/openssl-compat.h
index c5fc24eb4..a151eff38 100644
--- a/openbsd-compat/openssl-compat.h
+++ b/openbsd-compat/openssl-compat.h
@@ -1,4 +1,4 @@
1/* $Id: openssl-compat.h,v 1.19 2011/05/10 01:13:38 dtucker Exp $ */ 1/* $Id: openssl-compat.h,v 1.20 2012/01/17 03:03:39 dtucker Exp $ */
2 2
3/* 3/*
4 * Copyright (c) 2005 Darren Tucker <dtucker@zip.com.au> 4 * Copyright (c) 2005 Darren Tucker <dtucker@zip.com.au>
@@ -130,5 +130,10 @@ int ssh_EVP_CipherInit(EVP_CIPHER_CTX *, const EVP_CIPHER *, unsigned char *,
130int ssh_EVP_Cipher(EVP_CIPHER_CTX *, char *, char *, int); 130int ssh_EVP_Cipher(EVP_CIPHER_CTX *, char *, char *, int);
131int ssh_EVP_CIPHER_CTX_cleanup(EVP_CIPHER_CTX *); 131int ssh_EVP_CIPHER_CTX_cleanup(EVP_CIPHER_CTX *);
132void ssh_OpenSSL_add_all_algorithms(void); 132void ssh_OpenSSL_add_all_algorithms(void);
133
134# ifndef HAVE_HMAC_CTX_INIT
135# define HMAC_CTX_init(a)
136# endif
137
133#endif /* SSH_DONT_OVERLOAD_OPENSSL_FUNCS */ 138#endif /* SSH_DONT_OVERLOAD_OPENSSL_FUNCS */
134 139
diff --git a/openbsd-compat/port-linux.c b/openbsd-compat/port-linux.c
index ea8dff40f..aba75387c 100644
--- a/openbsd-compat/port-linux.c
+++ b/openbsd-compat/port-linux.c
@@ -1,4 +1,4 @@
1/* $Id: port-linux.c,v 1.16 2011/08/29 06:09:57 djm Exp $ */ 1/* $Id: port-linux.c,v 1.17 2012/03/08 23:25:18 djm Exp $ */
2 2
3/* 3/*
4 * Copyright (c) 2005 Daniel Walsh <dwalsh@redhat.com> 4 * Copyright (c) 2005 Daniel Walsh <dwalsh@redhat.com>
@@ -60,7 +60,7 @@ ssh_selinux_enabled(void)
60static security_context_t 60static security_context_t
61ssh_selinux_getctxbyname(char *pwname) 61ssh_selinux_getctxbyname(char *pwname)
62{ 62{
63 security_context_t sc; 63 security_context_t sc = NULL;
64 char *sename = NULL, *lvl = NULL; 64 char *sename = NULL, *lvl = NULL;
65 int r; 65 int r;
66 66
@@ -86,6 +86,7 @@ ssh_selinux_getctxbyname(char *pwname)
86 case 0: 86 case 0:
87 error("%s: Failed to get default SELinux security " 87 error("%s: Failed to get default SELinux security "
88 "context for %s", __func__, pwname); 88 "context for %s", __func__, pwname);
89 sc = NULL;
89 break; 90 break;
90 default: 91 default:
91 fatal("%s: Failed to get default SELinux security " 92 fatal("%s: Failed to get default SELinux security "
@@ -101,7 +102,7 @@ ssh_selinux_getctxbyname(char *pwname)
101 xfree(lvl); 102 xfree(lvl);
102#endif 103#endif
103 104
104 return (sc); 105 return sc;
105} 106}
106 107
107/* Set the execution context to the default for the specified user */ 108/* Set the execution context to the default for the specified user */
diff --git a/openbsd-compat/setenv.c b/openbsd-compat/setenv.c
index e2a8b6dd3..373b701d9 100644
--- a/openbsd-compat/setenv.c
+++ b/openbsd-compat/setenv.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: setenv.c,v 1.9 2005/08/08 08:05:37 espie Exp $ */ 1/* $OpenBSD: setenv.c,v 1.13 2010/08/23 22:31:50 millert Exp $ */
2/* 2/*
3 * Copyright (c) 1987 Regents of the University of California. 3 * Copyright (c) 1987 Regents of the University of California.
4 * All rights reserved. 4 * All rights reserved.
@@ -31,35 +31,38 @@
31/* OPENBSD ORIGINAL: lib/libc/stdlib/setenv.c */ 31/* OPENBSD ORIGINAL: lib/libc/stdlib/setenv.c */
32 32
33#include "includes.h" 33#include "includes.h"
34
34#if !defined(HAVE_SETENV) || !defined(HAVE_UNSETENV) 35#if !defined(HAVE_SETENV) || !defined(HAVE_UNSETENV)
35 36
37#include <errno.h>
36#include <stdlib.h> 38#include <stdlib.h>
37#include <string.h> 39#include <string.h>
38 40
39extern char **environ; 41extern char **environ;
42static char **lastenv; /* last value of environ */
40 43
41/* OpenSSH Portable: __findenv is from getenv.c rev 1.8, made static */ 44/* OpenSSH Portable: __findenv is from getenv.c rev 1.8, made static */
42/* 45/*
43 * __findenv -- 46 * __findenv --
44 * Returns pointer to value associated with name, if any, else NULL. 47 * Returns pointer to value associated with name, if any, else NULL.
48 * Starts searching within the environmental array at offset.
45 * Sets offset to be the offset of the name/value combination in the 49 * Sets offset to be the offset of the name/value combination in the
46 * environmental array, for use by setenv(3) and unsetenv(3). 50 * environmental array, for use by putenv(3), setenv(3) and unsetenv(3).
47 * Explicitly removes '=' in argument name. 51 * Explicitly removes '=' in argument name.
52 *
53 * This routine *should* be a static; don't use it.
48 */ 54 */
49static char * 55static char *
50__findenv(const char *name, size_t *offset) 56__findenv(const char *name, int len, int *offset)
51{ 57{
52 extern char **environ; 58 extern char **environ;
53 int len, i; 59 int i;
54 const char *np; 60 const char *np;
55 char **p, *cp; 61 char **p, *cp;
56 62
57 if (name == NULL || environ == NULL) 63 if (name == NULL || environ == NULL)
58 return (NULL); 64 return (NULL);
59 for (np = name; *np && *np != '='; ++np) 65 for (p = environ + *offset; (cp = *p) != NULL; ++p) {
60 ;
61 len = np - name;
62 for (p = environ; (cp = *p) != NULL; ++p) {
63 for (np = name, i = len; i && *cp; i--) 66 for (np = name, i = len; i && *cp; i--)
64 if (*cp++ != *np++) 67 if (*cp++ != *np++)
65 break; 68 break;
@@ -71,6 +74,54 @@ __findenv(const char *name, size_t *offset)
71 return (NULL); 74 return (NULL);
72} 75}
73 76
77#if 0 /* nothing uses putenv */
78/*
79 * putenv --
80 * Add a name=value string directly to the environmental, replacing
81 * any current value.
82 */
83int
84putenv(char *str)
85{
86 char **P, *cp;
87 size_t cnt;
88 int offset = 0;
89
90 for (cp = str; *cp && *cp != '='; ++cp)
91 ;
92 if (*cp != '=') {
93 errno = EINVAL;
94 return (-1); /* missing `=' in string */
95 }
96
97 if (__findenv(str, (int)(cp - str), &offset) != NULL) {
98 environ[offset++] = str;
99 /* could be set multiple times */
100 while (__findenv(str, (int)(cp - str), &offset)) {
101 for (P = &environ[offset];; ++P)
102 if (!(*P = *(P + 1)))
103 break;
104 }
105 return (0);
106 }
107
108 /* create new slot for string */
109 for (P = environ; *P != NULL; P++)
110 ;
111 cnt = P - environ;
112 P = (char **)realloc(lastenv, sizeof(char *) * (cnt + 2));
113 if (!P)
114 return (-1);
115 if (lastenv != environ)
116 memcpy(P, environ, cnt * sizeof(char *));
117 lastenv = environ = P;
118 environ[cnt] = str;
119 environ[cnt + 1] = NULL;
120 return (0);
121}
122
123#endif
124
74#ifndef HAVE_SETENV 125#ifndef HAVE_SETENV
75/* 126/*
76 * setenv -- 127 * setenv --
@@ -80,24 +131,39 @@ __findenv(const char *name, size_t *offset)
80int 131int
81setenv(const char *name, const char *value, int rewrite) 132setenv(const char *name, const char *value, int rewrite)
82{ 133{
83 static char **lastenv; /* last value of environ */ 134 char *C, **P;
84 char *C; 135 const char *np;
85 size_t l_value, offset; 136 int l_value, offset = 0;
137
138 for (np = name; *np && *np != '='; ++np)
139 ;
140#ifdef notyet
141 if (*np) {
142 errno = EINVAL;
143 return (-1); /* has `=' in name */
144 }
145#endif
86 146
87 if (*value == '=') /* no `=' in value */
88 ++value;
89 l_value = strlen(value); 147 l_value = strlen(value);
90 if ((C = __findenv(name, &offset))) { /* find if already exists */ 148 if ((C = __findenv(name, (int)(np - name), &offset)) != NULL) {
149 int tmpoff = offset + 1;
91 if (!rewrite) 150 if (!rewrite)
92 return (0); 151 return (0);
152#if 0 /* XXX - existing entry may not be writable */
93 if (strlen(C) >= l_value) { /* old larger; copy over */ 153 if (strlen(C) >= l_value) { /* old larger; copy over */
94 while ((*C++ = *value++)) 154 while ((*C++ = *value++))
95 ; 155 ;
96 return (0); 156 return (0);
97 } 157 }
158#endif
159 /* could be set multiple times */
160 while (__findenv(name, (int)(np - name), &tmpoff)) {
161 for (P = &environ[tmpoff];; ++P)
162 if (!(*P = *(P + 1)))
163 break;
164 }
98 } else { /* create new slot */ 165 } else { /* create new slot */
99 size_t cnt; 166 size_t cnt;
100 char **P;
101 167
102 for (P = environ; *P != NULL; P++) 168 for (P = environ; *P != NULL; P++)
103 ; 169 ;
@@ -111,10 +177,8 @@ setenv(const char *name, const char *value, int rewrite)
111 offset = cnt; 177 offset = cnt;
112 environ[cnt + 1] = NULL; 178 environ[cnt + 1] = NULL;
113 } 179 }
114 for (C = (char *)name; *C && *C != '='; ++C)
115 ; /* no `=' in name */
116 if (!(environ[offset] = /* name + `=' + value */ 180 if (!(environ[offset] = /* name + `=' + value */
117 malloc((size_t)((int)(C - name) + l_value + 2)))) 181 malloc((size_t)((int)(np - name) + l_value + 2))))
118 return (-1); 182 return (-1);
119 for (C = environ[offset]; (*C = *name++) && *C != '='; ++C) 183 for (C = environ[offset]; (*C = *name++) && *C != '='; ++C)
120 ; 184 ;
@@ -122,6 +186,7 @@ setenv(const char *name, const char *value, int rewrite)
122 ; 186 ;
123 return (0); 187 return (0);
124} 188}
189
125#endif /* HAVE_SETENV */ 190#endif /* HAVE_SETENV */
126 191
127#ifndef HAVE_UNSETENV 192#ifndef HAVE_UNSETENV
@@ -129,17 +194,33 @@ setenv(const char *name, const char *value, int rewrite)
129 * unsetenv(name) -- 194 * unsetenv(name) --
130 * Delete environmental variable "name". 195 * Delete environmental variable "name".
131 */ 196 */
132void 197int
133unsetenv(const char *name) 198unsetenv(const char *name)
134{ 199{
135 char **P; 200 char **P;
136 size_t offset; 201 const char *np;
202 int offset = 0;
137 203
138 while (__findenv(name, &offset)) /* if set multiple times */ 204 if (!name || !*name) {
205 errno = EINVAL;
206 return (-1);
207 }
208 for (np = name; *np && *np != '='; ++np)
209 ;
210 if (*np) {
211 errno = EINVAL;
212 return (-1); /* has `=' in name */
213 }
214
215 /* could be set multiple times */
216 while (__findenv(name, (int)(np - name), &offset)) {
139 for (P = &environ[offset];; ++P) 217 for (P = &environ[offset];; ++P)
140 if (!(*P = *(P + 1))) 218 if (!(*P = *(P + 1)))
141 break; 219 break;
220 }
221 return (0);
142} 222}
143#endif /* HAVE_UNSETENV */ 223#endif /* HAVE_UNSETENV */
144 224
145#endif /* !defined(HAVE_SETENV) || !defined(HAVE_UNSETENV) */ 225#endif /* !defined(HAVE_SETENV) || !defined(HAVE_UNSETENV) */
226
diff --git a/openbsd-compat/sha2.c b/openbsd-compat/sha2.c
index cf8e0ad66..f5bf74d1f 100755..100644
--- a/openbsd-compat/sha2.c
+++ b/openbsd-compat/sha2.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: sha2.c,v 1.11 2005/08/08 08:05:35 espie Exp $ */ 1/* from OpenBSD: sha2.c,v 1.11 2005/08/08 08:05:35 espie Exp */
2 2
3/* 3/*
4 * FILE: sha2.c 4 * FILE: sha2.c
diff --git a/openbsd-compat/sha2.h b/openbsd-compat/sha2.h
index 821f2dd6c..73e94f150 100755..100644
--- a/openbsd-compat/sha2.h
+++ b/openbsd-compat/sha2.h
@@ -1,4 +1,4 @@
1/* $OpenBSD: sha2.h,v 1.6 2004/06/22 01:57:30 jfb Exp $ */ 1/* OpenBSD: sha2.h,v 1.6 2004/06/22 01:57:30 jfb Exp */
2 2
3/* 3/*
4 * FILE: sha2.h 4 * FILE: sha2.h
diff --git a/openbsd-compat/strlcpy.c b/openbsd-compat/strlcpy.c
index 679a5b291..b4b1b6015 100644
--- a/openbsd-compat/strlcpy.c
+++ b/openbsd-compat/strlcpy.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: strlcpy.c,v 1.10 2005/08/08 08:05:37 espie Exp $ */ 1/* $OpenBSD: strlcpy.c,v 1.11 2006/05/05 15:27:38 millert Exp $ */
2 2
3/* 3/*
4 * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com> 4 * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
@@ -37,11 +37,11 @@ strlcpy(char *dst, const char *src, size_t siz)
37 size_t n = siz; 37 size_t n = siz;
38 38
39 /* Copy as many bytes as will fit */ 39 /* Copy as many bytes as will fit */
40 if (n != 0 && --n != 0) { 40 if (n != 0) {
41 do { 41 while (--n != 0) {
42 if ((*d++ = *s++) == 0) 42 if ((*d++ = *s++) == '\0')
43 break; 43 break;
44 } while (--n != 0); 44 }
45 } 45 }
46 46
47 /* Not enough room in dst, add NUL and traverse rest of src */ 47 /* Not enough room in dst, add NUL and traverse rest of src */
diff --git a/openbsd-compat/strnlen.c b/openbsd-compat/strnlen.c
new file mode 100644
index 000000000..93d515595
--- /dev/null
+++ b/openbsd-compat/strnlen.c
@@ -0,0 +1,37 @@
1/* $OpenBSD: strnlen.c,v 1.3 2010/06/02 12:58:12 millert Exp $ */
2
3/*
4 * Copyright (c) 2010 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/strnlen.c */
20
21#include "config.h"
22#ifndef HAVE_STRNLEN
23#include <sys/types.h>
24
25#include <string.h>
26
27size_t
28strnlen(const char *str, size_t maxlen)
29{
30 const char *cp;
31
32 for (cp = str; maxlen != 0 && *cp != '\0'; cp++, maxlen--)
33 ;
34
35 return (size_t)(cp - str);
36}
37#endif