예제 #1
0
def unpack_reply(response,
                 myxid=None,
                 myreply_stat=MSG_ACCEPTED,
                 myverf=NULL_AUTH,
                 myaccept_stat=SUCCESS,
                 myreject_stat=None,
                 myauth_stat=None):
    """Unpacks an RPC reply and returns a variable-length arg list
    of the same form as the argument to pack_reply, but for SUCCESS also
    returns an xdrlib.Unpacker as the final element of the list
    that the caller can use to unpack the results of the call.

    If values are given for any myXXX arguments, checks that those
    values match the unpacked XXX values.  Default myXXX values assume
    success with no authentication.
    
    Raises UnpackException on any errors or mismatches.
    """
    u = Unpacker(response)
    msg = RPCProto.unpack_rpc_msg(u)
    check(myxid, msg.xid, "xid")
    if msg.body.mtype == RPCProto.CALL:
        raise UnpackException("Expected reply, but got call")
    reply = msg.body.rbody
    check(myreply_stat, reply.stat, "reply_stat")
    retval = [msg.xid, reply.stat]
    if reply.stat == RPCProto.MSG_ACCEPTED:
        check(myverf, reply.areply.verf, "verf")
        retval.append(reply.areply.verf)
        accept_stat = reply.areply.reply_data.stat
        check(myaccept_stat, accept_stat, "accept_stat")
        retval.append(accept_stat)
        if accept_stat == RPCProto.SUCCESS:
            retval.append(u)
        elif accept_stat == RPCProto.PROG_MISMATCH:
            retval.append(reply.areply.reply_data.mismatch_info.low)
            retval.append(reply.areply.reply_data.mismatch_info.high)
        elif (accept_stat == RPCProto.PROG_UNAVAIL
              or accept_stat == RPCProto.PROC_UNAVAIL
              or accept_stat == RPCProto.GARBAGE_ARGS
              or accept_stat == RPCProto.SYSTEM_ERR):
            pass
        else:
            raise UnpackException("unknown accept_stat: %u" % accept_stat)
    elif reply.stat == RPCProto.MSG_DENIED:
        reject_stat = reply.rreply.stat
        check(myreject_stat, reject_stat, "reject_stat")
        retval.append(reject_stat)
        if reject_stat == RPCProto.RPC_MISMATCH:
            retval.append(reply.rreply.mismatch_info.low)
            retval.append(reply.rreply.mismatch_info.high)
        elif reject_stat == RPCProto.AUTH_ERROR:
            check(myauth_stat, reply.rreply.astat, "auth_stat")
            retval.append(reply.rreply.astat)
        else:
            raise UnpackException("unknown reject_stat: %u" % reject_stat)
    else:
        raise UnpackException("unknown reply_stat: %u" % reply.stat)
    return retval
예제 #2
0
파일: RPC.py 프로젝트: Amit-DU/dht
def unpack_reply(response, myxid=None, myreply_stat=MSG_ACCEPTED,
                 myverf=NULL_AUTH, myaccept_stat=SUCCESS,
                 myreject_stat=None, myauth_stat=None):
    """Unpacks an RPC reply and returns a variable-length arg list
    of the same form as the argument to pack_reply, but for SUCCESS also
    returns an xdrlib.Unpacker as the final element of the list
    that the caller can use to unpack the results of the call.

    If values are given for any myXXX arguments, checks that those
    values match the unpacked XXX values.  Default myXXX values assume
    success with no authentication.
    
    Raises UnpackException on any errors or mismatches.
    """
    u = Unpacker(response)
    msg = RPCProto.unpack_rpc_msg(u)
    check(myxid, msg.xid, "xid")
    if msg.body.mtype == RPCProto.CALL:
        raise UnpackException("Expected reply, but got call")
    reply = msg.body.rbody
    check(myreply_stat, reply.stat, "reply_stat")
    retval = [msg.xid, reply.stat]
    if reply.stat == RPCProto.MSG_ACCEPTED:
        check(myverf, reply.areply.verf, "verf")
        retval.append(reply.areply.verf)
        accept_stat = reply.areply.reply_data.stat
        check(myaccept_stat, accept_stat, "accept_stat")
        retval.append(accept_stat)
        if accept_stat == RPCProto.SUCCESS:
            retval.append(u)
        elif accept_stat == RPCProto.PROG_MISMATCH:
            retval.append(reply.areply.reply_data.mismatch_info.low)
            retval.append(reply.areply.reply_data.mismatch_info.high)
        elif (accept_stat == RPCProto.PROG_UNAVAIL or
              accept_stat == RPCProto.PROC_UNAVAIL or
              accept_stat == RPCProto.GARBAGE_ARGS or
              accept_stat == RPCProto.SYSTEM_ERR):
            pass
        else:
            raise UnpackException("unknown accept_stat: %u" % accept_stat)
    elif reply.stat == RPCProto.MSG_DENIED:
        reject_stat = reply.rreply.stat
        check(myreject_stat, reject_stat, "reject_stat")
        retval.append(reject_stat)
        if reject_stat == RPCProto.RPC_MISMATCH:
            retval.append(reply.rreply.mismatch_info.low)
            retval.append(reply.rreply.mismatch_info.high)
        elif reject_stat == RPCProto.AUTH_ERROR:
            check(myauth_stat, reply.rreply.astat, "auth_stat")
            retval.append(reply.rreply.astat)
        else:
            raise UnpackException("unknown reject_stat: %u" % reject_stat)
    else:
        raise UnpackException("unknown reply_stat: %u" % reply.stat)
    return retval
