KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > filesys > smb > dcerpc > server > SrvsvcDCEHandler


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.dcerpc.server;
18
19 import java.io.IOException JavaDoc;
20 import java.util.Enumeration JavaDoc;
21 import java.util.Vector JavaDoc;
22
23 import org.alfresco.filesys.server.auth.acl.AccessControlManager;
24 import org.alfresco.filesys.server.config.ServerConfiguration;
25 import org.alfresco.filesys.server.core.ShareType;
26 import org.alfresco.filesys.server.core.SharedDevice;
27 import org.alfresco.filesys.server.core.SharedDeviceList;
28 import org.alfresco.filesys.smb.Dialect;
29 import org.alfresco.filesys.smb.SMBStatus;
30 import org.alfresco.filesys.smb.dcerpc.DCEBuffer;
31 import org.alfresco.filesys.smb.dcerpc.DCEBufferException;
32 import org.alfresco.filesys.smb.dcerpc.Srvsvc;
33 import org.alfresco.filesys.smb.dcerpc.info.ServerInfo;
34 import org.alfresco.filesys.smb.dcerpc.info.ShareInfo;
35 import org.alfresco.filesys.smb.dcerpc.info.ShareInfoList;
36 import org.alfresco.filesys.smb.server.SMBServer;
37 import org.alfresco.filesys.smb.server.SMBSrvException;
38 import org.alfresco.filesys.smb.server.SMBSrvSession;
39 import org.apache.commons.logging.Log;
40 import org.apache.commons.logging.LogFactory;
41
42 /**
43  * Srvsvc DCE/RPC Handler Class
44  */

45 public class SrvsvcDCEHandler implements DCEHandler
46 {
47
48     // Debug logging
49

50     private static final Log logger = LogFactory.getLog("org.alfresco.smb.protocol");
51
52     /**
53      * Process a SrvSvc DCE/RPC request
54      *
55      * @param sess SMBSrvSession
56      * @param inBuf DCEBuffer
57      * @param pipeFile DCEPipeFile
58      * @exception IOException
59      * @exception SMBSrvException
60      */

61     public void processRequest(SMBSrvSession sess, DCEBuffer inBuf, DCEPipeFile pipeFile) throws IOException JavaDoc,
62             SMBSrvException
63     {
64
65         // Get the operation code and move the buffer pointer to the start of the request data
66

67         int opNum = inBuf.getHeaderValue(DCEBuffer.HDR_OPCODE);
68         try
69         {
70             inBuf.skipBytes(DCEBuffer.OPERATIONDATA);
71         }
72         catch (DCEBufferException ex)
73         {
74         }
75
76         // Debug
77

78         if (logger.isDebugEnabled() && sess.hasDebug(SMBSrvSession.DBG_DCERPC))
79             logger.debug("DCE/RPC SrvSvc request=" + Srvsvc.getOpcodeName(opNum));
80
81         // Create the output DCE buffer and add the response header
82

83         DCEBuffer outBuf = new DCEBuffer();
84         outBuf.putResponseHeader(inBuf.getHeaderValue(DCEBuffer.HDR_CALLID), 0);
85
86         // Process the request
87

88         boolean processed = false;
89
90         switch (opNum)
91         {
92
93         // Enumerate shares
94

95         case Srvsvc.NetrShareEnum:
96             processed = netShareEnum(sess, inBuf, outBuf);
97             break;
98
99         // Enumerate all shares
100

101         case Srvsvc.NetrShareEnumSticky:
102             processed = netShareEnum(sess, inBuf, outBuf);
103             break;
104
105         // Get share information
106

107         case Srvsvc.NetrShareGetInfo:
108             processed = netShareGetInfo(sess, inBuf, outBuf);
109             break;
110
111         // Get server information
112

113         case Srvsvc.NetrServerGetInfo:
114             processed = netServerGetInfo(sess, inBuf, outBuf);
115             break;
116
117         // Unsupported function
118

119         default:
120             break;
121         }
122
123         // Return an error status if the request was not processed
124

125         if (processed == false)
126         {
127             sess.sendErrorResponseSMB(SMBStatus.SRVNotSupported, SMBStatus.ErrSrv);
128             return;
129         }
130
131         // Set the allocation hint for the response
132

133         outBuf.setHeaderValue(DCEBuffer.HDR_ALLOCHINT, outBuf.getLength());
134
135         // Attach the output buffer to the pipe file
136

137         pipeFile.setBufferedData(outBuf);
138     }
139
140     /**
141      * Handle a share enumeration request
142      *
143      * @param sess SMBSrvSession
144      * @param inBuf DCEPacket
145      * @param outBuf DCEPacket
146      * @return boolean
147      */

148     protected final boolean netShareEnum(SMBSrvSession sess, DCEBuffer inBuf, DCEBuffer outBuf)
149     {
150
151         // Decode the request
152

153         String JavaDoc srvName = null;
154         ShareInfoList shrInfo = null;
155
156         try
157         {
158             inBuf.skipPointer();
159             srvName = inBuf.getString(DCEBuffer.ALIGN_INT);
160             shrInfo = new ShareInfoList(inBuf);
161         }
162         catch (DCEBufferException ex)
163         {
164             return false;
165         }
166
167         // Debug
168

169         if (logger.isDebugEnabled() && sess.hasDebug(SMBSrvSession.DBG_DCERPC))
170             logger.debug("NetShareEnum srvName=" + srvName + ", shrInfo=" + shrInfo.toString());
171
172         // Get the share list from the server
173

174         SharedDeviceList shareList = sess.getServer().getShareMapper().getShareList(srvName, sess, false);
175
176         // Check if there is an access control manager configured
177

178         if (sess.getServer().hasAccessControlManager())
179         {
180
181             // Filter the list of available shares by applying any access control rules
182

183             AccessControlManager aclMgr = sess.getServer().getAccessControlManager();
184
185             shareList = aclMgr.filterShareList(sess, shareList);
186         }
187
188         // Create a list of share information objects of the required information level
189

190         Vector JavaDoc infoList = new Vector JavaDoc();
191         Enumeration JavaDoc<SharedDevice> enm = shareList.enumerateShares();
192
193         while (enm.hasMoreElements())
194         {
195
196             // Get the current shared device details
197

198             SharedDevice share = enm.nextElement();
199
200             // Determine the share type
201

202             int shrTyp = ShareInfo.Disk;
203
204             if (share.getType() == ShareType.PRINTER)
205                 shrTyp = ShareInfo.PrintQueue;
206             else if (share.getType() == ShareType.NAMEDPIPE)
207                 shrTyp = ShareInfo.IPC;
208             else if (share.getType() == ShareType.ADMINPIPE)
209                 shrTyp = ShareInfo.IPC + ShareInfo.Hidden;
210
211             // Create a share information object with the basic information
212

213             ShareInfo info = new ShareInfo(shrInfo.getInformationLevel(), share.getName(), shrTyp, share.getComment());
214             infoList.add(info);
215
216             // Add additional information
217

218             switch (shrInfo.getInformationLevel())
219             {
220
221             // Level 2
222

223             case 2:
224                 if (share.getContext() != null)
225                     info.setPath(share.getContext().getDeviceName());
226                 break;
227
228             // Level 502
229

230             case 502:
231                 if (share.getContext() != null)
232                     info.setPath(share.getContext().getDeviceName());
233                 break;
234             }
235         }
236
237         // Set the share information list in the server share information and write the
238
// share information to the output DCE buffer.
239

240         shrInfo.setShareList(infoList);
241         try
242         {
243             shrInfo.writeList(outBuf);
244             outBuf.putInt(0); // status code
245
}
246         catch (DCEBufferException ex)
247         {
248         }
249
250         // Indicate that the request was processed successfully
251

252         return true;
253     }
254
255     /**
256      * Handle a get share information request
257      *
258      * @param sess SMBSrvSession
259      * @param inBuf DCEPacket
260      * @param outBuf DCEPacket
261      * @return boolean
262      */

263     protected final boolean netShareGetInfo(SMBSrvSession sess, DCEBuffer inBuf, DCEBuffer outBuf)
264     {
265
266         // Decode the request
267

268         String JavaDoc srvName = null;
269         String JavaDoc shrName = null;
270         int infoLevel = 0;
271
272         try
273         {
274             inBuf.skipPointer();
275             srvName = inBuf.getString(DCEBuffer.ALIGN_INT);
276             shrName = inBuf.getString(DCEBuffer.ALIGN_INT);
277             infoLevel = inBuf.getInt();
278         }
279         catch (DCEBufferException ex)
280         {
281             return false;
282         }
283
284         // Debug
285

286         if (logger.isDebugEnabled() && sess.hasDebug(SMBSrvSession.DBG_DCERPC))
287             logger.debug("netShareGetInfo srvname=" + srvName + ", share=" + shrName + ", infoLevel=" + infoLevel);
288
289         // Find the required shared device
290

291         SharedDevice share = null;
292
293         try
294         {
295
296             // Get the shared device details
297

298             share = sess.getServer().findShare(srvName, shrName, ShareType.UNKNOWN, sess, false);
299         }
300         catch (Exception JavaDoc ex)
301         {
302         }
303
304         // Check if the share details are valid
305

306         if (share == null)
307             return false;
308
309         // Determine the share type
310

311         int shrTyp = ShareInfo.Disk;
312
313         if (share.getType() == ShareType.PRINTER)
314             shrTyp = ShareInfo.PrintQueue;
315         else if (share.getType() == ShareType.NAMEDPIPE)
316             shrTyp = ShareInfo.IPC;
317         else if (share.getType() == ShareType.ADMINPIPE)
318             shrTyp = ShareInfo.IPC + ShareInfo.Hidden;
319
320         // Create the share information
321

322         ShareInfo shrInfo = new ShareInfo(infoLevel, share.getName(), shrTyp, share.getComment());
323
324         // Pack the information level, structure pointer and share information
325

326         outBuf.putInt(infoLevel);
327         outBuf.putPointer(true);
328
329         shrInfo.writeObject(outBuf, outBuf);
330
331         // Add the status and return a success status
332

333         outBuf.putInt(0);
334         return true;
335     }
336
337     /**
338      * Handle a get server information request
339      *
340      * @param sess SMBSrvSession
341      * @param inBuf DCEPacket
342      * @param outBuf DCEPacket
343      * @return boolean
344      */

345     protected final boolean netServerGetInfo(SMBSrvSession sess, DCEBuffer inBuf, DCEBuffer outBuf)
346     {
347
348         // Decode the request
349

350         String JavaDoc srvName = null;
351         int infoLevel = 0;
352
353         try
354         {
355             inBuf.skipPointer();
356             srvName = inBuf.getString(DCEBuffer.ALIGN_INT);
357             infoLevel = inBuf.getInt();
358         }
359         catch (DCEBufferException ex)
360         {
361             return false;
362         }
363
364         // Debug
365

366         if (logger.isDebugEnabled() && sess.hasDebug(SMBSrvSession.DBG_DCERPC))
367             logger.debug("netServerGetInfo srvname=" + srvName + ", infoLevel=" + infoLevel);
368
369         // Create the server information and set the common values
370

371         ServerInfo srvInfo = new ServerInfo(infoLevel);
372
373         SMBServer srv = sess.getSMBServer();
374         srvInfo.setServerName(srv.getServerName());
375         srvInfo.setComment(srv.getComment());
376         srvInfo.setServerType(srv.getServerType());
377
378         // Determine if the server is using the NT SMB dialect and set the platofmr id accordingly
379

380         ServerConfiguration srvConfig = srv.getConfiguration();
381         if (srvConfig != null && srvConfig.getEnabledDialects().hasDialect(Dialect.NT) == true)
382         {
383             srvInfo.setPlatformId(ServerInfo.PLATFORM_NT);
384             srvInfo.setVersion(5, 1);
385         }
386         else
387         {
388             srvInfo.setPlatformId(ServerInfo.PLATFORM_OS2);
389             srvInfo.setVersion(4, 0);
390         }
391
392         // Write the server information to the DCE response
393

394         srvInfo.writeObject(outBuf, outBuf);
395         outBuf.putInt(0);
396
397         // Indicate that the request was processed successfully
398

399         return true;
400     }
401 }
402
Popular Tags