summaryrefslogtreecommitdiffstats
path: root/filescan-utils.c
blob: 6ac90fe1b8ccf94b60859f38e8b43fb3fe9db028 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/*
 * Copyright (C) 2016-2018 "IoT.bzh"
 * Author Fulup Ar Foll <fulup@iot.bzh>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#define _GNU_SOURCE
#include <ctype.h>
#include <dirent.h>
#include <stdio.h>
#include <string.h>
#include <sys/prctl.h>
#include <sys/stat.h>
#include <time.h>
#include <unistd.h>

#include "filescan-utils.h"

static int ScanDir(char* searchPath, CtlScanDirModeT mode, size_t extentionLen,
    const char* prefix, const char* extention, json_object* responseJ)
{
    int found = 0;
    DIR* dirHandle;
    struct dirent* dirEnt;
    dirHandle = opendir(searchPath);
    if (!dirHandle) {
        if(afbBindingV3root)
            AFB_API_DEBUG_V3(afbBindingV3root, "CONFIG-SCANNING dir=%s not readable", searchPath);
        else
            AFB_API_DEBUG_V3(afbBindingV2data.service.closure, "CONFIG-SCANNING dir=%s not readable", searchPath);
        return 0;
    }

    //AFB_NOTICE ("CONFIG-SCANNING:ctl_listconfig scanning: %s", searchPath);
    while ((dirEnt = readdir(dirHandle)) != NULL) {

        // recursively search embedded directories ignoring any directory starting by '.' or '_'
        if (dirEnt->d_type == DT_DIR && mode == CTL_SCAN_RECURSIVE) {
            char newpath[CONTROL_MAXPATH_LEN];
            if (dirEnt->d_name[0] == '.' || dirEnt->d_name[0] == '_')
                continue;

            strncpy(newpath, searchPath, sizeof(newpath));
            strncat(newpath, "/", sizeof(newpath) - strlen(newpath) - 1);
            strncat(newpath, dirEnt->d_name, sizeof(newpath) - strlen(newpath) - 1);
            found += ScanDir(newpath, mode, extentionLen, prefix, extention, responseJ);
            continue;
        }

        // Unknown type is accepted to support dump filesystems
        if (dirEnt->d_type == DT_REG ||
            dirEnt->d_type == DT_UNKNOWN ||
            dirEnt->d_type == DT_LNK) {

            // check prefix and extention
            ssize_t extentionIdx = strlen(dirEnt->d_name) - extentionLen;
            if (extentionIdx <= 0)
                continue;
            if (prefix && strncasecmp(dirEnt->d_name, prefix, strlen(prefix)))
                continue;
            if (extention && strcasecmp(extention, &dirEnt->d_name[extentionIdx]))
                continue;

            struct json_object* pathJ = json_object_new_object();
            json_object_object_add(pathJ, "fullpath", json_object_new_string(searchPath));
            json_object_object_add(pathJ, "filename", json_object_new_string(dirEnt->d_name));
            json_object_array_add(responseJ, pathJ);
            found++;
        }
    }
    closedir(dirHandle);
    return found;
}

// List Avaliable Configuration Files
json_object* ScanForConfig(const char* searchPath, CtlScanDirModeT mode, const char* prefix, const char* extention)
{
    json_object* responseJ = json_object_new_array();
    char* dirPath;
    char* dirList;
    size_t extentionLen = 0;
    int count = 0;

    if (!searchPath)
        return responseJ;

    dirList = strdup(searchPath);

    if (extention)
        extentionLen = strlen(extention);

    // loop recursively on dir
    for (dirPath = strtok(dirList, ":"); dirPath && *dirPath; dirPath = strtok(NULL, ":")) {
        count += ScanDir(dirPath, mode, extentionLen, prefix, extention, responseJ);
    }
    if (count == 0) {
        json_object_put(responseJ);
        free(dirList);
        return NULL;
    }
    free(dirList);
    return (responseJ);
}

const char* GetMiddleName(const char* name)
{
    char* fullname = strdup(name);

    for (int idx = 0; fullname[idx] != '\0'; idx++) {
        if (fullname[idx] == '-') {
            int start;
            start = idx + 1;
            for (int jdx = start;; jdx++) {
                if (fullname[jdx] == '-' || fullname[jdx] == '@' || fullname[jdx] == '.' || fullname[jdx] == '\0') {
                    fullname[jdx] = '\0';
                    return &fullname[start];
                }
            }
            break;
        }
    }
    return "";
}

const char* GetBinderName()
{
    static char* binderName = NULL;

    if (binderName)
        return binderName;

    binderName = getenv("AFB_BINDER_NAME");
    if (!binderName) {
        static char psName[17];
        // retrieve binder name from process name afb-name-trailer
        prctl(PR_GET_NAME, psName, NULL, NULL, NULL);
        binderName = (char*)GetMiddleName(psName);
    }

    return binderName;
}

char *GetAFBRootDirPathUsingFd(int fd)
{
    // A file description should not be greater than 999.999.999
    char fd_link[CONTROL_MAXPATH_LEN];
    char retdir[CONTROL_MAXPATH_LEN];
    ssize_t len;

    sprintf(fd_link, "/proc/self/fd/%d", fd);

    if ((len = readlink(fd_link, retdir, sizeof(retdir) - 1)) == -1) {
        perror("lstat");
        strncpy(retdir, "/tmp", CONTROL_MAXPATH_LEN - 1);
    } else {
        retdir[len] = '\0';
    }

    return strndup(retdir, sizeof(retdir));
}

char *GetAFBRootDirPath(afb_api_t apiHandle)
{
    int fd;

    fd = afb_api_rootdir_get_fd(apiHandle);

    return GetAFBRootDirPathUsingFd(fd);
}

char *GetRunningBindingDirPath(afb_api_t apiHandle)
{
    int ret;

    char *lastSlashInPath, *bindingDirectoryPath;
    const char *bindingPath;

    json_object *settingsJ, *bindingPathJ = NULL;

    settingsJ = afb_api_settings(apiHandle);
    if(!settingsJ)
        return NULL;

    ret = json_object_object_get_ex(settingsJ, "binding-path", &bindingPathJ);
    if(!ret || !bindingPathJ || !json_object_is_type(bindingPathJ, json_type_string))
        return NULL;

    bindingPath = json_object_get_string(bindingPathJ);

    lastSlashInPath = rindex(bindingPath, '/');
    if(!lastSlashInPath)
        return NULL;

    bindingDirectoryPath = strndup(bindingPath, lastSlashInPath - bindingPath);

    return bindingDirectoryPath;
}


/**
 * @brief Takes an input string and makes it upper case. The output buffer
 * should be able to contains the whole input string else it will be truncated
 * at the output_size.
 *
 * @param input the input string to transform in an upper case version
 * @param output the upper case version of the input string
 * @param output_size the output buffer size, if size < to strlen of input then
 * the output will be truncated.
 *
 * @return size_t the string length of the resulting output variable, which should
 * be equal to the size of input string length.
 */
