Damien Miller | 4870afd | 2001-03-14 10:27:09 +1100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2001 Damien Miller. All rights reserved. |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions |
| 6 | * are met: |
| 7 | * 1. Redistributions of source code must retain the above copyright |
| 8 | * notice, this list of conditions and the following disclaimer. |
| 9 | * 2. Redistributions in binary form must reproduce the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer in the |
| 11 | * documentation and/or other materials provided with the distribution. |
| 12 | * |
| 13 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 14 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 15 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 16 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 17 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 18 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 19 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 20 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 21 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 22 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 23 | */ |
| 24 | |
| 25 | #include "includes.h" |
Ben Lindstrom | ad77313 | 2001-07-18 15:45:44 +0000 | [diff] [blame^] | 26 | RCSID("$OpenBSD: sftp-glob.c,v 1.8 2001/07/14 15:10:17 stevesk Exp $"); |
Damien Miller | 4870afd | 2001-03-14 10:27:09 +1100 | [diff] [blame] | 27 | |
Damien Miller | 4870afd | 2001-03-14 10:27:09 +1100 | [diff] [blame] | 28 | #include "buffer.h" |
| 29 | #include "bufaux.h" |
Damien Miller | 4870afd | 2001-03-14 10:27:09 +1100 | [diff] [blame] | 30 | #include "xmalloc.h" |
| 31 | #include "log.h" |
Damien Miller | 4870afd | 2001-03-14 10:27:09 +1100 | [diff] [blame] | 32 | |
| 33 | #include "sftp.h" |
| 34 | #include "sftp-common.h" |
| 35 | #include "sftp-client.h" |
| 36 | #include "sftp-glob.h" |
| 37 | |
| 38 | struct SFTP_OPENDIR { |
| 39 | SFTP_DIRENT **dir; |
| 40 | int offset; |
| 41 | }; |
| 42 | |
| 43 | static struct { |
| 44 | int fd_in; |
| 45 | int fd_out; |
| 46 | } cur; |
| 47 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 48 | static void * |
| 49 | fudge_opendir(const char *path) |
Damien Miller | 4870afd | 2001-03-14 10:27:09 +1100 | [diff] [blame] | 50 | { |
| 51 | struct SFTP_OPENDIR *r; |
| 52 | |
| 53 | r = xmalloc(sizeof(*r)); |
| 54 | |
| 55 | if (do_readdir(cur.fd_in, cur.fd_out, (char*)path, &r->dir)) |
| 56 | return(NULL); |
| 57 | |
| 58 | r->offset = 0; |
| 59 | |
| 60 | return((void*)r); |
| 61 | } |
| 62 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 63 | static struct dirent * |
| 64 | fudge_readdir(struct SFTP_OPENDIR *od) |
Damien Miller | 4870afd | 2001-03-14 10:27:09 +1100 | [diff] [blame] | 65 | { |
Damien Miller | 18bb473 | 2001-03-28 14:35:30 +1000 | [diff] [blame] | 66 | /* Solaris needs sizeof(dirent) + path length (see below) */ |
| 67 | static char buf[sizeof(struct dirent) + MAXPATHLEN]; |
| 68 | struct dirent *ret = (struct dirent *)buf; |
Damien Miller | 4870afd | 2001-03-14 10:27:09 +1100 | [diff] [blame] | 69 | #ifdef __GNU_LIBRARY__ |
| 70 | static int inum = 1; |
| 71 | #endif /* __GNU_LIBRARY__ */ |
| 72 | |
| 73 | if (od->dir[od->offset] == NULL) |
| 74 | return(NULL); |
| 75 | |
Damien Miller | 18bb473 | 2001-03-28 14:35:30 +1000 | [diff] [blame] | 76 | memset(buf, 0, sizeof(buf)); |
Damien Miller | 4870afd | 2001-03-14 10:27:09 +1100 | [diff] [blame] | 77 | |
Damien Miller | 18bb473 | 2001-03-28 14:35:30 +1000 | [diff] [blame] | 78 | /* |
| 79 | * Solaris defines dirent->d_name as a one byte array and expects |
| 80 | * you to hack around it. |
| 81 | */ |
| 82 | #ifdef BROKEN_ONE_BYTE_DIRENT_D_NAME |
| 83 | strlcpy(ret->d_name, od->dir[od->offset++]->filename, MAXPATHLEN); |
| 84 | #else |
Ben Lindstrom | a370005 | 2001-04-05 23:26:32 +0000 | [diff] [blame] | 85 | strlcpy(ret->d_name, od->dir[od->offset++]->filename, |
Damien Miller | 18bb473 | 2001-03-28 14:35:30 +1000 | [diff] [blame] | 86 | sizeof(ret->d_name)); |
| 87 | #endif |
Damien Miller | 4870afd | 2001-03-14 10:27:09 +1100 | [diff] [blame] | 88 | #ifdef __GNU_LIBRARY__ |
| 89 | /* |
| 90 | * Idiot glibc uses extensions to struct dirent for readdir with |
| 91 | * ALTDIRFUNCs. Not that this is documented anywhere but the |
| 92 | * source... Fake an inode number to appease it. |
| 93 | */ |
Damien Miller | 18bb473 | 2001-03-28 14:35:30 +1000 | [diff] [blame] | 94 | ret->d_ino = inum++; |
Damien Miller | 4870afd | 2001-03-14 10:27:09 +1100 | [diff] [blame] | 95 | if (!inum) |
| 96 | inum = 1; |
| 97 | #endif /* __GNU_LIBRARY__ */ |
| 98 | |
Damien Miller | 18bb473 | 2001-03-28 14:35:30 +1000 | [diff] [blame] | 99 | return(ret); |
Damien Miller | 4870afd | 2001-03-14 10:27:09 +1100 | [diff] [blame] | 100 | } |
| 101 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 102 | static void |
| 103 | fudge_closedir(struct SFTP_OPENDIR *od) |
Damien Miller | 4870afd | 2001-03-14 10:27:09 +1100 | [diff] [blame] | 104 | { |
| 105 | free_sftp_dirents(od->dir); |
Ben Lindstrom | 86ebcb6 | 2001-04-04 01:53:20 +0000 | [diff] [blame] | 106 | xfree(od); |
Damien Miller | 4870afd | 2001-03-14 10:27:09 +1100 | [diff] [blame] | 107 | } |
| 108 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 109 | static void |
| 110 | attrib_to_stat(Attrib *a, struct stat *st) |
Damien Miller | 4870afd | 2001-03-14 10:27:09 +1100 | [diff] [blame] | 111 | { |
| 112 | memset(st, 0, sizeof(*st)); |
| 113 | |
| 114 | if (a->flags & SSH2_FILEXFER_ATTR_SIZE) |
| 115 | st->st_size = a->size; |
| 116 | if (a->flags & SSH2_FILEXFER_ATTR_UIDGID) { |
| 117 | st->st_uid = a->uid; |
| 118 | st->st_gid = a->gid; |
| 119 | } |
| 120 | if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) |
| 121 | st->st_mode = a->perm; |
| 122 | if (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME) { |
| 123 | st->st_atime = a->atime; |
| 124 | st->st_mtime = a->mtime; |
| 125 | } |
| 126 | } |
| 127 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 128 | static int |
| 129 | fudge_lstat(const char *path, struct stat *st) |
Damien Miller | 4870afd | 2001-03-14 10:27:09 +1100 | [diff] [blame] | 130 | { |
| 131 | Attrib *a; |
| 132 | |
Ben Lindstrom | c8d1c30 | 2001-03-17 00:34:46 +0000 | [diff] [blame] | 133 | if (!(a = do_lstat(cur.fd_in, cur.fd_out, (char*)path, 0))) |
Damien Miller | 4870afd | 2001-03-14 10:27:09 +1100 | [diff] [blame] | 134 | return(-1); |
| 135 | |
| 136 | attrib_to_stat(a, st); |
| 137 | |
| 138 | return(0); |
| 139 | } |
| 140 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 141 | static int |
| 142 | fudge_stat(const char *path, struct stat *st) |
Damien Miller | 4870afd | 2001-03-14 10:27:09 +1100 | [diff] [blame] | 143 | { |
| 144 | Attrib *a; |
| 145 | |
Ben Lindstrom | c8d1c30 | 2001-03-17 00:34:46 +0000 | [diff] [blame] | 146 | if (!(a = do_stat(cur.fd_in, cur.fd_out, (char*)path, 0))) |
Damien Miller | 4870afd | 2001-03-14 10:27:09 +1100 | [diff] [blame] | 147 | return(-1); |
| 148 | |
| 149 | attrib_to_stat(a, st); |
| 150 | |
| 151 | return(0); |
| 152 | } |
| 153 | |
| 154 | int |
Ben Lindstrom | a370005 | 2001-04-05 23:26:32 +0000 | [diff] [blame] | 155 | remote_glob(int fd_in, int fd_out, const char *pattern, int flags, |
Ben Lindstrom | 206941f | 2001-04-15 14:27:16 +0000 | [diff] [blame] | 156 | int (*errfunc)(const char *, int), glob_t *pglob) |
Damien Miller | 4870afd | 2001-03-14 10:27:09 +1100 | [diff] [blame] | 157 | { |
Damien Miller | ea12acf | 2001-07-14 12:14:56 +1000 | [diff] [blame] | 158 | pglob->gl_opendir = fudge_opendir; |
| 159 | pglob->gl_readdir = (struct dirent *(*)(void *))fudge_readdir; |
| 160 | pglob->gl_closedir = (void (*)(void *))fudge_closedir; |
Damien Miller | 4870afd | 2001-03-14 10:27:09 +1100 | [diff] [blame] | 161 | pglob->gl_lstat = fudge_lstat; |
| 162 | pglob->gl_stat = fudge_stat; |
| 163 | |
| 164 | memset(&cur, 0, sizeof(cur)); |
| 165 | cur.fd_in = fd_in; |
| 166 | cur.fd_out = fd_out; |
| 167 | |
Damien Miller | ea12acf | 2001-07-14 12:14:56 +1000 | [diff] [blame] | 168 | return(glob(pattern, flags | GLOB_ALTDIRFUNC, errfunc, |
Damien Miller | 4870afd | 2001-03-14 10:27:09 +1100 | [diff] [blame] | 169 | pglob)); |
| 170 | } |