write_user_config.sh 953 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. #!/bin/bash
  2. SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
  3. source "$SCRIPT_DIR/functions.sh"
  4. show_usage() {
  5. echo "Usage: $0 <fullpath_user_ccd_file> [input_file|- for stdin]"
  6. echo "Example: $0 /etc/openvpn/server/server/ccd/user1 -"
  7. exit 1
  8. }
  9. main() {
  10. # Check permissions
  11. check_permissions
  12. # Process arguments
  13. [[ $# -lt 2 ]] && show_usage
  14. local ccd_file=$1
  15. local ccd_dir=$(dirname $ccd_file)
  16. local input_file=$2
  17. # Validate CCD directory path
  18. check_ccd_path "$ccd_dir"
  19. # Write config
  20. if [[ "$input_file" == "-" ]]; then
  21. # Read from stdin
  22. cat > "$ccd_file"
  23. else
  24. # Copy from existing file
  25. if [ -e "$ccd_file" ]; then
  26. rm -f "$ccd_file"
  27. fi
  28. cp "$input_file" "$ccd_file"
  29. fi
  30. chmod 660 "$ccd_file"
  31. chown ${owner_user}:${owner_group} "$ccd_file"
  32. log "Config saved to $ccd_file"
  33. exit 0
  34. }
  35. main "$@"