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