ser2netweb 3.0.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (121) hide show
  1. ser2netweb/__init__.py +20 -0
  2. ser2netweb/__main__.py +6 -0
  3. ser2netweb/accounts.py +503 -0
  4. ser2netweb/cli.py +489 -0
  5. ser2netweb/clock.py +128 -0
  6. ser2netweb/config.py +4491 -0
  7. ser2netweb/console.py +181 -0
  8. ser2netweb/datalock.py +181 -0
  9. ser2netweb/engine/__init__.py +0 -0
  10. ser2netweb/engine/access.py +173 -0
  11. ser2netweb/engine/bindhint.py +115 -0
  12. ser2netweb/engine/bridge.py +1928 -0
  13. ser2netweb/engine/cluster.py +1140 -0
  14. ser2netweb/engine/modbus.py +530 -0
  15. ser2netweb/engine/modbus_gateway.py +1465 -0
  16. ser2netweb/engine/mqtt_pub.py +632 -0
  17. ser2netweb/engine/netguard.py +805 -0
  18. ser2netweb/engine/netinfo.py +199 -0
  19. ser2netweb/engine/portdiag.py +414 -0
  20. ser2netweb/engine/portlist.py +471 -0
  21. ser2netweb/engine/protocols/__init__.py +28 -0
  22. ser2netweb/engine/protocols/base.py +117 -0
  23. ser2netweb/engine/protocols/raw.py +12 -0
  24. ser2netweb/engine/protocols/rfc2217.py +586 -0
  25. ser2netweb/engine/protocols/telnet.py +172 -0
  26. ser2netweb/engine/serial_compat.py +598 -0
  27. ser2netweb/engine/serial_io.py +210 -0
  28. ser2netweb/engine/serial_link.py +414 -0
  29. ser2netweb/engine/sockopts.py +103 -0
  30. ser2netweb/engine/sparkplug.py +538 -0
  31. ser2netweb/engine/sparkplug_node.py +550 -0
  32. ser2netweb/engine/supervisor.py +533 -0
  33. ser2netweb/engine/tls.py +232 -0
  34. ser2netweb/envvars.py +57 -0
  35. ser2netweb/explain.py +117 -0
  36. ser2netweb/healthcheck.py +78 -0
  37. ser2netweb/i18n.py +110 -0
  38. ser2netweb/importer.py +550 -0
  39. ser2netweb/layout.py +118 -0
  40. ser2netweb/licences.py +86 -0
  41. ser2netweb/locale/tr.json +1569 -0
  42. ser2netweb/logsetup.py +253 -0
  43. ser2netweb/logtext.py +23 -0
  44. ser2netweb/notify.py +761 -0
  45. ser2netweb/optional.py +76 -0
  46. ser2netweb/reload.py +295 -0
  47. ser2netweb/runtime.py +762 -0
  48. ser2netweb/sdnotify.py +85 -0
  49. ser2netweb/ser2net_import.py +2001 -0
  50. ser2netweb/signins.py +98 -0
  51. ser2netweb/standalone.py +154 -0
  52. ser2netweb/state.py +1457 -0
  53. ser2netweb/storage.py +130 -0
  54. ser2netweb/textfold.py +51 -0
  55. ser2netweb/tlsreload.py +37 -0
  56. ser2netweb/watchdog.py +201 -0
  57. ser2netweb/web/__init__.py +0 -0
  58. ser2netweb/web/api.py +793 -0
  59. ser2netweb/web/auth.py +699 -0
  60. ser2netweb/web/certinfo.py +144 -0
  61. ser2netweb/web/httpguard.py +413 -0
  62. ser2netweb/web/ldap_auth.py +556 -0
  63. ser2netweb/web/oidc_auth.py +330 -0
  64. ser2netweb/web/proxy.py +398 -0
  65. ser2netweb/web/routes/__init__.py +78 -0
  66. ser2netweb/web/routes/cluster.py +669 -0
  67. ser2netweb/web/routes/common.py +359 -0
  68. ser2netweb/web/routes/console.py +451 -0
  69. ser2netweb/web/routes/forms.py +853 -0
  70. ser2netweb/web/routes/login.py +573 -0
  71. ser2netweb/web/routes/mappings.py +470 -0
  72. ser2netweb/web/routes/ops.py +374 -0
  73. ser2netweb/web/routes/settings.py +2135 -0
  74. ser2netweb/web/routes/system.py +308 -0
  75. ser2netweb/web/routes/totp.py +282 -0
  76. ser2netweb/web/selfsigned.py +129 -0
  77. ser2netweb/web/server.py +483 -0
  78. ser2netweb/web/static/app.css +327 -0
  79. ser2netweb/web/static/app.js +1102 -0
  80. ser2netweb/web/static/htmx.min.js +1 -0
  81. ser2netweb/web/static/points.js +218 -0
  82. ser2netweb/web/static/theme.js +7 -0
  83. ser2netweb/web/static/xterm-addon-fit.js +2 -0
  84. ser2netweb/web/static/xterm.css +218 -0
  85. ser2netweb/web/static/xterm.js +2 -0
  86. ser2netweb/web/templates/_cluster_body.html +109 -0
  87. ser2netweb/web/templates/_console.html +21 -0
  88. ser2netweb/web/templates/_footer.html +13 -0
  89. ser2netweb/web/templates/_login_notice.html +8 -0
  90. ser2netweb/web/templates/_macros.html +21 -0
  91. ser2netweb/web/templates/_mapping_form.html +477 -0
  92. ser2netweb/web/templates/_mapping_log.html +28 -0
  93. ser2netweb/web/templates/_mapping_traces.html +44 -0
  94. ser2netweb/web/templates/_mappings_body.html +149 -0
  95. ser2netweb/web/templates/_modbus_read.html +18 -0
  96. ser2netweb/web/templates/_ports_table.html +37 -0
  97. ser2netweb/web/templates/_serial_port_select.html +48 -0
  98. ser2netweb/web/templates/_shell.html +41 -0
  99. ser2netweb/web/templates/audit.html +55 -0
  100. ser2netweb/web/templates/base.html +77 -0
  101. ser2netweb/web/templates/dashboard.html +72 -0
  102. ser2netweb/web/templates/licenses.html +18 -0
  103. ser2netweb/web/templates/login.html +32 -0
  104. ser2netweb/web/templates/login_totp.html +23 -0
  105. ser2netweb/web/templates/settings.html +830 -0
  106. ser2netweb/web/templates/setup.html +43 -0
  107. ser2netweb/web/templates/totp.html +41 -0
  108. ser2netweb/web/totp.py +94 -0
  109. ser2netweb/winservice.py +1793 -0
  110. ser2netweb-3.0.0.dist-info/METADATA +120 -0
  111. ser2netweb-3.0.0.dist-info/RECORD +121 -0
  112. ser2netweb-3.0.0.dist-info/WHEEL +5 -0
  113. ser2netweb-3.0.0.dist-info/entry_points.txt +2 -0
  114. ser2netweb-3.0.0.dist-info/licenses/COMMERCIAL_LICENSE.md +94 -0
  115. ser2netweb-3.0.0.dist-info/licenses/LICENSE +662 -0
  116. ser2netweb-3.0.0.dist-info/licenses/NOTICE +20 -0
  117. ser2netweb-3.0.0.dist-info/licenses/THIRD-PARTY-NOTICES.md +105 -0
  118. ser2netweb-3.0.0.dist-info/licenses/tools/licenses/npm/htmx.org.txt +13 -0
  119. ser2netweb-3.0.0.dist-info/licenses/tools/licenses/npm/xterm-addon-fit.txt +19 -0
  120. ser2netweb-3.0.0.dist-info/licenses/tools/licenses/npm/xterm.txt +21 -0
  121. ser2netweb-3.0.0.dist-info/top_level.txt +1 -0
