| 1 | #!/usr/bin/env bash |
| 2 | # Turn on extended globbing |
| 3 | shopt -s extglob |
| 4 | # Bail on any error |
| 5 | set -e |
| 6 | |
| 7 | prog=$(basename $0) |
| 8 | |
| 9 | print_help() { |
| 10 | cat <<EOF |
| 11 | NAME |
| 12 | $prog - Escalate Asterisk logging levels |
| 13 | |
| 14 | SYNOPSIS |
| 15 | $prog [ --help ] | [ [ --reset ] | [ |
| 16 | [ --uniqueid="<uniqueid>" ] |
| 17 | |
| 18 | [ --pjsip-debug=<on|off> ] |
| 19 | [ --iax2-debug=<on|off> ] |
| 20 | |
| 21 | [ --agi-debug=<on|off> ] [ --ami-debug=<on|off> ] |
| 22 | [ --ari-debug=<on|off> ] [ --cdr-debug=<on|off> ] |
| 23 | [ --channel-debug=<on|off> ] [ --rtp-debug=<on|off> ] |
| 24 | [ --rtcp-debug=<on|off> ] |
| 25 | |
| 26 | [ --dtmf-debug=<on|off> ] [ --fax-debug=<on|off> ] |
| 27 | [ --security-debug=<on|off> ] |
| 28 | |
| 29 | [ --pjsip-history=<on|off> ] |
| 30 | |
| 31 | [ --verbose=<level> ] [ --debug=<level> ] |
| 32 | ] ] |
| 33 | |
| 34 | DESCRIPTION |
| 35 | |
| 36 | Escalates log and/or debug levels on Asterisk subsystems. |
| 37 | |
| 38 | Options: |
| 39 | |
| 40 | --help |
| 41 | Print this help. |
| 42 | |
| 43 | --reset |
| 44 | Resets logging to the pre-escalation state. |
| 45 | |
| 46 | --uniqueid="<uniqueid>" |
| 47 | Normally DATEFORMAT from ast_debug_tools.conf is used to make |
| 48 | the log files unique but you can set the unique id to |
| 49 | something else such as the Jira issue. Once any logging |
| 50 | is enabled, the uniqueid is stored in cli.conf so any future |
| 51 | on/off commands will use the same uniqueid. Use the --reset |
| 52 | option to reset it (and everything else). |
| 53 | |
| 54 | --pjsip-debug --iax2-debug --agi-debug --ami-debug |
| 55 | --ari-debug --cdr-debug --channel-debug --rtp-debug --rtcp-debug |
| 56 | Issues the subsystem appropriate command to turn on |
| 57 | or off debugging. These are usually functional debug messages |
| 58 | such as packet dumps as opposed to code level messages and usually |
| 59 | go to the VERBOSE log channel. |
| 60 | |
| 61 | --dtmf-debug --fax-debug --security-debug |
| 62 | These subsystems set up their own log channels so if turned |
| 63 | on, log files will be created in \$astlogdir for them. |
| 64 | |
| 65 | --pjsip-history |
| 66 | The pjsip channels have the ability to output an abbreviated, |
| 67 | one-line, packet summary. If enabled, the summaries will be |
| 68 | written to \$astlogdir/pjsip_history.\$UNIQUEID and |
| 69 | \$astlogdir/sip_history.\$UNIQUEID. |
| 70 | |
| 71 | --verbose-level --debug-level |
| 72 | Sets the levels for their respective messages. |
| 73 | |
| 74 | NOTES |
| 75 | |
| 76 | The escalator works by creating a set of startup commands in cli.conf |
| 77 | that set up logger channels and issue the debug commands. If asterisk |
| 78 | is running when $prog is executed, the same commands will be issued |
| 79 | to the running instance. The original cli.conf is saved before any |
| 80 | changes are made and can be restored by executing '$prog --reset'. |
| 81 | |
| 82 | The log output will be stored in... |
| 83 | \$astlogdir/message.\$uniqueid |
| 84 | \$astlogdir/debug.\$uniqueid |
| 85 | \$astlogdir/dtmf.\$uniqueid |
| 86 | \$astlogdir/fax.\$uniqueid |
| 87 | \$astlogdir/security.\$uniqueid |
| 88 | \$astlogdir/pjsip_history.\$uniqueid |
| 89 | \$astlogdir/sip_history.\$uniqueid |
| 90 | |
| 91 | EOF |
| 92 | exit 1 |
| 93 | } |
| 94 | |
| 95 | PJSIP_DEBUG_SPECIFIED=false |
| 96 | PJSIP_HISTORY_SPECIFIED=false |
| 97 | SIP_DEBUG_SPECIFIED=false |
| 98 | SIP_HISTORY_SPECIFIED=false |
| 99 | IAX2_DEBUG_SPECIFIED=false |
| 100 | ARI_DEBUG_SPECIFIED=false |
| 101 | AMI_DEBUG_SPECIFIED=false |
| 102 | AGI_DEBUG_SPECIFIED=false |
| 103 | CDR_DEBUG_SPECIFIED=false |
| 104 | CHANNEL_DEBUG_SPECIFIED=false |
| 105 | RTP_DEBUG_SPECIFIED=false |
| 106 | RTCP_DEBUG_SPECIFIED=false |
| 107 | DTMF_DEBUG_SPECIFIED=false |
| 108 | FAX_DEBUG_SPECIFIED=false |
| 109 | SECURITY_DEBUG_SPECIFIED=false |
| 110 | DEBUG_LEVEL_SPECIFIED=false |
| 111 | VERBOSE_LEVEL_SPECIFIED=false |
| 112 | DEBUGS=false |
| 113 | RESET=false |
| 114 | |
| 115 | declare -A DEBUG_COMMANDS=( |
| 116 | [PJSIP,on]="pjsip set logger on" [PJSIP,off]="pjsip set logger off" |
| 117 | [IAX2,on]="iax2 set debug on" [IAX2,off]="iax2 set debug off" |
| 118 | [ARI,on]="ari set debug all on" [ARI,off]="ari set debug all off" |
| 119 | [AMI,on]="manager set debug on" [AMI,off]="manager set debug off" |
| 120 | [AGI,on]="agi set debug on" [AGI,off]="agi set debug off" |
| 121 | [CDR,on]="cdr set debug on" [CDR,off]="cdr set debug off" |
| 122 | [CHANNEL,on]="core set debug channel all" [CHANNEL,off]="core set debug channel all off" |
| 123 | [RTP,on]="rtp set debug on" [RTP,on]="rtp set debug off" |
| 124 | [RTCP,on]="rtcp set debug on" [RTCP,off]="rtcp set debug off" |
| 125 | ) |
| 126 | |
| 127 | VERBOSE_LEVELS="NOTICE,WARNING,ERROR,VERBOSE" |
| 128 | DEBUG_LEVELS="DEBUG" |
| 129 | |
| 130 | # Function to safely source a config file with security checks |
| 131 | # This prevents privilege escalation by ensuring config files are |
| 132 | # owned by root and not writable by group or others when running as root |
| 133 | safe_source_config() { |
| 134 | local config_file="$1" |
| 135 | |
| 136 | # Return if file doesn't exist |
| 137 | [ -f "$config_file" ] || return 0 |
| 138 | |
| 139 | # Get the absolute path |
| 140 | config_file=$(readlink -f "$config_file") |
| 141 | |
| 142 | # Check if running as root (effective UID is 0) |
| 143 | if [ $EUID -eq 0 ]; then |
| 144 | # Running as root - apply strict security checks |
| 145 | # Get file owner UID and permissions |
| 146 | local file_stat |
| 147 | file_stat=$(stat -c "%u %a" "$config_file" 2>/dev/null) || return 0 |
| 148 | local owner_uid=${file_stat%% *} |
| 149 | local perms=${file_stat##* } |
| 150 | |
| 151 | # File must be owned by root (UID 0) |
| 152 | if [ "$owner_uid" -ne 0 ]; then |
| 153 | echo "WARNING: Config file $config_file is not owned by root. Skipping for security." >&2 |
| 154 | return 1 |
| 155 | fi |
| 156 | |
| 157 | # File must not be writable by group or others (check group-write and other-write bits) |
| 158 | # Extract the group and other permission digits |
| 159 | local group_perms=$((perms / 10 % 10)) |
| 160 | local other_perms=$((perms % 10)) |
| 161 | |
| 162 | if [ $((group_perms & 2)) -ne 0 ] || [ $((other_perms & 2)) -ne 0 ]; then |
| 163 | echo "WARNING: Config file $config_file is writable by group or others. Skipping for security." >&2 |
| 164 | return 1 |
| 165 | fi |
| 166 | fi |
| 167 | |
| 168 | source "$config_file" |
| 169 | } |
| 170 | |
| 171 | # Read config files from least important to most important |
| 172 | safe_source_config /etc/asterisk/ast_debug_tools.conf |
| 173 | safe_source_config ~/ast_debug_tools.conf |
| 174 | safe_source_config ./ast_debug_tools.conf |
| 175 | |
| 176 | DATEFORMAT=${DATEFORMAT:-'date +%FT%H-%M-%S%z'} |
| 177 | UNIQUEID=$($DATEFORMAT) |
| 178 | UNIQUEID_SPECIFIED=false |
| 179 | |
| 180 | for a in "$@" ; do |
| 181 | case "$a" in |
| 182 | --*-debug=*) |
| 183 | subsystem=${a%-debug=*} |
| 184 | subsystem=${subsystem#--} |
| 185 | flag=${a#*=} |
| 186 | if [[ ${flag,,} =~ ^y(es)?|on ]] ; then |
| 187 | eval ${subsystem^^}_DEBUG=true |
| 188 | else |
| 189 | eval ${subsystem^^}_DEBUG=false |
| 190 | fi |
| 191 | eval ${subsystem^^}_DEBUG_SPECIFIED=true |
| 192 | DEBUGS=true |
| 193 | ;; |
| 194 | --pjsip-history=*) |
| 195 | subsystem=${a%-history=*} |
| 196 | subsystem=${subsystem#--} |
| 197 | if [[ ${a#*=} =~ ^[Yy].* ]] ; then |
| 198 | eval ${subsystem^^}_HISTORY=true |
| 199 | else |
| 200 | eval ${subsystem^^}_HISTORY=false |
| 201 | fi |
| 202 | eval ${subsystem^^}_HISTORY_SPECIFIED=true |
| 203 | DEBUGS=true |
| 204 | ;; |
| 205 | --verbose=*) |
| 206 | VERBOSE_LEVEL=${a#*=} |
| 207 | VERBOSE_LEVEL_SPECIFIED=true |
| 208 | DEBUGS=true |
| 209 | ;; |
| 210 | --debug=*) |
| 211 | DEBUG_LEVEL=${a#*=} |
| 212 | DEBUG_LEVEL_SPECIFIED=true |
| 213 | DEBUGS=true |
| 214 | ;; |
| 215 | --reset) |
| 216 | RESET=true |
| 217 | ;; |
| 218 | --uniqueid=*) |
| 219 | UNIQUEID=${a#*=} |
| 220 | UNIQUEID_SPECIFIED=true |
| 221 | DEBUGS=true |
| 222 | ;; |
| 223 | --help|*) |
| 224 | print_help |
| 225 | ;; |
| 226 | esac |
| 227 | done |
| 228 | |
| 229 | if $DEBUGS && $RESET ; then |
| 230 | echo "--reset must be specified by itself" |
| 231 | print_help |
| 232 | fi |
| 233 | |
| 234 | if ! $DEBUGS && ! $RESET ; then |
| 235 | echo "No options specified." |
| 236 | print_help |
| 237 | fi |
| 238 | |
| 239 | ASTERISK_IS_RUNNING=false |
| 240 | CONFIG_DIR=/etc/asterisk |
| 241 | LOG_DIR=/var/log/asterisk |
| 242 | |
| 243 | if [ "$(pidof asterisk)" != "" ] ; then |
| 244 | CONFIG_DIR=`asterisk -rx "core show settings" | sed -n -r -e "s/^\s*Configuration\s+directory:\s+(.*)$/\1/gp"` |
| 245 | LOG_DIR=`asterisk -rx "core show settings" | sed -n -r -e "s/^\s*Log\s+directory:\s+(.*)$/\1/gp"` |
| 246 | ASTERISK_IS_RUNNING=true |
| 247 | fi |
| 248 | CLI_CONF="$CONFIG_DIR/cli.conf" |
| 249 | |
| 250 | if [ ! -f "$CLI_CONF" ] ; then |
| 251 | echo "The location of cli.conf could not be determined." |
| 252 | exit 1 |
| 253 | fi |
| 254 | |
| 255 | if $RESET ; then |
| 256 | if [ -f "$CLI_CONF.unescalated" ] ; then |
| 257 | mv "$CLI_CONF.unescalated" "$CLI_CONF" |
| 258 | fi |
| 259 | if $ASTERISK_IS_RUNNING ; then |
| 260 | ( |
| 261 | asterisk -rx "core set verbose 0" |
| 262 | asterisk -rx "core set debug 0" |
| 263 | asterisk -rx "pjsip set logger off" |
| 264 | asterisk -rx "pjsip set history off" |
| 265 | asterisk -rx "iax2 set debug off" |
| 266 | asterisk -rx "manager set debug off" |
| 267 | asterisk -rx "ari set debug all off" |
| 268 | asterisk -rx "agi set debug off" |
| 269 | asterisk -rx "rtp set debug off" |
| 270 | asterisk -rx "rtcp set debug off" |
| 271 | asterisk -rx "cdr set debug off" |
| 272 | asterisk -rx "core set debug channel all off" |
| 273 | asterisk -rx "logger reload" |
| 274 | ) >/dev/null 2>&1 || : |
| 275 | fi |
| 276 | exit 1 |
| 277 | fi |
| 278 | |
| 279 | if ! grep -q "; --START DEBUG_LOGGING-- ;" $CLI_CONF ; then |
| 280 | VERBOSE_LOG="$LOG_DIR/message.${UNIQUEID}" |
| 281 | DEBUG_LOG="$LOG_DIR/debug.${UNIQUEID}" |
| 282 | PJSIP_HISTORY_LOG="$LOG_DIR/pjsip_history.${UNIQUEID}" |
| 283 | SIP_HISTORY_LOG="$LOG_DIR/sip_history.${UNIQUEID}" |
| 284 | DTMF_LOG="$LOG_DIR/dtmf.${UNIQUEID}" |
| 285 | FAX_LOG="$LOG_DIR/fax.${UNIQUEID}" |
| 286 | SECURITY_LOG="$LOG_DIR/security.${UNIQUEID}" |
| 287 | |
| 288 | cp "$CLI_CONF" "$CLI_CONF.unescalated" |
| 289 | |
| 290 | sed -i -r -e "s/\[startup_commands\]/[startup_commands_original](!)/g" "$CLI_CONF" |
| 291 | |
| 292 | cat >> "$CLI_CONF" <<-EOF |
| 293 | ; --START DEBUG_LOGGING-- ; |
| 294 | |
| 295 | [pjsip_debug](!) |
| 296 | pjsip set logger on = yes |
| 297 | |
| 298 | [iax2_debug](!) |
| 299 | iax2 set debug on = yes |
| 300 | |
| 301 | [ari_debug](!) |
| 302 | ari set debug all on = yes |
| 303 | |
| 304 | [ami_debug](!) |
| 305 | manager set debug on = yes |
| 306 | |
| 307 | [agi_debug](!) |
| 308 | agi set debug on = yes |
| 309 | |
| 310 | [cdr_debug](!) |
| 311 | cdr set debug on = yes |
| 312 | |
| 313 | [channel_debug](!) |
| 314 | core set debug channel all = yes |
| 315 | |
| 316 | [rtp_debug](!) |
| 317 | rtp set debug on = yes |
| 318 | |
| 319 | [rtcp_debug](!) |
| 320 | rtcp set debug on = yes |
| 321 | |
| 322 | [dtmf_debug](!) |
| 323 | logger add channel $DTMF_LOG DTMF = yes |
| 324 | |
| 325 | [fax_debug](!) |
| 326 | logger add channel $FAX_LOG FAX = yes |
| 327 | |
| 328 | [security_debug](!) |
| 329 | logger add channel $SECURITY_LOG SECURITY = yes |
| 330 | |
| 331 | [pjsip_history](!) |
| 332 | logger add channel $PJSIP_HISTORY_LOG PJSIP_HISTORY = yes |
| 333 | pjsip set history on = yes |
| 334 | |
| 335 | [verbose_level](!) |
| 336 | core set verbose 3 = yes |
| 337 | |
| 338 | [debug_level](!) |
| 339 | core set debug 3 = yes |
| 340 | |
| 341 | [log_channels](!) |
| 342 | logger add channel $VERBOSE_LOG NOTICE,WARNING,ERROR,VERBOSE = yes |
| 343 | logger add channel $DEBUG_LOG DEBUG = yes |
| 344 | |
| 345 | [startup_commands](startup_commands_original,log_channels) |
| 346 | |
| 347 | ; --END DEBUG_LOGGING-- ; |
| 348 | EOF |
| 349 | |
| 350 | else |
| 351 | if $UNIQUEID_SPECIFIED ; then |
| 352 | echo "Debug logging is already active. Either rerun $prog without --uniqueid or with --reset to start over." |
| 353 | exit 1 |
| 354 | fi |
| 355 | |
| 356 | VERBOSE_LOG=$(sed -n -r -e "s@logger add channel ($LOG_DIR/message\..+)\s+NOTICE.*@\1@p" "$CLI_CONF") |
| 357 | DEBUG_LOG=$(sed -n -r -e "s@logger add channel ($LOG_DIR/debug\..+)\s+DEBUG.*@\1@p" "$CLI_CONF") |
| 358 | PJSIP_HISTORY_LOG=$(sed -n -r -e "s@logger add channel ($LOG_DIR/pjsip_history\..+)\s+PJSIP.*@\1@p" "$CLI_CONF") |
| 359 | DTMF_LOG=$(sed -n -r -e "s@logger add channel ($LOG_DIR/dtmf\..+)\s+DTMF.*@\1@p" "$CLI_CONF") |
| 360 | FAX_LOG=$(sed -n -r -e "s@logger add channel ($LOG_DIR/fax\..+)\s+FAX.*@\1@p" "$CLI_CONF") |
| 361 | SECURITY_LOG=$(sed -n -r -e "s@logger add channel ($LOG_DIR/security\..+)\s+SECURITY.*@\1@p" "$CLI_CONF") |
| 362 | fi |
| 363 | |
| 364 | for x in PJSIP ARI AMI AGI ARI IAX2 CDR RTP RTCP ; do |
| 365 | if eval \$${x}_DEBUG_SPECIFIED ; then |
| 366 | if eval \$${x}_DEBUG ; then |
| 367 | if $ASTERISK_IS_RUNNING ; then |
| 368 | asterisk -rx "${DEBUG_COMMANDS[$x,on]}" |
| 369 | fi |
| 370 | egrep -q "^\[startup_commands\].*${x,,}_debug.*" "$CLI_CONF" || |
| 371 | sed -i -r -e "/\[startup_commands\]/ s/\((.*)\)/(\1,${x,,}_debug)/g" "$CLI_CONF" |
| 372 | else |
| 373 | if $ASTERISK_IS_RUNNING ; then |
| 374 | asterisk -rx "${DEBUG_COMMANDS[$x,off]}" |
| 375 | fi |
| 376 | sed -i -r -e "/\[startup_commands\].*${x,,}_debug.*/ s/,${x,,}_debug//g" "$CLI_CONF" |
| 377 | fi |
| 378 | fi |
| 379 | done |
| 380 | |
| 381 | for x in DTMF FAX SECURITY ; do |
| 382 | if eval \$${x}_DEBUG_SPECIFIED ; then |
| 383 | if eval \$${x}_DEBUG ; then |
| 384 | if $ASTERISK_IS_RUNNING ; then |
| 385 | asterisk -rx "$(eval "echo logger add channel \$${x}_LOG ${x}")" >/dev/null 2>&1 |
| 386 | fi |
| 387 | egrep -q "^\[startup_commands\].*${x,,}_debug.*" "$CLI_CONF" || |
| 388 | sed -i -r -e "/\[startup_commands\]/ s/\((.*)\)/(\1,${x,,}_debug)/g" "$CLI_CONF" |
| 389 | else |
| 390 | if $ASTERISK_IS_RUNNING ; then |
| 391 | asterisk -rx "$(eval "echo logger remove channel \$${x}_LOG")" |
| 392 | fi |
| 393 | sed -i -r -e "/\[startup_commands\].*${x,,}_debug.*/ s/,${x,,}_debug//g" "$CLI_CONF" |
| 394 | fi |
| 395 | fi |
| 396 | done |
| 397 | |
| 398 | for x in PJSIP ; do |
| 399 | if eval \$${x}_HISTORY_SPECIFIED ; then |
| 400 | if eval \$${x}_HISTORY ; then |
| 401 | if $ASTERISK_IS_RUNNING ; then |
| 402 | asterisk -rx "$(eval "echo logger add channel \$${x}_HISTORY_LOG ${x}_HISTORY")" |
| 403 | asterisk -rx "${x,,} set history on" |
| 404 | fi |
| 405 | egrep -q "^\[startup_commands\].*${x,,}_history.*" "$CLI_CONF" || |
| 406 | sed -i -r -e "/\[startup_commands\]/ s/\((.*)\)/(\1,${x,,}_history)/g" "$CLI_CONF" |
| 407 | else |
| 408 | if $ASTERISK_IS_RUNNING ; then |
| 409 | asterisk -rx "$(eval "echo logger remove channel \$${x}_HISTORY_LOG")" |
| 410 | asterisk -rx "${x,,} set history off" |
| 411 | fi |
| 412 | sed -i -r -e "/\[startup_commands\].*${x,,}_history.*/ s/,${x,,}_history//g" "$CLI_CONF" |
| 413 | fi |
| 414 | fi |
| 415 | done |
| 416 | |
| 417 | for x in VERBOSE DEBUG ; do |
| 418 | if eval \$${x}_LEVEL_SPECIFIED ; then |
| 419 | if $ASTERISK_IS_RUNNING ; then |
| 420 | asterisk -rx "$(eval "echo logger add channel \$${x}_LOG \$${x}_LEVELS")" |
| 421 | asterisk -rx "$(eval "echo core set ${x,,} \$${x}_LEVEL")" |
| 422 | fi |
| 423 | sed -i -r -e "$(eval "echo s/core set ${x,,} .*/core set ${x,,} \$${x}_LEVEL/g")" "$CLI_CONF" |
| 424 | egrep -q "^\[startup_commands\].*${x,,}_level.*" "$CLI_CONF" || |
| 425 | sed -i -r -e "/\[startup_commands\]/ s/\((.*)\)/(\1,${x,,}_level)/g" "$CLI_CONF" |
| 426 | fi |
| 427 | done |