openssl: update to 1.0.2d
[tomato.git] / release / src / router / dropbear / scpmisc.c
blobec4df353fd446775de3786444eb3985b9f1cc711
1 /*
2 * Copyright (c) 2000 Markus Friedl. All rights reserved.
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.
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.
25 /*RCSID("OpenBSD: misc.c,v 1.22 2003/09/18 08:49:45 markus Exp ");*/
27 /* For xmalloc, xfree etc:
28 * Author: Tatu Ylonen <ylo@cs.hut.fi>
29 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
30 * All rights reserved
31 * Versions of malloc and friends that check their results, and never return
32 * failure (they call fatal if they encounter an error).
34 * As far as I am concerned, the code I have written for this software
35 * can be used freely for any purpose. Any derived versions of this
36 * software must be clearly marked as such, and if the derived work is
37 * incompatible with the protocol description in the RFC file, it must be
38 * called by a name other than "ssh" or "Secure Shell".
41 /*RCSID("OpenBSD: xmalloc.c,v 1.16 2001/07/23 18:21:46 stevesk Exp ");*/
43 #define _GNU_SOURCE
44 #include "includes.h"
45 #include "scpmisc.h"
47 void *
48 xmalloc(size_t size)
50 void *ptr;
52 if (size == 0) {
53 fprintf(stderr, "xmalloc: zero size\n");
54 exit(EXIT_FAILURE);
56 ptr = malloc(size);
57 if (ptr == NULL) {
58 fprintf(stderr, "xmalloc: out of memory (allocating %lu bytes)\n", (u_long) size);
59 exit(EXIT_FAILURE);
61 return ptr;
64 void *
65 xrealloc(void *ptr, size_t new_size)
67 void *new_ptr;
69 if (new_size == 0) {
70 fprintf(stderr, "xrealloc: zero size\n");
71 exit(EXIT_FAILURE);
73 if (ptr == NULL)
74 new_ptr = malloc(new_size);
75 else
76 new_ptr = realloc(ptr, new_size);
77 if (new_ptr == NULL) {
78 fprintf(stderr, "xrealloc: out of memory (new_size %lu bytes)\n", (u_long) new_size);
79 exit(EXIT_FAILURE);
81 return new_ptr;
84 void
85 xfree(void *ptr)
87 if (ptr == NULL) {
88 fprintf(stderr, "xfree: NULL pointer given as argument\n");
89 exit(EXIT_FAILURE);
91 free(ptr);
94 char *
95 xstrdup(const char *str)
97 size_t len;
98 char *cp;
100 len = strlen(str) + 1;
101 cp = xmalloc(len);
102 strncpy(cp, str, len);
103 return cp;
106 char *
107 cleanhostname(char *host)
109 if (*host == '[' && host[strlen(host) - 1] == ']') {
110 host[strlen(host) - 1] = '\0';
111 return (host + 1);
112 } else
113 return host;
116 char *
117 colon(char *cp)
119 int flag = 0;
121 if (*cp == ':') /* Leading colon is part of file name. */
122 return (0);
123 if (*cp == '[')
124 flag = 1;
126 for (; *cp; ++cp) {
127 if (*cp == '@' && *(cp+1) == '[')
128 flag = 1;
129 if (*cp == ']' && *(cp+1) == ':' && flag)
130 return (cp+1);
131 if (*cp == ':' && !flag)
132 return (cp);
133 if (*cp == '/')
134 return (0);
136 return (0);
139 /* function to assist building execv() arguments */
140 void
141 addargs(arglist *args, char *fmt, ...)
143 va_list ap;
144 char *cp;
145 u_int nalloc;
146 int r;
148 va_start(ap, fmt);
149 r = vasprintf(&cp, fmt, ap);
150 va_end(ap);
151 if (r == -1)
152 fatal("addargs: argument too long");
154 nalloc = args->nalloc;
155 if (args->list == NULL) {
156 nalloc = 32;
157 args->num = 0;
158 } else if (args->num+2 >= nalloc)
159 nalloc *= 2;
161 args->list = xrealloc(args->list, nalloc * sizeof(char *));
162 args->nalloc = nalloc;
163 args->list[args->num++] = cp;
164 args->list[args->num] = NULL;
167 void
168 replacearg(arglist *args, u_int which, char *fmt, ...)
170 va_list ap;
171 char *cp;
172 int r;
174 va_start(ap, fmt);
175 r = vasprintf(&cp, fmt, ap);
176 va_end(ap);
177 if (r == -1)
178 fatal("replacearg: argument too long");
180 if (which >= args->num)
181 fatal("replacearg: tried to replace invalid arg %d >= %d",
182 which, args->num);
183 xfree(args->list[which]);
184 args->list[which] = cp;
187 void
188 freeargs(arglist *args)
190 u_int i;
192 if (args->list != NULL) {
193 for (i = 0; i < args->num; i++)
194 xfree(args->list[i]);
195 xfree(args->list);
196 args->nalloc = args->num = 0;
197 args->list = NULL;
202 * NB. duplicate __progname in case it is an alias for argv[0]
203 * Otherwise it may get clobbered by setproctitle()
205 char *ssh_get_progname(char *argv0)
207 char *p;
209 if (argv0 == NULL)
210 return ("unknown"); /* XXX */
211 p = strrchr(argv0, '/');
212 if (p == NULL)
213 p = argv0;
214 else
215 p++;
217 return (xstrdup(p));
220 void fatal(char* fmt,...)
222 va_list args;
223 va_start(args, fmt);
224 vfprintf(stderr, fmt, args);
225 va_end(args);
226 exit(255);
229 void
230 sanitise_stdfd(void)
232 int nullfd, dupfd;
234 if ((nullfd = dupfd = open(_PATH_DEVNULL, O_RDWR)) == -1) {
235 fprintf(stderr, "Couldn't open /dev/null: %s", strerror(errno));
236 exit(1);
238 while (++dupfd <= 2) {
239 /* Only clobber closed fds */
240 if (fcntl(dupfd, F_GETFL, 0) >= 0)
241 continue;
242 if (dup2(nullfd, dupfd) == -1) {
243 fprintf(stderr, "dup2: %s", strerror(errno));
244 exit(1);
247 if (nullfd > 2)
248 close(nullfd);