-
Notifications
You must be signed in to change notification settings - Fork 0
/
ockmacos.pas
326 lines (282 loc) · 10.9 KB
/
ockmacos.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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
unit OckMacOS;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, Process, StrUtils, FileUtil, LazFileUtils, URIParser, osRunCommandElevated, osLog, OckPathsUtils;
type
{ TPathsOnClientMacOS }
TPathsOnClientMacOS = class(TPathsOnClient)
private
procedure CopyCustomSettingsToWriteableFolder;
public
procedure SetAdminMode(theAdminMode: boolean); override;
procedure SetAdminModePaths; override;
procedure SetUserModePaths; override;
//constructor Create; override;
//destructor Destroy; override;
end;
{ TPathsOnDepotMacOS }
TPathsOnDepotMacOS = 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: TPathsOnClientMacOS;
PathsOnDepot: TPathsOnDepotMacOS;
const
// Include paths here. Setting of the used paths (admin mode vs. user mode) then occures in TOckPathsMacOS
MountPoint = '/tmp/opsi_depot_rw';
DefaultFolder = '/default';
CustomFolder = '/ock_custom';
AbsolutePathSettingsAdminMode = '/tmp/org.opsi.OpsiClientKiosk';
AbsolutePathCustomSettingsAdminMode = AbsolutePathSettingsAdminMode + CustomFolder;
AbsolutePathSettingsUserMode = '/Library/Application Support/org.opsi.OpsiClientKiosk';
AbsolutePathCustomSettingsUserMode = AbsolutePathSettingsUserMode + CustomFolder;
RelativePathDefaultSettings = '/../Resources' + DefaultFolder;
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 = '/m-opsi-client-kiosk/files/app';
{$ENDIF KIOSK_IN_AGENT}
implementation
var
MountPointAlreadyExists: boolean = False; //exists the mountpoint already in the system?
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;
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;
procedure MountDepot(const User: string; Password: string; PathToDepot: string);
var
ShellCommand: string;
ShellOutput: string;
URI: TURI;
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
//if RunCommandElevated.Run('mkdir ' + MountPoint, ShellOutput) then
if RunCommand('/bin/sh', ['-c', 'mkdir ' + MountPoint], ShellOutput) then
begin
LogDatei.log('mkdir ' + MountPoint + ' done ', LLInfo);
end
else
begin
LogDatei.log('Error while trying to run command "mkdir ' + MountPoint + '"', LLError);
end;
MountPointAlreadyExists := False;
end
else MountPointAlreadyExists := True;
Delete(PathToDepot, 1, 2);
ShellCommand := 'mount_smbfs' + ' '
+ '//' + User+ ':' + EscapeReservedURIChars(Password) + '@'
+ PathToDepot + ' '
+ MountPoint;
//if RunCommandElevated.Run(ShellCommand, ShellOutput, True) then
if RunCommand('/bin/sh', ['-c', ShellCommand], ShellOutput,[poWaitOnExit, poUsePipes], swoHide) then
begin
ShellCommand := '';
LogDatei.log('Mounting done', LLInfo);
//ShowMessage(ShellOutput);
end
else
begin
LogDatei.log('Error while trying to run command: ' + ShellCommand, 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
if RunCommand('/bin/sh', ['-c', ShellCommand], ShellOutput,[poWaitOnExit, poUsePipes], swoHide) then
begin
LogDatei.log('Umount done', LLInfo);
if DirectoryExists(MountPoint) and (not MountPointAlreadyExists) then
RunCommand('/bin/sh', ['-c', 'rm -d ' + MountPoint], ShellOutput,[poWaitOnExit, poUsePipes], swoHide)
//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 := '';
if RunCommand('/bin/sh', ['-c', 'rm -r' + ' ' + Destination + CustomFolder], Output) then
LogDatei.log('Remove ock_custom before copying', LLInfo)
else
LogDatei.log('Could not remove ock_custom before copying (' + Destination + CustomFolder+'): ' + Output, LLWarning);
Result := RunCommand('/bin/sh', ['-c', 'cp -fR' + ' '+ Source + ' ' + Destination], Output);
//Result := RunCommandElevated.Run('cp -fR' + ' '+ 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;
{ TPathsOnDepotMacOS }
procedure TPathsOnDepotMacOS.SetDepotPaths;
begin
FKioskApp := MountPoint + PathKioskAppOnDepot;
FCustomSettings := FKioskApp;
FCustomIcons := FCustomSettings + RelativePathProductIcons;
FCustomScreenShots := FCustomSettings + RelativePathScreenShots;
end;
{ TPathsOnClientMacOS}
procedure TPathsOnClientMacOS.CopyCustomSettingsToWriteableFolder;
var
Output: string;
begin
//RunCommand('/bin/sh',
// ['-c','cp -R' + ' ' + AbsolutePathCustomSettingsUserMode + ' ' + AbsolutePathCustomSettingsAdminMode ],
// Output, [poUsePipes, poWaitOnExit], swoHIDE);
LogDatei.log('Removing old settings from ' + AbsolutePathSettingsAdminMode, LLInfo);
if DeleteDirectory(AbsolutePathSettingsAdminMode, False) then
begin
LogDatei.log('Removing old settings done', LLInfo);
end;
if CopyDirTree(AbsolutePathSettingsUserMode, AbsolutePathSettingsAdminMode,[cffOverwriteFile, cffCreateDestDirectory]) then
LogDatei.log('Copy settings from ' + AbsolutePathSettingsUserMode + ' to ' + AbsolutePathSettingsAdminMode + ' done', LLInfo)
else
LogDatei.log('Copy settings from ' + AbsolutePathSettingsUserMode + ' to ' + AbsolutePathSettingsAdminMode + ' failed', LLError);
if not DirectoryExists(AbsolutePathCustomSettingsAdminMode) then
CreateDir(AbsolutePathCustomSettingsAdminMode);
if not DirectoryExists(AbsolutePathCustomSettingsAdminMode + RelativePathSkin) then
if CreateDir(AbsolutePathCustomSettingsAdminMode + RelativePathSkin) then
LogDatei.log('Directory ' + AbsolutePathCustomSettingsAdminMode + RelativePathSkin + ' created', LLInfo)
else
LogDatei.log('Directory ' + AbsolutePathCustomSettingsAdminMode + RelativePathSkin + ' could not be created', LLError);
if not DirectoryExists(AbsolutePathCustomSettingsAdminMode + RelativePathProductIcons) then
CreateDir(AbsolutePathCustomSettingsAdminMode + RelativePathProductIcons);
if not DirectoryExists(AbsolutePathCustomSettingsAdminMode + RelativePathScreenShots) then
CreateDir(AbsolutePathCustomSettingsAdminMode + RelativePathScreenShots);
end;
procedure TPathsOnClientMacOS.SetAdminMode(theAdminMode: boolean);
begin
inherited SetAdminMode(theAdminMode);
if FAdminMode then CopyCustomSettingsToWriteableFolder;
end;
procedure TPathsOnClientMacOS.SetAdminModePaths;
begin
FKioskApp := ChompPathDelim(ProgramDirectory);
//Default
FDefaultSettings := FKioskApp + RelativePathDefaultSettings;
FDefaultIcons := FDefaultSettings + RelativePathProductIcons;
FDefaultSkin := FDefaultSettings + RelativePathSkin;
//Custom
FCustomSettings := AbsolutePathCustomSettingsAdminMode;
FCustomSkin := FCustomSettings + RelativePathSkin;
FCustomIcons := FCustomSettings + RelativePathProductIcons;
FCustomScreenShots := FCustomSettings + RelativePathScreenShots;
end;
procedure TPathsOnClientMacOS.SetUserModePaths;
begin
FKioskApp := ChompPathDelim(ProgramDirectory);
//Dfault
FDefaultSettings := FKioskApp + RelativePathDefaultSettings;
FDefaultIcons := FDefaultSettings + RelativePathProductIcons;
FDefaultSkin := FDefaultSettings + RelativePathSkin;
//Custom
FCustomSettings := AbsolutePathCustomSettingsUserMode;
FCustomSkin := FCustomSettings + RelativePathSkin;
FCustomIcons := FCustomSettings + RelativePathProductIcons;
FCustomScreenShots := FCustomSettings + RelativePathScreenShots;
end;
initialization
RunCommandElevated := TRunCommandElevated.Create('',True);
PathsOnClient := TPathsOnClientMacOS.Create;
PathsOnDepot := TPathsOnDepotMacOS.Create;
finalization
FreeAndNil(RunCommandElevated);
FreeAndNil(PathsOnClient);
FreeAndNil(PathsOnDepot);
end.