From: Joseph Sutton Date: Mon, 20 Feb 2023 22:45:11 +0000 (+1300) Subject: s4:samba_dnsupdate: Avoid resource leaks X-Git-Tag: talloc-2.4.1~1529 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=fdc5f6ee9950add6c0e357c636ab571663feee9b;p=thirdparty%2Fsamba.git s4:samba_dnsupdate: Avoid resource leaks View with 'git show -b'. The seek(0) call is unnecessary. Closing a file removes the lock held on it. Signed-off-by: Joseph Sutton Reviewed-by: Andrew Bartlett --- diff --git a/source4/scripting/bin/samba_dnsupdate b/source4/scripting/bin/samba_dnsupdate index 0db490e21e9..82b8ec5279c 100755 --- a/source4/scripting/bin/samba_dnsupdate +++ b/source4/scripting/bin/samba_dnsupdate @@ -285,12 +285,13 @@ def check_dns_name(d): except IOError: return False - for line in dns_file: - line = line.strip() - if line == '' or line[0] == "#": - continue - if line.lower() == str(d).lower(): - return True + with dns_file: + for line in dns_file: + line = line.strip() + if line == '' or line[0] == "#": + continue + if line.lower() == str(d).lower(): + return True return False try: @@ -428,14 +429,13 @@ def call_nsupdate(d, op="add"): rfile = open(opts.use_file, 'r+') except IOError: # Perhaps create it - open(opts.use_file, 'w+') + open(opts.use_file, 'w+').close() # Open it for reading again, in case someone else got to it first rfile = open(opts.use_file, 'r+') fcntl.lockf(rfile, fcntl.LOCK_EX) (file_dir, file_name) = os.path.split(opts.use_file) (tmp_fd, tmpfile) = tempfile.mkstemp(dir=file_dir, prefix=file_name, suffix="XXXXXX") wfile = os.fdopen(tmp_fd, 'a') - rfile.seek(0) for line in rfile: if op == "delete": l = parse_dns_line(line, {}) @@ -444,8 +444,9 @@ def call_nsupdate(d, op="add"): wfile.write(line) if op == "add": wfile.write(str(d)+"\n") + rfile.close() + wfile.close() os.rename(tmpfile, opts.use_file) - fcntl.lockf(rfile, fcntl.LOCK_UN) return if opts.verbose: @@ -747,7 +748,7 @@ try: cfile = open(dns_update_cache, 'r+') except IOError: # Perhaps create it - open(dns_update_cache, 'w+') + open(dns_update_cache, 'w+').close() # Open it for reading again, in case someone else got to it first cfile = open(dns_update_cache, 'r+') fcntl.lockf(cfile, fcntl.LOCK_EX) @@ -762,6 +763,8 @@ for line in cfile: cache_list.append(c) cache_set.add(str(c)) +cfile.close() + site_specific_rec = [] # read each line, and check that the DNS name exists @@ -784,6 +787,8 @@ for line in file: dns_list.append(d) dup_set.add(str(d)) +file.close() + # Perform automatic site coverage by default auto_coverage = True @@ -948,9 +953,8 @@ if rebuild_cache: if opts.verbose: print("Adding %s to %s" % (str(d), file_name)) wfile.write(str(d)+"\n") - wfile.flush() + wfile.close() os.rename(tmpfile, dns_update_cache) -fcntl.lockf(cfile, fcntl.LOCK_UN) # delete the ccache if we created it if ccachename is not None: