| 
									
										
										
										
											2016-09-21 18:32:15 +08:00
										 |  |  | #!/bin/sh | 
					
						
							|  |  |  | ##  The contents of this file are subject to the Mozilla Public License | 
					
						
							|  |  |  | ##  Version 1.1 (the "License"); you may not use this file except in | 
					
						
							|  |  |  | ##  compliance with the License. You may obtain a copy of the License | 
					
						
							|  |  |  | ##  at http://www.mozilla.org/MPL/ | 
					
						
							|  |  |  | ## | 
					
						
							|  |  |  | ##  Software distributed under the License is distributed on an "AS IS" | 
					
						
							|  |  |  | ##  basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See | 
					
						
							|  |  |  | ##  the License for the specific language governing rights and | 
					
						
							|  |  |  | ##  limitations under the License. | 
					
						
							|  |  |  | ## | 
					
						
							|  |  |  | ##  The Original Code is RabbitMQ. | 
					
						
							|  |  |  | ## | 
					
						
							|  |  |  | ##  The Initial Developer of the Original Code is GoPivotal, Inc. | 
					
						
							|  |  |  | ##  Copyright (c) 2007-2015 Pivotal Software, Inc.  All rights reserved. | 
					
						
							|  |  |  | ## | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-28 23:52:54 +08:00
										 |  |  | SCRIPT="$(basename "$0")" | 
					
						
							|  |  |  | RABBITMQ_ENV=/usr/lib/rabbitmq/bin/rabbitmq-env | 
					
						
							|  |  |  | RABBITMQ_SCRIPTS_DIR="$(dirname "$RABBITMQ_ENV")" | 
					
						
							| 
									
										
										
										
											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 | 
					
						
							|  |  |  |     exec_script_as_rabbitmq "$@" | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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() { | 
					
						
							|  |  |  |   RABBITMQ_ENV=/usr/lib/rabbitmq/bin/rabbitmq-env | 
					
						
							|  |  |  |   # RABBITMQ_SCRIPTS_DIR is used in rabbitmq-env | 
					
						
							|  |  |  |   # shellcheck disable=SC2034 | 
					
						
							|  |  |  |   RABBITMQ_SCRIPTS_DIR="$(dirname "$RABBITMQ_ENV")" | 
					
						
							|  |  |  |   # shellcheck source=/dev/null | 
					
						
							|  |  |  |   . "$RABBITMQ_ENV" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   exec /usr/lib/rabbitmq/bin/rabbitmq-server "$@" @STDOUT_STDERR_REDIRECTION@ | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 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 "$@" |