compat.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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:
  13. *
  14. * The above copyright notice and this permission notice shall be included in
  15. * all copies or substantial portions of the Software.
  16. *
  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.
  24. *
  25. * strlcat() is copyright as follows:
  26. * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
  27. * All rights reserved.
  28. *
  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.
  39. *
  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.
  50. *
  51. * daemon() and getusershell() is copyright as follows:
  52. *
  53. * Copyright (c) 1990, 1993
  54. * The Regents of the University of California. All rights reserved.
  55. *
  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.
  67. *
  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.
  79. *
  80. * Modifications for Dropbear to getusershell() are by Paul Marinceu
  81. */
  82. #include "includes.h"
  83. #ifndef HAVE_GETUSERSHELL
  84. static char **curshell, **shells, *strings;
  85. static char **initshells();
  86. #endif
  87. #ifndef HAVE_STRLCPY
  88. /* Implemented by matt as specified in freebsd 4.7 manpage.
  89. * We don't require great speed, is simply for use with sshpty code */
  90. size_t strlcpy(char *dst, const char *src, size_t size) {
  91. size_t i;
  92. /* this is undefined, though size==0 -> return 0 */
  93. if (size < 1) {
  94. return 0;
  95. }
  96. for (i = 0; i < size-1; i++) {
  97. if (src[i] == '\0') {
  98. break;
  99. } else {
  100. dst[i] = src[i];
  101. }
  102. }
  103. dst[i] = '\0';
  104. return strlen(src);
  105. }
  106. #endif /* HAVE_STRLCPY */
  107. #ifndef HAVE_STRLCAT
  108. /* taken from openbsd-compat for OpenSSH 3.6.1p1 */
  109. /* "$OpenBSD: strlcat.c,v 1.8 2001/05/13 15:40:15 deraadt Exp $"
  110. *
  111. * Appends src to string dst of size siz (unlike strncat, siz is the
  112. * full size of dst, not space left). At most siz-1 characters
  113. * will be copied. Always NUL terminates (unless siz <= strlen(dst)).
  114. * Returns strlen(src) + MIN(siz, strlen(initial dst)).
  115. * If retval >= siz, truncation occurred.
  116. */
  117. size_t
  118. strlcat(dst, src, siz)
  119. char *dst;
  120. const char *src;
  121. size_t siz;
  122. {
  123. register char *d = dst;
  124. register const char *s = src;
  125. register size_t n = siz;
  126. size_t dlen;
  127. /* Find the end of dst and adjust bytes left but don't go past end */
  128. while (n-- != 0 && *d != '\0')
  129. d++;
  130. dlen = d - dst;
  131. n = siz - dlen;
  132. if (n == 0)
  133. return(dlen + strlen(s));
  134. while (*s != '\0') {
  135. if (n != 1) {
  136. *d++ = *s;
  137. n--;
  138. }
  139. s++;
  140. }
  141. *d = '\0';
  142. return(dlen + (s - src)); /* count does not include NUL */
  143. }
  144. #endif /* HAVE_STRLCAT */
  145. #ifndef HAVE_DAEMON
  146. /* From NetBSD - daemonise a process */
  147. int daemon(int nochdir, int noclose) {
  148. int fd;
  149. switch (fork()) {
  150. case -1:
  151. return (-1);
  152. case 0:
  153. break;
  154. default:
  155. _exit(0);
  156. }
  157. if (setsid() == -1)
  158. return -1;
  159. if (!nochdir)
  160. (void)chdir("/");
  161. if (!noclose && (fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) {
  162. (void)dup2(fd, STDIN_FILENO);
  163. (void)dup2(fd, STDOUT_FILENO);
  164. (void)dup2(fd, STDERR_FILENO);
  165. if (fd > STDERR_FILENO)
  166. (void)close(fd);
  167. }
  168. return 0;
  169. }
  170. #endif /* HAVE_DAEMON */
  171. #ifndef HAVE_BASENAME
  172. char *basename(const char *path) {
  173. char *foo = strrchr(path, '/');
  174. if (!foo)
  175. {
  176. return path;
  177. }
  178. return ++foo;
  179. }
  180. #endif /* HAVE_BASENAME */
  181. #ifndef HAVE_GETUSERSHELL
  182. /*
  183. * Get a list of shells from /etc/shells, if it exists.
  184. */
  185. char * getusershell() {
  186. char *ret;
  187. if (curshell == NULL)
  188. curshell = initshells();
  189. ret = *curshell;
  190. if (ret != NULL)
  191. curshell++;
  192. return (ret);
  193. }
  194. void endusershell() {
  195. if (shells != NULL)
  196. free(shells);
  197. shells = NULL;
  198. if (strings != NULL)
  199. free(strings);
  200. strings = NULL;
  201. curshell = NULL;
  202. }
  203. void setusershell() {
  204. curshell = initshells();
  205. }
  206. static char **initshells() {
  207. /* don't touch this list. */
  208. static const char *okshells[] = { "/bin/sh", "/bin/csh", NULL };
  209. register char **sp, *cp;
  210. register FILE *fp;
  211. struct stat statb;
  212. int flen;
  213. if (shells != NULL)
  214. free(shells);
  215. shells = NULL;
  216. if (strings != NULL)
  217. free(strings);
  218. strings = NULL;
  219. if ((fp = fopen("/etc/shells", "rc")) == NULL)
  220. return (char **) okshells;
  221. if (fstat(fileno(fp), &statb) == -1) {
  222. (void)fclose(fp);
  223. return (char **) okshells;
  224. }
  225. if ((strings = malloc((u_int)statb.st_size + 1)) == NULL) {
  226. (void)fclose(fp);
  227. return (char **) okshells;
  228. }
  229. shells = calloc((unsigned)statb.st_size / 3, sizeof (char *));
  230. if (shells == NULL) {
  231. (void)fclose(fp);
  232. free(strings);
  233. strings = NULL;
  234. return (char **) okshells;
  235. }
  236. sp = shells;
  237. cp = strings;
  238. flen = statb.st_size;
  239. while (fgets(cp, flen - (cp - strings), fp) != NULL) {
  240. while (*cp != '#' && *cp != '/' && *cp != '\0')
  241. cp++;
  242. if (*cp == '#' || *cp == '\0')
  243. continue;
  244. *sp++ = cp;
  245. while (!isspace(*cp) && *cp != '#' && *cp != '\0')
  246. cp++;
  247. *cp++ = '\0';
  248. }
  249. *sp = NULL;
  250. (void)fclose(fp);
  251. return (shells);
  252. }
  253. #endif /* HAVE_GETUSERSHELL */