public static string[] GetShareInfo(IntPtr handle_b, string sharename, string sHostname) { //// establish null session //if(!Session.EnsureNullSession(sHostname, ce)) //{ // return null; //} try { string[] sShareInfo = null; IntPtr pBuf = IntPtr.Zero; int nret = -1; int param_err = 0; Logger.Log(String.Format("NetShareGetInfo(handle_b={0:x},sHostname={1}, sharename={2}) called", handle_b.ToInt32(), sHostname, sharename), Logger.FileShareManagerLogLevel); if (Configurations.currentPlatform == LikewiseTargetPlatform.Windows) { nret = NetShareGetInfo( sHostname, sharename, 2, out pBuf ); } else { nret = NetShareGetInfo( handle_b, sHostname, sharename, 2, ref pBuf, ref param_err ); } Logger.Log(String.Format( "NetShareGetInfo(); result={0}, pBuf={1}, param_err={2}", nret, pBuf, param_err)); // now, iterate through the data in pBuf IntPtr pCur = pBuf; // marshal the entry into SHARE_INFO_2 si2 = (SHARE_INFO_2)Marshal.PtrToStructure(pCur, typeof(SHARE_INFO_2)); // only allow regular diskshares // TODO: Review this. Should we not display admin shares? if (si2.shi2_type == STYPE_DISKTREE) { sShareInfo = new string[] { "share", si2.shi2_netname, si2.shi2_path, si2.shi2_remark, si2.shi2_current_uses.ToString(), si2.shi2_max_uses.ToString() }; } else { sShareInfo = new string[] { "adminshare", si2.shi2_netname, si2.shi2_path, si2.shi2_remark, si2.shi2_current_uses.ToString(), si2.shi2_max_uses.ToString() }; } if (pBuf != IntPtr.Zero) { // free the data if (Configurations.currentPlatform == LikewiseTargetPlatform.Windows) { NetApiBufferFree(pBuf); } else { SrvSvcFreeMemory(pBuf); } } // all done return(sShareInfo); } catch (Win32Exception we) { throw new LikewiseAPIException(we); } }
public static Dictionary <int, string[]> EnumFiles(IntPtr pHandle, CredentialEntry ce, string sHostname) { // establish null session //if (!Session.EnsureNullSession(sHostname, ce)) //{ // return null; //} try { Dictionary <int, string[]> UserList = new Dictionary <int, string[]>(); IntPtr pBuf = IntPtr.Zero; int cRead = 0; int cTotal = 0; int hResume = 0; int maxlen = -1; int nret = -1; Logger.Log(String.Format("NetFileEnum(handle_b={0:x}, sHostname={1}) called", pHandle.ToInt32(), sHostname), Logger.FileShareManagerLogLevel); if (Configurations.currentPlatform == LikewiseTargetPlatform.Windows) { nret = NetFileEnum(sHostname, null, null, 3, ref pBuf, maxlen, ref cRead, ref cTotal, ref hResume); } else { nret = NetFileEnum(pHandle, sHostname, null, null, 3, ref pBuf, maxlen, ref cRead, ref cTotal, ref hResume); } Logger.Log(String.Format( "NetFileEnum(); result={0}, pBuf={1}, cRead={2}, cTotal={3}, hResume={4}", nret, pBuf, cRead, cTotal, hResume)); // now, iterate through the data in pBuf IntPtr pCur = pBuf; for (int i = 0; i < cRead; i++) { // marshal the entry into FILE_INFO_3 fi3 = (FILE_INFO_3)Marshal.PtrToStructure(pCur, typeof(FILE_INFO_3)); // create a row // set the mode int iMode = fi3.fi3_permission; string sMode = ""; if ((iMode & PERM_FILE_READ) == PERM_FILE_READ) { sMode = "Read"; } if ((iMode & PERM_FILE_WRITE) == PERM_FILE_WRITE) { if (sMode.Length > 0) { sMode += "+Write"; } else { sMode = "Write"; } } if ((iMode & PERM_FILE_CREATE) == PERM_FILE_CREATE) { if (sMode.Length > 0) { sMode += "+Create"; } else { sMode = "Create"; } } string[] sFileInfo = { fi3.fi3_pathname, fi3.fi3_username, fi3.fi3_num_locks.ToString(), sMode, fi3.fi3_id.ToString() }; UserList.Add(i, sFileInfo); // advance to the next entry pCur = (IntPtr)((int)pCur + Marshal.SizeOf(fi3)); } if (pBuf != IntPtr.Zero) { // free the data if (Configurations.currentPlatform == LikewiseTargetPlatform.Windows) { NetApiBufferFree(pBuf); } else { SrvSvcFreeMemory(pBuf); } } // all done return(UserList); } catch (Win32Exception we) { throw new LikewiseAPIException(we); } }
/// <summary> /// Deletes a printer from the designated server. Must have administrator access to it. /// </summary> /// <param name="sServer"></param> /// <param name="sPrinter"></param> public static void DeletePrinter(CredentialEntry ce, string sServer, string sPrinter) { Session.EnsureNullSession(sServer, ce); // first, open the printer string s = Canon(sServer) + @"\" + sPrinter; int hPrinter; PRINTER_DEFAULTS pd = new PRINTER_DEFAULTS(); pd.pDatatype = IntPtr.Zero; pd.pDevMode = IntPtr.Zero; pd.DesiredAccess = PRINTER_ALL_ACCESS; IntPtr p = Marshal.AllocHGlobal(Marshal.SizeOf(pd)); Marshal.StructureToPtr(pd, p, false); if (!OpenPrinter(s, out hPrinter, p)) { // free memory Marshal.DestroyStructure(p, typeof(PRINTER_DEFAULTS)); Marshal.FreeHGlobal(p); int nError = Marshal.GetLastWin32Error(); Win32Exception we = new Win32Exception(nError); if (nError == nErrorAccessDenied) { throw new AuthException(we); } else { throw we; } } Marshal.DestroyStructure(p, typeof(PRINTER_DEFAULTS)); Marshal.FreeHGlobal(p); // now remove it from AD PRINTER_INFO_7 pi7 = new PRINTER_INFO_7(); pi7.pszObjectGUID = IntPtr.Zero; pi7.dwAction = (uint)DSPRINT_OPTIONS.DSPRINT_UNPUBLISH; p = Marshal.AllocHGlobal(Marshal.SizeOf(pi7)); Marshal.StructureToPtr(pi7, p, false); SetPrinter(hPrinter, 7, p, 0); // NOTE: we swallow any error since the operation may be left in a "pending" state rather // than returning success or failure right away. // free memory Marshal.DestroyStructure(p, typeof(PRINTER_INFO_7)); Marshal.FreeHGlobal(p); // finally, remove the printer if (!DeletePrinter(hPrinter)) { int nError = Marshal.GetLastWin32Error(); Win32Exception we = new Win32Exception(nError); if (nError == nErrorAccessDenied) { throw new AuthException(we); } else { throw we; } } }
public static Dictionary <int, string[]> EnumShares(IntPtr handle_b, CredentialEntry ce, string sHostname) { //// establish null session //if(!Session.EnsureNullSession(sHostname, ce)) //{ // return null; //} try { Dictionary <int, string[]> ShareList = new Dictionary <int, string[]>(); IntPtr pBuf = IntPtr.Zero; int cRead = 0; int cTotal = 0; int hResume = 0; int maxlen = -1; int nret = -1; if (Configurations.currentPlatform == LikewiseTargetPlatform.Windows) { Logger.Log(String.Format("NetShareEnum(sHostname={0}) called", sHostname), Logger.FileShareManagerLogLevel); nret = NetShareEnum(sHostname, 2, ref pBuf, maxlen, ref cRead, ref cTotal, ref hResume); } else { Logger.Log(String.Format("NetShareEnum(handle_b={0:x},sHostname={1}) called", handle_b.ToInt32(), sHostname), Logger.FileShareManagerLogLevel); maxlen = 20; nret = NetShareEnum(handle_b, sHostname, 2, ref pBuf, maxlen, ref cRead, ref cTotal, ref hResume); } Logger.Log(String.Format( "NetShareEnum(); result={0}, pBuf={1}, cRead={2}, cTotal={3}, hResume={4}", nret, pBuf, cRead, cTotal, hResume)); // now, iterate through the data in pBuf IntPtr pCur = pBuf; for (int i = 0; i < cRead; i++) { // marshal the entry into SHARE_INFO_2 si2 = (SHARE_INFO_2)Marshal.PtrToStructure(pCur, typeof(SHARE_INFO_2)); // only allow regular diskshares // TODO: Review this. Should we not display admin shares? if (si2.shi2_type == STYPE_DISKTREE) { string[] sShareInfo = { si2.shi2_netname, si2.shi2_path, si2.shi2_remark, si2.shi2_current_uses.ToString() }; ShareList.Add(i, sShareInfo); } // advance to the next entry pCur = (IntPtr)((int)pCur + Marshal.SizeOf(si2)); } if (pBuf != IntPtr.Zero) { // free the data if (Configurations.currentPlatform == LikewiseTargetPlatform.Windows) { NetApiBufferFree(pBuf); } else { SrvSvcFreeMemory(pBuf); } } // all done return(ShareList); } catch (Win32Exception we) { throw new LikewiseAPIException(we); } }
/// <summary> /// Fetches data about the printers available on the designated server /// </summary> /// <param name="sServer"></param> /// <param name="dt">The DataTable into which the data should be placed. dt MUST have been created properly with CreateDataTable</param> public static Dictionary <int, string[]> FetchPrinterData(CredentialEntry ce, string sServer) { int dwFlags = (int)PEFlags.PRINTER_ENUM_NAME; string Name = Canon(sServer); int Level = 2; IntPtr pPrinterEnum = IntPtr.Zero; int cBufNeeded = 0; int cReturned = 0; if (!Session.EnsureNullSession(sServer, ce)) { return(null); } Dictionary <int, string[]> printInfoList = new Dictionary <int, string[]>(); /// /// TODO: revise. Samba 3.0.13 fails if we pass in a null buffer. To avoid this /// pass in a pointer to a zero length buffer! /// #if SAMBAFIXED if (EnumPrinters(dwFlags, Name, Level, pPrinterEnum, 0, ref cBufNeeded, ref cReturned)) { // Can only happen if no printers available! return; } #else { } pPrinterEnum = Marshal.AllocHGlobal(0); if (EnumPrinters(dwFlags, Name, Level, pPrinterEnum, 0, ref cBufNeeded, ref cReturned)) { // free the mem Marshal.FreeHGlobal(pPrinterEnum); // Can only happen if no printers available! return(null); } // free the mem Marshal.FreeHGlobal(pPrinterEnum); #endif // Normally, we fail because we need a bigger buffer. However, if we fail // with RPC_S_CALL_FAILED, this means that we "succeeded" but that the remote // machine has nothing to tell us int nError = Marshal.GetLastWin32Error(); if (nError == nErrorRPC_S_CALL_FAILED) { return(null); } if (nError != ERROR_INSUFFICIENT_BUFFER) { // Any other error results in an exception string sErr; if (nError == nErrorBadRPC) { sErr = sBadServerName; } else if (nError == nErrorAccessDenied) { throw new AuthException(new Win32Exception(nError)); } else { sErr = string.Format(sErrorMsg, sServer, nError); } throw new Win32Exception(sErr); } // allocate a big enough buffer pPrinterEnum = Marshal.AllocHGlobal(cBufNeeded); // retry if (!EnumPrinters(dwFlags, Name, Level, pPrinterEnum, cBufNeeded, ref cBufNeeded, ref cReturned)) { nError = Marshal.GetLastWin32Error(); Win32Exception we = new Win32Exception(nError); if (nError == nErrorAccessDenied) { throw new AuthException(we); } else { throw we; } } // iterate through the retrieved entries, copying printer entries // to the output table IntPtr pCur = pPrinterEnum; for (int i = 0; i < cReturned; i++) { // marshal the entry into PRINTER_INFO_2 pi2 = new PRINTER_INFO_2(); Marshal.PtrToStructure(pCur, pi2); // trim off server name string sName = Marshal.PtrToStringUni(pi2.pPrinterName); if (sName.StartsWith(@"\\")) { // yep, trim off prefix int ibackwhack = sName.LastIndexOf(@"\"); sName = sName.Substring(ibackwhack + 1); } string[] printInfo = { Marshal.PtrToStringUni(pi2.pShareName), Marshal.PtrToStringUni(pi2.pDriverName), pi2.cJobs.ToString(), Marshal.PtrToStringUni(pi2.pComment) }; // advance to the next entry pCur = (IntPtr)((int)pCur + Marshal.SizeOf(pi2)); printInfoList.Add(i, printInfo); } // free the mem Marshal.FreeHGlobal(pPrinterEnum); return(printInfoList); }