This file is indexed.

/usr/lib/stonith/plugins/external/riloe is in cluster-glue 1.0.8-2ubuntu6.

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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
#!/usr/bin/env python
#
# Stonith module for RILOE Stonith device
#
# Copyright (c) 2004 Alain St-Denis <alain.st-denis@ec.gc.ca>
#
# Modified by Alan Robertson <alanr@unix.sh> for STONITH external compatibility.
#
# Extended and merged by Tijl Van den broeck <subspawn@gmail.com>
#  with ilo-v2 script from Guy Coates
#
# Cleanup by Andrew Beekhof <abeekhof@suse.de>
#
# Rewritten by Dejan Muhamedagic <dejan@suse.de>
# Now, the plugin actually reads replies from iLO.
#
# Extended by Jochen Roeder <jochen.roeder@novell.com> 
# to enable access via proxies
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
# 
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
# 
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
import sys
import os
import socket
import subprocess
import xml.dom.minidom
import httplib
import time
import re

def log_msg(level,msg):
    subprocess.call("ha_log.sh %s '%s'" % (level,msg), shell=True)
def my_err(msg):
    log_msg("err", msg)
def my_warn(msg):
    log_msg("warn", msg)
def my_debug(msg):
    log_msg("debug", msg)
def fatal(msg):
    my_err(msg)
    sys.exit(1)

argv = sys.argv

try:
    cmd = argv[1]
except IndexError:
    my_err("Not enough arguments")
    sys.exit(1)

legacy_RI_HOST     = os.environ.get('RI_HOST',     '')
legacy_RI_HOSTRI   = os.environ.get('RI_HOSTRI',   '')
legacy_RI_LOGIN    = os.environ.get('RI_LOGIN',    'Administrator')
legacy_RI_PASSWORD = os.environ.get('RI_PASSWORD', '')

reset_ok     = os.environ.get('ilo_can_reset',        '0')
ilo_protocol = os.environ.get('ilo_protocol',         '1.2')
power_method = os.environ.get('ilo_powerdown_method', 'power')

realhost = os.environ.get('hostlist',     legacy_RI_HOST)
rihost   = os.environ.get('ilo_hostname', legacy_RI_HOSTRI)
ilouser  = os.environ.get('ilo_user',     legacy_RI_LOGIN)
ilopass  = os.environ.get('ilo_password', legacy_RI_PASSWORD)
iloproxyhost  = os.environ.get('ilo_proxyhost', '')
try:
    iloproxyport  = int(os.environ.get('ilo_proxyport', 3128))
except ValueError:
    my_err("ilo_proxyport is not a number")
    sys.exit(1)

xmlinfo = '''<parameters>
 <parameter name="hostlist" unique="1" required="1">
  <content type="string"/>
  <shortdesc lang="en">ilo target hostname</shortdesc>
  <longdesc lang="en">
   Contains the hostname that the ilo controls
  </longdesc>
 </parameter>
<parameter name="ilo_hostname" unique="1" required="1">
  <content type="string"/>
  <shortdesc lang="en">ilo device hostname</shortdesc>
  <longdesc lang="en">
   The hostname of the ilo device
  </longdesc>
 </parameter>
<parameter name="ilo_user" unique="0" required="1">
  <content type="string" default="Administrator"/>
  <shortdesc lang="en">ilo user</shortdesc>
  <longdesc lang="en">
   The user for connecting to the ilo device
  </longdesc>
 </parameter>
<parameter name="ilo_password" unique="0" required="1">
  <content type="string" default=""/>
  <shortdesc lang="en">password</shortdesc>
  <longdesc lang="en">
   The password for the ilo device user
  </longdesc>
 </parameter>
<parameter name="ilo_can_reset" unique="0" required="0">
  <content type="string" default="0"/>
  <shortdesc lang="en">Device can reset</shortdesc>
  <longdesc lang="en">
   Does the ILO device support RESET commands (hint: older ones cannot)
  </longdesc>
 </parameter>
<parameter name="ilo_protocol" unique="0" required="0">
  <content type="string" default="1.2"/>
  <shortdesc lang="en">ILO Protocol</shortdesc>
  <longdesc lang="en">
   Protocol version supported by the ILO device.
   Known supported versions: 1.2, 2.0
  </longdesc>
 </parameter>
<parameter name="ilo_powerdown_method" unique="0" required="0">
  <content type="string" default="power"/>
  <shortdesc lang="en">Power down method</shortdesc>
  <longdesc lang="en">
   The method to powerdown the host in question.
   * button - Emulate holding down the power button
   * power  - Emulate turning off the machines power 

   NB: A button request takes around 20 seconds. The power method
   about half a minute.
  </longdesc>
 </parameter>
<parameter name="ilo_proxyhost" unique="0" required="0">
  <content type="string" default=""/>
  <shortdesc lang="en">Proxy hostname</shortdesc>
  <longdesc lang="en">
  proxy hostname if required to access ILO board
  </longdesc>
 </parameter>
<parameter name="ilo_proxyport" unique="0" required="0">
  <content type="string" default="3128"/>
  <shortdesc lang="en">Proxy port</shortdesc>
  <longdesc lang="en">
  proxy port if required to access ILO board
  parameter will be ignored if proxy hostname is not set
  </longdesc>
 </parameter>

</parameters>'''

