001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.security;
019
020import org.apache.hbase.thirdparty.io.netty.buffer.ByteBuf;
021import org.apache.hbase.thirdparty.io.netty.channel.ChannelHandlerContext;
022import org.apache.hbase.thirdparty.io.netty.handler.codec.ByteToMessageDecoder;
023
024import java.io.IOException;
025import java.util.List;
026
027import org.apache.hadoop.hbase.HConstants;
028import org.apache.yetus.audience.InterfaceAudience;
029import org.apache.hadoop.ipc.RemoteException;
030
031/**
032 * Decode the sasl challenge sent by RpcServer.
033 */
034@InterfaceAudience.Private
035public class SaslChallengeDecoder extends ByteToMessageDecoder {
036
037  private static final int MAX_CHALLENGE_SIZE = 1024 * 1024; // 1M
038
039  private ByteBuf tryDecodeChallenge(ByteBuf in, int offset, int readableBytes) throws IOException {
040    if (readableBytes < 4) {
041      return null;
042    }
043    int len = in.getInt(offset);
044    if (len <= 0) {
045      // fall back to simple
046      in.readerIndex(offset + 4);
047      return in.retainedSlice(offset, 4);
048    }
049    if (len > MAX_CHALLENGE_SIZE) {
050      throw new IOException(
051          "Sasl challenge too large(" + len + "), max allowed is " + MAX_CHALLENGE_SIZE);
052    }
053    int totalLen = 4 + len;
054    if (readableBytes < totalLen) {
055      return null;
056    }
057    in.readerIndex(offset + totalLen);
058    return in.retainedSlice(offset, totalLen);
059  }
060
061  // will throw a RemoteException out if data is enough, so do not need to return anything.
062  private void tryDecodeError(ByteBuf in, int offset, int readableBytes) throws IOException {
063    if (readableBytes < 4) {
064      return;
065    }
066    int classLen = in.getInt(offset);
067    if (classLen <= 0) {
068      throw new IOException("Invalid exception class name length " + classLen);
069    }
070    if (classLen > MAX_CHALLENGE_SIZE) {
071      throw new IOException("Exception class name length too large(" + classLen
072          + "), max allowed is " + MAX_CHALLENGE_SIZE);
073    }
074    if (readableBytes < 4 + classLen + 4) {
075      return;
076    }
077    int msgLen = in.getInt(offset + 4 + classLen);
078    if (msgLen <= 0) {
079      throw new IOException("Invalid exception message length " + msgLen);
080    }
081    if (msgLen > MAX_CHALLENGE_SIZE) {
082      throw new IOException("Exception message length too large(" + msgLen + "), max allowed is "
083          + MAX_CHALLENGE_SIZE);
084    }
085    int totalLen = classLen + msgLen + 8;
086    if (readableBytes < totalLen) {
087      return;
088    }
089    String className = in.toString(offset + 4, classLen, HConstants.UTF8_CHARSET);
090    String msg = in.toString(offset + classLen + 8, msgLen, HConstants.UTF8_CHARSET);
091    in.readerIndex(offset + totalLen);
092    throw new RemoteException(className, msg);
093  }
094
095  @Override
096  protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
097    int readableBytes = in.readableBytes();
098    if (readableBytes < 4) {
099      return;
100    }
101    int offset = in.readerIndex();
102    int status = in.getInt(offset);
103    if (status == SaslStatus.SUCCESS.state) {
104      ByteBuf challenge = tryDecodeChallenge(in, offset + 4, readableBytes - 4);
105      if (challenge != null) {
106        out.add(challenge);
107      }
108    } else {
109      tryDecodeError(in, offset + 4, readableBytes - 4);
110    }
111  }
112}