Debian Codegraph @

lib/modules/sambaSamAccount/sambaMungedDial.inc

raw
1 <?php
2 /*
3 This code is part of GOsa (https://gosa.gonicus.de)
4 Copyright (C) 2004 Cajus Pollmeier
5 2024 - 2026 Roland Gruber
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
22
23 /**
24 * Manages terminal server settings for Samba 3.
25 *
26 * @package modules
27 * @author Cajus Pollmeier
28 */
29
30 /**
31 * File header
32 */
33 const SAMBA_MUNGEDDIAL_FILEHEADER = "20002000200020002000200020002000" .
34 "20002000200020002000200020002000" .
35 "20002000200020002000200020002000" .
36 "20002000200020002000200020002000" .
37 "20002000200020002000200020002000" .
38 "20002000200020002000200020002000" .
39 "5000";
40 /**
41 * File header for old format.
42 */
43 const SAMBA_MUNGEDDIAL_FILEHEADER_OLD = "6d000800200020002000200020002000" .
44 "20002000200020002000200020002000" .
45 "20002000200020002000200064000100" .
46 "20002000200020002000200020002000" .
47 "20002000200020002000200020002000" .
48 "20002000200020002000200020002000" .
49 "50001000";
50
51 /**
52 * Manages terminal server settings for Samba 3.
53 *
54 * @author Cajus Pollmeier
55 * @package modules
56 */
57 class sambaMungedDial {
58 /** @var array<string, string> $ctx Terminal server variables (samba3) */
59 public array $ctx = [
60 'CtxCfgPresent' => '551e0bb0',
61 'CtxCfgFlags1' => '00e00010',
62 'CtxCallback' => '',
63 'CtxShadow' => '01000000',
64 'CtxMaxConnectionTime' => '',
65 'CtxMaxDisconnectionTime' => '',
66 'CtxMaxIdleTime' => '',
67 'CtxKeyboardLayout' => '',
68 'CtxMinEncryptionLevel' => '00',
69 'CtxWorkDirectory' => '',
70 'CtxNWLogonServer' => '',
71 'CtxWFHomeDir' => '',
72 'CtxWFHomeDirDrive' => '',
73 'CtxWFProfilePath' => '',
74 'CtxInitialProgram' => '',
75 'CtxCallbackNumber' => ''];
76
77 /** @var string[] $stringParams These parameters are treated as strings and get a trailing zero */
78 private array $stringParams = ["CtxWorkDirectory", "CtxNWLogonServer",
79 "CtxWFHomeDir", "CtxWFHomeDirDrive",
80 "CtxWFProfilePath", "CtxInitialProgram", "CtxCallbackNumber"];
81
82 /** @var string[] $timeParams These parameters are treated as time values and get converted */
83 private array $timeParams = ["CtxMaxConnectionTime", "CtxMaxDisconnectionTime", "CtxMaxIdleTime"];
84
85 private bool $old_behavior = false;
86
87 /** strhex
88 * @param string $string string
89 * @return string hex
90 */
91 public function strhex($string): string {
92 $hex = "";
93
94 for ($i = 0; $i < strlen($string); $i++) {
95 $hex .= dechex(ord($string[$i]));
96 }
97
98 return ($hex);
99 }
100
101 /** hexstr
102 * @param string $hex hex value
103 * @return string value
104 */
105 public function hexstr($hex): string {
106 $string = "";
107
108 for ($i = 0; $i < strlen($hex) - 1; $i += 2) {
109 $string .= chr(hexdec($hex[$i] . $hex[$i + 1]));
110 }
111
112 return ($string);
113 }
114
115 /** endian
116 * @param string $src source
117 * @return string endian changed
118 */
119 public function endian($src): string {
120 return (substr($src, 2, 2) . substr($src, 0, 2));
121 }
122
123 /** genTime
124 * @param string $minutes minutes
125 * @return string time
126 */
127 public function genTime($minutes): string {
128 if ($minutes === '') {
129 $minutes = 0;
130 }
131 $usec = (int) ($minutes * 60 * 1000);
132 $src = sprintf('%04x%04x', $usec & 0x0FFFF, ($usec & 0x0FFFF0000) >> 16);
133 return (sambaMungedDial::endian(substr($src, 0, 4)) . sambaMungedDial::endian(substr($src, 4, 4)));
134 }
135
136 /** readTime
137 * @param string $time time
138 * @return int time
139 */
140 public function readTime($time): int {
141 $lo = substr($time, 0, 4);
142 $hi = substr($time, 4, 4);
143
144 $usecs = (hexdec(substr($lo, 2, 2)) * 256 + hexdec(substr($lo, 0, 2))) +
145 (hexdec(substr($hi, 2, 2)) * 256 + hexdec(substr($hi, 0, 2))) * 256 * 256;
146
147 return ((int) ($usecs / (60 * 1000)));
148 }
149
150 /** to8bit
151 * @param string $string string
152 * @return string 8bit string
153 */
154 public function to8bit($string): string {
155 $result = "";
156
157 /* Strip zeros */
158 for ($i = 0; $i < strlen($string); $i++) {
159 if ($string[$i] != chr(0)) {
160 $result .= $string[$i];
161 }
162 }
163
164 return ($result);
165 }
166
167 /** Encode full MungedDial-String
168 * @param array<string, string> $params parameters
169 * @return string result
170 */
171 public function encode_munged($params): string {
172 /* Walk through the parameters and convert them */
173 $result = sambaMungedDial::hexstr(SAMBA_MUNGEDDIAL_FILEHEADER);
174
175 // CHANGED: We need to insert the number of attributes right after SAMBA_MUNGEDDIAL_FILEHEADER.
176 $counter = 0;
177 $result_tmp = "";
178 foreach ($params as $paramName => $paramValue) {
179 /* String parameter? */
180 if (in_array($paramName, $this->stringParams)) {
181 $isString = TRUE;
182 $paramValue = sambaMungedDial::strhex($paramValue . chr(0) . chr(0));
183 }
184 else {
185 $isString = FALSE;
186 }
187
188 /* Time parameter? */
189 if (in_array($paramName, $this->timeParams)) {
190 $paramValue = sambaMungedDial::genTime($paramValue);
191 }
192
193 $result_tmp .= sambaMungedDial::munge($paramName, $paramValue, $isString);
194 $counter++;
195 }
196
197 // First add the number of attributes
198 $result .= sambaMungedDial::hexstr(sprintf("%02x00", $counter));
199
200 // Then the usual stuff
201 $result .= $result_tmp;
202
203 return ($result);
204 }
205
206 /** Setup parameter given by paramName to MungedDial-Format
207 * @param string $paramName param name
208 * @param string $paramValue value
209 * @param bool $isString is string
210 * @return string value
211 */
212 public function munge($paramName, $paramValue, $isString): string {
213 $result = "";
214
215 /* Encode paramName to UTF-16 */
216 if (function_exists("recode")) {
217 $utfName = recode("ISO8859-15..UTF-16", $paramName);
218 }
219 else {
220 $utfName = iconv("ISO8859-15", "UTF-16BE", $paramName);
221 }
222
223 /* Set parameter length, high and low byte */
224 $paramLen = strlen($utfName);
225 $result .= chr($paramLen & 0x0FF);
226 $result .= chr(($paramLen & 0x0FF00) >> 8);
227
228 /* String parameters have additional trailing bytes */
229 $valueLen = strlen($paramValue);
230 $result .= chr($valueLen & 0x0FF);
231 $result .= chr(($valueLen & 0x0FF00) >> 8);
232
233 /* Length fields have a trailing '01' appended by the UTF-16 converted name */
234 $result .= chr(1);
235 $result .= $utfName;
236
237 /* Parameter is padded with '00' */
238 $result .= chr(0);
239 $result .= $paramValue;
240
241 /* Append a trailing '00' to string parameters */
242 if ($isString && (strlen($paramValue) & 1)) {
243 $result .= chr(0);
244 }
245
246 return ($result);
247 }
248
249 /** Takes a base64-encoded MungedDial-String and returns an array of included parameters and values
250 * @param string $munge munge
251 * @return array<string, string> values
252 */
253 public function decode_munged($munge): array {
254 $result = [];
255
256 /*
257 * Remove base64 encoding and skip SAMBA_MUNGEDDIAL_FILEHEADER.
258 * The '4' is added, because the SAMBA_MUNGEDDIAL_FILEHEADER has been stripped by 4 chars.
259 * This is the number of attributes following - we don't need this at read time, only when writing.
260 */
261 if (str_starts_with(base64_decode($munge), "6d")) {
262 $this->old_behavior = true;
263 }
264
265 if ($this->old_behavior) {
266 $ctxField = substr(base64_decode($munge), (strlen(SAMBA_MUNGEDDIAL_FILEHEADER_OLD)) / 2);
267 }
268 else {
269 $ctxField = substr(base64_decode($munge), (strlen(SAMBA_MUNGEDDIAL_FILEHEADER) + 4) / 2);
270 }
271
272 /* Decode parameters */
273 while ($ctxField !== "") {
274
275 /* Read value lengths */
276 $ctxParmNameLength = ord($ctxField[0]) + 16 * ord($ctxField[1]);
277 $ctxParmLength = ord($ctxField[2]) + 16 * ord($ctxField[3]);
278
279 /* Reposition ctxField on start of parameter name, read parameter name */
280 $ctxField = substr($ctxField, 6);
281 $ctxParmName = sambaMungedDial::to8bit(substr($ctxField, 0, $ctxParmNameLength));
282
283 /* Reposition ctxField on start of parameter */
284 $ctxField = substr($ctxField, $ctxParmNameLength);
285 $ctxParm = substr($ctxField, 0, $ctxParmLength);
286
287 /* If string parameter, convert */
288 if (in_array($ctxParmName, $this->stringParams)) {
289 $ctxParm = sambaMungedDial::hexstr($ctxParm);
290 }
291 /* If time parameter, convert */
292 if (in_array($ctxParmName, $this->timeParams)) {
293 $ctxParm = sambaMungedDial::readTime($ctxParm);
294 }
295
296 /* Assign in result array */
297 $result[$ctxParmName] = trim($ctxParm);
298
299 /* Reposition ctxField on end of parameter and continue */
300 $ctxField = substr($ctxField, $ctxParmLength);
301 }
302
303 return ($result);
304 }
305
306 /** function takes a base64-encoded sambaMungedDial
307 * @param string $mungedDial munged
308 */
309 public function load($mungedDial): void {
310 $this->ctx = $this->decode_munged($mungedDial);
311 }
312
313 /** Returns ready-to-run mungedDialString to be filled into ldap
314 * @return string value
315 */
316 public function getMunged(): string {
317 // Do extra check for valid timeParams (they must be set to 0 if disabled)
318 foreach ($this->timeParams as $value) {
319 if (!isset($this->ctx[$value])) {
320 $this->ctx[$value] = 0;
321 }
322 }
323 return base64_encode($this->encode_munged($this->ctx));
324 }
325
326 /** Returns array of flags, which can be set on-demand with activated java-script
327 * @return array<string, string> values
328 */
329 public function getOnDemandFlags(): array {
330 $result = [];
331 foreach ($this->timeParams as $value) {
332 $result[$value . "Mode"] = "";
333 }
334 $result['CtxInitialProgramMode'] = "";
335 return $result;
336 }
337
338 /** Gets Terminal-Server-Login value: enabled/disabled
339 * @return bool enabled
340 */
341 public function getTsLogin(): bool {
342 $flags = ord(substr($this->ctx['CtxCfgFlags1'], 5, 1));
343 return ($flags & 1) === 0;
344 }
345
346 /** Sets Terminal-Server-Login value: enabled/disabled
347 * @param bool $checked checked
348 */
349 public function setTsLogin($checked): void {
350 $flag = (int) substr($this->ctx['CtxCfgFlags1'], 5, 1);
351
352 if ($checked) {
353 $flag |= 1;
354 }
355 else {
356 $flag &= 0xFE;
357 }
358
359 $this->ctx['CtxCfgFlags1'][5] = sprintf('%1x', $flag);
360 }
361
362 /** gets Broken-Connection value: disconnect/reset
363 * @return string value
364 */
365 public function getBrokenConn(): string {
366 $flags = ord(substr($this->ctx['CtxCfgFlags1'], 5, 1));
367 return (($flags & 4) !== 0) ? "1" : "0";
368 }
369
370 /** sets Broken-Connection value: disconnect/reset
371 * @param bool $checked checked
372 */
373 public function setBrokenConn($checked): void {
374 $flag = (int) substr($this->ctx['CtxCfgFlags1'], 5, 1);
375
376 if ($checked) {
377 $flag |= 4;
378 }
379 else {
380 $flag &= 0xFB;
381 }
382
383 $this->ctx['CtxCfgFlags1'][5] = sprintf('%1x', $flag);
384 }
385
386 /** gets Reconnection value: from any client/from previous client only
387 * @return string value
388 */
389 public function getReConn(): string {
390 $flags = ord(substr($this->ctx['CtxCfgFlags1'], 5, 1));
391 return (($flags & 2) !== 0) ? "1" : "0";
392 }
393
394 /** sets Reconnection value: from any client/from previous client only
395 * @param bool $checked checked
396 */
397 public function setReConn($checked): void {
398 $flag = (int) substr($this->ctx['CtxCfgFlags1'], 5, 1);
399
400 if ($checked) {
401 $flag |= 2;
402 }
403 else {
404 $flag &= 0xFD;
405 }
406
407 $this->ctx['CtxCfgFlags1'][5] = sprintf('%1x', $flag);
408 }
409
410 /** gets Inherit-config-from-client value: enabled/disabled
411 * @return bool enabled
412 */
413 public function getInheritMode(): bool {
414 return substr($this->ctx['CtxCfgFlags1'], 6, 1) === "1";
415 }
416
417 /** sets Inherit-config-from-client value: enabled/disabled
418 * @param bool $checked checked
419 */
420 public function setInheritMode($checked): void {
421 $this->ctx['CtxCfgFlags1'][6] = $checked ? "1" : "0";
422 }
423
424 /** gets shadow value (enum): 0-4
425 * 0: disabled
426 * 1: input on, notify on
427 * 2: input on, notify off
428 * 3: input off, notify on
429 * 4: input off, notify off
430 * @return string value
431 */
432 public function getShadow(): string {
433 return $this->old_behavior ? substr($this->ctx['CtxCfgFlags1'], 1, 1) : substr($this->ctx['CtxShadow'], 1, 1);
434 }
435
436 /** sets shadow value
437 * @param bool $checked checked
438 * @param string $value value
439 */
440 public function setShadow($checked, $value): void {
441 if ($checked) {
442 if ($this->old_behavior) {
443 // We need to reset the old setting
444 $this->ctx['CtxCfgFlags1'][1] = sprintf('%1X', $value);
445 }
446 $this->ctx['CtxShadow'][1] = sprintf('%1x', $value);
447 }
448 }
449
450 /** gets connect-client-drive-at-logon value: enabled/disabled
451 * @return int enabled
452 */
453 public function getConnectClientDrives(): int {
454 $connections = hexdec(substr($this->ctx['CtxCfgFlags1'], 2, 1));
455
456 return $connections & 8;
457 }
458
459 /** sets connect-client-drive-at-logon value: enabled/disabled
460 * @param bool $checked checked
461 */
462 public function setConnectClientDrives($checked): void {
463 $flag = hexdec(substr($this->ctx['CtxCfgFlags1'], 2, 1));
464 if ($checked) {
465 $flag |= 8;
466 }
467 else {
468 $flag &= 0xF7;
469 }
470
471 $this->ctx['CtxCfgFlags1'][2] = sprintf('%1x', $flag);
472 }
473
474 /** gets connect-client-printers-at-logon value: enabled/disabled
475 * @return int enabled
476 */
477 public function getConnectClientPrinters(): int {
478 $connections = hexdec(substr($this->ctx['CtxCfgFlags1'], 2, 1));
479
480 return $connections & 4;
481 }
482
483 /** sets connect-client-printers-at-logon value: enabled/disabled
484 * @param bool $checked checked
485 */
486 public function setConnectClientPrinters($checked): void {
487 $flag = hexdec(substr($this->ctx['CtxCfgFlags1'], 2, 1));
488
489 if ($checked) {
490 $flag |= 4;
491 }
492 else {
493 $flag &= 0xFB;
494 }
495
496 $this->ctx['CtxCfgFlags1'][2] = sprintf('%1x', $flag);
497 }
498
499 /** gets set-client-printer-to-default value: enabled/disabled
500 * @return int enabled
501 */
502 public function getDefaultPrinter(): int {
503 $connections = hexdec(substr($this->ctx['CtxCfgFlags1'], 2, 1));
504
505 return $connections & 2;
506 }
507
508 /** sets set-client-printer-to-default value: enabled/disabled
509 * @param bool $checked checked
510 */
511 public function setDefaultPrinter($checked): void {
512 $flag = hexdec(substr($this->ctx['CtxCfgFlags1'], 2, 1));
513
514 if ($checked) {
515 $flag |= 2;
516 }
517 else {
518 $flag &= 0xFD;
519 }
520
521 $this->ctx['CtxCfgFlags1'][2] = sprintf('%1x', $flag);
522 }
523
524 /** SMARTY: gets the checkbox state of "Connection" */
525 public function getCtxMaxConnectionTimeF(): bool {
526 // Connection Time is 0 if disabled
527 return isset($this->ctx['CtxMaxConnectionTime']) && ($this->ctx['CtxMaxConnectionTime'] != 0);
528 }
529
530 /** SMARTY: sets the checkbox "Connection" to unchecked
531 * @param bool $checked checked
532 */
533 public function setCtxMaxConnectionTimeF($checked): void {
534 if ($checked) {
535 unset ($this->ctx['CtxMaxConnectionTime']);
536 }
537 }
538
539 /** SMARTY: gets the checkbox state of "Disconnection"
540 * @return bool disconnection time set
541 */
542 public function getCtxMaxDisconnectionTimeF(): bool {
543 // Connection Time is 0 if disabled
544 return isset($this->ctx['CtxMaxDisconnectionTime']) && ($this->ctx['CtxMaxDisconnectionTime'] != 0);
545 }
546
547 /** SMARTY: sets the checkbox "Disconnection" to unchecked
548 * @param bool $checked checked
549 */
550 public function setCtxMaxDisconnectionTimeF($checked): void {
551 if ($checked) {
552 unset ($this->ctx['CtxMaxDisconnectionTime']);
553 }
554 }
555
556 /** SMARTY: gets the checkbox state of "Idle"
557 * @return bool is idle time set
558 */
559 public function getCtxMaxIdleTimeF(): bool {
560 // Connection Time is 0 if disabled
561 return isset($this->ctx['CtxMaxIdleTime']) && ($this->ctx['CtxMaxIdleTime'] != 0);
562 }
563
564 /** SMARTY: sets the checkbox "Idle" to unchecked
565 * @param bool $checked checked
566 */
567 public function setCtxMaxIdleTimeF($checked): void {
568 if ($checked) {
569 unset ($this->ctx['CtxMaxIdleTime']);
570 }
571 }
572 }