| 
									
										
										
										
											2016-09-21 18:32:15 +08:00
										 |  |  | #!/bin/sh | 
					
						
							| 
									
										
										
										
											2020-07-17 21:09:41 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | ## This Source Code Form is subject to the terms of the Mozilla Public | 
					
						
							|  |  |  | ## License, v. 2.0. If a copy of the MPL was not distributed with this | 
					
						
							|  |  |  | ## file, You can obtain one at https://mozilla.org/MPL/2.0/. | 
					
						
							| 
									
										
										
										
											2016-09-21 18:32:15 +08:00
										 |  |  | ## | 
					
						
							| 
									
										
										
										
											2023-11-22 12:32:51 +08:00
										 |  |  | ## Copyright (c) 2007-2023 Broadcom. All Rights Reserved. The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. | 
					
						
							| 
									
										
										
										
											2016-09-21 18:32:15 +08:00
										 |  |  | ## | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-28 23:52:54 +08:00
										 |  |  | SCRIPT="$(basename "$0")" | 
					
						
							| 
									
										
										
										
											2016-09-21 18:32:15 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-28 23:52:54 +08:00
										 |  |  | main() { | 
					
						
							| 
									
										
										
										
											2017-07-29 01:15:16 +08:00
										 |  |  |   ensure_we_are_in_a_readable_dir | 
					
						
							| 
									
										
										
										
											2017-08-02 00:17:39 +08:00
										 |  |  |   if current_user_is_rabbitmq && calling_rabbitmq_server | 
					
						
							| 
									
										
										
										
											2017-07-28 23:52:54 +08:00
										 |  |  |   then | 
					
						
							| 
									
										
										
										
											2017-08-02 00:17:39 +08:00
										 |  |  |     exec_rabbitmq_server "$@" | 
					
						
							|  |  |  |   elif current_user_is_rabbitmq && ! calling_rabbitmq_plugins | 
					
						
							|  |  |  |   then | 
					
						
							|  |  |  |     exec_script_as_rabbitmq "$@" | 
					
						
							|  |  |  |   elif current_user_is_root && calling_rabbitmq_plugins | 
					
						
							|  |  |  |   then | 
					
						
							| 
									
										
										
										
											2018-09-13 06:17:22 +08:00
										 |  |  |     run_script_as_rabbitmq "$@" | 
					
						
							| 
									
										
										
										
											2020-10-13 21:54:42 +08:00
										 |  |  |     _result="$?" | 
					
						
							| 
									
										
										
										
											2018-09-13 06:17:22 +08:00
										 |  |  |     maybe_fixup_erlang_cookie | 
					
						
							| 
									
										
										
										
											2020-10-13 21:54:42 +08:00
										 |  |  |     exit "$_result" | 
					
						
							| 
									
										
										
										
											2017-07-28 23:52:54 +08:00
										 |  |  |   elif current_user_is_root | 
					
						
							|  |  |  |   then | 
					
						
							| 
									
										
										
										
											2017-09-01 06:52:43 +08:00
										 |  |  |     exec_script_as_root "$@" | 
					
						
							| 
									
										
										
										
											2017-07-28 23:52:54 +08:00
										 |  |  |   else | 
					
						
							|  |  |  |     run_script_help_and_fail | 
					
						
							|  |  |  |   fi | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2016-09-21 18:32:15 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-13 06:17:22 +08:00
										 |  |  | maybe_fixup_erlang_cookie() { | 
					
						
							|  |  |  |     # rabbitmq/rabbitmq-server-release#85 | 
					
						
							|  |  |  |     _rabbitmq_home="$(cd ~rabbitmq && pwd)" | 
					
						
							|  |  |  |     _erlang_cookie="$_rabbitmq_home/.erlang.cookie" | 
					
						
							|  |  |  |     if [ -f "$_erlang_cookie" ] | 
					
						
							|  |  |  |     then | 
					
						
							| 
									
										
										
										
											2018-09-14 02:13:42 +08:00
										 |  |  |         chown @RABBITMQ_USER@:@RABBITMQ_GROUP@ "$_erlang_cookie" | 
					
						
							| 
									
										
										
										
											2018-09-13 06:17:22 +08:00
										 |  |  |         chmod 400 "$_erlang_cookie" | 
					
						
							|  |  |  |     fi | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-29 01:15:16 +08:00
										 |  |  | ensure_we_are_in_a_readable_dir() { | 
					
						
							|  |  |  |   # shellcheck disable=SC2164 | 
					
						
							|  |  |  |   cd ~rabbitmq || cd / | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-28 23:52:54 +08:00
										 |  |  | current_user_is_rabbitmq() { | 
					
						
							|  |  |  |   [ "$(id -un)" = "rabbitmq" ] | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2016-09-21 18:32:15 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-28 23:52:54 +08:00
										 |  |  | current_user_is_root() { | 
					
						
							|  |  |  |   [ "$(id -u)" = 0 ] | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | calling_rabbitmq_server() { | 
					
						
							|  |  |  |   [ "$SCRIPT" = "rabbitmq-server" ] | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-02 00:17:39 +08:00
										 |  |  | calling_rabbitmq_plugins() { | 
					
						
							|  |  |  |   [ "$SCRIPT" = "rabbitmq-plugins" ] | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-28 23:52:54 +08:00
										 |  |  | exec_rabbitmq_server() { | 
					
						
							| 
									
										
										
										
											2020-06-08 20:44:43 +08:00
										 |  |  |   exec /usr/lib/rabbitmq/bin/rabbitmq-server "$@" | 
					
						
							| 
									
										
										
										
											2017-07-28 23:52:54 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-13 06:17:22 +08:00
										 |  |  | run_script_as_rabbitmq() { | 
					
						
							|  |  |  |   HOME="$(cd ~rabbitmq && pwd)" "/usr/lib/rabbitmq/bin/$SCRIPT" "$@" | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-28 23:52:54 +08:00
										 |  |  | exec_script_as_rabbitmq() { | 
					
						
							|  |  |  |   HOME="$(cd ~rabbitmq && pwd)" exec "/usr/lib/rabbitmq/bin/$SCRIPT" "$@" | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | exec_script_as_root() { | 
					
						
							| 
									
										
										
										
											2017-08-17 11:13:30 +08:00
										 |  |  |   if [ -x /sbin/runuser ] | 
					
						
							|  |  |  |   then | 
					
						
							| 
									
										
										
										
											2017-09-09 08:41:10 +08:00
										 |  |  |     # TODO: | 
					
						
							|  |  |  |     # At some point all of the RabbitMQ supported distributions will be using | 
					
						
							|  |  |  |     # the util-linux version of /sbin/runuser, as it has been removed from GNU | 
					
						
							|  |  |  |     # coreutils as of 2012. At that point the first clause of the following | 
					
						
							|  |  |  |     # if statement can become the only statement used and the if/then | 
					
						
							|  |  |  |     # removed | 
					
						
							|  |  |  |     if /sbin/runuser --version | grep -qF util-linux | 
					
						
							|  |  |  |     then | 
					
						
							|  |  |  |         exec /sbin/runuser -u rabbitmq -- "/usr/lib/rabbitmq/bin/$SCRIPT" "$@" | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |         exec /sbin/runuser -s /bin/sh -- rabbitmq "/usr/lib/rabbitmq/bin/$SCRIPT" "$@" | 
					
						
							|  |  |  |     fi | 
					
						
							| 
									
										
										
										
											2017-08-17 11:13:30 +08:00
										 |  |  |   elif [ -x /bin/su ] | 
					
						
							|  |  |  |   then | 
					
						
							| 
									
										
										
										
											2017-09-01 06:52:43 +08:00
										 |  |  |     exec /bin/su -s /bin/sh rabbitmq -- "/usr/lib/rabbitmq/bin/$SCRIPT" "$@" | 
					
						
							| 
									
										
										
										
											2017-08-17 11:13:30 +08:00
										 |  |  |   else | 
					
						
							| 
									
										
										
										
											2017-08-17 21:03:04 +08:00
										 |  |  |     echo "Please ensure /bin/su or /sbin/runuser exists and can be executed by $USER." 1>&2 | 
					
						
							| 
									
										
										
										
											2017-08-17 11:13:30 +08:00
										 |  |  |     exit 1 | 
					
						
							|  |  |  |   fi | 
					
						
							| 
									
										
										
										
											2017-07-28 23:52:54 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | run_script_help_and_fail() { | 
					
						
							|  |  |  |   "/usr/lib/rabbitmq/bin/$SCRIPT" help | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   echo " | 
					
						
							|  |  |  | Only root or rabbitmq can run $SCRIPT | 
					
						
							|  |  |  | " | 
					
						
							|  |  |  |   exit 1 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | main "$@" |