1*a629fefcSchristos /* $NetBSD: sftp-glob.c,v 1.15 2023/10/25 20:19:57 christos Exp $ */
2*a629fefcSchristos /* $OpenBSD: sftp-glob.c,v 1.33 2023/09/10 23:12:32 djm Exp $ */
3*a629fefcSchristos
4ca32bd8dSchristos /*
5ca32bd8dSchristos * Copyright (c) 2001-2004 Damien Miller <djm@openbsd.org>
6ca32bd8dSchristos *
7ca32bd8dSchristos * Permission to use, copy, modify, and distribute this software for any
8ca32bd8dSchristos * purpose with or without fee is hereby granted, provided that the above
9ca32bd8dSchristos * copyright notice and this permission notice appear in all copies.
10ca32bd8dSchristos *
11ca32bd8dSchristos * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12ca32bd8dSchristos * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13ca32bd8dSchristos * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14ca32bd8dSchristos * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15ca32bd8dSchristos * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16ca32bd8dSchristos * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17ca32bd8dSchristos * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18ca32bd8dSchristos */
19ca32bd8dSchristos
20313c6c94Schristos #include "includes.h"
21*a629fefcSchristos __RCSID("$NetBSD: sftp-glob.c,v 1.15 2023/10/25 20:19:57 christos Exp $");
22ca32bd8dSchristos #include <sys/types.h>
23ca32bd8dSchristos #include <sys/stat.h>
24ca32bd8dSchristos
25ca32bd8dSchristos #include <dirent.h>
26ca32bd8dSchristos #include <glob.h>
278a4530f9Schristos #include <stdlib.h>
28ca32bd8dSchristos #include <string.h>
29ed75d7a8Schristos #include <stdarg.h>
30ca32bd8dSchristos
31ca32bd8dSchristos #include "xmalloc.h"
32ca32bd8dSchristos #include "sftp.h"
33ca32bd8dSchristos #include "sftp-common.h"
34ca32bd8dSchristos #include "sftp-client.h"
35ca32bd8dSchristos
36*a629fefcSchristos int sftp_glob(struct sftp_conn *, const char *, int,
37ca32bd8dSchristos int (*)(const char *, int), glob_t *);
38ca32bd8dSchristos
39ca32bd8dSchristos struct SFTP_OPENDIR {
40ca32bd8dSchristos SFTP_DIRENT **dir;
41ca32bd8dSchristos int offset;
42ca32bd8dSchristos };
43ca32bd8dSchristos
44ca32bd8dSchristos static struct {
45ca32bd8dSchristos struct sftp_conn *conn;
46ca32bd8dSchristos } cur;
47ca32bd8dSchristos
48ca32bd8dSchristos static void *
fudge_opendir(const char * path)49ca32bd8dSchristos fudge_opendir(const char *path)
50ca32bd8dSchristos {
51ca32bd8dSchristos struct SFTP_OPENDIR *r;
52ca32bd8dSchristos
5300a838c4Schristos r = xcalloc(1, sizeof(*r));
54ca32bd8dSchristos
55*a629fefcSchristos if (sftp_readdir(cur.conn, __UNCONST(path), &r->dir)) {
5600a838c4Schristos free(r);
57ca32bd8dSchristos return(NULL);
58ca32bd8dSchristos }
59ca32bd8dSchristos
60ca32bd8dSchristos r->offset = 0;
61ca32bd8dSchristos
62ca32bd8dSchristos return((void *)r);
63ca32bd8dSchristos }
64ca32bd8dSchristos
65ca32bd8dSchristos static struct dirent *
fudge_readdir(struct SFTP_OPENDIR * od)66ca32bd8dSchristos fudge_readdir(struct SFTP_OPENDIR *od)
67ca32bd8dSchristos {
68ca32bd8dSchristos static struct dirent ret;
69ca32bd8dSchristos
70ca32bd8dSchristos if (od->dir[od->offset] == NULL)
71ca32bd8dSchristos return(NULL);
72ca32bd8dSchristos
73ca32bd8dSchristos memset(&ret, 0, sizeof(ret));
74ca32bd8dSchristos strlcpy(ret.d_name, od->dir[od->offset++]->filename,
75ca32bd8dSchristos sizeof(ret.d_name));
76ca32bd8dSchristos
77ca32bd8dSchristos return(&ret);
78ca32bd8dSchristos }
79ca32bd8dSchristos
80ca32bd8dSchristos static void
fudge_closedir(struct SFTP_OPENDIR * od)81ca32bd8dSchristos fudge_closedir(struct SFTP_OPENDIR *od)
82ca32bd8dSchristos {
83*a629fefcSchristos sftp_free_dirents(od->dir);
8400a838c4Schristos free(od);
85ca32bd8dSchristos }
86ca32bd8dSchristos
87ca32bd8dSchristos static int
fudge_lstat(const char * path,struct stat * st)88ca32bd8dSchristos fudge_lstat(const char *path, struct stat *st)
89ca32bd8dSchristos {
90*a629fefcSchristos Attrib a;
91ca32bd8dSchristos
92*a629fefcSchristos if (sftp_lstat(cur.conn, path, 1, &a) != 0)
93*a629fefcSchristos return -1;
94ca32bd8dSchristos
95*a629fefcSchristos attrib_to_stat(&a, st);
96ca32bd8dSchristos
97*a629fefcSchristos return 0;
98ca32bd8dSchristos }
99ca32bd8dSchristos
100ca32bd8dSchristos static int
fudge_stat(const char * path,struct stat * st)101ca32bd8dSchristos fudge_stat(const char *path, struct stat *st)
102ca32bd8dSchristos {
103*a629fefcSchristos Attrib a;
104ca32bd8dSchristos
105*a629fefcSchristos if (sftp_stat(cur.conn, path, 1, &a) != 0)
106*a629fefcSchristos return -1;
107ca32bd8dSchristos
108*a629fefcSchristos attrib_to_stat(&a, st);
109ca32bd8dSchristos
110ca32bd8dSchristos return(0);
111ca32bd8dSchristos }
112ca32bd8dSchristos
113ca32bd8dSchristos int
sftp_glob(struct sftp_conn * conn,const char * pattern,int flags,int (* errfunc)(const char *,int),glob_t * pglob)114*a629fefcSchristos sftp_glob(struct sftp_conn *conn, const char *pattern, int flags,
115ca32bd8dSchristos int (*errfunc)(const char *, int), glob_t *pglob)
116ca32bd8dSchristos {
117b1066cf3Schristos int r;
118b1066cf3Schristos size_t l;
119b1066cf3Schristos char *s;
120b1066cf3Schristos struct stat sb;
121b1066cf3Schristos
122ca32bd8dSchristos pglob->gl_opendir = fudge_opendir;
123ca32bd8dSchristos pglob->gl_readdir = (struct dirent *(*)(void *))fudge_readdir;
124ca32bd8dSchristos pglob->gl_closedir = (void (*)(void *))fudge_closedir;
125ca32bd8dSchristos pglob->gl_lstat = fudge_lstat;
126ca32bd8dSchristos pglob->gl_stat = fudge_stat;
127ca32bd8dSchristos
128ca32bd8dSchristos memset(&cur, 0, sizeof(cur));
129ca32bd8dSchristos cur.conn = conn;
130ca32bd8dSchristos
131b1066cf3Schristos if ((r = glob(pattern, flags | GLOB_ALTDIRFUNC|GLOB_LIMIT, errfunc, pglob)) != 0)
132b1066cf3Schristos return r;
133b1066cf3Schristos /*
134b1066cf3Schristos * When both GLOB_NOCHECK and GLOB_MARK are active, a single gl_pathv
135b1066cf3Schristos * entry has been returned and that entry has not already been marked,
136b1066cf3Schristos * then check whether it needs a '/' appended as a directory mark.
137b1066cf3Schristos *
138b1066cf3Schristos * This ensures that a NOCHECK result is annotated as a directory.
139b1066cf3Schristos * The glob(3) spec doesn't promise to mark NOCHECK entries, but doing
140b1066cf3Schristos * it simplifies our callers (sftp/scp) considerably.
141b1066cf3Schristos *
142b1066cf3Schristos * XXX doesn't try to handle gl_offs.
143b1066cf3Schristos */
144b1066cf3Schristos if ((flags & (GLOB_NOCHECK|GLOB_MARK)) == (GLOB_NOCHECK|GLOB_MARK) &&
145b1066cf3Schristos pglob->gl_matchc == 0 && pglob->gl_offs == 0 &&
146b1066cf3Schristos pglob->gl_pathc == 1 && (s = pglob->gl_pathv[0]) != NULL &&
147b1066cf3Schristos (l = strlen(s)) > 0 && s[l-1] != '/') {
148b1066cf3Schristos if (fudge_stat(s, &sb) == 0 && S_ISDIR(sb.st_mode)) {
149b1066cf3Schristos /* NOCHECK on a directory; annotate */
150b1066cf3Schristos if ((s = realloc(s, l + 2)) != NULL) {
151b1066cf3Schristos memcpy(s + l, "/", 2);
152b1066cf3Schristos pglob->gl_pathv[0] = s;
153b1066cf3Schristos }
154b1066cf3Schristos }
155b1066cf3Schristos }
156b1066cf3Schristos return 0;
157ca32bd8dSchristos }
158