Exemple #1
0
    def _login_password(self):

        # authenticate
        auth_req = iRODSMessage(msg_type='RODS_API_REQ', int_info=703)
        self.send(auth_req)

        # challenge
        challenge_msg = self.recv()
        logger.debug(challenge_msg.msg)
        challenge = challenge_msg.get_main_message(AuthChallenge).challenge
        if six.PY3:
            challenge = challenge.encode('utf-8').strip()
            padded_pwd = struct.pack(
                "%ds" % MAX_PASSWORD_LENGTH, self.account.password.encode(
                    'utf-8').strip())
        else:
            padded_pwd = struct.pack(
                "%ds" % MAX_PASSWORD_LENGTH, self.account.password)
        m = hashlib.md5()
        m.update(challenge)
        m.update(padded_pwd)
        encoded_pwd = m.digest()

        if six.PY2:
            encoded_pwd = encoded_pwd.replace('\x00', '\x01')
        elif b'\x00' in encoded_pwd:
            encoded_pwd_array = bytearray(encoded_pwd)
            encoded_pwd = bytes(encoded_pwd_array.replace(b'\x00', b'\x01'))

        pwd_msg = AuthResponse(
            response=encoded_pwd, username=self.account.proxy_user)
        pwd_request = iRODSMessage(
            msg_type='RODS_API_REQ', int_info=704, msg=pwd_msg)
        self.send(pwd_request)
        auth_response = self.recv()
Exemple #2
0
    def _login(self):
        # authenticate
        auth_req = iRODSMessage(type='RODS_API_REQ', int_info=703)
        self.send(auth_req)

        # challenge
        challenge_msg = self.recv()
        logger.debug(challenge_msg.msg)
        challenge = challenge_msg.get_main_message(AuthChallenge).challenge
        padded_pwd = struct.pack("%ds" % MAX_PASSWORD_LENGTH,
                                 self.account.password)
        m = hashlib.md5()
        m.update(challenge)
        m.update(padded_pwd)
        encoded_pwd = m.digest()

        encoded_pwd = encoded_pwd.replace('\x00', '\x01')
        pwd_msg = AuthResponse(response=encoded_pwd,
                               username=self.account.proxy_user)
        pwd_request = iRODSMessage(type='RODS_API_REQ',
                                   int_info=704,
                                   msg=pwd_msg)
        self.send(pwd_request)

        auth_response = self.recv()
    def _login_native(self, password=None):

        # Default case, PAM login will send a new password
        if password is None:
            password = self.account.password or ''

        # authenticate
        auth_req = iRODSMessage(msg_type='RODS_API_REQ', int_info=703)
        self.send(auth_req)

        # challenge
        challenge_msg = self.recv()
        logger.debug(challenge_msg.msg)
        challenge = challenge_msg.get_main_message(AuthChallenge).challenge

        # one "session" signature per connection
        # see https://github.com/irods/irods/blob/4.2.1/plugins/auth/native/libnative.cpp#L137
        # and https://github.com/irods/irods/blob/4.2.1/lib/core/src/clientLogin.cpp#L38-L60
        if six.PY2:
            self._client_signature = "".join("{:02x}".format(ord(c))
                                             for c in challenge[:16])
        else:
            self._client_signature = "".join("{:02x}".format(c)
                                             for c in challenge[:16])

        if six.PY3:
            challenge = challenge.strip()
            padded_pwd = struct.pack("%ds" % MAX_PASSWORD_LENGTH,
                                     password.encode('utf-8').strip())
        else:
            padded_pwd = struct.pack("%ds" % MAX_PASSWORD_LENGTH, password)

        m = hashlib.md5()
        m.update(challenge)
        m.update(padded_pwd)
        encoded_pwd = m.digest()

        if six.PY2:
            encoded_pwd = encoded_pwd.replace('\x00', '\x01')
        elif b'\x00' in encoded_pwd:
            encoded_pwd_array = bytearray(encoded_pwd)
            encoded_pwd = bytes(encoded_pwd_array.replace(b'\x00', b'\x01'))

        pwd_msg = AuthResponse(response=encoded_pwd,
                               username=self.account.proxy_user)
        pwd_request = iRODSMessage(msg_type='RODS_API_REQ',
                                   int_info=api_number['AUTH_RESPONSE_AN'],
                                   msg=pwd_msg)
        self.send(pwd_request)
        self.recv()
    def _login_native(self, password=None):

        # Default case, PAM login will send a new password
        if password is None:
            password = self.account.password

        # authenticate
        auth_req = iRODSMessage(msg_type='RODS_API_REQ', int_info=703)
        self.send(auth_req)

        # challenge
        challenge_msg = self.recv()
        logger.debug(challenge_msg.msg)
        challenge = challenge_msg.get_main_message(AuthChallenge).challenge

        # one "session" signature per connection
        # see https://github.com/irods/irods/blob/4.2.1/plugins/auth/native/libnative.cpp#L137
        # and https://github.com/irods/irods/blob/4.2.1/lib/core/src/clientLogin.cpp#L38-L60
        if six.PY2:
            self._client_signature = "".join("{:02x}".format(ord(c)) for c in challenge[:16])
        else:
            self._client_signature = "".join("{:02x}".format(c) for c in challenge[:16])

        if six.PY3:
            challenge = challenge.strip()
            padded_pwd = struct.pack(
                "%ds" % MAX_PASSWORD_LENGTH, password.encode(
                    'utf-8').strip())
        else:
            padded_pwd = struct.pack(
                "%ds" % MAX_PASSWORD_LENGTH, password)

        m = hashlib.md5()
        m.update(challenge)
        m.update(padded_pwd)
        encoded_pwd = m.digest()

        if six.PY2:
            encoded_pwd = encoded_pwd.replace('\x00', '\x01')
        elif b'\x00' in encoded_pwd:
            encoded_pwd_array = bytearray(encoded_pwd)
            encoded_pwd = bytes(encoded_pwd_array.replace(b'\x00', b'\x01'))

        pwd_msg = AuthResponse(
            response=encoded_pwd, username=self.account.proxy_user)
        pwd_request = iRODSMessage(
            msg_type='RODS_API_REQ', int_info=704, msg=pwd_msg)
        self.send(pwd_request)
        self.recv()
    def modify(self, user_name, option, new_value, user_zone=""):

        # must append zone to username for this API call
        if len(user_zone) > 0:
            user_name += "#" + user_zone

        with self.sess.pool.get_connection() as conn:

            # if modifying password, new value needs obfuscating
            if option == 'password':
                current_password = self.sess.pool.account.password
                new_value = obf.obfuscate_new_password(new_value, current_password, conn.client_signature)

            message_body = GeneralAdminRequest(
                "modify",
                "user",
                user_name,
                option,
                new_value,
                user_zone,
            )
            request = iRODSMessage("RODS_API_REQ", msg=message_body,
                                   int_info=api_number['GENERAL_ADMIN_AN'])

            conn.send(request)
            response = conn.recv()
        logger.debug(response.int_info)
    def modify(self, user_name, option, new_value, user_zone=""):

        # must append zone to username for this API call
        if len(user_zone) > 0:
            user_name += "#" + user_zone

        with self.sess.pool.get_connection() as conn:

            # if modifying password, new value needs obfuscating
            if option == 'password':
                current_password = self.sess.pool.account.password
                new_value = obf.obfuscate_new_password(new_value, current_password, conn.client_signature)

                # html style escaping might have to be generalized:
                # https://github.com/irods/irods/blob/4.2.1/lib/core/src/packStruct.cpp#L1913
                # https://github.com/irods/irods/blob/4.2.1/lib/core/src/packStruct.cpp#L1331-L1368
                new_value = escape(new_value, quote=False)

            message_body = GeneralAdminRequest(
                "modify",
                "user",
                user_name,
                option,
                new_value,
                user_zone,
            )
            request = iRODSMessage("RODS_API_REQ", msg=message_body,
                                   int_info=api_number['GENERAL_ADMIN_AN'])

            conn.send(request)
            response = conn.recv()
        logger.debug(response.int_info)
    def issue(self, permission, target):
        message_body = TicketAdminRequest("create", self.ticket, permission, target)
        message = iRODSMessage("RODS_API_REQ", msg=message_body, int_info=api_number['TICKET_ADMIN_AN'])

        with self.session.pool.get_connection() as conn:
            conn.send(message)
            response = conn.recv()
