/** * initialize the APR Library by loading the associated native libraries * and creating the associated singleton */ private static synchronized void initialize() { if (library == null) { library = new AprLibrary(); } }
static String createLocalSocketAddress() throws IOException { initialize(); String name; if (OsUtils.isUNIX()) { // Since there is a race condition between bind and when // we can mark the socket readable only by its owner, make // the socket in a temporary directory that is visible only // to the owner. // File dir = File.createTempFile("mina", "apr"); if (!dir.delete() || !dir.mkdir()) { throw new IOException("Cannot create secure temp directory"); } chmodOwner(dir.getAbsolutePath(), true); File socket = File.createTempFile("mina", "apr", dir); socket.delete(); name = socket.getAbsolutePath(); } else { File socket = File.createTempFile("mina", "apr"); socket.delete(); name = "\\\\.\\pipe\\" + socket.getName(); } return name; }
/** * get the shared instance of APR library, if none, initialize one * * @return the current APR library singleton */ public static synchronized AprLibrary getInstance() { if (!isInitialized()) { initialize(); } return library; }
public String start() throws Exception { authSocket = AprLibrary.createLocalSocketAddress(); pool = Pool.create(AprLibrary.getInstance().getRootPool()); handle = Local.create(authSocket, pool); int result = Local.bind(handle, 0); if (result != Status.APR_SUCCESS) { throwException(result); } AprLibrary.secureLocalSocket(authSocket, handle); result = Local.listen(handle, 0); if (result != Status.APR_SUCCESS) { throwException(result); } CloseableExecutorService executor = getExecutorService(); agentThread = executor.submit(() -> { try { while (true) { long clientSock = Local.accept(handle); Socket.timeoutSet(clientSock, 10000000); // TODO make this configurable new SshAgentSession(clientSock, agent).run(); } } catch (Exception e) { log.error("Failed to run session", e); } }); return authSocket; }
public AgentClient(String authSocket, CloseableExecutorService executor) throws IOException { super((executor == null) ? ThreadUtils.newSingleThreadExecutor("AgentClient[" + authSocket + "]") : executor); this.authSocket = authSocket; try { pool = Pool.create(AprLibrary.getInstance().getRootPool()); handle = Local.create(authSocket, pool); int result = Local.connect(handle, 0); if (result != Status.APR_SUCCESS) { throwException(result); } receiveBuffer = new ByteArrayBuffer(); messages = new ArrayBlockingQueue<>(10); CloseableExecutorService service = getExecutorService(); pumper = service.submit(this); } catch (IOException e) { throw e; } catch (Exception e) { throw new SshException(e); } }
public static boolean isPreferredUnixAgent(PropertyResolver resolver) { if (PropertyResolverUtils.getBooleanProperty(resolver, PREFER_UNIX_AGENT, OsUtils.isUNIX())) { try { if (AprLibrary.getInstance() != null) { return true; } } catch (Exception ignore) { // ignored } } return false; } }
static void secureLocalSocket(String authSocket, long handle) throws IOException { // should be ok on windows if (OsUtils.isUNIX()) { chmodOwner(authSocket, false); } }
protected void signalEOS(AprLibrary libInstance, boolean debugEnabled) throws Exception { long tmpPool = Pool.create(libInstance.getRootPool()); long tmpSocket = Local.create(authSocket, tmpPool); long connectResult = Local.connect(tmpSocket, 0L); if (connectResult != Status.APR_SUCCESS) { if (debugEnabled) { log.debug("Unable to connect to socket PIPE {}. APR errcode {}", authSocket, connectResult); } } // write a single byte -- just wake up the accept() int sendResult = Socket.send(tmpSocket, END_OF_STREAM_MESSAGE, 0, 1); if (sendResult != 1) { if (debugEnabled) { log.debug("Unable to send signal the EOS for {}. APR retcode {} != 1", authSocket, sendResult); } } }
public String start() throws Exception { authSocket = AprLibrary.createLocalSocketAddress(); pool = Pool.create(AprLibrary.getInstance().getRootPool()); handle = Local.create(authSocket, pool); int result = Local.bind(handle, 0); if (result != Status.APR_SUCCESS) { throwException(result); } AprLibrary.secureLocalSocket(authSocket, handle); result = Local.listen(handle, 0); if (result != Status.APR_SUCCESS) { throwException(result); } CloseableExecutorService executor = getExecutorService(); agentThread = executor.submit(() -> { try { while (true) { long clientSock = Local.accept(handle); Socket.timeoutSet(clientSock, 10000000); // TODO make this configurable new SshAgentSession(clientSock, agent).run(); } } catch (Exception e) { log.error("Failed to run session", e); } }); return authSocket; }
public AgentClient(String authSocket, CloseableExecutorService executor) throws IOException { super((executor == null) ? ThreadUtils.newSingleThreadExecutor("AgentClient[" + authSocket + "]") : executor); this.authSocket = authSocket; try { pool = Pool.create(AprLibrary.getInstance().getRootPool()); handle = Local.create(authSocket, pool); int result = Local.connect(handle, 0); if (result != Status.APR_SUCCESS) { throwException(result); } receiveBuffer = new ByteArrayBuffer(); messages = new ArrayBlockingQueue<>(10); CloseableExecutorService service = getExecutorService(); pumper = service.submit(this); } catch (IOException e) { throw e; } catch (Exception e) { throw new SshException(e); } }
public static boolean isPreferredUnixAgent(PropertyResolver resolver) { if (PropertyResolverUtils.getBooleanProperty(resolver, PREFER_UNIX_AGENT, OsUtils.isUNIX())) { try { if (AprLibrary.getInstance() != null) { return true; } } catch (Exception ignore) { // ignored } } return false; } }
static void secureLocalSocket(String authSocket, long handle) throws IOException { // should be ok on windows if (OsUtils.isUNIX()) { chmodOwner(authSocket, false); } }
protected void signalEOS(AprLibrary libInstance, boolean debugEnabled) throws Exception { long tmpPool = Pool.create(libInstance.getRootPool()); long tmpSocket = Local.create(authSocket, tmpPool); long connectResult = Local.connect(tmpSocket, 0L); if (connectResult != Status.APR_SUCCESS) { if (debugEnabled) { log.debug("Unable to connect to socket PIPE {}. APR errcode {}", authSocket, connectResult); } } // write a single byte -- just wake up the accept() int sendResult = Socket.send(tmpSocket, END_OF_STREAM_MESSAGE, 0, 1); if (sendResult != 1) { if (debugEnabled) { log.debug("Unable to send signal the EOS for {}. APR retcode {} != 1", authSocket, sendResult); } } }
public AgentServerProxy(ConnectionService service, CloseableExecutorService executor) throws IOException { this.service = service; try { String authSocket = AprLibrary.createLocalSocketAddress(); pool = Pool.create(AprLibrary.getInstance().getRootPool()); handle = Local.create(authSocket, pool); this.authSocket = authSocket; throw toIOException(result); AprLibrary.secureLocalSocket(authSocket, handle); result = Local.listen(handle, 0); if (result != Status.APR_SUCCESS) {
out = new ChannelOutputStream(this, getRemoteWindow(), log, SshConstants.SSH_MSG_CHANNEL_DATA, true); authSocket = this.getString(SshAgent.SSH_AUTHSOCKET_ENV_NAME); pool = Pool.create(AprLibrary.getInstance().getRootPool()); handle = Local.create(authSocket, pool); int result = Local.connect(handle, 0);
/** * get the shared instance of APR library, if none, initialize one * * @return the current APR library singleton */ public static synchronized AprLibrary getInstance() { if (!isInitialized()) { initialize(); } return library; }
static String createLocalSocketAddress() throws IOException { initialize(); String name; if (OsUtils.isUNIX()) { // Since there is a race condition between bind and when // we can mark the socket readable only by its owner, make // the socket in a temporary directory that is visible only // to the owner. // File dir = File.createTempFile("mina", "apr"); if (!dir.delete() || !dir.mkdir()) { throw new IOException("Cannot create secure temp directory"); } chmodOwner(dir.getAbsolutePath(), true); File socket = File.createTempFile("mina", "apr", dir); socket.delete(); name = socket.getAbsolutePath(); } else { File socket = File.createTempFile("mina", "apr"); socket.delete(); name = "\\\\.\\pipe\\" + socket.getName(); } return name; }
if (!innerFinished.get()) { try { signalEOS(AprLibrary.getInstance(), debugEnabled); } catch (Exception e) {
/** * initialize the APR Library by loading the associated native libraries * and creating the associated singleton */ private static synchronized void initialize() { if (library == null) { library = new AprLibrary(); } }
public AgentServerProxy(ConnectionService service, CloseableExecutorService executor) throws IOException { this.service = service; try { String authSocket = AprLibrary.createLocalSocketAddress(); pool = Pool.create(AprLibrary.getInstance().getRootPool()); handle = Local.create(authSocket, pool); this.authSocket = authSocket; throw toIOException(result); AprLibrary.secureLocalSocket(authSocket, handle); result = Local.listen(handle, 0); if (result != Status.APR_SUCCESS) {