dtucker@openbsd.org | c6774d2 | 2017-03-14 00:55:37 +0000 | [diff] [blame] | 1 | /* $OpenBSD: misc.c,v 1.109 2017/03/14 00:55:37 dtucker 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> |
dtucker@openbsd.org | 65a3c0d | 2016-01-20 09:22:39 +0000 | [diff] [blame] | 32 | #include <sys/time.h> |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 33 | #include <sys/un.h> |
Damien Miller | 8ec8c3e | 2006-07-10 20:35:38 +1000 | [diff] [blame] | 34 | |
deraadt@openbsd.org | 2ae4f33 | 2015-01-16 06:40:12 +0000 | [diff] [blame] | 35 | #include <limits.h> |
Darren Tucker | 5d19626 | 2006-07-12 22:15:16 +1000 | [diff] [blame] | 36 | #include <stdarg.h> |
Damien Miller | a7a73ee | 2006-08-05 11:37:59 +1000 | [diff] [blame] | 37 | #include <stdio.h> |
Damien Miller | e7a1e5c | 2006-08-05 11:34:19 +1000 | [diff] [blame] | 38 | #include <stdlib.h> |
Damien Miller | e3476ed | 2006-07-24 14:13:33 +1000 | [diff] [blame] | 39 | #include <string.h> |
Damien Miller | 1708cb7 | 2011-01-13 12:21:34 +1100 | [diff] [blame] | 40 | #include <time.h> |
Damien Miller | e6b3b61 | 2006-07-24 14:01:23 +1000 | [diff] [blame] | 41 | #include <unistd.h> |
Darren Tucker | 5d19626 | 2006-07-12 22:15:16 +1000 | [diff] [blame] | 42 | |
Damien Miller | 8ec8c3e | 2006-07-10 20:35:38 +1000 | [diff] [blame] | 43 | #include <netinet/in.h> |
Damien Miller | 0dac6fb | 2010-11-20 15:19:38 +1100 | [diff] [blame] | 44 | #include <netinet/in_systm.h> |
| 45 | #include <netinet/ip.h> |
Damien Miller | 3a4051e | 2006-03-15 11:19:42 +1100 | [diff] [blame] | 46 | #include <netinet/tcp.h> |
Damien Miller | 8ec8c3e | 2006-07-10 20:35:38 +1000 | [diff] [blame] | 47 | |
Damien Miller | e9fc72e | 2013-10-15 12:14:12 +1100 | [diff] [blame] | 48 | #include <ctype.h> |
Darren Tucker | 3997249 | 2006-07-12 22:22:46 +1000 | [diff] [blame] | 49 | #include <errno.h> |
Damien Miller | 57cf638 | 2006-07-10 21:13:46 +1000 | [diff] [blame] | 50 | #include <fcntl.h> |
Darren Tucker | 4abde77 | 2007-12-29 02:43:51 +1100 | [diff] [blame] | 51 | #include <netdb.h> |
Damien Miller | 03e2003 | 2006-03-15 11:16:59 +1100 | [diff] [blame] | 52 | #ifdef HAVE_PATHS_H |
Damien Miller | a9263d0 | 2006-03-15 11:18:26 +1100 | [diff] [blame] | 53 | # include <paths.h> |
Damien Miller | 9f2abc4 | 2006-07-10 20:53:08 +1000 | [diff] [blame] | 54 | #include <pwd.h> |
Damien Miller | 03e2003 | 2006-03-15 11:16:59 +1100 | [diff] [blame] | 55 | #endif |
Damien Miller | 3beb852 | 2006-01-02 23:40:10 +1100 | [diff] [blame] | 56 | #ifdef SSH_TUN_OPENBSD |
| 57 | #include <net/if.h> |
| 58 | #endif |
Damien Miller | 61c5150 | 2000-08-18 14:01:04 +1000 | [diff] [blame] | 59 | |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 60 | #include "xmalloc.h" |
Kevin Steves | b6e773a | 2001-02-04 13:20:36 +0000 | [diff] [blame] | 61 | #include "misc.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 62 | #include "log.h" |
Darren Tucker | da34553 | 2006-07-10 23:04:19 +1000 | [diff] [blame] | 63 | #include "ssh.h" |
Damien Miller | 61c5150 | 2000-08-18 14:01:04 +1000 | [diff] [blame] | 64 | |
Ben Lindstrom | 4cc240d | 2001-07-04 04:46:56 +0000 | [diff] [blame] | 65 | /* remove newline at end of string */ |
Damien Miller | 61c5150 | 2000-08-18 14:01:04 +1000 | [diff] [blame] | 66 | char * |
| 67 | chop(char *s) |
| 68 | { |
| 69 | char *t = s; |
| 70 | while (*t) { |
Ben Lindstrom | 1c37c6a | 2001-12-06 18:00:18 +0000 | [diff] [blame] | 71 | if (*t == '\n' || *t == '\r') { |
Damien Miller | 61c5150 | 2000-08-18 14:01:04 +1000 | [diff] [blame] | 72 | *t = '\0'; |
| 73 | return s; |
| 74 | } |
| 75 | t++; |
| 76 | } |
| 77 | return s; |
| 78 | |
| 79 | } |
| 80 | |
Ben Lindstrom | 4cc240d | 2001-07-04 04:46:56 +0000 | [diff] [blame] | 81 | /* set/unset filedescriptor to non-blocking */ |
Damien Miller | 232711f | 2004-06-15 10:35:30 +1000 | [diff] [blame] | 82 | int |
Damien Miller | 61c5150 | 2000-08-18 14:01:04 +1000 | [diff] [blame] | 83 | set_nonblock(int fd) |
| 84 | { |
| 85 | int val; |
Ben Lindstrom | c93e84c | 2001-05-12 00:08:37 +0000 | [diff] [blame] | 86 | |
krw@openbsd.org | 574def0 | 2016-04-02 14:37:42 +0000 | [diff] [blame] | 87 | val = fcntl(fd, F_GETFL); |
Damien Miller | 61c5150 | 2000-08-18 14:01:04 +1000 | [diff] [blame] | 88 | if (val < 0) { |
krw@openbsd.org | 574def0 | 2016-04-02 14:37:42 +0000 | [diff] [blame] | 89 | error("fcntl(%d, F_GETFL): %s", fd, strerror(errno)); |
Damien Miller | 232711f | 2004-06-15 10:35:30 +1000 | [diff] [blame] | 90 | return (-1); |
Damien Miller | 61c5150 | 2000-08-18 14:01:04 +1000 | [diff] [blame] | 91 | } |
Damien Miller | 69b69aa | 2000-10-28 14:19:58 +1100 | [diff] [blame] | 92 | if (val & O_NONBLOCK) { |
Damien Miller | 232711f | 2004-06-15 10:35:30 +1000 | [diff] [blame] | 93 | debug3("fd %d is O_NONBLOCK", fd); |
| 94 | return (0); |
Damien Miller | 69b69aa | 2000-10-28 14:19:58 +1100 | [diff] [blame] | 95 | } |
Damien Miller | ef095ce | 2003-05-14 13:41:39 +1000 | [diff] [blame] | 96 | debug2("fd %d setting O_NONBLOCK", fd); |
Damien Miller | 61c5150 | 2000-08-18 14:01:04 +1000 | [diff] [blame] | 97 | val |= O_NONBLOCK; |
Damien Miller | 232711f | 2004-06-15 10:35:30 +1000 | [diff] [blame] | 98 | if (fcntl(fd, F_SETFL, val) == -1) { |
| 99 | debug("fcntl(%d, F_SETFL, O_NONBLOCK): %s", fd, |
| 100 | strerror(errno)); |
| 101 | return (-1); |
| 102 | } |
| 103 | return (0); |
Damien Miller | 61c5150 | 2000-08-18 14:01:04 +1000 | [diff] [blame] | 104 | } |
| 105 | |
Damien Miller | 232711f | 2004-06-15 10:35:30 +1000 | [diff] [blame] | 106 | int |
Ben Lindstrom | c93e84c | 2001-05-12 00:08:37 +0000 | [diff] [blame] | 107 | unset_nonblock(int fd) |
| 108 | { |
| 109 | int val; |
| 110 | |
krw@openbsd.org | 574def0 | 2016-04-02 14:37:42 +0000 | [diff] [blame] | 111 | val = fcntl(fd, F_GETFL); |
Ben Lindstrom | c93e84c | 2001-05-12 00:08:37 +0000 | [diff] [blame] | 112 | if (val < 0) { |
krw@openbsd.org | 574def0 | 2016-04-02 14:37:42 +0000 | [diff] [blame] | 113 | error("fcntl(%d, F_GETFL): %s", fd, strerror(errno)); |
Damien Miller | 232711f | 2004-06-15 10:35:30 +1000 | [diff] [blame] | 114 | return (-1); |
Ben Lindstrom | c93e84c | 2001-05-12 00:08:37 +0000 | [diff] [blame] | 115 | } |
| 116 | if (!(val & O_NONBLOCK)) { |
Damien Miller | 232711f | 2004-06-15 10:35:30 +1000 | [diff] [blame] | 117 | debug3("fd %d is not O_NONBLOCK", fd); |
| 118 | return (0); |
Ben Lindstrom | c93e84c | 2001-05-12 00:08:37 +0000 | [diff] [blame] | 119 | } |
Ben Lindstrom | 352b1c2 | 2001-06-21 03:04:37 +0000 | [diff] [blame] | 120 | debug("fd %d clearing O_NONBLOCK", fd); |
Ben Lindstrom | c93e84c | 2001-05-12 00:08:37 +0000 | [diff] [blame] | 121 | val &= ~O_NONBLOCK; |
Damien Miller | 232711f | 2004-06-15 10:35:30 +1000 | [diff] [blame] | 122 | if (fcntl(fd, F_SETFL, val) == -1) { |
| 123 | debug("fcntl(%d, F_SETFL, ~O_NONBLOCK): %s", |
Ben Lindstrom | 84fcb31 | 2002-03-05 01:48:09 +0000 | [diff] [blame] | 124 | fd, strerror(errno)); |
Damien Miller | 232711f | 2004-06-15 10:35:30 +1000 | [diff] [blame] | 125 | return (-1); |
| 126 | } |
| 127 | return (0); |
Ben Lindstrom | c93e84c | 2001-05-12 00:08:37 +0000 | [diff] [blame] | 128 | } |
| 129 | |
Darren Tucker | 4abde77 | 2007-12-29 02:43:51 +1100 | [diff] [blame] | 130 | const char * |
| 131 | ssh_gai_strerror(int gaierr) |
| 132 | { |
Damien Miller | 7313fc9 | 2013-07-18 16:13:19 +1000 | [diff] [blame] | 133 | if (gaierr == EAI_SYSTEM && errno != 0) |
Darren Tucker | 912428c | 2008-01-01 20:33:35 +1100 | [diff] [blame] | 134 | return strerror(errno); |
| 135 | return gai_strerror(gaierr); |
Darren Tucker | 4abde77 | 2007-12-29 02:43:51 +1100 | [diff] [blame] | 136 | } |
| 137 | |
Damien Miller | 398e1cf | 2002-02-05 11:52:13 +1100 | [diff] [blame] | 138 | /* disable nagle on socket */ |
| 139 | void |
| 140 | set_nodelay(int fd) |
| 141 | { |
Ben Lindstrom | e86de51 | 2002-03-05 01:28:14 +0000 | [diff] [blame] | 142 | int opt; |
| 143 | socklen_t optlen; |
Damien Miller | 398e1cf | 2002-02-05 11:52:13 +1100 | [diff] [blame] | 144 | |
Ben Lindstrom | 1ebd7a5 | 2002-02-26 18:12:51 +0000 | [diff] [blame] | 145 | optlen = sizeof opt; |
| 146 | if (getsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &opt, &optlen) == -1) { |
Darren Tucker | 6db8f93 | 2003-11-03 20:07:14 +1100 | [diff] [blame] | 147 | debug("getsockopt TCP_NODELAY: %.100s", strerror(errno)); |
Ben Lindstrom | 1ebd7a5 | 2002-02-26 18:12:51 +0000 | [diff] [blame] | 148 | return; |
| 149 | } |
| 150 | if (opt == 1) { |
| 151 | debug2("fd %d is TCP_NODELAY", fd); |
| 152 | return; |
| 153 | } |
| 154 | opt = 1; |
Ben Lindstrom | 1d568f9 | 2002-12-23 02:44:36 +0000 | [diff] [blame] | 155 | debug2("fd %d setting TCP_NODELAY", fd); |
Ben Lindstrom | 1ebd7a5 | 2002-02-26 18:12:51 +0000 | [diff] [blame] | 156 | if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &opt, sizeof opt) == -1) |
Damien Miller | 398e1cf | 2002-02-05 11:52:13 +1100 | [diff] [blame] | 157 | error("setsockopt TCP_NODELAY: %.100s", strerror(errno)); |
| 158 | } |
| 159 | |
Damien Miller | 61c5150 | 2000-08-18 14:01:04 +1000 | [diff] [blame] | 160 | /* Characters considered whitespace in strsep calls. */ |
| 161 | #define WHITESPACE " \t\r\n" |
Damien Miller | 306d118 | 2006-03-15 12:05:59 +1100 | [diff] [blame] | 162 | #define QUOTE "\"" |
Damien Miller | 61c5150 | 2000-08-18 14:01:04 +1000 | [diff] [blame] | 163 | |
Ben Lindstrom | 4cc240d | 2001-07-04 04:46:56 +0000 | [diff] [blame] | 164 | /* return next token in configuration line */ |
Damien Miller | 61c5150 | 2000-08-18 14:01:04 +1000 | [diff] [blame] | 165 | char * |
| 166 | strdelim(char **s) |
| 167 | { |
| 168 | char *old; |
| 169 | int wspace = 0; |
| 170 | |
| 171 | if (*s == NULL) |
| 172 | return NULL; |
| 173 | |
| 174 | old = *s; |
| 175 | |
Damien Miller | 306d118 | 2006-03-15 12:05:59 +1100 | [diff] [blame] | 176 | *s = strpbrk(*s, WHITESPACE QUOTE "="); |
Damien Miller | 61c5150 | 2000-08-18 14:01:04 +1000 | [diff] [blame] | 177 | if (*s == NULL) |
| 178 | return (old); |
| 179 | |
Damien Miller | 306d118 | 2006-03-15 12:05:59 +1100 | [diff] [blame] | 180 | if (*s[0] == '\"') { |
| 181 | memmove(*s, *s + 1, strlen(*s)); /* move nul too */ |
| 182 | /* Find matching quote */ |
| 183 | if ((*s = strpbrk(*s, QUOTE)) == NULL) { |
| 184 | return (NULL); /* no matching quote */ |
| 185 | } else { |
| 186 | *s[0] = '\0'; |
Damien Miller | 9308fc7 | 2010-07-16 13:56:01 +1000 | [diff] [blame] | 187 | *s += strspn(*s + 1, WHITESPACE) + 1; |
Damien Miller | 306d118 | 2006-03-15 12:05:59 +1100 | [diff] [blame] | 188 | return (old); |
| 189 | } |
| 190 | } |
| 191 | |
Damien Miller | 61c5150 | 2000-08-18 14:01:04 +1000 | [diff] [blame] | 192 | /* Allow only one '=' to be skipped */ |
| 193 | if (*s[0] == '=') |
| 194 | wspace = 1; |
| 195 | *s[0] = '\0'; |
| 196 | |
Damien Miller | 306d118 | 2006-03-15 12:05:59 +1100 | [diff] [blame] | 197 | /* Skip any extra whitespace after first token */ |
Damien Miller | 61c5150 | 2000-08-18 14:01:04 +1000 | [diff] [blame] | 198 | *s += strspn(*s + 1, WHITESPACE) + 1; |
| 199 | if (*s[0] == '=' && !wspace) |
| 200 | *s += strspn(*s + 1, WHITESPACE) + 1; |
| 201 | |
| 202 | return (old); |
| 203 | } |
Kevin Steves | b6e773a | 2001-02-04 13:20:36 +0000 | [diff] [blame] | 204 | |
Ben Lindstrom | 086cf21 | 2001-03-05 05:56:40 +0000 | [diff] [blame] | 205 | struct passwd * |
| 206 | pwcopy(struct passwd *pw) |
| 207 | { |
Damien Miller | 07d86be | 2006-03-26 14:19:21 +1100 | [diff] [blame] | 208 | struct passwd *copy = xcalloc(1, sizeof(*copy)); |
Ben Lindstrom | 4030442 | 2001-03-05 06:22:01 +0000 | [diff] [blame] | 209 | |
Ben Lindstrom | 086cf21 | 2001-03-05 05:56:40 +0000 | [diff] [blame] | 210 | copy->pw_name = xstrdup(pw->pw_name); |
| 211 | copy->pw_passwd = xstrdup(pw->pw_passwd); |
Damien Miller | 6332da2 | 2013-04-23 14:25:52 +1000 | [diff] [blame] | 212 | #ifdef HAVE_STRUCT_PASSWD_PW_GECOS |
Ben Lindstrom | 4030442 | 2001-03-05 06:22:01 +0000 | [diff] [blame] | 213 | copy->pw_gecos = xstrdup(pw->pw_gecos); |
Damien Miller | 6332da2 | 2013-04-23 14:25:52 +1000 | [diff] [blame] | 214 | #endif |
Ben Lindstrom | 086cf21 | 2001-03-05 05:56:40 +0000 | [diff] [blame] | 215 | copy->pw_uid = pw->pw_uid; |
| 216 | copy->pw_gid = pw->pw_gid; |
Damien Miller | 6332da2 | 2013-04-23 14:25:52 +1000 | [diff] [blame] | 217 | #ifdef HAVE_STRUCT_PASSWD_PW_EXPIRE |
Ben Lindstrom | 3af4d46 | 2001-06-21 03:11:27 +0000 | [diff] [blame] | 218 | copy->pw_expire = pw->pw_expire; |
Kevin Steves | 8245695 | 2001-06-22 21:14:18 +0000 | [diff] [blame] | 219 | #endif |
Damien Miller | 6332da2 | 2013-04-23 14:25:52 +1000 | [diff] [blame] | 220 | #ifdef HAVE_STRUCT_PASSWD_PW_CHANGE |
Ben Lindstrom | 3af4d46 | 2001-06-21 03:11:27 +0000 | [diff] [blame] | 221 | copy->pw_change = pw->pw_change; |
Kevin Steves | 8245695 | 2001-06-22 21:14:18 +0000 | [diff] [blame] | 222 | #endif |
Damien Miller | 6332da2 | 2013-04-23 14:25:52 +1000 | [diff] [blame] | 223 | #ifdef HAVE_STRUCT_PASSWD_PW_CLASS |
Ben Lindstrom | 086cf21 | 2001-03-05 05:56:40 +0000 | [diff] [blame] | 224 | copy->pw_class = xstrdup(pw->pw_class); |
Ben Lindstrom | 0f68db4 | 2001-03-05 07:57:09 +0000 | [diff] [blame] | 225 | #endif |
Ben Lindstrom | 086cf21 | 2001-03-05 05:56:40 +0000 | [diff] [blame] | 226 | copy->pw_dir = xstrdup(pw->pw_dir); |
| 227 | copy->pw_shell = xstrdup(pw->pw_shell); |
| 228 | return copy; |
| 229 | } |
| 230 | |
Ben Lindstrom | 4cc240d | 2001-07-04 04:46:56 +0000 | [diff] [blame] | 231 | /* |
| 232 | * Convert ASCII string to TCP/IP port number. |
Damien Miller | 3dc71ad | 2009-01-28 16:31:22 +1100 | [diff] [blame] | 233 | * Port must be >=0 and <=65535. |
| 234 | * Return -1 if invalid. |
Ben Lindstrom | 4cc240d | 2001-07-04 04:46:56 +0000 | [diff] [blame] | 235 | */ |
| 236 | int |
| 237 | a2port(const char *s) |
Ben Lindstrom | 19066a1 | 2001-04-12 23:39:26 +0000 | [diff] [blame] | 238 | { |
Damien Miller | 3dc71ad | 2009-01-28 16:31:22 +1100 | [diff] [blame] | 239 | long long port; |
| 240 | const char *errstr; |
Ben Lindstrom | 19066a1 | 2001-04-12 23:39:26 +0000 | [diff] [blame] | 241 | |
Damien Miller | 3dc71ad | 2009-01-28 16:31:22 +1100 | [diff] [blame] | 242 | port = strtonum(s, 0, 65535, &errstr); |
| 243 | if (errstr != NULL) |
| 244 | return -1; |
| 245 | return (int)port; |
Ben Lindstrom | 19066a1 | 2001-04-12 23:39:26 +0000 | [diff] [blame] | 246 | } |
| 247 | |
Damien Miller | d27b947 | 2005-12-13 19:29:02 +1100 | [diff] [blame] | 248 | int |
| 249 | a2tun(const char *s, int *remote) |
| 250 | { |
| 251 | const char *errstr = NULL; |
| 252 | char *sp, *ep; |
| 253 | int tun; |
| 254 | |
| 255 | if (remote != NULL) { |
Damien Miller | 7b58e80 | 2005-12-13 19:33:19 +1100 | [diff] [blame] | 256 | *remote = SSH_TUNID_ANY; |
Damien Miller | d27b947 | 2005-12-13 19:29:02 +1100 | [diff] [blame] | 257 | sp = xstrdup(s); |
| 258 | if ((ep = strchr(sp, ':')) == NULL) { |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 259 | free(sp); |
Damien Miller | d27b947 | 2005-12-13 19:29:02 +1100 | [diff] [blame] | 260 | return (a2tun(s, NULL)); |
| 261 | } |
| 262 | ep[0] = '\0'; ep++; |
| 263 | *remote = a2tun(ep, NULL); |
| 264 | tun = a2tun(sp, NULL); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 265 | free(sp); |
Damien Miller | 7b58e80 | 2005-12-13 19:33:19 +1100 | [diff] [blame] | 266 | return (*remote == SSH_TUNID_ERR ? *remote : tun); |
Damien Miller | d27b947 | 2005-12-13 19:29:02 +1100 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | if (strcasecmp(s, "any") == 0) |
Damien Miller | 7b58e80 | 2005-12-13 19:33:19 +1100 | [diff] [blame] | 270 | return (SSH_TUNID_ANY); |
Damien Miller | d27b947 | 2005-12-13 19:29:02 +1100 | [diff] [blame] | 271 | |
Damien Miller | 7b58e80 | 2005-12-13 19:33:19 +1100 | [diff] [blame] | 272 | tun = strtonum(s, 0, SSH_TUNID_MAX, &errstr); |
| 273 | if (errstr != NULL) |
| 274 | return (SSH_TUNID_ERR); |
Damien Miller | d27b947 | 2005-12-13 19:29:02 +1100 | [diff] [blame] | 275 | |
| 276 | return (tun); |
| 277 | } |
| 278 | |
Ben Lindstrom | 1bda4c8 | 2001-06-05 19:59:08 +0000 | [diff] [blame] | 279 | #define SECONDS 1 |
| 280 | #define MINUTES (SECONDS * 60) |
| 281 | #define HOURS (MINUTES * 60) |
| 282 | #define DAYS (HOURS * 24) |
| 283 | #define WEEKS (DAYS * 7) |
| 284 | |
Ben Lindstrom | 4cc240d | 2001-07-04 04:46:56 +0000 | [diff] [blame] | 285 | /* |
| 286 | * Convert a time string into seconds; format is |
| 287 | * a sequence of: |
| 288 | * time[qualifier] |
| 289 | * |
| 290 | * Valid time qualifiers are: |
| 291 | * <none> seconds |
| 292 | * s|S seconds |
| 293 | * m|M minutes |
| 294 | * h|H hours |
| 295 | * d|D days |
| 296 | * w|W weeks |
| 297 | * |
| 298 | * Examples: |
| 299 | * 90m 90 minutes |
| 300 | * 1h30m 90 minutes |
| 301 | * 2d 2 days |
| 302 | * 1w 1 week |
| 303 | * |
| 304 | * Return -1 if time string is invalid. |
| 305 | */ |
| 306 | long |
| 307 | convtime(const char *s) |
Ben Lindstrom | 1bda4c8 | 2001-06-05 19:59:08 +0000 | [diff] [blame] | 308 | { |
dtucker@openbsd.org | f5746b4 | 2017-03-14 00:25:03 +0000 | [diff] [blame] | 309 | long total, secs, multiplier = 1; |
Ben Lindstrom | 1bda4c8 | 2001-06-05 19:59:08 +0000 | [diff] [blame] | 310 | const char *p; |
| 311 | char *endp; |
| 312 | |
| 313 | errno = 0; |
| 314 | total = 0; |
| 315 | p = s; |
| 316 | |
| 317 | if (p == NULL || *p == '\0') |
| 318 | return -1; |
| 319 | |
| 320 | while (*p) { |
| 321 | secs = strtol(p, &endp, 10); |
| 322 | if (p == endp || |
| 323 | (errno == ERANGE && (secs == LONG_MIN || secs == LONG_MAX)) || |
| 324 | secs < 0) |
| 325 | return -1; |
| 326 | |
| 327 | switch (*endp++) { |
| 328 | case '\0': |
| 329 | endp--; |
Damien Miller | 69b7203 | 2006-03-26 14:02:35 +1100 | [diff] [blame] | 330 | break; |
Ben Lindstrom | 1bda4c8 | 2001-06-05 19:59:08 +0000 | [diff] [blame] | 331 | case 's': |
| 332 | case 'S': |
| 333 | break; |
| 334 | case 'm': |
| 335 | case 'M': |
dtucker@openbsd.org | f5746b4 | 2017-03-14 00:25:03 +0000 | [diff] [blame] | 336 | multiplier = MINUTES; |
Ben Lindstrom | 1bda4c8 | 2001-06-05 19:59:08 +0000 | [diff] [blame] | 337 | break; |
| 338 | case 'h': |
| 339 | case 'H': |
dtucker@openbsd.org | f5746b4 | 2017-03-14 00:25:03 +0000 | [diff] [blame] | 340 | multiplier = HOURS; |
Ben Lindstrom | 1bda4c8 | 2001-06-05 19:59:08 +0000 | [diff] [blame] | 341 | break; |
| 342 | case 'd': |
| 343 | case 'D': |
dtucker@openbsd.org | f5746b4 | 2017-03-14 00:25:03 +0000 | [diff] [blame] | 344 | multiplier = DAYS; |
Ben Lindstrom | 1bda4c8 | 2001-06-05 19:59:08 +0000 | [diff] [blame] | 345 | break; |
| 346 | case 'w': |
| 347 | case 'W': |
dtucker@openbsd.org | f5746b4 | 2017-03-14 00:25:03 +0000 | [diff] [blame] | 348 | multiplier = WEEKS; |
Ben Lindstrom | 1bda4c8 | 2001-06-05 19:59:08 +0000 | [diff] [blame] | 349 | break; |
| 350 | default: |
| 351 | return -1; |
| 352 | } |
dtucker@openbsd.org | c6774d2 | 2017-03-14 00:55:37 +0000 | [diff] [blame] | 353 | if (secs >= LONG_MAX / multiplier) |
dtucker@openbsd.org | f5746b4 | 2017-03-14 00:25:03 +0000 | [diff] [blame] | 354 | return -1; |
| 355 | secs *= multiplier; |
dtucker@openbsd.org | c6774d2 | 2017-03-14 00:55:37 +0000 | [diff] [blame] | 356 | if (total >= LONG_MAX - secs) |
dtucker@openbsd.org | f5746b4 | 2017-03-14 00:25:03 +0000 | [diff] [blame] | 357 | return -1; |
Ben Lindstrom | 1bda4c8 | 2001-06-05 19:59:08 +0000 | [diff] [blame] | 358 | total += secs; |
| 359 | if (total < 0) |
| 360 | return -1; |
| 361 | p = endp; |
| 362 | } |
| 363 | |
| 364 | return total; |
| 365 | } |
| 366 | |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 367 | /* |
Darren Tucker | da34553 | 2006-07-10 23:04:19 +1000 | [diff] [blame] | 368 | * Returns a standardized host+port identifier string. |
| 369 | * Caller must free returned string. |
| 370 | */ |
| 371 | char * |
| 372 | put_host_port(const char *host, u_short port) |
| 373 | { |
| 374 | char *hoststr; |
| 375 | |
| 376 | if (port == 0 || port == SSH_DEFAULT_PORT) |
| 377 | return(xstrdup(host)); |
| 378 | if (asprintf(&hoststr, "[%s]:%d", host, (int)port) < 0) |
| 379 | fatal("put_host_port: asprintf: %s", strerror(errno)); |
| 380 | debug3("put_host_port: %s", hoststr); |
| 381 | return hoststr; |
| 382 | } |
| 383 | |
| 384 | /* |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 385 | * Search for next delimiter between hostnames/addresses and ports. |
| 386 | * Argument may be modified (for termination). |
| 387 | * Returns *cp if parsing succeeds. |
| 388 | * *cp is set to the start of the next delimiter, if one was found. |
| 389 | * If this is the last field, *cp is set to NULL. |
| 390 | */ |
| 391 | char * |
| 392 | hpdelim(char **cp) |
| 393 | { |
| 394 | char *s, *old; |
| 395 | |
| 396 | if (cp == NULL || *cp == NULL) |
| 397 | return NULL; |
| 398 | |
| 399 | old = s = *cp; |
| 400 | if (*s == '[') { |
| 401 | if ((s = strchr(s, ']')) == NULL) |
| 402 | return NULL; |
| 403 | else |
| 404 | s++; |
| 405 | } else if ((s = strpbrk(s, ":/")) == NULL) |
| 406 | s = *cp + strlen(*cp); /* skip to end (see first case below) */ |
| 407 | |
| 408 | switch (*s) { |
| 409 | case '\0': |
| 410 | *cp = NULL; /* no more fields*/ |
| 411 | break; |
Darren Tucker | 47eede7 | 2005-03-14 23:08:12 +1100 | [diff] [blame] | 412 | |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 413 | case ':': |
| 414 | case '/': |
| 415 | *s = '\0'; /* terminate */ |
| 416 | *cp = s + 1; |
| 417 | break; |
Darren Tucker | 47eede7 | 2005-03-14 23:08:12 +1100 | [diff] [blame] | 418 | |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 419 | default: |
| 420 | return NULL; |
| 421 | } |
| 422 | |
| 423 | return old; |
| 424 | } |
| 425 | |
Ben Lindstrom | 4529b70 | 2001-05-03 23:39:53 +0000 | [diff] [blame] | 426 | char * |
| 427 | cleanhostname(char *host) |
| 428 | { |
| 429 | if (*host == '[' && host[strlen(host) - 1] == ']') { |
| 430 | host[strlen(host) - 1] = '\0'; |
| 431 | return (host + 1); |
| 432 | } else |
| 433 | return host; |
| 434 | } |
| 435 | |
| 436 | char * |
| 437 | colon(char *cp) |
| 438 | { |
| 439 | int flag = 0; |
| 440 | |
| 441 | if (*cp == ':') /* Leading colon is part of file name. */ |
Damien Miller | 2e77446 | 2010-06-26 09:30:47 +1000 | [diff] [blame] | 442 | return NULL; |
Ben Lindstrom | 4529b70 | 2001-05-03 23:39:53 +0000 | [diff] [blame] | 443 | if (*cp == '[') |
| 444 | flag = 1; |
| 445 | |
| 446 | for (; *cp; ++cp) { |
| 447 | if (*cp == '@' && *(cp+1) == '[') |
| 448 | flag = 1; |
| 449 | if (*cp == ']' && *(cp+1) == ':' && flag) |
| 450 | return (cp+1); |
| 451 | if (*cp == ':' && !flag) |
| 452 | return (cp); |
| 453 | if (*cp == '/') |
Damien Miller | 2e77446 | 2010-06-26 09:30:47 +1000 | [diff] [blame] | 454 | return NULL; |
Ben Lindstrom | 4529b70 | 2001-05-03 23:39:53 +0000 | [diff] [blame] | 455 | } |
Damien Miller | 2e77446 | 2010-06-26 09:30:47 +1000 | [diff] [blame] | 456 | return NULL; |
Ben Lindstrom | 4529b70 | 2001-05-03 23:39:53 +0000 | [diff] [blame] | 457 | } |
| 458 | |
djm@openbsd.org | ed877ef | 2016-07-15 00:24:30 +0000 | [diff] [blame] | 459 | /* |
| 460 | * Parse a [user@]host[:port] string. |
| 461 | * Caller must free returned user and host. |
| 462 | * Any of the pointer return arguments may be NULL (useful for syntax checking). |
| 463 | * If user was not specified then *userp will be set to NULL. |
| 464 | * If port was not specified then *portp will be -1. |
| 465 | * Returns 0 on success, -1 on failure. |
| 466 | */ |
| 467 | int |
| 468 | parse_user_host_port(const char *s, char **userp, char **hostp, int *portp) |
| 469 | { |
| 470 | char *sdup, *cp, *tmp; |
| 471 | char *user = NULL, *host = NULL; |
| 472 | int port = -1, ret = -1; |
| 473 | |
| 474 | if (userp != NULL) |
| 475 | *userp = NULL; |
| 476 | if (hostp != NULL) |
| 477 | *hostp = NULL; |
| 478 | if (portp != NULL) |
| 479 | *portp = -1; |
| 480 | |
| 481 | if ((sdup = tmp = strdup(s)) == NULL) |
| 482 | return -1; |
| 483 | /* Extract optional username */ |
| 484 | if ((cp = strchr(tmp, '@')) != NULL) { |
| 485 | *cp = '\0'; |
| 486 | if (*tmp == '\0') |
| 487 | goto out; |
| 488 | if ((user = strdup(tmp)) == NULL) |
| 489 | goto out; |
| 490 | tmp = cp + 1; |
| 491 | } |
| 492 | /* Extract mandatory hostname */ |
| 493 | if ((cp = hpdelim(&tmp)) == NULL || *cp == '\0') |
| 494 | goto out; |
| 495 | host = xstrdup(cleanhostname(cp)); |
| 496 | /* Convert and verify optional port */ |
| 497 | if (tmp != NULL && *tmp != '\0') { |
| 498 | if ((port = a2port(tmp)) <= 0) |
| 499 | goto out; |
| 500 | } |
| 501 | /* Success */ |
| 502 | if (userp != NULL) { |
| 503 | *userp = user; |
| 504 | user = NULL; |
| 505 | } |
| 506 | if (hostp != NULL) { |
| 507 | *hostp = host; |
| 508 | host = NULL; |
| 509 | } |
| 510 | if (portp != NULL) |
| 511 | *portp = port; |
| 512 | ret = 0; |
| 513 | out: |
| 514 | free(sdup); |
| 515 | free(user); |
| 516 | free(host); |
| 517 | return ret; |
| 518 | } |
| 519 | |
Ben Lindstrom | 4cc240d | 2001-07-04 04:46:56 +0000 | [diff] [blame] | 520 | /* function to assist building execv() arguments */ |
Ben Lindstrom | 387c472 | 2001-05-08 20:27:25 +0000 | [diff] [blame] | 521 | void |
| 522 | addargs(arglist *args, char *fmt, ...) |
| 523 | { |
| 524 | va_list ap; |
Damien Miller | 3eec6b7 | 2006-01-31 21:49:27 +1100 | [diff] [blame] | 525 | char *cp; |
Darren Tucker | c7a6fc4 | 2004-08-13 21:18:00 +1000 | [diff] [blame] | 526 | u_int nalloc; |
Damien Miller | 3eec6b7 | 2006-01-31 21:49:27 +1100 | [diff] [blame] | 527 | int r; |
Ben Lindstrom | 387c472 | 2001-05-08 20:27:25 +0000 | [diff] [blame] | 528 | |
| 529 | va_start(ap, fmt); |
Damien Miller | 3eec6b7 | 2006-01-31 21:49:27 +1100 | [diff] [blame] | 530 | r = vasprintf(&cp, fmt, ap); |
Ben Lindstrom | 387c472 | 2001-05-08 20:27:25 +0000 | [diff] [blame] | 531 | va_end(ap); |
Damien Miller | 3eec6b7 | 2006-01-31 21:49:27 +1100 | [diff] [blame] | 532 | if (r == -1) |
| 533 | fatal("addargs: argument too long"); |
Ben Lindstrom | 387c472 | 2001-05-08 20:27:25 +0000 | [diff] [blame] | 534 | |
Darren Tucker | fb16b24 | 2003-09-22 21:04:23 +1000 | [diff] [blame] | 535 | nalloc = args->nalloc; |
Ben Lindstrom | 387c472 | 2001-05-08 20:27:25 +0000 | [diff] [blame] | 536 | if (args->list == NULL) { |
Darren Tucker | fb16b24 | 2003-09-22 21:04:23 +1000 | [diff] [blame] | 537 | nalloc = 32; |
Ben Lindstrom | 387c472 | 2001-05-08 20:27:25 +0000 | [diff] [blame] | 538 | args->num = 0; |
Darren Tucker | fb16b24 | 2003-09-22 21:04:23 +1000 | [diff] [blame] | 539 | } else if (args->num+2 >= nalloc) |
| 540 | nalloc *= 2; |
Ben Lindstrom | 387c472 | 2001-05-08 20:27:25 +0000 | [diff] [blame] | 541 | |
deraadt@openbsd.org | 657a5fb | 2015-04-24 01:36:00 +0000 | [diff] [blame] | 542 | args->list = xreallocarray(args->list, nalloc, sizeof(char *)); |
Darren Tucker | fb16b24 | 2003-09-22 21:04:23 +1000 | [diff] [blame] | 543 | args->nalloc = nalloc; |
Damien Miller | 3eec6b7 | 2006-01-31 21:49:27 +1100 | [diff] [blame] | 544 | args->list[args->num++] = cp; |
Ben Lindstrom | 387c472 | 2001-05-08 20:27:25 +0000 | [diff] [blame] | 545 | args->list[args->num] = NULL; |
| 546 | } |
Darren Tucker | 22cc741 | 2004-12-06 22:47:41 +1100 | [diff] [blame] | 547 | |
Damien Miller | 3eec6b7 | 2006-01-31 21:49:27 +1100 | [diff] [blame] | 548 | void |
| 549 | replacearg(arglist *args, u_int which, char *fmt, ...) |
| 550 | { |
| 551 | va_list ap; |
| 552 | char *cp; |
| 553 | int r; |
| 554 | |
| 555 | va_start(ap, fmt); |
| 556 | r = vasprintf(&cp, fmt, ap); |
| 557 | va_end(ap); |
| 558 | if (r == -1) |
| 559 | fatal("replacearg: argument too long"); |
| 560 | |
| 561 | if (which >= args->num) |
| 562 | fatal("replacearg: tried to replace invalid arg %d >= %d", |
| 563 | which, args->num); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 564 | free(args->list[which]); |
Damien Miller | 3eec6b7 | 2006-01-31 21:49:27 +1100 | [diff] [blame] | 565 | args->list[which] = cp; |
| 566 | } |
| 567 | |
| 568 | void |
| 569 | freeargs(arglist *args) |
| 570 | { |
| 571 | u_int i; |
| 572 | |
| 573 | if (args->list != NULL) { |
| 574 | for (i = 0; i < args->num; i++) |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 575 | free(args->list[i]); |
| 576 | free(args->list); |
Damien Miller | 3eec6b7 | 2006-01-31 21:49:27 +1100 | [diff] [blame] | 577 | args->nalloc = args->num = 0; |
| 578 | args->list = NULL; |
| 579 | } |
| 580 | } |
| 581 | |
Darren Tucker | 22cc741 | 2004-12-06 22:47:41 +1100 | [diff] [blame] | 582 | /* |
Damien Miller | 5fd38c0 | 2005-05-26 12:02:14 +1000 | [diff] [blame] | 583 | * Expands tildes in the file name. Returns data allocated by xmalloc. |
| 584 | * Warning: this calls getpw*. |
| 585 | */ |
| 586 | char * |
| 587 | tilde_expand_filename(const char *filename, uid_t uid) |
| 588 | { |
Darren Tucker | 2ca51bf | 2013-05-16 20:22:46 +1000 | [diff] [blame] | 589 | const char *path, *sep; |
| 590 | char user[128], *ret; |
Damien Miller | 5fd38c0 | 2005-05-26 12:02:14 +1000 | [diff] [blame] | 591 | struct passwd *pw; |
Damien Miller | eccb9de | 2005-06-17 12:59:34 +1000 | [diff] [blame] | 592 | u_int len, slash; |
Damien Miller | 5fd38c0 | 2005-05-26 12:02:14 +1000 | [diff] [blame] | 593 | |
| 594 | if (*filename != '~') |
| 595 | return (xstrdup(filename)); |
| 596 | filename++; |
| 597 | |
| 598 | path = strchr(filename, '/'); |
| 599 | if (path != NULL && path > filename) { /* ~user/path */ |
Damien Miller | eccb9de | 2005-06-17 12:59:34 +1000 | [diff] [blame] | 600 | slash = path - filename; |
| 601 | if (slash > sizeof(user) - 1) |
Damien Miller | 5fd38c0 | 2005-05-26 12:02:14 +1000 | [diff] [blame] | 602 | fatal("tilde_expand_filename: ~username too long"); |
Damien Miller | eccb9de | 2005-06-17 12:59:34 +1000 | [diff] [blame] | 603 | memcpy(user, filename, slash); |
| 604 | user[slash] = '\0'; |
Damien Miller | 5fd38c0 | 2005-05-26 12:02:14 +1000 | [diff] [blame] | 605 | if ((pw = getpwnam(user)) == NULL) |
| 606 | fatal("tilde_expand_filename: No such user %s", user); |
| 607 | } else if ((pw = getpwuid(uid)) == NULL) /* ~/path */ |
Darren Tucker | 7517b5b | 2008-06-13 14:48:59 +1000 | [diff] [blame] | 608 | fatal("tilde_expand_filename: No such uid %ld", (long)uid); |
Damien Miller | 5fd38c0 | 2005-05-26 12:02:14 +1000 | [diff] [blame] | 609 | |
Damien Miller | 5fd38c0 | 2005-05-26 12:02:14 +1000 | [diff] [blame] | 610 | /* Make sure directory has a trailing '/' */ |
| 611 | len = strlen(pw->pw_dir); |
Darren Tucker | 026d9db | 2013-05-16 20:23:52 +1000 | [diff] [blame] | 612 | if (len == 0 || pw->pw_dir[len - 1] != '/') |
Darren Tucker | 2ca51bf | 2013-05-16 20:22:46 +1000 | [diff] [blame] | 613 | sep = "/"; |
| 614 | else |
| 615 | sep = ""; |
Damien Miller | 5fd38c0 | 2005-05-26 12:02:14 +1000 | [diff] [blame] | 616 | |
| 617 | /* Skip leading '/' from specified path */ |
| 618 | if (path != NULL) |
| 619 | filename = path + 1; |
Darren Tucker | 2ca51bf | 2013-05-16 20:22:46 +1000 | [diff] [blame] | 620 | |
deraadt@openbsd.org | 2ae4f33 | 2015-01-16 06:40:12 +0000 | [diff] [blame] | 621 | 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] | 622 | fatal("tilde_expand_filename: Path too long"); |
| 623 | |
Darren Tucker | 2ca51bf | 2013-05-16 20:22:46 +1000 | [diff] [blame] | 624 | return (ret); |
Damien Miller | 5fd38c0 | 2005-05-26 12:02:14 +1000 | [diff] [blame] | 625 | } |
| 626 | |
| 627 | /* |
Damien Miller | 6476cad | 2005-06-16 13:18:34 +1000 | [diff] [blame] | 628 | * Expand a string with a set of %[char] escapes. A number of escapes may be |
| 629 | * specified as (char *escape_chars, char *replacement) pairs. The list must |
Darren Tucker | bee73d5 | 2005-07-14 17:05:02 +1000 | [diff] [blame] | 630 | * be terminated by a NULL escape_char. Returns replaced string in memory |
Damien Miller | 6476cad | 2005-06-16 13:18:34 +1000 | [diff] [blame] | 631 | * allocated by xmalloc. |
| 632 | */ |
| 633 | char * |
| 634 | percent_expand(const char *string, ...) |
| 635 | { |
| 636 | #define EXPAND_MAX_KEYS 16 |
Darren Tucker | 70d8769 | 2010-01-08 18:49:16 +1100 | [diff] [blame] | 637 | u_int num_keys, i, j; |
Damien Miller | 6476cad | 2005-06-16 13:18:34 +1000 | [diff] [blame] | 638 | struct { |
| 639 | const char *key; |
| 640 | const char *repl; |
| 641 | } keys[EXPAND_MAX_KEYS]; |
Damien Miller | 6476cad | 2005-06-16 13:18:34 +1000 | [diff] [blame] | 642 | char buf[4096]; |
| 643 | va_list ap; |
| 644 | |
| 645 | /* Gather keys */ |
| 646 | va_start(ap, string); |
| 647 | for (num_keys = 0; num_keys < EXPAND_MAX_KEYS; num_keys++) { |
| 648 | keys[num_keys].key = va_arg(ap, char *); |
| 649 | if (keys[num_keys].key == NULL) |
| 650 | break; |
| 651 | keys[num_keys].repl = va_arg(ap, char *); |
| 652 | if (keys[num_keys].repl == NULL) |
Darren Tucker | 70d8769 | 2010-01-08 18:49:16 +1100 | [diff] [blame] | 653 | fatal("%s: NULL replacement", __func__); |
Damien Miller | 6476cad | 2005-06-16 13:18:34 +1000 | [diff] [blame] | 654 | } |
Darren Tucker | 70d8769 | 2010-01-08 18:49:16 +1100 | [diff] [blame] | 655 | if (num_keys == EXPAND_MAX_KEYS && va_arg(ap, char *) != NULL) |
| 656 | fatal("%s: too many keys", __func__); |
Damien Miller | 6476cad | 2005-06-16 13:18:34 +1000 | [diff] [blame] | 657 | va_end(ap); |
| 658 | |
Damien Miller | 6476cad | 2005-06-16 13:18:34 +1000 | [diff] [blame] | 659 | /* Expand string */ |
| 660 | *buf = '\0'; |
| 661 | for (i = 0; *string != '\0'; string++) { |
| 662 | if (*string != '%') { |
| 663 | append: |
| 664 | buf[i++] = *string; |
| 665 | if (i >= sizeof(buf)) |
Darren Tucker | 70d8769 | 2010-01-08 18:49:16 +1100 | [diff] [blame] | 666 | fatal("%s: string too long", __func__); |
Damien Miller | 6476cad | 2005-06-16 13:18:34 +1000 | [diff] [blame] | 667 | buf[i] = '\0'; |
| 668 | continue; |
| 669 | } |
| 670 | string++; |
Darren Tucker | 70d8769 | 2010-01-08 18:49:16 +1100 | [diff] [blame] | 671 | /* %% case */ |
Damien Miller | 6476cad | 2005-06-16 13:18:34 +1000 | [diff] [blame] | 672 | if (*string == '%') |
| 673 | goto append; |
tobias@openbsd.org | 8b56e59 | 2015-12-04 21:51:06 +0000 | [diff] [blame] | 674 | if (*string == '\0') |
| 675 | fatal("%s: invalid format", __func__); |
Damien Miller | 6476cad | 2005-06-16 13:18:34 +1000 | [diff] [blame] | 676 | for (j = 0; j < num_keys; j++) { |
| 677 | if (strchr(keys[j].key, *string) != NULL) { |
| 678 | i = strlcat(buf, keys[j].repl, sizeof(buf)); |
| 679 | if (i >= sizeof(buf)) |
Darren Tucker | 70d8769 | 2010-01-08 18:49:16 +1100 | [diff] [blame] | 680 | fatal("%s: string too long", __func__); |
Damien Miller | 6476cad | 2005-06-16 13:18:34 +1000 | [diff] [blame] | 681 | break; |
| 682 | } |
| 683 | } |
| 684 | if (j >= num_keys) |
Darren Tucker | 70d8769 | 2010-01-08 18:49:16 +1100 | [diff] [blame] | 685 | fatal("%s: unknown key %%%c", __func__, *string); |
Damien Miller | 6476cad | 2005-06-16 13:18:34 +1000 | [diff] [blame] | 686 | } |
| 687 | return (xstrdup(buf)); |
| 688 | #undef EXPAND_MAX_KEYS |
| 689 | } |
| 690 | |
| 691 | /* |
Darren Tucker | 22cc741 | 2004-12-06 22:47:41 +1100 | [diff] [blame] | 692 | * Read an entire line from a public key file into a static buffer, discarding |
| 693 | * lines that exceed the buffer size. Returns 0 on success, -1 on failure. |
| 694 | */ |
| 695 | int |
| 696 | read_keyfile_line(FILE *f, const char *filename, char *buf, size_t bufsz, |
Darren Tucker | f0f9098 | 2004-12-11 13:39:50 +1100 | [diff] [blame] | 697 | u_long *lineno) |
Darren Tucker | 22cc741 | 2004-12-06 22:47:41 +1100 | [diff] [blame] | 698 | { |
| 699 | while (fgets(buf, bufsz, f) != NULL) { |
Damien Miller | 3ca8b77 | 2007-01-05 16:24:47 +1100 | [diff] [blame] | 700 | if (buf[0] == '\0') |
| 701 | continue; |
Darren Tucker | 22cc741 | 2004-12-06 22:47:41 +1100 | [diff] [blame] | 702 | (*lineno)++; |
| 703 | if (buf[strlen(buf) - 1] == '\n' || feof(f)) { |
| 704 | return 0; |
| 705 | } else { |
Darren Tucker | f0f9098 | 2004-12-11 13:39:50 +1100 | [diff] [blame] | 706 | debug("%s: %s line %lu exceeds size limit", __func__, |
| 707 | filename, *lineno); |
Darren Tucker | 22cc741 | 2004-12-06 22:47:41 +1100 | [diff] [blame] | 708 | /* discard remainder of line */ |
Darren Tucker | 47eede7 | 2005-03-14 23:08:12 +1100 | [diff] [blame] | 709 | while (fgetc(f) != '\n' && !feof(f)) |
Darren Tucker | 22cc741 | 2004-12-06 22:47:41 +1100 | [diff] [blame] | 710 | ; /* nothing */ |
| 711 | } |
| 712 | } |
| 713 | return -1; |
| 714 | } |
Damien Miller | 1339002 | 2005-07-06 09:44:19 +1000 | [diff] [blame] | 715 | |
Damien Miller | d27b947 | 2005-12-13 19:29:02 +1100 | [diff] [blame] | 716 | int |
Damien Miller | 7b58e80 | 2005-12-13 19:33:19 +1100 | [diff] [blame] | 717 | tun_open(int tun, int mode) |
Damien Miller | d27b947 | 2005-12-13 19:29:02 +1100 | [diff] [blame] | 718 | { |
Damien Miller | 62a31c9 | 2005-12-13 20:44:13 +1100 | [diff] [blame] | 719 | #if defined(CUSTOM_SYS_TUN_OPEN) |
| 720 | return (sys_tun_open(tun, mode)); |
Damien Miller | 2dcddbf | 2006-01-01 19:47:05 +1100 | [diff] [blame] | 721 | #elif defined(SSH_TUN_OPENBSD) |
Damien Miller | 7b58e80 | 2005-12-13 19:33:19 +1100 | [diff] [blame] | 722 | struct ifreq ifr; |
Damien Miller | d27b947 | 2005-12-13 19:29:02 +1100 | [diff] [blame] | 723 | char name[100]; |
Damien Miller | 7b58e80 | 2005-12-13 19:33:19 +1100 | [diff] [blame] | 724 | int fd = -1, sock; |
sthen@openbsd.org | a820a86 | 2015-10-24 08:34:09 +0000 | [diff] [blame] | 725 | const char *tunbase = "tun"; |
| 726 | |
| 727 | if (mode == SSH_TUNMODE_ETHERNET) |
| 728 | tunbase = "tap"; |
Damien Miller | d27b947 | 2005-12-13 19:29:02 +1100 | [diff] [blame] | 729 | |
Damien Miller | 7b58e80 | 2005-12-13 19:33:19 +1100 | [diff] [blame] | 730 | /* Open the tunnel device */ |
| 731 | if (tun <= SSH_TUNID_MAX) { |
sthen@openbsd.org | a820a86 | 2015-10-24 08:34:09 +0000 | [diff] [blame] | 732 | snprintf(name, sizeof(name), "/dev/%s%d", tunbase, tun); |
Damien Miller | 7b58e80 | 2005-12-13 19:33:19 +1100 | [diff] [blame] | 733 | fd = open(name, O_RDWR); |
| 734 | } else if (tun == SSH_TUNID_ANY) { |
| 735 | for (tun = 100; tun >= 0; tun--) { |
sthen@openbsd.org | a820a86 | 2015-10-24 08:34:09 +0000 | [diff] [blame] | 736 | snprintf(name, sizeof(name), "/dev/%s%d", |
| 737 | tunbase, tun); |
Damien Miller | 7b58e80 | 2005-12-13 19:33:19 +1100 | [diff] [blame] | 738 | if ((fd = open(name, O_RDWR)) >= 0) |
| 739 | break; |
Damien Miller | d27b947 | 2005-12-13 19:29:02 +1100 | [diff] [blame] | 740 | } |
| 741 | } else { |
Damien Miller | a210d52 | 2006-01-02 23:40:30 +1100 | [diff] [blame] | 742 | debug("%s: invalid tunnel %u", __func__, tun); |
djm@openbsd.org | 2905d6f | 2015-10-07 00:54:06 +0000 | [diff] [blame] | 743 | return -1; |
Damien Miller | d27b947 | 2005-12-13 19:29:02 +1100 | [diff] [blame] | 744 | } |
Damien Miller | 7b58e80 | 2005-12-13 19:33:19 +1100 | [diff] [blame] | 745 | |
| 746 | if (fd < 0) { |
djm@openbsd.org | 2905d6f | 2015-10-07 00:54:06 +0000 | [diff] [blame] | 747 | debug("%s: %s open: %s", __func__, name, strerror(errno)); |
| 748 | return -1; |
Damien Miller | 7b58e80 | 2005-12-13 19:33:19 +1100 | [diff] [blame] | 749 | } |
| 750 | |
| 751 | debug("%s: %s mode %d fd %d", __func__, name, mode, fd); |
| 752 | |
sthen@openbsd.org | a820a86 | 2015-10-24 08:34:09 +0000 | [diff] [blame] | 753 | /* Bring interface up if it is not already */ |
| 754 | snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "%s%d", tunbase, tun); |
| 755 | if ((sock = socket(PF_UNIX, SOCK_STREAM, 0)) == -1) |
Damien Miller | 7b58e80 | 2005-12-13 19:33:19 +1100 | [diff] [blame] | 756 | goto failed; |
| 757 | |
djm@openbsd.org | 2905d6f | 2015-10-07 00:54:06 +0000 | [diff] [blame] | 758 | if (ioctl(sock, SIOCGIFFLAGS, &ifr) == -1) { |
| 759 | debug("%s: get interface %s flags: %s", __func__, |
| 760 | ifr.ifr_name, strerror(errno)); |
Damien Miller | 7b58e80 | 2005-12-13 19:33:19 +1100 | [diff] [blame] | 761 | goto failed; |
djm@openbsd.org | 2905d6f | 2015-10-07 00:54:06 +0000 | [diff] [blame] | 762 | } |
Damien Miller | a1d9a18 | 2006-01-02 23:41:21 +1100 | [diff] [blame] | 763 | |
djm@openbsd.org | 2905d6f | 2015-10-07 00:54:06 +0000 | [diff] [blame] | 764 | if (!(ifr.ifr_flags & IFF_UP)) { |
| 765 | ifr.ifr_flags |= IFF_UP; |
| 766 | if (ioctl(sock, SIOCSIFFLAGS, &ifr) == -1) { |
| 767 | debug("%s: activate interface %s: %s", __func__, |
| 768 | ifr.ifr_name, strerror(errno)); |
| 769 | goto failed; |
| 770 | } |
| 771 | } |
Damien Miller | 7b58e80 | 2005-12-13 19:33:19 +1100 | [diff] [blame] | 772 | |
| 773 | close(sock); |
djm@openbsd.org | 2905d6f | 2015-10-07 00:54:06 +0000 | [diff] [blame] | 774 | return fd; |
Damien Miller | 7b58e80 | 2005-12-13 19:33:19 +1100 | [diff] [blame] | 775 | |
| 776 | failed: |
| 777 | if (fd >= 0) |
| 778 | close(fd); |
| 779 | if (sock >= 0) |
| 780 | close(sock); |
djm@openbsd.org | 2905d6f | 2015-10-07 00:54:06 +0000 | [diff] [blame] | 781 | return -1; |
Damien Miller | 62a31c9 | 2005-12-13 20:44:13 +1100 | [diff] [blame] | 782 | #else |
| 783 | error("Tunnel interfaces are not supported on this platform"); |
| 784 | return (-1); |
| 785 | #endif |
Damien Miller | d27b947 | 2005-12-13 19:29:02 +1100 | [diff] [blame] | 786 | } |
| 787 | |
Darren Tucker | ce321d8 | 2005-10-03 18:11:24 +1000 | [diff] [blame] | 788 | void |
| 789 | sanitise_stdfd(void) |
| 790 | { |
Damien Miller | 72c5b7d | 2006-01-06 14:50:44 +1100 | [diff] [blame] | 791 | int nullfd, dupfd; |
Darren Tucker | ce321d8 | 2005-10-03 18:11:24 +1000 | [diff] [blame] | 792 | |
Damien Miller | 72c5b7d | 2006-01-06 14:50:44 +1100 | [diff] [blame] | 793 | if ((nullfd = dupfd = open(_PATH_DEVNULL, O_RDWR)) == -1) { |
Damien Miller | 9eab956 | 2009-02-22 08:47:02 +1100 | [diff] [blame] | 794 | fprintf(stderr, "Couldn't open /dev/null: %s\n", |
| 795 | strerror(errno)); |
Darren Tucker | ce321d8 | 2005-10-03 18:11:24 +1000 | [diff] [blame] | 796 | exit(1); |
| 797 | } |
krw@openbsd.org | 574def0 | 2016-04-02 14:37:42 +0000 | [diff] [blame] | 798 | while (++dupfd <= STDERR_FILENO) { |
| 799 | /* Only populate closed fds. */ |
| 800 | if (fcntl(dupfd, F_GETFL) == -1 && errno == EBADF) { |
| 801 | if (dup2(nullfd, dupfd) == -1) { |
| 802 | fprintf(stderr, "dup2: %s\n", strerror(errno)); |
| 803 | exit(1); |
| 804 | } |
Darren Tucker | ce321d8 | 2005-10-03 18:11:24 +1000 | [diff] [blame] | 805 | } |
Darren Tucker | ce321d8 | 2005-10-03 18:11:24 +1000 | [diff] [blame] | 806 | } |
krw@openbsd.org | 574def0 | 2016-04-02 14:37:42 +0000 | [diff] [blame] | 807 | if (nullfd > STDERR_FILENO) |
Darren Tucker | ce321d8 | 2005-10-03 18:11:24 +1000 | [diff] [blame] | 808 | close(nullfd); |
| 809 | } |
| 810 | |
Damien Miller | 1339002 | 2005-07-06 09:44:19 +1000 | [diff] [blame] | 811 | char * |
Damien Miller | 3f94188 | 2006-03-31 23:13:02 +1100 | [diff] [blame] | 812 | tohex(const void *vp, size_t l) |
Damien Miller | 1339002 | 2005-07-06 09:44:19 +1000 | [diff] [blame] | 813 | { |
Damien Miller | 3f94188 | 2006-03-31 23:13:02 +1100 | [diff] [blame] | 814 | const u_char *p = (const u_char *)vp; |
Damien Miller | 1339002 | 2005-07-06 09:44:19 +1000 | [diff] [blame] | 815 | char b[3], *r; |
Damien Miller | 3f94188 | 2006-03-31 23:13:02 +1100 | [diff] [blame] | 816 | size_t i, hl; |
| 817 | |
| 818 | if (l > 65536) |
| 819 | return xstrdup("tohex: length > 65536"); |
Damien Miller | 1339002 | 2005-07-06 09:44:19 +1000 | [diff] [blame] | 820 | |
| 821 | hl = l * 2 + 1; |
Damien Miller | 07d86be | 2006-03-26 14:19:21 +1100 | [diff] [blame] | 822 | r = xcalloc(1, hl); |
Damien Miller | 1339002 | 2005-07-06 09:44:19 +1000 | [diff] [blame] | 823 | for (i = 0; i < l; i++) { |
Damien Miller | 3f94188 | 2006-03-31 23:13:02 +1100 | [diff] [blame] | 824 | snprintf(b, sizeof(b), "%02x", p[i]); |
Damien Miller | 1339002 | 2005-07-06 09:44:19 +1000 | [diff] [blame] | 825 | strlcat(r, b, hl); |
| 826 | } |
| 827 | return (r); |
| 828 | } |
| 829 | |
Damien Miller | 3f94188 | 2006-03-31 23:13:02 +1100 | [diff] [blame] | 830 | u_int64_t |
| 831 | get_u64(const void *vp) |
| 832 | { |
| 833 | const u_char *p = (const u_char *)vp; |
| 834 | u_int64_t v; |
| 835 | |
| 836 | v = (u_int64_t)p[0] << 56; |
| 837 | v |= (u_int64_t)p[1] << 48; |
| 838 | v |= (u_int64_t)p[2] << 40; |
| 839 | v |= (u_int64_t)p[3] << 32; |
| 840 | v |= (u_int64_t)p[4] << 24; |
| 841 | v |= (u_int64_t)p[5] << 16; |
| 842 | v |= (u_int64_t)p[6] << 8; |
| 843 | v |= (u_int64_t)p[7]; |
| 844 | |
| 845 | return (v); |
| 846 | } |
| 847 | |
| 848 | u_int32_t |
| 849 | get_u32(const void *vp) |
| 850 | { |
| 851 | const u_char *p = (const u_char *)vp; |
| 852 | u_int32_t v; |
| 853 | |
| 854 | v = (u_int32_t)p[0] << 24; |
| 855 | v |= (u_int32_t)p[1] << 16; |
| 856 | v |= (u_int32_t)p[2] << 8; |
| 857 | v |= (u_int32_t)p[3]; |
| 858 | |
| 859 | return (v); |
| 860 | } |
| 861 | |
Damien Miller | 8885669 | 2014-04-20 13:33:19 +1000 | [diff] [blame] | 862 | u_int32_t |
| 863 | get_u32_le(const void *vp) |
| 864 | { |
| 865 | const u_char *p = (const u_char *)vp; |
| 866 | u_int32_t v; |
| 867 | |
| 868 | v = (u_int32_t)p[0]; |
| 869 | v |= (u_int32_t)p[1] << 8; |
| 870 | v |= (u_int32_t)p[2] << 16; |
| 871 | v |= (u_int32_t)p[3] << 24; |
| 872 | |
| 873 | return (v); |
| 874 | } |
| 875 | |
Damien Miller | 3f94188 | 2006-03-31 23:13:02 +1100 | [diff] [blame] | 876 | u_int16_t |
| 877 | get_u16(const void *vp) |
| 878 | { |
| 879 | const u_char *p = (const u_char *)vp; |
| 880 | u_int16_t v; |
| 881 | |
| 882 | v = (u_int16_t)p[0] << 8; |
| 883 | v |= (u_int16_t)p[1]; |
| 884 | |
| 885 | return (v); |
| 886 | } |
| 887 | |
| 888 | void |
| 889 | put_u64(void *vp, u_int64_t v) |
| 890 | { |
| 891 | u_char *p = (u_char *)vp; |
| 892 | |
| 893 | p[0] = (u_char)(v >> 56) & 0xff; |
| 894 | p[1] = (u_char)(v >> 48) & 0xff; |
| 895 | p[2] = (u_char)(v >> 40) & 0xff; |
| 896 | p[3] = (u_char)(v >> 32) & 0xff; |
| 897 | p[4] = (u_char)(v >> 24) & 0xff; |
| 898 | p[5] = (u_char)(v >> 16) & 0xff; |
| 899 | p[6] = (u_char)(v >> 8) & 0xff; |
| 900 | p[7] = (u_char)v & 0xff; |
| 901 | } |
| 902 | |
| 903 | void |
| 904 | put_u32(void *vp, u_int32_t v) |
| 905 | { |
| 906 | u_char *p = (u_char *)vp; |
| 907 | |
| 908 | p[0] = (u_char)(v >> 24) & 0xff; |
| 909 | p[1] = (u_char)(v >> 16) & 0xff; |
| 910 | p[2] = (u_char)(v >> 8) & 0xff; |
| 911 | p[3] = (u_char)v & 0xff; |
| 912 | } |
| 913 | |
Damien Miller | 8885669 | 2014-04-20 13:33:19 +1000 | [diff] [blame] | 914 | void |
| 915 | put_u32_le(void *vp, u_int32_t v) |
| 916 | { |
| 917 | u_char *p = (u_char *)vp; |
| 918 | |
| 919 | p[0] = (u_char)v & 0xff; |
| 920 | p[1] = (u_char)(v >> 8) & 0xff; |
| 921 | p[2] = (u_char)(v >> 16) & 0xff; |
| 922 | p[3] = (u_char)(v >> 24) & 0xff; |
| 923 | } |
Damien Miller | 3f94188 | 2006-03-31 23:13:02 +1100 | [diff] [blame] | 924 | |
| 925 | void |
| 926 | put_u16(void *vp, u_int16_t v) |
| 927 | { |
| 928 | u_char *p = (u_char *)vp; |
| 929 | |
| 930 | p[0] = (u_char)(v >> 8) & 0xff; |
| 931 | p[1] = (u_char)v & 0xff; |
| 932 | } |
Darren Tucker | 3fc464e | 2008-06-13 06:42:45 +1000 | [diff] [blame] | 933 | |
| 934 | void |
| 935 | ms_subtract_diff(struct timeval *start, int *ms) |
| 936 | { |
| 937 | struct timeval diff, finish; |
| 938 | |
| 939 | gettimeofday(&finish, NULL); |
| 940 | timersub(&finish, start, &diff); |
| 941 | *ms -= (diff.tv_sec * 1000) + (diff.tv_usec / 1000); |
| 942 | } |
| 943 | |
| 944 | void |
| 945 | ms_to_timeval(struct timeval *tv, int ms) |
| 946 | { |
| 947 | if (ms < 0) |
| 948 | ms = 0; |
| 949 | tv->tv_sec = ms / 1000; |
| 950 | tv->tv_usec = (ms % 1000) * 1000; |
| 951 | } |
| 952 | |
Darren Tucker | b759c9c | 2013-06-02 07:46:16 +1000 | [diff] [blame] | 953 | time_t |
| 954 | monotime(void) |
| 955 | { |
Damien Miller | 795b863 | 2014-05-21 17:12:53 +1000 | [diff] [blame] | 956 | #if defined(HAVE_CLOCK_GETTIME) && \ |
| 957 | (defined(CLOCK_MONOTONIC) || defined(CLOCK_BOOTTIME)) |
Darren Tucker | b759c9c | 2013-06-02 07:46:16 +1000 | [diff] [blame] | 958 | struct timespec ts; |
Darren Tucker | 94396b7 | 2013-08-08 11:52:37 +1000 | [diff] [blame] | 959 | static int gettime_failed = 0; |
Darren Tucker | b759c9c | 2013-06-02 07:46:16 +1000 | [diff] [blame] | 960 | |
Darren Tucker | 94396b7 | 2013-08-08 11:52:37 +1000 | [diff] [blame] | 961 | if (!gettime_failed) { |
Damien Miller | 795b863 | 2014-05-21 17:12:53 +1000 | [diff] [blame] | 962 | #if defined(CLOCK_BOOTTIME) |
| 963 | if (clock_gettime(CLOCK_BOOTTIME, &ts) == 0) |
| 964 | return (ts.tv_sec); |
| 965 | #endif |
| 966 | #if defined(CLOCK_MONOTONIC) |
Darren Tucker | 94396b7 | 2013-08-08 11:52:37 +1000 | [diff] [blame] | 967 | if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0) |
| 968 | return (ts.tv_sec); |
Damien Miller | 795b863 | 2014-05-21 17:12:53 +1000 | [diff] [blame] | 969 | #endif |
Darren Tucker | 94396b7 | 2013-08-08 11:52:37 +1000 | [diff] [blame] | 970 | debug3("clock_gettime: %s", strerror(errno)); |
| 971 | gettime_failed = 1; |
| 972 | } |
Damien Miller | 795b863 | 2014-05-21 17:12:53 +1000 | [diff] [blame] | 973 | #endif /* HAVE_CLOCK_GETTIME && (CLOCK_MONOTONIC || CLOCK_BOOTTIME */ |
Darren Tucker | 94396b7 | 2013-08-08 11:52:37 +1000 | [diff] [blame] | 974 | |
| 975 | return time(NULL); |
Darren Tucker | b759c9c | 2013-06-02 07:46:16 +1000 | [diff] [blame] | 976 | } |
| 977 | |
dtucker@openbsd.org | b8d4eaf | 2016-03-02 22:42:40 +0000 | [diff] [blame] | 978 | double |
| 979 | monotime_double(void) |
| 980 | { |
Darren Tucker | 2c48bd3 | 2016-03-09 12:46:50 +1100 | [diff] [blame] | 981 | #if defined(HAVE_CLOCK_GETTIME) && \ |
| 982 | (defined(CLOCK_MONOTONIC) || defined(CLOCK_BOOTTIME)) |
dtucker@openbsd.org | b8d4eaf | 2016-03-02 22:42:40 +0000 | [diff] [blame] | 983 | struct timespec ts; |
Darren Tucker | 2c48bd3 | 2016-03-09 12:46:50 +1100 | [diff] [blame] | 984 | static int gettime_failed = 0; |
dtucker@openbsd.org | b8d4eaf | 2016-03-02 22:42:40 +0000 | [diff] [blame] | 985 | |
Darren Tucker | 2c48bd3 | 2016-03-09 12:46:50 +1100 | [diff] [blame] | 986 | if (!gettime_failed) { |
| 987 | #if defined(CLOCK_BOOTTIME) |
| 988 | if (clock_gettime(CLOCK_BOOTTIME, &ts) == 0) |
| 989 | return (ts.tv_sec + (double)ts.tv_nsec / 1000000000); |
| 990 | #endif |
| 991 | #if defined(CLOCK_MONOTONIC) |
| 992 | if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0) |
| 993 | return (ts.tv_sec + (double)ts.tv_nsec / 1000000000); |
| 994 | #endif |
| 995 | debug3("clock_gettime: %s", strerror(errno)); |
| 996 | gettime_failed = 1; |
| 997 | } |
| 998 | #endif /* HAVE_CLOCK_GETTIME && (CLOCK_MONOTONIC || CLOCK_BOOTTIME */ |
dtucker@openbsd.org | b8d4eaf | 2016-03-02 22:42:40 +0000 | [diff] [blame] | 999 | |
Darren Tucker | 2c48bd3 | 2016-03-09 12:46:50 +1100 | [diff] [blame] | 1000 | return (double)time(NULL); |
dtucker@openbsd.org | b8d4eaf | 2016-03-02 22:42:40 +0000 | [diff] [blame] | 1001 | } |
| 1002 | |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 1003 | void |
| 1004 | bandwidth_limit_init(struct bwlimit *bw, u_int64_t kbps, size_t buflen) |
| 1005 | { |
| 1006 | bw->buflen = buflen; |
| 1007 | bw->rate = kbps; |
| 1008 | bw->thresh = bw->rate; |
| 1009 | bw->lamt = 0; |
| 1010 | timerclear(&bw->bwstart); |
| 1011 | timerclear(&bw->bwend); |
| 1012 | } |
| 1013 | |
| 1014 | /* Callback from read/write loop to insert bandwidth-limiting delays */ |
| 1015 | void |
| 1016 | bandwidth_limit(struct bwlimit *bw, size_t read_len) |
| 1017 | { |
| 1018 | u_int64_t waitlen; |
| 1019 | struct timespec ts, rm; |
| 1020 | |
| 1021 | if (!timerisset(&bw->bwstart)) { |
| 1022 | gettimeofday(&bw->bwstart, NULL); |
| 1023 | return; |
| 1024 | } |
| 1025 | |
| 1026 | bw->lamt += read_len; |
| 1027 | if (bw->lamt < bw->thresh) |
| 1028 | return; |
| 1029 | |
| 1030 | gettimeofday(&bw->bwend, NULL); |
| 1031 | timersub(&bw->bwend, &bw->bwstart, &bw->bwend); |
| 1032 | if (!timerisset(&bw->bwend)) |
| 1033 | return; |
| 1034 | |
| 1035 | bw->lamt *= 8; |
| 1036 | waitlen = (double)1000000L * bw->lamt / bw->rate; |
| 1037 | |
| 1038 | bw->bwstart.tv_sec = waitlen / 1000000L; |
| 1039 | bw->bwstart.tv_usec = waitlen % 1000000L; |
| 1040 | |
| 1041 | if (timercmp(&bw->bwstart, &bw->bwend, >)) { |
| 1042 | timersub(&bw->bwstart, &bw->bwend, &bw->bwend); |
| 1043 | |
| 1044 | /* Adjust the wait time */ |
| 1045 | if (bw->bwend.tv_sec) { |
| 1046 | bw->thresh /= 2; |
| 1047 | if (bw->thresh < bw->buflen / 4) |
| 1048 | bw->thresh = bw->buflen / 4; |
| 1049 | } else if (bw->bwend.tv_usec < 10000) { |
| 1050 | bw->thresh *= 2; |
| 1051 | if (bw->thresh > bw->buflen * 8) |
| 1052 | bw->thresh = bw->buflen * 8; |
| 1053 | } |
| 1054 | |
| 1055 | TIMEVAL_TO_TIMESPEC(&bw->bwend, &ts); |
| 1056 | while (nanosleep(&ts, &rm) == -1) { |
| 1057 | if (errno != EINTR) |
| 1058 | break; |
| 1059 | ts = rm; |
| 1060 | } |
| 1061 | } |
| 1062 | |
| 1063 | bw->lamt = 0; |
| 1064 | gettimeofday(&bw->bwstart, NULL); |
| 1065 | } |
Damien Miller | 0dac6fb | 2010-11-20 15:19:38 +1100 | [diff] [blame] | 1066 | |
Damien Miller | 2cd6293 | 2010-12-01 11:50:35 +1100 | [diff] [blame] | 1067 | /* Make a template filename for mk[sd]temp() */ |
| 1068 | void |
| 1069 | mktemp_proto(char *s, size_t len) |
| 1070 | { |
| 1071 | const char *tmpdir; |
| 1072 | int r; |
| 1073 | |
| 1074 | if ((tmpdir = getenv("TMPDIR")) != NULL) { |
| 1075 | r = snprintf(s, len, "%s/ssh-XXXXXXXXXXXX", tmpdir); |
| 1076 | if (r > 0 && (size_t)r < len) |
| 1077 | return; |
| 1078 | } |
| 1079 | r = snprintf(s, len, "/tmp/ssh-XXXXXXXXXXXX"); |
| 1080 | if (r < 0 || (size_t)r >= len) |
| 1081 | fatal("%s: template string too short", __func__); |
| 1082 | } |
| 1083 | |
Damien Miller | 0dac6fb | 2010-11-20 15:19:38 +1100 | [diff] [blame] | 1084 | static const struct { |
| 1085 | const char *name; |
| 1086 | int value; |
| 1087 | } ipqos[] = { |
| 1088 | { "af11", IPTOS_DSCP_AF11 }, |
| 1089 | { "af12", IPTOS_DSCP_AF12 }, |
| 1090 | { "af13", IPTOS_DSCP_AF13 }, |
Damien Miller | 2918e03 | 2011-09-22 21:34:35 +1000 | [diff] [blame] | 1091 | { "af21", IPTOS_DSCP_AF21 }, |
Damien Miller | 0dac6fb | 2010-11-20 15:19:38 +1100 | [diff] [blame] | 1092 | { "af22", IPTOS_DSCP_AF22 }, |
| 1093 | { "af23", IPTOS_DSCP_AF23 }, |
| 1094 | { "af31", IPTOS_DSCP_AF31 }, |
| 1095 | { "af32", IPTOS_DSCP_AF32 }, |
| 1096 | { "af33", IPTOS_DSCP_AF33 }, |
| 1097 | { "af41", IPTOS_DSCP_AF41 }, |
| 1098 | { "af42", IPTOS_DSCP_AF42 }, |
| 1099 | { "af43", IPTOS_DSCP_AF43 }, |
| 1100 | { "cs0", IPTOS_DSCP_CS0 }, |
| 1101 | { "cs1", IPTOS_DSCP_CS1 }, |
| 1102 | { "cs2", IPTOS_DSCP_CS2 }, |
| 1103 | { "cs3", IPTOS_DSCP_CS3 }, |
| 1104 | { "cs4", IPTOS_DSCP_CS4 }, |
| 1105 | { "cs5", IPTOS_DSCP_CS5 }, |
| 1106 | { "cs6", IPTOS_DSCP_CS6 }, |
| 1107 | { "cs7", IPTOS_DSCP_CS7 }, |
| 1108 | { "ef", IPTOS_DSCP_EF }, |
| 1109 | { "lowdelay", IPTOS_LOWDELAY }, |
| 1110 | { "throughput", IPTOS_THROUGHPUT }, |
| 1111 | { "reliability", IPTOS_RELIABILITY }, |
| 1112 | { NULL, -1 } |
| 1113 | }; |
| 1114 | |
| 1115 | int |
| 1116 | parse_ipqos(const char *cp) |
| 1117 | { |
| 1118 | u_int i; |
| 1119 | char *ep; |
| 1120 | long val; |
| 1121 | |
| 1122 | if (cp == NULL) |
| 1123 | return -1; |
| 1124 | for (i = 0; ipqos[i].name != NULL; i++) { |
| 1125 | if (strcasecmp(cp, ipqos[i].name) == 0) |
| 1126 | return ipqos[i].value; |
| 1127 | } |
| 1128 | /* Try parsing as an integer */ |
| 1129 | val = strtol(cp, &ep, 0); |
| 1130 | if (*cp == '\0' || *ep != '\0' || val < 0 || val > 255) |
| 1131 | return -1; |
| 1132 | return val; |
| 1133 | } |
| 1134 | |
Damien Miller | 9147586 | 2011-05-05 14:14:34 +1000 | [diff] [blame] | 1135 | const char * |
| 1136 | iptos2str(int iptos) |
| 1137 | { |
| 1138 | int i; |
| 1139 | static char iptos_str[sizeof "0xff"]; |
| 1140 | |
| 1141 | for (i = 0; ipqos[i].name != NULL; i++) { |
| 1142 | if (ipqos[i].value == iptos) |
| 1143 | return ipqos[i].name; |
| 1144 | } |
| 1145 | snprintf(iptos_str, sizeof iptos_str, "0x%02x", iptos); |
| 1146 | return iptos_str; |
| 1147 | } |
Damien Miller | e9fc72e | 2013-10-15 12:14:12 +1100 | [diff] [blame] | 1148 | |
| 1149 | void |
| 1150 | lowercase(char *s) |
| 1151 | { |
| 1152 | for (; *s; s++) |
| 1153 | *s = tolower((u_char)*s); |
| 1154 | } |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 1155 | |
| 1156 | int |
| 1157 | unix_listener(const char *path, int backlog, int unlink_first) |
| 1158 | { |
| 1159 | struct sockaddr_un sunaddr; |
| 1160 | int saved_errno, sock; |
| 1161 | |
| 1162 | memset(&sunaddr, 0, sizeof(sunaddr)); |
| 1163 | sunaddr.sun_family = AF_UNIX; |
| 1164 | if (strlcpy(sunaddr.sun_path, path, sizeof(sunaddr.sun_path)) >= sizeof(sunaddr.sun_path)) { |
| 1165 | error("%s: \"%s\" too long for Unix domain socket", __func__, |
| 1166 | path); |
| 1167 | errno = ENAMETOOLONG; |
| 1168 | return -1; |
| 1169 | } |
| 1170 | |
| 1171 | sock = socket(PF_UNIX, SOCK_STREAM, 0); |
| 1172 | if (sock < 0) { |
| 1173 | saved_errno = errno; |
| 1174 | error("socket: %.100s", strerror(errno)); |
| 1175 | errno = saved_errno; |
| 1176 | return -1; |
| 1177 | } |
| 1178 | if (unlink_first == 1) { |
| 1179 | if (unlink(path) != 0 && errno != ENOENT) |
| 1180 | error("unlink(%s): %.100s", path, strerror(errno)); |
| 1181 | } |
| 1182 | if (bind(sock, (struct sockaddr *)&sunaddr, sizeof(sunaddr)) < 0) { |
| 1183 | saved_errno = errno; |
| 1184 | error("bind: %.100s", strerror(errno)); |
| 1185 | close(sock); |
| 1186 | error("%s: cannot bind to path: %s", __func__, path); |
| 1187 | errno = saved_errno; |
| 1188 | return -1; |
| 1189 | } |
| 1190 | if (listen(sock, backlog) < 0) { |
| 1191 | saved_errno = errno; |
| 1192 | error("listen: %.100s", strerror(errno)); |
| 1193 | close(sock); |
| 1194 | unlink(path); |
| 1195 | error("%s: cannot listen on path: %s", __func__, path); |
| 1196 | errno = saved_errno; |
| 1197 | return -1; |
| 1198 | } |
| 1199 | return sock; |
| 1200 | } |
Damien Miller | 4687802 | 2014-08-30 03:29:19 +1000 | [diff] [blame] | 1201 | |
Damien Miller | 04ee0f8 | 2009-11-18 17:48:30 +1100 | [diff] [blame] | 1202 | void |
| 1203 | sock_set_v6only(int s) |
| 1204 | { |
Damien Miller | d86a3ba | 2015-12-09 09:18:45 +1100 | [diff] [blame] | 1205 | #if defined(IPV6_V6ONLY) && !defined(__OpenBSD__) |
Damien Miller | 04ee0f8 | 2009-11-18 17:48:30 +1100 | [diff] [blame] | 1206 | int on = 1; |
| 1207 | |
| 1208 | debug3("%s: set socket %d IPV6_V6ONLY", __func__, s); |
| 1209 | if (setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof(on)) == -1) |
| 1210 | error("setsockopt IPV6_V6ONLY: %s", strerror(errno)); |
| 1211 | #endif |
| 1212 | } |
djm@openbsd.org | 0ccbd5e | 2016-04-06 06:42:17 +0000 | [diff] [blame] | 1213 | |
| 1214 | /* |
| 1215 | * Compares two strings that maybe be NULL. Returns non-zero if strings |
| 1216 | * are both NULL or are identical, returns zero otherwise. |
| 1217 | */ |
| 1218 | static int |
| 1219 | strcmp_maybe_null(const char *a, const char *b) |
| 1220 | { |
| 1221 | if ((a == NULL && b != NULL) || (a != NULL && b == NULL)) |
| 1222 | return 0; |
| 1223 | if (a != NULL && strcmp(a, b) != 0) |
| 1224 | return 0; |
| 1225 | return 1; |
| 1226 | } |
| 1227 | |
| 1228 | /* |
| 1229 | * Compare two forwards, returning non-zero if they are identical or |
| 1230 | * zero otherwise. |
| 1231 | */ |
| 1232 | int |
| 1233 | forward_equals(const struct Forward *a, const struct Forward *b) |
| 1234 | { |
| 1235 | if (strcmp_maybe_null(a->listen_host, b->listen_host) == 0) |
| 1236 | return 0; |
| 1237 | if (a->listen_port != b->listen_port) |
| 1238 | return 0; |
| 1239 | if (strcmp_maybe_null(a->listen_path, b->listen_path) == 0) |
| 1240 | return 0; |
| 1241 | if (strcmp_maybe_null(a->connect_host, b->connect_host) == 0) |
| 1242 | return 0; |
| 1243 | if (a->connect_port != b->connect_port) |
| 1244 | return 0; |
| 1245 | if (strcmp_maybe_null(a->connect_path, b->connect_path) == 0) |
| 1246 | return 0; |
| 1247 | /* allocated_port and handle are not checked */ |
| 1248 | return 1; |
| 1249 | } |
| 1250 | |
dtucker@openbsd.org | 1c4ef0b | 2016-10-23 22:04:05 +0000 | [diff] [blame] | 1251 | /* returns 1 if bind to specified port by specified user is permitted */ |
| 1252 | int |
| 1253 | bind_permitted(int port, uid_t uid) |
| 1254 | { |
| 1255 | if (port < IPPORT_RESERVED && uid != 0) |
| 1256 | return 0; |
| 1257 | return 1; |
| 1258 | } |
dtucker@openbsd.org | 7fc4766 | 2016-11-30 00:28:31 +0000 | [diff] [blame] | 1259 | |
| 1260 | /* returns 1 if process is already daemonized, 0 otherwise */ |
| 1261 | int |
| 1262 | daemonized(void) |
| 1263 | { |
| 1264 | int fd; |
| 1265 | |
| 1266 | if ((fd = open(_PATH_TTY, O_RDONLY | O_NOCTTY)) >= 0) { |
| 1267 | close(fd); |
| 1268 | return 0; /* have controlling terminal */ |
| 1269 | } |
| 1270 | if (getppid() != 1) |
| 1271 | return 0; /* parent is not init */ |
| 1272 | if (getsid(0) != getpid()) |
| 1273 | return 0; /* not session leader */ |
| 1274 | debug3("already daemonized"); |
| 1275 | return 1; |
| 1276 | } |