void CLocalListView::DisplayShares(wxString computer) { // Cast through a union to avoid warning about breaking strict aliasing rule union { SHARE_INFO_1* pShareInfo; LPBYTE pShareInfoBlob; } si; DWORD read, total; DWORD resume_handle = 0; if (computer.Last() == '\\') computer.RemoveLast(); int j = m_fileData.size(); int share_count = 0; int res = 0; do { const wxWX2WCbuf buf = computer.wc_str(wxConvLocal); res = NetShareEnum((wchar_t*)(const wchar_t*)buf, 1, &si.pShareInfoBlob, MAX_PREFERRED_LENGTH, &read, &total, &resume_handle); if (res != ERROR_SUCCESS && res != ERROR_MORE_DATA) break; SHARE_INFO_1* p = si.pShareInfo; for (unsigned int i = 0; i < read; i++, p++) { if (p->shi1_type != STYPE_DISKTREE) continue; CLocalFileData data; data.flags = normal; data.name = p->shi1_netname; #ifdef __WXMSW__ data.label = data.name; #endif data.dir = true; data.icon = -2; data.size = -1; m_fileData.push_back(data); m_indexMapping.push_back(j++); share_count++; } NetApiBufferFree(si.pShareInfo); } while (res == ERROR_MORE_DATA); if (m_pFilelistStatusBar) m_pFilelistStatusBar->SetDirectoryContents(0, share_count, 0, false, 0); }
bool CScannerNetBios::GetShares(list<shareinfo*> *lpShares) { DWORD dwEntriesRead=0, dwTotalEntries=0; m_NetApiStatus=NetShareEnum(m_wszServer, 1, (LPBYTE*)&m_ShareInfo, MAX_PREFERRED_LENGTH, &dwEntriesRead, &dwTotalEntries, NULL); if(m_NetApiStatus!=NERR_Success) return false; SHARE_INFO_1* l_ShareInfo=m_ShareInfo; for(int x=0; x<(int)dwTotalEntries; x++) { shareinfo *pShare=new shareinfo; WideCharToMultiByte(CP_ACP, 0, (const wchar_t*)l_ShareInfo->shi1_netname, -1, pShare->sName.GetBuffer(256), 256, NULL, NULL); WideCharToMultiByte(CP_ACP, 0, (const wchar_t*)l_ShareInfo->shi1_remark, -1, pShare->sRemark.GetBuffer(256), 256, NULL, NULL); if(stricmp(pShare->sName.CStr(), "ipc$")) lpShares->push_back(pShare); l_ShareInfo++; } if(m_ShareInfo!=0) NetApiBufferFree(m_ShareInfo); return true; }
void Util::EnumShares() { PSHARE_INFO_0 shareInfo; DWORD entriesRead; DWORD total; DWORD i; NetShareEnum(Config::machine, 0, (LPBYTE *)&shareInfo, MAX_PREFERRED_LENGTH, &entriesRead, &total, NULL); for(i = 0; i < entriesRead; i++) { wprintf(L"Share: %s\n", shareInfo[i].shi0_netname); } }
static void complete_with_shared(const char *server, const char *file) { NET_API_STATUS res; size_t len = strlen(file); do { PSHARE_INFO_502 buf_ptr; DWORD er = 0, tr = 0, resume = 0; wchar_t *wserver = to_wide(server + 2); if(wserver == NULL) { show_error_msg("Memory Error", "Unable to allocate enough memory"); return; } res = NetShareEnum(wserver, 502, (LPBYTE *)&buf_ptr, -1, &er, &tr, &resume); free(wserver); if(res == ERROR_SUCCESS || res == ERROR_MORE_DATA) { PSHARE_INFO_502 p; DWORD i; p = buf_ptr; for(i = 1; i <= er; i++) { char buf[512]; WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)p->shi502_netname, -1, buf, sizeof(buf), NULL, NULL); strcat(buf, "/"); if(strnoscmp(buf, file, len) == 0) { char *const escaped = escape_filename(buf, 1); vle_compl_add_match(escaped); free(escaped); } p++; } NetApiBufferFree(buf_ptr); } } while(res == ERROR_MORE_DATA); }
void CLocalListView::DisplayShares(wxString computer) { SHARE_INFO_1* pShareInfo = 0; DWORD read, total; DWORD resume_handle = 0; if (computer.Last() == '\\') computer.RemoveLast(); int j = m_fileData.size(); int res = 0; do { const wxWX2WCbuf buf = computer.wc_str(wxConvLocal); res = NetShareEnum((wchar_t*)(const wchar_t*)buf, 1, (LPBYTE*)&pShareInfo, MAX_PREFERRED_LENGTH, &read, &total, &resume_handle); if (res != ERROR_SUCCESS && res != ERROR_MORE_DATA) break; SHARE_INFO_1* p = pShareInfo; for (unsigned int i = 0; i < read; i++, p++) { if (p->shi1_type != STYPE_DISKTREE) continue; t_fileData data; data.name = p->shi1_netname; data.dir = true; data.icon = -2; data.size = -1; data.hasTime = false; m_fileData.push_back(data); m_indexMapping.push_back(j++); } NetApiBufferFree(pShareInfo); } while (res == ERROR_MORE_DATA); }
bool ShareFinder::EnumerateShares(char* szServer) { PSHARE_INFO_502 BufPtr, p; NET_API_STATUS res; DWORD er=0, tr=0, resume=0, i; wchar_t server[MAX_PATH]; char szServerWithSlashes[MAX_PATH]; ::ZeroMemory(server, MAX_PATH); ::ZeroMemory(szServerWithSlashes, MAX_PATH); _snprintf_s(szServerWithSlashes, MAX_PATH, 2+strlen(szServer), "\\\\%s", szServer); size_t requiredSize = 0; mbstowcs_s(&requiredSize, server, MAX_PATH, szServerWithSlashes, strlen(szServerWithSlashes)); do { // F**k Microsoft and it's lame-ass unicode crap res = NetShareEnum((LPWSTR)server, 502, (LPBYTE*)&BufPtr, -1, &er, &tr, &resume); if(res == ERROR_SUCCESS || res == ERROR_MORE_DATA) { p = BufPtr; for(i=1; i <= er; i++) { Log.CachedReportError(m_nCacheID, INFO, "Found share %S, whose physical path is %S\n", p->shi502_netname, p->shi502_path); p++; } NetApiBufferFree(BufPtr); } else Log.CachedReportError(m_nCacheID, CRITICAL, "EnumerateShares returned an error of %ld\n",res); } while (res == ERROR_MORE_DATA); // end do return true; }
static DWORD Enum( int argc, char** ppszArgv ) { static const DWORD dwLevel = 0; static const DWORD dwMaxLen = 128; DWORD dwError = 0; PSHARE_INFO_0 pShareInfo = NULL; DWORD dwNumEntries = 0; DWORD dwTotalEntries = 0; DWORD dwSeenEntries = 0; DWORD dwResume = 0; DWORD dwIndex = 0; PSTR pszShareName = NULL; do { dwError = NetShareEnum( gState.pwszServerName, dwLevel, OUT_PPVOID(&pShareInfo), dwMaxLen, &dwNumEntries, &dwTotalEntries, &dwResume); BAIL_ON_SRVSVC_ERROR(dwError); dwSeenEntries += dwNumEntries; for (dwIndex = 0; dwIndex < dwNumEntries; dwIndex++) { dwError = LwWc16sToMbs(pShareInfo[dwIndex].shi0_netname, &pszShareName); BAIL_ON_SRVSVC_ERROR(dwError); printf("%s\n", pszShareName); LW_SAFE_FREE_STRING(pszShareName); } if (pShareInfo) { SrvSvcFreeMemory(pShareInfo); pShareInfo = NULL; } } while (dwSeenEntries < dwTotalEntries); cleanup: LW_SAFE_FREE_STRING(pszShareName); if (pShareInfo) { SrvSvcFreeMemory(pShareInfo); pShareInfo = NULL; } return dwError; error: goto cleanup; }
bool CWfpNET::NetBIOSShares_get(void) { DWORD i = 0, entriesread = 0, resume_handle = 0, totalentries = 0; PSHARE_INFO_1 pBuf = NULL, pTmpBuf = NULL; NET_API_STATUS nStatus = NULL; NETRESOURCE nr; CString tmp, tmp2; bool accessible = false; // The NetShareEnum function retrieves information about each shared // resource on a server. // No special group membership is required for level 0 or level 1 calls. do{ nStatus = NetShareEnum(node.szComputerW, 1, (LPBYTE *) &pBuf, 0xFFFFFFFF, &entriesread, &totalentries, &resume_handle); if(nStatus == ERROR_SUCCESS || nStatus == ERROR_MORE_DATA) { if((pTmpBuf = pBuf) != NULL) { for(i = 0; i < entriesread; i++) { if(node.NetBIOS.IsEmpty()) { tmp.Format(_T("\\\\%s\\%S"), node.ipaddress, pTmpBuf->shi1_netname); } else { tmp.Format(_T("\\\\%s\\%S"), node.NetBIOS, pTmpBuf->shi1_netname); } if((pTmpBuf->shi1_type == STYPE_DISKTREE) && (options.optionopensharetest)) { accessible = false; //WNetCancelConnection2(_T("X:") ,CONNECT_UPDATE_PROFILE, TRUE); nr.dwType = RESOURCETYPE_ANY; //nr.lpLocalName = _T("X:"); nr.lpLocalName = NULL; nr.lpRemoteName = tmp.GetBuffer(); nr.lpProvider = NULL; if(WNetAddConnection2(&nr, NULL, NULL, FALSE) == NO_ERROR) { accessible = true; //WNetCancelConnection2(_T("X:") ,CONNECT_UPDATE_PROFILE, TRUE); WNetCancelConnection2(tmp.GetBuffer() ,CONNECT_UPDATE_PROFILE, TRUE); } } tmp2.Format("%s %S %s", tmp, pTmpBuf->shi1_remark, (accessible) ? "accessible with current credentials" : ""); NetBIOSShares.Add(tmp2); pTmpBuf++; } } if(pBuf != NULL) { NetApiBufferFree(pBuf); pBuf = NULL; } } else { // Silence Errors // NetErrorHandler("NetShareEnum", nStatus); return false; } }while (nStatus==ERROR_MORE_DATA); return true; }
int main(int argc, const char **argv) { NET_API_STATUS status; struct libnetapi_ctx *ctx = NULL; const char *hostname = NULL; uint32_t level = 0; uint8_t *buffer = NULL; uint32_t entries_read = 0; uint32_t total_entries = 0; uint32_t resume_handle = 0; int i; struct SHARE_INFO_0 *i0 = NULL; struct SHARE_INFO_1 *i1 = NULL; struct SHARE_INFO_2 *i2 = NULL; poptContext pc; int opt; struct poptOption long_options[] = { POPT_AUTOHELP POPT_COMMON_LIBNETAPI_EXAMPLES POPT_TABLEEND }; status = libnetapi_init(&ctx); if (status != 0) { return status; } pc = poptGetContext("share_enum", argc, argv, long_options, 0); poptSetOtherOptionHelp(pc, "hostname level"); while((opt = poptGetNextOpt(pc)) != -1) { } if (!poptPeekArg(pc)) { poptPrintHelp(pc, stderr, 0); goto out; } hostname = poptGetArg(pc); if (poptPeekArg(pc)) { level = atoi(poptGetArg(pc)); } /* NetShareEnum */ do { status = NetShareEnum(hostname, level, &buffer, (uint32_t)-1, &entries_read, &total_entries, &resume_handle); if (status == 0 || status == ERROR_MORE_DATA) { printf("total entries: %d\n", total_entries); switch (level) { case 0: i0 = (struct SHARE_INFO_0 *)buffer; break; case 1: i1 = (struct SHARE_INFO_1 *)buffer; break; case 2: i2 = (struct SHARE_INFO_2 *)buffer; break; default: break; } for (i=0; i<entries_read; i++) { switch (level) { case 0: printf("#%d netname: %s\n", i, i0->shi0_netname); i0++; break; case 1: printf("#%d netname: %s\n", i, i1->shi1_netname); printf("#%d type: %d\n", i, i1->shi1_type); printf("#%d remark: %s\n", i, i1->shi1_remark); i1++; break; case 2: printf("#%d netname: %s\n", i, i2->shi2_netname); printf("#%d type: %d\n", i, i2->shi2_type); printf("#%d remark: %s\n", i, i2->shi2_remark); printf("#%d permissions: %d\n", i, i2->shi2_permissions); printf("#%d max users: %d\n", i, i2->shi2_max_uses); printf("#%d current users: %d\n", i, i2->shi2_current_uses); printf("#%d path: %s\n", i, i2->shi2_path); printf("#%d password: %s\n", i, i2->shi2_passwd); i2++; break; default: break; } } NetApiBufferFree(buffer); } } while (status == ERROR_MORE_DATA); if (status != 0) { printf("NetShareEnum failed with: %s\n", libnetapi_get_error_string(ctx, status)); } out: libnetapi_free(ctx); poptFreeContext(pc); return status; }
int smb_netshareenum(SMBHANDLE inConnection, CFDictionaryRef *outDict, int DiskAndPrintSharesOnly) { int error = 0; NTSTATUS status; SMBServerPropertiesV1 properties; CFMutableDictionaryRef shareDict = NULL; uint32_t ii; CFStringRef shareName, comments; u_int16_t shareType; struct statfs *fs = NULL; int fs_cnt = 0; fs = smb_getfsstat(&fs_cnt); shareDict = CFDictionaryCreateMutable(kCFAllocatorDefault, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks); if (shareDict == NULL) { error = ENOMEM; goto done; } status = SMBGetServerProperties(inConnection, &properties, kPropertiesVersion, sizeof(properties)); if (!NT_SUCCESS(status)) { /* Should never happen */ error = errno; goto done; } /* Only use RPC if the server supports DCE/RPC and UNICODE */ if (properties.capabilities & SMB_CAP_RPC_REMOTE_APIS) { PSHARE_ENUM_STRUCT InfoStruct = NULL; NET_API_STATUS api_status; /* Try getting a list of shares with the SRVSVC RPC service. */ api_status = NetShareEnum(properties.serverName, 1, &InfoStruct); if (api_status == 0) { for (ii = 0; ii < InfoStruct->ShareInfo.Level1->EntriesRead; ii++) { shareType = OSSwapLittleToHostInt16(InfoStruct->ShareInfo.Level1->Buffer[ii].shi1_type); /* They only want the disk and printer shares */ if (DiskAndPrintSharesOnly && (shareType != SMB_ST_DISK) && (shareType != SMB_ST_PRINTER)) continue; shareName = convertToStringRef(InfoStruct->ShareInfo.Level1->Buffer[ii].shi1_netname, 1024, TRUE); if (shareName == NULL) { continue; } if (InfoStruct->ShareInfo.Level1->Buffer[ii].shi1_remark) { comments = convertToStringRef(InfoStruct->ShareInfo.Level1->Buffer[ii].shi1_remark, 1024, TRUE); } else { comments = NULL; } addShareToDictionary(inConnection, shareDict, shareName, comments, shareType, fs, fs_cnt); CFRelease(shareName); if (comments) { CFRelease(comments); } } NetApiBufferFree(InfoStruct); goto done; } SMBLogInfo("Looking up shares with RPC failed api_status = %d", ASL_LEVEL_DEBUG, api_status); } /* * OK, that didn't work - either they don't support RPC or we * got an error in either case try RAP if enabled (lanman_on pref is set). */ if (properties.internalFlags & kLanmanOn) { void *rBuffer = NULL; unsigned char *endBuffer; uint32_t rBufferSize = 0; struct smb_share_info_1 *shareInfo1; uint32_t entriesRead = 0; SMBLogInfo("Looking up shares RAP", ASL_LEVEL_DEBUG); /* Try getting a list of shares with the RAP protocol. */ error = RapNetShareEnum(inConnection, 1, &rBuffer, &rBufferSize, &entriesRead, NULL); if (error) { SMBLogInfo("Looking up shares with RAP failed, error=%d", ASL_LEVEL_DEBUG, error); goto done; } endBuffer = (unsigned char *)rBuffer + rBufferSize; for (shareInfo1 = (struct smb_share_info_1 *)rBuffer, ii = 0; (ii < entriesRead) && (((unsigned char *)shareInfo1 + sizeof(smb_share_info_1)) <= endBuffer); ii++, shareInfo1++) { shareInfo1->shi1_pad = 0; /* Just to be safe */ /* Note we need to swap this item */ shareType = OSSwapLittleToHostInt16(shareInfo1->shi1_type); shareName = convertToStringRef(shareInfo1->shi1_netname, sizeof(shareInfo1->shi1_netname), FALSE); if (shareName == NULL) { continue; } /* Assume we have no comments for this entry */ comments = NULL; /* * The shi1_remark gets swapped in the rap processing, someday we just * take another look at this an make it work the same for all values. */ if ((shareInfo1->shi1_remark != 0) && (shareInfo1->shi1_remark < rBufferSize)) { unsigned char *remarks = (unsigned char *)rBuffer + shareInfo1->shi1_remark; /* * Make sure the comments don't start pass the end of the buffer * and we have a comment. */ if ((remarks < endBuffer) && *remarks) { size_t maxlen = endBuffer - remarks; /* Now make sure the comment is a null terminate string */ maxlen = smb_strnlen((const char *)remarks, maxlen); remarks[maxlen] = 0; comments = convertToStringRef(remarks, maxlen, FALSE); } } addShareToDictionary(inConnection, shareDict, shareName, comments, shareType, fs, fs_cnt); CFRelease(shareName); if (comments) { CFRelease(comments); } } RapNetApiBufferFree(rBuffer); } done: if (fs) { free(fs); } if (error) { *outDict = NULL; if (shareDict) { CFRelease(shareDict); } } else { *outDict = shareDict; } return error; }
// // MIB_dlog_lmget // Retrieve dlogion table information from Lan Manager. // If not cached, sort it and then // cache it. // // Notes: // // Return Codes: // SNMPAPI_NOERROR // SNMPAPI_ERROR // // Error Codes: // None. // SNMPAPI MIB_dlogons_lmget( ) { SNMPAPI nResult = SNMPAPI_NOERROR; #if 0 DWORD entriesread; DWORD totalentries; LPBYTE bufptr; unsigned lmCode; unsigned i; SHARE_INFO_2 *DataTable; DOM_LOGON_ENTRY *MIB_DomLogonTableElement ; int First_of_this_block; DWORD resumehandle=0; // // // If cached, return piece of info. // // // // // Do network call to gather information and put it in a nice array // // // free the old table LOOK OUT!! // init the length MIB_DomLogonTable.Len = 0; First_of_this_block = 0; do { // as long as there is more data to process lmCode = NetShareEnum(NULL, // local server 2, // level 2, &bufptr, // data structure to return MAX_PREFERRED_LENGTH, &entriesread, &totalentries, &resumehandle // resume handle ); // // Filter out all the Admin shares (name ending with $). // AdminFilter(2,&entriesread,bufptr); DataTable = (SHARE_INFO_2 *) bufptr ; if((NERR_Success == lmCode) || (ERROR_MORE_DATA == lmCode)) { // valid so process it, otherwise error if(0 == MIB_DomLogonTable.Len) { // 1st time, alloc the whole table // alloc the table space MIB_DomLogonTable.Table = SnmpUtilMemAlloc(totalentries * sizeof(DOM_LOGON_ENTRY) ); } MIB_DomLogonTableElement = MIB_DomLogonTable.Table + First_of_this_block ; for(i=0; i<entriesread; i++) { // once for each entry in the buffer // increment the entry number MIB_DomLogonTable.Len ++; // Stuff the data into each item in the table // dloge name MIB_DomLogonTableElement->svShareName.stream = SnmpUtilMemAlloc ( strlen( DataTable->shi2_netname ) ) ; MIB_DomLogonTableElement->svShareName.length = strlen( DataTable->shi2_netname ) ; MIB_DomLogonTableElement->svShareName.dynamic = TRUE; memcpy( MIB_DomLogonTableElement->svShareName.stream, DataTable->shi2_netname, strlen( DataTable->shi2_netname ) ) ; // Share Path MIB_DomLogonTableElement->svSharePath.stream = SnmpUtilMemAlloc ( strlen( DataTable->shi2_path ) ) ; MIB_DomLogonTableElement->svSharePath.length = strlen( DataTable->shi2_path ) ; MIB_DomLogonTableElement->svSharePath.dynamic = TRUE; memcpy( MIB_DomLogonTableElement->svSharePath.stream, DataTable->shi2_path, strlen( DataTable->shi2_path ) ) ; // Share Comment/Remark MIB_DomLogonTableElement->svShareComment.stream = SnmpUtilMemAlloc ( strlen( DataTable->shi2_remark ) ) ; MIB_DomLogonTableElement->svShareComment.length = strlen( DataTable->shi2_remark ) ; MIB_DomLogonTableElement->svShareComment.dynamic = TRUE; memcpy( MIB_DomLogonTableElement->svShareComment.stream, DataTable->shi2_remark, strlen( DataTable->shi2_remark ) ) ; DataTable ++ ; // advance pointer to next dlog entry in buffer MIB_DomLogonTableElement ++ ; // and table entry } // for each entry in the data table // indicate where to start adding on next pass, if any First_of_this_block = i ; } // if data is valid to process else { // Signal error nResult = SNMPAPI_ERROR; goto Exit; } } while (ERROR_MORE_DATA == lmCode) ; // iterate over the table populating the Oid field build_dlog_entry_oids(); // Sort the table information using MSC QuickSort routine qsort( &MIB_DomLogonTable.Table[0], MIB_DomLogonTable.Len, sizeof(DOM_LOGON_ENTRY), dlog_entry_cmp ); // // // Cache table // // // // // Return piece of information requested // // Exit: #endif return nResult; } // MIB_dlog_get
/* ================ PFBYTE inBuf - The start of the incoming buffer (needed for offset calculations) PSMB_TRANSACTIONRQ pInReq - the incoming request PFBYTE outBuf - the outgoing buffer (needed for offset calculations) PSMB_TRANSACTIONRS pOutRes - The out going transaction response ================ */ int RAP_Proc (PSMB_SESSIONCTX pCtx, PRTSMB_HEADER pInHdr, PRTSMB_TRANSACTION pTransaction, PFVOID pInBuf, PRTSMB_HEADER pOutHdr, PRTSMB_TRANSACTION_R pTransactionR, rtsmb_size size_left) { RTSMB_RAP_REQUEST func; rtsmb_char param [50]; rtsmb_char answer [50]; int size, data_size = 0; word param_size; func.parent = pTransaction; func.parameter = param; func.parameter_size = 49; func.answer = answer; func.answer_size = 49; size = srv_cmd_read_rap_request (pCtx->read_origin, pInBuf, pCtx->current_body_size - (rtsmb_size)(PDIFF (pInBuf, pCtx->read_origin)), pInHdr, &func); if (size == -1) return 0; pInBuf = PADD (pInBuf, size); param_size = 0; switch (func.opcode) { case RAP_COM_NET_SHARE_ENUM: data_size = NetShareEnum (pCtx, &func, pInHdr, pInBuf, pOutHdr, size_left, ¶m_size); break; case RAP_COM_NET_SERVER_GETINFO: data_size = NetServerGetInfo (pCtx, &func, pInHdr, pInBuf, pOutHdr, size_left, ¶m_size); break; case RAP_COM_NET_WKSTA_GETINFO: data_size = NetWkstaGetInfo (pCtx, &func, pInHdr, pInBuf, pOutHdr, size_left, ¶m_size); break; case RAP_COM_NET_SHARE_GETINFO: data_size = NetShareGetInfo (pCtx, &func, pInHdr, pInBuf, pOutHdr, size_left, ¶m_size); break; case RAP_COM_WPRINTQ_GETINFO: data_size = WPrintQGetInfo (pCtx, &func, pInHdr, pInBuf, pOutHdr, size_left, ¶m_size); break; case RAP_COM_NET_SERVER_ENUM2: data_size = NetServerEnum2 (pCtx, &func, pInHdr, pInBuf, pOutHdr, size_left, ¶m_size); break; /* case RAP_COM_NET_ACCESS_GETINFO: NetAccessGetInfo (pCtx, &func, pInHdr, pInBuf, pOutHdr, size_left); break;*/ default: RTSMB_DEBUG_OUTPUT_STR ("RAP_Proc: function unhandled: ", RTSMB_DEBUG_TYPE_ASCII); RTSMB_DEBUG_OUTPUT_INT (func.opcode); RTSMB_DEBUG_OUTPUT_STR ("\n", RTSMB_DEBUG_TYPE_ASCII); } if (data_size == -2) /* special error case where we don't want to answer */ return -1; pTransactionR->setup_size = 0; pTransactionR->setup = (PFWORD)0; pTransactionR->parameter = pCtx->tmpBuffer; pTransactionR->parameter_count = param_size; if (data_size == -1) { pTransactionR->data = (PFBYTE)0; pTransactionR->data_count = 0; } else { pTransactionR->data = PADD (pCtx->tmpBuffer, param_size); pTransactionR->data_count = (word) data_size; } return 0; } // End RAP_Proc
static int DirRead(stream_t *p_access, input_item_node_t *p_node) { access_sys_t *p_sys = p_access->p_sys; int i_ret = VLC_SUCCESS; struct vlc_readdir_helper rdh; vlc_readdir_helper_init( &rdh, p_access, p_node ); // Handle share listing from here. Directory browsing is handled by the // usual filesystem module. SHARE_INFO_1 *p_info; DWORD i_share_enum_res; DWORD i_nb_elem; DWORD i_resume_handle = 0; DWORD i_total_elements; // Unused, but needs to be passed wchar_t *wpsz_host = ToWide( p_sys->url.psz_host ); if( wpsz_host == NULL ) return VLC_ENOMEM; do { i_share_enum_res = NetShareEnum( wpsz_host, 1, (LPBYTE*)&p_info, MAX_PREFERRED_LENGTH, &i_nb_elem, &i_total_elements, &i_resume_handle ); if( i_share_enum_res == ERROR_SUCCESS || i_share_enum_res == ERROR_MORE_DATA ) { for ( DWORD i = 0; i < i_nb_elem; ++i ) { SHARE_INFO_1 *p_current = p_info + i; if( p_current->shi1_type & STYPE_SPECIAL ) continue; char* psz_name = FromWide( p_current->shi1_netname ); if( psz_name == NULL ) { i_ret = VLC_ENOMEM; break; } char* psz_path; if( smb_get_uri( p_access, &psz_path, NULL, NULL, NULL, p_sys->url.psz_host, p_sys->url.psz_path, psz_name ) < 0 ) { free( psz_name ); i_ret = VLC_ENOMEM; break; } // We need to concatenate the scheme before, as the window version // of smb_get_uri generates a path (and the other call site needs // a path). The path is already prefixed by "//" so we just need // to add "file:" char* psz_uri; if( asprintf( &psz_uri, "file:%s", psz_path ) < 0 ) { free( psz_name ); free( psz_path ); i_ret = VLC_ENOMEM; break; } free( psz_path ); i_ret = vlc_readdir_helper_additem( &rdh, psz_uri, NULL, psz_name, ITEM_TYPE_DIRECTORY, ITEM_NET ); free( psz_name ); free( psz_uri ); } } NetApiBufferFree( p_info ); } while( i_share_enum_res == ERROR_MORE_DATA && i_ret == VLC_SUCCESS ); free( wpsz_host ); vlc_readdir_helper_finish( &rdh, i_ret == VLC_SUCCESS ); return i_ret; }
bool ShareFinder::BindUploadShareToLocalDrive(char* szServer, int nBufferSize, char** lplpPhysicalPath, char* pAssignedDrive) { // Returns the drive letter if successful, otherwise 0 PSHARE_INFO_502 BufPtr, p; NET_API_STATUS res; DWORD er = 0, tr = 0, resume = 0, i; wchar_t server[MAX_PATH]; char szTemp[MAX_PATH]; bool bBound = false; char szServerWithSlashes[MAX_PATH]; char lpszLocalDrive[3]; ::ZeroMemory(server, MAX_PATH); ::ZeroMemory(szServerWithSlashes, MAX_PATH); ::ZeroMemory(*lplpPhysicalPath, nBufferSize); _snprintf_s(szServerWithSlashes, MAX_PATH, 2+strlen(szServer), "\\\\%s", szServer); size_t requiredSize = 0; mbstowcs_s(&requiredSize, server, MAX_PATH, szServerWithSlashes, strlen(szServerWithSlashes)); memset(lpszLocalDrive, 0, 3); // This needs to be protected with a critical section, since multiple threads may try to get a free // drive letter at the same time! That's a bad thing WaitForSingleObject(hMutex, INFINITE); char cDriveLetter = GetUnusedDriveLetter(); memset(lpszLocalDrive, 0, 3); lpszLocalDrive[0] = cDriveLetter; lpszLocalDrive[1] = ':'; if (cDriveLetter == 0) { ReleaseMutex(hMutex); return false; } memcpy(pAssignedDrive, &cDriveLetter, 1); do { // F**k Microsoft and it's lame-ass unicode crap res = NetShareEnum((LPWSTR)server, 502, (LPBYTE*)&BufPtr, -1, &er, &tr, &resume); if(res == ERROR_SUCCESS || res == ERROR_MORE_DATA) { p = BufPtr; for(i = 1; i <= er; i++) { ::ZeroMemory(szTemp, MAX_PATH); size_t requiredSize2 = 0; wcstombs_s(&requiredSize2, szTemp, MAX_PATH, (LPWSTR)(p->shi502_netname), MAX_PATH); // Look for shares that are not SYSVOL or NETLOGON, and that have a physical path if (_stricmp(szTemp, "SYSVOL") != 0 && _stricmp(szTemp, "NETLOGON") != 0 && wcslen((LPWSTR)(p->shi502_path)) > 0) { // If this is a potentially workable share, bind the drive and try uploading something if (BindDrive(lpszLocalDrive, szServerWithSlashes, szTemp)) { // Success! // Copy the physical path to the out variable size_t requiredSize3 = 0; wcstombs_s(&requiredSize3, szTemp, MAX_PATH, (LPWSTR)(p->shi502_path), MAX_PATH); strncpy_s(*lplpPhysicalPath, MAX_PATH, szTemp, nBufferSize); bBound = true; break; } // Otherwise continue and try another share } p++; } NetApiBufferFree(BufPtr); } else Log.CachedReportError(m_nCacheID, CRITICAL, "BindUploadShareToLocalDrive returned an error of %ld\n",res); } while (res == ERROR_MORE_DATA); // end do ReleaseMutex(hMutex); return true; }
bool GetAvailableWriteableShare(char* szServer, int nPhysicalBufferSize, char** lplpPhysicalPath, int nUNCPathSize, char** lplpUNCPath) { // Returns the drive letter if successful, otherwise 0 PSHARE_INFO_2 BufPtr, p; NET_API_STATUS res; DWORD er = 0, tr = 0, resume = 0, i; wchar_t server[MAX_PATH]; char szTemp[MAX_PATH], szTemp2[MAX_PATH]; bool bFound = false; char szServerWithSlashes[MAX_PATH]; ::ZeroMemory(server, MAX_PATH); ::ZeroMemory(szServerWithSlashes, MAX_PATH); ::ZeroMemory(*lplpPhysicalPath, nPhysicalBufferSize); ::ZeroMemory(*lplpUNCPath, nUNCPathSize); //_snprintf(szServerWithSlashes, MAX_PATH, "\\\\%s", szServer); _snprintf(szServerWithSlashes, MAX_PATH, "%s", szServer); mbstowcs(server, szServerWithSlashes, strlen(szServerWithSlashes)); do { // F**k Microsoft and it's lame-ass unicode crap res = NetShareEnum(server, 2, (LPBYTE*)&BufPtr, -1, &er, &tr, &resume); if(res == ERROR_SUCCESS || res == ERROR_MORE_DATA) { p = BufPtr; for(i = 1; i <= er; i++) { ::ZeroMemory(szTemp, MAX_PATH); wcstombs(szTemp, (LPWSTR)(p->shi2_netname), MAX_PATH); // Look for shares that are not SYSVOL or NETLOGON, and that have a physical path if (stricmp(szTemp, "SYSVOL") != 0 && stricmp(szTemp, "NETLOGON") != 0 && wcslen((LPWSTR)(p->shi2_path)) > 0) { // If this is a potentially workable share, try uploading something memset(szTemp2, 0, MAX_PATH); _snprintf(szTemp2, MAX_PATH, "%s\\%s", szServerWithSlashes, szTemp); if (CanUpload(szTemp2)) { // Success! // Copy the physical path to the out variable wcstombs(szTemp, (LPWSTR)(p->shi2_path), MAX_PATH); strncpy(*lplpPhysicalPath, szTemp, nPhysicalBufferSize); // Also copy the UNC path to the out variable strncpy(*lplpUNCPath, szTemp2, nUNCPathSize); bFound = true; break; } // Otherwise continue and try another share } p++; } NetApiBufferFree(BufPtr); } else fprintf(stderr, "GetAvailableWriteableShare returned an error of %ld\n",res); } while (res == ERROR_MORE_DATA); // end do return bFound; }
bool ShareFinder::GetAvailableWriteableShare(char* szServer, int nPhysicalBufferSize, char** lplpPhysicalPath, int nUNCPathSize, char** lplpUNCPath) { // Returns the drive letter if successful, otherwise 0 PSHARE_INFO_502 BufPtr, p; NET_API_STATUS res; DWORD er = 0, tr = 0, resume = 0, i; wchar_t server[MAX_PATH]; char szTemp[MAX_PATH], szTemp2[MAX_PATH]; bool bFound = false; char szServerWithSlashes[MAX_PATH]; ::ZeroMemory(server, MAX_PATH); ::ZeroMemory(szServerWithSlashes, MAX_PATH); ::ZeroMemory(*lplpPhysicalPath, nPhysicalBufferSize); ::ZeroMemory(*lplpUNCPath, nUNCPathSize); _snprintf_s(szServerWithSlashes, MAX_PATH, 2+strlen(szServer), "\\\\%s", szServer); size_t requiredSize = 0; mbstowcs_s(&requiredSize, server, MAX_PATH, szServerWithSlashes, strlen(szServerWithSlashes)); do { // F**k Microsoft and it's lame-ass unicode crap res = NetShareEnum((LPWSTR)server, 502, (LPBYTE*)&BufPtr, -1, &er, &tr, &resume); if(res == ERROR_SUCCESS || res == ERROR_MORE_DATA) { p = BufPtr; for(i = 1; i <= er; i++) { ::ZeroMemory(szTemp, MAX_PATH); size_t requiredSize2 = 0; wcstombs_s(&requiredSize, szTemp, MAX_PATH, (LPWSTR)(p->shi502_netname), MAX_PATH); // Look for shares that are not SYSVOL or NETLOGON, and that have a physical path if (((p->shi502_type == STYPE_DISKTREE) || (p->shi502_type == STYPE_SPECIAL)) /*&& stricmp(szTemp, "QS1LPT3") != 0*/ && _stricmp(szTemp, "SYSVOL") != 0 && _stricmp(szTemp, "NETLOGON") != 0 && wcslen((LPWSTR)(p->shi502_path)) > 0) { // If this is a potentially workable share, try uploading something memset(szTemp2, 0, MAX_PATH); _snprintf_s(szTemp2, MAX_PATH, strlen(szServerWithSlashes)+1+strlen(szTemp), "%s\\%s", szServerWithSlashes, szTemp); if (CanUpload(szTemp2)) { // Success! // Copy the physical path to the out variable size_t requiredSize3 = 0; wcstombs_s(&requiredSize3, szTemp, MAX_PATH, (LPWSTR)(p->shi502_path), MAX_PATH); strncpy_s(*lplpPhysicalPath, MAX_PATH, szTemp, nPhysicalBufferSize); // Also copy the UNC path to the out variable strncpy_s(*lplpUNCPath, MAX_PATH, szTemp2, nUNCPathSize); bFound = true; break; } // Otherwise continue and try another share } p++; } NetApiBufferFree(BufPtr); } else Log.CachedReportError(m_nCacheID, CRITICAL, "BindUploadShareToLocalDrive returned an error of %ld\n",res); } while (res == ERROR_MORE_DATA); // end do return bFound; }
static NET_API_STATUS test_netshareenum(const char *hostname, uint32_t level, const char *sharename) { NET_API_STATUS status; uint32_t entries_read = 0; uint32_t total_entries = 0; uint32_t resume_handle = 0; int found_share = 0; const char *current_name = NULL; uint8_t *buffer = NULL; int i; struct SHARE_INFO_0 *i0 = NULL; struct SHARE_INFO_1 *i1 = NULL; struct SHARE_INFO_2 *i2 = NULL; printf("testing NetShareEnum level %d\n", level); do { status = NetShareEnum(hostname, level, &buffer, (uint32_t)-1, &entries_read, &total_entries, &resume_handle); if (status == 0 || status == ERROR_MORE_DATA) { switch (level) { case 0: i0 = (struct SHARE_INFO_0 *)buffer; break; case 1: i1 = (struct SHARE_INFO_1 *)buffer; break; case 2: i2 = (struct SHARE_INFO_2 *)buffer; break; default: return -1; } for (i=0; i<entries_read; i++) { switch (level) { case 0: current_name = i0->shi0_netname; break; case 1: current_name = i1->shi1_netname; break; case 2: current_name = i2->shi2_netname; break; default: break; } if (strcasecmp(current_name, sharename) == 0) { found_share = 1; } switch (level) { case 0: i0++; break; case 1: i1++; break; case 2: i2++; break; } } NetApiBufferFree(buffer); } } while (status == ERROR_MORE_DATA); if (status) { return status; } if (!found_share) { printf("failed to get share\n"); return -1; } return 0; }
/***************************************************************************** * DirRead: *****************************************************************************/ static int DirRead (stream_t *p_access, input_item_node_t *p_node ) { access_sys_t *p_sys = p_access->p_sys; int i_ret = VLC_SUCCESS; struct vlc_readdir_helper rdh; vlc_readdir_helper_init( &rdh, p_access, p_node ); #ifndef _WIN32 struct smbc_dirent *p_entry; while( i_ret == VLC_SUCCESS && ( p_entry = smbc_readdir( p_sys->i_smb ) ) ) { char *psz_uri; const char *psz_server = p_sys->url.psz_host; const char *psz_path = p_sys->url.psz_path; const char *psz_name = p_entry->name; int i_type; switch( p_entry->smbc_type ) { case SMBC_SERVER: case SMBC_WORKGROUP: psz_server = p_sys->url.psz_host; psz_path = NULL; psz_name = NULL; case SMBC_FILE_SHARE: case SMBC_DIR: i_type = ITEM_TYPE_DIRECTORY; break; case SMBC_FILE: i_type = ITEM_TYPE_FILE; break; default: case SMBC_PRINTER_SHARE: case SMBC_COMMS_SHARE: case SMBC_IPC_SHARE: case SMBC_LINK: continue; } char *psz_encoded_name = NULL; if( psz_name != NULL && ( psz_encoded_name = vlc_uri_encode( psz_name ) ) == NULL ) { i_ret = VLC_ENOMEM; break; } if( smb_get_uri( p_access, &psz_uri, NULL, NULL, NULL, psz_server, psz_path, psz_encoded_name ) < 0 ) { free(psz_encoded_name); i_ret = VLC_ENOMEM; break; } free(psz_encoded_name); i_ret = vlc_readdir_helper_additem( &rdh, psz_uri, NULL, p_entry->name, i_type, ITEM_NET ); free( psz_uri ); } #else // Handle share listing from here. Directory browsing is handled by the // usual filesystem module. SHARE_INFO_1 *p_info; DWORD i_share_enum_res; DWORD i_nb_elem; DWORD i_resume_handle = 0; DWORD i_total_elements; // Unused, but needs to be passed wchar_t *wpsz_host = ToWide( p_sys->url.psz_host ); if( wpsz_host == NULL ) return VLC_ENOMEM; do { i_share_enum_res = NetShareEnum( wpsz_host, 1, (LPBYTE*)&p_info, MAX_PREFERRED_LENGTH, &i_nb_elem, &i_total_elements, &i_resume_handle ); if( i_share_enum_res == ERROR_SUCCESS || i_share_enum_res == ERROR_MORE_DATA ) { for ( DWORD i = 0; i < i_nb_elem; ++i ) { SHARE_INFO_1 *p_current = p_info + i; if( p_current->shi1_type & STYPE_SPECIAL ) continue; char* psz_name = FromWide( p_current->shi1_netname ); if( psz_name == NULL ) { i_ret = VLC_ENOMEM; break; } char* psz_path; if( smb_get_uri( p_access, &psz_path, NULL, NULL, NULL, p_sys->url.psz_host, p_sys->url.psz_path, psz_name ) < 0 ) { free( psz_name ); i_ret = VLC_ENOMEM; break; } free( psz_name ); // We need to concatenate the scheme before, as the window version // of smb_get_uri generates a path (and the other call site needs // a path). The path is already prefixed by "//" so we just need // to add "file:" char* psz_uri; if( asprintf( &psz_uri, "file:%s", psz_path ) < 0 ) { free( psz_path ); i_ret = VLC_ENOMEM; break; } free( psz_path ); i_ret = vlc_readdir_helper_additem( &rdh, psz_uri, NULL, psz_name, ITEM_TYPE_DIRECTORY, ITEM_NET ); free( psz_uri ); } } NetApiBufferFree( p_info ); } while( i_share_enum_res == ERROR_MORE_DATA && i_ret == VLC_SUCCESS ); free( wpsz_host ); #endif vlc_readdir_helper_finish( &rdh, i_ret == VLC_SUCCESS ); return i_ret; }