Exemple #8
0
    def execute(self):
        # rule input
        param_array = []
        for label, value in self.params.items():
            inOutStruct = STR_PI(myStr=value)
            param_array.append(
                MsParam(label=label, type='STR_PI', inOutStruct=inOutStruct))

        inpParamArray = MsParamArray(paramLen=len(param_array),
                                     oprType=0,
                                     MsParam_PI=param_array)

        # rule body
        addr = RodsHostAddress(hostAddr='', rodsZone='', port=0, dummyInt=0)
        condInput = StringStringMap({})
        message_body = RuleExecutionRequest(myRule=self.body,
                                            addr=addr,
                                            condInput=condInput,
                                            outParamDesc=self.output,
                                            inpParamArray=inpParamArray)

        request = iRODSMessage("RODS_API_REQ",
                               msg=message_body,
                               int_info=api_number['EXEC_MY_RULE_AN'])

        with self.session.pool.get_connection() as conn:
            conn.send(request)
            response = conn.recv()
            out_param_array = response.get_main_message(MsParamArray)
            self.session.cleanup()
        return out_param_array
    def create(self,
               name,
               resource_type,
               host="EMPTY_RESC_HOST",
               path="EMPTY_RESC_PATH",
               context="",
               zone="",
               resource_class=""):
        with self.sess.pool.get_connection() as conn:
            # check server version
            if conn.server_version < (4, 0, 0):
                # make resource, iRODS 3 style
                message_body = GeneralAdminRequest("add", "resource", name,
                                                   resource_type,
                                                   resource_class, host, path,
                                                   zone)
            else:
                message_body = GeneralAdminRequest("add", "resource", name,
                                                   resource_type,
                                                   host + ":" + path,
                                                   self.serialize(context),
                                                   zone)

            request = iRODSMessage("RODS_API_REQ",
                                   msg=message_body,
                                   int_info=api_number['GENERAL_ADMIN_AN'])

            conn.send(request)
            response = conn.recv()
            self.sess.cleanup()
            # close connections to get new agents with up to
            # date resource manager

        logger.debug(response.int_info)
        return self.get(name, zone)
Exemple #10
0
    def modify(self, user_name, option, new_value, user_zone=""):

        # must append zone to username for this API call
        if len(user_zone) > 0:
            user_name += "#" + user_zone

        with self.sess.pool.get_connection() as conn:

            # if modifying password, new value needs obfuscating
            if option == 'password':
                current_password = self.sess.pool.account.password
                new_value = obf.obfuscate_new_password(new_value, current_password, conn.client_signature)

            message_body = GeneralAdminRequest(
                "modify",
                "user",
                user_name,
                option,
                new_value,
                user_zone,
            )
            request = iRODSMessage("RODS_API_REQ", msg=message_body,
                                   int_info=api_number['GENERAL_ADMIN_AN'])

            conn.send(request)
            response = conn.recv()
        logger.debug(response.int_info)
