#!/bin/bash set -e printf "Host name: " >&2 if [ "$1" ]; then host="$1"; shift echo "$host" else read host fi if ! grep -Fq "." <<< "$host"; then host=$host.mit.edu; fi printf "User: " >&2 if [ "$1" ]; then user="$1"; shift echo "$user" else read user fi pw=$(perl -e '$, = ":"; print getpwnam($ARGV[0]);' -- "$user") if [ $? -ne 0 ]; then echo "User not found." >&2 exit $? fi IFS=: read user x uid gid x x x home x <<< "$pw" user_dn=$(ldapsearch -LLL -x -b ou=People,dc=scripts,dc=mit,dc=edu "(uid=$user)" dn | perl -0pe 's/\n //g; s/^dn: //') printf "Docroot: $home/web_scripts" >&2 read subdir tmpfile=$(mktemp -t vhostadd.XXXXXX) || exit $? trap 'rm -f "$tmpfile"' EXIT cat < "$tmpfile" dn: apacheServerName=$host,ou=VirtualHosts,dc=scripts,dc=mit,dc=edu objectClass: apacheConfig objectClass: top apacheServerName: $host EOF if [ "${host%mit.edu}" != "$host" ]; then cat <> "$tmpfile" apacheServerAlias: ${host%.mit.edu} EOF fi cat <> "$tmpfile" apacheDocumentRoot: $home/web_scripts$subdir apacheSuexecUid: $uid apacheSuexecGid: $gid dn: scriptsVhostName=$host,ou=VirtualHosts,dc=scripts,dc=mit,dc=edu objectClass: scriptsVhost objectClass: top scriptsVhostName: $host EOF if [ "${host%mit.edu}" != "$host" ]; then cat <> "$tmpfile" scriptsVhostAlias: ${host%.mit.edu} EOF fi cat <> "$tmpfile" scriptsVhostAccount: $user_dn scriptsVhostDirectory: ${subdir#/} EOF exec ldapvi --add --in "$tmpfile"