djm@openbsd.org | 7082bb5 | 2018-06-09 03:01:12 +0000 | [diff] [blame] | 1 | /* $OpenBSD: misc.c,v 1.129 2018/06/09 03:01:12 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> |
djm@openbsd.org | de4ae07 | 2017-08-18 05:36:45 +0000 | [diff] [blame] | 32 | #include <sys/stat.h> |
dtucker@openbsd.org | 65a3c0d | 2016-01-20 09:22:39 +0000 | [diff] [blame] | 33 | #include <sys/time.h> |
djm@openbsd.org | de4ae07 | 2017-08-18 05:36:45 +0000 | [diff] [blame] | 34 | #include <sys/wait.h> |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 35 | #include <sys/un.h> |
Damien Miller | 8ec8c3e | 2006-07-10 20:35:38 +1000 | [diff] [blame] | 36 | |
deraadt@openbsd.org | 2ae4f33 | 2015-01-16 06:40:12 +0000 | [diff] [blame] | 37 | #include <limits.h> |
djm@openbsd.org | de4ae07 | 2017-08-18 05:36:45 +0000 | [diff] [blame] | 38 | #ifdef HAVE_LIBGEN_H |
| 39 | # include <libgen.h> |
| 40 | #endif |
| 41 | #include <signal.h> |
Darren Tucker | 5d19626 | 2006-07-12 22:15:16 +1000 | [diff] [blame] | 42 | #include <stdarg.h> |
Damien Miller | a7a73ee | 2006-08-05 11:37:59 +1000 | [diff] [blame] | 43 | #include <stdio.h> |
Damien Miller | e7a1e5c | 2006-08-05 11:34:19 +1000 | [diff] [blame] | 44 | #include <stdlib.h> |
Damien Miller | e3476ed | 2006-07-24 14:13:33 +1000 | [diff] [blame] | 45 | #include <string.h> |
Damien Miller | 1708cb7 | 2011-01-13 12:21:34 +1100 | [diff] [blame] | 46 | #include <time.h> |
Damien Miller | e6b3b61 | 2006-07-24 14:01:23 +1000 | [diff] [blame] | 47 | #include <unistd.h> |
Darren Tucker | 5d19626 | 2006-07-12 22:15:16 +1000 | [diff] [blame] | 48 | |
Damien Miller | 8ec8c3e | 2006-07-10 20:35:38 +1000 | [diff] [blame] | 49 | #include <netinet/in.h> |
Damien Miller | 0dac6fb | 2010-11-20 15:19:38 +1100 | [diff] [blame] | 50 | #include <netinet/in_systm.h> |
| 51 | #include <netinet/ip.h> |
Damien Miller | 3a4051e | 2006-03-15 11:19:42 +1100 | [diff] [blame] | 52 | #include <netinet/tcp.h> |
Damien Miller | 8ec8c3e | 2006-07-10 20:35:38 +1000 | [diff] [blame] | 53 | |
Damien Miller | e9fc72e | 2013-10-15 12:14:12 +1100 | [diff] [blame] | 54 | #include <ctype.h> |
Darren Tucker | 3997249 | 2006-07-12 22:22:46 +1000 | [diff] [blame] | 55 | #include <errno.h> |
Damien Miller | 57cf638 | 2006-07-10 21:13:46 +1000 | [diff] [blame] | 56 | #include <fcntl.h> |
Darren Tucker | 4abde77 | 2007-12-29 02:43:51 +1100 | [diff] [blame] | 57 | #include <netdb.h> |
Damien Miller | 03e2003 | 2006-03-15 11:16:59 +1100 | [diff] [blame] | 58 | #ifdef HAVE_PATHS_H |
Damien Miller | a9263d0 | 2006-03-15 11:18:26 +1100 | [diff] [blame] | 59 | # include <paths.h> |
Damien Miller | 9f2abc4 | 2006-07-10 20:53:08 +1000 | [diff] [blame] | 60 | #include <pwd.h> |
Damien Miller | 03e2003 | 2006-03-15 11:16:59 +1100 | [diff] [blame] | 61 | #endif |
Damien Miller | 3beb852 | 2006-01-02 23:40:10 +1100 | [diff] [blame] | 62 | #ifdef SSH_TUN_OPENBSD |
| 63 | #include <net/if.h> |
| 64 | #endif |
Damien Miller | 61c5150 | 2000-08-18 14:01:04 +1000 | [diff] [blame] | 65 | |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 66 | #include "xmalloc.h" |
Kevin Steves | b6e773a | 2001-02-04 13:20:36 +0000 | [diff] [blame] | 67 | #include "misc.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 68 | #include "log.h" |
Darren Tucker | da34553 | 2006-07-10 23:04:19 +1000 | [diff] [blame] | 69 | #include "ssh.h" |
djm@openbsd.org | de4ae07 | 2017-08-18 05:36:45 +0000 | [diff] [blame] | 70 | #include "sshbuf.h" |
| 71 | #include "ssherr.h" |
| 72 | #include "uidswap.h" |
Damien Miller | 07949bf | 2017-08-23 20:13:18 +1000 | [diff] [blame] | 73 | #include "platform.h" |
Damien Miller | 61c5150 | 2000-08-18 14:01:04 +1000 | [diff] [blame] | 74 | |
Ben Lindstrom | 4cc240d | 2001-07-04 04:46:56 +0000 | [diff] [blame] | 75 | /* remove newline at end of string */ |
Damien Miller | 61c5150 | 2000-08-18 14:01:04 +1000 | [diff] [blame] | 76 | char * |
| 77 | chop(char *s) |
| 78 | { |
| 79 | char *t = s; |
| 80 | while (*t) { |
Ben Lindstrom | 1c37c6a | 2001-12-06 18:00:18 +0000 | [diff] [blame] | 81 | if (*t == '\n' || *t == '\r') { |
Damien Miller | 61c5150 | 2000-08-18 14:01:04 +1000 | [diff] [blame] | 82 | *t = '\0'; |
| 83 | return s; |
| 84 | } |
| 85 | t++; |
| 86 | } |
| 87 | return s; |
| 88 | |
| 89 | } |
| 90 | |
Ben Lindstrom | 4cc240d | 2001-07-04 04:46:56 +0000 | [diff] [blame] | 91 | /* set/unset filedescriptor to non-blocking */ |
Damien Miller | 232711f | 2004-06-15 10:35:30 +1000 | [diff] [blame] | 92 | int |
Damien Miller | 61c5150 | 2000-08-18 14:01:04 +1000 | [diff] [blame] | 93 | set_nonblock(int fd) |
| 94 | { |
| 95 | int val; |
Ben Lindstrom | c93e84c | 2001-05-12 00:08:37 +0000 | [diff] [blame] | 96 | |
krw@openbsd.org | 574def0 | 2016-04-02 14:37:42 +0000 | [diff] [blame] | 97 | val = fcntl(fd, F_GETFL); |
Damien Miller | 61c5150 | 2000-08-18 14:01:04 +1000 | [diff] [blame] | 98 | if (val < 0) { |
krw@openbsd.org | 574def0 | 2016-04-02 14:37:42 +0000 | [diff] [blame] | 99 | error("fcntl(%d, F_GETFL): %s", fd, strerror(errno)); |
Damien Miller | 232711f | 2004-06-15 10:35:30 +1000 | [diff] [blame] | 100 | return (-1); |
Damien Miller | 61c5150 | 2000-08-18 14:01:04 +1000 | [diff] [blame] | 101 | } |
Damien Miller | 69b69aa | 2000-10-28 14:19:58 +1100 | [diff] [blame] | 102 | if (val & O_NONBLOCK) { |
Damien Miller | 232711f | 2004-06-15 10:35:30 +1000 | [diff] [blame] | 103 | debug3("fd %d is O_NONBLOCK", fd); |
| 104 | return (0); |
Damien Miller | 69b69aa | 2000-10-28 14:19:58 +1100 | [diff] [blame] | 105 | } |
Damien Miller | ef095ce | 2003-05-14 13:41:39 +1000 | [diff] [blame] | 106 | debug2("fd %d setting O_NONBLOCK", fd); |
Damien Miller | 61c5150 | 2000-08-18 14:01:04 +1000 | [diff] [blame] | 107 | val |= O_NONBLOCK; |
Damien Miller | 232711f | 2004-06-15 10:35:30 +1000 | [diff] [blame] | 108 | if (fcntl(fd, F_SETFL, val) == -1) { |
| 109 | debug("fcntl(%d, F_SETFL, O_NONBLOCK): %s", fd, |
| 110 | strerror(errno)); |
| 111 | return (-1); |
| 112 | } |
| 113 | return (0); |
Damien Miller | 61c5150 | 2000-08-18 14:01:04 +1000 | [diff] [blame] | 114 | } |
| 115 | |
Damien Miller | 232711f | 2004-06-15 10:35:30 +1000 | [diff] [blame] | 116 | int |
Ben Lindstrom | c93e84c | 2001-05-12 00:08:37 +0000 | [diff] [blame] | 117 | unset_nonblock(int fd) |
| 118 | { |
| 119 | int val; |
| 120 | |
krw@openbsd.org | 574def0 | 2016-04-02 14:37:42 +0000 | [diff] [blame] | 121 | val = fcntl(fd, F_GETFL); |
Ben Lindstrom | c93e84c | 2001-05-12 00:08:37 +0000 | [diff] [blame] | 122 | if (val < 0) { |
krw@openbsd.org | 574def0 | 2016-04-02 14:37:42 +0000 | [diff] [blame] | 123 | error("fcntl(%d, F_GETFL): %s", fd, strerror(errno)); |
Damien Miller | 232711f | 2004-06-15 10:35:30 +1000 | [diff] [blame] | 124 | return (-1); |
Ben Lindstrom | c93e84c | 2001-05-12 00:08:37 +0000 | [diff] [blame] | 125 | } |
| 126 | if (!(val & O_NONBLOCK)) { |
Damien Miller | 232711f | 2004-06-15 10:35:30 +1000 | [diff] [blame] | 127 | debug3("fd %d is not O_NONBLOCK", fd); |
| 128 | return (0); |
Ben Lindstrom | c93e84c | 2001-05-12 00:08:37 +0000 | [diff] [blame] | 129 | } |
Ben Lindstrom | 352b1c2 | 2001-06-21 03:04:37 +0000 | [diff] [blame] | 130 | debug("fd %d clearing O_NONBLOCK", fd); |
Ben Lindstrom | c93e84c | 2001-05-12 00:08:37 +0000 | [diff] [blame] | 131 | val &= ~O_NONBLOCK; |
Damien Miller | 232711f | 2004-06-15 10:35:30 +1000 | [diff] [blame] | 132 | if (fcntl(fd, F_SETFL, val) == -1) { |
| 133 | debug("fcntl(%d, F_SETFL, ~O_NONBLOCK): %s", |
Ben Lindstrom | 84fcb31 | 2002-03-05 01:48:09 +0000 | [diff] [blame] | 134 | fd, strerror(errno)); |
Damien Miller | 232711f | 2004-06-15 10:35:30 +1000 | [diff] [blame] | 135 | return (-1); |
| 136 | } |
| 137 | return (0); |
Ben Lindstrom | c93e84c | 2001-05-12 00:08:37 +0000 | [diff] [blame] | 138 | } |
| 139 | |
Darren Tucker | 4abde77 | 2007-12-29 02:43:51 +1100 | [diff] [blame] | 140 | const char * |
| 141 | ssh_gai_strerror(int gaierr) |
| 142 | { |
Damien Miller | 7313fc9 | 2013-07-18 16:13:19 +1000 | [diff] [blame] | 143 | if (gaierr == EAI_SYSTEM && errno != 0) |
Darren Tucker | 912428c | 2008-01-01 20:33:35 +1100 | [diff] [blame] | 144 | return strerror(errno); |
| 145 | return gai_strerror(gaierr); |
Darren Tucker | 4abde77 | 2007-12-29 02:43:51 +1100 | [diff] [blame] | 146 | } |
| 147 | |
Damien Miller | 398e1cf | 2002-02-05 11:52:13 +1100 | [diff] [blame] | 148 | /* disable nagle on socket */ |
| 149 | void |
| 150 | set_nodelay(int fd) |
| 151 | { |
Ben Lindstrom | e86de51 | 2002-03-05 01:28:14 +0000 | [diff] [blame] | 152 | int opt; |
| 153 | socklen_t optlen; |
Damien Miller | 398e1cf | 2002-02-05 11:52:13 +1100 | [diff] [blame] | 154 | |
Ben Lindstrom | 1ebd7a5 | 2002-02-26 18:12:51 +0000 | [diff] [blame] | 155 | optlen = sizeof opt; |
| 156 | if (getsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &opt, &optlen) == -1) { |
Darren Tucker | 6db8f93 | 2003-11-03 20:07:14 +1100 | [diff] [blame] | 157 | debug("getsockopt TCP_NODELAY: %.100s", strerror(errno)); |
Ben Lindstrom | 1ebd7a5 | 2002-02-26 18:12:51 +0000 | [diff] [blame] | 158 | return; |
| 159 | } |
| 160 | if (opt == 1) { |
| 161 | debug2("fd %d is TCP_NODELAY", fd); |
| 162 | return; |
| 163 | } |
| 164 | opt = 1; |
Ben Lindstrom | 1d568f9 | 2002-12-23 02:44:36 +0000 | [diff] [blame] | 165 | debug2("fd %d setting TCP_NODELAY", fd); |
Ben Lindstrom | 1ebd7a5 | 2002-02-26 18:12:51 +0000 | [diff] [blame] | 166 | if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &opt, sizeof opt) == -1) |
Damien Miller | 398e1cf | 2002-02-05 11:52:13 +1100 | [diff] [blame] | 167 | error("setsockopt TCP_NODELAY: %.100s", strerror(errno)); |
| 168 | } |
| 169 | |
djm@openbsd.org | acf559e | 2017-10-25 00:15:35 +0000 | [diff] [blame] | 170 | /* Allow local port reuse in TIME_WAIT */ |
| 171 | int |
| 172 | set_reuseaddr(int fd) |
| 173 | { |
| 174 | int on = 1; |
| 175 | |
| 176 | if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) == -1) { |
| 177 | error("setsockopt SO_REUSEADDR fd %d: %s", fd, strerror(errno)); |
| 178 | return -1; |
| 179 | } |
| 180 | return 0; |
| 181 | } |
| 182 | |
djm@openbsd.org | 35eb33f | 2017-10-25 00:17:08 +0000 | [diff] [blame] | 183 | /* Get/set routing domain */ |
| 184 | char * |
| 185 | get_rdomain(int fd) |
| 186 | { |
Damien Miller | 43c29bb | 2017-10-25 13:10:59 +1100 | [diff] [blame] | 187 | #if defined(HAVE_SYS_GET_RDOMAIN) |
| 188 | return sys_get_rdomain(fd); |
| 189 | #elif defined(__OpenBSD__) |
djm@openbsd.org | 35eb33f | 2017-10-25 00:17:08 +0000 | [diff] [blame] | 190 | int rtable; |
| 191 | char *ret; |
| 192 | socklen_t len = sizeof(rtable); |
| 193 | |
| 194 | if (getsockopt(fd, SOL_SOCKET, SO_RTABLE, &rtable, &len) == -1) { |
| 195 | error("Failed to get routing domain for fd %d: %s", |
| 196 | fd, strerror(errno)); |
| 197 | return NULL; |
| 198 | } |
| 199 | xasprintf(&ret, "%d", rtable); |
| 200 | return ret; |
Damien Miller | 43c29bb | 2017-10-25 13:10:59 +1100 | [diff] [blame] | 201 | #else /* defined(__OpenBSD__) */ |
| 202 | return NULL; |
| 203 | #endif |
djm@openbsd.org | 35eb33f | 2017-10-25 00:17:08 +0000 | [diff] [blame] | 204 | } |
| 205 | |
djm@openbsd.org | acf559e | 2017-10-25 00:15:35 +0000 | [diff] [blame] | 206 | int |
| 207 | set_rdomain(int fd, const char *name) |
| 208 | { |
Damien Miller | 43c29bb | 2017-10-25 13:10:59 +1100 | [diff] [blame] | 209 | #if defined(HAVE_SYS_SET_RDOMAIN) |
| 210 | return sys_set_rdomain(fd, name); |
| 211 | #elif defined(__OpenBSD__) |
djm@openbsd.org | acf559e | 2017-10-25 00:15:35 +0000 | [diff] [blame] | 212 | int rtable; |
| 213 | const char *errstr; |
| 214 | |
| 215 | if (name == NULL) |
| 216 | return 0; /* default table */ |
| 217 | |
| 218 | rtable = (int)strtonum(name, 0, 255, &errstr); |
| 219 | if (errstr != NULL) { |
| 220 | /* Shouldn't happen */ |
| 221 | error("Invalid routing domain \"%s\": %s", name, errstr); |
| 222 | return -1; |
| 223 | } |
| 224 | if (setsockopt(fd, SOL_SOCKET, SO_RTABLE, |
| 225 | &rtable, sizeof(rtable)) == -1) { |
| 226 | error("Failed to set routing domain %d on fd %d: %s", |
| 227 | rtable, fd, strerror(errno)); |
| 228 | return -1; |
| 229 | } |
| 230 | return 0; |
Damien Miller | 43c29bb | 2017-10-25 13:10:59 +1100 | [diff] [blame] | 231 | #else /* defined(__OpenBSD__) */ |
| 232 | error("Setting routing domain is not supported on this platform"); |
| 233 | return -1; |
| 234 | #endif |
djm@openbsd.org | acf559e | 2017-10-25 00:15:35 +0000 | [diff] [blame] | 235 | } |
| 236 | |
Damien Miller | 61c5150 | 2000-08-18 14:01:04 +1000 | [diff] [blame] | 237 | /* Characters considered whitespace in strsep calls. */ |
| 238 | #define WHITESPACE " \t\r\n" |
Damien Miller | 306d118 | 2006-03-15 12:05:59 +1100 | [diff] [blame] | 239 | #define QUOTE "\"" |
Damien Miller | 61c5150 | 2000-08-18 14:01:04 +1000 | [diff] [blame] | 240 | |
Ben Lindstrom | 4cc240d | 2001-07-04 04:46:56 +0000 | [diff] [blame] | 241 | /* return next token in configuration line */ |
djm@openbsd.org | 7082bb5 | 2018-06-09 03:01:12 +0000 | [diff] [blame] | 242 | static char * |
| 243 | strdelim_internal(char **s, int split_equals) |
Damien Miller | 61c5150 | 2000-08-18 14:01:04 +1000 | [diff] [blame] | 244 | { |
djm@openbsd.org | c7c458e | 2018-03-07 23:53:08 +0000 | [diff] [blame] | 245 | char *old; |
Damien Miller | 61c5150 | 2000-08-18 14:01:04 +1000 | [diff] [blame] | 246 | int wspace = 0; |
| 247 | |
| 248 | if (*s == NULL) |
| 249 | return NULL; |
| 250 | |
| 251 | old = *s; |
| 252 | |
djm@openbsd.org | 7082bb5 | 2018-06-09 03:01:12 +0000 | [diff] [blame] | 253 | *s = strpbrk(*s, |
| 254 | split_equals ? WHITESPACE QUOTE "=" : WHITESPACE QUOTE); |
Damien Miller | 61c5150 | 2000-08-18 14:01:04 +1000 | [diff] [blame] | 255 | if (*s == NULL) |
| 256 | return (old); |
| 257 | |
Damien Miller | 306d118 | 2006-03-15 12:05:59 +1100 | [diff] [blame] | 258 | if (*s[0] == '\"') { |
| 259 | memmove(*s, *s + 1, strlen(*s)); /* move nul too */ |
| 260 | /* Find matching quote */ |
djm@openbsd.org | c7c458e | 2018-03-07 23:53:08 +0000 | [diff] [blame] | 261 | if ((*s = strpbrk(*s, QUOTE)) == NULL) { |
| 262 | return (NULL); /* no matching quote */ |
| 263 | } else { |
| 264 | *s[0] = '\0'; |
| 265 | *s += strspn(*s + 1, WHITESPACE) + 1; |
| 266 | return (old); |
Damien Miller | 306d118 | 2006-03-15 12:05:59 +1100 | [diff] [blame] | 267 | } |
| 268 | } |
| 269 | |
Damien Miller | 61c5150 | 2000-08-18 14:01:04 +1000 | [diff] [blame] | 270 | /* Allow only one '=' to be skipped */ |
djm@openbsd.org | 7082bb5 | 2018-06-09 03:01:12 +0000 | [diff] [blame] | 271 | if (split_equals && *s[0] == '=') |
Damien Miller | 61c5150 | 2000-08-18 14:01:04 +1000 | [diff] [blame] | 272 | wspace = 1; |
| 273 | *s[0] = '\0'; |
| 274 | |
Damien Miller | 306d118 | 2006-03-15 12:05:59 +1100 | [diff] [blame] | 275 | /* Skip any extra whitespace after first token */ |
Damien Miller | 61c5150 | 2000-08-18 14:01:04 +1000 | [diff] [blame] | 276 | *s += strspn(*s + 1, WHITESPACE) + 1; |
djm@openbsd.org | 7082bb5 | 2018-06-09 03:01:12 +0000 | [diff] [blame] | 277 | if (split_equals && *s[0] == '=' && !wspace) |
Damien Miller | 61c5150 | 2000-08-18 14:01:04 +1000 | [diff] [blame] | 278 | *s += strspn(*s + 1, WHITESPACE) + 1; |
| 279 | |
| 280 | return (old); |
| 281 | } |
Kevin Steves | b6e773a | 2001-02-04 13:20:36 +0000 | [diff] [blame] | 282 | |
djm@openbsd.org | 7082bb5 | 2018-06-09 03:01:12 +0000 | [diff] [blame] | 283 | /* |
| 284 | * Return next token in configuration line; splts on whitespace or a |
| 285 | * single '=' character. |
| 286 | */ |
| 287 | char * |
| 288 | strdelim(char **s) |
| 289 | { |
| 290 | return strdelim_internal(s, 1); |
| 291 | } |
| 292 | |
| 293 | /* |
| 294 | * Return next token in configuration line; splts on whitespace only. |
| 295 | */ |
| 296 | char * |
| 297 | strdelimw(char **s) |
| 298 | { |
| 299 | return strdelim_internal(s, 0); |
| 300 | } |
| 301 | |
Ben Lindstrom | 086cf21 | 2001-03-05 05:56:40 +0000 | [diff] [blame] | 302 | struct passwd * |
| 303 | pwcopy(struct passwd *pw) |
| 304 | { |
Damien Miller | 07d86be | 2006-03-26 14:19:21 +1100 | [diff] [blame] | 305 | struct passwd *copy = xcalloc(1, sizeof(*copy)); |
Ben Lindstrom | 4030442 | 2001-03-05 06:22:01 +0000 | [diff] [blame] | 306 | |
Ben Lindstrom | 086cf21 | 2001-03-05 05:56:40 +0000 | [diff] [blame] | 307 | copy->pw_name = xstrdup(pw->pw_name); |
| 308 | copy->pw_passwd = xstrdup(pw->pw_passwd); |
Damien Miller | 6332da2 | 2013-04-23 14:25:52 +1000 | [diff] [blame] | 309 | #ifdef HAVE_STRUCT_PASSWD_PW_GECOS |
Ben Lindstrom | 4030442 | 2001-03-05 06:22:01 +0000 | [diff] [blame] | 310 | copy->pw_gecos = xstrdup(pw->pw_gecos); |
Damien Miller | 6332da2 | 2013-04-23 14:25:52 +1000 | [diff] [blame] | 311 | #endif |
Ben Lindstrom | 086cf21 | 2001-03-05 05:56:40 +0000 | [diff] [blame] | 312 | copy->pw_uid = pw->pw_uid; |
| 313 | copy->pw_gid = pw->pw_gid; |
Damien Miller | 6332da2 | 2013-04-23 14:25:52 +1000 | [diff] [blame] | 314 | #ifdef HAVE_STRUCT_PASSWD_PW_EXPIRE |
Ben Lindstrom | 3af4d46 | 2001-06-21 03:11:27 +0000 | [diff] [blame] | 315 | copy->pw_expire = pw->pw_expire; |
Kevin Steves | 8245695 | 2001-06-22 21:14:18 +0000 | [diff] [blame] | 316 | #endif |
Damien Miller | 6332da2 | 2013-04-23 14:25:52 +1000 | [diff] [blame] | 317 | #ifdef HAVE_STRUCT_PASSWD_PW_CHANGE |
Ben Lindstrom | 3af4d46 | 2001-06-21 03:11:27 +0000 | [diff] [blame] | 318 | copy->pw_change = pw->pw_change; |
Kevin Steves | 8245695 | 2001-06-22 21:14:18 +0000 | [diff] [blame] | 319 | #endif |
Damien Miller | 6332da2 | 2013-04-23 14:25:52 +1000 | [diff] [blame] | 320 | #ifdef HAVE_STRUCT_PASSWD_PW_CLASS |
Ben Lindstrom | 086cf21 | 2001-03-05 05:56:40 +0000 | [diff] [blame] | 321 | copy->pw_class = xstrdup(pw->pw_class); |
Ben Lindstrom | 0f68db4 | 2001-03-05 07:57:09 +0000 | [diff] [blame] | 322 | #endif |
Ben Lindstrom | 086cf21 | 2001-03-05 05:56:40 +0000 | [diff] [blame] | 323 | copy->pw_dir = xstrdup(pw->pw_dir); |
| 324 | copy->pw_shell = xstrdup(pw->pw_shell); |
| 325 | return copy; |
| 326 | } |
| 327 | |
Ben Lindstrom | 4cc240d | 2001-07-04 04:46:56 +0000 | [diff] [blame] | 328 | /* |
| 329 | * Convert ASCII string to TCP/IP port number. |
Damien Miller | 3dc71ad | 2009-01-28 16:31:22 +1100 | [diff] [blame] | 330 | * Port must be >=0 and <=65535. |
| 331 | * Return -1 if invalid. |
Ben Lindstrom | 4cc240d | 2001-07-04 04:46:56 +0000 | [diff] [blame] | 332 | */ |
| 333 | int |
| 334 | a2port(const char *s) |
Ben Lindstrom | 19066a1 | 2001-04-12 23:39:26 +0000 | [diff] [blame] | 335 | { |
Damien Miller | 3dc71ad | 2009-01-28 16:31:22 +1100 | [diff] [blame] | 336 | long long port; |
| 337 | const char *errstr; |
Ben Lindstrom | 19066a1 | 2001-04-12 23:39:26 +0000 | [diff] [blame] | 338 | |
Damien Miller | 3dc71ad | 2009-01-28 16:31:22 +1100 | [diff] [blame] | 339 | port = strtonum(s, 0, 65535, &errstr); |
| 340 | if (errstr != NULL) |
| 341 | return -1; |
| 342 | return (int)port; |
Ben Lindstrom | 19066a1 | 2001-04-12 23:39:26 +0000 | [diff] [blame] | 343 | } |
| 344 | |
Damien Miller | d27b947 | 2005-12-13 19:29:02 +1100 | [diff] [blame] | 345 | int |
| 346 | a2tun(const char *s, int *remote) |
| 347 | { |
| 348 | const char *errstr = NULL; |
| 349 | char *sp, *ep; |
| 350 | int tun; |
| 351 | |
| 352 | if (remote != NULL) { |
Damien Miller | 7b58e80 | 2005-12-13 19:33:19 +1100 | [diff] [blame] | 353 | *remote = SSH_TUNID_ANY; |
Damien Miller | d27b947 | 2005-12-13 19:29:02 +1100 | [diff] [blame] | 354 | sp = xstrdup(s); |
| 355 | if ((ep = strchr(sp, ':')) == NULL) { |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 356 | free(sp); |
Damien Miller | d27b947 | 2005-12-13 19:29:02 +1100 | [diff] [blame] | 357 | return (a2tun(s, NULL)); |
| 358 | } |
| 359 | ep[0] = '\0'; ep++; |
| 360 | *remote = a2tun(ep, NULL); |
| 361 | tun = a2tun(sp, NULL); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 362 | free(sp); |
Damien Miller | 7b58e80 | 2005-12-13 19:33:19 +1100 | [diff] [blame] | 363 | return (*remote == SSH_TUNID_ERR ? *remote : tun); |
Damien Miller | d27b947 | 2005-12-13 19:29:02 +1100 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | if (strcasecmp(s, "any") == 0) |
Damien Miller | 7b58e80 | 2005-12-13 19:33:19 +1100 | [diff] [blame] | 367 | return (SSH_TUNID_ANY); |
Damien Miller | d27b947 | 2005-12-13 19:29:02 +1100 | [diff] [blame] | 368 | |
Damien Miller | 7b58e80 | 2005-12-13 19:33:19 +1100 | [diff] [blame] | 369 | tun = strtonum(s, 0, SSH_TUNID_MAX, &errstr); |
| 370 | if (errstr != NULL) |
| 371 | return (SSH_TUNID_ERR); |
Damien Miller | d27b947 | 2005-12-13 19:29:02 +1100 | [diff] [blame] | 372 | |
| 373 | return (tun); |
| 374 | } |
| 375 | |
Ben Lindstrom | 1bda4c8 | 2001-06-05 19:59:08 +0000 | [diff] [blame] | 376 | #define SECONDS 1 |
| 377 | #define MINUTES (SECONDS * 60) |
| 378 | #define HOURS (MINUTES * 60) |
| 379 | #define DAYS (HOURS * 24) |
| 380 | #define WEEKS (DAYS * 7) |
| 381 | |
Ben Lindstrom | 4cc240d | 2001-07-04 04:46:56 +0000 | [diff] [blame] | 382 | /* |
| 383 | * Convert a time string into seconds; format is |
| 384 | * a sequence of: |
| 385 | * time[qualifier] |
| 386 | * |
| 387 | * Valid time qualifiers are: |
| 388 | * <none> seconds |
| 389 | * s|S seconds |
| 390 | * m|M minutes |
| 391 | * h|H hours |
| 392 | * d|D days |
| 393 | * w|W weeks |
| 394 | * |
| 395 | * Examples: |
| 396 | * 90m 90 minutes |
| 397 | * 1h30m 90 minutes |
| 398 | * 2d 2 days |
| 399 | * 1w 1 week |
| 400 | * |
| 401 | * Return -1 if time string is invalid. |
| 402 | */ |
| 403 | long |
| 404 | convtime(const char *s) |
Ben Lindstrom | 1bda4c8 | 2001-06-05 19:59:08 +0000 | [diff] [blame] | 405 | { |
dtucker@openbsd.org | f5746b4 | 2017-03-14 00:25:03 +0000 | [diff] [blame] | 406 | long total, secs, multiplier = 1; |
Ben Lindstrom | 1bda4c8 | 2001-06-05 19:59:08 +0000 | [diff] [blame] | 407 | const char *p; |
| 408 | char *endp; |
| 409 | |
| 410 | errno = 0; |
| 411 | total = 0; |
| 412 | p = s; |
| 413 | |
| 414 | if (p == NULL || *p == '\0') |
| 415 | return -1; |
| 416 | |
| 417 | while (*p) { |
| 418 | secs = strtol(p, &endp, 10); |
| 419 | if (p == endp || |
| 420 | (errno == ERANGE && (secs == LONG_MIN || secs == LONG_MAX)) || |
| 421 | secs < 0) |
| 422 | return -1; |
| 423 | |
| 424 | switch (*endp++) { |
| 425 | case '\0': |
| 426 | endp--; |
Damien Miller | 69b7203 | 2006-03-26 14:02:35 +1100 | [diff] [blame] | 427 | break; |
Ben Lindstrom | 1bda4c8 | 2001-06-05 19:59:08 +0000 | [diff] [blame] | 428 | case 's': |
| 429 | case 'S': |
| 430 | break; |
| 431 | case 'm': |
| 432 | case 'M': |
dtucker@openbsd.org | f5746b4 | 2017-03-14 00:25:03 +0000 | [diff] [blame] | 433 | multiplier = MINUTES; |
Ben Lindstrom | 1bda4c8 | 2001-06-05 19:59:08 +0000 | [diff] [blame] | 434 | break; |
| 435 | case 'h': |
| 436 | case 'H': |
dtucker@openbsd.org | f5746b4 | 2017-03-14 00:25:03 +0000 | [diff] [blame] | 437 | multiplier = HOURS; |
Ben Lindstrom | 1bda4c8 | 2001-06-05 19:59:08 +0000 | [diff] [blame] | 438 | break; |
| 439 | case 'd': |
| 440 | case 'D': |
dtucker@openbsd.org | f5746b4 | 2017-03-14 00:25:03 +0000 | [diff] [blame] | 441 | multiplier = DAYS; |
Ben Lindstrom | 1bda4c8 | 2001-06-05 19:59:08 +0000 | [diff] [blame] | 442 | break; |
| 443 | case 'w': |
| 444 | case 'W': |
dtucker@openbsd.org | f5746b4 | 2017-03-14 00:25:03 +0000 | [diff] [blame] | 445 | multiplier = WEEKS; |
Ben Lindstrom | 1bda4c8 | 2001-06-05 19:59:08 +0000 | [diff] [blame] | 446 | break; |
| 447 | default: |
| 448 | return -1; |
| 449 | } |
dtucker@openbsd.org | c6774d2 | 2017-03-14 00:55:37 +0000 | [diff] [blame] | 450 | if (secs >= LONG_MAX / multiplier) |
dtucker@openbsd.org | f5746b4 | 2017-03-14 00:25:03 +0000 | [diff] [blame] | 451 | return -1; |
| 452 | secs *= multiplier; |
dtucker@openbsd.org | c6774d2 | 2017-03-14 00:55:37 +0000 | [diff] [blame] | 453 | if (total >= LONG_MAX - secs) |
dtucker@openbsd.org | f5746b4 | 2017-03-14 00:25:03 +0000 | [diff] [blame] | 454 | return -1; |
Ben Lindstrom | 1bda4c8 | 2001-06-05 19:59:08 +0000 | [diff] [blame] | 455 | total += secs; |
| 456 | if (total < 0) |
| 457 | return -1; |
| 458 | p = endp; |
| 459 | } |
| 460 | |
| 461 | return total; |
| 462 | } |
| 463 | |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 464 | /* |
Darren Tucker | da34553 | 2006-07-10 23:04:19 +1000 | [diff] [blame] | 465 | * Returns a standardized host+port identifier string. |
| 466 | * Caller must free returned string. |
| 467 | */ |
| 468 | char * |
| 469 | put_host_port(const char *host, u_short port) |
| 470 | { |
| 471 | char *hoststr; |
| 472 | |
| 473 | if (port == 0 || port == SSH_DEFAULT_PORT) |
| 474 | return(xstrdup(host)); |
| 475 | if (asprintf(&hoststr, "[%s]:%d", host, (int)port) < 0) |
| 476 | fatal("put_host_port: asprintf: %s", strerror(errno)); |
| 477 | debug3("put_host_port: %s", hoststr); |
| 478 | return hoststr; |
| 479 | } |
| 480 | |
| 481 | /* |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 482 | * Search for next delimiter between hostnames/addresses and ports. |
| 483 | * Argument may be modified (for termination). |
| 484 | * Returns *cp if parsing succeeds. |
millert@openbsd.org | 887669e | 2017-10-21 23:06:24 +0000 | [diff] [blame] | 485 | * *cp is set to the start of the next field, if one was found. |
| 486 | * The delimiter char, if present, is stored in delim. |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 487 | * If this is the last field, *cp is set to NULL. |
| 488 | */ |
millert@openbsd.org | 887669e | 2017-10-21 23:06:24 +0000 | [diff] [blame] | 489 | static char * |
| 490 | hpdelim2(char **cp, char *delim) |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 491 | { |
| 492 | char *s, *old; |
| 493 | |
| 494 | if (cp == NULL || *cp == NULL) |
| 495 | return NULL; |
| 496 | |
| 497 | old = s = *cp; |
| 498 | if (*s == '[') { |
| 499 | if ((s = strchr(s, ']')) == NULL) |
| 500 | return NULL; |
| 501 | else |
| 502 | s++; |
| 503 | } else if ((s = strpbrk(s, ":/")) == NULL) |
| 504 | s = *cp + strlen(*cp); /* skip to end (see first case below) */ |
| 505 | |
| 506 | switch (*s) { |
| 507 | case '\0': |
| 508 | *cp = NULL; /* no more fields*/ |
| 509 | break; |
Darren Tucker | 47eede7 | 2005-03-14 23:08:12 +1100 | [diff] [blame] | 510 | |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 511 | case ':': |
| 512 | case '/': |
millert@openbsd.org | 887669e | 2017-10-21 23:06:24 +0000 | [diff] [blame] | 513 | if (delim != NULL) |
| 514 | *delim = *s; |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 515 | *s = '\0'; /* terminate */ |
| 516 | *cp = s + 1; |
| 517 | break; |
Darren Tucker | 47eede7 | 2005-03-14 23:08:12 +1100 | [diff] [blame] | 518 | |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 519 | default: |
| 520 | return NULL; |
| 521 | } |
| 522 | |
| 523 | return old; |
| 524 | } |
| 525 | |
Ben Lindstrom | 4529b70 | 2001-05-03 23:39:53 +0000 | [diff] [blame] | 526 | char * |
millert@openbsd.org | 887669e | 2017-10-21 23:06:24 +0000 | [diff] [blame] | 527 | hpdelim(char **cp) |
| 528 | { |
| 529 | return hpdelim2(cp, NULL); |
| 530 | } |
| 531 | |
| 532 | char * |
Ben Lindstrom | 4529b70 | 2001-05-03 23:39:53 +0000 | [diff] [blame] | 533 | cleanhostname(char *host) |
| 534 | { |
| 535 | if (*host == '[' && host[strlen(host) - 1] == ']') { |
| 536 | host[strlen(host) - 1] = '\0'; |
| 537 | return (host + 1); |
| 538 | } else |
| 539 | return host; |
| 540 | } |
| 541 | |
| 542 | char * |
| 543 | colon(char *cp) |
| 544 | { |
| 545 | int flag = 0; |
| 546 | |
| 547 | if (*cp == ':') /* Leading colon is part of file name. */ |
Damien Miller | 2e77446 | 2010-06-26 09:30:47 +1000 | [diff] [blame] | 548 | return NULL; |
Ben Lindstrom | 4529b70 | 2001-05-03 23:39:53 +0000 | [diff] [blame] | 549 | if (*cp == '[') |
| 550 | flag = 1; |
| 551 | |
| 552 | for (; *cp; ++cp) { |
| 553 | if (*cp == '@' && *(cp+1) == '[') |
| 554 | flag = 1; |
| 555 | if (*cp == ']' && *(cp+1) == ':' && flag) |
| 556 | return (cp+1); |
| 557 | if (*cp == ':' && !flag) |
| 558 | return (cp); |
| 559 | if (*cp == '/') |
Damien Miller | 2e77446 | 2010-06-26 09:30:47 +1000 | [diff] [blame] | 560 | return NULL; |
Ben Lindstrom | 4529b70 | 2001-05-03 23:39:53 +0000 | [diff] [blame] | 561 | } |
Damien Miller | 2e77446 | 2010-06-26 09:30:47 +1000 | [diff] [blame] | 562 | return NULL; |
Ben Lindstrom | 4529b70 | 2001-05-03 23:39:53 +0000 | [diff] [blame] | 563 | } |
| 564 | |
djm@openbsd.org | ed877ef | 2016-07-15 00:24:30 +0000 | [diff] [blame] | 565 | /* |
millert@openbsd.org | 887669e | 2017-10-21 23:06:24 +0000 | [diff] [blame] | 566 | * Parse a [user@]host:[path] string. |
| 567 | * Caller must free returned user, host and path. |
| 568 | * Any of the pointer return arguments may be NULL (useful for syntax checking). |
| 569 | * If user was not specified then *userp will be set to NULL. |
| 570 | * If host was not specified then *hostp will be set to NULL. |
| 571 | * If path was not specified then *pathp will be set to ".". |
| 572 | * Returns 0 on success, -1 on failure. |
| 573 | */ |
| 574 | int |
| 575 | parse_user_host_path(const char *s, char **userp, char **hostp, char **pathp) |
| 576 | { |
| 577 | char *user = NULL, *host = NULL, *path = NULL; |
| 578 | char *sdup, *tmp; |
| 579 | int ret = -1; |
| 580 | |
| 581 | if (userp != NULL) |
| 582 | *userp = NULL; |
| 583 | if (hostp != NULL) |
| 584 | *hostp = NULL; |
| 585 | if (pathp != NULL) |
| 586 | *pathp = NULL; |
| 587 | |
millert@openbsd.org | b9903ee | 2017-10-24 19:41:45 +0000 | [diff] [blame] | 588 | sdup = xstrdup(s); |
millert@openbsd.org | 887669e | 2017-10-21 23:06:24 +0000 | [diff] [blame] | 589 | |
| 590 | /* Check for remote syntax: [user@]host:[path] */ |
| 591 | if ((tmp = colon(sdup)) == NULL) |
| 592 | goto out; |
| 593 | |
| 594 | /* Extract optional path */ |
| 595 | *tmp++ = '\0'; |
| 596 | if (*tmp == '\0') |
| 597 | tmp = "."; |
| 598 | path = xstrdup(tmp); |
| 599 | |
| 600 | /* Extract optional user and mandatory host */ |
| 601 | tmp = strrchr(sdup, '@'); |
| 602 | if (tmp != NULL) { |
| 603 | *tmp++ = '\0'; |
| 604 | host = xstrdup(cleanhostname(tmp)); |
| 605 | if (*sdup != '\0') |
| 606 | user = xstrdup(sdup); |
| 607 | } else { |
| 608 | host = xstrdup(cleanhostname(sdup)); |
| 609 | user = NULL; |
| 610 | } |
| 611 | |
| 612 | /* Success */ |
| 613 | if (userp != NULL) { |
| 614 | *userp = user; |
| 615 | user = NULL; |
| 616 | } |
| 617 | if (hostp != NULL) { |
| 618 | *hostp = host; |
| 619 | host = NULL; |
millert@openbsd.org | b9903ee | 2017-10-24 19:41:45 +0000 | [diff] [blame] | 620 | } |
millert@openbsd.org | 887669e | 2017-10-21 23:06:24 +0000 | [diff] [blame] | 621 | if (pathp != NULL) { |
| 622 | *pathp = path; |
| 623 | path = NULL; |
millert@openbsd.org | b9903ee | 2017-10-24 19:41:45 +0000 | [diff] [blame] | 624 | } |
millert@openbsd.org | 887669e | 2017-10-21 23:06:24 +0000 | [diff] [blame] | 625 | ret = 0; |
| 626 | out: |
| 627 | free(sdup); |
| 628 | free(user); |
| 629 | free(host); |
| 630 | free(path); |
| 631 | return ret; |
| 632 | } |
| 633 | |
| 634 | /* |
djm@openbsd.org | ed877ef | 2016-07-15 00:24:30 +0000 | [diff] [blame] | 635 | * Parse a [user@]host[:port] string. |
| 636 | * Caller must free returned user and host. |
| 637 | * Any of the pointer return arguments may be NULL (useful for syntax checking). |
| 638 | * If user was not specified then *userp will be set to NULL. |
| 639 | * If port was not specified then *portp will be -1. |
| 640 | * Returns 0 on success, -1 on failure. |
| 641 | */ |
| 642 | int |
| 643 | parse_user_host_port(const char *s, char **userp, char **hostp, int *portp) |
| 644 | { |
| 645 | char *sdup, *cp, *tmp; |
| 646 | char *user = NULL, *host = NULL; |
| 647 | int port = -1, ret = -1; |
| 648 | |
| 649 | if (userp != NULL) |
| 650 | *userp = NULL; |
| 651 | if (hostp != NULL) |
| 652 | *hostp = NULL; |
| 653 | if (portp != NULL) |
| 654 | *portp = -1; |
| 655 | |
| 656 | if ((sdup = tmp = strdup(s)) == NULL) |
| 657 | return -1; |
| 658 | /* Extract optional username */ |
millert@openbsd.org | 887669e | 2017-10-21 23:06:24 +0000 | [diff] [blame] | 659 | if ((cp = strrchr(tmp, '@')) != NULL) { |
djm@openbsd.org | ed877ef | 2016-07-15 00:24:30 +0000 | [diff] [blame] | 660 | *cp = '\0'; |
| 661 | if (*tmp == '\0') |
| 662 | goto out; |
| 663 | if ((user = strdup(tmp)) == NULL) |
| 664 | goto out; |
| 665 | tmp = cp + 1; |
| 666 | } |
| 667 | /* Extract mandatory hostname */ |
| 668 | if ((cp = hpdelim(&tmp)) == NULL || *cp == '\0') |
| 669 | goto out; |
| 670 | host = xstrdup(cleanhostname(cp)); |
| 671 | /* Convert and verify optional port */ |
| 672 | if (tmp != NULL && *tmp != '\0') { |
| 673 | if ((port = a2port(tmp)) <= 0) |
| 674 | goto out; |
| 675 | } |
| 676 | /* Success */ |
| 677 | if (userp != NULL) { |
| 678 | *userp = user; |
| 679 | user = NULL; |
| 680 | } |
| 681 | if (hostp != NULL) { |
| 682 | *hostp = host; |
| 683 | host = NULL; |
| 684 | } |
| 685 | if (portp != NULL) |
| 686 | *portp = port; |
| 687 | ret = 0; |
| 688 | out: |
| 689 | free(sdup); |
| 690 | free(user); |
| 691 | free(host); |
| 692 | return ret; |
| 693 | } |
| 694 | |
millert@openbsd.org | 887669e | 2017-10-21 23:06:24 +0000 | [diff] [blame] | 695 | /* |
| 696 | * Converts a two-byte hex string to decimal. |
| 697 | * Returns the decimal value or -1 for invalid input. |
| 698 | */ |
| 699 | static int |
| 700 | hexchar(const char *s) |
| 701 | { |
| 702 | unsigned char result[2]; |
| 703 | int i; |
| 704 | |
| 705 | for (i = 0; i < 2; i++) { |
| 706 | if (s[i] >= '0' && s[i] <= '9') |
| 707 | result[i] = (unsigned char)(s[i] - '0'); |
| 708 | else if (s[i] >= 'a' && s[i] <= 'f') |
| 709 | result[i] = (unsigned char)(s[i] - 'a') + 10; |
| 710 | else if (s[i] >= 'A' && s[i] <= 'F') |
| 711 | result[i] = (unsigned char)(s[i] - 'A') + 10; |
| 712 | else |
| 713 | return -1; |
| 714 | } |
| 715 | return (result[0] << 4) | result[1]; |
| 716 | } |
| 717 | |
| 718 | /* |
| 719 | * Decode an url-encoded string. |
| 720 | * Returns a newly allocated string on success or NULL on failure. |
| 721 | */ |
| 722 | static char * |
| 723 | urldecode(const char *src) |
| 724 | { |
| 725 | char *ret, *dst; |
| 726 | int ch; |
| 727 | |
| 728 | ret = xmalloc(strlen(src) + 1); |
| 729 | for (dst = ret; *src != '\0'; src++) { |
| 730 | switch (*src) { |
| 731 | case '+': |
| 732 | *dst++ = ' '; |
| 733 | break; |
| 734 | case '%': |
| 735 | if (!isxdigit((unsigned char)src[1]) || |
| 736 | !isxdigit((unsigned char)src[2]) || |
| 737 | (ch = hexchar(src + 1)) == -1) { |
| 738 | free(ret); |
| 739 | return NULL; |
| 740 | } |
| 741 | *dst++ = ch; |
| 742 | src += 2; |
| 743 | break; |
| 744 | default: |
| 745 | *dst++ = *src; |
| 746 | break; |
| 747 | } |
| 748 | } |
| 749 | *dst = '\0'; |
| 750 | |
| 751 | return ret; |
| 752 | } |
| 753 | |
| 754 | /* |
| 755 | * Parse an (scp|ssh|sftp)://[user@]host[:port][/path] URI. |
| 756 | * See https://tools.ietf.org/html/draft-ietf-secsh-scp-sftp-ssh-uri-04 |
| 757 | * Either user or path may be url-encoded (but not host or port). |
| 758 | * Caller must free returned user, host and path. |
| 759 | * Any of the pointer return arguments may be NULL (useful for syntax checking) |
| 760 | * but the scheme must always be specified. |
| 761 | * If user was not specified then *userp will be set to NULL. |
| 762 | * If port was not specified then *portp will be -1. |
| 763 | * If path was not specified then *pathp will be set to NULL. |
| 764 | * Returns 0 on success, 1 if non-uri/wrong scheme, -1 on error/invalid uri. |
| 765 | */ |
| 766 | int |
| 767 | parse_uri(const char *scheme, const char *uri, char **userp, char **hostp, |
| 768 | int *portp, char **pathp) |
| 769 | { |
| 770 | char *uridup, *cp, *tmp, ch; |
| 771 | char *user = NULL, *host = NULL, *path = NULL; |
| 772 | int port = -1, ret = -1; |
| 773 | size_t len; |
| 774 | |
| 775 | len = strlen(scheme); |
| 776 | if (strncmp(uri, scheme, len) != 0 || strncmp(uri + len, "://", 3) != 0) |
| 777 | return 1; |
| 778 | uri += len + 3; |
| 779 | |
| 780 | if (userp != NULL) |
| 781 | *userp = NULL; |
| 782 | if (hostp != NULL) |
| 783 | *hostp = NULL; |
| 784 | if (portp != NULL) |
| 785 | *portp = -1; |
| 786 | if (pathp != NULL) |
| 787 | *pathp = NULL; |
| 788 | |
| 789 | uridup = tmp = xstrdup(uri); |
| 790 | |
| 791 | /* Extract optional ssh-info (username + connection params) */ |
| 792 | if ((cp = strchr(tmp, '@')) != NULL) { |
| 793 | char *delim; |
| 794 | |
| 795 | *cp = '\0'; |
| 796 | /* Extract username and connection params */ |
| 797 | if ((delim = strchr(tmp, ';')) != NULL) { |
| 798 | /* Just ignore connection params for now */ |
| 799 | *delim = '\0'; |
| 800 | } |
| 801 | if (*tmp == '\0') { |
| 802 | /* Empty username */ |
| 803 | goto out; |
| 804 | } |
| 805 | if ((user = urldecode(tmp)) == NULL) |
| 806 | goto out; |
| 807 | tmp = cp + 1; |
| 808 | } |
| 809 | |
| 810 | /* Extract mandatory hostname */ |
| 811 | if ((cp = hpdelim2(&tmp, &ch)) == NULL || *cp == '\0') |
| 812 | goto out; |
| 813 | host = xstrdup(cleanhostname(cp)); |
| 814 | if (!valid_domain(host, 0, NULL)) |
| 815 | goto out; |
| 816 | |
| 817 | if (tmp != NULL && *tmp != '\0') { |
| 818 | if (ch == ':') { |
| 819 | /* Convert and verify port. */ |
| 820 | if ((cp = strchr(tmp, '/')) != NULL) |
| 821 | *cp = '\0'; |
| 822 | if ((port = a2port(tmp)) <= 0) |
| 823 | goto out; |
| 824 | tmp = cp ? cp + 1 : NULL; |
| 825 | } |
| 826 | if (tmp != NULL && *tmp != '\0') { |
| 827 | /* Extract optional path */ |
| 828 | if ((path = urldecode(tmp)) == NULL) |
| 829 | goto out; |
| 830 | } |
| 831 | } |
| 832 | |
| 833 | /* Success */ |
| 834 | if (userp != NULL) { |
| 835 | *userp = user; |
| 836 | user = NULL; |
| 837 | } |
| 838 | if (hostp != NULL) { |
| 839 | *hostp = host; |
| 840 | host = NULL; |
| 841 | } |
| 842 | if (portp != NULL) |
| 843 | *portp = port; |
| 844 | if (pathp != NULL) { |
| 845 | *pathp = path; |
| 846 | path = NULL; |
| 847 | } |
| 848 | ret = 0; |
| 849 | out: |
| 850 | free(uridup); |
| 851 | free(user); |
| 852 | free(host); |
| 853 | free(path); |
| 854 | return ret; |
| 855 | } |
| 856 | |
Ben Lindstrom | 4cc240d | 2001-07-04 04:46:56 +0000 | [diff] [blame] | 857 | /* function to assist building execv() arguments */ |
Ben Lindstrom | 387c472 | 2001-05-08 20:27:25 +0000 | [diff] [blame] | 858 | void |
| 859 | addargs(arglist *args, char *fmt, ...) |
| 860 | { |
| 861 | va_list ap; |
Damien Miller | 3eec6b7 | 2006-01-31 21:49:27 +1100 | [diff] [blame] | 862 | char *cp; |
Darren Tucker | c7a6fc4 | 2004-08-13 21:18:00 +1000 | [diff] [blame] | 863 | u_int nalloc; |
Damien Miller | 3eec6b7 | 2006-01-31 21:49:27 +1100 | [diff] [blame] | 864 | int r; |
Ben Lindstrom | 387c472 | 2001-05-08 20:27:25 +0000 | [diff] [blame] | 865 | |
| 866 | va_start(ap, fmt); |
Damien Miller | 3eec6b7 | 2006-01-31 21:49:27 +1100 | [diff] [blame] | 867 | r = vasprintf(&cp, fmt, ap); |
Ben Lindstrom | 387c472 | 2001-05-08 20:27:25 +0000 | [diff] [blame] | 868 | va_end(ap); |
Damien Miller | 3eec6b7 | 2006-01-31 21:49:27 +1100 | [diff] [blame] | 869 | if (r == -1) |
| 870 | fatal("addargs: argument too long"); |
Ben Lindstrom | 387c472 | 2001-05-08 20:27:25 +0000 | [diff] [blame] | 871 | |
Darren Tucker | fb16b24 | 2003-09-22 21:04:23 +1000 | [diff] [blame] | 872 | nalloc = args->nalloc; |
Ben Lindstrom | 387c472 | 2001-05-08 20:27:25 +0000 | [diff] [blame] | 873 | if (args->list == NULL) { |
Darren Tucker | fb16b24 | 2003-09-22 21:04:23 +1000 | [diff] [blame] | 874 | nalloc = 32; |
Ben Lindstrom | 387c472 | 2001-05-08 20:27:25 +0000 | [diff] [blame] | 875 | args->num = 0; |
Darren Tucker | fb16b24 | 2003-09-22 21:04:23 +1000 | [diff] [blame] | 876 | } else if (args->num+2 >= nalloc) |
| 877 | nalloc *= 2; |
Ben Lindstrom | 387c472 | 2001-05-08 20:27:25 +0000 | [diff] [blame] | 878 | |
deraadt@openbsd.org | 9e509d4 | 2017-05-31 09:15:42 +0000 | [diff] [blame] | 879 | args->list = xrecallocarray(args->list, args->nalloc, nalloc, sizeof(char *)); |
Darren Tucker | fb16b24 | 2003-09-22 21:04:23 +1000 | [diff] [blame] | 880 | args->nalloc = nalloc; |
Damien Miller | 3eec6b7 | 2006-01-31 21:49:27 +1100 | [diff] [blame] | 881 | args->list[args->num++] = cp; |
Ben Lindstrom | 387c472 | 2001-05-08 20:27:25 +0000 | [diff] [blame] | 882 | args->list[args->num] = NULL; |
| 883 | } |
Darren Tucker | 22cc741 | 2004-12-06 22:47:41 +1100 | [diff] [blame] | 884 | |
Damien Miller | 3eec6b7 | 2006-01-31 21:49:27 +1100 | [diff] [blame] | 885 | void |
| 886 | replacearg(arglist *args, u_int which, char *fmt, ...) |
| 887 | { |
| 888 | va_list ap; |
| 889 | char *cp; |
| 890 | int r; |
| 891 | |
| 892 | va_start(ap, fmt); |
| 893 | r = vasprintf(&cp, fmt, ap); |
| 894 | va_end(ap); |
| 895 | if (r == -1) |
| 896 | fatal("replacearg: argument too long"); |
| 897 | |
| 898 | if (which >= args->num) |
| 899 | fatal("replacearg: tried to replace invalid arg %d >= %d", |
| 900 | which, args->num); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 901 | free(args->list[which]); |
Damien Miller | 3eec6b7 | 2006-01-31 21:49:27 +1100 | [diff] [blame] | 902 | args->list[which] = cp; |
| 903 | } |
| 904 | |
| 905 | void |
| 906 | freeargs(arglist *args) |
| 907 | { |
| 908 | u_int i; |
| 909 | |
| 910 | if (args->list != NULL) { |
| 911 | for (i = 0; i < args->num; i++) |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 912 | free(args->list[i]); |
| 913 | free(args->list); |
Damien Miller | 3eec6b7 | 2006-01-31 21:49:27 +1100 | [diff] [blame] | 914 | args->nalloc = args->num = 0; |
| 915 | args->list = NULL; |
| 916 | } |
| 917 | } |
| 918 | |
Darren Tucker | 22cc741 | 2004-12-06 22:47:41 +1100 | [diff] [blame] | 919 | /* |
Damien Miller | 5fd38c0 | 2005-05-26 12:02:14 +1000 | [diff] [blame] | 920 | * Expands tildes in the file name. Returns data allocated by xmalloc. |
| 921 | * Warning: this calls getpw*. |
| 922 | */ |
| 923 | char * |
| 924 | tilde_expand_filename(const char *filename, uid_t uid) |
| 925 | { |
Darren Tucker | 2ca51bf | 2013-05-16 20:22:46 +1000 | [diff] [blame] | 926 | const char *path, *sep; |
| 927 | char user[128], *ret; |
Damien Miller | 5fd38c0 | 2005-05-26 12:02:14 +1000 | [diff] [blame] | 928 | struct passwd *pw; |
Damien Miller | eccb9de | 2005-06-17 12:59:34 +1000 | [diff] [blame] | 929 | u_int len, slash; |
Damien Miller | 5fd38c0 | 2005-05-26 12:02:14 +1000 | [diff] [blame] | 930 | |
| 931 | if (*filename != '~') |
| 932 | return (xstrdup(filename)); |
| 933 | filename++; |
| 934 | |
| 935 | path = strchr(filename, '/'); |
| 936 | if (path != NULL && path > filename) { /* ~user/path */ |
Damien Miller | eccb9de | 2005-06-17 12:59:34 +1000 | [diff] [blame] | 937 | slash = path - filename; |
| 938 | if (slash > sizeof(user) - 1) |
Damien Miller | 5fd38c0 | 2005-05-26 12:02:14 +1000 | [diff] [blame] | 939 | fatal("tilde_expand_filename: ~username too long"); |
Damien Miller | eccb9de | 2005-06-17 12:59:34 +1000 | [diff] [blame] | 940 | memcpy(user, filename, slash); |
| 941 | user[slash] = '\0'; |
Damien Miller | 5fd38c0 | 2005-05-26 12:02:14 +1000 | [diff] [blame] | 942 | if ((pw = getpwnam(user)) == NULL) |
| 943 | fatal("tilde_expand_filename: No such user %s", user); |
| 944 | } else if ((pw = getpwuid(uid)) == NULL) /* ~/path */ |
Darren Tucker | 7517b5b | 2008-06-13 14:48:59 +1000 | [diff] [blame] | 945 | fatal("tilde_expand_filename: No such uid %ld", (long)uid); |
Damien Miller | 5fd38c0 | 2005-05-26 12:02:14 +1000 | [diff] [blame] | 946 | |
Damien Miller | 5fd38c0 | 2005-05-26 12:02:14 +1000 | [diff] [blame] | 947 | /* Make sure directory has a trailing '/' */ |
| 948 | len = strlen(pw->pw_dir); |
Darren Tucker | 026d9db | 2013-05-16 20:23:52 +1000 | [diff] [blame] | 949 | if (len == 0 || pw->pw_dir[len - 1] != '/') |
Darren Tucker | 2ca51bf | 2013-05-16 20:22:46 +1000 | [diff] [blame] | 950 | sep = "/"; |
| 951 | else |
| 952 | sep = ""; |
Damien Miller | 5fd38c0 | 2005-05-26 12:02:14 +1000 | [diff] [blame] | 953 | |
| 954 | /* Skip leading '/' from specified path */ |
| 955 | if (path != NULL) |
| 956 | filename = path + 1; |
Darren Tucker | 2ca51bf | 2013-05-16 20:22:46 +1000 | [diff] [blame] | 957 | |
deraadt@openbsd.org | 2ae4f33 | 2015-01-16 06:40:12 +0000 | [diff] [blame] | 958 | if (xasprintf(&ret, "%s%s%s", pw->pw_dir, sep, filename) >= PATH_MAX) |
Damien Miller | 5fd38c0 | 2005-05-26 12:02:14 +1000 | [diff] [blame] | 959 | fatal("tilde_expand_filename: Path too long"); |
| 960 | |
Darren Tucker | 2ca51bf | 2013-05-16 20:22:46 +1000 | [diff] [blame] | 961 | return (ret); |
Damien Miller | 5fd38c0 | 2005-05-26 12:02:14 +1000 | [diff] [blame] | 962 | } |
| 963 | |
| 964 | /* |
Damien Miller | 6476cad | 2005-06-16 13:18:34 +1000 | [diff] [blame] | 965 | * Expand a string with a set of %[char] escapes. A number of escapes may be |
| 966 | * specified as (char *escape_chars, char *replacement) pairs. The list must |
Darren Tucker | bee73d5 | 2005-07-14 17:05:02 +1000 | [diff] [blame] | 967 | * be terminated by a NULL escape_char. Returns replaced string in memory |
Damien Miller | 6476cad | 2005-06-16 13:18:34 +1000 | [diff] [blame] | 968 | * allocated by xmalloc. |
| 969 | */ |
| 970 | char * |
| 971 | percent_expand(const char *string, ...) |
| 972 | { |
| 973 | #define EXPAND_MAX_KEYS 16 |
Darren Tucker | 70d8769 | 2010-01-08 18:49:16 +1100 | [diff] [blame] | 974 | u_int num_keys, i, j; |
Damien Miller | 6476cad | 2005-06-16 13:18:34 +1000 | [diff] [blame] | 975 | struct { |
| 976 | const char *key; |
| 977 | const char *repl; |
| 978 | } keys[EXPAND_MAX_KEYS]; |
Damien Miller | 6476cad | 2005-06-16 13:18:34 +1000 | [diff] [blame] | 979 | char buf[4096]; |
| 980 | va_list ap; |
| 981 | |
| 982 | /* Gather keys */ |
| 983 | va_start(ap, string); |
| 984 | for (num_keys = 0; num_keys < EXPAND_MAX_KEYS; num_keys++) { |
| 985 | keys[num_keys].key = va_arg(ap, char *); |
| 986 | if (keys[num_keys].key == NULL) |
| 987 | break; |
| 988 | keys[num_keys].repl = va_arg(ap, char *); |
| 989 | if (keys[num_keys].repl == NULL) |
Darren Tucker | 70d8769 | 2010-01-08 18:49:16 +1100 | [diff] [blame] | 990 | fatal("%s: NULL replacement", __func__); |
Damien Miller | 6476cad | 2005-06-16 13:18:34 +1000 | [diff] [blame] | 991 | } |
Darren Tucker | 70d8769 | 2010-01-08 18:49:16 +1100 | [diff] [blame] | 992 | if (num_keys == EXPAND_MAX_KEYS && va_arg(ap, char *) != NULL) |
| 993 | fatal("%s: too many keys", __func__); |
Damien Miller | 6476cad | 2005-06-16 13:18:34 +1000 | [diff] [blame] | 994 | va_end(ap); |
| 995 | |
Damien Miller | 6476cad | 2005-06-16 13:18:34 +1000 | [diff] [blame] | 996 | /* Expand string */ |
| 997 | *buf = '\0'; |
| 998 | for (i = 0; *string != '\0'; string++) { |
| 999 | if (*string != '%') { |
| 1000 | append: |
| 1001 | buf[i++] = *string; |
| 1002 | if (i >= sizeof(buf)) |
Darren Tucker | 70d8769 | 2010-01-08 18:49:16 +1100 | [diff] [blame] | 1003 | fatal("%s: string too long", __func__); |
Damien Miller | 6476cad | 2005-06-16 13:18:34 +1000 | [diff] [blame] | 1004 | buf[i] = '\0'; |
| 1005 | continue; |
| 1006 | } |
| 1007 | string++; |
Darren Tucker | 70d8769 | 2010-01-08 18:49:16 +1100 | [diff] [blame] | 1008 | /* %% case */ |
Damien Miller | 6476cad | 2005-06-16 13:18:34 +1000 | [diff] [blame] | 1009 | if (*string == '%') |
| 1010 | goto append; |
tobias@openbsd.org | 8b56e59 | 2015-12-04 21:51:06 +0000 | [diff] [blame] | 1011 | if (*string == '\0') |
| 1012 | fatal("%s: invalid format", __func__); |
Damien Miller | 6476cad | 2005-06-16 13:18:34 +1000 | [diff] [blame] | 1013 | for (j = 0; j < num_keys; j++) { |
| 1014 | if (strchr(keys[j].key, *string) != NULL) { |
| 1015 | i = strlcat(buf, keys[j].repl, sizeof(buf)); |
| 1016 | if (i >= sizeof(buf)) |
Darren Tucker | 70d8769 | 2010-01-08 18:49:16 +1100 | [diff] [blame] | 1017 | fatal("%s: string too long", __func__); |
Damien Miller | 6476cad | 2005-06-16 13:18:34 +1000 | [diff] [blame] | 1018 | break; |
| 1019 | } |
| 1020 | } |
| 1021 | if (j >= num_keys) |
Darren Tucker | 70d8769 | 2010-01-08 18:49:16 +1100 | [diff] [blame] | 1022 | fatal("%s: unknown key %%%c", __func__, *string); |
Damien Miller | 6476cad | 2005-06-16 13:18:34 +1000 | [diff] [blame] | 1023 | } |
| 1024 | return (xstrdup(buf)); |
| 1025 | #undef EXPAND_MAX_KEYS |
| 1026 | } |
| 1027 | |
Damien Miller | d27b947 | 2005-12-13 19:29:02 +1100 | [diff] [blame] | 1028 | int |
djm@openbsd.org | b7548b1 | 2017-10-23 05:08:00 +0000 | [diff] [blame] | 1029 | tun_open(int tun, int mode, char **ifname) |
Damien Miller | d27b947 | 2005-12-13 19:29:02 +1100 | [diff] [blame] | 1030 | { |
Damien Miller | 62a31c9 | 2005-12-13 20:44:13 +1100 | [diff] [blame] | 1031 | #if defined(CUSTOM_SYS_TUN_OPEN) |
djm@openbsd.org | b7548b1 | 2017-10-23 05:08:00 +0000 | [diff] [blame] | 1032 | return (sys_tun_open(tun, mode, ifname)); |
Damien Miller | 2dcddbf | 2006-01-01 19:47:05 +1100 | [diff] [blame] | 1033 | #elif defined(SSH_TUN_OPENBSD) |
Damien Miller | 7b58e80 | 2005-12-13 19:33:19 +1100 | [diff] [blame] | 1034 | struct ifreq ifr; |
Damien Miller | d27b947 | 2005-12-13 19:29:02 +1100 | [diff] [blame] | 1035 | char name[100]; |
Damien Miller | 7b58e80 | 2005-12-13 19:33:19 +1100 | [diff] [blame] | 1036 | int fd = -1, sock; |
sthen@openbsd.org | a820a86 | 2015-10-24 08:34:09 +0000 | [diff] [blame] | 1037 | const char *tunbase = "tun"; |
| 1038 | |
djm@openbsd.org | b7548b1 | 2017-10-23 05:08:00 +0000 | [diff] [blame] | 1039 | if (ifname != NULL) |
| 1040 | *ifname = NULL; |
| 1041 | |
sthen@openbsd.org | a820a86 | 2015-10-24 08:34:09 +0000 | [diff] [blame] | 1042 | if (mode == SSH_TUNMODE_ETHERNET) |
| 1043 | tunbase = "tap"; |
Damien Miller | d27b947 | 2005-12-13 19:29:02 +1100 | [diff] [blame] | 1044 | |
Damien Miller | 7b58e80 | 2005-12-13 19:33:19 +1100 | [diff] [blame] | 1045 | /* Open the tunnel device */ |
| 1046 | if (tun <= SSH_TUNID_MAX) { |
sthen@openbsd.org | a820a86 | 2015-10-24 08:34:09 +0000 | [diff] [blame] | 1047 | snprintf(name, sizeof(name), "/dev/%s%d", tunbase, tun); |
Damien Miller | 7b58e80 | 2005-12-13 19:33:19 +1100 | [diff] [blame] | 1048 | fd = open(name, O_RDWR); |
| 1049 | } else if (tun == SSH_TUNID_ANY) { |
| 1050 | for (tun = 100; tun >= 0; tun--) { |
sthen@openbsd.org | a820a86 | 2015-10-24 08:34:09 +0000 | [diff] [blame] | 1051 | snprintf(name, sizeof(name), "/dev/%s%d", |
| 1052 | tunbase, tun); |
Damien Miller | 7b58e80 | 2005-12-13 19:33:19 +1100 | [diff] [blame] | 1053 | if ((fd = open(name, O_RDWR)) >= 0) |
| 1054 | break; |
Damien Miller | d27b947 | 2005-12-13 19:29:02 +1100 | [diff] [blame] | 1055 | } |
| 1056 | } else { |
Damien Miller | a210d52 | 2006-01-02 23:40:30 +1100 | [diff] [blame] | 1057 | debug("%s: invalid tunnel %u", __func__, tun); |
djm@openbsd.org | 2905d6f | 2015-10-07 00:54:06 +0000 | [diff] [blame] | 1058 | return -1; |
Damien Miller | d27b947 | 2005-12-13 19:29:02 +1100 | [diff] [blame] | 1059 | } |
Damien Miller | 7b58e80 | 2005-12-13 19:33:19 +1100 | [diff] [blame] | 1060 | |
| 1061 | if (fd < 0) { |
djm@openbsd.org | 2905d6f | 2015-10-07 00:54:06 +0000 | [diff] [blame] | 1062 | debug("%s: %s open: %s", __func__, name, strerror(errno)); |
| 1063 | return -1; |
Damien Miller | 7b58e80 | 2005-12-13 19:33:19 +1100 | [diff] [blame] | 1064 | } |
| 1065 | |
| 1066 | debug("%s: %s mode %d fd %d", __func__, name, mode, fd); |
| 1067 | |
sthen@openbsd.org | a820a86 | 2015-10-24 08:34:09 +0000 | [diff] [blame] | 1068 | /* Bring interface up if it is not already */ |
| 1069 | snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "%s%d", tunbase, tun); |
| 1070 | if ((sock = socket(PF_UNIX, SOCK_STREAM, 0)) == -1) |
Damien Miller | 7b58e80 | 2005-12-13 19:33:19 +1100 | [diff] [blame] | 1071 | goto failed; |
| 1072 | |
djm@openbsd.org | 2905d6f | 2015-10-07 00:54:06 +0000 | [diff] [blame] | 1073 | if (ioctl(sock, SIOCGIFFLAGS, &ifr) == -1) { |
| 1074 | debug("%s: get interface %s flags: %s", __func__, |
| 1075 | ifr.ifr_name, strerror(errno)); |
Damien Miller | 7b58e80 | 2005-12-13 19:33:19 +1100 | [diff] [blame] | 1076 | goto failed; |
djm@openbsd.org | 2905d6f | 2015-10-07 00:54:06 +0000 | [diff] [blame] | 1077 | } |
Damien Miller | a1d9a18 | 2006-01-02 23:41:21 +1100 | [diff] [blame] | 1078 | |
djm@openbsd.org | 2905d6f | 2015-10-07 00:54:06 +0000 | [diff] [blame] | 1079 | if (!(ifr.ifr_flags & IFF_UP)) { |
| 1080 | ifr.ifr_flags |= IFF_UP; |
| 1081 | if (ioctl(sock, SIOCSIFFLAGS, &ifr) == -1) { |
| 1082 | debug("%s: activate interface %s: %s", __func__, |
| 1083 | ifr.ifr_name, strerror(errno)); |
| 1084 | goto failed; |
| 1085 | } |
| 1086 | } |
Damien Miller | 7b58e80 | 2005-12-13 19:33:19 +1100 | [diff] [blame] | 1087 | |
djm@openbsd.org | b7548b1 | 2017-10-23 05:08:00 +0000 | [diff] [blame] | 1088 | if (ifname != NULL) |
| 1089 | *ifname = xstrdup(ifr.ifr_name); |
| 1090 | |
Damien Miller | 7b58e80 | 2005-12-13 19:33:19 +1100 | [diff] [blame] | 1091 | close(sock); |
djm@openbsd.org | 2905d6f | 2015-10-07 00:54:06 +0000 | [diff] [blame] | 1092 | return fd; |
Damien Miller | 7b58e80 | 2005-12-13 19:33:19 +1100 | [diff] [blame] | 1093 | |
| 1094 | failed: |
| 1095 | if (fd >= 0) |
| 1096 | close(fd); |
| 1097 | if (sock >= 0) |
| 1098 | close(sock); |
djm@openbsd.org | 2905d6f | 2015-10-07 00:54:06 +0000 | [diff] [blame] | 1099 | return -1; |
Damien Miller | 62a31c9 | 2005-12-13 20:44:13 +1100 | [diff] [blame] | 1100 | #else |
| 1101 | error("Tunnel interfaces are not supported on this platform"); |
| 1102 | return (-1); |
| 1103 | #endif |
Damien Miller | d27b947 | 2005-12-13 19:29:02 +1100 | [diff] [blame] | 1104 | } |
| 1105 | |
Darren Tucker | ce321d8 | 2005-10-03 18:11:24 +1000 | [diff] [blame] | 1106 | void |
| 1107 | sanitise_stdfd(void) |
| 1108 | { |
Damien Miller | 72c5b7d | 2006-01-06 14:50:44 +1100 | [diff] [blame] | 1109 | int nullfd, dupfd; |
Darren Tucker | ce321d8 | 2005-10-03 18:11:24 +1000 | [diff] [blame] | 1110 | |
Damien Miller | 72c5b7d | 2006-01-06 14:50:44 +1100 | [diff] [blame] | 1111 | if ((nullfd = dupfd = open(_PATH_DEVNULL, O_RDWR)) == -1) { |
Damien Miller | 9eab956 | 2009-02-22 08:47:02 +1100 | [diff] [blame] | 1112 | fprintf(stderr, "Couldn't open /dev/null: %s\n", |
| 1113 | strerror(errno)); |
Darren Tucker | ce321d8 | 2005-10-03 18:11:24 +1000 | [diff] [blame] | 1114 | exit(1); |
| 1115 | } |
krw@openbsd.org | 574def0 | 2016-04-02 14:37:42 +0000 | [diff] [blame] | 1116 | while (++dupfd <= STDERR_FILENO) { |
| 1117 | /* Only populate closed fds. */ |
| 1118 | if (fcntl(dupfd, F_GETFL) == -1 && errno == EBADF) { |
| 1119 | if (dup2(nullfd, dupfd) == -1) { |
| 1120 | fprintf(stderr, "dup2: %s\n", strerror(errno)); |
| 1121 | exit(1); |
| 1122 | } |
Darren Tucker | ce321d8 | 2005-10-03 18:11:24 +1000 | [diff] [blame] | 1123 | } |
Darren Tucker | ce321d8 | 2005-10-03 18:11:24 +1000 | [diff] [blame] | 1124 | } |
krw@openbsd.org | 574def0 | 2016-04-02 14:37:42 +0000 | [diff] [blame] | 1125 | if (nullfd > STDERR_FILENO) |
Darren Tucker | ce321d8 | 2005-10-03 18:11:24 +1000 | [diff] [blame] | 1126 | close(nullfd); |
| 1127 | } |
| 1128 | |
Damien Miller | 1339002 | 2005-07-06 09:44:19 +1000 | [diff] [blame] | 1129 | char * |
Damien Miller | 3f94188 | 2006-03-31 23:13:02 +1100 | [diff] [blame] | 1130 | tohex(const void *vp, size_t l) |
Damien Miller | 1339002 | 2005-07-06 09:44:19 +1000 | [diff] [blame] | 1131 | { |
Damien Miller | 3f94188 | 2006-03-31 23:13:02 +1100 | [diff] [blame] | 1132 | const u_char *p = (const u_char *)vp; |
Damien Miller | 1339002 | 2005-07-06 09:44:19 +1000 | [diff] [blame] | 1133 | char b[3], *r; |
Damien Miller | 3f94188 | 2006-03-31 23:13:02 +1100 | [diff] [blame] | 1134 | size_t i, hl; |
| 1135 | |
| 1136 | if (l > 65536) |
| 1137 | return xstrdup("tohex: length > 65536"); |
Damien Miller | 1339002 | 2005-07-06 09:44:19 +1000 | [diff] [blame] | 1138 | |
| 1139 | hl = l * 2 + 1; |
Damien Miller | 07d86be | 2006-03-26 14:19:21 +1100 | [diff] [blame] | 1140 | r = xcalloc(1, hl); |
Damien Miller | 1339002 | 2005-07-06 09:44:19 +1000 | [diff] [blame] | 1141 | for (i = 0; i < l; i++) { |
Damien Miller | 3f94188 | 2006-03-31 23:13:02 +1100 | [diff] [blame] | 1142 | snprintf(b, sizeof(b), "%02x", p[i]); |
Damien Miller | 1339002 | 2005-07-06 09:44:19 +1000 | [diff] [blame] | 1143 | strlcat(r, b, hl); |
| 1144 | } |
| 1145 | return (r); |
| 1146 | } |
| 1147 | |
Damien Miller | 3f94188 | 2006-03-31 23:13:02 +1100 | [diff] [blame] | 1148 | u_int64_t |
| 1149 | get_u64(const void *vp) |
| 1150 | { |
| 1151 | const u_char *p = (const u_char *)vp; |
| 1152 | u_int64_t v; |
| 1153 | |
| 1154 | v = (u_int64_t)p[0] << 56; |
| 1155 | v |= (u_int64_t)p[1] << 48; |
| 1156 | v |= (u_int64_t)p[2] << 40; |
| 1157 | v |= (u_int64_t)p[3] << 32; |
| 1158 | v |= (u_int64_t)p[4] << 24; |
| 1159 | v |= (u_int64_t)p[5] << 16; |
| 1160 | v |= (u_int64_t)p[6] << 8; |
| 1161 | v |= (u_int64_t)p[7]; |
| 1162 | |
| 1163 | return (v); |
| 1164 | } |
| 1165 | |
| 1166 | u_int32_t |
| 1167 | get_u32(const void *vp) |
| 1168 | { |
| 1169 | const u_char *p = (const u_char *)vp; |
| 1170 | u_int32_t v; |
| 1171 | |
| 1172 | v = (u_int32_t)p[0] << 24; |
| 1173 | v |= (u_int32_t)p[1] << 16; |
| 1174 | v |= (u_int32_t)p[2] << 8; |
| 1175 | v |= (u_int32_t)p[3]; |
| 1176 | |
| 1177 | return (v); |
| 1178 | } |
| 1179 | |
Damien Miller | 8885669 | 2014-04-20 13:33:19 +1000 | [diff] [blame] | 1180 | u_int32_t |
| 1181 | get_u32_le(const void *vp) |
| 1182 | { |
| 1183 | const u_char *p = (const u_char *)vp; |
| 1184 | u_int32_t v; |
| 1185 | |
| 1186 | v = (u_int32_t)p[0]; |
| 1187 | v |= (u_int32_t)p[1] << 8; |
| 1188 | v |= (u_int32_t)p[2] << 16; |
| 1189 | v |= (u_int32_t)p[3] << 24; |
| 1190 | |
| 1191 | return (v); |
| 1192 | } |
| 1193 | |
Damien Miller | 3f94188 | 2006-03-31 23:13:02 +1100 | [diff] [blame] | 1194 | u_int16_t |
| 1195 | get_u16(const void *vp) |
| 1196 | { |
| 1197 | const u_char *p = (const u_char *)vp; |
| 1198 | u_int16_t v; |
| 1199 | |
| 1200 | v = (u_int16_t)p[0] << 8; |
| 1201 | v |= (u_int16_t)p[1]; |
| 1202 | |
| 1203 | return (v); |
| 1204 | } |
| 1205 | |
| 1206 | void |
| 1207 | put_u64(void *vp, u_int64_t v) |
| 1208 | { |
| 1209 | u_char *p = (u_char *)vp; |
| 1210 | |
| 1211 | p[0] = (u_char)(v >> 56) & 0xff; |
| 1212 | p[1] = (u_char)(v >> 48) & 0xff; |
| 1213 | p[2] = (u_char)(v >> 40) & 0xff; |
| 1214 | p[3] = (u_char)(v >> 32) & 0xff; |
| 1215 | p[4] = (u_char)(v >> 24) & 0xff; |
| 1216 | p[5] = (u_char)(v >> 16) & 0xff; |
| 1217 | p[6] = (u_char)(v >> 8) & 0xff; |
| 1218 | p[7] = (u_char)v & 0xff; |
| 1219 | } |
| 1220 | |
| 1221 | void |
| 1222 | put_u32(void *vp, u_int32_t v) |
| 1223 | { |
| 1224 | u_char *p = (u_char *)vp; |
| 1225 | |
| 1226 | p[0] = (u_char)(v >> 24) & 0xff; |
| 1227 | p[1] = (u_char)(v >> 16) & 0xff; |
| 1228 | p[2] = (u_char)(v >> 8) & 0xff; |
| 1229 | p[3] = (u_char)v & 0xff; |
| 1230 | } |
| 1231 | |
Damien Miller | 8885669 | 2014-04-20 13:33:19 +1000 | [diff] [blame] | 1232 | void |
| 1233 | put_u32_le(void *vp, u_int32_t v) |
| 1234 | { |
| 1235 | u_char *p = (u_char *)vp; |
| 1236 | |
| 1237 | p[0] = (u_char)v & 0xff; |
| 1238 | p[1] = (u_char)(v >> 8) & 0xff; |
| 1239 | p[2] = (u_char)(v >> 16) & 0xff; |
| 1240 | p[3] = (u_char)(v >> 24) & 0xff; |
| 1241 | } |
Damien Miller | 3f94188 | 2006-03-31 23:13:02 +1100 | [diff] [blame] | 1242 | |
| 1243 | void |
| 1244 | put_u16(void *vp, u_int16_t v) |
| 1245 | { |
| 1246 | u_char *p = (u_char *)vp; |
| 1247 | |
| 1248 | p[0] = (u_char)(v >> 8) & 0xff; |
| 1249 | p[1] = (u_char)v & 0xff; |
| 1250 | } |
Darren Tucker | 3fc464e | 2008-06-13 06:42:45 +1000 | [diff] [blame] | 1251 | |
| 1252 | void |
| 1253 | ms_subtract_diff(struct timeval *start, int *ms) |
| 1254 | { |
| 1255 | struct timeval diff, finish; |
| 1256 | |
dtucker@openbsd.org@openbsd.org | 5db6fbf | 2017-11-25 06:46:22 +0000 | [diff] [blame] | 1257 | monotime_tv(&finish); |
| 1258 | timersub(&finish, start, &diff); |
Darren Tucker | 3fc464e | 2008-06-13 06:42:45 +1000 | [diff] [blame] | 1259 | *ms -= (diff.tv_sec * 1000) + (diff.tv_usec / 1000); |
| 1260 | } |
| 1261 | |
| 1262 | void |
| 1263 | ms_to_timeval(struct timeval *tv, int ms) |
| 1264 | { |
| 1265 | if (ms < 0) |
| 1266 | ms = 0; |
| 1267 | tv->tv_sec = ms / 1000; |
| 1268 | tv->tv_usec = (ms % 1000) * 1000; |
| 1269 | } |
| 1270 | |
dtucker@openbsd.org@openbsd.org | 5db6fbf | 2017-11-25 06:46:22 +0000 | [diff] [blame] | 1271 | void |
| 1272 | monotime_ts(struct timespec *ts) |
Darren Tucker | b759c9c | 2013-06-02 07:46:16 +1000 | [diff] [blame] | 1273 | { |
dtucker@openbsd.org@openbsd.org | 5db6fbf | 2017-11-25 06:46:22 +0000 | [diff] [blame] | 1274 | struct timeval tv; |
| 1275 | #if defined(HAVE_CLOCK_GETTIME) && (defined(CLOCK_BOOTTIME) || \ |
| 1276 | defined(CLOCK_MONOTONIC) || defined(CLOCK_REALTIME)) |
Darren Tucker | 94396b7 | 2013-08-08 11:52:37 +1000 | [diff] [blame] | 1277 | static int gettime_failed = 0; |
Darren Tucker | b759c9c | 2013-06-02 07:46:16 +1000 | [diff] [blame] | 1278 | |
Darren Tucker | 94396b7 | 2013-08-08 11:52:37 +1000 | [diff] [blame] | 1279 | if (!gettime_failed) { |
dtucker@openbsd.org@openbsd.org | 5db6fbf | 2017-11-25 06:46:22 +0000 | [diff] [blame] | 1280 | # ifdef CLOCK_BOOTTIME |
| 1281 | if (clock_gettime(CLOCK_BOOTTIME, ts) == 0) |
| 1282 | return; |
| 1283 | # endif /* CLOCK_BOOTTIME */ |
| 1284 | # ifdef CLOCK_MONOTONIC |
| 1285 | if (clock_gettime(CLOCK_MONOTONIC, ts) == 0) |
| 1286 | return; |
| 1287 | # endif /* CLOCK_MONOTONIC */ |
| 1288 | # ifdef CLOCK_REALTIME |
| 1289 | /* Not monotonic, but we're almost out of options here. */ |
| 1290 | if (clock_gettime(CLOCK_REALTIME, ts) == 0) |
| 1291 | return; |
| 1292 | # endif /* CLOCK_REALTIME */ |
Darren Tucker | 94396b7 | 2013-08-08 11:52:37 +1000 | [diff] [blame] | 1293 | debug3("clock_gettime: %s", strerror(errno)); |
| 1294 | gettime_failed = 1; |
| 1295 | } |
dtucker@openbsd.org@openbsd.org | 5db6fbf | 2017-11-25 06:46:22 +0000 | [diff] [blame] | 1296 | #endif /* HAVE_CLOCK_GETTIME && (BOOTTIME || MONOTONIC || REALTIME) */ |
| 1297 | gettimeofday(&tv, NULL); |
| 1298 | ts->tv_sec = tv.tv_sec; |
| 1299 | ts->tv_nsec = (long)tv.tv_usec * 1000; |
| 1300 | } |
Darren Tucker | 94396b7 | 2013-08-08 11:52:37 +1000 | [diff] [blame] | 1301 | |
dtucker@openbsd.org@openbsd.org | 5db6fbf | 2017-11-25 06:46:22 +0000 | [diff] [blame] | 1302 | void |
| 1303 | monotime_tv(struct timeval *tv) |
| 1304 | { |
| 1305 | struct timespec ts; |
| 1306 | |
| 1307 | monotime_ts(&ts); |
| 1308 | tv->tv_sec = ts.tv_sec; |
| 1309 | tv->tv_usec = ts.tv_nsec / 1000; |
| 1310 | } |
| 1311 | |
| 1312 | time_t |
| 1313 | monotime(void) |
| 1314 | { |
| 1315 | struct timespec ts; |
| 1316 | |
| 1317 | monotime_ts(&ts); |
| 1318 | return ts.tv_sec; |
Darren Tucker | b759c9c | 2013-06-02 07:46:16 +1000 | [diff] [blame] | 1319 | } |
| 1320 | |
dtucker@openbsd.org | b8d4eaf | 2016-03-02 22:42:40 +0000 | [diff] [blame] | 1321 | double |
| 1322 | monotime_double(void) |
| 1323 | { |
| 1324 | struct timespec ts; |
| 1325 | |
dtucker@openbsd.org@openbsd.org | 5db6fbf | 2017-11-25 06:46:22 +0000 | [diff] [blame] | 1326 | monotime_ts(&ts); |
| 1327 | return ts.tv_sec + ((double)ts.tv_nsec / 1000000000); |
dtucker@openbsd.org | b8d4eaf | 2016-03-02 22:42:40 +0000 | [diff] [blame] | 1328 | } |
| 1329 | |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 1330 | void |
| 1331 | bandwidth_limit_init(struct bwlimit *bw, u_int64_t kbps, size_t buflen) |
| 1332 | { |
| 1333 | bw->buflen = buflen; |
| 1334 | bw->rate = kbps; |
| 1335 | bw->thresh = bw->rate; |
| 1336 | bw->lamt = 0; |
| 1337 | timerclear(&bw->bwstart); |
| 1338 | timerclear(&bw->bwend); |
| 1339 | } |
| 1340 | |
| 1341 | /* Callback from read/write loop to insert bandwidth-limiting delays */ |
| 1342 | void |
| 1343 | bandwidth_limit(struct bwlimit *bw, size_t read_len) |
| 1344 | { |
| 1345 | u_int64_t waitlen; |
| 1346 | struct timespec ts, rm; |
| 1347 | |
| 1348 | if (!timerisset(&bw->bwstart)) { |
dtucker@openbsd.org@openbsd.org | 5db6fbf | 2017-11-25 06:46:22 +0000 | [diff] [blame] | 1349 | monotime_tv(&bw->bwstart); |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 1350 | return; |
| 1351 | } |
| 1352 | |
| 1353 | bw->lamt += read_len; |
| 1354 | if (bw->lamt < bw->thresh) |
| 1355 | return; |
| 1356 | |
dtucker@openbsd.org@openbsd.org | 5db6fbf | 2017-11-25 06:46:22 +0000 | [diff] [blame] | 1357 | monotime_tv(&bw->bwend); |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 1358 | timersub(&bw->bwend, &bw->bwstart, &bw->bwend); |
| 1359 | if (!timerisset(&bw->bwend)) |
| 1360 | return; |
| 1361 | |
| 1362 | bw->lamt *= 8; |
| 1363 | waitlen = (double)1000000L * bw->lamt / bw->rate; |
| 1364 | |
| 1365 | bw->bwstart.tv_sec = waitlen / 1000000L; |
| 1366 | bw->bwstart.tv_usec = waitlen % 1000000L; |
| 1367 | |
| 1368 | if (timercmp(&bw->bwstart, &bw->bwend, >)) { |
| 1369 | timersub(&bw->bwstart, &bw->bwend, &bw->bwend); |
| 1370 | |
| 1371 | /* Adjust the wait time */ |
| 1372 | if (bw->bwend.tv_sec) { |
| 1373 | bw->thresh /= 2; |
| 1374 | if (bw->thresh < bw->buflen / 4) |
| 1375 | bw->thresh = bw->buflen / 4; |
| 1376 | } else if (bw->bwend.tv_usec < 10000) { |
| 1377 | bw->thresh *= 2; |
| 1378 | if (bw->thresh > bw->buflen * 8) |
| 1379 | bw->thresh = bw->buflen * 8; |
| 1380 | } |
| 1381 | |
| 1382 | TIMEVAL_TO_TIMESPEC(&bw->bwend, &ts); |
| 1383 | while (nanosleep(&ts, &rm) == -1) { |
| 1384 | if (errno != EINTR) |
| 1385 | break; |
| 1386 | ts = rm; |
| 1387 | } |
| 1388 | } |
| 1389 | |
| 1390 | bw->lamt = 0; |
dtucker@openbsd.org@openbsd.org | 5db6fbf | 2017-11-25 06:46:22 +0000 | [diff] [blame] | 1391 | monotime_tv(&bw->bwstart); |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 1392 | } |
Damien Miller | 0dac6fb | 2010-11-20 15:19:38 +1100 | [diff] [blame] | 1393 | |
Damien Miller | 2cd6293 | 2010-12-01 11:50:35 +1100 | [diff] [blame] | 1394 | /* Make a template filename for mk[sd]temp() */ |
| 1395 | void |
| 1396 | mktemp_proto(char *s, size_t len) |
| 1397 | { |
| 1398 | const char *tmpdir; |
| 1399 | int r; |
| 1400 | |
| 1401 | if ((tmpdir = getenv("TMPDIR")) != NULL) { |
| 1402 | r = snprintf(s, len, "%s/ssh-XXXXXXXXXXXX", tmpdir); |
| 1403 | if (r > 0 && (size_t)r < len) |
| 1404 | return; |
| 1405 | } |
| 1406 | r = snprintf(s, len, "/tmp/ssh-XXXXXXXXXXXX"); |
| 1407 | if (r < 0 || (size_t)r >= len) |
| 1408 | fatal("%s: template string too short", __func__); |
| 1409 | } |
| 1410 | |
Damien Miller | 0dac6fb | 2010-11-20 15:19:38 +1100 | [diff] [blame] | 1411 | static const struct { |
| 1412 | const char *name; |
| 1413 | int value; |
| 1414 | } ipqos[] = { |
djm@openbsd.org | 51676ec | 2017-07-23 23:37:02 +0000 | [diff] [blame] | 1415 | { "none", INT_MAX }, /* can't use 0 here; that's CS0 */ |
Damien Miller | 0dac6fb | 2010-11-20 15:19:38 +1100 | [diff] [blame] | 1416 | { "af11", IPTOS_DSCP_AF11 }, |
| 1417 | { "af12", IPTOS_DSCP_AF12 }, |
| 1418 | { "af13", IPTOS_DSCP_AF13 }, |
Damien Miller | 2918e03 | 2011-09-22 21:34:35 +1000 | [diff] [blame] | 1419 | { "af21", IPTOS_DSCP_AF21 }, |
Damien Miller | 0dac6fb | 2010-11-20 15:19:38 +1100 | [diff] [blame] | 1420 | { "af22", IPTOS_DSCP_AF22 }, |
| 1421 | { "af23", IPTOS_DSCP_AF23 }, |
| 1422 | { "af31", IPTOS_DSCP_AF31 }, |
| 1423 | { "af32", IPTOS_DSCP_AF32 }, |
| 1424 | { "af33", IPTOS_DSCP_AF33 }, |
| 1425 | { "af41", IPTOS_DSCP_AF41 }, |
| 1426 | { "af42", IPTOS_DSCP_AF42 }, |
| 1427 | { "af43", IPTOS_DSCP_AF43 }, |
| 1428 | { "cs0", IPTOS_DSCP_CS0 }, |
| 1429 | { "cs1", IPTOS_DSCP_CS1 }, |
| 1430 | { "cs2", IPTOS_DSCP_CS2 }, |
| 1431 | { "cs3", IPTOS_DSCP_CS3 }, |
| 1432 | { "cs4", IPTOS_DSCP_CS4 }, |
| 1433 | { "cs5", IPTOS_DSCP_CS5 }, |
| 1434 | { "cs6", IPTOS_DSCP_CS6 }, |
| 1435 | { "cs7", IPTOS_DSCP_CS7 }, |
| 1436 | { "ef", IPTOS_DSCP_EF }, |
| 1437 | { "lowdelay", IPTOS_LOWDELAY }, |
| 1438 | { "throughput", IPTOS_THROUGHPUT }, |
| 1439 | { "reliability", IPTOS_RELIABILITY }, |
| 1440 | { NULL, -1 } |
| 1441 | }; |
| 1442 | |
| 1443 | int |
| 1444 | parse_ipqos(const char *cp) |
| 1445 | { |
| 1446 | u_int i; |
| 1447 | char *ep; |
| 1448 | long val; |
| 1449 | |
| 1450 | if (cp == NULL) |
| 1451 | return -1; |
| 1452 | for (i = 0; ipqos[i].name != NULL; i++) { |
| 1453 | if (strcasecmp(cp, ipqos[i].name) == 0) |
| 1454 | return ipqos[i].value; |
| 1455 | } |
| 1456 | /* Try parsing as an integer */ |
| 1457 | val = strtol(cp, &ep, 0); |
| 1458 | if (*cp == '\0' || *ep != '\0' || val < 0 || val > 255) |
| 1459 | return -1; |
| 1460 | return val; |
| 1461 | } |
| 1462 | |
Damien Miller | 9147586 | 2011-05-05 14:14:34 +1000 | [diff] [blame] | 1463 | const char * |
| 1464 | iptos2str(int iptos) |
| 1465 | { |
| 1466 | int i; |
| 1467 | static char iptos_str[sizeof "0xff"]; |
| 1468 | |
| 1469 | for (i = 0; ipqos[i].name != NULL; i++) { |
| 1470 | if (ipqos[i].value == iptos) |
| 1471 | return ipqos[i].name; |
| 1472 | } |
| 1473 | snprintf(iptos_str, sizeof iptos_str, "0x%02x", iptos); |
| 1474 | return iptos_str; |
| 1475 | } |
Damien Miller | e9fc72e | 2013-10-15 12:14:12 +1100 | [diff] [blame] | 1476 | |
| 1477 | void |
| 1478 | lowercase(char *s) |
| 1479 | { |
| 1480 | for (; *s; s++) |
| 1481 | *s = tolower((u_char)*s); |
| 1482 | } |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 1483 | |
| 1484 | int |
| 1485 | unix_listener(const char *path, int backlog, int unlink_first) |
| 1486 | { |
| 1487 | struct sockaddr_un sunaddr; |
| 1488 | int saved_errno, sock; |
| 1489 | |
| 1490 | memset(&sunaddr, 0, sizeof(sunaddr)); |
| 1491 | sunaddr.sun_family = AF_UNIX; |
djm@openbsd.org | 155072f | 2017-12-08 02:13:02 +0000 | [diff] [blame] | 1492 | if (strlcpy(sunaddr.sun_path, path, |
| 1493 | sizeof(sunaddr.sun_path)) >= sizeof(sunaddr.sun_path)) { |
| 1494 | error("%s: path \"%s\" too long for Unix domain socket", |
| 1495 | __func__, path); |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 1496 | errno = ENAMETOOLONG; |
| 1497 | return -1; |
| 1498 | } |
| 1499 | |
| 1500 | sock = socket(PF_UNIX, SOCK_STREAM, 0); |
| 1501 | if (sock < 0) { |
| 1502 | saved_errno = errno; |
djm@openbsd.org | 155072f | 2017-12-08 02:13:02 +0000 | [diff] [blame] | 1503 | error("%s: socket: %.100s", __func__, strerror(errno)); |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 1504 | errno = saved_errno; |
| 1505 | return -1; |
| 1506 | } |
| 1507 | if (unlink_first == 1) { |
| 1508 | if (unlink(path) != 0 && errno != ENOENT) |
| 1509 | error("unlink(%s): %.100s", path, strerror(errno)); |
| 1510 | } |
| 1511 | if (bind(sock, (struct sockaddr *)&sunaddr, sizeof(sunaddr)) < 0) { |
| 1512 | saved_errno = errno; |
djm@openbsd.org | 155072f | 2017-12-08 02:13:02 +0000 | [diff] [blame] | 1513 | error("%s: cannot bind to path %s: %s", |
| 1514 | __func__, path, strerror(errno)); |
djm@openbsd.org | fd4eeee | 2017-12-08 02:14:33 +0000 | [diff] [blame] | 1515 | close(sock); |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 1516 | errno = saved_errno; |
| 1517 | return -1; |
| 1518 | } |
| 1519 | if (listen(sock, backlog) < 0) { |
| 1520 | saved_errno = errno; |
djm@openbsd.org | 155072f | 2017-12-08 02:13:02 +0000 | [diff] [blame] | 1521 | error("%s: cannot listen on path %s: %s", |
| 1522 | __func__, path, strerror(errno)); |
djm@openbsd.org | fd4eeee | 2017-12-08 02:14:33 +0000 | [diff] [blame] | 1523 | close(sock); |
| 1524 | unlink(path); |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 1525 | errno = saved_errno; |
| 1526 | return -1; |
| 1527 | } |
| 1528 | return sock; |
| 1529 | } |
Damien Miller | 4687802 | 2014-08-30 03:29:19 +1000 | [diff] [blame] | 1530 | |
Damien Miller | 04ee0f8 | 2009-11-18 17:48:30 +1100 | [diff] [blame] | 1531 | void |
| 1532 | sock_set_v6only(int s) |
| 1533 | { |
Damien Miller | d86a3ba | 2015-12-09 09:18:45 +1100 | [diff] [blame] | 1534 | #if defined(IPV6_V6ONLY) && !defined(__OpenBSD__) |
Damien Miller | 04ee0f8 | 2009-11-18 17:48:30 +1100 | [diff] [blame] | 1535 | int on = 1; |
| 1536 | |
| 1537 | debug3("%s: set socket %d IPV6_V6ONLY", __func__, s); |
| 1538 | if (setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof(on)) == -1) |
| 1539 | error("setsockopt IPV6_V6ONLY: %s", strerror(errno)); |
| 1540 | #endif |
| 1541 | } |
djm@openbsd.org | 0ccbd5e | 2016-04-06 06:42:17 +0000 | [diff] [blame] | 1542 | |
| 1543 | /* |
| 1544 | * Compares two strings that maybe be NULL. Returns non-zero if strings |
| 1545 | * are both NULL or are identical, returns zero otherwise. |
| 1546 | */ |
| 1547 | static int |
| 1548 | strcmp_maybe_null(const char *a, const char *b) |
| 1549 | { |
| 1550 | if ((a == NULL && b != NULL) || (a != NULL && b == NULL)) |
| 1551 | return 0; |
| 1552 | if (a != NULL && strcmp(a, b) != 0) |
| 1553 | return 0; |
| 1554 | return 1; |
| 1555 | } |
| 1556 | |
| 1557 | /* |
| 1558 | * Compare two forwards, returning non-zero if they are identical or |
| 1559 | * zero otherwise. |
| 1560 | */ |
| 1561 | int |
| 1562 | forward_equals(const struct Forward *a, const struct Forward *b) |
| 1563 | { |
| 1564 | if (strcmp_maybe_null(a->listen_host, b->listen_host) == 0) |
| 1565 | return 0; |
| 1566 | if (a->listen_port != b->listen_port) |
| 1567 | return 0; |
| 1568 | if (strcmp_maybe_null(a->listen_path, b->listen_path) == 0) |
| 1569 | return 0; |
| 1570 | if (strcmp_maybe_null(a->connect_host, b->connect_host) == 0) |
| 1571 | return 0; |
| 1572 | if (a->connect_port != b->connect_port) |
| 1573 | return 0; |
| 1574 | if (strcmp_maybe_null(a->connect_path, b->connect_path) == 0) |
| 1575 | return 0; |
| 1576 | /* allocated_port and handle are not checked */ |
| 1577 | return 1; |
| 1578 | } |
| 1579 | |
dtucker@openbsd.org | 1c4ef0b | 2016-10-23 22:04:05 +0000 | [diff] [blame] | 1580 | /* returns 1 if bind to specified port by specified user is permitted */ |
| 1581 | int |
| 1582 | bind_permitted(int port, uid_t uid) |
| 1583 | { |
| 1584 | if (port < IPPORT_RESERVED && uid != 0) |
| 1585 | return 0; |
| 1586 | return 1; |
| 1587 | } |
dtucker@openbsd.org | 7fc4766 | 2016-11-30 00:28:31 +0000 | [diff] [blame] | 1588 | |
| 1589 | /* returns 1 if process is already daemonized, 0 otherwise */ |
| 1590 | int |
| 1591 | daemonized(void) |
| 1592 | { |
| 1593 | int fd; |
| 1594 | |
| 1595 | if ((fd = open(_PATH_TTY, O_RDONLY | O_NOCTTY)) >= 0) { |
| 1596 | close(fd); |
| 1597 | return 0; /* have controlling terminal */ |
| 1598 | } |
| 1599 | if (getppid() != 1) |
| 1600 | return 0; /* parent is not init */ |
| 1601 | if (getsid(0) != getpid()) |
| 1602 | return 0; /* not session leader */ |
| 1603 | debug3("already daemonized"); |
| 1604 | return 1; |
| 1605 | } |
djm@openbsd.org | de4ae07 | 2017-08-18 05:36:45 +0000 | [diff] [blame] | 1606 | |
| 1607 | |
| 1608 | /* |
| 1609 | * Splits 's' into an argument vector. Handles quoted string and basic |
| 1610 | * escape characters (\\, \", \'). Caller must free the argument vector |
| 1611 | * and its members. |
| 1612 | */ |
| 1613 | int |
| 1614 | argv_split(const char *s, int *argcp, char ***argvp) |
| 1615 | { |
| 1616 | int r = SSH_ERR_INTERNAL_ERROR; |
| 1617 | int argc = 0, quote, i, j; |
| 1618 | char *arg, **argv = xcalloc(1, sizeof(*argv)); |
| 1619 | |
| 1620 | *argvp = NULL; |
| 1621 | *argcp = 0; |
| 1622 | |
| 1623 | for (i = 0; s[i] != '\0'; i++) { |
| 1624 | /* Skip leading whitespace */ |
| 1625 | if (s[i] == ' ' || s[i] == '\t') |
| 1626 | continue; |
| 1627 | |
| 1628 | /* Start of a token */ |
| 1629 | quote = 0; |
| 1630 | if (s[i] == '\\' && |
| 1631 | (s[i + 1] == '\'' || s[i + 1] == '\"' || s[i + 1] == '\\')) |
| 1632 | i++; |
| 1633 | else if (s[i] == '\'' || s[i] == '"') |
| 1634 | quote = s[i++]; |
| 1635 | |
| 1636 | argv = xreallocarray(argv, (argc + 2), sizeof(*argv)); |
| 1637 | arg = argv[argc++] = xcalloc(1, strlen(s + i) + 1); |
| 1638 | argv[argc] = NULL; |
| 1639 | |
| 1640 | /* Copy the token in, removing escapes */ |
| 1641 | for (j = 0; s[i] != '\0'; i++) { |
| 1642 | if (s[i] == '\\') { |
| 1643 | if (s[i + 1] == '\'' || |
| 1644 | s[i + 1] == '\"' || |
| 1645 | s[i + 1] == '\\') { |
| 1646 | i++; /* Skip '\' */ |
| 1647 | arg[j++] = s[i]; |
| 1648 | } else { |
| 1649 | /* Unrecognised escape */ |
| 1650 | arg[j++] = s[i]; |
| 1651 | } |
| 1652 | } else if (quote == 0 && (s[i] == ' ' || s[i] == '\t')) |
| 1653 | break; /* done */ |
| 1654 | else if (quote != 0 && s[i] == quote) |
| 1655 | break; /* done */ |
| 1656 | else |
| 1657 | arg[j++] = s[i]; |
| 1658 | } |
| 1659 | if (s[i] == '\0') { |
| 1660 | if (quote != 0) { |
| 1661 | /* Ran out of string looking for close quote */ |
| 1662 | r = SSH_ERR_INVALID_FORMAT; |
| 1663 | goto out; |
| 1664 | } |
| 1665 | break; |
| 1666 | } |
| 1667 | } |
| 1668 | /* Success */ |
| 1669 | *argcp = argc; |
| 1670 | *argvp = argv; |
| 1671 | argc = 0; |
| 1672 | argv = NULL; |
| 1673 | r = 0; |
| 1674 | out: |
| 1675 | if (argc != 0 && argv != NULL) { |
| 1676 | for (i = 0; i < argc; i++) |
| 1677 | free(argv[i]); |
| 1678 | free(argv); |
| 1679 | } |
| 1680 | return r; |
| 1681 | } |
| 1682 | |
| 1683 | /* |
| 1684 | * Reassemble an argument vector into a string, quoting and escaping as |
| 1685 | * necessary. Caller must free returned string. |
| 1686 | */ |
| 1687 | char * |
| 1688 | argv_assemble(int argc, char **argv) |
| 1689 | { |
| 1690 | int i, j, ws, r; |
| 1691 | char c, *ret; |
| 1692 | struct sshbuf *buf, *arg; |
| 1693 | |
| 1694 | if ((buf = sshbuf_new()) == NULL || (arg = sshbuf_new()) == NULL) |
| 1695 | fatal("%s: sshbuf_new failed", __func__); |
| 1696 | |
| 1697 | for (i = 0; i < argc; i++) { |
| 1698 | ws = 0; |
| 1699 | sshbuf_reset(arg); |
| 1700 | for (j = 0; argv[i][j] != '\0'; j++) { |
| 1701 | r = 0; |
| 1702 | c = argv[i][j]; |
| 1703 | switch (c) { |
| 1704 | case ' ': |
| 1705 | case '\t': |
| 1706 | ws = 1; |
| 1707 | r = sshbuf_put_u8(arg, c); |
| 1708 | break; |
| 1709 | case '\\': |
| 1710 | case '\'': |
| 1711 | case '"': |
| 1712 | if ((r = sshbuf_put_u8(arg, '\\')) != 0) |
| 1713 | break; |
| 1714 | /* FALLTHROUGH */ |
| 1715 | default: |
| 1716 | r = sshbuf_put_u8(arg, c); |
| 1717 | break; |
| 1718 | } |
| 1719 | if (r != 0) |
| 1720 | fatal("%s: sshbuf_put_u8: %s", |
| 1721 | __func__, ssh_err(r)); |
| 1722 | } |
| 1723 | if ((i != 0 && (r = sshbuf_put_u8(buf, ' ')) != 0) || |
| 1724 | (ws != 0 && (r = sshbuf_put_u8(buf, '"')) != 0) || |
| 1725 | (r = sshbuf_putb(buf, arg)) != 0 || |
| 1726 | (ws != 0 && (r = sshbuf_put_u8(buf, '"')) != 0)) |
| 1727 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
| 1728 | } |
| 1729 | if ((ret = malloc(sshbuf_len(buf) + 1)) == NULL) |
| 1730 | fatal("%s: malloc failed", __func__); |
| 1731 | memcpy(ret, sshbuf_ptr(buf), sshbuf_len(buf)); |
| 1732 | ret[sshbuf_len(buf)] = '\0'; |
| 1733 | sshbuf_free(buf); |
| 1734 | sshbuf_free(arg); |
| 1735 | return ret; |
| 1736 | } |
| 1737 | |
djm@openbsd.org | de4ae07 | 2017-08-18 05:36:45 +0000 | [diff] [blame] | 1738 | /* Returns 0 if pid exited cleanly, non-zero otherwise */ |
| 1739 | int |
djm@openbsd.org | b074c3c | 2017-08-18 05:48:04 +0000 | [diff] [blame] | 1740 | exited_cleanly(pid_t pid, const char *tag, const char *cmd, int quiet) |
djm@openbsd.org | de4ae07 | 2017-08-18 05:36:45 +0000 | [diff] [blame] | 1741 | { |
| 1742 | int status; |
| 1743 | |
| 1744 | while (waitpid(pid, &status, 0) == -1) { |
| 1745 | if (errno != EINTR) { |
| 1746 | error("%s: waitpid: %s", tag, strerror(errno)); |
| 1747 | return -1; |
| 1748 | } |
| 1749 | } |
| 1750 | if (WIFSIGNALED(status)) { |
| 1751 | error("%s %s exited on signal %d", tag, cmd, WTERMSIG(status)); |
| 1752 | return -1; |
| 1753 | } else if (WEXITSTATUS(status) != 0) { |
djm@openbsd.org | b074c3c | 2017-08-18 05:48:04 +0000 | [diff] [blame] | 1754 | do_log2(quiet ? SYSLOG_LEVEL_DEBUG1 : SYSLOG_LEVEL_INFO, |
| 1755 | "%s %s failed, status %d", tag, cmd, WEXITSTATUS(status)); |
djm@openbsd.org | de4ae07 | 2017-08-18 05:36:45 +0000 | [diff] [blame] | 1756 | return -1; |
| 1757 | } |
| 1758 | return 0; |
| 1759 | } |
| 1760 | |
| 1761 | /* |
| 1762 | * Check a given path for security. This is defined as all components |
| 1763 | * of the path to the file must be owned by either the owner of |
| 1764 | * of the file or root and no directories must be group or world writable. |
| 1765 | * |
| 1766 | * XXX Should any specific check be done for sym links ? |
| 1767 | * |
| 1768 | * Takes a file name, its stat information (preferably from fstat() to |
| 1769 | * avoid races), the uid of the expected owner, their home directory and an |
| 1770 | * error buffer plus max size as arguments. |
| 1771 | * |
| 1772 | * Returns 0 on success and -1 on failure |
| 1773 | */ |
| 1774 | int |
| 1775 | safe_path(const char *name, struct stat *stp, const char *pw_dir, |
| 1776 | uid_t uid, char *err, size_t errlen) |
| 1777 | { |
| 1778 | char buf[PATH_MAX], homedir[PATH_MAX]; |
| 1779 | char *cp; |
| 1780 | int comparehome = 0; |
| 1781 | struct stat st; |
| 1782 | |
| 1783 | if (realpath(name, buf) == NULL) { |
| 1784 | snprintf(err, errlen, "realpath %s failed: %s", name, |
| 1785 | strerror(errno)); |
| 1786 | return -1; |
| 1787 | } |
| 1788 | if (pw_dir != NULL && realpath(pw_dir, homedir) != NULL) |
| 1789 | comparehome = 1; |
| 1790 | |
| 1791 | if (!S_ISREG(stp->st_mode)) { |
| 1792 | snprintf(err, errlen, "%s is not a regular file", buf); |
| 1793 | return -1; |
| 1794 | } |
| 1795 | if ((!platform_sys_dir_uid(stp->st_uid) && stp->st_uid != uid) || |
| 1796 | (stp->st_mode & 022) != 0) { |
| 1797 | snprintf(err, errlen, "bad ownership or modes for file %s", |
| 1798 | buf); |
| 1799 | return -1; |
| 1800 | } |
| 1801 | |
| 1802 | /* for each component of the canonical path, walking upwards */ |
| 1803 | for (;;) { |
| 1804 | if ((cp = dirname(buf)) == NULL) { |
| 1805 | snprintf(err, errlen, "dirname() failed"); |
| 1806 | return -1; |
| 1807 | } |
| 1808 | strlcpy(buf, cp, sizeof(buf)); |
| 1809 | |
| 1810 | if (stat(buf, &st) < 0 || |
| 1811 | (!platform_sys_dir_uid(st.st_uid) && st.st_uid != uid) || |
| 1812 | (st.st_mode & 022) != 0) { |
| 1813 | snprintf(err, errlen, |
| 1814 | "bad ownership or modes for directory %s", buf); |
| 1815 | return -1; |
| 1816 | } |
| 1817 | |
| 1818 | /* If are past the homedir then we can stop */ |
| 1819 | if (comparehome && strcmp(homedir, buf) == 0) |
| 1820 | break; |
| 1821 | |
| 1822 | /* |
| 1823 | * dirname should always complete with a "/" path, |
| 1824 | * but we can be paranoid and check for "." too |
| 1825 | */ |
| 1826 | if ((strcmp("/", buf) == 0) || (strcmp(".", buf) == 0)) |
| 1827 | break; |
| 1828 | } |
| 1829 | return 0; |
| 1830 | } |
| 1831 | |
| 1832 | /* |
| 1833 | * Version of safe_path() that accepts an open file descriptor to |
| 1834 | * avoid races. |
| 1835 | * |
| 1836 | * Returns 0 on success and -1 on failure |
| 1837 | */ |
| 1838 | int |
| 1839 | safe_path_fd(int fd, const char *file, struct passwd *pw, |
| 1840 | char *err, size_t errlen) |
| 1841 | { |
| 1842 | struct stat st; |
| 1843 | |
| 1844 | /* check the open file to avoid races */ |
| 1845 | if (fstat(fd, &st) < 0) { |
| 1846 | snprintf(err, errlen, "cannot stat file %s: %s", |
| 1847 | file, strerror(errno)); |
| 1848 | return -1; |
| 1849 | } |
| 1850 | return safe_path(file, &st, pw->pw_dir, pw->pw_uid, err, errlen); |
| 1851 | } |
| 1852 | |
| 1853 | /* |
| 1854 | * Sets the value of the given variable in the environment. If the variable |
| 1855 | * already exists, its value is overridden. |
| 1856 | */ |
| 1857 | void |
| 1858 | child_set_env(char ***envp, u_int *envsizep, const char *name, |
| 1859 | const char *value) |
| 1860 | { |
| 1861 | char **env; |
| 1862 | u_int envsize; |
| 1863 | u_int i, namelen; |
| 1864 | |
| 1865 | if (strchr(name, '=') != NULL) { |
| 1866 | error("Invalid environment variable \"%.100s\"", name); |
| 1867 | return; |
| 1868 | } |
| 1869 | |
| 1870 | /* |
| 1871 | * If we're passed an uninitialized list, allocate a single null |
| 1872 | * entry before continuing. |
| 1873 | */ |
| 1874 | if (*envp == NULL && *envsizep == 0) { |
| 1875 | *envp = xmalloc(sizeof(char *)); |
| 1876 | *envp[0] = NULL; |
| 1877 | *envsizep = 1; |
| 1878 | } |
| 1879 | |
| 1880 | /* |
| 1881 | * Find the slot where the value should be stored. If the variable |
| 1882 | * already exists, we reuse the slot; otherwise we append a new slot |
| 1883 | * at the end of the array, expanding if necessary. |
| 1884 | */ |
| 1885 | env = *envp; |
| 1886 | namelen = strlen(name); |
| 1887 | for (i = 0; env[i]; i++) |
| 1888 | if (strncmp(env[i], name, namelen) == 0 && env[i][namelen] == '=') |
| 1889 | break; |
| 1890 | if (env[i]) { |
| 1891 | /* Reuse the slot. */ |
| 1892 | free(env[i]); |
| 1893 | } else { |
| 1894 | /* New variable. Expand if necessary. */ |
| 1895 | envsize = *envsizep; |
| 1896 | if (i >= envsize - 1) { |
| 1897 | if (envsize >= 1000) |
| 1898 | fatal("child_set_env: too many env vars"); |
| 1899 | envsize += 50; |
| 1900 | env = (*envp) = xreallocarray(env, envsize, sizeof(char *)); |
| 1901 | *envsizep = envsize; |
| 1902 | } |
| 1903 | /* Need to set the NULL pointer at end of array beyond the new slot. */ |
| 1904 | env[i + 1] = NULL; |
| 1905 | } |
| 1906 | |
| 1907 | /* Allocate space and format the variable in the appropriate slot. */ |
djm@openbsd.org | 7c85685 | 2018-03-03 03:15:51 +0000 | [diff] [blame] | 1908 | /* XXX xasprintf */ |
djm@openbsd.org | de4ae07 | 2017-08-18 05:36:45 +0000 | [diff] [blame] | 1909 | env[i] = xmalloc(strlen(name) + 1 + strlen(value) + 1); |
| 1910 | snprintf(env[i], strlen(name) + 1 + strlen(value) + 1, "%s=%s", name, value); |
| 1911 | } |
| 1912 | |
millert@openbsd.org | 887669e | 2017-10-21 23:06:24 +0000 | [diff] [blame] | 1913 | /* |
| 1914 | * Check and optionally lowercase a domain name, also removes trailing '.' |
| 1915 | * Returns 1 on success and 0 on failure, storing an error message in errstr. |
| 1916 | */ |
| 1917 | int |
| 1918 | valid_domain(char *name, int makelower, const char **errstr) |
| 1919 | { |
| 1920 | size_t i, l = strlen(name); |
| 1921 | u_char c, last = '\0'; |
| 1922 | static char errbuf[256]; |
| 1923 | |
| 1924 | if (l == 0) { |
| 1925 | strlcpy(errbuf, "empty domain name", sizeof(errbuf)); |
| 1926 | goto bad; |
| 1927 | } |
| 1928 | if (!isalpha((u_char)name[0]) && !isdigit((u_char)name[0])) { |
| 1929 | snprintf(errbuf, sizeof(errbuf), "domain name \"%.100s\" " |
| 1930 | "starts with invalid character", name); |
| 1931 | goto bad; |
| 1932 | } |
| 1933 | for (i = 0; i < l; i++) { |
| 1934 | c = tolower((u_char)name[i]); |
| 1935 | if (makelower) |
| 1936 | name[i] = (char)c; |
| 1937 | if (last == '.' && c == '.') { |
| 1938 | snprintf(errbuf, sizeof(errbuf), "domain name " |
| 1939 | "\"%.100s\" contains consecutive separators", name); |
| 1940 | goto bad; |
| 1941 | } |
| 1942 | if (c != '.' && c != '-' && !isalnum(c) && |
| 1943 | c != '_') /* technically invalid, but common */ { |
| 1944 | snprintf(errbuf, sizeof(errbuf), "domain name " |
| 1945 | "\"%.100s\" contains invalid characters", name); |
| 1946 | goto bad; |
| 1947 | } |
| 1948 | last = c; |
| 1949 | } |
| 1950 | if (name[l - 1] == '.') |
| 1951 | name[l - 1] = '\0'; |
| 1952 | if (errstr != NULL) |
| 1953 | *errstr = NULL; |
| 1954 | return 1; |
| 1955 | bad: |
| 1956 | if (errstr != NULL) |
| 1957 | *errstr = errbuf; |
| 1958 | return 0; |
| 1959 | } |
dtucker@openbsd.org | 609d96b | 2017-12-05 23:59:47 +0000 | [diff] [blame] | 1960 | |
| 1961 | const char * |
| 1962 | atoi_err(const char *nptr, int *val) |
| 1963 | { |
| 1964 | const char *errstr = NULL; |
| 1965 | long long num; |
| 1966 | |
| 1967 | if (nptr == NULL || *nptr == '\0') |
| 1968 | return "missing"; |
| 1969 | num = strtonum(nptr, 0, INT_MAX, &errstr); |
| 1970 | if (errstr == NULL) |
| 1971 | *val = (int)num; |
| 1972 | return errstr; |
| 1973 | } |
djm@openbsd.org | bf0fbf2 | 2018-03-12 00:52:01 +0000 | [diff] [blame] | 1974 | |
| 1975 | int |
| 1976 | parse_absolute_time(const char *s, uint64_t *tp) |
| 1977 | { |
| 1978 | struct tm tm; |
| 1979 | time_t tt; |
| 1980 | char buf[32], *fmt; |
| 1981 | |
| 1982 | *tp = 0; |
| 1983 | |
| 1984 | /* |
| 1985 | * POSIX strptime says "The application shall ensure that there |
| 1986 | * is white-space or other non-alphanumeric characters between |
| 1987 | * any two conversion specifications" so arrange things this way. |
| 1988 | */ |
| 1989 | switch (strlen(s)) { |
| 1990 | case 8: /* YYYYMMDD */ |
| 1991 | fmt = "%Y-%m-%d"; |
| 1992 | snprintf(buf, sizeof(buf), "%.4s-%.2s-%.2s", s, s + 4, s + 6); |
| 1993 | break; |
| 1994 | case 12: /* YYYYMMDDHHMM */ |
| 1995 | fmt = "%Y-%m-%dT%H:%M"; |
| 1996 | snprintf(buf, sizeof(buf), "%.4s-%.2s-%.2sT%.2s:%.2s", |
| 1997 | s, s + 4, s + 6, s + 8, s + 10); |
| 1998 | break; |
| 1999 | case 14: /* YYYYMMDDHHMMSS */ |
| 2000 | fmt = "%Y-%m-%dT%H:%M:%S"; |
| 2001 | snprintf(buf, sizeof(buf), "%.4s-%.2s-%.2sT%.2s:%.2s:%.2s", |
| 2002 | s, s + 4, s + 6, s + 8, s + 10, s + 12); |
| 2003 | break; |
| 2004 | default: |
| 2005 | return SSH_ERR_INVALID_FORMAT; |
| 2006 | } |
| 2007 | |
| 2008 | memset(&tm, 0, sizeof(tm)); |
| 2009 | if (strptime(buf, fmt, &tm) == NULL) |
| 2010 | return SSH_ERR_INVALID_FORMAT; |
| 2011 | if ((tt = mktime(&tm)) < 0) |
| 2012 | return SSH_ERR_INVALID_FORMAT; |
| 2013 | /* success */ |
| 2014 | *tp = (uint64_t)tt; |
| 2015 | return 0; |
| 2016 | } |
| 2017 | |
| 2018 | void |
| 2019 | format_absolute_time(uint64_t t, char *buf, size_t len) |
| 2020 | { |
| 2021 | time_t tt = t > INT_MAX ? INT_MAX : t; /* XXX revisit in 2038 :P */ |
| 2022 | struct tm tm; |
| 2023 | |
| 2024 | localtime_r(&tt, &tm); |
| 2025 | strftime(buf, len, "%Y-%m-%dT%H:%M:%S", &tm); |
| 2026 | } |