Skip to content

Instantly share code, notes, and snippets.

@nonakap
Created September 29, 2012 02:44
Show Gist options
  • Save nonakap/3802982 to your computer and use it in GitHub Desktop.
Save nonakap/3802982 to your computer and use it in GitHub Desktop.
[NetBSD] buf-fix and improvement per-user tmp.
diff --git a/etc/rc.d/cleartmp b/etc/rc.d/cleartmp
index 384bfa97..4fbc0c0 100755
--- a/etc/rc.d/cleartmp
+++ b/etc/rc.d/cleartmp
@@ -39,6 +39,25 @@ cleartmp_start()
(cd ${tmp_dir} && rm -rf [a-km-pr-zA-Z]* &&
find -x . ! -name . ! -name lost+found ! -name quota.user \
! -name quota.group -exec rm -rf -- {} \; -type d -prune)
+
+ if checkyesno per_user_tmp && [ -d ${per_user_tmp_dir} ]; then
+ # Create user tmp directory.
+ if [ ! -d ${per_user_tmp_dir}/0 ]; then
+ /bin/mkdir ${per_user_tmp_dir}/0
+ /usr/sbin/chown root:wheel ${per_user_tmp_dir}/0
+ /bin/chmod 1700 ${per_user_tmp_dir}/0
+ fi
+ if [ -n "${per_user_tmp_prepare_users}" ]; then
+ for user in ${per_user_tmp_prepare_users}; do
+ uid=$(/usr/bin/id -u ${user})
+ if [ $? -eq 0 ] && [ ! -d ${per_user_tmp_dir}/${uid} ]; then
+ /bin/mkdir ${per_user_tmp_dir}/${uid}
+ /usr/sbin/chown ${user}:$(/usr/bin/id -gn ${user}) ${per_user_tmp_dir}/${uid}
+ /bin/chmod 1700 ${per_user_tmp_dir}/${uid}
+ fi
+ done
+ fi
+ fi
}
load_rc_config $name
diff --git a/etc/rc.d/perusertmp b/etc/rc.d/perusertmp
index 2d757e7..7f16ce0 100755
--- a/etc/rc.d/perusertmp
+++ b/etc/rc.d/perusertmp
@@ -6,13 +6,14 @@
# PROVIDE: perusertmp
# REQUIRE: mountall
# BEFORE: cleartmp
+# KEYWORD: shutdown
$_rc_subr_loaded . /etc/rc.subr
name="perusertmp"
rcvar="per_user_tmp"
start_cmd="perusertmp_start"
-stop_cmd=":"
+stop_cmd="perusertmp_stop"
perusertmp_start()
{
@@ -44,6 +45,41 @@ perusertmp_start()
/bin/rm -rf /tmp
/bin/ln -s ${per_user_tmp_dir}/@ruid /tmp
fi
+
+ if ! checkyesno clear_tmp; then
+ # Create user tmp directory.
+ if [ ! -d ${per_user_tmp_dir}/0 ]; then
+ /bin/mkdir ${per_user_tmp_dir}/0
+ /usr/sbin/chown root:wheel ${per_user_tmp_dir}/0
+ /bin/chmod 1700 ${per_user_tmp_dir}/0
+ fi
+ if [ -n "${per_user_tmp_prepare_users}" ]; then
+ for user in ${per_user_tmp_prepare_users}; do
+ uid=$(/usr/bin/id -u ${user})
+ if [ $? -eq 0 ] && [ ! -d ${per_user_tmp_dir}/${uid} ]; then
+ /bin/mkdir ${per_user_tmp_dir}/${uid}
+ /usr/sbin/chown ${user}:$(/usr/bin/id -gn ${user}) ${per_user_tmp_dir}/${uid}
+ /bin/chmod 1700 ${per_user_tmp_dir}/${uid}
+ fi
+ done
+ fi
+ fi
+}
+
+perusertmp_stop()
+{
+ if [ -d "${per_user_tmp_dir}" ]; then
+ mount_point=$(cd ${per_user_tmp_dir} && /bin/df . | /usr/bin/tail -1 | /usr/bin/awk '{print $6}')
+ mount_fs=$(cd ${per_user_tmp_dir} && /bin/df . | /usr/bin/tail -1 | /usr/bin/awk '{print $1}')
+ if [ "${mount_point}" = "${per_user_tmp_dir}" ] && ( [ "${mount_fs}" = "tmpfs" ] || [ ${mount_fs} = "mfs" ] ); then
+ if [ "$(/usr/bin/readlink /tmp)" = ${per_user_tmp_dir}/@ruid ]; then
+ /bin/rm -f /tmp
+ /bin/mkdir /tmp
+ /usr/sbin/chown root:wheel /tmp
+ /bin/chmod 1777 /tmp
+ fi
+ fi
+ fi
}
load_rc_config $name
diff --git a/share/man/man5/rc.conf.5 b/share/man/man5/rc.conf.5
index af4bd45..e870bc2 100644
--- a/share/man/man5/rc.conf.5
+++ b/share/man/man5/rc.conf.5
@@ -423,6 +423,9 @@ can be used to override the default location of the
.Dq real
temporary directories,
.Dq Pa /private/tmp .
+List in
+.Sy per_user_tmp_prepare_users
+the users whose creating the temporary directory at start-up.
See
.Xr security 7
for additional details.
diff --git a/share/man/man7/security.7 b/share/man/man7/security.7
index 4d1f9e8..d2d9c87 100644
--- a/share/man/man7/security.7
+++ b/share/man/man7/security.7
@@ -393,6 +393,12 @@ entry to use
keyword) instead of
.Dq /tmp .
.Pp
+If you want to create a temporary directory of the user on startup,
+described a list of users to
+.Dq per_user_tmp_prepare_users
+.Xr rc.conf 5
+keyword .
+.Pp
Following that, run:
.Bd -literal -offset indent
# /etc/rc.d/perusertmp start
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment