Changeset View
Changeset View
Standalone View
Standalone View
misc.c
/* $OpenBSD: misc.c,v 1.137 2019/01/23 21:50:56 dtucker Exp $ */ | /* $OpenBSD: misc.c,v 1.153 2020/06/26 05:16:38 djm Exp $ */ | ||||
/* | /* | ||||
* Copyright (c) 2000 Markus Friedl. All rights reserved. | * Copyright (c) 2000 Markus Friedl. All rights reserved. | ||||
* Copyright (c) 2005,2006 Damien Miller. All rights reserved. | * Copyright (c) 2005-2020 Damien Miller. All rights reserved. | ||||
* Copyright (c) 2004 Henning Brauer <henning@openbsd.org> | |||||
* | * | ||||
* Redistribution and use in source and binary forms, with or without | * Permission to use, copy, modify, and distribute this software for any | ||||
* modification, are permitted provided that the following conditions | * purpose with or without fee is hereby granted, provided that the above | ||||
* are met: | * copyright notice and this permission notice appear in all copies. | ||||
* 1. Redistributions of source code must retain the above copyright | |||||
* notice, this list of conditions and the following disclaimer. | |||||
* 2. Redistributions in binary form must reproduce the above copyright | |||||
* notice, this list of conditions and the following disclaimer in the | |||||
* documentation and/or other materials provided with the distribution. | |||||
* | * | ||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||||
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||||
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | |||||
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |||||
*/ | */ | ||||
#include "includes.h" | #include "includes.h" | ||||
#include <sys/types.h> | #include <sys/types.h> | ||||
#include <sys/ioctl.h> | #include <sys/ioctl.h> | ||||
#include <sys/socket.h> | #include <sys/socket.h> | ||||
#include <sys/stat.h> | #include <sys/stat.h> | ||||
#include <sys/time.h> | #include <sys/time.h> | ||||
#include <sys/wait.h> | #include <sys/wait.h> | ||||
#include <sys/un.h> | #include <sys/un.h> | ||||
#include <limits.h> | #include <limits.h> | ||||
#ifdef HAVE_LIBGEN_H | #ifdef HAVE_LIBGEN_H | ||||
# include <libgen.h> | # include <libgen.h> | ||||
#endif | #endif | ||||
#ifdef HAVE_POLL_H | |||||
#include <poll.h> | #include <poll.h> | ||||
#endif | |||||
#include <signal.h> | #include <signal.h> | ||||
#include <stdarg.h> | #include <stdarg.h> | ||||
#include <stdio.h> | #include <stdio.h> | ||||
#include <stdlib.h> | #include <stdlib.h> | ||||
#include <string.h> | #include <string.h> | ||||
#include <time.h> | #include <time.h> | ||||
#include <unistd.h> | #include <unistd.h> | ||||
▲ Show 20 Lines • Show All 41 Lines • ▼ Show 20 Lines | |||||
/* set/unset filedescriptor to non-blocking */ | /* set/unset filedescriptor to non-blocking */ | ||||
int | int | ||||
set_nonblock(int fd) | set_nonblock(int fd) | ||||
{ | { | ||||
int val; | int val; | ||||
val = fcntl(fd, F_GETFL); | val = fcntl(fd, F_GETFL); | ||||
if (val < 0) { | if (val == -1) { | ||||
error("fcntl(%d, F_GETFL): %s", fd, strerror(errno)); | error("fcntl(%d, F_GETFL): %s", fd, strerror(errno)); | ||||
return (-1); | return (-1); | ||||
} | } | ||||
if (val & O_NONBLOCK) { | if (val & O_NONBLOCK) { | ||||
debug3("fd %d is O_NONBLOCK", fd); | debug3("fd %d is O_NONBLOCK", fd); | ||||
return (0); | return (0); | ||||
} | } | ||||
debug2("fd %d setting O_NONBLOCK", fd); | debug2("fd %d setting O_NONBLOCK", fd); | ||||
val |= O_NONBLOCK; | val |= O_NONBLOCK; | ||||
if (fcntl(fd, F_SETFL, val) == -1) { | if (fcntl(fd, F_SETFL, val) == -1) { | ||||
debug("fcntl(%d, F_SETFL, O_NONBLOCK): %s", fd, | debug("fcntl(%d, F_SETFL, O_NONBLOCK): %s", fd, | ||||
strerror(errno)); | strerror(errno)); | ||||
return (-1); | return (-1); | ||||
} | } | ||||
return (0); | return (0); | ||||
} | } | ||||
int | int | ||||
unset_nonblock(int fd) | unset_nonblock(int fd) | ||||
{ | { | ||||
int val; | int val; | ||||
val = fcntl(fd, F_GETFL); | val = fcntl(fd, F_GETFL); | ||||
if (val < 0) { | if (val == -1) { | ||||
error("fcntl(%d, F_GETFL): %s", fd, strerror(errno)); | error("fcntl(%d, F_GETFL): %s", fd, strerror(errno)); | ||||
return (-1); | return (-1); | ||||
} | } | ||||
if (!(val & O_NONBLOCK)) { | if (!(val & O_NONBLOCK)) { | ||||
debug3("fd %d is not O_NONBLOCK", fd); | debug3("fd %d is not O_NONBLOCK", fd); | ||||
return (0); | return (0); | ||||
} | } | ||||
debug("fd %d clearing O_NONBLOCK", fd); | debug("fd %d clearing O_NONBLOCK", fd); | ||||
▲ Show 20 Lines • Show All 99 Lines • ▼ Show 20 Lines | #elif defined(__OpenBSD__) | ||||
return 0; | return 0; | ||||
#else /* defined(__OpenBSD__) */ | #else /* defined(__OpenBSD__) */ | ||||
error("Setting routing domain is not supported on this platform"); | error("Setting routing domain is not supported on this platform"); | ||||
return -1; | return -1; | ||||
#endif | #endif | ||||
} | } | ||||
/* | /* | ||||
* Wait up to *timeoutp milliseconds for fd to be readable. Updates | * Wait up to *timeoutp milliseconds for events on fd. Updates | ||||
* *timeoutp with time remaining. | * *timeoutp with time remaining. | ||||
* Returns 0 if fd ready or -1 on timeout or error (see errno). | * Returns 0 if fd ready or -1 on timeout or error (see errno). | ||||
*/ | */ | ||||
int | static int | ||||
waitrfd(int fd, int *timeoutp) | waitfd(int fd, int *timeoutp, short events) | ||||
{ | { | ||||
struct pollfd pfd; | struct pollfd pfd; | ||||
struct timeval t_start; | struct timeval t_start; | ||||
int oerrno, r; | int oerrno, r; | ||||
monotime_tv(&t_start); | monotime_tv(&t_start); | ||||
pfd.fd = fd; | pfd.fd = fd; | ||||
pfd.events = POLLIN; | pfd.events = events; | ||||
for (; *timeoutp >= 0;) { | for (; *timeoutp >= 0;) { | ||||
r = poll(&pfd, 1, *timeoutp); | r = poll(&pfd, 1, *timeoutp); | ||||
oerrno = errno; | oerrno = errno; | ||||
ms_subtract_diff(&t_start, timeoutp); | ms_subtract_diff(&t_start, timeoutp); | ||||
errno = oerrno; | errno = oerrno; | ||||
if (r > 0) | if (r > 0) | ||||
return 0; | return 0; | ||||
else if (r == -1 && errno != EAGAIN) | else if (r == -1 && errno != EAGAIN && errno != EINTR) | ||||
return -1; | return -1; | ||||
else if (r == 0) | else if (r == 0) | ||||
break; | break; | ||||
} | } | ||||
/* timeout */ | /* timeout */ | ||||
errno = ETIMEDOUT; | errno = ETIMEDOUT; | ||||
return -1; | return -1; | ||||
} | } | ||||
/* | /* | ||||
* Wait up to *timeoutp milliseconds for fd to be readable. Updates | |||||
* *timeoutp with time remaining. | |||||
* Returns 0 if fd ready or -1 on timeout or error (see errno). | |||||
*/ | |||||
int | |||||
waitrfd(int fd, int *timeoutp) { | |||||
return waitfd(fd, timeoutp, POLLIN); | |||||
} | |||||
/* | |||||
* Attempt a non-blocking connect(2) to the specified address, waiting up to | * Attempt a non-blocking connect(2) to the specified address, waiting up to | ||||
* *timeoutp milliseconds for the connection to complete. If the timeout is | * *timeoutp milliseconds for the connection to complete. If the timeout is | ||||
* <=0, then wait indefinitely. | * <=0, then wait indefinitely. | ||||
* | * | ||||
* Returns 0 on success or -1 on failure. | * Returns 0 on success or -1 on failure. | ||||
*/ | */ | ||||
int | int | ||||
timeout_connect(int sockfd, const struct sockaddr *serv_addr, | timeout_connect(int sockfd, const struct sockaddr *serv_addr, | ||||
socklen_t addrlen, int *timeoutp) | socklen_t addrlen, int *timeoutp) | ||||
{ | { | ||||
int optval = 0; | int optval = 0; | ||||
socklen_t optlen = sizeof(optval); | socklen_t optlen = sizeof(optval); | ||||
/* No timeout: just do a blocking connect() */ | /* No timeout: just do a blocking connect() */ | ||||
if (timeoutp == NULL || *timeoutp <= 0) | if (timeoutp == NULL || *timeoutp <= 0) | ||||
return connect(sockfd, serv_addr, addrlen); | return connect(sockfd, serv_addr, addrlen); | ||||
set_nonblock(sockfd); | set_nonblock(sockfd); | ||||
for (;;) { | |||||
if (connect(sockfd, serv_addr, addrlen) == 0) { | if (connect(sockfd, serv_addr, addrlen) == 0) { | ||||
/* Succeeded already? */ | /* Succeeded already? */ | ||||
unset_nonblock(sockfd); | unset_nonblock(sockfd); | ||||
return 0; | return 0; | ||||
} else if (errno != EINPROGRESS) | } else if (errno == EINTR) | ||||
continue; | |||||
else if (errno != EINPROGRESS) | |||||
return -1; | return -1; | ||||
break; | |||||
} | |||||
if (waitrfd(sockfd, timeoutp) == -1) | if (waitfd(sockfd, timeoutp, POLLIN | POLLOUT) == -1) | ||||
return -1; | return -1; | ||||
/* Completed or failed */ | /* Completed or failed */ | ||||
if (getsockopt(sockfd, SOL_SOCKET, SO_ERROR, &optval, &optlen) == -1) { | if (getsockopt(sockfd, SOL_SOCKET, SO_ERROR, &optval, &optlen) == -1) { | ||||
debug("getsockopt: %s", strerror(errno)); | debug("getsockopt: %s", strerror(errno)); | ||||
return -1; | return -1; | ||||
} | } | ||||
if (optval != 0) { | if (optval != 0) { | ||||
▲ Show 20 Lines • Show All 171 Lines • ▼ Show 20 Lines | |||||
* 2d 2 days | * 2d 2 days | ||||
* 1w 1 week | * 1w 1 week | ||||
* | * | ||||
* Return -1 if time string is invalid. | * Return -1 if time string is invalid. | ||||
*/ | */ | ||||
long | long | ||||
convtime(const char *s) | convtime(const char *s) | ||||
{ | { | ||||
long total, secs, multiplier = 1; | long total, secs, multiplier; | ||||
const char *p; | const char *p; | ||||
char *endp; | char *endp; | ||||
errno = 0; | errno = 0; | ||||
total = 0; | total = 0; | ||||
p = s; | p = s; | ||||
if (p == NULL || *p == '\0') | if (p == NULL || *p == '\0') | ||||
return -1; | return -1; | ||||
while (*p) { | while (*p) { | ||||
secs = strtol(p, &endp, 10); | secs = strtol(p, &endp, 10); | ||||
if (p == endp || | if (p == endp || | ||||
(errno == ERANGE && (secs == LONG_MIN || secs == LONG_MAX)) || | (errno == ERANGE && (secs == LONG_MIN || secs == LONG_MAX)) || | ||||
secs < 0) | secs < 0) | ||||
return -1; | return -1; | ||||
multiplier = 1; | |||||
switch (*endp++) { | switch (*endp++) { | ||||
case '\0': | case '\0': | ||||
endp--; | endp--; | ||||
break; | break; | ||||
case 's': | case 's': | ||||
case 'S': | case 'S': | ||||
break; | break; | ||||
case 'm': | case 'm': | ||||
Show All 24 Lines | while (*p) { | ||||
if (total < 0) | if (total < 0) | ||||
return -1; | return -1; | ||||
p = endp; | p = endp; | ||||
} | } | ||||
return total; | return total; | ||||
} | } | ||||
#define TF_BUFS 8 | |||||
#define TF_LEN 9 | |||||
const char * | |||||
fmt_timeframe(time_t t) | |||||
{ | |||||
char *buf; | |||||
static char tfbuf[TF_BUFS][TF_LEN]; /* ring buffer */ | |||||
static int idx = 0; | |||||
unsigned int sec, min, hrs, day; | |||||
unsigned long long week; | |||||
buf = tfbuf[idx++]; | |||||
if (idx == TF_BUFS) | |||||
idx = 0; | |||||
week = t; | |||||
sec = week % 60; | |||||
week /= 60; | |||||
min = week % 60; | |||||
week /= 60; | |||||
hrs = week % 24; | |||||
week /= 24; | |||||
day = week % 7; | |||||
week /= 7; | |||||
if (week > 0) | |||||
snprintf(buf, TF_LEN, "%02lluw%01ud%02uh", week, day, hrs); | |||||
else if (day > 0) | |||||
snprintf(buf, TF_LEN, "%01ud%02uh%02um", day, hrs, min); | |||||
else | |||||
snprintf(buf, TF_LEN, "%02u:%02u:%02u", hrs, min, sec); | |||||
return (buf); | |||||
} | |||||
/* | /* | ||||
* Returns a standardized host+port identifier string. | * Returns a standardized host+port identifier string. | ||||
* Caller must free returned string. | * Caller must free returned string. | ||||
*/ | */ | ||||
char * | char * | ||||
put_host_port(const char *host, u_short port) | put_host_port(const char *host, u_short port) | ||||
{ | { | ||||
char *hoststr; | char *hoststr; | ||||
if (port == 0 || port == SSH_DEFAULT_PORT) | if (port == 0 || port == SSH_DEFAULT_PORT) | ||||
return(xstrdup(host)); | return(xstrdup(host)); | ||||
if (asprintf(&hoststr, "[%s]:%d", host, (int)port) < 0) | if (asprintf(&hoststr, "[%s]:%d", host, (int)port) == -1) | ||||
fatal("put_host_port: asprintf: %s", strerror(errno)); | fatal("put_host_port: asprintf: %s", strerror(errno)); | ||||
debug3("put_host_port: %s", hoststr); | debug3("put_host_port: %s", hoststr); | ||||
return hoststr; | return hoststr; | ||||
} | } | ||||
/* | /* | ||||
* Search for next delimiter between hostnames/addresses and ports. | * Search for next delimiter between hostnames/addresses and ports. | ||||
* Argument may be modified (for termination). | * Argument may be modified (for termination). | ||||
▲ Show 20 Lines • Show All 473 Lines • ▼ Show 20 Lines | tilde_expand_filename(const char *filename, uid_t uid) | ||||
if (xasprintf(&ret, "%s%s%s", pw->pw_dir, sep, filename) >= PATH_MAX) | if (xasprintf(&ret, "%s%s%s", pw->pw_dir, sep, filename) >= PATH_MAX) | ||||
fatal("tilde_expand_filename: Path too long"); | fatal("tilde_expand_filename: Path too long"); | ||||
return (ret); | return (ret); | ||||
} | } | ||||
/* | /* | ||||
* Expand a string with a set of %[char] escapes. A number of escapes may be | * Expand a string with a set of %[char] escapes and/or ${ENVIRONMENT} | ||||
* specified as (char *escape_chars, char *replacement) pairs. The list must | * substitutions. A number of escapes may be specified as | ||||
* be terminated by a NULL escape_char. Returns replaced string in memory | * (char *escape_chars, char *replacement) pairs. The list must be terminated | ||||
* allocated by xmalloc. | * by a NULL escape_char. Returns replaced string in memory allocated by | ||||
* xmalloc which the caller must free. | |||||
*/ | */ | ||||
char * | static char * | ||||
percent_expand(const char *string, ...) | vdollar_percent_expand(int *parseerror, int dollar, int percent, | ||||
const char *string, va_list ap) | |||||
{ | { | ||||
#define EXPAND_MAX_KEYS 16 | #define EXPAND_MAX_KEYS 16 | ||||
u_int num_keys, i, j; | u_int num_keys = 0, i; | ||||
struct { | struct { | ||||
const char *key; | const char *key; | ||||
const char *repl; | const char *repl; | ||||
} keys[EXPAND_MAX_KEYS]; | } keys[EXPAND_MAX_KEYS]; | ||||
char buf[4096]; | struct sshbuf *buf; | ||||
va_list ap; | int r, missingvar = 0; | ||||
char *ret = NULL, *var, *varend, *val; | |||||
size_t len; | |||||
/* Gather keys */ | if ((buf = sshbuf_new()) == NULL) | ||||
va_start(ap, string); | fatal("%s: sshbuf_new failed", __func__); | ||||
if (parseerror == NULL) | |||||
fatal("%s: null parseerror arg", __func__); | |||||
*parseerror = 1; | |||||
/* Gather keys if we're doing percent expansion. */ | |||||
if (percent) { | |||||
for (num_keys = 0; num_keys < EXPAND_MAX_KEYS; num_keys++) { | for (num_keys = 0; num_keys < EXPAND_MAX_KEYS; num_keys++) { | ||||
keys[num_keys].key = va_arg(ap, char *); | keys[num_keys].key = va_arg(ap, char *); | ||||
if (keys[num_keys].key == NULL) | if (keys[num_keys].key == NULL) | ||||
break; | break; | ||||
keys[num_keys].repl = va_arg(ap, char *); | keys[num_keys].repl = va_arg(ap, char *); | ||||
if (keys[num_keys].repl == NULL) | if (keys[num_keys].repl == NULL) | ||||
fatal("%s: NULL replacement", __func__); | fatal("%s: NULL replacement for token %s", __func__, keys[num_keys].key); | ||||
} | } | ||||
if (num_keys == EXPAND_MAX_KEYS && va_arg(ap, char *) != NULL) | if (num_keys == EXPAND_MAX_KEYS && va_arg(ap, char *) != NULL) | ||||
fatal("%s: too many keys", __func__); | fatal("%s: too many keys", __func__); | ||||
va_end(ap); | if (num_keys == 0) | ||||
fatal("%s: percent expansion without token list", | |||||
__func__); | |||||
} | |||||
/* Expand string */ | /* Expand string */ | ||||
*buf = '\0'; | |||||
for (i = 0; *string != '\0'; string++) { | for (i = 0; *string != '\0'; string++) { | ||||
if (*string != '%') { | /* Optionally process ${ENVIRONMENT} expansions. */ | ||||
if (dollar && string[0] == '$' && string[1] == '{') { | |||||
string += 2; /* skip over '${' */ | |||||
if ((varend = strchr(string, '}')) == NULL) { | |||||
error("%s: environment variable '%s' missing " | |||||
"closing '}'", __func__, string); | |||||
goto out; | |||||
} | |||||
len = varend - string; | |||||
if (len == 0) { | |||||
error("%s: zero-length environment variable", | |||||
__func__); | |||||
goto out; | |||||
} | |||||
var = xmalloc(len + 1); | |||||
(void)strlcpy(var, string, len + 1); | |||||
if ((val = getenv(var)) == NULL) { | |||||
error("%s: env var ${%s} has no value", | |||||
__func__, var); | |||||
missingvar = 1; | |||||
} else { | |||||
debug3("%s: expand ${%s} -> '%s'", __func__, | |||||
var, val); | |||||
if ((r = sshbuf_put(buf, val, strlen(val))) !=0) | |||||
fatal("%s: sshbuf_put: %s", __func__, | |||||
ssh_err(r)); | |||||
} | |||||
free(var); | |||||
string += len; | |||||
continue; | |||||
} | |||||
/* | |||||
* Process percent expansions if we have a list of TOKENs. | |||||
* If we're not doing percent expansion everything just gets | |||||
* appended here. | |||||
*/ | |||||
if (*string != '%' || !percent) { | |||||
append: | append: | ||||
buf[i++] = *string; | if ((r = sshbuf_put_u8(buf, *string)) != 0) { | ||||
if (i >= sizeof(buf)) | fatal("%s: sshbuf_put_u8: %s", | ||||
fatal("%s: string too long", __func__); | __func__, ssh_err(r)); | ||||
buf[i] = '\0'; | } | ||||
continue; | continue; | ||||
} | } | ||||
string++; | string++; | ||||
/* %% case */ | /* %% case */ | ||||
if (*string == '%') | if (*string == '%') | ||||
goto append; | goto append; | ||||
if (*string == '\0') | if (*string == '\0') { | ||||
fatal("%s: invalid format", __func__); | error("%s: invalid format", __func__); | ||||
for (j = 0; j < num_keys; j++) { | goto out; | ||||
if (strchr(keys[j].key, *string) != NULL) { | } | ||||
i = strlcat(buf, keys[j].repl, sizeof(buf)); | for (i = 0; i < num_keys; i++) { | ||||
if (i >= sizeof(buf)) | if (strchr(keys[i].key, *string) != NULL) { | ||||
fatal("%s: string too long", __func__); | if ((r = sshbuf_put(buf, keys[i].repl, | ||||
strlen(keys[i].repl))) != 0) { | |||||
fatal("%s: sshbuf_put: %s", | |||||
__func__, ssh_err(r)); | |||||
} | |||||
break; | break; | ||||
} | } | ||||
} | } | ||||
if (j >= num_keys) | if (i >= num_keys) { | ||||
fatal("%s: unknown key %%%c", __func__, *string); | error("%s: unknown key %%%c", __func__, *string); | ||||
goto out; | |||||
} | } | ||||
return (xstrdup(buf)); | } | ||||
if (!missingvar && (ret = sshbuf_dup_string(buf)) == NULL) | |||||
fatal("%s: sshbuf_dup_string failed", __func__); | |||||
*parseerror = 0; | |||||
out: | |||||
sshbuf_free(buf); | |||||
return *parseerror ? NULL : ret; | |||||
#undef EXPAND_MAX_KEYS | #undef EXPAND_MAX_KEYS | ||||
} | } | ||||
/* | |||||
* Expand only environment variables. | |||||
* Note that although this function is variadic like the other similar | |||||
* functions, any such arguments will be unused. | |||||
*/ | |||||
char * | |||||
dollar_expand(int *parseerr, const char *string, ...) | |||||
{ | |||||
char *ret; | |||||
int err; | |||||
va_list ap; | |||||
va_start(ap, string); | |||||
ret = vdollar_percent_expand(&err, 1, 0, string, ap); | |||||
va_end(ap); | |||||
if (parseerr != NULL) | |||||
*parseerr = err; | |||||
return ret; | |||||
} | |||||
/* | |||||
* Returns expanded string or NULL if a specified environment variable is | |||||
* not defined, or calls fatal if the string is invalid. | |||||
*/ | |||||
char * | |||||
percent_expand(const char *string, ...) | |||||
{ | |||||
char *ret; | |||||
int err; | |||||
va_list ap; | |||||
va_start(ap, string); | |||||
ret = vdollar_percent_expand(&err, 0, 1, string, ap); | |||||
va_end(ap); | |||||
if (err) | |||||
fatal("%s failed", __func__); | |||||
return ret; | |||||
} | |||||
/* | |||||
* Returns expanded string or NULL if a specified environment variable is | |||||
* not defined, or calls fatal if the string is invalid. | |||||
*/ | |||||
char * | |||||
percent_dollar_expand(const char *string, ...) | |||||
{ | |||||
char *ret; | |||||
int err; | |||||
va_list ap; | |||||
va_start(ap, string); | |||||
ret = vdollar_percent_expand(&err, 1, 1, string, ap); | |||||
va_end(ap); | |||||
if (err) | |||||
fatal("%s failed", __func__); | |||||
return ret; | |||||
} | |||||
int | int | ||||
tun_open(int tun, int mode, char **ifname) | tun_open(int tun, int mode, char **ifname) | ||||
{ | { | ||||
#if defined(CUSTOM_SYS_TUN_OPEN) | #if defined(CUSTOM_SYS_TUN_OPEN) | ||||
return (sys_tun_open(tun, mode, ifname)); | return (sys_tun_open(tun, mode, ifname)); | ||||
#elif defined(SSH_TUN_OPENBSD) | #elif defined(SSH_TUN_OPENBSD) | ||||
struct ifreq ifr; | struct ifreq ifr; | ||||
char name[100]; | char name[100]; | ||||
Show All 17 Lines | for (tun = 100; tun >= 0; tun--) { | ||||
if ((fd = open(name, O_RDWR)) >= 0) | if ((fd = open(name, O_RDWR)) >= 0) | ||||
break; | break; | ||||
} | } | ||||
} else { | } else { | ||||
debug("%s: invalid tunnel %u", __func__, tun); | debug("%s: invalid tunnel %u", __func__, tun); | ||||
return -1; | return -1; | ||||
} | } | ||||
if (fd < 0) { | if (fd == -1) { | ||||
debug("%s: %s open: %s", __func__, name, strerror(errno)); | debug("%s: %s open: %s", __func__, name, strerror(errno)); | ||||
return -1; | return -1; | ||||
} | } | ||||
debug("%s: %s mode %d fd %d", __func__, name, mode, fd); | debug("%s: %s mode %d fd %d", __func__, name, mode, fd); | ||||
/* Bring interface up if it is not already */ | /* Bring interface up if it is not already */ | ||||
snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "%s%d", tunbase, tun); | snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "%s%d", tunbase, tun); | ||||
▲ Show 20 Lines • Show All 70 Lines • ▼ Show 20 Lines | tohex(const void *vp, size_t l) | ||||
r = xcalloc(1, hl); | r = xcalloc(1, hl); | ||||
for (i = 0; i < l; i++) { | for (i = 0; i < l; i++) { | ||||
snprintf(b, sizeof(b), "%02x", p[i]); | snprintf(b, sizeof(b), "%02x", p[i]); | ||||
strlcat(r, b, hl); | strlcat(r, b, hl); | ||||
} | } | ||||
return (r); | return (r); | ||||
} | } | ||||
/* | |||||
* Extend string *sp by the specified format. If *sp is not NULL (or empty), | |||||
* then the separator 'sep' will be prepended before the formatted arguments. | |||||
* Extended strings are heap allocated. | |||||
*/ | |||||
void | |||||
xextendf(char **sp, const char *sep, const char *fmt, ...) | |||||
{ | |||||
va_list ap; | |||||
char *tmp1, *tmp2; | |||||
va_start(ap, fmt); | |||||
xvasprintf(&tmp1, fmt, ap); | |||||
va_end(ap); | |||||
if (*sp == NULL || **sp == '\0') { | |||||
free(*sp); | |||||
*sp = tmp1; | |||||
return; | |||||
} | |||||
xasprintf(&tmp2, "%s%s%s", *sp, sep == NULL ? "" : sep, tmp1); | |||||
free(tmp1); | |||||
free(*sp); | |||||
*sp = tmp2; | |||||
} | |||||
u_int64_t | u_int64_t | ||||
get_u64(const void *vp) | get_u64(const void *vp) | ||||
{ | { | ||||
const u_char *p = (const u_char *)vp; | const u_char *p = (const u_char *)vp; | ||||
u_int64_t v; | u_int64_t v; | ||||
v = (u_int64_t)p[0] << 56; | v = (u_int64_t)p[0] << 56; | ||||
v |= (u_int64_t)p[1] << 48; | v |= (u_int64_t)p[1] << 48; | ||||
▲ Show 20 Lines • Show All 272 Lines • ▼ Show 20 Lines | static const struct { | ||||
{ "cs1", IPTOS_DSCP_CS1 }, | { "cs1", IPTOS_DSCP_CS1 }, | ||||
{ "cs2", IPTOS_DSCP_CS2 }, | { "cs2", IPTOS_DSCP_CS2 }, | ||||
{ "cs3", IPTOS_DSCP_CS3 }, | { "cs3", IPTOS_DSCP_CS3 }, | ||||
{ "cs4", IPTOS_DSCP_CS4 }, | { "cs4", IPTOS_DSCP_CS4 }, | ||||
{ "cs5", IPTOS_DSCP_CS5 }, | { "cs5", IPTOS_DSCP_CS5 }, | ||||
{ "cs6", IPTOS_DSCP_CS6 }, | { "cs6", IPTOS_DSCP_CS6 }, | ||||
{ "cs7", IPTOS_DSCP_CS7 }, | { "cs7", IPTOS_DSCP_CS7 }, | ||||
{ "ef", IPTOS_DSCP_EF }, | { "ef", IPTOS_DSCP_EF }, | ||||
{ "le", IPTOS_DSCP_LE }, | |||||
{ "lowdelay", IPTOS_LOWDELAY }, | { "lowdelay", IPTOS_LOWDELAY }, | ||||
{ "throughput", IPTOS_THROUGHPUT }, | { "throughput", IPTOS_THROUGHPUT }, | ||||
{ "reliability", IPTOS_RELIABILITY }, | { "reliability", IPTOS_RELIABILITY }, | ||||
{ NULL, -1 } | { NULL, -1 } | ||||
}; | }; | ||||
int | int | ||||
parse_ipqos(const char *cp) | parse_ipqos(const char *cp) | ||||
▲ Show 20 Lines • Show All 48 Lines • ▼ Show 20 Lines | if (strlcpy(sunaddr.sun_path, path, | ||||
sizeof(sunaddr.sun_path)) >= sizeof(sunaddr.sun_path)) { | sizeof(sunaddr.sun_path)) >= sizeof(sunaddr.sun_path)) { | ||||
error("%s: path \"%s\" too long for Unix domain socket", | error("%s: path \"%s\" too long for Unix domain socket", | ||||
__func__, path); | __func__, path); | ||||
errno = ENAMETOOLONG; | errno = ENAMETOOLONG; | ||||
return -1; | return -1; | ||||
} | } | ||||
sock = socket(PF_UNIX, SOCK_STREAM, 0); | sock = socket(PF_UNIX, SOCK_STREAM, 0); | ||||
if (sock < 0) { | if (sock == -1) { | ||||
saved_errno = errno; | saved_errno = errno; | ||||
error("%s: socket: %.100s", __func__, strerror(errno)); | error("%s: socket: %.100s", __func__, strerror(errno)); | ||||
errno = saved_errno; | errno = saved_errno; | ||||
return -1; | return -1; | ||||
} | } | ||||
if (unlink_first == 1) { | if (unlink_first == 1) { | ||||
if (unlink(path) != 0 && errno != ENOENT) | if (unlink(path) != 0 && errno != ENOENT) | ||||
error("unlink(%s): %.100s", path, strerror(errno)); | error("unlink(%s): %.100s", path, strerror(errno)); | ||||
} | } | ||||
if (bind(sock, (struct sockaddr *)&sunaddr, sizeof(sunaddr)) < 0) { | if (bind(sock, (struct sockaddr *)&sunaddr, sizeof(sunaddr)) == -1) { | ||||
saved_errno = errno; | saved_errno = errno; | ||||
error("%s: cannot bind to path %s: %s", | error("%s: cannot bind to path %s: %s", | ||||
__func__, path, strerror(errno)); | __func__, path, strerror(errno)); | ||||
close(sock); | close(sock); | ||||
errno = saved_errno; | errno = saved_errno; | ||||
return -1; | return -1; | ||||
} | } | ||||
if (listen(sock, backlog) < 0) { | if (listen(sock, backlog) == -1) { | ||||
saved_errno = errno; | saved_errno = errno; | ||||
error("%s: cannot listen on path %s: %s", | error("%s: cannot listen on path %s: %s", | ||||
__func__, path, strerror(errno)); | __func__, path, strerror(errno)); | ||||
close(sock); | close(sock); | ||||
unlink(path); | unlink(path); | ||||
errno = saved_errno; | errno = saved_errno; | ||||
return -1; | return -1; | ||||
} | } | ||||
▲ Show 20 Lines • Show All 265 Lines • ▼ Show 20 Lines | safe_path(const char *name, struct stat *stp, const char *pw_dir, | ||||
/* for each component of the canonical path, walking upwards */ | /* for each component of the canonical path, walking upwards */ | ||||
for (;;) { | for (;;) { | ||||
if ((cp = dirname(buf)) == NULL) { | if ((cp = dirname(buf)) == NULL) { | ||||
snprintf(err, errlen, "dirname() failed"); | snprintf(err, errlen, "dirname() failed"); | ||||
return -1; | return -1; | ||||
} | } | ||||
strlcpy(buf, cp, sizeof(buf)); | strlcpy(buf, cp, sizeof(buf)); | ||||
if (stat(buf, &st) < 0 || | if (stat(buf, &st) == -1 || | ||||
(!platform_sys_dir_uid(st.st_uid) && st.st_uid != uid) || | (!platform_sys_dir_uid(st.st_uid) && st.st_uid != uid) || | ||||
(st.st_mode & 022) != 0) { | (st.st_mode & 022) != 0) { | ||||
snprintf(err, errlen, | snprintf(err, errlen, | ||||
"bad ownership or modes for directory %s", buf); | "bad ownership or modes for directory %s", buf); | ||||
return -1; | return -1; | ||||
} | } | ||||
/* If are past the homedir then we can stop */ | /* If are past the homedir then we can stop */ | ||||
Show All 18 Lines | |||||
*/ | */ | ||||
int | int | ||||
safe_path_fd(int fd, const char *file, struct passwd *pw, | safe_path_fd(int fd, const char *file, struct passwd *pw, | ||||
char *err, size_t errlen) | char *err, size_t errlen) | ||||
{ | { | ||||
struct stat st; | struct stat st; | ||||
/* check the open file to avoid races */ | /* check the open file to avoid races */ | ||||
if (fstat(fd, &st) < 0) { | if (fstat(fd, &st) == -1) { | ||||
snprintf(err, errlen, "cannot stat file %s: %s", | snprintf(err, errlen, "cannot stat file %s: %s", | ||||
file, strerror(errno)); | file, strerror(errno)); | ||||
return -1; | return -1; | ||||
} | } | ||||
return safe_path(file, &st, pw->pw_dir, pw->pw_uid, err, errlen); | return safe_path(file, &st, pw->pw_dir, pw->pw_uid, err, errlen); | ||||
} | } | ||||
/* | /* | ||||
▲ Show 20 Lines • Show All 190 Lines • ▼ Show 20 Lines | format_absolute_time(uint64_t t, char *buf, size_t len) | ||||
strftime(buf, len, "%Y-%m-%dT%H:%M:%S", &tm); | strftime(buf, len, "%Y-%m-%dT%H:%M:%S", &tm); | ||||
} | } | ||||
/* check if path is absolute */ | /* check if path is absolute */ | ||||
int | int | ||||
path_absolute(const char *path) | path_absolute(const char *path) | ||||
{ | { | ||||
return (*path == '/') ? 1 : 0; | return (*path == '/') ? 1 : 0; | ||||
} | |||||
void | |||||
skip_space(char **cpp) | |||||
{ | |||||
char *cp; | |||||
for (cp = *cpp; *cp == ' ' || *cp == '\t'; cp++) | |||||
; | |||||
*cpp = cp; | |||||
} | |||||
/* authorized_key-style options parsing helpers */ | |||||
/* | |||||
* Match flag 'opt' in *optsp, and if allow_negate is set then also match | |||||
* 'no-opt'. Returns -1 if option not matched, 1 if option matches or 0 | |||||
* if negated option matches. | |||||
* If the option or negated option matches, then *optsp is updated to | |||||
* point to the first character after the option. | |||||
*/ | |||||
int | |||||
opt_flag(const char *opt, int allow_negate, const char **optsp) | |||||
{ | |||||
size_t opt_len = strlen(opt); | |||||
const char *opts = *optsp; | |||||
int negate = 0; | |||||
if (allow_negate && strncasecmp(opts, "no-", 3) == 0) { | |||||
opts += 3; | |||||
negate = 1; | |||||
} | |||||
if (strncasecmp(opts, opt, opt_len) == 0) { | |||||
*optsp = opts + opt_len; | |||||
return negate ? 0 : 1; | |||||
} | |||||
return -1; | |||||
} | |||||
char * | |||||
opt_dequote(const char **sp, const char **errstrp) | |||||
{ | |||||
const char *s = *sp; | |||||
char *ret; | |||||
size_t i; | |||||
*errstrp = NULL; | |||||
if (*s != '"') { | |||||
*errstrp = "missing start quote"; | |||||
return NULL; | |||||
} | |||||
s++; | |||||
if ((ret = malloc(strlen((s)) + 1)) == NULL) { | |||||
*errstrp = "memory allocation failed"; | |||||
return NULL; | |||||
} | |||||
for (i = 0; *s != '\0' && *s != '"';) { | |||||
if (s[0] == '\\' && s[1] == '"') | |||||
s++; | |||||
ret[i++] = *s++; | |||||
} | |||||
if (*s == '\0') { | |||||
*errstrp = "missing end quote"; | |||||
free(ret); | |||||
return NULL; | |||||
} | |||||
ret[i] = '\0'; | |||||
s++; | |||||
*sp = s; | |||||
return ret; | |||||
} | |||||
int | |||||
opt_match(const char **opts, const char *term) | |||||
{ | |||||
if (strncasecmp((*opts), term, strlen(term)) == 0 && | |||||
(*opts)[strlen(term)] == '=') { | |||||
*opts += strlen(term) + 1; | |||||
return 1; | |||||
} | |||||
return 0; | |||||
} | |||||
sshsig_t | |||||
ssh_signal(int signum, sshsig_t handler) | |||||
{ | |||||
struct sigaction sa, osa; | |||||
/* mask all other signals while in handler */ | |||||
memset(&sa, 0, sizeof(sa)); | |||||
sa.sa_handler = handler; | |||||
sigfillset(&sa.sa_mask); | |||||
#if defined(SA_RESTART) && !defined(NO_SA_RESTART) | |||||
if (signum != SIGALRM) | |||||
sa.sa_flags = SA_RESTART; | |||||
#endif | |||||
if (sigaction(signum, &sa, &osa) == -1) { | |||||
debug3("sigaction(%s): %s", strsignal(signum), strerror(errno)); | |||||
return SIG_ERR; | |||||
} | |||||
return osa.sa_handler; | |||||
} | } |