com.hierynomus.smbj.auth.AuthenticationContext Java Examples

The following examples show how to use com.hierynomus.smbj.auth.AuthenticationContext. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Samba2FileSystem.java    From iaf with Apache License 2.0 9 votes vote down vote up
@Override
public void open() throws FileSystemException {
	try {
		AuthenticationContext auth = authenticate();
		client = new SMBClient();
		connection = client.connect(domain);
		if(connection.isConnected()) {
			log.debug("successfully created connection to ["+connection.getRemoteHostname()+"]");
		}
		session = connection.authenticate(auth);
		if(session == null) {
			throw new FileSystemException("Cannot create session for user ["+username+"] on domain ["+domain+"]");
		}
		diskShare = (DiskShare) session.connectShare(shareName);
		if(diskShare == null) {
			throw new FileSystemException("Cannot connect to the share ["+ shareName +"]");
		}
	} catch (IOException e) {
		throw new FileSystemException("Cannot connect to samba server", e);
	}
}
 
Example #2
Source File: SMBJController.java    From DanDanPlayForAndroid with MIT License 5 votes vote down vote up
@Override
public boolean linkStart(SmbLinkInfo smbLinkInfo, SmbLinkException exception) {
    String rootFolder = smbLinkInfo.getRootFolder();
    if (SmbUtils.isTextEmpty(rootFolder)) {
        exception.addException(SmbType.SMBJ, "Root Folder Must Not Empty");
        return false;
    }

    SmbConfig smbConfig = SmbConfig.builder()
            .withTimeout(180, TimeUnit.SECONDS)
            .withSoTimeout(180, TimeUnit.SECONDS)
            .build();

    try {
        smbClient = new SMBClient(smbConfig);
        connection = smbClient.connect(smbLinkInfo.getIP());
        AuthenticationContext ac = new AuthenticationContext(
                smbLinkInfo.getAccount(), smbLinkInfo.getPassword().toCharArray(), smbLinkInfo.getDomain());
        session = connection.authenticate(ac);

        ROOT_FLAG += smbLinkInfo.getRootFolder();
        mPath = ROOT_FLAG;

        diskShare = (DiskShare) session.connectShare(smbLinkInfo.getRootFolder());
        rootFileList = getFileInfoList(diskShare.list(""), diskShare);

        return true;
    } catch (Exception e) {
        e.printStackTrace();
        exception.addException(SmbType.SMBJ, e.getMessage());
    }
    return false;
}
 
Example #3
Source File: SMBJ_RPCController.java    From DanDanPlayForAndroid with MIT License 5 votes vote down vote up
@Override
public boolean linkStart(SmbLinkInfo smbLinkInfo, SmbLinkException exception) {

    //set smb config
    SmbConfig smbConfig = SmbConfig.builder()
            .withTimeout(180, TimeUnit.SECONDS)
            .withSoTimeout(180, TimeUnit.SECONDS)
            .build();

    try {
        smbClient = new SMBClient(smbConfig);
        smbConnection = smbClient.connect(smbLinkInfo.getIP());
        AuthenticationContext authContext = new AuthenticationContext(
                smbLinkInfo.getAccount(), smbLinkInfo.getPassword().toCharArray(), smbLinkInfo.getDomain());
        session = smbConnection.authenticate(authContext);

        RPCTransport transport = SMBTransportFactories.SRVSVC.getTransport(session);
        ServerService serverService = new ServerService(transport);
        List<NetShareInfo0> shareInfoList = serverService.getShares0();

        mPath = ROOT_FLAG;

        //get root directory file list
        rootFileList = getFileInfoList(shareInfoList);

        return true;
    } catch (Exception e) {
        e.printStackTrace();
        exception.addException(SmbType.SMBJ_RPC, e.getMessage());
    }
    return false;
}
 
