#!/bin/bash ################################################################################ # Copyright 2011 Amazon.com, Inc. or its affiliates. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"). You may not # use this file except in compliance with the License. A copy of the License is # located at # # http://aws.amazon.com/apache2.0/ # # or in the "license" file accompanying this file. This file is distributed on # an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express # or implied. See the License for the specific language governing permissions # and limitations under the License. ################################################################################ if [ $(/usr/bin/id -u) -ne 0 ]; then echo "You are not root" exit 1 fi # Parse the few supported options case "$1" in --enable) rm -f /var/lib/update-motd/disabled exit 0;; --disable) mkdir -p /var/lib/update-motd/ touch /var/lib/update-motd/disabled exit 0;; '' | --force);; *) echo "update-motd [option]" echo " --disable: Disable update-motd" echo " --enable: Enable update-motd" echo " --force: Ignore disabled setting" exit 1;; esac # Just exit if update-motd is disabled and --force wasn't passed if [ -e /var/lib/update-motd/disabled ] && [ "$1" != "--force" ] then exit 0 fi if [ -d /etc/update-motd.d ]; then TMPFILE=$(mktemp --tmpdir motd.XXXXX) if [ -f /etc/motd.head ]; then cat /etc/motd.head >> $TMPFILE fi # Simulated run-parts, wildcards in bash are expanded sorted # Skip files ending in ~ or , for part in /etc/update-motd.d/*[^~,]; do # Skip .rpmnew, etc. [[ $part =~ \.rpm* ]] && continue # Run only if it's a regular file and executable if [ -f $part ] && [ -x $part ]; then TMPPART=$(mktemp --tmpdir motd.partXXXXX) if (timeout 30s $part > $TMPPART); then cat $TMPPART >> $TMPFILE fi rm -f $TMPPART fi done if [ -f /etc/motd.tail ]; then cat /etc/motd.tail >> $TMPFILE fi # Only actually do the replacement if there is something in there if [ -s $TMPFILE ]; then # mktemp creates files with only user read-write permissions chmod go+r $TMPFILE mv $TMPFILE /var/lib/update-motd/motd else # Don't leave temp files rm -f $TMPFILE fi fi