Search in sources :

Example 1 with ErrorResponse

use of org.dcache.xrootd.protocol.messages.ErrorResponse in project xrootd4j by dCache.

the class XrootdAuthenticationHandler method channelRead.

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    /* Pass along any message that is not an xrootd requests.
         */
    if (!(msg instanceof XrootdRequest)) {
        super.channelRead(ctx, msg);
        return;
    }
    XrootdRequest request = (XrootdRequest) msg;
    int reqId = request.getRequestId();
    try {
        switch(reqId) {
            case kXR_login:
                try {
                    if (_isInProgress.compareAndSet(false, true)) {
                        try {
                            _state = State.NO_LOGIN;
                            LoginRequest loginRequest = (LoginRequest) request;
                            loginRequest.setUserName(UserNameUtils.checkUsernameValid(loginRequest.getUserName()));
                            _session = new XrootdSession(_sessionId, ctx.channel(), loginRequest);
                            request.setSession(_session);
                            doOnLogin(ctx, loginRequest);
                            _sessions.put(_sessionId, _session);
                        } finally {
                            _isInProgress.set(false);
                        }
                    } else {
                        throw new XrootdException(kXR_inProgress, "Login in progress");
                    }
                } finally {
                    ReferenceCountUtil.release(request);
                }
                break;
            case kXR_auth:
                try {
                    if (_isInProgress.compareAndSet(false, true)) {
                        try {
                            switch(_state) {
                                case NO_LOGIN:
                                    throw new XrootdException(kXR_NotAuthorized, "Login required");
                                case AUTH:
                                    throw new XrootdException(kXR_InvalidRequest, "Already authenticated");
                            }
                            request.setSession(_session);
                            doOnAuthentication(ctx, (AuthenticationRequest) request);
                        } finally {
                            _isInProgress.set(false);
                        }
                    } else {
                        throw new XrootdException(kXR_inProgress, "Login in progress");
                    }
                } finally {
                    ReferenceCountUtil.release(request);
                }
                break;
            case kXR_endsess:
                try {
                    switch(_state) {
                        case NO_LOGIN:
                            throw new XrootdException(kXR_NotAuthorized, "Login required");
                        case NO_AUTH:
                            throw new XrootdException(kXR_NotAuthorized, "Authentication required");
                    }
                    doOnEndSession(ctx, (EndSessionRequest) request);
                } finally {
                    ReferenceCountUtil.release(request);
                }
                break;
            case kXR_bind:
                if (_tlsSessionInfo != null && _tlsSessionInfo.serverUsesTls()) {
                    boolean isStarted = _tlsSessionInfo.serverTransitionedToTLS(kXR_bind, ctx);
                    _log.debug("kXR_bind, server has now transitioned to tls? {}.", isStarted);
                }
                super.channelRead(ctx, msg);
                break;
            case kXR_protocol:
                request.setSession(_session);
                super.channelRead(ctx, msg);
                break;
            case kXR_ping:
                if (_state == State.NO_LOGIN) {
                    ReferenceCountUtil.release(request);
                    throw new XrootdException(kXR_NotAuthorized, "Login required");
                }
                request.setSession(_session);
                super.channelRead(ctx, msg);
                break;
            default:
                switch(_state) {
                    case NO_LOGIN:
                        ReferenceCountUtil.release(request);
                        throw new XrootdException(kXR_NotAuthorized, "Login required");
                    case NO_AUTH:
                        ReferenceCountUtil.release(request);
                        throw new XrootdException(kXR_NotAuthorized, "Authentication required");
                }
                request.setSession(_session);
                super.channelRead(ctx, msg);
                break;
        }
    } catch (XrootdException e) {
        ErrorResponse error = new ErrorResponse<>(request, e.getError(), e.getMessage());
        ctx.writeAndFlush(error);
    } catch (RuntimeException e) {
        _log.error("xrootd server error while processing " + msg + " (please report this to support@dcache.org)", e);
        ErrorResponse error = new ErrorResponse<>(request, kXR_ServerError, String.format("Internal server error (%s)", e.getMessage()));
        ctx.writeAndFlush(error);
    }
}
Also used : XrootdRequest(org.dcache.xrootd.protocol.messages.XrootdRequest) LoginRequest(org.dcache.xrootd.protocol.messages.LoginRequest) ErrorResponse(org.dcache.xrootd.protocol.messages.ErrorResponse)

