-
Notifications
You must be signed in to change notification settings - Fork 0
/
ocklinux.pas
284 lines (247 loc) · 8.79 KB
/
ocklinux.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
unit OckLinux;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, Process, StrUtils, FileUtil, LazFileUtils, osRunCommandElevated, osLog, OckPathsUtils;
{ TPathsOnClientLinux }
type
TPathsOnClientLinux = class(TPathsOnClient)
private
procedure CopyCustomSettingsToWriteableFolder;
procedure SetAdminMode(theAdminMode: boolean); override;
public
procedure SetAdminModePaths; override;
procedure SetUserModePaths; override;
//constructor Create; override;
//destructor Destroy; override;
end;
{ TPathsOnDepotLinux }
TPathsOnDepotLinux = class(TPathsOnDepot)
procedure SetDepotPaths; override;
end;
function isAdmin: boolean;
function GetUserName_: string;
procedure MountDepot(const User: string; Password: string; PathToDepot: string);
procedure UmountDepot(const PathToDepot: string);
function IsDepotMounted(const PathToDepot: string):boolean;
function Copy(Source:string; Destination:string):boolean;
function PasswordCorrect:boolean;
var
RunCommandElevated: TRunCommandElevated;
PathsOnClient: TPathsOnClientLinux;
PathsOnDepot: TPathsOnDepotLinux;
const
// Include paths here. Setting of the used paths (admin mode vs. user mode) then occures in TOckPathsMacOS
MountPoint = '/mnt/opsi_depot_rw';
DefaultFolder = '/default';
CustomFolder = '/ock_custom';
AbsolutePathCustomSettingsAdminMode = '/tmp/opsi-client-kiosk' + CustomFolder;
AbsolutePathSettingsUserMode = '/opt/opsi-client-kiosk';
//AbsolutePathCustomSettingsUserMode = '/opt/opsi-client-kiosk' + CustomFolder;
RelativePathProductIcons = '/product_icons';
RelativePathScreenShots = '/screenshots';
RelativePathSkin ='/skin';
{$IFDEF KIOSK_IN_AGENT} //if the kiosk is in the opsi-client-agent product
PathKioskAppOnDepot = '/opsi-client-agent/files/opsi/opsiclientkiosk/app';
{$ELSE} //if the kiosk is a standalone opsi product
PathKioskAppOnDepot = '/l-opsi-client-kiosk/files/app';
{$ENDIF KIOSK_IN_AGENT}
implementation
var
MountPointAlreadyExists: boolean = False; //exists the mountpoint already in the system?
function isAdmin: boolean;
begin
Result := True;
end;
function GetUserName_: string;
var
Output: string;
begin
RunCommand('/bin/sh', ['-c','echo $USER'], Output, [poUsePipes, poWaitOnExit], swoHIDE);
//LogDatei.log('opsi-client-kiosk used by: ' + Output , LLInfo);
Result := Trim(Output);
end;
function EscapeReservedURIChars(theString:string):String;
const
ALPHA = ['A'..'Z', 'a'..'z'];
DIGIT = ['0'..'9'];
UNRESERVED = ALPHA + DIGIT + ['-', '.', '_', '~'];
var
i:integer;
begin
Result := '';
for i := 1 to length(theString) do
begin
if (theString[i] in UNRESERVED) then Result := Result + theString[i]
else Result := Result + '%' + IntToHex(ord(theString[i]));
end;
end;
procedure MountDepot(const User: string; Password: string; PathToDepot: string);
var
ShellCommand: string;
ShellOutput: string;
begin
try
LogDatei.log('Mounting ' + PathToDepot + ' on' + MountPoint , LLInfo);
{set shell and options}
if assigned(RunCommandElevated) then
begin
//RunCommandElevated.Shell := '/bin/sh'; //not necessary to set because this is the default value
//RunCommandElevated.ShellOptions := '-c'; //not necessary to set because this is the default value
if not DirectoryExists(MountPoint) then
begin
RunCommandElevated.Run('mkdir ' + MountPoint, ShellOutput);
MountPointAlreadyExists := False;
end
else MountPointAlreadyExists := True;
ShellCommand := 'mount -t cifs' + ' '
+ '-o username=' + User + ',' + 'password=' + Password + ' '
+ PathToDepot + ' '
+ MountPoint;
if RunCommandElevated.Run(ShellCommand, ShellOutput, True) then
begin
ShellCommand := '';
LogDatei.log('Mounting done', LLInfo);
//ShowMessage(ShellOutput);
end
else
begin
LogDatei.log('Error while trying to run command mount for path: ' + PathToDepot, LLError);
end;
end
else
begin
LogDatei.log('RunCommandElevated not assigned',LLError);
end;
except
LogDatei.log('Exception during mounting of ' + PathToDepot, LLDebug);
end;
end;
procedure UmountDepot(const PathToDepot: string);
var
ShellCommand, ShellOutput: string;
begin
try
LogDatei.log('Umount ' + PathToDepot, LLInfo);
{Run Command}
if assigned(RunCommandElevated) then
begin
//RunCommandElevated.Shell := '/bin/sh'; //not necessary to set because this is the default value
//RunCommandElevated.ShellOptions := '-c'; //not necessary to set because this is the default value
ShellCommand := 'umount' + ' ' + PathToDepot;
if RunCommandElevated.Run(ShellCommand, ShellOutput) then
begin
LogDatei.log('Umount done', LLInfo);
if DirectoryExists(MountPoint) and (not MountPointAlreadyExists) then RunCommandElevated.Run('rm -d ' + MountPoint, ShellOutput)
//ShowMessage(ShellOutput);
end
else
begin
LogDatei.log('Error while trying to run command.', LLError);
end;
end
else
begin
LogDatei.log('RunCommandElevated not assigned',LLError);
end;
except
LogDatei.log('Exception during unmounting of ' + PathToDepot, LLDebug);
end;
end;
function IsDepotMounted(const PathToDepot:string): boolean;
var
ShellOutput:String;
begin
ShellOutput := '';
RunCommand('/bin/sh', ['-c', 'mount | grep -i ' + '"' + PathToDepot + '"'], ShellOutput);
if ShellOutput <> '' then
begin
Result := True;
LogDatei.log(PathToDepot + ' already mounted', LLInfo);
end
else
begin
Result := False;
LogDatei.log(PathToDepot + ' not mounted', LLInfo);
end;
end;
function Copy(Source: string; Destination: string): boolean;
var
Output: string;
begin
Output := '';
Result := RunCommandElevated.Run('cp -r --remove-destination' + ' '+ Source + ' ' + Destination, Output);
end;
function PasswordCorrect: boolean;
var
Output:string;
begin
if RunCommandElevated.Run('echo "Passwort korrekt"', Output) then
begin
Result := ContainsStr(Output, 'Passwort korrekt');
end
else Result := False;
end;
{ TPathsOnDepotLinux }
procedure TPathsOnDepotLinux.SetDepotPaths;
begin
FKioskApp := MountPoint + PathKioskAppOnDepot;
FCustomSettings := FKioskApp;
FCustomIcons := FCustomSettings + RelativePathProductIcons;
FCustomScreenShots := FCustomSettings + RelativePathScreenShots;
end;
{ TPathsOnClientLinux }
procedure TPathsOnClientLinux.CopyCustomSettingsToWriteableFolder;
var
Output: string;
begin
//RunCommand('/bin/sh',
// ['-c','cp -R' + ' ' + AbsolutePathCustomSettingsUserMode + ' ' + AbsolutePathCustomSettingsAdminMode ],
// Output, [poUsePipes, poWaitOnExit], swoHIDE);
LogDatei.log('Removing old settings from ' + AbsolutePathCustomSettingsAdminMode, LLInfo);
if DeleteDirectory(AbsolutePathCustomSettingsAdminMode, False) then
begin
LogDatei.log('Removing old settings done', LLInfo);
end;
CopyDirTree(FKioskApp + CustomFolder, AbsolutePathCustomSettingsAdminMode,[cffOverwriteFile, cffCreateDestDirectory]);
end;
procedure TPathsOnClientLinux.SetAdminMode(theAdminMode: boolean);
begin
inherited SetAdminMode(theAdminMode);
if FAdminMode then CopyCustomSettingsToWriteableFolder;
end;
procedure TPathsOnClientLinux.SetAdminModePaths;
begin
FKioskApp := ChompPathDelim(ProgramDirectory);
//Default
FDefaultSettings := FKioskApp + DefaultFolder;
FDefaultIcons := FDefaultSettings + RelativePathProductIcons;
FDefaultSkin := FDefaultSettings + RelativePathSkin;
//Custom
FCustomSettings := AbsolutePathCustomSettingsAdminMode;
FCustomSkin := FCustomSettings + RelativePathSkin;
FCustomIcons := FCustomSettings + RelativePathProductIcons;
FCustomScreenShots := FCustomSettings + RelativePathScreenShots;
end;
procedure TPathsOnClientLinux.SetUserModePaths;
begin
FKioskApp := ChompPathDelim(ProgramDirectory);
//Dfault
FDefaultSettings := FKioskApp + DefaultFolder;
FDefaultIcons := FDefaultSettings + RelativePathProductIcons;
FDefaultSkin := FDefaultSettings + RelativePathSkin;
//Custom
FCustomSettings := FKioskApp + CustomFolder;
FCustomSkin := FCustomSettings + RelativePathSkin;
FCustomIcons := FCustomSettings + RelativePathProductIcons;
FCustomScreenShots := FCustomSettings + RelativePathScreenShots;
end;
initialization
RunCommandElevated := TRunCommandElevated.Create('',True);
PathsOnClient := TPathsOnClientLinux.Create;
PathsOnDepot := TPathsOnDepotLinux.Create;
finalization
FreeAndNil(RunCommandElevated);
FreeAndNil(PathsOnClient);
FreeAndNil(PathsOnDepot);
end.