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