예제 #3
0
파일: RPC.py 프로젝트: Amit-DU/dht
def unpack_call(request, myprog=None, myvers=None,
                mycred=NULL_AUTH, myverf=NULL_AUTH):
    """Unpacks an RPC call message from request.

    Returns (xid, prog, vers, proc, cred, verf, u) if okay,
    where u is an xdrlib.Unpacker.
    otherwise raises either UnpackException or ReplyException.
    If myXXX is not None, checks that XXX == myXXX.
    Assumes AUTH_NONE for cred and verf; override with mycred and myverf.
    """
    if len(request) < 24:
        raise UnpackException("Packet too short (%d bytes)" % len(request))
    u = Unpacker(request)
    msg = RPCProto.unpack_rpc_msg(u)
    if msg.body.mtype == RPCProto.REPLY:
        raise UnpackException("Expected call, but got reply")
    call = msg.body.cbody
    check(RPCProto.RPC_VERSION, call.rpcvers, "RPC version",
          lambda: pack_reply(msg.xid,
                             RPCProto.MSG_DENIED,
                             RPCProto.RPC_MISMATCH,
                             RPCProto.RPC_VERSION,
                             RPCProto.RPC_VERSION).get_buffer())
    check(myprog, call.prog, "program",
          lambda: pack_reply(msg.xid,
                             RPCProto.MSG_ACCEPTED,
                             NULL_AUTH,
                             RPCProto.PROG_UNAVAIL).get_buffer())
    check(myvers, call.vers, "version",
          lambda: pack_reply(msg.xid,
                             RPCProto.MSG_ACCEPTED,
                             NULL_AUTH,
                             RPCProto.PROG_MISMATCH,
                             myvers,
                             myvers).get_buffer())
    check(mycred, call.cred, "cred",
          lambda: pack_reply(msg.xid,
                             RPCProto.MSG_DENIED,
                             RPCProto.AUTH_ERROR,
                             RPCProto.AUTH_BADCRED).get_buffer())
    check(myverf, call.verf, "verf",
          lambda: pack_reply(msg.xid,
                             RPCProto.MSG_DENIED,
                             RPCProto.AUTH_ERROR,
                             RPCProto.AUTH_BADVERF).get_buffer())
    return (msg.xid, call.prog, call.vers,
            call.proc, call.cred, call.verf, u)
예제 #4
0
def unpack_call(request,
                myprog=None,
                myvers=None,
                mycred=NULL_AUTH,
                myverf=NULL_AUTH):
    """Unpacks an RPC call message from request.

    Returns (xid, prog, vers, proc, cred, verf, u) if okay,
    where u is an xdrlib.Unpacker.
    otherwise raises either UnpackException or ReplyException.
    If myXXX is not None, checks that XXX == myXXX.
    Assumes AUTH_NONE for cred and verf; override with mycred and myverf.
    """
    if len(request) < 24:
        raise UnpackException("Packet too short (%d bytes)" % len(request))
    u = Unpacker(request)
    msg = RPCProto.unpack_rpc_msg(u)
    if msg.body.mtype == RPCProto.REPLY:
        raise UnpackException("Expected call, but got reply")
    call = msg.body.cbody
    check(
        RPCProto.RPC_VERSION, call.rpcvers, "RPC version", lambda: pack_reply(
            msg.xid, RPCProto.MSG_DENIED, RPCProto.RPC_MISMATCH, RPCProto.
            RPC_VERSION, RPCProto.RPC_VERSION).get_buffer())
    check(
        myprog, call.prog, "program",
        lambda: pack_reply(msg.xid, RPCProto.MSG_ACCEPTED, NULL_AUTH, RPCProto.
                           PROG_UNAVAIL).get_buffer())
    check(
        myvers, call.vers, "version",
        lambda: pack_reply(msg.xid, RPCProto.MSG_ACCEPTED, NULL_AUTH, RPCProto.
                           PROG_MISMATCH, myvers, myvers).get_buffer())
    check(
        mycred, call.cred, "cred",
        lambda: pack_reply(msg.xid, RPCProto.MSG_DENIED, RPCProto.AUTH_ERROR,
                           RPCProto.AUTH_BADCRED).get_buffer())
    check(
        myverf, call.verf, "verf",
        lambda: pack_reply(msg.xid, RPCProto.MSG_DENIED, RPCProto.AUTH_ERROR,
                           RPCProto.AUTH_BADVERF).get_buffer())
    return (msg.xid, call.prog, call.vers, call.proc, call.cred, call.verf, u)