1 17 package org.alfresco.filesys.smb.server; 18 19 import java.io.IOException ; 20 21 import org.alfresco.filesys.server.filesys.NetworkFile; 22 import org.alfresco.filesys.server.filesys.TooManyFilesException; 23 import org.alfresco.filesys.server.filesys.TreeConnection; 24 import org.alfresco.filesys.smb.PacketType; 25 import org.alfresco.filesys.smb.SMBStatus; 26 import org.alfresco.filesys.smb.TransactionNames; 27 import org.alfresco.filesys.smb.dcerpc.DCEPipeType; 28 import org.alfresco.filesys.smb.dcerpc.server.DCEPipeFile; 29 import org.alfresco.filesys.smb.dcerpc.server.DCEPipeHandler; 30 import org.alfresco.filesys.util.DataBuffer; 31 import org.alfresco.filesys.util.DataPacker; 32 import org.apache.commons.logging.Log; 33 import org.apache.commons.logging.LogFactory; 34 35 40 class IPCHandler 41 { 42 43 45 private static final Log logger = LogFactory.getLog("org.alfresco.smb.protocol"); 46 47 55 public static void processIPCRequest(SMBSrvSession sess, SMBSrvPacket outPkt) throws java.io.IOException , 56 SMBSrvException 57 { 58 59 61 SMBSrvPacket smbPkt = sess.getReceivePacket(); 62 63 66 int treeId = smbPkt.getTreeId(); 67 TreeConnection conn = sess.findConnection(treeId); 68 69 if (conn == null) 70 { 71 sess.sendErrorResponseSMB(SMBStatus.DOSInvalidDrive, SMBStatus.ErrDos); 72 return; 73 } 74 75 77 if (logger.isDebugEnabled() && sess.hasDebug(SMBSrvSession.DBG_IPC)) 78 logger.debug("IPC$ Request [" + treeId + "] - cmd = " + smbPkt.getPacketTypeString()); 79 80 82 switch (smbPkt.getCommand()) 83 { 84 85 87 case PacketType.OpenAndX: 88 case PacketType.OpenFile: 89 procIPCFileOpen(sess, smbPkt, outPkt); 90 break; 91 92 94 case PacketType.ReadFile: 95 procIPCFileRead(sess, smbPkt, outPkt); 96 break; 97 98 100 case PacketType.ReadAndX: 101 procIPCFileReadAndX(sess, smbPkt, outPkt); 102 break; 103 104 106 case PacketType.WriteFile: 107 procIPCFileWrite(sess, smbPkt, outPkt); 108 break; 109 110 112 case PacketType.WriteAndX: 113 procIPCFileWriteAndX(sess, smbPkt, outPkt); 114 break; 115 116 118 case PacketType.CloseFile: 119 procIPCFileClose(sess, smbPkt, outPkt); 120 break; 121 122 124 case PacketType.NTCreateAndX: 125 procNTCreateAndX(sess, smbPkt, outPkt); 126 break; 127 128 130 default: 131 sess.sendErrorResponseSMB(SMBStatus.SRVUnrecognizedCommand, SMBStatus.ErrSrv); 132 break; 133 } 134 } 135 136 143 protected static void procTransaction(SrvTransactBuffer tbuf, SMBSrvSession sess, SMBSrvPacket outPkt) 144 throws IOException , SMBSrvException 145 { 146 147 149 if (logger.isDebugEnabled() && sess.hasDebug(SMBSrvSession.DBG_IPC)) 150 logger.debug("IPC$ Transaction pipe=" + tbuf.getName() + ", subCmd=" 151 + NamedPipeTransaction.getSubCommand(tbuf.getFunction())); 152 153 155 if (tbuf.getName().compareTo(TransactionNames.PipeLanman) == 0) 156 { 157 158 160 if (PipeLanmanHandler.processRequest(tbuf, sess, outPkt)) 161 return; 162 } 163 164 166 switch (tbuf.getFunction()) 167 { 168 169 171 case NamedPipeTransaction.SetNmPHandState: 172 procSetNamedPipeHandleState(sess, tbuf, outPkt); 173 break; 174 175 177 case NamedPipeTransaction.TransactNmPipe: 178 DCERPCHandler.processDCERPCRequest(sess, tbuf, outPkt); 179 break; 180 181 183 default: 184 sess.sendErrorResponseSMB(SMBStatus.SRVUnrecognizedCommand, SMBStatus.ErrSrv); 185 break; 186 } 187 } 188 189 196 protected static void procIPCFileOpen(SMBSrvSession sess, SMBSrvPacket rxPkt, SMBSrvPacket outPkt) 197 throws IOException , SMBSrvException 198 { 199 200 202 int dataPos = rxPkt.getByteOffset(); 203 int dataLen = rxPkt.getByteCount(); 204 byte[] buf = rxPkt.getBuffer(); 205 206 208 String fileName = DataPacker.getString(buf, dataPos, dataLen); 209 210 212 if (logger.isDebugEnabled() && sess.hasDebug(SMBSrvSession.DBG_IPC)) 213 logger.debug("IPC$ Open file = " + fileName); 214 215 217 int pipeType = DCEPipeType.getNameAsType(fileName); 218 if (pipeType == -1) 219 { 220 sess.sendErrorResponseSMB(SMBStatus.DOSFileNotFound, SMBStatus.ErrDos); 221 return; 222 } 223 224 226 int treeId = rxPkt.getTreeId(); 227 TreeConnection conn = sess.findConnection(treeId); 228 229 if (conn == null) 230 { 231 sess.sendErrorResponseSMB(SMBStatus.SRVInvalidTID, SMBStatus.ErrSrv); 232 return; 233 } 234 235 237 DCEPipeFile pipeFile = new DCEPipeFile(pipeType); 238 pipeFile.setGrantedAccess(NetworkFile.READWRITE); 239 240 242 int fid = -1; 243 244 try 245 { 246 fid = conn.addFile(pipeFile, sess); 247 } 248 catch (TooManyFilesException ex) 249 { 250 251 253 sess.sendErrorResponseSMB(SMBStatus.DOSTooManyOpenFiles, SMBStatus.ErrDos); 254 return; 255 } 256 257 259 outPkt.setParameterCount(15); 260 261 outPkt.setAndXCommand(0xFF); 262 outPkt.setParameter(1, 0); 264 outPkt.setParameter(2, fid); 265 outPkt.setParameter(3, 0); outPkt.setParameter(4, 0); outPkt.setParameter(5, 0); outPkt.setParameterLong(6, 0); outPkt.setParameter(8, 0); 270 outPkt.setParameter(9, 0); 271 outPkt.setParameter(10, 0); outPkt.setParameter(11, 0); 273 outPkt.setParameter(12, 0); outPkt.setParameter(13, 0); 275 outPkt.setParameter(14, 0); 276 277 outPkt.setByteCount(0); 278 279 281 sess.sendResponseSMB(outPkt); 282 } 283 284 291 protected static void procIPCFileRead(SMBSrvSession sess, SMBSrvPacket rxPkt, SMBSrvPacket outPkt) 292 throws IOException , SMBSrvException 293 { 294 295 297 if (rxPkt.checkPacketIsValid(5, 0) == false) 298 { 299 300 302 sess.sendErrorResponseSMB(SMBStatus.SRVUnrecognizedCommand, SMBStatus.ErrSrv); 303 return; 304 } 305 306 308 if (logger.isDebugEnabled() && sess.hasDebug(SMBSrvSession.DBG_IPC)) 309 logger.debug("IPC$ File Read"); 310 311 313 DCERPCHandler.processDCERPCRead(sess, rxPkt, outPkt); 314 } 315 316 323 protected static void procIPCFileReadAndX(SMBSrvSession sess, SMBSrvPacket rxPkt, SMBSrvPacket outPkt) 324 throws IOException , SMBSrvException 325 { 326 327 329 if (rxPkt.checkPacketIsValid(10, 0) == false) 330 { 331 332 334 sess.sendErrorResponseSMB(SMBStatus.SRVUnrecognizedCommand, SMBStatus.ErrSrv); 335 return; 336 } 337 338 340 if (logger.isDebugEnabled() && sess.hasDebug(SMBSrvSession.DBG_IPC)) 341 logger.debug("IPC$ File Read AndX"); 342 343 345 DCERPCHandler.processDCERPCRead(sess, rxPkt, outPkt); 346 } 347 348 355 protected static void procIPCFileWrite(SMBSrvSession sess, SMBSrvPacket rxPkt, SMBSrvPacket outPkt) 356 throws IOException , SMBSrvException 357 { 358 359 361 if (rxPkt.checkPacketIsValid(5, 0) == false) 362 { 363 364 366 sess.sendErrorResponseSMB(SMBStatus.SRVUnrecognizedCommand, SMBStatus.ErrSrv); 367 return; 368 } 369 370 372 if (logger.isDebugEnabled() && sess.hasDebug(SMBSrvSession.DBG_IPC)) 373 logger.debug("IPC$ File Write"); 374 375 377 DCERPCHandler.processDCERPCRequest(sess, rxPkt, outPkt); 378 } 379 380 387 protected static void procIPCFileWriteAndX(SMBSrvSession sess, SMBSrvPacket rxPkt, SMBSrvPacket outPkt) 388 throws IOException , SMBSrvException 389 { 390 391 393 if (rxPkt.checkPacketIsValid(12, 0) == false) 394 { 395 396 398 sess.sendErrorResponseSMB(SMBStatus.SRVUnrecognizedCommand, SMBStatus.ErrSrv); 399 return; 400 } 401 402 404 if (logger.isDebugEnabled() && sess.hasDebug(SMBSrvSession.DBG_IPC)) 405 logger.debug("IPC$ File Write AndX"); 406 407 409 DCERPCHandler.processDCERPCRequest(sess, rxPkt, outPkt); 410 } 411 412 419 protected static void procIPCFileClose(SMBSrvSession sess, SMBSrvPacket rxPkt, SMBSrvPacket outPkt) 420 throws IOException , SMBSrvException 421 { 422 423 425 if (rxPkt.checkPacketIsValid(3, 0) == false) 426 { 427 sess.sendErrorResponseSMB(SMBStatus.SRVUnrecognizedCommand, SMBStatus.ErrSrv); 428 return; 429 } 430 431 434 int treeId = rxPkt.getTreeId(); 435 TreeConnection conn = sess.findConnection(treeId); 436 437 if (conn == null) 438 { 439 sess.sendErrorResponseSMB(SMBStatus.DOSInvalidDrive, SMBStatus.ErrDos); 440 return; 441 } 442 443 445 int fid = rxPkt.getParameter(0); 446 DCEPipeFile netFile = (DCEPipeFile) conn.findFile(fid); 447 448 if (netFile == null) 449 { 450 sess.sendErrorResponseSMB(SMBStatus.DOSInvalidHandle, SMBStatus.ErrDos); 451 return; 452 } 453 454 456 if (logger.isDebugEnabled() && sess.hasDebug(SMBSrvSession.DBG_IPC)) 457 logger.debug("IPC$ File close [" + treeId + "] fid=" + fid); 458 459 461 conn.removeFile(fid, sess); 462 463 465 outPkt.setParameterCount(0); 466 outPkt.setByteCount(0); 467 468 470 sess.sendResponseSMB(outPkt); 471 } 472 473 480 protected static void procSetNamedPipeHandleState(SMBSrvSession sess, SrvTransactBuffer tbuf, SMBSrvPacket outPkt) 481 throws IOException , SMBSrvException 482 { 483 484 486 DataBuffer setupBuf = tbuf.getSetupBuffer(); 487 setupBuf.skipBytes(2); 488 int fid = setupBuf.getShort(); 489 490 DataBuffer paramBuf = tbuf.getParameterBuffer(); 491 int state = paramBuf.getShort(); 492 493 495 TreeConnection conn = sess.findConnection(tbuf.getTreeId()); 496 497 499 DCEPipeFile netFile = (DCEPipeFile) conn.findFile(fid); 500 if (netFile == null) 501 { 502 sess.sendErrorResponseSMB(SMBStatus.DOSInvalidHandle, SMBStatus.ErrDos); 503 return; 504 } 505 506 508 if (logger.isDebugEnabled() && sess.hasDebug(SMBSrvSession.DBG_IPC)) 509 logger.debug(" SetNmPHandState pipe=" + netFile.getName() + ", fid=" + fid + ", state=0x" 510 + Integer.toHexString(state)); 511 512 514 netFile.setPipeState(state); 515 516 518 SMBSrvTransPacket.initTransactReply(outPkt, 0, 0, 0, 0); 519 520 522 sess.sendResponseSMB(outPkt); 523 } 524 525 532 protected static void procNTCreateAndX(SMBSrvSession sess, SMBSrvPacket rxPkt, SMBSrvPacket outPkt) 533 throws IOException , SMBSrvException 534 { 535 536 539 int treeId = rxPkt.getTreeId(); 540 TreeConnection conn = sess.findConnection(treeId); 541 542 if (conn == null) 543 { 544 sess.sendErrorResponseSMB(SMBStatus.NTInvalidParameter, SMBStatus.NTErr); 545 return; 546 } 547 548 550 NTParameterPacker prms = new NTParameterPacker(rxPkt.getBuffer(), SMBSrvPacket.PARAMWORDS + 5); 551 552 int nameLen = prms.unpackWord(); 553 int flags = prms.unpackInt(); 554 int rootFID = prms.unpackInt(); 555 int accessMask = prms.unpackInt(); 556 long allocSize = prms.unpackLong(); 557 int attrib = prms.unpackInt(); 558 int shrAccess = prms.unpackInt(); 559 int createDisp = prms.unpackInt(); 560 int createOptn = prms.unpackInt(); 561 int impersonLev = prms.unpackInt(); 562 int secFlags = prms.unpackByte(); 563 564 566 int pos = DataPacker.wordAlign(rxPkt.getByteOffset()); 567 String fileName = DataPacker.getUnicodeString(rxPkt.getBuffer(), pos, nameLen); 568 if (fileName == null) 569 { 570 sess.sendErrorResponseSMB(SMBStatus.NTInvalidParameter, SMBStatus.NTErr); 571 return; 572 } 573 574 576 if (logger.isDebugEnabled() && sess.hasDebug(SMBSrvSession.DBG_IPC)) 577 logger.debug("NT Create AndX [" + treeId + "] name=" + fileName + ", flags=0x" 578 + Integer.toHexString(flags) + ", attr=0x" + Integer.toHexString(attrib) + ", allocSize=" 579 + allocSize); 580 581 583 if (fileName.startsWith("\\PIPE") == false) 584 fileName = "\\PIPE" + fileName; 585 586 588 int pipeType = DCEPipeType.getNameAsType(fileName); 589 if (pipeType == -1) 590 { 591 sess.sendErrorResponseSMB(SMBStatus.NTObjectNotFound, SMBStatus.NTErr); 592 return; 593 } 594 595 597 if (DCEPipeHandler.getHandlerForType(pipeType) == null) 598 { 599 sess.sendErrorResponseSMB(SMBStatus.NTAccessDenied, SMBStatus.NTErr); 600 return; 601 } 602 603 605 DCEPipeFile pipeFile = new DCEPipeFile(pipeType); 606 pipeFile.setGrantedAccess(NetworkFile.READWRITE); 607 608 610 int fid = -1; 611 612 try 613 { 614 fid = conn.addFile(pipeFile, sess); 615 } 616 catch (TooManyFilesException ex) 617 { 618 619 621 sess.sendErrorResponseSMB(SMBStatus.Win32InvalidHandle, SMBStatus.NTErr); 622 return; 623 } 624 625 627 outPkt.setParameterCount(34); 628 629 prms.reset(outPkt.getBuffer(), SMBSrvPacket.PARAMWORDS + 4); 630 631 prms.packByte(0); 632 prms.packWord(fid); 633 prms.packInt(0x0001); 635 prms.packLong(0); prms.packLong(0); prms.packLong(0); prms.packLong(0); 640 prms.packInt(0x0080); prms.packLong(4096); prms.packLong(0); prms.packWord(2); prms.packByte(0xFF); prms.packByte(0x05); 647 prms.packByte(0); 649 outPkt.setByteCount(0); 650 651 outPkt.setAndXCommand(0xFF); 652 outPkt.setParameter(1, outPkt.getLength()); 654 656 sess.sendResponseSMB(outPkt); 657 } 658 } | Popular Tags |