@Override
  public User getUser(Renderer renderer, String username, String password) throws IOException {
    if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password))
      throw new AuthException("Username or password is empty");
    if (StringUtils.isEmpty(renderer.getAuthServer()))
      throw new AuthException("No auth server given, check the parameters of the renderer");

    ActiveDirectory activeDirectory = null;
    try {
      String domain = renderer.getAuthDomain();
      String authServer = renderer.getAuthServer();

      User user = AuthUserCache.INSTANCE.get(username, domain);
      if (user != null) return user;

      NtlmPasswordAuthentication ntlmAuth = getNtlmAuth(renderer, username, password);
      UniAddress dc = UniAddress.getByName(authServer, true);
      SmbSession.logon(dc, ntlmAuth);

      activeDirectory =
          new ActiveDirectory(authServer, ntlmAuth.getUsername(), ntlmAuth.getPassword(), domain);

      NamingEnumeration<SearchResult> result = activeDirectory.findUser(username);
      Attributes attrs = ActiveDirectory.getAttributes(result);
      if (attrs == null) throw new AuthException("No user found: " + username);

      String userId = ActiveDirectory.getObjectSID(attrs);
      List<ADGroup> groups = new ArrayList<ADGroup>();
      activeDirectory.findUserGroups(attrs, groups);
      String dnUser = ActiveDirectory.getStringAttribute(attrs, "DistinguishedName");
      if (!StringUtils.isEmpty(dnUser)) activeDirectory.findUserGroup(dnUser, groups);

      Logging.info("USER authenticated: " + user);

      user =
          new User(
              userId.toLowerCase(),
              username.toLowerCase(),
              password,
              ActiveDirectory.toArray(groups, "everyone"),
              ActiveDirectory.getDisplayString(domain, username));
      AuthUserCache.INSTANCE.add(username, domain, user);
      return user;

    } catch (SmbAuthException e) {
      Logging.warn(e);
      throw new AuthException("Authentication error (SmbAuthException) : " + e.getMessage());
    } catch (UnknownHostException e) {
      Logging.warn(e);
      throw new AuthException("Authentication error (UnknownHostException) : " + e.getMessage());
    } catch (NamingException e) {
      Logging.warn(e);
      throw new AuthException("LDAP error (NamingException) : " + e.getMessage());
    } finally {
      IOUtils.close(activeDirectory);
    }
  }
Ejemplo n.º 2
0
  public boolean login(String path, String domain, String userName, String userPwd) {
    boolean success = false;
    try {
      SambaManager.login(path, domain, userName, userPwd);
      success = true;
    } catch (SmbAuthException e) {
      MtkLog.w(TAG, "Login SmbAuthException!!");
      e.printStackTrace();
    } catch (MalformedURLException e) {
      e.printStackTrace();
    } catch (SmbException e) {
      e.printStackTrace();
    }

    return success;
  }
Ejemplo n.º 3
0
  @Override
  public List<FileAdapter> listAllFiles(String path) {
    MtkLog.d(TAG, "------List All login Files : " + path);

    try {
      mFiles.clear();
      int type = SambaManager.TYPE_ALL;

      switch (mContentType) {
        case CONTENT_PHOTO:
          type = SambaManager.TYPE_IMAGE;
          break;
        case CONTENT_AUDIO:
          type = SambaManager.TYPE_AUDIO;
          break;
        case CONTENT_VIDEO:
          type = SambaManager.TYPE_VIDEO;
          break;
        case CONTENT_TEXT:
          type = SambaManager.TYPE_TEXT;
          break;
        case CONTENT_THRDPHOTO:
          type = SambaManager.TYPE_THRIMAGE;
          break;
        default:
          break;
      }

      LinkedList<String> filePaths = mOperator.getSmbFileList(path, type);
      filePaths = mOperator.sortByName(filePaths);

      mFiles = wrapFiles(filePaths);
      logFiles(TAG);

    } catch (SmbAuthException e) {
      e.printStackTrace();
      if (localLOGV) MtkLog.d(TAG, "Try to connect to login : "******"Try to connect to login : ?  GUEST  '' ");
          login(path, "?", "GUEST", "");
          mLoginCount++;
          listAllFiles(path);
        } else if (mLoginCount == 1) {
          if (localLOGV) MtkLog.d(TAG, "Try to connect to login : null null null");
          login(path, null, null, null);
          mLoginCount++;
          listAllFiles(path);
        } else if (mLoginCount == 2) {

          if (localLOGV) MtkLog.d(TAG, " --- User name and Password is not correct. ");
          mLoginCount = 0;
          setChanged();
          notifyObservers(REQUEST_LOGIN);
        }
      } else {
        setChanged();
        notifyObservers(REQUEST_LOGIN);
      }
    } catch (MalformedURLException e) {
      e.printStackTrace();
    } catch (SmbException e) {
      e.printStackTrace();
    } catch (UnknownHostException e) {
      e.printStackTrace();
    }
    System.setProperty("java.util.Arrays.useLegacyMergeSort", "true");
    sortFile();
    return mFiles;
  }