Index: openacs-4/packages/acs-admin/tcl/test/acs-admin-procs.tcl
===================================================================
RCS file: /usr/local/cvsroot/openacs-4/packages/acs-admin/tcl/test/acs-admin-procs.tcl,v
diff -u -r1.6.2.9 -r1.6.2.10
--- openacs-4/packages/acs-admin/tcl/test/acs-admin-procs.tcl	16 Feb 2021 20:59:02 -0000	1.6.2.9
+++ openacs-4/packages/acs-admin/tcl/test/acs-admin-procs.tcl	2 Aug 2021 21:26:54 -0000	1.6.2.10
@@ -98,7 +98,6 @@
     acs::test::user::create
     acs_user::create_portrait
     acs_user::get_portrait_id
-    ad_tmpnam
     permission::grant
     permission::permission_p
 } acs_admin_merge_MergeUserInfo {
@@ -112,8 +111,7 @@
             set user_id_2 [dict get [acs::test::user::create] user_id]
 
             # Fake non-image just to have a file to save
-            set tmpnam [ad_tmpnam].png
-            set wfd [open $tmpnam w]
+            set wfd [file tempfile tmpnam [ad_tmpdir]/test-XXXXXX.png]
             puts $wfd [string repeat a 1000]
             close $wfd
             # Give a fake portrait to user_1
Index: openacs-4/packages/acs-subsite/www/shared/1pixel.tcl
===================================================================
RCS file: /usr/local/cvsroot/openacs-4/packages/acs-subsite/www/shared/1pixel.tcl,v
diff -u -r1.7.2.1 -r1.7.2.2
--- openacs-4/packages/acs-subsite/www/shared/1pixel.tcl	16 Dec 2019 17:14:38 -0000	1.7.2.1
+++ openacs-4/packages/acs-subsite/www/shared/1pixel.tcl	2 Aug 2021 21:26:54 -0000	1.7.2.2
@@ -23,9 +23,8 @@
     # insist on UTF8-encoding it). So we write to a file, then dump
     # the file's contents.
 
-    set file_name [ad_tmpnam]
+    set file [file tempfile file_name]
     ns_log "Notice" "logging to $file_name"
-    set file [open $file_name w+]
     fconfigure $file -encoding binary -translation binary
     puts -nonewline $file [format "%c%c%c" $r $g $b]
     seek $file 0
Index: openacs-4/packages/acs-tcl/tcl/01-database-procs.tcl
===================================================================
RCS file: /usr/local/cvsroot/openacs-4/packages/acs-tcl/tcl/01-database-procs.tcl,v
diff -u -r1.1.2.29 -r1.1.2.30
--- openacs-4/packages/acs-tcl/tcl/01-database-procs.tcl	3 Mar 2021 10:32:57 -0000	1.1.2.29
+++ openacs-4/packages/acs-tcl/tcl/01-database-procs.tcl	2 Aug 2021 21:26:54 -0000	1.1.2.30
@@ -2922,15 +2922,13 @@
 
         oracle {
             set user_pass [db_get_sql_user -dbn $dbn]
-            set tmpnam [ad_tmpnam]
 
             set fd [open $file r]
             set file_contents [read $fd]
             close $fd
-
             set file_contents [subst $file_contents]
 
-            set fd1 [open "${tmpnam}.ctl" w]
+            set fd1 [file tempfile tmpnam [ns_config ns/parameters tmpdir]/oacs-XXXXXX.ctl]
             puts $fd1 $file_contents
             close $fd1
 
@@ -2972,8 +2970,7 @@
             set fd [open $file r]
             set copy_command [subst -nobackslashes [read $fd]]
             close $fd
-            set copy_file [ns_mktemp [ad_tmpdir]/psql-copyfile-XXXXXX]
-            set fd [open $copy_file {CREAT EXCL WRONLY} 0600]
+            set fd [file tempfile copy_file [ad_tmpdir]/psql-copyfile-XXXXXX]
             puts $fd $copy_command
             close $fd
 
Index: openacs-4/packages/acs-tcl/tcl/http-client-procs.tcl
===================================================================
RCS file: /usr/local/cvsroot/openacs-4/packages/acs-tcl/tcl/http-client-procs.tcl,v
diff -u -r1.30.2.20 -r1.30.2.21
--- openacs-4/packages/acs-tcl/tcl/http-client-procs.tcl	12 Mar 2021 10:24:42 -0000	1.30.2.20
+++ openacs-4/packages/acs-tcl/tcl/http-client-procs.tcl	2 Aug 2021 21:26:54 -0000	1.30.2.21
@@ -789,9 +789,9 @@
 
     if {$spool_file eq ""} {
         # create the spool file
-        set spool_file [ad_tmpnam]
-        set wfd [open $spool_file w]
+        set wfd [file tempfile spool_file [ad_tmpdir]/http-spool-XXXXXX]
         fconfigure $wfd -translation binary
+        
         # flush currently collected payload
         puts -nonewline $wfd $payload
         # set required encoding for next content
@@ -1683,8 +1683,7 @@
     # and let it be read by curl.
     set create_body_file_p [expr {$body_file eq ""}]
     if {$create_body_file_p} {
-        set body_file [ad_tmpnam]
-        set wfd [open $body_file w]
+        set wfd [file tempfile body_file [ad_tmpdir]/http-spool-XXXXXX]
         fconfigure $wfd -translation binary
         puts -nonewline $wfd $body
         close $wfd
Index: openacs-4/packages/acs-tcl/tcl/pdf-procs.tcl
===================================================================
RCS file: /usr/local/cvsroot/openacs-4/packages/acs-tcl/tcl/pdf-procs.tcl,v
diff -u -r1.4.2.3 -r1.4.2.4
--- openacs-4/packages/acs-tcl/tcl/pdf-procs.tcl	13 Jul 2020 12:02:33 -0000	1.4.2.3
+++ openacs-4/packages/acs-tcl/tcl/pdf-procs.tcl	2 Aug 2021 21:26:55 -0000	1.4.2.4
@@ -28,17 +28,15 @@
 
     @return the pdf-file-name
 } {
-
-    set tmp_filename [ad_tmpnam]
+    
     # create html.file
     set html_content [create_html_content -template_id $template_id -set_var_call $set_var_call]
-    set tmp_html_filename "${tmp_filename}.html"
-    set fp [open $tmp_html_filename w]
+    set fp [file tempfile tmp_html_filename [ad_tmpdir]/pdf-XXXXXX.html]
     puts $fp $html_content
     close $fp
 
     # create pdf-file
-    set tmp_pdf_filename "${tmp_filename}.pdf"
+    set tmp_pdf_filename "${tmp_html_filename}.pdf"
     set htmldoc_bin [parameter::get -parameter "HtmlDocBin" -default "/usr/bin/htmldoc"]
     if {[catch {exec $htmldoc_bin --webpage --quiet -t pdf -f $tmp_pdf_filename $tmp_html_filename} err]} {
         ns_log Error "Error during conversion from html to pdf: $err"
@@ -61,14 +59,12 @@
     @param html_content HTML Content that is transformed into PDF
     @return filename of the pdf file
 } {
-    set tmp_filename [ad_tmpnam]
-    set tmp_html_filename "${tmp_filename}.html"
-    set fp [open $tmp_html_filename w]
+    set fp [file tempfile tmp_html_filename [ad_tmpdir]/pdf-XXXXXX.html]
     puts $fp $html_content
     close $fp
 
     # create pdf-file
-    set tmp_pdf_filename "${tmp_filename}.pdf"
+    set tmp_pdf_filename "${tmp_html_filename}.pdf"
     set htmldoc_bin [parameter::get -parameter "HtmlDocBin" -default "/usr/bin/htmldoc"]
     if {[catch {exec $htmldoc_bin --webpage --quiet -t pdf -f $tmp_pdf_filename $tmp_html_filename} err]} {
         ns_log Error "Error during conversion from html to pdf: $err"
Index: openacs-4/packages/acs-tcl/tcl/utilities-procs-aolserver.tcl
===================================================================
RCS file: /usr/local/cvsroot/openacs-4/packages/acs-tcl/tcl/utilities-procs-aolserver.tcl,v
diff -u -r1.6.2.3 -r1.6.2.4
--- openacs-4/packages/acs-tcl/tcl/utilities-procs-aolserver.tcl	10 Aug 2019 14:33:27 -0000	1.6.2.3
+++ openacs-4/packages/acs-tcl/tcl/utilities-procs-aolserver.tcl	2 Aug 2021 21:26:55 -0000	1.6.2.4
@@ -132,7 +132,7 @@
     {-path "/"}
     {-discard f}
     {-scriptable t}
-    {-samesite none}    
+    {-samesite none}
     name
     {value ""}
 } {
@@ -353,8 +353,7 @@
         # If the file was not spooled, obtainit via [ns_conn content]
         # as write it to a file.
         #
-        set result [ad_tmpnam]
-        set F [open $result w]
+        set F [file tempfile result [ad_tmpdir]/nsspool-XXXXXX]
         if {$binary} {
             fconfigure $F -translation binary -encoding binary
         }
Index: openacs-4/packages/acs-tcl/tcl/utilities-procs.tcl
===================================================================
RCS file: /usr/local/cvsroot/openacs-4/packages/acs-tcl/tcl/utilities-procs.tcl,v
diff -u -r1.189.2.96 -r1.189.2.97
--- openacs-4/packages/acs-tcl/tcl/utilities-procs.tcl	29 Jul 2021 14:38:17 -0000	1.189.2.96
+++ openacs-4/packages/acs-tcl/tcl/utilities-procs.tcl	2 Aug 2021 21:26:55 -0000	1.189.2.97
@@ -155,11 +155,6 @@
 
         @return boolean
     } {
-        # set listing_fn [ns_mktemp]
-        # exec /bin/bash -c "LANG=C unzip -l $zip_fn > $listing_fn"
-        # set F [open $listing_fn rb]; set c [read $F]; close $F
-        # file delete listing_fn
-        # return [ns_valid_utf8 $c]
         set F [open $zip_fn rb]; set C [read $F]; close $F
         set validUTF8 1
         while {$validUTF8 && [binary encode hex [string range $C 0 3]] eq "504b0304"} {
@@ -3357,10 +3352,8 @@
         set new [$filter_proc $new]
     }
 
-    set old_f [ad_tmpnam]
-    set new_f [ad_tmpnam]
-    set old_fd [open $old_f "w"]
-    set new_fd [open $new_f "w"]
+    set old_fd [file tempfile old_f [ad_tmpdir]/nsdiff-XXXXXX]
+    set new_fd [file tempfile new_f [ad_tmpdir]/nsdiff-XXXXXX]
     puts $old_fd [join [split $old $split_by] "\n"]
     puts $new_fd [join [split $new $split_by] "\n"]
     close $old_fd
@@ -3755,8 +3748,10 @@
 }
 
 ad_proc ad_tmpnam {{template ""}} {
-    A stub function to replace the deprecated "ns_tmpnam",
-    which uses the deprecated C-library function "tmpnam()"
+    A stub function to replace the deprecated "ns_tmpnam", which uses
+    the deprecated C-library function "tmpnam()".  However, also
+    ns_mktemp is not recommended any more due to a potential race
+    between the name creation and the file open command.
 } {
     if {$template eq ""} {
         set template [ns_config ns/parameters tmpdir]/oacs-XXXXXX
Index: openacs-4/packages/acs-templating/tcl/spellcheck-procs.tcl
===================================================================
RCS file: /usr/local/cvsroot/openacs-4/packages/acs-templating/tcl/spellcheck-procs.tcl,v
diff -u -r1.28.2.5 -r1.28.2.6
--- openacs-4/packages/acs-templating/tcl/spellcheck-procs.tcl	1 Feb 2021 10:54:23 -0000	1.28.2.5
+++ openacs-4/packages/acs-templating/tcl/spellcheck-procs.tcl	2 Aug 2021 21:26:55 -0000	1.28.2.6
@@ -187,8 +187,7 @@
         regsub -all -- {<[^<]*>} $text_to_spell_check "" text_to_spell_check
     }
 
-    set tmpfile [ns_mktemp "[ad_tmpdir]/webspellXXXXXX"]
-    set f [open $tmpfile w]
+    set f [file tempfile tmpfile [ad_tmpdir]/webspell-XXXXXX]
     puts $f $text_to_spell_check
     close $f
 
@@ -198,12 +197,10 @@
     set suffix [expr {$language ne "" ? "-$language" : ""}]
     set dictionaryfile [file join [acs_package_root_dir acs-templating] resources forms webspell-local-dict$suffix]
 
-    # The webspell wrapper is necessary because ispell requires
-    # the HOME environment set, and setting env(HOME) doesn't appear
-    # to work from AOLserver.
+    # The webspell wrapper is used to achieve independence from 
+    # nsd's env(HOME).
 
     set spelling_wrapper [file join $::acs::rootdir bin webspell]
-
     if {![file executable $spelling_wrapper]} {
         #
         # In case no_abort is given we just return the error