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