rafael_grether

unrealircd

Oct 1st, 2021 (edited)
51
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/sh
  2.  
  3. PID_FILE="/opt/unrealircd/data/unrealircd.pid"
  4. PID_BACKUP="/opt/unrealircd/data/unrealircd.pid.bak"
  5. if [ ! -f /opt/unrealircd/bin/unrealircd ]; then
  6.     echo "ERROR: Could not find the IRCd binary (/opt/unrealircd/bin/unrealircd)"
  7.     echo "This could mean two things:"
  8.     echo "1) You forgot to run 'make install' after running 'make'"
  9.     echo "2) You answered a ./Config question incorrectly"
  10.     exit
  11. fi
  12. if [ "$1" = "start" ] ; then
  13.     echo "Starting UnrealIRCd"
  14.     if [ -r $PID_FILE ] ; then
  15.         mv -f $PID_FILE $PID_BACKUP
  16.     fi
  17.     # When built with --with-asan, ASan does not dump core by default because
  18.     # older gcc/clang might dump a 16TB core file. We explicitly enable it here.
  19.     export ASAN_OPTIONS="abort_on_error=1:disable_coredump=0:unmap_shadow_on_exit=1:log_path=/opt/unrealircd/tmp/unrealircd_asan:detect_leaks=0"
  20.  
  21.     # Check if ~/Unrealxxx/unrealircd.conf exists but the file
  22.     # ~/unrealircd/conf/unrealircd.conf does not.
  23.     # If so, then assume a user-build and give the user a nice hint...
  24.     if [ ! -f /opt/unrealircd/conf/unrealircd.conf -a -f /home/unrealircd/unrealircd-5.2.1.1/unrealircd.conf ]; then
  25.         echo ""
  26.         echo "There is no unrealircd.conf in /opt/unrealircd/conf"
  27.         echo "However I did find an unrealircd.conf in /home/unrealircd/unrealircd-5.2.1.1"
  28.         echo "With UnrealIRCd 4 you should no longer run the IRCd from /home/unrealircd/unrealircd-5.2.1.1."
  29.         echo "You should 'cd /opt/unrealircd' and work from there."
  30.         echo "See https://www.unrealircd.org/docs/UnrealIRCd_files_and_directories"
  31.         exit 1
  32.     fi
  33.     if [ ! -f /opt/unrealircd/conf/unrealircd.conf ]; then
  34.         echo ""
  35.         echo "The configuration file does not exist (/opt/unrealircd/conf/unrealircd.conf)."
  36.         echo "Create one using the example configuration file, see the documentation:"
  37.         echo "https://www.unrealircd.org/docs/Installing_from_source#Creating_a_configuration_file"
  38.         exit 1
  39.     fi
  40.     /opt/unrealircd/bin/unrealircd
  41.     if [ $? -ne 0 ] ; then
  42.         echo "====================================================="
  43.         echo "UnrealIRCd failed to start. Check above for possible errors."
  44.         echo "If you don't understand the problem, then have a look at our:"
  45.         echo "* FAQ (Frequently Asked Questions): https://www.unrealircd.org/docs/FAQ"
  46.         echo "* Documentation: https://www.unrealircd.org/docs/"
  47.         echo "====================================================="
  48.         if [ -r $PID_BACKUP ] ; then
  49.             mv -f $PID_BACKUP $PID_FILE
  50.         fi
  51.         exit 1
  52.     fi
  53.     # Now check if we need to create a crash report.
  54.     /opt/unrealircd/bin/unrealircd -R
  55. elif [ "$1" = "stop" ] ; then
  56.     echo -n "Stopping UnrealIRCd"
  57.     if [ ! -r $PID_FILE ] ; then
  58.         echo
  59.         echo "ERROR: UnrealIRCd is not running"
  60.         exit 1
  61.     fi
  62.     kill -15 `cat $PID_FILE`
  63.     if [ "$?" != 0 ]; then
  64.         echo
  65.         echo "ERROR: UnrealIRCd is not running"
  66.         exit 1
  67.     fi
  68.     # Wait for UnrealIRCd to terminate, but wait 10 seconds max
  69.     n="0"
  70.     while [ "$n" -lt 10 ]
  71.     do
  72.         echo -n "."
  73.         if [ ! -r $PID_FILE ] ; then
  74.             break
  75.         fi
  76.         if ! kill -0 `cat $PID_FILE`; then
  77.             break
  78.         fi
  79.         n=`expr $n + 1`
  80.         sleep 1
  81.     done
  82.     echo
  83.     # In case it is still running, kill it for good.
  84.     if [ -r $PID_FILE ] ; then
  85.         kill -9 `cat $PID_FILE` 1>/dev/null 2>&1
  86.     fi
  87. elif [ "$1" = "rehash" ] ; then
  88.     echo "Rehashing UnrealIRCd"
  89.     if [ ! -r $PID_FILE ] ; then
  90.         echo "ERROR: UnrealIRCd is not running"
  91.         exit 1
  92.     fi
  93.     kill -1 `cat $PID_FILE`
  94.     if [ "$?" != 0 ]; then
  95.         echo "ERROR: UnrealIRCd is not running"
  96.         exit 1
  97.     fi
  98. elif [ "$1" = "restart" ] ; then
  99.     echo "Restarting UnrealIRCd"
  100.     $0 stop
  101.     $0 start
  102. elif [ "$1" = "croncheck" ] ; then
  103.     if [ -r $PID_FILE ] ; then
  104.         kill -CHLD `cat $PID_FILE` 1>/dev/null 2>&1
  105.         if [ "$?" = 0 ]; then
  106.             # IRCd is running, bail out silently.
  107.             exit 0
  108.         fi
  109.     fi
  110.     # PID file not found or found but stale
  111.     echo "UnrealIRCd is not running. Starting now..."
  112.     $0 start
  113. elif [ "$1" = "configtest" ] ; then
  114.     /opt/unrealircd/bin/unrealircd -c
  115. elif [ "$1" = "module" ] ; then
  116.     shift
  117.     /opt/unrealircd/bin/unrealircd -m $*
  118. elif [ "$1" = "reloadtls" ] ; then
  119.     echo "Reloading SSL/TLS certificates"
  120.     if [ ! -r $PID_FILE ] ; then
  121.         echo "ERROR: UnrealIRCd is not running"
  122.         exit 1
  123.     fi
  124.     kill -USR1 `cat $PID_FILE`
  125.     if [ "$?" != 0 ]; then
  126.         echo "ERROR: UnrealIRCd is not running"
  127.         exit 1
  128.     fi
  129. elif [ "$1" = "mkpasswd" ] ; then
  130.     /opt/unrealircd/bin/unrealircd -P $2 $3
  131. elif [ "$1" = "version" ] ; then
  132.     /opt/unrealircd/bin/unrealircd -v
  133. elif [ "$1" = "gencloak" ] ; then
  134.     /opt/unrealircd/bin/unrealircd -k
  135. elif [ "$1" = "upgrade-conf" ] ; then
  136.     /opt/unrealircd/bin/unrealircd -U
  137. elif [ "$1" = "backtrace" ] ; then
  138.     cd /opt/unrealircd/tmp
  139.  
  140.     modpath="/opt/unrealircd/modules"
  141.  
  142.     # Find the corefile
  143.     echo "Core files available:"
  144.     n="0"
  145.     for i in `echo *core*`
  146.     do
  147.         ls -l $i
  148.         n=`expr $n + 1`
  149.     done
  150.  
  151.     if [ "$n" -gt 1 ]; then
  152.         echo "Type the name of the core file you want to research:"
  153.         read corefile
  154.     elif [ "$i" = "*core*" -o "$n" -eq 0 ]; then
  155.         echo 'No core files found... Nothing to do'
  156.         echo ''
  157.         echo 'If you are sure UnrealIRCd crashed, then verify that unreal'
  158.         echo 'has permission to dump core (type "ulimit -c unlimited" and see'
  159.         echo 'if you get permission denied errors). Also verify that you did'
  160.         echo 'not run out of quota.'
  161.         echo 'If all that is ok, then it might be that UnrealIRCd did not crash but'
  162.         echo 'got killed by the OS (eg: cpu/mem resource limits), the syadmin,'
  163.         echo 'or an automated process.'
  164.         exit 1
  165.     else
  166.         corefile="$i"
  167.     fi
  168.  
  169.     if [ ! -f "$corefile" ]; then
  170.         echo "Core file '$corefile' not found"
  171.     fi
  172.     if [ ! -s "$corefile" ]; then
  173.         echo 'Seems the corefile is 0 bytes'
  174.         echo 'This usually means you need to relax the core file resource limit'
  175.         echo '(type "ulimit -c unlimited"), or you might have ran out of quota.'
  176.         exit 1
  177.     fi
  178.  
  179.     # This is needed for the script below and is probably also helpful for the
  180.     # bug report since you usually want to paste this to the development team.
  181.     export LANG=C
  182.     export LC_ALL=C
  183.  
  184.     # The tmp/*.so files are often already deleted. Here we have some
  185.     # (ugly) scripting to recreate the tmp/*.so links to the modules *.so files...
  186.     echo 'info sharedlibrary'|gdb /opt/unrealircd/bin/unrealircd $corefile 2>/dev/null|\
  187.     grep No|grep tmp/|awk '{ print $2 }'|\
  188.     awk -F '.' "{ system(\"[ -f $modpath/\" \$2 \"/\" \$3 \".so ] && ln -s $modpath/\" \$2 \"/\" \$3 \".so \" \$0 \" || ln -s $modpath/\" \$2 \".so \" \$0) }"
  189.    
  190.     echo ""
  191.     echo "=================== START HERE ======================"
  192.     echo "BACKTRACE:"
  193.  
  194. cat >/opt/unrealircd/tmp/gdb.commands << __EOF__
  195. bt
  196. echo \n
  197. frame
  198. echo \n
  199. x/s backupbuf
  200. echo \n
  201. bt 3 full
  202. quit
  203. __EOF__
  204.  
  205.     gdb -batch -x /opt/unrealircd/tmp/gdb.commands /opt/unrealircd/bin/unrealircd $corefile
  206.     rm -f /opt/unrealircd/tmp/gdb.commands
  207.     echo "GCC: `gcc -v 2>&1|tail -n 1`"
  208.     echo "UNAME: `uname -a`"
  209.     echo "UNREAL: `$0 version`"
  210.     echo "CORE: `ls -al $corefile`"
  211.     echo "===================  STOP HERE ======================"
  212.     echo ""
  213.     echo "Copy the parts between the START HERE and STOP HERE marker"
  214.     echo "and report it on https://bugs.unrealircd.org/"
  215.     echo ""
  216.     echo 'But before you do, note the following:'
  217.     echo '1. We do not support modifications of any unrealircd code'
  218.     echo '   (except for config.h changes).'
  219.     echo '2. If you are using 3rd party modules we might request you'
  220.     echo '   to run without them and verify you still crash. This is'
  221.     echo '   to eleminate any loss of time due to bugs made by others'
  222.     echo '3. Use a reasonably recent UnrealIRCd version. We fix (crash)bugs'
  223.     echo '   all the time so your bug might as well be fixed already.'
  224.     echo ""
  225.     echo "Thanks!"
  226. elif [ "$1" = "spki" -o "$1" = "spkifp" ] ; then
  227.     CERT="/opt/unrealircd/conf/tls/server.cert.pem"
  228.     if [ "$2" != "" ]; then
  229.         CERT="$2"
  230.     fi
  231.     if [ ! -f "$CERT" ]; then
  232.         echo "Could not open certificate: $CERT"
  233.         exit 1
  234.     fi
  235.     openssl x509 -noout -in "$CERT" -pubkey | openssl asn1parse -noout -inform pem -out /opt/unrealircd/tmp/tmp.public.key
  236.     HASH="`openssl dgst -sha256 -binary /opt/unrealircd/tmp/tmp.public.key | openssl enc -base64`"
  237.     rm -f /opt/unrealircd/tmp/tmp.public.key
  238.     if [ "$HASH" = "" ]; then
  239.         echo "Sorry, something went wrong when generating the SPKI fingerprint."
  240.         echo "Is the 'openssl' tool properly installed?"
  241.         exit 1
  242.     fi
  243.     echo "The SPKI fingerprint for certificate $CERT is:"
  244.     echo "$HASH"
  245.     echo ""
  246.     echo "You normally add this password on the other side of the link as:"
  247.     echo "password \"$HASH\" { spkifp; };"
  248.     echo ""
  249. elif [ "$1" = "hot-patch" -o "$1" = "cold-patch" ] ; then
  250.     if [ ! -d "/home/unrealircd/unrealircd-5.2.1.1" ]; then
  251.         echo "UnrealIRCd source not found. Sorry, it is not possible to patch."
  252.         exit 1
  253.     fi
  254.     if [ "$2" = "" ]; then
  255.         echo "Argument required: ./unrealircd <hot-patch|cold-patch> <name-of-patch>"
  256.         exit 1
  257.     fi
  258.     if ! wget --help 1>/dev/null 2>&1; then
  259.         echo "The tool 'wget' is missing, which is used by this script."
  260.         echo "On Linux consider running 'apt install wget' or a similar command."
  261.         exit 1
  262.     fi
  263.     cd "/home/unrealircd/unrealircd-5.2.1.1" || exit 1
  264.  
  265.     # Weird way to get version, but ok.
  266.     UNREALVER="`./configure --version|head -n1|awk '{ print $3 }'`"
  267.     wget -O patch "https://www.unrealircd.org/patch?type=$1&patch=$2&version=$UNREALVER" || exit 1
  268.  
  269.     # A patch file of 0 bytes means the patch is not needed
  270.     if [ -f patch -a ! -s patch ]; then
  271.         echo "This UnrealIRCd version does not require that patch"
  272.     fi
  273.  
  274.     if patch --dry-run -p1 -R <patch 1>/dev/null 2>&1; then
  275.         echo "Patch already applied. Nothing to do."
  276.         exit 1
  277.     fi
  278.  
  279.     if ! patch -p1 <patch; then
  280.         echo "Patch failed to apply"
  281.         exit 1
  282.     fi
  283.  
  284.     echo "Patch applied successfully. Now recompiling..."
  285.     make || gmake || exit 1
  286.     make install || gmake install || exit 1
  287.  
  288.     cd -
  289.     if [ "$1" = "hot-patch" ]; then
  290.         echo "Patch applied successfully and installed. Rehashing your IRCd..."
  291.         ./unrealircd rehash
  292.         echo "Done! All should be good now."
  293.     else
  294.         echo "Patch applied successfully. You must now restart your IRC server."
  295.     fi
  296. elif [ "$1" = "upgrade" ] ; then
  297.     /opt/unrealircd/bin/unrealircd-upgrade-script $*
  298. elif [ "$1" = "genlinkblock" ] ; then
  299.     /opt/unrealircd/bin/unrealircd -L
  300. else
  301.     if [ "$1" = "" ]; then
  302.         echo "This script expects a parameter. Use:"
  303.     else
  304.         echo "Unrecognized parameter '$1'. Use:"
  305.     fi
  306.     echo "unrealircd configtest    Test the configuration file"
  307.     echo "unrealircd start         Start the IRC Server"
  308.     echo "unrealircd stop          Stop (kill) the IRC Server"
  309.     echo "unrealircd rehash        Reload the configuration file"
  310.     echo "unrealircd reloadtls     Reload the SSL/TLS certificates"
  311.     echo "unrealircd restart       Restart the IRC Server (stop+start)"
  312.     echo "unrealircd upgrade       Upgrade UnrealIRCd to the latest version"
  313.     echo "unrealircd upgrade-conf  Upgrade the configuration file from UnrealIRCd"
  314.     echo "                         3.2.x/4.x to 5.x format"
  315.     echo "unrealircd mkpasswd      Hash a password"
  316.     echo "unrealircd version       Display the UnrealIRCd version"
  317.     echo "unrealircd module        Install and uninstall 3rd party modules"
  318.     echo "unrealircd croncheck     For use in crontab: this checks if the server"
  319.     echo "                         is running. If not, the server is started."
  320.     echo "unrealircd genlinkblock  Generate link { } block for the other side."
  321.     echo "unrealircd gencloak      Display 3 random cloak keys"
  322.     echo "unrealircd spkifp        Display SPKI Fingerprint"
  323. fi
RAW Paste Data