home *** CD-ROM | disk | FTP | other *** search
/ Fish 'n' More 2 / fishmore-publicdomainlibraryvol.ii1991xetec.iso / fish / telecom / uucp_442 / src / lib / security.c < prev    next >
C/C++ Source or Header  |  1990-10-08  |  3KB  |  122 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. #ifndef LATTICE
  33. extern void *FindTask();
  34. #endif
  35.  
  36. static char TmpBuf[128];
  37.  
  38. int
  39. SecurityDisallow(file, type)
  40. char *file;
  41. int type;
  42. {
  43.     char *fs;
  44.     char c;
  45.     int  disallow = 1;
  46.     BPTR lock;
  47.     BPTR slock;
  48.     Process *proc = (Process *)FindTask(NULL);
  49.     APTR oldWindowPtr = proc->pr_WindowPtr;
  50.  
  51.     for (fs = file + strlen(file); fs >= file && *fs != '/' && *fs != ':'; --fs);
  52.     ++fs;
  53.     if (fs == file) {           /*  just a file name    */
  54.     if (type == 'c') {
  55.         if ((file[0]|0x20) == 'c' && file[1] == '.')
  56.         return(1);
  57.         return(-1);
  58.     }
  59.     return(0);              /*  type r or w, current dir, allow. */
  60.     }
  61.     if (type == 'c')            /*  not just a file name, allow C.   */
  62.     return(0);
  63.     c = *fs;
  64.     *fs = 0;        /*  keep just the path        */
  65.  
  66.     proc->pr_WindowPtr = (APTR)-1L;
  67.     lock = Lock(file, SHARED_LOCK);
  68.     if (lock) {
  69.     FILE *fi = fopen(MakeConfigPath(UULIB, "Security"), "r");
  70.     if (fi) {
  71.         while (fgets(TmpBuf, sizeof(TmpBuf), fi)) {
  72.         char *ptr;
  73.         if (TmpBuf[0] == '#' || TmpBuf[0] == '\n' || TmpBuf[0] == 0)
  74.             continue;
  75.  
  76.         /*
  77.          *  breakout the directory name and permissions
  78.          */
  79.  
  80.         for (ptr = TmpBuf; *ptr != '\n' && *ptr != ' ' && *ptr != 9; ++ptr);
  81.         *ptr = 0;
  82.  
  83.         /*
  84.          *  permissions allowed?
  85.          */
  86.  
  87.         for (++ptr; *ptr && *ptr != type; ++ptr);
  88.         if (*ptr == 0)      /*  sorry   */
  89.             continue;
  90.  
  91.         if (slock = Lock(TmpBuf, SHARED_LOCK)) {
  92.             if (SameLock(lock, slock))
  93.             disallow = 0;
  94.             UnLock(slock);
  95.         }
  96.         if (disallow == 0)
  97.             break;
  98.         }
  99.         fclose(fi);
  100.     }
  101.     UnLock(lock);
  102.     }
  103.     proc->pr_WindowPtr = oldWindowPtr;
  104.  
  105.     *fs = c;        /*  restore path    */
  106.  
  107.     return(disallow);
  108. }
  109.  
  110. int
  111. SameLock(lock, slock)
  112. long lock, slock;
  113. {
  114.     FileLock *fl1 = (FileLock *)((long)lock << 2);
  115.     FileLock *fl2 = (FileLock *)((long)slock << 2);
  116.  
  117.     if (fl1->fl_Task == fl2->fl_Task && fl1->fl_Key == fl2->fl_Key)
  118.     return(1);
  119.     return(0);
  120. }
  121.  
  122.