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