Exemple #11
0
    def remove(self, path, recurse=True, force=False, **options):
        if recurse:
            options[kw.RECURSIVE_OPR__KW] = ''
        if force:
            options[kw.FORCE_FLAG_KW] = ''

        try:
            oprType = options[kw.OPR_TYPE_KW]
        except KeyError:
            oprType = 0

        message_body = CollectionRequest(
            collName=path,
            flags=0,
            oprType=oprType,
            KeyValPair_PI=StringStringMap(options))
        message = iRODSMessage('RODS_API_REQ',
                               msg=message_body,
                               int_info=api_number['RM_COLL_AN'])
        with self.sess.pool.get_connection() as conn:
            conn.send(message)
            response = conn.recv()

            while response.int_info == SYS_SVR_TO_CLI_COLL_STAT:
                conn.reply(SYS_CLI_TO_SVR_COLL_STAT_REPLY)
                response = conn.recv()
Exemple #12
0
    def unlink(self, path, force=False, **options):
        if force:
            options[kw.FORCE_FLAG_KW] = ''

        try:
            oprType = options[kw.OPR_TYPE_KW]
        except KeyError:
            oprType = 0

        message_body = FileOpenRequest(
            objPath=path,
            createMode=0,
            openFlags=0,
            offset=0,
            dataSize=-1,
            numThreads=self.sess.numThreads,
            oprType=oprType,
            KeyValPair_PI=StringStringMap(options),
        )
        message = iRODSMessage('RODS_API_REQ',
                               msg=message_body,
                               int_info=api_number['DATA_OBJ_UNLINK_AN'])

        with self.sess.pool.get_connection() as conn:
            conn.send(message)
            response = conn.recv()
    def create(self, path, resource=None, **options):
        options[kw.DATA_TYPE_KW] = 'generic'

        if resource:
            options[kw.DEST_RESC_NAME_KW] = resource
        else:
            # Use client-side default resource if available
            try:
                options[kw.DEST_RESC_NAME_KW] = self.sess.default_resource
            except AttributeError:
                pass

        message_body = FileOpenRequest(
            objPath=path,
            createMode=0o644,
            openFlags=0,
            offset=0,
            dataSize=-1,
            numThreads=self.sess.numThreads,
            oprType=0,
            KeyValPair_PI=StringStringMap(options),
        )
        message = iRODSMessage('RODS_API_REQ', msg=message_body,
                               int_info=api_number['DATA_OBJ_CREATE_AN'])

        with self.sess.pool.get_connection() as conn:
            conn.send(message)
            response = conn.recv()
            desc = response.int_info
            conn.close_file(desc)

        return self.get(path)
