[BACK]Return to sftp-glob.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / ssh

Annotation of src/usr.bin/ssh/sftp-glob.c, Revision 1.32

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