From acfd13d60a953aecfad877118eca596b139239cf Mon Sep 17 00:00:00 2001 From: millert Date: Mon, 18 Dec 2017 17:28:54 +0000 Subject: [PATCH] Add helper function for uri handing in scp where a missing path simply means ".". Also fix exit code and add warnings when an invalid uri is encountered. OK otto@ --- usr.bin/ssh/scp.c | 47 ++++++++++++++++++++++++++++++++++++----------- 1 file changed, 36 insertions(+), 11 deletions(-) diff --git a/usr.bin/ssh/scp.c b/usr.bin/ssh/scp.c index e8b62cab982..5bc49723022 100644 --- a/usr.bin/ssh/scp.c +++ b/usr.bin/ssh/scp.c @@ -1,4 +1,4 @@ -/* $OpenBSD: scp.c,v 1.193 2017/10/21 23:06:24 millert Exp $ */ +/* $OpenBSD: scp.c,v 1.194 2017/12/18 17:28:54 millert Exp $ */ /* * scp - secure remote copy. This is basically patched BSD rcp which * uses ssh to do the data transfer (instead of using rcmd). @@ -583,6 +583,18 @@ do_times(int fd, int verb, const struct stat *sb) return (response()); } +static int +parse_scp_uri(const char *uri, char **userp, char **hostp, int *portp, + char **pathp) +{ + int r; + + r = parse_uri("scp", uri, userp, hostp, portp, pathp); + if (r == 0 && *pathp == NULL) + *pathp = xstrdup("."); + return r; +} + void toremote(int argc, char **argv) { @@ -597,27 +609,39 @@ toremote(int argc, char **argv) alist.list = NULL; /* Parse target */ - r = parse_uri("scp", argv[argc - 1], &tuser, &thost, &tport, &targ); - if (r == -1) - goto out; /* invalid URI */ + r = parse_scp_uri(argv[argc - 1], &tuser, &thost, &tport, &targ); + if (r == -1) { + fmprintf(stderr, "%s: invalid uri\n", argv[argc - 1]); + ++errs; + goto out; + } if (r != 0) { if (parse_user_host_path(argv[argc - 1], &tuser, &thost, - &targ) == -1) + &targ) == -1) { + fmprintf(stderr, "%s: invalid target\n", argv[argc - 1]); + ++errs; goto out; + } } - if (tuser != NULL && !okname(tuser)) + if (tuser != NULL && !okname(tuser)) { + ++errs; goto out; + } /* Parse source files */ for (i = 0; i < argc - 1; i++) { free(suser); free(host); free(src); - r = parse_uri("scp", argv[i], &suser, &host, &sport, &src); - if (r == -1) - continue; /* invalid URI */ - if (r != 0) + r = parse_scp_uri(argv[i], &suser, &host, &sport, &src); + if (r == -1) { + fmprintf(stderr, "%s: invalid uri\n", argv[i]); + ++errs; + continue; + } + if (r != 0) { parse_user_host_path(argv[i], &suser, &host, &src); + } if (suser != NULL && !okname(suser)) { ++errs; continue; @@ -707,8 +731,9 @@ tolocal(int argc, char **argv) free(suser); free(host); free(src); - r = parse_uri("scp", argv[i], &suser, &host, &sport, &src); + r = parse_scp_uri(argv[i], &suser, &host, &sport, &src); if (r == -1) { + fmprintf(stderr, "%s: invalid uri\n", argv[i]); ++errs; continue; } -- 2.20.1