info = {
        'getinfo-devid':    'iLO2',
        'getinfo-devname':  'ilo2 ' + rihost,
        'getinfo-devdescr': 'HP/COMPAQ iLO2 STONITH device',
        'getinfo-devurl':   'http://www.hp.com/',
        'gethosts':    realhost,
        'getinfo-xml':      xmlinfo
}

if cmd in info:
    print info[cmd]
    sys.exit(0)

if cmd == 'getconfignames':
    for arg in [ "hostlist", "ilo_hostname", "ilo_user", "ilo_password", "ilo_can_reset", "ilo_protocol", "ilo_powerdown_method", "ilo_proxyhost", "ilo_proxyport"]:
        print arg
    sys.exit(0)

if not rihost:
    fatal("ILO device hostname not specified")

if not realhost:
    fatal("Host controlled by this ILO device not specified")

if not power_method in ("power","button"):
    my_err('unknown power method %s, setting to "power"')
    power_method = "power"

# XML elements
E_RIBCL = "RIBCL"
E_LOGIN = "LOGIN"
E_SERVER_INFO = "SERVER_INFO"

# power mgmt methods
E_RESET = "RESET_SERVER" # error if powered off
E_COLD_BOOT = "COLD_BOOT_SERVER" # error if powered off
E_WARM_BOOT = "WARM_BOOT_SERVER" # error if powered off
E_PRESS_BUTTON = "PRESS_PWR_BTN"
E_HOLD_BUTTON = "HOLD_PWR_BTN"

# get/set status elements
E_SET_POWER = "SET_HOST_POWER"
E_GET_PSTATUS = "GET_HOST_POWER_STATUS"

# whatever this means, but we have to use it to get good XML
E_LOCFG = "LOCFG"
LOCFG_VER = '2.21'

# attributes
A_VERSION = "VERSION" # ilo_protocol
A_USER = "USER_LOGIN"
A_PWD = "PASSWORD"
A_MODE = "MODE" # info mode (read or write)
A_POWER_SW = "HOST_POWER"  # "Y" or "N"
A_POWER_STATE = "HOST_POWER"  # "ON" or "OFF"

def new_power_req(tag, name = None, value = None):
    '''
    Create a new RIBCL request (as XML).
    '''
    my_debug("creating power request: %s,%s,%s"%(tag,name,value))
    doc = xml.dom.minidom.Document()
    locfg = doc.createElement(E_LOCFG)
    locfg.setAttribute(A_VERSION,LOCFG_VER)
    ribcl = doc.createElement(E_RIBCL)
    ribcl.setAttribute(A_VERSION,ilo_protocol)
    login = doc.createElement(E_LOGIN)
    login.setAttribute(A_USER,ilouser)
    login.setAttribute(A_PWD,ilopass)
    serv_info = doc.createElement(E_SERVER_INFO)
    # read or write, it doesn't really matter, i.e. even if we
    # say "write" that doesn't mean we can't read
    serv_info.setAttribute(A_MODE,"write")
    doc.appendChild(locfg)
    locfg.appendChild(ribcl)
    ribcl.appendChild(login)
    login.appendChild(serv_info)
    el_node = doc.createElement(tag)
    if name:
        el_node.setAttribute(name,value)
    serv_info.appendChild(el_node)
    s = doc.toprettyxml()
    doc.unlink()
    # work around an iLO bug: last line containing "</LOCFG>"
    # produces a syntax error
    lines = s.split('\n')
    return '\n'.join(lines[:-2])

