s4: create a simple version of ktpass
[samba.git] / source4 / scripting / bin / ktpass.sh
1 #!/usr/bin/env bash
2 # vim: expandtab
3 #
4 # Copyright (C) Matthieu Patou <mat@matws.net>  2010
5 #
6 #
7 #
8 # This program is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
12 #
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
21 name="ktpass.sh"
22 TEMP=`getopt -o h --long princ:,pass:,out:,host:,ptype:,enc:,path-to-ldbsearch: \
23      -n "$name" -- "$@"`
24 eval set -- "$TEMP"
25
26 function usage {
27   echo -ne "$name --out <keytabfile> --princ <principal> --pass <password>|*\n"
28   echo -ne "      [--host hostname] [--enc <encryption>]\n"
29   echo -ne "      [--ptype <type>] [--path-to-ldbsearch <path>]\n"
30   echo -ne "\nEncoding should be one of:\n"
31   echo -ne " * des-cbc-crc\n"
32   echo -ne " * des-cbc-md5\n"
33   echo -ne " * rc4-hmac (default)\n"
34   echo -ne " * aes128-cts\n"
35   echo -ne " * aes256-cts\n"
36   exit 0
37 }
38 while true ; do
39   case "$1" in
40     --out) outfile=$2 ; shift 2 ;;
41     --princ) princ=$2 ; shift 2 ;;
42     --pass) pass=$2 ; shift 2 ;;
43     --host) host=$2 ; shift 2 ;;
44     --ptype) shift 2 ;;
45     --enc) enc=$2; shift 2;;
46     --path-to-ldbsearch) path="$2/"; shift 2;;
47     -h) usage;;
48     --) shift ; break ;;
49     *) echo "Internal error!" ; exit 1 ;;
50   esac
51 done
52 #RC4-HMAC-NT|AES256-SHA1|AES128-SHA
53 if [ -z "$enc" ]; then
54     enc="rc4-hmac"
55 fi
56 if [ -z "$path" ]; then
57   path=`dirname $0`/../bin/
58 fi
59 if [ -z "$outfile" -o -z "$princ" -o -z "$pass" ]; then
60   echo "At least one mandatory parameter (--out, --princ, --pass) was not specified"
61   usage
62 fi
63 if [ -z $host ]; then
64   host=`hostname`
65 fi
66 kvno=`${path}ldbsearch -H ldap://$host "(|(samaccountname=$princ)(serviceprincipalname=$princ))" msds-keyversionnumber  -k 1 -N 2>/dev/null| grep -i msds-keyversionnumber`
67 if [ "$kvno" == "" ]; then
68   echo -ne "Unable to find kvno for principal $princ\n"
69   echo -ne " check that you are authentified with kerberos\n"
70   exit 1
71 else
72   kvno=`echo $kvno | sed 's/^.*: //'`
73 fi
74
75 if [ "$pass" == "*" ]; then
76   echo -n "Enter password for $princ: "
77   stty -echo
78   read pass
79   stty echo
80   echo ""
81 fi
82
83 ktutil >/dev/null <<EOF
84 add_entry -password -p $princ -k $kvno -e $enc
85 $pass
86 wkt $outfile
87 EOF
88
89 if [ $? -eq 0 ]; then
90    echo "Keytab file $outfile created with success"
91 else
92   echo "Error while creating the keytab file $outfile"
93 fi