xref: /openbsd-src/usr.bin/ssh/sftp-glob.c (revision b2ea75c1b17e1a9a339660e7ed45cd24946b230e)
1 /*
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"
26 RCSID("$OpenBSD: sftp-glob.c,v 1.8 2001/07/14 15:10:17 stevesk 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 	int fd_in;
47 	int fd_out;
48 } cur;
49 
50 static void *
51 fudge_opendir(const char *path)
52 {
53 	struct SFTP_OPENDIR *r;
54 
55 	r = xmalloc(sizeof(*r));
56 
57 	if (do_readdir(cur.fd_in, cur.fd_out, (char*)path, &r->dir))
58 		return(NULL);
59 
60 	r->offset = 0;
61 
62 	return((void*)r);
63 }
64 
65 static struct dirent *
66 fudge_readdir(struct SFTP_OPENDIR *od)
67 {
68 	static struct dirent ret;
69 
70 	if (od->dir[od->offset] == NULL)
71 		return(NULL);
72 
73 	memset(&ret, 0, sizeof(ret));
74 	strlcpy(ret.d_name, od->dir[od->offset++]->filename,
75 	    sizeof(ret.d_name));
76 
77 	return(&ret);
78 }
79 
80 static void
81 fudge_closedir(struct SFTP_OPENDIR *od)
82 {
83 	free_sftp_dirents(od->dir);
84 	xfree(od);
85 }
86 
87 static void
88 attrib_to_stat(Attrib *a, struct stat *st)
89 {
90 	memset(st, 0, sizeof(*st));
91 
92 	if (a->flags & SSH2_FILEXFER_ATTR_SIZE)
93 		st->st_size = a->size;
94 	if (a->flags & SSH2_FILEXFER_ATTR_UIDGID) {
95 		st->st_uid = a->uid;
96 		st->st_gid = a->gid;
97 	}
98 	if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)
99 		st->st_mode = a->perm;
100 	if (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
101 		st->st_atime = a->atime;
102 		st->st_mtime = a->mtime;
103 	}
104 }
105 
106 static int
107 fudge_lstat(const char *path, struct stat *st)
108 {
109 	Attrib *a;
110 
111 	if (!(a = do_lstat(cur.fd_in, cur.fd_out, (char*)path, 0)))
112 		return(-1);
113 
114 	attrib_to_stat(a, st);
115 
116 	return(0);
117 }
118 
119 static int
120 fudge_stat(const char *path, struct stat *st)
121 {
122 	Attrib *a;
123 
124 	if (!(a = do_stat(cur.fd_in, cur.fd_out, (char*)path, 0)))
125 		return(-1);
126 
127 	attrib_to_stat(a, st);
128 
129 	return(0);
130 }
131 
132 int
133 remote_glob(int fd_in, int fd_out, const char *pattern, int flags,
134     int (*errfunc)(const char *, int), glob_t *pglob)
135 {
136 	pglob->gl_opendir = fudge_opendir;
137 	pglob->gl_readdir = (struct dirent *(*)(void *))fudge_readdir;
138 	pglob->gl_closedir = (void (*)(void *))fudge_closedir;
139 	pglob->gl_lstat = fudge_lstat;
140 	pglob->gl_stat = fudge_stat;
141 
142 	memset(&cur, 0, sizeof(cur));
143 	cur.fd_in = fd_in;
144 	cur.fd_out = fd_out;
145 
146 	return(glob(pattern, flags | GLOB_ALTDIRFUNC, errfunc,
147 	    pglob));
148 }
149