E_RESPONSE = "RESPONSE"
E_HOST_POWER = "GET_HOST_POWER"
A_STATUS = "STATUS"
# documentation mentions both; better safe than sorry
A_MSG = "MSG"
A_MSG2 = "MESSAGE"

def is_element(xmlnode):
    return xmlnode.nodeType == xmlnode.ELEMENT_NODE

def read_resp(node):
    '''
    Check if the RESPONSE XML is OK.
    '''
    msg = ""
    str_status = ""
    for attr in node.attributes.keys():
        if attr == A_STATUS:
            str_status = node.getAttribute(attr)
        elif attr == A_MSG:
            msg = node.getAttribute(attr)
        elif attr == A_MSG2:
            msg = node.getAttribute(attr)
        else:
            my_warn("unexpected attribute %s in %s" % (attr,E_RESPONSE))
    if not str_status:
        my_err("no status in response")
        return -1
    try:
        status = int(str_status,16)
    except ValueError:
        my_err("unexpected status %s in response" % str_status)
        return -1
    if status != 0:
        my_err("%s (rc: %s)"%(msg,str_status))
        return -1
    return 0

def read_power(node):
    '''
    Read the power from the XML node. Set the global power
    variable correspondingly.
    '''
    global power
    for attr in node.attributes.keys():
        if attr == A_POWER_STATE:
            power_state = node.getAttribute(attr).upper()
        else:
            my_warn("unexpected attribute %s in %s" % (attr,node.tagName))
    if not power_state:
        my_err("no %s attribute in %s" % (A_POWER_STATE,node.tagName))
        return -1
    if power_state not in ("ON","OFF"):
        my_err("unexpected value for %s: %s" % (A_POWER_STATE,power_state))
        return -1
    power = (power_state == "ON")
    my_debug("Host has power: %s"%power)
    return 0

el_parsers = {
    E_RESPONSE:read_resp,
    E_HOST_POWER:read_power
}
def proc_resp(doc):
    '''
    Process one iLO reply. Real work is done in el_parsers.
    '''
    ribcl = doc.childNodes[0]
    if not is_element(ribcl) or ribcl.tagName != E_RIBCL:
        my_err("unexpected top element in response")
        return -1
    for child in ribcl.childNodes:
        if not is_element(child):
            continue
        if child.tagName in el_parsers:
            rc = el_parsers[child.tagName](child)
            if rc != 0:
                return -1
        else:
            my_warn("unexpected element in response: %s" % child.toxml())
    return 0

def open_ilo(host):
    # open https connection
    try:
        if iloproxyhost != "" and iloproxyport != 0: 
            proxy=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
            proxy.connect((iloproxyhost, iloproxyport))
            proxy_connect='CONNECT %s:%s HTTP/1.1\r\n'%(host,443)
            user_agent='User-Agent: python\r\n'
            proxy_pieces=proxy_connect+user_agent+'\r\n'
            proxy.sendall(proxy_pieces)
            response=proxy.recv(8192) 
            status=response.split()[1]
            if status!=str(200):  
                fatal("Error status=: %s" %(response))
            import ssl
            sock = ssl.wrap_socket(proxy) 
            h=httplib.HTTPConnection('localhost')
            h.sock=sock
            return h
        else:
            return httplib.HTTPSConnection(host)
    except socket.gaierror, msg:
        fatal("%s: %s" %(msg,host))
    except socket.sslerror, msg:
        fatal("%s for %s" %(msg,host))
    except socket.error, msg:
        fatal("%s while talking to %s" %(msg,host))
    except ImportError, msg:
        fatal("ssl support missing (%s)" %msg)

