Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  *   Copyright (C) 2020, Microsoft Corporation.
0004  *
0005  *   Author(s): Steve French <stfrench@microsoft.com>
0006  *              Suresh Jayaraman <sjayaraman@suse.de>
0007  *              Jeff Layton <jlayton@kernel.org>
0008  */
0009 
0010 #include <linux/fs.h>
0011 #include <linux/slab.h>
0012 #include <linux/inet.h>
0013 #include <linux/ctype.h>
0014 #include "cifsglob.h"
0015 #include "cifsproto.h"
0016 
0017 /* extract the host portion of the UNC string */
0018 char *extract_hostname(const char *unc)
0019 {
0020     const char *src;
0021     char *dst, *delim;
0022     unsigned int len;
0023 
0024     /* skip double chars at beginning of string */
0025     /* BB: check validity of these bytes? */
0026     if (strlen(unc) < 3)
0027         return ERR_PTR(-EINVAL);
0028     for (src = unc; *src && *src == '\\'; src++)
0029         ;
0030     if (!*src)
0031         return ERR_PTR(-EINVAL);
0032 
0033     /* delimiter between hostname and sharename is always '\\' now */
0034     delim = strchr(src, '\\');
0035     if (!delim)
0036         return ERR_PTR(-EINVAL);
0037 
0038     len = delim - src;
0039     dst = kmalloc((len + 1), GFP_KERNEL);
0040     if (dst == NULL)
0041         return ERR_PTR(-ENOMEM);
0042 
0043     memcpy(dst, src, len);
0044     dst[len] = '\0';
0045 
0046     return dst;
0047 }
0048 
0049 char *extract_sharename(const char *unc)
0050 {
0051     const char *src;
0052     char *delim, *dst;
0053 
0054     /* skip double chars at the beginning */
0055     src = unc + 2;
0056 
0057     /* share name is always preceded by '\\' now */
0058     delim = strchr(src, '\\');
0059     if (!delim)
0060         return ERR_PTR(-EINVAL);
0061     delim++;
0062 
0063     /* caller has to free the memory */
0064     dst = kstrdup(delim, GFP_KERNEL);
0065     if (!dst)
0066         return ERR_PTR(-ENOMEM);
0067 
0068     return dst;
0069 }