This file is indexed.

/usr/sbin/fence_rsb is in fence-agents 3.1.5-2ubuntu2.

This file is owned by root:root, with mode 0o755.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
#!/usr/bin/python

import getopt, sys
import os
import socket
import time
import atexit

from telnetlib import Telnet

TELNET_TIMEOUT=30 #How long to wait for a response from a telnet try
MAX_TRIES = 20

# WARNING!! Do not add code bewteen "#BEGIN_VERSION_GENERATION" and
# "#END_VERSION_GENERATION"  It is generated by the Makefile

#BEGIN_VERSION_GENERATION
RELEASE_VERSION="3.1.5"
BUILD_DATE="(built Mon Feb 6 23:36:22 UTC 2012)"
REDHAT_COPYRIGHT="Copyright (C) Red Hat, Inc. 2004-2010 All rights reserved."
#END_VERSION_GENERATION

def usage():
  print "Usage:"
  print "fence_rsb [options]"
  print "Options:"
  print "   -a <ipaddress>           ip or hostname of rsb"
  print "   -h                       print out help"
  print "   -l [login]               login name"
  print "   -n [telnet port]         telnet port"
  print "   -p [password]            password"
  print "   -S [path]                script to run to retrieve password"
  print "   -o [action]              reboot (default), off, on, or status"
  print "   -v Verbose               Verbose mode"
  print "   -V                       Print Version, then exit"

  sys.exit (0)

def version():
  print "fence_rsb %s  %s\n" % (RELEASE_VERSION, BUILD_DATE)
  print "%s\n" % REDHAT_COPYRIGHT
  sys.exit(0)

def atexit_handler():
	try:
		sys.stdout.close()
		os.close(1)
	except IOError:
		sys.stderr.write("%s failed to close standard output\n"%(sys.argv[0]))
		sys.exit(1)

