xref: /titanic_41/usr/src/cmd/ssh/sftp/sftp-glob.c (revision 66f9d5cb3cc0652e2d9d1366fb950efbe4ca2f24)
1 /*
2  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
3  * Use is subject to license terms.
4  */
5 /*
6  * Copyright (c) 2001,2002 Damien Miller.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include "includes.h"
30 RCSID("$OpenBSD: sftp-glob.c,v 1.13 2002/09/11 22:41:50 djm Exp $");
31 
32 #pragma ident	"%Z%%M%	%I%	%E% SMI"
33 
34 #include "buffer.h"
35 #include "bufaux.h"
36 #include "xmalloc.h"
37 #include "log.h"
38 
39 #include "sftp.h"
40 #include "sftp-common.h"
41 #include "sftp-client.h"
42 #include "sftp-glob.h"
43 
44 struct SFTP_OPENDIR {
45 	SFTP_DIRENT **dir;
46 	int offset;
47 };
48 
49 static struct {
50 	struct sftp_conn *conn;
51 } cur;
52 
53 static void *
54 fudge_opendir(const char *path)
55 {
56 	struct SFTP_OPENDIR *r;
57 
58 	r = xmalloc(sizeof(*r));
59 
60 	if (do_readdir(cur.conn, (char *)path, &r->dir)) {
61 		xfree(r);
62 		return(NULL);
63 	}
64 
65 	r->offset = 0;
66 
67 	return((void *)r);
68 }
69 
70 static struct dirent *
71 fudge_readdir(struct SFTP_OPENDIR *od)
72 {
73 	/* Solaris needs sizeof(dirent) + path length (see below) */
74 	static union {
75 		char buf_chars[sizeof (struct dirent) + MAXPATHLEN];
76 		struct dirent buf_dirent;
77 	} buf;
78 	struct dirent *ret = &buf.buf_dirent;
79 #ifdef __GNU_LIBRARY__
80 	static int inum = 1;
81 #endif /* __GNU_LIBRARY__ */
82 
83 	if (od->dir[od->offset] == NULL)
84 		return(NULL);
85 
86 	memset(buf.buf_chars, 0, sizeof (buf.buf_chars));
87 
88 	/*
89 	 * Solaris defines dirent->d_name as a one byte array and expects
90 	 * you to hack around it.
91 	 */
92 #ifdef BROKEN_ONE_BYTE_DIRENT_D_NAME
93 	strlcpy(ret->d_name, od->dir[od->offset++]->filename, MAXPATHLEN);
94 #else
95 	strlcpy(ret->d_name, od->dir[od->offset++]->filename,
96 	    sizeof(ret->d_name));
97 #endif
98 #ifdef __GNU_LIBRARY__
99 	/*
100 	 * Idiot glibc uses extensions to struct dirent for readdir with
101 	 * ALTDIRFUNCs. Not that this is documented anywhere but the
102 	 * source... Fake an inode number to appease it.
103 	 */
104 	ret->d_ino = inum++;
105 	if (!inum)
106 		inum = 1;
107 #endif /* __GNU_LIBRARY__ */
108 
109 	return(ret);
110 }
111 
112 static void
113 fudge_closedir(struct SFTP_OPENDIR *od)
114 {
115 	free_sftp_dirents(od->dir);
116 	xfree(od);
117 }
118 
119 static int
120 fudge_lstat(const char *path, struct stat *st)
121 {
122 	Attrib *a;
123 
124 	if (!(a = do_lstat(cur.conn, (char *)path, 0)))
125 		return(-1);
126 
127 	attrib_to_stat(a, st);
128 
129 	return(0);
130 }
131 
132 static int
133 fudge_stat(const char *path, struct stat *st)
134 {
135 	Attrib *a;
136 
137 	if (!(a = do_stat(cur.conn, (char *)path, 0)))
138 		return(-1);
139 
140 	attrib_to_stat(a, st);
141 
142 	return(0);
143 }
144 
145 int
146 remote_glob(struct sftp_conn *conn, const char *pattern, int flags,
147     int (*errfunc)(const char *, int), glob_t *pglob)
148 {
149 	pglob->gl_opendir = fudge_opendir;
150 	pglob->gl_readdir = (struct dirent *(*)(void *))fudge_readdir;
151 	pglob->gl_closedir = (void (*)(void *))fudge_closedir;
152 	pglob->gl_lstat = fudge_lstat;
153 	pglob->gl_stat = fudge_stat;
154 
155 	memset(&cur, 0, sizeof(cur));
156 	cur.conn = conn;
157 
158 	return(glob(pattern, flags | GLOB_ALTDIRFUNC, errfunc, pglob));
159 }
160