blob: 849ac65ed1c873c12e7bef042428055fefbcfd9f [file] [log] [blame]
Damien Miller4870afd2001-03-14 10:27:09 +11001/*
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"
Damien Miller9f0f5c62001-12-21 14:45:46 +110026RCSID("$OpenBSD: sftp-glob.c,v 1.9 2001/12/19 07:18:56 deraadt Exp $");
Damien Miller4870afd2001-03-14 10:27:09 +110027
Damien Miller4870afd2001-03-14 10:27:09 +110028#include "buffer.h"
29#include "bufaux.h"
Damien Miller4870afd2001-03-14 10:27:09 +110030#include "xmalloc.h"
31#include "log.h"
Damien Miller4870afd2001-03-14 10:27:09 +110032
33#include "sftp.h"
34#include "sftp-common.h"
35#include "sftp-client.h"
36#include "sftp-glob.h"
37
38struct SFTP_OPENDIR {
39 SFTP_DIRENT **dir;
40 int offset;
41};
42
43static struct {
44 int fd_in;
45 int fd_out;
46} cur;
47
Ben Lindstrombba81212001-06-25 05:01:22 +000048static void *
49fudge_opendir(const char *path)
Damien Miller4870afd2001-03-14 10:27:09 +110050{
51 struct SFTP_OPENDIR *r;
Damien Miller9f0f5c62001-12-21 14:45:46 +110052
Damien Miller4870afd2001-03-14 10:27:09 +110053 r = xmalloc(sizeof(*r));
Damien Miller9f0f5c62001-12-21 14:45:46 +110054
Damien Miller4870afd2001-03-14 10:27:09 +110055 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 Lindstrombba81212001-06-25 05:01:22 +000063static struct dirent *
64fudge_readdir(struct SFTP_OPENDIR *od)
Damien Miller4870afd2001-03-14 10:27:09 +110065{
Damien Miller18bb4732001-03-28 14:35:30 +100066 /* Solaris needs sizeof(dirent) + path length (see below) */
67 static char buf[sizeof(struct dirent) + MAXPATHLEN];
68 struct dirent *ret = (struct dirent *)buf;
Damien Miller4870afd2001-03-14 10:27:09 +110069#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 Miller18bb4732001-03-28 14:35:30 +100076 memset(buf, 0, sizeof(buf));
Damien Miller4870afd2001-03-14 10:27:09 +110077
Damien Miller18bb4732001-03-28 14:35:30 +100078 /*
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 Lindstroma3700052001-04-05 23:26:32 +000085 strlcpy(ret->d_name, od->dir[od->offset++]->filename,
Damien Miller18bb4732001-03-28 14:35:30 +100086 sizeof(ret->d_name));
87#endif
Damien Miller4870afd2001-03-14 10:27:09 +110088#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 Miller18bb4732001-03-28 14:35:30 +100094 ret->d_ino = inum++;
Damien Miller4870afd2001-03-14 10:27:09 +110095 if (!inum)
96 inum = 1;
97#endif /* __GNU_LIBRARY__ */
98
Damien Miller18bb4732001-03-28 14:35:30 +100099 return(ret);
Damien Miller4870afd2001-03-14 10:27:09 +1100100}
101
Ben Lindstrombba81212001-06-25 05:01:22 +0000102static void
103fudge_closedir(struct SFTP_OPENDIR *od)
Damien Miller4870afd2001-03-14 10:27:09 +1100104{
105 free_sftp_dirents(od->dir);
Ben Lindstrom86ebcb62001-04-04 01:53:20 +0000106 xfree(od);
Damien Miller4870afd2001-03-14 10:27:09 +1100107}
108
Ben Lindstrombba81212001-06-25 05:01:22 +0000109static void
110attrib_to_stat(Attrib *a, struct stat *st)
Damien Miller4870afd2001-03-14 10:27:09 +1100111{
112 memset(st, 0, sizeof(*st));
Damien Miller9f0f5c62001-12-21 14:45:46 +1100113
Damien Miller4870afd2001-03-14 10:27:09 +1100114 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 Lindstrombba81212001-06-25 05:01:22 +0000128static int
129fudge_lstat(const char *path, struct stat *st)
Damien Miller4870afd2001-03-14 10:27:09 +1100130{
131 Attrib *a;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100132
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000133 if (!(a = do_lstat(cur.fd_in, cur.fd_out, (char*)path, 0)))
Damien Miller4870afd2001-03-14 10:27:09 +1100134 return(-1);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100135
Damien Miller4870afd2001-03-14 10:27:09 +1100136 attrib_to_stat(a, st);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100137
Damien Miller4870afd2001-03-14 10:27:09 +1100138 return(0);
139}
140
Ben Lindstrombba81212001-06-25 05:01:22 +0000141static int
142fudge_stat(const char *path, struct stat *st)
Damien Miller4870afd2001-03-14 10:27:09 +1100143{
144 Attrib *a;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100145
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000146 if (!(a = do_stat(cur.fd_in, cur.fd_out, (char*)path, 0)))
Damien Miller4870afd2001-03-14 10:27:09 +1100147 return(-1);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100148
Damien Miller4870afd2001-03-14 10:27:09 +1100149 attrib_to_stat(a, st);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100150
Damien Miller4870afd2001-03-14 10:27:09 +1100151 return(0);
152}
153
154int
Ben Lindstroma3700052001-04-05 23:26:32 +0000155remote_glob(int fd_in, int fd_out, const char *pattern, int flags,
Ben Lindstrom206941f2001-04-15 14:27:16 +0000156 int (*errfunc)(const char *, int), glob_t *pglob)
Damien Miller4870afd2001-03-14 10:27:09 +1100157{
Damien Millerea12acf2001-07-14 12:14:56 +1000158 pglob->gl_opendir = fudge_opendir;
159 pglob->gl_readdir = (struct dirent *(*)(void *))fudge_readdir;
160 pglob->gl_closedir = (void (*)(void *))fudge_closedir;
Damien Miller4870afd2001-03-14 10:27:09 +1100161 pglob->gl_lstat = fudge_lstat;
162 pglob->gl_stat = fudge_stat;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100163
Damien Miller4870afd2001-03-14 10:27:09 +1100164 memset(&cur, 0, sizeof(cur));
165 cur.fd_in = fd_in;
166 cur.fd_out = fd_out;
167
Damien Millerea12acf2001-07-14 12:14:56 +1000168 return(glob(pattern, flags | GLOB_ALTDIRFUNC, errfunc,
Damien Miller4870afd2001-03-14 10:27:09 +1100169 pglob));
170}