1 20 26 package org.apache.derby.impl.drda; 27 28 import java.io.ByteArrayInputStream ; 29 import java.io.IOException ; 30 import java.io.InputStream ; 31 import java.io.OutputStream ; 32 import java.io.IOException ; 33 import java.io.UnsupportedEncodingException ; 34 import java.math.BigDecimal ; 35 import java.sql.CallableStatement ; 36 import java.sql.Connection ; 37 import java.sql.Driver ; 38 import java.sql.PreparedStatement ; 39 import java.sql.ResultSet ; 40 import java.sql.ResultSetMetaData ; 41 import java.sql.SQLException ; 42 import java.sql.SQLWarning ; 43 import java.sql.Statement ; 44 import java.sql.Types ; 45 import java.util.ArrayList ; 46 import java.util.Date ; 47 import java.util.Properties ; 48 import java.util.Vector ; 49 50 import org.apache.derby.catalog.SystemProcedures; 51 import org.apache.derby.iapi.error.ExceptionSeverity; 52 import org.apache.derby.iapi.reference.Attribute; 53 import org.apache.derby.iapi.reference.DRDAConstants; 54 import org.apache.derby.iapi.reference.JDBC30Translation; 55 import org.apache.derby.iapi.reference.Property; 56 import org.apache.derby.iapi.reference.SQLState; 57 import org.apache.derby.iapi.services.info.JVMInfo; 58 import org.apache.derby.iapi.services.monitor.Monitor; 59 import org.apache.derby.iapi.services.sanity.SanityManager; 60 import org.apache.derby.iapi.services.stream.HeaderPrintWriter; 61 import org.apache.derby.iapi.tools.i18n.LocalizedResource; 62 import org.apache.derby.iapi.jdbc.AuthenticationService; 63 import org.apache.derby.iapi.jdbc.EngineParameterMetaData; 64 import org.apache.derby.impl.jdbc.EmbedSQLException; 65 import org.apache.derby.impl.jdbc.Util; 66 import org.apache.derby.jdbc.InternalDriver; 67 68 class DRDAConnThread extends Thread { 69 70 private static final String leftBrace = "{"; 71 private static final String rightBrace = "}"; 72 private static final byte NULL_VALUE = (byte)0xff; 73 private static final String SYNTAX_ERR = "42X01"; 74 75 private static final int MGRLVL_3 = 0x03; 77 78 private static final int MGRLVL_4 = 0x04; 80 81 private static final int MGRLVL_5 = 0x05; 83 84 private static final int MGRLVL_6 = 0x06; 86 87 private static final int MGRLVL_7 = 0x07; 89 90 91 private static final int COMMIT = 1; 93 private static final int ROLLBACK = 2; 94 95 96 protected CcsidManager ccsidManager = new EbcdicCcsidManager(); 97 private int correlationID; 98 private InputStream sockis; 99 private OutputStream sockos; 100 private DDMReader reader; 101 private DDMWriter writer; 102 private DRDAXAProtocol xaProto; 103 104 private static int [] ACCRDB_REQUIRED = {CodePoint.RDBACCCL, 105 CodePoint.CRRTKN, 106 CodePoint.PRDID, 107 CodePoint.TYPDEFNAM, 108 CodePoint.TYPDEFOVR}; 109 110 private static int MAX_REQUIRED_LEN = 5; 111 112 private int currentRequiredLength = 0; 113 private int [] required = new int[MAX_REQUIRED_LEN]; 114 115 116 private NetworkServerControlImpl server; private Session session; private long timeSlice; private Object timeSliceSync = new Object (); private boolean logConnections; 122 private boolean sendWarningsOnCNTQRY = false; private Object logConnectionsSync = new Object (); private boolean close; private Object closeSync = new Object (); private static HeaderPrintWriter logStream; 127 private AppRequester appRequester; private Database database; private int sqlamLevel; 132 private byte diagnosticLevel = (byte)0xF0; 134 135 private Vector unknownManagers; 137 private Vector knownManagers; 138 private Vector errorManagers; 139 private Vector errorManagersLevel; 140 141 private SQLException databaseAccessException; 143 144 147 private Pkgnamcsn prevPkgnamcsn = null; 148 149 private DRDAString rdbnam = new DRDAString(ccsidManager); 150 151 private DRDAString rdbcolid = new DRDAString(ccsidManager); 152 153 private DRDAString pkgid = new DRDAString(ccsidManager); 154 155 private DRDAString pkgcnstkn = new DRDAString(ccsidManager); 156 157 private int pkgsn; 158 159 private final static String TIMEOUT_STATEMENT = "SET STATEMENT_TIMEOUT "; 160 161 private int pendingStatementTimeout; 163 166 private static DecryptionManager decryptionManager; 168 169 private byte[] myPublicKey; 172 173 private byte[] myTargetSeed; 176 177 private static final byte[] eod00000 = { '0', '0', '0', '0', '0' }; 181 private static final byte[] eod02000 = { '0', '2', '0', '0', '0' }; 182 private static final byte[] nullSQLState = { ' ', ' ', ' ', ' ', ' ' }; 183 private static final byte[] errD4_D6 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; private static final byte[] warn0_warnA = { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' }; 186 private final static String AUTHENTICATION_PROVIDER_BUILTIN_CLASS = 187 "org.apache.derby.impl.jdbc.authentication.BasicAuthenticationServiceImpl"; 188 189 private final static String AUTHENTICATION_PROVIDER_NONE_CLASS = 190 "org.apache.derby.impl.jdbc.authentication.NoneAuthenticationServiceImpl"; 191 192 private static final DRDAProtocolExceptionInfo dummy = 203 new DRDAProtocolExceptionInfo(0,0,0,false); 204 205 214 215 DRDAConnThread(Session session, NetworkServerControlImpl server, 216 long timeSlice, 217 boolean logConnections) { 218 219 super(); 220 221 NetworkServerControlImpl.setUniqueThreadName(this, "DRDAConnThread"); 224 225 this.session = session; 226 this.server = server; 227 this.timeSlice = timeSlice; 228 this.logConnections = logConnections; 229 this.pendingStatementTimeout = -1; 230 initialize(); 231 } 232 233 237 public void run() { 238 if (SanityManager.DEBUG) 239 trace("Starting new connection thread"); 240 241 Session prevSession; 242 while(!closed()) 243 { 244 245 prevSession = session; 247 session = server.getNextSession(session); 248 if (session == null) 249 close(); 250 251 if (closed()) 252 break; 253 if (session != prevSession) 254 { 255 initializeForSession(); 256 } 257 try { 258 long timeStart = System.currentTimeMillis(); 259 260 switch (session.state) 261 { 262 case Session.INIT: 263 sessionInitialState(); 264 if (session == null) 265 break; 266 case Session.ATTEXC: 268 case Session.SECACC: 269 case Session.CHKSEC: 270 long currentTimeSlice; 271 272 do { 273 processCommands(); 274 currentTimeSlice = getTimeSlice(); 275 } while ((currentTimeSlice == 0) || 276 (System.currentTimeMillis() - timeStart < currentTimeSlice)); 277 278 break; 279 default: 280 agentError("Session in invalid state:" + session.state); 282 } 283 } catch (Exception e) { 284 if (e instanceof DRDAProtocolException && 285 ((DRDAProtocolException)e).isDisconnectException()) 286 { 287 closeSession(); 289 } 290 else 291 { 292 handleException(e); 293 } 294 } 295 } 296 if (SanityManager.DEBUG) 297 trace("Ending connection thread"); 298 server.removeThread(this); 299 300 } 301 306 protected InputStream getInputStream() 307 { 308 return sockis; 309 } 310 311 316 protected OutputStream getOutputStream() 317 { 318 return sockos; 319 } 320 321 325 protected DDMReader getReader() 326 { 327 return reader; 328 } 329 330 334 protected DDMWriter getWriter() 335 { 336 return writer; 337 } 338 339 344 protected int getCorrelationID () 345 { 346 return correlationID; 347 } 348 349 354 protected Session getSession() 355 { 356 return session; 357 } 358 359 364 protected Database getDatabase() 365 { 366 return database; 367 } 368 373 protected NetworkServerControlImpl getServer() 374 { 375 return server; 376 } 377 382 protected byte[] getCrrtkn() 383 { 384 if (database != null) 385 return database.crrtkn; 386 return null; 387 } 388 393 protected String getDbName() 394 { 395 if (database != null) 396 return database.dbName; 397 return null; 398 } 399 402 protected void close() 403 { 404 synchronized (closeSync) 405 { 406 close = true; 407 } 408 } 409 410 415 protected void setLogConnections(boolean value) 416 { 417 synchronized(logConnectionsSync) { 418 logConnections = value; 419 } 420 } 421 426 protected void setTimeSlice(long value) 427 { 428 synchronized(timeSliceSync) { 429 timeSlice = value; 430 } 431 } 432 442 protected void markCommunicationsFailure(String arg1, String arg2, String arg3, 443 String arg4) throws DRDAProtocolException 444 { 445 Object [] oa = {arg1,arg2,arg3,arg4}; 446 throw DRDAProtocolException.newDisconnectException(this,oa); 447 448 } 449 456 457 protected void throwSyntaxrm(int errcd, int cpArg) 458 throws DRDAProtocolException 459 { 460 throw new 461 DRDAProtocolException(DRDAProtocolException.DRDA_Proto_SYNTAXRM, 462 this, 463 cpArg, 464 errcd); 465 } 466 473 protected void agentError(String msg) throws DRDAProtocolException 474 { 475 476 String dbname = null; 477 if (database != null) 478 dbname = database.dbName; 479 throw DRDAProtocolException.newAgentError(this, CodePoint.SVRCOD_PRMDMG, 480 dbname, msg); 481 } 482 488 protected void missingCodePoint(int codePoint) throws DRDAProtocolException 489 { 490 throwSyntaxrm(CodePoint.SYNERRCD_REQ_OBJ_NOT_FOUND, codePoint); 491 } 492 499 protected static void println2Log(String dbname, String drdaID, String msg) 500 { 501 if (logStream == null) 502 logStream = Monitor.getStream(); 503 504 if (dbname != null) 505 { 506 int endOfName = dbname.indexOf(';'); 507 if (endOfName != -1) 508 dbname = dbname.substring(0, endOfName); 509 } 510 logStream.printlnWithHeader("(DATABASE = " + dbname + "), (DRDAID = " + drdaID + "), " + msg); 511 } 512 518 protected void writeRDBNAM(String rdbnam) 519 throws DRDAProtocolException 520 { 521 int len = rdbnam.length(); 522 if (len < CodePoint.RDBNAM_LEN) 523 len = CodePoint.RDBNAM_LEN; 524 writer.writeScalarHeader(CodePoint.RDBNAM, len); 525 try { 526 writer.writeScalarPaddedBytes(rdbnam.getBytes(server.DEFAULT_ENCODING), 527 len, server.SPACE_CHAR); 528 } 529 catch (UnsupportedEncodingException e) 530 { 531 agentError("Unsupported coding exception for server encoding " 532 + server.DEFAULT_ENCODING); 533 } 534 } 535 538 539 542 private void initialize() 543 { 544 sockis = session.sessionInput; 547 sockos = session.sessionOutput; 548 549 reader = new DDMReader(this, session.dssTrace); 550 writer = new DDMWriter(ccsidManager, this, session.dssTrace); 551 } 552 553 556 private void initializeForSession() 557 { 558 sockis = session.sessionInput; 560 sockos = session.sessionOutput; 561 562 reader.initialize(this, session.dssTrace); 564 writer.reset(session.dssTrace); 565 566 database = session.database; 568 appRequester = session.appRequester; 569 570 if (session.state == Session.ATTEXC) 572 sqlamLevel = appRequester.getManagerLevel(CodePoint.SQLAM); 573 574 } 575 584 private void sessionInitialState() 585 throws Exception 586 { 587 if (reader.isCmd()) 590 { 591 try { 592 server.processCommands(reader, writer, session); 593 reader.initialize(this, null); 595 writer.reset(null); 596 closeSession(); 597 } catch (Throwable t) { 598 if (t instanceof InterruptedException ) 599 throw (InterruptedException )t; 600 else 601 { 602 server.consoleExceptionPrintTrace(t); 603 } 604 } 605 606 } 607 else 608 { 609 exchangeServerAttributes(); 611 } 612 } 613 614 625 private void cleanUpAndCloseResultSet(DRDAStatement stmt, 626 SQLException sqle, 627 int writerMark) 628 throws DRDAProtocolException 629 { 630 if (stmt != null) { 631 writer.clearDSSesBackToMark(writerMark); 632 if (!stmt.rsIsClosed()) { 633 try { 634 stmt.rsClose(); 635 } catch (SQLException ec) { 636 if (SanityManager.DEBUG) { 637 trace("Warning: Error closing result set"); 638 } 639 } 640 writeABNUOWRM(); 641 writeSQLCARD(sqle, CodePoint.SVRCOD_ERROR, 0, 0); 642 } 643 } else { 644 writeSQLCARDs(sqle, 0); 645 } 646 errorInChain(sqle); 647 } 648 649 655 private void processCommands() throws DRDAProtocolException 656 { 657 DRDAStatement stmt = null; 658 int updateCount = 0; 659 boolean PRPSQLSTTfailed = false; 660 boolean checkSecurityCodepoint = session.requiresSecurityCodepoint(); 661 do 662 { 663 correlationID = reader.readDssHeader(); 664 int codePoint = reader.readLengthAndCodePoint(); 665 int writerMark = writer.markDSSClearPoint(); 666 667 if (checkSecurityCodepoint) 668 verifyInOrderACCSEC_SECCHK(codePoint,session.getRequiredSecurityCodepoint()); 669 670 switch(codePoint) 671 { 672 case CodePoint.CNTQRY: 673 try{ 674 stmt = parseCNTQRY(); 675 if (stmt != null) 676 { 677 writeQRYDTA(stmt); 678 if (stmt.rsIsClosed()) 679 { 680 writeENDQRYRM(CodePoint.SVRCOD_WARNING); 681 writeNullSQLCARDobject(); 682 } 683 checkWarning(null, null, stmt.getResultSet(), 0, false, sendWarningsOnCNTQRY); 685 } 686 } 687 catch(SQLException e) 688 { 689 cleanUpAndCloseResultSet(stmt, e, writerMark); 692 } 693 break; 694 case CodePoint.EXCSQLIMM: 695 try { 696 updateCount = parseEXCSQLIMM(); 697 if (database.RDBUPDRM_sent == false) 708 { 709 writeRDBUPDRM(); 710 } 711 712 checkWarning(null, database.getDefaultStatement().getStatement(), 714 null, updateCount, true, true); 715 } catch (SQLException e) 716 { 717 writer.clearDSSesBackToMark(writerMark); 718 writeSQLCARDs(e, 0); 719 errorInChain(e); 720 } 721 break; 722 723 case CodePoint.EXCSQLSET: 724 try { 725 if (parseEXCSQLSET()) 726 writeSQLCARDs(null,0); 728 } 729 catch (SQLWarning w) 730 { 731 writeSQLCARD(w, CodePoint.SVRCOD_WARNING, 0, 0); 732 } 733 catch (SQLException e) 734 { 735 writer.clearDSSesBackToMark(writerMark); 736 writeSQLCARDs(e, 0); 737 errorInChain(e); 738 } 739 break; 740 741 case CodePoint.PRPSQLSTT: 742 int sqldaType; 743 PRPSQLSTTfailed = false; 744 try { 745 database.getConnection().clearWarnings(); 746 sqldaType = parsePRPSQLSTT(); 747 if (sqldaType > 0) writeSQLDARD(database.getCurrentStatement(), 749 (sqldaType == CodePoint.TYPSQLDA_LIGHT_OUTPUT), 750 database.getConnection().getWarnings()); 751 else 752 checkWarning(database.getConnection(), null, null, 0, true, true); 753 754 } catch (SQLException e) 755 { 756 writer.clearDSSesBackToMark(writerMark); 757 writeSQLCARDs(e, 0, true); 758 PRPSQLSTTfailed = true; 759 errorInChain(e); 760 } 761 break; 762 case CodePoint.OPNQRY: 763 PreparedStatement ps = null; 764 try { 765 if (PRPSQLSTTfailed) { 766 skipRemainder(true); 770 771 writeOPNQFLRM(null); 774 break; 775 } 776 Pkgnamcsn pkgnamcsn = parseOPNQRY(); 777 if (pkgnamcsn != null) 778 { 779 stmt = database.getDRDAStatement(pkgnamcsn); 780 ps = stmt.getPreparedStatement(); 781 ps.clearWarnings(); 782 if (pendingStatementTimeout >= 0) { 783 ps.setQueryTimeout(pendingStatementTimeout); 784 pendingStatementTimeout = -1; 785 } 786 stmt.execute(); 787 writeOPNQRYRM(false, stmt); 788 checkWarning(null, ps, null, 0, false, true); 789 790 writeQRYDSC(stmt, false); 791 792 stmt.rsSuspend(); 793 794 if (stmt.getQryprctyp() == CodePoint.LMTBLKPRC && 795 stmt.getQryrowset() != 0) { 796 DRDAResultSet drdars = 800 stmt.getCurrentDrdaResultSet(); 801 try { 802 if (drdars != null && 803 !drdars.hasLobColumns()) { 804 writeQRYDTA(stmt); 805 } 806 } catch (SQLException sqle) { 807 cleanUpAndCloseResultSet(stmt, sqle, 808 writerMark); 809 } 810 } 811 } 812 } 813 catch (SQLException e) 814 { 815 writer.clearDSSesBackToMark(writerMark); 816 writeOPNQFLRM(e); 820 } 821 break; 822 case CodePoint.RDBCMM: 823 try 824 { 825 if (SanityManager.DEBUG) 826 trace("Received commit"); 827 if (!database.getConnection().getAutoCommit()) 828 { 829 database.getConnection().clearWarnings(); 830 database.commit(); 831 writeENDUOWRM(COMMIT); 832 checkWarning(database.getConnection(), null, null, 0, true, true); 833 } 834 database.RDBUPDRM_sent = false; 837 } 838 catch (SQLException e) 839 { 840 writer.clearDSSesBackToMark(writerMark); 841 writeENDUOWRM(COMMIT); 843 writeSQLCARDs(e, 0); 844 errorInChain(e); 845 } 846 break; 847 case CodePoint.RDBRLLBCK: 848 try 849 { 850 if (SanityManager.DEBUG) 851 trace("Received rollback"); 852 database.getConnection().clearWarnings(); 853 database.rollback(); 854 writeENDUOWRM(ROLLBACK); 855 checkWarning(database.getConnection(), null, null, 0, true, true); 856 database.RDBUPDRM_sent = false; 859 } 860 catch (SQLException e) 861 { 862 writer.clearDSSesBackToMark(writerMark); 863 writeENDUOWRM(ROLLBACK); 865 writeSQLCARDs(e, 0); 866 errorInChain(e); 867 } 868 break; 869 case CodePoint.CLSQRY: 870 try{ 871 stmt = parseCLSQRY(); 872 stmt.rsClose(); 873 writeSQLCARDs(null, 0); 874 } 875 catch (SQLException e) 876 { 877 writer.clearDSSesBackToMark(writerMark); 878 writeSQLCARDs(e, 0); 879 errorInChain(e); 880 } 881 break; 882 case CodePoint.EXCSAT: 883 parseEXCSAT(); 884 writeEXCSATRD(); 885 break; 886 case CodePoint.ACCSEC: 887 int securityCheckCode = parseACCSEC(); 888 writeACCSECRD(securityCheckCode); 889 checkSecurityCodepoint = true; 890 break; 891 case CodePoint.SECCHK: 892 if(parseDRDAConnection()) 893 checkSecurityCodepoint = false; 895 break; 896 899 case CodePoint.BGNBND: 900 reader.skipBytes(); 901 writeSQLCARDs(null, 0); 902 break; 903 case CodePoint.BNDSQLSTT: 904 reader.skipBytes(); 905 parseSQLSTTDss(); 906 writeSQLCARDs(null, 0); 907 break; 908 case CodePoint.SQLSTTVRB: 909 reader.skipBytes(); 911 break; 912 case CodePoint.ENDBND: 913 reader.skipBytes(); 914 writeSQLCARDs(null, 0); 915 break; 916 case CodePoint.DSCSQLSTT: 917 if (PRPSQLSTTfailed) { 918 reader.skipBytes(); 919 writeSQLCARDs(null, 0); 920 break; 921 } 922 try { 923 boolean rtnOutput = parseDSCSQLSTT(); 924 writeSQLDARD(database.getCurrentStatement(), rtnOutput, 925 null); 926 927 } catch (SQLException e) 928 { 929 writer.clearDSSesBackToMark(writerMark); 930 server.consoleExceptionPrint(e); 931 try { 932 writeSQLDARD(database.getCurrentStatement(), true, e); 933 } catch (SQLException e2) { agentError("Why am I getting another SQLException?"); 935 } 936 errorInChain(e); 937 } 938 break; 939 case CodePoint.EXCSQLSTT: 940 if (PRPSQLSTTfailed) { 941 skipRemainder(true); 943 writeSQLCARDs(null, 0); 944 break; 945 } 946 try { 947 parseEXCSQLSTT(); 948 949 DRDAStatement curStmt = database.getCurrentStatement(); 950 if (curStmt != null) 951 curStmt.rsSuspend(); 952 } catch (SQLException e) 953 { 954 skipRemainder(true); 955 writer.clearDSSesBackToMark(writerMark); 956 if (SanityManager.DEBUG) 957 { 958 server.consoleExceptionPrint(e); 959 } 960 writeSQLCARDs(e, 0); 961 errorInChain(e); 962 } 963 break; 964 case CodePoint.SYNCCTL: 965 if (xaProto == null) 966 xaProto = new DRDAXAProtocol(this); 967 xaProto.parseSYNCCTL(); 968 break; 969 default: 970 codePointNotSupported(codePoint); 971 } 972 973 finalizeChain(); 978 979 } 980 while (reader.isChainedWithSameID() || reader.isChainedWithDiffID()); 981 } 982 983 998 private void errorInChain(SQLException e) throws DRDAProtocolException 999 { 1000 if (reader.terminateChainOnErr() && (getExceptionSeverity(e) > CodePoint.SVRCOD_ERROR)) 1001 { 1002 if (SanityManager.DEBUG) trace("terminating the chain on error..."); 1003 skipRemainder(false); 1004 } 1005 } 1006 1007 1012 private void exchangeServerAttributes() 1013 throws DRDAProtocolException 1014 { 1015 int codePoint; 1016 correlationID = reader.readDssHeader(); 1017 if (SanityManager.DEBUG) { 1018 if (correlationID == 0) 1019 { 1020 SanityManager.THROWASSERT( 1021 "Unexpected value for correlationId = " + correlationID); 1022 } 1023 } 1024 1025 codePoint = reader.readLengthAndCodePoint(); 1026 1027 if (codePoint != CodePoint.EXCSAT) 1029 { 1030 throw 1032 new DRDAProtocolException(DRDAProtocolException.DRDA_Proto_PRCCNVRM, 1033 this, codePoint, 1034 CodePoint.PRCCNVCD_EXCSAT_FIRST_AFTER_CONN); 1035 } 1036 1037 parseEXCSAT(); 1038 writeEXCSATRD(); 1039 finalizeChain(); 1040 session.setState(session.ATTEXC); 1041 } 1042 1043 1044 private boolean parseDRDAConnection() throws DRDAProtocolException 1045 { 1046 int codePoint; 1047 boolean sessionOK = true; 1048 1049 1050 int securityCheckCode = parseSECCHK(); 1051 if (SanityManager.DEBUG) 1052 trace("*** SECCHKRM securityCheckCode is: "+securityCheckCode); 1053 writeSECCHKRM(securityCheckCode); 1054 if (securityCheckCode != 0) 1056 { 1057 return false; 1058 } 1059 1060 correlationID = reader.readDssHeader(); 1061 codePoint = reader.readLengthAndCodePoint(); 1062 verifyRequiredObject(codePoint,CodePoint.ACCRDB); 1063 int svrcod = parseACCRDB(); 1064 1065 if(database.getConnection() == null && databaseAccessException == null){ 1068 writeRDBfailure(CodePoint.RDBAFLRM); 1069 return false; 1070 } 1071 1072 if (databaseAccessException != null) 1074 { 1075 1076 int failureType = getRdbAccessErrorCodePoint(); 1078 if (failureType == CodePoint.RDBNFNRM 1079 || failureType == CodePoint.RDBATHRM) 1080 { 1081 writeRDBfailure(failureType); 1082 } 1083 else 1084 { 1085 writeRDBfailure(CodePoint.RDBAFLRM); 1086 } 1087 return false; 1088 } 1089 else if (database.accessCount > 1 ) { 1091 writeRDBfailure(CodePoint.RDBACCRM); 1092 return false; 1093 } 1094 else writeACCRDBRM(svrcod); 1096 1097 session.appRequester = server.getAppRequester(appRequester); 1101 return sessionOK; 1102 } 1103 1104 1114 private void writeRDBfailure(int codePoint) throws DRDAProtocolException 1115 { 1116 writer.createDssReply(); 1117 writer.startDdm(codePoint); 1118 writer.writeScalar2Bytes(CodePoint.SVRCOD, CodePoint.SVRCOD_ERROR); 1119 writeRDBNAM(database.dbName); 1120 writer.endDdmAndDss(); 1121 1122 switch(codePoint){ 1123 case CodePoint.RDBAFLRM: 1124 writer.createDssObject(); 1126 writer.writeScalarString(CodePoint.TYPDEFNAM, 1127 CodePoint.TYPDEFNAM_QTDSQLASC); 1128 writeTYPDEFOVR(); 1129 writer.endDss(); 1130 case CodePoint.RDBNFNRM: 1131 case CodePoint.RDBATHRM: 1132 writeSQLCARD(databaseAccessException,CodePoint.SVRCOD_ERROR,0,0); 1133 case CodePoint.RDBACCRM: 1134 skipRemainder(false); 1136 1137 finalizeChain(); 1140 break; 1141 } 1142 1143 } 1144 1145 1153 1154 private int getRdbAccessErrorCodePoint() 1155 { 1156 String sqlState = databaseAccessException.getSQLState(); 1157 if (sqlState.regionMatches(0,SQLState.DATABASE_NOT_FOUND,0,5) | 1158 sqlState.regionMatches(0,SQLState.NO_SUCH_DATABASE,0,5)) 1159 return CodePoint.RDBNFNRM; 1160 else 1161 if (sqlState.regionMatches(0,SQLState.LOGIN_FAILED,0,5) || 1162 sqlState.regionMatches(0,SQLState.AUTH_INVALID_USER_NAME,0,5)) 1163 return CodePoint.RDBATHRM; 1164 else 1165 return CodePoint.RDBAFLRM; 1166 } 1167 1168 1169 1178 private int verifyUserIdPassword() throws DRDAProtocolException 1179 { 1180 databaseAccessException = null; 1181 int retSecChkCode = 0; 1182 1183 String realName = database.dbName; int endOfName = realName.indexOf(';'); 1185 if (endOfName != -1) 1186 realName = realName.substring(0, endOfName); 1187 retSecChkCode = getConnFromDatabaseName(); 1188 return retSecChkCode; 1189 } 1190 1191 1200 private int getConnFromDatabaseName() throws DRDAProtocolException 1201 { 1202 Properties p = new Properties (); 1203 databaseAccessException = null; 1204 if (session.drdaID == null) 1206 session.drdaID = leftBrace + session.connNum + rightBrace; 1207 p.put(Attribute.DRDAID_ATTR, session.drdaID); 1208 1209 if ((database.securityMechanism == CodePoint.SECMEC_USRSSBPWD) && 1224 (database.dbName.indexOf(Attribute.PASSWORD_ATTR) == -1)) 1225 { 1226 p.put(Attribute.CLIENT_SECURITY_MECHANISM, 1227 String.valueOf(database.securityMechanism)); 1228 p.put(Attribute.DRDA_SECTKN_IN, 1229 DecryptionManager.toHexString(database.secTokenIn, 0, 1230 database.secTokenIn.length)); 1231 p.put(Attribute.DRDA_SECTKN_OUT, 1232 DecryptionManager.toHexString(database.secTokenOut, 0, 1233 database.secTokenOut.length)); 1234 } 1235 1236 try { 1237 database.makeConnection(p); 1238 } catch (SQLException se) { 1239 String sqlState = se.getSQLState(); 1240 databaseAccessException = se; 1244 for (; se != null; se = se.getNextException()) 1245 { 1246 if (SanityManager.DEBUG) 1247 trace(se.getMessage()); 1248 println2Log(database.dbName, session.drdaID, se.getMessage()); 1249 } 1250 1251 if (sqlState.regionMatches(0,SQLState.LOGIN_FAILED,0,5)) 1252 return CodePoint.SECCHKCD_USERIDINVALID; 1253 1254 return 0; 1255 } 1256 catch (Exception e) 1257 { 1258 println2Log(database.dbName, session.drdaID, 1264 "Driver not loaded" 1265 + e.getMessage()); 1266 try { 1267 agentError("Driver not loaded"); 1268 } 1269 catch (DRDAProtocolException dpe) 1270 { 1271 try { 1275 server.startNetworkServer(); 1276 } catch (Exception re) { 1277 println2Log(database.dbName, session.drdaID, "Failed attempt to reload driver " +re.getMessage() ); 1278 } 1279 throw dpe; 1280 } 1281 } 1282 1283 1284 if (getLogConnections()) 1286 println2Log(database.dbName, session.drdaID, 1287 "Apache Derby Network Server connected to database " + 1288 database.dbName); 1289 return 0; 1290 } 1291 1292 1293 1305 private void parseEXCSAT() throws DRDAProtocolException 1306 { 1307 int codePoint; 1308 String strVal; 1309 1310 1323 if (appRequester != null) 1326 { 1327 parseEXCSAT2(); 1328 return; 1329 } 1330 1331 1334 appRequester = new AppRequester(); 1335 1336 reader.markCollection(); 1337 1338 codePoint = reader.getCodePoint(); 1339 while (codePoint != -1) 1340 { 1341 switch (codePoint) 1342 { 1343 case CodePoint.EXTNAM: 1345 appRequester.extnam = reader.readString(); 1346 if (SanityManager.DEBUG) 1347 trace("extName = " + appRequester.extnam); 1348 if (appRequester.extnam.length() > CodePoint.MAX_NAME) 1349 tooBig(CodePoint.EXTNAM); 1350 break; 1351 case CodePoint.MGRLVLLS: 1353 parseMGRLVLLS(1); 1354 break; 1355 case CodePoint.SPVNAM: 1357 appRequester.spvnam = reader.readString(); 1358 if (appRequester.spvnam != null) 1361 badObjectLength(CodePoint.SPVNAM); 1362 break; 1363 case CodePoint.SRVNAM: 1365 appRequester.srvnam = reader.readString(); 1366 if (SanityManager.DEBUG) 1367 trace("serverName = " + appRequester.srvnam); 1368 if (appRequester.srvnam.length() > CodePoint.MAX_NAME) 1369 tooBig(CodePoint.SRVNAM); 1370 break; 1371 case CodePoint.SRVRLSLV: 1373 appRequester.srvrlslv = reader.readString(); 1374 if (SanityManager.DEBUG) 1375 trace("serverlslv = " + appRequester.srvrlslv); 1376 if (appRequester.srvrlslv.length() > CodePoint.MAX_NAME) 1377 tooBig(CodePoint.SRVRLSLV); 1378 break; 1379 case CodePoint.SRVCLSNM: 1381 appRequester.srvclsnm = reader.readString(); 1382 if (SanityManager.DEBUG) 1383 trace("serverClassName = " + appRequester.srvclsnm); 1384 if (appRequester.srvclsnm.length() > CodePoint.MAX_NAME) 1385 tooBig(CodePoint.SRVCLSNM); 1386 break; 1387 default: 1388 invalidCodePoint(codePoint); 1389 } 1390 codePoint = reader.getCodePoint(); 1391 } 1392 } 1393 1394 1411 private void parseEXCSAT2() throws DRDAProtocolException 1412 { 1413 int codePoint; 1414 reader.markCollection(); 1415 1416 codePoint = reader.getCodePoint(); 1417 while (codePoint != -1) 1418 { 1419 switch (codePoint) 1420 { 1421 case CodePoint.EXTNAM: 1423 case CodePoint.SRVNAM: 1424 case CodePoint.SRVRLSLV: 1425 case CodePoint.SRVCLSNM: 1426 case CodePoint.SPVNAM: 1427 reader.skipBytes(); 1428 break; 1429 case CodePoint.MGRLVLLS: 1431 parseMGRLVLLS(2); 1432 break; 1433 default: 1434 invalidCodePoint(codePoint); 1435 } 1436 codePoint = reader.getCodePoint(); 1437 } 1438 } 1439 1440 1464 private void parseMGRLVLLS(int time) throws DRDAProtocolException 1465 { 1466 int manager, managerLevel; 1467 int currentLevel; 1468 unknownManagers = new Vector (); 1470 knownManagers = new Vector (); 1471 errorManagers = new Vector (); 1472 errorManagersLevel = new Vector (); 1473 if (SanityManager.DEBUG) 1474 trace("Manager Levels"); 1475 1476 while (reader.moreDdmData()) 1477 { 1478 manager = reader.readNetworkShort(); 1479 managerLevel = reader.readNetworkShort(); 1480 if (CodePoint.isKnownManager(manager)) 1481 { 1482 knownManagers.addElement(new Integer (manager)); 1483 currentLevel = appRequester.getManagerLevel(manager); 1485 if (currentLevel == appRequester.MGR_LEVEL_UNKNOWN) 1486 appRequester.setManagerLevel(manager, managerLevel); 1487 else 1488 { 1489 if (currentLevel != managerLevel) 1491 { 1492 errorManagers.addElement(new Integer (manager)); 1494 errorManagersLevel.addElement(new Integer (managerLevel)); 1495 } 1496 } 1497 1498 } 1499 else 1500 unknownManagers.addElement(new Integer (manager)); 1501 if (SanityManager.DEBUG) 1502 trace("Manager = " + java.lang.Integer.toHexString(manager) + 1503 " ManagerLevel " + managerLevel); 1504 } 1505 sqlamLevel = appRequester.getManagerLevel(CodePoint.SQLAM); 1506 if (errorManagers.size() > 0) 1508 { 1509 Object [] oa = new Object [errorManagers.size()*2]; 1510 int j = 0; 1511 for (int i = 0; i < errorManagers.size(); i++) 1512 { 1513 oa[j++] = errorManagers.elementAt(i); 1514 oa[j++] = errorManagersLevel.elementAt(i); 1515 } 1516 throw new DRDAProtocolException(DRDAProtocolException.DRDA_Proto_MGRLVLRM, 1517 this, 0, 1518 0, oa); 1519 } 1520 } 1521 1532 private void writeEXCSATRD() throws DRDAProtocolException 1533 { 1534 writer.createDssReply(); 1535 writer.startDdm(CodePoint.EXCSATRD); 1536 writer.writeScalarString(CodePoint.EXTNAM, server.att_extnam); 1537 if (knownManagers != null && knownManagers.size() > 0) 1539 writeMGRLEVELS(); 1540 writer.writeScalarString(CodePoint.SRVCLSNM, server.att_srvclsnm); 1541 writer.writeScalarString(CodePoint.SRVNAM, server.ATT_SRVNAM); 1542 writer.writeScalarString(CodePoint.SRVRLSLV, server.att_srvrlslv); 1543 writer.endDdmAndDss(); 1544 } 1545 1587 private void writeMGRLEVELS() throws DRDAProtocolException 1588 { 1589 int manager; 1590 int appLevel; 1591 int serverLevel; 1592 writer.startDdm(CodePoint.MGRLVLLS); 1593 for (int i = 0; i < knownManagers.size(); i++) 1594 { 1595 manager = ((Integer )knownManagers.elementAt(i)).intValue(); 1596 appLevel = appRequester.getManagerLevel(manager); 1597 serverLevel = server.getManagerLevel(manager); 1598 if (serverLevel >= appLevel) 1599 { 1600 writer.writeCodePoint4Bytes(manager, appLevel); 1603 } 1604 else 1605 { 1606 writer.writeCodePoint4Bytes(manager, serverLevel); 1607 appRequester.setManagerLevel(manager, serverLevel); 1609 } 1610 } 1611 for (int i = 0; i < unknownManagers.size(); i++) 1613 { 1614 manager = ((Integer )unknownManagers.elementAt(i)).intValue(); 1615 writer.writeCodePoint4Bytes(manager, 0); 1616 } 1617 writer.endDdm(); 1618 } 1619 1641 private int parseACCSEC() throws DRDAProtocolException 1642 { 1643 int securityCheckCode = 0; 1644 int securityMechanism = 0; 1645 byte [] secTokenIn = null; 1646 1647 reader.markCollection(); 1648 int codePoint = reader.getCodePoint(); 1649 while (codePoint != -1) 1650 { 1651 switch(codePoint) 1652 { 1653 case CodePoint.SECMGRNM: 1655 if (reader.getDdmLength() != 0) 1657 badObjectLength(CodePoint.SECMGRNM); 1658 break; 1659 case CodePoint.SECMEC: 1661 checkLength(CodePoint.SECMEC, 2); 1662 securityMechanism = reader.readNetworkShort(); 1663 if (SanityManager.DEBUG) 1664 trace("parseACCSEC - Security mechanism = " + securityMechanism); 1665 1666 if ( (server.getSecurityMechanism() != 1676 NetworkServerControlImpl.INVALID_OR_NOTSET_SECURITYMECHANISM) 1677 && securityMechanism != server.getSecurityMechanism()) 1678 { 1679 securityCheckCode = CodePoint.SECCHKCD_NOTSUPPORTED; 1680 if (SanityManager.DEBUG) { 1681 trace("parseACCSEC - SECCHKCD_NOTSUPPORTED [1] - " + 1682 securityMechanism + " <> " + 1683 server.getSecurityMechanism() + "\n"); 1684 } 1685 } 1686 else 1687 { 1688 if (securityMechanism != CodePoint.SECMEC_USRIDPWD && 1691 securityMechanism != CodePoint.SECMEC_USRIDONL) 1692 { 1693 if (((securityMechanism != CodePoint.SECMEC_EUSRIDPWD) || 1695 (securityMechanism == CodePoint.SECMEC_EUSRIDPWD && 1696 !server.supportsEUSRIDPWD()) 1697 ) && 1698 (securityMechanism != 1699 CodePoint.SECMEC_USRSSBPWD)) 1700 { 1702 securityCheckCode = CodePoint.SECCHKCD_NOTSUPPORTED; 1703 if (SanityManager.DEBUG) { 1704 trace("parseACCSEC - SECCHKCD_NOTSUPPORTED [2]\n"); 1705 } 1706 } 1707 else 1708 { 1709 if (securityMechanism == 1718 CodePoint.SECMEC_USRSSBPWD) 1719 break; 1720 1721 try { 1723 if (decryptionManager == null) 1724 decryptionManager = new DecryptionManager(); 1725 myPublicKey = decryptionManager.obtainPublicKey(); 1726 } catch (SQLException e) { 1727 println2Log(null, session.drdaID, e.getMessage()); 1728 securityCheckCode = CodePoint.SECCHKCD_0A; 1730 } 1731 } 1732 } 1733 } 1734 break; 1735 case CodePoint.RDBNAM: 1738 String dbname = parseRDBNAM(); 1739 Database d = session.getDatabase(dbname); 1740 if (d == null) 1741 addDatabase(dbname); 1742 else 1743 { 1744 d.reset(); 1746 database = d; 1747 } 1748 break; 1749 case CodePoint.SECTKN: 1751 secTokenIn = reader.readBytes(); 1752 break; 1753 default: 1754 invalidCodePoint(codePoint); 1755 } 1756 codePoint = reader.getCodePoint(); 1757 } 1758 1759 if (securityMechanism == 0) 1761 missingCodePoint(CodePoint.SECMEC); 1762 1763 1764 if (database == null) 1769 missingCodePoint(CodePoint.RDBNAM); 1770 1771 database.securityMechanism = securityMechanism; 1772 database.secTokenIn = secTokenIn; 1773 1774 if (securityCheckCode == 0 && 1778 (database.securityMechanism == CodePoint.SECMEC_USRSSBPWD)) 1779 { 1780 if (SanityManager.DEBUG) 1781 SanityManager.ASSERT((securityCheckCode == 0), 1782 "SECMEC_USRSSBPWD: securityCheckCode should not " + 1783 "already be set, found it initialized with " + 1784 "a value of '" + securityCheckCode + "'."); 1785 securityCheckCode = validateSecMecUSRSSBPWD(); 1786 } 1787 1788 if (securityCheckCode == 0 && 1790 (database.securityMechanism == CodePoint.SECMEC_EUSRIDPWD || 1791 database.securityMechanism == CodePoint.SECMEC_USRSSBPWD) && 1792 database.secTokenIn == null) 1793 securityCheckCode = CodePoint.SECCHKCD_SECTKNMISSING_OR_INVALID; 1794 1795 if (securityCheckCode == 0 && 1797 (database.securityMechanism == CodePoint.SECMEC_USRIDPWD || 1798 database.securityMechanism == CodePoint.SECMEC_USRIDONL) && 1799 database.secTokenIn != null) 1800 securityCheckCode = CodePoint.SECCHKCD_SECTKNMISSING_OR_INVALID; 1801 1802 if (SanityManager.DEBUG) 1803 trace("** ACCSECRD securityCheckCode is: " + securityCheckCode); 1804 1805 if (securityCheckCode == 0) 1809 session.setState(session.SECACC); 1810 else 1811 session.setState(session.ATTEXC); 1812 1813 return securityCheckCode; 1814 } 1815 1816 1831 private Pkgnamcsn parseOPNQRY() throws DRDAProtocolException, SQLException 1832 { 1833 Pkgnamcsn pkgnamcsn = null; 1834 boolean gotQryblksz = false; 1835 int blksize = 0; 1836 int qryblkctl = CodePoint.QRYBLKCTL_DEFAULT; 1837 int maxblkext = CodePoint.MAXBLKEXT_DEFAULT; 1838 int qryrowset = CodePoint.QRYROWSET_DEFAULT; 1839 int qryclsimp = DRDAResultSet.QRYCLSIMP_DEFAULT; 1840 int outovropt = CodePoint.OUTOVRFRS; 1841 reader.markCollection(); 1842 int codePoint = reader.getCodePoint(); 1843 while (codePoint != -1) 1844 { 1845 switch(codePoint) 1846 { 1847 case CodePoint.RDBNAM: 1849 setDatabase(CodePoint.OPNQRY); 1850 break; 1851 case CodePoint.PKGNAMCSN: 1853 pkgnamcsn = parsePKGNAMCSN(); 1854 break; 1855 case CodePoint.QRYBLKSZ: 1857 blksize = parseQRYBLKSZ(); 1858 gotQryblksz = true; 1859 break; 1860 case CodePoint.QRYBLKCTL: 1862 qryblkctl = reader.readNetworkShort(); 1863 if (qryblkctl != CodePoint.FRCFIXROW) 1866 invalidCodePoint(qryblkctl); 1867 if (SanityManager.DEBUG) 1868 trace("!!qryblkctl = "+Integer.toHexString(qryblkctl)); 1869 gotQryblksz = true; 1870 break; 1871 case CodePoint.MAXBLKEXT: 1873 maxblkext = reader.readSignedNetworkShort(); 1874 if (SanityManager.DEBUG) 1875 trace("maxblkext = "+maxblkext); 1876 break; 1877 case CodePoint.OUTOVROPT: 1879 outovropt = parseOUTOVROPT(); 1880 break; 1881 case CodePoint.QRYROWSET: 1883 qryrowset = parseQRYROWSET(0); 1885 break; 1886 case CodePoint.QRYCLSIMP: 1887 qryclsimp = parseQRYCLSIMP(); 1889 break; 1890 case CodePoint.QRYCLSRLS: 1891 parseQRYCLSRLS(); 1893 break; 1894 case CodePoint.MONITOR: 1896 parseMONITOR(); 1897 break; 1898 default: 1899 invalidCodePoint(codePoint); 1900 } 1901 codePoint = reader.getCodePoint(); 1902 } 1903 if (pkgnamcsn == null) 1905 missingCodePoint(CodePoint.PKGNAMCSN); 1906 if (!gotQryblksz) 1907 missingCodePoint(CodePoint.QRYBLKSZ); 1908 1909 DRDAStatement stmt = database.getDRDAStatement(pkgnamcsn); 1911 if (stmt == null) 1912 { 1913 invalidValue(CodePoint.PKGNAMCSN); 1915 } 1916 1917 stmt.setOPNQRYOptions(blksize,qryblkctl,maxblkext,outovropt, 1930 qryrowset, qryclsimp); 1931 1933 if (reader.isChainedWithSameID()) 1936 { 1937 if (SanityManager.DEBUG) 1938 trace("&&&&&& parsing SQLDTA"); 1939 parseOPNQRYobjects(stmt); 1940 } 1941 return pkgnamcsn; 1942 } 1943 1959 private void parseOPNQRYobjects(DRDAStatement stmt) 1960 throws DRDAProtocolException, SQLException 1961 { 1962 int codePoint; 1963 do 1964 { 1965 correlationID = reader.readDssHeader(); 1966 while (reader.moreDssData()) 1967 { 1968 codePoint = reader.readLengthAndCodePoint(); 1969 switch(codePoint) 1970 { 1971 case CodePoint.TYPDEFNAM: 1973 setStmtOrDbByteOrder(false, stmt, parseTYPDEFNAM()); 1974 break; 1975 case CodePoint.TYPDEFOVR: 1977 parseTYPDEFOVR(stmt); 1978 break; 1979 case CodePoint.SQLDTA: 1981 parseSQLDTA(stmt); 1982 break; 1983 case CodePoint.EXTDTA: 1985 readAndSetAllExtParams(stmt, false); 1986 break; 1987 default: 1988 invalidCodePoint(codePoint); 1989 } 1990 } 1991 } while (reader.isChainedWithSameID()); 1992 1993 } 1994 2001 private int parseOUTOVROPT() throws DRDAProtocolException 2002 { 2003 checkLength(CodePoint.OUTOVROPT, 1); 2004 int outovropt = reader.readUnsignedByte(); 2005 if (SanityManager.DEBUG) 2006 trace("output override option: "+outovropt); 2007 if (outovropt != CodePoint.OUTOVRFRS && outovropt != CodePoint.OUTOVRANY) 2008 invalidValue(CodePoint.OUTOVROPT); 2009 return outovropt; 2010 } 2011 2012 2019 private int parseQRYBLKSZ() throws DRDAProtocolException 2020 { 2021 checkLength(CodePoint.QRYBLKSZ, 4); 2022 int blksize = reader.readNetworkInt(); 2023 if (SanityManager.DEBUG) 2024 trace("qryblksz = "+blksize); 2025 if (blksize < CodePoint.QRYBLKSZ_MIN || blksize > CodePoint.QRYBLKSZ_MAX) 2026 invalidValue(CodePoint.QRYBLKSZ); 2027 return blksize; 2028 } 2029 2036 private int parseQRYROWSET(int minVal) throws DRDAProtocolException 2037 { 2038 checkLength(CodePoint.QRYROWSET, 4); 2039 int qryrowset = reader.readNetworkInt(); 2040 if (SanityManager.DEBUG) 2041 trace("qryrowset = " + qryrowset); 2042 if (qryrowset < minVal || qryrowset > CodePoint.QRYROWSET_MAX) 2043 invalidValue(CodePoint.QRYROWSET); 2044 return qryrowset; 2045 } 2046 2047 2051 private int parseQRYCLSIMP() throws DRDAProtocolException 2052 { 2053 2054 checkLength(CodePoint.QRYCLSIMP, 1); 2055 int qryclsimp = reader.readUnsignedByte(); 2056 if (SanityManager.DEBUG) 2057 trace ("qryclsimp = " + qryclsimp); 2058 if (qryclsimp != CodePoint.QRYCLSIMP_SERVER_CHOICE && 2059 qryclsimp != CodePoint.QRYCLSIMP_YES && 2060 qryclsimp != CodePoint.QRYCLSIMP_NO ) 2061 invalidValue(CodePoint.QRYCLSIMP); 2062 return qryclsimp; 2063 } 2064 2065 2066 private int parseQRYCLSRLS() throws DRDAProtocolException 2067 { 2068 reader.skipBytes(); 2069 return 0; 2070 } 2071 2072 2081 private void writeQRYPOPRM() throws DRDAProtocolException 2082 { 2083 writer.createDssReply(); 2084 writer.startDdm(CodePoint.QRYPOPRM); 2085 writer.writeScalar2Bytes(CodePoint.SVRCOD, CodePoint.SVRCOD_ERROR); 2086 writeRDBNAM(database.dbName); 2087 writePKGNAMCSN(); 2088 writer.endDdmAndDss(); 2089 } 2090 2100 private void writeQRYNOPRM(int svrCod) throws DRDAProtocolException 2101 { 2102 writer.createDssReply(); 2103 writer.startDdm(CodePoint.QRYNOPRM); 2104 writer.writeScalar2Bytes(CodePoint.SVRCOD, svrCod); 2105 writeRDBNAM(database.dbName); 2106 writePKGNAMCSN(); 2107 writer.endDdmAndDss(); 2108 } 2109 2119 private void writeOPNQFLRM(SQLException e) throws DRDAProtocolException 2120 { 2121 writer.createDssReply(); 2122 writer.startDdm(CodePoint.OPNQFLRM); 2123 writer.writeScalar2Bytes(CodePoint.SVRCOD, CodePoint.SVRCOD_ERROR); 2124 writeRDBNAM(database.dbName); 2125 writer.endDdm(); 2126 writer.startDdm(CodePoint.SQLCARD); 2127 writeSQLCAGRP(e, getSqlCode(getExceptionSeverity(e)), 0, 0); 2128 writer.endDdmAndDss(); 2129 } 2130 2144 private void writePKGNAMCSN(byte[] pkgcnstkn) throws DRDAProtocolException 2145 { 2146 writer.startDdm(CodePoint.PKGNAMCSN); 2147 if (rdbnam.length() <= CodePoint.RDBNAM_LEN && 2148 rdbcolid.length() <= CodePoint.RDBCOLID_LEN && 2149 pkgid.length() <= CodePoint.PKGID_LEN) 2150 { writer.writeScalarPaddedString(rdbnam, CodePoint.RDBNAM_LEN); 2153 writer.writeScalarPaddedString(rdbcolid, CodePoint.RDBCOLID_LEN); 2154 writer.writeScalarPaddedString(pkgid, CodePoint.PKGID_LEN); 2155 writer.writeScalarPaddedBytes(pkgcnstkn, 2156 CodePoint.PKGCNSTKN_LEN, (byte) 0); 2157 writer.writeShort(pkgsn); 2158 } 2159 else { 2161 int len = Math.max(CodePoint.RDBNAM_LEN, rdbnam.length()); 2162 writer.writeShort(len); 2163 writer.writeScalarPaddedString(rdbnam, len); 2164 len = Math.max(CodePoint.RDBCOLID_LEN, rdbcolid.length()); 2165 writer.writeShort(len); 2166 writer.writeScalarPaddedString(rdbcolid, len); 2167 len = Math.max(CodePoint.PKGID_LEN, pkgid.length()); 2168 writer.writeShort(len); 2169 writer.writeScalarPaddedString(pkgid, len); 2170 writer.writeScalarPaddedBytes(pkgcnstkn, 2171 CodePoint.PKGCNSTKN_LEN, (byte) 0); 2172 writer.writeShort(pkgsn); 2173 } 2174 writer.endDdm(); 2175 } 2176 2177 private void writePKGNAMCSN() throws DRDAProtocolException 2178 { 2179 writePKGNAMCSN(pkgcnstkn.getBytes()); 2180 } 2181 2182 2205 private DRDAStatement parseCNTQRY() throws DRDAProtocolException, SQLException 2206 { 2207 byte val; 2208 Pkgnamcsn pkgnamcsn = null; 2209 boolean gotQryblksz = false; 2210 boolean qryrelscr = true; 2211 long qryrownbr = 1; 2212 boolean qryrfrtbl = false; 2213 int nbrrow = 1; 2214 int blksize = 0; 2215 int maxblkext = -1; 2216 long qryinsid; 2217 boolean gotQryinsid = false; 2218 int qryscrorn = CodePoint.QRYSCRREL; 2219 boolean qryrowsns = false; 2220 boolean gotQryrowsns = false; 2221 boolean qryblkrst = false; 2222 boolean qryrtndta = true; 2223 int qryrowset = CodePoint.QRYROWSET_DEFAULT; 2224 int rtnextdta = CodePoint.RTNEXTROW; 2225 reader.markCollection(); 2226 int codePoint = reader.getCodePoint(); 2227 while (codePoint != -1) 2228 { 2229 switch(codePoint) 2230 { 2231 case CodePoint.RDBNAM: 2233 setDatabase(CodePoint.CNTQRY); 2234 break; 2235 case CodePoint.PKGNAMCSN: 2237 pkgnamcsn = parsePKGNAMCSN(); 2238 break; 2239 case CodePoint.QRYBLKSZ: 2241 blksize = parseQRYBLKSZ(); 2242 gotQryblksz = true; 2243 break; 2244 case CodePoint.QRYRELSCR: 2246 qryrelscr = readBoolean(CodePoint.QRYRELSCR); 2247 if (SanityManager.DEBUG) 2248 trace("qryrelscr = "+qryrelscr); 2249 break; 2250 case CodePoint.QRYSCRORN: 2252 checkLength(CodePoint.QRYSCRORN, 1); 2253 qryscrorn = reader.readUnsignedByte(); 2254 if (SanityManager.DEBUG) 2255 trace("qryscrorn = "+qryscrorn); 2256 switch (qryscrorn) 2257 { 2258 case CodePoint.QRYSCRREL: 2259 case CodePoint.QRYSCRABS: 2260 case CodePoint.QRYSCRAFT: 2261 case CodePoint.QRYSCRBEF: 2262 break; 2263 default: 2264 invalidValue(CodePoint.QRYSCRORN); 2265 } 2266 break; 2267 case CodePoint.QRYROWNBR: 2269 checkLength(CodePoint.QRYROWNBR, 8); 2270 qryrownbr = reader.readNetworkLong(); 2271 if (SanityManager.DEBUG) 2272 trace("qryrownbr = "+qryrownbr); 2273 break; 2274 case CodePoint.QRYROWSNS: 2276 checkLength(CodePoint.QRYROWSNS, 1); 2277 qryrowsns = readBoolean(CodePoint.QRYROWSNS); 2278 if (SanityManager.DEBUG) 2279 trace("qryrowsns = "+qryrowsns); 2280 gotQryrowsns = true; 2281 break; 2282 case CodePoint.QRYBLKRST: 2284 checkLength(CodePoint.QRYBLKRST, 1); 2285 qryblkrst = readBoolean(CodePoint.QRYBLKRST); 2286 if (SanityManager.DEBUG) 2287 trace("qryblkrst = "+qryblkrst); 2288 break; 2289 case CodePoint.QRYRTNDTA: 2291 qryrtndta = readBoolean(CodePoint.QRYRTNDTA); 2292 if (SanityManager.DEBUG) 2293 trace("qryrtndta = "+qryrtndta); 2294 break; 2295 case CodePoint.QRYROWSET: 2297 qryrowset = parseQRYROWSET(1); 2299 if (SanityManager.DEBUG) 2300 trace("qryrowset = "+qryrowset); 2301 break; 2302 case CodePoint.QRYRFRTBL: 2304 qryrfrtbl = readBoolean(CodePoint.QRYRFRTBL); 2305 if (SanityManager.DEBUG) 2306 trace("qryrfrtbl = "+qryrfrtbl); 2307 break; 2308 case CodePoint.NBRROW: 2310 checkLength(CodePoint.NBRROW, 4); 2311 nbrrow = reader.readNetworkInt(); 2312 if (SanityManager.DEBUG) 2313 trace("nbrrow = "+nbrrow); 2314 break; 2315 case CodePoint.MAXBLKEXT: 2317 checkLength(CodePoint.MAXBLKEXT, 2); 2318 maxblkext = reader.readSignedNetworkShort(); 2319 if (SanityManager.DEBUG) 2320 trace("maxblkext = "+maxblkext); 2321 break; 2322 case CodePoint.RTNEXTDTA: 2324 checkLength(CodePoint.RTNEXTDTA, 1); 2325 rtnextdta = reader.readUnsignedByte(); 2326 if (rtnextdta != CodePoint.RTNEXTROW && 2327 rtnextdta != CodePoint.RTNEXTALL) 2328 invalidValue(CodePoint.RTNEXTDTA); 2329 if (SanityManager.DEBUG) 2330 trace("rtnextdta = "+rtnextdta); 2331 break; 2332 case CodePoint.QRYINSID: 2334 checkLength(CodePoint.QRYINSID, 8); 2335 qryinsid = reader.readNetworkLong(); 2336 gotQryinsid = true; 2337 if (SanityManager.DEBUG) 2338 trace("qryinsid = "+qryinsid); 2339 break; 2340 case CodePoint.MONITOR: 2342 parseMONITOR(); 2343 break; 2344 default: 2345 invalidCodePoint(codePoint); 2346 } 2347 codePoint = reader.getCodePoint(); 2348 } 2349 if (pkgnamcsn == null) 2351 missingCodePoint(CodePoint.PKGNAMCSN); 2352 if (!gotQryblksz) 2353 missingCodePoint(CodePoint.QRYBLKSZ); 2354 if (sqlamLevel >= MGRLVL_7 && !gotQryinsid) 2355 missingCodePoint(CodePoint.QRYINSID); 2356 2357 DRDAStatement stmt = database.getDRDAStatement(pkgnamcsn); 2359 if (stmt == null) 2360 { 2361 invalidValue(CodePoint.CNTQRY); 2363 } 2364 2365 if (stmt.rsIsClosed()) 2366 { 2367 writeQRYNOPRM(CodePoint.SVRCOD_ERROR); 2368 skipRemainder(true); 2369 return null; 2370 } 2371 stmt.setQueryOptions(blksize,qryrelscr,qryrownbr,qryrfrtbl,nbrrow,maxblkext, 2372 qryscrorn,qryrowsns,qryblkrst,qryrtndta,qryrowset, 2373 rtnextdta); 2374 2375 if (reader.isChainedWithSameID()) 2376 parseCNTQRYobjects(stmt); 2377 return stmt; 2378 } 2379 2388 private void skipRemainder(boolean onlySkipSameIds) throws DRDAProtocolException 2389 { 2390 reader.skipDss(); 2391 while (reader.isChainedWithSameID() || 2392 (!onlySkipSameIds && reader.isChainedWithDiffID())) 2393 { 2394 reader.readDssHeader(); 2395 reader.skipDss(); 2396 } 2397 } 2398 2406 private void parseCNTQRYobjects(DRDAStatement stmt) throws DRDAProtocolException, SQLException 2407 { 2408 int codePoint; 2409 do 2410 { 2411 correlationID = reader.readDssHeader(); 2412 while (reader.moreDssData()) 2413 { 2414 codePoint = reader.readLengthAndCodePoint(); 2415 switch(codePoint) 2416 { 2417 case CodePoint.OUTOVR: 2419 parseOUTOVR(stmt); 2420 break; 2421 default: 2422 invalidCodePoint(codePoint); 2423 } 2424 } 2425 } while (reader.isChainedWithSameID()); 2426 2427 } 2428 2436 private void parseOUTOVR(DRDAStatement stmt) throws DRDAProtocolException, SQLException 2437 { 2438 boolean first = true; 2439 int numVars; 2440 int dtaGrpLen; 2441 int tripType; 2442 int tripId; 2443 int precision; 2444 int start = 0; 2445 while (true) 2446 { 2447 dtaGrpLen = reader.readUnsignedByte(); 2448 tripType = reader.readUnsignedByte(); 2449 tripId = reader.readUnsignedByte(); 2450 if (tripType == FdocaConstants.RLO_TRIPLET_TYPE) 2452 { 2453 reader.skipBytes(); 2455 break; 2456 } 2457 numVars = (dtaGrpLen - 3) / 3; 2458 if (SanityManager.DEBUG) 2459 trace("num of vars is: "+numVars); 2460 int[] outovr_drdaType = null; 2461 if (first) 2462 { 2463 outovr_drdaType = new int[numVars]; 2464 first = false; 2465 } 2466 else 2467 { 2468 int[] oldoutovr_drdaType = stmt.getOutovr_drdaType(); 2469 int oldlen = oldoutovr_drdaType.length; 2470 outovr_drdaType = new int[oldlen + numVars]; 2472 System.arraycopy(oldoutovr_drdaType, 0, 2473 outovr_drdaType,0, 2474 oldlen); 2475 start = oldlen; 2476 } 2477 for (int i = start; i < numVars + start; i++) 2478 { 2479 outovr_drdaType[i] = reader.readUnsignedByte(); 2480 if (SanityManager.DEBUG) 2481 trace("drdaType is: "+ outovr_drdaType[i]); 2482 precision = reader.readNetworkShort(); 2483 if (SanityManager.DEBUG) 2484 trace("drdaLength is: "+precision); 2485 outovr_drdaType[i] |= (precision << 8); 2486 } 2487 stmt.setOutovr_drdaType(outovr_drdaType); 2488 } 2489 } 2490 2491 2508 private void writeOPNQRYRM(boolean isDssObject, DRDAStatement stmt) 2509 throws DRDAProtocolException, SQLException 2510 { 2511 if (SanityManager.DEBUG) 2512 trace("WriteOPNQRYRM"); 2513 2514 if (isDssObject) 2515 writer.createDssObject(); 2516 else 2517 writer.createDssReply(); 2518 writer.startDdm(CodePoint.OPNQRYRM); 2519 writer.writeScalar2Bytes(CodePoint.SVRCOD,CodePoint.SVRCOD_INFO); 2520 2521 int prcType = stmt.getQryprctyp(); 2525 if (SanityManager.DEBUG) 2526 trace("sending QRYPRCTYP: " + prcType); 2527 writer.writeScalar2Bytes(CodePoint.QRYPRCTYP, prcType); 2528 2529 if (stmt.getCurrentDrdaResultSet().withHoldCursor == JDBC30Translation.HOLD_CURSORS_OVER_COMMIT) 2534 writer.writeScalar1Byte(CodePoint.SQLCSRHLD, CodePoint.TRUE); 2535 if (sqlamLevel >= MGRLVL_7) 2536 { 2537 writer.writeScalarHeader(CodePoint.QRYINSID, 8); 2538 writer.writeInt(0); 2543 writer.writeInt(session.qryinsid++); 2545 if (stmt.isScrollable()) 2547 { 2548 writer.writeScalar1Byte(CodePoint.QRYATTSCR, CodePoint.TRUE); 2549 if ((stmt.getConcurType() == ResultSet.CONCUR_UPDATABLE) && 2550 (stmt.getResultSet().getType() == 2551 ResultSet.TYPE_SCROLL_INSENSITIVE)) { 2552 writer.writeScalar1Byte(CodePoint.QRYATTSNS, 2553 CodePoint.QRYSNSSTC); 2554 } else { 2555 writer.writeScalar1Byte(CodePoint.QRYATTSNS, 2556 CodePoint.QRYINS); 2557 } 2558 } 2559 if (stmt.getConcurType() == ResultSet.CONCUR_UPDATABLE) { 2560 if (stmt.getResultSet() != null) { 2561 if (stmt.getResultSet().getConcurrency() == 2565 ResultSet.CONCUR_UPDATABLE) { 2566 writer.writeScalar1Byte(CodePoint.QRYATTUPD, 2567 CodePoint.QRYUPD); 2568 } else { 2569 writer.writeScalar1Byte(CodePoint.QRYATTUPD, 2570 CodePoint.QRYRDO); 2571 } 2572 } else { 2573 writer.writeScalar1Byte(CodePoint.QRYATTUPD, 2574 CodePoint.QRYUPD); 2575 } 2576 } else { 2577 writer.writeScalar1Byte(CodePoint.QRYATTUPD, CodePoint.QRYRDO); 2578 } 2579 } 2580 writer.endDdmAndDss (); 2581 } 2582 2589 private void writeENDQRYRM(int svrCod) throws DRDAProtocolException 2590 { 2591 writer.createDssReply(); 2592 writer.startDdm(CodePoint.ENDQRYRM); 2593 writer.writeScalar2Bytes(CodePoint.SVRCOD,svrCod); 2594 writer.endDdmAndDss(); 2595 } 2596 2602 private void writeABNUOWRM() throws DRDAProtocolException 2603 { 2604 writer.createDssReply(); 2605 writer.startDdm(CodePoint.ABNUOWRM); 2606 writer.writeScalar2Bytes(CodePoint.SVRCOD,CodePoint.SVRCOD_ERROR); 2607 writeRDBNAM(database.dbName); 2608 writer.endDdmAndDss(); 2609 } 2610 2617 private String parseRDBNAM() throws DRDAProtocolException 2618 { 2619 String name; 2620 byte [] rdbName = reader.readBytes(); 2621 if (rdbName.length == 0) 2622 { 2623 rdbNotFound(null); 2625 } 2626 if (rdbName.length < CodePoint.RDBNAM_LEN || rdbName.length > CodePoint.MAX_NAME) 2628 badObjectLength(CodePoint.RDBNAM); 2629 name = reader.convertBytes(rdbName); 2630 name = name.trim(); 2632 if (SanityManager.DEBUG) 2633 trace("RdbName " + name); 2634 return name; 2635 } 2636 2637 2653 private void writeACCSECRD(int securityCheckCode) 2654 throws DRDAProtocolException 2655 { 2656 writer.createDssReply(); 2657 writer.startDdm(CodePoint.ACCSECRD); 2658 2659 if (securityCheckCode != CodePoint.SECCHKCD_NOTSUPPORTED) 2660 writer.writeScalar2Bytes(CodePoint.SECMEC, database.securityMechanism); 2661 else 2662 { 2663 2666 if ( server.getSecurityMechanism() != NetworkServerControlImpl.INVALID_OR_NOTSET_SECURITYMECHANISM ) 2670 writer.writeScalar2Bytes(CodePoint.SECMEC, server.getSecurityMechanism()); 2671 else 2672 { 2673 writer.writeScalar2Bytes(CodePoint.SECMEC, CodePoint.SECMEC_USRIDPWD); 2679 if ( server.supportsEUSRIDPWD()) 2682 writer.writeScalar2Bytes(CodePoint.SECMEC, CodePoint.SECMEC_EUSRIDPWD); 2683 writer.writeScalar2Bytes(CodePoint.SECMEC, CodePoint.SECMEC_USRIDONL); 2684 writer.writeScalar2Bytes(CodePoint.SECMEC, CodePoint.SECMEC_USRSSBPWD); 2685 } 2686 } 2687 2688 if (securityCheckCode != 0) 2689 { 2690 writer.writeScalar1Byte(CodePoint.SECCHKCD, securityCheckCode); 2691 } 2692 else 2693 { 2694 if (database.securityMechanism == CodePoint.SECMEC_EUSRIDPWD) 2696 writer.writeScalarBytes(CodePoint.SECTKN, myPublicKey); 2697 else if (database.securityMechanism == CodePoint.SECMEC_USRSSBPWD) 2698 writer.writeScalarBytes(CodePoint.SECTKN, myTargetSeed); 2699 } 2700 writer.endDdmAndDss (); 2701 2702 if (securityCheckCode != 0) { 2703 skipRemainder(false); 2706 } 2707 2708 finalizeChain(); 2709 } 2710 2711 2726 private int parseSECCHK() throws DRDAProtocolException 2727 { 2728 int codePoint, securityCheckCode = 0; 2729 int securityMechanism = 0; 2730 databaseAccessException = null; 2731 reader.markCollection(); 2732 codePoint = reader.getCodePoint(); 2733 while (codePoint != -1) 2734 { 2735 switch (codePoint) 2736 { 2737 case CodePoint.SECMGRNM: 2739 reader.skipBytes(); 2740 break; 2741 case CodePoint.SECMEC: 2743 checkLength(CodePoint.SECMEC, 2); 2744 securityMechanism = reader.readNetworkShort(); 2745 if (SanityManager.DEBUG) 2746 trace("parseSECCHK - Security mechanism = " + securityMechanism); 2747 if (securityMechanism != database.securityMechanism) 2750 invalidValue(CodePoint.SECMEC); 2751 break; 2752 case CodePoint.SECTKN: 2754 if ((database.securityMechanism != 2755 CodePoint.SECMEC_EUSRIDPWD) && 2756 (database.securityMechanism != 2757 CodePoint.SECMEC_USRSSBPWD)) 2758 { 2759 securityCheckCode = CodePoint.SECCHKCD_SECTKNMISSING_OR_INVALID; 2760 reader.skipBytes(); 2761 } 2762 else if (database.securityMechanism == 2763 CodePoint.SECMEC_EUSRIDPWD) 2764 { 2765 if (database.decryptedUserId == null) 2766 { 2767 try { 2768 database.decryptedUserId = 2769 reader.readEncryptedString( 2770 decryptionManager, 2771 database.securityMechanism, 2772 myPublicKey, 2773 database.secTokenIn); 2774 } catch (SQLException se) { 2775 println2Log(database.dbName, session.drdaID, 2776 se.getMessage()); 2777 if (securityCheckCode == 0) 2778 securityCheckCode = CodePoint.SECCHKCD_13; 2780 } 2781 database.userId = database.decryptedUserId; 2782 if (SanityManager.DEBUG) 2783 trace("**decrypted userid is: "+database.userId); 2784 } 2785 else if (database.decryptedPassword == null) 2786 { 2787 try { 2788 database.decryptedPassword = 2789 reader.readEncryptedString( 2790 decryptionManager, 2791 database.securityMechanism, 2792 myPublicKey, 2793 database.secTokenIn); 2794 } catch (SQLException se) { 2795 println2Log(database.dbName, session.drdaID, 2796 se.getMessage()); 2797 if (securityCheckCode == 0) 2798 securityCheckCode = CodePoint.SECCHKCD_0F; 2800 } 2801 database.password = database.decryptedPassword; 2802 if (SanityManager.DEBUG) 2803 trace("**decrypted password is: " + 2804 database.password); 2805 } 2806 } 2807 else if (database.securityMechanism == 2808 CodePoint.SECMEC_USRSSBPWD) 2809 { 2810 if (database.passwordSubstitute == null) 2811 { 2812 database.passwordSubstitute = reader.readBytes(); 2813 if (SanityManager.DEBUG) 2814 trace("** Substitute Password is:" + 2815 DecryptionManager.toHexString( 2816 database.passwordSubstitute, 0, 2817 database.passwordSubstitute.length)); 2818 database.password = 2819 DecryptionManager.toHexString( 2820 database.passwordSubstitute, 0, 2821 database.passwordSubstitute.length); 2822 } 2823 } 2824 else 2825 { 2826 tooMany(CodePoint.SECTKN); 2827 } 2828 break; 2829 case CodePoint.PASSWORD: 2831 database.password = reader.readString(); 2832 if (SanityManager.DEBUG) trace("PASSWORD " + database.password); 2833 break; 2834 case CodePoint.NEWPASSWORD: 2837 reader.skipBytes(); 2838 break; 2839 case CodePoint.USRID: 2841 database.userId = reader.readString(); 2842 if (SanityManager.DEBUG) trace("USERID " + database.userId); 2843 break; 2844 case CodePoint.RDBNAM: 2846 String dbname = parseRDBNAM(); 2847 if (database != null) 2848 { 2849 if (!database.dbName.equals(dbname)) 2850 rdbnamMismatch(CodePoint.SECCHK); 2851 } 2852 else 2853 { 2854 addDatabase(dbname); 2858 } 2859 break; 2860 default: 2861 invalidCodePoint(codePoint); 2862 2863 } 2864 codePoint = reader.getCodePoint(); 2865 } 2866 if (securityMechanism == 0) 2868 missingCodePoint(CodePoint.SECMEC); 2869 2870 if (securityCheckCode == 0 && 2872 (database.securityMechanism == CodePoint.SECMEC_USRIDPWD|| 2873 database.securityMechanism == CodePoint.SECMEC_USRIDONL )) 2874 { 2875 if (database.userId == null) 2876 securityCheckCode = CodePoint.SECCHKCD_USERIDMISSING; 2877 else if (database.securityMechanism == CodePoint.SECMEC_USRIDPWD) 2878 { 2879 if (database.password == null) 2880 securityCheckCode = CodePoint.SECCHKCD_PASSWORDMISSING; 2881 } 2882 } 2885 2886 if (securityCheckCode == 0 && 2887 database.securityMechanism == CodePoint.SECMEC_USRSSBPWD) 2888 { 2889 if (database.userId == null) 2890 securityCheckCode = CodePoint.SECCHKCD_USERIDMISSING; 2891 else if (database.passwordSubstitute == null) 2892 securityCheckCode = CodePoint.SECCHKCD_PASSWORDMISSING; 2893 } 2894 2895 if (securityCheckCode == 0 && 2896 database.securityMechanism == CodePoint.SECMEC_EUSRIDPWD) 2897 { 2898 if (database.decryptedUserId == null) 2899 securityCheckCode = CodePoint.SECCHKCD_USERIDMISSING; 2900 else if (database.decryptedPassword == null) 2901 securityCheckCode = CodePoint.SECCHKCD_PASSWORDMISSING; 2902 } 2903 2906 if ((securityCheckCode == 0) && (databaseAccessException == null)) 2908 { 2909 securityCheckCode = verifyUserIdPassword(); 2910 } 2911 2912 if (securityCheckCode == 0) 2914 session.setState(session.CHKSEC); 2915 2916 return securityCheckCode; 2917 2918 } 2919 2930 private void writeSECCHKRM(int securityCheckCode) throws DRDAProtocolException 2931 { 2932 writer.createDssReply(); 2933 writer.startDdm(CodePoint.SECCHKRM); 2934 writer.writeScalar2Bytes(CodePoint.SVRCOD, svrcodFromSecchkcd(securityCheckCode)); 2935 writer.writeScalar1Byte(CodePoint.SECCHKCD, securityCheckCode); 2936 writer.endDdmAndDss (); 2937 2938 if (securityCheckCode != 0) { 2939 skipRemainder(false); 2942 } 2943 2944 finalizeChain(); 2945 2946 } 2947 2953 private int svrcodFromSecchkcd(int securityCheckCode) 2954 { 2955 if (securityCheckCode == 0 || securityCheckCode == 2 || 2956 securityCheckCode == 5 || securityCheckCode == 8) 2957 return CodePoint.SVRCOD_INFO; 2958 else 2959 return CodePoint.SVRCOD_ERROR; 2960 } 2961 2980 private int parseACCRDB() throws DRDAProtocolException 2981 { 2982 int codePoint; 2983 int svrcod = 0; 2984 copyToRequired(ACCRDB_REQUIRED); 2985 reader.markCollection(); 2986 codePoint = reader.getCodePoint(); 2987 while (codePoint != -1) 2988 { 2989 switch (codePoint) 2990 { 2991 case CodePoint.RDBACCCL: 2993 checkLength(CodePoint.RDBACCCL, 2); 2994 int sqlam = reader.readNetworkShort(); 2995 if (SanityManager.DEBUG) 2996 trace("RDBACCCL = " + sqlam); 2997 2999 if (sqlam != CodePoint.SQLAM) 3000 invalidValue(CodePoint.RDBACCCL); 3001 removeFromRequired(CodePoint.RDBACCCL); 3002 break; 3003 case CodePoint.CRRTKN: 3005 database.crrtkn = reader.readBytes(); 3006 if (SanityManager.DEBUG) 3007 trace("crrtkn " + convertToHexString(database.crrtkn)); 3008 removeFromRequired(CodePoint.CRRTKN); 3009 int l = database.crrtkn.length; 3010 if (l > CodePoint.MAX_NAME) 3011 tooBig(CodePoint.CRRTKN); 3012 if (l < 9 || l > 23) 3018 invalidValue(CodePoint.CRRTKN); 3019 byte[] part1 = new byte[l - 6]; 3020 for (int i = 0; i < part1.length; i++) 3021 part1[i] = database.crrtkn[i]; 3022 long time = SignedBinary.getLong(database.crrtkn, 3023 l-8, SignedBinary.BIG_ENDIAN); session.drdaID = reader.convertBytes(part1) + 3025 time + leftBrace + session.connNum + rightBrace; 3026 if (SanityManager.DEBUG) 3027 trace("******************************************drdaID is: " + session.drdaID); 3028 database.setDrdaID(session.drdaID); 3029 3030 break; 3031 case CodePoint.RDBNAM: 3033 String dbname = parseRDBNAM(); 3034 if (database != null) 3035 { 3036 if (!database.dbName.equals(dbname)) 3037 rdbnamMismatch(CodePoint.ACCRDB); 3038 } 3039 else 3040 { 3041 Database d = session.getDatabase(dbname); 3043 if (d == null) 3044 addDatabase(dbname); 3045 else 3046 { 3047 database = d; 3048 database.accessCount++; 3049 } 3050 } 3051 removeFromRequired(CodePoint.RDBNAM); 3052 break; 3053 case CodePoint.PRDID: 3055 appRequester.setClientVersion(reader.readString()); 3056 if (SanityManager.DEBUG) 3057 trace("prdId " + appRequester.prdid); 3058 if (appRequester.prdid.length() > CodePoint.PRDID_MAX) 3059 tooBig(CodePoint.PRDID); 3060 3061 3062 if (((appRequester.getClientType() == appRequester.JCC_CLIENT) && 3063 (appRequester.greaterThanOrEqualTo(1, 5, 0))) || 3064 (appRequester.getClientType() == appRequester.DNC_CLIENT)) 3065 { 3066 sendWarningsOnCNTQRY = true; 3067 } 3068 else sendWarningsOnCNTQRY = false; 3069 3070 if ((appRequester.getClientType() == appRequester.DNC_CLIENT) && 3074 appRequester.greaterThanOrEqualTo(10, 2, 0)) { 3075 diagnosticLevel = CodePoint.DIAGLVL1; 3076 } 3077 3078 removeFromRequired(CodePoint.PRDID); 3079 break; 3080 case CodePoint.TYPDEFNAM: 3082 setStmtOrDbByteOrder(true, null, parseTYPDEFNAM()); 3083 removeFromRequired(CodePoint.TYPDEFNAM); 3084 break; 3085 case CodePoint.TYPDEFOVR: 3087 parseTYPDEFOVR(null); 3088 removeFromRequired(CodePoint.TYPDEFOVR); 3089 break; 3090 case CodePoint.RDBALWUPD: 3092 checkLength(CodePoint.RDBALWUPD, 1); 3093 database.rdbAllowUpdates = readBoolean(CodePoint.RDBALWUPD); 3094 if (SanityManager.DEBUG) 3095 trace("rdbAllowUpdates = "+database.rdbAllowUpdates); 3096 break; 3097 case CodePoint.PRDDTA: 3099 if (reader.getDdmLength() > CodePoint.MAX_NAME) 3101 tooBig(CodePoint.PRDDTA); 3102 reader.skipBytes(); 3103 break; 3104 case CodePoint.TRGDFTRT: 3105 byte b = reader.readByte(); 3106 if (b == 0xF1) 3107 database.sendTRGDFTRT = true; 3108 break; 3109 case CodePoint.STTDECDEL: 3111 case CodePoint.STTSTRDEL: 3112 codePointNotSupported(codePoint); 3113 break; 3114 default: 3115 invalidCodePoint(codePoint); 3116 } 3117 codePoint = reader.getCodePoint(); 3118 } 3119 checkRequired(CodePoint.ACCRDB); 3120 if ((database.ccsidDBC != 0 && !server.supportsCCSID(database.ccsidDBC)) || 3123 (database.ccsidMBC != 0 && !server.supportsCCSID(database.ccsidMBC))) 3124 svrcod = CodePoint.SVRCOD_WARNING; 3125 return svrcod; 3126 } 3127 3133 private String parseTYPDEFNAM() throws DRDAProtocolException 3134 { 3135 String typDefNam = reader.readString(); 3136 if (SanityManager.DEBUG) trace("typeDefName " + typDefNam); 3137 if (typDefNam.length() > CodePoint.MAX_NAME) 3138 tooBig(CodePoint.TYPDEFNAM); 3139 checkValidTypDefNam(typDefNam); 3140 if (!typDefNam.equals(CodePoint.TYPDEFNAM_QTDSQLASC) && 3142 !typDefNam.equals(CodePoint.TYPDEFNAM_QTDSQLJVM) && 3143 !typDefNam.equals(CodePoint.TYPDEFNAM_QTDSQLX86)) 3144 valueNotSupported(CodePoint.TYPDEFNAM); 3145 return typDefNam; 3146 } 3147 3148 3155 private void setStmtOrDbByteOrder(boolean setDatabase, DRDAStatement stmt, String typDefNam) 3156 { 3157 int byteOrder = (typDefNam.equals(CodePoint.TYPDEFNAM_QTDSQLX86) ? 3158 SignedBinary.LITTLE_ENDIAN : SignedBinary.BIG_ENDIAN); 3159 if (setDatabase) 3160 { 3161 database.typDefNam = typDefNam; 3162 database.byteOrder = byteOrder; 3163 } 3164 else 3165 { 3166 stmt.typDefNam = typDefNam; 3167 stmt.byteOrder = byteOrder; 3168 } 3169 } 3170 3171 3187 private void writeACCRDBRM(int svrcod) throws DRDAProtocolException 3188 { 3189 writer.createDssReply(); 3190 writer.startDdm(CodePoint.ACCRDBRM); 3191 writer.writeScalar2Bytes(CodePoint.SVRCOD, svrcod); 3192 writer.writeScalarString(CodePoint.PRDID, server.prdId); 3193 writer.writeScalarString(CodePoint.TYPDEFNAM, 3197 CodePoint.TYPDEFNAM_QTDSQLASC); 3198 writeTYPDEFOVR(); 3199 writer.endDdmAndDss (); 3200 finalizeChain(); 3201 } 3202 3203 private void writeTYPDEFOVR() throws DRDAProtocolException 3204 { 3205 writer.startDdm(CodePoint.TYPDEFOVR); 3207 writer.writeScalar2Bytes(CodePoint.CCSIDSBC, server.CCSIDSBC); 3208 writer.writeScalar2Bytes(CodePoint.CCSIDMBC, server.CCSIDMBC); 3209 if (database.sendTRGDFTRT) 3211 { 3212 writer.startDdm(CodePoint.PKGDFTCST); 3214 writer.writeShort(CodePoint.CSTMBCS); 3215 writer.endDdm(); 3216 writer.startDdm(CodePoint.USRID); 3218 writer.writeString(database.userId); 3219 writer.endDdm(); 3220 } 3221 writer.endDdm(); 3222 3223 } 3224 3225 3238 private void parseTYPDEFOVR(DRDAStatement st) throws DRDAProtocolException 3239 { 3240 int codePoint; 3241 int ccsidSBC = 0; 3242 int ccsidDBC = 0; 3243 int ccsidMBC = 0; 3244 String ccsidSBCEncoding = null; 3245 String ccsidDBCEncoding = null; 3246 String ccsidMBCEncoding = null; 3247 3248 reader.markCollection(); 3249 3250 codePoint = reader.getCodePoint(); 3251 if (codePoint == -1 && st != null) 3254 missingCodePoint(CodePoint.CCSIDSBC); 3255 3256 while (codePoint != -1) 3257 { 3258 switch (codePoint) 3259 { 3260 case CodePoint.CCSIDSBC: 3261 checkLength(CodePoint.CCSIDSBC, 2); 3262 ccsidSBC = reader.readNetworkShort(); 3263 try { 3264 ccsidSBCEncoding = 3265 CharacterEncodings.getJavaEncoding(ccsidSBC); 3266 } catch (Exception e) { 3267 valueNotSupported(CodePoint.CCSIDSBC); 3268 } 3269 if (SanityManager.DEBUG) 3270 trace("ccsidsbc = " + ccsidSBC + " encoding = " + ccsidSBCEncoding); 3271 break; 3272 case CodePoint.CCSIDDBC: 3273 checkLength(CodePoint.CCSIDDBC, 2); 3274 ccsidDBC = reader.readNetworkShort(); 3275 try { 3276 ccsidDBCEncoding = 3277 CharacterEncodings.getJavaEncoding(ccsidDBC); 3278 } catch (Exception e) { 3279 ccsidDBCEncoding = null; 3282 if (st != null) 3283 valueNotSupported(CodePoint.CCSIDSBC); 3284 } 3285 if (SanityManager.DEBUG) 3286 trace("ccsiddbc = " + ccsidDBC + " encoding = " + ccsidDBCEncoding); 3287 break; 3288 case CodePoint.CCSIDMBC: 3289 checkLength(CodePoint.CCSIDMBC, 2); 3290 ccsidMBC = reader.readNetworkShort(); 3291 try { 3292 ccsidMBCEncoding = 3293 CharacterEncodings.getJavaEncoding(ccsidMBC); 3294 } catch (Exception e) { 3295 ccsidMBCEncoding = null; 3297 if (st != null) 3298 valueNotSupported(CodePoint.CCSIDMBC); 3299 } 3300 if (SanityManager.DEBUG) 3301 trace("ccsidmbc = " + ccsidMBC + " encoding = " + ccsidMBCEncoding); 3302 break; 3303 default: 3304 invalidCodePoint(codePoint); 3305 3306 } 3307 codePoint = reader.getCodePoint(); 3308 } 3309 if (st == null) 3310 { 3311 if (ccsidSBC != 0) 3312 { 3313 database.ccsidSBC = ccsidSBC; 3314 database.ccsidSBCEncoding = ccsidSBCEncoding; 3315 } 3316 if (ccsidDBC != 0) 3317 { 3318 database.ccsidDBC = ccsidDBC; 3319 database.ccsidDBCEncoding = ccsidDBCEncoding; 3320 } 3321 if (ccsidMBC != 0) 3322 { 3323 database.ccsidMBC = ccsidMBC; 3324 database.ccsidMBCEncoding = ccsidMBCEncoding; 3325 } 3326 } 3327 else 3328 { 3329 if (ccsidSBC != 0) 3330 { 3331 st.ccsidSBC = ccsidSBC; 3332 st.ccsidSBCEncoding = ccsidSBCEncoding; 3333 } 3334 if (ccsidDBC != 0) 3335 { 3336 st.ccsidDBC = ccsidDBC; 3337 st.ccsidDBCEncoding = ccsidDBCEncoding; 3338 } 3339 if (ccsidMBC != 0) 3340 { 3341 st.ccsidMBC = ccsidMBC; 3342 st.ccsidMBCEncoding = ccsidMBCEncoding; 3343 } 3344 } 3345 } 3346 3359 private int parsePRPSQLSTT() throws DRDAProtocolException,SQLException 3360 { 3361 int codePoint; 3362 boolean rtnsqlda = false; 3363 boolean rtnOutput = true; String typdefnam; 3365 Pkgnamcsn pkgnamcsn = null; 3366 3367 DRDAStatement stmt = null; 3368 Database databaseToSet = null; 3369 3370 reader.markCollection(); 3371 3372 codePoint = reader.getCodePoint(); 3373 while (codePoint != -1) 3374 { 3375 switch (codePoint) 3376 { 3377 case CodePoint.RDBNAM: 3379 setDatabase(CodePoint.PRPSQLSTT); 3380 databaseToSet = database; 3381 break; 3382 case CodePoint.PKGNAMCSN: 3384 pkgnamcsn = parsePKGNAMCSN(); 3385 break; 3386 case CodePoint.RTNSQLDA: 3388 rtnsqlda = readBoolean(CodePoint.RTNSQLDA); 3390 break; 3391 case CodePoint.TYPSQLDA: 3393 rtnOutput = parseTYPSQLDA(); 3394 break; 3395 case CodePoint.MONITOR: 3397 parseMONITOR(); 3398 break; 3399 default: 3400 invalidCodePoint(codePoint); 3401 3402 } 3403 codePoint = reader.getCodePoint(); 3404 } 3405 3406 stmt = database.newDRDAStatement(pkgnamcsn); 3407 String sqlStmt = parsePRPSQLSTTobjects(stmt); 3408 if (databaseToSet != null) 3409 stmt.setDatabase(database); 3410 stmt.explicitPrepare(sqlStmt); 3411 database.setCurrentStatement(stmt); 3413 3414 if (!rtnsqlda) 3415 return 0; 3416 else if (rtnOutput) 3417 return 2; 3418 else 3419 return 1; 3420 } 3421 3439 private String parsePRPSQLSTTobjects(DRDAStatement stmt) 3440 throws DRDAProtocolException, SQLException 3441 { 3442 String sqlStmt = null; 3443 int codePoint; 3444 do 3445 { 3446 correlationID = reader.readDssHeader(); 3447 while (reader.moreDssData()) 3448 { 3449 codePoint = reader.readLengthAndCodePoint(); 3450 switch(codePoint) 3451 { 3452 case CodePoint.SQLSTT: 3454 sqlStmt = parseEncodedString(); 3455 if (SanityManager.DEBUG) 3456 trace("sqlStmt = " + sqlStmt); 3457 break; 3458 case CodePoint.TYPDEFNAM: 3460 setStmtOrDbByteOrder(false, stmt, parseTYPDEFNAM()); 3461 break; 3462 case CodePoint.TYPDEFOVR: 3464 parseTYPDEFOVR(stmt); 3465 break; 3466 case CodePoint.SQLATTR: 3468 parseSQLATTR(stmt); 3469 break; 3470 default: 3471 invalidCodePoint(codePoint); 3472 } 3473 } 3474 } while (reader.isChainedWithSameID()); 3475 if (sqlStmt == null) 3476 missingCodePoint(CodePoint.SQLSTT); 3477 3478 return sqlStmt; 3479 } 3480 3481 3487 private boolean parseTYPSQLDA() throws DRDAProtocolException 3488 { 3489 checkLength(CodePoint.TYPSQLDA, 1); 3490 byte sqldaType = reader.readByte(); 3491 if (SanityManager.DEBUG) 3492 trace("typSQLDa " + sqldaType); 3493 if (sqldaType == CodePoint.TYPSQLDA_STD_OUTPUT || 3494 sqldaType == CodePoint.TYPSQLDA_LIGHT_OUTPUT || 3495 sqldaType == CodePoint.TYPSQLDA_X_OUTPUT) 3496 return true; 3497 else if (sqldaType == CodePoint.TYPSQLDA_STD_INPUT || 3498 sqldaType == CodePoint.TYPSQLDA_LIGHT_INPUT || 3499 sqldaType == CodePoint.TYPSQLDA_X_INPUT) 3500 return false; 3501 else 3502 invalidValue(CodePoint.TYPSQLDA); 3503 3504 return false; 3506 } 3507 3520 protected void parseSQLATTR(DRDAStatement stmt) throws DRDAProtocolException 3521 { 3522 String attrs = parseEncodedString(); 3523 if (SanityManager.DEBUG) 3524 trace("sqlattr = '" + attrs+"'"); 3525 3528 boolean validAttribute = false; 3529 if (attrs.indexOf("INSENSITIVE SCROLL") != -1 || attrs.indexOf("SCROLL INSENSITIVE") != -1) { 3531 stmt.scrollType = ResultSet.TYPE_SCROLL_INSENSITIVE; 3532 stmt.concurType = ResultSet.CONCUR_READ_ONLY; 3533 validAttribute = true; 3534 } 3535 if ((attrs.indexOf("SENSITIVE DYNAMIC SCROLL") != -1) || (attrs.indexOf("SENSITIVE STATIC SCROLL") != -1)) 3536 { 3537 stmt.scrollType = ResultSet.TYPE_SCROLL_SENSITIVE; 3538 validAttribute = true; 3539 } 3540 3541 if ((attrs.indexOf("FOR UPDATE") != -1)) 3542 { 3543 validAttribute = true; 3544 stmt.concurType = ResultSet.CONCUR_UPDATABLE; 3545 } 3546 3547 if (attrs.indexOf("WITH HOLD") != -1) 3548 { 3549 stmt.withHoldCursor = JDBC30Translation.HOLD_CURSORS_OVER_COMMIT; 3550 validAttribute = true; 3551 } 3552 3553 if (!validAttribute) 3554 { 3555 invalidValue(CodePoint.SQLATTR); 3556 } 3557 } 3558 3559 3571 private boolean parseDSCSQLSTT() throws DRDAProtocolException,SQLException 3572 { 3573 int codePoint; 3574 boolean rtnOutput = true; Pkgnamcsn pkgnamcsn = null; 3576 reader.markCollection(); 3577 3578 codePoint = reader.getCodePoint(); 3579 while (codePoint != -1) 3580 { 3581 switch (codePoint) 3582 { 3583 case CodePoint.TYPSQLDA: 3585 rtnOutput = parseTYPSQLDA(); 3586 break; 3587 case CodePoint.RDBNAM: 3589 setDatabase(CodePoint.DSCSQLSTT); 3590 break; 3591 case CodePoint.PKGNAMCSN: 3593 pkgnamcsn = parsePKGNAMCSN(); 3594 DRDAStatement stmt = database.getDRDAStatement(pkgnamcsn); 3595 if (stmt == null) 3596 { 3597 invalidValue(CodePoint.PKGNAMCSN); 3598 } 3599 break; 3600 case CodePoint.MONITOR: 3602 parseMONITOR(); 3603 break; 3604 default: 3605 invalidCodePoint(codePoint); 3606 } 3607 codePoint = reader.getCodePoint(); 3608 } 3609 if (pkgnamcsn == null) 3610 missingCodePoint(CodePoint.PKGNAMCSN); 3611 return rtnOutput; 3612 } 3613 3614 3634 private void parseEXCSQLSTT() throws DRDAProtocolException,SQLException 3635 { 3636 int codePoint; 3637 String strVal; 3638 reader.markCollection(); 3639 3640 codePoint = reader.getCodePoint(); 3641 boolean outputExpected = false; 3642 Pkgnamcsn pkgnamcsn = null; 3643 int numRows = 1; int blkSize = 0; 3645 int maxrslcnt = 0; int maxblkext = CodePoint.MAXBLKEXT_DEFAULT; 3647 int qryrowset = CodePoint.QRYROWSET_DEFAULT; 3648 int outovropt = CodePoint.OUTOVRFRS; 3649 byte [] rslsetflg = null; 3650 String procName = null; 3651 3652 while (codePoint != -1) 3653 { 3654 switch (codePoint) 3655 { 3656 case CodePoint.RDBNAM: 3658 setDatabase(CodePoint.EXCSQLSTT); 3659 break; 3660 case CodePoint.PKGNAMCSN: 3662 pkgnamcsn = parsePKGNAMCSN(); 3663 break; 3664 case CodePoint.OUTEXP: 3666 outputExpected = readBoolean(CodePoint.OUTEXP); 3667 if (SanityManager.DEBUG) 3668 trace("outexp = "+ outputExpected); 3669 break; 3670 case CodePoint.NBRROW: 3672 checkLength(CodePoint.NBRROW, 4); 3673 numRows = reader.readNetworkInt(); 3674 if (SanityManager.DEBUG) 3675 trace("# of rows: "+numRows); 3676 break; 3677 case CodePoint.PRCNAM: 3679 procName = reader.readString(); 3680 if (SanityManager.DEBUG) 3681 trace("Procedure Name = " + procName); 3682 break; 3683 case CodePoint.QRYBLKSZ: 3685 blkSize = parseQRYBLKSZ(); 3686 break; 3687 case CodePoint.MAXRSLCNT: 3689 checkLength(CodePoint.MAXRSLCNT, 2); 3694 maxrslcnt = reader.readNetworkShort(); 3695 if (SanityManager.DEBUG) 3696 trace("max rs count: "+maxrslcnt); 3697 break; 3698 case CodePoint.MAXBLKEXT: 3700 checkLength(CodePoint.MAXBLKEXT, 2); 3704 maxblkext = reader.readNetworkShort(); 3705 if (SanityManager.DEBUG) 3706 trace("max extra blocks: "+maxblkext); 3707 break; 3708 case CodePoint.RSLSETFLG: 3710 rslsetflg = reader.readBytes(); 3712 for (int i=0;i<rslsetflg.length;i++) 3713 if (SanityManager.DEBUG) 3714 trace("rslsetflg: "+rslsetflg[i]); 3715 break; 3716 case CodePoint.RDBCMTOK: 3718 parseRDBCMTOK(); 3719 break; 3720 case CodePoint.OUTOVROPT: 3722 outovropt = parseOUTOVROPT(); 3723 break; 3724 case CodePoint.QRYROWSET: 3726 qryrowset = parseQRYROWSET(0); 3729 break; 3730 case CodePoint.MONITOR: 3732 parseMONITOR(); 3733 break; 3734 default: 3735 invalidCodePoint(codePoint); 3736 } 3737 codePoint = reader.getCodePoint(); 3738 } 3739 3740 if (pkgnamcsn == null) 3741 missingCodePoint(CodePoint.PKGNAMCSN); 3742 3743 DRDAStatement stmt; 3744 boolean needPrepareCall = false; 3745 3746 stmt = database.getDRDAStatement(pkgnamcsn); 3747 boolean isProcedure = (procName !=null || 3748 (stmt != null && 3749 stmt.wasExplicitlyPrepared() && 3750 stmt.isCall)); 3751 3752 if (isProcedure) { 3754 if ( stmt == null || !(stmt.wasExplicitlyPrepared())) 3755 { 3756 stmt = database.newDRDAStatement(pkgnamcsn); 3757 stmt.setQryprctyp(CodePoint.QRYBLKCTL_DEFAULT); 3758 needPrepareCall = true; 3759 } 3760 3761 stmt.procName = procName; 3762 stmt.outputExpected = outputExpected; 3763 } 3764 else 3765 { 3766 if (stmt == null) 3768 { 3769 invalidValue(CodePoint.PKGNAMCSN); 3770 } 3771 stmt.setQryprctyp(CodePoint.QRYBLKCTL_DEFAULT); 3772 } 3773 3774 stmt.nbrrow = numRows; 3775 stmt.qryrowset = qryrowset; 3776 stmt.blksize = blkSize; 3777 stmt.maxblkext = maxblkext; 3778 stmt.maxrslcnt = maxrslcnt; 3779 stmt.outovropt = outovropt; 3780 stmt.rslsetflg = rslsetflg; 3781 if (pendingStatementTimeout >= 0) { 3782 stmt.getPreparedStatement().setQueryTimeout(pendingStatementTimeout); 3783 pendingStatementTimeout = -1; 3784 } 3785 3786 3787 database.setCurrentStatement(stmt); 3789 3790 boolean hasResultSet; 3791 if (reader.isChainedWithSameID()) 3792 { 3793 hasResultSet = parseEXCSQLSTTobjects(stmt); 3794 } else 3795 { 3796 if (isProcedure && (needPrepareCall)) 3797 { 3798 String prepareString = "call " + stmt.procName +"()"; 3802 if (SanityManager.DEBUG) 3803 trace ("$$$prepareCall is: "+prepareString); 3804 database.getConnection().clearWarnings(); 3805 CallableStatement cs = (CallableStatement ) stmt.prepare(prepareString); 3806 } 3807 stmt.ps.clearWarnings(); 3808 hasResultSet = stmt.execute(); 3809 } 3810 3811 3812 ResultSet rs = null; 3813 if (hasResultSet) 3814 { 3815 rs = stmt.getResultSet(); 3816 } 3817 hasResultSet = (rs != null); 3819 int numResults = 0; 3820 if (hasResultSet) 3821 { 3822 numResults = stmt.getNumResultSets(); 3823 writeRSLSETRM(stmt); 3824 } 3825 3826 boolean sendSQLDTARD = stmt.hasOutputParams() && outputExpected; 3830 if (isProcedure) 3831 { 3832 if (sendSQLDTARD) { 3833 writer.createDssObject(); 3834 writer.startDdm(CodePoint.SQLDTARD); 3835 writer.startDdm(CodePoint.FDODSC); 3836 writeQRYDSC(stmt, true); 3837 writer.endDdm(); 3838 writer.startDdm(CodePoint.FDODTA); 3839 writeFDODTA(stmt); 3840 writer.endDdm(); 3841 writer.endDdmAndDss(); 3842 } 3843 else if (hasResultSet) 3844 writeNullSQLCARDobject(); 3855 } 3856 3857 stmt.finishParams(); 3860 3861 PreparedStatement ps = stmt.getPreparedStatement(); 3862 int rsNum = 0; 3863 do { 3864 if (hasResultSet) 3865 { 3866 stmt.setCurrentDrdaResultSet(rsNum); 3867 stmt.setQryrtndta(true); 3869 if (! isProcedure) 3870 checkWarning(null, ps, null, -1, true, true); 3871 if (rsNum == 0) 3872 writeSQLRSLRD(stmt); 3873 writeOPNQRYRM(true, stmt); 3874 writeSQLCINRD(stmt); 3875 writeQRYDSC(stmt, false); 3876 stmt.rsSuspend(); 3877 3878 3883 if (stmt.getQryprctyp() == CodePoint.LMTBLKPRC) 3884 writeQRYDTA(stmt); 3885 } 3886 else if (! sendSQLDTARD) 3887 { 3888 int updateCount = ps.getUpdateCount(); 3889 if (false && (database.RDBUPDRM_sent == false) && 3890 ! isProcedure) 3891 { 3892 writeRDBUPDRM(); 3893 } 3894 3895 checkWarning(database.getConnection(), stmt.ps, null, updateCount, true, true); 3896 } 3897 3898 } while(hasResultSet && (++rsNum < numResults)); 3899 3900 return; } 3902 3903 3904 3912 private void parseRDBCMTOK() throws DRDAProtocolException 3913 { 3914 boolean rdbcmtok = readBoolean(CodePoint.RDBCMTOK); 3915 if (SanityManager.DEBUG) 3916 trace("rdbcmtok = " + rdbcmtok); 3917 } 3918 3919 3938 private boolean parseEXCSQLSTTobjects(DRDAStatement stmt) throws DRDAProtocolException, SQLException 3939 { 3940 int codePoint; 3941 boolean gotSQLDTA = false, gotEXTDTA = false; 3942 boolean result = false; 3943 do 3944 { 3945 correlationID = reader.readDssHeader(); 3946 while (reader.moreDssData()) 3947 { 3948 codePoint = reader.readLengthAndCodePoint(); 3949 switch(codePoint) 3950 { 3951 case CodePoint.TYPDEFNAM: 3953 setStmtOrDbByteOrder(false, stmt, parseTYPDEFNAM()); 3954 stmt.setTypDefValues(); 3955 break; 3956 case CodePoint.TYPDEFOVR: 3958 parseTYPDEFOVR(stmt); 3959 stmt.setTypDefValues(); 3960 break; 3961 case CodePoint.SQLDTA: 3963 parseSQLDTA(stmt); 3964 gotSQLDTA = true; 3965 break; 3966 case CodePoint.EXTDTA: 3968 readAndSetAllExtParams(stmt, true); 3969 stmt.ps.clearWarnings(); 3970 result = stmt.execute(); 3971 gotEXTDTA = true; 3972 break; 3973 case CodePoint.OUTOVR: 3975 parseOUTOVR(stmt); 3976 break; 3977 default: 3978 invalidCodePoint(codePoint); 3979 } 3980 } 3981 } while (reader.isChainedWithSameID()); 3982 3983 if (! gotSQLDTA) 3985 missingCodePoint(CodePoint.SQLDTA); 3986 3987 if (! gotEXTDTA) { 3988 stmt.ps.clearWarnings(); 3989 result = stmt.execute(); 3990 } 3991 3992 return result; 3993 } 3994 3995 4001 private void writeSQLCINRD(DRDAStatement stmt) throws DRDAProtocolException,SQLException 4002 { 4003 ResultSet rs = null; 4004 PreparedStatement ps = stmt.getPreparedStatement(); 4006 4007 if (!stmt.needsToSendParamData) 4008 rs = stmt.getResultSet(); 4009 4010 writer.createDssObject(); 4011 writer.startDdm(CodePoint.SQLCINRD); 4012 if (sqlamLevel >= MGRLVL_7) 4013 writeSQLDHROW (stmt); 4014 4015 ResultSetMetaData rsmeta = rs.getMetaData(); 4016 int ncols = rsmeta.getColumnCount(); 4017 writer.writeShort(ncols); if (sqlamLevel >= MGRLVL_7) 4019 { 4020 for (int i = 0; i < ncols; i++) 4021 writeSQLDAGRP (rsmeta, null, i, true); 4022 } 4023 else 4024 { 4025 for (int i = 0; i < ncols; i++) 4026 { 4027 writeVCMorVCS(rsmeta.getColumnName(i+1)); 4028 writeVCMorVCS(rsmeta.getColumnLabel(i+1)); 4029 writeVCMorVCS(null); 4030 } 4031 } 4032 writer.endDdmAndDss(); 4033 } 4034 4035 4041 private void writeSQLRSLRD(DRDAStatement stmt) throws DRDAProtocolException,SQLException 4042 { 4043 int numResults = stmt.getNumResultSets(); 4044 4045 writer.createDssObject(); 4046 writer.startDdm(CodePoint.SQLRSLRD); 4047 writer.writeShort(numResults); 4049 for (int i = 0; i < numResults; i ++) 4050 { 4051 writer.writeInt(i); writeVCMorVCS(stmt.getResultSetCursorName(i)); 4053 writer.writeInt(1); 4055 } 4056 writer.endDdmAndDss(); 4057 } 4058 4059 4069 private void writeRSLSETRM(DRDAStatement stmt) throws DRDAProtocolException,SQLException 4070 { 4071 int numResults = stmt.getNumResultSets(); 4072 writer.createDssReply(); 4073 writer.startDdm(CodePoint.RSLSETRM); 4074 writer.writeScalar2Bytes(CodePoint.SVRCOD, 0); 4075 writer.startDdm(CodePoint.PKGSNLST); 4076 4077 for (int i = 0; i < numResults; i++) 4078 writePKGNAMCSN(stmt.getResultSetPkgcnstkn(i).getBytes()); 4079 writer.endDdm(); 4080 writer.endDdmAndDss(); 4081 } 4082 4083 4084 4089 4090 private void parseSQLDTA(DRDAStatement stmt) throws DRDAProtocolException,SQLException 4091 { 4092 try { 4093 parseSQLDTA_work(stmt); 4094 } 4095 catch (SQLException se) 4096 { 4097 skipRemainder(true); 4098 throw se; 4099 } 4100 } 4101 4102 4111 private void parseSQLDTA_work(DRDAStatement stmt) throws DRDAProtocolException,SQLException 4112 { 4113 String strVal; 4114 PreparedStatement ps = stmt.getPreparedStatement(); 4115 int codePoint; 4116 EngineParameterMetaData pmeta = null; 4117 Vector paramDrdaTypes = new Vector (); 4118 Vector paramLens = new Vector (); 4119 ArrayList paramExtPositions = null; 4120 int numVars = 0; 4121 boolean rtnParam = false; 4122 4123 reader.markCollection(); 4124 codePoint = reader.getCodePoint(); 4125 while (codePoint != -1) 4126 { 4127 switch (codePoint) 4128 { 4129 case CodePoint.FDODSC: 4131 while (reader.getDdmLength() > 6) { 4133 int dtaGrpLen = reader.readUnsignedByte(); 4134 int numVarsInGrp = (dtaGrpLen - 3) / 3; 4135 if (SanityManager.DEBUG) 4136 trace("num of vars in this group is: "+numVarsInGrp); 4137 reader.readByte(); reader.readByte(); for (int j = 0; j < numVarsInGrp; j++) 4140 { 4141 paramDrdaTypes.addElement(new Byte (reader.readByte())); 4142 if (SanityManager.DEBUG) 4143 trace("drdaType is: "+ "0x" + 4144 Integer.toHexString(((Byte ) paramDrdaTypes.lastElement()).byteValue())); 4145 int drdaLength = reader.readNetworkShort(); 4146 if (SanityManager.DEBUG) 4147 trace("drdaLength is: "+drdaLength); 4148 paramLens.addElement(new Integer (drdaLength)); 4149 } 4150 } 4151 numVars = paramDrdaTypes.size(); 4152 if (SanityManager.DEBUG) 4153 trace("numVars = " + numVars); 4154 if (ps == null) { 4156 String marks = "(?"; for (int i = 1; i < numVars; i++) 4158 marks += ", ?"; 4159 String prepareString = "call " + stmt.procName + marks + ")"; 4160 if (SanityManager.DEBUG) 4161 trace ("$$ prepareCall is: "+prepareString); 4162 CallableStatement cs = null; 4163 try { 4164 cs = (CallableStatement ) 4165 stmt.prepare(prepareString); 4166 stmt.registerAllOutParams(); 4167 } catch (SQLException se) { 4168 if (! stmt.outputExpected || 4169 (!se.getSQLState().equals(SQLState.LANG_NO_METHOD_FOUND))) 4170 throw se; 4171 if (SanityManager.DEBUG) 4172 trace("****** second try with return parameter..."); 4173 if (numVars == 1) 4175 prepareString = "? = call " + stmt.procName +"()"; 4176 else 4177 prepareString = "? = call " + stmt.procName +"("+marks.substring(3) + ")"; 4178 if (SanityManager.DEBUG) 4179 trace ("$$ prepareCall is: "+prepareString); 4180 try { 4181 cs = (CallableStatement ) stmt.prepare(prepareString); 4182 } catch (SQLException se2) 4183 { 4184 throw se; 4186 } 4187 rtnParam = true; 4188 } 4189 ps = cs; 4190 stmt.ps = ps; 4191 } 4192 4193 pmeta = stmt.getParameterMetaData(); 4194 4195 reader.readBytes(6); break; 4197 case CodePoint.FDODTA: 4199 reader.readByte(); for (int i = 0; i < numVars; i++) 4201 { 4202 4203 if ((((Byte )paramDrdaTypes.elementAt(i)).byteValue() & 0x1) == 0x1) { 4205 int nullData = reader.readUnsignedByte(); 4206 if ((nullData & 0xFF) == FdocaConstants.NULL_DATA) 4207 { 4208 if (SanityManager.DEBUG) 4209 trace("******param null"); 4210 if (pmeta.getParameterMode(i + 1) 4211 != JDBC30Translation.PARAMETER_MODE_OUT ) 4212 ps.setNull(i+1, pmeta.getParameterType(i+1)); 4213 if (stmt.isOutputParam(i+1)) 4214 stmt.registerOutParam(i+1); 4215 continue; 4216 } 4217 } 4218 4219 paramExtPositions = readAndSetParams(i, stmt, 4221 ((Byte )paramDrdaTypes.elementAt(i)).byteValue(), 4222 pmeta, 4223 paramExtPositions, 4224 ((Integer )(paramLens.elementAt(i))).intValue()); 4225 } 4226 stmt.cliParamExtPositions = paramExtPositions; 4227 stmt.cliParamDrdaTypes = paramDrdaTypes; 4228 stmt.cliParamLens = paramLens; 4229 break; 4230 case CodePoint.EXTDTA: 4231 readAndSetAllExtParams(stmt, false); 4232 break; 4233 default: 4234 invalidCodePoint(codePoint); 4235 4236 } 4237 codePoint = reader.getCodePoint(); 4238 } 4239 4240 4241 } 4242 4243 private int getByteOrder() 4244 { 4245 DRDAStatement stmt = database.getCurrentStatement(); 4246 return ((stmt != null && stmt.typDefNam != null) ? stmt.byteOrder : database.byteOrder); 4247 } 4248 4249 4262 private ArrayList readAndSetParams(int i, DRDAStatement stmt, int 4263 drdaType, EngineParameterMetaData pmeta, 4264 ArrayList paramExtPositions, 4265 int paramLenNumBytes) 4266 throws DRDAProtocolException, SQLException 4267 { 4268 PreparedStatement ps = stmt.getPreparedStatement(); 4269 drdaType = ((drdaType | 0x01) & 0x000000ff); 4271 4272 if (ps instanceof CallableStatement ) 4273 { 4274 if (stmt.isOutputParam(i+1)) 4275 { 4276 CallableStatement cs = (CallableStatement ) ps; 4277 cs.registerOutParameter(i+1, stmt.getOutputParamType(i+1)); 4278 } 4279 } 4280 4281 switch (drdaType) 4282 { 4283 case DRDAConstants.DRDA_TYPE_NSMALL: 4284 { 4285 short paramVal = (short) reader.readShort(getByteOrder()); 4286 if (SanityManager.DEBUG) 4287 trace("short parameter value is: "+paramVal); 4288 if (pmeta.getParameterType(i+1) == JDBC30Translation.BOOLEAN) 4290 ps.setBoolean(i+1, (paramVal == 1)); 4291 else 4292 ps.setShort(i+1, paramVal); 4293 break; 4294 } 4295 case DRDAConstants.DRDA_TYPE_NINTEGER: 4296 { 4297 int paramVal = reader.readInt(getByteOrder()); 4298 if (SanityManager.DEBUG) 4299 trace("integer parameter value is: "+paramVal); 4300 ps.setInt(i+1, paramVal); 4301 break; 4302 } 4303 case DRDAConstants.DRDA_TYPE_NINTEGER8: 4304 { 4305 long paramVal = reader.readLong(getByteOrder()); 4306 if (SanityManager.DEBUG) 4307 trace("parameter value is: "+paramVal); 4308 ps.setLong(i+1, paramVal); 4309 break; 4310 } 4311 case DRDAConstants.DRDA_TYPE_NFLOAT4: 4312 { 4313 float paramVal = reader.readFloat(getByteOrder()); 4314 if (SanityManager.DEBUG) 4315 trace("parameter value is: "+paramVal); 4316 ps.setFloat(i+1, paramVal); 4317 break; 4318 } 4319 case DRDAConstants.DRDA_TYPE_NFLOAT8: 4320 { 4321 double paramVal = reader.readDouble(getByteOrder()); 4322 if (SanityManager.DEBUG) 4323 trace("nfloat8 parameter value is: "+paramVal); 4324 ps.setDouble(i+1, paramVal); 4325 break; 4326 } 4327 case DRDAConstants.DRDA_TYPE_NDECIMAL: 4328 { 4329 int precision = (paramLenNumBytes >> 8) & 0xff; 4330 int scale = paramLenNumBytes & 0xff; 4331 BigDecimal paramVal = reader.readBigDecimal(precision, scale); 4332 if (SanityManager.DEBUG) 4333 trace("ndecimal parameter value is: "+paramVal); 4334 ps.setBigDecimal(i+1, paramVal); 4335 break; 4336 } 4337 case DRDAConstants.DRDA_TYPE_NDATE: 4338 { 4339 String paramVal = reader.readStringData(10).trim(); if (SanityManager.DEBUG) 4341 trace("ndate parameter value is: \""+paramVal+"\""); 4342 try { 4343 ps.setDate(i+1, java.sql.Date.valueOf(paramVal)); 4344 } catch (java.lang.IllegalArgumentException e) { 4345 throw new SQLException (SQLState.LANG_DATE_SYNTAX_EXCEPTION, 4351 SQLState.LANG_DATE_SYNTAX_EXCEPTION.substring(0,5)); 4352 } 4353 break; 4354 } 4355 case DRDAConstants.DRDA_TYPE_NTIME: 4356 { 4357 String paramVal = reader.readStringData(8).trim(); if (SanityManager.DEBUG) 4359 trace("ntime parameter value is: "+paramVal); 4360 try { 4361 ps.setTime(i+1, java.sql.Time.valueOf(paramVal)); 4362 } catch (java.lang.IllegalArgumentException e) { 4363 throw new SQLException (SQLState.LANG_DATE_SYNTAX_EXCEPTION, 4364 SQLState.LANG_DATE_SYNTAX_EXCEPTION.substring(0,5)); 4365 } 4366 break; 4367 } 4368 case DRDAConstants.DRDA_TYPE_NTIMESTAMP: 4369 { 4370 String paramVal = reader.readStringData(26).trim(); if (SanityManager.DEBUG) 4374 trace("ntimestamp parameter value is: "+paramVal); 4375 try { 4376 String tsString = paramVal.substring(0,10) + " " + 4377 paramVal.substring(11,19).replace('.', ':') + 4378 paramVal.substring(19); 4379 if (SanityManager.DEBUG) 4380 trace("tsString is: "+tsString); 4381 ps.setTimestamp(i+1, java.sql.Timestamp.valueOf(tsString)); 4382 } catch (java.lang.IllegalArgumentException e1) { 4383 throw new SQLException (SQLState.LANG_DATE_SYNTAX_EXCEPTION, 4385 SQLState.LANG_DATE_SYNTAX_EXCEPTION.substring(0,5)); 4386 } catch (java.lang.StringIndexOutOfBoundsException e2) { 4387 throw new SQLException (SQLState.LANG_DATE_SYNTAX_EXCEPTION, 4389 SQLState.LANG_DATE_SYNTAX_EXCEPTION.substring(0,5)); 4390 } 4391 break; 4392 } 4393 case DRDAConstants.DRDA_TYPE_NCHAR: 4394 case DRDAConstants.DRDA_TYPE_NVARCHAR: 4395 case DRDAConstants.DRDA_TYPE_NLONG: 4396 case DRDAConstants.DRDA_TYPE_NVARMIX: 4397 case DRDAConstants.DRDA_TYPE_NLONGMIX: 4398 { 4399 String paramVal = reader.readLDStringData(stmt.ccsidMBCEncoding); 4400 if (SanityManager.DEBUG) 4401 trace("char/varchar parameter value is: "+paramVal); 4402 ps.setString(i+1, paramVal); 4403 break; 4404 } 4405 case DRDAConstants.DRDA_TYPE_NFIXBYTE: 4406 { 4407 byte[] paramVal = reader.readBytes(); 4408 if (SanityManager.DEBUG) 4409 trace("fix bytes parameter value is: "+ convertToHexString(paramVal)); 4410 ps.setBytes(i+1, paramVal); 4411 break; 4412 } 4413 case DRDAConstants.DRDA_TYPE_NVARBYTE: 4414 case DRDAConstants.DRDA_TYPE_NLONGVARBYTE: 4415 { 4416 int length = reader.readNetworkShort(); if (SanityManager.DEBUG) 4418 trace("===== binary param length is: " + length); 4419 byte[] paramVal = reader.readBytes(length); 4420 ps.setBytes(i+1, paramVal); 4421 break; 4422 } 4423 case DRDAConstants.DRDA_TYPE_NLOBBYTES: 4424 case DRDAConstants.DRDA_TYPE_NLOBCMIXED: 4425 case DRDAConstants.DRDA_TYPE_NLOBCSBCS: 4426 case DRDAConstants.DRDA_TYPE_NLOBCDBCS: 4427 { 4428 long length = readLobLength(paramLenNumBytes); 4429 if (length != 0) { 4431 if (paramExtPositions == null) 4432 paramExtPositions = new ArrayList (); 4433 paramExtPositions.add(new Integer (i)); 4434 } 4435 else 4436 { 4437 if (drdaType == DRDAConstants.DRDA_TYPE_NLOBBYTES) 4438 ps.setBytes(i+1, new byte[0]); 4439 else 4440 ps.setString(i+1, ""); 4441 } 4442 break; 4443 } 4444 default: 4445 { 4446 String paramVal = reader.readLDStringData(stmt.ccsidMBCEncoding); 4447 if (SanityManager.DEBUG) 4448 trace("default type parameter value is: "+paramVal); 4449 ps.setObject(i+1, paramVal); 4450 } 4451 } 4452 return paramExtPositions; 4453 } 4454 4455 private long readLobLength(int extLenIndicator) 4456 throws DRDAProtocolException 4457 { 4458 switch (extLenIndicator) 4459 { 4460 case 0x8002: 4461 return (long) reader.readNetworkShort(); 4462 case 0x8004: 4463 return (long) reader.readNetworkInt(); 4464 case 0x8006: 4465 return (long) reader.readNetworkSixByteLong(); 4466 case 0x8008: 4467 return (long) reader.readNetworkLong(); 4468 default: 4469 throwSyntaxrm(CodePoint.SYNERRCD_INCORRECT_EXTENDED_LEN, extLenIndicator); 4470 return 0L; 4471 } 4472 4473 4474 4475 } 4476 4477 4478 private void readAndSetAllExtParams(final DRDAStatement stmt, final boolean streamLOB) 4479 throws SQLException , DRDAProtocolException 4480 { 4481 int numExt = stmt.cliParamExtPositions.size(); 4482 for (int i = 0; i < stmt.cliParamExtPositions.size(); i++) 4483 { 4484 int paramPos = ((Integer ) (stmt.cliParamExtPositions).get(i)).intValue(); 4485 final boolean doStreamLOB = (streamLOB && i == numExt -1); 4486 readAndSetExtParam(paramPos, 4487 stmt, 4488 ((Byte )stmt.cliParamDrdaTypes.elementAt(paramPos)).intValue(), 4489 ((Integer )(stmt.cliParamLens.elementAt(paramPos))).intValue(), 4490 doStreamLOB); 4491 if (i < numExt -1) 4493 { 4494 correlationID = reader.readDssHeader(); 4495 int codePoint = reader.readLengthAndCodePoint(); 4496 } 4497 } 4498 4499 } 4500 4501 4502 4511 private void readAndSetExtParam( int i, DRDAStatement stmt, 4512 int drdaType, int extLen, boolean streamLOB) 4513 throws DRDAProtocolException, SQLException 4514 { 4515 PreparedStatement ps = stmt.getPreparedStatement(); 4516 drdaType = (drdaType & 0x000000ff); boolean checkNullability = false; 4518 if (sqlamLevel >= MGRLVL_7 && 4519 FdocaConstants.isNullable(drdaType)) 4520 checkNullability = true; 4521 4522 try { 4523 final byte[] paramBytes; 4524 final String paramString; 4525 4526 switch (drdaType) 4527 { 4528 case DRDAConstants.DRDA_TYPE_LOBBYTES: 4529 case DRDAConstants.DRDA_TYPE_NLOBBYTES: 4530 paramString = ""; 4531 final boolean useSetBinaryStream = 4532 stmt.getParameterMetaData().getParameterType(i+1)==Types.BLOB; 4533 4534 if (streamLOB && useSetBinaryStream) { 4535 paramBytes = null; 4536 final EXTDTAReaderInputStream stream = 4537 reader.getEXTDTAReaderInputStream(checkNullability); 4538 if (stream==null) { 4539 ps.setBytes(i+1, null); 4540 } else { 4541 ps.setBinaryStream(i+1, stream, (int) stream.getLength()); 4542 } 4543 4544 if (SanityManager.DEBUG) { 4545 if (stream==null) { 4546 trace("parameter value : NULL"); 4547 } else { 4548 trace("parameter value will be streamed"); 4549 } 4550 } 4551 } else { 4552 paramBytes = reader.getExtData(checkNullability); 4553 if (paramBytes==null || !useSetBinaryStream) { 4554 ps.setBytes(i+1, paramBytes); 4555 } else { 4556 ps.setBinaryStream(i+1, new ByteArrayInputStream (paramBytes), 4557 paramBytes.length); 4558 } 4559 if (SanityManager.DEBUG) { 4560 if (paramBytes==null) { 4561 trace("parameter value : NULL"); 4562 } else { 4563 trace("parameter value is a LOB with length:" + 4564 paramBytes.length); 4565 } 4566 } 4567 } 4568 break; 4569 case DRDAConstants.DRDA_TYPE_LOBCSBCS: 4570 case DRDAConstants.DRDA_TYPE_NLOBCSBCS: 4571 paramBytes = reader.getExtData(checkNullability); 4572 paramString = new String (paramBytes, stmt.ccsidSBCEncoding); 4573 if (SanityManager.DEBUG) 4574 trace("parameter value is: "+ paramString); 4575 ps.setString(i+1,paramString); 4576 break; 4577 case DRDAConstants.DRDA_TYPE_LOBCDBCS: 4578 case DRDAConstants.DRDA_TYPE_NLOBCDBCS: 4579 paramBytes = reader.getExtData(checkNullability); 4580 paramString = new String (paramBytes, stmt.ccsidDBCEncoding ); 4581 if (SanityManager.DEBUG) 4582 trace("parameter value is: "+ paramString); 4583 ps.setString(i+1,paramString); 4584 break; 4585 case DRDAConstants.DRDA_TYPE_LOBCMIXED: 4586 case DRDAConstants.DRDA_TYPE_NLOBCMIXED: 4587 paramBytes = reader.getExtData(checkNullability); 4588 paramString = new String (paramBytes, stmt.ccsidMBCEncoding); 4589 if (SanityManager.DEBUG) 4590 trace("parameter value is: "+ paramString); 4591 ps.setString(i+1,paramString); 4592 break; 4593 default: 4594 paramBytes = null; 4595 paramString = ""; 4596 4597 invalidValue(drdaType); 4598 } 4599 4600 } 4601 catch (java.io.UnsupportedEncodingException e) { 4602 throw new SQLException (e.getMessage()); 4603 } 4604 } 4605 4606 4623 private int parseEXCSQLIMM() throws DRDAProtocolException,SQLException 4624 { 4625 int codePoint; 4626 reader.markCollection(); 4627 Pkgnamcsn pkgnamcsn = null; 4628 codePoint = reader.getCodePoint(); 4629 while (codePoint != -1) 4630 { 4631 switch (codePoint) 4632 { 4633 case CodePoint.RDBNAM: 4635 setDatabase(CodePoint.EXCSQLIMM); 4636 break; 4637 case CodePoint.PKGNAMCSN: 4639 pkgnamcsn = parsePKGNAMCSN(); 4640 break; 4641 case CodePoint.RDBCMTOK: 4642 parseRDBCMTOK(); 4643 break; 4644 case CodePoint.MONITOR: 4646 parseMONITOR(); 4647 break; 4648 default: 4649 invalidCodePoint(codePoint); 4650 4651 } 4652 codePoint = reader.getCodePoint(); 4653 } 4654 DRDAStatement drdaStmt = database.getDefaultStatement(pkgnamcsn); 4655 drdaStmt.initialize(); 4657 String sqlStmt = parseEXECSQLIMMobjects(); 4658 Statement statement = drdaStmt.getStatement(); 4659 statement.clearWarnings(); 4660 if (pendingStatementTimeout >= 0) { 4661 statement.setQueryTimeout(pendingStatementTimeout); 4662 pendingStatementTimeout = -1; 4663 } 4664 int updCount = statement.executeUpdate(sqlStmt); 4665 return updCount; 4666 } 4667 4668 4683 private boolean parseEXCSQLSET() throws DRDAProtocolException,SQLException 4684 { 4685 4686 int codePoint; 4687 reader.markCollection(); 4688 4689 4690 codePoint = reader.getCodePoint(); 4691 while (codePoint != -1) 4692 { 4693 switch (codePoint) 4694 { 4695 case CodePoint.RDBNAM: 4697 setDatabase(CodePoint.EXCSQLSET); 4698 break; 4699 case CodePoint.PKGNAMCT: 4701 String pkgnamct = parsePKGNAMCT(); 4704 break; 4705 case CodePoint.MONITOR: 4707 parseMONITOR(); 4708 break; 4709 case CodePoint.PKGNAMCSN: 4711 Pkgnamcsn pkgnamcsn = parsePKGNAMCSN(); 4717 break; 4718 default: 4719 invalidCodePoint(codePoint); 4720 4721 } 4722 codePoint = reader.getCodePoint(); 4723 } 4724 4725 parseEXCSQLSETobjects(); 4726 return true; 4727 } 4728 4729 4746 private String parseEXECSQLIMMobjects() throws DRDAProtocolException, SQLException 4747 { 4748 String sqlStmt = null; 4749 int codePoint; 4750 DRDAStatement stmt = database.getDefaultStatement(); 4751 do 4752 { 4753 correlationID = reader.readDssHeader(); 4754 while (reader.moreDssData()) 4755 { 4756 codePoint = reader.readLengthAndCodePoint(); 4757 switch(codePoint) 4758 { 4759 case CodePoint.TYPDEFNAM: 4761 setStmtOrDbByteOrder(false, stmt, parseTYPDEFNAM()); 4762 break; 4763 case CodePoint.TYPDEFOVR: 4765 parseTYPDEFOVR(stmt); 4766 break; 4767 case CodePoint.SQLSTT: 4769 sqlStmt = parseEncodedString(); 4770 if (SanityManager.DEBUG) 4771 trace("sqlStmt = " + sqlStmt); 4772 break; 4773 default: 4774 invalidCodePoint(codePoint); 4775 } 4776 } 4777 } while (reader.isChainedWithSameID()); 4778 4779 if (sqlStmt == null) 4781 missingCodePoint(CodePoint.SQLSTT); 4782 return sqlStmt; 4783 } 4784 4785 4797 private void parseEXCSQLSETobjects() 4798 throws DRDAProtocolException, SQLException 4799 { 4800 4801 boolean gotSqlStt = false; 4802 boolean hadUnrecognizedStmt = false; 4803 4804 String sqlStmt = null; 4805 int codePoint; 4806 DRDAStatement drdaStmt = database.getDefaultStatement(); 4807 drdaStmt.initialize(); 4808 4809 do 4810 { 4811 correlationID = reader.readDssHeader(); 4812 while (reader.moreDssData()) 4813 { 4814 4815 codePoint = reader.readLengthAndCodePoint(); 4816 4817 switch(codePoint) 4818 { 4819 case CodePoint.TYPDEFNAM: 4821 setStmtOrDbByteOrder(false, drdaStmt, parseTYPDEFNAM()); 4822 break; 4823 case CodePoint.TYPDEFOVR: 4825 parseTYPDEFOVR(drdaStmt); 4826 break; 4827 case CodePoint.SQLSTT: 4829 sqlStmt = parseEncodedString(); 4830 if (sqlStmt != null) 4831 gotSqlStt = true; 4833 4834 if (sqlStmt.startsWith(TIMEOUT_STATEMENT)) { 4835 String timeoutString = sqlStmt.substring(TIMEOUT_STATEMENT.length()); 4836 pendingStatementTimeout = Integer.valueOf(timeoutString).intValue(); 4837 break; 4838 } 4839 4840 if (canIgnoreStmt(sqlStmt)) { 4841 break; 4854 } 4855 4856 if (SanityManager.DEBUG) 4857 trace("sqlStmt = " + sqlStmt); 4858 4859 drdaStmt.initialize(); 4861 drdaStmt.getStatement().clearWarnings(); 4862 try { 4863 drdaStmt.getStatement().executeUpdate(sqlStmt); 4864 } catch (SQLException e) { 4865 4866 if (e.getSQLState().equals(SYNTAX_ERR)) 4871 hadUnrecognizedStmt = true; 4872 else 4873 throw e; 4875 } 4876 break; 4877 default: 4878 invalidCodePoint(codePoint); 4879 } 4880 } 4881 4882 } while (reader.isChainedWithSameID()); 4883 4884 if (!gotSqlStt) 4886 missingCodePoint(CodePoint.SQLSTT); 4887 4888 if (hadUnrecognizedStmt) { 4893 SQLWarning warn = new SQLWarning ("One or more SET statements " + 4894 "not recognized.", "01000"); 4895 throw warn; 4896 } 4898 return; 4899 } 4900 4901 private boolean canIgnoreStmt(String stmt) 4902 { 4903 if (stmt.indexOf("SET CLIENT") != -1) 4904 return true; 4905 return false; 4906 } 4907 4908 4917 private void writeRDBUPDRM() throws DRDAProtocolException 4918 { 4919 database.RDBUPDRM_sent = true; 4920 writer.createDssReply(); 4921 writer.startDdm(CodePoint.RDBUPDRM); 4922 writer.writeScalar2Bytes(CodePoint.SVRCOD, CodePoint.SVRCOD_INFO); 4923 writeRDBNAM(database.dbName); 4924 writer.endDdmAndDss(); 4925 } 4926 4927 4928 private String parsePKGNAMCT() throws DRDAProtocolException 4929 { 4930 reader.skipBytes(); 4931 return null; 4932 } 4933 4934 4946 private Pkgnamcsn parsePKGNAMCSN() throws DRDAProtocolException 4947 { 4948 if (reader.getDdmLength() == CodePoint.PKGNAMCSN_LEN) 4949 { 4950 reader.readString(rdbnam, CodePoint.RDBNAM_LEN, true); 4952 if (SanityManager.DEBUG) 4953 trace("rdbnam = " + rdbnam); 4954 4955 reader.readString(rdbcolid, CodePoint.RDBCOLID_LEN, true); 4956 if (SanityManager.DEBUG) 4957 trace("rdbcolid = " + rdbcolid); 4958 4959 reader.readString(pkgid, CodePoint.PKGID_LEN, true); 4960 if (SanityManager.DEBUG) 4961 trace("pkgid = " + pkgid); 4962 4963 reader.readString(pkgcnstkn, CodePoint.PKGCNSTKN_LEN, false); 4966 if (SanityManager.DEBUG) 4967 trace("pkgcnstkn = " + pkgcnstkn); 4968 4969 pkgsn = reader.readNetworkShort(); 4970 if (SanityManager.DEBUG) 4971 trace("pkgsn = " + pkgsn); 4972 } 4973 else { 4975 int length = reader.readNetworkShort(); 4976 if (length < CodePoint.RDBNAM_LEN || length > CodePoint.MAX_NAME) 4977 badObjectLength(CodePoint.RDBNAM); 4978 reader.readString(rdbnam, length, true); 4979 if (SanityManager.DEBUG) 4980 trace("rdbnam = " + rdbnam); 4981 4982 length = reader.readNetworkShort(); 4984 reader.readString(rdbcolid, length, true); 4985 if (SanityManager.DEBUG) 4986 trace("rdbcolid = " + rdbcolid); 4987 4988 length = reader.readNetworkShort(); 4989 if (length != CodePoint.PKGID_LEN) 4990 badObjectLength(CodePoint.PKGID); 4991 reader.readString(pkgid, CodePoint.PKGID_LEN, true); 4992 if (SanityManager.DEBUG) 4993 trace("pkgid = " + pkgid); 4994 4995 reader.readString(pkgcnstkn, CodePoint.PKGCNSTKN_LEN, false); 4996 if (SanityManager.DEBUG) 4997 trace("pkgcnstkn = " + pkgcnstkn); 4998 4999 pkgsn = reader.readNetworkShort(); 5000 if (SanityManager.DEBUG) 5001 trace("pkgsn = " + pkgsn); 5002 } 5003 5004 if ((prevPkgnamcsn == null) || 5009 rdbnam.wasModified() || 5010 rdbcolid.wasModified() || 5011 pkgid.wasModified() || 5012 pkgcnstkn.wasModified() || 5013 (prevPkgnamcsn.getPkgsn() != pkgsn)) 5014 { 5015 byte[] token = new byte[pkgcnstkn.length()]; 5019 System.arraycopy(pkgcnstkn.getBytes(), 0, token, 0, token.length); 5020 5021 prevPkgnamcsn = 5022 new Pkgnamcsn(rdbnam.toString(), rdbcolid.toString(), 5023 pkgid.toString(), pkgsn, 5024 new ConsistencyToken(token)); 5025 } 5026 5027 return prevPkgnamcsn; 5028 } 5029 5030 5034 private String parseSQLSTTDss() throws DRDAProtocolException 5035 { 5036 correlationID = reader.readDssHeader(); 5037 int codePoint = reader.readLengthAndCodePoint(); 5038 String strVal = parseEncodedString(); 5039 if (SanityManager.DEBUG) 5040 trace("SQL Statement = " + strVal); 5041 return strVal; 5042 } 5043 5044 5050 private String parseEncodedString() throws DRDAProtocolException 5051 { 5052 if (sqlamLevel < 7) 5053 return parseVCMorVCS(); 5054 else 5055 return parseNOCMorNOCS(); 5056 } 5057 5058 5069 private String parseVCMorVCS() throws DRDAProtocolException 5070 { 5071 String strVal = null; 5072 int vcm_length = reader.readNetworkShort(); 5073 if (vcm_length > 0) 5074 strVal = parseCcsidMBC(vcm_length); 5075 int vcs_length = reader.readNetworkShort(); 5076 if (vcs_length > 0) 5077 { 5078 if (strVal != null) 5079 agentError ("Both VCM and VCS have lengths > 0"); 5080 strVal = parseCcsidSBC(vcs_length); 5081 } 5082 return strVal; 5083 } 5084 5097 private String parseNOCMorNOCS() throws DRDAProtocolException 5098 { 5099 byte nocm_nullByte = reader.readByte(); 5100 String strVal = null; 5101 int length; 5102 if (nocm_nullByte != NULL_VALUE) 5103 { 5104 length = reader.readNetworkInt(); 5105 strVal = parseCcsidMBC(length); 5106 } 5107 byte nocs_nullByte = reader.readByte(); 5108 if (nocs_nullByte != NULL_VALUE) 5109 { 5110 if (strVal != null) 5111 agentError("Both CM and CS are non null"); 5112 length = reader.readNetworkInt(); 5113 strVal = parseCcsidSBC(length); 5114 } 5115 return strVal; 5116 } 5117 5123 private String parseCcsidMBC(int length) throws DRDAProtocolException 5124 { 5125 String strVal = null; 5126 DRDAStatement currentStatement; 5127 5128 currentStatement = database.getCurrentStatement(); 5129 if (currentStatement == null) 5130 { 5131 currentStatement = database.getDefaultStatement(); 5132 currentStatement.initialize(); 5133 } 5134 String ccsidMBCEncoding = currentStatement.ccsidMBCEncoding; 5135 5136 if (length == 0) 5137 return null; 5138 byte [] byteStr = reader.readBytes(length); 5139 if (ccsidMBCEncoding != null) 5140 { 5141 try { 5142 strVal = new String (byteStr, 0, length, ccsidMBCEncoding); 5143 } catch (UnsupportedEncodingException e) { 5144 agentError("Unsupported encoding " + ccsidMBCEncoding + 5145 "in parseCcsidMBC"); 5146 } 5147 } 5148 else 5149 agentError("Attempt to decode mixed byte string without CCSID being set"); 5150 return strVal; 5151 } 5152 5158 private String parseCcsidSBC(int length) throws DRDAProtocolException 5159 { 5160 String strVal = null; 5161 DRDAStatement currentStatement; 5162 5163 currentStatement = database.getCurrentStatement(); 5164 if (currentStatement == null) 5165 { 5166 currentStatement = database.getDefaultStatement(); 5167 currentStatement.initialize(); 5168 } 5169 String ccsidSBCEncoding = currentStatement.ccsidSBCEncoding; 5170 System.out.println("ccsidSBCEncoding - " + ccsidSBCEncoding); 5171 5172 if (length == 0) 5173 return null; 5174 byte [] byteStr = reader.readBytes(length); 5175 if (ccsidSBCEncoding != null) 5176 { 5177 try { 5178 strVal = new String (byteStr, 0, length, ccsidSBCEncoding); 5179 } catch (UnsupportedEncodingException e) { 5180 agentError("Unsupported encoding " + ccsidSBCEncoding + 5181 "in parseCcsidSBC"); 5182 } 5183 } 5184 else 5185 agentError("Attempt to decode single byte string without CCSID being set"); 5186 return strVal; 5187 } 5188 5200 private DRDAStatement parseCLSQRY() throws DRDAProtocolException, SQLException 5201 { 5202 Pkgnamcsn pkgnamcsn = null; 5203 reader.markCollection(); 5204 long qryinsid = 0; 5205 boolean gotQryinsid = false; 5206 5207 int codePoint = reader.getCodePoint(); 5208 while (codePoint != -1) 5209 { 5210 switch (codePoint) 5211 { 5212 case CodePoint.RDBNAM: 5214 setDatabase(CodePoint.CLSQRY); 5215 break; 5216 case CodePoint.PKGNAMCSN: 5218 pkgnamcsn = parsePKGNAMCSN(); 5219 break; 5220 case CodePoint.QRYINSID: 5221 qryinsid = reader.readNetworkLong(); 5222 gotQryinsid = true; 5223 break; 5224 case CodePoint.MONITOR: 5226 parseMONITOR(); 5227 break; 5228 default: 5229 invalidCodePoint(codePoint); 5230 } 5231 codePoint = reader.getCodePoint(); 5232 } 5233 if (pkgnamcsn == null) 5235 missingCodePoint(CodePoint.PKGNAMCSN); 5236 if (sqlamLevel >= MGRLVL_7 && !gotQryinsid) 5237 missingCodePoint(CodePoint.QRYINSID); 5238 5239 DRDAStatement stmt = database.getDRDAStatement(pkgnamcsn); 5240 if (stmt == null) 5241 { 5242 invalidValue(CodePoint.PKGNAMCSN); 5244 } 5245 5246 if (stmt.wasExplicitlyClosed()) 5247 { 5248 pkgnamcsn = null; 5257 } 5258 5259 stmt.CLSQRY(); 5260 5261 return stmt; 5262 } 5263 5264 5269 private void parseMONITOR() 5270 throws DRDAProtocolException 5271 { 5272 5273 reader.skipBytes(); 5275 return; 5276 5277 } 5278 5279 private void writeSQLCARDs(SQLException e, int updateCount) 5280 throws DRDAProtocolException 5281 { 5282 writeSQLCARDs(e, updateCount, false); 5283 } 5284 5285 private void writeSQLCARDs(SQLException e, int updateCount, boolean sendSQLERRRM) 5286 throws DRDAProtocolException 5287 { 5288 5289 int severity = CodePoint.SVRCOD_INFO; 5290 if (e == null) 5291 { 5292 writeSQLCARD(e,severity, updateCount, 0); 5293 return; 5294 } 5295 5296 5299 severity = getExceptionSeverity(e); 5301 if (severity > CodePoint.SVRCOD_ERROR) 5302 { 5303 writeCMDCHKRM(severity); 5309 } 5310 else if (sendSQLERRRM) 5311 { 5312 writeSQLERRRM(severity); 5313 } 5314 writeSQLCARD(e,severity, updateCount, 0); 5315 } 5316 5317 private int getSqlCode(int severity) 5318 { 5319 if (severity == CodePoint.SVRCOD_WARNING) return 100; else if (severity == CodePoint.SVRCOD_INFO) 5322 return 0; 5323 else 5324 return -1; 5325 } 5326 5327 private void writeSQLCARD(SQLException e,int severity, 5328 int updateCount, long rowCount ) throws DRDAProtocolException 5329 { 5330 writer.createDssObject(); 5331 writer.startDdm(CodePoint.SQLCARD); 5332 writeSQLCAGRP(e, getSqlCode(severity), updateCount, rowCount); 5333 writer.endDdmAndDss(); 5334 5335 if (e != null) { 5337 String sqlState = e.getSQLState(); 5338 if (sqlState.regionMatches(0, 5339 SQLState.CLOUDSCAPE_SYSTEM_SHUTDOWN, 0, 5)) { 5340 try { 5343 server.startNetworkServer(); 5344 } catch (Exception restart) 5345 {} 5348 } 5349 } 5350 5351 } 5352 5353 5358 private void writeNullSQLCARDobject() 5359 throws DRDAProtocolException 5360 { 5361 writer.createDssObject(); 5362 writer.startDdm(CodePoint.SQLCARD); 5363 writeSQLCAGRP(nullSQLState, 0, 0, 0); 5364 writer.endDdmAndDss(); 5365 } 5366 5376 private void writeSQLERRRM(int severity) throws DRDAProtocolException 5377 { 5378 writer.createDssReply(); 5379 writer.startDdm(CodePoint.SQLERRRM); 5380 writer.writeScalar2Bytes(CodePoint.SVRCOD, severity); 5381 writer.endDdmAndDss (); 5382 5383 } 5384 5385 5395 private void writeCMDCHKRM(int severity) throws DRDAProtocolException 5396 { 5397 writer.createDssReply(); 5398 writer.startDdm(CodePoint.CMDCHKRM); 5399 writer.writeScalar2Bytes(CodePoint.SVRCOD, severity); 5400 writer.endDdmAndDss (); 5401 5402 } 5403 5404 5409 private int getExceptionSeverity (SQLException e) 5410 { 5411 int severity= CodePoint.SVRCOD_INFO; 5412 5413 if (e == null) 5414 return severity; 5415 5416 int ec = e.getErrorCode(); 5417 switch (ec) 5418 { 5419 case ExceptionSeverity.STATEMENT_SEVERITY: 5420 case ExceptionSeverity.TRANSACTION_SEVERITY: 5421 severity = CodePoint.SVRCOD_ERROR; 5422 break; 5423 case ExceptionSeverity.WARNING_SEVERITY: 5424 severity = CodePoint.SVRCOD_WARNING; 5425 break; 5426 case ExceptionSeverity.SESSION_SEVERITY: 5427 case ExceptionSeverity.DATABASE_SEVERITY: 5428 case ExceptionSeverity.SYSTEM_SEVERITY: 5429 severity = CodePoint.SVRCOD_SESDMG; 5430 break; 5431 default: 5432 String sqlState = e.getSQLState(); 5433 if (sqlState != null && sqlState.startsWith("01")) severity = CodePoint.SVRCOD_WARNING; 5435 else 5436 severity = CodePoint.SVRCOD_ERROR; 5437 } 5438 5439 return severity; 5440 5441 } 5442 5466 private void writeSQLCAGRP(SQLException e, int sqlcode, int updateCount, 5467 long rowCount) throws DRDAProtocolException 5468 { 5469 if (e == null) { 5470 writeSQLCAGRP(nullSQLState, sqlcode, updateCount, rowCount); 5473 return; 5474 } 5475 5476 if (rowCount < 0 && updateCount < 0) 5477 { 5478 writer.writeByte(CodePoint.NULLDATA); 5479 return; 5480 } 5481 5482 if (SanityManager.DEBUG && server.debugOutput && sqlcode < 0) { 5483 trace("handle SQLException here"); 5484 trace("reason is: "+e.getMessage()); 5485 trace("SQLState is: "+e.getSQLState()); 5486 trace("vendorCode is: "+e.getErrorCode()); 5487 trace("nextException is: "+e.getNextException()); 5488 server.consoleExceptionPrint(e); 5489 trace("wrapping SQLException into SQLCARD..."); 5490 } 5491 5492 writer.writeByte(0); 5494 5495 writer.writeInt(sqlcode); 5497 5498 writer.writeString(e.getSQLState()); 5500 5501 writer.writeBytes(server.prdIdBytes_); 5504 5505 writeSQLCAXGRP(updateCount, rowCount, buildSqlerrmc(e), e.getNextException()); 5507 } 5508 5509 5537 5538 private void writeSQLCAGRP(byte[] sqlState, int sqlcode, 5539 int updateCount, long rowCount) throws DRDAProtocolException 5540 { 5541 if (rowCount < 0 && updateCount < 0) { 5542 writer.writeByte(CodePoint.NULLDATA); 5543 return; 5544 } 5545 5546 writer.writeByte(0); 5548 5549 writer.writeInt(sqlcode); 5551 5552 writer.writeBytes(sqlState); 5554 5555 writer.writeBytes(server.prdIdBytes_); 5557 5558 writeSQLCAXGRP(updateCount, rowCount, null, null); 5560 } 5561 5562 5563 5573 private static String SQLERRMC_TOKEN_DELIMITER = new String (new char[] {(char)20}); 5574 5575 5579 private static String SQLERRMC_PREFORMATTED_MESSAGE_DELIMITER = new String ("::"); 5580 5581 5608 private String buildSqlerrmc (SQLException se) 5609 { 5610 boolean severe = (se.getErrorCode() >= ExceptionSeverity.SESSION_SEVERITY); 5611 String sqlerrmc = null; 5612 5613 se = Util.getExceptionFactory().getArgumentFerry( se ); 5615 5616 if (se instanceof EmbedSQLException && ! severe) 5617 sqlerrmc = buildTokenizedSqlerrmc((EmbedSQLException) se); 5618 else { 5619 sqlerrmc = buildPreformattedSqlerrmc(se); 5623 } 5624 int maxlen = (sqlerrmc == null) ? -1 : Math.min(sqlerrmc.length(), 5626 appRequester.supportedMessageParamLength()); 5627 if ((maxlen >= 0) && (sqlerrmc.length() > maxlen)) 5628 sqlerrmc = sqlerrmc.substring(0, maxlen); 5630 return sqlerrmc; 5631 } 5632 5633 5643 private String buildPreformattedSqlerrmc(SQLException se) { 5644 if (se == null) 5645 return ""; 5646 5647 StringBuffer sb = new StringBuffer (); 5648 do { 5650 sb.append(se.getLocalizedMessage()); 5651 se = se.getNextException(); 5652 if (se != null) 5653 sb.append(SQLERRMC_PREFORMATTED_MESSAGE_DELIMITER + 5654 "SQLSTATE: " + se.getSQLState()); 5655 } while (se != null); 5656 return sb.toString(); 5657 } 5658 5659 5669 private String buildTokenizedSqlerrmc(EmbedSQLException se) { 5670 5671 String sqlerrmc = ""; 5672 do { 5673 String messageId = se.getMessageId(); 5674 Object [] args = se.getArguments(); 5676 for (int i = 0; args != null && i < args.length; i++) 5677 sqlerrmc += args[i] + SQLERRMC_TOKEN_DELIMITER; 5678 sqlerrmc += messageId; 5679 se = (EmbedSQLException) se.getNextException(); 5680 if (se != null) 5681 { 5682 sqlerrmc += SystemProcedures.SQLERRMC_MESSAGE_DELIMITER + se.getSQLState() + ":"; 5683 } 5684 } while (se != null); 5685 return sqlerrmc; 5686 } 5687 5688 5689 5743 private void writeSQLCAXGRP(int updateCount, long rowCount, String sqlerrmc, 5744 SQLException nextException) throws DRDAProtocolException 5745 { 5746 writer.writeByte(0); if (sqlamLevel < 7) 5748 { 5749 writeRDBNAM(database.dbName); 5750 writeSQLCAERRWARN(updateCount, rowCount); 5751 } 5752 else 5753 { 5754 writeSQLCAERRWARN(updateCount, rowCount); 5756 writer.writeShort(0); } 5758 writeVCMorVCS(sqlerrmc); 5759 if (sqlamLevel >=7) 5760 writeSQLDIAGGRP(nextException); 5761 } 5762 5763 5769 private void writeSQLCAERRWARN(int updateCount, long rowCount) 5770 { 5771 writer.writeInt((int)((rowCount>>>32))); 5773 writer.writeInt((int)(rowCount & 0x0000000ffffffffL)); 5774 writer.writeInt(updateCount); 5776 writer.writeBytes(errD4_D6); writer.writeBytes(warn0_warnA); } 5781 5782 5789 private void writeSQLDIAGGRP(SQLException nextException) 5790 throws DRDAProtocolException 5791 { 5792 while ( 5797 nextException != null && 5798 nextException.getSQLState() != SQLState.ROW_UPDATED && 5799 nextException.getSQLState() != SQLState.ROW_DELETED) { 5800 nextException = nextException.getNextException(); 5801 } 5802 5803 if ((nextException == null) || 5804 (diagnosticLevel == CodePoint.DIAGLVL0)) { 5805 writer.writeByte(CodePoint.NULLDATA); 5806 return; 5807 } 5808 writer.writeByte(0); 5810 writeSQLDIAGSTT(); 5811 writeSQLDIAGCI(nextException); 5812 writeSQLDIAGCN(); 5813 } 5814 5815 5818 private void writeSQLDIAGSTT() 5819 throws DRDAProtocolException 5820 { 5821 writer.writeByte(CodePoint.NULLDATA); 5822 return; 5823 } 5824 5825 5830 private void writeSQLDIAGCI(SQLException nextException) 5831 throws DRDAProtocolException 5832 { 5833 SQLException se = nextException; 5834 long rowNum = 1; 5835 5836 5837 writeSQLNUMROW(se); 5838 5839 while (se != null) 5840 { 5841 String sqlState = se.getSQLState(); 5842 5843 int severity = getExceptionSeverity(se); 5847 int sqlCode = -1; 5848 if (severity == CodePoint.SVRCOD_WARNING) 5849 sqlCode = 1; 5850 else if (severity == CodePoint.SVRCOD_INFO) 5851 sqlCode = 0; 5852 5853 String sqlerrmc = ""; 5854 if (diagnosticLevel == CodePoint.DIAGLVL1) { 5855 sqlerrmc = se.getLocalizedMessage(); 5856 } 5857 5858 if (diagnosticLevel == CodePoint.DIAGLVL0) { 5861 if (se instanceof EmbedSQLException) { 5863 Object [] args = ((EmbedSQLException)se).getArguments(); 5864 for (int i = 0; args != null && i < args.length; i++) 5865 sqlerrmc += args[i].toString() + SQLERRMC_TOKEN_DELIMITER; 5866 } 5867 } 5868 5869 String dbname = null; 5870 if (database != null) 5871 dbname = database.dbName; 5872 5873 writeSQLDCROW(rowNum++, sqlCode, sqlState, dbname, sqlerrmc); 5874 5875 se = se.getNextException(); 5876 } 5877 5878 return; 5879 } 5880 5881 5887 private void writeSQLNUMROW(SQLException nextException) 5888 throws DRDAProtocolException 5889 { 5890 writeSQLNUMGRP(nextException); 5891 } 5892 5893 5899 private void writeSQLNUMGRP(SQLException nextException) 5900 throws DRDAProtocolException 5901 { 5902 int i=0; 5903 SQLException se; 5904 5905 5906 for (se = nextException; se != null; se = se.getNextException()) i++; 5907 writer.writeShort(i); 5908 } 5909 5910 5914 private void writeSQLDCROW(long rowNum, int sqlCode, String sqlState, String dbname, 5915 String sqlerrmc) throws DRDAProtocolException 5916 { 5917 writeSQLDCGRP(rowNum, sqlCode, sqlState, dbname, sqlerrmc); 5918 } 5919 5920 5949 private void writeSQLDCGRP(long rowNum, int sqlCode, String sqlState, String dbname, 5950 String sqlerrmc) throws DRDAProtocolException 5951 { 5952 writer.writeInt(sqlCode); 5954 5955 writer.writeString(sqlState); 5957 5958 5959 writer.writeInt(0); writer.writeInt(0); writer.writeLong(rowNum); 5963 byte[] byteArray = new byte[1]; 5964 writer.writeScalarPaddedBytes(byteArray, 47, (byte) 0); 5965 5966 writer.writeShort(0); writer.writeByte(CodePoint.NULLDATA); writer.writeLDString(sqlerrmc); 5970 writeVCMorVCS(null); writeVCMorVCS(null); writeVCMorVCS(null); writer.writeByte(CodePoint.NULLDATA); } 5975 5976 5979 private void writeSQLDIAGCN() 5980 throws DRDAProtocolException 5981 { 5982 writer.writeByte(CodePoint.NULLDATA); 5983 return; 5984 } 5985 5986 6007 private void writeSQLDARD(DRDAStatement stmt, boolean rtnOutput, SQLException e) throws DRDAProtocolException, SQLException 6008 { 6009 PreparedStatement ps = stmt.getPreparedStatement(); 6010 ResultSetMetaData rsmeta = ps.getMetaData(); 6011 EngineParameterMetaData pmeta = stmt.getParameterMetaData(); 6012 int numElems = 0; 6013 if (e == null || e instanceof SQLWarning ) 6014 { 6015 if (rtnOutput && (rsmeta != null)) 6016 numElems = rsmeta.getColumnCount(); 6017 else if ((! rtnOutput) && (pmeta != null)) 6018 numElems = pmeta.getParameterCount(); 6019 } 6020 6021 writer.createDssObject(); 6022 6023 writer.startDdm(CodePoint.SQLDARD); 6025 writeSQLCAGRP(e, getSqlCode(getExceptionSeverity(e)), 0, 0); 6026 6027 if (sqlamLevel >= MGRLVL_7) 6028 writeSQLDHROW (stmt); 6029 6030 if (SanityManager.DEBUG) 6032 trace("num Elements = " + numElems); 6033 writer.writeShort(numElems); 6034 6035 for (int i=0; i < numElems; i++) 6036 writeSQLDAGRP (rsmeta, pmeta, i, rtnOutput); 6037 writer.endDdmAndDss(); 6038 6039 } 6040 6057 private void writeQRYDSC(DRDAStatement stmt, boolean FDODSConly) 6058 throws DRDAProtocolException, SQLException 6059 { 6060 6061 ResultSet rs = null; 6062 ResultSetMetaData rsmeta = null; 6063 EngineParameterMetaData pmeta = null; 6064 if (!stmt.needsToSendParamData) 6065 rs = stmt.getResultSet(); 6066 if (rs == null) pmeta = stmt.getParameterMetaData(); 6068 else 6069 rsmeta = rs.getMetaData(); 6070 6071 int numCols = (rsmeta != null ? rsmeta.getColumnCount() : pmeta.getParameterCount()); 6072 int numGroups = 1; 6073 int colStart = 1; 6074 int colEnd = numCols; 6075 int blksize = stmt.getBlksize() > 0 ? stmt.getBlksize() : CodePoint.QRYBLKSZ_MAX; 6076 6077 int remaining = blksize - (writer.getDSSLength() % blksize) - (3 + 6080 FdocaConstants.SQLCADTA_SQLDTARD_RLO_SIZE); 6081 6082 6083 int firstcols = remaining/FdocaConstants.SQLDTAGRP_COL_DSC_SIZE; 6085 6086 if (firstcols < numCols || numCols > FdocaConstants.MAX_VARS_IN_NGDA) 6089 { 6090 if (firstcols > FdocaConstants.MAX_VARS_IN_NGDA) 6092 { 6093 if (SanityManager.DEBUG) 6094 SanityManager.ASSERT(numCols > FdocaConstants.MAX_VARS_IN_NGDA, 6095 "Number of columns " + numCols + 6096 " is less than MAX_VARS_IN_NGDA"); 6097 numGroups = numCols/FdocaConstants.MAX_VARS_IN_NGDA; 6098 if (FdocaConstants.MAX_VARS_IN_NGDA * numGroups < numCols) 6100 numGroups++; 6101 colEnd = FdocaConstants.MAX_VARS_IN_NGDA; 6102 } 6103 else 6104 { 6105 colEnd = firstcols; 6106 numGroups += (numCols-firstcols)/FdocaConstants.MAX_VARS_IN_NGDA; 6107 if (FdocaConstants.MAX_VARS_IN_NGDA * numGroups < numCols) 6108 numGroups++; 6109 } 6110 } 6111 6112 if (! FDODSConly) 6113 { 6114 writer.createDssObject(); 6115 writer.startDdm(CodePoint.QRYDSC); 6116 } 6117 6118 for (int i = 0; i < numGroups; i++) 6119 { 6120 writeSQLDTAGRP(stmt, rsmeta, pmeta, colStart, colEnd, 6121 (i == 0 ? true : false)); 6122 colStart = colEnd + 1; 6123 colEnd = colEnd + FdocaConstants.MAX_VARS_IN_NGDA; 6125 if (colEnd > numCols) 6126 colEnd = numCols; 6127 } 6128 writer.writeBytes(FdocaConstants.SQLCADTA_SQLDTARD_RLO); 6129 if (! FDODSConly) 6130 writer.endDdmAndDss(); 6131 } 6132 6158 private void writeSQLDTAGRP(DRDAStatement stmt, ResultSetMetaData rsmeta, 6159 EngineParameterMetaData pmeta, 6160 int colStart, int colEnd, boolean first) 6161 throws DRDAProtocolException, SQLException 6162 { 6163 6164 int length = (FdocaConstants.SQLDTAGRP_COL_DSC_SIZE * 6165 ((colEnd+1) - colStart)) + 3; 6166 writer.writeByte(length); 6167 if (first) 6168 { 6169 6170 writer.writeByte(FdocaConstants.NGDA_TRIPLET_TYPE); 6171 writer.writeByte(FdocaConstants.SQLDTAGRP_LID); 6172 } 6173 else 6174 { 6175 writer.writeByte(FdocaConstants.CPT_TRIPLET_TYPE); 6177 writer.writeByte(FdocaConstants.NULL_LID); 6178 6179 } 6180 6181 6182 6183 boolean hasRs = (rsmeta != null); 6185 for (int i = colStart; i <= colEnd; i++) 6186 { 6187 boolean nullable = (hasRs ? (rsmeta.isNullable(i) == rsmeta.columnNullable) : 6188 (pmeta.isNullable(i) == JDBC30Translation.PARAMETER_NULLABLE)); 6189 int colType = (hasRs ? rsmeta.getColumnType(i) : pmeta.getParameterType(i)); 6190 int[] outlen = {-1}; 6191 int drdaType = 6192 (hasRs ?FdocaConstants.mapJdbcTypeToDrdaType(colType,nullable,outlen): 6193 stmt.getParamDRDAType(i)); 6194 6195 boolean isDecimal = ((drdaType | 1) == DRDAConstants.DRDA_TYPE_NDECIMAL); 6196 int precision = 0, scale = 0; 6197 if (hasRs) 6198 { 6199 precision = rsmeta.getPrecision(i); 6200 scale = rsmeta.getScale(i); 6201 stmt.setRsDRDAType(i,drdaType); 6202 stmt.setRsPrecision(i, precision); 6203 stmt.setRsScale(i,scale); 6204 } 6205 6206 else if (isDecimal) 6207 { 6208 if (stmt.isOutputParam(i)) 6209 ((CallableStatement ) stmt.ps).registerOutParameter(i,Types.DECIMAL); 6210 precision = pmeta.getPrecision(i); 6211 scale = pmeta.getScale(i); 6212 6213 } 6214 6215 if (SanityManager.DEBUG) 6216 trace("jdbcType=" + colType + " \tdrdaType=" + Integer.toHexString(drdaType)); 6217 6218 writer.writeByte(drdaType); 6220 if (isDecimal) 6221 { 6222 writer.writeByte(precision); 6223 writer.writeByte(scale); 6224 } 6225 else if (outlen[0] != -1) 6226 writer.writeShort(outlen[0]); 6227 else if (hasRs) 6228 writer.writeShort(rsmeta.getColumnDisplaySize(i)); 6229 else 6230 writer.writeShort(stmt.getParamLen(i)); 6231 } 6232 } 6233 6234 6235 6236 private void writeSQLDHROW (DRDAStatement stmt) throws DRDAProtocolException,SQLException 6239 { 6240 if (JVMInfo.JDK_ID < 2) { 6242 writer.writeByte(CodePoint.NULLDATA); 6243 return; 6244 } 6245 6246 writer.writeByte(0); 6248 writer.writeShort(stmt.getResultSetHoldability()); 6250 6251 writer.writeShort(0); 6253 writer.writeShort(0); 6255 writer.writeShort(0); 6257 writer.writeShort(0); 6259 writer.writeShort(0); 6261 writer.writeShort(0); writeVCMorVCS(null); 6265 6266 } 6267 6268 6315 private void writeQRYDTA (DRDAStatement stmt) 6316 throws DRDAProtocolException, SQLException 6317 { 6318 boolean getMoreData = true; 6319 boolean sentExtData = false; 6320 int startLength = 0; 6321 writer.createDssObject(); 6322 6323 if (SanityManager.DEBUG) 6324 trace("Write QRYDTA"); 6325 writer.startDdm(CodePoint.QRYDTA); 6326 if (processLeftoverQRYDTA(stmt)) 6332 { 6333 if (stmt.getSplitQRYDTA() == null) 6334 { 6335 stmt.rowCount += 1; 6336 if (stmt.getExtDtaObjects() != null) 6337 writeEXTDTA(stmt); 6338 } 6339 return; 6340 } 6341 6342 while(getMoreData) 6343 { 6344 sentExtData = false; 6345 getMoreData = writeFDODTA(stmt); 6346 6347 if (stmt.getExtDtaObjects() != null && 6348 stmt.getSplitQRYDTA() == null) 6349 { 6350 writer.endDdmAndDss(); 6351 writeEXTDTA(stmt); 6352 getMoreData=false; 6353 sentExtData = true; 6354 } 6355 6356 if (getMoreData == true) 6360 { 6361 int endLength = writer.getDSSLength(); 6362 int rowsize = endLength - startLength; 6363 if ((stmt.getBlksize() - endLength ) < rowsize) 6364 getMoreData = false; 6365 6366 startLength = endLength; 6367 } 6368 6369 } 6370 if (! sentExtData) 6374 writer.endDdmAndDss(); 6375 6376 if (!stmt.hasdata()) { 6377 final boolean qryclsOnLmtblkprc = 6378 appRequester.supportsQryclsimpForLmtblkprc(); 6379 if (stmt.isRSCloseImplicit(qryclsOnLmtblkprc)) { 6380 stmt.rsClose(); 6381 } 6382 } 6383 } 6384 6385 6427 private boolean writeFDODTA (DRDAStatement stmt) 6428 throws DRDAProtocolException, SQLException 6429 { 6430 boolean hasdata = false; 6431 int blksize = stmt.getBlksize() > 0 ? stmt.getBlksize() : CodePoint.QRYBLKSZ_MAX; 6432 long rowCount = 0; 6433 ResultSet rs =null; 6434 boolean moreData = (stmt.getQryprctyp() 6435 == CodePoint.LMTBLKPRC); 6436 int numCols; 6437 6438 if (!stmt.needsToSendParamData) 6439 { 6440 rs = stmt.getResultSet(); 6441 } 6442 6443 if (rs != null) 6444 { 6445 numCols = stmt.getNumRsCols(); 6446 if (stmt.isScrollable()) 6447 hasdata = positionCursor(stmt, rs); 6448 else 6449 hasdata = rs.next(); 6450 } 6451 else { 6453 hasdata = stmt.hasOutputParams(); 6454 numCols = stmt.getNumParams(); 6455 } 6456 6457 6458 do { 6459 if (!hasdata) 6460 { 6461 doneData(stmt, rs); 6462 moreData = false; 6463 return moreData; 6464 } 6465 6466 SQLWarning sqlw = (rs != null)? rs.getWarnings(): null; 6468 if (rs != null) { 6469 rs.clearWarnings(); 6470 } 6471 6472 if (rs != null && rs.rowUpdated()) { 6477 SQLWarning w = new SQLWarning ("", SQLState.ROW_UPDATED, 6478 ExceptionSeverity.WARNING_SEVERITY); 6479 if (sqlw != null) { 6480 sqlw.setNextWarning(w); 6481 } else { 6482 sqlw = w; 6483 } 6484 } 6485 if (rs != null && rs.rowDeleted()) { 6489 SQLWarning w = new SQLWarning ("", SQLState.ROW_DELETED, 6490 ExceptionSeverity.WARNING_SEVERITY); 6491 if (sqlw != null) { 6492 sqlw.setNextWarning(w); 6493 } else { 6494 sqlw = w; 6495 } 6496 } 6497 6498 if (sqlw == null) 6499 writeSQLCAGRP(nullSQLState, 0, -1, -1); 6500 else 6501 writeSQLCAGRP(sqlw, sqlw.getErrorCode(), 1, -1); 6502 6503 boolean noRetrieveRS = (rs != null && 6507 (!stmt.getQryrtndta() || rs.rowDeleted())); 6508 if (noRetrieveRS) 6509 writer.writeByte(0xFF); else 6511 writer.writeByte(0); 6513 for (int i = 1; i <= numCols; i++) 6514 { 6515 if (noRetrieveRS) 6516 break; 6517 6518 int drdaType; 6519 int ndrdaType; 6520 int precision; 6521 int scale; 6522 6523 Object val = null; 6524 boolean valNull; 6525 if (rs != null) 6526 { 6527 drdaType = stmt.getRsDRDAType(i) & 0xff; 6528 precision = stmt.getRsPrecision(i); 6529 scale = stmt.getRsScale(i); 6530 ndrdaType = drdaType | 1; 6531 6532 if (SanityManager.DEBUG) 6533 trace("!!drdaType = " + java.lang.Integer.toHexString(drdaType) + 6534 "precision = " + precision +" scale = " + scale); 6535 switch (ndrdaType) 6536 { 6537 case DRDAConstants.DRDA_TYPE_NLOBBYTES: 6538 case DRDAConstants.DRDA_TYPE_NLOBCMIXED: 6539 EXTDTAInputStream extdtaStream= 6540 EXTDTAInputStream.getEXTDTAStream(rs, i, drdaType); 6541 writeFdocaVal(i,extdtaStream, drdaType, 6542 precision,scale,rs.wasNull(),stmt); 6543 break; 6544 case DRDAConstants.DRDA_TYPE_NINTEGER: 6545 int ival = rs.getInt(i); 6546 valNull = rs.wasNull(); 6547 if (SanityManager.DEBUG) 6548 trace("====== writing int: "+ ival + " is null: " + valNull); 6549 writeNullability(drdaType,valNull); 6550 if (! valNull) 6551 writer.writeInt(ival); 6552 break; 6553 case DRDAConstants.DRDA_TYPE_NSMALL: 6554 short sval = rs.getShort(i); 6555 valNull = rs.wasNull(); 6556 if (SanityManager.DEBUG) 6557 trace("====== writing small: "+ sval + " is null: " + valNull); 6558 writeNullability(drdaType,valNull); 6559 if (! valNull) 6560 writer.writeShort(sval); 6561 break; 6562 case DRDAConstants.DRDA_TYPE_NINTEGER8: 6563 long lval = rs.getLong(i); 6564 valNull = rs.wasNull(); 6565 if (SanityManager.DEBUG) 6566 trace("====== writing long: "+ lval + " is null: " + valNull); 6567 writeNullability(drdaType,valNull); 6568 if (! valNull) 6569 writer.writeLong(lval); 6570 break; 6571 case DRDAConstants.DRDA_TYPE_NFLOAT4: 6572 float fval = rs.getFloat(i); 6573 valNull = rs.wasNull(); 6574 if (SanityManager.DEBUG) 6575 trace("====== writing float: "+ fval + " is null: " + valNull); 6576 writeNullability(drdaType,valNull); 6577 if (! valNull) 6578 writer.writeFloat(fval); 6579 break; 6580 case DRDAConstants.DRDA_TYPE_NFLOAT8: 6581 double dval = rs.getDouble(i); 6582 valNull = rs.wasNull(); 6583 if (SanityManager.DEBUG) 6584 trace("====== writing double: "+ dval + " is null: " + valNull); 6585 writeNullability(drdaType,valNull); 6586 if (! valNull) 6587 writer.writeDouble(dval); 6588 break; 6589 case DRDAConstants.DRDA_TYPE_NCHAR: 6590 case DRDAConstants.DRDA_TYPE_NVARCHAR: 6591 case DRDAConstants.DRDA_TYPE_NVARMIX: 6592 case DRDAConstants.DRDA_TYPE_NLONG: 6593 case DRDAConstants.DRDA_TYPE_NLONGMIX: 6594 String valStr = rs.getString(i); 6595 if (SanityManager.DEBUG) 6596 trace("====== writing char/varchar/mix :"+ valStr + ":"); 6597 writeFdocaVal(i, valStr, drdaType, 6598 precision,scale,rs.wasNull(),stmt); 6599 break; 6600 default: 6601 writeFdocaVal(i, rs.getObject(i),drdaType, 6602 precision,scale,rs.wasNull(),stmt); 6603 } 6604 } 6605 else 6606 { 6607 drdaType = stmt.getParamDRDAType(i) & 0xff; 6608 precision = stmt.getParamPrecision(i); 6609 scale = stmt.getParamScale(i); 6610 ndrdaType = drdaType | 1; 6611 6612 if (stmt.isOutputParam(i)) { 6613 if (SanityManager.DEBUG) 6614 trace("***getting Object "+i); 6615 val = ((CallableStatement ) stmt.ps).getObject(i); 6616 valNull = (val == null); 6617 writeFdocaVal(i,val,drdaType,precision, scale, valNull,stmt); 6618 } 6619 else 6620 writeFdocaVal(i,null,drdaType,precision,scale,true,stmt); 6621 6622 } 6623 } 6624 if (writer.getDSSLength() > blksize) 6626 { 6627 splitQRYDTA(stmt, blksize); 6628 return false; 6629 } 6630 6631 if (rs == null) 6632 return moreData; 6633 6634 rowCount++; 6636 if (rowCount < stmt.getQryrowset()) 6637 { 6638 hasdata = rs.next(); 6639 } 6640 6642 else if (stmt.isScrollable() || noRetrieveRS) 6643 moreData=false; 6644 6645 } while (hasdata && rowCount < stmt.getQryrowset()); 6646 6647 if (!stmt.isScrollable()) 6650 stmt.rowCount += rowCount; 6651 6652 if (!hasdata) 6653 { 6654 doneData(stmt, rs); 6655 moreData=false; 6656 } 6657 6658 if (!stmt.isScrollable()) 6659 stmt.setHasdata(hasdata); 6660 return moreData; 6661 } 6662 6687 private void splitQRYDTA(DRDAStatement stmt, int blksize) throws SQLException , 6688 DRDAProtocolException 6689 { 6690 byte [] temp = writer.copyDSSDataToEnd(blksize); 6692 writer.truncateDSS(blksize); 6694 if (temp.length == 0) 6695 agentError("LMTBLKPRC violation: splitQRYDTA was " + 6696 "called to split a QRYDTA block, but the " + 6697 "entire row fit successfully into the " + 6698 "current block. Server rowsize computation " + 6699 "was probably incorrect (perhaps an off-by-" + 6700 "one bug?). QRYDTA blocksize: " + blksize); 6701 stmt.setSplitQRYDTA(temp); 6702 } 6703 6718 private boolean processLeftoverQRYDTA(DRDAStatement stmt) 6719 throws SQLException ,DRDAProtocolException 6720 { 6721 byte []leftovers = stmt.getSplitQRYDTA(); 6722 if (leftovers == null) 6723 return false; 6724 int blksize = stmt.getBlksize() > 0 ? stmt.getBlksize() : CodePoint.QRYBLKSZ_MAX; 6725 blksize = blksize - 10; if (leftovers.length < blksize) 6727 { 6728 writer.writeBytes(leftovers, 0, leftovers.length); 6729 stmt.setSplitQRYDTA(null); 6730 } 6731 else 6732 { 6733 writer.writeBytes(leftovers, 0, blksize); 6734 byte []newLeftovers = new byte[leftovers.length-blksize]; 6735 for (int i = 0; i < newLeftovers.length; i++) 6736 newLeftovers[i] = leftovers[blksize+i]; 6737 stmt.setSplitQRYDTA(newLeftovers); 6738 } 6739 writer.endDdmAndDss(); 6741 return true; 6742 } 6743 6744 6745 6754 private void doneData(DRDAStatement stmt, ResultSet rs) 6755 throws DRDAProtocolException, SQLException 6756 { 6757 if (SanityManager.DEBUG) 6758 trace("*****NO MORE DATA!!"); 6759 int blksize = stmt.getBlksize() > 0 ? stmt.getBlksize() : CodePoint.QRYBLKSZ_MAX; 6760 if (rs != null) 6761 { 6762 if (stmt.isScrollable()) 6763 { 6764 boolean isAfterLast = rs.isAfterLast(); 6767 boolean isBeforeFirst = rs.isBeforeFirst(); 6768 6769 rs.last(); 6772 stmt.rowCount = rs.getRow(); 6773 6774 if (isAfterLast) { 6776 rs.afterLast(); 6777 } 6778 if (isBeforeFirst) { 6779 rs.beforeFirst(); 6780 } 6781 } 6782 else { 6784 final boolean qryclsOnLmtblkprc = 6785 appRequester.supportsQryclsimpForLmtblkprc(); 6786 if (stmt.isRSCloseImplicit(qryclsOnLmtblkprc)) { 6787 stmt.rsClose(); 6788 stmt.rsSuspend(); 6789 } 6790 6791 } 6792 } 6793 6794 boolean isQRYSCRAFT = (stmt.getQryscrorn() == CodePoint.QRYSCRAFT); 6798 6799 writeSQLCAGRP((isQRYSCRAFT ? eod00000 : eod02000), 6801 (isQRYSCRAFT ? 0 : 100), 0, stmt.rowCount); 6802 6803 writer.writeByte(CodePoint.NULLDATA); 6804 if (writer.getDSSLength() > blksize) 6806 { 6807 splitQRYDTA(stmt, blksize); 6808 } 6809 } 6810 6816 private boolean positionCursor(DRDAStatement stmt, ResultSet rs) 6817 throws SQLException , DRDAProtocolException 6818 { 6819 boolean retval = false; 6820 switch (stmt.getQryscrorn()) 6821 { 6822 case CodePoint.QRYSCRREL: 6823 int rows = (int)stmt.getQryrownbr(); 6824 if ((rs.isAfterLast() && rows > 0) || (rs.isBeforeFirst() && rows < 0)) { 6825 retval = false; 6826 } else { 6827 retval = rs.relative(rows); 6828 } 6829 break; 6830 case CodePoint.QRYSCRABS: 6831 if (stmt.getQryrownbr() == 0) 6834 { 6835 rs.beforeFirst(); 6836 retval = false; 6837 } 6838 else 6839 { 6840 retval = rs.absolute((int)stmt.getQryrownbr()); 6841 } 6842 break; 6843 case CodePoint.QRYSCRAFT: 6844 rs.afterLast(); 6845 retval = false; 6846 break; 6847 case CodePoint.QRYSCRBEF: 6848 rs.beforeFirst(); 6849 retval = false; 6850 break; 6851 default: 6852 agentError("Invalid value for cursor orientation "+ stmt.getQryscrorn()); 6853 } 6854 return retval; 6855 } 6856 6905 private void writeSQLDAGRP(ResultSetMetaData rsmeta, EngineParameterMetaData pmeta, int elemNum, boolean rtnOutput) 6906 throws DRDAProtocolException, SQLException 6907 { 6908 6910 int jdbcElemNum = elemNum +1; 6911 int[] outlen = {-1}; 6913 6914 int elemType = rtnOutput ? rsmeta.getColumnType(jdbcElemNum) : pmeta.getParameterType(jdbcElemNum); 6915 6916 int precision = rtnOutput ? rsmeta.getPrecision(jdbcElemNum) : pmeta.getPrecision(jdbcElemNum); 6917 if (precision > FdocaConstants.NUMERIC_MAX_PRECISION) 6918 precision = FdocaConstants.NUMERIC_MAX_PRECISION; 6919 6920 writer.writeShort(precision); 6922 int scale = (rtnOutput ? rsmeta.getScale(jdbcElemNum) : pmeta.getScale(jdbcElemNum)); 6924 writer.writeShort(scale); 6925 6926 boolean nullable = rtnOutput ? (rsmeta.isNullable(jdbcElemNum) == 6927 ResultSetMetaData.columnNullable) : 6928 (pmeta.isNullable(jdbcElemNum) == JDBC30Translation.PARAMETER_NULLABLE); 6929 6930 int sqlType = SQLTypes.mapJdbcTypeToDB2SqlType(elemType, 6931 nullable, 6932 outlen); 6933 6934 if (outlen[0] == -1) { 6936 switch (elemType) 6937 { 6938 case Types.DECIMAL: 6939 case Types.NUMERIC: 6940 scale = rtnOutput ? rsmeta.getScale(jdbcElemNum) : pmeta.getScale(jdbcElemNum); 6941 outlen[0] = ((precision <<8) | (scale <<0)); 6942 if (SanityManager.DEBUG) 6943 trace("\n\nprecision =" +precision + 6944 " scale =" + scale); 6945 break; 6946 default: 6947 outlen[0] = Math.min(FdocaConstants.LONGVARCHAR_MAX_LEN, 6948 (rtnOutput ? rsmeta.getColumnDisplaySize(jdbcElemNum) : 6949 pmeta.getPrecision(jdbcElemNum))); 6950 } 6951 } 6952 6953 switch (elemType) 6954 { 6955 case Types.BINARY: 6956 case Types.VARBINARY: 6957 case Types.LONGVARBINARY: 6958 case Types.BLOB: case Types.CLOB: 6960 outlen[0] = (rtnOutput ? rsmeta.getPrecision(jdbcElemNum) : 6961 pmeta.getPrecision(jdbcElemNum)); 6962 } 6963 6964 if (SanityManager.DEBUG) 6965 trace("SQLDAGRP len =" + java.lang.Integer.toHexString(outlen[0]) + "for type:" + elemType); 6966 6967 if (sqlamLevel >= MGRLVL_6) 6969 writer.writeLong(outlen[0]); 6970 else 6971 writer.writeInt(outlen[0]); 6972 6973 6974 String typeName = rtnOutput ? rsmeta.getColumnTypeName(jdbcElemNum) : 6975 pmeta.getParameterTypeName(jdbcElemNum); 6976 if (SanityManager.DEBUG) 6977 trace("jdbcType =" + typeName + " sqlType =" + sqlType + "len =" +outlen[0]); 6978 6979 writer.writeShort(sqlType); 6980 6981 6984 if (elemType == java.sql.Types.CHAR || 6985 elemType == java.sql.Types.VARCHAR 6986 || elemType == java.sql.Types.LONGVARCHAR 6987 || elemType == java.sql.Types.CLOB) 6988 writer.writeScalar2Bytes(1208); 6989 else 6990 writer.writeScalar2Bytes(0); 6991 6992 if (sqlamLevel < MGRLVL_7) 6993 { 6994 6995 writeVCMorVCS(rtnOutput ? rsmeta.getColumnName(jdbcElemNum) : null); 6997 writeVCMorVCS(null); 6999 writeVCMorVCS(null); 7001 7002 if (sqlamLevel == MGRLVL_6) 7003 writeSQLUDTGRP(rsmeta, pmeta, jdbcElemNum, rtnOutput); 7004 } 7005 else 7006 { 7007 writeSQLDOPTGRP(rsmeta, pmeta, jdbcElemNum, rtnOutput); 7008 } 7009 7010 } 7011 7012 7021 private void writeVCMorVCS(String s) 7022 throws DRDAProtocolException 7023 { 7024 7026 if (s == null) 7027 { 7028 writer.writeShort(0); 7029 writer.writeShort(0); 7030 return; 7031 } 7032 7033 writer.writeLDString(s); 7035 writer.writeShort(0); 7037 } 7038 7039 7040 private void writeSQLUDTGRP(ResultSetMetaData rsmeta, EngineParameterMetaData pmeta, int jdbcElemNum, boolean rtnOutput) 7041 throws DRDAProtocolException,SQLException 7042 { 7043 writer.writeByte(CodePoint.NULLDATA); 7044 7045 } 7046 7047 private void writeSQLDOPTGRP(ResultSetMetaData rsmeta, EngineParameterMetaData pmeta, int jdbcElemNum, boolean rtnOutput) 7048 throws DRDAProtocolException,SQLException 7049 { 7050 7051 writer.writeByte(0); 7052 writer.writeShort(0); 7054 writeVCMorVCS(rtnOutput ? rsmeta.getColumnName(jdbcElemNum) : null); 7056 writeVCMorVCS(null); 7058 writeVCMorVCS(null); 7060 writeSQLUDTGRP(rsmeta, pmeta, jdbcElemNum, rtnOutput); 7062 writeSQLDXGRP(rsmeta, pmeta, jdbcElemNum, rtnOutput); 7064 } 7065 7066 7067 private void writeSQLDXGRP(ResultSetMetaData rsmeta, EngineParameterMetaData pmeta, int jdbcElemNum, boolean rtnOutput) 7068 throws DRDAProtocolException,SQLException 7069 { 7070 writer.writeByte(0); 7072 writer.writeShort(0); 7075 writer.writeShort(rtnOutput ? rsmeta.isWritable(jdbcElemNum) : false); 7077 7078 if (rtnOutput && rsmeta.isAutoIncrement(jdbcElemNum)) 7080 writer.writeShort(2); 7081 else 7082 writer.writeShort(0); 7083 7084 if (pmeta != null && !rtnOutput) 7086 { 7087 int mode = pmeta.getParameterMode(jdbcElemNum); 7088 if (mode == JDBC30Translation.PARAMETER_MODE_UNKNOWN) 7089 { 7090 int type = DRDAStatement.getOutputParameterTypeFromClassName( 7093 pmeta.getParameterClassName(jdbcElemNum)); 7094 if (type != DRDAStatement.NOT_OUTPUT_PARAM) 7095 mode = JDBC30Translation.PARAMETER_MODE_IN_OUT; 7096 } 7097 writer.writeShort(mode); 7098 } 7099 else 7100 { 7101 writer.writeShort(0); 7102 } 7103 7104 writer.writeShort(0); 7107 7108 writeVCMorVCS(null); 7111 7112 writeVCMorVCS(rtnOutput ? rsmeta.getTableName(jdbcElemNum) : null); 7115 7116 writeVCMorVCS(rtnOutput ? rsmeta.getSchemaName(jdbcElemNum): null); 7119 7120 7121 writeVCMorVCS(rtnOutput ? rsmeta.getColumnName(jdbcElemNum): null); 7124 7125 } 7126 7127 7140 7141 protected void writeFdocaVal(int index, Object val, int drdaType, 7142 int precision, int scale, boolean valNull, 7143 7144 DRDAStatement stmt) throws DRDAProtocolException, SQLException 7145 { 7146 writeNullability(drdaType,valNull); 7147 7148 if (! valNull) 7149 { 7150 int ndrdaType = drdaType | 1; 7151 long valLength = 0; 7152 switch (ndrdaType) 7153 { 7154 case DRDAConstants.DRDA_TYPE_NSMALL: 7155 if (val instanceof Boolean ) 7158 { 7159 writer.writeShort(((Boolean ) val).booleanValue()); 7160 } 7161 else if (val instanceof Short ) 7162 writer.writeShort(((Short ) val).shortValue()); 7163 else if (val instanceof Byte ) 7164 writer.writeShort(((Byte ) val).byteValue()); 7165 else 7166 writer.writeShort(((Integer ) val).shortValue()); 7167 break; 7168 case DRDAConstants.DRDA_TYPE_NINTEGER: 7169 writer.writeInt(((Integer ) val).intValue()); 7170 break; 7171 case DRDAConstants.DRDA_TYPE_NINTEGER8: 7172 writer.writeLong(((Long ) val).longValue()); 7173 break; 7174 case DRDAConstants.DRDA_TYPE_NFLOAT4: 7175 writer.writeFloat(((Float ) val).floatValue()); 7176 break; 7177 case DRDAConstants.DRDA_TYPE_NFLOAT8: 7178 writer.writeDouble(((Double ) val).doubleValue()); 7179 break; 7180 case DRDAConstants.DRDA_TYPE_NDECIMAL: 7181 if (precision == 0) 7182 precision = FdocaConstants.NUMERIC_DEFAULT_PRECISION; 7183 BigDecimal bd = (java.math.BigDecimal ) val; 7184 writer.writeBigDecimal(bd,precision,scale); 7185 break; 7186 case DRDAConstants.DRDA_TYPE_NDATE: 7187 writer.writeString(((java.sql.Date ) val).toString()); 7188 break; 7189 case DRDAConstants.DRDA_TYPE_NTIME: 7190 writer.writeString(((java.sql.Time ) val).toString()); 7191 break; 7192 case DRDAConstants.DRDA_TYPE_NTIMESTAMP: 7193 String ts1 = ((java.sql.Timestamp ) val).toString(); 7196 String ts2 = ts1.replace(' ','-').replace(':','.'); 7197 int tsLen = ts2.length(); 7198 if (tsLen < 26) 7199 { 7200 for (int i = 0; i < 26-tsLen; i++) 7201 ts2 += "0"; 7202 } 7203 else if (tsLen > 26) 7204 ts2 = ts2.substring(0,26); 7205 writer.writeString(ts2); 7206 break; 7207 case DRDAConstants.DRDA_TYPE_NCHAR: 7208 writer.writeString(((String ) val).toString()); 7209 break; 7210 case DRDAConstants.DRDA_TYPE_NVARCHAR: 7211 case DRDAConstants.DRDA_TYPE_NVARMIX: 7212 case DRDAConstants.DRDA_TYPE_NLONG: 7213 case DRDAConstants.DRDA_TYPE_NLONGMIX: 7214 writer.writeLDString(val.toString(), index); 7217 break; 7218 case DRDAConstants.DRDA_TYPE_NLOBBYTES: 7219 case DRDAConstants.DRDA_TYPE_NLOBCMIXED: 7220 7221 if( ! ((EXTDTAInputStream) val).isEmptyStream() ){ 7223 stmt.addExtDtaObject(val, index); 7224 7225 writer.writeExtendedLength(0x8000); 7227 7228 }else{ 7229 writer.writeExtendedLength(0); 7230 7231 } 7232 7233 break; 7234 7235 case DRDAConstants.DRDA_TYPE_NFIXBYTE: 7236 writer.writeBytes((byte[]) val); 7237 break; 7238 case DRDAConstants.DRDA_TYPE_NVARBYTE: 7239 case DRDAConstants.DRDA_TYPE_NLONGVARBYTE: 7240 writer.writeLDBytes((byte[]) val, index); 7241 break; 7242 default: 7243 if (SanityManager.DEBUG) 7244 trace("ndrdaType is: "+ndrdaType); 7245 writer.writeLDString(val.toString(), index); 7246 } 7247 } 7248 } 7249 7250 7257 private void writeNullability(int drdaType, boolean valNull) 7258 { 7259 if(FdocaConstants.isNullable(drdaType)) 7260 { 7261 if (valNull) 7262 writer.writeByte(FdocaConstants.NULL_DATA); 7263 else 7264 { 7265 writer.writeByte(FdocaConstants.INDICATOR_NULLABLE); 7266 } 7267 } 7268 7269 } 7270 7271 7274 7279 private void copyToRequired(int [] req) 7280 { 7281 currentRequiredLength = req.length; 7282 if (currentRequiredLength > required.length) 7283 required = new int[currentRequiredLength]; 7284 for (int i = 0; i < req.length; i++) 7285 required[i] = req[i]; 7286 } 7287 7292 private void removeFromRequired(int codePoint) 7293 { 7294 for (int i = 0; i < currentRequiredLength; i++) 7295 if (required[i] == codePoint) 7296 required[i] = 0; 7297 7298 } 7299 7304 private void checkRequired(int codePoint) throws DRDAProtocolException 7305 { 7306 int firstMissing = 0; 7307 for (int i = 0; i < currentRequiredLength; i++) 7308 { 7309 if (required[i] != 0) 7310 { 7311 firstMissing = required[i]; 7312 break; 7313 } 7314 } 7315 if (firstMissing != 0) 7316 missingCodePoint(firstMissing); 7317 } 7318 7321 7328 private void tooMany(int codePoint) throws DRDAProtocolException 7329 { 7330 throwSyntaxrm(CodePoint.SYNERRCD_TOO_MANY, codePoint); 7331 } 7332 7338 private void tooBig(int codePoint) throws DRDAProtocolException 7339 { 7340 throwSyntaxrm(CodePoint.SYNERRCD_TOO_BIG, codePoint); 7341 } 7342 7348 private void badObjectLength(int codePoint) throws DRDAProtocolException 7349 { 7350 throwSyntaxrm(CodePoint.SYNERRCD_OBJ_LEN_NOT_ALLOWED, codePoint); 7351 } 7352 7358 private void rdbNotFound(String rdbnam) throws DRDAProtocolException 7359 { 7360 Object [] oa = {rdbnam}; 7361 throw new 7362 DRDAProtocolException(DRDAProtocolException.DRDA_Proto_RDBNFNRM, 7363 this,0, 7364 DRDAProtocolException.NO_ASSOC_ERRCD, oa); 7365 } 7366 7372 private void invalidValue(int codePoint) throws DRDAProtocolException 7373 { 7374 throwSyntaxrm(CodePoint.SYNERRCD_REQ_VAL_NOT_FOUND, codePoint); 7375 } 7376 7383 protected void invalidCodePoint(int codePoint) throws DRDAProtocolException 7384 { 7385 throwSyntaxrm(CodePoint.SYNERRCD_INVALID_CP_FOR_CMD, codePoint); 7386 } 7387 7393 protected void codePointNotSupported(int codePoint) throws DRDAProtocolException 7394 { 7395 throw new 7396 DRDAProtocolException(DRDAProtocolException.DRDA_Proto_CMDNSPRM, 7397 this,codePoint, 7398 DRDAProtocolException.NO_ASSOC_ERRCD); 7399 } 7400 7406 private void valueNotSupported(int codePoint) throws DRDAProtocolException 7407 { 7408 throw new 7409 DRDAProtocolException(DRDAProtocolException.DRDA_Proto_VALNSPRM, 7410 this,codePoint, 7411 DRDAProtocolException.NO_ASSOC_ERRCD); 7412 } 7413 7421 private void verifyRequiredObject(int codePoint, int reqCodePoint) 7422 throws DRDAProtocolException 7423 { 7424 if (codePoint != reqCodePoint ) 7425 { 7426 throwSyntaxrm(CodePoint.SYNERRCD_REQ_OBJ_NOT_FOUND,codePoint); 7427 } 7428 } 7429 7437 private void verifyInOrderACCSEC_SECCHK(int codePoint, int reqCodePoint) 7438 throws DRDAProtocolException 7439 { 7440 if (codePoint != reqCodePoint ) 7441 { 7442 throw 7443 new DRDAProtocolException(DRDAProtocolException.DRDA_Proto_PRCCNVRM, 7444 this, codePoint, 7445 CodePoint.PRCCNVCD_ACCSEC_SECCHK_WRONG_STATE); 7446 } 7447 } 7448 7449 7456 private void rdbnamMismatch(int codePoint) 7457 throws DRDAProtocolException 7458 { 7459 throw new DRDAProtocolException(DRDAProtocolException.DRDA_Proto_PRCCNVRM, 7460 this, codePoint, 7461 CodePoint.PRCCNVCD_RDBNAM_MISMATCH); 7462 } 7463 7466 private void closeSession() 7467 { 7468 if (session == null) 7469 return; 7470 server.removeFromSessionTable(session.connNum); 7471 try { 7472 session.close(); 7473 } catch (SQLException se) 7474 { 7475 sendUnexpectedException(se); 7479 close(); 7480 } 7481 finally { 7482 session = null; 7483 database = null; 7484 appRequester=null; 7485 sockis = null; 7486 sockos=null; 7487 databaseAccessException=null; 7488 } 7489 } 7490 7491 7495 private void handleException(Exception e) 7496 { 7497 try { 7498 if (e instanceof DRDAProtocolException) { 7499 sendProtocolException((DRDAProtocolException) e); 7501 } else { 7502 sendUnexpectedException(e); 7504 server.consoleExceptionPrintTrace(e); 7505 } 7506 } finally { 7507 closeSession(); 7510 close(); 7511 } 7512 } 7513 7514 7519 private void sendProtocolException(DRDAProtocolException de) { 7520 String dbname = null; 7521 if (database != null) { 7522 dbname = database.dbName; 7523 } 7524 7525 try { 7526 println2Log(dbname, session.drdaID, de.getMessage()); 7527 server.consoleExceptionPrintTrace(de); 7528 reader.clearBuffer(); 7529 de.write(writer); 7530 finalizeChain(); 7531 } catch (DRDAProtocolException ioe) { 7532 println2Log(dbname, session.drdaID, de.getMessage()); 7534 server.consoleExceptionPrintTrace(ioe); 7535 } 7536 } 7537 7538 7542 private void sendUnexpectedException(Exception e) 7543 { 7544 7545 DRDAProtocolException unExpDe; 7546 String dbname = null; 7547 try { 7548 if (database != null) 7549 dbname = database.dbName; 7550 println2Log(dbname,session.drdaID, e.getMessage()); 7551 server.consoleExceptionPrintTrace(e); 7552 unExpDe = DRDAProtocolException.newAgentError(this, 7553 CodePoint.SVRCOD_PRMDMG, 7554 dbname, e.getMessage()); 7555 7556 reader.clearBuffer(); 7557 unExpDe.write(writer); 7558 finalizeChain(); 7559 } 7560 catch (DRDAProtocolException nde) 7561 { 7562 } 7564 7565 } 7566 7567 7568 7573 private boolean closed() 7574 { 7575 synchronized (closeSync) 7576 { 7577 return close; 7578 } 7579 } 7580 7585 private boolean getLogConnections() 7586 { 7587 synchronized(logConnectionsSync) { 7588 return logConnections; 7589 } 7590 } 7591 7596 private long getTimeSlice() 7597 { 7598 synchronized(timeSliceSync) { 7599 return timeSlice; 7600 } 7601 } 7602 7607 protected void trace(String value) 7608 { 7609 if (SanityManager.DEBUG && server.debugOutput == true) 7610 server.consoleMessage(value); 7611 } 7612 7613 7617 public static void showmem() { 7618 Runtime rt = null; 7619 Date d = null; 7620 rt = Runtime.getRuntime(); 7621 rt.gc(); 7622 d = new Date (); 7623 System.out.println("total memory: " 7624 + rt.totalMemory() 7625 + " free: " 7626 + rt.freeMemory() 7627 + " " + d.toString()); 7628 7629 } 7630 7631 7637 private String convertToHexString(byte [] buf) 7638 { 7639 StringBuffer str = new StringBuffer (); 7640 str.append("0x"); 7641 String val; 7642 int byteVal; 7643 for (int i = 0; i < buf.length; i++) 7644 { 7645 byteVal = buf[i] & 0xff; 7646 val = Integer.toHexString(byteVal); 7647 if (val.length() < 2) 7648 str.append("0"); 7649 str.append(val); 7650 } 7651 return str.toString(); 7652 } 7653 7660 private void checkValidTypDefNam(String typdefnam) 7661 throws DRDAProtocolException 7662 { 7663 if (typdefnam.equals("QTDSQL370")) 7664 return; 7665 if (typdefnam.equals("QTDSQL400")) 7666 return; 7667 if (typdefnam.equals("QTDSQLX86")) 7668 return; 7669 if (typdefnam.equals("QTDSQLASC")) 7670 return; 7671 if (typdefnam.equals("QTDSQLVAX")) 7672 return; 7673 if (typdefnam.equals("QTDSQLJVM")) 7674 return; 7675 invalidValue(CodePoint.TYPDEFNAM); 7676 } 7677 7685 private void checkLength(int codepoint, int reqlen) 7686 throws DRDAProtocolException 7687 { 7688 long len = reader.getDdmLength(); 7689 if (len < reqlen) 7690 badObjectLength(codepoint); 7691 else if (len > reqlen) 7692 tooBig(codepoint); 7693 } 7694 7702 private boolean readBoolean(int codepoint) throws DRDAProtocolException 7703 { 7704 checkLength(codepoint, 1); 7705 byte val = reader.readByte(); 7706 if (val == CodePoint.TRUE) 7707 return true; 7708 else if (val == CodePoint.FALSE) 7709 return false; 7710 else 7711 invalidValue(codepoint); 7712 return false; } 7714 7718 private void addDatabase(String dbname) 7719 { 7720 Database db; 7721 if (appRequester.isXARequester()) 7722 { 7723 db = new XADatabase(dbname); 7724 } 7725 else 7726 db = new Database(dbname); 7727 session.addDatabase(db); 7728 session.database = db; 7729 database = db; 7730 } 7731 7738 private void setDatabase(int codePoint) throws DRDAProtocolException 7739 { 7740 String rdbnam = parseRDBNAM(); 7741 if (database != null && database.dbName.equals(rdbnam)) 7743 return; 7744 Database d = session.getDatabase(rdbnam); 7745 if (d == null) 7746 rdbnamMismatch(codePoint); 7747 else 7748 database = d; 7749 session.database = d; 7750 } 7751 7761 private void writeENDUOWRM(int opType) 7762 { 7763 writer.createDssReply(); 7764 writer.startDdm(CodePoint.ENDUOWRM); 7765 writer.writeScalar2Bytes(CodePoint.SVRCOD, CodePoint.SVRCOD_WARNING); 7766 writer.writeScalar1Byte(CodePoint.UOWDSP, opType); 7767 writer.endDdmAndDss(); 7768 } 7769 7770 void writeEXTDTA (DRDAStatement stmt) throws SQLException , DRDAProtocolException 7771 { 7772 7773 ArrayList extdtaValues = stmt.getExtDtaObjects(); 7774 if (extdtaValues == null) 7776 return; 7777 boolean chainFlag, chainedWithSameCorrelator; 7778 boolean writeNullByte = false; 7779 7780 for (int i = 0; i < extdtaValues.size(); i++) { 7781 if (i != extdtaValues.size() - 1) { chainFlag = true; 7784 chainedWithSameCorrelator = true; 7785 } 7786 else { chainFlag = false; chainedWithSameCorrelator = false; 7789 } 7790 7791 7792 if (sqlamLevel >= MGRLVL_7) 7793 if (stmt.isExtDtaValueNullable(i)) 7794 writeNullByte = true; 7795 7796 Object o = extdtaValues.get(i); 7797 if (o instanceof EXTDTAInputStream) { 7798 EXTDTAInputStream stream = (EXTDTAInputStream) o; 7799 try{ 7800 writer.writeScalarStream (chainedWithSameCorrelator, 7801 CodePoint.EXTDTA, 7802 stream, 7803 writeNullByte); 7804 7805 }finally{ 7806 closeStream(stream); 7808 } 7809 7810 } 7811 } 7812 stmt.clearExtDtaObjects(); 7814 7815 } 7816 7817 7818 7831 private void checkWarning(Connection conn, Statement stmt, ResultSet rs, 7832 int updateCount, boolean alwaysSend, boolean sendWarn) 7833 throws DRDAProtocolException, SQLException 7834 { 7835 SQLWarning warning = null; 7838 SQLWarning reportWarning = null; 7839 try 7840 { 7841 if (stmt != null) 7842 { 7843 warning = stmt.getWarnings(); 7844 if (warning != null) 7845 { 7846 stmt.clearWarnings(); 7847 reportWarning = warning; 7848 } 7849 } 7850 if (rs != null) 7851 { 7852 warning = rs.getWarnings(); 7853 if (warning != null) 7854 { 7855 rs.clearWarnings(); 7856 if (reportWarning == null) 7857 reportWarning = warning; 7858 } 7859 } 7860 if (conn != null) 7861 { 7862 warning = conn.getWarnings(); 7863 if (warning != null) 7864 { 7865 conn.clearWarnings(); 7866 if (reportWarning == null) 7867 reportWarning = warning; 7868 } 7869 } 7870 7871 } 7872 catch (SQLException se) 7873 { 7874 if (SanityManager.DEBUG) 7875 trace("got SQLException while trying to get warnings."); 7876 } 7877 7878 7879 if ((alwaysSend || reportWarning != null) && sendWarn) 7880 writeSQLCARDs(reportWarning, updateCount); 7881 } 7882 7883 7884 protected String buildRuntimeInfo(String indent, LocalizedResource localLangUtil ) 7885 { 7886 String s =""; 7887 if (session == null) 7888 return s; 7889 else 7890 s += session.buildRuntimeInfo("", localLangUtil); 7891 s += "\n"; 7892 return s; 7893 } 7894 7895 7896 7900 private void finalizeChain() throws DRDAProtocolException { 7901 7902 writer.finalizeChain(reader.getCurrChainState(), getOutputStream()); 7903 return; 7904 7905 } 7906 7907 7925 private int validateSecMecUSRSSBPWD() throws DRDAProtocolException 7926 { 7927 String dbName = null; 7928 AuthenticationService authenticationService = null; 7929 org.apache.derby.iapi.db.Database databaseObj = null; 7930 String srvrlslv = appRequester.srvrlslv; 7931 7932 if ((srvrlslv == null) || (srvrlslv.length() == 0) || 7942 (srvrlslv.length() < CodePoint.PRDID_MAX) || 7943 (srvrlslv.indexOf(DRDAConstants.DERBY_DRDA_CLIENT_ID) 7944 == -1)) 7945 return CodePoint.SECCHKCD_NOTSUPPORTED; 7947 7948 appRequester.setClientVersion( 7962 srvrlslv.substring(0, (int) CodePoint.PRDID_MAX)); 7963 7964 if (appRequester.supportsSecMecUSRSSBPWD() == false) 7965 return CodePoint.SECCHKCD_NOTSUPPORTED; 7967 dbName = database.shortDbName; 7968 if ((dbName == null) || (dbName.length() == 0)) 7974 { 7975 authenticationService = ((InternalDriver) 7981 NetworkServerControlImpl.getDriver()).getAuthenticationService(); 7982 } 7983 else 7984 { 7985 if (Monitor.getMonitor() != null) 7992 databaseObj = (org.apache.derby.iapi.db.Database) 7993 Monitor.findService(Property.DATABASE_MODULE, dbName); 7994 7995 if (databaseObj == null) 7996 { 7997 database.makeDummyConnection(); 7999 8000 databaseObj = (org.apache.derby.iapi.db.Database) 8002 Monitor.findService(Property.DATABASE_MODULE, dbName); 8003 } 8004 8005 if (databaseObj != null) 8010 authenticationService = 8011 databaseObj.getAuthenticationService(); 8012 } 8013 8014 if (authenticationService != null) 8016 { 8017 String authClassName = authenticationService.getClass().getName(); 8018 8019 if (!authClassName.equals(AUTHENTICATION_PROVIDER_BUILTIN_CLASS) && 8020 !authClassName.equals(AUTHENTICATION_PROVIDER_NONE_CLASS)) 8021 return CodePoint.SECCHKCD_NOTSUPPORTED; } 8023 8024 try { 8026 myTargetSeed = decryptionManager.generateSeed(); 8027 database.secTokenOut = myTargetSeed; 8028 } catch (SQLException se) { 8029 println2Log(null, session.drdaID, se.getMessage()); 8030 return CodePoint.SECCHKCD_0A; 8032 } 8033 8034 return 0; } 8036 8037 private static int peekStream(EXTDTAInputStream is) throws IOException { 8038 8039 is.mark(1); 8040 8041 try{ 8042 return is.read(); 8043 8044 }finally{ 8045 is.reset(); 8046 } 8047 8048 } 8049 8050 8051 private static void closeStream(InputStream stream){ 8052 8053 try{ 8054 if (stream != null) 8055 stream.close(); 8056 8057 } catch (IOException e) { 8058 Util.javaException(e); 8059 8060 } 8061 8062 } 8063} 8064 | Popular Tags |