Exemple #14
0
    def _coll_remove_yield(self, path, recurse=True, force=False, **options):
        """
        interruptible version of CollectionManager.coll_remove() method
        """
        if recurse:
            options[kw.RECURSIVE_OPR__KW] = ''
        if force:
            options[kw.FORCE_FLAG_KW] = ''

        try:
            oprType = options[kw.OPR_TYPE_KW]
        except KeyError:
            oprType = 0

        message_body = message.CollectionRequest(
            collName=path,
            flags=0,
            oprType=oprType,
            KeyValPair_PI=message.StringStringMap(options))
        msg = message.iRODSMessage('RODS_API_REQ',
                                   msg=message_body,
                                   int_info=api_number['RM_COLL_AN'])
        with self.session.pool.get_connection() as conn:
            conn.send(msg)
            response = conn.recv()

            try:
                while response.int_info == const.SYS_SVR_TO_CLI_COLL_STAT:
                    conn.reply(const.SYS_CLI_TO_SVR_COLL_STAT_REPLY)
                    yield
                    response = conn.recv()
            except GeneratorExit:
                # destroy connection which is in a bad state (could fix?)
                conn.release(destroy=True)
    def open(self, path, mode, options=None):

        flags, seek_to_end = {
            'r': (O_RDONLY, False),
            'r+': (O_RDWR, False),
            'w': (O_WRONLY | O_CREAT, False),
            'w+': (O_RDWR | O_CREAT, False),
            'a': (O_WRONLY | O_CREAT, True),
            'a+': (O_RDWR | O_CREAT, True),
        }[mode]
        # TODO: Use seek_to_end

        message_body = FileOpenRequest(
            objPath=path,
            createMode=0,
            openFlags=flags,
            offset=0,
            dataSize=-1,
            numThreads=0,
            oprType=0,
            KeyValPair_PI=StringStringMap(options),
        )
        message = iRODSMessage('RODS_API_REQ', msg=message_body,
                               int_info=api_number['DATA_OBJ_OPEN_AN'])

        conn = self.sess.pool.get_connection()
        conn.send(message)
        desc = conn.recv().int_info

        return BufferedRandom(iRODSDataObjectFileRaw(conn, desc, options))
    def unlink(self, path, force=False, **options):
        if force:
            options[kw.FORCE_FLAG_KW] = ''

        try:
            oprType = options[kw.OPR_TYPE_KW]
        except KeyError:
            oprType = 0

        message_body = FileOpenRequest(
            objPath=path,
            createMode=0,
            openFlags=0,
            offset=0,
            dataSize=-1,
            numThreads=self.sess.numThreads,
            oprType=oprType,
            KeyValPair_PI=StringStringMap(options),
        )
        message = iRODSMessage('RODS_API_REQ', msg=message_body,
                               int_info=api_number['DATA_OBJ_UNLINK_AN'])

        with self.sess.pool.get_connection() as conn:
            conn.send(message)
            response = conn.recv()
    def remove_child(self, parent, child):
        with self.sess.pool.get_connection() as conn:
            # check server version
            server_version = tuple(int(token)
                                   for token in conn.server_version.replace('rods', '').split('.'))
            if server_version < (4, 0, 0):
                # No resource hierarchies before iRODS 4
                raise OperationNotSupported

            message_body = GeneralAdminRequest(
                "rm",
                "childfromresc",
                parent,
                child
            )

            request = iRODSMessage("RODS_API_REQ", msg=message_body,
                                   int_info=api_number['GENERAL_ADMIN_AN'])

            conn.send(request)
            response = conn.recv()
            self.sess.cleanup()
            # close connections to get new agents with up to
            # date resource manager
        logger.debug(response.int_info)
    def create(self,
               name,
               resource_type,
               host,
               path,
               context="",
               zone="",
               resource_class=""):
        with self.sess.pool.get_connection() as conn:
            # check server version
            server_version = tuple(
                int(token) for token in conn.server_version.replace(
                    'rods', '').split('.'))
            if server_version < (4, 0, 0):
                # make resource, iRODS 3 style
                message_body = GeneralAdminRequest("add", "resource", name,
                                                   resource_type,
                                                   resource_class, host, path,
                                                   zone)
            else:
                message_body = GeneralAdminRequest("add", "resource", name,
                                                   resource_type,
                                                   host + ":" + path, context,
                                                   zone)

            request = iRODSMessage("RODS_API_REQ",
                                   msg=message_body,
                                   int_info=api_number['GENERAL_ADMIN_AN'])

            conn.send(request)
            response = conn.recv()
            self.sess.cleanup()
            # close connections to get new agents with up to
            # date resource manager
        logger.debug(response.int_info)
    def create(self, name, resource_type, host, path, context="", zone="", resource_class=""):
        with self.sess.pool.get_connection() as conn:
            # check server version
            server_version = tuple(int(token)
                                   for token in conn.server_version.replace('rods', '').split('.'))
            if server_version < (4, 0, 0):
                # make resource, iRODS 3 style
                message_body = GeneralAdminRequest(
                    "add",
                    "resource",
                    name,
                    resource_type,
                    resource_class,
                    host,
                    path,
                    zone
                )
            else:
                message_body = GeneralAdminRequest(
                    "add",
                    "resource",
                    name,
                    resource_type,
                    host + ":" + path,
                    context,
                    zone
                )

            request = iRODSMessage("RODS_API_REQ", msg=message_body,
                                   int_info=api_number['GENERAL_ADMIN_AN'])

            conn.send(request)
            response = conn.recv()
            self.sess.cleanup() # close connections to get new agents with up to date resource manager
        logger.debug(response.int_info)
    def remove_child(self, parent, child):
        with self.sess.pool.get_connection() as conn:
            # check server version
            server_version = tuple(int(token)
                                   for token in conn.server_version.replace('rods', '').split('.'))
            if server_version < (4, 0, 0):
                # No resource hierarchies before iRODS 4
                raise OperationNotSupported

            message_body = GeneralAdminRequest(
                "rm",
                "childfromresc",
                parent,
                child
            )

            request = iRODSMessage("RODS_API_REQ", msg=message_body,
                                   int_info=api_number['GENERAL_ADMIN_AN'])

            conn.send(request)
            response = conn.recv()
            self.sess.cleanup()
                              # close connections to get new agents with up to
                              # date resource manager
        logger.debug(response.int_info)
 def recv(self, into_buffer=None, return_message=(), acceptable_errors=()):
     acceptable_codes = set(nominal_code(e) for e in acceptable_errors)
     try:
         if into_buffer is None:
             msg = iRODSMessage.recv(self.socket)
         else:
             msg = iRODSMessage.recv_into(self.socket, into_buffer)
     except (socket.error, socket.timeout) as e:
         # If _recv_message_in_len() fails in recv() or recv_into(),
         # it will throw a socket.error exception. The exception is
         # caught here, a critical message is logged, and is wrapped
         # in a NetworkException with a more user friendly message
         logger.critical(e)
         logger.error("Could not receive server response")
         self.release(True)
         raise NetworkException("Could not receive server response")
     if isinstance(return_message, list): return_message[:] = [msg]
     if msg.int_info < 0:
         try:
             err_msg = iRODSMessage(
                 msg=msg.error).get_main_message(Error).RErrMsg_PI[0].msg
         except TypeError:
             err_msg = None
         if nominal_code(msg.int_info) not in acceptable_codes:
             raise get_exception_by_code(msg.int_info, err_msg)
     return msg
Exemple #22
0
    def _login_pam(self):

        ctx_user = '******' % (AUTH_USER_KEY, self.account.client_user)
        ctx_pwd = '%s=%s' % (AUTH_PWD_KEY, self.account.password)
        ctx_ttl = '%s=%s' % (AUTH_TTL_KEY, "60")

        ctx = ";".join([ctx_user, ctx_pwd, ctx_ttl])

        message_body = PluginAuthMessage(auth_scheme_=PAM_AUTH_SCHEME,
                                         context_=ctx)

        auth_req = iRODSMessage(
            msg_type='RODS_API_REQ',
            msg=message_body,
            # int_info=725
            int_info=1201)

        self.send(auth_req)
        # Getting the new password
        output_message = self.recv()

        auth_out = output_message.get_main_message(AuthPluginOut)

        self.disconnect()
        self._connect()
        self._login_native(password=auth_out.result_)

        logger.info("PAM authorization validated")
Exemple #23
0
    def execute(self,
                limit=MAX_SQL_ROWS,
                offset=0,
                options=0,
                conditions=None):
        target = self._alias or self._sql

        if conditions is None:
            conditions = StringStringMap({})

        sql_args = {}
        for i, arg in enumerate(self._args[:10], start=1):
            sql_args['arg{}'.format(i)] = arg

        message_body = SpecificQueryRequest(sql=target,
                                            maxRows=limit,
                                            continueInx=self._continue_index,
                                            rowOffset=offset,
                                            options=0,
                                            KeyValPair_PI=conditions,
                                            **sql_args)

        request = iRODSMessage("RODS_API_REQ",
                               msg=message_body,
                               int_info=api_number['SPECIFIC_QUERY_AN'])

        with self.session.pool.get_connection() as conn:
            conn.send(request)
            response = conn.recv()

        results = response.get_main_message(GenQueryResponse)
        return SpecificQueryResultSet(results, self._columns)
    def _login_pam(self):

        ctx_user = '******' % (AUTH_USER_KEY, self.account.client_user)
        ctx_pwd = '%s=%s' % (AUTH_PWD_KEY, self.account.password)
        ctx_ttl = '%s=%s' % (AUTH_TTL_KEY, "60")

        ctx = ";".join([ctx_user, ctx_pwd, ctx_ttl])

        message_body = PluginAuthMessage(
            auth_scheme_=PAM_AUTH_SCHEME,
            context_=ctx
        )

        auth_req = iRODSMessage(
            msg_type='RODS_API_REQ',
            msg=message_body,
            # int_info=725
            int_info=1201
        )

        self.send(auth_req)
        # Getting the new password
        output_message = self.recv()

        auth_out = output_message.get_main_message(AuthPluginOut)

        self.disconnect()
        self._connect()
        self._login_native(password=auth_out.result_)

        logger.info("PAM authorization validated")
    def move(self, src_path, dest_path):
        # check if dest is an existing collection
        # if so append collection name to it
        if self.sess.collections.exists(dest_path):
            coll_name = src_path.rsplit("/", 1)[1]
            target_path = dest_path + "/" + coll_name
        else:
            target_path = dest_path

        src = FileOpenRequest(
            objPath=src_path,
            createMode=0,
            openFlags=0,
            offset=0,
            dataSize=0,
            numThreads=0,
            oprType=12,  # RENAME_COLL
            KeyValPair_PI=StringStringMap(),
        )
        dest = FileOpenRequest(
            objPath=target_path,
            createMode=0,
            openFlags=0,
            offset=0,
            dataSize=0,
            numThreads=0,
            oprType=12,  # RENAME_COLL
            KeyValPair_PI=StringStringMap(),
        )
        message_body = ObjCopyRequest(srcDataObjInp_PI=src, destDataObjInp_PI=dest)
        message = iRODSMessage("RODS_API_REQ", msg=message_body, int_info=api_number["DATA_OBJ_RENAME_AN"])

        with self.sess.pool.get_connection() as conn:
            conn.send(message)
            response = conn.recv()
Exemple #26
0
 def chksum(self, path, **options):
     """
     See: https://github.com/irods/irods/blob/4-2-stable/lib/api/include/dataObjChksum.h
     for a list of applicable irods.keywords options.
     """
     r_error_stack = options.pop('r_error',None)
     message_body = DataObjChksumRequest(path, **options)
     message = iRODSMessage('RODS_API_REQ', msg=message_body,
                            int_info=api_number['DATA_OBJ_CHKSUM_AN'])
     checksum = ""
     msg_retn = []
     with self.sess.pool.get_connection() as conn:
         conn.send(message)
         try:
             response = conn.recv(return_message = msg_retn)
         except ex.CHECK_VERIFICATION_RESULTS as exc:
             # We'll get a response in the client to help qualify or elaborate on the error thrown.
             if msg_retn: response = msg_retn[0]
             logging.warning("Exception checksumming data object %r - %r",path,exc)
         if 'response' in locals():
             try:
                 results = response.get_main_message(DataObjChksumResponse, r_error = r_error_stack)
                 checksum = results.myStr.strip()
                 if checksum[0] in ( '[','{' ):  # in iRODS 4.2.11 and later, myStr is in JSON format.
                     exc = Server_Checksum_Warning( checksum )
                     if not r_error_stack:
                         r_error_stack.fill(exc.response)
                     raise exc
             except iRODSMessage.ResponseNotParseable:
                 # response.msg is None when VERIFY_CHKSUM_KW is used
                 pass
     return checksum
    def modify(self, user_name, option, new_value, user_zone=""):

        # must append zone to username for this API call
        if len(user_zone) > 0:
            user_name += "#" + user_zone

        with self.sess.pool.get_connection() as conn:

            # if modifying password, new value needs obfuscating
            if option == 'password':
                current_password = self.sess.pool.account.password
                new_value = obf.obfuscate_new_password(new_value,
                                                       current_password,
                                                       conn.client_signature)

                # html style escaping might have to be generalized:
                # https://github.com/irods/irods/blob/4.2.1/lib/core/src/packStruct.cpp#L1913
                # https://github.com/irods/irods/blob/4.2.1/lib/core/src/packStruct.cpp#L1331-L1368
                new_value = escape(new_value, quote=False)

            message_body = GeneralAdminRequest(
                "modify",
                "user",
                user_name,
                option,
                new_value,
                user_zone,
            )
            request = iRODSMessage("RODS_API_REQ",
                                   msg=message_body,
                                   int_info=api_number['GENERAL_ADMIN_AN'])

            conn.send(request)
            response = conn.recv()
        logger.debug(response.int_info)
 def create(self, path):
     message_body = CollectionRequest(collName=path, KeyValPair_PI=StringStringMap())
     message = iRODSMessage("RODS_API_REQ", msg=message_body, int_info=api_number["COLL_CREATE_AN"])
     with self.sess.pool.get_connection() as conn:
         conn.send(message)
         response = conn.recv()
     return self.get(path)
