summaryrefslogtreecommitdiffstats
path: root/cpukit/libcsupport/src/sup_fs_eval_path.c
blob: 43d7bbbff86cb86342c751643e7b73ecadda8585 (plain) (blame)
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/* SPDX-License-Identifier: BSD-2-Clause */

/**
 *  @file
 *
 *  @brief RTEMS File Sysyem Path Eval Support
 *  @ingroup LibIOInternal
 */

/*
 * Copyright (c) 2012 embedded brains GmbH & Co. KG
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <rtems/libio_.h>

#include <string.h>

static size_t get_parentpathlen(const char *path, size_t pathlen)
{
  while (pathlen > 0) {
    size_t i = pathlen - 1;

    if (rtems_filesystem_is_delimiter(path [i])) {
      return pathlen;
    }

    pathlen = i;
  }

  return 0;
}

static void set_startloc(
  rtems_filesystem_eval_path_context_t *ctx,
  rtems_filesystem_global_location_t *const *global_root_ptr,
  rtems_filesystem_global_location_t *const *global_current_ptr
)
{
  if (ctx->pathlen > 0) {
    char c = ctx->path [0];

    ctx->rootloc = rtems_filesystem_global_location_obtain(global_root_ptr);

    if (rtems_filesystem_is_delimiter(c)) {
      ++ctx->path;
      --ctx->pathlen;
      ctx->startloc = rtems_filesystem_global_location_obtain(
        &ctx->rootloc
      );
    } else {
      ctx->startloc = rtems_filesystem_global_location_obtain(
        global_current_ptr
      );
    }
  } else {
    ctx->rootloc = rtems_filesystem_global_location_obtain_null();
    ctx->startloc = rtems_filesystem_global_location_obtain_null();
    errno = ENOENT;
  }
}

static void check_access(
  rtems_filesystem_eval_path_context_t *ctx,
  int eval_flags
)
{
  const rtems_filesystem_location_info_t *currentloc = &ctx->currentloc;
  const rtems_filesystem_mount_table_entry_t *mt_entry = currentloc->mt_entry;

  if ((eval_flags & RTEMS_FS_PERMS_WRITE) == 0 || mt_entry->writeable) {
    struct stat st;
    int rv;

    st.st_mode = 0;
    st.st_uid = 0;
    st.st_gid = 0;
    rv = (*currentloc->handlers->fstat_h)(currentloc, &st);
    if (rv == 0) {
      bool access_ok = rtems_filesystem_check_access(
        eval_flags,
        st.st_mode,
        st.st_uid,
        st.st_gid
      );

      if (!access_ok) {
        rtems_filesystem_eval_path_error(ctx, EACCES);
      }
    } else {
      rtems_filesystem_eval_path_error(ctx, 0);
    }
  } else {
    rtems_filesystem_eval_path_error(ctx, EROFS);
  }
}

void rtems_filesystem_eval_path_continue(
  rtems_filesystem_eval_path_context_t *ctx
)
{
  int eval_flags;

  while (ctx->pathlen > 0) {
    (*ctx->currentloc.mt_entry->ops->eval_path_h)(ctx);
  }

  eval_flags = rtems_filesystem_eval_path_get_flags(ctx);
  if (rtems_filesystem_eval_path_has_token(ctx)) {
    bool make = (eval_flags & RTEMS_FS_MAKE) != 0;

    if (make) {
      check_access(ctx, RTEMS_FS_PERMS_WRITE);
    } else {
      rtems_filesystem_eval_path_error(ctx, ENOENT);
    }
  } else {
    bool exclusive = (eval_flags & RTEMS_FS_EXCLUSIVE) != 0;

    if (!exclusive) {
      check_access(ctx, ctx->flags);
    } else {
      rtems_filesystem_eval_path_error(ctx, EEXIST);
    }
  }
}

rtems_filesystem_location_info_t *
rtems_filesystem_eval_path_start_with_root_and_current(
  rtems_filesystem_eval_path_context_t *ctx,
  const char *path,
  size_t pathlen,
  int eval_flags,
  rtems_filesystem_global_location_t *const *global_root_ptr,
  rtems_filesystem_global_location_t *const *global_current_ptr
)
{
  memset(ctx, 0, sizeof(*ctx));

  ctx->path = path;
  ctx->pathlen = pathlen;
  ctx->flags = eval_flags;

  set_startloc(ctx, global_root_ptr, global_current_ptr);

  rtems_filesystem_instance_lock(&ctx->startloc->location);

  rtems_filesystem_location_clone(
    &ctx->currentloc,
    &ctx->startloc->location
  );

  rtems_filesystem_eval_path_continue(ctx);

  return &ctx->currentloc;
}

rtems_filesystem_location_info_t *
rtems_filesystem_eval_path_start(
  rtems_filesystem_eval_path_context_t *ctx,
  const char *path,
  int eval_flags
)
{
  return rtems_filesystem_eval_path_start_with_root_and_current(
    ctx,
    path,
    strlen(path),
    eval_flags,
    &rtems_filesystem_root,
    &rtems_filesystem_current
  );
}

rtems_filesystem_location_info_t *
rtems_filesystem_eval_path_start_with_parent(
  rtems_filesystem_eval_path_context_t *ctx,
  const char *path,
  int eval_flags,
  rtems_filesystem_location_info_t *parentloc,
  int parent_eval_flags
)
{
  size_t pathlen = strlen(path);
  const char *parentpath = path;
  size_t parentpathlen = get_parentpathlen(path, pathlen);
  const char *name = NULL;
  size_t namelen = 0;
  const rtems_filesystem_location_info_t *currentloc = NULL;

  if (pathlen > 0) {
    if (parentpathlen == 0) {
      parentpath = ".";
      parentpathlen = 1;
      name = path;
      namelen = pathlen;
    } else {
      name = path + parentpathlen;
      namelen = pathlen - parentpathlen;
    }
  }

  currentloc = rtems_filesystem_eval_path_start_with_root_and_current(
    ctx,
    parentpath,
    parentpathlen,
    parent_eval_flags,
    &rtems_filesystem_root,
    &rtems_filesystem_current
  );

  rtems_filesystem_location_clone(parentloc, currentloc);

  ctx->path = name;
  ctx->pathlen = namelen;
  ctx->flags = eval_flags;

  rtems_filesystem_eval_path_continue(ctx);

  return &ctx->currentloc;
}

void rtems_filesystem_eval_path_recursive(
  rtems_filesystem_eval_path_context_t *ctx,
  const char *path,
  size_t pathlen
)
{
  if (pathlen > 0) {
    if (ctx->recursionlevel < RTEMS_FILESYSTEM_SYMLOOP_MAX) {
      const char *saved_path = ctx->path;
      size_t saved_pathlen = ctx->pathlen;

      if (rtems_filesystem_is_delimiter(path [0])) {
        rtems_filesystem_eval_path_restart(ctx, &ctx->rootloc);
      }

      ctx->path = path;
      ctx->pathlen = pathlen;

      ++ctx->recursionlevel;
      while (ctx->pathlen > 0) {
        (*ctx->currentloc.mt_entry->ops->eval_path_h)(ctx);
      }
      --ctx->recursionlevel;

      ctx->path = saved_path;
      ctx->pathlen = saved_pathlen;
    } else {
      rtems_filesystem_eval_path_error(ctx, ELOOP);
    }
  } else {
    rtems_filesystem_eval_path_error(ctx, ENOENT);
  }
}

void rtems_filesystem_eval_path_error(
  rtems_filesystem_eval_path_context_t *ctx,
  int eno
)
{
  ctx->path = NULL;
  ctx->pathlen = 0;
  ctx->token = NULL;
  ctx->tokenlen = 0;

  if (!rtems_filesystem_location_is_null(&ctx->currentloc)) {
    if (eno != 0) {
      errno = eno;
    }

    rtems_filesystem_location_detach(&ctx->currentloc);
  }
}

static void free_location(rtems_filesystem_location_info_t *loc)
{
  rtems_filesystem_mt_entry_declare_lock_context(lock_context);

  (*loc->mt_entry->ops->freenod_h)(loc);

  rtems_filesystem_mt_entry_lock(lock_context);
  rtems_chain_extract_unprotected(&loc->mt_entry_node);
  rtems_filesystem_mt_entry_unlock(lock_context);
}

void rtems_filesystem_eval_path_cleanup(
  rtems_filesystem_eval_path_context_t *ctx
)
{
  free_location(&ctx->currentloc);
  rtems_filesystem_instance_unlock(&ctx->startloc->location);
  rtems_filesystem_global_location_release(ctx->startloc, false);
  rtems_filesystem_global_location_release(ctx->rootloc, false);
}

void rtems_filesystem_eval_path_cleanup_with_parent(
  rtems_filesystem_eval_path_context_t *ctx,
  rtems_filesystem_location_info_t *parentloc
)
{
  free_location(parentloc);
  rtems_filesystem_eval_path_cleanup(ctx);
}

void rtems_filesystem_eval_path_restart(
  rtems_filesystem_eval_path_context_t *ctx,
  rtems_filesystem_global_location_t **newstartloc_ptr
)
{
  free_location(&ctx->currentloc);
  rtems_filesystem_instance_unlock(&ctx->startloc->location);
  rtems_filesystem_global_location_assign(
    &ctx->startloc,
    rtems_filesystem_global_location_obtain(newstartloc_ptr)
  );
  rtems_filesystem_instance_lock(&ctx->startloc->location);
  rtems_filesystem_location_clone(&ctx->currentloc, &ctx->startloc->location);
}