Example #4
Source File: PooledSMB2Connection.java    From CloverETL-Engine with GNU Lesser General Public License v2.1 5 votes vote down vote up
private Session startSession() throws IOException {
	String userInfoString = authority.getUserInfo();
	String[] userInfo = userInfoString.split(":");
	String username = URIUtils.urlDecode(userInfo[0]);
	String password = URIUtils.urlDecode(userInfo[1]);
	String domain = null;
	if (username.contains(";")) {
		String[] user = username.split(";");
		domain = user[0];
		username = user[1];
	}
	
	AuthenticationContext authContext = new AuthenticationContext(username, password.toCharArray(), domain);
	return connection.authenticate(authContext);
}
 
Example #5
Source File: PooledSMB2Connection.java    From CloverETL-Engine with GNU Lesser General Public License v2.1 4 votes vote down vote up
/**
 * Used in tests.
 * 
 * @return the domain passed to SMBJ connection
 */
String getDomain() {
	DiskShare share = getShare();
	AuthenticationContext authenticationContext = share.getTreeConnect().getSession().getAuthenticationContext();
	return authenticationContext.getDomain();
}
 
Example #6
Source File: Samba2FileSystem.java    From iaf with Apache License 2.0 4 votes vote down vote up
private AuthenticationContext authenticate() throws FileSystemException {
	CredentialFactory credentialFactory = new CredentialFactory(getAuthAlias(), getUsername(), getPassword());
	if (StringUtils.isNotEmpty(credentialFactory.getUsername())) {
		if(StringUtils.equalsIgnoreCase(authType, "NTLM")) {
			return new AuthenticationContext(getUsername(), password.toCharArray(), getDomain());
		}else if(StringUtils.equalsIgnoreCase(authType, "SPNEGO")) {

			if(!StringUtils.isEmpty(getKdc()) && !StringUtils.isEmpty(getRealm())) {
				System.setProperty("java.security.krb5.kdc", getKdc());
				System.setProperty("java.security.krb5.realm", getRealm());
			}

			HashMap<String, String> loginParams = new HashMap<String, String>();
			loginParams.put("principal", getUsername());
			LoginContext lc;
			try {
				lc = new LoginContext(getUsername(), null, 
						new UsernameAndPasswordCallbackHandler(getUsername(), getPassword()),
						new KerberosLoginConfiguration(loginParams));
				lc.login();

				Subject subject = lc.getSubject();
				KerberosPrincipal krbPrincipal = subject.getPrincipals(KerberosPrincipal.class).iterator().next();

				Oid spnego = new Oid(SPNEGO_OID);
				Oid kerberos5 = new Oid(KERBEROS5_OID);

				final GSSManager manager = GSSManager.getInstance();

				final GSSName name = manager.createName(krbPrincipal.toString(), GSSName.NT_USER_NAME);
				Set<Oid> mechs = new HashSet<Oid>(Arrays.asList(manager.getMechsForName(name.getStringNameType())));
				final Oid mech;

				if (mechs.contains(kerberos5)) {
					mech = kerberos5;
				} else if (mechs.contains(spnego)) {
					mech = spnego;
				} else {
					throw new IllegalArgumentException("No mechanism found");
				}

				GSSCredential creds = Subject.doAs(subject, new PrivilegedExceptionAction<GSSCredential>() {
					@Override
					public GSSCredential run() throws GSSException {
						return manager.createCredential(name, GSSCredential.DEFAULT_LIFETIME, mech, GSSCredential.INITIATE_ONLY);
					}
				});

				GSSAuthenticationContext auth = new GSSAuthenticationContext(krbPrincipal.getName(), krbPrincipal.getRealm(), subject, creds);
				return auth;

			} catch (Exception e) {
				if(e.getMessage().contains("Cannot locate default realm")) {
					throw new FileSystemException("Please fill the kdc and realm field or provide krb5.conf file including realm",e);
				}
				throw new FileSystemException(e);
			}
		}
	}
	return null;
}