]> git.sur5r.net Git - bacula/bacula/blob - bacula/examples/afs-bacula
Tweak ChangeLog and ReleaseNotes for last backport from Enterprise
[bacula/bacula] / bacula / examples / afs-bacula
1 #!/usr/afsws/bin/pagsh
2 #
3 #   Get a Kerbos authentication ticket for AFS for Bacula, then run
4 #      the Bacula client.  See AFS-README for documentation.
5 #
6 # NAME          afs_bacula
7 # AUTHOR        Lucas Mingarro <lucas@easytech.com.ar>
8 # PURPOSE       Run an AFS authenticated program.
9 #               Get a PAG, get the user's token,
10 #               then exec user's command
11 #
12 TEXTDOMAIN=initscripts
13 TEXTDOMAINDIR=/etc/locale
14
15 # Make sure umask is sane
16 umask 022
17
18 # First set up a default search path.
19 export PATH="/sbin:/usr/sbin:/bin:/usr/bin:/usr/X11R6/bin"
20
21 # Get a sane screen width
22 [ -z "$COLUMNS" ] && COLUMNS=80
23
24 if [ -f /etc/sysconfig/i18n -a -z "$NOLOCALE" ] ; then
25    . /etc/sysconfig/i18n
26    if [ "$LANG" = "ja_JP.eucJP" -a "`/sbin/consoletype`" != "pty" ]; then
27      unset LANG
28    else
29      export LANG
30    fi
31 fi
32
33 # Read in our configuration
34 if [ -z "$BOOTUP" ]; then
35    if [ -f /etc/sysconfig/init ]; then
36        . /etc/sysconfig/init
37    else
38      # This all seem confusing? Look in /etc/sysconfig/init,
39      # or in /usr/doc/initscripts-*/sysconfig.txt
40      BOOTUP=color
41      RES_COL=60
42      MOVE_TO_COL="echo -en \\033[${RES_COL}G"
43      SETCOLOR_SUCCESS="echo -en \\033[1;32m"
44      SETCOLOR_FAILURE="echo -en \\033[1;31m"
45      SETCOLOR_WARNING="echo -en \\033[1;33m"
46      SETCOLOR_NORMAL="echo -en \\033[0;39m"
47      LOGLEVEL=1
48    fi
49    if [ -x /sbin/consoletype ]; then
50      if [ "`consoletype`" = "serial" ]; then
51        BOOTUP=serial
52        MOVE_TO_COL=
53        SETCOLOR_SUCCESS=
54        SETCOLOR_FAILURE=
55        SETCOLOR_WARNING=
56        SETCOLOR_NORMAL=
57      fi
58    fi
59 fi
60
61 if [ "$BOOTUP" != "verbose" ]; then
62     INITLOG_ARGS="-q"
63 else
64     INITLOG_ARGS=
65 fi
66
67 # Check if $pid (could be plural) are running
68 checkpid() {
69          while [ "$1" ]; do
70             [ -d /proc/$1 ] && return 0
71             shift
72          done
73          return 1
74 }
75
76
77 # A function to start a program.
78 daemon() {
79          # Test syntax.
80          local gotbase=
81          local base= user= nice= bg= pid
82          nicelevel=0
83          while [ "$1" != "${1##[-+]}" ]; do
84            case $1 in
85              '')    echo $"$0: Usage: daemon [+/-nicelevel] {program}"
86                     return 1;;
87              --check)
88                     base=$2
89                     gotbase="yes"
90                     shift 2
91                     ;;
92              --check=?*)
93                     base=${1#--user=}
94                     shift
95                     ;;
96              --user)
97                     user=$2
98                     shift 2
99                     ;;
100              --user=?*)
101                     user=${1#--user=}
102                     shift
103                     ;;
104              [-+][0-9]*)
105                     nice="nice -n $1"
106                     shift
107                     ;;
108              *)     echo $"$0: Usage: daemon [+/-nicelevel] {program}"
109                     return 1;;
110            esac
111          done
112
113          # Save basename.
114          [ -z $gotbase ] && base=${1##*/}
115
116          # See if it's already running. Look *only* at the pid file.
117          pidlist=`pidfileofproc $base`
118
119          [ -n "$pid" ] && return
120
121          # make sure it doesn't core dump anywhere; while this could mask
122          # problems with the daemon, it also closes some security problems
123          ulimit -S -c 0 >/dev/null 2>&1
124
125          # Echo daemon
126          [ "$BOOTUP" = "verbose" ] && echo -n " $base"
127
128          # And start it up.
129          if [ -z "$user" ]; then
130             $nice initlog $INITLOG_ARGS -c "$*"
131          else
132             $nice initlog $INITLOG_ARGS -c "su - $user -c \"$*\"" && 
133 success $"$base startup" || failure $"$base startup"
134          fi
135          [ $? = 0 ] && success $"$base startup" || failure $"$base startup"
136 }
137
138
139 # A function to find the pid of a program. Looks *only* at the pidfile
140 pidfileofproc() {
141          local base=${1##*/}
142          local pid
143
144          # Test syntax.
145          if [ $# = 0 ] ; then
146                  echo $"Usage: pidfileofproc {program}"
147                  return 1
148          fi
149
150          # First try "/var/run/*.pid" files
151          if [ -f /var/run/${base}.pid ] ; then
152                  read pid < /var/run/${base}.pid
153                  for p in $line ; do
154                         [ -z "${p//[0-9]/}" -a -d /proc/$p ] && 
155 pid="$pid $p"
156                  done
157                  if [ -n "$pid" ] ; then
158                          echo $pid
159                          return 0
160                  fi
161          fi
162 }
163
164 echo_success() {
165    [ "$BOOTUP" = "color" ] && $MOVE_TO_COL
166    echo -n "[  "
167    [ "$BOOTUP" = "color" ] && $SETCOLOR_SUCCESS
168    echo -n $"OK"
169    [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
170    echo -n "  ]"
171    echo -ne "\r"
172    return 0
173 }
174
175 echo_failure() {
176    [ "$BOOTUP" = "color" ] && $MOVE_TO_COL
177    echo -n "["
178    [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
179    echo -n $"FAILED"
180    [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
181    echo -n "]"
182    echo -ne "\r"
183    return 1
184 }
185
186 echo_passed() {
187    [ "$BOOTUP" = "color" ] && $MOVE_TO_COL
188    echo -n "["
189    [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
190    echo -n $"PASSED"
191    [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
192    echo -n "]"
193    echo -ne "\r"
194    return 1
195 }
196
197 # Log that something succeeded
198 success() {
199    if [ -z "$IN_INITLOG" ]; then
200       initlog $INITLOG_ARGS -n $0 -s "$1" -e 1
201    else
202       # silly hack to avoid EPIPE killing rc.sysinit
203       trap "" SIGPIPE
204       echo "$INITLOG_ARGS -n $0 -s \"$1\" -e 1" >&21
205       trap - SIGPIPE
206    fi
207    [ "$BOOTUP" != "verbose" ] && echo_success
208    return 0
209 }
210
211 # Log that something failed
212 failure() {
213    rc=$?
214    if [ -z "$IN_INITLOG" ]; then
215       initlog $INITLOG_ARGS -n $0 -s "$1" -e 2
216    else
217       trap "" SIGPIPE
218       echo "$INITLOG_ARGS -n $0 -s \"$1\" -e 2" >&21
219       trap - SIGPIPE
220    fi
221    [ "$BOOTUP" != "verbose" ] && echo_failure
222    return $rc
223 }
224
225 # Log that something passed, but may have had errors. Useful for fsck
226 passed() {
227    rc=$?
228    if [ -z "$IN_INITLOG" ]; then
229       initlog $INITLOG_ARGS -n $0 -s "$1" -e 1
230    else
231       trap "" SIGPIPE
232       echo "$INITLOG_ARGS -n $0 -s \"$1\" -e 1" >&21
233       trap - SIGPIPE
234    fi
235    [ "$BOOTUP" != "verbose" ] && echo_passed
236    return $rc
237 }
238
239 # Run some action. Log its output.
240 action() {
241    STRING=$1
242    echo -n "$STRING "
243    shift
244    initlog $INITLOG_ARGS -c "$*" && success $"$STRING" || failure $"$STRING"
245    rc=$?
246    echo
247    return $rc
248 }
249
250 # returns OK if $1 contains $2
251 strstr() {
252    [ "$1" = "$2" ] && return 0
253    slice=${1#*$2*}
254    [ "$slice" = "$1" ] && return 1
255    return 0
256 }
257
258 # Confirm whether we really want to run this service
259 confirm() {
260    local YES=$"yY"
261    local NO=$"nN"
262    local CONT=$"cC"
263
264    while : ; do
265        echo -n $"Start service $1 (Y)es/(N)o/(C)ontinue? [Y] "
266        read answer
267        if strstr "$YES" "$answer" || [ "$answer" = "" ] ; then
268           return 0
269        elif strstr "$CONT" "$answer" ; then
270           return 2
271        elif strstr "$NO" "$answer" ; then
272           return 1
273        fi
274    done
275 }
276
277 # Here is the authentication with the kas server
278
279 CMD=`basename ${0}`
280 PRINCIPAL='bacula'
281 passwordfile='/etc/security/afs_bacula.pw'
282 klog $PRINCIPAL -pipe < ${passwordfile}
283 command_line="$*"
284 command=`echo ${command_line} | awk '{print $1}'`
285 # Check if we can run the command.
286 # If we got this far, it is likely that the command name is correct
287 # but there may be a problem in accessing the command file.
288 # If there is an error, log it via syslog (logger) rather than ">&2"
289
290    #if [ ! -x "${command}" ]; then
291      #M="error: unable to execute command ${command}"
292      #logger -i -t "${CMD}" "${M}"
293      #exit 1
294    #fi
295 #fi
296 $command_line