/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2013 Nexenta Systems, Inc. All rights reserved.
*/
#include <sys/extdirent.h>
#include <smbsrv/smb_vops.h>
#include <smbsrv/smb_kproto.h>
#include <smbsrv/smb_fsops.h>
/*
* Characters we don't allow in DOS file names.
* If a filename contains any of these chars, it should get mangled.
*
* '.' is also an invalid DOS char but since it's a special
* case it doesn't appear in the list.
*/
static const char invalid_dos_chars[] =
"\001\002\003\004\005\006\007\010\011\012\013\014\015\016\017"
"\020\021\022\023\024\025\026\027\030\031\032\033\034\035\036\037"
" \"/\\:|<>*?";
/*
* According to MSKB article #142982, Windows deletes invalid chars and
* spaces from file name in mangling process; and invalid chars include:
* ."/\[]:;=,
*
* But some of these chars and some other chars (e.g. +) are replaced
* with underscore (_). They are introduced here as special chars.
*/
static char smb_mangle_char(char);
/*
* Return true if name contains characters that are invalid in a file
* name or it is a reserved DOS device name. Otherwise, returns false.
*
* Control characters (values 0 - 31) and the following characters are
* invalid:
* < > : " / \ | ? *
*/
{
const char *p;
if (*p != ' ')
return (B_TRUE);
}
return (smb_is_reserved_dos_name(name));
}
/*
* smb_is_reserved_dos_name
*
* This function checks if the name is a reserved DOS device name.
* The device name should not be followed immediately by an extension,
* for example, NUL.txt.
*/
{
"COM5", "COM6", "COM7", "COM8", "COM9", "CON" };
"LPT6", "LPT7", "LPT8", "LPT9" };
char **reserved;
char ch;
int n_reserved;
int len;
int i;
switch (ch) {
case 'A':
case 'N':
case 'P':
break;
case 'C':
break;
case 'L':
break;
default:
return (B_FALSE);
}
for (i = 0; i < n_reserved; ++i) {
return (B_TRUE);
}
}
return (B_FALSE);
}
/*
* smb_needs_mangled
*
* A name needs to be mangled if any of the following are true:
* - the first character is dot (.) and the name is not "." or ".."
* - the name contains illegal or special charsacter
* - the name name length > 12
* - the number of dots == 0 and length > 8
* - the number of dots > 1
* - the number of dots == 1 and name is not 8.3
*/
{
const char *p;
const char *last_dot;
return (B_FALSE);
if (*name == '.')
return (B_TRUE);
len = 0;
ndots = 0;
for (p = name; *p != '\0'; ++p) {
if (smb_iscntrl(*p) ||
return (B_TRUE);
if (*p == '.') {
++ndots;
last_dot = p;
}
++len;
}
if ((len > SMB_NAME83_LEN) ||
(ndots > 1)) {
return (B_TRUE);
}
return (B_TRUE);
return (B_TRUE);
}
return (B_FALSE);
}
/*
* smb_mangle_char
*
* If c is an invalid DOS character or non-ascii, it should
* not be used in the mangled name. We return -1 to indicate
* an invalid character.
*
* If c is a special chars, it should be replaced with '_'.
*
* Otherwise c is returned as uppercase.
*/
static char
smb_mangle_char(char c)
{
if (isinvalid(c))
return (-1);
if (strchr(special_chars, c))
return ('_');
return (smb_toupper(c));
}
/*
* smb_generate_mangle
*
* Generate a mangle string containing at least 2 characters and
* at most (buflen - 1) characters.
*
* Returns the number of chars in the generated mangle.
*/
static int
{
char *p = buf;
int i;
if (fid == 0)
*p++ = '~';
*p = '\0';
return (i - 1);
}
/*
* smb_maybe_mangled
*
* Mangled names should be valid DOS file names: less than 12 characters
* long, contain at least one tilde character and conform to an 8.3 name
* format.
*
* Returns true if the name looks like a mangled name.
*/
{
const char *p;
int ndots = 0;
int i;
return (B_FALSE);
if (*p == '.') {
if ((++ndots) > 1)
return (B_FALSE);
}
if ((*p == '~') && (i < SMB_NAME83_BASELEN))
if (*p == '.' && !has_tilde)
return (B_FALSE);
}
return ((*p == '\0') && has_tilde);
}
/*
* smb_mangle
*
* Microsoft knowledge base article #142982 describes how Windows
* generates 8.3 filenames from long file names. Some other details
* can be found in article #114816.
*
* This function will mangle the name whether mangling is required
* or not. Callers should use smb_needs_mangled() to determine whether
* mangling is required.
*
* name original file name
* fid inode number to generate unique mangle
* buf output buffer (buflen bytes) to contain mangled name
*/
void
{
int i, avail;
const char *p;
char c;
char *pbuf;
/*
* Copy up to avail characters from the base part of name
* to buf then append the generated mangle string.
*/
p = name;
if ((c = smb_mangle_char(*p)) == -1)
continue;
*pbuf++ = c;
}
*pbuf = '\0';
/*
* Find the last dot in the name. If there is a dot and an
* extension, append '.' and up to SMB_NAME83_EXTLEN extension
* characters to the mangled name.
*/
*pbuf++ = '.';
for (i = 0; (i < SMB_NAME83_EXTLEN) && (*p != '\0'); ++i, ++p) {
if ((c = smb_mangle_char(*p)) == -1)
continue;
*pbuf++ = c;
}
}
*pbuf = '\0';
}
/*
* smb_unmangle
*
* Given a mangled name, try to find the real file name as it appears
* in the directory entry.
*
* smb_unmangle should only be called on names for which
* smb_maybe_mangled() is true
*
* File systems which support VFSFT_EDIRENT_FLAGS will return the
* directory entries as a buffer of edirent_t structure. Others will
* return a buffer of dirent64_t structures. A union is used for the
* the pointer into the buffer (bufptr, edp and dp).
*
* Returns:
* 0 - SUCCESS. Unmangled name is returned in namebuf.
* EINVAL - a parameter was invalid.
* ENOTDIR - dnode is not a directory node.
* ENOENT - an unmangled name could not be found.
*/
int
{
union {
char *u_bufptr;
} u;
return (EINVAL);
if (!smb_node_is_dir(dnode))
return (ENOTDIR);
*namebuf = '\0';
offset = 0;
if (bufsize == 0) {
break;
}
reclen = 0;
if (is_edp) {
} else {
}
/* skip non utf8 filename */
U8_VALIDATE_ENTIRE, &err) < 0)
continue;
return (0);
}
}
if (eof) {
break;
}
}
return (err);
}