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