s4-scripting: allow to specify the number max of iteration around getNcChanges
[sfrench/samba-autobuild/.git] / source4 / scripting / bin / ktpass.sh
1 #!/bin/sh
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 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   if [ ! -f ${path}ldbsearch ]; then
59     path=`dirname $0`/../../bin/
60   fi
61 fi
62 if [ -z "$outfile" -o -z "$princ" -o -z "$pass" ]; then
63   echo "At least one mandatory parameter (--out, --princ, --pass) was not specified"
64   usage
65 fi
66 if [ -z $host ]; then
67   host=`hostname`
68 fi
69
70 kvno=`${path}ldbsearch -H ldap://$host "(|(samaccountname=$princ)(serviceprincipalname=$princ)(userprincipalname=$princ))" msds-keyversionnumber  -k 1 -N 2>/dev/null| grep -i msds-keyversionnumber`
71 if [ x"$kvno" = x"" ]; then
72   echo -ne "Unable to find kvno for principal $princ\n"
73   echo -ne " check that you are authentified with kerberos\n"
74   exit 1
75 else
76   kvno=`echo $kvno | sed 's/^.*: //'`
77 fi
78
79 if [ "$pass" = "*" ]; then
80   echo -n "Enter password for $princ: "
81   stty -echo
82   read pass
83   stty echo
84   echo ""
85 fi
86
87 ktutil >/dev/null <<EOF
88 add_entry -password -p $princ -k $kvno -e $enc
89 $pass
90 wkt $outfile
91 EOF
92
93 if [ $? -eq 0 ]; then
94    echo "Keytab file $outfile created with success"
95 else
96   echo "Error while creating the keytab file $outfile"
97 fi