\begin{exe} {Working with File Attributes}

   \begin{enumerate}
      \item
      With your normal user account use \textbf{touch} to
      create an empty file named \filelink{/tmp/appendit}.
      \item
      Use \textbf{cat} to append the contents of
      \filelink{/etc/hosts} to \filelink{/tmp/appendit}.
      \item
      Compare the contents of \filelink{/tmp/appendit} with
      \filelink{/etc/hosts}; there should not be any
      differences.
      \item
      Try to add the append-only attribute to
      \filelink{/tmp/appendit} by using \textbf{chattr}.
      You should see an error here.  Why?
      \item
      As root, retry adding the append-only attribute; this
      time it should work.  Look at the file's extended
      attributes by using \textbf{lsattr}.
      \item
      As a normal user, try and use \textbf{cat} to copy
      over the contents of \filelink{/etc/passwd} to
      \filelink{/tmp/appendit}.  You should get an
      error. Why?
      \item
      Try the same thing again as root.  You should also get
      an error.  Why?

      \item

      As the normal user, again use the append redirection
      operator (\verb?>>?) and try appending the
      \filelink{/etc/passwd} file to
      \filelink{/tmp/appendit}.  This should work.  Examine
      the resulting file to confirm.

      \item
      As root, set the immutable attribute on
      \filelink{/tmp/appendit}, and look at the extended
      attributes again.

      \item
      Try appending output to \filelink{/tmp/appendit}, try
      renaming the file, creating a hard link to the file,
      and deleting the file as both the normal user and as
      root.

      \item
      We can remove this file by removing the extended
      attributes. Do so.
   \end{enumerate}

   \begin{sol}

      \begin{enumerate}
         \item
         \begin{cmd}
$ cd /tmp
$ touch appendit
$ ls -l appendit
         \end{cmd}
         \begin{response}
-rw-rw-r-- 1 coop coop 0 Oct 23 19:04 appendit
         \end{response}
         \item
         \begin{cmd}
$ cat /etc/hosts > appendit
         \end{cmd}
         \item
         \begin{cmd}
$ diff /etc/hosts appendit
         \end{cmd}
         \item
         \begin{cmd}
$ chattr +a appendit
         \end{cmd}
         \begin{response}
chattr: Operation not permitted while setting flags on appendit
         \end{response}
         \item
         \begin{cmd}
$ sudo chattr +a appendit
$ lsattr appendit
         \end{cmd}
         \begin{response}
-----a-------e-- appendit
         \end{response}
         \item
         \begin{cmd}
$ cat /etc/passwd > appendit
         \end{cmd}
         \begin{response}
bash: appendit: Operation not permitted
         \end{response}
         \item
         \begin{cmd}
$ sudo su
$ cat /etc/passwd > appendit
         \end{cmd}
         \begin{response}
bash: appendit: Operation not permitted
         \end{response}
         \begin{cmd}
$ exit
         \end{cmd}
         \item
         \begin{cmd}
$ cat /etc/passwd >> /tmp/appendit
$ cat appendit
         \end{cmd}
         \item
         \begin{cmd}
$ sudo chattr +i appendit
$ lsattr appendit
         \end{cmd}
         \begin{response}
----ia-------e- appendit
         \end{response}
         \item
         \begin{cmd}
$ echo hello >> appendit
         \end{cmd}
         \begin{response}
-bash: appendit: Permission denied
         \end{response}
         \begin{cmd}
$ mv appendit appendit.rename
         \end{cmd}
         \begin{response}
mv: cannot move `appendit' to `appendit.rename': Operation not permitted
         \end{response}
         \begin{cmd}
$ ln appendit appendit.hardlink
         \end{cmd}
         \begin{response}
ln: creating hard link `appendit.hardlink' => `appendit': Operation not permitted
         \end{response}
         \begin{cmd}
$ rm -f appendit
         \end{cmd}
         \begin{response}
rm: cannot remove `appendit': Operation not permitted
         \end{response}

         \begin{cmd}
$ sudo su
$ echo hello >> appendit
         \end{cmd}
         \begin{response}
-bash: appendit: Permission denied
         \end{response}
         \begin{cmd}
$ mv appendit appendit.rename
         \end{cmd}
         \begin{response}
mv: cannot move `appendit' to `appendit.rename': Operation not permitted
         \end{response}
         \begin{cmd}
$ ln appendit appendit.hardlink
         \end{cmd}
         \begin{response}
ln: creating hard link `appendit.hardlink' => `appendit': Operation not permitted
         \end{response}
         \begin{cmd}
$ rm -f appendit
         \end{cmd}
         \begin{response}
rm: cannot remove `appendit': Operation not permitted
         \end{response}
         \begin{cmd}
$ exit
         \end{cmd}
         \item
         \begin{cmd}
$ sudo su
$ lsattr appendit
         \end{cmd}
         \begin{response}
----ia-------e- appendit
         \end{response}
         \begin{cmd}
$ chattr -ia appendit
$ rm appendit
         \end{cmd}
         \begin{response}
rm: remove regular file `appendit'? y
         \end{response}
         \begin{cmd}
$ ls appendit
         \end{cmd}
         \begin{response}
ls: cannot access appendit: No such file or directory
         \end{response}
      \end{enumerate}
   \end{sol}
\end{exe}

