1*0ae642c7SEd Maste /* $OpenBSD: misc.c,v 1.197 2024/09/25 01:24:04 djm Exp $ */
21e8db6e2SBrian Feldman /*
31e8db6e2SBrian Feldman * Copyright (c) 2000 Markus Friedl. All rights reserved.
419261079SEd Maste * Copyright (c) 2005-2020 Damien Miller. All rights reserved.
519261079SEd Maste * Copyright (c) 2004 Henning Brauer <henning@openbsd.org>
61e8db6e2SBrian Feldman *
719261079SEd Maste * Permission to use, copy, modify, and distribute this software for any
819261079SEd Maste * purpose with or without fee is hereby granted, provided that the above
919261079SEd Maste * copyright notice and this permission notice appear in all copies.
101e8db6e2SBrian Feldman *
1119261079SEd Maste * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
1219261079SEd Maste * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1319261079SEd Maste * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1419261079SEd Maste * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1519261079SEd Maste * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1619261079SEd Maste * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1719261079SEd Maste * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
181e8db6e2SBrian Feldman */
191e8db6e2SBrian Feldman
2019261079SEd Maste
211e8db6e2SBrian Feldman #include "includes.h"
22021d409fSDag-Erling Smørgrav
23761efaa7SDag-Erling Smørgrav #include <sys/types.h>
24761efaa7SDag-Erling Smørgrav #include <sys/ioctl.h>
25535af610SEd Maste #include <sys/mman.h>
26761efaa7SDag-Erling Smørgrav #include <sys/socket.h>
274f52dfbbSDag-Erling Smørgrav #include <sys/stat.h>
28acc1a9efSDag-Erling Smørgrav #include <sys/time.h>
294f52dfbbSDag-Erling Smørgrav #include <sys/wait.h>
30a0ee8cc6SDag-Erling Smørgrav #include <sys/un.h>
31761efaa7SDag-Erling Smørgrav
32bc5531deSDag-Erling Smørgrav #include <limits.h>
334f52dfbbSDag-Erling Smørgrav #ifdef HAVE_LIBGEN_H
344f52dfbbSDag-Erling Smørgrav # include <libgen.h>
354f52dfbbSDag-Erling Smørgrav #endif
3619261079SEd Maste #ifdef HAVE_POLL_H
3719261079SEd Maste #include <poll.h>
3819261079SEd Maste #endif
39535af610SEd Maste #ifdef HAVE_NLIST_H
40535af610SEd Maste #include <nlist.h>
41535af610SEd Maste #endif
424f52dfbbSDag-Erling Smørgrav #include <signal.h>
43761efaa7SDag-Erling Smørgrav #include <stdarg.h>
44761efaa7SDag-Erling Smørgrav #include <stdio.h>
45535af610SEd Maste #ifdef HAVE_STDINT_H
46535af610SEd Maste # include <stdint.h>
47535af610SEd Maste #endif
48761efaa7SDag-Erling Smørgrav #include <stdlib.h>
49761efaa7SDag-Erling Smørgrav #include <string.h>
504a421b63SDag-Erling Smørgrav #include <time.h>
51761efaa7SDag-Erling Smørgrav #include <unistd.h>
52761efaa7SDag-Erling Smørgrav
53761efaa7SDag-Erling Smørgrav #include <netinet/in.h>
544a421b63SDag-Erling Smørgrav #include <netinet/in_systm.h>
554a421b63SDag-Erling Smørgrav #include <netinet/ip.h>
56761efaa7SDag-Erling Smørgrav #include <netinet/tcp.h>
572f513db7SEd Maste #include <arpa/inet.h>
58761efaa7SDag-Erling Smørgrav
59f7167e0eSDag-Erling Smørgrav #include <ctype.h>
60761efaa7SDag-Erling Smørgrav #include <errno.h>
61761efaa7SDag-Erling Smørgrav #include <fcntl.h>
62d4af9e69SDag-Erling Smørgrav #include <netdb.h>
63761efaa7SDag-Erling Smørgrav #ifdef HAVE_PATHS_H
64761efaa7SDag-Erling Smørgrav # include <paths.h>
65761efaa7SDag-Erling Smørgrav #include <pwd.h>
66172fa4aaSEd Maste #include <grp.h>
67761efaa7SDag-Erling Smørgrav #endif
68021d409fSDag-Erling Smørgrav #ifdef SSH_TUN_OPENBSD
69021d409fSDag-Erling Smørgrav #include <net/if.h>
70021d409fSDag-Erling Smørgrav #endif
711e8db6e2SBrian Feldman
72761efaa7SDag-Erling Smørgrav #include "xmalloc.h"
731e8db6e2SBrian Feldman #include "misc.h"
741e8db6e2SBrian Feldman #include "log.h"
75761efaa7SDag-Erling Smørgrav #include "ssh.h"
764f52dfbbSDag-Erling Smørgrav #include "sshbuf.h"
774f52dfbbSDag-Erling Smørgrav #include "ssherr.h"
784f52dfbbSDag-Erling Smørgrav #include "platform.h"
791e8db6e2SBrian Feldman
80ae1f160dSDag-Erling Smørgrav /* remove newline at end of string */
811e8db6e2SBrian Feldman char *
chop(char * s)821e8db6e2SBrian Feldman chop(char *s)
831e8db6e2SBrian Feldman {
841e8db6e2SBrian Feldman char *t = s;
851e8db6e2SBrian Feldman while (*t) {
861e8db6e2SBrian Feldman if (*t == '\n' || *t == '\r') {
871e8db6e2SBrian Feldman *t = '\0';
881e8db6e2SBrian Feldman return s;
891e8db6e2SBrian Feldman }
901e8db6e2SBrian Feldman t++;
911e8db6e2SBrian Feldman }
921e8db6e2SBrian Feldman return s;
931e8db6e2SBrian Feldman
941e8db6e2SBrian Feldman }
951e8db6e2SBrian Feldman
9619261079SEd Maste /* remove whitespace from end of string */
9719261079SEd Maste void
rtrim(char * s)9819261079SEd Maste rtrim(char *s)
9919261079SEd Maste {
10019261079SEd Maste size_t i;
10119261079SEd Maste
10219261079SEd Maste if ((i = strlen(s)) == 0)
10319261079SEd Maste return;
10419261079SEd Maste for (i--; i > 0; i--) {
105f374ba41SEd Maste if (isspace((unsigned char)s[i]))
10619261079SEd Maste s[i] = '\0';
10719261079SEd Maste }
10819261079SEd Maste }
10919261079SEd Maste
110*0ae642c7SEd Maste /*
111*0ae642c7SEd Maste * returns pointer to character after 'prefix' in 's' or otherwise NULL
112*0ae642c7SEd Maste * if the prefix is not present.
113*0ae642c7SEd Maste */
114*0ae642c7SEd Maste const char *
strprefix(const char * s,const char * prefix,int ignorecase)115*0ae642c7SEd Maste strprefix(const char *s, const char *prefix, int ignorecase)
116*0ae642c7SEd Maste {
117*0ae642c7SEd Maste size_t prefixlen;
118*0ae642c7SEd Maste
119*0ae642c7SEd Maste if ((prefixlen = strlen(prefix)) == 0)
120*0ae642c7SEd Maste return s;
121*0ae642c7SEd Maste if (ignorecase) {
122*0ae642c7SEd Maste if (strncasecmp(s, prefix, prefixlen) != 0)
123*0ae642c7SEd Maste return NULL;
124*0ae642c7SEd Maste } else {
125*0ae642c7SEd Maste if (strncmp(s, prefix, prefixlen) != 0)
126*0ae642c7SEd Maste return NULL;
127*0ae642c7SEd Maste }
128*0ae642c7SEd Maste return s + prefixlen;
129*0ae642c7SEd Maste }
130*0ae642c7SEd Maste
131ae1f160dSDag-Erling Smørgrav /* set/unset filedescriptor to non-blocking */
132d74d50a8SDag-Erling Smørgrav int
set_nonblock(int fd)1331e8db6e2SBrian Feldman set_nonblock(int fd)
1341e8db6e2SBrian Feldman {
1351e8db6e2SBrian Feldman int val;
136ae1f160dSDag-Erling Smørgrav
137076ad2f8SDag-Erling Smørgrav val = fcntl(fd, F_GETFL);
13819261079SEd Maste if (val == -1) {
139076ad2f8SDag-Erling Smørgrav error("fcntl(%d, F_GETFL): %s", fd, strerror(errno));
140d74d50a8SDag-Erling Smørgrav return (-1);
1411e8db6e2SBrian Feldman }
1421e8db6e2SBrian Feldman if (val & O_NONBLOCK) {
143d74d50a8SDag-Erling Smørgrav debug3("fd %d is O_NONBLOCK", fd);
144d74d50a8SDag-Erling Smørgrav return (0);
1451e8db6e2SBrian Feldman }
146d95e11bfSDag-Erling Smørgrav debug2("fd %d setting O_NONBLOCK", fd);
1471e8db6e2SBrian Feldman val |= O_NONBLOCK;
148d74d50a8SDag-Erling Smørgrav if (fcntl(fd, F_SETFL, val) == -1) {
149d74d50a8SDag-Erling Smørgrav debug("fcntl(%d, F_SETFL, O_NONBLOCK): %s", fd,
150d74d50a8SDag-Erling Smørgrav strerror(errno));
151d74d50a8SDag-Erling Smørgrav return (-1);
152d74d50a8SDag-Erling Smørgrav }
153d74d50a8SDag-Erling Smørgrav return (0);
1541e8db6e2SBrian Feldman }
1551e8db6e2SBrian Feldman
156d74d50a8SDag-Erling Smørgrav int
unset_nonblock(int fd)157ae1f160dSDag-Erling Smørgrav unset_nonblock(int fd)
158ae1f160dSDag-Erling Smørgrav {
159ae1f160dSDag-Erling Smørgrav int val;
160ae1f160dSDag-Erling Smørgrav
161076ad2f8SDag-Erling Smørgrav val = fcntl(fd, F_GETFL);
16219261079SEd Maste if (val == -1) {
163076ad2f8SDag-Erling Smørgrav error("fcntl(%d, F_GETFL): %s", fd, strerror(errno));
164d74d50a8SDag-Erling Smørgrav return (-1);
165ae1f160dSDag-Erling Smørgrav }
166ae1f160dSDag-Erling Smørgrav if (!(val & O_NONBLOCK)) {
167d74d50a8SDag-Erling Smørgrav debug3("fd %d is not O_NONBLOCK", fd);
168d74d50a8SDag-Erling Smørgrav return (0);
169ae1f160dSDag-Erling Smørgrav }
170ae1f160dSDag-Erling Smørgrav debug("fd %d clearing O_NONBLOCK", fd);
171ae1f160dSDag-Erling Smørgrav val &= ~O_NONBLOCK;
172d74d50a8SDag-Erling Smørgrav if (fcntl(fd, F_SETFL, val) == -1) {
173d74d50a8SDag-Erling Smørgrav debug("fcntl(%d, F_SETFL, ~O_NONBLOCK): %s",
174ae1f160dSDag-Erling Smørgrav fd, strerror(errno));
175d74d50a8SDag-Erling Smørgrav return (-1);
176d74d50a8SDag-Erling Smørgrav }
177d74d50a8SDag-Erling Smørgrav return (0);
178ae1f160dSDag-Erling Smørgrav }
179ae1f160dSDag-Erling Smørgrav
180d4af9e69SDag-Erling Smørgrav const char *
ssh_gai_strerror(int gaierr)181d4af9e69SDag-Erling Smørgrav ssh_gai_strerror(int gaierr)
182d4af9e69SDag-Erling Smørgrav {
183e4a9863fSDag-Erling Smørgrav if (gaierr == EAI_SYSTEM && errno != 0)
184d4af9e69SDag-Erling Smørgrav return strerror(errno);
185d4af9e69SDag-Erling Smørgrav return gai_strerror(gaierr);
186d4af9e69SDag-Erling Smørgrav }
187d4af9e69SDag-Erling Smørgrav
188ae1f160dSDag-Erling Smørgrav /* disable nagle on socket */
189ae1f160dSDag-Erling Smørgrav void
set_nodelay(int fd)190ae1f160dSDag-Erling Smørgrav set_nodelay(int fd)
191ae1f160dSDag-Erling Smørgrav {
192ae1f160dSDag-Erling Smørgrav int opt;
193ae1f160dSDag-Erling Smørgrav socklen_t optlen;
194ae1f160dSDag-Erling Smørgrav
195ae1f160dSDag-Erling Smørgrav optlen = sizeof opt;
196ae1f160dSDag-Erling Smørgrav if (getsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &opt, &optlen) == -1) {
197efcad6b7SDag-Erling Smørgrav debug("getsockopt TCP_NODELAY: %.100s", strerror(errno));
198ae1f160dSDag-Erling Smørgrav return;
199ae1f160dSDag-Erling Smørgrav }
200ae1f160dSDag-Erling Smørgrav if (opt == 1) {
201ae1f160dSDag-Erling Smørgrav debug2("fd %d is TCP_NODELAY", fd);
202ae1f160dSDag-Erling Smørgrav return;
203ae1f160dSDag-Erling Smørgrav }
204ae1f160dSDag-Erling Smørgrav opt = 1;
205d0c8c0bcSDag-Erling Smørgrav debug2("fd %d setting TCP_NODELAY", fd);
206ae1f160dSDag-Erling Smørgrav if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &opt, sizeof opt) == -1)
207ae1f160dSDag-Erling Smørgrav error("setsockopt TCP_NODELAY: %.100s", strerror(errno));
208ae1f160dSDag-Erling Smørgrav }
209ae1f160dSDag-Erling Smørgrav
21047dd1d1bSDag-Erling Smørgrav /* Allow local port reuse in TIME_WAIT */
21147dd1d1bSDag-Erling Smørgrav int
set_reuseaddr(int fd)21247dd1d1bSDag-Erling Smørgrav set_reuseaddr(int fd)
21347dd1d1bSDag-Erling Smørgrav {
21447dd1d1bSDag-Erling Smørgrav int on = 1;
21547dd1d1bSDag-Erling Smørgrav
21647dd1d1bSDag-Erling Smørgrav if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) == -1) {
21747dd1d1bSDag-Erling Smørgrav error("setsockopt SO_REUSEADDR fd %d: %s", fd, strerror(errno));
21847dd1d1bSDag-Erling Smørgrav return -1;
21947dd1d1bSDag-Erling Smørgrav }
22047dd1d1bSDag-Erling Smørgrav return 0;
22147dd1d1bSDag-Erling Smørgrav }
22247dd1d1bSDag-Erling Smørgrav
22347dd1d1bSDag-Erling Smørgrav /* Get/set routing domain */
22447dd1d1bSDag-Erling Smørgrav char *
get_rdomain(int fd)22547dd1d1bSDag-Erling Smørgrav get_rdomain(int fd)
22647dd1d1bSDag-Erling Smørgrav {
22747dd1d1bSDag-Erling Smørgrav #if defined(HAVE_SYS_GET_RDOMAIN)
22847dd1d1bSDag-Erling Smørgrav return sys_get_rdomain(fd);
22947dd1d1bSDag-Erling Smørgrav #elif defined(__OpenBSD__)
23047dd1d1bSDag-Erling Smørgrav int rtable;
23147dd1d1bSDag-Erling Smørgrav char *ret;
23247dd1d1bSDag-Erling Smørgrav socklen_t len = sizeof(rtable);
23347dd1d1bSDag-Erling Smørgrav
23447dd1d1bSDag-Erling Smørgrav if (getsockopt(fd, SOL_SOCKET, SO_RTABLE, &rtable, &len) == -1) {
23547dd1d1bSDag-Erling Smørgrav error("Failed to get routing domain for fd %d: %s",
23647dd1d1bSDag-Erling Smørgrav fd, strerror(errno));
23747dd1d1bSDag-Erling Smørgrav return NULL;
23847dd1d1bSDag-Erling Smørgrav }
23947dd1d1bSDag-Erling Smørgrav xasprintf(&ret, "%d", rtable);
24047dd1d1bSDag-Erling Smørgrav return ret;
24147dd1d1bSDag-Erling Smørgrav #else /* defined(__OpenBSD__) */
24247dd1d1bSDag-Erling Smørgrav return NULL;
24347dd1d1bSDag-Erling Smørgrav #endif
24447dd1d1bSDag-Erling Smørgrav }
24547dd1d1bSDag-Erling Smørgrav
24647dd1d1bSDag-Erling Smørgrav int
set_rdomain(int fd,const char * name)24747dd1d1bSDag-Erling Smørgrav set_rdomain(int fd, const char *name)
24847dd1d1bSDag-Erling Smørgrav {
24947dd1d1bSDag-Erling Smørgrav #if defined(HAVE_SYS_SET_RDOMAIN)
25047dd1d1bSDag-Erling Smørgrav return sys_set_rdomain(fd, name);
25147dd1d1bSDag-Erling Smørgrav #elif defined(__OpenBSD__)
25247dd1d1bSDag-Erling Smørgrav int rtable;
25347dd1d1bSDag-Erling Smørgrav const char *errstr;
25447dd1d1bSDag-Erling Smørgrav
25547dd1d1bSDag-Erling Smørgrav if (name == NULL)
25647dd1d1bSDag-Erling Smørgrav return 0; /* default table */
25747dd1d1bSDag-Erling Smørgrav
25847dd1d1bSDag-Erling Smørgrav rtable = (int)strtonum(name, 0, 255, &errstr);
25947dd1d1bSDag-Erling Smørgrav if (errstr != NULL) {
26047dd1d1bSDag-Erling Smørgrav /* Shouldn't happen */
26147dd1d1bSDag-Erling Smørgrav error("Invalid routing domain \"%s\": %s", name, errstr);
26247dd1d1bSDag-Erling Smørgrav return -1;
26347dd1d1bSDag-Erling Smørgrav }
26447dd1d1bSDag-Erling Smørgrav if (setsockopt(fd, SOL_SOCKET, SO_RTABLE,
26547dd1d1bSDag-Erling Smørgrav &rtable, sizeof(rtable)) == -1) {
26647dd1d1bSDag-Erling Smørgrav error("Failed to set routing domain %d on fd %d: %s",
26747dd1d1bSDag-Erling Smørgrav rtable, fd, strerror(errno));
26847dd1d1bSDag-Erling Smørgrav return -1;
26947dd1d1bSDag-Erling Smørgrav }
27047dd1d1bSDag-Erling Smørgrav return 0;
27147dd1d1bSDag-Erling Smørgrav #else /* defined(__OpenBSD__) */
27247dd1d1bSDag-Erling Smørgrav error("Setting routing domain is not supported on this platform");
27347dd1d1bSDag-Erling Smørgrav return -1;
27447dd1d1bSDag-Erling Smørgrav #endif
27547dd1d1bSDag-Erling Smørgrav }
27647dd1d1bSDag-Erling Smørgrav
27719261079SEd Maste int
get_sock_af(int fd)27819261079SEd Maste get_sock_af(int fd)
27919261079SEd Maste {
28019261079SEd Maste struct sockaddr_storage to;
28119261079SEd Maste socklen_t tolen = sizeof(to);
28219261079SEd Maste
28319261079SEd Maste memset(&to, 0, sizeof(to));
28419261079SEd Maste if (getsockname(fd, (struct sockaddr *)&to, &tolen) == -1)
28519261079SEd Maste return -1;
28619261079SEd Maste #ifdef IPV4_IN_IPV6
28719261079SEd Maste if (to.ss_family == AF_INET6 &&
28819261079SEd Maste IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&to)->sin6_addr))
28919261079SEd Maste return AF_INET;
29019261079SEd Maste #endif
29119261079SEd Maste return to.ss_family;
29219261079SEd Maste }
29319261079SEd Maste
29419261079SEd Maste void
set_sock_tos(int fd,int tos)29519261079SEd Maste set_sock_tos(int fd, int tos)
29619261079SEd Maste {
29719261079SEd Maste #ifndef IP_TOS_IS_BROKEN
29819261079SEd Maste int af;
29919261079SEd Maste
30019261079SEd Maste switch ((af = get_sock_af(fd))) {
30119261079SEd Maste case -1:
30219261079SEd Maste /* assume not a socket */
30319261079SEd Maste break;
30419261079SEd Maste case AF_INET:
30519261079SEd Maste # ifdef IP_TOS
30619261079SEd Maste debug3_f("set socket %d IP_TOS 0x%02x", fd, tos);
30719261079SEd Maste if (setsockopt(fd, IPPROTO_IP, IP_TOS,
30819261079SEd Maste &tos, sizeof(tos)) == -1) {
309f374ba41SEd Maste error("setsockopt socket %d IP_TOS %d: %s",
31019261079SEd Maste fd, tos, strerror(errno));
31119261079SEd Maste }
31219261079SEd Maste # endif /* IP_TOS */
31319261079SEd Maste break;
31419261079SEd Maste case AF_INET6:
31519261079SEd Maste # ifdef IPV6_TCLASS
31619261079SEd Maste debug3_f("set socket %d IPV6_TCLASS 0x%02x", fd, tos);
31719261079SEd Maste if (setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS,
31819261079SEd Maste &tos, sizeof(tos)) == -1) {
319f374ba41SEd Maste error("setsockopt socket %d IPV6_TCLASS %d: %s",
32019261079SEd Maste fd, tos, strerror(errno));
32119261079SEd Maste }
32219261079SEd Maste # endif /* IPV6_TCLASS */
32319261079SEd Maste break;
32419261079SEd Maste default:
32519261079SEd Maste debug2_f("unsupported socket family %d", af);
32619261079SEd Maste break;
32719261079SEd Maste }
32819261079SEd Maste #endif /* IP_TOS_IS_BROKEN */
32919261079SEd Maste }
33019261079SEd Maste
33119261079SEd Maste /*
33219261079SEd Maste * Wait up to *timeoutp milliseconds for events on fd. Updates
33319261079SEd Maste * *timeoutp with time remaining.
33419261079SEd Maste * Returns 0 if fd ready or -1 on timeout or error (see errno).
33519261079SEd Maste */
33619261079SEd Maste static int
waitfd(int fd,int * timeoutp,short events,volatile sig_atomic_t * stop)3371b91d634SEd Maste waitfd(int fd, int *timeoutp, short events, volatile sig_atomic_t *stop)
33819261079SEd Maste {
33919261079SEd Maste struct pollfd pfd;
3401b91d634SEd Maste struct timespec timeout;
3411b91d634SEd Maste int oerrno, r;
3421b91d634SEd Maste sigset_t nsigset, osigset;
34319261079SEd Maste
3441b91d634SEd Maste if (timeoutp && *timeoutp == -1)
3451b91d634SEd Maste timeoutp = NULL;
34619261079SEd Maste pfd.fd = fd;
34719261079SEd Maste pfd.events = events;
3481b91d634SEd Maste ptimeout_init(&timeout);
3491b91d634SEd Maste if (timeoutp != NULL)
3501b91d634SEd Maste ptimeout_deadline_ms(&timeout, *timeoutp);
3511b91d634SEd Maste if (stop != NULL)
3521b91d634SEd Maste sigfillset(&nsigset);
3531b91d634SEd Maste for (; timeoutp == NULL || *timeoutp >= 0;) {
3541b91d634SEd Maste if (stop != NULL) {
3551b91d634SEd Maste sigprocmask(SIG_BLOCK, &nsigset, &osigset);
3561b91d634SEd Maste if (*stop) {
3571b91d634SEd Maste sigprocmask(SIG_SETMASK, &osigset, NULL);
3581b91d634SEd Maste errno = EINTR;
3591b91d634SEd Maste return -1;
3601b91d634SEd Maste }
3611b91d634SEd Maste }
3621b91d634SEd Maste r = ppoll(&pfd, 1, ptimeout_get_tsp(&timeout),
3631b91d634SEd Maste stop != NULL ? &osigset : NULL);
36419261079SEd Maste oerrno = errno;
3651b91d634SEd Maste if (stop != NULL)
3661b91d634SEd Maste sigprocmask(SIG_SETMASK, &osigset, NULL);
3671b91d634SEd Maste if (timeoutp)
3681b91d634SEd Maste *timeoutp = ptimeout_get_ms(&timeout);
36919261079SEd Maste errno = oerrno;
37019261079SEd Maste if (r > 0)
37119261079SEd Maste return 0;
37219261079SEd Maste else if (r == -1 && errno != EAGAIN && errno != EINTR)
37319261079SEd Maste return -1;
37419261079SEd Maste else if (r == 0)
37519261079SEd Maste break;
37619261079SEd Maste }
37719261079SEd Maste /* timeout */
37819261079SEd Maste errno = ETIMEDOUT;
37919261079SEd Maste return -1;
38019261079SEd Maste }
38119261079SEd Maste
38219261079SEd Maste /*
38319261079SEd Maste * Wait up to *timeoutp milliseconds for fd to be readable. Updates
38419261079SEd Maste * *timeoutp with time remaining.
38519261079SEd Maste * Returns 0 if fd ready or -1 on timeout or error (see errno).
38619261079SEd Maste */
38719261079SEd Maste int
waitrfd(int fd,int * timeoutp,volatile sig_atomic_t * stop)3881b91d634SEd Maste waitrfd(int fd, int *timeoutp, volatile sig_atomic_t *stop) {
3891b91d634SEd Maste return waitfd(fd, timeoutp, POLLIN, stop);
39019261079SEd Maste }
39119261079SEd Maste
39219261079SEd Maste /*
39319261079SEd Maste * Attempt a non-blocking connect(2) to the specified address, waiting up to
39419261079SEd Maste * *timeoutp milliseconds for the connection to complete. If the timeout is
39519261079SEd Maste * <=0, then wait indefinitely.
39619261079SEd Maste *
39719261079SEd Maste * Returns 0 on success or -1 on failure.
39819261079SEd Maste */
39919261079SEd Maste int
timeout_connect(int sockfd,const struct sockaddr * serv_addr,socklen_t addrlen,int * timeoutp)40019261079SEd Maste timeout_connect(int sockfd, const struct sockaddr *serv_addr,
40119261079SEd Maste socklen_t addrlen, int *timeoutp)
40219261079SEd Maste {
40319261079SEd Maste int optval = 0;
40419261079SEd Maste socklen_t optlen = sizeof(optval);
40519261079SEd Maste
40619261079SEd Maste /* No timeout: just do a blocking connect() */
40719261079SEd Maste if (timeoutp == NULL || *timeoutp <= 0)
40819261079SEd Maste return connect(sockfd, serv_addr, addrlen);
40919261079SEd Maste
41019261079SEd Maste set_nonblock(sockfd);
41119261079SEd Maste for (;;) {
41219261079SEd Maste if (connect(sockfd, serv_addr, addrlen) == 0) {
41319261079SEd Maste /* Succeeded already? */
41419261079SEd Maste unset_nonblock(sockfd);
41519261079SEd Maste return 0;
41619261079SEd Maste } else if (errno == EINTR)
41719261079SEd Maste continue;
41819261079SEd Maste else if (errno != EINPROGRESS)
41919261079SEd Maste return -1;
42019261079SEd Maste break;
42119261079SEd Maste }
42219261079SEd Maste
4231b91d634SEd Maste if (waitfd(sockfd, timeoutp, POLLIN | POLLOUT, NULL) == -1)
42419261079SEd Maste return -1;
42519261079SEd Maste
42619261079SEd Maste /* Completed or failed */
42719261079SEd Maste if (getsockopt(sockfd, SOL_SOCKET, SO_ERROR, &optval, &optlen) == -1) {
42819261079SEd Maste debug("getsockopt: %s", strerror(errno));
42919261079SEd Maste return -1;
43019261079SEd Maste }
43119261079SEd Maste if (optval != 0) {
43219261079SEd Maste errno = optval;
43319261079SEd Maste return -1;
43419261079SEd Maste }
43519261079SEd Maste unset_nonblock(sockfd);
43619261079SEd Maste return 0;
43719261079SEd Maste }
43819261079SEd Maste
4391e8db6e2SBrian Feldman /* Characters considered whitespace in strsep calls. */
4401e8db6e2SBrian Feldman #define WHITESPACE " \t\r\n"
441761efaa7SDag-Erling Smørgrav #define QUOTE "\""
4421e8db6e2SBrian Feldman
443ae1f160dSDag-Erling Smørgrav /* return next token in configuration line */
444190cef3dSDag-Erling Smørgrav static char *
strdelim_internal(char ** s,int split_equals)445190cef3dSDag-Erling Smørgrav strdelim_internal(char **s, int split_equals)
4461e8db6e2SBrian Feldman {
4471e8db6e2SBrian Feldman char *old;
4481e8db6e2SBrian Feldman int wspace = 0;
4491e8db6e2SBrian Feldman
4501e8db6e2SBrian Feldman if (*s == NULL)
4511e8db6e2SBrian Feldman return NULL;
4521e8db6e2SBrian Feldman
4531e8db6e2SBrian Feldman old = *s;
4541e8db6e2SBrian Feldman
455190cef3dSDag-Erling Smørgrav *s = strpbrk(*s,
456190cef3dSDag-Erling Smørgrav split_equals ? WHITESPACE QUOTE "=" : WHITESPACE QUOTE);
4571e8db6e2SBrian Feldman if (*s == NULL)
4581e8db6e2SBrian Feldman return (old);
4591e8db6e2SBrian Feldman
460761efaa7SDag-Erling Smørgrav if (*s[0] == '\"') {
461761efaa7SDag-Erling Smørgrav memmove(*s, *s + 1, strlen(*s)); /* move nul too */
462761efaa7SDag-Erling Smørgrav /* Find matching quote */
463761efaa7SDag-Erling Smørgrav if ((*s = strpbrk(*s, QUOTE)) == NULL) {
464761efaa7SDag-Erling Smørgrav return (NULL); /* no matching quote */
465761efaa7SDag-Erling Smørgrav } else {
466761efaa7SDag-Erling Smørgrav *s[0] = '\0';
467e2f6069cSDag-Erling Smørgrav *s += strspn(*s + 1, WHITESPACE) + 1;
468761efaa7SDag-Erling Smørgrav return (old);
469761efaa7SDag-Erling Smørgrav }
470761efaa7SDag-Erling Smørgrav }
471761efaa7SDag-Erling Smørgrav
4721e8db6e2SBrian Feldman /* Allow only one '=' to be skipped */
473190cef3dSDag-Erling Smørgrav if (split_equals && *s[0] == '=')
4741e8db6e2SBrian Feldman wspace = 1;
4751e8db6e2SBrian Feldman *s[0] = '\0';
4761e8db6e2SBrian Feldman
477761efaa7SDag-Erling Smørgrav /* Skip any extra whitespace after first token */
4781e8db6e2SBrian Feldman *s += strspn(*s + 1, WHITESPACE) + 1;
479190cef3dSDag-Erling Smørgrav if (split_equals && *s[0] == '=' && !wspace)
4801e8db6e2SBrian Feldman *s += strspn(*s + 1, WHITESPACE) + 1;
4811e8db6e2SBrian Feldman
4821e8db6e2SBrian Feldman return (old);
4831e8db6e2SBrian Feldman }
4841e8db6e2SBrian Feldman
485190cef3dSDag-Erling Smørgrav /*
486190cef3dSDag-Erling Smørgrav * Return next token in configuration line; splts on whitespace or a
487190cef3dSDag-Erling Smørgrav * single '=' character.
488190cef3dSDag-Erling Smørgrav */
489190cef3dSDag-Erling Smørgrav char *
strdelim(char ** s)490190cef3dSDag-Erling Smørgrav strdelim(char **s)
491190cef3dSDag-Erling Smørgrav {
492190cef3dSDag-Erling Smørgrav return strdelim_internal(s, 1);
493190cef3dSDag-Erling Smørgrav }
494190cef3dSDag-Erling Smørgrav
495190cef3dSDag-Erling Smørgrav /*
496190cef3dSDag-Erling Smørgrav * Return next token in configuration line; splts on whitespace only.
497190cef3dSDag-Erling Smørgrav */
498190cef3dSDag-Erling Smørgrav char *
strdelimw(char ** s)499190cef3dSDag-Erling Smørgrav strdelimw(char **s)
500190cef3dSDag-Erling Smørgrav {
501190cef3dSDag-Erling Smørgrav return strdelim_internal(s, 0);
502190cef3dSDag-Erling Smørgrav }
503190cef3dSDag-Erling Smørgrav
5041e8db6e2SBrian Feldman struct passwd *
pwcopy(struct passwd * pw)5051e8db6e2SBrian Feldman pwcopy(struct passwd *pw)
5061e8db6e2SBrian Feldman {
507761efaa7SDag-Erling Smørgrav struct passwd *copy = xcalloc(1, sizeof(*copy));
5081e8db6e2SBrian Feldman
5091e8db6e2SBrian Feldman copy->pw_name = xstrdup(pw->pw_name);
51019261079SEd Maste copy->pw_passwd = xstrdup(pw->pw_passwd == NULL ? "*" : pw->pw_passwd);
511e4a9863fSDag-Erling Smørgrav #ifdef HAVE_STRUCT_PASSWD_PW_GECOS
5121e8db6e2SBrian Feldman copy->pw_gecos = xstrdup(pw->pw_gecos);
513e4a9863fSDag-Erling Smørgrav #endif
5141e8db6e2SBrian Feldman copy->pw_uid = pw->pw_uid;
5151e8db6e2SBrian Feldman copy->pw_gid = pw->pw_gid;
516e4a9863fSDag-Erling Smørgrav #ifdef HAVE_STRUCT_PASSWD_PW_EXPIRE
517ae1f160dSDag-Erling Smørgrav copy->pw_expire = pw->pw_expire;
51883d2307dSDag-Erling Smørgrav #endif
519e4a9863fSDag-Erling Smørgrav #ifdef HAVE_STRUCT_PASSWD_PW_CHANGE
520ae1f160dSDag-Erling Smørgrav copy->pw_change = pw->pw_change;
52183d2307dSDag-Erling Smørgrav #endif
522e4a9863fSDag-Erling Smørgrav #ifdef HAVE_STRUCT_PASSWD_PW_CLASS
5231e8db6e2SBrian Feldman copy->pw_class = xstrdup(pw->pw_class);
52483d2307dSDag-Erling Smørgrav #endif
5251e8db6e2SBrian Feldman copy->pw_dir = xstrdup(pw->pw_dir);
5261e8db6e2SBrian Feldman copy->pw_shell = xstrdup(pw->pw_shell);
5271e8db6e2SBrian Feldman return copy;
5281e8db6e2SBrian Feldman }
5291e8db6e2SBrian Feldman
530ae1f160dSDag-Erling Smørgrav /*
531ae1f160dSDag-Erling Smørgrav * Convert ASCII string to TCP/IP port number.
532cce7d346SDag-Erling Smørgrav * Port must be >=0 and <=65535.
533cce7d346SDag-Erling Smørgrav * Return -1 if invalid.
534ae1f160dSDag-Erling Smørgrav */
535ae1f160dSDag-Erling Smørgrav int
a2port(const char * s)536ae1f160dSDag-Erling Smørgrav a2port(const char *s)
5371e8db6e2SBrian Feldman {
5382f513db7SEd Maste struct servent *se;
539cce7d346SDag-Erling Smørgrav long long port;
540cce7d346SDag-Erling Smørgrav const char *errstr;
5411e8db6e2SBrian Feldman
542cce7d346SDag-Erling Smørgrav port = strtonum(s, 0, 65535, &errstr);
5432f513db7SEd Maste if (errstr == NULL)
544cce7d346SDag-Erling Smørgrav return (int)port;
5452f513db7SEd Maste if ((se = getservbyname(s, "tcp")) != NULL)
5462f513db7SEd Maste return ntohs(se->s_port);
5472f513db7SEd Maste return -1;
5481e8db6e2SBrian Feldman }
549ae1f160dSDag-Erling Smørgrav
550021d409fSDag-Erling Smørgrav int
a2tun(const char * s,int * remote)551021d409fSDag-Erling Smørgrav a2tun(const char *s, int *remote)
552021d409fSDag-Erling Smørgrav {
553021d409fSDag-Erling Smørgrav const char *errstr = NULL;
554021d409fSDag-Erling Smørgrav char *sp, *ep;
555021d409fSDag-Erling Smørgrav int tun;
556021d409fSDag-Erling Smørgrav
557021d409fSDag-Erling Smørgrav if (remote != NULL) {
558021d409fSDag-Erling Smørgrav *remote = SSH_TUNID_ANY;
559021d409fSDag-Erling Smørgrav sp = xstrdup(s);
560021d409fSDag-Erling Smørgrav if ((ep = strchr(sp, ':')) == NULL) {
561e4a9863fSDag-Erling Smørgrav free(sp);
562021d409fSDag-Erling Smørgrav return (a2tun(s, NULL));
563021d409fSDag-Erling Smørgrav }
564021d409fSDag-Erling Smørgrav ep[0] = '\0'; ep++;
565021d409fSDag-Erling Smørgrav *remote = a2tun(ep, NULL);
566021d409fSDag-Erling Smørgrav tun = a2tun(sp, NULL);
567e4a9863fSDag-Erling Smørgrav free(sp);
568021d409fSDag-Erling Smørgrav return (*remote == SSH_TUNID_ERR ? *remote : tun);
569021d409fSDag-Erling Smørgrav }
570021d409fSDag-Erling Smørgrav
571021d409fSDag-Erling Smørgrav if (strcasecmp(s, "any") == 0)
572021d409fSDag-Erling Smørgrav return (SSH_TUNID_ANY);
573021d409fSDag-Erling Smørgrav
574021d409fSDag-Erling Smørgrav tun = strtonum(s, 0, SSH_TUNID_MAX, &errstr);
575021d409fSDag-Erling Smørgrav if (errstr != NULL)
576021d409fSDag-Erling Smørgrav return (SSH_TUNID_ERR);
577021d409fSDag-Erling Smørgrav
578021d409fSDag-Erling Smørgrav return (tun);
579021d409fSDag-Erling Smørgrav }
580021d409fSDag-Erling Smørgrav
581ae1f160dSDag-Erling Smørgrav #define SECONDS 1
582ae1f160dSDag-Erling Smørgrav #define MINUTES (SECONDS * 60)
583ae1f160dSDag-Erling Smørgrav #define HOURS (MINUTES * 60)
584ae1f160dSDag-Erling Smørgrav #define DAYS (HOURS * 24)
585ae1f160dSDag-Erling Smørgrav #define WEEKS (DAYS * 7)
586ae1f160dSDag-Erling Smørgrav
5870fdf8faeSEd Maste static char *
scandigits(char * s)5880fdf8faeSEd Maste scandigits(char *s)
5890fdf8faeSEd Maste {
5900fdf8faeSEd Maste while (isdigit((unsigned char)*s))
5910fdf8faeSEd Maste s++;
5920fdf8faeSEd Maste return s;
5930fdf8faeSEd Maste }
5940fdf8faeSEd Maste
595ae1f160dSDag-Erling Smørgrav /*
596ae1f160dSDag-Erling Smørgrav * Convert a time string into seconds; format is
597ae1f160dSDag-Erling Smørgrav * a sequence of:
598ae1f160dSDag-Erling Smørgrav * time[qualifier]
599ae1f160dSDag-Erling Smørgrav *
600ae1f160dSDag-Erling Smørgrav * Valid time qualifiers are:
601ae1f160dSDag-Erling Smørgrav * <none> seconds
602ae1f160dSDag-Erling Smørgrav * s|S seconds
603ae1f160dSDag-Erling Smørgrav * m|M minutes
604ae1f160dSDag-Erling Smørgrav * h|H hours
605ae1f160dSDag-Erling Smørgrav * d|D days
606ae1f160dSDag-Erling Smørgrav * w|W weeks
607ae1f160dSDag-Erling Smørgrav *
608ae1f160dSDag-Erling Smørgrav * Examples:
609ae1f160dSDag-Erling Smørgrav * 90m 90 minutes
610ae1f160dSDag-Erling Smørgrav * 1h30m 90 minutes
611ae1f160dSDag-Erling Smørgrav * 2d 2 days
612ae1f160dSDag-Erling Smørgrav * 1w 1 week
613ae1f160dSDag-Erling Smørgrav *
614ae1f160dSDag-Erling Smørgrav * Return -1 if time string is invalid.
615ae1f160dSDag-Erling Smørgrav */
61619261079SEd Maste int
convtime(const char * s)617ae1f160dSDag-Erling Smørgrav convtime(const char *s)
618ae1f160dSDag-Erling Smørgrav {
6190fdf8faeSEd Maste int secs, total = 0, multiplier;
6200fdf8faeSEd Maste char *p, *os, *np, c = 0;
6210fdf8faeSEd Maste const char *errstr;
622ae1f160dSDag-Erling Smørgrav
6230fdf8faeSEd Maste if (s == NULL || *s == '\0')
6240fdf8faeSEd Maste return -1;
6250fdf8faeSEd Maste p = os = strdup(s); /* deal with const */
6260fdf8faeSEd Maste if (os == NULL)
627ae1f160dSDag-Erling Smørgrav return -1;
628ae1f160dSDag-Erling Smørgrav
629ae1f160dSDag-Erling Smørgrav while (*p) {
6300fdf8faeSEd Maste np = scandigits(p);
6310fdf8faeSEd Maste if (np) {
6320fdf8faeSEd Maste c = *np;
6330fdf8faeSEd Maste *np = '\0';
6340fdf8faeSEd Maste }
6350fdf8faeSEd Maste secs = (int)strtonum(p, 0, INT_MAX, &errstr);
6360fdf8faeSEd Maste if (errstr)
6370fdf8faeSEd Maste goto fail;
6380fdf8faeSEd Maste *np = c;
639ae1f160dSDag-Erling Smørgrav
64019261079SEd Maste multiplier = 1;
6410fdf8faeSEd Maste switch (c) {
642ae1f160dSDag-Erling Smørgrav case '\0':
6430fdf8faeSEd Maste np--; /* back up */
644761efaa7SDag-Erling Smørgrav break;
645ae1f160dSDag-Erling Smørgrav case 's':
646ae1f160dSDag-Erling Smørgrav case 'S':
647ae1f160dSDag-Erling Smørgrav break;
648ae1f160dSDag-Erling Smørgrav case 'm':
649ae1f160dSDag-Erling Smørgrav case 'M':
650d93a896eSDag-Erling Smørgrav multiplier = MINUTES;
651ae1f160dSDag-Erling Smørgrav break;
652ae1f160dSDag-Erling Smørgrav case 'h':
653ae1f160dSDag-Erling Smørgrav case 'H':
654d93a896eSDag-Erling Smørgrav multiplier = HOURS;
655ae1f160dSDag-Erling Smørgrav break;
656ae1f160dSDag-Erling Smørgrav case 'd':
657ae1f160dSDag-Erling Smørgrav case 'D':
658d93a896eSDag-Erling Smørgrav multiplier = DAYS;
659ae1f160dSDag-Erling Smørgrav break;
660ae1f160dSDag-Erling Smørgrav case 'w':
661ae1f160dSDag-Erling Smørgrav case 'W':
662d93a896eSDag-Erling Smørgrav multiplier = WEEKS;
663ae1f160dSDag-Erling Smørgrav break;
664ae1f160dSDag-Erling Smørgrav default:
6650fdf8faeSEd Maste goto fail;
666ae1f160dSDag-Erling Smørgrav }
66719261079SEd Maste if (secs > INT_MAX / multiplier)
6680fdf8faeSEd Maste goto fail;
669d93a896eSDag-Erling Smørgrav secs *= multiplier;
67019261079SEd Maste if (total > INT_MAX - secs)
6710fdf8faeSEd Maste goto fail;
672ae1f160dSDag-Erling Smørgrav total += secs;
673ae1f160dSDag-Erling Smørgrav if (total < 0)
6740fdf8faeSEd Maste goto fail;
6750fdf8faeSEd Maste p = ++np;
676ae1f160dSDag-Erling Smørgrav }
6770fdf8faeSEd Maste free(os);
678ae1f160dSDag-Erling Smørgrav return total;
6790fdf8faeSEd Maste fail:
6800fdf8faeSEd Maste free(os);
6810fdf8faeSEd Maste return -1;
682ae1f160dSDag-Erling Smørgrav }
683ae1f160dSDag-Erling Smørgrav
68419261079SEd Maste #define TF_BUFS 8
68519261079SEd Maste #define TF_LEN 9
68619261079SEd Maste
68719261079SEd Maste const char *
fmt_timeframe(time_t t)68819261079SEd Maste fmt_timeframe(time_t t)
68919261079SEd Maste {
69019261079SEd Maste char *buf;
69119261079SEd Maste static char tfbuf[TF_BUFS][TF_LEN]; /* ring buffer */
69219261079SEd Maste static int idx = 0;
69319261079SEd Maste unsigned int sec, min, hrs, day;
69419261079SEd Maste unsigned long long week;
69519261079SEd Maste
69619261079SEd Maste buf = tfbuf[idx++];
69719261079SEd Maste if (idx == TF_BUFS)
69819261079SEd Maste idx = 0;
69919261079SEd Maste
70019261079SEd Maste week = t;
70119261079SEd Maste
70219261079SEd Maste sec = week % 60;
70319261079SEd Maste week /= 60;
70419261079SEd Maste min = week % 60;
70519261079SEd Maste week /= 60;
70619261079SEd Maste hrs = week % 24;
70719261079SEd Maste week /= 24;
70819261079SEd Maste day = week % 7;
70919261079SEd Maste week /= 7;
71019261079SEd Maste
71119261079SEd Maste if (week > 0)
71219261079SEd Maste snprintf(buf, TF_LEN, "%02lluw%01ud%02uh", week, day, hrs);
71319261079SEd Maste else if (day > 0)
71419261079SEd Maste snprintf(buf, TF_LEN, "%01ud%02uh%02um", day, hrs, min);
71519261079SEd Maste else
71619261079SEd Maste snprintf(buf, TF_LEN, "%02u:%02u:%02u", hrs, min, sec);
71719261079SEd Maste
71819261079SEd Maste return (buf);
71919261079SEd Maste }
72019261079SEd Maste
7215e8dbd04SDag-Erling Smørgrav /*
722761efaa7SDag-Erling Smørgrav * Returns a standardized host+port identifier string.
723761efaa7SDag-Erling Smørgrav * Caller must free returned string.
724761efaa7SDag-Erling Smørgrav */
725761efaa7SDag-Erling Smørgrav char *
put_host_port(const char * host,u_short port)726761efaa7SDag-Erling Smørgrav put_host_port(const char *host, u_short port)
727761efaa7SDag-Erling Smørgrav {
728761efaa7SDag-Erling Smørgrav char *hoststr;
729761efaa7SDag-Erling Smørgrav
730761efaa7SDag-Erling Smørgrav if (port == 0 || port == SSH_DEFAULT_PORT)
731761efaa7SDag-Erling Smørgrav return(xstrdup(host));
73219261079SEd Maste if (asprintf(&hoststr, "[%s]:%d", host, (int)port) == -1)
733761efaa7SDag-Erling Smørgrav fatal("put_host_port: asprintf: %s", strerror(errno));
734761efaa7SDag-Erling Smørgrav debug3("put_host_port: %s", hoststr);
735761efaa7SDag-Erling Smørgrav return hoststr;
736761efaa7SDag-Erling Smørgrav }
737761efaa7SDag-Erling Smørgrav
738761efaa7SDag-Erling Smørgrav /*
7395e8dbd04SDag-Erling Smørgrav * Search for next delimiter between hostnames/addresses and ports.
7405e8dbd04SDag-Erling Smørgrav * Argument may be modified (for termination).
7415e8dbd04SDag-Erling Smørgrav * Returns *cp if parsing succeeds.
74247dd1d1bSDag-Erling Smørgrav * *cp is set to the start of the next field, if one was found.
74347dd1d1bSDag-Erling Smørgrav * The delimiter char, if present, is stored in delim.
7445e8dbd04SDag-Erling Smørgrav * If this is the last field, *cp is set to NULL.
7455e8dbd04SDag-Erling Smørgrav */
74619261079SEd Maste char *
hpdelim2(char ** cp,char * delim)74747dd1d1bSDag-Erling Smørgrav hpdelim2(char **cp, char *delim)
7485e8dbd04SDag-Erling Smørgrav {
7495e8dbd04SDag-Erling Smørgrav char *s, *old;
7505e8dbd04SDag-Erling Smørgrav
7515e8dbd04SDag-Erling Smørgrav if (cp == NULL || *cp == NULL)
7525e8dbd04SDag-Erling Smørgrav return NULL;
7535e8dbd04SDag-Erling Smørgrav
7545e8dbd04SDag-Erling Smørgrav old = s = *cp;
7555e8dbd04SDag-Erling Smørgrav if (*s == '[') {
7565e8dbd04SDag-Erling Smørgrav if ((s = strchr(s, ']')) == NULL)
7575e8dbd04SDag-Erling Smørgrav return NULL;
7585e8dbd04SDag-Erling Smørgrav else
7595e8dbd04SDag-Erling Smørgrav s++;
7605e8dbd04SDag-Erling Smørgrav } else if ((s = strpbrk(s, ":/")) == NULL)
7615e8dbd04SDag-Erling Smørgrav s = *cp + strlen(*cp); /* skip to end (see first case below) */
7625e8dbd04SDag-Erling Smørgrav
7635e8dbd04SDag-Erling Smørgrav switch (*s) {
7645e8dbd04SDag-Erling Smørgrav case '\0':
7655e8dbd04SDag-Erling Smørgrav *cp = NULL; /* no more fields*/
7665e8dbd04SDag-Erling Smørgrav break;
7675e8dbd04SDag-Erling Smørgrav
7685e8dbd04SDag-Erling Smørgrav case ':':
7695e8dbd04SDag-Erling Smørgrav case '/':
77047dd1d1bSDag-Erling Smørgrav if (delim != NULL)
77147dd1d1bSDag-Erling Smørgrav *delim = *s;
7725e8dbd04SDag-Erling Smørgrav *s = '\0'; /* terminate */
7735e8dbd04SDag-Erling Smørgrav *cp = s + 1;
7745e8dbd04SDag-Erling Smørgrav break;
7755e8dbd04SDag-Erling Smørgrav
7765e8dbd04SDag-Erling Smørgrav default:
7775e8dbd04SDag-Erling Smørgrav return NULL;
7785e8dbd04SDag-Erling Smørgrav }
7795e8dbd04SDag-Erling Smørgrav
7805e8dbd04SDag-Erling Smørgrav return old;
7815e8dbd04SDag-Erling Smørgrav }
7825e8dbd04SDag-Erling Smørgrav
7831323ec57SEd Maste /* The common case: only accept colon as delimiter. */
784ae1f160dSDag-Erling Smørgrav char *
hpdelim(char ** cp)78547dd1d1bSDag-Erling Smørgrav hpdelim(char **cp)
78647dd1d1bSDag-Erling Smørgrav {
7871323ec57SEd Maste char *r, delim = '\0';
7881323ec57SEd Maste
7891323ec57SEd Maste r = hpdelim2(cp, &delim);
7901323ec57SEd Maste if (delim == '/')
7911323ec57SEd Maste return NULL;
7921323ec57SEd Maste return r;
79347dd1d1bSDag-Erling Smørgrav }
79447dd1d1bSDag-Erling Smørgrav
79547dd1d1bSDag-Erling Smørgrav char *
cleanhostname(char * host)796ae1f160dSDag-Erling Smørgrav cleanhostname(char *host)
797ae1f160dSDag-Erling Smørgrav {
798ae1f160dSDag-Erling Smørgrav if (*host == '[' && host[strlen(host) - 1] == ']') {
799ae1f160dSDag-Erling Smørgrav host[strlen(host) - 1] = '\0';
800ae1f160dSDag-Erling Smørgrav return (host + 1);
801ae1f160dSDag-Erling Smørgrav } else
802ae1f160dSDag-Erling Smørgrav return host;
803ae1f160dSDag-Erling Smørgrav }
804ae1f160dSDag-Erling Smørgrav
805ae1f160dSDag-Erling Smørgrav char *
colon(char * cp)806ae1f160dSDag-Erling Smørgrav colon(char *cp)
807ae1f160dSDag-Erling Smørgrav {
808ae1f160dSDag-Erling Smørgrav int flag = 0;
809ae1f160dSDag-Erling Smørgrav
810ae1f160dSDag-Erling Smørgrav if (*cp == ':') /* Leading colon is part of file name. */
811e2f6069cSDag-Erling Smørgrav return NULL;
812ae1f160dSDag-Erling Smørgrav if (*cp == '[')
813ae1f160dSDag-Erling Smørgrav flag = 1;
814ae1f160dSDag-Erling Smørgrav
815ae1f160dSDag-Erling Smørgrav for (; *cp; ++cp) {
816ae1f160dSDag-Erling Smørgrav if (*cp == '@' && *(cp+1) == '[')
817ae1f160dSDag-Erling Smørgrav flag = 1;
818ae1f160dSDag-Erling Smørgrav if (*cp == ']' && *(cp+1) == ':' && flag)
819ae1f160dSDag-Erling Smørgrav return (cp+1);
820ae1f160dSDag-Erling Smørgrav if (*cp == ':' && !flag)
821ae1f160dSDag-Erling Smørgrav return (cp);
822ae1f160dSDag-Erling Smørgrav if (*cp == '/')
823e2f6069cSDag-Erling Smørgrav return NULL;
824ae1f160dSDag-Erling Smørgrav }
825e2f6069cSDag-Erling Smørgrav return NULL;
826ae1f160dSDag-Erling Smørgrav }
827ae1f160dSDag-Erling Smørgrav
828076ad2f8SDag-Erling Smørgrav /*
82947dd1d1bSDag-Erling Smørgrav * Parse a [user@]host:[path] string.
83047dd1d1bSDag-Erling Smørgrav * Caller must free returned user, host and path.
83147dd1d1bSDag-Erling Smørgrav * Any of the pointer return arguments may be NULL (useful for syntax checking).
83247dd1d1bSDag-Erling Smørgrav * If user was not specified then *userp will be set to NULL.
83347dd1d1bSDag-Erling Smørgrav * If host was not specified then *hostp will be set to NULL.
83447dd1d1bSDag-Erling Smørgrav * If path was not specified then *pathp will be set to ".".
83547dd1d1bSDag-Erling Smørgrav * Returns 0 on success, -1 on failure.
83647dd1d1bSDag-Erling Smørgrav */
83747dd1d1bSDag-Erling Smørgrav int
parse_user_host_path(const char * s,char ** userp,char ** hostp,char ** pathp)83847dd1d1bSDag-Erling Smørgrav parse_user_host_path(const char *s, char **userp, char **hostp, char **pathp)
83947dd1d1bSDag-Erling Smørgrav {
84047dd1d1bSDag-Erling Smørgrav char *user = NULL, *host = NULL, *path = NULL;
84147dd1d1bSDag-Erling Smørgrav char *sdup, *tmp;
84247dd1d1bSDag-Erling Smørgrav int ret = -1;
84347dd1d1bSDag-Erling Smørgrav
84447dd1d1bSDag-Erling Smørgrav if (userp != NULL)
84547dd1d1bSDag-Erling Smørgrav *userp = NULL;
84647dd1d1bSDag-Erling Smørgrav if (hostp != NULL)
84747dd1d1bSDag-Erling Smørgrav *hostp = NULL;
84847dd1d1bSDag-Erling Smørgrav if (pathp != NULL)
84947dd1d1bSDag-Erling Smørgrav *pathp = NULL;
85047dd1d1bSDag-Erling Smørgrav
85147dd1d1bSDag-Erling Smørgrav sdup = xstrdup(s);
85247dd1d1bSDag-Erling Smørgrav
85347dd1d1bSDag-Erling Smørgrav /* Check for remote syntax: [user@]host:[path] */
85447dd1d1bSDag-Erling Smørgrav if ((tmp = colon(sdup)) == NULL)
85547dd1d1bSDag-Erling Smørgrav goto out;
85647dd1d1bSDag-Erling Smørgrav
85747dd1d1bSDag-Erling Smørgrav /* Extract optional path */
85847dd1d1bSDag-Erling Smørgrav *tmp++ = '\0';
85947dd1d1bSDag-Erling Smørgrav if (*tmp == '\0')
86047dd1d1bSDag-Erling Smørgrav tmp = ".";
86147dd1d1bSDag-Erling Smørgrav path = xstrdup(tmp);
86247dd1d1bSDag-Erling Smørgrav
86347dd1d1bSDag-Erling Smørgrav /* Extract optional user and mandatory host */
86447dd1d1bSDag-Erling Smørgrav tmp = strrchr(sdup, '@');
86547dd1d1bSDag-Erling Smørgrav if (tmp != NULL) {
86647dd1d1bSDag-Erling Smørgrav *tmp++ = '\0';
86747dd1d1bSDag-Erling Smørgrav host = xstrdup(cleanhostname(tmp));
86847dd1d1bSDag-Erling Smørgrav if (*sdup != '\0')
86947dd1d1bSDag-Erling Smørgrav user = xstrdup(sdup);
87047dd1d1bSDag-Erling Smørgrav } else {
87147dd1d1bSDag-Erling Smørgrav host = xstrdup(cleanhostname(sdup));
87247dd1d1bSDag-Erling Smørgrav user = NULL;
87347dd1d1bSDag-Erling Smørgrav }
87447dd1d1bSDag-Erling Smørgrav
87547dd1d1bSDag-Erling Smørgrav /* Success */
87647dd1d1bSDag-Erling Smørgrav if (userp != NULL) {
87747dd1d1bSDag-Erling Smørgrav *userp = user;
87847dd1d1bSDag-Erling Smørgrav user = NULL;
87947dd1d1bSDag-Erling Smørgrav }
88047dd1d1bSDag-Erling Smørgrav if (hostp != NULL) {
88147dd1d1bSDag-Erling Smørgrav *hostp = host;
88247dd1d1bSDag-Erling Smørgrav host = NULL;
88347dd1d1bSDag-Erling Smørgrav }
88447dd1d1bSDag-Erling Smørgrav if (pathp != NULL) {
88547dd1d1bSDag-Erling Smørgrav *pathp = path;
88647dd1d1bSDag-Erling Smørgrav path = NULL;
88747dd1d1bSDag-Erling Smørgrav }
88847dd1d1bSDag-Erling Smørgrav ret = 0;
88947dd1d1bSDag-Erling Smørgrav out:
89047dd1d1bSDag-Erling Smørgrav free(sdup);
89147dd1d1bSDag-Erling Smørgrav free(user);
89247dd1d1bSDag-Erling Smørgrav free(host);
89347dd1d1bSDag-Erling Smørgrav free(path);
89447dd1d1bSDag-Erling Smørgrav return ret;
89547dd1d1bSDag-Erling Smørgrav }
89647dd1d1bSDag-Erling Smørgrav
89747dd1d1bSDag-Erling Smørgrav /*
898076ad2f8SDag-Erling Smørgrav * Parse a [user@]host[:port] string.
899076ad2f8SDag-Erling Smørgrav * Caller must free returned user and host.
900076ad2f8SDag-Erling Smørgrav * Any of the pointer return arguments may be NULL (useful for syntax checking).
901076ad2f8SDag-Erling Smørgrav * If user was not specified then *userp will be set to NULL.
902076ad2f8SDag-Erling Smørgrav * If port was not specified then *portp will be -1.
903076ad2f8SDag-Erling Smørgrav * Returns 0 on success, -1 on failure.
904076ad2f8SDag-Erling Smørgrav */
905076ad2f8SDag-Erling Smørgrav int
parse_user_host_port(const char * s,char ** userp,char ** hostp,int * portp)906076ad2f8SDag-Erling Smørgrav parse_user_host_port(const char *s, char **userp, char **hostp, int *portp)
907076ad2f8SDag-Erling Smørgrav {
908076ad2f8SDag-Erling Smørgrav char *sdup, *cp, *tmp;
909076ad2f8SDag-Erling Smørgrav char *user = NULL, *host = NULL;
910076ad2f8SDag-Erling Smørgrav int port = -1, ret = -1;
911076ad2f8SDag-Erling Smørgrav
912076ad2f8SDag-Erling Smørgrav if (userp != NULL)
913076ad2f8SDag-Erling Smørgrav *userp = NULL;
914076ad2f8SDag-Erling Smørgrav if (hostp != NULL)
915076ad2f8SDag-Erling Smørgrav *hostp = NULL;
916076ad2f8SDag-Erling Smørgrav if (portp != NULL)
917076ad2f8SDag-Erling Smørgrav *portp = -1;
918076ad2f8SDag-Erling Smørgrav
919076ad2f8SDag-Erling Smørgrav if ((sdup = tmp = strdup(s)) == NULL)
920076ad2f8SDag-Erling Smørgrav return -1;
921076ad2f8SDag-Erling Smørgrav /* Extract optional username */
92247dd1d1bSDag-Erling Smørgrav if ((cp = strrchr(tmp, '@')) != NULL) {
923076ad2f8SDag-Erling Smørgrav *cp = '\0';
924076ad2f8SDag-Erling Smørgrav if (*tmp == '\0')
925076ad2f8SDag-Erling Smørgrav goto out;
926076ad2f8SDag-Erling Smørgrav if ((user = strdup(tmp)) == NULL)
927076ad2f8SDag-Erling Smørgrav goto out;
928076ad2f8SDag-Erling Smørgrav tmp = cp + 1;
929076ad2f8SDag-Erling Smørgrav }
930076ad2f8SDag-Erling Smørgrav /* Extract mandatory hostname */
931076ad2f8SDag-Erling Smørgrav if ((cp = hpdelim(&tmp)) == NULL || *cp == '\0')
932076ad2f8SDag-Erling Smørgrav goto out;
933076ad2f8SDag-Erling Smørgrav host = xstrdup(cleanhostname(cp));
934076ad2f8SDag-Erling Smørgrav /* Convert and verify optional port */
935076ad2f8SDag-Erling Smørgrav if (tmp != NULL && *tmp != '\0') {
936076ad2f8SDag-Erling Smørgrav if ((port = a2port(tmp)) <= 0)
937076ad2f8SDag-Erling Smørgrav goto out;
938076ad2f8SDag-Erling Smørgrav }
939076ad2f8SDag-Erling Smørgrav /* Success */
940076ad2f8SDag-Erling Smørgrav if (userp != NULL) {
941076ad2f8SDag-Erling Smørgrav *userp = user;
942076ad2f8SDag-Erling Smørgrav user = NULL;
943076ad2f8SDag-Erling Smørgrav }
944076ad2f8SDag-Erling Smørgrav if (hostp != NULL) {
945076ad2f8SDag-Erling Smørgrav *hostp = host;
946076ad2f8SDag-Erling Smørgrav host = NULL;
947076ad2f8SDag-Erling Smørgrav }
948076ad2f8SDag-Erling Smørgrav if (portp != NULL)
949076ad2f8SDag-Erling Smørgrav *portp = port;
950076ad2f8SDag-Erling Smørgrav ret = 0;
951076ad2f8SDag-Erling Smørgrav out:
952076ad2f8SDag-Erling Smørgrav free(sdup);
953076ad2f8SDag-Erling Smørgrav free(user);
954076ad2f8SDag-Erling Smørgrav free(host);
955076ad2f8SDag-Erling Smørgrav return ret;
956076ad2f8SDag-Erling Smørgrav }
957076ad2f8SDag-Erling Smørgrav
95847dd1d1bSDag-Erling Smørgrav /*
95947dd1d1bSDag-Erling Smørgrav * Converts a two-byte hex string to decimal.
96047dd1d1bSDag-Erling Smørgrav * Returns the decimal value or -1 for invalid input.
96147dd1d1bSDag-Erling Smørgrav */
96247dd1d1bSDag-Erling Smørgrav static int
hexchar(const char * s)96347dd1d1bSDag-Erling Smørgrav hexchar(const char *s)
96447dd1d1bSDag-Erling Smørgrav {
96547dd1d1bSDag-Erling Smørgrav unsigned char result[2];
96647dd1d1bSDag-Erling Smørgrav int i;
96747dd1d1bSDag-Erling Smørgrav
96847dd1d1bSDag-Erling Smørgrav for (i = 0; i < 2; i++) {
96947dd1d1bSDag-Erling Smørgrav if (s[i] >= '0' && s[i] <= '9')
97047dd1d1bSDag-Erling Smørgrav result[i] = (unsigned char)(s[i] - '0');
97147dd1d1bSDag-Erling Smørgrav else if (s[i] >= 'a' && s[i] <= 'f')
97247dd1d1bSDag-Erling Smørgrav result[i] = (unsigned char)(s[i] - 'a') + 10;
97347dd1d1bSDag-Erling Smørgrav else if (s[i] >= 'A' && s[i] <= 'F')
97447dd1d1bSDag-Erling Smørgrav result[i] = (unsigned char)(s[i] - 'A') + 10;
97547dd1d1bSDag-Erling Smørgrav else
97647dd1d1bSDag-Erling Smørgrav return -1;
97747dd1d1bSDag-Erling Smørgrav }
97847dd1d1bSDag-Erling Smørgrav return (result[0] << 4) | result[1];
97947dd1d1bSDag-Erling Smørgrav }
98047dd1d1bSDag-Erling Smørgrav
98147dd1d1bSDag-Erling Smørgrav /*
98247dd1d1bSDag-Erling Smørgrav * Decode an url-encoded string.
98347dd1d1bSDag-Erling Smørgrav * Returns a newly allocated string on success or NULL on failure.
98447dd1d1bSDag-Erling Smørgrav */
98547dd1d1bSDag-Erling Smørgrav static char *
urldecode(const char * src)98647dd1d1bSDag-Erling Smørgrav urldecode(const char *src)
98747dd1d1bSDag-Erling Smørgrav {
98847dd1d1bSDag-Erling Smørgrav char *ret, *dst;
98947dd1d1bSDag-Erling Smørgrav int ch;
990535af610SEd Maste size_t srclen;
99147dd1d1bSDag-Erling Smørgrav
992535af610SEd Maste if ((srclen = strlen(src)) >= SIZE_MAX)
993535af610SEd Maste fatal_f("input too large");
994535af610SEd Maste ret = xmalloc(srclen + 1);
99547dd1d1bSDag-Erling Smørgrav for (dst = ret; *src != '\0'; src++) {
99647dd1d1bSDag-Erling Smørgrav switch (*src) {
99747dd1d1bSDag-Erling Smørgrav case '+':
99847dd1d1bSDag-Erling Smørgrav *dst++ = ' ';
99947dd1d1bSDag-Erling Smørgrav break;
100047dd1d1bSDag-Erling Smørgrav case '%':
100147dd1d1bSDag-Erling Smørgrav if (!isxdigit((unsigned char)src[1]) ||
100247dd1d1bSDag-Erling Smørgrav !isxdigit((unsigned char)src[2]) ||
100347dd1d1bSDag-Erling Smørgrav (ch = hexchar(src + 1)) == -1) {
100447dd1d1bSDag-Erling Smørgrav free(ret);
100547dd1d1bSDag-Erling Smørgrav return NULL;
100647dd1d1bSDag-Erling Smørgrav }
100747dd1d1bSDag-Erling Smørgrav *dst++ = ch;
100847dd1d1bSDag-Erling Smørgrav src += 2;
100947dd1d1bSDag-Erling Smørgrav break;
101047dd1d1bSDag-Erling Smørgrav default:
101147dd1d1bSDag-Erling Smørgrav *dst++ = *src;
101247dd1d1bSDag-Erling Smørgrav break;
101347dd1d1bSDag-Erling Smørgrav }
101447dd1d1bSDag-Erling Smørgrav }
101547dd1d1bSDag-Erling Smørgrav *dst = '\0';
101647dd1d1bSDag-Erling Smørgrav
101747dd1d1bSDag-Erling Smørgrav return ret;
101847dd1d1bSDag-Erling Smørgrav }
101947dd1d1bSDag-Erling Smørgrav
102047dd1d1bSDag-Erling Smørgrav /*
102147dd1d1bSDag-Erling Smørgrav * Parse an (scp|ssh|sftp)://[user@]host[:port][/path] URI.
102247dd1d1bSDag-Erling Smørgrav * See https://tools.ietf.org/html/draft-ietf-secsh-scp-sftp-ssh-uri-04
102347dd1d1bSDag-Erling Smørgrav * Either user or path may be url-encoded (but not host or port).
102447dd1d1bSDag-Erling Smørgrav * Caller must free returned user, host and path.
102547dd1d1bSDag-Erling Smørgrav * Any of the pointer return arguments may be NULL (useful for syntax checking)
102647dd1d1bSDag-Erling Smørgrav * but the scheme must always be specified.
102747dd1d1bSDag-Erling Smørgrav * If user was not specified then *userp will be set to NULL.
102847dd1d1bSDag-Erling Smørgrav * If port was not specified then *portp will be -1.
102947dd1d1bSDag-Erling Smørgrav * If path was not specified then *pathp will be set to NULL.
103047dd1d1bSDag-Erling Smørgrav * Returns 0 on success, 1 if non-uri/wrong scheme, -1 on error/invalid uri.
103147dd1d1bSDag-Erling Smørgrav */
103247dd1d1bSDag-Erling Smørgrav int
parse_uri(const char * scheme,const char * uri,char ** userp,char ** hostp,int * portp,char ** pathp)103347dd1d1bSDag-Erling Smørgrav parse_uri(const char *scheme, const char *uri, char **userp, char **hostp,
103447dd1d1bSDag-Erling Smørgrav int *portp, char **pathp)
103547dd1d1bSDag-Erling Smørgrav {
103647dd1d1bSDag-Erling Smørgrav char *uridup, *cp, *tmp, ch;
103747dd1d1bSDag-Erling Smørgrav char *user = NULL, *host = NULL, *path = NULL;
103847dd1d1bSDag-Erling Smørgrav int port = -1, ret = -1;
103947dd1d1bSDag-Erling Smørgrav size_t len;
104047dd1d1bSDag-Erling Smørgrav
104147dd1d1bSDag-Erling Smørgrav len = strlen(scheme);
104247dd1d1bSDag-Erling Smørgrav if (strncmp(uri, scheme, len) != 0 || strncmp(uri + len, "://", 3) != 0)
104347dd1d1bSDag-Erling Smørgrav return 1;
104447dd1d1bSDag-Erling Smørgrav uri += len + 3;
104547dd1d1bSDag-Erling Smørgrav
104647dd1d1bSDag-Erling Smørgrav if (userp != NULL)
104747dd1d1bSDag-Erling Smørgrav *userp = NULL;
104847dd1d1bSDag-Erling Smørgrav if (hostp != NULL)
104947dd1d1bSDag-Erling Smørgrav *hostp = NULL;
105047dd1d1bSDag-Erling Smørgrav if (portp != NULL)
105147dd1d1bSDag-Erling Smørgrav *portp = -1;
105247dd1d1bSDag-Erling Smørgrav if (pathp != NULL)
105347dd1d1bSDag-Erling Smørgrav *pathp = NULL;
105447dd1d1bSDag-Erling Smørgrav
105547dd1d1bSDag-Erling Smørgrav uridup = tmp = xstrdup(uri);
105647dd1d1bSDag-Erling Smørgrav
105747dd1d1bSDag-Erling Smørgrav /* Extract optional ssh-info (username + connection params) */
105847dd1d1bSDag-Erling Smørgrav if ((cp = strchr(tmp, '@')) != NULL) {
105947dd1d1bSDag-Erling Smørgrav char *delim;
106047dd1d1bSDag-Erling Smørgrav
106147dd1d1bSDag-Erling Smørgrav *cp = '\0';
106247dd1d1bSDag-Erling Smørgrav /* Extract username and connection params */
106347dd1d1bSDag-Erling Smørgrav if ((delim = strchr(tmp, ';')) != NULL) {
106447dd1d1bSDag-Erling Smørgrav /* Just ignore connection params for now */
106547dd1d1bSDag-Erling Smørgrav *delim = '\0';
106647dd1d1bSDag-Erling Smørgrav }
106747dd1d1bSDag-Erling Smørgrav if (*tmp == '\0') {
106847dd1d1bSDag-Erling Smørgrav /* Empty username */
106947dd1d1bSDag-Erling Smørgrav goto out;
107047dd1d1bSDag-Erling Smørgrav }
107147dd1d1bSDag-Erling Smørgrav if ((user = urldecode(tmp)) == NULL)
107247dd1d1bSDag-Erling Smørgrav goto out;
107347dd1d1bSDag-Erling Smørgrav tmp = cp + 1;
107447dd1d1bSDag-Erling Smørgrav }
107547dd1d1bSDag-Erling Smørgrav
107647dd1d1bSDag-Erling Smørgrav /* Extract mandatory hostname */
107747dd1d1bSDag-Erling Smørgrav if ((cp = hpdelim2(&tmp, &ch)) == NULL || *cp == '\0')
107847dd1d1bSDag-Erling Smørgrav goto out;
107947dd1d1bSDag-Erling Smørgrav host = xstrdup(cleanhostname(cp));
108047dd1d1bSDag-Erling Smørgrav if (!valid_domain(host, 0, NULL))
108147dd1d1bSDag-Erling Smørgrav goto out;
108247dd1d1bSDag-Erling Smørgrav
108347dd1d1bSDag-Erling Smørgrav if (tmp != NULL && *tmp != '\0') {
108447dd1d1bSDag-Erling Smørgrav if (ch == ':') {
108547dd1d1bSDag-Erling Smørgrav /* Convert and verify port. */
108647dd1d1bSDag-Erling Smørgrav if ((cp = strchr(tmp, '/')) != NULL)
108747dd1d1bSDag-Erling Smørgrav *cp = '\0';
108847dd1d1bSDag-Erling Smørgrav if ((port = a2port(tmp)) <= 0)
108947dd1d1bSDag-Erling Smørgrav goto out;
109047dd1d1bSDag-Erling Smørgrav tmp = cp ? cp + 1 : NULL;
109147dd1d1bSDag-Erling Smørgrav }
109247dd1d1bSDag-Erling Smørgrav if (tmp != NULL && *tmp != '\0') {
109347dd1d1bSDag-Erling Smørgrav /* Extract optional path */
109447dd1d1bSDag-Erling Smørgrav if ((path = urldecode(tmp)) == NULL)
109547dd1d1bSDag-Erling Smørgrav goto out;
109647dd1d1bSDag-Erling Smørgrav }
109747dd1d1bSDag-Erling Smørgrav }
109847dd1d1bSDag-Erling Smørgrav
109947dd1d1bSDag-Erling Smørgrav /* Success */
110047dd1d1bSDag-Erling Smørgrav if (userp != NULL) {
110147dd1d1bSDag-Erling Smørgrav *userp = user;
110247dd1d1bSDag-Erling Smørgrav user = NULL;
110347dd1d1bSDag-Erling Smørgrav }
110447dd1d1bSDag-Erling Smørgrav if (hostp != NULL) {
110547dd1d1bSDag-Erling Smørgrav *hostp = host;
110647dd1d1bSDag-Erling Smørgrav host = NULL;
110747dd1d1bSDag-Erling Smørgrav }
110847dd1d1bSDag-Erling Smørgrav if (portp != NULL)
110947dd1d1bSDag-Erling Smørgrav *portp = port;
111047dd1d1bSDag-Erling Smørgrav if (pathp != NULL) {
111147dd1d1bSDag-Erling Smørgrav *pathp = path;
111247dd1d1bSDag-Erling Smørgrav path = NULL;
111347dd1d1bSDag-Erling Smørgrav }
111447dd1d1bSDag-Erling Smørgrav ret = 0;
111547dd1d1bSDag-Erling Smørgrav out:
111647dd1d1bSDag-Erling Smørgrav free(uridup);
111747dd1d1bSDag-Erling Smørgrav free(user);
111847dd1d1bSDag-Erling Smørgrav free(host);
111947dd1d1bSDag-Erling Smørgrav free(path);
112047dd1d1bSDag-Erling Smørgrav return ret;
112147dd1d1bSDag-Erling Smørgrav }
112247dd1d1bSDag-Erling Smørgrav
1123ae1f160dSDag-Erling Smørgrav /* function to assist building execv() arguments */
1124ae1f160dSDag-Erling Smørgrav void
addargs(arglist * args,char * fmt,...)1125ae1f160dSDag-Erling Smørgrav addargs(arglist *args, char *fmt, ...)
1126ae1f160dSDag-Erling Smørgrav {
1127ae1f160dSDag-Erling Smørgrav va_list ap;
1128021d409fSDag-Erling Smørgrav char *cp;
1129d74d50a8SDag-Erling Smørgrav u_int nalloc;
1130021d409fSDag-Erling Smørgrav int r;
1131ae1f160dSDag-Erling Smørgrav
1132ae1f160dSDag-Erling Smørgrav va_start(ap, fmt);
1133021d409fSDag-Erling Smørgrav r = vasprintf(&cp, fmt, ap);
1134ae1f160dSDag-Erling Smørgrav va_end(ap);
1135021d409fSDag-Erling Smørgrav if (r == -1)
113687c1498dSEd Maste fatal_f("argument too long");
1137ae1f160dSDag-Erling Smørgrav
113845441295SJacques Vidrine nalloc = args->nalloc;
1139ae1f160dSDag-Erling Smørgrav if (args->list == NULL) {
114045441295SJacques Vidrine nalloc = 32;
1141ae1f160dSDag-Erling Smørgrav args->num = 0;
114287c1498dSEd Maste } else if (args->num > (256 * 1024))
114387c1498dSEd Maste fatal_f("too many arguments");
114487c1498dSEd Maste else if (args->num >= args->nalloc)
114587c1498dSEd Maste fatal_f("arglist corrupt");
114687c1498dSEd Maste else if (args->num+2 >= nalloc)
114745441295SJacques Vidrine nalloc *= 2;
1148ae1f160dSDag-Erling Smørgrav
114987c1498dSEd Maste args->list = xrecallocarray(args->list, args->nalloc,
115087c1498dSEd Maste nalloc, sizeof(char *));
115145441295SJacques Vidrine args->nalloc = nalloc;
1152021d409fSDag-Erling Smørgrav args->list[args->num++] = cp;
1153ae1f160dSDag-Erling Smørgrav args->list[args->num] = NULL;
1154ae1f160dSDag-Erling Smørgrav }
11555e8dbd04SDag-Erling Smørgrav
1156021d409fSDag-Erling Smørgrav void
replacearg(arglist * args,u_int which,char * fmt,...)1157021d409fSDag-Erling Smørgrav replacearg(arglist *args, u_int which, char *fmt, ...)
1158021d409fSDag-Erling Smørgrav {
1159021d409fSDag-Erling Smørgrav va_list ap;
1160021d409fSDag-Erling Smørgrav char *cp;
1161021d409fSDag-Erling Smørgrav int r;
1162021d409fSDag-Erling Smørgrav
1163021d409fSDag-Erling Smørgrav va_start(ap, fmt);
1164021d409fSDag-Erling Smørgrav r = vasprintf(&cp, fmt, ap);
1165021d409fSDag-Erling Smørgrav va_end(ap);
1166021d409fSDag-Erling Smørgrav if (r == -1)
116787c1498dSEd Maste fatal_f("argument too long");
116887c1498dSEd Maste if (args->list == NULL || args->num >= args->nalloc)
116987c1498dSEd Maste fatal_f("arglist corrupt");
1170021d409fSDag-Erling Smørgrav
1171021d409fSDag-Erling Smørgrav if (which >= args->num)
117287c1498dSEd Maste fatal_f("tried to replace invalid arg %d >= %d",
1173021d409fSDag-Erling Smørgrav which, args->num);
1174e4a9863fSDag-Erling Smørgrav free(args->list[which]);
1175021d409fSDag-Erling Smørgrav args->list[which] = cp;
1176021d409fSDag-Erling Smørgrav }
1177021d409fSDag-Erling Smørgrav
1178021d409fSDag-Erling Smørgrav void
freeargs(arglist * args)1179021d409fSDag-Erling Smørgrav freeargs(arglist *args)
1180021d409fSDag-Erling Smørgrav {
1181021d409fSDag-Erling Smørgrav u_int i;
1182021d409fSDag-Erling Smørgrav
118387c1498dSEd Maste if (args == NULL)
118487c1498dSEd Maste return;
118587c1498dSEd Maste if (args->list != NULL && args->num < args->nalloc) {
1186021d409fSDag-Erling Smørgrav for (i = 0; i < args->num; i++)
1187e4a9863fSDag-Erling Smørgrav free(args->list[i]);
1188e4a9863fSDag-Erling Smørgrav free(args->list);
118987c1498dSEd Maste }
1190021d409fSDag-Erling Smørgrav args->nalloc = args->num = 0;
1191021d409fSDag-Erling Smørgrav args->list = NULL;
1192021d409fSDag-Erling Smørgrav }
1193021d409fSDag-Erling Smørgrav
11945e8dbd04SDag-Erling Smørgrav /*
1195043840dfSDag-Erling Smørgrav * Expands tildes in the file name. Returns data allocated by xmalloc.
1196043840dfSDag-Erling Smørgrav * Warning: this calls getpw*.
1197043840dfSDag-Erling Smørgrav */
119819261079SEd Maste int
tilde_expand(const char * filename,uid_t uid,char ** retp)119919261079SEd Maste tilde_expand(const char *filename, uid_t uid, char **retp)
1200043840dfSDag-Erling Smørgrav {
12011323ec57SEd Maste char *ocopy = NULL, *copy, *s = NULL;
12021323ec57SEd Maste const char *path = NULL, *user = NULL;
1203043840dfSDag-Erling Smørgrav struct passwd *pw;
12041323ec57SEd Maste size_t len;
12051323ec57SEd Maste int ret = -1, r, slash;
1206043840dfSDag-Erling Smørgrav
12071323ec57SEd Maste *retp = NULL;
120819261079SEd Maste if (*filename != '~') {
120919261079SEd Maste *retp = xstrdup(filename);
121019261079SEd Maste return 0;
121119261079SEd Maste }
12121323ec57SEd Maste ocopy = copy = xstrdup(filename + 1);
1213043840dfSDag-Erling Smørgrav
12141323ec57SEd Maste if (*copy == '\0') /* ~ */
12151323ec57SEd Maste path = NULL;
12161323ec57SEd Maste else if (*copy == '/') {
12171323ec57SEd Maste copy += strspn(copy, "/");
12181323ec57SEd Maste if (*copy == '\0')
12191323ec57SEd Maste path = NULL; /* ~/ */
12201323ec57SEd Maste else
12211323ec57SEd Maste path = copy; /* ~/path */
12221323ec57SEd Maste } else {
12231323ec57SEd Maste user = copy;
12241323ec57SEd Maste if ((path = strchr(copy, '/')) != NULL) {
12251323ec57SEd Maste copy[path - copy] = '\0';
12261323ec57SEd Maste path++;
12271323ec57SEd Maste path += strspn(path, "/");
12281323ec57SEd Maste if (*path == '\0') /* ~user/ */
12291323ec57SEd Maste path = NULL;
12301323ec57SEd Maste /* else ~user/path */
123119261079SEd Maste }
12321323ec57SEd Maste /* else ~user */
12331323ec57SEd Maste }
12341323ec57SEd Maste if (user != NULL) {
123519261079SEd Maste if ((pw = getpwnam(user)) == NULL) {
123619261079SEd Maste error_f("No such user %s", user);
12371323ec57SEd Maste goto out;
123819261079SEd Maste }
12391323ec57SEd Maste } else if ((pw = getpwuid(uid)) == NULL) {
124019261079SEd Maste error_f("No such uid %ld", (long)uid);
12411323ec57SEd Maste goto out;
124219261079SEd Maste }
1243043840dfSDag-Erling Smørgrav
1244043840dfSDag-Erling Smørgrav /* Make sure directory has a trailing '/' */
12451323ec57SEd Maste slash = (len = strlen(pw->pw_dir)) == 0 || pw->pw_dir[len - 1] != '/';
1246043840dfSDag-Erling Smørgrav
12471323ec57SEd Maste if ((r = xasprintf(&s, "%s%s%s", pw->pw_dir,
12481323ec57SEd Maste slash ? "/" : "", path != NULL ? path : "")) <= 0) {
12491323ec57SEd Maste error_f("xasprintf failed");
12501323ec57SEd Maste goto out;
125119261079SEd Maste }
12521323ec57SEd Maste if (r >= PATH_MAX) {
12531323ec57SEd Maste error_f("Path too long");
12541323ec57SEd Maste goto out;
12551323ec57SEd Maste }
12561323ec57SEd Maste /* success */
12571323ec57SEd Maste ret = 0;
12581323ec57SEd Maste *retp = s;
12591323ec57SEd Maste s = NULL;
12601323ec57SEd Maste out:
12611323ec57SEd Maste free(s);
12621323ec57SEd Maste free(ocopy);
12631323ec57SEd Maste return ret;
126419261079SEd Maste }
126519261079SEd Maste
126619261079SEd Maste char *
tilde_expand_filename(const char * filename,uid_t uid)126719261079SEd Maste tilde_expand_filename(const char *filename, uid_t uid)
126819261079SEd Maste {
126919261079SEd Maste char *ret;
127019261079SEd Maste
127119261079SEd Maste if (tilde_expand(filename, uid, &ret) != 0)
127219261079SEd Maste cleanup_exit(255);
127319261079SEd Maste return ret;
1274043840dfSDag-Erling Smørgrav }
1275043840dfSDag-Erling Smørgrav
1276043840dfSDag-Erling Smørgrav /*
127719261079SEd Maste * Expand a string with a set of %[char] escapes and/or ${ENVIRONMENT}
127819261079SEd Maste * substitutions. A number of escapes may be specified as
127919261079SEd Maste * (char *escape_chars, char *replacement) pairs. The list must be terminated
128019261079SEd Maste * by a NULL escape_char. Returns replaced string in memory allocated by
128119261079SEd Maste * xmalloc which the caller must free.
1282043840dfSDag-Erling Smørgrav */
128319261079SEd Maste static char *
vdollar_percent_expand(int * parseerror,int dollar,int percent,const char * string,va_list ap)128419261079SEd Maste vdollar_percent_expand(int *parseerror, int dollar, int percent,
128519261079SEd Maste const char *string, va_list ap)
1286043840dfSDag-Erling Smørgrav {
1287069ac184SEd Maste #define EXPAND_MAX_KEYS 64
128819261079SEd Maste u_int num_keys = 0, i;
1289043840dfSDag-Erling Smørgrav struct {
1290043840dfSDag-Erling Smørgrav const char *key;
1291043840dfSDag-Erling Smørgrav const char *repl;
1292043840dfSDag-Erling Smørgrav } keys[EXPAND_MAX_KEYS];
129319261079SEd Maste struct sshbuf *buf;
129419261079SEd Maste int r, missingvar = 0;
129519261079SEd Maste char *ret = NULL, *var, *varend, *val;
129619261079SEd Maste size_t len;
1297043840dfSDag-Erling Smørgrav
129819261079SEd Maste if ((buf = sshbuf_new()) == NULL)
129919261079SEd Maste fatal_f("sshbuf_new failed");
130019261079SEd Maste if (parseerror == NULL)
130119261079SEd Maste fatal_f("null parseerror arg");
130219261079SEd Maste *parseerror = 1;
130319261079SEd Maste
130419261079SEd Maste /* Gather keys if we're doing percent expansion. */
130519261079SEd Maste if (percent) {
1306043840dfSDag-Erling Smørgrav for (num_keys = 0; num_keys < EXPAND_MAX_KEYS; num_keys++) {
1307043840dfSDag-Erling Smørgrav keys[num_keys].key = va_arg(ap, char *);
1308043840dfSDag-Erling Smørgrav if (keys[num_keys].key == NULL)
1309043840dfSDag-Erling Smørgrav break;
1310043840dfSDag-Erling Smørgrav keys[num_keys].repl = va_arg(ap, char *);
131119261079SEd Maste if (keys[num_keys].repl == NULL) {
131219261079SEd Maste fatal_f("NULL replacement for token %s",
131319261079SEd Maste keys[num_keys].key);
131419261079SEd Maste }
1315043840dfSDag-Erling Smørgrav }
1316b15c8340SDag-Erling Smørgrav if (num_keys == EXPAND_MAX_KEYS && va_arg(ap, char *) != NULL)
131719261079SEd Maste fatal_f("too many keys");
131819261079SEd Maste if (num_keys == 0)
131919261079SEd Maste fatal_f("percent expansion without token list");
132019261079SEd Maste }
1321043840dfSDag-Erling Smørgrav
1322043840dfSDag-Erling Smørgrav /* Expand string */
1323043840dfSDag-Erling Smørgrav for (i = 0; *string != '\0'; string++) {
132419261079SEd Maste /* Optionally process ${ENVIRONMENT} expansions. */
132519261079SEd Maste if (dollar && string[0] == '$' && string[1] == '{') {
132619261079SEd Maste string += 2; /* skip over '${' */
132719261079SEd Maste if ((varend = strchr(string, '}')) == NULL) {
132819261079SEd Maste error_f("environment variable '%s' missing "
132919261079SEd Maste "closing '}'", string);
133019261079SEd Maste goto out;
133119261079SEd Maste }
133219261079SEd Maste len = varend - string;
133319261079SEd Maste if (len == 0) {
133419261079SEd Maste error_f("zero-length environment variable");
133519261079SEd Maste goto out;
133619261079SEd Maste }
133719261079SEd Maste var = xmalloc(len + 1);
133819261079SEd Maste (void)strlcpy(var, string, len + 1);
133919261079SEd Maste if ((val = getenv(var)) == NULL) {
134019261079SEd Maste error_f("env var ${%s} has no value", var);
134119261079SEd Maste missingvar = 1;
134219261079SEd Maste } else {
134319261079SEd Maste debug3_f("expand ${%s} -> '%s'", var, val);
134419261079SEd Maste if ((r = sshbuf_put(buf, val, strlen(val))) !=0)
134519261079SEd Maste fatal_fr(r, "sshbuf_put ${}");
134619261079SEd Maste }
134719261079SEd Maste free(var);
134819261079SEd Maste string += len;
134919261079SEd Maste continue;
135019261079SEd Maste }
135119261079SEd Maste
135219261079SEd Maste /*
135319261079SEd Maste * Process percent expansions if we have a list of TOKENs.
135419261079SEd Maste * If we're not doing percent expansion everything just gets
135519261079SEd Maste * appended here.
135619261079SEd Maste */
135719261079SEd Maste if (*string != '%' || !percent) {
1358043840dfSDag-Erling Smørgrav append:
135919261079SEd Maste if ((r = sshbuf_put_u8(buf, *string)) != 0)
136019261079SEd Maste fatal_fr(r, "sshbuf_put_u8 %%");
1361043840dfSDag-Erling Smørgrav continue;
1362043840dfSDag-Erling Smørgrav }
1363043840dfSDag-Erling Smørgrav string++;
1364b15c8340SDag-Erling Smørgrav /* %% case */
1365043840dfSDag-Erling Smørgrav if (*string == '%')
1366043840dfSDag-Erling Smørgrav goto append;
136719261079SEd Maste if (*string == '\0') {
136819261079SEd Maste error_f("invalid format");
136919261079SEd Maste goto out;
137019261079SEd Maste }
137119261079SEd Maste for (i = 0; i < num_keys; i++) {
137219261079SEd Maste if (strchr(keys[i].key, *string) != NULL) {
137319261079SEd Maste if ((r = sshbuf_put(buf, keys[i].repl,
137419261079SEd Maste strlen(keys[i].repl))) != 0)
137519261079SEd Maste fatal_fr(r, "sshbuf_put %%-repl");
1376043840dfSDag-Erling Smørgrav break;
1377043840dfSDag-Erling Smørgrav }
1378043840dfSDag-Erling Smørgrav }
137919261079SEd Maste if (i >= num_keys) {
138019261079SEd Maste error_f("unknown key %%%c", *string);
138119261079SEd Maste goto out;
1382043840dfSDag-Erling Smørgrav }
138319261079SEd Maste }
138419261079SEd Maste if (!missingvar && (ret = sshbuf_dup_string(buf)) == NULL)
138519261079SEd Maste fatal_f("sshbuf_dup_string failed");
138619261079SEd Maste *parseerror = 0;
138719261079SEd Maste out:
138819261079SEd Maste sshbuf_free(buf);
138919261079SEd Maste return *parseerror ? NULL : ret;
1390043840dfSDag-Erling Smørgrav #undef EXPAND_MAX_KEYS
1391043840dfSDag-Erling Smørgrav }
1392043840dfSDag-Erling Smørgrav
139319261079SEd Maste /*
139419261079SEd Maste * Expand only environment variables.
139519261079SEd Maste * Note that although this function is variadic like the other similar
139619261079SEd Maste * functions, any such arguments will be unused.
139719261079SEd Maste */
139819261079SEd Maste
139919261079SEd Maste char *
dollar_expand(int * parseerr,const char * string,...)140019261079SEd Maste dollar_expand(int *parseerr, const char *string, ...)
140119261079SEd Maste {
140219261079SEd Maste char *ret;
140319261079SEd Maste int err;
140419261079SEd Maste va_list ap;
140519261079SEd Maste
140619261079SEd Maste va_start(ap, string);
140719261079SEd Maste ret = vdollar_percent_expand(&err, 1, 0, string, ap);
140819261079SEd Maste va_end(ap);
140919261079SEd Maste if (parseerr != NULL)
141019261079SEd Maste *parseerr = err;
141119261079SEd Maste return ret;
141219261079SEd Maste }
141319261079SEd Maste
141419261079SEd Maste /*
141519261079SEd Maste * Returns expanded string or NULL if a specified environment variable is
141619261079SEd Maste * not defined, or calls fatal if the string is invalid.
141719261079SEd Maste */
141819261079SEd Maste char *
percent_expand(const char * string,...)141919261079SEd Maste percent_expand(const char *string, ...)
142019261079SEd Maste {
142119261079SEd Maste char *ret;
142219261079SEd Maste int err;
142319261079SEd Maste va_list ap;
142419261079SEd Maste
142519261079SEd Maste va_start(ap, string);
142619261079SEd Maste ret = vdollar_percent_expand(&err, 0, 1, string, ap);
142719261079SEd Maste va_end(ap);
142819261079SEd Maste if (err)
142919261079SEd Maste fatal_f("failed");
143019261079SEd Maste return ret;
143119261079SEd Maste }
143219261079SEd Maste
143319261079SEd Maste /*
143419261079SEd Maste * Returns expanded string or NULL if a specified environment variable is
143519261079SEd Maste * not defined, or calls fatal if the string is invalid.
143619261079SEd Maste */
143719261079SEd Maste char *
percent_dollar_expand(const char * string,...)143819261079SEd Maste percent_dollar_expand(const char *string, ...)
143919261079SEd Maste {
144019261079SEd Maste char *ret;
144119261079SEd Maste int err;
144219261079SEd Maste va_list ap;
144319261079SEd Maste
144419261079SEd Maste va_start(ap, string);
144519261079SEd Maste ret = vdollar_percent_expand(&err, 1, 1, string, ap);
144619261079SEd Maste va_end(ap);
144719261079SEd Maste if (err)
144819261079SEd Maste fatal_f("failed");
144919261079SEd Maste return ret;
145019261079SEd Maste }
145119261079SEd Maste
1452021d409fSDag-Erling Smørgrav int
tun_open(int tun,int mode,char ** ifname)145347dd1d1bSDag-Erling Smørgrav tun_open(int tun, int mode, char **ifname)
1454021d409fSDag-Erling Smørgrav {
1455021d409fSDag-Erling Smørgrav #if defined(CUSTOM_SYS_TUN_OPEN)
145647dd1d1bSDag-Erling Smørgrav return (sys_tun_open(tun, mode, ifname));
1457021d409fSDag-Erling Smørgrav #elif defined(SSH_TUN_OPENBSD)
1458021d409fSDag-Erling Smørgrav struct ifreq ifr;
1459021d409fSDag-Erling Smørgrav char name[100];
1460021d409fSDag-Erling Smørgrav int fd = -1, sock;
1461acc1a9efSDag-Erling Smørgrav const char *tunbase = "tun";
1462acc1a9efSDag-Erling Smørgrav
146347dd1d1bSDag-Erling Smørgrav if (ifname != NULL)
146447dd1d1bSDag-Erling Smørgrav *ifname = NULL;
146547dd1d1bSDag-Erling Smørgrav
1466acc1a9efSDag-Erling Smørgrav if (mode == SSH_TUNMODE_ETHERNET)
1467acc1a9efSDag-Erling Smørgrav tunbase = "tap";
1468021d409fSDag-Erling Smørgrav
1469021d409fSDag-Erling Smørgrav /* Open the tunnel device */
1470021d409fSDag-Erling Smørgrav if (tun <= SSH_TUNID_MAX) {
1471acc1a9efSDag-Erling Smørgrav snprintf(name, sizeof(name), "/dev/%s%d", tunbase, tun);
1472021d409fSDag-Erling Smørgrav fd = open(name, O_RDWR);
1473021d409fSDag-Erling Smørgrav } else if (tun == SSH_TUNID_ANY) {
1474021d409fSDag-Erling Smørgrav for (tun = 100; tun >= 0; tun--) {
1475acc1a9efSDag-Erling Smørgrav snprintf(name, sizeof(name), "/dev/%s%d",
1476acc1a9efSDag-Erling Smørgrav tunbase, tun);
1477021d409fSDag-Erling Smørgrav if ((fd = open(name, O_RDWR)) >= 0)
1478021d409fSDag-Erling Smørgrav break;
1479021d409fSDag-Erling Smørgrav }
1480021d409fSDag-Erling Smørgrav } else {
148119261079SEd Maste debug_f("invalid tunnel %u", tun);
1482acc1a9efSDag-Erling Smørgrav return -1;
1483021d409fSDag-Erling Smørgrav }
1484021d409fSDag-Erling Smørgrav
148519261079SEd Maste if (fd == -1) {
148619261079SEd Maste debug_f("%s open: %s", name, strerror(errno));
1487acc1a9efSDag-Erling Smørgrav return -1;
1488021d409fSDag-Erling Smørgrav }
1489021d409fSDag-Erling Smørgrav
149019261079SEd Maste debug_f("%s mode %d fd %d", name, mode, fd);
1491021d409fSDag-Erling Smørgrav
1492acc1a9efSDag-Erling Smørgrav /* Bring interface up if it is not already */
1493acc1a9efSDag-Erling Smørgrav snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "%s%d", tunbase, tun);
1494021d409fSDag-Erling Smørgrav if ((sock = socket(PF_UNIX, SOCK_STREAM, 0)) == -1)
1495021d409fSDag-Erling Smørgrav goto failed;
1496021d409fSDag-Erling Smørgrav
1497acc1a9efSDag-Erling Smørgrav if (ioctl(sock, SIOCGIFFLAGS, &ifr) == -1) {
149819261079SEd Maste debug_f("get interface %s flags: %s", ifr.ifr_name,
149919261079SEd Maste strerror(errno));
1500021d409fSDag-Erling Smørgrav goto failed;
1501acc1a9efSDag-Erling Smørgrav }
1502021d409fSDag-Erling Smørgrav
1503acc1a9efSDag-Erling Smørgrav if (!(ifr.ifr_flags & IFF_UP)) {
1504021d409fSDag-Erling Smørgrav ifr.ifr_flags |= IFF_UP;
1505acc1a9efSDag-Erling Smørgrav if (ioctl(sock, SIOCSIFFLAGS, &ifr) == -1) {
150619261079SEd Maste debug_f("activate interface %s: %s", ifr.ifr_name,
150719261079SEd Maste strerror(errno));
1508021d409fSDag-Erling Smørgrav goto failed;
1509acc1a9efSDag-Erling Smørgrav }
1510acc1a9efSDag-Erling Smørgrav }
1511021d409fSDag-Erling Smørgrav
151247dd1d1bSDag-Erling Smørgrav if (ifname != NULL)
151347dd1d1bSDag-Erling Smørgrav *ifname = xstrdup(ifr.ifr_name);
151447dd1d1bSDag-Erling Smørgrav
1515021d409fSDag-Erling Smørgrav close(sock);
1516acc1a9efSDag-Erling Smørgrav return fd;
1517021d409fSDag-Erling Smørgrav
1518021d409fSDag-Erling Smørgrav failed:
1519021d409fSDag-Erling Smørgrav if (fd >= 0)
1520021d409fSDag-Erling Smørgrav close(fd);
1521021d409fSDag-Erling Smørgrav if (sock >= 0)
1522021d409fSDag-Erling Smørgrav close(sock);
1523acc1a9efSDag-Erling Smørgrav return -1;
1524021d409fSDag-Erling Smørgrav #else
1525021d409fSDag-Erling Smørgrav error("Tunnel interfaces are not supported on this platform");
1526021d409fSDag-Erling Smørgrav return (-1);
1527021d409fSDag-Erling Smørgrav #endif
1528021d409fSDag-Erling Smørgrav }
1529021d409fSDag-Erling Smørgrav
1530021d409fSDag-Erling Smørgrav void
sanitise_stdfd(void)1531021d409fSDag-Erling Smørgrav sanitise_stdfd(void)
1532021d409fSDag-Erling Smørgrav {
1533021d409fSDag-Erling Smørgrav int nullfd, dupfd;
1534021d409fSDag-Erling Smørgrav
1535021d409fSDag-Erling Smørgrav if ((nullfd = dupfd = open(_PATH_DEVNULL, O_RDWR)) == -1) {
1536cce7d346SDag-Erling Smørgrav fprintf(stderr, "Couldn't open /dev/null: %s\n",
1537cce7d346SDag-Erling Smørgrav strerror(errno));
1538021d409fSDag-Erling Smørgrav exit(1);
1539021d409fSDag-Erling Smørgrav }
1540076ad2f8SDag-Erling Smørgrav while (++dupfd <= STDERR_FILENO) {
1541076ad2f8SDag-Erling Smørgrav /* Only populate closed fds. */
1542076ad2f8SDag-Erling Smørgrav if (fcntl(dupfd, F_GETFL) == -1 && errno == EBADF) {
1543021d409fSDag-Erling Smørgrav if (dup2(nullfd, dupfd) == -1) {
1544cce7d346SDag-Erling Smørgrav fprintf(stderr, "dup2: %s\n", strerror(errno));
1545021d409fSDag-Erling Smørgrav exit(1);
1546021d409fSDag-Erling Smørgrav }
1547021d409fSDag-Erling Smørgrav }
1548076ad2f8SDag-Erling Smørgrav }
1549076ad2f8SDag-Erling Smørgrav if (nullfd > STDERR_FILENO)
1550021d409fSDag-Erling Smørgrav close(nullfd);
1551021d409fSDag-Erling Smørgrav }
1552021d409fSDag-Erling Smørgrav
1553043840dfSDag-Erling Smørgrav char *
tohex(const void * vp,size_t l)1554761efaa7SDag-Erling Smørgrav tohex(const void *vp, size_t l)
1555043840dfSDag-Erling Smørgrav {
1556761efaa7SDag-Erling Smørgrav const u_char *p = (const u_char *)vp;
1557043840dfSDag-Erling Smørgrav char b[3], *r;
1558761efaa7SDag-Erling Smørgrav size_t i, hl;
1559761efaa7SDag-Erling Smørgrav
1560761efaa7SDag-Erling Smørgrav if (l > 65536)
1561761efaa7SDag-Erling Smørgrav return xstrdup("tohex: length > 65536");
1562043840dfSDag-Erling Smørgrav
1563043840dfSDag-Erling Smørgrav hl = l * 2 + 1;
1564761efaa7SDag-Erling Smørgrav r = xcalloc(1, hl);
1565043840dfSDag-Erling Smørgrav for (i = 0; i < l; i++) {
1566761efaa7SDag-Erling Smørgrav snprintf(b, sizeof(b), "%02x", p[i]);
1567043840dfSDag-Erling Smørgrav strlcat(r, b, hl);
1568043840dfSDag-Erling Smørgrav }
1569043840dfSDag-Erling Smørgrav return (r);
1570043840dfSDag-Erling Smørgrav }
1571043840dfSDag-Erling Smørgrav
157219261079SEd Maste /*
157319261079SEd Maste * Extend string *sp by the specified format. If *sp is not NULL (or empty),
157419261079SEd Maste * then the separator 'sep' will be prepended before the formatted arguments.
157519261079SEd Maste * Extended strings are heap allocated.
157619261079SEd Maste */
157719261079SEd Maste void
xextendf(char ** sp,const char * sep,const char * fmt,...)157819261079SEd Maste xextendf(char **sp, const char *sep, const char *fmt, ...)
157919261079SEd Maste {
158019261079SEd Maste va_list ap;
158119261079SEd Maste char *tmp1, *tmp2;
158219261079SEd Maste
158319261079SEd Maste va_start(ap, fmt);
158419261079SEd Maste xvasprintf(&tmp1, fmt, ap);
158519261079SEd Maste va_end(ap);
158619261079SEd Maste
158719261079SEd Maste if (*sp == NULL || **sp == '\0') {
158819261079SEd Maste free(*sp);
158919261079SEd Maste *sp = tmp1;
159019261079SEd Maste return;
159119261079SEd Maste }
159219261079SEd Maste xasprintf(&tmp2, "%s%s%s", *sp, sep == NULL ? "" : sep, tmp1);
159319261079SEd Maste free(tmp1);
159419261079SEd Maste free(*sp);
159519261079SEd Maste *sp = tmp2;
159619261079SEd Maste }
159719261079SEd Maste
159819261079SEd Maste
1599761efaa7SDag-Erling Smørgrav u_int64_t
get_u64(const void * vp)1600761efaa7SDag-Erling Smørgrav get_u64(const void *vp)
1601761efaa7SDag-Erling Smørgrav {
1602761efaa7SDag-Erling Smørgrav const u_char *p = (const u_char *)vp;
1603761efaa7SDag-Erling Smørgrav u_int64_t v;
1604761efaa7SDag-Erling Smørgrav
1605761efaa7SDag-Erling Smørgrav v = (u_int64_t)p[0] << 56;
1606761efaa7SDag-Erling Smørgrav v |= (u_int64_t)p[1] << 48;
1607761efaa7SDag-Erling Smørgrav v |= (u_int64_t)p[2] << 40;
1608761efaa7SDag-Erling Smørgrav v |= (u_int64_t)p[3] << 32;
1609761efaa7SDag-Erling Smørgrav v |= (u_int64_t)p[4] << 24;
1610761efaa7SDag-Erling Smørgrav v |= (u_int64_t)p[5] << 16;
1611761efaa7SDag-Erling Smørgrav v |= (u_int64_t)p[6] << 8;
1612761efaa7SDag-Erling Smørgrav v |= (u_int64_t)p[7];
1613761efaa7SDag-Erling Smørgrav
1614761efaa7SDag-Erling Smørgrav return (v);
1615761efaa7SDag-Erling Smørgrav }
1616761efaa7SDag-Erling Smørgrav
1617761efaa7SDag-Erling Smørgrav u_int32_t
get_u32(const void * vp)1618761efaa7SDag-Erling Smørgrav get_u32(const void *vp)
1619761efaa7SDag-Erling Smørgrav {
1620761efaa7SDag-Erling Smørgrav const u_char *p = (const u_char *)vp;
1621761efaa7SDag-Erling Smørgrav u_int32_t v;
1622761efaa7SDag-Erling Smørgrav
1623761efaa7SDag-Erling Smørgrav v = (u_int32_t)p[0] << 24;
1624761efaa7SDag-Erling Smørgrav v |= (u_int32_t)p[1] << 16;
1625761efaa7SDag-Erling Smørgrav v |= (u_int32_t)p[2] << 8;
1626761efaa7SDag-Erling Smørgrav v |= (u_int32_t)p[3];
1627761efaa7SDag-Erling Smørgrav
1628761efaa7SDag-Erling Smørgrav return (v);
1629761efaa7SDag-Erling Smørgrav }
1630761efaa7SDag-Erling Smørgrav
1631a0ee8cc6SDag-Erling Smørgrav u_int32_t
get_u32_le(const void * vp)1632a0ee8cc6SDag-Erling Smørgrav get_u32_le(const void *vp)
1633a0ee8cc6SDag-Erling Smørgrav {
1634a0ee8cc6SDag-Erling Smørgrav const u_char *p = (const u_char *)vp;
1635a0ee8cc6SDag-Erling Smørgrav u_int32_t v;
1636a0ee8cc6SDag-Erling Smørgrav
1637a0ee8cc6SDag-Erling Smørgrav v = (u_int32_t)p[0];
1638a0ee8cc6SDag-Erling Smørgrav v |= (u_int32_t)p[1] << 8;
1639a0ee8cc6SDag-Erling Smørgrav v |= (u_int32_t)p[2] << 16;
1640a0ee8cc6SDag-Erling Smørgrav v |= (u_int32_t)p[3] << 24;
1641a0ee8cc6SDag-Erling Smørgrav
1642a0ee8cc6SDag-Erling Smørgrav return (v);
1643a0ee8cc6SDag-Erling Smørgrav }
1644a0ee8cc6SDag-Erling Smørgrav
1645761efaa7SDag-Erling Smørgrav u_int16_t
get_u16(const void * vp)1646761efaa7SDag-Erling Smørgrav get_u16(const void *vp)
1647761efaa7SDag-Erling Smørgrav {
1648761efaa7SDag-Erling Smørgrav const u_char *p = (const u_char *)vp;
1649761efaa7SDag-Erling Smørgrav u_int16_t v;
1650761efaa7SDag-Erling Smørgrav
1651761efaa7SDag-Erling Smørgrav v = (u_int16_t)p[0] << 8;
1652761efaa7SDag-Erling Smørgrav v |= (u_int16_t)p[1];
1653761efaa7SDag-Erling Smørgrav
1654761efaa7SDag-Erling Smørgrav return (v);
1655761efaa7SDag-Erling Smørgrav }
1656761efaa7SDag-Erling Smørgrav
1657761efaa7SDag-Erling Smørgrav void
put_u64(void * vp,u_int64_t v)1658761efaa7SDag-Erling Smørgrav put_u64(void *vp, u_int64_t v)
1659761efaa7SDag-Erling Smørgrav {
1660761efaa7SDag-Erling Smørgrav u_char *p = (u_char *)vp;
1661761efaa7SDag-Erling Smørgrav
1662761efaa7SDag-Erling Smørgrav p[0] = (u_char)(v >> 56) & 0xff;
1663761efaa7SDag-Erling Smørgrav p[1] = (u_char)(v >> 48) & 0xff;
1664761efaa7SDag-Erling Smørgrav p[2] = (u_char)(v >> 40) & 0xff;
1665761efaa7SDag-Erling Smørgrav p[3] = (u_char)(v >> 32) & 0xff;
1666761efaa7SDag-Erling Smørgrav p[4] = (u_char)(v >> 24) & 0xff;
1667761efaa7SDag-Erling Smørgrav p[5] = (u_char)(v >> 16) & 0xff;
1668761efaa7SDag-Erling Smørgrav p[6] = (u_char)(v >> 8) & 0xff;
1669761efaa7SDag-Erling Smørgrav p[7] = (u_char)v & 0xff;
1670761efaa7SDag-Erling Smørgrav }
1671761efaa7SDag-Erling Smørgrav
1672761efaa7SDag-Erling Smørgrav void
put_u32(void * vp,u_int32_t v)1673761efaa7SDag-Erling Smørgrav put_u32(void *vp, u_int32_t v)
1674761efaa7SDag-Erling Smørgrav {
1675761efaa7SDag-Erling Smørgrav u_char *p = (u_char *)vp;
1676761efaa7SDag-Erling Smørgrav
1677761efaa7SDag-Erling Smørgrav p[0] = (u_char)(v >> 24) & 0xff;
1678761efaa7SDag-Erling Smørgrav p[1] = (u_char)(v >> 16) & 0xff;
1679761efaa7SDag-Erling Smørgrav p[2] = (u_char)(v >> 8) & 0xff;
1680761efaa7SDag-Erling Smørgrav p[3] = (u_char)v & 0xff;
1681761efaa7SDag-Erling Smørgrav }
1682761efaa7SDag-Erling Smørgrav
1683a0ee8cc6SDag-Erling Smørgrav void
put_u32_le(void * vp,u_int32_t v)1684a0ee8cc6SDag-Erling Smørgrav put_u32_le(void *vp, u_int32_t v)
1685a0ee8cc6SDag-Erling Smørgrav {
1686a0ee8cc6SDag-Erling Smørgrav u_char *p = (u_char *)vp;
1687a0ee8cc6SDag-Erling Smørgrav
1688a0ee8cc6SDag-Erling Smørgrav p[0] = (u_char)v & 0xff;
1689a0ee8cc6SDag-Erling Smørgrav p[1] = (u_char)(v >> 8) & 0xff;
1690a0ee8cc6SDag-Erling Smørgrav p[2] = (u_char)(v >> 16) & 0xff;
1691a0ee8cc6SDag-Erling Smørgrav p[3] = (u_char)(v >> 24) & 0xff;
1692a0ee8cc6SDag-Erling Smørgrav }
1693761efaa7SDag-Erling Smørgrav
1694761efaa7SDag-Erling Smørgrav void
put_u16(void * vp,u_int16_t v)1695761efaa7SDag-Erling Smørgrav put_u16(void *vp, u_int16_t v)
1696761efaa7SDag-Erling Smørgrav {
1697761efaa7SDag-Erling Smørgrav u_char *p = (u_char *)vp;
1698761efaa7SDag-Erling Smørgrav
1699761efaa7SDag-Erling Smørgrav p[0] = (u_char)(v >> 8) & 0xff;
1700761efaa7SDag-Erling Smørgrav p[1] = (u_char)v & 0xff;
1701761efaa7SDag-Erling Smørgrav }
1702d4af9e69SDag-Erling Smørgrav
1703d4af9e69SDag-Erling Smørgrav void
ms_subtract_diff(struct timeval * start,int * ms)1704d4af9e69SDag-Erling Smørgrav ms_subtract_diff(struct timeval *start, int *ms)
1705d4af9e69SDag-Erling Smørgrav {
1706d4af9e69SDag-Erling Smørgrav struct timeval diff, finish;
1707d4af9e69SDag-Erling Smørgrav
170847dd1d1bSDag-Erling Smørgrav monotime_tv(&finish);
1709d4af9e69SDag-Erling Smørgrav timersub(&finish, start, &diff);
1710d4af9e69SDag-Erling Smørgrav *ms -= (diff.tv_sec * 1000) + (diff.tv_usec / 1000);
1711d4af9e69SDag-Erling Smørgrav }
1712d4af9e69SDag-Erling Smørgrav
1713d4af9e69SDag-Erling Smørgrav void
ms_to_timespec(struct timespec * ts,int ms)17141323ec57SEd Maste ms_to_timespec(struct timespec *ts, int ms)
1715d4af9e69SDag-Erling Smørgrav {
1716d4af9e69SDag-Erling Smørgrav if (ms < 0)
1717d4af9e69SDag-Erling Smørgrav ms = 0;
17181323ec57SEd Maste ts->tv_sec = ms / 1000;
17191323ec57SEd Maste ts->tv_nsec = (ms % 1000) * 1000 * 1000;
1720d4af9e69SDag-Erling Smørgrav }
1721d4af9e69SDag-Erling Smørgrav
172247dd1d1bSDag-Erling Smørgrav void
monotime_ts(struct timespec * ts)172347dd1d1bSDag-Erling Smørgrav monotime_ts(struct timespec *ts)
1724e4a9863fSDag-Erling Smørgrav {
172547dd1d1bSDag-Erling Smørgrav struct timeval tv;
172647dd1d1bSDag-Erling Smørgrav #if defined(HAVE_CLOCK_GETTIME) && (defined(CLOCK_BOOTTIME) || \
172747dd1d1bSDag-Erling Smørgrav defined(CLOCK_MONOTONIC) || defined(CLOCK_REALTIME))
1728e4a9863fSDag-Erling Smørgrav static int gettime_failed = 0;
1729e4a9863fSDag-Erling Smørgrav
1730e4a9863fSDag-Erling Smørgrav if (!gettime_failed) {
173147dd1d1bSDag-Erling Smørgrav # ifdef CLOCK_BOOTTIME
173247dd1d1bSDag-Erling Smørgrav if (clock_gettime(CLOCK_BOOTTIME, ts) == 0)
173347dd1d1bSDag-Erling Smørgrav return;
173447dd1d1bSDag-Erling Smørgrav # endif /* CLOCK_BOOTTIME */
173547dd1d1bSDag-Erling Smørgrav # ifdef CLOCK_MONOTONIC
173647dd1d1bSDag-Erling Smørgrav if (clock_gettime(CLOCK_MONOTONIC, ts) == 0)
173747dd1d1bSDag-Erling Smørgrav return;
173847dd1d1bSDag-Erling Smørgrav # endif /* CLOCK_MONOTONIC */
173947dd1d1bSDag-Erling Smørgrav # ifdef CLOCK_REALTIME
174047dd1d1bSDag-Erling Smørgrav /* Not monotonic, but we're almost out of options here. */
174147dd1d1bSDag-Erling Smørgrav if (clock_gettime(CLOCK_REALTIME, ts) == 0)
174247dd1d1bSDag-Erling Smørgrav return;
174347dd1d1bSDag-Erling Smørgrav # endif /* CLOCK_REALTIME */
1744e4a9863fSDag-Erling Smørgrav debug3("clock_gettime: %s", strerror(errno));
1745e4a9863fSDag-Erling Smørgrav gettime_failed = 1;
1746e4a9863fSDag-Erling Smørgrav }
174747dd1d1bSDag-Erling Smørgrav #endif /* HAVE_CLOCK_GETTIME && (BOOTTIME || MONOTONIC || REALTIME) */
174847dd1d1bSDag-Erling Smørgrav gettimeofday(&tv, NULL);
174947dd1d1bSDag-Erling Smørgrav ts->tv_sec = tv.tv_sec;
175047dd1d1bSDag-Erling Smørgrav ts->tv_nsec = (long)tv.tv_usec * 1000;
175147dd1d1bSDag-Erling Smørgrav }
1752e4a9863fSDag-Erling Smørgrav
175347dd1d1bSDag-Erling Smørgrav void
monotime_tv(struct timeval * tv)175447dd1d1bSDag-Erling Smørgrav monotime_tv(struct timeval *tv)
175547dd1d1bSDag-Erling Smørgrav {
175647dd1d1bSDag-Erling Smørgrav struct timespec ts;
175747dd1d1bSDag-Erling Smørgrav
175847dd1d1bSDag-Erling Smørgrav monotime_ts(&ts);
175947dd1d1bSDag-Erling Smørgrav tv->tv_sec = ts.tv_sec;
176047dd1d1bSDag-Erling Smørgrav tv->tv_usec = ts.tv_nsec / 1000;
176147dd1d1bSDag-Erling Smørgrav }
176247dd1d1bSDag-Erling Smørgrav
176347dd1d1bSDag-Erling Smørgrav time_t
monotime(void)176447dd1d1bSDag-Erling Smørgrav monotime(void)
176547dd1d1bSDag-Erling Smørgrav {
176647dd1d1bSDag-Erling Smørgrav struct timespec ts;
176747dd1d1bSDag-Erling Smørgrav
176847dd1d1bSDag-Erling Smørgrav monotime_ts(&ts);
176947dd1d1bSDag-Erling Smørgrav return ts.tv_sec;
1770e4a9863fSDag-Erling Smørgrav }
1771e4a9863fSDag-Erling Smørgrav
1772076ad2f8SDag-Erling Smørgrav double
monotime_double(void)1773076ad2f8SDag-Erling Smørgrav monotime_double(void)
1774076ad2f8SDag-Erling Smørgrav {
1775076ad2f8SDag-Erling Smørgrav struct timespec ts;
1776076ad2f8SDag-Erling Smørgrav
177747dd1d1bSDag-Erling Smørgrav monotime_ts(&ts);
177847dd1d1bSDag-Erling Smørgrav return ts.tv_sec + ((double)ts.tv_nsec / 1000000000);
1779076ad2f8SDag-Erling Smørgrav }
1780076ad2f8SDag-Erling Smørgrav
17814a421b63SDag-Erling Smørgrav void
bandwidth_limit_init(struct bwlimit * bw,u_int64_t kbps,size_t buflen)17824a421b63SDag-Erling Smørgrav bandwidth_limit_init(struct bwlimit *bw, u_int64_t kbps, size_t buflen)
1783e2f6069cSDag-Erling Smørgrav {
17844a421b63SDag-Erling Smørgrav bw->buflen = buflen;
17854a421b63SDag-Erling Smørgrav bw->rate = kbps;
178619261079SEd Maste bw->thresh = buflen;
17874a421b63SDag-Erling Smørgrav bw->lamt = 0;
17884a421b63SDag-Erling Smørgrav timerclear(&bw->bwstart);
17894a421b63SDag-Erling Smørgrav timerclear(&bw->bwend);
1790e2f6069cSDag-Erling Smørgrav }
17914a421b63SDag-Erling Smørgrav
17924a421b63SDag-Erling Smørgrav /* Callback from read/write loop to insert bandwidth-limiting delays */
17934a421b63SDag-Erling Smørgrav void
bandwidth_limit(struct bwlimit * bw,size_t read_len)17944a421b63SDag-Erling Smørgrav bandwidth_limit(struct bwlimit *bw, size_t read_len)
17954a421b63SDag-Erling Smørgrav {
17964a421b63SDag-Erling Smørgrav u_int64_t waitlen;
17974a421b63SDag-Erling Smørgrav struct timespec ts, rm;
17984a421b63SDag-Erling Smørgrav
179919261079SEd Maste bw->lamt += read_len;
18004a421b63SDag-Erling Smørgrav if (!timerisset(&bw->bwstart)) {
180147dd1d1bSDag-Erling Smørgrav monotime_tv(&bw->bwstart);
18024a421b63SDag-Erling Smørgrav return;
18034a421b63SDag-Erling Smørgrav }
18044a421b63SDag-Erling Smørgrav if (bw->lamt < bw->thresh)
18054a421b63SDag-Erling Smørgrav return;
18064a421b63SDag-Erling Smørgrav
180747dd1d1bSDag-Erling Smørgrav monotime_tv(&bw->bwend);
18084a421b63SDag-Erling Smørgrav timersub(&bw->bwend, &bw->bwstart, &bw->bwend);
18094a421b63SDag-Erling Smørgrav if (!timerisset(&bw->bwend))
18104a421b63SDag-Erling Smørgrav return;
18114a421b63SDag-Erling Smørgrav
18124a421b63SDag-Erling Smørgrav bw->lamt *= 8;
18134a421b63SDag-Erling Smørgrav waitlen = (double)1000000L * bw->lamt / bw->rate;
18144a421b63SDag-Erling Smørgrav
18154a421b63SDag-Erling Smørgrav bw->bwstart.tv_sec = waitlen / 1000000L;
18164a421b63SDag-Erling Smørgrav bw->bwstart.tv_usec = waitlen % 1000000L;
18174a421b63SDag-Erling Smørgrav
18184a421b63SDag-Erling Smørgrav if (timercmp(&bw->bwstart, &bw->bwend, >)) {
18194a421b63SDag-Erling Smørgrav timersub(&bw->bwstart, &bw->bwend, &bw->bwend);
18204a421b63SDag-Erling Smørgrav
18214a421b63SDag-Erling Smørgrav /* Adjust the wait time */
18224a421b63SDag-Erling Smørgrav if (bw->bwend.tv_sec) {
18234a421b63SDag-Erling Smørgrav bw->thresh /= 2;
18244a421b63SDag-Erling Smørgrav if (bw->thresh < bw->buflen / 4)
18254a421b63SDag-Erling Smørgrav bw->thresh = bw->buflen / 4;
18264a421b63SDag-Erling Smørgrav } else if (bw->bwend.tv_usec < 10000) {
18274a421b63SDag-Erling Smørgrav bw->thresh *= 2;
18284a421b63SDag-Erling Smørgrav if (bw->thresh > bw->buflen * 8)
18294a421b63SDag-Erling Smørgrav bw->thresh = bw->buflen * 8;
18304a421b63SDag-Erling Smørgrav }
18314a421b63SDag-Erling Smørgrav
18324a421b63SDag-Erling Smørgrav TIMEVAL_TO_TIMESPEC(&bw->bwend, &ts);
18334a421b63SDag-Erling Smørgrav while (nanosleep(&ts, &rm) == -1) {
18344a421b63SDag-Erling Smørgrav if (errno != EINTR)
18354a421b63SDag-Erling Smørgrav break;
18364a421b63SDag-Erling Smørgrav ts = rm;
18374a421b63SDag-Erling Smørgrav }
18384a421b63SDag-Erling Smørgrav }
18394a421b63SDag-Erling Smørgrav
18404a421b63SDag-Erling Smørgrav bw->lamt = 0;
184147dd1d1bSDag-Erling Smørgrav monotime_tv(&bw->bwstart);
18424a421b63SDag-Erling Smørgrav }
18434a421b63SDag-Erling Smørgrav
18444a421b63SDag-Erling Smørgrav /* Make a template filename for mk[sd]temp() */
18454a421b63SDag-Erling Smørgrav void
mktemp_proto(char * s,size_t len)18464a421b63SDag-Erling Smørgrav mktemp_proto(char *s, size_t len)
18474a421b63SDag-Erling Smørgrav {
18484a421b63SDag-Erling Smørgrav const char *tmpdir;
18494a421b63SDag-Erling Smørgrav int r;
18504a421b63SDag-Erling Smørgrav
18514a421b63SDag-Erling Smørgrav if ((tmpdir = getenv("TMPDIR")) != NULL) {
18524a421b63SDag-Erling Smørgrav r = snprintf(s, len, "%s/ssh-XXXXXXXXXXXX", tmpdir);
18534a421b63SDag-Erling Smørgrav if (r > 0 && (size_t)r < len)
18544a421b63SDag-Erling Smørgrav return;
18554a421b63SDag-Erling Smørgrav }
18564a421b63SDag-Erling Smørgrav r = snprintf(s, len, "/tmp/ssh-XXXXXXXXXXXX");
18574a421b63SDag-Erling Smørgrav if (r < 0 || (size_t)r >= len)
185819261079SEd Maste fatal_f("template string too short");
18594a421b63SDag-Erling Smørgrav }
18604a421b63SDag-Erling Smørgrav
18614a421b63SDag-Erling Smørgrav static const struct {
18624a421b63SDag-Erling Smørgrav const char *name;
18634a421b63SDag-Erling Smørgrav int value;
18644a421b63SDag-Erling Smørgrav } ipqos[] = {
18654f52dfbbSDag-Erling Smørgrav { "none", INT_MAX }, /* can't use 0 here; that's CS0 */
18664a421b63SDag-Erling Smørgrav { "af11", IPTOS_DSCP_AF11 },
18674a421b63SDag-Erling Smørgrav { "af12", IPTOS_DSCP_AF12 },
18684a421b63SDag-Erling Smørgrav { "af13", IPTOS_DSCP_AF13 },
1869462c32cbSDag-Erling Smørgrav { "af21", IPTOS_DSCP_AF21 },
18704a421b63SDag-Erling Smørgrav { "af22", IPTOS_DSCP_AF22 },
18714a421b63SDag-Erling Smørgrav { "af23", IPTOS_DSCP_AF23 },
18724a421b63SDag-Erling Smørgrav { "af31", IPTOS_DSCP_AF31 },
18734a421b63SDag-Erling Smørgrav { "af32", IPTOS_DSCP_AF32 },
18744a421b63SDag-Erling Smørgrav { "af33", IPTOS_DSCP_AF33 },
18754a421b63SDag-Erling Smørgrav { "af41", IPTOS_DSCP_AF41 },
18764a421b63SDag-Erling Smørgrav { "af42", IPTOS_DSCP_AF42 },
18774a421b63SDag-Erling Smørgrav { "af43", IPTOS_DSCP_AF43 },
18784a421b63SDag-Erling Smørgrav { "cs0", IPTOS_DSCP_CS0 },
18794a421b63SDag-Erling Smørgrav { "cs1", IPTOS_DSCP_CS1 },
18804a421b63SDag-Erling Smørgrav { "cs2", IPTOS_DSCP_CS2 },
18814a421b63SDag-Erling Smørgrav { "cs3", IPTOS_DSCP_CS3 },
18824a421b63SDag-Erling Smørgrav { "cs4", IPTOS_DSCP_CS4 },
18834a421b63SDag-Erling Smørgrav { "cs5", IPTOS_DSCP_CS5 },
18844a421b63SDag-Erling Smørgrav { "cs6", IPTOS_DSCP_CS6 },
18854a421b63SDag-Erling Smørgrav { "cs7", IPTOS_DSCP_CS7 },
18864a421b63SDag-Erling Smørgrav { "ef", IPTOS_DSCP_EF },
188719261079SEd Maste { "le", IPTOS_DSCP_LE },
18884a421b63SDag-Erling Smørgrav { "lowdelay", IPTOS_LOWDELAY },
18894a421b63SDag-Erling Smørgrav { "throughput", IPTOS_THROUGHPUT },
18904a421b63SDag-Erling Smørgrav { "reliability", IPTOS_RELIABILITY },
18914a421b63SDag-Erling Smørgrav { NULL, -1 }
18924a421b63SDag-Erling Smørgrav };
18934a421b63SDag-Erling Smørgrav
18944a421b63SDag-Erling Smørgrav int
parse_ipqos(const char * cp)18954a421b63SDag-Erling Smørgrav parse_ipqos(const char *cp)
18964a421b63SDag-Erling Smørgrav {
18970fdf8faeSEd Maste const char *errstr;
18984a421b63SDag-Erling Smørgrav u_int i;
18990fdf8faeSEd Maste int val;
19004a421b63SDag-Erling Smørgrav
19014a421b63SDag-Erling Smørgrav if (cp == NULL)
19024a421b63SDag-Erling Smørgrav return -1;
19034a421b63SDag-Erling Smørgrav for (i = 0; ipqos[i].name != NULL; i++) {
19044a421b63SDag-Erling Smørgrav if (strcasecmp(cp, ipqos[i].name) == 0)
19054a421b63SDag-Erling Smørgrav return ipqos[i].value;
19064a421b63SDag-Erling Smørgrav }
19074a421b63SDag-Erling Smørgrav /* Try parsing as an integer */
19080fdf8faeSEd Maste val = (int)strtonum(cp, 0, 255, &errstr);
19090fdf8faeSEd Maste if (errstr)
19104a421b63SDag-Erling Smørgrav return -1;
19114a421b63SDag-Erling Smørgrav return val;
19124a421b63SDag-Erling Smørgrav }
19134a421b63SDag-Erling Smørgrav
1914e146993eSDag-Erling Smørgrav const char *
iptos2str(int iptos)1915e146993eSDag-Erling Smørgrav iptos2str(int iptos)
1916e146993eSDag-Erling Smørgrav {
1917e146993eSDag-Erling Smørgrav int i;
1918e146993eSDag-Erling Smørgrav static char iptos_str[sizeof "0xff"];
1919e146993eSDag-Erling Smørgrav
1920e146993eSDag-Erling Smørgrav for (i = 0; ipqos[i].name != NULL; i++) {
1921e146993eSDag-Erling Smørgrav if (ipqos[i].value == iptos)
1922e146993eSDag-Erling Smørgrav return ipqos[i].name;
1923e146993eSDag-Erling Smørgrav }
1924e146993eSDag-Erling Smørgrav snprintf(iptos_str, sizeof iptos_str, "0x%02x", iptos);
1925e146993eSDag-Erling Smørgrav return iptos_str;
1926e146993eSDag-Erling Smørgrav }
1927f7167e0eSDag-Erling Smørgrav
1928f7167e0eSDag-Erling Smørgrav void
lowercase(char * s)1929f7167e0eSDag-Erling Smørgrav lowercase(char *s)
1930f7167e0eSDag-Erling Smørgrav {
1931f7167e0eSDag-Erling Smørgrav for (; *s; s++)
1932f7167e0eSDag-Erling Smørgrav *s = tolower((u_char)*s);
1933f7167e0eSDag-Erling Smørgrav }
1934a0ee8cc6SDag-Erling Smørgrav
1935a0ee8cc6SDag-Erling Smørgrav int
unix_listener(const char * path,int backlog,int unlink_first)1936a0ee8cc6SDag-Erling Smørgrav unix_listener(const char *path, int backlog, int unlink_first)
1937a0ee8cc6SDag-Erling Smørgrav {
1938a0ee8cc6SDag-Erling Smørgrav struct sockaddr_un sunaddr;
1939a0ee8cc6SDag-Erling Smørgrav int saved_errno, sock;
1940a0ee8cc6SDag-Erling Smørgrav
1941a0ee8cc6SDag-Erling Smørgrav memset(&sunaddr, 0, sizeof(sunaddr));
1942a0ee8cc6SDag-Erling Smørgrav sunaddr.sun_family = AF_UNIX;
194347dd1d1bSDag-Erling Smørgrav if (strlcpy(sunaddr.sun_path, path,
194447dd1d1bSDag-Erling Smørgrav sizeof(sunaddr.sun_path)) >= sizeof(sunaddr.sun_path)) {
194519261079SEd Maste error_f("path \"%s\" too long for Unix domain socket", path);
1946a0ee8cc6SDag-Erling Smørgrav errno = ENAMETOOLONG;
1947a0ee8cc6SDag-Erling Smørgrav return -1;
1948a0ee8cc6SDag-Erling Smørgrav }
1949a0ee8cc6SDag-Erling Smørgrav
1950a0ee8cc6SDag-Erling Smørgrav sock = socket(PF_UNIX, SOCK_STREAM, 0);
195119261079SEd Maste if (sock == -1) {
1952a0ee8cc6SDag-Erling Smørgrav saved_errno = errno;
195319261079SEd Maste error_f("socket: %.100s", strerror(errno));
1954a0ee8cc6SDag-Erling Smørgrav errno = saved_errno;
1955a0ee8cc6SDag-Erling Smørgrav return -1;
1956a0ee8cc6SDag-Erling Smørgrav }
1957a0ee8cc6SDag-Erling Smørgrav if (unlink_first == 1) {
1958a0ee8cc6SDag-Erling Smørgrav if (unlink(path) != 0 && errno != ENOENT)
1959a0ee8cc6SDag-Erling Smørgrav error("unlink(%s): %.100s", path, strerror(errno));
1960a0ee8cc6SDag-Erling Smørgrav }
196119261079SEd Maste if (bind(sock, (struct sockaddr *)&sunaddr, sizeof(sunaddr)) == -1) {
1962a0ee8cc6SDag-Erling Smørgrav saved_errno = errno;
196319261079SEd Maste error_f("cannot bind to path %s: %s", path, strerror(errno));
1964a0ee8cc6SDag-Erling Smørgrav close(sock);
1965a0ee8cc6SDag-Erling Smørgrav errno = saved_errno;
1966a0ee8cc6SDag-Erling Smørgrav return -1;
1967a0ee8cc6SDag-Erling Smørgrav }
196819261079SEd Maste if (listen(sock, backlog) == -1) {
1969a0ee8cc6SDag-Erling Smørgrav saved_errno = errno;
197019261079SEd Maste error_f("cannot listen on path %s: %s", path, strerror(errno));
1971a0ee8cc6SDag-Erling Smørgrav close(sock);
1972a0ee8cc6SDag-Erling Smørgrav unlink(path);
1973a0ee8cc6SDag-Erling Smørgrav errno = saved_errno;
1974a0ee8cc6SDag-Erling Smørgrav return -1;
1975a0ee8cc6SDag-Erling Smørgrav }
1976a0ee8cc6SDag-Erling Smørgrav return sock;
1977a0ee8cc6SDag-Erling Smørgrav }
1978a0ee8cc6SDag-Erling Smørgrav
1979b15c8340SDag-Erling Smørgrav void
sock_set_v6only(int s)1980b15c8340SDag-Erling Smørgrav sock_set_v6only(int s)
1981b15c8340SDag-Erling Smørgrav {
1982acc1a9efSDag-Erling Smørgrav #if defined(IPV6_V6ONLY) && !defined(__OpenBSD__)
1983b15c8340SDag-Erling Smørgrav int on = 1;
1984b15c8340SDag-Erling Smørgrav
1985b15c8340SDag-Erling Smørgrav debug3("%s: set socket %d IPV6_V6ONLY", __func__, s);
1986b15c8340SDag-Erling Smørgrav if (setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof(on)) == -1)
1987b15c8340SDag-Erling Smørgrav error("setsockopt IPV6_V6ONLY: %s", strerror(errno));
1988b15c8340SDag-Erling Smørgrav #endif
1989b15c8340SDag-Erling Smørgrav }
1990076ad2f8SDag-Erling Smørgrav
1991076ad2f8SDag-Erling Smørgrav /*
1992076ad2f8SDag-Erling Smørgrav * Compares two strings that maybe be NULL. Returns non-zero if strings
1993076ad2f8SDag-Erling Smørgrav * are both NULL or are identical, returns zero otherwise.
1994076ad2f8SDag-Erling Smørgrav */
1995076ad2f8SDag-Erling Smørgrav static int
strcmp_maybe_null(const char * a,const char * b)1996076ad2f8SDag-Erling Smørgrav strcmp_maybe_null(const char *a, const char *b)
1997076ad2f8SDag-Erling Smørgrav {
1998076ad2f8SDag-Erling Smørgrav if ((a == NULL && b != NULL) || (a != NULL && b == NULL))
1999076ad2f8SDag-Erling Smørgrav return 0;
2000076ad2f8SDag-Erling Smørgrav if (a != NULL && strcmp(a, b) != 0)
2001076ad2f8SDag-Erling Smørgrav return 0;
2002076ad2f8SDag-Erling Smørgrav return 1;
2003076ad2f8SDag-Erling Smørgrav }
2004076ad2f8SDag-Erling Smørgrav
2005076ad2f8SDag-Erling Smørgrav /*
2006076ad2f8SDag-Erling Smørgrav * Compare two forwards, returning non-zero if they are identical or
2007076ad2f8SDag-Erling Smørgrav * zero otherwise.
2008076ad2f8SDag-Erling Smørgrav */
2009076ad2f8SDag-Erling Smørgrav int
forward_equals(const struct Forward * a,const struct Forward * b)2010076ad2f8SDag-Erling Smørgrav forward_equals(const struct Forward *a, const struct Forward *b)
2011076ad2f8SDag-Erling Smørgrav {
2012076ad2f8SDag-Erling Smørgrav if (strcmp_maybe_null(a->listen_host, b->listen_host) == 0)
2013076ad2f8SDag-Erling Smørgrav return 0;
2014076ad2f8SDag-Erling Smørgrav if (a->listen_port != b->listen_port)
2015076ad2f8SDag-Erling Smørgrav return 0;
2016076ad2f8SDag-Erling Smørgrav if (strcmp_maybe_null(a->listen_path, b->listen_path) == 0)
2017076ad2f8SDag-Erling Smørgrav return 0;
2018076ad2f8SDag-Erling Smørgrav if (strcmp_maybe_null(a->connect_host, b->connect_host) == 0)
2019076ad2f8SDag-Erling Smørgrav return 0;
2020076ad2f8SDag-Erling Smørgrav if (a->connect_port != b->connect_port)
2021076ad2f8SDag-Erling Smørgrav return 0;
2022076ad2f8SDag-Erling Smørgrav if (strcmp_maybe_null(a->connect_path, b->connect_path) == 0)
2023076ad2f8SDag-Erling Smørgrav return 0;
2024076ad2f8SDag-Erling Smørgrav /* allocated_port and handle are not checked */
2025076ad2f8SDag-Erling Smørgrav return 1;
2026076ad2f8SDag-Erling Smørgrav }
2027076ad2f8SDag-Erling Smørgrav
20280fdf8faeSEd Maste /* returns port number, FWD_PERMIT_ANY_PORT or -1 on error */
20290fdf8faeSEd Maste int
permitopen_port(const char * p)20300fdf8faeSEd Maste permitopen_port(const char *p)
20310fdf8faeSEd Maste {
20320fdf8faeSEd Maste int port;
20330fdf8faeSEd Maste
20340fdf8faeSEd Maste if (strcmp(p, "*") == 0)
20350fdf8faeSEd Maste return FWD_PERMIT_ANY_PORT;
20360fdf8faeSEd Maste if ((port = a2port(p)) > 0)
20370fdf8faeSEd Maste return port;
20380fdf8faeSEd Maste return -1;
20390fdf8faeSEd Maste }
20400fdf8faeSEd Maste
2041ca86bcf2SDag-Erling Smørgrav /* returns 1 if process is already daemonized, 0 otherwise */
2042ca86bcf2SDag-Erling Smørgrav int
daemonized(void)2043ca86bcf2SDag-Erling Smørgrav daemonized(void)
2044ca86bcf2SDag-Erling Smørgrav {
2045ca86bcf2SDag-Erling Smørgrav int fd;
2046ca86bcf2SDag-Erling Smørgrav
2047ca86bcf2SDag-Erling Smørgrav if ((fd = open(_PATH_TTY, O_RDONLY | O_NOCTTY)) >= 0) {
2048ca86bcf2SDag-Erling Smørgrav close(fd);
2049ca86bcf2SDag-Erling Smørgrav return 0; /* have controlling terminal */
2050ca86bcf2SDag-Erling Smørgrav }
2051ca86bcf2SDag-Erling Smørgrav if (getppid() != 1)
2052ca86bcf2SDag-Erling Smørgrav return 0; /* parent is not init */
2053ca86bcf2SDag-Erling Smørgrav if (getsid(0) != getpid())
2054ca86bcf2SDag-Erling Smørgrav return 0; /* not session leader */
2055ca86bcf2SDag-Erling Smørgrav debug3("already daemonized");
2056ca86bcf2SDag-Erling Smørgrav return 1;
2057ca86bcf2SDag-Erling Smørgrav }
20584f52dfbbSDag-Erling Smørgrav
20594f52dfbbSDag-Erling Smørgrav /*
20604f52dfbbSDag-Erling Smørgrav * Splits 's' into an argument vector. Handles quoted string and basic
20614f52dfbbSDag-Erling Smørgrav * escape characters (\\, \", \'). Caller must free the argument vector
20624f52dfbbSDag-Erling Smørgrav * and its members.
20634f52dfbbSDag-Erling Smørgrav */
20644f52dfbbSDag-Erling Smørgrav int
argv_split(const char * s,int * argcp,char *** argvp,int terminate_on_comment)206519261079SEd Maste argv_split(const char *s, int *argcp, char ***argvp, int terminate_on_comment)
20664f52dfbbSDag-Erling Smørgrav {
20674f52dfbbSDag-Erling Smørgrav int r = SSH_ERR_INTERNAL_ERROR;
20684f52dfbbSDag-Erling Smørgrav int argc = 0, quote, i, j;
20694f52dfbbSDag-Erling Smørgrav char *arg, **argv = xcalloc(1, sizeof(*argv));
20704f52dfbbSDag-Erling Smørgrav
20714f52dfbbSDag-Erling Smørgrav *argvp = NULL;
20724f52dfbbSDag-Erling Smørgrav *argcp = 0;
20734f52dfbbSDag-Erling Smørgrav
20744f52dfbbSDag-Erling Smørgrav for (i = 0; s[i] != '\0'; i++) {
20754f52dfbbSDag-Erling Smørgrav /* Skip leading whitespace */
20764f52dfbbSDag-Erling Smørgrav if (s[i] == ' ' || s[i] == '\t')
20774f52dfbbSDag-Erling Smørgrav continue;
207819261079SEd Maste if (terminate_on_comment && s[i] == '#')
207919261079SEd Maste break;
20804f52dfbbSDag-Erling Smørgrav /* Start of a token */
20814f52dfbbSDag-Erling Smørgrav quote = 0;
20824f52dfbbSDag-Erling Smørgrav
20834f52dfbbSDag-Erling Smørgrav argv = xreallocarray(argv, (argc + 2), sizeof(*argv));
20844f52dfbbSDag-Erling Smørgrav arg = argv[argc++] = xcalloc(1, strlen(s + i) + 1);
20854f52dfbbSDag-Erling Smørgrav argv[argc] = NULL;
20864f52dfbbSDag-Erling Smørgrav
20874f52dfbbSDag-Erling Smørgrav /* Copy the token in, removing escapes */
20884f52dfbbSDag-Erling Smørgrav for (j = 0; s[i] != '\0'; i++) {
20894f52dfbbSDag-Erling Smørgrav if (s[i] == '\\') {
20904f52dfbbSDag-Erling Smørgrav if (s[i + 1] == '\'' ||
20914f52dfbbSDag-Erling Smørgrav s[i + 1] == '\"' ||
209219261079SEd Maste s[i + 1] == '\\' ||
209319261079SEd Maste (quote == 0 && s[i + 1] == ' ')) {
20944f52dfbbSDag-Erling Smørgrav i++; /* Skip '\' */
20954f52dfbbSDag-Erling Smørgrav arg[j++] = s[i];
20964f52dfbbSDag-Erling Smørgrav } else {
20974f52dfbbSDag-Erling Smørgrav /* Unrecognised escape */
20984f52dfbbSDag-Erling Smørgrav arg[j++] = s[i];
20994f52dfbbSDag-Erling Smørgrav }
21004f52dfbbSDag-Erling Smørgrav } else if (quote == 0 && (s[i] == ' ' || s[i] == '\t'))
21014f52dfbbSDag-Erling Smørgrav break; /* done */
210219261079SEd Maste else if (quote == 0 && (s[i] == '\"' || s[i] == '\''))
210319261079SEd Maste quote = s[i]; /* quote start */
21044f52dfbbSDag-Erling Smørgrav else if (quote != 0 && s[i] == quote)
210519261079SEd Maste quote = 0; /* quote end */
21064f52dfbbSDag-Erling Smørgrav else
21074f52dfbbSDag-Erling Smørgrav arg[j++] = s[i];
21084f52dfbbSDag-Erling Smørgrav }
21094f52dfbbSDag-Erling Smørgrav if (s[i] == '\0') {
21104f52dfbbSDag-Erling Smørgrav if (quote != 0) {
21114f52dfbbSDag-Erling Smørgrav /* Ran out of string looking for close quote */
21124f52dfbbSDag-Erling Smørgrav r = SSH_ERR_INVALID_FORMAT;
21134f52dfbbSDag-Erling Smørgrav goto out;
21144f52dfbbSDag-Erling Smørgrav }
21154f52dfbbSDag-Erling Smørgrav break;
21164f52dfbbSDag-Erling Smørgrav }
21174f52dfbbSDag-Erling Smørgrav }
21184f52dfbbSDag-Erling Smørgrav /* Success */
21194f52dfbbSDag-Erling Smørgrav *argcp = argc;
21204f52dfbbSDag-Erling Smørgrav *argvp = argv;
21214f52dfbbSDag-Erling Smørgrav argc = 0;
21224f52dfbbSDag-Erling Smørgrav argv = NULL;
21234f52dfbbSDag-Erling Smørgrav r = 0;
21244f52dfbbSDag-Erling Smørgrav out:
21254f52dfbbSDag-Erling Smørgrav if (argc != 0 && argv != NULL) {
21264f52dfbbSDag-Erling Smørgrav for (i = 0; i < argc; i++)
21274f52dfbbSDag-Erling Smørgrav free(argv[i]);
21284f52dfbbSDag-Erling Smørgrav free(argv);
21294f52dfbbSDag-Erling Smørgrav }
21304f52dfbbSDag-Erling Smørgrav return r;
21314f52dfbbSDag-Erling Smørgrav }
21324f52dfbbSDag-Erling Smørgrav
21334f52dfbbSDag-Erling Smørgrav /*
21344f52dfbbSDag-Erling Smørgrav * Reassemble an argument vector into a string, quoting and escaping as
21354f52dfbbSDag-Erling Smørgrav * necessary. Caller must free returned string.
21364f52dfbbSDag-Erling Smørgrav */
21374f52dfbbSDag-Erling Smørgrav char *
argv_assemble(int argc,char ** argv)21384f52dfbbSDag-Erling Smørgrav argv_assemble(int argc, char **argv)
21394f52dfbbSDag-Erling Smørgrav {
21404f52dfbbSDag-Erling Smørgrav int i, j, ws, r;
21414f52dfbbSDag-Erling Smørgrav char c, *ret;
21424f52dfbbSDag-Erling Smørgrav struct sshbuf *buf, *arg;
21434f52dfbbSDag-Erling Smørgrav
21444f52dfbbSDag-Erling Smørgrav if ((buf = sshbuf_new()) == NULL || (arg = sshbuf_new()) == NULL)
214519261079SEd Maste fatal_f("sshbuf_new failed");
21464f52dfbbSDag-Erling Smørgrav
21474f52dfbbSDag-Erling Smørgrav for (i = 0; i < argc; i++) {
21484f52dfbbSDag-Erling Smørgrav ws = 0;
21494f52dfbbSDag-Erling Smørgrav sshbuf_reset(arg);
21504f52dfbbSDag-Erling Smørgrav for (j = 0; argv[i][j] != '\0'; j++) {
21514f52dfbbSDag-Erling Smørgrav r = 0;
21524f52dfbbSDag-Erling Smørgrav c = argv[i][j];
21534f52dfbbSDag-Erling Smørgrav switch (c) {
21544f52dfbbSDag-Erling Smørgrav case ' ':
21554f52dfbbSDag-Erling Smørgrav case '\t':
21564f52dfbbSDag-Erling Smørgrav ws = 1;
21574f52dfbbSDag-Erling Smørgrav r = sshbuf_put_u8(arg, c);
21584f52dfbbSDag-Erling Smørgrav break;
21594f52dfbbSDag-Erling Smørgrav case '\\':
21604f52dfbbSDag-Erling Smørgrav case '\'':
21614f52dfbbSDag-Erling Smørgrav case '"':
21624f52dfbbSDag-Erling Smørgrav if ((r = sshbuf_put_u8(arg, '\\')) != 0)
21634f52dfbbSDag-Erling Smørgrav break;
21644f52dfbbSDag-Erling Smørgrav /* FALLTHROUGH */
21654f52dfbbSDag-Erling Smørgrav default:
21664f52dfbbSDag-Erling Smørgrav r = sshbuf_put_u8(arg, c);
21674f52dfbbSDag-Erling Smørgrav break;
21684f52dfbbSDag-Erling Smørgrav }
21694f52dfbbSDag-Erling Smørgrav if (r != 0)
217019261079SEd Maste fatal_fr(r, "sshbuf_put_u8");
21714f52dfbbSDag-Erling Smørgrav }
21724f52dfbbSDag-Erling Smørgrav if ((i != 0 && (r = sshbuf_put_u8(buf, ' ')) != 0) ||
21734f52dfbbSDag-Erling Smørgrav (ws != 0 && (r = sshbuf_put_u8(buf, '"')) != 0) ||
21744f52dfbbSDag-Erling Smørgrav (r = sshbuf_putb(buf, arg)) != 0 ||
21754f52dfbbSDag-Erling Smørgrav (ws != 0 && (r = sshbuf_put_u8(buf, '"')) != 0))
217619261079SEd Maste fatal_fr(r, "assemble");
21774f52dfbbSDag-Erling Smørgrav }
21784f52dfbbSDag-Erling Smørgrav if ((ret = malloc(sshbuf_len(buf) + 1)) == NULL)
217919261079SEd Maste fatal_f("malloc failed");
21804f52dfbbSDag-Erling Smørgrav memcpy(ret, sshbuf_ptr(buf), sshbuf_len(buf));
21814f52dfbbSDag-Erling Smørgrav ret[sshbuf_len(buf)] = '\0';
21824f52dfbbSDag-Erling Smørgrav sshbuf_free(buf);
21834f52dfbbSDag-Erling Smørgrav sshbuf_free(arg);
21844f52dfbbSDag-Erling Smørgrav return ret;
21854f52dfbbSDag-Erling Smørgrav }
21864f52dfbbSDag-Erling Smørgrav
218719261079SEd Maste char *
argv_next(int * argcp,char *** argvp)218819261079SEd Maste argv_next(int *argcp, char ***argvp)
218919261079SEd Maste {
219019261079SEd Maste char *ret = (*argvp)[0];
219119261079SEd Maste
219219261079SEd Maste if (*argcp > 0 && ret != NULL) {
219319261079SEd Maste (*argcp)--;
219419261079SEd Maste (*argvp)++;
219519261079SEd Maste }
219619261079SEd Maste return ret;
219719261079SEd Maste }
219819261079SEd Maste
219919261079SEd Maste void
argv_consume(int * argcp)220019261079SEd Maste argv_consume(int *argcp)
220119261079SEd Maste {
220219261079SEd Maste *argcp = 0;
220319261079SEd Maste }
220419261079SEd Maste
220519261079SEd Maste void
argv_free(char ** av,int ac)220619261079SEd Maste argv_free(char **av, int ac)
220719261079SEd Maste {
220819261079SEd Maste int i;
220919261079SEd Maste
221019261079SEd Maste if (av == NULL)
221119261079SEd Maste return;
221219261079SEd Maste for (i = 0; i < ac; i++)
221319261079SEd Maste free(av[i]);
221419261079SEd Maste free(av);
221519261079SEd Maste }
221619261079SEd Maste
22174f52dfbbSDag-Erling Smørgrav /* Returns 0 if pid exited cleanly, non-zero otherwise */
22184f52dfbbSDag-Erling Smørgrav int
exited_cleanly(pid_t pid,const char * tag,const char * cmd,int quiet)22194f52dfbbSDag-Erling Smørgrav exited_cleanly(pid_t pid, const char *tag, const char *cmd, int quiet)
22204f52dfbbSDag-Erling Smørgrav {
22214f52dfbbSDag-Erling Smørgrav int status;
22224f52dfbbSDag-Erling Smørgrav
22234f52dfbbSDag-Erling Smørgrav while (waitpid(pid, &status, 0) == -1) {
22244f52dfbbSDag-Erling Smørgrav if (errno != EINTR) {
222519261079SEd Maste error("%s waitpid: %s", tag, strerror(errno));
22264f52dfbbSDag-Erling Smørgrav return -1;
22274f52dfbbSDag-Erling Smørgrav }
22284f52dfbbSDag-Erling Smørgrav }
22294f52dfbbSDag-Erling Smørgrav if (WIFSIGNALED(status)) {
22304f52dfbbSDag-Erling Smørgrav error("%s %s exited on signal %d", tag, cmd, WTERMSIG(status));
22314f52dfbbSDag-Erling Smørgrav return -1;
22324f52dfbbSDag-Erling Smørgrav } else if (WEXITSTATUS(status) != 0) {
22334f52dfbbSDag-Erling Smørgrav do_log2(quiet ? SYSLOG_LEVEL_DEBUG1 : SYSLOG_LEVEL_INFO,
22344f52dfbbSDag-Erling Smørgrav "%s %s failed, status %d", tag, cmd, WEXITSTATUS(status));
22354f52dfbbSDag-Erling Smørgrav return -1;
22364f52dfbbSDag-Erling Smørgrav }
22374f52dfbbSDag-Erling Smørgrav return 0;
22384f52dfbbSDag-Erling Smørgrav }
22394f52dfbbSDag-Erling Smørgrav
22404f52dfbbSDag-Erling Smørgrav /*
22414f52dfbbSDag-Erling Smørgrav * Check a given path for security. This is defined as all components
22424f52dfbbSDag-Erling Smørgrav * of the path to the file must be owned by either the owner of
22434f52dfbbSDag-Erling Smørgrav * of the file or root and no directories must be group or world writable.
22444f52dfbbSDag-Erling Smørgrav *
22454f52dfbbSDag-Erling Smørgrav * XXX Should any specific check be done for sym links ?
22464f52dfbbSDag-Erling Smørgrav *
22474f52dfbbSDag-Erling Smørgrav * Takes a file name, its stat information (preferably from fstat() to
22484f52dfbbSDag-Erling Smørgrav * avoid races), the uid of the expected owner, their home directory and an
22494f52dfbbSDag-Erling Smørgrav * error buffer plus max size as arguments.
22504f52dfbbSDag-Erling Smørgrav *
22514f52dfbbSDag-Erling Smørgrav * Returns 0 on success and -1 on failure
22524f52dfbbSDag-Erling Smørgrav */
22534f52dfbbSDag-Erling Smørgrav int
safe_path(const char * name,struct stat * stp,const char * pw_dir,uid_t uid,char * err,size_t errlen)22544f52dfbbSDag-Erling Smørgrav safe_path(const char *name, struct stat *stp, const char *pw_dir,
22554f52dfbbSDag-Erling Smørgrav uid_t uid, char *err, size_t errlen)
22564f52dfbbSDag-Erling Smørgrav {
22574f52dfbbSDag-Erling Smørgrav char buf[PATH_MAX], homedir[PATH_MAX];
22584f52dfbbSDag-Erling Smørgrav char *cp;
22594f52dfbbSDag-Erling Smørgrav int comparehome = 0;
22604f52dfbbSDag-Erling Smørgrav struct stat st;
22614f52dfbbSDag-Erling Smørgrav
22624f52dfbbSDag-Erling Smørgrav if (realpath(name, buf) == NULL) {
22634f52dfbbSDag-Erling Smørgrav snprintf(err, errlen, "realpath %s failed: %s", name,
22644f52dfbbSDag-Erling Smørgrav strerror(errno));
22654f52dfbbSDag-Erling Smørgrav return -1;
22664f52dfbbSDag-Erling Smørgrav }
22674f52dfbbSDag-Erling Smørgrav if (pw_dir != NULL && realpath(pw_dir, homedir) != NULL)
22684f52dfbbSDag-Erling Smørgrav comparehome = 1;
22694f52dfbbSDag-Erling Smørgrav
22704f52dfbbSDag-Erling Smørgrav if (!S_ISREG(stp->st_mode)) {
22714f52dfbbSDag-Erling Smørgrav snprintf(err, errlen, "%s is not a regular file", buf);
22724f52dfbbSDag-Erling Smørgrav return -1;
22734f52dfbbSDag-Erling Smørgrav }
22744f52dfbbSDag-Erling Smørgrav if ((!platform_sys_dir_uid(stp->st_uid) && stp->st_uid != uid) ||
22754f52dfbbSDag-Erling Smørgrav (stp->st_mode & 022) != 0) {
22764f52dfbbSDag-Erling Smørgrav snprintf(err, errlen, "bad ownership or modes for file %s",
22774f52dfbbSDag-Erling Smørgrav buf);
22784f52dfbbSDag-Erling Smørgrav return -1;
22794f52dfbbSDag-Erling Smørgrav }
22804f52dfbbSDag-Erling Smørgrav
22814f52dfbbSDag-Erling Smørgrav /* for each component of the canonical path, walking upwards */
22824f52dfbbSDag-Erling Smørgrav for (;;) {
22834f52dfbbSDag-Erling Smørgrav if ((cp = dirname(buf)) == NULL) {
22844f52dfbbSDag-Erling Smørgrav snprintf(err, errlen, "dirname() failed");
22854f52dfbbSDag-Erling Smørgrav return -1;
22864f52dfbbSDag-Erling Smørgrav }
22874f52dfbbSDag-Erling Smørgrav strlcpy(buf, cp, sizeof(buf));
22884f52dfbbSDag-Erling Smørgrav
228919261079SEd Maste if (stat(buf, &st) == -1 ||
22904f52dfbbSDag-Erling Smørgrav (!platform_sys_dir_uid(st.st_uid) && st.st_uid != uid) ||
22914f52dfbbSDag-Erling Smørgrav (st.st_mode & 022) != 0) {
22924f52dfbbSDag-Erling Smørgrav snprintf(err, errlen,
22934f52dfbbSDag-Erling Smørgrav "bad ownership or modes for directory %s", buf);
22944f52dfbbSDag-Erling Smørgrav return -1;
22954f52dfbbSDag-Erling Smørgrav }
22964f52dfbbSDag-Erling Smørgrav
22974f52dfbbSDag-Erling Smørgrav /* If are past the homedir then we can stop */
22984f52dfbbSDag-Erling Smørgrav if (comparehome && strcmp(homedir, buf) == 0)
22994f52dfbbSDag-Erling Smørgrav break;
23004f52dfbbSDag-Erling Smørgrav
23014f52dfbbSDag-Erling Smørgrav /*
23024f52dfbbSDag-Erling Smørgrav * dirname should always complete with a "/" path,
23034f52dfbbSDag-Erling Smørgrav * but we can be paranoid and check for "." too
23044f52dfbbSDag-Erling Smørgrav */
23054f52dfbbSDag-Erling Smørgrav if ((strcmp("/", buf) == 0) || (strcmp(".", buf) == 0))
23064f52dfbbSDag-Erling Smørgrav break;
23074f52dfbbSDag-Erling Smørgrav }
23084f52dfbbSDag-Erling Smørgrav return 0;
23094f52dfbbSDag-Erling Smørgrav }
23104f52dfbbSDag-Erling Smørgrav
23114f52dfbbSDag-Erling Smørgrav /*
23124f52dfbbSDag-Erling Smørgrav * Version of safe_path() that accepts an open file descriptor to
23134f52dfbbSDag-Erling Smørgrav * avoid races.
23144f52dfbbSDag-Erling Smørgrav *
23154f52dfbbSDag-Erling Smørgrav * Returns 0 on success and -1 on failure
23164f52dfbbSDag-Erling Smørgrav */
23174f52dfbbSDag-Erling Smørgrav int
safe_path_fd(int fd,const char * file,struct passwd * pw,char * err,size_t errlen)23184f52dfbbSDag-Erling Smørgrav safe_path_fd(int fd, const char *file, struct passwd *pw,
23194f52dfbbSDag-Erling Smørgrav char *err, size_t errlen)
23204f52dfbbSDag-Erling Smørgrav {
23214f52dfbbSDag-Erling Smørgrav struct stat st;
23224f52dfbbSDag-Erling Smørgrav
23234f52dfbbSDag-Erling Smørgrav /* check the open file to avoid races */
232419261079SEd Maste if (fstat(fd, &st) == -1) {
23254f52dfbbSDag-Erling Smørgrav snprintf(err, errlen, "cannot stat file %s: %s",
23264f52dfbbSDag-Erling Smørgrav file, strerror(errno));
23274f52dfbbSDag-Erling Smørgrav return -1;
23284f52dfbbSDag-Erling Smørgrav }
23294f52dfbbSDag-Erling Smørgrav return safe_path(file, &st, pw->pw_dir, pw->pw_uid, err, errlen);
23304f52dfbbSDag-Erling Smørgrav }
23314f52dfbbSDag-Erling Smørgrav
23324f52dfbbSDag-Erling Smørgrav /*
23334f52dfbbSDag-Erling Smørgrav * Sets the value of the given variable in the environment. If the variable
23344f52dfbbSDag-Erling Smørgrav * already exists, its value is overridden.
23354f52dfbbSDag-Erling Smørgrav */
23364f52dfbbSDag-Erling Smørgrav void
child_set_env(char *** envp,u_int * envsizep,const char * name,const char * value)23374f52dfbbSDag-Erling Smørgrav child_set_env(char ***envp, u_int *envsizep, const char *name,
23384f52dfbbSDag-Erling Smørgrav const char *value)
23394f52dfbbSDag-Erling Smørgrav {
23404f52dfbbSDag-Erling Smørgrav char **env;
23414f52dfbbSDag-Erling Smørgrav u_int envsize;
23424f52dfbbSDag-Erling Smørgrav u_int i, namelen;
23434f52dfbbSDag-Erling Smørgrav
23444f52dfbbSDag-Erling Smørgrav if (strchr(name, '=') != NULL) {
23454f52dfbbSDag-Erling Smørgrav error("Invalid environment variable \"%.100s\"", name);
23464f52dfbbSDag-Erling Smørgrav return;
23474f52dfbbSDag-Erling Smørgrav }
23484f52dfbbSDag-Erling Smørgrav
23494f52dfbbSDag-Erling Smørgrav /*
23504f52dfbbSDag-Erling Smørgrav * If we're passed an uninitialized list, allocate a single null
23514f52dfbbSDag-Erling Smørgrav * entry before continuing.
23524f52dfbbSDag-Erling Smørgrav */
2353535af610SEd Maste if ((*envp == NULL) != (*envsizep == 0))
2354535af610SEd Maste fatal_f("environment size mismatch");
23554f52dfbbSDag-Erling Smørgrav if (*envp == NULL && *envsizep == 0) {
23564f52dfbbSDag-Erling Smørgrav *envp = xmalloc(sizeof(char *));
23574f52dfbbSDag-Erling Smørgrav *envp[0] = NULL;
23584f52dfbbSDag-Erling Smørgrav *envsizep = 1;
23594f52dfbbSDag-Erling Smørgrav }
23604f52dfbbSDag-Erling Smørgrav
23614f52dfbbSDag-Erling Smørgrav /*
23624f52dfbbSDag-Erling Smørgrav * Find the slot where the value should be stored. If the variable
23634f52dfbbSDag-Erling Smørgrav * already exists, we reuse the slot; otherwise we append a new slot
23644f52dfbbSDag-Erling Smørgrav * at the end of the array, expanding if necessary.
23654f52dfbbSDag-Erling Smørgrav */
23664f52dfbbSDag-Erling Smørgrav env = *envp;
23674f52dfbbSDag-Erling Smørgrav namelen = strlen(name);
23684f52dfbbSDag-Erling Smørgrav for (i = 0; env[i]; i++)
23694f52dfbbSDag-Erling Smørgrav if (strncmp(env[i], name, namelen) == 0 && env[i][namelen] == '=')
23704f52dfbbSDag-Erling Smørgrav break;
23714f52dfbbSDag-Erling Smørgrav if (env[i]) {
23724f52dfbbSDag-Erling Smørgrav /* Reuse the slot. */
23734f52dfbbSDag-Erling Smørgrav free(env[i]);
23744f52dfbbSDag-Erling Smørgrav } else {
23754f52dfbbSDag-Erling Smørgrav /* New variable. Expand if necessary. */
23764f52dfbbSDag-Erling Smørgrav envsize = *envsizep;
23774f52dfbbSDag-Erling Smørgrav if (i >= envsize - 1) {
23784f52dfbbSDag-Erling Smørgrav if (envsize >= 1000)
23794f52dfbbSDag-Erling Smørgrav fatal("child_set_env: too many env vars");
23804f52dfbbSDag-Erling Smørgrav envsize += 50;
23814f52dfbbSDag-Erling Smørgrav env = (*envp) = xreallocarray(env, envsize, sizeof(char *));
23824f52dfbbSDag-Erling Smørgrav *envsizep = envsize;
23834f52dfbbSDag-Erling Smørgrav }
23844f52dfbbSDag-Erling Smørgrav /* Need to set the NULL pointer at end of array beyond the new slot. */
23854f52dfbbSDag-Erling Smørgrav env[i + 1] = NULL;
23864f52dfbbSDag-Erling Smørgrav }
23874f52dfbbSDag-Erling Smørgrav
23884f52dfbbSDag-Erling Smørgrav /* Allocate space and format the variable in the appropriate slot. */
238947dd1d1bSDag-Erling Smørgrav /* XXX xasprintf */
23904f52dfbbSDag-Erling Smørgrav env[i] = xmalloc(strlen(name) + 1 + strlen(value) + 1);
23914f52dfbbSDag-Erling Smørgrav snprintf(env[i], strlen(name) + 1 + strlen(value) + 1, "%s=%s", name, value);
23924f52dfbbSDag-Erling Smørgrav }
23934f52dfbbSDag-Erling Smørgrav
239447dd1d1bSDag-Erling Smørgrav /*
239547dd1d1bSDag-Erling Smørgrav * Check and optionally lowercase a domain name, also removes trailing '.'
239647dd1d1bSDag-Erling Smørgrav * Returns 1 on success and 0 on failure, storing an error message in errstr.
239747dd1d1bSDag-Erling Smørgrav */
239847dd1d1bSDag-Erling Smørgrav int
valid_domain(char * name,int makelower,const char ** errstr)239947dd1d1bSDag-Erling Smørgrav valid_domain(char *name, int makelower, const char **errstr)
240047dd1d1bSDag-Erling Smørgrav {
240147dd1d1bSDag-Erling Smørgrav size_t i, l = strlen(name);
240247dd1d1bSDag-Erling Smørgrav u_char c, last = '\0';
240347dd1d1bSDag-Erling Smørgrav static char errbuf[256];
240447dd1d1bSDag-Erling Smørgrav
240547dd1d1bSDag-Erling Smørgrav if (l == 0) {
240647dd1d1bSDag-Erling Smørgrav strlcpy(errbuf, "empty domain name", sizeof(errbuf));
240747dd1d1bSDag-Erling Smørgrav goto bad;
240847dd1d1bSDag-Erling Smørgrav }
240947dd1d1bSDag-Erling Smørgrav if (!isalpha((u_char)name[0]) && !isdigit((u_char)name[0])) {
241047dd1d1bSDag-Erling Smørgrav snprintf(errbuf, sizeof(errbuf), "domain name \"%.100s\" "
241147dd1d1bSDag-Erling Smørgrav "starts with invalid character", name);
241247dd1d1bSDag-Erling Smørgrav goto bad;
241347dd1d1bSDag-Erling Smørgrav }
241447dd1d1bSDag-Erling Smørgrav for (i = 0; i < l; i++) {
241547dd1d1bSDag-Erling Smørgrav c = tolower((u_char)name[i]);
241647dd1d1bSDag-Erling Smørgrav if (makelower)
241747dd1d1bSDag-Erling Smørgrav name[i] = (char)c;
241847dd1d1bSDag-Erling Smørgrav if (last == '.' && c == '.') {
241947dd1d1bSDag-Erling Smørgrav snprintf(errbuf, sizeof(errbuf), "domain name "
242047dd1d1bSDag-Erling Smørgrav "\"%.100s\" contains consecutive separators", name);
242147dd1d1bSDag-Erling Smørgrav goto bad;
242247dd1d1bSDag-Erling Smørgrav }
242347dd1d1bSDag-Erling Smørgrav if (c != '.' && c != '-' && !isalnum(c) &&
242447dd1d1bSDag-Erling Smørgrav c != '_') /* technically invalid, but common */ {
242547dd1d1bSDag-Erling Smørgrav snprintf(errbuf, sizeof(errbuf), "domain name "
242647dd1d1bSDag-Erling Smørgrav "\"%.100s\" contains invalid characters", name);
242747dd1d1bSDag-Erling Smørgrav goto bad;
242847dd1d1bSDag-Erling Smørgrav }
242947dd1d1bSDag-Erling Smørgrav last = c;
243047dd1d1bSDag-Erling Smørgrav }
243147dd1d1bSDag-Erling Smørgrav if (name[l - 1] == '.')
243247dd1d1bSDag-Erling Smørgrav name[l - 1] = '\0';
243347dd1d1bSDag-Erling Smørgrav if (errstr != NULL)
243447dd1d1bSDag-Erling Smørgrav *errstr = NULL;
243547dd1d1bSDag-Erling Smørgrav return 1;
243647dd1d1bSDag-Erling Smørgrav bad:
243747dd1d1bSDag-Erling Smørgrav if (errstr != NULL)
243847dd1d1bSDag-Erling Smørgrav *errstr = errbuf;
243947dd1d1bSDag-Erling Smørgrav return 0;
244047dd1d1bSDag-Erling Smørgrav }
244147dd1d1bSDag-Erling Smørgrav
24422f513db7SEd Maste /*
24432f513db7SEd Maste * Verify that a environment variable name (not including initial '$') is
24442f513db7SEd Maste * valid; consisting of one or more alphanumeric or underscore characters only.
24452f513db7SEd Maste * Returns 1 on valid, 0 otherwise.
24462f513db7SEd Maste */
24472f513db7SEd Maste int
valid_env_name(const char * name)24482f513db7SEd Maste valid_env_name(const char *name)
24492f513db7SEd Maste {
24502f513db7SEd Maste const char *cp;
24512f513db7SEd Maste
24522f513db7SEd Maste if (name[0] == '\0')
24532f513db7SEd Maste return 0;
24542f513db7SEd Maste for (cp = name; *cp != '\0'; cp++) {
24552f513db7SEd Maste if (!isalnum((u_char)*cp) && *cp != '_')
24562f513db7SEd Maste return 0;
24572f513db7SEd Maste }
24582f513db7SEd Maste return 1;
24592f513db7SEd Maste }
24602f513db7SEd Maste
246147dd1d1bSDag-Erling Smørgrav const char *
atoi_err(const char * nptr,int * val)246247dd1d1bSDag-Erling Smørgrav atoi_err(const char *nptr, int *val)
246347dd1d1bSDag-Erling Smørgrav {
246447dd1d1bSDag-Erling Smørgrav const char *errstr = NULL;
246547dd1d1bSDag-Erling Smørgrav
246647dd1d1bSDag-Erling Smørgrav if (nptr == NULL || *nptr == '\0')
246747dd1d1bSDag-Erling Smørgrav return "missing";
24680fdf8faeSEd Maste *val = strtonum(nptr, 0, INT_MAX, &errstr);
246947dd1d1bSDag-Erling Smørgrav return errstr;
247047dd1d1bSDag-Erling Smørgrav }
247147dd1d1bSDag-Erling Smørgrav
247247dd1d1bSDag-Erling Smørgrav int
parse_absolute_time(const char * s,uint64_t * tp)247347dd1d1bSDag-Erling Smørgrav parse_absolute_time(const char *s, uint64_t *tp)
247447dd1d1bSDag-Erling Smørgrav {
247547dd1d1bSDag-Erling Smørgrav struct tm tm;
247647dd1d1bSDag-Erling Smørgrav time_t tt;
247747dd1d1bSDag-Erling Smørgrav char buf[32], *fmt;
247838a52bd3SEd Maste const char *cp;
247938a52bd3SEd Maste size_t l;
248038a52bd3SEd Maste int is_utc = 0;
248147dd1d1bSDag-Erling Smørgrav
248247dd1d1bSDag-Erling Smørgrav *tp = 0;
248347dd1d1bSDag-Erling Smørgrav
248438a52bd3SEd Maste l = strlen(s);
248538a52bd3SEd Maste if (l > 1 && strcasecmp(s + l - 1, "Z") == 0) {
248638a52bd3SEd Maste is_utc = 1;
248738a52bd3SEd Maste l--;
248838a52bd3SEd Maste } else if (l > 3 && strcasecmp(s + l - 3, "UTC") == 0) {
248938a52bd3SEd Maste is_utc = 1;
249038a52bd3SEd Maste l -= 3;
249138a52bd3SEd Maste }
249247dd1d1bSDag-Erling Smørgrav /*
249347dd1d1bSDag-Erling Smørgrav * POSIX strptime says "The application shall ensure that there
249447dd1d1bSDag-Erling Smørgrav * is white-space or other non-alphanumeric characters between
249547dd1d1bSDag-Erling Smørgrav * any two conversion specifications" so arrange things this way.
249647dd1d1bSDag-Erling Smørgrav */
249738a52bd3SEd Maste switch (l) {
249847dd1d1bSDag-Erling Smørgrav case 8: /* YYYYMMDD */
249947dd1d1bSDag-Erling Smørgrav fmt = "%Y-%m-%d";
250047dd1d1bSDag-Erling Smørgrav snprintf(buf, sizeof(buf), "%.4s-%.2s-%.2s", s, s + 4, s + 6);
250147dd1d1bSDag-Erling Smørgrav break;
250247dd1d1bSDag-Erling Smørgrav case 12: /* YYYYMMDDHHMM */
250347dd1d1bSDag-Erling Smørgrav fmt = "%Y-%m-%dT%H:%M";
250447dd1d1bSDag-Erling Smørgrav snprintf(buf, sizeof(buf), "%.4s-%.2s-%.2sT%.2s:%.2s",
250547dd1d1bSDag-Erling Smørgrav s, s + 4, s + 6, s + 8, s + 10);
250647dd1d1bSDag-Erling Smørgrav break;
250747dd1d1bSDag-Erling Smørgrav case 14: /* YYYYMMDDHHMMSS */
250847dd1d1bSDag-Erling Smørgrav fmt = "%Y-%m-%dT%H:%M:%S";
250947dd1d1bSDag-Erling Smørgrav snprintf(buf, sizeof(buf), "%.4s-%.2s-%.2sT%.2s:%.2s:%.2s",
251047dd1d1bSDag-Erling Smørgrav s, s + 4, s + 6, s + 8, s + 10, s + 12);
251147dd1d1bSDag-Erling Smørgrav break;
251247dd1d1bSDag-Erling Smørgrav default:
251347dd1d1bSDag-Erling Smørgrav return SSH_ERR_INVALID_FORMAT;
251447dd1d1bSDag-Erling Smørgrav }
251547dd1d1bSDag-Erling Smørgrav
251647dd1d1bSDag-Erling Smørgrav memset(&tm, 0, sizeof(tm));
251738a52bd3SEd Maste if ((cp = strptime(buf, fmt, &tm)) == NULL || *cp != '\0')
251847dd1d1bSDag-Erling Smørgrav return SSH_ERR_INVALID_FORMAT;
251938a52bd3SEd Maste if (is_utc) {
252038a52bd3SEd Maste if ((tt = timegm(&tm)) < 0)
252138a52bd3SEd Maste return SSH_ERR_INVALID_FORMAT;
252238a52bd3SEd Maste } else {
252347dd1d1bSDag-Erling Smørgrav if ((tt = mktime(&tm)) < 0)
252447dd1d1bSDag-Erling Smørgrav return SSH_ERR_INVALID_FORMAT;
252538a52bd3SEd Maste }
252647dd1d1bSDag-Erling Smørgrav /* success */
252747dd1d1bSDag-Erling Smørgrav *tp = (uint64_t)tt;
252847dd1d1bSDag-Erling Smørgrav return 0;
252947dd1d1bSDag-Erling Smørgrav }
253047dd1d1bSDag-Erling Smørgrav
253147dd1d1bSDag-Erling Smørgrav void
format_absolute_time(uint64_t t,char * buf,size_t len)253247dd1d1bSDag-Erling Smørgrav format_absolute_time(uint64_t t, char *buf, size_t len)
253347dd1d1bSDag-Erling Smørgrav {
253419261079SEd Maste time_t tt = t > SSH_TIME_T_MAX ? SSH_TIME_T_MAX : t;
253547dd1d1bSDag-Erling Smørgrav struct tm tm;
253647dd1d1bSDag-Erling Smørgrav
253747dd1d1bSDag-Erling Smørgrav localtime_r(&tt, &tm);
253847dd1d1bSDag-Erling Smørgrav strftime(buf, len, "%Y-%m-%dT%H:%M:%S", &tm);
253947dd1d1bSDag-Erling Smørgrav }
254019261079SEd Maste
2541069ac184SEd Maste /*
2542069ac184SEd Maste * Parse a "pattern=interval" clause (e.g. a ChannelTimeout).
2543069ac184SEd Maste * Returns 0 on success or non-zero on failure.
2544069ac184SEd Maste * Caller must free *typep.
2545069ac184SEd Maste */
2546069ac184SEd Maste int
parse_pattern_interval(const char * s,char ** typep,int * secsp)2547069ac184SEd Maste parse_pattern_interval(const char *s, char **typep, int *secsp)
2548069ac184SEd Maste {
2549069ac184SEd Maste char *cp, *sdup;
2550069ac184SEd Maste int secs;
2551069ac184SEd Maste
2552069ac184SEd Maste if (typep != NULL)
2553069ac184SEd Maste *typep = NULL;
2554069ac184SEd Maste if (secsp != NULL)
2555069ac184SEd Maste *secsp = 0;
2556069ac184SEd Maste if (s == NULL)
2557069ac184SEd Maste return -1;
2558069ac184SEd Maste sdup = xstrdup(s);
2559069ac184SEd Maste
2560069ac184SEd Maste if ((cp = strchr(sdup, '=')) == NULL || cp == sdup) {
2561069ac184SEd Maste free(sdup);
2562069ac184SEd Maste return -1;
2563069ac184SEd Maste }
2564069ac184SEd Maste *cp++ = '\0';
2565069ac184SEd Maste if ((secs = convtime(cp)) < 0) {
2566069ac184SEd Maste free(sdup);
2567069ac184SEd Maste return -1;
2568069ac184SEd Maste }
2569069ac184SEd Maste /* success */
2570069ac184SEd Maste if (typep != NULL)
2571069ac184SEd Maste *typep = xstrdup(sdup);
2572069ac184SEd Maste if (secsp != NULL)
2573069ac184SEd Maste *secsp = secs;
2574069ac184SEd Maste free(sdup);
2575069ac184SEd Maste return 0;
2576069ac184SEd Maste }
2577069ac184SEd Maste
257819261079SEd Maste /* check if path is absolute */
257919261079SEd Maste int
path_absolute(const char * path)258019261079SEd Maste path_absolute(const char *path)
258119261079SEd Maste {
258219261079SEd Maste return (*path == '/') ? 1 : 0;
258319261079SEd Maste }
258419261079SEd Maste
258519261079SEd Maste void
skip_space(char ** cpp)258619261079SEd Maste skip_space(char **cpp)
258719261079SEd Maste {
258819261079SEd Maste char *cp;
258919261079SEd Maste
259019261079SEd Maste for (cp = *cpp; *cp == ' ' || *cp == '\t'; cp++)
259119261079SEd Maste ;
259219261079SEd Maste *cpp = cp;
259319261079SEd Maste }
259419261079SEd Maste
259519261079SEd Maste /* authorized_key-style options parsing helpers */
259619261079SEd Maste
259719261079SEd Maste /*
259819261079SEd Maste * Match flag 'opt' in *optsp, and if allow_negate is set then also match
259919261079SEd Maste * 'no-opt'. Returns -1 if option not matched, 1 if option matches or 0
260019261079SEd Maste * if negated option matches.
260119261079SEd Maste * If the option or negated option matches, then *optsp is updated to
260219261079SEd Maste * point to the first character after the option.
260319261079SEd Maste */
260419261079SEd Maste int
opt_flag(const char * opt,int allow_negate,const char ** optsp)260519261079SEd Maste opt_flag(const char *opt, int allow_negate, const char **optsp)
260619261079SEd Maste {
260719261079SEd Maste size_t opt_len = strlen(opt);
260819261079SEd Maste const char *opts = *optsp;
260919261079SEd Maste int negate = 0;
261019261079SEd Maste
261119261079SEd Maste if (allow_negate && strncasecmp(opts, "no-", 3) == 0) {
261219261079SEd Maste opts += 3;
261319261079SEd Maste negate = 1;
261419261079SEd Maste }
261519261079SEd Maste if (strncasecmp(opts, opt, opt_len) == 0) {
261619261079SEd Maste *optsp = opts + opt_len;
261719261079SEd Maste return negate ? 0 : 1;
261819261079SEd Maste }
261919261079SEd Maste return -1;
262019261079SEd Maste }
262119261079SEd Maste
262219261079SEd Maste char *
opt_dequote(const char ** sp,const char ** errstrp)262319261079SEd Maste opt_dequote(const char **sp, const char **errstrp)
262419261079SEd Maste {
262519261079SEd Maste const char *s = *sp;
262619261079SEd Maste char *ret;
262719261079SEd Maste size_t i;
262819261079SEd Maste
262919261079SEd Maste *errstrp = NULL;
263019261079SEd Maste if (*s != '"') {
263119261079SEd Maste *errstrp = "missing start quote";
263219261079SEd Maste return NULL;
263319261079SEd Maste }
263419261079SEd Maste s++;
263519261079SEd Maste if ((ret = malloc(strlen((s)) + 1)) == NULL) {
263619261079SEd Maste *errstrp = "memory allocation failed";
263719261079SEd Maste return NULL;
263819261079SEd Maste }
263919261079SEd Maste for (i = 0; *s != '\0' && *s != '"';) {
264019261079SEd Maste if (s[0] == '\\' && s[1] == '"')
264119261079SEd Maste s++;
264219261079SEd Maste ret[i++] = *s++;
264319261079SEd Maste }
264419261079SEd Maste if (*s == '\0') {
264519261079SEd Maste *errstrp = "missing end quote";
264619261079SEd Maste free(ret);
264719261079SEd Maste return NULL;
264819261079SEd Maste }
264919261079SEd Maste ret[i] = '\0';
265019261079SEd Maste s++;
265119261079SEd Maste *sp = s;
265219261079SEd Maste return ret;
265319261079SEd Maste }
265419261079SEd Maste
265519261079SEd Maste int
opt_match(const char ** opts,const char * term)265619261079SEd Maste opt_match(const char **opts, const char *term)
265719261079SEd Maste {
265819261079SEd Maste if (strncasecmp((*opts), term, strlen(term)) == 0 &&
265919261079SEd Maste (*opts)[strlen(term)] == '=') {
266019261079SEd Maste *opts += strlen(term) + 1;
266119261079SEd Maste return 1;
266219261079SEd Maste }
266319261079SEd Maste return 0;
266419261079SEd Maste }
266519261079SEd Maste
266619261079SEd Maste void
opt_array_append2(const char * file,const int line,const char * directive,char *** array,int ** iarray,u_int * lp,const char * s,int i)266719261079SEd Maste opt_array_append2(const char *file, const int line, const char *directive,
266819261079SEd Maste char ***array, int **iarray, u_int *lp, const char *s, int i)
266919261079SEd Maste {
267019261079SEd Maste
267119261079SEd Maste if (*lp >= INT_MAX)
267219261079SEd Maste fatal("%s line %d: Too many %s entries", file, line, directive);
267319261079SEd Maste
267419261079SEd Maste if (iarray != NULL) {
267519261079SEd Maste *iarray = xrecallocarray(*iarray, *lp, *lp + 1,
267619261079SEd Maste sizeof(**iarray));
267719261079SEd Maste (*iarray)[*lp] = i;
267819261079SEd Maste }
267919261079SEd Maste
268019261079SEd Maste *array = xrecallocarray(*array, *lp, *lp + 1, sizeof(**array));
268119261079SEd Maste (*array)[*lp] = xstrdup(s);
268219261079SEd Maste (*lp)++;
268319261079SEd Maste }
268419261079SEd Maste
268519261079SEd Maste void
opt_array_append(const char * file,const int line,const char * directive,char *** array,u_int * lp,const char * s)268619261079SEd Maste opt_array_append(const char *file, const int line, const char *directive,
268719261079SEd Maste char ***array, u_int *lp, const char *s)
268819261079SEd Maste {
268919261079SEd Maste opt_array_append2(file, line, directive, array, NULL, lp, s, 0);
269019261079SEd Maste }
269119261079SEd Maste
2692a91a2465SEd Maste void
opt_array_free2(char ** array,int ** iarray,u_int l)2693a91a2465SEd Maste opt_array_free2(char **array, int **iarray, u_int l)
2694a91a2465SEd Maste {
2695a91a2465SEd Maste u_int i;
2696a91a2465SEd Maste
2697a91a2465SEd Maste if (array == NULL || l == 0)
2698a91a2465SEd Maste return;
2699a91a2465SEd Maste for (i = 0; i < l; i++)
2700a91a2465SEd Maste free(array[i]);
2701a91a2465SEd Maste free(array);
2702a91a2465SEd Maste free(iarray);
2703a91a2465SEd Maste }
2704a91a2465SEd Maste
270519261079SEd Maste sshsig_t
ssh_signal(int signum,sshsig_t handler)270619261079SEd Maste ssh_signal(int signum, sshsig_t handler)
270719261079SEd Maste {
270819261079SEd Maste struct sigaction sa, osa;
270919261079SEd Maste
271019261079SEd Maste /* mask all other signals while in handler */
271119261079SEd Maste memset(&sa, 0, sizeof(sa));
271219261079SEd Maste sa.sa_handler = handler;
271319261079SEd Maste sigfillset(&sa.sa_mask);
271419261079SEd Maste #if defined(SA_RESTART) && !defined(NO_SA_RESTART)
271519261079SEd Maste if (signum != SIGALRM)
271619261079SEd Maste sa.sa_flags = SA_RESTART;
271719261079SEd Maste #endif
271819261079SEd Maste if (sigaction(signum, &sa, &osa) == -1) {
271919261079SEd Maste debug3("sigaction(%s): %s", strsignal(signum), strerror(errno));
272019261079SEd Maste return SIG_ERR;
272119261079SEd Maste }
272219261079SEd Maste return osa.sa_handler;
272319261079SEd Maste }
272419261079SEd Maste
272519261079SEd Maste int
stdfd_devnull(int do_stdin,int do_stdout,int do_stderr)272619261079SEd Maste stdfd_devnull(int do_stdin, int do_stdout, int do_stderr)
272719261079SEd Maste {
272819261079SEd Maste int devnull, ret = 0;
272919261079SEd Maste
273019261079SEd Maste if ((devnull = open(_PATH_DEVNULL, O_RDWR)) == -1) {
273119261079SEd Maste error_f("open %s: %s", _PATH_DEVNULL,
273219261079SEd Maste strerror(errno));
273319261079SEd Maste return -1;
273419261079SEd Maste }
273519261079SEd Maste if ((do_stdin && dup2(devnull, STDIN_FILENO) == -1) ||
273619261079SEd Maste (do_stdout && dup2(devnull, STDOUT_FILENO) == -1) ||
273719261079SEd Maste (do_stderr && dup2(devnull, STDERR_FILENO) == -1)) {
273819261079SEd Maste error_f("dup2: %s", strerror(errno));
273919261079SEd Maste ret = -1;
274019261079SEd Maste }
274119261079SEd Maste if (devnull > STDERR_FILENO)
274219261079SEd Maste close(devnull);
274319261079SEd Maste return ret;
274419261079SEd Maste }
274519261079SEd Maste
274619261079SEd Maste /*
274719261079SEd Maste * Runs command in a subprocess with a minimal environment.
274819261079SEd Maste * Returns pid on success, 0 on failure.
274919261079SEd Maste * The child stdout and stderr maybe captured, left attached or sent to
275019261079SEd Maste * /dev/null depending on the contents of flags.
275119261079SEd Maste * "tag" is prepended to log messages.
275219261079SEd Maste * NB. "command" is only used for logging; the actual command executed is
275319261079SEd Maste * av[0].
275419261079SEd Maste */
275519261079SEd Maste pid_t
subprocess(const char * tag,const char * command,int ac,char ** av,FILE ** child,u_int flags,struct passwd * pw,privdrop_fn * drop_privs,privrestore_fn * restore_privs)275619261079SEd Maste subprocess(const char *tag, const char *command,
275719261079SEd Maste int ac, char **av, FILE **child, u_int flags,
275819261079SEd Maste struct passwd *pw, privdrop_fn *drop_privs, privrestore_fn *restore_privs)
275919261079SEd Maste {
276019261079SEd Maste FILE *f = NULL;
276119261079SEd Maste struct stat st;
276219261079SEd Maste int fd, devnull, p[2], i;
276319261079SEd Maste pid_t pid;
276419261079SEd Maste char *cp, errmsg[512];
276519261079SEd Maste u_int nenv = 0;
276619261079SEd Maste char **env = NULL;
276719261079SEd Maste
276819261079SEd Maste /* If dropping privs, then must specify user and restore function */
276919261079SEd Maste if (drop_privs != NULL && (pw == NULL || restore_privs == NULL)) {
277019261079SEd Maste error("%s: inconsistent arguments", tag); /* XXX fatal? */
277119261079SEd Maste return 0;
277219261079SEd Maste }
277319261079SEd Maste if (pw == NULL && (pw = getpwuid(getuid())) == NULL) {
277419261079SEd Maste error("%s: no user for current uid", tag);
277519261079SEd Maste return 0;
277619261079SEd Maste }
277719261079SEd Maste if (child != NULL)
277819261079SEd Maste *child = NULL;
277919261079SEd Maste
278019261079SEd Maste debug3_f("%s command \"%s\" running as %s (flags 0x%x)",
278119261079SEd Maste tag, command, pw->pw_name, flags);
278219261079SEd Maste
278319261079SEd Maste /* Check consistency */
278419261079SEd Maste if ((flags & SSH_SUBPROCESS_STDOUT_DISCARD) != 0 &&
278519261079SEd Maste (flags & SSH_SUBPROCESS_STDOUT_CAPTURE) != 0) {
278619261079SEd Maste error_f("inconsistent flags");
278719261079SEd Maste return 0;
278819261079SEd Maste }
278919261079SEd Maste if (((flags & SSH_SUBPROCESS_STDOUT_CAPTURE) == 0) != (child == NULL)) {
279019261079SEd Maste error_f("inconsistent flags/output");
279119261079SEd Maste return 0;
279219261079SEd Maste }
279319261079SEd Maste
279419261079SEd Maste /*
279519261079SEd Maste * If executing an explicit binary, then verify the it exists
279619261079SEd Maste * and appears safe-ish to execute
279719261079SEd Maste */
279819261079SEd Maste if (!path_absolute(av[0])) {
279919261079SEd Maste error("%s path is not absolute", tag);
280019261079SEd Maste return 0;
280119261079SEd Maste }
280219261079SEd Maste if (drop_privs != NULL)
280319261079SEd Maste drop_privs(pw);
280419261079SEd Maste if (stat(av[0], &st) == -1) {
280519261079SEd Maste error("Could not stat %s \"%s\": %s", tag,
280619261079SEd Maste av[0], strerror(errno));
280719261079SEd Maste goto restore_return;
280819261079SEd Maste }
280919261079SEd Maste if ((flags & SSH_SUBPROCESS_UNSAFE_PATH) == 0 &&
281019261079SEd Maste safe_path(av[0], &st, NULL, 0, errmsg, sizeof(errmsg)) != 0) {
281119261079SEd Maste error("Unsafe %s \"%s\": %s", tag, av[0], errmsg);
281219261079SEd Maste goto restore_return;
281319261079SEd Maste }
281419261079SEd Maste /* Prepare to keep the child's stdout if requested */
281519261079SEd Maste if (pipe(p) == -1) {
281619261079SEd Maste error("%s: pipe: %s", tag, strerror(errno));
281719261079SEd Maste restore_return:
281819261079SEd Maste if (restore_privs != NULL)
281919261079SEd Maste restore_privs();
282019261079SEd Maste return 0;
282119261079SEd Maste }
282219261079SEd Maste if (restore_privs != NULL)
282319261079SEd Maste restore_privs();
282419261079SEd Maste
282519261079SEd Maste switch ((pid = fork())) {
282619261079SEd Maste case -1: /* error */
282719261079SEd Maste error("%s: fork: %s", tag, strerror(errno));
282819261079SEd Maste close(p[0]);
282919261079SEd Maste close(p[1]);
283019261079SEd Maste return 0;
283119261079SEd Maste case 0: /* child */
283219261079SEd Maste /* Prepare a minimal environment for the child. */
283319261079SEd Maste if ((flags & SSH_SUBPROCESS_PRESERVE_ENV) == 0) {
283419261079SEd Maste nenv = 5;
283519261079SEd Maste env = xcalloc(sizeof(*env), nenv);
283619261079SEd Maste child_set_env(&env, &nenv, "PATH", _PATH_STDPATH);
283719261079SEd Maste child_set_env(&env, &nenv, "USER", pw->pw_name);
283819261079SEd Maste child_set_env(&env, &nenv, "LOGNAME", pw->pw_name);
283919261079SEd Maste child_set_env(&env, &nenv, "HOME", pw->pw_dir);
284019261079SEd Maste if ((cp = getenv("LANG")) != NULL)
284119261079SEd Maste child_set_env(&env, &nenv, "LANG", cp);
284219261079SEd Maste }
284319261079SEd Maste
284419261079SEd Maste for (i = 1; i < NSIG; i++)
284519261079SEd Maste ssh_signal(i, SIG_DFL);
284619261079SEd Maste
284719261079SEd Maste if ((devnull = open(_PATH_DEVNULL, O_RDWR)) == -1) {
284819261079SEd Maste error("%s: open %s: %s", tag, _PATH_DEVNULL,
284919261079SEd Maste strerror(errno));
285019261079SEd Maste _exit(1);
285119261079SEd Maste }
285219261079SEd Maste if (dup2(devnull, STDIN_FILENO) == -1) {
285319261079SEd Maste error("%s: dup2: %s", tag, strerror(errno));
285419261079SEd Maste _exit(1);
285519261079SEd Maste }
285619261079SEd Maste
285719261079SEd Maste /* Set up stdout as requested; leave stderr in place for now. */
285819261079SEd Maste fd = -1;
285919261079SEd Maste if ((flags & SSH_SUBPROCESS_STDOUT_CAPTURE) != 0)
286019261079SEd Maste fd = p[1];
286119261079SEd Maste else if ((flags & SSH_SUBPROCESS_STDOUT_DISCARD) != 0)
286219261079SEd Maste fd = devnull;
286319261079SEd Maste if (fd != -1 && dup2(fd, STDOUT_FILENO) == -1) {
286419261079SEd Maste error("%s: dup2: %s", tag, strerror(errno));
286519261079SEd Maste _exit(1);
286619261079SEd Maste }
286719261079SEd Maste closefrom(STDERR_FILENO + 1);
286819261079SEd Maste
2869172fa4aaSEd Maste if (geteuid() == 0 &&
2870172fa4aaSEd Maste initgroups(pw->pw_name, pw->pw_gid) == -1) {
2871172fa4aaSEd Maste error("%s: initgroups(%s, %u): %s", tag,
2872172fa4aaSEd Maste pw->pw_name, (u_int)pw->pw_gid, strerror(errno));
2873172fa4aaSEd Maste _exit(1);
2874172fa4aaSEd Maste }
287519261079SEd Maste if (setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) == -1) {
287619261079SEd Maste error("%s: setresgid %u: %s", tag, (u_int)pw->pw_gid,
287719261079SEd Maste strerror(errno));
287819261079SEd Maste _exit(1);
287919261079SEd Maste }
288019261079SEd Maste if (setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid) == -1) {
288119261079SEd Maste error("%s: setresuid %u: %s", tag, (u_int)pw->pw_uid,
288219261079SEd Maste strerror(errno));
288319261079SEd Maste _exit(1);
288419261079SEd Maste }
288519261079SEd Maste /* stdin is pointed to /dev/null at this point */
288619261079SEd Maste if ((flags & SSH_SUBPROCESS_STDOUT_DISCARD) != 0 &&
288719261079SEd Maste dup2(STDIN_FILENO, STDERR_FILENO) == -1) {
288819261079SEd Maste error("%s: dup2: %s", tag, strerror(errno));
288919261079SEd Maste _exit(1);
289019261079SEd Maste }
289119261079SEd Maste if (env != NULL)
289219261079SEd Maste execve(av[0], av, env);
289319261079SEd Maste else
289419261079SEd Maste execv(av[0], av);
289519261079SEd Maste error("%s %s \"%s\": %s", tag, env == NULL ? "execv" : "execve",
289619261079SEd Maste command, strerror(errno));
289719261079SEd Maste _exit(127);
289819261079SEd Maste default: /* parent */
289919261079SEd Maste break;
290019261079SEd Maste }
290119261079SEd Maste
290219261079SEd Maste close(p[1]);
290319261079SEd Maste if ((flags & SSH_SUBPROCESS_STDOUT_CAPTURE) == 0)
290419261079SEd Maste close(p[0]);
290519261079SEd Maste else if ((f = fdopen(p[0], "r")) == NULL) {
290619261079SEd Maste error("%s: fdopen: %s", tag, strerror(errno));
290719261079SEd Maste close(p[0]);
290819261079SEd Maste /* Don't leave zombie child */
290919261079SEd Maste kill(pid, SIGTERM);
291019261079SEd Maste while (waitpid(pid, NULL, 0) == -1 && errno == EINTR)
291119261079SEd Maste ;
291219261079SEd Maste return 0;
291319261079SEd Maste }
291419261079SEd Maste /* Success */
291519261079SEd Maste debug3_f("%s pid %ld", tag, (long)pid);
291619261079SEd Maste if (child != NULL)
291719261079SEd Maste *child = f;
291819261079SEd Maste return pid;
291919261079SEd Maste }
292019261079SEd Maste
292119261079SEd Maste const char *
lookup_env_in_list(const char * env,char * const * envs,size_t nenvs)292219261079SEd Maste lookup_env_in_list(const char *env, char * const *envs, size_t nenvs)
292319261079SEd Maste {
292419261079SEd Maste size_t i, envlen;
292519261079SEd Maste
292619261079SEd Maste envlen = strlen(env);
292719261079SEd Maste for (i = 0; i < nenvs; i++) {
292819261079SEd Maste if (strncmp(envs[i], env, envlen) == 0 &&
292919261079SEd Maste envs[i][envlen] == '=') {
293019261079SEd Maste return envs[i] + envlen + 1;
293119261079SEd Maste }
293219261079SEd Maste }
293319261079SEd Maste return NULL;
293419261079SEd Maste }
293538a52bd3SEd Maste
293638a52bd3SEd Maste const char *
lookup_setenv_in_list(const char * env,char * const * envs,size_t nenvs)293738a52bd3SEd Maste lookup_setenv_in_list(const char *env, char * const *envs, size_t nenvs)
293838a52bd3SEd Maste {
293938a52bd3SEd Maste char *name, *cp;
294038a52bd3SEd Maste const char *ret;
294138a52bd3SEd Maste
294238a52bd3SEd Maste name = xstrdup(env);
294338a52bd3SEd Maste if ((cp = strchr(name, '=')) == NULL) {
294438a52bd3SEd Maste free(name);
294538a52bd3SEd Maste return NULL; /* not env=val */
294638a52bd3SEd Maste }
294738a52bd3SEd Maste *cp = '\0';
294838a52bd3SEd Maste ret = lookup_env_in_list(name, envs, nenvs);
294938a52bd3SEd Maste free(name);
295038a52bd3SEd Maste return ret;
295138a52bd3SEd Maste }
2952f374ba41SEd Maste
2953f374ba41SEd Maste /*
2954f374ba41SEd Maste * Helpers for managing poll(2)/ppoll(2) timeouts
2955f374ba41SEd Maste * Will remember the earliest deadline and return it for use in poll/ppoll.
2956f374ba41SEd Maste */
2957f374ba41SEd Maste
2958f374ba41SEd Maste /* Initialise a poll/ppoll timeout with an indefinite deadline */
2959f374ba41SEd Maste void
ptimeout_init(struct timespec * pt)2960f374ba41SEd Maste ptimeout_init(struct timespec *pt)
2961f374ba41SEd Maste {
2962f374ba41SEd Maste /*
2963f374ba41SEd Maste * Deliberately invalid for ppoll(2).
2964f374ba41SEd Maste * Will be converted to NULL in ptimeout_get_tspec() later.
2965f374ba41SEd Maste */
2966f374ba41SEd Maste pt->tv_sec = -1;
2967f374ba41SEd Maste pt->tv_nsec = 0;
2968f374ba41SEd Maste }
2969f374ba41SEd Maste
2970f374ba41SEd Maste /* Specify a poll/ppoll deadline of at most 'sec' seconds */
2971f374ba41SEd Maste void
ptimeout_deadline_sec(struct timespec * pt,long sec)2972f374ba41SEd Maste ptimeout_deadline_sec(struct timespec *pt, long sec)
2973f374ba41SEd Maste {
2974f374ba41SEd Maste if (pt->tv_sec == -1 || pt->tv_sec >= sec) {
2975f374ba41SEd Maste pt->tv_sec = sec;
2976f374ba41SEd Maste pt->tv_nsec = 0;
2977f374ba41SEd Maste }
2978f374ba41SEd Maste }
2979f374ba41SEd Maste
2980f374ba41SEd Maste /* Specify a poll/ppoll deadline of at most 'p' (timespec) */
2981f374ba41SEd Maste static void
ptimeout_deadline_tsp(struct timespec * pt,struct timespec * p)2982f374ba41SEd Maste ptimeout_deadline_tsp(struct timespec *pt, struct timespec *p)
2983f374ba41SEd Maste {
2984f374ba41SEd Maste if (pt->tv_sec == -1 || timespeccmp(pt, p, >=))
2985f374ba41SEd Maste *pt = *p;
2986f374ba41SEd Maste }
2987f374ba41SEd Maste
2988f374ba41SEd Maste /* Specify a poll/ppoll deadline of at most 'ms' milliseconds */
2989f374ba41SEd Maste void
ptimeout_deadline_ms(struct timespec * pt,long ms)2990f374ba41SEd Maste ptimeout_deadline_ms(struct timespec *pt, long ms)
2991f374ba41SEd Maste {
2992f374ba41SEd Maste struct timespec p;
2993f374ba41SEd Maste
2994f374ba41SEd Maste p.tv_sec = ms / 1000;
2995f374ba41SEd Maste p.tv_nsec = (ms % 1000) * 1000000;
2996f374ba41SEd Maste ptimeout_deadline_tsp(pt, &p);
2997f374ba41SEd Maste }
2998f374ba41SEd Maste
2999edf85781SEd Maste /* Specify a poll/ppoll deadline at wall clock monotime 'when' (timespec) */
3000edf85781SEd Maste void
ptimeout_deadline_monotime_tsp(struct timespec * pt,struct timespec * when)3001edf85781SEd Maste ptimeout_deadline_monotime_tsp(struct timespec *pt, struct timespec *when)
3002edf85781SEd Maste {
3003edf85781SEd Maste struct timespec now, t;
3004edf85781SEd Maste
3005edf85781SEd Maste monotime_ts(&now);
3006edf85781SEd Maste
3007edf85781SEd Maste if (timespeccmp(&now, when, >=)) {
3008edf85781SEd Maste /* 'when' is now or in the past. Timeout ASAP */
3009edf85781SEd Maste pt->tv_sec = 0;
3010edf85781SEd Maste pt->tv_nsec = 0;
3011edf85781SEd Maste } else {
3012edf85781SEd Maste timespecsub(when, &now, &t);
3013edf85781SEd Maste ptimeout_deadline_tsp(pt, &t);
3014edf85781SEd Maste }
3015edf85781SEd Maste }
3016edf85781SEd Maste
3017f374ba41SEd Maste /* Specify a poll/ppoll deadline at wall clock monotime 'when' */
3018f374ba41SEd Maste void
ptimeout_deadline_monotime(struct timespec * pt,time_t when)3019f374ba41SEd Maste ptimeout_deadline_monotime(struct timespec *pt, time_t when)
3020f374ba41SEd Maste {
3021edf85781SEd Maste struct timespec t;
3022f374ba41SEd Maste
3023f374ba41SEd Maste t.tv_sec = when;
3024f374ba41SEd Maste t.tv_nsec = 0;
3025edf85781SEd Maste ptimeout_deadline_monotime_tsp(pt, &t);
3026f374ba41SEd Maste }
3027f374ba41SEd Maste
3028f374ba41SEd Maste /* Get a poll(2) timeout value in milliseconds */
3029f374ba41SEd Maste int
ptimeout_get_ms(struct timespec * pt)3030f374ba41SEd Maste ptimeout_get_ms(struct timespec *pt)
3031f374ba41SEd Maste {
3032f374ba41SEd Maste if (pt->tv_sec == -1)
3033f374ba41SEd Maste return -1;
3034f374ba41SEd Maste if (pt->tv_sec >= (INT_MAX - (pt->tv_nsec / 1000000)) / 1000)
3035f374ba41SEd Maste return INT_MAX;
3036f374ba41SEd Maste return (pt->tv_sec * 1000) + (pt->tv_nsec / 1000000);
3037f374ba41SEd Maste }
3038f374ba41SEd Maste
3039f374ba41SEd Maste /* Get a ppoll(2) timeout value as a timespec pointer */
3040f374ba41SEd Maste struct timespec *
ptimeout_get_tsp(struct timespec * pt)3041f374ba41SEd Maste ptimeout_get_tsp(struct timespec *pt)
3042f374ba41SEd Maste {
3043f374ba41SEd Maste return pt->tv_sec == -1 ? NULL : pt;
3044f374ba41SEd Maste }
3045f374ba41SEd Maste
3046f374ba41SEd Maste /* Returns non-zero if a timeout has been set (i.e. is not indefinite) */
3047f374ba41SEd Maste int
ptimeout_isset(struct timespec * pt)3048f374ba41SEd Maste ptimeout_isset(struct timespec *pt)
3049f374ba41SEd Maste {
3050f374ba41SEd Maste return pt->tv_sec != -1;
3051f374ba41SEd Maste }
3052535af610SEd Maste
3053535af610SEd Maste /*
3054535af610SEd Maste * Returns zero if the library at 'path' contains symbol 's', nonzero
3055535af610SEd Maste * otherwise.
3056535af610SEd Maste */
3057535af610SEd Maste int
lib_contains_symbol(const char * path,const char * s)3058535af610SEd Maste lib_contains_symbol(const char *path, const char *s)
3059535af610SEd Maste {
3060535af610SEd Maste #ifdef HAVE_NLIST_H
3061535af610SEd Maste struct nlist nl[2];
3062535af610SEd Maste int ret = -1, r;
3063535af610SEd Maste
3064535af610SEd Maste memset(nl, 0, sizeof(nl));
3065535af610SEd Maste nl[0].n_name = xstrdup(s);
3066535af610SEd Maste nl[1].n_name = NULL;
3067535af610SEd Maste if ((r = nlist(path, nl)) == -1) {
3068535af610SEd Maste error_f("nlist failed for %s", path);
3069535af610SEd Maste goto out;
3070535af610SEd Maste }
3071535af610SEd Maste if (r != 0 || nl[0].n_value == 0 || nl[0].n_type == 0) {
3072535af610SEd Maste error_f("library %s does not contain symbol %s", path, s);
3073535af610SEd Maste goto out;
3074535af610SEd Maste }
3075535af610SEd Maste /* success */
3076535af610SEd Maste ret = 0;
3077535af610SEd Maste out:
3078535af610SEd Maste free(nl[0].n_name);
3079535af610SEd Maste return ret;
3080535af610SEd Maste #else /* HAVE_NLIST_H */
3081535af610SEd Maste int fd, ret = -1;
3082535af610SEd Maste struct stat st;
3083535af610SEd Maste void *m = NULL;
3084535af610SEd Maste size_t sz = 0;
3085535af610SEd Maste
3086535af610SEd Maste memset(&st, 0, sizeof(st));
3087535af610SEd Maste if ((fd = open(path, O_RDONLY)) < 0) {
3088535af610SEd Maste error_f("open %s: %s", path, strerror(errno));
3089535af610SEd Maste return -1;
3090535af610SEd Maste }
3091535af610SEd Maste if (fstat(fd, &st) != 0) {
3092535af610SEd Maste error_f("fstat %s: %s", path, strerror(errno));
3093535af610SEd Maste goto out;
3094535af610SEd Maste }
3095535af610SEd Maste if (!S_ISREG(st.st_mode)) {
3096535af610SEd Maste error_f("%s is not a regular file", path);
3097535af610SEd Maste goto out;
3098535af610SEd Maste }
3099535af610SEd Maste if (st.st_size < 0 ||
3100535af610SEd Maste (size_t)st.st_size < strlen(s) ||
3101535af610SEd Maste st.st_size >= INT_MAX/2) {
3102535af610SEd Maste error_f("%s bad size %lld", path, (long long)st.st_size);
3103535af610SEd Maste goto out;
3104535af610SEd Maste }
3105535af610SEd Maste sz = (size_t)st.st_size;
3106535af610SEd Maste if ((m = mmap(NULL, sz, PROT_READ, MAP_PRIVATE, fd, 0)) == MAP_FAILED ||
3107535af610SEd Maste m == NULL) {
3108535af610SEd Maste error_f("mmap %s: %s", path, strerror(errno));
3109535af610SEd Maste goto out;
3110535af610SEd Maste }
3111535af610SEd Maste if (memmem(m, sz, s, strlen(s)) == NULL) {
3112535af610SEd Maste error_f("%s does not contain expected string %s", path, s);
3113535af610SEd Maste goto out;
3114535af610SEd Maste }
3115535af610SEd Maste /* success */
3116535af610SEd Maste ret = 0;
3117535af610SEd Maste out:
3118535af610SEd Maste if (m != NULL && m != MAP_FAILED)
3119535af610SEd Maste munmap(m, sz);
3120535af610SEd Maste close(fd);
3121535af610SEd Maste return ret;
3122535af610SEd Maste #endif /* HAVE_NLIST_H */
3123535af610SEd Maste }
31240fdf8faeSEd Maste
31250fdf8faeSEd Maste int
signal_is_crash(int sig)31260fdf8faeSEd Maste signal_is_crash(int sig)
31270fdf8faeSEd Maste {
31280fdf8faeSEd Maste switch (sig) {
31290fdf8faeSEd Maste case SIGSEGV:
31300fdf8faeSEd Maste case SIGBUS:
31310fdf8faeSEd Maste case SIGTRAP:
31320fdf8faeSEd Maste case SIGSYS:
31330fdf8faeSEd Maste case SIGFPE:
31340fdf8faeSEd Maste case SIGILL:
31350fdf8faeSEd Maste case SIGABRT:
31360fdf8faeSEd Maste return 1;
31370fdf8faeSEd Maste }
31380fdf8faeSEd Maste return 0;
31390fdf8faeSEd Maste }
3140