📄 telnetsession.java
字号:
public void execDONT(int code) { String response = execCommand(TCC.DONT, code, null); try { telnetOutputStream.writeCommand(response); } catch (IOException ex) { log.error("Command DONT exception " + ex.toString()); } } /** * * DO * * * * @parameter code, the optional command code */ public void execDO(int code) { String response = execCommand(TCC.DO, code, null); try { telnetOutputStream.writeCommand(response); } catch (IOException ex) { log.error("Command DO exception " + ex.toString()); } } /** * * WONT * * * * @parameter code, the optional command code */ public void execWONT(int code) { String response = execCommand(TCC.WONT, code, null); try { telnetOutputStream.writeCommand(response); } catch (IOException ex) { log.error("Command WONT exception " + ex.toString()); } } /** * * WILL * * * * @parameter code, the optional command code */ public void execWILL(int code) { String response = execCommand(TCC.WILL, code, null); try { telnetOutputStream.writeCommand(response); } catch (IOException ex) { log.error("Command WILL exception " + ex.toString()); } } /** * * On SE command, execute optional sub negotiated command. * * * * @parameter code, the optional command code * * @parameter param, the optional parameters */ public void execSE(int code, byte[] param) { String response = execCommand(TCC.SE, code, param); try { telnetOutputStream.writeCommand(response); } catch (IOException ex) { log.error("Command SE exception " + ex.toString()); } } // Individual command execution code /** * * Execute optional sub command. * * In the case that there is no support * for the option, * the WILL and DO will be responded with DONT and WONT * * respectively, to inform the requestor that the * option is not supported. * * * * @parameter action, the negotiation code * * @parameter code, the optional command code * * @parameter params, the optional parameters */ private String execCommand(int action, int code, byte[] params) { String response = null; TelnetCommand tc = telnetCommands[code]; if (tc != null) { response = tc.execute(action, code, params); } else { // Refuse unknown options if (action == TCC.WILL) { response = TCC.IAC_string + TCC.DONT_string + String.valueOf((char) code); } if (action == TCC.DO) { response = TCC.IAC_string + TCC.WONT_string + String.valueOf((char) code); } } return response; } /** * * A login procedure (primitive and ugly) * * * * @parameter in Telnetreader * * @parameter out Printwriter * * @return a TelnetLogin object with the result of the login process. * */ private TelnetLogin login(TelnetReader in, PrintWriter out, TelnetOutputStream tos) { String userName = null; String password = null; TelnetCommand echo = telnetCommands[TCC.ECHO]; try { // System.out.println("TelnetLogin.login()"); out.print("login: "); out.flush(); // 1. Convince the client to do remote echo // and make sure the server does echo tos.writeCommand(echo.getWILL()); tos.writeCommand(echo.getDO()); if (echo.getDoStatus() == false) { echo.setDoStatus(true); } enableEcho = true; userName = in.readLine(); out.print("password: "); out.flush(); // 2. Convince the client to do remote echo // but cheat when the server is to do remote echo enableEcho = false; // out.println(); password = in.readLine(); out.println(); // 3a. Enable the echo in the server again enableEcho = true; /* * Login processing depends on: * * 1. um is required in the configuration and exist in the system => * um is used 2. um is required in the configuration but does NOT * exist in the system => login is denied 3. um is not required in * configuration => use default user admin, default passwd admin */ boolean requireUM = telnetConfig.umRequired(); String requiredGroup = telnetConfig.getRequiredGroup(); String forbiddenGroup = telnetConfig.getForbiddenGroup(); ServiceReference sr = bc .getServiceReference(PasswdAuthenticator.class.getName()); TelnetLogin telnetLogin = new TelnetLogin(false, null, userName); // Only one of the following if cases (1,2 or 3) is executed // 1. um required in configuration and exists in the system if (requireUM && sr != null) { // System.out.println("require UM = true, sr != null"); PasswdAuthenticator pa = (PasswdAuthenticator) bc .getService(sr); if (pa == null) { log.warn("Failed to get PasswdAuthenticator service."); telnetLogin = new TelnetLogin(false, null, userName); } else { PasswdSession ps = pa.createSession(); ps.setUsername(userName); ps.setPassword(password); ContextualAuthorization ca = null; try { ca = ps.getAuthorization(); } catch (IllegalStateException ex) { log.warn("Failed to get UserAdmin service."); } if (ca != null) { log.info("ContextualAuthorization used."); if (!"".equals(requiredGroup) && !ca.hasRole(requiredGroup)) { telnetLogin = new TelnetLogin(false, null, userName); log.info("User " + ca.getName() + " has not required group " + requiredGroup); } else if (!"".equals(forbiddenGroup) && ca.hasRole(forbiddenGroup)) { telnetLogin = new TelnetLogin(false, null, userName); log.info("User " + ca.getName() + " is in forbidden group " + forbiddenGroup); } else { telnetLogin = new TelnetLogin(true, ca, userName); } } } bc.ungetService(sr); // Set context if (telnetLogin.getAuthorization() instanceof ContextualAuthorization) { String inputPath = telnetConfig.getInputPath(); String authMethod = telnetConfig.getAuthorizationMethod(); ((ContextualAuthorization) telnetLogin.getAuthorization()) .setIPAMContext(inputPath, authMethod); Dictionary context = ((ContextualAuthorization) telnetLogin .getAuthorization()).getContext(); log.info("User " + telnetLogin.getAuthorization().getName() + " logged in, authentication context is " + context + "."); } else if (telnetLogin.getAuthorization() == null) { log.info("Default user " + telnetConfig.getDefaultUser() + " not logged in."); } else { log.info("User " + telnetLogin.getAuthorization().getName() + " not logged in."); } } // 2. um required in configuration but does NOT exists in the system // => // login is always denied if (requireUM && sr == null) { log .warn("User management required but not available, login denied"); telnetLogin = new TelnetLogin(false, null, userName); } // 3. um is not required in configuration => // use default user and password if (requireUM == false) { if (telnetConfig.getDefaultUser().equals(userName) && telnetConfig.getDefaultPassword().equals(password)) { telnetLogin = new TelnetLogin(true, null, userName); } else { telnetLogin = new TelnetLogin(false, null, userName); } } return telnetLogin; } catch (Exception e) { log.error("Login error", e); } log.error("failed to login"); return new TelnetLogin(false, null, "noone"); } /** * * Initial option setup * * * * @parameter in, * * @parameter out, * */ private void initialNegotiation(TelnetReader in, PrintWriter out, TelnetOutputStream tos) { try { Thread.sleep(20); } catch (Exception ex) { log.error("Fail during Thread sleep" + ex.toString()); } // Offer all telnet options that should be shown. for (int i = 0; i < telnetCommands.length; i++) { TelnetCommand tc = telnetCommands[i]; if (tc != null && tc.getShow()) { try { tos.writeCommand(tc.getWILL()); } catch (IOException ex) { log.error("Fail during initial option negotiation" + ex.toString()); } } } }} // TelnetSession
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -