source: rtems/c/src/libfs/src/imfs/imfs_eval.c @ 70d689a

4.104.114.84.95
Last change on this file since 70d689a was d6b1d73, checked in by Joel Sherrill <joel.sherrill@…>, on 01/22/01 at 14:05:14

2001-01-22 Ralf Corsepius <corsepiu@…>

  • configure.in: Add src/imfs/config.h
  • src/imfs/Makefile.am: Add INCLUDES += -I. to pickup config.h
  • src/imfs/.cvsignore: Add config.h and stamp-h
  • src/imfs/*.c: Add config.h support.
  • Property mode set to 100644
File size: 14.9 KB
Line 
1/*
2 *  Evaluation IMFS Node Support Routines
3 *
4 *  COPYRIGHT (c) 1989-1999.
5 *  On-Line Applications Research Corporation (OAR).
6 *
7 *  The license and distribution terms for this file may be
8 *  found in the file LICENSE in this distribution or at
9 *  http://www.OARcorp.com/rtems/license.html.
10 *
11 *  $Id$
12 */
13
14#if HAVE_CONFIG_H
15#include "config.h"
16#endif
17
18#include <sys/types.h>
19#include <sys/stat.h>
20#include <fcntl.h>
21#include <unistd.h>
22#include <errno.h>
23#include <stdlib.h>
24#include <assert.h>
25
26#include "imfs.h"
27#include <rtems/libio_.h>
28
29#define RTEMS_LIBIO_PERMS_RX (RTEMS_LIBIO_PERMS_SEARCH | RTEMS_LIBIO_PERMS_READ)
30#define RTEMS_LIBIO_PERMS_WX (RTEMS_LIBIO_PERMS_SEARCH | RTEMS_LIBIO_PERMS_WRITE)
31
32#define MAXSYMLINK 5
33
34int IMFS_Set_handlers(
35  rtems_filesystem_location_info_t   *loc
36)
37{
38  IMFS_jnode_t    *node = loc->node_access;
39  IMFS_fs_info_t  *fs_info;
40
41  fs_info = loc->mt_entry->fs_info;
42  switch( node->type ) {
43    case IMFS_DIRECTORY:
44      loc->handlers = fs_info->directory_handlers;
45      break;
46    case IMFS_DEVICE:
47      loc->handlers = &IMFS_device_handlers;
48      break;
49    case IMFS_SYM_LINK:
50    case IMFS_HARD_LINK:
51      loc->handlers = &IMFS_link_handlers;
52      break;
53    case IMFS_LINEAR_FILE:
54      loc->handlers = fs_info->linearfile_handlers;
55      break;
56    case IMFS_MEMORY_FILE:
57      loc->handlers = fs_info->memfile_handlers;
58      break;
59  }
60
61  return 0;
62}
63
64/*
65 *  IMFS_evaluate_permission
66 *
67 *  The following routine evaluates that we have permission
68 *  to do flags on the node.
69 */
70
71int IMFS_evaluate_permission(
72  rtems_filesystem_location_info_t  *node,
73  int                                flags
74)
75{
76  uid_t         st_uid;
77  gid_t         st_gid;
78  IMFS_jnode_t *jnode;
79  int           flags_to_test;
80
81  if ( !rtems_libio_is_valid_perms( flags ) ) {
82    assert( 0 );
83    set_errno_and_return_minus_one( EIO );   
84  }
85
86  jnode = node->node_access;
87
88#if defined(RTEMS_POSIX_API)
89  st_uid = geteuid();
90  st_gid = getegid();
91#else
92  st_uid = jnode->st_uid;
93  st_gid = jnode->st_gid;
94#endif
95 
96  /*
97   * Check if I am owner or a group member or someone else.
98   */
99 
100  flags_to_test = flags;
101
102  if ( st_uid == jnode->st_uid )
103    flags_to_test <<= 6;
104  else if ( st_gid == jnode->st_gid )
105    flags_to_test <<= 3;
106  else
107    /* must be other - do nothing */;
108
109  /*
110   * If all of the flags are set we have permission
111   * to do this.
112   */
113  if ( ( flags_to_test & jnode->st_mode) == flags_to_test )
114    return 1;
115
116  return 0;
117}
118
119/*
120 *  IMFS_evaluate_hard_link
121 *
122 *  The following routine evaluates a hardlink to the actual node.
123 */
124
125int IMFS_evaluate_hard_link(
126  rtems_filesystem_location_info_t  *node,   /* IN/OUT */
127  int                                flags   /* IN     */
128)
129{
130  IMFS_jnode_t                     *jnode  = node->node_access;
131  int                               result = 0;
132
133  /*
134   * Check for things that should never happen.
135   */
136
137  if ( jnode->type != IMFS_HARD_LINK )
138    rtems_fatal_error_occurred (0xABCD0000);
139
140  /*
141   * Set the hard link value and the handlers.
142   */
143
144  node->node_access = jnode->info.hard_link.link_node;
145 
146  IMFS_Set_handlers( node );
147
148  /*
149   * Verify we have the correct permissions for this node.
150   */
151
152  if ( !IMFS_evaluate_permission( node, flags ) )
153    set_errno_and_return_minus_one( EACCES );
154
155  return result;
156}
157
158
159/*
160 *  IMFS_evaluate_sym_link
161 *
162 *  The following routine evaluates a symbolic link to the actual node.
163 */
164
165int IMFS_evaluate_sym_link(
166  rtems_filesystem_location_info_t  *node,   /* IN/OUT */
167  int                                flags   /* IN     */
168)
169{
170  IMFS_jnode_t                     *jnode  = node->node_access;
171  int                               result = 0;
172  int                               i;
173
174  /*
175   * Check for things that should never happen.
176   */
177
178  if ( jnode->type != IMFS_SYM_LINK )
179    rtems_fatal_error_occurred (0xABCD0000);
180
181  if ( !jnode->Parent )
182    rtems_fatal_error_occurred( 0xBAD00000 );
183
184
185  /*
186   * Move the node_access to either the symbolic links parent or
187   * root depending on the symbolic links path.
188   */
189
190  node->node_access = jnode->Parent;
191
192  rtems_filesystem_get_sym_start_loc(
193    jnode->info.sym_link.name,
194    &i,
195    node
196  );
197
198  /*
199   * Use eval path to evaluate the path of the symbolic link.
200   */
201
202  result = IMFS_eval_path( 
203    &jnode->info.sym_link.name[i],
204    flags,
205    node
206  );
207
208  IMFS_Set_handlers( node );
209
210  /*
211   * Verify we have the correct permissions for this node.
212   */
213
214  if ( !IMFS_evaluate_permission( node, flags ) )
215    set_errno_and_return_minus_one( EACCES );
216
217  return result;
218}
219
220/*
221 *  IMFS_evaluate_link
222 *
223 *  The following routine returns the real node pointed to by a link.
224 */
225
226int IMFS_evaluate_link(
227  rtems_filesystem_location_info_t  *node,   /* IN/OUT */
228  int                                flags   /* IN     */
229)
230{
231  IMFS_jnode_t                     *jnode;
232  int                               result = 0;
233
234  do {
235    jnode  = node->node_access;
236
237    /*
238     * Increment and check the link counter.
239     */
240
241    rtems_filesystem_link_counts ++;
242    if ( rtems_filesystem_link_counts > MAXSYMLINK ) {
243      rtems_filesystem_link_counts = 0;
244      set_errno_and_return_minus_one( ELOOP );
245    }
246
247    /*
248     *  Follow the Link node.
249     */
250
251    if ( jnode->type == IMFS_HARD_LINK )
252      result = IMFS_evaluate_hard_link( node, flags );
253
254    else if (jnode->type == IMFS_SYM_LINK )
255      result = IMFS_evaluate_sym_link( node, flags );
256
257  } while ( ( result == 0 ) && ( ( jnode->type == IMFS_SYM_LINK  ) ||
258                                 ( jnode->type == IMFS_HARD_LINK ) ) );
259
260  /*
261   * Clear link counter.
262   */
263
264  rtems_filesystem_link_counts = 0;
265
266  return result;
267
268
269
270/*
271 *  IMFS_evaluate_for_make
272 *
273 *  The following routine evaluate path for a new node to be created.
274 *  pathloc is returned with a pointer to the parent of the new node.
275 *  name is returned with a pointer to the first character in the
276 *  new node name.  The parent node is verified to be a directory.
277 */
278
279int IMFS_evaluate_for_make(
280   const char                         *path,       /* IN     */
281   rtems_filesystem_location_info_t   *pathloc,    /* IN/OUT */
282   const char                        **name        /* OUT    */
283)
284{
285  int                                 i = 0;
286  int                                 len;
287  IMFS_token_types                    type;
288  char                                token[ IMFS_NAME_MAX + 1 ];
289  rtems_filesystem_location_info_t    newloc;
290  IMFS_jnode_t                       *node;
291  int                                 done = 0;
292  int                                 result;
293 
294  /*
295   * This was filled in by the caller and is valid in the
296   * mount table.
297   */
298  node = pathloc->node_access;
299
300  /*
301   *  Evaluate all tokens until we are done or an error occurs.
302   */
303
304  while( !done ) {
305
306    type = IMFS_get_token( &path[i], token, &len );
307    i +=  len;
308   
309    if ( !pathloc->node_access )
310      set_errno_and_return_minus_one( ENOENT );
311
312    /*
313     * I cannot move out of this directory without execute permission.
314     */
315
316    if ( type != IMFS_NO_MORE_PATH )
317      if ( node->type == IMFS_DIRECTORY )
318        if ( !IMFS_evaluate_permission( pathloc, RTEMS_LIBIO_PERMS_SEARCH ) )
319           set_errno_and_return_minus_one( EACCES );
320
321    node = pathloc->node_access;
322
323    switch( type ) {
324
325      case IMFS_UP_DIR:
326
327        /*
328         * Am I at the root of this mounted filesystem?
329         */
330
331        if (pathloc->node_access == pathloc->mt_entry->mt_fs_root.node_access){
332
333          /*
334           *  Am I at the root of all filesystems?
335           */
336
337          if ( pathloc->node_access == rtems_filesystem_root.node_access ) {
338            break;
339
340          } else {
341            newloc = pathloc->mt_entry->mt_point_node;
342            *pathloc = newloc;
343            return (*pathloc->ops->evalformake_h)( &path[i-len], pathloc, name );
344          }
345        } else {
346
347          if ( !node->Parent )
348            set_errno_and_return_minus_one( ENOENT );
349
350          node = node->Parent;
351        }
352
353        pathloc->node_access = node;
354        break;
355
356      case IMFS_NAME:
357
358        if ( node->type == IMFS_HARD_LINK ) {
359
360          result = IMFS_evaluate_link( pathloc, 0 );
361          if ( result == -1 )
362            return -1;
363
364        } else if ( node->type == IMFS_SYM_LINK ) {
365
366          result = IMFS_evaluate_link( pathloc, 0 );
367
368          if ( result == -1 )
369            return -1;
370        }
371
372        node = pathloc->node_access;
373        if ( !node )
374          set_errno_and_return_minus_one( ENOTDIR );
375
376        /*
377         * Only a directory can be decended into.
378         */
379
380        if ( node->type != IMFS_DIRECTORY )
381          set_errno_and_return_minus_one( ENOTDIR );
382
383        /*
384         * If we are at a node that is a mount point. Set loc to the
385         * new fs root node and let them finish evaluating the path.
386         */
387
388        if ( node->info.directory.mt_fs != NULL ) {
389          newloc  = node->info.directory.mt_fs->mt_fs_root;
390          *pathloc = newloc;
391          return (*pathloc->ops->evalformake_h)( &path[i-len], pathloc, name );
392        }
393
394        /*
395         * Otherwise find the token name in the present location.
396         */
397
398        node = IMFS_find_match_in_dir( node, token );
399
400        /*
401         * If there is no node we have found the name of the node we
402         * wish to create.
403         */
404
405        if ( ! node )
406          done = TRUE;
407        else
408          pathloc->node_access = node;
409
410        break;
411 
412      case IMFS_NO_MORE_PATH:
413        set_errno_and_return_minus_one( EEXIST );
414        break;
415
416      case IMFS_INVALID_TOKEN:
417        set_errno_and_return_minus_one( ENAMETOOLONG );
418        break;
419
420      case IMFS_CURRENT_DIR:
421        break;
422    }
423  }
424
425  *name = &path[ i - len ];
426   
427  /*
428   * We have evaluated the path as far as we can.
429   * Verify there is not any invalid stuff at the end of the name.
430   */
431
432  for( ; path[i] != '\0'; i++) {
433    if ( !IMFS_is_separator( path[ i ] ) )
434      set_errno_and_return_minus_one( ENOENT );
435  }
436
437  /*
438   * Verify we can execute and write to this directory.
439   */
440
441  result = IMFS_Set_handlers( pathloc );
442
443  /*
444   * The returned node must be a directory
445   */
446  node = pathloc->node_access;
447  if ( node->type != IMFS_DIRECTORY )
448    set_errno_and_return_minus_one( ENOTDIR );
449
450  /*
451   * We must have Write and execute permission on the returned node.
452   */
453
454  if ( !IMFS_evaluate_permission( pathloc, RTEMS_LIBIO_PERMS_WX ) )
455    set_errno_and_return_minus_one( EACCES );
456 
457  return result;
458}
459
460
461/*
462 *  IMFS_eval_path
463 *
464 *  The following routine evaluate path for a node that wishes to be
465 *  accessed with mode.  pathloc is returned with a pointer to the
466 *  node to be accessed.
467 */
468
469int IMFS_eval_path( 
470  const char                        *pathname,     /* IN     */
471  int                                flags,        /* IN     */
472  rtems_filesystem_location_info_t  *pathloc       /* IN/OUT */
473)
474{
475  int                                 i = 0;
476  int                                 len;
477  IMFS_token_types                    type = IMFS_CURRENT_DIR;
478  char                                token[ IMFS_NAME_MAX + 1 ];
479  rtems_filesystem_location_info_t    newloc;
480  IMFS_jnode_t                       *node;
481  int                                 result;
482
483  if ( !rtems_libio_is_valid_perms( flags ) ) {
484    assert( 0 );
485    set_errno_and_return_minus_one( EIO );   
486  }
487
488  /*
489   *  This was filled in by the caller and is valid in the
490   *  mount table.
491   */
492
493  node = pathloc->node_access;
494
495  /*
496   *  Evaluate all tokens until we are done or an error occurs.
497   */
498
499  while( (type != IMFS_NO_MORE_PATH) && (type != IMFS_INVALID_TOKEN) ) {
500
501    type = IMFS_get_token( &pathname[i], token, &len );
502    i +=  len;
503   
504    if ( !pathloc->node_access )
505      set_errno_and_return_minus_one( ENOENT );
506
507    /*
508     * I cannot move out of this directory without execute permission.
509     */
510    if ( type != IMFS_NO_MORE_PATH )
511      if ( node->type == IMFS_DIRECTORY )
512        if ( !IMFS_evaluate_permission( pathloc, RTEMS_LIBIO_PERMS_SEARCH ) )
513           set_errno_and_return_minus_one( EACCES );
514
515    node = pathloc->node_access;
516
517    switch( type ) {
518      case IMFS_UP_DIR:
519
520        /*
521         *  Am I at the root of this mounted filesystem?
522         */
523
524        if (pathloc->node_access ==
525            pathloc->mt_entry->mt_fs_root.node_access) {
526
527          /*
528           *  Am I at the root of all filesystems?
529           */
530
531          if ( pathloc->node_access == rtems_filesystem_root.node_access ) {
532            break;       /* Throw out the .. in this case */
533          } else {
534            newloc = pathloc->mt_entry->mt_point_node;
535            *pathloc = newloc;
536            return (*pathloc->ops->evalpath_h)(&(pathname[i-len]),flags,pathloc);
537          }
538        } else {
539
540          if ( !node->Parent )
541            set_errno_and_return_minus_one( ENOENT );
542
543          node = node->Parent;
544          pathloc->node_access = node;
545
546        }
547
548        pathloc->node_access = node;
549        break;
550
551      case IMFS_NAME:
552        /*
553         *  If we are at a link follow it.
554         */       
555
556        if ( node->type == IMFS_HARD_LINK ) {
557
558          IMFS_evaluate_hard_link( pathloc, 0 );
559
560          node = pathloc->node_access;
561          if ( !node )
562            set_errno_and_return_minus_one( ENOTDIR );
563
564        } else if ( node->type == IMFS_SYM_LINK ) {
565
566          result = IMFS_evaluate_sym_link( pathloc, 0 );
567
568          node = pathloc->node_access;
569          if ( result == -1 )
570            return -1;
571        }
572
573       /*
574        *  Only a directory can be decended into.
575        */
576
577       if ( node->type != IMFS_DIRECTORY )
578          set_errno_and_return_minus_one( ENOTDIR );
579
580        /*
581         *  If we are at a node that is a mount point. Set loc to the
582         *  new fs root node and let them finish evaluating the path.
583         */
584
585        if ( node->info.directory.mt_fs != NULL ) {
586          newloc   = node->info.directory.mt_fs->mt_fs_root;
587          *pathloc = newloc;
588          return (*pathloc->ops->evalpath_h)( &pathname[i-len], flags, pathloc );
589        }
590
591        /*
592         *  Otherwise find the token name in the present location.
593         */
594
595        node = IMFS_find_match_in_dir( node, token );
596        if ( !node )
597          set_errno_and_return_minus_one( ENOENT );
598
599        /*
600         *  Set the node access to the point we have found.
601         */
602
603        pathloc->node_access = node;
604        break;
605
606      case IMFS_NO_MORE_PATH:
607      case IMFS_CURRENT_DIR:
608        break;
609
610      case IMFS_INVALID_TOKEN:
611        set_errno_and_return_minus_one( ENAMETOOLONG );
612        break;
613
614    }
615  }
616
617  /*
618   *  Always return the root node.
619   *
620   *  If we are at a node that is a mount point. Set loc to the
621   *  new fs root node and let let the mounted filesystem set the handlers.
622   *
623   *  NOTE: The behavior of stat() on a mount point appears to be questionable.
624   */
625
626  if ( node->type == IMFS_DIRECTORY ) {
627    if ( node->info.directory.mt_fs != NULL ) {
628      newloc   = node->info.directory.mt_fs->mt_fs_root;
629      *pathloc = newloc;
630      return (*pathloc->ops->evalpath_h)( &pathname[i-len], flags, pathloc );
631    } else {
632      result = IMFS_Set_handlers( pathloc );
633    }
634  } else {
635    result = IMFS_Set_handlers( pathloc );
636  }
637
638  /*
639   * Verify we have the correct permissions for this node.
640   */
641
642  if ( !IMFS_evaluate_permission( pathloc, flags ) )
643    set_errno_and_return_minus_one( EACCES );
644
645  return result;
646}
Note: See TracBrowser for help on using the repository browser.