Exemple #29
0
    def create(self, path, resource=None, options=None):
        if options is None:
            options = {}
        kvp = {kw.DATA_TYPE_KW: 'generic'}
        if resource:
            kvp[kw.DEST_RESC_NAME_KW] = resource
        kvp.update(options)
        message_body = FileOpenRequest(
            objPath=path,
            createMode=0644,
            openFlags=0,
            offset=0,
            dataSize=-1,
            numThreads=0,
            oprType=0,
            KeyValPair_PI=StringStringMap(kvp),
        )
        message = iRODSMessage('RODS_API_REQ',
                               msg=message_body,
                               int_info=api_number['DATA_OBJ_CREATE_AN'])

        with self.sess.pool.get_connection() as conn:
            conn.send(message)
            response = conn.recv()
            desc = response.int_info
            conn.close_file(desc)

        return self.get(path)
    def remove(self, path, recurse=True, force=False, **options):
        if recurse:
            options[kw.RECURSIVE_OPR__KW] = ''
        if force:
            options[kw.FORCE_FLAG_KW] = ''

        try:
            oprType = options[kw.OPR_TYPE_KW]
        except KeyError:
            oprType = 0

        message_body = CollectionRequest(
            collName=path,
            flags = 0,
            oprType = oprType,
            KeyValPair_PI=StringStringMap(options)
        )
        message = iRODSMessage('RODS_API_REQ', msg=message_body,
                               int_info=api_number['RM_COLL_AN'])
        with self.sess.pool.get_connection() as conn:
            conn.send(message)
            response = conn.recv()

            while response.int_info == SYS_SVR_TO_CLI_COLL_STAT:
                conn.reply(SYS_CLI_TO_SVR_COLL_STAT_REPLY)
                response = conn.recv()
    def read_file(self, desc, size=-1, buffer=None):
        if size < 0:
            size = len(buffer)
        elif buffer is not None:
            size = min(size, len(buffer))

        message_body = OpenedDataObjRequest(
            l1descInx=desc,
            len=size,
            whence=0,
            oprType=0,
            offset=0,
            bytesWritten=0,
            KeyValPair_PI=StringStringMap()
        )
        message = iRODSMessage('RODS_API_REQ', msg=message_body,
                               int_info=api_number['DATA_OBJ_READ_AN'])

        logger.debug(desc)
        self.send(message)
        if buffer is None:
            response = self.recv()
        else:
            response = self.recv_into(buffer)

        return response.bs
Exemple #32
0
    def create(self, path, resource=None, **options):
        options[kw.DATA_TYPE_KW] = 'generic'

        if resource:
            options[kw.DEST_RESC_NAME_KW] = resource
        else:
            # Use client-side default resource if available
            try:
                options[kw.DEST_RESC_NAME_KW] = self.sess.default_resource
            except AttributeError:
                pass

        message_body = FileOpenRequest(
            objPath=path,
            createMode=0o644,
            openFlags=0,
            offset=0,
            dataSize=-1,
            numThreads=self.sess.numThreads,
            oprType=0,
            KeyValPair_PI=StringStringMap(options),
        )
        message = iRODSMessage('RODS_API_REQ',
                               msg=message_body,
                               int_info=api_number['DATA_OBJ_CREATE_AN'])

        with self.sess.pool.get_connection() as conn:
            conn.send(message)
            response = conn.recv()
            desc = response.int_info
            conn.close_file(desc)

        return self.get(path)
Exemple #33
0
 def _close_replica(self):
     server_version = ast.literal_eval(
         os.environ.get('IRODS_VERSION_OVERRIDE', '()'))
     if (server_version or self.conn.server_version
         ) < IRODS_SERVER_WITH_CLOSE_REPLICA_API:
         return False
     message_body = JSON_Message(
         {
             "fd": self.desc,
             "send_notification": False,
             "update_size": False,
             "update_status": False,
             "compute_checksum": False
         },
         server_version=self.conn.server_version)
     self.conn.send(
         iRODSMessage('RODS_API_REQ',
                      msg=message_body,
                      int_info=api_number['REPLICA_CLOSE_APN']))
     try:
         self.conn.recv().int_info
     except Exception:
         logger.warning('** ERROR on closing replica **')
         raise
     return True
Exemple #34
0
    def execute(self, limit=MAX_SQL_ROWS, offset=0, options=0, conditions=None):
        target =  self._alias or self._sql

        if conditions is None:
            conditions = StringStringMap({})

        sql_args = {}
        for i, arg in enumerate(self._args[:10]):
            sql_args['arg{}'.format(i)] = arg

        message_body = SpecificQueryRequest(sql=target,
                                            maxRows=limit,
                                            continueInx=self._continue_index,
                                            rowOffset=offset,
                                            options=0,
                                            KeyValPair_PI=conditions,
                                            **sql_args)

        request = iRODSMessage("RODS_API_REQ", msg=message_body, int_info=api_number['SPECIFIC_QUERY_AN'])

        with self.session.pool.get_connection() as conn:
            conn.send(request)
            response = conn.recv()

        results = response.get_main_message(GenQueryResponse)
        return SpecificQueryResultSet(results, self._columns)
    def read_file(self, desc, size=-1, buffer=None):
        if size < 0:
            size = len(buffer)
        elif buffer is not None:
            size = min(size, len(buffer))

        message_body = OpenedDataObjRequest(l1descInx=desc,
                                            len=size,
                                            whence=0,
                                            oprType=0,
                                            offset=0,
                                            bytesWritten=0,
                                            KeyValPair_PI=StringStringMap())
        message = iRODSMessage('RODS_API_REQ',
                               msg=message_body,
                               int_info=api_number['DATA_OBJ_READ_AN'])

        logger.debug(desc)
        self.send(message)
        if buffer is None:
            response = self.recv()
        else:
            response = self.recv_into(buffer)

        return response.bs
    def create(self, path, resource=None, options={}):
        kvp = {kw.DATA_TYPE_KW: 'generic'}
        if resource:
            kvp[kw.DEST_RESC_NAME_KW] = resource
        kvp.update(options)
        message_body = FileOpenRequest(
            objPath=path,
            createMode=0644,
            openFlags=0,
            offset=0,
            dataSize=-1,
            numThreads=0,
            oprType=0,
            KeyValPair_PI=StringStringMap(kvp),
        )
        message = iRODSMessage('RODS_API_REQ', msg=message_body,
            int_info=api_number['DATA_OBJ_CREATE_AN'])

        with self.sess.pool.get_connection() as conn:
            conn.send(message)
            response = conn.recv()
            desc = response.int_info
            conn.close_file(desc)

        return self.get(path)
 def _call_atomic_metadata_api(self, request_text):
     with self.sess.pool.get_connection() as conn:
         request_msg = iRODSMessage("RODS_API_REQ",  JSON_Message( request_text, conn.server_version ),
                                    int_info=api_number['ATOMIC_APPLY_METADATA_OPERATIONS_APN'])
         conn.send( request_msg )
         response = conn.recv()
     response_msg = response.get_json_encoded_struct()
     logger.debug("in atomic_metadata, server responded with: %r",response_msg)