def send_request(req,proc_f):
    '''
    1. After every request, the iLO closes the connection.
    2. For every request, there are multiple replies. Each reply
    is an XML document. Most of replies are just a kind of
    (verbose) XML "OK".
    '''
    t_begin = time.time()
    c = open_ilo(rihost)
    try:
        c.send(req+'\r\n')
    except socket.error, msg:
        fatal("%s, while talking to %s" %(msg,rihost))
    t_end = time.time()
    my_debug("request sent in %0.2f s" % ((t_end-t_begin)))

    t_begin = time.time()
    result = []
    while True:
        try:
            reply = c.sock.recv(1024)
            if not reply:
                break
            result.append(reply)
        except socket.error, msg:
            if msg[0] == 6: # connection closed
                break
            my_err("%s, while talking to %s" %(msg,rihost))
            return -1
    c.close()
    t_end = time.time()

    if not result:
        fatal("no response from %s within %0.2f s"%(rihost,(t_end-t_begin)))
    for reply in result:
        # work around the iLO bug, i.e. element RIBCL closed twice
        if re.search("</RIBCL", reply) and re.search("<RIBCL.*/>", reply):
            reply = re.sub("<(RIBCL.*)/>", r"<\1>", reply)
        try:
            doc = xml.dom.minidom.parseString(reply)
        except xml.parsers.expat.ExpatError,msg:
            fatal("malformed response: %s\n%s"%(msg,reply))
        rc = proc_f(doc)
        doc.unlink()
        if rc != 0:
            break
    my_debug("iLO processed request (rc=%d) in %0.2f s" % (rc,(t_end-t_begin)))
    return rc

def manage_power(cmd):
    '''
    Before trying to send a request we have to check the power
    state.
    '''
    rc = 0
    req = ''
    # it won't do to turn it on if it's already on!
    if cmd == "on" and not power:
        req = new_power_req(E_SET_POWER,A_POWER_SW,"Y")
    # also to turn it off if it's already off
    elif cmd == "off" and power:
        req = new_power_req(E_SET_POWER,A_POWER_SW,"N")
    elif cmd == "cold_boot" and power:
        req = new_power_req(E_COLD_BOOT)
    elif cmd == "warm_boot" and power:
        req = new_power_req(E_WARM_BOOT)
    elif cmd == "reset":
        if power:
            req = new_power_req(E_RESET)
        # reset doesn't work if the host's off
        else:
            req = new_power_req(E_SET_POWER,A_POWER_SW,"Y")
    if req:
        rc = send_request(req,proc_resp)
    return rc
def power_on():
    '''
    Update the power variable without checking the power state.
    The iLO is slow at times to report the actual power state, so
    we assume that it changed if the request succeeded.
    '''
    rc = manage_power("on")
    if rc == 0:
        global power
        power = True
    return rc
def power_off():
    rc = manage_power("off")
    if rc == 0:
        global power
        power = False
    return rc
def cold_boot():
    rc = manage_power("cold_boot")
    return rc
def warm_boot():
    rc = manage_power("warm_boot")
    return rc
def reset():
    rc = manage_power("reset")
    if rc == 0:
        global power
        power = True
    return rc
def hold_button():
    '''
    Hold the power button. Got this error message when tried
    without the TOGGLE attribute:
        Command without TOGGLE="Yes" attribute is ignored
        when host power is off. (rc: 0x0054)
    Didn't find any documentation about TOGGLE.
    '''
    if power:
        req = new_power_req(E_HOLD_BUTTON)
    else:
        req = new_power_req(E_HOLD_BUTTON,"TOGGLE","Yes")
    rc = send_request(req,proc_resp)
    return rc
def read_power_state():
    req = new_power_req(E_GET_PSTATUS)
    rc = send_request(req,proc_resp)
    return rc

def reset_power():
    '''
    Three methods to reset:
    - hold power button
    - reset (only if host has power and user said that reset is ok)
    - power off/on
    '''
    do_power_on = False
    if power_method == 'button':
        rc = hold_button()
    elif reset_ok != '0':
        if power:
            return reset()
        else:
            return power_on()
    else:
        do_power_on = True
        rc = power_off()
    if rc == 0:
        rc = read_power_state()
    if do_power_on:
        while rc == 0 and power: # wait for the power state to go off
            time.sleep(5)
            rc = read_power_state()
    if rc == 0 and do_power_on and not power:
        rc = power_on()
    return rc

# track state of host power
power = -1

todo = {
'reset':reset_power,
'on':power_on,
'off':power_off,
'cold':cold_boot,
'warm':warm_boot,
'status':lambda: 0  # just return 0, we already read the state
}

rc = read_power_state()
if rc == 0:
    if cmd in todo:
        rc = todo[cmd]()
    else:
        fatal('Invalid command: %s' % cmd)
if rc != 0:
    fatal("request failed")
sys.exit(rc)

# vi:ts=4:sw=4:et: