KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > filesys > smb > server > PipeLanmanHandler


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.filesys.smb.server;
18
19 import java.io.IOException JavaDoc;
20 import java.util.Enumeration JavaDoc;
21
22 import org.alfresco.filesys.server.core.ShareType;
23 import org.alfresco.filesys.server.core.SharedDevice;
24 import org.alfresco.filesys.server.core.SharedDeviceList;
25 import org.alfresco.filesys.smb.PacketType;
26 import org.alfresco.filesys.smb.SMBStatus;
27 import org.alfresco.filesys.smb.TransactBuffer;
28 import org.alfresco.filesys.util.DataBuffer;
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31
32 /**
33  * IPC$ Transaction handler for \PIPE\LANMAN requests.
34  */

35 class PipeLanmanHandler
36 {
37     private static final Log logger = LogFactory.getLog("org.alfresco.smb.protocol");
38
39     // Server capability flags
40

41     public static final int WorkStation = 0x00000001;
42     public static final int Server = 0x00000002;
43     public static final int SQLServer = 0x00000004;
44     public static final int DomainCtrl = 0x00000008;
45     public static final int DomainBakCtrl = 0x00000010;
46     public static final int TimeSource = 0x00000020;
47     public static final int AFPServer = 0x00000040;
48     public static final int NovellServer = 0x00000080;
49     public static final int DomainMember = 0x00000100;
50     public static final int PrintServer = 0x00000200;
51     public static final int DialinServer = 0x00000400;
52     public static final int UnixServer = 0x00000800;
53     public static final int NTServer = 0x00001000;
54     public static final int WfwServer = 0x00002000;
55     public static final int MFPNServer = 0x00004000;
56     public static final int NTNonDCServer = 0x00008000;
57     public static final int PotentialBrowse = 0x00010000;
58     public static final int BackupBrowser = 0x00020000;
59     public static final int MasterBrowser = 0x00040000;
60     public static final int DomainMaster = 0x00080000;
61     public static final int OSFServer = 0x00100000;
62     public static final int VMSServer = 0x00200000;
63     public static final int Win95Plus = 0x00400000;
64     public static final int DFSRoot = 0x00800000;
65     public static final int NTCluster = 0x01000000;
66     public static final int TerminalServer = 0x02000000;
67     public static final int DCEServer = 0x10000000;
68     public static final int AlternateXport = 0x20000000;
69     public static final int LocalListOnly = 0x40000000;
70     public static final int DomainEnum = 0x80000000;
71
72     /**
73      * Process a \PIPE\LANMAN transaction request.
74      *
75      * @param tbuf Transaction setup, parameter and data buffers
76      * @param sess SMB server session that received the transaction.
77      * @param trans Packet to use for reply
78      * @return true if the transaction has been handled, else false.
79      * @exception java.io.IOException If an I/O error occurs
80      * @exception SMBSrvException If an SMB protocol error occurs
81      */

82     public final static boolean processRequest(TransactBuffer tbuf, SMBSrvSession sess, SMBSrvPacket trans)
83             throws IOException JavaDoc, SMBSrvException
84     {
85
86         // Create a transaction packet
87

88         SMBSrvTransPacket tpkt = new SMBSrvTransPacket(trans.getBuffer());
89
90         // Get the transaction command code, parameter descriptor and data descriptor strings from
91
// the parameter block.
92

93         DataBuffer paramBuf = tbuf.getParameterBuffer();
94
95         int cmd = paramBuf.getShort();
96         String JavaDoc prmDesc = paramBuf.getString(false);
97         String JavaDoc dataDesc = paramBuf.getString(false);
98
99         // Debug
100

101         if (logger.isDebugEnabled() && sess.hasDebug(SMBSrvSession.DBG_IPC))
102             logger.debug("\\PIPE\\LANMAN\\ transact request, cmd=" + cmd + ", prm=" + prmDesc + ", data=" + dataDesc);
103
104         // Call the required transaction handler
105

106         boolean processed = false;
107
108         switch (cmd)
109         {
110
111         // Share
112

113         case PacketType.RAPShareEnum:
114             processed = procNetShareEnum(sess, tbuf, prmDesc, dataDesc, tpkt);
115             break;
116
117         // Get share information
118

119         case PacketType.RAPShareGetInfo:
120             processed = procNetShareGetInfo(sess, tbuf, prmDesc, dataDesc, tpkt);
121             break;
122
123         // Workstation information
124

125         case PacketType.RAPWkstaGetInfo:
126             processed = procNetWkstaGetInfo(sess, tbuf, prmDesc, dataDesc, tpkt);
127             break;
128
129         // Server information
130

131         case PacketType.RAPServerGetInfo:
132             processed = procNetServerGetInfo(sess, tbuf, prmDesc, dataDesc, tpkt);
133             break;
134
135         // Print queue information
136

137         case PacketType.NetPrintQGetInfo:
138             processed = procNetPrintQGetInfo(sess, tbuf, prmDesc, dataDesc, tpkt);
139             break;
140
141         // No handler
142

143         default:
144
145             // Debug
146

147             if (logger.isDebugEnabled() && sess.hasDebug(SMBSrvSession.DBG_IPC))
148                 logger.debug("No handler for \\PIPE\\LANMAN\\ request, cmd=" + cmd + ", prm=" + prmDesc + ", data="
149                         + dataDesc);
150             break;
151         }
152
153         // Return the transaction processed status
154

155         return processed;
156     }
157
158     /**
159      * Process a NetServerGetInfo transaction request.
160      *
161      * @param sess Server session that received the request.
162      * @param tbuf Transaction buffer
163      * @param prmDesc Parameter descriptor string.
164      * @param dataDesc Data descriptor string.
165      * @param tpkt Transaction reply packet
166      * @return true if the transaction has been processed, else false.
167      */

168     protected final static boolean procNetServerGetInfo(SMBSrvSession sess, TransactBuffer tbuf, String JavaDoc prmDesc,
169             String JavaDoc dataDesc, SMBSrvTransPacket tpkt) throws IOException JavaDoc, SMBSrvException
170     {
171
172         // Validate the parameter string
173

174         if (prmDesc.compareTo("WrLh") != 0)
175             throw new SMBSrvException(SMBStatus.SRVInternalServerError, SMBStatus.ErrSrv);
176
177         // Unpack the server get information specific parameters
178

179         DataBuffer paramBuf = tbuf.getParameterBuffer();
180
181         int infoLevel = paramBuf.getShort();
182         int bufSize = paramBuf.getShort();
183
184         // Debug
185

186         if (logger.isDebugEnabled() && sess.hasDebug(SMBSrvSession.DBG_IPC))
187             logger.debug("NetServerGetInfo infoLevel=" + infoLevel);
188
189         // Check if the information level requested and data descriptor string match
190

191         if (infoLevel == 1 && dataDesc.compareTo("B16BBDz") == 0)
192         {
193
194             // Create the transaction reply data buffer
195

196             TransactBuffer replyBuf = new TransactBuffer(tbuf.isType(), 0, 6, 1024);
197
198             // Pack the parameter block
199

200             paramBuf = replyBuf.getParameterBuffer();
201
202             paramBuf.putShort(0); // status code
203
paramBuf.putShort(0); // converter for strings
204
paramBuf.putShort(1); // number of entries
205

206             // Pack the data block, calculate the size of the fixed data block
207

208             DataBuffer dataBuf = replyBuf.getDataBuffer();
209             int strPos = SMBSrvTransPacket.CalculateDataItemSize("B16BBDz");
210
211             // Pack the server name pointer and string
212

213             dataBuf.putStringPointer(strPos);
214             strPos = dataBuf.putFixedStringAt(sess.getServerName(), 16, strPos);
215
216             // Pack the major/minor version
217

218             dataBuf.putByte(1);
219             dataBuf.putByte(0);
220
221             // Pack the server capability flags
222

223             dataBuf.putInt(sess.getSMBServer().getServerType());
224
225             // Pack the server comment string
226

227             String JavaDoc srvComment = sess.getSMBServer().getComment();
228             if (srvComment == null)
229                 srvComment = "";
230
231             dataBuf.putStringPointer(strPos);
232             strPos = dataBuf.putStringAt(srvComment, strPos, false, true);
233
234             // Set the data block length
235

236             dataBuf.setLength(strPos);
237
238             // Send the transaction response
239

240             tpkt.doTransactionResponse(sess, replyBuf);
241         }
242         else
243             throw new SMBSrvException(SMBStatus.SRVInternalServerError, SMBStatus.ErrSrv);
244
245         // We processed the request
246

247         return true;
248     }
249
250     /**
251      * Process a NetShareEnum transaction request.
252      *
253      * @param sess Server session that received the request.
254      * @param tbuf Transaction buffer
255      * @param prmDesc Parameter descriptor string.
256      * @param dataDesc Data descriptor string.
257      * @param tpkt Transaction reply packet
258      * @return true if the transaction has been processed, else false.
259      */

260     protected final static boolean procNetShareEnum(SMBSrvSession sess, TransactBuffer tbuf, String JavaDoc prmDesc,
261             String JavaDoc dataDesc, SMBSrvTransPacket tpkt) throws IOException JavaDoc, SMBSrvException
262     {
263
264         // Validate the parameter string
265

266         if (prmDesc.compareTo("WrLeh") != 0)
267             throw new SMBSrvException(SMBStatus.SRVInternalServerError, SMBStatus.ErrSrv);
268
269         // Unpack the server get information specific parameters
270

271         DataBuffer paramBuf = tbuf.getParameterBuffer();
272
273         int infoLevel = paramBuf.getShort();
274         int bufSize = paramBuf.getShort();
275
276         // Debug
277

278         if (logger.isDebugEnabled() && sess.hasDebug(SMBSrvSession.DBG_IPC))
279             logger.debug("NetShareEnum infoLevel=" + infoLevel);
280
281         // Check if the information level requested and data descriptor string match
282

283         if (infoLevel == 1 && dataDesc.compareTo("B13BWz") == 0)
284         {
285
286             // Get the share list from the server
287

288             SharedDeviceList shrList = sess.getSMBServer().getShareList(null, sess);
289             int shrCount = 0;
290             int strPos = 0;
291
292             if (shrList != null)
293             {
294
295                 // Calculate the fixed data length
296

297                 shrCount = shrList.numberOfShares();
298                 strPos = SMBSrvTransPacket.CalculateDataItemSize("B13BWz") * shrCount;
299             }
300
301             // Create the transaction reply data buffer
302

303             TransactBuffer replyBuf = new TransactBuffer(tbuf.isType(), 0, 6, bufSize);
304
305             // Pack the parameter block
306

307             paramBuf = replyBuf.getParameterBuffer();
308
309             paramBuf.putShort(0); // status code
310
paramBuf.putShort(0); // converter for strings
311
paramBuf.putShort(shrCount); // number of entries
312
paramBuf.putShort(shrCount); // total number of entries
313

314             // Pack the data block
315

316             DataBuffer dataBuf = replyBuf.getDataBuffer();
317             Enumeration JavaDoc<SharedDevice> enm = shrList.enumerateShares();
318
319             while (enm.hasMoreElements())
320             {
321
322                 // Get the current share
323

324                 SharedDevice shrDev = enm.nextElement();
325
326                 // Pack the share name, share type and comment pointer
327

328                 dataBuf.putFixedString(shrDev.getName(), 13);
329                 dataBuf.putByte(0);
330                 dataBuf.putShort(ShareType.asShareInfoType(shrDev.getType()));
331                 dataBuf.putStringPointer(strPos);
332
333                 if (shrDev.getComment() != null)
334                     strPos = dataBuf.putStringAt(shrDev.getComment(), strPos, false, true);
335                 else
336                     strPos = dataBuf.putStringAt("", strPos, false, true);
337             }
338
339             // Set the data block length
340

341             dataBuf.setLength(strPos);
342
343             // Send the transaction response
344

345             tpkt.doTransactionResponse(sess, replyBuf);
346         }
347         else
348             throw new SMBSrvException(SMBStatus.SRVInternalServerError, SMBStatus.ErrSrv);
349
350         // We processed the request
351

352         return true;
353     }
354
355     /**
356      * Process a NetShareGetInfo transaction request.
357      *
358      * @param sess Server session that received the request.
359      * @param tbuf Transaction buffer
360      * @param prmDesc Parameter descriptor string.
361      * @param dataDesc Data descriptor string.
362      * @param tpkt Transaction reply packet
363      * @return true if the transaction has been processed, else false.
364      */

365     protected final static boolean procNetShareGetInfo(SMBSrvSession sess, TransactBuffer tbuf, String JavaDoc prmDesc,
366             String JavaDoc dataDesc, SMBSrvTransPacket tpkt) throws IOException JavaDoc, SMBSrvException
367     {
368
369         // Validate the parameter string
370

371         if (prmDesc.compareTo("zWrLh") != 0)
372             throw new SMBSrvException(SMBStatus.SRVInternalServerError, SMBStatus.ErrSrv);
373
374         // Unpack the share get information specific parameters
375

376         DataBuffer paramBuf = tbuf.getParameterBuffer();
377
378         String JavaDoc shareName = paramBuf.getString(32, false);
379         int infoLevel = paramBuf.getShort();
380         int bufSize = paramBuf.getShort();
381
382         // Debug
383

384         if (logger.isDebugEnabled() && sess.hasDebug(SMBSrvSession.DBG_IPC))
385             logger.debug("NetShareGetInfo - " + shareName + ", infoLevel=" + infoLevel);
386
387         // Check if the information level requested and data descriptor string match
388

389         if (infoLevel == 1 && dataDesc.compareTo("B13BWz") == 0)
390         {
391
392             // Find the required share information
393

394             SharedDevice share = null;
395
396             try
397             {
398
399                 // Get the shared device details
400

401                 share = sess.getSMBServer().findShare(null, shareName, ShareType.UNKNOWN, sess, false);
402             }
403             catch (Exception JavaDoc ex)
404             {
405             }
406
407             if (share == null)
408             {
409                 sess.sendErrorResponseSMB(SMBStatus.SRVInternalServerError, SMBStatus.ErrSrv);
410                 return true;
411             }
412
413             // Create the transaction reply data buffer
414

415             TransactBuffer replyBuf = new TransactBuffer(tbuf.isType(), 0, 6, 1024);
416
417             // Pack the parameter block
418

419             paramBuf = replyBuf.getParameterBuffer();
420
421             paramBuf.putShort(0); // status code
422
paramBuf.putShort(0); // converter for strings
423
paramBuf.putShort(1); // number of entries
424

425             // Pack the data block, calculate the size of the fixed data block
426

427             DataBuffer dataBuf = replyBuf.getDataBuffer();
428             int strPos = SMBSrvTransPacket.CalculateDataItemSize("B13BWz");
429
430             // Pack the share name
431

432             dataBuf.putStringPointer(strPos);
433             strPos = dataBuf.putFixedStringAt(share.getName(), 13, strPos);
434
435             // Pack unknown byte, alignment ?
436

437             dataBuf.putByte(0);
438
439             // Pack the share type flags
440

441             dataBuf.putShort(share.getType());
442
443             // Pack the share comment
444

445             dataBuf.putStringPointer(strPos);
446
447             if (share.getComment() != null)
448                 strPos = dataBuf.putStringAt(share.getComment(), strPos, false, true);
449             else
450                 strPos = dataBuf.putStringAt("", strPos, false, true);
451
452             // Set the data block length
453

454             dataBuf.setLength(strPos);
455
456             // Send the transaction response
457

458             tpkt.doTransactionResponse(sess, replyBuf);
459         }
460         else
461         {
462
463             // Debug
464

465             if (logger.isDebugEnabled() && sess.hasDebug(SMBSrvSession.DBG_IPC))
466                 logger.debug("NetShareGetInfo - UNSUPPORTED " + shareName + ", infoLevel=" + infoLevel + ", dataDesc="
467                         + dataDesc);
468
469             // Server error
470

471             throw new SMBSrvException(SMBStatus.SRVInternalServerError, SMBStatus.ErrSrv);
472         }
473
474         // We processed the request
475

476         return true;
477     }
478
479     /**
480      * Process a NetWkstaGetInfo transaction request.
481      *
482      * @param sess Server session that received the request.
483      * @param tbuf Transaction buffer
484      * @param prmDesc Parameter descriptor string.
485      * @param dataDesc Data descriptor string.
486      * @param tpkt Transaction reply packet
487      * @return true if the transaction has been processed, else false.
488      */

489     protected final static boolean procNetWkstaGetInfo(SMBSrvSession sess, TransactBuffer tbuf, String JavaDoc prmDesc,
490             String JavaDoc dataDesc, SMBSrvTransPacket tpkt) throws IOException JavaDoc, SMBSrvException
491     {
492
493         // Validate the parameter string
494

495         if (prmDesc.compareTo("WrLh") != 0)
496             throw new SMBSrvException(SMBStatus.SRVInternalServerError, SMBStatus.ErrSrv);
497
498         // Unpack the share get information specific parameters
499

500         DataBuffer paramBuf = tbuf.getParameterBuffer();
501
502         int infoLevel = paramBuf.getShort();
503         int bufSize = paramBuf.getShort();
504
505         // Debug
506

507         if (logger.isDebugEnabled() && sess.hasDebug(SMBSrvSession.DBG_IPC))
508             logger.debug("NetWkstaGetInfo infoLevel=" + infoLevel);
509
510         // Check if the information level requested and data descriptor string match
511

512         if ((infoLevel == 1 && dataDesc.compareTo("zzzBBzzz") == 0)
513                 || (infoLevel == 10 && dataDesc.compareTo("zzzBBzz") == 0))
514         {
515
516             // Create the transaction reply data buffer
517

518             TransactBuffer replyBuf = new TransactBuffer(tbuf.isType(), 0, 6, 1024);
519
520             // Pack the data block, calculate the size of the fixed data block
521

522             DataBuffer dataBuf = replyBuf.getDataBuffer();
523             int strPos = SMBSrvTransPacket.CalculateDataItemSize(dataDesc);
524
525             // Pack the server name
526

527             dataBuf.putStringPointer(strPos);
528             strPos = dataBuf.putStringAt(sess.getServerName(), strPos, false, true);
529
530             // Pack the user name
531

532             dataBuf.putStringPointer(strPos);
533             strPos = dataBuf.putStringAt("", strPos, false, true);
534
535             // Pack the domain name
536

537             dataBuf.putStringPointer(strPos);
538
539             String JavaDoc domain = sess.getServer().getConfiguration().getDomainName();
540             if (domain == null)
541                 domain = "";
542             strPos = dataBuf.putStringAt(domain, strPos, false, true);
543
544             // Pack the major/minor version number
545

546             dataBuf.putByte(4);
547             dataBuf.putByte(2);
548
549             // Pack the logon domain
550

551             dataBuf.putStringPointer(strPos);
552             strPos = dataBuf.putStringAt("", strPos, false, true);
553
554             // Check if the other domains should be packed
555

556             if (infoLevel == 1 && dataDesc.compareTo("zzzBBzzz") == 0)
557             {
558
559                 // Pack the other domains
560

561                 dataBuf.putStringPointer(strPos);
562                 strPos = dataBuf.putStringAt("", strPos, false, true);
563             }
564
565             // Set the data block length
566

567             dataBuf.setLength(strPos);
568
569             // Pack the parameter block
570

571             paramBuf = replyBuf.getParameterBuffer();
572
573             paramBuf.putShort(0); // status code
574
paramBuf.putShort(0); // converter for strings
575
paramBuf.putShort(dataBuf.getLength());
576             paramBuf.putShort(0); // number of entries
577

578             // Send the transaction response
579

580             tpkt.doTransactionResponse(sess, replyBuf);
581         }
582         else
583         {
584
585             // Debug
586

587             if (logger.isDebugEnabled() && sess.hasDebug(SMBSrvSession.DBG_IPC))
588                 logger.debug("NetWkstaGetInfo UNSUPPORTED infoLevel=" + infoLevel + ", dataDesc=" + dataDesc);
589
590             // Unsupported request
591

592             throw new SMBSrvException(SMBStatus.SRVInternalServerError, SMBStatus.ErrSrv);
593         }
594
595         // We processed the request
596

597         return true;
598     }
599
600     /**
601      * Process a NetPrintQGetInfo transaction request.
602      *
603      * @param sess Server session that received the request.
604      * @param tbuf Transaction buffer
605      * @param prmDesc Parameter descriptor string.
606      * @param dataDesc Data descriptor string.
607      * @param tpkt Transaction reply packet
608      * @return true if the transaction has been processed, else false.
609      */

610     protected final static boolean procNetPrintQGetInfo(SMBSrvSession sess, TransactBuffer tbuf, String JavaDoc prmDesc,
611             String JavaDoc dataDesc, SMBSrvTransPacket tpkt) throws IOException JavaDoc, SMBSrvException
612     {
613
614         // Validate the parameter string
615

616         if (prmDesc.compareTo("zWrLh") != 0)
617             throw new SMBSrvException(SMBStatus.SRVInternalServerError, SMBStatus.ErrSrv);
618
619         // Unpack the share get information specific parameters
620

621         DataBuffer paramBuf = tbuf.getParameterBuffer();
622
623         String JavaDoc shareName = paramBuf.getString(32, false);
624         int infoLevel = paramBuf.getShort();
625         int bufSize = paramBuf.getShort();
626
627         // Debug
628

629         if (logger.isDebugEnabled() && sess.hasDebug(SMBSrvSession.DBG_IPC))
630             logger.debug("NetPrintQGetInfo - " + shareName + ", infoLevel=" + infoLevel);
631
632         // We did not process the request
633

634         return false;
635     }
636 }
Popular Tags