blob: baa84aa7129a9ae737eccebcf9741d43206f49ec [file] [log] [blame]
Damien Miller4870afd2001-03-14 10:27:09 +11001/*
Damien Miller4e60ed72004-02-17 17:07:59 +11002 * Copyright (c) 2001-2004 Damien Miller <djm@openbsd.org>
Damien Miller4870afd2001-03-14 10:27:09 +11003 *
Damien Miller4e60ed72004-02-17 17:07:59 +11004 * 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 Miller4870afd2001-03-14 10:27:09 +11007 *
Damien Miller4e60ed72004-02-17 17:07:59 +11008 * 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 Miller4870afd2001-03-14 10:27:09 +110015 */
16
17#include "includes.h"
Damien Millerf17883e2006-03-15 11:45:54 +110018RCSID("$OpenBSD: sftp-glob.c,v 1.17 2006/02/20 17:19:54 stevesk Exp $");
19
20#include <sys/types.h>
21#ifdef HAVE_SYS_STAT_H
22# include <sys/stat.h>
23#endif
Damien Miller88f254b2006-03-15 11:25:13 +110024
25#include <dirent.h>
Damien Miller4870afd2001-03-14 10:27:09 +110026
Damien Miller4870afd2001-03-14 10:27:09 +110027#include "buffer.h"
28#include "bufaux.h"
Damien Miller4870afd2001-03-14 10:27:09 +110029#include "xmalloc.h"
30#include "log.h"
Damien Miller4870afd2001-03-14 10:27:09 +110031
32#include "sftp.h"
33#include "sftp-common.h"
34#include "sftp-client.h"
Damien Millerd7d46bb2004-02-18 14:11:13 +110035
36int remote_glob(struct sftp_conn *, const char *, int,
37 int (*)(const char *, int), glob_t *);
Damien Miller4870afd2001-03-14 10:27:09 +110038
39struct SFTP_OPENDIR {
40 SFTP_DIRENT **dir;
41 int offset;
42};
43
44static struct {
Damien Miller3db5f532002-02-13 14:10:32 +110045 struct sftp_conn *conn;
Damien Miller4870afd2001-03-14 10:27:09 +110046} 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
Ben Lindstromc51b9242002-07-07 22:10:15 +000055 if (do_readdir(cur.conn, (char *)path, &r->dir)) {
56 xfree(r);
Damien Miller4870afd2001-03-14 10:27:09 +110057 return(NULL);
Ben Lindstromc51b9242002-07-07 22:10:15 +000058 }
Damien Miller4870afd2001-03-14 10:27:09 +110059
60 r->offset = 0;
61
Ben Lindstroma962c2f2002-07-04 00:14:17 +000062 return((void *)r);
Damien Miller4870afd2001-03-14 10:27:09 +110063}
64
Ben Lindstrombba81212001-06-25 05:01:22 +000065static struct dirent *
66fudge_readdir(struct SFTP_OPENDIR *od)
Damien Miller4870afd2001-03-14 10:27:09 +110067{
Damien Miller18bb4732001-03-28 14:35:30 +100068 /* Solaris needs sizeof(dirent) + path length (see below) */
69 static char buf[sizeof(struct dirent) + MAXPATHLEN];
70 struct dirent *ret = (struct dirent *)buf;
Damien Miller4870afd2001-03-14 10:27:09 +110071#ifdef __GNU_LIBRARY__
72 static int inum = 1;
73#endif /* __GNU_LIBRARY__ */
Damien Miller787b2ec2003-11-21 23:56:47 +110074
Damien Miller4870afd2001-03-14 10:27:09 +110075 if (od->dir[od->offset] == NULL)
76 return(NULL);
77
Damien Miller18bb4732001-03-28 14:35:30 +100078 memset(buf, 0, sizeof(buf));
Damien Miller4870afd2001-03-14 10:27:09 +110079
Damien Miller18bb4732001-03-28 14:35:30 +100080 /*
81 * Solaris defines dirent->d_name as a one byte array and expects
82 * you to hack around it.
83 */
84#ifdef BROKEN_ONE_BYTE_DIRENT_D_NAME
85 strlcpy(ret->d_name, od->dir[od->offset++]->filename, MAXPATHLEN);
86#else
Ben Lindstroma3700052001-04-05 23:26:32 +000087 strlcpy(ret->d_name, od->dir[od->offset++]->filename,
Damien Miller18bb4732001-03-28 14:35:30 +100088 sizeof(ret->d_name));
89#endif
Damien Miller4870afd2001-03-14 10:27:09 +110090#ifdef __GNU_LIBRARY__
91 /*
92 * Idiot glibc uses extensions to struct dirent for readdir with
Damien Millera8e06ce2003-11-21 23:48:55 +110093 * ALTDIRFUNCs. Not that this is documented anywhere but the
Damien Miller4870afd2001-03-14 10:27:09 +110094 * source... Fake an inode number to appease it.
95 */
Damien Miller18bb4732001-03-28 14:35:30 +100096 ret->d_ino = inum++;
Damien Miller4870afd2001-03-14 10:27:09 +110097 if (!inum)
98 inum = 1;
99#endif /* __GNU_LIBRARY__ */
100
Damien Miller18bb4732001-03-28 14:35:30 +1000101 return(ret);
Damien Miller4870afd2001-03-14 10:27:09 +1100102}
103
Ben Lindstrombba81212001-06-25 05:01:22 +0000104static void
105fudge_closedir(struct SFTP_OPENDIR *od)
Damien Miller4870afd2001-03-14 10:27:09 +1100106{
107 free_sftp_dirents(od->dir);
Ben Lindstrom86ebcb62001-04-04 01:53:20 +0000108 xfree(od);
Damien Miller4870afd2001-03-14 10:27:09 +1100109}
110
Ben Lindstrombba81212001-06-25 05:01:22 +0000111static int
112fudge_lstat(const char *path, struct stat *st)
Damien Miller4870afd2001-03-14 10:27:09 +1100113{
114 Attrib *a;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100115
Ben Lindstroma962c2f2002-07-04 00:14:17 +0000116 if (!(a = do_lstat(cur.conn, (char *)path, 0)))
Damien Miller4870afd2001-03-14 10:27:09 +1100117 return(-1);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100118
Damien Miller4870afd2001-03-14 10:27:09 +1100119 attrib_to_stat(a, st);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100120
Damien Miller4870afd2001-03-14 10:27:09 +1100121 return(0);
122}
123
Ben Lindstrombba81212001-06-25 05:01:22 +0000124static int
125fudge_stat(const char *path, struct stat *st)
Damien Miller4870afd2001-03-14 10:27:09 +1100126{
127 Attrib *a;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100128
Ben Lindstroma962c2f2002-07-04 00:14:17 +0000129 if (!(a = do_stat(cur.conn, (char *)path, 0)))
Damien Miller4870afd2001-03-14 10:27:09 +1100130 return(-1);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100131
Damien Miller4870afd2001-03-14 10:27:09 +1100132 attrib_to_stat(a, st);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100133
Damien Miller4870afd2001-03-14 10:27:09 +1100134 return(0);
135}
136
137int
Damien Miller3db5f532002-02-13 14:10:32 +1100138remote_glob(struct sftp_conn *conn, const char *pattern, int flags,
Ben Lindstrom206941f2001-04-15 14:27:16 +0000139 int (*errfunc)(const char *, int), glob_t *pglob)
Damien Miller4870afd2001-03-14 10:27:09 +1100140{
Damien Millerea12acf2001-07-14 12:14:56 +1000141 pglob->gl_opendir = fudge_opendir;
142 pglob->gl_readdir = (struct dirent *(*)(void *))fudge_readdir;
143 pglob->gl_closedir = (void (*)(void *))fudge_closedir;
Damien Miller4870afd2001-03-14 10:27:09 +1100144 pglob->gl_lstat = fudge_lstat;
145 pglob->gl_stat = fudge_stat;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100146
Damien Miller4870afd2001-03-14 10:27:09 +1100147 memset(&cur, 0, sizeof(cur));
Damien Miller3db5f532002-02-13 14:10:32 +1100148 cur.conn = conn;
Damien Miller4870afd2001-03-14 10:27:09 +1100149
Damien Miller3db5f532002-02-13 14:10:32 +1100150 return(glob(pattern, flags | GLOB_ALTDIRFUNC, errfunc, pglob));
Damien Miller4870afd2001-03-14 10:27:09 +1100151}