unc.c 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2020, Microsoft Corporation.
  4. *
  5. * Author(s): Steve French <[email protected]>
  6. * Suresh Jayaraman <[email protected]>
  7. * Jeff Layton <[email protected]>
  8. */
  9. #include <linux/fs.h>
  10. #include <linux/slab.h>
  11. #include <linux/inet.h>
  12. #include <linux/ctype.h>
  13. #include "cifsglob.h"
  14. #include "cifsproto.h"
  15. /* extract the host portion of the UNC string */
  16. char *extract_hostname(const char *unc)
  17. {
  18. const char *src;
  19. char *dst, *delim;
  20. unsigned int len;
  21. /* skip double chars at beginning of string */
  22. /* BB: check validity of these bytes? */
  23. if (strlen(unc) < 3)
  24. return ERR_PTR(-EINVAL);
  25. for (src = unc; *src && *src == '\\'; src++)
  26. ;
  27. if (!*src)
  28. return ERR_PTR(-EINVAL);
  29. /* delimiter between hostname and sharename is always '\\' now */
  30. delim = strchr(src, '\\');
  31. if (!delim)
  32. return ERR_PTR(-EINVAL);
  33. len = delim - src;
  34. dst = kmalloc((len + 1), GFP_KERNEL);
  35. if (dst == NULL)
  36. return ERR_PTR(-ENOMEM);
  37. memcpy(dst, src, len);
  38. dst[len] = '\0';
  39. return dst;
  40. }
  41. char *extract_sharename(const char *unc)
  42. {
  43. const char *src;
  44. char *delim, *dst;
  45. /* skip double chars at the beginning */
  46. src = unc + 2;
  47. /* share name is always preceded by '\\' now */
  48. delim = strchr(src, '\\');
  49. if (!delim)
  50. return ERR_PTR(-EINVAL);
  51. delim++;
  52. /* caller has to free the memory */
  53. dst = kstrdup(delim, GFP_KERNEL);
  54. if (!dst)
  55. return ERR_PTR(-ENOMEM);
  56. return dst;
  57. }