k5srvutil 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #!/bin/sh
  2. # list_princs keytab
  3. # returns a list of principals in the keytab
  4. # sorted and uniquified
  5. list_princs() {
  6. klist -k $keytab | awk '(NR > 3) {print $2}' | sort | uniq
  7. }
  8. set_command() {
  9. if [ x$command != x ] ; then
  10. cmd_error Only one command can be specified
  11. usage
  12. exit 1
  13. fi
  14. command=$1
  15. }
  16. #interactive_prompt prompt princ
  17. # If in interactive mode return true if the principal should be acted on
  18. # otherwise return true all the time
  19. interactive_prompt() {
  20. if [ $interactive = 0 ] ; then
  21. return 0
  22. fi
  23. printf "%s for %s? [yn]" "$1" "$2"
  24. read ans
  25. case $ans in
  26. n*|N*)
  27. return 1
  28. ;;
  29. esac
  30. return 0
  31. }
  32. cmd_error() {
  33. echo $@ 2>&1
  34. }
  35. usage() {
  36. echo "Usage: $0 [-i] [-f file] [-e keysalts] list|change|delete|delold"
  37. }
  38. change_key() {
  39. princs=`list_princs `
  40. for princ in $princs; do
  41. if interactive_prompt "Change key " $princ; then
  42. kadmin -k -t $keytab -p $princ -q \
  43. "ktadd -k $keytab $keysalts $princ"
  44. fi
  45. done
  46. }
  47. delete_old_keys() {
  48. princs=`list_princs `
  49. for princ in $princs; do
  50. if interactive_prompt "Delete old keys " $princ; then
  51. kadmin -k -t $keytab -p $princ -q "ktrem -k $keytab $princ old"
  52. fi
  53. done
  54. }
  55. delete_keys() {
  56. interactive=1
  57. princs=`list_princs `
  58. for princ in $princs; do
  59. if interactive_prompt "Delete all keys " $princ; then
  60. kadmin -p $princ -k -t $keytab -q "ktrem -k $keytab $princ all"
  61. fi
  62. done
  63. }
  64. keytab=/etc/krb5.keytab
  65. interactive=0
  66. keysalts=""
  67. while [ $# -gt 0 ] ; do
  68. opt=$1
  69. shift
  70. case $opt in
  71. "-f")
  72. keytab=$1
  73. shift
  74. ;;
  75. "-i")
  76. interactive=1
  77. ;;
  78. "-e")
  79. keysalts="$keysalts -e \"$1\""
  80. shift
  81. ;;
  82. change|delold|delete|list)
  83. set_command $opt
  84. ;;
  85. *)
  86. cmd_error Illegal option: $opt
  87. usage
  88. exit 1
  89. ;;
  90. esac
  91. done
  92. case $command in
  93. change)
  94. change_key
  95. ;;
  96. delold)
  97. delete_old_keys
  98. ;;
  99. delete)
  100. delete_keys
  101. ;;
  102. list)
  103. klist -k $keytab
  104. ;;
  105. *)
  106. usage
  107. ;;
  108. esac