ser2netweb/signins.py ADDED
@@ -0,0 +1,98 @@
1
+ """When each web-UI account last signed in, and which LDAP/OIDC accounts are unused.
2
+
3
+ The times come from the audit log — its login / oidc_login / first_run_setup records,
4
+ read once at start-up (AppState.load_sign_ins) — and from every session issued since
5
+ (routes.common.Ctx.set_session). They are kept in memory only: writing them to
6
+ config.json would save it, and push a backup out of the 50 kept, at every sign-in.
7
+
8
+ An LDAP/OIDC account is only a shadow of the directory's or identity provider's: it
9
+ comes back at the next sign-in while that still gives the person a role. When an admin
10
+ sets AppConfig.external_users_expire_days (0, the default, keeps every account), one not
11
+ signed in for that many days is removed by the hourly maintenance
12
+ (AppState.expire_external_accounts), so people who left don't stay in config.json and
13
+ its backups for ever. Never removed: local accounts, admins, and the last accounts
14
+ (an empty account list would open first-run setup again). An account without any
15
+ sign-in in the audit log is only removed when the log goes back that far for its kind
16
+ of sign-in — older versions did not record every LDAP sign-in.
17
+ """
18
+ from __future__ import annotations
19
+
20
+ import time
21
+ from typing import Optional
22
+
23
+ from .clock import parse_stamp
24
+ from .config import User, fold
25
+
26
+
27
+ def _when(text: str) -> Optional[float]:
28
+ """An audit record's time: local time with its UTC offset, or without one (older)."""
29
+ return parse_stamp(text)
30
+
31
+
32
+ def sign_in_of(record: dict) -> Optional[tuple[str, str]]:
33
+ """(account name, source) of an audit record that is a sign-in, else None:
34
+ login user: the name (before this version: as typed), detail "role, source";
35
+ oidc_login user and detail: the name (v2.7: four fields, the name in detail);
36
+ first_run_setup the setup page's admin, signed in at once (the command line's
37
+ first admin, by "(command line)", is not a sign-in)."""
38
+ action, user, detail = record.get("action"), record.get("user", ""), record.get("detail", "")
39
+ if action == "login" and user and "," in detail:
40
+ return user, detail.rsplit(",", 1)[1].strip()
41
+ if action == "oidc_login" and (user or detail):
42
+ return user or detail, "oidc"
43
+ if action == "first_run_setup" and user and user == detail:
44
+ return user, "local"
45
+ return None
46
+
47
+
48
+ class SignIns:
49
+ """The last sign-in of each account name (LDAP names also in any letter case, as
50
+ they sign in), and for each source the oldest sign-in on record: how far back the
51
+ audit log knows that kind of sign-in."""
52
+
53
+ def __init__(self) -> None:
54
+ self._last: dict[str, float] = {} # account name -> time
55
+ self._folded: dict[str, float] = {} # fold(name) -> time (LDAP accounts)
56
+ self._since: dict[str, float] = {} # source -> its oldest sign-in on record
57
+ self.loaded = False # the audit log has been read
58
+
59
+ def note(self, name: str, source: str, when: Optional[float] = None) -> None:
60
+ """A sign-in (a session issued) for this account, now or at `when`."""
61
+ when = time.time() if when is None else when
62
+ for table, key in ((self._last, name), (self._folded, fold(name))):
63
+ if when > table.get(key, 0.0):
64
+ table[key] = when
65
+ if when < self._since.get(source, float("inf")):
66
+ self._since[source] = when
67
+
68
+ def load(self, records) -> None:
69
+ """Add the sign-ins of these audit records (AppState.read_audit)."""
70
+ for record in records:
71
+ found = sign_in_of(record)
72
+ when = _when(record.get("time", "")) if found else None
73
+ if found and when is not None:
74
+ self.note(found[0], found[1], when)
75
+ self.loaded = True
76
+
77
+ def last(self, user: User) -> Optional[float]:
78
+ """When this account last signed in (an LDAP account: in any letter case), or
79
+ None when the audit log doesn't know."""
80
+ if user.source == "ldap":
81
+ return self._folded.get(fold(user.username))
82
+ return self._last.get(user.username)
83
+
84
+ def expired(self, users: list[User], days: int, now: Optional[float] = None) -> list[User]:
85
+ """The LDAP/OIDC accounts not signed in for `days` (see the module docstring)."""
86
+ if days <= 0 or not self.loaded:
87
+ return []
88
+ cutoff = (time.time() if now is None else now) - days * 86400
89
+ gone = []
90
+ for u in users:
91
+ if u.source not in ("ldap", "oidc") or u.role == "admin":
92
+ continue
93
+ last = self.last(u)
94
+ if last is not None and last < cutoff:
95
+ gone.append(u)
96
+ elif last is None and self._since.get(u.source, float("inf")) <= cutoff:
97
+ gone.append(u)
98
+ return [] if len(gone) == len(users) else gone
@@ -0,0 +1,154 @@
1
+ """Runtime adjustments for the standalone (PyInstaller) executables.
2
+
3
+ The Linux binary carries its own OpenSSL, which looks for CA certificates where
4
+ the distribution it was built on keeps them. Other distributions keep them
5
+ elsewhere (RHEL/Fedora under /etc/pki, openSUSE in /etc/ssl/ca-bundle.pem…), so
6
+ certificate checks — MQTT over TLS, OIDC discovery, LDAPS / StartTLS — would fail there
7
+ for want of trust anchors. prepare() points OpenSSL at the system bundle through the standard
8
+ SSL_CERT_FILE variable, unless the admin already set SSL_CERT_FILE / SSL_CERT_DIR.
9
+ Windows needs nothing: Python reads the Windows certificate store.
10
+
11
+ Both executables are single files that unpack themselves at every start into a new
12
+ _MEIxxxxxx directory under $TMPDIR (/tmp) or %TEMP% and run from it for as long as
13
+ they run: the page templates, static files, translations, ldap3 and native libraries
14
+ are read from there long after the start. Temp cleaners delete files nobody touched
15
+ for some days (systemd-tmpfiles: 10 days in /tmp; tmpreaper, Windows Disk Cleanup: a
16
+ week), which would break the web UI of an instance that runs longer. keep_unpacked()
17
+ holds a shared lock on the directory — systemd-tmpfiles (242+) skips a locked
18
+ directory and everything in it — and refreshes the access times inside it for the
19
+ cleaners that only compare timestamps (systemd 239 on RHEL 8, tmpreaper, Windows). A
20
+ directory left behind by a killed process is neither locked nor refreshed, so it
21
+ still ages out.
22
+ """
23
+ from __future__ import annotations
24
+
25
+ import os
26
+ import sys
27
+ import threading
28
+ import time
29
+
30
+ # Probe order as in Go's crypto/x509 (root_linux.go).
31
+ CA_BUNDLES = (
32
+ "/etc/ssl/certs/ca-certificates.crt", # Debian, Ubuntu, Arch, Gentoo
33
+ "/etc/pki/tls/certs/ca-bundle.crt", # Fedora, RHEL, CentOS, Alma, Rocky
34
+ "/etc/ssl/ca-bundle.pem", # openSUSE, SLES
35
+ "/etc/pki/tls/cacert.pem", # OpenELEC
36
+ "/etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem", # RHEL 7+ extracted bundle
37
+ "/etc/ssl/cert.pem", # Alpine
38
+ )
39
+
40
+ # How often a one-file build refreshes the access times of its unpacked files: far
41
+ # inside the cleaners' ages (10 days, a week), even where an admin cut them to a day.
42
+ REFRESH_EVERY_S = 6 * 3600
43
+
44
+ notes: list[str] = [] # startup log lines, written once logging is up (runtime._serve)
45
+ _kept: dict[str, str] = {} # unpacked directory -> its startup note
46
+ _locks: dict[str, int] = {} # unpacked directory -> the fd holding its lock until exit
47
+
48
+
49
+ def use_system_ca_bundle(environ=None, isfile=os.path.isfile) -> str | None:
50
+ """Set SSL_CERT_FILE to the first CA bundle that exists; returns its path, or
51
+ None if the admin configured SSL_CERT_FILE/SSL_CERT_DIR or none was found."""
52
+ environ = os.environ if environ is None else environ
53
+ if environ.get("SSL_CERT_FILE") or environ.get("SSL_CERT_DIR"):
54
+ return None
55
+ for path in CA_BUNDLES:
56
+ if isfile(path):
57
+ environ["SSL_CERT_FILE"] = path
58
+ return path
59
+ return None
60
+
61
+
62
+ def is_unpacked(path: str) -> bool:
63
+ """True for the temporary directory a one-file PyInstaller build unpacked itself
64
+ into (the bootloader names it _MEIxxxxxx; a one-folder build has none)."""
65
+ return os.path.basename(os.path.normpath(path)).startswith("_MEI") and os.path.isdir(path)
66
+
67
+
68
+ def touch_tree(root: str, now_ns: int | None = None) -> int:
69
+ """Set the access time of root and of everything below it to now. Each modification
70
+ time stays, so nothing looks changed (Jinja's template cache, the static files'
71
+ ETags and Python's bytecode cache stay valid); the inode change time moves along.
72
+ Symbolic links are refreshed themselves. Returns how many entries were refreshed."""
73
+ now = time.time_ns() if now_ns is None else now_ns
74
+ follow = os.utime not in os.supports_follow_symlinks # Windows: no links in the bundle
75
+
76
+ def touch(path: str) -> int:
77
+ try:
78
+ os.utime(path, ns=(now, os.lstat(path).st_mtime_ns), follow_symlinks=follow)
79
+ except OSError: # gone already, or not ours to change
80
+ return 0
81
+ return 1
82
+
83
+ count = touch(root)
84
+ for dirpath, dirnames, filenames in os.walk(root):
85
+ count += sum(touch(os.path.join(dirpath, name)) for name in dirnames + filenames)
86
+ return count
87
+
88
+
89
+ def _lock_shared(path: str) -> int:
90
+ """An fd on path that holds flock(LOCK_SH). Waits up to 5 s for an exclusive holder
91
+ (systemd-tmpfiles locks a directory only while it scans it) rather than hang."""
92
+ if sys.platform == "win32": # keep_unpacked locks on POSIX only
93
+ raise OSError("flock() is POSIX only")
94
+ import fcntl
95
+
96
+ fd = os.open(path, os.O_RDONLY)
97
+ for _ in range(50):
98
+ try:
99
+ fcntl.flock(fd, fcntl.LOCK_SH | fcntl.LOCK_NB)
100
+ return fd
101
+ except BlockingIOError:
102
+ time.sleep(0.1)
103
+ except OSError:
104
+ os.close(fd)
105
+ raise
106
+ os.close(fd)
107
+ raise BlockingIOError(f"{path} stays locked by another process")
108
+
109
+
110
+ def _refresh_forever(path: str, every: float) -> None:
111
+ while True:
112
+ time.sleep(every)
113
+ touch_tree(path)
114
+
115
+
116
+ def keep_unpacked(path: str, every: float = REFRESH_EVERY_S) -> str:
117
+ """Keep temp cleaners away from a one-file build's unpacked directory for as long
118
+ as this process runs: a shared lock on it (POSIX), and its access times refreshed
119
+ every `every` seconds. Returns the line for the startup log."""
120
+ if path in _kept:
121
+ return _kept[path]
122
+ how = f"access times refreshed every {every / 3600:g} h"
123
+ if os.name == "posix":
124
+ try:
125
+ _locks[path] = _lock_shared(path)
126
+ how = "locked, " + how
127
+ except OSError as e:
128
+ how += f" (could not lock it: {e})"
129
+ threading.Thread(target=_refresh_forever, args=(path, every), daemon=True,
130
+ name="ser2netweb-keep-unpacked").start()
131
+ _kept[path] = f"standalone: unpacked in {path} — {how}, so temp cleaners leave it alone"
132
+ return _kept[path]
133
+
134
+
135
+ def prepare(frozen: bool | None = None, platform: str = sys.platform, environ=None,
136
+ isfile=os.path.isfile, bundle: str | None = None) -> None:
137
+ """Call once at startup, before anything creates an SSL context."""
138
+ frozen = getattr(sys, "frozen", False) if frozen is None else frozen
139
+ environ = os.environ if environ is None else environ
140
+ if not frozen:
141
+ return
142
+ bundle = getattr(sys, "_MEIPASS", "") if bundle is None else bundle
143
+ if is_unpacked(bundle):
144
+ notes.append(keep_unpacked(bundle))
145
+ if not platform.startswith("linux"):
146
+ return
147
+ if environ.get("SSL_CERT_FILE") or environ.get("SSL_CERT_DIR"):
148
+ notes.append("TLS: CA certificates from SSL_CERT_FILE / SSL_CERT_DIR")
149
+ elif path := use_system_ca_bundle(environ, isfile):
150
+ notes.append(f"TLS: using the system CA bundle {path}")
151
+ else:
152
+ notes.append("WARNING: no system CA certificates found — install the ca-certificates "
153
+ "package or set SSL_CERT_FILE, otherwise certificate checks for MQTT over "
154
+ "TLS, OIDC sign-in and LDAPS / StartTLS (unless ldap.ca_file is set) fail")