Damien Miller | e9fc72e | 2013-10-15 12:14:12 +1100 | [diff] [blame] | 1 | /* $OpenBSD: misc.c,v 1.92 2013/10/14 23:28:23 djm Exp $ */ |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2000 Markus Friedl. All rights reserved. |
Damien Miller | 3f94188 | 2006-03-31 23:13:02 +1100 | [diff] [blame] | 4 | * Copyright (c) 2005,2006 Damien Miller. All rights reserved. |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 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 | * 1. Redistributions of source code must retain the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer. |
| 11 | * 2. Redistributions in binary form must reproduce the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer in the |
| 13 | * documentation and/or other materials provided with the distribution. |
| 14 | * |
| 15 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 16 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 17 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 18 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 19 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 20 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 21 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 22 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 24 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 25 | */ |
| 26 | |
Damien Miller | 61c5150 | 2000-08-18 14:01:04 +1000 | [diff] [blame] | 27 | #include "includes.h" |
Damien Miller | 17e91c0 | 2006-03-15 11:28:34 +1100 | [diff] [blame] | 28 | |
Damien Miller | 9f2abc4 | 2006-07-10 20:53:08 +1000 | [diff] [blame] | 29 | #include <sys/types.h> |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 30 | #include <sys/ioctl.h> |
Damien Miller | 8ec8c3e | 2006-07-10 20:35:38 +1000 | [diff] [blame] | 31 | #include <sys/socket.h> |
Damien Miller | 8dbffe7 | 2006-08-05 11:02:17 +1000 | [diff] [blame] | 32 | #include <sys/param.h> |
Damien Miller | 8ec8c3e | 2006-07-10 20:35:38 +1000 | [diff] [blame] | 33 | |
Darren Tucker | 5d19626 | 2006-07-12 22:15:16 +1000 | [diff] [blame] | 34 | #include <stdarg.h> |
Damien Miller | a7a73ee | 2006-08-05 11:37:59 +1000 | [diff] [blame] | 35 | #include <stdio.h> |
Damien Miller | e7a1e5c | 2006-08-05 11:34:19 +1000 | [diff] [blame] | 36 | #include <stdlib.h> |
Damien Miller | e3476ed | 2006-07-24 14:13:33 +1000 | [diff] [blame] | 37 | #include <string.h> |
Damien Miller | 1708cb7 | 2011-01-13 12:21:34 +1100 | [diff] [blame] | 38 | #include <time.h> |
Damien Miller | e6b3b61 | 2006-07-24 14:01:23 +1000 | [diff] [blame] | 39 | #include <unistd.h> |
Darren Tucker | 5d19626 | 2006-07-12 22:15:16 +1000 | [diff] [blame] | 40 | |
Damien Miller | 8ec8c3e | 2006-07-10 20:35:38 +1000 | [diff] [blame] | 41 | #include <netinet/in.h> |
Damien Miller | 0dac6fb | 2010-11-20 15:19:38 +1100 | [diff] [blame] | 42 | #include <netinet/in_systm.h> |
| 43 | #include <netinet/ip.h> |
Damien Miller | 3a4051e | 2006-03-15 11:19:42 +1100 | [diff] [blame] | 44 | #include <netinet/tcp.h> |
Damien Miller | 8ec8c3e | 2006-07-10 20:35:38 +1000 | [diff] [blame] | 45 | |
Damien Miller | e9fc72e | 2013-10-15 12:14:12 +1100 | [diff] [blame] | 46 | #include <ctype.h> |
Darren Tucker | 3997249 | 2006-07-12 22:22:46 +1000 | [diff] [blame] | 47 | #include <errno.h> |
Damien Miller | 57cf638 | 2006-07-10 21:13:46 +1000 | [diff] [blame] | 48 | #include <fcntl.h> |
Darren Tucker | 4abde77 | 2007-12-29 02:43:51 +1100 | [diff] [blame] | 49 | #include <netdb.h> |
Damien Miller | 03e2003 | 2006-03-15 11:16:59 +1100 | [diff] [blame] | 50 | #ifdef HAVE_PATHS_H |
Damien Miller | a9263d0 | 2006-03-15 11:18:26 +1100 | [diff] [blame] | 51 | # include <paths.h> |
Damien Miller | 9f2abc4 | 2006-07-10 20:53:08 +1000 | [diff] [blame] | 52 | #include <pwd.h> |
Damien Miller | 03e2003 | 2006-03-15 11:16:59 +1100 | [diff] [blame] | 53 | #endif |
Damien Miller | 3beb852 | 2006-01-02 23:40:10 +1100 | [diff] [blame] | 54 | #ifdef SSH_TUN_OPENBSD |
| 55 | #include <net/if.h> |
| 56 | #endif |
Damien Miller | 61c5150 | 2000-08-18 14:01:04 +1000 | [diff] [blame] | 57 | |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 58 | #include "xmalloc.h" |
Kevin Steves | b6e773a | 2001-02-04 13:20:36 +0000 | [diff] [blame] | 59 | #include "misc.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 60 | #include "log.h" |
Darren Tucker | da34553 | 2006-07-10 23:04:19 +1000 | [diff] [blame] | 61 | #include "ssh.h" |
Damien Miller | 61c5150 | 2000-08-18 14:01:04 +1000 | [diff] [blame] | 62 | |
Ben Lindstrom | 4cc240d | 2001-07-04 04:46:56 +0000 | [diff] [blame] | 63 | /* remove newline at end of string */ |
Damien Miller | 61c5150 | 2000-08-18 14:01:04 +1000 | [diff] [blame] | 64 | char * |
| 65 | chop(char *s) |
| 66 | { |
| 67 | char *t = s; |
| 68 | while (*t) { |
Ben Lindstrom | 1c37c6a | 2001-12-06 18:00:18 +0000 | [diff] [blame] | 69 | if (*t == '\n' || *t == '\r') { |
Damien Miller | 61c5150 | 2000-08-18 14:01:04 +1000 | [diff] [blame] | 70 | *t = '\0'; |
| 71 | return s; |
| 72 | } |
| 73 | t++; |
| 74 | } |
| 75 | return s; |
| 76 | |
| 77 | } |
| 78 | |
Ben Lindstrom | 4cc240d | 2001-07-04 04:46:56 +0000 | [diff] [blame] | 79 | /* set/unset filedescriptor to non-blocking */ |
Damien Miller | 232711f | 2004-06-15 10:35:30 +1000 | [diff] [blame] | 80 | int |
Damien Miller | 61c5150 | 2000-08-18 14:01:04 +1000 | [diff] [blame] | 81 | set_nonblock(int fd) |
| 82 | { |
| 83 | int val; |
Ben Lindstrom | c93e84c | 2001-05-12 00:08:37 +0000 | [diff] [blame] | 84 | |
Damien Miller | 61c5150 | 2000-08-18 14:01:04 +1000 | [diff] [blame] | 85 | val = fcntl(fd, F_GETFL, 0); |
| 86 | if (val < 0) { |
| 87 | error("fcntl(%d, F_GETFL, 0): %s", fd, strerror(errno)); |
Damien Miller | 232711f | 2004-06-15 10:35:30 +1000 | [diff] [blame] | 88 | return (-1); |
Damien Miller | 61c5150 | 2000-08-18 14:01:04 +1000 | [diff] [blame] | 89 | } |
Damien Miller | 69b69aa | 2000-10-28 14:19:58 +1100 | [diff] [blame] | 90 | if (val & O_NONBLOCK) { |
Damien Miller | 232711f | 2004-06-15 10:35:30 +1000 | [diff] [blame] | 91 | debug3("fd %d is O_NONBLOCK", fd); |
| 92 | return (0); |
Damien Miller | 69b69aa | 2000-10-28 14:19:58 +1100 | [diff] [blame] | 93 | } |
Damien Miller | ef095ce | 2003-05-14 13:41:39 +1000 | [diff] [blame] | 94 | debug2("fd %d setting O_NONBLOCK", fd); |
Damien Miller | 61c5150 | 2000-08-18 14:01:04 +1000 | [diff] [blame] | 95 | val |= O_NONBLOCK; |
Damien Miller | 232711f | 2004-06-15 10:35:30 +1000 | [diff] [blame] | 96 | if (fcntl(fd, F_SETFL, val) == -1) { |
| 97 | debug("fcntl(%d, F_SETFL, O_NONBLOCK): %s", fd, |
| 98 | strerror(errno)); |
| 99 | return (-1); |
| 100 | } |
| 101 | return (0); |
Damien Miller | 61c5150 | 2000-08-18 14:01:04 +1000 | [diff] [blame] | 102 | } |
| 103 | |
Damien Miller | 232711f | 2004-06-15 10:35:30 +1000 | [diff] [blame] | 104 | int |
Ben Lindstrom | c93e84c | 2001-05-12 00:08:37 +0000 | [diff] [blame] | 105 | unset_nonblock(int fd) |
| 106 | { |
| 107 | int val; |
| 108 | |
| 109 | val = fcntl(fd, F_GETFL, 0); |
| 110 | if (val < 0) { |
| 111 | error("fcntl(%d, F_GETFL, 0): %s", fd, strerror(errno)); |
Damien Miller | 232711f | 2004-06-15 10:35:30 +1000 | [diff] [blame] | 112 | return (-1); |
Ben Lindstrom | c93e84c | 2001-05-12 00:08:37 +0000 | [diff] [blame] | 113 | } |
| 114 | if (!(val & O_NONBLOCK)) { |
Damien Miller | 232711f | 2004-06-15 10:35:30 +1000 | [diff] [blame] | 115 | debug3("fd %d is not O_NONBLOCK", fd); |
| 116 | return (0); |
Ben Lindstrom | c93e84c | 2001-05-12 00:08:37 +0000 | [diff] [blame] | 117 | } |
Ben Lindstrom | 352b1c2 | 2001-06-21 03:04:37 +0000 | [diff] [blame] | 118 | debug("fd %d clearing O_NONBLOCK", fd); |
Ben Lindstrom | c93e84c | 2001-05-12 00:08:37 +0000 | [diff] [blame] | 119 | val &= ~O_NONBLOCK; |
Damien Miller | 232711f | 2004-06-15 10:35:30 +1000 | [diff] [blame] | 120 | if (fcntl(fd, F_SETFL, val) == -1) { |
| 121 | debug("fcntl(%d, F_SETFL, ~O_NONBLOCK): %s", |
Ben Lindstrom | 84fcb31 | 2002-03-05 01:48:09 +0000 | [diff] [blame] | 122 | fd, strerror(errno)); |
Damien Miller | 232711f | 2004-06-15 10:35:30 +1000 | [diff] [blame] | 123 | return (-1); |
| 124 | } |
| 125 | return (0); |
Ben Lindstrom | c93e84c | 2001-05-12 00:08:37 +0000 | [diff] [blame] | 126 | } |
| 127 | |
Darren Tucker | 4abde77 | 2007-12-29 02:43:51 +1100 | [diff] [blame] | 128 | const char * |
| 129 | ssh_gai_strerror(int gaierr) |
| 130 | { |
Damien Miller | 7313fc9 | 2013-07-18 16:13:19 +1000 | [diff] [blame] | 131 | if (gaierr == EAI_SYSTEM && errno != 0) |
Darren Tucker | 912428c | 2008-01-01 20:33:35 +1100 | [diff] [blame] | 132 | return strerror(errno); |
| 133 | return gai_strerror(gaierr); |
Darren Tucker | 4abde77 | 2007-12-29 02:43:51 +1100 | [diff] [blame] | 134 | } |
| 135 | |
Damien Miller | 398e1cf | 2002-02-05 11:52:13 +1100 | [diff] [blame] | 136 | /* disable nagle on socket */ |
| 137 | void |
| 138 | set_nodelay(int fd) |
| 139 | { |
Ben Lindstrom | e86de51 | 2002-03-05 01:28:14 +0000 | [diff] [blame] | 140 | int opt; |
| 141 | socklen_t optlen; |
Damien Miller | 398e1cf | 2002-02-05 11:52:13 +1100 | [diff] [blame] | 142 | |
Ben Lindstrom | 1ebd7a5 | 2002-02-26 18:12:51 +0000 | [diff] [blame] | 143 | optlen = sizeof opt; |
| 144 | if (getsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &opt, &optlen) == -1) { |
Darren Tucker | 6db8f93 | 2003-11-03 20:07:14 +1100 | [diff] [blame] | 145 | debug("getsockopt TCP_NODELAY: %.100s", strerror(errno)); |
Ben Lindstrom | 1ebd7a5 | 2002-02-26 18:12:51 +0000 | [diff] [blame] | 146 | return; |
| 147 | } |
| 148 | if (opt == 1) { |
| 149 | debug2("fd %d is TCP_NODELAY", fd); |
| 150 | return; |
| 151 | } |
| 152 | opt = 1; |
Ben Lindstrom | 1d568f9 | 2002-12-23 02:44:36 +0000 | [diff] [blame] | 153 | debug2("fd %d setting TCP_NODELAY", fd); |
Ben Lindstrom | 1ebd7a5 | 2002-02-26 18:12:51 +0000 | [diff] [blame] | 154 | if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &opt, sizeof opt) == -1) |
Damien Miller | 398e1cf | 2002-02-05 11:52:13 +1100 | [diff] [blame] | 155 | error("setsockopt TCP_NODELAY: %.100s", strerror(errno)); |
| 156 | } |
| 157 | |
Damien Miller | 61c5150 | 2000-08-18 14:01:04 +1000 | [diff] [blame] | 158 | /* Characters considered whitespace in strsep calls. */ |
| 159 | #define WHITESPACE " \t\r\n" |
Damien Miller | 306d118 | 2006-03-15 12:05:59 +1100 | [diff] [blame] | 160 | #define QUOTE "\"" |
Damien Miller | 61c5150 | 2000-08-18 14:01:04 +1000 | [diff] [blame] | 161 | |
Ben Lindstrom | 4cc240d | 2001-07-04 04:46:56 +0000 | [diff] [blame] | 162 | /* return next token in configuration line */ |
Damien Miller | 61c5150 | 2000-08-18 14:01:04 +1000 | [diff] [blame] | 163 | char * |
| 164 | strdelim(char **s) |
| 165 | { |
| 166 | char *old; |
| 167 | int wspace = 0; |
| 168 | |
| 169 | if (*s == NULL) |
| 170 | return NULL; |
| 171 | |
| 172 | old = *s; |
| 173 | |
Damien Miller | 306d118 | 2006-03-15 12:05:59 +1100 | [diff] [blame] | 174 | *s = strpbrk(*s, WHITESPACE QUOTE "="); |
Damien Miller | 61c5150 | 2000-08-18 14:01:04 +1000 | [diff] [blame] | 175 | if (*s == NULL) |
| 176 | return (old); |
| 177 | |
Damien Miller | 306d118 | 2006-03-15 12:05:59 +1100 | [diff] [blame] | 178 | if (*s[0] == '\"') { |
| 179 | memmove(*s, *s + 1, strlen(*s)); /* move nul too */ |
| 180 | /* Find matching quote */ |
| 181 | if ((*s = strpbrk(*s, QUOTE)) == NULL) { |
| 182 | return (NULL); /* no matching quote */ |
| 183 | } else { |
| 184 | *s[0] = '\0'; |
Damien Miller | 9308fc7 | 2010-07-16 13:56:01 +1000 | [diff] [blame] | 185 | *s += strspn(*s + 1, WHITESPACE) + 1; |
Damien Miller | 306d118 | 2006-03-15 12:05:59 +1100 | [diff] [blame] | 186 | return (old); |
| 187 | } |
| 188 | } |
| 189 | |
Damien Miller | 61c5150 | 2000-08-18 14:01:04 +1000 | [diff] [blame] | 190 | /* Allow only one '=' to be skipped */ |
| 191 | if (*s[0] == '=') |
| 192 | wspace = 1; |
| 193 | *s[0] = '\0'; |
| 194 | |
Damien Miller | 306d118 | 2006-03-15 12:05:59 +1100 | [diff] [blame] | 195 | /* Skip any extra whitespace after first token */ |
Damien Miller | 61c5150 | 2000-08-18 14:01:04 +1000 | [diff] [blame] | 196 | *s += strspn(*s + 1, WHITESPACE) + 1; |
| 197 | if (*s[0] == '=' && !wspace) |
| 198 | *s += strspn(*s + 1, WHITESPACE) + 1; |
| 199 | |
| 200 | return (old); |
| 201 | } |
Kevin Steves | b6e773a | 2001-02-04 13:20:36 +0000 | [diff] [blame] | 202 | |
Ben Lindstrom | 086cf21 | 2001-03-05 05:56:40 +0000 | [diff] [blame] | 203 | struct passwd * |
| 204 | pwcopy(struct passwd *pw) |
| 205 | { |
Damien Miller | 07d86be | 2006-03-26 14:19:21 +1100 | [diff] [blame] | 206 | struct passwd *copy = xcalloc(1, sizeof(*copy)); |
Ben Lindstrom | 4030442 | 2001-03-05 06:22:01 +0000 | [diff] [blame] | 207 | |
Ben Lindstrom | 086cf21 | 2001-03-05 05:56:40 +0000 | [diff] [blame] | 208 | copy->pw_name = xstrdup(pw->pw_name); |
| 209 | copy->pw_passwd = xstrdup(pw->pw_passwd); |
Damien Miller | 6332da2 | 2013-04-23 14:25:52 +1000 | [diff] [blame] | 210 | #ifdef HAVE_STRUCT_PASSWD_PW_GECOS |
Ben Lindstrom | 4030442 | 2001-03-05 06:22:01 +0000 | [diff] [blame] | 211 | copy->pw_gecos = xstrdup(pw->pw_gecos); |
Damien Miller | 6332da2 | 2013-04-23 14:25:52 +1000 | [diff] [blame] | 212 | #endif |
Ben Lindstrom | 086cf21 | 2001-03-05 05:56:40 +0000 | [diff] [blame] | 213 | copy->pw_uid = pw->pw_uid; |
| 214 | copy->pw_gid = pw->pw_gid; |
Damien Miller | 6332da2 | 2013-04-23 14:25:52 +1000 | [diff] [blame] | 215 | #ifdef HAVE_STRUCT_PASSWD_PW_EXPIRE |
Ben Lindstrom | 3af4d46 | 2001-06-21 03:11:27 +0000 | [diff] [blame] | 216 | copy->pw_expire = pw->pw_expire; |
Kevin Steves | 8245695 | 2001-06-22 21:14:18 +0000 | [diff] [blame] | 217 | #endif |
Damien Miller | 6332da2 | 2013-04-23 14:25:52 +1000 | [diff] [blame] | 218 | #ifdef HAVE_STRUCT_PASSWD_PW_CHANGE |
Ben Lindstrom | 3af4d46 | 2001-06-21 03:11:27 +0000 | [diff] [blame] | 219 | copy->pw_change = pw->pw_change; |
Kevin Steves | 8245695 | 2001-06-22 21:14:18 +0000 | [diff] [blame] | 220 | #endif |
Damien Miller | 6332da2 | 2013-04-23 14:25:52 +1000 | [diff] [blame] | 221 | #ifdef HAVE_STRUCT_PASSWD_PW_CLASS |
Ben Lindstrom | 086cf21 | 2001-03-05 05:56:40 +0000 | [diff] [blame] | 222 | copy->pw_class = xstrdup(pw->pw_class); |
Ben Lindstrom | 0f68db4 | 2001-03-05 07:57:09 +0000 | [diff] [blame] | 223 | #endif |
Ben Lindstrom | 086cf21 | 2001-03-05 05:56:40 +0000 | [diff] [blame] | 224 | copy->pw_dir = xstrdup(pw->pw_dir); |
| 225 | copy->pw_shell = xstrdup(pw->pw_shell); |
| 226 | return copy; |
| 227 | } |
| 228 | |
Ben Lindstrom | 4cc240d | 2001-07-04 04:46:56 +0000 | [diff] [blame] | 229 | /* |
| 230 | * Convert ASCII string to TCP/IP port number. |
Damien Miller | 3dc71ad | 2009-01-28 16:31:22 +1100 | [diff] [blame] | 231 | * Port must be >=0 and <=65535. |
| 232 | * Return -1 if invalid. |
Ben Lindstrom | 4cc240d | 2001-07-04 04:46:56 +0000 | [diff] [blame] | 233 | */ |
| 234 | int |
| 235 | a2port(const char *s) |
Ben Lindstrom | 19066a1 | 2001-04-12 23:39:26 +0000 | [diff] [blame] | 236 | { |
Damien Miller | 3dc71ad | 2009-01-28 16:31:22 +1100 | [diff] [blame] | 237 | long long port; |
| 238 | const char *errstr; |
Ben Lindstrom | 19066a1 | 2001-04-12 23:39:26 +0000 | [diff] [blame] | 239 | |
Damien Miller | 3dc71ad | 2009-01-28 16:31:22 +1100 | [diff] [blame] | 240 | port = strtonum(s, 0, 65535, &errstr); |
| 241 | if (errstr != NULL) |
| 242 | return -1; |
| 243 | return (int)port; |
Ben Lindstrom | 19066a1 | 2001-04-12 23:39:26 +0000 | [diff] [blame] | 244 | } |
| 245 | |
Damien Miller | d27b947 | 2005-12-13 19:29:02 +1100 | [diff] [blame] | 246 | int |
| 247 | a2tun(const char *s, int *remote) |
| 248 | { |
| 249 | const char *errstr = NULL; |
| 250 | char *sp, *ep; |
| 251 | int tun; |
| 252 | |
| 253 | if (remote != NULL) { |
Damien Miller | 7b58e80 | 2005-12-13 19:33:19 +1100 | [diff] [blame] | 254 | *remote = SSH_TUNID_ANY; |
Damien Miller | d27b947 | 2005-12-13 19:29:02 +1100 | [diff] [blame] | 255 | sp = xstrdup(s); |
| 256 | if ((ep = strchr(sp, ':')) == NULL) { |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 257 | free(sp); |
Damien Miller | d27b947 | 2005-12-13 19:29:02 +1100 | [diff] [blame] | 258 | return (a2tun(s, NULL)); |
| 259 | } |
| 260 | ep[0] = '\0'; ep++; |
| 261 | *remote = a2tun(ep, NULL); |
| 262 | tun = a2tun(sp, NULL); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 263 | free(sp); |
Damien Miller | 7b58e80 | 2005-12-13 19:33:19 +1100 | [diff] [blame] | 264 | return (*remote == SSH_TUNID_ERR ? *remote : tun); |
Damien Miller | d27b947 | 2005-12-13 19:29:02 +1100 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | if (strcasecmp(s, "any") == 0) |
Damien Miller | 7b58e80 | 2005-12-13 19:33:19 +1100 | [diff] [blame] | 268 | return (SSH_TUNID_ANY); |
Damien Miller | d27b947 | 2005-12-13 19:29:02 +1100 | [diff] [blame] | 269 | |
Damien Miller | 7b58e80 | 2005-12-13 19:33:19 +1100 | [diff] [blame] | 270 | tun = strtonum(s, 0, SSH_TUNID_MAX, &errstr); |
| 271 | if (errstr != NULL) |
| 272 | return (SSH_TUNID_ERR); |
Damien Miller | d27b947 | 2005-12-13 19:29:02 +1100 | [diff] [blame] | 273 | |
| 274 | return (tun); |
| 275 | } |
| 276 | |
Ben Lindstrom | 1bda4c8 | 2001-06-05 19:59:08 +0000 | [diff] [blame] | 277 | #define SECONDS 1 |
| 278 | #define MINUTES (SECONDS * 60) |
| 279 | #define HOURS (MINUTES * 60) |
| 280 | #define DAYS (HOURS * 24) |
| 281 | #define WEEKS (DAYS * 7) |
| 282 | |
Ben Lindstrom | 4cc240d | 2001-07-04 04:46:56 +0000 | [diff] [blame] | 283 | /* |
| 284 | * Convert a time string into seconds; format is |
| 285 | * a sequence of: |
| 286 | * time[qualifier] |
| 287 | * |
| 288 | * Valid time qualifiers are: |
| 289 | * <none> seconds |
| 290 | * s|S seconds |
| 291 | * m|M minutes |
| 292 | * h|H hours |
| 293 | * d|D days |
| 294 | * w|W weeks |
| 295 | * |
| 296 | * Examples: |
| 297 | * 90m 90 minutes |
| 298 | * 1h30m 90 minutes |
| 299 | * 2d 2 days |
| 300 | * 1w 1 week |
| 301 | * |
| 302 | * Return -1 if time string is invalid. |
| 303 | */ |
| 304 | long |
| 305 | convtime(const char *s) |
Ben Lindstrom | 1bda4c8 | 2001-06-05 19:59:08 +0000 | [diff] [blame] | 306 | { |
| 307 | long total, secs; |
| 308 | const char *p; |
| 309 | char *endp; |
| 310 | |
| 311 | errno = 0; |
| 312 | total = 0; |
| 313 | p = s; |
| 314 | |
| 315 | if (p == NULL || *p == '\0') |
| 316 | return -1; |
| 317 | |
| 318 | while (*p) { |
| 319 | secs = strtol(p, &endp, 10); |
| 320 | if (p == endp || |
| 321 | (errno == ERANGE && (secs == LONG_MIN || secs == LONG_MAX)) || |
| 322 | secs < 0) |
| 323 | return -1; |
| 324 | |
| 325 | switch (*endp++) { |
| 326 | case '\0': |
| 327 | endp--; |
Damien Miller | 69b7203 | 2006-03-26 14:02:35 +1100 | [diff] [blame] | 328 | break; |
Ben Lindstrom | 1bda4c8 | 2001-06-05 19:59:08 +0000 | [diff] [blame] | 329 | case 's': |
| 330 | case 'S': |
| 331 | break; |
| 332 | case 'm': |
| 333 | case 'M': |
| 334 | secs *= MINUTES; |
| 335 | break; |
| 336 | case 'h': |
| 337 | case 'H': |
| 338 | secs *= HOURS; |
| 339 | break; |
| 340 | case 'd': |
| 341 | case 'D': |
| 342 | secs *= DAYS; |
| 343 | break; |
| 344 | case 'w': |
| 345 | case 'W': |
| 346 | secs *= WEEKS; |
| 347 | break; |
| 348 | default: |
| 349 | return -1; |
| 350 | } |
| 351 | total += secs; |
| 352 | if (total < 0) |
| 353 | return -1; |
| 354 | p = endp; |
| 355 | } |
| 356 | |
| 357 | return total; |
| 358 | } |
| 359 | |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 360 | /* |
Darren Tucker | da34553 | 2006-07-10 23:04:19 +1000 | [diff] [blame] | 361 | * Returns a standardized host+port identifier string. |
| 362 | * Caller must free returned string. |
| 363 | */ |
| 364 | char * |
| 365 | put_host_port(const char *host, u_short port) |
| 366 | { |
| 367 | char *hoststr; |
| 368 | |
| 369 | if (port == 0 || port == SSH_DEFAULT_PORT) |
| 370 | return(xstrdup(host)); |
| 371 | if (asprintf(&hoststr, "[%s]:%d", host, (int)port) < 0) |
| 372 | fatal("put_host_port: asprintf: %s", strerror(errno)); |
| 373 | debug3("put_host_port: %s", hoststr); |
| 374 | return hoststr; |
| 375 | } |
| 376 | |
| 377 | /* |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 378 | * Search for next delimiter between hostnames/addresses and ports. |
| 379 | * Argument may be modified (for termination). |
| 380 | * Returns *cp if parsing succeeds. |
| 381 | * *cp is set to the start of the next delimiter, if one was found. |
| 382 | * If this is the last field, *cp is set to NULL. |
| 383 | */ |
| 384 | char * |
| 385 | hpdelim(char **cp) |
| 386 | { |
| 387 | char *s, *old; |
| 388 | |
| 389 | if (cp == NULL || *cp == NULL) |
| 390 | return NULL; |
| 391 | |
| 392 | old = s = *cp; |
| 393 | if (*s == '[') { |
| 394 | if ((s = strchr(s, ']')) == NULL) |
| 395 | return NULL; |
| 396 | else |
| 397 | s++; |
| 398 | } else if ((s = strpbrk(s, ":/")) == NULL) |
| 399 | s = *cp + strlen(*cp); /* skip to end (see first case below) */ |
| 400 | |
| 401 | switch (*s) { |
| 402 | case '\0': |
| 403 | *cp = NULL; /* no more fields*/ |
| 404 | break; |
Darren Tucker | 47eede7 | 2005-03-14 23:08:12 +1100 | [diff] [blame] | 405 | |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 406 | case ':': |
| 407 | case '/': |
| 408 | *s = '\0'; /* terminate */ |
| 409 | *cp = s + 1; |
| 410 | break; |
Darren Tucker | 47eede7 | 2005-03-14 23:08:12 +1100 | [diff] [blame] | 411 | |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 412 | default: |
| 413 | return NULL; |
| 414 | } |
| 415 | |
| 416 | return old; |
| 417 | } |
| 418 | |
Ben Lindstrom | 4529b70 | 2001-05-03 23:39:53 +0000 | [diff] [blame] | 419 | char * |
| 420 | cleanhostname(char *host) |
| 421 | { |
| 422 | if (*host == '[' && host[strlen(host) - 1] == ']') { |
| 423 | host[strlen(host) - 1] = '\0'; |
| 424 | return (host + 1); |
| 425 | } else |
| 426 | return host; |
| 427 | } |
| 428 | |
| 429 | char * |
| 430 | colon(char *cp) |
| 431 | { |
| 432 | int flag = 0; |
| 433 | |
| 434 | if (*cp == ':') /* Leading colon is part of file name. */ |
Damien Miller | 2e77446 | 2010-06-26 09:30:47 +1000 | [diff] [blame] | 435 | return NULL; |
Ben Lindstrom | 4529b70 | 2001-05-03 23:39:53 +0000 | [diff] [blame] | 436 | if (*cp == '[') |
| 437 | flag = 1; |
| 438 | |
| 439 | for (; *cp; ++cp) { |
| 440 | if (*cp == '@' && *(cp+1) == '[') |
| 441 | flag = 1; |
| 442 | if (*cp == ']' && *(cp+1) == ':' && flag) |
| 443 | return (cp+1); |
| 444 | if (*cp == ':' && !flag) |
| 445 | return (cp); |
| 446 | if (*cp == '/') |
Damien Miller | 2e77446 | 2010-06-26 09:30:47 +1000 | [diff] [blame] | 447 | return NULL; |
Ben Lindstrom | 4529b70 | 2001-05-03 23:39:53 +0000 | [diff] [blame] | 448 | } |
Damien Miller | 2e77446 | 2010-06-26 09:30:47 +1000 | [diff] [blame] | 449 | return NULL; |
Ben Lindstrom | 4529b70 | 2001-05-03 23:39:53 +0000 | [diff] [blame] | 450 | } |
| 451 | |
Ben Lindstrom | 4cc240d | 2001-07-04 04:46:56 +0000 | [diff] [blame] | 452 | /* function to assist building execv() arguments */ |
Ben Lindstrom | 387c472 | 2001-05-08 20:27:25 +0000 | [diff] [blame] | 453 | void |
| 454 | addargs(arglist *args, char *fmt, ...) |
| 455 | { |
| 456 | va_list ap; |
Damien Miller | 3eec6b7 | 2006-01-31 21:49:27 +1100 | [diff] [blame] | 457 | char *cp; |
Darren Tucker | c7a6fc4 | 2004-08-13 21:18:00 +1000 | [diff] [blame] | 458 | u_int nalloc; |
Damien Miller | 3eec6b7 | 2006-01-31 21:49:27 +1100 | [diff] [blame] | 459 | int r; |
Ben Lindstrom | 387c472 | 2001-05-08 20:27:25 +0000 | [diff] [blame] | 460 | |
| 461 | va_start(ap, fmt); |
Damien Miller | 3eec6b7 | 2006-01-31 21:49:27 +1100 | [diff] [blame] | 462 | r = vasprintf(&cp, fmt, ap); |
Ben Lindstrom | 387c472 | 2001-05-08 20:27:25 +0000 | [diff] [blame] | 463 | va_end(ap); |
Damien Miller | 3eec6b7 | 2006-01-31 21:49:27 +1100 | [diff] [blame] | 464 | if (r == -1) |
| 465 | fatal("addargs: argument too long"); |
Ben Lindstrom | 387c472 | 2001-05-08 20:27:25 +0000 | [diff] [blame] | 466 | |
Darren Tucker | fb16b24 | 2003-09-22 21:04:23 +1000 | [diff] [blame] | 467 | nalloc = args->nalloc; |
Ben Lindstrom | 387c472 | 2001-05-08 20:27:25 +0000 | [diff] [blame] | 468 | if (args->list == NULL) { |
Darren Tucker | fb16b24 | 2003-09-22 21:04:23 +1000 | [diff] [blame] | 469 | nalloc = 32; |
Ben Lindstrom | 387c472 | 2001-05-08 20:27:25 +0000 | [diff] [blame] | 470 | args->num = 0; |
Darren Tucker | fb16b24 | 2003-09-22 21:04:23 +1000 | [diff] [blame] | 471 | } else if (args->num+2 >= nalloc) |
| 472 | nalloc *= 2; |
Ben Lindstrom | 387c472 | 2001-05-08 20:27:25 +0000 | [diff] [blame] | 473 | |
Damien Miller | 3681209 | 2006-03-26 14:22:47 +1100 | [diff] [blame] | 474 | args->list = xrealloc(args->list, nalloc, sizeof(char *)); |
Darren Tucker | fb16b24 | 2003-09-22 21:04:23 +1000 | [diff] [blame] | 475 | args->nalloc = nalloc; |
Damien Miller | 3eec6b7 | 2006-01-31 21:49:27 +1100 | [diff] [blame] | 476 | args->list[args->num++] = cp; |
Ben Lindstrom | 387c472 | 2001-05-08 20:27:25 +0000 | [diff] [blame] | 477 | args->list[args->num] = NULL; |
| 478 | } |
Darren Tucker | 22cc741 | 2004-12-06 22:47:41 +1100 | [diff] [blame] | 479 | |
Damien Miller | 3eec6b7 | 2006-01-31 21:49:27 +1100 | [diff] [blame] | 480 | void |
| 481 | replacearg(arglist *args, u_int which, char *fmt, ...) |
| 482 | { |
| 483 | va_list ap; |
| 484 | char *cp; |
| 485 | int r; |
| 486 | |
| 487 | va_start(ap, fmt); |
| 488 | r = vasprintf(&cp, fmt, ap); |
| 489 | va_end(ap); |
| 490 | if (r == -1) |
| 491 | fatal("replacearg: argument too long"); |
| 492 | |
| 493 | if (which >= args->num) |
| 494 | fatal("replacearg: tried to replace invalid arg %d >= %d", |
| 495 | which, args->num); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 496 | free(args->list[which]); |
Damien Miller | 3eec6b7 | 2006-01-31 21:49:27 +1100 | [diff] [blame] | 497 | args->list[which] = cp; |
| 498 | } |
| 499 | |
| 500 | void |
| 501 | freeargs(arglist *args) |
| 502 | { |
| 503 | u_int i; |
| 504 | |
| 505 | if (args->list != NULL) { |
| 506 | for (i = 0; i < args->num; i++) |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 507 | free(args->list[i]); |
| 508 | free(args->list); |
Damien Miller | 3eec6b7 | 2006-01-31 21:49:27 +1100 | [diff] [blame] | 509 | args->nalloc = args->num = 0; |
| 510 | args->list = NULL; |
| 511 | } |
| 512 | } |
| 513 | |
Darren Tucker | 22cc741 | 2004-12-06 22:47:41 +1100 | [diff] [blame] | 514 | /* |
Damien Miller | 5fd38c0 | 2005-05-26 12:02:14 +1000 | [diff] [blame] | 515 | * Expands tildes in the file name. Returns data allocated by xmalloc. |
| 516 | * Warning: this calls getpw*. |
| 517 | */ |
| 518 | char * |
| 519 | tilde_expand_filename(const char *filename, uid_t uid) |
| 520 | { |
Darren Tucker | 2ca51bf | 2013-05-16 20:22:46 +1000 | [diff] [blame] | 521 | const char *path, *sep; |
| 522 | char user[128], *ret; |
Damien Miller | 5fd38c0 | 2005-05-26 12:02:14 +1000 | [diff] [blame] | 523 | struct passwd *pw; |
Damien Miller | eccb9de | 2005-06-17 12:59:34 +1000 | [diff] [blame] | 524 | u_int len, slash; |
Damien Miller | 5fd38c0 | 2005-05-26 12:02:14 +1000 | [diff] [blame] | 525 | |
| 526 | if (*filename != '~') |
| 527 | return (xstrdup(filename)); |
| 528 | filename++; |
| 529 | |
| 530 | path = strchr(filename, '/'); |
| 531 | if (path != NULL && path > filename) { /* ~user/path */ |
Damien Miller | eccb9de | 2005-06-17 12:59:34 +1000 | [diff] [blame] | 532 | slash = path - filename; |
| 533 | if (slash > sizeof(user) - 1) |
Damien Miller | 5fd38c0 | 2005-05-26 12:02:14 +1000 | [diff] [blame] | 534 | fatal("tilde_expand_filename: ~username too long"); |
Damien Miller | eccb9de | 2005-06-17 12:59:34 +1000 | [diff] [blame] | 535 | memcpy(user, filename, slash); |
| 536 | user[slash] = '\0'; |
Damien Miller | 5fd38c0 | 2005-05-26 12:02:14 +1000 | [diff] [blame] | 537 | if ((pw = getpwnam(user)) == NULL) |
| 538 | fatal("tilde_expand_filename: No such user %s", user); |
| 539 | } else if ((pw = getpwuid(uid)) == NULL) /* ~/path */ |
Darren Tucker | 7517b5b | 2008-06-13 14:48:59 +1000 | [diff] [blame] | 540 | fatal("tilde_expand_filename: No such uid %ld", (long)uid); |
Damien Miller | 5fd38c0 | 2005-05-26 12:02:14 +1000 | [diff] [blame] | 541 | |
Damien Miller | 5fd38c0 | 2005-05-26 12:02:14 +1000 | [diff] [blame] | 542 | /* Make sure directory has a trailing '/' */ |
| 543 | len = strlen(pw->pw_dir); |
Darren Tucker | 026d9db | 2013-05-16 20:23:52 +1000 | [diff] [blame] | 544 | if (len == 0 || pw->pw_dir[len - 1] != '/') |
Darren Tucker | 2ca51bf | 2013-05-16 20:22:46 +1000 | [diff] [blame] | 545 | sep = "/"; |
| 546 | else |
| 547 | sep = ""; |
Damien Miller | 5fd38c0 | 2005-05-26 12:02:14 +1000 | [diff] [blame] | 548 | |
| 549 | /* Skip leading '/' from specified path */ |
| 550 | if (path != NULL) |
| 551 | filename = path + 1; |
Darren Tucker | 2ca51bf | 2013-05-16 20:22:46 +1000 | [diff] [blame] | 552 | |
| 553 | if (xasprintf(&ret, "%s%s%s", pw->pw_dir, sep, filename) >= MAXPATHLEN) |
Damien Miller | 5fd38c0 | 2005-05-26 12:02:14 +1000 | [diff] [blame] | 554 | fatal("tilde_expand_filename: Path too long"); |
| 555 | |
Darren Tucker | 2ca51bf | 2013-05-16 20:22:46 +1000 | [diff] [blame] | 556 | return (ret); |
Damien Miller | 5fd38c0 | 2005-05-26 12:02:14 +1000 | [diff] [blame] | 557 | } |
| 558 | |
| 559 | /* |
Damien Miller | 6476cad | 2005-06-16 13:18:34 +1000 | [diff] [blame] | 560 | * Expand a string with a set of %[char] escapes. A number of escapes may be |
| 561 | * specified as (char *escape_chars, char *replacement) pairs. The list must |
Darren Tucker | bee73d5 | 2005-07-14 17:05:02 +1000 | [diff] [blame] | 562 | * be terminated by a NULL escape_char. Returns replaced string in memory |
Damien Miller | 6476cad | 2005-06-16 13:18:34 +1000 | [diff] [blame] | 563 | * allocated by xmalloc. |
| 564 | */ |
| 565 | char * |
| 566 | percent_expand(const char *string, ...) |
| 567 | { |
| 568 | #define EXPAND_MAX_KEYS 16 |
Darren Tucker | 70d8769 | 2010-01-08 18:49:16 +1100 | [diff] [blame] | 569 | u_int num_keys, i, j; |
Damien Miller | 6476cad | 2005-06-16 13:18:34 +1000 | [diff] [blame] | 570 | struct { |
| 571 | const char *key; |
| 572 | const char *repl; |
| 573 | } keys[EXPAND_MAX_KEYS]; |
Damien Miller | 6476cad | 2005-06-16 13:18:34 +1000 | [diff] [blame] | 574 | char buf[4096]; |
| 575 | va_list ap; |
| 576 | |
| 577 | /* Gather keys */ |
| 578 | va_start(ap, string); |
| 579 | for (num_keys = 0; num_keys < EXPAND_MAX_KEYS; num_keys++) { |
| 580 | keys[num_keys].key = va_arg(ap, char *); |
| 581 | if (keys[num_keys].key == NULL) |
| 582 | break; |
| 583 | keys[num_keys].repl = va_arg(ap, char *); |
| 584 | if (keys[num_keys].repl == NULL) |
Darren Tucker | 70d8769 | 2010-01-08 18:49:16 +1100 | [diff] [blame] | 585 | fatal("%s: NULL replacement", __func__); |
Damien Miller | 6476cad | 2005-06-16 13:18:34 +1000 | [diff] [blame] | 586 | } |
Darren Tucker | 70d8769 | 2010-01-08 18:49:16 +1100 | [diff] [blame] | 587 | if (num_keys == EXPAND_MAX_KEYS && va_arg(ap, char *) != NULL) |
| 588 | fatal("%s: too many keys", __func__); |
Damien Miller | 6476cad | 2005-06-16 13:18:34 +1000 | [diff] [blame] | 589 | va_end(ap); |
| 590 | |
Damien Miller | 6476cad | 2005-06-16 13:18:34 +1000 | [diff] [blame] | 591 | /* Expand string */ |
| 592 | *buf = '\0'; |
| 593 | for (i = 0; *string != '\0'; string++) { |
| 594 | if (*string != '%') { |
| 595 | append: |
| 596 | buf[i++] = *string; |
| 597 | if (i >= sizeof(buf)) |
Darren Tucker | 70d8769 | 2010-01-08 18:49:16 +1100 | [diff] [blame] | 598 | fatal("%s: string too long", __func__); |
Damien Miller | 6476cad | 2005-06-16 13:18:34 +1000 | [diff] [blame] | 599 | buf[i] = '\0'; |
| 600 | continue; |
| 601 | } |
| 602 | string++; |
Darren Tucker | 70d8769 | 2010-01-08 18:49:16 +1100 | [diff] [blame] | 603 | /* %% case */ |
Damien Miller | 6476cad | 2005-06-16 13:18:34 +1000 | [diff] [blame] | 604 | if (*string == '%') |
| 605 | goto append; |
| 606 | for (j = 0; j < num_keys; j++) { |
| 607 | if (strchr(keys[j].key, *string) != NULL) { |
| 608 | i = strlcat(buf, keys[j].repl, sizeof(buf)); |
| 609 | if (i >= sizeof(buf)) |
Darren Tucker | 70d8769 | 2010-01-08 18:49:16 +1100 | [diff] [blame] | 610 | fatal("%s: string too long", __func__); |
Damien Miller | 6476cad | 2005-06-16 13:18:34 +1000 | [diff] [blame] | 611 | break; |
| 612 | } |
| 613 | } |
| 614 | if (j >= num_keys) |
Darren Tucker | 70d8769 | 2010-01-08 18:49:16 +1100 | [diff] [blame] | 615 | fatal("%s: unknown key %%%c", __func__, *string); |
Damien Miller | 6476cad | 2005-06-16 13:18:34 +1000 | [diff] [blame] | 616 | } |
| 617 | return (xstrdup(buf)); |
| 618 | #undef EXPAND_MAX_KEYS |
| 619 | } |
| 620 | |
| 621 | /* |
Darren Tucker | 22cc741 | 2004-12-06 22:47:41 +1100 | [diff] [blame] | 622 | * Read an entire line from a public key file into a static buffer, discarding |
| 623 | * lines that exceed the buffer size. Returns 0 on success, -1 on failure. |
| 624 | */ |
| 625 | int |
| 626 | read_keyfile_line(FILE *f, const char *filename, char *buf, size_t bufsz, |
Darren Tucker | f0f9098 | 2004-12-11 13:39:50 +1100 | [diff] [blame] | 627 | u_long *lineno) |
Darren Tucker | 22cc741 | 2004-12-06 22:47:41 +1100 | [diff] [blame] | 628 | { |
| 629 | while (fgets(buf, bufsz, f) != NULL) { |
Damien Miller | 3ca8b77 | 2007-01-05 16:24:47 +1100 | [diff] [blame] | 630 | if (buf[0] == '\0') |
| 631 | continue; |
Darren Tucker | 22cc741 | 2004-12-06 22:47:41 +1100 | [diff] [blame] | 632 | (*lineno)++; |
| 633 | if (buf[strlen(buf) - 1] == '\n' || feof(f)) { |
| 634 | return 0; |
| 635 | } else { |
Darren Tucker | f0f9098 | 2004-12-11 13:39:50 +1100 | [diff] [blame] | 636 | debug("%s: %s line %lu exceeds size limit", __func__, |
| 637 | filename, *lineno); |
Darren Tucker | 22cc741 | 2004-12-06 22:47:41 +1100 | [diff] [blame] | 638 | /* discard remainder of line */ |
Darren Tucker | 47eede7 | 2005-03-14 23:08:12 +1100 | [diff] [blame] | 639 | while (fgetc(f) != '\n' && !feof(f)) |
Darren Tucker | 22cc741 | 2004-12-06 22:47:41 +1100 | [diff] [blame] | 640 | ; /* nothing */ |
| 641 | } |
| 642 | } |
| 643 | return -1; |
| 644 | } |
Damien Miller | 1339002 | 2005-07-06 09:44:19 +1000 | [diff] [blame] | 645 | |
Damien Miller | d27b947 | 2005-12-13 19:29:02 +1100 | [diff] [blame] | 646 | int |
Damien Miller | 7b58e80 | 2005-12-13 19:33:19 +1100 | [diff] [blame] | 647 | tun_open(int tun, int mode) |
Damien Miller | d27b947 | 2005-12-13 19:29:02 +1100 | [diff] [blame] | 648 | { |
Damien Miller | 62a31c9 | 2005-12-13 20:44:13 +1100 | [diff] [blame] | 649 | #if defined(CUSTOM_SYS_TUN_OPEN) |
| 650 | return (sys_tun_open(tun, mode)); |
Damien Miller | 2dcddbf | 2006-01-01 19:47:05 +1100 | [diff] [blame] | 651 | #elif defined(SSH_TUN_OPENBSD) |
Damien Miller | 7b58e80 | 2005-12-13 19:33:19 +1100 | [diff] [blame] | 652 | struct ifreq ifr; |
Damien Miller | d27b947 | 2005-12-13 19:29:02 +1100 | [diff] [blame] | 653 | char name[100]; |
Damien Miller | 7b58e80 | 2005-12-13 19:33:19 +1100 | [diff] [blame] | 654 | int fd = -1, sock; |
Damien Miller | d27b947 | 2005-12-13 19:29:02 +1100 | [diff] [blame] | 655 | |
Damien Miller | 7b58e80 | 2005-12-13 19:33:19 +1100 | [diff] [blame] | 656 | /* Open the tunnel device */ |
| 657 | if (tun <= SSH_TUNID_MAX) { |
Damien Miller | d27b947 | 2005-12-13 19:29:02 +1100 | [diff] [blame] | 658 | snprintf(name, sizeof(name), "/dev/tun%d", tun); |
Damien Miller | 7b58e80 | 2005-12-13 19:33:19 +1100 | [diff] [blame] | 659 | fd = open(name, O_RDWR); |
| 660 | } else if (tun == SSH_TUNID_ANY) { |
| 661 | for (tun = 100; tun >= 0; tun--) { |
| 662 | snprintf(name, sizeof(name), "/dev/tun%d", tun); |
| 663 | if ((fd = open(name, O_RDWR)) >= 0) |
| 664 | break; |
Damien Miller | d27b947 | 2005-12-13 19:29:02 +1100 | [diff] [blame] | 665 | } |
| 666 | } else { |
Damien Miller | a210d52 | 2006-01-02 23:40:30 +1100 | [diff] [blame] | 667 | debug("%s: invalid tunnel %u", __func__, tun); |
Damien Miller | 7b58e80 | 2005-12-13 19:33:19 +1100 | [diff] [blame] | 668 | return (-1); |
Damien Miller | d27b947 | 2005-12-13 19:29:02 +1100 | [diff] [blame] | 669 | } |
Damien Miller | 7b58e80 | 2005-12-13 19:33:19 +1100 | [diff] [blame] | 670 | |
| 671 | if (fd < 0) { |
| 672 | debug("%s: %s open failed: %s", __func__, name, strerror(errno)); |
| 673 | return (-1); |
| 674 | } |
| 675 | |
| 676 | debug("%s: %s mode %d fd %d", __func__, name, mode, fd); |
| 677 | |
| 678 | /* Set the tunnel device operation mode */ |
| 679 | snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "tun%d", tun); |
| 680 | if ((sock = socket(PF_UNIX, SOCK_STREAM, 0)) == -1) |
| 681 | goto failed; |
| 682 | |
| 683 | if (ioctl(sock, SIOCGIFFLAGS, &ifr) == -1) |
| 684 | goto failed; |
Damien Miller | a1d9a18 | 2006-01-02 23:41:21 +1100 | [diff] [blame] | 685 | |
| 686 | /* Set interface mode */ |
| 687 | ifr.ifr_flags &= ~IFF_UP; |
| 688 | if (mode == SSH_TUNMODE_ETHERNET) |
Damien Miller | 7b58e80 | 2005-12-13 19:33:19 +1100 | [diff] [blame] | 689 | ifr.ifr_flags |= IFF_LINK0; |
Damien Miller | a1d9a18 | 2006-01-02 23:41:21 +1100 | [diff] [blame] | 690 | else |
| 691 | ifr.ifr_flags &= ~IFF_LINK0; |
| 692 | if (ioctl(sock, SIOCSIFFLAGS, &ifr) == -1) |
| 693 | goto failed; |
| 694 | |
| 695 | /* Bring interface up */ |
Damien Miller | 7b58e80 | 2005-12-13 19:33:19 +1100 | [diff] [blame] | 696 | ifr.ifr_flags |= IFF_UP; |
| 697 | if (ioctl(sock, SIOCSIFFLAGS, &ifr) == -1) |
| 698 | goto failed; |
| 699 | |
| 700 | close(sock); |
| 701 | return (fd); |
| 702 | |
| 703 | failed: |
| 704 | if (fd >= 0) |
| 705 | close(fd); |
| 706 | if (sock >= 0) |
| 707 | close(sock); |
| 708 | debug("%s: failed to set %s mode %d: %s", __func__, name, |
| 709 | mode, strerror(errno)); |
Damien Miller | d27b947 | 2005-12-13 19:29:02 +1100 | [diff] [blame] | 710 | return (-1); |
Damien Miller | 62a31c9 | 2005-12-13 20:44:13 +1100 | [diff] [blame] | 711 | #else |
| 712 | error("Tunnel interfaces are not supported on this platform"); |
| 713 | return (-1); |
| 714 | #endif |
Damien Miller | d27b947 | 2005-12-13 19:29:02 +1100 | [diff] [blame] | 715 | } |
| 716 | |
Darren Tucker | ce321d8 | 2005-10-03 18:11:24 +1000 | [diff] [blame] | 717 | void |
| 718 | sanitise_stdfd(void) |
| 719 | { |
Damien Miller | 72c5b7d | 2006-01-06 14:50:44 +1100 | [diff] [blame] | 720 | int nullfd, dupfd; |
Darren Tucker | ce321d8 | 2005-10-03 18:11:24 +1000 | [diff] [blame] | 721 | |
Damien Miller | 72c5b7d | 2006-01-06 14:50:44 +1100 | [diff] [blame] | 722 | if ((nullfd = dupfd = open(_PATH_DEVNULL, O_RDWR)) == -1) { |
Damien Miller | 9eab956 | 2009-02-22 08:47:02 +1100 | [diff] [blame] | 723 | fprintf(stderr, "Couldn't open /dev/null: %s\n", |
| 724 | strerror(errno)); |
Darren Tucker | ce321d8 | 2005-10-03 18:11:24 +1000 | [diff] [blame] | 725 | exit(1); |
| 726 | } |
Damien Miller | 72c5b7d | 2006-01-06 14:50:44 +1100 | [diff] [blame] | 727 | while (++dupfd <= 2) { |
| 728 | /* Only clobber closed fds */ |
| 729 | if (fcntl(dupfd, F_GETFL, 0) >= 0) |
| 730 | continue; |
| 731 | if (dup2(nullfd, dupfd) == -1) { |
Damien Miller | 9eab956 | 2009-02-22 08:47:02 +1100 | [diff] [blame] | 732 | fprintf(stderr, "dup2: %s\n", strerror(errno)); |
Darren Tucker | ce321d8 | 2005-10-03 18:11:24 +1000 | [diff] [blame] | 733 | exit(1); |
| 734 | } |
Darren Tucker | ce321d8 | 2005-10-03 18:11:24 +1000 | [diff] [blame] | 735 | } |
| 736 | if (nullfd > 2) |
| 737 | close(nullfd); |
| 738 | } |
| 739 | |
Damien Miller | 1339002 | 2005-07-06 09:44:19 +1000 | [diff] [blame] | 740 | char * |
Damien Miller | 3f94188 | 2006-03-31 23:13:02 +1100 | [diff] [blame] | 741 | tohex(const void *vp, size_t l) |
Damien Miller | 1339002 | 2005-07-06 09:44:19 +1000 | [diff] [blame] | 742 | { |
Damien Miller | 3f94188 | 2006-03-31 23:13:02 +1100 | [diff] [blame] | 743 | const u_char *p = (const u_char *)vp; |
Damien Miller | 1339002 | 2005-07-06 09:44:19 +1000 | [diff] [blame] | 744 | char b[3], *r; |
Damien Miller | 3f94188 | 2006-03-31 23:13:02 +1100 | [diff] [blame] | 745 | size_t i, hl; |
| 746 | |
| 747 | if (l > 65536) |
| 748 | return xstrdup("tohex: length > 65536"); |
Damien Miller | 1339002 | 2005-07-06 09:44:19 +1000 | [diff] [blame] | 749 | |
| 750 | hl = l * 2 + 1; |
Damien Miller | 07d86be | 2006-03-26 14:19:21 +1100 | [diff] [blame] | 751 | r = xcalloc(1, hl); |
Damien Miller | 1339002 | 2005-07-06 09:44:19 +1000 | [diff] [blame] | 752 | for (i = 0; i < l; i++) { |
Damien Miller | 3f94188 | 2006-03-31 23:13:02 +1100 | [diff] [blame] | 753 | snprintf(b, sizeof(b), "%02x", p[i]); |
Damien Miller | 1339002 | 2005-07-06 09:44:19 +1000 | [diff] [blame] | 754 | strlcat(r, b, hl); |
| 755 | } |
| 756 | return (r); |
| 757 | } |
| 758 | |
Damien Miller | 3f94188 | 2006-03-31 23:13:02 +1100 | [diff] [blame] | 759 | u_int64_t |
| 760 | get_u64(const void *vp) |
| 761 | { |
| 762 | const u_char *p = (const u_char *)vp; |
| 763 | u_int64_t v; |
| 764 | |
| 765 | v = (u_int64_t)p[0] << 56; |
| 766 | v |= (u_int64_t)p[1] << 48; |
| 767 | v |= (u_int64_t)p[2] << 40; |
| 768 | v |= (u_int64_t)p[3] << 32; |
| 769 | v |= (u_int64_t)p[4] << 24; |
| 770 | v |= (u_int64_t)p[5] << 16; |
| 771 | v |= (u_int64_t)p[6] << 8; |
| 772 | v |= (u_int64_t)p[7]; |
| 773 | |
| 774 | return (v); |
| 775 | } |
| 776 | |
| 777 | u_int32_t |
| 778 | get_u32(const void *vp) |
| 779 | { |
| 780 | const u_char *p = (const u_char *)vp; |
| 781 | u_int32_t v; |
| 782 | |
| 783 | v = (u_int32_t)p[0] << 24; |
| 784 | v |= (u_int32_t)p[1] << 16; |
| 785 | v |= (u_int32_t)p[2] << 8; |
| 786 | v |= (u_int32_t)p[3]; |
| 787 | |
| 788 | return (v); |
| 789 | } |
| 790 | |
| 791 | u_int16_t |
| 792 | get_u16(const void *vp) |
| 793 | { |
| 794 | const u_char *p = (const u_char *)vp; |
| 795 | u_int16_t v; |
| 796 | |
| 797 | v = (u_int16_t)p[0] << 8; |
| 798 | v |= (u_int16_t)p[1]; |
| 799 | |
| 800 | return (v); |
| 801 | } |
| 802 | |
| 803 | void |
| 804 | put_u64(void *vp, u_int64_t v) |
| 805 | { |
| 806 | u_char *p = (u_char *)vp; |
| 807 | |
| 808 | p[0] = (u_char)(v >> 56) & 0xff; |
| 809 | p[1] = (u_char)(v >> 48) & 0xff; |
| 810 | p[2] = (u_char)(v >> 40) & 0xff; |
| 811 | p[3] = (u_char)(v >> 32) & 0xff; |
| 812 | p[4] = (u_char)(v >> 24) & 0xff; |
| 813 | p[5] = (u_char)(v >> 16) & 0xff; |
| 814 | p[6] = (u_char)(v >> 8) & 0xff; |
| 815 | p[7] = (u_char)v & 0xff; |
| 816 | } |
| 817 | |
| 818 | void |
| 819 | put_u32(void *vp, u_int32_t v) |
| 820 | { |
| 821 | u_char *p = (u_char *)vp; |
| 822 | |
| 823 | p[0] = (u_char)(v >> 24) & 0xff; |
| 824 | p[1] = (u_char)(v >> 16) & 0xff; |
| 825 | p[2] = (u_char)(v >> 8) & 0xff; |
| 826 | p[3] = (u_char)v & 0xff; |
| 827 | } |
| 828 | |
| 829 | |
| 830 | void |
| 831 | put_u16(void *vp, u_int16_t v) |
| 832 | { |
| 833 | u_char *p = (u_char *)vp; |
| 834 | |
| 835 | p[0] = (u_char)(v >> 8) & 0xff; |
| 836 | p[1] = (u_char)v & 0xff; |
| 837 | } |
Darren Tucker | 3fc464e | 2008-06-13 06:42:45 +1000 | [diff] [blame] | 838 | |
| 839 | void |
| 840 | ms_subtract_diff(struct timeval *start, int *ms) |
| 841 | { |
| 842 | struct timeval diff, finish; |
| 843 | |
| 844 | gettimeofday(&finish, NULL); |
| 845 | timersub(&finish, start, &diff); |
| 846 | *ms -= (diff.tv_sec * 1000) + (diff.tv_usec / 1000); |
| 847 | } |
| 848 | |
| 849 | void |
| 850 | ms_to_timeval(struct timeval *tv, int ms) |
| 851 | { |
| 852 | if (ms < 0) |
| 853 | ms = 0; |
| 854 | tv->tv_sec = ms / 1000; |
| 855 | tv->tv_usec = (ms % 1000) * 1000; |
| 856 | } |
| 857 | |
Darren Tucker | b759c9c | 2013-06-02 07:46:16 +1000 | [diff] [blame] | 858 | time_t |
| 859 | monotime(void) |
| 860 | { |
Darren Tucker | a710891 | 2013-06-02 08:18:31 +1000 | [diff] [blame] | 861 | #if defined(HAVE_CLOCK_GETTIME) && defined(CLOCK_MONOTONIC) |
Darren Tucker | b759c9c | 2013-06-02 07:46:16 +1000 | [diff] [blame] | 862 | struct timespec ts; |
Darren Tucker | 94396b7 | 2013-08-08 11:52:37 +1000 | [diff] [blame] | 863 | static int gettime_failed = 0; |
Darren Tucker | b759c9c | 2013-06-02 07:46:16 +1000 | [diff] [blame] | 864 | |
Darren Tucker | 94396b7 | 2013-08-08 11:52:37 +1000 | [diff] [blame] | 865 | if (!gettime_failed) { |
| 866 | if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0) |
| 867 | return (ts.tv_sec); |
| 868 | debug3("clock_gettime: %s", strerror(errno)); |
| 869 | gettime_failed = 1; |
| 870 | } |
Darren Tucker | a710891 | 2013-06-02 08:18:31 +1000 | [diff] [blame] | 871 | #endif |
Darren Tucker | 94396b7 | 2013-08-08 11:52:37 +1000 | [diff] [blame] | 872 | |
| 873 | return time(NULL); |
Darren Tucker | b759c9c | 2013-06-02 07:46:16 +1000 | [diff] [blame] | 874 | } |
| 875 | |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 876 | void |
| 877 | bandwidth_limit_init(struct bwlimit *bw, u_int64_t kbps, size_t buflen) |
| 878 | { |
| 879 | bw->buflen = buflen; |
| 880 | bw->rate = kbps; |
| 881 | bw->thresh = bw->rate; |
| 882 | bw->lamt = 0; |
| 883 | timerclear(&bw->bwstart); |
| 884 | timerclear(&bw->bwend); |
| 885 | } |
| 886 | |
| 887 | /* Callback from read/write loop to insert bandwidth-limiting delays */ |
| 888 | void |
| 889 | bandwidth_limit(struct bwlimit *bw, size_t read_len) |
| 890 | { |
| 891 | u_int64_t waitlen; |
| 892 | struct timespec ts, rm; |
| 893 | |
| 894 | if (!timerisset(&bw->bwstart)) { |
| 895 | gettimeofday(&bw->bwstart, NULL); |
| 896 | return; |
| 897 | } |
| 898 | |
| 899 | bw->lamt += read_len; |
| 900 | if (bw->lamt < bw->thresh) |
| 901 | return; |
| 902 | |
| 903 | gettimeofday(&bw->bwend, NULL); |
| 904 | timersub(&bw->bwend, &bw->bwstart, &bw->bwend); |
| 905 | if (!timerisset(&bw->bwend)) |
| 906 | return; |
| 907 | |
| 908 | bw->lamt *= 8; |
| 909 | waitlen = (double)1000000L * bw->lamt / bw->rate; |
| 910 | |
| 911 | bw->bwstart.tv_sec = waitlen / 1000000L; |
| 912 | bw->bwstart.tv_usec = waitlen % 1000000L; |
| 913 | |
| 914 | if (timercmp(&bw->bwstart, &bw->bwend, >)) { |
| 915 | timersub(&bw->bwstart, &bw->bwend, &bw->bwend); |
| 916 | |
| 917 | /* Adjust the wait time */ |
| 918 | if (bw->bwend.tv_sec) { |
| 919 | bw->thresh /= 2; |
| 920 | if (bw->thresh < bw->buflen / 4) |
| 921 | bw->thresh = bw->buflen / 4; |
| 922 | } else if (bw->bwend.tv_usec < 10000) { |
| 923 | bw->thresh *= 2; |
| 924 | if (bw->thresh > bw->buflen * 8) |
| 925 | bw->thresh = bw->buflen * 8; |
| 926 | } |
| 927 | |
| 928 | TIMEVAL_TO_TIMESPEC(&bw->bwend, &ts); |
| 929 | while (nanosleep(&ts, &rm) == -1) { |
| 930 | if (errno != EINTR) |
| 931 | break; |
| 932 | ts = rm; |
| 933 | } |
| 934 | } |
| 935 | |
| 936 | bw->lamt = 0; |
| 937 | gettimeofday(&bw->bwstart, NULL); |
| 938 | } |
Damien Miller | 0dac6fb | 2010-11-20 15:19:38 +1100 | [diff] [blame] | 939 | |
Damien Miller | 2cd6293 | 2010-12-01 11:50:35 +1100 | [diff] [blame] | 940 | /* Make a template filename for mk[sd]temp() */ |
| 941 | void |
| 942 | mktemp_proto(char *s, size_t len) |
| 943 | { |
| 944 | const char *tmpdir; |
| 945 | int r; |
| 946 | |
| 947 | if ((tmpdir = getenv("TMPDIR")) != NULL) { |
| 948 | r = snprintf(s, len, "%s/ssh-XXXXXXXXXXXX", tmpdir); |
| 949 | if (r > 0 && (size_t)r < len) |
| 950 | return; |
| 951 | } |
| 952 | r = snprintf(s, len, "/tmp/ssh-XXXXXXXXXXXX"); |
| 953 | if (r < 0 || (size_t)r >= len) |
| 954 | fatal("%s: template string too short", __func__); |
| 955 | } |
| 956 | |
Damien Miller | 0dac6fb | 2010-11-20 15:19:38 +1100 | [diff] [blame] | 957 | static const struct { |
| 958 | const char *name; |
| 959 | int value; |
| 960 | } ipqos[] = { |
| 961 | { "af11", IPTOS_DSCP_AF11 }, |
| 962 | { "af12", IPTOS_DSCP_AF12 }, |
| 963 | { "af13", IPTOS_DSCP_AF13 }, |
Damien Miller | 2918e03 | 2011-09-22 21:34:35 +1000 | [diff] [blame] | 964 | { "af21", IPTOS_DSCP_AF21 }, |
Damien Miller | 0dac6fb | 2010-11-20 15:19:38 +1100 | [diff] [blame] | 965 | { "af22", IPTOS_DSCP_AF22 }, |
| 966 | { "af23", IPTOS_DSCP_AF23 }, |
| 967 | { "af31", IPTOS_DSCP_AF31 }, |
| 968 | { "af32", IPTOS_DSCP_AF32 }, |
| 969 | { "af33", IPTOS_DSCP_AF33 }, |
| 970 | { "af41", IPTOS_DSCP_AF41 }, |
| 971 | { "af42", IPTOS_DSCP_AF42 }, |
| 972 | { "af43", IPTOS_DSCP_AF43 }, |
| 973 | { "cs0", IPTOS_DSCP_CS0 }, |
| 974 | { "cs1", IPTOS_DSCP_CS1 }, |
| 975 | { "cs2", IPTOS_DSCP_CS2 }, |
| 976 | { "cs3", IPTOS_DSCP_CS3 }, |
| 977 | { "cs4", IPTOS_DSCP_CS4 }, |
| 978 | { "cs5", IPTOS_DSCP_CS5 }, |
| 979 | { "cs6", IPTOS_DSCP_CS6 }, |
| 980 | { "cs7", IPTOS_DSCP_CS7 }, |
| 981 | { "ef", IPTOS_DSCP_EF }, |
| 982 | { "lowdelay", IPTOS_LOWDELAY }, |
| 983 | { "throughput", IPTOS_THROUGHPUT }, |
| 984 | { "reliability", IPTOS_RELIABILITY }, |
| 985 | { NULL, -1 } |
| 986 | }; |
| 987 | |
| 988 | int |
| 989 | parse_ipqos(const char *cp) |
| 990 | { |
| 991 | u_int i; |
| 992 | char *ep; |
| 993 | long val; |
| 994 | |
| 995 | if (cp == NULL) |
| 996 | return -1; |
| 997 | for (i = 0; ipqos[i].name != NULL; i++) { |
| 998 | if (strcasecmp(cp, ipqos[i].name) == 0) |
| 999 | return ipqos[i].value; |
| 1000 | } |
| 1001 | /* Try parsing as an integer */ |
| 1002 | val = strtol(cp, &ep, 0); |
| 1003 | if (*cp == '\0' || *ep != '\0' || val < 0 || val > 255) |
| 1004 | return -1; |
| 1005 | return val; |
| 1006 | } |
| 1007 | |
Damien Miller | 9147586 | 2011-05-05 14:14:34 +1000 | [diff] [blame] | 1008 | const char * |
| 1009 | iptos2str(int iptos) |
| 1010 | { |
| 1011 | int i; |
| 1012 | static char iptos_str[sizeof "0xff"]; |
| 1013 | |
| 1014 | for (i = 0; ipqos[i].name != NULL; i++) { |
| 1015 | if (ipqos[i].value == iptos) |
| 1016 | return ipqos[i].name; |
| 1017 | } |
| 1018 | snprintf(iptos_str, sizeof iptos_str, "0x%02x", iptos); |
| 1019 | return iptos_str; |
| 1020 | } |
Damien Miller | e9fc72e | 2013-10-15 12:14:12 +1100 | [diff] [blame] | 1021 | |
| 1022 | void |
| 1023 | lowercase(char *s) |
| 1024 | { |
| 1025 | for (; *s; s++) |
| 1026 | *s = tolower((u_char)*s); |
| 1027 | } |
Damien Miller | 04ee0f8 | 2009-11-18 17:48:30 +1100 | [diff] [blame] | 1028 | void |
| 1029 | sock_set_v6only(int s) |
| 1030 | { |
| 1031 | #ifdef IPV6_V6ONLY |
| 1032 | int on = 1; |
| 1033 | |
| 1034 | debug3("%s: set socket %d IPV6_V6ONLY", __func__, s); |
| 1035 | if (setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof(on)) == -1) |
| 1036 | error("setsockopt IPV6_V6ONLY: %s", strerror(errno)); |
| 1037 | #endif |
| 1038 | } |