static size_t toUpperString(const char *input, char *output, size_t output_size) {
    int i = 0;

    if(input && output) {
        while(input[i] && i <= output_size) {
            output[i] = (char)toupper(input[i]);
            i++;
        }
        output[i] = '\0';
        return strlen(output);
    }

   return 0;
}

const char *getEnvDirList(const char *prefix, const char *suffix) {
    size_t lenSimple = 0, lenFull = 0, plen = 0, slen = 0, blen = 0;
    char *envConfigPathSimple = NULL, *envConfigPathFull = NULL;
    char *upperPrefix = NULL, *upperSuffix = NULL, *upperBinderName = NULL;
    char *envDirList = NULL;
    const char *binderName = GetBinderName(), *envDirListSimple = NULL;
    const char *envDirListFull = NULL;

    if(! prefix || ! suffix)
        return NULL;

    plen = strlen(prefix);
    upperPrefix = alloca(plen + 1);

    slen = strlen(suffix);
    upperSuffix = alloca(slen + 1);

    blen = strlen(binderName);
    upperBinderName = alloca(blen + 1);

    lenSimple = plen + slen + 1;
    envConfigPathSimple = alloca(lenSimple + 1);

    lenFull = slen + blen + slen + 2;
    envConfigPathFull = alloca(lenFull + 1);

    if(toUpperString(prefix, upperPrefix, plen) != plen ||
       toUpperString(suffix, upperSuffix, slen) != slen ||
       toUpperString(binderName, upperBinderName, blen) != blen ||
       (snprintf(envConfigPathSimple, lenSimple + 1, "%s_%s", upperPrefix, upperSuffix) >= lenSimple + 1) ||
       (snprintf(envConfigPathFull, lenFull + 1, "%s_%s_%s", upperPrefix, upperBinderName, upperSuffix) >= lenFull + 1)) {
        return NULL;
    }

    envDirListFull = getenv(envConfigPathFull);
    envDirListSimple = getenv(envConfigPathSimple);
    if(envDirListSimple && envDirListFull) {
        lenFull = strlen(envDirListSimple) + strlen(envDirListFull) + 1;
        envDirList = malloc(lenFull + 1);
        snprintf(envDirList, lenFull + 1, "%s:%s", envDirListFull, envDirListSimple);
    }
    else {
        envDirList = envDirListSimple ? strdup(envDirListSimple) : envDirListFull ? strdup(envDirListFull) : NULL;
    }

    return envDirList;
}