Example 2 with ErrorResponse

use of org.dcache.xrootd.protocol.messages.ErrorResponse in project xrootd4j by dCache.

the class XrootdSigverDecoder method decode.

@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) {
    int length = verifyMessageLength(in);
    if (length < 0) {
        ctx.channel().close();
        return;
    }
    if (length == 0) {
        return;
    }
    ByteBuf frame = in.readSlice(length);
    XrootdRequest request = getRequest(frame);
    try {
        if (request instanceof SigverRequest) {
            setSigver((SigverRequest) request);
            /*
                 *  No need to pass it downstream.
                 */
            return;
        }
        int requestId = request.getRequestId();
        if (signingPolicy.requiresSigning(request)) {
            verifySignedHash(request.getStreamId(), requestId, frame, ctx);
        }
    } catch (XrootdException e) {
        ErrorResponse<?> response = new ErrorResponse<>(request, e.getError(), Strings.nullToEmpty(e.getMessage()));
        ctx.writeAndFlush(response).addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE);
        return;
    }
    out.add(request);
}
Also used : XrootdRequest(org.dcache.xrootd.protocol.messages.XrootdRequest) ByteBuf(io.netty.buffer.ByteBuf) SigverRequest(org.dcache.xrootd.protocol.messages.SigverRequest) ErrorResponse(org.dcache.xrootd.protocol.messages.ErrorResponse)

Example 3 with ErrorResponse

use of org.dcache.xrootd.protocol.messages.ErrorResponse in project dcache by dCache.

the class TpcWriteDescriptor method handleStat.

public synchronized XrootdResponse<StatRequest> handleStat(StatRequest msg) throws XrootdException {
    if (client.getError() != null) {
        return new ErrorResponse<>(msg, client.getErrno(), client.getError());
    }
    int fd = msg.getFhandle();
    NettyMoverChannel channel = getChannel();
    FileStatus fileStatus;
    try {
        fileStatus = new FileStatus(fd, channel.size(), XrootdPoolRequestHandler.DEFAULT_FILESTATUS_FLAGS, channel.getFileAttributes().getModificationTime() / 1000);
    } catch (IOException e) {
        String error = String.format("Failed to get channel " + "info for %s: %s.", msg, e.toString());
        throw new XrootdException(kXR_IOError, error);
    }
    return new StatResponse(msg, fileStatus);
}
Also used : NettyMoverChannel(org.dcache.pool.movers.NettyTransferService.NettyMoverChannel) FileStatus(org.dcache.xrootd.util.FileStatus) StatResponse(org.dcache.xrootd.protocol.messages.StatResponse) IOException(java.io.IOException) XrootdException(org.dcache.xrootd.core.XrootdException) ErrorResponse(org.dcache.xrootd.protocol.messages.ErrorResponse)

Example 4 with ErrorResponse

use of org.dcache.xrootd.protocol.messages.ErrorResponse in project dcache by dCache.

the class AccessLogHandler method write.

