-
Notifications
You must be signed in to change notification settings - Fork 1
/
GROUPDEL.PAS
277 lines (268 loc) · 6.38 KB
/
GROUPDEL.PAS
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
{ @author: Sylvain Maltais (support@gladir.com)
@created: 2024
@website(https://www.gladir.com/linux-0)
@abstract(Target: Turbo Pascal 7, Free Pascal 3.2)
}
Program GROUPDEL;
Uses DOS;
Var
Language:(_Albanian,_French,_English,_Germany,_Italian,_Spain);
Ok,Skip:Boolean;
TmpLanguage,GroupOrID,GroupName,GroupID:String;
Function StrToUpper(S:String):String;
Var
I:Byte;
Begin
For I:=1 to Length(S)do Begin
If S[I] in['a'..'z']Then S[I]:=Chr(Ord(S[I])-32);
End;
StrToUpper:=S;
End;
Function GetGroupName(GroupOrID:String):String;
Var
FileGroup:Text;
I,CellPos:Integer;
CurrLine,CurrWord:String;
Cells:Array[0..6]of String;
Begin
GetGroupName:='';
{$I-}Assign(FileGroup,'/etc/group');
Reset(FileGroup);{$I+}
If IOResult<>0 Then Begin
WriteLn('Impossible de lire le fichier /etc/group');
Halt(1);
End;
While Not EOF(FileGroup)do Begin
ReadLn(FileGroup,CurrLine);
FillChar(Cells,SizeOf(Cells),0);
CurrWord:='';
CellPos:=0;
For I:=1 to Length(CurrLine) do Begin
If CurrLine[I]=':'Then Begin
Cells[CellPos]:=CurrWord;
CurrWord:='';
Inc(CellPos);
If CellPos>5 Then Break;
End
Else
CurrWord:=CurrWord+CurrLine[I];
End;
If CurrWord<>''Then Begin
Cells[CellPos]:=CurrWord;
Inc(CellPos);
End;
If Cells[0]<>''Then Begin
If(Cells[0]=GroupOrID)or(Cells[2]=GroupOrID)Then Begin
GetGroupName:=Cells[0];
Close(FileGroup);
Exit;
End;
End;
End;
Close(FileGroup);
End;
Function UsersInGroupExist(GroupID:String):Boolean;
Var
FilePasswd:Text;
I,CellPos:Integer;
CurrLine,CurrWord:String;
Cells:Array[0..6]of String;
Begin
UsersInGroupExist:=False;
{$I-}Assign(FilePasswd,'/etc/passwd');
Reset(FilePasswd);{$I+}
If IOResult<>0 Then Begin
WriteLn('Impossible de lire le fichier /etc/passwd');
Halt(1);
End;
While Not EOF(Filepasswd)do Begin
ReadLn(Filepasswd,CurrLine);
FillChar(Cells,SizeOf(Cells),0);
CurrWord:='';
CellPos:=0;
For I:=1 to Length(CurrLine) do Begin
If CurrLine[I]=':'Then Begin
Cells[CellPos]:=CurrWord;
CurrWord:='';
Inc(CellPos);
If CellPos>5 Then Break;
End
Else
CurrWord:=CurrWord+CurrLine[I];
End;
If CurrWord<>''Then Begin
Cells[CellPos]:=CurrWord;
Inc(CellPos);
End;
If Cells[0]<>''Then Begin
If(Cells[3]=GroupID)Then Begin
Close(FilePasswd);
UsersInGroupExist:=True;
Exit;
End;
End;
End;
Close(FilePasswd);
End;
Function GroupToID(GroupName:String):String;
Var
FileGroup:Text;
I,CellPos:Integer;
CurrLine,CurrWord:String;
Cells:Array[0..6]of String;
Begin
GroupToID:='';
{$I-}Assign(FileGroup,'/etc/group');
Reset(FileGroup);{$I+}
If IOResult<>0 Then Begin
WriteLn('Impossible de lire le fichier /etc/group');
Halt(1);
End;
While Not EOF(FileGroup)do Begin
ReadLn(FileGroup,CurrLine);
FillChar(Cells,SizeOf(Cells),0);
CurrWord:='';
CellPos:=0;
For I:=1 to Length(CurrLine) do Begin
If CurrLine[I]=':'Then Begin
Cells[CellPos]:=CurrWord;
CurrWord:='';
Inc(CellPos);
If CellPos>5 Then Break;
End
Else
CurrWord:=CurrWord+CurrLine[I];
End;
If CurrWord<>''Then Begin
Cells[CellPos]:=CurrWord;
Inc(CellPos);
End;
If Cells[0]<>''Then Begin
If(Cells[0]=GroupName)Then Begin
Close(FileGroup);
GroupToID:=Cells[2];
Exit;
End;
End;
End;
Close(FileGroup);
End;
Function DeleteGroup(GroupOrID:String):Boolean;
Var
FileGroup,NewFileGroup:Text;
I,CellPos:Integer;
UserFound:Boolean;
CurrLine,CurrWord:String;
Cells:Array[0..6]of String;
Begin
DeleteGroup:=False;
{$I-}Assign(FileGroup,'/etc/group');
Reset(FileGroup);{$I+}
If IOResult<>0 Then Begin
WriteLn('Impossible de lire le fichier /etc/group');
Halt(1);
End;
{$I-}Assign(NewFileGroup,'/etc/group.tmp');
Rewrite(NewFileGroup);{$I+}
If IOResult<>0 Then Begin
WriteLn('Impossible de cr‚er un nouveau fichier /etc/group');
Halt(2);
End;
While Not EOF(FileGroup)do Begin
UserFound:=False;
ReadLn(FileGroup,CurrLine);
FillChar(Cells,SizeOf(Cells),0);
CurrWord:='';
CellPos:=0;
For I:=1 to Length(CurrLine) do Begin
If CurrLine[I]=':'Then Begin
Cells[CellPos]:=CurrWord;
CurrWord:='';
Inc(CellPos);
If CellPos>5 Then Break;
End
Else
CurrWord:=CurrWord+CurrLine[I];
End;
If CurrWord<>''Then Begin
Cells[CellPos]:=CurrWord;
Inc(CellPos);
End;
If Cells[0]<>''Then Begin
If(Cells[0]=GroupOrID)or(Cells[2]=GroupOrID)Then Begin
UserFound:=True;
DeleteGroup:=True;
End;
End;
If(UserFound)Then Begin
{ Ne l'‚crit pas dans le nouveau fichier }
End
Else
WriteLn(NewFileGroup,CurrLine);
End;
Close(NewFileGroup);
Close(FileGroup);
Assign(NewFileGroup,'/etc/group');
{$I-}Erase(FileGroup);{$I+}
If IOResult<>0 Then Begin
WriteLn('Impossible de remplacer le fichier /etc/group');
Halt(3);
End;
Assign(NewFileGroup,'/etc/group.tmp');
Rename(NewFileGroup,'/etc/group');
End;
BEGIN
Language:=_French;
TmpLanguage:=GetEnv('LANGUAGE');
If TmpLanguage<>''Then Begin
If TmpLanguage[1]='"'Then TmpLanguage:=Copy(TmpLanguage,2,255);
If StrToUpper(Copy(TmpLanguage,1,2))='EN'Then Language:=_English Else
If StrToUpper(Copy(TmpLanguage,1,2))='GR'Then Language:=_Germany Else
If StrToUpper(Copy(TmpLanguage,1,2))='IT'Then Language:=_Italian Else
If StrToUpper(Copy(TmpLanguage,1,2))='SP'Then Language:=_Spain Else
If(StrToUpper(Copy(TmpLanguage,1,2))='SQ')or
(StrToUpper(Copy(TmpLanguage,1,3))='ALB')Then Language:=_Albanian;
End;
If(ParamStr(1)='/?')or(ParamStr(1)='--help')or(ParamStr(1)='-h')or
(ParamStr(1)='/h')or(ParamStr(1)='/H')Then Begin
Case Language of
_English:Begin
WriteLn('groupdel - Removes a group from the system.');
WriteLn;
WriteLn('Syntax: groupdel groupname');
WriteLn;
End;
Else Begin
WriteLn('groupdel : Cette commande permet de supprimer un groupe du systŠme.');
WriteLn;
WriteLn('Syntaxe : groupdel groupname');
WriteLn;
WriteLn(' groupname Indique le nom du groupe … supprimer');
End;
End;
End
Else
Begin
GroupOrID:=ParamStr(1);
GroupName:=GetGroupName(GroupOrID);
GroupID:=GroupToID(GroupOrID);
If GroupID=''Then Begin
WriteLn('Groupe introuvable');
Halt(2);
End
Else
If UsersInGroupExist(GroupID)Then Begin
WriteLn('Impossible de supprimer un groupe utiliser par des utilisateurs');
Halt(1);
End
Else
If DeleteGroup(GroupOrID)Then Begin
WriteLn('Groupe ',GroupName,' supprim‚');
End
Else
Begin
WriteLn('Groupe introuvable');
Halt(1);
End;
End;
END.