Coverage Report

Created: 2025-10-10 07:11

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/net-snmp/snmplib/dir_utils.c
Line
Count
Source
1
/* Portions of this file are subject to the following copyright(s).  See
2
 * the Net-SNMP's COPYING file for more details and other copyrights
3
 * that may apply:
4
 */
5
/*
6
 * Portions of this file are copyrighted by:
7
 * Copyright (C) 2007 Apple, Inc. All rights reserved.
8
 * Use is subject to license terms specified in the COPYING file
9
 * distributed with the Net-SNMP package.
10
 */
11
#include <net-snmp/net-snmp-config.h>
12
#include <net-snmp/net-snmp-features.h>
13
#include <net-snmp/net-snmp-includes.h>
14
15
#include <stdio.h>
16
#include <ctype.h>
17
#ifdef HAVE_STDLIB_H
18
#   include <stdlib.h>
19
#endif
20
#ifdef HAVE_UNISTD_H
21
#   include <unistd.h>
22
#endif
23
#ifdef HAVE_STRING_H
24
#   include <string.h>
25
#else
26
#  include <strings.h>
27
#endif
28
29
#include <sys/types.h>
30
#ifdef HAVE_LIMITS_H
31
#include <limits.h>
32
#endif
33
#ifdef HAVE_SYS_STAT_H
34
#include <sys/stat.h>
35
#endif
36
#ifdef HAVE_DIRENT_H
37
# include <dirent.h>
38
#endif
39
40
#include <errno.h>
41
42
#include <net-snmp/types.h>
43
#include <net-snmp/library/container.h>
44
#include <net-snmp/library/file_utils.h>
45
#include <net-snmp/library/dir_utils.h>
46
47
netsnmp_feature_child_of(container_directory, container_types);
48
#ifdef NETSNMP_FEATURE_REQUIRE_CONTAINER_DIRECTORY
49
netsnmp_feature_require(file_utils);
50
netsnmp_feature_require(container_free_all);
51
#endif /* NETSNMP_FEATURE_REQUIRE_CONTAINER_DIRECTORY */
52
53
#ifndef NETSNMP_FEATURE_REMOVE_CONTAINER_DIRECTORY
54
static int
55
_insert_nsfile( netsnmp_container *c, const char *name, struct stat *stats,
56
                u_int flags);
57
58
/*
59
 * read file names in a directory, with an optional filter
60
 */
61
netsnmp_container *
62
netsnmp_directory_container_read_some(netsnmp_container *user_container,
63
                                      const char *dirname,
64
                                      netsnmp_directory_filter *filter,
65
                                      void *filter_ctx, u_int flags)
