Changeset 5464

Show
Ignore:
Timestamp:
04/02/08 14:03:42 (4 months ago)
Author:
hdm
Message:

Patch from I)ruid

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • framework3/trunk/lib/rex/proto/sunrpc/client.rb

    r3562 r5464  
    55module Proto 
    66module SunRPC 
     7 
     8class RPCTimeout < ::Interrupt 
     9   def initialize(msg = 'Operation timed out.') 
     10      @msg = msg 
     11   end 
     12 
     13        def to_s 
     14                @msg 
     15        end 
     16end 
    717 
    818# XXX: CPORT! 
     
    3242        def initialize(rhost, rport, proto, program, version) 
    3343                if proto.downcase !~ /^(tcp|udp)$/ 
    34                         raise ArgumentError, 'Protocol is not "tcp" or "udp"' 
     44                        raise ::Rex::ArgumentError, 'Protocol is not "tcp" or "udp"' 
    3545                end 
    3646 
     
    6373                send_rpc(sock, buf) 
    6474                ret = recv_rpc(sock) 
     75                raise ::Rex::RuntimeError, "No response to SunRPC PortMap request" if ! ret 
    6576                close_rpc(sock) 
    6677 
     
    6980                if arr[1] != MSG_ACCEPTED || arr[4] != SUCCESS || arr[5] == 0 
    7081# Check PRO[CG]_*/GARBAGE_ARGS 
    71                         raise RuntimeError, 'create failed' 
     82                        err = "SunRPC PortMap request to #{@rhost}:#{rport} failed: " 
     83                        err << 'Message not accepted'  if arr[1] != MSG_ACCEPTED 
     84                        err << 'RPC did not execute'   if arr[4] != SUCCESS 
     85                        err << 'Program not available' if arr[5] == 0 
     86                        raise ::Rex::RuntimeError, err 
    7287                end 
    7388 
    7489                @pport = arr[5] 
    7590        end 
    76          
     91 
    7792        def call(procedure, buffer) 
    7893                buf = 
     
    87102                send_rpc(@call_sock, buf) 
    88103                ret = recv_rpc(@call_sock) 
    89                  
    90                 arr = Rex::Encoder::XDR.decode!(ret, Integer, Integer, Integer, String, Integer) 
    91                 if arr[1] != MSG_ACCEPTED || arr[4] != SUCCESS 
    92                         raise 'call failed' 
     104 
     105                if ret 
     106                        arr = Rex::Encoder::XDR.decode!(ret, Integer, Integer, Integer, String, Integer) 
     107                        if arr[1] != MSG_ACCEPTED || arr[4] != SUCCESS 
     108                                err = "SunRPC call for program #{program}, procedure #{procedure}, failed: " 
     109                                case arr[4] 
     110                                        when PROG_UMAVAIL  then err << "Program Unavailable" 
     111                                        when PROG_MISMATCH then err << "Program Version Mismatch" 
     112                                        when PROC_UNAVAIL  then err << "Procedure Unavailable" 
     113                                        when GARBAGE_ARGS  then err << "Garbage Arguments" 
     114                                        else err << "Unknown Error" 
     115                                end 
     116                                raise ::Rex::RuntimeError, err 
     117                        end 
     118                else 
     119                        raise RPCTimeout, "No response to SunRPC call for procedure #{procedure}" 
    93120                end 
    94121                 
     
    101128        end 
    102129         
    103          
    104130        def authnull_create 
    105131                @auth_type = AUTH_NULL 
     
    108134         
    109135        def authunix_create(host, uid, gid, groupz) 
    110                 raise ArgumentError, 'Hostname length is too long' if host.length > 255 
     136                raise ::Rex::ArgumentError, 'Hostname length is too long' if host.length > 255 
    111137# 10? 
    112                 raise ArgumentError, 'Too many groups' if groupz.length > 10 
     138                raise ::Rex::ArgumentError, 'Too many groups' if groupz.length > 10 
    113139                 
    114140                @auth_type = AUTH_UNIX 
     
    116142                        Rex::Encoder::XDR.encode(0, host, uid, gid, groupz) # XXX: TIME! GROUPZ?! 
    117143        end 
    118          
    119144         
    120145# XXX: Dirty, integrate some sort of request system into create/call? 
     
    126151                send_rpc(sock, buf) 
    127152                ret = recv_rpc(sock) 
     153                raise ::Rex::RuntimeError, "No response to SunRPC request" if ! ret 
    128154                close_rpc(sock) 
    129155                 
    130156                arr = Rex::Encoder::XDR.decode!(ret, Integer, Integer, Integer, String, Integer) 
    131157                if arr[1] != MSG_ACCEPTED || arr[4] != SUCCESS || arr[5] == 0 
    132                         raise 'portmap_req failed' 
     158                        err = "SunRPC call for program #{program}, procedure #{procedure}, failed: " 
     159                        case arr[4] 
     160                                when PROG_UMAVAIL  then err << "Program Unavailable" 
     161                                when PROG_MISMATCH then err << "Program Version Mismatch" 
     162                                when PROC_UNAVAIL  then err << "Procedure Unavailable" 
     163                                when GARBAGE_ARGS  then err << "Garbage Arguments" 
     164                                else err << "Unknown Error" 
     165                        end 
     166                        raise ::Rex::RuntimeError, err 
    133167                end 
    134168                 
     
    188222         
    189223        def recv_rpc(sock) 
    190                 buf = sock.get(5) 
     224                buf = sock.get(60) # 5 secs was WAY too slow for some RPC calls 
    191225                buf.slice!(0..3) 
    192226                if sock.type?.eql?('tcp') 
    193227                        buf.slice!(0..3) 
    194228                end 
    195                 return buf 
     229                return buf if buf.length > 1 
     230                return nil 
    196231        end 
    197232