home *** CD-ROM | disk | FTP | other *** search
/ back2roots/padua / padua.7z / padua / uucp / auucp+-1.02 / fuucp_plus_src.lzh / lib / security.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-11-21  |  2.7 KB  |  118 lines

  1.  
  2. /*
  3.  *  SECURITY.C
  4.  *
  5.  *  $Header: Beta:src/uucp/src/lib/RCS/security.c,v 1.1 90/02/02 12:08:33 dillon Exp Locker: dillon $
  6.  *
  7.  *  (C) Copyright 1989-1990 by Matthew Dillon,  All Rights Reserved.
  8.  *
  9.  *  Checks whether a given file should be allowed to be read or written
  10.  *
  11.  *  Lock directory containing file
  12.  *  Generate directory path
  13.  *  Check path against allowed list (UULIB:Security)
  14.  *
  15.  *  If type == 'c' return  1 if file name begins with a C.
  16.  *           return -1 if file name does not begin with a C.
  17.  *           return  0 if file name begins with a C. but is for
  18.  *            a directory other than the current one.
  19.  */
  20.  
  21. #include <exec/types.h>
  22. #include <libraries/dosextens.h>
  23. #include <stdio.h>
  24. #include "config.h"
  25.  
  26. typedef struct FileLock FileLock;
  27. typedef struct Process    Process;
  28.  
  29. Prototype int SecurityDisallow(char *, int);
  30. Prototype int SameLock(long, long);
  31.  
  32. static char TmpBuf[128];
  33.  
  34. int
  35. SecurityDisallow(file, type)
  36. char *file;
  37. int type;
  38. {
  39.     char *fs;
  40.     char c;
  41.     int  disallow = 1;
  42.     BPTR lock;
  43.     BPTR slock;
  44.     Process *proc = (Process *)FindTask(NULL);
  45.     APTR oldWindowPtr = proc->pr_WindowPtr;
  46.  
  47.     for (fs = file + strlen(file); fs >= file && *fs != '/' && *fs != ':'; --fs);
  48.     ++fs;
  49.     if (fs == file) {           /*  just a file name    */
  50.     if (type == 'c') {
  51.         if ((file[0]|0x20) == 'c' && file[1] == '.')
  52.         return(1);
  53.         return(-1);
  54.     }
  55.     return(0);              /*  type r or w, current dir, allow. */
  56.     }
  57.     if (type == 'c')            /*  not just a file name, allow C.   */
  58.     return(0);
  59.     c = *fs;
  60.     *fs = 0;        /*  keep just the path        */
  61.  
  62.     proc->pr_WindowPtr = (APTR)-1L;
  63.     lock = Lock(file, SHARED_LOCK);
  64.     if (lock) {
  65.     FILE *fi = fopen(MakeConfigPath(UULIB, "Security"), "r");
  66.     if (fi) {
  67.         while (fgets(TmpBuf, sizeof(TmpBuf), fi)) {
  68.         char *ptr;
  69.         if (TmpBuf[0] == '#' || TmpBuf[0] == '\n' || TmpBuf[0] == 0)
  70.             continue;
  71.  
  72.         /*
  73.          *  breakout the directory name and permissions
  74.          */
  75.  
  76.         for (ptr = TmpBuf; *ptr != '\n' && *ptr != ' ' && *ptr != 9; ++ptr);
  77.         *ptr = 0;
  78.  
  79.         /*
  80.          *  permissions allowed?
  81.          */
  82.  
  83.         for (++ptr; *ptr && *ptr != type; ++ptr);
  84.         if (*ptr == 0)      /*  sorry   */
  85.             continue;
  86.  
  87.         if (slock = Lock(TmpBuf, SHARED_LOCK)) {
  88.             if (SameLock(lock, slock))
  89.             disallow = 0;
  90.             UnLock(slock);
  91.         }
  92.         if (disallow == 0)
  93.             break;
  94.         }
  95.         fclose(fi);
  96.     }
  97.     UnLock(lock);
  98.     }
  99.     proc->pr_WindowPtr = oldWindowPtr;
  100.  
  101.     *fs = c;        /*  restore path    */
  102.  
  103.     return(disallow);
  104. }
  105.  
  106. int
  107. SameLock(lock, slock)
  108. long lock, slock;
  109. {
  110.     FileLock *fl1 = (FileLock *)((long)lock << 2);
  111.     FileLock *fl2 = (FileLock *)((long)slock << 2);
  112.  
  113.     if (fl1->fl_Task == fl2->fl_Task && fl1->fl_Key == fl2->fl_Key)
  114.     return(1);
  115.     return(0);
  116. }
  117.  
  118.