Backport PR #5166: remove mktemp usage

mktemp is insecure, by the time the generated filename is used someone
else might have modified it.