Damien Miller | 4870afd | 2001-03-14 10:27:09 +1100 | [diff] [blame] | 1 | /* |
Damien Miller | 4e60ed7 | 2004-02-17 17:07:59 +1100 | [diff] [blame] | 2 | * Copyright (c) 2001-2004 Damien Miller <djm@openbsd.org> |
Damien Miller | 4870afd | 2001-03-14 10:27:09 +1100 | [diff] [blame] | 3 | * |
Damien Miller | 4e60ed7 | 2004-02-17 17:07:59 +1100 | [diff] [blame] | 4 | * Permission to use, copy, modify, and distribute this software for any |
| 5 | * purpose with or without fee is hereby granted, provided that the above |
| 6 | * copyright notice and this permission notice appear in all copies. |
Damien Miller | 4870afd | 2001-03-14 10:27:09 +1100 | [diff] [blame] | 7 | * |
Damien Miller | 4e60ed7 | 2004-02-17 17:07:59 +1100 | [diff] [blame] | 8 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 9 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 10 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 11 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 12 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 13 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 14 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
Damien Miller | 4870afd | 2001-03-14 10:27:09 +1100 | [diff] [blame] | 15 | */ |
| 16 | |
| 17 | #include "includes.h" |
Damien Miller | 88f254b | 2006-03-15 11:25:13 +1100 | [diff] [blame] | 18 | RCSID("$OpenBSD: sftp-glob.c,v 1.16 2006/02/08 23:51:24 stevesk Exp $"); |
| 19 | |
| 20 | #include <dirent.h> |
Damien Miller | 4870afd | 2001-03-14 10:27:09 +1100 | [diff] [blame] | 21 | |
Damien Miller | 4870afd | 2001-03-14 10:27:09 +1100 | [diff] [blame] | 22 | #include "buffer.h" |
| 23 | #include "bufaux.h" |
Damien Miller | 4870afd | 2001-03-14 10:27:09 +1100 | [diff] [blame] | 24 | #include "xmalloc.h" |
| 25 | #include "log.h" |
Damien Miller | 4870afd | 2001-03-14 10:27:09 +1100 | [diff] [blame] | 26 | |
| 27 | #include "sftp.h" |
| 28 | #include "sftp-common.h" |
| 29 | #include "sftp-client.h" |
Damien Miller | d7d46bb | 2004-02-18 14:11:13 +1100 | [diff] [blame] | 30 | |
| 31 | int remote_glob(struct sftp_conn *, const char *, int, |
| 32 | int (*)(const char *, int), glob_t *); |
Damien Miller | 4870afd | 2001-03-14 10:27:09 +1100 | [diff] [blame] | 33 | |
| 34 | struct SFTP_OPENDIR { |
| 35 | SFTP_DIRENT **dir; |
| 36 | int offset; |
| 37 | }; |
| 38 | |
| 39 | static struct { |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 40 | struct sftp_conn *conn; |
Damien Miller | 4870afd | 2001-03-14 10:27:09 +1100 | [diff] [blame] | 41 | } cur; |
| 42 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 43 | static void * |
| 44 | fudge_opendir(const char *path) |
Damien Miller | 4870afd | 2001-03-14 10:27:09 +1100 | [diff] [blame] | 45 | { |
| 46 | struct SFTP_OPENDIR *r; |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 47 | |
Damien Miller | 4870afd | 2001-03-14 10:27:09 +1100 | [diff] [blame] | 48 | r = xmalloc(sizeof(*r)); |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 49 | |
Ben Lindstrom | c51b924 | 2002-07-07 22:10:15 +0000 | [diff] [blame] | 50 | if (do_readdir(cur.conn, (char *)path, &r->dir)) { |
| 51 | xfree(r); |
Damien Miller | 4870afd | 2001-03-14 10:27:09 +1100 | [diff] [blame] | 52 | return(NULL); |
Ben Lindstrom | c51b924 | 2002-07-07 22:10:15 +0000 | [diff] [blame] | 53 | } |
Damien Miller | 4870afd | 2001-03-14 10:27:09 +1100 | [diff] [blame] | 54 | |
| 55 | r->offset = 0; |
| 56 | |
Ben Lindstrom | a962c2f | 2002-07-04 00:14:17 +0000 | [diff] [blame] | 57 | return((void *)r); |
Damien Miller | 4870afd | 2001-03-14 10:27:09 +1100 | [diff] [blame] | 58 | } |
| 59 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 60 | static struct dirent * |
| 61 | fudge_readdir(struct SFTP_OPENDIR *od) |
Damien Miller | 4870afd | 2001-03-14 10:27:09 +1100 | [diff] [blame] | 62 | { |
Damien Miller | 18bb473 | 2001-03-28 14:35:30 +1000 | [diff] [blame] | 63 | /* Solaris needs sizeof(dirent) + path length (see below) */ |
| 64 | static char buf[sizeof(struct dirent) + MAXPATHLEN]; |
| 65 | struct dirent *ret = (struct dirent *)buf; |
Damien Miller | 4870afd | 2001-03-14 10:27:09 +1100 | [diff] [blame] | 66 | #ifdef __GNU_LIBRARY__ |
| 67 | static int inum = 1; |
| 68 | #endif /* __GNU_LIBRARY__ */ |
Damien Miller | 787b2ec | 2003-11-21 23:56:47 +1100 | [diff] [blame] | 69 | |
Damien Miller | 4870afd | 2001-03-14 10:27:09 +1100 | [diff] [blame] | 70 | if (od->dir[od->offset] == NULL) |
| 71 | return(NULL); |
| 72 | |
Damien Miller | 18bb473 | 2001-03-28 14:35:30 +1000 | [diff] [blame] | 73 | memset(buf, 0, sizeof(buf)); |
Damien Miller | 4870afd | 2001-03-14 10:27:09 +1100 | [diff] [blame] | 74 | |
Damien Miller | 18bb473 | 2001-03-28 14:35:30 +1000 | [diff] [blame] | 75 | /* |
| 76 | * Solaris defines dirent->d_name as a one byte array and expects |
| 77 | * you to hack around it. |
| 78 | */ |
| 79 | #ifdef BROKEN_ONE_BYTE_DIRENT_D_NAME |
| 80 | strlcpy(ret->d_name, od->dir[od->offset++]->filename, MAXPATHLEN); |
| 81 | #else |
Ben Lindstrom | a370005 | 2001-04-05 23:26:32 +0000 | [diff] [blame] | 82 | strlcpy(ret->d_name, od->dir[od->offset++]->filename, |
Damien Miller | 18bb473 | 2001-03-28 14:35:30 +1000 | [diff] [blame] | 83 | sizeof(ret->d_name)); |
| 84 | #endif |
Damien Miller | 4870afd | 2001-03-14 10:27:09 +1100 | [diff] [blame] | 85 | #ifdef __GNU_LIBRARY__ |
| 86 | /* |
| 87 | * Idiot glibc uses extensions to struct dirent for readdir with |
Damien Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 88 | * ALTDIRFUNCs. Not that this is documented anywhere but the |
Damien Miller | 4870afd | 2001-03-14 10:27:09 +1100 | [diff] [blame] | 89 | * source... Fake an inode number to appease it. |
| 90 | */ |
Damien Miller | 18bb473 | 2001-03-28 14:35:30 +1000 | [diff] [blame] | 91 | ret->d_ino = inum++; |
Damien Miller | 4870afd | 2001-03-14 10:27:09 +1100 | [diff] [blame] | 92 | if (!inum) |
| 93 | inum = 1; |
| 94 | #endif /* __GNU_LIBRARY__ */ |
| 95 | |
Damien Miller | 18bb473 | 2001-03-28 14:35:30 +1000 | [diff] [blame] | 96 | return(ret); |
Damien Miller | 4870afd | 2001-03-14 10:27:09 +1100 | [diff] [blame] | 97 | } |
| 98 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 99 | static void |
| 100 | fudge_closedir(struct SFTP_OPENDIR *od) |
Damien Miller | 4870afd | 2001-03-14 10:27:09 +1100 | [diff] [blame] | 101 | { |
| 102 | free_sftp_dirents(od->dir); |
Ben Lindstrom | 86ebcb6 | 2001-04-04 01:53:20 +0000 | [diff] [blame] | 103 | xfree(od); |
Damien Miller | 4870afd | 2001-03-14 10:27:09 +1100 | [diff] [blame] | 104 | } |
| 105 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 106 | static int |
| 107 | fudge_lstat(const char *path, struct stat *st) |
Damien Miller | 4870afd | 2001-03-14 10:27:09 +1100 | [diff] [blame] | 108 | { |
| 109 | Attrib *a; |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 110 | |
Ben Lindstrom | a962c2f | 2002-07-04 00:14:17 +0000 | [diff] [blame] | 111 | if (!(a = do_lstat(cur.conn, (char *)path, 0))) |
Damien Miller | 4870afd | 2001-03-14 10:27:09 +1100 | [diff] [blame] | 112 | return(-1); |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 113 | |
Damien Miller | 4870afd | 2001-03-14 10:27:09 +1100 | [diff] [blame] | 114 | attrib_to_stat(a, st); |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 115 | |
Damien Miller | 4870afd | 2001-03-14 10:27:09 +1100 | [diff] [blame] | 116 | return(0); |
| 117 | } |
| 118 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 119 | static int |
| 120 | fudge_stat(const char *path, struct stat *st) |
Damien Miller | 4870afd | 2001-03-14 10:27:09 +1100 | [diff] [blame] | 121 | { |
| 122 | Attrib *a; |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 123 | |
Ben Lindstrom | a962c2f | 2002-07-04 00:14:17 +0000 | [diff] [blame] | 124 | if (!(a = do_stat(cur.conn, (char *)path, 0))) |
Damien Miller | 4870afd | 2001-03-14 10:27:09 +1100 | [diff] [blame] | 125 | return(-1); |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 126 | |
Damien Miller | 4870afd | 2001-03-14 10:27:09 +1100 | [diff] [blame] | 127 | attrib_to_stat(a, st); |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 128 | |
Damien Miller | 4870afd | 2001-03-14 10:27:09 +1100 | [diff] [blame] | 129 | return(0); |
| 130 | } |
| 131 | |
| 132 | int |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 133 | remote_glob(struct sftp_conn *conn, const char *pattern, int flags, |
Ben Lindstrom | 206941f | 2001-04-15 14:27:16 +0000 | [diff] [blame] | 134 | int (*errfunc)(const char *, int), glob_t *pglob) |
Damien Miller | 4870afd | 2001-03-14 10:27:09 +1100 | [diff] [blame] | 135 | { |
Damien Miller | ea12acf | 2001-07-14 12:14:56 +1000 | [diff] [blame] | 136 | pglob->gl_opendir = fudge_opendir; |
| 137 | pglob->gl_readdir = (struct dirent *(*)(void *))fudge_readdir; |
| 138 | pglob->gl_closedir = (void (*)(void *))fudge_closedir; |
Damien Miller | 4870afd | 2001-03-14 10:27:09 +1100 | [diff] [blame] | 139 | pglob->gl_lstat = fudge_lstat; |
| 140 | pglob->gl_stat = fudge_stat; |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 141 | |
Damien Miller | 4870afd | 2001-03-14 10:27:09 +1100 | [diff] [blame] | 142 | memset(&cur, 0, sizeof(cur)); |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 143 | cur.conn = conn; |
Damien Miller | 4870afd | 2001-03-14 10:27:09 +1100 | [diff] [blame] | 144 | |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 145 | return(glob(pattern, flags | GLOB_ALTDIRFUNC, errfunc, pglob)); |
Damien Miller | 4870afd | 2001-03-14 10:27:09 +1100 | [diff] [blame] | 146 | } |