2010-12-02 12:34:34 +03:00
#!/bin/sh
2010-06-28 21:38:35 +04:00
# vim: expandtab
#
# Copyright (C) Matthieu Patou <mat@matws.net> 2010
#
#
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
name = "ktpass.sh"
2022-02-23 14:01:51 +03:00
TEMP = $( getopt -o h --long princ:,pass:,out:,host:,ptype:,enc:,path-to-ldbsearch: \
-n " $name " -- " $@ " )
2010-06-28 21:38:35 +04:00
eval set -- " $TEMP "
2022-02-23 14:01:51 +03:00
usage( )
{
echo -ne " $name --out <keytabfile> --princ <principal> --pass <password>|*\n "
echo -ne " [--host hostname] [--enc <encryption>]\n"
echo -ne " [--ptype <type>] [--path-to-ldbsearch <path>]\n"
echo -ne "\nEncoding should be one of:\n"
echo -ne " * des-cbc-crc\n"
echo -ne " * des-cbc-md5\n"
echo -ne " * rc4-hmac (default)\n"
echo -ne " * aes128-cts\n"
echo -ne " * aes256-cts\n"
exit 0
2010-06-28 21:38:35 +04:00
}
2022-02-23 14:01:51 +03:00
while true; do
case " $1 " in
--out)
outfile = $2
shift 2
; ;
--princ)
princ = $2
shift 2
; ;
--pass)
pass = $2
shift 2
; ;
--host)
host = $2
shift 2
; ;
--ptype) shift 2 ; ;
--enc)
enc = $2
shift 2
; ;
--path-to-ldbsearch)
path = " $2 / "
shift 2
; ;
-h) usage ; ;
--)
shift
break
; ;
*)
echo "Internal error!"
exit 1
; ;
esac
2010-06-28 21:38:35 +04:00
done
#RC4-HMAC-NT|AES256-SHA1|AES128-SHA
if [ -z " $enc " ] ; then
2022-02-23 14:01:51 +03:00
enc = "rc4-hmac"
2010-06-28 21:38:35 +04:00
fi
if [ -z " $path " ] ; then
2022-02-23 14:01:51 +03:00
path = $( dirname $0 ) /../bin/
if [ ! -f ${ path } ldbsearch ] ; then
path = $( dirname $0 ) /../../bin/
fi
2010-06-28 21:38:35 +04:00
fi
if [ -z " $outfile " -o -z " $princ " -o -z " $pass " ] ; then
2022-02-23 14:01:51 +03:00
echo "At least one mandatory parameter (--out, --princ, --pass) was not specified"
usage
2010-06-28 21:38:35 +04:00
fi
if [ -z $host ] ; then
2022-02-23 14:01:51 +03:00
host = $( hostname)
2010-06-28 21:38:35 +04:00
fi
2010-10-19 17:24:27 +04:00
2022-02-23 14:01:51 +03:00
kvno = $( ${ path } ldbsearch -H ldap://$host " (|(samaccountname= $princ )(serviceprincipalname= $princ )(userprincipalname= $princ )) " msds-keyversionnumber -k 1 -N 2>/dev/null | grep -i msds-keyversionnumber)
2010-12-02 12:34:34 +03:00
if [ x" $kvno " = x"" ] ; then
2022-02-23 14:01:51 +03:00
echo -ne " Unable to find kvno for principal $princ \n "
echo -ne " check that you are authentified with kerberos\n"
exit 1
2010-06-28 21:38:35 +04:00
else
2022-02-23 14:01:51 +03:00
kvno = $( echo $kvno | sed 's/^.*: //' )
2010-06-28 21:38:35 +04:00
fi
2010-12-02 12:34:34 +03:00
if [ " $pass " = "*" ] ; then
2022-02-23 14:01:51 +03:00
echo -n " Enter password for $princ : "
stty -echo
read pass
stty echo
echo ""
2010-06-28 21:38:35 +04:00
fi
ktutil >/dev/null <<EOF
add_entry -password -p $princ -k $kvno -e $enc
$pass
wkt $outfile
EOF
if [ $? -eq 0 ] ; then
2022-02-23 14:01:51 +03:00
echo " Keytab file $outfile created with success "
2010-06-28 21:38:35 +04:00
else
2022-02-23 14:01:51 +03:00
echo " Error while creating the keytab file $outfile "
2010-06-28 21:38:35 +04:00
fi