1# Copyright (C) Internet Systems Consortium, Inc. ("ISC")
2#
3# SPDX-License-Identifier: MPL-2.0
4#
5# This Source Code Form is subject to the terms of the Mozilla Public
6# License, v. 2.0.  If a copy of the MPL was not distributed with this
7# file, you can obtain one at https://mozilla.org/MPL/2.0/.
8#
9# See the COPYRIGHT file distributed with this work for additional
10# information regarding copyright ownership.
11
12import os
13
14# These routines permit platform-independent location of BIND 9 tools
15if os.name == 'nt':
16    import win32con
17    import win32api
18
19
20def prefix(bindir=''):
21    if os.name != 'nt':
22        return os.path.join('@prefix@', bindir)
23
24    hklm = win32con.HKEY_LOCAL_MACHINE
25    bind_subkey = "Software\\ISC\\BIND"
26    sam = win32con.KEY_READ
27    h_key = None
28    key_found = True
29    # can fail if the registry redirected for 32/64 bits
30    try:
31        h_key = win32api.RegOpenKeyEx(hklm, bind_subkey, 0, sam)
32    except:
33        key_found = False
34    # retry for 32 bit python with 64 bit bind9
35    if not key_found:
36        key_found = True
37        sam64 = sam | win32con.KEY_WOW64_64KEY
38        try:
39            h_key = win32api.RegOpenKeyEx(hklm, bind_subkey, 0, sam64)
40        except:
41            key_found = False
42    # retry 64 bit python with 32 bit bind9
43    if not key_found:
44        key_found = True
45        sam32 = sam | win32con.KEY_WOW64_32KEY
46        try:
47            h_key = win32api.RegOpenKeyEx(hklm, bind_subkey, 0, sam32)
48        except:
49            key_found = False
50    if key_found:
51        try:
52            (named_base, _) = win32api.RegQueryValueEx(h_key, "InstallDir")
53        except:
54            key_found = False
55        win32api.RegCloseKey(h_key)
56    if key_found:
57        return os.path.join(named_base, bindir)
58    return os.path.join(win32api.GetSystemDirectory(), bindir)
59
60
61def shellquote(s):
62    if os.name == 'nt':
63        return '"' + s.replace('"', '"\\"') + '"'
64    return "'" + s.replace("'", "'\\''") + "'"
65
66
67version = '@BIND9_VERSION@'
68if os.name != 'nt':
69    sysconfdir = '@expanded_sysconfdir@'
70else:
71    sysconfdir = prefix('etc')
72