home *** CD-ROM | disk | FTP | other *** search
/ minnie.tuhs.org / unixen.tar / unixen / PDP-11 / Distributions / ucb / spencer_2bsd.tar.gz / 2bsd.tar / src / net / netrm.c < prev    next >
C/C++ Source or Header  |  1980-02-17  |  2KB  |  117 lines

  1. /* Copyright (c) 1979 Regents of the University of California */
  2. # include "defs.h"
  3.  
  4. /*
  5.  *    netrm - remove an entry from the network queue.
  6.  *
  7.  *    first does a creat to truncate the file to zero length in
  8.  *    case it is being sent. (this stops the daemon!) Next the
  9.  *    file is removed. 
  10.  *    must be setuid root
  11.  */
  12.  
  13. char    path[] =    NETRMPATH;
  14. char    pathname[]=      NETRMNAME;
  15.  
  16. int hisuid, hisgid;
  17. static char visit[26];
  18.  
  19. main(argc,argv)
  20. char    *argv[];
  21. {
  22.     int cnt, mach, i, all = 0;
  23.  
  24.     if (argc < 2)
  25.     {
  26.         printf("usage: netrm [-] file1 file2 ... filen\n");
  27.         exit(1);
  28.     }
  29.  
  30.     hisuid = getuid();
  31.     hisgid = getgid();
  32.     
  33.     if(argv[1][0] == '-'){
  34.         all++;
  35.         argv++;
  36.         argc--;
  37.         }
  38.     cnt = 0;
  39.  
  40.     while (++cnt < argc)rmfile(argv[cnt]);
  41.     if(all){
  42.         visit[local - 'a'] = 1;            /* skip this machine */
  43.         for(i = 'a'; i <= 'z'; i++)
  44.             if((mach = gothru(local,i)) && !visit[mach - 'a']){
  45.                 visit[mach - 'a'] = 1;
  46.                 senddir[strlen(senddir)-1] = mach;
  47.                 pdir(senddir);
  48.                 }
  49.         }
  50.     }
  51. static pdir(str)
  52.   char *str; {
  53.     FILE *df;
  54.     df = fopen(str,"r");
  55.     if(df == NULL || chdir(str) < 0){
  56.         perror(str);
  57.         exit(1);
  58.         }
  59.     while(fread(&dirbuf,1,sizeof dirbuf,df) == sizeof dirbuf){
  60.         if(dirbuf.d_ino == 0
  61.         || dirbuf.d_name[0] != 'd'
  62.         || dirbuf.d_name[1] != 'f'
  63.         || stat(dirbuf.d_name,&statbuf) < 0)
  64.              continue;
  65.         if(guid(statbuf.st_uid,statbuf.st_gid) != hisuid)
  66.             continue;
  67.         /* kludge in file name */
  68.         dirbuf.d_name[3] = dirbuf.d_name[2];
  69.         rmfile(dirbuf.d_name+3);
  70.         }
  71.     fclose(df);
  72.     }
  73. rmfile(str)
  74.   char *str;
  75.   {
  76.     register char *ap, *cp;
  77.     int tt;
  78.     char *ostr,*rem,buf[20];
  79.     ostr = str;
  80.     if(str[0] != 'd' || str[1] != 'f' || str[3] != 'a'){
  81.         strcpy(buf+3,str);
  82.         buf[0] = 'd';
  83.         buf[1] = 'f';
  84.         buf[2] = str[0];
  85.         buf[3] = 'a';
  86.         str = buf;
  87.         }
  88.     cp = path;
  89.     ap = pathname;
  90.     while (*ap++ = *cp++);
  91.     cp = pathname + strlen(pathname) - 10;
  92.     while(*cp++ != 'f');
  93.     cp--;
  94.     cp--;
  95.     rem = cp;
  96.     ap = str;
  97.     while(*cp != '\0' && (*cp++ = *ap++));
  98.     pathname[strlen(pathname) - 11] = str[2];    /* set dir for mach */
  99.  
  100.     if (stat(pathname,&statbuf) < 0) {
  101.         perror(ostr);
  102.         return;
  103.     }
  104.  
  105.     tt = guid(statbuf.st_uid,statbuf.st_gid);
  106.     if(tt != hisuid && hisuid != 0) {
  107.         printf("%s: Permission Denied\n",ostr);
  108.         return;
  109.     }
  110.  
  111.     printf("removing file %s\n",ostr);
  112.     creat(pathname,0600);
  113.     unlink(pathname);
  114.     *rem = 'c';
  115.     unlink(pathname);
  116. }
  117.