Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2000 Markus Friedl. All rights reserved. |
Damien Miller | 6476cad | 2005-06-16 13:18:34 +1000 | [diff] [blame] | 3 | * Copyright (c) 2005 Damien Miller. All rights reserved. |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 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 | * |
| 14 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 15 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 16 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 17 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 18 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 19 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 20 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 21 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 23 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 24 | */ |
| 25 | |
Damien Miller | 61c5150 | 2000-08-18 14:01:04 +1000 | [diff] [blame] | 26 | #include "includes.h" |
Damien Miller | 72c5b7d | 2006-01-06 14:50:44 +1100 | [diff] [blame] | 27 | RCSID("$OpenBSD: misc.c,v 1.41 2006/01/05 23:43:53 djm Exp $"); |
Damien Miller | 3beb852 | 2006-01-02 23:40:10 +1100 | [diff] [blame] | 28 | |
| 29 | #ifdef SSH_TUN_OPENBSD |
| 30 | #include <net/if.h> |
| 31 | #endif |
Damien Miller | 61c5150 | 2000-08-18 14:01:04 +1000 | [diff] [blame] | 32 | |
Kevin Steves | b6e773a | 2001-02-04 13:20:36 +0000 | [diff] [blame] | 33 | #include "misc.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 34 | #include "log.h" |
Ben Lindstrom | 0690901 | 2001-03-05 06:09:31 +0000 | [diff] [blame] | 35 | #include "xmalloc.h" |
Damien Miller | 61c5150 | 2000-08-18 14:01:04 +1000 | [diff] [blame] | 36 | |
Ben Lindstrom | 4cc240d | 2001-07-04 04:46:56 +0000 | [diff] [blame] | 37 | /* remove newline at end of string */ |
Damien Miller | 61c5150 | 2000-08-18 14:01:04 +1000 | [diff] [blame] | 38 | char * |
| 39 | chop(char *s) |
| 40 | { |
| 41 | char *t = s; |
| 42 | while (*t) { |
Ben Lindstrom | 1c37c6a | 2001-12-06 18:00:18 +0000 | [diff] [blame] | 43 | if (*t == '\n' || *t == '\r') { |
Damien Miller | 61c5150 | 2000-08-18 14:01:04 +1000 | [diff] [blame] | 44 | *t = '\0'; |
| 45 | return s; |
| 46 | } |
| 47 | t++; |
| 48 | } |
| 49 | return s; |
| 50 | |
| 51 | } |
| 52 | |
Ben Lindstrom | 4cc240d | 2001-07-04 04:46:56 +0000 | [diff] [blame] | 53 | /* set/unset filedescriptor to non-blocking */ |
Damien Miller | 232711f | 2004-06-15 10:35:30 +1000 | [diff] [blame] | 54 | int |
Damien Miller | 61c5150 | 2000-08-18 14:01:04 +1000 | [diff] [blame] | 55 | set_nonblock(int fd) |
| 56 | { |
| 57 | int val; |
Ben Lindstrom | c93e84c | 2001-05-12 00:08:37 +0000 | [diff] [blame] | 58 | |
Damien Miller | 61c5150 | 2000-08-18 14:01:04 +1000 | [diff] [blame] | 59 | val = fcntl(fd, F_GETFL, 0); |
| 60 | if (val < 0) { |
| 61 | error("fcntl(%d, F_GETFL, 0): %s", fd, strerror(errno)); |
Damien Miller | 232711f | 2004-06-15 10:35:30 +1000 | [diff] [blame] | 62 | return (-1); |
Damien Miller | 61c5150 | 2000-08-18 14:01:04 +1000 | [diff] [blame] | 63 | } |
Damien Miller | 69b69aa | 2000-10-28 14:19:58 +1100 | [diff] [blame] | 64 | if (val & O_NONBLOCK) { |
Damien Miller | 232711f | 2004-06-15 10:35:30 +1000 | [diff] [blame] | 65 | debug3("fd %d is O_NONBLOCK", fd); |
| 66 | return (0); |
Damien Miller | 69b69aa | 2000-10-28 14:19:58 +1100 | [diff] [blame] | 67 | } |
Damien Miller | ef095ce | 2003-05-14 13:41:39 +1000 | [diff] [blame] | 68 | debug2("fd %d setting O_NONBLOCK", fd); |
Damien Miller | 61c5150 | 2000-08-18 14:01:04 +1000 | [diff] [blame] | 69 | val |= O_NONBLOCK; |
Damien Miller | 232711f | 2004-06-15 10:35:30 +1000 | [diff] [blame] | 70 | if (fcntl(fd, F_SETFL, val) == -1) { |
| 71 | debug("fcntl(%d, F_SETFL, O_NONBLOCK): %s", fd, |
| 72 | strerror(errno)); |
| 73 | return (-1); |
| 74 | } |
| 75 | return (0); |
Damien Miller | 61c5150 | 2000-08-18 14:01:04 +1000 | [diff] [blame] | 76 | } |
| 77 | |
Damien Miller | 232711f | 2004-06-15 10:35:30 +1000 | [diff] [blame] | 78 | int |
Ben Lindstrom | c93e84c | 2001-05-12 00:08:37 +0000 | [diff] [blame] | 79 | unset_nonblock(int fd) |
| 80 | { |
| 81 | int val; |
| 82 | |
| 83 | val = fcntl(fd, F_GETFL, 0); |
| 84 | if (val < 0) { |
| 85 | error("fcntl(%d, F_GETFL, 0): %s", fd, strerror(errno)); |
Damien Miller | 232711f | 2004-06-15 10:35:30 +1000 | [diff] [blame] | 86 | return (-1); |
Ben Lindstrom | c93e84c | 2001-05-12 00:08:37 +0000 | [diff] [blame] | 87 | } |
| 88 | if (!(val & O_NONBLOCK)) { |
Damien Miller | 232711f | 2004-06-15 10:35:30 +1000 | [diff] [blame] | 89 | debug3("fd %d is not O_NONBLOCK", fd); |
| 90 | return (0); |
Ben Lindstrom | c93e84c | 2001-05-12 00:08:37 +0000 | [diff] [blame] | 91 | } |
Ben Lindstrom | 352b1c2 | 2001-06-21 03:04:37 +0000 | [diff] [blame] | 92 | debug("fd %d clearing O_NONBLOCK", fd); |
Ben Lindstrom | c93e84c | 2001-05-12 00:08:37 +0000 | [diff] [blame] | 93 | val &= ~O_NONBLOCK; |
Damien Miller | 232711f | 2004-06-15 10:35:30 +1000 | [diff] [blame] | 94 | if (fcntl(fd, F_SETFL, val) == -1) { |
| 95 | debug("fcntl(%d, F_SETFL, ~O_NONBLOCK): %s", |
Ben Lindstrom | 84fcb31 | 2002-03-05 01:48:09 +0000 | [diff] [blame] | 96 | fd, strerror(errno)); |
Damien Miller | 232711f | 2004-06-15 10:35:30 +1000 | [diff] [blame] | 97 | return (-1); |
| 98 | } |
| 99 | return (0); |
Ben Lindstrom | c93e84c | 2001-05-12 00:08:37 +0000 | [diff] [blame] | 100 | } |
| 101 | |
Damien Miller | 398e1cf | 2002-02-05 11:52:13 +1100 | [diff] [blame] | 102 | /* disable nagle on socket */ |
| 103 | void |
| 104 | set_nodelay(int fd) |
| 105 | { |
Ben Lindstrom | e86de51 | 2002-03-05 01:28:14 +0000 | [diff] [blame] | 106 | int opt; |
| 107 | socklen_t optlen; |
Damien Miller | 398e1cf | 2002-02-05 11:52:13 +1100 | [diff] [blame] | 108 | |
Ben Lindstrom | 1ebd7a5 | 2002-02-26 18:12:51 +0000 | [diff] [blame] | 109 | optlen = sizeof opt; |
| 110 | if (getsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &opt, &optlen) == -1) { |
Darren Tucker | 6db8f93 | 2003-11-03 20:07:14 +1100 | [diff] [blame] | 111 | debug("getsockopt TCP_NODELAY: %.100s", strerror(errno)); |
Ben Lindstrom | 1ebd7a5 | 2002-02-26 18:12:51 +0000 | [diff] [blame] | 112 | return; |
| 113 | } |
| 114 | if (opt == 1) { |
| 115 | debug2("fd %d is TCP_NODELAY", fd); |
| 116 | return; |
| 117 | } |
| 118 | opt = 1; |
Ben Lindstrom | 1d568f9 | 2002-12-23 02:44:36 +0000 | [diff] [blame] | 119 | debug2("fd %d setting TCP_NODELAY", fd); |
Ben Lindstrom | 1ebd7a5 | 2002-02-26 18:12:51 +0000 | [diff] [blame] | 120 | if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &opt, sizeof opt) == -1) |
Damien Miller | 398e1cf | 2002-02-05 11:52:13 +1100 | [diff] [blame] | 121 | error("setsockopt TCP_NODELAY: %.100s", strerror(errno)); |
| 122 | } |
| 123 | |
Damien Miller | 61c5150 | 2000-08-18 14:01:04 +1000 | [diff] [blame] | 124 | /* Characters considered whitespace in strsep calls. */ |
| 125 | #define WHITESPACE " \t\r\n" |
| 126 | |
Ben Lindstrom | 4cc240d | 2001-07-04 04:46:56 +0000 | [diff] [blame] | 127 | /* return next token in configuration line */ |
Damien Miller | 61c5150 | 2000-08-18 14:01:04 +1000 | [diff] [blame] | 128 | char * |
| 129 | strdelim(char **s) |
| 130 | { |
| 131 | char *old; |
| 132 | int wspace = 0; |
| 133 | |
| 134 | if (*s == NULL) |
| 135 | return NULL; |
| 136 | |
| 137 | old = *s; |
| 138 | |
| 139 | *s = strpbrk(*s, WHITESPACE "="); |
| 140 | if (*s == NULL) |
| 141 | return (old); |
| 142 | |
| 143 | /* Allow only one '=' to be skipped */ |
| 144 | if (*s[0] == '=') |
| 145 | wspace = 1; |
| 146 | *s[0] = '\0'; |
| 147 | |
| 148 | *s += strspn(*s + 1, WHITESPACE) + 1; |
| 149 | if (*s[0] == '=' && !wspace) |
| 150 | *s += strspn(*s + 1, WHITESPACE) + 1; |
| 151 | |
| 152 | return (old); |
| 153 | } |
Kevin Steves | b6e773a | 2001-02-04 13:20:36 +0000 | [diff] [blame] | 154 | |
Ben Lindstrom | 086cf21 | 2001-03-05 05:56:40 +0000 | [diff] [blame] | 155 | struct passwd * |
| 156 | pwcopy(struct passwd *pw) |
| 157 | { |
| 158 | struct passwd *copy = xmalloc(sizeof(*copy)); |
Ben Lindstrom | 4030442 | 2001-03-05 06:22:01 +0000 | [diff] [blame] | 159 | |
Ben Lindstrom | 086cf21 | 2001-03-05 05:56:40 +0000 | [diff] [blame] | 160 | memset(copy, 0, sizeof(*copy)); |
| 161 | copy->pw_name = xstrdup(pw->pw_name); |
| 162 | copy->pw_passwd = xstrdup(pw->pw_passwd); |
Ben Lindstrom | 4030442 | 2001-03-05 06:22:01 +0000 | [diff] [blame] | 163 | copy->pw_gecos = xstrdup(pw->pw_gecos); |
Ben Lindstrom | 086cf21 | 2001-03-05 05:56:40 +0000 | [diff] [blame] | 164 | copy->pw_uid = pw->pw_uid; |
| 165 | copy->pw_gid = pw->pw_gid; |
Kevin Steves | 8245695 | 2001-06-22 21:14:18 +0000 | [diff] [blame] | 166 | #ifdef HAVE_PW_EXPIRE_IN_PASSWD |
Ben Lindstrom | 3af4d46 | 2001-06-21 03:11:27 +0000 | [diff] [blame] | 167 | copy->pw_expire = pw->pw_expire; |
Kevin Steves | 8245695 | 2001-06-22 21:14:18 +0000 | [diff] [blame] | 168 | #endif |
| 169 | #ifdef HAVE_PW_CHANGE_IN_PASSWD |
Ben Lindstrom | 3af4d46 | 2001-06-21 03:11:27 +0000 | [diff] [blame] | 170 | copy->pw_change = pw->pw_change; |
Kevin Steves | 8245695 | 2001-06-22 21:14:18 +0000 | [diff] [blame] | 171 | #endif |
Ben Lindstrom | 0f68db4 | 2001-03-05 07:57:09 +0000 | [diff] [blame] | 172 | #ifdef HAVE_PW_CLASS_IN_PASSWD |
Ben Lindstrom | 086cf21 | 2001-03-05 05:56:40 +0000 | [diff] [blame] | 173 | copy->pw_class = xstrdup(pw->pw_class); |
Ben Lindstrom | 0f68db4 | 2001-03-05 07:57:09 +0000 | [diff] [blame] | 174 | #endif |
Ben Lindstrom | 086cf21 | 2001-03-05 05:56:40 +0000 | [diff] [blame] | 175 | copy->pw_dir = xstrdup(pw->pw_dir); |
| 176 | copy->pw_shell = xstrdup(pw->pw_shell); |
| 177 | return copy; |
| 178 | } |
| 179 | |
Ben Lindstrom | 4cc240d | 2001-07-04 04:46:56 +0000 | [diff] [blame] | 180 | /* |
| 181 | * Convert ASCII string to TCP/IP port number. |
| 182 | * Port must be >0 and <=65535. |
| 183 | * Return 0 if invalid. |
| 184 | */ |
| 185 | int |
| 186 | a2port(const char *s) |
Ben Lindstrom | 19066a1 | 2001-04-12 23:39:26 +0000 | [diff] [blame] | 187 | { |
| 188 | long port; |
| 189 | char *endp; |
| 190 | |
| 191 | errno = 0; |
| 192 | port = strtol(s, &endp, 0); |
| 193 | if (s == endp || *endp != '\0' || |
| 194 | (errno == ERANGE && (port == LONG_MIN || port == LONG_MAX)) || |
| 195 | port <= 0 || port > 65535) |
| 196 | return 0; |
| 197 | |
| 198 | return port; |
| 199 | } |
| 200 | |
Damien Miller | d27b947 | 2005-12-13 19:29:02 +1100 | [diff] [blame] | 201 | int |
| 202 | a2tun(const char *s, int *remote) |
| 203 | { |
| 204 | const char *errstr = NULL; |
| 205 | char *sp, *ep; |
| 206 | int tun; |
| 207 | |
| 208 | if (remote != NULL) { |
Damien Miller | 7b58e80 | 2005-12-13 19:33:19 +1100 | [diff] [blame] | 209 | *remote = SSH_TUNID_ANY; |
Damien Miller | d27b947 | 2005-12-13 19:29:02 +1100 | [diff] [blame] | 210 | sp = xstrdup(s); |
| 211 | if ((ep = strchr(sp, ':')) == NULL) { |
| 212 | xfree(sp); |
| 213 | return (a2tun(s, NULL)); |
| 214 | } |
| 215 | ep[0] = '\0'; ep++; |
| 216 | *remote = a2tun(ep, NULL); |
| 217 | tun = a2tun(sp, NULL); |
| 218 | xfree(sp); |
Damien Miller | 7b58e80 | 2005-12-13 19:33:19 +1100 | [diff] [blame] | 219 | return (*remote == SSH_TUNID_ERR ? *remote : tun); |
Damien Miller | d27b947 | 2005-12-13 19:29:02 +1100 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | if (strcasecmp(s, "any") == 0) |
Damien Miller | 7b58e80 | 2005-12-13 19:33:19 +1100 | [diff] [blame] | 223 | return (SSH_TUNID_ANY); |
Damien Miller | d27b947 | 2005-12-13 19:29:02 +1100 | [diff] [blame] | 224 | |
Damien Miller | 7b58e80 | 2005-12-13 19:33:19 +1100 | [diff] [blame] | 225 | tun = strtonum(s, 0, SSH_TUNID_MAX, &errstr); |
| 226 | if (errstr != NULL) |
| 227 | return (SSH_TUNID_ERR); |
Damien Miller | d27b947 | 2005-12-13 19:29:02 +1100 | [diff] [blame] | 228 | |
| 229 | return (tun); |
| 230 | } |
| 231 | |
Ben Lindstrom | 1bda4c8 | 2001-06-05 19:59:08 +0000 | [diff] [blame] | 232 | #define SECONDS 1 |
| 233 | #define MINUTES (SECONDS * 60) |
| 234 | #define HOURS (MINUTES * 60) |
| 235 | #define DAYS (HOURS * 24) |
| 236 | #define WEEKS (DAYS * 7) |
| 237 | |
Ben Lindstrom | 4cc240d | 2001-07-04 04:46:56 +0000 | [diff] [blame] | 238 | /* |
| 239 | * Convert a time string into seconds; format is |
| 240 | * a sequence of: |
| 241 | * time[qualifier] |
| 242 | * |
| 243 | * Valid time qualifiers are: |
| 244 | * <none> seconds |
| 245 | * s|S seconds |
| 246 | * m|M minutes |
| 247 | * h|H hours |
| 248 | * d|D days |
| 249 | * w|W weeks |
| 250 | * |
| 251 | * Examples: |
| 252 | * 90m 90 minutes |
| 253 | * 1h30m 90 minutes |
| 254 | * 2d 2 days |
| 255 | * 1w 1 week |
| 256 | * |
| 257 | * Return -1 if time string is invalid. |
| 258 | */ |
| 259 | long |
| 260 | convtime(const char *s) |
Ben Lindstrom | 1bda4c8 | 2001-06-05 19:59:08 +0000 | [diff] [blame] | 261 | { |
| 262 | long total, secs; |
| 263 | const char *p; |
| 264 | char *endp; |
| 265 | |
| 266 | errno = 0; |
| 267 | total = 0; |
| 268 | p = s; |
| 269 | |
| 270 | if (p == NULL || *p == '\0') |
| 271 | return -1; |
| 272 | |
| 273 | while (*p) { |
| 274 | secs = strtol(p, &endp, 10); |
| 275 | if (p == endp || |
| 276 | (errno == ERANGE && (secs == LONG_MIN || secs == LONG_MAX)) || |
| 277 | secs < 0) |
| 278 | return -1; |
| 279 | |
| 280 | switch (*endp++) { |
| 281 | case '\0': |
| 282 | endp--; |
| 283 | case 's': |
| 284 | case 'S': |
| 285 | break; |
| 286 | case 'm': |
| 287 | case 'M': |
| 288 | secs *= MINUTES; |
| 289 | break; |
| 290 | case 'h': |
| 291 | case 'H': |
| 292 | secs *= HOURS; |
| 293 | break; |
| 294 | case 'd': |
| 295 | case 'D': |
| 296 | secs *= DAYS; |
| 297 | break; |
| 298 | case 'w': |
| 299 | case 'W': |
| 300 | secs *= WEEKS; |
| 301 | break; |
| 302 | default: |
| 303 | return -1; |
| 304 | } |
| 305 | total += secs; |
| 306 | if (total < 0) |
| 307 | return -1; |
| 308 | p = endp; |
| 309 | } |
| 310 | |
| 311 | return total; |
| 312 | } |
| 313 | |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 314 | /* |
| 315 | * Search for next delimiter between hostnames/addresses and ports. |
| 316 | * Argument may be modified (for termination). |
| 317 | * Returns *cp if parsing succeeds. |
| 318 | * *cp is set to the start of the next delimiter, if one was found. |
| 319 | * If this is the last field, *cp is set to NULL. |
| 320 | */ |
| 321 | char * |
| 322 | hpdelim(char **cp) |
| 323 | { |
| 324 | char *s, *old; |
| 325 | |
| 326 | if (cp == NULL || *cp == NULL) |
| 327 | return NULL; |
| 328 | |
| 329 | old = s = *cp; |
| 330 | if (*s == '[') { |
| 331 | if ((s = strchr(s, ']')) == NULL) |
| 332 | return NULL; |
| 333 | else |
| 334 | s++; |
| 335 | } else if ((s = strpbrk(s, ":/")) == NULL) |
| 336 | s = *cp + strlen(*cp); /* skip to end (see first case below) */ |
| 337 | |
| 338 | switch (*s) { |
| 339 | case '\0': |
| 340 | *cp = NULL; /* no more fields*/ |
| 341 | break; |
Darren Tucker | 47eede7 | 2005-03-14 23:08:12 +1100 | [diff] [blame] | 342 | |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 343 | case ':': |
| 344 | case '/': |
| 345 | *s = '\0'; /* terminate */ |
| 346 | *cp = s + 1; |
| 347 | break; |
Darren Tucker | 47eede7 | 2005-03-14 23:08:12 +1100 | [diff] [blame] | 348 | |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 349 | default: |
| 350 | return NULL; |
| 351 | } |
| 352 | |
| 353 | return old; |
| 354 | } |
| 355 | |
Ben Lindstrom | 4529b70 | 2001-05-03 23:39:53 +0000 | [diff] [blame] | 356 | char * |
| 357 | cleanhostname(char *host) |
| 358 | { |
| 359 | if (*host == '[' && host[strlen(host) - 1] == ']') { |
| 360 | host[strlen(host) - 1] = '\0'; |
| 361 | return (host + 1); |
| 362 | } else |
| 363 | return host; |
| 364 | } |
| 365 | |
| 366 | char * |
| 367 | colon(char *cp) |
| 368 | { |
| 369 | int flag = 0; |
| 370 | |
| 371 | if (*cp == ':') /* Leading colon is part of file name. */ |
| 372 | return (0); |
| 373 | if (*cp == '[') |
| 374 | flag = 1; |
| 375 | |
| 376 | for (; *cp; ++cp) { |
| 377 | if (*cp == '@' && *(cp+1) == '[') |
| 378 | flag = 1; |
| 379 | if (*cp == ']' && *(cp+1) == ':' && flag) |
| 380 | return (cp+1); |
| 381 | if (*cp == ':' && !flag) |
| 382 | return (cp); |
| 383 | if (*cp == '/') |
| 384 | return (0); |
| 385 | } |
| 386 | return (0); |
| 387 | } |
| 388 | |
Ben Lindstrom | 4cc240d | 2001-07-04 04:46:56 +0000 | [diff] [blame] | 389 | /* function to assist building execv() arguments */ |
Ben Lindstrom | 387c472 | 2001-05-08 20:27:25 +0000 | [diff] [blame] | 390 | void |
| 391 | addargs(arglist *args, char *fmt, ...) |
| 392 | { |
| 393 | va_list ap; |
| 394 | char buf[1024]; |
Darren Tucker | c7a6fc4 | 2004-08-13 21:18:00 +1000 | [diff] [blame] | 395 | u_int nalloc; |
Ben Lindstrom | 387c472 | 2001-05-08 20:27:25 +0000 | [diff] [blame] | 396 | |
| 397 | va_start(ap, fmt); |
| 398 | vsnprintf(buf, sizeof(buf), fmt, ap); |
| 399 | va_end(ap); |
| 400 | |
Darren Tucker | fb16b24 | 2003-09-22 21:04:23 +1000 | [diff] [blame] | 401 | nalloc = args->nalloc; |
Ben Lindstrom | 387c472 | 2001-05-08 20:27:25 +0000 | [diff] [blame] | 402 | if (args->list == NULL) { |
Darren Tucker | fb16b24 | 2003-09-22 21:04:23 +1000 | [diff] [blame] | 403 | nalloc = 32; |
Ben Lindstrom | 387c472 | 2001-05-08 20:27:25 +0000 | [diff] [blame] | 404 | args->num = 0; |
Darren Tucker | fb16b24 | 2003-09-22 21:04:23 +1000 | [diff] [blame] | 405 | } else if (args->num+2 >= nalloc) |
| 406 | nalloc *= 2; |
Ben Lindstrom | 387c472 | 2001-05-08 20:27:25 +0000 | [diff] [blame] | 407 | |
Darren Tucker | fb16b24 | 2003-09-22 21:04:23 +1000 | [diff] [blame] | 408 | args->list = xrealloc(args->list, nalloc * sizeof(char *)); |
| 409 | args->nalloc = nalloc; |
Ben Lindstrom | 387c472 | 2001-05-08 20:27:25 +0000 | [diff] [blame] | 410 | args->list[args->num++] = xstrdup(buf); |
| 411 | args->list[args->num] = NULL; |
| 412 | } |
Darren Tucker | 22cc741 | 2004-12-06 22:47:41 +1100 | [diff] [blame] | 413 | |
| 414 | /* |
Damien Miller | 5fd38c0 | 2005-05-26 12:02:14 +1000 | [diff] [blame] | 415 | * Expands tildes in the file name. Returns data allocated by xmalloc. |
| 416 | * Warning: this calls getpw*. |
| 417 | */ |
| 418 | char * |
| 419 | tilde_expand_filename(const char *filename, uid_t uid) |
| 420 | { |
| 421 | const char *path; |
| 422 | char user[128], ret[MAXPATHLEN]; |
| 423 | struct passwd *pw; |
Damien Miller | eccb9de | 2005-06-17 12:59:34 +1000 | [diff] [blame] | 424 | u_int len, slash; |
Damien Miller | 5fd38c0 | 2005-05-26 12:02:14 +1000 | [diff] [blame] | 425 | |
| 426 | if (*filename != '~') |
| 427 | return (xstrdup(filename)); |
| 428 | filename++; |
| 429 | |
| 430 | path = strchr(filename, '/'); |
| 431 | if (path != NULL && path > filename) { /* ~user/path */ |
Damien Miller | eccb9de | 2005-06-17 12:59:34 +1000 | [diff] [blame] | 432 | slash = path - filename; |
| 433 | if (slash > sizeof(user) - 1) |
Damien Miller | 5fd38c0 | 2005-05-26 12:02:14 +1000 | [diff] [blame] | 434 | fatal("tilde_expand_filename: ~username too long"); |
Damien Miller | eccb9de | 2005-06-17 12:59:34 +1000 | [diff] [blame] | 435 | memcpy(user, filename, slash); |
| 436 | user[slash] = '\0'; |
Damien Miller | 5fd38c0 | 2005-05-26 12:02:14 +1000 | [diff] [blame] | 437 | if ((pw = getpwnam(user)) == NULL) |
| 438 | fatal("tilde_expand_filename: No such user %s", user); |
| 439 | } else if ((pw = getpwuid(uid)) == NULL) /* ~/path */ |
| 440 | fatal("tilde_expand_filename: No such uid %d", uid); |
| 441 | |
| 442 | if (strlcpy(ret, pw->pw_dir, sizeof(ret)) >= sizeof(ret)) |
| 443 | fatal("tilde_expand_filename: Path too long"); |
| 444 | |
| 445 | /* Make sure directory has a trailing '/' */ |
| 446 | len = strlen(pw->pw_dir); |
| 447 | if ((len == 0 || pw->pw_dir[len - 1] != '/') && |
| 448 | strlcat(ret, "/", sizeof(ret)) >= sizeof(ret)) |
| 449 | fatal("tilde_expand_filename: Path too long"); |
| 450 | |
| 451 | /* Skip leading '/' from specified path */ |
| 452 | if (path != NULL) |
| 453 | filename = path + 1; |
| 454 | if (strlcat(ret, filename, sizeof(ret)) >= sizeof(ret)) |
| 455 | fatal("tilde_expand_filename: Path too long"); |
| 456 | |
| 457 | return (xstrdup(ret)); |
| 458 | } |
| 459 | |
| 460 | /* |
Damien Miller | 6476cad | 2005-06-16 13:18:34 +1000 | [diff] [blame] | 461 | * Expand a string with a set of %[char] escapes. A number of escapes may be |
| 462 | * specified as (char *escape_chars, char *replacement) pairs. The list must |
Darren Tucker | bee73d5 | 2005-07-14 17:05:02 +1000 | [diff] [blame] | 463 | * be terminated by a NULL escape_char. Returns replaced string in memory |
Damien Miller | 6476cad | 2005-06-16 13:18:34 +1000 | [diff] [blame] | 464 | * allocated by xmalloc. |
| 465 | */ |
| 466 | char * |
| 467 | percent_expand(const char *string, ...) |
| 468 | { |
| 469 | #define EXPAND_MAX_KEYS 16 |
| 470 | struct { |
| 471 | const char *key; |
| 472 | const char *repl; |
| 473 | } keys[EXPAND_MAX_KEYS]; |
Damien Miller | eccb9de | 2005-06-17 12:59:34 +1000 | [diff] [blame] | 474 | u_int num_keys, i, j; |
Damien Miller | 6476cad | 2005-06-16 13:18:34 +1000 | [diff] [blame] | 475 | char buf[4096]; |
| 476 | va_list ap; |
| 477 | |
| 478 | /* Gather keys */ |
| 479 | va_start(ap, string); |
| 480 | for (num_keys = 0; num_keys < EXPAND_MAX_KEYS; num_keys++) { |
| 481 | keys[num_keys].key = va_arg(ap, char *); |
| 482 | if (keys[num_keys].key == NULL) |
| 483 | break; |
| 484 | keys[num_keys].repl = va_arg(ap, char *); |
| 485 | if (keys[num_keys].repl == NULL) |
| 486 | fatal("percent_expand: NULL replacement"); |
| 487 | } |
| 488 | va_end(ap); |
| 489 | |
| 490 | if (num_keys >= EXPAND_MAX_KEYS) |
| 491 | fatal("percent_expand: too many keys"); |
| 492 | |
| 493 | /* Expand string */ |
| 494 | *buf = '\0'; |
| 495 | for (i = 0; *string != '\0'; string++) { |
| 496 | if (*string != '%') { |
| 497 | append: |
| 498 | buf[i++] = *string; |
| 499 | if (i >= sizeof(buf)) |
| 500 | fatal("percent_expand: string too long"); |
| 501 | buf[i] = '\0'; |
| 502 | continue; |
| 503 | } |
| 504 | string++; |
| 505 | if (*string == '%') |
| 506 | goto append; |
| 507 | for (j = 0; j < num_keys; j++) { |
| 508 | if (strchr(keys[j].key, *string) != NULL) { |
| 509 | i = strlcat(buf, keys[j].repl, sizeof(buf)); |
| 510 | if (i >= sizeof(buf)) |
| 511 | fatal("percent_expand: string too long"); |
| 512 | break; |
| 513 | } |
| 514 | } |
| 515 | if (j >= num_keys) |
| 516 | fatal("percent_expand: unknown key %%%c", *string); |
| 517 | } |
| 518 | return (xstrdup(buf)); |
| 519 | #undef EXPAND_MAX_KEYS |
| 520 | } |
| 521 | |
| 522 | /* |
Darren Tucker | 22cc741 | 2004-12-06 22:47:41 +1100 | [diff] [blame] | 523 | * Read an entire line from a public key file into a static buffer, discarding |
| 524 | * lines that exceed the buffer size. Returns 0 on success, -1 on failure. |
| 525 | */ |
| 526 | int |
| 527 | 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] | 528 | u_long *lineno) |
Darren Tucker | 22cc741 | 2004-12-06 22:47:41 +1100 | [diff] [blame] | 529 | { |
| 530 | while (fgets(buf, bufsz, f) != NULL) { |
| 531 | (*lineno)++; |
| 532 | if (buf[strlen(buf) - 1] == '\n' || feof(f)) { |
| 533 | return 0; |
| 534 | } else { |
Darren Tucker | f0f9098 | 2004-12-11 13:39:50 +1100 | [diff] [blame] | 535 | debug("%s: %s line %lu exceeds size limit", __func__, |
| 536 | filename, *lineno); |
Darren Tucker | 22cc741 | 2004-12-06 22:47:41 +1100 | [diff] [blame] | 537 | /* discard remainder of line */ |
Darren Tucker | 47eede7 | 2005-03-14 23:08:12 +1100 | [diff] [blame] | 538 | while (fgetc(f) != '\n' && !feof(f)) |
Darren Tucker | 22cc741 | 2004-12-06 22:47:41 +1100 | [diff] [blame] | 539 | ; /* nothing */ |
| 540 | } |
| 541 | } |
| 542 | return -1; |
| 543 | } |
Damien Miller | 1339002 | 2005-07-06 09:44:19 +1000 | [diff] [blame] | 544 | |
Damien Miller | d27b947 | 2005-12-13 19:29:02 +1100 | [diff] [blame] | 545 | int |
Damien Miller | 7b58e80 | 2005-12-13 19:33:19 +1100 | [diff] [blame] | 546 | tun_open(int tun, int mode) |
Damien Miller | d27b947 | 2005-12-13 19:29:02 +1100 | [diff] [blame] | 547 | { |
Damien Miller | 62a31c9 | 2005-12-13 20:44:13 +1100 | [diff] [blame] | 548 | #if defined(CUSTOM_SYS_TUN_OPEN) |
| 549 | return (sys_tun_open(tun, mode)); |
Damien Miller | 2dcddbf | 2006-01-01 19:47:05 +1100 | [diff] [blame] | 550 | #elif defined(SSH_TUN_OPENBSD) |
Damien Miller | 7b58e80 | 2005-12-13 19:33:19 +1100 | [diff] [blame] | 551 | struct ifreq ifr; |
Damien Miller | d27b947 | 2005-12-13 19:29:02 +1100 | [diff] [blame] | 552 | char name[100]; |
Damien Miller | 7b58e80 | 2005-12-13 19:33:19 +1100 | [diff] [blame] | 553 | int fd = -1, sock; |
Damien Miller | d27b947 | 2005-12-13 19:29:02 +1100 | [diff] [blame] | 554 | |
Damien Miller | 7b58e80 | 2005-12-13 19:33:19 +1100 | [diff] [blame] | 555 | /* Open the tunnel device */ |
| 556 | if (tun <= SSH_TUNID_MAX) { |
Damien Miller | d27b947 | 2005-12-13 19:29:02 +1100 | [diff] [blame] | 557 | snprintf(name, sizeof(name), "/dev/tun%d", tun); |
Damien Miller | 7b58e80 | 2005-12-13 19:33:19 +1100 | [diff] [blame] | 558 | fd = open(name, O_RDWR); |
| 559 | } else if (tun == SSH_TUNID_ANY) { |
| 560 | for (tun = 100; tun >= 0; tun--) { |
| 561 | snprintf(name, sizeof(name), "/dev/tun%d", tun); |
| 562 | if ((fd = open(name, O_RDWR)) >= 0) |
| 563 | break; |
Damien Miller | d27b947 | 2005-12-13 19:29:02 +1100 | [diff] [blame] | 564 | } |
| 565 | } else { |
Damien Miller | a210d52 | 2006-01-02 23:40:30 +1100 | [diff] [blame] | 566 | debug("%s: invalid tunnel %u", __func__, tun); |
Damien Miller | 7b58e80 | 2005-12-13 19:33:19 +1100 | [diff] [blame] | 567 | return (-1); |
Damien Miller | d27b947 | 2005-12-13 19:29:02 +1100 | [diff] [blame] | 568 | } |
Damien Miller | 7b58e80 | 2005-12-13 19:33:19 +1100 | [diff] [blame] | 569 | |
| 570 | if (fd < 0) { |
| 571 | debug("%s: %s open failed: %s", __func__, name, strerror(errno)); |
| 572 | return (-1); |
| 573 | } |
| 574 | |
| 575 | debug("%s: %s mode %d fd %d", __func__, name, mode, fd); |
| 576 | |
| 577 | /* Set the tunnel device operation mode */ |
| 578 | snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "tun%d", tun); |
| 579 | if ((sock = socket(PF_UNIX, SOCK_STREAM, 0)) == -1) |
| 580 | goto failed; |
| 581 | |
| 582 | if (ioctl(sock, SIOCGIFFLAGS, &ifr) == -1) |
| 583 | goto failed; |
Damien Miller | a1d9a18 | 2006-01-02 23:41:21 +1100 | [diff] [blame] | 584 | |
| 585 | /* Set interface mode */ |
| 586 | ifr.ifr_flags &= ~IFF_UP; |
| 587 | if (mode == SSH_TUNMODE_ETHERNET) |
Damien Miller | 7b58e80 | 2005-12-13 19:33:19 +1100 | [diff] [blame] | 588 | ifr.ifr_flags |= IFF_LINK0; |
Damien Miller | a1d9a18 | 2006-01-02 23:41:21 +1100 | [diff] [blame] | 589 | else |
| 590 | ifr.ifr_flags &= ~IFF_LINK0; |
| 591 | if (ioctl(sock, SIOCSIFFLAGS, &ifr) == -1) |
| 592 | goto failed; |
| 593 | |
| 594 | /* Bring interface up */ |
Damien Miller | 7b58e80 | 2005-12-13 19:33:19 +1100 | [diff] [blame] | 595 | ifr.ifr_flags |= IFF_UP; |
| 596 | if (ioctl(sock, SIOCSIFFLAGS, &ifr) == -1) |
| 597 | goto failed; |
| 598 | |
| 599 | close(sock); |
| 600 | return (fd); |
| 601 | |
| 602 | failed: |
| 603 | if (fd >= 0) |
| 604 | close(fd); |
| 605 | if (sock >= 0) |
| 606 | close(sock); |
| 607 | debug("%s: failed to set %s mode %d: %s", __func__, name, |
| 608 | mode, strerror(errno)); |
Damien Miller | d27b947 | 2005-12-13 19:29:02 +1100 | [diff] [blame] | 609 | return (-1); |
Damien Miller | 62a31c9 | 2005-12-13 20:44:13 +1100 | [diff] [blame] | 610 | #else |
| 611 | error("Tunnel interfaces are not supported on this platform"); |
| 612 | return (-1); |
| 613 | #endif |
Damien Miller | d27b947 | 2005-12-13 19:29:02 +1100 | [diff] [blame] | 614 | } |
| 615 | |
Darren Tucker | ce321d8 | 2005-10-03 18:11:24 +1000 | [diff] [blame] | 616 | void |
| 617 | sanitise_stdfd(void) |
| 618 | { |
Damien Miller | 72c5b7d | 2006-01-06 14:50:44 +1100 | [diff] [blame] | 619 | int nullfd, dupfd; |
Darren Tucker | ce321d8 | 2005-10-03 18:11:24 +1000 | [diff] [blame] | 620 | |
Damien Miller | 72c5b7d | 2006-01-06 14:50:44 +1100 | [diff] [blame] | 621 | if ((nullfd = dupfd = open(_PATH_DEVNULL, O_RDWR)) == -1) { |
Darren Tucker | ce321d8 | 2005-10-03 18:11:24 +1000 | [diff] [blame] | 622 | fprintf(stderr, "Couldn't open /dev/null: %s", strerror(errno)); |
| 623 | exit(1); |
| 624 | } |
Damien Miller | 72c5b7d | 2006-01-06 14:50:44 +1100 | [diff] [blame] | 625 | while (++dupfd <= 2) { |
| 626 | /* Only clobber closed fds */ |
| 627 | if (fcntl(dupfd, F_GETFL, 0) >= 0) |
| 628 | continue; |
| 629 | if (dup2(nullfd, dupfd) == -1) { |
Darren Tucker | ce321d8 | 2005-10-03 18:11:24 +1000 | [diff] [blame] | 630 | fprintf(stderr, "dup2: %s", strerror(errno)); |
| 631 | exit(1); |
| 632 | } |
Darren Tucker | ce321d8 | 2005-10-03 18:11:24 +1000 | [diff] [blame] | 633 | } |
| 634 | if (nullfd > 2) |
| 635 | close(nullfd); |
| 636 | } |
| 637 | |
Damien Miller | 1339002 | 2005-07-06 09:44:19 +1000 | [diff] [blame] | 638 | char * |
| 639 | tohex(const u_char *d, u_int l) |
| 640 | { |
| 641 | char b[3], *r; |
| 642 | u_int i, hl; |
| 643 | |
| 644 | hl = l * 2 + 1; |
| 645 | r = xmalloc(hl); |
| 646 | *r = '\0'; |
| 647 | for (i = 0; i < l; i++) { |
| 648 | snprintf(b, sizeof(b), "%02x", d[i]); |
| 649 | strlcat(r, b, hl); |
| 650 | } |
| 651 | return (r); |
| 652 | } |
| 653 | |