xref: /openbsd-src/usr.bin/ssh/sftp-glob.c (revision db3296cf5c1dd9058ceecc3a29fe4aaa0bd26000)
1 /*
2  * Copyright (c) 2001,2002 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"
26 RCSID("$OpenBSD: sftp-glob.c,v 1.13 2002/09/11 22:41:50 djm Exp $");
27 
28 #include <glob.h>
29 
30 #include "buffer.h"
31 #include "bufaux.h"
32 #include "xmalloc.h"
33 #include "log.h"
34 
35 #include "sftp.h"
36 #include "sftp-common.h"
37 #include "sftp-client.h"
38 #include "sftp-glob.h"
39 
40 struct SFTP_OPENDIR {
41 	SFTP_DIRENT **dir;
42 	int offset;
43 };
44 
45 static struct {
46 	struct sftp_conn *conn;
47 } cur;
48 
49 static void *
50 fudge_opendir(const char *path)
51 {
52 	struct SFTP_OPENDIR *r;
53 
54 	r = xmalloc(sizeof(*r));
55 
56 	if (do_readdir(cur.conn, (char *)path, &r->dir)) {
57 		xfree(r);
58 		return(NULL);
59 	}
60 
61 	r->offset = 0;
62 
63 	return((void *)r);
64 }
65 
66 static struct dirent *
67 fudge_readdir(struct SFTP_OPENDIR *od)
68 {
69 	static struct dirent ret;
70 
71 	if (od->dir[od->offset] == NULL)
72 		return(NULL);
73 
74 	memset(&ret, 0, sizeof(ret));
75 	strlcpy(ret.d_name, od->dir[od->offset++]->filename,
76 	    sizeof(ret.d_name));
77 
78 	return(&ret);
79 }
80 
81 static void
82 fudge_closedir(struct SFTP_OPENDIR *od)
83 {
84 	free_sftp_dirents(od->dir);
85 	xfree(od);
86 }
87 
88 static int
89 fudge_lstat(const char *path, struct stat *st)
90 {
91 	Attrib *a;
92 
93 	if (!(a = do_lstat(cur.conn, (char *)path, 0)))
94 		return(-1);
95 
96 	attrib_to_stat(a, st);
97 
98 	return(0);
99 }
100 
101 static int
102 fudge_stat(const char *path, struct stat *st)
103 {
104 	Attrib *a;
105 
106 	if (!(a = do_stat(cur.conn, (char *)path, 0)))
107 		return(-1);
108 
109 	attrib_to_stat(a, st);
110 
111 	return(0);
112 }
113 
114 int
115 remote_glob(struct sftp_conn *conn, const char *pattern, int flags,
116     int (*errfunc)(const char *, int), glob_t *pglob)
117 {
118 	pglob->gl_opendir = fudge_opendir;
119 	pglob->gl_readdir = (struct dirent *(*)(void *))fudge_readdir;
120 	pglob->gl_closedir = (void (*)(void *))fudge_closedir;
121 	pglob->gl_lstat = fudge_lstat;
122 	pglob->gl_stat = fudge_stat;
123 
124 	memset(&cur, 0, sizeof(cur));
125 	cur.conn = conn;
126 
127 	return(glob(pattern, flags | GLOB_ALTDIRFUNC, errfunc, pglob));
128 }
129