@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
    if (msg instanceof XrootdResponse<?> && logger.isErrorEnabled()) {
        XrootdResponse<?> response = (XrootdResponse<?>) msg;
        XrootdRequest request = response.getRequest();
        NetLoggerBuilder.Level level;
        if (response instanceof ErrorResponse) {
            level = ERROR;
        } else if (request instanceof WriteRequest || request instanceof ReadRequest || request instanceof ReadVRequest) {
            level = DEBUG;
        } else {
            level = INFO;
        }
        if (level == ERROR || level == INFO && logger.isInfoEnabled() || level == DEBUG && logger.isDebugEnabled()) {
            NetLoggerBuilder log = new NetLoggerBuilder(level, "org.dcache.xrootd.request").omitNullValues();
            log.add("session", CDC.getSession());
            log.add("request", getRequestId(request));
            if (request instanceof PathRequest) {
                log.add("path", (Strings.emptyToNull(((PathRequest) request).getPath())));
                log.add("opaque", (Strings.emptyToNull(((PathRequest) request).getOpaque())));
                if (request instanceof OpenRequest) {
                    if (!((OpenRequest) request).isReadOnly()) {
                        int mode = ((OpenRequest) request).getUMask();
                        if (mode == 0) {
                            log.add("mode", "0");
                        } else {
                            log.add("mode", "0" + Integer.toOctalString(mode));
                        }
                    }
                    log.add("options", "0x" + Integer.toHexString(((OpenRequest) request).getOptions()));
                } else if (request instanceof LocateRequest) {
                    log.add("options", "0x" + Integer.toHexString(((LocateRequest) request).getOptions()));
                } else if (request instanceof MkDirRequest) {
                    log.add("options", "0x" + Integer.toHexString(((MkDirRequest) request).getOptions()));
                } else if (request instanceof StatRequest) {
                    if (((StatRequest) request).getTarget() == Target.FHANDLE) {
                        log.add("handle", ((StatRequest) request).getFhandle());
                    }
                    log.add("vfs", ((StatRequest) request).isVfsSet());
                }
            } else if (request instanceof CloseRequest) {
                log.add("handle", ((CloseRequest) request).getFileHandle());
            } else if (request instanceof LoginRequest) {
                log.add("username", ((LoginRequest) request).getUserName());
                log.add("capver", ((LoginRequest) request).getClientProtocolVersion());
                log.add("pid", ((LoginRequest) request).getPID());
                log.add("token", emptyToNull(((LoginRequest) request).getToken()));
            } else if (request instanceof MvRequest) {
                log.add("source", ((MvRequest) request).getSourcePath());
                log.add("target", ((MvRequest) request).getTargetPath());
            } else if (request instanceof PrepareRequest) {
                log.add("options", "0x" + Integer.toHexString(((PrepareRequest) request).getOptions()));
                if (((PrepareRequest) request).getPathList().length == 1) {
                    log.add("path", ((PrepareRequest) request).getPathList()[0]);
                } else {
                    log.add("files", ((PrepareRequest) request).getPathList().length);
                }
            } else if (request instanceof QueryRequest) {
                log.add("reqcode", getQueryReqCode(request));
                int fhandle = ((QueryRequest) request).getFhandle();
                if (fhandle != 0) {
                    log.add("fhandle", fhandle);
                }
                log.add("args", Strings.emptyToNull(((QueryRequest) request).getArgs()));
            } else if (request instanceof StatxRequest) {
                if (((StatxRequest) request).getPaths().length == 1) {
                    log.add("path", ((StatxRequest) request).getPaths()[0]);
                } else {
                    log.add("files", ((StatxRequest) request).getPaths().length);
                }
            } else if (request instanceof SetRequest) {
                final String APPID_PREFIX = "appid ";
                final int APPID_PREFIX_LENGTH = APPID_PREFIX.length();
                final int APPID_MSG_LENGTH = 80;
                String data = ((SetRequest) request).getData();
                if (data.startsWith(APPID_PREFIX)) {
                    log.add("appid", data.substring(APPID_PREFIX_LENGTH, Math.min(APPID_PREFIX_LENGTH + APPID_MSG_LENGTH, data.length())));
                }
            } else if (request instanceof EndSessionRequest) {
                log.add("sessionId", ((EndSessionRequest) request).getSessionId());
            } else if (request instanceof SyncRequest) {
                log.add("handle", ((SyncRequest) request).getFileHandle());
            }
            log.add("response", getStatusCode(response));
            if (response instanceof ErrorResponse) {
                log.add("error.code", getErrorCode((ErrorResponse) response));
                log.add("error.msg", ((ErrorResponse) response).getErrorMessage());
            } else if (response instanceof RedirectResponse) {
                log.add("host", ((RedirectResponse) response).getHost());
                log.add("port", ((RedirectResponse) response).getPort());
                log.add("token", emptyToNull(((RedirectResponse) response).getToken()));
            } else if (response instanceof StatResponse) {
                log.add("flags", ((StatResponse) response).getFlags());
                log.add("modtime", Instant.ofEpochSecond(((StatResponse) response).getModificationTime()));
                log.add("size", ((StatResponse) response).getSize());
            } else if (response instanceof LoginResponse) {
                log.add("sessionId", ((LoginResponse) response).getSessionId());
                log.add("sec", emptyToNull(((LoginResponse) response).getSec()));
            } else if (response instanceof OpenResponse) {
                log.add("handle", ((OpenResponse) response).getFileHandle());
                FileStatus fs = ((OpenResponse) response).getFileStatus();
                if (fs != null) {
                    log.add("flags", fs.getFlags());
                    log.add("modtime", Instant.ofEpochSecond(fs.getModificationTime()));
                    log.add("size", fs.getSize());
                }
            }
            log.toLogger(logger);
        }
    }
    ctx.write(msg, promise);
}
Also used : FileStatus(org.dcache.xrootd.util.FileStatus) PathRequest(org.dcache.xrootd.protocol.messages.PathRequest) MkDirRequest(org.dcache.xrootd.protocol.messages.MkDirRequest) LoginRequest(org.dcache.xrootd.protocol.messages.LoginRequest) StatxRequest(org.dcache.xrootd.protocol.messages.StatxRequest) StatRequest(org.dcache.xrootd.protocol.messages.StatRequest) XrootdResponse(org.dcache.xrootd.protocol.messages.XrootdResponse) ReadVRequest(org.dcache.xrootd.protocol.messages.ReadVRequest) EndSessionRequest(org.dcache.xrootd.protocol.messages.EndSessionRequest) OpenResponse(org.dcache.xrootd.protocol.messages.OpenResponse) ReadRequest(org.dcache.xrootd.protocol.messages.ReadRequest) PrepareRequest(org.dcache.xrootd.protocol.messages.PrepareRequest) MvRequest(org.dcache.xrootd.protocol.messages.MvRequest) XrootdRequest(org.dcache.xrootd.protocol.messages.XrootdRequest) LoginResponse(org.dcache.xrootd.protocol.messages.LoginResponse) QueryRequest(org.dcache.xrootd.protocol.messages.QueryRequest) WriteRequest(org.dcache.xrootd.protocol.messages.WriteRequest) RedirectResponse(org.dcache.xrootd.protocol.messages.RedirectResponse) XrootdProtocol.kXR_chkpoint(org.dcache.xrootd.protocol.XrootdProtocol.kXR_chkpoint) NetLoggerBuilder(org.dcache.util.NetLoggerBuilder) ErrorResponse(org.dcache.xrootd.protocol.messages.ErrorResponse) LocateRequest(org.dcache.xrootd.protocol.messages.LocateRequest) SetRequest(org.dcache.xrootd.protocol.messages.SetRequest) SyncRequest(org.dcache.xrootd.protocol.messages.SyncRequest) StatResponse(org.dcache.xrootd.protocol.messages.StatResponse) OpenRequest(org.dcache.xrootd.protocol.messages.OpenRequest) CloseRequest(org.dcache.xrootd.protocol.messages.CloseRequest)

