home *** CD-ROM | disk | FTP | other *** search
/ The CDPD Public Domain Collection for CDTV 4 / CDPD_IV.bin / networking / tcpip / amitcp-support / wustl-ftpdaemon / support / amiga.c next >
Encoding:
C/C++ Source or Header  |  1994-06-29  |  3.5 KB  |  185 lines

  1. /*
  2.  * Amiga support routines for wuarchive ftp daemon
  3.  * © 1994 Blaz Zupan, <blaz.zupan@uni-mb.si
  4.  * All Rights Reserved
  5.  *
  6.  * Contains: fchown(), chroot(), IsInRoot(), chdir(), getcwd()
  7.  *
  8.  */
  9.  
  10. #include <exec/memory.h>
  11. #include <proto/dos.h>
  12. #include <proto/exec.h>
  13. #include <string.h>
  14. #include <stdlib.h>
  15. #include <errno.h>
  16. #include "/src/config.h"
  17.  
  18. #if 0
  19. #include <proto/usergroup.h>
  20. #include <ios1.h>
  21.  
  22. /* This is a terrible hack. First we try to find the UFB with
  23.  * a SAS specific function and extract the Amiga filehandle from
  24.  * it. Then we close the file (by using the Amiga filehandle) and
  25.  * set the owner of it and then reopen it (and hope that the
  26.  * reopening succeeds).
  27.  *
  28.  * This will stay a hack as long as C= doesn't provide us with
  29.  * a SetOwner() that can operate on filehandles and locks.
  30.  */
  31.  
  32. int fchown(int fd, int uid, int gid)
  33. {
  34.   int ret = -1;
  35.   struct UFB *ufb;
  36.  
  37.   if (ufb = chkufb (fd)) /* SAS specific! */
  38.   {
  39.     Close((BPTR)ufb->ufbfh);
  40.     if (SetOwner(ufb->ufbfn, (uid << 16) + gid))
  41.       ret = 0;
  42.     ufb->ufbfh = Open(ufb->ufbfn, MODE_READWRITE);
  43.   }
  44.   return (ret);
  45. }
  46. #else
  47. int fchown(int fd, int uid, int gid)
  48. {
  49.   return 0;
  50. }
  51. #endif
  52.  
  53.  
  54. /*
  55.  * This routine is an attempt to emulate the Unix chroot()
  56.  * function. It only saves the name supplied in the global
  57.  * AmigaRootDir. Everything else must be done by the suppord
  58.  * routines (IsInRoot(), chdir() and getcwd()).
  59.  */
  60. extern int anonymous, guest;
  61. char AmigaRootDir[MAXPATHLEN + 2] = "";
  62.  
  63. int
  64. chroot (char *dir)
  65. {
  66.   BPTR lock;
  67.  
  68.   if (lock = Lock (dir, SHARED_LOCK))
  69.   {
  70.     NameFromLock (lock, AmigaRootDir, MAXPATHLEN);
  71.     UnLock (lock);
  72.     return 0;
  73.   }
  74.   return -1;
  75. }
  76.  
  77. /* Checks to see if file is located under users
  78.  * home directory structure (but only for anonymous and guest users!).
  79.  */
  80. int
  81. IsInRoot (char *dir)
  82. {
  83.   int ret = 0;
  84.   BPTR lock;
  85.  
  86.   if (!anonymous && !guest)
  87.     return 1;
  88.  
  89.   if (lock = Lock (dir, SHARED_LOCK))
  90.   {
  91.     char name[MAXPATHLEN];
  92.  
  93.     if (NameFromLock (lock, name, MAXPATHLEN))
  94.     {
  95.       if (strlen (AmigaRootDir) <= strlen (name))
  96.       {
  97.     if (!strncmp (AmigaRootDir, name, strlen (AmigaRootDir)))
  98.       ret = 1;
  99.       }
  100.     }
  101.     UnLock (lock);
  102.   }
  103.   return ret;
  104. }
  105.  
  106. /* chdir() replacement that enables use of chroot() */
  107. int
  108. chdir (const char *path)
  109. {
  110.   BPTR lock;
  111.   int err = ENOENT;
  112.   char newpath[MAXPATHLEN];
  113.  
  114.   if (anonymous || guest)
  115.   {
  116.     if (path[0] == '/')
  117.     {
  118.       strcpy(newpath, AmigaRootDir);
  119.       AddPart(newpath, (void *)&path[1], MAXPATHLEN);
  120.     }
  121.     else
  122.       strcpy(newpath, path);
  123.   }
  124.   else
  125.     strcpy(newpath, path);
  126.  
  127.   {
  128.     char tmppath[MAXPATHLEN];
  129.  
  130.     adjustpath(newpath, tmppath, MAXPATHLEN); /* unix<->amiga path conversion */
  131.     strcpy(newpath, tmppath);
  132.   }
  133.  
  134.   if (IsInRoot (newpath))
  135.   {
  136.     if (lock = Lock (newpath, SHARED_LOCK))
  137.     {
  138.       UnLock (CurrentDir (lock));
  139.       err = 0;
  140.     }
  141.   }
  142.   errno = err;
  143.   return err ? -1 : 0;
  144. }
  145.  
  146. /* getcwd() replacement that returns a fake directory under
  147.  * users home directory
  148.  */
  149. char *
  150. getcwd (char *buf, int size)
  151. {
  152.   if (!buf)
  153.     buf = malloc (MAXPATHLEN);
  154.  
  155.   if (buf)
  156.   {
  157.     BPTR lock;
  158.  
  159.     if (lock = Lock ("", SHARED_LOCK))
  160.     {
  161.       if (NameFromLock (lock, buf, size))
  162.       {
  163.         if (anonymous || guest)
  164.         {
  165.           if (*(buf + strlen(AmigaRootDir)) == '/')
  166.         strcpy (buf, buf + strlen (AmigaRootDir));
  167.       else
  168.       {
  169.         strcpy (buf + 1, buf + strlen (AmigaRootDir));
  170.         buf[0] = '/';
  171.       }
  172.     }
  173.       }
  174.       else
  175.     errno = ENOMEM;
  176.       UnLock (lock);
  177.     }
  178.     else
  179.       errno = ENOENT;
  180.   }
  181.   else
  182.     errno = ENOMEM;
  183.   return buf;
  184. }
  185.