def main():
  depth = 0
  POWER_OFF = 0
  POWER_ON = 1
  POWER_STATUS = 2
  POWER_REBOOT = 3

  STATUS_ON = 0
  STATUS_OFF = 2

  power_command_issued = 0 

  address = ""
  login = ""
  passwd = ""
  passwd_script = ""
  action = POWER_REBOOT   #default action
  telnet_port = 3172
  verbose = False
  power_state = None

  standard_err = 2

  result = 0

  completed_action = 0

  #set up regex list
  USERNAME = 0
  PASSWORD = 1
  PROMPT = 2
  STATE = 3
  ERROR = 4
  CONT = 5
  CONFIRM = 6
  DONE = 7

  regex_list = list()
  regex_list.append("user name\s*:")
  regex_list.append("pass phrase\s*:")
  regex_list.append("[Ee]nter\s+[Ss]election[^\r\n]*:")
  regex_list.append("[pP]ower Status\s*:")
  regex_list.append("[Ee]rror\s*:")
  regex_list.append("[Pp]ress any key to continue")
  regex_list.append("really want to")
  regex_list.append("CLOSING TELNET CONNECTION")

  atexit.register(atexit_handler)

  if len(sys.argv) > 1:
    try:
      opts, args = getopt.getopt(sys.argv[1:], "a:hl:n:o:p:S:vV", ["help", "output="])
    except getopt.GetoptError:
      #print help info and quit
      usage()
      sys.exit(2)

    for o, a in opts:
      if o == "-v":
        verbose = True
      if o == "-V":
        version()
      if o in ("-h", "--help"):
        usage()
        sys.exit(0)
      if o == "-l":
        login = a
      if o == "-n":
        telnet_port = a
      if o == "-p":
        passwd = a
      if o == "-S":
        passwd_script = a
      if o  == "-o":
        a_lower=a.lower()
        if a_lower == "off":
          action = POWER_OFF
        elif a_lower == "on":
          action = POWER_ON
        elif a_lower == "status":
          action = POWER_STATUS
        elif a_lower == "reboot":
          action = POWER_REBOOT
        else:
          usage()
          sys.exit(1)
      if o == "-a":
        address = a
    if address == "" or login == "" or (passwd == "" and passwd_script == ""):
      usage()
      sys.exit(1)

  else: #Take args from stdin...
    params = {}
    #place params in dict
    for line in sys.stdin:
      val = line.split("=")
      if len(val) == 2:
        params[val[0].strip()] = val[1].strip()

    try:
      address = params["ipaddr"]
    except KeyError, e:
      os.write(standard_err, "FENCE: Missing ipaddr param for fence_rsb...exiting")
      sys.exit(1)
    
    try:
      login = params["login"]
    except KeyError, e:
      os.write(standard_err, "FENCE: Missing login param for fence_rsb...exiting")
      sys.exit(1)
    
    try:
      if 'passwd' in params:
        passwd = params["passwd"]
      if 'passwd_script' in params:
        passwd_script = params['passwd_script']
      if passwd == "" and passwd_script == "":
        raise Exception, "missing password"
    except KeyError, e:
      os.write(standard_err, "FENCE: Missing passwd param for fence_rsb...exiting")
      sys.exit(1)
    
    try:
      telnet_port = params["telnet_port"]
    except KeyError, e:
      pass

    try:
      a = params["option"]
      a_lower=a.lower()
      if a_lower == "off":
        action = POWER_OFF
      elif a_lower == "on":
        action = POWER_ON
      elif a_lower == "reboot":
        action = POWER_REBOOT
    except KeyError, e:
      action = POWER_REBOOT

    ####End of stdin section
  
  
  # retrieve passwd from passwd_script (if specified)
  passwd_scr = ''
  if len(passwd_script):
    try:
      if not os.access(passwd_script, os.X_OK):
        raise Exception, 'script not executable'
      p = os.popen(passwd_script, 'r', 1024)
      passwd_scr = p.readline().strip()
      if p.close() != None:
        raise Exception, 'script failed'
    except:
      sys.stderr.write('password-script "%s" failed\n' % passwd_script)
      passwd_scr = ''
  
  if passwd == "" and passwd_scr == "":
    sys.stderr.write('password not available, exiting...')
    sys.exit(1)
  elif passwd == passwd_scr:
    pass
  elif passwd and passwd_scr:
    # execute self, with password_scr as passwd,
    # if that fails, continue with "passwd" argument as password
    if len(sys.argv) > 1:
      comm = sys.argv[0]
      skip_next = False
      for w in sys.argv[1:]:
        if skip_next:
          skip_next = False
        elif w in ['-p', '-S']:
          skip_next = True
        else:
          comm += ' ' + w
      comm += ' -p ' + passwd_scr
      ret = os.system(comm)
      if ret != -1 and os.WIFEXITED(ret) and os.WEXITSTATUS(ret) == 0:
        # success
        sys.exit(0)
      else:
        sys.stderr.write('Use of password from "passwd_script" failed, trying "passwd" argument\n')
    else: # use stdin
      p = os.popen(sys.argv[0], 'w', 1024)
      for par in params:
        if par not in ['passwd', 'passwd_script']:
          p.write(par + '=' + params[par] + '\n')
      p.write('passwd=' + passwd_scr + '\n')
      p.flush()
      if p.close() == None:
        # success
        sys.exit(0)
      else:
        sys.stderr.write('Use of password from "passwd_script" failed, trying "passwd" argument\n')
  elif passwd_scr:
    passwd = passwd_scr
  # passwd all set
  
  
  
  try:
    telnet_port = int(telnet_port)
  except:
    os.write(standard_err, ("FENCE: Invalid telnet port: %s\n" % telnet_port))
    sys.exit(1)
    
  ##Time to open telnet session and log in. 
  try:
    sock = Telnet(address.strip(), telnet_port)
  except socket.error, (errno, msg):
    my_msg = "FENCE: A problem was encountered opening a telnet session with " + address
    os.write(standard_err, my_msg)
    os.write(standard_err, ("FENCE: Error number: %d -- Message: %s\n" % (errno, msg)))
    os.write(standard_err, "Firewall issue? Correct address?\n")
    sys.exit(1)

  if verbose:
    #sock.set_debuglevel(10000)
    print  "socket open to %s %d\n" % (address, telnet_port)

  tries = MAX_TRIES
  while 1:
    i, mo, txt = sock.expect(regex_list, TELNET_TIMEOUT)
    if i == ERROR:
      os.write(standard_err,("FENCE: An error was encountered when communicating with the rsb device at %s" % address))
      buf = sock.read_eager()
      os.write(standard_err,("FENCE: The error message is - %s" % txt + " " + buf))
      sock.close()
      sys.exit(1)

    try:
      buf = sock.read_eager()
    except EOFError:
      if completed_action == 1:
      	# action was completed succesfully, connection closed is OK
        sys.exit(result)
      else:
        raise
	         
    if i == USERNAME:
      if verbose:
        print "Sending login: %s\n" % login
      sock.write(login + "\r")

    elif i == PASSWORD:
      if verbose:
        print "Sending password: %s\n" % passwd
      sock.write(passwd + "\r")

    elif i == CONT:
      if verbose:
        print "Sending continue char..."
      sock.write("\r")
      time.sleep(2)

    elif i == CONFIRM:
      if verbose:
        print "Confirming..."
      sock.write("yes\r")

    elif i == PROMPT:
      if verbose:
        print "Evaluating prompt...\n"

      if depth == 0:
        sock.write("2\r")
        depth += 1
      elif depth == 1:
        if action == POWER_OFF or action == POWER_REBOOT:
          if power_command_issued == 0:
            if verbose:
              print "Sending power off %s" % address
            sock.write("1\r")
            power_command_issued += 1
            time.sleep(2)
          elif power_command_issued and power_state == 0:
            if verbose:
              print "Power off was successful"
            if action == POWER_OFF:
              completed_action = 1
              depth += 1
              sock.write("0\r")
            else:
              action = POWER_ON
              power_command_issued = 0
              sock.write("\r")
          elif tries > 0:
            if verbose:
              print "Waiting for power off to complete"
            tries -= 1
            sock.write("\r")
            time.sleep(2)
          else:
            os.write(standard_err, "FENCE: Unable to power off server")
            depth += 1
            sock.write("0\r")

        elif action == POWER_ON:
          if power_command_issued == 0:
            if verbose:
              print "Sending power on %s" % address
            sock.write("4\r")
            power_command_issued += 1
            time.sleep(2)
          elif power_command_issued and power_state == 1:
            if verbose:
              print "Power on was successful"
            completed_action = 1
            depth += 1
            sock.write("0\r")
          elif tries > 0:
            if verbose:
              print "Waiting for power on to complete"
            tries -= 1
            sock.write("\r")
            time.sleep(2)
          else:
            os.write(standard_err, "FENCE: Unable to power on server")
            depth += 1
            sock.write("0\r")
      else:
        sock.write("0\r")

    elif i == STATE:
      if buf.find(" On") != (-1):
        power_state = 1
      elif buf.find(" Off") != (-1):
        power_state = 0
      else:
        power_state = None

      if action == POWER_STATUS:
        if verbose:
          print "Determining power state..."
        if power_state == 1:
          print "Server is On"
          result = STATUS_ON
        elif power_state == 0:
          print "Server is Off"
          result = STATUS_OFF
        else:
          os.write(standard_err, ("FENCE: Cannot determine power state: %s" % buf))
          sys.exit(1)
        depth = 2
        completed_action = 1

    elif i == DONE:
      break

    else:
      sock.write("\r")

  sock.close()
  sys.exit(result)

if __name__ == "__main__":
  main()