1 /* $OpenBSD: sftp-glob.c,v 1.31 2022/10/24 21:51:55 djm Exp $ */ 2 /* 3 * Copyright (c) 2001-2004 Damien Miller <djm@openbsd.org> 4 * 5 * Permission to use, copy, modify, and distribute this software for any 6 * purpose with or without fee is hereby granted, provided that the above 7 * copyright notice and this permission notice appear in all copies. 8 * 9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 */ 17 18 #include <sys/types.h> 19 #include <sys/stat.h> 20 21 #include <dirent.h> 22 #include <glob.h> 23 #include <stdlib.h> 24 #include <string.h> 25 #include <stdarg.h> 26 27 #include "xmalloc.h" 28 #include "sftp.h" 29 #include "sftp-common.h" 30 #include "sftp-client.h" 31 32 int remote_glob(struct sftp_conn *, const char *, int, 33 int (*)(const char *, int), glob_t *); 34 35 struct SFTP_OPENDIR { 36 SFTP_DIRENT **dir; 37 int offset; 38 }; 39 40 static struct { 41 struct sftp_conn *conn; 42 } cur; 43 44 static void * 45 fudge_opendir(const char *path) 46 { 47 struct SFTP_OPENDIR *r; 48 49 r = xcalloc(1, sizeof(*r)); 50 51 if (do_readdir(cur.conn, path, &r->dir)) { 52 free(r); 53 return(NULL); 54 } 55 56 r->offset = 0; 57 58 return((void *)r); 59 } 60 61 static struct dirent * 62 fudge_readdir(struct SFTP_OPENDIR *od) 63 { 64 static struct dirent ret; 65 66 if (od->dir[od->offset] == NULL) 67 return(NULL); 68 69 memset(&ret, 0, sizeof(ret)); 70 strlcpy(ret.d_name, od->dir[od->offset++]->filename, 71 sizeof(ret.d_name)); 72 73 return(&ret); 74 } 75 76 static void 77 fudge_closedir(struct SFTP_OPENDIR *od) 78 { 79 free_sftp_dirents(od->dir); 80 free(od); 81 } 82 83 static int 84 fudge_lstat(const char *path, struct stat *st) 85 { 86 Attrib *a; 87 88 if (!(a = do_lstat(cur.conn, path, 1))) 89 return(-1); 90 91 attrib_to_stat(a, st); 92 93 return(0); 94 } 95 96 static int 97 fudge_stat(const char *path, struct stat *st) 98 { 99 Attrib *a; 100 101 if (!(a = do_stat(cur.conn, path, 1))) 102 return(-1); 103 104 attrib_to_stat(a, st); 105 106 return(0); 107 } 108 109 int 110 remote_glob(struct sftp_conn *conn, const char *pattern, int flags, 111 int (*errfunc)(const char *, int), glob_t *pglob) 112 { 113 int r; 114 size_t l; 115 char *s; 116 struct stat sb; 117 118 pglob->gl_opendir = fudge_opendir; 119 pglob->gl_readdir = (struct dirent *(*)(void *))fudge_readdir; 120 pglob->gl_closedir = (void (*)(void *))fudge_closedir; 121 pglob->gl_lstat = fudge_lstat; 122 pglob->gl_stat = fudge_stat; 123 124 memset(&cur, 0, sizeof(cur)); 125 cur.conn = conn; 126 127 if ((r = glob(pattern, flags | GLOB_ALTDIRFUNC, errfunc, pglob)) != 0) 128 return r; 129 /* 130 * When both GLOB_NOCHECK and GLOB_MARK are active, a single gl_pathv 131 * entry has been returned and that entry has not already been marked, 132 * then check whether it needs a '/' appended as a directory mark. 133 * 134 * This ensures that a NOCHECK result is annotated as a directory. 135 * The glob(3) spec doesn't promise to mark NOCHECK entries, but doing 136 * it simplifies our callers (sftp/scp) considerably. 137 * 138 * XXX doesn't try to handle gl_offs. 139 */ 140 if ((flags & (GLOB_NOCHECK|GLOB_MARK)) == (GLOB_NOCHECK|GLOB_MARK) && 141 pglob->gl_matchc == 0 && pglob->gl_offs == 0 && 142 pglob->gl_pathc == 1 && (s = pglob->gl_pathv[0]) != NULL && 143 (l = strlen(s)) > 0 && s[l-1] != '/') { 144 if (fudge_stat(s, &sb) == 0 && S_ISDIR(sb.st_mode)) { 145 /* NOCHECK on a directory; annotate */ 146 if ((s = realloc(s, l + 2)) != NULL) { 147 memcpy(s + l, "/", 2); 148 pglob->gl_pathv[0] = s; 149 } 150 } 151 } 152 return 0; 153 } 154