diff --git a/app.py b/app.py index afa2e57..c87fc6c 100644 --- a/app.py +++ b/app.py @@ -471,8 +471,8 @@ def append_record_to_file(filepath, name, ttl, rtype, data): with open(filepath, 'w') as f: f.write(content) - run_cmd(f"chown bind:bind {filepath}") - + run_cmd(f"chown bind:bind {filepath} 2>/dev/null || chown named:named {filepath} 2>/dev/null") + run_cmd(f"chmod 644 {filepath}") return True, "OK" @@ -541,8 +541,8 @@ def delete_record_from_file(filepath, zone_name, idx): with open(filepath, 'w') as f: f.write(content) - run_cmd(f"chown bind:bind {filepath}") - + run_cmd(f"chown bind:bind {filepath} 2>/dev/null || chown named:named {filepath} 2>/dev/null") + run_cmd(f"chmod 644 {filepath}") return True, f'{deleted["name"]} {deleted["type"]} {deleted["data"]}', "OK" @@ -774,9 +774,16 @@ def zone_create(): """ # Write zone file + try: + os.makedirs(BIND_ZONES_DIR, exist_ok=True) + except PermissionError as e: + flash(f"无法创建 zone 目录 {BIND_ZONES_DIR}(权限不足):{e}", "error") + return render_template("zone_form.html") with open(zone_file, 'w') as f: f.write(zone_content) - run_cmd(f"chown bind:bind {zone_file}") + # BIND runs as user 'bind' (Debian/Ubuntu) or 'named' (RHEL/CentOS). + # Try Debian first; fall back to RHEL. + run_cmd(f"chown bind:bind {zone_file} 2>/dev/null || chown named:named {zone_file} 2>/dev/null") run_cmd(f"chmod 644 {zone_file}") # Validate zone @@ -877,8 +884,8 @@ def zone_raw(zone_name): # Write with open(zone["file"], 'w') as f: f.write(content) - run_cmd(f"chown bind:bind {zone['file']}") - run_cmd(f"rm -f /tmp/zone_check.tmp") + run_cmd(f"chown bind:bind {zone['file']} 2>/dev/null || chown named:named {zone['file']} 2>/dev/null") + run_cmd(f"chmod 644 {zone['file']}") bind_reload()