Exemple #38
0
    def close_file(self, desc):
        message_body = FileCloseRequest(l1descInx=desc)
        message = iRODSMessage('RODS_API_REQ',
                               msg=message_body,
                               int_info=api_number['DATA_OBJ_CLOSE201_AN'])

        self.send(message)
        response = self.recv()
 def remove(self, model_cls, path, meta):
     resource_type = self._model_class_to_resource_type(model_cls)
     message_body = MetadataRequest("rm", "-" + resource_type, path, meta.name, meta.value, meta.units)
     request = iRODSMessage("RODS_API_REQ", msg=message_body, int_info=api_number["MOD_AVU_METADATA_AN"])
     with self.sess.pool.get_connection() as conn:
         conn.send(request)
         response = conn.recv()
     logger.debug(response.int_info)
 def remove(self, user_name, user_zone=""):
     message_body = GeneralAdminRequest("rm", "user", user_name, user_zone)
     request = iRODSMessage("RODS_API_REQ",
                            msg=message_body,
                            int_info=api_number['GENERAL_ADMIN_AN'])
     with self.sess.pool.get_connection() as conn:
         conn.send(request)
         response = conn.recv()
     logger.debug(response.int_info)
    def close_file(self, desc):
        message_body = FileCloseRequest(
            l1descInx=desc
        )
        message = iRODSMessage('RODS_API_REQ', msg=message_body,
            int_info=api_number['DATA_OBJ_CLOSE201_AN'])

        self.send(message)
        response = self.recv()
Exemple #42
0
 def remove_by_id(self,*ids):
     with self.session.pool.get_connection() as conn:
         for id_ in ids:
             request = iRODSMessage("RODS_API_REQ", msg=RemoveRuleMessage(id_),
                                    int_info=api_number['RULE_EXEC_DEL_AN'])
             conn.send(request)
             response = conn.recv()
             if response.int_info != 0:
                 raise RuntimeError("Error removing rule {id_}".format(**locals()))
Exemple #43
0
    def supply(self):
        message_body = TicketAdminRequest("session", self.ticket)
        message = iRODSMessage("RODS_API_REQ",
                               msg=message_body,
                               int_info=api_number['TICKET_ADMIN_AN'])

        with self.session.pool.get_connection() as conn:
            conn.send(message)
            response = conn.recv()
Exemple #44
0
 def write_file(self, desc, string):
     message_body = FileWriteRequest(dataObjInx=desc, len=len(string))
     message = iRODSMessage('RODS_API_REQ',
                            msg=message_body,
                            bs=string,
                            int_info=api_number['DATA_OBJ_WRITE201_AN'])
     self.send(message)
     response = self.recv()
     return response.int_info
    def setInfo(self):
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        try:
            s.connect((self.host, self.port))
        except socket.error:
            raise NetworkException(
                "Could not connect to specified host and port!")
        mainMessage = StartupPack((self.dummycred, self.dummycred),
                                  (self.dummycred, self.dummycred))

        msg = iRODSMessage(msg_type='RODS_CONNECT', msg=mainMessage)
        string = msg.pack()
        try:
            s.sendall(string)
        except:
            raise NetworkException("Unable to send message")
        try:
            msg = iRODSMessage.recv(s)
        except socket.error:
            exit(1)
        if msg.int_info < 0:
            raise get_exception_by_code(msg.int_info)

        msg = iRODSMessage(msg_type='RODS_API_REQ',
                           msg=None,
                           int_info=api_number['GET_MISC_SVR_INFO_AN'])
        string = msg.pack()
        try:
            s.sendall(string)
        except:
            raise NetworkException("Unable to send message")
        try:
            miscsvrinfo = iRODSMessage.recv(s)
        except socket.error:
            exit(1)
        if msg.int_info < 0:
            raise get_exception_by_code(msg.int_info)

        root = ET.fromstring(miscsvrinfo.msg)
        self.setProps("RCAT_ENABLED" if int(root[0].text) else "RCAT_DISABLED",
                      root[2].text, root[3].text, root[4].text,
                      int(root[1].text))
        s.close()
        return
    def copy(self, src_model_cls, dest_model_cls, src, dest):
        src_resource_type = self._model_class_to_resource_type(src_model_cls)
        dest_resource_type = self._model_class_to_resource_type(dest_model_cls)
        message_body = MetadataRequest("cp", "-" + src_resource_type, "-" + dest_resource_type, src, dest)
        request = iRODSMessage("RODS_API_REQ", msg=message_body, int_info=api_number["MOD_AVU_METADATA_AN"])

        with self.sess.pool.get_connection() as conn:
            conn.send(request)
            response = conn.recv()
        logger.debug(response.int_info)
Exemple #47
0
    def read_file(self, desc, size):
        message_body = FileReadRequest(l1descInx=desc, len=size)
        message = iRODSMessage('RODS_API_REQ',
                               msg=message_body,
                               int_info=api_number['DATA_OBJ_READ201_AN'])

        logger.debug(desc)
        self.send(message)
        response = self.recv()
        return response.bs
