/* * smb_sd_write * * Takes a Win SD in absolute form, converts it to * ZFS format and write it to filesystem. The write routine * converts ZFS acl to Posix acl if required. */ uint32_t smb_sd_write(smb_request_t *sr, smb_sd_t *sd, uint32_t secinfo) { smb_node_t *node; smb_fssd_t fs_sd; uint32_t status; uint32_t sd_flags; int error; node = sr->fid_ofile->f_node; sd_flags = smb_node_is_dir(node) ? SMB_FSSD_FLAGS_DIR : 0; smb_fssd_init(&fs_sd, secinfo, sd_flags); status = smb_sd_tofs(sd, &fs_sd); if (status != NT_STATUS_SUCCESS) { smb_fssd_term(&fs_sd); return (status); } error = smb_fsop_sdwrite(sr, sr->user_cr, node, &fs_sd, 0); smb_fssd_term(&fs_sd); if (error) { if (error == EBADE) return (NT_STATUS_INVALID_OWNER); return (smb_errno2status(error)); } return (NT_STATUS_SUCCESS); }
/* * smb_odir_open * * Create an odir representing the directory specified in pathname. * * Returns: * odid - Unique identifier of newly created odir. * 0 - error, error details set in sr. */ uint16_t smb_odir_open(smb_request_t *sr, char *path, uint16_t sattr, uint32_t flags) { int rc; smb_tree_t *tree; smb_node_t *dnode; char pattern[MAXNAMELEN]; uint16_t odid; cred_t *cr; ASSERT(sr); ASSERT(sr->sr_magic == SMB_REQ_MAGIC); ASSERT(sr->tid_tree); ASSERT(sr->tid_tree->t_magic == SMB_TREE_MAGIC); tree = sr->tid_tree; if (sr->session->dialect < NT_LM_0_12) smb_convert_wildcards(path); rc = smb_pathname_reduce(sr, sr->user_cr, path, tree->t_snode, tree->t_snode, &dnode, pattern); if (rc != 0) { smbsr_errno(sr, rc); return (0); } if (!smb_node_is_dir(dnode)) { smbsr_error(sr, NT_STATUS_OBJECT_PATH_NOT_FOUND, ERRDOS, ERROR_PATH_NOT_FOUND); smb_node_release(dnode); return (0); } if (smb_fsop_access(sr, sr->user_cr, dnode, FILE_LIST_DIRECTORY) != 0) { smbsr_error(sr, NT_STATUS_ACCESS_DENIED, ERRDOS, ERROR_ACCESS_DENIED); smb_node_release(dnode); return (0); } if (flags & SMB_ODIR_OPENF_BACKUP_INTENT) cr = smb_user_getprivcred(sr->uid_user); else cr = sr->uid_user->u_cred; odid = smb_odir_create(sr, dnode, pattern, sattr, cr); smb_node_release(dnode); return (odid); }
/* * smb_sd_read * * Read uid, gid and ACL from filesystem. The returned ACL from read * routine is always in ZFS format. Convert the ZFS acl to a Win acl * and return the Win SD in absolute form. * * NOTE: upon successful return caller MUST free the memory allocated * for the returned SD by calling smb_sd_term(). */ uint32_t smb_sd_read(smb_request_t *sr, smb_sd_t *sd, uint32_t secinfo) { smb_fssd_t fs_sd; smb_node_t *node; uint32_t status = NT_STATUS_SUCCESS; uint32_t sd_flags; int error; node = sr->fid_ofile->f_node; sd_flags = smb_node_is_dir(node) ? SMB_FSSD_FLAGS_DIR : 0; smb_fssd_init(&fs_sd, secinfo, sd_flags); error = smb_fsop_sdread(sr, sr->user_cr, node, &fs_sd); if (error) return (smb_errno2status(error)); status = smb_sd_fromfs(&fs_sd, sd); smb_fssd_term(&fs_sd); return (status); }
/* * smb_odir_lookup_link * * If the file is a symlink we lookup the object to which the * symlink refers so that we can return its attributes. * This can cause a problem if a symlink in a sub-directory * points to a parent directory (some UNIX GUI's create a symlink * in $HOME/.desktop that points to the user's home directory). * Some Windows applications (e.g. virus scanning) loop/hang * trying to follow this recursive path and there is little * we can do because the path is constructed on the client. * smb_dirsymlink_enable allows an end-user to disable * symlinks to directories. Symlinks to other object types * should be unaffected. * * Returns: B_TRUE - followed link. tgt_node and tgt_attr set * B_FALSE - link not followed */ static boolean_t smb_odir_lookup_link(smb_request_t *sr, smb_odir_t *od, char *fname, smb_node_t **tgt_node) { int rc; uint32_t flags = SMB_FOLLOW_LINKS | SMB_CASE_SENSITIVE; rc = smb_fsop_lookup(sr, od->d_cred, flags, od->d_tree->t_snode, od->d_dnode, fname, tgt_node); if (rc != 0) { *tgt_node = NULL; return (B_FALSE); } if (smb_node_is_dir(*tgt_node) && (!smb_dirsymlink_enable)) { smb_node_release(*tgt_node); *tgt_node = NULL; return (B_FALSE); } return (B_TRUE); }
/* * smb_set_open_timestamps * * Last write time: * - If the last_write time specified in the open params is not 0 or -1, * use it as file's mtime. This will be considered an explicitly set * timestamps, not reset by subsequent writes. * * Opening existing file (not directory): * - If opening an existing file for overwrite set initial ATIME, MTIME * & CTIME to now. (This is achieved by setting them as pending then forcing * an smb_node_setattr() to apply pending times.) * * - Note If opening an existing file NOT for overwrite, windows would * set the atime on file close, however setting the atime would cause * the ARCHIVE attribute to be set, which does not occur on windows, * so we do not do the atime update. * * Returns: errno */ static int smb_set_open_timestamps(smb_request_t *sr, smb_ofile_t *of, boolean_t created) { int rc = 0; smb_arg_open_t *op = &sr->sr_open; smb_node_t *node = of->f_node; boolean_t existing_file, set_times; smb_attr_t attr; bzero(&attr, sizeof (smb_attr_t)); set_times = B_FALSE; if ((op->mtime.tv_sec != 0) && (op->mtime.tv_sec != UINT_MAX)) { attr.sa_mask = SMB_AT_MTIME; attr.sa_vattr.va_mtime = op->mtime; set_times = B_TRUE; } existing_file = !(created || smb_node_is_dir(node)); if (existing_file) { switch (op->create_disposition) { case FILE_SUPERSEDE: case FILE_OVERWRITE_IF: case FILE_OVERWRITE: smb_ofile_set_write_time_pending(of); set_times = B_TRUE; break; default: break; } } if (set_times) rc = smb_node_setattr(sr, node, sr->user_cr, of, &attr); return (rc); }
/* * smb_open_subr * * Notes on write-through behaviour. It looks like pre-LM0.12 versions * of the protocol specify the write-through mode when a file is opened, * (SmbOpen, SmbOpenAndX) so the write calls (SmbWrite, SmbWriteAndClose, * SmbWriteAndUnlock) don't need to contain a write-through flag. * * With LM0.12, the open calls (SmbCreateAndX, SmbNtTransactCreate) * don't indicate which write-through mode to use. Instead the write * calls (SmbWriteAndX, SmbWriteRaw) specify the mode on a per call * basis. * * We don't care which open call was used to get us here, we just need * to ensure that the write-through mode flag is copied from the open * parameters to the node. We test the omode write-through flag in all * write functions. * * This function will return NT status codes but it also raises errors, * in which case it won't return to the caller. Be careful how you * handle things in here. * * The following rules apply when processing a file open request: * * - Oplocks must be broken prior to share checking as the break may * cause other clients to close the file, which would affect sharing * checks. * * - Share checks must take place prior to access checks for correct * Windows semantics and to prevent unnecessary NFS delegation recalls. * * - Oplocks must be acquired after open to ensure the correct * synchronization with NFS delegation and FEM installation. * * DOS readonly bit rules * * 1. The creator of a readonly file can write to/modify the size of the file * using the original create fid, even though the file will appear as readonly * to all other fids and via a CIFS getattr call. * The readonly bit therefore cannot be set in the filesystem until the file * is closed (smb_ofile_close). It is accounted for via ofile and node flags. * * 2. A setinfo operation (using either an open fid or a path) to set/unset * readonly will be successful regardless of whether a creator of a readonly * file has an open fid (and has the special privilege mentioned in #1, * above). I.e., the creator of a readonly fid holding that fid will no longer * have a special privilege. * * 3. The DOS readonly bit affects only data and some metadata. * The following metadata can be changed regardless of the readonly bit: * - security descriptors * - DOS attributes * - timestamps * * In the current implementation, the file size cannot be changed (except for * the exceptions in #1 and #2, above). * * * DOS attribute rules * * These rules are specific to creating / opening files and directories. * How the attribute value (specifically ZERO or FILE_ATTRIBUTE_NORMAL) * should be interpreted may differ in other requests. * * - An attribute value equal to ZERO or FILE_ATTRIBUTE_NORMAL means that the * file's attributes should be cleared. * - If FILE_ATTRIBUTE_NORMAL is specified with any other attributes, * FILE_ATTRIBUTE_NORMAL is ignored. * * 1. Creating a new file * - The request attributes + FILE_ATTRIBUTE_ARCHIVE are applied to the file. * * 2. Creating a new directory * - The request attributes + FILE_ATTRIBUTE_DIRECTORY are applied to the file. * - FILE_ATTRIBUTE_ARCHIVE does not get set. * * 3. Overwriting an existing file * - the request attributes are used as search attributes. If the existing * file does not meet the search criteria access is denied. * - otherwise, applies attributes + FILE_ATTRIBUTE_ARCHIVE. * * 4. Opening an existing file or directory * The request attributes are ignored. */ static uint32_t smb_open_subr(smb_request_t *sr) { boolean_t created = B_FALSE; boolean_t last_comp_found = B_FALSE; smb_node_t *node = NULL; smb_node_t *dnode = NULL; smb_node_t *cur_node = NULL; smb_arg_open_t *op = &sr->sr_open; int rc; smb_ofile_t *of; smb_attr_t new_attr; int max_requested = 0; uint32_t max_allowed; uint32_t status = NT_STATUS_SUCCESS; int is_dir; smb_error_t err; boolean_t is_stream = B_FALSE; int lookup_flags = SMB_FOLLOW_LINKS; uint32_t uniq_fid; smb_pathname_t *pn = &op->fqi.fq_path; smb_server_t *sv = sr->sr_server; is_dir = (op->create_options & FILE_DIRECTORY_FILE) ? 1 : 0; /* * If the object being created or opened is a directory * the Disposition parameter must be one of FILE_CREATE, * FILE_OPEN, or FILE_OPEN_IF */ if (is_dir) { if ((op->create_disposition != FILE_CREATE) && (op->create_disposition != FILE_OPEN_IF) && (op->create_disposition != FILE_OPEN)) { smbsr_error(sr, NT_STATUS_INVALID_PARAMETER, ERRDOS, ERROR_INVALID_ACCESS); return (NT_STATUS_INVALID_PARAMETER); } } if (op->desired_access & MAXIMUM_ALLOWED) { max_requested = 1; op->desired_access &= ~MAXIMUM_ALLOWED; } op->desired_access = smb_access_generic_to_file(op->desired_access); if (sr->session->s_file_cnt >= SMB_SESSION_OFILE_MAX) { ASSERT(sr->uid_user); cmn_err(CE_NOTE, "smbsrv[%s\\%s]: TOO_MANY_OPENED_FILES", sr->uid_user->u_domain, sr->uid_user->u_name); smbsr_error(sr, NT_STATUS_TOO_MANY_OPENED_FILES, ERRDOS, ERROR_TOO_MANY_OPEN_FILES); return (NT_STATUS_TOO_MANY_OPENED_FILES); } /* This must be NULL at this point */ sr->fid_ofile = NULL; op->devstate = 0; switch (sr->tid_tree->t_res_type & STYPE_MASK) { case STYPE_DISKTREE: case STYPE_PRINTQ: break; case STYPE_IPC: if ((rc = smb_threshold_enter(&sv->sv_opipe_ct)) != 0) { status = RPC_NT_SERVER_TOO_BUSY; smbsr_error(sr, status, 0, 0); return (status); } /* * No further processing for IPC, we need to either * raise an exception or return success here. */ if ((status = smb_opipe_open(sr)) != NT_STATUS_SUCCESS) smbsr_error(sr, status, 0, 0); smb_threshold_exit(&sv->sv_opipe_ct, sv); return (status); default: smbsr_error(sr, NT_STATUS_BAD_DEVICE_TYPE, ERRDOS, ERROR_BAD_DEV_TYPE); return (NT_STATUS_BAD_DEVICE_TYPE); } smb_pathname_init(sr, pn, pn->pn_path); if (!smb_pathname_validate(sr, pn)) return (sr->smb_error.status); if (strlen(pn->pn_path) >= MAXPATHLEN) { smbsr_error(sr, 0, ERRSRV, ERRfilespecs); return (NT_STATUS_NAME_TOO_LONG); } if (is_dir) { if (!smb_validate_dirname(sr, pn)) return (sr->smb_error.status); } else { if (!smb_validate_object_name(sr, pn)) return (sr->smb_error.status); } cur_node = op->fqi.fq_dnode ? op->fqi.fq_dnode : sr->tid_tree->t_snode; /* * if no path or filename are specified the stream should be * created on cur_node */ if (!is_dir && !pn->pn_pname && !pn->pn_fname && pn->pn_sname) { /* * Can't currently handle a stream on the tree root. * If a stream is being opened return "not found", otherwise * return "access denied". */ if (cur_node == sr->tid_tree->t_snode) { if (op->create_disposition == FILE_OPEN) { smbsr_error(sr, NT_STATUS_OBJECT_NAME_NOT_FOUND, ERRDOS, ERROR_FILE_NOT_FOUND); return (NT_STATUS_OBJECT_NAME_NOT_FOUND); } smbsr_error(sr, NT_STATUS_ACCESS_DENIED, ERRDOS, ERROR_ACCESS_DENIED); return (NT_STATUS_ACCESS_DENIED); } (void) snprintf(op->fqi.fq_last_comp, sizeof (op->fqi.fq_last_comp), "%s%s", cur_node->od_name, pn->pn_sname); op->fqi.fq_dnode = cur_node->n_dnode; smb_node_ref(op->fqi.fq_dnode); } else { if (rc = smb_pathname_reduce(sr, sr->user_cr, pn->pn_path, sr->tid_tree->t_snode, cur_node, &op->fqi.fq_dnode, op->fqi.fq_last_comp)) { smbsr_errno(sr, rc); return (sr->smb_error.status); } } /* * If the access mask has only DELETE set (ignore * FILE_READ_ATTRIBUTES), then assume that this * is a request to delete the link (if a link) * and do not follow links. Otherwise, follow * the link to the target. */ if ((op->desired_access & ~FILE_READ_ATTRIBUTES) == DELETE) lookup_flags &= ~SMB_FOLLOW_LINKS; rc = smb_fsop_lookup_name(sr, kcred, lookup_flags, sr->tid_tree->t_snode, op->fqi.fq_dnode, op->fqi.fq_last_comp, &op->fqi.fq_fnode); if (rc == 0) { last_comp_found = B_TRUE; rc = smb_node_getattr(sr, op->fqi.fq_fnode, &op->fqi.fq_fattr); if (rc != 0) { smb_node_release(op->fqi.fq_fnode); smb_node_release(op->fqi.fq_dnode); smbsr_error(sr, NT_STATUS_INTERNAL_ERROR, ERRDOS, ERROR_INTERNAL_ERROR); return (sr->smb_error.status); } } else if (rc == ENOENT) { last_comp_found = B_FALSE; op->fqi.fq_fnode = NULL; rc = 0; } else { smb_node_release(op->fqi.fq_dnode); smbsr_errno(sr, rc); return (sr->smb_error.status); } /* * The uniq_fid is a CIFS-server-wide unique identifier for an ofile * which is used to uniquely identify open instances for the * VFS share reservation and POSIX locks. */ uniq_fid = SMB_UNIQ_FID(); if (last_comp_found) { node = op->fqi.fq_fnode; dnode = op->fqi.fq_dnode; if (!smb_node_is_file(node) && !smb_node_is_dir(node) && !smb_node_is_symlink(node)) { smb_node_release(node); smb_node_release(dnode); smbsr_error(sr, NT_STATUS_ACCESS_DENIED, ERRDOS, ERRnoaccess); return (NT_STATUS_ACCESS_DENIED); } /* * Reject this request if either: * - the target IS a directory and the client requires that * it must NOT be (required by Lotus Notes) * - the target is NOT a directory and client requires that * it MUST be. */ if (smb_node_is_dir(node)) { if (op->create_options & FILE_NON_DIRECTORY_FILE) { smb_node_release(node); smb_node_release(dnode); smbsr_error(sr, NT_STATUS_FILE_IS_A_DIRECTORY, ERRDOS, ERROR_ACCESS_DENIED); return (NT_STATUS_FILE_IS_A_DIRECTORY); } } else { if ((op->create_options & FILE_DIRECTORY_FILE) || (op->nt_flags & NT_CREATE_FLAG_OPEN_TARGET_DIR)) { smb_node_release(node); smb_node_release(dnode); smbsr_error(sr, NT_STATUS_NOT_A_DIRECTORY, ERRDOS, ERROR_DIRECTORY); return (NT_STATUS_NOT_A_DIRECTORY); } } /* * No more open should be accepted when "Delete on close" * flag is set. */ if (node->flags & NODE_FLAGS_DELETE_ON_CLOSE) { smb_node_release(node); smb_node_release(dnode); smbsr_error(sr, NT_STATUS_DELETE_PENDING, ERRDOS, ERROR_ACCESS_DENIED); return (NT_STATUS_DELETE_PENDING); } /* * Specified file already exists so the operation should fail. */ if (op->create_disposition == FILE_CREATE) { smb_node_release(node); smb_node_release(dnode); smbsr_error(sr, NT_STATUS_OBJECT_NAME_COLLISION, ERRDOS, ERROR_FILE_EXISTS); return (NT_STATUS_OBJECT_NAME_COLLISION); } /* * Windows seems to check read-only access before file * sharing check. * * Check to see if the file is currently readonly (irrespective * of whether this open will make it readonly). */ if (SMB_PATHFILE_IS_READONLY(sr, node)) { /* Files data only */ if (!smb_node_is_dir(node)) { if (op->desired_access & (FILE_WRITE_DATA | FILE_APPEND_DATA)) { smb_node_release(node); smb_node_release(dnode); smbsr_error(sr, NT_STATUS_ACCESS_DENIED, ERRDOS, ERRnoaccess); return (NT_STATUS_ACCESS_DENIED); } } } /* * Oplock break is done prior to sharing checks as the break * may cause other clients to close the file which would * affect the sharing checks. */ smb_node_inc_opening_count(node); smb_open_oplock_break(sr, node); smb_node_wrlock(node); if ((op->create_disposition == FILE_SUPERSEDE) || (op->create_disposition == FILE_OVERWRITE_IF) || (op->create_disposition == FILE_OVERWRITE)) { if ((!(op->desired_access & (FILE_WRITE_DATA | FILE_APPEND_DATA | FILE_WRITE_ATTRIBUTES | FILE_WRITE_EA))) || (!smb_sattr_check(op->fqi.fq_fattr.sa_dosattr, op->dattr))) { smb_node_unlock(node); smb_node_dec_opening_count(node); smb_node_release(node); smb_node_release(dnode); smbsr_error(sr, NT_STATUS_ACCESS_DENIED, ERRDOS, ERRnoaccess); return (NT_STATUS_ACCESS_DENIED); } } status = smb_fsop_shrlock(sr->user_cr, node, uniq_fid, op->desired_access, op->share_access); if (status == NT_STATUS_SHARING_VIOLATION) { smb_node_unlock(node); smb_node_dec_opening_count(node); smb_node_release(node); smb_node_release(dnode); return (status); } status = smb_fsop_access(sr, sr->user_cr, node, op->desired_access); if (status != NT_STATUS_SUCCESS) { smb_fsop_unshrlock(sr->user_cr, node, uniq_fid); smb_node_unlock(node); smb_node_dec_opening_count(node); smb_node_release(node); smb_node_release(dnode); if (status == NT_STATUS_PRIVILEGE_NOT_HELD) { smbsr_error(sr, status, ERRDOS, ERROR_PRIVILEGE_NOT_HELD); return (status); } else { smbsr_error(sr, NT_STATUS_ACCESS_DENIED, ERRDOS, ERROR_ACCESS_DENIED); return (NT_STATUS_ACCESS_DENIED); } } switch (op->create_disposition) { case FILE_SUPERSEDE: case FILE_OVERWRITE_IF: case FILE_OVERWRITE: if (smb_node_is_dir(node)) { smb_fsop_unshrlock(sr->user_cr, node, uniq_fid); smb_node_unlock(node); smb_node_dec_opening_count(node); smb_node_release(node); smb_node_release(dnode); smbsr_error(sr, NT_STATUS_ACCESS_DENIED, ERRDOS, ERROR_ACCESS_DENIED); return (NT_STATUS_ACCESS_DENIED); } op->dattr |= FILE_ATTRIBUTE_ARCHIVE; /* Don't apply readonly bit until smb_ofile_close */ if (op->dattr & FILE_ATTRIBUTE_READONLY) { op->created_readonly = B_TRUE; op->dattr &= ~FILE_ATTRIBUTE_READONLY; } bzero(&new_attr, sizeof (new_attr)); new_attr.sa_dosattr = op->dattr; new_attr.sa_vattr.va_size = op->dsize; new_attr.sa_mask = SMB_AT_DOSATTR | SMB_AT_SIZE; rc = smb_fsop_setattr(sr, sr->user_cr, node, &new_attr); if (rc != 0) { smb_fsop_unshrlock(sr->user_cr, node, uniq_fid); smb_node_unlock(node); smb_node_dec_opening_count(node); smb_node_release(node); smb_node_release(dnode); smbsr_errno(sr, rc); return (sr->smb_error.status); } /* * If file is being replaced, remove existing streams */ if (SMB_IS_STREAM(node) == 0) { rc = smb_fsop_remove_streams(sr, sr->user_cr, node); if (rc != 0) { smb_fsop_unshrlock(sr->user_cr, node, uniq_fid); smb_node_unlock(node); smb_node_dec_opening_count(node); smb_node_release(node); smb_node_release(dnode); return (sr->smb_error.status); } } op->action_taken = SMB_OACT_TRUNCATED; break; default: /* * FILE_OPEN or FILE_OPEN_IF. */ op->action_taken = SMB_OACT_OPENED; break; } } else { /* Last component was not found. */ dnode = op->fqi.fq_dnode; if (is_dir == 0) is_stream = smb_is_stream_name(pn->pn_path); if ((op->create_disposition == FILE_OPEN) || (op->create_disposition == FILE_OVERWRITE)) { smb_node_release(dnode); smbsr_error(sr, NT_STATUS_OBJECT_NAME_NOT_FOUND, ERRDOS, ERROR_FILE_NOT_FOUND); return (NT_STATUS_OBJECT_NAME_NOT_FOUND); } if (pn->pn_fname && smb_is_invalid_filename(pn->pn_fname)) { smb_node_release(dnode); smbsr_error(sr, NT_STATUS_OBJECT_NAME_INVALID, ERRDOS, ERROR_INVALID_NAME); return (NT_STATUS_OBJECT_NAME_INVALID); } /* * lock the parent dir node in case another create * request to the same parent directory comes in. */ smb_node_wrlock(dnode); /* Don't apply readonly bit until smb_ofile_close */ if (op->dattr & FILE_ATTRIBUTE_READONLY) { op->dattr &= ~FILE_ATTRIBUTE_READONLY; op->created_readonly = B_TRUE; } bzero(&new_attr, sizeof (new_attr)); if ((op->crtime.tv_sec != 0) && (op->crtime.tv_sec != UINT_MAX)) { new_attr.sa_mask |= SMB_AT_CRTIME; new_attr.sa_crtime = op->crtime; } if (is_dir == 0) { op->dattr |= FILE_ATTRIBUTE_ARCHIVE; new_attr.sa_dosattr = op->dattr; new_attr.sa_vattr.va_type = VREG; new_attr.sa_vattr.va_mode = is_stream ? S_IRUSR : S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR | S_IWGRP | S_IWOTH; new_attr.sa_mask |= SMB_AT_DOSATTR | SMB_AT_TYPE | SMB_AT_MODE; if (op->dsize) { new_attr.sa_vattr.va_size = op->dsize; new_attr.sa_mask |= SMB_AT_SIZE; } rc = smb_fsop_create(sr, sr->user_cr, dnode, op->fqi.fq_last_comp, &new_attr, &op->fqi.fq_fnode); if (rc != 0) { smb_node_unlock(dnode); smb_node_release(dnode); smbsr_errno(sr, rc); return (sr->smb_error.status); } node = op->fqi.fq_fnode; smb_node_inc_opening_count(node); smb_node_wrlock(node); status = smb_fsop_shrlock(sr->user_cr, node, uniq_fid, op->desired_access, op->share_access); if (status == NT_STATUS_SHARING_VIOLATION) { smb_node_unlock(node); smb_node_dec_opening_count(node); smb_delete_new_object(sr); smb_node_release(node); smb_node_unlock(dnode); smb_node_release(dnode); return (status); } } else { op->dattr |= FILE_ATTRIBUTE_DIRECTORY; new_attr.sa_dosattr = op->dattr; new_attr.sa_vattr.va_type = VDIR; new_attr.sa_vattr.va_mode = 0777; new_attr.sa_mask |= SMB_AT_DOSATTR | SMB_AT_TYPE | SMB_AT_MODE; rc = smb_fsop_mkdir(sr, sr->user_cr, dnode, op->fqi.fq_last_comp, &new_attr, &op->fqi.fq_fnode); if (rc != 0) { smb_node_unlock(dnode); smb_node_release(dnode); smbsr_errno(sr, rc); return (sr->smb_error.status); } node = op->fqi.fq_fnode; smb_node_inc_opening_count(node); smb_node_wrlock(node); } created = B_TRUE; op->action_taken = SMB_OACT_CREATED; } if (max_requested) { smb_fsop_eaccess(sr, sr->user_cr, node, &max_allowed); op->desired_access |= max_allowed; } status = NT_STATUS_SUCCESS; of = smb_ofile_open(sr->tid_tree, node, sr->smb_pid, op, SMB_FTYPE_DISK, uniq_fid, &err); if (of == NULL) { smbsr_error(sr, err.status, err.errcls, err.errcode); status = err.status; } if (status == NT_STATUS_SUCCESS) { if (!smb_tree_is_connected(sr->tid_tree)) { smbsr_error(sr, 0, ERRSRV, ERRinvnid); status = NT_STATUS_UNSUCCESSFUL; } } /* * This MUST be done after ofile creation, so that explicitly * set timestamps can be remembered on the ofile. */ if (status == NT_STATUS_SUCCESS) { if ((rc = smb_set_open_timestamps(sr, of, created)) != 0) { smbsr_errno(sr, rc); status = sr->smb_error.status; } } if (status == NT_STATUS_SUCCESS) { if (smb_node_getattr(sr, node, &op->fqi.fq_fattr) != 0) { smbsr_error(sr, NT_STATUS_INTERNAL_ERROR, ERRDOS, ERROR_INTERNAL_ERROR); status = NT_STATUS_INTERNAL_ERROR; } } /* * smb_fsop_unshrlock is a no-op if node is a directory * smb_fsop_unshrlock is done in smb_ofile_close */ if (status != NT_STATUS_SUCCESS) { if (of == NULL) { smb_fsop_unshrlock(sr->user_cr, node, uniq_fid); } else { smb_ofile_close(of, 0); smb_ofile_release(of); } if (created) smb_delete_new_object(sr); smb_node_unlock(node); smb_node_dec_opening_count(node); smb_node_release(node); if (created) smb_node_unlock(dnode); smb_node_release(dnode); return (status); } /* * Propagate the write-through mode from the open params * to the node: see the notes in the function header. */ if (sr->sr_cfg->skc_sync_enable || (op->create_options & FILE_WRITE_THROUGH)) node->flags |= NODE_FLAGS_WRITE_THROUGH; /* * Set up the fileid and dosattr in open_param for response */ op->fileid = op->fqi.fq_fattr.sa_vattr.va_nodeid; op->dattr = op->fqi.fq_fattr.sa_dosattr; /* * Set up the file type in open_param for the response */ op->ftype = SMB_FTYPE_DISK; sr->smb_fid = of->f_fid; sr->fid_ofile = of; if (smb_node_is_file(node)) { smb_oplock_acquire(sr, node, of); op->dsize = op->fqi.fq_fattr.sa_vattr.va_size; } else { /* directory or symlink */ op->op_oplock_level = SMB_OPLOCK_NONE; op->dsize = 0; } smb_node_dec_opening_count(node); smb_node_unlock(node); if (created) smb_node_unlock(dnode); smb_node_release(node); smb_node_release(dnode); return (NT_STATUS_SUCCESS); }
/* * smb_open_subr * * Notes on write-through behaviour. It looks like pre-LM0.12 versions * of the protocol specify the write-through mode when a file is opened, * (SmbOpen, SmbOpenAndX) so the write calls (SmbWrite, SmbWriteAndClose, * SmbWriteAndUnlock) don't need to contain a write-through flag. * * With LM0.12, the open calls (SmbCreateAndX, SmbNtTransactCreate) * don't indicate which write-through mode to use. Instead the write * calls (SmbWriteAndX, SmbWriteRaw) specify the mode on a per call * basis. * * We don't care which open call was used to get us here, we just need * to ensure that the write-through mode flag is copied from the open * parameters to the node. We test the omode write-through flag in all * write functions. * * This function returns NT status codes. * * The following rules apply when processing a file open request: * * - Oplocks must be broken prior to share checking as the break may * cause other clients to close the file, which would affect sharing * checks. * * - Share checks must take place prior to access checks for correct * Windows semantics and to prevent unnecessary NFS delegation recalls. * * - Oplocks must be acquired after open to ensure the correct * synchronization with NFS delegation and FEM installation. * * DOS readonly bit rules * * 1. The creator of a readonly file can write to/modify the size of the file * using the original create fid, even though the file will appear as readonly * to all other fids and via a CIFS getattr call. * The readonly bit therefore cannot be set in the filesystem until the file * is closed (smb_ofile_close). It is accounted for via ofile and node flags. * * 2. A setinfo operation (using either an open fid or a path) to set/unset * readonly will be successful regardless of whether a creator of a readonly * file has an open fid (and has the special privilege mentioned in #1, * above). I.e., the creator of a readonly fid holding that fid will no longer * have a special privilege. * * 3. The DOS readonly bit affects only data and some metadata. * The following metadata can be changed regardless of the readonly bit: * - security descriptors * - DOS attributes * - timestamps * * In the current implementation, the file size cannot be changed (except for * the exceptions in #1 and #2, above). * * * DOS attribute rules * * These rules are specific to creating / opening files and directories. * How the attribute value (specifically ZERO or FILE_ATTRIBUTE_NORMAL) * should be interpreted may differ in other requests. * * - An attribute value equal to ZERO or FILE_ATTRIBUTE_NORMAL means that the * file's attributes should be cleared. * - If FILE_ATTRIBUTE_NORMAL is specified with any other attributes, * FILE_ATTRIBUTE_NORMAL is ignored. * * 1. Creating a new file * - The request attributes + FILE_ATTRIBUTE_ARCHIVE are applied to the file. * * 2. Creating a new directory * - The request attributes + FILE_ATTRIBUTE_DIRECTORY are applied to the file. * - FILE_ATTRIBUTE_ARCHIVE does not get set. * * 3. Overwriting an existing file * - the request attributes are used as search attributes. If the existing * file does not meet the search criteria access is denied. * - otherwise, applies attributes + FILE_ATTRIBUTE_ARCHIVE. * * 4. Opening an existing file or directory * The request attributes are ignored. */ static uint32_t smb_open_subr(smb_request_t *sr) { boolean_t created = B_FALSE; boolean_t last_comp_found = B_FALSE; smb_node_t *node = NULL; smb_node_t *dnode = NULL; smb_node_t *cur_node = NULL; smb_arg_open_t *op = &sr->sr_open; int rc; smb_ofile_t *of; smb_attr_t new_attr; int max_requested = 0; uint32_t max_allowed; uint32_t status = NT_STATUS_SUCCESS; int is_dir; smb_error_t err; boolean_t is_stream = B_FALSE; int lookup_flags = SMB_FOLLOW_LINKS; uint32_t uniq_fid; smb_pathname_t *pn = &op->fqi.fq_path; smb_server_t *sv = sr->sr_server; is_dir = (op->create_options & FILE_DIRECTORY_FILE) ? 1 : 0; /* * If the object being created or opened is a directory * the Disposition parameter must be one of FILE_CREATE, * FILE_OPEN, or FILE_OPEN_IF */ if (is_dir) { if ((op->create_disposition != FILE_CREATE) && (op->create_disposition != FILE_OPEN_IF) && (op->create_disposition != FILE_OPEN)) { return (NT_STATUS_INVALID_PARAMETER); } } if (op->desired_access & MAXIMUM_ALLOWED) { max_requested = 1; op->desired_access &= ~MAXIMUM_ALLOWED; } op->desired_access = smb_access_generic_to_file(op->desired_access); if (sr->session->s_file_cnt >= SMB_SESSION_OFILE_MAX) { ASSERT(sr->uid_user); cmn_err(CE_NOTE, "smbsrv[%s\\%s]: TOO_MANY_OPENED_FILES", sr->uid_user->u_domain, sr->uid_user->u_name); return (NT_STATUS_TOO_MANY_OPENED_FILES); } /* This must be NULL at this point */ sr->fid_ofile = NULL; op->devstate = 0; switch (sr->tid_tree->t_res_type & STYPE_MASK) { case STYPE_DISKTREE: case STYPE_PRINTQ: break; case STYPE_IPC: /* * Security descriptors for pipes are not implemented, * so just setup a reasonable access mask. */ op->desired_access = (READ_CONTROL | SYNCHRONIZE | FILE_READ_DATA | FILE_READ_ATTRIBUTES | FILE_WRITE_DATA | FILE_APPEND_DATA); /* * Limit the number of open pipe instances. */ if ((rc = smb_threshold_enter(&sv->sv_opipe_ct)) != 0) { status = RPC_NT_SERVER_TOO_BUSY; return (status); } /* * No further processing for IPC, we need to either * raise an exception or return success here. */ uniq_fid = SMB_UNIQ_FID(); status = smb_opipe_open(sr, uniq_fid); smb_threshold_exit(&sv->sv_opipe_ct); return (status); default: return (NT_STATUS_BAD_DEVICE_TYPE); } smb_pathname_init(sr, pn, pn->pn_path); if (!smb_pathname_validate(sr, pn)) return (sr->smb_error.status); if (strlen(pn->pn_path) >= SMB_MAXPATHLEN) { return (NT_STATUS_OBJECT_PATH_INVALID); } if (is_dir) { if (!smb_validate_dirname(sr, pn)) return (sr->smb_error.status); } else { if (!smb_validate_object_name(sr, pn)) return (sr->smb_error.status); } cur_node = op->fqi.fq_dnode ? op->fqi.fq_dnode : sr->tid_tree->t_snode; /* * if no path or filename are specified the stream should be * created on cur_node */ if (!is_dir && !pn->pn_pname && !pn->pn_fname && pn->pn_sname) { /* * Can't currently handle a stream on the tree root. * If a stream is being opened return "not found", otherwise * return "access denied". */ if (cur_node == sr->tid_tree->t_snode) { if (op->create_disposition == FILE_OPEN) { return (NT_STATUS_OBJECT_NAME_NOT_FOUND); } return (NT_STATUS_ACCESS_DENIED); } (void) snprintf(op->fqi.fq_last_comp, sizeof (op->fqi.fq_last_comp), "%s%s", cur_node->od_name, pn->pn_sname); op->fqi.fq_dnode = cur_node->n_dnode; smb_node_ref(op->fqi.fq_dnode); } else { rc = smb_pathname_reduce(sr, sr->user_cr, pn->pn_path, sr->tid_tree->t_snode, cur_node, &op->fqi.fq_dnode, op->fqi.fq_last_comp); if (rc != 0) { return (smb_errno2status(rc)); } } /* * If the access mask has only DELETE set (ignore * FILE_READ_ATTRIBUTES), then assume that this * is a request to delete the link (if a link) * and do not follow links. Otherwise, follow * the link to the target. */ if ((op->desired_access & ~FILE_READ_ATTRIBUTES) == DELETE) lookup_flags &= ~SMB_FOLLOW_LINKS; rc = smb_fsop_lookup_name(sr, zone_kcred(), lookup_flags, sr->tid_tree->t_snode, op->fqi.fq_dnode, op->fqi.fq_last_comp, &op->fqi.fq_fnode); if (rc == 0) { last_comp_found = B_TRUE; /* * Need the DOS attributes below, where we * check the search attributes (sattr). */ op->fqi.fq_fattr.sa_mask = SMB_AT_DOSATTR; rc = smb_node_getattr(sr, op->fqi.fq_fnode, zone_kcred(), NULL, &op->fqi.fq_fattr); if (rc != 0) { smb_node_release(op->fqi.fq_fnode); smb_node_release(op->fqi.fq_dnode); return (NT_STATUS_INTERNAL_ERROR); } } else if (rc == ENOENT) { last_comp_found = B_FALSE; op->fqi.fq_fnode = NULL; rc = 0; } else { smb_node_release(op->fqi.fq_dnode); return (smb_errno2status(rc)); } /* * The uniq_fid is a CIFS-server-wide unique identifier for an ofile * which is used to uniquely identify open instances for the * VFS share reservation and POSIX locks. */ uniq_fid = SMB_UNIQ_FID(); if (last_comp_found) { node = op->fqi.fq_fnode; dnode = op->fqi.fq_dnode; if (!smb_node_is_file(node) && !smb_node_is_dir(node) && !smb_node_is_symlink(node)) { smb_node_release(node); smb_node_release(dnode); return (NT_STATUS_ACCESS_DENIED); } /* * Reject this request if either: * - the target IS a directory and the client requires that * it must NOT be (required by Lotus Notes) * - the target is NOT a directory and client requires that * it MUST be. */ if (smb_node_is_dir(node)) { if (op->create_options & FILE_NON_DIRECTORY_FILE) { smb_node_release(node); smb_node_release(dnode); return (NT_STATUS_FILE_IS_A_DIRECTORY); } } else { if ((op->create_options & FILE_DIRECTORY_FILE) || (op->nt_flags & NT_CREATE_FLAG_OPEN_TARGET_DIR)) { smb_node_release(node); smb_node_release(dnode); return (NT_STATUS_NOT_A_DIRECTORY); } } /* * No more open should be accepted when "Delete on close" * flag is set. */ if (node->flags & NODE_FLAGS_DELETE_ON_CLOSE) { smb_node_release(node); smb_node_release(dnode); return (NT_STATUS_DELETE_PENDING); } /* * Specified file already exists so the operation should fail. */ if (op->create_disposition == FILE_CREATE) { smb_node_release(node); smb_node_release(dnode); return (NT_STATUS_OBJECT_NAME_COLLISION); } /* * Windows seems to check read-only access before file * sharing check. * * Check to see if the file is currently readonly (irrespective * of whether this open will make it readonly). */ if (SMB_PATHFILE_IS_READONLY(sr, node)) { /* Files data only */ if (!smb_node_is_dir(node)) { if (op->desired_access & (FILE_WRITE_DATA | FILE_APPEND_DATA)) { smb_node_release(node); smb_node_release(dnode); return (NT_STATUS_ACCESS_DENIED); } } } if ((op->create_disposition == FILE_SUPERSEDE) || (op->create_disposition == FILE_OVERWRITE_IF) || (op->create_disposition == FILE_OVERWRITE)) { if (!smb_sattr_check(op->fqi.fq_fattr.sa_dosattr, op->dattr)) { smb_node_release(node); smb_node_release(dnode); return (NT_STATUS_ACCESS_DENIED); } if (smb_node_is_dir(node)) { smb_node_release(node); smb_node_release(dnode); return (NT_STATUS_ACCESS_DENIED); } } /* MS-FSA 2.1.5.1.2 */ if (op->create_disposition == FILE_SUPERSEDE) op->desired_access |= DELETE; if ((op->create_disposition == FILE_OVERWRITE_IF) || (op->create_disposition == FILE_OVERWRITE)) op->desired_access |= FILE_WRITE_DATA; status = smb_fsop_access(sr, sr->user_cr, node, op->desired_access); if (status != NT_STATUS_SUCCESS) { smb_node_release(node); smb_node_release(dnode); /* SMB1 specific? NT_STATUS_PRIVILEGE_NOT_HELD */ if (status == NT_STATUS_PRIVILEGE_NOT_HELD) { return (status); } else { return (NT_STATUS_ACCESS_DENIED); } } if (max_requested) { smb_fsop_eaccess(sr, sr->user_cr, node, &max_allowed); op->desired_access |= max_allowed; } /* * According to MS "dochelp" mail in Mar 2015, any handle * on which read or write access is granted implicitly * gets "read attributes", even if it was not requested. * This avoids unexpected access failures later that * would happen if these were not granted. */ if ((op->desired_access & FILE_DATA_ALL) != 0) { op->desired_access |= (READ_CONTROL | FILE_READ_ATTRIBUTES); } /* * Oplock break is done prior to sharing checks as the break * may cause other clients to close the file which would * affect the sharing checks. This may block, so set the * file opening count before oplock stuff. */ smb_node_inc_opening_count(node); smb_open_oplock_break(sr, node); smb_node_wrlock(node); /* * Check for sharing violations */ status = smb_fsop_shrlock(sr->user_cr, node, uniq_fid, op->desired_access, op->share_access); if (status == NT_STATUS_SHARING_VIOLATION) { smb_node_unlock(node); smb_node_dec_opening_count(node); smb_node_release(node); smb_node_release(dnode); return (status); } /* * Go ahead with modifications as necessary. */ switch (op->create_disposition) { case FILE_SUPERSEDE: case FILE_OVERWRITE_IF: case FILE_OVERWRITE: op->dattr |= FILE_ATTRIBUTE_ARCHIVE; /* Don't apply readonly bit until smb_ofile_close */ if (op->dattr & FILE_ATTRIBUTE_READONLY) { op->created_readonly = B_TRUE; op->dattr &= ~FILE_ATTRIBUTE_READONLY; } /* * Truncate the file data here. * We set alloc_size = op->dsize later, * after we have an ofile. See: * smb_set_open_attributes */ bzero(&new_attr, sizeof (new_attr)); new_attr.sa_dosattr = op->dattr; new_attr.sa_vattr.va_size = 0; new_attr.sa_mask = SMB_AT_DOSATTR | SMB_AT_SIZE; rc = smb_fsop_setattr(sr, sr->user_cr, node, &new_attr); if (rc != 0) { smb_fsop_unshrlock(sr->user_cr, node, uniq_fid); smb_node_unlock(node); smb_node_dec_opening_count(node); smb_node_release(node); smb_node_release(dnode); return (smb_errno2status(rc)); } /* * If file is being replaced, remove existing streams */ if (SMB_IS_STREAM(node) == 0) { status = smb_fsop_remove_streams(sr, sr->user_cr, node); if (status != 0) { smb_fsop_unshrlock(sr->user_cr, node, uniq_fid); smb_node_unlock(node); smb_node_dec_opening_count(node); smb_node_release(node); smb_node_release(dnode); return (status); } } op->action_taken = SMB_OACT_TRUNCATED; break; default: /* * FILE_OPEN or FILE_OPEN_IF. */ /* * Ignore any user-specified alloc_size for * existing files, to avoid truncation in * smb_set_open_attributes */ op->dsize = 0L; op->action_taken = SMB_OACT_OPENED; break; } } else { /* Last component was not found. */ dnode = op->fqi.fq_dnode; if (is_dir == 0) is_stream = smb_is_stream_name(pn->pn_path); if ((op->create_disposition == FILE_OPEN) || (op->create_disposition == FILE_OVERWRITE)) { smb_node_release(dnode); return (NT_STATUS_OBJECT_NAME_NOT_FOUND); } if (pn->pn_fname && smb_is_invalid_filename(pn->pn_fname)) { smb_node_release(dnode); return (NT_STATUS_OBJECT_NAME_INVALID); } /* * lock the parent dir node in case another create * request to the same parent directory comes in. */ smb_node_wrlock(dnode); /* Don't apply readonly bit until smb_ofile_close */ if (op->dattr & FILE_ATTRIBUTE_READONLY) { op->dattr &= ~FILE_ATTRIBUTE_READONLY; op->created_readonly = B_TRUE; } bzero(&new_attr, sizeof (new_attr)); if ((op->crtime.tv_sec != 0) && (op->crtime.tv_sec != UINT_MAX)) { new_attr.sa_mask |= SMB_AT_CRTIME; new_attr.sa_crtime = op->crtime; } if (is_dir == 0) { op->dattr |= FILE_ATTRIBUTE_ARCHIVE; new_attr.sa_dosattr = op->dattr; new_attr.sa_vattr.va_type = VREG; new_attr.sa_vattr.va_mode = is_stream ? S_IRUSR : S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR | S_IWGRP | S_IWOTH; new_attr.sa_mask |= SMB_AT_DOSATTR | SMB_AT_TYPE | SMB_AT_MODE; /* * We set alloc_size = op->dsize later, * after we have an ofile. See: * smb_set_open_attributes */ rc = smb_fsop_create(sr, sr->user_cr, dnode, op->fqi.fq_last_comp, &new_attr, &op->fqi.fq_fnode); if (rc != 0) { smb_node_unlock(dnode); smb_node_release(dnode); return (smb_errno2status(rc)); } node = op->fqi.fq_fnode; smb_node_inc_opening_count(node); smb_node_wrlock(node); status = smb_fsop_shrlock(sr->user_cr, node, uniq_fid, op->desired_access, op->share_access); if (status == NT_STATUS_SHARING_VIOLATION) { smb_node_unlock(node); smb_node_dec_opening_count(node); smb_delete_new_object(sr); smb_node_release(node); smb_node_unlock(dnode); smb_node_release(dnode); return (status); } } else { op->dattr |= FILE_ATTRIBUTE_DIRECTORY; new_attr.sa_dosattr = op->dattr; new_attr.sa_vattr.va_type = VDIR; new_attr.sa_vattr.va_mode = 0777; new_attr.sa_mask |= SMB_AT_DOSATTR | SMB_AT_TYPE | SMB_AT_MODE; rc = smb_fsop_mkdir(sr, sr->user_cr, dnode, op->fqi.fq_last_comp, &new_attr, &op->fqi.fq_fnode); if (rc != 0) { smb_node_unlock(dnode); smb_node_release(dnode); return (smb_errno2status(rc)); } node = op->fqi.fq_fnode; smb_node_inc_opening_count(node); smb_node_wrlock(node); } created = B_TRUE; op->action_taken = SMB_OACT_CREATED; if (max_requested) { smb_fsop_eaccess(sr, sr->user_cr, node, &max_allowed); op->desired_access |= max_allowed; } /* * We created created this object (we own it) so * grant read/write attributes on this handle, * even if that was not requested. This avoids * unexpected access failures later that would * happen if these were not granted. */ op->desired_access |= (READ_CONTROL | FILE_READ_ATTRIBUTES | FILE_WRITE_ATTRIBUTES); } status = NT_STATUS_SUCCESS; of = smb_ofile_open(sr, node, op, SMB_FTYPE_DISK, uniq_fid, &err); if (of == NULL) { status = err.status; } /* * We might have blocked in smb_ofile_open long enough so a * tree disconnect might have happened. In that case, we've * just added an ofile to a tree that's disconnecting, and * need to undo that to avoid interfering with tear-down of * the tree connection. */ if (status == NT_STATUS_SUCCESS && !smb_tree_is_connected(sr->tid_tree)) { status = NT_STATUS_INVALID_PARAMETER; } /* * This MUST be done after ofile creation, so that explicitly * set timestamps can be remembered on the ofile, and the * readonly flag will be stored "pending" on the node. */ if (status == NT_STATUS_SUCCESS) { if ((rc = smb_set_open_attributes(sr, of)) != 0) { status = smb_errno2status(rc); } } if (status == NT_STATUS_SUCCESS) { /* * We've already done access checks above, * and want this call to succeed even when * !(desired_access & FILE_READ_ATTRIBUTES), * so pass kcred here. */ op->fqi.fq_fattr.sa_mask = SMB_AT_ALL; rc = smb_node_getattr(sr, node, zone_kcred(), of, &op->fqi.fq_fattr); if (rc != 0) { status = NT_STATUS_INTERNAL_ERROR; } } /* * smb_fsop_unshrlock is a no-op if node is a directory * smb_fsop_unshrlock is done in smb_ofile_close */ if (status != NT_STATUS_SUCCESS) { if (of == NULL) { smb_fsop_unshrlock(sr->user_cr, node, uniq_fid); } else { smb_ofile_close(of, 0); smb_ofile_release(of); } if (created) smb_delete_new_object(sr); smb_node_unlock(node); smb_node_dec_opening_count(node); smb_node_release(node); if (created) smb_node_unlock(dnode); smb_node_release(dnode); return (status); } /* * Propagate the write-through mode from the open params * to the node: see the notes in the function header. */ if (sr->sr_cfg->skc_sync_enable || (op->create_options & FILE_WRITE_THROUGH)) node->flags |= NODE_FLAGS_WRITE_THROUGH; /* * Set up the fileid and dosattr in open_param for response */ op->fileid = op->fqi.fq_fattr.sa_vattr.va_nodeid; op->dattr = op->fqi.fq_fattr.sa_dosattr; /* * Set up the file type in open_param for the response */ op->ftype = SMB_FTYPE_DISK; sr->smb_fid = of->f_fid; sr->fid_ofile = of; if (smb_node_is_file(node)) { smb_oplock_acquire(sr, node, of); op->dsize = op->fqi.fq_fattr.sa_vattr.va_size; } else { /* directory or symlink */ op->op_oplock_level = SMB_OPLOCK_NONE; op->dsize = 0; } smb_node_dec_opening_count(node); smb_node_unlock(node); if (created) smb_node_unlock(dnode); smb_node_release(node); smb_node_release(dnode); return (NT_STATUS_SUCCESS); }
smb_sdrc_t smb2_read(smb_request_t *sr) { smb_ofile_t *of = NULL; smb_vdb_t *vdb = NULL; struct mbuf *m = NULL; uint16_t StructSize; uint8_t Padding; uint8_t DataOff; uint32_t Length; uint64_t Offset; smb2fid_t smb2fid; uint32_t MinCount; uint32_t Channel; uint32_t Remaining; uint16_t ChanInfoOffset; uint16_t ChanInfoLength; uint32_t XferCount; uint32_t status; int rc = 0; /* * SMB2 Read request */ rc = smb_mbc_decodef( &sr->smb_data, "wb.lqqqlllww", &StructSize, /* w */ &Padding, /* b. */ &Length, /* l */ &Offset, /* q */ &smb2fid.persistent, /* q */ &smb2fid.temporal, /* q */ &MinCount, /* l */ &Channel, /* l */ &Remaining, /* l */ &ChanInfoOffset, /* w */ &ChanInfoLength); /* w */ if (rc) return (SDRC_ERROR); if (StructSize != 49) return (SDRC_ERROR); status = smb2sr_lookup_fid(sr, &smb2fid); if (status) { smb2sr_put_error(sr, status); return (SDRC_SUCCESS); } of = sr->fid_ofile; if (Length > smb2_max_rwsize) { smb2sr_put_error(sr, NT_STATUS_INVALID_PARAMETER); return (SDRC_SUCCESS); } if (MinCount > Length) MinCount = Length; /* This is automatically free'd. */ vdb = smb_srm_zalloc(sr, sizeof (*vdb)); vdb->vdb_tag = 0; vdb->vdb_uio.uio_iov = &vdb->vdb_iovec[0]; vdb->vdb_uio.uio_iovcnt = MAX_IOVEC; vdb->vdb_uio.uio_resid = Length; vdb->vdb_uio.uio_loffset = (offset_t)Offset; vdb->vdb_uio.uio_segflg = UIO_SYSSPACE; vdb->vdb_uio.uio_extflg = UIO_COPY_DEFAULT; sr->raw_data.max_bytes = Length; m = smb_mbuf_allocate(&vdb->vdb_uio); switch (of->f_tree->t_res_type & STYPE_MASK) { case STYPE_DISKTREE: if (!smb_node_is_dir(of->f_node)) { /* Check for conflicting locks. */ rc = smb_lock_range_access(sr, of->f_node, Offset, Length, B_FALSE); if (rc) { rc = ERANGE; break; } } rc = smb_fsop_read(sr, of->f_cr, of->f_node, &vdb->vdb_uio); break; case STYPE_IPC: rc = smb_opipe_read(sr, &vdb->vdb_uio); break; default: case STYPE_PRINTQ: rc = EACCES; break; } /* How much data we moved. */ XferCount = Length - vdb->vdb_uio.uio_resid; sr->raw_data.max_bytes = XferCount; smb_mbuf_trim(m, XferCount); MBC_ATTACH_MBUF(&sr->raw_data, m); /* * Checking the error return _after_ dealing with * the returned data so that if m was allocated, * it will be free'd via sr->raw_data cleanup. */ if (rc) { smb2sr_put_errno(sr, rc); return (SDRC_SUCCESS); } /* * SMB2 Read reply */ DataOff = SMB2_HDR_SIZE + 16; rc = smb_mbc_encodef( &sr->reply, "wb.lllC", 17, /* StructSize */ /* w */ DataOff, /* b. */ XferCount, /* l */ 0, /* DataRemaining */ /* l */ 0, /* reserved */ /* l */ &sr->raw_data); /* C */ if (rc) return (SDRC_ERROR); mutex_enter(&of->f_mutex); of->f_seek_pos = Offset + XferCount; mutex_exit(&of->f_mutex); return (SDRC_SUCCESS); }
/* * smb_nt_transact_notify_change * * This function is responsible for processing NOTIFY CHANGE requests. * Requests are stored in a global queue. This queue is processed when * a monitored directory is changed or client cancels one of its already * sent requests. */ smb_sdrc_t smb_nt_transact_notify_change(struct smb_request *sr, struct smb_xa *xa) { uint32_t CompletionFilter; unsigned char WatchTree; smb_node_t *node; if (smb_mbc_decodef(&xa->req_setup_mb, "lwb", &CompletionFilter, &sr->smb_fid, &WatchTree) != 0) return (SDRC_NOT_IMPLEMENTED); smbsr_lookup_file(sr); if (sr->fid_ofile == NULL) { smbsr_error(sr, NT_STATUS_INVALID_HANDLE, ERRDOS, ERRbadfid); return (SDRC_ERROR); } node = sr->fid_ofile->f_node; if (!smb_node_is_dir(node)) { /* * Notify change requests are only valid on directories. */ smbsr_error(sr, NT_STATUS_NOT_A_DIRECTORY, 0, 0); return (SDRC_ERROR); } mutex_enter(&sr->sr_mutex); switch (sr->sr_state) { case SMB_REQ_STATE_ACTIVE: node->waiting_event++; node->flags |= NODE_FLAGS_NOTIFY_CHANGE; if ((node->flags & NODE_FLAGS_CHANGED) == 0) { sr->sr_ncr.nc_node = node; sr->sr_ncr.nc_flags = CompletionFilter; if (WatchTree) sr->sr_ncr.nc_flags |= NODE_FLAGS_WATCH_TREE; sr->sr_keep = B_TRUE; sr->sr_state = SMB_REQ_STATE_WAITING_EVENT; smb_slist_insert_tail(&smb_ncr_list, sr); /* * Monitor events system-wide. * * XXX: smb_node_ref() and smb_node_release() * take &node->n_lock. May need alternate forms * of these routines if node->n_lock is taken * around calls to smb_fem_fcn_install() and * smb_fem_fcn_uninstall(). */ smb_fem_fcn_install(node); mutex_exit(&sr->sr_mutex); return (SDRC_SR_KEPT); } else { /* node already changed, reply immediately */ if (--node->waiting_event == 0) node->flags &= ~(NODE_FLAGS_NOTIFY_CHANGE | NODE_FLAGS_CHANGED); mutex_exit(&sr->sr_mutex); return (SDRC_SUCCESS); } case SMB_REQ_STATE_CANCELED: mutex_exit(&sr->sr_mutex); smbsr_error(sr, NT_STATUS_CANCELLED, 0, 0); return (SDRC_ERROR); default: ASSERT(0); mutex_exit(&sr->sr_mutex); return (SDRC_SUCCESS); } }
smb_sdrc_t smb_com_nt_create_andx(struct smb_request *sr) { struct open_param *op = &sr->arg.open; unsigned char DirFlag; smb_attr_t attr; smb_node_t *node; int rc; if ((op->create_options & FILE_DELETE_ON_CLOSE) && !(op->desired_access & DELETE)) { smbsr_error(sr, NT_STATUS_INVALID_PARAMETER, ERRDOS, ERRbadaccess); return (SDRC_ERROR); } if (op->create_disposition > FILE_MAXIMUM_DISPOSITION) { smbsr_error(sr, NT_STATUS_INVALID_PARAMETER, ERRDOS, ERRbadaccess); return (SDRC_ERROR); } if (op->dattr & FILE_FLAG_WRITE_THROUGH) op->create_options |= FILE_WRITE_THROUGH; if (op->dattr & FILE_FLAG_DELETE_ON_CLOSE) op->create_options |= FILE_DELETE_ON_CLOSE; if (op->dattr & FILE_FLAG_BACKUP_SEMANTICS) op->create_options |= FILE_OPEN_FOR_BACKUP_INTENT; if (op->create_options & FILE_OPEN_FOR_BACKUP_INTENT) sr->user_cr = smb_user_getprivcred(sr->uid_user); if (op->rootdirfid == 0) { op->fqi.fq_dnode = sr->tid_tree->t_snode; } else { op->dir = smb_ofile_lookup_by_fid(sr->tid_tree, (uint16_t)op->rootdirfid); if (op->dir == NULL) { smbsr_error(sr, NT_STATUS_INVALID_HANDLE, ERRDOS, ERRbadfid); return (SDRC_ERROR); } op->fqi.fq_dnode = op->dir->f_node; } op->op_oplock_levelII = B_TRUE; if (smb_common_open(sr) != NT_STATUS_SUCCESS) return (SDRC_ERROR); switch (sr->tid_tree->t_res_type & STYPE_MASK) { case STYPE_DISKTREE: case STYPE_PRINTQ: if (op->create_options & FILE_DELETE_ON_CLOSE) smb_ofile_set_delete_on_close(sr->fid_ofile); node = sr->fid_ofile->f_node; DirFlag = smb_node_is_dir(node) ? 1 : 0; if (smb_node_getattr(sr, node, &attr) != 0) { smbsr_error(sr, NT_STATUS_INTERNAL_ERROR, ERRDOS, ERROR_INTERNAL_ERROR); return (SDRC_ERROR); } rc = smbsr_encode_result(sr, 34, 0, "bb.wbwlTTTTlqqwwbw", 34, sr->andx_com, 0x67, op->op_oplock_level, sr->smb_fid, op->action_taken, &attr.sa_crtime, &attr.sa_vattr.va_atime, &attr.sa_vattr.va_mtime, &attr.sa_vattr.va_ctime, op->dattr & FILE_ATTRIBUTE_MASK, attr.sa_allocsz, attr.sa_vattr.va_size, op->ftype, op->devstate, DirFlag, 0); break; case STYPE_IPC: rc = smbsr_encode_result(sr, 34, 0, "bb.wbwlqqqqlqqwwbw", 34, sr->andx_com, 0x67, 0, sr->smb_fid, op->action_taken, 0LL, 0LL, 0LL, 0LL, FILE_ATTRIBUTE_NORMAL, 0x1000LL, 0LL, op->ftype, op->devstate, 0, 0); break; default: smbsr_error(sr, NT_STATUS_INVALID_DEVICE_REQUEST, ERRDOS, ERROR_INVALID_FUNCTION); return (SDRC_ERROR); } return ((rc == 0) ? SDRC_SUCCESS : SDRC_ERROR); }
/* * smb_make_link * * Creating a hard link (adding an additional name) for a file. * * If the source and destination are identical, we go through all * the checks but we don't create a link. * * If the file is a symlink we create the hardlink on the target * of the symlink (i.e. use SMB_FOLLOW_LINKS when looking up src). * If the target of the symlink does not exist we fail with ENOENT. * * Returns NT status values. * * Similar to smb_common_rename() above. */ uint32_t smb_make_link(smb_request_t *sr, smb_fqi_t *src_fqi, smb_fqi_t *dst_fqi) { smb_node_t *tnode; char *path; int rc; tnode = sr->tid_tree->t_snode; path = dst_fqi->fq_path.pn_path; /* Cannnot create link on named stream */ if (smb_is_stream_name(src_fqi->fq_path.pn_path) || smb_is_stream_name(dst_fqi->fq_path.pn_path)) { return (NT_STATUS_INVALID_PARAMETER); } /* The source node may already have been provided */ if (src_fqi->fq_fnode) { smb_node_start_crit(src_fqi->fq_fnode, RW_READER); smb_node_ref(src_fqi->fq_fnode); smb_node_ref(src_fqi->fq_dnode); } else { /* lookup and validate src node */ rc = smb_rename_lookup_src(sr); if (rc != 0) return (smb_rename_errno2status(rc)); } /* Not valid to create hardlink for directory */ if (smb_node_is_dir(src_fqi->fq_fnode)) { smb_rename_release_src(sr); return (NT_STATUS_FILE_IS_A_DIRECTORY); } /* * Find the destination dnode and last component. * May already be provided, i.e. when called via * SMB1 trans2 setinfo. */ if (dst_fqi->fq_dnode) { smb_node_ref(dst_fqi->fq_dnode); } else { rc = smb_pathname_reduce(sr, sr->user_cr, path, tnode, tnode, &dst_fqi->fq_dnode, dst_fqi->fq_last_comp); if (rc != 0) { smb_rename_release_src(sr); return (smb_rename_errno2status(rc)); } } /* If CI name match in same directory, we're done */ if ((src_fqi->fq_dnode == dst_fqi->fq_dnode) && (smb_strcasecmp(src_fqi->fq_fnode->od_name, dst_fqi->fq_last_comp, 0) == 0)) { smb_rename_release_src(sr); smb_node_release(dst_fqi->fq_dnode); return (0); } if (smb_is_invalid_filename(dst_fqi->fq_last_comp)) { smb_rename_release_src(sr); smb_node_release(dst_fqi->fq_dnode); return (NT_STATUS_OBJECT_NAME_INVALID); } /* Lookup the destination node. It MUST NOT exist. */ rc = smb_fsop_lookup(sr, sr->user_cr, 0, tnode, dst_fqi->fq_dnode, dst_fqi->fq_last_comp, &dst_fqi->fq_fnode); if (rc == 0) { smb_node_release(dst_fqi->fq_fnode); rc = EEXIST; } if (rc != ENOENT) { smb_rename_release_src(sr); smb_node_release(dst_fqi->fq_dnode); return (smb_rename_errno2status(rc)); } rc = smb_fsop_link(sr, sr->user_cr, src_fqi->fq_fnode, dst_fqi->fq_dnode, dst_fqi->fq_last_comp); if (rc == 0) { smb_node_notify_change(dst_fqi->fq_dnode, FILE_ACTION_ADDED, dst_fqi->fq_last_comp); } smb_rename_release_src(sr); smb_node_release(dst_fqi->fq_dnode); return (smb_rename_errno2status(rc)); }
smb_sdrc_t smb_nt_transact_create(smb_request_t *sr, smb_xa_t *xa) { struct open_param *op = &sr->arg.open; uint8_t DirFlag; smb_attr_t attr; smb_ofile_t *of; int rc; if ((op->create_options & FILE_DELETE_ON_CLOSE) && !(op->desired_access & DELETE)) { smbsr_error(sr, NT_STATUS_INVALID_PARAMETER, ERRDOS, ERRbadaccess); return (SDRC_ERROR); } if (op->create_disposition > FILE_MAXIMUM_DISPOSITION) { smbsr_error(sr, NT_STATUS_INVALID_PARAMETER, ERRDOS, ERRbadaccess); return (SDRC_ERROR); } if (op->dattr & FILE_FLAG_WRITE_THROUGH) op->create_options |= FILE_WRITE_THROUGH; if (op->dattr & FILE_FLAG_DELETE_ON_CLOSE) op->create_options |= FILE_DELETE_ON_CLOSE; if (op->dattr & FILE_FLAG_BACKUP_SEMANTICS) op->create_options |= FILE_OPEN_FOR_BACKUP_INTENT; if (op->create_options & FILE_OPEN_FOR_BACKUP_INTENT) sr->user_cr = smb_user_getprivcred(sr->uid_user); if (op->rootdirfid == 0) { op->fqi.fq_dnode = sr->tid_tree->t_snode; } else { op->dir = smb_ofile_lookup_by_fid(sr, (uint16_t)op->rootdirfid); if (op->dir == NULL) { smbsr_error(sr, NT_STATUS_INVALID_HANDLE, ERRDOS, ERRbadfid); return (SDRC_ERROR); } op->fqi.fq_dnode = op->dir->f_node; } op->op_oplock_levelII = B_TRUE; if (smb_common_open(sr) != NT_STATUS_SUCCESS) return (SDRC_ERROR); /* * NB: after the above smb_common_open() success, * we have a handle allocated (sr->fid_ofile). * If we don't return success, we must close it. */ of = sr->fid_ofile; switch (sr->tid_tree->t_res_type & STYPE_MASK) { case STYPE_DISKTREE: case STYPE_PRINTQ: if (op->create_options & FILE_DELETE_ON_CLOSE) smb_ofile_set_delete_on_close(of); DirFlag = smb_node_is_dir(of->f_node) ? 1 : 0; bzero(&attr, sizeof (attr)); attr.sa_mask = SMB_AT_ALL; rc = smb_node_getattr(sr, of->f_node, of->f_cr, of, &attr); if (rc != 0) { smbsr_errno(sr, rc); goto errout; } (void) smb_mbc_encodef(&xa->rep_param_mb, "b.wllTTTTlqqwwb", op->op_oplock_level, sr->smb_fid, op->action_taken, 0, /* EaErrorOffset */ &attr.sa_crtime, &attr.sa_vattr.va_atime, &attr.sa_vattr.va_mtime, &attr.sa_vattr.va_ctime, op->dattr & FILE_ATTRIBUTE_MASK, attr.sa_allocsz, attr.sa_vattr.va_size, op->ftype, op->devstate, DirFlag); break; case STYPE_IPC: bzero(&attr, sizeof (smb_attr_t)); (void) smb_mbc_encodef(&xa->rep_param_mb, "b.wllTTTTlqqwwb", 0, sr->smb_fid, op->action_taken, 0, /* EaErrorOffset */ &attr.sa_crtime, &attr.sa_vattr.va_atime, &attr.sa_vattr.va_mtime, &attr.sa_vattr.va_ctime, op->dattr, 0x1000LL, 0LL, op->ftype, op->devstate, 0); break; default: smbsr_error(sr, NT_STATUS_INVALID_DEVICE_REQUEST, ERRDOS, ERROR_INVALID_FUNCTION); goto errout; } return (SDRC_SUCCESS); errout: smb_ofile_close(of, 0); return (SDRC_ERROR); }
smb_sdrc_t smb_com_check_directory(smb_request_t *sr) { int rc; smb_fqi_t *fqi; smb_node_t *tnode; smb_node_t *node; char *path; smb_pathname_t *pn; if (STYPE_ISIPC(sr->tid_tree->t_res_type)) { smbsr_error(sr, NT_STATUS_ACCESS_DENIED, ERRDOS, ERROR_ACCESS_DENIED); return (SDRC_ERROR); } fqi = &sr->arg.dirop.fqi; pn = &fqi->fq_path; if (pn->pn_path[0] == '\0') { rc = smbsr_encode_empty_result(sr); return ((rc == 0) ? SDRC_SUCCESS : SDRC_ERROR); } smb_pathname_init(sr, pn, pn->pn_path); if (!smb_pathname_validate(sr, pn) || !smb_validate_dirname(sr, pn)) { return (SDRC_ERROR); } path = pn->pn_path; tnode = sr->tid_tree->t_snode; rc = smb_pathname_reduce(sr, sr->user_cr, path, tnode, tnode, &fqi->fq_dnode, fqi->fq_last_comp); if (rc != 0) { smbsr_errno(sr, rc); return (SDRC_ERROR); } rc = smb_fsop_lookup(sr, sr->user_cr, SMB_FOLLOW_LINKS, tnode, fqi->fq_dnode, fqi->fq_last_comp, &fqi->fq_fnode); smb_node_release(fqi->fq_dnode); if (rc != 0) { if (rc == ENOENT) smbsr_error(sr, NT_STATUS_OBJECT_NAME_NOT_FOUND, ERRDOS, ERROR_PATH_NOT_FOUND); else smbsr_errno(sr, rc); return (SDRC_ERROR); } node = fqi->fq_fnode; if (!smb_node_is_dir(node)) { smbsr_error(sr, NT_STATUS_NOT_A_DIRECTORY, ERRDOS, ERROR_PATH_NOT_FOUND); smb_node_release(node); return (SDRC_ERROR); } if ((sr->smb_flg2 & SMB_FLAGS2_DFS) && smb_node_is_dfslink(node)) { smbsr_error(sr, NT_STATUS_PATH_NOT_COVERED, ERRSRV, ERRbadpath); smb_node_release(node); return (SDRC_ERROR); } rc = smb_fsop_access(sr, sr->user_cr, node, FILE_TRAVERSE); smb_node_release(node); if (rc != 0) { smbsr_error(sr, NT_STATUS_ACCESS_DENIED, ERRDOS, ERROR_ACCESS_DENIED); return (SDRC_ERROR); } rc = smbsr_encode_empty_result(sr); return ((rc == 0) ? SDRC_SUCCESS : SDRC_ERROR); }
smb_sdrc_t smb_com_delete_directory(smb_request_t *sr) { int rc; uint32_t flags = 0; smb_fqi_t *fqi; smb_node_t *tnode; if (!STYPE_ISDSK(sr->tid_tree->t_res_type)) { smbsr_error(sr, NT_STATUS_ACCESS_DENIED, ERRDOS, ERROR_ACCESS_DENIED); return (SDRC_ERROR); } fqi = &sr->arg.dirop.fqi; tnode = sr->tid_tree->t_snode; smb_pathname_init(sr, &fqi->fq_path, fqi->fq_path.pn_path); if (!smb_pathname_validate(sr, &fqi->fq_path) || !smb_validate_dirname(sr, &fqi->fq_path)) { return (SDRC_ERROR); } rc = smb_pathname_reduce(sr, sr->user_cr, fqi->fq_path.pn_path, tnode, tnode, &fqi->fq_dnode, fqi->fq_last_comp); if (rc != 0) { smbsr_errno(sr, rc); return (SDRC_ERROR); } rc = smb_fsop_lookup(sr, sr->user_cr, SMB_FOLLOW_LINKS, tnode, fqi->fq_dnode, fqi->fq_last_comp, &fqi->fq_fnode); if (rc != 0) { if (rc == ENOENT) smbsr_error(sr, NT_STATUS_OBJECT_NAME_NOT_FOUND, ERRDOS, ERROR_FILE_NOT_FOUND); else smbsr_errno(sr, rc); smb_node_release(fqi->fq_dnode); return (SDRC_ERROR); } /* * Delete should fail if this is the root of a share * or a DFS link */ if ((fqi->fq_fnode == tnode) || smb_node_is_dfslink(fqi->fq_fnode)) { smbsr_error(sr, NT_STATUS_ACCESS_DENIED, ERRDOS, ERROR_ACCESS_DENIED); smb_node_release(fqi->fq_dnode); smb_node_release(fqi->fq_fnode); return (SDRC_ERROR); } if (!smb_node_is_dir(fqi->fq_fnode)) { smbsr_error(sr, NT_STATUS_NOT_A_DIRECTORY, ERRDOS, ERROR_PATH_NOT_FOUND); smb_node_release(fqi->fq_dnode); smb_node_release(fqi->fq_fnode); return (SDRC_ERROR); } /* * Using kcred because we just want the DOS attrs * and don't want access errors for this. */ fqi->fq_fattr.sa_mask = SMB_AT_DOSATTR; rc = smb_node_getattr(sr, fqi->fq_fnode, zone_kcred(), NULL, &fqi->fq_fattr); if (rc != 0) { smbsr_errno(sr, rc); smb_node_release(fqi->fq_dnode); smb_node_release(fqi->fq_fnode); return (SDRC_ERROR); } if ((fqi->fq_fattr.sa_dosattr & FILE_ATTRIBUTE_READONLY) || (smb_fsop_access(sr, sr->user_cr, fqi->fq_fnode, DELETE) != NT_STATUS_SUCCESS)) { smbsr_error(sr, NT_STATUS_CANNOT_DELETE, ERRDOS, ERROR_ACCESS_DENIED); smb_node_release(fqi->fq_dnode); smb_node_release(fqi->fq_fnode); return (SDRC_ERROR); } if (SMB_TREE_SUPPORTS_CATIA(sr)) flags |= SMB_CATIA; rc = smb_fsop_rmdir(sr, sr->user_cr, fqi->fq_dnode, fqi->fq_fnode->od_name, flags); smb_node_release(fqi->fq_fnode); smb_node_release(fqi->fq_dnode); if (rc != 0) { if (rc == EEXIST) smbsr_error(sr, NT_STATUS_DIRECTORY_NOT_EMPTY, ERRDOS, ERROR_DIR_NOT_EMPTY); else smbsr_errno(sr, rc); return (SDRC_ERROR); } rc = smbsr_encode_empty_result(sr); return ((rc == 0) ? SDRC_SUCCESS : SDRC_ERROR); }