home *** CD-ROM | disk | FTP | other *** search
- /*
- * SECURITY.C
- *
- * (C) Copyright 1989-1990 by Matthew Dillon, All Rights Reserved.
- * Changes copyright 1993 by Michael B. Smith, All Rights Reserved.
- *
- * Checks whether a given file should be allowed to be read or written
- *
- * Lock directory containing file
- * Generate directory path
- * Check path against allowed list (UULIB:Security)
- *
- * If type == 'c' return 1 if file name begins with a C.
- * return -1 if file name does not begin with a C.
- * return 0 if file name begins with a C. but is for
- * a directory other than the current one.
- */
-
- #include <exec/types.h>
- #include <libraries/dosextens.h>
- #include <string.h>
- #include <stdio.h>
- #include "config.h"
-
- Prototype int SecurityDisallow (char *, int);
-
- static long mySameLock (BPTR, BPTR);
- static char
- TmpBuf [128];
-
- extern struct DosLibrary
- *DOSBase;
-
- int
- SecurityDisallow (char *file, int type)
- {
- char
- c,
- *fs;
- int
- disallow = 1;
- FILE
- *fi;
- BPTR
- lock,
- slock;
- struct Process
- *proc = (struct Process *) FindTask (NULL);
- APTR
- oldWindowPtr = proc->pr_WindowPtr;
-
- for (fs = file + strlen(file); fs >= file && *fs != '/' && *fs != ':'; --fs)
- ;
- ++fs;
-
- if (fs == file) { /* just a file name */
- if (type == 'c') {
- if ((file [0] | 0x20) == 'c' && file [1] == '.')
- return 1;
-
- return -1;
- }
- return 0; /* type r or w, current dir, allow */
- }
-
- if (type == 'c') /* not just a file name, allow C. */
- return 0;
-
- c = *fs;
- *fs = 0; /* keep just the path */
-
- proc->pr_WindowPtr = (APTR) -1L;
- lock = Lock ((UBYTE *) file, SHARED_LOCK);
- fi = fopen (MakeConfigPath (UULIB, "Security"), "r");
-
- if (lock && fi) {
- while (fgets(TmpBuf, sizeof(TmpBuf), fi)) {
- char
- *ptr;
-
- if (TmpBuf[0] == '#' || TmpBuf[0] == '\n' || TmpBuf[0] == 0)
- continue;
-
- /*
- * breakout the directory name and permissions
- */
-
- for (ptr = TmpBuf; *ptr != '\n' && *ptr != ' ' && *ptr != '\t'; ++ptr)
- ;
- *ptr = 0;
-
- /*
- * permissions allowed?
- */
-
- for (++ptr; *ptr && *ptr != type; ++ptr)
- ;
- if (*ptr == 0) /* sorry */
- continue;
-
- /*
- * Determine whether the directory <lock> is equivalent
- * to the directory in the security file or a sub directory
- * of a directory in the security file
- */
-
- if (slock = Lock ((UBYTE *) TmpBuf, SHARED_LOCK)) {
- BPTR
- copyLock = DupLock (lock);
-
- while (copyLock) {
- BPTR tmpLock;
-
- if (mySameLock (copyLock, slock)) {
- disallow = 0;
- break;
- }
- tmpLock = ParentDir (copyLock);
- UnLock (copyLock);
- copyLock = tmpLock;
- }
- if (copyLock)
- UnLock (copyLock);
- UnLock (slock);
- }
-
- if (disallow == 0)
- break;
- }
- }
-
- if (fi)
- fclose (fi);
- if (lock)
- UnLock (lock);
-
- proc->pr_WindowPtr = oldWindowPtr;
-
- *fs = c; /* restore path */
-
- return disallow;
- }
-
- static long
- mySameLock (BPTR lock, BPTR slock)
- {
- if (DOSBase->dl_lib.lib_Version < 36) {
- struct FileLock
- *fl1 = (struct FileLock *)((long) lock << 2),
- *fl2 = (struct FileLock *)((long) slock << 2);
-
- if (fl1->fl_Task == fl2->fl_Task && fl1->fl_Key == fl2->fl_Key)
- return 1;
- return 0;
- }
-
- return SameLock (lock, slock);
- }
-