NET_API_STATUS NetShareEnum( IN PCWSTR pwszServername, IN DWORD dwLevel, OUT PBYTE *ppBuffer, IN DWORD dwMaxLen, OUT PDWORD pdwNumEntries, OUT PDWORD pdwTotalEntries, OUT PDWORD pdwResume ) { NET_API_STATUS err = ERROR_SUCCESS; PSRVSVC_CONTEXT pContext = NULL; BAIL_ON_INVALID_PTR(ppBuffer, err); BAIL_ON_INVALID_PTR(pdwNumEntries, err); BAIL_ON_INVALID_PTR(pdwTotalEntries, err); err = SrvSvcCreateContext(pwszServername, &pContext); BAIL_ON_WIN_ERROR(err); err = NetrShareEnum( pContext, pwszServername, dwLevel, ppBuffer, dwMaxLen, pdwNumEntries, pdwTotalEntries, pdwResume); BAIL_ON_WIN_ERROR(err); cleanup: if (pContext) { SrvSvcCloseContext(pContext); } return err; error: goto cleanup; }
static int rpc_netshareenum(struct smb_ctx *ctx, int *entriesp, int *totalp, struct share_info **entries_listp) { char ctx_string[2+16+1]; /* enough for 64-bit pointer, in hex */ unsigned_char_p_t binding; unsigned32 binding_status; rpc_binding_handle_t binding_h; int error, i, entries; char *addrstr, *srvnamestr; unsigned short *usrvnamestr; unsigned32 level; SHARE_ENUM_STRUCT share_info; SHARE_INFO_1_CONTAINER share_info_1_container; SHARE_INFO_1 *shares, *share; unsigned32 total_entries; unsigned32 status, free_status; struct share_info *entry_list, *elp; static EXCEPTION rpc_x_connect_rejected; static int exceptions_initialized; sprintf(ctx_string, "%p", ctx); rpc_string_binding_compose(NULL, "ncacn_np", ctx_string, "srvsvc", NULL, &binding, &binding_status); if (binding_status != rpc_s_ok) { smb_error(dgettext(TEXT_DOMAIN, "rpc_string_binding_compose failed with %d"), 0, binding_status); return (EINVAL); } rpc_binding_from_string_binding(binding, &binding_h, &status); rpc_string_free(&binding, (unsigned32 *)&free_status); if (binding_status != rpc_s_ok) { smb_error(dgettext(TEXT_DOMAIN, "rpc_binding_from_string_binding failed with %d"), 0, binding_status); return (EINVAL); } level = 1; share_info.share_union.level = 1; share_info.share_union.tagged_union.share1 = &share_info_1_container; share_info_1_container.share_count = 0; share_info_1_container.shares = NULL; /* * Convert the server IP address to a string, and send that as * the "server name" - that's what Windows appears to do, and * that avoids problems with NetBIOS names containing * non-ASCII characters. */ addrstr = inet_ntoa(ctx->ct_srvinaddr.sin_addr); srvnamestr = malloc(strlen(addrstr) + 3); if (srvnamestr == NULL) { status = errno; smb_error(dgettext(TEXT_DOMAIN, "can't allocate string for server address"), status); rpc_binding_free(&binding_h, &free_status); return (status); } strcpy(srvnamestr, "\\\\"); strcat(srvnamestr, addrstr); usrvnamestr = convert_utf8_to_leunicode(srvnamestr); if (usrvnamestr == NULL) { smb_error(dgettext(TEXT_DOMAIN, "can't convert string for server address to Unicode"), 0); rpc_binding_free(&binding_h, &free_status); free(srvnamestr); return (EINVAL); } if (!exceptions_initialized) { EXCEPTION_INIT(rpc_x_connect_rejected); exc_set_status(&rpc_x_connect_rejected, rpc_s_connect_rejected); exceptions_initialized = 1; } /* printf("Calling NetrShareEnum.."); XXX */ TRY status = NetrShareEnum(binding_h, usrvnamestr, &level, &share_info, 4294967295U, &total_entries, NULL); if (status != 0) smb_error(dgettext(TEXT_DOMAIN, "error from NetrShareEnum call: status = 0x%08x"), 0, status); /*CSTYLED*/ CATCH (rpc_x_connect_rejected) /* * This is what we get if we can't open the pipe. * That's a normal occurrence when we're talking * to a system that (presumably) doesn't support * DCE RPC on the server side, such as Windows 95/98/Me, * so we don't log an error. */ /*CSTYLED*/ status = ENOTSUP; CATCH_ALL /* * XXX - should we handle some exceptions differently, * returning different errors, and try RAP only for * ENOTSUP? */ smb_error(dgettext(TEXT_DOMAIN, "error from NetrShareEnum call: exception = %u"), 0, THIS_CATCH->match.value); status = ENOTSUP; ENDTRY rpc_binding_free(&binding_h, &free_status); free(srvnamestr); free(usrvnamestr); if (status != 0) return (ENOTSUP); /* * XXX - if the IDL is correct, it's not clear whether the * unmarshalling code will properly handle the case where * a packet where "share_count" and the max count for the * array of shares don't match; a valid DCE RPC implementation * won't marshal something like that, but there's no guarantee * that the server we're talking to has a valid implementation * (which could be a *malicious* implementation!). */ entries = share_info.share_union.tagged_union.share1->share_count; shares = share_info.share_union.tagged_union.share1->shares; entry_list = calloc(entries, sizeof (struct share_info)); if (entry_list == NULL) { error = errno; goto cleanup_and_return; } for (share = shares, elp = entry_list, i = 0; i < entries; i++, share++) { elp->type = share->shi1_type; elp->netname = convert_unicode_to_utf8(share->shi1_share); if (elp->netname == NULL) goto fail; elp->remark = convert_unicode_to_utf8(share->shi1_remark); if (elp->remark == NULL) goto fail; elp++; } *entriesp = entries; *totalp = total_entries; *entries_listp = entry_list; error = 0; goto cleanup_and_return; fail: error = errno; for (elp = entry_list, i = 0; i < entries; i++, elp++) { /* * elp->netname is set before elp->remark, so if * elp->netname is null, elp->remark is also null. * If either of them is null, we haven't done anything * to any entries after this one. */ if (elp->netname == NULL) break; free(elp->netname); if (elp->remark == NULL) break; free(elp->remark); } free(entry_list); cleanup_and_return: for (share = shares, i = 0; i < entries; i++, share++) { free(share->shi1_share); free(share->shi1_remark); } free(shares); /* * XXX - "share1" should be a unique pointer, but we haven't * changed the marshalling code to support non-full pointers * in unions, so we leave it as a full pointer. * * That means that this might, or might not, be changed from * pointing to "share_info_1_container" to pointing to a * mallocated structure, according to the DCE RPC 1.1 IDL spec; * we free it only if it's changed. */ if (share_info.share_union.tagged_union.share1 != &share_info_1_container) free(share_info.share_union.tagged_union.share1); return (error); }
NET_API_STATUS NetShareEnum( const char * ServerName, uint32_t Level, PSHARE_ENUM_STRUCT * InfoStruct) { WCHAR * serverName = SMBConvertFromUTF8ToUTF16(ServerName, 1024, 0); if (!serverName || !InfoStruct) { if (serverName) free(serverName); return ERROR_INVALID_PARAMETER; } rpc_binding binding = make_rpc_binding(ServerName, "srvsvc"); if (binding.get() == NULL) { SMBLogInfo("make_rpc_binding failed", ASL_LEVEL_DEBUG); if (serverName) { free(serverName); } return ERROR_INVALID_PARAMETER; } rpc_ss_allocator_t allocator; NET_API_STATUS api_status = NERR_Success; error_status_t rpc_status = rpc_s_ok; memset(&allocator, 0, sizeof(allocator)); std::pair<rpc_mempool *, SHARE_ENUM_STRUCT *> result( allocate_rpc_mempool<SHARE_ENUM_STRUCT>()); DWORD entries = 0; DWORD resume = 0; allocator.p_allocate = share_memalloc; allocator.p_free = share_memfree; allocator.p_context = (idl_void_p_t)result.first; rpc_ss_swap_client_alloc_free_ex(&allocator, &allocator); result.second->Level = Level; /* * Windows requires a valid pointer for the ShareInfo union. It doesn't matter * which container type we choose here, since they all have the same binary * layout. */ result.second->ShareInfo.Level0 = (SHARE_INFO_0_CONTAINER *)result.first->alloc( sizeof(SHARE_INFO_0_CONTAINER)); result.second->ShareInfo.Level0->EntriesRead = 0; result.second->ShareInfo.Level0->Buffer = NULL; DCETHREAD_TRY api_status = NetrShareEnum( binding.get(), const_cast<WCHAR *>(serverName), result.second, 0xffffffff, &entries, &resume, &rpc_status); DCETHREAD_CATCH_ALL(exc) rpc_status = rpc_exception_status(exc); DCETHREAD_ENDTRY rpc_ss_swap_client_alloc_free_ex(&allocator, &allocator); free(serverName); if (rpc_status != rpc_s_ok) { SMBLogInfo("RPC to srvsrvc gave error %#08x", ASL_LEVEL_ERR, rpc_status); NetApiBufferFree(result.second); return RPC_S_PROTOCOL_ERROR; } if (api_status == NERR_Success) { *InfoStruct = result.second; } else { NetApiBufferFree(result.second); *InfoStruct = NULL; } return api_status; }