dropbear 2016.73
[tomato.git] / release / src / router / dropbear / compat.c
blob71558a58e891881bad15677b0e40315f4f78cf62
1 /*
2 * Dropbear - a SSH2 server
3 *
4 * Copyright (c) 2002,2003 Matt Johnston
5 * All rights reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
25 * strlcat() is copyright as follows:
26 * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
27 * All rights reserved.
29 * Redistribution and use in source and binary forms, with or without
30 * modification, are permitted provided that the following conditions
31 * are met:
32 * 1. Redistributions of source code must retain the above copyright
33 * notice, this list of conditions and the following disclaimer.
34 * 2. Redistributions in binary form must reproduce the above copyright
35 * notice, this list of conditions and the following disclaimer in the
36 * documentation and/or other materials provided with the distribution.
37 * 3. The name of the author may not be used to endorse or promote products
38 * derived from this software without specific prior written permission.
40 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
41 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
42 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
43 * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
44 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
45 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
46 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
47 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
48 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
49 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
51 * daemon() and getusershell() is copyright as follows:
53 * Copyright (c) 1990, 1993
54 * The Regents of the University of California. All rights reserved.
56 * Redistribution and use in source and binary forms, with or without
57 * modification, are permitted provided that the following conditions
58 * are met:
59 * 1. Redistributions of source code must retain the above copyright
60 * notice, this list of conditions and the following disclaimer.
61 * 2. Redistributions in binary form must reproduce the above copyright
62 * notice, this list of conditions and the following disclaimer in the
63 * documentation and/or other materials provided with the distribution.
64 * 3. Neither the name of the University nor the names of its contributors
65 * may be used to endorse or promote products derived from this software
66 * without specific prior written permission.
68 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
69 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
70 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
71 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
72 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
73 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
74 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
75 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
76 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
77 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
78 * SUCH DAMAGE.
80 * Modifications for Dropbear to getusershell() are by Paul Marinceu
83 #include "includes.h"
85 #ifndef HAVE_GETUSERSHELL
86 static char **curshell, **shells, *strings;
87 static char **initshells();
88 #endif
90 #ifndef HAVE_STRLCPY
91 /* Implemented by matt as specified in freebsd 4.7 manpage.
92 * We don't require great speed, is simply for use with sshpty code */
93 size_t strlcpy(char *dst, const char *src, size_t size) {
95 size_t i;
97 /* this is undefined, though size==0 -> return 0 */
98 if (size < 1) {
99 return 0;
102 for (i = 0; i < size-1; i++) {
103 if (src[i] == '\0') {
104 break;
105 } else {
106 dst[i] = src[i];
110 dst[i] = '\0';
111 return strlen(src);
114 #endif /* HAVE_STRLCPY */
116 #ifndef HAVE_STRLCAT
117 /* taken from openbsd-compat for OpenSSH 3.6.1p1 */
118 /* "$OpenBSD: strlcat.c,v 1.8 2001/05/13 15:40:15 deraadt Exp $"
120 * Appends src to string dst of size siz (unlike strncat, siz is the
121 * full size of dst, not space left). At most siz-1 characters
122 * will be copied. Always NUL terminates (unless siz <= strlen(dst)).
123 * Returns strlen(src) + MIN(siz, strlen(initial dst)).
124 * If retval >= siz, truncation occurred.
126 size_t
127 strlcat(dst, src, siz)
128 char *dst;
129 const char *src;
130 size_t siz;
132 register char *d = dst;
133 register const char *s = src;
134 register size_t n = siz;
135 size_t dlen;
137 /* Find the end of dst and adjust bytes left but don't go past end */
138 while (n-- != 0 && *d != '\0')
139 d++;
140 dlen = d - dst;
141 n = siz - dlen;
143 if (n == 0)
144 return(dlen + strlen(s));
145 while (*s != '\0') {
146 if (n != 1) {
147 *d++ = *s;
148 n--;
150 s++;
152 *d = '\0';
154 return(dlen + (s - src)); /* count does not include NUL */
156 #endif /* HAVE_STRLCAT */
158 #ifndef HAVE_DAEMON
159 /* From NetBSD - daemonise a process */
161 int daemon(int nochdir, int noclose) {
163 int fd;
165 switch (fork()) {
166 case -1:
167 return (-1);
168 case 0:
169 break;
170 default:
171 _exit(0);
174 if (setsid() == -1)
175 return -1;
177 if (!nochdir)
178 (void)chdir("/");
180 if (!noclose && (fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) {
181 (void)dup2(fd, STDIN_FILENO);
182 (void)dup2(fd, STDOUT_FILENO);
183 (void)dup2(fd, STDERR_FILENO);
184 if (fd > STDERR_FILENO)
185 (void)close(fd);
187 return 0;
189 #endif /* HAVE_DAEMON */
191 #ifndef HAVE_BASENAME
193 char *basename(const char *path) {
195 char *foo = strrchr(path, '/');
196 if (!foo)
198 return path;
200 return ++foo;
203 #endif /* HAVE_BASENAME */
205 #ifndef HAVE_GETUSERSHELL
208 * Get a list of shells from /etc/shells, if it exists.
210 char * getusershell() {
211 char *ret;
213 if (curshell == NULL)
214 curshell = initshells();
215 ret = *curshell;
216 if (ret != NULL)
217 curshell++;
218 return (ret);
221 void endusershell() {
223 if (shells != NULL)
224 free(shells);
225 shells = NULL;
226 if (strings != NULL)
227 free(strings);
228 strings = NULL;
229 curshell = NULL;
232 void setusershell() {
233 curshell = initshells();
236 static char **initshells() {
237 /* don't touch this list. */
238 static const char *okshells[] = { "/bin/sh", "/bin/csh", NULL };
239 register char **sp, *cp;
240 register FILE *fp;
241 struct stat statb;
242 int flen;
244 if (shells != NULL)
245 free(shells);
246 shells = NULL;
247 if (strings != NULL)
248 free(strings);
249 strings = NULL;
250 if ((fp = fopen("/etc/shells", "rc")) == NULL)
251 return (char **) okshells;
252 if (fstat(fileno(fp), &statb) == -1) {
253 (void)fclose(fp);
254 return (char **) okshells;
256 if ((strings = malloc((u_int)statb.st_size + 1)) == NULL) {
257 (void)fclose(fp);
258 return (char **) okshells;
260 shells = calloc((unsigned)statb.st_size / 3, sizeof (char *));
261 if (shells == NULL) {
262 (void)fclose(fp);
263 free(strings);
264 strings = NULL;
265 return (char **) okshells;
267 sp = shells;
268 cp = strings;
269 flen = statb.st_size;
270 while (fgets(cp, flen - (cp - strings), fp) != NULL) {
271 while (*cp != '#' && *cp != '/' && *cp != '\0')
272 cp++;
273 if (*cp == '#' || *cp == '\0')
274 continue;
275 *sp++ = cp;
276 while (!isspace(*cp) && *cp != '#' && *cp != '\0')
277 cp++;
278 *cp++ = '\0';
280 *sp = NULL;
281 (void)fclose(fp);
282 return (shells);
285 #endif /* HAVE_GETUSERSHELL */