#!/bin/bash
# add machine account
# paul@vandervlis.nl

# ask machine when not on commandline:
if test "$1" = ""; then
  read -p "Machine, e.g. 'pc1.example.com' : " machine
else
  machine=$1
fi
if test "$machine" = ""; then
  echo "no machine name."
  exit
fi
machine=$(echo "${machine}" | tr '[:upper:]' '[:lower:]')

# test for correctness of the name:
if ! echo "$machine" | LC_ALL=C grep -Eq '^[a-z0-9_.][a-z0-9_.-]{0,31}$' ; then
    echo "Error: incorrect machine account name"; exit
fi

# add machine
kadmin.local -q "addprinc -randkey -policy machine nfs/$machine"

# nscd restart
service nscd restart > /dev/null

# log
echo "`date` created machine $machine" >> /var/log/au.log

