xref: /titanic_50/usr/src/cmd/ssh/sftp/sftp-glob.c (revision cf58b2543a341d4f5a661de47968a7016c3207ff)
17c478bd9Sstevel@tonic-gate /*
290685d2cSjp161948  * Copyright (c) 2001-2004 Damien Miller <djm@openbsd.org>
37c478bd9Sstevel@tonic-gate  *
490685d2cSjp161948  * Permission to use, copy, modify, and distribute this software for any
590685d2cSjp161948  * purpose with or without fee is hereby granted, provided that the above
690685d2cSjp161948  * copyright notice and this permission notice appear in all copies.
77c478bd9Sstevel@tonic-gate  *
890685d2cSjp161948  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
990685d2cSjp161948  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1090685d2cSjp161948  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1190685d2cSjp161948  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1290685d2cSjp161948  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1390685d2cSjp161948  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1490685d2cSjp161948  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
157c478bd9Sstevel@tonic-gate  */
167c478bd9Sstevel@tonic-gate 
1790685d2cSjp161948 /* $OpenBSD: sftp-glob.c,v 1.22 2006/08/03 03:34:42 deraadt Exp $ */
187c478bd9Sstevel@tonic-gate 
1990685d2cSjp161948 #include "includes.h"
207c478bd9Sstevel@tonic-gate 
2190685d2cSjp161948 #include <sys/types.h>
2290685d2cSjp161948 #ifdef HAVE_SYS_STAT_H
2390685d2cSjp161948 # include <sys/stat.h>
2490685d2cSjp161948 #endif
2590685d2cSjp161948 
2690685d2cSjp161948 #include <dirent.h>
2790685d2cSjp161948 #include <string.h>
2890685d2cSjp161948 
2990685d2cSjp161948 #include "xmalloc.h"
307c478bd9Sstevel@tonic-gate #include "sftp.h"
3190685d2cSjp161948 #include "buffer.h"
327c478bd9Sstevel@tonic-gate #include "sftp-common.h"
337c478bd9Sstevel@tonic-gate #include "sftp-client.h"
3490685d2cSjp161948 
3590685d2cSjp161948 int remote_glob(struct sftp_conn *, const char *, int,
3690685d2cSjp161948     int (*)(const char *, int), glob_t *);
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate struct SFTP_OPENDIR {
397c478bd9Sstevel@tonic-gate 	SFTP_DIRENT **dir;
407c478bd9Sstevel@tonic-gate 	int offset;
417c478bd9Sstevel@tonic-gate };
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate static struct {
447c478bd9Sstevel@tonic-gate 	struct sftp_conn *conn;
457c478bd9Sstevel@tonic-gate } cur;
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate static void *
fudge_opendir(const char * path)487c478bd9Sstevel@tonic-gate fudge_opendir(const char *path)
497c478bd9Sstevel@tonic-gate {
507c478bd9Sstevel@tonic-gate 	struct SFTP_OPENDIR *r;
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate 	r = xmalloc(sizeof(*r));
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate 	if (do_readdir(cur.conn, (char *)path, &r->dir)) {
557c478bd9Sstevel@tonic-gate 		xfree(r);
567c478bd9Sstevel@tonic-gate 		return(NULL);
577c478bd9Sstevel@tonic-gate 	}
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate 	r->offset = 0;
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate 	return((void *)r);
627c478bd9Sstevel@tonic-gate }
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate static struct dirent *
fudge_readdir(struct SFTP_OPENDIR * od)657c478bd9Sstevel@tonic-gate fudge_readdir(struct SFTP_OPENDIR *od)
667c478bd9Sstevel@tonic-gate {
677c478bd9Sstevel@tonic-gate 	/* Solaris needs sizeof(dirent) + path length (see below) */
687c478bd9Sstevel@tonic-gate 	static union {
697c478bd9Sstevel@tonic-gate 		char buf_chars[sizeof (struct dirent) + MAXPATHLEN];
707c478bd9Sstevel@tonic-gate 		struct dirent buf_dirent;
717c478bd9Sstevel@tonic-gate 	} buf;
727c478bd9Sstevel@tonic-gate 	struct dirent *ret = &buf.buf_dirent;
737c478bd9Sstevel@tonic-gate #ifdef __GNU_LIBRARY__
747c478bd9Sstevel@tonic-gate 	static int inum = 1;
757c478bd9Sstevel@tonic-gate #endif /* __GNU_LIBRARY__ */
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate 	if (od->dir[od->offset] == NULL)
787c478bd9Sstevel@tonic-gate 		return(NULL);
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate 	memset(buf.buf_chars, 0, sizeof (buf.buf_chars));
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate 	/*
837c478bd9Sstevel@tonic-gate 	 * Solaris defines dirent->d_name as a one byte array and expects
847c478bd9Sstevel@tonic-gate 	 * you to hack around it.
857c478bd9Sstevel@tonic-gate 	 */
867c478bd9Sstevel@tonic-gate #ifdef BROKEN_ONE_BYTE_DIRENT_D_NAME
877c478bd9Sstevel@tonic-gate 	strlcpy(ret->d_name, od->dir[od->offset++]->filename, MAXPATHLEN);
887c478bd9Sstevel@tonic-gate #else
897c478bd9Sstevel@tonic-gate 	strlcpy(ret->d_name, od->dir[od->offset++]->filename,
907c478bd9Sstevel@tonic-gate 	    sizeof(ret->d_name));
917c478bd9Sstevel@tonic-gate #endif
927c478bd9Sstevel@tonic-gate #ifdef __GNU_LIBRARY__
937c478bd9Sstevel@tonic-gate 	/*
947c478bd9Sstevel@tonic-gate 	 * Idiot glibc uses extensions to struct dirent for readdir with
957c478bd9Sstevel@tonic-gate 	 * ALTDIRFUNCs. Not that this is documented anywhere but the
967c478bd9Sstevel@tonic-gate 	 * source... Fake an inode number to appease it.
977c478bd9Sstevel@tonic-gate 	 */
987c478bd9Sstevel@tonic-gate 	ret->d_ino = inum++;
997c478bd9Sstevel@tonic-gate 	if (!inum)
1007c478bd9Sstevel@tonic-gate 		inum = 1;
1017c478bd9Sstevel@tonic-gate #endif /* __GNU_LIBRARY__ */
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate 	return(ret);
1047c478bd9Sstevel@tonic-gate }
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate static void
fudge_closedir(struct SFTP_OPENDIR * od)1077c478bd9Sstevel@tonic-gate fudge_closedir(struct SFTP_OPENDIR *od)
1087c478bd9Sstevel@tonic-gate {
1097c478bd9Sstevel@tonic-gate 	free_sftp_dirents(od->dir);
1107c478bd9Sstevel@tonic-gate 	xfree(od);
1117c478bd9Sstevel@tonic-gate }
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate static int
fudge_lstat(const char * path,struct stat * st)1147c478bd9Sstevel@tonic-gate fudge_lstat(const char *path, struct stat *st)
1157c478bd9Sstevel@tonic-gate {
1167c478bd9Sstevel@tonic-gate 	Attrib *a;
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate 	if (!(a = do_lstat(cur.conn, (char *)path, 0)))
1197c478bd9Sstevel@tonic-gate 		return(-1);
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate 	attrib_to_stat(a, st);
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate 	return(0);
1247c478bd9Sstevel@tonic-gate }
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate static int
fudge_stat(const char * path,struct stat * st)1277c478bd9Sstevel@tonic-gate fudge_stat(const char *path, struct stat *st)
1287c478bd9Sstevel@tonic-gate {
1297c478bd9Sstevel@tonic-gate 	Attrib *a;
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate 	if (!(a = do_stat(cur.conn, (char *)path, 0)))
1327c478bd9Sstevel@tonic-gate 		return(-1);
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate 	attrib_to_stat(a, st);
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate 	return(0);
1377c478bd9Sstevel@tonic-gate }
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate int
remote_glob(struct sftp_conn * conn,const char * pattern,int flags,int (* errfunc)(const char *,int),glob_t * pglob)1407c478bd9Sstevel@tonic-gate remote_glob(struct sftp_conn *conn, const char *pattern, int flags,
1417c478bd9Sstevel@tonic-gate     int (*errfunc)(const char *, int), glob_t *pglob)
1427c478bd9Sstevel@tonic-gate {
1437c478bd9Sstevel@tonic-gate 	pglob->gl_opendir = fudge_opendir;
1447c478bd9Sstevel@tonic-gate 	pglob->gl_readdir = (struct dirent *(*)(void *))fudge_readdir;
1457c478bd9Sstevel@tonic-gate 	pglob->gl_closedir = (void (*)(void *))fudge_closedir;
1467c478bd9Sstevel@tonic-gate 	pglob->gl_lstat = fudge_lstat;
1477c478bd9Sstevel@tonic-gate 	pglob->gl_stat = fudge_stat;
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate 	memset(&cur, 0, sizeof(cur));
1507c478bd9Sstevel@tonic-gate 	cur.conn = conn;
1517c478bd9Sstevel@tonic-gate 
152*cf58b254SGary Mills 	return(glob(pattern, flags|GLOB_LIMIT|GLOB_ALTDIRFUNC, errfunc, pglob));
1537c478bd9Sstevel@tonic-gate }
154