Skip to content

Instantly share code, notes, and snippets.

@youhey
Created September 20, 2012 03:52
Show Gist options
  • Save youhey/3753879 to your computer and use it in GitHub Desktop.
Save youhey/3753879 to your computer and use it in GitHub Desktop.
特定環境のWebDAVでユーザとディレクトリを作成
#!/bin/sh
set -e
set -u
WEBDAV_PATH="/var/www/WebDAV"
WEBDAV_URL="https://www.example.com/WebDAV/"
HTPASSWD=".htpasswd"
PASSWORD_LENGTH=16
HTTPD_USER="apache"
HTTPD_GROUP="apache"
SELFID=`id | sed -e 's/uid=//' -e 's/(.*//'`
if [ $SELFID -ne 0 ]; then
echo "You are not root, You cannot execute this script."
exit 100
fi
if [ $# -ne 1 ]; then
echo "Usage: webdav-useradd.sh USERNAME"
exit 101
fi
USERNAME=$1
if [ -z $USERNAME ]; then
echo "User name does not exists."
exit 102
fi
USER_PATH="${WEBDAV_PATH}/${USERNAME}"
if [ -d $USER_PATH ]; then
echo "User exists: username=${USERNAME}"
exit 103
fi
mkdir $USER_PATH
chown ${HTTPD_USER}:${HTTPD_GROUP} $USER_PATH
HTPASSWD_PATH="${USER_PATH}/${HTPASSWD}"
PASSWORD=`mkpasswd -l $PASSWORD_LENGTH`
htpasswd -b -c $HTPASSWD_PATH "${USERNAME}" "${PASSWORD}" > /dev/null
HTACCESS_PATH="${USER_PATH}/.htaccess"
echo 'AuthType Basic' > $HTACCESS_PATH
echo 'AuthName "Please Enter Your Password"' >> $HTACCESS_PATH
echo 'AuthBasicProvider file' >> $HTACCESS_PATH
echo "AuthUserFile ${HTPASSWD_PATH}" >> $HTACCESS_PATH
echo 'Require valid-user' >> $HTACCESS_PATH
printf "%s%s/\t%s\t%s\n" $WEBDAV_URL $USERNAME $USERNAME $PASSWORD
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment