vfs module for delete files by mime type

Дейтер Александр Ва Дейтер Александр Ва
Thu Nov 11 12:19:01 GMT 2004


Hi,

I'm working on my first vfs module for samba and would like to share it 
with community. This module controlling the MIME file types that users 
write on samba server and delete it, if mime type does
not match with rules. Module use library libmagic (file-4.x) for 
determine file type.

Sample smb.conf:

[test]
  path = /path/to/test
  read only = No
  vfs objects = magic_perms
  magic_perms:filetypes = application/msword, image/jpeg, application/pdf

allow writes only MS Office files, jpeg and PDF documents.

Thanks a lot!



-------------- next part --------------
/* 
 * VFS module to delete files at closing if their mime type does
 * not match with rules.
 *
 * Copyright (C) Alex Deiter 2004
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *  
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *  
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

/*
 * Short instructions:
 *
 * Build it with:
 *	LDFLAGS = -L/usr/local/lib -lmagic
 *	CFLAGS  = -I/usr/local/include
 *
 * Sample smb.conf:
 *
 * [test]
 *	path = /path/to/test
 *	vfs objects = magic_perms
 *	magic_perms: filetypes = application/msword, image/jpeg, application/pdf
 *	writeable = yes
 */

#include "includes.h"
#include <string.h>
#include <magic.h>

#define MODULE_NAME	"magic_perms"
#define FILETYPES	"filetypes"

static int magic_perms_close(vfs_handle_struct *handle, files_struct *fsp, int fd)
{
	int i;
	magic_t mc;
	const char *ftype;
	const char **ftypes;
	pstring filepath;
	int retval = SMB_VFS_NEXT_CLOSE(handle, fsp, fd);

	if ( !fsp->modified ) {
		DEBUG(10,("%s file %s was not modified.\n",
			MODULE_NAME, fsp->fsp_name));
		return retval;
	}

	if ( fsp->is_directory ) {
		DEBUG(10,("%s %s like directory.\n",
			MODULE_NAME, fsp->fsp_name));
		return retval;
	}

	ftypes = lp_parm_string_list(SNUM(handle->conn),
		(handle->param ? handle->param : MODULE_NAME),
		FILETYPES, NULL);

	if (!ftypes) {
		DEBUG(2,("%s empty file type list.\n",
			MODULE_NAME));
		return retval;
	}

        pstrcpy(filepath, fsp->conn->connectpath);
        pstrcat(filepath, "/"); 
        pstrcat(filepath, fsp->fsp_name);        

	DEBUG(10,("%s get full path for %s: %s.\n",
		MODULE_NAME, fsp->fsp_name, filepath));
	
	mc = magic_open(MAGIC_PRESERVE_ATIME|MAGIC_MIME);
	if (mc == NULL) {
		DEBUG(2,("%s cannot open magic file: %s.\n",
			MODULE_NAME, strerror(errno)));
		return retval;
	}

	if (magic_load(mc, NULL) == -1) {
		DEBUG(2,("%s cannot load magic file: %s.\n",
			MODULE_NAME, magic_error(mc)));
		magic_close(mc);
		return retval;
	}

	ftype = magic_file(mc, filepath);
	if (ftype == NULL) {
		DEBUG(2,("%s cannot get type for file %s.\n",
			MODULE_NAME, filepath));
		magic_close(mc);
		return retval;
	}

	magic_close(mc);

	DEBUG(10,("%s get magic for %s: %s.\n",
		MODULE_NAME, filepath, ftype));

	for (i=0; ftypes[i]; i++) {
		if (strcmp(ftype, ftypes[i]) == 0) {
			DEBUG(2,("%s match %s for %s.\n",
				MODULE_NAME, ftype, filepath));
			return retval;
		}
	}

	DEBUG(1,("%s unlink file %s with type %s.\n",
		MODULE_NAME, filepath, ftype));
	unlink(filepath);
	return retval;
}

static vfs_op_tuple magic_perms_op_tuples[] = {
	{SMB_VFS_OP(magic_perms_close),	SMB_VFS_OP_CLOSE,	SMB_VFS_LAYER_TRANSPARENT},
	{SMB_VFS_OP(NULL),		SMB_VFS_OP_NOOP,	SMB_VFS_LAYER_NOOP}

};

NTSTATUS init_module(void)
{
	return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, MODULE_NAME, magic_perms_op_tuples);
}


More information about the samba-technical mailing list