summaryrefslogtreecommitdiffstats
path: root/cpukit/libfs/src/imfs/imfs_eval.c
blob: 1f1e7c6daf0333d3f88272cea3d48c3f142cae7e (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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
/*
 *  Evaluation IMFS Node Support Routines
 *
 *  COPYRIGHT (c) 1989-1999.
 *  On-Line Applications Research Corporation (OAR).
 *
 *  The license and distribution terms for this file may be
 *  found in the file LICENSE in this distribution or at
 *  http://www.OARcorp.com/rtems/license.html.
 *
 *  $Id$
 */

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <stdlib.h>
#include <assert.h>

#include "imfs.h"
#include "libio_.h"

#define RTEMS_LIBIO_PERMS_RX (RTEMS_LIBIO_PERMS_SEARCH | RTEMS_LIBIO_PERMS_READ)
#define RTEMS_LIBIO_PERMS_WX (RTEMS_LIBIO_PERMS_SEARCH | RTEMS_LIBIO_PERMS_WRITE)

#define MAXSYMLINK 5

int IMFS_Set_handlers( 
  rtems_filesystem_location_info_t   *loc
)
{
  IMFS_jnode_t    *node = loc->node_access;
  IMFS_fs_info_t  *fs_info;

  fs_info = loc->mt_entry->fs_info;
  switch( node->type ) {
    case IMFS_DIRECTORY:
      loc->handlers = fs_info->directory_handlers;
      break;
    case IMFS_DEVICE:
      loc->handlers = &IMFS_device_handlers;
      break;
    case IMFS_SYM_LINK:
    case IMFS_HARD_LINK:
      loc->handlers = &IMFS_link_handlers;
      break;
    case IMFS_MEMORY_FILE:
      loc->handlers = fs_info->memfile_handlers;
      break;
  }

  return 0;
}

/*
 *  IMFS_evaluate_permission
 *
 *  The following routine evaluates that we have permission
 *  to do flags on the node.
 */

int IMFS_evaluate_permission(
  rtems_filesystem_location_info_t  *node,
  int                                flags
)
{
  uid_t         st_uid;
  gid_t         st_gid;
  IMFS_jnode_t *jnode;
  int           flags_to_test;

  if ( !rtems_libio_is_valid_perms( flags ) ) {
    assert( 0 );
    set_errno_and_return_minus_one( EIO );    
  }

  jnode = node->node_access;

#if defined(RTEMS_POSIX_API)
  st_uid = geteuid();
  st_gid = getegid(); 
#else
  st_uid = jnode->st_uid;
  st_gid = jnode->st_gid;
#endif
  
  /*
   * Check if I am owner or a group member or someone else.
   */
  
  flags_to_test = flags;

  if ( st_uid == jnode->st_uid )
    flags_to_test <<= 6;
  else if ( st_gid == jnode->st_gid )
    flags_to_test <<= 3;
  else 
    /* must be other - do nothing */;

  /*
   * If all of the flags are set we have permission
   * to do this.
   */
  if ( ( flags_to_test & jnode->st_mode) == flags_to_test )
    return 1;

  return 0;
}

/*
 *  IMFS_evaluate_hard_link
 *
 *  The following routine evaluates a hardlink to the actual node.
 */

int IMFS_evaluate_hard_link(
  rtems_filesystem_location_info_t  *node,   /* IN/OUT */
  int                                flags   /* IN     */
)
{
  IMFS_jnode_t                     *jnode  = node->node_access;
  int                               result = 0;

  /*
   * Check for things that should never happen.
   */

  if ( jnode->type != IMFS_HARD_LINK )
    rtems_fatal_error_occurred (0xABCD0000);

  /*
   * Set the hard link value and the handlers.
   */

  node->node_access = jnode->info.hard_link.link_node;
  
  IMFS_Set_handlers( node );

  /*
   * Verify we have the correct permissions for this node.
   */

  if ( !IMFS_evaluate_permission( node, flags ) )
    set_errno_and_return_minus_one( EACCES );

  return result;
}


/* 
 *  IMFS_evaluate_sym_link
 *
 *  The following routine evaluates a symbolic link to the actual node.
 */

int IMFS_evaluate_sym_link( 
  rtems_filesystem_location_info_t  *node,   /* IN/OUT */
  int                                flags   /* IN     */
)
{
  IMFS_jnode_t                     *jnode  = node->node_access;
  int                               result = 0;
  int                               i;

  /*
   * Check for things that should never happen.
   */

  if ( jnode->type != IMFS_SYM_LINK )
    rtems_fatal_error_occurred (0xABCD0000);

  if ( !jnode->Parent )
    rtems_fatal_error_occurred( 0xBAD00000 );


  /*
   * Move the node_access to either the symbolic links parent or
   * root depending on the symbolic links path.
   */

  node->node_access = jnode->Parent;

  rtems_filesystem_get_sym_start_loc( 
    jnode->info.sym_link.name,
    &i,
    node
  );

  /*
   * Use eval path to evaluate the path of the symbolic link.
   */

  result = IMFS_eval_path(  
    &jnode->info.sym_link.name[i],
    flags,
    node
  );

  IMFS_Set_handlers( node );

  /*
   * Verify we have the correct permissions for this node.
   */

  if ( !IMFS_evaluate_permission( node, flags ) )
    set_errno_and_return_minus_one( EACCES );

  return result;
}

/*
 *  IMFS_evaluate_link
 *
 *  The following routine returns the real node pointed to by a link.
 */

int IMFS_evaluate_link( 
  rtems_filesystem_location_info_t  *node,   /* IN/OUT */
  int                                flags   /* IN     */
)
{
  IMFS_jnode_t                     *jnode;
  int                               result = 0;

  do {
    jnode  = node->node_access;

    /*
     * Increment and check the link counter.
     */

    rtems_filesystem_link_counts ++;
    if ( rtems_filesystem_link_counts > MAXSYMLINK ) {
      rtems_filesystem_link_counts = 0;
      set_errno_and_return_minus_one( ELOOP );
    }

    /*
     *  Follow the Link node.
     */

    if ( jnode->type == IMFS_HARD_LINK )
      result = IMFS_evaluate_hard_link( node, flags );

    else if (jnode->type == IMFS_SYM_LINK )
      result = IMFS_evaluate_sym_link( node, flags );

  } while ( ( result == 0 ) && ( ( jnode->type == IMFS_SYM_LINK  ) || 
                                 ( jnode->type == IMFS_HARD_LINK ) ) );

  /*
   * Clear link counter.
   */

  rtems_filesystem_link_counts = 0;

  return result;
}  


/*
 *  IMFS_evaluate_for_make
 *
 *  The following routine evaluate path for a new node to be created.
 *  pathloc is returned with a pointer to the parent of the new node.
 *  name is returned with a pointer to the first character in the 
 *  new node name.  The parent node is verified to be a directory.
 */

int IMFS_evaluate_for_make(
   const char                         *path,       /* IN     */
   rtems_filesystem_location_info_t   *pathloc,    /* IN/OUT */
   const char                        **name        /* OUT    */
)
{
  int                                 i = 0;
  int                                 len;
  IMFS_token_types                    type;
  char                                token[ IMFS_NAME_MAX + 1 ];
  rtems_filesystem_location_info_t    newloc;
  IMFS_jnode_t                       *node;
  int                                 done = 0;
  int                                 result; 
  
  /*
   * This was filled in by the caller and is valid in the 
   * mount table.
   */
  node = pathloc->node_access;

  /*
   *  Evaluate all tokens until we are done or an error occurs.
   */

  while( !done ) {

    type = IMFS_get_token( &path[i], token, &len );
    i +=  len;
    
    if ( !pathloc->node_access )
      set_errno_and_return_minus_one( ENOENT );

    /*
     * I cannot move out of this directory without execute permission.
     */

    if ( type != IMFS_NO_MORE_PATH )
      if ( node->type == IMFS_DIRECTORY )
        if ( !IMFS_evaluate_permission( pathloc, RTEMS_LIBIO_PERMS_SEARCH ) )
           set_errno_and_return_minus_one( EACCES );

    node = pathloc->node_access;

    switch( type ) {

      case IMFS_UP_DIR:

	/*
	 * Am I at the root of this mounted filesystem?
	 */

        if (pathloc->node_access == pathloc->mt_entry->mt_fs_root.node_access){

          /*
	   *  Am I at the root of all filesystems?
	   */

          if ( pathloc->node_access == rtems_filesystem_root.node_access ) {
            break; 

	  } else {
            newloc = pathloc->mt_entry->mt_point_node;
            *pathloc = newloc;
            return (*pathloc->ops->evalformake)( &path[i-len], pathloc, name );
	  }
	} else {

          if ( !node->Parent )
            set_errno_and_return_minus_one( ENOENT );

          node = node->Parent;
	}

        pathloc->node_access = node;
        break;

      case IMFS_NAME:

	if ( node->type == IMFS_HARD_LINK ) {

          result = IMFS_evaluate_link( pathloc, 0 );
          if ( result == -1 )
            return -1;

	} else if ( node->type == IMFS_SYM_LINK ) {

          result = IMFS_evaluate_link( pathloc, 0 );

          if ( result == -1 )
            return -1;
	}

        node = pathloc->node_access;
        if ( !node )
          set_errno_and_return_minus_one( ENOTDIR ); 

        /*
         * Only a directory can be decended into.
	 */

        if ( node->type != IMFS_DIRECTORY )
          set_errno_and_return_minus_one( ENOTDIR );

	/*
	 * If we are at a node that is a mount point. Set loc to the
	 * new fs root node and let them finish evaluating the path.
	 */

        if ( node->info.directory.mt_fs != NULL ) {
          newloc  = node->info.directory.mt_fs->mt_fs_root;
          *pathloc = newloc;
          return (*pathloc->ops->evalformake)( &path[i-len], pathloc, name );
	}

	/*
	 * Otherwise find the token name in the present location.
	 */

        node = IMFS_find_match_in_dir( node, token );

	/*
	 * If there is no node we have found the name of the node we 
         * wish to create.
	 */

        if ( ! node )
          done = TRUE;
        else
          pathloc->node_access = node;

        break;
 
      case IMFS_NO_MORE_PATH:
        set_errno_and_return_minus_one( EEXIST );
        break;

      case IMFS_INVALID_TOKEN:
        set_errno_and_return_minus_one( ENAMETOOLONG );
        break;

      case IMFS_CURRENT_DIR:
        break;
    }
  }

  *name = &path[ i - len ];
   
  /*
   * We have evaluated the path as far as we can.
   * Verify there is not any invalid stuff at the end of the name. 
   */

  for( ; path[i] != '\0'; i++) {
    if ( !IMFS_is_separator( path[ i ] ) ) 
      set_errno_and_return_minus_one( ENOENT );
  }

  /* 
   * Verify we can execute and write to this directory.
   */

  result = IMFS_Set_handlers( pathloc );

  /*
   * The returned node must be a directory
   */
  node = pathloc->node_access;
  if ( node->type != IMFS_DIRECTORY )
    set_errno_and_return_minus_one( ENOTDIR );

  /*
   * We must have Write and execute permission on the returned node.
   */

  if ( !IMFS_evaluate_permission( pathloc, RTEMS_LIBIO_PERMS_WX ) )
    set_errno_and_return_minus_one( EACCES );
  
  return result;
}


/*
 *  IMFS_eval_path
 *
 *  The following routine evaluate path for a node that wishes to be
 *  accessed with mode.  pathloc is returned with a pointer to the
 *  node to be accessed.
 */

int IMFS_eval_path(  
  const char                        *pathname,     /* IN     */
  int                                flags,        /* IN     */
  rtems_filesystem_location_info_t  *pathloc       /* IN/OUT */
)
{
  int                                 i = 0;
  int                                 len;
  IMFS_token_types                    type = IMFS_CURRENT_DIR;
  char                                token[ IMFS_NAME_MAX + 1 ];
  rtems_filesystem_location_info_t    newloc;
  IMFS_jnode_t                       *node;
  int                                 result; 

  if ( !rtems_libio_is_valid_perms( flags ) ) {
    assert( 0 );
    set_errno_and_return_minus_one( EIO );    
  }

  /*
   *  This was filled in by the caller and is valid in the 
   *  mount table.
   */

  node = pathloc->node_access;

  /*
   *  Evaluate all tokens until we are done or an error occurs.
   */

  while( (type != IMFS_NO_MORE_PATH) && (type != IMFS_INVALID_TOKEN) ) {

    type = IMFS_get_token( &pathname[i], token, &len );
    i +=  len;
    
    if ( !pathloc->node_access )
      set_errno_and_return_minus_one( ENOENT );

    /*
     * I cannot move out of this directory without execute permission.
     */
    if ( type != IMFS_NO_MORE_PATH )
      if ( node->type == IMFS_DIRECTORY )
        if ( !IMFS_evaluate_permission( pathloc, RTEMS_LIBIO_PERMS_SEARCH ) )
           set_errno_and_return_minus_one( EACCES );

    node = pathloc->node_access;

    switch( type ) {
      case IMFS_UP_DIR:

	/*
	 *  Am I at the root of this mounted filesystem?
	 */

        if (pathloc->node_access == 
            pathloc->mt_entry->mt_fs_root.node_access) {

          /*
	   *  Am I at the root of all filesystems?
	   */

          if ( pathloc->node_access == rtems_filesystem_root.node_access ) {
            break;       /* Throw out the .. in this case */
	  } else {
            newloc = pathloc->mt_entry->mt_point_node;
            *pathloc = newloc;
            return (*pathloc->ops->evalpath)(&(pathname[i-len]),flags,pathloc);
	  }
	} else {

          if ( !node->Parent )
            set_errno_and_return_minus_one( ENOENT );

          node = node->Parent;
          pathloc->node_access = node;

	}

        pathloc->node_access = node;
        break;

      case IMFS_NAME:
	/*
	 *  If we are at a link follow it.
	 */        

	if ( node->type == IMFS_HARD_LINK ) {

          IMFS_evaluate_hard_link( pathloc, 0 );

          node = pathloc->node_access;
          if ( !node )
            set_errno_and_return_minus_one( ENOTDIR ); 

	} else if ( node->type == IMFS_SYM_LINK ) {

          result = IMFS_evaluate_sym_link( pathloc, 0 );

          node = pathloc->node_access;
          if ( result == -1 )
            return -1;
	}

       /*
        *  Only a directory can be decended into.
        */

       if ( node->type != IMFS_DIRECTORY )
          set_errno_and_return_minus_one( ENOTDIR );

	/*
	 *  If we are at a node that is a mount point. Set loc to the
	 *  new fs root node and let them finish evaluating the path.
	 */

        if ( node->info.directory.mt_fs != NULL ) {
          newloc   = node->info.directory.mt_fs->mt_fs_root;
          *pathloc = newloc;
          return (*pathloc->ops->evalpath)( &pathname[i-len], flags, pathloc );
	}

	/*
	 *  Otherwise find the token name in the present location.
	 */

        node = IMFS_find_match_in_dir( node, token );
        if ( !node )
          set_errno_and_return_minus_one( ENOENT );

	/*
	 *  Set the node access to the point we have found.
	 */

        pathloc->node_access = node;
        break;

      case IMFS_NO_MORE_PATH:
      case IMFS_CURRENT_DIR:
        break;

      case IMFS_INVALID_TOKEN:
        set_errno_and_return_minus_one( ENAMETOOLONG );
        break;

    }
  }

  /*
   *  Only return root node if this is the base file system.
   */

  if ((pathloc->node_access == pathloc->mt_entry->mt_fs_root.node_access) &&
      (pathloc->node_access != rtems_filesystem_root.node_access) ) {
    newloc = pathloc->mt_entry->mt_point_node;
    *pathloc = newloc;
  }

  result = IMFS_Set_handlers( pathloc );

  /*
   * Verify we have the correct permissions for this node.
   */

  if ( !IMFS_evaluate_permission( pathloc, flags ) )
    set_errno_and_return_minus_one( EACCES );

  return result;
}