home *** CD-ROM | disk | FTP | other *** search
/ back2roots/padua / padua.7z / padua / uucp / duucp-1.17 / AU-117b4-src.lha / src / lib / security.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-24  |  3.1 KB  |  159 lines

  1. /*
  2.  *  SECURITY.C
  3.  *
  4.  *  (C) Copyright 1989-1990 by Matthew Dillon,    All Rights Reserved.
  5.  *  Changes copyright 1993 by Michael B. Smith, All Rights Reserved.
  6.  *
  7.  *  Checks whether a given file should be allowed to be read or written
  8.  *
  9.  *  Lock directory containing file
  10.  *  Generate directory path
  11.  *  Check path against allowed list (UULIB:Security)
  12.  *
  13.  *  If type == 'c' return  1 if file name begins with a C.
  14.  *           return -1 if file name does not begin with a C.
  15.  *           return  0 if file name begins with a C. but is for
  16.  *            a directory other than the current one.
  17.  */
  18.  
  19. #include <exec/types.h>
  20. #include <libraries/dosextens.h>
  21. #include <string.h>
  22. #include <stdio.h>
  23. #include "config.h"
  24.  
  25. Prototype int SecurityDisallow (char *, int);
  26.  
  27. static long mySameLock (BPTR, BPTR);
  28. static char
  29.     TmpBuf [128];
  30.  
  31. extern struct DosLibrary
  32.     *DOSBase;
  33.  
  34. int
  35. SecurityDisallow (char *file, int type)
  36. {
  37.     char
  38.         c,
  39.         *fs;
  40.     int
  41.         disallow = 1;
  42.     FILE
  43.         *fi;
  44.     BPTR
  45.         lock,
  46.         slock;
  47.     struct Process
  48.         *proc = (struct Process *) FindTask (NULL);
  49.     APTR
  50.         oldWindowPtr = proc->pr_WindowPtr;
  51.  
  52.     for (fs = file + strlen(file); fs >= file && *fs != '/' && *fs != ':'; --fs)
  53.         ;
  54.     ++fs;
  55.  
  56.     if (fs == file) {        /*    just a file name    */
  57.         if (type == 'c') {
  58.             if ((file [0] | 0x20) == 'c' && file [1] == '.')
  59.                 return 1;
  60.  
  61.             return -1;
  62.         }
  63.         return 0;   /*    type r or w, current dir, allow */
  64.     }
  65.  
  66.     if (type == 'c')    /*  not just a file name, allow C.   */
  67.         return 0;
  68.  
  69.     c = *fs;
  70.     *fs = 0;        /*    keep just the path    */
  71.  
  72.     proc->pr_WindowPtr = (APTR) -1L;
  73.     lock = Lock ((UBYTE *) file, SHARED_LOCK);
  74.     fi = fopen (MakeConfigPath (UULIB, "Security"), "r");
  75.  
  76.     if (lock && fi) {
  77.         while (fgets(TmpBuf, sizeof(TmpBuf), fi)) {
  78.             char
  79.                 *ptr;
  80.  
  81.             if (TmpBuf[0] == '#' || TmpBuf[0] == '\n' || TmpBuf[0] == 0)
  82.                 continue;
  83.  
  84.             /*
  85.              *  breakout the directory name and permissions
  86.              */
  87.  
  88.             for (ptr = TmpBuf; *ptr != '\n' && *ptr != ' ' && *ptr != '\t'; ++ptr)
  89.                 ;
  90.             *ptr = 0;
  91.  
  92.             /*
  93.              *  permissions allowed?
  94.              */
  95.  
  96.             for (++ptr; *ptr && *ptr != type; ++ptr)
  97.                 ;
  98.             if (*ptr == 0)        /*    sorry    */
  99.                 continue;
  100.  
  101.             /*
  102.              *  Determine whether the directory <lock> is equivalent
  103.              *  to the directory in the security file or a sub directory
  104.              *  of a directory in the security file
  105.              */
  106.  
  107.             if (slock = Lock ((UBYTE *) TmpBuf, SHARED_LOCK)) {
  108.                 BPTR
  109.                     copyLock = DupLock (lock);
  110.  
  111.                 while (copyLock) {
  112.                     BPTR tmpLock;
  113.  
  114.                     if (mySameLock (copyLock, slock)) {
  115.                         disallow = 0;
  116.                         break;
  117.                     }
  118.                     tmpLock = ParentDir (copyLock);
  119.                     UnLock (copyLock);
  120.                     copyLock = tmpLock;
  121.                 }
  122.                 if (copyLock)
  123.                     UnLock (copyLock);
  124.                 UnLock (slock);
  125.             }
  126.  
  127.             if (disallow == 0)
  128.                 break;
  129.         }
  130.     }
  131.  
  132.     if (fi)
  133.         fclose (fi);
  134.     if (lock)
  135.         UnLock (lock);
  136.  
  137.     proc->pr_WindowPtr = oldWindowPtr;
  138.  
  139.     *fs = c;        /*    restore path    */
  140.  
  141.     return disallow;
  142. }
  143.  
  144. static long
  145. mySameLock (BPTR lock, BPTR slock)
  146. {
  147.     if (DOSBase->dl_lib.lib_Version < 36) {
  148.         struct FileLock
  149.             *fl1 = (struct FileLock *)((long) lock << 2),
  150.             *fl2 = (struct FileLock *)((long) slock << 2);
  151.  
  152.         if (fl1->fl_Task == fl2->fl_Task && fl1->fl_Key == fl2->fl_Key)
  153.             return 1;
  154.         return 0;
  155.     }
  156.  
  157.     return SameLock (lock, slock);
  158. }
  159.