📄 consoletcp.java
字号:
LogService sLog = (LogService) bc.getService(srLog); if (sLog != null) { sLog.log(level, msg); } bc.ungetService(srLog); } } catch (IllegalStateException exp) { // if the thread has survied the stop of the bundle we get this // which we unfortunately has to ignore } } // ******************** AcceptThread ******************** /** * * Waits for tcp connetions * */ class AcceptThread extends Thread implements SessionListener { Hashtable sessions = new Hashtable(); ConsoleService cs; ThreadGroup tg; AcceptThread(ConsoleService cs) { super(new ThreadGroup("Console TCP"), "Console TCP accept thread"); this.cs = cs; } public void quit() { quit = true; log(LogService.LOG_INFO, "Stop listening for connections"); for (Enumeration e = sessions.keys(); e.hasMoreElements();) { ((Session) e.nextElement()).close(); } if (tg != null) { Thread[] threads = new Thread[tg.activeCount()]; tg.enumerate(threads); for (int i = 0; i < threads.length; i++) threads[i].interrupt(); // BUG workaround! Connect so that socket is closed properly try { (new Socket("localhost", port.intValue())).close(); } catch (IOException ignore) { } try { join(); } catch (InterruptedException ignore) { } } } public void run() { ServerSocket serverSock = null; tg = getThreadGroup(); try { log(LogService.LOG_INFO, "Listening for connections on port " + port); serverSock = new ServerSocket(port.intValue(), 50, address); final SessionListener thisSessionListener = this; // used in // thread // below while (!quit) { // Listen for incoming requests: final Socket client = serverSock.accept(); if (quit) { client.close(); break; } // Create new Thread to be able to handle simultaneous // logins: Thread startSessionThread = new Thread() { public void run() { String session = "console session " + client.getInetAddress().getHostName(); PrintWriter out = null; try { out = new PrintWriter(client.getOutputStream(), true); InputStream is = client.getInputStream(); InputStreamReader isr = new InputStreamReader( is); BufferedReader in = new BufferedReader(isr); Authorization authorization = null; if (login.booleanValue()) { LoginResult loginResult = doLogin(client); if (loginResult.loginOK) { authorization = loginResult.authorization; } else { return; } } // To avoid bug with hanging sockets we set // SO_TIMEOUT // addition: to avoid a bug in j9 it is now // possible to // configure the console to not use the timeout if (useTimeout.booleanValue()) { client.setSoTimeout(3000); } Session s = cs.runSession(session, in, out); Dictionary props = s.getProperties(); if (authorization != null) { props.put(Session.PROPERTY_AUTHORIZATION, authorization); } props.put(Session.PROPERTY_TCP, new Boolean( true)); if (exitOnLogout.booleanValue()) { props.put(Session.PROPERTY_EXIT_ON_LOGOUT, new Boolean(true)); /* * Add properties to session to be able to * exit on login? if (requiredGroup != null) { * props.put(Session.PROPERTY_REQUIRED_GROUP, * requiredGroup); } if (forbiddenGroup != * null) { * props.put(Session.PROPERTY_FORBIDDEN_GROUP, * forbiddenGroup); } */ } sessions.put(s, client); s.addSessionListener(thisSessionListener); s.setInterruptString(TELNET_INTERRUPT); log(LogService.LOG_INFO, "Started " + session); } catch (IOException e) { log(LogService.LOG_ERROR, "Failed to get input or output from socket to console session: " + e); try { if (out != null && client.getSoTimeout() > 0) { out.println("\nSession timed out (" + client.getSoTimeout() + " ms)."); } } catch (SocketException ignore) { } try { client.close(); } catch (IOException e2) { log(LogService.LOG_ERROR, "Failed to close socket: " + e); } } } }; startSessionThread.start(); } // while (!quit) } catch (Exception e) { if (!quit) { log(LogService.LOG_ERROR, "Communication error in ConsoleTcp: " + e); } } if (serverSock != null) { try { serverSock.close(); } catch (IOException ignore) { } } } // SessionListener public void sessionEnd(Session s) { try { ((Socket) sessions.remove(s)).close(); } catch (IOException ignore) { } } LoginResult doLogin(Socket client) throws IOException { PrintWriter out = new PrintWriter(client.getOutputStream(), true); InputStream is = client.getInputStream(); InputStreamReader isr = new InputStreamReader(is); BufferedReader in = new BufferedReader(isr); // To avoid bug with hanging sockets we set SO_TIMEOUT // 60 secs to login // addition: to avoid a bug in j9 it is now possible to configure // the console to not use the timeout if (useTimeout.booleanValue()) { client.setSoTimeout(60000); } out.print("login: "); out.flush(); String userName = in.readLine(); out.print("password: "); // Turn off echo: out.print((char) 255); // IAC out.print((char) 251); // WILL out.print((char) 1); // TELOPT_ECHO out.print((char) 0); out.flush(); in.read(); in.read(); in.read(); // Read answer. // Read password: String password = in.readLine(); // Turn echo back on: out.print((char) 255); // IAC out.print((char) 252); // WONT out.print((char) 1); // TELOPT_ECHO out.print((char) 0); out.flush(); in.read(); in.read(); in.read(); // Read answer. out.println(); Authorization authorization = null; boolean loginOK = false; ServiceReference sr = bc .getServiceReference(PasswdAuthenticator.class.getName()); if (sr == null) { if (requireUM.booleanValue()) { log(LogService.LOG_WARNING, "Failed to get PasswdAuthenticator reference. UM required but not present."); } else { loginOK = (DEFAULT_USER_NAME.equals(userName) && DEFAULT_PASSWORD .equals(password)); } } else { PasswdAuthenticator pa = (PasswdAuthenticator) bc .getService(sr); if (pa != null) { PasswdSession ps = pa.createSession(); ps.setUsername(userName); ps.setPassword(password); ContextualAuthorization ca = null; try { ca = ps.getAuthorization(); } catch (IllegalStateException ex) { log(LogService.LOG_WARNING, "UserAdmin service not available."); } if (ca != null) { if (requiredGroup != null && !ca.hasRole(requiredGroup)) { loginOK = false; log( LogService.LOG_INFO, userName + " tried to login, but did not have required role " + requiredGroup); } else if (forbiddenGroup != null && ca.hasRole(forbiddenGroup)) { loginOK = false; log( LogService.LOG_INFO, userName + " tried to login, but had forbidden role " + forbiddenGroup); } else { authorization = ca; loginOK = true; out.println("Logged in."); } } } else { log(LogService.LOG_WARNING, "Failed to get PasswdAuthenticator service."); } bc.ungetService(sr); } // Check if login successful: if (!loginOK) { out.println("Login failed!"); client.close(); log(LogService.LOG_INFO, "Login failed for " + userName); return new LoginResult(); } // Set context: if (authorization instanceof ContextualAuthorization) { String authMethod = "passwd"; // TBD String inputPath = "tcp"; // TBD ((ContextualAuthorization) authorization).setIPAMContext( inputPath, authMethod); Dictionary context = ((ContextualAuthorization) authorization) .getContext(); log(LogService.LOG_INFO, "User " + authorization.getName() + " logged in, authentication context is " + context + "."); } else if (authorization == null) { log(LogService.LOG_INFO, "Default user " + DEFAULT_USER_NAME + " logged in."); } else { log(LogService.LOG_INFO, "User " + authorization.getName() + " logged in."); } return new LoginResult(authorization); } class LoginResult { Authorization authorization; boolean loginOK; LoginResult() { loginOK = false; } LoginResult(Authorization authorization) { this.authorization = authorization; loginOK = true; } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -