cleared redundant code revamp v003
[feisty_meow.git] / scripts / security / cool_permissionator.sh
1 #!/bin/bash
2
3 # a library file for redoing file ownership and permissions as we feel is
4 # appropriate.  this approach is a little bit specific to our way of doing
5 # things, but it does handle a lot of important fixes everyone would want,
6 # like making ~/.ssh really secure.
7
8 # cleans up the ownership and permissions for all of the important files and dirs.
9 function reapply_cool_permissions()
10 {
11   local cooluser="$1"; shift
12
13   # first build a list of dirs based on their location in /home/archives.
14   local arch_builder="archons basement codebarn games imaginations musix pooling prewar_toaster stuffing toaster walrus"
15   local ARCHIVE_TOP=/home/archives
16   local dirname
17   local arch_addin
18   for dirname in $arch_builder; do
19     arch_addin+="$ARCHIVE_TOP/$dirname "
20   done
21 #echo arch addin now is: $arch_addin
22
23   # special case that makes our software hierarchy folder, if it doesn't exist.
24   # everything else is only re-permed if it exists.
25   if [ ! -d "$DEFAULT_FEISTYMEOW_ORG_DIR" ]; then
26     sudo mkdir "$DEFAULT_FEISTYMEOW_ORG_DIR"
27     test_or_die "making directory: $DEFAULT_FEISTYMEOW_ORG_DIR"
28   fi
29
30   # fix some permissions for important security considerations.
31   if [ -d $HOME/.ssh ]; then
32     harsh_perm $HOME/.ssh
33   fi
34
35 #hmmm: consider adding feisty meow apex to the dirname list below.
36
37   # iterate across the list of dirs we want cooluser to own and change their ownership.
38   for dirname in $HOME \
39         $DEFAULT_FEISTYMEOW_ORG_DIR \
40         /usr/local/${cooluser} \
41         /home/games \
42         $arch_addin; do
43     if [ -d "$dirname" ]; then
44       echo "revising ownership on '$dirname'"
45       sudo chown -R ${cooluser}:${cooluser} "$dirname"
46       test_or_die "chowning for ${cooluser}: $dirname"
47     fi
48   done
49
50   # special case for archives directory in stuffing.
51   if [ -d /z/stuffing -o -L /z/stuffing ]; then
52     sudo chown ${cooluser}:${cooluser} /z/
53     test_or_die "chowning /z for ${cooluser}"
54     sudo chmod g+rx,o+rx /z
55     test_or_die "chmodding /z/ for ${cooluser}"
56     sudo chown ${cooluser}:${cooluser} /z/stuffing/
57     test_or_die "chowning /z/stuffing for ${cooluser}"
58     sudo chmod g+rx,o-rwx /z/stuffing
59     test_or_die "chmodding /z/stuffing for ${cooluser}"
60     pushd /z/stuffing &>/dev/null
61     if [ -d archives -o -L archives ]; then
62       sudo chown ${cooluser}:${cooluser} archives/
63       test_or_die "chowning /z/stuffing/archives for ${cooluser}"
64       sudo chmod -R g+rwx archives
65       test_or_die "chmodding /z/stuffing/archives for ${cooluser}"
66     fi
67     popd &>/dev/null
68   fi
69
70   # make the log files readable by normal humans.
71   sudo bash $FEISTY_MEOW_SCRIPTS/security/normal_perm.sh /var/log
72   test_or_die "setting normal perms on /var/log"
73 }
74
75 # this block should execute when the script is actually run, rather
76 # than when it's just being sourced.
77
78 # this runs the cool permission applier on the current user.
79 if [[ $0 =~ .*cool_permissionator\.sh.* ]]; then
80 echo A
81   THISDIR="$( \cd "$(\dirname "$0")" && /bin/pwd )"
82 echo B
83   export FEISTY_MEOW_APEX="$( \cd "$THISDIR/../.." && \pwd )"
84 echo B.2
85   source "$THISDIR/../core/launch_feisty_meow.sh"
86   test_or_die "sourcing the feisty meow launcher"
87 echo C
88   reapply_cool_permissions $(logname)
89   test_or_die "reapplying cool permissions on $(logname)"
90 echo D
91 fi
92