66
0
{
67
0
    DIR               *dir;
68
0
    netsnmp_container *container = user_container;
69
0
    struct dirent     *file;
70
0
    char               path[SNMP_MAXPATH];
71
0
    size_t             dirname_len;
72
0
    int                rc;
73
0
    struct stat        statbuf;
74
0
    netsnmp_file       ns_file_tmp;
75
76
0
    if ((flags & NETSNMP_DIR_RELATIVE_PATH) && (flags & NETSNMP_DIR_RECURSE)) {
77
0
        DEBUGMSGTL(("directory:container",
78
0
                    "no support for relative path with recursion\n"));
79
0
        return NULL;
80
0
    }
81
82
0
    DEBUGMSGTL(("directory:container", "reading %s\n", dirname));
83
84
    /*
85
     * create the container, if needed
86
     */
87
0
    if (NULL == container) {
88
0
        if (flags & NETSNMP_DIR_NSFILE) {
89
0
            container = netsnmp_container_find("nsfile_directory_container:"
90
0
                                               "binary_array");
91
0
            if (container) {
92
0
                container->compare = netsnmp_file_compare_name;
93
0
                container->free_item = netsnmp_file_container_free;
94
0
            }
95
0
        }
96
0
        else
97
0
            container = netsnmp_container_find("directory_container:cstring");
98
0
        if (NULL == container)
99
0
            return NULL;
100
0
        container->container_name = strdup(dirname);
101
        /** default to unsorted */
102
0
        if (! (flags & NETSNMP_DIR_SORTED))
103
0
            CONTAINER_SET_OPTIONS(container, CONTAINER_KEY_UNSORTED, rc);
104
        /** default to duplicates not allowed */
105
0
        if (! (flags & NETSNMP_DIR_ALLOW_DUPLICATES))
106
0
           CONTAINER_SET_OPTIONS(container, CONTAINER_KEY_ALLOW_DUPLICATES, rc);
107
0
    }
108
109
0
    dir = opendir(dirname);
110
0
    if (NULL == dir) {
111
0
        DEBUGMSGTL(("directory:container", "  not a dir\n"));
112
0
        if (container != user_container)
113
0
            netsnmp_directory_container_free(container);
114
0
        return NULL;
115
0
    }
116
117
    /** copy dirname into path */
118
0
    if (flags & NETSNMP_DIR_RELATIVE_PATH)
119
0
        dirname_len = 0;
120
0
    else {
121
0
        dirname_len = strlen(dirname);
122
0
        strlcpy(path, dirname, sizeof(path));
123
0
        if ((dirname_len + 2) > sizeof(path)) {
124
            /** not enough room for files */
125
0
            closedir(dir);
126
0
            if (container != user_container)
127
0
                netsnmp_directory_container_free(container);
128
0
            return NULL;
129
0
        }
130
0
        path[dirname_len] = '/';
131
0
        path[++dirname_len] = '\0';
132
0
    }
133
134
    /** iterate over dir */
135
0
    while ((file = readdir(dir))) {
136
0
        if (file->d_name[0] == 0)
137
0
            continue;
138
139
        /** skip '.' and '..' */
140
0
        if ((file->d_name[0] == '.') &&
141
0
            ((file->d_name[1] == 0) ||
142
0
             ((file->d_name[1] == '.') && ((file->d_name[2] == 0)))))
143
0
            continue;
144
145
0
        strlcpy(&path[dirname_len], file->d_name, sizeof(path) - dirname_len);
146
0
        if (NULL != filter) {
147
0
            if (flags & NETSNMP_DIR_NSFILE_STATS) {
148
                /** use local vars for now */
149
0
                if (stat(path, &statbuf) != 0) {
150
0
                    snmp_log(LOG_ERR, "could not stat %s\n", file->d_name);
151
0
                    break;
152
0
                }
153
0
                ns_file_tmp.stats = &statbuf;
154
0
                ns_file_tmp.name = path;
155
0
                rc = (*filter)(&ns_file_tmp, filter_ctx);
156
0
            }
157
0
            else
158
0
                rc = (*filter)(path, filter_ctx);
159
0
            if (0 == rc) {
160
0
                DEBUGMSGTL(("directory:container:filtered", "%s\n",
161
0
                            file->d_name));
162
0
                continue;
163
0
            }
164
0
        }
165
166
0
        DEBUGMSGTL(("directory:container:found", "%s\n", path));
167
0
        if ((flags & NETSNMP_DIR_RECURSE) 
168
0
#if defined(HAVE_STRUCT_DIRENT_D_TYPE) && defined(DT_DIR)
169
0
            && (file->d_type == DT_DIR)
170
#elif defined(S_ISDIR)
171
            && (stat(file->d_name, &statbuf) != 0) && (S_ISDIR(statbuf.st_mode))
172
#endif
173
0
            ) {
174
            /** xxx add the dir as well? not for now.. maybe another flag? */
175
0
            netsnmp_directory_container_read(container, path, flags);
176
0
        }
177
0
        else if (flags & NETSNMP_DIR_NSFILE) {
178
0
            if (_insert_nsfile( container, file->d_name,
179
0
                                filter ? &statbuf : NULL, flags ) < 0)
180
0
                break;
181
0
        }
182
0
        else {
183
0
            char *dup = strdup(path);
184
0
            if (NULL == dup) {
185
0
                snmp_log(LOG_ERR,
186
0
                         "strdup failed while building directory container\n");
187
0
                break;
188
0
            }
189
0
            rc = CONTAINER_INSERT(container, dup);
190
0
            if (-1 == rc ) {
191
0
                DEBUGMSGTL(("directory:container", "  err adding %s\n", path));
192
0
                free(dup);
193
0
            }
194
0
        }
195
0
    } /* while */
196
197
0
    closedir(dir);
198
199
0
    rc = CONTAINER_SIZE(container);
200
0
    DEBUGMSGTL(("directory:container", "  container now has %d items\n", rc));
201
0
    if ((0 == rc) && !(flags & NETSNMP_DIR_EMPTY_OK)) {
202
0
        netsnmp_directory_container_free(container);
203
0
        return NULL;
204
0
    }
205
    
206
0
    return container;
207
0
}
208
209
void
210
netsnmp_directory_container_free(netsnmp_container *container)
211
0
{
212
0
    CONTAINER_FREE_ALL(container, NULL);
213
0
    CONTAINER_FREE(container);
214
0
}
215
216
static int
217
_insert_nsfile( netsnmp_container *c, const char *name, struct stat *stats,
218
                u_int flags)
219
0
{
220
0
    int           rc;
221
0
    netsnmp_file *ns_file = netsnmp_file_new(name, 0, 0, 0);
222
0
    if (NULL == ns_file) {
223
0
        snmp_log(LOG_ERR, "error creating ns_file\n");
224
0
        return -1;
225
0
    }
226
227
0
    if (flags & NETSNMP_DIR_NSFILE_STATS) {
228
0
        ns_file->stats = calloc(1,sizeof(*(ns_file->stats)));
229
0
        if (NULL == ns_file->stats) {
230
0
            snmp_log(LOG_ERR, "error creating stats for ns_file\n");
231
0
            netsnmp_file_release(ns_file);
232
0
            return -1;
233
0
        }
234
    
235
        /** use stats from earlier if we have them */
236
0
        if (stats) {
237
0
            memcpy(ns_file->stats, stats, sizeof(*stats));
238
0
        } else if (stat(ns_file->name, ns_file->stats) < 0) {
239
0
            snmp_log(LOG_ERR, "stat() failed for ns_file\n");
240
0
            netsnmp_file_release(ns_file);
241
0
            return -1;
242
0
        }
243
0
    }
244
245
0
    rc = CONTAINER_INSERT(c, ns_file);
246
0
    if (-1 == rc ) {
247
0
        DEBUGMSGTL(("directory:container", "  err adding %s\n", name));
248
0
        netsnmp_file_release(ns_file);
249
0
    }
250
251
0
    return 0;
252
0
}
253
#else  /* NETSNMP_FEATURE_REMOVE_CONTAINER_DIRECTORY */
254
netsnmp_feature_unused(container_directory);
255
#endif /* NETSNMP_FEATURE_REMOVE_CONTAINER_DIRECTORY */