smbrap2client.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include "headers.h"
  10. static SmbTransactionMethod smbtransactionmethodrap = {
  11. .encodeprimary = smbtransactionencodeprimary,
  12. .sendrequest = smbtransactionclientsend,
  13. .receiveresponse = smbtransactionclientreceive,
  14. .decoderesponse = smbtransactiondecoderesponse,
  15. };
  16. int
  17. smbclientrap(SmbClient *c, SmbBuffer *inparam, SmbBuffer *outparam, SmbBuffer *outdata,
  18. char **errmsgp)
  19. {
  20. SmbTransaction transaction;
  21. SmbHeader h;
  22. memset(&transaction, 0, sizeof(transaction));
  23. transaction.in.name = smbglobals.pipelanman;
  24. transaction.in.parameters = smbbufferreadpointer(inparam);
  25. transaction.in.tpcount = smbbufferreadspace(inparam);
  26. transaction.in.maxpcount = smbbufferwritespace(outparam);
  27. transaction.in.maxdcount = smbbufferwritespace(outdata);
  28. transaction.out.parameters = outparam;
  29. transaction.out.data = outdata;
  30. h = c->protoh;
  31. h.tid = c->ipctid;
  32. h.mid = 0;
  33. return smbtransactionexecute(&transaction, &h, &c->peerinfo, c->b, &smbtransactionmethodrap, c, nil, errmsgp);
  34. }
  35. int
  36. smbnetserverenum2(SmbClient *c, uint32_t stype, char *domain,
  37. int *entriesp,
  38. SmbRapServerInfo1 **sip, char **errmsgp)
  39. {
  40. int rv;
  41. uint16_t ec, entries, total, converter;
  42. SmbRapServerInfo1 *si = nil;
  43. SmbBuffer *ipb = smbbuffernew(512);
  44. SmbBuffer *odb = smbbuffernew(65535);
  45. SmbBuffer *opb = smbbuffernew(8);
  46. smbbufferputs(ipb, 104);
  47. smbbufferputstring(ipb, nil, SMB_STRING_ASCII, "WrLehDz");
  48. smbbufferputstring(ipb, nil, SMB_STRING_ASCII, "B16BBDz");
  49. smbbufferputs(ipb, 1);
  50. smbbufferputs(ipb, smbbufferwritespace(odb));
  51. smbbufferputl(ipb, stype);
  52. smbbufferputstring(ipb, nil, SMB_STRING_ASCII, domain);
  53. rv = !smbclientrap(c, ipb, opb, odb, errmsgp);
  54. smbbufferfree(&ipb);
  55. if (rv == 0) {
  56. char *remark, *eremark;
  57. int remarkspace;
  58. int i;
  59. if (!smbbuffergets(opb, &ec)
  60. || !smbbuffergets(opb, &converter)
  61. || !smbbuffergets(opb, &entries)
  62. || !smbbuffergets(opb, &total)) {
  63. smbstringprint(errmsgp, "smbnetserverenum2: not enough return parameters");
  64. rv = -1;
  65. goto done;
  66. }
  67. if (ec != 0) {
  68. rv = ec;
  69. goto done;
  70. }
  71. if (smbbufferreadspace(odb) < entries * 26) {
  72. smbstringprint(errmsgp, "smbnetserverenum2: not enough return data");
  73. rv = -1;
  74. goto done;
  75. }
  76. remarkspace = smbbufferreadspace(odb) - entries * 26;
  77. si = smbemalloc(entries * sizeof(SmbRapServerInfo1) + remarkspace);
  78. remark = (char *)&si[entries];
  79. eremark = remark + remarkspace;
  80. for (i = 0; i < entries; i++) {
  81. uint32_t offset;
  82. int remarklen;
  83. assert(smbbuffergetbytes(odb, si[i].name, 16));
  84. assert(smbbuffergetb(odb, &si[i].vmaj));
  85. assert(smbbuffergetb(odb, &si[i].vmin));
  86. assert(smbbuffergetl(odb, &si[i].type));
  87. assert(smbbuffergetl(odb, &offset));
  88. offset -= converter;
  89. if (!smbbufferoffsetcopystr(odb, offset, remark, eremark - remark, &remarklen)) {
  90. smbstringprint(errmsgp, "smbnetserverenum2: invalid string offset");
  91. rv = -1;
  92. goto done;
  93. }
  94. si[i].remark = remark;
  95. remark += remarklen;
  96. }
  97. *sip = si;
  98. si = nil;
  99. *entriesp = entries;
  100. }
  101. else
  102. rv = -1;
  103. done:
  104. free(si);
  105. smbbufferfree(&opb);
  106. smbbufferfree(&odb);
  107. return rv;
  108. }