Darren Tucker | 5d19626 | 2006-07-12 22:15:16 +1000 | [diff] [blame] | 1 | /* $OpenBSD: misc.c,v 1.57 2006/07/10 16:37:36 stevesk 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 | |
| 29 | #include <sys/ioctl.h> |
Damien Miller | 9f2abc4 | 2006-07-10 20:53:08 +1000 | [diff] [blame] | 30 | #include <sys/types.h> |
Damien Miller | 8ec8c3e | 2006-07-10 20:35:38 +1000 | [diff] [blame] | 31 | #include <sys/socket.h> |
| 32 | |
Darren Tucker | 5d19626 | 2006-07-12 22:15:16 +1000 | [diff] [blame] | 33 | #include <stdarg.h> |
| 34 | |
Damien Miller | 8ec8c3e | 2006-07-10 20:35:38 +1000 | [diff] [blame] | 35 | #include <netinet/in.h> |
Damien Miller | 3a4051e | 2006-03-15 11:19:42 +1100 | [diff] [blame] | 36 | #include <netinet/tcp.h> |
Damien Miller | 8ec8c3e | 2006-07-10 20:35:38 +1000 | [diff] [blame] | 37 | |
Damien Miller | 57cf638 | 2006-07-10 21:13:46 +1000 | [diff] [blame] | 38 | #include <fcntl.h> |
Damien Miller | 03e2003 | 2006-03-15 11:16:59 +1100 | [diff] [blame] | 39 | #ifdef HAVE_PATHS_H |
Damien Miller | a9263d0 | 2006-03-15 11:18:26 +1100 | [diff] [blame] | 40 | # include <paths.h> |
Damien Miller | 9f2abc4 | 2006-07-10 20:53:08 +1000 | [diff] [blame] | 41 | #include <pwd.h> |
Damien Miller | 03e2003 | 2006-03-15 11:16:59 +1100 | [diff] [blame] | 42 | #endif |
Damien Miller | 3beb852 | 2006-01-02 23:40:10 +1100 | [diff] [blame] | 43 | #ifdef SSH_TUN_OPENBSD |
| 44 | #include <net/if.h> |
| 45 | #endif |
Damien Miller | 61c5150 | 2000-08-18 14:01:04 +1000 | [diff] [blame] | 46 | |
Kevin Steves | b6e773a | 2001-02-04 13:20:36 +0000 | [diff] [blame] | 47 | #include "misc.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 48 | #include "log.h" |
Ben Lindstrom | 0690901 | 2001-03-05 06:09:31 +0000 | [diff] [blame] | 49 | #include "xmalloc.h" |
Darren Tucker | da34553 | 2006-07-10 23:04:19 +1000 | [diff] [blame] | 50 | #include "ssh.h" |
Damien Miller | 61c5150 | 2000-08-18 14:01:04 +1000 | [diff] [blame] | 51 | |
Ben Lindstrom | 4cc240d | 2001-07-04 04:46:56 +0000 | [diff] [blame] | 52 | /* remove newline at end of string */ |
Damien Miller | 61c5150 | 2000-08-18 14:01:04 +1000 | [diff] [blame] | 53 | char * |
| 54 | chop(char *s) |
| 55 | { |
| 56 | char *t = s; |
| 57 | while (*t) { |
Ben Lindstrom | 1c37c6a | 2001-12-06 18:00:18 +0000 | [diff] [blame] | 58 | if (*t == '\n' || *t == '\r') { |
Damien Miller | 61c5150 | 2000-08-18 14:01:04 +1000 | [diff] [blame] | 59 | *t = '\0'; |
| 60 | return s; |
| 61 | } |
| 62 | t++; |
| 63 | } |
| 64 | return s; |
| 65 | |
| 66 | } |
| 67 | |
Ben Lindstrom | 4cc240d | 2001-07-04 04:46:56 +0000 | [diff] [blame] | 68 | /* set/unset filedescriptor to non-blocking */ |
Damien Miller | 232711f | 2004-06-15 10:35:30 +1000 | [diff] [blame] | 69 | int |
Damien Miller | 61c5150 | 2000-08-18 14:01:04 +1000 | [diff] [blame] | 70 | set_nonblock(int fd) |
| 71 | { |
| 72 | int val; |
Ben Lindstrom | c93e84c | 2001-05-12 00:08:37 +0000 | [diff] [blame] | 73 | |
Damien Miller | 61c5150 | 2000-08-18 14:01:04 +1000 | [diff] [blame] | 74 | val = fcntl(fd, F_GETFL, 0); |
| 75 | if (val < 0) { |
| 76 | error("fcntl(%d, F_GETFL, 0): %s", fd, strerror(errno)); |
Damien Miller | 232711f | 2004-06-15 10:35:30 +1000 | [diff] [blame] | 77 | return (-1); |
Damien Miller | 61c5150 | 2000-08-18 14:01:04 +1000 | [diff] [blame] | 78 | } |
Damien Miller | 69b69aa | 2000-10-28 14:19:58 +1100 | [diff] [blame] | 79 | if (val & O_NONBLOCK) { |
Damien Miller | 232711f | 2004-06-15 10:35:30 +1000 | [diff] [blame] | 80 | debug3("fd %d is O_NONBLOCK", fd); |
| 81 | return (0); |
Damien Miller | 69b69aa | 2000-10-28 14:19:58 +1100 | [diff] [blame] | 82 | } |
Damien Miller | ef095ce | 2003-05-14 13:41:39 +1000 | [diff] [blame] | 83 | debug2("fd %d setting O_NONBLOCK", fd); |
Damien Miller | 61c5150 | 2000-08-18 14:01:04 +1000 | [diff] [blame] | 84 | val |= O_NONBLOCK; |
Damien Miller | 232711f | 2004-06-15 10:35:30 +1000 | [diff] [blame] | 85 | if (fcntl(fd, F_SETFL, val) == -1) { |
| 86 | debug("fcntl(%d, F_SETFL, O_NONBLOCK): %s", fd, |
| 87 | strerror(errno)); |
| 88 | return (-1); |
| 89 | } |
| 90 | return (0); |
Damien Miller | 61c5150 | 2000-08-18 14:01:04 +1000 | [diff] [blame] | 91 | } |
| 92 | |
Damien Miller | 232711f | 2004-06-15 10:35:30 +1000 | [diff] [blame] | 93 | int |
Ben Lindstrom | c93e84c | 2001-05-12 00:08:37 +0000 | [diff] [blame] | 94 | unset_nonblock(int fd) |
| 95 | { |
| 96 | int val; |
| 97 | |
| 98 | val = fcntl(fd, F_GETFL, 0); |
| 99 | if (val < 0) { |
| 100 | error("fcntl(%d, F_GETFL, 0): %s", fd, strerror(errno)); |
Damien Miller | 232711f | 2004-06-15 10:35:30 +1000 | [diff] [blame] | 101 | return (-1); |
Ben Lindstrom | c93e84c | 2001-05-12 00:08:37 +0000 | [diff] [blame] | 102 | } |
| 103 | if (!(val & O_NONBLOCK)) { |
Damien Miller | 232711f | 2004-06-15 10:35:30 +1000 | [diff] [blame] | 104 | debug3("fd %d is not O_NONBLOCK", fd); |
| 105 | return (0); |
Ben Lindstrom | c93e84c | 2001-05-12 00:08:37 +0000 | [diff] [blame] | 106 | } |
Ben Lindstrom | 352b1c2 | 2001-06-21 03:04:37 +0000 | [diff] [blame] | 107 | debug("fd %d clearing O_NONBLOCK", fd); |
Ben Lindstrom | c93e84c | 2001-05-12 00:08:37 +0000 | [diff] [blame] | 108 | val &= ~O_NONBLOCK; |
Damien Miller | 232711f | 2004-06-15 10:35:30 +1000 | [diff] [blame] | 109 | if (fcntl(fd, F_SETFL, val) == -1) { |
| 110 | debug("fcntl(%d, F_SETFL, ~O_NONBLOCK): %s", |
Ben Lindstrom | 84fcb31 | 2002-03-05 01:48:09 +0000 | [diff] [blame] | 111 | fd, strerror(errno)); |
Damien Miller | 232711f | 2004-06-15 10:35:30 +1000 | [diff] [blame] | 112 | return (-1); |
| 113 | } |
| 114 | return (0); |
Ben Lindstrom | c93e84c | 2001-05-12 00:08:37 +0000 | [diff] [blame] | 115 | } |
| 116 | |
Damien Miller | 398e1cf | 2002-02-05 11:52:13 +1100 | [diff] [blame] | 117 | /* disable nagle on socket */ |
| 118 | void |
| 119 | set_nodelay(int fd) |
| 120 | { |
Ben Lindstrom | e86de51 | 2002-03-05 01:28:14 +0000 | [diff] [blame] | 121 | int opt; |
| 122 | socklen_t optlen; |
Damien Miller | 398e1cf | 2002-02-05 11:52:13 +1100 | [diff] [blame] | 123 | |
Ben Lindstrom | 1ebd7a5 | 2002-02-26 18:12:51 +0000 | [diff] [blame] | 124 | optlen = sizeof opt; |
| 125 | if (getsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &opt, &optlen) == -1) { |
Darren Tucker | 6db8f93 | 2003-11-03 20:07:14 +1100 | [diff] [blame] | 126 | debug("getsockopt TCP_NODELAY: %.100s", strerror(errno)); |
Ben Lindstrom | 1ebd7a5 | 2002-02-26 18:12:51 +0000 | [diff] [blame] | 127 | return; |
| 128 | } |
| 129 | if (opt == 1) { |
| 130 | debug2("fd %d is TCP_NODELAY", fd); |
| 131 | return; |
| 132 | } |
| 133 | opt = 1; |
Ben Lindstrom | 1d568f9 | 2002-12-23 02:44:36 +0000 | [diff] [blame] | 134 | debug2("fd %d setting TCP_NODELAY", fd); |
Ben Lindstrom | 1ebd7a5 | 2002-02-26 18:12:51 +0000 | [diff] [blame] | 135 | if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &opt, sizeof opt) == -1) |
Damien Miller | 398e1cf | 2002-02-05 11:52:13 +1100 | [diff] [blame] | 136 | error("setsockopt TCP_NODELAY: %.100s", strerror(errno)); |
| 137 | } |
| 138 | |
Damien Miller | 61c5150 | 2000-08-18 14:01:04 +1000 | [diff] [blame] | 139 | /* Characters considered whitespace in strsep calls. */ |
| 140 | #define WHITESPACE " \t\r\n" |
Damien Miller | 306d118 | 2006-03-15 12:05:59 +1100 | [diff] [blame] | 141 | #define QUOTE "\"" |
Damien Miller | 61c5150 | 2000-08-18 14:01:04 +1000 | [diff] [blame] | 142 | |
Ben Lindstrom | 4cc240d | 2001-07-04 04:46:56 +0000 | [diff] [blame] | 143 | /* return next token in configuration line */ |
Damien Miller | 61c5150 | 2000-08-18 14:01:04 +1000 | [diff] [blame] | 144 | char * |
| 145 | strdelim(char **s) |
| 146 | { |
| 147 | char *old; |
| 148 | int wspace = 0; |
| 149 | |
| 150 | if (*s == NULL) |
| 151 | return NULL; |
| 152 | |
| 153 | old = *s; |
| 154 | |
Damien Miller | 306d118 | 2006-03-15 12:05:59 +1100 | [diff] [blame] | 155 | *s = strpbrk(*s, WHITESPACE QUOTE "="); |
Damien Miller | 61c5150 | 2000-08-18 14:01:04 +1000 | [diff] [blame] | 156 | if (*s == NULL) |
| 157 | return (old); |
| 158 | |
Damien Miller | 306d118 | 2006-03-15 12:05:59 +1100 | [diff] [blame] | 159 | if (*s[0] == '\"') { |
| 160 | memmove(*s, *s + 1, strlen(*s)); /* move nul too */ |
| 161 | /* Find matching quote */ |
| 162 | if ((*s = strpbrk(*s, QUOTE)) == NULL) { |
| 163 | return (NULL); /* no matching quote */ |
| 164 | } else { |
| 165 | *s[0] = '\0'; |
| 166 | return (old); |
| 167 | } |
| 168 | } |
| 169 | |
Damien Miller | 61c5150 | 2000-08-18 14:01:04 +1000 | [diff] [blame] | 170 | /* Allow only one '=' to be skipped */ |
| 171 | if (*s[0] == '=') |
| 172 | wspace = 1; |
| 173 | *s[0] = '\0'; |
| 174 | |
Damien Miller | 306d118 | 2006-03-15 12:05:59 +1100 | [diff] [blame] | 175 | /* Skip any extra whitespace after first token */ |
Damien Miller | 61c5150 | 2000-08-18 14:01:04 +1000 | [diff] [blame] | 176 | *s += strspn(*s + 1, WHITESPACE) + 1; |
| 177 | if (*s[0] == '=' && !wspace) |
| 178 | *s += strspn(*s + 1, WHITESPACE) + 1; |
| 179 | |
| 180 | return (old); |
| 181 | } |
Kevin Steves | b6e773a | 2001-02-04 13:20:36 +0000 | [diff] [blame] | 182 | |
Ben Lindstrom | 086cf21 | 2001-03-05 05:56:40 +0000 | [diff] [blame] | 183 | struct passwd * |
| 184 | pwcopy(struct passwd *pw) |
| 185 | { |
Damien Miller | 07d86be | 2006-03-26 14:19:21 +1100 | [diff] [blame] | 186 | struct passwd *copy = xcalloc(1, sizeof(*copy)); |
Ben Lindstrom | 4030442 | 2001-03-05 06:22:01 +0000 | [diff] [blame] | 187 | |
Ben Lindstrom | 086cf21 | 2001-03-05 05:56:40 +0000 | [diff] [blame] | 188 | copy->pw_name = xstrdup(pw->pw_name); |
| 189 | copy->pw_passwd = xstrdup(pw->pw_passwd); |
Ben Lindstrom | 4030442 | 2001-03-05 06:22:01 +0000 | [diff] [blame] | 190 | copy->pw_gecos = xstrdup(pw->pw_gecos); |
Ben Lindstrom | 086cf21 | 2001-03-05 05:56:40 +0000 | [diff] [blame] | 191 | copy->pw_uid = pw->pw_uid; |
| 192 | copy->pw_gid = pw->pw_gid; |
Kevin Steves | 8245695 | 2001-06-22 21:14:18 +0000 | [diff] [blame] | 193 | #ifdef HAVE_PW_EXPIRE_IN_PASSWD |
Ben Lindstrom | 3af4d46 | 2001-06-21 03:11:27 +0000 | [diff] [blame] | 194 | copy->pw_expire = pw->pw_expire; |
Kevin Steves | 8245695 | 2001-06-22 21:14:18 +0000 | [diff] [blame] | 195 | #endif |
| 196 | #ifdef HAVE_PW_CHANGE_IN_PASSWD |
Ben Lindstrom | 3af4d46 | 2001-06-21 03:11:27 +0000 | [diff] [blame] | 197 | copy->pw_change = pw->pw_change; |
Kevin Steves | 8245695 | 2001-06-22 21:14:18 +0000 | [diff] [blame] | 198 | #endif |
Ben Lindstrom | 0f68db4 | 2001-03-05 07:57:09 +0000 | [diff] [blame] | 199 | #ifdef HAVE_PW_CLASS_IN_PASSWD |
Ben Lindstrom | 086cf21 | 2001-03-05 05:56:40 +0000 | [diff] [blame] | 200 | copy->pw_class = xstrdup(pw->pw_class); |
Ben Lindstrom | 0f68db4 | 2001-03-05 07:57:09 +0000 | [diff] [blame] | 201 | #endif |
Ben Lindstrom | 086cf21 | 2001-03-05 05:56:40 +0000 | [diff] [blame] | 202 | copy->pw_dir = xstrdup(pw->pw_dir); |
| 203 | copy->pw_shell = xstrdup(pw->pw_shell); |
| 204 | return copy; |
| 205 | } |
| 206 | |
Ben Lindstrom | 4cc240d | 2001-07-04 04:46:56 +0000 | [diff] [blame] | 207 | /* |
| 208 | * Convert ASCII string to TCP/IP port number. |
| 209 | * Port must be >0 and <=65535. |
| 210 | * Return 0 if invalid. |
| 211 | */ |
| 212 | int |
| 213 | a2port(const char *s) |
Ben Lindstrom | 19066a1 | 2001-04-12 23:39:26 +0000 | [diff] [blame] | 214 | { |
| 215 | long port; |
| 216 | char *endp; |
| 217 | |
| 218 | errno = 0; |
| 219 | port = strtol(s, &endp, 0); |
| 220 | if (s == endp || *endp != '\0' || |
| 221 | (errno == ERANGE && (port == LONG_MIN || port == LONG_MAX)) || |
| 222 | port <= 0 || port > 65535) |
| 223 | return 0; |
| 224 | |
| 225 | return port; |
| 226 | } |
| 227 | |
Damien Miller | d27b947 | 2005-12-13 19:29:02 +1100 | [diff] [blame] | 228 | int |
| 229 | a2tun(const char *s, int *remote) |
| 230 | { |
| 231 | const char *errstr = NULL; |
| 232 | char *sp, *ep; |
| 233 | int tun; |
| 234 | |
| 235 | if (remote != NULL) { |
Damien Miller | 7b58e80 | 2005-12-13 19:33:19 +1100 | [diff] [blame] | 236 | *remote = SSH_TUNID_ANY; |
Damien Miller | d27b947 | 2005-12-13 19:29:02 +1100 | [diff] [blame] | 237 | sp = xstrdup(s); |
| 238 | if ((ep = strchr(sp, ':')) == NULL) { |
| 239 | xfree(sp); |
| 240 | return (a2tun(s, NULL)); |
| 241 | } |
| 242 | ep[0] = '\0'; ep++; |
| 243 | *remote = a2tun(ep, NULL); |
| 244 | tun = a2tun(sp, NULL); |
| 245 | xfree(sp); |
Damien Miller | 7b58e80 | 2005-12-13 19:33:19 +1100 | [diff] [blame] | 246 | return (*remote == SSH_TUNID_ERR ? *remote : tun); |
Damien Miller | d27b947 | 2005-12-13 19:29:02 +1100 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | if (strcasecmp(s, "any") == 0) |
Damien Miller | 7b58e80 | 2005-12-13 19:33:19 +1100 | [diff] [blame] | 250 | return (SSH_TUNID_ANY); |
Damien Miller | d27b947 | 2005-12-13 19:29:02 +1100 | [diff] [blame] | 251 | |
Damien Miller | 7b58e80 | 2005-12-13 19:33:19 +1100 | [diff] [blame] | 252 | tun = strtonum(s, 0, SSH_TUNID_MAX, &errstr); |
| 253 | if (errstr != NULL) |
| 254 | return (SSH_TUNID_ERR); |
Damien Miller | d27b947 | 2005-12-13 19:29:02 +1100 | [diff] [blame] | 255 | |
| 256 | return (tun); |
| 257 | } |
| 258 | |
Ben Lindstrom | 1bda4c8 | 2001-06-05 19:59:08 +0000 | [diff] [blame] | 259 | #define SECONDS 1 |
| 260 | #define MINUTES (SECONDS * 60) |
| 261 | #define HOURS (MINUTES * 60) |
| 262 | #define DAYS (HOURS * 24) |
| 263 | #define WEEKS (DAYS * 7) |
| 264 | |
Ben Lindstrom | 4cc240d | 2001-07-04 04:46:56 +0000 | [diff] [blame] | 265 | /* |
| 266 | * Convert a time string into seconds; format is |
| 267 | * a sequence of: |
| 268 | * time[qualifier] |
| 269 | * |
| 270 | * Valid time qualifiers are: |
| 271 | * <none> seconds |
| 272 | * s|S seconds |
| 273 | * m|M minutes |
| 274 | * h|H hours |
| 275 | * d|D days |
| 276 | * w|W weeks |
| 277 | * |
| 278 | * Examples: |
| 279 | * 90m 90 minutes |
| 280 | * 1h30m 90 minutes |
| 281 | * 2d 2 days |
| 282 | * 1w 1 week |
| 283 | * |
| 284 | * Return -1 if time string is invalid. |
| 285 | */ |
| 286 | long |
| 287 | convtime(const char *s) |
Ben Lindstrom | 1bda4c8 | 2001-06-05 19:59:08 +0000 | [diff] [blame] | 288 | { |
| 289 | long total, secs; |
| 290 | const char *p; |
| 291 | char *endp; |
| 292 | |
| 293 | errno = 0; |
| 294 | total = 0; |
| 295 | p = s; |
| 296 | |
| 297 | if (p == NULL || *p == '\0') |
| 298 | return -1; |
| 299 | |
| 300 | while (*p) { |
| 301 | secs = strtol(p, &endp, 10); |
| 302 | if (p == endp || |
| 303 | (errno == ERANGE && (secs == LONG_MIN || secs == LONG_MAX)) || |
| 304 | secs < 0) |
| 305 | return -1; |
| 306 | |
| 307 | switch (*endp++) { |
| 308 | case '\0': |
| 309 | endp--; |
Damien Miller | 69b7203 | 2006-03-26 14:02:35 +1100 | [diff] [blame] | 310 | break; |
Ben Lindstrom | 1bda4c8 | 2001-06-05 19:59:08 +0000 | [diff] [blame] | 311 | case 's': |
| 312 | case 'S': |
| 313 | break; |
| 314 | case 'm': |
| 315 | case 'M': |
| 316 | secs *= MINUTES; |
| 317 | break; |
| 318 | case 'h': |
| 319 | case 'H': |
| 320 | secs *= HOURS; |
| 321 | break; |
| 322 | case 'd': |
| 323 | case 'D': |
| 324 | secs *= DAYS; |
| 325 | break; |
| 326 | case 'w': |
| 327 | case 'W': |
| 328 | secs *= WEEKS; |
| 329 | break; |
| 330 | default: |
| 331 | return -1; |
| 332 | } |
| 333 | total += secs; |
| 334 | if (total < 0) |
| 335 | return -1; |
| 336 | p = endp; |
| 337 | } |
| 338 | |
| 339 | return total; |
| 340 | } |
| 341 | |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 342 | /* |
Darren Tucker | da34553 | 2006-07-10 23:04:19 +1000 | [diff] [blame] | 343 | * Returns a standardized host+port identifier string. |
| 344 | * Caller must free returned string. |
| 345 | */ |
| 346 | char * |
| 347 | put_host_port(const char *host, u_short port) |
| 348 | { |
| 349 | char *hoststr; |
| 350 | |
| 351 | if (port == 0 || port == SSH_DEFAULT_PORT) |
| 352 | return(xstrdup(host)); |
| 353 | if (asprintf(&hoststr, "[%s]:%d", host, (int)port) < 0) |
| 354 | fatal("put_host_port: asprintf: %s", strerror(errno)); |
| 355 | debug3("put_host_port: %s", hoststr); |
| 356 | return hoststr; |
| 357 | } |
| 358 | |
| 359 | /* |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 360 | * Search for next delimiter between hostnames/addresses and ports. |
| 361 | * Argument may be modified (for termination). |
| 362 | * Returns *cp if parsing succeeds. |
| 363 | * *cp is set to the start of the next delimiter, if one was found. |
| 364 | * If this is the last field, *cp is set to NULL. |
| 365 | */ |
| 366 | char * |
| 367 | hpdelim(char **cp) |
| 368 | { |
| 369 | char *s, *old; |
| 370 | |
| 371 | if (cp == NULL || *cp == NULL) |
| 372 | return NULL; |
| 373 | |
| 374 | old = s = *cp; |
| 375 | if (*s == '[') { |
| 376 | if ((s = strchr(s, ']')) == NULL) |
| 377 | return NULL; |
| 378 | else |
| 379 | s++; |
| 380 | } else if ((s = strpbrk(s, ":/")) == NULL) |
| 381 | s = *cp + strlen(*cp); /* skip to end (see first case below) */ |
| 382 | |
| 383 | switch (*s) { |
| 384 | case '\0': |
| 385 | *cp = NULL; /* no more fields*/ |
| 386 | break; |
Darren Tucker | 47eede7 | 2005-03-14 23:08:12 +1100 | [diff] [blame] | 387 | |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 388 | case ':': |
| 389 | case '/': |
| 390 | *s = '\0'; /* terminate */ |
| 391 | *cp = s + 1; |
| 392 | break; |
Darren Tucker | 47eede7 | 2005-03-14 23:08:12 +1100 | [diff] [blame] | 393 | |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 394 | default: |
| 395 | return NULL; |
| 396 | } |
| 397 | |
| 398 | return old; |
| 399 | } |
| 400 | |
Ben Lindstrom | 4529b70 | 2001-05-03 23:39:53 +0000 | [diff] [blame] | 401 | char * |
| 402 | cleanhostname(char *host) |
| 403 | { |
| 404 | if (*host == '[' && host[strlen(host) - 1] == ']') { |
| 405 | host[strlen(host) - 1] = '\0'; |
| 406 | return (host + 1); |
| 407 | } else |
| 408 | return host; |
| 409 | } |
| 410 | |
| 411 | char * |
| 412 | colon(char *cp) |
| 413 | { |
| 414 | int flag = 0; |
| 415 | |
| 416 | if (*cp == ':') /* Leading colon is part of file name. */ |
| 417 | return (0); |
| 418 | if (*cp == '[') |
| 419 | flag = 1; |
| 420 | |
| 421 | for (; *cp; ++cp) { |
| 422 | if (*cp == '@' && *(cp+1) == '[') |
| 423 | flag = 1; |
| 424 | if (*cp == ']' && *(cp+1) == ':' && flag) |
| 425 | return (cp+1); |
| 426 | if (*cp == ':' && !flag) |
| 427 | return (cp); |
| 428 | if (*cp == '/') |
| 429 | return (0); |
| 430 | } |
| 431 | return (0); |
| 432 | } |
| 433 | |
Ben Lindstrom | 4cc240d | 2001-07-04 04:46:56 +0000 | [diff] [blame] | 434 | /* function to assist building execv() arguments */ |
Ben Lindstrom | 387c472 | 2001-05-08 20:27:25 +0000 | [diff] [blame] | 435 | void |
| 436 | addargs(arglist *args, char *fmt, ...) |
| 437 | { |
| 438 | va_list ap; |
Damien Miller | 3eec6b7 | 2006-01-31 21:49:27 +1100 | [diff] [blame] | 439 | char *cp; |
Darren Tucker | c7a6fc4 | 2004-08-13 21:18:00 +1000 | [diff] [blame] | 440 | u_int nalloc; |
Damien Miller | 3eec6b7 | 2006-01-31 21:49:27 +1100 | [diff] [blame] | 441 | int r; |
Ben Lindstrom | 387c472 | 2001-05-08 20:27:25 +0000 | [diff] [blame] | 442 | |
| 443 | va_start(ap, fmt); |
Damien Miller | 3eec6b7 | 2006-01-31 21:49:27 +1100 | [diff] [blame] | 444 | r = vasprintf(&cp, fmt, ap); |
Ben Lindstrom | 387c472 | 2001-05-08 20:27:25 +0000 | [diff] [blame] | 445 | va_end(ap); |
Damien Miller | 3eec6b7 | 2006-01-31 21:49:27 +1100 | [diff] [blame] | 446 | if (r == -1) |
| 447 | fatal("addargs: argument too long"); |
Ben Lindstrom | 387c472 | 2001-05-08 20:27:25 +0000 | [diff] [blame] | 448 | |
Darren Tucker | fb16b24 | 2003-09-22 21:04:23 +1000 | [diff] [blame] | 449 | nalloc = args->nalloc; |
Ben Lindstrom | 387c472 | 2001-05-08 20:27:25 +0000 | [diff] [blame] | 450 | if (args->list == NULL) { |
Darren Tucker | fb16b24 | 2003-09-22 21:04:23 +1000 | [diff] [blame] | 451 | nalloc = 32; |
Ben Lindstrom | 387c472 | 2001-05-08 20:27:25 +0000 | [diff] [blame] | 452 | args->num = 0; |
Darren Tucker | fb16b24 | 2003-09-22 21:04:23 +1000 | [diff] [blame] | 453 | } else if (args->num+2 >= nalloc) |
| 454 | nalloc *= 2; |
Ben Lindstrom | 387c472 | 2001-05-08 20:27:25 +0000 | [diff] [blame] | 455 | |
Damien Miller | 3681209 | 2006-03-26 14:22:47 +1100 | [diff] [blame] | 456 | args->list = xrealloc(args->list, nalloc, sizeof(char *)); |
Darren Tucker | fb16b24 | 2003-09-22 21:04:23 +1000 | [diff] [blame] | 457 | args->nalloc = nalloc; |
Damien Miller | 3eec6b7 | 2006-01-31 21:49:27 +1100 | [diff] [blame] | 458 | args->list[args->num++] = cp; |
Ben Lindstrom | 387c472 | 2001-05-08 20:27:25 +0000 | [diff] [blame] | 459 | args->list[args->num] = NULL; |
| 460 | } |
Darren Tucker | 22cc741 | 2004-12-06 22:47:41 +1100 | [diff] [blame] | 461 | |
Damien Miller | 3eec6b7 | 2006-01-31 21:49:27 +1100 | [diff] [blame] | 462 | void |
| 463 | replacearg(arglist *args, u_int which, char *fmt, ...) |
| 464 | { |
| 465 | va_list ap; |
| 466 | char *cp; |
| 467 | int r; |
| 468 | |
| 469 | va_start(ap, fmt); |
| 470 | r = vasprintf(&cp, fmt, ap); |
| 471 | va_end(ap); |
| 472 | if (r == -1) |
| 473 | fatal("replacearg: argument too long"); |
| 474 | |
| 475 | if (which >= args->num) |
| 476 | fatal("replacearg: tried to replace invalid arg %d >= %d", |
| 477 | which, args->num); |
| 478 | xfree(args->list[which]); |
| 479 | args->list[which] = cp; |
| 480 | } |
| 481 | |
| 482 | void |
| 483 | freeargs(arglist *args) |
| 484 | { |
| 485 | u_int i; |
| 486 | |
| 487 | if (args->list != NULL) { |
| 488 | for (i = 0; i < args->num; i++) |
| 489 | xfree(args->list[i]); |
| 490 | xfree(args->list); |
| 491 | args->nalloc = args->num = 0; |
| 492 | args->list = NULL; |
| 493 | } |
| 494 | } |
| 495 | |
Darren Tucker | 22cc741 | 2004-12-06 22:47:41 +1100 | [diff] [blame] | 496 | /* |
Damien Miller | 5fd38c0 | 2005-05-26 12:02:14 +1000 | [diff] [blame] | 497 | * Expands tildes in the file name. Returns data allocated by xmalloc. |
| 498 | * Warning: this calls getpw*. |
| 499 | */ |
| 500 | char * |
| 501 | tilde_expand_filename(const char *filename, uid_t uid) |
| 502 | { |
| 503 | const char *path; |
| 504 | char user[128], ret[MAXPATHLEN]; |
| 505 | struct passwd *pw; |
Damien Miller | eccb9de | 2005-06-17 12:59:34 +1000 | [diff] [blame] | 506 | u_int len, slash; |
Damien Miller | 5fd38c0 | 2005-05-26 12:02:14 +1000 | [diff] [blame] | 507 | |
| 508 | if (*filename != '~') |
| 509 | return (xstrdup(filename)); |
| 510 | filename++; |
| 511 | |
| 512 | path = strchr(filename, '/'); |
| 513 | if (path != NULL && path > filename) { /* ~user/path */ |
Damien Miller | eccb9de | 2005-06-17 12:59:34 +1000 | [diff] [blame] | 514 | slash = path - filename; |
| 515 | if (slash > sizeof(user) - 1) |
Damien Miller | 5fd38c0 | 2005-05-26 12:02:14 +1000 | [diff] [blame] | 516 | fatal("tilde_expand_filename: ~username too long"); |
Damien Miller | eccb9de | 2005-06-17 12:59:34 +1000 | [diff] [blame] | 517 | memcpy(user, filename, slash); |
| 518 | user[slash] = '\0'; |
Damien Miller | 5fd38c0 | 2005-05-26 12:02:14 +1000 | [diff] [blame] | 519 | if ((pw = getpwnam(user)) == NULL) |
| 520 | fatal("tilde_expand_filename: No such user %s", user); |
| 521 | } else if ((pw = getpwuid(uid)) == NULL) /* ~/path */ |
| 522 | fatal("tilde_expand_filename: No such uid %d", uid); |
| 523 | |
| 524 | if (strlcpy(ret, pw->pw_dir, sizeof(ret)) >= sizeof(ret)) |
| 525 | fatal("tilde_expand_filename: Path too long"); |
| 526 | |
| 527 | /* Make sure directory has a trailing '/' */ |
| 528 | len = strlen(pw->pw_dir); |
| 529 | if ((len == 0 || pw->pw_dir[len - 1] != '/') && |
| 530 | strlcat(ret, "/", sizeof(ret)) >= sizeof(ret)) |
| 531 | fatal("tilde_expand_filename: Path too long"); |
| 532 | |
| 533 | /* Skip leading '/' from specified path */ |
| 534 | if (path != NULL) |
| 535 | filename = path + 1; |
| 536 | if (strlcat(ret, filename, sizeof(ret)) >= sizeof(ret)) |
| 537 | fatal("tilde_expand_filename: Path too long"); |
| 538 | |
| 539 | return (xstrdup(ret)); |
| 540 | } |
| 541 | |
| 542 | /* |
Damien Miller | 6476cad | 2005-06-16 13:18:34 +1000 | [diff] [blame] | 543 | * Expand a string with a set of %[char] escapes. A number of escapes may be |
| 544 | * specified as (char *escape_chars, char *replacement) pairs. The list must |
Darren Tucker | bee73d5 | 2005-07-14 17:05:02 +1000 | [diff] [blame] | 545 | * be terminated by a NULL escape_char. Returns replaced string in memory |
Damien Miller | 6476cad | 2005-06-16 13:18:34 +1000 | [diff] [blame] | 546 | * allocated by xmalloc. |
| 547 | */ |
| 548 | char * |
| 549 | percent_expand(const char *string, ...) |
| 550 | { |
| 551 | #define EXPAND_MAX_KEYS 16 |
| 552 | struct { |
| 553 | const char *key; |
| 554 | const char *repl; |
| 555 | } keys[EXPAND_MAX_KEYS]; |
Damien Miller | eccb9de | 2005-06-17 12:59:34 +1000 | [diff] [blame] | 556 | u_int num_keys, i, j; |
Damien Miller | 6476cad | 2005-06-16 13:18:34 +1000 | [diff] [blame] | 557 | char buf[4096]; |
| 558 | va_list ap; |
| 559 | |
| 560 | /* Gather keys */ |
| 561 | va_start(ap, string); |
| 562 | for (num_keys = 0; num_keys < EXPAND_MAX_KEYS; num_keys++) { |
| 563 | keys[num_keys].key = va_arg(ap, char *); |
| 564 | if (keys[num_keys].key == NULL) |
| 565 | break; |
| 566 | keys[num_keys].repl = va_arg(ap, char *); |
| 567 | if (keys[num_keys].repl == NULL) |
| 568 | fatal("percent_expand: NULL replacement"); |
| 569 | } |
| 570 | va_end(ap); |
| 571 | |
| 572 | if (num_keys >= EXPAND_MAX_KEYS) |
| 573 | fatal("percent_expand: too many keys"); |
| 574 | |
| 575 | /* Expand string */ |
| 576 | *buf = '\0'; |
| 577 | for (i = 0; *string != '\0'; string++) { |
| 578 | if (*string != '%') { |
| 579 | append: |
| 580 | buf[i++] = *string; |
| 581 | if (i >= sizeof(buf)) |
| 582 | fatal("percent_expand: string too long"); |
| 583 | buf[i] = '\0'; |
| 584 | continue; |
| 585 | } |
| 586 | string++; |
| 587 | if (*string == '%') |
| 588 | goto append; |
| 589 | for (j = 0; j < num_keys; j++) { |
| 590 | if (strchr(keys[j].key, *string) != NULL) { |
| 591 | i = strlcat(buf, keys[j].repl, sizeof(buf)); |
| 592 | if (i >= sizeof(buf)) |
| 593 | fatal("percent_expand: string too long"); |
| 594 | break; |
| 595 | } |
| 596 | } |
| 597 | if (j >= num_keys) |
| 598 | fatal("percent_expand: unknown key %%%c", *string); |
| 599 | } |
| 600 | return (xstrdup(buf)); |
| 601 | #undef EXPAND_MAX_KEYS |
| 602 | } |
| 603 | |
| 604 | /* |
Darren Tucker | 22cc741 | 2004-12-06 22:47:41 +1100 | [diff] [blame] | 605 | * Read an entire line from a public key file into a static buffer, discarding |
| 606 | * lines that exceed the buffer size. Returns 0 on success, -1 on failure. |
| 607 | */ |
| 608 | int |
| 609 | 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] | 610 | u_long *lineno) |
Darren Tucker | 22cc741 | 2004-12-06 22:47:41 +1100 | [diff] [blame] | 611 | { |
| 612 | while (fgets(buf, bufsz, f) != NULL) { |
| 613 | (*lineno)++; |
| 614 | if (buf[strlen(buf) - 1] == '\n' || feof(f)) { |
| 615 | return 0; |
| 616 | } else { |
Darren Tucker | f0f9098 | 2004-12-11 13:39:50 +1100 | [diff] [blame] | 617 | debug("%s: %s line %lu exceeds size limit", __func__, |
| 618 | filename, *lineno); |
Darren Tucker | 22cc741 | 2004-12-06 22:47:41 +1100 | [diff] [blame] | 619 | /* discard remainder of line */ |
Darren Tucker | 47eede7 | 2005-03-14 23:08:12 +1100 | [diff] [blame] | 620 | while (fgetc(f) != '\n' && !feof(f)) |
Darren Tucker | 22cc741 | 2004-12-06 22:47:41 +1100 | [diff] [blame] | 621 | ; /* nothing */ |
| 622 | } |
| 623 | } |
| 624 | return -1; |
| 625 | } |
Damien Miller | 1339002 | 2005-07-06 09:44:19 +1000 | [diff] [blame] | 626 | |
Damien Miller | d27b947 | 2005-12-13 19:29:02 +1100 | [diff] [blame] | 627 | int |
Damien Miller | 7b58e80 | 2005-12-13 19:33:19 +1100 | [diff] [blame] | 628 | tun_open(int tun, int mode) |
Damien Miller | d27b947 | 2005-12-13 19:29:02 +1100 | [diff] [blame] | 629 | { |
Damien Miller | 62a31c9 | 2005-12-13 20:44:13 +1100 | [diff] [blame] | 630 | #if defined(CUSTOM_SYS_TUN_OPEN) |
| 631 | return (sys_tun_open(tun, mode)); |
Damien Miller | 2dcddbf | 2006-01-01 19:47:05 +1100 | [diff] [blame] | 632 | #elif defined(SSH_TUN_OPENBSD) |
Damien Miller | 7b58e80 | 2005-12-13 19:33:19 +1100 | [diff] [blame] | 633 | struct ifreq ifr; |
Damien Miller | d27b947 | 2005-12-13 19:29:02 +1100 | [diff] [blame] | 634 | char name[100]; |
Damien Miller | 7b58e80 | 2005-12-13 19:33:19 +1100 | [diff] [blame] | 635 | int fd = -1, sock; |
Damien Miller | d27b947 | 2005-12-13 19:29:02 +1100 | [diff] [blame] | 636 | |
Damien Miller | 7b58e80 | 2005-12-13 19:33:19 +1100 | [diff] [blame] | 637 | /* Open the tunnel device */ |
| 638 | if (tun <= SSH_TUNID_MAX) { |
Damien Miller | d27b947 | 2005-12-13 19:29:02 +1100 | [diff] [blame] | 639 | snprintf(name, sizeof(name), "/dev/tun%d", tun); |
Damien Miller | 7b58e80 | 2005-12-13 19:33:19 +1100 | [diff] [blame] | 640 | fd = open(name, O_RDWR); |
| 641 | } else if (tun == SSH_TUNID_ANY) { |
| 642 | for (tun = 100; tun >= 0; tun--) { |
| 643 | snprintf(name, sizeof(name), "/dev/tun%d", tun); |
| 644 | if ((fd = open(name, O_RDWR)) >= 0) |
| 645 | break; |
Damien Miller | d27b947 | 2005-12-13 19:29:02 +1100 | [diff] [blame] | 646 | } |
| 647 | } else { |
Damien Miller | a210d52 | 2006-01-02 23:40:30 +1100 | [diff] [blame] | 648 | debug("%s: invalid tunnel %u", __func__, tun); |
Damien Miller | 7b58e80 | 2005-12-13 19:33:19 +1100 | [diff] [blame] | 649 | return (-1); |
Damien Miller | d27b947 | 2005-12-13 19:29:02 +1100 | [diff] [blame] | 650 | } |
Damien Miller | 7b58e80 | 2005-12-13 19:33:19 +1100 | [diff] [blame] | 651 | |
| 652 | if (fd < 0) { |
| 653 | debug("%s: %s open failed: %s", __func__, name, strerror(errno)); |
| 654 | return (-1); |
| 655 | } |
| 656 | |
| 657 | debug("%s: %s mode %d fd %d", __func__, name, mode, fd); |
| 658 | |
| 659 | /* Set the tunnel device operation mode */ |
| 660 | snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "tun%d", tun); |
| 661 | if ((sock = socket(PF_UNIX, SOCK_STREAM, 0)) == -1) |
| 662 | goto failed; |
| 663 | |
| 664 | if (ioctl(sock, SIOCGIFFLAGS, &ifr) == -1) |
| 665 | goto failed; |
Damien Miller | a1d9a18 | 2006-01-02 23:41:21 +1100 | [diff] [blame] | 666 | |
| 667 | /* Set interface mode */ |
| 668 | ifr.ifr_flags &= ~IFF_UP; |
| 669 | if (mode == SSH_TUNMODE_ETHERNET) |
Damien Miller | 7b58e80 | 2005-12-13 19:33:19 +1100 | [diff] [blame] | 670 | ifr.ifr_flags |= IFF_LINK0; |
Damien Miller | a1d9a18 | 2006-01-02 23:41:21 +1100 | [diff] [blame] | 671 | else |
| 672 | ifr.ifr_flags &= ~IFF_LINK0; |
| 673 | if (ioctl(sock, SIOCSIFFLAGS, &ifr) == -1) |
| 674 | goto failed; |
| 675 | |
| 676 | /* Bring interface up */ |
Damien Miller | 7b58e80 | 2005-12-13 19:33:19 +1100 | [diff] [blame] | 677 | ifr.ifr_flags |= IFF_UP; |
| 678 | if (ioctl(sock, SIOCSIFFLAGS, &ifr) == -1) |
| 679 | goto failed; |
| 680 | |
| 681 | close(sock); |
| 682 | return (fd); |
| 683 | |
| 684 | failed: |
| 685 | if (fd >= 0) |
| 686 | close(fd); |
| 687 | if (sock >= 0) |
| 688 | close(sock); |
| 689 | debug("%s: failed to set %s mode %d: %s", __func__, name, |
| 690 | mode, strerror(errno)); |
Damien Miller | d27b947 | 2005-12-13 19:29:02 +1100 | [diff] [blame] | 691 | return (-1); |
Damien Miller | 62a31c9 | 2005-12-13 20:44:13 +1100 | [diff] [blame] | 692 | #else |
| 693 | error("Tunnel interfaces are not supported on this platform"); |
| 694 | return (-1); |
| 695 | #endif |
Damien Miller | d27b947 | 2005-12-13 19:29:02 +1100 | [diff] [blame] | 696 | } |
| 697 | |
Darren Tucker | ce321d8 | 2005-10-03 18:11:24 +1000 | [diff] [blame] | 698 | void |
| 699 | sanitise_stdfd(void) |
| 700 | { |
Damien Miller | 72c5b7d | 2006-01-06 14:50:44 +1100 | [diff] [blame] | 701 | int nullfd, dupfd; |
Darren Tucker | ce321d8 | 2005-10-03 18:11:24 +1000 | [diff] [blame] | 702 | |
Damien Miller | 72c5b7d | 2006-01-06 14:50:44 +1100 | [diff] [blame] | 703 | if ((nullfd = dupfd = open(_PATH_DEVNULL, O_RDWR)) == -1) { |
Darren Tucker | ce321d8 | 2005-10-03 18:11:24 +1000 | [diff] [blame] | 704 | fprintf(stderr, "Couldn't open /dev/null: %s", strerror(errno)); |
| 705 | exit(1); |
| 706 | } |
Damien Miller | 72c5b7d | 2006-01-06 14:50:44 +1100 | [diff] [blame] | 707 | while (++dupfd <= 2) { |
| 708 | /* Only clobber closed fds */ |
| 709 | if (fcntl(dupfd, F_GETFL, 0) >= 0) |
| 710 | continue; |
| 711 | if (dup2(nullfd, dupfd) == -1) { |
Darren Tucker | ce321d8 | 2005-10-03 18:11:24 +1000 | [diff] [blame] | 712 | fprintf(stderr, "dup2: %s", strerror(errno)); |
| 713 | exit(1); |
| 714 | } |
Darren Tucker | ce321d8 | 2005-10-03 18:11:24 +1000 | [diff] [blame] | 715 | } |
| 716 | if (nullfd > 2) |
| 717 | close(nullfd); |
| 718 | } |
| 719 | |
Damien Miller | 1339002 | 2005-07-06 09:44:19 +1000 | [diff] [blame] | 720 | char * |
Damien Miller | 3f94188 | 2006-03-31 23:13:02 +1100 | [diff] [blame] | 721 | tohex(const void *vp, size_t l) |
Damien Miller | 1339002 | 2005-07-06 09:44:19 +1000 | [diff] [blame] | 722 | { |
Damien Miller | 3f94188 | 2006-03-31 23:13:02 +1100 | [diff] [blame] | 723 | const u_char *p = (const u_char *)vp; |
Damien Miller | 1339002 | 2005-07-06 09:44:19 +1000 | [diff] [blame] | 724 | char b[3], *r; |
Damien Miller | 3f94188 | 2006-03-31 23:13:02 +1100 | [diff] [blame] | 725 | size_t i, hl; |
| 726 | |
| 727 | if (l > 65536) |
| 728 | return xstrdup("tohex: length > 65536"); |
Damien Miller | 1339002 | 2005-07-06 09:44:19 +1000 | [diff] [blame] | 729 | |
| 730 | hl = l * 2 + 1; |
Damien Miller | 07d86be | 2006-03-26 14:19:21 +1100 | [diff] [blame] | 731 | r = xcalloc(1, hl); |
Damien Miller | 1339002 | 2005-07-06 09:44:19 +1000 | [diff] [blame] | 732 | for (i = 0; i < l; i++) { |
Damien Miller | 3f94188 | 2006-03-31 23:13:02 +1100 | [diff] [blame] | 733 | snprintf(b, sizeof(b), "%02x", p[i]); |
Damien Miller | 1339002 | 2005-07-06 09:44:19 +1000 | [diff] [blame] | 734 | strlcat(r, b, hl); |
| 735 | } |
| 736 | return (r); |
| 737 | } |
| 738 | |
Damien Miller | 3f94188 | 2006-03-31 23:13:02 +1100 | [diff] [blame] | 739 | u_int64_t |
| 740 | get_u64(const void *vp) |
| 741 | { |
| 742 | const u_char *p = (const u_char *)vp; |
| 743 | u_int64_t v; |
| 744 | |
| 745 | v = (u_int64_t)p[0] << 56; |
| 746 | v |= (u_int64_t)p[1] << 48; |
| 747 | v |= (u_int64_t)p[2] << 40; |
| 748 | v |= (u_int64_t)p[3] << 32; |
| 749 | v |= (u_int64_t)p[4] << 24; |
| 750 | v |= (u_int64_t)p[5] << 16; |
| 751 | v |= (u_int64_t)p[6] << 8; |
| 752 | v |= (u_int64_t)p[7]; |
| 753 | |
| 754 | return (v); |
| 755 | } |
| 756 | |
| 757 | u_int32_t |
| 758 | get_u32(const void *vp) |
| 759 | { |
| 760 | const u_char *p = (const u_char *)vp; |
| 761 | u_int32_t v; |
| 762 | |
| 763 | v = (u_int32_t)p[0] << 24; |
| 764 | v |= (u_int32_t)p[1] << 16; |
| 765 | v |= (u_int32_t)p[2] << 8; |
| 766 | v |= (u_int32_t)p[3]; |
| 767 | |
| 768 | return (v); |
| 769 | } |
| 770 | |
| 771 | u_int16_t |
| 772 | get_u16(const void *vp) |
| 773 | { |
| 774 | const u_char *p = (const u_char *)vp; |
| 775 | u_int16_t v; |
| 776 | |
| 777 | v = (u_int16_t)p[0] << 8; |
| 778 | v |= (u_int16_t)p[1]; |
| 779 | |
| 780 | return (v); |
| 781 | } |
| 782 | |
| 783 | void |
| 784 | put_u64(void *vp, u_int64_t v) |
| 785 | { |
| 786 | u_char *p = (u_char *)vp; |
| 787 | |
| 788 | p[0] = (u_char)(v >> 56) & 0xff; |
| 789 | p[1] = (u_char)(v >> 48) & 0xff; |
| 790 | p[2] = (u_char)(v >> 40) & 0xff; |
| 791 | p[3] = (u_char)(v >> 32) & 0xff; |
| 792 | p[4] = (u_char)(v >> 24) & 0xff; |
| 793 | p[5] = (u_char)(v >> 16) & 0xff; |
| 794 | p[6] = (u_char)(v >> 8) & 0xff; |
| 795 | p[7] = (u_char)v & 0xff; |
| 796 | } |
| 797 | |
| 798 | void |
| 799 | put_u32(void *vp, u_int32_t v) |
| 800 | { |
| 801 | u_char *p = (u_char *)vp; |
| 802 | |
| 803 | p[0] = (u_char)(v >> 24) & 0xff; |
| 804 | p[1] = (u_char)(v >> 16) & 0xff; |
| 805 | p[2] = (u_char)(v >> 8) & 0xff; |
| 806 | p[3] = (u_char)v & 0xff; |
| 807 | } |
| 808 | |
| 809 | |
| 810 | void |
| 811 | put_u16(void *vp, u_int16_t v) |
| 812 | { |
| 813 | u_char *p = (u_char *)vp; |
| 814 | |
| 815 | p[0] = (u_char)(v >> 8) & 0xff; |
| 816 | p[1] = (u_char)v & 0xff; |
| 817 | } |