s3: smbd: Make extract_snapshot_token() a wrapper for extract_snapshot_token_internal().
[Samba.git] / source4 / scripting / bin / ktpass.sh
bloba165816b1c3e9a5458af7a8188630969618a8447
1 #!/bin/sh
2 # vim: expandtab
4 # Copyright (C) Matthieu Patou <mat@matws.net> 2010
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.
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.
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/>.
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"
26 usage()
28 echo -ne "$name --out <keytabfile> --princ <principal> --pass <password>|*\n"
29 echo -ne " [--host hostname] [--enc <encryption>]\n"
30 echo -ne " [--ptype <type>] [--path-to-ldbsearch <path>]\n"
31 echo -ne "\nEncoding should be one of:\n"
32 echo -ne " * des-cbc-crc\n"
33 echo -ne " * des-cbc-md5\n"
34 echo -ne " * rc4-hmac (default)\n"
35 echo -ne " * aes128-cts\n"
36 echo -ne " * aes256-cts\n"
37 exit 0
39 while true; do
40 case "$1" in
41 --out)
42 outfile=$2
43 shift 2
45 --princ)
46 princ=$2
47 shift 2
49 --pass)
50 pass=$2
51 shift 2
53 --host)
54 host=$2
55 shift 2
57 --ptype) shift 2 ;;
58 --enc)
59 enc=$2
60 shift 2
62 --path-to-ldbsearch)
63 path="$2/"
64 shift 2
66 -h) usage ;;
67 --)
68 shift
69 break
72 echo "Internal error!"
73 exit 1
75 esac
76 done
77 #RC4-HMAC-NT|AES256-SHA1|AES128-SHA
78 if [ -z "$enc" ]; then
79 enc="rc4-hmac"
81 if [ -z "$path" ]; then
82 path=$(dirname $0)/../bin/
83 if [ ! -f ${path}ldbsearch ]; then
84 path=$(dirname $0)/../../bin/
87 if [ -z "$outfile" -o -z "$princ" -o -z "$pass" ]; then
88 echo "At least one mandatory parameter (--out, --princ, --pass) was not specified"
89 usage
91 if [ -z $host ]; then
92 host=$(hostname)
95 kvno=$(${path}ldbsearch -H ldap://$host "(|(samaccountname=$princ)(serviceprincipalname=$princ)(userprincipalname=$princ))" msds-keyversionnumber -k 1 -N 2>/dev/null | grep -i msds-keyversionnumber)
96 if [ x"$kvno" = x"" ]; then
97 echo -ne "Unable to find kvno for principal $princ\n"
98 echo -ne " check that you are authentified with kerberos\n"
99 exit 1
100 else
101 kvno=$(echo $kvno | sed 's/^.*: //')
104 if [ "$pass" = "*" ]; then
105 echo -n "Enter password for $princ: "
106 stty -echo
107 read pass
108 stty echo
109 echo ""
112 ktutil >/dev/null <<EOF
113 add_entry -password -p $princ -k $kvno -e $enc
114 $pass
115 wkt $outfile
118 if [ $? -eq 0 ]; then
119 echo "Keytab file $outfile created with success"
120 else
121 echo "Error while creating the keytab file $outfile"