summaryrefslogtreecommitdiff
path: root/openbsd-compat
diff options
context:
space:
mode:
Diffstat (limited to 'openbsd-compat')
-rw-r--r--openbsd-compat/Makefile.in4
-rw-r--r--openbsd-compat/bsd-cygwin_util.c2
-rw-r--r--openbsd-compat/bsd-cygwin_util.h2
-rw-r--r--openbsd-compat/bsd-misc.h14
-rw-r--r--openbsd-compat/getopt.c123
-rw-r--r--openbsd-compat/getopt.h74
-rw-r--r--openbsd-compat/getopt_long.c532
-rw-r--r--openbsd-compat/getrrsetbyname-ldns.c1
-rw-r--r--openbsd-compat/openbsd-compat.h12
-rw-r--r--openbsd-compat/port-aix.c10
-rw-r--r--openbsd-compat/port-linux.c12
-rw-r--r--openbsd-compat/xcrypt.c7
12 files changed, 649 insertions, 144 deletions
diff --git a/openbsd-compat/Makefile.in b/openbsd-compat/Makefile.in
index e1c3651e8..365cf006d 100644
--- a/openbsd-compat/Makefile.in
+++ b/openbsd-compat/Makefile.in
@@ -1,4 +1,4 @@
1# $Id: Makefile.in,v 1.50 2013/02/15 01:13:02 dtucker Exp $ 1# $Id: Makefile.in,v 1.51 2013/05/10 06:28:56 dtucker Exp $
2 2
3sysconfdir=@sysconfdir@ 3sysconfdir=@sysconfdir@
4piddir=@piddir@ 4piddir=@piddir@
@@ -16,7 +16,7 @@ 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 strnlen.o strptime.o strsep.o strtonum.o strtoll.o strtoul.o strtoull.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_long.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 strtoull.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 getrrsetbyname-ldns.o bsd-misc.o bsd-nextstep.o bsd-openpty.o bsd-poll.o bsd-setres_id.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-setres_id.o bsd-snprintf.o bsd-statvfs.o bsd-waitpid.o fake-rfc2553.o openssl-compat.o xmmap.o xcrypt.o
22 22
diff --git a/openbsd-compat/bsd-cygwin_util.c b/openbsd-compat/bsd-cygwin_util.c
index d3d2d913a..267e77a11 100644
--- a/openbsd-compat/bsd-cygwin_util.c
+++ b/openbsd-compat/bsd-cygwin_util.c
@@ -97,7 +97,7 @@ fetch_windows_environment(void)
97void 97void
98free_windows_environment(char **p) 98free_windows_environment(char **p)
99{ 99{
100 xfree(p); 100 free(p);
101} 101}
102 102
103#endif /* HAVE_CYGWIN */ 103#endif /* HAVE_CYGWIN */
diff --git a/openbsd-compat/bsd-cygwin_util.h b/openbsd-compat/bsd-cygwin_util.h
index 6061a6b01..372e41955 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.15.4.1 2013/04/04 23:53:31 dtucker Exp $ */ 1/* $Id: bsd-cygwin_util.h,v 1.16 2013/04/01 01:40:49 dtucker Exp $ */
2 2
3/* 3/*
4 * Copyright (c) 2000, 2001, 2011, 2013 Corinna Vinschen <vinschen@redhat.com> 4 * Copyright (c) 2000, 2001, 2011, 2013 Corinna Vinschen <vinschen@redhat.com>
diff --git a/openbsd-compat/bsd-misc.h b/openbsd-compat/bsd-misc.h
index 430066376..65c18ec2f 100644
--- a/openbsd-compat/bsd-misc.h
+++ b/openbsd-compat/bsd-misc.h
@@ -1,4 +1,4 @@
1/* $Id: bsd-misc.h,v 1.23 2013/03/14 23:34:27 djm Exp $ */ 1/* $Id: bsd-misc.h,v 1.25 2013/08/04 11:48:41 dtucker 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>
@@ -110,4 +110,16 @@ int isblank(int);
110pid_t getpgid(pid_t); 110pid_t getpgid(pid_t);
111#endif 111#endif
112 112
113#ifndef HAVE_ENDGRENT
114# define endgrent() {}
115#endif
116
117#ifndef HAVE_KRB5_GET_ERROR_MESSAGE
118# define krb5_get_error_message krb5_get_err_text
119#endif
120
121#ifndef HAVE_KRB5_FREE_ERROR_MESSAGE
122# define krb5_free_error_message(a,b) while(0)
123#endif
124
113#endif /* _BSD_MISC_H */ 125#endif /* _BSD_MISC_H */
diff --git a/openbsd-compat/getopt.c b/openbsd-compat/getopt.c
deleted file mode 100644
index 5450e43d9..000000000
--- a/openbsd-compat/getopt.c
+++ /dev/null
@@ -1,123 +0,0 @@
1/*
2 * Copyright (c) 1987, 1993, 1994
3 * The Regents of the University of California. 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 * 3. Neither the name of the University 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 REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * 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 THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30/* OPENBSD ORIGINAL: lib/libc/stdlib/getopt.c */
31
32#include "includes.h"
33#if !defined(HAVE_GETOPT) || !defined(HAVE_GETOPT_OPTRESET)
34
35#if defined(LIBC_SCCS) && !defined(lint)
36static char *rcsid = "$OpenBSD: getopt.c,v 1.5 2003/06/02 20:18:37 millert Exp $";
37#endif /* LIBC_SCCS and not lint */
38
39#include <stdio.h>
40#include <stdlib.h>
41#include <string.h>
42
43int BSDopterr = 1, /* if error message should be printed */
44 BSDoptind = 1, /* index into parent argv vector */
45 BSDoptopt, /* character checked for validity */
46 BSDoptreset; /* reset getopt */
47char *BSDoptarg; /* argument associated with option */
48
49#define BADCH (int)'?'
50#define BADARG (int)':'
51#define EMSG ""
52
53/*
54 * getopt --
55 * Parse argc/argv argument vector.
56 */
57int
58BSDgetopt(nargc, nargv, ostr)
59 int nargc;
60 char * const *nargv;
61 const char *ostr;
62{
63 extern char *__progname;
64 static char *place = EMSG; /* option letter processing */
65 char *oli; /* option letter list index */
66
67 if (ostr == NULL)
68 return (-1);
69
70 if (BSDoptreset || !*place) { /* update scanning pointer */
71 BSDoptreset = 0;
72 if (BSDoptind >= nargc || *(place = nargv[BSDoptind]) != '-') {
73 place = EMSG;
74 return (-1);
75 }
76 if (place[1] && *++place == '-') { /* found "--" */
77 ++BSDoptind;
78 place = EMSG;
79 return (-1);
80 }
81 } /* option letter okay? */
82 if ((BSDoptopt = (int)*place++) == (int)':' ||
83 !(oli = strchr(ostr, BSDoptopt))) {
84 /*
85 * if the user didn't specify '-' as an option,
86 * assume it means -1.
87 */
88 if (BSDoptopt == (int)'-')
89 return (-1);
90 if (!*place)
91 ++BSDoptind;
92 if (BSDopterr && *ostr != ':')
93 (void)fprintf(stderr,
94 "%s: illegal option -- %c\n", __progname, BSDoptopt);
95 return (BADCH);
96 }
97 if (*++oli != ':') { /* don't need argument */
98 BSDoptarg = NULL;
99 if (!*place)
100 ++BSDoptind;
101 }
102 else { /* need an argument */
103 if (*place) /* no white space */
104 BSDoptarg = place;
105 else if (nargc <= ++BSDoptind) { /* no arg */
106 place = EMSG;
107 if (*ostr == ':')
108 return (BADARG);
109 if (BSDopterr)
110 (void)fprintf(stderr,
111 "%s: option requires an argument -- %c\n",
112 __progname, BSDoptopt);
113 return (BADCH);
114 }
115 else /* white space */
116 BSDoptarg = nargv[BSDoptind];
117 place = EMSG;
118 ++BSDoptind;
119 }
120 return (BSDoptopt); /* dump back option letter */
121}
122
123#endif /* !defined(HAVE_GETOPT) || !defined(HAVE_OPTRESET) */
diff --git a/openbsd-compat/getopt.h b/openbsd-compat/getopt.h
new file mode 100644
index 000000000..8eb12447e
--- /dev/null
+++ b/openbsd-compat/getopt.h
@@ -0,0 +1,74 @@
1/* $OpenBSD: getopt.h,v 1.2 2008/06/26 05:42:04 ray Exp $ */
2/* $NetBSD: getopt.h,v 1.4 2000/07/07 10:43:54 ad Exp $ */
3
4/*-
5 * Copyright (c) 2000 The NetBSD Foundation, Inc.
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Dieter Baron and Thomas Klausner.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33#ifndef _GETOPT_H_
34#define _GETOPT_H_
35
36/*
37 * GNU-like getopt_long() and 4.4BSD getsubopt()/optreset extensions
38 */
39#define no_argument 0
40#define required_argument 1
41#define optional_argument 2
42
43struct option {
44 /* name of long option */
45 const char *name;
46 /*
47 * one of no_argument, required_argument, and optional_argument:
48 * whether option takes an argument
49 */
50 int has_arg;
51 /* if not NULL, set *flag to val when option found */
52 int *flag;
53 /* if flag not NULL, value to set *flag to; else return value */
54 int val;
55};
56
57int getopt_long(int, char * const *, const char *,
58 const struct option *, int *);
59int getopt_long_only(int, char * const *, const char *,
60 const struct option *, int *);
61#ifndef _GETOPT_DEFINED_
62#define _GETOPT_DEFINED_
63int getopt(int, char * const *, const char *);
64int getsubopt(char **, char * const *, char **);
65
66extern char *optarg; /* getopt(3) external variables */
67extern int opterr;
68extern int optind;
69extern int optopt;
70extern int optreset;
71extern char *suboptarg; /* getsubopt(3) external variable */
72#endif
73
74#endif /* !_GETOPT_H_ */
diff --git a/openbsd-compat/getopt_long.c b/openbsd-compat/getopt_long.c
new file mode 100644
index 000000000..e28947430
--- /dev/null
+++ b/openbsd-compat/getopt_long.c
@@ -0,0 +1,532 @@
1/* $OpenBSD: getopt_long.c,v 1.25 2011/03/05 22:10:11 guenther Exp $ */
2/* $NetBSD: getopt_long.c,v 1.15 2002/01/31 22:43:40 tv Exp $ */
3
4/*
5 * Copyright (c) 2002 Todd C. Miller <Todd.Miller@courtesan.com>
6 *
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 *
19 * Sponsored in part by the Defense Advanced Research Projects
20 * Agency (DARPA) and Air Force Research Laboratory, Air Force
21 * Materiel Command, USAF, under agreement number F39502-99-1-0512.
22 */
23/*-
24 * Copyright (c) 2000 The NetBSD Foundation, Inc.
25 * All rights reserved.
26 *
27 * This code is derived from software contributed to The NetBSD Foundation
28 * by Dieter Baron and Thomas Klausner.
29 *
30 * Redistribution and use in source and binary forms, with or without
31 * modification, are permitted provided that the following conditions
32 * are met:
33 * 1. Redistributions of source code must retain the above copyright
34 * notice, this list of conditions and the following disclaimer.
35 * 2. Redistributions in binary form must reproduce the above copyright
36 * notice, this list of conditions and the following disclaimer in the
37 * documentation and/or other materials provided with the distribution.
38 *
39 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
40 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
41 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
43 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
44 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
45 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
46 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
47 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
48 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
49 * POSSIBILITY OF SUCH DAMAGE.
50 */
51
52/* OPENBSD ORIGINAL: lib/libc/stdlib/getopt_long.c */
53#include "includes.h"
54
55#if !defined(HAVE_GETOPT) || !defined(HAVE_GETOPT_OPTRESET)
56
57/*
58 * Some defines to make it easier to keep the code in sync with upstream.
59 * getopt opterr optind optopt optreset optarg are all in defines.h which is
60 * pulled in by includes.h.
61 */
62#define warnx logit
63
64#if 0
65#include <err.h>
66#include <getopt.h>
67#endif
68#include <errno.h>
69#include <stdlib.h>
70#include <string.h>
71#include <stdarg.h>
72
73#include "log.h"
74
75int opterr = 1; /* if error message should be printed */
76int optind = 1; /* index into parent argv vector */
77int optopt = '?'; /* character checked for validity */
78int optreset; /* reset getopt */
79char *optarg; /* argument associated with option */
80
81#define PRINT_ERROR ((opterr) && (*options != ':'))
82
83#define FLAG_PERMUTE 0x01 /* permute non-options to the end of argv */
84#define FLAG_ALLARGS 0x02 /* treat non-options as args to option "-1" */
85#define FLAG_LONGONLY 0x04 /* operate as getopt_long_only */
86
87/* return values */
88#define BADCH (int)'?'
89#define BADARG ((*options == ':') ? (int)':' : (int)'?')
90#define INORDER (int)1
91
92#define EMSG ""
93
94static int getopt_internal(int, char * const *, const char *,
95 const struct option *, int *, int);
96static int parse_long_options(char * const *, const char *,
97 const struct option *, int *, int);
98static int gcd(int, int);
99static void permute_args(int, int, int, char * const *);
100
101static char *place = EMSG; /* option letter processing */
102
103/* XXX: set optreset to 1 rather than these two */
104static int nonopt_start = -1; /* first non option argument (for permute) */
105static int nonopt_end = -1; /* first option after non options (for permute) */
106
107/* Error messages */
108static const char recargchar[] = "option requires an argument -- %c";
109static const char recargstring[] = "option requires an argument -- %s";
110static const char ambig[] = "ambiguous option -- %.*s";
111static const char noarg[] = "option doesn't take an argument -- %.*s";
112static const char illoptchar[] = "unknown option -- %c";
113static const char illoptstring[] = "unknown option -- %s";
114
115/*
116 * Compute the greatest common divisor of a and b.
117 */
118static int
119gcd(int a, int b)
120{
121 int c;
122
123 c = a % b;
124 while (c != 0) {
125 a = b;
126 b = c;
127 c = a % b;
128 }
129
130 return (b);
131}
132
133/*
134 * Exchange the block from nonopt_start to nonopt_end with the block
135 * from nonopt_end to opt_end (keeping the same order of arguments
136 * in each block).
137 */
138static void
139permute_args(int panonopt_start, int panonopt_end, int opt_end,
140 char * const *nargv)
141{
142 int cstart, cyclelen, i, j, ncycle, nnonopts, nopts, pos;
143 char *swap;
144
145 /*
146 * compute lengths of blocks and number and size of cycles
147 */
148 nnonopts = panonopt_end - panonopt_start;
149 nopts = opt_end - panonopt_end;
150 ncycle = gcd(nnonopts, nopts);
151 cyclelen = (opt_end - panonopt_start) / ncycle;
152
153 for (i = 0; i < ncycle; i++) {
154 cstart = panonopt_end+i;
155 pos = cstart;
156 for (j = 0; j < cyclelen; j++) {
157 if (pos >= panonopt_end)
158 pos -= nnonopts;
159 else
160 pos += nopts;
161 swap = nargv[pos];
162 /* LINTED const cast */
163 ((char **) nargv)[pos] = nargv[cstart];
164 /* LINTED const cast */
165 ((char **)nargv)[cstart] = swap;
166 }
167 }
168}
169
170/*
171 * parse_long_options --
172 * Parse long options in argc/argv argument vector.
173 * Returns -1 if short_too is set and the option does not match long_options.
174 */
175static int
176parse_long_options(char * const *nargv, const char *options,
177 const struct option *long_options, int *idx, int short_too)
178{
179 char *current_argv, *has_equal;
180 size_t current_argv_len;
181 int i, match;
182
183 current_argv = place;
184 match = -1;
185
186 optind++;
187
188 if ((has_equal = strchr(current_argv, '=')) != NULL) {
189 /* argument found (--option=arg) */
190 current_argv_len = has_equal - current_argv;
191 has_equal++;
192 } else
193 current_argv_len = strlen(current_argv);
194
195 for (i = 0; long_options[i].name; i++) {
196 /* find matching long option */
197 if (strncmp(current_argv, long_options[i].name,
198 current_argv_len))
199 continue;
200
201 if (strlen(long_options[i].name) == current_argv_len) {
202 /* exact match */
203 match = i;
204 break;
205 }
206 /*
207 * If this is a known short option, don't allow
208 * a partial match of a single character.
209 */
210 if (short_too && current_argv_len == 1)
211 continue;
212
213 if (match == -1) /* partial match */
214 match = i;
215 else {
216 /* ambiguous abbreviation */
217 if (PRINT_ERROR)
218 warnx(ambig, (int)current_argv_len,
219 current_argv);
220 optopt = 0;
221 return (BADCH);
222 }
223 }
224 if (match != -1) { /* option found */
225 if (long_options[match].has_arg == no_argument
226 && has_equal) {
227 if (PRINT_ERROR)
228 warnx(noarg, (int)current_argv_len,
229 current_argv);
230 /*
231 * XXX: GNU sets optopt to val regardless of flag
232 */
233 if (long_options[match].flag == NULL)
234 optopt = long_options[match].val;
235 else
236 optopt = 0;
237 return (BADARG);
238 }
239 if (long_options[match].has_arg == required_argument ||
240 long_options[match].has_arg == optional_argument) {
241 if (has_equal)
242 optarg = has_equal;
243 else if (long_options[match].has_arg ==
244 required_argument) {
245 /*
246 * optional argument doesn't use next nargv
247 */
248 optarg = nargv[optind++];
249 }
250 }
251 if ((long_options[match].has_arg == required_argument)
252 && (optarg == NULL)) {
253 /*
254 * Missing argument; leading ':' indicates no error
255 * should be generated.
256 */
257 if (PRINT_ERROR)
258 warnx(recargstring,
259 current_argv);
260 /*
261 * XXX: GNU sets optopt to val regardless of flag
262 */
263 if (long_options[match].flag == NULL)
264 optopt = long_options[match].val;
265 else
266 optopt = 0;
267 --optind;
268 return (BADARG);
269 }
270 } else { /* unknown option */
271 if (short_too) {
272 --optind;
273 return (-1);
274 }
275 if (PRINT_ERROR)
276 warnx(illoptstring, current_argv);
277 optopt = 0;
278 return (BADCH);
279 }
280 if (idx)
281 *idx = match;
282 if (long_options[match].flag) {
283 *long_options[match].flag = long_options[match].val;
284 return (0);
285 } else
286 return (long_options[match].val);
287}
288
289/*
290 * getopt_internal --
291 * Parse argc/argv argument vector. Called by user level routines.
292 */
293static int
294getopt_internal(int nargc, char * const *nargv, const char *options,
295 const struct option *long_options, int *idx, int flags)
296{
297 char *oli; /* option letter list index */
298 int optchar, short_too;
299 static int posixly_correct = -1;
300
301 if (options == NULL)
302 return (-1);
303
304 /*
305 * XXX Some GNU programs (like cvs) set optind to 0 instead of
306 * XXX using optreset. Work around this braindamage.
307 */
308 if (optind == 0)
309 optind = optreset = 1;
310
311 /*
312 * Disable GNU extensions if POSIXLY_CORRECT is set or options
313 * string begins with a '+'.
314 */
315 if (posixly_correct == -1 || optreset)
316 posixly_correct = (getenv("POSIXLY_CORRECT") != NULL);
317 if (*options == '-')
318 flags |= FLAG_ALLARGS;
319 else if (posixly_correct || *options == '+')
320 flags &= ~FLAG_PERMUTE;
321 if (*options == '+' || *options == '-')
322 options++;
323
324 optarg = NULL;
325 if (optreset)
326 nonopt_start = nonopt_end = -1;
327start:
328 if (optreset || !*place) { /* update scanning pointer */
329 optreset = 0;
330 if (optind >= nargc) { /* end of argument vector */
331 place = EMSG;
332 if (nonopt_end != -1) {
333 /* do permutation, if we have to */
334 permute_args(nonopt_start, nonopt_end,
335 optind, nargv);
336 optind -= nonopt_end - nonopt_start;
337 }
338 else if (nonopt_start != -1) {
339 /*
340 * If we skipped non-options, set optind
341 * to the first of them.
342 */
343 optind = nonopt_start;
344 }
345 nonopt_start = nonopt_end = -1;
346 return (-1);
347 }
348 if (*(place = nargv[optind]) != '-' ||
349 (place[1] == '\0' && strchr(options, '-') == NULL)) {
350 place = EMSG; /* found non-option */
351 if (flags & FLAG_ALLARGS) {
352 /*
353 * GNU extension:
354 * return non-option as argument to option 1
355 */
356 optarg = nargv[optind++];
357 return (INORDER);
358 }
359 if (!(flags & FLAG_PERMUTE)) {
360 /*
361 * If no permutation wanted, stop parsing
362 * at first non-option.
363 */
364 return (-1);
365 }
366 /* do permutation */
367 if (nonopt_start == -1)
368 nonopt_start = optind;
369 else if (nonopt_end != -1) {
370 permute_args(nonopt_start, nonopt_end,
371 optind, nargv);
372 nonopt_start = optind -
373 (nonopt_end - nonopt_start);
374 nonopt_end = -1;
375 }
376 optind++;
377 /* process next argument */
378 goto start;
379 }
380 if (nonopt_start != -1 && nonopt_end == -1)
381 nonopt_end = optind;
382
383 /*
384 * If we have "-" do nothing, if "--" we are done.
385 */
386 if (place[1] != '\0' && *++place == '-' && place[1] == '\0') {
387 optind++;
388 place = EMSG;
389 /*
390 * We found an option (--), so if we skipped
391 * non-options, we have to permute.
392 */
393 if (nonopt_end != -1) {
394 permute_args(nonopt_start, nonopt_end,
395 optind, nargv);
396 optind -= nonopt_end - nonopt_start;
397 }
398 nonopt_start = nonopt_end = -1;
399 return (-1);
400 }
401 }
402
403 /*
404 * Check long options if:
405 * 1) we were passed some
406 * 2) the arg is not just "-"
407 * 3) either the arg starts with -- we are getopt_long_only()
408 */
409 if (long_options != NULL && place != nargv[optind] &&
410 (*place == '-' || (flags & FLAG_LONGONLY))) {
411 short_too = 0;
412 if (*place == '-')
413 place++; /* --foo long option */
414 else if (*place != ':' && strchr(options, *place) != NULL)
415 short_too = 1; /* could be short option too */
416
417 optchar = parse_long_options(nargv, options, long_options,
418 idx, short_too);
419 if (optchar != -1) {
420 place = EMSG;
421 return (optchar);
422 }
423 }
424
425 if ((optchar = (int)*place++) == (int)':' ||
426 (optchar == (int)'-' && *place != '\0') ||
427 (oli = strchr(options, optchar)) == NULL) {
428 /*
429 * If the user specified "-" and '-' isn't listed in
430 * options, return -1 (non-option) as per POSIX.
431 * Otherwise, it is an unknown option character (or ':').
432 */
433 if (optchar == (int)'-' && *place == '\0')
434 return (-1);
435 if (!*place)
436 ++optind;
437 if (PRINT_ERROR)
438 warnx(illoptchar, optchar);
439 optopt = optchar;
440 return (BADCH);
441 }
442 if (long_options != NULL && optchar == 'W' && oli[1] == ';') {
443 /* -W long-option */
444 if (*place) /* no space */
445 /* NOTHING */;
446 else if (++optind >= nargc) { /* no arg */
447 place = EMSG;
448 if (PRINT_ERROR)
449 warnx(recargchar, optchar);
450 optopt = optchar;
451 return (BADARG);
452 } else /* white space */
453 place = nargv[optind];
454 optchar = parse_long_options(nargv, options, long_options,
455 idx, 0);
456 place = EMSG;
457 return (optchar);
458 }
459 if (*++oli != ':') { /* doesn't take argument */
460 if (!*place)
461 ++optind;
462 } else { /* takes (optional) argument */
463 optarg = NULL;
464 if (*place) /* no white space */
465 optarg = place;
466 else if (oli[1] != ':') { /* arg not optional */
467 if (++optind >= nargc) { /* no arg */
468 place = EMSG;
469 if (PRINT_ERROR)
470 warnx(recargchar, optchar);
471 optopt = optchar;
472 return (BADARG);
473 } else
474 optarg = nargv[optind];
475 }
476 place = EMSG;
477 ++optind;
478 }
479 /* dump back option letter */
480 return (optchar);
481}
482
483/*
484 * getopt --
485 * Parse argc/argv argument vector.
486 *
487 * [eventually this will replace the BSD getopt]
488 */
489int
490getopt(int nargc, char * const *nargv, const char *options)
491{
492
493 /*
494 * We don't pass FLAG_PERMUTE to getopt_internal() since
495 * the BSD getopt(3) (unlike GNU) has never done this.
496 *
497 * Furthermore, since many privileged programs call getopt()
498 * before dropping privileges it makes sense to keep things
499 * as simple (and bug-free) as possible.
500 */
501 return (getopt_internal(nargc, nargv, options, NULL, NULL, 0));
502}
503
504#if 0
505/*
506 * getopt_long --
507 * Parse argc/argv argument vector.
508 */
509int
510getopt_long(int nargc, char * const *nargv, const char *options,
511 const struct option *long_options, int *idx)
512{
513
514 return (getopt_internal(nargc, nargv, options, long_options, idx,
515 FLAG_PERMUTE));
516}
517
518/*
519 * getopt_long_only --
520 * Parse argc/argv argument vector.
521 */
522int
523getopt_long_only(int nargc, char * const *nargv, const char *options,
524 const struct option *long_options, int *idx)
525{
526
527 return (getopt_internal(nargc, nargv, options, long_options, idx,
528 FLAG_PERMUTE|FLAG_LONGONLY));
529}
530#endif
531
532#endif /* !defined(HAVE_GETOPT) || !defined(HAVE_OPTRESET) */
diff --git a/openbsd-compat/getrrsetbyname-ldns.c b/openbsd-compat/getrrsetbyname-ldns.c
index 19666346b..343720f10 100644
--- a/openbsd-compat/getrrsetbyname-ldns.c
+++ b/openbsd-compat/getrrsetbyname-ldns.c
@@ -58,7 +58,6 @@
58 58
59#define malloc(x) (xmalloc(x)) 59#define malloc(x) (xmalloc(x))
60#define calloc(x, y) (xcalloc((x),(y))) 60#define calloc(x, y) (xcalloc((x),(y)))
61#define free(x) (xfree(x))
62 61
63int 62int
64getrrsetbyname(const char *hostname, unsigned int rdclass, 63getrrsetbyname(const char *hostname, unsigned int rdclass,
diff --git a/openbsd-compat/openbsd-compat.h b/openbsd-compat/openbsd-compat.h
index a8c579f49..392fa38dc 100644
--- a/openbsd-compat/openbsd-compat.h
+++ b/openbsd-compat/openbsd-compat.h
@@ -1,4 +1,4 @@
1/* $Id: openbsd-compat.h,v 1.55 2013/02/15 01:20:42 dtucker Exp $ */ 1/* $Id: openbsd-compat.h,v 1.58 2013/06/05 22:30:21 dtucker 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.
@@ -111,6 +111,10 @@ char *dirname(const char *path);
111int fmt_scaled(long long number, char *result); 111int fmt_scaled(long long number, char *result);
112#endif 112#endif
113 113
114#ifndef HAVE_SCAN_SCALED
115int scan_scaled(char *, long long *);
116#endif
117
114#if defined(BROKEN_INET_NTOA) || !defined(HAVE_INET_NTOA) 118#if defined(BROKEN_INET_NTOA) || !defined(HAVE_INET_NTOA)
115char *inet_ntoa(struct in_addr in); 119char *inet_ntoa(struct in_addr in);
116#endif 120#endif
@@ -139,6 +143,7 @@ int getgrouplist(const char *, gid_t, gid_t *, int *);
139 143
140#if !defined(HAVE_GETOPT) || !defined(HAVE_GETOPT_OPTRESET) 144#if !defined(HAVE_GETOPT) || !defined(HAVE_GETOPT_OPTRESET)
141int BSDgetopt(int argc, char * const *argv, const char *opts); 145int BSDgetopt(int argc, char * const *argv, const char *opts);
146#include "openbsd-compat/getopt.h"
142#endif 147#endif
143 148
144#if defined(HAVE_DECL_WRITEV) && HAVE_DECL_WRITEV == 0 149#if defined(HAVE_DECL_WRITEV) && HAVE_DECL_WRITEV == 0
@@ -202,6 +207,11 @@ unsigned long long strtoull(const char *, char **, int);
202long long strtonum(const char *, long long, long long, const char **); 207long long strtonum(const char *, long long, long long, const char **);
203#endif 208#endif
204 209
210/* multibyte character support */
211#ifndef HAVE_MBLEN
212# define mblen(x, y) 1
213#endif
214
205#if !defined(HAVE_VASPRINTF) || !defined(HAVE_VSNPRINTF) 215#if !defined(HAVE_VASPRINTF) || !defined(HAVE_VSNPRINTF)
206# include <stdarg.h> 216# include <stdarg.h>
207#endif 217#endif
diff --git a/openbsd-compat/port-aix.c b/openbsd-compat/port-aix.c
index 0bdefbf6d..8da367d48 100644
--- a/openbsd-compat/port-aix.c
+++ b/openbsd-compat/port-aix.c
@@ -86,7 +86,7 @@ aix_usrinfo(struct passwd *pw)
86 fatal("Couldn't set usrinfo: %s", strerror(errno)); 86 fatal("Couldn't set usrinfo: %s", strerror(errno));
87 debug3("AIX/UsrInfo: set len %d", i); 87 debug3("AIX/UsrInfo: set len %d", i);
88 88
89 xfree(cp); 89 free(cp);
90} 90}
91 91
92# ifdef WITH_AIXAUTHENTICATE 92# ifdef WITH_AIXAUTHENTICATE
@@ -215,16 +215,14 @@ sys_auth_passwd(Authctxt *ctxt, const char *password)
215 default: /* user can't change(2) or other error (-1) */ 215 default: /* user can't change(2) or other error (-1) */
216 logit("Password can't be changed for user %s: %.100s", 216 logit("Password can't be changed for user %s: %.100s",
217 name, msg); 217 name, msg);
218 if (msg) 218 free(msg);
219 xfree(msg);
220 authsuccess = 0; 219 authsuccess = 0;
221 } 220 }
222 221
223 aix_restoreauthdb(); 222 aix_restoreauthdb();
224 } 223 }
225 224
226 if (authmsg != NULL) 225 free(authmsg);
227 xfree(authmsg);
228 226
229 return authsuccess; 227 return authsuccess;
230} 228}
@@ -269,7 +267,7 @@ sys_auth_allowed_user(struct passwd *pw, Buffer *loginmsg)
269 267
270 if (!permitted) 268 if (!permitted)
271 logit("Login restricted for %s: %.100s", pw->pw_name, msg); 269 logit("Login restricted for %s: %.100s", pw->pw_name, msg);
272 xfree(msg); 270 free(msg);
273 return permitted; 271 return permitted;
274} 272}
275 273
diff --git a/openbsd-compat/port-linux.c b/openbsd-compat/port-linux.c
index 2b8a14a59..de6ad3fd7 100644
--- a/openbsd-compat/port-linux.c
+++ b/openbsd-compat/port-linux.c
@@ -1,4 +1,4 @@
1/* $Id: port-linux.c,v 1.17 2012/03/08 23:25:18 djm Exp $ */ 1/* $Id: port-linux.c,v 1.18 2013/06/01 22:07:32 dtucker Exp $ */
2 2
3/* 3/*
4 * Copyright (c) 2005 Daniel Walsh <dwalsh@redhat.com> 4 * Copyright (c) 2005 Daniel Walsh <dwalsh@redhat.com>
@@ -109,10 +109,8 @@ ssh_selinux_getctxbyname(char *pwname, const char *role)
109 } 109 }
110 110
111#ifdef HAVE_GETSEUSERBYNAME 111#ifdef HAVE_GETSEUSERBYNAME
112 if (sename != NULL) 112 free(sename);
113 xfree(sename); 113 free(lvl);
114 if (lvl != NULL)
115 xfree(lvl);
116#endif 114#endif
117 115
118 return sc; 116 return sc;
@@ -230,8 +228,8 @@ ssh_selinux_change_context(const char *newname)
230 if (setcon(newctx) < 0) 228 if (setcon(newctx) < 0)
231 switchlog("%s: setcon %s from %s failed with %s", __func__, 229 switchlog("%s: setcon %s from %s failed with %s", __func__,
232 newctx, oldctx, strerror(errno)); 230 newctx, oldctx, strerror(errno));
233 xfree(oldctx); 231 free(oldctx);
234 xfree(newctx); 232 free(newctx);
235} 233}
236 234
237void 235void
diff --git a/openbsd-compat/xcrypt.c b/openbsd-compat/xcrypt.c
index 6291e2884..c8aea461d 100644
--- a/openbsd-compat/xcrypt.c
+++ b/openbsd-compat/xcrypt.c
@@ -55,7 +55,12 @@
55 55
56# if defined(HAVE_MD5_PASSWORDS) && !defined(HAVE_MD5_CRYPT) 56# if defined(HAVE_MD5_PASSWORDS) && !defined(HAVE_MD5_CRYPT)
57# include "md5crypt.h" 57# include "md5crypt.h"
58# endif 58# endif
59
60# if !defined(HAVE_CRYPT) && defined(HAVE_DES_CRYPT)
61# include <openssl/des.h>
62# define crypt DES_crypt
63# endif
59 64
60char * 65char *
61xcrypt(const char *password, const char *salt) 66xcrypt(const char *password, const char *salt)