home *** CD-ROM | disk | FTP | other *** search
/ The CDPD Public Domain Collection for CDTV 4 / CDPD_IV.bin / networking / uucp / amigauucpsrc / lib / security.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-06-29  |  3.2 KB  |  162 lines

  1.  
  2. /*
  3.  *  SECURITY.C
  4.  *
  5.  *  (C) Copyright 1989-1990 by Matthew Dillon,  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 <stdio.h>
  22. #include "config.h"
  23.  
  24. typedef struct FileLock FileLock;
  25. typedef struct Process    Process;
  26.  
  27. Prototype int SecurityDisallow(char *, int);
  28. Prototype int SameLock(long, long);
  29.  
  30. #ifndef LATTICE
  31. extern void *FindTask();
  32. #endif
  33.  
  34. static char TmpBuf[128];
  35.  
  36. int
  37. SecurityDisallow(file, type)
  38. char *file;
  39. int type;
  40. {
  41.     char *fs;
  42.     char c;
  43.     int  disallow = 1;
  44.     BPTR lock;
  45.     BPTR slock;
  46.     Process *proc = (Process *)FindTask(NULL);
  47.     APTR oldWindowPtr = proc->pr_WindowPtr;
  48.  
  49.     for (fs = file + strlen(file); fs >= file && *fs != '/' && *fs != ':'; --fs)
  50.     ;
  51.     ++fs;
  52.     if (fs == file) {           /*  just a file name    */
  53.     if (type == 'c') {
  54.         if ((file[0]|0x20) == 'c' && file[1] == '.')
  55.         return(1);
  56.         return(-1);
  57.     }
  58.     return(0);              /*  type r or w, current dir, allow. */
  59.     }
  60.     if (type == 'c')            /*  not just a file name, allow C.   */
  61.     return(0);
  62.     c = *fs;
  63.     *fs = 0;        /*  keep just the path        */
  64.  
  65.     proc->pr_WindowPtr = (APTR)-1L;
  66.     lock = Lock(file, SHARED_LOCK);
  67.  
  68.     if (lock) {
  69.     FILE *fi = fopen(MakeConfigPath(UULIB, "Security"), "r");
  70.  
  71.     if (fi) {
  72.         while (fgets(TmpBuf, sizeof(TmpBuf), fi)) {
  73.         char *ptr;
  74.  
  75.         if (TmpBuf[0] == '#' || TmpBuf[0] == '\n' || TmpBuf[0] == 0)
  76.             continue;
  77.  
  78.         /*
  79.          *  breakout the directory name and permissions
  80.          */
  81.  
  82.         for (ptr = TmpBuf; *ptr != '\n' && *ptr != ' ' && *ptr != 9; ++ptr)
  83.             ;
  84.         *ptr = 0;
  85.  
  86.         /*
  87.          *  permissions allowed?
  88.          */
  89.  
  90.         for (++ptr; *ptr && *ptr != type; ++ptr)
  91.             ;
  92.         if (*ptr == 0)      /*  sorry   */
  93.             continue;
  94.  
  95.         /*
  96.          *  Determine whether the directory <lock> is equivalent
  97.          *  to the directory in the security file or a sub directory
  98.          *  of a directory in the security file
  99.          */
  100.  
  101.         if (slock = Lock(TmpBuf, SHARED_LOCK)) {
  102.             BPTR copyLock = DupLock(lock);
  103.  
  104.             while (copyLock) {
  105.             BPTR tmpLock;
  106.  
  107.             if (SameLock(copyLock, slock)) {
  108.                 disallow = 0;
  109.                 break;
  110.             }
  111.             tmpLock = ParentDir(copyLock);
  112.             UnLock(copyLock);
  113.             copyLock = tmpLock;
  114.             }
  115.             if (copyLock)
  116.             UnLock(copyLock);
  117.             UnLock(slock);
  118.         }
  119.         if (disallow == 0)
  120.             break;
  121.         }
  122.         fclose(fi);
  123.     }
  124.     UnLock(lock);
  125.     }
  126.     proc->pr_WindowPtr = oldWindowPtr;
  127.  
  128.     *fs = c;        /*  restore path    */
  129.  
  130.     return(disallow);
  131. }
  132.  
  133. int
  134. SameLock(lock, slock)
  135. long lock, slock;
  136. {
  137.     FileLock *fl1 = (FileLock *)((long)lock << 2);
  138.     FileLock *fl2 = (FileLock *)((long)slock << 2);
  139.  
  140.     if (fl1->fl_Task == fl2->fl_Task && fl1->fl_Key == fl2->fl_Key)
  141.     return(1);
  142.     return(0);
  143. }
  144.  
  145. #ifdef TEST
  146.  
  147. /*
  148.  * dcc security.c -DTEST -Iuucp:src/include -o t:st -luucp:src/dlib/uucp
  149.  */
  150.  
  151. main(ac, av)
  152. char *av[];
  153. {
  154.     int r;
  155.  
  156.     r = SecurityDisallow(av[1], av[2][0]);
  157.     printf("r = %d\n", r);
  158.     return(0);
  159. }
  160.  
  161. #endif
  162.