smbservice.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 SmbService local = {
  11. .name = "local",
  12. .type = "A:",
  13. .stype = STYPE_DISKTREE,
  14. .remark = "The standard namespace",
  15. .path = "/n/local",
  16. };
  17. static SmbService ipc = {
  18. .name = "IPC$",
  19. .type = "IPC",
  20. .stype = STYPE_IPC,
  21. .remark = "The aquarela IPC service",
  22. .path = nil,
  23. .next = &local,
  24. };
  25. SmbService *smbservices = &ipc;
  26. static int
  27. run9fs(char *arg)
  28. {
  29. int rv;
  30. Waitmsg *w;
  31. rv = fork();
  32. if (rv < 0)
  33. return -1;
  34. if (rv == 0) {
  35. char *argv[3];
  36. argv[0] = "/rc/bin/9fs";
  37. argv[1] = arg;
  38. argv[2] = 0;
  39. exec(argv[0], argv);
  40. exits("failed to exec 9fs");
  41. }
  42. for (;;) {
  43. w = wait();
  44. if (w == nil)
  45. return -1;
  46. if (w->pid == rv)
  47. break;
  48. free(w);
  49. }
  50. if (w->msg[0]) {
  51. smblogprint(SMB_COM_TREE_CONNECT_ANDX, "smbservicefind: %s\n", w->msg);
  52. free(w);
  53. return -1;
  54. }
  55. free(w);
  56. smblogprint(SMB_COM_TREE_CONNECT_ANDX, "smbservicefind: 9fs %s executed successfully\n", arg);
  57. return 0;
  58. }
  59. SmbService *
  60. smbservicefind(SmbSession *s, char *uncpath, char *servicetype,
  61. uint8_t *errclassp,
  62. uint16_t *errorp)
  63. {
  64. char *p, *q;
  65. if ((uncpath[0] == '/' && uncpath[1] == '/')
  66. || (uncpath[0] == '\\' && uncpath[1] == '\\')) {
  67. /* check that the server name matches mine */
  68. p = uncpath + 2;
  69. q = strchr(p, uncpath[0]);
  70. if (q == nil)
  71. goto bad;
  72. *q++ = 0;
  73. // if (cistrcmp(p, smbglobals.serverinfo.name) != 0)
  74. // goto bad;
  75. }
  76. else
  77. q = uncpath + 1;
  78. if (strcmp(servicetype, "?????") == 0 && strcmp(q, "IPC$") == 0)
  79. return &ipc;
  80. if ((strcmp(servicetype, "?????") == 0 || strcmp(servicetype, "A:") == 0)) {
  81. SmbService *serv;
  82. if (cistrcmp(q, local.name) == 0)
  83. return &local;
  84. /* try the session specific list */
  85. for (serv = s->serv; serv; serv = serv->next)
  86. if (cistrcmp(q, serv->name) == 0)
  87. return serv;
  88. /* exec "9fs q" in case it invents /n/q */
  89. for (p = q; *p; p++)
  90. if (*p >= 'A' && *p <= 'Z')
  91. *p = tolower(*p);
  92. if (run9fs(q) >= 0) {
  93. serv = smbemallocz(sizeof(*serv), 1);
  94. serv->name = smbestrdup(q);
  95. serv->type = smbestrdup("A:");
  96. serv->stype = STYPE_DISKTREE;
  97. smbstringprint(&serv->remark, "9fs %s", q);
  98. smbstringprint(&serv->path, "/n/%s", q);
  99. serv->next = s->serv;
  100. s->serv = serv;
  101. return serv;
  102. }
  103. }
  104. bad:
  105. *errclassp = ERRDOS;
  106. *errorp = ERRbadpath;
  107. return nil;
  108. }
  109. void
  110. smbserviceget(SmbService *serv)
  111. {
  112. incref(&serv->ref);
  113. }
  114. void
  115. smbserviceput(SmbService *serv)
  116. {
  117. decref(&serv->ref);
  118. }