view_autodiscover.sh 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #!/bin/bash
  2. # Autodiscover XML Debug Script
  3. # Usage: ./view_autodiscover.sh [OPTIONS] [[email protected]]
  4. # Function to display help
  5. show_help() {
  6. cat << EOF
  7. Autodiscover XML Debug Script
  8. Usage: $0 [OPTIONS] [[email protected]]
  9. OPTIONS:
  10. -h, --help Show this help message
  11. -d, --domain FQDN Override autodiscover domain (default: autodiscover.DOMAIN)
  12. Example: -d mail.example.com
  13. EXAMPLES:
  14. $0 [email protected]
  15. Test autodiscover for [email protected] using autodiscover.example.com
  16. $0 -d mail.example.com [email protected]
  17. Test autodiscover for [email protected] using mail.example.com
  18. $0 -d localhost:8443 [email protected]
  19. Test autodiscover using localhost:8443 (useful for development)
  20. EOF
  21. exit 0
  22. }
  23. # Initialize variables
  24. EMAIL=""
  25. DOMAIN_OVERRIDE=""
  26. # Parse command line arguments
  27. while [[ $# -gt 0 ]]; do
  28. case $1 in
  29. -h|--help)
  30. show_help
  31. ;;
  32. -d|--domain)
  33. DOMAIN_OVERRIDE="$2"
  34. shift 2
  35. ;;
  36. -*)
  37. echo "Error: Unknown option $1"
  38. echo "Use -h or --help for usage information"
  39. exit 1
  40. ;;
  41. *)
  42. EMAIL="$1"
  43. shift
  44. ;;
  45. esac
  46. done
  47. # Check if xmllint is available
  48. if ! command -v xmllint &> /dev/null; then
  49. echo "WARNING: xmllint not found. Output will not be formatted."
  50. echo "Install with: apt install libxml2-utils (Debian/Ubuntu) or yum install libxml2 (CentOS/RHEL)"
  51. echo ""
  52. USE_XMLLINT=false
  53. else
  54. USE_XMLLINT=true
  55. fi
  56. # Get email address from user input if not provided
  57. if [ -z "$EMAIL" ]; then
  58. read -p "Enter email address to test: " EMAIL
  59. fi
  60. # Validate email format
  61. if [[ ! "$EMAIL" =~ ^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$ ]]; then
  62. echo "Error: Invalid email address format"
  63. exit 1
  64. fi
  65. # Extract domain from email
  66. EMAIL_DOMAIN="${EMAIL#*@}"
  67. # Determine autodiscover URL
  68. if [ -n "$DOMAIN_OVERRIDE" ]; then
  69. AUTODISCOVER_URL="https://${DOMAIN_OVERRIDE}/Autodiscover/Autodiscover.xml"
  70. echo "Testing Autodiscover for: $EMAIL"
  71. echo "Override domain: $DOMAIN_OVERRIDE"
  72. else
  73. AUTODISCOVER_URL="https://autodiscover.${EMAIL_DOMAIN}/Autodiscover/Autodiscover.xml"
  74. echo "Testing Autodiscover for: $EMAIL"
  75. fi
  76. echo "URL: $AUTODISCOVER_URL"
  77. echo "============================================"
  78. echo ""
  79. # Make the request
  80. RESPONSE=$(curl -k -s -X POST "$AUTODISCOVER_URL" \
  81. -H "Content-Type: text/xml" \
  82. -d "<?xml version=\"1.0\" encoding=\"utf-8\"?>
  83. <Autodiscover xmlns=\"http://schemas.microsoft.com/exchange/autodiscover/request/2006\">
  84. <Request>
  85. <EMailAddress>$EMAIL</EMailAddress>
  86. <AcceptableResponseSchema>http://schemas.microsoft.com/exchange/autodiscover/outlook/responseschema/2006a</AcceptableResponseSchema>
  87. </Request>
  88. </Autodiscover>")
  89. # Check if response is empty
  90. if [ -z "$RESPONSE" ]; then
  91. echo "Error: No response received from server"
  92. exit 1
  93. fi
  94. # Format and display output
  95. if [ "$USE_XMLLINT" = true ]; then
  96. echo "$RESPONSE" | xmllint --format - 2>&1
  97. else
  98. echo "$RESPONSE"
  99. fi
  100. echo ""
  101. echo "============================================"
  102. echo "Response length: ${#RESPONSE} bytes"