Aggregations

ErrorResponse (org.dcache.xrootd.protocol.messages.ErrorResponse)4 XrootdRequest (org.dcache.xrootd.protocol.messages.XrootdRequest)3 LoginRequest (org.dcache.xrootd.protocol.messages.LoginRequest)2 StatResponse (org.dcache.xrootd.protocol.messages.StatResponse)2 FileStatus (org.dcache.xrootd.util.FileStatus)2 ByteBuf (io.netty.buffer.ByteBuf)1 IOException (java.io.IOException)1 NettyMoverChannel (org.dcache.pool.movers.NettyTransferService.NettyMoverChannel)1 NetLoggerBuilder (org.dcache.util.NetLoggerBuilder)1 XrootdException (org.dcache.xrootd.core.XrootdException)1 XrootdProtocol.kXR_chkpoint (org.dcache.xrootd.protocol.XrootdProtocol.kXR_chkpoint)1 CloseRequest (org.dcache.xrootd.protocol.messages.CloseRequest)1 EndSessionRequest (org.dcache.xrootd.protocol.messages.EndSessionRequest)1 LocateRequest (org.dcache.xrootd.protocol.messages.LocateRequest)1 LoginResponse (org.dcache.xrootd.protocol.messages.LoginResponse)1 MkDirRequest (org.dcache.xrootd.protocol.messages.MkDirRequest)1 MvRequest (org.dcache.xrootd.protocol.messages.MvRequest)1 OpenRequest (org.dcache.xrootd.protocol.messages.OpenRequest)1 OpenResponse (org.dcache.xrootd.protocol.messages.OpenResponse)1 PathRequest (org.dcache.xrootd.protocol.messages.PathRequest)1