Advertisement
Guest User

/etc/init.d/saslauthd

a guest
Oct 21st, 2016
282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.50 KB | None | 0 0
  1. #! /bin/sh
  2. ### BEGIN INIT INFO
  3. # Provides: saslauthd
  4. # Required-Start: $local_fs $remote_fs
  5. # Required-Stop: $local_fs $remote_fs
  6. # Default-Start: 2 3 4 5
  7. # Default-Stop: 0 1 6
  8. # Short-Description: saslauthd startup script
  9. # Description: This script starts the saslauthd daemon. It is
  10. # configured using the file /etc/default/saslauthd.
  11. ### END INIT INFO
  12.  
  13. # Author: Fabian Fagerholm <fabbe@debian.org>
  14.  
  15. # Do NOT "set -e"
  16.  
  17. # PATH should only include /usr/* if it runs after the mountnfs.sh script
  18. PATH=/sbin:/usr/sbin:/bin:/usr/bin
  19.  
  20. # Global variables
  21. DAEMON=/usr/sbin/saslauthd
  22. DEFAULT_FILES=`find /etc/default -regex '/etc/default/saslauthd[_a-zA-Z0-9\-]*$' -print | sort`
  23.  
  24. # Exit if the package is not installed
  25. [ -x "$DAEMON" ] || exit 0
  26.  
  27. # Load the VERBOSE setting and other rcS variables
  28. . /lib/init/vars.sh
  29.  
  30. # Define LSB log_* functions.
  31. # Depend on lsb-base (>= 3.0-6) to ensure that this file is present.
  32. . /lib/lsb/init-functions
  33.  
  34. # Function that starts all saslauthd instances
  35. # Parameters: none
  36. # Return value: none
  37. do_startall()
  38. {
  39. for instance in $DEFAULT_FILES
  40. do
  41. start_instance $instance
  42. done
  43. }
  44.  
  45. # Function that stops all saslauthd instances
  46. # Parameters: none
  47. # Return value: none
  48. do_stopall()
  49. {
  50. for instance in $DEFAULT_FILES
  51. do
  52. stop_instance $instance
  53. done
  54. }
  55.  
  56. # Function that sends a SIGHUP to all saslauthd instances
  57. # Parameters: none
  58. # Return value: none
  59. do_reloadall()
  60. {
  61. for instance in $DEFAULT_FILES
  62. do
  63. reload_instance $instance
  64. done
  65. }
  66.  
  67. # Function that sends a SIG0 to all saslauthd instances
  68. # Parameters: none
  69. # Return value: none
  70. do_checkall()
  71. {
  72. for instance in $DEFAULT_FILES
  73. do
  74. check_instance $instance
  75. done
  76. }
  77.  
  78. # Function that starts a single saslauthd instance
  79. # Parameters:
  80. # $1 = path of default file for this instance
  81. # Return value:
  82. # 0 on success (does not mean the instance started)
  83. # 1 on failure
  84. start_instance()
  85. {
  86. # Load defaults file for this instance.
  87. . $1
  88.  
  89. # If the daemon is not enabled, give the user a warning and stop.
  90. if [ "$START" != "yes" ]; then
  91. log_warning_msg "To enable $NAME, edit $1 and set START=yes"
  92. return 0
  93. fi
  94.  
  95. # If the short name of this instance is undefined, warn the user
  96. # but choose a default name.
  97. if [ -z "$NAME" ]; then
  98. log_warning_msg "Short name (NAME) undefined in $1, using default"
  99. NAME=default
  100. fi
  101.  
  102. log_daemon_msg "Starting $DESC" "$NAME"
  103.  
  104. # Check to see if the debug option is specified; fail if it is
  105. if [ `expr match "$OPTIONS" '-d'` -ne 0 ]; then
  106. log_failure_msg "The debug option (-d) has been set in /etc/default/saslauthd or elsewhere, not starting"
  107. return 1
  108. fi
  109. # Set OPTIONS to a default value, as noted in the defaults file
  110. if [ -z "$OPTIONS" ]; then
  111. log_warning_msg "Options (OPTIONS) undefined in $1, using default (-c -m /var/run/saslauthd)"
  112. OPTIONS="-c -m /var/run/saslauthd"
  113. fi
  114.  
  115. # Determine run directory and pid file location by looking
  116. # for an -m option.
  117. RUN_DIR=`echo "$OPTIONS" | xargs -n 1 echo | sed -n '/^-m$/{n;p}'`
  118. if [ -z "$RUN_DIR" ]; then
  119. # No run directory defined in defaults file, fail.
  120. log_failure_msg "No run directory defined for $NAME (did you forget to set OPTIONS=\"-c -m /var/run/saslauthd\" in the defaults file?), not starting"
  121. return 1
  122. fi
  123. PIDFILE=$RUN_DIR/saslauthd.pid
  124.  
  125. # If no mechanisms are defined, fail.
  126. if [ -z "$MECHANISMS" ]; then
  127. log_failure_msg "No mechanisms defined in $1, not starting $NAME"
  128. return 1
  129. fi
  130.  
  131. # If there are mechanism options defined, prepare them for use with
  132. # the -O flag.
  133. if [ -n "$MECH_OPTIONS" ]; then
  134. MECH_OPTIONS="-O $MECH_OPTIONS"
  135. fi
  136.  
  137. # If there is a threads option defined, prepare it for use with
  138. # the -n flag.
  139. if [ -n "$THREADS" ]; then
  140. THREAD_OPTIONS="-n $THREADS"
  141. fi
  142.  
  143. # Construct argument string.
  144. DAEMON_ARGS="-a $MECHANISMS $MECH_OPTIONS $OPTIONS $THREAD_OPTIONS"
  145.  
  146. # If there is a statoverride for the run directory, then pull
  147. # permission and ownership information from it and create the directory.
  148. # Otherwise, we create the directory with default permissions and
  149. # ownership (root:sasl, 710).
  150. if dpkg-statoverride --list $RUN_DIR > /dev/null; then
  151. createdir `dpkg-statoverride --list $RUN_DIR`
  152. else
  153. createdir root sasl 710 $RUN_DIR
  154. fi
  155.  
  156. # Start the daemon, phase 1: see if it is already running.
  157. start-stop-daemon --start --quiet --pidfile $PIDFILE --name $NAME \
  158. --exec $DAEMON --test > /dev/null
  159. if [ "$?" != "0" ]; then
  160. log_progress_msg "(already running)"
  161. log_end_msg 0
  162. return 0
  163. fi
  164.  
  165. # Start the daemon, phase 2: it was not running, so actually start it now.
  166. start-stop-daemon --start --quiet --pidfile $PIDFILE --name $NAME \
  167. --exec $DAEMON -- $DAEMON_ARGS
  168. if [ "$?" -ne "0" ]; then
  169. log_end_msg 1
  170. return 1
  171. fi
  172.  
  173. # Started successfully.
  174. log_end_msg 0
  175. return 0
  176. }
  177.  
  178. # Function that stops a single saslauthd instance
  179. # Parameters:
  180. # $1 = path of default file for this instance
  181. # Return value:
  182. # 0 on success (daemon was stopped)
  183. # 1 if the daemon was already stopped
  184. # 2 if the daemon could not be stopped
  185. stop_instance()
  186. {
  187. # Load defaults file for this instance.
  188. . $1
  189.  
  190. # If the short name of this instance is undefined, warn the user
  191. # but choose a default name.
  192. if [ -z "$NAME" ]; then
  193. log_warning_msg "Short name (NAME) undefined in $1, using default"
  194. NAME=default
  195. fi
  196.  
  197. # Set OPTIONS to a default value, as noted in the defaults file
  198. if [ -z "$OPTIONS" ]; then
  199. log_warning_msg "Options (OPTIONS) undefined in $1, using default (-c -m /var/run/saslauthd)"
  200. OPTIONS="-c -m /var/run/saslauthd"
  201. fi
  202.  
  203. # Determine run directory and pid file location by looking
  204. # for an -m option.
  205. RUN_DIR=`echo "$OPTIONS" | xargs -n 1 echo | sed -n '/^-m$/{n;p}'`
  206. if [ -z "$RUN_DIR" ]; then
  207. # No run directory defined in defaults file, fail.
  208. log_failure_msg "No run directory defined for $NAME (did you forget to set OPTIONS=\"-c -m /var/run/saslauthd\" in the defaults file?), not starting"
  209. return 2
  210. fi
  211. PIDFILE=$RUN_DIR/saslauthd.pid
  212.  
  213. log_daemon_msg "Stopping $DESC" "$NAME"
  214.  
  215. start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 \
  216. --pidfile $PIDFILE --exec $DAEMON
  217.  
  218. if [ "$?" -eq "2" ]; then
  219. # Failed to stop.
  220. log_end_msg 1
  221. return 2
  222. fi
  223.  
  224. if [ "$?" -eq "1" ]; then
  225. # Already stopped.
  226. log_progress_msg "(not running)"
  227. fi
  228.  
  229. # Many daemons don't delete their pidfiles when they exit.
  230. rm -f $PIDFILE
  231.  
  232. # Stopped successfully.
  233. log_end_msg 0
  234. return $RETVAL
  235. }
  236.  
  237. # Function that sends a SIGHUP to a single saslauthd instance
  238. # Parameters:
  239. # $1 = path of default file for this instance
  240. # Return value:
  241. # 0 on success (does not mean the daemon was reloaded)
  242. # other values on failure
  243. reload_instance()
  244. {
  245. # Load defaults file for this instance.
  246. . $1
  247.  
  248. # If the short name of this instance is undefined, warn the user
  249. # but choose a default name.
  250. if [ -z "$NAME" ]; then
  251. log_warning_msg "Short name (NAME) undefined in $1, using default"
  252. NAME=default
  253. fi
  254.  
  255. # Set OPTIONS to a default value, as noted in the defaults file
  256. if [ -z "$OPTIONS" ]; then
  257. log_warning_msg "Options (OPTIONS) undefined in $1, using default (-c -m /var/run/saslauthd)"
  258. OPTIONS="-c -m /var/run/saslauthd"
  259. fi
  260.  
  261. # Determine run directory and pid file location by looking
  262. # for an -m option.
  263. RUN_DIR=`echo "$OPTIONS" | xargs -n 1 echo | sed -n '/^-m$/{n;p}'`
  264. if [ -z "$RUN_DIR" ]; then
  265. # No run directory defined in defaults file, fail.
  266. log_failure_msg "No run directory defined for $NAME (did you forget to set OPTIONS=\"-c -m /var/run/saslauthd\" in the defaults file?), not starting"
  267. return 2
  268. fi
  269. PIDFILE=$RUN_DIR/saslauthd.pid
  270.  
  271. log_daemon_msg "Reloading $DESC" "$NAME"
  272.  
  273. # Reload the daemon. First, see if it is already running.
  274. start-stop-daemon --start --quiet --pidfile $PIDFILE \
  275. --exec $DAEMON --test > /dev/null
  276.  
  277. if [ "$?" -eq "0" ]; then
  278. # Not running, signal this and stop.
  279. log_progress_msg "(not running)"
  280. log_end_msg 0
  281. return 0
  282. fi
  283.  
  284. start-stop-daemon --stop --signal 1 \
  285. --pidfile $PIDFILE --exec $DAEMON
  286. log_end_msg $?
  287. }
  288.  
  289. # Function that sends a SIG0 to a single saslauthd instance
  290. # Parameters:
  291. # $1 = path of default file for this instance
  292. # Return value:
  293. # 0 on success (does not mean the daemon was reloaded)
  294. # other values on failure
  295. check_instance()
  296. {
  297. # Load defaults file for this instance.
  298. . $1
  299.  
  300. # If the short name of this instance is undefined, warn the user
  301. # but choose a default name.
  302. if [ -z "$NAME" ]; then
  303. log_warning_msg "Short name (NAME) undefined in $1, using default"
  304. NAME=default
  305. fi
  306.  
  307. # Determine run directory and pid file location by looking
  308. # for an -m option.
  309. RUN_DIR=`echo "$OPTIONS" | xargs -n 1 echo | sed -n '/^-m$/{n;p}'`
  310. if [ -z "$RUN_DIR" ]; then
  311. # No run directory defined in defaults file, fail.
  312. log_failure_msg "No run directory defined for $NAME, cannot check"
  313. return 2
  314. fi
  315. PIDFILE=$RUN_DIR/saslauthd.pid
  316.  
  317. log_daemon_msg "Checking $DESC" "$NAME"
  318.  
  319. # Reload the daemon. First, see if it is already running.
  320. start-stop-daemon --start --quiet --pidfile $PIDFILE \
  321. --exec $DAEMON --test > /dev/null
  322.  
  323. if [ "$?" -eq "0" ]; then
  324. # Not running, signal this and stop.
  325. log_progress_msg "(not running)"
  326. log_end_msg 3
  327. return 3
  328. fi
  329.  
  330. log_progress_msg "(running)"
  331. log_end_msg $?
  332. return 0
  333. }
  334.  
  335. # Function that creates a directory with the specified
  336. # ownership and permissions
  337. # Parameters:
  338. # $1 = user
  339. # $2 = group
  340. # $3 = permissions (octal)
  341. # $4 = path to directory
  342. # Return value: none
  343. createdir()
  344. {
  345. # In the future, use -P/-Z to have SE Linux enhancement
  346. install -d --group="$2" --mode="$3" --owner="$1" "$4"
  347. [ -x /sbin/restorecon ] && /sbin/restorecon "$4"
  348. }
  349.  
  350. # Action switch
  351. case "$1" in
  352. start)
  353. do_startall
  354. ;;
  355. stop)
  356. do_stopall
  357. ;;
  358. reload|force-reload)
  359. do_reloadall
  360. ;;
  361. restart)
  362. do_stopall
  363. do_startall
  364. ;;
  365. status)
  366. do_checkall
  367. exit $?
  368. ;;
  369. start-instance)
  370. if [ -f /etc/default/$2 ]; then
  371. start_instance /etc/default/$2
  372. else
  373. log_failure_msg "Instance $2 does not exist."
  374. fi
  375. ;;
  376. stop-instance)
  377. if [ -f /etc/default/$2 ]; then
  378. stop_instance /etc/default/$2
  379. else
  380. log_failure_msg "Instance $2 does not exist."
  381. fi
  382. ;;
  383. reload-instance|force-reload-instance)
  384. if [ -f /etc/default/$2 ]; then
  385. reload_instance /etc/default/$2
  386. else
  387. log_failure_msg "Instance $2 does not exist."
  388. fi
  389. ;;
  390. restart-instance)
  391. if [ -f /etc/default/$2 ]; then
  392. stop_instance /etc/default/$2
  393. start_instance /etc/default/$2
  394. else
  395. log_failure_msg "Instance $2 does not exist."
  396. fi
  397. ;;
  398. *)
  399. SCRIPTNAME=$0
  400. echo "Usage: $SCRIPTNAME {start|stop|restart|reload|force-reload}" >&2
  401. echo " or {start-instance|stop-instance|restart-instance|" >&2
  402. echo " reload-instance|force-reload-instance} " \
  403. "<instance name>" >&2
  404. exit 3
  405. ;;
  406. esac
  407.  
  408. :
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement