-
Notifications
You must be signed in to change notification settings - Fork 1
/
sockftpdctl.pas
301 lines (223 loc) · 6.6 KB
/
sockftpdctl.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
{$mode objfpc}
program sockftpdctl;
uses {$ifdef unix}cthreads, baseunix, unix, {$endif} Classes, SysUtils, StringsLib, AppUtils, Dos;
procedure start; forward;
procedure stop; forward;
procedure usage();
begin
writeln( 'sockftpdctl - a program to manage the SockFTPD daemon ' + {$IFDEF windows}'Windows'{$ELSE}'Unix'{$ENDIF} + ' service functions.');
writeln( '' );
writeln( 'Note that this tool must be executed as an administrator in order to function properly.');
writeln( '' );
writeln( 'Usage: ' );
writeln( ' sockftpdctl --install' );
writeln( ' sockftpdctl --uninstall' );
writeln( ' sockftpdctl --start' );
writeln( ' sockftpdctl --stop' );
writeln( 'Where:' );
writeln( ' --install is used to install SockFTPD program as a service' );
writeln( ' --uninstall is used to uninstall SockFTPD service' );
writeln( ' --start is used to start the service (use it after --install)' );
writeln( ' --stop is used to stop the service' );
halt(2);
end;
procedure install();
var f: Text;
{$ifdef linux}
lines: TStrArray;
i: Integer;
Len: Integer;
{$endif}
{$ifdef windows}
sc: AnsiString;
cmd: AnsiString;
{$endif}
begin
{$ifdef windows}
assign( F, getApplicationDir() + PATH_SEPARATOR + 'sockftpdsvc.ini' );
{$i-}
rewrite( F );
{$i+}
if ( IOResult <> 0 ) then
raise Exception.Create( 'Failed to create file: ' + getApplicationDir() + PATH_SEPARATOR + 'sockftpdsvc.ini' );
writeln( F, '[SockFTPD]' );
writeln( F, 'startup=', getApplicationDir(), PATH_SEPARATOR, 'sockftpd.exe' );
writeln( F, 'shutdown_method=winmessage' );
close( F );
sc := FileSearch( 'SC.EXE', GetEnv( 'PATH' ) );
if ( sc = '' ) then
raise Exception.Create( 'The system tool SC.EXE was not found in the PATH environment.' );
ExecuteProcess( 'SC.Exe', [
'CREATE',
'SockFTPD',
'DisplayName=',
'SockFTPD',
'binpath=',
'\"' + getApplicationDir() + PATH_SEPARATOR + 'srvstart.exe\" SockFTPD -c \"' + getApplicationDir() + PATH_SEPARATOR + 'sockftpdsvc.ini\"',
'start=',
'auto'
] );
ExecuteProcess( 'SC.Exe', [
'DESCRIPTION',
'SockFTPD',
'The WebSocket File Server'
] );
{$endif}
{$ifdef linux}
if ( not fileExists( getApplicationDir() + PATH_SEPARATOR + 'sockftpd' ) ) then
begin
writeln( 'ERROR: The sockftpd file does not exists! Please compile sockftpd first!' );
writeln();
halt( 1 );
end;
writeln( 'Installing sockftpd service in "/etc/init.d/sockftpd" ...' );
assign( F, getApplicationDir() + PATH_SEPARATOR + 'sockftpd.sh' );
{$I-}
reset( F );
{$I+}
if IOResult <> 0 then
begin
writeln( 'ERROR: Failed to open "sockftpd.sh" file from application directory!' );
writeln();
halt( 1 );
end;
setLength( Lines, 0 );
while not eof( F ) do
begin
setLength( Lines, Length( Lines ) + 1 );
readln( F, Lines[ Length(Lines) - 1 ] );
end;
{$I-}
close(F);
{$I+}
if ( IOResult <> 0 ) then
begin
writeln( 'ERROR: Failed to close opened file "sockftpd.sh" located in application directory!' );
writeln();
halt( 1 );
end;
assign( F, '/etc/init.d/sockftpd' );
{$I-}
rewrite( F );
{$I+}
if ( IOResult <> 0 ) then
begin
writeln( 'ERROR: Failed to create file "/etc/init.d/sockftpd". Check if enough permissions exists' );
writeln();
halt( 1 );
end;
Len := Length( Lines );
for i := 1 to Len do
begin
Lines[i - 1] := StringReplace( Lines[ i - 1 ], '%APPDIR%', getApplicationDir(), [ rfReplaceAll ] );
{$I-}
writeln( F, Lines[ i - 1 ] );
{$I+}
if ( IOResult <> 0 ) then
begin
writeln( 'ERROR: Failed to write in file "/etc/init.d/sockftpd". Check if enough free space on disk maybe?' );
writeln;
halt(1);
end;
end;
{$I-}
close( F );
{$I+}
if ( IOResult <> 0 ) then
begin
writeln( 'ERROR: Failed to close file "/etc/init.d/sockftpd". Operation aborted!' );
writeln;
halt( 1 );
end;
// now set file permissions
if fpchmod( '/etc/init.d/sockftpd', &755 ) <> 0 then
begin
writeln( 'ERROR: Failed to chmod file "/etc/init.d/sockftpd".' );
writeln;
halt(1);
end;
writeln( '* Service installed as "/etc/init.d/sockftpd".' );
{$endif}
start;
end;
procedure uninstall();
begin
{$ifdef windows}
writeln( 'Trying to uninstall the SockFTPD service...' );
try
stop;
except
On E:Exception do begin
writeln( 'The service does not seem to be started...' );
end;
end;
ExecuteProcess( 'SC.Exe', [
'delete',
'SockFTPD'
] );
{$endif}
{$ifdef linux}
if fileExists( '/etc/init.d/sockftpd' ) then
begin
writeln( 'Uninstalling sockftpd from /etc/init.d/sockftpd...' );
stop;
if DeleteFile( '/etc/init.d/sockftpd' ) then
begin
writeln( '* SockFTPD service has been uninstalled successfully!' );
end else
begin
writeln( 'Sockftpd service could not be uninstalled. Not enough permissions to delete file /etc/init.d/sockftpd!' );
halt(2);
end;
end;
{$endif}
end;
procedure start();
begin
{$ifdef windows}
ExecuteProcess( 'NET.Exe', [
'start',
'SockFTPD'
] );
{$endif}
{$ifdef linux}
fpexecl( '/etc/init.d/sockftpd', [ 'start' ] );
{$endif}
end;
procedure stop();
begin
{$ifdef windows}
ExecuteProcess( 'NET.Exe', [
'stop',
'SockFTPD'
]);
{$endif}
{$ifdef linux}
fpexecl( '/etc/init.d/sockftpd', [ 'stop' ] );
{$endif}
end;
begin
try
if ( paramstr(1) = '--install' ) then
install()
else
if ( paramstr(1) = '--uninstall' ) then
uninstall()
else
if ( paramstr(1) = '--start' ) then
start()
else
if ( paramstr(1) = '--stop' ) then
stop()
else
usage();
except
On E: Exception Do
begin
writeln();
writeln();
writeln( 'ERROR:' );
writeln( E.Message );
end;
end;
end.