Exemple #48
0
 def create(self, path):
     message_body = CollectionRequest(collName=path,
                                      KeyValPair_PI=StringStringMap())
     message = iRODSMessage('RODS_API_REQ',
                            msg=message_body,
                            int_info=api_number['COLL_CREATE201_AN'])
     with self.sess.pool.get_connection() as conn:
         conn.send(message)
         response = conn.recv()
     return self.get(path)
 def disconnect(self):
     disconnect_msg = iRODSMessage(msg_type='RODS_DISCONNECT')
     self.send(disconnect_msg)
     try:
         # SSL shutdown handshake
         self.socket = self.socket.unwrap()
     except AttributeError:
         pass
     self.socket.shutdown(socket.SHUT_RDWR)
     self.socket.close()
     self.socket = None
 def write_file(self, desc, string):
     message_body = FileWriteRequest(
         dataObjInx=desc,
         len=len(string)
     )
     message = iRODSMessage('RODS_API_REQ', msg=message_body,
         bs=string,
         int_info=api_number['DATA_OBJ_WRITE201_AN'])
     self.send(message)
     response = self.recv()
     return response.int_info
    def _login(self):
        # authenticate
        auth_req = iRODSMessage(type='RODS_API_REQ', int_info=703)
        self.send(auth_req)

        # challenge
        challenge_msg = self.recv()
        logging.debug(challenge_msg.msg)
        challenge = challenge_msg.get_main_message(AuthChallenge).challenge
        padded_pwd = struct.pack("%ds" % MAX_PASSWORD_LENGTH, self.account.password)
        m = hashlib.md5()
        m.update(challenge)
        m.update(padded_pwd)
        encoded_pwd = m.digest()

        encoded_pwd = encoded_pwd.replace('\x00', '\x01')
        pwd_msg = AuthResponse(response=encoded_pwd, username=self.account.proxy_user)
        pwd_request = iRODSMessage(type='RODS_API_REQ', int_info=704, msg=pwd_msg)
        self.send(pwd_request)

        auth_response = self.recv()
    def read_file(self, desc, size):
        message_body = FileReadRequest(
            l1descInx=desc,
            len=size
        )
        message = iRODSMessage('RODS_API_REQ', msg=message_body,
            int_info=api_number['DATA_OBJ_READ201_AN'])

        logging.debug(desc)
        self.send(message)
        response = self.recv()
        return response.bs
Exemple #53
0
 def execute(self):
     message_body = self._message()
     message = iRODSMessage("RODS_API_REQ", msg=message_body, int_info=api_number["GEN_QUERY_AN"])
     with self.sess.pool.get_connection() as conn:
         conn.send(message)
         try:
             result_message = conn.recv()
             results = result_message.get_main_message(GenQueryResponse)
             result_set = ResultSet(results)
         except CAT_NO_ROWS_FOUND:
             result_set = ResultSet(empty_gen_query_out(self.columns.keys()))
     return result_set
    def rename(self, path, new_name):
        message_body = FileRenameRequest(
            fileType=0,
            RHostAddr_PI=StringStringIntegerIntegerMap('data.iplantc.org', 'iplant', 1247), # TODO need some way to get connection information here...
            oldFileName=basename(path),
            newFileName=basename(new_name)
        )
        message = iRODSMessage('RODS_API_REQ', msg=message_body,
            int_info=api_number['DATA_OBJ_RENAME_AN'])

        with self.sess.pool.get_connection() as conn:
            conn.send(message)
            response = conn.recv()
 def remove(self, user_name, user_zone=""):
     message_body = GeneralAdminRequest(
         "rm",
         "user",
         user_name,
         user_zone
     )
     request = iRODSMessage("RODS_API_REQ", msg=message_body,
                            int_info=api_number['GENERAL_ADMIN_AN'])
     with self.sess.pool.get_connection() as conn:
         conn.send(request)
         response = conn.recv()
     logger.debug(response.int_info)
    def seek_file(self, desc, offset, whence):
        message_body = FileSeekRequest(
            fileInx=desc,
            offset=offset,
            whence=whence
        )
        message = iRODSMessage('RODS_API_REQ', msg=message_body,
            int_info=api_number['DATA_OBJ_LSEEK201_AN'])

        self.send(message)
        response = self.recv()
        offset = response.get_main_message(FileSeekResponse).offset
        return offset
 def recv(self):
     try:
         msg = iRODSMessage.recv(self.socket)
     except socket.error:
         logger.error("Could not receive server response")
         self.release(True)
         raise NetworkException("Could not receive server response")
     if msg.int_info < 0:
         try:
             err_msg = iRODSMessage(msg=msg.error).get_main_message(Error).RErrMsg_PI[0].msg
         except TypeError:
             raise get_exception_by_code(msg.int_info)
         raise get_exception_by_code(msg.int_info, err_msg)
     return msg
    def gsi_client_auth_request(self):

        # Request for authentication with GSI on current user
        message_body = GSIAuthMessage(
            auth_scheme_=GSI_AUTH_PLUGIN,
            context_='a_user=%s' % self.account.client_user
        )
        # GSI = 1201
# https://github.com/irods/irods/blob/master/lib/api/include/apiNumber.h#L158
        auth_req = iRODSMessage(
            msg_type='RODS_API_REQ', msg=message_body, int_info=1201)
        self.send(auth_req)
        # Getting the challenge message
        self.recv()
 def create(self, path, recurse=True, **options):
     if recurse:
         options[kw.RECURSIVE_OPR__KW] = ''
    
     message_body = CollectionRequest(
         collName=path,
         KeyValPair_PI=StringStringMap(options)
     )
     message = iRODSMessage('RODS_API_REQ', msg=message_body,
                            int_info=api_number['COLL_CREATE_AN'])
     with self.sess.pool.get_connection() as conn:
         conn.send(message)
         response = conn.recv()
     return self.get(path)
 def set(self, acl, recursive=False):
     message_body = ModAclRequest(
         recursiveFlag=int(recursive),
         accessLevel=acl.access_name,
         userName=acl.user_name,
         zone=acl.user_zone,
         path=acl.path
     )
     request = iRODSMessage("RODS_API_REQ", msg=message_body,
                            int_info=api_number['MOD_ACCESS_CONTROL_AN'])
     with self.sess.pool.get_connection() as conn:
         conn.send(request)
         response = conn.recv()
     logger.debug(response.int_info)