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