\documentclass{ictlab} \RCS $Revision: 1.3 $ \usepackage{alltt,key} \usepackage[pdfpagemode=None,pdfauthor={Nick Urbanik}]{hyperref} \usepackage[nolineno,noindent]{lgrind} \newcommand*{\labTitle}{Assignment 1: Shell Programming --- {\Large Solutions}} \renewcommand*{\subject}{Operating Systems and Systems Integration} \providecommand{\SUID}{\acro{SUID}\xspace}{ \providecommand{\SGID}{\acro{SGID}\xspace}{ \begin{document} \paragraph{Submission:} by 5pm, Monday, 10 February 2003 \paragraph{Where:} Electronically via this \URL: \url{http://nicku.org/perl2/submit.cgi} \ You can submit as many times as you like; a later submission will never overwrite an earlier submission. \paragraph{Format:} Must be \emph{plain text}. Submissions in proprietary formats (i.e., Word documents) will be copied straight to \texttt{/dev/null} and will receive \emph{ZERO MARKS}. You can put the shell scripts together as a \texttt{.zip} file or as a tarball. Do not submit \texttt{.rar} archives. \paragraph{Add your email address} Put your email address in a comment on the second line of your submissions, so that I can return them marked to you. I will not return assignments that do not have an email address on the second line of each file. \paragraph{Weighting:} This assignment shall provide 20\% of your Continuous Assessment mark, i.e., 10\% of the total marks for this subject. \paragraph{Marking Criteria:} Originality is essential. Efficient implementations get more marks. Elegant and well-structured solutions get more marks. Flexible solutions get more marks (i.e., offer more useful options to the user). Original submissions that answer the first four of these questions will receive a bonus of 5\% if they are submitted one week before the deadline. \paragraph{Cheating:} Your work \emph{must} be original. Copying will be \emph{severely} dealt with. \section{Background} \label{sec:background} \subsection{Example Programs and Online Resources} \label{sec:examples} I have written a number of example shell scripts---please download them, run them, examine them, understand them. Get them from \url{http://nicku.org/ossi/lectures/shell/bin/}. My colleague Joe Lee, the lecturer for the part time version of this subject, wrote some of these examples too. Thanks, Joe\e. Please also remember that there are hundreds of excellent examples in the freely downloadable book \emph{Advanced Bash Scripting Guide} at \url{http://tldp.org/LDP/abs/html/index.html}. There is also an excellent book about the \texttt{awk} programming language, available from \url{http://www.ssc.com/ssc/eap/}. Note: I have put one copy of the book, \emph{Learning the Bash Shell}, 2nd edition, by Cameron Newham and Bill Rosenblat, in the library open reserve. Another copy is on the shelves for loan. I have also purchased some other books on shell programming for the library, and expect them to be available after Christmas\e. One book you may find helpful is \emph{UNIX Power Tools}, 3rd edition. \subsection{Set User ID and Set Group ID Permissions} \label{sec:background-on-suid-sgid} Files that have the ``set user \ID bit'' (\SUID) set, and those which have the ``set group \ID bit'' (\SGID) set may be security risks, since a program file with the permission \SUID will execute as the user that owns the file, while a program with the \SGID permission will execute as the group owner of the file. You can set the \SUID permission on a file like this: \begin{alltt} $ \textbf{sudo chmod u+s \meta{file}} \end{alltt}%$ You can remove the \SUID permission from the file called \meta{file} with this command: \begin{alltt} $ \textbf{sudo chmod u-s \meta{file}} \end{alltt}%$ Similarly, you can add the \SGID permission with \begin{alltt} $ \textbf{sudo chmod g+s \meta{file}} \end{alltt}%$ Read the documentation for \texttt{chmod} with \begin{alltt} $ \textbf{info chmod} \end{alltt}%$ or \begin{alltt} $ \textbf{pinfo chmod} \end{alltt}%$ My preferred way to read \texttt{info} documentation is in \texttt{emacs} by: \begin{itemize*} \item start emacs \item type \key{Control-h}\,\key{i} to go to the top \texttt{info} menu \begin{explanation} I.e., hold down the \key{Control} key, and press the \key{h} key, then release them both, then press the \key{i} key. \end{explanation} \item type \begin{alltt} \textbf{\key{m}chmod} \end{alltt} \item Click on hyperlinks with the middle mouse button, and also click on the arrows at the top of emacs. \end{itemize*} You can use the \texttt{-perms} option to \texttt{find} to identify files that have any particular permissions. \subsection{Some uses of the RPM Package Manager, \texttt{rpm}} \label{sec:rpm} Using the \RPM Package Manager, \texttt{rpm}, you can: \begin{itemize} \item determine which software package a file belongs to: \begin{alltt} $ \textbf{rpm -qf \meta{file}} \end{alltt}%$ and determine what the installed permissions were for the files that belong to a software package called \meta{package}: \begin{alltt} \textbf{rpm -qlv \meta{package}} \end{alltt} e.g., \begin{alltt} \scriptsize$ \textbf{rpm -qlv unix2dos} -rwxr-xr-x 1 root root 15589 Jun 24 08:57 /usr/bin/unix2dos drwxr-xr-x 2 root root 0 Jun 24 08:57 /usr/share/doc/unix2dos-2.2 -rw-r--r-- 1 root root 1199 Oct 9 1996 /usr/share/doc/unix2dos-2.2/COPYRIGHT -r--r--r-- 1 root root 1105 Jun 24 08:57 /usr/share/man/man1/unix2dos.1.gz \end{alltt}%$ \item You can also verify that a package is correctly installed: \begin{alltt} $ \textbf{rpm -V \meta{package}} \end{alltt}%$ Please see my chapter on documentation in the workshop notes. Also, see the chapters on \RPM in the Red Hat Reference Guide\e. Perhaps most useful is chapter 5 of the book \emph{Maximum RPM}, particularly the subsection called \emph{--queryformat --- Construct a Custom Query Response} on the web page \url{http://nicku.org/doc/maximum-rpm-1.0/html/s1-rpm-query-parts.html}, also available from \url{http://www.rpm.org/max-rpm/s1-rpm-query-parts.html}. Note that this is part of the \RPM package \texttt{maximum-rpm-1.0-0.20020905.noarch.rpm} on the documentation \CDROM for Red hat 8.0, also downloadable from \url{http://nicku.org/ftp/redhat-8.0/doc/RedHat/RPMS/index-en.html}. \end{itemize} %\clearpage \section*{Assignment Requirements} \label{sec:requirements} If these requirements are not clear, please come and ask for clarification. I will then update this document with the clarification. Note the version number at the bottom right of each page. \begin{enumerate} \item Write a shell script that will display all the times that the computer was booted, as recorded in all the files \texttt{/var/log/messages*}. \item[\textbf{A Poss. Soln:}]\mbox{} \lgrindfile{booted} \item Write a shell script that will show the number of times that the user \texttt{root} has logged in each day of at least the last four weeks. Do not include days that the \texttt{root} user did not log in at all. \begin{explanation} Note that the \texttt{last} command is useful here. Do \texttt{man last} for more information. Note that the \texttt{/var/log/wtmp} log is \emph{rotated} every month so that there is at least four weeks of data available before the old log is deleted. At the start of every month, the following happens automatically: \begin{itemize} \item \texttt{/var/log/wtmp.1} is deleted \item \texttt{/var/log/wtmp} is renamed to \texttt{/var/log/wtmp.1} \item A new, empty \texttt{/var/log/wtmp} log file is created. \end{itemize} The behaviour is controlled by the \texttt{logrotate} software package, and in particular, by the following extract from \texttt{/etc/logrotate.conf}: \begin{verbatim} # no packages own wtmp -- we'll rotate them here /var/log/wtmp { monthly create 0664 root utmp rotate 1 } \end{verbatim} Read the manual page: \texttt{man logrotate} \end{explanation} \item[\textbf{A Poss. Soln:}]\mbox{} \lgrindfile{root-logged-in} \item Write a shell script that will show all failed attempts to log in as root for each day of at least the last four weeks. Provide options to sort by date, or by the number of failed attempts. \begin{explanation} I suggest that you investigate the man page for the \texttt{lastb} command. You will need to create a file \texttt{/var/log/btmp} and process output from the command \texttt{lastb}. \end{explanation} \item[\textbf{A Poss. Soln:}]\mbox{} \lgrindfile{root-bad-logins} \item Write a shell script that will list the size of each directory given on the command line, sorted by size. The size includes disk space used by the directory and all the files and subdirectories inside it. The script should take options to sort with smallest first, and with largest first. \item[\textbf{A Poss. Soln:}]\mbox{} \lgrindfile{dirsize} \item \label{que:find}Write a shell script that can search for all files below any given directory, and provide a nice report on all ordinary files that have the \SUID bit set, and also on files that have the \SGID bit set. Refer to section~\ref{sec:background-on-suid-sgid}. \item[\textbf{A Poss. Soln:}]\mbox{} \lgrindfile{finduidgid} \item Extend the program from question~\ref{que:find} so that the script can look up each file in the \RPM package manager database and determine if the original state of the file had the \SUID (or \SGID) permission set. Refer to section~\ref{sec:rpm}. \item[\textbf{A Poss. Soln:}]\mbox{} \lgrindfile{finduidgidcheck} \end{enumerate} \end{document}