blob: cdc270827972c7074f67bdb06ca46045929d11f8 [file] [log] [blame]
Damien Millerd7834352006-08-05 12:39:39 +10001/* $OpenBSD: sftp-glob.c,v 1.22 2006/08/03 03:34:42 deraadt Exp $ */
Damien Miller4870afd2001-03-14 10:27:09 +11002/*
Damien Miller4e60ed72004-02-17 17:07:59 +11003 * Copyright (c) 2001-2004 Damien Miller <djm@openbsd.org>
Damien Miller4870afd2001-03-14 10:27:09 +11004 *
Damien Miller4e60ed72004-02-17 17:07:59 +11005 * 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.
Damien Miller4870afd2001-03-14 10:27:09 +11008 *
Damien Miller4e60ed72004-02-17 17:07:59 +11009 * 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.
Damien Miller4870afd2001-03-14 10:27:09 +110016 */
17
18#include "includes.h"
Damien Millerf17883e2006-03-15 11:45:54 +110019
20#include <sys/types.h>
21#ifdef HAVE_SYS_STAT_H
22# include <sys/stat.h>
23#endif
Damien Millere3476ed2006-07-24 14:13:33 +100024
Damien Miller88f254b2006-03-15 11:25:13 +110025#include <dirent.h>
Damien Millere3476ed2006-07-24 14:13:33 +100026#include <string.h>
Damien Miller4870afd2001-03-14 10:27:09 +110027
Damien Miller4870afd2001-03-14 10:27:09 +110028#include "xmalloc.h"
Damien Miller4870afd2001-03-14 10:27:09 +110029#include "sftp.h"
Damien Millerd7834352006-08-05 12:39:39 +100030#include "buffer.h"
Damien Miller4870afd2001-03-14 10:27:09 +110031#include "sftp-common.h"
32#include "sftp-client.h"
Damien Millerd7d46bb2004-02-18 14:11:13 +110033
34int remote_glob(struct sftp_conn *, const char *, int,
35 int (*)(const char *, int), glob_t *);
Damien Miller4870afd2001-03-14 10:27:09 +110036
37struct SFTP_OPENDIR {
38 SFTP_DIRENT **dir;
39 int offset;
40};
41
42static struct {
Damien Miller3db5f532002-02-13 14:10:32 +110043 struct sftp_conn *conn;
Damien Miller4870afd2001-03-14 10:27:09 +110044} cur;
45
Ben Lindstrombba81212001-06-25 05:01:22 +000046static void *
47fudge_opendir(const char *path)
Damien Miller4870afd2001-03-14 10:27:09 +110048{
49 struct SFTP_OPENDIR *r;
Damien Miller9f0f5c62001-12-21 14:45:46 +110050
Damien Miller4870afd2001-03-14 10:27:09 +110051 r = xmalloc(sizeof(*r));
Damien Miller9f0f5c62001-12-21 14:45:46 +110052
Ben Lindstromc51b9242002-07-07 22:10:15 +000053 if (do_readdir(cur.conn, (char *)path, &r->dir)) {
54 xfree(r);
Damien Miller4870afd2001-03-14 10:27:09 +110055 return(NULL);
Ben Lindstromc51b9242002-07-07 22:10:15 +000056 }
Damien Miller4870afd2001-03-14 10:27:09 +110057
58 r->offset = 0;
59
Ben Lindstroma962c2f2002-07-04 00:14:17 +000060 return((void *)r);
Damien Miller4870afd2001-03-14 10:27:09 +110061}
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__ */
Damien Miller787b2ec2003-11-21 23:56:47 +110072
Damien Miller4870afd2001-03-14 10:27:09 +110073 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
Damien Millera8e06ce2003-11-21 23:48:55 +110091 * ALTDIRFUNCs. Not that this is documented anywhere but the
Damien Miller4870afd2001-03-14 10:27:09 +110092 * 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 int
110fudge_lstat(const char *path, struct stat *st)
Damien Miller4870afd2001-03-14 10:27:09 +1100111{
112 Attrib *a;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100113
Ben Lindstroma962c2f2002-07-04 00:14:17 +0000114 if (!(a = do_lstat(cur.conn, (char *)path, 0)))
Damien Miller4870afd2001-03-14 10:27:09 +1100115 return(-1);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100116
Damien Miller4870afd2001-03-14 10:27:09 +1100117 attrib_to_stat(a, st);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100118
Damien Miller4870afd2001-03-14 10:27:09 +1100119 return(0);
120}
121
Ben Lindstrombba81212001-06-25 05:01:22 +0000122static int
123fudge_stat(const char *path, struct stat *st)
Damien Miller4870afd2001-03-14 10:27:09 +1100124{
125 Attrib *a;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100126
Ben Lindstroma962c2f2002-07-04 00:14:17 +0000127 if (!(a = do_stat(cur.conn, (char *)path, 0)))
Damien Miller4870afd2001-03-14 10:27:09 +1100128 return(-1);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100129
Damien Miller4870afd2001-03-14 10:27:09 +1100130 attrib_to_stat(a, st);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100131
Damien Miller4870afd2001-03-14 10:27:09 +1100132 return(0);
133}
134
135int
Damien Miller3db5f532002-02-13 14:10:32 +1100136remote_glob(struct sftp_conn *conn, const char *pattern, int flags,
Ben Lindstrom206941f2001-04-15 14:27:16 +0000137 int (*errfunc)(const char *, int), glob_t *pglob)
Damien Miller4870afd2001-03-14 10:27:09 +1100138{
Damien Millerea12acf2001-07-14 12:14:56 +1000139 pglob->gl_opendir = fudge_opendir;
140 pglob->gl_readdir = (struct dirent *(*)(void *))fudge_readdir;
141 pglob->gl_closedir = (void (*)(void *))fudge_closedir;
Damien Miller4870afd2001-03-14 10:27:09 +1100142 pglob->gl_lstat = fudge_lstat;
143 pglob->gl_stat = fudge_stat;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100144
Damien Miller4870afd2001-03-14 10:27:09 +1100145 memset(&cur, 0, sizeof(cur));
Damien Miller3db5f532002-02-13 14:10:32 +1100146 cur.conn = conn;
Damien Miller4870afd2001-03-14 10:27:09 +1100147
Damien Miller3db5f532002-02-13 14:10:32 +1100148 return(glob(pattern, flags | GLOB_ALTDIRFUNC, errfunc, pglob));
Damien Miller4870afd2001-03-14 10:27:09 +1100149}